blob: 95bbdca0ba87303c9d7e33c7a3aa844bbc11dc97 [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka5fb2972010-01-12 13:51:38 +01005 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020015#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010016#include <linux/spinlock.h>
17#include <linux/list.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
20#include <linux/completion.h>
21#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010023#include <net/mac80211.h>
24#include <linux/moduleparam.h>
25#include <linux/firmware.h>
26#include <linux/workqueue.h>
27
28#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
29#define MWL8K_NAME KBUILD_MODNAME
Lennert Buytenheka5fb2972010-01-12 13:51:38 +010030#define MWL8K_VERSION "0.12"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031
Brian Cavagnolo0863ade2010-11-12 17:23:50 -080032/* Module parameters */
33static unsigned ap_mode_default;
34module_param(ap_mode_default, bool, 0);
35MODULE_PARM_DESC(ap_mode_default,
36 "Set to 1 to make ap mode the default instead of sta mode");
37
Lennert Buytenheka66098d2009-03-10 10:13:33 +010038/* Register definitions */
39#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020040#define MWL8K_MODE_STA 0x0000005a
41#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010042#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020043#define MWL8K_FWSTA_READY 0xf0f1f2f4
44#define MWL8K_FWAP_READY 0xf1f2f4a5
45#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010046#define MWL8K_HIU_SCRATCH 0x00000c40
47
48/* Host->device communications */
49#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
50#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
51#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
52#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
53#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020054#define MWL8K_H2A_INT_DUMMY (1 << 20)
55#define MWL8K_H2A_INT_RESET (1 << 15)
56#define MWL8K_H2A_INT_DOORBELL (1 << 1)
57#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010058
59/* Device->host communications */
60#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
61#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
62#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
63#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
64#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020065#define MWL8K_A2H_INT_DUMMY (1 << 20)
66#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
67#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
68#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
69#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
70#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
71#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
72#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
73#define MWL8K_A2H_INT_RX_READY (1 << 1)
74#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010075
76#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
77 MWL8K_A2H_INT_CHNL_SWITCHED | \
78 MWL8K_A2H_INT_QUEUE_EMPTY | \
79 MWL8K_A2H_INT_RADAR_DETECT | \
80 MWL8K_A2H_INT_RADIO_ON | \
81 MWL8K_A2H_INT_RADIO_OFF | \
82 MWL8K_A2H_INT_MAC_EVENT | \
83 MWL8K_A2H_INT_OPC_DONE | \
84 MWL8K_A2H_INT_RX_READY | \
85 MWL8K_A2H_INT_TX_DONE)
86
Lennert Buytenheka66098d2009-03-10 10:13:33 +010087#define MWL8K_RX_QUEUES 1
88#define MWL8K_TX_QUEUES 4
89
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020090struct rxd_ops {
91 int rxd_size;
92 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
93 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010094 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -040095 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020096};
97
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020098struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020099 char *part_name;
100 char *helper_image;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800101 char *fw_image_sta;
102 char *fw_image_ap;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100103 struct rxd_ops *ap_rxd_ops;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -0800104 u32 fw_api_ap;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200105};
106
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100107struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100109
110 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200111 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100112
113 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200114 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200116 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200118 struct {
119 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900120 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200121 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100122};
123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100124struct mwl8k_tx_queue {
125 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200126 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100127
128 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200129 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200131 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200132 struct mwl8k_tx_desc *txd;
133 dma_addr_t txd_dma;
134 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100135};
136
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100137struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100138 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100139 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200141 struct mwl8k_device_info *device_info;
142
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100143 void __iomem *sram;
144 void __iomem *regs;
145
146 /* firmware */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800147 const struct firmware *fw_helper;
148 const struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100149
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100150 /* hardware/firmware parameters */
151 bool ap_fw;
152 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100153 struct ieee80211_supported_band band_24;
154 struct ieee80211_channel channels_24[14];
155 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100156 struct ieee80211_supported_band band_50;
157 struct ieee80211_channel channels_50[4];
158 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100159 u32 ap_macids_supported;
160 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100161
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200162 /* firmware access */
163 struct mutex fw_mutex;
164 struct task_struct *fw_mutex_owner;
165 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200166 struct completion *hostcmd_wait;
167
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168 /* lock held over TX and TX reap */
169 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100170
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200171 /* TX quiesce completion, protected by fw_mutex and tx_lock */
172 struct completion *tx_wait;
173
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100174 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100175 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100176 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100178 /* power management status cookie from firmware */
179 u32 *cookie;
180 dma_addr_t cookie_dma;
181
182 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100183 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200184 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100185
186 /*
187 * Running count of TX packets in flight, to avoid
188 * iterating over the transmit rings each time.
189 */
190 int pending_tx_pkts;
191
192 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
193 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
194
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200195 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200196 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200197 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200198 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100199
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100200 /* XXX need to convert this to handle multiple interfaces */
201 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200202 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100203 struct sk_buff *beacon_skb;
204
205 /*
206 * This FJ worker has to be global as it is scheduled from the
207 * RX handler. At this point we don't know which interface it
208 * belongs to until the list of bssids waiting to complete join
209 * is checked.
210 */
211 struct work_struct finalize_join_worker;
212
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100213 /* Tasklet to perform TX reclaim. */
214 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100215
216 /* Tasklet to perform RX. */
217 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400218
219 /* Most recently reported noise in dBm */
220 s8 noise;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800221
222 /*
223 * preserve the queue configurations so they can be restored if/when
224 * the firmware image is swapped.
225 */
226 struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_QUEUES];
Brian Cavagnolo99020472010-11-12 17:23:52 -0800227
228 /* async firmware loading state */
229 unsigned fw_state;
230 char *fw_pref;
231 char *fw_alt;
232 struct completion firmware_loading_complete;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100233};
234
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800235#define MAX_WEP_KEY_LEN 13
236#define NUM_WEP_KEYS 4
237
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100238/* Per interface specific private data */
239struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100240 struct list_head list;
241 struct ieee80211_vif *vif;
242
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100243 /* Firmware macid for this vif. */
244 int macid;
245
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100246 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100247 u16 seqno;
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800248
249 /* Saved WEP keys */
250 struct {
251 u8 enabled;
252 u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
253 } wep_key_conf[NUM_WEP_KEYS];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100254};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200255#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100256
Lennert Buytenheka6804002010-01-04 21:55:42 +0100257struct mwl8k_sta {
258 /* Index into station database. Returned by UPDATE_STADB. */
259 u8 peer_id;
260};
261#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
262
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100263static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100264 { .center_freq = 2412, .hw_value = 1, },
265 { .center_freq = 2417, .hw_value = 2, },
266 { .center_freq = 2422, .hw_value = 3, },
267 { .center_freq = 2427, .hw_value = 4, },
268 { .center_freq = 2432, .hw_value = 5, },
269 { .center_freq = 2437, .hw_value = 6, },
270 { .center_freq = 2442, .hw_value = 7, },
271 { .center_freq = 2447, .hw_value = 8, },
272 { .center_freq = 2452, .hw_value = 9, },
273 { .center_freq = 2457, .hw_value = 10, },
274 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100275 { .center_freq = 2467, .hw_value = 12, },
276 { .center_freq = 2472, .hw_value = 13, },
277 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100278};
279
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100280static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100281 { .bitrate = 10, .hw_value = 2, },
282 { .bitrate = 20, .hw_value = 4, },
283 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200284 { .bitrate = 110, .hw_value = 22, },
285 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100286 { .bitrate = 60, .hw_value = 12, },
287 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100288 { .bitrate = 120, .hw_value = 24, },
289 { .bitrate = 180, .hw_value = 36, },
290 { .bitrate = 240, .hw_value = 48, },
291 { .bitrate = 360, .hw_value = 72, },
292 { .bitrate = 480, .hw_value = 96, },
293 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100294 { .bitrate = 720, .hw_value = 144, },
295};
296
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100297static const struct ieee80211_channel mwl8k_channels_50[] = {
298 { .center_freq = 5180, .hw_value = 36, },
299 { .center_freq = 5200, .hw_value = 40, },
300 { .center_freq = 5220, .hw_value = 44, },
301 { .center_freq = 5240, .hw_value = 48, },
302};
303
304static const struct ieee80211_rate mwl8k_rates_50[] = {
305 { .bitrate = 60, .hw_value = 12, },
306 { .bitrate = 90, .hw_value = 18, },
307 { .bitrate = 120, .hw_value = 24, },
308 { .bitrate = 180, .hw_value = 36, },
309 { .bitrate = 240, .hw_value = 48, },
310 { .bitrate = 360, .hw_value = 72, },
311 { .bitrate = 480, .hw_value = 96, },
312 { .bitrate = 540, .hw_value = 108, },
313 { .bitrate = 720, .hw_value = 144, },
314};
315
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100316/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100317#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800318#define MWL8K_CMD_SET 0x0001
319#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100320
321/* Firmware command codes */
322#define MWL8K_CMD_CODE_DNLD 0x0001
323#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200324#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100325#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
326#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200327#define MWL8K_CMD_RADIO_CONTROL 0x001c
328#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800329#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200330#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100331#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100332#define MWL8K_CMD_SET_PRE_SCAN 0x0107
333#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200334#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100335#define MWL8K_CMD_SET_AID 0x010d
336#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200337#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100338#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200339#define MWL8K_CMD_SET_SLOT 0x0114
340#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
341#define MWL8K_CMD_SET_WMM_MODE 0x0123
342#define MWL8K_CMD_MIMO_CONFIG 0x0125
343#define MWL8K_CMD_USE_FIXED_RATE 0x0126
344#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100345#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200346#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100347#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
348#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200349#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100350
John W. Linvilleb6037422010-07-20 13:55:00 -0400351static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100352{
John W. Linvilleb6037422010-07-20 13:55:00 -0400353 u16 command = le16_to_cpu(cmd);
354
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100355#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
356 snprintf(buf, bufsize, "%s", #x);\
357 return buf;\
358 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400359 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100360 MWL8K_CMDNAME(CODE_DNLD);
361 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200362 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100363 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
364 MWL8K_CMDNAME(GET_STAT);
365 MWL8K_CMDNAME(RADIO_CONTROL);
366 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800367 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200368 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100369 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100370 MWL8K_CMDNAME(SET_PRE_SCAN);
371 MWL8K_CMDNAME(SET_POST_SCAN);
372 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100373 MWL8K_CMDNAME(SET_AID);
374 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200375 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100376 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200377 MWL8K_CMDNAME(SET_SLOT);
378 MWL8K_CMDNAME(SET_EDCA_PARAMS);
379 MWL8K_CMDNAME(SET_WMM_MODE);
380 MWL8K_CMDNAME(MIMO_CONFIG);
381 MWL8K_CMDNAME(USE_FIXED_RATE);
382 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200383 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200384 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100385 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100386 MWL8K_CMDNAME(SET_NEW_STN);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200387 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100388 default:
389 snprintf(buf, bufsize, "0x%x", cmd);
390 }
391#undef MWL8K_CMDNAME
392
393 return buf;
394}
395
396/* Hardware and firmware reset */
397static void mwl8k_hw_reset(struct mwl8k_priv *priv)
398{
399 iowrite32(MWL8K_H2A_INT_RESET,
400 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
401 iowrite32(MWL8K_H2A_INT_RESET,
402 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
403 msleep(20);
404}
405
406/* Release fw image */
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800407static void mwl8k_release_fw(const struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100408{
409 if (*fw == NULL)
410 return;
411 release_firmware(*fw);
412 *fw = NULL;
413}
414
415static void mwl8k_release_firmware(struct mwl8k_priv *priv)
416{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100417 mwl8k_release_fw(&priv->fw_ucode);
418 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100419}
420
Brian Cavagnolo99020472010-11-12 17:23:52 -0800421/* states for asynchronous f/w loading */
422static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
423enum {
424 FW_STATE_INIT = 0,
425 FW_STATE_LOADING_PREF,
426 FW_STATE_LOADING_ALT,
427 FW_STATE_ERROR,
428};
429
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100430/* Request fw image */
431static int mwl8k_request_fw(struct mwl8k_priv *priv,
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800432 const char *fname, const struct firmware **fw,
Brian Cavagnolo99020472010-11-12 17:23:52 -0800433 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100434{
435 /* release current image */
436 if (*fw != NULL)
437 mwl8k_release_fw(fw);
438
Brian Cavagnolo99020472010-11-12 17:23:52 -0800439 if (nowait)
440 return request_firmware_nowait(THIS_MODULE, 1, fname,
441 &priv->pdev->dev, GFP_KERNEL,
442 priv, mwl8k_fw_state_machine);
443 else
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800444 return request_firmware(fw, fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100445}
446
Brian Cavagnolo99020472010-11-12 17:23:52 -0800447static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
448 bool nowait)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100449{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200450 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100451 int rc;
452
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200453 if (di->helper_image != NULL) {
Brian Cavagnolo99020472010-11-12 17:23:52 -0800454 if (nowait)
455 rc = mwl8k_request_fw(priv, di->helper_image,
456 &priv->fw_helper, true);
457 else
458 rc = mwl8k_request_fw(priv, di->helper_image,
459 &priv->fw_helper, false);
460 if (rc)
461 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
462 pci_name(priv->pdev), di->helper_image);
463
464 if (rc || nowait)
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200465 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100466 }
467
Brian Cavagnolo99020472010-11-12 17:23:52 -0800468 if (nowait) {
469 /*
470 * if we get here, no helper image is needed. Skip the
471 * FW_STATE_INIT state.
472 */
473 priv->fw_state = FW_STATE_LOADING_PREF;
474 rc = mwl8k_request_fw(priv, fw_image,
475 &priv->fw_ucode,
476 true);
477 } else
478 rc = mwl8k_request_fw(priv, fw_image,
479 &priv->fw_ucode, false);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100480 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200481 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -0800482 pci_name(priv->pdev), fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100483 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100484 return rc;
485 }
486
487 return 0;
488}
489
490struct mwl8k_cmd_pkt {
491 __le16 code;
492 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100493 __u8 seq_num;
494 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100495 __le16 result;
496 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000497} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100498
499/*
500 * Firmware loading.
501 */
502static int
503mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
504{
505 void __iomem *regs = priv->regs;
506 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100507 int loops;
508
509 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
510 if (pci_dma_mapping_error(priv->pdev, dma_addr))
511 return -ENOMEM;
512
513 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
514 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
515 iowrite32(MWL8K_H2A_INT_DOORBELL,
516 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
517 iowrite32(MWL8K_H2A_INT_DUMMY,
518 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
519
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100520 loops = 1000;
521 do {
522 u32 int_code;
523
524 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
525 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
526 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100527 break;
528 }
529
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200530 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100531 udelay(1);
532 } while (--loops);
533
534 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
535
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200536 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100537}
538
539static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
540 const u8 *data, size_t length)
541{
542 struct mwl8k_cmd_pkt *cmd;
543 int done;
544 int rc = 0;
545
546 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
547 if (cmd == NULL)
548 return -ENOMEM;
549
550 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
551 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100552 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100553 cmd->result = 0;
554
555 done = 0;
556 while (length) {
557 int block_size = length > 256 ? 256 : length;
558
559 memcpy(cmd->payload, data + done, block_size);
560 cmd->length = cpu_to_le16(block_size);
561
562 rc = mwl8k_send_fw_load_cmd(priv, cmd,
563 sizeof(*cmd) + block_size);
564 if (rc)
565 break;
566
567 done += block_size;
568 length -= block_size;
569 }
570
571 if (!rc) {
572 cmd->length = 0;
573 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
574 }
575
576 kfree(cmd);
577
578 return rc;
579}
580
581static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
582 const u8 *data, size_t length)
583{
584 unsigned char *buffer;
585 int may_continue, rc = 0;
586 u32 done, prev_block_size;
587
588 buffer = kmalloc(1024, GFP_KERNEL);
589 if (buffer == NULL)
590 return -ENOMEM;
591
592 done = 0;
593 prev_block_size = 0;
594 may_continue = 1000;
595 while (may_continue > 0) {
596 u32 block_size;
597
598 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
599 if (block_size & 1) {
600 block_size &= ~1;
601 may_continue--;
602 } else {
603 done += prev_block_size;
604 length -= prev_block_size;
605 }
606
607 if (block_size > 1024 || block_size > length) {
608 rc = -EOVERFLOW;
609 break;
610 }
611
612 if (length == 0) {
613 rc = 0;
614 break;
615 }
616
617 if (block_size == 0) {
618 rc = -EPROTO;
619 may_continue--;
620 udelay(1);
621 continue;
622 }
623
624 prev_block_size = block_size;
625 memcpy(buffer, data + done, block_size);
626
627 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
628 if (rc)
629 break;
630 }
631
632 if (!rc && length != 0)
633 rc = -EREMOTEIO;
634
635 kfree(buffer);
636
637 return rc;
638}
639
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200640static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100641{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200642 struct mwl8k_priv *priv = hw->priv;
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800643 const struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200644 int rc;
645 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100646
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200647 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Brian Cavagnolod1f9e412010-11-12 17:23:53 -0800648 const struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100649
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200650 if (helper == NULL) {
651 printk(KERN_ERR "%s: helper image needed but none "
652 "given\n", pci_name(priv->pdev));
653 return -EINVAL;
654 }
655
656 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100657 if (rc) {
658 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200659 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100660 return rc;
661 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100662 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100663
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200664 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100665 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200666 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100667 }
668
669 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200670 printk(KERN_ERR "%s: unable to load firmware image\n",
671 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100672 return rc;
673 }
674
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100675 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100676
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100677 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100678 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200679 u32 ready_code;
680
681 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
682 if (ready_code == MWL8K_FWAP_READY) {
683 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100684 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200685 } else if (ready_code == MWL8K_FWSTA_READY) {
686 priv->ap_fw = 0;
687 break;
688 }
689
690 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100691 udelay(1);
692 } while (--loops);
693
694 return loops ? 0 : -ETIMEDOUT;
695}
696
697
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100698/* DMA header used by firmware and hardware. */
699struct mwl8k_dma_data {
700 __le16 fwlen;
701 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100702 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000703} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100704
705/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100706static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100707{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100708 struct mwl8k_dma_data *tr;
709 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100710
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100711 tr = (struct mwl8k_dma_data *)skb->data;
712 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
713
714 if (hdrlen != sizeof(tr->wh)) {
715 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
716 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
717 *((__le16 *)(tr->data - 2)) = qos;
718 } else {
719 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
720 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100721 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100722
723 if (hdrlen != sizeof(*tr))
724 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100725}
726
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800727static void
728mwl8k_add_dma_header(struct sk_buff *skb, int tail_pad)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100729{
730 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100731 int hdrlen;
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800732 int reqd_hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100733 struct mwl8k_dma_data *tr;
734
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100735 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100736 * Add a firmware DMA header; the firmware requires that we
737 * present a 2-byte payload length followed by a 4-address
738 * header (without QoS field), followed (optionally) by any
739 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100740 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100741 wh = (struct ieee80211_hdr *)skb->data;
742
743 hdrlen = ieee80211_hdrlen(wh->frame_control);
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800744 reqd_hdrlen = sizeof(*tr);
745
746 if (hdrlen != reqd_hdrlen)
747 skb_push(skb, reqd_hdrlen - hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100748
749 if (ieee80211_is_data_qos(wh->frame_control))
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800750 hdrlen -= IEEE80211_QOS_CTL_LEN;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100751
752 tr = (struct mwl8k_dma_data *)skb->data;
753 if (wh != &tr->wh)
754 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100755 if (hdrlen != sizeof(tr->wh))
756 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100757
758 /*
759 * Firmware length is the length of the fully formed "802.11
760 * payload". That is, everything except for the 802.11 header.
761 * This includes all crypto material including the MIC.
762 */
Nishant Sarmukadam252486a2010-12-30 11:23:31 -0800763 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100764}
765
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -0800766static void mwl8k_encapsulate_tx_frame(struct sk_buff *skb)
767{
768 struct ieee80211_hdr *wh;
769 struct ieee80211_tx_info *tx_info;
770 struct ieee80211_key_conf *key_conf;
771 int data_pad;
772
773 wh = (struct ieee80211_hdr *)skb->data;
774
775 tx_info = IEEE80211_SKB_CB(skb);
776
777 key_conf = NULL;
778 if (ieee80211_is_data(wh->frame_control))
779 key_conf = tx_info->control.hw_key;
780
781 /*
782 * Make sure the packet header is in the DMA header format (4-address
783 * without QoS), the necessary crypto padding between the header and the
784 * payload has already been provided by mac80211, but it doesn't add tail
785 * padding when HW crypto is enabled.
786 *
787 * We have the following trailer padding requirements:
788 * - WEP: 4 trailer bytes (ICV)
789 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
790 * - CCMP: 8 trailer bytes (MIC)
791 */
792 data_pad = 0;
793 if (key_conf != NULL) {
794 switch (key_conf->cipher) {
795 case WLAN_CIPHER_SUITE_WEP40:
796 case WLAN_CIPHER_SUITE_WEP104:
797 data_pad = 4;
798 break;
799 case WLAN_CIPHER_SUITE_TKIP:
800 data_pad = 12;
801 break;
802 case WLAN_CIPHER_SUITE_CCMP:
803 data_pad = 8;
804 break;
805 }
806 }
807 mwl8k_add_dma_header(skb, data_pad);
808}
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100809
810/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100811 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200812 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100813struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200814 __le16 pkt_len;
815 __u8 sq2;
816 __u8 rate;
817 __le32 pkt_phys_addr;
818 __le32 next_rxd_phys_addr;
819 __le16 qos_control;
820 __le16 htsig2;
821 __le32 hw_rssi_info;
822 __le32 hw_noise_floor_info;
823 __u8 noise_floor;
824 __u8 pad0[3];
825 __u8 rssi;
826 __u8 rx_status;
827 __u8 channel;
828 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000829} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200830
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100831#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
832#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
833#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100834
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100835#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200836
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100837static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200838{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100839 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200840
841 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100842 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200843}
844
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100845static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200846{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100847 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200848
849 rxd->pkt_len = cpu_to_le16(len);
850 rxd->pkt_phys_addr = cpu_to_le32(addr);
851 wmb();
852 rxd->rx_ctrl = 0;
853}
854
855static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100856mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400857 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200858{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100859 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200860
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100861 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200862 return -1;
863 rmb();
864
865 memset(status, 0, sizeof(*status));
866
867 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400868 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200869
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100870 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200871 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100872 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100873 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100874 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200875 } else {
876 int i;
877
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100878 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
879 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200880 status->rate_idx = i;
881 break;
882 }
883 }
884 }
885
Lennert Buytenhek85478342010-01-12 13:48:56 +0100886 if (rxd->channel > 14) {
887 status->band = IEEE80211_BAND_5GHZ;
888 if (!(status->flag & RX_FLAG_HT))
889 status->rate_idx -= 5;
890 } else {
891 status->band = IEEE80211_BAND_2GHZ;
892 }
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200893 status->freq = ieee80211_channel_to_frequency(rxd->channel);
894
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100895 *qos = rxd->qos_control;
896
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200897 return le16_to_cpu(rxd->pkt_len);
898}
899
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100900static struct rxd_ops rxd_8366_ap_ops = {
901 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
902 .rxd_init = mwl8k_rxd_8366_ap_init,
903 .rxd_refill = mwl8k_rxd_8366_ap_refill,
904 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200905};
906
907/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100908 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100909 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100910struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100911 __le16 pkt_len;
912 __u8 link_quality;
913 __u8 noise_level;
914 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200915 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100916 __le16 qos_control;
917 __le16 rate_info;
918 __le32 pad0[4];
919 __u8 rssi;
920 __u8 channel;
921 __le16 pad1;
922 __u8 rx_ctrl;
923 __u8 rx_status;
924 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000925} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100926
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100927#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
928#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
929#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
930#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
931#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
932#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200933
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100934#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200935
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100936static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200937{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100938 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200939
940 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100941 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200942}
943
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100944static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200945{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100946 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200947
948 rxd->pkt_len = cpu_to_le16(len);
949 rxd->pkt_phys_addr = cpu_to_le32(addr);
950 wmb();
951 rxd->rx_ctrl = 0;
952}
953
954static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100955mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400956 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200957{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100958 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200959 u16 rate_info;
960
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100961 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200962 return -1;
963 rmb();
964
965 rate_info = le16_to_cpu(rxd->rate_info);
966
967 memset(status, 0, sizeof(*status));
968
969 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400970 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100971 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
972 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200973
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100974 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200975 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100976 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200977 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100978 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200979 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100980 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200981 status->flag |= RX_FLAG_HT;
982
Lennert Buytenhek85478342010-01-12 13:48:56 +0100983 if (rxd->channel > 14) {
984 status->band = IEEE80211_BAND_5GHZ;
985 if (!(status->flag & RX_FLAG_HT))
986 status->rate_idx -= 5;
987 } else {
988 status->band = IEEE80211_BAND_2GHZ;
989 }
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200990 status->freq = ieee80211_channel_to_frequency(rxd->channel);
991
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100992 *qos = rxd->qos_control;
993
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200994 return le16_to_cpu(rxd->pkt_len);
995}
996
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100997static struct rxd_ops rxd_sta_ops = {
998 .rxd_size = sizeof(struct mwl8k_rxd_sta),
999 .rxd_init = mwl8k_rxd_sta_init,
1000 .rxd_refill = mwl8k_rxd_sta_refill,
1001 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001002};
1003
1004
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001005#define MWL8K_RX_DESCS 256
1006#define MWL8K_RX_MAXSZ 3800
1007
1008static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1009{
1010 struct mwl8k_priv *priv = hw->priv;
1011 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1012 int size;
1013 int i;
1014
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001015 rxq->rxd_count = 0;
1016 rxq->head = 0;
1017 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001018
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001019 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001020
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001021 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
1022 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001023 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001024 return -ENOMEM;
1025 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001026 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001027
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001028 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
1029 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001030 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001031 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001032 return -ENOMEM;
1033 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001034 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001035
1036 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001037 int desc_size;
1038 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001039 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001040 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001041
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001042 desc_size = priv->rxd_ops->rxd_size;
1043 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001044
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001045 nexti = i + 1;
1046 if (nexti == MWL8K_RX_DESCS)
1047 nexti = 0;
1048 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1049
1050 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001051 }
1052
1053 return 0;
1054}
1055
1056static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1057{
1058 struct mwl8k_priv *priv = hw->priv;
1059 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1060 int refilled;
1061
1062 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001063 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001064 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001065 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001066 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001067 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001068
1069 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1070 if (skb == NULL)
1071 break;
1072
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001073 addr = pci_map_single(priv->pdev, skb->data,
1074 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001075
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001076 rxq->rxd_count++;
1077 rx = rxq->tail++;
1078 if (rxq->tail == MWL8K_RX_DESCS)
1079 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001080 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001081 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001082
1083 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1084 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001085
1086 refilled++;
1087 }
1088
1089 return refilled;
1090}
1091
1092/* Must be called only when the card's reception is completely halted */
1093static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1094{
1095 struct mwl8k_priv *priv = hw->priv;
1096 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1097 int i;
1098
1099 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001100 if (rxq->buf[i].skb != NULL) {
1101 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001102 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001103 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001104 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001105
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001106 kfree_skb(rxq->buf[i].skb);
1107 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001108 }
1109 }
1110
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001111 kfree(rxq->buf);
1112 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001113
1114 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001115 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001116 rxq->rxd, rxq->rxd_dma);
1117 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001118}
1119
1120
1121/*
1122 * Scan a list of BSSIDs to process for finalize join.
1123 * Allows for extension to process multiple BSSIDs.
1124 */
1125static inline int
1126mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1127{
1128 return priv->capture_beacon &&
1129 ieee80211_is_beacon(wh->frame_control) &&
1130 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1131}
1132
Lennert Buytenhek37797522009-10-22 20:19:45 +02001133static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1134 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001135{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001136 struct mwl8k_priv *priv = hw->priv;
1137
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001138 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001139 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001140
1141 /*
1142 * Use GFP_ATOMIC as rxq_process is called from
1143 * the primary interrupt handler, memory allocation call
1144 * must not sleep.
1145 */
1146 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1147 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001148 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001149}
1150
1151static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1152{
1153 struct mwl8k_priv *priv = hw->priv;
1154 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1155 int processed;
1156
1157 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001158 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001160 void *rxd;
1161 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001162 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001163 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001164
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001165 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001166 if (skb == NULL)
1167 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001168
1169 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1170
John W. Linville0d462bb2010-07-28 14:04:24 -04001171 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1172 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001173 if (pkt_len < 0)
1174 break;
1175
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001176 rxq->buf[rxq->head].skb = NULL;
1177
1178 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001179 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001180 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001181 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001182
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001183 rxq->head++;
1184 if (rxq->head == MWL8K_RX_DESCS)
1185 rxq->head = 0;
1186
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001187 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001188
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001189 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001190 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001191
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001192 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001193 * Check for a pending join operation. Save a
1194 * copy of the beacon and schedule a tasklet to
1195 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001196 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001197 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001198 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001199
Johannes Bergf1d58c22009-06-17 13:13:00 +02001200 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1201 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001202
1203 processed++;
1204 }
1205
1206 return processed;
1207}
1208
1209
1210/*
1211 * Packet transmission.
1212 */
1213
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001214#define MWL8K_TXD_STATUS_OK 0x00000001
1215#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1216#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1217#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001218#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001219
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001220#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1221#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1222#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1223#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1224#define MWL8K_QOS_EOSP 0x0010
1225
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001226struct mwl8k_tx_desc {
1227 __le32 status;
1228 __u8 data_rate;
1229 __u8 tx_priority;
1230 __le16 qos_control;
1231 __le32 pkt_phys_addr;
1232 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001233 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001234 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001235 __le32 reserved;
1236 __le16 rate_info;
1237 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001238 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001239} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001240
1241#define MWL8K_TX_DESCS 128
1242
1243static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1244{
1245 struct mwl8k_priv *priv = hw->priv;
1246 struct mwl8k_tx_queue *txq = priv->txq + index;
1247 int size;
1248 int i;
1249
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001250 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001251 txq->head = 0;
1252 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001253
1254 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1255
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001256 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1257 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001258 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001259 return -ENOMEM;
1260 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001261 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001262
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001263 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1264 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001265 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001266 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001267 return -ENOMEM;
1268 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001269 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001270
1271 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1272 struct mwl8k_tx_desc *tx_desc;
1273 int nexti;
1274
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001275 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001276 nexti = (i + 1) % MWL8K_TX_DESCS;
1277
1278 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001279 tx_desc->next_txd_phys_addr =
1280 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001281 }
1282
1283 return 0;
1284}
1285
1286static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1287{
1288 iowrite32(MWL8K_H2A_INT_PPA_READY,
1289 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1290 iowrite32(MWL8K_H2A_INT_DUMMY,
1291 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1292 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1293}
1294
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001295static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001296{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001297 struct mwl8k_priv *priv = hw->priv;
1298 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001300 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1301 struct mwl8k_tx_queue *txq = priv->txq + i;
1302 int fw_owned = 0;
1303 int drv_owned = 0;
1304 int unused = 0;
1305 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001306
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001307 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001308 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1309 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001310
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001311 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001312 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001313 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001314 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001315 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001316
1317 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001318 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001319 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001320
Joe Perchesc96c31e2010-07-26 14:39:58 -07001321 wiphy_err(hw->wiphy,
1322 "txq[%d] len=%d head=%d tail=%d "
1323 "fw_owned=%d drv_owned=%d unused=%d\n",
1324 i,
1325 txq->len, txq->head, txq->tail,
1326 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001327 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001328}
1329
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001330/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001331 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001332 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001333#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001334
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001335static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001336{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001337 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001338 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001339 int retry;
1340 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341
1342 might_sleep();
1343
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001344 /*
1345 * The TX queues are stopped at this point, so this test
1346 * doesn't need to take ->tx_lock.
1347 */
1348 if (!priv->pending_tx_pkts)
1349 return 0;
1350
1351 retry = 0;
1352 rc = 0;
1353
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001354 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001355 priv->tx_wait = &tx_wait;
1356 while (!rc) {
1357 int oldcount;
1358 unsigned long timeout;
1359
1360 oldcount = priv->pending_tx_pkts;
1361
1362 spin_unlock_bh(&priv->tx_lock);
1363 timeout = wait_for_completion_timeout(&tx_wait,
1364 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1365 spin_lock_bh(&priv->tx_lock);
1366
1367 if (timeout) {
1368 WARN_ON(priv->pending_tx_pkts);
1369 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001370 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001371 }
1372 break;
1373 }
1374
1375 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001376 wiphy_notice(hw->wiphy,
1377 "waiting for tx rings to drain (%d -> %d pkts)\n",
1378 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001379 retry = 1;
1380 continue;
1381 }
1382
1383 priv->tx_wait = NULL;
1384
Joe Perchesc96c31e2010-07-26 14:39:58 -07001385 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1386 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001387 mwl8k_dump_tx_rings(hw);
1388
1389 rc = -ETIMEDOUT;
1390 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001391 spin_unlock_bh(&priv->tx_lock);
1392
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001393 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001394}
1395
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001396#define MWL8K_TXD_SUCCESS(status) \
1397 ((status) & (MWL8K_TXD_STATUS_OK | \
1398 MWL8K_TXD_STATUS_OK_RETRY | \
1399 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001400
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001401static int
1402mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001403{
1404 struct mwl8k_priv *priv = hw->priv;
1405 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001406 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001407
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001408 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001409 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001410 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001411 struct mwl8k_tx_desc *tx_desc;
1412 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001413 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001414 struct sk_buff *skb;
1415 struct ieee80211_tx_info *info;
1416 u32 status;
1417
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001418 tx = txq->head;
1419 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001420
1421 status = le32_to_cpu(tx_desc->status);
1422
1423 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1424 if (!force)
1425 break;
1426 tx_desc->status &=
1427 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1428 }
1429
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001430 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001431 BUG_ON(txq->len == 0);
1432 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001433 priv->pending_tx_pkts--;
1434
1435 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001436 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001437 skb = txq->skb[tx];
1438 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001439
1440 BUG_ON(skb == NULL);
1441 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1442
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001443 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001444
1445 /* Mark descriptor as unused */
1446 tx_desc->pkt_phys_addr = 0;
1447 tx_desc->pkt_len = 0;
1448
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001449 info = IEEE80211_SKB_CB(skb);
1450 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001451 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001452 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001453
1454 ieee80211_tx_status_irqsafe(hw, skb);
1455
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001456 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001457 }
1458
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001459 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001460 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001461
1462 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001463}
1464
1465/* must be called only when the card's transmit is completely halted */
1466static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1467{
1468 struct mwl8k_priv *priv = hw->priv;
1469 struct mwl8k_tx_queue *txq = priv->txq + index;
1470
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001471 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001472
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001473 kfree(txq->skb);
1474 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001475
1476 pci_free_consistent(priv->pdev,
1477 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001478 txq->txd, txq->txd_dma);
1479 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001480}
1481
1482static int
1483mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1484{
1485 struct mwl8k_priv *priv = hw->priv;
1486 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001487 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001488 struct ieee80211_hdr *wh;
1489 struct mwl8k_tx_queue *txq;
1490 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001491 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001492 u32 txstatus;
1493 u8 txdatarate;
1494 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001495
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001496 wh = (struct ieee80211_hdr *)skb->data;
1497 if (ieee80211_is_data_qos(wh->frame_control))
1498 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1499 else
1500 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001501
Nishant Sarmukadame53d9b92010-12-30 11:23:32 -08001502 mwl8k_encapsulate_tx_frame(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001503 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001504
1505 tx_info = IEEE80211_SKB_CB(skb);
1506 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001507
1508 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001509 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001510 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1511 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001512 }
1513
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001514 /* Setup firmware control bit fields for each frame type. */
1515 txstatus = 0;
1516 txdatarate = 0;
1517 if (ieee80211_is_mgmt(wh->frame_control) ||
1518 ieee80211_is_ctl(wh->frame_control)) {
1519 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001520 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001521 } else if (ieee80211_is_data(wh->frame_control)) {
1522 txdatarate = 1;
1523 if (is_multicast_ether_addr(wh->addr1))
1524 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1525
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001526 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001527 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001528 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001529 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001530 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001531 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001532
1533 dma = pci_map_single(priv->pdev, skb->data,
1534 skb->len, PCI_DMA_TODEVICE);
1535
1536 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001537 wiphy_debug(hw->wiphy,
1538 "failed to dma map skb, dropping TX frame.\n");
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001539 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001540 return NETDEV_TX_OK;
1541 }
1542
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001543 spin_lock_bh(&priv->tx_lock);
1544
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001545 txq = priv->txq + index;
1546
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001547 BUG_ON(txq->skb[txq->tail] != NULL);
1548 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001549
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001550 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001551 tx->data_rate = txdatarate;
1552 tx->tx_priority = index;
1553 tx->qos_control = cpu_to_le16(qos);
1554 tx->pkt_phys_addr = cpu_to_le32(dma);
1555 tx->pkt_len = cpu_to_le16(skb->len);
1556 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001557 if (!priv->ap_fw && tx_info->control.sta != NULL)
1558 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1559 else
1560 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001561 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001562 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1563
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001564 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001565 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001566
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001567 txq->tail++;
1568 if (txq->tail == MWL8K_TX_DESCS)
1569 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001570
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001571 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001572 ieee80211_stop_queue(hw, index);
1573
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001574 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001575
1576 spin_unlock_bh(&priv->tx_lock);
1577
1578 return NETDEV_TX_OK;
1579}
1580
1581
1582/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001583 * Firmware access.
1584 *
1585 * We have the following requirements for issuing firmware commands:
1586 * - Some commands require that the packet transmit path is idle when
1587 * the command is issued. (For simplicity, we'll just quiesce the
1588 * transmit path for every command.)
1589 * - There are certain sequences of commands that need to be issued to
1590 * the hardware sequentially, with no other intervening commands.
1591 *
1592 * This leads to an implementation of a "firmware lock" as a mutex that
1593 * can be taken recursively, and which is taken by both the low-level
1594 * command submission function (mwl8k_post_cmd) as well as any users of
1595 * that function that require issuing of an atomic sequence of commands,
1596 * and quiesces the transmit path whenever it's taken.
1597 */
1598static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1599{
1600 struct mwl8k_priv *priv = hw->priv;
1601
1602 if (priv->fw_mutex_owner != current) {
1603 int rc;
1604
1605 mutex_lock(&priv->fw_mutex);
1606 ieee80211_stop_queues(hw);
1607
1608 rc = mwl8k_tx_wait_empty(hw);
1609 if (rc) {
1610 ieee80211_wake_queues(hw);
1611 mutex_unlock(&priv->fw_mutex);
1612
1613 return rc;
1614 }
1615
1616 priv->fw_mutex_owner = current;
1617 }
1618
1619 priv->fw_mutex_depth++;
1620
1621 return 0;
1622}
1623
1624static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1625{
1626 struct mwl8k_priv *priv = hw->priv;
1627
1628 if (!--priv->fw_mutex_depth) {
1629 ieee80211_wake_queues(hw);
1630 priv->fw_mutex_owner = NULL;
1631 mutex_unlock(&priv->fw_mutex);
1632 }
1633}
1634
1635
1636/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001637 * Command processing.
1638 */
1639
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001640/* Timeout firmware commands after 10s */
1641#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001642
1643static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1644{
1645 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1646 struct mwl8k_priv *priv = hw->priv;
1647 void __iomem *regs = priv->regs;
1648 dma_addr_t dma_addr;
1649 unsigned int dma_size;
1650 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001651 unsigned long timeout = 0;
1652 u8 buf[32];
1653
John W. Linvilleb6037422010-07-20 13:55:00 -04001654 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001655 dma_size = le16_to_cpu(cmd->length);
1656 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1657 PCI_DMA_BIDIRECTIONAL);
1658 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1659 return -ENOMEM;
1660
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001661 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001662 if (rc) {
1663 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1664 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001665 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001666 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001667
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001668 priv->hostcmd_wait = &cmd_wait;
1669 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1670 iowrite32(MWL8K_H2A_INT_DOORBELL,
1671 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1672 iowrite32(MWL8K_H2A_INT_DUMMY,
1673 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001674
1675 timeout = wait_for_completion_timeout(&cmd_wait,
1676 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1677
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001678 priv->hostcmd_wait = NULL;
1679
1680 mwl8k_fw_unlock(hw);
1681
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001682 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1683 PCI_DMA_BIDIRECTIONAL);
1684
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001685 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001686 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001687 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1688 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001689 rc = -ETIMEDOUT;
1690 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001691 int ms;
1692
1693 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1694
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001695 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001696 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001697 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001698 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1699 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001700 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001701 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001702 mwl8k_cmd_name(cmd->code,
1703 buf, sizeof(buf)),
1704 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001705 }
1706
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001707 return rc;
1708}
1709
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001710static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1711 struct ieee80211_vif *vif,
1712 struct mwl8k_cmd_pkt *cmd)
1713{
1714 if (vif != NULL)
1715 cmd->macid = MWL8K_VIF(vif)->macid;
1716 return mwl8k_post_cmd(hw, cmd);
1717}
1718
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001719/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001720 * Setup code shared between STA and AP firmware images.
1721 */
1722static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1723{
1724 struct mwl8k_priv *priv = hw->priv;
1725
1726 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1727 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1728
1729 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1730 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1731
1732 priv->band_24.band = IEEE80211_BAND_2GHZ;
1733 priv->band_24.channels = priv->channels_24;
1734 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1735 priv->band_24.bitrates = priv->rates_24;
1736 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1737
1738 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1739}
1740
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001741static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1742{
1743 struct mwl8k_priv *priv = hw->priv;
1744
1745 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1746 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1747
1748 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1749 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1750
1751 priv->band_50.band = IEEE80211_BAND_5GHZ;
1752 priv->band_50.channels = priv->channels_50;
1753 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1754 priv->band_50.bitrates = priv->rates_50;
1755 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1756
1757 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1758}
1759
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001760/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001761 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001763struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001764 struct mwl8k_cmd_pkt header;
1765 __u8 hw_rev;
1766 __u8 host_interface;
1767 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001768 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001769 __le16 region_code;
1770 __le32 fw_rev;
1771 __le32 ps_cookie;
1772 __le32 caps;
1773 __u8 mcs_bitmap[16];
1774 __le32 rx_queue_ptr;
1775 __le32 num_tx_queues;
1776 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1777 __le32 caps2;
1778 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001779 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001780} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001781
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001782#define MWL8K_CAP_MAX_AMSDU 0x20000000
1783#define MWL8K_CAP_GREENFIELD 0x08000000
1784#define MWL8K_CAP_AMPDU 0x04000000
1785#define MWL8K_CAP_RX_STBC 0x01000000
1786#define MWL8K_CAP_TX_STBC 0x00800000
1787#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1788#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1789#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1790#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1791#define MWL8K_CAP_DELAY_BA 0x00003000
1792#define MWL8K_CAP_MIMO 0x00000200
1793#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001794#define MWL8K_CAP_BAND_MASK 0x00000007
1795#define MWL8K_CAP_5GHZ 0x00000004
1796#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001797
Lennert Buytenhek06953232010-01-12 13:49:41 +01001798static void
1799mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1800 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001801{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001802 int rx_streams;
1803 int tx_streams;
1804
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001805 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001806
1807 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001808 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001809 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001810 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001811 if (cap & MWL8K_CAP_AMPDU) {
1812 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001813 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1814 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001815 }
1816 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001817 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001818 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001819 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001820 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001821 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001822 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001823 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001824 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001825 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001826 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001827 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001828
1829 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1830 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1831
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001832 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001833 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001834 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001835 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001836 band->ht_cap.mcs.rx_mask[2] = 0xff;
1837 band->ht_cap.mcs.rx_mask[4] = 0x01;
1838 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001839
1840 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001841 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1842 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001843 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1844 }
1845}
1846
Lennert Buytenhek06953232010-01-12 13:49:41 +01001847static void
1848mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1849{
1850 struct mwl8k_priv *priv = hw->priv;
1851
1852 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1853 mwl8k_setup_2ghz_band(hw);
1854 if (caps & MWL8K_CAP_MIMO)
1855 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1856 }
1857
1858 if (caps & MWL8K_CAP_5GHZ) {
1859 mwl8k_setup_5ghz_band(hw);
1860 if (caps & MWL8K_CAP_MIMO)
1861 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1862 }
1863}
1864
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001865static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001866{
1867 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001868 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001869 int rc;
1870 int i;
1871
1872 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1873 if (cmd == NULL)
1874 return -ENOMEM;
1875
1876 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1877 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1878
1879 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1880 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001881 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001882 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001883 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001884 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001885 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001886 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001887
1888 rc = mwl8k_post_cmd(hw, &cmd->header);
1889
1890 if (!rc) {
1891 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1892 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001893 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001894 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001895 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001896 priv->ap_macids_supported = 0x00000000;
1897 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001898 }
1899
1900 kfree(cmd);
1901 return rc;
1902}
1903
1904/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001905 * CMD_GET_HW_SPEC (AP version).
1906 */
1907struct mwl8k_cmd_get_hw_spec_ap {
1908 struct mwl8k_cmd_pkt header;
1909 __u8 hw_rev;
1910 __u8 host_interface;
1911 __le16 num_wcb;
1912 __le16 num_mcaddrs;
1913 __u8 perm_addr[ETH_ALEN];
1914 __le16 region_code;
1915 __le16 num_antenna;
1916 __le32 fw_rev;
1917 __le32 wcbbase0;
1918 __le32 rxwrptr;
1919 __le32 rxrdptr;
1920 __le32 ps_cookie;
1921 __le32 wcbbase1;
1922 __le32 wcbbase2;
1923 __le32 wcbbase3;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08001924 __le32 fw_api_version;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001925} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001926
1927static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1928{
1929 struct mwl8k_priv *priv = hw->priv;
1930 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1931 int rc;
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08001932 u32 api_version;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001933
1934 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1935 if (cmd == NULL)
1936 return -ENOMEM;
1937
1938 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1939 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1940
1941 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1942 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1943
1944 rc = mwl8k_post_cmd(hw, &cmd->header);
1945
1946 if (!rc) {
1947 int off;
1948
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08001949 api_version = le32_to_cpu(cmd->fw_api_version);
1950 if (priv->device_info->fw_api_ap != api_version) {
1951 printk(KERN_ERR "%s: Unsupported fw API version for %s."
1952 " Expected %d got %d.\n", MWL8K_NAME,
1953 priv->device_info->part_name,
1954 priv->device_info->fw_api_ap,
1955 api_version);
1956 rc = -EINVAL;
1957 goto done;
1958 }
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001959 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1960 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1961 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1962 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001963 mwl8k_setup_2ghz_band(hw);
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001964 priv->ap_macids_supported = 0x000000ff;
1965 priv->sta_macids_supported = 0x00000000;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001966
1967 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001968 iowrite32(priv->txq[0].txd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001969
1970 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001971 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001972
1973 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001974 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001975
1976 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001977 iowrite32(priv->txq[1].txd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001978
1979 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001980 iowrite32(priv->txq[2].txd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001981
1982 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001983 iowrite32(priv->txq[3].txd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001984 }
1985
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08001986done:
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001987 kfree(cmd);
1988 return rc;
1989}
1990
1991/*
1992 * CMD_SET_HW_SPEC.
1993 */
1994struct mwl8k_cmd_set_hw_spec {
1995 struct mwl8k_cmd_pkt header;
1996 __u8 hw_rev;
1997 __u8 host_interface;
1998 __le16 num_mcaddrs;
1999 __u8 perm_addr[ETH_ALEN];
2000 __le16 region_code;
2001 __le32 fw_rev;
2002 __le32 ps_cookie;
2003 __le32 caps;
2004 __le32 rx_queue_ptr;
2005 __le32 num_tx_queues;
2006 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
2007 __le32 flags;
2008 __le32 num_tx_desc_per_queue;
2009 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002010} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002011
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002012#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
2013#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
2014#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002015
2016static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2017{
2018 struct mwl8k_priv *priv = hw->priv;
2019 struct mwl8k_cmd_set_hw_spec *cmd;
2020 int rc;
2021 int i;
2022
2023 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2024 if (cmd == NULL)
2025 return -ENOMEM;
2026
2027 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2028 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2029
2030 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2031 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2032 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
2033 for (i = 0; i < MWL8K_TX_QUEUES; i++)
2034 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002035 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2036 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2037 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02002038 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2039 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2040
2041 rc = mwl8k_post_cmd(hw, &cmd->header);
2042 kfree(cmd);
2043
2044 return rc;
2045}
2046
2047/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002048 * CMD_MAC_MULTICAST_ADR.
2049 */
2050struct mwl8k_cmd_mac_multicast_adr {
2051 struct mwl8k_cmd_pkt header;
2052 __le16 action;
2053 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002054 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002055};
2056
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002057#define MWL8K_ENABLE_RX_DIRECTED 0x0001
2058#define MWL8K_ENABLE_RX_MULTICAST 0x0002
2059#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
2060#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002061
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002062static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002063__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad2010-04-01 21:22:57 +00002064 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002065{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002066 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002067 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002068 int size;
Jiri Pirko22bedad2010-04-01 21:22:57 +00002069 int mc_count = 0;
2070
2071 if (mc_list)
2072 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002073
Lennert Buytenhek447ced02009-10-22 20:19:50 +02002074 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002075 allmulti = 1;
2076 mc_count = 0;
2077 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002078
2079 size = sizeof(*cmd) + mc_count * ETH_ALEN;
2080
2081 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002082 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002083 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002084
2085 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2086 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002087 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2088 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002089
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002090 if (allmulti) {
2091 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2092 } else if (mc_count) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00002093 struct netdev_hw_addr *ha;
2094 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02002095
2096 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2097 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad2010-04-01 21:22:57 +00002098 netdev_hw_addr_list_for_each(ha, mc_list) {
2099 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002100 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002101 }
2102
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02002103 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002104}
2105
2106/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002107 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002108 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002109struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002110 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002111 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002112} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002113
2114#define MWL8K_STAT_ACK_FAILURE 9
2115#define MWL8K_STAT_RTS_FAILURE 12
2116#define MWL8K_STAT_FCS_ERROR 24
2117#define MWL8K_STAT_RTS_SUCCESS 11
2118
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002119static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2120 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002121{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002122 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002123 int rc;
2124
2125 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2126 if (cmd == NULL)
2127 return -ENOMEM;
2128
2129 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2130 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002131
2132 rc = mwl8k_post_cmd(hw, &cmd->header);
2133 if (!rc) {
2134 stats->dot11ACKFailureCount =
2135 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2136 stats->dot11RTSFailureCount =
2137 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2138 stats->dot11FCSErrorCount =
2139 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2140 stats->dot11RTSSuccessCount =
2141 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2142 }
2143 kfree(cmd);
2144
2145 return rc;
2146}
2147
2148/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002149 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002150 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002151struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002152 struct mwl8k_cmd_pkt header;
2153 __le16 action;
2154 __le16 control;
2155 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002156} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002157
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002158static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002159mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002160{
2161 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002162 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002163 int rc;
2164
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002165 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002166 return 0;
2167
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002168 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2169 if (cmd == NULL)
2170 return -ENOMEM;
2171
2172 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2173 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2174 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002175 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002176 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2177
2178 rc = mwl8k_post_cmd(hw, &cmd->header);
2179 kfree(cmd);
2180
2181 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002182 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002183
2184 return rc;
2185}
2186
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002187static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002188{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002189 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002190}
2191
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002192static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002193{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002194 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002195}
2196
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002197static int
2198mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2199{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002200 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002201
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002202 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002203
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002204 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002205}
2206
2207/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002208 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002209 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002210#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002211
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002212struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002213 struct mwl8k_cmd_pkt header;
2214 __le16 action;
2215 __le16 support_level;
2216 __le16 current_level;
2217 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002218 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002219} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002220
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002221static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002222{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002223 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002224 int rc;
2225
2226 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2227 if (cmd == NULL)
2228 return -ENOMEM;
2229
2230 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2231 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2232 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2233 cmd->support_level = cpu_to_le16(dBm);
2234
2235 rc = mwl8k_post_cmd(hw, &cmd->header);
2236 kfree(cmd);
2237
2238 return rc;
2239}
2240
2241/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002242 * CMD_TX_POWER.
2243 */
2244#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2245
2246struct mwl8k_cmd_tx_power {
2247 struct mwl8k_cmd_pkt header;
2248 __le16 action;
2249 __le16 band;
2250 __le16 channel;
2251 __le16 bw;
2252 __le16 sub_ch;
2253 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2254} __attribute__((packed));
2255
2256static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2257 struct ieee80211_conf *conf,
2258 unsigned short pwr)
2259{
2260 struct ieee80211_channel *channel = conf->channel;
2261 struct mwl8k_cmd_tx_power *cmd;
2262 int rc;
2263 int i;
2264
2265 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2266 if (cmd == NULL)
2267 return -ENOMEM;
2268
2269 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2270 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2271 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2272
2273 if (channel->band == IEEE80211_BAND_2GHZ)
2274 cmd->band = cpu_to_le16(0x1);
2275 else if (channel->band == IEEE80211_BAND_5GHZ)
2276 cmd->band = cpu_to_le16(0x4);
2277
2278 cmd->channel = channel->hw_value;
2279
2280 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2281 conf->channel_type == NL80211_CHAN_HT20) {
2282 cmd->bw = cpu_to_le16(0x2);
2283 } else {
2284 cmd->bw = cpu_to_le16(0x4);
2285 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2286 cmd->sub_ch = cpu_to_le16(0x3);
2287 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2288 cmd->sub_ch = cpu_to_le16(0x1);
2289 }
2290
2291 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2292 cmd->power_level_list[i] = cpu_to_le16(pwr);
2293
2294 rc = mwl8k_post_cmd(hw, &cmd->header);
2295 kfree(cmd);
2296
2297 return rc;
2298}
2299
2300/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002301 * CMD_RF_ANTENNA.
2302 */
2303struct mwl8k_cmd_rf_antenna {
2304 struct mwl8k_cmd_pkt header;
2305 __le16 antenna;
2306 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002307} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002308
2309#define MWL8K_RF_ANTENNA_RX 1
2310#define MWL8K_RF_ANTENNA_TX 2
2311
2312static int
2313mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2314{
2315 struct mwl8k_cmd_rf_antenna *cmd;
2316 int rc;
2317
2318 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2319 if (cmd == NULL)
2320 return -ENOMEM;
2321
2322 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2323 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2324 cmd->antenna = cpu_to_le16(antenna);
2325 cmd->mode = cpu_to_le16(mask);
2326
2327 rc = mwl8k_post_cmd(hw, &cmd->header);
2328 kfree(cmd);
2329
2330 return rc;
2331}
2332
2333/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002334 * CMD_SET_BEACON.
2335 */
2336struct mwl8k_cmd_set_beacon {
2337 struct mwl8k_cmd_pkt header;
2338 __le16 beacon_len;
2339 __u8 beacon[0];
2340};
2341
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002342static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2343 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002344{
2345 struct mwl8k_cmd_set_beacon *cmd;
2346 int rc;
2347
2348 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2349 if (cmd == NULL)
2350 return -ENOMEM;
2351
2352 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2353 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2354 cmd->beacon_len = cpu_to_le16(len);
2355 memcpy(cmd->beacon, beacon, len);
2356
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002357 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002358 kfree(cmd);
2359
2360 return rc;
2361}
2362
2363/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002364 * CMD_SET_PRE_SCAN.
2365 */
2366struct mwl8k_cmd_set_pre_scan {
2367 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002368} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002369
2370static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2371{
2372 struct mwl8k_cmd_set_pre_scan *cmd;
2373 int rc;
2374
2375 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2376 if (cmd == NULL)
2377 return -ENOMEM;
2378
2379 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2380 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2381
2382 rc = mwl8k_post_cmd(hw, &cmd->header);
2383 kfree(cmd);
2384
2385 return rc;
2386}
2387
2388/*
2389 * CMD_SET_POST_SCAN.
2390 */
2391struct mwl8k_cmd_set_post_scan {
2392 struct mwl8k_cmd_pkt header;
2393 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002394 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002395} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002396
2397static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002398mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002399{
2400 struct mwl8k_cmd_set_post_scan *cmd;
2401 int rc;
2402
2403 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2404 if (cmd == NULL)
2405 return -ENOMEM;
2406
2407 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2408 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2409 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002410 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002411
2412 rc = mwl8k_post_cmd(hw, &cmd->header);
2413 kfree(cmd);
2414
2415 return rc;
2416}
2417
2418/*
2419 * CMD_SET_RF_CHANNEL.
2420 */
2421struct mwl8k_cmd_set_rf_channel {
2422 struct mwl8k_cmd_pkt header;
2423 __le16 action;
2424 __u8 current_channel;
2425 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002426} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002427
2428static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002429 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002430{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002431 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002432 struct mwl8k_cmd_set_rf_channel *cmd;
2433 int rc;
2434
2435 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2436 if (cmd == NULL)
2437 return -ENOMEM;
2438
2439 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2440 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2441 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2442 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002443
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002444 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002445 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002446 else if (channel->band == IEEE80211_BAND_5GHZ)
2447 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002448
2449 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2450 conf->channel_type == NL80211_CHAN_HT20)
2451 cmd->channel_flags |= cpu_to_le32(0x00000080);
2452 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2453 cmd->channel_flags |= cpu_to_le32(0x000001900);
2454 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2455 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002456
2457 rc = mwl8k_post_cmd(hw, &cmd->header);
2458 kfree(cmd);
2459
2460 return rc;
2461}
2462
2463/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002464 * CMD_SET_AID.
2465 */
2466#define MWL8K_FRAME_PROT_DISABLED 0x00
2467#define MWL8K_FRAME_PROT_11G 0x07
2468#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2469#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2470
2471struct mwl8k_cmd_update_set_aid {
2472 struct mwl8k_cmd_pkt header;
2473 __le16 aid;
2474
2475 /* AP's MAC address (BSSID) */
2476 __u8 bssid[ETH_ALEN];
2477 __le16 protection_mode;
2478 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002479} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002480
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002481static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2482{
2483 int i;
2484 int j;
2485
2486 /*
2487 * Clear nonstandard rates 4 and 13.
2488 */
2489 mask &= 0x1fef;
2490
2491 for (i = 0, j = 0; i < 14; i++) {
2492 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002493 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002494 }
2495}
2496
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002497static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002498mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2499 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002500{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002501 struct mwl8k_cmd_update_set_aid *cmd;
2502 u16 prot_mode;
2503 int rc;
2504
2505 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2506 if (cmd == NULL)
2507 return -ENOMEM;
2508
2509 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2510 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002511 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002512 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002513
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002514 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002515 prot_mode = MWL8K_FRAME_PROT_11G;
2516 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002517 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002518 IEEE80211_HT_OP_MODE_PROTECTION) {
2519 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2520 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2521 break;
2522 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2523 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2524 break;
2525 default:
2526 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2527 break;
2528 }
2529 }
2530 cmd->protection_mode = cpu_to_le16(prot_mode);
2531
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002532 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002533
2534 rc = mwl8k_post_cmd(hw, &cmd->header);
2535 kfree(cmd);
2536
2537 return rc;
2538}
2539
2540/*
2541 * CMD_SET_RATE.
2542 */
2543struct mwl8k_cmd_set_rate {
2544 struct mwl8k_cmd_pkt header;
2545 __u8 legacy_rates[14];
2546
2547 /* Bitmap for supported MCS codes. */
2548 __u8 mcs_set[16];
2549 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002550} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002551
2552static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002553mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002554 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002555{
2556 struct mwl8k_cmd_set_rate *cmd;
2557 int rc;
2558
2559 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2560 if (cmd == NULL)
2561 return -ENOMEM;
2562
2563 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2564 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002565 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002566 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002567
2568 rc = mwl8k_post_cmd(hw, &cmd->header);
2569 kfree(cmd);
2570
2571 return rc;
2572}
2573
2574/*
2575 * CMD_FINALIZE_JOIN.
2576 */
2577#define MWL8K_FJ_BEACON_MAXLEN 128
2578
2579struct mwl8k_cmd_finalize_join {
2580 struct mwl8k_cmd_pkt header;
2581 __le32 sleep_interval; /* Number of beacon periods to sleep */
2582 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002583} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002584
2585static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2586 int framelen, int dtim)
2587{
2588 struct mwl8k_cmd_finalize_join *cmd;
2589 struct ieee80211_mgmt *payload = frame;
2590 int payload_len;
2591 int rc;
2592
2593 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2594 if (cmd == NULL)
2595 return -ENOMEM;
2596
2597 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2598 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2599 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2600
2601 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2602 if (payload_len < 0)
2603 payload_len = 0;
2604 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2605 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2606
2607 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2608
2609 rc = mwl8k_post_cmd(hw, &cmd->header);
2610 kfree(cmd);
2611
2612 return rc;
2613}
2614
2615/*
2616 * CMD_SET_RTS_THRESHOLD.
2617 */
2618struct mwl8k_cmd_set_rts_threshold {
2619 struct mwl8k_cmd_pkt header;
2620 __le16 action;
2621 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002622} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002623
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002624static int
2625mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002626{
2627 struct mwl8k_cmd_set_rts_threshold *cmd;
2628 int rc;
2629
2630 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2631 if (cmd == NULL)
2632 return -ENOMEM;
2633
2634 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2635 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002636 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2637 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002638
2639 rc = mwl8k_post_cmd(hw, &cmd->header);
2640 kfree(cmd);
2641
2642 return rc;
2643}
2644
2645/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002646 * CMD_SET_SLOT.
2647 */
2648struct mwl8k_cmd_set_slot {
2649 struct mwl8k_cmd_pkt header;
2650 __le16 action;
2651 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002652} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002653
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002654static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002655{
2656 struct mwl8k_cmd_set_slot *cmd;
2657 int rc;
2658
2659 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2660 if (cmd == NULL)
2661 return -ENOMEM;
2662
2663 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2664 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2665 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002666 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002667
2668 rc = mwl8k_post_cmd(hw, &cmd->header);
2669 kfree(cmd);
2670
2671 return rc;
2672}
2673
2674/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002675 * CMD_SET_EDCA_PARAMS.
2676 */
2677struct mwl8k_cmd_set_edca_params {
2678 struct mwl8k_cmd_pkt header;
2679
2680 /* See MWL8K_SET_EDCA_XXX below */
2681 __le16 action;
2682
2683 /* TX opportunity in units of 32 us */
2684 __le16 txop;
2685
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002686 union {
2687 struct {
2688 /* Log exponent of max contention period: 0...15 */
2689 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002690
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002691 /* Log exponent of min contention period: 0...15 */
2692 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002693
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002694 /* Adaptive interframe spacing in units of 32us */
2695 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002696
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002697 /* TX queue to configure */
2698 __u8 txq;
2699 } ap;
2700 struct {
2701 /* Log exponent of max contention period: 0...15 */
2702 __u8 log_cw_max;
2703
2704 /* Log exponent of min contention period: 0...15 */
2705 __u8 log_cw_min;
2706
2707 /* Adaptive interframe spacing in units of 32us */
2708 __u8 aifs;
2709
2710 /* TX queue to configure */
2711 __u8 txq;
2712 } sta;
2713 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002714} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002715
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002716#define MWL8K_SET_EDCA_CW 0x01
2717#define MWL8K_SET_EDCA_TXOP 0x02
2718#define MWL8K_SET_EDCA_AIFS 0x04
2719
2720#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2721 MWL8K_SET_EDCA_TXOP | \
2722 MWL8K_SET_EDCA_AIFS)
2723
2724static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002725mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2726 __u16 cw_min, __u16 cw_max,
2727 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002728{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002729 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002730 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002731 int rc;
2732
2733 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2734 if (cmd == NULL)
2735 return -ENOMEM;
2736
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002737 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2738 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002739 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2740 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002741 if (priv->ap_fw) {
2742 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2743 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2744 cmd->ap.aifs = aifs;
2745 cmd->ap.txq = qnum;
2746 } else {
2747 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2748 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2749 cmd->sta.aifs = aifs;
2750 cmd->sta.txq = qnum;
2751 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002752
2753 rc = mwl8k_post_cmd(hw, &cmd->header);
2754 kfree(cmd);
2755
2756 return rc;
2757}
2758
2759/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002760 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002761 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002762struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002763 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002764 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002765} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002766
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002767static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002768{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002769 struct mwl8k_priv *priv = hw->priv;
2770 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002771 int rc;
2772
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002773 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2774 if (cmd == NULL)
2775 return -ENOMEM;
2776
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002777 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002778 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002779 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002780
2781 rc = mwl8k_post_cmd(hw, &cmd->header);
2782 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002783
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002784 if (!rc)
2785 priv->wmm_enabled = enable;
2786
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002787 return rc;
2788}
2789
2790/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002791 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002792 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002793struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002794 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002795 __le32 action;
2796 __u8 rx_antenna_map;
2797 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002798} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002799
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002800static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002801{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002802 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002803 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002804
2805 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2806 if (cmd == NULL)
2807 return -ENOMEM;
2808
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002809 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002810 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002811 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2812 cmd->rx_antenna_map = rx;
2813 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002814
2815 rc = mwl8k_post_cmd(hw, &cmd->header);
2816 kfree(cmd);
2817
2818 return rc;
2819}
2820
2821/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002822 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002823 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002824struct mwl8k_cmd_use_fixed_rate_sta {
2825 struct mwl8k_cmd_pkt header;
2826 __le32 action;
2827 __le32 allow_rate_drop;
2828 __le32 num_rates;
2829 struct {
2830 __le32 is_ht_rate;
2831 __le32 enable_retry;
2832 __le32 rate;
2833 __le32 retry_count;
2834 } rate_entry[8];
2835 __le32 rate_type;
2836 __le32 reserved1;
2837 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002838} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002839
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002840#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002841#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002842
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002843static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002844{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002845 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002846 int rc;
2847
2848 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2849 if (cmd == NULL)
2850 return -ENOMEM;
2851
2852 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2853 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002854 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2855 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002856
2857 rc = mwl8k_post_cmd(hw, &cmd->header);
2858 kfree(cmd);
2859
2860 return rc;
2861}
2862
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002863/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002864 * CMD_USE_FIXED_RATE (AP version).
2865 */
2866struct mwl8k_cmd_use_fixed_rate_ap {
2867 struct mwl8k_cmd_pkt header;
2868 __le32 action;
2869 __le32 allow_rate_drop;
2870 __le32 num_rates;
2871 struct mwl8k_rate_entry_ap {
2872 __le32 is_ht_rate;
2873 __le32 enable_retry;
2874 __le32 rate;
2875 __le32 retry_count;
2876 } rate_entry[4];
2877 u8 multicast_rate;
2878 u8 multicast_rate_type;
2879 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002880} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002881
2882static int
2883mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2884{
2885 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2886 int rc;
2887
2888 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2889 if (cmd == NULL)
2890 return -ENOMEM;
2891
2892 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2893 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2894 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2895 cmd->multicast_rate = mcast;
2896 cmd->management_rate = mgmt;
2897
2898 rc = mwl8k_post_cmd(hw, &cmd->header);
2899 kfree(cmd);
2900
2901 return rc;
2902}
2903
2904/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002905 * CMD_ENABLE_SNIFFER.
2906 */
2907struct mwl8k_cmd_enable_sniffer {
2908 struct mwl8k_cmd_pkt header;
2909 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002910} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002911
2912static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2913{
2914 struct mwl8k_cmd_enable_sniffer *cmd;
2915 int rc;
2916
2917 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2918 if (cmd == NULL)
2919 return -ENOMEM;
2920
2921 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2922 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2923 cmd->action = cpu_to_le32(!!enable);
2924
2925 rc = mwl8k_post_cmd(hw, &cmd->header);
2926 kfree(cmd);
2927
2928 return rc;
2929}
2930
2931/*
2932 * CMD_SET_MAC_ADDR.
2933 */
2934struct mwl8k_cmd_set_mac_addr {
2935 struct mwl8k_cmd_pkt header;
2936 union {
2937 struct {
2938 __le16 mac_type;
2939 __u8 mac_addr[ETH_ALEN];
2940 } mbss;
2941 __u8 mac_addr[ETH_ALEN];
2942 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002943} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002944
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002945#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
2946#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
2947#define MWL8K_MAC_TYPE_PRIMARY_AP 2
2948#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002949
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002950static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
2951 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002952{
2953 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002954 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002955 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002956 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002957 int rc;
2958
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002959 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2960 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
2961 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
2962 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
2963 else
2964 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
2965 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
2966 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
2967 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2968 else
2969 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
2970 }
2971
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002972 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2973 if (cmd == NULL)
2974 return -ENOMEM;
2975
2976 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2977 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2978 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002979 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002980 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2981 } else {
2982 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2983 }
2984
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002985 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002986 kfree(cmd);
2987
2988 return rc;
2989}
2990
2991/*
2992 * CMD_SET_RATEADAPT_MODE.
2993 */
2994struct mwl8k_cmd_set_rate_adapt_mode {
2995 struct mwl8k_cmd_pkt header;
2996 __le16 action;
2997 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002998} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002999
3000static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3001{
3002 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3003 int rc;
3004
3005 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3006 if (cmd == NULL)
3007 return -ENOMEM;
3008
3009 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3010 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3011 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3012 cmd->mode = cpu_to_le16(mode);
3013
3014 rc = mwl8k_post_cmd(hw, &cmd->header);
3015 kfree(cmd);
3016
3017 return rc;
3018}
3019
3020/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003021 * CMD_BSS_START.
3022 */
3023struct mwl8k_cmd_bss_start {
3024 struct mwl8k_cmd_pkt header;
3025 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003026} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003027
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003028static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3029 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003030{
3031 struct mwl8k_cmd_bss_start *cmd;
3032 int rc;
3033
3034 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3035 if (cmd == NULL)
3036 return -ENOMEM;
3037
3038 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3039 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3040 cmd->enable = cpu_to_le32(enable);
3041
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003042 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003043 kfree(cmd);
3044
3045 return rc;
3046}
3047
3048/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003049 * CMD_SET_NEW_STN.
3050 */
3051struct mwl8k_cmd_set_new_stn {
3052 struct mwl8k_cmd_pkt header;
3053 __le16 aid;
3054 __u8 mac_addr[6];
3055 __le16 stn_id;
3056 __le16 action;
3057 __le16 rsvd;
3058 __le32 legacy_rates;
3059 __u8 ht_rates[4];
3060 __le16 cap_info;
3061 __le16 ht_capabilities_info;
3062 __u8 mac_ht_param_info;
3063 __u8 rev;
3064 __u8 control_channel;
3065 __u8 add_channel;
3066 __le16 op_mode;
3067 __le16 stbc;
3068 __u8 add_qos_info;
3069 __u8 is_qos_sta;
3070 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003071} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003072
3073#define MWL8K_STA_ACTION_ADD 0
3074#define MWL8K_STA_ACTION_REMOVE 2
3075
3076static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
3077 struct ieee80211_vif *vif,
3078 struct ieee80211_sta *sta)
3079{
3080 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003081 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003082 int rc;
3083
3084 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3085 if (cmd == NULL)
3086 return -ENOMEM;
3087
3088 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3089 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3090 cmd->aid = cpu_to_le16(sta->aid);
3091 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
3092 cmd->stn_id = cpu_to_le16(sta->aid);
3093 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003094 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3095 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3096 else
3097 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3098 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003099 if (sta->ht_cap.ht_supported) {
3100 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
3101 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
3102 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
3103 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
3104 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
3105 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
3106 ((sta->ht_cap.ampdu_density & 7) << 2);
3107 cmd->is_qos_sta = 1;
3108 }
3109
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003110 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003111 kfree(cmd);
3112
3113 return rc;
3114}
3115
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003116static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
3117 struct ieee80211_vif *vif)
3118{
3119 struct mwl8k_cmd_set_new_stn *cmd;
3120 int rc;
3121
3122 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3123 if (cmd == NULL)
3124 return -ENOMEM;
3125
3126 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3127 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3128 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3129
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003130 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003131 kfree(cmd);
3132
3133 return rc;
3134}
3135
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003136static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3137 struct ieee80211_vif *vif, u8 *addr)
3138{
3139 struct mwl8k_cmd_set_new_stn *cmd;
3140 int rc;
3141
3142 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3143 if (cmd == NULL)
3144 return -ENOMEM;
3145
3146 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3147 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3148 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3149 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3150
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003151 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003152 kfree(cmd);
3153
3154 return rc;
3155}
3156
3157/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003158 * CMD_UPDATE_STADB.
3159 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003160struct ewc_ht_info {
3161 __le16 control1;
3162 __le16 control2;
3163 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003164} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003165
3166struct peer_capability_info {
3167 /* Peer type - AP vs. STA. */
3168 __u8 peer_type;
3169
3170 /* Basic 802.11 capabilities from assoc resp. */
3171 __le16 basic_caps;
3172
3173 /* Set if peer supports 802.11n high throughput (HT). */
3174 __u8 ht_support;
3175
3176 /* Valid if HT is supported. */
3177 __le16 ht_caps;
3178 __u8 extended_ht_caps;
3179 struct ewc_ht_info ewc_info;
3180
3181 /* Legacy rate table. Intersection of our rates and peer rates. */
3182 __u8 legacy_rates[12];
3183
3184 /* HT rate table. Intersection of our rates and peer rates. */
3185 __u8 ht_rates[16];
3186 __u8 pad[16];
3187
3188 /* If set, interoperability mode, no proprietary extensions. */
3189 __u8 interop;
3190 __u8 pad2;
3191 __u8 station_id;
3192 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003193} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003194
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003195struct mwl8k_cmd_update_stadb {
3196 struct mwl8k_cmd_pkt header;
3197
3198 /* See STADB_ACTION_TYPE */
3199 __le32 action;
3200
3201 /* Peer MAC address */
3202 __u8 peer_addr[ETH_ALEN];
3203
3204 __le32 reserved;
3205
3206 /* Peer info - valid during add/update. */
3207 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003208} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003209
Lennert Buytenheka6804002010-01-04 21:55:42 +01003210#define MWL8K_STA_DB_MODIFY_ENTRY 1
3211#define MWL8K_STA_DB_DEL_ENTRY 2
3212
3213/* Peer Entry flags - used to define the type of the peer node */
3214#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3215
3216static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003217 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003218 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003219{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003220 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003221 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003222 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003223 int rc;
3224
3225 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3226 if (cmd == NULL)
3227 return -ENOMEM;
3228
3229 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3230 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003231 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003232 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003233
Lennert Buytenheka6804002010-01-04 21:55:42 +01003234 p = &cmd->peer_info;
3235 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3236 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003237 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003238 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003239 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3240 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003241 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3242 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3243 else
3244 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3245 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003246 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003247 p->interop = 1;
3248 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003249
Lennert Buytenheka6804002010-01-04 21:55:42 +01003250 rc = mwl8k_post_cmd(hw, &cmd->header);
3251 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003252
Lennert Buytenheka6804002010-01-04 21:55:42 +01003253 return rc ? rc : p->station_id;
3254}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003255
Lennert Buytenheka6804002010-01-04 21:55:42 +01003256static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3257 struct ieee80211_vif *vif, u8 *addr)
3258{
3259 struct mwl8k_cmd_update_stadb *cmd;
3260 int rc;
3261
3262 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3263 if (cmd == NULL)
3264 return -ENOMEM;
3265
3266 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3267 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3268 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3269 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3270
3271 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003272 kfree(cmd);
3273
3274 return rc;
3275}
3276
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003277
3278/*
3279 * Interrupt handling.
3280 */
3281static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3282{
3283 struct ieee80211_hw *hw = dev_id;
3284 struct mwl8k_priv *priv = hw->priv;
3285 u32 status;
3286
3287 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003288 if (!status)
3289 return IRQ_NONE;
3290
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003291 if (status & MWL8K_A2H_INT_TX_DONE) {
3292 status &= ~MWL8K_A2H_INT_TX_DONE;
3293 tasklet_schedule(&priv->poll_tx_task);
3294 }
3295
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003296 if (status & MWL8K_A2H_INT_RX_READY) {
3297 status &= ~MWL8K_A2H_INT_RX_READY;
3298 tasklet_schedule(&priv->poll_rx_task);
3299 }
3300
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003301 if (status)
3302 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003303
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003304 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003305 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003306 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003307 }
3308
3309 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003310 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003311 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003312 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003313 }
3314
3315 return IRQ_HANDLED;
3316}
3317
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003318static void mwl8k_tx_poll(unsigned long data)
3319{
3320 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3321 struct mwl8k_priv *priv = hw->priv;
3322 int limit;
3323 int i;
3324
3325 limit = 32;
3326
3327 spin_lock_bh(&priv->tx_lock);
3328
3329 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3330 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3331
3332 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3333 complete(priv->tx_wait);
3334 priv->tx_wait = NULL;
3335 }
3336
3337 spin_unlock_bh(&priv->tx_lock);
3338
3339 if (limit) {
3340 writel(~MWL8K_A2H_INT_TX_DONE,
3341 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3342 } else {
3343 tasklet_schedule(&priv->poll_tx_task);
3344 }
3345}
3346
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003347static void mwl8k_rx_poll(unsigned long data)
3348{
3349 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3350 struct mwl8k_priv *priv = hw->priv;
3351 int limit;
3352
3353 limit = 32;
3354 limit -= rxq_process(hw, 0, limit);
3355 limit -= rxq_refill(hw, 0, limit);
3356
3357 if (limit) {
3358 writel(~MWL8K_A2H_INT_RX_READY,
3359 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3360 } else {
3361 tasklet_schedule(&priv->poll_rx_task);
3362 }
3363}
3364
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003365
3366/*
3367 * Core driver operations.
3368 */
3369static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3370{
3371 struct mwl8k_priv *priv = hw->priv;
3372 int index = skb_get_queue_mapping(skb);
3373 int rc;
3374
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003375 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003376 wiphy_debug(hw->wiphy,
3377 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003378 dev_kfree_skb(skb);
3379 return NETDEV_TX_OK;
3380 }
3381
3382 rc = mwl8k_txq_xmit(hw, index, skb);
3383
3384 return rc;
3385}
3386
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003387static int mwl8k_start(struct ieee80211_hw *hw)
3388{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003389 struct mwl8k_priv *priv = hw->priv;
3390 int rc;
3391
Joe Perchesa0607fd2009-11-18 23:29:17 -08003392 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003393 IRQF_SHARED, MWL8K_NAME, hw);
3394 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07003395 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003396 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003397 }
3398
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003399 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003400 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003401 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003402
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003403 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003404 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003405
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003406 rc = mwl8k_fw_lock(hw);
3407 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003408 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003409
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003410 if (!priv->ap_fw) {
3411 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003412 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003413
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003414 if (!rc)
3415 rc = mwl8k_cmd_set_pre_scan(hw);
3416
3417 if (!rc)
3418 rc = mwl8k_cmd_set_post_scan(hw,
3419 "\x00\x00\x00\x00\x00\x00");
3420 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003421
3422 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003423 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003424
3425 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003426 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003427
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003428 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003429 }
3430
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003431 if (rc) {
3432 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3433 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003434 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003435 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003436 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003437
3438 return rc;
3439}
3440
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003441static void mwl8k_stop(struct ieee80211_hw *hw)
3442{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003443 struct mwl8k_priv *priv = hw->priv;
3444 int i;
3445
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003446 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003447
3448 ieee80211_stop_queues(hw);
3449
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003450 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003451 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003452 free_irq(priv->pdev->irq, hw);
3453
3454 /* Stop finalize join worker */
3455 cancel_work_sync(&priv->finalize_join_worker);
3456 if (priv->beacon_skb != NULL)
3457 dev_kfree_skb(priv->beacon_skb);
3458
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003459 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003460 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003461 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003462
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003463 /* Return all skbs to mac80211 */
3464 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003465 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003466}
3467
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003468static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
3469
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003470static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003471 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003472{
3473 struct mwl8k_priv *priv = hw->priv;
3474 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003475 u32 macids_supported;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003476 int macid, rc;
3477 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003478
3479 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003480 * Reject interface creation if sniffer mode is active, as
3481 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003482 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003483 */
3484 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003485 wiphy_info(hw->wiphy,
3486 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003487 return -EINVAL;
3488 }
3489
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003490 di = priv->device_info;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003491 switch (vif->type) {
3492 case NL80211_IFTYPE_AP:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003493 if (!priv->ap_fw && di->fw_image_ap) {
3494 /* we must load the ap fw to meet this request */
3495 if (!list_empty(&priv->vif_list))
3496 return -EBUSY;
3497 rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
3498 if (rc)
3499 return rc;
3500 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003501 macids_supported = priv->ap_macids_supported;
3502 break;
3503 case NL80211_IFTYPE_STATION:
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003504 if (priv->ap_fw && di->fw_image_sta) {
3505 /* we must load the sta fw to meet this request */
3506 if (!list_empty(&priv->vif_list))
3507 return -EBUSY;
3508 rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
3509 if (rc)
3510 return rc;
3511 }
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003512 macids_supported = priv->sta_macids_supported;
3513 break;
3514 default:
3515 return -EINVAL;
3516 }
3517
3518 macid = ffs(macids_supported & ~priv->macids_used);
3519 if (!macid--)
3520 return -EBUSY;
3521
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003522 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003523 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003524 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003525 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003526 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003527 mwl8k_vif->seqno = 0;
3528
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003529 /* Set the mac address. */
3530 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3531
3532 if (priv->ap_fw)
3533 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3534
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003535 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003536 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003537
3538 return 0;
3539}
3540
3541static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003542 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003543{
3544 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003545 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003546
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003547 if (priv->ap_fw)
3548 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3549
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003550 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003551
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003552 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003553 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003554}
3555
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003556static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003557{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003558 struct ieee80211_conf *conf = &hw->conf;
3559 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003560 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003561
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003562 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003563 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003564 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003565 }
3566
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003567 rc = mwl8k_fw_lock(hw);
3568 if (rc)
3569 return rc;
3570
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003571 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003572 if (rc)
3573 goto out;
3574
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003575 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003576 if (rc)
3577 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003578
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003579 if (conf->power_level > 18)
3580 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003581
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003582 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003583 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
3584 if (rc)
3585 goto out;
3586
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003587 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3588 if (!rc)
3589 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3590 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003591 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
3592 if (rc)
3593 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003594 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3595 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003596
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003597out:
3598 mwl8k_fw_unlock(hw);
3599
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003600 return rc;
3601}
3602
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003603static void
3604mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3605 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003606{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003607 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003608 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003609 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003610 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003611
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003612 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003613 return;
3614
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003615 /*
3616 * No need to capture a beacon if we're no longer associated.
3617 */
3618 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3619 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003620
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003621 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003622 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003623 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003624 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003625 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003626
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003627 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003628
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003629 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003630 if (ap == NULL) {
3631 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003632 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003633 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003634
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003635 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
3636 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
3637 } else {
3638 ap_legacy_rates =
3639 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3640 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003641 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003642
3643 rcu_read_unlock();
3644 }
3645
3646 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003647 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003648 if (rc)
3649 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003650
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003651 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003652 if (rc)
3653 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003654 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003655
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003656 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003657 rc = mwl8k_set_radio_preamble(hw,
3658 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003659 if (rc)
3660 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003661 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003662
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003663 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003664 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003665 if (rc)
3666 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003667 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003668
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003669 if (vif->bss_conf.assoc &&
3670 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
3671 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003672 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003673 if (rc)
3674 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003675 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003676
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003677 if (vif->bss_conf.assoc &&
3678 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003679 /*
3680 * Finalize the join. Tell rx handler to process
3681 * next beacon from our BSSID.
3682 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003683 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003684 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003685 }
3686
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003687out:
3688 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003689}
3690
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003691static void
3692mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3693 struct ieee80211_bss_conf *info, u32 changed)
3694{
3695 int rc;
3696
3697 if (mwl8k_fw_lock(hw))
3698 return;
3699
3700 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3701 rc = mwl8k_set_radio_preamble(hw,
3702 vif->bss_conf.use_short_preamble);
3703 if (rc)
3704 goto out;
3705 }
3706
3707 if (changed & BSS_CHANGED_BASIC_RATES) {
3708 int idx;
3709 int rate;
3710
3711 /*
3712 * Use lowest supported basic rate for multicasts
3713 * and management frames (such as probe responses --
3714 * beacons will always go out at 1 Mb/s).
3715 */
3716 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003717 if (idx)
3718 idx--;
3719
3720 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3721 rate = mwl8k_rates_24[idx].hw_value;
3722 else
3723 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003724
3725 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
3726 }
3727
3728 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
3729 struct sk_buff *skb;
3730
3731 skb = ieee80211_beacon_get(hw, vif);
3732 if (skb != NULL) {
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003733 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003734 kfree_skb(skb);
3735 }
3736 }
3737
3738 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003739 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003740
3741out:
3742 mwl8k_fw_unlock(hw);
3743}
3744
3745static void
3746mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3747 struct ieee80211_bss_conf *info, u32 changed)
3748{
3749 struct mwl8k_priv *priv = hw->priv;
3750
3751 if (!priv->ap_fw)
3752 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
3753 else
3754 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
3755}
3756
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003757static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00003758 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003759{
3760 struct mwl8k_cmd_pkt *cmd;
3761
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003762 /*
3763 * Synthesize and return a command packet that programs the
3764 * hardware multicast address filter. At this point we don't
3765 * know whether FIF_ALLMULTI is being requested, but if it is,
3766 * we'll end up throwing this packet away and creating a new
3767 * one in mwl8k_configure_filter().
3768 */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003769 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003770
3771 return (unsigned long)cmd;
3772}
3773
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003774static int
3775mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3776 unsigned int changed_flags,
3777 unsigned int *total_flags)
3778{
3779 struct mwl8k_priv *priv = hw->priv;
3780
3781 /*
3782 * Hardware sniffer mode is mutually exclusive with STA
3783 * operation, so refuse to enable sniffer mode if a STA
3784 * interface is active.
3785 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003786 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003787 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07003788 wiphy_info(hw->wiphy,
3789 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003790 return 0;
3791 }
3792
3793 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003794 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003795 return 0;
3796 priv->sniffer_enabled = true;
3797 }
3798
3799 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3800 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3801 FIF_OTHER_BSS;
3802
3803 return 1;
3804}
3805
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003806static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
3807{
3808 if (!list_empty(&priv->vif_list))
3809 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
3810
3811 return NULL;
3812}
3813
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003814static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3815 unsigned int changed_flags,
3816 unsigned int *total_flags,
3817 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003818{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003819 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003820 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3821
3822 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003823 * AP firmware doesn't allow fine-grained control over
3824 * the receive filter.
3825 */
3826 if (priv->ap_fw) {
3827 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3828 kfree(cmd);
3829 return;
3830 }
3831
3832 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003833 * Enable hardware sniffer mode if FIF_CONTROL or
3834 * FIF_OTHER_BSS is requested.
3835 */
3836 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3837 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3838 kfree(cmd);
3839 return;
3840 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003841
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003842 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003843 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003844
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003845 if (mwl8k_fw_lock(hw)) {
3846 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003847 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003848 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003849
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003850 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003851 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003852 priv->sniffer_enabled = false;
3853 }
3854
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003855 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003856 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3857 /*
3858 * Disable the BSS filter.
3859 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003860 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003861 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003862 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003863 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003864
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003865 /*
3866 * Enable the BSS filter.
3867 *
3868 * If there is an active STA interface, use that
3869 * interface's BSSID, otherwise use a dummy one
3870 * (where the OUI part needs to be nonzero for
3871 * the BSSID to be accepted by POST_SCAN).
3872 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003873 mwl8k_vif = mwl8k_first_vif(priv);
3874 if (mwl8k_vif != NULL)
3875 bssid = mwl8k_vif->vif->bss_conf.bssid;
3876 else
3877 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003878
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003879 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003880 }
3881 }
3882
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003883 /*
3884 * If FIF_ALLMULTI is being requested, throw away the command
3885 * packet that ->prepare_multicast() built and replace it with
3886 * a command packet that enables reception of all multicast
3887 * packets.
3888 */
3889 if (*total_flags & FIF_ALLMULTI) {
3890 kfree(cmd);
Jiri Pirko22bedad2010-04-01 21:22:57 +00003891 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003892 }
3893
3894 if (cmd != NULL) {
3895 mwl8k_post_cmd(hw, cmd);
3896 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003897 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003898
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003899 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003900}
3901
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003902static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3903{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003904 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003905}
3906
Johannes Berg4a6967b2010-02-19 19:18:37 +01003907static int mwl8k_sta_remove(struct ieee80211_hw *hw,
3908 struct ieee80211_vif *vif,
3909 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003910{
3911 struct mwl8k_priv *priv = hw->priv;
3912
Johannes Berg4a6967b2010-02-19 19:18:37 +01003913 if (priv->ap_fw)
3914 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
3915 else
3916 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
3917}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003918
Johannes Berg4a6967b2010-02-19 19:18:37 +01003919static int mwl8k_sta_add(struct ieee80211_hw *hw,
3920 struct ieee80211_vif *vif,
3921 struct ieee80211_sta *sta)
3922{
3923 struct mwl8k_priv *priv = hw->priv;
3924 int ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003925
Johannes Berg4a6967b2010-02-19 19:18:37 +01003926 if (!priv->ap_fw) {
3927 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
3928 if (ret >= 0) {
3929 MWL8K_STA(sta)->peer_id = ret;
3930 return 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003931 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01003932
3933 return ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003934 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003935
Johannes Berg4a6967b2010-02-19 19:18:37 +01003936 return mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003937}
3938
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003939static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3940 const struct ieee80211_tx_queue_params *params)
3941{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003942 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003943 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003944
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003945 rc = mwl8k_fw_lock(hw);
3946 if (!rc) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08003947 BUG_ON(queue > MWL8K_TX_QUEUES - 1);
3948 memcpy(&priv->wmm_params[queue], params, sizeof(*params));
3949
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003950 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003951 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003952
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003953 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003954 rc = mwl8k_cmd_set_edca_params(hw, queue,
3955 params->cw_min,
3956 params->cw_max,
3957 params->aifs,
3958 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003959
3960 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003961 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003962
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003963 return rc;
3964}
3965
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003966static int mwl8k_get_stats(struct ieee80211_hw *hw,
3967 struct ieee80211_low_level_stats *stats)
3968{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003969 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003970}
3971
John W. Linville0d462bb2010-07-28 14:04:24 -04003972static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
3973 struct survey_info *survey)
3974{
3975 struct mwl8k_priv *priv = hw->priv;
3976 struct ieee80211_conf *conf = &hw->conf;
3977
3978 if (idx != 0)
3979 return -ENOENT;
3980
3981 survey->channel = conf->channel;
3982 survey->filled = SURVEY_INFO_NOISE_DBM;
3983 survey->noise = priv->noise;
3984
3985 return 0;
3986}
3987
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003988static int
3989mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3990 enum ieee80211_ampdu_mlme_action action,
3991 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3992{
3993 switch (action) {
3994 case IEEE80211_AMPDU_RX_START:
3995 case IEEE80211_AMPDU_RX_STOP:
3996 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3997 return -ENOTSUPP;
3998 return 0;
3999 default:
4000 return -ENOTSUPP;
4001 }
4002}
4003
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004004static const struct ieee80211_ops mwl8k_ops = {
4005 .tx = mwl8k_tx,
4006 .start = mwl8k_start,
4007 .stop = mwl8k_stop,
4008 .add_interface = mwl8k_add_interface,
4009 .remove_interface = mwl8k_remove_interface,
4010 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004011 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02004012 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004013 .configure_filter = mwl8k_configure_filter,
4014 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01004015 .sta_add = mwl8k_sta_add,
4016 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004017 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004018 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04004019 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01004020 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004021};
4022
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004023static void mwl8k_finalize_join_worker(struct work_struct *work)
4024{
4025 struct mwl8k_priv *priv =
4026 container_of(work, struct mwl8k_priv, finalize_join_worker);
4027 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01004028 struct ieee80211_mgmt *mgmt = (void *)skb->data;
4029 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
4030 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
4031 mgmt->u.beacon.variable, len);
4032 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004033
Johannes Berg56007a02010-01-26 14:19:52 +01004034 if (tim && tim[1] >= 2)
4035 dtim_period = tim[3];
4036
4037 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004038
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004039 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004040 priv->beacon_skb = NULL;
4041}
4042
John W. Linvillebcb628d2009-11-06 16:40:16 -05004043enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004044 MWL8363 = 0,
4045 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004046 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02004047};
4048
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004049#define MWL8K_8366_AP_FW_API 1
4050#define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
4051#define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
4052
John W. Linvillebcb628d2009-11-06 16:40:16 -05004053static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004054 [MWL8363] = {
4055 .part_name = "88w8363",
4056 .helper_image = "mwl8k/helper_8363.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004057 .fw_image_sta = "mwl8k/fmimage_8363.fw",
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004058 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004059 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004060 .part_name = "88w8687",
4061 .helper_image = "mwl8k/helper_8687.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004062 .fw_image_sta = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05004063 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01004064 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05004065 .part_name = "88w8366",
4066 .helper_image = "mwl8k/helper_8366.fw",
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004067 .fw_image_sta = "mwl8k/fmimage_8366.fw",
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004068 .fw_image_ap = MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
4069 .fw_api_ap = MWL8K_8366_AP_FW_API,
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004070 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05004071 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004072};
4073
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004074MODULE_FIRMWARE("mwl8k/helper_8363.fw");
4075MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
4076MODULE_FIRMWARE("mwl8k/helper_8687.fw");
4077MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
4078MODULE_FIRMWARE("mwl8k/helper_8366.fw");
4079MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
Brian Cavagnolo952a0e92010-11-12 17:23:51 -08004080MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01004081
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004082static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01004083 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01004084 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
4085 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004086 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
4087 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
4088 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01004089 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05004090 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004091};
4092MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
4093
Brian Cavagnolo99020472010-11-12 17:23:52 -08004094static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
4095{
4096 int rc;
4097 printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
4098 "Trying alternative firmware %s\n", pci_name(priv->pdev),
4099 priv->fw_pref, priv->fw_alt);
4100 rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
4101 if (rc) {
4102 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4103 pci_name(priv->pdev), priv->fw_alt);
4104 return rc;
4105 }
4106 return 0;
4107}
4108
4109static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
4110static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
4111{
4112 struct mwl8k_priv *priv = context;
4113 struct mwl8k_device_info *di = priv->device_info;
4114 int rc;
4115
4116 switch (priv->fw_state) {
4117 case FW_STATE_INIT:
4118 if (!fw) {
4119 printk(KERN_ERR "%s: Error requesting helper fw %s\n",
4120 pci_name(priv->pdev), di->helper_image);
4121 goto fail;
4122 }
4123 priv->fw_helper = fw;
4124 rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
4125 true);
4126 if (rc && priv->fw_alt) {
4127 rc = mwl8k_request_alt_fw(priv);
4128 if (rc)
4129 goto fail;
4130 priv->fw_state = FW_STATE_LOADING_ALT;
4131 } else if (rc)
4132 goto fail;
4133 else
4134 priv->fw_state = FW_STATE_LOADING_PREF;
4135 break;
4136
4137 case FW_STATE_LOADING_PREF:
4138 if (!fw) {
4139 if (priv->fw_alt) {
4140 rc = mwl8k_request_alt_fw(priv);
4141 if (rc)
4142 goto fail;
4143 priv->fw_state = FW_STATE_LOADING_ALT;
4144 } else
4145 goto fail;
4146 } else {
4147 priv->fw_ucode = fw;
4148 rc = mwl8k_firmware_load_success(priv);
4149 if (rc)
4150 goto fail;
4151 else
4152 complete(&priv->firmware_loading_complete);
4153 }
4154 break;
4155
4156 case FW_STATE_LOADING_ALT:
4157 if (!fw) {
4158 printk(KERN_ERR "%s: Error requesting alt fw %s\n",
4159 pci_name(priv->pdev), di->helper_image);
4160 goto fail;
4161 }
4162 priv->fw_ucode = fw;
4163 rc = mwl8k_firmware_load_success(priv);
4164 if (rc)
4165 goto fail;
4166 else
4167 complete(&priv->firmware_loading_complete);
4168 break;
4169
4170 default:
4171 printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
4172 MWL8K_NAME, priv->fw_state);
4173 BUG_ON(1);
4174 }
4175
4176 return;
4177
4178fail:
4179 priv->fw_state = FW_STATE_ERROR;
4180 complete(&priv->firmware_loading_complete);
4181 device_release_driver(&priv->pdev->dev);
4182 mwl8k_release_firmware(priv);
4183}
4184
4185static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
4186 bool nowait)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004187{
4188 struct mwl8k_priv *priv = hw->priv;
4189 int rc;
4190
4191 /* Reset firmware and hardware */
4192 mwl8k_hw_reset(priv);
4193
4194 /* Ask userland hotplug daemon for the device firmware */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004195 rc = mwl8k_request_firmware(priv, fw_image, nowait);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004196 if (rc) {
4197 wiphy_err(hw->wiphy, "Firmware files not found\n");
4198 return rc;
4199 }
4200
Brian Cavagnolo99020472010-11-12 17:23:52 -08004201 if (nowait)
4202 return rc;
4203
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004204 /* Load firmware into hardware */
4205 rc = mwl8k_load_firmware(hw);
4206 if (rc)
4207 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4208
4209 /* Reclaim memory once firmware is successfully loaded */
4210 mwl8k_release_firmware(priv);
4211
4212 return rc;
4213}
4214
4215/* initialize hw after successfully loading a firmware image */
4216static int mwl8k_probe_hw(struct ieee80211_hw *hw)
4217{
4218 struct mwl8k_priv *priv = hw->priv;
4219 int rc = 0;
4220 int i;
4221
4222 if (priv->ap_fw) {
4223 priv->rxd_ops = priv->device_info->ap_rxd_ops;
4224 if (priv->rxd_ops == NULL) {
4225 wiphy_err(hw->wiphy,
4226 "Driver does not have AP firmware image support for this hardware\n");
4227 goto err_stop_firmware;
4228 }
4229 } else {
4230 priv->rxd_ops = &rxd_sta_ops;
4231 }
4232
4233 priv->sniffer_enabled = false;
4234 priv->wmm_enabled = false;
4235 priv->pending_tx_pkts = 0;
4236
4237 rc = mwl8k_rxq_init(hw, 0);
4238 if (rc)
4239 goto err_stop_firmware;
4240 rxq_refill(hw, 0, INT_MAX);
4241
4242 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4243 rc = mwl8k_txq_init(hw, i);
4244 if (rc)
4245 goto err_free_queues;
4246 }
4247
4248 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4249 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4250 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
4251 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
4252 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4253
4254 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4255 IRQF_SHARED, MWL8K_NAME, hw);
4256 if (rc) {
4257 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4258 goto err_free_queues;
4259 }
4260
4261 /*
4262 * Temporarily enable interrupts. Initial firmware host
4263 * commands use interrupts and avoid polling. Disable
4264 * interrupts when done.
4265 */
4266 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4267
4268 /* Get config data, mac addrs etc */
4269 if (priv->ap_fw) {
4270 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4271 if (!rc)
4272 rc = mwl8k_cmd_set_hw_spec(hw);
4273 } else {
4274 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4275 }
4276 if (rc) {
4277 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
4278 goto err_free_irq;
4279 }
4280
4281 /* Turn radio off */
4282 rc = mwl8k_cmd_radio_disable(hw);
4283 if (rc) {
4284 wiphy_err(hw->wiphy, "Cannot disable\n");
4285 goto err_free_irq;
4286 }
4287
4288 /* Clear MAC address */
4289 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
4290 if (rc) {
4291 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
4292 goto err_free_irq;
4293 }
4294
4295 /* Disable interrupts */
4296 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4297 free_irq(priv->pdev->irq, hw);
4298
4299 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
4300 priv->device_info->part_name,
4301 priv->hw_rev, hw->wiphy->perm_addr,
4302 priv->ap_fw ? "AP" : "STA",
4303 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4304 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
4305
4306 return 0;
4307
4308err_free_irq:
4309 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4310 free_irq(priv->pdev->irq, hw);
4311
4312err_free_queues:
4313 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4314 mwl8k_txq_deinit(hw, i);
4315 mwl8k_rxq_deinit(hw, 0);
4316
4317err_stop_firmware:
4318 mwl8k_hw_reset(priv);
4319
4320 return rc;
4321}
4322
4323/*
4324 * invoke mwl8k_reload_firmware to change the firmware image after the device
4325 * has already been registered
4326 */
4327static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
4328{
4329 int i, rc = 0;
4330 struct mwl8k_priv *priv = hw->priv;
4331
4332 mwl8k_stop(hw);
4333 mwl8k_rxq_deinit(hw, 0);
4334
4335 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4336 mwl8k_txq_deinit(hw, i);
4337
Brian Cavagnolo99020472010-11-12 17:23:52 -08004338 rc = mwl8k_init_firmware(hw, fw_image, false);
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004339 if (rc)
4340 goto fail;
4341
4342 rc = mwl8k_probe_hw(hw);
4343 if (rc)
4344 goto fail;
4345
4346 rc = mwl8k_start(hw);
4347 if (rc)
4348 goto fail;
4349
4350 rc = mwl8k_config(hw, ~0);
4351 if (rc)
4352 goto fail;
4353
4354 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4355 rc = mwl8k_conf_tx(hw, i, &priv->wmm_params[i]);
4356 if (rc)
4357 goto fail;
4358 }
4359
4360 return rc;
4361
4362fail:
4363 printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
4364 return rc;
4365}
4366
4367static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
4368{
4369 struct ieee80211_hw *hw = priv->hw;
4370 int i, rc;
4371
Brian Cavagnolo99020472010-11-12 17:23:52 -08004372 rc = mwl8k_load_firmware(hw);
4373 mwl8k_release_firmware(priv);
4374 if (rc) {
4375 wiphy_err(hw->wiphy, "Cannot start firmware\n");
4376 return rc;
4377 }
4378
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004379 /*
4380 * Extra headroom is the size of the required DMA header
4381 * minus the size of the smallest 802.11 frame (CTS frame).
4382 */
4383 hw->extra_tx_headroom =
4384 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
4385
4386 hw->channel_change_time = 10;
4387
4388 hw->queues = MWL8K_TX_QUEUES;
4389
4390 /* Set rssi values to dBm */
4391 hw->flags |= IEEE80211_HW_SIGNAL_DBM;
4392 hw->vif_data_size = sizeof(struct mwl8k_vif);
4393 hw->sta_data_size = sizeof(struct mwl8k_sta);
4394
4395 priv->macids_used = 0;
4396 INIT_LIST_HEAD(&priv->vif_list);
4397
4398 /* Set default radio state and preamble */
4399 priv->radio_on = 0;
4400 priv->radio_short_preamble = 0;
4401
4402 /* Finalize join worker */
4403 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4404
4405 /* TX reclaim and RX tasklets. */
4406 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4407 tasklet_disable(&priv->poll_tx_task);
4408 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4409 tasklet_disable(&priv->poll_rx_task);
4410
4411 /* Power management cookie */
4412 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4413 if (priv->cookie == NULL)
4414 return -ENOMEM;
4415
4416 mutex_init(&priv->fw_mutex);
4417 priv->fw_mutex_owner = NULL;
4418 priv->fw_mutex_depth = 0;
4419 priv->hostcmd_wait = NULL;
4420
4421 spin_lock_init(&priv->tx_lock);
4422
4423 priv->tx_wait = NULL;
4424
4425 rc = mwl8k_probe_hw(hw);
4426 if (rc)
4427 goto err_free_cookie;
4428
4429 hw->wiphy->interface_modes = 0;
4430 if (priv->ap_macids_supported || priv->device_info->fw_image_ap)
4431 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4432 if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
4433 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4434
4435 rc = ieee80211_register_hw(hw);
4436 if (rc) {
4437 wiphy_err(hw->wiphy, "Cannot register device\n");
4438 goto err_unprobe_hw;
4439 }
4440
4441 return 0;
4442
4443err_unprobe_hw:
4444 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4445 mwl8k_txq_deinit(hw, i);
4446 mwl8k_rxq_deinit(hw, 0);
4447
4448err_free_cookie:
4449 if (priv->cookie != NULL)
4450 pci_free_consistent(priv->pdev, 4,
4451 priv->cookie, priv->cookie_dma);
4452
4453 return rc;
4454}
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004455static int __devinit mwl8k_probe(struct pci_dev *pdev,
4456 const struct pci_device_id *id)
4457{
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004458 static int printed_version;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004459 struct ieee80211_hw *hw;
4460 struct mwl8k_priv *priv;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004461 struct mwl8k_device_info *di;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004462 int rc;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004463
4464 if (!printed_version) {
4465 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
4466 printed_version = 1;
4467 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004468
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004469
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004470 rc = pci_enable_device(pdev);
4471 if (rc) {
4472 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
4473 MWL8K_NAME);
4474 return rc;
4475 }
4476
4477 rc = pci_request_regions(pdev, MWL8K_NAME);
4478 if (rc) {
4479 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
4480 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004481 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004482 }
4483
4484 pci_set_master(pdev);
4485
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004486
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004487 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
4488 if (hw == NULL) {
4489 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
4490 rc = -ENOMEM;
4491 goto err_free_reg;
4492 }
4493
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004494 SET_IEEE80211_DEV(hw, &pdev->dev);
4495 pci_set_drvdata(pdev, hw);
4496
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004497 priv = hw->priv;
4498 priv->hw = hw;
4499 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05004500 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004501
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004502
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004503 priv->sram = pci_iomap(pdev, 0, 0x10000);
4504 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004505 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004506 goto err_iounmap;
4507 }
4508
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004509 /*
4510 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
4511 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
4512 */
4513 priv->regs = pci_iomap(pdev, 1, 0x10000);
4514 if (priv->regs == NULL) {
4515 priv->regs = pci_iomap(pdev, 2, 0x10000);
4516 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004517 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004518 goto err_iounmap;
4519 }
4520 }
4521
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004522 /*
Brian Cavagnolo99020472010-11-12 17:23:52 -08004523 * Choose the initial fw image depending on user input. If a second
4524 * image is available, make it the alternative image that will be
4525 * loaded if the first one fails.
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004526 */
Brian Cavagnolo99020472010-11-12 17:23:52 -08004527 init_completion(&priv->firmware_loading_complete);
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004528 di = priv->device_info;
Brian Cavagnolo99020472010-11-12 17:23:52 -08004529 if (ap_mode_default && di->fw_image_ap) {
4530 priv->fw_pref = di->fw_image_ap;
4531 priv->fw_alt = di->fw_image_sta;
4532 } else if (!ap_mode_default && di->fw_image_sta) {
4533 priv->fw_pref = di->fw_image_sta;
4534 priv->fw_alt = di->fw_image_ap;
4535 } else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004536 printk(KERN_WARNING "AP fw is unavailable. Using STA fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08004537 priv->fw_pref = di->fw_image_sta;
Brian Cavagnolo0863ade2010-11-12 17:23:50 -08004538 } else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
4539 printk(KERN_WARNING "STA fw is unavailable. Using AP fw.");
Brian Cavagnolo99020472010-11-12 17:23:52 -08004540 priv->fw_pref = di->fw_image_ap;
4541 }
4542 rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004543 if (rc)
Brian Cavagnolo3cc77722010-11-12 17:23:49 -08004544 goto err_stop_firmware;
Brian Cavagnolo99020472010-11-12 17:23:52 -08004545 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004546
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004547err_stop_firmware:
4548 mwl8k_hw_reset(priv);
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004549
4550err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004551 if (priv->regs != NULL)
4552 pci_iounmap(pdev, priv->regs);
4553
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004554 if (priv->sram != NULL)
4555 pci_iounmap(pdev, priv->sram);
4556
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004557 pci_set_drvdata(pdev, NULL);
4558 ieee80211_free_hw(hw);
4559
4560err_free_reg:
4561 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004562
4563err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004564 pci_disable_device(pdev);
4565
4566 return rc;
4567}
4568
Joerg Albert230f7af2009-04-18 02:10:45 +02004569static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004570{
4571 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4572}
4573
Joerg Albert230f7af2009-04-18 02:10:45 +02004574static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004575{
4576 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4577 struct mwl8k_priv *priv;
4578 int i;
4579
4580 if (hw == NULL)
4581 return;
4582 priv = hw->priv;
4583
Brian Cavagnolo99020472010-11-12 17:23:52 -08004584 wait_for_completion(&priv->firmware_loading_complete);
4585
4586 if (priv->fw_state == FW_STATE_ERROR) {
4587 mwl8k_hw_reset(priv);
4588 goto unmap;
4589 }
4590
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004591 ieee80211_stop_queues(hw);
4592
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004593 ieee80211_unregister_hw(hw);
4594
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004595 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004596 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004597 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004598
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004599 /* Stop hardware */
4600 mwl8k_hw_reset(priv);
4601
4602 /* Return all skbs to mac80211 */
4603 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004604 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004605
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004606 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4607 mwl8k_txq_deinit(hw, i);
4608
4609 mwl8k_rxq_deinit(hw, 0);
4610
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004611 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004612
Brian Cavagnolo99020472010-11-12 17:23:52 -08004613unmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004614 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004615 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004616 pci_set_drvdata(pdev, NULL);
4617 ieee80211_free_hw(hw);
4618 pci_release_regions(pdev);
4619 pci_disable_device(pdev);
4620}
4621
4622static struct pci_driver mwl8k_driver = {
4623 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004624 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004625 .probe = mwl8k_probe,
4626 .remove = __devexit_p(mwl8k_remove),
4627 .shutdown = __devexit_p(mwl8k_shutdown),
4628};
4629
4630static int __init mwl8k_init(void)
4631{
4632 return pci_register_driver(&mwl8k_driver);
4633}
4634
4635static void __exit mwl8k_exit(void)
4636{
4637 pci_unregister_driver(&mwl8k_driver);
4638}
4639
4640module_init(mwl8k_init);
4641module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004642
4643MODULE_DESCRIPTION(MWL8K_DESC);
4644MODULE_VERSION(MWL8K_VERSION);
4645MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
4646MODULE_LICENSE("GPL");