blob: 4987c3f942ce7dff2bb599feab7bb1aa26d40f50 [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
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000012#include <linux/interrupt.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010013#include <linux/module.h>
14#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020015#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010016#include <linux/spinlock.h>
17#include <linux/list.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
20#include <linux/completion.h>
21#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010023#include <net/mac80211.h>
24#include <linux/moduleparam.h>
25#include <linux/firmware.h>
26#include <linux/workqueue.h>
27
28#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
29#define MWL8K_NAME KBUILD_MODNAME
Yogesh Ashok Powar00e8e692011-12-20 11:39:29 +053030#define MWL8K_VERSION "0.13"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031
Brian Cavagnolo0863ade2010-11-12 17:23:50 -080032/* Module parameters */
Rusty Russelleb939922011-12-19 14:08:01 +000033static bool ap_mode_default;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -080034module_param(ap_mode_default, bool, 0);
35MODULE_PARM_DESC(ap_mode_default,
36 "Set to 1 to make ap mode the default instead of sta mode");
37
Lennert Buytenheka66098d2009-03-10 10:13:33 +010038/* Register definitions */
39#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020040#define MWL8K_MODE_STA 0x0000005a
41#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010042#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020043#define MWL8K_FWSTA_READY 0xf0f1f2f4
44#define MWL8K_FWAP_READY 0xf1f2f4a5
45#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010046#define MWL8K_HIU_SCRATCH 0x00000c40
47
48/* Host->device communications */
49#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
50#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
51#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
52#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
53#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020054#define MWL8K_H2A_INT_DUMMY (1 << 20)
55#define MWL8K_H2A_INT_RESET (1 << 15)
56#define MWL8K_H2A_INT_DOORBELL (1 << 1)
57#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010058
59/* Device->host communications */
60#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
61#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
62#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
63#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
64#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020065#define MWL8K_A2H_INT_DUMMY (1 << 20)
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -070066#define MWL8K_A2H_INT_BA_WATCHDOG (1 << 14)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020067#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
68#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
69#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
70#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
71#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
72#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
73#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
74#define MWL8K_A2H_INT_RX_READY (1 << 1)
75#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010076
Pradeep Nemavat566875d2011-04-21 16:34:57 +053077/* HW micro second timer register
78 * located at offset 0xA600. This
79 * will be used to timestamp tx
80 * packets.
81 */
82
83#define MWL8K_HW_TIMER_REGISTER 0x0000a600
84
Lennert Buytenheka66098d2009-03-10 10:13:33 +010085#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
86 MWL8K_A2H_INT_CHNL_SWITCHED | \
87 MWL8K_A2H_INT_QUEUE_EMPTY | \
88 MWL8K_A2H_INT_RADAR_DETECT | \
89 MWL8K_A2H_INT_RADIO_ON | \
90 MWL8K_A2H_INT_RADIO_OFF | \
91 MWL8K_A2H_INT_MAC_EVENT | \
92 MWL8K_A2H_INT_OPC_DONE | \
93 MWL8K_A2H_INT_RX_READY | \
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -070094 MWL8K_A2H_INT_TX_DONE | \
95 MWL8K_A2H_INT_BA_WATCHDOG)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010096
Lennert Buytenheka66098d2009-03-10 10:13:33 +010097#define MWL8K_RX_QUEUES 1
Brian Cavagnoloe6007072011-03-17 11:58:44 -070098#define MWL8K_TX_WMM_QUEUES 4
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -070099#define MWL8K_MAX_AMPDU_QUEUES 8
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700100#define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
101#define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100102
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +0530103/* txpriorities are mapped with hw queues.
104 * Each hw queue has a txpriority.
105 */
106#define TOTAL_HW_TX_QUEUES 8
107
108/* Each HW queue can have one AMPDU stream.
109 * But, because one of the hw queue is reserved,
110 * maximum AMPDU queues that can be created are
111 * one short of total tx queues.
112 */
113#define MWL8K_NUM_AMPDU_STREAMS (TOTAL_HW_TX_QUEUES - 1)
114
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200115struct rxd_ops {
116 int rxd_size;
117 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
118 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100119 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400120 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200121};
122
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200123struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200124 char *part_name;
125 char *helper_image;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800126 char *fw_image_sta;
127 char *fw_image_ap;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100128 struct rxd_ops *ap_rxd_ops;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -0800129 u32 fw_api_ap;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200130};
131
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100132struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200133 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100134
135 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200136 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100137
138 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200139 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200141 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200142 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200143 struct {
144 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900145 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200146 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100147};
148
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100149struct mwl8k_tx_queue {
150 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200151 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100152
153 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200154 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100155
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200156 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200157 struct mwl8k_tx_desc *txd;
158 dma_addr_t txd_dma;
159 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100160};
161
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700162enum {
163 AMPDU_NO_STREAM,
164 AMPDU_STREAM_NEW,
165 AMPDU_STREAM_IN_PROGRESS,
166 AMPDU_STREAM_ACTIVE,
167};
168
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700169struct mwl8k_ampdu_stream {
170 struct ieee80211_sta *sta;
171 u8 tid;
172 u8 state;
173 u8 idx;
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700174};
175
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100176struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100178 struct pci_dev *pdev;
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +0530179 int irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100180
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200181 struct mwl8k_device_info *device_info;
182
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100183 void __iomem *sram;
184 void __iomem *regs;
185
186 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800187 const struct firmware *fw_helper;
188 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100189
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100190 /* hardware/firmware parameters */
191 bool ap_fw;
192 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100193 struct ieee80211_supported_band band_24;
194 struct ieee80211_channel channels_24[14];
Jonas Gorski3f524552013-04-14 14:11:58 +0200195 struct ieee80211_rate rates_24[13];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100196 struct ieee80211_supported_band band_50;
197 struct ieee80211_channel channels_50[4];
Jonas Gorski3f524552013-04-14 14:11:58 +0200198 struct ieee80211_rate rates_50[8];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100199 u32 ap_macids_supported;
200 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100201
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700202 /* Ampdu stream information */
203 u8 num_ampdu_queues;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700204 spinlock_t stream_lock;
205 struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700206 struct work_struct watchdog_ba_handle;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700207
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200208 /* firmware access */
209 struct mutex fw_mutex;
210 struct task_struct *fw_mutex_owner;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +0530211 struct task_struct *hw_restart_owner;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200212 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200213 struct completion *hostcmd_wait;
214
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +0530215 atomic_t watchdog_event_pending;
216
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100217 /* lock held over TX and TX reap */
218 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100219
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200220 /* TX quiesce completion, protected by fw_mutex and tx_lock */
221 struct completion *tx_wait;
222
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100223 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100224 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100225 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100226
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100227 /* power management status cookie from firmware */
228 u32 *cookie;
229 dma_addr_t cookie_dma;
230
231 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100232 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200233 u32 fw_rev;
Jonas Gorskic3f251a2013-03-11 17:44:21 +0100234 u32 caps;
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};
369
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100370static const struct ieee80211_channel mwl8k_channels_50[] = {
Jonas Gorskid786f672013-02-08 16:07:25 +0100371 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
372 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
373 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
374 { .band = IEEE80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100375};
376
377static const struct ieee80211_rate mwl8k_rates_50[] = {
378 { .bitrate = 60, .hw_value = 12, },
379 { .bitrate = 90, .hw_value = 18, },
380 { .bitrate = 120, .hw_value = 24, },
381 { .bitrate = 180, .hw_value = 36, },
382 { .bitrate = 240, .hw_value = 48, },
383 { .bitrate = 360, .hw_value = 72, },
384 { .bitrate = 480, .hw_value = 96, },
385 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100386};
387
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100388/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100389#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800390#define MWL8K_CMD_SET 0x0001
391#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100392
393/* Firmware command codes */
394#define MWL8K_CMD_CODE_DNLD 0x0001
395#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200396#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100397#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
398#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200399#define MWL8K_CMD_RADIO_CONTROL 0x001c
400#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800401#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200402#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100403#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100404#define MWL8K_CMD_SET_PRE_SCAN 0x0107
405#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200406#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100407#define MWL8K_CMD_SET_AID 0x010d
408#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200409#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100410#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200411#define MWL8K_CMD_SET_SLOT 0x0114
412#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
413#define MWL8K_CMD_SET_WMM_MODE 0x0123
414#define MWL8K_CMD_MIMO_CONFIG 0x0125
415#define MWL8K_CMD_USE_FIXED_RATE 0x0126
416#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100417#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200418#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700419#define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +0530420#define MWL8K_CMD_DEL_MAC_ADDR 0x0206 /* per-vif */
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100421#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
422#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800423#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200424#define MWL8K_CMD_UPDATE_STADB 0x1123
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700425#define MWL8K_CMD_BASTREAM 0x1125
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100426
John W. Linvilleb6037422010-07-20 13:55:00 -0400427static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100428{
John W. Linvilleb6037422010-07-20 13:55:00 -0400429 u16 command = le16_to_cpu(cmd);
430
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100431#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
432 snprintf(buf, bufsize, "%s", #x);\
433 return buf;\
434 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400435 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100436 MWL8K_CMDNAME(CODE_DNLD);
437 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200438 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100439 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
440 MWL8K_CMDNAME(GET_STAT);
441 MWL8K_CMDNAME(RADIO_CONTROL);
442 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800443 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200444 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100445 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100446 MWL8K_CMDNAME(SET_PRE_SCAN);
447 MWL8K_CMDNAME(SET_POST_SCAN);
448 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100449 MWL8K_CMDNAME(SET_AID);
450 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200451 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100452 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200453 MWL8K_CMDNAME(SET_SLOT);
454 MWL8K_CMDNAME(SET_EDCA_PARAMS);
455 MWL8K_CMDNAME(SET_WMM_MODE);
456 MWL8K_CMDNAME(MIMO_CONFIG);
457 MWL8K_CMDNAME(USE_FIXED_RATE);
458 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200459 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200460 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100461 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100462 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800463 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200464 MWL8K_CMDNAME(UPDATE_STADB);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700465 MWL8K_CMDNAME(BASTREAM);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700466 MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100467 default:
468 snprintf(buf, bufsize, "0x%x", cmd);
469 }
470#undef MWL8K_CMDNAME
471
472 return buf;
473}
474
475/* Hardware and firmware reset */
476static void mwl8k_hw_reset(struct mwl8k_priv *priv)
477{
478 iowrite32(MWL8K_H2A_INT_RESET,
479 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
480 iowrite32(MWL8K_H2A_INT_RESET,
481 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
482 msleep(20);
483}
484
485/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800486static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100487{
488 if (*fw == NULL)
489 return;
490 release_firmware(*fw);
491 *fw = NULL;
492}
493
494static void mwl8k_release_firmware(struct mwl8k_priv *priv)
495{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100496 mwl8k_release_fw(&priv->fw_ucode);
497 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100498}
499
Brian Cavagnolo99020472010-11-12 17:23:52 -0800500/* states for asynchronous f/w loading */
501static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
502enum {
503 FW_STATE_INIT = 0,
504 FW_STATE_LOADING_PREF,
505 FW_STATE_LOADING_ALT,
506 FW_STATE_ERROR,
507};
508
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100509/* Request fw image */
510static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800511 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800512 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100513{
514 /* release current image */
515 if (*fw != NULL)
516 mwl8k_release_fw(fw);
517
Brian Cavagnolo99020472010-11-12 17:23:52 -0800518 if (nowait)
519 return request_firmware_nowait(THIS_MODULE, 1, fname,
520 &priv->pdev->dev, GFP_KERNEL,
521 priv, mwl8k_fw_state_machine);
522 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800523 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100524}
525
Brian Cavagnolo99020472010-11-12 17:23:52 -0800526static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
527 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100528{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200529 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100530 int rc;
531
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200532 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800533 if (nowait)
534 rc = mwl8k_request_fw(priv, di->helper_image,
535 &priv->fw_helper, true);
536 else
537 rc = mwl8k_request_fw(priv, di->helper_image,
538 &priv->fw_helper, false);
539 if (rc)
540 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
541 pci_name(priv->pdev), di->helper_image);
542
543 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200544 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100545 }
546
Brian Cavagnolo99020472010-11-12 17:23:52 -0800547 if (nowait) {
548 /*
549 * if we get here, no helper image is needed. Skip the
550 * FW_STATE_INIT state.
551 */
552 priv->fw_state = FW_STATE_LOADING_PREF;
553 rc = mwl8k_request_fw(priv, fw_image,
554 &priv->fw_ucode,
555 true);
556 } else
557 rc = mwl8k_request_fw(priv, fw_image,
558 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100559 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200560 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800561 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100562 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100563 return rc;
564 }
565
566 return 0;
567}
568
569struct mwl8k_cmd_pkt {
570 __le16 code;
571 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100572 __u8 seq_num;
573 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100574 __le16 result;
575 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000576} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100577
578/*
579 * Firmware loading.
580 */
581static int
582mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
583{
584 void __iomem *regs = priv->regs;
585 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100586 int loops;
587
588 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
589 if (pci_dma_mapping_error(priv->pdev, dma_addr))
590 return -ENOMEM;
591
592 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
593 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
594 iowrite32(MWL8K_H2A_INT_DOORBELL,
595 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
596 iowrite32(MWL8K_H2A_INT_DUMMY,
597 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
598
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100599 loops = 1000;
600 do {
601 u32 int_code;
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530602 if (priv->is_8764) {
603 int_code = ioread32(regs +
604 MWL8K_HIU_H2A_INTERRUPT_STATUS);
605 if (int_code == 0)
606 break;
607 } else {
608 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
609 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
610 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
611 break;
612 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100613 }
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200614 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100615 udelay(1);
616 } while (--loops);
617
618 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
619
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200620 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100621}
622
623static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
624 const u8 *data, size_t length)
625{
626 struct mwl8k_cmd_pkt *cmd;
627 int done;
628 int rc = 0;
629
630 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
631 if (cmd == NULL)
632 return -ENOMEM;
633
634 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
635 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100636 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100637 cmd->result = 0;
638
639 done = 0;
640 while (length) {
641 int block_size = length > 256 ? 256 : length;
642
643 memcpy(cmd->payload, data + done, block_size);
644 cmd->length = cpu_to_le16(block_size);
645
646 rc = mwl8k_send_fw_load_cmd(priv, cmd,
647 sizeof(*cmd) + block_size);
648 if (rc)
649 break;
650
651 done += block_size;
652 length -= block_size;
653 }
654
655 if (!rc) {
656 cmd->length = 0;
657 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
658 }
659
660 kfree(cmd);
661
662 return rc;
663}
664
665static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
666 const u8 *data, size_t length)
667{
668 unsigned char *buffer;
669 int may_continue, rc = 0;
670 u32 done, prev_block_size;
671
672 buffer = kmalloc(1024, GFP_KERNEL);
673 if (buffer == NULL)
674 return -ENOMEM;
675
676 done = 0;
677 prev_block_size = 0;
678 may_continue = 1000;
679 while (may_continue > 0) {
680 u32 block_size;
681
682 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
683 if (block_size & 1) {
684 block_size &= ~1;
685 may_continue--;
686 } else {
687 done += prev_block_size;
688 length -= prev_block_size;
689 }
690
691 if (block_size > 1024 || block_size > length) {
692 rc = -EOVERFLOW;
693 break;
694 }
695
696 if (length == 0) {
697 rc = 0;
698 break;
699 }
700
701 if (block_size == 0) {
702 rc = -EPROTO;
703 may_continue--;
704 udelay(1);
705 continue;
706 }
707
708 prev_block_size = block_size;
709 memcpy(buffer, data + done, block_size);
710
711 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
712 if (rc)
713 break;
714 }
715
716 if (!rc && length != 0)
717 rc = -EREMOTEIO;
718
719 kfree(buffer);
720
721 return rc;
722}
723
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200724static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100725{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200726 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800727 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200728 int rc;
729 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100730
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530731 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800732 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100733
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200734 if (helper == NULL) {
735 printk(KERN_ERR "%s: helper image needed but none "
736 "given\n", pci_name(priv->pdev));
737 return -EINVAL;
738 }
739
740 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100741 if (rc) {
742 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200743 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100744 return rc;
745 }
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +0530746 msleep(20);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100747
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200748 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100749 } else {
Nishant Sarmukadam98929822013-03-01 17:13:01 +0530750 if (priv->is_8764)
751 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
752 else
753 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100754 }
755
756 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200757 printk(KERN_ERR "%s: unable to load firmware image\n",
758 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100759 return rc;
760 }
761
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100762 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100763
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100764 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100765 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200766 u32 ready_code;
767
768 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
769 if (ready_code == MWL8K_FWAP_READY) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000770 priv->ap_fw = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100771 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200772 } else if (ready_code == MWL8K_FWSTA_READY) {
Rusty Russell3db1cd52011-12-19 13:56:45 +0000773 priv->ap_fw = false;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200774 break;
775 }
776
777 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100778 udelay(1);
779 } while (--loops);
780
781 return loops ? 0 : -ETIMEDOUT;
782}
783
784
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100785/* DMA header used by firmware and hardware. */
786struct mwl8k_dma_data {
787 __le16 fwlen;
788 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100789 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000790} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100791
792/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100793static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100794{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100795 struct mwl8k_dma_data *tr;
796 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100797
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100798 tr = (struct mwl8k_dma_data *)skb->data;
799 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
800
801 if (hdrlen != sizeof(tr->wh)) {
802 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
803 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
804 *((__le16 *)(tr->data - 2)) = qos;
805 } else {
806 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
807 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100808 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100809
810 if (hdrlen != sizeof(*tr))
811 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100812}
813
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530814#define REDUCED_TX_HEADROOM 8
815
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800816static void
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530817mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
818 int head_pad, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100819{
820 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100821 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800822 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100823 struct mwl8k_dma_data *tr;
824
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100825 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100826 * Add a firmware DMA header; the firmware requires that we
827 * present a 2-byte payload length followed by a 4-address
828 * header (without QoS field), followed (optionally) by any
829 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100830 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100831 wh = (struct ieee80211_hdr *)skb->data;
832
833 hdrlen = ieee80211_hdrlen(wh->frame_control);
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530834
835 /*
836 * Check if skb_resize is required because of
837 * tx_headroom adjustment.
838 */
839 if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
840 + REDUCED_TX_HEADROOM))) {
841 if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
842
843 wiphy_err(priv->hw->wiphy,
844 "Failed to reallocate TX buffer\n");
845 return;
846 }
847 skb->truesize += REDUCED_TX_HEADROOM;
848 }
849
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530850 reqd_hdrlen = sizeof(*tr) + head_pad;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800851
852 if (hdrlen != reqd_hdrlen)
853 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100854
855 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800856 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100857
858 tr = (struct mwl8k_dma_data *)skb->data;
859 if (wh != &tr->wh)
860 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100861 if (hdrlen != sizeof(tr->wh))
862 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100863
864 /*
865 * Firmware length is the length of the fully formed "802.11
866 * payload". That is, everything except for the 802.11 header.
867 * This includes all crypto material including the MIC.
868 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800869 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100870}
871
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +0530872static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
873 struct sk_buff *skb)
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800874{
875 struct ieee80211_hdr *wh;
876 struct ieee80211_tx_info *tx_info;
877 struct ieee80211_key_conf *key_conf;
878 int data_pad;
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530879 int head_pad = 0;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800880
881 wh = (struct ieee80211_hdr *)skb->data;
882
883 tx_info = IEEE80211_SKB_CB(skb);
884
885 key_conf = NULL;
886 if (ieee80211_is_data(wh->frame_control))
887 key_conf = tx_info->control.hw_key;
888
889 /*
890 * Make sure the packet header is in the DMA header format (4-address
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530891 * without QoS), and add head & tail padding when HW crypto is enabled.
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800892 *
893 * We have the following trailer padding requirements:
894 * - WEP: 4 trailer bytes (ICV)
895 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
896 * - CCMP: 8 trailer bytes (MIC)
897 */
898 data_pad = 0;
899 if (key_conf != NULL) {
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530900 head_pad = key_conf->iv_len;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800901 switch (key_conf->cipher) {
902 case WLAN_CIPHER_SUITE_WEP40:
903 case WLAN_CIPHER_SUITE_WEP104:
904 data_pad = 4;
905 break;
906 case WLAN_CIPHER_SUITE_TKIP:
907 data_pad = 12;
908 break;
909 case WLAN_CIPHER_SUITE_CCMP:
910 data_pad = 8;
911 break;
912 }
913 }
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +0530914 mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800915}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100916
917/*
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530918 * Packet reception for 88w8366/88w8764 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200919 */
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530920struct mwl8k_rxd_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200921 __le16 pkt_len;
922 __u8 sq2;
923 __u8 rate;
924 __le32 pkt_phys_addr;
925 __le32 next_rxd_phys_addr;
926 __le16 qos_control;
927 __le16 htsig2;
928 __le32 hw_rssi_info;
929 __le32 hw_noise_floor_info;
930 __u8 noise_floor;
931 __u8 pad0[3];
932 __u8 rssi;
933 __u8 rx_status;
934 __u8 channel;
935 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000936} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200937
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530938#define MWL8K_AP_RATE_INFO_MCS_FORMAT 0x80
939#define MWL8K_AP_RATE_INFO_40MHZ 0x40
940#define MWL8K_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100941
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530942#define MWL8K_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200943
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530944/* 8366/8764 AP rx_status bits */
945#define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
946#define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
947#define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
948#define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
949#define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800950
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530951static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200952{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530953 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200954
955 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530956 rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200957}
958
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530959static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200960{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530961 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200962
963 rxd->pkt_len = cpu_to_le16(len);
964 rxd->pkt_phys_addr = cpu_to_le32(addr);
965 wmb();
966 rxd->rx_ctrl = 0;
967}
968
969static int
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530970mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
971 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200972{
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530973 struct mwl8k_rxd_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200974
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530975 if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200976 return -1;
977 rmb();
978
979 memset(status, 0, sizeof(*status));
980
981 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400982 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200983
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530984 if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200985 status->flag |= RX_FLAG_HT;
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530986 if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100987 status->flag |= RX_FLAG_40MHZ;
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +0530988 status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200989 } else {
990 int i;
991
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100992 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
993 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200994 status->rate_idx = i;
995 break;
996 }
997 }
998 }
999
Lennert Buytenhek85478342010-01-12 13:48:56 +01001000 if (rxd->channel > 14) {
1001 status->band = IEEE80211_BAND_5GHZ;
1002 if (!(status->flag & RX_FLAG_HT))
1003 status->rate_idx -= 5;
1004 } else {
1005 status->band = IEEE80211_BAND_2GHZ;
1006 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001007 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1008 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001009
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001010 *qos = rxd->qos_control;
1011
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05301012 if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
1013 (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
1014 (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001015 status->flag |= RX_FLAG_MMIC_ERROR;
1016
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001017 return le16_to_cpu(rxd->pkt_len);
1018}
1019
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05301020static struct rxd_ops rxd_ap_ops = {
1021 .rxd_size = sizeof(struct mwl8k_rxd_ap),
1022 .rxd_init = mwl8k_rxd_ap_init,
1023 .rxd_refill = mwl8k_rxd_ap_refill,
1024 .rxd_process = mwl8k_rxd_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02001025};
1026
1027/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001028 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001029 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001030struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001031 __le16 pkt_len;
1032 __u8 link_quality;
1033 __u8 noise_level;
1034 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001035 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001036 __le16 qos_control;
1037 __le16 rate_info;
1038 __le32 pad0[4];
1039 __u8 rssi;
1040 __u8 channel;
1041 __le16 pad1;
1042 __u8 rx_ctrl;
1043 __u8 rx_status;
1044 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +00001045} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001046
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001047#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
1048#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
1049#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
1050#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
1051#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
1052#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001053
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001054#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001055#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
1056/* ICV=0 or MIC=1 */
1057#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
1058/* Key is uploaded only in failure case */
1059#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001060
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001061static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001062{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001063 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001064
1065 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001066 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001067}
1068
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001069static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001070{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001071 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001072
1073 rxd->pkt_len = cpu_to_le16(len);
1074 rxd->pkt_phys_addr = cpu_to_le32(addr);
1075 wmb();
1076 rxd->rx_ctrl = 0;
1077}
1078
1079static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001080mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -04001081 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001082{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001083 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001084 u16 rate_info;
1085
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001086 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001087 return -1;
1088 rmb();
1089
1090 rate_info = le16_to_cpu(rxd->rate_info);
1091
1092 memset(status, 0, sizeof(*status));
1093
1094 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -04001095 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001096 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1097 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001098
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001099 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001100 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001101 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001102 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001103 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001104 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001105 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001106 status->flag |= RX_FLAG_HT;
1107
Lennert Buytenhek85478342010-01-12 13:48:56 +01001108 if (rxd->channel > 14) {
1109 status->band = IEEE80211_BAND_5GHZ;
1110 if (!(status->flag & RX_FLAG_HT))
1111 status->rate_idx -= 5;
1112 } else {
1113 status->band = IEEE80211_BAND_2GHZ;
1114 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001115 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1116 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001117
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001118 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001119 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1120 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1121 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001122
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001123 return le16_to_cpu(rxd->pkt_len);
1124}
1125
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001126static struct rxd_ops rxd_sta_ops = {
1127 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1128 .rxd_init = mwl8k_rxd_sta_init,
1129 .rxd_refill = mwl8k_rxd_sta_refill,
1130 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001131};
1132
1133
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001134#define MWL8K_RX_DESCS 256
1135#define MWL8K_RX_MAXSZ 3800
1136
1137static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1138{
1139 struct mwl8k_priv *priv = hw->priv;
1140 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1141 int size;
1142 int i;
1143
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001144 rxq->rxd_count = 0;
1145 rxq->head = 0;
1146 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001147
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001148 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001149
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001150 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1151 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001152 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001153 return -ENOMEM;
1154 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001155 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001156
Shan Weib9ede5f2011-03-08 11:02:03 +08001157 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001158 if (rxq->buf == NULL) {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001159 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001160 return -ENOMEM;
1161 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001162
1163 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001164 int desc_size;
1165 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001166 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001167 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001168
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001169 desc_size = priv->rxd_ops->rxd_size;
1170 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001171
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001172 nexti = i + 1;
1173 if (nexti == MWL8K_RX_DESCS)
1174 nexti = 0;
1175 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1176
1177 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001178 }
1179
1180 return 0;
1181}
1182
1183static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1184{
1185 struct mwl8k_priv *priv = hw->priv;
1186 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1187 int refilled;
1188
1189 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001190 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001191 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001192 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001193 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001194 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001195
1196 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1197 if (skb == NULL)
1198 break;
1199
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001200 addr = pci_map_single(priv->pdev, skb->data,
1201 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001202
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001203 rxq->rxd_count++;
1204 rx = rxq->tail++;
1205 if (rxq->tail == MWL8K_RX_DESCS)
1206 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001207 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001208 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001209
1210 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1211 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001212
1213 refilled++;
1214 }
1215
1216 return refilled;
1217}
1218
1219/* Must be called only when the card's reception is completely halted */
1220static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1221{
1222 struct mwl8k_priv *priv = hw->priv;
1223 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1224 int i;
1225
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001226 if (rxq->rxd == NULL)
1227 return;
1228
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001229 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001230 if (rxq->buf[i].skb != NULL) {
1231 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001232 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001233 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001234 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001235
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001236 kfree_skb(rxq->buf[i].skb);
1237 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001238 }
1239 }
1240
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001241 kfree(rxq->buf);
1242 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001243
1244 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001245 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001246 rxq->rxd, rxq->rxd_dma);
1247 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001248}
1249
1250
1251/*
1252 * Scan a list of BSSIDs to process for finalize join.
1253 * Allows for extension to process multiple BSSIDs.
1254 */
1255static inline int
1256mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1257{
1258 return priv->capture_beacon &&
1259 ieee80211_is_beacon(wh->frame_control) &&
Julia Lawall3f9a79b2013-12-30 19:15:01 +01001260 ether_addr_equal_64bits(wh->addr3, priv->capture_bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001261}
1262
Lennert Buytenhek37797522009-10-22 20:19:45 +02001263static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1264 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001265{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001266 struct mwl8k_priv *priv = hw->priv;
1267
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001268 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001269 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001270
1271 /*
1272 * Use GFP_ATOMIC as rxq_process is called from
1273 * the primary interrupt handler, memory allocation call
1274 * must not sleep.
1275 */
1276 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1277 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001278 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001279}
1280
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001281static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1282 u8 *bssid)
1283{
1284 struct mwl8k_vif *mwl8k_vif;
1285
1286 list_for_each_entry(mwl8k_vif,
1287 vif_list, list) {
1288 if (memcmp(bssid, mwl8k_vif->bssid,
1289 ETH_ALEN) == 0)
1290 return mwl8k_vif;
1291 }
1292
1293 return NULL;
1294}
1295
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001296static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1297{
1298 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001299 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1301 int processed;
1302
1303 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001304 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001305 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001306 void *rxd;
1307 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001308 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001309 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001310 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001311
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001312 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001313 if (skb == NULL)
1314 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001315
1316 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1317
John W. Linville0d462bb2010-07-28 14:04:24 -04001318 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1319 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001320 if (pkt_len < 0)
1321 break;
1322
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001323 rxq->buf[rxq->head].skb = NULL;
1324
1325 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001326 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001327 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001328 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001329
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001330 rxq->head++;
1331 if (rxq->head == MWL8K_RX_DESCS)
1332 rxq->head = 0;
1333
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001334 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001335
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001336 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001337
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001338 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001339 * Check for a pending join operation. Save a
1340 * copy of the beacon and schedule a tasklet to
1341 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001342 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001343 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001344 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001346 if (ieee80211_has_protected(wh->frame_control)) {
1347
1348 /* Check if hw crypto has been enabled for
1349 * this bss. If yes, set the status flags
1350 * accordingly
1351 */
1352 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1353 wh->addr1);
1354
1355 if (mwl8k_vif != NULL &&
Joe Perches23677ce2012-02-09 11:17:23 +00001356 mwl8k_vif->is_hw_crypto_enabled) {
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001357 /*
1358 * When MMIC ERROR is encountered
1359 * by the firmware, payload is
1360 * dropped and only 32 bytes of
1361 * mwl8k Firmware header is sent
1362 * to the host.
1363 *
1364 * We need to add four bytes of
1365 * key information. In it
1366 * MAC80211 expects keyidx set to
1367 * 0 for triggering Counter
1368 * Measure of MMIC failure.
1369 */
1370 if (status.flag & RX_FLAG_MMIC_ERROR) {
1371 struct mwl8k_dma_data *tr;
1372 tr = (struct mwl8k_dma_data *)skb->data;
1373 memset((void *)&(tr->data), 0, 4);
1374 pkt_len += 4;
1375 }
1376
1377 if (!ieee80211_is_auth(wh->frame_control))
1378 status.flag |= RX_FLAG_IV_STRIPPED |
1379 RX_FLAG_DECRYPTED |
1380 RX_FLAG_MMIC_STRIPPED;
1381 }
1382 }
1383
1384 skb_put(skb, pkt_len);
1385 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001386 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1387 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001388
1389 processed++;
1390 }
1391
1392 return processed;
1393}
1394
1395
1396/*
1397 * Packet transmission.
1398 */
1399
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001400#define MWL8K_TXD_STATUS_OK 0x00000001
1401#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1402#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1403#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001404#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001405
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001406#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1407#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1408#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1409#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1410#define MWL8K_QOS_EOSP 0x0010
1411
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001412struct mwl8k_tx_desc {
1413 __le32 status;
1414 __u8 data_rate;
1415 __u8 tx_priority;
1416 __le16 qos_control;
1417 __le32 pkt_phys_addr;
1418 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001419 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001420 __le32 next_txd_phys_addr;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07001421 __le32 timestamp;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001422 __le16 rate_info;
1423 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001424 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001425} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001426
1427#define MWL8K_TX_DESCS 128
1428
1429static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1430{
1431 struct mwl8k_priv *priv = hw->priv;
1432 struct mwl8k_tx_queue *txq = priv->txq + index;
1433 int size;
1434 int i;
1435
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001436 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001437 txq->head = 0;
1438 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439
1440 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1441
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001442 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1443 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001444 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001445 return -ENOMEM;
1446 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001447 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001448
Shan Weib9ede5f2011-03-08 11:02:03 +08001449 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001450 if (txq->skb == NULL) {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001451 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001452 return -ENOMEM;
1453 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001454
1455 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1456 struct mwl8k_tx_desc *tx_desc;
1457 int nexti;
1458
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001459 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001460 nexti = (i + 1) % MWL8K_TX_DESCS;
1461
1462 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001463 tx_desc->next_txd_phys_addr =
1464 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001465 }
1466
1467 return 0;
1468}
1469
1470static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1471{
1472 iowrite32(MWL8K_H2A_INT_PPA_READY,
1473 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1474 iowrite32(MWL8K_H2A_INT_DUMMY,
1475 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1476 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1477}
1478
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001479static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001480{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001481 struct mwl8k_priv *priv = hw->priv;
1482 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001483
Brian Cavagnoloe6007072011-03-17 11:58:44 -07001484 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001485 struct mwl8k_tx_queue *txq = priv->txq + i;
1486 int fw_owned = 0;
1487 int drv_owned = 0;
1488 int unused = 0;
1489 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001490
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001491 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001492 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1493 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001494
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001495 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001497 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001498 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001499 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001500
1501 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001502 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001503 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001504
Joe Perchesc96c31e2010-07-26 14:39:58 -07001505 wiphy_err(hw->wiphy,
1506 "txq[%d] len=%d head=%d tail=%d "
1507 "fw_owned=%d drv_owned=%d unused=%d\n",
1508 i,
1509 txq->len, txq->head, txq->tail,
1510 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001511 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001512}
1513
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001514/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001515 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001516 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001517#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001518
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001519static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001520{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001521 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001522 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001523 int retry;
1524 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001525
1526 might_sleep();
1527
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05301528 /* Since fw restart is in progress, allow only the firmware
1529 * commands from the restart code and block the other
1530 * commands since they are going to fail in any case since
1531 * the firmware has crashed
1532 */
1533 if (priv->hw_restart_in_progress) {
1534 if (priv->hw_restart_owner == current)
1535 return 0;
1536 else
1537 return -EBUSY;
1538 }
1539
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05301540 if (atomic_read(&priv->watchdog_event_pending))
1541 return 0;
1542
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001543 /*
1544 * The TX queues are stopped at this point, so this test
1545 * doesn't need to take ->tx_lock.
1546 */
1547 if (!priv->pending_tx_pkts)
1548 return 0;
1549
Nishant Sarmukadambbf71a82013-05-24 14:42:25 +05301550 retry = 1;
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001551 rc = 0;
1552
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001553 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001554 priv->tx_wait = &tx_wait;
1555 while (!rc) {
1556 int oldcount;
1557 unsigned long timeout;
1558
1559 oldcount = priv->pending_tx_pkts;
1560
1561 spin_unlock_bh(&priv->tx_lock);
1562 timeout = wait_for_completion_timeout(&tx_wait,
1563 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05301564
1565 if (atomic_read(&priv->watchdog_event_pending)) {
1566 spin_lock_bh(&priv->tx_lock);
1567 priv->tx_wait = NULL;
1568 spin_unlock_bh(&priv->tx_lock);
1569 return 0;
1570 }
1571
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001572 spin_lock_bh(&priv->tx_lock);
1573
Nishant Sarmukadambbf71a82013-05-24 14:42:25 +05301574 if (timeout || !priv->pending_tx_pkts) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001575 WARN_ON(priv->pending_tx_pkts);
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301576 if (retry)
Joe Perchesc96c31e2010-07-26 14:39:58 -07001577 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001578 break;
1579 }
1580
Nishant Sarmukadambbf71a82013-05-24 14:42:25 +05301581 if (retry) {
1582 mwl8k_tx_start(priv);
1583 retry = 0;
1584 continue;
1585 }
1586
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001587 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001588 wiphy_notice(hw->wiphy,
1589 "waiting for tx rings to drain (%d -> %d pkts)\n",
1590 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001591 retry = 1;
1592 continue;
1593 }
1594
1595 priv->tx_wait = NULL;
1596
Joe Perchesc96c31e2010-07-26 14:39:58 -07001597 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1598 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001599 mwl8k_dump_tx_rings(hw);
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05301600 priv->hw_restart_in_progress = true;
1601 ieee80211_queue_work(hw, &priv->fw_reload);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001602
1603 rc = -ETIMEDOUT;
1604 }
Nishant Sarmukadam9b0b11f2013-01-08 10:16:05 +05301605 priv->tx_wait = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001606 spin_unlock_bh(&priv->tx_lock);
1607
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001608 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001609}
1610
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001611#define MWL8K_TXD_SUCCESS(status) \
1612 ((status) & (MWL8K_TXD_STATUS_OK | \
1613 MWL8K_TXD_STATUS_OK_RETRY | \
1614 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001615
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001616static int mwl8k_tid_queue_mapping(u8 tid)
1617{
1618 BUG_ON(tid > 7);
1619
1620 switch (tid) {
1621 case 0:
1622 case 3:
1623 return IEEE80211_AC_BE;
1624 break;
1625 case 1:
1626 case 2:
1627 return IEEE80211_AC_BK;
1628 break;
1629 case 4:
1630 case 5:
1631 return IEEE80211_AC_VI;
1632 break;
1633 case 6:
1634 case 7:
1635 return IEEE80211_AC_VO;
1636 break;
1637 default:
1638 return -1;
1639 break;
1640 }
1641}
1642
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001643/* The firmware will fill in the rate information
1644 * for each packet that gets queued in the hardware
John W. Linville49adc5c2011-04-27 15:04:28 -04001645 * and these macros will interpret that info.
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001646 */
1647
John W. Linville49adc5c2011-04-27 15:04:28 -04001648#define RI_FORMAT(a) (a & 0x0001)
1649#define RI_RATE_ID_MCS(a) ((a & 0x01f8) >> 3)
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001650
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001651static int
1652mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001653{
1654 struct mwl8k_priv *priv = hw->priv;
1655 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001656 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001658 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001659 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001660 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001661 struct mwl8k_tx_desc *tx_desc;
1662 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001663 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001664 struct sk_buff *skb;
1665 struct ieee80211_tx_info *info;
1666 u32 status;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001667 struct ieee80211_sta *sta;
1668 struct mwl8k_sta *sta_info = NULL;
1669 u16 rate_info;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001670 struct ieee80211_hdr *wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001671
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001672 tx = txq->head;
1673 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001674
1675 status = le32_to_cpu(tx_desc->status);
1676
1677 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1678 if (!force)
1679 break;
1680 tx_desc->status &=
1681 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1682 }
1683
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001684 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001685 BUG_ON(txq->len == 0);
1686 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001687 priv->pending_tx_pkts--;
1688
1689 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001690 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001691 skb = txq->skb[tx];
1692 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001693
1694 BUG_ON(skb == NULL);
1695 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1696
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001697 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001698
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001699 wh = (struct ieee80211_hdr *) skb->data;
1700
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001701 /* Mark descriptor as unused */
1702 tx_desc->pkt_phys_addr = 0;
1703 tx_desc->pkt_len = 0;
1704
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001705 info = IEEE80211_SKB_CB(skb);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001706 if (ieee80211_is_data(wh->frame_control)) {
Thomas Huehn89e11802012-07-11 13:21:41 +02001707 rcu_read_lock();
1708 sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
1709 wh->addr2);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001710 if (sta) {
1711 sta_info = MWL8K_STA(sta);
1712 BUG_ON(sta_info == NULL);
1713 rate_info = le16_to_cpu(tx_desc->rate_info);
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001714 /* If rate is < 6.5 Mpbs for an ht station
1715 * do not form an ampdu. If the station is a
1716 * legacy station (format = 0), do not form an
1717 * ampdu
1718 */
John W. Linville49adc5c2011-04-27 15:04:28 -04001719 if (RI_RATE_ID_MCS(rate_info) < 1 ||
1720 RI_FORMAT(rate_info) == 0) {
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001721 sta_info->is_ampdu_allowed = false;
1722 } else {
1723 sta_info->is_ampdu_allowed = true;
1724 }
1725 }
Thomas Huehn89e11802012-07-11 13:21:41 +02001726 rcu_read_unlock();
Nishant Sarmukadam170335432011-03-17 11:58:48 -07001727 }
1728
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001729 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001730
1731 /* Rate control is happening in the firmware.
1732 * Ensure no tx rate is being reported.
1733 */
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301734 info->status.rates[0].idx = -1;
1735 info->status.rates[0].count = 1;
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001736
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001737 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001738 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001739
1740 ieee80211_tx_status_irqsafe(hw, skb);
1741
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001742 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001743 }
1744
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001745 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001746}
1747
1748/* must be called only when the card's transmit is completely halted */
1749static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1750{
1751 struct mwl8k_priv *priv = hw->priv;
1752 struct mwl8k_tx_queue *txq = priv->txq + index;
1753
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001754 if (txq->txd == NULL)
1755 return;
1756
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001757 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001758
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001759 kfree(txq->skb);
1760 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001761
1762 pci_free_consistent(priv->pdev,
1763 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001764 txq->txd, txq->txd_dma);
1765 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001766}
1767
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001768/* caller must hold priv->stream_lock when calling the stream functions */
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05301769static struct mwl8k_ampdu_stream *
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001770mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1771{
1772 struct mwl8k_ampdu_stream *stream;
1773 struct mwl8k_priv *priv = hw->priv;
1774 int i;
1775
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301776 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001777 stream = &priv->ampdu[i];
1778 if (stream->state == AMPDU_NO_STREAM) {
1779 stream->sta = sta;
1780 stream->state = AMPDU_STREAM_NEW;
1781 stream->tid = tid;
1782 stream->idx = i;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001783 wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1784 sta->addr, tid);
1785 return stream;
1786 }
1787 }
1788 return NULL;
1789}
1790
1791static int
1792mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1793{
1794 int ret;
1795
1796 /* if the stream has already been started, don't start it again */
1797 if (stream->state != AMPDU_STREAM_NEW)
1798 return 0;
1799 ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1800 if (ret)
1801 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1802 "%d\n", stream->sta->addr, stream->tid, ret);
1803 else
1804 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1805 stream->sta->addr, stream->tid);
1806 return ret;
1807}
1808
1809static void
1810mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1811{
1812 wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1813 stream->tid);
1814 memset(stream, 0, sizeof(*stream));
1815}
1816
1817static struct mwl8k_ampdu_stream *
1818mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1819{
1820 struct mwl8k_priv *priv = hw->priv;
1821 int i;
1822
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301823 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001824 struct mwl8k_ampdu_stream *stream;
1825 stream = &priv->ampdu[i];
1826 if (stream->state == AMPDU_NO_STREAM)
1827 continue;
1828 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1829 stream->tid == tid)
1830 return stream;
1831 }
1832 return NULL;
1833}
1834
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001835#define MWL8K_AMPDU_PACKET_THRESHOLD 64
1836static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
1837{
1838 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1839 struct tx_traffic_info *tx_stats;
1840
1841 BUG_ON(tid >= MWL8K_MAX_TID);
1842 tx_stats = &sta_info->tx_stats[tid];
1843
1844 return sta_info->is_ampdu_allowed &&
1845 tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
1846}
1847
1848static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
1849{
1850 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1851 struct tx_traffic_info *tx_stats;
1852
1853 BUG_ON(tid >= MWL8K_MAX_TID);
1854 tx_stats = &sta_info->tx_stats[tid];
1855
1856 if (tx_stats->start_time == 0)
1857 tx_stats->start_time = jiffies;
1858
1859 /* reset the packet count after each second elapses. If the number of
1860 * packets ever exceeds the ampdu_min_traffic threshold, we will allow
1861 * an ampdu stream to be started.
1862 */
1863 if (jiffies - tx_stats->start_time > HZ) {
1864 tx_stats->pkts = 0;
1865 tx_stats->start_time = 0;
1866 } else
1867 tx_stats->pkts++;
1868}
1869
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301870/* The hardware ampdu queues start from 5.
1871 * txpriorities for ampdu queues are
1872 * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
1873 * and queue 3 is lowest (queue 4 is reserved)
1874 */
1875#define BA_QUEUE 5
1876
Johannes Berg7bb45682011-02-24 14:42:06 +01001877static void
Thomas Huehn36323f82012-07-23 21:33:42 +02001878mwl8k_txq_xmit(struct ieee80211_hw *hw,
1879 int index,
1880 struct ieee80211_sta *sta,
1881 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001882{
1883 struct mwl8k_priv *priv = hw->priv;
1884 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001885 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001886 struct ieee80211_hdr *wh;
1887 struct mwl8k_tx_queue *txq;
1888 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001889 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001890 u32 txstatus;
1891 u8 txdatarate;
1892 u16 qos;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001893 int txpriority;
1894 u8 tid = 0;
1895 struct mwl8k_ampdu_stream *stream = NULL;
1896 bool start_ba_session = false;
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05301897 bool mgmtframe = false;
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001898 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301899 bool eapol_frame = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001900
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001901 wh = (struct ieee80211_hdr *)skb->data;
1902 if (ieee80211_is_data_qos(wh->frame_control))
1903 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1904 else
1905 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001906
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301907 if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1908 eapol_frame = true;
1909
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05301910 if (ieee80211_is_mgmt(wh->frame_control))
1911 mgmtframe = true;
1912
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001913 if (priv->ap_fw)
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +05301914 mwl8k_encapsulate_tx_frame(priv, skb);
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001915 else
Yogesh Ashok Poware4eefec2011-05-05 16:30:48 +05301916 mwl8k_add_dma_header(priv, skb, 0, 0);
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001917
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001918 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919
1920 tx_info = IEEE80211_SKB_CB(skb);
1921 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001922
1923 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001924 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001925 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1926 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001927 }
1928
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001929 /* Setup firmware control bit fields for each frame type. */
1930 txstatus = 0;
1931 txdatarate = 0;
1932 if (ieee80211_is_mgmt(wh->frame_control) ||
1933 ieee80211_is_ctl(wh->frame_control)) {
1934 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001935 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001936 } else if (ieee80211_is_data(wh->frame_control)) {
1937 txdatarate = 1;
1938 if (is_multicast_ether_addr(wh->addr1))
1939 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1940
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001941 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001942 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001943 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001944 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001945 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001946 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001947
Nishant Sarmukadama0e7c6c2011-03-17 11:58:49 -07001948 /* Queue ADDBA request in the respective data queue. While setting up
1949 * the ampdu stream, mac80211 queues further packets for that
1950 * particular ra/tid pair. However, packets piled up in the hardware
1951 * for that ra/tid pair will still go out. ADDBA request and the
1952 * related data packets going out from different queues asynchronously
1953 * will cause a shift in the receiver window which might result in
1954 * ampdu packets getting dropped at the receiver after the stream has
1955 * been setup.
1956 */
1957 if (unlikely(ieee80211_is_action(wh->frame_control) &&
1958 mgmt->u.action.category == WLAN_CATEGORY_BACK &&
1959 mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
1960 priv->ap_fw)) {
1961 u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1962 tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1963 index = mwl8k_tid_queue_mapping(tid);
1964 }
1965
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001966 txpriority = index;
1967
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05301968 if (priv->ap_fw && sta && sta->ht_cap.ht_supported && !eapol_frame &&
1969 ieee80211_is_data_qos(wh->frame_control)) {
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001970 tid = qos & 0xf;
Brian Cavagnolod0805c12011-04-12 11:06:28 -07001971 mwl8k_tx_count_packet(sta, tid);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001972 spin_lock(&priv->stream_lock);
1973 stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1974 if (stream != NULL) {
1975 if (stream->state == AMPDU_STREAM_ACTIVE) {
Yogesh Ashok Powar5f2a1492013-01-03 13:21:25 +05301976 WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
Yogesh Ashok Powar7fb978b2013-01-03 13:22:56 +05301977 txpriority = (BA_QUEUE + stream->idx) %
1978 TOTAL_HW_TX_QUEUES;
1979 if (stream->idx <= 1)
1980 index = stream->idx +
1981 MWL8K_TX_WMM_QUEUES;
1982
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001983 } else if (stream->state == AMPDU_STREAM_NEW) {
1984 /* We get here if the driver sends us packets
1985 * after we've initiated a stream, but before
1986 * our ampdu_action routine has been called
1987 * with IEEE80211_AMPDU_TX_START to get the SSN
1988 * for the ADDBA request. So this packet can
1989 * go out with no risk of sequence number
1990 * mismatch. No special handling is required.
1991 */
1992 } else {
1993 /* Drop packets that would go out after the
1994 * ADDBA request was sent but before the ADDBA
1995 * response is received. If we don't do this,
1996 * the recipient would probably receive it
1997 * after the ADDBA request with SSN 0. This
1998 * will cause the recipient's BA receive window
1999 * to shift, which would cause the subsequent
2000 * packets in the BA stream to be discarded.
2001 * mac80211 queues our packets for us in this
2002 * case, so this is really just a safety check.
2003 */
2004 wiphy_warn(hw->wiphy,
2005 "Cannot send packet while ADDBA "
2006 "dialog is underway.\n");
2007 spin_unlock(&priv->stream_lock);
2008 dev_kfree_skb(skb);
2009 return;
2010 }
2011 } else {
2012 /* Defer calling mwl8k_start_stream so that the current
2013 * skb can go out before the ADDBA request. This
2014 * prevents sequence number mismatch at the recepient
2015 * as described above.
2016 */
Brian Cavagnolod0805c12011-04-12 11:06:28 -07002017 if (mwl8k_ampdu_allowed(sta, tid)) {
Nishant Sarmukadam170335432011-03-17 11:58:48 -07002018 stream = mwl8k_add_stream(hw, sta, tid);
2019 if (stream != NULL)
2020 start_ba_session = true;
2021 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002022 }
2023 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar5f2a1492013-01-03 13:21:25 +05302024 } else {
2025 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
2026 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002027 }
2028
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002029 dma = pci_map_single(priv->pdev, skb->data,
2030 skb->len, PCI_DMA_TODEVICE);
2031
2032 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07002033 wiphy_debug(hw->wiphy,
2034 "failed to dma map skb, dropping TX frame.\n");
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002035 if (start_ba_session) {
2036 spin_lock(&priv->stream_lock);
2037 mwl8k_remove_stream(hw, stream);
2038 spin_unlock(&priv->stream_lock);
2039 }
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002040 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01002041 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002042 }
2043
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002044 spin_lock_bh(&priv->tx_lock);
2045
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002046 txq = priv->txq + index;
2047
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302048 /* Mgmt frames that go out frequently are probe
2049 * responses. Other mgmt frames got out relatively
2050 * infrequently. Hence reserve 2 buffers so that
2051 * other mgmt frames do not get dropped due to an
2052 * already queued probe response in one of the
2053 * reserved buffers.
2054 */
2055
2056 if (txq->len >= MWL8K_TX_DESCS - 2) {
Joe Perches23677ce2012-02-09 11:17:23 +00002057 if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302058 if (start_ba_session) {
2059 spin_lock(&priv->stream_lock);
2060 mwl8k_remove_stream(hw, stream);
2061 spin_unlock(&priv->stream_lock);
2062 }
Nishant Sarmukadambbf71a82013-05-24 14:42:25 +05302063 mwl8k_tx_start(priv);
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302064 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadamff7aa962012-11-06 19:22:48 +05302065 pci_unmap_single(priv->pdev, dma, skb->len,
2066 PCI_DMA_TODEVICE);
Nishant Sarmukadam3a769882011-04-21 16:34:58 +05302067 dev_kfree_skb(skb);
2068 return;
Pradeep Nemavat3a7dbc32011-04-21 16:34:56 +05302069 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002070 }
2071
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002072 BUG_ON(txq->skb[txq->tail] != NULL);
2073 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002074
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002075 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002076 tx->data_rate = txdatarate;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002077 tx->tx_priority = txpriority;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002078 tx->qos_control = cpu_to_le16(qos);
2079 tx->pkt_phys_addr = cpu_to_le32(dma);
2080 tx->pkt_len = cpu_to_le16(skb->len);
2081 tx->rate_info = 0;
Thomas Huehn36323f82012-07-23 21:33:42 +02002082 if (!priv->ap_fw && sta != NULL)
2083 tx->peer_id = MWL8K_STA(sta)->peer_id;
Lennert Buytenheka6804002010-01-04 21:55:42 +01002084 else
2085 tx->peer_id = 0;
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302086
Nishant Sarmukadame1f4d692012-11-06 19:23:01 +05302087 if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302088 tx->timestamp = cpu_to_le32(ioread32(priv->regs +
2089 MWL8K_HW_TIMER_REGISTER));
Nishant Sarmukadamb8d9e572012-11-06 19:23:15 +05302090 else
2091 tx->timestamp = 0;
Pradeep Nemavat566875d2011-04-21 16:34:57 +05302092
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002093 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002094 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
2095
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02002096 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002097 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002098
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002099 txq->tail++;
2100 if (txq->tail == MWL8K_TX_DESCS)
2101 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002102
Lennert Buytenhek23b33902009-07-17 05:21:04 +02002103 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002104
2105 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07002106
2107 /* Initiate the ampdu session here */
2108 if (start_ba_session) {
2109 spin_lock(&priv->stream_lock);
2110 if (mwl8k_start_stream(hw, stream))
2111 mwl8k_remove_stream(hw, stream);
2112 spin_unlock(&priv->stream_lock);
2113 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002114}
2115
2116
2117/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002118 * Firmware access.
2119 *
2120 * We have the following requirements for issuing firmware commands:
2121 * - Some commands require that the packet transmit path is idle when
2122 * the command is issued. (For simplicity, we'll just quiesce the
2123 * transmit path for every command.)
2124 * - There are certain sequences of commands that need to be issued to
2125 * the hardware sequentially, with no other intervening commands.
2126 *
2127 * This leads to an implementation of a "firmware lock" as a mutex that
2128 * can be taken recursively, and which is taken by both the low-level
2129 * command submission function (mwl8k_post_cmd) as well as any users of
2130 * that function that require issuing of an atomic sequence of commands,
2131 * and quiesces the transmit path whenever it's taken.
2132 */
2133static int mwl8k_fw_lock(struct ieee80211_hw *hw)
2134{
2135 struct mwl8k_priv *priv = hw->priv;
2136
2137 if (priv->fw_mutex_owner != current) {
2138 int rc;
2139
2140 mutex_lock(&priv->fw_mutex);
2141 ieee80211_stop_queues(hw);
2142
2143 rc = mwl8k_tx_wait_empty(hw);
2144 if (rc) {
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05302145 if (!priv->hw_restart_in_progress)
2146 ieee80211_wake_queues(hw);
2147
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002148 mutex_unlock(&priv->fw_mutex);
2149
2150 return rc;
2151 }
2152
2153 priv->fw_mutex_owner = current;
2154 }
2155
2156 priv->fw_mutex_depth++;
2157
2158 return 0;
2159}
2160
2161static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
2162{
2163 struct mwl8k_priv *priv = hw->priv;
2164
2165 if (!--priv->fw_mutex_depth) {
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05302166 if (!priv->hw_restart_in_progress)
2167 ieee80211_wake_queues(hw);
2168
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002169 priv->fw_mutex_owner = NULL;
2170 mutex_unlock(&priv->fw_mutex);
2171 }
2172}
2173
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302174static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
2175 u32 bitmap);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002176
2177/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002178 * Command processing.
2179 */
2180
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002181/* Timeout firmware commands after 10s */
2182#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002183
2184static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
2185{
2186 DECLARE_COMPLETION_ONSTACK(cmd_wait);
2187 struct mwl8k_priv *priv = hw->priv;
2188 void __iomem *regs = priv->regs;
2189 dma_addr_t dma_addr;
2190 unsigned int dma_size;
2191 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002192 unsigned long timeout = 0;
2193 u8 buf[32];
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302194 u32 bitmap = 0;
2195
2196 wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
2197 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
2198
2199 /* Before posting firmware commands that could change the hardware
2200 * characteristics, make sure that all BSSes are stopped temporary.
2201 * Enable these stopped BSSes after completion of the commands
2202 */
2203
2204 rc = mwl8k_fw_lock(hw);
2205 if (rc)
2206 return rc;
2207
2208 if (priv->ap_fw && priv->running_bsses) {
2209 switch (le16_to_cpu(cmd->code)) {
2210 case MWL8K_CMD_SET_RF_CHANNEL:
2211 case MWL8K_CMD_RADIO_CONTROL:
2212 case MWL8K_CMD_RF_TX_POWER:
2213 case MWL8K_CMD_TX_POWER:
2214 case MWL8K_CMD_RF_ANTENNA:
2215 case MWL8K_CMD_RTS_THRESHOLD:
2216 case MWL8K_CMD_MIMO_CONFIG:
2217 bitmap = priv->running_bsses;
2218 mwl8k_enable_bsses(hw, false, bitmap);
2219 break;
2220 }
2221 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002222
John W. Linvilleb6037422010-07-20 13:55:00 -04002223 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002224 dma_size = le16_to_cpu(cmd->length);
2225 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
2226 PCI_DMA_BIDIRECTIONAL);
2227 if (pci_dma_mapping_error(priv->pdev, dma_addr))
2228 return -ENOMEM;
2229
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002230 priv->hostcmd_wait = &cmd_wait;
2231 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
2232 iowrite32(MWL8K_H2A_INT_DOORBELL,
2233 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2234 iowrite32(MWL8K_H2A_INT_DUMMY,
2235 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002236
2237 timeout = wait_for_completion_timeout(&cmd_wait,
2238 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
2239
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002240 priv->hostcmd_wait = NULL;
2241
Lennert Buytenhek618952a2009-08-18 03:18:01 +02002242
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02002243 pci_unmap_single(priv->pdev, dma_addr, dma_size,
2244 PCI_DMA_BIDIRECTIONAL);
2245
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002246 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07002247 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002248 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2249 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002250 rc = -ETIMEDOUT;
2251 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002252 int ms;
2253
2254 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
2255
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002256 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002257 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07002258 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002259 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2260 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01002261 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07002262 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07002263 mwl8k_cmd_name(cmd->code,
2264 buf, sizeof(buf)),
2265 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002266 }
2267
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05302268 if (bitmap)
2269 mwl8k_enable_bsses(hw, true, bitmap);
2270
2271 mwl8k_fw_unlock(hw);
2272
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002273 return rc;
2274}
2275
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01002276static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
2277 struct ieee80211_vif *vif,
2278 struct mwl8k_cmd_pkt *cmd)
2279{
2280 if (vif != NULL)
2281 cmd->macid = MWL8K_VIF(vif)->macid;
2282 return mwl8k_post_cmd(hw, cmd);
2283}
2284
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002285/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002286 * Setup code shared between STA and AP firmware images.
2287 */
2288static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2289{
2290 struct mwl8k_priv *priv = hw->priv;
2291
2292 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2293 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2294
2295 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2296 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2297
2298 priv->band_24.band = IEEE80211_BAND_2GHZ;
2299 priv->band_24.channels = priv->channels_24;
2300 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2301 priv->band_24.bitrates = priv->rates_24;
2302 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2303
2304 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
2305}
2306
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01002307static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2308{
2309 struct mwl8k_priv *priv = hw->priv;
2310
2311 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2312 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2313
2314 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2315 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2316
2317 priv->band_50.band = IEEE80211_BAND_5GHZ;
2318 priv->band_50.channels = priv->channels_50;
2319 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2320 priv->band_50.bitrates = priv->rates_50;
2321 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2322
2323 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
2324}
2325
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002326/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002327 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002328 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002329struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002330 struct mwl8k_cmd_pkt header;
2331 __u8 hw_rev;
2332 __u8 host_interface;
2333 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002334 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002335 __le16 region_code;
2336 __le32 fw_rev;
2337 __le32 ps_cookie;
2338 __le32 caps;
2339 __u8 mcs_bitmap[16];
2340 __le32 rx_queue_ptr;
2341 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002342 __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002343 __le32 caps2;
2344 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002345 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002346} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002347
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002348#define MWL8K_CAP_MAX_AMSDU 0x20000000
2349#define MWL8K_CAP_GREENFIELD 0x08000000
2350#define MWL8K_CAP_AMPDU 0x04000000
2351#define MWL8K_CAP_RX_STBC 0x01000000
2352#define MWL8K_CAP_TX_STBC 0x00800000
2353#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
2354#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
2355#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
2356#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
2357#define MWL8K_CAP_DELAY_BA 0x00003000
2358#define MWL8K_CAP_MIMO 0x00000200
2359#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01002360#define MWL8K_CAP_BAND_MASK 0x00000007
2361#define MWL8K_CAP_5GHZ 0x00000004
2362#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002363
Lennert Buytenhek06953232010-01-12 13:49:41 +01002364static void
2365mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2366 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002367{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002368 int rx_streams;
2369 int tx_streams;
2370
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002371 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002372
2373 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002374 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002375 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002376 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002377 if (cap & MWL8K_CAP_AMPDU) {
2378 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002379 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2380 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002381 }
2382 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002383 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002384 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002385 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002386 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002387 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002388 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002389 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002390 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002391 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002392 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002393 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002394
2395 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2396 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2397
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002398 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002399 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002400 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002401 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002402 band->ht_cap.mcs.rx_mask[2] = 0xff;
2403 band->ht_cap.mcs.rx_mask[4] = 0x01;
2404 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002405
2406 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002407 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2408 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002409 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2410 }
2411}
2412
Lennert Buytenhek06953232010-01-12 13:49:41 +01002413static void
2414mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2415{
2416 struct mwl8k_priv *priv = hw->priv;
2417
Jonas Gorskic3f251a2013-03-11 17:44:21 +01002418 if (priv->caps)
2419 return;
2420
Lennert Buytenhek06953232010-01-12 13:49:41 +01002421 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2422 mwl8k_setup_2ghz_band(hw);
2423 if (caps & MWL8K_CAP_MIMO)
2424 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2425 }
2426
2427 if (caps & MWL8K_CAP_5GHZ) {
2428 mwl8k_setup_5ghz_band(hw);
2429 if (caps & MWL8K_CAP_MIMO)
2430 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2431 }
Jonas Gorskic3f251a2013-03-11 17:44:21 +01002432
2433 priv->caps = caps;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002434}
2435
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002436static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002437{
2438 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002439 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002440 int rc;
2441 int i;
2442
2443 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2444 if (cmd == NULL)
2445 return -ENOMEM;
2446
2447 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2448 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2449
2450 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2451 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002452 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002453 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2454 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002455 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002456 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002457 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002458
2459 rc = mwl8k_post_cmd(hw, &cmd->header);
2460
2461 if (!rc) {
2462 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2463 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002464 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002465 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002466 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002467 priv->ap_macids_supported = 0x00000000;
2468 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002469 }
2470
2471 kfree(cmd);
2472 return rc;
2473}
2474
2475/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002476 * CMD_GET_HW_SPEC (AP version).
2477 */
2478struct mwl8k_cmd_get_hw_spec_ap {
2479 struct mwl8k_cmd_pkt header;
2480 __u8 hw_rev;
2481 __u8 host_interface;
2482 __le16 num_wcb;
2483 __le16 num_mcaddrs;
2484 __u8 perm_addr[ETH_ALEN];
2485 __le16 region_code;
2486 __le16 num_antenna;
2487 __le32 fw_rev;
2488 __le32 wcbbase0;
2489 __le32 rxwrptr;
2490 __le32 rxrdptr;
2491 __le32 ps_cookie;
2492 __le32 wcbbase1;
2493 __le32 wcbbase2;
2494 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002495 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002496 __le32 caps;
2497 __le32 num_of_ampdu_queues;
2498 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002499} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002500
2501static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2502{
2503 struct mwl8k_priv *priv = hw->priv;
2504 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002505 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002506 u32 api_version;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002507
2508 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2509 if (cmd == NULL)
2510 return -ENOMEM;
2511
2512 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2513 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2514
2515 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2516 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2517
2518 rc = mwl8k_post_cmd(hw, &cmd->header);
2519
2520 if (!rc) {
2521 int off;
2522
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002523 api_version = le32_to_cpu(cmd->fw_api_version);
2524 if (priv->device_info->fw_api_ap != api_version) {
2525 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2526 " Expected %d got %d.\n", MWL8K_NAME,
2527 priv->device_info->part_name,
2528 priv->device_info->fw_api_ap,
2529 api_version);
2530 rc = -EINVAL;
2531 goto done;
2532 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002533 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2534 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2535 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2536 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002537 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002538 priv->ap_macids_supported = 0x000000ff;
Yogesh Ashok Poward59c1cf2013-01-25 16:20:03 +05302539 priv->sta_macids_supported = 0x00000100;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002540 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2541 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2542 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2543 " but we only support %d.\n",
2544 priv->num_ampdu_queues,
2545 MWL8K_MAX_AMPDU_QUEUES);
2546 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2547 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002548 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002549 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002550
2551 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002552 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002553
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002554 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2555 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2556 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2557 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002558
2559 for (i = 0; i < priv->num_ampdu_queues; i++)
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002560 priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002561 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002562 }
2563
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002564done:
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002565 kfree(cmd);
2566 return rc;
2567}
2568
2569/*
2570 * CMD_SET_HW_SPEC.
2571 */
2572struct mwl8k_cmd_set_hw_spec {
2573 struct mwl8k_cmd_pkt header;
2574 __u8 hw_rev;
2575 __u8 host_interface;
2576 __le16 num_mcaddrs;
2577 __u8 perm_addr[ETH_ALEN];
2578 __le16 region_code;
2579 __le32 fw_rev;
2580 __le32 ps_cookie;
2581 __le32 caps;
2582 __le32 rx_queue_ptr;
2583 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002584 __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002585 __le32 flags;
2586 __le32 num_tx_desc_per_queue;
2587 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002588} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002589
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002590/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2591 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2592 * the packets that are queued for more than 500ms, will be dropped in the
2593 * hardware. This helps minimizing the issues caused due to head-of-line
2594 * blocking where a slow client can hog the bandwidth and affect traffic to a
2595 * faster client.
2596 */
2597#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Nishant Sarmukadam3373b282011-06-13 16:26:15 +05302598#define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR 0x00000200
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002599#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2600#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2601#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002602
2603static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2604{
2605 struct mwl8k_priv *priv = hw->priv;
2606 struct mwl8k_cmd_set_hw_spec *cmd;
2607 int rc;
2608 int i;
2609
2610 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2611 if (cmd == NULL)
2612 return -ENOMEM;
2613
2614 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2615 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2616
2617 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2618 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002619 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002620
2621 /*
2622 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2623 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2624 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2625 * priority is interpreted the right way in firmware.
2626 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002627 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2628 int j = mwl8k_tx_queues(priv) - 1 - i;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002629 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2630 }
2631
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002632 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2633 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
Nishant Sarmukadam31d291a2011-04-21 16:34:59 +05302634 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
Nishant Sarmukadam3373b282011-06-13 16:26:15 +05302635 MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
2636 MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002637 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2638 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2639
2640 rc = mwl8k_post_cmd(hw, &cmd->header);
2641 kfree(cmd);
2642
2643 return rc;
2644}
2645
2646/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002647 * CMD_MAC_MULTICAST_ADR.
2648 */
2649struct mwl8k_cmd_mac_multicast_adr {
2650 struct mwl8k_cmd_pkt header;
2651 __le16 action;
2652 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002653 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002654};
2655
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002656#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2657#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2658#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2659#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002660
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002661static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002662__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002663 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002664{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002665 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002666 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002667 int size;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002668 int mc_count = 0;
2669
2670 if (mc_list)
2671 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002672
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002673 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002674 allmulti = 1;
2675 mc_count = 0;
2676 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002677
2678 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2679
2680 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002681 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002682 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002683
2684 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2685 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002686 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2687 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002688
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002689 if (allmulti) {
2690 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2691 } else if (mc_count) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002692 struct netdev_hw_addr *ha;
2693 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002694
2695 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2696 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002697 netdev_hw_addr_list_for_each(ha, mc_list) {
2698 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002699 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002700 }
2701
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002702 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002703}
2704
2705/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002706 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002707 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002708struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002709 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002710 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002711} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002712
2713#define MWL8K_STAT_ACK_FAILURE 9
2714#define MWL8K_STAT_RTS_FAILURE 12
2715#define MWL8K_STAT_FCS_ERROR 24
2716#define MWL8K_STAT_RTS_SUCCESS 11
2717
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002718static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2719 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002720{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002721 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002722 int rc;
2723
2724 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2725 if (cmd == NULL)
2726 return -ENOMEM;
2727
2728 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2729 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002730
2731 rc = mwl8k_post_cmd(hw, &cmd->header);
2732 if (!rc) {
2733 stats->dot11ACKFailureCount =
2734 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2735 stats->dot11RTSFailureCount =
2736 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2737 stats->dot11FCSErrorCount =
2738 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2739 stats->dot11RTSSuccessCount =
2740 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2741 }
2742 kfree(cmd);
2743
2744 return rc;
2745}
2746
2747/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002748 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002749 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002750struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002751 struct mwl8k_cmd_pkt header;
2752 __le16 action;
2753 __le16 control;
2754 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002755} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002756
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002757static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002758mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002759{
2760 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002761 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002762 int rc;
2763
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002764 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002765 return 0;
2766
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002767 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2768 if (cmd == NULL)
2769 return -ENOMEM;
2770
2771 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2772 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2773 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002774 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002775 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2776
2777 rc = mwl8k_post_cmd(hw, &cmd->header);
2778 kfree(cmd);
2779
2780 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002781 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002782
2783 return rc;
2784}
2785
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002786static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002787{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002788 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002789}
2790
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002791static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002792{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002793 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002794}
2795
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002796static int
2797mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2798{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002799 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002800
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002801 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002802
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002803 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002804}
2805
2806/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002807 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002808 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002809#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002810
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002811struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002812 struct mwl8k_cmd_pkt header;
2813 __le16 action;
2814 __le16 support_level;
2815 __le16 current_level;
2816 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002817 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002818} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002819
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002820static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002821{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002822 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002823 int rc;
2824
2825 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2826 if (cmd == NULL)
2827 return -ENOMEM;
2828
2829 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2830 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2831 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2832 cmd->support_level = cpu_to_le16(dBm);
2833
2834 rc = mwl8k_post_cmd(hw, &cmd->header);
2835 kfree(cmd);
2836
2837 return rc;
2838}
2839
2840/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002841 * CMD_TX_POWER.
2842 */
2843#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2844
2845struct mwl8k_cmd_tx_power {
2846 struct mwl8k_cmd_pkt header;
2847 __le16 action;
2848 __le16 band;
2849 __le16 channel;
2850 __le16 bw;
2851 __le16 sub_ch;
2852 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05302853} __packed;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002854
2855static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2856 struct ieee80211_conf *conf,
2857 unsigned short pwr)
2858{
Karl Beldan675a0b02013-03-25 16:26:57 +01002859 struct ieee80211_channel *channel = conf->chandef.chan;
2860 enum nl80211_channel_type channel_type =
2861 cfg80211_get_chandef_type(&conf->chandef);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002862 struct mwl8k_cmd_tx_power *cmd;
2863 int rc;
2864 int i;
2865
2866 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2867 if (cmd == NULL)
2868 return -ENOMEM;
2869
2870 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2871 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2872 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2873
2874 if (channel->band == IEEE80211_BAND_2GHZ)
2875 cmd->band = cpu_to_le16(0x1);
2876 else if (channel->band == IEEE80211_BAND_5GHZ)
2877 cmd->band = cpu_to_le16(0x4);
2878
Yogesh Ashok Powar604c4ef2012-01-17 15:45:15 +05302879 cmd->channel = cpu_to_le16(channel->hw_value);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002880
Karl Beldan675a0b02013-03-25 16:26:57 +01002881 if (channel_type == NL80211_CHAN_NO_HT ||
2882 channel_type == NL80211_CHAN_HT20) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002883 cmd->bw = cpu_to_le16(0x2);
2884 } else {
2885 cmd->bw = cpu_to_le16(0x4);
Karl Beldan675a0b02013-03-25 16:26:57 +01002886 if (channel_type == NL80211_CHAN_HT40MINUS)
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002887 cmd->sub_ch = cpu_to_le16(0x3);
Karl Beldan675a0b02013-03-25 16:26:57 +01002888 else if (channel_type == NL80211_CHAN_HT40PLUS)
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002889 cmd->sub_ch = cpu_to_le16(0x1);
2890 }
2891
2892 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2893 cmd->power_level_list[i] = cpu_to_le16(pwr);
2894
2895 rc = mwl8k_post_cmd(hw, &cmd->header);
2896 kfree(cmd);
2897
2898 return rc;
2899}
2900
2901/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002902 * CMD_RF_ANTENNA.
2903 */
2904struct mwl8k_cmd_rf_antenna {
2905 struct mwl8k_cmd_pkt header;
2906 __le16 antenna;
2907 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002908} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002909
2910#define MWL8K_RF_ANTENNA_RX 1
2911#define MWL8K_RF_ANTENNA_TX 2
2912
2913static int
2914mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2915{
2916 struct mwl8k_cmd_rf_antenna *cmd;
2917 int rc;
2918
2919 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2920 if (cmd == NULL)
2921 return -ENOMEM;
2922
2923 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2924 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2925 cmd->antenna = cpu_to_le16(antenna);
2926 cmd->mode = cpu_to_le16(mask);
2927
2928 rc = mwl8k_post_cmd(hw, &cmd->header);
2929 kfree(cmd);
2930
2931 return rc;
2932}
2933
2934/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002935 * CMD_SET_BEACON.
2936 */
2937struct mwl8k_cmd_set_beacon {
2938 struct mwl8k_cmd_pkt header;
2939 __le16 beacon_len;
2940 __u8 beacon[0];
2941};
2942
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002943static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2944 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002945{
2946 struct mwl8k_cmd_set_beacon *cmd;
2947 int rc;
2948
2949 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2950 if (cmd == NULL)
2951 return -ENOMEM;
2952
2953 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2954 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2955 cmd->beacon_len = cpu_to_le16(len);
2956 memcpy(cmd->beacon, beacon, len);
2957
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002958 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002959 kfree(cmd);
2960
2961 return rc;
2962}
2963
2964/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002965 * CMD_SET_PRE_SCAN.
2966 */
2967struct mwl8k_cmd_set_pre_scan {
2968 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002969} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002970
2971static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2972{
2973 struct mwl8k_cmd_set_pre_scan *cmd;
2974 int rc;
2975
2976 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2977 if (cmd == NULL)
2978 return -ENOMEM;
2979
2980 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2981 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2982
2983 rc = mwl8k_post_cmd(hw, &cmd->header);
2984 kfree(cmd);
2985
2986 return rc;
2987}
2988
2989/*
2990 * CMD_SET_POST_SCAN.
2991 */
2992struct mwl8k_cmd_set_post_scan {
2993 struct mwl8k_cmd_pkt header;
2994 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002995 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002996} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002997
2998static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002999mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003000{
3001 struct mwl8k_cmd_set_post_scan *cmd;
3002 int rc;
3003
3004 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3005 if (cmd == NULL)
3006 return -ENOMEM;
3007
3008 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
3009 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3010 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02003011 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003012
3013 rc = mwl8k_post_cmd(hw, &cmd->header);
3014 kfree(cmd);
3015
3016 return rc;
3017}
3018
3019/*
3020 * CMD_SET_RF_CHANNEL.
3021 */
3022struct mwl8k_cmd_set_rf_channel {
3023 struct mwl8k_cmd_pkt header;
3024 __le16 action;
3025 __u8 current_channel;
3026 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003027} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003028
3029static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003030 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003031{
Karl Beldan675a0b02013-03-25 16:26:57 +01003032 struct ieee80211_channel *channel = conf->chandef.chan;
3033 enum nl80211_channel_type channel_type =
3034 cfg80211_get_chandef_type(&conf->chandef);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003035 struct mwl8k_cmd_set_rf_channel *cmd;
3036 int rc;
3037
3038 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3039 if (cmd == NULL)
3040 return -ENOMEM;
3041
3042 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
3043 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3044 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3045 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003046
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003047 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003048 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01003049 else if (channel->band == IEEE80211_BAND_5GHZ)
3050 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003051
Karl Beldan675a0b02013-03-25 16:26:57 +01003052 if (channel_type == NL80211_CHAN_NO_HT ||
3053 channel_type == NL80211_CHAN_HT20)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003054 cmd->channel_flags |= cpu_to_le32(0x00000080);
Karl Beldan675a0b02013-03-25 16:26:57 +01003055 else if (channel_type == NL80211_CHAN_HT40MINUS)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003056 cmd->channel_flags |= cpu_to_le32(0x000001900);
Karl Beldan675a0b02013-03-25 16:26:57 +01003057 else if (channel_type == NL80211_CHAN_HT40PLUS)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003058 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003059
3060 rc = mwl8k_post_cmd(hw, &cmd->header);
3061 kfree(cmd);
3062
3063 return rc;
3064}
3065
3066/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003067 * CMD_SET_AID.
3068 */
3069#define MWL8K_FRAME_PROT_DISABLED 0x00
3070#define MWL8K_FRAME_PROT_11G 0x07
3071#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
3072#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
3073
3074struct mwl8k_cmd_update_set_aid {
3075 struct mwl8k_cmd_pkt header;
3076 __le16 aid;
3077
3078 /* AP's MAC address (BSSID) */
3079 __u8 bssid[ETH_ALEN];
3080 __le16 protection_mode;
3081 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003082} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003083
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003084static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
3085{
3086 int i;
3087 int j;
3088
3089 /*
Jonas Gorski3f524552013-04-14 14:11:58 +02003090 * Clear nonstandard rate 4.
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003091 */
3092 mask &= 0x1fef;
3093
Jonas Gorski3f524552013-04-14 14:11:58 +02003094 for (i = 0, j = 0; i < 13; i++) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003095 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01003096 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003097 }
3098}
3099
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003100static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003101mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
3102 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003103{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003104 struct mwl8k_cmd_update_set_aid *cmd;
3105 u16 prot_mode;
3106 int rc;
3107
3108 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3109 if (cmd == NULL)
3110 return -ENOMEM;
3111
3112 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
3113 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003114 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003115 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003116
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003117 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003118 prot_mode = MWL8K_FRAME_PROT_11G;
3119 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003120 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003121 IEEE80211_HT_OP_MODE_PROTECTION) {
3122 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
3123 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
3124 break;
3125 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
3126 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
3127 break;
3128 default:
3129 prot_mode = MWL8K_FRAME_PROT_DISABLED;
3130 break;
3131 }
3132 }
3133 cmd->protection_mode = cpu_to_le16(prot_mode);
3134
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003135 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003136
3137 rc = mwl8k_post_cmd(hw, &cmd->header);
3138 kfree(cmd);
3139
3140 return rc;
3141}
3142
3143/*
3144 * CMD_SET_RATE.
3145 */
3146struct mwl8k_cmd_set_rate {
3147 struct mwl8k_cmd_pkt header;
3148 __u8 legacy_rates[14];
3149
3150 /* Bitmap for supported MCS codes. */
3151 __u8 mcs_set[16];
3152 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003153} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003154
3155static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003156mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003157 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003158{
3159 struct mwl8k_cmd_set_rate *cmd;
3160 int rc;
3161
3162 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3163 if (cmd == NULL)
3164 return -ENOMEM;
3165
3166 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
3167 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003168 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003169 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003170
3171 rc = mwl8k_post_cmd(hw, &cmd->header);
3172 kfree(cmd);
3173
3174 return rc;
3175}
3176
3177/*
3178 * CMD_FINALIZE_JOIN.
3179 */
3180#define MWL8K_FJ_BEACON_MAXLEN 128
3181
3182struct mwl8k_cmd_finalize_join {
3183 struct mwl8k_cmd_pkt header;
3184 __le32 sleep_interval; /* Number of beacon periods to sleep */
3185 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00003186} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003187
3188static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
3189 int framelen, int dtim)
3190{
3191 struct mwl8k_cmd_finalize_join *cmd;
3192 struct ieee80211_mgmt *payload = frame;
3193 int payload_len;
3194 int rc;
3195
3196 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3197 if (cmd == NULL)
3198 return -ENOMEM;
3199
3200 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
3201 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3202 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
3203
3204 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
3205 if (payload_len < 0)
3206 payload_len = 0;
3207 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
3208 payload_len = MWL8K_FJ_BEACON_MAXLEN;
3209
3210 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
3211
3212 rc = mwl8k_post_cmd(hw, &cmd->header);
3213 kfree(cmd);
3214
3215 return rc;
3216}
3217
3218/*
3219 * CMD_SET_RTS_THRESHOLD.
3220 */
3221struct mwl8k_cmd_set_rts_threshold {
3222 struct mwl8k_cmd_pkt header;
3223 __le16 action;
3224 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003225} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003226
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003227static int
3228mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003229{
3230 struct mwl8k_cmd_set_rts_threshold *cmd;
3231 int rc;
3232
3233 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3234 if (cmd == NULL)
3235 return -ENOMEM;
3236
3237 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3238 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003239 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3240 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003241
3242 rc = mwl8k_post_cmd(hw, &cmd->header);
3243 kfree(cmd);
3244
3245 return rc;
3246}
3247
3248/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003249 * CMD_SET_SLOT.
3250 */
3251struct mwl8k_cmd_set_slot {
3252 struct mwl8k_cmd_pkt header;
3253 __le16 action;
3254 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003255} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003256
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02003257static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003258{
3259 struct mwl8k_cmd_set_slot *cmd;
3260 int rc;
3261
3262 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3263 if (cmd == NULL)
3264 return -ENOMEM;
3265
3266 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3267 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3268 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02003269 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003270
3271 rc = mwl8k_post_cmd(hw, &cmd->header);
3272 kfree(cmd);
3273
3274 return rc;
3275}
3276
3277/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003278 * CMD_SET_EDCA_PARAMS.
3279 */
3280struct mwl8k_cmd_set_edca_params {
3281 struct mwl8k_cmd_pkt header;
3282
3283 /* See MWL8K_SET_EDCA_XXX below */
3284 __le16 action;
3285
3286 /* TX opportunity in units of 32 us */
3287 __le16 txop;
3288
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003289 union {
3290 struct {
3291 /* Log exponent of max contention period: 0...15 */
3292 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003293
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003294 /* Log exponent of min contention period: 0...15 */
3295 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003296
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003297 /* Adaptive interframe spacing in units of 32us */
3298 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003299
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003300 /* TX queue to configure */
3301 __u8 txq;
3302 } ap;
3303 struct {
3304 /* Log exponent of max contention period: 0...15 */
3305 __u8 log_cw_max;
3306
3307 /* Log exponent of min contention period: 0...15 */
3308 __u8 log_cw_min;
3309
3310 /* Adaptive interframe spacing in units of 32us */
3311 __u8 aifs;
3312
3313 /* TX queue to configure */
3314 __u8 txq;
3315 } sta;
3316 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003317} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003318
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003319#define MWL8K_SET_EDCA_CW 0x01
3320#define MWL8K_SET_EDCA_TXOP 0x02
3321#define MWL8K_SET_EDCA_AIFS 0x04
3322
3323#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
3324 MWL8K_SET_EDCA_TXOP | \
3325 MWL8K_SET_EDCA_AIFS)
3326
3327static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003328mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3329 __u16 cw_min, __u16 cw_max,
3330 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003331{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003332 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003333 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003334 int rc;
3335
3336 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3337 if (cmd == NULL)
3338 return -ENOMEM;
3339
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003340 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3341 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003342 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3343 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003344 if (priv->ap_fw) {
3345 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3346 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3347 cmd->ap.aifs = aifs;
3348 cmd->ap.txq = qnum;
3349 } else {
3350 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3351 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3352 cmd->sta.aifs = aifs;
3353 cmd->sta.txq = qnum;
3354 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003355
3356 rc = mwl8k_post_cmd(hw, &cmd->header);
3357 kfree(cmd);
3358
3359 return rc;
3360}
3361
3362/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003363 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003364 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003365struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003366 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003367 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003368} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003369
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003370static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003371{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003372 struct mwl8k_priv *priv = hw->priv;
3373 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003374 int rc;
3375
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003376 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3377 if (cmd == NULL)
3378 return -ENOMEM;
3379
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003380 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003381 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003382 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003383
3384 rc = mwl8k_post_cmd(hw, &cmd->header);
3385 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01003386
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003387 if (!rc)
3388 priv->wmm_enabled = enable;
3389
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003390 return rc;
3391}
3392
3393/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003394 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003395 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003396struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003397 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003398 __le32 action;
3399 __u8 rx_antenna_map;
3400 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003401} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003402
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003403static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003404{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003405 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003406 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003407
3408 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3409 if (cmd == NULL)
3410 return -ENOMEM;
3411
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003412 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003413 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003414 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3415 cmd->rx_antenna_map = rx;
3416 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003417
3418 rc = mwl8k_post_cmd(hw, &cmd->header);
3419 kfree(cmd);
3420
3421 return rc;
3422}
3423
3424/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003425 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003426 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003427struct mwl8k_cmd_use_fixed_rate_sta {
3428 struct mwl8k_cmd_pkt header;
3429 __le32 action;
3430 __le32 allow_rate_drop;
3431 __le32 num_rates;
3432 struct {
3433 __le32 is_ht_rate;
3434 __le32 enable_retry;
3435 __le32 rate;
3436 __le32 retry_count;
3437 } rate_entry[8];
3438 __le32 rate_type;
3439 __le32 reserved1;
3440 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003441} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003442
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003443#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003444#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003445
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003446static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003447{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003448 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003449 int rc;
3450
3451 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3452 if (cmd == NULL)
3453 return -ENOMEM;
3454
3455 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3456 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003457 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3458 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003459
3460 rc = mwl8k_post_cmd(hw, &cmd->header);
3461 kfree(cmd);
3462
3463 return rc;
3464}
3465
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003466/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003467 * CMD_USE_FIXED_RATE (AP version).
3468 */
3469struct mwl8k_cmd_use_fixed_rate_ap {
3470 struct mwl8k_cmd_pkt header;
3471 __le32 action;
3472 __le32 allow_rate_drop;
3473 __le32 num_rates;
3474 struct mwl8k_rate_entry_ap {
3475 __le32 is_ht_rate;
3476 __le32 enable_retry;
3477 __le32 rate;
3478 __le32 retry_count;
3479 } rate_entry[4];
3480 u8 multicast_rate;
3481 u8 multicast_rate_type;
3482 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003483} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003484
3485static int
3486mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3487{
3488 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3489 int rc;
3490
3491 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3492 if (cmd == NULL)
3493 return -ENOMEM;
3494
3495 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3496 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3497 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3498 cmd->multicast_rate = mcast;
3499 cmd->management_rate = mgmt;
3500
3501 rc = mwl8k_post_cmd(hw, &cmd->header);
3502 kfree(cmd);
3503
3504 return rc;
3505}
3506
3507/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003508 * CMD_ENABLE_SNIFFER.
3509 */
3510struct mwl8k_cmd_enable_sniffer {
3511 struct mwl8k_cmd_pkt header;
3512 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003513} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003514
3515static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3516{
3517 struct mwl8k_cmd_enable_sniffer *cmd;
3518 int rc;
3519
3520 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3521 if (cmd == NULL)
3522 return -ENOMEM;
3523
3524 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3525 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3526 cmd->action = cpu_to_le32(!!enable);
3527
3528 rc = mwl8k_post_cmd(hw, &cmd->header);
3529 kfree(cmd);
3530
3531 return rc;
3532}
3533
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303534struct mwl8k_cmd_update_mac_addr {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003535 struct mwl8k_cmd_pkt header;
3536 union {
3537 struct {
3538 __le16 mac_type;
3539 __u8 mac_addr[ETH_ALEN];
3540 } mbss;
3541 __u8 mac_addr[ETH_ALEN];
3542 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003543} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003544
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003545#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3546#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3547#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3548#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003549
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303550static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3551 struct ieee80211_vif *vif, u8 *mac, bool set)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003552{
3553 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003554 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303555 struct mwl8k_cmd_update_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003556 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003557 int rc;
3558
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003559 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3560 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3561 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
Yogesh Ashok Powaraf458832013-01-25 16:20:12 +05303562 if (priv->ap_fw)
3563 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3564 else
3565 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003566 else
3567 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3568 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3569 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3570 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3571 else
3572 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3573 }
3574
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003575 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3576 if (cmd == NULL)
3577 return -ENOMEM;
3578
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303579 if (set)
3580 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3581 else
3582 cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3583
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003584 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3585 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003586 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003587 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3588 } else {
3589 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3590 }
3591
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003592 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003593 kfree(cmd);
3594
3595 return rc;
3596}
3597
3598/*
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05303599 * MWL8K_CMD_SET_MAC_ADDR.
3600 */
3601static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3602 struct ieee80211_vif *vif, u8 *mac)
3603{
3604 return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3605}
3606
3607/*
3608 * MWL8K_CMD_DEL_MAC_ADDR.
3609 */
3610static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3611 struct ieee80211_vif *vif, u8 *mac)
3612{
3613 return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3614}
3615
3616/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003617 * CMD_SET_RATEADAPT_MODE.
3618 */
3619struct mwl8k_cmd_set_rate_adapt_mode {
3620 struct mwl8k_cmd_pkt header;
3621 __le16 action;
3622 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003623} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003624
3625static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3626{
3627 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3628 int rc;
3629
3630 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3631 if (cmd == NULL)
3632 return -ENOMEM;
3633
3634 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3635 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3636 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3637 cmd->mode = cpu_to_le16(mode);
3638
3639 rc = mwl8k_post_cmd(hw, &cmd->header);
3640 kfree(cmd);
3641
3642 return rc;
3643}
3644
3645/*
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003646 * CMD_GET_WATCHDOG_BITMAP.
3647 */
3648struct mwl8k_cmd_get_watchdog_bitmap {
3649 struct mwl8k_cmd_pkt header;
3650 u8 bitmap;
3651} __packed;
3652
3653static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3654{
3655 struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3656 int rc;
3657
3658 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3659 if (cmd == NULL)
3660 return -ENOMEM;
3661
3662 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3663 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3664
3665 rc = mwl8k_post_cmd(hw, &cmd->header);
3666 if (!rc)
3667 *bitmap = cmd->bitmap;
3668
3669 kfree(cmd);
3670
3671 return rc;
3672}
3673
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303674#define MWL8K_WMM_QUEUE_NUMBER 3
3675
3676static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3677 u8 idx);
3678
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003679static void mwl8k_watchdog_ba_events(struct work_struct *work)
3680{
3681 int rc;
3682 u8 bitmap = 0, stream_index;
3683 struct mwl8k_ampdu_stream *streams;
3684 struct mwl8k_priv *priv =
3685 container_of(work, struct mwl8k_priv, watchdog_ba_handle);
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303686 struct ieee80211_hw *hw = priv->hw;
3687 int i;
3688 u32 status = 0;
3689
3690 mwl8k_fw_lock(hw);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003691
3692 rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3693 if (rc)
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303694 goto done;
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003695
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303696 spin_lock(&priv->stream_lock);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003697
3698 /* the bitmap is the hw queue number. Map it to the ampdu queue. */
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303699 for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
3700 if (bitmap & (1 << i)) {
3701 stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
3702 TOTAL_HW_TX_QUEUES;
3703 streams = &priv->ampdu[stream_index];
3704 if (streams->state == AMPDU_STREAM_ACTIVE) {
3705 ieee80211_stop_tx_ba_session(streams->sta,
3706 streams->tid);
3707 spin_unlock(&priv->stream_lock);
3708 mwl8k_destroy_ba(hw, stream_index);
3709 spin_lock(&priv->stream_lock);
3710 }
3711 }
3712 }
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003713
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303714 spin_unlock(&priv->stream_lock);
3715done:
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05303716 atomic_dec(&priv->watchdog_event_pending);
Yogesh Ashok Powarcfacba12013-01-03 13:24:03 +05303717 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3718 iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
3719 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3720 mwl8k_fw_unlock(hw);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003721 return;
3722}
3723
3724
3725/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003726 * CMD_BSS_START.
3727 */
3728struct mwl8k_cmd_bss_start {
3729 struct mwl8k_cmd_pkt header;
3730 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003731} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003732
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003733static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3734 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003735{
3736 struct mwl8k_cmd_bss_start *cmd;
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303737 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3738 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003739 int rc;
3740
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303741 if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
3742 return 0;
3743
3744 if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
3745 return 0;
3746
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003747 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3748 if (cmd == NULL)
3749 return -ENOMEM;
3750
3751 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3752 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3753 cmd->enable = cpu_to_le32(enable);
3754
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003755 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003756 kfree(cmd);
3757
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303758 if (!rc) {
3759 if (enable)
3760 priv->running_bsses |= (1 << mwl8k_vif->macid);
3761 else
3762 priv->running_bsses &= ~(1 << mwl8k_vif->macid);
3763 }
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003764 return rc;
3765}
3766
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05303767static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
3768{
3769 struct mwl8k_priv *priv = hw->priv;
3770 struct mwl8k_vif *mwl8k_vif, *tmp_vif;
3771 struct ieee80211_vif *vif;
3772
3773 list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
3774 vif = mwl8k_vif->vif;
3775
3776 if (!(bitmap & (1 << mwl8k_vif->macid)))
3777 continue;
3778
3779 if (vif->type == NL80211_IFTYPE_AP)
3780 mwl8k_cmd_bss_start(hw, vif, enable);
3781 }
3782}
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003783/*
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003784 * CMD_BASTREAM.
3785 */
3786
3787/*
3788 * UPSTREAM is tx direction
3789 */
3790#define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3791#define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3792
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303793enum ba_stream_action_type {
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003794 MWL8K_BA_CREATE,
3795 MWL8K_BA_UPDATE,
3796 MWL8K_BA_DESTROY,
3797 MWL8K_BA_FLUSH,
3798 MWL8K_BA_CHECK,
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05303799};
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003800
3801
3802struct mwl8k_create_ba_stream {
3803 __le32 flags;
3804 __le32 idle_thrs;
3805 __le32 bar_thrs;
3806 __le32 window_size;
3807 u8 peer_mac_addr[6];
3808 u8 dialog_token;
3809 u8 tid;
3810 u8 queue_id;
3811 u8 param_info;
3812 __le32 ba_context;
3813 u8 reset_seq_no_flag;
3814 __le16 curr_seq_no;
3815 u8 sta_src_mac_addr[6];
3816} __packed;
3817
3818struct mwl8k_destroy_ba_stream {
3819 __le32 flags;
3820 __le32 ba_context;
3821} __packed;
3822
3823struct mwl8k_cmd_bastream {
3824 struct mwl8k_cmd_pkt header;
3825 __le32 action;
3826 union {
3827 struct mwl8k_create_ba_stream create_params;
3828 struct mwl8k_destroy_ba_stream destroy_params;
3829 };
3830} __packed;
3831
3832static int
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303833mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3834 struct ieee80211_vif *vif)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003835{
3836 struct mwl8k_cmd_bastream *cmd;
3837 int rc;
3838
3839 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3840 if (cmd == NULL)
3841 return -ENOMEM;
3842
3843 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3844 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3845
3846 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3847
3848 cmd->create_params.queue_id = stream->idx;
3849 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3850 ETH_ALEN);
3851 cmd->create_params.tid = stream->tid;
3852
3853 cmd->create_params.flags =
3854 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3855 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3856
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303857 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003858
3859 kfree(cmd);
3860
3861 return rc;
3862}
3863
3864static int
3865mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303866 u8 buf_size, struct ieee80211_vif *vif)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003867{
3868 struct mwl8k_cmd_bastream *cmd;
3869 int rc;
3870
3871 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3872 if (cmd == NULL)
3873 return -ENOMEM;
3874
3875
3876 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3877 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3878
3879 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3880
3881 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3882 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3883 cmd->create_params.queue_id = stream->idx;
3884
3885 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3886 cmd->create_params.tid = stream->tid;
3887 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3888 cmd->create_params.reset_seq_no_flag = 1;
3889
3890 cmd->create_params.param_info =
3891 (stream->sta->ht_cap.ampdu_factor &
3892 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3893 ((stream->sta->ht_cap.ampdu_density << 2) &
3894 IEEE80211_HT_AMPDU_PARM_DENSITY);
3895
3896 cmd->create_params.flags =
3897 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3898 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3899
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05303900 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003901
3902 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3903 stream->sta->addr, stream->tid);
3904 kfree(cmd);
3905
3906 return rc;
3907}
3908
3909static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303910 u8 idx)
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003911{
3912 struct mwl8k_cmd_bastream *cmd;
3913
3914 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3915 if (cmd == NULL)
3916 return;
3917
3918 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3919 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3920 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3921
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303922 cmd->destroy_params.ba_context = cpu_to_le32(idx);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003923 mwl8k_post_cmd(hw, &cmd->header);
3924
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05303925 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003926
3927 kfree(cmd);
3928}
3929
3930/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003931 * CMD_SET_NEW_STN.
3932 */
3933struct mwl8k_cmd_set_new_stn {
3934 struct mwl8k_cmd_pkt header;
3935 __le16 aid;
3936 __u8 mac_addr[6];
3937 __le16 stn_id;
3938 __le16 action;
3939 __le16 rsvd;
3940 __le32 legacy_rates;
3941 __u8 ht_rates[4];
3942 __le16 cap_info;
3943 __le16 ht_capabilities_info;
3944 __u8 mac_ht_param_info;
3945 __u8 rev;
3946 __u8 control_channel;
3947 __u8 add_channel;
3948 __le16 op_mode;
3949 __le16 stbc;
3950 __u8 add_qos_info;
3951 __u8 is_qos_sta;
3952 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003953} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003954
3955#define MWL8K_STA_ACTION_ADD 0
3956#define MWL8K_STA_ACTION_REMOVE 2
3957
3958static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3959 struct ieee80211_vif *vif,
3960 struct ieee80211_sta *sta)
3961{
3962 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003963 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003964 int rc;
3965
3966 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3967 if (cmd == NULL)
3968 return -ENOMEM;
3969
3970 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3971 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3972 cmd->aid = cpu_to_le16(sta->aid);
3973 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3974 cmd->stn_id = cpu_to_le16(sta->aid);
3975 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Karl Beldan675a0b02013-03-25 16:26:57 +01003976 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003977 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3978 else
3979 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3980 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003981 if (sta->ht_cap.ht_supported) {
3982 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3983 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3984 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3985 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3986 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3987 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3988 ((sta->ht_cap.ampdu_density & 7) << 2);
3989 cmd->is_qos_sta = 1;
3990 }
3991
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003992 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003993 kfree(cmd);
3994
3995 return rc;
3996}
3997
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003998static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3999 struct ieee80211_vif *vif)
4000{
4001 struct mwl8k_cmd_set_new_stn *cmd;
4002 int rc;
4003
4004 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4005 if (cmd == NULL)
4006 return -ENOMEM;
4007
4008 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4009 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4010 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
4011
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004012 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004013 kfree(cmd);
4014
4015 return rc;
4016}
4017
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004018static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
4019 struct ieee80211_vif *vif, u8 *addr)
4020{
4021 struct mwl8k_cmd_set_new_stn *cmd;
Yogesh Ashok Powar0dd13a42013-01-08 10:16:37 +05304022 struct mwl8k_priv *priv = hw->priv;
4023 int rc, i;
4024 u8 idx;
4025
4026 spin_lock(&priv->stream_lock);
4027 /* Destroy any active ampdu streams for this sta */
4028 for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
4029 struct mwl8k_ampdu_stream *s;
4030 s = &priv->ampdu[i];
4031 if (s->state != AMPDU_NO_STREAM) {
4032 if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
4033 if (s->state == AMPDU_STREAM_ACTIVE) {
4034 idx = s->idx;
4035 spin_unlock(&priv->stream_lock);
4036 mwl8k_destroy_ba(hw, idx);
4037 spin_lock(&priv->stream_lock);
4038 } else if (s->state == AMPDU_STREAM_NEW) {
4039 mwl8k_remove_stream(hw, s);
4040 }
4041 }
4042 }
4043 }
4044
4045 spin_unlock(&priv->stream_lock);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004046
4047 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4048 if (cmd == NULL)
4049 return -ENOMEM;
4050
4051 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4052 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4053 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4054 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
4055
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004056 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004057 kfree(cmd);
4058
4059 return rc;
4060}
4061
4062/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004063 * CMD_UPDATE_ENCRYPTION.
4064 */
4065
4066#define MAX_ENCR_KEY_LENGTH 16
4067#define MIC_KEY_LENGTH 8
4068
4069struct mwl8k_cmd_update_encryption {
4070 struct mwl8k_cmd_pkt header;
4071
4072 __le32 action;
4073 __le32 reserved;
4074 __u8 mac_addr[6];
4075 __u8 encr_type;
4076
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304077} __packed;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004078
4079struct mwl8k_cmd_set_key {
4080 struct mwl8k_cmd_pkt header;
4081
4082 __le32 action;
4083 __le32 reserved;
4084 __le16 length;
4085 __le16 key_type_id;
4086 __le32 key_info;
4087 __le32 key_id;
4088 __le16 key_len;
4089 __u8 key_material[MAX_ENCR_KEY_LENGTH];
4090 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
4091 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
4092 __le16 tkip_rsc_low;
4093 __le32 tkip_rsc_high;
4094 __le16 tkip_tsc_low;
4095 __le32 tkip_tsc_high;
4096 __u8 mac_addr[6];
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304097} __packed;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004098
4099enum {
4100 MWL8K_ENCR_ENABLE,
4101 MWL8K_ENCR_SET_KEY,
4102 MWL8K_ENCR_REMOVE_KEY,
4103 MWL8K_ENCR_SET_GROUP_KEY,
4104};
4105
4106#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
4107#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
4108#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
4109#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
4110#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
4111
4112enum {
4113 MWL8K_ALG_WEP,
4114 MWL8K_ALG_TKIP,
4115 MWL8K_ALG_CCMP,
4116};
4117
4118#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
4119#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
4120#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
4121#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
4122#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
4123
4124static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
4125 struct ieee80211_vif *vif,
4126 u8 *addr,
4127 u8 encr_type)
4128{
4129 struct mwl8k_cmd_update_encryption *cmd;
4130 int rc;
4131
4132 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4133 if (cmd == NULL)
4134 return -ENOMEM;
4135
4136 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4137 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4138 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
4139 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4140 cmd->encr_type = encr_type;
4141
4142 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4143 kfree(cmd);
4144
4145 return rc;
4146}
4147
4148static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4149 u8 *addr,
4150 struct ieee80211_key_conf *key)
4151{
4152 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4153 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4154 cmd->length = cpu_to_le16(sizeof(*cmd) -
4155 offsetof(struct mwl8k_cmd_set_key, length));
4156 cmd->key_id = cpu_to_le32(key->keyidx);
4157 cmd->key_len = cpu_to_le16(key->keylen);
4158 memcpy(cmd->mac_addr, addr, ETH_ALEN);
4159
4160 switch (key->cipher) {
4161 case WLAN_CIPHER_SUITE_WEP40:
4162 case WLAN_CIPHER_SUITE_WEP104:
4163 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4164 if (key->keyidx == 0)
4165 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4166
4167 break;
4168 case WLAN_CIPHER_SUITE_TKIP:
4169 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4170 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4171 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4172 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4173 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4174 | MWL8K_KEY_FLAG_TSC_VALID);
4175 break;
4176 case WLAN_CIPHER_SUITE_CCMP:
4177 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4178 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4179 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4180 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4181 break;
4182 default:
4183 return -ENOTSUPP;
4184 }
4185
4186 return 0;
4187}
4188
4189static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4190 struct ieee80211_vif *vif,
4191 u8 *addr,
4192 struct ieee80211_key_conf *key)
4193{
4194 struct mwl8k_cmd_set_key *cmd;
4195 int rc;
4196 int keymlen;
4197 u32 action;
4198 u8 idx;
4199 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4200
4201 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4202 if (cmd == NULL)
4203 return -ENOMEM;
4204
4205 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4206 if (rc < 0)
4207 goto done;
4208
4209 idx = key->keyidx;
4210
4211 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4212 action = MWL8K_ENCR_SET_KEY;
4213 else
4214 action = MWL8K_ENCR_SET_GROUP_KEY;
4215
4216 switch (key->cipher) {
4217 case WLAN_CIPHER_SUITE_WEP40:
4218 case WLAN_CIPHER_SUITE_WEP104:
4219 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4220 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4221 sizeof(*key) + key->keylen);
4222 mwl8k_vif->wep_key_conf[idx].enabled = 1;
4223 }
4224
Yogesh Ashok Powar9b571e22011-05-04 17:22:16 +05304225 keymlen = key->keylen;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004226 action = MWL8K_ENCR_SET_KEY;
4227 break;
4228 case WLAN_CIPHER_SUITE_TKIP:
4229 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4230 break;
4231 case WLAN_CIPHER_SUITE_CCMP:
4232 keymlen = key->keylen;
4233 break;
4234 default:
4235 rc = -ENOTSUPP;
4236 goto done;
4237 }
4238
4239 memcpy(cmd->key_material, key->key, keymlen);
4240 cmd->action = cpu_to_le32(action);
4241
4242 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4243done:
4244 kfree(cmd);
4245
4246 return rc;
4247}
4248
4249static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4250 struct ieee80211_vif *vif,
4251 u8 *addr,
4252 struct ieee80211_key_conf *key)
4253{
4254 struct mwl8k_cmd_set_key *cmd;
4255 int rc;
4256 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4257
4258 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4259 if (cmd == NULL)
4260 return -ENOMEM;
4261
4262 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4263 if (rc < 0)
4264 goto done;
4265
4266 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
Dan Carpenterd981e052012-01-17 10:33:31 +03004267 key->cipher == WLAN_CIPHER_SUITE_WEP104)
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004268 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4269
4270 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4271
4272 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4273done:
4274 kfree(cmd);
4275
4276 return rc;
4277}
4278
4279static int mwl8k_set_key(struct ieee80211_hw *hw,
4280 enum set_key_cmd cmd_param,
4281 struct ieee80211_vif *vif,
4282 struct ieee80211_sta *sta,
4283 struct ieee80211_key_conf *key)
4284{
4285 int rc = 0;
4286 u8 encr_type;
4287 u8 *addr;
4288 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Yogesh Ashok Powar751930c2013-01-25 16:20:28 +05304289 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004290
Yogesh Ashok Powar751930c2013-01-25 16:20:28 +05304291 if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004292 return -EOPNOTSUPP;
4293
4294 if (sta == NULL)
Yogesh Ashok Powarff7e9f92012-01-27 16:08:27 +05304295 addr = vif->addr;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004296 else
4297 addr = sta->addr;
4298
4299 if (cmd_param == SET_KEY) {
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004300 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4301 if (rc)
4302 goto out;
4303
4304 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4305 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4306 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4307 else
4308 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4309
4310 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4311 encr_type);
4312 if (rc)
4313 goto out;
4314
4315 mwl8k_vif->is_hw_crypto_enabled = true;
4316
4317 } else {
4318 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4319
4320 if (rc)
4321 goto out;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004322 }
4323out:
4324 return rc;
4325}
4326
4327/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004328 * CMD_UPDATE_STADB.
4329 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004330struct ewc_ht_info {
4331 __le16 control1;
4332 __le16 control2;
4333 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004334} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004335
4336struct peer_capability_info {
4337 /* Peer type - AP vs. STA. */
4338 __u8 peer_type;
4339
4340 /* Basic 802.11 capabilities from assoc resp. */
4341 __le16 basic_caps;
4342
4343 /* Set if peer supports 802.11n high throughput (HT). */
4344 __u8 ht_support;
4345
4346 /* Valid if HT is supported. */
4347 __le16 ht_caps;
4348 __u8 extended_ht_caps;
4349 struct ewc_ht_info ewc_info;
4350
4351 /* Legacy rate table. Intersection of our rates and peer rates. */
4352 __u8 legacy_rates[12];
4353
4354 /* HT rate table. Intersection of our rates and peer rates. */
4355 __u8 ht_rates[16];
4356 __u8 pad[16];
4357
4358 /* If set, interoperability mode, no proprietary extensions. */
4359 __u8 interop;
4360 __u8 pad2;
4361 __u8 station_id;
4362 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004363} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01004364
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004365struct mwl8k_cmd_update_stadb {
4366 struct mwl8k_cmd_pkt header;
4367
4368 /* See STADB_ACTION_TYPE */
4369 __le32 action;
4370
4371 /* Peer MAC address */
4372 __u8 peer_addr[ETH_ALEN];
4373
4374 __le32 reserved;
4375
4376 /* Peer info - valid during add/update. */
4377 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00004378} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004379
Lennert Buytenheka6804002010-01-04 21:55:42 +01004380#define MWL8K_STA_DB_MODIFY_ENTRY 1
4381#define MWL8K_STA_DB_DEL_ENTRY 2
4382
4383/* Peer Entry flags - used to define the type of the peer node */
4384#define MWL8K_PEER_TYPE_ACCESSPOINT 2
4385
4386static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004387 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004388 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004389{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004390 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004391 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004392 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004393 int rc;
4394
4395 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4396 if (cmd == NULL)
4397 return -ENOMEM;
4398
4399 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4400 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01004401 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004402 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004403
Lennert Buytenheka6804002010-01-04 21:55:42 +01004404 p = &cmd->peer_info;
4405 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4406 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004407 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04004408 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004409 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
4410 ((sta->ht_cap.ampdu_density & 7) << 2);
Karl Beldan675a0b02013-03-25 16:26:57 +01004411 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004412 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
4413 else
4414 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4415 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004416 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004417 p->interop = 1;
4418 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004419
Lennert Buytenheka6804002010-01-04 21:55:42 +01004420 rc = mwl8k_post_cmd(hw, &cmd->header);
Nickolai Zeldovichc4f74d32013-01-06 20:27:22 -05004421 if (!rc)
4422 rc = p->station_id;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004423 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004424
Nickolai Zeldovichc4f74d32013-01-06 20:27:22 -05004425 return rc;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004426}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004427
Lennert Buytenheka6804002010-01-04 21:55:42 +01004428static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4429 struct ieee80211_vif *vif, u8 *addr)
4430{
4431 struct mwl8k_cmd_update_stadb *cmd;
4432 int rc;
4433
4434 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4435 if (cmd == NULL)
4436 return -ENOMEM;
4437
4438 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4439 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4440 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4441 memcpy(cmd->peer_addr, addr, ETH_ALEN);
4442
4443 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004444 kfree(cmd);
4445
4446 return rc;
4447}
4448
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004449
4450/*
4451 * Interrupt handling.
4452 */
4453static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4454{
4455 struct ieee80211_hw *hw = dev_id;
4456 struct mwl8k_priv *priv = hw->priv;
4457 u32 status;
4458
4459 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004460 if (!status)
4461 return IRQ_NONE;
4462
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004463 if (status & MWL8K_A2H_INT_TX_DONE) {
4464 status &= ~MWL8K_A2H_INT_TX_DONE;
4465 tasklet_schedule(&priv->poll_tx_task);
4466 }
4467
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004468 if (status & MWL8K_A2H_INT_RX_READY) {
4469 status &= ~MWL8K_A2H_INT_RX_READY;
4470 tasklet_schedule(&priv->poll_rx_task);
4471 }
4472
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004473 if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05304474 iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
4475 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4476
4477 atomic_inc(&priv->watchdog_event_pending);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004478 status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4479 ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4480 }
4481
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004482 if (status)
4483 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004484
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004485 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004486 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004487 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004488 }
4489
4490 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004491 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004492 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004493 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004494 }
4495
4496 return IRQ_HANDLED;
4497}
4498
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004499static void mwl8k_tx_poll(unsigned long data)
4500{
4501 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4502 struct mwl8k_priv *priv = hw->priv;
4503 int limit;
4504 int i;
4505
4506 limit = 32;
4507
4508 spin_lock_bh(&priv->tx_lock);
4509
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004510 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004511 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4512
4513 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4514 complete(priv->tx_wait);
4515 priv->tx_wait = NULL;
4516 }
4517
4518 spin_unlock_bh(&priv->tx_lock);
4519
4520 if (limit) {
4521 writel(~MWL8K_A2H_INT_TX_DONE,
4522 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4523 } else {
4524 tasklet_schedule(&priv->poll_tx_task);
4525 }
4526}
4527
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004528static void mwl8k_rx_poll(unsigned long data)
4529{
4530 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4531 struct mwl8k_priv *priv = hw->priv;
4532 int limit;
4533
4534 limit = 32;
4535 limit -= rxq_process(hw, 0, limit);
4536 limit -= rxq_refill(hw, 0, limit);
4537
4538 if (limit) {
4539 writel(~MWL8K_A2H_INT_RX_READY,
4540 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4541 } else {
4542 tasklet_schedule(&priv->poll_rx_task);
4543 }
4544}
4545
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004546
4547/*
4548 * Core driver operations.
4549 */
Thomas Huehn36323f82012-07-23 21:33:42 +02004550static void mwl8k_tx(struct ieee80211_hw *hw,
4551 struct ieee80211_tx_control *control,
4552 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004553{
4554 struct mwl8k_priv *priv = hw->priv;
4555 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004556
Lennert Buytenhek9189c102010-01-12 13:47:32 +01004557 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004558 wiphy_debug(hw->wiphy,
4559 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004560 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01004561 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004562 }
4563
Thomas Huehn36323f82012-07-23 21:33:42 +02004564 mwl8k_txq_xmit(hw, index, control->sta, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004565}
4566
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004567static int mwl8k_start(struct ieee80211_hw *hw)
4568{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004569 struct mwl8k_priv *priv = hw->priv;
4570 int rc;
4571
Joe Perchesa0607fd2009-11-18 23:29:17 -08004572 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004573 IRQF_SHARED, MWL8K_NAME, hw);
4574 if (rc) {
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304575 priv->irq = -1;
Joe Perches5db55842010-08-11 19:11:19 -07004576 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004577 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004578 }
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304579 priv->irq = priv->pdev->irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004580
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004581 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004582 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004583 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004584
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004585 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004586 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam12488e02011-04-08 14:38:27 +05304587 iowrite32(MWL8K_A2H_EVENTS,
4588 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004589
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004590 rc = mwl8k_fw_lock(hw);
4591 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004592 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004593
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004594 if (!priv->ap_fw) {
4595 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004596 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004597
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004598 if (!rc)
4599 rc = mwl8k_cmd_set_pre_scan(hw);
4600
4601 if (!rc)
4602 rc = mwl8k_cmd_set_post_scan(hw,
4603 "\x00\x00\x00\x00\x00\x00");
4604 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004605
4606 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004607 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004608
4609 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004610 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004611
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004612 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004613 }
4614
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004615 if (rc) {
4616 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4617 free_irq(priv->pdev->irq, hw);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304618 priv->irq = -1;
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004619 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004620 tasklet_disable(&priv->poll_rx_task);
Nishant Sarmukadam4850b6d2013-01-08 12:10:53 +05304621 } else {
4622 ieee80211_wake_queues(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004623 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004624
4625 return rc;
4626}
4627
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004628static void mwl8k_stop(struct ieee80211_hw *hw)
4629{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004630 struct mwl8k_priv *priv = hw->priv;
4631 int i;
4632
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304633 if (!priv->hw_restart_in_progress)
4634 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004635
4636 ieee80211_stop_queues(hw);
4637
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004638 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004639 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05304640 if (priv->irq != -1) {
4641 free_irq(priv->pdev->irq, hw);
4642 priv->irq = -1;
4643 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004644
4645 /* Stop finalize join worker */
4646 cancel_work_sync(&priv->finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004647 cancel_work_sync(&priv->watchdog_ba_handle);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004648 if (priv->beacon_skb != NULL)
4649 dev_kfree_skb(priv->beacon_skb);
4650
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004651 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004652 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004653 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004654
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004655 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004656 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004657 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004658}
4659
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004660static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4661
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004662static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004663 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004664{
4665 struct mwl8k_priv *priv = hw->priv;
4666 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004667 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004668 int macid, rc;
4669 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004670
4671 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004672 * Reject interface creation if sniffer mode is active, as
4673 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004674 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004675 */
4676 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004677 wiphy_info(hw->wiphy,
4678 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004679 return -EINVAL;
4680 }
4681
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004682 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004683 switch (vif->type) {
4684 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004685 if (!priv->ap_fw && di->fw_image_ap) {
4686 /* we must load the ap fw to meet this request */
4687 if (!list_empty(&priv->vif_list))
4688 return -EBUSY;
4689 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4690 if (rc)
4691 return rc;
4692 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004693 macids_supported = priv->ap_macids_supported;
4694 break;
4695 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004696 if (priv->ap_fw && di->fw_image_sta) {
Yogesh Ashok Poward59c1cf2013-01-25 16:20:03 +05304697 if (!list_empty(&priv->vif_list)) {
4698 wiphy_warn(hw->wiphy, "AP interface is running.\n"
4699 "Adding STA interface for WDS");
4700 } else {
4701 /* we must load the sta fw to
4702 * meet this request.
4703 */
4704 rc = mwl8k_reload_firmware(hw,
4705 di->fw_image_sta);
4706 if (rc)
4707 return rc;
4708 }
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004709 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004710 macids_supported = priv->sta_macids_supported;
4711 break;
4712 default:
4713 return -EINVAL;
4714 }
4715
4716 macid = ffs(macids_supported & ~priv->macids_used);
4717 if (!macid--)
4718 return -EBUSY;
4719
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004720 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01004721 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004722 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004723 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004724 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004725 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004726 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4727 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004728
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004729 /* Set the mac address. */
4730 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4731
Yogesh Ashok Poward994a1c2013-01-25 16:20:35 +05304732 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004733 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4734
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004735 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004736 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004737
4738 return 0;
4739}
4740
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304741static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4742{
4743 /* Has ieee80211_restart_hw re-added the removed interfaces? */
4744 if (!priv->macids_used)
4745 return;
4746
4747 priv->macids_used &= ~(1 << vif->macid);
4748 list_del(&vif->list);
4749}
4750
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004751static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004752 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004753{
4754 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004755 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004756
Yogesh Ashok Poward994a1c2013-01-25 16:20:35 +05304757 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004758 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4759
Yogesh Ashok Powar197a4e42012-02-01 16:19:54 +05304760 mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004761
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05304762 mwl8k_remove_vif(priv, mwl8k_vif);
4763}
4764
4765static void mwl8k_hw_restart_work(struct work_struct *work)
4766{
4767 struct mwl8k_priv *priv =
4768 container_of(work, struct mwl8k_priv, fw_reload);
4769 struct ieee80211_hw *hw = priv->hw;
4770 struct mwl8k_device_info *di;
4771 int rc;
4772
4773 /* If some command is waiting for a response, clear it */
4774 if (priv->hostcmd_wait != NULL) {
4775 complete(priv->hostcmd_wait);
4776 priv->hostcmd_wait = NULL;
4777 }
4778
4779 priv->hw_restart_owner = current;
4780 di = priv->device_info;
4781 mwl8k_fw_lock(hw);
4782
4783 if (priv->ap_fw)
4784 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4785 else
4786 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4787
4788 if (rc)
4789 goto fail;
4790
4791 priv->hw_restart_owner = NULL;
4792 priv->hw_restart_in_progress = false;
4793
4794 /*
4795 * This unlock will wake up the queues and
4796 * also opens the command path for other
4797 * commands
4798 */
4799 mwl8k_fw_unlock(hw);
4800
4801 ieee80211_restart_hw(hw);
4802
4803 wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
4804
4805 return;
4806fail:
4807 mwl8k_fw_unlock(hw);
4808
4809 wiphy_err(hw->wiphy, "Firmware restart failed\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004810}
4811
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004812static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004813{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004814 struct ieee80211_conf *conf = &hw->conf;
4815 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004816 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004817
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004818 rc = mwl8k_fw_lock(hw);
4819 if (rc)
4820 return rc;
4821
Jonas Gorskife21bb02013-03-25 16:39:54 +01004822 if (conf->flags & IEEE80211_CONF_IDLE)
4823 rc = mwl8k_cmd_radio_disable(hw);
4824 else
4825 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004826 if (rc)
4827 goto out;
4828
Yogesh Ashok Powar0f4316b2013-01-25 16:17:45 +05304829 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
4830 rc = mwl8k_cmd_set_rf_channel(hw, conf);
4831 if (rc)
4832 goto out;
4833 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004834
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004835 if (conf->power_level > 18)
4836 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004837
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004838 if (priv->ap_fw) {
Nishant Sarmukadam03217082011-04-05 14:18:09 +05304839
4840 if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
4841 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4842 if (rc)
4843 goto out;
4844 }
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004845
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08004846
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004847 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004848 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4849 if (rc)
4850 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004851 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4852 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004853
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004854out:
4855 mwl8k_fw_unlock(hw);
4856
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004857 return rc;
4858}
4859
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004860static void
4861mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4862 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004863{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004864 struct mwl8k_priv *priv = hw->priv;
Yogesh Ashok Powarba30c4a2011-04-09 00:25:37 +05304865 u32 ap_legacy_rates = 0;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004866 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004867 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004868
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004869 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004870 return;
4871
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004872 /*
4873 * No need to capture a beacon if we're no longer associated.
4874 */
4875 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4876 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004877
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004878 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004879 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004880 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004881 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004882 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004883
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004884 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004885
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004886 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004887 if (ap == NULL) {
4888 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004889 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004890 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004891
Karl Beldan675a0b02013-03-25 16:26:57 +01004892 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ) {
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004893 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4894 } else {
4895 ap_legacy_rates =
4896 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4897 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004898 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004899
4900 rcu_read_unlock();
4901 }
4902
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304903 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4904 !priv->ap_fw) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004905 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004906 if (rc)
4907 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004908
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004909 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004910 if (rc)
4911 goto out;
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304912 } else {
4913 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc &&
4914 priv->ap_fw) {
4915 int idx;
4916 int rate;
4917
4918 /* Use AP firmware specific rate command.
4919 */
4920 idx = ffs(vif->bss_conf.basic_rates);
4921 if (idx)
4922 idx--;
4923
Karl Beldan675a0b02013-03-25 16:26:57 +01004924 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304925 rate = mwl8k_rates_24[idx].hw_value;
4926 else
4927 rate = mwl8k_rates_50[idx].hw_value;
4928
4929 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4930 }
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004931 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004932
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004933 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004934 rc = mwl8k_set_radio_preamble(hw,
4935 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004936 if (rc)
4937 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004938 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004939
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304940 if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004941 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004942 if (rc)
4943 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004944 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004945
Yogesh Ashok Powardcee7432013-01-25 16:20:50 +05304946 if (vif->bss_conf.assoc && !priv->ap_fw &&
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004947 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4948 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004949 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004950 if (rc)
4951 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004952 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004953
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004954 if (vif->bss_conf.assoc &&
4955 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004956 /*
4957 * Finalize the join. Tell rx handler to process
4958 * next beacon from our BSSID.
4959 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004960 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004961 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004962 }
4963
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004964out:
4965 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004966}
4967
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004968static void
4969mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4970 struct ieee80211_bss_conf *info, u32 changed)
4971{
4972 int rc;
4973
4974 if (mwl8k_fw_lock(hw))
4975 return;
4976
4977 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4978 rc = mwl8k_set_radio_preamble(hw,
4979 vif->bss_conf.use_short_preamble);
4980 if (rc)
4981 goto out;
4982 }
4983
4984 if (changed & BSS_CHANGED_BASIC_RATES) {
4985 int idx;
4986 int rate;
4987
4988 /*
4989 * Use lowest supported basic rate for multicasts
4990 * and management frames (such as probe responses --
4991 * beacons will always go out at 1 Mb/s).
4992 */
4993 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004994 if (idx)
4995 idx--;
4996
Karl Beldan675a0b02013-03-25 16:26:57 +01004997 if (hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004998 rate = mwl8k_rates_24[idx].hw_value;
4999 else
5000 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005001
5002 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
5003 }
5004
5005 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
5006 struct sk_buff *skb;
5007
5008 skb = ieee80211_beacon_get(hw, vif);
5009 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01005010 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005011 kfree_skb(skb);
5012 }
5013 }
5014
5015 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01005016 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005017
5018out:
5019 mwl8k_fw_unlock(hw);
5020}
5021
5022static void
5023mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5024 struct ieee80211_bss_conf *info, u32 changed)
5025{
Yogesh Ashok Powar41bf9112013-01-25 16:20:42 +05305026 if (vif->type == NL80211_IFTYPE_STATION)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005027 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
Yogesh Ashok Powar41bf9112013-01-25 16:20:42 +05305028 if (vif->type == NL80211_IFTYPE_AP)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01005029 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
5030}
5031
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005032static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00005033 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005034{
5035 struct mwl8k_cmd_pkt *cmd;
5036
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005037 /*
5038 * Synthesize and return a command packet that programs the
5039 * hardware multicast address filter. At this point we don't
5040 * know whether FIF_ALLMULTI is being requested, but if it is,
5041 * we'll end up throwing this packet away and creating a new
5042 * one in mwl8k_configure_filter().
5043 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00005044 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02005045
5046 return (unsigned long)cmd;
5047}
5048
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005049static int
5050mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
5051 unsigned int changed_flags,
5052 unsigned int *total_flags)
5053{
5054 struct mwl8k_priv *priv = hw->priv;
5055
5056 /*
5057 * Hardware sniffer mode is mutually exclusive with STA
5058 * operation, so refuse to enable sniffer mode if a STA
5059 * interface is active.
5060 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005061 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005062 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07005063 wiphy_info(hw->wiphy,
5064 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005065 return 0;
5066 }
5067
5068 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005069 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005070 return 0;
5071 priv->sniffer_enabled = true;
5072 }
5073
5074 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
5075 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
5076 FIF_OTHER_BSS;
5077
5078 return 1;
5079}
5080
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005081static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
5082{
5083 if (!list_empty(&priv->vif_list))
5084 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
5085
5086 return NULL;
5087}
5088
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005089static void mwl8k_configure_filter(struct ieee80211_hw *hw,
5090 unsigned int changed_flags,
5091 unsigned int *total_flags,
5092 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005093{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005094 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005095 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
5096
5097 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02005098 * AP firmware doesn't allow fine-grained control over
5099 * the receive filter.
5100 */
5101 if (priv->ap_fw) {
5102 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5103 kfree(cmd);
5104 return;
5105 }
5106
5107 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005108 * Enable hardware sniffer mode if FIF_CONTROL or
5109 * FIF_OTHER_BSS is requested.
5110 */
5111 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
5112 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
5113 kfree(cmd);
5114 return;
5115 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005116
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005117 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005118 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005119
Lennert Buytenhek90852f72010-01-02 10:31:42 +01005120 if (mwl8k_fw_lock(hw)) {
5121 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005122 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01005123 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005124
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005125 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005126 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02005127 priv->sniffer_enabled = false;
5128 }
5129
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005130 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005131 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
5132 /*
5133 * Disable the BSS filter.
5134 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005135 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005136 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005137 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01005138 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02005139
Lennert Buytenhek77165d82009-10-22 20:19:53 +02005140 /*
5141 * Enable the BSS filter.
5142 *
5143 * If there is an active STA interface, use that
5144 * interface's BSSID, otherwise use a dummy one
5145 * (where the OUI part needs to be nonzero for
5146 * the BSSID to be accepted by POST_SCAN).
5147 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005148 mwl8k_vif = mwl8k_first_vif(priv);
5149 if (mwl8k_vif != NULL)
5150 bssid = mwl8k_vif->vif->bss_conf.bssid;
5151 else
5152 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02005153
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005154 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005155 }
5156 }
5157
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005158 /*
5159 * If FIF_ALLMULTI is being requested, throw away the command
5160 * packet that ->prepare_multicast() built and replace it with
5161 * a command packet that enables reception of all multicast
5162 * packets.
5163 */
5164 if (*total_flags & FIF_ALLMULTI) {
5165 kfree(cmd);
Jiri Pirko22bedad32010-04-01 21:22:57 +00005166 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02005167 }
5168
5169 if (cmd != NULL) {
5170 mwl8k_post_cmd(hw, cmd);
5171 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005172 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02005173
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02005174 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005175}
5176
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005177static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5178{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01005179 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005180}
5181
Johannes Berg4a6967b2010-02-19 19:18:37 +01005182static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5183 struct ieee80211_vif *vif,
5184 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005185{
5186 struct mwl8k_priv *priv = hw->priv;
5187
Johannes Berg4a6967b2010-02-19 19:18:37 +01005188 if (priv->ap_fw)
5189 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5190 else
5191 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5192}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005193
Johannes Berg4a6967b2010-02-19 19:18:37 +01005194static int mwl8k_sta_add(struct ieee80211_hw *hw,
5195 struct ieee80211_vif *vif,
5196 struct ieee80211_sta *sta)
5197{
5198 struct mwl8k_priv *priv = hw->priv;
5199 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005200 int i;
5201 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5202 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005203
Johannes Berg4a6967b2010-02-19 19:18:37 +01005204 if (!priv->ap_fw) {
5205 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5206 if (ret >= 0) {
5207 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadam170335432011-03-17 11:58:48 -07005208 if (sta->ht_cap.ht_supported)
5209 MWL8K_STA(sta)->is_ampdu_allowed = true;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005210 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005211 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01005212
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08005213 } else {
5214 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005215 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01005216
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08005217 for (i = 0; i < NUM_WEP_KEYS; i++) {
5218 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5219 if (mwl8k_vif->wep_key_conf[i].enabled)
5220 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5221 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005222 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01005223}
5224
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005225static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5226 struct ieee80211_vif *vif, u16 queue,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005227 const struct ieee80211_tx_queue_params *params)
5228{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005229 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005230 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005231
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005232 rc = mwl8k_fw_lock(hw);
5233 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005234 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005235 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5236
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005237 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005238 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005239
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005240 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005241 int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005242 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005243 params->cw_min,
5244 params->cw_max,
5245 params->aifs,
5246 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08005247 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005248
5249 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005250 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02005251
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005252 return rc;
5253}
5254
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005255static int mwl8k_get_stats(struct ieee80211_hw *hw,
5256 struct ieee80211_low_level_stats *stats)
5257{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01005258 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005259}
5260
John W. Linville0d462bb2010-07-28 14:04:24 -04005261static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5262 struct survey_info *survey)
5263{
5264 struct mwl8k_priv *priv = hw->priv;
5265 struct ieee80211_conf *conf = &hw->conf;
5266
5267 if (idx != 0)
5268 return -ENOENT;
5269
Karl Beldan675a0b02013-03-25 16:26:57 +01005270 survey->channel = conf->chandef.chan;
John W. Linville0d462bb2010-07-28 14:04:24 -04005271 survey->filled = SURVEY_INFO_NOISE_DBM;
5272 survey->noise = priv->noise;
5273
5274 return 0;
5275}
5276
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005277#define MAX_AMPDU_ATTEMPTS 5
5278
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005279static int
5280mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5281 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01005282 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5283 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005284{
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005285
5286 int i, rc = 0;
5287 struct mwl8k_priv *priv = hw->priv;
5288 struct mwl8k_ampdu_stream *stream;
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305289 u8 *addr = sta->addr, idx;
Yogesh Ashok Powarfd712f52012-11-06 19:22:35 +05305290 struct mwl8k_sta *sta_info = MWL8K_STA(sta);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005291
5292 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
5293 return -ENOTSUPP;
5294
5295 spin_lock(&priv->stream_lock);
5296 stream = mwl8k_lookup_stream(hw, addr, tid);
5297
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005298 switch (action) {
5299 case IEEE80211_AMPDU_RX_START:
5300 case IEEE80211_AMPDU_RX_STOP:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005301 break;
5302 case IEEE80211_AMPDU_TX_START:
5303 /* By the time we get here the hw queues may contain outgoing
5304 * packets for this RA/TID that are not part of this BA
5305 * session. The hw will assign sequence numbers to these
5306 * packets as they go out. So if we query the hw for its next
5307 * sequence number and use that for the SSN here, it may end up
5308 * being wrong, which will lead to sequence number mismatch at
5309 * the recipient. To avoid this, we reset the sequence number
5310 * to O for the first MPDU in this BA stream.
5311 */
5312 *ssn = 0;
5313 if (stream == NULL) {
5314 /* This means that somebody outside this driver called
5315 * ieee80211_start_tx_ba_session. This is unexpected
5316 * because we do our own rate control. Just warn and
5317 * move on.
5318 */
5319 wiphy_warn(hw->wiphy, "Unexpected call to %s. "
5320 "Proceeding anyway.\n", __func__);
5321 stream = mwl8k_add_stream(hw, sta, tid);
5322 }
5323 if (stream == NULL) {
5324 wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5325 rc = -EBUSY;
5326 break;
5327 }
5328 stream->state = AMPDU_STREAM_IN_PROGRESS;
5329
5330 /* Release the lock before we do the time consuming stuff */
5331 spin_unlock(&priv->stream_lock);
5332 for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
Yogesh Ashok Powarfd712f52012-11-06 19:22:35 +05305333
5334 /* Check if link is still valid */
5335 if (!sta_info->is_ampdu_allowed) {
5336 spin_lock(&priv->stream_lock);
5337 mwl8k_remove_stream(hw, stream);
5338 spin_unlock(&priv->stream_lock);
5339 return -EBUSY;
5340 }
5341
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05305342 rc = mwl8k_check_ba(hw, stream, vif);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005343
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305344 /* If HW restart is in progress mwl8k_post_cmd will
5345 * return -EBUSY. Avoid retrying mwl8k_check_ba in
5346 * such cases
5347 */
5348 if (!rc || rc == -EBUSY)
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005349 break;
5350 /*
5351 * HW queues take time to be flushed, give them
5352 * sufficient time
5353 */
5354
5355 msleep(1000);
5356 }
5357 spin_lock(&priv->stream_lock);
5358 if (rc) {
5359 wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5360 " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5361 mwl8k_remove_stream(hw, stream);
5362 rc = -EBUSY;
5363 break;
5364 }
5365 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
5366 break;
Johannes Berg18b559d2012-07-18 13:51:25 +02005367 case IEEE80211_AMPDU_TX_STOP_CONT:
5368 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5369 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305370 if (stream) {
5371 if (stream->state == AMPDU_STREAM_ACTIVE) {
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305372 idx = stream->idx;
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305373 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305374 mwl8k_destroy_ba(hw, idx);
Yogesh Ashok Powareca107f2011-12-20 11:37:08 +05305375 spin_lock(&priv->stream_lock);
5376 }
5377 mwl8k_remove_stream(hw, stream);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005378 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005379 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5380 break;
5381 case IEEE80211_AMPDU_TX_OPERATIONAL:
5382 BUG_ON(stream == NULL);
5383 BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5384 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powarf95275c2012-11-08 19:10:44 +05305385 rc = mwl8k_create_ba(hw, stream, buf_size, vif);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005386 spin_lock(&priv->stream_lock);
5387 if (!rc)
5388 stream->state = AMPDU_STREAM_ACTIVE;
5389 else {
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305390 idx = stream->idx;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005391 spin_unlock(&priv->stream_lock);
Yogesh Ashok Powar07f6dda2013-01-03 13:23:33 +05305392 mwl8k_destroy_ba(hw, idx);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005393 spin_lock(&priv->stream_lock);
5394 wiphy_debug(hw->wiphy,
5395 "Failed adding stream for sta %pM tid %d\n",
5396 addr, tid);
5397 mwl8k_remove_stream(hw, stream);
5398 }
5399 break;
5400
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005401 default:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005402 rc = -ENOTSUPP;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005403 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07005404
5405 spin_unlock(&priv->stream_lock);
5406 return rc;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005407}
5408
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005409static const struct ieee80211_ops mwl8k_ops = {
5410 .tx = mwl8k_tx,
5411 .start = mwl8k_start,
5412 .stop = mwl8k_stop,
5413 .add_interface = mwl8k_add_interface,
5414 .remove_interface = mwl8k_remove_interface,
5415 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005416 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02005417 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005418 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08005419 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005420 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01005421 .sta_add = mwl8k_sta_add,
5422 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005423 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005424 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04005425 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01005426 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005427};
5428
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005429static void mwl8k_finalize_join_worker(struct work_struct *work)
5430{
5431 struct mwl8k_priv *priv =
5432 container_of(work, struct mwl8k_priv, finalize_join_worker);
5433 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01005434 struct ieee80211_mgmt *mgmt = (void *)skb->data;
5435 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5436 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5437 mgmt->u.beacon.variable, len);
5438 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005439
Johannes Berg56007a02010-01-26 14:19:52 +01005440 if (tim && tim[1] >= 2)
5441 dtim_period = tim[3];
5442
5443 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01005444
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005445 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005446 priv->beacon_skb = NULL;
5447}
5448
John W. Linvillebcb628d2009-11-06 16:40:16 -05005449enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005450 MWL8363 = 0,
5451 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05005452 MWL8366,
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305453 MWL8764,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02005454};
5455
Yogesh Ashok Powarc2f2e202013-01-03 13:24:44 +05305456#define MWL8K_8366_AP_FW_API 3
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005457#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5458#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5459
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305460#define MWL8K_8764_AP_FW_API 1
5461#define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
5462#define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
5463
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005464static struct mwl8k_device_info mwl8k_info_tbl[] = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005465 [MWL8363] = {
5466 .part_name = "88w8363",
5467 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005468 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005469 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01005470 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05005471 .part_name = "88w8687",
5472 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005473 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05005474 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01005475 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05005476 .part_name = "88w8366",
5477 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005478 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005479 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5480 .fw_api_ap = MWL8K_8366_AP_FW_API,
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305481 .ap_rxd_ops = &rxd_ap_ops,
5482 },
5483 [MWL8764] = {
5484 .part_name = "88w8764",
5485 .fw_image_ap = MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
5486 .fw_api_ap = MWL8K_8764_AP_FW_API,
5487 .ap_rxd_ops = &rxd_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05005488 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005489};
5490
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01005491MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5492MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5493MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5494MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5495MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5496MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08005497MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01005498
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005499static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01005500 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01005501 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5502 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05005503 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5504 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5505 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Jonas Gorskifc5bc162013-02-05 16:59:17 +01005506 { PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
5507 { PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01005508 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
Nishant Sarmukadamd926dc72013-03-01 17:12:45 +05305509 { PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05005510 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005511};
5512MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5513
Brian Cavagnolo99020472010-11-12 17:23:52 -08005514static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5515{
5516 int rc;
5517 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5518 "Trying alternative firmware %s\n", pci_name(priv->pdev),
5519 priv->fw_pref, priv->fw_alt);
5520 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5521 if (rc) {
5522 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5523 pci_name(priv->pdev), priv->fw_alt);
5524 return rc;
5525 }
5526 return 0;
5527}
5528
5529static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5530static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5531{
5532 struct mwl8k_priv *priv = context;
5533 struct mwl8k_device_info *di = priv->device_info;
5534 int rc;
5535
5536 switch (priv->fw_state) {
5537 case FW_STATE_INIT:
5538 if (!fw) {
5539 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5540 pci_name(priv->pdev), di->helper_image);
5541 goto fail;
5542 }
5543 priv->fw_helper = fw;
5544 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5545 true);
5546 if (rc && priv->fw_alt) {
5547 rc = mwl8k_request_alt_fw(priv);
5548 if (rc)
5549 goto fail;
5550 priv->fw_state = FW_STATE_LOADING_ALT;
5551 } else if (rc)
5552 goto fail;
5553 else
5554 priv->fw_state = FW_STATE_LOADING_PREF;
5555 break;
5556
5557 case FW_STATE_LOADING_PREF:
5558 if (!fw) {
5559 if (priv->fw_alt) {
5560 rc = mwl8k_request_alt_fw(priv);
5561 if (rc)
5562 goto fail;
5563 priv->fw_state = FW_STATE_LOADING_ALT;
5564 } else
5565 goto fail;
5566 } else {
5567 priv->fw_ucode = fw;
5568 rc = mwl8k_firmware_load_success(priv);
5569 if (rc)
5570 goto fail;
5571 else
5572 complete(&priv->firmware_loading_complete);
5573 }
5574 break;
5575
5576 case FW_STATE_LOADING_ALT:
5577 if (!fw) {
5578 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5579 pci_name(priv->pdev), di->helper_image);
5580 goto fail;
5581 }
5582 priv->fw_ucode = fw;
5583 rc = mwl8k_firmware_load_success(priv);
5584 if (rc)
5585 goto fail;
5586 else
5587 complete(&priv->firmware_loading_complete);
5588 break;
5589
5590 default:
5591 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5592 MWL8K_NAME, priv->fw_state);
5593 BUG_ON(1);
5594 }
5595
5596 return;
5597
5598fail:
5599 priv->fw_state = FW_STATE_ERROR;
5600 complete(&priv->firmware_loading_complete);
5601 device_release_driver(&priv->pdev->dev);
5602 mwl8k_release_firmware(priv);
5603}
5604
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305605#define MAX_RESTART_ATTEMPTS 1
Brian Cavagnolo99020472010-11-12 17:23:52 -08005606static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5607 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005608{
5609 struct mwl8k_priv *priv = hw->priv;
5610 int rc;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305611 int count = MAX_RESTART_ATTEMPTS;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005612
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305613retry:
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005614 /* Reset firmware and hardware */
5615 mwl8k_hw_reset(priv);
5616
5617 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005618 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005619 if (rc) {
5620 wiphy_err(hw->wiphy, "Firmware files not found\n");
5621 return rc;
5622 }
5623
Brian Cavagnolo99020472010-11-12 17:23:52 -08005624 if (nowait)
5625 return rc;
5626
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005627 /* Load firmware into hardware */
5628 rc = mwl8k_load_firmware(hw);
5629 if (rc)
5630 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5631
5632 /* Reclaim memory once firmware is successfully loaded */
5633 mwl8k_release_firmware(priv);
5634
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305635 if (rc && count) {
5636 /* FW did not start successfully;
5637 * lets try one more time
5638 */
5639 count--;
5640 wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5641 msleep(20);
5642 goto retry;
5643 }
5644
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005645 return rc;
5646}
5647
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005648static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5649{
5650 struct mwl8k_priv *priv = hw->priv;
5651 int rc = 0;
5652 int i;
5653
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005654 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005655 rc = mwl8k_txq_init(hw, i);
5656 if (rc)
5657 break;
5658 if (priv->ap_fw)
5659 iowrite32(priv->txq[i].txd_dma,
5660 priv->sram + priv->txq_offset[i]);
5661 }
5662 return rc;
5663}
5664
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005665/* initialize hw after successfully loading a firmware image */
5666static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5667{
5668 struct mwl8k_priv *priv = hw->priv;
5669 int rc = 0;
5670 int i;
5671
5672 if (priv->ap_fw) {
5673 priv->rxd_ops = priv->device_info->ap_rxd_ops;
5674 if (priv->rxd_ops == NULL) {
5675 wiphy_err(hw->wiphy,
5676 "Driver does not have AP firmware image support for this hardware\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04005677 rc = -ENOENT;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005678 goto err_stop_firmware;
5679 }
5680 } else {
5681 priv->rxd_ops = &rxd_sta_ops;
5682 }
5683
5684 priv->sniffer_enabled = false;
5685 priv->wmm_enabled = false;
5686 priv->pending_tx_pkts = 0;
Yogesh Ashok Powarc27a54d2013-01-03 13:24:19 +05305687 atomic_set(&priv->watchdog_event_pending, 0);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005688
5689 rc = mwl8k_rxq_init(hw, 0);
5690 if (rc)
5691 goto err_stop_firmware;
5692 rxq_refill(hw, 0, INT_MAX);
5693
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005694 /* For the sta firmware, we need to know the dma addresses of tx queues
5695 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
5696 * prior to issuing this command. But for the AP case, we learn the
5697 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5698 * case we must initialize the tx queues after.
5699 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07005700 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005701 if (!priv->ap_fw) {
5702 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005703 if (rc)
5704 goto err_free_queues;
5705 }
5706
5707 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5708 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005709 iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5710 MWL8K_A2H_INT_BA_WATCHDOG,
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005711 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Nishant Sarmukadam12488e02011-04-08 14:38:27 +05305712 iowrite32(MWL8K_A2H_INT_OPC_DONE,
5713 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005714
5715 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5716 IRQF_SHARED, MWL8K_NAME, hw);
5717 if (rc) {
5718 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5719 goto err_free_queues;
5720 }
5721
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305722 /*
5723 * When hw restart is requested,
5724 * mac80211 will take care of clearing
5725 * the ampdu streams, so do not clear
5726 * the ampdu state here
5727 */
5728 if (!priv->hw_restart_in_progress)
5729 memset(priv->ampdu, 0, sizeof(priv->ampdu));
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005730
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005731 /*
5732 * Temporarily enable interrupts. Initial firmware host
5733 * commands use interrupts and avoid polling. Disable
5734 * interrupts when done.
5735 */
5736 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5737
5738 /* Get config data, mac addrs etc */
5739 if (priv->ap_fw) {
5740 rc = mwl8k_cmd_get_hw_spec_ap(hw);
5741 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005742 rc = mwl8k_init_txqs(hw);
5743 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005744 rc = mwl8k_cmd_set_hw_spec(hw);
5745 } else {
5746 rc = mwl8k_cmd_get_hw_spec_sta(hw);
5747 }
5748 if (rc) {
5749 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5750 goto err_free_irq;
5751 }
5752
5753 /* Turn radio off */
5754 rc = mwl8k_cmd_radio_disable(hw);
5755 if (rc) {
5756 wiphy_err(hw->wiphy, "Cannot disable\n");
5757 goto err_free_irq;
5758 }
5759
5760 /* Clear MAC address */
5761 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5762 if (rc) {
5763 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5764 goto err_free_irq;
5765 }
5766
Yogesh Ashok Powara246ac32013-01-25 16:17:56 +05305767 /* Configure Antennas */
5768 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
5769 if (rc)
5770 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
5771 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
5772 if (rc)
5773 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
5774
5775
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005776 /* Disable interrupts */
5777 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5778 free_irq(priv->pdev->irq, hw);
5779
5780 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5781 priv->device_info->part_name,
5782 priv->hw_rev, hw->wiphy->perm_addr,
5783 priv->ap_fw ? "AP" : "STA",
5784 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5785 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5786
5787 return 0;
5788
5789err_free_irq:
5790 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5791 free_irq(priv->pdev->irq, hw);
5792
5793err_free_queues:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005794 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005795 mwl8k_txq_deinit(hw, i);
5796 mwl8k_rxq_deinit(hw, 0);
5797
5798err_stop_firmware:
5799 mwl8k_hw_reset(priv);
5800
5801 return rc;
5802}
5803
5804/*
5805 * invoke mwl8k_reload_firmware to change the firmware image after the device
5806 * has already been registered
5807 */
5808static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5809{
5810 int i, rc = 0;
5811 struct mwl8k_priv *priv = hw->priv;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305812 struct mwl8k_vif *vif, *tmp_vif;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005813
5814 mwl8k_stop(hw);
5815 mwl8k_rxq_deinit(hw, 0);
5816
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305817 /*
5818 * All the existing interfaces are re-added by the ieee80211_reconfig;
5819 * which means driver should remove existing interfaces before calling
5820 * ieee80211_restart_hw
5821 */
5822 if (priv->hw_restart_in_progress)
5823 list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
5824 mwl8k_remove_vif(priv, vif);
5825
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005826 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005827 mwl8k_txq_deinit(hw, i);
5828
Brian Cavagnolo99020472010-11-12 17:23:52 -08005829 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005830 if (rc)
5831 goto fail;
5832
5833 rc = mwl8k_probe_hw(hw);
5834 if (rc)
5835 goto fail;
5836
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305837 if (priv->hw_restart_in_progress)
5838 return rc;
5839
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005840 rc = mwl8k_start(hw);
5841 if (rc)
5842 goto fail;
5843
5844 rc = mwl8k_config(hw, ~0);
5845 if (rc)
5846 goto fail;
5847
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005848 for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
Eliad Peller8a3a3c82011-10-02 10:15:52 +02005849 rc = mwl8k_conf_tx(hw, NULL, i, &priv->wmm_params[i]);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005850 if (rc)
5851 goto fail;
5852 }
5853
5854 return rc;
5855
5856fail:
5857 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5858 return rc;
5859}
5860
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305861static const struct ieee80211_iface_limit ap_if_limits[] = {
5862 { .max = 8, .types = BIT(NL80211_IFTYPE_AP) },
Yogesh Ashok Powar2acdaa72013-01-25 16:19:55 +05305863 { .max = 1, .types = BIT(NL80211_IFTYPE_STATION) },
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305864};
5865
5866static const struct ieee80211_iface_combination ap_if_comb = {
5867 .limits = ap_if_limits,
5868 .n_limits = ARRAY_SIZE(ap_if_limits),
5869 .max_interfaces = 8,
5870 .num_different_channels = 1,
5871};
5872
5873
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005874static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5875{
5876 struct ieee80211_hw *hw = priv->hw;
5877 int i, rc;
5878
Brian Cavagnolo99020472010-11-12 17:23:52 -08005879 rc = mwl8k_load_firmware(hw);
5880 mwl8k_release_firmware(priv);
5881 if (rc) {
5882 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5883 return rc;
5884 }
5885
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005886 /*
5887 * Extra headroom is the size of the required DMA header
5888 * minus the size of the smallest 802.11 frame (CTS frame).
5889 */
5890 hw->extra_tx_headroom =
5891 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5892
Yogesh Ashok Powarff776ce2011-04-28 17:34:48 +05305893 hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
5894
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005895 hw->queues = MWL8K_TX_WMM_QUEUES;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005896
5897 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08005898 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Yogesh Ashok Powar2a36a0e2011-08-29 17:12:44 +05305899
5900 /*
5901 * Ask mac80211 to not to trigger PS mode
5902 * based on PM bit of incoming frames.
5903 */
5904 if (priv->ap_fw)
5905 hw->flags |= IEEE80211_HW_AP_LINK_PS;
5906
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005907 hw->vif_data_size = sizeof(struct mwl8k_vif);
5908 hw->sta_data_size = sizeof(struct mwl8k_sta);
5909
5910 priv->macids_used = 0;
5911 INIT_LIST_HEAD(&priv->vif_list);
5912
5913 /* Set default radio state and preamble */
Rusty Russell3db1cd52011-12-19 13:56:45 +00005914 priv->radio_on = false;
5915 priv->radio_short_preamble = false;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005916
5917 /* Finalize join worker */
5918 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005919 /* Handle watchdog ba events */
5920 INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05305921 /* To reload the firmware if it crashes */
5922 INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005923
5924 /* TX reclaim and RX tasklets. */
5925 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5926 tasklet_disable(&priv->poll_tx_task);
5927 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5928 tasklet_disable(&priv->poll_rx_task);
5929
5930 /* Power management cookie */
5931 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5932 if (priv->cookie == NULL)
5933 return -ENOMEM;
5934
5935 mutex_init(&priv->fw_mutex);
5936 priv->fw_mutex_owner = NULL;
5937 priv->fw_mutex_depth = 0;
5938 priv->hostcmd_wait = NULL;
5939
5940 spin_lock_init(&priv->tx_lock);
5941
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005942 spin_lock_init(&priv->stream_lock);
5943
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005944 priv->tx_wait = NULL;
5945
5946 rc = mwl8k_probe_hw(hw);
5947 if (rc)
5948 goto err_free_cookie;
5949
5950 hw->wiphy->interface_modes = 0;
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305951
5952 if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005953 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
Yogesh Ashok Powar2acdaa72013-01-25 16:19:55 +05305954 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
Yogesh Ashok Powar5d377fc2012-11-06 19:22:16 +05305955 hw->wiphy->iface_combinations = &ap_if_comb;
5956 hw->wiphy->n_iface_combinations = 1;
5957 }
5958
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005959 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5960 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5961
5962 rc = ieee80211_register_hw(hw);
5963 if (rc) {
5964 wiphy_err(hw->wiphy, "Cannot register device\n");
5965 goto err_unprobe_hw;
5966 }
5967
5968 return 0;
5969
5970err_unprobe_hw:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005971 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005972 mwl8k_txq_deinit(hw, i);
5973 mwl8k_rxq_deinit(hw, 0);
5974
5975err_free_cookie:
5976 if (priv->cookie != NULL)
5977 pci_free_consistent(priv->pdev, 4,
5978 priv->cookie, priv->cookie_dma);
5979
5980 return rc;
5981}
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05005982static int mwl8k_probe(struct pci_dev *pdev,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005983 const struct pci_device_id *id)
5984{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005985 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005986 struct ieee80211_hw *hw;
5987 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005988 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005989 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02005990
5991 if (!printed_version) {
5992 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5993 printed_version = 1;
5994 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005995
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005996
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005997 rc = pci_enable_device(pdev);
5998 if (rc) {
5999 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
6000 MWL8K_NAME);
6001 return rc;
6002 }
6003
6004 rc = pci_request_regions(pdev, MWL8K_NAME);
6005 if (rc) {
6006 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
6007 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01006008 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006009 }
6010
6011 pci_set_master(pdev);
6012
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006013
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006014 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
6015 if (hw == NULL) {
6016 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
6017 rc = -ENOMEM;
6018 goto err_free_reg;
6019 }
6020
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006021 SET_IEEE80211_DEV(hw, &pdev->dev);
6022 pci_set_drvdata(pdev, hw);
6023
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006024 priv = hw->priv;
6025 priv->hw = hw;
6026 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05006027 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006028
Nishant Sarmukadam98929822013-03-01 17:13:01 +05306029 if (id->driver_data == MWL8764)
6030 priv->is_8764 = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006031
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006032 priv->sram = pci_iomap(pdev, 0, 0x10000);
6033 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07006034 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04006035 rc = -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006036 goto err_iounmap;
6037 }
6038
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006039 /*
6040 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
6041 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
6042 */
6043 priv->regs = pci_iomap(pdev, 1, 0x10000);
6044 if (priv->regs == NULL) {
6045 priv->regs = pci_iomap(pdev, 2, 0x10000);
6046 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07006047 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Alexey Khoroshilova2ca8ec2013-01-19 01:24:01 +04006048 rc = -EIO;
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006049 goto err_iounmap;
6050 }
6051 }
6052
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006053 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08006054 * Choose the initial fw image depending on user input. If a second
6055 * image is available, make it the alternative image that will be
6056 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006057 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08006058 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006059 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08006060 if (ap_mode_default && di->fw_image_ap) {
6061 priv->fw_pref = di->fw_image_ap;
6062 priv->fw_alt = di->fw_image_sta;
6063 } else if (!ap_mode_default && di->fw_image_sta) {
6064 priv->fw_pref = di->fw_image_sta;
6065 priv->fw_alt = di->fw_image_ap;
6066 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006067 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08006068 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08006069 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
6070 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08006071 priv->fw_pref = di->fw_image_ap;
6072 }
6073 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006074 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08006075 goto err_stop_firmware;
Yogesh Ashok Powar6b6accc2011-12-30 16:35:27 +05306076
6077 priv->hw_restart_in_progress = false;
6078
Yogesh Ashok Poware882efc2013-01-25 16:17:32 +05306079 priv->running_bsses = 0;
6080
Brian Cavagnolo99020472010-11-12 17:23:52 -08006081 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006082
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006083err_stop_firmware:
6084 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01006085
6086err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006087 if (priv->regs != NULL)
6088 pci_iounmap(pdev, priv->regs);
6089
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006090 if (priv->sram != NULL)
6091 pci_iounmap(pdev, priv->sram);
6092
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006093 ieee80211_free_hw(hw);
6094
6095err_free_reg:
6096 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01006097
6098err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006099 pci_disable_device(pdev);
6100
6101 return rc;
6102}
6103
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05006104static void mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006105{
6106 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
6107 struct mwl8k_priv *priv;
6108 int i;
6109
6110 if (hw == NULL)
6111 return;
6112 priv = hw->priv;
6113
Brian Cavagnolo99020472010-11-12 17:23:52 -08006114 wait_for_completion(&priv->firmware_loading_complete);
6115
6116 if (priv->fw_state == FW_STATE_ERROR) {
6117 mwl8k_hw_reset(priv);
6118 goto unmap;
6119 }
6120
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006121 ieee80211_stop_queues(hw);
6122
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02006123 ieee80211_unregister_hw(hw);
6124
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01006125 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01006126 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01006127 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006128
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006129 /* Stop hardware */
6130 mwl8k_hw_reset(priv);
6131
6132 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07006133 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01006134 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006135
Brian Cavagnoloe6007072011-03-17 11:58:44 -07006136 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006137 mwl8k_txq_deinit(hw, i);
6138
6139 mwl8k_rxq_deinit(hw, 0);
6140
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02006141 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006142
Brian Cavagnolo99020472010-11-12 17:23:52 -08006143unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006144 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02006145 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006146 ieee80211_free_hw(hw);
6147 pci_release_regions(pdev);
6148 pci_disable_device(pdev);
6149}
6150
6151static struct pci_driver mwl8k_driver = {
6152 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02006153 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006154 .probe = mwl8k_probe,
Bill Pemberton8dee5ee2012-12-03 09:56:36 -05006155 .remove = mwl8k_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006156};
6157
Axel Lin5b0a3b72012-04-14 10:38:36 +08006158module_pci_driver(mwl8k_driver);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02006159
6160MODULE_DESCRIPTION(MWL8K_DESC);
6161MODULE_VERSION(MWL8K_VERSION);
6162MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
6163MODULE_LICENSE("GPL");