blob: dcd4508b1fd47e0d8cc326cef4b30d8394d77dc0 [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
Brian Cavagnoloe6007072011-03-17 11:58:44 -070088#define MWL8K_TX_WMM_QUEUES 4
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -070089#define MWL8K_MAX_AMPDU_QUEUES 8
Brian Cavagnoloe6007072011-03-17 11:58:44 -070090#define MWL8K_MAX_TX_QUEUES (MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
91#define mwl8k_tx_queues(priv) (MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010092
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020093struct rxd_ops {
94 int rxd_size;
95 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
96 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010097 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -040098 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020099};
100
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200101struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200102 char *part_name;
103 char *helper_image;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800104 char *fw_image_sta;
105 char *fw_image_ap;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100106 struct rxd_ops *ap_rxd_ops;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -0800107 u32 fw_api_ap;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200108};
109
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100110struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200111 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100112
113 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200114 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115
116 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100118
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200119 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200120 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200121 struct {
122 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900123 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200124 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100125};
126
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100127struct mwl8k_tx_queue {
128 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200129 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130
131 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200132 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100133
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200134 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200135 struct mwl8k_tx_desc *txd;
136 dma_addr_t txd_dma;
137 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100138};
139
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700140enum {
141 AMPDU_NO_STREAM,
142 AMPDU_STREAM_NEW,
143 AMPDU_STREAM_IN_PROGRESS,
144 AMPDU_STREAM_ACTIVE,
145};
146
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700147struct mwl8k_ampdu_stream {
148 struct ieee80211_sta *sta;
149 u8 tid;
150 u8 state;
151 u8 idx;
152 u8 txq_idx; /* index of this stream in priv->txq */
153};
154
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100155struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100156 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100158
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200159 struct mwl8k_device_info *device_info;
160
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100161 void __iomem *sram;
162 void __iomem *regs;
163
164 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800165 const struct firmware *fw_helper;
166 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100167
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100168 /* hardware/firmware parameters */
169 bool ap_fw;
170 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100171 struct ieee80211_supported_band band_24;
172 struct ieee80211_channel channels_24[14];
173 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100174 struct ieee80211_supported_band band_50;
175 struct ieee80211_channel channels_50[4];
176 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100177 u32 ap_macids_supported;
178 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100179
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700180 /* Ampdu stream information */
181 u8 num_ampdu_queues;
Brian Cavagnoloac109fd2011-03-17 11:58:45 -0700182 spinlock_t stream_lock;
183 struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -0700184
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200185 /* firmware access */
186 struct mutex fw_mutex;
187 struct task_struct *fw_mutex_owner;
188 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200189 struct completion *hostcmd_wait;
190
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100191 /* lock held over TX and TX reap */
192 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100193
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200194 /* TX quiesce completion, protected by fw_mutex and tx_lock */
195 struct completion *tx_wait;
196
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100197 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100198 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100199 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100200
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100201 /* power management status cookie from firmware */
202 u32 *cookie;
203 dma_addr_t cookie_dma;
204
205 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100206 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200207 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100208
209 /*
210 * Running count of TX packets in flight, to avoid
211 * iterating over the transmit rings each time.
212 */
213 int pending_tx_pkts;
214
215 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700216 struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
217 u32 txq_offset[MWL8K_MAX_TX_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100218
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200219 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200220 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200221 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200222 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100223
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100224 /* XXX need to convert this to handle multiple interfaces */
225 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200226 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100227 struct sk_buff *beacon_skb;
228
229 /*
230 * This FJ worker has to be global as it is scheduled from the
231 * RX handler. At this point we don't know which interface it
232 * belongs to until the list of bssids waiting to complete join
233 * is checked.
234 */
235 struct work_struct finalize_join_worker;
236
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100237 /* Tasklet to perform TX reclaim. */
238 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100239
240 /* Tasklet to perform RX. */
241 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400242
243 /* Most recently reported noise in dBm */
244 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800245
246 /*
247 * preserve the queue configurations so they can be restored if/when
248 * the firmware image is swapped.
249 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -0700250 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800251
252 /* async firmware loading state */
253 unsigned fw_state;
254 char *fw_pref;
255 char *fw_alt;
256 struct completion firmware_loading_complete;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100257};
258
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800259#define MAX_WEP_KEY_LEN 13
260#define NUM_WEP_KEYS 4
261
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100262/* Per interface specific private data */
263struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100264 struct list_head list;
265 struct ieee80211_vif *vif;
266
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100267 /* Firmware macid for this vif. */
268 int macid;
269
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100270 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100271 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800272
273 /* Saved WEP keys */
274 struct {
275 u8 enabled;
276 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
277 } wep_key_conf[NUM_WEP_KEYS];
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800278
279 /* BSSID */
280 u8 bssid[ETH_ALEN];
281
282 /* A flag to indicate is HW crypto is enabled for this bssid */
283 bool is_hw_crypto_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100284};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200285#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800286#define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100287
Lennert Buytenheka6804002010-01-04 21:55:42 +0100288struct mwl8k_sta {
289 /* Index into station database. Returned by UPDATE_STADB. */
290 u8 peer_id;
291};
292#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
293
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100294static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100295 { .center_freq = 2412, .hw_value = 1, },
296 { .center_freq = 2417, .hw_value = 2, },
297 { .center_freq = 2422, .hw_value = 3, },
298 { .center_freq = 2427, .hw_value = 4, },
299 { .center_freq = 2432, .hw_value = 5, },
300 { .center_freq = 2437, .hw_value = 6, },
301 { .center_freq = 2442, .hw_value = 7, },
302 { .center_freq = 2447, .hw_value = 8, },
303 { .center_freq = 2452, .hw_value = 9, },
304 { .center_freq = 2457, .hw_value = 10, },
305 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100306 { .center_freq = 2467, .hw_value = 12, },
307 { .center_freq = 2472, .hw_value = 13, },
308 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100309};
310
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100311static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100312 { .bitrate = 10, .hw_value = 2, },
313 { .bitrate = 20, .hw_value = 4, },
314 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200315 { .bitrate = 110, .hw_value = 22, },
316 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100317 { .bitrate = 60, .hw_value = 12, },
318 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100319 { .bitrate = 120, .hw_value = 24, },
320 { .bitrate = 180, .hw_value = 36, },
321 { .bitrate = 240, .hw_value = 48, },
322 { .bitrate = 360, .hw_value = 72, },
323 { .bitrate = 480, .hw_value = 96, },
324 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100325 { .bitrate = 720, .hw_value = 144, },
326};
327
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100328static const struct ieee80211_channel mwl8k_channels_50[] = {
329 { .center_freq = 5180, .hw_value = 36, },
330 { .center_freq = 5200, .hw_value = 40, },
331 { .center_freq = 5220, .hw_value = 44, },
332 { .center_freq = 5240, .hw_value = 48, },
333};
334
335static const struct ieee80211_rate mwl8k_rates_50[] = {
336 { .bitrate = 60, .hw_value = 12, },
337 { .bitrate = 90, .hw_value = 18, },
338 { .bitrate = 120, .hw_value = 24, },
339 { .bitrate = 180, .hw_value = 36, },
340 { .bitrate = 240, .hw_value = 48, },
341 { .bitrate = 360, .hw_value = 72, },
342 { .bitrate = 480, .hw_value = 96, },
343 { .bitrate = 540, .hw_value = 108, },
344 { .bitrate = 720, .hw_value = 144, },
345};
346
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100347/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100348#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800349#define MWL8K_CMD_SET 0x0001
350#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100351
352/* Firmware command codes */
353#define MWL8K_CMD_CODE_DNLD 0x0001
354#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200355#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100356#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
357#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200358#define MWL8K_CMD_RADIO_CONTROL 0x001c
359#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800360#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200361#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100362#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100363#define MWL8K_CMD_SET_PRE_SCAN 0x0107
364#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200365#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100366#define MWL8K_CMD_SET_AID 0x010d
367#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200368#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100369#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200370#define MWL8K_CMD_SET_SLOT 0x0114
371#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
372#define MWL8K_CMD_SET_WMM_MODE 0x0123
373#define MWL8K_CMD_MIMO_CONFIG 0x0125
374#define MWL8K_CMD_USE_FIXED_RATE 0x0126
375#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100376#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200377#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100378#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
379#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800380#define MWL8K_CMD_UPDATE_ENCRYPTION 0x1122 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200381#define MWL8K_CMD_UPDATE_STADB 0x1123
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700382#define MWL8K_CMD_BASTREAM 0x1125
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100383
John W. Linvilleb6037422010-07-20 13:55:00 -0400384static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100385{
John W. Linvilleb6037422010-07-20 13:55:00 -0400386 u16 command = le16_to_cpu(cmd);
387
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100388#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
389 snprintf(buf, bufsize, "%s", #x);\
390 return buf;\
391 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400392 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100393 MWL8K_CMDNAME(CODE_DNLD);
394 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200395 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100396 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
397 MWL8K_CMDNAME(GET_STAT);
398 MWL8K_CMDNAME(RADIO_CONTROL);
399 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800400 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200401 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100402 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100403 MWL8K_CMDNAME(SET_PRE_SCAN);
404 MWL8K_CMDNAME(SET_POST_SCAN);
405 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100406 MWL8K_CMDNAME(SET_AID);
407 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200408 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100409 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200410 MWL8K_CMDNAME(SET_SLOT);
411 MWL8K_CMDNAME(SET_EDCA_PARAMS);
412 MWL8K_CMDNAME(SET_WMM_MODE);
413 MWL8K_CMDNAME(MIMO_CONFIG);
414 MWL8K_CMDNAME(USE_FIXED_RATE);
415 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200416 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200417 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100418 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100419 MWL8K_CMDNAME(SET_NEW_STN);
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -0800420 MWL8K_CMDNAME(UPDATE_ENCRYPTION);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200421 MWL8K_CMDNAME(UPDATE_STADB);
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -0700422 MWL8K_CMDNAME(BASTREAM);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100423 default:
424 snprintf(buf, bufsize, "0x%x", cmd);
425 }
426#undef MWL8K_CMDNAME
427
428 return buf;
429}
430
431/* Hardware and firmware reset */
432static void mwl8k_hw_reset(struct mwl8k_priv *priv)
433{
434 iowrite32(MWL8K_H2A_INT_RESET,
435 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
436 iowrite32(MWL8K_H2A_INT_RESET,
437 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
438 msleep(20);
439}
440
441/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800442static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100443{
444 if (*fw == NULL)
445 return;
446 release_firmware(*fw);
447 *fw = NULL;
448}
449
450static void mwl8k_release_firmware(struct mwl8k_priv *priv)
451{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100452 mwl8k_release_fw(&priv->fw_ucode);
453 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100454}
455
Brian Cavagnolo99020472010-11-12 17:23:52 -0800456/* states for asynchronous f/w loading */
457static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
458enum {
459 FW_STATE_INIT = 0,
460 FW_STATE_LOADING_PREF,
461 FW_STATE_LOADING_ALT,
462 FW_STATE_ERROR,
463};
464
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100465/* Request fw image */
466static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800467 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800468 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100469{
470 /* release current image */
471 if (*fw != NULL)
472 mwl8k_release_fw(fw);
473
Brian Cavagnolo99020472010-11-12 17:23:52 -0800474 if (nowait)
475 return request_firmware_nowait(THIS_MODULE, 1, fname,
476 &priv->pdev->dev, GFP_KERNEL,
477 priv, mwl8k_fw_state_machine);
478 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800479 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100480}
481
Brian Cavagnolo99020472010-11-12 17:23:52 -0800482static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
483 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100484{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200485 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100486 int rc;
487
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200488 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800489 if (nowait)
490 rc = mwl8k_request_fw(priv, di->helper_image,
491 &priv->fw_helper, true);
492 else
493 rc = mwl8k_request_fw(priv, di->helper_image,
494 &priv->fw_helper, false);
495 if (rc)
496 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
497 pci_name(priv->pdev), di->helper_image);
498
499 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200500 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100501 }
502
Brian Cavagnolo99020472010-11-12 17:23:52 -0800503 if (nowait) {
504 /*
505 * if we get here, no helper image is needed. Skip the
506 * FW_STATE_INIT state.
507 */
508 priv->fw_state = FW_STATE_LOADING_PREF;
509 rc = mwl8k_request_fw(priv, fw_image,
510 &priv->fw_ucode,
511 true);
512 } else
513 rc = mwl8k_request_fw(priv, fw_image,
514 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100515 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200516 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800517 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100518 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100519 return rc;
520 }
521
522 return 0;
523}
524
525struct mwl8k_cmd_pkt {
526 __le16 code;
527 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100528 __u8 seq_num;
529 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100530 __le16 result;
531 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000532} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100533
534/*
535 * Firmware loading.
536 */
537static int
538mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
539{
540 void __iomem *regs = priv->regs;
541 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100542 int loops;
543
544 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
545 if (pci_dma_mapping_error(priv->pdev, dma_addr))
546 return -ENOMEM;
547
548 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
549 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
550 iowrite32(MWL8K_H2A_INT_DOORBELL,
551 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
552 iowrite32(MWL8K_H2A_INT_DUMMY,
553 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
554
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100555 loops = 1000;
556 do {
557 u32 int_code;
558
559 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
560 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
561 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100562 break;
563 }
564
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200565 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100566 udelay(1);
567 } while (--loops);
568
569 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
570
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200571 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100572}
573
574static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
575 const u8 *data, size_t length)
576{
577 struct mwl8k_cmd_pkt *cmd;
578 int done;
579 int rc = 0;
580
581 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
582 if (cmd == NULL)
583 return -ENOMEM;
584
585 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
586 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100587 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100588 cmd->result = 0;
589
590 done = 0;
591 while (length) {
592 int block_size = length > 256 ? 256 : length;
593
594 memcpy(cmd->payload, data + done, block_size);
595 cmd->length = cpu_to_le16(block_size);
596
597 rc = mwl8k_send_fw_load_cmd(priv, cmd,
598 sizeof(*cmd) + block_size);
599 if (rc)
600 break;
601
602 done += block_size;
603 length -= block_size;
604 }
605
606 if (!rc) {
607 cmd->length = 0;
608 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
609 }
610
611 kfree(cmd);
612
613 return rc;
614}
615
616static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
617 const u8 *data, size_t length)
618{
619 unsigned char *buffer;
620 int may_continue, rc = 0;
621 u32 done, prev_block_size;
622
623 buffer = kmalloc(1024, GFP_KERNEL);
624 if (buffer == NULL)
625 return -ENOMEM;
626
627 done = 0;
628 prev_block_size = 0;
629 may_continue = 1000;
630 while (may_continue > 0) {
631 u32 block_size;
632
633 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
634 if (block_size & 1) {
635 block_size &= ~1;
636 may_continue--;
637 } else {
638 done += prev_block_size;
639 length -= prev_block_size;
640 }
641
642 if (block_size > 1024 || block_size > length) {
643 rc = -EOVERFLOW;
644 break;
645 }
646
647 if (length == 0) {
648 rc = 0;
649 break;
650 }
651
652 if (block_size == 0) {
653 rc = -EPROTO;
654 may_continue--;
655 udelay(1);
656 continue;
657 }
658
659 prev_block_size = block_size;
660 memcpy(buffer, data + done, block_size);
661
662 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
663 if (rc)
664 break;
665 }
666
667 if (!rc && length != 0)
668 rc = -EREMOTEIO;
669
670 kfree(buffer);
671
672 return rc;
673}
674
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200675static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100676{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200677 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800678 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200679 int rc;
680 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100681
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200682 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800683 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100684
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200685 if (helper == NULL) {
686 printk(KERN_ERR "%s: helper image needed but none "
687 "given\n", pci_name(priv->pdev));
688 return -EINVAL;
689 }
690
691 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100692 if (rc) {
693 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200694 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100695 return rc;
696 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100697 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100698
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200699 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100700 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200701 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100702 }
703
704 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200705 printk(KERN_ERR "%s: unable to load firmware image\n",
706 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100707 return rc;
708 }
709
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100710 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100711
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100712 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100713 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200714 u32 ready_code;
715
716 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
717 if (ready_code == MWL8K_FWAP_READY) {
718 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100719 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200720 } else if (ready_code == MWL8K_FWSTA_READY) {
721 priv->ap_fw = 0;
722 break;
723 }
724
725 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100726 udelay(1);
727 } while (--loops);
728
729 return loops ? 0 : -ETIMEDOUT;
730}
731
732
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100733/* DMA header used by firmware and hardware. */
734struct mwl8k_dma_data {
735 __le16 fwlen;
736 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100737 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000738} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100739
740/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100741static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100742{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100743 struct mwl8k_dma_data *tr;
744 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100745
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100746 tr = (struct mwl8k_dma_data *)skb->data;
747 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
748
749 if (hdrlen != sizeof(tr->wh)) {
750 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
751 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
752 *((__le16 *)(tr->data - 2)) = qos;
753 } else {
754 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
755 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100756 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100757
758 if (hdrlen != sizeof(*tr))
759 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100760}
761
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800762static void
763mwl8k_add_dma_header(struct sk_buff *skb, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100764{
765 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100766 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800767 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100768 struct mwl8k_dma_data *tr;
769
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100770 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100771 * Add a firmware DMA header; the firmware requires that we
772 * present a 2-byte payload length followed by a 4-address
773 * header (without QoS field), followed (optionally) by any
774 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100775 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100776 wh = (struct ieee80211_hdr *)skb->data;
777
778 hdrlen = ieee80211_hdrlen(wh->frame_control);
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800779 reqd_hdrlen = sizeof(*tr);
780
781 if (hdrlen != reqd_hdrlen)
782 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100783
784 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800785 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100786
787 tr = (struct mwl8k_dma_data *)skb->data;
788 if (wh != &tr->wh)
789 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100790 if (hdrlen != sizeof(tr->wh))
791 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100792
793 /*
794 * Firmware length is the length of the fully formed "802.11
795 * payload". That is, everything except for the 802.11 header.
796 * This includes all crypto material including the MIC.
797 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800798 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100799}
800
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800801static void mwl8k_encapsulate_tx_frame(struct sk_buff *skb)
802{
803 struct ieee80211_hdr *wh;
804 struct ieee80211_tx_info *tx_info;
805 struct ieee80211_key_conf *key_conf;
806 int data_pad;
807
808 wh = (struct ieee80211_hdr *)skb->data;
809
810 tx_info = IEEE80211_SKB_CB(skb);
811
812 key_conf = NULL;
813 if (ieee80211_is_data(wh->frame_control))
814 key_conf = tx_info->control.hw_key;
815
816 /*
817 * Make sure the packet header is in the DMA header format (4-address
818 * without QoS), the necessary crypto padding between the header and the
819 * payload has already been provided by mac80211, but it doesn't add tail
820 * padding when HW crypto is enabled.
821 *
822 * We have the following trailer padding requirements:
823 * - WEP: 4 trailer bytes (ICV)
824 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
825 * - CCMP: 8 trailer bytes (MIC)
826 */
827 data_pad = 0;
828 if (key_conf != NULL) {
829 switch (key_conf->cipher) {
830 case WLAN_CIPHER_SUITE_WEP40:
831 case WLAN_CIPHER_SUITE_WEP104:
832 data_pad = 4;
833 break;
834 case WLAN_CIPHER_SUITE_TKIP:
835 data_pad = 12;
836 break;
837 case WLAN_CIPHER_SUITE_CCMP:
838 data_pad = 8;
839 break;
840 }
841 }
842 mwl8k_add_dma_header(skb, data_pad);
843}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100844
845/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100846 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200847 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100848struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200849 __le16 pkt_len;
850 __u8 sq2;
851 __u8 rate;
852 __le32 pkt_phys_addr;
853 __le32 next_rxd_phys_addr;
854 __le16 qos_control;
855 __le16 htsig2;
856 __le32 hw_rssi_info;
857 __le32 hw_noise_floor_info;
858 __u8 noise_floor;
859 __u8 pad0[3];
860 __u8 rssi;
861 __u8 rx_status;
862 __u8 channel;
863 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000864} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200865
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100866#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
867#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
868#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100869
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100870#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200871
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800872/* 8366 AP rx_status bits */
873#define MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK 0x80
874#define MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR 0xFF
875#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR 0x02
876#define MWL8K_8366_AP_RXSTAT_WEP_DECRYPT_ICV_ERR 0x04
877#define MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR 0x08
878
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100879static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200880{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100881 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200882
883 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100884 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200885}
886
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100887static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200888{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100889 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200890
891 rxd->pkt_len = cpu_to_le16(len);
892 rxd->pkt_phys_addr = cpu_to_le32(addr);
893 wmb();
894 rxd->rx_ctrl = 0;
895}
896
897static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100898mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400899 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200900{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100901 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200902
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100903 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200904 return -1;
905 rmb();
906
907 memset(status, 0, sizeof(*status));
908
909 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400910 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200911
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100912 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200913 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100914 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100915 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100916 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200917 } else {
918 int i;
919
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100920 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
921 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200922 status->rate_idx = i;
923 break;
924 }
925 }
926 }
927
Lennert Buytenhek85478342010-01-12 13:48:56 +0100928 if (rxd->channel > 14) {
929 status->band = IEEE80211_BAND_5GHZ;
930 if (!(status->flag & RX_FLAG_HT))
931 status->rate_idx -= 5;
932 } else {
933 status->band = IEEE80211_BAND_2GHZ;
934 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +0900935 status->freq = ieee80211_channel_to_frequency(rxd->channel,
936 status->band);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200937
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100938 *qos = rxd->qos_control;
939
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800940 if ((rxd->rx_status != MWL8K_8366_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
941 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_DECRYPT_ERR_MASK) &&
942 (rxd->rx_status & MWL8K_8366_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
943 status->flag |= RX_FLAG_MMIC_ERROR;
944
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200945 return le16_to_cpu(rxd->pkt_len);
946}
947
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100948static struct rxd_ops rxd_8366_ap_ops = {
949 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
950 .rxd_init = mwl8k_rxd_8366_ap_init,
951 .rxd_refill = mwl8k_rxd_8366_ap_refill,
952 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200953};
954
955/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100956 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100957 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100958struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100959 __le16 pkt_len;
960 __u8 link_quality;
961 __u8 noise_level;
962 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200963 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100964 __le16 qos_control;
965 __le16 rate_info;
966 __le32 pad0[4];
967 __u8 rssi;
968 __u8 channel;
969 __le16 pad1;
970 __u8 rx_ctrl;
971 __u8 rx_status;
972 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000973} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100974
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100975#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
976#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
977#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
978#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
979#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
980#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200981
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100982#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -0800983#define MWL8K_STA_RX_CTRL_DECRYPT_ERROR 0x04
984/* ICV=0 or MIC=1 */
985#define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE 0x08
986/* Key is uploaded only in failure case */
987#define MWL8K_STA_RX_CTRL_KEY_INDEX 0x30
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200988
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100989static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200990{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100991 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200992
993 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100994 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200995}
996
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100997static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200998{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100999 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001000
1001 rxd->pkt_len = cpu_to_le16(len);
1002 rxd->pkt_phys_addr = cpu_to_le32(addr);
1003 wmb();
1004 rxd->rx_ctrl = 0;
1005}
1006
1007static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001008mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -04001009 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001010{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001011 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001012 u16 rate_info;
1013
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001014 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001015 return -1;
1016 rmb();
1017
1018 rate_info = le16_to_cpu(rxd->rate_info);
1019
1020 memset(status, 0, sizeof(*status));
1021
1022 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -04001023 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001024 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1025 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001026
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001027 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001028 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001029 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001030 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001031 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001032 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001033 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001034 status->flag |= RX_FLAG_HT;
1035
Lennert Buytenhek85478342010-01-12 13:48:56 +01001036 if (rxd->channel > 14) {
1037 status->band = IEEE80211_BAND_5GHZ;
1038 if (!(status->flag & RX_FLAG_HT))
1039 status->rate_idx -= 5;
1040 } else {
1041 status->band = IEEE80211_BAND_2GHZ;
1042 }
Bruno Randolf59eb21a2011-01-17 13:37:28 +09001043 status->freq = ieee80211_channel_to_frequency(rxd->channel,
1044 status->band);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001045
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001046 *qos = rxd->qos_control;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001047 if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1048 (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1049 status->flag |= RX_FLAG_MMIC_ERROR;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001050
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001051 return le16_to_cpu(rxd->pkt_len);
1052}
1053
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01001054static struct rxd_ops rxd_sta_ops = {
1055 .rxd_size = sizeof(struct mwl8k_rxd_sta),
1056 .rxd_init = mwl8k_rxd_sta_init,
1057 .rxd_refill = mwl8k_rxd_sta_refill,
1058 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001059};
1060
1061
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001062#define MWL8K_RX_DESCS 256
1063#define MWL8K_RX_MAXSZ 3800
1064
1065static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1066{
1067 struct mwl8k_priv *priv = hw->priv;
1068 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1069 int size;
1070 int i;
1071
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001072 rxq->rxd_count = 0;
1073 rxq->head = 0;
1074 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001075
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001076 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001077
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001078 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1079 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001080 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001081 return -ENOMEM;
1082 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001083 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001084
Shan Weib9ede5f2011-03-08 11:02:03 +08001085 rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001086 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001087 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001088 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001089 return -ENOMEM;
1090 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001091
1092 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001093 int desc_size;
1094 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001095 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001096 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001097
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001098 desc_size = priv->rxd_ops->rxd_size;
1099 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001100
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001101 nexti = i + 1;
1102 if (nexti == MWL8K_RX_DESCS)
1103 nexti = 0;
1104 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1105
1106 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001107 }
1108
1109 return 0;
1110}
1111
1112static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1113{
1114 struct mwl8k_priv *priv = hw->priv;
1115 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1116 int refilled;
1117
1118 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001119 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001120 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001121 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001122 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001123 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001124
1125 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1126 if (skb == NULL)
1127 break;
1128
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001129 addr = pci_map_single(priv->pdev, skb->data,
1130 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001131
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001132 rxq->rxd_count++;
1133 rx = rxq->tail++;
1134 if (rxq->tail == MWL8K_RX_DESCS)
1135 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001136 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001137 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001138
1139 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1140 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001141
1142 refilled++;
1143 }
1144
1145 return refilled;
1146}
1147
1148/* Must be called only when the card's reception is completely halted */
1149static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1150{
1151 struct mwl8k_priv *priv = hw->priv;
1152 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1153 int i;
1154
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001155 if (rxq->rxd == NULL)
1156 return;
1157
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001158 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001159 if (rxq->buf[i].skb != NULL) {
1160 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001161 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001162 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001163 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001164
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001165 kfree_skb(rxq->buf[i].skb);
1166 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167 }
1168 }
1169
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001170 kfree(rxq->buf);
1171 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001172
1173 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001174 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001175 rxq->rxd, rxq->rxd_dma);
1176 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001177}
1178
1179
1180/*
1181 * Scan a list of BSSIDs to process for finalize join.
1182 * Allows for extension to process multiple BSSIDs.
1183 */
1184static inline int
1185mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1186{
1187 return priv->capture_beacon &&
1188 ieee80211_is_beacon(wh->frame_control) &&
1189 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1190}
1191
Lennert Buytenhek37797522009-10-22 20:19:45 +02001192static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1193 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001194{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001195 struct mwl8k_priv *priv = hw->priv;
1196
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001197 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001198 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001199
1200 /*
1201 * Use GFP_ATOMIC as rxq_process is called from
1202 * the primary interrupt handler, memory allocation call
1203 * must not sleep.
1204 */
1205 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1206 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001207 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001208}
1209
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001210static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1211 u8 *bssid)
1212{
1213 struct mwl8k_vif *mwl8k_vif;
1214
1215 list_for_each_entry(mwl8k_vif,
1216 vif_list, list) {
1217 if (memcmp(bssid, mwl8k_vif->bssid,
1218 ETH_ALEN) == 0)
1219 return mwl8k_vif;
1220 }
1221
1222 return NULL;
1223}
1224
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001225static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1226{
1227 struct mwl8k_priv *priv = hw->priv;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001228 struct mwl8k_vif *mwl8k_vif = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001229 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1230 int processed;
1231
1232 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001233 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001234 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001235 void *rxd;
1236 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001237 struct ieee80211_rx_status status;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001238 struct ieee80211_hdr *wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001239 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001240
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001241 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001242 if (skb == NULL)
1243 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001244
1245 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1246
John W. Linville0d462bb2010-07-28 14:04:24 -04001247 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1248 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001249 if (pkt_len < 0)
1250 break;
1251
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001252 rxq->buf[rxq->head].skb = NULL;
1253
1254 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001255 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001256 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001257 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001258
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001259 rxq->head++;
1260 if (rxq->head == MWL8K_RX_DESCS)
1261 rxq->head = 0;
1262
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001263 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001264
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001265 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001266
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001267 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001268 * Check for a pending join operation. Save a
1269 * copy of the beacon and schedule a tasklet to
1270 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001271 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001272 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001273 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001274
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001275 if (ieee80211_has_protected(wh->frame_control)) {
1276
1277 /* Check if hw crypto has been enabled for
1278 * this bss. If yes, set the status flags
1279 * accordingly
1280 */
1281 mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1282 wh->addr1);
1283
1284 if (mwl8k_vif != NULL &&
1285 mwl8k_vif->is_hw_crypto_enabled == true) {
1286 /*
1287 * When MMIC ERROR is encountered
1288 * by the firmware, payload is
1289 * dropped and only 32 bytes of
1290 * mwl8k Firmware header is sent
1291 * to the host.
1292 *
1293 * We need to add four bytes of
1294 * key information. In it
1295 * MAC80211 expects keyidx set to
1296 * 0 for triggering Counter
1297 * Measure of MMIC failure.
1298 */
1299 if (status.flag & RX_FLAG_MMIC_ERROR) {
1300 struct mwl8k_dma_data *tr;
1301 tr = (struct mwl8k_dma_data *)skb->data;
1302 memset((void *)&(tr->data), 0, 4);
1303 pkt_len += 4;
1304 }
1305
1306 if (!ieee80211_is_auth(wh->frame_control))
1307 status.flag |= RX_FLAG_IV_STRIPPED |
1308 RX_FLAG_DECRYPTED |
1309 RX_FLAG_MMIC_STRIPPED;
1310 }
1311 }
1312
1313 skb_put(skb, pkt_len);
1314 mwl8k_remove_dma_header(skb, qos);
Johannes Bergf1d58c22009-06-17 13:13:00 +02001315 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1316 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001317
1318 processed++;
1319 }
1320
1321 return processed;
1322}
1323
1324
1325/*
1326 * Packet transmission.
1327 */
1328
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001329#define MWL8K_TXD_STATUS_OK 0x00000001
1330#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1331#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1332#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001333#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001334
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001335#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1336#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1337#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1338#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1339#define MWL8K_QOS_EOSP 0x0010
1340
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341struct mwl8k_tx_desc {
1342 __le32 status;
1343 __u8 data_rate;
1344 __u8 tx_priority;
1345 __le16 qos_control;
1346 __le32 pkt_phys_addr;
1347 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001348 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001349 __le32 next_txd_phys_addr;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07001350 __le32 timestamp;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001351 __le16 rate_info;
1352 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001353 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001354} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001355
1356#define MWL8K_TX_DESCS 128
1357
1358static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1359{
1360 struct mwl8k_priv *priv = hw->priv;
1361 struct mwl8k_tx_queue *txq = priv->txq + index;
1362 int size;
1363 int i;
1364
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001365 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001366 txq->head = 0;
1367 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001368
1369 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1370
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001371 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1372 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001373 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001374 return -ENOMEM;
1375 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001376 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001377
Shan Weib9ede5f2011-03-08 11:02:03 +08001378 txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001379 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001380 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001381 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001382 return -ENOMEM;
1383 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001384
1385 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1386 struct mwl8k_tx_desc *tx_desc;
1387 int nexti;
1388
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001389 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001390 nexti = (i + 1) % MWL8K_TX_DESCS;
1391
1392 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001393 tx_desc->next_txd_phys_addr =
1394 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001395 }
1396
1397 return 0;
1398}
1399
1400static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1401{
1402 iowrite32(MWL8K_H2A_INT_PPA_READY,
1403 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1404 iowrite32(MWL8K_H2A_INT_DUMMY,
1405 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1406 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1407}
1408
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001409static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001410{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001411 struct mwl8k_priv *priv = hw->priv;
1412 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001413
Brian Cavagnoloe6007072011-03-17 11:58:44 -07001414 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001415 struct mwl8k_tx_queue *txq = priv->txq + i;
1416 int fw_owned = 0;
1417 int drv_owned = 0;
1418 int unused = 0;
1419 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001420
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001421 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001422 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1423 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001424
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001425 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001426 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001427 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001428 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001429 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001430
1431 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001432 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001433 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001434
Joe Perchesc96c31e2010-07-26 14:39:58 -07001435 wiphy_err(hw->wiphy,
1436 "txq[%d] len=%d head=%d tail=%d "
1437 "fw_owned=%d drv_owned=%d unused=%d\n",
1438 i,
1439 txq->len, txq->head, txq->tail,
1440 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001441 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001442}
1443
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001444/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001445 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001446 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001447#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001448
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001449static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001450{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001451 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001452 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001453 int retry;
1454 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001455
1456 might_sleep();
1457
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001458 /*
1459 * The TX queues are stopped at this point, so this test
1460 * doesn't need to take ->tx_lock.
1461 */
1462 if (!priv->pending_tx_pkts)
1463 return 0;
1464
1465 retry = 0;
1466 rc = 0;
1467
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001468 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001469 priv->tx_wait = &tx_wait;
1470 while (!rc) {
1471 int oldcount;
1472 unsigned long timeout;
1473
1474 oldcount = priv->pending_tx_pkts;
1475
1476 spin_unlock_bh(&priv->tx_lock);
1477 timeout = wait_for_completion_timeout(&tx_wait,
1478 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1479 spin_lock_bh(&priv->tx_lock);
1480
1481 if (timeout) {
1482 WARN_ON(priv->pending_tx_pkts);
1483 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001484 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001485 }
1486 break;
1487 }
1488
1489 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001490 wiphy_notice(hw->wiphy,
1491 "waiting for tx rings to drain (%d -> %d pkts)\n",
1492 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001493 retry = 1;
1494 continue;
1495 }
1496
1497 priv->tx_wait = NULL;
1498
Joe Perchesc96c31e2010-07-26 14:39:58 -07001499 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1500 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001501 mwl8k_dump_tx_rings(hw);
1502
1503 rc = -ETIMEDOUT;
1504 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001505 spin_unlock_bh(&priv->tx_lock);
1506
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001507 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001508}
1509
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001510#define MWL8K_TXD_SUCCESS(status) \
1511 ((status) & (MWL8K_TXD_STATUS_OK | \
1512 MWL8K_TXD_STATUS_OK_RETRY | \
1513 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001514
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001515static int
1516mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001517{
1518 struct mwl8k_priv *priv = hw->priv;
1519 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001520 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001521
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001522 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001523 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001524 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001525 struct mwl8k_tx_desc *tx_desc;
1526 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001527 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001528 struct sk_buff *skb;
1529 struct ieee80211_tx_info *info;
1530 u32 status;
1531
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001532 tx = txq->head;
1533 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001534
1535 status = le32_to_cpu(tx_desc->status);
1536
1537 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1538 if (!force)
1539 break;
1540 tx_desc->status &=
1541 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1542 }
1543
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001544 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001545 BUG_ON(txq->len == 0);
1546 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001547 priv->pending_tx_pkts--;
1548
1549 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001550 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001551 skb = txq->skb[tx];
1552 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001553
1554 BUG_ON(skb == NULL);
1555 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1556
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001557 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001558
1559 /* Mark descriptor as unused */
1560 tx_desc->pkt_phys_addr = 0;
1561 tx_desc->pkt_len = 0;
1562
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001563 info = IEEE80211_SKB_CB(skb);
1564 ieee80211_tx_info_clear_status(info);
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08001565
1566 /* Rate control is happening in the firmware.
1567 * Ensure no tx rate is being reported.
1568 */
1569 info->status.rates[0].idx = -1;
1570 info->status.rates[0].count = 1;
1571
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001572 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001573 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001574
1575 ieee80211_tx_status_irqsafe(hw, skb);
1576
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001577 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578 }
1579
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001580 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001581 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001582
1583 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001584}
1585
1586/* must be called only when the card's transmit is completely halted */
1587static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1588{
1589 struct mwl8k_priv *priv = hw->priv;
1590 struct mwl8k_tx_queue *txq = priv->txq + index;
1591
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001592 if (txq->txd == NULL)
1593 return;
1594
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001595 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001596
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001597 kfree(txq->skb);
1598 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001599
1600 pci_free_consistent(priv->pdev,
1601 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001602 txq->txd, txq->txd_dma);
1603 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001604}
1605
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001606/* caller must hold priv->stream_lock when calling the stream functions */
1607struct mwl8k_ampdu_stream *
1608mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1609{
1610 struct mwl8k_ampdu_stream *stream;
1611 struct mwl8k_priv *priv = hw->priv;
1612 int i;
1613
1614 for (i = 0; i < priv->num_ampdu_queues; i++) {
1615 stream = &priv->ampdu[i];
1616 if (stream->state == AMPDU_NO_STREAM) {
1617 stream->sta = sta;
1618 stream->state = AMPDU_STREAM_NEW;
1619 stream->tid = tid;
1620 stream->idx = i;
1621 stream->txq_idx = MWL8K_TX_WMM_QUEUES + i;
1622 wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1623 sta->addr, tid);
1624 return stream;
1625 }
1626 }
1627 return NULL;
1628}
1629
1630static int
1631mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1632{
1633 int ret;
1634
1635 /* if the stream has already been started, don't start it again */
1636 if (stream->state != AMPDU_STREAM_NEW)
1637 return 0;
1638 ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1639 if (ret)
1640 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1641 "%d\n", stream->sta->addr, stream->tid, ret);
1642 else
1643 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1644 stream->sta->addr, stream->tid);
1645 return ret;
1646}
1647
1648static void
1649mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1650{
1651 wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1652 stream->tid);
1653 memset(stream, 0, sizeof(*stream));
1654}
1655
1656static struct mwl8k_ampdu_stream *
1657mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1658{
1659 struct mwl8k_priv *priv = hw->priv;
1660 int i;
1661
1662 for (i = 0 ; i < priv->num_ampdu_queues; i++) {
1663 struct mwl8k_ampdu_stream *stream;
1664 stream = &priv->ampdu[i];
1665 if (stream->state == AMPDU_NO_STREAM)
1666 continue;
1667 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1668 stream->tid == tid)
1669 return stream;
1670 }
1671 return NULL;
1672}
1673
Johannes Berg7bb45682011-02-24 14:42:06 +01001674static void
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001675mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1676{
1677 struct mwl8k_priv *priv = hw->priv;
1678 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001679 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001680 struct ieee80211_hdr *wh;
1681 struct mwl8k_tx_queue *txq;
1682 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001683 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001684 u32 txstatus;
1685 u8 txdatarate;
1686 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001687
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001688 wh = (struct ieee80211_hdr *)skb->data;
1689 if (ieee80211_is_data_qos(wh->frame_control))
1690 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1691 else
1692 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001693
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001694 if (priv->ap_fw)
1695 mwl8k_encapsulate_tx_frame(skb);
1696 else
1697 mwl8k_add_dma_header(skb, 0);
1698
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001699 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001700
1701 tx_info = IEEE80211_SKB_CB(skb);
1702 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001703
1704 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001705 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001706 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1707 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001708 }
1709
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001710 /* Setup firmware control bit fields for each frame type. */
1711 txstatus = 0;
1712 txdatarate = 0;
1713 if (ieee80211_is_mgmt(wh->frame_control) ||
1714 ieee80211_is_ctl(wh->frame_control)) {
1715 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001716 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001717 } else if (ieee80211_is_data(wh->frame_control)) {
1718 txdatarate = 1;
1719 if (is_multicast_ether_addr(wh->addr1))
1720 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1721
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001722 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001723 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001724 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001725 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001726 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001727 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001728
1729 dma = pci_map_single(priv->pdev, skb->data,
1730 skb->len, PCI_DMA_TODEVICE);
1731
1732 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001733 wiphy_debug(hw->wiphy,
1734 "failed to dma map skb, dropping TX frame.\n");
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001735 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001736 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001737 }
1738
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001739 spin_lock_bh(&priv->tx_lock);
1740
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001741 txq = priv->txq + index;
1742
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001743 BUG_ON(txq->skb[txq->tail] != NULL);
1744 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001745
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001746 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001747 tx->data_rate = txdatarate;
1748 tx->tx_priority = index;
1749 tx->qos_control = cpu_to_le16(qos);
1750 tx->pkt_phys_addr = cpu_to_le32(dma);
1751 tx->pkt_len = cpu_to_le16(skb->len);
1752 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001753 if (!priv->ap_fw && tx_info->control.sta != NULL)
1754 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1755 else
1756 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001757 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001758 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1759
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001760 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001761 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001763 txq->tail++;
1764 if (txq->tail == MWL8K_TX_DESCS)
1765 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001766
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001767 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001768 ieee80211_stop_queue(hw, index);
1769
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001770 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001771
1772 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001773}
1774
1775
1776/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001777 * Firmware access.
1778 *
1779 * We have the following requirements for issuing firmware commands:
1780 * - Some commands require that the packet transmit path is idle when
1781 * the command is issued. (For simplicity, we'll just quiesce the
1782 * transmit path for every command.)
1783 * - There are certain sequences of commands that need to be issued to
1784 * the hardware sequentially, with no other intervening commands.
1785 *
1786 * This leads to an implementation of a "firmware lock" as a mutex that
1787 * can be taken recursively, and which is taken by both the low-level
1788 * command submission function (mwl8k_post_cmd) as well as any users of
1789 * that function that require issuing of an atomic sequence of commands,
1790 * and quiesces the transmit path whenever it's taken.
1791 */
1792static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1793{
1794 struct mwl8k_priv *priv = hw->priv;
1795
1796 if (priv->fw_mutex_owner != current) {
1797 int rc;
1798
1799 mutex_lock(&priv->fw_mutex);
1800 ieee80211_stop_queues(hw);
1801
1802 rc = mwl8k_tx_wait_empty(hw);
1803 if (rc) {
1804 ieee80211_wake_queues(hw);
1805 mutex_unlock(&priv->fw_mutex);
1806
1807 return rc;
1808 }
1809
1810 priv->fw_mutex_owner = current;
1811 }
1812
1813 priv->fw_mutex_depth++;
1814
1815 return 0;
1816}
1817
1818static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1819{
1820 struct mwl8k_priv *priv = hw->priv;
1821
1822 if (!--priv->fw_mutex_depth) {
1823 ieee80211_wake_queues(hw);
1824 priv->fw_mutex_owner = NULL;
1825 mutex_unlock(&priv->fw_mutex);
1826 }
1827}
1828
1829
1830/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001831 * Command processing.
1832 */
1833
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001834/* Timeout firmware commands after 10s */
1835#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001836
1837static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1838{
1839 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1840 struct mwl8k_priv *priv = hw->priv;
1841 void __iomem *regs = priv->regs;
1842 dma_addr_t dma_addr;
1843 unsigned int dma_size;
1844 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001845 unsigned long timeout = 0;
1846 u8 buf[32];
1847
John W. Linvilleb6037422010-07-20 13:55:00 -04001848 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001849 dma_size = le16_to_cpu(cmd->length);
1850 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1851 PCI_DMA_BIDIRECTIONAL);
1852 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1853 return -ENOMEM;
1854
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001855 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001856 if (rc) {
1857 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1858 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001859 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001860 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001861
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001862 priv->hostcmd_wait = &cmd_wait;
1863 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1864 iowrite32(MWL8K_H2A_INT_DOORBELL,
1865 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1866 iowrite32(MWL8K_H2A_INT_DUMMY,
1867 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001868
1869 timeout = wait_for_completion_timeout(&cmd_wait,
1870 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1871
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001872 priv->hostcmd_wait = NULL;
1873
1874 mwl8k_fw_unlock(hw);
1875
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001876 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1877 PCI_DMA_BIDIRECTIONAL);
1878
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001879 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001880 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001881 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1882 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001883 rc = -ETIMEDOUT;
1884 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001885 int ms;
1886
1887 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1888
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001889 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001890 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001891 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001892 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1893 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001894 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001895 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001896 mwl8k_cmd_name(cmd->code,
1897 buf, sizeof(buf)),
1898 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001899 }
1900
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001901 return rc;
1902}
1903
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001904static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1905 struct ieee80211_vif *vif,
1906 struct mwl8k_cmd_pkt *cmd)
1907{
1908 if (vif != NULL)
1909 cmd->macid = MWL8K_VIF(vif)->macid;
1910 return mwl8k_post_cmd(hw, cmd);
1911}
1912
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001913/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001914 * Setup code shared between STA and AP firmware images.
1915 */
1916static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1917{
1918 struct mwl8k_priv *priv = hw->priv;
1919
1920 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1921 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1922
1923 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1924 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1925
1926 priv->band_24.band = IEEE80211_BAND_2GHZ;
1927 priv->band_24.channels = priv->channels_24;
1928 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1929 priv->band_24.bitrates = priv->rates_24;
1930 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1931
1932 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1933}
1934
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001935static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1936{
1937 struct mwl8k_priv *priv = hw->priv;
1938
1939 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1940 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1941
1942 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1943 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1944
1945 priv->band_50.band = IEEE80211_BAND_5GHZ;
1946 priv->band_50.channels = priv->channels_50;
1947 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1948 priv->band_50.bitrates = priv->rates_50;
1949 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1950
1951 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1952}
1953
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001954/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001955 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001956 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001957struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001958 struct mwl8k_cmd_pkt header;
1959 __u8 hw_rev;
1960 __u8 host_interface;
1961 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001962 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001963 __le16 region_code;
1964 __le32 fw_rev;
1965 __le32 ps_cookie;
1966 __le32 caps;
1967 __u8 mcs_bitmap[16];
1968 __le32 rx_queue_ptr;
1969 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07001970 __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001971 __le32 caps2;
1972 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001973 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001974} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001975
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001976#define MWL8K_CAP_MAX_AMSDU 0x20000000
1977#define MWL8K_CAP_GREENFIELD 0x08000000
1978#define MWL8K_CAP_AMPDU 0x04000000
1979#define MWL8K_CAP_RX_STBC 0x01000000
1980#define MWL8K_CAP_TX_STBC 0x00800000
1981#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1982#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1983#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1984#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1985#define MWL8K_CAP_DELAY_BA 0x00003000
1986#define MWL8K_CAP_MIMO 0x00000200
1987#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001988#define MWL8K_CAP_BAND_MASK 0x00000007
1989#define MWL8K_CAP_5GHZ 0x00000004
1990#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001991
Lennert Buytenhek06953232010-01-12 13:49:41 +01001992static void
1993mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1994 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001995{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001996 int rx_streams;
1997 int tx_streams;
1998
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001999 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002000
2001 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002002 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002003 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002004 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002005 if (cap & MWL8K_CAP_AMPDU) {
2006 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002007 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2008 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002009 }
2010 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002011 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002012 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002013 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002014 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002015 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002016 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002017 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002018 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002019 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002020 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002021 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002022
2023 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2024 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2025
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002026 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002027 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002028 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002029 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002030 band->ht_cap.mcs.rx_mask[2] = 0xff;
2031 band->ht_cap.mcs.rx_mask[4] = 0x01;
2032 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002033
2034 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002035 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2036 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002037 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2038 }
2039}
2040
Lennert Buytenhek06953232010-01-12 13:49:41 +01002041static void
2042mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2043{
2044 struct mwl8k_priv *priv = hw->priv;
2045
2046 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2047 mwl8k_setup_2ghz_band(hw);
2048 if (caps & MWL8K_CAP_MIMO)
2049 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2050 }
2051
2052 if (caps & MWL8K_CAP_5GHZ) {
2053 mwl8k_setup_5ghz_band(hw);
2054 if (caps & MWL8K_CAP_MIMO)
2055 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2056 }
2057}
2058
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002059static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002060{
2061 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02002062 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002063 int rc;
2064 int i;
2065
2066 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2067 if (cmd == NULL)
2068 return -ENOMEM;
2069
2070 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2071 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2072
2073 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2074 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002075 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002076 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2077 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002078 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002079 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002080 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002081
2082 rc = mwl8k_post_cmd(hw, &cmd->header);
2083
2084 if (!rc) {
2085 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2086 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002087 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002088 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002089 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002090 priv->ap_macids_supported = 0x00000000;
2091 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002092 }
2093
2094 kfree(cmd);
2095 return rc;
2096}
2097
2098/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002099 * CMD_GET_HW_SPEC (AP version).
2100 */
2101struct mwl8k_cmd_get_hw_spec_ap {
2102 struct mwl8k_cmd_pkt header;
2103 __u8 hw_rev;
2104 __u8 host_interface;
2105 __le16 num_wcb;
2106 __le16 num_mcaddrs;
2107 __u8 perm_addr[ETH_ALEN];
2108 __le16 region_code;
2109 __le16 num_antenna;
2110 __le32 fw_rev;
2111 __le32 wcbbase0;
2112 __le32 rxwrptr;
2113 __le32 rxrdptr;
2114 __le32 ps_cookie;
2115 __le32 wcbbase1;
2116 __le32 wcbbase2;
2117 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002118 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002119 __le32 caps;
2120 __le32 num_of_ampdu_queues;
2121 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002122} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002123
2124static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2125{
2126 struct mwl8k_priv *priv = hw->priv;
2127 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002128 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002129 u32 api_version;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002130
2131 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2132 if (cmd == NULL)
2133 return -ENOMEM;
2134
2135 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2136 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2137
2138 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2139 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2140
2141 rc = mwl8k_post_cmd(hw, &cmd->header);
2142
2143 if (!rc) {
2144 int off;
2145
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002146 api_version = le32_to_cpu(cmd->fw_api_version);
2147 if (priv->device_info->fw_api_ap != api_version) {
2148 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2149 " Expected %d got %d.\n", MWL8K_NAME,
2150 priv->device_info->part_name,
2151 priv->device_info->fw_api_ap,
2152 api_version);
2153 rc = -EINVAL;
2154 goto done;
2155 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002156 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2157 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2158 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2159 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002160 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002161 priv->ap_macids_supported = 0x000000ff;
2162 priv->sta_macids_supported = 0x00000000;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002163 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2164 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2165 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2166 " but we only support %d.\n",
2167 priv->num_ampdu_queues,
2168 MWL8K_MAX_AMPDU_QUEUES);
2169 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2170 }
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002171 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002172 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002173
2174 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002175 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002176
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002177 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2178 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2179 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2180 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002181
2182 for (i = 0; i < priv->num_ampdu_queues; i++)
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002183 priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002184 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002185 }
2186
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002187done:
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002188 kfree(cmd);
2189 return rc;
2190}
2191
2192/*
2193 * CMD_SET_HW_SPEC.
2194 */
2195struct mwl8k_cmd_set_hw_spec {
2196 struct mwl8k_cmd_pkt header;
2197 __u8 hw_rev;
2198 __u8 host_interface;
2199 __le16 num_mcaddrs;
2200 __u8 perm_addr[ETH_ALEN];
2201 __le16 region_code;
2202 __le32 fw_rev;
2203 __le32 ps_cookie;
2204 __le32 caps;
2205 __le32 rx_queue_ptr;
2206 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002207 __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002208 __le32 flags;
2209 __le32 num_tx_desc_per_queue;
2210 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002211} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002212
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002213/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2214 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2215 * the packets that are queued for more than 500ms, will be dropped in the
2216 * hardware. This helps minimizing the issues caused due to head-of-line
2217 * blocking where a slow client can hog the bandwidth and affect traffic to a
2218 * faster client.
2219 */
2220#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002221#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2222#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2223#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002224
2225static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2226{
2227 struct mwl8k_priv *priv = hw->priv;
2228 struct mwl8k_cmd_set_hw_spec *cmd;
2229 int rc;
2230 int i;
2231
2232 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2233 if (cmd == NULL)
2234 return -ENOMEM;
2235
2236 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2237 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2238
2239 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2240 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002241 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002242
2243 /*
2244 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2245 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2246 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2247 * priority is interpreted the right way in firmware.
2248 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002249 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2250 int j = mwl8k_tx_queues(priv) - 1 - i;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002251 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2252 }
2253
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002254 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2255 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2256 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02002257 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2258 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2259
2260 rc = mwl8k_post_cmd(hw, &cmd->header);
2261 kfree(cmd);
2262
2263 return rc;
2264}
2265
2266/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002267 * CMD_MAC_MULTICAST_ADR.
2268 */
2269struct mwl8k_cmd_mac_multicast_adr {
2270 struct mwl8k_cmd_pkt header;
2271 __le16 action;
2272 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002273 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002274};
2275
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002276#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2277#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2278#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2279#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002280
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002281static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002282__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad32010-04-01 21:22:57 +00002283 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002284{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002285 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002286 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002287 int size;
Jiri Pirko22bedad32010-04-01 21:22:57 +00002288 int mc_count = 0;
2289
2290 if (mc_list)
2291 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002292
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002293 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002294 allmulti = 1;
2295 mc_count = 0;
2296 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002297
2298 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2299
2300 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002301 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002302 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002303
2304 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2305 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002306 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2307 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002308
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002309 if (allmulti) {
2310 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2311 } else if (mc_count) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00002312 struct netdev_hw_addr *ha;
2313 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002314
2315 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2316 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad32010-04-01 21:22:57 +00002317 netdev_hw_addr_list_for_each(ha, mc_list) {
2318 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002319 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002320 }
2321
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002322 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002323}
2324
2325/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002326 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002327 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002328struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002329 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002330 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002331} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002332
2333#define MWL8K_STAT_ACK_FAILURE 9
2334#define MWL8K_STAT_RTS_FAILURE 12
2335#define MWL8K_STAT_FCS_ERROR 24
2336#define MWL8K_STAT_RTS_SUCCESS 11
2337
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002338static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2339 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002340{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002341 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002342 int rc;
2343
2344 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2345 if (cmd == NULL)
2346 return -ENOMEM;
2347
2348 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2349 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002350
2351 rc = mwl8k_post_cmd(hw, &cmd->header);
2352 if (!rc) {
2353 stats->dot11ACKFailureCount =
2354 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2355 stats->dot11RTSFailureCount =
2356 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2357 stats->dot11FCSErrorCount =
2358 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2359 stats->dot11RTSSuccessCount =
2360 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2361 }
2362 kfree(cmd);
2363
2364 return rc;
2365}
2366
2367/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002368 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002369 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002370struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002371 struct mwl8k_cmd_pkt header;
2372 __le16 action;
2373 __le16 control;
2374 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002375} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002376
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002377static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002378mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002379{
2380 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002381 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002382 int rc;
2383
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002384 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002385 return 0;
2386
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002387 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2388 if (cmd == NULL)
2389 return -ENOMEM;
2390
2391 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2392 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2393 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002394 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002395 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2396
2397 rc = mwl8k_post_cmd(hw, &cmd->header);
2398 kfree(cmd);
2399
2400 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002401 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002402
2403 return rc;
2404}
2405
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002406static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002407{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002408 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002409}
2410
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002411static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002412{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002413 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002414}
2415
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002416static int
2417mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2418{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002419 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002420
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002421 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002422
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002423 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002424}
2425
2426/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002427 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002428 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002429#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002430
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002431struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002432 struct mwl8k_cmd_pkt header;
2433 __le16 action;
2434 __le16 support_level;
2435 __le16 current_level;
2436 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002437 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002438} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002439
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002440static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002441{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002442 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002443 int rc;
2444
2445 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2446 if (cmd == NULL)
2447 return -ENOMEM;
2448
2449 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2450 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2451 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2452 cmd->support_level = cpu_to_le16(dBm);
2453
2454 rc = mwl8k_post_cmd(hw, &cmd->header);
2455 kfree(cmd);
2456
2457 return rc;
2458}
2459
2460/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002461 * CMD_TX_POWER.
2462 */
2463#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2464
2465struct mwl8k_cmd_tx_power {
2466 struct mwl8k_cmd_pkt header;
2467 __le16 action;
2468 __le16 band;
2469 __le16 channel;
2470 __le16 bw;
2471 __le16 sub_ch;
2472 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2473} __attribute__((packed));
2474
2475static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2476 struct ieee80211_conf *conf,
2477 unsigned short pwr)
2478{
2479 struct ieee80211_channel *channel = conf->channel;
2480 struct mwl8k_cmd_tx_power *cmd;
2481 int rc;
2482 int i;
2483
2484 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2485 if (cmd == NULL)
2486 return -ENOMEM;
2487
2488 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2489 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2490 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2491
2492 if (channel->band == IEEE80211_BAND_2GHZ)
2493 cmd->band = cpu_to_le16(0x1);
2494 else if (channel->band == IEEE80211_BAND_5GHZ)
2495 cmd->band = cpu_to_le16(0x4);
2496
2497 cmd->channel = channel->hw_value;
2498
2499 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2500 conf->channel_type == NL80211_CHAN_HT20) {
2501 cmd->bw = cpu_to_le16(0x2);
2502 } else {
2503 cmd->bw = cpu_to_le16(0x4);
2504 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2505 cmd->sub_ch = cpu_to_le16(0x3);
2506 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2507 cmd->sub_ch = cpu_to_le16(0x1);
2508 }
2509
2510 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2511 cmd->power_level_list[i] = cpu_to_le16(pwr);
2512
2513 rc = mwl8k_post_cmd(hw, &cmd->header);
2514 kfree(cmd);
2515
2516 return rc;
2517}
2518
2519/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002520 * CMD_RF_ANTENNA.
2521 */
2522struct mwl8k_cmd_rf_antenna {
2523 struct mwl8k_cmd_pkt header;
2524 __le16 antenna;
2525 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002526} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002527
2528#define MWL8K_RF_ANTENNA_RX 1
2529#define MWL8K_RF_ANTENNA_TX 2
2530
2531static int
2532mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2533{
2534 struct mwl8k_cmd_rf_antenna *cmd;
2535 int rc;
2536
2537 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2538 if (cmd == NULL)
2539 return -ENOMEM;
2540
2541 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2542 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2543 cmd->antenna = cpu_to_le16(antenna);
2544 cmd->mode = cpu_to_le16(mask);
2545
2546 rc = mwl8k_post_cmd(hw, &cmd->header);
2547 kfree(cmd);
2548
2549 return rc;
2550}
2551
2552/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002553 * CMD_SET_BEACON.
2554 */
2555struct mwl8k_cmd_set_beacon {
2556 struct mwl8k_cmd_pkt header;
2557 __le16 beacon_len;
2558 __u8 beacon[0];
2559};
2560
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002561static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2562 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002563{
2564 struct mwl8k_cmd_set_beacon *cmd;
2565 int rc;
2566
2567 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2568 if (cmd == NULL)
2569 return -ENOMEM;
2570
2571 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2572 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2573 cmd->beacon_len = cpu_to_le16(len);
2574 memcpy(cmd->beacon, beacon, len);
2575
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002576 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002577 kfree(cmd);
2578
2579 return rc;
2580}
2581
2582/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002583 * CMD_SET_PRE_SCAN.
2584 */
2585struct mwl8k_cmd_set_pre_scan {
2586 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002587} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002588
2589static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2590{
2591 struct mwl8k_cmd_set_pre_scan *cmd;
2592 int rc;
2593
2594 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2595 if (cmd == NULL)
2596 return -ENOMEM;
2597
2598 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2599 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2600
2601 rc = mwl8k_post_cmd(hw, &cmd->header);
2602 kfree(cmd);
2603
2604 return rc;
2605}
2606
2607/*
2608 * CMD_SET_POST_SCAN.
2609 */
2610struct mwl8k_cmd_set_post_scan {
2611 struct mwl8k_cmd_pkt header;
2612 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002613 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002614} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002615
2616static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002617mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002618{
2619 struct mwl8k_cmd_set_post_scan *cmd;
2620 int rc;
2621
2622 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2623 if (cmd == NULL)
2624 return -ENOMEM;
2625
2626 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2627 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2628 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002629 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002630
2631 rc = mwl8k_post_cmd(hw, &cmd->header);
2632 kfree(cmd);
2633
2634 return rc;
2635}
2636
2637/*
2638 * CMD_SET_RF_CHANNEL.
2639 */
2640struct mwl8k_cmd_set_rf_channel {
2641 struct mwl8k_cmd_pkt header;
2642 __le16 action;
2643 __u8 current_channel;
2644 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002645} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002646
2647static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002648 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002649{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002650 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002651 struct mwl8k_cmd_set_rf_channel *cmd;
2652 int rc;
2653
2654 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2655 if (cmd == NULL)
2656 return -ENOMEM;
2657
2658 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2659 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2660 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2661 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002662
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002663 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002664 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002665 else if (channel->band == IEEE80211_BAND_5GHZ)
2666 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002667
2668 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2669 conf->channel_type == NL80211_CHAN_HT20)
2670 cmd->channel_flags |= cpu_to_le32(0x00000080);
2671 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2672 cmd->channel_flags |= cpu_to_le32(0x000001900);
2673 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2674 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002675
2676 rc = mwl8k_post_cmd(hw, &cmd->header);
2677 kfree(cmd);
2678
2679 return rc;
2680}
2681
2682/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002683 * CMD_SET_AID.
2684 */
2685#define MWL8K_FRAME_PROT_DISABLED 0x00
2686#define MWL8K_FRAME_PROT_11G 0x07
2687#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2688#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2689
2690struct mwl8k_cmd_update_set_aid {
2691 struct mwl8k_cmd_pkt header;
2692 __le16 aid;
2693
2694 /* AP's MAC address (BSSID) */
2695 __u8 bssid[ETH_ALEN];
2696 __le16 protection_mode;
2697 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002698} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002699
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002700static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2701{
2702 int i;
2703 int j;
2704
2705 /*
2706 * Clear nonstandard rates 4 and 13.
2707 */
2708 mask &= 0x1fef;
2709
2710 for (i = 0, j = 0; i < 14; i++) {
2711 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002712 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002713 }
2714}
2715
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002716static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002717mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2718 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002719{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002720 struct mwl8k_cmd_update_set_aid *cmd;
2721 u16 prot_mode;
2722 int rc;
2723
2724 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2725 if (cmd == NULL)
2726 return -ENOMEM;
2727
2728 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2729 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002730 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002731 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002732
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002733 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002734 prot_mode = MWL8K_FRAME_PROT_11G;
2735 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002736 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002737 IEEE80211_HT_OP_MODE_PROTECTION) {
2738 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2739 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2740 break;
2741 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2742 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2743 break;
2744 default:
2745 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2746 break;
2747 }
2748 }
2749 cmd->protection_mode = cpu_to_le16(prot_mode);
2750
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002751 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002752
2753 rc = mwl8k_post_cmd(hw, &cmd->header);
2754 kfree(cmd);
2755
2756 return rc;
2757}
2758
2759/*
2760 * CMD_SET_RATE.
2761 */
2762struct mwl8k_cmd_set_rate {
2763 struct mwl8k_cmd_pkt header;
2764 __u8 legacy_rates[14];
2765
2766 /* Bitmap for supported MCS codes. */
2767 __u8 mcs_set[16];
2768 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002769} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002770
2771static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002772mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002773 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002774{
2775 struct mwl8k_cmd_set_rate *cmd;
2776 int rc;
2777
2778 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2779 if (cmd == NULL)
2780 return -ENOMEM;
2781
2782 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2783 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002784 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002785 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002786
2787 rc = mwl8k_post_cmd(hw, &cmd->header);
2788 kfree(cmd);
2789
2790 return rc;
2791}
2792
2793/*
2794 * CMD_FINALIZE_JOIN.
2795 */
2796#define MWL8K_FJ_BEACON_MAXLEN 128
2797
2798struct mwl8k_cmd_finalize_join {
2799 struct mwl8k_cmd_pkt header;
2800 __le32 sleep_interval; /* Number of beacon periods to sleep */
2801 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002802} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002803
2804static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2805 int framelen, int dtim)
2806{
2807 struct mwl8k_cmd_finalize_join *cmd;
2808 struct ieee80211_mgmt *payload = frame;
2809 int payload_len;
2810 int rc;
2811
2812 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2813 if (cmd == NULL)
2814 return -ENOMEM;
2815
2816 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2817 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2818 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2819
2820 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2821 if (payload_len < 0)
2822 payload_len = 0;
2823 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2824 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2825
2826 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2827
2828 rc = mwl8k_post_cmd(hw, &cmd->header);
2829 kfree(cmd);
2830
2831 return rc;
2832}
2833
2834/*
2835 * CMD_SET_RTS_THRESHOLD.
2836 */
2837struct mwl8k_cmd_set_rts_threshold {
2838 struct mwl8k_cmd_pkt header;
2839 __le16 action;
2840 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002841} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002842
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002843static int
2844mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002845{
2846 struct mwl8k_cmd_set_rts_threshold *cmd;
2847 int rc;
2848
2849 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2850 if (cmd == NULL)
2851 return -ENOMEM;
2852
2853 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2854 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002855 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2856 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002857
2858 rc = mwl8k_post_cmd(hw, &cmd->header);
2859 kfree(cmd);
2860
2861 return rc;
2862}
2863
2864/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002865 * CMD_SET_SLOT.
2866 */
2867struct mwl8k_cmd_set_slot {
2868 struct mwl8k_cmd_pkt header;
2869 __le16 action;
2870 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002871} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002872
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002873static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002874{
2875 struct mwl8k_cmd_set_slot *cmd;
2876 int rc;
2877
2878 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2879 if (cmd == NULL)
2880 return -ENOMEM;
2881
2882 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2883 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2884 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002885 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002886
2887 rc = mwl8k_post_cmd(hw, &cmd->header);
2888 kfree(cmd);
2889
2890 return rc;
2891}
2892
2893/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002894 * CMD_SET_EDCA_PARAMS.
2895 */
2896struct mwl8k_cmd_set_edca_params {
2897 struct mwl8k_cmd_pkt header;
2898
2899 /* See MWL8K_SET_EDCA_XXX below */
2900 __le16 action;
2901
2902 /* TX opportunity in units of 32 us */
2903 __le16 txop;
2904
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002905 union {
2906 struct {
2907 /* Log exponent of max contention period: 0...15 */
2908 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002909
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002910 /* Log exponent of min contention period: 0...15 */
2911 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002912
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002913 /* Adaptive interframe spacing in units of 32us */
2914 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002915
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002916 /* TX queue to configure */
2917 __u8 txq;
2918 } ap;
2919 struct {
2920 /* Log exponent of max contention period: 0...15 */
2921 __u8 log_cw_max;
2922
2923 /* Log exponent of min contention period: 0...15 */
2924 __u8 log_cw_min;
2925
2926 /* Adaptive interframe spacing in units of 32us */
2927 __u8 aifs;
2928
2929 /* TX queue to configure */
2930 __u8 txq;
2931 } sta;
2932 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002933} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002934
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002935#define MWL8K_SET_EDCA_CW 0x01
2936#define MWL8K_SET_EDCA_TXOP 0x02
2937#define MWL8K_SET_EDCA_AIFS 0x04
2938
2939#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2940 MWL8K_SET_EDCA_TXOP | \
2941 MWL8K_SET_EDCA_AIFS)
2942
2943static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002944mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2945 __u16 cw_min, __u16 cw_max,
2946 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002947{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002948 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002949 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002950 int rc;
2951
2952 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2953 if (cmd == NULL)
2954 return -ENOMEM;
2955
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002956 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2957 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002958 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2959 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002960 if (priv->ap_fw) {
2961 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2962 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2963 cmd->ap.aifs = aifs;
2964 cmd->ap.txq = qnum;
2965 } else {
2966 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2967 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2968 cmd->sta.aifs = aifs;
2969 cmd->sta.txq = qnum;
2970 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002971
2972 rc = mwl8k_post_cmd(hw, &cmd->header);
2973 kfree(cmd);
2974
2975 return rc;
2976}
2977
2978/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002979 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002980 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002981struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002982 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002983 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002984} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002985
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002986static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002987{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002988 struct mwl8k_priv *priv = hw->priv;
2989 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002990 int rc;
2991
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002992 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2993 if (cmd == NULL)
2994 return -ENOMEM;
2995
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002996 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002997 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002998 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002999
3000 rc = mwl8k_post_cmd(hw, &cmd->header);
3001 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01003002
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003003 if (!rc)
3004 priv->wmm_enabled = enable;
3005
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003006 return rc;
3007}
3008
3009/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003010 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003011 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003012struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003013 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003014 __le32 action;
3015 __u8 rx_antenna_map;
3016 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003017} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003018
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003019static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003020{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003021 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003022 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003023
3024 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3025 if (cmd == NULL)
3026 return -ENOMEM;
3027
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003028 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003029 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003030 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3031 cmd->rx_antenna_map = rx;
3032 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003033
3034 rc = mwl8k_post_cmd(hw, &cmd->header);
3035 kfree(cmd);
3036
3037 return rc;
3038}
3039
3040/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003041 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003042 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003043struct mwl8k_cmd_use_fixed_rate_sta {
3044 struct mwl8k_cmd_pkt header;
3045 __le32 action;
3046 __le32 allow_rate_drop;
3047 __le32 num_rates;
3048 struct {
3049 __le32 is_ht_rate;
3050 __le32 enable_retry;
3051 __le32 rate;
3052 __le32 retry_count;
3053 } rate_entry[8];
3054 __le32 rate_type;
3055 __le32 reserved1;
3056 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003057} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003058
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003059#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003060#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003061
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003062static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003063{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003064 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003065 int rc;
3066
3067 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3068 if (cmd == NULL)
3069 return -ENOMEM;
3070
3071 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3072 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003073 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3074 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003075
3076 rc = mwl8k_post_cmd(hw, &cmd->header);
3077 kfree(cmd);
3078
3079 return rc;
3080}
3081
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003082/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003083 * CMD_USE_FIXED_RATE (AP version).
3084 */
3085struct mwl8k_cmd_use_fixed_rate_ap {
3086 struct mwl8k_cmd_pkt header;
3087 __le32 action;
3088 __le32 allow_rate_drop;
3089 __le32 num_rates;
3090 struct mwl8k_rate_entry_ap {
3091 __le32 is_ht_rate;
3092 __le32 enable_retry;
3093 __le32 rate;
3094 __le32 retry_count;
3095 } rate_entry[4];
3096 u8 multicast_rate;
3097 u8 multicast_rate_type;
3098 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003099} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003100
3101static int
3102mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3103{
3104 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3105 int rc;
3106
3107 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3108 if (cmd == NULL)
3109 return -ENOMEM;
3110
3111 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3112 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3113 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3114 cmd->multicast_rate = mcast;
3115 cmd->management_rate = mgmt;
3116
3117 rc = mwl8k_post_cmd(hw, &cmd->header);
3118 kfree(cmd);
3119
3120 return rc;
3121}
3122
3123/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003124 * CMD_ENABLE_SNIFFER.
3125 */
3126struct mwl8k_cmd_enable_sniffer {
3127 struct mwl8k_cmd_pkt header;
3128 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003129} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003130
3131static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3132{
3133 struct mwl8k_cmd_enable_sniffer *cmd;
3134 int rc;
3135
3136 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3137 if (cmd == NULL)
3138 return -ENOMEM;
3139
3140 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3141 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3142 cmd->action = cpu_to_le32(!!enable);
3143
3144 rc = mwl8k_post_cmd(hw, &cmd->header);
3145 kfree(cmd);
3146
3147 return rc;
3148}
3149
3150/*
3151 * CMD_SET_MAC_ADDR.
3152 */
3153struct mwl8k_cmd_set_mac_addr {
3154 struct mwl8k_cmd_pkt header;
3155 union {
3156 struct {
3157 __le16 mac_type;
3158 __u8 mac_addr[ETH_ALEN];
3159 } mbss;
3160 __u8 mac_addr[ETH_ALEN];
3161 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003162} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003163
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003164#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3165#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3166#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3167#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003168
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003169static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3170 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003171{
3172 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003173 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003174 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003175 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003176 int rc;
3177
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003178 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3179 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3180 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3181 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3182 else
3183 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3184 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3185 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3186 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3187 else
3188 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3189 }
3190
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003191 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3192 if (cmd == NULL)
3193 return -ENOMEM;
3194
3195 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3196 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3197 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003198 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003199 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3200 } else {
3201 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3202 }
3203
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003204 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003205 kfree(cmd);
3206
3207 return rc;
3208}
3209
3210/*
3211 * CMD_SET_RATEADAPT_MODE.
3212 */
3213struct mwl8k_cmd_set_rate_adapt_mode {
3214 struct mwl8k_cmd_pkt header;
3215 __le16 action;
3216 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003217} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003218
3219static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3220{
3221 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3222 int rc;
3223
3224 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3225 if (cmd == NULL)
3226 return -ENOMEM;
3227
3228 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3229 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3230 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3231 cmd->mode = cpu_to_le16(mode);
3232
3233 rc = mwl8k_post_cmd(hw, &cmd->header);
3234 kfree(cmd);
3235
3236 return rc;
3237}
3238
3239/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003240 * CMD_BSS_START.
3241 */
3242struct mwl8k_cmd_bss_start {
3243 struct mwl8k_cmd_pkt header;
3244 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003245} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003246
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003247static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3248 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003249{
3250 struct mwl8k_cmd_bss_start *cmd;
3251 int rc;
3252
3253 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3254 if (cmd == NULL)
3255 return -ENOMEM;
3256
3257 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3258 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3259 cmd->enable = cpu_to_le32(enable);
3260
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003261 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003262 kfree(cmd);
3263
3264 return rc;
3265}
3266
3267/*
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003268 * CMD_BASTREAM.
3269 */
3270
3271/*
3272 * UPSTREAM is tx direction
3273 */
3274#define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3275#define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3276
3277enum {
3278 MWL8K_BA_CREATE,
3279 MWL8K_BA_UPDATE,
3280 MWL8K_BA_DESTROY,
3281 MWL8K_BA_FLUSH,
3282 MWL8K_BA_CHECK,
3283} ba_stream_action_type;
3284
3285
3286struct mwl8k_create_ba_stream {
3287 __le32 flags;
3288 __le32 idle_thrs;
3289 __le32 bar_thrs;
3290 __le32 window_size;
3291 u8 peer_mac_addr[6];
3292 u8 dialog_token;
3293 u8 tid;
3294 u8 queue_id;
3295 u8 param_info;
3296 __le32 ba_context;
3297 u8 reset_seq_no_flag;
3298 __le16 curr_seq_no;
3299 u8 sta_src_mac_addr[6];
3300} __packed;
3301
3302struct mwl8k_destroy_ba_stream {
3303 __le32 flags;
3304 __le32 ba_context;
3305} __packed;
3306
3307struct mwl8k_cmd_bastream {
3308 struct mwl8k_cmd_pkt header;
3309 __le32 action;
3310 union {
3311 struct mwl8k_create_ba_stream create_params;
3312 struct mwl8k_destroy_ba_stream destroy_params;
3313 };
3314} __packed;
3315
3316static int
3317mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
3318{
3319 struct mwl8k_cmd_bastream *cmd;
3320 int rc;
3321
3322 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3323 if (cmd == NULL)
3324 return -ENOMEM;
3325
3326 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3327 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3328
3329 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3330
3331 cmd->create_params.queue_id = stream->idx;
3332 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3333 ETH_ALEN);
3334 cmd->create_params.tid = stream->tid;
3335
3336 cmd->create_params.flags =
3337 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3338 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3339
3340 rc = mwl8k_post_cmd(hw, &cmd->header);
3341
3342 kfree(cmd);
3343
3344 return rc;
3345}
3346
3347static int
3348mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3349 u8 buf_size)
3350{
3351 struct mwl8k_cmd_bastream *cmd;
3352 int rc;
3353
3354 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3355 if (cmd == NULL)
3356 return -ENOMEM;
3357
3358
3359 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3360 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3361
3362 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3363
3364 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3365 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3366 cmd->create_params.queue_id = stream->idx;
3367
3368 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3369 cmd->create_params.tid = stream->tid;
3370 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3371 cmd->create_params.reset_seq_no_flag = 1;
3372
3373 cmd->create_params.param_info =
3374 (stream->sta->ht_cap.ampdu_factor &
3375 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3376 ((stream->sta->ht_cap.ampdu_density << 2) &
3377 IEEE80211_HT_AMPDU_PARM_DENSITY);
3378
3379 cmd->create_params.flags =
3380 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3381 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3382
3383 rc = mwl8k_post_cmd(hw, &cmd->header);
3384
3385 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3386 stream->sta->addr, stream->tid);
3387 kfree(cmd);
3388
3389 return rc;
3390}
3391
3392static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3393 struct mwl8k_ampdu_stream *stream)
3394{
3395 struct mwl8k_cmd_bastream *cmd;
3396
3397 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3398 if (cmd == NULL)
3399 return;
3400
3401 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3402 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3403 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3404
3405 cmd->destroy_params.ba_context = cpu_to_le32(stream->idx);
3406 mwl8k_post_cmd(hw, &cmd->header);
3407
3408 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", stream->idx);
3409
3410 kfree(cmd);
3411}
3412
3413/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003414 * CMD_SET_NEW_STN.
3415 */
3416struct mwl8k_cmd_set_new_stn {
3417 struct mwl8k_cmd_pkt header;
3418 __le16 aid;
3419 __u8 mac_addr[6];
3420 __le16 stn_id;
3421 __le16 action;
3422 __le16 rsvd;
3423 __le32 legacy_rates;
3424 __u8 ht_rates[4];
3425 __le16 cap_info;
3426 __le16 ht_capabilities_info;
3427 __u8 mac_ht_param_info;
3428 __u8 rev;
3429 __u8 control_channel;
3430 __u8 add_channel;
3431 __le16 op_mode;
3432 __le16 stbc;
3433 __u8 add_qos_info;
3434 __u8 is_qos_sta;
3435 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003436} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003437
3438#define MWL8K_STA_ACTION_ADD 0
3439#define MWL8K_STA_ACTION_REMOVE 2
3440
3441static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3442 struct ieee80211_vif *vif,
3443 struct ieee80211_sta *sta)
3444{
3445 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003446 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003447 int rc;
3448
3449 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3450 if (cmd == NULL)
3451 return -ENOMEM;
3452
3453 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3454 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3455 cmd->aid = cpu_to_le16(sta->aid);
3456 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3457 cmd->stn_id = cpu_to_le16(sta->aid);
3458 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003459 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3460 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3461 else
3462 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3463 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003464 if (sta->ht_cap.ht_supported) {
3465 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3466 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3467 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3468 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3469 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3470 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3471 ((sta->ht_cap.ampdu_density & 7) << 2);
3472 cmd->is_qos_sta = 1;
3473 }
3474
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003475 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003476 kfree(cmd);
3477
3478 return rc;
3479}
3480
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003481static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3482 struct ieee80211_vif *vif)
3483{
3484 struct mwl8k_cmd_set_new_stn *cmd;
3485 int rc;
3486
3487 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3488 if (cmd == NULL)
3489 return -ENOMEM;
3490
3491 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3492 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3493 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3494
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003495 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003496 kfree(cmd);
3497
3498 return rc;
3499}
3500
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003501static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3502 struct ieee80211_vif *vif, u8 *addr)
3503{
3504 struct mwl8k_cmd_set_new_stn *cmd;
3505 int rc;
3506
3507 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3508 if (cmd == NULL)
3509 return -ENOMEM;
3510
3511 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3512 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3513 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3514 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3515
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003516 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003517 kfree(cmd);
3518
3519 return rc;
3520}
3521
3522/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003523 * CMD_UPDATE_ENCRYPTION.
3524 */
3525
3526#define MAX_ENCR_KEY_LENGTH 16
3527#define MIC_KEY_LENGTH 8
3528
3529struct mwl8k_cmd_update_encryption {
3530 struct mwl8k_cmd_pkt header;
3531
3532 __le32 action;
3533 __le32 reserved;
3534 __u8 mac_addr[6];
3535 __u8 encr_type;
3536
3537} __attribute__((packed));
3538
3539struct mwl8k_cmd_set_key {
3540 struct mwl8k_cmd_pkt header;
3541
3542 __le32 action;
3543 __le32 reserved;
3544 __le16 length;
3545 __le16 key_type_id;
3546 __le32 key_info;
3547 __le32 key_id;
3548 __le16 key_len;
3549 __u8 key_material[MAX_ENCR_KEY_LENGTH];
3550 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
3551 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
3552 __le16 tkip_rsc_low;
3553 __le32 tkip_rsc_high;
3554 __le16 tkip_tsc_low;
3555 __le32 tkip_tsc_high;
3556 __u8 mac_addr[6];
3557} __attribute__((packed));
3558
3559enum {
3560 MWL8K_ENCR_ENABLE,
3561 MWL8K_ENCR_SET_KEY,
3562 MWL8K_ENCR_REMOVE_KEY,
3563 MWL8K_ENCR_SET_GROUP_KEY,
3564};
3565
3566#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
3567#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
3568#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
3569#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
3570#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
3571
3572enum {
3573 MWL8K_ALG_WEP,
3574 MWL8K_ALG_TKIP,
3575 MWL8K_ALG_CCMP,
3576};
3577
3578#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
3579#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
3580#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
3581#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
3582#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
3583
3584static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
3585 struct ieee80211_vif *vif,
3586 u8 *addr,
3587 u8 encr_type)
3588{
3589 struct mwl8k_cmd_update_encryption *cmd;
3590 int rc;
3591
3592 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3593 if (cmd == NULL)
3594 return -ENOMEM;
3595
3596 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3597 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3598 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
3599 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3600 cmd->encr_type = encr_type;
3601
3602 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3603 kfree(cmd);
3604
3605 return rc;
3606}
3607
3608static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
3609 u8 *addr,
3610 struct ieee80211_key_conf *key)
3611{
3612 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3613 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3614 cmd->length = cpu_to_le16(sizeof(*cmd) -
3615 offsetof(struct mwl8k_cmd_set_key, length));
3616 cmd->key_id = cpu_to_le32(key->keyidx);
3617 cmd->key_len = cpu_to_le16(key->keylen);
3618 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3619
3620 switch (key->cipher) {
3621 case WLAN_CIPHER_SUITE_WEP40:
3622 case WLAN_CIPHER_SUITE_WEP104:
3623 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
3624 if (key->keyidx == 0)
3625 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
3626
3627 break;
3628 case WLAN_CIPHER_SUITE_TKIP:
3629 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
3630 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3631 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3632 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3633 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
3634 | MWL8K_KEY_FLAG_TSC_VALID);
3635 break;
3636 case WLAN_CIPHER_SUITE_CCMP:
3637 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
3638 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3639 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3640 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3641 break;
3642 default:
3643 return -ENOTSUPP;
3644 }
3645
3646 return 0;
3647}
3648
3649static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
3650 struct ieee80211_vif *vif,
3651 u8 *addr,
3652 struct ieee80211_key_conf *key)
3653{
3654 struct mwl8k_cmd_set_key *cmd;
3655 int rc;
3656 int keymlen;
3657 u32 action;
3658 u8 idx;
3659 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3660
3661 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3662 if (cmd == NULL)
3663 return -ENOMEM;
3664
3665 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3666 if (rc < 0)
3667 goto done;
3668
3669 idx = key->keyidx;
3670
3671 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3672 action = MWL8K_ENCR_SET_KEY;
3673 else
3674 action = MWL8K_ENCR_SET_GROUP_KEY;
3675
3676 switch (key->cipher) {
3677 case WLAN_CIPHER_SUITE_WEP40:
3678 case WLAN_CIPHER_SUITE_WEP104:
3679 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
3680 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
3681 sizeof(*key) + key->keylen);
3682 mwl8k_vif->wep_key_conf[idx].enabled = 1;
3683 }
3684
3685 keymlen = 0;
3686 action = MWL8K_ENCR_SET_KEY;
3687 break;
3688 case WLAN_CIPHER_SUITE_TKIP:
3689 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
3690 break;
3691 case WLAN_CIPHER_SUITE_CCMP:
3692 keymlen = key->keylen;
3693 break;
3694 default:
3695 rc = -ENOTSUPP;
3696 goto done;
3697 }
3698
3699 memcpy(cmd->key_material, key->key, keymlen);
3700 cmd->action = cpu_to_le32(action);
3701
3702 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3703done:
3704 kfree(cmd);
3705
3706 return rc;
3707}
3708
3709static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
3710 struct ieee80211_vif *vif,
3711 u8 *addr,
3712 struct ieee80211_key_conf *key)
3713{
3714 struct mwl8k_cmd_set_key *cmd;
3715 int rc;
3716 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3717
3718 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3719 if (cmd == NULL)
3720 return -ENOMEM;
3721
3722 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3723 if (rc < 0)
3724 goto done;
3725
3726 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3727 WLAN_CIPHER_SUITE_WEP104)
3728 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
3729
3730 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
3731
3732 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3733done:
3734 kfree(cmd);
3735
3736 return rc;
3737}
3738
3739static int mwl8k_set_key(struct ieee80211_hw *hw,
3740 enum set_key_cmd cmd_param,
3741 struct ieee80211_vif *vif,
3742 struct ieee80211_sta *sta,
3743 struct ieee80211_key_conf *key)
3744{
3745 int rc = 0;
3746 u8 encr_type;
3747 u8 *addr;
3748 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3749
3750 if (vif->type == NL80211_IFTYPE_STATION)
3751 return -EOPNOTSUPP;
3752
3753 if (sta == NULL)
3754 addr = hw->wiphy->perm_addr;
3755 else
3756 addr = sta->addr;
3757
3758 if (cmd_param == SET_KEY) {
3759 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3760 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
3761 if (rc)
3762 goto out;
3763
3764 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
3765 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
3766 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
3767 else
3768 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
3769
3770 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
3771 encr_type);
3772 if (rc)
3773 goto out;
3774
3775 mwl8k_vif->is_hw_crypto_enabled = true;
3776
3777 } else {
3778 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
3779
3780 if (rc)
3781 goto out;
3782
3783 mwl8k_vif->is_hw_crypto_enabled = false;
3784
3785 }
3786out:
3787 return rc;
3788}
3789
3790/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003791 * CMD_UPDATE_STADB.
3792 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003793struct ewc_ht_info {
3794 __le16 control1;
3795 __le16 control2;
3796 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003797} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003798
3799struct peer_capability_info {
3800 /* Peer type - AP vs. STA. */
3801 __u8 peer_type;
3802
3803 /* Basic 802.11 capabilities from assoc resp. */
3804 __le16 basic_caps;
3805
3806 /* Set if peer supports 802.11n high throughput (HT). */
3807 __u8 ht_support;
3808
3809 /* Valid if HT is supported. */
3810 __le16 ht_caps;
3811 __u8 extended_ht_caps;
3812 struct ewc_ht_info ewc_info;
3813
3814 /* Legacy rate table. Intersection of our rates and peer rates. */
3815 __u8 legacy_rates[12];
3816
3817 /* HT rate table. Intersection of our rates and peer rates. */
3818 __u8 ht_rates[16];
3819 __u8 pad[16];
3820
3821 /* If set, interoperability mode, no proprietary extensions. */
3822 __u8 interop;
3823 __u8 pad2;
3824 __u8 station_id;
3825 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003826} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003827
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003828struct mwl8k_cmd_update_stadb {
3829 struct mwl8k_cmd_pkt header;
3830
3831 /* See STADB_ACTION_TYPE */
3832 __le32 action;
3833
3834 /* Peer MAC address */
3835 __u8 peer_addr[ETH_ALEN];
3836
3837 __le32 reserved;
3838
3839 /* Peer info - valid during add/update. */
3840 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003841} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003842
Lennert Buytenheka6804002010-01-04 21:55:42 +01003843#define MWL8K_STA_DB_MODIFY_ENTRY 1
3844#define MWL8K_STA_DB_DEL_ENTRY 2
3845
3846/* Peer Entry flags - used to define the type of the peer node */
3847#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3848
3849static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003850 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003851 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003852{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003853 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003854 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003855 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003856 int rc;
3857
3858 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3859 if (cmd == NULL)
3860 return -ENOMEM;
3861
3862 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3863 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003864 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003865 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003866
Lennert Buytenheka6804002010-01-04 21:55:42 +01003867 p = &cmd->peer_info;
3868 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3869 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003870 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003871 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003872 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3873 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003874 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3875 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3876 else
3877 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3878 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003879 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003880 p->interop = 1;
3881 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003882
Lennert Buytenheka6804002010-01-04 21:55:42 +01003883 rc = mwl8k_post_cmd(hw, &cmd->header);
3884 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003885
Lennert Buytenheka6804002010-01-04 21:55:42 +01003886 return rc ? rc : p->station_id;
3887}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003888
Lennert Buytenheka6804002010-01-04 21:55:42 +01003889static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3890 struct ieee80211_vif *vif, u8 *addr)
3891{
3892 struct mwl8k_cmd_update_stadb *cmd;
3893 int rc;
3894
3895 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3896 if (cmd == NULL)
3897 return -ENOMEM;
3898
3899 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3900 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3901 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3902 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3903
3904 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003905 kfree(cmd);
3906
3907 return rc;
3908}
3909
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003910
3911/*
3912 * Interrupt handling.
3913 */
3914static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3915{
3916 struct ieee80211_hw *hw = dev_id;
3917 struct mwl8k_priv *priv = hw->priv;
3918 u32 status;
3919
3920 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003921 if (!status)
3922 return IRQ_NONE;
3923
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003924 if (status & MWL8K_A2H_INT_TX_DONE) {
3925 status &= ~MWL8K_A2H_INT_TX_DONE;
3926 tasklet_schedule(&priv->poll_tx_task);
3927 }
3928
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003929 if (status & MWL8K_A2H_INT_RX_READY) {
3930 status &= ~MWL8K_A2H_INT_RX_READY;
3931 tasklet_schedule(&priv->poll_rx_task);
3932 }
3933
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003934 if (status)
3935 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003936
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003937 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003938 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003939 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003940 }
3941
3942 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003943 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003944 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003945 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003946 }
3947
3948 return IRQ_HANDLED;
3949}
3950
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003951static void mwl8k_tx_poll(unsigned long data)
3952{
3953 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3954 struct mwl8k_priv *priv = hw->priv;
3955 int limit;
3956 int i;
3957
3958 limit = 32;
3959
3960 spin_lock_bh(&priv->tx_lock);
3961
Brian Cavagnoloe6007072011-03-17 11:58:44 -07003962 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003963 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3964
3965 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3966 complete(priv->tx_wait);
3967 priv->tx_wait = NULL;
3968 }
3969
3970 spin_unlock_bh(&priv->tx_lock);
3971
3972 if (limit) {
3973 writel(~MWL8K_A2H_INT_TX_DONE,
3974 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3975 } else {
3976 tasklet_schedule(&priv->poll_tx_task);
3977 }
3978}
3979
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003980static void mwl8k_rx_poll(unsigned long data)
3981{
3982 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3983 struct mwl8k_priv *priv = hw->priv;
3984 int limit;
3985
3986 limit = 32;
3987 limit -= rxq_process(hw, 0, limit);
3988 limit -= rxq_refill(hw, 0, limit);
3989
3990 if (limit) {
3991 writel(~MWL8K_A2H_INT_RX_READY,
3992 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3993 } else {
3994 tasklet_schedule(&priv->poll_rx_task);
3995 }
3996}
3997
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003998
3999/*
4000 * Core driver operations.
4001 */
Johannes Berg7bb45682011-02-24 14:42:06 +01004002static void mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004003{
4004 struct mwl8k_priv *priv = hw->priv;
4005 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004006
Lennert Buytenhek9189c102010-01-12 13:47:32 +01004007 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004008 wiphy_debug(hw->wiphy,
4009 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004010 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01004011 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004012 }
4013
Johannes Berg7bb45682011-02-24 14:42:06 +01004014 mwl8k_txq_xmit(hw, index, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004015}
4016
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004017static int mwl8k_start(struct ieee80211_hw *hw)
4018{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004019 struct mwl8k_priv *priv = hw->priv;
4020 int rc;
4021
Joe Perchesa0607fd2009-11-18 23:29:17 -08004022 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004023 IRQF_SHARED, MWL8K_NAME, hw);
4024 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004025 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004026 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004027 }
4028
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004029 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004030 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004031 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004032
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004033 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004034 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004035
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004036 rc = mwl8k_fw_lock(hw);
4037 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004038 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004039
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004040 if (!priv->ap_fw) {
4041 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004042 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004043
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004044 if (!rc)
4045 rc = mwl8k_cmd_set_pre_scan(hw);
4046
4047 if (!rc)
4048 rc = mwl8k_cmd_set_post_scan(hw,
4049 "\x00\x00\x00\x00\x00\x00");
4050 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004051
4052 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004053 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004054
4055 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004056 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004057
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004058 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004059 }
4060
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004061 if (rc) {
4062 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4063 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004064 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004065 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004066 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004067
4068 return rc;
4069}
4070
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004071static void mwl8k_stop(struct ieee80211_hw *hw)
4072{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004073 struct mwl8k_priv *priv = hw->priv;
4074 int i;
4075
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004076 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004077
4078 ieee80211_stop_queues(hw);
4079
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004080 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004081 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004082 free_irq(priv->pdev->irq, hw);
4083
4084 /* Stop finalize join worker */
4085 cancel_work_sync(&priv->finalize_join_worker);
4086 if (priv->beacon_skb != NULL)
4087 dev_kfree_skb(priv->beacon_skb);
4088
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004089 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004090 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004091 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004092
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004093 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004094 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004095 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004096}
4097
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004098static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4099
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004100static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004101 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004102{
4103 struct mwl8k_priv *priv = hw->priv;
4104 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004105 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004106 int macid, rc;
4107 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004108
4109 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004110 * Reject interface creation if sniffer mode is active, as
4111 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004112 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004113 */
4114 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004115 wiphy_info(hw->wiphy,
4116 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004117 return -EINVAL;
4118 }
4119
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004120 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004121 switch (vif->type) {
4122 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004123 if (!priv->ap_fw && di->fw_image_ap) {
4124 /* we must load the ap fw to meet this request */
4125 if (!list_empty(&priv->vif_list))
4126 return -EBUSY;
4127 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4128 if (rc)
4129 return rc;
4130 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004131 macids_supported = priv->ap_macids_supported;
4132 break;
4133 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004134 if (priv->ap_fw && di->fw_image_sta) {
4135 /* we must load the sta fw to meet this request */
4136 if (!list_empty(&priv->vif_list))
4137 return -EBUSY;
4138 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4139 if (rc)
4140 return rc;
4141 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004142 macids_supported = priv->sta_macids_supported;
4143 break;
4144 default:
4145 return -EINVAL;
4146 }
4147
4148 macid = ffs(macids_supported & ~priv->macids_used);
4149 if (!macid--)
4150 return -EBUSY;
4151
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004152 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01004153 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004154 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004155 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004156 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004157 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004158 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4159 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004160
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004161 /* Set the mac address. */
4162 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4163
4164 if (priv->ap_fw)
4165 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4166
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004167 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004168 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004169
4170 return 0;
4171}
4172
4173static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004174 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004175{
4176 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004177 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004178
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004179 if (priv->ap_fw)
4180 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4181
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004182 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004183
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004184 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004185 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004186}
4187
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004188static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004189{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004190 struct ieee80211_conf *conf = &hw->conf;
4191 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004192 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004193
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004194 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004195 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004196 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004197 }
4198
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004199 rc = mwl8k_fw_lock(hw);
4200 if (rc)
4201 return rc;
4202
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004203 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004204 if (rc)
4205 goto out;
4206
Lennert Buytenhek610677d2010-01-04 21:56:46 +01004207 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004208 if (rc)
4209 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004210
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004211 if (conf->power_level > 18)
4212 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004213
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004214 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004215 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4216 if (rc)
4217 goto out;
4218
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08004219 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
4220 if (rc)
4221 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
4222 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
4223 if (rc)
4224 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
4225
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004226 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004227 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4228 if (rc)
4229 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004230 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4231 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004232
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004233out:
4234 mwl8k_fw_unlock(hw);
4235
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004236 return rc;
4237}
4238
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004239static void
4240mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4241 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004242{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004243 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004244 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004245 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004246 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004247
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004248 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004249 return;
4250
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004251 /*
4252 * No need to capture a beacon if we're no longer associated.
4253 */
4254 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4255 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004256
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004257 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004258 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004259 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004260 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004261 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004262
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004263 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004264
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004265 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004266 if (ap == NULL) {
4267 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004268 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004269 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004270
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004271 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4272 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4273 } else {
4274 ap_legacy_rates =
4275 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4276 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004277 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004278
4279 rcu_read_unlock();
4280 }
4281
4282 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004283 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004284 if (rc)
4285 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004286
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004287 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004288 if (rc)
4289 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004290 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004291
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004292 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004293 rc = mwl8k_set_radio_preamble(hw,
4294 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004295 if (rc)
4296 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004297 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004298
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004299 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004300 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004301 if (rc)
4302 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004303 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004304
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004305 if (vif->bss_conf.assoc &&
4306 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4307 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004308 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004309 if (rc)
4310 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004311 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004312
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004313 if (vif->bss_conf.assoc &&
4314 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004315 /*
4316 * Finalize the join. Tell rx handler to process
4317 * next beacon from our BSSID.
4318 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004319 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004320 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004321 }
4322
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004323out:
4324 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004325}
4326
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004327static void
4328mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4329 struct ieee80211_bss_conf *info, u32 changed)
4330{
4331 int rc;
4332
4333 if (mwl8k_fw_lock(hw))
4334 return;
4335
4336 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4337 rc = mwl8k_set_radio_preamble(hw,
4338 vif->bss_conf.use_short_preamble);
4339 if (rc)
4340 goto out;
4341 }
4342
4343 if (changed & BSS_CHANGED_BASIC_RATES) {
4344 int idx;
4345 int rate;
4346
4347 /*
4348 * Use lowest supported basic rate for multicasts
4349 * and management frames (such as probe responses --
4350 * beacons will always go out at 1 Mb/s).
4351 */
4352 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004353 if (idx)
4354 idx--;
4355
4356 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4357 rate = mwl8k_rates_24[idx].hw_value;
4358 else
4359 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004360
4361 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4362 }
4363
4364 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4365 struct sk_buff *skb;
4366
4367 skb = ieee80211_beacon_get(hw, vif);
4368 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004369 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004370 kfree_skb(skb);
4371 }
4372 }
4373
4374 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004375 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004376
4377out:
4378 mwl8k_fw_unlock(hw);
4379}
4380
4381static void
4382mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4383 struct ieee80211_bss_conf *info, u32 changed)
4384{
4385 struct mwl8k_priv *priv = hw->priv;
4386
4387 if (!priv->ap_fw)
4388 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
4389 else
4390 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
4391}
4392
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004393static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00004394 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004395{
4396 struct mwl8k_cmd_pkt *cmd;
4397
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004398 /*
4399 * Synthesize and return a command packet that programs the
4400 * hardware multicast address filter. At this point we don't
4401 * know whether FIF_ALLMULTI is being requested, but if it is,
4402 * we'll end up throwing this packet away and creating a new
4403 * one in mwl8k_configure_filter().
4404 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00004405 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004406
4407 return (unsigned long)cmd;
4408}
4409
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004410static int
4411mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
4412 unsigned int changed_flags,
4413 unsigned int *total_flags)
4414{
4415 struct mwl8k_priv *priv = hw->priv;
4416
4417 /*
4418 * Hardware sniffer mode is mutually exclusive with STA
4419 * operation, so refuse to enable sniffer mode if a STA
4420 * interface is active.
4421 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004422 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004423 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07004424 wiphy_info(hw->wiphy,
4425 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004426 return 0;
4427 }
4428
4429 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004430 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004431 return 0;
4432 priv->sniffer_enabled = true;
4433 }
4434
4435 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
4436 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
4437 FIF_OTHER_BSS;
4438
4439 return 1;
4440}
4441
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004442static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
4443{
4444 if (!list_empty(&priv->vif_list))
4445 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
4446
4447 return NULL;
4448}
4449
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004450static void mwl8k_configure_filter(struct ieee80211_hw *hw,
4451 unsigned int changed_flags,
4452 unsigned int *total_flags,
4453 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004454{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004455 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004456 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
4457
4458 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02004459 * AP firmware doesn't allow fine-grained control over
4460 * the receive filter.
4461 */
4462 if (priv->ap_fw) {
4463 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
4464 kfree(cmd);
4465 return;
4466 }
4467
4468 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004469 * Enable hardware sniffer mode if FIF_CONTROL or
4470 * FIF_OTHER_BSS is requested.
4471 */
4472 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
4473 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
4474 kfree(cmd);
4475 return;
4476 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004477
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004478 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004479 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004480
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004481 if (mwl8k_fw_lock(hw)) {
4482 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004483 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004484 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004485
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004486 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004487 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004488 priv->sniffer_enabled = false;
4489 }
4490
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004491 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004492 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
4493 /*
4494 * Disable the BSS filter.
4495 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004496 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004497 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004498 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004499 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004500
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004501 /*
4502 * Enable the BSS filter.
4503 *
4504 * If there is an active STA interface, use that
4505 * interface's BSSID, otherwise use a dummy one
4506 * (where the OUI part needs to be nonzero for
4507 * the BSSID to be accepted by POST_SCAN).
4508 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004509 mwl8k_vif = mwl8k_first_vif(priv);
4510 if (mwl8k_vif != NULL)
4511 bssid = mwl8k_vif->vif->bss_conf.bssid;
4512 else
4513 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004514
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004515 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004516 }
4517 }
4518
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004519 /*
4520 * If FIF_ALLMULTI is being requested, throw away the command
4521 * packet that ->prepare_multicast() built and replace it with
4522 * a command packet that enables reception of all multicast
4523 * packets.
4524 */
4525 if (*total_flags & FIF_ALLMULTI) {
4526 kfree(cmd);
Jiri Pirko22bedad32010-04-01 21:22:57 +00004527 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004528 }
4529
4530 if (cmd != NULL) {
4531 mwl8k_post_cmd(hw, cmd);
4532 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004533 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004534
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004535 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004536}
4537
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004538static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4539{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004540 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004541}
4542
Johannes Berg4a6967b2010-02-19 19:18:37 +01004543static int mwl8k_sta_remove(struct ieee80211_hw *hw,
4544 struct ieee80211_vif *vif,
4545 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004546{
4547 struct mwl8k_priv *priv = hw->priv;
4548
Johannes Berg4a6967b2010-02-19 19:18:37 +01004549 if (priv->ap_fw)
4550 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
4551 else
4552 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
4553}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004554
Johannes Berg4a6967b2010-02-19 19:18:37 +01004555static int mwl8k_sta_add(struct ieee80211_hw *hw,
4556 struct ieee80211_vif *vif,
4557 struct ieee80211_sta *sta)
4558{
4559 struct mwl8k_priv *priv = hw->priv;
4560 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004561 int i;
4562 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4563 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004564
Johannes Berg4a6967b2010-02-19 19:18:37 +01004565 if (!priv->ap_fw) {
4566 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
4567 if (ret >= 0) {
4568 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004569 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004570 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01004571
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004572 } else {
4573 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004574 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004575
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004576 for (i = 0; i < NUM_WEP_KEYS; i++) {
4577 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
4578 if (mwl8k_vif->wep_key_conf[i].enabled)
4579 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
4580 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004581 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004582}
4583
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004584static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
4585 const struct ieee80211_tx_queue_params *params)
4586{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004587 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004588 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004589
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004590 rc = mwl8k_fw_lock(hw);
4591 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004592 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004593 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
4594
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004595 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004596 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004597
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004598 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004599 int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004600 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004601 params->cw_min,
4602 params->cw_max,
4603 params->aifs,
4604 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004605 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004606
4607 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004608 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004609
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004610 return rc;
4611}
4612
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004613static int mwl8k_get_stats(struct ieee80211_hw *hw,
4614 struct ieee80211_low_level_stats *stats)
4615{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004616 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004617}
4618
John W. Linville0d462bb2010-07-28 14:04:24 -04004619static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
4620 struct survey_info *survey)
4621{
4622 struct mwl8k_priv *priv = hw->priv;
4623 struct ieee80211_conf *conf = &hw->conf;
4624
4625 if (idx != 0)
4626 return -ENOENT;
4627
4628 survey->channel = conf->channel;
4629 survey->filled = SURVEY_INFO_NOISE_DBM;
4630 survey->noise = priv->noise;
4631
4632 return 0;
4633}
4634
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004635static int
4636mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4637 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01004638 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4639 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004640{
4641 switch (action) {
4642 case IEEE80211_AMPDU_RX_START:
4643 case IEEE80211_AMPDU_RX_STOP:
4644 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
4645 return -ENOTSUPP;
4646 return 0;
4647 default:
4648 return -ENOTSUPP;
4649 }
4650}
4651
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004652static const struct ieee80211_ops mwl8k_ops = {
4653 .tx = mwl8k_tx,
4654 .start = mwl8k_start,
4655 .stop = mwl8k_stop,
4656 .add_interface = mwl8k_add_interface,
4657 .remove_interface = mwl8k_remove_interface,
4658 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004659 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004660 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004661 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004662 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004663 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01004664 .sta_add = mwl8k_sta_add,
4665 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004666 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004667 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04004668 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004669 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004670};
4671
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004672static void mwl8k_finalize_join_worker(struct work_struct *work)
4673{
4674 struct mwl8k_priv *priv =
4675 container_of(work, struct mwl8k_priv, finalize_join_worker);
4676 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01004677 struct ieee80211_mgmt *mgmt = (void *)skb->data;
4678 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
4679 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
4680 mgmt->u.beacon.variable, len);
4681 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004682
Johannes Berg56007a02010-01-26 14:19:52 +01004683 if (tim && tim[1] >= 2)
4684 dtim_period = tim[3];
4685
4686 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004687
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004688 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004689 priv->beacon_skb = NULL;
4690}
4691
John W. Linvillebcb628d2009-11-06 16:40:16 -05004692enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004693 MWL8363 = 0,
4694 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004695 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02004696};
4697
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07004698#define MWL8K_8366_AP_FW_API 2
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004699#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
4700#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
4701
John W. Linvillebcb628d2009-11-06 16:40:16 -05004702static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004703 [MWL8363] = {
4704 .part_name = "88w8363",
4705 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004706 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004707 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004708 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004709 .part_name = "88w8687",
4710 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004711 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05004712 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004713 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004714 .part_name = "88w8366",
4715 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004716 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004717 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
4718 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004719 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004720 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004721};
4722
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004723MODULE_FIRMWARE("mwl8k/helper_8363.fw");
4724MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
4725MODULE_FIRMWARE("mwl8k/helper_8687.fw");
4726MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
4727MODULE_FIRMWARE("mwl8k/helper_8366.fw");
4728MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004729MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004730
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004731static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01004732 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004733 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
4734 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004735 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
4736 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
4737 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01004738 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004739 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004740};
4741MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
4742
Brian Cavagnolo99020472010-11-12 17:23:52 -08004743static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
4744{
4745 int rc;
4746 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
4747 "Trying alternative firmware %s\n", pci_name(priv->pdev),
4748 priv->fw_pref, priv->fw_alt);
4749 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
4750 if (rc) {
4751 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4752 pci_name(priv->pdev), priv->fw_alt);
4753 return rc;
4754 }
4755 return 0;
4756}
4757
4758static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
4759static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
4760{
4761 struct mwl8k_priv *priv = context;
4762 struct mwl8k_device_info *di = priv->device_info;
4763 int rc;
4764
4765 switch (priv->fw_state) {
4766 case FW_STATE_INIT:
4767 if (!fw) {
4768 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
4769 pci_name(priv->pdev), di->helper_image);
4770 goto fail;
4771 }
4772 priv->fw_helper = fw;
4773 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
4774 true);
4775 if (rc && priv->fw_alt) {
4776 rc = mwl8k_request_alt_fw(priv);
4777 if (rc)
4778 goto fail;
4779 priv->fw_state = FW_STATE_LOADING_ALT;
4780 } else if (rc)
4781 goto fail;
4782 else
4783 priv->fw_state = FW_STATE_LOADING_PREF;
4784 break;
4785
4786 case FW_STATE_LOADING_PREF:
4787 if (!fw) {
4788 if (priv->fw_alt) {
4789 rc = mwl8k_request_alt_fw(priv);
4790 if (rc)
4791 goto fail;
4792 priv->fw_state = FW_STATE_LOADING_ALT;
4793 } else
4794 goto fail;
4795 } else {
4796 priv->fw_ucode = fw;
4797 rc = mwl8k_firmware_load_success(priv);
4798 if (rc)
4799 goto fail;
4800 else
4801 complete(&priv->firmware_loading_complete);
4802 }
4803 break;
4804
4805 case FW_STATE_LOADING_ALT:
4806 if (!fw) {
4807 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4808 pci_name(priv->pdev), di->helper_image);
4809 goto fail;
4810 }
4811 priv->fw_ucode = fw;
4812 rc = mwl8k_firmware_load_success(priv);
4813 if (rc)
4814 goto fail;
4815 else
4816 complete(&priv->firmware_loading_complete);
4817 break;
4818
4819 default:
4820 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
4821 MWL8K_NAME, priv->fw_state);
4822 BUG_ON(1);
4823 }
4824
4825 return;
4826
4827fail:
4828 priv->fw_state = FW_STATE_ERROR;
4829 complete(&priv->firmware_loading_complete);
4830 device_release_driver(&priv->pdev->dev);
4831 mwl8k_release_firmware(priv);
4832}
4833
4834static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
4835 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004836{
4837 struct mwl8k_priv *priv = hw->priv;
4838 int rc;
4839
4840 /* Reset firmware and hardware */
4841 mwl8k_hw_reset(priv);
4842
4843 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004844 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004845 if (rc) {
4846 wiphy_err(hw->wiphy, "Firmware files not found\n");
4847 return rc;
4848 }
4849
Brian Cavagnolo99020472010-11-12 17:23:52 -08004850 if (nowait)
4851 return rc;
4852
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004853 /* Load firmware into hardware */
4854 rc = mwl8k_load_firmware(hw);
4855 if (rc)
4856 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4857
4858 /* Reclaim memory once firmware is successfully loaded */
4859 mwl8k_release_firmware(priv);
4860
4861 return rc;
4862}
4863
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004864static int mwl8k_init_txqs(struct ieee80211_hw *hw)
4865{
4866 struct mwl8k_priv *priv = hw->priv;
4867 int rc = 0;
4868 int i;
4869
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004870 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004871 rc = mwl8k_txq_init(hw, i);
4872 if (rc)
4873 break;
4874 if (priv->ap_fw)
4875 iowrite32(priv->txq[i].txd_dma,
4876 priv->sram + priv->txq_offset[i]);
4877 }
4878 return rc;
4879}
4880
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004881/* initialize hw after successfully loading a firmware image */
4882static int mwl8k_probe_hw(struct ieee80211_hw *hw)
4883{
4884 struct mwl8k_priv *priv = hw->priv;
4885 int rc = 0;
4886 int i;
4887
4888 if (priv->ap_fw) {
4889 priv->rxd_ops = priv->device_info->ap_rxd_ops;
4890 if (priv->rxd_ops == NULL) {
4891 wiphy_err(hw->wiphy,
4892 "Driver does not have AP firmware image support for this hardware\n");
4893 goto err_stop_firmware;
4894 }
4895 } else {
4896 priv->rxd_ops = &rxd_sta_ops;
4897 }
4898
4899 priv->sniffer_enabled = false;
4900 priv->wmm_enabled = false;
4901 priv->pending_tx_pkts = 0;
4902
4903 rc = mwl8k_rxq_init(hw, 0);
4904 if (rc)
4905 goto err_stop_firmware;
4906 rxq_refill(hw, 0, INT_MAX);
4907
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004908 /* For the sta firmware, we need to know the dma addresses of tx queues
4909 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
4910 * prior to issuing this command. But for the AP case, we learn the
4911 * total number of queues from the result CMD_GET_HW_SPEC, so for this
4912 * case we must initialize the tx queues after.
4913 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07004914 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004915 if (!priv->ap_fw) {
4916 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004917 if (rc)
4918 goto err_free_queues;
4919 }
4920
4921 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4922 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4923 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
4924 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
4925 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4926
4927 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4928 IRQF_SHARED, MWL8K_NAME, hw);
4929 if (rc) {
4930 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4931 goto err_free_queues;
4932 }
4933
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07004934 memset(priv->ampdu, 0, sizeof(priv->ampdu));
4935
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004936 /*
4937 * Temporarily enable interrupts. Initial firmware host
4938 * commands use interrupts and avoid polling. Disable
4939 * interrupts when done.
4940 */
4941 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4942
4943 /* Get config data, mac addrs etc */
4944 if (priv->ap_fw) {
4945 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4946 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07004947 rc = mwl8k_init_txqs(hw);
4948 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004949 rc = mwl8k_cmd_set_hw_spec(hw);
4950 } else {
4951 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4952 }
4953 if (rc) {
4954 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
4955 goto err_free_irq;
4956 }
4957
4958 /* Turn radio off */
4959 rc = mwl8k_cmd_radio_disable(hw);
4960 if (rc) {
4961 wiphy_err(hw->wiphy, "Cannot disable\n");
4962 goto err_free_irq;
4963 }
4964
4965 /* Clear MAC address */
4966 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
4967 if (rc) {
4968 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
4969 goto err_free_irq;
4970 }
4971
4972 /* Disable interrupts */
4973 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4974 free_irq(priv->pdev->irq, hw);
4975
4976 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
4977 priv->device_info->part_name,
4978 priv->hw_rev, hw->wiphy->perm_addr,
4979 priv->ap_fw ? "AP" : "STA",
4980 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4981 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
4982
4983 return 0;
4984
4985err_free_irq:
4986 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4987 free_irq(priv->pdev->irq, hw);
4988
4989err_free_queues:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004990 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004991 mwl8k_txq_deinit(hw, i);
4992 mwl8k_rxq_deinit(hw, 0);
4993
4994err_stop_firmware:
4995 mwl8k_hw_reset(priv);
4996
4997 return rc;
4998}
4999
5000/*
5001 * invoke mwl8k_reload_firmware to change the firmware image after the device
5002 * has already been registered
5003 */
5004static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5005{
5006 int i, rc = 0;
5007 struct mwl8k_priv *priv = hw->priv;
5008
5009 mwl8k_stop(hw);
5010 mwl8k_rxq_deinit(hw, 0);
5011
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005012 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005013 mwl8k_txq_deinit(hw, i);
5014
Brian Cavagnolo99020472010-11-12 17:23:52 -08005015 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005016 if (rc)
5017 goto fail;
5018
5019 rc = mwl8k_probe_hw(hw);
5020 if (rc)
5021 goto fail;
5022
5023 rc = mwl8k_start(hw);
5024 if (rc)
5025 goto fail;
5026
5027 rc = mwl8k_config(hw, ~0);
5028 if (rc)
5029 goto fail;
5030
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005031 for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005032 rc = mwl8k_conf_tx(hw, i, &priv->wmm_params[i]);
5033 if (rc)
5034 goto fail;
5035 }
5036
5037 return rc;
5038
5039fail:
5040 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5041 return rc;
5042}
5043
5044static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5045{
5046 struct ieee80211_hw *hw = priv->hw;
5047 int i, rc;
5048
Brian Cavagnolo99020472010-11-12 17:23:52 -08005049 rc = mwl8k_load_firmware(hw);
5050 mwl8k_release_firmware(priv);
5051 if (rc) {
5052 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5053 return rc;
5054 }
5055
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005056 /*
5057 * Extra headroom is the size of the required DMA header
5058 * minus the size of the smallest 802.11 frame (CTS frame).
5059 */
5060 hw->extra_tx_headroom =
5061 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5062
5063 hw->channel_change_time = 10;
5064
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005065 hw->queues = MWL8K_TX_WMM_QUEUES;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005066
5067 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08005068 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005069 hw->vif_data_size = sizeof(struct mwl8k_vif);
5070 hw->sta_data_size = sizeof(struct mwl8k_sta);
5071
5072 priv->macids_used = 0;
5073 INIT_LIST_HEAD(&priv->vif_list);
5074
5075 /* Set default radio state and preamble */
5076 priv->radio_on = 0;
5077 priv->radio_short_preamble = 0;
5078
5079 /* Finalize join worker */
5080 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
5081
5082 /* TX reclaim and RX tasklets. */
5083 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5084 tasklet_disable(&priv->poll_tx_task);
5085 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5086 tasklet_disable(&priv->poll_rx_task);
5087
5088 /* Power management cookie */
5089 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5090 if (priv->cookie == NULL)
5091 return -ENOMEM;
5092
5093 mutex_init(&priv->fw_mutex);
5094 priv->fw_mutex_owner = NULL;
5095 priv->fw_mutex_depth = 0;
5096 priv->hostcmd_wait = NULL;
5097
5098 spin_lock_init(&priv->tx_lock);
5099
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005100 spin_lock_init(&priv->stream_lock);
5101
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005102 priv->tx_wait = NULL;
5103
5104 rc = mwl8k_probe_hw(hw);
5105 if (rc)
5106 goto err_free_cookie;
5107
5108 hw->wiphy->interface_modes = 0;
5109 if (priv->ap_macids_supported || priv->device_info->fw_image_ap)
5110 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
5111 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5112 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5113
5114 rc = ieee80211_register_hw(hw);
5115 if (rc) {
5116 wiphy_err(hw->wiphy, "Cannot register device\n");
5117 goto err_unprobe_hw;
5118 }
5119
5120 return 0;
5121
5122err_unprobe_hw:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005123 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005124 mwl8k_txq_deinit(hw, i);
5125 mwl8k_rxq_deinit(hw, 0);
5126
5127err_free_cookie:
5128 if (priv->cookie != NULL)
5129 pci_free_consistent(priv->pdev, 4,
5130 priv->cookie, priv->cookie_dma);
5131
5132 return rc;
5133}
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005134static int __devinit mwl8k_probe(struct pci_dev *pdev,
5135 const struct pci_device_id *id)
5136{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005137 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005138 struct ieee80211_hw *hw;
5139 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005140 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005141 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02005142
5143 if (!printed_version) {
5144 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5145 printed_version = 1;
5146 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005147
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005148
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005149 rc = pci_enable_device(pdev);
5150 if (rc) {
5151 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
5152 MWL8K_NAME);
5153 return rc;
5154 }
5155
5156 rc = pci_request_regions(pdev, MWL8K_NAME);
5157 if (rc) {
5158 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
5159 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005160 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005161 }
5162
5163 pci_set_master(pdev);
5164
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005165
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005166 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
5167 if (hw == NULL) {
5168 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
5169 rc = -ENOMEM;
5170 goto err_free_reg;
5171 }
5172
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005173 SET_IEEE80211_DEV(hw, &pdev->dev);
5174 pci_set_drvdata(pdev, hw);
5175
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005176 priv = hw->priv;
5177 priv->hw = hw;
5178 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05005179 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005180
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005181
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005182 priv->sram = pci_iomap(pdev, 0, 0x10000);
5183 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005184 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005185 goto err_iounmap;
5186 }
5187
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005188 /*
5189 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
5190 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
5191 */
5192 priv->regs = pci_iomap(pdev, 1, 0x10000);
5193 if (priv->regs == NULL) {
5194 priv->regs = pci_iomap(pdev, 2, 0x10000);
5195 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005196 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005197 goto err_iounmap;
5198 }
5199 }
5200
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005201 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08005202 * Choose the initial fw image depending on user input. If a second
5203 * image is available, make it the alternative image that will be
5204 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005205 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005206 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005207 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005208 if (ap_mode_default && di->fw_image_ap) {
5209 priv->fw_pref = di->fw_image_ap;
5210 priv->fw_alt = di->fw_image_sta;
5211 } else if (!ap_mode_default && di->fw_image_sta) {
5212 priv->fw_pref = di->fw_image_sta;
5213 priv->fw_alt = di->fw_image_ap;
5214 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005215 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005216 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005217 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
5218 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005219 priv->fw_pref = di->fw_image_ap;
5220 }
5221 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005222 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005223 goto err_stop_firmware;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005224 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005225
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005226err_stop_firmware:
5227 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005228
5229err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005230 if (priv->regs != NULL)
5231 pci_iounmap(pdev, priv->regs);
5232
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005233 if (priv->sram != NULL)
5234 pci_iounmap(pdev, priv->sram);
5235
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005236 pci_set_drvdata(pdev, NULL);
5237 ieee80211_free_hw(hw);
5238
5239err_free_reg:
5240 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005241
5242err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005243 pci_disable_device(pdev);
5244
5245 return rc;
5246}
5247
Joerg Albert230f7af2009-04-18 02:10:45 +02005248static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005249{
5250 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
5251}
5252
Joerg Albert230f7af2009-04-18 02:10:45 +02005253static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005254{
5255 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
5256 struct mwl8k_priv *priv;
5257 int i;
5258
5259 if (hw == NULL)
5260 return;
5261 priv = hw->priv;
5262
Brian Cavagnolo99020472010-11-12 17:23:52 -08005263 wait_for_completion(&priv->firmware_loading_complete);
5264
5265 if (priv->fw_state == FW_STATE_ERROR) {
5266 mwl8k_hw_reset(priv);
5267 goto unmap;
5268 }
5269
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005270 ieee80211_stop_queues(hw);
5271
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02005272 ieee80211_unregister_hw(hw);
5273
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005274 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01005275 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005276 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005277
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005278 /* Stop hardware */
5279 mwl8k_hw_reset(priv);
5280
5281 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005282 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01005283 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005284
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005285 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005286 mwl8k_txq_deinit(hw, i);
5287
5288 mwl8k_rxq_deinit(hw, 0);
5289
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005290 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005291
Brian Cavagnolo99020472010-11-12 17:23:52 -08005292unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005293 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005294 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005295 pci_set_drvdata(pdev, NULL);
5296 ieee80211_free_hw(hw);
5297 pci_release_regions(pdev);
5298 pci_disable_device(pdev);
5299}
5300
5301static struct pci_driver mwl8k_driver = {
5302 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005303 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005304 .probe = mwl8k_probe,
5305 .remove = __devexit_p(mwl8k_remove),
5306 .shutdown = __devexit_p(mwl8k_shutdown),
5307};
5308
5309static int __init mwl8k_init(void)
5310{
5311 return pci_register_driver(&mwl8k_driver);
5312}
5313
5314static void __exit mwl8k_exit(void)
5315{
5316 pci_unregister_driver(&mwl8k_driver);
5317}
5318
5319module_init(mwl8k_init);
5320module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005321
5322MODULE_DESCRIPTION(MWL8K_DESC);
5323MODULE_VERSION(MWL8K_VERSION);
5324MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
5325MODULE_LICENSE("GPL");