blob: cfda87a595e3dbd26de37f6864fdf63fe9d91c7d [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
Lennert Buytenheka66098d2009-03-10 10:13:33 +010032/* Register definitions */
33#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020034#define MWL8K_MODE_STA 0x0000005a
35#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010036#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020037#define MWL8K_FWSTA_READY 0xf0f1f2f4
38#define MWL8K_FWAP_READY 0xf1f2f4a5
39#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010040#define MWL8K_HIU_SCRATCH 0x00000c40
41
42/* Host->device communications */
43#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
44#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
45#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
46#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
47#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020048#define MWL8K_H2A_INT_DUMMY (1 << 20)
49#define MWL8K_H2A_INT_RESET (1 << 15)
50#define MWL8K_H2A_INT_DOORBELL (1 << 1)
51#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010052
53/* Device->host communications */
54#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
55#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
56#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
57#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
58#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020059#define MWL8K_A2H_INT_DUMMY (1 << 20)
60#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
61#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
62#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
63#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
64#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
65#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
66#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
67#define MWL8K_A2H_INT_RX_READY (1 << 1)
68#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010069
70#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
71 MWL8K_A2H_INT_CHNL_SWITCHED | \
72 MWL8K_A2H_INT_QUEUE_EMPTY | \
73 MWL8K_A2H_INT_RADAR_DETECT | \
74 MWL8K_A2H_INT_RADIO_ON | \
75 MWL8K_A2H_INT_RADIO_OFF | \
76 MWL8K_A2H_INT_MAC_EVENT | \
77 MWL8K_A2H_INT_OPC_DONE | \
78 MWL8K_A2H_INT_RX_READY | \
79 MWL8K_A2H_INT_TX_DONE)
80
Lennert Buytenheka66098d2009-03-10 10:13:33 +010081#define MWL8K_RX_QUEUES 1
82#define MWL8K_TX_QUEUES 4
83
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020084struct rxd_ops {
85 int rxd_size;
86 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
87 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010088 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -040089 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020090};
91
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020092struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020093 char *part_name;
94 char *helper_image;
95 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010096 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020097};
98
Lennert Buytenheka66098d2009-03-10 10:13:33 +010099struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200100 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100101
102 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200103 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100104
105 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200106 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100107
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200108 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200109 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200110 struct {
111 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900112 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200113 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100114};
115
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100116struct mwl8k_tx_queue {
117 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200118 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100119
120 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200121 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100122
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200123 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200124 struct mwl8k_tx_desc *txd;
125 dma_addr_t txd_dma;
126 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100127};
128
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100132
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200133 struct mwl8k_device_info *device_info;
134
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100135 void __iomem *sram;
136 void __iomem *regs;
137
138 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100139 struct firmware *fw_helper;
140 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100141
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100142 /* hardware/firmware parameters */
143 bool ap_fw;
144 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100145 struct ieee80211_supported_band band_24;
146 struct ieee80211_channel channels_24[14];
147 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100148 struct ieee80211_supported_band band_50;
149 struct ieee80211_channel channels_50[4];
150 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100151 u32 ap_macids_supported;
152 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100153
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200154 /* firmware access */
155 struct mutex fw_mutex;
156 struct task_struct *fw_mutex_owner;
157 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200158 struct completion *hostcmd_wait;
159
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100160 /* lock held over TX and TX reap */
161 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100162
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200163 /* TX quiesce completion, protected by fw_mutex and tx_lock */
164 struct completion *tx_wait;
165
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100166 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100167 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100168 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100169
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100170 /* power management status cookie from firmware */
171 u32 *cookie;
172 dma_addr_t cookie_dma;
173
174 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100175 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200176 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177
178 /*
179 * Running count of TX packets in flight, to avoid
180 * iterating over the transmit rings each time.
181 */
182 int pending_tx_pkts;
183
184 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
185 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
186
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200187 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200188 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200189 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200190 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100191
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100192 /* XXX need to convert this to handle multiple interfaces */
193 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200194 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 struct sk_buff *beacon_skb;
196
197 /*
198 * This FJ worker has to be global as it is scheduled from the
199 * RX handler. At this point we don't know which interface it
200 * belongs to until the list of bssids waiting to complete join
201 * is checked.
202 */
203 struct work_struct finalize_join_worker;
204
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100205 /* Tasklet to perform TX reclaim. */
206 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100207
208 /* Tasklet to perform RX. */
209 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400210
211 /* Most recently reported noise in dBm */
212 s8 noise;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100213};
214
215/* Per interface specific private data */
216struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100217 struct list_head list;
218 struct ieee80211_vif *vif;
219
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100220 /* Firmware macid for this vif. */
221 int macid;
222
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100223 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100224 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200226#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100227
Lennert Buytenheka6804002010-01-04 21:55:42 +0100228struct mwl8k_sta {
229 /* Index into station database. Returned by UPDATE_STADB. */
230 u8 peer_id;
231};
232#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
233
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100234static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100235 { .center_freq = 2412, .hw_value = 1, },
236 { .center_freq = 2417, .hw_value = 2, },
237 { .center_freq = 2422, .hw_value = 3, },
238 { .center_freq = 2427, .hw_value = 4, },
239 { .center_freq = 2432, .hw_value = 5, },
240 { .center_freq = 2437, .hw_value = 6, },
241 { .center_freq = 2442, .hw_value = 7, },
242 { .center_freq = 2447, .hw_value = 8, },
243 { .center_freq = 2452, .hw_value = 9, },
244 { .center_freq = 2457, .hw_value = 10, },
245 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100246 { .center_freq = 2467, .hw_value = 12, },
247 { .center_freq = 2472, .hw_value = 13, },
248 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100249};
250
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100251static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100252 { .bitrate = 10, .hw_value = 2, },
253 { .bitrate = 20, .hw_value = 4, },
254 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200255 { .bitrate = 110, .hw_value = 22, },
256 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100257 { .bitrate = 60, .hw_value = 12, },
258 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100259 { .bitrate = 120, .hw_value = 24, },
260 { .bitrate = 180, .hw_value = 36, },
261 { .bitrate = 240, .hw_value = 48, },
262 { .bitrate = 360, .hw_value = 72, },
263 { .bitrate = 480, .hw_value = 96, },
264 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100265 { .bitrate = 720, .hw_value = 144, },
266};
267
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100268static const struct ieee80211_channel mwl8k_channels_50[] = {
269 { .center_freq = 5180, .hw_value = 36, },
270 { .center_freq = 5200, .hw_value = 40, },
271 { .center_freq = 5220, .hw_value = 44, },
272 { .center_freq = 5240, .hw_value = 48, },
273};
274
275static const struct ieee80211_rate mwl8k_rates_50[] = {
276 { .bitrate = 60, .hw_value = 12, },
277 { .bitrate = 90, .hw_value = 18, },
278 { .bitrate = 120, .hw_value = 24, },
279 { .bitrate = 180, .hw_value = 36, },
280 { .bitrate = 240, .hw_value = 48, },
281 { .bitrate = 360, .hw_value = 72, },
282 { .bitrate = 480, .hw_value = 96, },
283 { .bitrate = 540, .hw_value = 108, },
284 { .bitrate = 720, .hw_value = 144, },
285};
286
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100287/* Set or get info from Firmware */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100288#define MWL8K_CMD_GET 0x0000
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800289#define MWL8K_CMD_SET 0x0001
290#define MWL8K_CMD_SET_LIST 0x0002
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100291
292/* Firmware command codes */
293#define MWL8K_CMD_CODE_DNLD 0x0001
294#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200295#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100296#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
297#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200298#define MWL8K_CMD_RADIO_CONTROL 0x001c
299#define MWL8K_CMD_RF_TX_POWER 0x001e
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800300#define MWL8K_CMD_TX_POWER 0x001f
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200301#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100302#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100303#define MWL8K_CMD_SET_PRE_SCAN 0x0107
304#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200305#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100306#define MWL8K_CMD_SET_AID 0x010d
307#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200308#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100309#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200310#define MWL8K_CMD_SET_SLOT 0x0114
311#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
312#define MWL8K_CMD_SET_WMM_MODE 0x0123
313#define MWL8K_CMD_MIMO_CONFIG 0x0125
314#define MWL8K_CMD_USE_FIXED_RATE 0x0126
315#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100316#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200317#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +0100318#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
319#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200320#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100321
John W. Linvilleb6037422010-07-20 13:55:00 -0400322static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100323{
John W. Linvilleb6037422010-07-20 13:55:00 -0400324 u16 command = le16_to_cpu(cmd);
325
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100326#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
327 snprintf(buf, bufsize, "%s", #x);\
328 return buf;\
329 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400330 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100331 MWL8K_CMDNAME(CODE_DNLD);
332 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +0200333 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100334 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
335 MWL8K_CMDNAME(GET_STAT);
336 MWL8K_CMDNAME(RADIO_CONTROL);
337 MWL8K_CMDNAME(RF_TX_POWER);
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -0800338 MWL8K_CMDNAME(TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200339 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100340 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100341 MWL8K_CMDNAME(SET_PRE_SCAN);
342 MWL8K_CMDNAME(SET_POST_SCAN);
343 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100344 MWL8K_CMDNAME(SET_AID);
345 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200346 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100347 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200348 MWL8K_CMDNAME(SET_SLOT);
349 MWL8K_CMDNAME(SET_EDCA_PARAMS);
350 MWL8K_CMDNAME(SET_WMM_MODE);
351 MWL8K_CMDNAME(MIMO_CONFIG);
352 MWL8K_CMDNAME(USE_FIXED_RATE);
353 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200354 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200355 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100356 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100357 MWL8K_CMDNAME(SET_NEW_STN);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200358 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100359 default:
360 snprintf(buf, bufsize, "0x%x", cmd);
361 }
362#undef MWL8K_CMDNAME
363
364 return buf;
365}
366
367/* Hardware and firmware reset */
368static void mwl8k_hw_reset(struct mwl8k_priv *priv)
369{
370 iowrite32(MWL8K_H2A_INT_RESET,
371 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
372 iowrite32(MWL8K_H2A_INT_RESET,
373 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
374 msleep(20);
375}
376
377/* Release fw image */
378static void mwl8k_release_fw(struct firmware **fw)
379{
380 if (*fw == NULL)
381 return;
382 release_firmware(*fw);
383 *fw = NULL;
384}
385
386static void mwl8k_release_firmware(struct mwl8k_priv *priv)
387{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100388 mwl8k_release_fw(&priv->fw_ucode);
389 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100390}
391
392/* Request fw image */
393static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200394 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100395{
396 /* release current image */
397 if (*fw != NULL)
398 mwl8k_release_fw(fw);
399
400 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200401 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100402}
403
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200404static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100405{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200406 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100407 int rc;
408
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200409 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100410 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200411 if (rc) {
412 printk(KERN_ERR "%s: Error requesting helper "
413 "firmware file %s\n", pci_name(priv->pdev),
414 di->helper_image);
415 return rc;
416 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100417 }
418
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100419 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100420 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200421 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200422 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100423 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100424 return rc;
425 }
426
427 return 0;
428}
429
430struct mwl8k_cmd_pkt {
431 __le16 code;
432 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100433 __u8 seq_num;
434 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100435 __le16 result;
436 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000437} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100438
439/*
440 * Firmware loading.
441 */
442static int
443mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
444{
445 void __iomem *regs = priv->regs;
446 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100447 int loops;
448
449 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
450 if (pci_dma_mapping_error(priv->pdev, dma_addr))
451 return -ENOMEM;
452
453 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
454 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
455 iowrite32(MWL8K_H2A_INT_DOORBELL,
456 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
457 iowrite32(MWL8K_H2A_INT_DUMMY,
458 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
459
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100460 loops = 1000;
461 do {
462 u32 int_code;
463
464 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
465 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
466 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100467 break;
468 }
469
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200470 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100471 udelay(1);
472 } while (--loops);
473
474 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
475
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200476 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100477}
478
479static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
480 const u8 *data, size_t length)
481{
482 struct mwl8k_cmd_pkt *cmd;
483 int done;
484 int rc = 0;
485
486 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
487 if (cmd == NULL)
488 return -ENOMEM;
489
490 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
491 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100492 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100493 cmd->result = 0;
494
495 done = 0;
496 while (length) {
497 int block_size = length > 256 ? 256 : length;
498
499 memcpy(cmd->payload, data + done, block_size);
500 cmd->length = cpu_to_le16(block_size);
501
502 rc = mwl8k_send_fw_load_cmd(priv, cmd,
503 sizeof(*cmd) + block_size);
504 if (rc)
505 break;
506
507 done += block_size;
508 length -= block_size;
509 }
510
511 if (!rc) {
512 cmd->length = 0;
513 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
514 }
515
516 kfree(cmd);
517
518 return rc;
519}
520
521static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
522 const u8 *data, size_t length)
523{
524 unsigned char *buffer;
525 int may_continue, rc = 0;
526 u32 done, prev_block_size;
527
528 buffer = kmalloc(1024, GFP_KERNEL);
529 if (buffer == NULL)
530 return -ENOMEM;
531
532 done = 0;
533 prev_block_size = 0;
534 may_continue = 1000;
535 while (may_continue > 0) {
536 u32 block_size;
537
538 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
539 if (block_size & 1) {
540 block_size &= ~1;
541 may_continue--;
542 } else {
543 done += prev_block_size;
544 length -= prev_block_size;
545 }
546
547 if (block_size > 1024 || block_size > length) {
548 rc = -EOVERFLOW;
549 break;
550 }
551
552 if (length == 0) {
553 rc = 0;
554 break;
555 }
556
557 if (block_size == 0) {
558 rc = -EPROTO;
559 may_continue--;
560 udelay(1);
561 continue;
562 }
563
564 prev_block_size = block_size;
565 memcpy(buffer, data + done, block_size);
566
567 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
568 if (rc)
569 break;
570 }
571
572 if (!rc && length != 0)
573 rc = -EREMOTEIO;
574
575 kfree(buffer);
576
577 return rc;
578}
579
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200580static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100581{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200582 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100583 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200584 int rc;
585 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100586
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200587 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100588 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100589
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200590 if (helper == NULL) {
591 printk(KERN_ERR "%s: helper image needed but none "
592 "given\n", pci_name(priv->pdev));
593 return -EINVAL;
594 }
595
596 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100597 if (rc) {
598 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200599 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100600 return rc;
601 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100602 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100603
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200604 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100605 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200606 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100607 }
608
609 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200610 printk(KERN_ERR "%s: unable to load firmware image\n",
611 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100612 return rc;
613 }
614
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100615 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100616
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100617 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100618 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200619 u32 ready_code;
620
621 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
622 if (ready_code == MWL8K_FWAP_READY) {
623 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100624 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200625 } else if (ready_code == MWL8K_FWSTA_READY) {
626 priv->ap_fw = 0;
627 break;
628 }
629
630 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100631 udelay(1);
632 } while (--loops);
633
634 return loops ? 0 : -ETIMEDOUT;
635}
636
637
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100638/* DMA header used by firmware and hardware. */
639struct mwl8k_dma_data {
640 __le16 fwlen;
641 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100642 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000643} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100644
645/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100646static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100647{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100648 struct mwl8k_dma_data *tr;
649 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100650
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100651 tr = (struct mwl8k_dma_data *)skb->data;
652 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
653
654 if (hdrlen != sizeof(tr->wh)) {
655 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
656 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
657 *((__le16 *)(tr->data - 2)) = qos;
658 } else {
659 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
660 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100661 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100662
663 if (hdrlen != sizeof(*tr))
664 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100665}
666
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200667static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100668{
669 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100670 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100671 struct mwl8k_dma_data *tr;
672
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100673 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100674 * Add a firmware DMA header; the firmware requires that we
675 * present a 2-byte payload length followed by a 4-address
676 * header (without QoS field), followed (optionally) by any
677 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100678 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100679 wh = (struct ieee80211_hdr *)skb->data;
680
681 hdrlen = ieee80211_hdrlen(wh->frame_control);
682 if (hdrlen != sizeof(*tr))
683 skb_push(skb, sizeof(*tr) - hdrlen);
684
685 if (ieee80211_is_data_qos(wh->frame_control))
686 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100687
688 tr = (struct mwl8k_dma_data *)skb->data;
689 if (wh != &tr->wh)
690 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100691 if (hdrlen != sizeof(tr->wh))
692 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100693
694 /*
695 * Firmware length is the length of the fully formed "802.11
696 * payload". That is, everything except for the 802.11 header.
697 * This includes all crypto material including the MIC.
698 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100699 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100700}
701
702
703/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100704 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200705 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100706struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200707 __le16 pkt_len;
708 __u8 sq2;
709 __u8 rate;
710 __le32 pkt_phys_addr;
711 __le32 next_rxd_phys_addr;
712 __le16 qos_control;
713 __le16 htsig2;
714 __le32 hw_rssi_info;
715 __le32 hw_noise_floor_info;
716 __u8 noise_floor;
717 __u8 pad0[3];
718 __u8 rssi;
719 __u8 rx_status;
720 __u8 channel;
721 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000722} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200723
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100724#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
725#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
726#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100727
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100728#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200729
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100730static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200731{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100732 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200733
734 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100735 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200736}
737
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100738static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200739{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100740 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200741
742 rxd->pkt_len = cpu_to_le16(len);
743 rxd->pkt_phys_addr = cpu_to_le32(addr);
744 wmb();
745 rxd->rx_ctrl = 0;
746}
747
748static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100749mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400750 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200751{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100752 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200753
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100754 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200755 return -1;
756 rmb();
757
758 memset(status, 0, sizeof(*status));
759
760 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400761 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200762
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100763 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200764 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100765 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100766 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100767 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200768 } else {
769 int i;
770
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100771 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
772 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200773 status->rate_idx = i;
774 break;
775 }
776 }
777 }
778
Lennert Buytenhek85478342010-01-12 13:48:56 +0100779 if (rxd->channel > 14) {
780 status->band = IEEE80211_BAND_5GHZ;
781 if (!(status->flag & RX_FLAG_HT))
782 status->rate_idx -= 5;
783 } else {
784 status->band = IEEE80211_BAND_2GHZ;
785 }
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200786 status->freq = ieee80211_channel_to_frequency(rxd->channel);
787
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100788 *qos = rxd->qos_control;
789
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200790 return le16_to_cpu(rxd->pkt_len);
791}
792
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100793static struct rxd_ops rxd_8366_ap_ops = {
794 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
795 .rxd_init = mwl8k_rxd_8366_ap_init,
796 .rxd_refill = mwl8k_rxd_8366_ap_refill,
797 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200798};
799
800/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100801 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100802 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100803struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100804 __le16 pkt_len;
805 __u8 link_quality;
806 __u8 noise_level;
807 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200808 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100809 __le16 qos_control;
810 __le16 rate_info;
811 __le32 pad0[4];
812 __u8 rssi;
813 __u8 channel;
814 __le16 pad1;
815 __u8 rx_ctrl;
816 __u8 rx_status;
817 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000818} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100819
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100820#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
821#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
822#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
823#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
824#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
825#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200826
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100827#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200828
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100829static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200830{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100831 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200832
833 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100834 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200835}
836
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100837static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200838{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100839 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200840
841 rxd->pkt_len = cpu_to_le16(len);
842 rxd->pkt_phys_addr = cpu_to_le32(addr);
843 wmb();
844 rxd->rx_ctrl = 0;
845}
846
847static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100848mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400849 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200850{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100851 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200852 u16 rate_info;
853
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100854 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200855 return -1;
856 rmb();
857
858 rate_info = le16_to_cpu(rxd->rate_info);
859
860 memset(status, 0, sizeof(*status));
861
862 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400863 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100864 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
865 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200866
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100867 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200868 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100869 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200870 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100871 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200872 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100873 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200874 status->flag |= RX_FLAG_HT;
875
Lennert Buytenhek85478342010-01-12 13:48:56 +0100876 if (rxd->channel > 14) {
877 status->band = IEEE80211_BAND_5GHZ;
878 if (!(status->flag & RX_FLAG_HT))
879 status->rate_idx -= 5;
880 } else {
881 status->band = IEEE80211_BAND_2GHZ;
882 }
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200883 status->freq = ieee80211_channel_to_frequency(rxd->channel);
884
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100885 *qos = rxd->qos_control;
886
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200887 return le16_to_cpu(rxd->pkt_len);
888}
889
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100890static struct rxd_ops rxd_sta_ops = {
891 .rxd_size = sizeof(struct mwl8k_rxd_sta),
892 .rxd_init = mwl8k_rxd_sta_init,
893 .rxd_refill = mwl8k_rxd_sta_refill,
894 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200895};
896
897
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100898#define MWL8K_RX_DESCS 256
899#define MWL8K_RX_MAXSZ 3800
900
901static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
902{
903 struct mwl8k_priv *priv = hw->priv;
904 struct mwl8k_rx_queue *rxq = priv->rxq + index;
905 int size;
906 int i;
907
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200908 rxq->rxd_count = 0;
909 rxq->head = 0;
910 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100911
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200912 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100913
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200914 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
915 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -0700916 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100917 return -ENOMEM;
918 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200919 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100920
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200921 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
922 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -0700923 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200924 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100925 return -ENOMEM;
926 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200927 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100928
929 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200930 int desc_size;
931 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100932 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200933 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100934
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200935 desc_size = priv->rxd_ops->rxd_size;
936 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100937
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200938 nexti = i + 1;
939 if (nexti == MWL8K_RX_DESCS)
940 nexti = 0;
941 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
942
943 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100944 }
945
946 return 0;
947}
948
949static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
950{
951 struct mwl8k_priv *priv = hw->priv;
952 struct mwl8k_rx_queue *rxq = priv->rxq + index;
953 int refilled;
954
955 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200956 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100957 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200958 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100959 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200960 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100961
962 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
963 if (skb == NULL)
964 break;
965
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200966 addr = pci_map_single(priv->pdev, skb->data,
967 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100968
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200969 rxq->rxd_count++;
970 rx = rxq->tail++;
971 if (rxq->tail == MWL8K_RX_DESCS)
972 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200973 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900974 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200975
976 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
977 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100978
979 refilled++;
980 }
981
982 return refilled;
983}
984
985/* Must be called only when the card's reception is completely halted */
986static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
987{
988 struct mwl8k_priv *priv = hw->priv;
989 struct mwl8k_rx_queue *rxq = priv->rxq + index;
990 int i;
991
992 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200993 if (rxq->buf[i].skb != NULL) {
994 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900995 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200996 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900997 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100998
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200999 kfree_skb(rxq->buf[i].skb);
1000 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001001 }
1002 }
1003
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001004 kfree(rxq->buf);
1005 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001006
1007 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001008 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001009 rxq->rxd, rxq->rxd_dma);
1010 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001011}
1012
1013
1014/*
1015 * Scan a list of BSSIDs to process for finalize join.
1016 * Allows for extension to process multiple BSSIDs.
1017 */
1018static inline int
1019mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1020{
1021 return priv->capture_beacon &&
1022 ieee80211_is_beacon(wh->frame_control) &&
1023 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1024}
1025
Lennert Buytenhek37797522009-10-22 20:19:45 +02001026static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1027 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001028{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001029 struct mwl8k_priv *priv = hw->priv;
1030
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001031 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001032 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001033
1034 /*
1035 * Use GFP_ATOMIC as rxq_process is called from
1036 * the primary interrupt handler, memory allocation call
1037 * must not sleep.
1038 */
1039 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1040 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001041 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001042}
1043
1044static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1045{
1046 struct mwl8k_priv *priv = hw->priv;
1047 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1048 int processed;
1049
1050 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001051 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001052 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001053 void *rxd;
1054 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001055 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001056 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001057
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001058 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001059 if (skb == NULL)
1060 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001061
1062 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1063
John W. Linville0d462bb2010-07-28 14:04:24 -04001064 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1065 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001066 if (pkt_len < 0)
1067 break;
1068
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001069 rxq->buf[rxq->head].skb = NULL;
1070
1071 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001072 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001073 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001074 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001075
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001076 rxq->head++;
1077 if (rxq->head == MWL8K_RX_DESCS)
1078 rxq->head = 0;
1079
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001080 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001081
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001082 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001083 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001084
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001085 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001086 * Check for a pending join operation. Save a
1087 * copy of the beacon and schedule a tasklet to
1088 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001089 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001090 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001091 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001092
Johannes Bergf1d58c22009-06-17 13:13:00 +02001093 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1094 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001095
1096 processed++;
1097 }
1098
1099 return processed;
1100}
1101
1102
1103/*
1104 * Packet transmission.
1105 */
1106
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001107#define MWL8K_TXD_STATUS_OK 0x00000001
1108#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1109#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1110#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001111#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001112
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001113#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1114#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1115#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1116#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1117#define MWL8K_QOS_EOSP 0x0010
1118
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001119struct mwl8k_tx_desc {
1120 __le32 status;
1121 __u8 data_rate;
1122 __u8 tx_priority;
1123 __le16 qos_control;
1124 __le32 pkt_phys_addr;
1125 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001126 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001127 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001128 __le32 reserved;
1129 __le16 rate_info;
1130 __u8 peer_id;
Brian Cavagnoloa1fe24b2010-11-12 17:23:47 -08001131 __u8 tx_frag_cnt;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001132} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001133
1134#define MWL8K_TX_DESCS 128
1135
1136static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1137{
1138 struct mwl8k_priv *priv = hw->priv;
1139 struct mwl8k_tx_queue *txq = priv->txq + index;
1140 int size;
1141 int i;
1142
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001143 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001144 txq->head = 0;
1145 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001146
1147 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1148
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001149 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1150 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001151 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001152 return -ENOMEM;
1153 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001154 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001155
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001156 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1157 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001158 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001159 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001160 return -ENOMEM;
1161 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001162 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001163
1164 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1165 struct mwl8k_tx_desc *tx_desc;
1166 int nexti;
1167
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001168 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001169 nexti = (i + 1) % MWL8K_TX_DESCS;
1170
1171 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001172 tx_desc->next_txd_phys_addr =
1173 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001174 }
1175
1176 return 0;
1177}
1178
1179static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1180{
1181 iowrite32(MWL8K_H2A_INT_PPA_READY,
1182 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1183 iowrite32(MWL8K_H2A_INT_DUMMY,
1184 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1185 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1186}
1187
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001188static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001189{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001190 struct mwl8k_priv *priv = hw->priv;
1191 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001192
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001193 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1194 struct mwl8k_tx_queue *txq = priv->txq + i;
1195 int fw_owned = 0;
1196 int drv_owned = 0;
1197 int unused = 0;
1198 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001199
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001200 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001201 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1202 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001203
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001204 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001206 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001207 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001208 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001209
1210 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001211 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001212 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001213
Joe Perchesc96c31e2010-07-26 14:39:58 -07001214 wiphy_err(hw->wiphy,
1215 "txq[%d] len=%d head=%d tail=%d "
1216 "fw_owned=%d drv_owned=%d unused=%d\n",
1217 i,
1218 txq->len, txq->head, txq->tail,
1219 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001220 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001221}
1222
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001223/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001224 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001225 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001226#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001227
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001228static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001229{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001230 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001231 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001232 int retry;
1233 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001234
1235 might_sleep();
1236
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001237 /*
1238 * The TX queues are stopped at this point, so this test
1239 * doesn't need to take ->tx_lock.
1240 */
1241 if (!priv->pending_tx_pkts)
1242 return 0;
1243
1244 retry = 0;
1245 rc = 0;
1246
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001247 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001248 priv->tx_wait = &tx_wait;
1249 while (!rc) {
1250 int oldcount;
1251 unsigned long timeout;
1252
1253 oldcount = priv->pending_tx_pkts;
1254
1255 spin_unlock_bh(&priv->tx_lock);
1256 timeout = wait_for_completion_timeout(&tx_wait,
1257 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1258 spin_lock_bh(&priv->tx_lock);
1259
1260 if (timeout) {
1261 WARN_ON(priv->pending_tx_pkts);
1262 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001263 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001264 }
1265 break;
1266 }
1267
1268 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001269 wiphy_notice(hw->wiphy,
1270 "waiting for tx rings to drain (%d -> %d pkts)\n",
1271 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001272 retry = 1;
1273 continue;
1274 }
1275
1276 priv->tx_wait = NULL;
1277
Joe Perchesc96c31e2010-07-26 14:39:58 -07001278 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1279 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001280 mwl8k_dump_tx_rings(hw);
1281
1282 rc = -ETIMEDOUT;
1283 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001284 spin_unlock_bh(&priv->tx_lock);
1285
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001286 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001287}
1288
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001289#define MWL8K_TXD_SUCCESS(status) \
1290 ((status) & (MWL8K_TXD_STATUS_OK | \
1291 MWL8K_TXD_STATUS_OK_RETRY | \
1292 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001293
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001294static int
1295mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001296{
1297 struct mwl8k_priv *priv = hw->priv;
1298 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001299 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001301 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001302 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001304 struct mwl8k_tx_desc *tx_desc;
1305 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001306 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001307 struct sk_buff *skb;
1308 struct ieee80211_tx_info *info;
1309 u32 status;
1310
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001311 tx = txq->head;
1312 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001313
1314 status = le32_to_cpu(tx_desc->status);
1315
1316 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1317 if (!force)
1318 break;
1319 tx_desc->status &=
1320 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1321 }
1322
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001323 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001324 BUG_ON(txq->len == 0);
1325 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001326 priv->pending_tx_pkts--;
1327
1328 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001329 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001330 skb = txq->skb[tx];
1331 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001332
1333 BUG_ON(skb == NULL);
1334 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1335
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001336 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001337
1338 /* Mark descriptor as unused */
1339 tx_desc->pkt_phys_addr = 0;
1340 tx_desc->pkt_len = 0;
1341
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001342 info = IEEE80211_SKB_CB(skb);
1343 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001344 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001346
1347 ieee80211_tx_status_irqsafe(hw, skb);
1348
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001349 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001350 }
1351
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001352 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001353 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001354
1355 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001356}
1357
1358/* must be called only when the card's transmit is completely halted */
1359static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1360{
1361 struct mwl8k_priv *priv = hw->priv;
1362 struct mwl8k_tx_queue *txq = priv->txq + index;
1363
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001364 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001365
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001366 kfree(txq->skb);
1367 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001368
1369 pci_free_consistent(priv->pdev,
1370 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001371 txq->txd, txq->txd_dma);
1372 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001373}
1374
1375static int
1376mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1377{
1378 struct mwl8k_priv *priv = hw->priv;
1379 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001380 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001381 struct ieee80211_hdr *wh;
1382 struct mwl8k_tx_queue *txq;
1383 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001384 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001385 u32 txstatus;
1386 u8 txdatarate;
1387 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001388
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001389 wh = (struct ieee80211_hdr *)skb->data;
1390 if (ieee80211_is_data_qos(wh->frame_control))
1391 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1392 else
1393 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001394
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001395 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001396 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001397
1398 tx_info = IEEE80211_SKB_CB(skb);
1399 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001400
1401 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001402 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001403 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1404 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001405 }
1406
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001407 /* Setup firmware control bit fields for each frame type. */
1408 txstatus = 0;
1409 txdatarate = 0;
1410 if (ieee80211_is_mgmt(wh->frame_control) ||
1411 ieee80211_is_ctl(wh->frame_control)) {
1412 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001413 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001414 } else if (ieee80211_is_data(wh->frame_control)) {
1415 txdatarate = 1;
1416 if (is_multicast_ether_addr(wh->addr1))
1417 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1418
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001419 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001420 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001421 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001422 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001423 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001424 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001425
1426 dma = pci_map_single(priv->pdev, skb->data,
1427 skb->len, PCI_DMA_TODEVICE);
1428
1429 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001430 wiphy_debug(hw->wiphy,
1431 "failed to dma map skb, dropping TX frame.\n");
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001432 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001433 return NETDEV_TX_OK;
1434 }
1435
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001436 spin_lock_bh(&priv->tx_lock);
1437
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001438 txq = priv->txq + index;
1439
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001440 BUG_ON(txq->skb[txq->tail] != NULL);
1441 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001442
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001443 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001444 tx->data_rate = txdatarate;
1445 tx->tx_priority = index;
1446 tx->qos_control = cpu_to_le16(qos);
1447 tx->pkt_phys_addr = cpu_to_le32(dma);
1448 tx->pkt_len = cpu_to_le16(skb->len);
1449 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001450 if (!priv->ap_fw && tx_info->control.sta != NULL)
1451 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1452 else
1453 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001454 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001455 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1456
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001457 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001458 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001459
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001460 txq->tail++;
1461 if (txq->tail == MWL8K_TX_DESCS)
1462 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001463
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001464 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001465 ieee80211_stop_queue(hw, index);
1466
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001467 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001468
1469 spin_unlock_bh(&priv->tx_lock);
1470
1471 return NETDEV_TX_OK;
1472}
1473
1474
1475/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001476 * Firmware access.
1477 *
1478 * We have the following requirements for issuing firmware commands:
1479 * - Some commands require that the packet transmit path is idle when
1480 * the command is issued. (For simplicity, we'll just quiesce the
1481 * transmit path for every command.)
1482 * - There are certain sequences of commands that need to be issued to
1483 * the hardware sequentially, with no other intervening commands.
1484 *
1485 * This leads to an implementation of a "firmware lock" as a mutex that
1486 * can be taken recursively, and which is taken by both the low-level
1487 * command submission function (mwl8k_post_cmd) as well as any users of
1488 * that function that require issuing of an atomic sequence of commands,
1489 * and quiesces the transmit path whenever it's taken.
1490 */
1491static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1492{
1493 struct mwl8k_priv *priv = hw->priv;
1494
1495 if (priv->fw_mutex_owner != current) {
1496 int rc;
1497
1498 mutex_lock(&priv->fw_mutex);
1499 ieee80211_stop_queues(hw);
1500
1501 rc = mwl8k_tx_wait_empty(hw);
1502 if (rc) {
1503 ieee80211_wake_queues(hw);
1504 mutex_unlock(&priv->fw_mutex);
1505
1506 return rc;
1507 }
1508
1509 priv->fw_mutex_owner = current;
1510 }
1511
1512 priv->fw_mutex_depth++;
1513
1514 return 0;
1515}
1516
1517static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1518{
1519 struct mwl8k_priv *priv = hw->priv;
1520
1521 if (!--priv->fw_mutex_depth) {
1522 ieee80211_wake_queues(hw);
1523 priv->fw_mutex_owner = NULL;
1524 mutex_unlock(&priv->fw_mutex);
1525 }
1526}
1527
1528
1529/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001530 * Command processing.
1531 */
1532
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001533/* Timeout firmware commands after 10s */
1534#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001535
1536static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1537{
1538 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1539 struct mwl8k_priv *priv = hw->priv;
1540 void __iomem *regs = priv->regs;
1541 dma_addr_t dma_addr;
1542 unsigned int dma_size;
1543 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001544 unsigned long timeout = 0;
1545 u8 buf[32];
1546
John W. Linvilleb6037422010-07-20 13:55:00 -04001547 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001548 dma_size = le16_to_cpu(cmd->length);
1549 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1550 PCI_DMA_BIDIRECTIONAL);
1551 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1552 return -ENOMEM;
1553
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001554 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001555 if (rc) {
1556 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1557 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001558 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001559 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001560
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001561 priv->hostcmd_wait = &cmd_wait;
1562 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1563 iowrite32(MWL8K_H2A_INT_DOORBELL,
1564 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1565 iowrite32(MWL8K_H2A_INT_DUMMY,
1566 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001567
1568 timeout = wait_for_completion_timeout(&cmd_wait,
1569 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1570
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001571 priv->hostcmd_wait = NULL;
1572
1573 mwl8k_fw_unlock(hw);
1574
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001575 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1576 PCI_DMA_BIDIRECTIONAL);
1577
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001579 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001580 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1581 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001582 rc = -ETIMEDOUT;
1583 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001584 int ms;
1585
1586 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1587
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001588 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001589 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001590 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001591 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1592 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001593 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001594 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001595 mwl8k_cmd_name(cmd->code,
1596 buf, sizeof(buf)),
1597 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001598 }
1599
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001600 return rc;
1601}
1602
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001603static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1604 struct ieee80211_vif *vif,
1605 struct mwl8k_cmd_pkt *cmd)
1606{
1607 if (vif != NULL)
1608 cmd->macid = MWL8K_VIF(vif)->macid;
1609 return mwl8k_post_cmd(hw, cmd);
1610}
1611
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001612/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001613 * Setup code shared between STA and AP firmware images.
1614 */
1615static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1616{
1617 struct mwl8k_priv *priv = hw->priv;
1618
1619 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1620 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1621
1622 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1623 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1624
1625 priv->band_24.band = IEEE80211_BAND_2GHZ;
1626 priv->band_24.channels = priv->channels_24;
1627 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1628 priv->band_24.bitrates = priv->rates_24;
1629 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1630
1631 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1632}
1633
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001634static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1635{
1636 struct mwl8k_priv *priv = hw->priv;
1637
1638 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1639 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1640
1641 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1642 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1643
1644 priv->band_50.band = IEEE80211_BAND_5GHZ;
1645 priv->band_50.channels = priv->channels_50;
1646 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1647 priv->band_50.bitrates = priv->rates_50;
1648 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1649
1650 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1651}
1652
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001653/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001654 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001655 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001656struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 struct mwl8k_cmd_pkt header;
1658 __u8 hw_rev;
1659 __u8 host_interface;
1660 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001661 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001662 __le16 region_code;
1663 __le32 fw_rev;
1664 __le32 ps_cookie;
1665 __le32 caps;
1666 __u8 mcs_bitmap[16];
1667 __le32 rx_queue_ptr;
1668 __le32 num_tx_queues;
1669 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1670 __le32 caps2;
1671 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001672 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001673} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001674
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001675#define MWL8K_CAP_MAX_AMSDU 0x20000000
1676#define MWL8K_CAP_GREENFIELD 0x08000000
1677#define MWL8K_CAP_AMPDU 0x04000000
1678#define MWL8K_CAP_RX_STBC 0x01000000
1679#define MWL8K_CAP_TX_STBC 0x00800000
1680#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1681#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1682#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1683#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1684#define MWL8K_CAP_DELAY_BA 0x00003000
1685#define MWL8K_CAP_MIMO 0x00000200
1686#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001687#define MWL8K_CAP_BAND_MASK 0x00000007
1688#define MWL8K_CAP_5GHZ 0x00000004
1689#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001690
Lennert Buytenhek06953232010-01-12 13:49:41 +01001691static void
1692mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1693 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001694{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001695 int rx_streams;
1696 int tx_streams;
1697
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001698 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001699
1700 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001701 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001702 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001703 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001704 if (cap & MWL8K_CAP_AMPDU) {
1705 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001706 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1707 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001708 }
1709 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001710 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001711 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001712 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001713 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001714 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001715 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001716 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001717 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001718 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001719 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001720 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001721
1722 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1723 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1724
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001725 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001726 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001727 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001728 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001729 band->ht_cap.mcs.rx_mask[2] = 0xff;
1730 band->ht_cap.mcs.rx_mask[4] = 0x01;
1731 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001732
1733 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001734 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1735 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001736 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1737 }
1738}
1739
Lennert Buytenhek06953232010-01-12 13:49:41 +01001740static void
1741mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1742{
1743 struct mwl8k_priv *priv = hw->priv;
1744
1745 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1746 mwl8k_setup_2ghz_band(hw);
1747 if (caps & MWL8K_CAP_MIMO)
1748 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1749 }
1750
1751 if (caps & MWL8K_CAP_5GHZ) {
1752 mwl8k_setup_5ghz_band(hw);
1753 if (caps & MWL8K_CAP_MIMO)
1754 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1755 }
1756}
1757
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001758static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001759{
1760 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001761 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762 int rc;
1763 int i;
1764
1765 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1766 if (cmd == NULL)
1767 return -ENOMEM;
1768
1769 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1770 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1771
1772 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1773 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001774 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001775 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001776 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001777 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001778 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001779 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001780
1781 rc = mwl8k_post_cmd(hw, &cmd->header);
1782
1783 if (!rc) {
1784 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1785 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001786 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001787 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001788 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001789 priv->ap_macids_supported = 0x00000000;
1790 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001791 }
1792
1793 kfree(cmd);
1794 return rc;
1795}
1796
1797/*
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001798 * CMD_GET_HW_SPEC (AP version).
1799 */
1800struct mwl8k_cmd_get_hw_spec_ap {
1801 struct mwl8k_cmd_pkt header;
1802 __u8 hw_rev;
1803 __u8 host_interface;
1804 __le16 num_wcb;
1805 __le16 num_mcaddrs;
1806 __u8 perm_addr[ETH_ALEN];
1807 __le16 region_code;
1808 __le16 num_antenna;
1809 __le32 fw_rev;
1810 __le32 wcbbase0;
1811 __le32 rxwrptr;
1812 __le32 rxrdptr;
1813 __le32 ps_cookie;
1814 __le32 wcbbase1;
1815 __le32 wcbbase2;
1816 __le32 wcbbase3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001817} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001818
1819static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1820{
1821 struct mwl8k_priv *priv = hw->priv;
1822 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1823 int rc;
1824
1825 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1826 if (cmd == NULL)
1827 return -ENOMEM;
1828
1829 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1830 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1831
1832 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1833 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1834
1835 rc = mwl8k_post_cmd(hw, &cmd->header);
1836
1837 if (!rc) {
1838 int off;
1839
1840 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1841 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1842 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1843 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001844 mwl8k_setup_2ghz_band(hw);
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001845 priv->ap_macids_supported = 0x000000ff;
1846 priv->sta_macids_supported = 0x00000000;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001847
1848 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001849 iowrite32(priv->txq[0].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001850
1851 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001852 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001853
1854 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001855 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001856
1857 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001858 iowrite32(priv->txq[1].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001859
1860 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001861 iowrite32(priv->txq[2].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001862
1863 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001864 iowrite32(priv->txq[3].txd_dma, priv->sram + off);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001865 }
1866
1867 kfree(cmd);
1868 return rc;
1869}
1870
1871/*
1872 * CMD_SET_HW_SPEC.
1873 */
1874struct mwl8k_cmd_set_hw_spec {
1875 struct mwl8k_cmd_pkt header;
1876 __u8 hw_rev;
1877 __u8 host_interface;
1878 __le16 num_mcaddrs;
1879 __u8 perm_addr[ETH_ALEN];
1880 __le16 region_code;
1881 __le32 fw_rev;
1882 __le32 ps_cookie;
1883 __le32 caps;
1884 __le32 rx_queue_ptr;
1885 __le32 num_tx_queues;
1886 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1887 __le32 flags;
1888 __le32 num_tx_desc_per_queue;
1889 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001890} __packed;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001891
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001892#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1893#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
1894#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001895
1896static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1897{
1898 struct mwl8k_priv *priv = hw->priv;
1899 struct mwl8k_cmd_set_hw_spec *cmd;
1900 int rc;
1901 int i;
1902
1903 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1904 if (cmd == NULL)
1905 return -ENOMEM;
1906
1907 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1908 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1909
1910 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1911 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1912 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1913 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1914 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001915 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
1916 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
1917 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001918 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1919 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1920
1921 rc = mwl8k_post_cmd(hw, &cmd->header);
1922 kfree(cmd);
1923
1924 return rc;
1925}
1926
1927/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001928 * CMD_MAC_MULTICAST_ADR.
1929 */
1930struct mwl8k_cmd_mac_multicast_adr {
1931 struct mwl8k_cmd_pkt header;
1932 __le16 action;
1933 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001934 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001935};
1936
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001937#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1938#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1939#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1940#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001941
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001942static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001943__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad32010-04-01 21:22:57 +00001944 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001945{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001946 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001947 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001948 int size;
Jiri Pirko22bedad32010-04-01 21:22:57 +00001949 int mc_count = 0;
1950
1951 if (mc_list)
1952 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001953
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001954 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001955 allmulti = 1;
1956 mc_count = 0;
1957 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001958
1959 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1960
1961 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001962 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001963 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001964
1965 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1966 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001967 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1968 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001969
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001970 if (allmulti) {
1971 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1972 } else if (mc_count) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00001973 struct netdev_hw_addr *ha;
1974 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001975
1976 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1977 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad32010-04-01 21:22:57 +00001978 netdev_hw_addr_list_for_each(ha, mc_list) {
1979 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001980 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001981 }
1982
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001983 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001984}
1985
1986/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001987 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001988 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001989struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001990 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001991 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00001992} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001993
1994#define MWL8K_STAT_ACK_FAILURE 9
1995#define MWL8K_STAT_RTS_FAILURE 12
1996#define MWL8K_STAT_FCS_ERROR 24
1997#define MWL8K_STAT_RTS_SUCCESS 11
1998
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001999static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2000 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002001{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002002 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002003 int rc;
2004
2005 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2006 if (cmd == NULL)
2007 return -ENOMEM;
2008
2009 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2010 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002011
2012 rc = mwl8k_post_cmd(hw, &cmd->header);
2013 if (!rc) {
2014 stats->dot11ACKFailureCount =
2015 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2016 stats->dot11RTSFailureCount =
2017 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2018 stats->dot11FCSErrorCount =
2019 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2020 stats->dot11RTSSuccessCount =
2021 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2022 }
2023 kfree(cmd);
2024
2025 return rc;
2026}
2027
2028/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002029 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002030 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002031struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002032 struct mwl8k_cmd_pkt header;
2033 __le16 action;
2034 __le16 control;
2035 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002036} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002037
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002038static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002039mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002040{
2041 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002042 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002043 int rc;
2044
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002045 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002046 return 0;
2047
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002048 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2049 if (cmd == NULL)
2050 return -ENOMEM;
2051
2052 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2053 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2054 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002055 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002056 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2057
2058 rc = mwl8k_post_cmd(hw, &cmd->header);
2059 kfree(cmd);
2060
2061 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002062 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002063
2064 return rc;
2065}
2066
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002067static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002068{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002069 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002070}
2071
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002072static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002073{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002074 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002075}
2076
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002077static int
2078mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2079{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002080 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002081
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002082 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002083
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002084 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002085}
2086
2087/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002088 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002089 */
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002090#define MWL8K_RF_TX_POWER_LEVEL_TOTAL 8
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002091
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002092struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002093 struct mwl8k_cmd_pkt header;
2094 __le16 action;
2095 __le16 support_level;
2096 __le16 current_level;
2097 __le16 reserved;
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002098 __le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002099} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002100
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002101static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002102{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002103 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002104 int rc;
2105
2106 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2107 if (cmd == NULL)
2108 return -ENOMEM;
2109
2110 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2111 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2112 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2113 cmd->support_level = cpu_to_le16(dBm);
2114
2115 rc = mwl8k_post_cmd(hw, &cmd->header);
2116 kfree(cmd);
2117
2118 return rc;
2119}
2120
2121/*
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08002122 * CMD_TX_POWER.
2123 */
2124#define MWL8K_TX_POWER_LEVEL_TOTAL 12
2125
2126struct mwl8k_cmd_tx_power {
2127 struct mwl8k_cmd_pkt header;
2128 __le16 action;
2129 __le16 band;
2130 __le16 channel;
2131 __le16 bw;
2132 __le16 sub_ch;
2133 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2134} __attribute__((packed));
2135
2136static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2137 struct ieee80211_conf *conf,
2138 unsigned short pwr)
2139{
2140 struct ieee80211_channel *channel = conf->channel;
2141 struct mwl8k_cmd_tx_power *cmd;
2142 int rc;
2143 int i;
2144
2145 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2146 if (cmd == NULL)
2147 return -ENOMEM;
2148
2149 cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2150 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2151 cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2152
2153 if (channel->band == IEEE80211_BAND_2GHZ)
2154 cmd->band = cpu_to_le16(0x1);
2155 else if (channel->band == IEEE80211_BAND_5GHZ)
2156 cmd->band = cpu_to_le16(0x4);
2157
2158 cmd->channel = channel->hw_value;
2159
2160 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2161 conf->channel_type == NL80211_CHAN_HT20) {
2162 cmd->bw = cpu_to_le16(0x2);
2163 } else {
2164 cmd->bw = cpu_to_le16(0x4);
2165 if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2166 cmd->sub_ch = cpu_to_le16(0x3);
2167 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2168 cmd->sub_ch = cpu_to_le16(0x1);
2169 }
2170
2171 for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2172 cmd->power_level_list[i] = cpu_to_le16(pwr);
2173
2174 rc = mwl8k_post_cmd(hw, &cmd->header);
2175 kfree(cmd);
2176
2177 return rc;
2178}
2179
2180/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002181 * CMD_RF_ANTENNA.
2182 */
2183struct mwl8k_cmd_rf_antenna {
2184 struct mwl8k_cmd_pkt header;
2185 __le16 antenna;
2186 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002187} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002188
2189#define MWL8K_RF_ANTENNA_RX 1
2190#define MWL8K_RF_ANTENNA_TX 2
2191
2192static int
2193mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2194{
2195 struct mwl8k_cmd_rf_antenna *cmd;
2196 int rc;
2197
2198 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2199 if (cmd == NULL)
2200 return -ENOMEM;
2201
2202 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2203 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2204 cmd->antenna = cpu_to_le16(antenna);
2205 cmd->mode = cpu_to_le16(mask);
2206
2207 rc = mwl8k_post_cmd(hw, &cmd->header);
2208 kfree(cmd);
2209
2210 return rc;
2211}
2212
2213/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002214 * CMD_SET_BEACON.
2215 */
2216struct mwl8k_cmd_set_beacon {
2217 struct mwl8k_cmd_pkt header;
2218 __le16 beacon_len;
2219 __u8 beacon[0];
2220};
2221
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002222static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2223 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002224{
2225 struct mwl8k_cmd_set_beacon *cmd;
2226 int rc;
2227
2228 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2229 if (cmd == NULL)
2230 return -ENOMEM;
2231
2232 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2233 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2234 cmd->beacon_len = cpu_to_le16(len);
2235 memcpy(cmd->beacon, beacon, len);
2236
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002237 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002238 kfree(cmd);
2239
2240 return rc;
2241}
2242
2243/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002244 * CMD_SET_PRE_SCAN.
2245 */
2246struct mwl8k_cmd_set_pre_scan {
2247 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002248} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002249
2250static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2251{
2252 struct mwl8k_cmd_set_pre_scan *cmd;
2253 int rc;
2254
2255 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2256 if (cmd == NULL)
2257 return -ENOMEM;
2258
2259 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2260 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2261
2262 rc = mwl8k_post_cmd(hw, &cmd->header);
2263 kfree(cmd);
2264
2265 return rc;
2266}
2267
2268/*
2269 * CMD_SET_POST_SCAN.
2270 */
2271struct mwl8k_cmd_set_post_scan {
2272 struct mwl8k_cmd_pkt header;
2273 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002274 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002275} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002276
2277static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002278mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002279{
2280 struct mwl8k_cmd_set_post_scan *cmd;
2281 int rc;
2282
2283 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2284 if (cmd == NULL)
2285 return -ENOMEM;
2286
2287 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2288 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2289 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002290 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002291
2292 rc = mwl8k_post_cmd(hw, &cmd->header);
2293 kfree(cmd);
2294
2295 return rc;
2296}
2297
2298/*
2299 * CMD_SET_RF_CHANNEL.
2300 */
2301struct mwl8k_cmd_set_rf_channel {
2302 struct mwl8k_cmd_pkt header;
2303 __le16 action;
2304 __u8 current_channel;
2305 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002306} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002307
2308static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002309 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002310{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002311 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002312 struct mwl8k_cmd_set_rf_channel *cmd;
2313 int rc;
2314
2315 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2316 if (cmd == NULL)
2317 return -ENOMEM;
2318
2319 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2320 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2321 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2322 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002323
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002324 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002325 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002326 else if (channel->band == IEEE80211_BAND_5GHZ)
2327 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002328
2329 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2330 conf->channel_type == NL80211_CHAN_HT20)
2331 cmd->channel_flags |= cpu_to_le32(0x00000080);
2332 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2333 cmd->channel_flags |= cpu_to_le32(0x000001900);
2334 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2335 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002336
2337 rc = mwl8k_post_cmd(hw, &cmd->header);
2338 kfree(cmd);
2339
2340 return rc;
2341}
2342
2343/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002344 * CMD_SET_AID.
2345 */
2346#define MWL8K_FRAME_PROT_DISABLED 0x00
2347#define MWL8K_FRAME_PROT_11G 0x07
2348#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2349#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2350
2351struct mwl8k_cmd_update_set_aid {
2352 struct mwl8k_cmd_pkt header;
2353 __le16 aid;
2354
2355 /* AP's MAC address (BSSID) */
2356 __u8 bssid[ETH_ALEN];
2357 __le16 protection_mode;
2358 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002359} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002360
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002361static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2362{
2363 int i;
2364 int j;
2365
2366 /*
2367 * Clear nonstandard rates 4 and 13.
2368 */
2369 mask &= 0x1fef;
2370
2371 for (i = 0, j = 0; i < 14; i++) {
2372 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002373 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002374 }
2375}
2376
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002377static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002378mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2379 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002380{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002381 struct mwl8k_cmd_update_set_aid *cmd;
2382 u16 prot_mode;
2383 int rc;
2384
2385 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2386 if (cmd == NULL)
2387 return -ENOMEM;
2388
2389 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2390 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002391 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002392 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002393
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002394 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002395 prot_mode = MWL8K_FRAME_PROT_11G;
2396 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002397 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002398 IEEE80211_HT_OP_MODE_PROTECTION) {
2399 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2400 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2401 break;
2402 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2403 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2404 break;
2405 default:
2406 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2407 break;
2408 }
2409 }
2410 cmd->protection_mode = cpu_to_le16(prot_mode);
2411
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002412 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002413
2414 rc = mwl8k_post_cmd(hw, &cmd->header);
2415 kfree(cmd);
2416
2417 return rc;
2418}
2419
2420/*
2421 * CMD_SET_RATE.
2422 */
2423struct mwl8k_cmd_set_rate {
2424 struct mwl8k_cmd_pkt header;
2425 __u8 legacy_rates[14];
2426
2427 /* Bitmap for supported MCS codes. */
2428 __u8 mcs_set[16];
2429 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002430} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002431
2432static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002433mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002434 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002435{
2436 struct mwl8k_cmd_set_rate *cmd;
2437 int rc;
2438
2439 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2440 if (cmd == NULL)
2441 return -ENOMEM;
2442
2443 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2444 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002445 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002446 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002447
2448 rc = mwl8k_post_cmd(hw, &cmd->header);
2449 kfree(cmd);
2450
2451 return rc;
2452}
2453
2454/*
2455 * CMD_FINALIZE_JOIN.
2456 */
2457#define MWL8K_FJ_BEACON_MAXLEN 128
2458
2459struct mwl8k_cmd_finalize_join {
2460 struct mwl8k_cmd_pkt header;
2461 __le32 sleep_interval; /* Number of beacon periods to sleep */
2462 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002463} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002464
2465static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2466 int framelen, int dtim)
2467{
2468 struct mwl8k_cmd_finalize_join *cmd;
2469 struct ieee80211_mgmt *payload = frame;
2470 int payload_len;
2471 int rc;
2472
2473 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2474 if (cmd == NULL)
2475 return -ENOMEM;
2476
2477 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2478 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2479 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2480
2481 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2482 if (payload_len < 0)
2483 payload_len = 0;
2484 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2485 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2486
2487 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2488
2489 rc = mwl8k_post_cmd(hw, &cmd->header);
2490 kfree(cmd);
2491
2492 return rc;
2493}
2494
2495/*
2496 * CMD_SET_RTS_THRESHOLD.
2497 */
2498struct mwl8k_cmd_set_rts_threshold {
2499 struct mwl8k_cmd_pkt header;
2500 __le16 action;
2501 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002502} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002503
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002504static int
2505mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002506{
2507 struct mwl8k_cmd_set_rts_threshold *cmd;
2508 int rc;
2509
2510 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2511 if (cmd == NULL)
2512 return -ENOMEM;
2513
2514 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2515 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002516 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2517 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002518
2519 rc = mwl8k_post_cmd(hw, &cmd->header);
2520 kfree(cmd);
2521
2522 return rc;
2523}
2524
2525/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002526 * CMD_SET_SLOT.
2527 */
2528struct mwl8k_cmd_set_slot {
2529 struct mwl8k_cmd_pkt header;
2530 __le16 action;
2531 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002532} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002533
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002534static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002535{
2536 struct mwl8k_cmd_set_slot *cmd;
2537 int rc;
2538
2539 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2540 if (cmd == NULL)
2541 return -ENOMEM;
2542
2543 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2544 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2545 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002546 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002547
2548 rc = mwl8k_post_cmd(hw, &cmd->header);
2549 kfree(cmd);
2550
2551 return rc;
2552}
2553
2554/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002555 * CMD_SET_EDCA_PARAMS.
2556 */
2557struct mwl8k_cmd_set_edca_params {
2558 struct mwl8k_cmd_pkt header;
2559
2560 /* See MWL8K_SET_EDCA_XXX below */
2561 __le16 action;
2562
2563 /* TX opportunity in units of 32 us */
2564 __le16 txop;
2565
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002566 union {
2567 struct {
2568 /* Log exponent of max contention period: 0...15 */
2569 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002570
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002571 /* Log exponent of min contention period: 0...15 */
2572 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002573
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002574 /* Adaptive interframe spacing in units of 32us */
2575 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002576
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002577 /* TX queue to configure */
2578 __u8 txq;
2579 } ap;
2580 struct {
2581 /* Log exponent of max contention period: 0...15 */
2582 __u8 log_cw_max;
2583
2584 /* Log exponent of min contention period: 0...15 */
2585 __u8 log_cw_min;
2586
2587 /* Adaptive interframe spacing in units of 32us */
2588 __u8 aifs;
2589
2590 /* TX queue to configure */
2591 __u8 txq;
2592 } sta;
2593 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002594} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002595
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002596#define MWL8K_SET_EDCA_CW 0x01
2597#define MWL8K_SET_EDCA_TXOP 0x02
2598#define MWL8K_SET_EDCA_AIFS 0x04
2599
2600#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2601 MWL8K_SET_EDCA_TXOP | \
2602 MWL8K_SET_EDCA_AIFS)
2603
2604static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002605mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2606 __u16 cw_min, __u16 cw_max,
2607 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002608{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002609 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002610 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002611 int rc;
2612
2613 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2614 if (cmd == NULL)
2615 return -ENOMEM;
2616
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002617 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2618 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002619 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2620 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002621 if (priv->ap_fw) {
2622 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2623 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2624 cmd->ap.aifs = aifs;
2625 cmd->ap.txq = qnum;
2626 } else {
2627 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2628 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2629 cmd->sta.aifs = aifs;
2630 cmd->sta.txq = qnum;
2631 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002632
2633 rc = mwl8k_post_cmd(hw, &cmd->header);
2634 kfree(cmd);
2635
2636 return rc;
2637}
2638
2639/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002640 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002641 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002642struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002643 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002644 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002645} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002646
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002647static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002648{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002649 struct mwl8k_priv *priv = hw->priv;
2650 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002651 int rc;
2652
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002653 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2654 if (cmd == NULL)
2655 return -ENOMEM;
2656
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002657 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002658 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002659 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002660
2661 rc = mwl8k_post_cmd(hw, &cmd->header);
2662 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002663
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002664 if (!rc)
2665 priv->wmm_enabled = enable;
2666
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002667 return rc;
2668}
2669
2670/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002671 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002672 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002673struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002674 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002675 __le32 action;
2676 __u8 rx_antenna_map;
2677 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002678} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002679
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002680static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002681{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002682 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002683 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002684
2685 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2686 if (cmd == NULL)
2687 return -ENOMEM;
2688
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002689 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002690 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002691 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2692 cmd->rx_antenna_map = rx;
2693 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002694
2695 rc = mwl8k_post_cmd(hw, &cmd->header);
2696 kfree(cmd);
2697
2698 return rc;
2699}
2700
2701/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002702 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002703 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002704struct mwl8k_cmd_use_fixed_rate_sta {
2705 struct mwl8k_cmd_pkt header;
2706 __le32 action;
2707 __le32 allow_rate_drop;
2708 __le32 num_rates;
2709 struct {
2710 __le32 is_ht_rate;
2711 __le32 enable_retry;
2712 __le32 rate;
2713 __le32 retry_count;
2714 } rate_entry[8];
2715 __le32 rate_type;
2716 __le32 reserved1;
2717 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002718} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002719
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002720#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002721#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002722
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002723static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002724{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002725 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002726 int rc;
2727
2728 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2729 if (cmd == NULL)
2730 return -ENOMEM;
2731
2732 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2733 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002734 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2735 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002736
2737 rc = mwl8k_post_cmd(hw, &cmd->header);
2738 kfree(cmd);
2739
2740 return rc;
2741}
2742
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002743/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002744 * CMD_USE_FIXED_RATE (AP version).
2745 */
2746struct mwl8k_cmd_use_fixed_rate_ap {
2747 struct mwl8k_cmd_pkt header;
2748 __le32 action;
2749 __le32 allow_rate_drop;
2750 __le32 num_rates;
2751 struct mwl8k_rate_entry_ap {
2752 __le32 is_ht_rate;
2753 __le32 enable_retry;
2754 __le32 rate;
2755 __le32 retry_count;
2756 } rate_entry[4];
2757 u8 multicast_rate;
2758 u8 multicast_rate_type;
2759 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002760} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002761
2762static int
2763mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2764{
2765 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2766 int rc;
2767
2768 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2769 if (cmd == NULL)
2770 return -ENOMEM;
2771
2772 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2773 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2774 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2775 cmd->multicast_rate = mcast;
2776 cmd->management_rate = mgmt;
2777
2778 rc = mwl8k_post_cmd(hw, &cmd->header);
2779 kfree(cmd);
2780
2781 return rc;
2782}
2783
2784/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002785 * CMD_ENABLE_SNIFFER.
2786 */
2787struct mwl8k_cmd_enable_sniffer {
2788 struct mwl8k_cmd_pkt header;
2789 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002790} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002791
2792static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2793{
2794 struct mwl8k_cmd_enable_sniffer *cmd;
2795 int rc;
2796
2797 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2798 if (cmd == NULL)
2799 return -ENOMEM;
2800
2801 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2802 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2803 cmd->action = cpu_to_le32(!!enable);
2804
2805 rc = mwl8k_post_cmd(hw, &cmd->header);
2806 kfree(cmd);
2807
2808 return rc;
2809}
2810
2811/*
2812 * CMD_SET_MAC_ADDR.
2813 */
2814struct mwl8k_cmd_set_mac_addr {
2815 struct mwl8k_cmd_pkt header;
2816 union {
2817 struct {
2818 __le16 mac_type;
2819 __u8 mac_addr[ETH_ALEN];
2820 } mbss;
2821 __u8 mac_addr[ETH_ALEN];
2822 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002823} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002824
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002825#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
2826#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
2827#define MWL8K_MAC_TYPE_PRIMARY_AP 2
2828#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002829
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002830static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
2831 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002832{
2833 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002834 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002835 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002836 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002837 int rc;
2838
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002839 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2840 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
2841 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
2842 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
2843 else
2844 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
2845 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
2846 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
2847 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2848 else
2849 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
2850 }
2851
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002852 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2853 if (cmd == NULL)
2854 return -ENOMEM;
2855
2856 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2857 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2858 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002859 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002860 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2861 } else {
2862 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2863 }
2864
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002865 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002866 kfree(cmd);
2867
2868 return rc;
2869}
2870
2871/*
2872 * CMD_SET_RATEADAPT_MODE.
2873 */
2874struct mwl8k_cmd_set_rate_adapt_mode {
2875 struct mwl8k_cmd_pkt header;
2876 __le16 action;
2877 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002878} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002879
2880static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2881{
2882 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2883 int rc;
2884
2885 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2886 if (cmd == NULL)
2887 return -ENOMEM;
2888
2889 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2890 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2891 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2892 cmd->mode = cpu_to_le16(mode);
2893
2894 rc = mwl8k_post_cmd(hw, &cmd->header);
2895 kfree(cmd);
2896
2897 return rc;
2898}
2899
2900/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002901 * CMD_BSS_START.
2902 */
2903struct mwl8k_cmd_bss_start {
2904 struct mwl8k_cmd_pkt header;
2905 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002906} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002907
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002908static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
2909 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002910{
2911 struct mwl8k_cmd_bss_start *cmd;
2912 int rc;
2913
2914 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2915 if (cmd == NULL)
2916 return -ENOMEM;
2917
2918 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
2919 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2920 cmd->enable = cpu_to_le32(enable);
2921
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002922 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002923 kfree(cmd);
2924
2925 return rc;
2926}
2927
2928/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002929 * CMD_SET_NEW_STN.
2930 */
2931struct mwl8k_cmd_set_new_stn {
2932 struct mwl8k_cmd_pkt header;
2933 __le16 aid;
2934 __u8 mac_addr[6];
2935 __le16 stn_id;
2936 __le16 action;
2937 __le16 rsvd;
2938 __le32 legacy_rates;
2939 __u8 ht_rates[4];
2940 __le16 cap_info;
2941 __le16 ht_capabilities_info;
2942 __u8 mac_ht_param_info;
2943 __u8 rev;
2944 __u8 control_channel;
2945 __u8 add_channel;
2946 __le16 op_mode;
2947 __le16 stbc;
2948 __u8 add_qos_info;
2949 __u8 is_qos_sta;
2950 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002951} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002952
2953#define MWL8K_STA_ACTION_ADD 0
2954#define MWL8K_STA_ACTION_REMOVE 2
2955
2956static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
2957 struct ieee80211_vif *vif,
2958 struct ieee80211_sta *sta)
2959{
2960 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002961 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002962 int rc;
2963
2964 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2965 if (cmd == NULL)
2966 return -ENOMEM;
2967
2968 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2969 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2970 cmd->aid = cpu_to_le16(sta->aid);
2971 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
2972 cmd->stn_id = cpu_to_le16(sta->aid);
2973 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002974 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
2975 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
2976 else
2977 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
2978 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002979 if (sta->ht_cap.ht_supported) {
2980 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
2981 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
2982 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
2983 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
2984 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
2985 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
2986 ((sta->ht_cap.ampdu_density & 7) << 2);
2987 cmd->is_qos_sta = 1;
2988 }
2989
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002990 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002991 kfree(cmd);
2992
2993 return rc;
2994}
2995
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002996static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
2997 struct ieee80211_vif *vif)
2998{
2999 struct mwl8k_cmd_set_new_stn *cmd;
3000 int rc;
3001
3002 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3003 if (cmd == NULL)
3004 return -ENOMEM;
3005
3006 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3007 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3008 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
3009
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003010 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003011 kfree(cmd);
3012
3013 return rc;
3014}
3015
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003016static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
3017 struct ieee80211_vif *vif, u8 *addr)
3018{
3019 struct mwl8k_cmd_set_new_stn *cmd;
3020 int rc;
3021
3022 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3023 if (cmd == NULL)
3024 return -ENOMEM;
3025
3026 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
3027 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3028 memcpy(cmd->mac_addr, addr, ETH_ALEN);
3029 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
3030
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003031 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003032 kfree(cmd);
3033
3034 return rc;
3035}
3036
3037/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003038 * CMD_UPDATE_STADB.
3039 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003040struct ewc_ht_info {
3041 __le16 control1;
3042 __le16 control2;
3043 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003044} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003045
3046struct peer_capability_info {
3047 /* Peer type - AP vs. STA. */
3048 __u8 peer_type;
3049
3050 /* Basic 802.11 capabilities from assoc resp. */
3051 __le16 basic_caps;
3052
3053 /* Set if peer supports 802.11n high throughput (HT). */
3054 __u8 ht_support;
3055
3056 /* Valid if HT is supported. */
3057 __le16 ht_caps;
3058 __u8 extended_ht_caps;
3059 struct ewc_ht_info ewc_info;
3060
3061 /* Legacy rate table. Intersection of our rates and peer rates. */
3062 __u8 legacy_rates[12];
3063
3064 /* HT rate table. Intersection of our rates and peer rates. */
3065 __u8 ht_rates[16];
3066 __u8 pad[16];
3067
3068 /* If set, interoperability mode, no proprietary extensions. */
3069 __u8 interop;
3070 __u8 pad2;
3071 __u8 station_id;
3072 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003073} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003074
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003075struct mwl8k_cmd_update_stadb {
3076 struct mwl8k_cmd_pkt header;
3077
3078 /* See STADB_ACTION_TYPE */
3079 __le32 action;
3080
3081 /* Peer MAC address */
3082 __u8 peer_addr[ETH_ALEN];
3083
3084 __le32 reserved;
3085
3086 /* Peer info - valid during add/update. */
3087 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003088} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003089
Lennert Buytenheka6804002010-01-04 21:55:42 +01003090#define MWL8K_STA_DB_MODIFY_ENTRY 1
3091#define MWL8K_STA_DB_DEL_ENTRY 2
3092
3093/* Peer Entry flags - used to define the type of the peer node */
3094#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3095
3096static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003097 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003098 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003099{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003100 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003101 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003102 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003103 int rc;
3104
3105 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3106 if (cmd == NULL)
3107 return -ENOMEM;
3108
3109 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3110 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003111 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003112 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003113
Lennert Buytenheka6804002010-01-04 21:55:42 +01003114 p = &cmd->peer_info;
3115 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3116 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003117 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003118 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003119 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3120 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003121 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3122 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3123 else
3124 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3125 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003126 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003127 p->interop = 1;
3128 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003129
Lennert Buytenheka6804002010-01-04 21:55:42 +01003130 rc = mwl8k_post_cmd(hw, &cmd->header);
3131 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003132
Lennert Buytenheka6804002010-01-04 21:55:42 +01003133 return rc ? rc : p->station_id;
3134}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003135
Lennert Buytenheka6804002010-01-04 21:55:42 +01003136static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3137 struct ieee80211_vif *vif, u8 *addr)
3138{
3139 struct mwl8k_cmd_update_stadb *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_UPDATE_STADB);
3147 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3148 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3149 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3150
3151 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003152 kfree(cmd);
3153
3154 return rc;
3155}
3156
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003157
3158/*
3159 * Interrupt handling.
3160 */
3161static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3162{
3163 struct ieee80211_hw *hw = dev_id;
3164 struct mwl8k_priv *priv = hw->priv;
3165 u32 status;
3166
3167 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003168 if (!status)
3169 return IRQ_NONE;
3170
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003171 if (status & MWL8K_A2H_INT_TX_DONE) {
3172 status &= ~MWL8K_A2H_INT_TX_DONE;
3173 tasklet_schedule(&priv->poll_tx_task);
3174 }
3175
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003176 if (status & MWL8K_A2H_INT_RX_READY) {
3177 status &= ~MWL8K_A2H_INT_RX_READY;
3178 tasklet_schedule(&priv->poll_rx_task);
3179 }
3180
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003181 if (status)
3182 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003183
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003184 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003185 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003186 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003187 }
3188
3189 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003190 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003191 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003192 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003193 }
3194
3195 return IRQ_HANDLED;
3196}
3197
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003198static void mwl8k_tx_poll(unsigned long data)
3199{
3200 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3201 struct mwl8k_priv *priv = hw->priv;
3202 int limit;
3203 int i;
3204
3205 limit = 32;
3206
3207 spin_lock_bh(&priv->tx_lock);
3208
3209 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3210 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3211
3212 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3213 complete(priv->tx_wait);
3214 priv->tx_wait = NULL;
3215 }
3216
3217 spin_unlock_bh(&priv->tx_lock);
3218
3219 if (limit) {
3220 writel(~MWL8K_A2H_INT_TX_DONE,
3221 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3222 } else {
3223 tasklet_schedule(&priv->poll_tx_task);
3224 }
3225}
3226
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003227static void mwl8k_rx_poll(unsigned long data)
3228{
3229 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3230 struct mwl8k_priv *priv = hw->priv;
3231 int limit;
3232
3233 limit = 32;
3234 limit -= rxq_process(hw, 0, limit);
3235 limit -= rxq_refill(hw, 0, limit);
3236
3237 if (limit) {
3238 writel(~MWL8K_A2H_INT_RX_READY,
3239 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3240 } else {
3241 tasklet_schedule(&priv->poll_rx_task);
3242 }
3243}
3244
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003245
3246/*
3247 * Core driver operations.
3248 */
3249static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3250{
3251 struct mwl8k_priv *priv = hw->priv;
3252 int index = skb_get_queue_mapping(skb);
3253 int rc;
3254
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003255 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003256 wiphy_debug(hw->wiphy,
3257 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003258 dev_kfree_skb(skb);
3259 return NETDEV_TX_OK;
3260 }
3261
3262 rc = mwl8k_txq_xmit(hw, index, skb);
3263
3264 return rc;
3265}
3266
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003267static int mwl8k_start(struct ieee80211_hw *hw)
3268{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003269 struct mwl8k_priv *priv = hw->priv;
3270 int rc;
3271
Joe Perchesa0607fd2009-11-18 23:29:17 -08003272 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003273 IRQF_SHARED, MWL8K_NAME, hw);
3274 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07003275 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003276 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003277 }
3278
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003279 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003280 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003281 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003282
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003283 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003284 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003285
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003286 rc = mwl8k_fw_lock(hw);
3287 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003288 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003289
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003290 if (!priv->ap_fw) {
3291 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003292 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003293
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003294 if (!rc)
3295 rc = mwl8k_cmd_set_pre_scan(hw);
3296
3297 if (!rc)
3298 rc = mwl8k_cmd_set_post_scan(hw,
3299 "\x00\x00\x00\x00\x00\x00");
3300 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003301
3302 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003303 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003304
3305 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003306 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003307
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003308 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003309 }
3310
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003311 if (rc) {
3312 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3313 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003314 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003315 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003316 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003317
3318 return rc;
3319}
3320
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003321static void mwl8k_stop(struct ieee80211_hw *hw)
3322{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003323 struct mwl8k_priv *priv = hw->priv;
3324 int i;
3325
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003326 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003327
3328 ieee80211_stop_queues(hw);
3329
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003330 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003331 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003332 free_irq(priv->pdev->irq, hw);
3333
3334 /* Stop finalize join worker */
3335 cancel_work_sync(&priv->finalize_join_worker);
3336 if (priv->beacon_skb != NULL)
3337 dev_kfree_skb(priv->beacon_skb);
3338
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003339 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003340 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003341 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003342
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003343 /* Return all skbs to mac80211 */
3344 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003345 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003346}
3347
3348static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003349 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003350{
3351 struct mwl8k_priv *priv = hw->priv;
3352 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003353 u32 macids_supported;
3354 int macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003355
3356 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003357 * Reject interface creation if sniffer mode is active, as
3358 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003359 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003360 */
3361 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003362 wiphy_info(hw->wiphy,
3363 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003364 return -EINVAL;
3365 }
3366
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003367
3368 switch (vif->type) {
3369 case NL80211_IFTYPE_AP:
3370 macids_supported = priv->ap_macids_supported;
3371 break;
3372 case NL80211_IFTYPE_STATION:
3373 macids_supported = priv->sta_macids_supported;
3374 break;
3375 default:
3376 return -EINVAL;
3377 }
3378
3379 macid = ffs(macids_supported & ~priv->macids_used);
3380 if (!macid--)
3381 return -EBUSY;
3382
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003383 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003384 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003385 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003386 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003387 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003388 mwl8k_vif->seqno = 0;
3389
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003390 /* Set the mac address. */
3391 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3392
3393 if (priv->ap_fw)
3394 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3395
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003396 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003397 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003398
3399 return 0;
3400}
3401
3402static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003403 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003404{
3405 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003406 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003407
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003408 if (priv->ap_fw)
3409 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3410
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003411 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003412
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003413 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003414 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003415}
3416
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003417static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003418{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003419 struct ieee80211_conf *conf = &hw->conf;
3420 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003421 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003422
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003423 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003424 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003425 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003426 }
3427
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003428 rc = mwl8k_fw_lock(hw);
3429 if (rc)
3430 return rc;
3431
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003432 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003433 if (rc)
3434 goto out;
3435
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003436 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003437 if (rc)
3438 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003439
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003440 if (conf->power_level > 18)
3441 conf->power_level = 18;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003442
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003443 if (priv->ap_fw) {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003444 rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
3445 if (rc)
3446 goto out;
3447
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003448 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3449 if (!rc)
3450 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3451 } else {
Nishant Sarmukadam41fdf092010-11-12 17:23:48 -08003452 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
3453 if (rc)
3454 goto out;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003455 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3456 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003457
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003458out:
3459 mwl8k_fw_unlock(hw);
3460
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003461 return rc;
3462}
3463
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003464static void
3465mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3466 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003467{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003468 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003469 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003470 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003471 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003472
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003473 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003474 return;
3475
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003476 /*
3477 * No need to capture a beacon if we're no longer associated.
3478 */
3479 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3480 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003481
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003482 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003483 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003484 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003485 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003486 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003487
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003488 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003489
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003490 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003491 if (ap == NULL) {
3492 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003493 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003494 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003495
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003496 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
3497 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
3498 } else {
3499 ap_legacy_rates =
3500 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3501 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003502 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003503
3504 rcu_read_unlock();
3505 }
3506
3507 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003508 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003509 if (rc)
3510 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003511
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003512 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003513 if (rc)
3514 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003515 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003516
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003517 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003518 rc = mwl8k_set_radio_preamble(hw,
3519 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003520 if (rc)
3521 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003522 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003523
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003524 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003525 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003526 if (rc)
3527 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003528 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003529
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003530 if (vif->bss_conf.assoc &&
3531 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
3532 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003533 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003534 if (rc)
3535 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003536 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003537
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003538 if (vif->bss_conf.assoc &&
3539 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003540 /*
3541 * Finalize the join. Tell rx handler to process
3542 * next beacon from our BSSID.
3543 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003544 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003545 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003546 }
3547
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003548out:
3549 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003550}
3551
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003552static void
3553mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3554 struct ieee80211_bss_conf *info, u32 changed)
3555{
3556 int rc;
3557
3558 if (mwl8k_fw_lock(hw))
3559 return;
3560
3561 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3562 rc = mwl8k_set_radio_preamble(hw,
3563 vif->bss_conf.use_short_preamble);
3564 if (rc)
3565 goto out;
3566 }
3567
3568 if (changed & BSS_CHANGED_BASIC_RATES) {
3569 int idx;
3570 int rate;
3571
3572 /*
3573 * Use lowest supported basic rate for multicasts
3574 * and management frames (such as probe responses --
3575 * beacons will always go out at 1 Mb/s).
3576 */
3577 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003578 if (idx)
3579 idx--;
3580
3581 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3582 rate = mwl8k_rates_24[idx].hw_value;
3583 else
3584 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003585
3586 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
3587 }
3588
3589 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
3590 struct sk_buff *skb;
3591
3592 skb = ieee80211_beacon_get(hw, vif);
3593 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003594 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003595 kfree_skb(skb);
3596 }
3597 }
3598
3599 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003600 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003601
3602out:
3603 mwl8k_fw_unlock(hw);
3604}
3605
3606static void
3607mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3608 struct ieee80211_bss_conf *info, u32 changed)
3609{
3610 struct mwl8k_priv *priv = hw->priv;
3611
3612 if (!priv->ap_fw)
3613 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
3614 else
3615 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
3616}
3617
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003618static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad32010-04-01 21:22:57 +00003619 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003620{
3621 struct mwl8k_cmd_pkt *cmd;
3622
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003623 /*
3624 * Synthesize and return a command packet that programs the
3625 * hardware multicast address filter. At this point we don't
3626 * know whether FIF_ALLMULTI is being requested, but if it is,
3627 * we'll end up throwing this packet away and creating a new
3628 * one in mwl8k_configure_filter().
3629 */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003630 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003631
3632 return (unsigned long)cmd;
3633}
3634
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003635static int
3636mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3637 unsigned int changed_flags,
3638 unsigned int *total_flags)
3639{
3640 struct mwl8k_priv *priv = hw->priv;
3641
3642 /*
3643 * Hardware sniffer mode is mutually exclusive with STA
3644 * operation, so refuse to enable sniffer mode if a STA
3645 * interface is active.
3646 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003647 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003648 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07003649 wiphy_info(hw->wiphy,
3650 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003651 return 0;
3652 }
3653
3654 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003655 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003656 return 0;
3657 priv->sniffer_enabled = true;
3658 }
3659
3660 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3661 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3662 FIF_OTHER_BSS;
3663
3664 return 1;
3665}
3666
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003667static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
3668{
3669 if (!list_empty(&priv->vif_list))
3670 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
3671
3672 return NULL;
3673}
3674
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003675static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3676 unsigned int changed_flags,
3677 unsigned int *total_flags,
3678 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003679{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003680 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003681 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3682
3683 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003684 * AP firmware doesn't allow fine-grained control over
3685 * the receive filter.
3686 */
3687 if (priv->ap_fw) {
3688 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3689 kfree(cmd);
3690 return;
3691 }
3692
3693 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003694 * Enable hardware sniffer mode if FIF_CONTROL or
3695 * FIF_OTHER_BSS is requested.
3696 */
3697 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3698 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3699 kfree(cmd);
3700 return;
3701 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003702
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003703 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003704 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003705
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003706 if (mwl8k_fw_lock(hw)) {
3707 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003708 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003709 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003710
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003711 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003712 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003713 priv->sniffer_enabled = false;
3714 }
3715
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003716 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003717 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3718 /*
3719 * Disable the BSS filter.
3720 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003721 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003722 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003723 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003724 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003725
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003726 /*
3727 * Enable the BSS filter.
3728 *
3729 * If there is an active STA interface, use that
3730 * interface's BSSID, otherwise use a dummy one
3731 * (where the OUI part needs to be nonzero for
3732 * the BSSID to be accepted by POST_SCAN).
3733 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003734 mwl8k_vif = mwl8k_first_vif(priv);
3735 if (mwl8k_vif != NULL)
3736 bssid = mwl8k_vif->vif->bss_conf.bssid;
3737 else
3738 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003739
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003740 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003741 }
3742 }
3743
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003744 /*
3745 * If FIF_ALLMULTI is being requested, throw away the command
3746 * packet that ->prepare_multicast() built and replace it with
3747 * a command packet that enables reception of all multicast
3748 * packets.
3749 */
3750 if (*total_flags & FIF_ALLMULTI) {
3751 kfree(cmd);
Jiri Pirko22bedad32010-04-01 21:22:57 +00003752 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003753 }
3754
3755 if (cmd != NULL) {
3756 mwl8k_post_cmd(hw, cmd);
3757 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003758 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003759
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003760 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003761}
3762
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003763static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3764{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003765 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003766}
3767
Johannes Berg4a6967b2010-02-19 19:18:37 +01003768static int mwl8k_sta_remove(struct ieee80211_hw *hw,
3769 struct ieee80211_vif *vif,
3770 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003771{
3772 struct mwl8k_priv *priv = hw->priv;
3773
Johannes Berg4a6967b2010-02-19 19:18:37 +01003774 if (priv->ap_fw)
3775 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
3776 else
3777 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
3778}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003779
Johannes Berg4a6967b2010-02-19 19:18:37 +01003780static int mwl8k_sta_add(struct ieee80211_hw *hw,
3781 struct ieee80211_vif *vif,
3782 struct ieee80211_sta *sta)
3783{
3784 struct mwl8k_priv *priv = hw->priv;
3785 int ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003786
Johannes Berg4a6967b2010-02-19 19:18:37 +01003787 if (!priv->ap_fw) {
3788 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
3789 if (ret >= 0) {
3790 MWL8K_STA(sta)->peer_id = ret;
3791 return 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003792 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01003793
3794 return ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003795 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003796
Johannes Berg4a6967b2010-02-19 19:18:37 +01003797 return mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003798}
3799
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003800static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3801 const struct ieee80211_tx_queue_params *params)
3802{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003803 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003804 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003805
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003806 rc = mwl8k_fw_lock(hw);
3807 if (!rc) {
3808 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003809 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003810
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003811 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003812 rc = mwl8k_cmd_set_edca_params(hw, queue,
3813 params->cw_min,
3814 params->cw_max,
3815 params->aifs,
3816 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003817
3818 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003819 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003820
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003821 return rc;
3822}
3823
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003824static int mwl8k_get_stats(struct ieee80211_hw *hw,
3825 struct ieee80211_low_level_stats *stats)
3826{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003827 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003828}
3829
John W. Linville0d462bb2010-07-28 14:04:24 -04003830static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
3831 struct survey_info *survey)
3832{
3833 struct mwl8k_priv *priv = hw->priv;
3834 struct ieee80211_conf *conf = &hw->conf;
3835
3836 if (idx != 0)
3837 return -ENOENT;
3838
3839 survey->channel = conf->channel;
3840 survey->filled = SURVEY_INFO_NOISE_DBM;
3841 survey->noise = priv->noise;
3842
3843 return 0;
3844}
3845
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003846static int
3847mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3848 enum ieee80211_ampdu_mlme_action action,
3849 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3850{
3851 switch (action) {
3852 case IEEE80211_AMPDU_RX_START:
3853 case IEEE80211_AMPDU_RX_STOP:
3854 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3855 return -ENOTSUPP;
3856 return 0;
3857 default:
3858 return -ENOTSUPP;
3859 }
3860}
3861
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003862static const struct ieee80211_ops mwl8k_ops = {
3863 .tx = mwl8k_tx,
3864 .start = mwl8k_start,
3865 .stop = mwl8k_stop,
3866 .add_interface = mwl8k_add_interface,
3867 .remove_interface = mwl8k_remove_interface,
3868 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003869 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003870 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003871 .configure_filter = mwl8k_configure_filter,
3872 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01003873 .sta_add = mwl8k_sta_add,
3874 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003875 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003876 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04003877 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003878 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003879};
3880
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003881static void mwl8k_finalize_join_worker(struct work_struct *work)
3882{
3883 struct mwl8k_priv *priv =
3884 container_of(work, struct mwl8k_priv, finalize_join_worker);
3885 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01003886 struct ieee80211_mgmt *mgmt = (void *)skb->data;
3887 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
3888 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
3889 mgmt->u.beacon.variable, len);
3890 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003891
Johannes Berg56007a02010-01-26 14:19:52 +01003892 if (tim && tim[1] >= 2)
3893 dtim_period = tim[3];
3894
3895 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003896
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003897 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003898 priv->beacon_skb = NULL;
3899}
3900
John W. Linvillebcb628d2009-11-06 16:40:16 -05003901enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003902 MWL8363 = 0,
3903 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003904 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003905};
3906
John W. Linvillebcb628d2009-11-06 16:40:16 -05003907static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003908 [MWL8363] = {
3909 .part_name = "88w8363",
3910 .helper_image = "mwl8k/helper_8363.fw",
3911 .fw_image = "mwl8k/fmimage_8363.fw",
3912 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003913 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003914 .part_name = "88w8687",
3915 .helper_image = "mwl8k/helper_8687.fw",
3916 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003917 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003918 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003919 .part_name = "88w8366",
3920 .helper_image = "mwl8k/helper_8366.fw",
3921 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003922 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003923 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003924};
3925
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01003926MODULE_FIRMWARE("mwl8k/helper_8363.fw");
3927MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
3928MODULE_FIRMWARE("mwl8k/helper_8687.fw");
3929MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
3930MODULE_FIRMWARE("mwl8k/helper_8366.fw");
3931MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
3932
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003933static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01003934 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003935 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3936 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003937 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3938 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3939 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01003940 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003941 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003942};
3943MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3944
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003945static int __devinit mwl8k_probe(struct pci_dev *pdev,
3946 const struct pci_device_id *id)
3947{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003948 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003949 struct ieee80211_hw *hw;
3950 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003951 int rc;
3952 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003953
3954 if (!printed_version) {
3955 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3956 printed_version = 1;
3957 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003958
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003959
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003960 rc = pci_enable_device(pdev);
3961 if (rc) {
3962 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3963 MWL8K_NAME);
3964 return rc;
3965 }
3966
3967 rc = pci_request_regions(pdev, MWL8K_NAME);
3968 if (rc) {
3969 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3970 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003971 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003972 }
3973
3974 pci_set_master(pdev);
3975
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003976
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003977 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3978 if (hw == NULL) {
3979 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3980 rc = -ENOMEM;
3981 goto err_free_reg;
3982 }
3983
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003984 SET_IEEE80211_DEV(hw, &pdev->dev);
3985 pci_set_drvdata(pdev, hw);
3986
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003987 priv = hw->priv;
3988 priv->hw = hw;
3989 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003990 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003991
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003992
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003993 priv->sram = pci_iomap(pdev, 0, 0x10000);
3994 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07003995 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003996 goto err_iounmap;
3997 }
3998
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003999 /*
4000 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
4001 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
4002 */
4003 priv->regs = pci_iomap(pdev, 1, 0x10000);
4004 if (priv->regs == NULL) {
4005 priv->regs = pci_iomap(pdev, 2, 0x10000);
4006 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07004007 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004008 goto err_iounmap;
4009 }
4010 }
4011
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004012
4013 /* Reset firmware and hardware */
4014 mwl8k_hw_reset(priv);
4015
4016 /* Ask userland hotplug daemon for the device firmware */
4017 rc = mwl8k_request_firmware(priv);
4018 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004019 wiphy_err(hw->wiphy, "Firmware files not found\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004020 goto err_stop_firmware;
4021 }
4022
4023 /* Load firmware into hardware */
4024 rc = mwl8k_load_firmware(hw);
4025 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004026 wiphy_err(hw->wiphy, "Cannot start firmware\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004027 goto err_stop_firmware;
4028 }
4029
4030 /* Reclaim memory once firmware is successfully loaded */
4031 mwl8k_release_firmware(priv);
4032
4033
Lennert Buytenhek91942232010-01-04 21:53:54 +01004034 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004035 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01004036 if (priv->rxd_ops == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07004037 wiphy_err(hw->wiphy,
4038 "Driver does not have AP firmware image support for this hardware\n");
Lennert Buytenhek91942232010-01-04 21:53:54 +01004039 goto err_stop_firmware;
4040 }
4041 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004042 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01004043 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004044
4045 priv->sniffer_enabled = false;
4046 priv->wmm_enabled = false;
4047 priv->pending_tx_pkts = 0;
4048
4049
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004050 /*
4051 * Extra headroom is the size of the required DMA header
4052 * minus the size of the smallest 802.11 frame (CTS frame).
4053 */
4054 hw->extra_tx_headroom =
4055 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
4056
4057 hw->channel_change_time = 10;
4058
4059 hw->queues = MWL8K_TX_QUEUES;
4060
John W. Linvillef5c044e2010-04-30 15:37:00 -04004061 /* Set rssi values to dBm */
4062 hw->flags |= IEEE80211_HW_SIGNAL_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004063 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004064 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004065
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004066 priv->macids_used = 0;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004067 INIT_LIST_HEAD(&priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004068
4069 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02004070 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02004071 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004072
4073 /* Finalize join worker */
4074 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4075
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004076 /* TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004077 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4078 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004079 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4080 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004081
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004082 /* Power management cookie */
4083 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4084 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004085 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004086
4087 rc = mwl8k_rxq_init(hw, 0);
4088 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004089 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004090 rxq_refill(hw, 0, INT_MAX);
4091
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004092 mutex_init(&priv->fw_mutex);
4093 priv->fw_mutex_owner = NULL;
4094 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004095 priv->hostcmd_wait = NULL;
4096
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004097 spin_lock_init(&priv->tx_lock);
4098
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004099 priv->tx_wait = NULL;
4100
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004101 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4102 rc = mwl8k_txq_init(hw, i);
4103 if (rc)
4104 goto err_free_queues;
4105 }
4106
4107 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004108 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004109 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004110 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004111 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4112
Joe Perchesa0607fd2009-11-18 23:29:17 -08004113 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004114 IRQF_SHARED, MWL8K_NAME, hw);
4115 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004116 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004117 goto err_free_queues;
4118 }
4119
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004120 /*
4121 * Temporarily enable interrupts. Initial firmware host
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004122 * commands use interrupts and avoid polling. Disable
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004123 * interrupts when done.
4124 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004125 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004126
4127 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02004128 if (priv->ap_fw) {
4129 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4130 if (!rc)
4131 rc = mwl8k_cmd_set_hw_spec(hw);
4132 } else {
4133 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4134 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004135 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004136 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004137 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004138 }
4139
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004140 hw->wiphy->interface_modes = 0;
4141 if (priv->ap_macids_supported)
4142 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4143 if (priv->sta_macids_supported)
4144 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4145
4146
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004147 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004148 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004149 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004150 wiphy_err(hw->wiphy, "Cannot disable\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004151 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004152 }
4153
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004154 /* Clear MAC address */
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004155 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004156 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004157 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004158 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004159 }
4160
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004161 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004162 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004163 free_irq(priv->pdev->irq, hw);
4164
4165 rc = ieee80211_register_hw(hw);
4166 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004167 wiphy_err(hw->wiphy, "Cannot register device\n");
Lennert Buytenhek153458f2010-01-04 21:54:08 +01004168 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004169 }
4170
Joe Perchesc96c31e2010-07-26 14:39:58 -07004171 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
4172 priv->device_info->part_name,
4173 priv->hw_rev, hw->wiphy->perm_addr,
4174 priv->ap_fw ? "AP" : "STA",
4175 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4176 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004177
4178 return 0;
4179
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004180err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004181 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004182 free_irq(priv->pdev->irq, hw);
4183
4184err_free_queues:
4185 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4186 mwl8k_txq_deinit(hw, i);
4187 mwl8k_rxq_deinit(hw, 0);
4188
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004189err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004190 if (priv->cookie != NULL)
4191 pci_free_consistent(priv->pdev, 4,
4192 priv->cookie, priv->cookie_dma);
4193
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004194err_stop_firmware:
4195 mwl8k_hw_reset(priv);
4196 mwl8k_release_firmware(priv);
4197
4198err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004199 if (priv->regs != NULL)
4200 pci_iounmap(pdev, priv->regs);
4201
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004202 if (priv->sram != NULL)
4203 pci_iounmap(pdev, priv->sram);
4204
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004205 pci_set_drvdata(pdev, NULL);
4206 ieee80211_free_hw(hw);
4207
4208err_free_reg:
4209 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004210
4211err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004212 pci_disable_device(pdev);
4213
4214 return rc;
4215}
4216
Joerg Albert230f7af2009-04-18 02:10:45 +02004217static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004218{
4219 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4220}
4221
Joerg Albert230f7af2009-04-18 02:10:45 +02004222static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004223{
4224 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4225 struct mwl8k_priv *priv;
4226 int i;
4227
4228 if (hw == NULL)
4229 return;
4230 priv = hw->priv;
4231
4232 ieee80211_stop_queues(hw);
4233
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004234 ieee80211_unregister_hw(hw);
4235
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004236 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004237 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004238 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004239
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004240 /* Stop hardware */
4241 mwl8k_hw_reset(priv);
4242
4243 /* Return all skbs to mac80211 */
4244 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004245 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004246
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004247 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4248 mwl8k_txq_deinit(hw, i);
4249
4250 mwl8k_rxq_deinit(hw, 0);
4251
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004252 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004253
4254 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004255 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004256 pci_set_drvdata(pdev, NULL);
4257 ieee80211_free_hw(hw);
4258 pci_release_regions(pdev);
4259 pci_disable_device(pdev);
4260}
4261
4262static struct pci_driver mwl8k_driver = {
4263 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004264 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004265 .probe = mwl8k_probe,
4266 .remove = __devexit_p(mwl8k_remove),
4267 .shutdown = __devexit_p(mwl8k_shutdown),
4268};
4269
4270static int __init mwl8k_init(void)
4271{
4272 return pci_register_driver(&mwl8k_driver);
4273}
4274
4275static void __exit mwl8k_exit(void)
4276{
4277 pci_unregister_driver(&mwl8k_driver);
4278}
4279
4280module_init(mwl8k_init);
4281module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004282
4283MODULE_DESCRIPTION(MWL8K_DESC);
4284MODULE_VERSION(MWL8K_VERSION);
4285MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
4286MODULE_LICENSE("GPL");