blob: ec1190ab0f8e9b4a543e6257d02f10ab1601d5fb [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 Buytenhek42fba212009-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 Buytenhekaa21d0f2010-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 Buytenhekaa21d0f2010-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 Buytenhekaa21d0f2010-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 Buytenhek42fba212009-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 Buytenhekd4b705702009-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
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001580 if (index < MWL8K_TX_WMM_QUEUES && processed && priv->radio_on &&
1581 !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001582 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001583
1584 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001585}
1586
1587/* must be called only when the card's transmit is completely halted */
1588static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1589{
1590 struct mwl8k_priv *priv = hw->priv;
1591 struct mwl8k_tx_queue *txq = priv->txq + index;
1592
Brian Cavagnolo73b46322011-03-17 11:58:41 -07001593 if (txq->txd == NULL)
1594 return;
1595
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001596 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001597
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001598 kfree(txq->skb);
1599 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001600
1601 pci_free_consistent(priv->pdev,
1602 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001603 txq->txd, txq->txd_dma);
1604 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001605}
1606
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07001607/* caller must hold priv->stream_lock when calling the stream functions */
1608struct mwl8k_ampdu_stream *
1609mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1610{
1611 struct mwl8k_ampdu_stream *stream;
1612 struct mwl8k_priv *priv = hw->priv;
1613 int i;
1614
1615 for (i = 0; i < priv->num_ampdu_queues; i++) {
1616 stream = &priv->ampdu[i];
1617 if (stream->state == AMPDU_NO_STREAM) {
1618 stream->sta = sta;
1619 stream->state = AMPDU_STREAM_NEW;
1620 stream->tid = tid;
1621 stream->idx = i;
1622 stream->txq_idx = MWL8K_TX_WMM_QUEUES + i;
1623 wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1624 sta->addr, tid);
1625 return stream;
1626 }
1627 }
1628 return NULL;
1629}
1630
1631static int
1632mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1633{
1634 int ret;
1635
1636 /* if the stream has already been started, don't start it again */
1637 if (stream->state != AMPDU_STREAM_NEW)
1638 return 0;
1639 ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1640 if (ret)
1641 wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1642 "%d\n", stream->sta->addr, stream->tid, ret);
1643 else
1644 wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1645 stream->sta->addr, stream->tid);
1646 return ret;
1647}
1648
1649static void
1650mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1651{
1652 wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1653 stream->tid);
1654 memset(stream, 0, sizeof(*stream));
1655}
1656
1657static struct mwl8k_ampdu_stream *
1658mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1659{
1660 struct mwl8k_priv *priv = hw->priv;
1661 int i;
1662
1663 for (i = 0 ; i < priv->num_ampdu_queues; i++) {
1664 struct mwl8k_ampdu_stream *stream;
1665 stream = &priv->ampdu[i];
1666 if (stream->state == AMPDU_NO_STREAM)
1667 continue;
1668 if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1669 stream->tid == tid)
1670 return stream;
1671 }
1672 return NULL;
1673}
1674
Johannes Berg7bb45682011-02-24 14:42:06 +01001675static void
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001676mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1677{
1678 struct mwl8k_priv *priv = hw->priv;
1679 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001680 struct mwl8k_vif *mwl8k_vif;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001681 struct ieee80211_sta *sta;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001682 struct ieee80211_hdr *wh;
1683 struct mwl8k_tx_queue *txq;
1684 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001685 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001686 u32 txstatus;
1687 u8 txdatarate;
1688 u16 qos;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001689 int txpriority;
1690 u8 tid = 0;
1691 struct mwl8k_ampdu_stream *stream = NULL;
1692 bool start_ba_session = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001693
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001694 wh = (struct ieee80211_hdr *)skb->data;
1695 if (ieee80211_is_data_qos(wh->frame_control))
1696 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1697 else
1698 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001699
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08001700 if (priv->ap_fw)
1701 mwl8k_encapsulate_tx_frame(skb);
1702 else
1703 mwl8k_add_dma_header(skb, 0);
1704
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001705 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001706
1707 tx_info = IEEE80211_SKB_CB(skb);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001708 sta = tx_info->control.sta;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001709 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001710
1711 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001712 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001713 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1714 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001715 }
1716
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001717 /* Setup firmware control bit fields for each frame type. */
1718 txstatus = 0;
1719 txdatarate = 0;
1720 if (ieee80211_is_mgmt(wh->frame_control) ||
1721 ieee80211_is_ctl(wh->frame_control)) {
1722 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001723 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001724 } else if (ieee80211_is_data(wh->frame_control)) {
1725 txdatarate = 1;
1726 if (is_multicast_ether_addr(wh->addr1))
1727 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1728
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001729 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001730 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001731 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001732 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001733 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001734 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001735
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001736 txpriority = index;
1737
1738 if (ieee80211_is_data_qos(wh->frame_control) &&
1739 skb->protocol != cpu_to_be16(ETH_P_PAE) &&
1740 sta->ht_cap.ht_supported && priv->ap_fw) {
1741 tid = qos & 0xf;
1742 spin_lock(&priv->stream_lock);
1743 stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1744 if (stream != NULL) {
1745 if (stream->state == AMPDU_STREAM_ACTIVE) {
1746 txpriority = stream->txq_idx;
1747 index = stream->txq_idx;
1748 } else if (stream->state == AMPDU_STREAM_NEW) {
1749 /* We get here if the driver sends us packets
1750 * after we've initiated a stream, but before
1751 * our ampdu_action routine has been called
1752 * with IEEE80211_AMPDU_TX_START to get the SSN
1753 * for the ADDBA request. So this packet can
1754 * go out with no risk of sequence number
1755 * mismatch. No special handling is required.
1756 */
1757 } else {
1758 /* Drop packets that would go out after the
1759 * ADDBA request was sent but before the ADDBA
1760 * response is received. If we don't do this,
1761 * the recipient would probably receive it
1762 * after the ADDBA request with SSN 0. This
1763 * will cause the recipient's BA receive window
1764 * to shift, which would cause the subsequent
1765 * packets in the BA stream to be discarded.
1766 * mac80211 queues our packets for us in this
1767 * case, so this is really just a safety check.
1768 */
1769 wiphy_warn(hw->wiphy,
1770 "Cannot send packet while ADDBA "
1771 "dialog is underway.\n");
1772 spin_unlock(&priv->stream_lock);
1773 dev_kfree_skb(skb);
1774 return;
1775 }
1776 } else {
1777 /* Defer calling mwl8k_start_stream so that the current
1778 * skb can go out before the ADDBA request. This
1779 * prevents sequence number mismatch at the recepient
1780 * as described above.
1781 */
1782 stream = mwl8k_add_stream(hw, sta, tid);
1783 if (stream != NULL)
1784 start_ba_session = true;
1785 }
1786 spin_unlock(&priv->stream_lock);
1787 }
1788
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001789 dma = pci_map_single(priv->pdev, skb->data,
1790 skb->len, PCI_DMA_TODEVICE);
1791
1792 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001793 wiphy_debug(hw->wiphy,
1794 "failed to dma map skb, dropping TX frame.\n");
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001795 if (start_ba_session) {
1796 spin_lock(&priv->stream_lock);
1797 mwl8k_remove_stream(hw, stream);
1798 spin_unlock(&priv->stream_lock);
1799 }
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001800 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01001801 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001802 }
1803
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001804 spin_lock_bh(&priv->tx_lock);
1805
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001806 txq = priv->txq + index;
1807
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001808 if (index >= MWL8K_TX_WMM_QUEUES && txq->len >= MWL8K_TX_DESCS) {
1809 /* This is the case in which the tx packet is destined for an
1810 * AMPDU queue and that AMPDU queue is full. Because we don't
1811 * start and stop the AMPDU queues, we must drop these packets.
1812 */
1813 dev_kfree_skb(skb);
1814 spin_unlock_bh(&priv->tx_lock);
1815 return;
1816 }
1817
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001818 BUG_ON(txq->skb[txq->tail] != NULL);
1819 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001820
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001821 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001822 tx->data_rate = txdatarate;
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001823 tx->tx_priority = txpriority;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001824 tx->qos_control = cpu_to_le16(qos);
1825 tx->pkt_phys_addr = cpu_to_le32(dma);
1826 tx->pkt_len = cpu_to_le16(skb->len);
1827 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001828 if (!priv->ap_fw && tx_info->control.sta != NULL)
1829 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1830 else
1831 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001832 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001833 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1834
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001835 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001836 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001837
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001838 txq->tail++;
1839 if (txq->tail == MWL8K_TX_DESCS)
1840 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001841
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001842 if (txq->head == txq->tail && index < MWL8K_TX_WMM_QUEUES)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001843 ieee80211_stop_queue(hw, index);
1844
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001845 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001846
1847 spin_unlock_bh(&priv->tx_lock);
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07001848
1849 /* Initiate the ampdu session here */
1850 if (start_ba_session) {
1851 spin_lock(&priv->stream_lock);
1852 if (mwl8k_start_stream(hw, stream))
1853 mwl8k_remove_stream(hw, stream);
1854 spin_unlock(&priv->stream_lock);
1855 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001856}
1857
1858
1859/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001860 * Firmware access.
1861 *
1862 * We have the following requirements for issuing firmware commands:
1863 * - Some commands require that the packet transmit path is idle when
1864 * the command is issued. (For simplicity, we'll just quiesce the
1865 * transmit path for every command.)
1866 * - There are certain sequences of commands that need to be issued to
1867 * the hardware sequentially, with no other intervening commands.
1868 *
1869 * This leads to an implementation of a "firmware lock" as a mutex that
1870 * can be taken recursively, and which is taken by both the low-level
1871 * command submission function (mwl8k_post_cmd) as well as any users of
1872 * that function that require issuing of an atomic sequence of commands,
1873 * and quiesces the transmit path whenever it's taken.
1874 */
1875static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1876{
1877 struct mwl8k_priv *priv = hw->priv;
1878
1879 if (priv->fw_mutex_owner != current) {
1880 int rc;
1881
1882 mutex_lock(&priv->fw_mutex);
1883 ieee80211_stop_queues(hw);
1884
1885 rc = mwl8k_tx_wait_empty(hw);
1886 if (rc) {
1887 ieee80211_wake_queues(hw);
1888 mutex_unlock(&priv->fw_mutex);
1889
1890 return rc;
1891 }
1892
1893 priv->fw_mutex_owner = current;
1894 }
1895
1896 priv->fw_mutex_depth++;
1897
1898 return 0;
1899}
1900
1901static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1902{
1903 struct mwl8k_priv *priv = hw->priv;
1904
1905 if (!--priv->fw_mutex_depth) {
1906 ieee80211_wake_queues(hw);
1907 priv->fw_mutex_owner = NULL;
1908 mutex_unlock(&priv->fw_mutex);
1909 }
1910}
1911
1912
1913/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001914 * Command processing.
1915 */
1916
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001917/* Timeout firmware commands after 10s */
1918#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001919
1920static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1921{
1922 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1923 struct mwl8k_priv *priv = hw->priv;
1924 void __iomem *regs = priv->regs;
1925 dma_addr_t dma_addr;
1926 unsigned int dma_size;
1927 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001928 unsigned long timeout = 0;
1929 u8 buf[32];
1930
John W. Linvilleb6037422010-07-20 13:55:00 -04001931 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001932 dma_size = le16_to_cpu(cmd->length);
1933 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1934 PCI_DMA_BIDIRECTIONAL);
1935 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1936 return -ENOMEM;
1937
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001938 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001939 if (rc) {
1940 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1941 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001942 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001943 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001944
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001945 priv->hostcmd_wait = &cmd_wait;
1946 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1947 iowrite32(MWL8K_H2A_INT_DOORBELL,
1948 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1949 iowrite32(MWL8K_H2A_INT_DUMMY,
1950 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001951
1952 timeout = wait_for_completion_timeout(&cmd_wait,
1953 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1954
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001955 priv->hostcmd_wait = NULL;
1956
1957 mwl8k_fw_unlock(hw);
1958
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001959 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1960 PCI_DMA_BIDIRECTIONAL);
1961
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001962 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001963 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001964 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1965 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001966 rc = -ETIMEDOUT;
1967 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001968 int ms;
1969
1970 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1971
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001972 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001973 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001974 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001975 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1976 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001977 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001978 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001979 mwl8k_cmd_name(cmd->code,
1980 buf, sizeof(buf)),
1981 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001982 }
1983
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001984 return rc;
1985}
1986
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001987static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1988 struct ieee80211_vif *vif,
1989 struct mwl8k_cmd_pkt *cmd)
1990{
1991 if (vif != NULL)
1992 cmd->macid = MWL8K_VIF(vif)->macid;
1993 return mwl8k_post_cmd(hw, cmd);
1994}
1995
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001996/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001997 * Setup code shared between STA and AP firmware images.
1998 */
1999static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2000{
2001 struct mwl8k_priv *priv = hw->priv;
2002
2003 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2004 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2005
2006 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2007 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2008
2009 priv->band_24.band = IEEE80211_BAND_2GHZ;
2010 priv->band_24.channels = priv->channels_24;
2011 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2012 priv->band_24.bitrates = priv->rates_24;
2013 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2014
2015 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
2016}
2017
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01002018static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2019{
2020 struct mwl8k_priv *priv = hw->priv;
2021
2022 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2023 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2024
2025 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2026 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2027
2028 priv->band_50.band = IEEE80211_BAND_5GHZ;
2029 priv->band_50.channels = priv->channels_50;
2030 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2031 priv->band_50.bitrates = priv->rates_50;
2032 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2033
2034 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
2035}
2036
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01002037/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002038 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002039 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002040struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002041 struct mwl8k_cmd_pkt header;
2042 __u8 hw_rev;
2043 __u8 host_interface;
2044 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002045 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002046 __le16 region_code;
2047 __le32 fw_rev;
2048 __le32 ps_cookie;
2049 __le32 caps;
2050 __u8 mcs_bitmap[16];
2051 __le32 rx_queue_ptr;
2052 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002053 __le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002054 __le32 caps2;
2055 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002056 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002057} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002058
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002059#define MWL8K_CAP_MAX_AMSDU 0x20000000
2060#define MWL8K_CAP_GREENFIELD 0x08000000
2061#define MWL8K_CAP_AMPDU 0x04000000
2062#define MWL8K_CAP_RX_STBC 0x01000000
2063#define MWL8K_CAP_TX_STBC 0x00800000
2064#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
2065#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
2066#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
2067#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
2068#define MWL8K_CAP_DELAY_BA 0x00003000
2069#define MWL8K_CAP_MIMO 0x00000200
2070#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01002071#define MWL8K_CAP_BAND_MASK 0x00000007
2072#define MWL8K_CAP_5GHZ 0x00000004
2073#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002074
Lennert Buytenhek06953232010-01-12 13:49:41 +01002075static void
2076mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2077 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002078{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002079 int rx_streams;
2080 int tx_streams;
2081
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002082 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002083
2084 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002085 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002086 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002087 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002088 if (cap & MWL8K_CAP_AMPDU) {
2089 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002090 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2091 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002092 }
2093 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002094 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002095 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002096 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002097 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002098 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002099 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002100 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002101 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002102 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002103 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002104 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002105
2106 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2107 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2108
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002109 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002110 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002111 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002112 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002113 band->ht_cap.mcs.rx_mask[2] = 0xff;
2114 band->ht_cap.mcs.rx_mask[4] = 0x01;
2115 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002116
2117 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002118 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2119 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01002120 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2121 }
2122}
2123
Lennert Buytenhek06953232010-01-12 13:49:41 +01002124static void
2125mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2126{
2127 struct mwl8k_priv *priv = hw->priv;
2128
2129 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2130 mwl8k_setup_2ghz_band(hw);
2131 if (caps & MWL8K_CAP_MIMO)
2132 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2133 }
2134
2135 if (caps & MWL8K_CAP_5GHZ) {
2136 mwl8k_setup_5ghz_band(hw);
2137 if (caps & MWL8K_CAP_MIMO)
2138 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2139 }
2140}
2141
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002142static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002143{
2144 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02002145 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002146 int rc;
2147 int i;
2148
2149 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2150 if (cmd == NULL)
2151 return -ENOMEM;
2152
2153 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2154 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2155
2156 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2157 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002158 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002159 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2160 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002161 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002162 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02002163 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002164
2165 rc = mwl8k_post_cmd(hw, &cmd->header);
2166
2167 if (!rc) {
2168 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2169 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02002170 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002171 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01002172 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002173 priv->ap_macids_supported = 0x00000000;
2174 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002175 }
2176
2177 kfree(cmd);
2178 return rc;
2179}
2180
2181/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002182 * CMD_GET_HW_SPEC (AP version).
2183 */
2184struct mwl8k_cmd_get_hw_spec_ap {
2185 struct mwl8k_cmd_pkt header;
2186 __u8 hw_rev;
2187 __u8 host_interface;
2188 __le16 num_wcb;
2189 __le16 num_mcaddrs;
2190 __u8 perm_addr[ETH_ALEN];
2191 __le16 region_code;
2192 __le16 num_antenna;
2193 __le32 fw_rev;
2194 __le32 wcbbase0;
2195 __le32 rxwrptr;
2196 __le32 rxrdptr;
2197 __le32 ps_cookie;
2198 __le32 wcbbase1;
2199 __le32 wcbbase2;
2200 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002201 __le32 fw_api_version;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002202 __le32 caps;
2203 __le32 num_of_ampdu_queues;
2204 __le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002205} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002206
2207static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2208{
2209 struct mwl8k_priv *priv = hw->priv;
2210 struct mwl8k_cmd_get_hw_spec_ap *cmd;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002211 int rc, i;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002212 u32 api_version;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002213
2214 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2215 if (cmd == NULL)
2216 return -ENOMEM;
2217
2218 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2219 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2220
2221 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2222 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2223
2224 rc = mwl8k_post_cmd(hw, &cmd->header);
2225
2226 if (!rc) {
2227 int off;
2228
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002229 api_version = le32_to_cpu(cmd->fw_api_version);
2230 if (priv->device_info->fw_api_ap != api_version) {
2231 printk(KERN_ERR "%s: Unsupported fw API version for %s."
2232 " Expected %d got %d.\n", MWL8K_NAME,
2233 priv->device_info->part_name,
2234 priv->device_info->fw_api_ap,
2235 api_version);
2236 rc = -EINVAL;
2237 goto done;
2238 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002239 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2240 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2241 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2242 priv->hw_rev = cmd->hw_rev;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002243 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002244 priv->ap_macids_supported = 0x000000ff;
2245 priv->sta_macids_supported = 0x00000000;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002246 priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2247 if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2248 wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2249 " but we only support %d.\n",
2250 priv->num_ampdu_queues,
2251 MWL8K_MAX_AMPDU_QUEUES);
2252 priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2253 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002254 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002255 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002256
2257 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04002258 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002259
Brian Cavagnolo73b46322011-03-17 11:58:41 -07002260 priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2261 priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2262 priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2263 priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002264
2265 for (i = 0; i < priv->num_ampdu_queues; i++)
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002266 priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002267 le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002268 }
2269
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08002270done:
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002271 kfree(cmd);
2272 return rc;
2273}
2274
2275/*
2276 * CMD_SET_HW_SPEC.
2277 */
2278struct mwl8k_cmd_set_hw_spec {
2279 struct mwl8k_cmd_pkt header;
2280 __u8 hw_rev;
2281 __u8 host_interface;
2282 __le16 num_mcaddrs;
2283 __u8 perm_addr[ETH_ALEN];
2284 __le16 region_code;
2285 __le32 fw_rev;
2286 __le32 ps_cookie;
2287 __le32 caps;
2288 __le32 rx_queue_ptr;
2289 __le32 num_tx_queues;
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002290 __le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002291 __le32 flags;
2292 __le32 num_tx_desc_per_queue;
2293 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002294} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002295
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07002296/* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2297 * packets to expire 500 ms after the timestamp in the tx descriptor. That is,
2298 * the packets that are queued for more than 500ms, will be dropped in the
2299 * hardware. This helps minimizing the issues caused due to head-of-line
2300 * blocking where a slow client can hog the bandwidth and affect traffic to a
2301 * faster client.
2302 */
2303#define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY 0x00000400
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002304#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2305#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2306#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002307
2308static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2309{
2310 struct mwl8k_priv *priv = hw->priv;
2311 struct mwl8k_cmd_set_hw_spec *cmd;
2312 int rc;
2313 int i;
2314
2315 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2316 if (cmd == NULL)
2317 return -ENOMEM;
2318
2319 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2320 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2321
2322 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2323 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002324 cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002325
2326 /*
2327 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2328 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2329 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2330 * priority is interpreted the right way in firmware.
2331 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07002332 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2333 int j = mwl8k_tx_queues(priv) - 1 - i;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08002334 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2335 }
2336
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002337 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2338 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2339 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002340 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2341 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2342
2343 rc = mwl8k_post_cmd(hw, &cmd->header);
2344 kfree(cmd);
2345
2346 return rc;
2347}
2348
2349/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002350 * CMD_MAC_MULTICAST_ADR.
2351 */
2352struct mwl8k_cmd_mac_multicast_adr {
2353 struct mwl8k_cmd_pkt header;
2354 __le16 action;
2355 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002356 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002357};
2358
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002359#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2360#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2361#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2362#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002363
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002364static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002365__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002366 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002367{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002368 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002369 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002370 int size;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002371 int mc_count = 0;
2372
2373 if (mc_list)
2374 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002375
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002376 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002377 allmulti = 1;
2378 mc_count = 0;
2379 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002380
2381 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2382
2383 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002384 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002385 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002386
2387 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2388 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002389 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2390 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002391
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002392 if (allmulti) {
2393 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2394 } else if (mc_count) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00002395 struct netdev_hw_addr *ha;
2396 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002397
2398 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2399 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad2010-04-01 21:22:57 +00002400 netdev_hw_addr_list_for_each(ha, mc_list) {
2401 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002402 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002403 }
2404
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002405 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002406}
2407
2408/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002409 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002410 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002411struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002412 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002413 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002414} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002415
2416#define MWL8K_STAT_ACK_FAILURE 9
2417#define MWL8K_STAT_RTS_FAILURE 12
2418#define MWL8K_STAT_FCS_ERROR 24
2419#define MWL8K_STAT_RTS_SUCCESS 11
2420
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002421static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2422 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002423{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002424 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002425 int rc;
2426
2427 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2428 if (cmd == NULL)
2429 return -ENOMEM;
2430
2431 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2432 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002433
2434 rc = mwl8k_post_cmd(hw, &cmd->header);
2435 if (!rc) {
2436 stats->dot11ACKFailureCount =
2437 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2438 stats->dot11RTSFailureCount =
2439 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2440 stats->dot11FCSErrorCount =
2441 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2442 stats->dot11RTSSuccessCount =
2443 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2444 }
2445 kfree(cmd);
2446
2447 return rc;
2448}
2449
2450/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002451 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002452 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002453struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002454 struct mwl8k_cmd_pkt header;
2455 __le16 action;
2456 __le16 control;
2457 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002458} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002459
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002460static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002461mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002462{
2463 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002464 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002465 int rc;
2466
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002467 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002468 return 0;
2469
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002470 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2471 if (cmd == NULL)
2472 return -ENOMEM;
2473
2474 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2475 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2476 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002477 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002478 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2479
2480 rc = mwl8k_post_cmd(hw, &cmd->header);
2481 kfree(cmd);
2482
2483 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002484 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002485
2486 return rc;
2487}
2488
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002489static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002490{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002491 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002492}
2493
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002494static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002495{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002496 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002497}
2498
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002499static int
2500mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2501{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002502 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002503
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002504 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002505
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002506 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002507}
2508
2509/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002510 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002511 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002512#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002513
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002514struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002515 struct mwl8k_cmd_pkt header;
2516 __le16 action;
2517 __le16 support_level;
2518 __le16 current_level;
2519 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002520 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002521} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002522
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002523static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002524{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002525 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002526 int rc;
2527
2528 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2529 if (cmd == NULL)
2530 return -ENOMEM;
2531
2532 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2533 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2534 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2535 cmd->support_level = cpu_to_le16(dBm);
2536
2537 rc = mwl8k_post_cmd(hw, &cmd->header);
2538 kfree(cmd);
2539
2540 return rc;
2541}
2542
2543/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002544 * CMD_TX_POWER.
2545 */
2546#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2547
2548struct mwl8k_cmd_tx_power {
2549 struct mwl8k_cmd_pkt header;
2550 __le16 action;
2551 __le16 band;
2552 __le16 channel;
2553 __le16 bw;
2554 __le16 sub_ch;
2555 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2556} __attribute__((packed));
2557
2558static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2559 struct ieee80211_conf *conf,
2560 unsigned short pwr)
2561{
2562 struct ieee80211_channel *channel = conf->channel;
2563 struct mwl8k_cmd_tx_power *cmd;
2564 int rc;
2565 int i;
2566
2567 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2568 if (cmd == NULL)
2569 return -ENOMEM;
2570
2571 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2572 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2573 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2574
2575 if (channel->band == IEEE80211_BAND_2GHZ)
2576 cmd->band = cpu_to_le16(0x1);
2577 else if (channel->band == IEEE80211_BAND_5GHZ)
2578 cmd->band = cpu_to_le16(0x4);
2579
2580 cmd->channel = channel->hw_value;
2581
2582 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2583 conf->channel_type == NL80211_CHAN_HT20) {
2584 cmd->bw = cpu_to_le16(0x2);
2585 } else {
2586 cmd->bw = cpu_to_le16(0x4);
2587 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2588 cmd->sub_ch = cpu_to_le16(0x3);
2589 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2590 cmd->sub_ch = cpu_to_le16(0x1);
2591 }
2592
2593 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2594 cmd->power_level_list[i] = cpu_to_le16(pwr);
2595
2596 rc = mwl8k_post_cmd(hw, &cmd->header);
2597 kfree(cmd);
2598
2599 return rc;
2600}
2601
2602/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002603 * CMD_RF_ANTENNA.
2604 */
2605struct mwl8k_cmd_rf_antenna {
2606 struct mwl8k_cmd_pkt header;
2607 __le16 antenna;
2608 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002609} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002610
2611#define MWL8K_RF_ANTENNA_RX 1
2612#define MWL8K_RF_ANTENNA_TX 2
2613
2614static int
2615mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2616{
2617 struct mwl8k_cmd_rf_antenna *cmd;
2618 int rc;
2619
2620 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2621 if (cmd == NULL)
2622 return -ENOMEM;
2623
2624 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2625 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2626 cmd->antenna = cpu_to_le16(antenna);
2627 cmd->mode = cpu_to_le16(mask);
2628
2629 rc = mwl8k_post_cmd(hw, &cmd->header);
2630 kfree(cmd);
2631
2632 return rc;
2633}
2634
2635/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002636 * CMD_SET_BEACON.
2637 */
2638struct mwl8k_cmd_set_beacon {
2639 struct mwl8k_cmd_pkt header;
2640 __le16 beacon_len;
2641 __u8 beacon[0];
2642};
2643
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002644static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2645 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002646{
2647 struct mwl8k_cmd_set_beacon *cmd;
2648 int rc;
2649
2650 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2651 if (cmd == NULL)
2652 return -ENOMEM;
2653
2654 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2655 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2656 cmd->beacon_len = cpu_to_le16(len);
2657 memcpy(cmd->beacon, beacon, len);
2658
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002659 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002660 kfree(cmd);
2661
2662 return rc;
2663}
2664
2665/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002666 * CMD_SET_PRE_SCAN.
2667 */
2668struct mwl8k_cmd_set_pre_scan {
2669 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002670} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002671
2672static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2673{
2674 struct mwl8k_cmd_set_pre_scan *cmd;
2675 int rc;
2676
2677 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2678 if (cmd == NULL)
2679 return -ENOMEM;
2680
2681 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2682 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2683
2684 rc = mwl8k_post_cmd(hw, &cmd->header);
2685 kfree(cmd);
2686
2687 return rc;
2688}
2689
2690/*
2691 * CMD_SET_POST_SCAN.
2692 */
2693struct mwl8k_cmd_set_post_scan {
2694 struct mwl8k_cmd_pkt header;
2695 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002696 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002697} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002698
2699static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002700mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002701{
2702 struct mwl8k_cmd_set_post_scan *cmd;
2703 int rc;
2704
2705 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2706 if (cmd == NULL)
2707 return -ENOMEM;
2708
2709 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2710 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2711 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002712 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002713
2714 rc = mwl8k_post_cmd(hw, &cmd->header);
2715 kfree(cmd);
2716
2717 return rc;
2718}
2719
2720/*
2721 * CMD_SET_RF_CHANNEL.
2722 */
2723struct mwl8k_cmd_set_rf_channel {
2724 struct mwl8k_cmd_pkt header;
2725 __le16 action;
2726 __u8 current_channel;
2727 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002728} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002729
2730static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002731 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002732{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002733 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002734 struct mwl8k_cmd_set_rf_channel *cmd;
2735 int rc;
2736
2737 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2738 if (cmd == NULL)
2739 return -ENOMEM;
2740
2741 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2742 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2743 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2744 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002745
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002746 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002747 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002748 else if (channel->band == IEEE80211_BAND_5GHZ)
2749 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002750
2751 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2752 conf->channel_type == NL80211_CHAN_HT20)
2753 cmd->channel_flags |= cpu_to_le32(0x00000080);
2754 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2755 cmd->channel_flags |= cpu_to_le32(0x000001900);
2756 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2757 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002758
2759 rc = mwl8k_post_cmd(hw, &cmd->header);
2760 kfree(cmd);
2761
2762 return rc;
2763}
2764
2765/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002766 * CMD_SET_AID.
2767 */
2768#define MWL8K_FRAME_PROT_DISABLED 0x00
2769#define MWL8K_FRAME_PROT_11G 0x07
2770#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2771#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2772
2773struct mwl8k_cmd_update_set_aid {
2774 struct mwl8k_cmd_pkt header;
2775 __le16 aid;
2776
2777 /* AP's MAC address (BSSID) */
2778 __u8 bssid[ETH_ALEN];
2779 __le16 protection_mode;
2780 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002781} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002782
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002783static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2784{
2785 int i;
2786 int j;
2787
2788 /*
2789 * Clear nonstandard rates 4 and 13.
2790 */
2791 mask &= 0x1fef;
2792
2793 for (i = 0, j = 0; i < 14; i++) {
2794 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002795 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002796 }
2797}
2798
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002799static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002800mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2801 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002802{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002803 struct mwl8k_cmd_update_set_aid *cmd;
2804 u16 prot_mode;
2805 int rc;
2806
2807 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2808 if (cmd == NULL)
2809 return -ENOMEM;
2810
2811 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2812 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002813 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002814 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002815
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002816 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002817 prot_mode = MWL8K_FRAME_PROT_11G;
2818 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002819 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002820 IEEE80211_HT_OP_MODE_PROTECTION) {
2821 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2822 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2823 break;
2824 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2825 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2826 break;
2827 default:
2828 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2829 break;
2830 }
2831 }
2832 cmd->protection_mode = cpu_to_le16(prot_mode);
2833
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002834 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002835
2836 rc = mwl8k_post_cmd(hw, &cmd->header);
2837 kfree(cmd);
2838
2839 return rc;
2840}
2841
2842/*
2843 * CMD_SET_RATE.
2844 */
2845struct mwl8k_cmd_set_rate {
2846 struct mwl8k_cmd_pkt header;
2847 __u8 legacy_rates[14];
2848
2849 /* Bitmap for supported MCS codes. */
2850 __u8 mcs_set[16];
2851 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002852} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002853
2854static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002855mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002856 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002857{
2858 struct mwl8k_cmd_set_rate *cmd;
2859 int rc;
2860
2861 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2862 if (cmd == NULL)
2863 return -ENOMEM;
2864
2865 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2866 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002867 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002868 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002869
2870 rc = mwl8k_post_cmd(hw, &cmd->header);
2871 kfree(cmd);
2872
2873 return rc;
2874}
2875
2876/*
2877 * CMD_FINALIZE_JOIN.
2878 */
2879#define MWL8K_FJ_BEACON_MAXLEN 128
2880
2881struct mwl8k_cmd_finalize_join {
2882 struct mwl8k_cmd_pkt header;
2883 __le32 sleep_interval; /* Number of beacon periods to sleep */
2884 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002885} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002886
2887static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2888 int framelen, int dtim)
2889{
2890 struct mwl8k_cmd_finalize_join *cmd;
2891 struct ieee80211_mgmt *payload = frame;
2892 int payload_len;
2893 int rc;
2894
2895 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2896 if (cmd == NULL)
2897 return -ENOMEM;
2898
2899 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2900 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2901 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2902
2903 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2904 if (payload_len < 0)
2905 payload_len = 0;
2906 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2907 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2908
2909 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2910
2911 rc = mwl8k_post_cmd(hw, &cmd->header);
2912 kfree(cmd);
2913
2914 return rc;
2915}
2916
2917/*
2918 * CMD_SET_RTS_THRESHOLD.
2919 */
2920struct mwl8k_cmd_set_rts_threshold {
2921 struct mwl8k_cmd_pkt header;
2922 __le16 action;
2923 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002924} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002925
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002926static int
2927mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002928{
2929 struct mwl8k_cmd_set_rts_threshold *cmd;
2930 int rc;
2931
2932 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2933 if (cmd == NULL)
2934 return -ENOMEM;
2935
2936 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2937 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002938 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2939 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002940
2941 rc = mwl8k_post_cmd(hw, &cmd->header);
2942 kfree(cmd);
2943
2944 return rc;
2945}
2946
2947/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002948 * CMD_SET_SLOT.
2949 */
2950struct mwl8k_cmd_set_slot {
2951 struct mwl8k_cmd_pkt header;
2952 __le16 action;
2953 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002954} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002955
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002956static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002957{
2958 struct mwl8k_cmd_set_slot *cmd;
2959 int rc;
2960
2961 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2962 if (cmd == NULL)
2963 return -ENOMEM;
2964
2965 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2966 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2967 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002968 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002969
2970 rc = mwl8k_post_cmd(hw, &cmd->header);
2971 kfree(cmd);
2972
2973 return rc;
2974}
2975
2976/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002977 * CMD_SET_EDCA_PARAMS.
2978 */
2979struct mwl8k_cmd_set_edca_params {
2980 struct mwl8k_cmd_pkt header;
2981
2982 /* See MWL8K_SET_EDCA_XXX below */
2983 __le16 action;
2984
2985 /* TX opportunity in units of 32 us */
2986 __le16 txop;
2987
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002988 union {
2989 struct {
2990 /* Log exponent of max contention period: 0...15 */
2991 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002992
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002993 /* Log exponent of min contention period: 0...15 */
2994 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002995
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002996 /* Adaptive interframe spacing in units of 32us */
2997 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002998
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002999 /* TX queue to configure */
3000 __u8 txq;
3001 } ap;
3002 struct {
3003 /* Log exponent of max contention period: 0...15 */
3004 __u8 log_cw_max;
3005
3006 /* Log exponent of min contention period: 0...15 */
3007 __u8 log_cw_min;
3008
3009 /* Adaptive interframe spacing in units of 32us */
3010 __u8 aifs;
3011
3012 /* TX queue to configure */
3013 __u8 txq;
3014 } sta;
3015 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003016} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003017
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003018#define MWL8K_SET_EDCA_CW 0x01
3019#define MWL8K_SET_EDCA_TXOP 0x02
3020#define MWL8K_SET_EDCA_AIFS 0x04
3021
3022#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
3023 MWL8K_SET_EDCA_TXOP | \
3024 MWL8K_SET_EDCA_AIFS)
3025
3026static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003027mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3028 __u16 cw_min, __u16 cw_max,
3029 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003030{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003031 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003032 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003033 int rc;
3034
3035 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3036 if (cmd == NULL)
3037 return -ENOMEM;
3038
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003039 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3040 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003041 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3042 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02003043 if (priv->ap_fw) {
3044 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3045 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3046 cmd->ap.aifs = aifs;
3047 cmd->ap.txq = qnum;
3048 } else {
3049 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3050 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3051 cmd->sta.aifs = aifs;
3052 cmd->sta.txq = qnum;
3053 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003054
3055 rc = mwl8k_post_cmd(hw, &cmd->header);
3056 kfree(cmd);
3057
3058 return rc;
3059}
3060
3061/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003062 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003063 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003064struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003065 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003066 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003067} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003068
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003069static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003070{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003071 struct mwl8k_priv *priv = hw->priv;
3072 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003073 int rc;
3074
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003075 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3076 if (cmd == NULL)
3077 return -ENOMEM;
3078
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003079 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003080 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003081 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003082
3083 rc = mwl8k_post_cmd(hw, &cmd->header);
3084 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01003085
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003086 if (!rc)
3087 priv->wmm_enabled = enable;
3088
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003089 return rc;
3090}
3091
3092/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003093 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003094 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003095struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003096 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003097 __le32 action;
3098 __u8 rx_antenna_map;
3099 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003100} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003101
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003102static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003103{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003104 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003105 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003106
3107 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3108 if (cmd == NULL)
3109 return -ENOMEM;
3110
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003111 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003112 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003113 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3114 cmd->rx_antenna_map = rx;
3115 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003116
3117 rc = mwl8k_post_cmd(hw, &cmd->header);
3118 kfree(cmd);
3119
3120 return rc;
3121}
3122
3123/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003124 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003125 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003126struct mwl8k_cmd_use_fixed_rate_sta {
3127 struct mwl8k_cmd_pkt header;
3128 __le32 action;
3129 __le32 allow_rate_drop;
3130 __le32 num_rates;
3131 struct {
3132 __le32 is_ht_rate;
3133 __le32 enable_retry;
3134 __le32 rate;
3135 __le32 retry_count;
3136 } rate_entry[8];
3137 __le32 rate_type;
3138 __le32 reserved1;
3139 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003140} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003141
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003142#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003143#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003144
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003145static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003146{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003147 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003148 int rc;
3149
3150 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3151 if (cmd == NULL)
3152 return -ENOMEM;
3153
3154 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3155 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003156 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3157 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003158
3159 rc = mwl8k_post_cmd(hw, &cmd->header);
3160 kfree(cmd);
3161
3162 return rc;
3163}
3164
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003165/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003166 * CMD_USE_FIXED_RATE (AP version).
3167 */
3168struct mwl8k_cmd_use_fixed_rate_ap {
3169 struct mwl8k_cmd_pkt header;
3170 __le32 action;
3171 __le32 allow_rate_drop;
3172 __le32 num_rates;
3173 struct mwl8k_rate_entry_ap {
3174 __le32 is_ht_rate;
3175 __le32 enable_retry;
3176 __le32 rate;
3177 __le32 retry_count;
3178 } rate_entry[4];
3179 u8 multicast_rate;
3180 u8 multicast_rate_type;
3181 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003182} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01003183
3184static int
3185mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3186{
3187 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3188 int rc;
3189
3190 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3191 if (cmd == NULL)
3192 return -ENOMEM;
3193
3194 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3195 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3196 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3197 cmd->multicast_rate = mcast;
3198 cmd->management_rate = mgmt;
3199
3200 rc = mwl8k_post_cmd(hw, &cmd->header);
3201 kfree(cmd);
3202
3203 return rc;
3204}
3205
3206/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003207 * CMD_ENABLE_SNIFFER.
3208 */
3209struct mwl8k_cmd_enable_sniffer {
3210 struct mwl8k_cmd_pkt header;
3211 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003212} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003213
3214static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3215{
3216 struct mwl8k_cmd_enable_sniffer *cmd;
3217 int rc;
3218
3219 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3220 if (cmd == NULL)
3221 return -ENOMEM;
3222
3223 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3224 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3225 cmd->action = cpu_to_le32(!!enable);
3226
3227 rc = mwl8k_post_cmd(hw, &cmd->header);
3228 kfree(cmd);
3229
3230 return rc;
3231}
3232
3233/*
3234 * CMD_SET_MAC_ADDR.
3235 */
3236struct mwl8k_cmd_set_mac_addr {
3237 struct mwl8k_cmd_pkt header;
3238 union {
3239 struct {
3240 __le16 mac_type;
3241 __u8 mac_addr[ETH_ALEN];
3242 } mbss;
3243 __u8 mac_addr[ETH_ALEN];
3244 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00003245} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003246
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003247#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
3248#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
3249#define MWL8K_MAC_TYPE_PRIMARY_AP 2
3250#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01003251
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003252static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3253 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003254{
3255 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003256 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003257 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003258 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003259 int rc;
3260
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003261 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3262 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3263 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3264 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3265 else
3266 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3267 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3268 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3269 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3270 else
3271 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3272 }
3273
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003274 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3275 if (cmd == NULL)
3276 return -ENOMEM;
3277
3278 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3279 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3280 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003281 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003282 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3283 } else {
3284 memcpy(cmd->mac_addr, mac, ETH_ALEN);
3285 }
3286
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003287 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003288 kfree(cmd);
3289
3290 return rc;
3291}
3292
3293/*
3294 * CMD_SET_RATEADAPT_MODE.
3295 */
3296struct mwl8k_cmd_set_rate_adapt_mode {
3297 struct mwl8k_cmd_pkt header;
3298 __le16 action;
3299 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003300} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003301
3302static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3303{
3304 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3305 int rc;
3306
3307 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3308 if (cmd == NULL)
3309 return -ENOMEM;
3310
3311 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3312 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3313 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3314 cmd->mode = cpu_to_le16(mode);
3315
3316 rc = mwl8k_post_cmd(hw, &cmd->header);
3317 kfree(cmd);
3318
3319 return rc;
3320}
3321
3322/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003323 * CMD_BSS_START.
3324 */
3325struct mwl8k_cmd_bss_start {
3326 struct mwl8k_cmd_pkt header;
3327 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003328} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003329
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003330static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3331 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003332{
3333 struct mwl8k_cmd_bss_start *cmd;
3334 int rc;
3335
3336 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3337 if (cmd == NULL)
3338 return -ENOMEM;
3339
3340 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3341 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3342 cmd->enable = cpu_to_le32(enable);
3343
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003344 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003345 kfree(cmd);
3346
3347 return rc;
3348}
3349
3350/*
Nishant Sarmukadam5faa1af2011-03-17 11:58:43 -07003351 * CMD_BASTREAM.
3352 */
3353
3354/*
3355 * UPSTREAM is tx direction
3356 */
3357#define BASTREAM_FLAG_DIRECTION_UPSTREAM 0x00
3358#define BASTREAM_FLAG_IMMEDIATE_TYPE 0x01
3359
3360enum {
3361 MWL8K_BA_CREATE,
3362 MWL8K_BA_UPDATE,
3363 MWL8K_BA_DESTROY,
3364 MWL8K_BA_FLUSH,
3365 MWL8K_BA_CHECK,
3366} ba_stream_action_type;
3367
3368
3369struct mwl8k_create_ba_stream {
3370 __le32 flags;
3371 __le32 idle_thrs;
3372 __le32 bar_thrs;
3373 __le32 window_size;
3374 u8 peer_mac_addr[6];
3375 u8 dialog_token;
3376 u8 tid;
3377 u8 queue_id;
3378 u8 param_info;
3379 __le32 ba_context;
3380 u8 reset_seq_no_flag;
3381 __le16 curr_seq_no;
3382 u8 sta_src_mac_addr[6];
3383} __packed;
3384
3385struct mwl8k_destroy_ba_stream {
3386 __le32 flags;
3387 __le32 ba_context;
3388} __packed;
3389
3390struct mwl8k_cmd_bastream {
3391 struct mwl8k_cmd_pkt header;
3392 __le32 action;
3393 union {
3394 struct mwl8k_create_ba_stream create_params;
3395 struct mwl8k_destroy_ba_stream destroy_params;
3396 };
3397} __packed;
3398
3399static int
3400mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
3401{
3402 struct mwl8k_cmd_bastream *cmd;
3403 int rc;
3404
3405 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3406 if (cmd == NULL)
3407 return -ENOMEM;
3408
3409 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3410 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3411
3412 cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3413
3414 cmd->create_params.queue_id = stream->idx;
3415 memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3416 ETH_ALEN);
3417 cmd->create_params.tid = stream->tid;
3418
3419 cmd->create_params.flags =
3420 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3421 cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3422
3423 rc = mwl8k_post_cmd(hw, &cmd->header);
3424
3425 kfree(cmd);
3426
3427 return rc;
3428}
3429
3430static int
3431mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3432 u8 buf_size)
3433{
3434 struct mwl8k_cmd_bastream *cmd;
3435 int rc;
3436
3437 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3438 if (cmd == NULL)
3439 return -ENOMEM;
3440
3441
3442 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3443 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3444
3445 cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
3446
3447 cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
3448 cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
3449 cmd->create_params.queue_id = stream->idx;
3450
3451 memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
3452 cmd->create_params.tid = stream->tid;
3453 cmd->create_params.curr_seq_no = cpu_to_le16(0);
3454 cmd->create_params.reset_seq_no_flag = 1;
3455
3456 cmd->create_params.param_info =
3457 (stream->sta->ht_cap.ampdu_factor &
3458 IEEE80211_HT_AMPDU_PARM_FACTOR) |
3459 ((stream->sta->ht_cap.ampdu_density << 2) &
3460 IEEE80211_HT_AMPDU_PARM_DENSITY);
3461
3462 cmd->create_params.flags =
3463 cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
3464 BASTREAM_FLAG_DIRECTION_UPSTREAM);
3465
3466 rc = mwl8k_post_cmd(hw, &cmd->header);
3467
3468 wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
3469 stream->sta->addr, stream->tid);
3470 kfree(cmd);
3471
3472 return rc;
3473}
3474
3475static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3476 struct mwl8k_ampdu_stream *stream)
3477{
3478 struct mwl8k_cmd_bastream *cmd;
3479
3480 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3481 if (cmd == NULL)
3482 return;
3483
3484 cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3485 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3486 cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
3487
3488 cmd->destroy_params.ba_context = cpu_to_le32(stream->idx);
3489 mwl8k_post_cmd(hw, &cmd->header);
3490
3491 wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", stream->idx);
3492
3493 kfree(cmd);
3494}
3495
3496/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003497 * CMD_SET_NEW_STN.
3498 */
3499struct mwl8k_cmd_set_new_stn {
3500 struct mwl8k_cmd_pkt header;
3501 __le16 aid;
3502 __u8 mac_addr[6];
3503 __le16 stn_id;
3504 __le16 action;
3505 __le16 rsvd;
3506 __le32 legacy_rates;
3507 __u8 ht_rates[4];
3508 __le16 cap_info;
3509 __le16 ht_capabilities_info;
3510 __u8 mac_ht_param_info;
3511 __u8 rev;
3512 __u8 control_channel;
3513 __u8 add_channel;
3514 __le16 op_mode;
3515 __le16 stbc;
3516 __u8 add_qos_info;
3517 __u8 is_qos_sta;
3518 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003519} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003520
3521#define MWL8K_STA_ACTION_ADD 0
3522#define MWL8K_STA_ACTION_REMOVE 2
3523
3524static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3525 struct ieee80211_vif *vif,
3526 struct ieee80211_sta *sta)
3527{
3528 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003529 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003530 int rc;
3531
3532 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3533 if (cmd == NULL)
3534 return -ENOMEM;
3535
3536 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3537 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3538 cmd->aid = cpu_to_le16(sta->aid);
3539 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3540 cmd->stn_id = cpu_to_le16(sta->aid);
3541 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003542 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3543 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3544 else
3545 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3546 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003547 if (sta->ht_cap.ht_supported) {
3548 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3549 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3550 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3551 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3552 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3553 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3554 ((sta->ht_cap.ampdu_density & 7) << 2);
3555 cmd->is_qos_sta = 1;
3556 }
3557
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003558 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003559 kfree(cmd);
3560
3561 return rc;
3562}
3563
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003564static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3565 struct ieee80211_vif *vif)
3566{
3567 struct mwl8k_cmd_set_new_stn *cmd;
3568 int rc;
3569
3570 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3571 if (cmd == NULL)
3572 return -ENOMEM;
3573
3574 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3575 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3576 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3577
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003578 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003579 kfree(cmd);
3580
3581 return rc;
3582}
3583
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003584static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3585 struct ieee80211_vif *vif, u8 *addr)
3586{
3587 struct mwl8k_cmd_set_new_stn *cmd;
3588 int rc;
3589
3590 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3591 if (cmd == NULL)
3592 return -ENOMEM;
3593
3594 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3595 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3596 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3597 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3598
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003599 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003600 kfree(cmd);
3601
3602 return rc;
3603}
3604
3605/*
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08003606 * CMD_UPDATE_ENCRYPTION.
3607 */
3608
3609#define MAX_ENCR_KEY_LENGTH 16
3610#define MIC_KEY_LENGTH 8
3611
3612struct mwl8k_cmd_update_encryption {
3613 struct mwl8k_cmd_pkt header;
3614
3615 __le32 action;
3616 __le32 reserved;
3617 __u8 mac_addr[6];
3618 __u8 encr_type;
3619
3620} __attribute__((packed));
3621
3622struct mwl8k_cmd_set_key {
3623 struct mwl8k_cmd_pkt header;
3624
3625 __le32 action;
3626 __le32 reserved;
3627 __le16 length;
3628 __le16 key_type_id;
3629 __le32 key_info;
3630 __le32 key_id;
3631 __le16 key_len;
3632 __u8 key_material[MAX_ENCR_KEY_LENGTH];
3633 __u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
3634 __u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
3635 __le16 tkip_rsc_low;
3636 __le32 tkip_rsc_high;
3637 __le16 tkip_tsc_low;
3638 __le32 tkip_tsc_high;
3639 __u8 mac_addr[6];
3640} __attribute__((packed));
3641
3642enum {
3643 MWL8K_ENCR_ENABLE,
3644 MWL8K_ENCR_SET_KEY,
3645 MWL8K_ENCR_REMOVE_KEY,
3646 MWL8K_ENCR_SET_GROUP_KEY,
3647};
3648
3649#define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP 0
3650#define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE 1
3651#define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP 4
3652#define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED 7
3653#define MWL8K_UPDATE_ENCRYPTION_TYPE_AES 8
3654
3655enum {
3656 MWL8K_ALG_WEP,
3657 MWL8K_ALG_TKIP,
3658 MWL8K_ALG_CCMP,
3659};
3660
3661#define MWL8K_KEY_FLAG_TXGROUPKEY 0x00000004
3662#define MWL8K_KEY_FLAG_PAIRWISE 0x00000008
3663#define MWL8K_KEY_FLAG_TSC_VALID 0x00000040
3664#define MWL8K_KEY_FLAG_WEP_TXKEY 0x01000000
3665#define MWL8K_KEY_FLAG_MICKEY_VALID 0x02000000
3666
3667static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
3668 struct ieee80211_vif *vif,
3669 u8 *addr,
3670 u8 encr_type)
3671{
3672 struct mwl8k_cmd_update_encryption *cmd;
3673 int rc;
3674
3675 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3676 if (cmd == NULL)
3677 return -ENOMEM;
3678
3679 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3680 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3681 cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
3682 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3683 cmd->encr_type = encr_type;
3684
3685 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3686 kfree(cmd);
3687
3688 return rc;
3689}
3690
3691static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
3692 u8 *addr,
3693 struct ieee80211_key_conf *key)
3694{
3695 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
3696 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3697 cmd->length = cpu_to_le16(sizeof(*cmd) -
3698 offsetof(struct mwl8k_cmd_set_key, length));
3699 cmd->key_id = cpu_to_le32(key->keyidx);
3700 cmd->key_len = cpu_to_le16(key->keylen);
3701 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3702
3703 switch (key->cipher) {
3704 case WLAN_CIPHER_SUITE_WEP40:
3705 case WLAN_CIPHER_SUITE_WEP104:
3706 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
3707 if (key->keyidx == 0)
3708 cmd->key_info = cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
3709
3710 break;
3711 case WLAN_CIPHER_SUITE_TKIP:
3712 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
3713 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3714 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3715 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3716 cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
3717 | MWL8K_KEY_FLAG_TSC_VALID);
3718 break;
3719 case WLAN_CIPHER_SUITE_CCMP:
3720 cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
3721 cmd->key_info = (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3722 ? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
3723 : cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
3724 break;
3725 default:
3726 return -ENOTSUPP;
3727 }
3728
3729 return 0;
3730}
3731
3732static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
3733 struct ieee80211_vif *vif,
3734 u8 *addr,
3735 struct ieee80211_key_conf *key)
3736{
3737 struct mwl8k_cmd_set_key *cmd;
3738 int rc;
3739 int keymlen;
3740 u32 action;
3741 u8 idx;
3742 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3743
3744 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3745 if (cmd == NULL)
3746 return -ENOMEM;
3747
3748 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3749 if (rc < 0)
3750 goto done;
3751
3752 idx = key->keyidx;
3753
3754 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3755 action = MWL8K_ENCR_SET_KEY;
3756 else
3757 action = MWL8K_ENCR_SET_GROUP_KEY;
3758
3759 switch (key->cipher) {
3760 case WLAN_CIPHER_SUITE_WEP40:
3761 case WLAN_CIPHER_SUITE_WEP104:
3762 if (!mwl8k_vif->wep_key_conf[idx].enabled) {
3763 memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
3764 sizeof(*key) + key->keylen);
3765 mwl8k_vif->wep_key_conf[idx].enabled = 1;
3766 }
3767
3768 keymlen = 0;
3769 action = MWL8K_ENCR_SET_KEY;
3770 break;
3771 case WLAN_CIPHER_SUITE_TKIP:
3772 keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
3773 break;
3774 case WLAN_CIPHER_SUITE_CCMP:
3775 keymlen = key->keylen;
3776 break;
3777 default:
3778 rc = -ENOTSUPP;
3779 goto done;
3780 }
3781
3782 memcpy(cmd->key_material, key->key, keymlen);
3783 cmd->action = cpu_to_le32(action);
3784
3785 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3786done:
3787 kfree(cmd);
3788
3789 return rc;
3790}
3791
3792static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
3793 struct ieee80211_vif *vif,
3794 u8 *addr,
3795 struct ieee80211_key_conf *key)
3796{
3797 struct mwl8k_cmd_set_key *cmd;
3798 int rc;
3799 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3800
3801 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3802 if (cmd == NULL)
3803 return -ENOMEM;
3804
3805 rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
3806 if (rc < 0)
3807 goto done;
3808
3809 if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3810 WLAN_CIPHER_SUITE_WEP104)
3811 mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
3812
3813 cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
3814
3815 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3816done:
3817 kfree(cmd);
3818
3819 return rc;
3820}
3821
3822static int mwl8k_set_key(struct ieee80211_hw *hw,
3823 enum set_key_cmd cmd_param,
3824 struct ieee80211_vif *vif,
3825 struct ieee80211_sta *sta,
3826 struct ieee80211_key_conf *key)
3827{
3828 int rc = 0;
3829 u8 encr_type;
3830 u8 *addr;
3831 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3832
3833 if (vif->type == NL80211_IFTYPE_STATION)
3834 return -EOPNOTSUPP;
3835
3836 if (sta == NULL)
3837 addr = hw->wiphy->perm_addr;
3838 else
3839 addr = sta->addr;
3840
3841 if (cmd_param == SET_KEY) {
3842 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
3843 rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
3844 if (rc)
3845 goto out;
3846
3847 if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
3848 || (key->cipher == WLAN_CIPHER_SUITE_WEP104))
3849 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
3850 else
3851 encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
3852
3853 rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
3854 encr_type);
3855 if (rc)
3856 goto out;
3857
3858 mwl8k_vif->is_hw_crypto_enabled = true;
3859
3860 } else {
3861 rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
3862
3863 if (rc)
3864 goto out;
3865
3866 mwl8k_vif->is_hw_crypto_enabled = false;
3867
3868 }
3869out:
3870 return rc;
3871}
3872
3873/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003874 * CMD_UPDATE_STADB.
3875 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003876struct ewc_ht_info {
3877 __le16 control1;
3878 __le16 control2;
3879 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003880} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003881
3882struct peer_capability_info {
3883 /* Peer type - AP vs. STA. */
3884 __u8 peer_type;
3885
3886 /* Basic 802.11 capabilities from assoc resp. */
3887 __le16 basic_caps;
3888
3889 /* Set if peer supports 802.11n high throughput (HT). */
3890 __u8 ht_support;
3891
3892 /* Valid if HT is supported. */
3893 __le16 ht_caps;
3894 __u8 extended_ht_caps;
3895 struct ewc_ht_info ewc_info;
3896
3897 /* Legacy rate table. Intersection of our rates and peer rates. */
3898 __u8 legacy_rates[12];
3899
3900 /* HT rate table. Intersection of our rates and peer rates. */
3901 __u8 ht_rates[16];
3902 __u8 pad[16];
3903
3904 /* If set, interoperability mode, no proprietary extensions. */
3905 __u8 interop;
3906 __u8 pad2;
3907 __u8 station_id;
3908 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003909} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003910
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003911struct mwl8k_cmd_update_stadb {
3912 struct mwl8k_cmd_pkt header;
3913
3914 /* See STADB_ACTION_TYPE */
3915 __le32 action;
3916
3917 /* Peer MAC address */
3918 __u8 peer_addr[ETH_ALEN];
3919
3920 __le32 reserved;
3921
3922 /* Peer info - valid during add/update. */
3923 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003924} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003925
Lennert Buytenheka6804002010-01-04 21:55:42 +01003926#define MWL8K_STA_DB_MODIFY_ENTRY 1
3927#define MWL8K_STA_DB_DEL_ENTRY 2
3928
3929/* Peer Entry flags - used to define the type of the peer node */
3930#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3931
3932static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003933 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003934 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003935{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003936 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003937 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003938 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003939 int rc;
3940
3941 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3942 if (cmd == NULL)
3943 return -ENOMEM;
3944
3945 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3946 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003947 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003948 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003949
Lennert Buytenheka6804002010-01-04 21:55:42 +01003950 p = &cmd->peer_info;
3951 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3952 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003953 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003954 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003955 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3956 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003957 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3958 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3959 else
3960 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3961 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003962 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003963 p->interop = 1;
3964 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003965
Lennert Buytenheka6804002010-01-04 21:55:42 +01003966 rc = mwl8k_post_cmd(hw, &cmd->header);
3967 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003968
Lennert Buytenheka6804002010-01-04 21:55:42 +01003969 return rc ? rc : p->station_id;
3970}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003971
Lennert Buytenheka6804002010-01-04 21:55:42 +01003972static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3973 struct ieee80211_vif *vif, u8 *addr)
3974{
3975 struct mwl8k_cmd_update_stadb *cmd;
3976 int rc;
3977
3978 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3979 if (cmd == NULL)
3980 return -ENOMEM;
3981
3982 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3983 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3984 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3985 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3986
3987 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003988 kfree(cmd);
3989
3990 return rc;
3991}
3992
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003993
3994/*
3995 * Interrupt handling.
3996 */
3997static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3998{
3999 struct ieee80211_hw *hw = dev_id;
4000 struct mwl8k_priv *priv = hw->priv;
4001 u32 status;
4002
4003 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004004 if (!status)
4005 return IRQ_NONE;
4006
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004007 if (status & MWL8K_A2H_INT_TX_DONE) {
4008 status &= ~MWL8K_A2H_INT_TX_DONE;
4009 tasklet_schedule(&priv->poll_tx_task);
4010 }
4011
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004012 if (status & MWL8K_A2H_INT_RX_READY) {
4013 status &= ~MWL8K_A2H_INT_RX_READY;
4014 tasklet_schedule(&priv->poll_rx_task);
4015 }
4016
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004017 if (status)
4018 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004019
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004020 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004021 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004022 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004023 }
4024
4025 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004026 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004027 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004028 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004029 }
4030
4031 return IRQ_HANDLED;
4032}
4033
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004034static void mwl8k_tx_poll(unsigned long data)
4035{
4036 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4037 struct mwl8k_priv *priv = hw->priv;
4038 int limit;
4039 int i;
4040
4041 limit = 32;
4042
4043 spin_lock_bh(&priv->tx_lock);
4044
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004045 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004046 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4047
4048 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4049 complete(priv->tx_wait);
4050 priv->tx_wait = NULL;
4051 }
4052
4053 spin_unlock_bh(&priv->tx_lock);
4054
4055 if (limit) {
4056 writel(~MWL8K_A2H_INT_TX_DONE,
4057 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4058 } else {
4059 tasklet_schedule(&priv->poll_tx_task);
4060 }
4061}
4062
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004063static void mwl8k_rx_poll(unsigned long data)
4064{
4065 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
4066 struct mwl8k_priv *priv = hw->priv;
4067 int limit;
4068
4069 limit = 32;
4070 limit -= rxq_process(hw, 0, limit);
4071 limit -= rxq_refill(hw, 0, limit);
4072
4073 if (limit) {
4074 writel(~MWL8K_A2H_INT_RX_READY,
4075 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4076 } else {
4077 tasklet_schedule(&priv->poll_rx_task);
4078 }
4079}
4080
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004081
4082/*
4083 * Core driver operations.
4084 */
Johannes Berg7bb45682011-02-24 14:42:06 +01004085static void mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004086{
4087 struct mwl8k_priv *priv = hw->priv;
4088 int index = skb_get_queue_mapping(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004089
Lennert Buytenhek9189c102010-01-12 13:47:32 +01004090 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004091 wiphy_debug(hw->wiphy,
4092 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004093 dev_kfree_skb(skb);
Johannes Berg7bb45682011-02-24 14:42:06 +01004094 return;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004095 }
4096
Johannes Berg7bb45682011-02-24 14:42:06 +01004097 mwl8k_txq_xmit(hw, index, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004098}
4099
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004100static int mwl8k_start(struct ieee80211_hw *hw)
4101{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004102 struct mwl8k_priv *priv = hw->priv;
4103 int rc;
4104
Joe Perchesa0607fd2009-11-18 23:29:17 -08004105 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004106 IRQF_SHARED, MWL8K_NAME, hw);
4107 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004108 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004109 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004110 }
4111
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004112 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004113 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004114 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004115
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004116 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004117 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004118
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004119 rc = mwl8k_fw_lock(hw);
4120 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004121 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004122
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004123 if (!priv->ap_fw) {
4124 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004125 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004126
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02004127 if (!rc)
4128 rc = mwl8k_cmd_set_pre_scan(hw);
4129
4130 if (!rc)
4131 rc = mwl8k_cmd_set_post_scan(hw,
4132 "\x00\x00\x00\x00\x00\x00");
4133 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004134
4135 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004136 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004137
4138 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004139 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004140
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004141 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004142 }
4143
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004144 if (rc) {
4145 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4146 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004147 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004148 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02004149 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004150
4151 return rc;
4152}
4153
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004154static void mwl8k_stop(struct ieee80211_hw *hw)
4155{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004156 struct mwl8k_priv *priv = hw->priv;
4157 int i;
4158
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004159 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004160
4161 ieee80211_stop_queues(hw);
4162
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004163 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004164 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004165 free_irq(priv->pdev->irq, hw);
4166
4167 /* Stop finalize join worker */
4168 cancel_work_sync(&priv->finalize_join_worker);
4169 if (priv->beacon_skb != NULL)
4170 dev_kfree_skb(priv->beacon_skb);
4171
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004172 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004173 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004174 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004175
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004176 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004177 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004178 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004179}
4180
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004181static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4182
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004183static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004184 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004185{
4186 struct mwl8k_priv *priv = hw->priv;
4187 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004188 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004189 int macid, rc;
4190 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004191
4192 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004193 * Reject interface creation if sniffer mode is active, as
4194 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004195 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004196 */
4197 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004198 wiphy_info(hw->wiphy,
4199 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004200 return -EINVAL;
4201 }
4202
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004203 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004204 switch (vif->type) {
4205 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004206 if (!priv->ap_fw && di->fw_image_ap) {
4207 /* we must load the ap fw to meet this request */
4208 if (!list_empty(&priv->vif_list))
4209 return -EBUSY;
4210 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4211 if (rc)
4212 return rc;
4213 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004214 macids_supported = priv->ap_macids_supported;
4215 break;
4216 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004217 if (priv->ap_fw && di->fw_image_sta) {
4218 /* we must load the sta fw to meet this request */
4219 if (!list_empty(&priv->vif_list))
4220 return -EBUSY;
4221 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4222 if (rc)
4223 return rc;
4224 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004225 macids_supported = priv->sta_macids_supported;
4226 break;
4227 default:
4228 return -EINVAL;
4229 }
4230
4231 macid = ffs(macids_supported & ~priv->macids_used);
4232 if (!macid--)
4233 return -EBUSY;
4234
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004235 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01004236 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004237 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004238 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004239 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004240 mwl8k_vif->seqno = 0;
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004241 memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4242 mwl8k_vif->is_hw_crypto_enabled = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004243
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004244 /* Set the mac address. */
4245 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4246
4247 if (priv->ap_fw)
4248 mwl8k_cmd_set_new_stn_add_self(hw, vif);
4249
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004250 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004251 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004252
4253 return 0;
4254}
4255
4256static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01004257 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004258{
4259 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004260 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004261
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004262 if (priv->ap_fw)
4263 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4264
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004265 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004266
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004267 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004268 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004269}
4270
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004271static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004272{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004273 struct ieee80211_conf *conf = &hw->conf;
4274 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004275 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004276
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004277 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004278 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004279 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02004280 }
4281
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004282 rc = mwl8k_fw_lock(hw);
4283 if (rc)
4284 return rc;
4285
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004286 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004287 if (rc)
4288 goto out;
4289
Lennert Buytenhek610677d2010-01-04 21:56:46 +01004290 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004291 if (rc)
4292 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004293
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004294 if (conf->power_level > 18)
4295 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004296
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004297 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004298 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4299 if (rc)
4300 goto out;
4301
Nishant Sarmukadamda62b762011-02-17 14:45:16 -08004302 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
4303 if (rc)
4304 wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
4305 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
4306 if (rc)
4307 wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
4308
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004309 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08004310 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4311 if (rc)
4312 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02004313 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4314 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004315
Lennert Buytenhekee03a932009-07-17 07:19:37 +02004316out:
4317 mwl8k_fw_unlock(hw);
4318
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004319 return rc;
4320}
4321
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004322static void
4323mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4324 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004325{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004326 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004327 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004328 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004329 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004330
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004331 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004332 return;
4333
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004334 /*
4335 * No need to capture a beacon if we're no longer associated.
4336 */
4337 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
4338 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004339
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004340 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004341 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004342 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004343 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004344 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004345
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004346 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004347
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004348 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004349 if (ap == NULL) {
4350 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004351 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004352 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01004353
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004354 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
4355 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
4356 } else {
4357 ap_legacy_rates =
4358 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
4359 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004360 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004361
4362 rcu_read_unlock();
4363 }
4364
4365 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01004366 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004367 if (rc)
4368 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004369
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01004370 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004371 if (rc)
4372 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004373 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004374
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004375 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004376 rc = mwl8k_set_radio_preamble(hw,
4377 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004378 if (rc)
4379 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004380 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004381
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004382 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01004383 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004384 if (rc)
4385 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004386 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004387
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01004388 if (vif->bss_conf.assoc &&
4389 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
4390 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004391 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004392 if (rc)
4393 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004394 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004395
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01004396 if (vif->bss_conf.assoc &&
4397 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004398 /*
4399 * Finalize the join. Tell rx handler to process
4400 * next beacon from our BSSID.
4401 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004402 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004403 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004404 }
4405
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02004406out:
4407 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004408}
4409
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004410static void
4411mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4412 struct ieee80211_bss_conf *info, u32 changed)
4413{
4414 int rc;
4415
4416 if (mwl8k_fw_lock(hw))
4417 return;
4418
4419 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
4420 rc = mwl8k_set_radio_preamble(hw,
4421 vif->bss_conf.use_short_preamble);
4422 if (rc)
4423 goto out;
4424 }
4425
4426 if (changed & BSS_CHANGED_BASIC_RATES) {
4427 int idx;
4428 int rate;
4429
4430 /*
4431 * Use lowest supported basic rate for multicasts
4432 * and management frames (such as probe responses --
4433 * beacons will always go out at 1 Mb/s).
4434 */
4435 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01004436 if (idx)
4437 idx--;
4438
4439 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
4440 rate = mwl8k_rates_24[idx].hw_value;
4441 else
4442 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004443
4444 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
4445 }
4446
4447 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
4448 struct sk_buff *skb;
4449
4450 skb = ieee80211_beacon_get(hw, vif);
4451 if (skb != NULL) {
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004452 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004453 kfree_skb(skb);
4454 }
4455 }
4456
4457 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004458 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004459
4460out:
4461 mwl8k_fw_unlock(hw);
4462}
4463
4464static void
4465mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4466 struct ieee80211_bss_conf *info, u32 changed)
4467{
4468 struct mwl8k_priv *priv = hw->priv;
4469
4470 if (!priv->ap_fw)
4471 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
4472 else
4473 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
4474}
4475
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004476static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00004477 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004478{
4479 struct mwl8k_cmd_pkt *cmd;
4480
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004481 /*
4482 * Synthesize and return a command packet that programs the
4483 * hardware multicast address filter. At this point we don't
4484 * know whether FIF_ALLMULTI is being requested, but if it is,
4485 * we'll end up throwing this packet away and creating a new
4486 * one in mwl8k_configure_filter().
4487 */
Jiri Pirko22bedad2010-04-01 21:22:57 +00004488 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02004489
4490 return (unsigned long)cmd;
4491}
4492
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004493static int
4494mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
4495 unsigned int changed_flags,
4496 unsigned int *total_flags)
4497{
4498 struct mwl8k_priv *priv = hw->priv;
4499
4500 /*
4501 * Hardware sniffer mode is mutually exclusive with STA
4502 * operation, so refuse to enable sniffer mode if a STA
4503 * interface is active.
4504 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004505 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004506 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07004507 wiphy_info(hw->wiphy,
4508 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004509 return 0;
4510 }
4511
4512 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004513 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004514 return 0;
4515 priv->sniffer_enabled = true;
4516 }
4517
4518 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
4519 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
4520 FIF_OTHER_BSS;
4521
4522 return 1;
4523}
4524
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004525static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
4526{
4527 if (!list_empty(&priv->vif_list))
4528 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
4529
4530 return NULL;
4531}
4532
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004533static void mwl8k_configure_filter(struct ieee80211_hw *hw,
4534 unsigned int changed_flags,
4535 unsigned int *total_flags,
4536 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004537{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004538 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004539 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
4540
4541 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02004542 * AP firmware doesn't allow fine-grained control over
4543 * the receive filter.
4544 */
4545 if (priv->ap_fw) {
4546 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
4547 kfree(cmd);
4548 return;
4549 }
4550
4551 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004552 * Enable hardware sniffer mode if FIF_CONTROL or
4553 * FIF_OTHER_BSS is requested.
4554 */
4555 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
4556 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
4557 kfree(cmd);
4558 return;
4559 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004560
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004561 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004562 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004563
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004564 if (mwl8k_fw_lock(hw)) {
4565 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004566 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01004567 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004568
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004569 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004570 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02004571 priv->sniffer_enabled = false;
4572 }
4573
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004574 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004575 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
4576 /*
4577 * Disable the BSS filter.
4578 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004579 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004580 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004581 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01004582 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004583
Lennert Buytenhek77165d82009-10-22 20:19:53 +02004584 /*
4585 * Enable the BSS filter.
4586 *
4587 * If there is an active STA interface, use that
4588 * interface's BSSID, otherwise use a dummy one
4589 * (where the OUI part needs to be nonzero for
4590 * the BSSID to be accepted by POST_SCAN).
4591 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004592 mwl8k_vif = mwl8k_first_vif(priv);
4593 if (mwl8k_vif != NULL)
4594 bssid = mwl8k_vif->vif->bss_conf.bssid;
4595 else
4596 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02004597
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004598 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004599 }
4600 }
4601
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004602 /*
4603 * If FIF_ALLMULTI is being requested, throw away the command
4604 * packet that ->prepare_multicast() built and replace it with
4605 * a command packet that enables reception of all multicast
4606 * packets.
4607 */
4608 if (*total_flags & FIF_ALLMULTI) {
4609 kfree(cmd);
Jiri Pirko22bedad2010-04-01 21:22:57 +00004610 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02004611 }
4612
4613 if (cmd != NULL) {
4614 mwl8k_post_cmd(hw, cmd);
4615 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004616 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004617
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02004618 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004619}
4620
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004621static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4622{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004623 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004624}
4625
Johannes Berg4a6967b2010-02-19 19:18:37 +01004626static int mwl8k_sta_remove(struct ieee80211_hw *hw,
4627 struct ieee80211_vif *vif,
4628 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004629{
4630 struct mwl8k_priv *priv = hw->priv;
4631
Johannes Berg4a6967b2010-02-19 19:18:37 +01004632 if (priv->ap_fw)
4633 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
4634 else
4635 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
4636}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004637
Johannes Berg4a6967b2010-02-19 19:18:37 +01004638static int mwl8k_sta_add(struct ieee80211_hw *hw,
4639 struct ieee80211_vif *vif,
4640 struct ieee80211_sta *sta)
4641{
4642 struct mwl8k_priv *priv = hw->priv;
4643 int ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004644 int i;
4645 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4646 struct ieee80211_key_conf *key;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004647
Johannes Berg4a6967b2010-02-19 19:18:37 +01004648 if (!priv->ap_fw) {
4649 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
4650 if (ret >= 0) {
4651 MWL8K_STA(sta)->peer_id = ret;
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004652 ret = 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004653 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01004654
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004655 } else {
4656 ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004657 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01004658
Nishant Sarmukadamd9a07d42010-12-30 11:23:33 -08004659 for (i = 0; i < NUM_WEP_KEYS; i++) {
4660 key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
4661 if (mwl8k_vif->wep_key_conf[i].enabled)
4662 mwl8k_set_key(hw, SET_KEY, vif, sta, key);
4663 }
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004664 return ret;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004665}
4666
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004667static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
4668 const struct ieee80211_tx_queue_params *params)
4669{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004670 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004671 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004672
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004673 rc = mwl8k_fw_lock(hw);
4674 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004675 BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004676 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
4677
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004678 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004679 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004680
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004681 if (!rc) {
Brian Cavagnoloe6007072011-03-17 11:58:44 -07004682 int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004683 rc = mwl8k_cmd_set_edca_params(hw, q,
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004684 params->cw_min,
4685 params->cw_max,
4686 params->aifs,
4687 params->txop);
Nishant Sarmukadam85c92052011-02-17 14:45:18 -08004688 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004689
4690 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004691 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02004692
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004693 return rc;
4694}
4695
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004696static int mwl8k_get_stats(struct ieee80211_hw *hw,
4697 struct ieee80211_low_level_stats *stats)
4698{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004699 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004700}
4701
John W. Linville0d462bb2010-07-28 14:04:24 -04004702static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
4703 struct survey_info *survey)
4704{
4705 struct mwl8k_priv *priv = hw->priv;
4706 struct ieee80211_conf *conf = &hw->conf;
4707
4708 if (idx != 0)
4709 return -ENOENT;
4710
4711 survey->channel = conf->channel;
4712 survey->filled = SURVEY_INFO_NOISE_DBM;
4713 survey->noise = priv->noise;
4714
4715 return 0;
4716}
4717
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004718#define MAX_AMPDU_ATTEMPTS 5
4719
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004720static int
4721mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4722 enum ieee80211_ampdu_mlme_action action,
Johannes Berg0b01f032011-01-18 13:51:05 +01004723 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4724 u8 buf_size)
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004725{
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004726
4727 int i, rc = 0;
4728 struct mwl8k_priv *priv = hw->priv;
4729 struct mwl8k_ampdu_stream *stream;
4730 u8 *addr = sta->addr;
4731
4732 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
4733 return -ENOTSUPP;
4734
4735 spin_lock(&priv->stream_lock);
4736 stream = mwl8k_lookup_stream(hw, addr, tid);
4737
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004738 switch (action) {
4739 case IEEE80211_AMPDU_RX_START:
4740 case IEEE80211_AMPDU_RX_STOP:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004741 break;
4742 case IEEE80211_AMPDU_TX_START:
4743 /* By the time we get here the hw queues may contain outgoing
4744 * packets for this RA/TID that are not part of this BA
4745 * session. The hw will assign sequence numbers to these
4746 * packets as they go out. So if we query the hw for its next
4747 * sequence number and use that for the SSN here, it may end up
4748 * being wrong, which will lead to sequence number mismatch at
4749 * the recipient. To avoid this, we reset the sequence number
4750 * to O for the first MPDU in this BA stream.
4751 */
4752 *ssn = 0;
4753 if (stream == NULL) {
4754 /* This means that somebody outside this driver called
4755 * ieee80211_start_tx_ba_session. This is unexpected
4756 * because we do our own rate control. Just warn and
4757 * move on.
4758 */
4759 wiphy_warn(hw->wiphy, "Unexpected call to %s. "
4760 "Proceeding anyway.\n", __func__);
4761 stream = mwl8k_add_stream(hw, sta, tid);
4762 }
4763 if (stream == NULL) {
4764 wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
4765 rc = -EBUSY;
4766 break;
4767 }
4768 stream->state = AMPDU_STREAM_IN_PROGRESS;
4769
4770 /* Release the lock before we do the time consuming stuff */
4771 spin_unlock(&priv->stream_lock);
4772 for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
4773 rc = mwl8k_check_ba(hw, stream);
4774
4775 if (!rc)
4776 break;
4777 /*
4778 * HW queues take time to be flushed, give them
4779 * sufficient time
4780 */
4781
4782 msleep(1000);
4783 }
4784 spin_lock(&priv->stream_lock);
4785 if (rc) {
4786 wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
4787 " attempts\n", tid, MAX_AMPDU_ATTEMPTS);
4788 mwl8k_remove_stream(hw, stream);
4789 rc = -EBUSY;
4790 break;
4791 }
4792 ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
4793 break;
4794 case IEEE80211_AMPDU_TX_STOP:
4795 if (stream == NULL)
4796 break;
4797 if (stream->state == AMPDU_STREAM_ACTIVE) {
4798 spin_unlock(&priv->stream_lock);
4799 mwl8k_destroy_ba(hw, stream);
4800 spin_lock(&priv->stream_lock);
4801 }
4802 mwl8k_remove_stream(hw, stream);
4803 ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
4804 break;
4805 case IEEE80211_AMPDU_TX_OPERATIONAL:
4806 BUG_ON(stream == NULL);
4807 BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
4808 spin_unlock(&priv->stream_lock);
4809 rc = mwl8k_create_ba(hw, stream, buf_size);
4810 spin_lock(&priv->stream_lock);
4811 if (!rc)
4812 stream->state = AMPDU_STREAM_ACTIVE;
4813 else {
4814 spin_unlock(&priv->stream_lock);
4815 mwl8k_destroy_ba(hw, stream);
4816 spin_lock(&priv->stream_lock);
4817 wiphy_debug(hw->wiphy,
4818 "Failed adding stream for sta %pM tid %d\n",
4819 addr, tid);
4820 mwl8k_remove_stream(hw, stream);
4821 }
4822 break;
4823
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004824 default:
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004825 rc = -ENOTSUPP;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004826 }
Nishant Sarmukadam65f3ddc2011-03-17 11:58:46 -07004827
4828 spin_unlock(&priv->stream_lock);
4829 return rc;
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004830}
4831
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004832static const struct ieee80211_ops mwl8k_ops = {
4833 .tx = mwl8k_tx,
4834 .start = mwl8k_start,
4835 .stop = mwl8k_stop,
4836 .add_interface = mwl8k_add_interface,
4837 .remove_interface = mwl8k_remove_interface,
4838 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004839 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004840 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004841 .configure_filter = mwl8k_configure_filter,
Nishant Sarmukadamfcdc4032010-12-30 11:23:34 -08004842 .set_key = mwl8k_set_key,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004843 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01004844 .sta_add = mwl8k_sta_add,
4845 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004846 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004847 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04004848 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004849 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004850};
4851
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004852static void mwl8k_finalize_join_worker(struct work_struct *work)
4853{
4854 struct mwl8k_priv *priv =
4855 container_of(work, struct mwl8k_priv, finalize_join_worker);
4856 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01004857 struct ieee80211_mgmt *mgmt = (void *)skb->data;
4858 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
4859 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
4860 mgmt->u.beacon.variable, len);
4861 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004862
Johannes Berg56007a02010-01-26 14:19:52 +01004863 if (tim && tim[1] >= 2)
4864 dtim_period = tim[3];
4865
4866 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004867
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004868 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004869 priv->beacon_skb = NULL;
4870}
4871
John W. Linvillebcb628d2009-11-06 16:40:16 -05004872enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004873 MWL8363 = 0,
4874 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004875 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02004876};
4877
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07004878#define MWL8K_8366_AP_FW_API 2
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004879#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
4880#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
4881
John W. Linvillebcb628d2009-11-06 16:40:16 -05004882static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004883 [MWL8363] = {
4884 .part_name = "88w8363",
4885 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004886 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004887 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004888 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004889 .part_name = "88w8687",
4890 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004891 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05004892 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004893 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004894 .part_name = "88w8366",
4895 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004896 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004897 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
4898 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004899 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004900 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004901};
4902
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004903MODULE_FIRMWARE("mwl8k/helper_8363.fw");
4904MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
4905MODULE_FIRMWARE("mwl8k/helper_8687.fw");
4906MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
4907MODULE_FIRMWARE("mwl8k/helper_8366.fw");
4908MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004909MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004910
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004911static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01004912 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004913 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
4914 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004915 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
4916 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
4917 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01004918 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004919 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004920};
4921MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
4922
Brian Cavagnolo99020472010-11-12 17:23:52 -08004923static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
4924{
4925 int rc;
4926 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
4927 "Trying alternative firmware %s\n", pci_name(priv->pdev),
4928 priv->fw_pref, priv->fw_alt);
4929 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
4930 if (rc) {
4931 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4932 pci_name(priv->pdev), priv->fw_alt);
4933 return rc;
4934 }
4935 return 0;
4936}
4937
4938static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
4939static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
4940{
4941 struct mwl8k_priv *priv = context;
4942 struct mwl8k_device_info *di = priv->device_info;
4943 int rc;
4944
4945 switch (priv->fw_state) {
4946 case FW_STATE_INIT:
4947 if (!fw) {
4948 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
4949 pci_name(priv->pdev), di->helper_image);
4950 goto fail;
4951 }
4952 priv->fw_helper = fw;
4953 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
4954 true);
4955 if (rc && priv->fw_alt) {
4956 rc = mwl8k_request_alt_fw(priv);
4957 if (rc)
4958 goto fail;
4959 priv->fw_state = FW_STATE_LOADING_ALT;
4960 } else if (rc)
4961 goto fail;
4962 else
4963 priv->fw_state = FW_STATE_LOADING_PREF;
4964 break;
4965
4966 case FW_STATE_LOADING_PREF:
4967 if (!fw) {
4968 if (priv->fw_alt) {
4969 rc = mwl8k_request_alt_fw(priv);
4970 if (rc)
4971 goto fail;
4972 priv->fw_state = FW_STATE_LOADING_ALT;
4973 } else
4974 goto fail;
4975 } else {
4976 priv->fw_ucode = fw;
4977 rc = mwl8k_firmware_load_success(priv);
4978 if (rc)
4979 goto fail;
4980 else
4981 complete(&priv->firmware_loading_complete);
4982 }
4983 break;
4984
4985 case FW_STATE_LOADING_ALT:
4986 if (!fw) {
4987 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4988 pci_name(priv->pdev), di->helper_image);
4989 goto fail;
4990 }
4991 priv->fw_ucode = fw;
4992 rc = mwl8k_firmware_load_success(priv);
4993 if (rc)
4994 goto fail;
4995 else
4996 complete(&priv->firmware_loading_complete);
4997 break;
4998
4999 default:
5000 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5001 MWL8K_NAME, priv->fw_state);
5002 BUG_ON(1);
5003 }
5004
5005 return;
5006
5007fail:
5008 priv->fw_state = FW_STATE_ERROR;
5009 complete(&priv->firmware_loading_complete);
5010 device_release_driver(&priv->pdev->dev);
5011 mwl8k_release_firmware(priv);
5012}
5013
5014static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5015 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005016{
5017 struct mwl8k_priv *priv = hw->priv;
5018 int rc;
5019
5020 /* Reset firmware and hardware */
5021 mwl8k_hw_reset(priv);
5022
5023 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005024 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005025 if (rc) {
5026 wiphy_err(hw->wiphy, "Firmware files not found\n");
5027 return rc;
5028 }
5029
Brian Cavagnolo99020472010-11-12 17:23:52 -08005030 if (nowait)
5031 return rc;
5032
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005033 /* Load firmware into hardware */
5034 rc = mwl8k_load_firmware(hw);
5035 if (rc)
5036 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5037
5038 /* Reclaim memory once firmware is successfully loaded */
5039 mwl8k_release_firmware(priv);
5040
5041 return rc;
5042}
5043
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005044static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5045{
5046 struct mwl8k_priv *priv = hw->priv;
5047 int rc = 0;
5048 int i;
5049
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005050 for (i = 0; i < mwl8k_tx_queues(priv); i++) {
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005051 rc = mwl8k_txq_init(hw, i);
5052 if (rc)
5053 break;
5054 if (priv->ap_fw)
5055 iowrite32(priv->txq[i].txd_dma,
5056 priv->sram + priv->txq_offset[i]);
5057 }
5058 return rc;
5059}
5060
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005061/* initialize hw after successfully loading a firmware image */
5062static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5063{
5064 struct mwl8k_priv *priv = hw->priv;
5065 int rc = 0;
5066 int i;
5067
5068 if (priv->ap_fw) {
5069 priv->rxd_ops = priv->device_info->ap_rxd_ops;
5070 if (priv->rxd_ops == NULL) {
5071 wiphy_err(hw->wiphy,
5072 "Driver does not have AP firmware image support for this hardware\n");
5073 goto err_stop_firmware;
5074 }
5075 } else {
5076 priv->rxd_ops = &rxd_sta_ops;
5077 }
5078
5079 priv->sniffer_enabled = false;
5080 priv->wmm_enabled = false;
5081 priv->pending_tx_pkts = 0;
5082
5083 rc = mwl8k_rxq_init(hw, 0);
5084 if (rc)
5085 goto err_stop_firmware;
5086 rxq_refill(hw, 0, INT_MAX);
5087
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005088 /* For the sta firmware, we need to know the dma addresses of tx queues
5089 * before sending MWL8K_CMD_GET_HW_SPEC. So we must initialize them
5090 * prior to issuing this command. But for the AP case, we learn the
5091 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5092 * case we must initialize the tx queues after.
5093 */
Brian Cavagnolo8a7a5782011-03-17 11:58:42 -07005094 priv->num_ampdu_queues = 0;
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005095 if (!priv->ap_fw) {
5096 rc = mwl8k_init_txqs(hw);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005097 if (rc)
5098 goto err_free_queues;
5099 }
5100
5101 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5102 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5103 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
5104 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
5105 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
5106
5107 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5108 IRQF_SHARED, MWL8K_NAME, hw);
5109 if (rc) {
5110 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5111 goto err_free_queues;
5112 }
5113
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005114 memset(priv->ampdu, 0, sizeof(priv->ampdu));
5115
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005116 /*
5117 * Temporarily enable interrupts. Initial firmware host
5118 * commands use interrupts and avoid polling. Disable
5119 * interrupts when done.
5120 */
5121 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5122
5123 /* Get config data, mac addrs etc */
5124 if (priv->ap_fw) {
5125 rc = mwl8k_cmd_get_hw_spec_ap(hw);
5126 if (!rc)
Brian Cavagnolo73b46322011-03-17 11:58:41 -07005127 rc = mwl8k_init_txqs(hw);
5128 if (!rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005129 rc = mwl8k_cmd_set_hw_spec(hw);
5130 } else {
5131 rc = mwl8k_cmd_get_hw_spec_sta(hw);
5132 }
5133 if (rc) {
5134 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5135 goto err_free_irq;
5136 }
5137
5138 /* Turn radio off */
5139 rc = mwl8k_cmd_radio_disable(hw);
5140 if (rc) {
5141 wiphy_err(hw->wiphy, "Cannot disable\n");
5142 goto err_free_irq;
5143 }
5144
5145 /* Clear MAC address */
5146 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5147 if (rc) {
5148 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5149 goto err_free_irq;
5150 }
5151
5152 /* Disable interrupts */
5153 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5154 free_irq(priv->pdev->irq, hw);
5155
5156 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5157 priv->device_info->part_name,
5158 priv->hw_rev, hw->wiphy->perm_addr,
5159 priv->ap_fw ? "AP" : "STA",
5160 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5161 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5162
5163 return 0;
5164
5165err_free_irq:
5166 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5167 free_irq(priv->pdev->irq, hw);
5168
5169err_free_queues:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005170 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005171 mwl8k_txq_deinit(hw, i);
5172 mwl8k_rxq_deinit(hw, 0);
5173
5174err_stop_firmware:
5175 mwl8k_hw_reset(priv);
5176
5177 return rc;
5178}
5179
5180/*
5181 * invoke mwl8k_reload_firmware to change the firmware image after the device
5182 * has already been registered
5183 */
5184static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
5185{
5186 int i, rc = 0;
5187 struct mwl8k_priv *priv = hw->priv;
5188
5189 mwl8k_stop(hw);
5190 mwl8k_rxq_deinit(hw, 0);
5191
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005192 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005193 mwl8k_txq_deinit(hw, i);
5194
Brian Cavagnolo99020472010-11-12 17:23:52 -08005195 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005196 if (rc)
5197 goto fail;
5198
5199 rc = mwl8k_probe_hw(hw);
5200 if (rc)
5201 goto fail;
5202
5203 rc = mwl8k_start(hw);
5204 if (rc)
5205 goto fail;
5206
5207 rc = mwl8k_config(hw, ~0);
5208 if (rc)
5209 goto fail;
5210
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005211 for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005212 rc = mwl8k_conf_tx(hw, i, &priv->wmm_params[i]);
5213 if (rc)
5214 goto fail;
5215 }
5216
5217 return rc;
5218
5219fail:
5220 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
5221 return rc;
5222}
5223
5224static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
5225{
5226 struct ieee80211_hw *hw = priv->hw;
5227 int i, rc;
5228
Brian Cavagnolo99020472010-11-12 17:23:52 -08005229 rc = mwl8k_load_firmware(hw);
5230 mwl8k_release_firmware(priv);
5231 if (rc) {
5232 wiphy_err(hw->wiphy, "Cannot start firmware\n");
5233 return rc;
5234 }
5235
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005236 /*
5237 * Extra headroom is the size of the required DMA header
5238 * minus the size of the smallest 802.11 frame (CTS frame).
5239 */
5240 hw->extra_tx_headroom =
5241 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
5242
5243 hw->channel_change_time = 10;
5244
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005245 hw->queues = MWL8K_TX_WMM_QUEUES;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005246
5247 /* Set rssi values to dBm */
Nishant Sarmukadam0bf22c32011-02-17 14:45:17 -08005248 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_HAS_RATE_CONTROL;
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005249 hw->vif_data_size = sizeof(struct mwl8k_vif);
5250 hw->sta_data_size = sizeof(struct mwl8k_sta);
5251
5252 priv->macids_used = 0;
5253 INIT_LIST_HEAD(&priv->vif_list);
5254
5255 /* Set default radio state and preamble */
5256 priv->radio_on = 0;
5257 priv->radio_short_preamble = 0;
5258
5259 /* Finalize join worker */
5260 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
5261
5262 /* TX reclaim and RX tasklets. */
5263 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
5264 tasklet_disable(&priv->poll_tx_task);
5265 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
5266 tasklet_disable(&priv->poll_rx_task);
5267
5268 /* Power management cookie */
5269 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
5270 if (priv->cookie == NULL)
5271 return -ENOMEM;
5272
5273 mutex_init(&priv->fw_mutex);
5274 priv->fw_mutex_owner = NULL;
5275 priv->fw_mutex_depth = 0;
5276 priv->hostcmd_wait = NULL;
5277
5278 spin_lock_init(&priv->tx_lock);
5279
Brian Cavagnoloac109fd2011-03-17 11:58:45 -07005280 spin_lock_init(&priv->stream_lock);
5281
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005282 priv->tx_wait = NULL;
5283
5284 rc = mwl8k_probe_hw(hw);
5285 if (rc)
5286 goto err_free_cookie;
5287
5288 hw->wiphy->interface_modes = 0;
5289 if (priv->ap_macids_supported || priv->device_info->fw_image_ap)
5290 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
5291 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
5292 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
5293
5294 rc = ieee80211_register_hw(hw);
5295 if (rc) {
5296 wiphy_err(hw->wiphy, "Cannot register device\n");
5297 goto err_unprobe_hw;
5298 }
5299
5300 return 0;
5301
5302err_unprobe_hw:
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005303 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005304 mwl8k_txq_deinit(hw, i);
5305 mwl8k_rxq_deinit(hw, 0);
5306
5307err_free_cookie:
5308 if (priv->cookie != NULL)
5309 pci_free_consistent(priv->pdev, 4,
5310 priv->cookie, priv->cookie_dma);
5311
5312 return rc;
5313}
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005314static int __devinit mwl8k_probe(struct pci_dev *pdev,
5315 const struct pci_device_id *id)
5316{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005317 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005318 struct ieee80211_hw *hw;
5319 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005320 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005321 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02005322
5323 if (!printed_version) {
5324 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
5325 printed_version = 1;
5326 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005327
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005328
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005329 rc = pci_enable_device(pdev);
5330 if (rc) {
5331 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
5332 MWL8K_NAME);
5333 return rc;
5334 }
5335
5336 rc = pci_request_regions(pdev, MWL8K_NAME);
5337 if (rc) {
5338 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
5339 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005340 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005341 }
5342
5343 pci_set_master(pdev);
5344
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005345
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005346 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
5347 if (hw == NULL) {
5348 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
5349 rc = -ENOMEM;
5350 goto err_free_reg;
5351 }
5352
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005353 SET_IEEE80211_DEV(hw, &pdev->dev);
5354 pci_set_drvdata(pdev, hw);
5355
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005356 priv = hw->priv;
5357 priv->hw = hw;
5358 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05005359 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005360
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005361
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005362 priv->sram = pci_iomap(pdev, 0, 0x10000);
5363 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005364 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005365 goto err_iounmap;
5366 }
5367
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005368 /*
5369 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
5370 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
5371 */
5372 priv->regs = pci_iomap(pdev, 1, 0x10000);
5373 if (priv->regs == NULL) {
5374 priv->regs = pci_iomap(pdev, 2, 0x10000);
5375 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07005376 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005377 goto err_iounmap;
5378 }
5379 }
5380
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005381 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08005382 * Choose the initial fw image depending on user input. If a second
5383 * image is available, make it the alternative image that will be
5384 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005385 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08005386 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005387 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005388 if (ap_mode_default && di->fw_image_ap) {
5389 priv->fw_pref = di->fw_image_ap;
5390 priv->fw_alt = di->fw_image_sta;
5391 } else if (!ap_mode_default && di->fw_image_sta) {
5392 priv->fw_pref = di->fw_image_sta;
5393 priv->fw_alt = di->fw_image_ap;
5394 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005395 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005396 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08005397 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
5398 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08005399 priv->fw_pref = di->fw_image_ap;
5400 }
5401 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005402 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08005403 goto err_stop_firmware;
Brian Cavagnolo99020472010-11-12 17:23:52 -08005404 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005405
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005406err_stop_firmware:
5407 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01005408
5409err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005410 if (priv->regs != NULL)
5411 pci_iounmap(pdev, priv->regs);
5412
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005413 if (priv->sram != NULL)
5414 pci_iounmap(pdev, priv->sram);
5415
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005416 pci_set_drvdata(pdev, NULL);
5417 ieee80211_free_hw(hw);
5418
5419err_free_reg:
5420 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01005421
5422err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005423 pci_disable_device(pdev);
5424
5425 return rc;
5426}
5427
Joerg Albert230f7af2009-04-18 02:10:45 +02005428static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005429{
5430 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
5431}
5432
Joerg Albert230f7af2009-04-18 02:10:45 +02005433static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005434{
5435 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
5436 struct mwl8k_priv *priv;
5437 int i;
5438
5439 if (hw == NULL)
5440 return;
5441 priv = hw->priv;
5442
Brian Cavagnolo99020472010-11-12 17:23:52 -08005443 wait_for_completion(&priv->firmware_loading_complete);
5444
5445 if (priv->fw_state == FW_STATE_ERROR) {
5446 mwl8k_hw_reset(priv);
5447 goto unmap;
5448 }
5449
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005450 ieee80211_stop_queues(hw);
5451
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02005452 ieee80211_unregister_hw(hw);
5453
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005454 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01005455 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01005456 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005457
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005458 /* Stop hardware */
5459 mwl8k_hw_reset(priv);
5460
5461 /* Return all skbs to mac80211 */
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005462 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01005463 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005464
Brian Cavagnoloe6007072011-03-17 11:58:44 -07005465 for (i = 0; i < mwl8k_tx_queues(priv); i++)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005466 mwl8k_txq_deinit(hw, i);
5467
5468 mwl8k_rxq_deinit(hw, 0);
5469
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005470 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005471
Brian Cavagnolo99020472010-11-12 17:23:52 -08005472unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005473 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02005474 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005475 pci_set_drvdata(pdev, NULL);
5476 ieee80211_free_hw(hw);
5477 pci_release_regions(pdev);
5478 pci_disable_device(pdev);
5479}
5480
5481static struct pci_driver mwl8k_driver = {
5482 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02005483 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01005484 .probe = mwl8k_probe,
5485 .remove = __devexit_p(mwl8k_remove),
5486 .shutdown = __devexit_p(mwl8k_shutdown),
5487};
5488
5489static int __init mwl8k_init(void)
5490{
5491 return pci_register_driver(&mwl8k_driver);
5492}
5493
5494static void __exit mwl8k_exit(void)
5495{
5496 pci_unregister_driver(&mwl8k_driver);
5497}
5498
5499module_init(mwl8k_init);
5500module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02005501
5502MODULE_DESCRIPTION(MWL8K_DESC);
5503MODULE_VERSION(MWL8K_VERSION);
5504MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
5505MODULE_LICENSE("GPL");