blob: 9db66d58f0efb05ce1b2e7536b685e51dac0604c [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
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100138struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100139 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100141
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200142 struct mwl8k_device_info *device_info;
143
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100144 void __iomem *sram;
145 void __iomem *regs;
146
147 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800148 const struct firmware *fw_helper;
149 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100150
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100151 /* hardware/firmware parameters */
152 bool ap_fw;
153 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100154 struct ieee80211_supported_band band_24;
155 struct ieee80211_channel channels_24[14];
156 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100157 struct ieee80211_supported_band band_50;
158 struct ieee80211_channel channels_50[4];
159 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100160 u32 ap_macids_supported;
161 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100162
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700163 /* Ampdu stream information */
164 u8 num_ampdu_queues;
165
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200166 /* firmware access */
167 struct mutex fw_mutex;
168 struct task_struct *fw_mutex_owner;
169 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200170 struct completion *hostcmd_wait;
171
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100172 /* lock held over TX and TX reap */
173 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100174
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200175 /* TX quiesce completion, protected by fw_mutex and tx_lock */
176 struct completion *tx_wait;
177
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100178 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100179 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100180 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100181
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100182 /* power management status cookie from firmware */
183 u32 *cookie;
184 dma_addr_t cookie_dma;
185
186 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100187 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200188 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100189
190 /*
191 * Running count of TX packets in flight, to avoid
192 * iterating over the transmit rings each time.
193 */
194 int pending_tx_pkts;
195
196 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700197 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES + MWL8K_MAX_AMPDU_QUEUES];
198 u32 txq_offset[MWL8K_TX_QUEUES + MWL8K_MAX_AMPDU_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100199
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200200 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200201 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200202 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200203 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100204
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100205 /* XXX need to convert this to handle multiple interfaces */
206 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200207 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100208 struct sk_buff *beacon_skb;
209
210 /*
211 * This FJ worker has to be global as it is scheduled from the
212 * RX handler. At this point we don't know which interface it
213 * belongs to until the list of bssids waiting to complete join
214 * is checked.
215 */
216 struct work_struct finalize_join_worker;
217
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100218 /* Tasklet to perform TX reclaim. */
219 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100220
221 /* Tasklet to perform RX. */
222 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400223
224 /* Most recently reported noise in dBm */
225 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800226
227 /*
228 * preserve the queue configurations so they can be restored if/when
229 * the firmware image is swapped.
230 */
231 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800232
233 /* async firmware loading state */
234 unsigned fw_state;
235 char *fw_pref;
236 char *fw_alt;
237 struct completion firmware_loading_complete;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100238};
239
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800240#define MAX_WEP_KEY_LEN 13
241#define NUM_WEP_KEYS 4
242
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100243/* Per interface specific private data */
244struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100245 struct list_head list;
246 struct ieee80211_vif *vif;
247
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100248 /* Firmware macid for this vif. */
249 int macid;
250
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100251 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100252 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800253
254 /* Saved WEP keys */
255 struct {
256 u8 enabled;
257 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
258 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800259
260 /* BSSID */
261 u8 bssid[ETH_ALEN];
262
263 /* A flag to indicate is HW crypto is enabled for this bssid */
264 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100265};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200266#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800267#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100268
Lennert Buytenheka6804002010-01-04 21:55:42 +0100269struct mwl8k_sta {
270 /* Index into station database. Returned by UPDATE_STADB. */
271 u8 peer_id;
272};
273#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
274
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100275static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100276 { .center_freq = 2412, .hw_value = 1, },
277 { .center_freq = 2417, .hw_value = 2, },
278 { .center_freq = 2422, .hw_value = 3, },
279 { .center_freq = 2427, .hw_value = 4, },
280 { .center_freq = 2432, .hw_value = 5, },
281 { .center_freq = 2437, .hw_value = 6, },
282 { .center_freq = 2442, .hw_value = 7, },
283 { .center_freq = 2447, .hw_value = 8, },
284 { .center_freq = 2452, .hw_value = 9, },
285 { .center_freq = 2457, .hw_value = 10, },
286 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100287 { .center_freq = 2467, .hw_value = 12, },
288 { .center_freq = 2472, .hw_value = 13, },
289 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100290};
291
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100292static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100293 { .bitrate = 10, .hw_value = 2, },
294 { .bitrate = 20, .hw_value = 4, },
295 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200296 { .bitrate = 110, .hw_value = 22, },
297 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100298 { .bitrate = 60, .hw_value = 12, },
299 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100300 { .bitrate = 120, .hw_value = 24, },
301 { .bitrate = 180, .hw_value = 36, },
302 { .bitrate = 240, .hw_value = 48, },
303 { .bitrate = 360, .hw_value = 72, },
304 { .bitrate = 480, .hw_value = 96, },
305 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100306 { .bitrate = 720, .hw_value = 144, },
307};
308
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100309static const struct ieee80211_channel mwl8k_channels_50[] = {
310 { .center_freq = 5180, .hw_value = 36, },
311 { .center_freq = 5200, .hw_value = 40, },
312 { .center_freq = 5220, .hw_value = 44, },
313 { .center_freq = 5240, .hw_value = 48, },
314};
315
316static const struct ieee80211_rate mwl8k_rates_50[] = {
317 { .bitrate = 60, .hw_value = 12, },
318 { .bitrate = 90, .hw_value = 18, },
319 { .bitrate = 120, .hw_value = 24, },
320 { .bitrate = 180, .hw_value = 36, },
321 { .bitrate = 240, .hw_value = 48, },
322 { .bitrate = 360, .hw_value = 72, },
323 { .bitrate = 480, .hw_value = 96, },
324 { .bitrate = 540, .hw_value = 108, },
325 { .bitrate = 720, .hw_value = 144, },
326};
327
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100328/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100329#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800330#define MWL8K_CMD_SET 0x0001
331#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100332
333/* Firmware command codes */
334#define MWL8K_CMD_CODE_DNLD 0x0001
335#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200336#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100337#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
338#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200339#define MWL8K_CMD_RADIO_CONTROL 0x001c
340#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800341#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200342#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100343#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100344#define MWL8K_CMD_SET_PRE_SCAN 0x0107
345#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200346#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100347#define MWL8K_CMD_SET_AID 0x010d
348#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200349#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100350#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200351#define MWL8K_CMD_SET_SLOT 0x0114
352#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
353#define MWL8K_CMD_SET_WMM_MODE 0x0123
354#define MWL8K_CMD_MIMO_CONFIG 0x0125
355#define MWL8K_CMD_USE_FIXED_RATE 0x0126
356#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100357#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200358#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100359#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
360#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800361#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200362#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100363
John W. Linvilleb6037422010-07-20 13:55:00 -0400364static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100365{
John W. Linvilleb6037422010-07-20 13:55:00 -0400366 u16 command = le16_to_cpu(cmd);
367
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100368#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
369 snprintf(buf, bufsize, "%s", #x);\
370 return buf;\
371 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400372 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100373 MWL8K_CMDNAME(CODE_DNLD);
374 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200375 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100376 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
377 MWL8K_CMDNAME(GET_STAT);
378 MWL8K_CMDNAME(RADIO_CONTROL);
379 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800380 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200381 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100382 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100383 MWL8K_CMDNAME(SET_PRE_SCAN);
384 MWL8K_CMDNAME(SET_POST_SCAN);
385 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100386 MWL8K_CMDNAME(SET_AID);
387 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200388 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100389 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200390 MWL8K_CMDNAME(SET_SLOT);
391 MWL8K_CMDNAME(SET_EDCA_PARAMS);
392 MWL8K_CMDNAME(SET_WMM_MODE);
393 MWL8K_CMDNAME(MIMO_CONFIG);
394 MWL8K_CMDNAME(USE_FIXED_RATE);
395 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200396 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200397 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100398 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100399 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800400 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200401 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100402 default:
403 snprintf(buf, bufsize, "0x%x", cmd);
404 }
405#undef MWL8K_CMDNAME
406
407 return buf;
408}
409
410/* Hardware and firmware reset */
411static void mwl8k_hw_reset(struct mwl8k_priv *priv)
412{
413 iowrite32(MWL8K_H2A_INT_RESET,
414 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
415 iowrite32(MWL8K_H2A_INT_RESET,
416 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
417 msleep(20);
418}
419
420/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800421static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100422{
423 if (*fw == NULL)
424 return;
425 release_firmware(*fw);
426 *fw = NULL;
427}
428
429static void mwl8k_release_firmware(struct mwl8k_priv *priv)
430{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100431 mwl8k_release_fw(&priv->fw_ucode);
432 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100433}
434
Brian Cavagnolo99020472010-11-12 17:23:52 -0800435/* states for asynchronous f/w loading */
436static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
437enum {
438 FW_STATE_INIT = 0,
439 FW_STATE_LOADING_PREF,
440 FW_STATE_LOADING_ALT,
441 FW_STATE_ERROR,
442};
443
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100444/* Request fw image */
445static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800446 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800447 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100448{
449 /* release current image */
450 if (*fw != NULL)
451 mwl8k_release_fw(fw);
452
Brian Cavagnolo99020472010-11-12 17:23:52 -0800453 if (nowait)
454 return request_firmware_nowait(THIS_MODULE, 1, fname,
455 &priv->pdev->dev, GFP_KERNEL,
456 priv, mwl8k_fw_state_machine);
457 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800458 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100459}
460
Brian Cavagnolo99020472010-11-12 17:23:52 -0800461static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
462 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100463{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200464 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100465 int rc;
466
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200467 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800468 if (nowait)
469 rc = mwl8k_request_fw(priv, di->helper_image,
470 &priv->fw_helper, true);
471 else
472 rc = mwl8k_request_fw(priv, di->helper_image,
473 &priv->fw_helper, false);
474 if (rc)
475 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
476 pci_name(priv->pdev), di->helper_image);
477
478 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200479 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100480 }
481
Brian Cavagnolo99020472010-11-12 17:23:52 -0800482 if (nowait) {
483 /*
484 * if we get here, no helper image is needed. Skip the
485 * FW_STATE_INIT state.
486 */
487 priv->fw_state = FW_STATE_LOADING_PREF;
488 rc = mwl8k_request_fw(priv, fw_image,
489 &priv->fw_ucode,
490 true);
491 } else
492 rc = mwl8k_request_fw(priv, fw_image,
493 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100494 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200495 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800496 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100497 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100498 return rc;
499 }
500
501 return 0;
502}
503
504struct mwl8k_cmd_pkt {
505 __le16 code;
506 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100507 __u8 seq_num;
508 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100509 __le16 result;
510 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000511} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100512
513/*
514 * Firmware loading.
515 */
516static int
517mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
518{
519 void __iomem *regs = priv->regs;
520 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100521 int loops;
522
523 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
524 if (pci_dma_mapping_error(priv->pdev, dma_addr))
525 return -ENOMEM;
526
527 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
528 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
529 iowrite32(MWL8K_H2A_INT_DOORBELL,
530 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
531 iowrite32(MWL8K_H2A_INT_DUMMY,
532 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
533
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100534 loops = 1000;
535 do {
536 u32 int_code;
537
538 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
539 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
540 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100541 break;
542 }
543
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200544 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100545 udelay(1);
546 } while (--loops);
547
548 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
549
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200550 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100551}
552
553static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
554 const u8 *data, size_t length)
555{
556 struct mwl8k_cmd_pkt *cmd;
557 int done;
558 int rc = 0;
559
560 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
561 if (cmd == NULL)
562 return -ENOMEM;
563
564 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
565 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100566 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100567 cmd->result = 0;
568
569 done = 0;
570 while (length) {
571 int block_size = length > 256 ? 256 : length;
572
573 memcpy(cmd->payload, data + done, block_size);
574 cmd->length = cpu_to_le16(block_size);
575
576 rc = mwl8k_send_fw_load_cmd(priv, cmd,
577 sizeof(*cmd) + block_size);
578 if (rc)
579 break;
580
581 done += block_size;
582 length -= block_size;
583 }
584
585 if (!rc) {
586 cmd->length = 0;
587 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
588 }
589
590 kfree(cmd);
591
592 return rc;
593}
594
595static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
596 const u8 *data, size_t length)
597{
598 unsigned char *buffer;
599 int may_continue, rc = 0;
600 u32 done, prev_block_size;
601
602 buffer = kmalloc(1024, GFP_KERNEL);
603 if (buffer == NULL)
604 return -ENOMEM;
605
606 done = 0;
607 prev_block_size = 0;
608 may_continue = 1000;
609 while (may_continue > 0) {
610 u32 block_size;
611
612 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
613 if (block_size & 1) {
614 block_size &= ~1;
615 may_continue--;
616 } else {
617 done += prev_block_size;
618 length -= prev_block_size;
619 }
620
621 if (block_size > 1024 || block_size > length) {
622 rc = -EOVERFLOW;
623 break;
624 }
625
626 if (length == 0) {
627 rc = 0;
628 break;
629 }
630
631 if (block_size == 0) {
632 rc = -EPROTO;
633 may_continue--;
634 udelay(1);
635 continue;
636 }
637
638 prev_block_size = block_size;
639 memcpy(buffer, data + done, block_size);
640
641 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
642 if (rc)
643 break;
644 }
645
646 if (!rc && length != 0)
647 rc = -EREMOTEIO;
648
649 kfree(buffer);
650
651 return rc;
652}
653
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200654static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100655{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200656 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800657 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200658 int rc;
659 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100660
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200661 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800662 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100663
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200664 if (helper == NULL) {
665 printk(KERN_ERR "%s: helper image needed but none "
666 "given\n", pci_name(priv->pdev));
667 return -EINVAL;
668 }
669
670 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100671 if (rc) {
672 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200673 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100674 return rc;
675 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100676 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100677
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200678 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100679 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200680 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100681 }
682
683 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200684 printk(KERN_ERR "%s: unable to load firmware image\n",
685 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100686 return rc;
687 }
688
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100689 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100690
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100691 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100692 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200693 u32 ready_code;
694
695 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
696 if (ready_code == MWL8K_FWAP_READY) {
697 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100698 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200699 } else if (ready_code == MWL8K_FWSTA_READY) {
700 priv->ap_fw = 0;
701 break;
702 }
703
704 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100705 udelay(1);
706 } while (--loops);
707
708 return loops ? 0 : -ETIMEDOUT;
709}
710
711
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100712/* DMA header used by firmware and hardware. */
713struct mwl8k_dma_data {
714 __le16 fwlen;
715 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100716 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000717} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100718
719/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100720static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100721{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100722 struct mwl8k_dma_data *tr;
723 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100724
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100725 tr = (struct mwl8k_dma_data *)skb->data;
726 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
727
728 if (hdrlen != sizeof(tr->wh)) {
729 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
730 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
731 *((__le16 *)(tr->data - 2)) = qos;
732 } else {
733 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
734 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100735 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100736
737 if (hdrlen != sizeof(*tr))
738 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100739}
740
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800741static void
742mwl8k_add_dma_header(struct sk_buff *skb, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100743{
744 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100745 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800746 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100747 struct mwl8k_dma_data *tr;
748
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100749 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100750 * Add a firmware DMA header; the firmware requires that we
751 * present a 2-byte payload length followed by a 4-address
752 * header (without QoS field), followed (optionally) by any
753 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100754 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100755 wh = (struct ieee80211_hdr *)skb->data;
756
757 hdrlen = ieee80211_hdrlen(wh->frame_control);
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800758 reqd_hdrlen = sizeof(*tr);
759
760 if (hdrlen != reqd_hdrlen)
761 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100762
763 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800764 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100765
766 tr = (struct mwl8k_dma_data *)skb->data;
767 if (wh != &tr->wh)
768 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100769 if (hdrlen != sizeof(tr->wh))
770 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100771
772 /*
773 * Firmware length is the length of the fully formed "802.11
774 * payload". That is, everything except for the 802.11 header.
775 * This includes all crypto material including the MIC.
776 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800777 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100778}
779
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800780static void mwl8k_encapsulate_tx_frame(struct sk_buff *skb)
781{
782 struct ieee80211_hdr *wh;
783 struct ieee80211_tx_info *tx_info;
784 struct ieee80211_key_conf *key_conf;
785 int data_pad;
786
787 wh = (struct ieee80211_hdr *)skb->data;
788
789 tx_info = IEEE80211_SKB_CB(skb);
790
791 key_conf = NULL;
792 if (ieee80211_is_data(wh->frame_control))
793 key_conf = tx_info->control.hw_key;
794
795 /*
796 * Make sure the packet header is in the DMA header format (4-address
797 * without QoS), the necessary crypto padding between the header and the
798 * payload has already been provided by mac80211, but it doesn't add tail
799 * padding when HW crypto is enabled.
800 *
801 * We have the following trailer padding requirements:
802 * - WEP: 4 trailer bytes (ICV)
803 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
804 * - CCMP: 8 trailer bytes (MIC)
805 */
806 data_pad = 0;
807 if (key_conf != NULL) {
808 switch (key_conf->cipher) {
809 case WLAN_CIPHER_SUITE_WEP40:
810 case WLAN_CIPHER_SUITE_WEP104:
811 data_pad = 4;
812 break;
813 case WLAN_CIPHER_SUITE_TKIP:
814 data_pad = 12;
815 break;
816 case WLAN_CIPHER_SUITE_CCMP:
817 data_pad = 8;
818 break;
819 }
820 }
821 mwl8k_add_dma_header(skb, data_pad);
822}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100823
824/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100825 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200826 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100827struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200828 __le16 pkt_len;
829 __u8 sq2;
830 __u8 rate;
831 __le32 pkt_phys_addr;
832 __le32 next_rxd_phys_addr;
833 __le16 qos_control;
834 __le16 htsig2;
835 __le32 hw_rssi_info;
836 __le32 hw_noise_floor_info;
837 __u8 noise_floor;
838 __u8 pad0[3];
839 __u8 rssi;
840 __u8 rx_status;
841 __u8 channel;
842 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000843} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200844
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100845#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
846#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
847#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100848
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100849#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200850
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800851/* 8366 AP rx_status bits */
852#define MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
853#define MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
854#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
855#define MWL8K_8366_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
856#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
857
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100858static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200859{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100860 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200861
862 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100863 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200864}
865
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100866static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200867{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100868 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200869
870 rxd->pkt_len = cpu_to_le16(len);
871 rxd->pkt_phys_addr = cpu_to_le32(addr);
872 wmb();
873 rxd->rx_ctrl = 0;
874}
875
876static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100877mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400878 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200879{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100880 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200881
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100882 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200883 return -1;
884 rmb();
885
886 memset(status, 0, sizeof(*status));
887
888 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400889 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200890
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100891 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200892 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100893 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100894 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100895 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200896 } else {
897 int i;
898
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100899 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
900 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200901 status->rate_idx = i;
902 break;
903 }
904 }
905 }
906
Lennert Buytenhek85478342010-01-12 13:48:56 +0100907 if (rxd->channel > 14) {
908 status->band = IEEE80211_BAND_5GHZ;
909 if (!(status->flag & RX_FLAG_HT))
910 status->rate_idx -= 5;
911 } else {
912 status->band = IEEE80211_BAND_2GHZ;
913 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900914 status->freq = ieee80211_channel_to_frequency(rxd->channel,
915 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200916
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100917 *qos = rxd->qos_control;
918
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800919 if ((rxd->rx_status != MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
920 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK) &&
921 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
922 status->flag |= RX_FLAG_MMIC_ERROR;
923
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200924 return le16_to_cpu(rxd->pkt_len);
925}
926
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100927static struct rxd_ops rxd_8366_ap_ops = {
928 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
929 .rxd_init = mwl8k_rxd_8366_ap_init,
930 .rxd_refill = mwl8k_rxd_8366_ap_refill,
931 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200932};
933
934/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100935 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100936 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100937struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100938 __le16 pkt_len;
939 __u8 link_quality;
940 __u8 noise_level;
941 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200942 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100943 __le16 qos_control;
944 __le16 rate_info;
945 __le32 pad0[4];
946 __u8 rssi;
947 __u8 channel;
948 __le16 pad1;
949 __u8 rx_ctrl;
950 __u8 rx_status;
951 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000952} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100953
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100954#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
955#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
956#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
957#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
958#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
959#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200960
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100961#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800962#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
963/* ICV=0 or MIC=1 */
964#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
965/* Key is uploaded only in failure case */
966#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200967
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100968static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200969{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100970 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200971
972 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100973 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200974}
975
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100976static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200977{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100978 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200979
980 rxd->pkt_len = cpu_to_le16(len);
981 rxd->pkt_phys_addr = cpu_to_le32(addr);
982 wmb();
983 rxd->rx_ctrl = 0;
984}
985
986static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100987mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400988 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200989{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100990 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200991 u16 rate_info;
992
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100993 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200994 return -1;
995 rmb();
996
997 rate_info = le16_to_cpu(rxd->rate_info);
998
999 memset(status, 0, sizeof(*status));
1000
1001 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -04001002 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001003 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1004 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001005
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001006 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001007 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001008 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001009 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001010 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001011 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001012 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001013 status->flag |= RX_FLAG_HT;
1014
Lennert Buytenhek85478342010-01-12 13:48:56 +01001015 if (rxd->channel > 14) {
1016 status->band = IEEE80211_BAND_5GHZ;
1017 if (!(status->flag & RX_FLAG_HT))
1018 status->rate_idx -= 5;
1019 } else {
1020 status->band = IEEE80211_BAND_2GHZ;
1021 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001022 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1023 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001024
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001025 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001026 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1027 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1028 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001029
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001030 return le16_to_cpu(rxd->pkt_len);
1031}
1032
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001033static struct rxd_ops rxd_sta_ops = {
1034 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1035 .rxd_init = mwl8k_rxd_sta_init,
1036 .rxd_refill = mwl8k_rxd_sta_refill,
1037 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001038};
1039
1040
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001041#define MWL8K_RX_DESCS 256
1042#define MWL8K_RX_MAXSZ 3800
1043
1044static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1045{
1046 struct mwl8k_priv *priv = hw->priv;
1047 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1048 int size;
1049 int i;
1050
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001051 rxq->rxd_count = 0;
1052 rxq->head = 0;
1053 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001054
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001055 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001056
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001057 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1058 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001059 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001060 return -ENOMEM;
1061 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001062 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001063
Shan Weib9ede5f2011-03-08 11:02:03 +08001064 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001065 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001066 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001067 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001068 return -ENOMEM;
1069 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001070
1071 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001072 int desc_size;
1073 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001074 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001075 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001076
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001077 desc_size = priv->rxd_ops->rxd_size;
1078 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001079
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001080 nexti = i + 1;
1081 if (nexti == MWL8K_RX_DESCS)
1082 nexti = 0;
1083 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1084
1085 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001086 }
1087
1088 return 0;
1089}
1090
1091static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1092{
1093 struct mwl8k_priv *priv = hw->priv;
1094 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1095 int refilled;
1096
1097 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001098 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001099 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001100 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001101 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001102 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001103
1104 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1105 if (skb == NULL)
1106 break;
1107
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001108 addr = pci_map_single(priv->pdev, skb->data,
1109 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001110
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001111 rxq->rxd_count++;
1112 rx = rxq->tail++;
1113 if (rxq->tail == MWL8K_RX_DESCS)
1114 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001115 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001116 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001117
1118 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1119 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001120
1121 refilled++;
1122 }
1123
1124 return refilled;
1125}
1126
1127/* Must be called only when the card's reception is completely halted */
1128static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1129{
1130 struct mwl8k_priv *priv = hw->priv;
1131 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1132 int i;
1133
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001134 if (rxq->rxd == NULL)
1135 return;
1136
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001137 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001138 if (rxq->buf[i].skb != NULL) {
1139 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001140 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001141 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001142 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001143
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001144 kfree_skb(rxq->buf[i].skb);
1145 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001146 }
1147 }
1148
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001149 kfree(rxq->buf);
1150 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001151
1152 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001153 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001154 rxq->rxd, rxq->rxd_dma);
1155 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001156}
1157
1158
1159/*
1160 * Scan a list of BSSIDs to process for finalize join.
1161 * Allows for extension to process multiple BSSIDs.
1162 */
1163static inline int
1164mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1165{
1166 return priv->capture_beacon &&
1167 ieee80211_is_beacon(wh->frame_control) &&
1168 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1169}
1170
Lennert Buytenhek37797522009-10-22 20:19:45 +02001171static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1172 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001173{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001174 struct mwl8k_priv *priv = hw->priv;
1175
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001176 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001177 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001178
1179 /*
1180 * Use GFP_ATOMIC as rxq_process is called from
1181 * the primary interrupt handler, memory allocation call
1182 * must not sleep.
1183 */
1184 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1185 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001186 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001187}
1188
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001189static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1190 u8 *bssid)
1191{
1192 struct mwl8k_vif *mwl8k_vif;
1193
1194 list_for_each_entry(mwl8k_vif,
1195 vif_list, list) {
1196 if (memcmp(bssid, mwl8k_vif->bssid,
1197 ETH_ALEN) == 0)
1198 return mwl8k_vif;
1199 }
1200
1201 return NULL;
1202}
1203
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001204static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1205{
1206 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001207 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001208 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1209 int processed;
1210
1211 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001212 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001213 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001214 void *rxd;
1215 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001216 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001217 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001218 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001219
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001220 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001221 if (skb == NULL)
1222 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001223
1224 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1225
John W. Linville0d462bb2010-07-28 14:04:24 -04001226 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1227 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001228 if (pkt_len < 0)
1229 break;
1230
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001231 rxq->buf[rxq->head].skb = NULL;
1232
1233 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001234 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001235 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001236 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001237
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001238 rxq->head++;
1239 if (rxq->head == MWL8K_RX_DESCS)
1240 rxq->head = 0;
1241
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001242 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001243
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001244 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001245
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001246 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001247 * Check for a pending join operation. Save a
1248 * copy of the beacon and schedule a tasklet to
1249 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001250 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001251 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001252 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001253
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001254 if (ieee80211_has_protected(wh->frame_control)) {
1255
1256 /* Check if hw crypto has been enabled for
1257 * this bss. If yes, set the status flags
1258 * accordingly
1259 */
1260 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1261 wh->addr1);
1262
1263 if (mwl8k_vif != NULL &&
1264 mwl8k_vif->is_hw_crypto_enabled == true) {
1265 /*
1266 * When MMIC ERROR is encountered
1267 * by the firmware, payload is
1268 * dropped and only 32 bytes of
1269 * mwl8k Firmware header is sent
1270 * to the host.
1271 *
1272 * We need to add four bytes of
1273 * key information. In it
1274 * MAC80211 expects keyidx set to
1275 * 0 for triggering Counter
1276 * Measure of MMIC failure.
1277 */
1278 if (status.flag & RX_FLAG_MMIC_ERROR) {
1279 struct mwl8k_dma_data *tr;
1280 tr = (struct mwl8k_dma_data *)skb->data;
1281 memset((void *)&(tr->data), 0, 4);
1282 pkt_len += 4;
1283 }
1284
1285 if (!ieee80211_is_auth(wh->frame_control))
1286 status.flag |= RX_FLAG_IV_STRIPPED |
1287 RX_FLAG_DECRYPTED |
1288 RX_FLAG_MMIC_STRIPPED;
1289 }
1290 }
1291
1292 skb_put(skb, pkt_len);
1293 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001294 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1295 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001296
1297 processed++;
1298 }
1299
1300 return processed;
1301}
1302
1303
1304/*
1305 * Packet transmission.
1306 */
1307
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001308#define MWL8K_TXD_STATUS_OK 0x00000001
1309#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1310#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1311#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001312#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001313
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001314#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1315#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1316#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1317#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1318#define MWL8K_QOS_EOSP 0x0010
1319
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001320struct mwl8k_tx_desc {
1321 __le32 status;
1322 __u8 data_rate;
1323 __u8 tx_priority;
1324 __le16 qos_control;
1325 __le32 pkt_phys_addr;
1326 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001327 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001328 __le32 next_txd_phys_addr;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07001329 __le32 timestamp;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001330 __le16 rate_info;
1331 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001332 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001333} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001334
1335#define MWL8K_TX_DESCS 128
1336
1337static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1338{
1339 struct mwl8k_priv *priv = hw->priv;
1340 struct mwl8k_tx_queue *txq = priv->txq + index;
1341 int size;
1342 int i;
1343
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001344 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001345 txq->head = 0;
1346 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001347
1348 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1349
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001350 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1351 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001352 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001353 return -ENOMEM;
1354 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001355 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001356
Shan Weib9ede5f2011-03-08 11:02:03 +08001357 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001358 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001359 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001360 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001361 return -ENOMEM;
1362 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001363
1364 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1365 struct mwl8k_tx_desc *tx_desc;
1366 int nexti;
1367
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001368 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001369 nexti = (i + 1) % MWL8K_TX_DESCS;
1370
1371 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001372 tx_desc->next_txd_phys_addr =
1373 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001374 }
1375
1376 return 0;
1377}
1378
1379static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1380{
1381 iowrite32(MWL8K_H2A_INT_PPA_READY,
1382 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1383 iowrite32(MWL8K_H2A_INT_DUMMY,
1384 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1385 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1386}
1387
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001388static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001389{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001390 struct mwl8k_priv *priv = hw->priv;
1391 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001392
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001393 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1394 struct mwl8k_tx_queue *txq = priv->txq + i;
1395 int fw_owned = 0;
1396 int drv_owned = 0;
1397 int unused = 0;
1398 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001399
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001400 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001401 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1402 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001403
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001404 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001405 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001406 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001407 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001408 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001409
1410 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001411 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001412 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001413
Joe Perchesc96c31e2010-07-26 14:39:58 -07001414 wiphy_err(hw->wiphy,
1415 "txq[%d] len=%d head=%d tail=%d "
1416 "fw_owned=%d drv_owned=%d unused=%d\n",
1417 i,
1418 txq->len, txq->head, txq->tail,
1419 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001420 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001421}
1422
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001423/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001424 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001425 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001426#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001427
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001428static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001429{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001430 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001431 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001432 int retry;
1433 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001434
1435 might_sleep();
1436
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001437 /*
1438 * The TX queues are stopped at this point, so this test
1439 * doesn't need to take ->tx_lock.
1440 */
1441 if (!priv->pending_tx_pkts)
1442 return 0;
1443
1444 retry = 0;
1445 rc = 0;
1446
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001447 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001448 priv->tx_wait = &tx_wait;
1449 while (!rc) {
1450 int oldcount;
1451 unsigned long timeout;
1452
1453 oldcount = priv->pending_tx_pkts;
1454
1455 spin_unlock_bh(&priv->tx_lock);
1456 timeout = wait_for_completion_timeout(&tx_wait,
1457 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1458 spin_lock_bh(&priv->tx_lock);
1459
1460 if (timeout) {
1461 WARN_ON(priv->pending_tx_pkts);
1462 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001463 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001464 }
1465 break;
1466 }
1467
1468 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001469 wiphy_notice(hw->wiphy,
1470 "waiting for tx rings to drain (%d -> %d pkts)\n",
1471 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001472 retry = 1;
1473 continue;
1474 }
1475
1476 priv->tx_wait = NULL;
1477
Joe Perchesc96c31e2010-07-26 14:39:58 -07001478 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1479 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001480 mwl8k_dump_tx_rings(hw);
1481
1482 rc = -ETIMEDOUT;
1483 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001484 spin_unlock_bh(&priv->tx_lock);
1485
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001486 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001487}
1488
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001489#define MWL8K_TXD_SUCCESS(status) \
1490 ((status) & (MWL8K_TXD_STATUS_OK | \
1491 MWL8K_TXD_STATUS_OK_RETRY | \
1492 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001493
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001494static int
1495mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496{
1497 struct mwl8k_priv *priv = hw->priv;
1498 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001499 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001500
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001501 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001502 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001503 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001504 struct mwl8k_tx_desc *tx_desc;
1505 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001506 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001507 struct sk_buff *skb;
1508 struct ieee80211_tx_info *info;
1509 u32 status;
1510
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001511 tx = txq->head;
1512 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001513
1514 status = le32_to_cpu(tx_desc->status);
1515
1516 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1517 if (!force)
1518 break;
1519 tx_desc->status &=
1520 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1521 }
1522
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001523 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001524 BUG_ON(txq->len == 0);
1525 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001526 priv->pending_tx_pkts--;
1527
1528 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001529 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001530 skb = txq->skb[tx];
1531 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001532
1533 BUG_ON(skb == NULL);
1534 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1535
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001536 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001537
1538 /* Mark descriptor as unused */
1539 tx_desc->pkt_phys_addr = 0;
1540 tx_desc->pkt_len = 0;
1541
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001542 info = IEEE80211_SKB_CB(skb);
1543 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001544
1545 /* Rate control is happening in the firmware.
1546 * Ensure no tx rate is being reported.
1547 */
1548 info->status.rates[0].idx = -1;
1549 info->status.rates[0].count = 1;
1550
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001551 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001552 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001553
1554 ieee80211_tx_status_irqsafe(hw, skb);
1555
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001556 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001557 }
1558
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001559 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001560 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001561
1562 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001563}
1564
1565/* must be called only when the card's transmit is completely halted */
1566static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1567{
1568 struct mwl8k_priv *priv = hw->priv;
1569 struct mwl8k_tx_queue *txq = priv->txq + index;
1570
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001571 if (txq->txd == NULL)
1572 return;
1573
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001574 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001575
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001576 kfree(txq->skb);
1577 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578
1579 pci_free_consistent(priv->pdev,
1580 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001581 txq->txd, txq->txd_dma);
1582 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001583}
1584
Johannes Berg7bb45682011-02-24 14:42:06 +01001585static void
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001586mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1587{
1588 struct mwl8k_priv *priv = hw->priv;
1589 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001590 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001591 struct ieee80211_hdr *wh;
1592 struct mwl8k_tx_queue *txq;
1593 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001594 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001595 u32 txstatus;
1596 u8 txdatarate;
1597 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001598
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001599 wh = (struct ieee80211_hdr *)skb->data;
1600 if (ieee80211_is_data_qos(wh->frame_control))
1601 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1602 else
1603 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001604
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001605 if (priv->ap_fw)
1606 mwl8k_encapsulate_tx_frame(skb);
1607 else
1608 mwl8k_add_dma_header(skb, 0);
1609
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001610 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001611
1612 tx_info = IEEE80211_SKB_CB(skb);
1613 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001614
1615 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001616 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001617 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1618 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001619 }
1620
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001621 /* Setup firmware control bit fields for each frame type. */
1622 txstatus = 0;
1623 txdatarate = 0;
1624 if (ieee80211_is_mgmt(wh->frame_control) ||
1625 ieee80211_is_ctl(wh->frame_control)) {
1626 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001627 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001628 } else if (ieee80211_is_data(wh->frame_control)) {
1629 txdatarate = 1;
1630 if (is_multicast_ether_addr(wh->addr1))
1631 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1632
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001633 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001634 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001635 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001636 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001637 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001638 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001639
1640 dma = pci_map_single(priv->pdev, skb->data,
1641 skb->len, PCI_DMA_TODEVICE);
1642
1643 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001644 wiphy_debug(hw->wiphy,
1645 "failed to dma map skb, dropping TX frame.\n");
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001646 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001647 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001648 }
1649
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001650 spin_lock_bh(&priv->tx_lock);
1651
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001652 txq = priv->txq + index;
1653
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001654 BUG_ON(txq->skb[txq->tail] != NULL);
1655 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001656
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001657 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001658 tx->data_rate = txdatarate;
1659 tx->tx_priority = index;
1660 tx->qos_control = cpu_to_le16(qos);
1661 tx->pkt_phys_addr = cpu_to_le32(dma);
1662 tx->pkt_len = cpu_to_le16(skb->len);
1663 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001664 if (!priv->ap_fw && tx_info->control.sta != NULL)
1665 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1666 else
1667 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001668 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001669 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1670
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001671 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001672 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001673
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001674 txq->tail++;
1675 if (txq->tail == MWL8K_TX_DESCS)
1676 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001677
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001678 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001679 ieee80211_stop_queue(hw, index);
1680
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001681 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001682
1683 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001684}
1685
1686
1687/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001688 * Firmware access.
1689 *
1690 * We have the following requirements for issuing firmware commands:
1691 * - Some commands require that the packet transmit path is idle when
1692 * the command is issued. (For simplicity, we'll just quiesce the
1693 * transmit path for every command.)
1694 * - There are certain sequences of commands that need to be issued to
1695 * the hardware sequentially, with no other intervening commands.
1696 *
1697 * This leads to an implementation of a "firmware lock" as a mutex that
1698 * can be taken recursively, and which is taken by both the low-level
1699 * command submission function (mwl8k_post_cmd) as well as any users of
1700 * that function that require issuing of an atomic sequence of commands,
1701 * and quiesces the transmit path whenever it's taken.
1702 */
1703static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1704{
1705 struct mwl8k_priv *priv = hw->priv;
1706
1707 if (priv->fw_mutex_owner != current) {
1708 int rc;
1709
1710 mutex_lock(&priv->fw_mutex);
1711 ieee80211_stop_queues(hw);
1712
1713 rc = mwl8k_tx_wait_empty(hw);
1714 if (rc) {
1715 ieee80211_wake_queues(hw);
1716 mutex_unlock(&priv->fw_mutex);
1717
1718 return rc;
1719 }
1720
1721 priv->fw_mutex_owner = current;
1722 }
1723
1724 priv->fw_mutex_depth++;
1725
1726 return 0;
1727}
1728
1729static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1730{
1731 struct mwl8k_priv *priv = hw->priv;
1732
1733 if (!--priv->fw_mutex_depth) {
1734 ieee80211_wake_queues(hw);
1735 priv->fw_mutex_owner = NULL;
1736 mutex_unlock(&priv->fw_mutex);
1737 }
1738}
1739
1740
1741/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001742 * Command processing.
1743 */
1744
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001745/* Timeout firmware commands after 10s */
1746#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001747
1748static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1749{
1750 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1751 struct mwl8k_priv *priv = hw->priv;
1752 void __iomem *regs = priv->regs;
1753 dma_addr_t dma_addr;
1754 unsigned int dma_size;
1755 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001756 unsigned long timeout = 0;
1757 u8 buf[32];
1758
John W. Linvilleb6037422010-07-20 13:55:00 -04001759 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001760 dma_size = le16_to_cpu(cmd->length);
1761 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1762 PCI_DMA_BIDIRECTIONAL);
1763 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1764 return -ENOMEM;
1765
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001766 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001767 if (rc) {
1768 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1769 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001770 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001771 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001772
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001773 priv->hostcmd_wait = &cmd_wait;
1774 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1775 iowrite32(MWL8K_H2A_INT_DOORBELL,
1776 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1777 iowrite32(MWL8K_H2A_INT_DUMMY,
1778 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001779
1780 timeout = wait_for_completion_timeout(&cmd_wait,
1781 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1782
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001783 priv->hostcmd_wait = NULL;
1784
1785 mwl8k_fw_unlock(hw);
1786
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001787 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1788 PCI_DMA_BIDIRECTIONAL);
1789
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001790 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001791 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001792 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1793 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001794 rc = -ETIMEDOUT;
1795 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001796 int ms;
1797
1798 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1799
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001800 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001801 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001802 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001803 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1804 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001805 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001806 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001807 mwl8k_cmd_name(cmd->code,
1808 buf, sizeof(buf)),
1809 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001810 }
1811
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001812 return rc;
1813}
1814
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001815static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1816 struct ieee80211_vif *vif,
1817 struct mwl8k_cmd_pkt *cmd)
1818{
1819 if (vif != NULL)
1820 cmd->macid = MWL8K_VIF(vif)->macid;
1821 return mwl8k_post_cmd(hw, cmd);
1822}
1823
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001824/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001825 * Setup code shared between STA and AP firmware images.
1826 */
1827static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1828{
1829 struct mwl8k_priv *priv = hw->priv;
1830
1831 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1832 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1833
1834 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1835 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1836
1837 priv->band_24.band = IEEE80211_BAND_2GHZ;
1838 priv->band_24.channels = priv->channels_24;
1839 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1840 priv->band_24.bitrates = priv->rates_24;
1841 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1842
1843 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1844}
1845
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001846static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1847{
1848 struct mwl8k_priv *priv = hw->priv;
1849
1850 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1851 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1852
1853 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1854 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1855
1856 priv->band_50.band = IEEE80211_BAND_5GHZ;
1857 priv->band_50.channels = priv->channels_50;
1858 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1859 priv->band_50.bitrates = priv->rates_50;
1860 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1861
1862 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1863}
1864
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001865/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001866 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001867 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001868struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001869 struct mwl8k_cmd_pkt header;
1870 __u8 hw_rev;
1871 __u8 host_interface;
1872 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001873 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001874 __le16 region_code;
1875 __le32 fw_rev;
1876 __le32 ps_cookie;
1877 __le32 caps;
1878 __u8 mcs_bitmap[16];
1879 __le32 rx_queue_ptr;
1880 __le32 num_tx_queues;
1881 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1882 __le32 caps2;
1883 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001884 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001885} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001886
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001887#define MWL8K_CAP_MAX_AMSDU 0x20000000
1888#define MWL8K_CAP_GREENFIELD 0x08000000
1889#define MWL8K_CAP_AMPDU 0x04000000
1890#define MWL8K_CAP_RX_STBC 0x01000000
1891#define MWL8K_CAP_TX_STBC 0x00800000
1892#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1893#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1894#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1895#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1896#define MWL8K_CAP_DELAY_BA 0x00003000
1897#define MWL8K_CAP_MIMO 0x00000200
1898#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001899#define MWL8K_CAP_BAND_MASK 0x00000007
1900#define MWL8K_CAP_5GHZ 0x00000004
1901#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001902
Lennert Buytenhek06953232010-01-12 13:49:41 +01001903static void
1904mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1905 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001906{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001907 int rx_streams;
1908 int tx_streams;
1909
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001910 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001911
1912 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001913 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001914 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001915 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001916 if (cap & MWL8K_CAP_AMPDU) {
1917 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001918 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1919 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001920 }
1921 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001922 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001923 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001924 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001925 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001926 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001927 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001928 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001929 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001930 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001931 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001932 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001933
1934 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1935 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1936
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001937 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001938 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001939 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001940 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001941 band->ht_cap.mcs.rx_mask[2] = 0xff;
1942 band->ht_cap.mcs.rx_mask[4] = 0x01;
1943 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001944
1945 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001946 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1947 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001948 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1949 }
1950}
1951
Lennert Buytenhek06953232010-01-12 13:49:41 +01001952static void
1953mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1954{
1955 struct mwl8k_priv *priv = hw->priv;
1956
1957 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1958 mwl8k_setup_2ghz_band(hw);
1959 if (caps & MWL8K_CAP_MIMO)
1960 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1961 }
1962
1963 if (caps & MWL8K_CAP_5GHZ) {
1964 mwl8k_setup_5ghz_band(hw);
1965 if (caps & MWL8K_CAP_MIMO)
1966 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1967 }
1968}
1969
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001970static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001971{
1972 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001973 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001974 int rc;
1975 int i;
1976
1977 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1978 if (cmd == NULL)
1979 return -ENOMEM;
1980
1981 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1982 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1983
1984 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1985 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001986 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001987 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001988 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001989 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001990 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001991 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001992
1993 rc = mwl8k_post_cmd(hw, &cmd->header);
1994
1995 if (!rc) {
1996 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1997 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001998 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001999 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002000 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002001 priv->ap_macids_supported = 0x00000000;
2002 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002003 }
2004
2005 kfree(cmd);
2006 return rc;
2007}
2008
2009/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002010 * CMD_GET_HW_SPEC (AP version).
2011 */
2012struct mwl8k_cmd_get_hw_spec_ap {
2013 struct mwl8k_cmd_pkt header;
2014 __u8 hw_rev;
2015 __u8 host_interface;
2016 __le16 num_wcb;
2017 __le16 num_mcaddrs;
2018 __u8 perm_addr[ETH_ALEN];
2019 __le16 region_code;
2020 __le16 num_antenna;
2021 __le32 fw_rev;
2022 __le32 wcbbase0;
2023 __le32 rxwrptr;
2024 __le32 rxrdptr;
2025 __le32 ps_cookie;
2026 __le32 wcbbase1;
2027 __le32 wcbbase2;
2028 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002029 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002030 __le32 caps;
2031 __le32 num_of_ampdu_queues;
2032 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002033} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002034
2035static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2036{
2037 struct mwl8k_priv *priv = hw->priv;
2038 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002039 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002040 u32 api_version;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002041
2042 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2043 if (cmd == NULL)
2044 return -ENOMEM;
2045
2046 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2047 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2048
2049 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2050 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2051
2052 rc = mwl8k_post_cmd(hw, &cmd->header);
2053
2054 if (!rc) {
2055 int off;
2056
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002057 api_version = le32_to_cpu(cmd->fw_api_version);
2058 if (priv->device_info->fw_api_ap != api_version) {
2059 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2060 " Expected %d got %d.\n", MWL8K_NAME,
2061 priv->device_info->part_name,
2062 priv->device_info->fw_api_ap,
2063 api_version);
2064 rc = -EINVAL;
2065 goto done;
2066 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002067 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2068 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2069 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2070 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002071 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002072 priv->ap_macids_supported = 0x000000ff;
2073 priv->sta_macids_supported = 0x00000000;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002074 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2075 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2076 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2077 " but we only support %d.\n",
2078 priv->num_ampdu_queues,
2079 MWL8K_MAX_AMPDU_QUEUES);
2080 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2081 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002082 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002083 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002084
2085 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002086 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002087
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002088 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2089 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2090 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2091 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002092
2093 for (i = 0; i < priv->num_ampdu_queues; i++)
2094 priv->txq_offset[i + MWL8K_TX_QUEUES] =
2095 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002096 }
2097
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002098done:
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002099 kfree(cmd);
2100 return rc;
2101}
2102
2103/*
2104 * CMD_SET_HW_SPEC.
2105 */
2106struct mwl8k_cmd_set_hw_spec {
2107 struct mwl8k_cmd_pkt header;
2108 __u8 hw_rev;
2109 __u8 host_interface;
2110 __le16 num_mcaddrs;
2111 __u8 perm_addr[ETH_ALEN];
2112 __le16 region_code;
2113 __le32 fw_rev;
2114 __le32 ps_cookie;
2115 __le32 caps;
2116 __le32 rx_queue_ptr;
2117 __le32 num_tx_queues;
2118 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
2119 __le32 flags;
2120 __le32 num_tx_desc_per_queue;
2121 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002122} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002123
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002124/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2125 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2126 * the packets that are queued for more than 500ms, will be dropped in the
2127 * hardware. This helps minimizing the issues caused due to head-of-line
2128 * blocking where a slow client can hog the bandwidth and affect traffic to a
2129 * faster client.
2130 */
2131#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002132#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2133#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2134#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002135
2136static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2137{
2138 struct mwl8k_priv *priv = hw->priv;
2139 struct mwl8k_cmd_set_hw_spec *cmd;
2140 int rc;
2141 int i;
2142
2143 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2144 if (cmd == NULL)
2145 return -ENOMEM;
2146
2147 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2148 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2149
2150 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2151 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2152 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002153
2154 /*
2155 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2156 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2157 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2158 * priority is interpreted the right way in firmware.
2159 */
2160 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
2161 int j = MWL8K_TX_QUEUES - 1 - i;
2162 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2163 }
2164
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002165 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2166 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2167 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002168 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2169 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2170
2171 rc = mwl8k_post_cmd(hw, &cmd->header);
2172 kfree(cmd);
2173
2174 return rc;
2175}
2176
2177/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002178 * CMD_MAC_MULTICAST_ADR.
2179 */
2180struct mwl8k_cmd_mac_multicast_adr {
2181 struct mwl8k_cmd_pkt header;
2182 __le16 action;
2183 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002184 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002185};
2186
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002187#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2188#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2189#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2190#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002191
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002192static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002193__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002194 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002195{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002196 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002197 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002198 int size;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002199 int mc_count = 0;
2200
2201 if (mc_list)
2202 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002203
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002204 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002205 allmulti = 1;
2206 mc_count = 0;
2207 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002208
2209 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2210
2211 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002212 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002213 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002214
2215 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2216 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002217 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2218 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002219
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002220 if (allmulti) {
2221 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2222 } else if (mc_count) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00002223 struct netdev_hw_addr *ha;
2224 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002225
2226 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2227 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad2010-04-01 21:22:57 +00002228 netdev_hw_addr_list_for_each(ha, mc_list) {
2229 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002230 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002231 }
2232
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002233 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002234}
2235
2236/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002237 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002238 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002239struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002240 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002241 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002242} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002243
2244#define MWL8K_STAT_ACK_FAILURE 9
2245#define MWL8K_STAT_RTS_FAILURE 12
2246#define MWL8K_STAT_FCS_ERROR 24
2247#define MWL8K_STAT_RTS_SUCCESS 11
2248
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002249static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2250 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002251{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002252 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002253 int rc;
2254
2255 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2256 if (cmd == NULL)
2257 return -ENOMEM;
2258
2259 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2260 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002261
2262 rc = mwl8k_post_cmd(hw, &cmd->header);
2263 if (!rc) {
2264 stats->dot11ACKFailureCount =
2265 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2266 stats->dot11RTSFailureCount =
2267 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2268 stats->dot11FCSErrorCount =
2269 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2270 stats->dot11RTSSuccessCount =
2271 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2272 }
2273 kfree(cmd);
2274
2275 return rc;
2276}
2277
2278/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002279 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002280 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002281struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002282 struct mwl8k_cmd_pkt header;
2283 __le16 action;
2284 __le16 control;
2285 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002286} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002287
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002288static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002289mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002290{
2291 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002292 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002293 int rc;
2294
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002295 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002296 return 0;
2297
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002298 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2299 if (cmd == NULL)
2300 return -ENOMEM;
2301
2302 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2303 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2304 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002305 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002306 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2307
2308 rc = mwl8k_post_cmd(hw, &cmd->header);
2309 kfree(cmd);
2310
2311 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002312 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002313
2314 return rc;
2315}
2316
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002317static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002318{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002319 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002320}
2321
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002322static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002323{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002324 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002325}
2326
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002327static int
2328mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2329{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002330 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002331
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002332 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002333
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002334 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002335}
2336
2337/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002338 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002339 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002340#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002341
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002342struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002343 struct mwl8k_cmd_pkt header;
2344 __le16 action;
2345 __le16 support_level;
2346 __le16 current_level;
2347 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002348 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002349} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002350
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002351static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002352{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002353 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002354 int rc;
2355
2356 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2357 if (cmd == NULL)
2358 return -ENOMEM;
2359
2360 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2361 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2362 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2363 cmd->support_level = cpu_to_le16(dBm);
2364
2365 rc = mwl8k_post_cmd(hw, &cmd->header);
2366 kfree(cmd);
2367
2368 return rc;
2369}
2370
2371/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002372 * CMD_TX_POWER.
2373 */
2374#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2375
2376struct mwl8k_cmd_tx_power {
2377 struct mwl8k_cmd_pkt header;
2378 __le16 action;
2379 __le16 band;
2380 __le16 channel;
2381 __le16 bw;
2382 __le16 sub_ch;
2383 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2384} __attribute__((packed));
2385
2386static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2387 struct ieee80211_conf *conf,
2388 unsigned short pwr)
2389{
2390 struct ieee80211_channel *channel = conf->channel;
2391 struct mwl8k_cmd_tx_power *cmd;
2392 int rc;
2393 int i;
2394
2395 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2396 if (cmd == NULL)
2397 return -ENOMEM;
2398
2399 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2400 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2401 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2402
2403 if (channel->band == IEEE80211_BAND_2GHZ)
2404 cmd->band = cpu_to_le16(0x1);
2405 else if (channel->band == IEEE80211_BAND_5GHZ)
2406 cmd->band = cpu_to_le16(0x4);
2407
2408 cmd->channel = channel->hw_value;
2409
2410 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2411 conf->channel_type == NL80211_CHAN_HT20) {
2412 cmd->bw = cpu_to_le16(0x2);
2413 } else {
2414 cmd->bw = cpu_to_le16(0x4);
2415 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2416 cmd->sub_ch = cpu_to_le16(0x3);
2417 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2418 cmd->sub_ch = cpu_to_le16(0x1);
2419 }
2420
2421 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2422 cmd->power_level_list[i] = cpu_to_le16(pwr);
2423
2424 rc = mwl8k_post_cmd(hw, &cmd->header);
2425 kfree(cmd);
2426
2427 return rc;
2428}
2429
2430/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002431 * CMD_RF_ANTENNA.
2432 */
2433struct mwl8k_cmd_rf_antenna {
2434 struct mwl8k_cmd_pkt header;
2435 __le16 antenna;
2436 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002437} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002438
2439#define MWL8K_RF_ANTENNA_RX 1
2440#define MWL8K_RF_ANTENNA_TX 2
2441
2442static int
2443mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2444{
2445 struct mwl8k_cmd_rf_antenna *cmd;
2446 int rc;
2447
2448 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2449 if (cmd == NULL)
2450 return -ENOMEM;
2451
2452 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2453 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2454 cmd->antenna = cpu_to_le16(antenna);
2455 cmd->mode = cpu_to_le16(mask);
2456
2457 rc = mwl8k_post_cmd(hw, &cmd->header);
2458 kfree(cmd);
2459
2460 return rc;
2461}
2462
2463/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002464 * CMD_SET_BEACON.
2465 */
2466struct mwl8k_cmd_set_beacon {
2467 struct mwl8k_cmd_pkt header;
2468 __le16 beacon_len;
2469 __u8 beacon[0];
2470};
2471
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002472static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2473 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002474{
2475 struct mwl8k_cmd_set_beacon *cmd;
2476 int rc;
2477
2478 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2479 if (cmd == NULL)
2480 return -ENOMEM;
2481
2482 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2483 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2484 cmd->beacon_len = cpu_to_le16(len);
2485 memcpy(cmd->beacon, beacon, len);
2486
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002487 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002488 kfree(cmd);
2489
2490 return rc;
2491}
2492
2493/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002494 * CMD_SET_PRE_SCAN.
2495 */
2496struct mwl8k_cmd_set_pre_scan {
2497 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002498} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002499
2500static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2501{
2502 struct mwl8k_cmd_set_pre_scan *cmd;
2503 int rc;
2504
2505 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2506 if (cmd == NULL)
2507 return -ENOMEM;
2508
2509 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2510 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2511
2512 rc = mwl8k_post_cmd(hw, &cmd->header);
2513 kfree(cmd);
2514
2515 return rc;
2516}
2517
2518/*
2519 * CMD_SET_POST_SCAN.
2520 */
2521struct mwl8k_cmd_set_post_scan {
2522 struct mwl8k_cmd_pkt header;
2523 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002524 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002525} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002526
2527static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002528mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002529{
2530 struct mwl8k_cmd_set_post_scan *cmd;
2531 int rc;
2532
2533 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2534 if (cmd == NULL)
2535 return -ENOMEM;
2536
2537 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2538 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2539 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002540 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002541
2542 rc = mwl8k_post_cmd(hw, &cmd->header);
2543 kfree(cmd);
2544
2545 return rc;
2546}
2547
2548/*
2549 * CMD_SET_RF_CHANNEL.
2550 */
2551struct mwl8k_cmd_set_rf_channel {
2552 struct mwl8k_cmd_pkt header;
2553 __le16 action;
2554 __u8 current_channel;
2555 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002556} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002557
2558static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002559 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002560{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002561 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002562 struct mwl8k_cmd_set_rf_channel *cmd;
2563 int rc;
2564
2565 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2566 if (cmd == NULL)
2567 return -ENOMEM;
2568
2569 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2570 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2571 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2572 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002573
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002574 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002575 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002576 else if (channel->band == IEEE80211_BAND_5GHZ)
2577 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002578
2579 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2580 conf->channel_type == NL80211_CHAN_HT20)
2581 cmd->channel_flags |= cpu_to_le32(0x00000080);
2582 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2583 cmd->channel_flags |= cpu_to_le32(0x000001900);
2584 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2585 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002586
2587 rc = mwl8k_post_cmd(hw, &cmd->header);
2588 kfree(cmd);
2589
2590 return rc;
2591}
2592
2593/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002594 * CMD_SET_AID.
2595 */
2596#define MWL8K_FRAME_PROT_DISABLED 0x00
2597#define MWL8K_FRAME_PROT_11G 0x07
2598#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2599#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2600
2601struct mwl8k_cmd_update_set_aid {
2602 struct mwl8k_cmd_pkt header;
2603 __le16 aid;
2604
2605 /* AP's MAC address (BSSID) */
2606 __u8 bssid[ETH_ALEN];
2607 __le16 protection_mode;
2608 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002609} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002610
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002611static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2612{
2613 int i;
2614 int j;
2615
2616 /*
2617 * Clear nonstandard rates 4 and 13.
2618 */
2619 mask &= 0x1fef;
2620
2621 for (i = 0, j = 0; i < 14; i++) {
2622 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002623 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002624 }
2625}
2626
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002627static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002628mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2629 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002630{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002631 struct mwl8k_cmd_update_set_aid *cmd;
2632 u16 prot_mode;
2633 int rc;
2634
2635 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2636 if (cmd == NULL)
2637 return -ENOMEM;
2638
2639 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2640 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002641 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002642 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002643
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002644 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002645 prot_mode = MWL8K_FRAME_PROT_11G;
2646 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002647 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002648 IEEE80211_HT_OP_MODE_PROTECTION) {
2649 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2650 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2651 break;
2652 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2653 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2654 break;
2655 default:
2656 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2657 break;
2658 }
2659 }
2660 cmd->protection_mode = cpu_to_le16(prot_mode);
2661
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002662 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002663
2664 rc = mwl8k_post_cmd(hw, &cmd->header);
2665 kfree(cmd);
2666
2667 return rc;
2668}
2669
2670/*
2671 * CMD_SET_RATE.
2672 */
2673struct mwl8k_cmd_set_rate {
2674 struct mwl8k_cmd_pkt header;
2675 __u8 legacy_rates[14];
2676
2677 /* Bitmap for supported MCS codes. */
2678 __u8 mcs_set[16];
2679 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002680} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002681
2682static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002683mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002684 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002685{
2686 struct mwl8k_cmd_set_rate *cmd;
2687 int rc;
2688
2689 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2690 if (cmd == NULL)
2691 return -ENOMEM;
2692
2693 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2694 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002695 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002696 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002697
2698 rc = mwl8k_post_cmd(hw, &cmd->header);
2699 kfree(cmd);
2700
2701 return rc;
2702}
2703
2704/*
2705 * CMD_FINALIZE_JOIN.
2706 */
2707#define MWL8K_FJ_BEACON_MAXLEN 128
2708
2709struct mwl8k_cmd_finalize_join {
2710 struct mwl8k_cmd_pkt header;
2711 __le32 sleep_interval; /* Number of beacon periods to sleep */
2712 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002713} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002714
2715static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2716 int framelen, int dtim)
2717{
2718 struct mwl8k_cmd_finalize_join *cmd;
2719 struct ieee80211_mgmt *payload = frame;
2720 int payload_len;
2721 int rc;
2722
2723 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2724 if (cmd == NULL)
2725 return -ENOMEM;
2726
2727 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2728 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2729 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2730
2731 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2732 if (payload_len < 0)
2733 payload_len = 0;
2734 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2735 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2736
2737 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2738
2739 rc = mwl8k_post_cmd(hw, &cmd->header);
2740 kfree(cmd);
2741
2742 return rc;
2743}
2744
2745/*
2746 * CMD_SET_RTS_THRESHOLD.
2747 */
2748struct mwl8k_cmd_set_rts_threshold {
2749 struct mwl8k_cmd_pkt header;
2750 __le16 action;
2751 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002752} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002753
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002754static int
2755mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002756{
2757 struct mwl8k_cmd_set_rts_threshold *cmd;
2758 int rc;
2759
2760 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2761 if (cmd == NULL)
2762 return -ENOMEM;
2763
2764 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2765 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002766 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2767 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002768
2769 rc = mwl8k_post_cmd(hw, &cmd->header);
2770 kfree(cmd);
2771
2772 return rc;
2773}
2774
2775/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002776 * CMD_SET_SLOT.
2777 */
2778struct mwl8k_cmd_set_slot {
2779 struct mwl8k_cmd_pkt header;
2780 __le16 action;
2781 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002782} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002783
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002784static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002785{
2786 struct mwl8k_cmd_set_slot *cmd;
2787 int rc;
2788
2789 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2790 if (cmd == NULL)
2791 return -ENOMEM;
2792
2793 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2794 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2795 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002796 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002797
2798 rc = mwl8k_post_cmd(hw, &cmd->header);
2799 kfree(cmd);
2800
2801 return rc;
2802}
2803
2804/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002805 * CMD_SET_EDCA_PARAMS.
2806 */
2807struct mwl8k_cmd_set_edca_params {
2808 struct mwl8k_cmd_pkt header;
2809
2810 /* See MWL8K_SET_EDCA_XXX below */
2811 __le16 action;
2812
2813 /* TX opportunity in units of 32 us */
2814 __le16 txop;
2815
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002816 union {
2817 struct {
2818 /* Log exponent of max contention period: 0...15 */
2819 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002820
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002821 /* Log exponent of min contention period: 0...15 */
2822 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002823
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002824 /* Adaptive interframe spacing in units of 32us */
2825 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002826
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002827 /* TX queue to configure */
2828 __u8 txq;
2829 } ap;
2830 struct {
2831 /* Log exponent of max contention period: 0...15 */
2832 __u8 log_cw_max;
2833
2834 /* Log exponent of min contention period: 0...15 */
2835 __u8 log_cw_min;
2836
2837 /* Adaptive interframe spacing in units of 32us */
2838 __u8 aifs;
2839
2840 /* TX queue to configure */
2841 __u8 txq;
2842 } sta;
2843 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002844} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002845
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002846#define MWL8K_SET_EDCA_CW 0x01
2847#define MWL8K_SET_EDCA_TXOP 0x02
2848#define MWL8K_SET_EDCA_AIFS 0x04
2849
2850#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2851 MWL8K_SET_EDCA_TXOP | \
2852 MWL8K_SET_EDCA_AIFS)
2853
2854static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002855mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2856 __u16 cw_min, __u16 cw_max,
2857 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002858{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002859 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002860 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002861 int rc;
2862
2863 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2864 if (cmd == NULL)
2865 return -ENOMEM;
2866
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002867 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2868 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002869 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2870 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002871 if (priv->ap_fw) {
2872 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2873 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2874 cmd->ap.aifs = aifs;
2875 cmd->ap.txq = qnum;
2876 } else {
2877 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2878 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2879 cmd->sta.aifs = aifs;
2880 cmd->sta.txq = qnum;
2881 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002882
2883 rc = mwl8k_post_cmd(hw, &cmd->header);
2884 kfree(cmd);
2885
2886 return rc;
2887}
2888
2889/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002890 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002891 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002892struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002893 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002894 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002895} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002896
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002897static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002898{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002899 struct mwl8k_priv *priv = hw->priv;
2900 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002901 int rc;
2902
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002903 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2904 if (cmd == NULL)
2905 return -ENOMEM;
2906
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002907 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002908 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002909 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002910
2911 rc = mwl8k_post_cmd(hw, &cmd->header);
2912 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002913
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002914 if (!rc)
2915 priv->wmm_enabled = enable;
2916
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002917 return rc;
2918}
2919
2920/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002921 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002922 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002923struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002924 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002925 __le32 action;
2926 __u8 rx_antenna_map;
2927 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002928} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002929
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002930static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002931{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002932 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002933 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002934
2935 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2936 if (cmd == NULL)
2937 return -ENOMEM;
2938
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002939 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002940 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002941 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2942 cmd->rx_antenna_map = rx;
2943 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002944
2945 rc = mwl8k_post_cmd(hw, &cmd->header);
2946 kfree(cmd);
2947
2948 return rc;
2949}
2950
2951/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002952 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002953 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002954struct mwl8k_cmd_use_fixed_rate_sta {
2955 struct mwl8k_cmd_pkt header;
2956 __le32 action;
2957 __le32 allow_rate_drop;
2958 __le32 num_rates;
2959 struct {
2960 __le32 is_ht_rate;
2961 __le32 enable_retry;
2962 __le32 rate;
2963 __le32 retry_count;
2964 } rate_entry[8];
2965 __le32 rate_type;
2966 __le32 reserved1;
2967 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002968} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002969
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002970#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002971#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002972
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002973static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002974{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002975 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002976 int rc;
2977
2978 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2979 if (cmd == NULL)
2980 return -ENOMEM;
2981
2982 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2983 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002984 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2985 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002986
2987 rc = mwl8k_post_cmd(hw, &cmd->header);
2988 kfree(cmd);
2989
2990 return rc;
2991}
2992
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002993/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002994 * CMD_USE_FIXED_RATE (AP version).
2995 */
2996struct mwl8k_cmd_use_fixed_rate_ap {
2997 struct mwl8k_cmd_pkt header;
2998 __le32 action;
2999 __le32 allow_rate_drop;
3000 __le32 num_rates;
3001 struct mwl8k_rate_entry_ap {
3002 __le32 is_ht_rate;
3003 __le32 enable_retry;
3004 __le32 rate;
3005 __le32 retry_count;
3006 } rate_entry[4];
3007 u8 multicast_rate;
3008 u8 multicast_rate_type;
3009 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003010} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003011
3012static int
3013mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3014{
3015 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3016 int rc;
3017
3018 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3019 if (cmd == NULL)
3020 return -ENOMEM;
3021
3022 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3023 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3024 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3025 cmd->multicast_rate = mcast;
3026 cmd->management_rate = mgmt;
3027
3028 rc = mwl8k_post_cmd(hw, &cmd->header);
3029 kfree(cmd);
3030
3031 return rc;
3032}
3033
3034/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003035 * CMD_ENABLE_SNIFFER.
3036 */
3037struct mwl8k_cmd_enable_sniffer {
3038 struct mwl8k_cmd_pkt header;
3039 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003040} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003041
3042static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3043{
3044 struct mwl8k_cmd_enable_sniffer *cmd;
3045 int rc;
3046
3047 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3048 if (cmd == NULL)
3049 return -ENOMEM;
3050
3051 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3052 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3053 cmd->action = cpu_to_le32(!!enable);
3054
3055 rc = mwl8k_post_cmd(hw, &cmd->header);
3056 kfree(cmd);
3057
3058 return rc;
3059}
3060
3061/*
3062 * CMD_SET_MAC_ADDR.
3063 */
3064struct mwl8k_cmd_set_mac_addr {
3065 struct mwl8k_cmd_pkt header;
3066 union {
3067 struct {
3068 __le16 mac_type;
3069 __u8 mac_addr[ETH_ALEN];
3070 } mbss;
3071 __u8 mac_addr[ETH_ALEN];
3072 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003073} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003074
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003075#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3076#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3077#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3078#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003079
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003080static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3081 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003082{
3083 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003084 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003085 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003086 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003087 int rc;
3088
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003089 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3090 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3091 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3092 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3093 else
3094 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3095 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3096 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3097 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3098 else
3099 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3100 }
3101
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003102 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3103 if (cmd == NULL)
3104 return -ENOMEM;
3105
3106 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3107 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3108 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003109 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003110 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3111 } else {
3112 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3113 }
3114
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003115 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003116 kfree(cmd);
3117
3118 return rc;
3119}
3120
3121/*
3122 * CMD_SET_RATEADAPT_MODE.
3123 */
3124struct mwl8k_cmd_set_rate_adapt_mode {
3125 struct mwl8k_cmd_pkt header;
3126 __le16 action;
3127 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003128} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003129
3130static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3131{
3132 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3133 int rc;
3134
3135 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3136 if (cmd == NULL)
3137 return -ENOMEM;
3138
3139 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3140 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3141 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3142 cmd->mode = cpu_to_le16(mode);
3143
3144 rc = mwl8k_post_cmd(hw, &cmd->header);
3145 kfree(cmd);
3146
3147 return rc;
3148}
3149
3150/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003151 * CMD_BSS_START.
3152 */
3153struct mwl8k_cmd_bss_start {
3154 struct mwl8k_cmd_pkt header;
3155 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003156} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003157
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003158static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3159 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003160{
3161 struct mwl8k_cmd_bss_start *cmd;
3162 int rc;
3163
3164 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3165 if (cmd == NULL)
3166 return -ENOMEM;
3167
3168 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3169 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3170 cmd->enable = cpu_to_le32(enable);
3171
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003172 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003173 kfree(cmd);
3174
3175 return rc;
3176}
3177
3178/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003179 * CMD_SET_NEW_STN.
3180 */
3181struct mwl8k_cmd_set_new_stn {
3182 struct mwl8k_cmd_pkt header;
3183 __le16 aid;
3184 __u8 mac_addr[6];
3185 __le16 stn_id;
3186 __le16 action;
3187 __le16 rsvd;
3188 __le32 legacy_rates;
3189 __u8 ht_rates[4];
3190 __le16 cap_info;
3191 __le16 ht_capabilities_info;
3192 __u8 mac_ht_param_info;
3193 __u8 rev;
3194 __u8 control_channel;
3195 __u8 add_channel;
3196 __le16 op_mode;
3197 __le16 stbc;
3198 __u8 add_qos_info;
3199 __u8 is_qos_sta;
3200 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003201} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003202
3203#define MWL8K_STA_ACTION_ADD 0
3204#define MWL8K_STA_ACTION_REMOVE 2
3205
3206static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3207 struct ieee80211_vif *vif,
3208 struct ieee80211_sta *sta)
3209{
3210 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003211 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003212 int rc;
3213
3214 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3215 if (cmd == NULL)
3216 return -ENOMEM;
3217
3218 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3219 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3220 cmd->aid = cpu_to_le16(sta->aid);
3221 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3222 cmd->stn_id = cpu_to_le16(sta->aid);
3223 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003224 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3225 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3226 else
3227 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3228 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003229 if (sta->ht_cap.ht_supported) {
3230 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3231 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3232 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3233 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3234 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3235 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3236 ((sta->ht_cap.ampdu_density & 7) << 2);
3237 cmd->is_qos_sta = 1;
3238 }
3239
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003240 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003241 kfree(cmd);
3242
3243 return rc;
3244}
3245
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003246static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3247 struct ieee80211_vif *vif)
3248{
3249 struct mwl8k_cmd_set_new_stn *cmd;
3250 int rc;
3251
3252 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3253 if (cmd == NULL)
3254 return -ENOMEM;
3255
3256 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3257 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3258 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3259
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003260 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003261 kfree(cmd);
3262
3263 return rc;
3264}
3265
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003266static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3267 struct ieee80211_vif *vif, u8 *addr)
3268{
3269 struct mwl8k_cmd_set_new_stn *cmd;
3270 int rc;
3271
3272 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3273 if (cmd == NULL)
3274 return -ENOMEM;
3275
3276 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3277 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3278 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3279 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3280
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003281 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003282 kfree(cmd);
3283
3284 return rc;
3285}
3286
3287/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003288 * CMD_UPDATE_ENCRYPTION.
3289 */
3290
3291#define MAX_ENCR_KEY_LENGTH 16
3292#define MIC_KEY_LENGTH 8
3293
3294struct mwl8k_cmd_update_encryption {
3295 struct mwl8k_cmd_pkt header;
3296
3297 __le32 action;
3298 __le32 reserved;
3299 __u8 mac_addr[6];
3300 __u8 encr_type;
3301
3302} __attribute__((packed));
3303
3304struct mwl8k_cmd_set_key {
3305 struct mwl8k_cmd_pkt header;
3306
3307 __le32 action;
3308 __le32 reserved;
3309 __le16 length;
3310 __le16 key_type_id;
3311 __le32 key_info;
3312 __le32 key_id;
3313 __le16 key_len;
3314 __u8 key_material[MAX_ENCR_KEY_LENGTH];
3315 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
3316 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
3317 __le16 tkip_rsc_low;
3318 __le32 tkip_rsc_high;
3319 __le16 tkip_tsc_low;
3320 __le32 tkip_tsc_high;
3321 __u8 mac_addr[6];
3322} __attribute__((packed));
3323
3324enum {
3325 MWL8K_ENCR_ENABLE,
3326 MWL8K_ENCR_SET_KEY,
3327 MWL8K_ENCR_REMOVE_KEY,
3328 MWL8K_ENCR_SET_GROUP_KEY,
3329};
3330
3331#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
3332#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
3333#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
3334#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
3335#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
3336
3337enum {
3338 MWL8K_ALG_WEP,
3339 MWL8K_ALG_TKIP,
3340 MWL8K_ALG_CCMP,
3341};
3342
3343#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
3344#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
3345#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
3346#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
3347#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
3348
3349static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
3350 struct ieee80211_vif *vif,
3351 u8 *addr,
3352 u8 encr_type)
3353{
3354 struct mwl8k_cmd_update_encryption *cmd;
3355 int rc;
3356
3357 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3358 if (cmd == NULL)
3359 return -ENOMEM;
3360
3361 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3362 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3363 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
3364 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3365 cmd->encr_type = encr_type;
3366
3367 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3368 kfree(cmd);
3369
3370 return rc;
3371}
3372
3373static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
3374 u8 *addr,
3375 struct ieee80211_key_conf *key)
3376{
3377 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3378 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3379 cmd->length = cpu_to_le16(sizeof(*cmd) -
3380 offsetof(struct mwl8k_cmd_set_key, length));
3381 cmd->key_id = cpu_to_le32(key->keyidx);
3382 cmd->key_len = cpu_to_le16(key->keylen);
3383 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3384
3385 switch (key->cipher) {
3386 case WLAN_CIPHER_SUITE_WEP40:
3387 case WLAN_CIPHER_SUITE_WEP104:
3388 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
3389 if (key->keyidx == 0)
3390 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
3391
3392 break;
3393 case WLAN_CIPHER_SUITE_TKIP:
3394 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
3395 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3396 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3397 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3398 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
3399 | MWL8K_KEY_FLAG_TSC_VALID);
3400 break;
3401 case WLAN_CIPHER_SUITE_CCMP:
3402 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
3403 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3404 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3405 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3406 break;
3407 default:
3408 return -ENOTSUPP;
3409 }
3410
3411 return 0;
3412}
3413
3414static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
3415 struct ieee80211_vif *vif,
3416 u8 *addr,
3417 struct ieee80211_key_conf *key)
3418{
3419 struct mwl8k_cmd_set_key *cmd;
3420 int rc;
3421 int keymlen;
3422 u32 action;
3423 u8 idx;
3424 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3425
3426 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3427 if (cmd == NULL)
3428 return -ENOMEM;
3429
3430 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3431 if (rc < 0)
3432 goto done;
3433
3434 idx = key->keyidx;
3435
3436 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3437 action = MWL8K_ENCR_SET_KEY;
3438 else
3439 action = MWL8K_ENCR_SET_GROUP_KEY;
3440
3441 switch (key->cipher) {
3442 case WLAN_CIPHER_SUITE_WEP40:
3443 case WLAN_CIPHER_SUITE_WEP104:
3444 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
3445 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
3446 sizeof(*key) + key->keylen);
3447 mwl8k_vif->wep_key_conf[idx].enabled = 1;
3448 }
3449
3450 keymlen = 0;
3451 action = MWL8K_ENCR_SET_KEY;
3452 break;
3453 case WLAN_CIPHER_SUITE_TKIP:
3454 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
3455 break;
3456 case WLAN_CIPHER_SUITE_CCMP:
3457 keymlen = key->keylen;
3458 break;
3459 default:
3460 rc = -ENOTSUPP;
3461 goto done;
3462 }
3463
3464 memcpy(cmd->key_material, key->key, keymlen);
3465 cmd->action = cpu_to_le32(action);
3466
3467 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3468done:
3469 kfree(cmd);
3470
3471 return rc;
3472}
3473
3474static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
3475 struct ieee80211_vif *vif,
3476 u8 *addr,
3477 struct ieee80211_key_conf *key)
3478{
3479 struct mwl8k_cmd_set_key *cmd;
3480 int rc;
3481 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3482
3483 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3484 if (cmd == NULL)
3485 return -ENOMEM;
3486
3487 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3488 if (rc < 0)
3489 goto done;
3490
3491 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3492 WLAN_CIPHER_SUITE_WEP104)
3493 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
3494
3495 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
3496
3497 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3498done:
3499 kfree(cmd);
3500
3501 return rc;
3502}
3503
3504static int mwl8k_set_key(struct ieee80211_hw *hw,
3505 enum set_key_cmd cmd_param,
3506 struct ieee80211_vif *vif,
3507 struct ieee80211_sta *sta,
3508 struct ieee80211_key_conf *key)
3509{
3510 int rc = 0;
3511 u8 encr_type;
3512 u8 *addr;
3513 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3514
3515 if (vif->type == NL80211_IFTYPE_STATION)
3516 return -EOPNOTSUPP;
3517
3518 if (sta == NULL)
3519 addr = hw->wiphy->perm_addr;
3520 else
3521 addr = sta->addr;
3522
3523 if (cmd_param == SET_KEY) {
3524 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3525 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
3526 if (rc)
3527 goto out;
3528
3529 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
3530 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
3531 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
3532 else
3533 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
3534
3535 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
3536 encr_type);
3537 if (rc)
3538 goto out;
3539
3540 mwl8k_vif->is_hw_crypto_enabled = true;
3541
3542 } else {
3543 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
3544
3545 if (rc)
3546 goto out;
3547
3548 mwl8k_vif->is_hw_crypto_enabled = false;
3549
3550 }
3551out:
3552 return rc;
3553}
3554
3555/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003556 * CMD_UPDATE_STADB.
3557 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003558struct ewc_ht_info {
3559 __le16 control1;
3560 __le16 control2;
3561 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003562} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003563
3564struct peer_capability_info {
3565 /* Peer type - AP vs. STA. */
3566 __u8 peer_type;
3567
3568 /* Basic 802.11 capabilities from assoc resp. */
3569 __le16 basic_caps;
3570
3571 /* Set if peer supports 802.11n high throughput (HT). */
3572 __u8 ht_support;
3573
3574 /* Valid if HT is supported. */
3575 __le16 ht_caps;
3576 __u8 extended_ht_caps;
3577 struct ewc_ht_info ewc_info;
3578
3579 /* Legacy rate table. Intersection of our rates and peer rates. */
3580 __u8 legacy_rates[12];
3581
3582 /* HT rate table. Intersection of our rates and peer rates. */
3583 __u8 ht_rates[16];
3584 __u8 pad[16];
3585
3586 /* If set, interoperability mode, no proprietary extensions. */
3587 __u8 interop;
3588 __u8 pad2;
3589 __u8 station_id;
3590 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003591} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003592
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003593struct mwl8k_cmd_update_stadb {
3594 struct mwl8k_cmd_pkt header;
3595
3596 /* See STADB_ACTION_TYPE */
3597 __le32 action;
3598
3599 /* Peer MAC address */
3600 __u8 peer_addr[ETH_ALEN];
3601
3602 __le32 reserved;
3603
3604 /* Peer info - valid during add/update. */
3605 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003606} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003607
Lennert Buytenheka6804002010-01-04 21:55:42 +01003608#define MWL8K_STA_DB_MODIFY_ENTRY 1
3609#define MWL8K_STA_DB_DEL_ENTRY 2
3610
3611/* Peer Entry flags - used to define the type of the peer node */
3612#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3613
3614static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003615 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003616 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003617{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003618 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003619 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003620 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003621 int rc;
3622
3623 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3624 if (cmd == NULL)
3625 return -ENOMEM;
3626
3627 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3628 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003629 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003630 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003631
Lennert Buytenheka6804002010-01-04 21:55:42 +01003632 p = &cmd->peer_info;
3633 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3634 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003635 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003636 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003637 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3638 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003639 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3640 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3641 else
3642 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3643 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003644 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003645 p->interop = 1;
3646 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003647
Lennert Buytenheka6804002010-01-04 21:55:42 +01003648 rc = mwl8k_post_cmd(hw, &cmd->header);
3649 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003650
Lennert Buytenheka6804002010-01-04 21:55:42 +01003651 return rc ? rc : p->station_id;
3652}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003653
Lennert Buytenheka6804002010-01-04 21:55:42 +01003654static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3655 struct ieee80211_vif *vif, u8 *addr)
3656{
3657 struct mwl8k_cmd_update_stadb *cmd;
3658 int rc;
3659
3660 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3661 if (cmd == NULL)
3662 return -ENOMEM;
3663
3664 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3665 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3666 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3667 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3668
3669 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003670 kfree(cmd);
3671
3672 return rc;
3673}
3674
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003675
3676/*
3677 * Interrupt handling.
3678 */
3679static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3680{
3681 struct ieee80211_hw *hw = dev_id;
3682 struct mwl8k_priv *priv = hw->priv;
3683 u32 status;
3684
3685 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003686 if (!status)
3687 return IRQ_NONE;
3688
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003689 if (status & MWL8K_A2H_INT_TX_DONE) {
3690 status &= ~MWL8K_A2H_INT_TX_DONE;
3691 tasklet_schedule(&priv->poll_tx_task);
3692 }
3693
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003694 if (status & MWL8K_A2H_INT_RX_READY) {
3695 status &= ~MWL8K_A2H_INT_RX_READY;
3696 tasklet_schedule(&priv->poll_rx_task);
3697 }
3698
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003699 if (status)
3700 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003701
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003702 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003703 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003704 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003705 }
3706
3707 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003708 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003709 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003710 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003711 }
3712
3713 return IRQ_HANDLED;
3714}
3715
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003716static void mwl8k_tx_poll(unsigned long data)
3717{
3718 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3719 struct mwl8k_priv *priv = hw->priv;
3720 int limit;
3721 int i;
3722
3723 limit = 32;
3724
3725 spin_lock_bh(&priv->tx_lock);
3726
3727 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3728 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3729
3730 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3731 complete(priv->tx_wait);
3732 priv->tx_wait = NULL;
3733 }
3734
3735 spin_unlock_bh(&priv->tx_lock);
3736
3737 if (limit) {
3738 writel(~MWL8K_A2H_INT_TX_DONE,
3739 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3740 } else {
3741 tasklet_schedule(&priv->poll_tx_task);
3742 }
3743}
3744
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003745static void mwl8k_rx_poll(unsigned long data)
3746{
3747 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3748 struct mwl8k_priv *priv = hw->priv;
3749 int limit;
3750
3751 limit = 32;
3752 limit -= rxq_process(hw, 0, limit);
3753 limit -= rxq_refill(hw, 0, limit);
3754
3755 if (limit) {
3756 writel(~MWL8K_A2H_INT_RX_READY,
3757 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3758 } else {
3759 tasklet_schedule(&priv->poll_rx_task);
3760 }
3761}
3762
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003763
3764/*
3765 * Core driver operations.
3766 */
Johannes Berg7bb45682011-02-24 14:42:06 +01003767static void mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003768{
3769 struct mwl8k_priv *priv = hw->priv;
3770 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003771
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003772 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003773 wiphy_debug(hw->wiphy,
3774 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003775 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003776 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003777 }
3778
Johannes Berg7bb45682011-02-24 14:42:06 +01003779 mwl8k_txq_xmit(hw, index, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003780}
3781
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003782static int mwl8k_start(struct ieee80211_hw *hw)
3783{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003784 struct mwl8k_priv *priv = hw->priv;
3785 int rc;
3786
Joe Perchesa0607fd2009-11-18 23:29:17 -08003787 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003788 IRQF_SHARED, MWL8K_NAME, hw);
3789 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07003790 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003791 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003792 }
3793
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003794 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003795 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003796 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003797
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003798 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003799 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003800
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003801 rc = mwl8k_fw_lock(hw);
3802 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003803 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003804
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003805 if (!priv->ap_fw) {
3806 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003807 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003808
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003809 if (!rc)
3810 rc = mwl8k_cmd_set_pre_scan(hw);
3811
3812 if (!rc)
3813 rc = mwl8k_cmd_set_post_scan(hw,
3814 "\x00\x00\x00\x00\x00\x00");
3815 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003816
3817 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003818 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003819
3820 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003821 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003822
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003823 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003824 }
3825
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003826 if (rc) {
3827 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3828 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003829 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003830 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003831 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003832
3833 return rc;
3834}
3835
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003836static void mwl8k_stop(struct ieee80211_hw *hw)
3837{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003838 struct mwl8k_priv *priv = hw->priv;
3839 int i;
3840
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003841 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003842
3843 ieee80211_stop_queues(hw);
3844
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003845 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003846 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003847 free_irq(priv->pdev->irq, hw);
3848
3849 /* Stop finalize join worker */
3850 cancel_work_sync(&priv->finalize_join_worker);
3851 if (priv->beacon_skb != NULL)
3852 dev_kfree_skb(priv->beacon_skb);
3853
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003854 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003855 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003856 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003857
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003858 /* Return all skbs to mac80211 */
3859 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003860 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003861}
3862
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003863static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
3864
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003865static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003866 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003867{
3868 struct mwl8k_priv *priv = hw->priv;
3869 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003870 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003871 int macid, rc;
3872 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003873
3874 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003875 * Reject interface creation if sniffer mode is active, as
3876 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003877 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003878 */
3879 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003880 wiphy_info(hw->wiphy,
3881 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003882 return -EINVAL;
3883 }
3884
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003885 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003886 switch (vif->type) {
3887 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003888 if (!priv->ap_fw && di->fw_image_ap) {
3889 /* we must load the ap fw to meet this request */
3890 if (!list_empty(&priv->vif_list))
3891 return -EBUSY;
3892 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
3893 if (rc)
3894 return rc;
3895 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003896 macids_supported = priv->ap_macids_supported;
3897 break;
3898 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003899 if (priv->ap_fw && di->fw_image_sta) {
3900 /* we must load the sta fw to meet this request */
3901 if (!list_empty(&priv->vif_list))
3902 return -EBUSY;
3903 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
3904 if (rc)
3905 return rc;
3906 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003907 macids_supported = priv->sta_macids_supported;
3908 break;
3909 default:
3910 return -EINVAL;
3911 }
3912
3913 macid = ffs(macids_supported & ~priv->macids_used);
3914 if (!macid--)
3915 return -EBUSY;
3916
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003917 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003918 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003919 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003920 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003921 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003922 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08003923 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
3924 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003925
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003926 /* Set the mac address. */
3927 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3928
3929 if (priv->ap_fw)
3930 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3931
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003932 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003933 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003934
3935 return 0;
3936}
3937
3938static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003939 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003940{
3941 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003942 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003943
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003944 if (priv->ap_fw)
3945 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3946
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003947 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003948
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003949 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003950 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003951}
3952
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003953static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003954{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003955 struct ieee80211_conf *conf = &hw->conf;
3956 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003957 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003958
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003959 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003960 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003961 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003962 }
3963
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003964 rc = mwl8k_fw_lock(hw);
3965 if (rc)
3966 return rc;
3967
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003968 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003969 if (rc)
3970 goto out;
3971
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003972 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003973 if (rc)
3974 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003975
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003976 if (conf->power_level > 18)
3977 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003978
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003979 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003980 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
3981 if (rc)
3982 goto out;
3983
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08003984 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
3985 if (rc)
3986 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
3987 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3988 if (rc)
3989 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
3990
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003991 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003992 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
3993 if (rc)
3994 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003995 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3996 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003997
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003998out:
3999 mwl8k_fw_unlock(hw);
4000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004001 return rc;
4002}
4003
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004004static void
4005mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4006 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004007{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004008 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004009 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004010 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004011 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004012
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004013 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004014 return;
4015
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004016 /*
4017 * No need to capture a beacon if we're no longer associated.
4018 */
4019 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4020 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004021
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004022 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004023 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004024 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004025 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004026 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004027
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004028 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004029
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004030 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004031 if (ap == NULL) {
4032 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004033 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004034 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004035
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004036 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4037 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4038 } else {
4039 ap_legacy_rates =
4040 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4041 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004042 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004043
4044 rcu_read_unlock();
4045 }
4046
4047 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004048 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004049 if (rc)
4050 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004051
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004052 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004053 if (rc)
4054 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004055 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004056
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004057 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004058 rc = mwl8k_set_radio_preamble(hw,
4059 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004060 if (rc)
4061 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004062 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004063
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004064 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004065 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004066 if (rc)
4067 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004068 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004069
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004070 if (vif->bss_conf.assoc &&
4071 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4072 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004073 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004074 if (rc)
4075 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004076 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004077
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004078 if (vif->bss_conf.assoc &&
4079 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004080 /*
4081 * Finalize the join. Tell rx handler to process
4082 * next beacon from our BSSID.
4083 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004084 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004085 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004086 }
4087
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004088out:
4089 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004090}
4091
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004092static void
4093mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4094 struct ieee80211_bss_conf *info, u32 changed)
4095{
4096 int rc;
4097
4098 if (mwl8k_fw_lock(hw))
4099 return;
4100
4101 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4102 rc = mwl8k_set_radio_preamble(hw,
4103 vif->bss_conf.use_short_preamble);
4104 if (rc)
4105 goto out;
4106 }
4107
4108 if (changed & BSS_CHANGED_BASIC_RATES) {
4109 int idx;
4110 int rate;
4111
4112 /*
4113 * Use lowest supported basic rate for multicasts
4114 * and management frames (such as probe responses --
4115 * beacons will always go out at 1 Mb/s).
4116 */
4117 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004118 if (idx)
4119 idx--;
4120
4121 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4122 rate = mwl8k_rates_24[idx].hw_value;
4123 else
4124 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004125
4126 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4127 }
4128
4129 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4130 struct sk_buff *skb;
4131
4132 skb = ieee80211_beacon_get(hw, vif);
4133 if (skb != NULL) {
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004134 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004135 kfree_skb(skb);
4136 }
4137 }
4138
4139 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004140 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004141
4142out:
4143 mwl8k_fw_unlock(hw);
4144}
4145
4146static void
4147mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4148 struct ieee80211_bss_conf *info, u32 changed)
4149{
4150 struct mwl8k_priv *priv = hw->priv;
4151
4152 if (!priv->ap_fw)
4153 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
4154 else
4155 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
4156}
4157
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004158static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00004159 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004160{
4161 struct mwl8k_cmd_pkt *cmd;
4162
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004163 /*
4164 * Synthesize and return a command packet that programs the
4165 * hardware multicast address filter. At this point we don't
4166 * know whether FIF_ALLMULTI is being requested, but if it is,
4167 * we'll end up throwing this packet away and creating a new
4168 * one in mwl8k_configure_filter().
4169 */
Jiri Pirko22bedad2010-04-01 21:22:57 +00004170 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004171
4172 return (unsigned long)cmd;
4173}
4174
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004175static int
4176mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
4177 unsigned int changed_flags,
4178 unsigned int *total_flags)
4179{
4180 struct mwl8k_priv *priv = hw->priv;
4181
4182 /*
4183 * Hardware sniffer mode is mutually exclusive with STA
4184 * operation, so refuse to enable sniffer mode if a STA
4185 * interface is active.
4186 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004187 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004188 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07004189 wiphy_info(hw->wiphy,
4190 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004191 return 0;
4192 }
4193
4194 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004195 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004196 return 0;
4197 priv->sniffer_enabled = true;
4198 }
4199
4200 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
4201 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
4202 FIF_OTHER_BSS;
4203
4204 return 1;
4205}
4206
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004207static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
4208{
4209 if (!list_empty(&priv->vif_list))
4210 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
4211
4212 return NULL;
4213}
4214
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004215static void mwl8k_configure_filter(struct ieee80211_hw *hw,
4216 unsigned int changed_flags,
4217 unsigned int *total_flags,
4218 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004219{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004220 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004221 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
4222
4223 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02004224 * AP firmware doesn't allow fine-grained control over
4225 * the receive filter.
4226 */
4227 if (priv->ap_fw) {
4228 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
4229 kfree(cmd);
4230 return;
4231 }
4232
4233 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004234 * Enable hardware sniffer mode if FIF_CONTROL or
4235 * FIF_OTHER_BSS is requested.
4236 */
4237 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
4238 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
4239 kfree(cmd);
4240 return;
4241 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004242
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004243 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004244 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004245
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004246 if (mwl8k_fw_lock(hw)) {
4247 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004248 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004249 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004250
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004251 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004252 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004253 priv->sniffer_enabled = false;
4254 }
4255
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004256 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004257 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
4258 /*
4259 * Disable the BSS filter.
4260 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004261 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004262 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004263 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004264 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004265
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004266 /*
4267 * Enable the BSS filter.
4268 *
4269 * If there is an active STA interface, use that
4270 * interface's BSSID, otherwise use a dummy one
4271 * (where the OUI part needs to be nonzero for
4272 * the BSSID to be accepted by POST_SCAN).
4273 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004274 mwl8k_vif = mwl8k_first_vif(priv);
4275 if (mwl8k_vif != NULL)
4276 bssid = mwl8k_vif->vif->bss_conf.bssid;
4277 else
4278 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004279
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004280 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004281 }
4282 }
4283
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004284 /*
4285 * If FIF_ALLMULTI is being requested, throw away the command
4286 * packet that ->prepare_multicast() built and replace it with
4287 * a command packet that enables reception of all multicast
4288 * packets.
4289 */
4290 if (*total_flags & FIF_ALLMULTI) {
4291 kfree(cmd);
Jiri Pirko22bedad2010-04-01 21:22:57 +00004292 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004293 }
4294
4295 if (cmd != NULL) {
4296 mwl8k_post_cmd(hw, cmd);
4297 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004298 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004299
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004300 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004301}
4302
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004303static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4304{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004305 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004306}
4307
Johannes Berg4a6967b2010-02-19 19:18:37 +01004308static int mwl8k_sta_remove(struct ieee80211_hw *hw,
4309 struct ieee80211_vif *vif,
4310 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004311{
4312 struct mwl8k_priv *priv = hw->priv;
4313
Johannes Berg4a6967b2010-02-19 19:18:37 +01004314 if (priv->ap_fw)
4315 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
4316 else
4317 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
4318}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004319
Johannes Berg4a6967b2010-02-19 19:18:37 +01004320static int mwl8k_sta_add(struct ieee80211_hw *hw,
4321 struct ieee80211_vif *vif,
4322 struct ieee80211_sta *sta)
4323{
4324 struct mwl8k_priv *priv = hw->priv;
4325 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004326 int i;
4327 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4328 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004329
Johannes Berg4a6967b2010-02-19 19:18:37 +01004330 if (!priv->ap_fw) {
4331 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
4332 if (ret >= 0) {
4333 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004334 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004335 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01004336
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004337 } else {
4338 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004339 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004340
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004341 for (i = 0; i < NUM_WEP_KEYS; i++) {
4342 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
4343 if (mwl8k_vif->wep_key_conf[i].enabled)
4344 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
4345 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004346 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004347}
4348
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004349static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
4350 const struct ieee80211_tx_queue_params *params)
4351{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004352 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004353 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004354
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004355 rc = mwl8k_fw_lock(hw);
4356 if (!rc) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004357 BUG_ON(queue > MWL8K_TX_QUEUES - 1);
4358 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
4359
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004360 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004361 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004362
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004363 if (!rc) {
4364 int q = MWL8K_TX_QUEUES - 1 - queue;
4365 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004366 params->cw_min,
4367 params->cw_max,
4368 params->aifs,
4369 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004370 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004371
4372 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004373 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004374
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004375 return rc;
4376}
4377
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004378static int mwl8k_get_stats(struct ieee80211_hw *hw,
4379 struct ieee80211_low_level_stats *stats)
4380{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004381 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004382}
4383
John W. Linville0d462bb2010-07-28 14:04:24 -04004384static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
4385 struct survey_info *survey)
4386{
4387 struct mwl8k_priv *priv = hw->priv;
4388 struct ieee80211_conf *conf = &hw->conf;
4389
4390 if (idx != 0)
4391 return -ENOENT;
4392
4393 survey->channel = conf->channel;
4394 survey->filled = SURVEY_INFO_NOISE_DBM;
4395 survey->noise = priv->noise;
4396
4397 return 0;
4398}
4399
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004400static int
4401mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4402 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01004403 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4404 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004405{
4406 switch (action) {
4407 case IEEE80211_AMPDU_RX_START:
4408 case IEEE80211_AMPDU_RX_STOP:
4409 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
4410 return -ENOTSUPP;
4411 return 0;
4412 default:
4413 return -ENOTSUPP;
4414 }
4415}
4416
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004417static const struct ieee80211_ops mwl8k_ops = {
4418 .tx = mwl8k_tx,
4419 .start = mwl8k_start,
4420 .stop = mwl8k_stop,
4421 .add_interface = mwl8k_add_interface,
4422 .remove_interface = mwl8k_remove_interface,
4423 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004424 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004425 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004426 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004427 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004428 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01004429 .sta_add = mwl8k_sta_add,
4430 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004431 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004432 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04004433 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004434 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004435};
4436
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004437static void mwl8k_finalize_join_worker(struct work_struct *work)
4438{
4439 struct mwl8k_priv *priv =
4440 container_of(work, struct mwl8k_priv, finalize_join_worker);
4441 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01004442 struct ieee80211_mgmt *mgmt = (void *)skb->data;
4443 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
4444 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
4445 mgmt->u.beacon.variable, len);
4446 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004447
Johannes Berg56007a02010-01-26 14:19:52 +01004448 if (tim && tim[1] >= 2)
4449 dtim_period = tim[3];
4450
4451 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004452
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004453 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004454 priv->beacon_skb = NULL;
4455}
4456
John W. Linvillebcb628d2009-11-06 16:40:16 -05004457enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004458 MWL8363 = 0,
4459 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004460 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02004461};
4462
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07004463#define MWL8K_8366_AP_FW_API 2
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004464#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
4465#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
4466
John W. Linvillebcb628d2009-11-06 16:40:16 -05004467static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004468 [MWL8363] = {
4469 .part_name = "88w8363",
4470 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004471 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004472 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004473 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004474 .part_name = "88w8687",
4475 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004476 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05004477 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004478 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004479 .part_name = "88w8366",
4480 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004481 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004482 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
4483 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004484 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004485 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004486};
4487
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004488MODULE_FIRMWARE("mwl8k/helper_8363.fw");
4489MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
4490MODULE_FIRMWARE("mwl8k/helper_8687.fw");
4491MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
4492MODULE_FIRMWARE("mwl8k/helper_8366.fw");
4493MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004494MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004495
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004496static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01004497 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004498 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
4499 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004500 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
4501 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
4502 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01004503 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004504 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004505};
4506MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
4507
Brian Cavagnolo99020472010-11-12 17:23:52 -08004508static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
4509{
4510 int rc;
4511 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
4512 "Trying alternative firmware %s\n", pci_name(priv->pdev),
4513 priv->fw_pref, priv->fw_alt);
4514 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
4515 if (rc) {
4516 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4517 pci_name(priv->pdev), priv->fw_alt);
4518 return rc;
4519 }
4520 return 0;
4521}
4522
4523static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
4524static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
4525{
4526 struct mwl8k_priv *priv = context;
4527 struct mwl8k_device_info *di = priv->device_info;
4528 int rc;
4529
4530 switch (priv->fw_state) {
4531 case FW_STATE_INIT:
4532 if (!fw) {
4533 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
4534 pci_name(priv->pdev), di->helper_image);
4535 goto fail;
4536 }
4537 priv->fw_helper = fw;
4538 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
4539 true);
4540 if (rc && priv->fw_alt) {
4541 rc = mwl8k_request_alt_fw(priv);
4542 if (rc)
4543 goto fail;
4544 priv->fw_state = FW_STATE_LOADING_ALT;
4545 } else if (rc)
4546 goto fail;
4547 else
4548 priv->fw_state = FW_STATE_LOADING_PREF;
4549 break;
4550
4551 case FW_STATE_LOADING_PREF:
4552 if (!fw) {
4553 if (priv->fw_alt) {
4554 rc = mwl8k_request_alt_fw(priv);
4555 if (rc)
4556 goto fail;
4557 priv->fw_state = FW_STATE_LOADING_ALT;
4558 } else
4559 goto fail;
4560 } else {
4561 priv->fw_ucode = fw;
4562 rc = mwl8k_firmware_load_success(priv);
4563 if (rc)
4564 goto fail;
4565 else
4566 complete(&priv->firmware_loading_complete);
4567 }
4568 break;
4569
4570 case FW_STATE_LOADING_ALT:
4571 if (!fw) {
4572 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4573 pci_name(priv->pdev), di->helper_image);
4574 goto fail;
4575 }
4576 priv->fw_ucode = fw;
4577 rc = mwl8k_firmware_load_success(priv);
4578 if (rc)
4579 goto fail;
4580 else
4581 complete(&priv->firmware_loading_complete);
4582 break;
4583
4584 default:
4585 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
4586 MWL8K_NAME, priv->fw_state);
4587 BUG_ON(1);
4588 }
4589
4590 return;
4591
4592fail:
4593 priv->fw_state = FW_STATE_ERROR;
4594 complete(&priv->firmware_loading_complete);
4595 device_release_driver(&priv->pdev->dev);
4596 mwl8k_release_firmware(priv);
4597}
4598
4599static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
4600 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004601{
4602 struct mwl8k_priv *priv = hw->priv;
4603 int rc;
4604
4605 /* Reset firmware and hardware */
4606 mwl8k_hw_reset(priv);
4607
4608 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004609 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004610 if (rc) {
4611 wiphy_err(hw->wiphy, "Firmware files not found\n");
4612 return rc;
4613 }
4614
Brian Cavagnolo99020472010-11-12 17:23:52 -08004615 if (nowait)
4616 return rc;
4617
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004618 /* Load firmware into hardware */
4619 rc = mwl8k_load_firmware(hw);
4620 if (rc)
4621 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4622
4623 /* Reclaim memory once firmware is successfully loaded */
4624 mwl8k_release_firmware(priv);
4625
4626 return rc;
4627}
4628
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004629static int mwl8k_init_txqs(struct ieee80211_hw *hw)
4630{
4631 struct mwl8k_priv *priv = hw->priv;
4632 int rc = 0;
4633 int i;
4634
4635 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4636 rc = mwl8k_txq_init(hw, i);
4637 if (rc)
4638 break;
4639 if (priv->ap_fw)
4640 iowrite32(priv->txq[i].txd_dma,
4641 priv->sram + priv->txq_offset[i]);
4642 }
4643 return rc;
4644}
4645
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004646/* initialize hw after successfully loading a firmware image */
4647static int mwl8k_probe_hw(struct ieee80211_hw *hw)
4648{
4649 struct mwl8k_priv *priv = hw->priv;
4650 int rc = 0;
4651 int i;
4652
4653 if (priv->ap_fw) {
4654 priv->rxd_ops = priv->device_info->ap_rxd_ops;
4655 if (priv->rxd_ops == NULL) {
4656 wiphy_err(hw->wiphy,
4657 "Driver does not have AP firmware image support for this hardware\n");
4658 goto err_stop_firmware;
4659 }
4660 } else {
4661 priv->rxd_ops = &rxd_sta_ops;
4662 }
4663
4664 priv->sniffer_enabled = false;
4665 priv->wmm_enabled = false;
4666 priv->pending_tx_pkts = 0;
4667
4668 rc = mwl8k_rxq_init(hw, 0);
4669 if (rc)
4670 goto err_stop_firmware;
4671 rxq_refill(hw, 0, INT_MAX);
4672
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004673 /* For the sta firmware, we need to know the dma addresses of tx queues
4674 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
4675 * prior to issuing this command. But for the AP case, we learn the
4676 * total number of queues from the result CMD_GET_HW_SPEC, so for this
4677 * case we must initialize the tx queues after.
4678 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07004679 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004680 if (!priv->ap_fw) {
4681 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004682 if (rc)
4683 goto err_free_queues;
4684 }
4685
4686 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4687 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4688 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
4689 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
4690 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4691
4692 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4693 IRQF_SHARED, MWL8K_NAME, hw);
4694 if (rc) {
4695 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4696 goto err_free_queues;
4697 }
4698
4699 /*
4700 * Temporarily enable interrupts. Initial firmware host
4701 * commands use interrupts and avoid polling. Disable
4702 * interrupts when done.
4703 */
4704 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4705
4706 /* Get config data, mac addrs etc */
4707 if (priv->ap_fw) {
4708 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4709 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004710 rc = mwl8k_init_txqs(hw);
4711 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004712 rc = mwl8k_cmd_set_hw_spec(hw);
4713 } else {
4714 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4715 }
4716 if (rc) {
4717 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
4718 goto err_free_irq;
4719 }
4720
4721 /* Turn radio off */
4722 rc = mwl8k_cmd_radio_disable(hw);
4723 if (rc) {
4724 wiphy_err(hw->wiphy, "Cannot disable\n");
4725 goto err_free_irq;
4726 }
4727
4728 /* Clear MAC address */
4729 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
4730 if (rc) {
4731 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
4732 goto err_free_irq;
4733 }
4734
4735 /* Disable interrupts */
4736 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4737 free_irq(priv->pdev->irq, hw);
4738
4739 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
4740 priv->device_info->part_name,
4741 priv->hw_rev, hw->wiphy->perm_addr,
4742 priv->ap_fw ? "AP" : "STA",
4743 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4744 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
4745
4746 return 0;
4747
4748err_free_irq:
4749 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4750 free_irq(priv->pdev->irq, hw);
4751
4752err_free_queues:
4753 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4754 mwl8k_txq_deinit(hw, i);
4755 mwl8k_rxq_deinit(hw, 0);
4756
4757err_stop_firmware:
4758 mwl8k_hw_reset(priv);
4759
4760 return rc;
4761}
4762
4763/*
4764 * invoke mwl8k_reload_firmware to change the firmware image after the device
4765 * has already been registered
4766 */
4767static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
4768{
4769 int i, rc = 0;
4770 struct mwl8k_priv *priv = hw->priv;
4771
4772 mwl8k_stop(hw);
4773 mwl8k_rxq_deinit(hw, 0);
4774
4775 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4776 mwl8k_txq_deinit(hw, i);
4777
Brian Cavagnolo99020472010-11-12 17:23:52 -08004778 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004779 if (rc)
4780 goto fail;
4781
4782 rc = mwl8k_probe_hw(hw);
4783 if (rc)
4784 goto fail;
4785
4786 rc = mwl8k_start(hw);
4787 if (rc)
4788 goto fail;
4789
4790 rc = mwl8k_config(hw, ~0);
4791 if (rc)
4792 goto fail;
4793
4794 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4795 rc = mwl8k_conf_tx(hw, i, &priv->wmm_params[i]);
4796 if (rc)
4797 goto fail;
4798 }
4799
4800 return rc;
4801
4802fail:
4803 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
4804 return rc;
4805}
4806
4807static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
4808{
4809 struct ieee80211_hw *hw = priv->hw;
4810 int i, rc;
4811
Brian Cavagnolo99020472010-11-12 17:23:52 -08004812 rc = mwl8k_load_firmware(hw);
4813 mwl8k_release_firmware(priv);
4814 if (rc) {
4815 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4816 return rc;
4817 }
4818
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004819 /*
4820 * Extra headroom is the size of the required DMA header
4821 * minus the size of the smallest 802.11 frame (CTS frame).
4822 */
4823 hw->extra_tx_headroom =
4824 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
4825
4826 hw->channel_change_time = 10;
4827
4828 hw->queues = MWL8K_TX_QUEUES;
4829
4830 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08004831 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004832 hw->vif_data_size = sizeof(struct mwl8k_vif);
4833 hw->sta_data_size = sizeof(struct mwl8k_sta);
4834
4835 priv->macids_used = 0;
4836 INIT_LIST_HEAD(&priv->vif_list);
4837
4838 /* Set default radio state and preamble */
4839 priv->radio_on = 0;
4840 priv->radio_short_preamble = 0;
4841
4842 /* Finalize join worker */
4843 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4844
4845 /* TX reclaim and RX tasklets. */
4846 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4847 tasklet_disable(&priv->poll_tx_task);
4848 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4849 tasklet_disable(&priv->poll_rx_task);
4850
4851 /* Power management cookie */
4852 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4853 if (priv->cookie == NULL)
4854 return -ENOMEM;
4855
4856 mutex_init(&priv->fw_mutex);
4857 priv->fw_mutex_owner = NULL;
4858 priv->fw_mutex_depth = 0;
4859 priv->hostcmd_wait = NULL;
4860
4861 spin_lock_init(&priv->tx_lock);
4862
4863 priv->tx_wait = NULL;
4864
4865 rc = mwl8k_probe_hw(hw);
4866 if (rc)
4867 goto err_free_cookie;
4868
4869 hw->wiphy->interface_modes = 0;
4870 if (priv->ap_macids_supported || priv->device_info->fw_image_ap)
4871 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4872 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
4873 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4874
4875 rc = ieee80211_register_hw(hw);
4876 if (rc) {
4877 wiphy_err(hw->wiphy, "Cannot register device\n");
4878 goto err_unprobe_hw;
4879 }
4880
4881 return 0;
4882
4883err_unprobe_hw:
4884 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4885 mwl8k_txq_deinit(hw, i);
4886 mwl8k_rxq_deinit(hw, 0);
4887
4888err_free_cookie:
4889 if (priv->cookie != NULL)
4890 pci_free_consistent(priv->pdev, 4,
4891 priv->cookie, priv->cookie_dma);
4892
4893 return rc;
4894}
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004895static int __devinit mwl8k_probe(struct pci_dev *pdev,
4896 const struct pci_device_id *id)
4897{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004898 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004899 struct ieee80211_hw *hw;
4900 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004901 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004902 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004903
4904 if (!printed_version) {
4905 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
4906 printed_version = 1;
4907 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004908
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004909
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004910 rc = pci_enable_device(pdev);
4911 if (rc) {
4912 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
4913 MWL8K_NAME);
4914 return rc;
4915 }
4916
4917 rc = pci_request_regions(pdev, MWL8K_NAME);
4918 if (rc) {
4919 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
4920 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004921 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004922 }
4923
4924 pci_set_master(pdev);
4925
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004926
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004927 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
4928 if (hw == NULL) {
4929 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
4930 rc = -ENOMEM;
4931 goto err_free_reg;
4932 }
4933
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004934 SET_IEEE80211_DEV(hw, &pdev->dev);
4935 pci_set_drvdata(pdev, hw);
4936
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004937 priv = hw->priv;
4938 priv->hw = hw;
4939 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05004940 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004941
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004942
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004943 priv->sram = pci_iomap(pdev, 0, 0x10000);
4944 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004945 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004946 goto err_iounmap;
4947 }
4948
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004949 /*
4950 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
4951 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
4952 */
4953 priv->regs = pci_iomap(pdev, 1, 0x10000);
4954 if (priv->regs == NULL) {
4955 priv->regs = pci_iomap(pdev, 2, 0x10000);
4956 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004957 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004958 goto err_iounmap;
4959 }
4960 }
4961
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004962 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08004963 * Choose the initial fw image depending on user input. If a second
4964 * image is available, make it the alternative image that will be
4965 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004966 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004967 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004968 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08004969 if (ap_mode_default && di->fw_image_ap) {
4970 priv->fw_pref = di->fw_image_ap;
4971 priv->fw_alt = di->fw_image_sta;
4972 } else if (!ap_mode_default && di->fw_image_sta) {
4973 priv->fw_pref = di->fw_image_sta;
4974 priv->fw_alt = di->fw_image_ap;
4975 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004976 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08004977 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004978 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
4979 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08004980 priv->fw_pref = di->fw_image_ap;
4981 }
4982 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004983 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004984 goto err_stop_firmware;
Brian Cavagnolo99020472010-11-12 17:23:52 -08004985 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004986
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004987err_stop_firmware:
4988 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004989
4990err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004991 if (priv->regs != NULL)
4992 pci_iounmap(pdev, priv->regs);
4993
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004994 if (priv->sram != NULL)
4995 pci_iounmap(pdev, priv->sram);
4996
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004997 pci_set_drvdata(pdev, NULL);
4998 ieee80211_free_hw(hw);
4999
5000err_free_reg:
5001 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005002
5003err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005004 pci_disable_device(pdev);
5005
5006 return rc;
5007}
5008
Joerg Albert230f7af2009-04-18 02:10:45 +02005009static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005010{
5011 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
5012}
5013
Joerg Albert230f7af2009-04-18 02:10:45 +02005014static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005015{
5016 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
5017 struct mwl8k_priv *priv;
5018 int i;
5019
5020 if (hw == NULL)
5021 return;
5022 priv = hw->priv;
5023
Brian Cavagnolo99020472010-11-12 17:23:52 -08005024 wait_for_completion(&priv->firmware_loading_complete);
5025
5026 if (priv->fw_state == FW_STATE_ERROR) {
5027 mwl8k_hw_reset(priv);
5028 goto unmap;
5029 }
5030
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005031 ieee80211_stop_queues(hw);
5032
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02005033 ieee80211_unregister_hw(hw);
5034
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005035 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01005036 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005037 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005038
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005039 /* Stop hardware */
5040 mwl8k_hw_reset(priv);
5041
5042 /* Return all skbs to mac80211 */
5043 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01005044 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005045
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005046 for (i = 0; i < MWL8K_TX_QUEUES; i++)
5047 mwl8k_txq_deinit(hw, i);
5048
5049 mwl8k_rxq_deinit(hw, 0);
5050
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005051 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005052
Brian Cavagnolo99020472010-11-12 17:23:52 -08005053unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005054 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005055 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005056 pci_set_drvdata(pdev, NULL);
5057 ieee80211_free_hw(hw);
5058 pci_release_regions(pdev);
5059 pci_disable_device(pdev);
5060}
5061
5062static struct pci_driver mwl8k_driver = {
5063 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005064 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005065 .probe = mwl8k_probe,
5066 .remove = __devexit_p(mwl8k_remove),
5067 .shutdown = __devexit_p(mwl8k_shutdown),
5068};
5069
5070static int __init mwl8k_init(void)
5071{
5072 return pci_register_driver(&mwl8k_driver);
5073}
5074
5075static void __exit mwl8k_exit(void)
5076{
5077 pci_unregister_driver(&mwl8k_driver);
5078}
5079
5080module_init(mwl8k_init);
5081module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005082
5083MODULE_DESCRIPTION(MWL8K_DESC);
5084MODULE_VERSION(MWL8K_VERSION);
5085MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
5086MODULE_LICENSE("GPL");