blob: aaaf10d48f7dec1b5b8f17dd2ccfbbe58415ad71 [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka5fb2972010-01-12 13:51:38 +01005 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000013#include <linux/interrupt.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010014#include <linux/module.h>
15#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020016#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010017#include <linux/spinlock.h>
18#include <linux/list.h>
19#include <linux/pci.h>
20#include <linux/delay.h>
21#include <linux/completion.h>
22#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010024#include <net/mac80211.h>
25#include <linux/moduleparam.h>
26#include <linux/firmware.h>
27#include <linux/workqueue.h>
28
29#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
30#define MWL8K_NAME KBUILD_MODNAME
Yogesh Ashok Powar00e8e692011-12-20 11:39:29 +053031#define MWL8K_VERSION "0.13"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010032
Brian Cavagnolo0863ade2010-11-12 17:23:50 -080033/* Module parameters */
Rusty Russelleb939922011-12-19 14:08:01 +000034static bool ap_mode_default;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -080035module_param(ap_mode_default, bool, 0);
36MODULE_PARM_DESC(ap_mode_default,
37 "Set to 1 to make ap mode the default instead of sta mode");
38
Lennert Buytenheka66098d2009-03-10 10:13:33 +010039/* Register definitions */
40#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020041#define MWL8K_MODE_STA 0x0000005a
42#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010043#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020044#define MWL8K_FWSTA_READY 0xf0f1f2f4
45#define MWL8K_FWAP_READY 0xf1f2f4a5
46#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010047#define MWL8K_HIU_SCRATCH 0x00000c40
48
49/* Host->device communications */
50#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
51#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
52#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
53#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
54#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020055#define MWL8K_H2A_INT_DUMMY (1 << 20)
56#define MWL8K_H2A_INT_RESET (1 << 15)
57#define MWL8K_H2A_INT_DOORBELL (1 << 1)
58#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010059
60/* Device->host communications */
61#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
62#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
63#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
64#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
65#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020066#define MWL8K_A2H_INT_DUMMY (1 << 20)
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -070067#define MWL8K_A2H_INT_BA_WATCHDOG (1 << 14)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020068#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
69#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
70#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
71#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
72#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
73#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
74#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
75#define MWL8K_A2H_INT_RX_READY (1 << 1)
76#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010077
Pradeep Nemavat566875d2011-04-21 16:34:57 +053078/* HW micro second timer register
79 * located at offset 0xA600. This
80 * will be used to timestamp tx
81 * packets.
82 */
83
84#define MWL8K_HW_TIMER_REGISTER 0x0000a600
85
Lennert Buytenheka66098d2009-03-10 10:13:33 +010086#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
87 MWL8K_A2H_INT_CHNL_SWITCHED | \
88 MWL8K_A2H_INT_QUEUE_EMPTY | \
89 MWL8K_A2H_INT_RADAR_DETECT | \
90 MWL8K_A2H_INT_RADIO_ON | \
91 MWL8K_A2H_INT_RADIO_OFF | \
92 MWL8K_A2H_INT_MAC_EVENT | \
93 MWL8K_A2H_INT_OPC_DONE | \
94 MWL8K_A2H_INT_RX_READY | \
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -070095 MWL8K_A2H_INT_TX_DONE | \
96 MWL8K_A2H_INT_BA_WATCHDOG)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010097
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098#define MWL8K_RX_QUEUES 1
Brian Cavagnoloe6007072011-03-17 11:58:44 -070099#define MWL8K_TX_WMM_QUEUES 4
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700100#define MWL8K_MAX_AMPDU_QUEUES 8
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700101#define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
102#define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +0530104/* txpriorities are mapped with hw queues.
105 * Each hw queue has a txpriority.
106 */
107#define TOTAL_HW_TX_QUEUES 8
108
109/* Each HW queue can have one AMPDU stream.
110 * But, because one of the hw queue is reserved,
111 * maximum AMPDU queues that can be created are
112 * one short of total tx queues.
113 */
114#define MWL8K_NUM_AMPDU_STREAMS (TOTAL_HW_TX_QUEUES - 1)
115
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200116struct rxd_ops {
117 int rxd_size;
118 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
119 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100120 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400121 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200122};
123
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200124struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200125 char *part_name;
126 char *helper_image;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800127 char *fw_image_sta;
128 char *fw_image_ap;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100129 struct rxd_ops *ap_rxd_ops;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -0800130 u32 fw_api_ap;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200131};
132
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100133struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200134 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100135
136 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200137 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100138
139 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200140 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100141
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200142 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200143 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200144 struct {
145 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900146 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200147 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100148};
149
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100150struct mwl8k_tx_queue {
151 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200152 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100153
154 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200155 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100156
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200157 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200158 struct mwl8k_tx_desc *txd;
159 dma_addr_t txd_dma;
160 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100161};
162
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700163enum {
164 AMPDU_NO_STREAM,
165 AMPDU_STREAM_NEW,
166 AMPDU_STREAM_IN_PROGRESS,
167 AMPDU_STREAM_ACTIVE,
168};
169
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700170struct mwl8k_ampdu_stream {
171 struct ieee80211_sta *sta;
172 u8 tid;
173 u8 state;
174 u8 idx;
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700175};
176
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100178 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100179 struct pci_dev *pdev;
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +0530180 int irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100181
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200182 struct mwl8k_device_info *device_info;
183
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100184 void __iomem *sram;
185 void __iomem *regs;
186
187 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800188 const struct firmware *fw_helper;
189 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100190
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100191 /* hardware/firmware parameters */
192 bool ap_fw;
193 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100194 struct ieee80211_supported_band band_24;
195 struct ieee80211_channel channels_24[14];
196 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100197 struct ieee80211_supported_band band_50;
198 struct ieee80211_channel channels_50[4];
199 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100200 u32 ap_macids_supported;
201 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100202
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700203 /* Ampdu stream information */
204 u8 num_ampdu_queues;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700205 spinlock_t stream_lock;
206 struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700207 struct work_struct watchdog_ba_handle;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700208
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200209 /* firmware access */
210 struct mutex fw_mutex;
211 struct task_struct *fw_mutex_owner;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +0530212 struct task_struct *hw_restart_owner;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200213 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200214 struct completion *hostcmd_wait;
215
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;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100235
236 /*
237 * Running count of TX packets in flight, to avoid
238 * iterating over the transmit rings each time.
239 */
240 int pending_tx_pkts;
241
242 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700243 struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
244 u32 txq_offset[MWL8K_MAX_TX_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100245
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200246 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200247 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200248 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200249 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100250
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100251 /* XXX need to convert this to handle multiple interfaces */
252 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200253 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100254 struct sk_buff *beacon_skb;
255
256 /*
257 * This FJ worker has to be global as it is scheduled from the
258 * RX handler. At this point we don't know which interface it
259 * belongs to until the list of bssids waiting to complete join
260 * is checked.
261 */
262 struct work_struct finalize_join_worker;
263
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100264 /* Tasklet to perform TX reclaim. */
265 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100266
267 /* Tasklet to perform RX. */
268 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400269
270 /* Most recently reported noise in dBm */
271 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800272
273 /*
274 * preserve the queue configurations so they can be restored if/when
275 * the firmware image is swapped.
276 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700277 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800278
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +0530279 /* To perform the task of reloading the firmware */
280 struct work_struct fw_reload;
281 bool hw_restart_in_progress;
282
Brian Cavagnolo99020472010-11-12 17:23:52 -0800283 /* async firmware loading state */
284 unsigned fw_state;
285 char *fw_pref;
286 char *fw_alt;
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530287 bool is_8764;
Brian Cavagnolo99020472010-11-12 17:23:52 -0800288 struct completion firmware_loading_complete;
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +0530289
290 /* bitmap of running BSSes */
291 u32 running_bsses;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100292};
293
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800294#define MAX_WEP_KEY_LEN 13
295#define NUM_WEP_KEYS 4
296
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100297/* Per interface specific private data */
298struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100299 struct list_head list;
300 struct ieee80211_vif *vif;
301
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100302 /* Firmware macid for this vif. */
303 int macid;
304
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100305 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100306 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800307
308 /* Saved WEP keys */
309 struct {
310 u8 enabled;
311 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
312 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800313
314 /* BSSID */
315 u8 bssid[ETH_ALEN];
316
317 /* A flag to indicate is HW crypto is enabled for this bssid */
318 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100319};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200320#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800321#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100322
Brian Cavagnolod0805c12011-04-12 11:06:28 -0700323struct tx_traffic_info {
324 u32 start_time;
325 u32 pkts;
326};
327
328#define MWL8K_MAX_TID 8
Lennert Buytenheka6804002010-01-04 21:55:42 +0100329struct mwl8k_sta {
330 /* Index into station database. Returned by UPDATE_STADB. */
331 u8 peer_id;
Nishant Sarmukadam170335432011-03-17 11:58:48 -0700332 u8 is_ampdu_allowed;
Brian Cavagnolod0805c12011-04-12 11:06:28 -0700333 struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
Lennert Buytenheka6804002010-01-04 21:55:42 +0100334};
335#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
336
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100337static const struct ieee80211_channel mwl8k_channels_24[] = {
Jonas Gorskid786f672013-02-08 16:07:25 +0100338 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2412, .hw_value = 1, },
339 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2417, .hw_value = 2, },
340 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2422, .hw_value = 3, },
341 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2427, .hw_value = 4, },
342 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2432, .hw_value = 5, },
343 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2437, .hw_value = 6, },
344 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2442, .hw_value = 7, },
345 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2447, .hw_value = 8, },
346 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2452, .hw_value = 9, },
347 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2457, .hw_value = 10, },
348 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2462, .hw_value = 11, },
349 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2467, .hw_value = 12, },
350 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2472, .hw_value = 13, },
351 { .band = IEEE80211_BAND_2GHZ, .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100352};
353
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100354static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100355 { .bitrate = 10, .hw_value = 2, },
356 { .bitrate = 20, .hw_value = 4, },
357 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200358 { .bitrate = 110, .hw_value = 22, },
359 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100360 { .bitrate = 60, .hw_value = 12, },
361 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100362 { .bitrate = 120, .hw_value = 24, },
363 { .bitrate = 180, .hw_value = 36, },
364 { .bitrate = 240, .hw_value = 48, },
365 { .bitrate = 360, .hw_value = 72, },
366 { .bitrate = 480, .hw_value = 96, },
367 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100368 { .bitrate = 720, .hw_value = 144, },
369};
370
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100371static const struct ieee80211_channel mwl8k_channels_50[] = {
Jonas Gorskid786f672013-02-08 16:07:25 +0100372 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
373 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
374 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
375 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100376};
377
378static const struct ieee80211_rate mwl8k_rates_50[] = {
379 { .bitrate = 60, .hw_value = 12, },
380 { .bitrate = 90, .hw_value = 18, },
381 { .bitrate = 120, .hw_value = 24, },
382 { .bitrate = 180, .hw_value = 36, },
383 { .bitrate = 240, .hw_value = 48, },
384 { .bitrate = 360, .hw_value = 72, },
385 { .bitrate = 480, .hw_value = 96, },
386 { .bitrate = 540, .hw_value = 108, },
387 { .bitrate = 720, .hw_value = 144, },
388};
389
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100390/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100391#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800392#define MWL8K_CMD_SET 0x0001
393#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100394
395/* Firmware command codes */
396#define MWL8K_CMD_CODE_DNLD 0x0001
397#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200398#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100399#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
400#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200401#define MWL8K_CMD_RADIO_CONTROL 0x001c
402#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800403#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200404#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100405#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100406#define MWL8K_CMD_SET_PRE_SCAN 0x0107
407#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200408#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100409#define MWL8K_CMD_SET_AID 0x010d
410#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200411#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100412#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200413#define MWL8K_CMD_SET_SLOT 0x0114
414#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
415#define MWL8K_CMD_SET_WMM_MODE 0x0123
416#define MWL8K_CMD_MIMO_CONFIG 0x0125
417#define MWL8K_CMD_USE_FIXED_RATE 0x0126
418#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100419#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200420#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700421#define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +0530422#define MWL8K_CMD_DEL_MAC_ADDR 0x0206 /* per-vif */
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100423#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
424#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800425#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200426#define MWL8K_CMD_UPDATE_STADB 0x1123
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700427#define MWL8K_CMD_BASTREAM 0x1125
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100428
John W. Linvilleb6037422010-07-20 13:55:00 -0400429static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100430{
John W. Linvilleb6037422010-07-20 13:55:00 -0400431 u16 command = le16_to_cpu(cmd);
432
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100433#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
434 snprintf(buf, bufsize, "%s", #x);\
435 return buf;\
436 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400437 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100438 MWL8K_CMDNAME(CODE_DNLD);
439 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200440 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100441 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
442 MWL8K_CMDNAME(GET_STAT);
443 MWL8K_CMDNAME(RADIO_CONTROL);
444 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800445 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200446 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100447 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100448 MWL8K_CMDNAME(SET_PRE_SCAN);
449 MWL8K_CMDNAME(SET_POST_SCAN);
450 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100451 MWL8K_CMDNAME(SET_AID);
452 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200453 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100454 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200455 MWL8K_CMDNAME(SET_SLOT);
456 MWL8K_CMDNAME(SET_EDCA_PARAMS);
457 MWL8K_CMDNAME(SET_WMM_MODE);
458 MWL8K_CMDNAME(MIMO_CONFIG);
459 MWL8K_CMDNAME(USE_FIXED_RATE);
460 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200461 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200462 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100463 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100464 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800465 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200466 MWL8K_CMDNAME(UPDATE_STADB);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700467 MWL8K_CMDNAME(BASTREAM);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700468 MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100469 default:
470 snprintf(buf, bufsize, "0x%x", cmd);
471 }
472#undef MWL8K_CMDNAME
473
474 return buf;
475}
476
477/* Hardware and firmware reset */
478static void mwl8k_hw_reset(struct mwl8k_priv *priv)
479{
480 iowrite32(MWL8K_H2A_INT_RESET,
481 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
482 iowrite32(MWL8K_H2A_INT_RESET,
483 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
484 msleep(20);
485}
486
487/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800488static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100489{
490 if (*fw == NULL)
491 return;
492 release_firmware(*fw);
493 *fw = NULL;
494}
495
496static void mwl8k_release_firmware(struct mwl8k_priv *priv)
497{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100498 mwl8k_release_fw(&priv->fw_ucode);
499 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100500}
501
Brian Cavagnolo99020472010-11-12 17:23:52 -0800502/* states for asynchronous f/w loading */
503static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
504enum {
505 FW_STATE_INIT = 0,
506 FW_STATE_LOADING_PREF,
507 FW_STATE_LOADING_ALT,
508 FW_STATE_ERROR,
509};
510
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100511/* Request fw image */
512static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800513 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800514 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100515{
516 /* release current image */
517 if (*fw != NULL)
518 mwl8k_release_fw(fw);
519
Brian Cavagnolo99020472010-11-12 17:23:52 -0800520 if (nowait)
521 return request_firmware_nowait(THIS_MODULE, 1, fname,
522 &priv->pdev->dev, GFP_KERNEL,
523 priv, mwl8k_fw_state_machine);
524 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800525 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100526}
527
Brian Cavagnolo99020472010-11-12 17:23:52 -0800528static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
529 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100530{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200531 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100532 int rc;
533
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200534 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800535 if (nowait)
536 rc = mwl8k_request_fw(priv, di->helper_image,
537 &priv->fw_helper, true);
538 else
539 rc = mwl8k_request_fw(priv, di->helper_image,
540 &priv->fw_helper, false);
541 if (rc)
542 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
543 pci_name(priv->pdev), di->helper_image);
544
545 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200546 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100547 }
548
Brian Cavagnolo99020472010-11-12 17:23:52 -0800549 if (nowait) {
550 /*
551 * if we get here, no helper image is needed. Skip the
552 * FW_STATE_INIT state.
553 */
554 priv->fw_state = FW_STATE_LOADING_PREF;
555 rc = mwl8k_request_fw(priv, fw_image,
556 &priv->fw_ucode,
557 true);
558 } else
559 rc = mwl8k_request_fw(priv, fw_image,
560 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100561 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200562 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800563 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100564 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100565 return rc;
566 }
567
568 return 0;
569}
570
571struct mwl8k_cmd_pkt {
572 __le16 code;
573 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100574 __u8 seq_num;
575 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100576 __le16 result;
577 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000578} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100579
580/*
581 * Firmware loading.
582 */
583static int
584mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
585{
586 void __iomem *regs = priv->regs;
587 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100588 int loops;
589
590 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
591 if (pci_dma_mapping_error(priv->pdev, dma_addr))
592 return -ENOMEM;
593
594 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
595 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
596 iowrite32(MWL8K_H2A_INT_DOORBELL,
597 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
598 iowrite32(MWL8K_H2A_INT_DUMMY,
599 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
600
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100601 loops = 1000;
602 do {
603 u32 int_code;
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530604 if (priv->is_8764) {
605 int_code = ioread32(regs +
606 MWL8K_HIU_H2A_INTERRUPT_STATUS);
607 if (int_code == 0)
608 break;
609 } else {
610 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
611 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
612 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
613 break;
614 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100615 }
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200616 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100617 udelay(1);
618 } while (--loops);
619
620 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
621
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200622 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100623}
624
625static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
626 const u8 *data, size_t length)
627{
628 struct mwl8k_cmd_pkt *cmd;
629 int done;
630 int rc = 0;
631
632 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
633 if (cmd == NULL)
634 return -ENOMEM;
635
636 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
637 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100638 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100639 cmd->result = 0;
640
641 done = 0;
642 while (length) {
643 int block_size = length > 256 ? 256 : length;
644
645 memcpy(cmd->payload, data + done, block_size);
646 cmd->length = cpu_to_le16(block_size);
647
648 rc = mwl8k_send_fw_load_cmd(priv, cmd,
649 sizeof(*cmd) + block_size);
650 if (rc)
651 break;
652
653 done += block_size;
654 length -= block_size;
655 }
656
657 if (!rc) {
658 cmd->length = 0;
659 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
660 }
661
662 kfree(cmd);
663
664 return rc;
665}
666
667static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
668 const u8 *data, size_t length)
669{
670 unsigned char *buffer;
671 int may_continue, rc = 0;
672 u32 done, prev_block_size;
673
674 buffer = kmalloc(1024, GFP_KERNEL);
675 if (buffer == NULL)
676 return -ENOMEM;
677
678 done = 0;
679 prev_block_size = 0;
680 may_continue = 1000;
681 while (may_continue > 0) {
682 u32 block_size;
683
684 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
685 if (block_size & 1) {
686 block_size &= ~1;
687 may_continue--;
688 } else {
689 done += prev_block_size;
690 length -= prev_block_size;
691 }
692
693 if (block_size > 1024 || block_size > length) {
694 rc = -EOVERFLOW;
695 break;
696 }
697
698 if (length == 0) {
699 rc = 0;
700 break;
701 }
702
703 if (block_size == 0) {
704 rc = -EPROTO;
705 may_continue--;
706 udelay(1);
707 continue;
708 }
709
710 prev_block_size = block_size;
711 memcpy(buffer, data + done, block_size);
712
713 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
714 if (rc)
715 break;
716 }
717
718 if (!rc && length != 0)
719 rc = -EREMOTEIO;
720
721 kfree(buffer);
722
723 return rc;
724}
725
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200726static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100727{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200728 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800729 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200730 int rc;
731 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100732
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530733 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800734 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100735
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200736 if (helper == NULL) {
737 printk(KERN_ERR "%s: helper image needed but none "
738 "given\n", pci_name(priv->pdev));
739 return -EINVAL;
740 }
741
742 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100743 if (rc) {
744 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200745 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100746 return rc;
747 }
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +0530748 msleep(20);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100749
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200750 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100751 } else {
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530752 if (priv->is_8764)
753 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
754 else
755 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100756 }
757
758 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200759 printk(KERN_ERR "%s: unable to load firmware image\n",
760 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100761 return rc;
762 }
763
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100764 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100765
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100766 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100767 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200768 u32 ready_code;
769
770 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
771 if (ready_code == MWL8K_FWAP_READY) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000772 priv->ap_fw = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100773 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200774 } else if (ready_code == MWL8K_FWSTA_READY) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000775 priv->ap_fw = false;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200776 break;
777 }
778
779 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100780 udelay(1);
781 } while (--loops);
782
783 return loops ? 0 : -ETIMEDOUT;
784}
785
786
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100787/* DMA header used by firmware and hardware. */
788struct mwl8k_dma_data {
789 __le16 fwlen;
790 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100791 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000792} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100793
794/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100795static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100796{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100797 struct mwl8k_dma_data *tr;
798 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100799
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100800 tr = (struct mwl8k_dma_data *)skb->data;
801 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
802
803 if (hdrlen != sizeof(tr->wh)) {
804 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
805 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
806 *((__le16 *)(tr->data - 2)) = qos;
807 } else {
808 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
809 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100810 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100811
812 if (hdrlen != sizeof(*tr))
813 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100814}
815
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530816#define REDUCED_TX_HEADROOM 8
817
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800818static void
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530819mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
820 int head_pad, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100821{
822 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100823 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800824 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100825 struct mwl8k_dma_data *tr;
826
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100827 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100828 * Add a firmware DMA header; the firmware requires that we
829 * present a 2-byte payload length followed by a 4-address
830 * header (without QoS field), followed (optionally) by any
831 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100832 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100833 wh = (struct ieee80211_hdr *)skb->data;
834
835 hdrlen = ieee80211_hdrlen(wh->frame_control);
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530836
837 /*
838 * Check if skb_resize is required because of
839 * tx_headroom adjustment.
840 */
841 if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
842 + REDUCED_TX_HEADROOM))) {
843 if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
844
845 wiphy_err(priv->hw->wiphy,
846 "Failed to reallocate TX buffer\n");
847 return;
848 }
849 skb->truesize += REDUCED_TX_HEADROOM;
850 }
851
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530852 reqd_hdrlen = sizeof(*tr) + head_pad;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800853
854 if (hdrlen != reqd_hdrlen)
855 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100856
857 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800858 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100859
860 tr = (struct mwl8k_dma_data *)skb->data;
861 if (wh != &tr->wh)
862 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100863 if (hdrlen != sizeof(tr->wh))
864 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100865
866 /*
867 * Firmware length is the length of the fully formed "802.11
868 * payload". That is, everything except for the 802.11 header.
869 * This includes all crypto material including the MIC.
870 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800871 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100872}
873
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530874static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
875 struct sk_buff *skb)
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800876{
877 struct ieee80211_hdr *wh;
878 struct ieee80211_tx_info *tx_info;
879 struct ieee80211_key_conf *key_conf;
880 int data_pad;
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530881 int head_pad = 0;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800882
883 wh = (struct ieee80211_hdr *)skb->data;
884
885 tx_info = IEEE80211_SKB_CB(skb);
886
887 key_conf = NULL;
888 if (ieee80211_is_data(wh->frame_control))
889 key_conf = tx_info->control.hw_key;
890
891 /*
892 * Make sure the packet header is in the DMA header format (4-address
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530893 * without QoS), and add head & tail padding when HW crypto is enabled.
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800894 *
895 * We have the following trailer padding requirements:
896 * - WEP: 4 trailer bytes (ICV)
897 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
898 * - CCMP: 8 trailer bytes (MIC)
899 */
900 data_pad = 0;
901 if (key_conf != NULL) {
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530902 head_pad = key_conf->iv_len;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800903 switch (key_conf->cipher) {
904 case WLAN_CIPHER_SUITE_WEP40:
905 case WLAN_CIPHER_SUITE_WEP104:
906 data_pad = 4;
907 break;
908 case WLAN_CIPHER_SUITE_TKIP:
909 data_pad = 12;
910 break;
911 case WLAN_CIPHER_SUITE_CCMP:
912 data_pad = 8;
913 break;
914 }
915 }
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530916 mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800917}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100918
919/*
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530920 * Packet reception for 88w8366/88w8764 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200921 */
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530922struct mwl8k_rxd_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200923 __le16 pkt_len;
924 __u8 sq2;
925 __u8 rate;
926 __le32 pkt_phys_addr;
927 __le32 next_rxd_phys_addr;
928 __le16 qos_control;
929 __le16 htsig2;
930 __le32 hw_rssi_info;
931 __le32 hw_noise_floor_info;
932 __u8 noise_floor;
933 __u8 pad0[3];
934 __u8 rssi;
935 __u8 rx_status;
936 __u8 channel;
937 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000938} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200939
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530940#define MWL8K_AP_RATE_INFO_MCS_FORMAT 0x80
941#define MWL8K_AP_RATE_INFO_40MHZ 0x40
942#define MWL8K_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100943
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530944#define MWL8K_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200945
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530946/* 8366/8764 AP rx_status bits */
947#define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
948#define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
949#define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
950#define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
951#define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800952
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530953static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200954{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530955 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200956
957 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530958 rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200959}
960
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530961static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200962{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530963 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200964
965 rxd->pkt_len = cpu_to_le16(len);
966 rxd->pkt_phys_addr = cpu_to_le32(addr);
967 wmb();
968 rxd->rx_ctrl = 0;
969}
970
971static int
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530972mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
973 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200974{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530975 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200976
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530977 if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200978 return -1;
979 rmb();
980
981 memset(status, 0, sizeof(*status));
982
983 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400984 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200985
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530986 if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200987 status->flag |= RX_FLAG_HT;
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530988 if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100989 status->flag |= RX_FLAG_40MHZ;
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530990 status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200991 } else {
992 int i;
993
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100994 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
995 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200996 status->rate_idx = i;
997 break;
998 }
999 }
1000 }
1001
Lennert Buytenhek85478342010-01-12 13:48:56 +01001002 if (rxd->channel > 14) {
1003 status->band = IEEE80211_BAND_5GHZ;
1004 if (!(status->flag & RX_FLAG_HT))
1005 status->rate_idx -= 5;
1006 } else {
1007 status->band = IEEE80211_BAND_2GHZ;
1008 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001009 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1010 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001011
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001012 *qos = rxd->qos_control;
1013
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05301014 if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
1015 (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
1016 (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001017 status->flag |= RX_FLAG_MMIC_ERROR;
1018
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001019 return le16_to_cpu(rxd->pkt_len);
1020}
1021
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05301022static struct rxd_ops rxd_ap_ops = {
1023 .rxd_size = sizeof(struct mwl8k_rxd_ap),
1024 .rxd_init = mwl8k_rxd_ap_init,
1025 .rxd_refill = mwl8k_rxd_ap_refill,
1026 .rxd_process = mwl8k_rxd_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001027};
1028
1029/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001030 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001031 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001032struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001033 __le16 pkt_len;
1034 __u8 link_quality;
1035 __u8 noise_level;
1036 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001037 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001038 __le16 qos_control;
1039 __le16 rate_info;
1040 __le32 pad0[4];
1041 __u8 rssi;
1042 __u8 channel;
1043 __le16 pad1;
1044 __u8 rx_ctrl;
1045 __u8 rx_status;
1046 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +00001047} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001048
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001049#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
1050#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
1051#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
1052#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
1053#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
1054#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001055
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001056#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001057#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
1058/* ICV=0 or MIC=1 */
1059#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
1060/* Key is uploaded only in failure case */
1061#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001062
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001063static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001064{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001065 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001066
1067 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001068 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001069}
1070
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001071static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001072{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001073 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001074
1075 rxd->pkt_len = cpu_to_le16(len);
1076 rxd->pkt_phys_addr = cpu_to_le32(addr);
1077 wmb();
1078 rxd->rx_ctrl = 0;
1079}
1080
1081static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001082mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -04001083 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001084{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001085 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001086 u16 rate_info;
1087
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001088 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001089 return -1;
1090 rmb();
1091
1092 rate_info = le16_to_cpu(rxd->rate_info);
1093
1094 memset(status, 0, sizeof(*status));
1095
1096 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -04001097 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001098 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1099 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001100
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001101 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001102 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001103 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001104 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001105 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001106 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001107 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001108 status->flag |= RX_FLAG_HT;
1109
Lennert Buytenhek85478342010-01-12 13:48:56 +01001110 if (rxd->channel > 14) {
1111 status->band = IEEE80211_BAND_5GHZ;
1112 if (!(status->flag & RX_FLAG_HT))
1113 status->rate_idx -= 5;
1114 } else {
1115 status->band = IEEE80211_BAND_2GHZ;
1116 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001117 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1118 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001119
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001120 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001121 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1122 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1123 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001124
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001125 return le16_to_cpu(rxd->pkt_len);
1126}
1127
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001128static struct rxd_ops rxd_sta_ops = {
1129 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1130 .rxd_init = mwl8k_rxd_sta_init,
1131 .rxd_refill = mwl8k_rxd_sta_refill,
1132 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001133};
1134
1135
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001136#define MWL8K_RX_DESCS 256
1137#define MWL8K_RX_MAXSZ 3800
1138
1139static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1140{
1141 struct mwl8k_priv *priv = hw->priv;
1142 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1143 int size;
1144 int i;
1145
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001146 rxq->rxd_count = 0;
1147 rxq->head = 0;
1148 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001149
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001150 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001151
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001152 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1153 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001154 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001155 return -ENOMEM;
1156 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001157 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001158
Shan Weib9ede5f2011-03-08 11:02:03 +08001159 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001160 if (rxq->buf == NULL) {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001161 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001162 return -ENOMEM;
1163 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001164
1165 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001166 int desc_size;
1167 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001168 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001169 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001170
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001171 desc_size = priv->rxd_ops->rxd_size;
1172 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001173
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001174 nexti = i + 1;
1175 if (nexti == MWL8K_RX_DESCS)
1176 nexti = 0;
1177 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1178
1179 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001180 }
1181
1182 return 0;
1183}
1184
1185static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1186{
1187 struct mwl8k_priv *priv = hw->priv;
1188 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1189 int refilled;
1190
1191 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001192 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001193 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001194 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001195 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001196 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001197
1198 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1199 if (skb == NULL)
1200 break;
1201
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001202 addr = pci_map_single(priv->pdev, skb->data,
1203 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001204
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001205 rxq->rxd_count++;
1206 rx = rxq->tail++;
1207 if (rxq->tail == MWL8K_RX_DESCS)
1208 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001209 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001210 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001211
1212 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1213 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001214
1215 refilled++;
1216 }
1217
1218 return refilled;
1219}
1220
1221/* Must be called only when the card's reception is completely halted */
1222static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1223{
1224 struct mwl8k_priv *priv = hw->priv;
1225 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1226 int i;
1227
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001228 if (rxq->rxd == NULL)
1229 return;
1230
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001231 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001232 if (rxq->buf[i].skb != NULL) {
1233 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001234 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001235 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001236 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001237
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001238 kfree_skb(rxq->buf[i].skb);
1239 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001240 }
1241 }
1242
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001243 kfree(rxq->buf);
1244 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001245
1246 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001247 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001248 rxq->rxd, rxq->rxd_dma);
1249 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001250}
1251
1252
1253/*
1254 * Scan a list of BSSIDs to process for finalize join.
1255 * Allows for extension to process multiple BSSIDs.
1256 */
1257static inline int
1258mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1259{
1260 return priv->capture_beacon &&
1261 ieee80211_is_beacon(wh->frame_control) &&
Joe Perches2e42e472012-05-09 17:17:46 +00001262 ether_addr_equal(wh->addr3, priv->capture_bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001263}
1264
Lennert Buytenhek37797522009-10-22 20:19:45 +02001265static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1266 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001267{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001268 struct mwl8k_priv *priv = hw->priv;
1269
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001270 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001271 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001272
1273 /*
1274 * Use GFP_ATOMIC as rxq_process is called from
1275 * the primary interrupt handler, memory allocation call
1276 * must not sleep.
1277 */
1278 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1279 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001280 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001281}
1282
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001283static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1284 u8 *bssid)
1285{
1286 struct mwl8k_vif *mwl8k_vif;
1287
1288 list_for_each_entry(mwl8k_vif,
1289 vif_list, list) {
1290 if (memcmp(bssid, mwl8k_vif->bssid,
1291 ETH_ALEN) == 0)
1292 return mwl8k_vif;
1293 }
1294
1295 return NULL;
1296}
1297
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001298static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1299{
1300 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001301 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001302 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1303 int processed;
1304
1305 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001306 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001307 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001308 void *rxd;
1309 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001310 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001311 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001312 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001313
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001314 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001315 if (skb == NULL)
1316 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001317
1318 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1319
John W. Linville0d462bb2010-07-28 14:04:24 -04001320 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1321 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001322 if (pkt_len < 0)
1323 break;
1324
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001325 rxq->buf[rxq->head].skb = NULL;
1326
1327 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001328 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001329 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001330 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001331
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001332 rxq->head++;
1333 if (rxq->head == MWL8K_RX_DESCS)
1334 rxq->head = 0;
1335
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001336 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001337
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001338 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001339
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001341 * Check for a pending join operation. Save a
1342 * copy of the beacon and schedule a tasklet to
1343 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001344 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001345 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001346 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001347
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001348 if (ieee80211_has_protected(wh->frame_control)) {
1349
1350 /* Check if hw crypto has been enabled for
1351 * this bss. If yes, set the status flags
1352 * accordingly
1353 */
1354 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1355 wh->addr1);
1356
1357 if (mwl8k_vif != NULL &&
Joe Perches23677ce2012-02-09 11:17:23 +00001358 mwl8k_vif->is_hw_crypto_enabled) {
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001359 /*
1360 * When MMIC ERROR is encountered
1361 * by the firmware, payload is
1362 * dropped and only 32 bytes of
1363 * mwl8k Firmware header is sent
1364 * to the host.
1365 *
1366 * We need to add four bytes of
1367 * key information. In it
1368 * MAC80211 expects keyidx set to
1369 * 0 for triggering Counter
1370 * Measure of MMIC failure.
1371 */
1372 if (status.flag & RX_FLAG_MMIC_ERROR) {
1373 struct mwl8k_dma_data *tr;
1374 tr = (struct mwl8k_dma_data *)skb->data;
1375 memset((void *)&(tr->data), 0, 4);
1376 pkt_len += 4;
1377 }
1378
1379 if (!ieee80211_is_auth(wh->frame_control))
1380 status.flag |= RX_FLAG_IV_STRIPPED |
1381 RX_FLAG_DECRYPTED |
1382 RX_FLAG_MMIC_STRIPPED;
1383 }
1384 }
1385
1386 skb_put(skb, pkt_len);
1387 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001388 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1389 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001390
1391 processed++;
1392 }
1393
1394 return processed;
1395}
1396
1397
1398/*
1399 * Packet transmission.
1400 */
1401
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001402#define MWL8K_TXD_STATUS_OK 0x00000001
1403#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1404#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1405#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001406#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001407
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001408#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1409#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1410#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1411#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1412#define MWL8K_QOS_EOSP 0x0010
1413
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001414struct mwl8k_tx_desc {
1415 __le32 status;
1416 __u8 data_rate;
1417 __u8 tx_priority;
1418 __le16 qos_control;
1419 __le32 pkt_phys_addr;
1420 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001421 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001422 __le32 next_txd_phys_addr;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07001423 __le32 timestamp;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001424 __le16 rate_info;
1425 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001426 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001427} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001428
1429#define MWL8K_TX_DESCS 128
1430
1431static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1432{
1433 struct mwl8k_priv *priv = hw->priv;
1434 struct mwl8k_tx_queue *txq = priv->txq + index;
1435 int size;
1436 int i;
1437
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001438 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001439 txq->head = 0;
1440 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001441
1442 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1443
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001444 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1445 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001446 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001447 return -ENOMEM;
1448 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001449 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001450
Shan Weib9ede5f2011-03-08 11:02:03 +08001451 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001452 if (txq->skb == NULL) {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001453 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001454 return -ENOMEM;
1455 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001456
1457 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1458 struct mwl8k_tx_desc *tx_desc;
1459 int nexti;
1460
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001461 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001462 nexti = (i + 1) % MWL8K_TX_DESCS;
1463
1464 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001465 tx_desc->next_txd_phys_addr =
1466 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001467 }
1468
1469 return 0;
1470}
1471
1472static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1473{
1474 iowrite32(MWL8K_H2A_INT_PPA_READY,
1475 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1476 iowrite32(MWL8K_H2A_INT_DUMMY,
1477 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1478 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1479}
1480
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001481static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001482{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001483 struct mwl8k_priv *priv = hw->priv;
1484 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001485
Brian Cavagnoloe6007072011-03-17 11:58:44 -07001486 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001487 struct mwl8k_tx_queue *txq = priv->txq + i;
1488 int fw_owned = 0;
1489 int drv_owned = 0;
1490 int unused = 0;
1491 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001492
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001493 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001494 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1495 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001497 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001498 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001499 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001500 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001501 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001502
1503 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001504 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001505 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001506
Joe Perchesc96c31e2010-07-26 14:39:58 -07001507 wiphy_err(hw->wiphy,
1508 "txq[%d] len=%d head=%d tail=%d "
1509 "fw_owned=%d drv_owned=%d unused=%d\n",
1510 i,
1511 txq->len, txq->head, txq->tail,
1512 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001513 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001514}
1515
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001516/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001517 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001518 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001519#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001520
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001521static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001522{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001523 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001524 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001525 int retry;
1526 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001527
1528 might_sleep();
1529
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05301530 /* Since fw restart is in progress, allow only the firmware
1531 * commands from the restart code and block the other
1532 * commands since they are going to fail in any case since
1533 * the firmware has crashed
1534 */
1535 if (priv->hw_restart_in_progress) {
1536 if (priv->hw_restart_owner == current)
1537 return 0;
1538 else
1539 return -EBUSY;
1540 }
1541
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05301542 if (atomic_read(&priv->watchdog_event_pending))
1543 return 0;
1544
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001545 /*
1546 * The TX queues are stopped at this point, so this test
1547 * doesn't need to take ->tx_lock.
1548 */
1549 if (!priv->pending_tx_pkts)
1550 return 0;
1551
1552 retry = 0;
1553 rc = 0;
1554
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001555 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001556 priv->tx_wait = &tx_wait;
1557 while (!rc) {
1558 int oldcount;
1559 unsigned long timeout;
1560
1561 oldcount = priv->pending_tx_pkts;
1562
1563 spin_unlock_bh(&priv->tx_lock);
1564 timeout = wait_for_completion_timeout(&tx_wait,
1565 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05301566
1567 if (atomic_read(&priv->watchdog_event_pending)) {
1568 spin_lock_bh(&priv->tx_lock);
1569 priv->tx_wait = NULL;
1570 spin_unlock_bh(&priv->tx_lock);
1571 return 0;
1572 }
1573
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001574 spin_lock_bh(&priv->tx_lock);
1575
1576 if (timeout) {
1577 WARN_ON(priv->pending_tx_pkts);
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301578 if (retry)
Joe Perchesc96c31e2010-07-26 14:39:58 -07001579 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001580 break;
1581 }
1582
1583 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001584 wiphy_notice(hw->wiphy,
1585 "waiting for tx rings to drain (%d -> %d pkts)\n",
1586 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001587 retry = 1;
1588 continue;
1589 }
1590
1591 priv->tx_wait = NULL;
1592
Joe Perchesc96c31e2010-07-26 14:39:58 -07001593 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1594 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001595 mwl8k_dump_tx_rings(hw);
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05301596 priv->hw_restart_in_progress = true;
1597 ieee80211_queue_work(hw, &priv->fw_reload);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001598
1599 rc = -ETIMEDOUT;
1600 }
Nishant Sarmukadam9b0b11f2013-01-08 10:16:05 +05301601 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001602 spin_unlock_bh(&priv->tx_lock);
1603
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001604 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001605}
1606
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001607#define MWL8K_TXD_SUCCESS(status) \
1608 ((status) & (MWL8K_TXD_STATUS_OK | \
1609 MWL8K_TXD_STATUS_OK_RETRY | \
1610 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001611
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001612static int mwl8k_tid_queue_mapping(u8 tid)
1613{
1614 BUG_ON(tid > 7);
1615
1616 switch (tid) {
1617 case 0:
1618 case 3:
1619 return IEEE80211_AC_BE;
1620 break;
1621 case 1:
1622 case 2:
1623 return IEEE80211_AC_BK;
1624 break;
1625 case 4:
1626 case 5:
1627 return IEEE80211_AC_VI;
1628 break;
1629 case 6:
1630 case 7:
1631 return IEEE80211_AC_VO;
1632 break;
1633 default:
1634 return -1;
1635 break;
1636 }
1637}
1638
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001639/* The firmware will fill in the rate information
1640 * for each packet that gets queued in the hardware
John W. Linville49adc5c2011-04-27 15:04:28 -04001641 * and these macros will interpret that info.
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001642 */
1643
John W. Linville49adc5c2011-04-27 15:04:28 -04001644#define RI_FORMAT(a) (a & 0x0001)
1645#define RI_RATE_ID_MCS(a) ((a & 0x01f8) >> 3)
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001646
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001647static int
1648mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001649{
1650 struct mwl8k_priv *priv = hw->priv;
1651 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001652 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001653
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001654 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001655 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001656 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 struct mwl8k_tx_desc *tx_desc;
1658 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001659 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001660 struct sk_buff *skb;
1661 struct ieee80211_tx_info *info;
1662 u32 status;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001663 struct ieee80211_sta *sta;
1664 struct mwl8k_sta *sta_info = NULL;
1665 u16 rate_info;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001666 struct ieee80211_hdr *wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001667
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001668 tx = txq->head;
1669 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001670
1671 status = le32_to_cpu(tx_desc->status);
1672
1673 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1674 if (!force)
1675 break;
1676 tx_desc->status &=
1677 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1678 }
1679
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001680 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001681 BUG_ON(txq->len == 0);
1682 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001683 priv->pending_tx_pkts--;
1684
1685 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001686 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001687 skb = txq->skb[tx];
1688 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001689
1690 BUG_ON(skb == NULL);
1691 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1692
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001693 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001694
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001695 wh = (struct ieee80211_hdr *) skb->data;
1696
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001697 /* Mark descriptor as unused */
1698 tx_desc->pkt_phys_addr = 0;
1699 tx_desc->pkt_len = 0;
1700
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001701 info = IEEE80211_SKB_CB(skb);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001702 if (ieee80211_is_data(wh->frame_control)) {
Thomas Huehn89e11802012-07-11 13:21:41 +02001703 rcu_read_lock();
1704 sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
1705 wh->addr2);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001706 if (sta) {
1707 sta_info = MWL8K_STA(sta);
1708 BUG_ON(sta_info == NULL);
1709 rate_info = le16_to_cpu(tx_desc->rate_info);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001710 /* If rate is < 6.5 Mpbs for an ht station
1711 * do not form an ampdu. If the station is a
1712 * legacy station (format = 0), do not form an
1713 * ampdu
1714 */
John W. Linville49adc5c2011-04-27 15:04:28 -04001715 if (RI_RATE_ID_MCS(rate_info) < 1 ||
1716 RI_FORMAT(rate_info) == 0) {
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001717 sta_info->is_ampdu_allowed = false;
1718 } else {
1719 sta_info->is_ampdu_allowed = true;
1720 }
1721 }
Thomas Huehn89e11802012-07-11 13:21:41 +02001722 rcu_read_unlock();
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001723 }
1724
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001725 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001726
1727 /* Rate control is happening in the firmware.
1728 * Ensure no tx rate is being reported.
1729 */
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301730 info->status.rates[0].idx = -1;
1731 info->status.rates[0].count = 1;
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001732
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001733 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001734 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001735
1736 ieee80211_tx_status_irqsafe(hw, skb);
1737
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001738 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001739 }
1740
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001741 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001742}
1743
1744/* must be called only when the card's transmit is completely halted */
1745static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1746{
1747 struct mwl8k_priv *priv = hw->priv;
1748 struct mwl8k_tx_queue *txq = priv->txq + index;
1749
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001750 if (txq->txd == NULL)
1751 return;
1752
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001753 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001754
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001755 kfree(txq->skb);
1756 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001757
1758 pci_free_consistent(priv->pdev,
1759 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001760 txq->txd, txq->txd_dma);
1761 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762}
1763
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001764/* caller must hold priv->stream_lock when calling the stream functions */
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301765static struct mwl8k_ampdu_stream *
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001766mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1767{
1768 struct mwl8k_ampdu_stream *stream;
1769 struct mwl8k_priv *priv = hw->priv;
1770 int i;
1771
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301772 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001773 stream = &priv->ampdu[i];
1774 if (stream->state == AMPDU_NO_STREAM) {
1775 stream->sta = sta;
1776 stream->state = AMPDU_STREAM_NEW;
1777 stream->tid = tid;
1778 stream->idx = i;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001779 wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1780 sta->addr, tid);
1781 return stream;
1782 }
1783 }
1784 return NULL;
1785}
1786
1787static int
1788mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1789{
1790 int ret;
1791
1792 /* if the stream has already been started, don't start it again */
1793 if (stream->state != AMPDU_STREAM_NEW)
1794 return 0;
1795 ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1796 if (ret)
1797 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1798 "%d\n", stream->sta->addr, stream->tid, ret);
1799 else
1800 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1801 stream->sta->addr, stream->tid);
1802 return ret;
1803}
1804
1805static void
1806mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1807{
1808 wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1809 stream->tid);
1810 memset(stream, 0, sizeof(*stream));
1811}
1812
1813static struct mwl8k_ampdu_stream *
1814mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1815{
1816 struct mwl8k_priv *priv = hw->priv;
1817 int i;
1818
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301819 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001820 struct mwl8k_ampdu_stream *stream;
1821 stream = &priv->ampdu[i];
1822 if (stream->state == AMPDU_NO_STREAM)
1823 continue;
1824 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1825 stream->tid == tid)
1826 return stream;
1827 }
1828 return NULL;
1829}
1830
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001831#define MWL8K_AMPDU_PACKET_THRESHOLD 64
1832static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
1833{
1834 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1835 struct tx_traffic_info *tx_stats;
1836
1837 BUG_ON(tid >= MWL8K_MAX_TID);
1838 tx_stats = &sta_info->tx_stats[tid];
1839
1840 return sta_info->is_ampdu_allowed &&
1841 tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
1842}
1843
1844static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
1845{
1846 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1847 struct tx_traffic_info *tx_stats;
1848
1849 BUG_ON(tid >= MWL8K_MAX_TID);
1850 tx_stats = &sta_info->tx_stats[tid];
1851
1852 if (tx_stats->start_time == 0)
1853 tx_stats->start_time = jiffies;
1854
1855 /* reset the packet count after each second elapses. If the number of
1856 * packets ever exceeds the ampdu_min_traffic threshold, we will allow
1857 * an ampdu stream to be started.
1858 */
1859 if (jiffies - tx_stats->start_time > HZ) {
1860 tx_stats->pkts = 0;
1861 tx_stats->start_time = 0;
1862 } else
1863 tx_stats->pkts++;
1864}
1865
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301866/* The hardware ampdu queues start from 5.
1867 * txpriorities for ampdu queues are
1868 * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
1869 * and queue 3 is lowest (queue 4 is reserved)
1870 */
1871#define BA_QUEUE 5
1872
Johannes Berg7bb45682011-02-24 14:42:06 +01001873static void
Thomas Huehn36323f82012-07-23 21:33:42 +02001874mwl8k_txq_xmit(struct ieee80211_hw *hw,
1875 int index,
1876 struct ieee80211_sta *sta,
1877 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001878{
1879 struct mwl8k_priv *priv = hw->priv;
1880 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001881 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001882 struct ieee80211_hdr *wh;
1883 struct mwl8k_tx_queue *txq;
1884 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001885 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001886 u32 txstatus;
1887 u8 txdatarate;
1888 u16 qos;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001889 int txpriority;
1890 u8 tid = 0;
1891 struct mwl8k_ampdu_stream *stream = NULL;
1892 bool start_ba_session = false;
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05301893 bool mgmtframe = false;
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001894 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301895 bool eapol_frame = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001896
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001897 wh = (struct ieee80211_hdr *)skb->data;
1898 if (ieee80211_is_data_qos(wh->frame_control))
1899 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1900 else
1901 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001902
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301903 if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1904 eapol_frame = true;
1905
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05301906 if (ieee80211_is_mgmt(wh->frame_control))
1907 mgmtframe = true;
1908
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001909 if (priv->ap_fw)
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +05301910 mwl8k_encapsulate_tx_frame(priv, skb);
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001911 else
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +05301912 mwl8k_add_dma_header(priv, skb, 0, 0);
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001913
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001914 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001915
1916 tx_info = IEEE80211_SKB_CB(skb);
1917 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001918
1919 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001920 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001921 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1922 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001923 }
1924
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001925 /* Setup firmware control bit fields for each frame type. */
1926 txstatus = 0;
1927 txdatarate = 0;
1928 if (ieee80211_is_mgmt(wh->frame_control) ||
1929 ieee80211_is_ctl(wh->frame_control)) {
1930 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001931 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001932 } else if (ieee80211_is_data(wh->frame_control)) {
1933 txdatarate = 1;
1934 if (is_multicast_ether_addr(wh->addr1))
1935 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1936
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001937 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001938 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001939 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001940 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001941 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001942 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001943
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001944 /* Queue ADDBA request in the respective data queue. While setting up
1945 * the ampdu stream, mac80211 queues further packets for that
1946 * particular ra/tid pair. However, packets piled up in the hardware
1947 * for that ra/tid pair will still go out. ADDBA request and the
1948 * related data packets going out from different queues asynchronously
1949 * will cause a shift in the receiver window which might result in
1950 * ampdu packets getting dropped at the receiver after the stream has
1951 * been setup.
1952 */
1953 if (unlikely(ieee80211_is_action(wh->frame_control) &&
1954 mgmt->u.action.category == WLAN_CATEGORY_BACK &&
1955 mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
1956 priv->ap_fw)) {
1957 u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1958 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1959 index = mwl8k_tid_queue_mapping(tid);
1960 }
1961
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001962 txpriority = index;
1963
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301964 if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
1965 ieee80211_is_data_qos(wh->frame_control)) {
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001966 tid = qos & 0xf;
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001967 mwl8k_tx_count_packet(sta, tid);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001968 spin_lock(&priv->stream_lock);
1969 stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1970 if (stream != NULL) {
1971 if (stream->state == AMPDU_STREAM_ACTIVE) {
Yogesh Ashok Powar5f2a1492013-01-03 13:21:25 +05301972 WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301973 txpriority = (BA_QUEUE + stream->idx) %
1974 TOTAL_HW_TX_QUEUES;
1975 if (stream->idx <= 1)
1976 index = stream->idx +
1977 MWL8K_TX_WMM_QUEUES;
1978
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001979 } else if (stream->state == AMPDU_STREAM_NEW) {
1980 /* We get here if the driver sends us packets
1981 * after we've initiated a stream, but before
1982 * our ampdu_action routine has been called
1983 * with IEEE80211_AMPDU_TX_START to get the SSN
1984 * for the ADDBA request. So this packet can
1985 * go out with no risk of sequence number
1986 * mismatch. No special handling is required.
1987 */
1988 } else {
1989 /* Drop packets that would go out after the
1990 * ADDBA request was sent but before the ADDBA
1991 * response is received. If we don't do this,
1992 * the recipient would probably receive it
1993 * after the ADDBA request with SSN 0. This
1994 * will cause the recipient's BA receive window
1995 * to shift, which would cause the subsequent
1996 * packets in the BA stream to be discarded.
1997 * mac80211 queues our packets for us in this
1998 * case, so this is really just a safety check.
1999 */
2000 wiphy_warn(hw->wiphy,
2001 "Cannot send packet while ADDBA "
2002 "dialog is underway.\n");
2003 spin_unlock(&priv->stream_lock);
2004 dev_kfree_skb(skb);
2005 return;
2006 }
2007 } else {
2008 /* Defer calling mwl8k_start_stream so that the current
2009 * skb can go out before the ADDBA request. This
2010 * prevents sequence number mismatch at the recepient
2011 * as described above.
2012 */
Brian Cavagnolod0805c12011-04-12 11:06:28 -07002013 if (mwl8k_ampdu_allowed(sta, tid)) {
Nishant Sarmukadam170335432011-03-17 11:58:48 -07002014 stream = mwl8k_add_stream(hw, sta, tid);
2015 if (stream != NULL)
2016 start_ba_session = true;
2017 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002018 }
2019 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar5f2a1492013-01-03 13:21:25 +05302020 } else {
2021 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
2022 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002023 }
2024
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002025 dma = pci_map_single(priv->pdev, skb->data,
2026 skb->len, PCI_DMA_TODEVICE);
2027
2028 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07002029 wiphy_debug(hw->wiphy,
2030 "failed to dma map skb, dropping TX frame.\n");
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002031 if (start_ba_session) {
2032 spin_lock(&priv->stream_lock);
2033 mwl8k_remove_stream(hw, stream);
2034 spin_unlock(&priv->stream_lock);
2035 }
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002036 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01002037 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002038 }
2039
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002040 spin_lock_bh(&priv->tx_lock);
2041
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002042 txq = priv->txq + index;
2043
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302044 /* Mgmt frames that go out frequently are probe
2045 * responses. Other mgmt frames got out relatively
2046 * infrequently. Hence reserve 2 buffers so that
2047 * other mgmt frames do not get dropped due to an
2048 * already queued probe response in one of the
2049 * reserved buffers.
2050 */
2051
2052 if (txq->len >= MWL8K_TX_DESCS - 2) {
Joe Perches23677ce2012-02-09 11:17:23 +00002053 if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302054 if (start_ba_session) {
2055 spin_lock(&priv->stream_lock);
2056 mwl8k_remove_stream(hw, stream);
2057 spin_unlock(&priv->stream_lock);
2058 }
2059 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadamff7aa962012-11-06 19:22:48 +05302060 pci_unmap_single(priv->pdev, dma, skb->len,
2061 PCI_DMA_TODEVICE);
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302062 dev_kfree_skb(skb);
2063 return;
Pradeep Nemavat3a7dbc32011-04-21 16:34:56 +05302064 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002065 }
2066
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002067 BUG_ON(txq->skb[txq->tail] != NULL);
2068 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002069
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002070 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002071 tx->data_rate = txdatarate;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002072 tx->tx_priority = txpriority;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002073 tx->qos_control = cpu_to_le16(qos);
2074 tx->pkt_phys_addr = cpu_to_le32(dma);
2075 tx->pkt_len = cpu_to_le16(skb->len);
2076 tx->rate_info = 0;
Thomas Huehn36323f82012-07-23 21:33:42 +02002077 if (!priv->ap_fw && sta != NULL)
2078 tx->peer_id = MWL8K_STA(sta)->peer_id;
Lennert Buytenheka6804002010-01-04 21:55:42 +01002079 else
2080 tx->peer_id = 0;
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302081
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05302082 if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302083 tx->timestamp = cpu_to_le32(ioread32(priv->regs +
2084 MWL8K_HW_TIMER_REGISTER));
Nishant Sarmukadamb8d9e572012-11-06 19:23:15 +05302085 else
2086 tx->timestamp = 0;
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302087
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002088 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002089 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
2090
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02002091 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002092 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002093
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002094 txq->tail++;
2095 if (txq->tail == MWL8K_TX_DESCS)
2096 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002097
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002098 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002099
2100 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002101
2102 /* Initiate the ampdu session here */
2103 if (start_ba_session) {
2104 spin_lock(&priv->stream_lock);
2105 if (mwl8k_start_stream(hw, stream))
2106 mwl8k_remove_stream(hw, stream);
2107 spin_unlock(&priv->stream_lock);
2108 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002109}
2110
2111
2112/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002113 * Firmware access.
2114 *
2115 * We have the following requirements for issuing firmware commands:
2116 * - Some commands require that the packet transmit path is idle when
2117 * the command is issued. (For simplicity, we'll just quiesce the
2118 * transmit path for every command.)
2119 * - There are certain sequences of commands that need to be issued to
2120 * the hardware sequentially, with no other intervening commands.
2121 *
2122 * This leads to an implementation of a "firmware lock" as a mutex that
2123 * can be taken recursively, and which is taken by both the low-level
2124 * command submission function (mwl8k_post_cmd) as well as any users of
2125 * that function that require issuing of an atomic sequence of commands,
2126 * and quiesces the transmit path whenever it's taken.
2127 */
2128static int mwl8k_fw_lock(struct ieee80211_hw *hw)
2129{
2130 struct mwl8k_priv *priv = hw->priv;
2131
2132 if (priv->fw_mutex_owner != current) {
2133 int rc;
2134
2135 mutex_lock(&priv->fw_mutex);
2136 ieee80211_stop_queues(hw);
2137
2138 rc = mwl8k_tx_wait_empty(hw);
2139 if (rc) {
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05302140 if (!priv->hw_restart_in_progress)
2141 ieee80211_wake_queues(hw);
2142
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002143 mutex_unlock(&priv->fw_mutex);
2144
2145 return rc;
2146 }
2147
2148 priv->fw_mutex_owner = current;
2149 }
2150
2151 priv->fw_mutex_depth++;
2152
2153 return 0;
2154}
2155
2156static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
2157{
2158 struct mwl8k_priv *priv = hw->priv;
2159
2160 if (!--priv->fw_mutex_depth) {
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05302161 if (!priv->hw_restart_in_progress)
2162 ieee80211_wake_queues(hw);
2163
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002164 priv->fw_mutex_owner = NULL;
2165 mutex_unlock(&priv->fw_mutex);
2166 }
2167}
2168
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302169static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
2170 u32 bitmap);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002171
2172/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002173 * Command processing.
2174 */
2175
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002176/* Timeout firmware commands after 10s */
2177#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002178
2179static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
2180{
2181 DECLARE_COMPLETION_ONSTACK(cmd_wait);
2182 struct mwl8k_priv *priv = hw->priv;
2183 void __iomem *regs = priv->regs;
2184 dma_addr_t dma_addr;
2185 unsigned int dma_size;
2186 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002187 unsigned long timeout = 0;
2188 u8 buf[32];
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302189 u32 bitmap = 0;
2190
2191 wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
2192 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
2193
2194 /* Before posting firmware commands that could change the hardware
2195 * characteristics, make sure that all BSSes are stopped temporary.
2196 * Enable these stopped BSSes after completion of the commands
2197 */
2198
2199 rc = mwl8k_fw_lock(hw);
2200 if (rc)
2201 return rc;
2202
2203 if (priv->ap_fw && priv->running_bsses) {
2204 switch (le16_to_cpu(cmd->code)) {
2205 case MWL8K_CMD_SET_RF_CHANNEL:
2206 case MWL8K_CMD_RADIO_CONTROL:
2207 case MWL8K_CMD_RF_TX_POWER:
2208 case MWL8K_CMD_TX_POWER:
2209 case MWL8K_CMD_RF_ANTENNA:
2210 case MWL8K_CMD_RTS_THRESHOLD:
2211 case MWL8K_CMD_MIMO_CONFIG:
2212 bitmap = priv->running_bsses;
2213 mwl8k_enable_bsses(hw, false, bitmap);
2214 break;
2215 }
2216 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002217
John W. Linvilleb6037422010-07-20 13:55:00 -04002218 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002219 dma_size = le16_to_cpu(cmd->length);
2220 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
2221 PCI_DMA_BIDIRECTIONAL);
2222 if (pci_dma_mapping_error(priv->pdev, dma_addr))
2223 return -ENOMEM;
2224
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002225 priv->hostcmd_wait = &cmd_wait;
2226 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
2227 iowrite32(MWL8K_H2A_INT_DOORBELL,
2228 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2229 iowrite32(MWL8K_H2A_INT_DUMMY,
2230 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002231
2232 timeout = wait_for_completion_timeout(&cmd_wait,
2233 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
2234
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002235 priv->hostcmd_wait = NULL;
2236
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002237
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02002238 pci_unmap_single(priv->pdev, dma_addr, dma_size,
2239 PCI_DMA_BIDIRECTIONAL);
2240
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002241 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07002242 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002243 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2244 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002245 rc = -ETIMEDOUT;
2246 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002247 int ms;
2248
2249 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
2250
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002251 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002252 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07002253 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002254 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2255 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002256 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07002257 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002258 mwl8k_cmd_name(cmd->code,
2259 buf, sizeof(buf)),
2260 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002261 }
2262
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302263 if (bitmap)
2264 mwl8k_enable_bsses(hw, true, bitmap);
2265
2266 mwl8k_fw_unlock(hw);
2267
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002268 return rc;
2269}
2270
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01002271static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
2272 struct ieee80211_vif *vif,
2273 struct mwl8k_cmd_pkt *cmd)
2274{
2275 if (vif != NULL)
2276 cmd->macid = MWL8K_VIF(vif)->macid;
2277 return mwl8k_post_cmd(hw, cmd);
2278}
2279
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002280/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002281 * Setup code shared between STA and AP firmware images.
2282 */
2283static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2284{
2285 struct mwl8k_priv *priv = hw->priv;
2286
2287 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2288 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2289
2290 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2291 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2292
2293 priv->band_24.band = IEEE80211_BAND_2GHZ;
2294 priv->band_24.channels = priv->channels_24;
2295 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2296 priv->band_24.bitrates = priv->rates_24;
2297 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2298
2299 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
2300}
2301
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01002302static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2303{
2304 struct mwl8k_priv *priv = hw->priv;
2305
2306 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2307 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2308
2309 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2310 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2311
2312 priv->band_50.band = IEEE80211_BAND_5GHZ;
2313 priv->band_50.channels = priv->channels_50;
2314 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2315 priv->band_50.bitrates = priv->rates_50;
2316 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2317
2318 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
2319}
2320
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002321/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002322 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002323 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002324struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002325 struct mwl8k_cmd_pkt header;
2326 __u8 hw_rev;
2327 __u8 host_interface;
2328 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002329 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002330 __le16 region_code;
2331 __le32 fw_rev;
2332 __le32 ps_cookie;
2333 __le32 caps;
2334 __u8 mcs_bitmap[16];
2335 __le32 rx_queue_ptr;
2336 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002337 __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002338 __le32 caps2;
2339 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002340 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002341} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002342
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002343#define MWL8K_CAP_MAX_AMSDU 0x20000000
2344#define MWL8K_CAP_GREENFIELD 0x08000000
2345#define MWL8K_CAP_AMPDU 0x04000000
2346#define MWL8K_CAP_RX_STBC 0x01000000
2347#define MWL8K_CAP_TX_STBC 0x00800000
2348#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
2349#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
2350#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
2351#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
2352#define MWL8K_CAP_DELAY_BA 0x00003000
2353#define MWL8K_CAP_MIMO 0x00000200
2354#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01002355#define MWL8K_CAP_BAND_MASK 0x00000007
2356#define MWL8K_CAP_5GHZ 0x00000004
2357#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002358
Lennert Buytenhek06953232010-01-12 13:49:41 +01002359static void
2360mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2361 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002362{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002363 int rx_streams;
2364 int tx_streams;
2365
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002366 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002367
2368 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002369 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002370 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002371 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002372 if (cap & MWL8K_CAP_AMPDU) {
2373 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002374 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2375 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002376 }
2377 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002378 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002379 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002380 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002381 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002382 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002383 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002384 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002385 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002386 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002387 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002388 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002389
2390 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2391 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2392
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002393 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002394 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002395 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002396 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002397 band->ht_cap.mcs.rx_mask[2] = 0xff;
2398 band->ht_cap.mcs.rx_mask[4] = 0x01;
2399 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002400
2401 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002402 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2403 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002404 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2405 }
2406}
2407
Lennert Buytenhek06953232010-01-12 13:49:41 +01002408static void
2409mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2410{
2411 struct mwl8k_priv *priv = hw->priv;
2412
2413 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2414 mwl8k_setup_2ghz_band(hw);
2415 if (caps & MWL8K_CAP_MIMO)
2416 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2417 }
2418
2419 if (caps & MWL8K_CAP_5GHZ) {
2420 mwl8k_setup_5ghz_band(hw);
2421 if (caps & MWL8K_CAP_MIMO)
2422 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2423 }
2424}
2425
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002426static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002427{
2428 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002429 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002430 int rc;
2431 int i;
2432
2433 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2434 if (cmd == NULL)
2435 return -ENOMEM;
2436
2437 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2438 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2439
2440 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2441 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002442 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002443 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2444 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002445 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002446 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002447 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002448
2449 rc = mwl8k_post_cmd(hw, &cmd->header);
2450
2451 if (!rc) {
2452 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2453 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002454 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002455 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002456 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002457 priv->ap_macids_supported = 0x00000000;
2458 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002459 }
2460
2461 kfree(cmd);
2462 return rc;
2463}
2464
2465/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002466 * CMD_GET_HW_SPEC (AP version).
2467 */
2468struct mwl8k_cmd_get_hw_spec_ap {
2469 struct mwl8k_cmd_pkt header;
2470 __u8 hw_rev;
2471 __u8 host_interface;
2472 __le16 num_wcb;
2473 __le16 num_mcaddrs;
2474 __u8 perm_addr[ETH_ALEN];
2475 __le16 region_code;
2476 __le16 num_antenna;
2477 __le32 fw_rev;
2478 __le32 wcbbase0;
2479 __le32 rxwrptr;
2480 __le32 rxrdptr;
2481 __le32 ps_cookie;
2482 __le32 wcbbase1;
2483 __le32 wcbbase2;
2484 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002485 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002486 __le32 caps;
2487 __le32 num_of_ampdu_queues;
2488 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002489} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002490
2491static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2492{
2493 struct mwl8k_priv *priv = hw->priv;
2494 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002495 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002496 u32 api_version;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002497
2498 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2499 if (cmd == NULL)
2500 return -ENOMEM;
2501
2502 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2503 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2504
2505 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2506 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2507
2508 rc = mwl8k_post_cmd(hw, &cmd->header);
2509
2510 if (!rc) {
2511 int off;
2512
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002513 api_version = le32_to_cpu(cmd->fw_api_version);
2514 if (priv->device_info->fw_api_ap != api_version) {
2515 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2516 " Expected %d got %d.\n", MWL8K_NAME,
2517 priv->device_info->part_name,
2518 priv->device_info->fw_api_ap,
2519 api_version);
2520 rc = -EINVAL;
2521 goto done;
2522 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002523 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2524 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2525 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2526 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002527 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002528 priv->ap_macids_supported = 0x000000ff;
Yogesh Ashok Poward59c1cf2013-01-25 16:20:03 +05302529 priv->sta_macids_supported = 0x00000100;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002530 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2531 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2532 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2533 " but we only support %d.\n",
2534 priv->num_ampdu_queues,
2535 MWL8K_MAX_AMPDU_QUEUES);
2536 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2537 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002538 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002539 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002540
2541 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002542 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002543
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002544 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2545 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2546 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2547 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002548
2549 for (i = 0; i < priv->num_ampdu_queues; i++)
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002550 priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002551 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002552 }
2553
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002554done:
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002555 kfree(cmd);
2556 return rc;
2557}
2558
2559/*
2560 * CMD_SET_HW_SPEC.
2561 */
2562struct mwl8k_cmd_set_hw_spec {
2563 struct mwl8k_cmd_pkt header;
2564 __u8 hw_rev;
2565 __u8 host_interface;
2566 __le16 num_mcaddrs;
2567 __u8 perm_addr[ETH_ALEN];
2568 __le16 region_code;
2569 __le32 fw_rev;
2570 __le32 ps_cookie;
2571 __le32 caps;
2572 __le32 rx_queue_ptr;
2573 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002574 __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002575 __le32 flags;
2576 __le32 num_tx_desc_per_queue;
2577 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002578} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002579
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002580/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2581 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2582 * the packets that are queued for more than 500ms, will be dropped in the
2583 * hardware. This helps minimizing the issues caused due to head-of-line
2584 * blocking where a slow client can hog the bandwidth and affect traffic to a
2585 * faster client.
2586 */
2587#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Nishant Sarmukadam3373b282011-06-13 16:26:15 +05302588#define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR 0x00000200
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002589#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2590#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2591#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002592
2593static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2594{
2595 struct mwl8k_priv *priv = hw->priv;
2596 struct mwl8k_cmd_set_hw_spec *cmd;
2597 int rc;
2598 int i;
2599
2600 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2601 if (cmd == NULL)
2602 return -ENOMEM;
2603
2604 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2605 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2606
2607 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2608 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002609 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002610
2611 /*
2612 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2613 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2614 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2615 * priority is interpreted the right way in firmware.
2616 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002617 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2618 int j = mwl8k_tx_queues(priv) - 1 - i;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002619 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2620 }
2621
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002622 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2623 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
Nishant Sarmukadam31d291a2011-04-21 16:34:59 +05302624 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
Nishant Sarmukadam3373b282011-06-13 16:26:15 +05302625 MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
2626 MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002627 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2628 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2629
2630 rc = mwl8k_post_cmd(hw, &cmd->header);
2631 kfree(cmd);
2632
2633 return rc;
2634}
2635
2636/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002637 * CMD_MAC_MULTICAST_ADR.
2638 */
2639struct mwl8k_cmd_mac_multicast_adr {
2640 struct mwl8k_cmd_pkt header;
2641 __le16 action;
2642 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002643 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002644};
2645
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002646#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2647#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2648#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2649#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002650
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002651static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002652__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002653 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002654{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002655 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002656 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002657 int size;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002658 int mc_count = 0;
2659
2660 if (mc_list)
2661 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002662
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002663 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002664 allmulti = 1;
2665 mc_count = 0;
2666 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002667
2668 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2669
2670 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002671 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002672 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002673
2674 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2675 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002676 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2677 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002678
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002679 if (allmulti) {
2680 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2681 } else if (mc_count) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002682 struct netdev_hw_addr *ha;
2683 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002684
2685 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2686 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002687 netdev_hw_addr_list_for_each(ha, mc_list) {
2688 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002689 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002690 }
2691
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002692 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002693}
2694
2695/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002696 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002697 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002698struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002699 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002700 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002701} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002702
2703#define MWL8K_STAT_ACK_FAILURE 9
2704#define MWL8K_STAT_RTS_FAILURE 12
2705#define MWL8K_STAT_FCS_ERROR 24
2706#define MWL8K_STAT_RTS_SUCCESS 11
2707
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002708static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2709 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002710{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002711 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002712 int rc;
2713
2714 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2715 if (cmd == NULL)
2716 return -ENOMEM;
2717
2718 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2719 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002720
2721 rc = mwl8k_post_cmd(hw, &cmd->header);
2722 if (!rc) {
2723 stats->dot11ACKFailureCount =
2724 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2725 stats->dot11RTSFailureCount =
2726 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2727 stats->dot11FCSErrorCount =
2728 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2729 stats->dot11RTSSuccessCount =
2730 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2731 }
2732 kfree(cmd);
2733
2734 return rc;
2735}
2736
2737/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002738 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002739 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002740struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002741 struct mwl8k_cmd_pkt header;
2742 __le16 action;
2743 __le16 control;
2744 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002745} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002746
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002747static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002748mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002749{
2750 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002751 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002752 int rc;
2753
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002754 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002755 return 0;
2756
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002757 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2758 if (cmd == NULL)
2759 return -ENOMEM;
2760
2761 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2762 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2763 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002764 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002765 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2766
2767 rc = mwl8k_post_cmd(hw, &cmd->header);
2768 kfree(cmd);
2769
2770 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002771 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002772
2773 return rc;
2774}
2775
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002776static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002777{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002778 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002779}
2780
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002781static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002782{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002783 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002784}
2785
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002786static int
2787mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2788{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002789 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002790
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002791 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002792
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002793 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002794}
2795
2796/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002797 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002798 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002799#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002800
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002801struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002802 struct mwl8k_cmd_pkt header;
2803 __le16 action;
2804 __le16 support_level;
2805 __le16 current_level;
2806 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002807 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002808} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002809
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002810static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002811{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002812 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002813 int rc;
2814
2815 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2816 if (cmd == NULL)
2817 return -ENOMEM;
2818
2819 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2820 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2821 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2822 cmd->support_level = cpu_to_le16(dBm);
2823
2824 rc = mwl8k_post_cmd(hw, &cmd->header);
2825 kfree(cmd);
2826
2827 return rc;
2828}
2829
2830/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002831 * CMD_TX_POWER.
2832 */
2833#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2834
2835struct mwl8k_cmd_tx_power {
2836 struct mwl8k_cmd_pkt header;
2837 __le16 action;
2838 __le16 band;
2839 __le16 channel;
2840 __le16 bw;
2841 __le16 sub_ch;
2842 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05302843} __packed;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002844
2845static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2846 struct ieee80211_conf *conf,
2847 unsigned short pwr)
2848{
2849 struct ieee80211_channel *channel = conf->channel;
2850 struct mwl8k_cmd_tx_power *cmd;
2851 int rc;
2852 int i;
2853
2854 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2855 if (cmd == NULL)
2856 return -ENOMEM;
2857
2858 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2859 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2860 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2861
2862 if (channel->band == IEEE80211_BAND_2GHZ)
2863 cmd->band = cpu_to_le16(0x1);
2864 else if (channel->band == IEEE80211_BAND_5GHZ)
2865 cmd->band = cpu_to_le16(0x4);
2866
Yogesh Ashok Powar604c4ef2012-01-17 15:45:15 +05302867 cmd->channel = cpu_to_le16(channel->hw_value);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002868
2869 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2870 conf->channel_type == NL80211_CHAN_HT20) {
2871 cmd->bw = cpu_to_le16(0x2);
2872 } else {
2873 cmd->bw = cpu_to_le16(0x4);
2874 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2875 cmd->sub_ch = cpu_to_le16(0x3);
2876 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2877 cmd->sub_ch = cpu_to_le16(0x1);
2878 }
2879
2880 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2881 cmd->power_level_list[i] = cpu_to_le16(pwr);
2882
2883 rc = mwl8k_post_cmd(hw, &cmd->header);
2884 kfree(cmd);
2885
2886 return rc;
2887}
2888
2889/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002890 * CMD_RF_ANTENNA.
2891 */
2892struct mwl8k_cmd_rf_antenna {
2893 struct mwl8k_cmd_pkt header;
2894 __le16 antenna;
2895 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002896} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002897
2898#define MWL8K_RF_ANTENNA_RX 1
2899#define MWL8K_RF_ANTENNA_TX 2
2900
2901static int
2902mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2903{
2904 struct mwl8k_cmd_rf_antenna *cmd;
2905 int rc;
2906
2907 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2908 if (cmd == NULL)
2909 return -ENOMEM;
2910
2911 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2912 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2913 cmd->antenna = cpu_to_le16(antenna);
2914 cmd->mode = cpu_to_le16(mask);
2915
2916 rc = mwl8k_post_cmd(hw, &cmd->header);
2917 kfree(cmd);
2918
2919 return rc;
2920}
2921
2922/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002923 * CMD_SET_BEACON.
2924 */
2925struct mwl8k_cmd_set_beacon {
2926 struct mwl8k_cmd_pkt header;
2927 __le16 beacon_len;
2928 __u8 beacon[0];
2929};
2930
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002931static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2932 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002933{
2934 struct mwl8k_cmd_set_beacon *cmd;
2935 int rc;
2936
2937 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2938 if (cmd == NULL)
2939 return -ENOMEM;
2940
2941 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2942 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2943 cmd->beacon_len = cpu_to_le16(len);
2944 memcpy(cmd->beacon, beacon, len);
2945
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002946 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002947 kfree(cmd);
2948
2949 return rc;
2950}
2951
2952/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002953 * CMD_SET_PRE_SCAN.
2954 */
2955struct mwl8k_cmd_set_pre_scan {
2956 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002957} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002958
2959static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2960{
2961 struct mwl8k_cmd_set_pre_scan *cmd;
2962 int rc;
2963
2964 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2965 if (cmd == NULL)
2966 return -ENOMEM;
2967
2968 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2969 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2970
2971 rc = mwl8k_post_cmd(hw, &cmd->header);
2972 kfree(cmd);
2973
2974 return rc;
2975}
2976
2977/*
2978 * CMD_SET_POST_SCAN.
2979 */
2980struct mwl8k_cmd_set_post_scan {
2981 struct mwl8k_cmd_pkt header;
2982 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002983 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002984} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002985
2986static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002987mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002988{
2989 struct mwl8k_cmd_set_post_scan *cmd;
2990 int rc;
2991
2992 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2993 if (cmd == NULL)
2994 return -ENOMEM;
2995
2996 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2997 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2998 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002999 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003000
3001 rc = mwl8k_post_cmd(hw, &cmd->header);
3002 kfree(cmd);
3003
3004 return rc;
3005}
3006
3007/*
3008 * CMD_SET_RF_CHANNEL.
3009 */
3010struct mwl8k_cmd_set_rf_channel {
3011 struct mwl8k_cmd_pkt header;
3012 __le16 action;
3013 __u8 current_channel;
3014 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003015} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003016
3017static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003018 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003019{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003020 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003021 struct mwl8k_cmd_set_rf_channel *cmd;
3022 int rc;
3023
3024 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3025 if (cmd == NULL)
3026 return -ENOMEM;
3027
3028 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
3029 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3030 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3031 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003032
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003033 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003034 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01003035 else if (channel->band == IEEE80211_BAND_5GHZ)
3036 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003037
3038 if (conf->channel_type == NL80211_CHAN_NO_HT ||
3039 conf->channel_type == NL80211_CHAN_HT20)
3040 cmd->channel_flags |= cpu_to_le32(0x00000080);
3041 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
3042 cmd->channel_flags |= cpu_to_le32(0x000001900);
3043 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
3044 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003045
3046 rc = mwl8k_post_cmd(hw, &cmd->header);
3047 kfree(cmd);
3048
3049 return rc;
3050}
3051
3052/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003053 * CMD_SET_AID.
3054 */
3055#define MWL8K_FRAME_PROT_DISABLED 0x00
3056#define MWL8K_FRAME_PROT_11G 0x07
3057#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
3058#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
3059
3060struct mwl8k_cmd_update_set_aid {
3061 struct mwl8k_cmd_pkt header;
3062 __le16 aid;
3063
3064 /* AP's MAC address (BSSID) */
3065 __u8 bssid[ETH_ALEN];
3066 __le16 protection_mode;
3067 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003068} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003069
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003070static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
3071{
3072 int i;
3073 int j;
3074
3075 /*
3076 * Clear nonstandard rates 4 and 13.
3077 */
3078 mask &= 0x1fef;
3079
3080 for (i = 0, j = 0; i < 14; i++) {
3081 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01003082 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003083 }
3084}
3085
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003086static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003087mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
3088 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003089{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003090 struct mwl8k_cmd_update_set_aid *cmd;
3091 u16 prot_mode;
3092 int rc;
3093
3094 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3095 if (cmd == NULL)
3096 return -ENOMEM;
3097
3098 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
3099 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003100 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003101 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003102
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003103 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003104 prot_mode = MWL8K_FRAME_PROT_11G;
3105 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003106 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003107 IEEE80211_HT_OP_MODE_PROTECTION) {
3108 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
3109 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
3110 break;
3111 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
3112 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
3113 break;
3114 default:
3115 prot_mode = MWL8K_FRAME_PROT_DISABLED;
3116 break;
3117 }
3118 }
3119 cmd->protection_mode = cpu_to_le16(prot_mode);
3120
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003121 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003122
3123 rc = mwl8k_post_cmd(hw, &cmd->header);
3124 kfree(cmd);
3125
3126 return rc;
3127}
3128
3129/*
3130 * CMD_SET_RATE.
3131 */
3132struct mwl8k_cmd_set_rate {
3133 struct mwl8k_cmd_pkt header;
3134 __u8 legacy_rates[14];
3135
3136 /* Bitmap for supported MCS codes. */
3137 __u8 mcs_set[16];
3138 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003139} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003140
3141static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003142mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003143 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003144{
3145 struct mwl8k_cmd_set_rate *cmd;
3146 int rc;
3147
3148 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3149 if (cmd == NULL)
3150 return -ENOMEM;
3151
3152 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
3153 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003154 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003155 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003156
3157 rc = mwl8k_post_cmd(hw, &cmd->header);
3158 kfree(cmd);
3159
3160 return rc;
3161}
3162
3163/*
3164 * CMD_FINALIZE_JOIN.
3165 */
3166#define MWL8K_FJ_BEACON_MAXLEN 128
3167
3168struct mwl8k_cmd_finalize_join {
3169 struct mwl8k_cmd_pkt header;
3170 __le32 sleep_interval; /* Number of beacon periods to sleep */
3171 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003172} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003173
3174static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
3175 int framelen, int dtim)
3176{
3177 struct mwl8k_cmd_finalize_join *cmd;
3178 struct ieee80211_mgmt *payload = frame;
3179 int payload_len;
3180 int rc;
3181
3182 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3183 if (cmd == NULL)
3184 return -ENOMEM;
3185
3186 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
3187 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3188 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
3189
3190 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
3191 if (payload_len < 0)
3192 payload_len = 0;
3193 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
3194 payload_len = MWL8K_FJ_BEACON_MAXLEN;
3195
3196 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
3197
3198 rc = mwl8k_post_cmd(hw, &cmd->header);
3199 kfree(cmd);
3200
3201 return rc;
3202}
3203
3204/*
3205 * CMD_SET_RTS_THRESHOLD.
3206 */
3207struct mwl8k_cmd_set_rts_threshold {
3208 struct mwl8k_cmd_pkt header;
3209 __le16 action;
3210 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003211} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003212
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003213static int
3214mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003215{
3216 struct mwl8k_cmd_set_rts_threshold *cmd;
3217 int rc;
3218
3219 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3220 if (cmd == NULL)
3221 return -ENOMEM;
3222
3223 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3224 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003225 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3226 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003227
3228 rc = mwl8k_post_cmd(hw, &cmd->header);
3229 kfree(cmd);
3230
3231 return rc;
3232}
3233
3234/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003235 * CMD_SET_SLOT.
3236 */
3237struct mwl8k_cmd_set_slot {
3238 struct mwl8k_cmd_pkt header;
3239 __le16 action;
3240 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003241} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003242
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02003243static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003244{
3245 struct mwl8k_cmd_set_slot *cmd;
3246 int rc;
3247
3248 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3249 if (cmd == NULL)
3250 return -ENOMEM;
3251
3252 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3253 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3254 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02003255 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003256
3257 rc = mwl8k_post_cmd(hw, &cmd->header);
3258 kfree(cmd);
3259
3260 return rc;
3261}
3262
3263/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003264 * CMD_SET_EDCA_PARAMS.
3265 */
3266struct mwl8k_cmd_set_edca_params {
3267 struct mwl8k_cmd_pkt header;
3268
3269 /* See MWL8K_SET_EDCA_XXX below */
3270 __le16 action;
3271
3272 /* TX opportunity in units of 32 us */
3273 __le16 txop;
3274
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003275 union {
3276 struct {
3277 /* Log exponent of max contention period: 0...15 */
3278 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003279
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003280 /* Log exponent of min contention period: 0...15 */
3281 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003282
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003283 /* Adaptive interframe spacing in units of 32us */
3284 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003285
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003286 /* TX queue to configure */
3287 __u8 txq;
3288 } ap;
3289 struct {
3290 /* Log exponent of max contention period: 0...15 */
3291 __u8 log_cw_max;
3292
3293 /* Log exponent of min contention period: 0...15 */
3294 __u8 log_cw_min;
3295
3296 /* Adaptive interframe spacing in units of 32us */
3297 __u8 aifs;
3298
3299 /* TX queue to configure */
3300 __u8 txq;
3301 } sta;
3302 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003303} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003304
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003305#define MWL8K_SET_EDCA_CW 0x01
3306#define MWL8K_SET_EDCA_TXOP 0x02
3307#define MWL8K_SET_EDCA_AIFS 0x04
3308
3309#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
3310 MWL8K_SET_EDCA_TXOP | \
3311 MWL8K_SET_EDCA_AIFS)
3312
3313static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003314mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3315 __u16 cw_min, __u16 cw_max,
3316 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003317{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003318 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003319 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003320 int rc;
3321
3322 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3323 if (cmd == NULL)
3324 return -ENOMEM;
3325
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003326 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3327 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003328 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3329 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003330 if (priv->ap_fw) {
3331 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3332 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3333 cmd->ap.aifs = aifs;
3334 cmd->ap.txq = qnum;
3335 } else {
3336 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3337 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3338 cmd->sta.aifs = aifs;
3339 cmd->sta.txq = qnum;
3340 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003341
3342 rc = mwl8k_post_cmd(hw, &cmd->header);
3343 kfree(cmd);
3344
3345 return rc;
3346}
3347
3348/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003349 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003350 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003351struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003352 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003353 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003354} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003355
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003356static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003357{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003358 struct mwl8k_priv *priv = hw->priv;
3359 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003360 int rc;
3361
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003362 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3363 if (cmd == NULL)
3364 return -ENOMEM;
3365
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003366 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003367 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003368 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003369
3370 rc = mwl8k_post_cmd(hw, &cmd->header);
3371 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01003372
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003373 if (!rc)
3374 priv->wmm_enabled = enable;
3375
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003376 return rc;
3377}
3378
3379/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003380 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003381 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003382struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003383 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003384 __le32 action;
3385 __u8 rx_antenna_map;
3386 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003387} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003388
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003389static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003390{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003391 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003392 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003393
3394 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3395 if (cmd == NULL)
3396 return -ENOMEM;
3397
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003398 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003399 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003400 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3401 cmd->rx_antenna_map = rx;
3402 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003403
3404 rc = mwl8k_post_cmd(hw, &cmd->header);
3405 kfree(cmd);
3406
3407 return rc;
3408}
3409
3410/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003411 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003412 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003413struct mwl8k_cmd_use_fixed_rate_sta {
3414 struct mwl8k_cmd_pkt header;
3415 __le32 action;
3416 __le32 allow_rate_drop;
3417 __le32 num_rates;
3418 struct {
3419 __le32 is_ht_rate;
3420 __le32 enable_retry;
3421 __le32 rate;
3422 __le32 retry_count;
3423 } rate_entry[8];
3424 __le32 rate_type;
3425 __le32 reserved1;
3426 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003427} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003428
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003429#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003430#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003431
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003432static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003433{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003434 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003435 int rc;
3436
3437 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3438 if (cmd == NULL)
3439 return -ENOMEM;
3440
3441 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3442 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003443 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3444 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003445
3446 rc = mwl8k_post_cmd(hw, &cmd->header);
3447 kfree(cmd);
3448
3449 return rc;
3450}
3451
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003452/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003453 * CMD_USE_FIXED_RATE (AP version).
3454 */
3455struct mwl8k_cmd_use_fixed_rate_ap {
3456 struct mwl8k_cmd_pkt header;
3457 __le32 action;
3458 __le32 allow_rate_drop;
3459 __le32 num_rates;
3460 struct mwl8k_rate_entry_ap {
3461 __le32 is_ht_rate;
3462 __le32 enable_retry;
3463 __le32 rate;
3464 __le32 retry_count;
3465 } rate_entry[4];
3466 u8 multicast_rate;
3467 u8 multicast_rate_type;
3468 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003469} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003470
3471static int
3472mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3473{
3474 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3475 int rc;
3476
3477 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3478 if (cmd == NULL)
3479 return -ENOMEM;
3480
3481 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3482 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3483 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3484 cmd->multicast_rate = mcast;
3485 cmd->management_rate = mgmt;
3486
3487 rc = mwl8k_post_cmd(hw, &cmd->header);
3488 kfree(cmd);
3489
3490 return rc;
3491}
3492
3493/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003494 * CMD_ENABLE_SNIFFER.
3495 */
3496struct mwl8k_cmd_enable_sniffer {
3497 struct mwl8k_cmd_pkt header;
3498 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003499} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003500
3501static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3502{
3503 struct mwl8k_cmd_enable_sniffer *cmd;
3504 int rc;
3505
3506 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3507 if (cmd == NULL)
3508 return -ENOMEM;
3509
3510 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3511 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3512 cmd->action = cpu_to_le32(!!enable);
3513
3514 rc = mwl8k_post_cmd(hw, &cmd->header);
3515 kfree(cmd);
3516
3517 return rc;
3518}
3519
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303520struct mwl8k_cmd_update_mac_addr {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003521 struct mwl8k_cmd_pkt header;
3522 union {
3523 struct {
3524 __le16 mac_type;
3525 __u8 mac_addr[ETH_ALEN];
3526 } mbss;
3527 __u8 mac_addr[ETH_ALEN];
3528 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003529} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003530
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003531#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3532#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3533#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3534#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003535
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303536static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3537 struct ieee80211_vif *vif, u8 *mac, bool set)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003538{
3539 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003540 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303541 struct mwl8k_cmd_update_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003542 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003543 int rc;
3544
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003545 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3546 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3547 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
Yogesh Ashok Powaraf458832013-01-25 16:20:12 +05303548 if (priv->ap_fw)
3549 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3550 else
3551 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003552 else
3553 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3554 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3555 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3556 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3557 else
3558 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3559 }
3560
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003561 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3562 if (cmd == NULL)
3563 return -ENOMEM;
3564
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303565 if (set)
3566 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3567 else
3568 cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3569
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003570 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3571 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003572 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003573 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3574 } else {
3575 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3576 }
3577
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003578 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003579 kfree(cmd);
3580
3581 return rc;
3582}
3583
3584/*
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303585 * MWL8K_CMD_SET_MAC_ADDR.
3586 */
3587static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3588 struct ieee80211_vif *vif, u8 *mac)
3589{
3590 return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3591}
3592
3593/*
3594 * MWL8K_CMD_DEL_MAC_ADDR.
3595 */
3596static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3597 struct ieee80211_vif *vif, u8 *mac)
3598{
3599 return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3600}
3601
3602/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003603 * CMD_SET_RATEADAPT_MODE.
3604 */
3605struct mwl8k_cmd_set_rate_adapt_mode {
3606 struct mwl8k_cmd_pkt header;
3607 __le16 action;
3608 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003609} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003610
3611static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3612{
3613 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3614 int rc;
3615
3616 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3617 if (cmd == NULL)
3618 return -ENOMEM;
3619
3620 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3621 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3622 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3623 cmd->mode = cpu_to_le16(mode);
3624
3625 rc = mwl8k_post_cmd(hw, &cmd->header);
3626 kfree(cmd);
3627
3628 return rc;
3629}
3630
3631/*
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003632 * CMD_GET_WATCHDOG_BITMAP.
3633 */
3634struct mwl8k_cmd_get_watchdog_bitmap {
3635 struct mwl8k_cmd_pkt header;
3636 u8 bitmap;
3637} __packed;
3638
3639static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3640{
3641 struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3642 int rc;
3643
3644 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3645 if (cmd == NULL)
3646 return -ENOMEM;
3647
3648 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3649 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3650
3651 rc = mwl8k_post_cmd(hw, &cmd->header);
3652 if (!rc)
3653 *bitmap = cmd->bitmap;
3654
3655 kfree(cmd);
3656
3657 return rc;
3658}
3659
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303660#define MWL8K_WMM_QUEUE_NUMBER 3
3661
3662static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3663 u8 idx);
3664
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003665static void mwl8k_watchdog_ba_events(struct work_struct *work)
3666{
3667 int rc;
3668 u8 bitmap = 0, stream_index;
3669 struct mwl8k_ampdu_stream *streams;
3670 struct mwl8k_priv *priv =
3671 container_of(work, struct mwl8k_priv, watchdog_ba_handle);
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303672 struct ieee80211_hw *hw = priv->hw;
3673 int i;
3674 u32 status = 0;
3675
3676 mwl8k_fw_lock(hw);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003677
3678 rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3679 if (rc)
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303680 goto done;
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003681
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303682 spin_lock(&priv->stream_lock);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003683
3684 /* the bitmap is the hw queue number. Map it to the ampdu queue. */
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303685 for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
3686 if (bitmap & (1 << i)) {
3687 stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
3688 TOTAL_HW_TX_QUEUES;
3689 streams = &priv->ampdu[stream_index];
3690 if (streams->state == AMPDU_STREAM_ACTIVE) {
3691 ieee80211_stop_tx_ba_session(streams->sta,
3692 streams->tid);
3693 spin_unlock(&priv->stream_lock);
3694 mwl8k_destroy_ba(hw, stream_index);
3695 spin_lock(&priv->stream_lock);
3696 }
3697 }
3698 }
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003699
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303700 spin_unlock(&priv->stream_lock);
3701done:
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05303702 atomic_dec(&priv->watchdog_event_pending);
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303703 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3704 iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
3705 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3706 mwl8k_fw_unlock(hw);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003707 return;
3708}
3709
3710
3711/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003712 * CMD_BSS_START.
3713 */
3714struct mwl8k_cmd_bss_start {
3715 struct mwl8k_cmd_pkt header;
3716 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003717} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003718
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003719static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3720 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003721{
3722 struct mwl8k_cmd_bss_start *cmd;
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303723 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3724 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003725 int rc;
3726
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303727 if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
3728 return 0;
3729
3730 if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
3731 return 0;
3732
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003733 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3734 if (cmd == NULL)
3735 return -ENOMEM;
3736
3737 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3738 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3739 cmd->enable = cpu_to_le32(enable);
3740
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003741 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003742 kfree(cmd);
3743
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303744 if (!rc) {
3745 if (enable)
3746 priv->running_bsses |= (1 << mwl8k_vif->macid);
3747 else
3748 priv->running_bsses &= ~(1 << mwl8k_vif->macid);
3749 }
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003750 return rc;
3751}
3752
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303753static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
3754{
3755 struct mwl8k_priv *priv = hw->priv;
3756 struct mwl8k_vif *mwl8k_vif, *tmp_vif;
3757 struct ieee80211_vif *vif;
3758
3759 list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
3760 vif = mwl8k_vif->vif;
3761
3762 if (!(bitmap & (1 << mwl8k_vif->macid)))
3763 continue;
3764
3765 if (vif->type == NL80211_IFTYPE_AP)
3766 mwl8k_cmd_bss_start(hw, vif, enable);
3767 }
3768}
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003769/*
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003770 * CMD_BASTREAM.
3771 */
3772
3773/*
3774 * UPSTREAM is tx direction
3775 */
3776#define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3777#define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3778
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303779enum ba_stream_action_type {
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003780 MWL8K_BA_CREATE,
3781 MWL8K_BA_UPDATE,
3782 MWL8K_BA_DESTROY,
3783 MWL8K_BA_FLUSH,
3784 MWL8K_BA_CHECK,
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303785};
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003786
3787
3788struct mwl8k_create_ba_stream {
3789 __le32 flags;
3790 __le32 idle_thrs;
3791 __le32 bar_thrs;
3792 __le32 window_size;
3793 u8 peer_mac_addr[6];
3794 u8 dialog_token;
3795 u8 tid;
3796 u8 queue_id;
3797 u8 param_info;
3798 __le32 ba_context;
3799 u8 reset_seq_no_flag;
3800 __le16 curr_seq_no;
3801 u8 sta_src_mac_addr[6];
3802} __packed;
3803
3804struct mwl8k_destroy_ba_stream {
3805 __le32 flags;
3806 __le32 ba_context;
3807} __packed;
3808
3809struct mwl8k_cmd_bastream {
3810 struct mwl8k_cmd_pkt header;
3811 __le32 action;
3812 union {
3813 struct mwl8k_create_ba_stream create_params;
3814 struct mwl8k_destroy_ba_stream destroy_params;
3815 };
3816} __packed;
3817
3818static int
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303819mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3820 struct ieee80211_vif *vif)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003821{
3822 struct mwl8k_cmd_bastream *cmd;
3823 int rc;
3824
3825 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3826 if (cmd == NULL)
3827 return -ENOMEM;
3828
3829 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3830 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3831
3832 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3833
3834 cmd->create_params.queue_id = stream->idx;
3835 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3836 ETH_ALEN);
3837 cmd->create_params.tid = stream->tid;
3838
3839 cmd->create_params.flags =
3840 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3841 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3842
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303843 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003844
3845 kfree(cmd);
3846
3847 return rc;
3848}
3849
3850static int
3851mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303852 u8 buf_size, struct ieee80211_vif *vif)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003853{
3854 struct mwl8k_cmd_bastream *cmd;
3855 int rc;
3856
3857 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3858 if (cmd == NULL)
3859 return -ENOMEM;
3860
3861
3862 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3863 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3864
3865 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3866
3867 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3868 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3869 cmd->create_params.queue_id = stream->idx;
3870
3871 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3872 cmd->create_params.tid = stream->tid;
3873 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3874 cmd->create_params.reset_seq_no_flag = 1;
3875
3876 cmd->create_params.param_info =
3877 (stream->sta->ht_cap.ampdu_factor &
3878 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3879 ((stream->sta->ht_cap.ampdu_density << 2) &
3880 IEEE80211_HT_AMPDU_PARM_DENSITY);
3881
3882 cmd->create_params.flags =
3883 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3884 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3885
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303886 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003887
3888 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3889 stream->sta->addr, stream->tid);
3890 kfree(cmd);
3891
3892 return rc;
3893}
3894
3895static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303896 u8 idx)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003897{
3898 struct mwl8k_cmd_bastream *cmd;
3899
3900 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3901 if (cmd == NULL)
3902 return;
3903
3904 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3905 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3906 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3907
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303908 cmd->destroy_params.ba_context = cpu_to_le32(idx);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003909 mwl8k_post_cmd(hw, &cmd->header);
3910
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303911 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003912
3913 kfree(cmd);
3914}
3915
3916/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003917 * CMD_SET_NEW_STN.
3918 */
3919struct mwl8k_cmd_set_new_stn {
3920 struct mwl8k_cmd_pkt header;
3921 __le16 aid;
3922 __u8 mac_addr[6];
3923 __le16 stn_id;
3924 __le16 action;
3925 __le16 rsvd;
3926 __le32 legacy_rates;
3927 __u8 ht_rates[4];
3928 __le16 cap_info;
3929 __le16 ht_capabilities_info;
3930 __u8 mac_ht_param_info;
3931 __u8 rev;
3932 __u8 control_channel;
3933 __u8 add_channel;
3934 __le16 op_mode;
3935 __le16 stbc;
3936 __u8 add_qos_info;
3937 __u8 is_qos_sta;
3938 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003939} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003940
3941#define MWL8K_STA_ACTION_ADD 0
3942#define MWL8K_STA_ACTION_REMOVE 2
3943
3944static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3945 struct ieee80211_vif *vif,
3946 struct ieee80211_sta *sta)
3947{
3948 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003949 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003950 int rc;
3951
3952 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3953 if (cmd == NULL)
3954 return -ENOMEM;
3955
3956 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3957 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3958 cmd->aid = cpu_to_le16(sta->aid);
3959 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3960 cmd->stn_id = cpu_to_le16(sta->aid);
3961 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003962 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3963 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3964 else
3965 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3966 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003967 if (sta->ht_cap.ht_supported) {
3968 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3969 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3970 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3971 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3972 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3973 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3974 ((sta->ht_cap.ampdu_density & 7) << 2);
3975 cmd->is_qos_sta = 1;
3976 }
3977
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003978 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003979 kfree(cmd);
3980
3981 return rc;
3982}
3983
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003984static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3985 struct ieee80211_vif *vif)
3986{
3987 struct mwl8k_cmd_set_new_stn *cmd;
3988 int rc;
3989
3990 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3991 if (cmd == NULL)
3992 return -ENOMEM;
3993
3994 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3995 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3996 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3997
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003998 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003999 kfree(cmd);
4000
4001 return rc;
4002}
4003
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004004static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
4005 struct ieee80211_vif *vif, u8 *addr)
4006{
4007 struct mwl8k_cmd_set_new_stn *cmd;
Yogesh Ashok Powar0dd13a42013-01-08 10:16:37 +05304008 struct mwl8k_priv *priv = hw->priv;
4009 int rc, i;
4010 u8 idx;
4011
4012 spin_lock(&priv->stream_lock);
4013 /* Destroy any active ampdu streams for this sta */
4014 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
4015 struct mwl8k_ampdu_stream *s;
4016 s = &priv->ampdu[i];
4017 if (s->state != AMPDU_NO_STREAM) {
4018 if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
4019 if (s->state == AMPDU_STREAM_ACTIVE) {
4020 idx = s->idx;
4021 spin_unlock(&priv->stream_lock);
4022 mwl8k_destroy_ba(hw, idx);
4023 spin_lock(&priv->stream_lock);
4024 } else if (s->state == AMPDU_STREAM_NEW) {
4025 mwl8k_remove_stream(hw, s);
4026 }
4027 }
4028 }
4029 }
4030
4031 spin_unlock(&priv->stream_lock);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004032
4033 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4034 if (cmd == NULL)
4035 return -ENOMEM;
4036
4037 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4038 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4039 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4040 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
4041
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004042 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004043 kfree(cmd);
4044
4045 return rc;
4046}
4047
4048/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004049 * CMD_UPDATE_ENCRYPTION.
4050 */
4051
4052#define MAX_ENCR_KEY_LENGTH 16
4053#define MIC_KEY_LENGTH 8
4054
4055struct mwl8k_cmd_update_encryption {
4056 struct mwl8k_cmd_pkt header;
4057
4058 __le32 action;
4059 __le32 reserved;
4060 __u8 mac_addr[6];
4061 __u8 encr_type;
4062
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304063} __packed;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004064
4065struct mwl8k_cmd_set_key {
4066 struct mwl8k_cmd_pkt header;
4067
4068 __le32 action;
4069 __le32 reserved;
4070 __le16 length;
4071 __le16 key_type_id;
4072 __le32 key_info;
4073 __le32 key_id;
4074 __le16 key_len;
4075 __u8 key_material[MAX_ENCR_KEY_LENGTH];
4076 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
4077 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
4078 __le16 tkip_rsc_low;
4079 __le32 tkip_rsc_high;
4080 __le16 tkip_tsc_low;
4081 __le32 tkip_tsc_high;
4082 __u8 mac_addr[6];
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304083} __packed;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004084
4085enum {
4086 MWL8K_ENCR_ENABLE,
4087 MWL8K_ENCR_SET_KEY,
4088 MWL8K_ENCR_REMOVE_KEY,
4089 MWL8K_ENCR_SET_GROUP_KEY,
4090};
4091
4092#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
4093#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
4094#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
4095#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
4096#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
4097
4098enum {
4099 MWL8K_ALG_WEP,
4100 MWL8K_ALG_TKIP,
4101 MWL8K_ALG_CCMP,
4102};
4103
4104#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
4105#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
4106#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
4107#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
4108#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
4109
4110static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
4111 struct ieee80211_vif *vif,
4112 u8 *addr,
4113 u8 encr_type)
4114{
4115 struct mwl8k_cmd_update_encryption *cmd;
4116 int rc;
4117
4118 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4119 if (cmd == NULL)
4120 return -ENOMEM;
4121
4122 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4123 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4124 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
4125 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4126 cmd->encr_type = encr_type;
4127
4128 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4129 kfree(cmd);
4130
4131 return rc;
4132}
4133
4134static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4135 u8 *addr,
4136 struct ieee80211_key_conf *key)
4137{
4138 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4139 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4140 cmd->length = cpu_to_le16(sizeof(*cmd) -
4141 offsetof(struct mwl8k_cmd_set_key, length));
4142 cmd->key_id = cpu_to_le32(key->keyidx);
4143 cmd->key_len = cpu_to_le16(key->keylen);
4144 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4145
4146 switch (key->cipher) {
4147 case WLAN_CIPHER_SUITE_WEP40:
4148 case WLAN_CIPHER_SUITE_WEP104:
4149 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4150 if (key->keyidx == 0)
4151 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4152
4153 break;
4154 case WLAN_CIPHER_SUITE_TKIP:
4155 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4156 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4157 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4158 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4159 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4160 | MWL8K_KEY_FLAG_TSC_VALID);
4161 break;
4162 case WLAN_CIPHER_SUITE_CCMP:
4163 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4164 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4165 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4166 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4167 break;
4168 default:
4169 return -ENOTSUPP;
4170 }
4171
4172 return 0;
4173}
4174
4175static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4176 struct ieee80211_vif *vif,
4177 u8 *addr,
4178 struct ieee80211_key_conf *key)
4179{
4180 struct mwl8k_cmd_set_key *cmd;
4181 int rc;
4182 int keymlen;
4183 u32 action;
4184 u8 idx;
4185 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4186
4187 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4188 if (cmd == NULL)
4189 return -ENOMEM;
4190
4191 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4192 if (rc < 0)
4193 goto done;
4194
4195 idx = key->keyidx;
4196
4197 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4198 action = MWL8K_ENCR_SET_KEY;
4199 else
4200 action = MWL8K_ENCR_SET_GROUP_KEY;
4201
4202 switch (key->cipher) {
4203 case WLAN_CIPHER_SUITE_WEP40:
4204 case WLAN_CIPHER_SUITE_WEP104:
4205 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4206 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4207 sizeof(*key) + key->keylen);
4208 mwl8k_vif->wep_key_conf[idx].enabled = 1;
4209 }
4210
Yogesh Ashok Powar9b571e22011-05-04 17:22:16 +05304211 keymlen = key->keylen;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004212 action = MWL8K_ENCR_SET_KEY;
4213 break;
4214 case WLAN_CIPHER_SUITE_TKIP:
4215 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4216 break;
4217 case WLAN_CIPHER_SUITE_CCMP:
4218 keymlen = key->keylen;
4219 break;
4220 default:
4221 rc = -ENOTSUPP;
4222 goto done;
4223 }
4224
4225 memcpy(cmd->key_material, key->key, keymlen);
4226 cmd->action = cpu_to_le32(action);
4227
4228 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4229done:
4230 kfree(cmd);
4231
4232 return rc;
4233}
4234
4235static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4236 struct ieee80211_vif *vif,
4237 u8 *addr,
4238 struct ieee80211_key_conf *key)
4239{
4240 struct mwl8k_cmd_set_key *cmd;
4241 int rc;
4242 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4243
4244 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4245 if (cmd == NULL)
4246 return -ENOMEM;
4247
4248 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4249 if (rc < 0)
4250 goto done;
4251
4252 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Dan Carpenterd981e052012-01-17 10:33:31 +03004253 key->cipher == WLAN_CIPHER_SUITE_WEP104)
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004254 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4255
4256 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4257
4258 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4259done:
4260 kfree(cmd);
4261
4262 return rc;
4263}
4264
4265static int mwl8k_set_key(struct ieee80211_hw *hw,
4266 enum set_key_cmd cmd_param,
4267 struct ieee80211_vif *vif,
4268 struct ieee80211_sta *sta,
4269 struct ieee80211_key_conf *key)
4270{
4271 int rc = 0;
4272 u8 encr_type;
4273 u8 *addr;
4274 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Yogesh Ashok Powar751930c2013-01-25 16:20:28 +05304275 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004276
Yogesh Ashok Powar751930c2013-01-25 16:20:28 +05304277 if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004278 return -EOPNOTSUPP;
4279
4280 if (sta == NULL)
Yogesh Ashok Powarff7e9f92012-01-27 16:08:27 +05304281 addr = vif->addr;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004282 else
4283 addr = sta->addr;
4284
4285 if (cmd_param == SET_KEY) {
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004286 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4287 if (rc)
4288 goto out;
4289
4290 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4291 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4292 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4293 else
4294 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4295
4296 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4297 encr_type);
4298 if (rc)
4299 goto out;
4300
4301 mwl8k_vif->is_hw_crypto_enabled = true;
4302
4303 } else {
4304 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4305
4306 if (rc)
4307 goto out;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004308 }
4309out:
4310 return rc;
4311}
4312
4313/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004314 * CMD_UPDATE_STADB.
4315 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004316struct ewc_ht_info {
4317 __le16 control1;
4318 __le16 control2;
4319 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004320} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004321
4322struct peer_capability_info {
4323 /* Peer type - AP vs. STA. */
4324 __u8 peer_type;
4325
4326 /* Basic 802.11 capabilities from assoc resp. */
4327 __le16 basic_caps;
4328
4329 /* Set if peer supports 802.11n high throughput (HT). */
4330 __u8 ht_support;
4331
4332 /* Valid if HT is supported. */
4333 __le16 ht_caps;
4334 __u8 extended_ht_caps;
4335 struct ewc_ht_info ewc_info;
4336
4337 /* Legacy rate table. Intersection of our rates and peer rates. */
4338 __u8 legacy_rates[12];
4339
4340 /* HT rate table. Intersection of our rates and peer rates. */
4341 __u8 ht_rates[16];
4342 __u8 pad[16];
4343
4344 /* If set, interoperability mode, no proprietary extensions. */
4345 __u8 interop;
4346 __u8 pad2;
4347 __u8 station_id;
4348 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004349} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004350
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004351struct mwl8k_cmd_update_stadb {
4352 struct mwl8k_cmd_pkt header;
4353
4354 /* See STADB_ACTION_TYPE */
4355 __le32 action;
4356
4357 /* Peer MAC address */
4358 __u8 peer_addr[ETH_ALEN];
4359
4360 __le32 reserved;
4361
4362 /* Peer info - valid during add/update. */
4363 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004364} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004365
Lennert Buytenheka6804002010-01-04 21:55:42 +01004366#define MWL8K_STA_DB_MODIFY_ENTRY 1
4367#define MWL8K_STA_DB_DEL_ENTRY 2
4368
4369/* Peer Entry flags - used to define the type of the peer node */
4370#define MWL8K_PEER_TYPE_ACCESSPOINT 2
4371
4372static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004373 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004374 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004375{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004376 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004377 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004378 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004379 int rc;
4380
4381 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4382 if (cmd == NULL)
4383 return -ENOMEM;
4384
4385 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4386 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01004387 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004388 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004389
Lennert Buytenheka6804002010-01-04 21:55:42 +01004390 p = &cmd->peer_info;
4391 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4392 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004393 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04004394 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004395 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
4396 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004397 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4398 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
4399 else
4400 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4401 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004402 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004403 p->interop = 1;
4404 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004405
Lennert Buytenheka6804002010-01-04 21:55:42 +01004406 rc = mwl8k_post_cmd(hw, &cmd->header);
Nickolai Zeldovichc4f74d32013-01-06 20:27:22 -05004407 if (!rc)
4408 rc = p->station_id;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004409 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004410
Nickolai Zeldovichc4f74d32013-01-06 20:27:22 -05004411 return rc;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004412}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004413
Lennert Buytenheka6804002010-01-04 21:55:42 +01004414static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4415 struct ieee80211_vif *vif, u8 *addr)
4416{
4417 struct mwl8k_cmd_update_stadb *cmd;
4418 int rc;
4419
4420 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4421 if (cmd == NULL)
4422 return -ENOMEM;
4423
4424 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4425 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4426 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4427 memcpy(cmd->peer_addr, addr, ETH_ALEN);
4428
4429 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004430 kfree(cmd);
4431
4432 return rc;
4433}
4434
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004435
4436/*
4437 * Interrupt handling.
4438 */
4439static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4440{
4441 struct ieee80211_hw *hw = dev_id;
4442 struct mwl8k_priv *priv = hw->priv;
4443 u32 status;
4444
4445 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004446 if (!status)
4447 return IRQ_NONE;
4448
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004449 if (status & MWL8K_A2H_INT_TX_DONE) {
4450 status &= ~MWL8K_A2H_INT_TX_DONE;
4451 tasklet_schedule(&priv->poll_tx_task);
4452 }
4453
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004454 if (status & MWL8K_A2H_INT_RX_READY) {
4455 status &= ~MWL8K_A2H_INT_RX_READY;
4456 tasklet_schedule(&priv->poll_rx_task);
4457 }
4458
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004459 if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05304460 iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
4461 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4462
4463 atomic_inc(&priv->watchdog_event_pending);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004464 status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4465 ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4466 }
4467
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004468 if (status)
4469 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004470
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004471 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004472 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004473 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004474 }
4475
4476 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004477 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004478 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004479 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004480 }
4481
4482 return IRQ_HANDLED;
4483}
4484
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004485static void mwl8k_tx_poll(unsigned long data)
4486{
4487 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4488 struct mwl8k_priv *priv = hw->priv;
4489 int limit;
4490 int i;
4491
4492 limit = 32;
4493
4494 spin_lock_bh(&priv->tx_lock);
4495
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004496 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004497 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4498
4499 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4500 complete(priv->tx_wait);
4501 priv->tx_wait = NULL;
4502 }
4503
4504 spin_unlock_bh(&priv->tx_lock);
4505
4506 if (limit) {
4507 writel(~MWL8K_A2H_INT_TX_DONE,
4508 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4509 } else {
4510 tasklet_schedule(&priv->poll_tx_task);
4511 }
4512}
4513
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004514static void mwl8k_rx_poll(unsigned long data)
4515{
4516 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4517 struct mwl8k_priv *priv = hw->priv;
4518 int limit;
4519
4520 limit = 32;
4521 limit -= rxq_process(hw, 0, limit);
4522 limit -= rxq_refill(hw, 0, limit);
4523
4524 if (limit) {
4525 writel(~MWL8K_A2H_INT_RX_READY,
4526 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4527 } else {
4528 tasklet_schedule(&priv->poll_rx_task);
4529 }
4530}
4531
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004532
4533/*
4534 * Core driver operations.
4535 */
Thomas Huehn36323f82012-07-23 21:33:42 +02004536static void mwl8k_tx(struct ieee80211_hw *hw,
4537 struct ieee80211_tx_control *control,
4538 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004539{
4540 struct mwl8k_priv *priv = hw->priv;
4541 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004542
Lennert Buytenhek9189c102010-01-12 13:47:32 +01004543 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004544 wiphy_debug(hw->wiphy,
4545 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004546 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01004547 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004548 }
4549
Thomas Huehn36323f82012-07-23 21:33:42 +02004550 mwl8k_txq_xmit(hw, index, control->sta, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004551}
4552
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004553static int mwl8k_start(struct ieee80211_hw *hw)
4554{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004555 struct mwl8k_priv *priv = hw->priv;
4556 int rc;
4557
Joe Perchesa0607fd2009-11-18 23:29:17 -08004558 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004559 IRQF_SHARED, MWL8K_NAME, hw);
4560 if (rc) {
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304561 priv->irq = -1;
Joe Perches5db55842010-08-11 19:11:19 -07004562 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004563 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004564 }
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304565 priv->irq = priv->pdev->irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004566
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004567 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004568 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004569 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004570
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004571 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004572 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam12488e02011-04-08 14:38:27 +05304573 iowrite32(MWL8K_A2H_EVENTS,
4574 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004575
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004576 rc = mwl8k_fw_lock(hw);
4577 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004578 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004579
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004580 if (!priv->ap_fw) {
4581 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004582 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004583
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004584 if (!rc)
4585 rc = mwl8k_cmd_set_pre_scan(hw);
4586
4587 if (!rc)
4588 rc = mwl8k_cmd_set_post_scan(hw,
4589 "\x00\x00\x00\x00\x00\x00");
4590 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004591
4592 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004593 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004594
4595 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004596 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004597
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004598 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004599 }
4600
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004601 if (rc) {
4602 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4603 free_irq(priv->pdev->irq, hw);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304604 priv->irq = -1;
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004605 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004606 tasklet_disable(&priv->poll_rx_task);
Nishant Sarmukadam4850b6d2013-01-08 12:10:53 +05304607 } else {
4608 ieee80211_wake_queues(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004609 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004610
4611 return rc;
4612}
4613
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004614static void mwl8k_stop(struct ieee80211_hw *hw)
4615{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004616 struct mwl8k_priv *priv = hw->priv;
4617 int i;
4618
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304619 if (!priv->hw_restart_in_progress)
4620 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004621
4622 ieee80211_stop_queues(hw);
4623
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004624 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004625 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304626 if (priv->irq != -1) {
4627 free_irq(priv->pdev->irq, hw);
4628 priv->irq = -1;
4629 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004630
4631 /* Stop finalize join worker */
4632 cancel_work_sync(&priv->finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004633 cancel_work_sync(&priv->watchdog_ba_handle);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004634 if (priv->beacon_skb != NULL)
4635 dev_kfree_skb(priv->beacon_skb);
4636
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004637 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004638 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004639 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004640
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004641 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004642 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004643 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004644}
4645
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004646static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4647
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004648static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004649 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004650{
4651 struct mwl8k_priv *priv = hw->priv;
4652 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004653 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004654 int macid, rc;
4655 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004656
4657 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004658 * Reject interface creation if sniffer mode is active, as
4659 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004660 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004661 */
4662 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004663 wiphy_info(hw->wiphy,
4664 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004665 return -EINVAL;
4666 }
4667
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004668 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004669 switch (vif->type) {
4670 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004671 if (!priv->ap_fw && di->fw_image_ap) {
4672 /* we must load the ap fw to meet this request */
4673 if (!list_empty(&priv->vif_list))
4674 return -EBUSY;
4675 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4676 if (rc)
4677 return rc;
4678 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004679 macids_supported = priv->ap_macids_supported;
4680 break;
4681 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004682 if (priv->ap_fw && di->fw_image_sta) {
Yogesh Ashok Poward59c1cf2013-01-25 16:20:03 +05304683 if (!list_empty(&priv->vif_list)) {
4684 wiphy_warn(hw->wiphy, "AP interface is running.\n"
4685 "Adding STA interface for WDS");
4686 } else {
4687 /* we must load the sta fw to
4688 * meet this request.
4689 */
4690 rc = mwl8k_reload_firmware(hw,
4691 di->fw_image_sta);
4692 if (rc)
4693 return rc;
4694 }
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004695 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004696 macids_supported = priv->sta_macids_supported;
4697 break;
4698 default:
4699 return -EINVAL;
4700 }
4701
4702 macid = ffs(macids_supported & ~priv->macids_used);
4703 if (!macid--)
4704 return -EBUSY;
4705
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004706 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01004707 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004708 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004709 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004710 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004711 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004712 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4713 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004714
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004715 /* Set the mac address. */
4716 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4717
Yogesh Ashok Poward994a1c2013-01-25 16:20:35 +05304718 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004719 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4720
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004721 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004722 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004723
4724 return 0;
4725}
4726
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304727static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4728{
4729 /* Has ieee80211_restart_hw re-added the removed interfaces? */
4730 if (!priv->macids_used)
4731 return;
4732
4733 priv->macids_used &= ~(1 << vif->macid);
4734 list_del(&vif->list);
4735}
4736
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004737static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004738 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004739{
4740 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004741 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004742
Yogesh Ashok Poward994a1c2013-01-25 16:20:35 +05304743 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004744 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4745
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05304746 mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004747
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304748 mwl8k_remove_vif(priv, mwl8k_vif);
4749}
4750
4751static void mwl8k_hw_restart_work(struct work_struct *work)
4752{
4753 struct mwl8k_priv *priv =
4754 container_of(work, struct mwl8k_priv, fw_reload);
4755 struct ieee80211_hw *hw = priv->hw;
4756 struct mwl8k_device_info *di;
4757 int rc;
4758
4759 /* If some command is waiting for a response, clear it */
4760 if (priv->hostcmd_wait != NULL) {
4761 complete(priv->hostcmd_wait);
4762 priv->hostcmd_wait = NULL;
4763 }
4764
4765 priv->hw_restart_owner = current;
4766 di = priv->device_info;
4767 mwl8k_fw_lock(hw);
4768
4769 if (priv->ap_fw)
4770 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4771 else
4772 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4773
4774 if (rc)
4775 goto fail;
4776
4777 priv->hw_restart_owner = NULL;
4778 priv->hw_restart_in_progress = false;
4779
4780 /*
4781 * This unlock will wake up the queues and
4782 * also opens the command path for other
4783 * commands
4784 */
4785 mwl8k_fw_unlock(hw);
4786
4787 ieee80211_restart_hw(hw);
4788
4789 wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
4790
4791 return;
4792fail:
4793 mwl8k_fw_unlock(hw);
4794
4795 wiphy_err(hw->wiphy, "Firmware restart failed\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004796}
4797
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004798static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004799{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004800 struct ieee80211_conf *conf = &hw->conf;
4801 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004802 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004803
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004804 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004805 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004806 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004807 }
4808
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004809 rc = mwl8k_fw_lock(hw);
4810 if (rc)
4811 return rc;
4812
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004813 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004814 if (rc)
4815 goto out;
4816
Yogesh Ashok Powar0f4316b2013-01-25 16:17:45 +05304817 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
4818 rc = mwl8k_cmd_set_rf_channel(hw, conf);
4819 if (rc)
4820 goto out;
4821 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004822
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004823 if (conf->power_level > 18)
4824 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004825
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004826 if (priv->ap_fw) {
Nishant Sarmukadam03217082011-04-05 14:18:09 +05304827
4828 if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
4829 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4830 if (rc)
4831 goto out;
4832 }
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004833
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08004834
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004835 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004836 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4837 if (rc)
4838 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004839 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4840 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004841
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004842out:
4843 mwl8k_fw_unlock(hw);
4844
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004845 return rc;
4846}
4847
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004848static void
4849mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4850 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004851{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004852 struct mwl8k_priv *priv = hw->priv;
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304853 u32 ap_legacy_rates = 0;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004854 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004855 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004856
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004857 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004858 return;
4859
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004860 /*
4861 * No need to capture a beacon if we're no longer associated.
4862 */
4863 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4864 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004865
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004866 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004867 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004868 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004869 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004870 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004871
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004872 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004873
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004874 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004875 if (ap == NULL) {
4876 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004877 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004878 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004879
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004880 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4881 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4882 } else {
4883 ap_legacy_rates =
4884 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4885 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004886 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004887
4888 rcu_read_unlock();
4889 }
4890
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304891 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4892 !priv->ap_fw) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004893 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004894 if (rc)
4895 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004896
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004897 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004898 if (rc)
4899 goto out;
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304900 } else {
4901 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4902 priv->ap_fw) {
4903 int idx;
4904 int rate;
4905
4906 /* Use AP firmware specific rate command.
4907 */
4908 idx = ffs(vif->bss_conf.basic_rates);
4909 if (idx)
4910 idx--;
4911
4912 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4913 rate = mwl8k_rates_24[idx].hw_value;
4914 else
4915 rate = mwl8k_rates_50[idx].hw_value;
4916
4917 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4918 }
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004919 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004920
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004921 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004922 rc = mwl8k_set_radio_preamble(hw,
4923 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004924 if (rc)
4925 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004926 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004927
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304928 if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004929 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004930 if (rc)
4931 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004932 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004933
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304934 if (vif->bss_conf.assoc && !priv->ap_fw &&
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004935 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4936 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004937 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
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
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004942 if (vif->bss_conf.assoc &&
4943 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004944 /*
4945 * Finalize the join. Tell rx handler to process
4946 * next beacon from our BSSID.
4947 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004948 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004949 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004950 }
4951
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004952out:
4953 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004954}
4955
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004956static void
4957mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4958 struct ieee80211_bss_conf *info, u32 changed)
4959{
4960 int rc;
4961
4962 if (mwl8k_fw_lock(hw))
4963 return;
4964
4965 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4966 rc = mwl8k_set_radio_preamble(hw,
4967 vif->bss_conf.use_short_preamble);
4968 if (rc)
4969 goto out;
4970 }
4971
4972 if (changed & BSS_CHANGED_BASIC_RATES) {
4973 int idx;
4974 int rate;
4975
4976 /*
4977 * Use lowest supported basic rate for multicasts
4978 * and management frames (such as probe responses --
4979 * beacons will always go out at 1 Mb/s).
4980 */
4981 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004982 if (idx)
4983 idx--;
4984
4985 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4986 rate = mwl8k_rates_24[idx].hw_value;
4987 else
4988 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004989
4990 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4991 }
4992
4993 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4994 struct sk_buff *skb;
4995
4996 skb = ieee80211_beacon_get(hw, vif);
4997 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004998 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004999 kfree_skb(skb);
5000 }
5001 }
5002
5003 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01005004 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005005
5006out:
5007 mwl8k_fw_unlock(hw);
5008}
5009
5010static void
5011mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5012 struct ieee80211_bss_conf *info, u32 changed)
5013{
Yogesh Ashok Powar41bf9112013-01-25 16:20:42 +05305014 if (vif->type == NL80211_IFTYPE_STATION)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005015 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
Yogesh Ashok Powar41bf9112013-01-25 16:20:42 +05305016 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005017 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
5018}
5019
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005020static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00005021 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005022{
5023 struct mwl8k_cmd_pkt *cmd;
5024
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005025 /*
5026 * Synthesize and return a command packet that programs the
5027 * hardware multicast address filter. At this point we don't
5028 * know whether FIF_ALLMULTI is being requested, but if it is,
5029 * we'll end up throwing this packet away and creating a new
5030 * one in mwl8k_configure_filter().
5031 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00005032 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005033
5034 return (unsigned long)cmd;
5035}
5036
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005037static int
5038mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
5039 unsigned int changed_flags,
5040 unsigned int *total_flags)
5041{
5042 struct mwl8k_priv *priv = hw->priv;
5043
5044 /*
5045 * Hardware sniffer mode is mutually exclusive with STA
5046 * operation, so refuse to enable sniffer mode if a STA
5047 * interface is active.
5048 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005049 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005050 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07005051 wiphy_info(hw->wiphy,
5052 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005053 return 0;
5054 }
5055
5056 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005057 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005058 return 0;
5059 priv->sniffer_enabled = true;
5060 }
5061
5062 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
5063 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
5064 FIF_OTHER_BSS;
5065
5066 return 1;
5067}
5068
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005069static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
5070{
5071 if (!list_empty(&priv->vif_list))
5072 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
5073
5074 return NULL;
5075}
5076
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005077static void mwl8k_configure_filter(struct ieee80211_hw *hw,
5078 unsigned int changed_flags,
5079 unsigned int *total_flags,
5080 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005081{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005082 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005083 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
5084
5085 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02005086 * AP firmware doesn't allow fine-grained control over
5087 * the receive filter.
5088 */
5089 if (priv->ap_fw) {
5090 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5091 kfree(cmd);
5092 return;
5093 }
5094
5095 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005096 * Enable hardware sniffer mode if FIF_CONTROL or
5097 * FIF_OTHER_BSS is requested.
5098 */
5099 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
5100 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
5101 kfree(cmd);
5102 return;
5103 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005104
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005105 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005106 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005107
Lennert Buytenhek90852f72010-01-02 10:31:42 +01005108 if (mwl8k_fw_lock(hw)) {
5109 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005110 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01005111 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005112
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005113 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005114 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005115 priv->sniffer_enabled = false;
5116 }
5117
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005118 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005119 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
5120 /*
5121 * Disable the BSS filter.
5122 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005123 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005124 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005125 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01005126 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02005127
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005128 /*
5129 * Enable the BSS filter.
5130 *
5131 * If there is an active STA interface, use that
5132 * interface's BSSID, otherwise use a dummy one
5133 * (where the OUI part needs to be nonzero for
5134 * the BSSID to be accepted by POST_SCAN).
5135 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005136 mwl8k_vif = mwl8k_first_vif(priv);
5137 if (mwl8k_vif != NULL)
5138 bssid = mwl8k_vif->vif->bss_conf.bssid;
5139 else
5140 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02005141
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005142 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005143 }
5144 }
5145
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005146 /*
5147 * If FIF_ALLMULTI is being requested, throw away the command
5148 * packet that ->prepare_multicast() built and replace it with
5149 * a command packet that enables reception of all multicast
5150 * packets.
5151 */
5152 if (*total_flags & FIF_ALLMULTI) {
5153 kfree(cmd);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005154 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005155 }
5156
5157 if (cmd != NULL) {
5158 mwl8k_post_cmd(hw, cmd);
5159 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005160 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02005161
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005162 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005163}
5164
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005165static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5166{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01005167 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005168}
5169
Johannes Berg4a6967b2010-02-19 19:18:37 +01005170static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5171 struct ieee80211_vif *vif,
5172 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005173{
5174 struct mwl8k_priv *priv = hw->priv;
5175
Johannes Berg4a6967b2010-02-19 19:18:37 +01005176 if (priv->ap_fw)
5177 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5178 else
5179 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5180}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005181
Johannes Berg4a6967b2010-02-19 19:18:37 +01005182static int mwl8k_sta_add(struct ieee80211_hw *hw,
5183 struct ieee80211_vif *vif,
5184 struct ieee80211_sta *sta)
5185{
5186 struct mwl8k_priv *priv = hw->priv;
5187 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005188 int i;
5189 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5190 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005191
Johannes Berg4a6967b2010-02-19 19:18:37 +01005192 if (!priv->ap_fw) {
5193 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5194 if (ret >= 0) {
5195 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07005196 if (sta->ht_cap.ht_supported)
5197 MWL8K_STA(sta)->is_ampdu_allowed = true;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005198 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005199 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01005200
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08005201 } else {
5202 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005203 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005204
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08005205 for (i = 0; i < NUM_WEP_KEYS; i++) {
5206 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5207 if (mwl8k_vif->wep_key_conf[i].enabled)
5208 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5209 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005210 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01005211}
5212
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005213static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5214 struct ieee80211_vif *vif, u16 queue,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005215 const struct ieee80211_tx_queue_params *params)
5216{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005217 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005218 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005219
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005220 rc = mwl8k_fw_lock(hw);
5221 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005222 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005223 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5224
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005225 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005226 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005227
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005228 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005229 int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005230 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005231 params->cw_min,
5232 params->cw_max,
5233 params->aifs,
5234 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005235 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005236
5237 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005238 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005239
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005240 return rc;
5241}
5242
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005243static int mwl8k_get_stats(struct ieee80211_hw *hw,
5244 struct ieee80211_low_level_stats *stats)
5245{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005246 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005247}
5248
John W. Linville0d462bb2010-07-28 14:04:24 -04005249static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5250 struct survey_info *survey)
5251{
5252 struct mwl8k_priv *priv = hw->priv;
5253 struct ieee80211_conf *conf = &hw->conf;
5254
5255 if (idx != 0)
5256 return -ENOENT;
5257
5258 survey->channel = conf->channel;
5259 survey->filled = SURVEY_INFO_NOISE_DBM;
5260 survey->noise = priv->noise;
5261
5262 return 0;
5263}
5264
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005265#define MAX_AMPDU_ATTEMPTS 5
5266
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005267static int
5268mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5269 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01005270 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5271 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005272{
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005273
5274 int i, rc = 0;
5275 struct mwl8k_priv *priv = hw->priv;
5276 struct mwl8k_ampdu_stream *stream;
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305277 u8 *addr = sta->addr, idx;
Yogesh Ashok Powarfd712f52012-11-06 19:22:35 +05305278 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005279
5280 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
5281 return -ENOTSUPP;
5282
5283 spin_lock(&priv->stream_lock);
5284 stream = mwl8k_lookup_stream(hw, addr, tid);
5285
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005286 switch (action) {
5287 case IEEE80211_AMPDU_RX_START:
5288 case IEEE80211_AMPDU_RX_STOP:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005289 break;
5290 case IEEE80211_AMPDU_TX_START:
5291 /* By the time we get here the hw queues may contain outgoing
5292 * packets for this RA/TID that are not part of this BA
5293 * session. The hw will assign sequence numbers to these
5294 * packets as they go out. So if we query the hw for its next
5295 * sequence number and use that for the SSN here, it may end up
5296 * being wrong, which will lead to sequence number mismatch at
5297 * the recipient. To avoid this, we reset the sequence number
5298 * to O for the first MPDU in this BA stream.
5299 */
5300 *ssn = 0;
5301 if (stream == NULL) {
5302 /* This means that somebody outside this driver called
5303 * ieee80211_start_tx_ba_session. This is unexpected
5304 * because we do our own rate control. Just warn and
5305 * move on.
5306 */
5307 wiphy_warn(hw->wiphy, "Unexpected call to %s. "
5308 "Proceeding anyway.\n", __func__);
5309 stream = mwl8k_add_stream(hw, sta, tid);
5310 }
5311 if (stream == NULL) {
5312 wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5313 rc = -EBUSY;
5314 break;
5315 }
5316 stream->state = AMPDU_STREAM_IN_PROGRESS;
5317
5318 /* Release the lock before we do the time consuming stuff */
5319 spin_unlock(&priv->stream_lock);
5320 for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
Yogesh Ashok Powarfd712f52012-11-06 19:22:35 +05305321
5322 /* Check if link is still valid */
5323 if (!sta_info->is_ampdu_allowed) {
5324 spin_lock(&priv->stream_lock);
5325 mwl8k_remove_stream(hw, stream);
5326 spin_unlock(&priv->stream_lock);
5327 return -EBUSY;
5328 }
5329
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05305330 rc = mwl8k_check_ba(hw, stream, vif);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005331
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305332 /* If HW restart is in progress mwl8k_post_cmd will
5333 * return -EBUSY. Avoid retrying mwl8k_check_ba in
5334 * such cases
5335 */
5336 if (!rc || rc == -EBUSY)
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005337 break;
5338 /*
5339 * HW queues take time to be flushed, give them
5340 * sufficient time
5341 */
5342
5343 msleep(1000);
5344 }
5345 spin_lock(&priv->stream_lock);
5346 if (rc) {
5347 wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5348 " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5349 mwl8k_remove_stream(hw, stream);
5350 rc = -EBUSY;
5351 break;
5352 }
5353 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
5354 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02005355 case IEEE80211_AMPDU_TX_STOP_CONT:
5356 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5357 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305358 if (stream) {
5359 if (stream->state == AMPDU_STREAM_ACTIVE) {
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305360 idx = stream->idx;
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305361 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305362 mwl8k_destroy_ba(hw, idx);
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305363 spin_lock(&priv->stream_lock);
5364 }
5365 mwl8k_remove_stream(hw, stream);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005366 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005367 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5368 break;
5369 case IEEE80211_AMPDU_TX_OPERATIONAL:
5370 BUG_ON(stream == NULL);
5371 BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5372 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05305373 rc = mwl8k_create_ba(hw, stream, buf_size, vif);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005374 spin_lock(&priv->stream_lock);
5375 if (!rc)
5376 stream->state = AMPDU_STREAM_ACTIVE;
5377 else {
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305378 idx = stream->idx;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005379 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305380 mwl8k_destroy_ba(hw, idx);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005381 spin_lock(&priv->stream_lock);
5382 wiphy_debug(hw->wiphy,
5383 "Failed adding stream for sta %pM tid %d\n",
5384 addr, tid);
5385 mwl8k_remove_stream(hw, stream);
5386 }
5387 break;
5388
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005389 default:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005390 rc = -ENOTSUPP;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005391 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005392
5393 spin_unlock(&priv->stream_lock);
5394 return rc;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005395}
5396
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005397static const struct ieee80211_ops mwl8k_ops = {
5398 .tx = mwl8k_tx,
5399 .start = mwl8k_start,
5400 .stop = mwl8k_stop,
5401 .add_interface = mwl8k_add_interface,
5402 .remove_interface = mwl8k_remove_interface,
5403 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005404 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02005405 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005406 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005407 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005408 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01005409 .sta_add = mwl8k_sta_add,
5410 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005411 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005412 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04005413 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005414 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005415};
5416
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005417static void mwl8k_finalize_join_worker(struct work_struct *work)
5418{
5419 struct mwl8k_priv *priv =
5420 container_of(work, struct mwl8k_priv, finalize_join_worker);
5421 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01005422 struct ieee80211_mgmt *mgmt = (void *)skb->data;
5423 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5424 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5425 mgmt->u.beacon.variable, len);
5426 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005427
Johannes Berg56007a02010-01-26 14:19:52 +01005428 if (tim && tim[1] >= 2)
5429 dtim_period = tim[3];
5430
5431 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005432
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005433 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005434 priv->beacon_skb = NULL;
5435}
5436
John W. Linvillebcb628d2009-11-06 16:40:16 -05005437enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005438 MWL8363 = 0,
5439 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05005440 MWL8366,
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305441 MWL8764,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02005442};
5443
Yogesh Ashok Powarc2f2e202013-01-03 13:24:44 +05305444#define MWL8K_8366_AP_FW_API 3
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005445#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5446#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5447
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305448#define MWL8K_8764_AP_FW_API 1
5449#define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
5450#define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
5451
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005452static struct mwl8k_device_info mwl8k_info_tbl[] = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005453 [MWL8363] = {
5454 .part_name = "88w8363",
5455 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005456 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005457 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01005458 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05005459 .part_name = "88w8687",
5460 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005461 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05005462 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01005463 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05005464 .part_name = "88w8366",
5465 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005466 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005467 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5468 .fw_api_ap = MWL8K_8366_AP_FW_API,
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305469 .ap_rxd_ops = &rxd_ap_ops,
5470 },
5471 [MWL8764] = {
5472 .part_name = "88w8764",
5473 .fw_image_ap = MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
5474 .fw_api_ap = MWL8K_8764_AP_FW_API,
5475 .ap_rxd_ops = &rxd_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05005476 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005477};
5478
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01005479MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5480MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5481MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5482MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5483MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5484MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005485MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01005486
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005487static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01005488 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005489 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5490 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05005491 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5492 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5493 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Jonas Gorskifc5bc162013-02-05 16:59:17 +01005494 { PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
5495 { PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01005496 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305497 { PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05005498 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005499};
5500MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5501
Brian Cavagnolo99020472010-11-12 17:23:52 -08005502static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5503{
5504 int rc;
5505 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5506 "Trying alternative firmware %s\n", pci_name(priv->pdev),
5507 priv->fw_pref, priv->fw_alt);
5508 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5509 if (rc) {
5510 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5511 pci_name(priv->pdev), priv->fw_alt);
5512 return rc;
5513 }
5514 return 0;
5515}
5516
5517static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5518static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5519{
5520 struct mwl8k_priv *priv = context;
5521 struct mwl8k_device_info *di = priv->device_info;
5522 int rc;
5523
5524 switch (priv->fw_state) {
5525 case FW_STATE_INIT:
5526 if (!fw) {
5527 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5528 pci_name(priv->pdev), di->helper_image);
5529 goto fail;
5530 }
5531 priv->fw_helper = fw;
5532 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5533 true);
5534 if (rc && priv->fw_alt) {
5535 rc = mwl8k_request_alt_fw(priv);
5536 if (rc)
5537 goto fail;
5538 priv->fw_state = FW_STATE_LOADING_ALT;
5539 } else if (rc)
5540 goto fail;
5541 else
5542 priv->fw_state = FW_STATE_LOADING_PREF;
5543 break;
5544
5545 case FW_STATE_LOADING_PREF:
5546 if (!fw) {
5547 if (priv->fw_alt) {
5548 rc = mwl8k_request_alt_fw(priv);
5549 if (rc)
5550 goto fail;
5551 priv->fw_state = FW_STATE_LOADING_ALT;
5552 } else
5553 goto fail;
5554 } else {
5555 priv->fw_ucode = fw;
5556 rc = mwl8k_firmware_load_success(priv);
5557 if (rc)
5558 goto fail;
5559 else
5560 complete(&priv->firmware_loading_complete);
5561 }
5562 break;
5563
5564 case FW_STATE_LOADING_ALT:
5565 if (!fw) {
5566 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5567 pci_name(priv->pdev), di->helper_image);
5568 goto fail;
5569 }
5570 priv->fw_ucode = fw;
5571 rc = mwl8k_firmware_load_success(priv);
5572 if (rc)
5573 goto fail;
5574 else
5575 complete(&priv->firmware_loading_complete);
5576 break;
5577
5578 default:
5579 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5580 MWL8K_NAME, priv->fw_state);
5581 BUG_ON(1);
5582 }
5583
5584 return;
5585
5586fail:
5587 priv->fw_state = FW_STATE_ERROR;
5588 complete(&priv->firmware_loading_complete);
5589 device_release_driver(&priv->pdev->dev);
5590 mwl8k_release_firmware(priv);
5591}
5592
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305593#define MAX_RESTART_ATTEMPTS 1
Brian Cavagnolo99020472010-11-12 17:23:52 -08005594static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5595 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005596{
5597 struct mwl8k_priv *priv = hw->priv;
5598 int rc;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305599 int count = MAX_RESTART_ATTEMPTS;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005600
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305601retry:
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005602 /* Reset firmware and hardware */
5603 mwl8k_hw_reset(priv);
5604
5605 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005606 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005607 if (rc) {
5608 wiphy_err(hw->wiphy, "Firmware files not found\n");
5609 return rc;
5610 }
5611
Brian Cavagnolo99020472010-11-12 17:23:52 -08005612 if (nowait)
5613 return rc;
5614
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005615 /* Load firmware into hardware */
5616 rc = mwl8k_load_firmware(hw);
5617 if (rc)
5618 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5619
5620 /* Reclaim memory once firmware is successfully loaded */
5621 mwl8k_release_firmware(priv);
5622
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305623 if (rc && count) {
5624 /* FW did not start successfully;
5625 * lets try one more time
5626 */
5627 count--;
5628 wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5629 msleep(20);
5630 goto retry;
5631 }
5632
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005633 return rc;
5634}
5635
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005636static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5637{
5638 struct mwl8k_priv *priv = hw->priv;
5639 int rc = 0;
5640 int i;
5641
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005642 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005643 rc = mwl8k_txq_init(hw, i);
5644 if (rc)
5645 break;
5646 if (priv->ap_fw)
5647 iowrite32(priv->txq[i].txd_dma,
5648 priv->sram + priv->txq_offset[i]);
5649 }
5650 return rc;
5651}
5652
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005653/* initialize hw after successfully loading a firmware image */
5654static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5655{
5656 struct mwl8k_priv *priv = hw->priv;
5657 int rc = 0;
5658 int i;
5659
5660 if (priv->ap_fw) {
5661 priv->rxd_ops = priv->device_info->ap_rxd_ops;
5662 if (priv->rxd_ops == NULL) {
5663 wiphy_err(hw->wiphy,
5664 "Driver does not have AP firmware image support for this hardware\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04005665 rc = -ENOENT;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005666 goto err_stop_firmware;
5667 }
5668 } else {
5669 priv->rxd_ops = &rxd_sta_ops;
5670 }
5671
5672 priv->sniffer_enabled = false;
5673 priv->wmm_enabled = false;
5674 priv->pending_tx_pkts = 0;
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05305675 atomic_set(&priv->watchdog_event_pending, 0);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005676
5677 rc = mwl8k_rxq_init(hw, 0);
5678 if (rc)
5679 goto err_stop_firmware;
5680 rxq_refill(hw, 0, INT_MAX);
5681
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005682 /* For the sta firmware, we need to know the dma addresses of tx queues
5683 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
5684 * prior to issuing this command. But for the AP case, we learn the
5685 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5686 * case we must initialize the tx queues after.
5687 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07005688 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005689 if (!priv->ap_fw) {
5690 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005691 if (rc)
5692 goto err_free_queues;
5693 }
5694
5695 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5696 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005697 iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5698 MWL8K_A2H_INT_BA_WATCHDOG,
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005699 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Nishant Sarmukadam12488e02011-04-08 14:38:27 +05305700 iowrite32(MWL8K_A2H_INT_OPC_DONE,
5701 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005702
5703 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5704 IRQF_SHARED, MWL8K_NAME, hw);
5705 if (rc) {
5706 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5707 goto err_free_queues;
5708 }
5709
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305710 /*
5711 * When hw restart is requested,
5712 * mac80211 will take care of clearing
5713 * the ampdu streams, so do not clear
5714 * the ampdu state here
5715 */
5716 if (!priv->hw_restart_in_progress)
5717 memset(priv->ampdu, 0, sizeof(priv->ampdu));
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005718
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005719 /*
5720 * Temporarily enable interrupts. Initial firmware host
5721 * commands use interrupts and avoid polling. Disable
5722 * interrupts when done.
5723 */
5724 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5725
5726 /* Get config data, mac addrs etc */
5727 if (priv->ap_fw) {
5728 rc = mwl8k_cmd_get_hw_spec_ap(hw);
5729 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005730 rc = mwl8k_init_txqs(hw);
5731 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005732 rc = mwl8k_cmd_set_hw_spec(hw);
5733 } else {
5734 rc = mwl8k_cmd_get_hw_spec_sta(hw);
5735 }
5736 if (rc) {
5737 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5738 goto err_free_irq;
5739 }
5740
5741 /* Turn radio off */
5742 rc = mwl8k_cmd_radio_disable(hw);
5743 if (rc) {
5744 wiphy_err(hw->wiphy, "Cannot disable\n");
5745 goto err_free_irq;
5746 }
5747
5748 /* Clear MAC address */
5749 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5750 if (rc) {
5751 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5752 goto err_free_irq;
5753 }
5754
Yogesh Ashok Powara246ac32013-01-25 16:17:56 +05305755 /* Configure Antennas */
5756 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
5757 if (rc)
5758 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
5759 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
5760 if (rc)
5761 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
5762
5763
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005764 /* Disable interrupts */
5765 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5766 free_irq(priv->pdev->irq, hw);
5767
5768 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5769 priv->device_info->part_name,
5770 priv->hw_rev, hw->wiphy->perm_addr,
5771 priv->ap_fw ? "AP" : "STA",
5772 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5773 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5774
5775 return 0;
5776
5777err_free_irq:
5778 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5779 free_irq(priv->pdev->irq, hw);
5780
5781err_free_queues:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005782 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005783 mwl8k_txq_deinit(hw, i);
5784 mwl8k_rxq_deinit(hw, 0);
5785
5786err_stop_firmware:
5787 mwl8k_hw_reset(priv);
5788
5789 return rc;
5790}
5791
5792/*
5793 * invoke mwl8k_reload_firmware to change the firmware image after the device
5794 * has already been registered
5795 */
5796static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5797{
5798 int i, rc = 0;
5799 struct mwl8k_priv *priv = hw->priv;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305800 struct mwl8k_vif *vif, *tmp_vif;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005801
5802 mwl8k_stop(hw);
5803 mwl8k_rxq_deinit(hw, 0);
5804
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305805 /*
5806 * All the existing interfaces are re-added by the ieee80211_reconfig;
5807 * which means driver should remove existing interfaces before calling
5808 * ieee80211_restart_hw
5809 */
5810 if (priv->hw_restart_in_progress)
5811 list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
5812 mwl8k_remove_vif(priv, vif);
5813
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005814 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005815 mwl8k_txq_deinit(hw, i);
5816
Brian Cavagnolo99020472010-11-12 17:23:52 -08005817 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005818 if (rc)
5819 goto fail;
5820
5821 rc = mwl8k_probe_hw(hw);
5822 if (rc)
5823 goto fail;
5824
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305825 if (priv->hw_restart_in_progress)
5826 return rc;
5827
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005828 rc = mwl8k_start(hw);
5829 if (rc)
5830 goto fail;
5831
5832 rc = mwl8k_config(hw, ~0);
5833 if (rc)
5834 goto fail;
5835
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005836 for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005837 rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005838 if (rc)
5839 goto fail;
5840 }
5841
5842 return rc;
5843
5844fail:
5845 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5846 return rc;
5847}
5848
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305849static const struct ieee80211_iface_limit ap_if_limits[] = {
5850 { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
Yogesh Ashok Powar2acdaa72013-01-25 16:19:55 +05305851 { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305852};
5853
5854static const struct ieee80211_iface_combination ap_if_comb = {
5855 .limits = ap_if_limits,
5856 .n_limits = ARRAY_SIZE(ap_if_limits),
5857 .max_interfaces = 8,
5858 .num_different_channels = 1,
5859};
5860
5861
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005862static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5863{
5864 struct ieee80211_hw *hw = priv->hw;
5865 int i, rc;
5866
Brian Cavagnolo99020472010-11-12 17:23:52 -08005867 rc = mwl8k_load_firmware(hw);
5868 mwl8k_release_firmware(priv);
5869 if (rc) {
5870 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5871 return rc;
5872 }
5873
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005874 /*
5875 * Extra headroom is the size of the required DMA header
5876 * minus the size of the smallest 802.11 frame (CTS frame).
5877 */
5878 hw->extra_tx_headroom =
5879 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5880
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +05305881 hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
5882
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005883 hw->channel_change_time = 10;
5884
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005885 hw->queues = MWL8K_TX_WMM_QUEUES;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005886
5887 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08005888 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Yogesh Ashok Powar2a36a0e2011-08-29 17:12:44 +05305889
5890 /*
5891 * Ask mac80211 to not to trigger PS mode
5892 * based on PM bit of incoming frames.
5893 */
5894 if (priv->ap_fw)
5895 hw->flags |= IEEE80211_HW_AP_LINK_PS;
5896
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005897 hw->vif_data_size = sizeof(struct mwl8k_vif);
5898 hw->sta_data_size = sizeof(struct mwl8k_sta);
5899
5900 priv->macids_used = 0;
5901 INIT_LIST_HEAD(&priv->vif_list);
5902
5903 /* Set default radio state and preamble */
Rusty Russell3db1cd52011-12-19 13:56:45 +00005904 priv->radio_on = false;
5905 priv->radio_short_preamble = false;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005906
5907 /* Finalize join worker */
5908 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005909 /* Handle watchdog ba events */
5910 INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305911 /* To reload the firmware if it crashes */
5912 INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005913
5914 /* TX reclaim and RX tasklets. */
5915 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5916 tasklet_disable(&priv->poll_tx_task);
5917 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5918 tasklet_disable(&priv->poll_rx_task);
5919
5920 /* Power management cookie */
5921 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5922 if (priv->cookie == NULL)
5923 return -ENOMEM;
5924
5925 mutex_init(&priv->fw_mutex);
5926 priv->fw_mutex_owner = NULL;
5927 priv->fw_mutex_depth = 0;
5928 priv->hostcmd_wait = NULL;
5929
5930 spin_lock_init(&priv->tx_lock);
5931
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005932 spin_lock_init(&priv->stream_lock);
5933
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005934 priv->tx_wait = NULL;
5935
5936 rc = mwl8k_probe_hw(hw);
5937 if (rc)
5938 goto err_free_cookie;
5939
5940 hw->wiphy->interface_modes = 0;
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305941
5942 if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005943 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
Yogesh Ashok Powar2acdaa72013-01-25 16:19:55 +05305944 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305945 hw->wiphy->iface_combinations = &ap_if_comb;
5946 hw->wiphy->n_iface_combinations = 1;
5947 }
5948
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005949 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5950 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5951
5952 rc = ieee80211_register_hw(hw);
5953 if (rc) {
5954 wiphy_err(hw->wiphy, "Cannot register device\n");
5955 goto err_unprobe_hw;
5956 }
5957
5958 return 0;
5959
5960err_unprobe_hw:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005961 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005962 mwl8k_txq_deinit(hw, i);
5963 mwl8k_rxq_deinit(hw, 0);
5964
5965err_free_cookie:
5966 if (priv->cookie != NULL)
5967 pci_free_consistent(priv->pdev, 4,
5968 priv->cookie, priv->cookie_dma);
5969
5970 return rc;
5971}
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005972static int mwl8k_probe(struct pci_dev *pdev,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005973 const struct pci_device_id *id)
5974{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005975 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005976 struct ieee80211_hw *hw;
5977 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005978 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005979 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02005980
5981 if (!printed_version) {
5982 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5983 printed_version = 1;
5984 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005985
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005986
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005987 rc = pci_enable_device(pdev);
5988 if (rc) {
5989 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
5990 MWL8K_NAME);
5991 return rc;
5992 }
5993
5994 rc = pci_request_regions(pdev, MWL8K_NAME);
5995 if (rc) {
5996 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
5997 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005998 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005999 }
6000
6001 pci_set_master(pdev);
6002
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006003
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006004 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
6005 if (hw == NULL) {
6006 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
6007 rc = -ENOMEM;
6008 goto err_free_reg;
6009 }
6010
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006011 SET_IEEE80211_DEV(hw, &pdev->dev);
6012 pci_set_drvdata(pdev, hw);
6013
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006014 priv = hw->priv;
6015 priv->hw = hw;
6016 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05006017 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006018
Nishant Sarmukadam98929822013-03-01 17:13:01 +05306019 if (id->driver_data == MWL8764)
6020 priv->is_8764 = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006021
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006022 priv->sram = pci_iomap(pdev, 0, 0x10000);
6023 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07006024 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04006025 rc = -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006026 goto err_iounmap;
6027 }
6028
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006029 /*
6030 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
6031 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
6032 */
6033 priv->regs = pci_iomap(pdev, 1, 0x10000);
6034 if (priv->regs == NULL) {
6035 priv->regs = pci_iomap(pdev, 2, 0x10000);
6036 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07006037 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04006038 rc = -EIO;
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006039 goto err_iounmap;
6040 }
6041 }
6042
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006043 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08006044 * Choose the initial fw image depending on user input. If a second
6045 * image is available, make it the alternative image that will be
6046 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006047 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08006048 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006049 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08006050 if (ap_mode_default && di->fw_image_ap) {
6051 priv->fw_pref = di->fw_image_ap;
6052 priv->fw_alt = di->fw_image_sta;
6053 } else if (!ap_mode_default && di->fw_image_sta) {
6054 priv->fw_pref = di->fw_image_sta;
6055 priv->fw_alt = di->fw_image_ap;
6056 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006057 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08006058 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006059 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
6060 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08006061 priv->fw_pref = di->fw_image_ap;
6062 }
6063 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006064 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08006065 goto err_stop_firmware;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05306066
6067 priv->hw_restart_in_progress = false;
6068
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05306069 priv->running_bsses = 0;
6070
Brian Cavagnolo99020472010-11-12 17:23:52 -08006071 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006072
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006073err_stop_firmware:
6074 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006075
6076err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006077 if (priv->regs != NULL)
6078 pci_iounmap(pdev, priv->regs);
6079
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006080 if (priv->sram != NULL)
6081 pci_iounmap(pdev, priv->sram);
6082
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006083 pci_set_drvdata(pdev, NULL);
6084 ieee80211_free_hw(hw);
6085
6086err_free_reg:
6087 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01006088
6089err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006090 pci_disable_device(pdev);
6091
6092 return rc;
6093}
6094
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05006095static void mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006096{
6097 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
6098 struct mwl8k_priv *priv;
6099 int i;
6100
6101 if (hw == NULL)
6102 return;
6103 priv = hw->priv;
6104
Brian Cavagnolo99020472010-11-12 17:23:52 -08006105 wait_for_completion(&priv->firmware_loading_complete);
6106
6107 if (priv->fw_state == FW_STATE_ERROR) {
6108 mwl8k_hw_reset(priv);
6109 goto unmap;
6110 }
6111
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006112 ieee80211_stop_queues(hw);
6113
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02006114 ieee80211_unregister_hw(hw);
6115
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01006116 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01006117 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01006118 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006119
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006120 /* Stop hardware */
6121 mwl8k_hw_reset(priv);
6122
6123 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07006124 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01006125 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006126
Brian Cavagnoloe6007072011-03-17 11:58:44 -07006127 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006128 mwl8k_txq_deinit(hw, i);
6129
6130 mwl8k_rxq_deinit(hw, 0);
6131
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02006132 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006133
Brian Cavagnolo99020472010-11-12 17:23:52 -08006134unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006135 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006136 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006137 pci_set_drvdata(pdev, NULL);
6138 ieee80211_free_hw(hw);
6139 pci_release_regions(pdev);
6140 pci_disable_device(pdev);
6141}
6142
6143static struct pci_driver mwl8k_driver = {
6144 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02006145 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006146 .probe = mwl8k_probe,
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05006147 .remove = mwl8k_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006148};
6149
Axel Lin5b0a3b72012-04-14 10:38:36 +08006150module_pci_driver(mwl8k_driver);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02006151
6152MODULE_DESCRIPTION(MWL8K_DESC);
6153MODULE_VERSION(MWL8K_VERSION);
6154MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
6155MODULE_LICENSE("GPL");