blob: 944ad030e4ddaad6a11915408ddc9678b419a9dc [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)
66#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
67#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
68#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
69#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
70#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
71#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
72#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
73#define MWL8K_A2H_INT_RX_READY (1 << 1)
74#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010075
76#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
77 MWL8K_A2H_INT_CHNL_SWITCHED | \
78 MWL8K_A2H_INT_QUEUE_EMPTY | \
79 MWL8K_A2H_INT_RADAR_DETECT | \
80 MWL8K_A2H_INT_RADIO_ON | \
81 MWL8K_A2H_INT_RADIO_OFF | \
82 MWL8K_A2H_INT_MAC_EVENT | \
83 MWL8K_A2H_INT_OPC_DONE | \
84 MWL8K_A2H_INT_RX_READY | \
85 MWL8K_A2H_INT_TX_DONE)
86
Lennert Buytenheka66098d2009-03-10 10:13:33 +010087#define MWL8K_RX_QUEUES 1
88#define MWL8K_TX_QUEUES 4
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -070089#define MWL8K_MAX_AMPDU_QUEUES 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +010090
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020091struct rxd_ops {
92 int rxd_size;
93 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
94 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010095 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -040096 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020097};
98
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020099struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200100 char *part_name;
101 char *helper_image;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800102 char *fw_image_sta;
103 char *fw_image_ap;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100104 struct rxd_ops *ap_rxd_ops;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -0800105 u32 fw_api_ap;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200106};
107
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100108struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200109 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100110
111 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200112 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100113
114 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200115 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100116
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200117 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200118 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200119 struct {
120 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900121 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200122 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100123};
124
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100125struct mwl8k_tx_queue {
126 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200127 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100128
129 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200130 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200132 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200133 struct mwl8k_tx_desc *txd;
134 dma_addr_t txd_dma;
135 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100136};
137
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700138struct mwl8k_ampdu_stream {
139 struct ieee80211_sta *sta;
140 u8 tid;
141 u8 state;
142 u8 idx;
143 u8 txq_idx; /* index of this stream in priv->txq */
144};
145
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100146struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100147 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100148 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100149
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200150 struct mwl8k_device_info *device_info;
151
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100152 void __iomem *sram;
153 void __iomem *regs;
154
155 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800156 const struct firmware *fw_helper;
157 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100159 /* hardware/firmware parameters */
160 bool ap_fw;
161 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100162 struct ieee80211_supported_band band_24;
163 struct ieee80211_channel channels_24[14];
164 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100165 struct ieee80211_supported_band band_50;
166 struct ieee80211_channel channels_50[4];
167 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100168 u32 ap_macids_supported;
169 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100170
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700171 /* Ampdu stream information */
172 u8 num_ampdu_queues;
173
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200174 /* firmware access */
175 struct mutex fw_mutex;
176 struct task_struct *fw_mutex_owner;
177 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200178 struct completion *hostcmd_wait;
179
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100180 /* lock held over TX and TX reap */
181 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100182
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200183 /* TX quiesce completion, protected by fw_mutex and tx_lock */
184 struct completion *tx_wait;
185
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100186 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100187 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100188 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100189
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100190 /* power management status cookie from firmware */
191 u32 *cookie;
192 dma_addr_t cookie_dma;
193
194 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200196 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100197
198 /*
199 * Running count of TX packets in flight, to avoid
200 * iterating over the transmit rings each time.
201 */
202 int pending_tx_pkts;
203
204 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700205 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES + MWL8K_MAX_AMPDU_QUEUES];
206 u32 txq_offset[MWL8K_TX_QUEUES + MWL8K_MAX_AMPDU_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100207
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200208 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200209 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200210 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200211 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100212
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100213 /* XXX need to convert this to handle multiple interfaces */
214 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200215 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100216 struct sk_buff *beacon_skb;
217
218 /*
219 * This FJ worker has to be global as it is scheduled from the
220 * RX handler. At this point we don't know which interface it
221 * belongs to until the list of bssids waiting to complete join
222 * is checked.
223 */
224 struct work_struct finalize_join_worker;
225
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100226 /* Tasklet to perform TX reclaim. */
227 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100228
229 /* Tasklet to perform RX. */
230 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400231
232 /* Most recently reported noise in dBm */
233 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800234
235 /*
236 * preserve the queue configurations so they can be restored if/when
237 * the firmware image is swapped.
238 */
239 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800240
241 /* async firmware loading state */
242 unsigned fw_state;
243 char *fw_pref;
244 char *fw_alt;
245 struct completion firmware_loading_complete;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100246};
247
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800248#define MAX_WEP_KEY_LEN 13
249#define NUM_WEP_KEYS 4
250
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100251/* Per interface specific private data */
252struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100253 struct list_head list;
254 struct ieee80211_vif *vif;
255
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100256 /* Firmware macid for this vif. */
257 int macid;
258
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100259 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100260 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800261
262 /* Saved WEP keys */
263 struct {
264 u8 enabled;
265 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
266 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800267
268 /* BSSID */
269 u8 bssid[ETH_ALEN];
270
271 /* A flag to indicate is HW crypto is enabled for this bssid */
272 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100273};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200274#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800275#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100276
Lennert Buytenheka6804002010-01-04 21:55:42 +0100277struct mwl8k_sta {
278 /* Index into station database. Returned by UPDATE_STADB. */
279 u8 peer_id;
280};
281#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
282
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100283static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100284 { .center_freq = 2412, .hw_value = 1, },
285 { .center_freq = 2417, .hw_value = 2, },
286 { .center_freq = 2422, .hw_value = 3, },
287 { .center_freq = 2427, .hw_value = 4, },
288 { .center_freq = 2432, .hw_value = 5, },
289 { .center_freq = 2437, .hw_value = 6, },
290 { .center_freq = 2442, .hw_value = 7, },
291 { .center_freq = 2447, .hw_value = 8, },
292 { .center_freq = 2452, .hw_value = 9, },
293 { .center_freq = 2457, .hw_value = 10, },
294 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100295 { .center_freq = 2467, .hw_value = 12, },
296 { .center_freq = 2472, .hw_value = 13, },
297 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100298};
299
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100300static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301 { .bitrate = 10, .hw_value = 2, },
302 { .bitrate = 20, .hw_value = 4, },
303 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200304 { .bitrate = 110, .hw_value = 22, },
305 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100306 { .bitrate = 60, .hw_value = 12, },
307 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100308 { .bitrate = 120, .hw_value = 24, },
309 { .bitrate = 180, .hw_value = 36, },
310 { .bitrate = 240, .hw_value = 48, },
311 { .bitrate = 360, .hw_value = 72, },
312 { .bitrate = 480, .hw_value = 96, },
313 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100314 { .bitrate = 720, .hw_value = 144, },
315};
316
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100317static const struct ieee80211_channel mwl8k_channels_50[] = {
318 { .center_freq = 5180, .hw_value = 36, },
319 { .center_freq = 5200, .hw_value = 40, },
320 { .center_freq = 5220, .hw_value = 44, },
321 { .center_freq = 5240, .hw_value = 48, },
322};
323
324static const struct ieee80211_rate mwl8k_rates_50[] = {
325 { .bitrate = 60, .hw_value = 12, },
326 { .bitrate = 90, .hw_value = 18, },
327 { .bitrate = 120, .hw_value = 24, },
328 { .bitrate = 180, .hw_value = 36, },
329 { .bitrate = 240, .hw_value = 48, },
330 { .bitrate = 360, .hw_value = 72, },
331 { .bitrate = 480, .hw_value = 96, },
332 { .bitrate = 540, .hw_value = 108, },
333 { .bitrate = 720, .hw_value = 144, },
334};
335
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100336/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100337#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800338#define MWL8K_CMD_SET 0x0001
339#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100340
341/* Firmware command codes */
342#define MWL8K_CMD_CODE_DNLD 0x0001
343#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200344#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100345#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
346#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200347#define MWL8K_CMD_RADIO_CONTROL 0x001c
348#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800349#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200350#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100351#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100352#define MWL8K_CMD_SET_PRE_SCAN 0x0107
353#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200354#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100355#define MWL8K_CMD_SET_AID 0x010d
356#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200357#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100358#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200359#define MWL8K_CMD_SET_SLOT 0x0114
360#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
361#define MWL8K_CMD_SET_WMM_MODE 0x0123
362#define MWL8K_CMD_MIMO_CONFIG 0x0125
363#define MWL8K_CMD_USE_FIXED_RATE 0x0126
364#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100365#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200366#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100367#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
368#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800369#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200370#define MWL8K_CMD_UPDATE_STADB 0x1123
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700371#define MWL8K_CMD_BASTREAM 0x1125
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100372
John W. Linvilleb6037422010-07-20 13:55:00 -0400373static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100374{
John W. Linvilleb6037422010-07-20 13:55:00 -0400375 u16 command = le16_to_cpu(cmd);
376
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100377#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
378 snprintf(buf, bufsize, "%s", #x);\
379 return buf;\
380 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400381 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100382 MWL8K_CMDNAME(CODE_DNLD);
383 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200384 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100385 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
386 MWL8K_CMDNAME(GET_STAT);
387 MWL8K_CMDNAME(RADIO_CONTROL);
388 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800389 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200390 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100391 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100392 MWL8K_CMDNAME(SET_PRE_SCAN);
393 MWL8K_CMDNAME(SET_POST_SCAN);
394 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100395 MWL8K_CMDNAME(SET_AID);
396 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200397 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100398 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200399 MWL8K_CMDNAME(SET_SLOT);
400 MWL8K_CMDNAME(SET_EDCA_PARAMS);
401 MWL8K_CMDNAME(SET_WMM_MODE);
402 MWL8K_CMDNAME(MIMO_CONFIG);
403 MWL8K_CMDNAME(USE_FIXED_RATE);
404 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200405 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200406 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100407 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100408 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800409 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200410 MWL8K_CMDNAME(UPDATE_STADB);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700411 MWL8K_CMDNAME(BASTREAM);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100412 default:
413 snprintf(buf, bufsize, "0x%x", cmd);
414 }
415#undef MWL8K_CMDNAME
416
417 return buf;
418}
419
420/* Hardware and firmware reset */
421static void mwl8k_hw_reset(struct mwl8k_priv *priv)
422{
423 iowrite32(MWL8K_H2A_INT_RESET,
424 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
425 iowrite32(MWL8K_H2A_INT_RESET,
426 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
427 msleep(20);
428}
429
430/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800431static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100432{
433 if (*fw == NULL)
434 return;
435 release_firmware(*fw);
436 *fw = NULL;
437}
438
439static void mwl8k_release_firmware(struct mwl8k_priv *priv)
440{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100441 mwl8k_release_fw(&priv->fw_ucode);
442 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100443}
444
Brian Cavagnolo99020472010-11-12 17:23:52 -0800445/* states for asynchronous f/w loading */
446static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
447enum {
448 FW_STATE_INIT = 0,
449 FW_STATE_LOADING_PREF,
450 FW_STATE_LOADING_ALT,
451 FW_STATE_ERROR,
452};
453
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100454/* Request fw image */
455static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800456 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800457 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100458{
459 /* release current image */
460 if (*fw != NULL)
461 mwl8k_release_fw(fw);
462
Brian Cavagnolo99020472010-11-12 17:23:52 -0800463 if (nowait)
464 return request_firmware_nowait(THIS_MODULE, 1, fname,
465 &priv->pdev->dev, GFP_KERNEL,
466 priv, mwl8k_fw_state_machine);
467 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800468 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100469}
470
Brian Cavagnolo99020472010-11-12 17:23:52 -0800471static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
472 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100473{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200474 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100475 int rc;
476
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200477 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800478 if (nowait)
479 rc = mwl8k_request_fw(priv, di->helper_image,
480 &priv->fw_helper, true);
481 else
482 rc = mwl8k_request_fw(priv, di->helper_image,
483 &priv->fw_helper, false);
484 if (rc)
485 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
486 pci_name(priv->pdev), di->helper_image);
487
488 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200489 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100490 }
491
Brian Cavagnolo99020472010-11-12 17:23:52 -0800492 if (nowait) {
493 /*
494 * if we get here, no helper image is needed. Skip the
495 * FW_STATE_INIT state.
496 */
497 priv->fw_state = FW_STATE_LOADING_PREF;
498 rc = mwl8k_request_fw(priv, fw_image,
499 &priv->fw_ucode,
500 true);
501 } else
502 rc = mwl8k_request_fw(priv, fw_image,
503 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100504 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200505 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800506 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100507 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100508 return rc;
509 }
510
511 return 0;
512}
513
514struct mwl8k_cmd_pkt {
515 __le16 code;
516 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100517 __u8 seq_num;
518 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100519 __le16 result;
520 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000521} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100522
523/*
524 * Firmware loading.
525 */
526static int
527mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
528{
529 void __iomem *regs = priv->regs;
530 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100531 int loops;
532
533 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
534 if (pci_dma_mapping_error(priv->pdev, dma_addr))
535 return -ENOMEM;
536
537 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
538 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
539 iowrite32(MWL8K_H2A_INT_DOORBELL,
540 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
541 iowrite32(MWL8K_H2A_INT_DUMMY,
542 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
543
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100544 loops = 1000;
545 do {
546 u32 int_code;
547
548 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
549 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
550 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100551 break;
552 }
553
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200554 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100555 udelay(1);
556 } while (--loops);
557
558 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
559
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200560 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100561}
562
563static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
564 const u8 *data, size_t length)
565{
566 struct mwl8k_cmd_pkt *cmd;
567 int done;
568 int rc = 0;
569
570 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
571 if (cmd == NULL)
572 return -ENOMEM;
573
574 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
575 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100576 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100577 cmd->result = 0;
578
579 done = 0;
580 while (length) {
581 int block_size = length > 256 ? 256 : length;
582
583 memcpy(cmd->payload, data + done, block_size);
584 cmd->length = cpu_to_le16(block_size);
585
586 rc = mwl8k_send_fw_load_cmd(priv, cmd,
587 sizeof(*cmd) + block_size);
588 if (rc)
589 break;
590
591 done += block_size;
592 length -= block_size;
593 }
594
595 if (!rc) {
596 cmd->length = 0;
597 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
598 }
599
600 kfree(cmd);
601
602 return rc;
603}
604
605static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
606 const u8 *data, size_t length)
607{
608 unsigned char *buffer;
609 int may_continue, rc = 0;
610 u32 done, prev_block_size;
611
612 buffer = kmalloc(1024, GFP_KERNEL);
613 if (buffer == NULL)
614 return -ENOMEM;
615
616 done = 0;
617 prev_block_size = 0;
618 may_continue = 1000;
619 while (may_continue > 0) {
620 u32 block_size;
621
622 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
623 if (block_size & 1) {
624 block_size &= ~1;
625 may_continue--;
626 } else {
627 done += prev_block_size;
628 length -= prev_block_size;
629 }
630
631 if (block_size > 1024 || block_size > length) {
632 rc = -EOVERFLOW;
633 break;
634 }
635
636 if (length == 0) {
637 rc = 0;
638 break;
639 }
640
641 if (block_size == 0) {
642 rc = -EPROTO;
643 may_continue--;
644 udelay(1);
645 continue;
646 }
647
648 prev_block_size = block_size;
649 memcpy(buffer, data + done, block_size);
650
651 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
652 if (rc)
653 break;
654 }
655
656 if (!rc && length != 0)
657 rc = -EREMOTEIO;
658
659 kfree(buffer);
660
661 return rc;
662}
663
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200664static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100665{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200666 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800667 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200668 int rc;
669 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100670
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200671 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800672 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100673
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200674 if (helper == NULL) {
675 printk(KERN_ERR "%s: helper image needed but none "
676 "given\n", pci_name(priv->pdev));
677 return -EINVAL;
678 }
679
680 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100681 if (rc) {
682 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200683 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100684 return rc;
685 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100686 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100687
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200688 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100689 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200690 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100691 }
692
693 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200694 printk(KERN_ERR "%s: unable to load firmware image\n",
695 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100696 return rc;
697 }
698
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100699 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100700
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100701 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100702 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200703 u32 ready_code;
704
705 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
706 if (ready_code == MWL8K_FWAP_READY) {
707 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100708 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200709 } else if (ready_code == MWL8K_FWSTA_READY) {
710 priv->ap_fw = 0;
711 break;
712 }
713
714 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100715 udelay(1);
716 } while (--loops);
717
718 return loops ? 0 : -ETIMEDOUT;
719}
720
721
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100722/* DMA header used by firmware and hardware. */
723struct mwl8k_dma_data {
724 __le16 fwlen;
725 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100726 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000727} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100728
729/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100730static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100731{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100732 struct mwl8k_dma_data *tr;
733 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100734
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100735 tr = (struct mwl8k_dma_data *)skb->data;
736 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
737
738 if (hdrlen != sizeof(tr->wh)) {
739 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
740 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
741 *((__le16 *)(tr->data - 2)) = qos;
742 } else {
743 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
744 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100745 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100746
747 if (hdrlen != sizeof(*tr))
748 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100749}
750
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800751static void
752mwl8k_add_dma_header(struct sk_buff *skb, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100753{
754 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100755 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800756 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100757 struct mwl8k_dma_data *tr;
758
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100759 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100760 * Add a firmware DMA header; the firmware requires that we
761 * present a 2-byte payload length followed by a 4-address
762 * header (without QoS field), followed (optionally) by any
763 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100764 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100765 wh = (struct ieee80211_hdr *)skb->data;
766
767 hdrlen = ieee80211_hdrlen(wh->frame_control);
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800768 reqd_hdrlen = sizeof(*tr);
769
770 if (hdrlen != reqd_hdrlen)
771 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100772
773 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800774 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100775
776 tr = (struct mwl8k_dma_data *)skb->data;
777 if (wh != &tr->wh)
778 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100779 if (hdrlen != sizeof(tr->wh))
780 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100781
782 /*
783 * Firmware length is the length of the fully formed "802.11
784 * payload". That is, everything except for the 802.11 header.
785 * This includes all crypto material including the MIC.
786 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800787 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100788}
789
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800790static void mwl8k_encapsulate_tx_frame(struct sk_buff *skb)
791{
792 struct ieee80211_hdr *wh;
793 struct ieee80211_tx_info *tx_info;
794 struct ieee80211_key_conf *key_conf;
795 int data_pad;
796
797 wh = (struct ieee80211_hdr *)skb->data;
798
799 tx_info = IEEE80211_SKB_CB(skb);
800
801 key_conf = NULL;
802 if (ieee80211_is_data(wh->frame_control))
803 key_conf = tx_info->control.hw_key;
804
805 /*
806 * Make sure the packet header is in the DMA header format (4-address
807 * without QoS), the necessary crypto padding between the header and the
808 * payload has already been provided by mac80211, but it doesn't add tail
809 * padding when HW crypto is enabled.
810 *
811 * We have the following trailer padding requirements:
812 * - WEP: 4 trailer bytes (ICV)
813 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
814 * - CCMP: 8 trailer bytes (MIC)
815 */
816 data_pad = 0;
817 if (key_conf != NULL) {
818 switch (key_conf->cipher) {
819 case WLAN_CIPHER_SUITE_WEP40:
820 case WLAN_CIPHER_SUITE_WEP104:
821 data_pad = 4;
822 break;
823 case WLAN_CIPHER_SUITE_TKIP:
824 data_pad = 12;
825 break;
826 case WLAN_CIPHER_SUITE_CCMP:
827 data_pad = 8;
828 break;
829 }
830 }
831 mwl8k_add_dma_header(skb, data_pad);
832}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100833
834/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100835 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200836 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100837struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200838 __le16 pkt_len;
839 __u8 sq2;
840 __u8 rate;
841 __le32 pkt_phys_addr;
842 __le32 next_rxd_phys_addr;
843 __le16 qos_control;
844 __le16 htsig2;
845 __le32 hw_rssi_info;
846 __le32 hw_noise_floor_info;
847 __u8 noise_floor;
848 __u8 pad0[3];
849 __u8 rssi;
850 __u8 rx_status;
851 __u8 channel;
852 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000853} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200854
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100855#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
856#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
857#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100858
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100859#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200860
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800861/* 8366 AP rx_status bits */
862#define MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
863#define MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
864#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
865#define MWL8K_8366_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
866#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
867
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100868static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200869{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100870 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200871
872 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100873 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200874}
875
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100876static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200877{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100878 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200879
880 rxd->pkt_len = cpu_to_le16(len);
881 rxd->pkt_phys_addr = cpu_to_le32(addr);
882 wmb();
883 rxd->rx_ctrl = 0;
884}
885
886static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100887mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400888 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200889{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100890 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200891
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100892 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200893 return -1;
894 rmb();
895
896 memset(status, 0, sizeof(*status));
897
898 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400899 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200900
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100901 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200902 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100903 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100904 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100905 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200906 } else {
907 int i;
908
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100909 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
910 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200911 status->rate_idx = i;
912 break;
913 }
914 }
915 }
916
Lennert Buytenhek85478342010-01-12 13:48:56 +0100917 if (rxd->channel > 14) {
918 status->band = IEEE80211_BAND_5GHZ;
919 if (!(status->flag & RX_FLAG_HT))
920 status->rate_idx -= 5;
921 } else {
922 status->band = IEEE80211_BAND_2GHZ;
923 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900924 status->freq = ieee80211_channel_to_frequency(rxd->channel,
925 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200926
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100927 *qos = rxd->qos_control;
928
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800929 if ((rxd->rx_status != MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
930 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK) &&
931 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
932 status->flag |= RX_FLAG_MMIC_ERROR;
933
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200934 return le16_to_cpu(rxd->pkt_len);
935}
936
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100937static struct rxd_ops rxd_8366_ap_ops = {
938 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
939 .rxd_init = mwl8k_rxd_8366_ap_init,
940 .rxd_refill = mwl8k_rxd_8366_ap_refill,
941 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200942};
943
944/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100945 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100946 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100947struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100948 __le16 pkt_len;
949 __u8 link_quality;
950 __u8 noise_level;
951 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200952 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100953 __le16 qos_control;
954 __le16 rate_info;
955 __le32 pad0[4];
956 __u8 rssi;
957 __u8 channel;
958 __le16 pad1;
959 __u8 rx_ctrl;
960 __u8 rx_status;
961 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000962} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100963
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100964#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
965#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
966#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
967#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
968#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
969#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200970
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100971#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800972#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
973/* ICV=0 or MIC=1 */
974#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
975/* Key is uploaded only in failure case */
976#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200977
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100978static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200979{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100980 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200981
982 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100983 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200984}
985
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100986static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200987{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100988 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200989
990 rxd->pkt_len = cpu_to_le16(len);
991 rxd->pkt_phys_addr = cpu_to_le32(addr);
992 wmb();
993 rxd->rx_ctrl = 0;
994}
995
996static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100997mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400998 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200999{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001000 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001001 u16 rate_info;
1002
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001003 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001004 return -1;
1005 rmb();
1006
1007 rate_info = le16_to_cpu(rxd->rate_info);
1008
1009 memset(status, 0, sizeof(*status));
1010
1011 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -04001012 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001013 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1014 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001015
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001016 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001017 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001018 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001019 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001020 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001021 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001022 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001023 status->flag |= RX_FLAG_HT;
1024
Lennert Buytenhek85478342010-01-12 13:48:56 +01001025 if (rxd->channel > 14) {
1026 status->band = IEEE80211_BAND_5GHZ;
1027 if (!(status->flag & RX_FLAG_HT))
1028 status->rate_idx -= 5;
1029 } else {
1030 status->band = IEEE80211_BAND_2GHZ;
1031 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001032 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1033 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001034
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001035 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001036 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1037 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1038 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001039
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001040 return le16_to_cpu(rxd->pkt_len);
1041}
1042
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001043static struct rxd_ops rxd_sta_ops = {
1044 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1045 .rxd_init = mwl8k_rxd_sta_init,
1046 .rxd_refill = mwl8k_rxd_sta_refill,
1047 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001048};
1049
1050
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001051#define MWL8K_RX_DESCS 256
1052#define MWL8K_RX_MAXSZ 3800
1053
1054static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1055{
1056 struct mwl8k_priv *priv = hw->priv;
1057 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1058 int size;
1059 int i;
1060
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001061 rxq->rxd_count = 0;
1062 rxq->head = 0;
1063 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001064
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001065 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001066
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001067 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1068 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001069 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001070 return -ENOMEM;
1071 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001072 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001073
Shan Weib9ede5f2011-03-08 11:02:03 +08001074 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001075 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001076 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001077 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001078 return -ENOMEM;
1079 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001080
1081 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001082 int desc_size;
1083 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001084 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001085 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001086
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001087 desc_size = priv->rxd_ops->rxd_size;
1088 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001089
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001090 nexti = i + 1;
1091 if (nexti == MWL8K_RX_DESCS)
1092 nexti = 0;
1093 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1094
1095 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001096 }
1097
1098 return 0;
1099}
1100
1101static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1102{
1103 struct mwl8k_priv *priv = hw->priv;
1104 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1105 int refilled;
1106
1107 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001108 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001109 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001110 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001111 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001112 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001113
1114 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1115 if (skb == NULL)
1116 break;
1117
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001118 addr = pci_map_single(priv->pdev, skb->data,
1119 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001120
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001121 rxq->rxd_count++;
1122 rx = rxq->tail++;
1123 if (rxq->tail == MWL8K_RX_DESCS)
1124 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001125 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001126 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001127
1128 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1129 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001130
1131 refilled++;
1132 }
1133
1134 return refilled;
1135}
1136
1137/* Must be called only when the card's reception is completely halted */
1138static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1139{
1140 struct mwl8k_priv *priv = hw->priv;
1141 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1142 int i;
1143
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001144 if (rxq->rxd == NULL)
1145 return;
1146
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001147 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001148 if (rxq->buf[i].skb != NULL) {
1149 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001150 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001151 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001152 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001153
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001154 kfree_skb(rxq->buf[i].skb);
1155 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001156 }
1157 }
1158
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001159 kfree(rxq->buf);
1160 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001161
1162 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001163 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001164 rxq->rxd, rxq->rxd_dma);
1165 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001166}
1167
1168
1169/*
1170 * Scan a list of BSSIDs to process for finalize join.
1171 * Allows for extension to process multiple BSSIDs.
1172 */
1173static inline int
1174mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1175{
1176 return priv->capture_beacon &&
1177 ieee80211_is_beacon(wh->frame_control) &&
1178 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1179}
1180
Lennert Buytenhek37797522009-10-22 20:19:45 +02001181static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1182 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001183{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001184 struct mwl8k_priv *priv = hw->priv;
1185
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001186 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001187 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001188
1189 /*
1190 * Use GFP_ATOMIC as rxq_process is called from
1191 * the primary interrupt handler, memory allocation call
1192 * must not sleep.
1193 */
1194 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1195 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001196 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001197}
1198
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001199static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1200 u8 *bssid)
1201{
1202 struct mwl8k_vif *mwl8k_vif;
1203
1204 list_for_each_entry(mwl8k_vif,
1205 vif_list, list) {
1206 if (memcmp(bssid, mwl8k_vif->bssid,
1207 ETH_ALEN) == 0)
1208 return mwl8k_vif;
1209 }
1210
1211 return NULL;
1212}
1213
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001214static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1215{
1216 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001217 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001218 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1219 int processed;
1220
1221 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001222 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001223 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001224 void *rxd;
1225 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001226 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001227 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001228 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001229
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001230 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001231 if (skb == NULL)
1232 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001233
1234 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1235
John W. Linville0d462bb2010-07-28 14:04:24 -04001236 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1237 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001238 if (pkt_len < 0)
1239 break;
1240
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001241 rxq->buf[rxq->head].skb = NULL;
1242
1243 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001244 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001245 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001246 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001247
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001248 rxq->head++;
1249 if (rxq->head == MWL8K_RX_DESCS)
1250 rxq->head = 0;
1251
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001252 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001253
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001254 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001255
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001256 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001257 * Check for a pending join operation. Save a
1258 * copy of the beacon and schedule a tasklet to
1259 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001260 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001261 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001262 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001263
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001264 if (ieee80211_has_protected(wh->frame_control)) {
1265
1266 /* Check if hw crypto has been enabled for
1267 * this bss. If yes, set the status flags
1268 * accordingly
1269 */
1270 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1271 wh->addr1);
1272
1273 if (mwl8k_vif != NULL &&
1274 mwl8k_vif->is_hw_crypto_enabled == true) {
1275 /*
1276 * When MMIC ERROR is encountered
1277 * by the firmware, payload is
1278 * dropped and only 32 bytes of
1279 * mwl8k Firmware header is sent
1280 * to the host.
1281 *
1282 * We need to add four bytes of
1283 * key information. In it
1284 * MAC80211 expects keyidx set to
1285 * 0 for triggering Counter
1286 * Measure of MMIC failure.
1287 */
1288 if (status.flag & RX_FLAG_MMIC_ERROR) {
1289 struct mwl8k_dma_data *tr;
1290 tr = (struct mwl8k_dma_data *)skb->data;
1291 memset((void *)&(tr->data), 0, 4);
1292 pkt_len += 4;
1293 }
1294
1295 if (!ieee80211_is_auth(wh->frame_control))
1296 status.flag |= RX_FLAG_IV_STRIPPED |
1297 RX_FLAG_DECRYPTED |
1298 RX_FLAG_MMIC_STRIPPED;
1299 }
1300 }
1301
1302 skb_put(skb, pkt_len);
1303 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001304 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1305 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001306
1307 processed++;
1308 }
1309
1310 return processed;
1311}
1312
1313
1314/*
1315 * Packet transmission.
1316 */
1317
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001318#define MWL8K_TXD_STATUS_OK 0x00000001
1319#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1320#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1321#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001322#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001323
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001324#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1325#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1326#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1327#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1328#define MWL8K_QOS_EOSP 0x0010
1329
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001330struct mwl8k_tx_desc {
1331 __le32 status;
1332 __u8 data_rate;
1333 __u8 tx_priority;
1334 __le16 qos_control;
1335 __le32 pkt_phys_addr;
1336 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001337 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001338 __le32 next_txd_phys_addr;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07001339 __le32 timestamp;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340 __le16 rate_info;
1341 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001342 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001343} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001344
1345#define MWL8K_TX_DESCS 128
1346
1347static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1348{
1349 struct mwl8k_priv *priv = hw->priv;
1350 struct mwl8k_tx_queue *txq = priv->txq + index;
1351 int size;
1352 int i;
1353
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001354 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001355 txq->head = 0;
1356 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001357
1358 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1359
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001360 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1361 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001362 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001363 return -ENOMEM;
1364 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001365 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001366
Shan Weib9ede5f2011-03-08 11:02:03 +08001367 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001368 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001369 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001370 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001371 return -ENOMEM;
1372 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001373
1374 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1375 struct mwl8k_tx_desc *tx_desc;
1376 int nexti;
1377
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001378 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001379 nexti = (i + 1) % MWL8K_TX_DESCS;
1380
1381 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001382 tx_desc->next_txd_phys_addr =
1383 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001384 }
1385
1386 return 0;
1387}
1388
1389static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1390{
1391 iowrite32(MWL8K_H2A_INT_PPA_READY,
1392 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1393 iowrite32(MWL8K_H2A_INT_DUMMY,
1394 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1395 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1396}
1397
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001398static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001399{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001400 struct mwl8k_priv *priv = hw->priv;
1401 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001402
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001403 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1404 struct mwl8k_tx_queue *txq = priv->txq + i;
1405 int fw_owned = 0;
1406 int drv_owned = 0;
1407 int unused = 0;
1408 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001409
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001410 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001411 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1412 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001413
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001414 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001415 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001416 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001417 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001418 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001419
1420 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001421 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001422 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001423
Joe Perchesc96c31e2010-07-26 14:39:58 -07001424 wiphy_err(hw->wiphy,
1425 "txq[%d] len=%d head=%d tail=%d "
1426 "fw_owned=%d drv_owned=%d unused=%d\n",
1427 i,
1428 txq->len, txq->head, txq->tail,
1429 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001430 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001431}
1432
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001433/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001434 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001435 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001436#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001437
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001438static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001440 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001441 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001442 int retry;
1443 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001444
1445 might_sleep();
1446
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001447 /*
1448 * The TX queues are stopped at this point, so this test
1449 * doesn't need to take ->tx_lock.
1450 */
1451 if (!priv->pending_tx_pkts)
1452 return 0;
1453
1454 retry = 0;
1455 rc = 0;
1456
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001457 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001458 priv->tx_wait = &tx_wait;
1459 while (!rc) {
1460 int oldcount;
1461 unsigned long timeout;
1462
1463 oldcount = priv->pending_tx_pkts;
1464
1465 spin_unlock_bh(&priv->tx_lock);
1466 timeout = wait_for_completion_timeout(&tx_wait,
1467 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1468 spin_lock_bh(&priv->tx_lock);
1469
1470 if (timeout) {
1471 WARN_ON(priv->pending_tx_pkts);
1472 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001473 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001474 }
1475 break;
1476 }
1477
1478 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001479 wiphy_notice(hw->wiphy,
1480 "waiting for tx rings to drain (%d -> %d pkts)\n",
1481 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001482 retry = 1;
1483 continue;
1484 }
1485
1486 priv->tx_wait = NULL;
1487
Joe Perchesc96c31e2010-07-26 14:39:58 -07001488 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1489 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001490 mwl8k_dump_tx_rings(hw);
1491
1492 rc = -ETIMEDOUT;
1493 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001494 spin_unlock_bh(&priv->tx_lock);
1495
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001496 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001497}
1498
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001499#define MWL8K_TXD_SUCCESS(status) \
1500 ((status) & (MWL8K_TXD_STATUS_OK | \
1501 MWL8K_TXD_STATUS_OK_RETRY | \
1502 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001503
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001504static int
1505mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001506{
1507 struct mwl8k_priv *priv = hw->priv;
1508 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001509 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001510
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001511 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001512 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001513 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001514 struct mwl8k_tx_desc *tx_desc;
1515 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001516 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001517 struct sk_buff *skb;
1518 struct ieee80211_tx_info *info;
1519 u32 status;
1520
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001521 tx = txq->head;
1522 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001523
1524 status = le32_to_cpu(tx_desc->status);
1525
1526 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1527 if (!force)
1528 break;
1529 tx_desc->status &=
1530 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1531 }
1532
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001533 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001534 BUG_ON(txq->len == 0);
1535 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001536 priv->pending_tx_pkts--;
1537
1538 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001539 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001540 skb = txq->skb[tx];
1541 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001542
1543 BUG_ON(skb == NULL);
1544 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1545
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001546 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001547
1548 /* Mark descriptor as unused */
1549 tx_desc->pkt_phys_addr = 0;
1550 tx_desc->pkt_len = 0;
1551
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001552 info = IEEE80211_SKB_CB(skb);
1553 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001554
1555 /* Rate control is happening in the firmware.
1556 * Ensure no tx rate is being reported.
1557 */
1558 info->status.rates[0].idx = -1;
1559 info->status.rates[0].count = 1;
1560
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001561 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001562 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001563
1564 ieee80211_tx_status_irqsafe(hw, skb);
1565
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001566 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001567 }
1568
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001569 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001570 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001571
1572 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001573}
1574
1575/* must be called only when the card's transmit is completely halted */
1576static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1577{
1578 struct mwl8k_priv *priv = hw->priv;
1579 struct mwl8k_tx_queue *txq = priv->txq + index;
1580
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001581 if (txq->txd == NULL)
1582 return;
1583
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001584 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001585
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001586 kfree(txq->skb);
1587 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001588
1589 pci_free_consistent(priv->pdev,
1590 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001591 txq->txd, txq->txd_dma);
1592 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001593}
1594
Johannes Berg7bb45682011-02-24 14:42:06 +01001595static void
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001596mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1597{
1598 struct mwl8k_priv *priv = hw->priv;
1599 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001600 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001601 struct ieee80211_hdr *wh;
1602 struct mwl8k_tx_queue *txq;
1603 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001604 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001605 u32 txstatus;
1606 u8 txdatarate;
1607 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001608
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001609 wh = (struct ieee80211_hdr *)skb->data;
1610 if (ieee80211_is_data_qos(wh->frame_control))
1611 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1612 else
1613 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001614
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001615 if (priv->ap_fw)
1616 mwl8k_encapsulate_tx_frame(skb);
1617 else
1618 mwl8k_add_dma_header(skb, 0);
1619
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001620 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001621
1622 tx_info = IEEE80211_SKB_CB(skb);
1623 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001624
1625 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001626 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001627 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1628 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001629 }
1630
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001631 /* Setup firmware control bit fields for each frame type. */
1632 txstatus = 0;
1633 txdatarate = 0;
1634 if (ieee80211_is_mgmt(wh->frame_control) ||
1635 ieee80211_is_ctl(wh->frame_control)) {
1636 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001637 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001638 } else if (ieee80211_is_data(wh->frame_control)) {
1639 txdatarate = 1;
1640 if (is_multicast_ether_addr(wh->addr1))
1641 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1642
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001643 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001644 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001645 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001646 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001647 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001648 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001649
1650 dma = pci_map_single(priv->pdev, skb->data,
1651 skb->len, PCI_DMA_TODEVICE);
1652
1653 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001654 wiphy_debug(hw->wiphy,
1655 "failed to dma map skb, dropping TX frame.\n");
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001656 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001657 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001658 }
1659
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001660 spin_lock_bh(&priv->tx_lock);
1661
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001662 txq = priv->txq + index;
1663
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001664 BUG_ON(txq->skb[txq->tail] != NULL);
1665 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001666
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001667 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001668 tx->data_rate = txdatarate;
1669 tx->tx_priority = index;
1670 tx->qos_control = cpu_to_le16(qos);
1671 tx->pkt_phys_addr = cpu_to_le32(dma);
1672 tx->pkt_len = cpu_to_le16(skb->len);
1673 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001674 if (!priv->ap_fw && tx_info->control.sta != NULL)
1675 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1676 else
1677 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001678 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001679 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1680
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001681 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001682 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001683
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001684 txq->tail++;
1685 if (txq->tail == MWL8K_TX_DESCS)
1686 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001687
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001688 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001689 ieee80211_stop_queue(hw, index);
1690
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001691 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001692
1693 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001694}
1695
1696
1697/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001698 * Firmware access.
1699 *
1700 * We have the following requirements for issuing firmware commands:
1701 * - Some commands require that the packet transmit path is idle when
1702 * the command is issued. (For simplicity, we'll just quiesce the
1703 * transmit path for every command.)
1704 * - There are certain sequences of commands that need to be issued to
1705 * the hardware sequentially, with no other intervening commands.
1706 *
1707 * This leads to an implementation of a "firmware lock" as a mutex that
1708 * can be taken recursively, and which is taken by both the low-level
1709 * command submission function (mwl8k_post_cmd) as well as any users of
1710 * that function that require issuing of an atomic sequence of commands,
1711 * and quiesces the transmit path whenever it's taken.
1712 */
1713static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1714{
1715 struct mwl8k_priv *priv = hw->priv;
1716
1717 if (priv->fw_mutex_owner != current) {
1718 int rc;
1719
1720 mutex_lock(&priv->fw_mutex);
1721 ieee80211_stop_queues(hw);
1722
1723 rc = mwl8k_tx_wait_empty(hw);
1724 if (rc) {
1725 ieee80211_wake_queues(hw);
1726 mutex_unlock(&priv->fw_mutex);
1727
1728 return rc;
1729 }
1730
1731 priv->fw_mutex_owner = current;
1732 }
1733
1734 priv->fw_mutex_depth++;
1735
1736 return 0;
1737}
1738
1739static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1740{
1741 struct mwl8k_priv *priv = hw->priv;
1742
1743 if (!--priv->fw_mutex_depth) {
1744 ieee80211_wake_queues(hw);
1745 priv->fw_mutex_owner = NULL;
1746 mutex_unlock(&priv->fw_mutex);
1747 }
1748}
1749
1750
1751/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001752 * Command processing.
1753 */
1754
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001755/* Timeout firmware commands after 10s */
1756#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001757
1758static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1759{
1760 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1761 struct mwl8k_priv *priv = hw->priv;
1762 void __iomem *regs = priv->regs;
1763 dma_addr_t dma_addr;
1764 unsigned int dma_size;
1765 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001766 unsigned long timeout = 0;
1767 u8 buf[32];
1768
John W. Linvilleb6037422010-07-20 13:55:00 -04001769 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001770 dma_size = le16_to_cpu(cmd->length);
1771 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1772 PCI_DMA_BIDIRECTIONAL);
1773 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1774 return -ENOMEM;
1775
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001776 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001777 if (rc) {
1778 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1779 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001780 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001781 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001782
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001783 priv->hostcmd_wait = &cmd_wait;
1784 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1785 iowrite32(MWL8K_H2A_INT_DOORBELL,
1786 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1787 iowrite32(MWL8K_H2A_INT_DUMMY,
1788 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001789
1790 timeout = wait_for_completion_timeout(&cmd_wait,
1791 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1792
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001793 priv->hostcmd_wait = NULL;
1794
1795 mwl8k_fw_unlock(hw);
1796
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001797 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1798 PCI_DMA_BIDIRECTIONAL);
1799
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001800 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001801 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001802 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1803 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001804 rc = -ETIMEDOUT;
1805 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001806 int ms;
1807
1808 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1809
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001810 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001811 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001812 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001813 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1814 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001815 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001816 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001817 mwl8k_cmd_name(cmd->code,
1818 buf, sizeof(buf)),
1819 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001820 }
1821
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001822 return rc;
1823}
1824
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001825static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1826 struct ieee80211_vif *vif,
1827 struct mwl8k_cmd_pkt *cmd)
1828{
1829 if (vif != NULL)
1830 cmd->macid = MWL8K_VIF(vif)->macid;
1831 return mwl8k_post_cmd(hw, cmd);
1832}
1833
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001834/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001835 * Setup code shared between STA and AP firmware images.
1836 */
1837static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1838{
1839 struct mwl8k_priv *priv = hw->priv;
1840
1841 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1842 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1843
1844 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1845 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1846
1847 priv->band_24.band = IEEE80211_BAND_2GHZ;
1848 priv->band_24.channels = priv->channels_24;
1849 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1850 priv->band_24.bitrates = priv->rates_24;
1851 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1852
1853 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1854}
1855
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001856static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1857{
1858 struct mwl8k_priv *priv = hw->priv;
1859
1860 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1861 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1862
1863 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1864 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1865
1866 priv->band_50.band = IEEE80211_BAND_5GHZ;
1867 priv->band_50.channels = priv->channels_50;
1868 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1869 priv->band_50.bitrates = priv->rates_50;
1870 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1871
1872 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1873}
1874
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001875/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001876 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001877 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001878struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001879 struct mwl8k_cmd_pkt header;
1880 __u8 hw_rev;
1881 __u8 host_interface;
1882 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001883 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001884 __le16 region_code;
1885 __le32 fw_rev;
1886 __le32 ps_cookie;
1887 __le32 caps;
1888 __u8 mcs_bitmap[16];
1889 __le32 rx_queue_ptr;
1890 __le32 num_tx_queues;
1891 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1892 __le32 caps2;
1893 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001894 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001895} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001896
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001897#define MWL8K_CAP_MAX_AMSDU 0x20000000
1898#define MWL8K_CAP_GREENFIELD 0x08000000
1899#define MWL8K_CAP_AMPDU 0x04000000
1900#define MWL8K_CAP_RX_STBC 0x01000000
1901#define MWL8K_CAP_TX_STBC 0x00800000
1902#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1903#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1904#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1905#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1906#define MWL8K_CAP_DELAY_BA 0x00003000
1907#define MWL8K_CAP_MIMO 0x00000200
1908#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001909#define MWL8K_CAP_BAND_MASK 0x00000007
1910#define MWL8K_CAP_5GHZ 0x00000004
1911#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001912
Lennert Buytenhek06953232010-01-12 13:49:41 +01001913static void
1914mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1915 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001916{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001917 int rx_streams;
1918 int tx_streams;
1919
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001920 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001921
1922 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001923 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001924 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001925 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001926 if (cap & MWL8K_CAP_AMPDU) {
1927 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001928 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1929 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001930 }
1931 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001932 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001933 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001934 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001935 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001936 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001937 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001938 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001939 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001940 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001941 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001942 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001943
1944 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1945 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1946
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001947 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001948 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001949 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001950 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001951 band->ht_cap.mcs.rx_mask[2] = 0xff;
1952 band->ht_cap.mcs.rx_mask[4] = 0x01;
1953 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001954
1955 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001956 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1957 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001958 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1959 }
1960}
1961
Lennert Buytenhek06953232010-01-12 13:49:41 +01001962static void
1963mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1964{
1965 struct mwl8k_priv *priv = hw->priv;
1966
1967 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1968 mwl8k_setup_2ghz_band(hw);
1969 if (caps & MWL8K_CAP_MIMO)
1970 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1971 }
1972
1973 if (caps & MWL8K_CAP_5GHZ) {
1974 mwl8k_setup_5ghz_band(hw);
1975 if (caps & MWL8K_CAP_MIMO)
1976 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1977 }
1978}
1979
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001980static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001981{
1982 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001983 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001984 int rc;
1985 int i;
1986
1987 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1988 if (cmd == NULL)
1989 return -ENOMEM;
1990
1991 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1992 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1993
1994 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1995 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001996 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001997 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001998 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001999 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002000 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002001 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002002
2003 rc = mwl8k_post_cmd(hw, &cmd->header);
2004
2005 if (!rc) {
2006 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2007 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002008 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002009 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002010 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002011 priv->ap_macids_supported = 0x00000000;
2012 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002013 }
2014
2015 kfree(cmd);
2016 return rc;
2017}
2018
2019/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002020 * CMD_GET_HW_SPEC (AP version).
2021 */
2022struct mwl8k_cmd_get_hw_spec_ap {
2023 struct mwl8k_cmd_pkt header;
2024 __u8 hw_rev;
2025 __u8 host_interface;
2026 __le16 num_wcb;
2027 __le16 num_mcaddrs;
2028 __u8 perm_addr[ETH_ALEN];
2029 __le16 region_code;
2030 __le16 num_antenna;
2031 __le32 fw_rev;
2032 __le32 wcbbase0;
2033 __le32 rxwrptr;
2034 __le32 rxrdptr;
2035 __le32 ps_cookie;
2036 __le32 wcbbase1;
2037 __le32 wcbbase2;
2038 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002039 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002040 __le32 caps;
2041 __le32 num_of_ampdu_queues;
2042 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002043} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002044
2045static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2046{
2047 struct mwl8k_priv *priv = hw->priv;
2048 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002049 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002050 u32 api_version;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002051
2052 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2053 if (cmd == NULL)
2054 return -ENOMEM;
2055
2056 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2057 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2058
2059 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2060 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2061
2062 rc = mwl8k_post_cmd(hw, &cmd->header);
2063
2064 if (!rc) {
2065 int off;
2066
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002067 api_version = le32_to_cpu(cmd->fw_api_version);
2068 if (priv->device_info->fw_api_ap != api_version) {
2069 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2070 " Expected %d got %d.\n", MWL8K_NAME,
2071 priv->device_info->part_name,
2072 priv->device_info->fw_api_ap,
2073 api_version);
2074 rc = -EINVAL;
2075 goto done;
2076 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002077 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2078 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2079 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2080 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002081 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002082 priv->ap_macids_supported = 0x000000ff;
2083 priv->sta_macids_supported = 0x00000000;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002084 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2085 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2086 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2087 " but we only support %d.\n",
2088 priv->num_ampdu_queues,
2089 MWL8K_MAX_AMPDU_QUEUES);
2090 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2091 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002092 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002093 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002094
2095 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002096 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002097
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002098 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2099 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2100 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2101 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002102
2103 for (i = 0; i < priv->num_ampdu_queues; i++)
2104 priv->txq_offset[i + MWL8K_TX_QUEUES] =
2105 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002106 }
2107
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002108done:
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002109 kfree(cmd);
2110 return rc;
2111}
2112
2113/*
2114 * CMD_SET_HW_SPEC.
2115 */
2116struct mwl8k_cmd_set_hw_spec {
2117 struct mwl8k_cmd_pkt header;
2118 __u8 hw_rev;
2119 __u8 host_interface;
2120 __le16 num_mcaddrs;
2121 __u8 perm_addr[ETH_ALEN];
2122 __le16 region_code;
2123 __le32 fw_rev;
2124 __le32 ps_cookie;
2125 __le32 caps;
2126 __le32 rx_queue_ptr;
2127 __le32 num_tx_queues;
2128 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
2129 __le32 flags;
2130 __le32 num_tx_desc_per_queue;
2131 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002132} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002133
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002134/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2135 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2136 * the packets that are queued for more than 500ms, will be dropped in the
2137 * hardware. This helps minimizing the issues caused due to head-of-line
2138 * blocking where a slow client can hog the bandwidth and affect traffic to a
2139 * faster client.
2140 */
2141#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002142#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2143#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2144#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002145
2146static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2147{
2148 struct mwl8k_priv *priv = hw->priv;
2149 struct mwl8k_cmd_set_hw_spec *cmd;
2150 int rc;
2151 int i;
2152
2153 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2154 if (cmd == NULL)
2155 return -ENOMEM;
2156
2157 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2158 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2159
2160 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2161 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2162 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002163
2164 /*
2165 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2166 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2167 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2168 * priority is interpreted the right way in firmware.
2169 */
2170 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
2171 int j = MWL8K_TX_QUEUES - 1 - i;
2172 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2173 }
2174
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002175 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2176 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2177 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002178 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2179 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2180
2181 rc = mwl8k_post_cmd(hw, &cmd->header);
2182 kfree(cmd);
2183
2184 return rc;
2185}
2186
2187/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002188 * CMD_MAC_MULTICAST_ADR.
2189 */
2190struct mwl8k_cmd_mac_multicast_adr {
2191 struct mwl8k_cmd_pkt header;
2192 __le16 action;
2193 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002194 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002195};
2196
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002197#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2198#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2199#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2200#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002201
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002202static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002203__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002204 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002205{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002206 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002207 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002208 int size;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002209 int mc_count = 0;
2210
2211 if (mc_list)
2212 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002213
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002214 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002215 allmulti = 1;
2216 mc_count = 0;
2217 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002218
2219 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2220
2221 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002222 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002223 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002224
2225 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2226 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002227 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2228 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002229
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002230 if (allmulti) {
2231 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2232 } else if (mc_count) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00002233 struct netdev_hw_addr *ha;
2234 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002235
2236 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2237 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad2010-04-01 21:22:57 +00002238 netdev_hw_addr_list_for_each(ha, mc_list) {
2239 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002240 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002241 }
2242
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002243 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002244}
2245
2246/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002247 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002248 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002249struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002250 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002251 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002252} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002253
2254#define MWL8K_STAT_ACK_FAILURE 9
2255#define MWL8K_STAT_RTS_FAILURE 12
2256#define MWL8K_STAT_FCS_ERROR 24
2257#define MWL8K_STAT_RTS_SUCCESS 11
2258
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002259static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2260 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002261{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002262 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002263 int rc;
2264
2265 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2266 if (cmd == NULL)
2267 return -ENOMEM;
2268
2269 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2270 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002271
2272 rc = mwl8k_post_cmd(hw, &cmd->header);
2273 if (!rc) {
2274 stats->dot11ACKFailureCount =
2275 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2276 stats->dot11RTSFailureCount =
2277 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2278 stats->dot11FCSErrorCount =
2279 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2280 stats->dot11RTSSuccessCount =
2281 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2282 }
2283 kfree(cmd);
2284
2285 return rc;
2286}
2287
2288/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002289 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002290 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002291struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002292 struct mwl8k_cmd_pkt header;
2293 __le16 action;
2294 __le16 control;
2295 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002296} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002297
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002298static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002299mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002300{
2301 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002302 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002303 int rc;
2304
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002305 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002306 return 0;
2307
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002308 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2309 if (cmd == NULL)
2310 return -ENOMEM;
2311
2312 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2313 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2314 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002315 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002316 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2317
2318 rc = mwl8k_post_cmd(hw, &cmd->header);
2319 kfree(cmd);
2320
2321 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002322 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002323
2324 return rc;
2325}
2326
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002327static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002328{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002329 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002330}
2331
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002332static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002333{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002334 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002335}
2336
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002337static int
2338mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2339{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002340 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002341
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002342 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002343
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002344 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002345}
2346
2347/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002348 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002349 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002350#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002351
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002352struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002353 struct mwl8k_cmd_pkt header;
2354 __le16 action;
2355 __le16 support_level;
2356 __le16 current_level;
2357 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002358 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002359} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002360
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002361static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002362{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002363 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002364 int rc;
2365
2366 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2367 if (cmd == NULL)
2368 return -ENOMEM;
2369
2370 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2371 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2372 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2373 cmd->support_level = cpu_to_le16(dBm);
2374
2375 rc = mwl8k_post_cmd(hw, &cmd->header);
2376 kfree(cmd);
2377
2378 return rc;
2379}
2380
2381/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002382 * CMD_TX_POWER.
2383 */
2384#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2385
2386struct mwl8k_cmd_tx_power {
2387 struct mwl8k_cmd_pkt header;
2388 __le16 action;
2389 __le16 band;
2390 __le16 channel;
2391 __le16 bw;
2392 __le16 sub_ch;
2393 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2394} __attribute__((packed));
2395
2396static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2397 struct ieee80211_conf *conf,
2398 unsigned short pwr)
2399{
2400 struct ieee80211_channel *channel = conf->channel;
2401 struct mwl8k_cmd_tx_power *cmd;
2402 int rc;
2403 int i;
2404
2405 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2406 if (cmd == NULL)
2407 return -ENOMEM;
2408
2409 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2410 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2411 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2412
2413 if (channel->band == IEEE80211_BAND_2GHZ)
2414 cmd->band = cpu_to_le16(0x1);
2415 else if (channel->band == IEEE80211_BAND_5GHZ)
2416 cmd->band = cpu_to_le16(0x4);
2417
2418 cmd->channel = channel->hw_value;
2419
2420 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2421 conf->channel_type == NL80211_CHAN_HT20) {
2422 cmd->bw = cpu_to_le16(0x2);
2423 } else {
2424 cmd->bw = cpu_to_le16(0x4);
2425 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2426 cmd->sub_ch = cpu_to_le16(0x3);
2427 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2428 cmd->sub_ch = cpu_to_le16(0x1);
2429 }
2430
2431 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2432 cmd->power_level_list[i] = cpu_to_le16(pwr);
2433
2434 rc = mwl8k_post_cmd(hw, &cmd->header);
2435 kfree(cmd);
2436
2437 return rc;
2438}
2439
2440/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002441 * CMD_RF_ANTENNA.
2442 */
2443struct mwl8k_cmd_rf_antenna {
2444 struct mwl8k_cmd_pkt header;
2445 __le16 antenna;
2446 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002447} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002448
2449#define MWL8K_RF_ANTENNA_RX 1
2450#define MWL8K_RF_ANTENNA_TX 2
2451
2452static int
2453mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2454{
2455 struct mwl8k_cmd_rf_antenna *cmd;
2456 int rc;
2457
2458 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2459 if (cmd == NULL)
2460 return -ENOMEM;
2461
2462 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2463 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2464 cmd->antenna = cpu_to_le16(antenna);
2465 cmd->mode = cpu_to_le16(mask);
2466
2467 rc = mwl8k_post_cmd(hw, &cmd->header);
2468 kfree(cmd);
2469
2470 return rc;
2471}
2472
2473/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002474 * CMD_SET_BEACON.
2475 */
2476struct mwl8k_cmd_set_beacon {
2477 struct mwl8k_cmd_pkt header;
2478 __le16 beacon_len;
2479 __u8 beacon[0];
2480};
2481
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002482static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2483 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002484{
2485 struct mwl8k_cmd_set_beacon *cmd;
2486 int rc;
2487
2488 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2489 if (cmd == NULL)
2490 return -ENOMEM;
2491
2492 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2493 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2494 cmd->beacon_len = cpu_to_le16(len);
2495 memcpy(cmd->beacon, beacon, len);
2496
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002497 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002498 kfree(cmd);
2499
2500 return rc;
2501}
2502
2503/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002504 * CMD_SET_PRE_SCAN.
2505 */
2506struct mwl8k_cmd_set_pre_scan {
2507 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002508} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002509
2510static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2511{
2512 struct mwl8k_cmd_set_pre_scan *cmd;
2513 int rc;
2514
2515 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2516 if (cmd == NULL)
2517 return -ENOMEM;
2518
2519 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2520 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2521
2522 rc = mwl8k_post_cmd(hw, &cmd->header);
2523 kfree(cmd);
2524
2525 return rc;
2526}
2527
2528/*
2529 * CMD_SET_POST_SCAN.
2530 */
2531struct mwl8k_cmd_set_post_scan {
2532 struct mwl8k_cmd_pkt header;
2533 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002534 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002535} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002536
2537static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002538mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002539{
2540 struct mwl8k_cmd_set_post_scan *cmd;
2541 int rc;
2542
2543 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2544 if (cmd == NULL)
2545 return -ENOMEM;
2546
2547 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2548 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2549 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002550 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002551
2552 rc = mwl8k_post_cmd(hw, &cmd->header);
2553 kfree(cmd);
2554
2555 return rc;
2556}
2557
2558/*
2559 * CMD_SET_RF_CHANNEL.
2560 */
2561struct mwl8k_cmd_set_rf_channel {
2562 struct mwl8k_cmd_pkt header;
2563 __le16 action;
2564 __u8 current_channel;
2565 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002566} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002567
2568static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002569 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002570{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002571 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002572 struct mwl8k_cmd_set_rf_channel *cmd;
2573 int rc;
2574
2575 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2576 if (cmd == NULL)
2577 return -ENOMEM;
2578
2579 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2580 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2581 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2582 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002583
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002584 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002585 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002586 else if (channel->band == IEEE80211_BAND_5GHZ)
2587 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002588
2589 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2590 conf->channel_type == NL80211_CHAN_HT20)
2591 cmd->channel_flags |= cpu_to_le32(0x00000080);
2592 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2593 cmd->channel_flags |= cpu_to_le32(0x000001900);
2594 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2595 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002596
2597 rc = mwl8k_post_cmd(hw, &cmd->header);
2598 kfree(cmd);
2599
2600 return rc;
2601}
2602
2603/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002604 * CMD_SET_AID.
2605 */
2606#define MWL8K_FRAME_PROT_DISABLED 0x00
2607#define MWL8K_FRAME_PROT_11G 0x07
2608#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2609#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2610
2611struct mwl8k_cmd_update_set_aid {
2612 struct mwl8k_cmd_pkt header;
2613 __le16 aid;
2614
2615 /* AP's MAC address (BSSID) */
2616 __u8 bssid[ETH_ALEN];
2617 __le16 protection_mode;
2618 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002619} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002620
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002621static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2622{
2623 int i;
2624 int j;
2625
2626 /*
2627 * Clear nonstandard rates 4 and 13.
2628 */
2629 mask &= 0x1fef;
2630
2631 for (i = 0, j = 0; i < 14; i++) {
2632 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002633 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002634 }
2635}
2636
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002637static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002638mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2639 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002640{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002641 struct mwl8k_cmd_update_set_aid *cmd;
2642 u16 prot_mode;
2643 int rc;
2644
2645 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2646 if (cmd == NULL)
2647 return -ENOMEM;
2648
2649 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2650 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002651 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002652 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002653
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002654 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002655 prot_mode = MWL8K_FRAME_PROT_11G;
2656 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002657 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002658 IEEE80211_HT_OP_MODE_PROTECTION) {
2659 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2660 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2661 break;
2662 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2663 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2664 break;
2665 default:
2666 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2667 break;
2668 }
2669 }
2670 cmd->protection_mode = cpu_to_le16(prot_mode);
2671
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002672 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002673
2674 rc = mwl8k_post_cmd(hw, &cmd->header);
2675 kfree(cmd);
2676
2677 return rc;
2678}
2679
2680/*
2681 * CMD_SET_RATE.
2682 */
2683struct mwl8k_cmd_set_rate {
2684 struct mwl8k_cmd_pkt header;
2685 __u8 legacy_rates[14];
2686
2687 /* Bitmap for supported MCS codes. */
2688 __u8 mcs_set[16];
2689 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002690} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002691
2692static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002693mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002694 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002695{
2696 struct mwl8k_cmd_set_rate *cmd;
2697 int rc;
2698
2699 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2700 if (cmd == NULL)
2701 return -ENOMEM;
2702
2703 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2704 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002705 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002706 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002707
2708 rc = mwl8k_post_cmd(hw, &cmd->header);
2709 kfree(cmd);
2710
2711 return rc;
2712}
2713
2714/*
2715 * CMD_FINALIZE_JOIN.
2716 */
2717#define MWL8K_FJ_BEACON_MAXLEN 128
2718
2719struct mwl8k_cmd_finalize_join {
2720 struct mwl8k_cmd_pkt header;
2721 __le32 sleep_interval; /* Number of beacon periods to sleep */
2722 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002723} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002724
2725static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2726 int framelen, int dtim)
2727{
2728 struct mwl8k_cmd_finalize_join *cmd;
2729 struct ieee80211_mgmt *payload = frame;
2730 int payload_len;
2731 int rc;
2732
2733 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2734 if (cmd == NULL)
2735 return -ENOMEM;
2736
2737 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2738 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2739 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2740
2741 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2742 if (payload_len < 0)
2743 payload_len = 0;
2744 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2745 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2746
2747 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2748
2749 rc = mwl8k_post_cmd(hw, &cmd->header);
2750 kfree(cmd);
2751
2752 return rc;
2753}
2754
2755/*
2756 * CMD_SET_RTS_THRESHOLD.
2757 */
2758struct mwl8k_cmd_set_rts_threshold {
2759 struct mwl8k_cmd_pkt header;
2760 __le16 action;
2761 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002762} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002763
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002764static int
2765mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002766{
2767 struct mwl8k_cmd_set_rts_threshold *cmd;
2768 int rc;
2769
2770 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2771 if (cmd == NULL)
2772 return -ENOMEM;
2773
2774 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2775 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002776 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2777 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002778
2779 rc = mwl8k_post_cmd(hw, &cmd->header);
2780 kfree(cmd);
2781
2782 return rc;
2783}
2784
2785/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002786 * CMD_SET_SLOT.
2787 */
2788struct mwl8k_cmd_set_slot {
2789 struct mwl8k_cmd_pkt header;
2790 __le16 action;
2791 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002792} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002793
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002794static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002795{
2796 struct mwl8k_cmd_set_slot *cmd;
2797 int rc;
2798
2799 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2800 if (cmd == NULL)
2801 return -ENOMEM;
2802
2803 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2804 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2805 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002806 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002807
2808 rc = mwl8k_post_cmd(hw, &cmd->header);
2809 kfree(cmd);
2810
2811 return rc;
2812}
2813
2814/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002815 * CMD_SET_EDCA_PARAMS.
2816 */
2817struct mwl8k_cmd_set_edca_params {
2818 struct mwl8k_cmd_pkt header;
2819
2820 /* See MWL8K_SET_EDCA_XXX below */
2821 __le16 action;
2822
2823 /* TX opportunity in units of 32 us */
2824 __le16 txop;
2825
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002826 union {
2827 struct {
2828 /* Log exponent of max contention period: 0...15 */
2829 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002830
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002831 /* Log exponent of min contention period: 0...15 */
2832 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002833
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002834 /* Adaptive interframe spacing in units of 32us */
2835 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002836
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002837 /* TX queue to configure */
2838 __u8 txq;
2839 } ap;
2840 struct {
2841 /* Log exponent of max contention period: 0...15 */
2842 __u8 log_cw_max;
2843
2844 /* Log exponent of min contention period: 0...15 */
2845 __u8 log_cw_min;
2846
2847 /* Adaptive interframe spacing in units of 32us */
2848 __u8 aifs;
2849
2850 /* TX queue to configure */
2851 __u8 txq;
2852 } sta;
2853 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002854} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002855
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002856#define MWL8K_SET_EDCA_CW 0x01
2857#define MWL8K_SET_EDCA_TXOP 0x02
2858#define MWL8K_SET_EDCA_AIFS 0x04
2859
2860#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2861 MWL8K_SET_EDCA_TXOP | \
2862 MWL8K_SET_EDCA_AIFS)
2863
2864static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002865mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2866 __u16 cw_min, __u16 cw_max,
2867 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002868{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002869 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002870 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002871 int rc;
2872
2873 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2874 if (cmd == NULL)
2875 return -ENOMEM;
2876
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002877 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2878 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002879 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2880 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002881 if (priv->ap_fw) {
2882 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2883 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2884 cmd->ap.aifs = aifs;
2885 cmd->ap.txq = qnum;
2886 } else {
2887 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2888 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2889 cmd->sta.aifs = aifs;
2890 cmd->sta.txq = qnum;
2891 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002892
2893 rc = mwl8k_post_cmd(hw, &cmd->header);
2894 kfree(cmd);
2895
2896 return rc;
2897}
2898
2899/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002900 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002901 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002902struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002903 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002904 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002905} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002906
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002907static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002908{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002909 struct mwl8k_priv *priv = hw->priv;
2910 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002911 int rc;
2912
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002913 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2914 if (cmd == NULL)
2915 return -ENOMEM;
2916
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002917 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002918 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002919 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002920
2921 rc = mwl8k_post_cmd(hw, &cmd->header);
2922 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002923
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002924 if (!rc)
2925 priv->wmm_enabled = enable;
2926
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002927 return rc;
2928}
2929
2930/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002931 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002932 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002933struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002934 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002935 __le32 action;
2936 __u8 rx_antenna_map;
2937 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002938} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002939
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002940static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002941{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002942 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002943 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002944
2945 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2946 if (cmd == NULL)
2947 return -ENOMEM;
2948
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002949 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002950 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002951 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2952 cmd->rx_antenna_map = rx;
2953 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002954
2955 rc = mwl8k_post_cmd(hw, &cmd->header);
2956 kfree(cmd);
2957
2958 return rc;
2959}
2960
2961/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002962 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002963 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002964struct mwl8k_cmd_use_fixed_rate_sta {
2965 struct mwl8k_cmd_pkt header;
2966 __le32 action;
2967 __le32 allow_rate_drop;
2968 __le32 num_rates;
2969 struct {
2970 __le32 is_ht_rate;
2971 __le32 enable_retry;
2972 __le32 rate;
2973 __le32 retry_count;
2974 } rate_entry[8];
2975 __le32 rate_type;
2976 __le32 reserved1;
2977 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002978} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002979
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002980#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002981#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002982
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002983static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002984{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002985 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002986 int rc;
2987
2988 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2989 if (cmd == NULL)
2990 return -ENOMEM;
2991
2992 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2993 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002994 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2995 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002996
2997 rc = mwl8k_post_cmd(hw, &cmd->header);
2998 kfree(cmd);
2999
3000 return rc;
3001}
3002
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003003/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003004 * CMD_USE_FIXED_RATE (AP version).
3005 */
3006struct mwl8k_cmd_use_fixed_rate_ap {
3007 struct mwl8k_cmd_pkt header;
3008 __le32 action;
3009 __le32 allow_rate_drop;
3010 __le32 num_rates;
3011 struct mwl8k_rate_entry_ap {
3012 __le32 is_ht_rate;
3013 __le32 enable_retry;
3014 __le32 rate;
3015 __le32 retry_count;
3016 } rate_entry[4];
3017 u8 multicast_rate;
3018 u8 multicast_rate_type;
3019 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003020} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003021
3022static int
3023mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3024{
3025 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3026 int rc;
3027
3028 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3029 if (cmd == NULL)
3030 return -ENOMEM;
3031
3032 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3033 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3034 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3035 cmd->multicast_rate = mcast;
3036 cmd->management_rate = mgmt;
3037
3038 rc = mwl8k_post_cmd(hw, &cmd->header);
3039 kfree(cmd);
3040
3041 return rc;
3042}
3043
3044/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003045 * CMD_ENABLE_SNIFFER.
3046 */
3047struct mwl8k_cmd_enable_sniffer {
3048 struct mwl8k_cmd_pkt header;
3049 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003050} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003051
3052static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3053{
3054 struct mwl8k_cmd_enable_sniffer *cmd;
3055 int rc;
3056
3057 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3058 if (cmd == NULL)
3059 return -ENOMEM;
3060
3061 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3062 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3063 cmd->action = cpu_to_le32(!!enable);
3064
3065 rc = mwl8k_post_cmd(hw, &cmd->header);
3066 kfree(cmd);
3067
3068 return rc;
3069}
3070
3071/*
3072 * CMD_SET_MAC_ADDR.
3073 */
3074struct mwl8k_cmd_set_mac_addr {
3075 struct mwl8k_cmd_pkt header;
3076 union {
3077 struct {
3078 __le16 mac_type;
3079 __u8 mac_addr[ETH_ALEN];
3080 } mbss;
3081 __u8 mac_addr[ETH_ALEN];
3082 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003083} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003084
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003085#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3086#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3087#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3088#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003089
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003090static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3091 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003092{
3093 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003094 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003095 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003096 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003097 int rc;
3098
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003099 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3100 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3101 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3102 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3103 else
3104 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3105 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3106 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3107 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3108 else
3109 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3110 }
3111
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003112 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3113 if (cmd == NULL)
3114 return -ENOMEM;
3115
3116 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3117 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3118 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003119 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003120 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3121 } else {
3122 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3123 }
3124
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003125 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003126 kfree(cmd);
3127
3128 return rc;
3129}
3130
3131/*
3132 * CMD_SET_RATEADAPT_MODE.
3133 */
3134struct mwl8k_cmd_set_rate_adapt_mode {
3135 struct mwl8k_cmd_pkt header;
3136 __le16 action;
3137 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003138} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003139
3140static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3141{
3142 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3143 int rc;
3144
3145 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3146 if (cmd == NULL)
3147 return -ENOMEM;
3148
3149 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3150 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3151 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3152 cmd->mode = cpu_to_le16(mode);
3153
3154 rc = mwl8k_post_cmd(hw, &cmd->header);
3155 kfree(cmd);
3156
3157 return rc;
3158}
3159
3160/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003161 * CMD_BSS_START.
3162 */
3163struct mwl8k_cmd_bss_start {
3164 struct mwl8k_cmd_pkt header;
3165 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003166} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003167
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003168static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3169 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003170{
3171 struct mwl8k_cmd_bss_start *cmd;
3172 int rc;
3173
3174 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3175 if (cmd == NULL)
3176 return -ENOMEM;
3177
3178 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3179 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3180 cmd->enable = cpu_to_le32(enable);
3181
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003182 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003183 kfree(cmd);
3184
3185 return rc;
3186}
3187
3188/*
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003189 * CMD_BASTREAM.
3190 */
3191
3192/*
3193 * UPSTREAM is tx direction
3194 */
3195#define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3196#define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3197
3198enum {
3199 MWL8K_BA_CREATE,
3200 MWL8K_BA_UPDATE,
3201 MWL8K_BA_DESTROY,
3202 MWL8K_BA_FLUSH,
3203 MWL8K_BA_CHECK,
3204} ba_stream_action_type;
3205
3206
3207struct mwl8k_create_ba_stream {
3208 __le32 flags;
3209 __le32 idle_thrs;
3210 __le32 bar_thrs;
3211 __le32 window_size;
3212 u8 peer_mac_addr[6];
3213 u8 dialog_token;
3214 u8 tid;
3215 u8 queue_id;
3216 u8 param_info;
3217 __le32 ba_context;
3218 u8 reset_seq_no_flag;
3219 __le16 curr_seq_no;
3220 u8 sta_src_mac_addr[6];
3221} __packed;
3222
3223struct mwl8k_destroy_ba_stream {
3224 __le32 flags;
3225 __le32 ba_context;
3226} __packed;
3227
3228struct mwl8k_cmd_bastream {
3229 struct mwl8k_cmd_pkt header;
3230 __le32 action;
3231 union {
3232 struct mwl8k_create_ba_stream create_params;
3233 struct mwl8k_destroy_ba_stream destroy_params;
3234 };
3235} __packed;
3236
3237static int
3238mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
3239{
3240 struct mwl8k_cmd_bastream *cmd;
3241 int rc;
3242
3243 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3244 if (cmd == NULL)
3245 return -ENOMEM;
3246
3247 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3248 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3249
3250 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3251
3252 cmd->create_params.queue_id = stream->idx;
3253 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3254 ETH_ALEN);
3255 cmd->create_params.tid = stream->tid;
3256
3257 cmd->create_params.flags =
3258 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3259 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3260
3261 rc = mwl8k_post_cmd(hw, &cmd->header);
3262
3263 kfree(cmd);
3264
3265 return rc;
3266}
3267
3268static int
3269mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3270 u8 buf_size)
3271{
3272 struct mwl8k_cmd_bastream *cmd;
3273 int rc;
3274
3275 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3276 if (cmd == NULL)
3277 return -ENOMEM;
3278
3279
3280 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3281 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3282
3283 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3284
3285 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3286 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3287 cmd->create_params.queue_id = stream->idx;
3288
3289 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3290 cmd->create_params.tid = stream->tid;
3291 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3292 cmd->create_params.reset_seq_no_flag = 1;
3293
3294 cmd->create_params.param_info =
3295 (stream->sta->ht_cap.ampdu_factor &
3296 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3297 ((stream->sta->ht_cap.ampdu_density << 2) &
3298 IEEE80211_HT_AMPDU_PARM_DENSITY);
3299
3300 cmd->create_params.flags =
3301 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3302 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3303
3304 rc = mwl8k_post_cmd(hw, &cmd->header);
3305
3306 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3307 stream->sta->addr, stream->tid);
3308 kfree(cmd);
3309
3310 return rc;
3311}
3312
3313static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3314 struct mwl8k_ampdu_stream *stream)
3315{
3316 struct mwl8k_cmd_bastream *cmd;
3317
3318 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3319 if (cmd == NULL)
3320 return;
3321
3322 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3323 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3324 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3325
3326 cmd->destroy_params.ba_context = cpu_to_le32(stream->idx);
3327 mwl8k_post_cmd(hw, &cmd->header);
3328
3329 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", stream->idx);
3330
3331 kfree(cmd);
3332}
3333
3334/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003335 * CMD_SET_NEW_STN.
3336 */
3337struct mwl8k_cmd_set_new_stn {
3338 struct mwl8k_cmd_pkt header;
3339 __le16 aid;
3340 __u8 mac_addr[6];
3341 __le16 stn_id;
3342 __le16 action;
3343 __le16 rsvd;
3344 __le32 legacy_rates;
3345 __u8 ht_rates[4];
3346 __le16 cap_info;
3347 __le16 ht_capabilities_info;
3348 __u8 mac_ht_param_info;
3349 __u8 rev;
3350 __u8 control_channel;
3351 __u8 add_channel;
3352 __le16 op_mode;
3353 __le16 stbc;
3354 __u8 add_qos_info;
3355 __u8 is_qos_sta;
3356 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003357} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003358
3359#define MWL8K_STA_ACTION_ADD 0
3360#define MWL8K_STA_ACTION_REMOVE 2
3361
3362static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3363 struct ieee80211_vif *vif,
3364 struct ieee80211_sta *sta)
3365{
3366 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003367 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003368 int rc;
3369
3370 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3371 if (cmd == NULL)
3372 return -ENOMEM;
3373
3374 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3375 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3376 cmd->aid = cpu_to_le16(sta->aid);
3377 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3378 cmd->stn_id = cpu_to_le16(sta->aid);
3379 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003380 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3381 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3382 else
3383 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3384 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003385 if (sta->ht_cap.ht_supported) {
3386 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3387 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3388 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3389 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3390 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3391 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3392 ((sta->ht_cap.ampdu_density & 7) << 2);
3393 cmd->is_qos_sta = 1;
3394 }
3395
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003396 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003397 kfree(cmd);
3398
3399 return rc;
3400}
3401
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003402static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3403 struct ieee80211_vif *vif)
3404{
3405 struct mwl8k_cmd_set_new_stn *cmd;
3406 int rc;
3407
3408 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3409 if (cmd == NULL)
3410 return -ENOMEM;
3411
3412 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3413 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3414 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3415
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003416 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003417 kfree(cmd);
3418
3419 return rc;
3420}
3421
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003422static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3423 struct ieee80211_vif *vif, u8 *addr)
3424{
3425 struct mwl8k_cmd_set_new_stn *cmd;
3426 int rc;
3427
3428 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3429 if (cmd == NULL)
3430 return -ENOMEM;
3431
3432 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3433 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3434 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3435 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3436
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003437 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003438 kfree(cmd);
3439
3440 return rc;
3441}
3442
3443/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003444 * CMD_UPDATE_ENCRYPTION.
3445 */
3446
3447#define MAX_ENCR_KEY_LENGTH 16
3448#define MIC_KEY_LENGTH 8
3449
3450struct mwl8k_cmd_update_encryption {
3451 struct mwl8k_cmd_pkt header;
3452
3453 __le32 action;
3454 __le32 reserved;
3455 __u8 mac_addr[6];
3456 __u8 encr_type;
3457
3458} __attribute__((packed));
3459
3460struct mwl8k_cmd_set_key {
3461 struct mwl8k_cmd_pkt header;
3462
3463 __le32 action;
3464 __le32 reserved;
3465 __le16 length;
3466 __le16 key_type_id;
3467 __le32 key_info;
3468 __le32 key_id;
3469 __le16 key_len;
3470 __u8 key_material[MAX_ENCR_KEY_LENGTH];
3471 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
3472 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
3473 __le16 tkip_rsc_low;
3474 __le32 tkip_rsc_high;
3475 __le16 tkip_tsc_low;
3476 __le32 tkip_tsc_high;
3477 __u8 mac_addr[6];
3478} __attribute__((packed));
3479
3480enum {
3481 MWL8K_ENCR_ENABLE,
3482 MWL8K_ENCR_SET_KEY,
3483 MWL8K_ENCR_REMOVE_KEY,
3484 MWL8K_ENCR_SET_GROUP_KEY,
3485};
3486
3487#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
3488#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
3489#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
3490#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
3491#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
3492
3493enum {
3494 MWL8K_ALG_WEP,
3495 MWL8K_ALG_TKIP,
3496 MWL8K_ALG_CCMP,
3497};
3498
3499#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
3500#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
3501#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
3502#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
3503#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
3504
3505static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
3506 struct ieee80211_vif *vif,
3507 u8 *addr,
3508 u8 encr_type)
3509{
3510 struct mwl8k_cmd_update_encryption *cmd;
3511 int rc;
3512
3513 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3514 if (cmd == NULL)
3515 return -ENOMEM;
3516
3517 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3518 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3519 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
3520 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3521 cmd->encr_type = encr_type;
3522
3523 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3524 kfree(cmd);
3525
3526 return rc;
3527}
3528
3529static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
3530 u8 *addr,
3531 struct ieee80211_key_conf *key)
3532{
3533 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3534 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3535 cmd->length = cpu_to_le16(sizeof(*cmd) -
3536 offsetof(struct mwl8k_cmd_set_key, length));
3537 cmd->key_id = cpu_to_le32(key->keyidx);
3538 cmd->key_len = cpu_to_le16(key->keylen);
3539 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3540
3541 switch (key->cipher) {
3542 case WLAN_CIPHER_SUITE_WEP40:
3543 case WLAN_CIPHER_SUITE_WEP104:
3544 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
3545 if (key->keyidx == 0)
3546 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
3547
3548 break;
3549 case WLAN_CIPHER_SUITE_TKIP:
3550 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
3551 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3552 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3553 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3554 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
3555 | MWL8K_KEY_FLAG_TSC_VALID);
3556 break;
3557 case WLAN_CIPHER_SUITE_CCMP:
3558 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
3559 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3560 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3561 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3562 break;
3563 default:
3564 return -ENOTSUPP;
3565 }
3566
3567 return 0;
3568}
3569
3570static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
3571 struct ieee80211_vif *vif,
3572 u8 *addr,
3573 struct ieee80211_key_conf *key)
3574{
3575 struct mwl8k_cmd_set_key *cmd;
3576 int rc;
3577 int keymlen;
3578 u32 action;
3579 u8 idx;
3580 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3581
3582 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3583 if (cmd == NULL)
3584 return -ENOMEM;
3585
3586 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3587 if (rc < 0)
3588 goto done;
3589
3590 idx = key->keyidx;
3591
3592 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3593 action = MWL8K_ENCR_SET_KEY;
3594 else
3595 action = MWL8K_ENCR_SET_GROUP_KEY;
3596
3597 switch (key->cipher) {
3598 case WLAN_CIPHER_SUITE_WEP40:
3599 case WLAN_CIPHER_SUITE_WEP104:
3600 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
3601 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
3602 sizeof(*key) + key->keylen);
3603 mwl8k_vif->wep_key_conf[idx].enabled = 1;
3604 }
3605
3606 keymlen = 0;
3607 action = MWL8K_ENCR_SET_KEY;
3608 break;
3609 case WLAN_CIPHER_SUITE_TKIP:
3610 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
3611 break;
3612 case WLAN_CIPHER_SUITE_CCMP:
3613 keymlen = key->keylen;
3614 break;
3615 default:
3616 rc = -ENOTSUPP;
3617 goto done;
3618 }
3619
3620 memcpy(cmd->key_material, key->key, keymlen);
3621 cmd->action = cpu_to_le32(action);
3622
3623 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3624done:
3625 kfree(cmd);
3626
3627 return rc;
3628}
3629
3630static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
3631 struct ieee80211_vif *vif,
3632 u8 *addr,
3633 struct ieee80211_key_conf *key)
3634{
3635 struct mwl8k_cmd_set_key *cmd;
3636 int rc;
3637 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3638
3639 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3640 if (cmd == NULL)
3641 return -ENOMEM;
3642
3643 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3644 if (rc < 0)
3645 goto done;
3646
3647 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3648 WLAN_CIPHER_SUITE_WEP104)
3649 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
3650
3651 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
3652
3653 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3654done:
3655 kfree(cmd);
3656
3657 return rc;
3658}
3659
3660static int mwl8k_set_key(struct ieee80211_hw *hw,
3661 enum set_key_cmd cmd_param,
3662 struct ieee80211_vif *vif,
3663 struct ieee80211_sta *sta,
3664 struct ieee80211_key_conf *key)
3665{
3666 int rc = 0;
3667 u8 encr_type;
3668 u8 *addr;
3669 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3670
3671 if (vif->type == NL80211_IFTYPE_STATION)
3672 return -EOPNOTSUPP;
3673
3674 if (sta == NULL)
3675 addr = hw->wiphy->perm_addr;
3676 else
3677 addr = sta->addr;
3678
3679 if (cmd_param == SET_KEY) {
3680 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3681 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
3682 if (rc)
3683 goto out;
3684
3685 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
3686 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
3687 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
3688 else
3689 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
3690
3691 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
3692 encr_type);
3693 if (rc)
3694 goto out;
3695
3696 mwl8k_vif->is_hw_crypto_enabled = true;
3697
3698 } else {
3699 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
3700
3701 if (rc)
3702 goto out;
3703
3704 mwl8k_vif->is_hw_crypto_enabled = false;
3705
3706 }
3707out:
3708 return rc;
3709}
3710
3711/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003712 * CMD_UPDATE_STADB.
3713 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003714struct ewc_ht_info {
3715 __le16 control1;
3716 __le16 control2;
3717 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003718} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003719
3720struct peer_capability_info {
3721 /* Peer type - AP vs. STA. */
3722 __u8 peer_type;
3723
3724 /* Basic 802.11 capabilities from assoc resp. */
3725 __le16 basic_caps;
3726
3727 /* Set if peer supports 802.11n high throughput (HT). */
3728 __u8 ht_support;
3729
3730 /* Valid if HT is supported. */
3731 __le16 ht_caps;
3732 __u8 extended_ht_caps;
3733 struct ewc_ht_info ewc_info;
3734
3735 /* Legacy rate table. Intersection of our rates and peer rates. */
3736 __u8 legacy_rates[12];
3737
3738 /* HT rate table. Intersection of our rates and peer rates. */
3739 __u8 ht_rates[16];
3740 __u8 pad[16];
3741
3742 /* If set, interoperability mode, no proprietary extensions. */
3743 __u8 interop;
3744 __u8 pad2;
3745 __u8 station_id;
3746 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003747} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003748
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003749struct mwl8k_cmd_update_stadb {
3750 struct mwl8k_cmd_pkt header;
3751
3752 /* See STADB_ACTION_TYPE */
3753 __le32 action;
3754
3755 /* Peer MAC address */
3756 __u8 peer_addr[ETH_ALEN];
3757
3758 __le32 reserved;
3759
3760 /* Peer info - valid during add/update. */
3761 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003762} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003763
Lennert Buytenheka6804002010-01-04 21:55:42 +01003764#define MWL8K_STA_DB_MODIFY_ENTRY 1
3765#define MWL8K_STA_DB_DEL_ENTRY 2
3766
3767/* Peer Entry flags - used to define the type of the peer node */
3768#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3769
3770static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003771 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003772 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003773{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003774 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003775 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003776 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003777 int rc;
3778
3779 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3780 if (cmd == NULL)
3781 return -ENOMEM;
3782
3783 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3784 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003785 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003786 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003787
Lennert Buytenheka6804002010-01-04 21:55:42 +01003788 p = &cmd->peer_info;
3789 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3790 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003791 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003792 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003793 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3794 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003795 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3796 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3797 else
3798 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3799 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003800 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003801 p->interop = 1;
3802 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003803
Lennert Buytenheka6804002010-01-04 21:55:42 +01003804 rc = mwl8k_post_cmd(hw, &cmd->header);
3805 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003806
Lennert Buytenheka6804002010-01-04 21:55:42 +01003807 return rc ? rc : p->station_id;
3808}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003809
Lennert Buytenheka6804002010-01-04 21:55:42 +01003810static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3811 struct ieee80211_vif *vif, u8 *addr)
3812{
3813 struct mwl8k_cmd_update_stadb *cmd;
3814 int rc;
3815
3816 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3817 if (cmd == NULL)
3818 return -ENOMEM;
3819
3820 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3821 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3822 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3823 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3824
3825 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003826 kfree(cmd);
3827
3828 return rc;
3829}
3830
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003831
3832/*
3833 * Interrupt handling.
3834 */
3835static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3836{
3837 struct ieee80211_hw *hw = dev_id;
3838 struct mwl8k_priv *priv = hw->priv;
3839 u32 status;
3840
3841 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003842 if (!status)
3843 return IRQ_NONE;
3844
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003845 if (status & MWL8K_A2H_INT_TX_DONE) {
3846 status &= ~MWL8K_A2H_INT_TX_DONE;
3847 tasklet_schedule(&priv->poll_tx_task);
3848 }
3849
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003850 if (status & MWL8K_A2H_INT_RX_READY) {
3851 status &= ~MWL8K_A2H_INT_RX_READY;
3852 tasklet_schedule(&priv->poll_rx_task);
3853 }
3854
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003855 if (status)
3856 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003857
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003858 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003859 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003860 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003861 }
3862
3863 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003864 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003865 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003866 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003867 }
3868
3869 return IRQ_HANDLED;
3870}
3871
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003872static void mwl8k_tx_poll(unsigned long data)
3873{
3874 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3875 struct mwl8k_priv *priv = hw->priv;
3876 int limit;
3877 int i;
3878
3879 limit = 32;
3880
3881 spin_lock_bh(&priv->tx_lock);
3882
3883 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3884 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3885
3886 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3887 complete(priv->tx_wait);
3888 priv->tx_wait = NULL;
3889 }
3890
3891 spin_unlock_bh(&priv->tx_lock);
3892
3893 if (limit) {
3894 writel(~MWL8K_A2H_INT_TX_DONE,
3895 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3896 } else {
3897 tasklet_schedule(&priv->poll_tx_task);
3898 }
3899}
3900
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003901static void mwl8k_rx_poll(unsigned long data)
3902{
3903 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3904 struct mwl8k_priv *priv = hw->priv;
3905 int limit;
3906
3907 limit = 32;
3908 limit -= rxq_process(hw, 0, limit);
3909 limit -= rxq_refill(hw, 0, limit);
3910
3911 if (limit) {
3912 writel(~MWL8K_A2H_INT_RX_READY,
3913 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3914 } else {
3915 tasklet_schedule(&priv->poll_rx_task);
3916 }
3917}
3918
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003919
3920/*
3921 * Core driver operations.
3922 */
Johannes Berg7bb45682011-02-24 14:42:06 +01003923static void mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003924{
3925 struct mwl8k_priv *priv = hw->priv;
3926 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003927
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003928 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003929 wiphy_debug(hw->wiphy,
3930 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003931 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003932 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003933 }
3934
Johannes Berg7bb45682011-02-24 14:42:06 +01003935 mwl8k_txq_xmit(hw, index, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003936}
3937
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003938static int mwl8k_start(struct ieee80211_hw *hw)
3939{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003940 struct mwl8k_priv *priv = hw->priv;
3941 int rc;
3942
Joe Perchesa0607fd2009-11-18 23:29:17 -08003943 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003944 IRQF_SHARED, MWL8K_NAME, hw);
3945 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07003946 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003947 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003948 }
3949
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003950 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003951 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003952 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003953
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003954 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003955 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003956
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003957 rc = mwl8k_fw_lock(hw);
3958 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003959 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003960
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003961 if (!priv->ap_fw) {
3962 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003963 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003964
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003965 if (!rc)
3966 rc = mwl8k_cmd_set_pre_scan(hw);
3967
3968 if (!rc)
3969 rc = mwl8k_cmd_set_post_scan(hw,
3970 "\x00\x00\x00\x00\x00\x00");
3971 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003972
3973 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003974 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003975
3976 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003977 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003978
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003979 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003980 }
3981
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003982 if (rc) {
3983 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3984 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003985 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003986 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003987 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003988
3989 return rc;
3990}
3991
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003992static void mwl8k_stop(struct ieee80211_hw *hw)
3993{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003994 struct mwl8k_priv *priv = hw->priv;
3995 int i;
3996
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003997 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003998
3999 ieee80211_stop_queues(hw);
4000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004001 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004002 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004003 free_irq(priv->pdev->irq, hw);
4004
4005 /* Stop finalize join worker */
4006 cancel_work_sync(&priv->finalize_join_worker);
4007 if (priv->beacon_skb != NULL)
4008 dev_kfree_skb(priv->beacon_skb);
4009
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004010 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004011 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004012 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004013
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004014 /* Return all skbs to mac80211 */
4015 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004016 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004017}
4018
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004019static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4020
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004021static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004022 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004023{
4024 struct mwl8k_priv *priv = hw->priv;
4025 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004026 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004027 int macid, rc;
4028 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004029
4030 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004031 * Reject interface creation if sniffer mode is active, as
4032 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004033 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004034 */
4035 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004036 wiphy_info(hw->wiphy,
4037 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004038 return -EINVAL;
4039 }
4040
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004041 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004042 switch (vif->type) {
4043 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004044 if (!priv->ap_fw && di->fw_image_ap) {
4045 /* we must load the ap fw to meet this request */
4046 if (!list_empty(&priv->vif_list))
4047 return -EBUSY;
4048 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4049 if (rc)
4050 return rc;
4051 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004052 macids_supported = priv->ap_macids_supported;
4053 break;
4054 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004055 if (priv->ap_fw && di->fw_image_sta) {
4056 /* we must load the sta fw to meet this request */
4057 if (!list_empty(&priv->vif_list))
4058 return -EBUSY;
4059 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4060 if (rc)
4061 return rc;
4062 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004063 macids_supported = priv->sta_macids_supported;
4064 break;
4065 default:
4066 return -EINVAL;
4067 }
4068
4069 macid = ffs(macids_supported & ~priv->macids_used);
4070 if (!macid--)
4071 return -EBUSY;
4072
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004073 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01004074 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004075 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004076 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004077 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004078 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004079 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4080 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004081
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004082 /* Set the mac address. */
4083 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4084
4085 if (priv->ap_fw)
4086 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4087
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004088 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004089 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004090
4091 return 0;
4092}
4093
4094static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004095 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004096{
4097 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004098 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004099
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004100 if (priv->ap_fw)
4101 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4102
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004103 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004104
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004105 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004106 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004107}
4108
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004109static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004110{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004111 struct ieee80211_conf *conf = &hw->conf;
4112 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004113 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004114
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004115 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004116 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004117 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004118 }
4119
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004120 rc = mwl8k_fw_lock(hw);
4121 if (rc)
4122 return rc;
4123
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004124 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004125 if (rc)
4126 goto out;
4127
Lennert Buytenhek610677d2010-01-04 21:56:46 +01004128 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004129 if (rc)
4130 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004131
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004132 if (conf->power_level > 18)
4133 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004134
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004135 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004136 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4137 if (rc)
4138 goto out;
4139
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08004140 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
4141 if (rc)
4142 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
4143 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
4144 if (rc)
4145 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
4146
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004147 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004148 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4149 if (rc)
4150 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004151 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4152 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004153
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004154out:
4155 mwl8k_fw_unlock(hw);
4156
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004157 return rc;
4158}
4159
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004160static void
4161mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4162 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004163{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004164 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004165 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004166 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004167 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004168
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004169 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004170 return;
4171
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004172 /*
4173 * No need to capture a beacon if we're no longer associated.
4174 */
4175 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4176 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004177
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004178 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004179 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004180 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004181 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004182 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004183
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004184 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004185
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004186 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004187 if (ap == NULL) {
4188 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004189 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004190 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004191
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004192 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4193 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4194 } else {
4195 ap_legacy_rates =
4196 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4197 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004198 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004199
4200 rcu_read_unlock();
4201 }
4202
4203 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004204 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004205 if (rc)
4206 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004207
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004208 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004209 if (rc)
4210 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004211 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004212
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004213 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004214 rc = mwl8k_set_radio_preamble(hw,
4215 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004216 if (rc)
4217 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004218 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004219
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004220 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004221 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004222 if (rc)
4223 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004224 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004225
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004226 if (vif->bss_conf.assoc &&
4227 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4228 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004229 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004230 if (rc)
4231 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004232 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004233
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004234 if (vif->bss_conf.assoc &&
4235 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004236 /*
4237 * Finalize the join. Tell rx handler to process
4238 * next beacon from our BSSID.
4239 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004240 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004241 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004242 }
4243
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004244out:
4245 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004246}
4247
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004248static void
4249mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4250 struct ieee80211_bss_conf *info, u32 changed)
4251{
4252 int rc;
4253
4254 if (mwl8k_fw_lock(hw))
4255 return;
4256
4257 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4258 rc = mwl8k_set_radio_preamble(hw,
4259 vif->bss_conf.use_short_preamble);
4260 if (rc)
4261 goto out;
4262 }
4263
4264 if (changed & BSS_CHANGED_BASIC_RATES) {
4265 int idx;
4266 int rate;
4267
4268 /*
4269 * Use lowest supported basic rate for multicasts
4270 * and management frames (such as probe responses --
4271 * beacons will always go out at 1 Mb/s).
4272 */
4273 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004274 if (idx)
4275 idx--;
4276
4277 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4278 rate = mwl8k_rates_24[idx].hw_value;
4279 else
4280 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004281
4282 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4283 }
4284
4285 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4286 struct sk_buff *skb;
4287
4288 skb = ieee80211_beacon_get(hw, vif);
4289 if (skb != NULL) {
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004290 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004291 kfree_skb(skb);
4292 }
4293 }
4294
4295 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004296 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004297
4298out:
4299 mwl8k_fw_unlock(hw);
4300}
4301
4302static void
4303mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4304 struct ieee80211_bss_conf *info, u32 changed)
4305{
4306 struct mwl8k_priv *priv = hw->priv;
4307
4308 if (!priv->ap_fw)
4309 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
4310 else
4311 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
4312}
4313
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004314static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00004315 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004316{
4317 struct mwl8k_cmd_pkt *cmd;
4318
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004319 /*
4320 * Synthesize and return a command packet that programs the
4321 * hardware multicast address filter. At this point we don't
4322 * know whether FIF_ALLMULTI is being requested, but if it is,
4323 * we'll end up throwing this packet away and creating a new
4324 * one in mwl8k_configure_filter().
4325 */
Jiri Pirko22bedad2010-04-01 21:22:57 +00004326 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004327
4328 return (unsigned long)cmd;
4329}
4330
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004331static int
4332mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
4333 unsigned int changed_flags,
4334 unsigned int *total_flags)
4335{
4336 struct mwl8k_priv *priv = hw->priv;
4337
4338 /*
4339 * Hardware sniffer mode is mutually exclusive with STA
4340 * operation, so refuse to enable sniffer mode if a STA
4341 * interface is active.
4342 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004343 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004344 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07004345 wiphy_info(hw->wiphy,
4346 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004347 return 0;
4348 }
4349
4350 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004351 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004352 return 0;
4353 priv->sniffer_enabled = true;
4354 }
4355
4356 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
4357 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
4358 FIF_OTHER_BSS;
4359
4360 return 1;
4361}
4362
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004363static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
4364{
4365 if (!list_empty(&priv->vif_list))
4366 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
4367
4368 return NULL;
4369}
4370
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004371static void mwl8k_configure_filter(struct ieee80211_hw *hw,
4372 unsigned int changed_flags,
4373 unsigned int *total_flags,
4374 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004375{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004376 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004377 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
4378
4379 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02004380 * AP firmware doesn't allow fine-grained control over
4381 * the receive filter.
4382 */
4383 if (priv->ap_fw) {
4384 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
4385 kfree(cmd);
4386 return;
4387 }
4388
4389 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004390 * Enable hardware sniffer mode if FIF_CONTROL or
4391 * FIF_OTHER_BSS is requested.
4392 */
4393 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
4394 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
4395 kfree(cmd);
4396 return;
4397 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004398
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004399 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004400 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004401
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004402 if (mwl8k_fw_lock(hw)) {
4403 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004404 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004405 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004406
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004407 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004408 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004409 priv->sniffer_enabled = false;
4410 }
4411
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004412 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004413 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
4414 /*
4415 * Disable the BSS filter.
4416 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004417 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004418 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004419 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004420 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004421
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004422 /*
4423 * Enable the BSS filter.
4424 *
4425 * If there is an active STA interface, use that
4426 * interface's BSSID, otherwise use a dummy one
4427 * (where the OUI part needs to be nonzero for
4428 * the BSSID to be accepted by POST_SCAN).
4429 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004430 mwl8k_vif = mwl8k_first_vif(priv);
4431 if (mwl8k_vif != NULL)
4432 bssid = mwl8k_vif->vif->bss_conf.bssid;
4433 else
4434 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004435
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004436 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004437 }
4438 }
4439
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004440 /*
4441 * If FIF_ALLMULTI is being requested, throw away the command
4442 * packet that ->prepare_multicast() built and replace it with
4443 * a command packet that enables reception of all multicast
4444 * packets.
4445 */
4446 if (*total_flags & FIF_ALLMULTI) {
4447 kfree(cmd);
Jiri Pirko22bedad2010-04-01 21:22:57 +00004448 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004449 }
4450
4451 if (cmd != NULL) {
4452 mwl8k_post_cmd(hw, cmd);
4453 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004454 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004455
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004456 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004457}
4458
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004459static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4460{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004461 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004462}
4463
Johannes Berg4a6967b2010-02-19 19:18:37 +01004464static int mwl8k_sta_remove(struct ieee80211_hw *hw,
4465 struct ieee80211_vif *vif,
4466 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004467{
4468 struct mwl8k_priv *priv = hw->priv;
4469
Johannes Berg4a6967b2010-02-19 19:18:37 +01004470 if (priv->ap_fw)
4471 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
4472 else
4473 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
4474}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004475
Johannes Berg4a6967b2010-02-19 19:18:37 +01004476static int mwl8k_sta_add(struct ieee80211_hw *hw,
4477 struct ieee80211_vif *vif,
4478 struct ieee80211_sta *sta)
4479{
4480 struct mwl8k_priv *priv = hw->priv;
4481 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004482 int i;
4483 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4484 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004485
Johannes Berg4a6967b2010-02-19 19:18:37 +01004486 if (!priv->ap_fw) {
4487 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
4488 if (ret >= 0) {
4489 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004490 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004491 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01004492
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004493 } else {
4494 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004495 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004496
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004497 for (i = 0; i < NUM_WEP_KEYS; i++) {
4498 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
4499 if (mwl8k_vif->wep_key_conf[i].enabled)
4500 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
4501 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004502 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004503}
4504
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004505static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
4506 const struct ieee80211_tx_queue_params *params)
4507{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004508 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004509 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004510
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004511 rc = mwl8k_fw_lock(hw);
4512 if (!rc) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004513 BUG_ON(queue > MWL8K_TX_QUEUES - 1);
4514 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
4515
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004516 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004517 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004518
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004519 if (!rc) {
4520 int q = MWL8K_TX_QUEUES - 1 - queue;
4521 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004522 params->cw_min,
4523 params->cw_max,
4524 params->aifs,
4525 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004526 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004527
4528 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004529 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004530
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004531 return rc;
4532}
4533
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004534static int mwl8k_get_stats(struct ieee80211_hw *hw,
4535 struct ieee80211_low_level_stats *stats)
4536{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004537 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004538}
4539
John W. Linville0d462bb2010-07-28 14:04:24 -04004540static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
4541 struct survey_info *survey)
4542{
4543 struct mwl8k_priv *priv = hw->priv;
4544 struct ieee80211_conf *conf = &hw->conf;
4545
4546 if (idx != 0)
4547 return -ENOENT;
4548
4549 survey->channel = conf->channel;
4550 survey->filled = SURVEY_INFO_NOISE_DBM;
4551 survey->noise = priv->noise;
4552
4553 return 0;
4554}
4555
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004556static int
4557mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4558 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01004559 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4560 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004561{
4562 switch (action) {
4563 case IEEE80211_AMPDU_RX_START:
4564 case IEEE80211_AMPDU_RX_STOP:
4565 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
4566 return -ENOTSUPP;
4567 return 0;
4568 default:
4569 return -ENOTSUPP;
4570 }
4571}
4572
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004573static const struct ieee80211_ops mwl8k_ops = {
4574 .tx = mwl8k_tx,
4575 .start = mwl8k_start,
4576 .stop = mwl8k_stop,
4577 .add_interface = mwl8k_add_interface,
4578 .remove_interface = mwl8k_remove_interface,
4579 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004580 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004581 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004582 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004583 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004584 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01004585 .sta_add = mwl8k_sta_add,
4586 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004587 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004588 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04004589 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004590 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004591};
4592
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004593static void mwl8k_finalize_join_worker(struct work_struct *work)
4594{
4595 struct mwl8k_priv *priv =
4596 container_of(work, struct mwl8k_priv, finalize_join_worker);
4597 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01004598 struct ieee80211_mgmt *mgmt = (void *)skb->data;
4599 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
4600 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
4601 mgmt->u.beacon.variable, len);
4602 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004603
Johannes Berg56007a02010-01-26 14:19:52 +01004604 if (tim && tim[1] >= 2)
4605 dtim_period = tim[3];
4606
4607 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004608
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004609 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004610 priv->beacon_skb = NULL;
4611}
4612
John W. Linvillebcb628d2009-11-06 16:40:16 -05004613enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004614 MWL8363 = 0,
4615 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004616 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02004617};
4618
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07004619#define MWL8K_8366_AP_FW_API 2
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004620#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
4621#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
4622
John W. Linvillebcb628d2009-11-06 16:40:16 -05004623static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004624 [MWL8363] = {
4625 .part_name = "88w8363",
4626 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004627 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004628 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004629 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004630 .part_name = "88w8687",
4631 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004632 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05004633 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004634 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004635 .part_name = "88w8366",
4636 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004637 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004638 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
4639 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004640 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004641 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004642};
4643
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004644MODULE_FIRMWARE("mwl8k/helper_8363.fw");
4645MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
4646MODULE_FIRMWARE("mwl8k/helper_8687.fw");
4647MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
4648MODULE_FIRMWARE("mwl8k/helper_8366.fw");
4649MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004650MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004651
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004652static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01004653 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004654 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
4655 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004656 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
4657 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
4658 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01004659 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004660 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004661};
4662MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
4663
Brian Cavagnolo99020472010-11-12 17:23:52 -08004664static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
4665{
4666 int rc;
4667 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
4668 "Trying alternative firmware %s\n", pci_name(priv->pdev),
4669 priv->fw_pref, priv->fw_alt);
4670 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
4671 if (rc) {
4672 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4673 pci_name(priv->pdev), priv->fw_alt);
4674 return rc;
4675 }
4676 return 0;
4677}
4678
4679static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
4680static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
4681{
4682 struct mwl8k_priv *priv = context;
4683 struct mwl8k_device_info *di = priv->device_info;
4684 int rc;
4685
4686 switch (priv->fw_state) {
4687 case FW_STATE_INIT:
4688 if (!fw) {
4689 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
4690 pci_name(priv->pdev), di->helper_image);
4691 goto fail;
4692 }
4693 priv->fw_helper = fw;
4694 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
4695 true);
4696 if (rc && priv->fw_alt) {
4697 rc = mwl8k_request_alt_fw(priv);
4698 if (rc)
4699 goto fail;
4700 priv->fw_state = FW_STATE_LOADING_ALT;
4701 } else if (rc)
4702 goto fail;
4703 else
4704 priv->fw_state = FW_STATE_LOADING_PREF;
4705 break;
4706
4707 case FW_STATE_LOADING_PREF:
4708 if (!fw) {
4709 if (priv->fw_alt) {
4710 rc = mwl8k_request_alt_fw(priv);
4711 if (rc)
4712 goto fail;
4713 priv->fw_state = FW_STATE_LOADING_ALT;
4714 } else
4715 goto fail;
4716 } else {
4717 priv->fw_ucode = fw;
4718 rc = mwl8k_firmware_load_success(priv);
4719 if (rc)
4720 goto fail;
4721 else
4722 complete(&priv->firmware_loading_complete);
4723 }
4724 break;
4725
4726 case FW_STATE_LOADING_ALT:
4727 if (!fw) {
4728 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4729 pci_name(priv->pdev), di->helper_image);
4730 goto fail;
4731 }
4732 priv->fw_ucode = fw;
4733 rc = mwl8k_firmware_load_success(priv);
4734 if (rc)
4735 goto fail;
4736 else
4737 complete(&priv->firmware_loading_complete);
4738 break;
4739
4740 default:
4741 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
4742 MWL8K_NAME, priv->fw_state);
4743 BUG_ON(1);
4744 }
4745
4746 return;
4747
4748fail:
4749 priv->fw_state = FW_STATE_ERROR;
4750 complete(&priv->firmware_loading_complete);
4751 device_release_driver(&priv->pdev->dev);
4752 mwl8k_release_firmware(priv);
4753}
4754
4755static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
4756 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004757{
4758 struct mwl8k_priv *priv = hw->priv;
4759 int rc;
4760
4761 /* Reset firmware and hardware */
4762 mwl8k_hw_reset(priv);
4763
4764 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004765 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004766 if (rc) {
4767 wiphy_err(hw->wiphy, "Firmware files not found\n");
4768 return rc;
4769 }
4770
Brian Cavagnolo99020472010-11-12 17:23:52 -08004771 if (nowait)
4772 return rc;
4773
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004774 /* Load firmware into hardware */
4775 rc = mwl8k_load_firmware(hw);
4776 if (rc)
4777 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4778
4779 /* Reclaim memory once firmware is successfully loaded */
4780 mwl8k_release_firmware(priv);
4781
4782 return rc;
4783}
4784
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004785static int mwl8k_init_txqs(struct ieee80211_hw *hw)
4786{
4787 struct mwl8k_priv *priv = hw->priv;
4788 int rc = 0;
4789 int i;
4790
4791 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4792 rc = mwl8k_txq_init(hw, i);
4793 if (rc)
4794 break;
4795 if (priv->ap_fw)
4796 iowrite32(priv->txq[i].txd_dma,
4797 priv->sram + priv->txq_offset[i]);
4798 }
4799 return rc;
4800}
4801
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004802/* initialize hw after successfully loading a firmware image */
4803static int mwl8k_probe_hw(struct ieee80211_hw *hw)
4804{
4805 struct mwl8k_priv *priv = hw->priv;
4806 int rc = 0;
4807 int i;
4808
4809 if (priv->ap_fw) {
4810 priv->rxd_ops = priv->device_info->ap_rxd_ops;
4811 if (priv->rxd_ops == NULL) {
4812 wiphy_err(hw->wiphy,
4813 "Driver does not have AP firmware image support for this hardware\n");
4814 goto err_stop_firmware;
4815 }
4816 } else {
4817 priv->rxd_ops = &rxd_sta_ops;
4818 }
4819
4820 priv->sniffer_enabled = false;
4821 priv->wmm_enabled = false;
4822 priv->pending_tx_pkts = 0;
4823
4824 rc = mwl8k_rxq_init(hw, 0);
4825 if (rc)
4826 goto err_stop_firmware;
4827 rxq_refill(hw, 0, INT_MAX);
4828
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004829 /* For the sta firmware, we need to know the dma addresses of tx queues
4830 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
4831 * prior to issuing this command. But for the AP case, we learn the
4832 * total number of queues from the result CMD_GET_HW_SPEC, so for this
4833 * case we must initialize the tx queues after.
4834 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07004835 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004836 if (!priv->ap_fw) {
4837 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004838 if (rc)
4839 goto err_free_queues;
4840 }
4841
4842 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4843 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4844 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
4845 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
4846 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4847
4848 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4849 IRQF_SHARED, MWL8K_NAME, hw);
4850 if (rc) {
4851 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4852 goto err_free_queues;
4853 }
4854
4855 /*
4856 * Temporarily enable interrupts. Initial firmware host
4857 * commands use interrupts and avoid polling. Disable
4858 * interrupts when done.
4859 */
4860 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4861
4862 /* Get config data, mac addrs etc */
4863 if (priv->ap_fw) {
4864 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4865 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004866 rc = mwl8k_init_txqs(hw);
4867 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004868 rc = mwl8k_cmd_set_hw_spec(hw);
4869 } else {
4870 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4871 }
4872 if (rc) {
4873 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
4874 goto err_free_irq;
4875 }
4876
4877 /* Turn radio off */
4878 rc = mwl8k_cmd_radio_disable(hw);
4879 if (rc) {
4880 wiphy_err(hw->wiphy, "Cannot disable\n");
4881 goto err_free_irq;
4882 }
4883
4884 /* Clear MAC address */
4885 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
4886 if (rc) {
4887 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
4888 goto err_free_irq;
4889 }
4890
4891 /* Disable interrupts */
4892 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4893 free_irq(priv->pdev->irq, hw);
4894
4895 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
4896 priv->device_info->part_name,
4897 priv->hw_rev, hw->wiphy->perm_addr,
4898 priv->ap_fw ? "AP" : "STA",
4899 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4900 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
4901
4902 return 0;
4903
4904err_free_irq:
4905 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4906 free_irq(priv->pdev->irq, hw);
4907
4908err_free_queues:
4909 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4910 mwl8k_txq_deinit(hw, i);
4911 mwl8k_rxq_deinit(hw, 0);
4912
4913err_stop_firmware:
4914 mwl8k_hw_reset(priv);
4915
4916 return rc;
4917}
4918
4919/*
4920 * invoke mwl8k_reload_firmware to change the firmware image after the device
4921 * has already been registered
4922 */
4923static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
4924{
4925 int i, rc = 0;
4926 struct mwl8k_priv *priv = hw->priv;
4927
4928 mwl8k_stop(hw);
4929 mwl8k_rxq_deinit(hw, 0);
4930
4931 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4932 mwl8k_txq_deinit(hw, i);
4933
Brian Cavagnolo99020472010-11-12 17:23:52 -08004934 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004935 if (rc)
4936 goto fail;
4937
4938 rc = mwl8k_probe_hw(hw);
4939 if (rc)
4940 goto fail;
4941
4942 rc = mwl8k_start(hw);
4943 if (rc)
4944 goto fail;
4945
4946 rc = mwl8k_config(hw, ~0);
4947 if (rc)
4948 goto fail;
4949
4950 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4951 rc = mwl8k_conf_tx(hw, i, &priv->wmm_params[i]);
4952 if (rc)
4953 goto fail;
4954 }
4955
4956 return rc;
4957
4958fail:
4959 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
4960 return rc;
4961}
4962
4963static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
4964{
4965 struct ieee80211_hw *hw = priv->hw;
4966 int i, rc;
4967
Brian Cavagnolo99020472010-11-12 17:23:52 -08004968 rc = mwl8k_load_firmware(hw);
4969 mwl8k_release_firmware(priv);
4970 if (rc) {
4971 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4972 return rc;
4973 }
4974
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004975 /*
4976 * Extra headroom is the size of the required DMA header
4977 * minus the size of the smallest 802.11 frame (CTS frame).
4978 */
4979 hw->extra_tx_headroom =
4980 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
4981
4982 hw->channel_change_time = 10;
4983
4984 hw->queues = MWL8K_TX_QUEUES;
4985
4986 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08004987 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004988 hw->vif_data_size = sizeof(struct mwl8k_vif);
4989 hw->sta_data_size = sizeof(struct mwl8k_sta);
4990
4991 priv->macids_used = 0;
4992 INIT_LIST_HEAD(&priv->vif_list);
4993
4994 /* Set default radio state and preamble */
4995 priv->radio_on = 0;
4996 priv->radio_short_preamble = 0;
4997
4998 /* Finalize join worker */
4999 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
5000
5001 /* TX reclaim and RX tasklets. */
5002 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5003 tasklet_disable(&priv->poll_tx_task);
5004 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5005 tasklet_disable(&priv->poll_rx_task);
5006
5007 /* Power management cookie */
5008 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5009 if (priv->cookie == NULL)
5010 return -ENOMEM;
5011
5012 mutex_init(&priv->fw_mutex);
5013 priv->fw_mutex_owner = NULL;
5014 priv->fw_mutex_depth = 0;
5015 priv->hostcmd_wait = NULL;
5016
5017 spin_lock_init(&priv->tx_lock);
5018
5019 priv->tx_wait = NULL;
5020
5021 rc = mwl8k_probe_hw(hw);
5022 if (rc)
5023 goto err_free_cookie;
5024
5025 hw->wiphy->interface_modes = 0;
5026 if (priv->ap_macids_supported || priv->device_info->fw_image_ap)
5027 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
5028 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5029 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5030
5031 rc = ieee80211_register_hw(hw);
5032 if (rc) {
5033 wiphy_err(hw->wiphy, "Cannot register device\n");
5034 goto err_unprobe_hw;
5035 }
5036
5037 return 0;
5038
5039err_unprobe_hw:
5040 for (i = 0; i < MWL8K_TX_QUEUES; i++)
5041 mwl8k_txq_deinit(hw, i);
5042 mwl8k_rxq_deinit(hw, 0);
5043
5044err_free_cookie:
5045 if (priv->cookie != NULL)
5046 pci_free_consistent(priv->pdev, 4,
5047 priv->cookie, priv->cookie_dma);
5048
5049 return rc;
5050}
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005051static int __devinit mwl8k_probe(struct pci_dev *pdev,
5052 const struct pci_device_id *id)
5053{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005054 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005055 struct ieee80211_hw *hw;
5056 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005057 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005058 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02005059
5060 if (!printed_version) {
5061 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5062 printed_version = 1;
5063 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005064
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005065
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005066 rc = pci_enable_device(pdev);
5067 if (rc) {
5068 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
5069 MWL8K_NAME);
5070 return rc;
5071 }
5072
5073 rc = pci_request_regions(pdev, MWL8K_NAME);
5074 if (rc) {
5075 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
5076 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005077 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005078 }
5079
5080 pci_set_master(pdev);
5081
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005082
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005083 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
5084 if (hw == NULL) {
5085 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
5086 rc = -ENOMEM;
5087 goto err_free_reg;
5088 }
5089
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005090 SET_IEEE80211_DEV(hw, &pdev->dev);
5091 pci_set_drvdata(pdev, hw);
5092
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005093 priv = hw->priv;
5094 priv->hw = hw;
5095 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05005096 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005097
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005098
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005099 priv->sram = pci_iomap(pdev, 0, 0x10000);
5100 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005101 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005102 goto err_iounmap;
5103 }
5104
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005105 /*
5106 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
5107 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
5108 */
5109 priv->regs = pci_iomap(pdev, 1, 0x10000);
5110 if (priv->regs == NULL) {
5111 priv->regs = pci_iomap(pdev, 2, 0x10000);
5112 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005113 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005114 goto err_iounmap;
5115 }
5116 }
5117
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005118 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08005119 * Choose the initial fw image depending on user input. If a second
5120 * image is available, make it the alternative image that will be
5121 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005122 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005123 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005124 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005125 if (ap_mode_default && di->fw_image_ap) {
5126 priv->fw_pref = di->fw_image_ap;
5127 priv->fw_alt = di->fw_image_sta;
5128 } else if (!ap_mode_default && di->fw_image_sta) {
5129 priv->fw_pref = di->fw_image_sta;
5130 priv->fw_alt = di->fw_image_ap;
5131 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005132 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005133 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005134 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
5135 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005136 priv->fw_pref = di->fw_image_ap;
5137 }
5138 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005139 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005140 goto err_stop_firmware;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005141 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005142
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005143err_stop_firmware:
5144 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005145
5146err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005147 if (priv->regs != NULL)
5148 pci_iounmap(pdev, priv->regs);
5149
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005150 if (priv->sram != NULL)
5151 pci_iounmap(pdev, priv->sram);
5152
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005153 pci_set_drvdata(pdev, NULL);
5154 ieee80211_free_hw(hw);
5155
5156err_free_reg:
5157 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005158
5159err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005160 pci_disable_device(pdev);
5161
5162 return rc;
5163}
5164
Joerg Albert230f7af2009-04-18 02:10:45 +02005165static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005166{
5167 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
5168}
5169
Joerg Albert230f7af2009-04-18 02:10:45 +02005170static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005171{
5172 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
5173 struct mwl8k_priv *priv;
5174 int i;
5175
5176 if (hw == NULL)
5177 return;
5178 priv = hw->priv;
5179
Brian Cavagnolo99020472010-11-12 17:23:52 -08005180 wait_for_completion(&priv->firmware_loading_complete);
5181
5182 if (priv->fw_state == FW_STATE_ERROR) {
5183 mwl8k_hw_reset(priv);
5184 goto unmap;
5185 }
5186
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005187 ieee80211_stop_queues(hw);
5188
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02005189 ieee80211_unregister_hw(hw);
5190
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005191 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01005192 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005193 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005194
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005195 /* Stop hardware */
5196 mwl8k_hw_reset(priv);
5197
5198 /* Return all skbs to mac80211 */
5199 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01005200 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005201
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005202 for (i = 0; i < MWL8K_TX_QUEUES; i++)
5203 mwl8k_txq_deinit(hw, i);
5204
5205 mwl8k_rxq_deinit(hw, 0);
5206
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005207 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005208
Brian Cavagnolo99020472010-11-12 17:23:52 -08005209unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005210 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005211 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005212 pci_set_drvdata(pdev, NULL);
5213 ieee80211_free_hw(hw);
5214 pci_release_regions(pdev);
5215 pci_disable_device(pdev);
5216}
5217
5218static struct pci_driver mwl8k_driver = {
5219 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005220 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005221 .probe = mwl8k_probe,
5222 .remove = __devexit_p(mwl8k_remove),
5223 .shutdown = __devexit_p(mwl8k_shutdown),
5224};
5225
5226static int __init mwl8k_init(void)
5227{
5228 return pci_register_driver(&mwl8k_driver);
5229}
5230
5231static void __exit mwl8k_exit(void)
5232{
5233 pci_unregister_driver(&mwl8k_driver);
5234}
5235
5236module_init(mwl8k_init);
5237module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005238
5239MODULE_DESCRIPTION(MWL8K_DESC);
5240MODULE_VERSION(MWL8K_VERSION);
5241MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
5242MODULE_LICENSE("GPL");