blob: 36952274950e3cf7537834a132d5374f7b2b7322 [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
89
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020090struct rxd_ops {
91 int rxd_size;
92 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
93 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010094 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -040095 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020096};
97
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020098struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020099 char *part_name;
100 char *helper_image;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800101 char *fw_image_sta;
102 char *fw_image_ap;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100103 struct rxd_ops *ap_rxd_ops;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -0800104 u32 fw_api_ap;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200105};
106
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100107struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100109
110 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200111 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100112
113 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200114 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200116 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200118 struct {
119 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900120 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200121 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100122};
123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100124struct mwl8k_tx_queue {
125 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200126 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100127
128 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200129 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200131 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200132 struct mwl8k_tx_desc *txd;
133 dma_addr_t txd_dma;
134 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100135};
136
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100137struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100138 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100139 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200141 struct mwl8k_device_info *device_info;
142
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100143 void __iomem *sram;
144 void __iomem *regs;
145
146 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800147 const struct firmware *fw_helper;
148 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100149
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100150 /* hardware/firmware parameters */
151 bool ap_fw;
152 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100153 struct ieee80211_supported_band band_24;
154 struct ieee80211_channel channels_24[14];
155 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100156 struct ieee80211_supported_band band_50;
157 struct ieee80211_channel channels_50[4];
158 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100159 u32 ap_macids_supported;
160 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100161
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200162 /* firmware access */
163 struct mutex fw_mutex;
164 struct task_struct *fw_mutex_owner;
165 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200166 struct completion *hostcmd_wait;
167
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168 /* lock held over TX and TX reap */
169 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100170
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200171 /* TX quiesce completion, protected by fw_mutex and tx_lock */
172 struct completion *tx_wait;
173
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100174 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100175 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100176 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100178 /* power management status cookie from firmware */
179 u32 *cookie;
180 dma_addr_t cookie_dma;
181
182 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100183 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200184 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100185
186 /*
187 * Running count of TX packets in flight, to avoid
188 * iterating over the transmit rings each time.
189 */
190 int pending_tx_pkts;
191
192 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
193 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
194
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200195 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200196 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200197 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200198 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100199
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100200 /* XXX need to convert this to handle multiple interfaces */
201 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200202 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100203 struct sk_buff *beacon_skb;
204
205 /*
206 * This FJ worker has to be global as it is scheduled from the
207 * RX handler. At this point we don't know which interface it
208 * belongs to until the list of bssids waiting to complete join
209 * is checked.
210 */
211 struct work_struct finalize_join_worker;
212
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100213 /* Tasklet to perform TX reclaim. */
214 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100215
216 /* Tasklet to perform RX. */
217 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400218
219 /* Most recently reported noise in dBm */
220 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800221
222 /*
223 * preserve the queue configurations so they can be restored if/when
224 * the firmware image is swapped.
225 */
226 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800227
228 /* async firmware loading state */
229 unsigned fw_state;
230 char *fw_pref;
231 char *fw_alt;
232 struct completion firmware_loading_complete;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100233};
234
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800235#define MAX_WEP_KEY_LEN 13
236#define NUM_WEP_KEYS 4
237
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100238/* Per interface specific private data */
239struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100240 struct list_head list;
241 struct ieee80211_vif *vif;
242
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100243 /* Firmware macid for this vif. */
244 int macid;
245
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100246 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100247 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800248
249 /* Saved WEP keys */
250 struct {
251 u8 enabled;
252 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
253 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800254
255 /* BSSID */
256 u8 bssid[ETH_ALEN];
257
258 /* A flag to indicate is HW crypto is enabled for this bssid */
259 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100260};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200261#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800262#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100263
Lennert Buytenheka6804002010-01-04 21:55:42 +0100264struct mwl8k_sta {
265 /* Index into station database. Returned by UPDATE_STADB. */
266 u8 peer_id;
267};
268#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
269
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100270static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100271 { .center_freq = 2412, .hw_value = 1, },
272 { .center_freq = 2417, .hw_value = 2, },
273 { .center_freq = 2422, .hw_value = 3, },
274 { .center_freq = 2427, .hw_value = 4, },
275 { .center_freq = 2432, .hw_value = 5, },
276 { .center_freq = 2437, .hw_value = 6, },
277 { .center_freq = 2442, .hw_value = 7, },
278 { .center_freq = 2447, .hw_value = 8, },
279 { .center_freq = 2452, .hw_value = 9, },
280 { .center_freq = 2457, .hw_value = 10, },
281 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100282 { .center_freq = 2467, .hw_value = 12, },
283 { .center_freq = 2472, .hw_value = 13, },
284 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100285};
286
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100287static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100288 { .bitrate = 10, .hw_value = 2, },
289 { .bitrate = 20, .hw_value = 4, },
290 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200291 { .bitrate = 110, .hw_value = 22, },
292 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100293 { .bitrate = 60, .hw_value = 12, },
294 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100295 { .bitrate = 120, .hw_value = 24, },
296 { .bitrate = 180, .hw_value = 36, },
297 { .bitrate = 240, .hw_value = 48, },
298 { .bitrate = 360, .hw_value = 72, },
299 { .bitrate = 480, .hw_value = 96, },
300 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100301 { .bitrate = 720, .hw_value = 144, },
302};
303
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100304static const struct ieee80211_channel mwl8k_channels_50[] = {
305 { .center_freq = 5180, .hw_value = 36, },
306 { .center_freq = 5200, .hw_value = 40, },
307 { .center_freq = 5220, .hw_value = 44, },
308 { .center_freq = 5240, .hw_value = 48, },
309};
310
311static const struct ieee80211_rate mwl8k_rates_50[] = {
312 { .bitrate = 60, .hw_value = 12, },
313 { .bitrate = 90, .hw_value = 18, },
314 { .bitrate = 120, .hw_value = 24, },
315 { .bitrate = 180, .hw_value = 36, },
316 { .bitrate = 240, .hw_value = 48, },
317 { .bitrate = 360, .hw_value = 72, },
318 { .bitrate = 480, .hw_value = 96, },
319 { .bitrate = 540, .hw_value = 108, },
320 { .bitrate = 720, .hw_value = 144, },
321};
322
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100323/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100324#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800325#define MWL8K_CMD_SET 0x0001
326#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100327
328/* Firmware command codes */
329#define MWL8K_CMD_CODE_DNLD 0x0001
330#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200331#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100332#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
333#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200334#define MWL8K_CMD_RADIO_CONTROL 0x001c
335#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800336#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200337#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100338#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100339#define MWL8K_CMD_SET_PRE_SCAN 0x0107
340#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200341#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100342#define MWL8K_CMD_SET_AID 0x010d
343#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200344#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100345#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200346#define MWL8K_CMD_SET_SLOT 0x0114
347#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
348#define MWL8K_CMD_SET_WMM_MODE 0x0123
349#define MWL8K_CMD_MIMO_CONFIG 0x0125
350#define MWL8K_CMD_USE_FIXED_RATE 0x0126
351#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100352#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200353#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100354#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
355#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800356#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200357#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100358
John W. Linvilleb6037422010-07-20 13:55:00 -0400359static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100360{
John W. Linvilleb6037422010-07-20 13:55:00 -0400361 u16 command = le16_to_cpu(cmd);
362
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100363#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
364 snprintf(buf, bufsize, "%s", #x);\
365 return buf;\
366 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400367 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100368 MWL8K_CMDNAME(CODE_DNLD);
369 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200370 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100371 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
372 MWL8K_CMDNAME(GET_STAT);
373 MWL8K_CMDNAME(RADIO_CONTROL);
374 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800375 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200376 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100377 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100378 MWL8K_CMDNAME(SET_PRE_SCAN);
379 MWL8K_CMDNAME(SET_POST_SCAN);
380 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100381 MWL8K_CMDNAME(SET_AID);
382 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200383 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100384 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200385 MWL8K_CMDNAME(SET_SLOT);
386 MWL8K_CMDNAME(SET_EDCA_PARAMS);
387 MWL8K_CMDNAME(SET_WMM_MODE);
388 MWL8K_CMDNAME(MIMO_CONFIG);
389 MWL8K_CMDNAME(USE_FIXED_RATE);
390 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200391 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200392 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100393 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100394 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800395 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200396 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100397 default:
398 snprintf(buf, bufsize, "0x%x", cmd);
399 }
400#undef MWL8K_CMDNAME
401
402 return buf;
403}
404
405/* Hardware and firmware reset */
406static void mwl8k_hw_reset(struct mwl8k_priv *priv)
407{
408 iowrite32(MWL8K_H2A_INT_RESET,
409 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
410 iowrite32(MWL8K_H2A_INT_RESET,
411 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
412 msleep(20);
413}
414
415/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800416static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100417{
418 if (*fw == NULL)
419 return;
420 release_firmware(*fw);
421 *fw = NULL;
422}
423
424static void mwl8k_release_firmware(struct mwl8k_priv *priv)
425{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100426 mwl8k_release_fw(&priv->fw_ucode);
427 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100428}
429
Brian Cavagnolo99020472010-11-12 17:23:52 -0800430/* states for asynchronous f/w loading */
431static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
432enum {
433 FW_STATE_INIT = 0,
434 FW_STATE_LOADING_PREF,
435 FW_STATE_LOADING_ALT,
436 FW_STATE_ERROR,
437};
438
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100439/* Request fw image */
440static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800441 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800442 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100443{
444 /* release current image */
445 if (*fw != NULL)
446 mwl8k_release_fw(fw);
447
Brian Cavagnolo99020472010-11-12 17:23:52 -0800448 if (nowait)
449 return request_firmware_nowait(THIS_MODULE, 1, fname,
450 &priv->pdev->dev, GFP_KERNEL,
451 priv, mwl8k_fw_state_machine);
452 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800453 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100454}
455
Brian Cavagnolo99020472010-11-12 17:23:52 -0800456static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
457 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100458{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200459 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100460 int rc;
461
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200462 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800463 if (nowait)
464 rc = mwl8k_request_fw(priv, di->helper_image,
465 &priv->fw_helper, true);
466 else
467 rc = mwl8k_request_fw(priv, di->helper_image,
468 &priv->fw_helper, false);
469 if (rc)
470 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
471 pci_name(priv->pdev), di->helper_image);
472
473 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200474 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100475 }
476
Brian Cavagnolo99020472010-11-12 17:23:52 -0800477 if (nowait) {
478 /*
479 * if we get here, no helper image is needed. Skip the
480 * FW_STATE_INIT state.
481 */
482 priv->fw_state = FW_STATE_LOADING_PREF;
483 rc = mwl8k_request_fw(priv, fw_image,
484 &priv->fw_ucode,
485 true);
486 } else
487 rc = mwl8k_request_fw(priv, fw_image,
488 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100489 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200490 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800491 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100492 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100493 return rc;
494 }
495
496 return 0;
497}
498
499struct mwl8k_cmd_pkt {
500 __le16 code;
501 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100502 __u8 seq_num;
503 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100504 __le16 result;
505 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000506} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100507
508/*
509 * Firmware loading.
510 */
511static int
512mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
513{
514 void __iomem *regs = priv->regs;
515 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100516 int loops;
517
518 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
519 if (pci_dma_mapping_error(priv->pdev, dma_addr))
520 return -ENOMEM;
521
522 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
523 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
524 iowrite32(MWL8K_H2A_INT_DOORBELL,
525 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
526 iowrite32(MWL8K_H2A_INT_DUMMY,
527 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
528
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100529 loops = 1000;
530 do {
531 u32 int_code;
532
533 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
534 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
535 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100536 break;
537 }
538
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200539 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100540 udelay(1);
541 } while (--loops);
542
543 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
544
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200545 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100546}
547
548static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
549 const u8 *data, size_t length)
550{
551 struct mwl8k_cmd_pkt *cmd;
552 int done;
553 int rc = 0;
554
555 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
556 if (cmd == NULL)
557 return -ENOMEM;
558
559 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
560 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100561 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100562 cmd->result = 0;
563
564 done = 0;
565 while (length) {
566 int block_size = length > 256 ? 256 : length;
567
568 memcpy(cmd->payload, data + done, block_size);
569 cmd->length = cpu_to_le16(block_size);
570
571 rc = mwl8k_send_fw_load_cmd(priv, cmd,
572 sizeof(*cmd) + block_size);
573 if (rc)
574 break;
575
576 done += block_size;
577 length -= block_size;
578 }
579
580 if (!rc) {
581 cmd->length = 0;
582 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
583 }
584
585 kfree(cmd);
586
587 return rc;
588}
589
590static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
591 const u8 *data, size_t length)
592{
593 unsigned char *buffer;
594 int may_continue, rc = 0;
595 u32 done, prev_block_size;
596
597 buffer = kmalloc(1024, GFP_KERNEL);
598 if (buffer == NULL)
599 return -ENOMEM;
600
601 done = 0;
602 prev_block_size = 0;
603 may_continue = 1000;
604 while (may_continue > 0) {
605 u32 block_size;
606
607 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
608 if (block_size & 1) {
609 block_size &= ~1;
610 may_continue--;
611 } else {
612 done += prev_block_size;
613 length -= prev_block_size;
614 }
615
616 if (block_size > 1024 || block_size > length) {
617 rc = -EOVERFLOW;
618 break;
619 }
620
621 if (length == 0) {
622 rc = 0;
623 break;
624 }
625
626 if (block_size == 0) {
627 rc = -EPROTO;
628 may_continue--;
629 udelay(1);
630 continue;
631 }
632
633 prev_block_size = block_size;
634 memcpy(buffer, data + done, block_size);
635
636 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
637 if (rc)
638 break;
639 }
640
641 if (!rc && length != 0)
642 rc = -EREMOTEIO;
643
644 kfree(buffer);
645
646 return rc;
647}
648
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200649static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100650{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200651 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800652 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200653 int rc;
654 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100655
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200656 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800657 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100658
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200659 if (helper == NULL) {
660 printk(KERN_ERR "%s: helper image needed but none "
661 "given\n", pci_name(priv->pdev));
662 return -EINVAL;
663 }
664
665 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100666 if (rc) {
667 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200668 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100669 return rc;
670 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100671 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100672
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200673 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100674 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200675 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100676 }
677
678 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200679 printk(KERN_ERR "%s: unable to load firmware image\n",
680 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100681 return rc;
682 }
683
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100684 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100685
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100686 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100687 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200688 u32 ready_code;
689
690 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
691 if (ready_code == MWL8K_FWAP_READY) {
692 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100693 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200694 } else if (ready_code == MWL8K_FWSTA_READY) {
695 priv->ap_fw = 0;
696 break;
697 }
698
699 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100700 udelay(1);
701 } while (--loops);
702
703 return loops ? 0 : -ETIMEDOUT;
704}
705
706
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100707/* DMA header used by firmware and hardware. */
708struct mwl8k_dma_data {
709 __le16 fwlen;
710 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100711 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000712} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100713
714/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100715static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100716{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100717 struct mwl8k_dma_data *tr;
718 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100719
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100720 tr = (struct mwl8k_dma_data *)skb->data;
721 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
722
723 if (hdrlen != sizeof(tr->wh)) {
724 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
725 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
726 *((__le16 *)(tr->data - 2)) = qos;
727 } else {
728 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
729 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100730 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100731
732 if (hdrlen != sizeof(*tr))
733 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100734}
735
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800736static void
737mwl8k_add_dma_header(struct sk_buff *skb, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100738{
739 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100740 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800741 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100742 struct mwl8k_dma_data *tr;
743
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100744 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100745 * Add a firmware DMA header; the firmware requires that we
746 * present a 2-byte payload length followed by a 4-address
747 * header (without QoS field), followed (optionally) by any
748 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100749 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100750 wh = (struct ieee80211_hdr *)skb->data;
751
752 hdrlen = ieee80211_hdrlen(wh->frame_control);
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800753 reqd_hdrlen = sizeof(*tr);
754
755 if (hdrlen != reqd_hdrlen)
756 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100757
758 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800759 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100760
761 tr = (struct mwl8k_dma_data *)skb->data;
762 if (wh != &tr->wh)
763 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100764 if (hdrlen != sizeof(tr->wh))
765 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100766
767 /*
768 * Firmware length is the length of the fully formed "802.11
769 * payload". That is, everything except for the 802.11 header.
770 * This includes all crypto material including the MIC.
771 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800772 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100773}
774
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800775static void mwl8k_encapsulate_tx_frame(struct sk_buff *skb)
776{
777 struct ieee80211_hdr *wh;
778 struct ieee80211_tx_info *tx_info;
779 struct ieee80211_key_conf *key_conf;
780 int data_pad;
781
782 wh = (struct ieee80211_hdr *)skb->data;
783
784 tx_info = IEEE80211_SKB_CB(skb);
785
786 key_conf = NULL;
787 if (ieee80211_is_data(wh->frame_control))
788 key_conf = tx_info->control.hw_key;
789
790 /*
791 * Make sure the packet header is in the DMA header format (4-address
792 * without QoS), the necessary crypto padding between the header and the
793 * payload has already been provided by mac80211, but it doesn't add tail
794 * padding when HW crypto is enabled.
795 *
796 * We have the following trailer padding requirements:
797 * - WEP: 4 trailer bytes (ICV)
798 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
799 * - CCMP: 8 trailer bytes (MIC)
800 */
801 data_pad = 0;
802 if (key_conf != NULL) {
803 switch (key_conf->cipher) {
804 case WLAN_CIPHER_SUITE_WEP40:
805 case WLAN_CIPHER_SUITE_WEP104:
806 data_pad = 4;
807 break;
808 case WLAN_CIPHER_SUITE_TKIP:
809 data_pad = 12;
810 break;
811 case WLAN_CIPHER_SUITE_CCMP:
812 data_pad = 8;
813 break;
814 }
815 }
816 mwl8k_add_dma_header(skb, data_pad);
817}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100818
819/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100820 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200821 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100822struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200823 __le16 pkt_len;
824 __u8 sq2;
825 __u8 rate;
826 __le32 pkt_phys_addr;
827 __le32 next_rxd_phys_addr;
828 __le16 qos_control;
829 __le16 htsig2;
830 __le32 hw_rssi_info;
831 __le32 hw_noise_floor_info;
832 __u8 noise_floor;
833 __u8 pad0[3];
834 __u8 rssi;
835 __u8 rx_status;
836 __u8 channel;
837 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000838} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200839
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100840#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
841#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
842#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100843
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100844#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200845
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800846/* 8366 AP rx_status bits */
847#define MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
848#define MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
849#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
850#define MWL8K_8366_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
851#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
852
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100853static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200854{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100855 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200856
857 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100858 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200859}
860
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100861static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200862{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100863 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200864
865 rxd->pkt_len = cpu_to_le16(len);
866 rxd->pkt_phys_addr = cpu_to_le32(addr);
867 wmb();
868 rxd->rx_ctrl = 0;
869}
870
871static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100872mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400873 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200874{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100875 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200876
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100877 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200878 return -1;
879 rmb();
880
881 memset(status, 0, sizeof(*status));
882
883 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400884 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200885
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100886 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200887 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100888 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100889 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100890 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200891 } else {
892 int i;
893
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100894 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
895 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200896 status->rate_idx = i;
897 break;
898 }
899 }
900 }
901
Lennert Buytenhek85478342010-01-12 13:48:56 +0100902 if (rxd->channel > 14) {
903 status->band = IEEE80211_BAND_5GHZ;
904 if (!(status->flag & RX_FLAG_HT))
905 status->rate_idx -= 5;
906 } else {
907 status->band = IEEE80211_BAND_2GHZ;
908 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900909 status->freq = ieee80211_channel_to_frequency(rxd->channel,
910 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200911
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100912 *qos = rxd->qos_control;
913
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800914 if ((rxd->rx_status != MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
915 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK) &&
916 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
917 status->flag |= RX_FLAG_MMIC_ERROR;
918
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200919 return le16_to_cpu(rxd->pkt_len);
920}
921
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100922static struct rxd_ops rxd_8366_ap_ops = {
923 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
924 .rxd_init = mwl8k_rxd_8366_ap_init,
925 .rxd_refill = mwl8k_rxd_8366_ap_refill,
926 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200927};
928
929/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100930 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100931 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100932struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100933 __le16 pkt_len;
934 __u8 link_quality;
935 __u8 noise_level;
936 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200937 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100938 __le16 qos_control;
939 __le16 rate_info;
940 __le32 pad0[4];
941 __u8 rssi;
942 __u8 channel;
943 __le16 pad1;
944 __u8 rx_ctrl;
945 __u8 rx_status;
946 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000947} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100948
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100949#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
950#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
951#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
952#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
953#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
954#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200955
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100956#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800957#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
958/* ICV=0 or MIC=1 */
959#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
960/* Key is uploaded only in failure case */
961#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200962
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100963static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200964{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100965 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200966
967 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100968 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200969}
970
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100971static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200972{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100973 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200974
975 rxd->pkt_len = cpu_to_le16(len);
976 rxd->pkt_phys_addr = cpu_to_le32(addr);
977 wmb();
978 rxd->rx_ctrl = 0;
979}
980
981static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100982mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400983 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200984{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100985 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200986 u16 rate_info;
987
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100988 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200989 return -1;
990 rmb();
991
992 rate_info = le16_to_cpu(rxd->rate_info);
993
994 memset(status, 0, sizeof(*status));
995
996 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400997 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100998 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
999 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001000
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001001 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001002 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001003 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001004 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001005 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001006 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001007 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001008 status->flag |= RX_FLAG_HT;
1009
Lennert Buytenhek85478342010-01-12 13:48:56 +01001010 if (rxd->channel > 14) {
1011 status->band = IEEE80211_BAND_5GHZ;
1012 if (!(status->flag & RX_FLAG_HT))
1013 status->rate_idx -= 5;
1014 } else {
1015 status->band = IEEE80211_BAND_2GHZ;
1016 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001017 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1018 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001019
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001020 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001021 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1022 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1023 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001024
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001025 return le16_to_cpu(rxd->pkt_len);
1026}
1027
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001028static struct rxd_ops rxd_sta_ops = {
1029 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1030 .rxd_init = mwl8k_rxd_sta_init,
1031 .rxd_refill = mwl8k_rxd_sta_refill,
1032 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001033};
1034
1035
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001036#define MWL8K_RX_DESCS 256
1037#define MWL8K_RX_MAXSZ 3800
1038
1039static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1040{
1041 struct mwl8k_priv *priv = hw->priv;
1042 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1043 int size;
1044 int i;
1045
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001046 rxq->rxd_count = 0;
1047 rxq->head = 0;
1048 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001049
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001050 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001051
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001052 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1053 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001054 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001055 return -ENOMEM;
1056 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001057 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001058
Shan Weib9ede5f2011-03-08 11:02:03 +08001059 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001060 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001061 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001062 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001063 return -ENOMEM;
1064 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001065
1066 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001067 int desc_size;
1068 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001069 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001070 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001071
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001072 desc_size = priv->rxd_ops->rxd_size;
1073 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001074
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001075 nexti = i + 1;
1076 if (nexti == MWL8K_RX_DESCS)
1077 nexti = 0;
1078 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1079
1080 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001081 }
1082
1083 return 0;
1084}
1085
1086static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1087{
1088 struct mwl8k_priv *priv = hw->priv;
1089 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1090 int refilled;
1091
1092 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001093 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001094 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001095 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001096 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001097 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001098
1099 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1100 if (skb == NULL)
1101 break;
1102
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001103 addr = pci_map_single(priv->pdev, skb->data,
1104 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001105
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001106 rxq->rxd_count++;
1107 rx = rxq->tail++;
1108 if (rxq->tail == MWL8K_RX_DESCS)
1109 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001110 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001111 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001112
1113 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1114 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001115
1116 refilled++;
1117 }
1118
1119 return refilled;
1120}
1121
1122/* Must be called only when the card's reception is completely halted */
1123static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1124{
1125 struct mwl8k_priv *priv = hw->priv;
1126 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1127 int i;
1128
1129 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001130 if (rxq->buf[i].skb != NULL) {
1131 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001132 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001133 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001134 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001135
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001136 kfree_skb(rxq->buf[i].skb);
1137 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001138 }
1139 }
1140
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001141 kfree(rxq->buf);
1142 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001143
1144 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001145 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001146 rxq->rxd, rxq->rxd_dma);
1147 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001148}
1149
1150
1151/*
1152 * Scan a list of BSSIDs to process for finalize join.
1153 * Allows for extension to process multiple BSSIDs.
1154 */
1155static inline int
1156mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1157{
1158 return priv->capture_beacon &&
1159 ieee80211_is_beacon(wh->frame_control) &&
1160 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1161}
1162
Lennert Buytenhek37797522009-10-22 20:19:45 +02001163static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1164 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001165{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001166 struct mwl8k_priv *priv = hw->priv;
1167
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001168 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001169 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001170
1171 /*
1172 * Use GFP_ATOMIC as rxq_process is called from
1173 * the primary interrupt handler, memory allocation call
1174 * must not sleep.
1175 */
1176 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1177 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001178 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001179}
1180
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001181static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1182 u8 *bssid)
1183{
1184 struct mwl8k_vif *mwl8k_vif;
1185
1186 list_for_each_entry(mwl8k_vif,
1187 vif_list, list) {
1188 if (memcmp(bssid, mwl8k_vif->bssid,
1189 ETH_ALEN) == 0)
1190 return mwl8k_vif;
1191 }
1192
1193 return NULL;
1194}
1195
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001196static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1197{
1198 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001199 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001200 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1201 int processed;
1202
1203 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001204 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001206 void *rxd;
1207 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001208 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001209 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001210 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001211
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001212 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001213 if (skb == NULL)
1214 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001215
1216 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1217
John W. Linville0d462bb2010-07-28 14:04:24 -04001218 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1219 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001220 if (pkt_len < 0)
1221 break;
1222
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001223 rxq->buf[rxq->head].skb = NULL;
1224
1225 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001226 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001227 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001228 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001229
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001230 rxq->head++;
1231 if (rxq->head == MWL8K_RX_DESCS)
1232 rxq->head = 0;
1233
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001234 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001235
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001236 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001237
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001238 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001239 * Check for a pending join operation. Save a
1240 * copy of the beacon and schedule a tasklet to
1241 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001242 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001243 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001244 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001245
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001246 if (ieee80211_has_protected(wh->frame_control)) {
1247
1248 /* Check if hw crypto has been enabled for
1249 * this bss. If yes, set the status flags
1250 * accordingly
1251 */
1252 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1253 wh->addr1);
1254
1255 if (mwl8k_vif != NULL &&
1256 mwl8k_vif->is_hw_crypto_enabled == true) {
1257 /*
1258 * When MMIC ERROR is encountered
1259 * by the firmware, payload is
1260 * dropped and only 32 bytes of
1261 * mwl8k Firmware header is sent
1262 * to the host.
1263 *
1264 * We need to add four bytes of
1265 * key information. In it
1266 * MAC80211 expects keyidx set to
1267 * 0 for triggering Counter
1268 * Measure of MMIC failure.
1269 */
1270 if (status.flag & RX_FLAG_MMIC_ERROR) {
1271 struct mwl8k_dma_data *tr;
1272 tr = (struct mwl8k_dma_data *)skb->data;
1273 memset((void *)&(tr->data), 0, 4);
1274 pkt_len += 4;
1275 }
1276
1277 if (!ieee80211_is_auth(wh->frame_control))
1278 status.flag |= RX_FLAG_IV_STRIPPED |
1279 RX_FLAG_DECRYPTED |
1280 RX_FLAG_MMIC_STRIPPED;
1281 }
1282 }
1283
1284 skb_put(skb, pkt_len);
1285 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001286 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1287 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001288
1289 processed++;
1290 }
1291
1292 return processed;
1293}
1294
1295
1296/*
1297 * Packet transmission.
1298 */
1299
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300#define MWL8K_TXD_STATUS_OK 0x00000001
1301#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1302#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1303#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001304#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001305
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001306#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1307#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1308#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1309#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1310#define MWL8K_QOS_EOSP 0x0010
1311
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001312struct mwl8k_tx_desc {
1313 __le32 status;
1314 __u8 data_rate;
1315 __u8 tx_priority;
1316 __le16 qos_control;
1317 __le32 pkt_phys_addr;
1318 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001319 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001320 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001321 __le32 reserved;
1322 __le16 rate_info;
1323 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001324 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001325} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001326
1327#define MWL8K_TX_DESCS 128
1328
1329static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1330{
1331 struct mwl8k_priv *priv = hw->priv;
1332 struct mwl8k_tx_queue *txq = priv->txq + index;
1333 int size;
1334 int i;
1335
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001336 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001337 txq->head = 0;
1338 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001339
1340 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1341
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001342 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1343 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001344 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345 return -ENOMEM;
1346 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001347 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001348
Shan Weib9ede5f2011-03-08 11:02:03 +08001349 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001350 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001351 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001352 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001353 return -ENOMEM;
1354 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001355
1356 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1357 struct mwl8k_tx_desc *tx_desc;
1358 int nexti;
1359
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001360 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001361 nexti = (i + 1) % MWL8K_TX_DESCS;
1362
1363 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001364 tx_desc->next_txd_phys_addr =
1365 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001366 }
1367
1368 return 0;
1369}
1370
1371static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1372{
1373 iowrite32(MWL8K_H2A_INT_PPA_READY,
1374 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1375 iowrite32(MWL8K_H2A_INT_DUMMY,
1376 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1377 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1378}
1379
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001380static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001381{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001382 struct mwl8k_priv *priv = hw->priv;
1383 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001384
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001385 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1386 struct mwl8k_tx_queue *txq = priv->txq + i;
1387 int fw_owned = 0;
1388 int drv_owned = 0;
1389 int unused = 0;
1390 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001391
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001392 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001393 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1394 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001395
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001396 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001397 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001398 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001399 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001400 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001401
1402 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001403 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001404 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001405
Joe Perchesc96c31e2010-07-26 14:39:58 -07001406 wiphy_err(hw->wiphy,
1407 "txq[%d] len=%d head=%d tail=%d "
1408 "fw_owned=%d drv_owned=%d unused=%d\n",
1409 i,
1410 txq->len, txq->head, txq->tail,
1411 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001412 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001413}
1414
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001415/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001416 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001417 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001418#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001419
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001420static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001421{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001422 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001423 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001424 int retry;
1425 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001426
1427 might_sleep();
1428
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001429 /*
1430 * The TX queues are stopped at this point, so this test
1431 * doesn't need to take ->tx_lock.
1432 */
1433 if (!priv->pending_tx_pkts)
1434 return 0;
1435
1436 retry = 0;
1437 rc = 0;
1438
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001440 priv->tx_wait = &tx_wait;
1441 while (!rc) {
1442 int oldcount;
1443 unsigned long timeout;
1444
1445 oldcount = priv->pending_tx_pkts;
1446
1447 spin_unlock_bh(&priv->tx_lock);
1448 timeout = wait_for_completion_timeout(&tx_wait,
1449 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1450 spin_lock_bh(&priv->tx_lock);
1451
1452 if (timeout) {
1453 WARN_ON(priv->pending_tx_pkts);
1454 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001455 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001456 }
1457 break;
1458 }
1459
1460 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001461 wiphy_notice(hw->wiphy,
1462 "waiting for tx rings to drain (%d -> %d pkts)\n",
1463 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001464 retry = 1;
1465 continue;
1466 }
1467
1468 priv->tx_wait = NULL;
1469
Joe Perchesc96c31e2010-07-26 14:39:58 -07001470 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1471 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001472 mwl8k_dump_tx_rings(hw);
1473
1474 rc = -ETIMEDOUT;
1475 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001476 spin_unlock_bh(&priv->tx_lock);
1477
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001478 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001479}
1480
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001481#define MWL8K_TXD_SUCCESS(status) \
1482 ((status) & (MWL8K_TXD_STATUS_OK | \
1483 MWL8K_TXD_STATUS_OK_RETRY | \
1484 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001485
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001486static int
1487mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001488{
1489 struct mwl8k_priv *priv = hw->priv;
1490 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001491 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001492
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001493 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001494 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001495 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496 struct mwl8k_tx_desc *tx_desc;
1497 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001498 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001499 struct sk_buff *skb;
1500 struct ieee80211_tx_info *info;
1501 u32 status;
1502
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001503 tx = txq->head;
1504 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001505
1506 status = le32_to_cpu(tx_desc->status);
1507
1508 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1509 if (!force)
1510 break;
1511 tx_desc->status &=
1512 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1513 }
1514
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001515 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001516 BUG_ON(txq->len == 0);
1517 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001518 priv->pending_tx_pkts--;
1519
1520 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001521 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001522 skb = txq->skb[tx];
1523 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001524
1525 BUG_ON(skb == NULL);
1526 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1527
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001528 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001529
1530 /* Mark descriptor as unused */
1531 tx_desc->pkt_phys_addr = 0;
1532 tx_desc->pkt_len = 0;
1533
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001534 info = IEEE80211_SKB_CB(skb);
1535 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001536
1537 /* Rate control is happening in the firmware.
1538 * Ensure no tx rate is being reported.
1539 */
1540 info->status.rates[0].idx = -1;
1541 info->status.rates[0].count = 1;
1542
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001543 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001544 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001545
1546 ieee80211_tx_status_irqsafe(hw, skb);
1547
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001548 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001549 }
1550
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001551 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001552 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001553
1554 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001555}
1556
1557/* must be called only when the card's transmit is completely halted */
1558static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1559{
1560 struct mwl8k_priv *priv = hw->priv;
1561 struct mwl8k_tx_queue *txq = priv->txq + index;
1562
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001563 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001564
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001565 kfree(txq->skb);
1566 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001567
1568 pci_free_consistent(priv->pdev,
1569 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001570 txq->txd, txq->txd_dma);
1571 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001572}
1573
Johannes Berg7bb45682011-02-24 14:42:06 +01001574static void
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001575mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1576{
1577 struct mwl8k_priv *priv = hw->priv;
1578 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001579 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001580 struct ieee80211_hdr *wh;
1581 struct mwl8k_tx_queue *txq;
1582 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001583 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001584 u32 txstatus;
1585 u8 txdatarate;
1586 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001587
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001588 wh = (struct ieee80211_hdr *)skb->data;
1589 if (ieee80211_is_data_qos(wh->frame_control))
1590 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1591 else
1592 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001593
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001594 if (priv->ap_fw)
1595 mwl8k_encapsulate_tx_frame(skb);
1596 else
1597 mwl8k_add_dma_header(skb, 0);
1598
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001599 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001600
1601 tx_info = IEEE80211_SKB_CB(skb);
1602 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001603
1604 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001605 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001606 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1607 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001608 }
1609
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001610 /* Setup firmware control bit fields for each frame type. */
1611 txstatus = 0;
1612 txdatarate = 0;
1613 if (ieee80211_is_mgmt(wh->frame_control) ||
1614 ieee80211_is_ctl(wh->frame_control)) {
1615 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001616 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001617 } else if (ieee80211_is_data(wh->frame_control)) {
1618 txdatarate = 1;
1619 if (is_multicast_ether_addr(wh->addr1))
1620 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1621
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001622 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001623 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001624 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001625 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001626 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001627 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001628
1629 dma = pci_map_single(priv->pdev, skb->data,
1630 skb->len, PCI_DMA_TODEVICE);
1631
1632 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001633 wiphy_debug(hw->wiphy,
1634 "failed to dma map skb, dropping TX frame.\n");
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001635 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001636 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001637 }
1638
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001639 spin_lock_bh(&priv->tx_lock);
1640
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001641 txq = priv->txq + index;
1642
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001643 BUG_ON(txq->skb[txq->tail] != NULL);
1644 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001645
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001646 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001647 tx->data_rate = txdatarate;
1648 tx->tx_priority = index;
1649 tx->qos_control = cpu_to_le16(qos);
1650 tx->pkt_phys_addr = cpu_to_le32(dma);
1651 tx->pkt_len = cpu_to_le16(skb->len);
1652 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001653 if (!priv->ap_fw && tx_info->control.sta != NULL)
1654 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1655 else
1656 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001658 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1659
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001660 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001661 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001662
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001663 txq->tail++;
1664 if (txq->tail == MWL8K_TX_DESCS)
1665 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001666
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001667 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001668 ieee80211_stop_queue(hw, index);
1669
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001670 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001671
1672 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001673}
1674
1675
1676/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001677 * Firmware access.
1678 *
1679 * We have the following requirements for issuing firmware commands:
1680 * - Some commands require that the packet transmit path is idle when
1681 * the command is issued. (For simplicity, we'll just quiesce the
1682 * transmit path for every command.)
1683 * - There are certain sequences of commands that need to be issued to
1684 * the hardware sequentially, with no other intervening commands.
1685 *
1686 * This leads to an implementation of a "firmware lock" as a mutex that
1687 * can be taken recursively, and which is taken by both the low-level
1688 * command submission function (mwl8k_post_cmd) as well as any users of
1689 * that function that require issuing of an atomic sequence of commands,
1690 * and quiesces the transmit path whenever it's taken.
1691 */
1692static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1693{
1694 struct mwl8k_priv *priv = hw->priv;
1695
1696 if (priv->fw_mutex_owner != current) {
1697 int rc;
1698
1699 mutex_lock(&priv->fw_mutex);
1700 ieee80211_stop_queues(hw);
1701
1702 rc = mwl8k_tx_wait_empty(hw);
1703 if (rc) {
1704 ieee80211_wake_queues(hw);
1705 mutex_unlock(&priv->fw_mutex);
1706
1707 return rc;
1708 }
1709
1710 priv->fw_mutex_owner = current;
1711 }
1712
1713 priv->fw_mutex_depth++;
1714
1715 return 0;
1716}
1717
1718static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1719{
1720 struct mwl8k_priv *priv = hw->priv;
1721
1722 if (!--priv->fw_mutex_depth) {
1723 ieee80211_wake_queues(hw);
1724 priv->fw_mutex_owner = NULL;
1725 mutex_unlock(&priv->fw_mutex);
1726 }
1727}
1728
1729
1730/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001731 * Command processing.
1732 */
1733
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001734/* Timeout firmware commands after 10s */
1735#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001736
1737static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1738{
1739 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1740 struct mwl8k_priv *priv = hw->priv;
1741 void __iomem *regs = priv->regs;
1742 dma_addr_t dma_addr;
1743 unsigned int dma_size;
1744 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001745 unsigned long timeout = 0;
1746 u8 buf[32];
1747
John W. Linvilleb6037422010-07-20 13:55:00 -04001748 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001749 dma_size = le16_to_cpu(cmd->length);
1750 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1751 PCI_DMA_BIDIRECTIONAL);
1752 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1753 return -ENOMEM;
1754
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001755 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001756 if (rc) {
1757 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1758 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001759 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001760 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001761
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762 priv->hostcmd_wait = &cmd_wait;
1763 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1764 iowrite32(MWL8K_H2A_INT_DOORBELL,
1765 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1766 iowrite32(MWL8K_H2A_INT_DUMMY,
1767 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001768
1769 timeout = wait_for_completion_timeout(&cmd_wait,
1770 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1771
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001772 priv->hostcmd_wait = NULL;
1773
1774 mwl8k_fw_unlock(hw);
1775
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001776 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1777 PCI_DMA_BIDIRECTIONAL);
1778
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001779 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001780 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001781 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1782 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001783 rc = -ETIMEDOUT;
1784 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001785 int ms;
1786
1787 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1788
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001789 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001790 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001791 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001792 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1793 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001794 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001795 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001796 mwl8k_cmd_name(cmd->code,
1797 buf, sizeof(buf)),
1798 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001799 }
1800
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001801 return rc;
1802}
1803
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001804static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1805 struct ieee80211_vif *vif,
1806 struct mwl8k_cmd_pkt *cmd)
1807{
1808 if (vif != NULL)
1809 cmd->macid = MWL8K_VIF(vif)->macid;
1810 return mwl8k_post_cmd(hw, cmd);
1811}
1812
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001813/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001814 * Setup code shared between STA and AP firmware images.
1815 */
1816static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1817{
1818 struct mwl8k_priv *priv = hw->priv;
1819
1820 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1821 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1822
1823 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1824 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1825
1826 priv->band_24.band = IEEE80211_BAND_2GHZ;
1827 priv->band_24.channels = priv->channels_24;
1828 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1829 priv->band_24.bitrates = priv->rates_24;
1830 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1831
1832 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1833}
1834
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001835static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1836{
1837 struct mwl8k_priv *priv = hw->priv;
1838
1839 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1840 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1841
1842 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1843 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1844
1845 priv->band_50.band = IEEE80211_BAND_5GHZ;
1846 priv->band_50.channels = priv->channels_50;
1847 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1848 priv->band_50.bitrates = priv->rates_50;
1849 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1850
1851 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1852}
1853
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001854/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001855 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001856 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001857struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001858 struct mwl8k_cmd_pkt header;
1859 __u8 hw_rev;
1860 __u8 host_interface;
1861 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001862 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001863 __le16 region_code;
1864 __le32 fw_rev;
1865 __le32 ps_cookie;
1866 __le32 caps;
1867 __u8 mcs_bitmap[16];
1868 __le32 rx_queue_ptr;
1869 __le32 num_tx_queues;
1870 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1871 __le32 caps2;
1872 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001873 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001874} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001875
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001876#define MWL8K_CAP_MAX_AMSDU 0x20000000
1877#define MWL8K_CAP_GREENFIELD 0x08000000
1878#define MWL8K_CAP_AMPDU 0x04000000
1879#define MWL8K_CAP_RX_STBC 0x01000000
1880#define MWL8K_CAP_TX_STBC 0x00800000
1881#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1882#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1883#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1884#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1885#define MWL8K_CAP_DELAY_BA 0x00003000
1886#define MWL8K_CAP_MIMO 0x00000200
1887#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001888#define MWL8K_CAP_BAND_MASK 0x00000007
1889#define MWL8K_CAP_5GHZ 0x00000004
1890#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001891
Lennert Buytenhek06953232010-01-12 13:49:41 +01001892static void
1893mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1894 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001895{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001896 int rx_streams;
1897 int tx_streams;
1898
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001899 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001900
1901 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001902 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001903 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001904 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001905 if (cap & MWL8K_CAP_AMPDU) {
1906 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001907 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1908 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001909 }
1910 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001911 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001912 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001913 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001914 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001915 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001916 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001917 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001918 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001919 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001920 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001921 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001922
1923 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1924 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1925
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001926 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001927 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001928 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001929 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001930 band->ht_cap.mcs.rx_mask[2] = 0xff;
1931 band->ht_cap.mcs.rx_mask[4] = 0x01;
1932 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001933
1934 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001935 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1936 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001937 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1938 }
1939}
1940
Lennert Buytenhek06953232010-01-12 13:49:41 +01001941static void
1942mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1943{
1944 struct mwl8k_priv *priv = hw->priv;
1945
1946 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1947 mwl8k_setup_2ghz_band(hw);
1948 if (caps & MWL8K_CAP_MIMO)
1949 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1950 }
1951
1952 if (caps & MWL8K_CAP_5GHZ) {
1953 mwl8k_setup_5ghz_band(hw);
1954 if (caps & MWL8K_CAP_MIMO)
1955 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1956 }
1957}
1958
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001959static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001960{
1961 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001962 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001963 int rc;
1964 int i;
1965
1966 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1967 if (cmd == NULL)
1968 return -ENOMEM;
1969
1970 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1971 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1972
1973 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1974 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001975 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001976 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001977 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001978 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001979 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001980 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001981
1982 rc = mwl8k_post_cmd(hw, &cmd->header);
1983
1984 if (!rc) {
1985 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1986 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001987 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001988 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001989 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001990 priv->ap_macids_supported = 0x00000000;
1991 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001992 }
1993
1994 kfree(cmd);
1995 return rc;
1996}
1997
1998/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001999 * CMD_GET_HW_SPEC (AP version).
2000 */
2001struct mwl8k_cmd_get_hw_spec_ap {
2002 struct mwl8k_cmd_pkt header;
2003 __u8 hw_rev;
2004 __u8 host_interface;
2005 __le16 num_wcb;
2006 __le16 num_mcaddrs;
2007 __u8 perm_addr[ETH_ALEN];
2008 __le16 region_code;
2009 __le16 num_antenna;
2010 __le32 fw_rev;
2011 __le32 wcbbase0;
2012 __le32 rxwrptr;
2013 __le32 rxrdptr;
2014 __le32 ps_cookie;
2015 __le32 wcbbase1;
2016 __le32 wcbbase2;
2017 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002018 __le32 fw_api_version;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002019} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002020
2021static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2022{
2023 struct mwl8k_priv *priv = hw->priv;
2024 struct mwl8k_cmd_get_hw_spec_ap *cmd;
2025 int rc;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002026 u32 api_version;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002027
2028 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2029 if (cmd == NULL)
2030 return -ENOMEM;
2031
2032 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2033 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2034
2035 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2036 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2037
2038 rc = mwl8k_post_cmd(hw, &cmd->header);
2039
2040 if (!rc) {
2041 int off;
2042
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002043 api_version = le32_to_cpu(cmd->fw_api_version);
2044 if (priv->device_info->fw_api_ap != api_version) {
2045 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2046 " Expected %d got %d.\n", MWL8K_NAME,
2047 priv->device_info->part_name,
2048 priv->device_info->fw_api_ap,
2049 api_version);
2050 rc = -EINVAL;
2051 goto done;
2052 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002053 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2054 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2055 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2056 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002057 mwl8k_setup_2ghz_band(hw);
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002058 priv->ap_macids_supported = 0x000000ff;
2059 priv->sta_macids_supported = 0x00000000;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002060
2061 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002062 iowrite32(priv->txq[0].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002063
2064 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002065 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002066
2067 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002068 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002069
2070 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002071 iowrite32(priv->txq[1].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002072
2073 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002074 iowrite32(priv->txq[2].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002075
2076 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002077 iowrite32(priv->txq[3].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002078 }
2079
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002080done:
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002081 kfree(cmd);
2082 return rc;
2083}
2084
2085/*
2086 * CMD_SET_HW_SPEC.
2087 */
2088struct mwl8k_cmd_set_hw_spec {
2089 struct mwl8k_cmd_pkt header;
2090 __u8 hw_rev;
2091 __u8 host_interface;
2092 __le16 num_mcaddrs;
2093 __u8 perm_addr[ETH_ALEN];
2094 __le16 region_code;
2095 __le32 fw_rev;
2096 __le32 ps_cookie;
2097 __le32 caps;
2098 __le32 rx_queue_ptr;
2099 __le32 num_tx_queues;
2100 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
2101 __le32 flags;
2102 __le32 num_tx_desc_per_queue;
2103 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002104} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002105
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002106#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2107#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2108#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002109
2110static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2111{
2112 struct mwl8k_priv *priv = hw->priv;
2113 struct mwl8k_cmd_set_hw_spec *cmd;
2114 int rc;
2115 int i;
2116
2117 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2118 if (cmd == NULL)
2119 return -ENOMEM;
2120
2121 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2122 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2123
2124 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2125 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2126 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002127
2128 /*
2129 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2130 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2131 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2132 * priority is interpreted the right way in firmware.
2133 */
2134 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
2135 int j = MWL8K_TX_QUEUES - 1 - i;
2136 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2137 }
2138
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002139 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2140 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2141 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002142 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2143 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2144
2145 rc = mwl8k_post_cmd(hw, &cmd->header);
2146 kfree(cmd);
2147
2148 return rc;
2149}
2150
2151/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002152 * CMD_MAC_MULTICAST_ADR.
2153 */
2154struct mwl8k_cmd_mac_multicast_adr {
2155 struct mwl8k_cmd_pkt header;
2156 __le16 action;
2157 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002158 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002159};
2160
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002161#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2162#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2163#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2164#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002165
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002166static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002167__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002168 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002169{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002170 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002171 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002172 int size;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002173 int mc_count = 0;
2174
2175 if (mc_list)
2176 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002177
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002178 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002179 allmulti = 1;
2180 mc_count = 0;
2181 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002182
2183 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2184
2185 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002186 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002187 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002188
2189 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2190 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002191 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2192 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002193
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002194 if (allmulti) {
2195 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2196 } else if (mc_count) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002197 struct netdev_hw_addr *ha;
2198 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002199
2200 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2201 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002202 netdev_hw_addr_list_for_each(ha, mc_list) {
2203 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002204 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002205 }
2206
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002207 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002208}
2209
2210/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002211 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002212 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002213struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002214 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002215 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002216} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002217
2218#define MWL8K_STAT_ACK_FAILURE 9
2219#define MWL8K_STAT_RTS_FAILURE 12
2220#define MWL8K_STAT_FCS_ERROR 24
2221#define MWL8K_STAT_RTS_SUCCESS 11
2222
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002223static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2224 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002225{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002226 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002227 int rc;
2228
2229 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2230 if (cmd == NULL)
2231 return -ENOMEM;
2232
2233 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2234 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002235
2236 rc = mwl8k_post_cmd(hw, &cmd->header);
2237 if (!rc) {
2238 stats->dot11ACKFailureCount =
2239 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2240 stats->dot11RTSFailureCount =
2241 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2242 stats->dot11FCSErrorCount =
2243 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2244 stats->dot11RTSSuccessCount =
2245 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2246 }
2247 kfree(cmd);
2248
2249 return rc;
2250}
2251
2252/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002253 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002254 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002255struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002256 struct mwl8k_cmd_pkt header;
2257 __le16 action;
2258 __le16 control;
2259 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002260} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002261
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002262static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002263mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002264{
2265 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002266 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002267 int rc;
2268
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002269 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002270 return 0;
2271
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002272 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2273 if (cmd == NULL)
2274 return -ENOMEM;
2275
2276 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2277 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2278 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002279 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002280 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2281
2282 rc = mwl8k_post_cmd(hw, &cmd->header);
2283 kfree(cmd);
2284
2285 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002286 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002287
2288 return rc;
2289}
2290
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002291static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002292{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002293 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002294}
2295
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002296static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002297{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002298 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002299}
2300
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002301static int
2302mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2303{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002304 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002305
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002306 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002307
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002308 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002309}
2310
2311/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002312 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002313 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002314#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002315
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002316struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002317 struct mwl8k_cmd_pkt header;
2318 __le16 action;
2319 __le16 support_level;
2320 __le16 current_level;
2321 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002322 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002323} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002324
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002325static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002326{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002327 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002328 int rc;
2329
2330 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2331 if (cmd == NULL)
2332 return -ENOMEM;
2333
2334 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2335 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2336 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2337 cmd->support_level = cpu_to_le16(dBm);
2338
2339 rc = mwl8k_post_cmd(hw, &cmd->header);
2340 kfree(cmd);
2341
2342 return rc;
2343}
2344
2345/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002346 * CMD_TX_POWER.
2347 */
2348#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2349
2350struct mwl8k_cmd_tx_power {
2351 struct mwl8k_cmd_pkt header;
2352 __le16 action;
2353 __le16 band;
2354 __le16 channel;
2355 __le16 bw;
2356 __le16 sub_ch;
2357 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2358} __attribute__((packed));
2359
2360static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2361 struct ieee80211_conf *conf,
2362 unsigned short pwr)
2363{
2364 struct ieee80211_channel *channel = conf->channel;
2365 struct mwl8k_cmd_tx_power *cmd;
2366 int rc;
2367 int i;
2368
2369 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2370 if (cmd == NULL)
2371 return -ENOMEM;
2372
2373 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2374 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2375 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2376
2377 if (channel->band == IEEE80211_BAND_2GHZ)
2378 cmd->band = cpu_to_le16(0x1);
2379 else if (channel->band == IEEE80211_BAND_5GHZ)
2380 cmd->band = cpu_to_le16(0x4);
2381
2382 cmd->channel = channel->hw_value;
2383
2384 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2385 conf->channel_type == NL80211_CHAN_HT20) {
2386 cmd->bw = cpu_to_le16(0x2);
2387 } else {
2388 cmd->bw = cpu_to_le16(0x4);
2389 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2390 cmd->sub_ch = cpu_to_le16(0x3);
2391 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2392 cmd->sub_ch = cpu_to_le16(0x1);
2393 }
2394
2395 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2396 cmd->power_level_list[i] = cpu_to_le16(pwr);
2397
2398 rc = mwl8k_post_cmd(hw, &cmd->header);
2399 kfree(cmd);
2400
2401 return rc;
2402}
2403
2404/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002405 * CMD_RF_ANTENNA.
2406 */
2407struct mwl8k_cmd_rf_antenna {
2408 struct mwl8k_cmd_pkt header;
2409 __le16 antenna;
2410 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002411} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002412
2413#define MWL8K_RF_ANTENNA_RX 1
2414#define MWL8K_RF_ANTENNA_TX 2
2415
2416static int
2417mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2418{
2419 struct mwl8k_cmd_rf_antenna *cmd;
2420 int rc;
2421
2422 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2423 if (cmd == NULL)
2424 return -ENOMEM;
2425
2426 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2427 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2428 cmd->antenna = cpu_to_le16(antenna);
2429 cmd->mode = cpu_to_le16(mask);
2430
2431 rc = mwl8k_post_cmd(hw, &cmd->header);
2432 kfree(cmd);
2433
2434 return rc;
2435}
2436
2437/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002438 * CMD_SET_BEACON.
2439 */
2440struct mwl8k_cmd_set_beacon {
2441 struct mwl8k_cmd_pkt header;
2442 __le16 beacon_len;
2443 __u8 beacon[0];
2444};
2445
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002446static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2447 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002448{
2449 struct mwl8k_cmd_set_beacon *cmd;
2450 int rc;
2451
2452 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2453 if (cmd == NULL)
2454 return -ENOMEM;
2455
2456 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2457 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2458 cmd->beacon_len = cpu_to_le16(len);
2459 memcpy(cmd->beacon, beacon, len);
2460
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002461 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002462 kfree(cmd);
2463
2464 return rc;
2465}
2466
2467/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002468 * CMD_SET_PRE_SCAN.
2469 */
2470struct mwl8k_cmd_set_pre_scan {
2471 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002472} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002473
2474static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2475{
2476 struct mwl8k_cmd_set_pre_scan *cmd;
2477 int rc;
2478
2479 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2480 if (cmd == NULL)
2481 return -ENOMEM;
2482
2483 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2484 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2485
2486 rc = mwl8k_post_cmd(hw, &cmd->header);
2487 kfree(cmd);
2488
2489 return rc;
2490}
2491
2492/*
2493 * CMD_SET_POST_SCAN.
2494 */
2495struct mwl8k_cmd_set_post_scan {
2496 struct mwl8k_cmd_pkt header;
2497 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002498 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002499} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002500
2501static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002502mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002503{
2504 struct mwl8k_cmd_set_post_scan *cmd;
2505 int rc;
2506
2507 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2508 if (cmd == NULL)
2509 return -ENOMEM;
2510
2511 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2512 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2513 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002514 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002515
2516 rc = mwl8k_post_cmd(hw, &cmd->header);
2517 kfree(cmd);
2518
2519 return rc;
2520}
2521
2522/*
2523 * CMD_SET_RF_CHANNEL.
2524 */
2525struct mwl8k_cmd_set_rf_channel {
2526 struct mwl8k_cmd_pkt header;
2527 __le16 action;
2528 __u8 current_channel;
2529 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002530} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002531
2532static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002533 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002534{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002535 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002536 struct mwl8k_cmd_set_rf_channel *cmd;
2537 int rc;
2538
2539 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2540 if (cmd == NULL)
2541 return -ENOMEM;
2542
2543 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2544 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2545 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2546 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002547
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002548 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002549 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002550 else if (channel->band == IEEE80211_BAND_5GHZ)
2551 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002552
2553 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2554 conf->channel_type == NL80211_CHAN_HT20)
2555 cmd->channel_flags |= cpu_to_le32(0x00000080);
2556 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2557 cmd->channel_flags |= cpu_to_le32(0x000001900);
2558 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2559 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002560
2561 rc = mwl8k_post_cmd(hw, &cmd->header);
2562 kfree(cmd);
2563
2564 return rc;
2565}
2566
2567/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002568 * CMD_SET_AID.
2569 */
2570#define MWL8K_FRAME_PROT_DISABLED 0x00
2571#define MWL8K_FRAME_PROT_11G 0x07
2572#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2573#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2574
2575struct mwl8k_cmd_update_set_aid {
2576 struct mwl8k_cmd_pkt header;
2577 __le16 aid;
2578
2579 /* AP's MAC address (BSSID) */
2580 __u8 bssid[ETH_ALEN];
2581 __le16 protection_mode;
2582 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002583} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002584
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002585static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2586{
2587 int i;
2588 int j;
2589
2590 /*
2591 * Clear nonstandard rates 4 and 13.
2592 */
2593 mask &= 0x1fef;
2594
2595 for (i = 0, j = 0; i < 14; i++) {
2596 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002597 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002598 }
2599}
2600
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002601static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002602mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2603 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002604{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002605 struct mwl8k_cmd_update_set_aid *cmd;
2606 u16 prot_mode;
2607 int rc;
2608
2609 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2610 if (cmd == NULL)
2611 return -ENOMEM;
2612
2613 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2614 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002615 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002616 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002617
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002618 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002619 prot_mode = MWL8K_FRAME_PROT_11G;
2620 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002621 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002622 IEEE80211_HT_OP_MODE_PROTECTION) {
2623 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2624 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2625 break;
2626 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2627 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2628 break;
2629 default:
2630 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2631 break;
2632 }
2633 }
2634 cmd->protection_mode = cpu_to_le16(prot_mode);
2635
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002636 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002637
2638 rc = mwl8k_post_cmd(hw, &cmd->header);
2639 kfree(cmd);
2640
2641 return rc;
2642}
2643
2644/*
2645 * CMD_SET_RATE.
2646 */
2647struct mwl8k_cmd_set_rate {
2648 struct mwl8k_cmd_pkt header;
2649 __u8 legacy_rates[14];
2650
2651 /* Bitmap for supported MCS codes. */
2652 __u8 mcs_set[16];
2653 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002654} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002655
2656static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002657mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002658 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002659{
2660 struct mwl8k_cmd_set_rate *cmd;
2661 int rc;
2662
2663 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2664 if (cmd == NULL)
2665 return -ENOMEM;
2666
2667 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2668 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002669 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002670 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002671
2672 rc = mwl8k_post_cmd(hw, &cmd->header);
2673 kfree(cmd);
2674
2675 return rc;
2676}
2677
2678/*
2679 * CMD_FINALIZE_JOIN.
2680 */
2681#define MWL8K_FJ_BEACON_MAXLEN 128
2682
2683struct mwl8k_cmd_finalize_join {
2684 struct mwl8k_cmd_pkt header;
2685 __le32 sleep_interval; /* Number of beacon periods to sleep */
2686 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002687} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002688
2689static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2690 int framelen, int dtim)
2691{
2692 struct mwl8k_cmd_finalize_join *cmd;
2693 struct ieee80211_mgmt *payload = frame;
2694 int payload_len;
2695 int rc;
2696
2697 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2698 if (cmd == NULL)
2699 return -ENOMEM;
2700
2701 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2702 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2703 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2704
2705 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2706 if (payload_len < 0)
2707 payload_len = 0;
2708 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2709 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2710
2711 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2712
2713 rc = mwl8k_post_cmd(hw, &cmd->header);
2714 kfree(cmd);
2715
2716 return rc;
2717}
2718
2719/*
2720 * CMD_SET_RTS_THRESHOLD.
2721 */
2722struct mwl8k_cmd_set_rts_threshold {
2723 struct mwl8k_cmd_pkt header;
2724 __le16 action;
2725 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002726} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002727
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002728static int
2729mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002730{
2731 struct mwl8k_cmd_set_rts_threshold *cmd;
2732 int rc;
2733
2734 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2735 if (cmd == NULL)
2736 return -ENOMEM;
2737
2738 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2739 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002740 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2741 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002742
2743 rc = mwl8k_post_cmd(hw, &cmd->header);
2744 kfree(cmd);
2745
2746 return rc;
2747}
2748
2749/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002750 * CMD_SET_SLOT.
2751 */
2752struct mwl8k_cmd_set_slot {
2753 struct mwl8k_cmd_pkt header;
2754 __le16 action;
2755 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002756} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002757
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002758static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002759{
2760 struct mwl8k_cmd_set_slot *cmd;
2761 int rc;
2762
2763 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2764 if (cmd == NULL)
2765 return -ENOMEM;
2766
2767 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2768 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2769 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002770 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002771
2772 rc = mwl8k_post_cmd(hw, &cmd->header);
2773 kfree(cmd);
2774
2775 return rc;
2776}
2777
2778/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002779 * CMD_SET_EDCA_PARAMS.
2780 */
2781struct mwl8k_cmd_set_edca_params {
2782 struct mwl8k_cmd_pkt header;
2783
2784 /* See MWL8K_SET_EDCA_XXX below */
2785 __le16 action;
2786
2787 /* TX opportunity in units of 32 us */
2788 __le16 txop;
2789
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002790 union {
2791 struct {
2792 /* Log exponent of max contention period: 0...15 */
2793 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002794
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002795 /* Log exponent of min contention period: 0...15 */
2796 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002797
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002798 /* Adaptive interframe spacing in units of 32us */
2799 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002800
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002801 /* TX queue to configure */
2802 __u8 txq;
2803 } ap;
2804 struct {
2805 /* Log exponent of max contention period: 0...15 */
2806 __u8 log_cw_max;
2807
2808 /* Log exponent of min contention period: 0...15 */
2809 __u8 log_cw_min;
2810
2811 /* Adaptive interframe spacing in units of 32us */
2812 __u8 aifs;
2813
2814 /* TX queue to configure */
2815 __u8 txq;
2816 } sta;
2817 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002818} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002819
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002820#define MWL8K_SET_EDCA_CW 0x01
2821#define MWL8K_SET_EDCA_TXOP 0x02
2822#define MWL8K_SET_EDCA_AIFS 0x04
2823
2824#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2825 MWL8K_SET_EDCA_TXOP | \
2826 MWL8K_SET_EDCA_AIFS)
2827
2828static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002829mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2830 __u16 cw_min, __u16 cw_max,
2831 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002832{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002833 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002834 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002835 int rc;
2836
2837 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2838 if (cmd == NULL)
2839 return -ENOMEM;
2840
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002841 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2842 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002843 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2844 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002845 if (priv->ap_fw) {
2846 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2847 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2848 cmd->ap.aifs = aifs;
2849 cmd->ap.txq = qnum;
2850 } else {
2851 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2852 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2853 cmd->sta.aifs = aifs;
2854 cmd->sta.txq = qnum;
2855 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002856
2857 rc = mwl8k_post_cmd(hw, &cmd->header);
2858 kfree(cmd);
2859
2860 return rc;
2861}
2862
2863/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002864 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002865 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002866struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002867 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002868 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002869} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002870
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002871static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002872{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002873 struct mwl8k_priv *priv = hw->priv;
2874 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002875 int rc;
2876
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002877 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2878 if (cmd == NULL)
2879 return -ENOMEM;
2880
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002881 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002882 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002883 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002884
2885 rc = mwl8k_post_cmd(hw, &cmd->header);
2886 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002887
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002888 if (!rc)
2889 priv->wmm_enabled = enable;
2890
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002891 return rc;
2892}
2893
2894/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002895 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002896 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002897struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002898 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002899 __le32 action;
2900 __u8 rx_antenna_map;
2901 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002902} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002903
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002904static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002905{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002906 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002907 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002908
2909 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2910 if (cmd == NULL)
2911 return -ENOMEM;
2912
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002913 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002914 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002915 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2916 cmd->rx_antenna_map = rx;
2917 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002918
2919 rc = mwl8k_post_cmd(hw, &cmd->header);
2920 kfree(cmd);
2921
2922 return rc;
2923}
2924
2925/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002926 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002927 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002928struct mwl8k_cmd_use_fixed_rate_sta {
2929 struct mwl8k_cmd_pkt header;
2930 __le32 action;
2931 __le32 allow_rate_drop;
2932 __le32 num_rates;
2933 struct {
2934 __le32 is_ht_rate;
2935 __le32 enable_retry;
2936 __le32 rate;
2937 __le32 retry_count;
2938 } rate_entry[8];
2939 __le32 rate_type;
2940 __le32 reserved1;
2941 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002942} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002943
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002944#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002945#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002946
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002947static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002948{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002949 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002950 int rc;
2951
2952 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2953 if (cmd == NULL)
2954 return -ENOMEM;
2955
2956 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2957 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002958 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2959 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002960
2961 rc = mwl8k_post_cmd(hw, &cmd->header);
2962 kfree(cmd);
2963
2964 return rc;
2965}
2966
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002967/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002968 * CMD_USE_FIXED_RATE (AP version).
2969 */
2970struct mwl8k_cmd_use_fixed_rate_ap {
2971 struct mwl8k_cmd_pkt header;
2972 __le32 action;
2973 __le32 allow_rate_drop;
2974 __le32 num_rates;
2975 struct mwl8k_rate_entry_ap {
2976 __le32 is_ht_rate;
2977 __le32 enable_retry;
2978 __le32 rate;
2979 __le32 retry_count;
2980 } rate_entry[4];
2981 u8 multicast_rate;
2982 u8 multicast_rate_type;
2983 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002984} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002985
2986static int
2987mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2988{
2989 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2990 int rc;
2991
2992 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2993 if (cmd == NULL)
2994 return -ENOMEM;
2995
2996 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2997 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2998 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2999 cmd->multicast_rate = mcast;
3000 cmd->management_rate = mgmt;
3001
3002 rc = mwl8k_post_cmd(hw, &cmd->header);
3003 kfree(cmd);
3004
3005 return rc;
3006}
3007
3008/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003009 * CMD_ENABLE_SNIFFER.
3010 */
3011struct mwl8k_cmd_enable_sniffer {
3012 struct mwl8k_cmd_pkt header;
3013 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003014} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003015
3016static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3017{
3018 struct mwl8k_cmd_enable_sniffer *cmd;
3019 int rc;
3020
3021 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3022 if (cmd == NULL)
3023 return -ENOMEM;
3024
3025 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3026 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3027 cmd->action = cpu_to_le32(!!enable);
3028
3029 rc = mwl8k_post_cmd(hw, &cmd->header);
3030 kfree(cmd);
3031
3032 return rc;
3033}
3034
3035/*
3036 * CMD_SET_MAC_ADDR.
3037 */
3038struct mwl8k_cmd_set_mac_addr {
3039 struct mwl8k_cmd_pkt header;
3040 union {
3041 struct {
3042 __le16 mac_type;
3043 __u8 mac_addr[ETH_ALEN];
3044 } mbss;
3045 __u8 mac_addr[ETH_ALEN];
3046 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003047} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003048
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003049#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3050#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3051#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3052#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003053
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003054static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3055 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003056{
3057 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003058 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003059 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003060 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003061 int rc;
3062
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003063 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3064 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3065 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3066 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3067 else
3068 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3069 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3070 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3071 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3072 else
3073 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3074 }
3075
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003076 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3077 if (cmd == NULL)
3078 return -ENOMEM;
3079
3080 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3081 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3082 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003083 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003084 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3085 } else {
3086 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3087 }
3088
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003089 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003090 kfree(cmd);
3091
3092 return rc;
3093}
3094
3095/*
3096 * CMD_SET_RATEADAPT_MODE.
3097 */
3098struct mwl8k_cmd_set_rate_adapt_mode {
3099 struct mwl8k_cmd_pkt header;
3100 __le16 action;
3101 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003102} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003103
3104static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3105{
3106 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3107 int rc;
3108
3109 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3110 if (cmd == NULL)
3111 return -ENOMEM;
3112
3113 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3114 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3115 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3116 cmd->mode = cpu_to_le16(mode);
3117
3118 rc = mwl8k_post_cmd(hw, &cmd->header);
3119 kfree(cmd);
3120
3121 return rc;
3122}
3123
3124/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003125 * CMD_BSS_START.
3126 */
3127struct mwl8k_cmd_bss_start {
3128 struct mwl8k_cmd_pkt header;
3129 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003130} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003131
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003132static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3133 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003134{
3135 struct mwl8k_cmd_bss_start *cmd;
3136 int rc;
3137
3138 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3139 if (cmd == NULL)
3140 return -ENOMEM;
3141
3142 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3143 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3144 cmd->enable = cpu_to_le32(enable);
3145
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003146 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003147 kfree(cmd);
3148
3149 return rc;
3150}
3151
3152/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003153 * CMD_SET_NEW_STN.
3154 */
3155struct mwl8k_cmd_set_new_stn {
3156 struct mwl8k_cmd_pkt header;
3157 __le16 aid;
3158 __u8 mac_addr[6];
3159 __le16 stn_id;
3160 __le16 action;
3161 __le16 rsvd;
3162 __le32 legacy_rates;
3163 __u8 ht_rates[4];
3164 __le16 cap_info;
3165 __le16 ht_capabilities_info;
3166 __u8 mac_ht_param_info;
3167 __u8 rev;
3168 __u8 control_channel;
3169 __u8 add_channel;
3170 __le16 op_mode;
3171 __le16 stbc;
3172 __u8 add_qos_info;
3173 __u8 is_qos_sta;
3174 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003175} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003176
3177#define MWL8K_STA_ACTION_ADD 0
3178#define MWL8K_STA_ACTION_REMOVE 2
3179
3180static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3181 struct ieee80211_vif *vif,
3182 struct ieee80211_sta *sta)
3183{
3184 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003185 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003186 int rc;
3187
3188 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3189 if (cmd == NULL)
3190 return -ENOMEM;
3191
3192 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3193 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3194 cmd->aid = cpu_to_le16(sta->aid);
3195 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3196 cmd->stn_id = cpu_to_le16(sta->aid);
3197 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003198 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3199 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3200 else
3201 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3202 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003203 if (sta->ht_cap.ht_supported) {
3204 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3205 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3206 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3207 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3208 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3209 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3210 ((sta->ht_cap.ampdu_density & 7) << 2);
3211 cmd->is_qos_sta = 1;
3212 }
3213
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003214 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003215 kfree(cmd);
3216
3217 return rc;
3218}
3219
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003220static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3221 struct ieee80211_vif *vif)
3222{
3223 struct mwl8k_cmd_set_new_stn *cmd;
3224 int rc;
3225
3226 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3227 if (cmd == NULL)
3228 return -ENOMEM;
3229
3230 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3231 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3232 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3233
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003234 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003235 kfree(cmd);
3236
3237 return rc;
3238}
3239
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003240static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3241 struct ieee80211_vif *vif, u8 *addr)
3242{
3243 struct mwl8k_cmd_set_new_stn *cmd;
3244 int rc;
3245
3246 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3247 if (cmd == NULL)
3248 return -ENOMEM;
3249
3250 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3251 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3252 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3253 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3254
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003255 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003256 kfree(cmd);
3257
3258 return rc;
3259}
3260
3261/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003262 * CMD_UPDATE_ENCRYPTION.
3263 */
3264
3265#define MAX_ENCR_KEY_LENGTH 16
3266#define MIC_KEY_LENGTH 8
3267
3268struct mwl8k_cmd_update_encryption {
3269 struct mwl8k_cmd_pkt header;
3270
3271 __le32 action;
3272 __le32 reserved;
3273 __u8 mac_addr[6];
3274 __u8 encr_type;
3275
3276} __attribute__((packed));
3277
3278struct mwl8k_cmd_set_key {
3279 struct mwl8k_cmd_pkt header;
3280
3281 __le32 action;
3282 __le32 reserved;
3283 __le16 length;
3284 __le16 key_type_id;
3285 __le32 key_info;
3286 __le32 key_id;
3287 __le16 key_len;
3288 __u8 key_material[MAX_ENCR_KEY_LENGTH];
3289 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
3290 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
3291 __le16 tkip_rsc_low;
3292 __le32 tkip_rsc_high;
3293 __le16 tkip_tsc_low;
3294 __le32 tkip_tsc_high;
3295 __u8 mac_addr[6];
3296} __attribute__((packed));
3297
3298enum {
3299 MWL8K_ENCR_ENABLE,
3300 MWL8K_ENCR_SET_KEY,
3301 MWL8K_ENCR_REMOVE_KEY,
3302 MWL8K_ENCR_SET_GROUP_KEY,
3303};
3304
3305#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
3306#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
3307#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
3308#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
3309#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
3310
3311enum {
3312 MWL8K_ALG_WEP,
3313 MWL8K_ALG_TKIP,
3314 MWL8K_ALG_CCMP,
3315};
3316
3317#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
3318#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
3319#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
3320#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
3321#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
3322
3323static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
3324 struct ieee80211_vif *vif,
3325 u8 *addr,
3326 u8 encr_type)
3327{
3328 struct mwl8k_cmd_update_encryption *cmd;
3329 int rc;
3330
3331 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3332 if (cmd == NULL)
3333 return -ENOMEM;
3334
3335 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3336 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3337 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
3338 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3339 cmd->encr_type = encr_type;
3340
3341 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3342 kfree(cmd);
3343
3344 return rc;
3345}
3346
3347static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
3348 u8 *addr,
3349 struct ieee80211_key_conf *key)
3350{
3351 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3352 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3353 cmd->length = cpu_to_le16(sizeof(*cmd) -
3354 offsetof(struct mwl8k_cmd_set_key, length));
3355 cmd->key_id = cpu_to_le32(key->keyidx);
3356 cmd->key_len = cpu_to_le16(key->keylen);
3357 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3358
3359 switch (key->cipher) {
3360 case WLAN_CIPHER_SUITE_WEP40:
3361 case WLAN_CIPHER_SUITE_WEP104:
3362 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
3363 if (key->keyidx == 0)
3364 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
3365
3366 break;
3367 case WLAN_CIPHER_SUITE_TKIP:
3368 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
3369 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3370 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3371 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3372 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
3373 | MWL8K_KEY_FLAG_TSC_VALID);
3374 break;
3375 case WLAN_CIPHER_SUITE_CCMP:
3376 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
3377 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3378 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3379 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3380 break;
3381 default:
3382 return -ENOTSUPP;
3383 }
3384
3385 return 0;
3386}
3387
3388static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
3389 struct ieee80211_vif *vif,
3390 u8 *addr,
3391 struct ieee80211_key_conf *key)
3392{
3393 struct mwl8k_cmd_set_key *cmd;
3394 int rc;
3395 int keymlen;
3396 u32 action;
3397 u8 idx;
3398 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3399
3400 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3401 if (cmd == NULL)
3402 return -ENOMEM;
3403
3404 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3405 if (rc < 0)
3406 goto done;
3407
3408 idx = key->keyidx;
3409
3410 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3411 action = MWL8K_ENCR_SET_KEY;
3412 else
3413 action = MWL8K_ENCR_SET_GROUP_KEY;
3414
3415 switch (key->cipher) {
3416 case WLAN_CIPHER_SUITE_WEP40:
3417 case WLAN_CIPHER_SUITE_WEP104:
3418 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
3419 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
3420 sizeof(*key) + key->keylen);
3421 mwl8k_vif->wep_key_conf[idx].enabled = 1;
3422 }
3423
3424 keymlen = 0;
3425 action = MWL8K_ENCR_SET_KEY;
3426 break;
3427 case WLAN_CIPHER_SUITE_TKIP:
3428 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
3429 break;
3430 case WLAN_CIPHER_SUITE_CCMP:
3431 keymlen = key->keylen;
3432 break;
3433 default:
3434 rc = -ENOTSUPP;
3435 goto done;
3436 }
3437
3438 memcpy(cmd->key_material, key->key, keymlen);
3439 cmd->action = cpu_to_le32(action);
3440
3441 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3442done:
3443 kfree(cmd);
3444
3445 return rc;
3446}
3447
3448static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
3449 struct ieee80211_vif *vif,
3450 u8 *addr,
3451 struct ieee80211_key_conf *key)
3452{
3453 struct mwl8k_cmd_set_key *cmd;
3454 int rc;
3455 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3456
3457 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3458 if (cmd == NULL)
3459 return -ENOMEM;
3460
3461 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3462 if (rc < 0)
3463 goto done;
3464
3465 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3466 WLAN_CIPHER_SUITE_WEP104)
3467 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
3468
3469 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
3470
3471 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3472done:
3473 kfree(cmd);
3474
3475 return rc;
3476}
3477
3478static int mwl8k_set_key(struct ieee80211_hw *hw,
3479 enum set_key_cmd cmd_param,
3480 struct ieee80211_vif *vif,
3481 struct ieee80211_sta *sta,
3482 struct ieee80211_key_conf *key)
3483{
3484 int rc = 0;
3485 u8 encr_type;
3486 u8 *addr;
3487 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3488
3489 if (vif->type == NL80211_IFTYPE_STATION)
3490 return -EOPNOTSUPP;
3491
3492 if (sta == NULL)
3493 addr = hw->wiphy->perm_addr;
3494 else
3495 addr = sta->addr;
3496
3497 if (cmd_param == SET_KEY) {
3498 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3499 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
3500 if (rc)
3501 goto out;
3502
3503 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
3504 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
3505 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
3506 else
3507 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
3508
3509 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
3510 encr_type);
3511 if (rc)
3512 goto out;
3513
3514 mwl8k_vif->is_hw_crypto_enabled = true;
3515
3516 } else {
3517 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
3518
3519 if (rc)
3520 goto out;
3521
3522 mwl8k_vif->is_hw_crypto_enabled = false;
3523
3524 }
3525out:
3526 return rc;
3527}
3528
3529/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003530 * CMD_UPDATE_STADB.
3531 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003532struct ewc_ht_info {
3533 __le16 control1;
3534 __le16 control2;
3535 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003536} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003537
3538struct peer_capability_info {
3539 /* Peer type - AP vs. STA. */
3540 __u8 peer_type;
3541
3542 /* Basic 802.11 capabilities from assoc resp. */
3543 __le16 basic_caps;
3544
3545 /* Set if peer supports 802.11n high throughput (HT). */
3546 __u8 ht_support;
3547
3548 /* Valid if HT is supported. */
3549 __le16 ht_caps;
3550 __u8 extended_ht_caps;
3551 struct ewc_ht_info ewc_info;
3552
3553 /* Legacy rate table. Intersection of our rates and peer rates. */
3554 __u8 legacy_rates[12];
3555
3556 /* HT rate table. Intersection of our rates and peer rates. */
3557 __u8 ht_rates[16];
3558 __u8 pad[16];
3559
3560 /* If set, interoperability mode, no proprietary extensions. */
3561 __u8 interop;
3562 __u8 pad2;
3563 __u8 station_id;
3564 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003565} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003566
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003567struct mwl8k_cmd_update_stadb {
3568 struct mwl8k_cmd_pkt header;
3569
3570 /* See STADB_ACTION_TYPE */
3571 __le32 action;
3572
3573 /* Peer MAC address */
3574 __u8 peer_addr[ETH_ALEN];
3575
3576 __le32 reserved;
3577
3578 /* Peer info - valid during add/update. */
3579 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003580} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003581
Lennert Buytenheka6804002010-01-04 21:55:42 +01003582#define MWL8K_STA_DB_MODIFY_ENTRY 1
3583#define MWL8K_STA_DB_DEL_ENTRY 2
3584
3585/* Peer Entry flags - used to define the type of the peer node */
3586#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3587
3588static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003589 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003590 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003591{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003592 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003593 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003594 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003595 int rc;
3596
3597 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3598 if (cmd == NULL)
3599 return -ENOMEM;
3600
3601 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3602 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003603 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003604 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003605
Lennert Buytenheka6804002010-01-04 21:55:42 +01003606 p = &cmd->peer_info;
3607 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3608 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003609 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003610 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003611 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3612 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003613 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3614 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3615 else
3616 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3617 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003618 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003619 p->interop = 1;
3620 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003621
Lennert Buytenheka6804002010-01-04 21:55:42 +01003622 rc = mwl8k_post_cmd(hw, &cmd->header);
3623 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003624
Lennert Buytenheka6804002010-01-04 21:55:42 +01003625 return rc ? rc : p->station_id;
3626}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003627
Lennert Buytenheka6804002010-01-04 21:55:42 +01003628static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3629 struct ieee80211_vif *vif, u8 *addr)
3630{
3631 struct mwl8k_cmd_update_stadb *cmd;
3632 int rc;
3633
3634 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3635 if (cmd == NULL)
3636 return -ENOMEM;
3637
3638 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3639 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3640 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3641 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3642
3643 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003644 kfree(cmd);
3645
3646 return rc;
3647}
3648
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003649
3650/*
3651 * Interrupt handling.
3652 */
3653static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3654{
3655 struct ieee80211_hw *hw = dev_id;
3656 struct mwl8k_priv *priv = hw->priv;
3657 u32 status;
3658
3659 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003660 if (!status)
3661 return IRQ_NONE;
3662
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003663 if (status & MWL8K_A2H_INT_TX_DONE) {
3664 status &= ~MWL8K_A2H_INT_TX_DONE;
3665 tasklet_schedule(&priv->poll_tx_task);
3666 }
3667
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003668 if (status & MWL8K_A2H_INT_RX_READY) {
3669 status &= ~MWL8K_A2H_INT_RX_READY;
3670 tasklet_schedule(&priv->poll_rx_task);
3671 }
3672
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003673 if (status)
3674 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003675
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003676 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003677 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003678 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003679 }
3680
3681 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003682 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003683 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003684 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003685 }
3686
3687 return IRQ_HANDLED;
3688}
3689
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003690static void mwl8k_tx_poll(unsigned long data)
3691{
3692 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3693 struct mwl8k_priv *priv = hw->priv;
3694 int limit;
3695 int i;
3696
3697 limit = 32;
3698
3699 spin_lock_bh(&priv->tx_lock);
3700
3701 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3702 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3703
3704 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3705 complete(priv->tx_wait);
3706 priv->tx_wait = NULL;
3707 }
3708
3709 spin_unlock_bh(&priv->tx_lock);
3710
3711 if (limit) {
3712 writel(~MWL8K_A2H_INT_TX_DONE,
3713 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3714 } else {
3715 tasklet_schedule(&priv->poll_tx_task);
3716 }
3717}
3718
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003719static void mwl8k_rx_poll(unsigned long data)
3720{
3721 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3722 struct mwl8k_priv *priv = hw->priv;
3723 int limit;
3724
3725 limit = 32;
3726 limit -= rxq_process(hw, 0, limit);
3727 limit -= rxq_refill(hw, 0, limit);
3728
3729 if (limit) {
3730 writel(~MWL8K_A2H_INT_RX_READY,
3731 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3732 } else {
3733 tasklet_schedule(&priv->poll_rx_task);
3734 }
3735}
3736
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003737
3738/*
3739 * Core driver operations.
3740 */
Johannes Berg7bb45682011-02-24 14:42:06 +01003741static void mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003742{
3743 struct mwl8k_priv *priv = hw->priv;
3744 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003745
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003746 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003747 wiphy_debug(hw->wiphy,
3748 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003749 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003750 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003751 }
3752
Johannes Berg7bb45682011-02-24 14:42:06 +01003753 mwl8k_txq_xmit(hw, index, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003754}
3755
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003756static int mwl8k_start(struct ieee80211_hw *hw)
3757{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003758 struct mwl8k_priv *priv = hw->priv;
3759 int rc;
3760
Joe Perchesa0607fd2009-11-18 23:29:17 -08003761 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003762 IRQF_SHARED, MWL8K_NAME, hw);
3763 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07003764 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003765 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003766 }
3767
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003768 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003769 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003770 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003771
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003772 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003773 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003774
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003775 rc = mwl8k_fw_lock(hw);
3776 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003777 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003778
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003779 if (!priv->ap_fw) {
3780 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003781 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003782
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003783 if (!rc)
3784 rc = mwl8k_cmd_set_pre_scan(hw);
3785
3786 if (!rc)
3787 rc = mwl8k_cmd_set_post_scan(hw,
3788 "\x00\x00\x00\x00\x00\x00");
3789 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003790
3791 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003792 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003793
3794 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003795 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003796
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003797 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003798 }
3799
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003800 if (rc) {
3801 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3802 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003803 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003804 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003805 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003806
3807 return rc;
3808}
3809
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003810static void mwl8k_stop(struct ieee80211_hw *hw)
3811{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003812 struct mwl8k_priv *priv = hw->priv;
3813 int i;
3814
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003815 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003816
3817 ieee80211_stop_queues(hw);
3818
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003819 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003820 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003821 free_irq(priv->pdev->irq, hw);
3822
3823 /* Stop finalize join worker */
3824 cancel_work_sync(&priv->finalize_join_worker);
3825 if (priv->beacon_skb != NULL)
3826 dev_kfree_skb(priv->beacon_skb);
3827
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003828 /* Stop TX reclaim and RX tasklets. */
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 Buytenheka66098d2009-03-10 10:13:33 +01003831
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003832 /* Return all skbs to mac80211 */
3833 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003834 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003835}
3836
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003837static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
3838
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003839static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003840 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003841{
3842 struct mwl8k_priv *priv = hw->priv;
3843 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003844 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003845 int macid, rc;
3846 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003847
3848 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003849 * Reject interface creation if sniffer mode is active, as
3850 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003851 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003852 */
3853 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003854 wiphy_info(hw->wiphy,
3855 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003856 return -EINVAL;
3857 }
3858
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003859 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003860 switch (vif->type) {
3861 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003862 if (!priv->ap_fw && di->fw_image_ap) {
3863 /* we must load the ap fw to meet this request */
3864 if (!list_empty(&priv->vif_list))
3865 return -EBUSY;
3866 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
3867 if (rc)
3868 return rc;
3869 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003870 macids_supported = priv->ap_macids_supported;
3871 break;
3872 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003873 if (priv->ap_fw && di->fw_image_sta) {
3874 /* we must load the sta fw to meet this request */
3875 if (!list_empty(&priv->vif_list))
3876 return -EBUSY;
3877 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
3878 if (rc)
3879 return rc;
3880 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003881 macids_supported = priv->sta_macids_supported;
3882 break;
3883 default:
3884 return -EINVAL;
3885 }
3886
3887 macid = ffs(macids_supported & ~priv->macids_used);
3888 if (!macid--)
3889 return -EBUSY;
3890
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003891 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003892 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003893 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003894 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003895 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003896 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08003897 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
3898 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003899
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003900 /* Set the mac address. */
3901 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3902
3903 if (priv->ap_fw)
3904 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3905
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003906 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003907 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003908
3909 return 0;
3910}
3911
3912static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003913 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003914{
3915 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003916 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003917
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003918 if (priv->ap_fw)
3919 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3920
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003921 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003922
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003923 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003924 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003925}
3926
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003927static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003928{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003929 struct ieee80211_conf *conf = &hw->conf;
3930 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003931 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003932
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003933 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003934 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003935 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003936 }
3937
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003938 rc = mwl8k_fw_lock(hw);
3939 if (rc)
3940 return rc;
3941
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003942 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003943 if (rc)
3944 goto out;
3945
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003946 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003947 if (rc)
3948 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003949
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003950 if (conf->power_level > 18)
3951 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003952
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003953 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003954 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
3955 if (rc)
3956 goto out;
3957
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08003958 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
3959 if (rc)
3960 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
3961 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3962 if (rc)
3963 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
3964
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003965 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003966 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
3967 if (rc)
3968 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003969 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3970 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003971
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003972out:
3973 mwl8k_fw_unlock(hw);
3974
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003975 return rc;
3976}
3977
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003978static void
3979mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3980 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003981{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003982 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003983 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003984 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003985 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003986
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003987 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003988 return;
3989
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003990 /*
3991 * No need to capture a beacon if we're no longer associated.
3992 */
3993 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3994 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003995
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003996 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003997 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003998 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003999 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004000 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004001
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004002 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004003
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004004 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004005 if (ap == NULL) {
4006 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004007 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004008 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004009
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004010 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4011 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4012 } else {
4013 ap_legacy_rates =
4014 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4015 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004016 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004017
4018 rcu_read_unlock();
4019 }
4020
4021 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004022 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004023 if (rc)
4024 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004025
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004026 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004027 if (rc)
4028 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004029 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004030
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004031 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004032 rc = mwl8k_set_radio_preamble(hw,
4033 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004034 if (rc)
4035 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004036 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004037
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004038 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004039 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004040 if (rc)
4041 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004042 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004043
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004044 if (vif->bss_conf.assoc &&
4045 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4046 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004047 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004048 if (rc)
4049 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004050 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004051
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004052 if (vif->bss_conf.assoc &&
4053 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004054 /*
4055 * Finalize the join. Tell rx handler to process
4056 * next beacon from our BSSID.
4057 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004058 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004059 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004060 }
4061
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004062out:
4063 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004064}
4065
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004066static void
4067mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4068 struct ieee80211_bss_conf *info, u32 changed)
4069{
4070 int rc;
4071
4072 if (mwl8k_fw_lock(hw))
4073 return;
4074
4075 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4076 rc = mwl8k_set_radio_preamble(hw,
4077 vif->bss_conf.use_short_preamble);
4078 if (rc)
4079 goto out;
4080 }
4081
4082 if (changed & BSS_CHANGED_BASIC_RATES) {
4083 int idx;
4084 int rate;
4085
4086 /*
4087 * Use lowest supported basic rate for multicasts
4088 * and management frames (such as probe responses --
4089 * beacons will always go out at 1 Mb/s).
4090 */
4091 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004092 if (idx)
4093 idx--;
4094
4095 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4096 rate = mwl8k_rates_24[idx].hw_value;
4097 else
4098 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004099
4100 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4101 }
4102
4103 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4104 struct sk_buff *skb;
4105
4106 skb = ieee80211_beacon_get(hw, vif);
4107 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004108 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004109 kfree_skb(skb);
4110 }
4111 }
4112
4113 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004114 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004115
4116out:
4117 mwl8k_fw_unlock(hw);
4118}
4119
4120static void
4121mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4122 struct ieee80211_bss_conf *info, u32 changed)
4123{
4124 struct mwl8k_priv *priv = hw->priv;
4125
4126 if (!priv->ap_fw)
4127 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
4128 else
4129 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
4130}
4131
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004132static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00004133 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004134{
4135 struct mwl8k_cmd_pkt *cmd;
4136
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004137 /*
4138 * Synthesize and return a command packet that programs the
4139 * hardware multicast address filter. At this point we don't
4140 * know whether FIF_ALLMULTI is being requested, but if it is,
4141 * we'll end up throwing this packet away and creating a new
4142 * one in mwl8k_configure_filter().
4143 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004144 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004145
4146 return (unsigned long)cmd;
4147}
4148
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004149static int
4150mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
4151 unsigned int changed_flags,
4152 unsigned int *total_flags)
4153{
4154 struct mwl8k_priv *priv = hw->priv;
4155
4156 /*
4157 * Hardware sniffer mode is mutually exclusive with STA
4158 * operation, so refuse to enable sniffer mode if a STA
4159 * interface is active.
4160 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004161 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004162 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07004163 wiphy_info(hw->wiphy,
4164 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004165 return 0;
4166 }
4167
4168 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004169 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004170 return 0;
4171 priv->sniffer_enabled = true;
4172 }
4173
4174 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
4175 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
4176 FIF_OTHER_BSS;
4177
4178 return 1;
4179}
4180
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004181static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
4182{
4183 if (!list_empty(&priv->vif_list))
4184 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
4185
4186 return NULL;
4187}
4188
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004189static void mwl8k_configure_filter(struct ieee80211_hw *hw,
4190 unsigned int changed_flags,
4191 unsigned int *total_flags,
4192 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004193{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004194 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004195 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
4196
4197 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02004198 * AP firmware doesn't allow fine-grained control over
4199 * the receive filter.
4200 */
4201 if (priv->ap_fw) {
4202 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
4203 kfree(cmd);
4204 return;
4205 }
4206
4207 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004208 * Enable hardware sniffer mode if FIF_CONTROL or
4209 * FIF_OTHER_BSS is requested.
4210 */
4211 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
4212 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
4213 kfree(cmd);
4214 return;
4215 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004216
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004217 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004218 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004219
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004220 if (mwl8k_fw_lock(hw)) {
4221 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004222 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004223 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004224
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004225 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004226 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004227 priv->sniffer_enabled = false;
4228 }
4229
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004230 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004231 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
4232 /*
4233 * Disable the BSS filter.
4234 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004235 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004236 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004237 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004238 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004239
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004240 /*
4241 * Enable the BSS filter.
4242 *
4243 * If there is an active STA interface, use that
4244 * interface's BSSID, otherwise use a dummy one
4245 * (where the OUI part needs to be nonzero for
4246 * the BSSID to be accepted by POST_SCAN).
4247 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004248 mwl8k_vif = mwl8k_first_vif(priv);
4249 if (mwl8k_vif != NULL)
4250 bssid = mwl8k_vif->vif->bss_conf.bssid;
4251 else
4252 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004253
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004254 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004255 }
4256 }
4257
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004258 /*
4259 * If FIF_ALLMULTI is being requested, throw away the command
4260 * packet that ->prepare_multicast() built and replace it with
4261 * a command packet that enables reception of all multicast
4262 * packets.
4263 */
4264 if (*total_flags & FIF_ALLMULTI) {
4265 kfree(cmd);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004266 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004267 }
4268
4269 if (cmd != NULL) {
4270 mwl8k_post_cmd(hw, cmd);
4271 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004272 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004273
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004274 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004275}
4276
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004277static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4278{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004279 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004280}
4281
Johannes Berg4a6967b2010-02-19 19:18:37 +01004282static int mwl8k_sta_remove(struct ieee80211_hw *hw,
4283 struct ieee80211_vif *vif,
4284 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004285{
4286 struct mwl8k_priv *priv = hw->priv;
4287
Johannes Berg4a6967b2010-02-19 19:18:37 +01004288 if (priv->ap_fw)
4289 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
4290 else
4291 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
4292}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004293
Johannes Berg4a6967b2010-02-19 19:18:37 +01004294static int mwl8k_sta_add(struct ieee80211_hw *hw,
4295 struct ieee80211_vif *vif,
4296 struct ieee80211_sta *sta)
4297{
4298 struct mwl8k_priv *priv = hw->priv;
4299 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004300 int i;
4301 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4302 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004303
Johannes Berg4a6967b2010-02-19 19:18:37 +01004304 if (!priv->ap_fw) {
4305 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
4306 if (ret >= 0) {
4307 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004308 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004309 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01004310
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004311 } else {
4312 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004313 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004314
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004315 for (i = 0; i < NUM_WEP_KEYS; i++) {
4316 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
4317 if (mwl8k_vif->wep_key_conf[i].enabled)
4318 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
4319 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004320 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004321}
4322
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004323static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
4324 const struct ieee80211_tx_queue_params *params)
4325{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004326 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004327 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004328
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004329 rc = mwl8k_fw_lock(hw);
4330 if (!rc) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004331 BUG_ON(queue > MWL8K_TX_QUEUES - 1);
4332 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
4333
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004334 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004335 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004336
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004337 if (!rc) {
4338 int q = MWL8K_TX_QUEUES - 1 - queue;
4339 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004340 params->cw_min,
4341 params->cw_max,
4342 params->aifs,
4343 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004344 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004345
4346 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004347 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004348
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004349 return rc;
4350}
4351
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004352static int mwl8k_get_stats(struct ieee80211_hw *hw,
4353 struct ieee80211_low_level_stats *stats)
4354{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004355 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004356}
4357
John W. Linville0d462bb2010-07-28 14:04:24 -04004358static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
4359 struct survey_info *survey)
4360{
4361 struct mwl8k_priv *priv = hw->priv;
4362 struct ieee80211_conf *conf = &hw->conf;
4363
4364 if (idx != 0)
4365 return -ENOENT;
4366
4367 survey->channel = conf->channel;
4368 survey->filled = SURVEY_INFO_NOISE_DBM;
4369 survey->noise = priv->noise;
4370
4371 return 0;
4372}
4373
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004374static int
4375mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4376 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01004377 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4378 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004379{
4380 switch (action) {
4381 case IEEE80211_AMPDU_RX_START:
4382 case IEEE80211_AMPDU_RX_STOP:
4383 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
4384 return -ENOTSUPP;
4385 return 0;
4386 default:
4387 return -ENOTSUPP;
4388 }
4389}
4390
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004391static const struct ieee80211_ops mwl8k_ops = {
4392 .tx = mwl8k_tx,
4393 .start = mwl8k_start,
4394 .stop = mwl8k_stop,
4395 .add_interface = mwl8k_add_interface,
4396 .remove_interface = mwl8k_remove_interface,
4397 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004398 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004399 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004400 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004401 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004402 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01004403 .sta_add = mwl8k_sta_add,
4404 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004405 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004406 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04004407 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004408 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004409};
4410
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004411static void mwl8k_finalize_join_worker(struct work_struct *work)
4412{
4413 struct mwl8k_priv *priv =
4414 container_of(work, struct mwl8k_priv, finalize_join_worker);
4415 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01004416 struct ieee80211_mgmt *mgmt = (void *)skb->data;
4417 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
4418 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
4419 mgmt->u.beacon.variable, len);
4420 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004421
Johannes Berg56007a02010-01-26 14:19:52 +01004422 if (tim && tim[1] >= 2)
4423 dtim_period = tim[3];
4424
4425 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004426
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004427 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004428 priv->beacon_skb = NULL;
4429}
4430
John W. Linvillebcb628d2009-11-06 16:40:16 -05004431enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004432 MWL8363 = 0,
4433 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004434 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02004435};
4436
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004437#define MWL8K_8366_AP_FW_API 1
4438#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
4439#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
4440
John W. Linvillebcb628d2009-11-06 16:40:16 -05004441static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004442 [MWL8363] = {
4443 .part_name = "88w8363",
4444 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004445 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004446 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004447 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004448 .part_name = "88w8687",
4449 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004450 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05004451 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004452 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004453 .part_name = "88w8366",
4454 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004455 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004456 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
4457 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004458 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004459 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004460};
4461
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004462MODULE_FIRMWARE("mwl8k/helper_8363.fw");
4463MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
4464MODULE_FIRMWARE("mwl8k/helper_8687.fw");
4465MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
4466MODULE_FIRMWARE("mwl8k/helper_8366.fw");
4467MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004468MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004469
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004470static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba12010-03-19 01:46:10 +01004471 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004472 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
4473 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004474 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
4475 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
4476 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01004477 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004478 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004479};
4480MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
4481
Brian Cavagnolo99020472010-11-12 17:23:52 -08004482static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
4483{
4484 int rc;
4485 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
4486 "Trying alternative firmware %s\n", pci_name(priv->pdev),
4487 priv->fw_pref, priv->fw_alt);
4488 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
4489 if (rc) {
4490 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4491 pci_name(priv->pdev), priv->fw_alt);
4492 return rc;
4493 }
4494 return 0;
4495}
4496
4497static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
4498static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
4499{
4500 struct mwl8k_priv *priv = context;
4501 struct mwl8k_device_info *di = priv->device_info;
4502 int rc;
4503
4504 switch (priv->fw_state) {
4505 case FW_STATE_INIT:
4506 if (!fw) {
4507 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
4508 pci_name(priv->pdev), di->helper_image);
4509 goto fail;
4510 }
4511 priv->fw_helper = fw;
4512 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
4513 true);
4514 if (rc && priv->fw_alt) {
4515 rc = mwl8k_request_alt_fw(priv);
4516 if (rc)
4517 goto fail;
4518 priv->fw_state = FW_STATE_LOADING_ALT;
4519 } else if (rc)
4520 goto fail;
4521 else
4522 priv->fw_state = FW_STATE_LOADING_PREF;
4523 break;
4524
4525 case FW_STATE_LOADING_PREF:
4526 if (!fw) {
4527 if (priv->fw_alt) {
4528 rc = mwl8k_request_alt_fw(priv);
4529 if (rc)
4530 goto fail;
4531 priv->fw_state = FW_STATE_LOADING_ALT;
4532 } else
4533 goto fail;
4534 } else {
4535 priv->fw_ucode = fw;
4536 rc = mwl8k_firmware_load_success(priv);
4537 if (rc)
4538 goto fail;
4539 else
4540 complete(&priv->firmware_loading_complete);
4541 }
4542 break;
4543
4544 case FW_STATE_LOADING_ALT:
4545 if (!fw) {
4546 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4547 pci_name(priv->pdev), di->helper_image);
4548 goto fail;
4549 }
4550 priv->fw_ucode = fw;
4551 rc = mwl8k_firmware_load_success(priv);
4552 if (rc)
4553 goto fail;
4554 else
4555 complete(&priv->firmware_loading_complete);
4556 break;
4557
4558 default:
4559 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
4560 MWL8K_NAME, priv->fw_state);
4561 BUG_ON(1);
4562 }
4563
4564 return;
4565
4566fail:
4567 priv->fw_state = FW_STATE_ERROR;
4568 complete(&priv->firmware_loading_complete);
4569 device_release_driver(&priv->pdev->dev);
4570 mwl8k_release_firmware(priv);
4571}
4572
4573static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
4574 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004575{
4576 struct mwl8k_priv *priv = hw->priv;
4577 int rc;
4578
4579 /* Reset firmware and hardware */
4580 mwl8k_hw_reset(priv);
4581
4582 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004583 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004584 if (rc) {
4585 wiphy_err(hw->wiphy, "Firmware files not found\n");
4586 return rc;
4587 }
4588
Brian Cavagnolo99020472010-11-12 17:23:52 -08004589 if (nowait)
4590 return rc;
4591
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004592 /* Load firmware into hardware */
4593 rc = mwl8k_load_firmware(hw);
4594 if (rc)
4595 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4596
4597 /* Reclaim memory once firmware is successfully loaded */
4598 mwl8k_release_firmware(priv);
4599
4600 return rc;
4601}
4602
4603/* initialize hw after successfully loading a firmware image */
4604static int mwl8k_probe_hw(struct ieee80211_hw *hw)
4605{
4606 struct mwl8k_priv *priv = hw->priv;
4607 int rc = 0;
4608 int i;
4609
4610 if (priv->ap_fw) {
4611 priv->rxd_ops = priv->device_info->ap_rxd_ops;
4612 if (priv->rxd_ops == NULL) {
4613 wiphy_err(hw->wiphy,
4614 "Driver does not have AP firmware image support for this hardware\n");
4615 goto err_stop_firmware;
4616 }
4617 } else {
4618 priv->rxd_ops = &rxd_sta_ops;
4619 }
4620
4621 priv->sniffer_enabled = false;
4622 priv->wmm_enabled = false;
4623 priv->pending_tx_pkts = 0;
4624
4625 rc = mwl8k_rxq_init(hw, 0);
4626 if (rc)
4627 goto err_stop_firmware;
4628 rxq_refill(hw, 0, INT_MAX);
4629
4630 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4631 rc = mwl8k_txq_init(hw, i);
4632 if (rc)
4633 goto err_free_queues;
4634 }
4635
4636 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4637 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4638 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
4639 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
4640 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4641
4642 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4643 IRQF_SHARED, MWL8K_NAME, hw);
4644 if (rc) {
4645 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4646 goto err_free_queues;
4647 }
4648
4649 /*
4650 * Temporarily enable interrupts. Initial firmware host
4651 * commands use interrupts and avoid polling. Disable
4652 * interrupts when done.
4653 */
4654 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4655
4656 /* Get config data, mac addrs etc */
4657 if (priv->ap_fw) {
4658 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4659 if (!rc)
4660 rc = mwl8k_cmd_set_hw_spec(hw);
4661 } else {
4662 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4663 }
4664 if (rc) {
4665 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
4666 goto err_free_irq;
4667 }
4668
4669 /* Turn radio off */
4670 rc = mwl8k_cmd_radio_disable(hw);
4671 if (rc) {
4672 wiphy_err(hw->wiphy, "Cannot disable\n");
4673 goto err_free_irq;
4674 }
4675
4676 /* Clear MAC address */
4677 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
4678 if (rc) {
4679 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
4680 goto err_free_irq;
4681 }
4682
4683 /* Disable interrupts */
4684 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4685 free_irq(priv->pdev->irq, hw);
4686
4687 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
4688 priv->device_info->part_name,
4689 priv->hw_rev, hw->wiphy->perm_addr,
4690 priv->ap_fw ? "AP" : "STA",
4691 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4692 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
4693
4694 return 0;
4695
4696err_free_irq:
4697 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4698 free_irq(priv->pdev->irq, hw);
4699
4700err_free_queues:
4701 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4702 mwl8k_txq_deinit(hw, i);
4703 mwl8k_rxq_deinit(hw, 0);
4704
4705err_stop_firmware:
4706 mwl8k_hw_reset(priv);
4707
4708 return rc;
4709}
4710
4711/*
4712 * invoke mwl8k_reload_firmware to change the firmware image after the device
4713 * has already been registered
4714 */
4715static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
4716{
4717 int i, rc = 0;
4718 struct mwl8k_priv *priv = hw->priv;
4719
4720 mwl8k_stop(hw);
4721 mwl8k_rxq_deinit(hw, 0);
4722
4723 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4724 mwl8k_txq_deinit(hw, i);
4725
Brian Cavagnolo99020472010-11-12 17:23:52 -08004726 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004727 if (rc)
4728 goto fail;
4729
4730 rc = mwl8k_probe_hw(hw);
4731 if (rc)
4732 goto fail;
4733
4734 rc = mwl8k_start(hw);
4735 if (rc)
4736 goto fail;
4737
4738 rc = mwl8k_config(hw, ~0);
4739 if (rc)
4740 goto fail;
4741
4742 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4743 rc = mwl8k_conf_tx(hw, i, &priv->wmm_params[i]);
4744 if (rc)
4745 goto fail;
4746 }
4747
4748 return rc;
4749
4750fail:
4751 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
4752 return rc;
4753}
4754
4755static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
4756{
4757 struct ieee80211_hw *hw = priv->hw;
4758 int i, rc;
4759
Brian Cavagnolo99020472010-11-12 17:23:52 -08004760 rc = mwl8k_load_firmware(hw);
4761 mwl8k_release_firmware(priv);
4762 if (rc) {
4763 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4764 return rc;
4765 }
4766
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004767 /*
4768 * Extra headroom is the size of the required DMA header
4769 * minus the size of the smallest 802.11 frame (CTS frame).
4770 */
4771 hw->extra_tx_headroom =
4772 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
4773
4774 hw->channel_change_time = 10;
4775
4776 hw->queues = MWL8K_TX_QUEUES;
4777
4778 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08004779 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004780 hw->vif_data_size = sizeof(struct mwl8k_vif);
4781 hw->sta_data_size = sizeof(struct mwl8k_sta);
4782
4783 priv->macids_used = 0;
4784 INIT_LIST_HEAD(&priv->vif_list);
4785
4786 /* Set default radio state and preamble */
4787 priv->radio_on = 0;
4788 priv->radio_short_preamble = 0;
4789
4790 /* Finalize join worker */
4791 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4792
4793 /* TX reclaim and RX tasklets. */
4794 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4795 tasklet_disable(&priv->poll_tx_task);
4796 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4797 tasklet_disable(&priv->poll_rx_task);
4798
4799 /* Power management cookie */
4800 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4801 if (priv->cookie == NULL)
4802 return -ENOMEM;
4803
4804 mutex_init(&priv->fw_mutex);
4805 priv->fw_mutex_owner = NULL;
4806 priv->fw_mutex_depth = 0;
4807 priv->hostcmd_wait = NULL;
4808
4809 spin_lock_init(&priv->tx_lock);
4810
4811 priv->tx_wait = NULL;
4812
4813 rc = mwl8k_probe_hw(hw);
4814 if (rc)
4815 goto err_free_cookie;
4816
4817 hw->wiphy->interface_modes = 0;
4818 if (priv->ap_macids_supported || priv->device_info->fw_image_ap)
4819 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4820 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
4821 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4822
4823 rc = ieee80211_register_hw(hw);
4824 if (rc) {
4825 wiphy_err(hw->wiphy, "Cannot register device\n");
4826 goto err_unprobe_hw;
4827 }
4828
4829 return 0;
4830
4831err_unprobe_hw:
4832 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4833 mwl8k_txq_deinit(hw, i);
4834 mwl8k_rxq_deinit(hw, 0);
4835
4836err_free_cookie:
4837 if (priv->cookie != NULL)
4838 pci_free_consistent(priv->pdev, 4,
4839 priv->cookie, priv->cookie_dma);
4840
4841 return rc;
4842}
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004843static int __devinit mwl8k_probe(struct pci_dev *pdev,
4844 const struct pci_device_id *id)
4845{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004846 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004847 struct ieee80211_hw *hw;
4848 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004849 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004850 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004851
4852 if (!printed_version) {
4853 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
4854 printed_version = 1;
4855 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004856
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004857
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004858 rc = pci_enable_device(pdev);
4859 if (rc) {
4860 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
4861 MWL8K_NAME);
4862 return rc;
4863 }
4864
4865 rc = pci_request_regions(pdev, MWL8K_NAME);
4866 if (rc) {
4867 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
4868 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004869 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004870 }
4871
4872 pci_set_master(pdev);
4873
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004874
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004875 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
4876 if (hw == NULL) {
4877 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
4878 rc = -ENOMEM;
4879 goto err_free_reg;
4880 }
4881
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004882 SET_IEEE80211_DEV(hw, &pdev->dev);
4883 pci_set_drvdata(pdev, hw);
4884
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004885 priv = hw->priv;
4886 priv->hw = hw;
4887 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05004888 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004889
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004890
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004891 priv->sram = pci_iomap(pdev, 0, 0x10000);
4892 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004893 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004894 goto err_iounmap;
4895 }
4896
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004897 /*
4898 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
4899 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
4900 */
4901 priv->regs = pci_iomap(pdev, 1, 0x10000);
4902 if (priv->regs == NULL) {
4903 priv->regs = pci_iomap(pdev, 2, 0x10000);
4904 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004905 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004906 goto err_iounmap;
4907 }
4908 }
4909
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004910 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08004911 * Choose the initial fw image depending on user input. If a second
4912 * image is available, make it the alternative image that will be
4913 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004914 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004915 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004916 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08004917 if (ap_mode_default && di->fw_image_ap) {
4918 priv->fw_pref = di->fw_image_ap;
4919 priv->fw_alt = di->fw_image_sta;
4920 } else if (!ap_mode_default && di->fw_image_sta) {
4921 priv->fw_pref = di->fw_image_sta;
4922 priv->fw_alt = di->fw_image_ap;
4923 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004924 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08004925 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004926 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
4927 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08004928 priv->fw_pref = di->fw_image_ap;
4929 }
4930 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004931 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004932 goto err_stop_firmware;
Brian Cavagnolo99020472010-11-12 17:23:52 -08004933 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004934
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004935err_stop_firmware:
4936 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004937
4938err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004939 if (priv->regs != NULL)
4940 pci_iounmap(pdev, priv->regs);
4941
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004942 if (priv->sram != NULL)
4943 pci_iounmap(pdev, priv->sram);
4944
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004945 pci_set_drvdata(pdev, NULL);
4946 ieee80211_free_hw(hw);
4947
4948err_free_reg:
4949 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004950
4951err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004952 pci_disable_device(pdev);
4953
4954 return rc;
4955}
4956
Joerg Albert230f7af2009-04-18 02:10:45 +02004957static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004958{
4959 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4960}
4961
Joerg Albert230f7af2009-04-18 02:10:45 +02004962static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004963{
4964 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4965 struct mwl8k_priv *priv;
4966 int i;
4967
4968 if (hw == NULL)
4969 return;
4970 priv = hw->priv;
4971
Brian Cavagnolo99020472010-11-12 17:23:52 -08004972 wait_for_completion(&priv->firmware_loading_complete);
4973
4974 if (priv->fw_state == FW_STATE_ERROR) {
4975 mwl8k_hw_reset(priv);
4976 goto unmap;
4977 }
4978
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004979 ieee80211_stop_queues(hw);
4980
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004981 ieee80211_unregister_hw(hw);
4982
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004983 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004984 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004985 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004986
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004987 /* Stop hardware */
4988 mwl8k_hw_reset(priv);
4989
4990 /* Return all skbs to mac80211 */
4991 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004992 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004993
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004994 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4995 mwl8k_txq_deinit(hw, i);
4996
4997 mwl8k_rxq_deinit(hw, 0);
4998
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004999 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005000
Brian Cavagnolo99020472010-11-12 17:23:52 -08005001unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005002 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005003 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005004 pci_set_drvdata(pdev, NULL);
5005 ieee80211_free_hw(hw);
5006 pci_release_regions(pdev);
5007 pci_disable_device(pdev);
5008}
5009
5010static struct pci_driver mwl8k_driver = {
5011 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005012 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005013 .probe = mwl8k_probe,
5014 .remove = __devexit_p(mwl8k_remove),
5015 .shutdown = __devexit_p(mwl8k_shutdown),
5016};
5017
5018static int __init mwl8k_init(void)
5019{
5020 return pci_register_driver(&mwl8k_driver);
5021}
5022
5023static void __exit mwl8k_exit(void)
5024{
5025 pci_unregister_driver(&mwl8k_driver);
5026}
5027
5028module_init(mwl8k_init);
5029module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005030
5031MODULE_DESCRIPTION(MWL8K_DESC);
5032MODULE_VERSION(MWL8K_VERSION);
5033MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
5034MODULE_LICENSE("GPL");