blob: b90178da5a27b1889f9f443cbc7754f520dd23d3 [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka5fb2972010-01-12 13:51:38 +01005 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
13#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
Lennert Buytenheka5fb2972010-01-12 13:51:38 +010030#define MWL8K_VERSION "0.12"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031
Brian Cavagnolo0863ade2010-11-12 17:23:50 -080032/* Module parameters */
33static unsigned ap_mode_default;
34module_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
77#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
78 MWL8K_A2H_INT_CHNL_SWITCHED | \
79 MWL8K_A2H_INT_QUEUE_EMPTY | \
80 MWL8K_A2H_INT_RADAR_DETECT | \
81 MWL8K_A2H_INT_RADIO_ON | \
82 MWL8K_A2H_INT_RADIO_OFF | \
83 MWL8K_A2H_INT_MAC_EVENT | \
84 MWL8K_A2H_INT_OPC_DONE | \
85 MWL8K_A2H_INT_RX_READY | \
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -070086 MWL8K_A2H_INT_TX_DONE | \
87 MWL8K_A2H_INT_BA_WATCHDOG)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010088
Lennert Buytenheka66098d2009-03-10 10:13:33 +010089#define MWL8K_RX_QUEUES 1
Brian Cavagnoloe6007072011-03-17 11:58:44 -070090#define MWL8K_TX_WMM_QUEUES 4
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -070091#define MWL8K_MAX_AMPDU_QUEUES 8
Brian Cavagnoloe6007072011-03-17 11:58:44 -070092#define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
93#define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010094
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020095struct rxd_ops {
96 int rxd_size;
97 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
98 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010099 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400100 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200101};
102
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200103struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200104 char *part_name;
105 char *helper_image;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800106 char *fw_image_sta;
107 char *fw_image_ap;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100108 struct rxd_ops *ap_rxd_ops;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -0800109 u32 fw_api_ap;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200110};
111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100112struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200113 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100114
115 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200116 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100117
118 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200119 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100120
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200121 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200122 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200123 struct {
124 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900125 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200126 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100127};
128
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129struct mwl8k_tx_queue {
130 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200131 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100132
133 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200134 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100135
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200136 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200137 struct mwl8k_tx_desc *txd;
138 dma_addr_t txd_dma;
139 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140};
141
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700142enum {
143 AMPDU_NO_STREAM,
144 AMPDU_STREAM_NEW,
145 AMPDU_STREAM_IN_PROGRESS,
146 AMPDU_STREAM_ACTIVE,
147};
148
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700149struct mwl8k_ampdu_stream {
150 struct ieee80211_sta *sta;
151 u8 tid;
152 u8 state;
153 u8 idx;
154 u8 txq_idx; /* index of this stream in priv->txq */
155};
156
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100159 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100160
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200161 struct mwl8k_device_info *device_info;
162
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100163 void __iomem *sram;
164 void __iomem *regs;
165
166 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800167 const struct firmware *fw_helper;
168 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100169
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100170 /* hardware/firmware parameters */
171 bool ap_fw;
172 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100173 struct ieee80211_supported_band band_24;
174 struct ieee80211_channel channels_24[14];
175 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100176 struct ieee80211_supported_band band_50;
177 struct ieee80211_channel channels_50[4];
178 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100179 u32 ap_macids_supported;
180 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100181
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700182 /* Ampdu stream information */
183 u8 num_ampdu_queues;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700184 spinlock_t stream_lock;
185 struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700186 struct work_struct watchdog_ba_handle;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700187
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200188 /* firmware access */
189 struct mutex fw_mutex;
190 struct task_struct *fw_mutex_owner;
191 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200192 struct completion *hostcmd_wait;
193
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100194 /* lock held over TX and TX reap */
195 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100196
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200197 /* TX quiesce completion, protected by fw_mutex and tx_lock */
198 struct completion *tx_wait;
199
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100200 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100201 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100202 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100203
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100204 /* power management status cookie from firmware */
205 u32 *cookie;
206 dma_addr_t cookie_dma;
207
208 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100209 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200210 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100211
212 /*
213 * Running count of TX packets in flight, to avoid
214 * iterating over the transmit rings each time.
215 */
216 int pending_tx_pkts;
217
218 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700219 struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
220 u32 txq_offset[MWL8K_MAX_TX_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100221
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200222 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200223 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200224 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200225 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100226
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100227 /* XXX need to convert this to handle multiple interfaces */
228 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200229 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100230 struct sk_buff *beacon_skb;
231
232 /*
233 * This FJ worker has to be global as it is scheduled from the
234 * RX handler. At this point we don't know which interface it
235 * belongs to until the list of bssids waiting to complete join
236 * is checked.
237 */
238 struct work_struct finalize_join_worker;
239
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100240 /* Tasklet to perform TX reclaim. */
241 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100242
243 /* Tasklet to perform RX. */
244 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400245
246 /* Most recently reported noise in dBm */
247 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800248
249 /*
250 * preserve the queue configurations so they can be restored if/when
251 * the firmware image is swapped.
252 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700253 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800254
255 /* async firmware loading state */
256 unsigned fw_state;
257 char *fw_pref;
258 char *fw_alt;
259 struct completion firmware_loading_complete;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100260};
261
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800262#define MAX_WEP_KEY_LEN 13
263#define NUM_WEP_KEYS 4
264
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100265/* Per interface specific private data */
266struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100267 struct list_head list;
268 struct ieee80211_vif *vif;
269
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100270 /* Firmware macid for this vif. */
271 int macid;
272
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100273 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100274 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800275
276 /* Saved WEP keys */
277 struct {
278 u8 enabled;
279 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
280 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800281
282 /* BSSID */
283 u8 bssid[ETH_ALEN];
284
285 /* A flag to indicate is HW crypto is enabled for this bssid */
286 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100287};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200288#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800289#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100290
Lennert Buytenheka6804002010-01-04 21:55:42 +0100291struct mwl8k_sta {
292 /* Index into station database. Returned by UPDATE_STADB. */
293 u8 peer_id;
294};
295#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
296
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100297static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100298 { .center_freq = 2412, .hw_value = 1, },
299 { .center_freq = 2417, .hw_value = 2, },
300 { .center_freq = 2422, .hw_value = 3, },
301 { .center_freq = 2427, .hw_value = 4, },
302 { .center_freq = 2432, .hw_value = 5, },
303 { .center_freq = 2437, .hw_value = 6, },
304 { .center_freq = 2442, .hw_value = 7, },
305 { .center_freq = 2447, .hw_value = 8, },
306 { .center_freq = 2452, .hw_value = 9, },
307 { .center_freq = 2457, .hw_value = 10, },
308 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100309 { .center_freq = 2467, .hw_value = 12, },
310 { .center_freq = 2472, .hw_value = 13, },
311 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100312};
313
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100314static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100315 { .bitrate = 10, .hw_value = 2, },
316 { .bitrate = 20, .hw_value = 4, },
317 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200318 { .bitrate = 110, .hw_value = 22, },
319 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100320 { .bitrate = 60, .hw_value = 12, },
321 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100322 { .bitrate = 120, .hw_value = 24, },
323 { .bitrate = 180, .hw_value = 36, },
324 { .bitrate = 240, .hw_value = 48, },
325 { .bitrate = 360, .hw_value = 72, },
326 { .bitrate = 480, .hw_value = 96, },
327 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100328 { .bitrate = 720, .hw_value = 144, },
329};
330
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100331static const struct ieee80211_channel mwl8k_channels_50[] = {
332 { .center_freq = 5180, .hw_value = 36, },
333 { .center_freq = 5200, .hw_value = 40, },
334 { .center_freq = 5220, .hw_value = 44, },
335 { .center_freq = 5240, .hw_value = 48, },
336};
337
338static const struct ieee80211_rate mwl8k_rates_50[] = {
339 { .bitrate = 60, .hw_value = 12, },
340 { .bitrate = 90, .hw_value = 18, },
341 { .bitrate = 120, .hw_value = 24, },
342 { .bitrate = 180, .hw_value = 36, },
343 { .bitrate = 240, .hw_value = 48, },
344 { .bitrate = 360, .hw_value = 72, },
345 { .bitrate = 480, .hw_value = 96, },
346 { .bitrate = 540, .hw_value = 108, },
347 { .bitrate = 720, .hw_value = 144, },
348};
349
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100350/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100351#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800352#define MWL8K_CMD_SET 0x0001
353#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100354
355/* Firmware command codes */
356#define MWL8K_CMD_CODE_DNLD 0x0001
357#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200358#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100359#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
360#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200361#define MWL8K_CMD_RADIO_CONTROL 0x001c
362#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800363#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200364#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100365#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100366#define MWL8K_CMD_SET_PRE_SCAN 0x0107
367#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200368#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100369#define MWL8K_CMD_SET_AID 0x010d
370#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200371#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100372#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200373#define MWL8K_CMD_SET_SLOT 0x0114
374#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
375#define MWL8K_CMD_SET_WMM_MODE 0x0123
376#define MWL8K_CMD_MIMO_CONFIG 0x0125
377#define MWL8K_CMD_USE_FIXED_RATE 0x0126
378#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100379#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200380#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700381#define MWL8K_CMD_GET_WATCHDOG_BITMAP 0x0205
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100382#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
383#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800384#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200385#define MWL8K_CMD_UPDATE_STADB 0x1123
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700386#define MWL8K_CMD_BASTREAM 0x1125
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100387
John W. Linvilleb6037422010-07-20 13:55:00 -0400388static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100389{
John W. Linvilleb6037422010-07-20 13:55:00 -0400390 u16 command = le16_to_cpu(cmd);
391
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100392#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
393 snprintf(buf, bufsize, "%s", #x);\
394 return buf;\
395 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400396 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100397 MWL8K_CMDNAME(CODE_DNLD);
398 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200399 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100400 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
401 MWL8K_CMDNAME(GET_STAT);
402 MWL8K_CMDNAME(RADIO_CONTROL);
403 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800404 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200405 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100406 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100407 MWL8K_CMDNAME(SET_PRE_SCAN);
408 MWL8K_CMDNAME(SET_POST_SCAN);
409 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100410 MWL8K_CMDNAME(SET_AID);
411 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200412 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100413 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200414 MWL8K_CMDNAME(SET_SLOT);
415 MWL8K_CMDNAME(SET_EDCA_PARAMS);
416 MWL8K_CMDNAME(SET_WMM_MODE);
417 MWL8K_CMDNAME(MIMO_CONFIG);
418 MWL8K_CMDNAME(USE_FIXED_RATE);
419 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200420 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200421 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100422 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100423 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800424 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200425 MWL8K_CMDNAME(UPDATE_STADB);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700426 MWL8K_CMDNAME(BASTREAM);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -0700427 MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100428 default:
429 snprintf(buf, bufsize, "0x%x", cmd);
430 }
431#undef MWL8K_CMDNAME
432
433 return buf;
434}
435
436/* Hardware and firmware reset */
437static void mwl8k_hw_reset(struct mwl8k_priv *priv)
438{
439 iowrite32(MWL8K_H2A_INT_RESET,
440 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
441 iowrite32(MWL8K_H2A_INT_RESET,
442 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
443 msleep(20);
444}
445
446/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800447static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100448{
449 if (*fw == NULL)
450 return;
451 release_firmware(*fw);
452 *fw = NULL;
453}
454
455static void mwl8k_release_firmware(struct mwl8k_priv *priv)
456{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100457 mwl8k_release_fw(&priv->fw_ucode);
458 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100459}
460
Brian Cavagnolo99020472010-11-12 17:23:52 -0800461/* states for asynchronous f/w loading */
462static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
463enum {
464 FW_STATE_INIT = 0,
465 FW_STATE_LOADING_PREF,
466 FW_STATE_LOADING_ALT,
467 FW_STATE_ERROR,
468};
469
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100470/* Request fw image */
471static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800472 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800473 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100474{
475 /* release current image */
476 if (*fw != NULL)
477 mwl8k_release_fw(fw);
478
Brian Cavagnolo99020472010-11-12 17:23:52 -0800479 if (nowait)
480 return request_firmware_nowait(THIS_MODULE, 1, fname,
481 &priv->pdev->dev, GFP_KERNEL,
482 priv, mwl8k_fw_state_machine);
483 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800484 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100485}
486
Brian Cavagnolo99020472010-11-12 17:23:52 -0800487static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
488 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100489{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200490 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100491 int rc;
492
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200493 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800494 if (nowait)
495 rc = mwl8k_request_fw(priv, di->helper_image,
496 &priv->fw_helper, true);
497 else
498 rc = mwl8k_request_fw(priv, di->helper_image,
499 &priv->fw_helper, false);
500 if (rc)
501 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
502 pci_name(priv->pdev), di->helper_image);
503
504 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200505 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100506 }
507
Brian Cavagnolo99020472010-11-12 17:23:52 -0800508 if (nowait) {
509 /*
510 * if we get here, no helper image is needed. Skip the
511 * FW_STATE_INIT state.
512 */
513 priv->fw_state = FW_STATE_LOADING_PREF;
514 rc = mwl8k_request_fw(priv, fw_image,
515 &priv->fw_ucode,
516 true);
517 } else
518 rc = mwl8k_request_fw(priv, fw_image,
519 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100520 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200521 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800522 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100523 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100524 return rc;
525 }
526
527 return 0;
528}
529
530struct mwl8k_cmd_pkt {
531 __le16 code;
532 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100533 __u8 seq_num;
534 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100535 __le16 result;
536 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000537} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100538
539/*
540 * Firmware loading.
541 */
542static int
543mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
544{
545 void __iomem *regs = priv->regs;
546 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100547 int loops;
548
549 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
550 if (pci_dma_mapping_error(priv->pdev, dma_addr))
551 return -ENOMEM;
552
553 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
554 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
555 iowrite32(MWL8K_H2A_INT_DOORBELL,
556 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
557 iowrite32(MWL8K_H2A_INT_DUMMY,
558 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
559
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100560 loops = 1000;
561 do {
562 u32 int_code;
563
564 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
565 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
566 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100567 break;
568 }
569
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200570 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100571 udelay(1);
572 } while (--loops);
573
574 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
575
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200576 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100577}
578
579static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
580 const u8 *data, size_t length)
581{
582 struct mwl8k_cmd_pkt *cmd;
583 int done;
584 int rc = 0;
585
586 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
587 if (cmd == NULL)
588 return -ENOMEM;
589
590 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
591 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100592 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100593 cmd->result = 0;
594
595 done = 0;
596 while (length) {
597 int block_size = length > 256 ? 256 : length;
598
599 memcpy(cmd->payload, data + done, block_size);
600 cmd->length = cpu_to_le16(block_size);
601
602 rc = mwl8k_send_fw_load_cmd(priv, cmd,
603 sizeof(*cmd) + block_size);
604 if (rc)
605 break;
606
607 done += block_size;
608 length -= block_size;
609 }
610
611 if (!rc) {
612 cmd->length = 0;
613 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
614 }
615
616 kfree(cmd);
617
618 return rc;
619}
620
621static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
622 const u8 *data, size_t length)
623{
624 unsigned char *buffer;
625 int may_continue, rc = 0;
626 u32 done, prev_block_size;
627
628 buffer = kmalloc(1024, GFP_KERNEL);
629 if (buffer == NULL)
630 return -ENOMEM;
631
632 done = 0;
633 prev_block_size = 0;
634 may_continue = 1000;
635 while (may_continue > 0) {
636 u32 block_size;
637
638 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
639 if (block_size & 1) {
640 block_size &= ~1;
641 may_continue--;
642 } else {
643 done += prev_block_size;
644 length -= prev_block_size;
645 }
646
647 if (block_size > 1024 || block_size > length) {
648 rc = -EOVERFLOW;
649 break;
650 }
651
652 if (length == 0) {
653 rc = 0;
654 break;
655 }
656
657 if (block_size == 0) {
658 rc = -EPROTO;
659 may_continue--;
660 udelay(1);
661 continue;
662 }
663
664 prev_block_size = block_size;
665 memcpy(buffer, data + done, block_size);
666
667 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
668 if (rc)
669 break;
670 }
671
672 if (!rc && length != 0)
673 rc = -EREMOTEIO;
674
675 kfree(buffer);
676
677 return rc;
678}
679
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200680static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100681{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200682 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800683 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200684 int rc;
685 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100686
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200687 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800688 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100689
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200690 if (helper == NULL) {
691 printk(KERN_ERR "%s: helper image needed but none "
692 "given\n", pci_name(priv->pdev));
693 return -EINVAL;
694 }
695
696 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100697 if (rc) {
698 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200699 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100700 return rc;
701 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100702 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100703
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200704 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100705 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200706 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100707 }
708
709 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200710 printk(KERN_ERR "%s: unable to load firmware image\n",
711 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100712 return rc;
713 }
714
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100715 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100716
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100717 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100718 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200719 u32 ready_code;
720
721 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
722 if (ready_code == MWL8K_FWAP_READY) {
723 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100724 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200725 } else if (ready_code == MWL8K_FWSTA_READY) {
726 priv->ap_fw = 0;
727 break;
728 }
729
730 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100731 udelay(1);
732 } while (--loops);
733
734 return loops ? 0 : -ETIMEDOUT;
735}
736
737
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100738/* DMA header used by firmware and hardware. */
739struct mwl8k_dma_data {
740 __le16 fwlen;
741 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100742 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000743} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100744
745/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100746static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100747{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100748 struct mwl8k_dma_data *tr;
749 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100750
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100751 tr = (struct mwl8k_dma_data *)skb->data;
752 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
753
754 if (hdrlen != sizeof(tr->wh)) {
755 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
756 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
757 *((__le16 *)(tr->data - 2)) = qos;
758 } else {
759 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
760 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100761 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100762
763 if (hdrlen != sizeof(*tr))
764 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100765}
766
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800767static void
768mwl8k_add_dma_header(struct sk_buff *skb, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100769{
770 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100771 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800772 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100773 struct mwl8k_dma_data *tr;
774
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100775 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100776 * Add a firmware DMA header; the firmware requires that we
777 * present a 2-byte payload length followed by a 4-address
778 * header (without QoS field), followed (optionally) by any
779 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100780 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100781 wh = (struct ieee80211_hdr *)skb->data;
782
783 hdrlen = ieee80211_hdrlen(wh->frame_control);
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800784 reqd_hdrlen = sizeof(*tr);
785
786 if (hdrlen != reqd_hdrlen)
787 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100788
789 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800790 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100791
792 tr = (struct mwl8k_dma_data *)skb->data;
793 if (wh != &tr->wh)
794 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100795 if (hdrlen != sizeof(tr->wh))
796 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100797
798 /*
799 * Firmware length is the length of the fully formed "802.11
800 * payload". That is, everything except for the 802.11 header.
801 * This includes all crypto material including the MIC.
802 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800803 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100804}
805
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800806static void mwl8k_encapsulate_tx_frame(struct sk_buff *skb)
807{
808 struct ieee80211_hdr *wh;
809 struct ieee80211_tx_info *tx_info;
810 struct ieee80211_key_conf *key_conf;
811 int data_pad;
812
813 wh = (struct ieee80211_hdr *)skb->data;
814
815 tx_info = IEEE80211_SKB_CB(skb);
816
817 key_conf = NULL;
818 if (ieee80211_is_data(wh->frame_control))
819 key_conf = tx_info->control.hw_key;
820
821 /*
822 * Make sure the packet header is in the DMA header format (4-address
823 * without QoS), the necessary crypto padding between the header and the
824 * payload has already been provided by mac80211, but it doesn't add tail
825 * padding when HW crypto is enabled.
826 *
827 * We have the following trailer padding requirements:
828 * - WEP: 4 trailer bytes (ICV)
829 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
830 * - CCMP: 8 trailer bytes (MIC)
831 */
832 data_pad = 0;
833 if (key_conf != NULL) {
834 switch (key_conf->cipher) {
835 case WLAN_CIPHER_SUITE_WEP40:
836 case WLAN_CIPHER_SUITE_WEP104:
837 data_pad = 4;
838 break;
839 case WLAN_CIPHER_SUITE_TKIP:
840 data_pad = 12;
841 break;
842 case WLAN_CIPHER_SUITE_CCMP:
843 data_pad = 8;
844 break;
845 }
846 }
847 mwl8k_add_dma_header(skb, data_pad);
848}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100849
850/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100851 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200852 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100853struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200854 __le16 pkt_len;
855 __u8 sq2;
856 __u8 rate;
857 __le32 pkt_phys_addr;
858 __le32 next_rxd_phys_addr;
859 __le16 qos_control;
860 __le16 htsig2;
861 __le32 hw_rssi_info;
862 __le32 hw_noise_floor_info;
863 __u8 noise_floor;
864 __u8 pad0[3];
865 __u8 rssi;
866 __u8 rx_status;
867 __u8 channel;
868 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000869} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200870
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100871#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
872#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
873#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100874
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100875#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200876
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800877/* 8366 AP rx_status bits */
878#define MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
879#define MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
880#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
881#define MWL8K_8366_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
882#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
883
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100884static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200885{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100886 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200887
888 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100889 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200890}
891
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100892static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200893{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100894 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200895
896 rxd->pkt_len = cpu_to_le16(len);
897 rxd->pkt_phys_addr = cpu_to_le32(addr);
898 wmb();
899 rxd->rx_ctrl = 0;
900}
901
902static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100903mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400904 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200905{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100906 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200907
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100908 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200909 return -1;
910 rmb();
911
912 memset(status, 0, sizeof(*status));
913
914 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400915 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200916
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100917 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200918 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100919 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100920 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100921 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200922 } else {
923 int i;
924
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100925 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
926 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200927 status->rate_idx = i;
928 break;
929 }
930 }
931 }
932
Lennert Buytenhek85478342010-01-12 13:48:56 +0100933 if (rxd->channel > 14) {
934 status->band = IEEE80211_BAND_5GHZ;
935 if (!(status->flag & RX_FLAG_HT))
936 status->rate_idx -= 5;
937 } else {
938 status->band = IEEE80211_BAND_2GHZ;
939 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900940 status->freq = ieee80211_channel_to_frequency(rxd->channel,
941 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200942
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100943 *qos = rxd->qos_control;
944
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800945 if ((rxd->rx_status != MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
946 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK) &&
947 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
948 status->flag |= RX_FLAG_MMIC_ERROR;
949
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200950 return le16_to_cpu(rxd->pkt_len);
951}
952
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100953static struct rxd_ops rxd_8366_ap_ops = {
954 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
955 .rxd_init = mwl8k_rxd_8366_ap_init,
956 .rxd_refill = mwl8k_rxd_8366_ap_refill,
957 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200958};
959
960/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100961 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100962 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100963struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100964 __le16 pkt_len;
965 __u8 link_quality;
966 __u8 noise_level;
967 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200968 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100969 __le16 qos_control;
970 __le16 rate_info;
971 __le32 pad0[4];
972 __u8 rssi;
973 __u8 channel;
974 __le16 pad1;
975 __u8 rx_ctrl;
976 __u8 rx_status;
977 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000978} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100979
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100980#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
981#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
982#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
983#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
984#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
985#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200986
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100987#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800988#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
989/* ICV=0 or MIC=1 */
990#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
991/* Key is uploaded only in failure case */
992#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200993
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100994static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200995{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100996 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200997
998 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100999 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001000}
1001
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001002static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001003{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001004 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001005
1006 rxd->pkt_len = cpu_to_le16(len);
1007 rxd->pkt_phys_addr = cpu_to_le32(addr);
1008 wmb();
1009 rxd->rx_ctrl = 0;
1010}
1011
1012static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001013mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -04001014 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001015{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001016 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001017 u16 rate_info;
1018
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001019 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001020 return -1;
1021 rmb();
1022
1023 rate_info = le16_to_cpu(rxd->rate_info);
1024
1025 memset(status, 0, sizeof(*status));
1026
1027 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -04001028 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001029 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1030 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001031
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001032 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001033 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001034 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001035 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001036 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001037 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001038 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001039 status->flag |= RX_FLAG_HT;
1040
Lennert Buytenhek85478342010-01-12 13:48:56 +01001041 if (rxd->channel > 14) {
1042 status->band = IEEE80211_BAND_5GHZ;
1043 if (!(status->flag & RX_FLAG_HT))
1044 status->rate_idx -= 5;
1045 } else {
1046 status->band = IEEE80211_BAND_2GHZ;
1047 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001048 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1049 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001050
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001051 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001052 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1053 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1054 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001055
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001056 return le16_to_cpu(rxd->pkt_len);
1057}
1058
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001059static struct rxd_ops rxd_sta_ops = {
1060 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1061 .rxd_init = mwl8k_rxd_sta_init,
1062 .rxd_refill = mwl8k_rxd_sta_refill,
1063 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001064};
1065
1066
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001067#define MWL8K_RX_DESCS 256
1068#define MWL8K_RX_MAXSZ 3800
1069
1070static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1071{
1072 struct mwl8k_priv *priv = hw->priv;
1073 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1074 int size;
1075 int i;
1076
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001077 rxq->rxd_count = 0;
1078 rxq->head = 0;
1079 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001080
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001081 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001082
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001083 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1084 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001085 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001086 return -ENOMEM;
1087 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001088 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001089
Shan Weib9ede5f2011-03-08 11:02:03 +08001090 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001091 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001092 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001093 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001094 return -ENOMEM;
1095 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001096
1097 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001098 int desc_size;
1099 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001100 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001101 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001102
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001103 desc_size = priv->rxd_ops->rxd_size;
1104 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001105
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001106 nexti = i + 1;
1107 if (nexti == MWL8K_RX_DESCS)
1108 nexti = 0;
1109 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1110
1111 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001112 }
1113
1114 return 0;
1115}
1116
1117static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1118{
1119 struct mwl8k_priv *priv = hw->priv;
1120 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1121 int refilled;
1122
1123 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001124 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001125 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001126 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001127 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001128 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001129
1130 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1131 if (skb == NULL)
1132 break;
1133
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001134 addr = pci_map_single(priv->pdev, skb->data,
1135 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001136
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001137 rxq->rxd_count++;
1138 rx = rxq->tail++;
1139 if (rxq->tail == MWL8K_RX_DESCS)
1140 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001141 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001142 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001143
1144 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1145 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001146
1147 refilled++;
1148 }
1149
1150 return refilled;
1151}
1152
1153/* Must be called only when the card's reception is completely halted */
1154static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1155{
1156 struct mwl8k_priv *priv = hw->priv;
1157 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1158 int i;
1159
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001160 if (rxq->rxd == NULL)
1161 return;
1162
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001164 if (rxq->buf[i].skb != NULL) {
1165 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001166 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001167 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001168 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001169
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001170 kfree_skb(rxq->buf[i].skb);
1171 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001172 }
1173 }
1174
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001175 kfree(rxq->buf);
1176 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001177
1178 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001179 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001180 rxq->rxd, rxq->rxd_dma);
1181 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001182}
1183
1184
1185/*
1186 * Scan a list of BSSIDs to process for finalize join.
1187 * Allows for extension to process multiple BSSIDs.
1188 */
1189static inline int
1190mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1191{
1192 return priv->capture_beacon &&
1193 ieee80211_is_beacon(wh->frame_control) &&
1194 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1195}
1196
Lennert Buytenhek37797522009-10-22 20:19:45 +02001197static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1198 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001199{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001200 struct mwl8k_priv *priv = hw->priv;
1201
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001202 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001203 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001204
1205 /*
1206 * Use GFP_ATOMIC as rxq_process is called from
1207 * the primary interrupt handler, memory allocation call
1208 * must not sleep.
1209 */
1210 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1211 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001212 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001213}
1214
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001215static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1216 u8 *bssid)
1217{
1218 struct mwl8k_vif *mwl8k_vif;
1219
1220 list_for_each_entry(mwl8k_vif,
1221 vif_list, list) {
1222 if (memcmp(bssid, mwl8k_vif->bssid,
1223 ETH_ALEN) == 0)
1224 return mwl8k_vif;
1225 }
1226
1227 return NULL;
1228}
1229
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001230static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1231{
1232 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001233 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001234 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1235 int processed;
1236
1237 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001238 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001239 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001240 void *rxd;
1241 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001242 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001243 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001244 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001245
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001246 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001247 if (skb == NULL)
1248 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001249
1250 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1251
John W. Linville0d462bb2010-07-28 14:04:24 -04001252 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1253 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001254 if (pkt_len < 0)
1255 break;
1256
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001257 rxq->buf[rxq->head].skb = NULL;
1258
1259 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001260 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001261 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001262 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001263
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001264 rxq->head++;
1265 if (rxq->head == MWL8K_RX_DESCS)
1266 rxq->head = 0;
1267
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001268 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001269
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001270 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001271
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001272 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001273 * Check for a pending join operation. Save a
1274 * copy of the beacon and schedule a tasklet to
1275 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001276 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001277 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001278 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001279
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001280 if (ieee80211_has_protected(wh->frame_control)) {
1281
1282 /* Check if hw crypto has been enabled for
1283 * this bss. If yes, set the status flags
1284 * accordingly
1285 */
1286 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1287 wh->addr1);
1288
1289 if (mwl8k_vif != NULL &&
1290 mwl8k_vif->is_hw_crypto_enabled == true) {
1291 /*
1292 * When MMIC ERROR is encountered
1293 * by the firmware, payload is
1294 * dropped and only 32 bytes of
1295 * mwl8k Firmware header is sent
1296 * to the host.
1297 *
1298 * We need to add four bytes of
1299 * key information. In it
1300 * MAC80211 expects keyidx set to
1301 * 0 for triggering Counter
1302 * Measure of MMIC failure.
1303 */
1304 if (status.flag & RX_FLAG_MMIC_ERROR) {
1305 struct mwl8k_dma_data *tr;
1306 tr = (struct mwl8k_dma_data *)skb->data;
1307 memset((void *)&(tr->data), 0, 4);
1308 pkt_len += 4;
1309 }
1310
1311 if (!ieee80211_is_auth(wh->frame_control))
1312 status.flag |= RX_FLAG_IV_STRIPPED |
1313 RX_FLAG_DECRYPTED |
1314 RX_FLAG_MMIC_STRIPPED;
1315 }
1316 }
1317
1318 skb_put(skb, pkt_len);
1319 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001320 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1321 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001322
1323 processed++;
1324 }
1325
1326 return processed;
1327}
1328
1329
1330/*
1331 * Packet transmission.
1332 */
1333
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001334#define MWL8K_TXD_STATUS_OK 0x00000001
1335#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1336#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1337#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001338#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001339
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001340#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1341#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1342#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1343#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1344#define MWL8K_QOS_EOSP 0x0010
1345
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001346struct mwl8k_tx_desc {
1347 __le32 status;
1348 __u8 data_rate;
1349 __u8 tx_priority;
1350 __le16 qos_control;
1351 __le32 pkt_phys_addr;
1352 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001353 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001354 __le32 next_txd_phys_addr;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07001355 __le32 timestamp;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001356 __le16 rate_info;
1357 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001358 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001359} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001360
1361#define MWL8K_TX_DESCS 128
1362
1363static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1364{
1365 struct mwl8k_priv *priv = hw->priv;
1366 struct mwl8k_tx_queue *txq = priv->txq + index;
1367 int size;
1368 int i;
1369
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001370 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001371 txq->head = 0;
1372 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001373
1374 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1375
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001376 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1377 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001378 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001379 return -ENOMEM;
1380 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001381 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001382
Shan Weib9ede5f2011-03-08 11:02:03 +08001383 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001384 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001385 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001386 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001387 return -ENOMEM;
1388 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001389
1390 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1391 struct mwl8k_tx_desc *tx_desc;
1392 int nexti;
1393
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001394 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001395 nexti = (i + 1) % MWL8K_TX_DESCS;
1396
1397 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001398 tx_desc->next_txd_phys_addr =
1399 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001400 }
1401
1402 return 0;
1403}
1404
1405static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1406{
1407 iowrite32(MWL8K_H2A_INT_PPA_READY,
1408 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1409 iowrite32(MWL8K_H2A_INT_DUMMY,
1410 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1411 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1412}
1413
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001414static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001415{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001416 struct mwl8k_priv *priv = hw->priv;
1417 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001418
Brian Cavagnoloe6007072011-03-17 11:58:44 -07001419 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001420 struct mwl8k_tx_queue *txq = priv->txq + i;
1421 int fw_owned = 0;
1422 int drv_owned = 0;
1423 int unused = 0;
1424 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001425
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001426 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001427 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1428 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001429
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001430 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001431 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001432 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001433 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001434 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001435
1436 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001437 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001438 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439
Joe Perchesc96c31e2010-07-26 14:39:58 -07001440 wiphy_err(hw->wiphy,
1441 "txq[%d] len=%d head=%d tail=%d "
1442 "fw_owned=%d drv_owned=%d unused=%d\n",
1443 i,
1444 txq->len, txq->head, txq->tail,
1445 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001446 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001447}
1448
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001449/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001450 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001451 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001452#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001453
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001454static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001455{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001456 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001457 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001458 int retry;
1459 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001460
1461 might_sleep();
1462
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001463 /*
1464 * The TX queues are stopped at this point, so this test
1465 * doesn't need to take ->tx_lock.
1466 */
1467 if (!priv->pending_tx_pkts)
1468 return 0;
1469
1470 retry = 0;
1471 rc = 0;
1472
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001473 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001474 priv->tx_wait = &tx_wait;
1475 while (!rc) {
1476 int oldcount;
1477 unsigned long timeout;
1478
1479 oldcount = priv->pending_tx_pkts;
1480
1481 spin_unlock_bh(&priv->tx_lock);
1482 timeout = wait_for_completion_timeout(&tx_wait,
1483 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1484 spin_lock_bh(&priv->tx_lock);
1485
1486 if (timeout) {
1487 WARN_ON(priv->pending_tx_pkts);
1488 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001489 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001490 }
1491 break;
1492 }
1493
1494 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001495 wiphy_notice(hw->wiphy,
1496 "waiting for tx rings to drain (%d -> %d pkts)\n",
1497 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001498 retry = 1;
1499 continue;
1500 }
1501
1502 priv->tx_wait = NULL;
1503
Joe Perchesc96c31e2010-07-26 14:39:58 -07001504 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1505 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001506 mwl8k_dump_tx_rings(hw);
1507
1508 rc = -ETIMEDOUT;
1509 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001510 spin_unlock_bh(&priv->tx_lock);
1511
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001512 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001513}
1514
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001515#define MWL8K_TXD_SUCCESS(status) \
1516 ((status) & (MWL8K_TXD_STATUS_OK | \
1517 MWL8K_TXD_STATUS_OK_RETRY | \
1518 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001519
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001520static int
1521mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001522{
1523 struct mwl8k_priv *priv = hw->priv;
1524 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001525 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001526
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001527 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001528 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001529 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001530 struct mwl8k_tx_desc *tx_desc;
1531 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001532 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001533 struct sk_buff *skb;
1534 struct ieee80211_tx_info *info;
1535 u32 status;
1536
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001537 tx = txq->head;
1538 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001539
1540 status = le32_to_cpu(tx_desc->status);
1541
1542 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1543 if (!force)
1544 break;
1545 tx_desc->status &=
1546 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1547 }
1548
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001549 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001550 BUG_ON(txq->len == 0);
1551 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001552 priv->pending_tx_pkts--;
1553
1554 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001555 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001556 skb = txq->skb[tx];
1557 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001558
1559 BUG_ON(skb == NULL);
1560 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1561
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001562 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001563
1564 /* Mark descriptor as unused */
1565 tx_desc->pkt_phys_addr = 0;
1566 tx_desc->pkt_len = 0;
1567
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001568 info = IEEE80211_SKB_CB(skb);
1569 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001570
1571 /* Rate control is happening in the firmware.
1572 * Ensure no tx rate is being reported.
1573 */
1574 info->status.rates[0].idx = -1;
1575 info->status.rates[0].count = 1;
1576
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001577 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001579
1580 ieee80211_tx_status_irqsafe(hw, skb);
1581
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001582 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001583 }
1584
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001585 if (index < MWL8K_TX_WMM_QUEUES && processed && priv->radio_on &&
1586 !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001587 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001588
1589 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001590}
1591
1592/* must be called only when the card's transmit is completely halted */
1593static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1594{
1595 struct mwl8k_priv *priv = hw->priv;
1596 struct mwl8k_tx_queue *txq = priv->txq + index;
1597
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001598 if (txq->txd == NULL)
1599 return;
1600
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001601 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001602
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001603 kfree(txq->skb);
1604 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001605
1606 pci_free_consistent(priv->pdev,
1607 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001608 txq->txd, txq->txd_dma);
1609 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001610}
1611
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001612/* caller must hold priv->stream_lock when calling the stream functions */
1613struct mwl8k_ampdu_stream *
1614mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1615{
1616 struct mwl8k_ampdu_stream *stream;
1617 struct mwl8k_priv *priv = hw->priv;
1618 int i;
1619
1620 for (i = 0; i < priv->num_ampdu_queues; i++) {
1621 stream = &priv->ampdu[i];
1622 if (stream->state == AMPDU_NO_STREAM) {
1623 stream->sta = sta;
1624 stream->state = AMPDU_STREAM_NEW;
1625 stream->tid = tid;
1626 stream->idx = i;
1627 stream->txq_idx = MWL8K_TX_WMM_QUEUES + i;
1628 wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1629 sta->addr, tid);
1630 return stream;
1631 }
1632 }
1633 return NULL;
1634}
1635
1636static int
1637mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1638{
1639 int ret;
1640
1641 /* if the stream has already been started, don't start it again */
1642 if (stream->state != AMPDU_STREAM_NEW)
1643 return 0;
1644 ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1645 if (ret)
1646 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1647 "%d\n", stream->sta->addr, stream->tid, ret);
1648 else
1649 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1650 stream->sta->addr, stream->tid);
1651 return ret;
1652}
1653
1654static void
1655mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1656{
1657 wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1658 stream->tid);
1659 memset(stream, 0, sizeof(*stream));
1660}
1661
1662static struct mwl8k_ampdu_stream *
1663mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1664{
1665 struct mwl8k_priv *priv = hw->priv;
1666 int i;
1667
1668 for (i = 0 ; i < priv->num_ampdu_queues; i++) {
1669 struct mwl8k_ampdu_stream *stream;
1670 stream = &priv->ampdu[i];
1671 if (stream->state == AMPDU_NO_STREAM)
1672 continue;
1673 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1674 stream->tid == tid)
1675 return stream;
1676 }
1677 return NULL;
1678}
1679
Johannes Berg7bb45682011-02-24 14:42:06 +01001680static void
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001681mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1682{
1683 struct mwl8k_priv *priv = hw->priv;
1684 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001685 struct mwl8k_vif *mwl8k_vif;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001686 struct ieee80211_sta *sta;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001687 struct ieee80211_hdr *wh;
1688 struct mwl8k_tx_queue *txq;
1689 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001690 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001691 u32 txstatus;
1692 u8 txdatarate;
1693 u16 qos;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001694 int txpriority;
1695 u8 tid = 0;
1696 struct mwl8k_ampdu_stream *stream = NULL;
1697 bool start_ba_session = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001698
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001699 wh = (struct ieee80211_hdr *)skb->data;
1700 if (ieee80211_is_data_qos(wh->frame_control))
1701 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1702 else
1703 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001704
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001705 if (priv->ap_fw)
1706 mwl8k_encapsulate_tx_frame(skb);
1707 else
1708 mwl8k_add_dma_header(skb, 0);
1709
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001710 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001711
1712 tx_info = IEEE80211_SKB_CB(skb);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001713 sta = tx_info->control.sta;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001714 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001715
1716 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001717 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001718 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1719 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001720 }
1721
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001722 /* Setup firmware control bit fields for each frame type. */
1723 txstatus = 0;
1724 txdatarate = 0;
1725 if (ieee80211_is_mgmt(wh->frame_control) ||
1726 ieee80211_is_ctl(wh->frame_control)) {
1727 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001728 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001729 } else if (ieee80211_is_data(wh->frame_control)) {
1730 txdatarate = 1;
1731 if (is_multicast_ether_addr(wh->addr1))
1732 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1733
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001734 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001735 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001736 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001737 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001738 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001739 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001740
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001741 txpriority = index;
1742
1743 if (ieee80211_is_data_qos(wh->frame_control) &&
1744 skb->protocol != cpu_to_be16(ETH_P_PAE) &&
1745 sta->ht_cap.ht_supported && priv->ap_fw) {
1746 tid = qos & 0xf;
1747 spin_lock(&priv->stream_lock);
1748 stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1749 if (stream != NULL) {
1750 if (stream->state == AMPDU_STREAM_ACTIVE) {
1751 txpriority = stream->txq_idx;
1752 index = stream->txq_idx;
1753 } else if (stream->state == AMPDU_STREAM_NEW) {
1754 /* We get here if the driver sends us packets
1755 * after we've initiated a stream, but before
1756 * our ampdu_action routine has been called
1757 * with IEEE80211_AMPDU_TX_START to get the SSN
1758 * for the ADDBA request. So this packet can
1759 * go out with no risk of sequence number
1760 * mismatch. No special handling is required.
1761 */
1762 } else {
1763 /* Drop packets that would go out after the
1764 * ADDBA request was sent but before the ADDBA
1765 * response is received. If we don't do this,
1766 * the recipient would probably receive it
1767 * after the ADDBA request with SSN 0. This
1768 * will cause the recipient's BA receive window
1769 * to shift, which would cause the subsequent
1770 * packets in the BA stream to be discarded.
1771 * mac80211 queues our packets for us in this
1772 * case, so this is really just a safety check.
1773 */
1774 wiphy_warn(hw->wiphy,
1775 "Cannot send packet while ADDBA "
1776 "dialog is underway.\n");
1777 spin_unlock(&priv->stream_lock);
1778 dev_kfree_skb(skb);
1779 return;
1780 }
1781 } else {
1782 /* Defer calling mwl8k_start_stream so that the current
1783 * skb can go out before the ADDBA request. This
1784 * prevents sequence number mismatch at the recepient
1785 * as described above.
1786 */
1787 stream = mwl8k_add_stream(hw, sta, tid);
1788 if (stream != NULL)
1789 start_ba_session = true;
1790 }
1791 spin_unlock(&priv->stream_lock);
1792 }
1793
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001794 dma = pci_map_single(priv->pdev, skb->data,
1795 skb->len, PCI_DMA_TODEVICE);
1796
1797 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001798 wiphy_debug(hw->wiphy,
1799 "failed to dma map skb, dropping TX frame.\n");
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001800 if (start_ba_session) {
1801 spin_lock(&priv->stream_lock);
1802 mwl8k_remove_stream(hw, stream);
1803 spin_unlock(&priv->stream_lock);
1804 }
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001805 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001806 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001807 }
1808
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001809 spin_lock_bh(&priv->tx_lock);
1810
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001811 txq = priv->txq + index;
1812
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001813 if (index >= MWL8K_TX_WMM_QUEUES && txq->len >= MWL8K_TX_DESCS) {
1814 /* This is the case in which the tx packet is destined for an
1815 * AMPDU queue and that AMPDU queue is full. Because we don't
1816 * start and stop the AMPDU queues, we must drop these packets.
1817 */
1818 dev_kfree_skb(skb);
1819 spin_unlock_bh(&priv->tx_lock);
1820 return;
1821 }
1822
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001823 BUG_ON(txq->skb[txq->tail] != NULL);
1824 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001825
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001826 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001827 tx->data_rate = txdatarate;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001828 tx->tx_priority = txpriority;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001829 tx->qos_control = cpu_to_le16(qos);
1830 tx->pkt_phys_addr = cpu_to_le32(dma);
1831 tx->pkt_len = cpu_to_le16(skb->len);
1832 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001833 if (!priv->ap_fw && tx_info->control.sta != NULL)
1834 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1835 else
1836 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001837 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001838 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1839
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001840 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001841 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001842
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001843 txq->tail++;
1844 if (txq->tail == MWL8K_TX_DESCS)
1845 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001846
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001847 if (txq->head == txq->tail && index < MWL8K_TX_WMM_QUEUES)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001848 ieee80211_stop_queue(hw, index);
1849
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001850 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001851
1852 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001853
1854 /* Initiate the ampdu session here */
1855 if (start_ba_session) {
1856 spin_lock(&priv->stream_lock);
1857 if (mwl8k_start_stream(hw, stream))
1858 mwl8k_remove_stream(hw, stream);
1859 spin_unlock(&priv->stream_lock);
1860 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001861}
1862
1863
1864/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001865 * Firmware access.
1866 *
1867 * We have the following requirements for issuing firmware commands:
1868 * - Some commands require that the packet transmit path is idle when
1869 * the command is issued. (For simplicity, we'll just quiesce the
1870 * transmit path for every command.)
1871 * - There are certain sequences of commands that need to be issued to
1872 * the hardware sequentially, with no other intervening commands.
1873 *
1874 * This leads to an implementation of a "firmware lock" as a mutex that
1875 * can be taken recursively, and which is taken by both the low-level
1876 * command submission function (mwl8k_post_cmd) as well as any users of
1877 * that function that require issuing of an atomic sequence of commands,
1878 * and quiesces the transmit path whenever it's taken.
1879 */
1880static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1881{
1882 struct mwl8k_priv *priv = hw->priv;
1883
1884 if (priv->fw_mutex_owner != current) {
1885 int rc;
1886
1887 mutex_lock(&priv->fw_mutex);
1888 ieee80211_stop_queues(hw);
1889
1890 rc = mwl8k_tx_wait_empty(hw);
1891 if (rc) {
1892 ieee80211_wake_queues(hw);
1893 mutex_unlock(&priv->fw_mutex);
1894
1895 return rc;
1896 }
1897
1898 priv->fw_mutex_owner = current;
1899 }
1900
1901 priv->fw_mutex_depth++;
1902
1903 return 0;
1904}
1905
1906static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1907{
1908 struct mwl8k_priv *priv = hw->priv;
1909
1910 if (!--priv->fw_mutex_depth) {
1911 ieee80211_wake_queues(hw);
1912 priv->fw_mutex_owner = NULL;
1913 mutex_unlock(&priv->fw_mutex);
1914 }
1915}
1916
1917
1918/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919 * Command processing.
1920 */
1921
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001922/* Timeout firmware commands after 10s */
1923#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001924
1925static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1926{
1927 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1928 struct mwl8k_priv *priv = hw->priv;
1929 void __iomem *regs = priv->regs;
1930 dma_addr_t dma_addr;
1931 unsigned int dma_size;
1932 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001933 unsigned long timeout = 0;
1934 u8 buf[32];
1935
John W. Linvilleb6037422010-07-20 13:55:00 -04001936 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001937 dma_size = le16_to_cpu(cmd->length);
1938 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1939 PCI_DMA_BIDIRECTIONAL);
1940 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1941 return -ENOMEM;
1942
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001943 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001944 if (rc) {
1945 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1946 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001947 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001948 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001949
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001950 priv->hostcmd_wait = &cmd_wait;
1951 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1952 iowrite32(MWL8K_H2A_INT_DOORBELL,
1953 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1954 iowrite32(MWL8K_H2A_INT_DUMMY,
1955 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001956
1957 timeout = wait_for_completion_timeout(&cmd_wait,
1958 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1959
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001960 priv->hostcmd_wait = NULL;
1961
1962 mwl8k_fw_unlock(hw);
1963
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001964 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1965 PCI_DMA_BIDIRECTIONAL);
1966
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001967 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001968 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001969 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1970 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001971 rc = -ETIMEDOUT;
1972 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001973 int ms;
1974
1975 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1976
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001977 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001978 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001979 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001980 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1981 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001982 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001983 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001984 mwl8k_cmd_name(cmd->code,
1985 buf, sizeof(buf)),
1986 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001987 }
1988
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001989 return rc;
1990}
1991
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001992static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1993 struct ieee80211_vif *vif,
1994 struct mwl8k_cmd_pkt *cmd)
1995{
1996 if (vif != NULL)
1997 cmd->macid = MWL8K_VIF(vif)->macid;
1998 return mwl8k_post_cmd(hw, cmd);
1999}
2000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002001/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002002 * Setup code shared between STA and AP firmware images.
2003 */
2004static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2005{
2006 struct mwl8k_priv *priv = hw->priv;
2007
2008 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2009 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2010
2011 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2012 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2013
2014 priv->band_24.band = IEEE80211_BAND_2GHZ;
2015 priv->band_24.channels = priv->channels_24;
2016 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2017 priv->band_24.bitrates = priv->rates_24;
2018 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2019
2020 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
2021}
2022
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01002023static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2024{
2025 struct mwl8k_priv *priv = hw->priv;
2026
2027 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2028 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2029
2030 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2031 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2032
2033 priv->band_50.band = IEEE80211_BAND_5GHZ;
2034 priv->band_50.channels = priv->channels_50;
2035 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2036 priv->band_50.bitrates = priv->rates_50;
2037 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2038
2039 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
2040}
2041
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002042/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002043 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002044 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002045struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002046 struct mwl8k_cmd_pkt header;
2047 __u8 hw_rev;
2048 __u8 host_interface;
2049 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002050 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002051 __le16 region_code;
2052 __le32 fw_rev;
2053 __le32 ps_cookie;
2054 __le32 caps;
2055 __u8 mcs_bitmap[16];
2056 __le32 rx_queue_ptr;
2057 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002058 __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002059 __le32 caps2;
2060 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002061 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002062} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002063
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002064#define MWL8K_CAP_MAX_AMSDU 0x20000000
2065#define MWL8K_CAP_GREENFIELD 0x08000000
2066#define MWL8K_CAP_AMPDU 0x04000000
2067#define MWL8K_CAP_RX_STBC 0x01000000
2068#define MWL8K_CAP_TX_STBC 0x00800000
2069#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
2070#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
2071#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
2072#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
2073#define MWL8K_CAP_DELAY_BA 0x00003000
2074#define MWL8K_CAP_MIMO 0x00000200
2075#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01002076#define MWL8K_CAP_BAND_MASK 0x00000007
2077#define MWL8K_CAP_5GHZ 0x00000004
2078#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002079
Lennert Buytenhek06953232010-01-12 13:49:41 +01002080static void
2081mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2082 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002083{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002084 int rx_streams;
2085 int tx_streams;
2086
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002087 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002088
2089 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002090 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002091 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002092 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002093 if (cap & MWL8K_CAP_AMPDU) {
2094 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002095 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2096 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002097 }
2098 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002099 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002100 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002101 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002102 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002103 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002104 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002105 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002106 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002107 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002108 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002109 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002110
2111 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2112 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2113
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002114 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002115 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002116 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002117 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002118 band->ht_cap.mcs.rx_mask[2] = 0xff;
2119 band->ht_cap.mcs.rx_mask[4] = 0x01;
2120 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002121
2122 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002123 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2124 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002125 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2126 }
2127}
2128
Lennert Buytenhek06953232010-01-12 13:49:41 +01002129static void
2130mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2131{
2132 struct mwl8k_priv *priv = hw->priv;
2133
2134 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2135 mwl8k_setup_2ghz_band(hw);
2136 if (caps & MWL8K_CAP_MIMO)
2137 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2138 }
2139
2140 if (caps & MWL8K_CAP_5GHZ) {
2141 mwl8k_setup_5ghz_band(hw);
2142 if (caps & MWL8K_CAP_MIMO)
2143 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2144 }
2145}
2146
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002147static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002148{
2149 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002150 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002151 int rc;
2152 int i;
2153
2154 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2155 if (cmd == NULL)
2156 return -ENOMEM;
2157
2158 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2159 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2160
2161 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2162 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002163 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002164 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2165 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002166 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002167 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002168 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002169
2170 rc = mwl8k_post_cmd(hw, &cmd->header);
2171
2172 if (!rc) {
2173 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2174 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002175 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002176 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002177 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002178 priv->ap_macids_supported = 0x00000000;
2179 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002180 }
2181
2182 kfree(cmd);
2183 return rc;
2184}
2185
2186/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002187 * CMD_GET_HW_SPEC (AP version).
2188 */
2189struct mwl8k_cmd_get_hw_spec_ap {
2190 struct mwl8k_cmd_pkt header;
2191 __u8 hw_rev;
2192 __u8 host_interface;
2193 __le16 num_wcb;
2194 __le16 num_mcaddrs;
2195 __u8 perm_addr[ETH_ALEN];
2196 __le16 region_code;
2197 __le16 num_antenna;
2198 __le32 fw_rev;
2199 __le32 wcbbase0;
2200 __le32 rxwrptr;
2201 __le32 rxrdptr;
2202 __le32 ps_cookie;
2203 __le32 wcbbase1;
2204 __le32 wcbbase2;
2205 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002206 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002207 __le32 caps;
2208 __le32 num_of_ampdu_queues;
2209 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002210} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002211
2212static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2213{
2214 struct mwl8k_priv *priv = hw->priv;
2215 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002216 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002217 u32 api_version;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002218
2219 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2220 if (cmd == NULL)
2221 return -ENOMEM;
2222
2223 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2224 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2225
2226 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2227 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2228
2229 rc = mwl8k_post_cmd(hw, &cmd->header);
2230
2231 if (!rc) {
2232 int off;
2233
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002234 api_version = le32_to_cpu(cmd->fw_api_version);
2235 if (priv->device_info->fw_api_ap != api_version) {
2236 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2237 " Expected %d got %d.\n", MWL8K_NAME,
2238 priv->device_info->part_name,
2239 priv->device_info->fw_api_ap,
2240 api_version);
2241 rc = -EINVAL;
2242 goto done;
2243 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002244 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2245 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2246 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2247 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002248 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002249 priv->ap_macids_supported = 0x000000ff;
2250 priv->sta_macids_supported = 0x00000000;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002251 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2252 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2253 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2254 " but we only support %d.\n",
2255 priv->num_ampdu_queues,
2256 MWL8K_MAX_AMPDU_QUEUES);
2257 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2258 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002259 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002260 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002261
2262 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002263 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002264
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002265 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2266 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2267 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2268 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002269
2270 for (i = 0; i < priv->num_ampdu_queues; i++)
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002271 priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002272 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002273 }
2274
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002275done:
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002276 kfree(cmd);
2277 return rc;
2278}
2279
2280/*
2281 * CMD_SET_HW_SPEC.
2282 */
2283struct mwl8k_cmd_set_hw_spec {
2284 struct mwl8k_cmd_pkt header;
2285 __u8 hw_rev;
2286 __u8 host_interface;
2287 __le16 num_mcaddrs;
2288 __u8 perm_addr[ETH_ALEN];
2289 __le16 region_code;
2290 __le32 fw_rev;
2291 __le32 ps_cookie;
2292 __le32 caps;
2293 __le32 rx_queue_ptr;
2294 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002295 __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002296 __le32 flags;
2297 __le32 num_tx_desc_per_queue;
2298 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002299} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002300
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002301/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2302 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2303 * the packets that are queued for more than 500ms, will be dropped in the
2304 * hardware. This helps minimizing the issues caused due to head-of-line
2305 * blocking where a slow client can hog the bandwidth and affect traffic to a
2306 * faster client.
2307 */
2308#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002309#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2310#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2311#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002312
2313static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2314{
2315 struct mwl8k_priv *priv = hw->priv;
2316 struct mwl8k_cmd_set_hw_spec *cmd;
2317 int rc;
2318 int i;
2319
2320 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2321 if (cmd == NULL)
2322 return -ENOMEM;
2323
2324 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2325 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2326
2327 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2328 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002329 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002330
2331 /*
2332 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2333 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2334 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2335 * priority is interpreted the right way in firmware.
2336 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002337 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2338 int j = mwl8k_tx_queues(priv) - 1 - i;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002339 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2340 }
2341
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002342 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2343 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2344 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002345 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2346 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2347
2348 rc = mwl8k_post_cmd(hw, &cmd->header);
2349 kfree(cmd);
2350
2351 return rc;
2352}
2353
2354/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002355 * CMD_MAC_MULTICAST_ADR.
2356 */
2357struct mwl8k_cmd_mac_multicast_adr {
2358 struct mwl8k_cmd_pkt header;
2359 __le16 action;
2360 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002361 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002362};
2363
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002364#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2365#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2366#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2367#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002368
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002369static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002370__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002371 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002372{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002373 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002374 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002375 int size;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002376 int mc_count = 0;
2377
2378 if (mc_list)
2379 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002380
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002381 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002382 allmulti = 1;
2383 mc_count = 0;
2384 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002385
2386 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2387
2388 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002389 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002390 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002391
2392 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2393 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002394 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2395 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002396
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002397 if (allmulti) {
2398 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2399 } else if (mc_count) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00002400 struct netdev_hw_addr *ha;
2401 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002402
2403 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2404 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad2010-04-01 21:22:57 +00002405 netdev_hw_addr_list_for_each(ha, mc_list) {
2406 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002407 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002408 }
2409
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002410 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002411}
2412
2413/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002414 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002415 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002416struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002417 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002418 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002419} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002420
2421#define MWL8K_STAT_ACK_FAILURE 9
2422#define MWL8K_STAT_RTS_FAILURE 12
2423#define MWL8K_STAT_FCS_ERROR 24
2424#define MWL8K_STAT_RTS_SUCCESS 11
2425
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002426static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2427 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002428{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002429 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002430 int rc;
2431
2432 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2433 if (cmd == NULL)
2434 return -ENOMEM;
2435
2436 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2437 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002438
2439 rc = mwl8k_post_cmd(hw, &cmd->header);
2440 if (!rc) {
2441 stats->dot11ACKFailureCount =
2442 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2443 stats->dot11RTSFailureCount =
2444 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2445 stats->dot11FCSErrorCount =
2446 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2447 stats->dot11RTSSuccessCount =
2448 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2449 }
2450 kfree(cmd);
2451
2452 return rc;
2453}
2454
2455/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002456 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002457 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002458struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002459 struct mwl8k_cmd_pkt header;
2460 __le16 action;
2461 __le16 control;
2462 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002463} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002464
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002465static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002466mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002467{
2468 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002469 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002470 int rc;
2471
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002472 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002473 return 0;
2474
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002475 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2476 if (cmd == NULL)
2477 return -ENOMEM;
2478
2479 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2480 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2481 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002482 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002483 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2484
2485 rc = mwl8k_post_cmd(hw, &cmd->header);
2486 kfree(cmd);
2487
2488 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002489 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002490
2491 return rc;
2492}
2493
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002494static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002495{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002496 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002497}
2498
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002499static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002500{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002501 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002502}
2503
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002504static int
2505mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2506{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002507 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002508
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002509 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002510
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002511 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002512}
2513
2514/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002515 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002516 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002517#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002518
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002519struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002520 struct mwl8k_cmd_pkt header;
2521 __le16 action;
2522 __le16 support_level;
2523 __le16 current_level;
2524 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002525 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002526} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002527
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002528static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002529{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002530 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002531 int rc;
2532
2533 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2534 if (cmd == NULL)
2535 return -ENOMEM;
2536
2537 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2538 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2539 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2540 cmd->support_level = cpu_to_le16(dBm);
2541
2542 rc = mwl8k_post_cmd(hw, &cmd->header);
2543 kfree(cmd);
2544
2545 return rc;
2546}
2547
2548/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002549 * CMD_TX_POWER.
2550 */
2551#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2552
2553struct mwl8k_cmd_tx_power {
2554 struct mwl8k_cmd_pkt header;
2555 __le16 action;
2556 __le16 band;
2557 __le16 channel;
2558 __le16 bw;
2559 __le16 sub_ch;
2560 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2561} __attribute__((packed));
2562
2563static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2564 struct ieee80211_conf *conf,
2565 unsigned short pwr)
2566{
2567 struct ieee80211_channel *channel = conf->channel;
2568 struct mwl8k_cmd_tx_power *cmd;
2569 int rc;
2570 int i;
2571
2572 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2573 if (cmd == NULL)
2574 return -ENOMEM;
2575
2576 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2577 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2578 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2579
2580 if (channel->band == IEEE80211_BAND_2GHZ)
2581 cmd->band = cpu_to_le16(0x1);
2582 else if (channel->band == IEEE80211_BAND_5GHZ)
2583 cmd->band = cpu_to_le16(0x4);
2584
2585 cmd->channel = channel->hw_value;
2586
2587 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2588 conf->channel_type == NL80211_CHAN_HT20) {
2589 cmd->bw = cpu_to_le16(0x2);
2590 } else {
2591 cmd->bw = cpu_to_le16(0x4);
2592 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2593 cmd->sub_ch = cpu_to_le16(0x3);
2594 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2595 cmd->sub_ch = cpu_to_le16(0x1);
2596 }
2597
2598 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2599 cmd->power_level_list[i] = cpu_to_le16(pwr);
2600
2601 rc = mwl8k_post_cmd(hw, &cmd->header);
2602 kfree(cmd);
2603
2604 return rc;
2605}
2606
2607/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002608 * CMD_RF_ANTENNA.
2609 */
2610struct mwl8k_cmd_rf_antenna {
2611 struct mwl8k_cmd_pkt header;
2612 __le16 antenna;
2613 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002614} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002615
2616#define MWL8K_RF_ANTENNA_RX 1
2617#define MWL8K_RF_ANTENNA_TX 2
2618
2619static int
2620mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2621{
2622 struct mwl8k_cmd_rf_antenna *cmd;
2623 int rc;
2624
2625 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2626 if (cmd == NULL)
2627 return -ENOMEM;
2628
2629 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2630 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2631 cmd->antenna = cpu_to_le16(antenna);
2632 cmd->mode = cpu_to_le16(mask);
2633
2634 rc = mwl8k_post_cmd(hw, &cmd->header);
2635 kfree(cmd);
2636
2637 return rc;
2638}
2639
2640/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002641 * CMD_SET_BEACON.
2642 */
2643struct mwl8k_cmd_set_beacon {
2644 struct mwl8k_cmd_pkt header;
2645 __le16 beacon_len;
2646 __u8 beacon[0];
2647};
2648
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002649static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2650 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002651{
2652 struct mwl8k_cmd_set_beacon *cmd;
2653 int rc;
2654
2655 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2656 if (cmd == NULL)
2657 return -ENOMEM;
2658
2659 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2660 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2661 cmd->beacon_len = cpu_to_le16(len);
2662 memcpy(cmd->beacon, beacon, len);
2663
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002664 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002665 kfree(cmd);
2666
2667 return rc;
2668}
2669
2670/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002671 * CMD_SET_PRE_SCAN.
2672 */
2673struct mwl8k_cmd_set_pre_scan {
2674 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002675} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002676
2677static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2678{
2679 struct mwl8k_cmd_set_pre_scan *cmd;
2680 int rc;
2681
2682 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2683 if (cmd == NULL)
2684 return -ENOMEM;
2685
2686 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2687 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2688
2689 rc = mwl8k_post_cmd(hw, &cmd->header);
2690 kfree(cmd);
2691
2692 return rc;
2693}
2694
2695/*
2696 * CMD_SET_POST_SCAN.
2697 */
2698struct mwl8k_cmd_set_post_scan {
2699 struct mwl8k_cmd_pkt header;
2700 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002701 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002702} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002703
2704static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002705mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002706{
2707 struct mwl8k_cmd_set_post_scan *cmd;
2708 int rc;
2709
2710 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2711 if (cmd == NULL)
2712 return -ENOMEM;
2713
2714 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2715 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2716 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002717 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002718
2719 rc = mwl8k_post_cmd(hw, &cmd->header);
2720 kfree(cmd);
2721
2722 return rc;
2723}
2724
2725/*
2726 * CMD_SET_RF_CHANNEL.
2727 */
2728struct mwl8k_cmd_set_rf_channel {
2729 struct mwl8k_cmd_pkt header;
2730 __le16 action;
2731 __u8 current_channel;
2732 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002733} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002734
2735static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002736 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002737{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002738 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002739 struct mwl8k_cmd_set_rf_channel *cmd;
2740 int rc;
2741
2742 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2743 if (cmd == NULL)
2744 return -ENOMEM;
2745
2746 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2747 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2748 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2749 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002750
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002751 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002752 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002753 else if (channel->band == IEEE80211_BAND_5GHZ)
2754 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002755
2756 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2757 conf->channel_type == NL80211_CHAN_HT20)
2758 cmd->channel_flags |= cpu_to_le32(0x00000080);
2759 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2760 cmd->channel_flags |= cpu_to_le32(0x000001900);
2761 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2762 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002763
2764 rc = mwl8k_post_cmd(hw, &cmd->header);
2765 kfree(cmd);
2766
2767 return rc;
2768}
2769
2770/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002771 * CMD_SET_AID.
2772 */
2773#define MWL8K_FRAME_PROT_DISABLED 0x00
2774#define MWL8K_FRAME_PROT_11G 0x07
2775#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2776#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2777
2778struct mwl8k_cmd_update_set_aid {
2779 struct mwl8k_cmd_pkt header;
2780 __le16 aid;
2781
2782 /* AP's MAC address (BSSID) */
2783 __u8 bssid[ETH_ALEN];
2784 __le16 protection_mode;
2785 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002786} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002787
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002788static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2789{
2790 int i;
2791 int j;
2792
2793 /*
2794 * Clear nonstandard rates 4 and 13.
2795 */
2796 mask &= 0x1fef;
2797
2798 for (i = 0, j = 0; i < 14; i++) {
2799 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002800 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002801 }
2802}
2803
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002804static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002805mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2806 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002807{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002808 struct mwl8k_cmd_update_set_aid *cmd;
2809 u16 prot_mode;
2810 int rc;
2811
2812 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2813 if (cmd == NULL)
2814 return -ENOMEM;
2815
2816 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2817 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002818 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002819 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002820
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002821 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002822 prot_mode = MWL8K_FRAME_PROT_11G;
2823 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002824 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002825 IEEE80211_HT_OP_MODE_PROTECTION) {
2826 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2827 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2828 break;
2829 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2830 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2831 break;
2832 default:
2833 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2834 break;
2835 }
2836 }
2837 cmd->protection_mode = cpu_to_le16(prot_mode);
2838
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002839 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002840
2841 rc = mwl8k_post_cmd(hw, &cmd->header);
2842 kfree(cmd);
2843
2844 return rc;
2845}
2846
2847/*
2848 * CMD_SET_RATE.
2849 */
2850struct mwl8k_cmd_set_rate {
2851 struct mwl8k_cmd_pkt header;
2852 __u8 legacy_rates[14];
2853
2854 /* Bitmap for supported MCS codes. */
2855 __u8 mcs_set[16];
2856 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002857} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002858
2859static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002860mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002861 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002862{
2863 struct mwl8k_cmd_set_rate *cmd;
2864 int rc;
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_SET_RATE);
2871 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002872 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002873 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002874
2875 rc = mwl8k_post_cmd(hw, &cmd->header);
2876 kfree(cmd);
2877
2878 return rc;
2879}
2880
2881/*
2882 * CMD_FINALIZE_JOIN.
2883 */
2884#define MWL8K_FJ_BEACON_MAXLEN 128
2885
2886struct mwl8k_cmd_finalize_join {
2887 struct mwl8k_cmd_pkt header;
2888 __le32 sleep_interval; /* Number of beacon periods to sleep */
2889 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002890} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002891
2892static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2893 int framelen, int dtim)
2894{
2895 struct mwl8k_cmd_finalize_join *cmd;
2896 struct ieee80211_mgmt *payload = frame;
2897 int payload_len;
2898 int rc;
2899
2900 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2901 if (cmd == NULL)
2902 return -ENOMEM;
2903
2904 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2905 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2906 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2907
2908 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2909 if (payload_len < 0)
2910 payload_len = 0;
2911 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2912 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2913
2914 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2915
2916 rc = mwl8k_post_cmd(hw, &cmd->header);
2917 kfree(cmd);
2918
2919 return rc;
2920}
2921
2922/*
2923 * CMD_SET_RTS_THRESHOLD.
2924 */
2925struct mwl8k_cmd_set_rts_threshold {
2926 struct mwl8k_cmd_pkt header;
2927 __le16 action;
2928 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002929} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002930
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002931static int
2932mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002933{
2934 struct mwl8k_cmd_set_rts_threshold *cmd;
2935 int rc;
2936
2937 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2938 if (cmd == NULL)
2939 return -ENOMEM;
2940
2941 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2942 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002943 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2944 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002945
2946 rc = mwl8k_post_cmd(hw, &cmd->header);
2947 kfree(cmd);
2948
2949 return rc;
2950}
2951
2952/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002953 * CMD_SET_SLOT.
2954 */
2955struct mwl8k_cmd_set_slot {
2956 struct mwl8k_cmd_pkt header;
2957 __le16 action;
2958 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002959} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002960
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002961static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002962{
2963 struct mwl8k_cmd_set_slot *cmd;
2964 int rc;
2965
2966 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2967 if (cmd == NULL)
2968 return -ENOMEM;
2969
2970 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2971 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2972 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002973 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002974
2975 rc = mwl8k_post_cmd(hw, &cmd->header);
2976 kfree(cmd);
2977
2978 return rc;
2979}
2980
2981/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002982 * CMD_SET_EDCA_PARAMS.
2983 */
2984struct mwl8k_cmd_set_edca_params {
2985 struct mwl8k_cmd_pkt header;
2986
2987 /* See MWL8K_SET_EDCA_XXX below */
2988 __le16 action;
2989
2990 /* TX opportunity in units of 32 us */
2991 __le16 txop;
2992
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002993 union {
2994 struct {
2995 /* Log exponent of max contention period: 0...15 */
2996 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002997
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002998 /* Log exponent of min contention period: 0...15 */
2999 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003000
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003001 /* Adaptive interframe spacing in units of 32us */
3002 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003003
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003004 /* TX queue to configure */
3005 __u8 txq;
3006 } ap;
3007 struct {
3008 /* Log exponent of max contention period: 0...15 */
3009 __u8 log_cw_max;
3010
3011 /* Log exponent of min contention period: 0...15 */
3012 __u8 log_cw_min;
3013
3014 /* Adaptive interframe spacing in units of 32us */
3015 __u8 aifs;
3016
3017 /* TX queue to configure */
3018 __u8 txq;
3019 } sta;
3020 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003021} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003022
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003023#define MWL8K_SET_EDCA_CW 0x01
3024#define MWL8K_SET_EDCA_TXOP 0x02
3025#define MWL8K_SET_EDCA_AIFS 0x04
3026
3027#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
3028 MWL8K_SET_EDCA_TXOP | \
3029 MWL8K_SET_EDCA_AIFS)
3030
3031static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003032mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3033 __u16 cw_min, __u16 cw_max,
3034 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003035{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003036 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003037 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003038 int rc;
3039
3040 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3041 if (cmd == NULL)
3042 return -ENOMEM;
3043
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003044 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3045 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003046 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3047 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003048 if (priv->ap_fw) {
3049 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3050 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3051 cmd->ap.aifs = aifs;
3052 cmd->ap.txq = qnum;
3053 } else {
3054 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3055 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3056 cmd->sta.aifs = aifs;
3057 cmd->sta.txq = qnum;
3058 }
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_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003068 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003069struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003070 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003071 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003072} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003073
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003074static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003075{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003076 struct mwl8k_priv *priv = hw->priv;
3077 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003078 int rc;
3079
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003080 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3081 if (cmd == NULL)
3082 return -ENOMEM;
3083
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003084 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003085 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003086 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003087
3088 rc = mwl8k_post_cmd(hw, &cmd->header);
3089 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01003090
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003091 if (!rc)
3092 priv->wmm_enabled = enable;
3093
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003094 return rc;
3095}
3096
3097/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003098 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003099 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003100struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003101 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003102 __le32 action;
3103 __u8 rx_antenna_map;
3104 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003105} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003106
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003107static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003108{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003109 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003110 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003111
3112 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3113 if (cmd == NULL)
3114 return -ENOMEM;
3115
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003116 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003117 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003118 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3119 cmd->rx_antenna_map = rx;
3120 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003121
3122 rc = mwl8k_post_cmd(hw, &cmd->header);
3123 kfree(cmd);
3124
3125 return rc;
3126}
3127
3128/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003129 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003130 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003131struct mwl8k_cmd_use_fixed_rate_sta {
3132 struct mwl8k_cmd_pkt header;
3133 __le32 action;
3134 __le32 allow_rate_drop;
3135 __le32 num_rates;
3136 struct {
3137 __le32 is_ht_rate;
3138 __le32 enable_retry;
3139 __le32 rate;
3140 __le32 retry_count;
3141 } rate_entry[8];
3142 __le32 rate_type;
3143 __le32 reserved1;
3144 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003145} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003146
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003147#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003148#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003149
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003150static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003151{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003152 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003153 int rc;
3154
3155 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3156 if (cmd == NULL)
3157 return -ENOMEM;
3158
3159 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3160 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003161 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3162 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003163
3164 rc = mwl8k_post_cmd(hw, &cmd->header);
3165 kfree(cmd);
3166
3167 return rc;
3168}
3169
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003170/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003171 * CMD_USE_FIXED_RATE (AP version).
3172 */
3173struct mwl8k_cmd_use_fixed_rate_ap {
3174 struct mwl8k_cmd_pkt header;
3175 __le32 action;
3176 __le32 allow_rate_drop;
3177 __le32 num_rates;
3178 struct mwl8k_rate_entry_ap {
3179 __le32 is_ht_rate;
3180 __le32 enable_retry;
3181 __le32 rate;
3182 __le32 retry_count;
3183 } rate_entry[4];
3184 u8 multicast_rate;
3185 u8 multicast_rate_type;
3186 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003187} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003188
3189static int
3190mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3191{
3192 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3193 int rc;
3194
3195 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3196 if (cmd == NULL)
3197 return -ENOMEM;
3198
3199 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3200 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3201 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3202 cmd->multicast_rate = mcast;
3203 cmd->management_rate = mgmt;
3204
3205 rc = mwl8k_post_cmd(hw, &cmd->header);
3206 kfree(cmd);
3207
3208 return rc;
3209}
3210
3211/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003212 * CMD_ENABLE_SNIFFER.
3213 */
3214struct mwl8k_cmd_enable_sniffer {
3215 struct mwl8k_cmd_pkt header;
3216 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003217} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003218
3219static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3220{
3221 struct mwl8k_cmd_enable_sniffer *cmd;
3222 int rc;
3223
3224 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3225 if (cmd == NULL)
3226 return -ENOMEM;
3227
3228 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3229 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3230 cmd->action = cpu_to_le32(!!enable);
3231
3232 rc = mwl8k_post_cmd(hw, &cmd->header);
3233 kfree(cmd);
3234
3235 return rc;
3236}
3237
3238/*
3239 * CMD_SET_MAC_ADDR.
3240 */
3241struct mwl8k_cmd_set_mac_addr {
3242 struct mwl8k_cmd_pkt header;
3243 union {
3244 struct {
3245 __le16 mac_type;
3246 __u8 mac_addr[ETH_ALEN];
3247 } mbss;
3248 __u8 mac_addr[ETH_ALEN];
3249 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003250} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003251
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003252#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3253#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3254#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3255#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003256
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003257static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3258 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003259{
3260 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003261 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003262 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003263 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003264 int rc;
3265
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003266 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3267 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3268 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3269 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3270 else
3271 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3272 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3273 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3274 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3275 else
3276 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3277 }
3278
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003279 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3280 if (cmd == NULL)
3281 return -ENOMEM;
3282
3283 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3284 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3285 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003286 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003287 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3288 } else {
3289 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3290 }
3291
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003292 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003293 kfree(cmd);
3294
3295 return rc;
3296}
3297
3298/*
3299 * CMD_SET_RATEADAPT_MODE.
3300 */
3301struct mwl8k_cmd_set_rate_adapt_mode {
3302 struct mwl8k_cmd_pkt header;
3303 __le16 action;
3304 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003305} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003306
3307static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3308{
3309 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3310 int rc;
3311
3312 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3313 if (cmd == NULL)
3314 return -ENOMEM;
3315
3316 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3317 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3318 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3319 cmd->mode = cpu_to_le16(mode);
3320
3321 rc = mwl8k_post_cmd(hw, &cmd->header);
3322 kfree(cmd);
3323
3324 return rc;
3325}
3326
3327/*
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07003328 * CMD_GET_WATCHDOG_BITMAP.
3329 */
3330struct mwl8k_cmd_get_watchdog_bitmap {
3331 struct mwl8k_cmd_pkt header;
3332 u8 bitmap;
3333} __packed;
3334
3335static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3336{
3337 struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3338 int rc;
3339
3340 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3341 if (cmd == NULL)
3342 return -ENOMEM;
3343
3344 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3345 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3346
3347 rc = mwl8k_post_cmd(hw, &cmd->header);
3348 if (!rc)
3349 *bitmap = cmd->bitmap;
3350
3351 kfree(cmd);
3352
3353 return rc;
3354}
3355
3356#define INVALID_BA 0xAA
3357static void mwl8k_watchdog_ba_events(struct work_struct *work)
3358{
3359 int rc;
3360 u8 bitmap = 0, stream_index;
3361 struct mwl8k_ampdu_stream *streams;
3362 struct mwl8k_priv *priv =
3363 container_of(work, struct mwl8k_priv, watchdog_ba_handle);
3364
3365 rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3366 if (rc)
3367 return;
3368
3369 if (bitmap == INVALID_BA)
3370 return;
3371
3372 /* the bitmap is the hw queue number. Map it to the ampdu queue. */
3373 stream_index = bitmap - MWL8K_TX_WMM_QUEUES;
3374
3375 BUG_ON(stream_index >= priv->num_ampdu_queues);
3376
3377 streams = &priv->ampdu[stream_index];
3378
3379 if (streams->state == AMPDU_STREAM_ACTIVE)
3380 ieee80211_stop_tx_ba_session(streams->sta, streams->tid);
3381
3382 return;
3383}
3384
3385
3386/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003387 * CMD_BSS_START.
3388 */
3389struct mwl8k_cmd_bss_start {
3390 struct mwl8k_cmd_pkt header;
3391 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003392} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003393
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003394static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3395 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003396{
3397 struct mwl8k_cmd_bss_start *cmd;
3398 int rc;
3399
3400 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3401 if (cmd == NULL)
3402 return -ENOMEM;
3403
3404 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3405 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3406 cmd->enable = cpu_to_le32(enable);
3407
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003408 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003409 kfree(cmd);
3410
3411 return rc;
3412}
3413
3414/*
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003415 * CMD_BASTREAM.
3416 */
3417
3418/*
3419 * UPSTREAM is tx direction
3420 */
3421#define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3422#define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3423
3424enum {
3425 MWL8K_BA_CREATE,
3426 MWL8K_BA_UPDATE,
3427 MWL8K_BA_DESTROY,
3428 MWL8K_BA_FLUSH,
3429 MWL8K_BA_CHECK,
3430} ba_stream_action_type;
3431
3432
3433struct mwl8k_create_ba_stream {
3434 __le32 flags;
3435 __le32 idle_thrs;
3436 __le32 bar_thrs;
3437 __le32 window_size;
3438 u8 peer_mac_addr[6];
3439 u8 dialog_token;
3440 u8 tid;
3441 u8 queue_id;
3442 u8 param_info;
3443 __le32 ba_context;
3444 u8 reset_seq_no_flag;
3445 __le16 curr_seq_no;
3446 u8 sta_src_mac_addr[6];
3447} __packed;
3448
3449struct mwl8k_destroy_ba_stream {
3450 __le32 flags;
3451 __le32 ba_context;
3452} __packed;
3453
3454struct mwl8k_cmd_bastream {
3455 struct mwl8k_cmd_pkt header;
3456 __le32 action;
3457 union {
3458 struct mwl8k_create_ba_stream create_params;
3459 struct mwl8k_destroy_ba_stream destroy_params;
3460 };
3461} __packed;
3462
3463static int
3464mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
3465{
3466 struct mwl8k_cmd_bastream *cmd;
3467 int rc;
3468
3469 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3470 if (cmd == NULL)
3471 return -ENOMEM;
3472
3473 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3474 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3475
3476 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3477
3478 cmd->create_params.queue_id = stream->idx;
3479 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3480 ETH_ALEN);
3481 cmd->create_params.tid = stream->tid;
3482
3483 cmd->create_params.flags =
3484 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3485 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3486
3487 rc = mwl8k_post_cmd(hw, &cmd->header);
3488
3489 kfree(cmd);
3490
3491 return rc;
3492}
3493
3494static int
3495mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3496 u8 buf_size)
3497{
3498 struct mwl8k_cmd_bastream *cmd;
3499 int rc;
3500
3501 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3502 if (cmd == NULL)
3503 return -ENOMEM;
3504
3505
3506 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3507 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3508
3509 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3510
3511 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3512 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3513 cmd->create_params.queue_id = stream->idx;
3514
3515 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3516 cmd->create_params.tid = stream->tid;
3517 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3518 cmd->create_params.reset_seq_no_flag = 1;
3519
3520 cmd->create_params.param_info =
3521 (stream->sta->ht_cap.ampdu_factor &
3522 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3523 ((stream->sta->ht_cap.ampdu_density << 2) &
3524 IEEE80211_HT_AMPDU_PARM_DENSITY);
3525
3526 cmd->create_params.flags =
3527 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3528 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3529
3530 rc = mwl8k_post_cmd(hw, &cmd->header);
3531
3532 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3533 stream->sta->addr, stream->tid);
3534 kfree(cmd);
3535
3536 return rc;
3537}
3538
3539static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3540 struct mwl8k_ampdu_stream *stream)
3541{
3542 struct mwl8k_cmd_bastream *cmd;
3543
3544 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3545 if (cmd == NULL)
3546 return;
3547
3548 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3549 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3550 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3551
3552 cmd->destroy_params.ba_context = cpu_to_le32(stream->idx);
3553 mwl8k_post_cmd(hw, &cmd->header);
3554
3555 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", stream->idx);
3556
3557 kfree(cmd);
3558}
3559
3560/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003561 * CMD_SET_NEW_STN.
3562 */
3563struct mwl8k_cmd_set_new_stn {
3564 struct mwl8k_cmd_pkt header;
3565 __le16 aid;
3566 __u8 mac_addr[6];
3567 __le16 stn_id;
3568 __le16 action;
3569 __le16 rsvd;
3570 __le32 legacy_rates;
3571 __u8 ht_rates[4];
3572 __le16 cap_info;
3573 __le16 ht_capabilities_info;
3574 __u8 mac_ht_param_info;
3575 __u8 rev;
3576 __u8 control_channel;
3577 __u8 add_channel;
3578 __le16 op_mode;
3579 __le16 stbc;
3580 __u8 add_qos_info;
3581 __u8 is_qos_sta;
3582 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003583} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003584
3585#define MWL8K_STA_ACTION_ADD 0
3586#define MWL8K_STA_ACTION_REMOVE 2
3587
3588static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3589 struct ieee80211_vif *vif,
3590 struct ieee80211_sta *sta)
3591{
3592 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003593 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003594 int rc;
3595
3596 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3597 if (cmd == NULL)
3598 return -ENOMEM;
3599
3600 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3601 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3602 cmd->aid = cpu_to_le16(sta->aid);
3603 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3604 cmd->stn_id = cpu_to_le16(sta->aid);
3605 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003606 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3607 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3608 else
3609 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3610 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003611 if (sta->ht_cap.ht_supported) {
3612 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3613 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3614 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3615 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3616 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3617 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3618 ((sta->ht_cap.ampdu_density & 7) << 2);
3619 cmd->is_qos_sta = 1;
3620 }
3621
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003622 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003623 kfree(cmd);
3624
3625 return rc;
3626}
3627
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003628static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3629 struct ieee80211_vif *vif)
3630{
3631 struct mwl8k_cmd_set_new_stn *cmd;
3632 int rc;
3633
3634 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3635 if (cmd == NULL)
3636 return -ENOMEM;
3637
3638 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3639 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3640 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3641
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003642 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003643 kfree(cmd);
3644
3645 return rc;
3646}
3647
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003648static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3649 struct ieee80211_vif *vif, u8 *addr)
3650{
3651 struct mwl8k_cmd_set_new_stn *cmd;
3652 int rc;
3653
3654 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3655 if (cmd == NULL)
3656 return -ENOMEM;
3657
3658 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3659 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3660 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3661 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3662
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003663 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003664 kfree(cmd);
3665
3666 return rc;
3667}
3668
3669/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003670 * CMD_UPDATE_ENCRYPTION.
3671 */
3672
3673#define MAX_ENCR_KEY_LENGTH 16
3674#define MIC_KEY_LENGTH 8
3675
3676struct mwl8k_cmd_update_encryption {
3677 struct mwl8k_cmd_pkt header;
3678
3679 __le32 action;
3680 __le32 reserved;
3681 __u8 mac_addr[6];
3682 __u8 encr_type;
3683
3684} __attribute__((packed));
3685
3686struct mwl8k_cmd_set_key {
3687 struct mwl8k_cmd_pkt header;
3688
3689 __le32 action;
3690 __le32 reserved;
3691 __le16 length;
3692 __le16 key_type_id;
3693 __le32 key_info;
3694 __le32 key_id;
3695 __le16 key_len;
3696 __u8 key_material[MAX_ENCR_KEY_LENGTH];
3697 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
3698 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
3699 __le16 tkip_rsc_low;
3700 __le32 tkip_rsc_high;
3701 __le16 tkip_tsc_low;
3702 __le32 tkip_tsc_high;
3703 __u8 mac_addr[6];
3704} __attribute__((packed));
3705
3706enum {
3707 MWL8K_ENCR_ENABLE,
3708 MWL8K_ENCR_SET_KEY,
3709 MWL8K_ENCR_REMOVE_KEY,
3710 MWL8K_ENCR_SET_GROUP_KEY,
3711};
3712
3713#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
3714#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
3715#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
3716#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
3717#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
3718
3719enum {
3720 MWL8K_ALG_WEP,
3721 MWL8K_ALG_TKIP,
3722 MWL8K_ALG_CCMP,
3723};
3724
3725#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
3726#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
3727#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
3728#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
3729#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
3730
3731static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
3732 struct ieee80211_vif *vif,
3733 u8 *addr,
3734 u8 encr_type)
3735{
3736 struct mwl8k_cmd_update_encryption *cmd;
3737 int rc;
3738
3739 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3740 if (cmd == NULL)
3741 return -ENOMEM;
3742
3743 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3744 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3745 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
3746 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3747 cmd->encr_type = encr_type;
3748
3749 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3750 kfree(cmd);
3751
3752 return rc;
3753}
3754
3755static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
3756 u8 *addr,
3757 struct ieee80211_key_conf *key)
3758{
3759 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3760 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3761 cmd->length = cpu_to_le16(sizeof(*cmd) -
3762 offsetof(struct mwl8k_cmd_set_key, length));
3763 cmd->key_id = cpu_to_le32(key->keyidx);
3764 cmd->key_len = cpu_to_le16(key->keylen);
3765 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3766
3767 switch (key->cipher) {
3768 case WLAN_CIPHER_SUITE_WEP40:
3769 case WLAN_CIPHER_SUITE_WEP104:
3770 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
3771 if (key->keyidx == 0)
3772 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
3773
3774 break;
3775 case WLAN_CIPHER_SUITE_TKIP:
3776 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
3777 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3778 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3779 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3780 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
3781 | MWL8K_KEY_FLAG_TSC_VALID);
3782 break;
3783 case WLAN_CIPHER_SUITE_CCMP:
3784 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
3785 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3786 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3787 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3788 break;
3789 default:
3790 return -ENOTSUPP;
3791 }
3792
3793 return 0;
3794}
3795
3796static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
3797 struct ieee80211_vif *vif,
3798 u8 *addr,
3799 struct ieee80211_key_conf *key)
3800{
3801 struct mwl8k_cmd_set_key *cmd;
3802 int rc;
3803 int keymlen;
3804 u32 action;
3805 u8 idx;
3806 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3807
3808 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3809 if (cmd == NULL)
3810 return -ENOMEM;
3811
3812 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3813 if (rc < 0)
3814 goto done;
3815
3816 idx = key->keyidx;
3817
3818 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3819 action = MWL8K_ENCR_SET_KEY;
3820 else
3821 action = MWL8K_ENCR_SET_GROUP_KEY;
3822
3823 switch (key->cipher) {
3824 case WLAN_CIPHER_SUITE_WEP40:
3825 case WLAN_CIPHER_SUITE_WEP104:
3826 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
3827 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
3828 sizeof(*key) + key->keylen);
3829 mwl8k_vif->wep_key_conf[idx].enabled = 1;
3830 }
3831
3832 keymlen = 0;
3833 action = MWL8K_ENCR_SET_KEY;
3834 break;
3835 case WLAN_CIPHER_SUITE_TKIP:
3836 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
3837 break;
3838 case WLAN_CIPHER_SUITE_CCMP:
3839 keymlen = key->keylen;
3840 break;
3841 default:
3842 rc = -ENOTSUPP;
3843 goto done;
3844 }
3845
3846 memcpy(cmd->key_material, key->key, keymlen);
3847 cmd->action = cpu_to_le32(action);
3848
3849 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3850done:
3851 kfree(cmd);
3852
3853 return rc;
3854}
3855
3856static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
3857 struct ieee80211_vif *vif,
3858 u8 *addr,
3859 struct ieee80211_key_conf *key)
3860{
3861 struct mwl8k_cmd_set_key *cmd;
3862 int rc;
3863 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3864
3865 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3866 if (cmd == NULL)
3867 return -ENOMEM;
3868
3869 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3870 if (rc < 0)
3871 goto done;
3872
3873 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3874 WLAN_CIPHER_SUITE_WEP104)
3875 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
3876
3877 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
3878
3879 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3880done:
3881 kfree(cmd);
3882
3883 return rc;
3884}
3885
3886static int mwl8k_set_key(struct ieee80211_hw *hw,
3887 enum set_key_cmd cmd_param,
3888 struct ieee80211_vif *vif,
3889 struct ieee80211_sta *sta,
3890 struct ieee80211_key_conf *key)
3891{
3892 int rc = 0;
3893 u8 encr_type;
3894 u8 *addr;
3895 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3896
3897 if (vif->type == NL80211_IFTYPE_STATION)
3898 return -EOPNOTSUPP;
3899
3900 if (sta == NULL)
3901 addr = hw->wiphy->perm_addr;
3902 else
3903 addr = sta->addr;
3904
3905 if (cmd_param == SET_KEY) {
3906 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3907 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
3908 if (rc)
3909 goto out;
3910
3911 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
3912 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
3913 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
3914 else
3915 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
3916
3917 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
3918 encr_type);
3919 if (rc)
3920 goto out;
3921
3922 mwl8k_vif->is_hw_crypto_enabled = true;
3923
3924 } else {
3925 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
3926
3927 if (rc)
3928 goto out;
3929
3930 mwl8k_vif->is_hw_crypto_enabled = false;
3931
3932 }
3933out:
3934 return rc;
3935}
3936
3937/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003938 * CMD_UPDATE_STADB.
3939 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003940struct ewc_ht_info {
3941 __le16 control1;
3942 __le16 control2;
3943 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003944} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003945
3946struct peer_capability_info {
3947 /* Peer type - AP vs. STA. */
3948 __u8 peer_type;
3949
3950 /* Basic 802.11 capabilities from assoc resp. */
3951 __le16 basic_caps;
3952
3953 /* Set if peer supports 802.11n high throughput (HT). */
3954 __u8 ht_support;
3955
3956 /* Valid if HT is supported. */
3957 __le16 ht_caps;
3958 __u8 extended_ht_caps;
3959 struct ewc_ht_info ewc_info;
3960
3961 /* Legacy rate table. Intersection of our rates and peer rates. */
3962 __u8 legacy_rates[12];
3963
3964 /* HT rate table. Intersection of our rates and peer rates. */
3965 __u8 ht_rates[16];
3966 __u8 pad[16];
3967
3968 /* If set, interoperability mode, no proprietary extensions. */
3969 __u8 interop;
3970 __u8 pad2;
3971 __u8 station_id;
3972 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003973} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003974
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003975struct mwl8k_cmd_update_stadb {
3976 struct mwl8k_cmd_pkt header;
3977
3978 /* See STADB_ACTION_TYPE */
3979 __le32 action;
3980
3981 /* Peer MAC address */
3982 __u8 peer_addr[ETH_ALEN];
3983
3984 __le32 reserved;
3985
3986 /* Peer info - valid during add/update. */
3987 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003988} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003989
Lennert Buytenheka6804002010-01-04 21:55:42 +01003990#define MWL8K_STA_DB_MODIFY_ENTRY 1
3991#define MWL8K_STA_DB_DEL_ENTRY 2
3992
3993/* Peer Entry flags - used to define the type of the peer node */
3994#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3995
3996static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003997 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003998 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003999{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004000 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01004001 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004002 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004003 int rc;
4004
4005 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4006 if (cmd == NULL)
4007 return -ENOMEM;
4008
4009 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4010 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01004011 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004012 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004013
Lennert Buytenheka6804002010-01-04 21:55:42 +01004014 p = &cmd->peer_info;
4015 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4016 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004017 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04004018 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004019 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
4020 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004021 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4022 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
4023 else
4024 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4025 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004026 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004027 p->interop = 1;
4028 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004029
Lennert Buytenheka6804002010-01-04 21:55:42 +01004030 rc = mwl8k_post_cmd(hw, &cmd->header);
4031 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004032
Lennert Buytenheka6804002010-01-04 21:55:42 +01004033 return rc ? rc : p->station_id;
4034}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004035
Lennert Buytenheka6804002010-01-04 21:55:42 +01004036static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4037 struct ieee80211_vif *vif, u8 *addr)
4038{
4039 struct mwl8k_cmd_update_stadb *cmd;
4040 int rc;
4041
4042 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4043 if (cmd == NULL)
4044 return -ENOMEM;
4045
4046 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4047 cmd->header.length = cpu_to_le16(sizeof(*cmd));
4048 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4049 memcpy(cmd->peer_addr, addr, ETH_ALEN);
4050
4051 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004052 kfree(cmd);
4053
4054 return rc;
4055}
4056
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004057
4058/*
4059 * Interrupt handling.
4060 */
4061static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4062{
4063 struct ieee80211_hw *hw = dev_id;
4064 struct mwl8k_priv *priv = hw->priv;
4065 u32 status;
4066
4067 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004068 if (!status)
4069 return IRQ_NONE;
4070
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004071 if (status & MWL8K_A2H_INT_TX_DONE) {
4072 status &= ~MWL8K_A2H_INT_TX_DONE;
4073 tasklet_schedule(&priv->poll_tx_task);
4074 }
4075
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004076 if (status & MWL8K_A2H_INT_RX_READY) {
4077 status &= ~MWL8K_A2H_INT_RX_READY;
4078 tasklet_schedule(&priv->poll_rx_task);
4079 }
4080
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004081 if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
4082 status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4083 ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4084 }
4085
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004086 if (status)
4087 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004088
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004089 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004090 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004091 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004092 }
4093
4094 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004095 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004096 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004097 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004098 }
4099
4100 return IRQ_HANDLED;
4101}
4102
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004103static void mwl8k_tx_poll(unsigned long data)
4104{
4105 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4106 struct mwl8k_priv *priv = hw->priv;
4107 int limit;
4108 int i;
4109
4110 limit = 32;
4111
4112 spin_lock_bh(&priv->tx_lock);
4113
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004114 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004115 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4116
4117 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4118 complete(priv->tx_wait);
4119 priv->tx_wait = NULL;
4120 }
4121
4122 spin_unlock_bh(&priv->tx_lock);
4123
4124 if (limit) {
4125 writel(~MWL8K_A2H_INT_TX_DONE,
4126 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4127 } else {
4128 tasklet_schedule(&priv->poll_tx_task);
4129 }
4130}
4131
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004132static void mwl8k_rx_poll(unsigned long data)
4133{
4134 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4135 struct mwl8k_priv *priv = hw->priv;
4136 int limit;
4137
4138 limit = 32;
4139 limit -= rxq_process(hw, 0, limit);
4140 limit -= rxq_refill(hw, 0, limit);
4141
4142 if (limit) {
4143 writel(~MWL8K_A2H_INT_RX_READY,
4144 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4145 } else {
4146 tasklet_schedule(&priv->poll_rx_task);
4147 }
4148}
4149
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004150
4151/*
4152 * Core driver operations.
4153 */
Johannes Berg7bb45682011-02-24 14:42:06 +01004154static void mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004155{
4156 struct mwl8k_priv *priv = hw->priv;
4157 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004158
Lennert Buytenhek9189c102010-01-12 13:47:32 +01004159 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004160 wiphy_debug(hw->wiphy,
4161 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004162 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01004163 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004164 }
4165
Johannes Berg7bb45682011-02-24 14:42:06 +01004166 mwl8k_txq_xmit(hw, index, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004167}
4168
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004169static int mwl8k_start(struct ieee80211_hw *hw)
4170{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004171 struct mwl8k_priv *priv = hw->priv;
4172 int rc;
4173
Joe Perchesa0607fd2009-11-18 23:29:17 -08004174 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004175 IRQF_SHARED, MWL8K_NAME, hw);
4176 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004177 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004178 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004179 }
4180
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004181 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004182 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004183 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004184
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004185 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004186 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004187
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004188 rc = mwl8k_fw_lock(hw);
4189 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004190 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004191
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004192 if (!priv->ap_fw) {
4193 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004194 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004195
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004196 if (!rc)
4197 rc = mwl8k_cmd_set_pre_scan(hw);
4198
4199 if (!rc)
4200 rc = mwl8k_cmd_set_post_scan(hw,
4201 "\x00\x00\x00\x00\x00\x00");
4202 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004203
4204 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004205 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004206
4207 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004208 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004209
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004210 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004211 }
4212
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004213 if (rc) {
4214 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4215 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004216 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004217 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004218 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004219
4220 return rc;
4221}
4222
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004223static void mwl8k_stop(struct ieee80211_hw *hw)
4224{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004225 struct mwl8k_priv *priv = hw->priv;
4226 int i;
4227
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004228 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004229
4230 ieee80211_stop_queues(hw);
4231
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004232 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004233 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004234 free_irq(priv->pdev->irq, hw);
4235
4236 /* Stop finalize join worker */
4237 cancel_work_sync(&priv->finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07004238 cancel_work_sync(&priv->watchdog_ba_handle);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004239 if (priv->beacon_skb != NULL)
4240 dev_kfree_skb(priv->beacon_skb);
4241
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004242 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004243 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004244 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004245
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004246 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004247 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004248 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004249}
4250
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004251static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4252
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004253static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004254 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004255{
4256 struct mwl8k_priv *priv = hw->priv;
4257 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004258 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004259 int macid, rc;
4260 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004261
4262 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004263 * Reject interface creation if sniffer mode is active, as
4264 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004265 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004266 */
4267 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004268 wiphy_info(hw->wiphy,
4269 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004270 return -EINVAL;
4271 }
4272
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004273 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004274 switch (vif->type) {
4275 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004276 if (!priv->ap_fw && di->fw_image_ap) {
4277 /* we must load the ap fw to meet this request */
4278 if (!list_empty(&priv->vif_list))
4279 return -EBUSY;
4280 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4281 if (rc)
4282 return rc;
4283 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004284 macids_supported = priv->ap_macids_supported;
4285 break;
4286 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004287 if (priv->ap_fw && di->fw_image_sta) {
4288 /* we must load the sta fw to meet this request */
4289 if (!list_empty(&priv->vif_list))
4290 return -EBUSY;
4291 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4292 if (rc)
4293 return rc;
4294 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004295 macids_supported = priv->sta_macids_supported;
4296 break;
4297 default:
4298 return -EINVAL;
4299 }
4300
4301 macid = ffs(macids_supported & ~priv->macids_used);
4302 if (!macid--)
4303 return -EBUSY;
4304
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004305 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01004306 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004307 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004308 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004309 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004310 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004311 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4312 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004313
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004314 /* Set the mac address. */
4315 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4316
4317 if (priv->ap_fw)
4318 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4319
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004320 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004321 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004322
4323 return 0;
4324}
4325
4326static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004327 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004328{
4329 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004330 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004331
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004332 if (priv->ap_fw)
4333 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4334
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004335 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004336
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004337 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004338 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004339}
4340
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004341static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004342{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004343 struct ieee80211_conf *conf = &hw->conf;
4344 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004345 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004346
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004347 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004348 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004349 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004350 }
4351
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004352 rc = mwl8k_fw_lock(hw);
4353 if (rc)
4354 return rc;
4355
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004356 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004357 if (rc)
4358 goto out;
4359
Lennert Buytenhek610677d2010-01-04 21:56:46 +01004360 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004361 if (rc)
4362 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004363
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004364 if (conf->power_level > 18)
4365 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004366
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004367 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004368 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4369 if (rc)
4370 goto out;
4371
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08004372 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
4373 if (rc)
4374 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
4375 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
4376 if (rc)
4377 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
4378
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004379 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004380 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4381 if (rc)
4382 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004383 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4384 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004385
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004386out:
4387 mwl8k_fw_unlock(hw);
4388
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004389 return rc;
4390}
4391
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004392static void
4393mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4394 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004395{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004396 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004397 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004398 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004399 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004400
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004401 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004402 return;
4403
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004404 /*
4405 * No need to capture a beacon if we're no longer associated.
4406 */
4407 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4408 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004409
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004410 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004411 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004412 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004413 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004414 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004415
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004416 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004417
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004418 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004419 if (ap == NULL) {
4420 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004421 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004422 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004423
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004424 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4425 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4426 } else {
4427 ap_legacy_rates =
4428 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4429 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004430 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004431
4432 rcu_read_unlock();
4433 }
4434
4435 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004436 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004437 if (rc)
4438 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004439
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004440 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004441 if (rc)
4442 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004443 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004444
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004445 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004446 rc = mwl8k_set_radio_preamble(hw,
4447 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004448 if (rc)
4449 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004450 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004451
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004452 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004453 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004454 if (rc)
4455 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004456 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004457
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004458 if (vif->bss_conf.assoc &&
4459 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4460 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004461 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004462 if (rc)
4463 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004464 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004465
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004466 if (vif->bss_conf.assoc &&
4467 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004468 /*
4469 * Finalize the join. Tell rx handler to process
4470 * next beacon from our BSSID.
4471 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004472 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004473 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004474 }
4475
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004476out:
4477 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004478}
4479
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004480static void
4481mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4482 struct ieee80211_bss_conf *info, u32 changed)
4483{
4484 int rc;
4485
4486 if (mwl8k_fw_lock(hw))
4487 return;
4488
4489 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4490 rc = mwl8k_set_radio_preamble(hw,
4491 vif->bss_conf.use_short_preamble);
4492 if (rc)
4493 goto out;
4494 }
4495
4496 if (changed & BSS_CHANGED_BASIC_RATES) {
4497 int idx;
4498 int rate;
4499
4500 /*
4501 * Use lowest supported basic rate for multicasts
4502 * and management frames (such as probe responses --
4503 * beacons will always go out at 1 Mb/s).
4504 */
4505 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004506 if (idx)
4507 idx--;
4508
4509 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4510 rate = mwl8k_rates_24[idx].hw_value;
4511 else
4512 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004513
4514 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4515 }
4516
4517 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4518 struct sk_buff *skb;
4519
4520 skb = ieee80211_beacon_get(hw, vif);
4521 if (skb != NULL) {
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004522 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004523 kfree_skb(skb);
4524 }
4525 }
4526
4527 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004528 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004529
4530out:
4531 mwl8k_fw_unlock(hw);
4532}
4533
4534static void
4535mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4536 struct ieee80211_bss_conf *info, u32 changed)
4537{
4538 struct mwl8k_priv *priv = hw->priv;
4539
4540 if (!priv->ap_fw)
4541 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
4542 else
4543 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
4544}
4545
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004546static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00004547 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004548{
4549 struct mwl8k_cmd_pkt *cmd;
4550
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004551 /*
4552 * Synthesize and return a command packet that programs the
4553 * hardware multicast address filter. At this point we don't
4554 * know whether FIF_ALLMULTI is being requested, but if it is,
4555 * we'll end up throwing this packet away and creating a new
4556 * one in mwl8k_configure_filter().
4557 */
Jiri Pirko22bedad2010-04-01 21:22:57 +00004558 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004559
4560 return (unsigned long)cmd;
4561}
4562
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004563static int
4564mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
4565 unsigned int changed_flags,
4566 unsigned int *total_flags)
4567{
4568 struct mwl8k_priv *priv = hw->priv;
4569
4570 /*
4571 * Hardware sniffer mode is mutually exclusive with STA
4572 * operation, so refuse to enable sniffer mode if a STA
4573 * interface is active.
4574 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004575 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004576 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07004577 wiphy_info(hw->wiphy,
4578 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004579 return 0;
4580 }
4581
4582 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004583 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004584 return 0;
4585 priv->sniffer_enabled = true;
4586 }
4587
4588 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
4589 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
4590 FIF_OTHER_BSS;
4591
4592 return 1;
4593}
4594
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004595static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
4596{
4597 if (!list_empty(&priv->vif_list))
4598 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
4599
4600 return NULL;
4601}
4602
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004603static void mwl8k_configure_filter(struct ieee80211_hw *hw,
4604 unsigned int changed_flags,
4605 unsigned int *total_flags,
4606 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004607{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004608 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004609 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
4610
4611 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02004612 * AP firmware doesn't allow fine-grained control over
4613 * the receive filter.
4614 */
4615 if (priv->ap_fw) {
4616 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
4617 kfree(cmd);
4618 return;
4619 }
4620
4621 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004622 * Enable hardware sniffer mode if FIF_CONTROL or
4623 * FIF_OTHER_BSS is requested.
4624 */
4625 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
4626 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
4627 kfree(cmd);
4628 return;
4629 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004630
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004631 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004632 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004633
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004634 if (mwl8k_fw_lock(hw)) {
4635 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004636 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004637 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004638
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004639 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004640 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004641 priv->sniffer_enabled = false;
4642 }
4643
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004644 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004645 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
4646 /*
4647 * Disable the BSS filter.
4648 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004649 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004650 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004651 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004652 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004653
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004654 /*
4655 * Enable the BSS filter.
4656 *
4657 * If there is an active STA interface, use that
4658 * interface's BSSID, otherwise use a dummy one
4659 * (where the OUI part needs to be nonzero for
4660 * the BSSID to be accepted by POST_SCAN).
4661 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004662 mwl8k_vif = mwl8k_first_vif(priv);
4663 if (mwl8k_vif != NULL)
4664 bssid = mwl8k_vif->vif->bss_conf.bssid;
4665 else
4666 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004667
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004668 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004669 }
4670 }
4671
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004672 /*
4673 * If FIF_ALLMULTI is being requested, throw away the command
4674 * packet that ->prepare_multicast() built and replace it with
4675 * a command packet that enables reception of all multicast
4676 * packets.
4677 */
4678 if (*total_flags & FIF_ALLMULTI) {
4679 kfree(cmd);
Jiri Pirko22bedad2010-04-01 21:22:57 +00004680 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004681 }
4682
4683 if (cmd != NULL) {
4684 mwl8k_post_cmd(hw, cmd);
4685 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004686 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004687
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004688 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004689}
4690
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004691static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4692{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004693 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004694}
4695
Johannes Berg4a6967b2010-02-19 19:18:37 +01004696static int mwl8k_sta_remove(struct ieee80211_hw *hw,
4697 struct ieee80211_vif *vif,
4698 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004699{
4700 struct mwl8k_priv *priv = hw->priv;
4701
Johannes Berg4a6967b2010-02-19 19:18:37 +01004702 if (priv->ap_fw)
4703 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
4704 else
4705 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
4706}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004707
Johannes Berg4a6967b2010-02-19 19:18:37 +01004708static int mwl8k_sta_add(struct ieee80211_hw *hw,
4709 struct ieee80211_vif *vif,
4710 struct ieee80211_sta *sta)
4711{
4712 struct mwl8k_priv *priv = hw->priv;
4713 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004714 int i;
4715 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4716 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004717
Johannes Berg4a6967b2010-02-19 19:18:37 +01004718 if (!priv->ap_fw) {
4719 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
4720 if (ret >= 0) {
4721 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004722 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004723 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01004724
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004725 } else {
4726 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004727 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004728
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004729 for (i = 0; i < NUM_WEP_KEYS; i++) {
4730 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
4731 if (mwl8k_vif->wep_key_conf[i].enabled)
4732 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
4733 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004734 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004735}
4736
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004737static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
4738 const struct ieee80211_tx_queue_params *params)
4739{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004740 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004741 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004742
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004743 rc = mwl8k_fw_lock(hw);
4744 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004745 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004746 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
4747
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004748 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004749 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004750
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004751 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004752 int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004753 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004754 params->cw_min,
4755 params->cw_max,
4756 params->aifs,
4757 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004758 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004759
4760 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004761 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004762
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004763 return rc;
4764}
4765
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004766static int mwl8k_get_stats(struct ieee80211_hw *hw,
4767 struct ieee80211_low_level_stats *stats)
4768{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004769 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004770}
4771
John W. Linville0d462bb2010-07-28 14:04:24 -04004772static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
4773 struct survey_info *survey)
4774{
4775 struct mwl8k_priv *priv = hw->priv;
4776 struct ieee80211_conf *conf = &hw->conf;
4777
4778 if (idx != 0)
4779 return -ENOENT;
4780
4781 survey->channel = conf->channel;
4782 survey->filled = SURVEY_INFO_NOISE_DBM;
4783 survey->noise = priv->noise;
4784
4785 return 0;
4786}
4787
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004788#define MAX_AMPDU_ATTEMPTS 5
4789
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004790static int
4791mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4792 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01004793 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4794 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004795{
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004796
4797 int i, rc = 0;
4798 struct mwl8k_priv *priv = hw->priv;
4799 struct mwl8k_ampdu_stream *stream;
4800 u8 *addr = sta->addr;
4801
4802 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
4803 return -ENOTSUPP;
4804
4805 spin_lock(&priv->stream_lock);
4806 stream = mwl8k_lookup_stream(hw, addr, tid);
4807
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004808 switch (action) {
4809 case IEEE80211_AMPDU_RX_START:
4810 case IEEE80211_AMPDU_RX_STOP:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004811 break;
4812 case IEEE80211_AMPDU_TX_START:
4813 /* By the time we get here the hw queues may contain outgoing
4814 * packets for this RA/TID that are not part of this BA
4815 * session. The hw will assign sequence numbers to these
4816 * packets as they go out. So if we query the hw for its next
4817 * sequence number and use that for the SSN here, it may end up
4818 * being wrong, which will lead to sequence number mismatch at
4819 * the recipient. To avoid this, we reset the sequence number
4820 * to O for the first MPDU in this BA stream.
4821 */
4822 *ssn = 0;
4823 if (stream == NULL) {
4824 /* This means that somebody outside this driver called
4825 * ieee80211_start_tx_ba_session. This is unexpected
4826 * because we do our own rate control. Just warn and
4827 * move on.
4828 */
4829 wiphy_warn(hw->wiphy, "Unexpected call to %s. "
4830 "Proceeding anyway.\n", __func__);
4831 stream = mwl8k_add_stream(hw, sta, tid);
4832 }
4833 if (stream == NULL) {
4834 wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
4835 rc = -EBUSY;
4836 break;
4837 }
4838 stream->state = AMPDU_STREAM_IN_PROGRESS;
4839
4840 /* Release the lock before we do the time consuming stuff */
4841 spin_unlock(&priv->stream_lock);
4842 for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
4843 rc = mwl8k_check_ba(hw, stream);
4844
4845 if (!rc)
4846 break;
4847 /*
4848 * HW queues take time to be flushed, give them
4849 * sufficient time
4850 */
4851
4852 msleep(1000);
4853 }
4854 spin_lock(&priv->stream_lock);
4855 if (rc) {
4856 wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
4857 " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
4858 mwl8k_remove_stream(hw, stream);
4859 rc = -EBUSY;
4860 break;
4861 }
4862 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
4863 break;
4864 case IEEE80211_AMPDU_TX_STOP:
4865 if (stream == NULL)
4866 break;
4867 if (stream->state == AMPDU_STREAM_ACTIVE) {
4868 spin_unlock(&priv->stream_lock);
4869 mwl8k_destroy_ba(hw, stream);
4870 spin_lock(&priv->stream_lock);
4871 }
4872 mwl8k_remove_stream(hw, stream);
4873 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
4874 break;
4875 case IEEE80211_AMPDU_TX_OPERATIONAL:
4876 BUG_ON(stream == NULL);
4877 BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
4878 spin_unlock(&priv->stream_lock);
4879 rc = mwl8k_create_ba(hw, stream, buf_size);
4880 spin_lock(&priv->stream_lock);
4881 if (!rc)
4882 stream->state = AMPDU_STREAM_ACTIVE;
4883 else {
4884 spin_unlock(&priv->stream_lock);
4885 mwl8k_destroy_ba(hw, stream);
4886 spin_lock(&priv->stream_lock);
4887 wiphy_debug(hw->wiphy,
4888 "Failed adding stream for sta %pM tid %d\n",
4889 addr, tid);
4890 mwl8k_remove_stream(hw, stream);
4891 }
4892 break;
4893
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004894 default:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004895 rc = -ENOTSUPP;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004896 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004897
4898 spin_unlock(&priv->stream_lock);
4899 return rc;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004900}
4901
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004902static const struct ieee80211_ops mwl8k_ops = {
4903 .tx = mwl8k_tx,
4904 .start = mwl8k_start,
4905 .stop = mwl8k_stop,
4906 .add_interface = mwl8k_add_interface,
4907 .remove_interface = mwl8k_remove_interface,
4908 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004909 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004910 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004911 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004912 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004913 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01004914 .sta_add = mwl8k_sta_add,
4915 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004916 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004917 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04004918 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004919 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004920};
4921
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004922static void mwl8k_finalize_join_worker(struct work_struct *work)
4923{
4924 struct mwl8k_priv *priv =
4925 container_of(work, struct mwl8k_priv, finalize_join_worker);
4926 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01004927 struct ieee80211_mgmt *mgmt = (void *)skb->data;
4928 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
4929 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
4930 mgmt->u.beacon.variable, len);
4931 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004932
Johannes Berg56007a02010-01-26 14:19:52 +01004933 if (tim && tim[1] >= 2)
4934 dtim_period = tim[3];
4935
4936 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004937
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004938 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004939 priv->beacon_skb = NULL;
4940}
4941
John W. Linvillebcb628d2009-11-06 16:40:16 -05004942enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004943 MWL8363 = 0,
4944 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004945 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02004946};
4947
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07004948#define MWL8K_8366_AP_FW_API 2
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004949#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
4950#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
4951
John W. Linvillebcb628d2009-11-06 16:40:16 -05004952static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004953 [MWL8363] = {
4954 .part_name = "88w8363",
4955 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004956 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004957 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004958 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004959 .part_name = "88w8687",
4960 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004961 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05004962 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004963 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004964 .part_name = "88w8366",
4965 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004966 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004967 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
4968 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004969 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004970 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004971};
4972
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004973MODULE_FIRMWARE("mwl8k/helper_8363.fw");
4974MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
4975MODULE_FIRMWARE("mwl8k/helper_8687.fw");
4976MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
4977MODULE_FIRMWARE("mwl8k/helper_8366.fw");
4978MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004979MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004980
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004981static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01004982 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004983 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
4984 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004985 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
4986 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
4987 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01004988 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004989 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004990};
4991MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
4992
Brian Cavagnolo99020472010-11-12 17:23:52 -08004993static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
4994{
4995 int rc;
4996 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
4997 "Trying alternative firmware %s\n", pci_name(priv->pdev),
4998 priv->fw_pref, priv->fw_alt);
4999 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5000 if (rc) {
5001 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5002 pci_name(priv->pdev), priv->fw_alt);
5003 return rc;
5004 }
5005 return 0;
5006}
5007
5008static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5009static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5010{
5011 struct mwl8k_priv *priv = context;
5012 struct mwl8k_device_info *di = priv->device_info;
5013 int rc;
5014
5015 switch (priv->fw_state) {
5016 case FW_STATE_INIT:
5017 if (!fw) {
5018 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5019 pci_name(priv->pdev), di->helper_image);
5020 goto fail;
5021 }
5022 priv->fw_helper = fw;
5023 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5024 true);
5025 if (rc && priv->fw_alt) {
5026 rc = mwl8k_request_alt_fw(priv);
5027 if (rc)
5028 goto fail;
5029 priv->fw_state = FW_STATE_LOADING_ALT;
5030 } else if (rc)
5031 goto fail;
5032 else
5033 priv->fw_state = FW_STATE_LOADING_PREF;
5034 break;
5035
5036 case FW_STATE_LOADING_PREF:
5037 if (!fw) {
5038 if (priv->fw_alt) {
5039 rc = mwl8k_request_alt_fw(priv);
5040 if (rc)
5041 goto fail;
5042 priv->fw_state = FW_STATE_LOADING_ALT;
5043 } else
5044 goto fail;
5045 } else {
5046 priv->fw_ucode = fw;
5047 rc = mwl8k_firmware_load_success(priv);
5048 if (rc)
5049 goto fail;
5050 else
5051 complete(&priv->firmware_loading_complete);
5052 }
5053 break;
5054
5055 case FW_STATE_LOADING_ALT:
5056 if (!fw) {
5057 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5058 pci_name(priv->pdev), di->helper_image);
5059 goto fail;
5060 }
5061 priv->fw_ucode = fw;
5062 rc = mwl8k_firmware_load_success(priv);
5063 if (rc)
5064 goto fail;
5065 else
5066 complete(&priv->firmware_loading_complete);
5067 break;
5068
5069 default:
5070 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5071 MWL8K_NAME, priv->fw_state);
5072 BUG_ON(1);
5073 }
5074
5075 return;
5076
5077fail:
5078 priv->fw_state = FW_STATE_ERROR;
5079 complete(&priv->firmware_loading_complete);
5080 device_release_driver(&priv->pdev->dev);
5081 mwl8k_release_firmware(priv);
5082}
5083
5084static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5085 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005086{
5087 struct mwl8k_priv *priv = hw->priv;
5088 int rc;
5089
5090 /* Reset firmware and hardware */
5091 mwl8k_hw_reset(priv);
5092
5093 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005094 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005095 if (rc) {
5096 wiphy_err(hw->wiphy, "Firmware files not found\n");
5097 return rc;
5098 }
5099
Brian Cavagnolo99020472010-11-12 17:23:52 -08005100 if (nowait)
5101 return rc;
5102
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005103 /* Load firmware into hardware */
5104 rc = mwl8k_load_firmware(hw);
5105 if (rc)
5106 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5107
5108 /* Reclaim memory once firmware is successfully loaded */
5109 mwl8k_release_firmware(priv);
5110
5111 return rc;
5112}
5113
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005114static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5115{
5116 struct mwl8k_priv *priv = hw->priv;
5117 int rc = 0;
5118 int i;
5119
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005120 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005121 rc = mwl8k_txq_init(hw, i);
5122 if (rc)
5123 break;
5124 if (priv->ap_fw)
5125 iowrite32(priv->txq[i].txd_dma,
5126 priv->sram + priv->txq_offset[i]);
5127 }
5128 return rc;
5129}
5130
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005131/* initialize hw after successfully loading a firmware image */
5132static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5133{
5134 struct mwl8k_priv *priv = hw->priv;
5135 int rc = 0;
5136 int i;
5137
5138 if (priv->ap_fw) {
5139 priv->rxd_ops = priv->device_info->ap_rxd_ops;
5140 if (priv->rxd_ops == NULL) {
5141 wiphy_err(hw->wiphy,
5142 "Driver does not have AP firmware image support for this hardware\n");
5143 goto err_stop_firmware;
5144 }
5145 } else {
5146 priv->rxd_ops = &rxd_sta_ops;
5147 }
5148
5149 priv->sniffer_enabled = false;
5150 priv->wmm_enabled = false;
5151 priv->pending_tx_pkts = 0;
5152
5153 rc = mwl8k_rxq_init(hw, 0);
5154 if (rc)
5155 goto err_stop_firmware;
5156 rxq_refill(hw, 0, INT_MAX);
5157
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005158 /* For the sta firmware, we need to know the dma addresses of tx queues
5159 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
5160 * prior to issuing this command. But for the AP case, we learn the
5161 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5162 * case we must initialize the tx queues after.
5163 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07005164 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005165 if (!priv->ap_fw) {
5166 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005167 if (rc)
5168 goto err_free_queues;
5169 }
5170
5171 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5172 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005173 iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5174 MWL8K_A2H_INT_BA_WATCHDOG,
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005175 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
5176 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
5177
5178 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5179 IRQF_SHARED, MWL8K_NAME, hw);
5180 if (rc) {
5181 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5182 goto err_free_queues;
5183 }
5184
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005185 memset(priv->ampdu, 0, sizeof(priv->ampdu));
5186
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005187 /*
5188 * Temporarily enable interrupts. Initial firmware host
5189 * commands use interrupts and avoid polling. Disable
5190 * interrupts when done.
5191 */
5192 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5193
5194 /* Get config data, mac addrs etc */
5195 if (priv->ap_fw) {
5196 rc = mwl8k_cmd_get_hw_spec_ap(hw);
5197 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005198 rc = mwl8k_init_txqs(hw);
5199 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005200 rc = mwl8k_cmd_set_hw_spec(hw);
5201 } else {
5202 rc = mwl8k_cmd_get_hw_spec_sta(hw);
5203 }
5204 if (rc) {
5205 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5206 goto err_free_irq;
5207 }
5208
5209 /* Turn radio off */
5210 rc = mwl8k_cmd_radio_disable(hw);
5211 if (rc) {
5212 wiphy_err(hw->wiphy, "Cannot disable\n");
5213 goto err_free_irq;
5214 }
5215
5216 /* Clear MAC address */
5217 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5218 if (rc) {
5219 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5220 goto err_free_irq;
5221 }
5222
5223 /* Disable interrupts */
5224 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5225 free_irq(priv->pdev->irq, hw);
5226
5227 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5228 priv->device_info->part_name,
5229 priv->hw_rev, hw->wiphy->perm_addr,
5230 priv->ap_fw ? "AP" : "STA",
5231 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5232 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5233
5234 return 0;
5235
5236err_free_irq:
5237 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5238 free_irq(priv->pdev->irq, hw);
5239
5240err_free_queues:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005241 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005242 mwl8k_txq_deinit(hw, i);
5243 mwl8k_rxq_deinit(hw, 0);
5244
5245err_stop_firmware:
5246 mwl8k_hw_reset(priv);
5247
5248 return rc;
5249}
5250
5251/*
5252 * invoke mwl8k_reload_firmware to change the firmware image after the device
5253 * has already been registered
5254 */
5255static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5256{
5257 int i, rc = 0;
5258 struct mwl8k_priv *priv = hw->priv;
5259
5260 mwl8k_stop(hw);
5261 mwl8k_rxq_deinit(hw, 0);
5262
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005263 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005264 mwl8k_txq_deinit(hw, i);
5265
Brian Cavagnolo99020472010-11-12 17:23:52 -08005266 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005267 if (rc)
5268 goto fail;
5269
5270 rc = mwl8k_probe_hw(hw);
5271 if (rc)
5272 goto fail;
5273
5274 rc = mwl8k_start(hw);
5275 if (rc)
5276 goto fail;
5277
5278 rc = mwl8k_config(hw, ~0);
5279 if (rc)
5280 goto fail;
5281
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005282 for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005283 rc = mwl8k_conf_tx(hw, i, &priv->wmm_params[i]);
5284 if (rc)
5285 goto fail;
5286 }
5287
5288 return rc;
5289
5290fail:
5291 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5292 return rc;
5293}
5294
5295static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5296{
5297 struct ieee80211_hw *hw = priv->hw;
5298 int i, rc;
5299
Brian Cavagnolo99020472010-11-12 17:23:52 -08005300 rc = mwl8k_load_firmware(hw);
5301 mwl8k_release_firmware(priv);
5302 if (rc) {
5303 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5304 return rc;
5305 }
5306
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005307 /*
5308 * Extra headroom is the size of the required DMA header
5309 * minus the size of the smallest 802.11 frame (CTS frame).
5310 */
5311 hw->extra_tx_headroom =
5312 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5313
5314 hw->channel_change_time = 10;
5315
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005316 hw->queues = MWL8K_TX_WMM_QUEUES;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005317
5318 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08005319 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005320 hw->vif_data_size = sizeof(struct mwl8k_vif);
5321 hw->sta_data_size = sizeof(struct mwl8k_sta);
5322
5323 priv->macids_used = 0;
5324 INIT_LIST_HEAD(&priv->vif_list);
5325
5326 /* Set default radio state and preamble */
5327 priv->radio_on = 0;
5328 priv->radio_short_preamble = 0;
5329
5330 /* Finalize join worker */
5331 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
Nishant Sarmukadam3aefc372011-03-17 11:58:47 -07005332 /* Handle watchdog ba events */
5333 INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005334
5335 /* TX reclaim and RX tasklets. */
5336 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5337 tasklet_disable(&priv->poll_tx_task);
5338 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5339 tasklet_disable(&priv->poll_rx_task);
5340
5341 /* Power management cookie */
5342 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5343 if (priv->cookie == NULL)
5344 return -ENOMEM;
5345
5346 mutex_init(&priv->fw_mutex);
5347 priv->fw_mutex_owner = NULL;
5348 priv->fw_mutex_depth = 0;
5349 priv->hostcmd_wait = NULL;
5350
5351 spin_lock_init(&priv->tx_lock);
5352
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005353 spin_lock_init(&priv->stream_lock);
5354
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005355 priv->tx_wait = NULL;
5356
5357 rc = mwl8k_probe_hw(hw);
5358 if (rc)
5359 goto err_free_cookie;
5360
5361 hw->wiphy->interface_modes = 0;
5362 if (priv->ap_macids_supported || priv->device_info->fw_image_ap)
5363 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
5364 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5365 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5366
5367 rc = ieee80211_register_hw(hw);
5368 if (rc) {
5369 wiphy_err(hw->wiphy, "Cannot register device\n");
5370 goto err_unprobe_hw;
5371 }
5372
5373 return 0;
5374
5375err_unprobe_hw:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005376 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005377 mwl8k_txq_deinit(hw, i);
5378 mwl8k_rxq_deinit(hw, 0);
5379
5380err_free_cookie:
5381 if (priv->cookie != NULL)
5382 pci_free_consistent(priv->pdev, 4,
5383 priv->cookie, priv->cookie_dma);
5384
5385 return rc;
5386}
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005387static int __devinit mwl8k_probe(struct pci_dev *pdev,
5388 const struct pci_device_id *id)
5389{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005390 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005391 struct ieee80211_hw *hw;
5392 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005393 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005394 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02005395
5396 if (!printed_version) {
5397 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5398 printed_version = 1;
5399 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005400
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005401
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005402 rc = pci_enable_device(pdev);
5403 if (rc) {
5404 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
5405 MWL8K_NAME);
5406 return rc;
5407 }
5408
5409 rc = pci_request_regions(pdev, MWL8K_NAME);
5410 if (rc) {
5411 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
5412 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005413 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005414 }
5415
5416 pci_set_master(pdev);
5417
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005418
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005419 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
5420 if (hw == NULL) {
5421 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
5422 rc = -ENOMEM;
5423 goto err_free_reg;
5424 }
5425
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005426 SET_IEEE80211_DEV(hw, &pdev->dev);
5427 pci_set_drvdata(pdev, hw);
5428
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005429 priv = hw->priv;
5430 priv->hw = hw;
5431 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05005432 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005433
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005434
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005435 priv->sram = pci_iomap(pdev, 0, 0x10000);
5436 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005437 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005438 goto err_iounmap;
5439 }
5440
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005441 /*
5442 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
5443 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
5444 */
5445 priv->regs = pci_iomap(pdev, 1, 0x10000);
5446 if (priv->regs == NULL) {
5447 priv->regs = pci_iomap(pdev, 2, 0x10000);
5448 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005449 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005450 goto err_iounmap;
5451 }
5452 }
5453
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005454 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08005455 * Choose the initial fw image depending on user input. If a second
5456 * image is available, make it the alternative image that will be
5457 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005458 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005459 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005460 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005461 if (ap_mode_default && di->fw_image_ap) {
5462 priv->fw_pref = di->fw_image_ap;
5463 priv->fw_alt = di->fw_image_sta;
5464 } else if (!ap_mode_default && di->fw_image_sta) {
5465 priv->fw_pref = di->fw_image_sta;
5466 priv->fw_alt = di->fw_image_ap;
5467 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005468 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005469 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005470 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
5471 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005472 priv->fw_pref = di->fw_image_ap;
5473 }
5474 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005475 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005476 goto err_stop_firmware;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005477 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005478
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005479err_stop_firmware:
5480 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005481
5482err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005483 if (priv->regs != NULL)
5484 pci_iounmap(pdev, priv->regs);
5485
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005486 if (priv->sram != NULL)
5487 pci_iounmap(pdev, priv->sram);
5488
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005489 pci_set_drvdata(pdev, NULL);
5490 ieee80211_free_hw(hw);
5491
5492err_free_reg:
5493 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005494
5495err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005496 pci_disable_device(pdev);
5497
5498 return rc;
5499}
5500
Joerg Albert230f7af2009-04-18 02:10:45 +02005501static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005502{
5503 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
5504}
5505
Joerg Albert230f7af2009-04-18 02:10:45 +02005506static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005507{
5508 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
5509 struct mwl8k_priv *priv;
5510 int i;
5511
5512 if (hw == NULL)
5513 return;
5514 priv = hw->priv;
5515
Brian Cavagnolo99020472010-11-12 17:23:52 -08005516 wait_for_completion(&priv->firmware_loading_complete);
5517
5518 if (priv->fw_state == FW_STATE_ERROR) {
5519 mwl8k_hw_reset(priv);
5520 goto unmap;
5521 }
5522
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005523 ieee80211_stop_queues(hw);
5524
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02005525 ieee80211_unregister_hw(hw);
5526
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005527 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01005528 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005529 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005530
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005531 /* Stop hardware */
5532 mwl8k_hw_reset(priv);
5533
5534 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005535 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01005536 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005537
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005538 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005539 mwl8k_txq_deinit(hw, i);
5540
5541 mwl8k_rxq_deinit(hw, 0);
5542
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005543 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005544
Brian Cavagnolo99020472010-11-12 17:23:52 -08005545unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005546 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005547 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005548 pci_set_drvdata(pdev, NULL);
5549 ieee80211_free_hw(hw);
5550 pci_release_regions(pdev);
5551 pci_disable_device(pdev);
5552}
5553
5554static struct pci_driver mwl8k_driver = {
5555 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005556 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005557 .probe = mwl8k_probe,
5558 .remove = __devexit_p(mwl8k_remove),
5559 .shutdown = __devexit_p(mwl8k_shutdown),
5560};
5561
5562static int __init mwl8k_init(void)
5563{
5564 return pci_register_driver(&mwl8k_driver);
5565}
5566
5567static void __exit mwl8k_exit(void)
5568{
5569 pci_unregister_driver(&mwl8k_driver);
5570}
5571
5572module_init(mwl8k_init);
5573module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005574
5575MODULE_DESCRIPTION(MWL8K_DESC);
5576MODULE_VERSION(MWL8K_VERSION);
5577MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
5578MODULE_LICENSE("GPL");