blob: c1ceb4b23971ea884577bb876432e4a764301446 [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;
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +0530140 int irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100141
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200142 struct mwl8k_device_info *device_info;
143
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100144 void __iomem *sram;
145 void __iomem *regs;
146
147 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800148 const struct firmware *fw_helper;
149 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100150
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100151 /* hardware/firmware parameters */
152 bool ap_fw;
153 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100154 struct ieee80211_supported_band band_24;
155 struct ieee80211_channel channels_24[14];
156 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100157 struct ieee80211_supported_band band_50;
158 struct ieee80211_channel channels_50[4];
159 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100160 u32 ap_macids_supported;
161 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100162
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200163 /* firmware access */
164 struct mutex fw_mutex;
165 struct task_struct *fw_mutex_owner;
166 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200167 struct completion *hostcmd_wait;
168
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100169 /* lock held over TX and TX reap */
170 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100171
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200172 /* TX quiesce completion, protected by fw_mutex and tx_lock */
173 struct completion *tx_wait;
174
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100175 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100176 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100177 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100178
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100179 /* power management status cookie from firmware */
180 u32 *cookie;
181 dma_addr_t cookie_dma;
182
183 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100184 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200185 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100186
187 /*
188 * Running count of TX packets in flight, to avoid
189 * iterating over the transmit rings each time.
190 */
191 int pending_tx_pkts;
192
193 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
194 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
195
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200196 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200197 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200198 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200199 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100200
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100201 /* XXX need to convert this to handle multiple interfaces */
202 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200203 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100204 struct sk_buff *beacon_skb;
205
206 /*
207 * This FJ worker has to be global as it is scheduled from the
208 * RX handler. At this point we don't know which interface it
209 * belongs to until the list of bssids waiting to complete join
210 * is checked.
211 */
212 struct work_struct finalize_join_worker;
213
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100214 /* Tasklet to perform TX reclaim. */
215 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100216
217 /* Tasklet to perform RX. */
218 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400219
220 /* Most recently reported noise in dBm */
221 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800222
223 /*
224 * preserve the queue configurations so they can be restored if/when
225 * the firmware image is swapped.
226 */
227 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800228
229 /* async firmware loading state */
230 unsigned fw_state;
231 char *fw_pref;
232 char *fw_alt;
233 struct completion firmware_loading_complete;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100234};
235
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800236#define MAX_WEP_KEY_LEN 13
237#define NUM_WEP_KEYS 4
238
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100239/* Per interface specific private data */
240struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100241 struct list_head list;
242 struct ieee80211_vif *vif;
243
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100244 /* Firmware macid for this vif. */
245 int macid;
246
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100247 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100248 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800249
250 /* Saved WEP keys */
251 struct {
252 u8 enabled;
253 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
254 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800255
256 /* BSSID */
257 u8 bssid[ETH_ALEN];
258
259 /* A flag to indicate is HW crypto is enabled for this bssid */
260 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100261};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200262#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800263#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100264
Lennert Buytenheka6804002010-01-04 21:55:42 +0100265struct mwl8k_sta {
266 /* Index into station database. Returned by UPDATE_STADB. */
267 u8 peer_id;
268};
269#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
270
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100271static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100272 { .center_freq = 2412, .hw_value = 1, },
273 { .center_freq = 2417, .hw_value = 2, },
274 { .center_freq = 2422, .hw_value = 3, },
275 { .center_freq = 2427, .hw_value = 4, },
276 { .center_freq = 2432, .hw_value = 5, },
277 { .center_freq = 2437, .hw_value = 6, },
278 { .center_freq = 2442, .hw_value = 7, },
279 { .center_freq = 2447, .hw_value = 8, },
280 { .center_freq = 2452, .hw_value = 9, },
281 { .center_freq = 2457, .hw_value = 10, },
282 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100283 { .center_freq = 2467, .hw_value = 12, },
284 { .center_freq = 2472, .hw_value = 13, },
285 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100286};
287
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100288static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100289 { .bitrate = 10, .hw_value = 2, },
290 { .bitrate = 20, .hw_value = 4, },
291 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200292 { .bitrate = 110, .hw_value = 22, },
293 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100294 { .bitrate = 60, .hw_value = 12, },
295 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100296 { .bitrate = 120, .hw_value = 24, },
297 { .bitrate = 180, .hw_value = 36, },
298 { .bitrate = 240, .hw_value = 48, },
299 { .bitrate = 360, .hw_value = 72, },
300 { .bitrate = 480, .hw_value = 96, },
301 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100302 { .bitrate = 720, .hw_value = 144, },
303};
304
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100305static const struct ieee80211_channel mwl8k_channels_50[] = {
306 { .center_freq = 5180, .hw_value = 36, },
307 { .center_freq = 5200, .hw_value = 40, },
308 { .center_freq = 5220, .hw_value = 44, },
309 { .center_freq = 5240, .hw_value = 48, },
310};
311
312static const struct ieee80211_rate mwl8k_rates_50[] = {
313 { .bitrate = 60, .hw_value = 12, },
314 { .bitrate = 90, .hw_value = 18, },
315 { .bitrate = 120, .hw_value = 24, },
316 { .bitrate = 180, .hw_value = 36, },
317 { .bitrate = 240, .hw_value = 48, },
318 { .bitrate = 360, .hw_value = 72, },
319 { .bitrate = 480, .hw_value = 96, },
320 { .bitrate = 540, .hw_value = 108, },
321 { .bitrate = 720, .hw_value = 144, },
322};
323
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100324/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100325#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800326#define MWL8K_CMD_SET 0x0001
327#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100328
329/* Firmware command codes */
330#define MWL8K_CMD_CODE_DNLD 0x0001
331#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200332#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100333#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
334#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200335#define MWL8K_CMD_RADIO_CONTROL 0x001c
336#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800337#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200338#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100339#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100340#define MWL8K_CMD_SET_PRE_SCAN 0x0107
341#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200342#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100343#define MWL8K_CMD_SET_AID 0x010d
344#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200345#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100346#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200347#define MWL8K_CMD_SET_SLOT 0x0114
348#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
349#define MWL8K_CMD_SET_WMM_MODE 0x0123
350#define MWL8K_CMD_MIMO_CONFIG 0x0125
351#define MWL8K_CMD_USE_FIXED_RATE 0x0126
352#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100353#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200354#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100355#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
356#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800357#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200358#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100359
John W. Linvilleb6037422010-07-20 13:55:00 -0400360static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100361{
John W. Linvilleb6037422010-07-20 13:55:00 -0400362 u16 command = le16_to_cpu(cmd);
363
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100364#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
365 snprintf(buf, bufsize, "%s", #x);\
366 return buf;\
367 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400368 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100369 MWL8K_CMDNAME(CODE_DNLD);
370 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200371 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100372 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
373 MWL8K_CMDNAME(GET_STAT);
374 MWL8K_CMDNAME(RADIO_CONTROL);
375 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800376 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200377 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100378 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100379 MWL8K_CMDNAME(SET_PRE_SCAN);
380 MWL8K_CMDNAME(SET_POST_SCAN);
381 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100382 MWL8K_CMDNAME(SET_AID);
383 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200384 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100385 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200386 MWL8K_CMDNAME(SET_SLOT);
387 MWL8K_CMDNAME(SET_EDCA_PARAMS);
388 MWL8K_CMDNAME(SET_WMM_MODE);
389 MWL8K_CMDNAME(MIMO_CONFIG);
390 MWL8K_CMDNAME(USE_FIXED_RATE);
391 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200392 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200393 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100394 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100395 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800396 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200397 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100398 default:
399 snprintf(buf, bufsize, "0x%x", cmd);
400 }
401#undef MWL8K_CMDNAME
402
403 return buf;
404}
405
406/* Hardware and firmware reset */
407static void mwl8k_hw_reset(struct mwl8k_priv *priv)
408{
409 iowrite32(MWL8K_H2A_INT_RESET,
410 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
411 iowrite32(MWL8K_H2A_INT_RESET,
412 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
413 msleep(20);
414}
415
416/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800417static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100418{
419 if (*fw == NULL)
420 return;
421 release_firmware(*fw);
422 *fw = NULL;
423}
424
425static void mwl8k_release_firmware(struct mwl8k_priv *priv)
426{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100427 mwl8k_release_fw(&priv->fw_ucode);
428 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100429}
430
Brian Cavagnolo99020472010-11-12 17:23:52 -0800431/* states for asynchronous f/w loading */
432static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
433enum {
434 FW_STATE_INIT = 0,
435 FW_STATE_LOADING_PREF,
436 FW_STATE_LOADING_ALT,
437 FW_STATE_ERROR,
438};
439
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100440/* Request fw image */
441static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800442 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800443 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100444{
445 /* release current image */
446 if (*fw != NULL)
447 mwl8k_release_fw(fw);
448
Brian Cavagnolo99020472010-11-12 17:23:52 -0800449 if (nowait)
450 return request_firmware_nowait(THIS_MODULE, 1, fname,
451 &priv->pdev->dev, GFP_KERNEL,
452 priv, mwl8k_fw_state_machine);
453 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800454 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100455}
456
Brian Cavagnolo99020472010-11-12 17:23:52 -0800457static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
458 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100459{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200460 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100461 int rc;
462
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200463 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800464 if (nowait)
465 rc = mwl8k_request_fw(priv, di->helper_image,
466 &priv->fw_helper, true);
467 else
468 rc = mwl8k_request_fw(priv, di->helper_image,
469 &priv->fw_helper, false);
470 if (rc)
471 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
472 pci_name(priv->pdev), di->helper_image);
473
474 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200475 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100476 }
477
Brian Cavagnolo99020472010-11-12 17:23:52 -0800478 if (nowait) {
479 /*
480 * if we get here, no helper image is needed. Skip the
481 * FW_STATE_INIT state.
482 */
483 priv->fw_state = FW_STATE_LOADING_PREF;
484 rc = mwl8k_request_fw(priv, fw_image,
485 &priv->fw_ucode,
486 true);
487 } else
488 rc = mwl8k_request_fw(priv, fw_image,
489 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100490 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200491 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800492 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100493 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100494 return rc;
495 }
496
497 return 0;
498}
499
500struct mwl8k_cmd_pkt {
501 __le16 code;
502 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100503 __u8 seq_num;
504 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100505 __le16 result;
506 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000507} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100508
509/*
510 * Firmware loading.
511 */
512static int
513mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
514{
515 void __iomem *regs = priv->regs;
516 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100517 int loops;
518
519 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
520 if (pci_dma_mapping_error(priv->pdev, dma_addr))
521 return -ENOMEM;
522
523 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
524 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
525 iowrite32(MWL8K_H2A_INT_DOORBELL,
526 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
527 iowrite32(MWL8K_H2A_INT_DUMMY,
528 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
529
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100530 loops = 1000;
531 do {
532 u32 int_code;
533
534 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
535 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
536 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100537 break;
538 }
539
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200540 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100541 udelay(1);
542 } while (--loops);
543
544 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
545
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200546 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100547}
548
549static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
550 const u8 *data, size_t length)
551{
552 struct mwl8k_cmd_pkt *cmd;
553 int done;
554 int rc = 0;
555
556 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
557 if (cmd == NULL)
558 return -ENOMEM;
559
560 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
561 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100562 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100563 cmd->result = 0;
564
565 done = 0;
566 while (length) {
567 int block_size = length > 256 ? 256 : length;
568
569 memcpy(cmd->payload, data + done, block_size);
570 cmd->length = cpu_to_le16(block_size);
571
572 rc = mwl8k_send_fw_load_cmd(priv, cmd,
573 sizeof(*cmd) + block_size);
574 if (rc)
575 break;
576
577 done += block_size;
578 length -= block_size;
579 }
580
581 if (!rc) {
582 cmd->length = 0;
583 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
584 }
585
586 kfree(cmd);
587
588 return rc;
589}
590
591static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
592 const u8 *data, size_t length)
593{
594 unsigned char *buffer;
595 int may_continue, rc = 0;
596 u32 done, prev_block_size;
597
598 buffer = kmalloc(1024, GFP_KERNEL);
599 if (buffer == NULL)
600 return -ENOMEM;
601
602 done = 0;
603 prev_block_size = 0;
604 may_continue = 1000;
605 while (may_continue > 0) {
606 u32 block_size;
607
608 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
609 if (block_size & 1) {
610 block_size &= ~1;
611 may_continue--;
612 } else {
613 done += prev_block_size;
614 length -= prev_block_size;
615 }
616
617 if (block_size > 1024 || block_size > length) {
618 rc = -EOVERFLOW;
619 break;
620 }
621
622 if (length == 0) {
623 rc = 0;
624 break;
625 }
626
627 if (block_size == 0) {
628 rc = -EPROTO;
629 may_continue--;
630 udelay(1);
631 continue;
632 }
633
634 prev_block_size = block_size;
635 memcpy(buffer, data + done, block_size);
636
637 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
638 if (rc)
639 break;
640 }
641
642 if (!rc && length != 0)
643 rc = -EREMOTEIO;
644
645 kfree(buffer);
646
647 return rc;
648}
649
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200650static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100651{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200652 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800653 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200654 int rc;
655 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100656
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200657 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800658 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100659
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200660 if (helper == NULL) {
661 printk(KERN_ERR "%s: helper image needed but none "
662 "given\n", pci_name(priv->pdev));
663 return -EINVAL;
664 }
665
666 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100667 if (rc) {
668 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200669 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100670 return rc;
671 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100672 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100673
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200674 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100675 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200676 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100677 }
678
679 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200680 printk(KERN_ERR "%s: unable to load firmware image\n",
681 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100682 return rc;
683 }
684
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100685 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100686
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100687 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100688 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200689 u32 ready_code;
690
691 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
692 if (ready_code == MWL8K_FWAP_READY) {
693 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100694 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200695 } else if (ready_code == MWL8K_FWSTA_READY) {
696 priv->ap_fw = 0;
697 break;
698 }
699
700 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100701 udelay(1);
702 } while (--loops);
703
704 return loops ? 0 : -ETIMEDOUT;
705}
706
707
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100708/* DMA header used by firmware and hardware. */
709struct mwl8k_dma_data {
710 __le16 fwlen;
711 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100712 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000713} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100714
715/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100716static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100717{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100718 struct mwl8k_dma_data *tr;
719 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100720
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100721 tr = (struct mwl8k_dma_data *)skb->data;
722 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
723
724 if (hdrlen != sizeof(tr->wh)) {
725 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
726 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
727 *((__le16 *)(tr->data - 2)) = qos;
728 } else {
729 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
730 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100731 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100732
733 if (hdrlen != sizeof(*tr))
734 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100735}
736
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800737static void
738mwl8k_add_dma_header(struct sk_buff *skb, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100739{
740 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100741 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800742 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100743 struct mwl8k_dma_data *tr;
744
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100745 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100746 * Add a firmware DMA header; the firmware requires that we
747 * present a 2-byte payload length followed by a 4-address
748 * header (without QoS field), followed (optionally) by any
749 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100750 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100751 wh = (struct ieee80211_hdr *)skb->data;
752
753 hdrlen = ieee80211_hdrlen(wh->frame_control);
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800754 reqd_hdrlen = sizeof(*tr);
755
756 if (hdrlen != reqd_hdrlen)
757 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100758
759 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800760 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100761
762 tr = (struct mwl8k_dma_data *)skb->data;
763 if (wh != &tr->wh)
764 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100765 if (hdrlen != sizeof(tr->wh))
766 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100767
768 /*
769 * Firmware length is the length of the fully formed "802.11
770 * payload". That is, everything except for the 802.11 header.
771 * This includes all crypto material including the MIC.
772 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800773 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100774}
775
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800776static void mwl8k_encapsulate_tx_frame(struct sk_buff *skb)
777{
778 struct ieee80211_hdr *wh;
779 struct ieee80211_tx_info *tx_info;
780 struct ieee80211_key_conf *key_conf;
781 int data_pad;
782
783 wh = (struct ieee80211_hdr *)skb->data;
784
785 tx_info = IEEE80211_SKB_CB(skb);
786
787 key_conf = NULL;
788 if (ieee80211_is_data(wh->frame_control))
789 key_conf = tx_info->control.hw_key;
790
791 /*
792 * Make sure the packet header is in the DMA header format (4-address
793 * without QoS), the necessary crypto padding between the header and the
794 * payload has already been provided by mac80211, but it doesn't add tail
795 * padding when HW crypto is enabled.
796 *
797 * We have the following trailer padding requirements:
798 * - WEP: 4 trailer bytes (ICV)
799 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
800 * - CCMP: 8 trailer bytes (MIC)
801 */
802 data_pad = 0;
803 if (key_conf != NULL) {
804 switch (key_conf->cipher) {
805 case WLAN_CIPHER_SUITE_WEP40:
806 case WLAN_CIPHER_SUITE_WEP104:
807 data_pad = 4;
808 break;
809 case WLAN_CIPHER_SUITE_TKIP:
810 data_pad = 12;
811 break;
812 case WLAN_CIPHER_SUITE_CCMP:
813 data_pad = 8;
814 break;
815 }
816 }
817 mwl8k_add_dma_header(skb, data_pad);
818}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100819
820/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100821 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200822 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100823struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200824 __le16 pkt_len;
825 __u8 sq2;
826 __u8 rate;
827 __le32 pkt_phys_addr;
828 __le32 next_rxd_phys_addr;
829 __le16 qos_control;
830 __le16 htsig2;
831 __le32 hw_rssi_info;
832 __le32 hw_noise_floor_info;
833 __u8 noise_floor;
834 __u8 pad0[3];
835 __u8 rssi;
836 __u8 rx_status;
837 __u8 channel;
838 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000839} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200840
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100841#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
842#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
843#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100844
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100845#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200846
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800847/* 8366 AP rx_status bits */
848#define MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
849#define MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
850#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
851#define MWL8K_8366_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
852#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
853
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100854static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200855{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100856 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200857
858 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100859 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200860}
861
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100862static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200863{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100864 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200865
866 rxd->pkt_len = cpu_to_le16(len);
867 rxd->pkt_phys_addr = cpu_to_le32(addr);
868 wmb();
869 rxd->rx_ctrl = 0;
870}
871
872static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100873mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400874 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200875{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100876 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200877
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100878 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200879 return -1;
880 rmb();
881
882 memset(status, 0, sizeof(*status));
883
884 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400885 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200886
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100887 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200888 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100889 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100890 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100891 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200892 } else {
893 int i;
894
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100895 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
896 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200897 status->rate_idx = i;
898 break;
899 }
900 }
901 }
902
Lennert Buytenhek85478342010-01-12 13:48:56 +0100903 if (rxd->channel > 14) {
904 status->band = IEEE80211_BAND_5GHZ;
905 if (!(status->flag & RX_FLAG_HT))
906 status->rate_idx -= 5;
907 } else {
908 status->band = IEEE80211_BAND_2GHZ;
909 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900910 status->freq = ieee80211_channel_to_frequency(rxd->channel,
911 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200912
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100913 *qos = rxd->qos_control;
914
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800915 if ((rxd->rx_status != MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
916 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK) &&
917 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
918 status->flag |= RX_FLAG_MMIC_ERROR;
919
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200920 return le16_to_cpu(rxd->pkt_len);
921}
922
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100923static struct rxd_ops rxd_8366_ap_ops = {
924 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
925 .rxd_init = mwl8k_rxd_8366_ap_init,
926 .rxd_refill = mwl8k_rxd_8366_ap_refill,
927 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200928};
929
930/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100931 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100932 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100933struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100934 __le16 pkt_len;
935 __u8 link_quality;
936 __u8 noise_level;
937 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200938 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100939 __le16 qos_control;
940 __le16 rate_info;
941 __le32 pad0[4];
942 __u8 rssi;
943 __u8 channel;
944 __le16 pad1;
945 __u8 rx_ctrl;
946 __u8 rx_status;
947 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000948} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100949
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100950#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
951#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
952#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
953#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
954#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
955#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200956
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100957#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800958#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
959/* ICV=0 or MIC=1 */
960#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
961/* Key is uploaded only in failure case */
962#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200963
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100964static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200965{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100966 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200967
968 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100969 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200970}
971
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100972static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200973{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100974 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200975
976 rxd->pkt_len = cpu_to_le16(len);
977 rxd->pkt_phys_addr = cpu_to_le32(addr);
978 wmb();
979 rxd->rx_ctrl = 0;
980}
981
982static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100983mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400984 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200985{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100986 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200987 u16 rate_info;
988
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100989 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200990 return -1;
991 rmb();
992
993 rate_info = le16_to_cpu(rxd->rate_info);
994
995 memset(status, 0, sizeof(*status));
996
997 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400998 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100999 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1000 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001001
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001002 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001003 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001004 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001005 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001006 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001007 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001008 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001009 status->flag |= RX_FLAG_HT;
1010
Lennert Buytenhek85478342010-01-12 13:48:56 +01001011 if (rxd->channel > 14) {
1012 status->band = IEEE80211_BAND_5GHZ;
1013 if (!(status->flag & RX_FLAG_HT))
1014 status->rate_idx -= 5;
1015 } else {
1016 status->band = IEEE80211_BAND_2GHZ;
1017 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001018 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1019 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001020
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001021 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001022 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1023 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1024 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001025
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001026 return le16_to_cpu(rxd->pkt_len);
1027}
1028
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001029static struct rxd_ops rxd_sta_ops = {
1030 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1031 .rxd_init = mwl8k_rxd_sta_init,
1032 .rxd_refill = mwl8k_rxd_sta_refill,
1033 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001034};
1035
1036
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001037#define MWL8K_RX_DESCS 256
1038#define MWL8K_RX_MAXSZ 3800
1039
1040static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1041{
1042 struct mwl8k_priv *priv = hw->priv;
1043 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1044 int size;
1045 int i;
1046
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001047 rxq->rxd_count = 0;
1048 rxq->head = 0;
1049 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001050
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001051 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001052
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001053 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1054 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001055 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001056 return -ENOMEM;
1057 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001058 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001059
Shan Weib9ede5f2011-03-08 11:02:03 +08001060 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001061 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001062 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001063 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001064 return -ENOMEM;
1065 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001066
1067 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001068 int desc_size;
1069 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001070 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001071 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001072
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001073 desc_size = priv->rxd_ops->rxd_size;
1074 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001075
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001076 nexti = i + 1;
1077 if (nexti == MWL8K_RX_DESCS)
1078 nexti = 0;
1079 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1080
1081 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001082 }
1083
1084 return 0;
1085}
1086
1087static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1088{
1089 struct mwl8k_priv *priv = hw->priv;
1090 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1091 int refilled;
1092
1093 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001094 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001095 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001096 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001097 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001098 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001099
1100 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1101 if (skb == NULL)
1102 break;
1103
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001104 addr = pci_map_single(priv->pdev, skb->data,
1105 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001106
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001107 rxq->rxd_count++;
1108 rx = rxq->tail++;
1109 if (rxq->tail == MWL8K_RX_DESCS)
1110 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001111 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001112 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001113
1114 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1115 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001116
1117 refilled++;
1118 }
1119
1120 return refilled;
1121}
1122
1123/* Must be called only when the card's reception is completely halted */
1124static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1125{
1126 struct mwl8k_priv *priv = hw->priv;
1127 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1128 int i;
1129
1130 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001131 if (rxq->buf[i].skb != NULL) {
1132 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001133 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001134 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001135 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001136
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001137 kfree_skb(rxq->buf[i].skb);
1138 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001139 }
1140 }
1141
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001142 kfree(rxq->buf);
1143 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001144
1145 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001146 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001147 rxq->rxd, rxq->rxd_dma);
1148 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001149}
1150
1151
1152/*
1153 * Scan a list of BSSIDs to process for finalize join.
1154 * Allows for extension to process multiple BSSIDs.
1155 */
1156static inline int
1157mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1158{
1159 return priv->capture_beacon &&
1160 ieee80211_is_beacon(wh->frame_control) &&
1161 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1162}
1163
Lennert Buytenhek37797522009-10-22 20:19:45 +02001164static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1165 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001166{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001167 struct mwl8k_priv *priv = hw->priv;
1168
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001169 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001170 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001171
1172 /*
1173 * Use GFP_ATOMIC as rxq_process is called from
1174 * the primary interrupt handler, memory allocation call
1175 * must not sleep.
1176 */
1177 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1178 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001179 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001180}
1181
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001182static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1183 u8 *bssid)
1184{
1185 struct mwl8k_vif *mwl8k_vif;
1186
1187 list_for_each_entry(mwl8k_vif,
1188 vif_list, list) {
1189 if (memcmp(bssid, mwl8k_vif->bssid,
1190 ETH_ALEN) == 0)
1191 return mwl8k_vif;
1192 }
1193
1194 return NULL;
1195}
1196
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001197static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1198{
1199 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001200 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001201 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1202 int processed;
1203
1204 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001205 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001206 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001207 void *rxd;
1208 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001209 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001210 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001211 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001212
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001213 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001214 if (skb == NULL)
1215 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001216
1217 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1218
John W. Linville0d462bb2010-07-28 14:04:24 -04001219 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1220 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001221 if (pkt_len < 0)
1222 break;
1223
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001224 rxq->buf[rxq->head].skb = NULL;
1225
1226 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001227 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001228 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001229 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001230
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001231 rxq->head++;
1232 if (rxq->head == MWL8K_RX_DESCS)
1233 rxq->head = 0;
1234
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001235 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001236
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001237 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001238
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001239 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001240 * Check for a pending join operation. Save a
1241 * copy of the beacon and schedule a tasklet to
1242 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001243 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001244 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001245 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001246
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001247 if (ieee80211_has_protected(wh->frame_control)) {
1248
1249 /* Check if hw crypto has been enabled for
1250 * this bss. If yes, set the status flags
1251 * accordingly
1252 */
1253 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1254 wh->addr1);
1255
1256 if (mwl8k_vif != NULL &&
1257 mwl8k_vif->is_hw_crypto_enabled == true) {
1258 /*
1259 * When MMIC ERROR is encountered
1260 * by the firmware, payload is
1261 * dropped and only 32 bytes of
1262 * mwl8k Firmware header is sent
1263 * to the host.
1264 *
1265 * We need to add four bytes of
1266 * key information. In it
1267 * MAC80211 expects keyidx set to
1268 * 0 for triggering Counter
1269 * Measure of MMIC failure.
1270 */
1271 if (status.flag & RX_FLAG_MMIC_ERROR) {
1272 struct mwl8k_dma_data *tr;
1273 tr = (struct mwl8k_dma_data *)skb->data;
1274 memset((void *)&(tr->data), 0, 4);
1275 pkt_len += 4;
1276 }
1277
1278 if (!ieee80211_is_auth(wh->frame_control))
1279 status.flag |= RX_FLAG_IV_STRIPPED |
1280 RX_FLAG_DECRYPTED |
1281 RX_FLAG_MMIC_STRIPPED;
1282 }
1283 }
1284
1285 skb_put(skb, pkt_len);
1286 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001287 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1288 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001289
1290 processed++;
1291 }
1292
1293 return processed;
1294}
1295
1296
1297/*
1298 * Packet transmission.
1299 */
1300
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001301#define MWL8K_TXD_STATUS_OK 0x00000001
1302#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1303#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1304#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001305#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001306
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001307#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1308#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1309#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1310#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1311#define MWL8K_QOS_EOSP 0x0010
1312
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001313struct mwl8k_tx_desc {
1314 __le32 status;
1315 __u8 data_rate;
1316 __u8 tx_priority;
1317 __le16 qos_control;
1318 __le32 pkt_phys_addr;
1319 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001320 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001321 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001322 __le32 reserved;
1323 __le16 rate_info;
1324 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001325 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001326} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001327
1328#define MWL8K_TX_DESCS 128
1329
1330static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1331{
1332 struct mwl8k_priv *priv = hw->priv;
1333 struct mwl8k_tx_queue *txq = priv->txq + index;
1334 int size;
1335 int i;
1336
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001337 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001338 txq->head = 0;
1339 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001340
1341 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1342
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001343 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1344 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001345 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001346 return -ENOMEM;
1347 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001348 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001349
Shan Weib9ede5f2011-03-08 11:02:03 +08001350 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001351 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001352 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001353 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001354 return -ENOMEM;
1355 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001356
1357 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1358 struct mwl8k_tx_desc *tx_desc;
1359 int nexti;
1360
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001361 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001362 nexti = (i + 1) % MWL8K_TX_DESCS;
1363
1364 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001365 tx_desc->next_txd_phys_addr =
1366 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001367 }
1368
1369 return 0;
1370}
1371
1372static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1373{
1374 iowrite32(MWL8K_H2A_INT_PPA_READY,
1375 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1376 iowrite32(MWL8K_H2A_INT_DUMMY,
1377 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1378 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1379}
1380
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001381static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001382{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001383 struct mwl8k_priv *priv = hw->priv;
1384 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001385
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001386 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1387 struct mwl8k_tx_queue *txq = priv->txq + i;
1388 int fw_owned = 0;
1389 int drv_owned = 0;
1390 int unused = 0;
1391 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001392
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001393 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001394 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1395 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001396
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001397 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001398 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001399 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001400 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001401 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001402
1403 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001404 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001405 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001406
Joe Perchesc96c31e2010-07-26 14:39:58 -07001407 wiphy_err(hw->wiphy,
1408 "txq[%d] len=%d head=%d tail=%d "
1409 "fw_owned=%d drv_owned=%d unused=%d\n",
1410 i,
1411 txq->len, txq->head, txq->tail,
1412 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001413 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001414}
1415
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001416/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001417 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001418 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001419#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001420
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001421static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001422{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001423 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001424 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001425 int retry;
1426 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001427
1428 might_sleep();
1429
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001430 /*
1431 * The TX queues are stopped at this point, so this test
1432 * doesn't need to take ->tx_lock.
1433 */
1434 if (!priv->pending_tx_pkts)
1435 return 0;
1436
1437 retry = 0;
1438 rc = 0;
1439
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001440 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001441 priv->tx_wait = &tx_wait;
1442 while (!rc) {
1443 int oldcount;
1444 unsigned long timeout;
1445
1446 oldcount = priv->pending_tx_pkts;
1447
1448 spin_unlock_bh(&priv->tx_lock);
1449 timeout = wait_for_completion_timeout(&tx_wait,
1450 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1451 spin_lock_bh(&priv->tx_lock);
1452
1453 if (timeout) {
1454 WARN_ON(priv->pending_tx_pkts);
1455 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001456 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001457 }
1458 break;
1459 }
1460
1461 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001462 wiphy_notice(hw->wiphy,
1463 "waiting for tx rings to drain (%d -> %d pkts)\n",
1464 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001465 retry = 1;
1466 continue;
1467 }
1468
1469 priv->tx_wait = NULL;
1470
Joe Perchesc96c31e2010-07-26 14:39:58 -07001471 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1472 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001473 mwl8k_dump_tx_rings(hw);
1474
1475 rc = -ETIMEDOUT;
1476 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001477 spin_unlock_bh(&priv->tx_lock);
1478
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001479 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001480}
1481
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001482#define MWL8K_TXD_SUCCESS(status) \
1483 ((status) & (MWL8K_TXD_STATUS_OK | \
1484 MWL8K_TXD_STATUS_OK_RETRY | \
1485 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001486
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001487static int
1488mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001489{
1490 struct mwl8k_priv *priv = hw->priv;
1491 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001492 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001493
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001494 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001495 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001496 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001497 struct mwl8k_tx_desc *tx_desc;
1498 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001499 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001500 struct sk_buff *skb;
1501 struct ieee80211_tx_info *info;
1502 u32 status;
1503
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001504 tx = txq->head;
1505 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001506
1507 status = le32_to_cpu(tx_desc->status);
1508
1509 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1510 if (!force)
1511 break;
1512 tx_desc->status &=
1513 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1514 }
1515
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001516 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001517 BUG_ON(txq->len == 0);
1518 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001519 priv->pending_tx_pkts--;
1520
1521 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001522 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001523 skb = txq->skb[tx];
1524 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001525
1526 BUG_ON(skb == NULL);
1527 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1528
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001529 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001530
1531 /* Mark descriptor as unused */
1532 tx_desc->pkt_phys_addr = 0;
1533 tx_desc->pkt_len = 0;
1534
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001535 info = IEEE80211_SKB_CB(skb);
1536 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001537
1538 /* Rate control is happening in the firmware.
1539 * Ensure no tx rate is being reported.
1540 */
1541 info->status.rates[0].idx = -1;
1542 info->status.rates[0].count = 1;
1543
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001544 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001545 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001546
1547 ieee80211_tx_status_irqsafe(hw, skb);
1548
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001549 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001550 }
1551
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001552 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001553 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001554
1555 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001556}
1557
1558/* must be called only when the card's transmit is completely halted */
1559static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1560{
1561 struct mwl8k_priv *priv = hw->priv;
1562 struct mwl8k_tx_queue *txq = priv->txq + index;
1563
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001564 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001565
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001566 kfree(txq->skb);
1567 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001568
1569 pci_free_consistent(priv->pdev,
1570 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001571 txq->txd, txq->txd_dma);
1572 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001573}
1574
Johannes Berg7bb45682011-02-24 14:42:06 +01001575static void
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001576mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1577{
1578 struct mwl8k_priv *priv = hw->priv;
1579 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001580 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001581 struct ieee80211_hdr *wh;
1582 struct mwl8k_tx_queue *txq;
1583 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001584 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001585 u32 txstatus;
1586 u8 txdatarate;
1587 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001588
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001589 wh = (struct ieee80211_hdr *)skb->data;
1590 if (ieee80211_is_data_qos(wh->frame_control))
1591 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1592 else
1593 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001594
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001595 if (priv->ap_fw)
1596 mwl8k_encapsulate_tx_frame(skb);
1597 else
1598 mwl8k_add_dma_header(skb, 0);
1599
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001600 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001601
1602 tx_info = IEEE80211_SKB_CB(skb);
1603 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001604
1605 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001606 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001607 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1608 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001609 }
1610
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001611 /* Setup firmware control bit fields for each frame type. */
1612 txstatus = 0;
1613 txdatarate = 0;
1614 if (ieee80211_is_mgmt(wh->frame_control) ||
1615 ieee80211_is_ctl(wh->frame_control)) {
1616 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001617 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001618 } else if (ieee80211_is_data(wh->frame_control)) {
1619 txdatarate = 1;
1620 if (is_multicast_ether_addr(wh->addr1))
1621 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1622
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001623 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001624 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001625 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001626 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001627 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001628 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001629
1630 dma = pci_map_single(priv->pdev, skb->data,
1631 skb->len, PCI_DMA_TODEVICE);
1632
1633 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001634 wiphy_debug(hw->wiphy,
1635 "failed to dma map skb, dropping TX frame.\n");
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001636 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001637 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001638 }
1639
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001640 spin_lock_bh(&priv->tx_lock);
1641
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001642 txq = priv->txq + index;
1643
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001644 BUG_ON(txq->skb[txq->tail] != NULL);
1645 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001646
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001647 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001648 tx->data_rate = txdatarate;
1649 tx->tx_priority = index;
1650 tx->qos_control = cpu_to_le16(qos);
1651 tx->pkt_phys_addr = cpu_to_le32(dma);
1652 tx->pkt_len = cpu_to_le16(skb->len);
1653 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001654 if (!priv->ap_fw && tx_info->control.sta != NULL)
1655 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1656 else
1657 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001658 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001659 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1660
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001661 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001662 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001663
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001664 txq->tail++;
1665 if (txq->tail == MWL8K_TX_DESCS)
1666 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001667
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001668 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001669 ieee80211_stop_queue(hw, index);
1670
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001671 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001672
1673 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001674}
1675
1676
1677/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001678 * Firmware access.
1679 *
1680 * We have the following requirements for issuing firmware commands:
1681 * - Some commands require that the packet transmit path is idle when
1682 * the command is issued. (For simplicity, we'll just quiesce the
1683 * transmit path for every command.)
1684 * - There are certain sequences of commands that need to be issued to
1685 * the hardware sequentially, with no other intervening commands.
1686 *
1687 * This leads to an implementation of a "firmware lock" as a mutex that
1688 * can be taken recursively, and which is taken by both the low-level
1689 * command submission function (mwl8k_post_cmd) as well as any users of
1690 * that function that require issuing of an atomic sequence of commands,
1691 * and quiesces the transmit path whenever it's taken.
1692 */
1693static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1694{
1695 struct mwl8k_priv *priv = hw->priv;
1696
1697 if (priv->fw_mutex_owner != current) {
1698 int rc;
1699
1700 mutex_lock(&priv->fw_mutex);
1701 ieee80211_stop_queues(hw);
1702
1703 rc = mwl8k_tx_wait_empty(hw);
1704 if (rc) {
1705 ieee80211_wake_queues(hw);
1706 mutex_unlock(&priv->fw_mutex);
1707
1708 return rc;
1709 }
1710
1711 priv->fw_mutex_owner = current;
1712 }
1713
1714 priv->fw_mutex_depth++;
1715
1716 return 0;
1717}
1718
1719static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1720{
1721 struct mwl8k_priv *priv = hw->priv;
1722
1723 if (!--priv->fw_mutex_depth) {
1724 ieee80211_wake_queues(hw);
1725 priv->fw_mutex_owner = NULL;
1726 mutex_unlock(&priv->fw_mutex);
1727 }
1728}
1729
1730
1731/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001732 * Command processing.
1733 */
1734
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001735/* Timeout firmware commands after 10s */
1736#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001737
1738static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1739{
1740 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1741 struct mwl8k_priv *priv = hw->priv;
1742 void __iomem *regs = priv->regs;
1743 dma_addr_t dma_addr;
1744 unsigned int dma_size;
1745 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001746 unsigned long timeout = 0;
1747 u8 buf[32];
1748
John W. Linvilleb6037422010-07-20 13:55:00 -04001749 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001750 dma_size = le16_to_cpu(cmd->length);
1751 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1752 PCI_DMA_BIDIRECTIONAL);
1753 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1754 return -ENOMEM;
1755
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001756 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001757 if (rc) {
1758 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1759 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001760 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001761 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001763 priv->hostcmd_wait = &cmd_wait;
1764 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1765 iowrite32(MWL8K_H2A_INT_DOORBELL,
1766 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1767 iowrite32(MWL8K_H2A_INT_DUMMY,
1768 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001769
1770 timeout = wait_for_completion_timeout(&cmd_wait,
1771 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1772
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001773 priv->hostcmd_wait = NULL;
1774
1775 mwl8k_fw_unlock(hw);
1776
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001777 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1778 PCI_DMA_BIDIRECTIONAL);
1779
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001780 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001781 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001782 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1783 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001784 rc = -ETIMEDOUT;
1785 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001786 int ms;
1787
1788 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1789
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001790 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001791 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001792 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001793 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1794 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001795 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001796 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001797 mwl8k_cmd_name(cmd->code,
1798 buf, sizeof(buf)),
1799 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001800 }
1801
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001802 return rc;
1803}
1804
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001805static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1806 struct ieee80211_vif *vif,
1807 struct mwl8k_cmd_pkt *cmd)
1808{
1809 if (vif != NULL)
1810 cmd->macid = MWL8K_VIF(vif)->macid;
1811 return mwl8k_post_cmd(hw, cmd);
1812}
1813
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001814/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001815 * Setup code shared between STA and AP firmware images.
1816 */
1817static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1818{
1819 struct mwl8k_priv *priv = hw->priv;
1820
1821 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1822 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1823
1824 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1825 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1826
1827 priv->band_24.band = IEEE80211_BAND_2GHZ;
1828 priv->band_24.channels = priv->channels_24;
1829 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1830 priv->band_24.bitrates = priv->rates_24;
1831 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1832
1833 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1834}
1835
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001836static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1837{
1838 struct mwl8k_priv *priv = hw->priv;
1839
1840 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1841 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1842
1843 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1844 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1845
1846 priv->band_50.band = IEEE80211_BAND_5GHZ;
1847 priv->band_50.channels = priv->channels_50;
1848 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1849 priv->band_50.bitrates = priv->rates_50;
1850 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1851
1852 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1853}
1854
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001855/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001856 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001857 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001858struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001859 struct mwl8k_cmd_pkt header;
1860 __u8 hw_rev;
1861 __u8 host_interface;
1862 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001863 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001864 __le16 region_code;
1865 __le32 fw_rev;
1866 __le32 ps_cookie;
1867 __le32 caps;
1868 __u8 mcs_bitmap[16];
1869 __le32 rx_queue_ptr;
1870 __le32 num_tx_queues;
1871 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1872 __le32 caps2;
1873 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001874 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001875} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001876
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001877#define MWL8K_CAP_MAX_AMSDU 0x20000000
1878#define MWL8K_CAP_GREENFIELD 0x08000000
1879#define MWL8K_CAP_AMPDU 0x04000000
1880#define MWL8K_CAP_RX_STBC 0x01000000
1881#define MWL8K_CAP_TX_STBC 0x00800000
1882#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1883#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1884#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1885#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1886#define MWL8K_CAP_DELAY_BA 0x00003000
1887#define MWL8K_CAP_MIMO 0x00000200
1888#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001889#define MWL8K_CAP_BAND_MASK 0x00000007
1890#define MWL8K_CAP_5GHZ 0x00000004
1891#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001892
Lennert Buytenhek06953232010-01-12 13:49:41 +01001893static void
1894mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1895 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001896{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001897 int rx_streams;
1898 int tx_streams;
1899
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001900 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001901
1902 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001903 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001904 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001905 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001906 if (cap & MWL8K_CAP_AMPDU) {
1907 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001908 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1909 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001910 }
1911 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001912 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001913 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001914 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001915 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001916 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001917 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001918 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001919 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001920 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001921 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001922 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001923
1924 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1925 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1926
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001927 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001928 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001929 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001930 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001931 band->ht_cap.mcs.rx_mask[2] = 0xff;
1932 band->ht_cap.mcs.rx_mask[4] = 0x01;
1933 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001934
1935 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001936 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1937 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001938 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1939 }
1940}
1941
Lennert Buytenhek06953232010-01-12 13:49:41 +01001942static void
1943mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1944{
1945 struct mwl8k_priv *priv = hw->priv;
1946
1947 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1948 mwl8k_setup_2ghz_band(hw);
1949 if (caps & MWL8K_CAP_MIMO)
1950 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1951 }
1952
1953 if (caps & MWL8K_CAP_5GHZ) {
1954 mwl8k_setup_5ghz_band(hw);
1955 if (caps & MWL8K_CAP_MIMO)
1956 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1957 }
1958}
1959
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001960static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001961{
1962 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001963 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001964 int rc;
1965 int i;
1966
1967 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1968 if (cmd == NULL)
1969 return -ENOMEM;
1970
1971 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1972 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1973
1974 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1975 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001976 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001977 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001978 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001979 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001980 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001981 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001982
1983 rc = mwl8k_post_cmd(hw, &cmd->header);
1984
1985 if (!rc) {
1986 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1987 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001988 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001989 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001990 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001991 priv->ap_macids_supported = 0x00000000;
1992 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001993 }
1994
1995 kfree(cmd);
1996 return rc;
1997}
1998
1999/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002000 * CMD_GET_HW_SPEC (AP version).
2001 */
2002struct mwl8k_cmd_get_hw_spec_ap {
2003 struct mwl8k_cmd_pkt header;
2004 __u8 hw_rev;
2005 __u8 host_interface;
2006 __le16 num_wcb;
2007 __le16 num_mcaddrs;
2008 __u8 perm_addr[ETH_ALEN];
2009 __le16 region_code;
2010 __le16 num_antenna;
2011 __le32 fw_rev;
2012 __le32 wcbbase0;
2013 __le32 rxwrptr;
2014 __le32 rxrdptr;
2015 __le32 ps_cookie;
2016 __le32 wcbbase1;
2017 __le32 wcbbase2;
2018 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002019 __le32 fw_api_version;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002020} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002021
2022static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2023{
2024 struct mwl8k_priv *priv = hw->priv;
2025 struct mwl8k_cmd_get_hw_spec_ap *cmd;
2026 int rc;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002027 u32 api_version;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002028
2029 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2030 if (cmd == NULL)
2031 return -ENOMEM;
2032
2033 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2034 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2035
2036 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2037 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2038
2039 rc = mwl8k_post_cmd(hw, &cmd->header);
2040
2041 if (!rc) {
2042 int off;
2043
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002044 api_version = le32_to_cpu(cmd->fw_api_version);
2045 if (priv->device_info->fw_api_ap != api_version) {
2046 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2047 " Expected %d got %d.\n", MWL8K_NAME,
2048 priv->device_info->part_name,
2049 priv->device_info->fw_api_ap,
2050 api_version);
2051 rc = -EINVAL;
2052 goto done;
2053 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002054 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2055 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2056 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2057 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002058 mwl8k_setup_2ghz_band(hw);
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002059 priv->ap_macids_supported = 0x000000ff;
2060 priv->sta_macids_supported = 0x00000000;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002061
2062 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002063 iowrite32(priv->txq[0].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002064
2065 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002066 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002067
2068 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002069 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002070
2071 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002072 iowrite32(priv->txq[1].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002073
2074 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002075 iowrite32(priv->txq[2].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002076
2077 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002078 iowrite32(priv->txq[3].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002079 }
2080
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002081done:
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002082 kfree(cmd);
2083 return rc;
2084}
2085
2086/*
2087 * CMD_SET_HW_SPEC.
2088 */
2089struct mwl8k_cmd_set_hw_spec {
2090 struct mwl8k_cmd_pkt header;
2091 __u8 hw_rev;
2092 __u8 host_interface;
2093 __le16 num_mcaddrs;
2094 __u8 perm_addr[ETH_ALEN];
2095 __le16 region_code;
2096 __le32 fw_rev;
2097 __le32 ps_cookie;
2098 __le32 caps;
2099 __le32 rx_queue_ptr;
2100 __le32 num_tx_queues;
2101 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
2102 __le32 flags;
2103 __le32 num_tx_desc_per_queue;
2104 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002105} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002106
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002107#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2108#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2109#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002110
2111static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2112{
2113 struct mwl8k_priv *priv = hw->priv;
2114 struct mwl8k_cmd_set_hw_spec *cmd;
2115 int rc;
2116 int i;
2117
2118 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2119 if (cmd == NULL)
2120 return -ENOMEM;
2121
2122 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2123 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2124
2125 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2126 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2127 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002128
2129 /*
2130 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2131 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2132 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2133 * priority is interpreted the right way in firmware.
2134 */
2135 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
2136 int j = MWL8K_TX_QUEUES - 1 - i;
2137 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2138 }
2139
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002140 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2141 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2142 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002143 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2144 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2145
2146 rc = mwl8k_post_cmd(hw, &cmd->header);
2147 kfree(cmd);
2148
2149 return rc;
2150}
2151
2152/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002153 * CMD_MAC_MULTICAST_ADR.
2154 */
2155struct mwl8k_cmd_mac_multicast_adr {
2156 struct mwl8k_cmd_pkt header;
2157 __le16 action;
2158 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002159 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002160};
2161
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002162#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2163#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2164#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2165#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002166
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002167static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002168__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002169 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002170{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002171 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002172 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002173 int size;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002174 int mc_count = 0;
2175
2176 if (mc_list)
2177 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002178
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002179 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002180 allmulti = 1;
2181 mc_count = 0;
2182 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002183
2184 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2185
2186 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002187 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002188 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002189
2190 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2191 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002192 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2193 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002194
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002195 if (allmulti) {
2196 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2197 } else if (mc_count) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002198 struct netdev_hw_addr *ha;
2199 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002200
2201 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2202 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002203 netdev_hw_addr_list_for_each(ha, mc_list) {
2204 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002205 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002206 }
2207
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002208 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002209}
2210
2211/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002212 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002213 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002214struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002215 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002216 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002217} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002218
2219#define MWL8K_STAT_ACK_FAILURE 9
2220#define MWL8K_STAT_RTS_FAILURE 12
2221#define MWL8K_STAT_FCS_ERROR 24
2222#define MWL8K_STAT_RTS_SUCCESS 11
2223
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002224static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2225 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002226{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002227 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002228 int rc;
2229
2230 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2231 if (cmd == NULL)
2232 return -ENOMEM;
2233
2234 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2235 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002236
2237 rc = mwl8k_post_cmd(hw, &cmd->header);
2238 if (!rc) {
2239 stats->dot11ACKFailureCount =
2240 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2241 stats->dot11RTSFailureCount =
2242 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2243 stats->dot11FCSErrorCount =
2244 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2245 stats->dot11RTSSuccessCount =
2246 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2247 }
2248 kfree(cmd);
2249
2250 return rc;
2251}
2252
2253/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002254 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002255 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002256struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002257 struct mwl8k_cmd_pkt header;
2258 __le16 action;
2259 __le16 control;
2260 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002261} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002262
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002263static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002264mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002265{
2266 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002267 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002268 int rc;
2269
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002270 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002271 return 0;
2272
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002273 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2274 if (cmd == NULL)
2275 return -ENOMEM;
2276
2277 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2278 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2279 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002280 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002281 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2282
2283 rc = mwl8k_post_cmd(hw, &cmd->header);
2284 kfree(cmd);
2285
2286 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002287 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002288
2289 return rc;
2290}
2291
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002292static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002293{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002294 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002295}
2296
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002297static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002298{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002299 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002300}
2301
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002302static int
2303mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2304{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002305 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002306
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002307 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002308
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002309 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002310}
2311
2312/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002313 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002314 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002315#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002316
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002317struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002318 struct mwl8k_cmd_pkt header;
2319 __le16 action;
2320 __le16 support_level;
2321 __le16 current_level;
2322 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002323 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002324} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002325
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002326static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002327{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002328 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002329 int rc;
2330
2331 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2332 if (cmd == NULL)
2333 return -ENOMEM;
2334
2335 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2336 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2337 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2338 cmd->support_level = cpu_to_le16(dBm);
2339
2340 rc = mwl8k_post_cmd(hw, &cmd->header);
2341 kfree(cmd);
2342
2343 return rc;
2344}
2345
2346/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002347 * CMD_TX_POWER.
2348 */
2349#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2350
2351struct mwl8k_cmd_tx_power {
2352 struct mwl8k_cmd_pkt header;
2353 __le16 action;
2354 __le16 band;
2355 __le16 channel;
2356 __le16 bw;
2357 __le16 sub_ch;
2358 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2359} __attribute__((packed));
2360
2361static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2362 struct ieee80211_conf *conf,
2363 unsigned short pwr)
2364{
2365 struct ieee80211_channel *channel = conf->channel;
2366 struct mwl8k_cmd_tx_power *cmd;
2367 int rc;
2368 int i;
2369
2370 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2371 if (cmd == NULL)
2372 return -ENOMEM;
2373
2374 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2375 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2376 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2377
2378 if (channel->band == IEEE80211_BAND_2GHZ)
2379 cmd->band = cpu_to_le16(0x1);
2380 else if (channel->band == IEEE80211_BAND_5GHZ)
2381 cmd->band = cpu_to_le16(0x4);
2382
2383 cmd->channel = channel->hw_value;
2384
2385 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2386 conf->channel_type == NL80211_CHAN_HT20) {
2387 cmd->bw = cpu_to_le16(0x2);
2388 } else {
2389 cmd->bw = cpu_to_le16(0x4);
2390 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2391 cmd->sub_ch = cpu_to_le16(0x3);
2392 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2393 cmd->sub_ch = cpu_to_le16(0x1);
2394 }
2395
2396 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2397 cmd->power_level_list[i] = cpu_to_le16(pwr);
2398
2399 rc = mwl8k_post_cmd(hw, &cmd->header);
2400 kfree(cmd);
2401
2402 return rc;
2403}
2404
2405/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002406 * CMD_RF_ANTENNA.
2407 */
2408struct mwl8k_cmd_rf_antenna {
2409 struct mwl8k_cmd_pkt header;
2410 __le16 antenna;
2411 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002412} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002413
2414#define MWL8K_RF_ANTENNA_RX 1
2415#define MWL8K_RF_ANTENNA_TX 2
2416
2417static int
2418mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2419{
2420 struct mwl8k_cmd_rf_antenna *cmd;
2421 int rc;
2422
2423 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2424 if (cmd == NULL)
2425 return -ENOMEM;
2426
2427 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2428 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2429 cmd->antenna = cpu_to_le16(antenna);
2430 cmd->mode = cpu_to_le16(mask);
2431
2432 rc = mwl8k_post_cmd(hw, &cmd->header);
2433 kfree(cmd);
2434
2435 return rc;
2436}
2437
2438/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002439 * CMD_SET_BEACON.
2440 */
2441struct mwl8k_cmd_set_beacon {
2442 struct mwl8k_cmd_pkt header;
2443 __le16 beacon_len;
2444 __u8 beacon[0];
2445};
2446
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002447static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2448 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002449{
2450 struct mwl8k_cmd_set_beacon *cmd;
2451 int rc;
2452
2453 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2454 if (cmd == NULL)
2455 return -ENOMEM;
2456
2457 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2458 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2459 cmd->beacon_len = cpu_to_le16(len);
2460 memcpy(cmd->beacon, beacon, len);
2461
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002462 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002463 kfree(cmd);
2464
2465 return rc;
2466}
2467
2468/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002469 * CMD_SET_PRE_SCAN.
2470 */
2471struct mwl8k_cmd_set_pre_scan {
2472 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002473} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002474
2475static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2476{
2477 struct mwl8k_cmd_set_pre_scan *cmd;
2478 int rc;
2479
2480 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2481 if (cmd == NULL)
2482 return -ENOMEM;
2483
2484 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2485 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2486
2487 rc = mwl8k_post_cmd(hw, &cmd->header);
2488 kfree(cmd);
2489
2490 return rc;
2491}
2492
2493/*
2494 * CMD_SET_POST_SCAN.
2495 */
2496struct mwl8k_cmd_set_post_scan {
2497 struct mwl8k_cmd_pkt header;
2498 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002499 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002500} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002501
2502static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002503mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002504{
2505 struct mwl8k_cmd_set_post_scan *cmd;
2506 int rc;
2507
2508 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2509 if (cmd == NULL)
2510 return -ENOMEM;
2511
2512 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2513 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2514 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002515 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002516
2517 rc = mwl8k_post_cmd(hw, &cmd->header);
2518 kfree(cmd);
2519
2520 return rc;
2521}
2522
2523/*
2524 * CMD_SET_RF_CHANNEL.
2525 */
2526struct mwl8k_cmd_set_rf_channel {
2527 struct mwl8k_cmd_pkt header;
2528 __le16 action;
2529 __u8 current_channel;
2530 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002531} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002532
2533static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002534 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002535{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002536 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002537 struct mwl8k_cmd_set_rf_channel *cmd;
2538 int rc;
2539
2540 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2541 if (cmd == NULL)
2542 return -ENOMEM;
2543
2544 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2545 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2546 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2547 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002548
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002549 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002550 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002551 else if (channel->band == IEEE80211_BAND_5GHZ)
2552 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002553
2554 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2555 conf->channel_type == NL80211_CHAN_HT20)
2556 cmd->channel_flags |= cpu_to_le32(0x00000080);
2557 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2558 cmd->channel_flags |= cpu_to_le32(0x000001900);
2559 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2560 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002561
2562 rc = mwl8k_post_cmd(hw, &cmd->header);
2563 kfree(cmd);
2564
2565 return rc;
2566}
2567
2568/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002569 * CMD_SET_AID.
2570 */
2571#define MWL8K_FRAME_PROT_DISABLED 0x00
2572#define MWL8K_FRAME_PROT_11G 0x07
2573#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2574#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2575
2576struct mwl8k_cmd_update_set_aid {
2577 struct mwl8k_cmd_pkt header;
2578 __le16 aid;
2579
2580 /* AP's MAC address (BSSID) */
2581 __u8 bssid[ETH_ALEN];
2582 __le16 protection_mode;
2583 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002584} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002585
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002586static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2587{
2588 int i;
2589 int j;
2590
2591 /*
2592 * Clear nonstandard rates 4 and 13.
2593 */
2594 mask &= 0x1fef;
2595
2596 for (i = 0, j = 0; i < 14; i++) {
2597 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002598 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002599 }
2600}
2601
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002602static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002603mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2604 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002605{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002606 struct mwl8k_cmd_update_set_aid *cmd;
2607 u16 prot_mode;
2608 int rc;
2609
2610 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2611 if (cmd == NULL)
2612 return -ENOMEM;
2613
2614 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2615 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002616 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002617 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002618
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002619 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002620 prot_mode = MWL8K_FRAME_PROT_11G;
2621 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002622 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002623 IEEE80211_HT_OP_MODE_PROTECTION) {
2624 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2625 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2626 break;
2627 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2628 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2629 break;
2630 default:
2631 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2632 break;
2633 }
2634 }
2635 cmd->protection_mode = cpu_to_le16(prot_mode);
2636
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002637 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002638
2639 rc = mwl8k_post_cmd(hw, &cmd->header);
2640 kfree(cmd);
2641
2642 return rc;
2643}
2644
2645/*
2646 * CMD_SET_RATE.
2647 */
2648struct mwl8k_cmd_set_rate {
2649 struct mwl8k_cmd_pkt header;
2650 __u8 legacy_rates[14];
2651
2652 /* Bitmap for supported MCS codes. */
2653 __u8 mcs_set[16];
2654 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002655} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002656
2657static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002658mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002659 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002660{
2661 struct mwl8k_cmd_set_rate *cmd;
2662 int rc;
2663
2664 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2665 if (cmd == NULL)
2666 return -ENOMEM;
2667
2668 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2669 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002670 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002671 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002672
2673 rc = mwl8k_post_cmd(hw, &cmd->header);
2674 kfree(cmd);
2675
2676 return rc;
2677}
2678
2679/*
2680 * CMD_FINALIZE_JOIN.
2681 */
2682#define MWL8K_FJ_BEACON_MAXLEN 128
2683
2684struct mwl8k_cmd_finalize_join {
2685 struct mwl8k_cmd_pkt header;
2686 __le32 sleep_interval; /* Number of beacon periods to sleep */
2687 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002688} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002689
2690static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2691 int framelen, int dtim)
2692{
2693 struct mwl8k_cmd_finalize_join *cmd;
2694 struct ieee80211_mgmt *payload = frame;
2695 int payload_len;
2696 int rc;
2697
2698 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2699 if (cmd == NULL)
2700 return -ENOMEM;
2701
2702 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2703 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2704 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2705
2706 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2707 if (payload_len < 0)
2708 payload_len = 0;
2709 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2710 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2711
2712 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2713
2714 rc = mwl8k_post_cmd(hw, &cmd->header);
2715 kfree(cmd);
2716
2717 return rc;
2718}
2719
2720/*
2721 * CMD_SET_RTS_THRESHOLD.
2722 */
2723struct mwl8k_cmd_set_rts_threshold {
2724 struct mwl8k_cmd_pkt header;
2725 __le16 action;
2726 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002727} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002728
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002729static int
2730mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002731{
2732 struct mwl8k_cmd_set_rts_threshold *cmd;
2733 int rc;
2734
2735 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2736 if (cmd == NULL)
2737 return -ENOMEM;
2738
2739 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2740 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002741 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2742 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002743
2744 rc = mwl8k_post_cmd(hw, &cmd->header);
2745 kfree(cmd);
2746
2747 return rc;
2748}
2749
2750/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002751 * CMD_SET_SLOT.
2752 */
2753struct mwl8k_cmd_set_slot {
2754 struct mwl8k_cmd_pkt header;
2755 __le16 action;
2756 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002757} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002758
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002759static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002760{
2761 struct mwl8k_cmd_set_slot *cmd;
2762 int rc;
2763
2764 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2765 if (cmd == NULL)
2766 return -ENOMEM;
2767
2768 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2769 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2770 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002771 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002772
2773 rc = mwl8k_post_cmd(hw, &cmd->header);
2774 kfree(cmd);
2775
2776 return rc;
2777}
2778
2779/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002780 * CMD_SET_EDCA_PARAMS.
2781 */
2782struct mwl8k_cmd_set_edca_params {
2783 struct mwl8k_cmd_pkt header;
2784
2785 /* See MWL8K_SET_EDCA_XXX below */
2786 __le16 action;
2787
2788 /* TX opportunity in units of 32 us */
2789 __le16 txop;
2790
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002791 union {
2792 struct {
2793 /* Log exponent of max contention period: 0...15 */
2794 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002795
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002796 /* Log exponent of min contention period: 0...15 */
2797 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002798
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002799 /* Adaptive interframe spacing in units of 32us */
2800 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002801
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002802 /* TX queue to configure */
2803 __u8 txq;
2804 } ap;
2805 struct {
2806 /* Log exponent of max contention period: 0...15 */
2807 __u8 log_cw_max;
2808
2809 /* Log exponent of min contention period: 0...15 */
2810 __u8 log_cw_min;
2811
2812 /* Adaptive interframe spacing in units of 32us */
2813 __u8 aifs;
2814
2815 /* TX queue to configure */
2816 __u8 txq;
2817 } sta;
2818 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002819} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002820
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002821#define MWL8K_SET_EDCA_CW 0x01
2822#define MWL8K_SET_EDCA_TXOP 0x02
2823#define MWL8K_SET_EDCA_AIFS 0x04
2824
2825#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2826 MWL8K_SET_EDCA_TXOP | \
2827 MWL8K_SET_EDCA_AIFS)
2828
2829static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002830mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2831 __u16 cw_min, __u16 cw_max,
2832 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002833{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002834 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002835 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002836 int rc;
2837
2838 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2839 if (cmd == NULL)
2840 return -ENOMEM;
2841
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002842 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2843 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002844 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2845 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002846 if (priv->ap_fw) {
2847 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2848 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2849 cmd->ap.aifs = aifs;
2850 cmd->ap.txq = qnum;
2851 } else {
2852 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2853 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2854 cmd->sta.aifs = aifs;
2855 cmd->sta.txq = qnum;
2856 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002857
2858 rc = mwl8k_post_cmd(hw, &cmd->header);
2859 kfree(cmd);
2860
2861 return rc;
2862}
2863
2864/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002865 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002866 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002867struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002868 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002869 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002870} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002871
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002872static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002873{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002874 struct mwl8k_priv *priv = hw->priv;
2875 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002876 int rc;
2877
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002878 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2879 if (cmd == NULL)
2880 return -ENOMEM;
2881
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002882 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002883 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002884 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002885
2886 rc = mwl8k_post_cmd(hw, &cmd->header);
2887 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002888
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002889 if (!rc)
2890 priv->wmm_enabled = enable;
2891
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002892 return rc;
2893}
2894
2895/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002896 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002897 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002898struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002899 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002900 __le32 action;
2901 __u8 rx_antenna_map;
2902 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002903} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002904
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002905static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002906{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002907 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002908 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002909
2910 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2911 if (cmd == NULL)
2912 return -ENOMEM;
2913
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002914 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002915 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002916 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2917 cmd->rx_antenna_map = rx;
2918 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002919
2920 rc = mwl8k_post_cmd(hw, &cmd->header);
2921 kfree(cmd);
2922
2923 return rc;
2924}
2925
2926/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002927 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002928 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002929struct mwl8k_cmd_use_fixed_rate_sta {
2930 struct mwl8k_cmd_pkt header;
2931 __le32 action;
2932 __le32 allow_rate_drop;
2933 __le32 num_rates;
2934 struct {
2935 __le32 is_ht_rate;
2936 __le32 enable_retry;
2937 __le32 rate;
2938 __le32 retry_count;
2939 } rate_entry[8];
2940 __le32 rate_type;
2941 __le32 reserved1;
2942 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002943} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002944
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002945#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002946#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002947
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002948static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002949{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002950 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002951 int rc;
2952
2953 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2954 if (cmd == NULL)
2955 return -ENOMEM;
2956
2957 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2958 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002959 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2960 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002961
2962 rc = mwl8k_post_cmd(hw, &cmd->header);
2963 kfree(cmd);
2964
2965 return rc;
2966}
2967
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002968/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002969 * CMD_USE_FIXED_RATE (AP version).
2970 */
2971struct mwl8k_cmd_use_fixed_rate_ap {
2972 struct mwl8k_cmd_pkt header;
2973 __le32 action;
2974 __le32 allow_rate_drop;
2975 __le32 num_rates;
2976 struct mwl8k_rate_entry_ap {
2977 __le32 is_ht_rate;
2978 __le32 enable_retry;
2979 __le32 rate;
2980 __le32 retry_count;
2981 } rate_entry[4];
2982 u8 multicast_rate;
2983 u8 multicast_rate_type;
2984 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002985} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002986
2987static int
2988mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2989{
2990 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2991 int rc;
2992
2993 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2994 if (cmd == NULL)
2995 return -ENOMEM;
2996
2997 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2998 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2999 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3000 cmd->multicast_rate = mcast;
3001 cmd->management_rate = mgmt;
3002
3003 rc = mwl8k_post_cmd(hw, &cmd->header);
3004 kfree(cmd);
3005
3006 return rc;
3007}
3008
3009/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003010 * CMD_ENABLE_SNIFFER.
3011 */
3012struct mwl8k_cmd_enable_sniffer {
3013 struct mwl8k_cmd_pkt header;
3014 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003015} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003016
3017static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3018{
3019 struct mwl8k_cmd_enable_sniffer *cmd;
3020 int rc;
3021
3022 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3023 if (cmd == NULL)
3024 return -ENOMEM;
3025
3026 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3027 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3028 cmd->action = cpu_to_le32(!!enable);
3029
3030 rc = mwl8k_post_cmd(hw, &cmd->header);
3031 kfree(cmd);
3032
3033 return rc;
3034}
3035
3036/*
3037 * CMD_SET_MAC_ADDR.
3038 */
3039struct mwl8k_cmd_set_mac_addr {
3040 struct mwl8k_cmd_pkt header;
3041 union {
3042 struct {
3043 __le16 mac_type;
3044 __u8 mac_addr[ETH_ALEN];
3045 } mbss;
3046 __u8 mac_addr[ETH_ALEN];
3047 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003048} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003049
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003050#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3051#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3052#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3053#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003054
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003055static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3056 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003057{
3058 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003059 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003060 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003061 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003062 int rc;
3063
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003064 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3065 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3066 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3067 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3068 else
3069 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3070 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3071 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3072 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3073 else
3074 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3075 }
3076
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003077 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3078 if (cmd == NULL)
3079 return -ENOMEM;
3080
3081 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3082 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3083 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003084 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003085 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3086 } else {
3087 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3088 }
3089
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003090 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003091 kfree(cmd);
3092
3093 return rc;
3094}
3095
3096/*
3097 * CMD_SET_RATEADAPT_MODE.
3098 */
3099struct mwl8k_cmd_set_rate_adapt_mode {
3100 struct mwl8k_cmd_pkt header;
3101 __le16 action;
3102 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003103} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003104
3105static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3106{
3107 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3108 int rc;
3109
3110 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3111 if (cmd == NULL)
3112 return -ENOMEM;
3113
3114 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3115 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3116 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3117 cmd->mode = cpu_to_le16(mode);
3118
3119 rc = mwl8k_post_cmd(hw, &cmd->header);
3120 kfree(cmd);
3121
3122 return rc;
3123}
3124
3125/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003126 * CMD_BSS_START.
3127 */
3128struct mwl8k_cmd_bss_start {
3129 struct mwl8k_cmd_pkt header;
3130 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003131} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003132
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003133static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3134 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003135{
3136 struct mwl8k_cmd_bss_start *cmd;
3137 int rc;
3138
3139 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3140 if (cmd == NULL)
3141 return -ENOMEM;
3142
3143 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3144 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3145 cmd->enable = cpu_to_le32(enable);
3146
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003147 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003148 kfree(cmd);
3149
3150 return rc;
3151}
3152
3153/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003154 * CMD_SET_NEW_STN.
3155 */
3156struct mwl8k_cmd_set_new_stn {
3157 struct mwl8k_cmd_pkt header;
3158 __le16 aid;
3159 __u8 mac_addr[6];
3160 __le16 stn_id;
3161 __le16 action;
3162 __le16 rsvd;
3163 __le32 legacy_rates;
3164 __u8 ht_rates[4];
3165 __le16 cap_info;
3166 __le16 ht_capabilities_info;
3167 __u8 mac_ht_param_info;
3168 __u8 rev;
3169 __u8 control_channel;
3170 __u8 add_channel;
3171 __le16 op_mode;
3172 __le16 stbc;
3173 __u8 add_qos_info;
3174 __u8 is_qos_sta;
3175 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003176} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003177
3178#define MWL8K_STA_ACTION_ADD 0
3179#define MWL8K_STA_ACTION_REMOVE 2
3180
3181static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3182 struct ieee80211_vif *vif,
3183 struct ieee80211_sta *sta)
3184{
3185 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003186 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003187 int rc;
3188
3189 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3190 if (cmd == NULL)
3191 return -ENOMEM;
3192
3193 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3194 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3195 cmd->aid = cpu_to_le16(sta->aid);
3196 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3197 cmd->stn_id = cpu_to_le16(sta->aid);
3198 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003199 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3200 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3201 else
3202 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3203 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003204 if (sta->ht_cap.ht_supported) {
3205 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3206 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3207 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3208 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3209 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3210 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3211 ((sta->ht_cap.ampdu_density & 7) << 2);
3212 cmd->is_qos_sta = 1;
3213 }
3214
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003215 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003216 kfree(cmd);
3217
3218 return rc;
3219}
3220
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003221static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3222 struct ieee80211_vif *vif)
3223{
3224 struct mwl8k_cmd_set_new_stn *cmd;
3225 int rc;
3226
3227 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3228 if (cmd == NULL)
3229 return -ENOMEM;
3230
3231 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3232 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3233 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3234
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003235 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003236 kfree(cmd);
3237
3238 return rc;
3239}
3240
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003241static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3242 struct ieee80211_vif *vif, u8 *addr)
3243{
3244 struct mwl8k_cmd_set_new_stn *cmd;
3245 int rc;
3246
3247 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3248 if (cmd == NULL)
3249 return -ENOMEM;
3250
3251 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3252 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3253 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3254 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3255
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003256 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003257 kfree(cmd);
3258
3259 return rc;
3260}
3261
3262/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003263 * CMD_UPDATE_ENCRYPTION.
3264 */
3265
3266#define MAX_ENCR_KEY_LENGTH 16
3267#define MIC_KEY_LENGTH 8
3268
3269struct mwl8k_cmd_update_encryption {
3270 struct mwl8k_cmd_pkt header;
3271
3272 __le32 action;
3273 __le32 reserved;
3274 __u8 mac_addr[6];
3275 __u8 encr_type;
3276
3277} __attribute__((packed));
3278
3279struct mwl8k_cmd_set_key {
3280 struct mwl8k_cmd_pkt header;
3281
3282 __le32 action;
3283 __le32 reserved;
3284 __le16 length;
3285 __le16 key_type_id;
3286 __le32 key_info;
3287 __le32 key_id;
3288 __le16 key_len;
3289 __u8 key_material[MAX_ENCR_KEY_LENGTH];
3290 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
3291 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
3292 __le16 tkip_rsc_low;
3293 __le32 tkip_rsc_high;
3294 __le16 tkip_tsc_low;
3295 __le32 tkip_tsc_high;
3296 __u8 mac_addr[6];
3297} __attribute__((packed));
3298
3299enum {
3300 MWL8K_ENCR_ENABLE,
3301 MWL8K_ENCR_SET_KEY,
3302 MWL8K_ENCR_REMOVE_KEY,
3303 MWL8K_ENCR_SET_GROUP_KEY,
3304};
3305
3306#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
3307#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
3308#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
3309#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
3310#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
3311
3312enum {
3313 MWL8K_ALG_WEP,
3314 MWL8K_ALG_TKIP,
3315 MWL8K_ALG_CCMP,
3316};
3317
3318#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
3319#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
3320#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
3321#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
3322#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
3323
3324static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
3325 struct ieee80211_vif *vif,
3326 u8 *addr,
3327 u8 encr_type)
3328{
3329 struct mwl8k_cmd_update_encryption *cmd;
3330 int rc;
3331
3332 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3333 if (cmd == NULL)
3334 return -ENOMEM;
3335
3336 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3337 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3338 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
3339 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3340 cmd->encr_type = encr_type;
3341
3342 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3343 kfree(cmd);
3344
3345 return rc;
3346}
3347
3348static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
3349 u8 *addr,
3350 struct ieee80211_key_conf *key)
3351{
3352 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3353 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3354 cmd->length = cpu_to_le16(sizeof(*cmd) -
3355 offsetof(struct mwl8k_cmd_set_key, length));
3356 cmd->key_id = cpu_to_le32(key->keyidx);
3357 cmd->key_len = cpu_to_le16(key->keylen);
3358 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3359
3360 switch (key->cipher) {
3361 case WLAN_CIPHER_SUITE_WEP40:
3362 case WLAN_CIPHER_SUITE_WEP104:
3363 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
3364 if (key->keyidx == 0)
3365 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
3366
3367 break;
3368 case WLAN_CIPHER_SUITE_TKIP:
3369 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
3370 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3371 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3372 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3373 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
3374 | MWL8K_KEY_FLAG_TSC_VALID);
3375 break;
3376 case WLAN_CIPHER_SUITE_CCMP:
3377 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
3378 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3379 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3380 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3381 break;
3382 default:
3383 return -ENOTSUPP;
3384 }
3385
3386 return 0;
3387}
3388
3389static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
3390 struct ieee80211_vif *vif,
3391 u8 *addr,
3392 struct ieee80211_key_conf *key)
3393{
3394 struct mwl8k_cmd_set_key *cmd;
3395 int rc;
3396 int keymlen;
3397 u32 action;
3398 u8 idx;
3399 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3400
3401 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3402 if (cmd == NULL)
3403 return -ENOMEM;
3404
3405 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3406 if (rc < 0)
3407 goto done;
3408
3409 idx = key->keyidx;
3410
3411 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3412 action = MWL8K_ENCR_SET_KEY;
3413 else
3414 action = MWL8K_ENCR_SET_GROUP_KEY;
3415
3416 switch (key->cipher) {
3417 case WLAN_CIPHER_SUITE_WEP40:
3418 case WLAN_CIPHER_SUITE_WEP104:
3419 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
3420 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
3421 sizeof(*key) + key->keylen);
3422 mwl8k_vif->wep_key_conf[idx].enabled = 1;
3423 }
3424
3425 keymlen = 0;
3426 action = MWL8K_ENCR_SET_KEY;
3427 break;
3428 case WLAN_CIPHER_SUITE_TKIP:
3429 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
3430 break;
3431 case WLAN_CIPHER_SUITE_CCMP:
3432 keymlen = key->keylen;
3433 break;
3434 default:
3435 rc = -ENOTSUPP;
3436 goto done;
3437 }
3438
3439 memcpy(cmd->key_material, key->key, keymlen);
3440 cmd->action = cpu_to_le32(action);
3441
3442 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3443done:
3444 kfree(cmd);
3445
3446 return rc;
3447}
3448
3449static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
3450 struct ieee80211_vif *vif,
3451 u8 *addr,
3452 struct ieee80211_key_conf *key)
3453{
3454 struct mwl8k_cmd_set_key *cmd;
3455 int rc;
3456 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3457
3458 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3459 if (cmd == NULL)
3460 return -ENOMEM;
3461
3462 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3463 if (rc < 0)
3464 goto done;
3465
3466 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3467 WLAN_CIPHER_SUITE_WEP104)
3468 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
3469
3470 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
3471
3472 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3473done:
3474 kfree(cmd);
3475
3476 return rc;
3477}
3478
3479static int mwl8k_set_key(struct ieee80211_hw *hw,
3480 enum set_key_cmd cmd_param,
3481 struct ieee80211_vif *vif,
3482 struct ieee80211_sta *sta,
3483 struct ieee80211_key_conf *key)
3484{
3485 int rc = 0;
3486 u8 encr_type;
3487 u8 *addr;
3488 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3489
3490 if (vif->type == NL80211_IFTYPE_STATION)
3491 return -EOPNOTSUPP;
3492
3493 if (sta == NULL)
3494 addr = hw->wiphy->perm_addr;
3495 else
3496 addr = sta->addr;
3497
3498 if (cmd_param == SET_KEY) {
3499 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3500 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
3501 if (rc)
3502 goto out;
3503
3504 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
3505 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
3506 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
3507 else
3508 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
3509
3510 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
3511 encr_type);
3512 if (rc)
3513 goto out;
3514
3515 mwl8k_vif->is_hw_crypto_enabled = true;
3516
3517 } else {
3518 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
3519
3520 if (rc)
3521 goto out;
3522
3523 mwl8k_vif->is_hw_crypto_enabled = false;
3524
3525 }
3526out:
3527 return rc;
3528}
3529
3530/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003531 * CMD_UPDATE_STADB.
3532 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003533struct ewc_ht_info {
3534 __le16 control1;
3535 __le16 control2;
3536 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003537} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003538
3539struct peer_capability_info {
3540 /* Peer type - AP vs. STA. */
3541 __u8 peer_type;
3542
3543 /* Basic 802.11 capabilities from assoc resp. */
3544 __le16 basic_caps;
3545
3546 /* Set if peer supports 802.11n high throughput (HT). */
3547 __u8 ht_support;
3548
3549 /* Valid if HT is supported. */
3550 __le16 ht_caps;
3551 __u8 extended_ht_caps;
3552 struct ewc_ht_info ewc_info;
3553
3554 /* Legacy rate table. Intersection of our rates and peer rates. */
3555 __u8 legacy_rates[12];
3556
3557 /* HT rate table. Intersection of our rates and peer rates. */
3558 __u8 ht_rates[16];
3559 __u8 pad[16];
3560
3561 /* If set, interoperability mode, no proprietary extensions. */
3562 __u8 interop;
3563 __u8 pad2;
3564 __u8 station_id;
3565 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003566} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003567
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003568struct mwl8k_cmd_update_stadb {
3569 struct mwl8k_cmd_pkt header;
3570
3571 /* See STADB_ACTION_TYPE */
3572 __le32 action;
3573
3574 /* Peer MAC address */
3575 __u8 peer_addr[ETH_ALEN];
3576
3577 __le32 reserved;
3578
3579 /* Peer info - valid during add/update. */
3580 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003581} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003582
Lennert Buytenheka6804002010-01-04 21:55:42 +01003583#define MWL8K_STA_DB_MODIFY_ENTRY 1
3584#define MWL8K_STA_DB_DEL_ENTRY 2
3585
3586/* Peer Entry flags - used to define the type of the peer node */
3587#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3588
3589static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003590 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003591 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003592{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003593 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003594 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003595 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003596 int rc;
3597
3598 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3599 if (cmd == NULL)
3600 return -ENOMEM;
3601
3602 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3603 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003604 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003605 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003606
Lennert Buytenheka6804002010-01-04 21:55:42 +01003607 p = &cmd->peer_info;
3608 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3609 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003610 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003611 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003612 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3613 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003614 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3615 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3616 else
3617 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3618 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003619 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003620 p->interop = 1;
3621 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003622
Lennert Buytenheka6804002010-01-04 21:55:42 +01003623 rc = mwl8k_post_cmd(hw, &cmd->header);
3624 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003625
Lennert Buytenheka6804002010-01-04 21:55:42 +01003626 return rc ? rc : p->station_id;
3627}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003628
Lennert Buytenheka6804002010-01-04 21:55:42 +01003629static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3630 struct ieee80211_vif *vif, u8 *addr)
3631{
3632 struct mwl8k_cmd_update_stadb *cmd;
3633 int rc;
3634
3635 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3636 if (cmd == NULL)
3637 return -ENOMEM;
3638
3639 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3640 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3641 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3642 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3643
3644 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003645 kfree(cmd);
3646
3647 return rc;
3648}
3649
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003650
3651/*
3652 * Interrupt handling.
3653 */
3654static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3655{
3656 struct ieee80211_hw *hw = dev_id;
3657 struct mwl8k_priv *priv = hw->priv;
3658 u32 status;
3659
3660 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003661 if (!status)
3662 return IRQ_NONE;
3663
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003664 if (status & MWL8K_A2H_INT_TX_DONE) {
3665 status &= ~MWL8K_A2H_INT_TX_DONE;
3666 tasklet_schedule(&priv->poll_tx_task);
3667 }
3668
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003669 if (status & MWL8K_A2H_INT_RX_READY) {
3670 status &= ~MWL8K_A2H_INT_RX_READY;
3671 tasklet_schedule(&priv->poll_rx_task);
3672 }
3673
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003674 if (status)
3675 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003676
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003677 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003678 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003679 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003680 }
3681
3682 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003683 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003684 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003685 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003686 }
3687
3688 return IRQ_HANDLED;
3689}
3690
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003691static void mwl8k_tx_poll(unsigned long data)
3692{
3693 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3694 struct mwl8k_priv *priv = hw->priv;
3695 int limit;
3696 int i;
3697
3698 limit = 32;
3699
3700 spin_lock_bh(&priv->tx_lock);
3701
3702 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3703 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3704
3705 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3706 complete(priv->tx_wait);
3707 priv->tx_wait = NULL;
3708 }
3709
3710 spin_unlock_bh(&priv->tx_lock);
3711
3712 if (limit) {
3713 writel(~MWL8K_A2H_INT_TX_DONE,
3714 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3715 } else {
3716 tasklet_schedule(&priv->poll_tx_task);
3717 }
3718}
3719
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003720static void mwl8k_rx_poll(unsigned long data)
3721{
3722 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3723 struct mwl8k_priv *priv = hw->priv;
3724 int limit;
3725
3726 limit = 32;
3727 limit -= rxq_process(hw, 0, limit);
3728 limit -= rxq_refill(hw, 0, limit);
3729
3730 if (limit) {
3731 writel(~MWL8K_A2H_INT_RX_READY,
3732 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3733 } else {
3734 tasklet_schedule(&priv->poll_rx_task);
3735 }
3736}
3737
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003738
3739/*
3740 * Core driver operations.
3741 */
Johannes Berg7bb45682011-02-24 14:42:06 +01003742static void mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003743{
3744 struct mwl8k_priv *priv = hw->priv;
3745 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003746
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003747 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003748 wiphy_debug(hw->wiphy,
3749 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003750 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01003751 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003752 }
3753
Johannes Berg7bb45682011-02-24 14:42:06 +01003754 mwl8k_txq_xmit(hw, index, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003755}
3756
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003757static int mwl8k_start(struct ieee80211_hw *hw)
3758{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003759 struct mwl8k_priv *priv = hw->priv;
3760 int rc;
3761
Joe Perchesa0607fd2009-11-18 23:29:17 -08003762 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003763 IRQF_SHARED, MWL8K_NAME, hw);
3764 if (rc) {
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05303765 priv->irq = -1;
Joe Perches5db55842010-08-11 19:11:19 -07003766 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003767 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003768 }
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05303769 priv->irq = priv->pdev->irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003770
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003771 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003772 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003773 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003774
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003775 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003776 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003777
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003778 rc = mwl8k_fw_lock(hw);
3779 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003780 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003781
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003782 if (!priv->ap_fw) {
3783 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003784 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003785
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003786 if (!rc)
3787 rc = mwl8k_cmd_set_pre_scan(hw);
3788
3789 if (!rc)
3790 rc = mwl8k_cmd_set_post_scan(hw,
3791 "\x00\x00\x00\x00\x00\x00");
3792 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003793
3794 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003795 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003796
3797 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003798 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003799
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003800 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003801 }
3802
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003803 if (rc) {
3804 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3805 free_irq(priv->pdev->irq, hw);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05303806 priv->irq = -1;
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003807 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003808 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003809 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003810
3811 return rc;
3812}
3813
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003814static void mwl8k_stop(struct ieee80211_hw *hw)
3815{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003816 struct mwl8k_priv *priv = hw->priv;
3817 int i;
3818
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003819 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003820
3821 ieee80211_stop_queues(hw);
3822
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003823 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003824 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Brian Cavagnolobf3ca7f2011-04-06 14:18:46 +05303825 if (priv->irq != -1) {
3826 free_irq(priv->pdev->irq, hw);
3827 priv->irq = -1;
3828 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003829
3830 /* Stop finalize join worker */
3831 cancel_work_sync(&priv->finalize_join_worker);
3832 if (priv->beacon_skb != NULL)
3833 dev_kfree_skb(priv->beacon_skb);
3834
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003835 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003836 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003837 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003838
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003839 /* Return all skbs to mac80211 */
3840 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003841 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003842}
3843
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003844static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
3845
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003846static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003847 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003848{
3849 struct mwl8k_priv *priv = hw->priv;
3850 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003851 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003852 int macid, rc;
3853 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003854
3855 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003856 * Reject interface creation if sniffer mode is active, as
3857 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003858 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003859 */
3860 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003861 wiphy_info(hw->wiphy,
3862 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003863 return -EINVAL;
3864 }
3865
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003866 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003867 switch (vif->type) {
3868 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003869 if (!priv->ap_fw && di->fw_image_ap) {
3870 /* we must load the ap fw to meet this request */
3871 if (!list_empty(&priv->vif_list))
3872 return -EBUSY;
3873 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
3874 if (rc)
3875 return rc;
3876 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003877 macids_supported = priv->ap_macids_supported;
3878 break;
3879 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003880 if (priv->ap_fw && di->fw_image_sta) {
3881 /* we must load the sta fw to meet this request */
3882 if (!list_empty(&priv->vif_list))
3883 return -EBUSY;
3884 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
3885 if (rc)
3886 return rc;
3887 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003888 macids_supported = priv->sta_macids_supported;
3889 break;
3890 default:
3891 return -EINVAL;
3892 }
3893
3894 macid = ffs(macids_supported & ~priv->macids_used);
3895 if (!macid--)
3896 return -EBUSY;
3897
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003898 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003899 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003900 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003901 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003902 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003903 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08003904 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
3905 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003906
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003907 /* Set the mac address. */
3908 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3909
3910 if (priv->ap_fw)
3911 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3912
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003913 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003914 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003915
3916 return 0;
3917}
3918
3919static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003920 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003921{
3922 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003923 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003924
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003925 if (priv->ap_fw)
3926 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3927
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003928 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003929
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003930 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003931 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003932}
3933
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003934static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003935{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003936 struct ieee80211_conf *conf = &hw->conf;
3937 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003938 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003939
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003940 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003941 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003942 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003943 }
3944
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003945 rc = mwl8k_fw_lock(hw);
3946 if (rc)
3947 return rc;
3948
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003949 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003950 if (rc)
3951 goto out;
3952
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003953 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003954 if (rc)
3955 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003956
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003957 if (conf->power_level > 18)
3958 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003959
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003960 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003961 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
3962 if (rc)
3963 goto out;
3964
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08003965 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
3966 if (rc)
3967 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
3968 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3969 if (rc)
3970 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
3971
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003972 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003973 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
3974 if (rc)
3975 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003976 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3977 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003978
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003979out:
3980 mwl8k_fw_unlock(hw);
3981
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003982 return rc;
3983}
3984
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003985static void
3986mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3987 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003988{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003989 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003990 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003991 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003992 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003993
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003994 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003995 return;
3996
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003997 /*
3998 * No need to capture a beacon if we're no longer associated.
3999 */
4000 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4001 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004002
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004003 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004004 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004005 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004006 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004007 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004008
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004009 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004010
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004011 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004012 if (ap == NULL) {
4013 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004014 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004015 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004016
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004017 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4018 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4019 } else {
4020 ap_legacy_rates =
4021 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4022 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004023 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004024
4025 rcu_read_unlock();
4026 }
4027
4028 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004029 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004030 if (rc)
4031 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004032
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004033 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
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_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004039 rc = mwl8k_set_radio_preamble(hw,
4040 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004041 if (rc)
4042 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004043 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004044
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004045 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004046 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004047 if (rc)
4048 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004049 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004050
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004051 if (vif->bss_conf.assoc &&
4052 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4053 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004054 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004055 if (rc)
4056 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004057 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004058
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004059 if (vif->bss_conf.assoc &&
4060 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004061 /*
4062 * Finalize the join. Tell rx handler to process
4063 * next beacon from our BSSID.
4064 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004065 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004066 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004067 }
4068
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004069out:
4070 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004071}
4072
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004073static void
4074mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4075 struct ieee80211_bss_conf *info, u32 changed)
4076{
4077 int rc;
4078
4079 if (mwl8k_fw_lock(hw))
4080 return;
4081
4082 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4083 rc = mwl8k_set_radio_preamble(hw,
4084 vif->bss_conf.use_short_preamble);
4085 if (rc)
4086 goto out;
4087 }
4088
4089 if (changed & BSS_CHANGED_BASIC_RATES) {
4090 int idx;
4091 int rate;
4092
4093 /*
4094 * Use lowest supported basic rate for multicasts
4095 * and management frames (such as probe responses --
4096 * beacons will always go out at 1 Mb/s).
4097 */
4098 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004099 if (idx)
4100 idx--;
4101
4102 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4103 rate = mwl8k_rates_24[idx].hw_value;
4104 else
4105 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004106
4107 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4108 }
4109
4110 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4111 struct sk_buff *skb;
4112
4113 skb = ieee80211_beacon_get(hw, vif);
4114 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004115 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004116 kfree_skb(skb);
4117 }
4118 }
4119
4120 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004121 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004122
4123out:
4124 mwl8k_fw_unlock(hw);
4125}
4126
4127static void
4128mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4129 struct ieee80211_bss_conf *info, u32 changed)
4130{
4131 struct mwl8k_priv *priv = hw->priv;
4132
4133 if (!priv->ap_fw)
4134 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
4135 else
4136 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
4137}
4138
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004139static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00004140 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004141{
4142 struct mwl8k_cmd_pkt *cmd;
4143
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004144 /*
4145 * Synthesize and return a command packet that programs the
4146 * hardware multicast address filter. At this point we don't
4147 * know whether FIF_ALLMULTI is being requested, but if it is,
4148 * we'll end up throwing this packet away and creating a new
4149 * one in mwl8k_configure_filter().
4150 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004151 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004152
4153 return (unsigned long)cmd;
4154}
4155
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004156static int
4157mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
4158 unsigned int changed_flags,
4159 unsigned int *total_flags)
4160{
4161 struct mwl8k_priv *priv = hw->priv;
4162
4163 /*
4164 * Hardware sniffer mode is mutually exclusive with STA
4165 * operation, so refuse to enable sniffer mode if a STA
4166 * interface is active.
4167 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004168 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004169 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07004170 wiphy_info(hw->wiphy,
4171 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004172 return 0;
4173 }
4174
4175 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004176 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004177 return 0;
4178 priv->sniffer_enabled = true;
4179 }
4180
4181 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
4182 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
4183 FIF_OTHER_BSS;
4184
4185 return 1;
4186}
4187
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004188static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
4189{
4190 if (!list_empty(&priv->vif_list))
4191 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
4192
4193 return NULL;
4194}
4195
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004196static void mwl8k_configure_filter(struct ieee80211_hw *hw,
4197 unsigned int changed_flags,
4198 unsigned int *total_flags,
4199 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004200{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004201 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004202 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
4203
4204 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02004205 * AP firmware doesn't allow fine-grained control over
4206 * the receive filter.
4207 */
4208 if (priv->ap_fw) {
4209 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
4210 kfree(cmd);
4211 return;
4212 }
4213
4214 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004215 * Enable hardware sniffer mode if FIF_CONTROL or
4216 * FIF_OTHER_BSS is requested.
4217 */
4218 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
4219 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
4220 kfree(cmd);
4221 return;
4222 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004223
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004224 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004225 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004226
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004227 if (mwl8k_fw_lock(hw)) {
4228 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004229 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004230 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004231
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004232 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004233 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004234 priv->sniffer_enabled = false;
4235 }
4236
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004237 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004238 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
4239 /*
4240 * Disable the BSS filter.
4241 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004242 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004243 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004244 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004245 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004246
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004247 /*
4248 * Enable the BSS filter.
4249 *
4250 * If there is an active STA interface, use that
4251 * interface's BSSID, otherwise use a dummy one
4252 * (where the OUI part needs to be nonzero for
4253 * the BSSID to be accepted by POST_SCAN).
4254 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004255 mwl8k_vif = mwl8k_first_vif(priv);
4256 if (mwl8k_vif != NULL)
4257 bssid = mwl8k_vif->vif->bss_conf.bssid;
4258 else
4259 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004260
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004261 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004262 }
4263 }
4264
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004265 /*
4266 * If FIF_ALLMULTI is being requested, throw away the command
4267 * packet that ->prepare_multicast() built and replace it with
4268 * a command packet that enables reception of all multicast
4269 * packets.
4270 */
4271 if (*total_flags & FIF_ALLMULTI) {
4272 kfree(cmd);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004273 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004274 }
4275
4276 if (cmd != NULL) {
4277 mwl8k_post_cmd(hw, cmd);
4278 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004279 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004280
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004281 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004282}
4283
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004284static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4285{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004286 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004287}
4288
Johannes Berg4a6967b2010-02-19 19:18:37 +01004289static int mwl8k_sta_remove(struct ieee80211_hw *hw,
4290 struct ieee80211_vif *vif,
4291 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004292{
4293 struct mwl8k_priv *priv = hw->priv;
4294
Johannes Berg4a6967b2010-02-19 19:18:37 +01004295 if (priv->ap_fw)
4296 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
4297 else
4298 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
4299}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004300
Johannes Berg4a6967b2010-02-19 19:18:37 +01004301static int mwl8k_sta_add(struct ieee80211_hw *hw,
4302 struct ieee80211_vif *vif,
4303 struct ieee80211_sta *sta)
4304{
4305 struct mwl8k_priv *priv = hw->priv;
4306 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004307 int i;
4308 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4309 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004310
Johannes Berg4a6967b2010-02-19 19:18:37 +01004311 if (!priv->ap_fw) {
4312 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
4313 if (ret >= 0) {
4314 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004315 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004316 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01004317
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004318 } else {
4319 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004320 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004321
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004322 for (i = 0; i < NUM_WEP_KEYS; i++) {
4323 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
4324 if (mwl8k_vif->wep_key_conf[i].enabled)
4325 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
4326 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004327 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004328}
4329
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004330static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
4331 const struct ieee80211_tx_queue_params *params)
4332{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004333 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004334 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004335
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004336 rc = mwl8k_fw_lock(hw);
4337 if (!rc) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004338 BUG_ON(queue > MWL8K_TX_QUEUES - 1);
4339 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
4340
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004341 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004342 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004343
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004344 if (!rc) {
4345 int q = MWL8K_TX_QUEUES - 1 - queue;
4346 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004347 params->cw_min,
4348 params->cw_max,
4349 params->aifs,
4350 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004351 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004352
4353 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004354 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004355
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004356 return rc;
4357}
4358
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004359static int mwl8k_get_stats(struct ieee80211_hw *hw,
4360 struct ieee80211_low_level_stats *stats)
4361{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004362 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004363}
4364
John W. Linville0d462bb2010-07-28 14:04:24 -04004365static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
4366 struct survey_info *survey)
4367{
4368 struct mwl8k_priv *priv = hw->priv;
4369 struct ieee80211_conf *conf = &hw->conf;
4370
4371 if (idx != 0)
4372 return -ENOENT;
4373
4374 survey->channel = conf->channel;
4375 survey->filled = SURVEY_INFO_NOISE_DBM;
4376 survey->noise = priv->noise;
4377
4378 return 0;
4379}
4380
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004381static int
4382mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4383 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01004384 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4385 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004386{
4387 switch (action) {
4388 case IEEE80211_AMPDU_RX_START:
4389 case IEEE80211_AMPDU_RX_STOP:
4390 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
4391 return -ENOTSUPP;
4392 return 0;
4393 default:
4394 return -ENOTSUPP;
4395 }
4396}
4397
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004398static const struct ieee80211_ops mwl8k_ops = {
4399 .tx = mwl8k_tx,
4400 .start = mwl8k_start,
4401 .stop = mwl8k_stop,
4402 .add_interface = mwl8k_add_interface,
4403 .remove_interface = mwl8k_remove_interface,
4404 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004405 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004406 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004407 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004408 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004409 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01004410 .sta_add = mwl8k_sta_add,
4411 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004412 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004413 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04004414 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004415 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004416};
4417
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004418static void mwl8k_finalize_join_worker(struct work_struct *work)
4419{
4420 struct mwl8k_priv *priv =
4421 container_of(work, struct mwl8k_priv, finalize_join_worker);
4422 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01004423 struct ieee80211_mgmt *mgmt = (void *)skb->data;
4424 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
4425 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
4426 mgmt->u.beacon.variable, len);
4427 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004428
Johannes Berg56007a02010-01-26 14:19:52 +01004429 if (tim && tim[1] >= 2)
4430 dtim_period = tim[3];
4431
4432 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004433
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004434 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004435 priv->beacon_skb = NULL;
4436}
4437
John W. Linvillebcb628d2009-11-06 16:40:16 -05004438enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004439 MWL8363 = 0,
4440 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004441 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02004442};
4443
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004444#define MWL8K_8366_AP_FW_API 1
4445#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
4446#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
4447
John W. Linvillebcb628d2009-11-06 16:40:16 -05004448static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004449 [MWL8363] = {
4450 .part_name = "88w8363",
4451 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004452 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004453 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004454 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004455 .part_name = "88w8687",
4456 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004457 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05004458 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004459 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004460 .part_name = "88w8366",
4461 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004462 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004463 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
4464 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004465 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004466 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004467};
4468
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004469MODULE_FIRMWARE("mwl8k/helper_8363.fw");
4470MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
4471MODULE_FIRMWARE("mwl8k/helper_8687.fw");
4472MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
4473MODULE_FIRMWARE("mwl8k/helper_8366.fw");
4474MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004475MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004476
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004477static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01004478 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004479 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
4480 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004481 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
4482 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
4483 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01004484 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004485 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004486};
4487MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
4488
Brian Cavagnolo99020472010-11-12 17:23:52 -08004489static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
4490{
4491 int rc;
4492 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
4493 "Trying alternative firmware %s\n", pci_name(priv->pdev),
4494 priv->fw_pref, priv->fw_alt);
4495 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
4496 if (rc) {
4497 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4498 pci_name(priv->pdev), priv->fw_alt);
4499 return rc;
4500 }
4501 return 0;
4502}
4503
4504static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
4505static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
4506{
4507 struct mwl8k_priv *priv = context;
4508 struct mwl8k_device_info *di = priv->device_info;
4509 int rc;
4510
4511 switch (priv->fw_state) {
4512 case FW_STATE_INIT:
4513 if (!fw) {
4514 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
4515 pci_name(priv->pdev), di->helper_image);
4516 goto fail;
4517 }
4518 priv->fw_helper = fw;
4519 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
4520 true);
4521 if (rc && priv->fw_alt) {
4522 rc = mwl8k_request_alt_fw(priv);
4523 if (rc)
4524 goto fail;
4525 priv->fw_state = FW_STATE_LOADING_ALT;
4526 } else if (rc)
4527 goto fail;
4528 else
4529 priv->fw_state = FW_STATE_LOADING_PREF;
4530 break;
4531
4532 case FW_STATE_LOADING_PREF:
4533 if (!fw) {
4534 if (priv->fw_alt) {
4535 rc = mwl8k_request_alt_fw(priv);
4536 if (rc)
4537 goto fail;
4538 priv->fw_state = FW_STATE_LOADING_ALT;
4539 } else
4540 goto fail;
4541 } else {
4542 priv->fw_ucode = fw;
4543 rc = mwl8k_firmware_load_success(priv);
4544 if (rc)
4545 goto fail;
4546 else
4547 complete(&priv->firmware_loading_complete);
4548 }
4549 break;
4550
4551 case FW_STATE_LOADING_ALT:
4552 if (!fw) {
4553 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4554 pci_name(priv->pdev), di->helper_image);
4555 goto fail;
4556 }
4557 priv->fw_ucode = fw;
4558 rc = mwl8k_firmware_load_success(priv);
4559 if (rc)
4560 goto fail;
4561 else
4562 complete(&priv->firmware_loading_complete);
4563 break;
4564
4565 default:
4566 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
4567 MWL8K_NAME, priv->fw_state);
4568 BUG_ON(1);
4569 }
4570
4571 return;
4572
4573fail:
4574 priv->fw_state = FW_STATE_ERROR;
4575 complete(&priv->firmware_loading_complete);
4576 device_release_driver(&priv->pdev->dev);
4577 mwl8k_release_firmware(priv);
4578}
4579
4580static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
4581 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004582{
4583 struct mwl8k_priv *priv = hw->priv;
4584 int rc;
4585
4586 /* Reset firmware and hardware */
4587 mwl8k_hw_reset(priv);
4588
4589 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004590 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004591 if (rc) {
4592 wiphy_err(hw->wiphy, "Firmware files not found\n");
4593 return rc;
4594 }
4595
Brian Cavagnolo99020472010-11-12 17:23:52 -08004596 if (nowait)
4597 return rc;
4598
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004599 /* Load firmware into hardware */
4600 rc = mwl8k_load_firmware(hw);
4601 if (rc)
4602 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4603
4604 /* Reclaim memory once firmware is successfully loaded */
4605 mwl8k_release_firmware(priv);
4606
4607 return rc;
4608}
4609
4610/* initialize hw after successfully loading a firmware image */
4611static int mwl8k_probe_hw(struct ieee80211_hw *hw)
4612{
4613 struct mwl8k_priv *priv = hw->priv;
4614 int rc = 0;
4615 int i;
4616
4617 if (priv->ap_fw) {
4618 priv->rxd_ops = priv->device_info->ap_rxd_ops;
4619 if (priv->rxd_ops == NULL) {
4620 wiphy_err(hw->wiphy,
4621 "Driver does not have AP firmware image support for this hardware\n");
4622 goto err_stop_firmware;
4623 }
4624 } else {
4625 priv->rxd_ops = &rxd_sta_ops;
4626 }
4627
4628 priv->sniffer_enabled = false;
4629 priv->wmm_enabled = false;
4630 priv->pending_tx_pkts = 0;
4631
4632 rc = mwl8k_rxq_init(hw, 0);
4633 if (rc)
4634 goto err_stop_firmware;
4635 rxq_refill(hw, 0, INT_MAX);
4636
4637 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4638 rc = mwl8k_txq_init(hw, i);
4639 if (rc)
4640 goto err_free_queues;
4641 }
4642
4643 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4644 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4645 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
4646 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
4647 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4648
4649 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4650 IRQF_SHARED, MWL8K_NAME, hw);
4651 if (rc) {
4652 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4653 goto err_free_queues;
4654 }
4655
4656 /*
4657 * Temporarily enable interrupts. Initial firmware host
4658 * commands use interrupts and avoid polling. Disable
4659 * interrupts when done.
4660 */
4661 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4662
4663 /* Get config data, mac addrs etc */
4664 if (priv->ap_fw) {
4665 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4666 if (!rc)
4667 rc = mwl8k_cmd_set_hw_spec(hw);
4668 } else {
4669 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4670 }
4671 if (rc) {
4672 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
4673 goto err_free_irq;
4674 }
4675
4676 /* Turn radio off */
4677 rc = mwl8k_cmd_radio_disable(hw);
4678 if (rc) {
4679 wiphy_err(hw->wiphy, "Cannot disable\n");
4680 goto err_free_irq;
4681 }
4682
4683 /* Clear MAC address */
4684 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
4685 if (rc) {
4686 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
4687 goto err_free_irq;
4688 }
4689
4690 /* Disable interrupts */
4691 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4692 free_irq(priv->pdev->irq, hw);
4693
4694 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
4695 priv->device_info->part_name,
4696 priv->hw_rev, hw->wiphy->perm_addr,
4697 priv->ap_fw ? "AP" : "STA",
4698 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4699 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
4700
4701 return 0;
4702
4703err_free_irq:
4704 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4705 free_irq(priv->pdev->irq, hw);
4706
4707err_free_queues:
4708 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4709 mwl8k_txq_deinit(hw, i);
4710 mwl8k_rxq_deinit(hw, 0);
4711
4712err_stop_firmware:
4713 mwl8k_hw_reset(priv);
4714
4715 return rc;
4716}
4717
4718/*
4719 * invoke mwl8k_reload_firmware to change the firmware image after the device
4720 * has already been registered
4721 */
4722static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
4723{
4724 int i, rc = 0;
4725 struct mwl8k_priv *priv = hw->priv;
4726
4727 mwl8k_stop(hw);
4728 mwl8k_rxq_deinit(hw, 0);
4729
4730 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4731 mwl8k_txq_deinit(hw, i);
4732
Brian Cavagnolo99020472010-11-12 17:23:52 -08004733 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004734 if (rc)
4735 goto fail;
4736
4737 rc = mwl8k_probe_hw(hw);
4738 if (rc)
4739 goto fail;
4740
4741 rc = mwl8k_start(hw);
4742 if (rc)
4743 goto fail;
4744
4745 rc = mwl8k_config(hw, ~0);
4746 if (rc)
4747 goto fail;
4748
4749 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4750 rc = mwl8k_conf_tx(hw, i, &priv->wmm_params[i]);
4751 if (rc)
4752 goto fail;
4753 }
4754
4755 return rc;
4756
4757fail:
4758 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
4759 return rc;
4760}
4761
4762static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
4763{
4764 struct ieee80211_hw *hw = priv->hw;
4765 int i, rc;
4766
Brian Cavagnolo99020472010-11-12 17:23:52 -08004767 rc = mwl8k_load_firmware(hw);
4768 mwl8k_release_firmware(priv);
4769 if (rc) {
4770 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4771 return rc;
4772 }
4773
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004774 /*
4775 * Extra headroom is the size of the required DMA header
4776 * minus the size of the smallest 802.11 frame (CTS frame).
4777 */
4778 hw->extra_tx_headroom =
4779 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
4780
4781 hw->channel_change_time = 10;
4782
4783 hw->queues = MWL8K_TX_QUEUES;
4784
4785 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08004786 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004787 hw->vif_data_size = sizeof(struct mwl8k_vif);
4788 hw->sta_data_size = sizeof(struct mwl8k_sta);
4789
4790 priv->macids_used = 0;
4791 INIT_LIST_HEAD(&priv->vif_list);
4792
4793 /* Set default radio state and preamble */
4794 priv->radio_on = 0;
4795 priv->radio_short_preamble = 0;
4796
4797 /* Finalize join worker */
4798 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4799
4800 /* TX reclaim and RX tasklets. */
4801 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4802 tasklet_disable(&priv->poll_tx_task);
4803 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4804 tasklet_disable(&priv->poll_rx_task);
4805
4806 /* Power management cookie */
4807 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4808 if (priv->cookie == NULL)
4809 return -ENOMEM;
4810
4811 mutex_init(&priv->fw_mutex);
4812 priv->fw_mutex_owner = NULL;
4813 priv->fw_mutex_depth = 0;
4814 priv->hostcmd_wait = NULL;
4815
4816 spin_lock_init(&priv->tx_lock);
4817
4818 priv->tx_wait = NULL;
4819
4820 rc = mwl8k_probe_hw(hw);
4821 if (rc)
4822 goto err_free_cookie;
4823
4824 hw->wiphy->interface_modes = 0;
4825 if (priv->ap_macids_supported || priv->device_info->fw_image_ap)
4826 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4827 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
4828 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4829
4830 rc = ieee80211_register_hw(hw);
4831 if (rc) {
4832 wiphy_err(hw->wiphy, "Cannot register device\n");
4833 goto err_unprobe_hw;
4834 }
4835
4836 return 0;
4837
4838err_unprobe_hw:
4839 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4840 mwl8k_txq_deinit(hw, i);
4841 mwl8k_rxq_deinit(hw, 0);
4842
4843err_free_cookie:
4844 if (priv->cookie != NULL)
4845 pci_free_consistent(priv->pdev, 4,
4846 priv->cookie, priv->cookie_dma);
4847
4848 return rc;
4849}
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004850static int __devinit mwl8k_probe(struct pci_dev *pdev,
4851 const struct pci_device_id *id)
4852{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004853 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004854 struct ieee80211_hw *hw;
4855 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004856 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004857 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004858
4859 if (!printed_version) {
4860 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
4861 printed_version = 1;
4862 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004863
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004864
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004865 rc = pci_enable_device(pdev);
4866 if (rc) {
4867 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
4868 MWL8K_NAME);
4869 return rc;
4870 }
4871
4872 rc = pci_request_regions(pdev, MWL8K_NAME);
4873 if (rc) {
4874 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
4875 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004876 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004877 }
4878
4879 pci_set_master(pdev);
4880
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004881
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004882 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
4883 if (hw == NULL) {
4884 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
4885 rc = -ENOMEM;
4886 goto err_free_reg;
4887 }
4888
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004889 SET_IEEE80211_DEV(hw, &pdev->dev);
4890 pci_set_drvdata(pdev, hw);
4891
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004892 priv = hw->priv;
4893 priv->hw = hw;
4894 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05004895 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004896
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004897
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004898 priv->sram = pci_iomap(pdev, 0, 0x10000);
4899 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004900 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004901 goto err_iounmap;
4902 }
4903
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004904 /*
4905 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
4906 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
4907 */
4908 priv->regs = pci_iomap(pdev, 1, 0x10000);
4909 if (priv->regs == NULL) {
4910 priv->regs = pci_iomap(pdev, 2, 0x10000);
4911 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004912 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004913 goto err_iounmap;
4914 }
4915 }
4916
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004917 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08004918 * Choose the initial fw image depending on user input. If a second
4919 * image is available, make it the alternative image that will be
4920 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004921 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004922 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004923 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08004924 if (ap_mode_default && di->fw_image_ap) {
4925 priv->fw_pref = di->fw_image_ap;
4926 priv->fw_alt = di->fw_image_sta;
4927 } else if (!ap_mode_default && di->fw_image_sta) {
4928 priv->fw_pref = di->fw_image_sta;
4929 priv->fw_alt = di->fw_image_ap;
4930 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004931 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08004932 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004933 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
4934 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08004935 priv->fw_pref = di->fw_image_ap;
4936 }
4937 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004938 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004939 goto err_stop_firmware;
Brian Cavagnolo99020472010-11-12 17:23:52 -08004940 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004941
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004942err_stop_firmware:
4943 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004944
4945err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004946 if (priv->regs != NULL)
4947 pci_iounmap(pdev, priv->regs);
4948
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004949 if (priv->sram != NULL)
4950 pci_iounmap(pdev, priv->sram);
4951
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004952 pci_set_drvdata(pdev, NULL);
4953 ieee80211_free_hw(hw);
4954
4955err_free_reg:
4956 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004957
4958err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004959 pci_disable_device(pdev);
4960
4961 return rc;
4962}
4963
Joerg Albert230f7af2009-04-18 02:10:45 +02004964static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004965{
4966 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4967}
4968
Joerg Albert230f7af2009-04-18 02:10:45 +02004969static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004970{
4971 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4972 struct mwl8k_priv *priv;
4973 int i;
4974
4975 if (hw == NULL)
4976 return;
4977 priv = hw->priv;
4978
Brian Cavagnolo99020472010-11-12 17:23:52 -08004979 wait_for_completion(&priv->firmware_loading_complete);
4980
4981 if (priv->fw_state == FW_STATE_ERROR) {
4982 mwl8k_hw_reset(priv);
4983 goto unmap;
4984 }
4985
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004986 ieee80211_stop_queues(hw);
4987
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004988 ieee80211_unregister_hw(hw);
4989
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004990 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004991 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004992 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004993
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004994 /* Stop hardware */
4995 mwl8k_hw_reset(priv);
4996
4997 /* Return all skbs to mac80211 */
4998 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004999 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005001 for (i = 0; i < MWL8K_TX_QUEUES; i++)
5002 mwl8k_txq_deinit(hw, i);
5003
5004 mwl8k_rxq_deinit(hw, 0);
5005
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005006 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005007
Brian Cavagnolo99020472010-11-12 17:23:52 -08005008unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005009 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005010 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005011 pci_set_drvdata(pdev, NULL);
5012 ieee80211_free_hw(hw);
5013 pci_release_regions(pdev);
5014 pci_disable_device(pdev);
5015}
5016
5017static struct pci_driver mwl8k_driver = {
5018 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005019 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005020 .probe = mwl8k_probe,
5021 .remove = __devexit_p(mwl8k_remove),
5022 .shutdown = __devexit_p(mwl8k_shutdown),
5023};
5024
5025static int __init mwl8k_init(void)
5026{
5027 return pci_register_driver(&mwl8k_driver);
5028}
5029
5030static void __exit mwl8k_exit(void)
5031{
5032 pci_unregister_driver(&mwl8k_driver);
5033}
5034
5035module_init(mwl8k_init);
5036module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005037
5038MODULE_DESCRIPTION(MWL8K_DESC);
5039MODULE_VERSION(MWL8K_VERSION);
5040MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
5041MODULE_LICENSE("GPL");