blob: 0cfdb9db66f7bd2aef8d0b097e005c40c45fd3ef [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>
22#include <net/mac80211.h>
23#include <linux/moduleparam.h>
24#include <linux/firmware.h>
25#include <linux/workqueue.h>
26
27#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
28#define MWL8K_NAME KBUILD_MODNAME
Lennert Buytenheka5fb2972010-01-12 13:51:38 +010029#define MWL8K_VERSION "0.12"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010030
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031/* Register definitions */
32#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020033#define MWL8K_MODE_STA 0x0000005a
34#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010035#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020036#define MWL8K_FWSTA_READY 0xf0f1f2f4
37#define MWL8K_FWAP_READY 0xf1f2f4a5
38#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010039#define MWL8K_HIU_SCRATCH 0x00000c40
40
41/* Host->device communications */
42#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
43#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
44#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
45#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
46#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020047#define MWL8K_H2A_INT_DUMMY (1 << 20)
48#define MWL8K_H2A_INT_RESET (1 << 15)
49#define MWL8K_H2A_INT_DOORBELL (1 << 1)
50#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010051
52/* Device->host communications */
53#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
54#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
55#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
56#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
57#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020058#define MWL8K_A2H_INT_DUMMY (1 << 20)
59#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
60#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
61#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
62#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
63#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
64#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
65#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
66#define MWL8K_A2H_INT_RX_READY (1 << 1)
67#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010068
69#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
70 MWL8K_A2H_INT_CHNL_SWITCHED | \
71 MWL8K_A2H_INT_QUEUE_EMPTY | \
72 MWL8K_A2H_INT_RADAR_DETECT | \
73 MWL8K_A2H_INT_RADIO_ON | \
74 MWL8K_A2H_INT_RADIO_OFF | \
75 MWL8K_A2H_INT_MAC_EVENT | \
76 MWL8K_A2H_INT_OPC_DONE | \
77 MWL8K_A2H_INT_RX_READY | \
78 MWL8K_A2H_INT_TX_DONE)
79
Lennert Buytenheka66098d2009-03-10 10:13:33 +010080#define MWL8K_RX_QUEUES 1
81#define MWL8K_TX_QUEUES 4
82
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020083struct rxd_ops {
84 int rxd_size;
85 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
86 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010087 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
88 __le16 *qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020089};
90
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020091struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020092 char *part_name;
93 char *helper_image;
94 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010095 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020096};
97
Lennert Buytenheka66098d2009-03-10 10:13:33 +010098struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +020099 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100100
101 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200102 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100103
104 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200105 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100106
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200107 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200108 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200109 struct {
110 struct sk_buff *skb;
111 DECLARE_PCI_UNMAP_ADDR(dma)
112 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100113};
114
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100115struct mwl8k_tx_queue {
116 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200117 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100118
119 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200120 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100121
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200122 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200123 struct mwl8k_tx_desc *txd;
124 dma_addr_t txd_dma;
125 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100126};
127
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100128struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200132 struct mwl8k_device_info *device_info;
133
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100134 void __iomem *sram;
135 void __iomem *regs;
136
137 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100138 struct firmware *fw_helper;
139 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100140
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100141 /* hardware/firmware parameters */
142 bool ap_fw;
143 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100144 struct ieee80211_supported_band band_24;
145 struct ieee80211_channel channels_24[14];
146 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100147 struct ieee80211_supported_band band_50;
148 struct ieee80211_channel channels_50[4];
149 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100150 u32 ap_macids_supported;
151 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100152
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200153 /* firmware access */
154 struct mutex fw_mutex;
155 struct task_struct *fw_mutex_owner;
156 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200157 struct completion *hostcmd_wait;
158
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100159 /* lock held over TX and TX reap */
160 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100161
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200162 /* TX quiesce completion, protected by fw_mutex and tx_lock */
163 struct completion *tx_wait;
164
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100165 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100166 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100167 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100168
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100169 /* power management status cookie from firmware */
170 u32 *cookie;
171 dma_addr_t cookie_dma;
172
173 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100174 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200175 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100176
177 /*
178 * Running count of TX packets in flight, to avoid
179 * iterating over the transmit rings each time.
180 */
181 int pending_tx_pkts;
182
183 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
184 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
185
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200186 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200187 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200188 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200189 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100190
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +0100191 struct work_struct sta_notify_worker;
192 spinlock_t sta_notify_list_lock;
193 struct list_head sta_notify_list;
194
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 /* XXX need to convert this to handle multiple interfaces */
196 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200197 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100198 struct sk_buff *beacon_skb;
199
200 /*
201 * This FJ worker has to be global as it is scheduled from the
202 * RX handler. At this point we don't know which interface it
203 * belongs to until the list of bssids waiting to complete join
204 * is checked.
205 */
206 struct work_struct finalize_join_worker;
207
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100208 /* Tasklet to perform TX reclaim. */
209 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100210
211 /* Tasklet to perform RX. */
212 struct tasklet_struct poll_rx_task;
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 */
288#define MWL8K_CMD_SET 0x0001
289#define MWL8K_CMD_GET 0x0000
290
291/* Firmware command codes */
292#define MWL8K_CMD_CODE_DNLD 0x0001
293#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200294#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100295#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
296#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200297#define MWL8K_CMD_RADIO_CONTROL 0x001c
298#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200299#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100300#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301#define MWL8K_CMD_SET_PRE_SCAN 0x0107
302#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200303#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100304#define MWL8K_CMD_SET_AID 0x010d
305#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200306#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100307#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200308#define MWL8K_CMD_SET_SLOT 0x0114
309#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
310#define MWL8K_CMD_SET_WMM_MODE 0x0123
311#define MWL8K_CMD_MIMO_CONFIG 0x0125
312#define MWL8K_CMD_USE_FIXED_RATE 0x0126
313#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100314#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200315#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100316#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
317#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200318#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100319
320static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
321{
322#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
323 snprintf(buf, bufsize, "%s", #x);\
324 return buf;\
325 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200326 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100327 MWL8K_CMDNAME(CODE_DNLD);
328 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200329 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100330 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
331 MWL8K_CMDNAME(GET_STAT);
332 MWL8K_CMDNAME(RADIO_CONTROL);
333 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200334 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100335 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100336 MWL8K_CMDNAME(SET_PRE_SCAN);
337 MWL8K_CMDNAME(SET_POST_SCAN);
338 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100339 MWL8K_CMDNAME(SET_AID);
340 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200341 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100342 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200343 MWL8K_CMDNAME(SET_SLOT);
344 MWL8K_CMDNAME(SET_EDCA_PARAMS);
345 MWL8K_CMDNAME(SET_WMM_MODE);
346 MWL8K_CMDNAME(MIMO_CONFIG);
347 MWL8K_CMDNAME(USE_FIXED_RATE);
348 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200349 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200350 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100351 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100352 MWL8K_CMDNAME(SET_NEW_STN);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200353 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100354 default:
355 snprintf(buf, bufsize, "0x%x", cmd);
356 }
357#undef MWL8K_CMDNAME
358
359 return buf;
360}
361
362/* Hardware and firmware reset */
363static void mwl8k_hw_reset(struct mwl8k_priv *priv)
364{
365 iowrite32(MWL8K_H2A_INT_RESET,
366 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
367 iowrite32(MWL8K_H2A_INT_RESET,
368 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
369 msleep(20);
370}
371
372/* Release fw image */
373static void mwl8k_release_fw(struct firmware **fw)
374{
375 if (*fw == NULL)
376 return;
377 release_firmware(*fw);
378 *fw = NULL;
379}
380
381static void mwl8k_release_firmware(struct mwl8k_priv *priv)
382{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100383 mwl8k_release_fw(&priv->fw_ucode);
384 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100385}
386
387/* Request fw image */
388static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200389 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100390{
391 /* release current image */
392 if (*fw != NULL)
393 mwl8k_release_fw(fw);
394
395 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200396 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100397}
398
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200399static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100400{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200401 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100402 int rc;
403
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200404 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100405 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200406 if (rc) {
407 printk(KERN_ERR "%s: Error requesting helper "
408 "firmware file %s\n", pci_name(priv->pdev),
409 di->helper_image);
410 return rc;
411 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100412 }
413
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100414 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100415 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200416 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200417 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100418 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100419 return rc;
420 }
421
422 return 0;
423}
424
425struct mwl8k_cmd_pkt {
426 __le16 code;
427 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100428 __u8 seq_num;
429 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100430 __le16 result;
431 char payload[0];
432} __attribute__((packed));
433
434/*
435 * Firmware loading.
436 */
437static int
438mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
439{
440 void __iomem *regs = priv->regs;
441 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100442 int loops;
443
444 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
445 if (pci_dma_mapping_error(priv->pdev, dma_addr))
446 return -ENOMEM;
447
448 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
449 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
450 iowrite32(MWL8K_H2A_INT_DOORBELL,
451 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
452 iowrite32(MWL8K_H2A_INT_DUMMY,
453 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
454
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100455 loops = 1000;
456 do {
457 u32 int_code;
458
459 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
460 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
461 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100462 break;
463 }
464
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200465 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100466 udelay(1);
467 } while (--loops);
468
469 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
470
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200471 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100472}
473
474static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
475 const u8 *data, size_t length)
476{
477 struct mwl8k_cmd_pkt *cmd;
478 int done;
479 int rc = 0;
480
481 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
482 if (cmd == NULL)
483 return -ENOMEM;
484
485 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
486 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100487 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100488 cmd->result = 0;
489
490 done = 0;
491 while (length) {
492 int block_size = length > 256 ? 256 : length;
493
494 memcpy(cmd->payload, data + done, block_size);
495 cmd->length = cpu_to_le16(block_size);
496
497 rc = mwl8k_send_fw_load_cmd(priv, cmd,
498 sizeof(*cmd) + block_size);
499 if (rc)
500 break;
501
502 done += block_size;
503 length -= block_size;
504 }
505
506 if (!rc) {
507 cmd->length = 0;
508 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
509 }
510
511 kfree(cmd);
512
513 return rc;
514}
515
516static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
517 const u8 *data, size_t length)
518{
519 unsigned char *buffer;
520 int may_continue, rc = 0;
521 u32 done, prev_block_size;
522
523 buffer = kmalloc(1024, GFP_KERNEL);
524 if (buffer == NULL)
525 return -ENOMEM;
526
527 done = 0;
528 prev_block_size = 0;
529 may_continue = 1000;
530 while (may_continue > 0) {
531 u32 block_size;
532
533 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
534 if (block_size & 1) {
535 block_size &= ~1;
536 may_continue--;
537 } else {
538 done += prev_block_size;
539 length -= prev_block_size;
540 }
541
542 if (block_size > 1024 || block_size > length) {
543 rc = -EOVERFLOW;
544 break;
545 }
546
547 if (length == 0) {
548 rc = 0;
549 break;
550 }
551
552 if (block_size == 0) {
553 rc = -EPROTO;
554 may_continue--;
555 udelay(1);
556 continue;
557 }
558
559 prev_block_size = block_size;
560 memcpy(buffer, data + done, block_size);
561
562 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
563 if (rc)
564 break;
565 }
566
567 if (!rc && length != 0)
568 rc = -EREMOTEIO;
569
570 kfree(buffer);
571
572 return rc;
573}
574
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200575static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100576{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200577 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100578 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200579 int rc;
580 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100581
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200582 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100583 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100584
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200585 if (helper == NULL) {
586 printk(KERN_ERR "%s: helper image needed but none "
587 "given\n", pci_name(priv->pdev));
588 return -EINVAL;
589 }
590
591 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100592 if (rc) {
593 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200594 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100595 return rc;
596 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100597 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100598
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200599 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100600 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200601 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100602 }
603
604 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200605 printk(KERN_ERR "%s: unable to load firmware image\n",
606 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100607 return rc;
608 }
609
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100610 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100611
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100612 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100613 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200614 u32 ready_code;
615
616 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
617 if (ready_code == MWL8K_FWAP_READY) {
618 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100619 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200620 } else if (ready_code == MWL8K_FWSTA_READY) {
621 priv->ap_fw = 0;
622 break;
623 }
624
625 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100626 udelay(1);
627 } while (--loops);
628
629 return loops ? 0 : -ETIMEDOUT;
630}
631
632
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100633/* DMA header used by firmware and hardware. */
634struct mwl8k_dma_data {
635 __le16 fwlen;
636 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100637 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100638} __attribute__((packed));
639
640/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100641static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100642{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100643 struct mwl8k_dma_data *tr;
644 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100645
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100646 tr = (struct mwl8k_dma_data *)skb->data;
647 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
648
649 if (hdrlen != sizeof(tr->wh)) {
650 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
651 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
652 *((__le16 *)(tr->data - 2)) = qos;
653 } else {
654 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
655 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100656 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100657
658 if (hdrlen != sizeof(*tr))
659 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100660}
661
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200662static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100663{
664 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100665 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100666 struct mwl8k_dma_data *tr;
667
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100668 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100669 * Add a firmware DMA header; the firmware requires that we
670 * present a 2-byte payload length followed by a 4-address
671 * header (without QoS field), followed (optionally) by any
672 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100673 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100674 wh = (struct ieee80211_hdr *)skb->data;
675
676 hdrlen = ieee80211_hdrlen(wh->frame_control);
677 if (hdrlen != sizeof(*tr))
678 skb_push(skb, sizeof(*tr) - hdrlen);
679
680 if (ieee80211_is_data_qos(wh->frame_control))
681 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100682
683 tr = (struct mwl8k_dma_data *)skb->data;
684 if (wh != &tr->wh)
685 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100686 if (hdrlen != sizeof(tr->wh))
687 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100688
689 /*
690 * Firmware length is the length of the fully formed "802.11
691 * payload". That is, everything except for the 802.11 header.
692 * This includes all crypto material including the MIC.
693 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100694 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100695}
696
697
698/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100699 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200700 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100701struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200702 __le16 pkt_len;
703 __u8 sq2;
704 __u8 rate;
705 __le32 pkt_phys_addr;
706 __le32 next_rxd_phys_addr;
707 __le16 qos_control;
708 __le16 htsig2;
709 __le32 hw_rssi_info;
710 __le32 hw_noise_floor_info;
711 __u8 noise_floor;
712 __u8 pad0[3];
713 __u8 rssi;
714 __u8 rx_status;
715 __u8 channel;
716 __u8 rx_ctrl;
717} __attribute__((packed));
718
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100719#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
720#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
721#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100722
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100723#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200724
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100725static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200726{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100727 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200728
729 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100730 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200731}
732
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100733static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200734{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100735 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200736
737 rxd->pkt_len = cpu_to_le16(len);
738 rxd->pkt_phys_addr = cpu_to_le32(addr);
739 wmb();
740 rxd->rx_ctrl = 0;
741}
742
743static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100744mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
745 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200746{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100747 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200748
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100749 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200750 return -1;
751 rmb();
752
753 memset(status, 0, sizeof(*status));
754
755 status->signal = -rxd->rssi;
756 status->noise = -rxd->noise_floor;
757
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100758 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200759 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100760 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100761 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100762 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200763 } else {
764 int i;
765
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100766 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
767 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200768 status->rate_idx = i;
769 break;
770 }
771 }
772 }
773
Lennert Buytenhek85478342010-01-12 13:48:56 +0100774 if (rxd->channel > 14) {
775 status->band = IEEE80211_BAND_5GHZ;
776 if (!(status->flag & RX_FLAG_HT))
777 status->rate_idx -= 5;
778 } else {
779 status->band = IEEE80211_BAND_2GHZ;
780 }
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200781 status->freq = ieee80211_channel_to_frequency(rxd->channel);
782
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100783 *qos = rxd->qos_control;
784
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200785 return le16_to_cpu(rxd->pkt_len);
786}
787
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100788static struct rxd_ops rxd_8366_ap_ops = {
789 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
790 .rxd_init = mwl8k_rxd_8366_ap_init,
791 .rxd_refill = mwl8k_rxd_8366_ap_refill,
792 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200793};
794
795/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100796 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100797 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100798struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100799 __le16 pkt_len;
800 __u8 link_quality;
801 __u8 noise_level;
802 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200803 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100804 __le16 qos_control;
805 __le16 rate_info;
806 __le32 pad0[4];
807 __u8 rssi;
808 __u8 channel;
809 __le16 pad1;
810 __u8 rx_ctrl;
811 __u8 rx_status;
812 __u8 pad2[2];
813} __attribute__((packed));
814
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100815#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
816#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
817#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
818#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
819#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
820#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200821
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100822#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200823
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100824static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200825{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100826 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200827
828 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100829 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200830}
831
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100832static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200833{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100834 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200835
836 rxd->pkt_len = cpu_to_le16(len);
837 rxd->pkt_phys_addr = cpu_to_le32(addr);
838 wmb();
839 rxd->rx_ctrl = 0;
840}
841
842static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100843mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100844 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200845{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100846 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200847 u16 rate_info;
848
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100849 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200850 return -1;
851 rmb();
852
853 rate_info = le16_to_cpu(rxd->rate_info);
854
855 memset(status, 0, sizeof(*status));
856
857 status->signal = -rxd->rssi;
858 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100859 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
860 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200861
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100862 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200863 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100864 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200865 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100866 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200867 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100868 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200869 status->flag |= RX_FLAG_HT;
870
Lennert Buytenhek85478342010-01-12 13:48:56 +0100871 if (rxd->channel > 14) {
872 status->band = IEEE80211_BAND_5GHZ;
873 if (!(status->flag & RX_FLAG_HT))
874 status->rate_idx -= 5;
875 } else {
876 status->band = IEEE80211_BAND_2GHZ;
877 }
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200878 status->freq = ieee80211_channel_to_frequency(rxd->channel);
879
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100880 *qos = rxd->qos_control;
881
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200882 return le16_to_cpu(rxd->pkt_len);
883}
884
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100885static struct rxd_ops rxd_sta_ops = {
886 .rxd_size = sizeof(struct mwl8k_rxd_sta),
887 .rxd_init = mwl8k_rxd_sta_init,
888 .rxd_refill = mwl8k_rxd_sta_refill,
889 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200890};
891
892
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100893#define MWL8K_RX_DESCS 256
894#define MWL8K_RX_MAXSZ 3800
895
896static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
897{
898 struct mwl8k_priv *priv = hw->priv;
899 struct mwl8k_rx_queue *rxq = priv->rxq + index;
900 int size;
901 int i;
902
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200903 rxq->rxd_count = 0;
904 rxq->head = 0;
905 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100906
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200907 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100908
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200909 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
910 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100911 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200912 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100913 return -ENOMEM;
914 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200915 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100916
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200917 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
918 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100919 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200920 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200921 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100922 return -ENOMEM;
923 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200924 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100925
926 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200927 int desc_size;
928 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100929 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200930 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100931
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200932 desc_size = priv->rxd_ops->rxd_size;
933 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100934
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200935 nexti = i + 1;
936 if (nexti == MWL8K_RX_DESCS)
937 nexti = 0;
938 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
939
940 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100941 }
942
943 return 0;
944}
945
946static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
947{
948 struct mwl8k_priv *priv = hw->priv;
949 struct mwl8k_rx_queue *rxq = priv->rxq + index;
950 int refilled;
951
952 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200953 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100954 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200955 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100956 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200957 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100958
959 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
960 if (skb == NULL)
961 break;
962
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200963 addr = pci_map_single(priv->pdev, skb->data,
964 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100965
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200966 rxq->rxd_count++;
967 rx = rxq->tail++;
968 if (rxq->tail == MWL8K_RX_DESCS)
969 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200970 rxq->buf[rx].skb = skb;
971 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200972
973 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
974 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100975
976 refilled++;
977 }
978
979 return refilled;
980}
981
982/* Must be called only when the card's reception is completely halted */
983static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
984{
985 struct mwl8k_priv *priv = hw->priv;
986 struct mwl8k_rx_queue *rxq = priv->rxq + index;
987 int i;
988
989 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200990 if (rxq->buf[i].skb != NULL) {
991 pci_unmap_single(priv->pdev,
992 pci_unmap_addr(&rxq->buf[i], dma),
993 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
994 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100995
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200996 kfree_skb(rxq->buf[i].skb);
997 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100998 }
999 }
1000
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001001 kfree(rxq->buf);
1002 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001003
1004 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001005 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001006 rxq->rxd, rxq->rxd_dma);
1007 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001008}
1009
1010
1011/*
1012 * Scan a list of BSSIDs to process for finalize join.
1013 * Allows for extension to process multiple BSSIDs.
1014 */
1015static inline int
1016mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1017{
1018 return priv->capture_beacon &&
1019 ieee80211_is_beacon(wh->frame_control) &&
1020 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1021}
1022
Lennert Buytenhek37797522009-10-22 20:19:45 +02001023static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1024 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001025{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001026 struct mwl8k_priv *priv = hw->priv;
1027
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001028 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001029 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001030
1031 /*
1032 * Use GFP_ATOMIC as rxq_process is called from
1033 * the primary interrupt handler, memory allocation call
1034 * must not sleep.
1035 */
1036 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1037 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001038 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001039}
1040
1041static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1042{
1043 struct mwl8k_priv *priv = hw->priv;
1044 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1045 int processed;
1046
1047 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001048 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001049 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001050 void *rxd;
1051 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001052 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001053 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001054
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001055 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001056 if (skb == NULL)
1057 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001058
1059 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1060
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001061 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001062 if (pkt_len < 0)
1063 break;
1064
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001065 rxq->buf[rxq->head].skb = NULL;
1066
1067 pci_unmap_single(priv->pdev,
1068 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1069 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1070 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001071
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001072 rxq->head++;
1073 if (rxq->head == MWL8K_RX_DESCS)
1074 rxq->head = 0;
1075
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001076 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001077
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001078 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001079 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001080
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001081 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001082 * Check for a pending join operation. Save a
1083 * copy of the beacon and schedule a tasklet to
1084 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001085 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001086 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001087 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001088
Johannes Bergf1d58c22009-06-17 13:13:00 +02001089 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1090 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001091
1092 processed++;
1093 }
1094
1095 return processed;
1096}
1097
1098
1099/*
1100 * Packet transmission.
1101 */
1102
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001103#define MWL8K_TXD_STATUS_OK 0x00000001
1104#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1105#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1106#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001107#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001108
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001109#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1110#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1111#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1112#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1113#define MWL8K_QOS_EOSP 0x0010
1114
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001115struct mwl8k_tx_desc {
1116 __le32 status;
1117 __u8 data_rate;
1118 __u8 tx_priority;
1119 __le16 qos_control;
1120 __le32 pkt_phys_addr;
1121 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001122 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001123 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001124 __le32 reserved;
1125 __le16 rate_info;
1126 __u8 peer_id;
1127 __u8 tx_frag_cnt;
1128} __attribute__((packed));
1129
1130#define MWL8K_TX_DESCS 128
1131
1132static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1133{
1134 struct mwl8k_priv *priv = hw->priv;
1135 struct mwl8k_tx_queue *txq = priv->txq + index;
1136 int size;
1137 int i;
1138
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001139 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001140 txq->head = 0;
1141 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001142
1143 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1144
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001145 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1146 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001147 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001148 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001149 return -ENOMEM;
1150 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001151 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001152
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001153 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1154 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001155 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001156 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001157 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001158 return -ENOMEM;
1159 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001160 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001161
1162 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1163 struct mwl8k_tx_desc *tx_desc;
1164 int nexti;
1165
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001166 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001167 nexti = (i + 1) % MWL8K_TX_DESCS;
1168
1169 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001170 tx_desc->next_txd_phys_addr =
1171 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001172 }
1173
1174 return 0;
1175}
1176
1177static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1178{
1179 iowrite32(MWL8K_H2A_INT_PPA_READY,
1180 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1181 iowrite32(MWL8K_H2A_INT_DUMMY,
1182 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1183 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1184}
1185
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001186static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001187{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001188 struct mwl8k_priv *priv = hw->priv;
1189 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001190
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001191 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1192 struct mwl8k_tx_queue *txq = priv->txq + i;
1193 int fw_owned = 0;
1194 int drv_owned = 0;
1195 int unused = 0;
1196 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001197
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001198 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001199 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1200 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001201
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001202 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001203 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001204 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001206 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001207
1208 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001209 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001210 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001211
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001212 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1213 "fw_owned=%d drv_owned=%d unused=%d\n",
1214 wiphy_name(hw->wiphy), i,
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001215 txq->len, txq->head, txq->tail,
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001216 fw_owned, drv_owned, unused);
1217 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001218}
1219
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001220/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001221 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001222 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001223#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001224
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001225static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001226{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001227 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001228 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001229 int retry;
1230 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001231
1232 might_sleep();
1233
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001234 /*
1235 * The TX queues are stopped at this point, so this test
1236 * doesn't need to take ->tx_lock.
1237 */
1238 if (!priv->pending_tx_pkts)
1239 return 0;
1240
1241 retry = 0;
1242 rc = 0;
1243
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001244 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001245 priv->tx_wait = &tx_wait;
1246 while (!rc) {
1247 int oldcount;
1248 unsigned long timeout;
1249
1250 oldcount = priv->pending_tx_pkts;
1251
1252 spin_unlock_bh(&priv->tx_lock);
1253 timeout = wait_for_completion_timeout(&tx_wait,
1254 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1255 spin_lock_bh(&priv->tx_lock);
1256
1257 if (timeout) {
1258 WARN_ON(priv->pending_tx_pkts);
1259 if (retry) {
1260 printk(KERN_NOTICE "%s: tx rings drained\n",
1261 wiphy_name(hw->wiphy));
1262 }
1263 break;
1264 }
1265
1266 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001267 printk(KERN_NOTICE "%s: waiting for tx rings "
1268 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001269 wiphy_name(hw->wiphy), oldcount,
1270 priv->pending_tx_pkts);
1271 retry = 1;
1272 continue;
1273 }
1274
1275 priv->tx_wait = NULL;
1276
1277 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1278 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1279 mwl8k_dump_tx_rings(hw);
1280
1281 rc = -ETIMEDOUT;
1282 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001283 spin_unlock_bh(&priv->tx_lock);
1284
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001285 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001286}
1287
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001288#define MWL8K_TXD_SUCCESS(status) \
1289 ((status) & (MWL8K_TXD_STATUS_OK | \
1290 MWL8K_TXD_STATUS_OK_RETRY | \
1291 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001292
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001293static int
1294mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001295{
1296 struct mwl8k_priv *priv = hw->priv;
1297 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001298 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001300 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001301 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001302 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303 struct mwl8k_tx_desc *tx_desc;
1304 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001305 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001306 struct sk_buff *skb;
1307 struct ieee80211_tx_info *info;
1308 u32 status;
1309
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001310 tx = txq->head;
1311 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001312
1313 status = le32_to_cpu(tx_desc->status);
1314
1315 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1316 if (!force)
1317 break;
1318 tx_desc->status &=
1319 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1320 }
1321
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001322 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001323 BUG_ON(txq->len == 0);
1324 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001325 priv->pending_tx_pkts--;
1326
1327 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001328 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001329 skb = txq->skb[tx];
1330 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001331
1332 BUG_ON(skb == NULL);
1333 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1334
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001335 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001336
1337 /* Mark descriptor as unused */
1338 tx_desc->pkt_phys_addr = 0;
1339 tx_desc->pkt_len = 0;
1340
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341 info = IEEE80211_SKB_CB(skb);
1342 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001343 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001344 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345
1346 ieee80211_tx_status_irqsafe(hw, skb);
1347
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001348 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001349 }
1350
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001351 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001352 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001353
1354 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001355}
1356
1357/* must be called only when the card's transmit is completely halted */
1358static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1359{
1360 struct mwl8k_priv *priv = hw->priv;
1361 struct mwl8k_tx_queue *txq = priv->txq + index;
1362
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001363 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001364
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001365 kfree(txq->skb);
1366 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001367
1368 pci_free_consistent(priv->pdev,
1369 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001370 txq->txd, txq->txd_dma);
1371 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001372}
1373
1374static int
1375mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1376{
1377 struct mwl8k_priv *priv = hw->priv;
1378 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001379 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001380 struct ieee80211_hdr *wh;
1381 struct mwl8k_tx_queue *txq;
1382 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001383 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001384 u32 txstatus;
1385 u8 txdatarate;
1386 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001387
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001388 wh = (struct ieee80211_hdr *)skb->data;
1389 if (ieee80211_is_data_qos(wh->frame_control))
1390 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1391 else
1392 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001393
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001394 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001395 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001396
1397 tx_info = IEEE80211_SKB_CB(skb);
1398 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001399
1400 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001401 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001402 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1403 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001404 }
1405
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001406 /* Setup firmware control bit fields for each frame type. */
1407 txstatus = 0;
1408 txdatarate = 0;
1409 if (ieee80211_is_mgmt(wh->frame_control) ||
1410 ieee80211_is_ctl(wh->frame_control)) {
1411 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001412 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001413 } else if (ieee80211_is_data(wh->frame_control)) {
1414 txdatarate = 1;
1415 if (is_multicast_ether_addr(wh->addr1))
1416 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1417
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001418 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001419 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001420 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001421 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001422 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001423 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001424
1425 dma = pci_map_single(priv->pdev, skb->data,
1426 skb->len, PCI_DMA_TODEVICE);
1427
1428 if (pci_dma_mapping_error(priv->pdev, dma)) {
1429 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001430 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001431 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001432 return NETDEV_TX_OK;
1433 }
1434
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001435 spin_lock_bh(&priv->tx_lock);
1436
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001437 txq = priv->txq + index;
1438
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001439 BUG_ON(txq->skb[txq->tail] != NULL);
1440 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001441
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001442 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001443 tx->data_rate = txdatarate;
1444 tx->tx_priority = index;
1445 tx->qos_control = cpu_to_le16(qos);
1446 tx->pkt_phys_addr = cpu_to_le32(dma);
1447 tx->pkt_len = cpu_to_le16(skb->len);
1448 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001449 if (!priv->ap_fw && tx_info->control.sta != NULL)
1450 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1451 else
1452 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001453 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001454 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1455
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001456 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001457 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001458
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001459 txq->tail++;
1460 if (txq->tail == MWL8K_TX_DESCS)
1461 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001462
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001463 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001464 ieee80211_stop_queue(hw, index);
1465
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001466 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001467
1468 spin_unlock_bh(&priv->tx_lock);
1469
1470 return NETDEV_TX_OK;
1471}
1472
1473
1474/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001475 * Firmware access.
1476 *
1477 * We have the following requirements for issuing firmware commands:
1478 * - Some commands require that the packet transmit path is idle when
1479 * the command is issued. (For simplicity, we'll just quiesce the
1480 * transmit path for every command.)
1481 * - There are certain sequences of commands that need to be issued to
1482 * the hardware sequentially, with no other intervening commands.
1483 *
1484 * This leads to an implementation of a "firmware lock" as a mutex that
1485 * can be taken recursively, and which is taken by both the low-level
1486 * command submission function (mwl8k_post_cmd) as well as any users of
1487 * that function that require issuing of an atomic sequence of commands,
1488 * and quiesces the transmit path whenever it's taken.
1489 */
1490static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1491{
1492 struct mwl8k_priv *priv = hw->priv;
1493
1494 if (priv->fw_mutex_owner != current) {
1495 int rc;
1496
1497 mutex_lock(&priv->fw_mutex);
1498 ieee80211_stop_queues(hw);
1499
1500 rc = mwl8k_tx_wait_empty(hw);
1501 if (rc) {
1502 ieee80211_wake_queues(hw);
1503 mutex_unlock(&priv->fw_mutex);
1504
1505 return rc;
1506 }
1507
1508 priv->fw_mutex_owner = current;
1509 }
1510
1511 priv->fw_mutex_depth++;
1512
1513 return 0;
1514}
1515
1516static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1517{
1518 struct mwl8k_priv *priv = hw->priv;
1519
1520 if (!--priv->fw_mutex_depth) {
1521 ieee80211_wake_queues(hw);
1522 priv->fw_mutex_owner = NULL;
1523 mutex_unlock(&priv->fw_mutex);
1524 }
1525}
1526
1527
1528/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001529 * Command processing.
1530 */
1531
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001532/* Timeout firmware commands after 10s */
1533#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001534
1535static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1536{
1537 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1538 struct mwl8k_priv *priv = hw->priv;
1539 void __iomem *regs = priv->regs;
1540 dma_addr_t dma_addr;
1541 unsigned int dma_size;
1542 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001543 unsigned long timeout = 0;
1544 u8 buf[32];
1545
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001546 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001547 dma_size = le16_to_cpu(cmd->length);
1548 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1549 PCI_DMA_BIDIRECTIONAL);
1550 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1551 return -ENOMEM;
1552
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001553 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001554 if (rc) {
1555 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1556 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001557 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001558 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001559
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001560 priv->hostcmd_wait = &cmd_wait;
1561 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1562 iowrite32(MWL8K_H2A_INT_DOORBELL,
1563 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1564 iowrite32(MWL8K_H2A_INT_DUMMY,
1565 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001566
1567 timeout = wait_for_completion_timeout(&cmd_wait,
1568 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1569
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001570 priv->hostcmd_wait = NULL;
1571
1572 mwl8k_fw_unlock(hw);
1573
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001574 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1575 PCI_DMA_BIDIRECTIONAL);
1576
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001577 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001578 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001579 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001580 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1581 MWL8K_CMD_TIMEOUT_MS);
1582 rc = -ETIMEDOUT;
1583 } else {
Lennert Buytenhek0c9cc642009-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)
1590 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001591 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001592 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001593 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001594 else if (ms > 2000)
1595 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1596 wiphy_name(hw->wiphy),
1597 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1598 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001599 }
1600
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001601 return rc;
1602}
1603
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001604static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1605 struct ieee80211_vif *vif,
1606 struct mwl8k_cmd_pkt *cmd)
1607{
1608 if (vif != NULL)
1609 cmd->macid = MWL8K_VIF(vif)->macid;
1610 return mwl8k_post_cmd(hw, cmd);
1611}
1612
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001613/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001614 * Setup code shared between STA and AP firmware images.
1615 */
1616static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1617{
1618 struct mwl8k_priv *priv = hw->priv;
1619
1620 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1621 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1622
1623 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1624 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1625
1626 priv->band_24.band = IEEE80211_BAND_2GHZ;
1627 priv->band_24.channels = priv->channels_24;
1628 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1629 priv->band_24.bitrates = priv->rates_24;
1630 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1631
1632 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1633}
1634
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001635static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1636{
1637 struct mwl8k_priv *priv = hw->priv;
1638
1639 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1640 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1641
1642 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1643 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1644
1645 priv->band_50.band = IEEE80211_BAND_5GHZ;
1646 priv->band_50.channels = priv->channels_50;
1647 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1648 priv->band_50.bitrates = priv->rates_50;
1649 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1650
1651 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1652}
1653
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001654/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001655 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001656 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001657struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001658 struct mwl8k_cmd_pkt header;
1659 __u8 hw_rev;
1660 __u8 host_interface;
1661 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001662 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001663 __le16 region_code;
1664 __le32 fw_rev;
1665 __le32 ps_cookie;
1666 __le32 caps;
1667 __u8 mcs_bitmap[16];
1668 __le32 rx_queue_ptr;
1669 __le32 num_tx_queues;
1670 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1671 __le32 caps2;
1672 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001673 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001674} __attribute__((packed));
1675
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001676#define MWL8K_CAP_MAX_AMSDU 0x20000000
1677#define MWL8K_CAP_GREENFIELD 0x08000000
1678#define MWL8K_CAP_AMPDU 0x04000000
1679#define MWL8K_CAP_RX_STBC 0x01000000
1680#define MWL8K_CAP_TX_STBC 0x00800000
1681#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1682#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1683#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1684#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1685#define MWL8K_CAP_DELAY_BA 0x00003000
1686#define MWL8K_CAP_MIMO 0x00000200
1687#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001688#define MWL8K_CAP_BAND_MASK 0x00000007
1689#define MWL8K_CAP_5GHZ 0x00000004
1690#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001691
Lennert Buytenhek06953232010-01-12 13:49:41 +01001692static void
1693mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1694 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001695{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001696 int rx_streams;
1697 int tx_streams;
1698
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001699 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001700
1701 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001702 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001703 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001704 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001705 if (cap & MWL8K_CAP_AMPDU) {
1706 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001707 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1708 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001709 }
1710 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001711 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001712 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001713 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001714 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001715 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001716 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001717 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001718 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001719 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001720 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001721 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001722
1723 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1724 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1725
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001726 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001727 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001728 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001729 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001730 band->ht_cap.mcs.rx_mask[2] = 0xff;
1731 band->ht_cap.mcs.rx_mask[4] = 0x01;
1732 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001733
1734 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001735 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1736 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001737 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1738 }
1739}
1740
Lennert Buytenhek06953232010-01-12 13:49:41 +01001741static void
1742mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1743{
1744 struct mwl8k_priv *priv = hw->priv;
1745
1746 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1747 mwl8k_setup_2ghz_band(hw);
1748 if (caps & MWL8K_CAP_MIMO)
1749 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1750 }
1751
1752 if (caps & MWL8K_CAP_5GHZ) {
1753 mwl8k_setup_5ghz_band(hw);
1754 if (caps & MWL8K_CAP_MIMO)
1755 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1756 }
1757}
1758
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001759static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001760{
1761 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001762 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001763 int rc;
1764 int i;
1765
1766 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1767 if (cmd == NULL)
1768 return -ENOMEM;
1769
1770 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1771 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1772
1773 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1774 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001775 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001776 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001777 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001778 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001779 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001780 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001781
1782 rc = mwl8k_post_cmd(hw, &cmd->header);
1783
1784 if (!rc) {
1785 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1786 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001787 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001788 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001789 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001790 priv->ap_macids_supported = 0x00000000;
1791 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001792 }
1793
1794 kfree(cmd);
1795 return rc;
1796}
1797
1798/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001799 * CMD_GET_HW_SPEC (AP version).
1800 */
1801struct mwl8k_cmd_get_hw_spec_ap {
1802 struct mwl8k_cmd_pkt header;
1803 __u8 hw_rev;
1804 __u8 host_interface;
1805 __le16 num_wcb;
1806 __le16 num_mcaddrs;
1807 __u8 perm_addr[ETH_ALEN];
1808 __le16 region_code;
1809 __le16 num_antenna;
1810 __le32 fw_rev;
1811 __le32 wcbbase0;
1812 __le32 rxwrptr;
1813 __le32 rxrdptr;
1814 __le32 ps_cookie;
1815 __le32 wcbbase1;
1816 __le32 wcbbase2;
1817 __le32 wcbbase3;
1818} __attribute__((packed));
1819
1820static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1821{
1822 struct mwl8k_priv *priv = hw->priv;
1823 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1824 int rc;
1825
1826 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1827 if (cmd == NULL)
1828 return -ENOMEM;
1829
1830 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1831 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1832
1833 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1834 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1835
1836 rc = mwl8k_post_cmd(hw, &cmd->header);
1837
1838 if (!rc) {
1839 int off;
1840
1841 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1842 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1843 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1844 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001845 mwl8k_setup_2ghz_band(hw);
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001846 priv->ap_macids_supported = 0x000000ff;
1847 priv->sta_macids_supported = 0x00000000;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001848
1849 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1850 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1851
1852 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1853 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1854
1855 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1856 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1857
1858 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1859 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1860
1861 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1862 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1863
1864 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1865 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1866 }
1867
1868 kfree(cmd);
1869 return rc;
1870}
1871
1872/*
1873 * CMD_SET_HW_SPEC.
1874 */
1875struct mwl8k_cmd_set_hw_spec {
1876 struct mwl8k_cmd_pkt header;
1877 __u8 hw_rev;
1878 __u8 host_interface;
1879 __le16 num_mcaddrs;
1880 __u8 perm_addr[ETH_ALEN];
1881 __le16 region_code;
1882 __le32 fw_rev;
1883 __le32 ps_cookie;
1884 __le32 caps;
1885 __le32 rx_queue_ptr;
1886 __le32 num_tx_queues;
1887 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1888 __le32 flags;
1889 __le32 num_tx_desc_per_queue;
1890 __le32 total_rxd;
1891} __attribute__((packed));
1892
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001893#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1894#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
1895#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001896
1897static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1898{
1899 struct mwl8k_priv *priv = hw->priv;
1900 struct mwl8k_cmd_set_hw_spec *cmd;
1901 int rc;
1902 int i;
1903
1904 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1905 if (cmd == NULL)
1906 return -ENOMEM;
1907
1908 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1909 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1910
1911 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1912 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1913 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1914 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1915 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001916 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
1917 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
1918 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001919 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1920 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1921
1922 rc = mwl8k_post_cmd(hw, &cmd->header);
1923 kfree(cmd);
1924
1925 return rc;
1926}
1927
1928/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001929 * CMD_MAC_MULTICAST_ADR.
1930 */
1931struct mwl8k_cmd_mac_multicast_adr {
1932 struct mwl8k_cmd_pkt header;
1933 __le16 action;
1934 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001935 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001936};
1937
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001938#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1939#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1940#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1941#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001942
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001943static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001944__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001945 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001946{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001947 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001948 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001949 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001950
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001951 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001952 allmulti = 1;
1953 mc_count = 0;
1954 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001955
1956 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1957
1958 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001959 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001960 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001961
1962 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1963 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001964 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1965 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001966
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001967 if (allmulti) {
1968 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1969 } else if (mc_count) {
1970 int i;
1971
1972 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1973 cmd->numaddr = cpu_to_le16(mc_count);
1974 for (i = 0; i < mc_count && mclist; i++) {
1975 if (mclist->da_addrlen != ETH_ALEN) {
1976 kfree(cmd);
1977 return NULL;
1978 }
1979 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1980 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001981 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001982 }
1983
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001984 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001985}
1986
1987/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001988 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001989 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001990struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001991 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001992 __le32 stats[64];
1993} __attribute__((packed));
1994
1995#define MWL8K_STAT_ACK_FAILURE 9
1996#define MWL8K_STAT_RTS_FAILURE 12
1997#define MWL8K_STAT_FCS_ERROR 24
1998#define MWL8K_STAT_RTS_SUCCESS 11
1999
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002000static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2001 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002002{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002003 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002004 int rc;
2005
2006 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2007 if (cmd == NULL)
2008 return -ENOMEM;
2009
2010 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2011 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002012
2013 rc = mwl8k_post_cmd(hw, &cmd->header);
2014 if (!rc) {
2015 stats->dot11ACKFailureCount =
2016 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2017 stats->dot11RTSFailureCount =
2018 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2019 stats->dot11FCSErrorCount =
2020 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2021 stats->dot11RTSSuccessCount =
2022 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2023 }
2024 kfree(cmd);
2025
2026 return rc;
2027}
2028
2029/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002030 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002031 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002032struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002033 struct mwl8k_cmd_pkt header;
2034 __le16 action;
2035 __le16 control;
2036 __le16 radio_on;
2037} __attribute__((packed));
2038
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002039static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002040mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002041{
2042 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002043 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002044 int rc;
2045
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002046 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002047 return 0;
2048
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002049 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2050 if (cmd == NULL)
2051 return -ENOMEM;
2052
2053 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2054 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2055 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002056 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002057 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2058
2059 rc = mwl8k_post_cmd(hw, &cmd->header);
2060 kfree(cmd);
2061
2062 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002063 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002064
2065 return rc;
2066}
2067
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002068static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002069{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002070 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002071}
2072
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002073static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002074{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002075 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002076}
2077
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002078static int
2079mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2080{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002081 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002082
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002083 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002084
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002085 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002086}
2087
2088/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002089 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002090 */
2091#define MWL8K_TX_POWER_LEVEL_TOTAL 8
2092
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002093struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002094 struct mwl8k_cmd_pkt header;
2095 __le16 action;
2096 __le16 support_level;
2097 __le16 current_level;
2098 __le16 reserved;
2099 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2100} __attribute__((packed));
2101
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002102static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002103{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002104 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002105 int rc;
2106
2107 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2108 if (cmd == NULL)
2109 return -ENOMEM;
2110
2111 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2112 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2113 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2114 cmd->support_level = cpu_to_le16(dBm);
2115
2116 rc = mwl8k_post_cmd(hw, &cmd->header);
2117 kfree(cmd);
2118
2119 return rc;
2120}
2121
2122/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002123 * CMD_RF_ANTENNA.
2124 */
2125struct mwl8k_cmd_rf_antenna {
2126 struct mwl8k_cmd_pkt header;
2127 __le16 antenna;
2128 __le16 mode;
2129} __attribute__((packed));
2130
2131#define MWL8K_RF_ANTENNA_RX 1
2132#define MWL8K_RF_ANTENNA_TX 2
2133
2134static int
2135mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2136{
2137 struct mwl8k_cmd_rf_antenna *cmd;
2138 int rc;
2139
2140 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2141 if (cmd == NULL)
2142 return -ENOMEM;
2143
2144 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2145 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2146 cmd->antenna = cpu_to_le16(antenna);
2147 cmd->mode = cpu_to_le16(mask);
2148
2149 rc = mwl8k_post_cmd(hw, &cmd->header);
2150 kfree(cmd);
2151
2152 return rc;
2153}
2154
2155/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002156 * CMD_SET_BEACON.
2157 */
2158struct mwl8k_cmd_set_beacon {
2159 struct mwl8k_cmd_pkt header;
2160 __le16 beacon_len;
2161 __u8 beacon[0];
2162};
2163
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002164static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2165 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002166{
2167 struct mwl8k_cmd_set_beacon *cmd;
2168 int rc;
2169
2170 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2171 if (cmd == NULL)
2172 return -ENOMEM;
2173
2174 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2175 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2176 cmd->beacon_len = cpu_to_le16(len);
2177 memcpy(cmd->beacon, beacon, len);
2178
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002179 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002180 kfree(cmd);
2181
2182 return rc;
2183}
2184
2185/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002186 * CMD_SET_PRE_SCAN.
2187 */
2188struct mwl8k_cmd_set_pre_scan {
2189 struct mwl8k_cmd_pkt header;
2190} __attribute__((packed));
2191
2192static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2193{
2194 struct mwl8k_cmd_set_pre_scan *cmd;
2195 int rc;
2196
2197 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2198 if (cmd == NULL)
2199 return -ENOMEM;
2200
2201 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2202 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2203
2204 rc = mwl8k_post_cmd(hw, &cmd->header);
2205 kfree(cmd);
2206
2207 return rc;
2208}
2209
2210/*
2211 * CMD_SET_POST_SCAN.
2212 */
2213struct mwl8k_cmd_set_post_scan {
2214 struct mwl8k_cmd_pkt header;
2215 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002216 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002217} __attribute__((packed));
2218
2219static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002220mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002221{
2222 struct mwl8k_cmd_set_post_scan *cmd;
2223 int rc;
2224
2225 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2226 if (cmd == NULL)
2227 return -ENOMEM;
2228
2229 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2230 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2231 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002232 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002233
2234 rc = mwl8k_post_cmd(hw, &cmd->header);
2235 kfree(cmd);
2236
2237 return rc;
2238}
2239
2240/*
2241 * CMD_SET_RF_CHANNEL.
2242 */
2243struct mwl8k_cmd_set_rf_channel {
2244 struct mwl8k_cmd_pkt header;
2245 __le16 action;
2246 __u8 current_channel;
2247 __le32 channel_flags;
2248} __attribute__((packed));
2249
2250static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002251 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002252{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002253 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002254 struct mwl8k_cmd_set_rf_channel *cmd;
2255 int rc;
2256
2257 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2258 if (cmd == NULL)
2259 return -ENOMEM;
2260
2261 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2262 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2263 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2264 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002265
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002266 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002267 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002268 else if (channel->band == IEEE80211_BAND_5GHZ)
2269 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002270
2271 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2272 conf->channel_type == NL80211_CHAN_HT20)
2273 cmd->channel_flags |= cpu_to_le32(0x00000080);
2274 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2275 cmd->channel_flags |= cpu_to_le32(0x000001900);
2276 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2277 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002278
2279 rc = mwl8k_post_cmd(hw, &cmd->header);
2280 kfree(cmd);
2281
2282 return rc;
2283}
2284
2285/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002286 * CMD_SET_AID.
2287 */
2288#define MWL8K_FRAME_PROT_DISABLED 0x00
2289#define MWL8K_FRAME_PROT_11G 0x07
2290#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2291#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2292
2293struct mwl8k_cmd_update_set_aid {
2294 struct mwl8k_cmd_pkt header;
2295 __le16 aid;
2296
2297 /* AP's MAC address (BSSID) */
2298 __u8 bssid[ETH_ALEN];
2299 __le16 protection_mode;
2300 __u8 supp_rates[14];
2301} __attribute__((packed));
2302
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002303static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2304{
2305 int i;
2306 int j;
2307
2308 /*
2309 * Clear nonstandard rates 4 and 13.
2310 */
2311 mask &= 0x1fef;
2312
2313 for (i = 0, j = 0; i < 14; i++) {
2314 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002315 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002316 }
2317}
2318
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002319static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002320mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2321 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002322{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002323 struct mwl8k_cmd_update_set_aid *cmd;
2324 u16 prot_mode;
2325 int rc;
2326
2327 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2328 if (cmd == NULL)
2329 return -ENOMEM;
2330
2331 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2332 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002333 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002334 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002335
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002336 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002337 prot_mode = MWL8K_FRAME_PROT_11G;
2338 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002339 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002340 IEEE80211_HT_OP_MODE_PROTECTION) {
2341 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2342 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2343 break;
2344 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2345 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2346 break;
2347 default:
2348 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2349 break;
2350 }
2351 }
2352 cmd->protection_mode = cpu_to_le16(prot_mode);
2353
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002354 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002355
2356 rc = mwl8k_post_cmd(hw, &cmd->header);
2357 kfree(cmd);
2358
2359 return rc;
2360}
2361
2362/*
2363 * CMD_SET_RATE.
2364 */
2365struct mwl8k_cmd_set_rate {
2366 struct mwl8k_cmd_pkt header;
2367 __u8 legacy_rates[14];
2368
2369 /* Bitmap for supported MCS codes. */
2370 __u8 mcs_set[16];
2371 __u8 reserved[16];
2372} __attribute__((packed));
2373
2374static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002375mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002376 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002377{
2378 struct mwl8k_cmd_set_rate *cmd;
2379 int rc;
2380
2381 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2382 if (cmd == NULL)
2383 return -ENOMEM;
2384
2385 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2386 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002387 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002388 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002389
2390 rc = mwl8k_post_cmd(hw, &cmd->header);
2391 kfree(cmd);
2392
2393 return rc;
2394}
2395
2396/*
2397 * CMD_FINALIZE_JOIN.
2398 */
2399#define MWL8K_FJ_BEACON_MAXLEN 128
2400
2401struct mwl8k_cmd_finalize_join {
2402 struct mwl8k_cmd_pkt header;
2403 __le32 sleep_interval; /* Number of beacon periods to sleep */
2404 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2405} __attribute__((packed));
2406
2407static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2408 int framelen, int dtim)
2409{
2410 struct mwl8k_cmd_finalize_join *cmd;
2411 struct ieee80211_mgmt *payload = frame;
2412 int payload_len;
2413 int rc;
2414
2415 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2416 if (cmd == NULL)
2417 return -ENOMEM;
2418
2419 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2420 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2421 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2422
2423 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2424 if (payload_len < 0)
2425 payload_len = 0;
2426 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2427 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2428
2429 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2430
2431 rc = mwl8k_post_cmd(hw, &cmd->header);
2432 kfree(cmd);
2433
2434 return rc;
2435}
2436
2437/*
2438 * CMD_SET_RTS_THRESHOLD.
2439 */
2440struct mwl8k_cmd_set_rts_threshold {
2441 struct mwl8k_cmd_pkt header;
2442 __le16 action;
2443 __le16 threshold;
2444} __attribute__((packed));
2445
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002446static int
2447mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002448{
2449 struct mwl8k_cmd_set_rts_threshold *cmd;
2450 int rc;
2451
2452 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2453 if (cmd == NULL)
2454 return -ENOMEM;
2455
2456 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2457 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002458 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2459 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002460
2461 rc = mwl8k_post_cmd(hw, &cmd->header);
2462 kfree(cmd);
2463
2464 return rc;
2465}
2466
2467/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002468 * CMD_SET_SLOT.
2469 */
2470struct mwl8k_cmd_set_slot {
2471 struct mwl8k_cmd_pkt header;
2472 __le16 action;
2473 __u8 short_slot;
2474} __attribute__((packed));
2475
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002476static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002477{
2478 struct mwl8k_cmd_set_slot *cmd;
2479 int rc;
2480
2481 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2482 if (cmd == NULL)
2483 return -ENOMEM;
2484
2485 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2486 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2487 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002488 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002489
2490 rc = mwl8k_post_cmd(hw, &cmd->header);
2491 kfree(cmd);
2492
2493 return rc;
2494}
2495
2496/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002497 * CMD_SET_EDCA_PARAMS.
2498 */
2499struct mwl8k_cmd_set_edca_params {
2500 struct mwl8k_cmd_pkt header;
2501
2502 /* See MWL8K_SET_EDCA_XXX below */
2503 __le16 action;
2504
2505 /* TX opportunity in units of 32 us */
2506 __le16 txop;
2507
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002508 union {
2509 struct {
2510 /* Log exponent of max contention period: 0...15 */
2511 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002512
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002513 /* Log exponent of min contention period: 0...15 */
2514 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002515
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002516 /* Adaptive interframe spacing in units of 32us */
2517 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002518
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002519 /* TX queue to configure */
2520 __u8 txq;
2521 } ap;
2522 struct {
2523 /* Log exponent of max contention period: 0...15 */
2524 __u8 log_cw_max;
2525
2526 /* Log exponent of min contention period: 0...15 */
2527 __u8 log_cw_min;
2528
2529 /* Adaptive interframe spacing in units of 32us */
2530 __u8 aifs;
2531
2532 /* TX queue to configure */
2533 __u8 txq;
2534 } sta;
2535 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002536} __attribute__((packed));
2537
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002538#define MWL8K_SET_EDCA_CW 0x01
2539#define MWL8K_SET_EDCA_TXOP 0x02
2540#define MWL8K_SET_EDCA_AIFS 0x04
2541
2542#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2543 MWL8K_SET_EDCA_TXOP | \
2544 MWL8K_SET_EDCA_AIFS)
2545
2546static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002547mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2548 __u16 cw_min, __u16 cw_max,
2549 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002550{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002551 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002552 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002553 int rc;
2554
2555 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2556 if (cmd == NULL)
2557 return -ENOMEM;
2558
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002559 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2560 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002561 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2562 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002563 if (priv->ap_fw) {
2564 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2565 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2566 cmd->ap.aifs = aifs;
2567 cmd->ap.txq = qnum;
2568 } else {
2569 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2570 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2571 cmd->sta.aifs = aifs;
2572 cmd->sta.txq = qnum;
2573 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002574
2575 rc = mwl8k_post_cmd(hw, &cmd->header);
2576 kfree(cmd);
2577
2578 return rc;
2579}
2580
2581/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002582 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002583 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002584struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002585 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002586 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002587} __attribute__((packed));
2588
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002589static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002590{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002591 struct mwl8k_priv *priv = hw->priv;
2592 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002593 int rc;
2594
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002595 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2596 if (cmd == NULL)
2597 return -ENOMEM;
2598
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002599 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002600 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002601 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002602
2603 rc = mwl8k_post_cmd(hw, &cmd->header);
2604 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002605
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002606 if (!rc)
2607 priv->wmm_enabled = enable;
2608
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002609 return rc;
2610}
2611
2612/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002613 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002614 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002615struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002616 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002617 __le32 action;
2618 __u8 rx_antenna_map;
2619 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002620} __attribute__((packed));
2621
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002622static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002623{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002624 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002625 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002626
2627 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2628 if (cmd == NULL)
2629 return -ENOMEM;
2630
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002631 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002632 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002633 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2634 cmd->rx_antenna_map = rx;
2635 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002636
2637 rc = mwl8k_post_cmd(hw, &cmd->header);
2638 kfree(cmd);
2639
2640 return rc;
2641}
2642
2643/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002644 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002645 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002646struct mwl8k_cmd_use_fixed_rate_sta {
2647 struct mwl8k_cmd_pkt header;
2648 __le32 action;
2649 __le32 allow_rate_drop;
2650 __le32 num_rates;
2651 struct {
2652 __le32 is_ht_rate;
2653 __le32 enable_retry;
2654 __le32 rate;
2655 __le32 retry_count;
2656 } rate_entry[8];
2657 __le32 rate_type;
2658 __le32 reserved1;
2659 __le32 reserved2;
2660} __attribute__((packed));
2661
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002662#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002663#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002664
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002665static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002666{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002667 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002668 int rc;
2669
2670 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2671 if (cmd == NULL)
2672 return -ENOMEM;
2673
2674 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2675 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002676 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2677 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002678
2679 rc = mwl8k_post_cmd(hw, &cmd->header);
2680 kfree(cmd);
2681
2682 return rc;
2683}
2684
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002685/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002686 * CMD_USE_FIXED_RATE (AP version).
2687 */
2688struct mwl8k_cmd_use_fixed_rate_ap {
2689 struct mwl8k_cmd_pkt header;
2690 __le32 action;
2691 __le32 allow_rate_drop;
2692 __le32 num_rates;
2693 struct mwl8k_rate_entry_ap {
2694 __le32 is_ht_rate;
2695 __le32 enable_retry;
2696 __le32 rate;
2697 __le32 retry_count;
2698 } rate_entry[4];
2699 u8 multicast_rate;
2700 u8 multicast_rate_type;
2701 u8 management_rate;
2702} __attribute__((packed));
2703
2704static int
2705mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2706{
2707 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2708 int rc;
2709
2710 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2711 if (cmd == NULL)
2712 return -ENOMEM;
2713
2714 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2715 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2716 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2717 cmd->multicast_rate = mcast;
2718 cmd->management_rate = mgmt;
2719
2720 rc = mwl8k_post_cmd(hw, &cmd->header);
2721 kfree(cmd);
2722
2723 return rc;
2724}
2725
2726/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002727 * CMD_ENABLE_SNIFFER.
2728 */
2729struct mwl8k_cmd_enable_sniffer {
2730 struct mwl8k_cmd_pkt header;
2731 __le32 action;
2732} __attribute__((packed));
2733
2734static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2735{
2736 struct mwl8k_cmd_enable_sniffer *cmd;
2737 int rc;
2738
2739 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2740 if (cmd == NULL)
2741 return -ENOMEM;
2742
2743 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2744 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2745 cmd->action = cpu_to_le32(!!enable);
2746
2747 rc = mwl8k_post_cmd(hw, &cmd->header);
2748 kfree(cmd);
2749
2750 return rc;
2751}
2752
2753/*
2754 * CMD_SET_MAC_ADDR.
2755 */
2756struct mwl8k_cmd_set_mac_addr {
2757 struct mwl8k_cmd_pkt header;
2758 union {
2759 struct {
2760 __le16 mac_type;
2761 __u8 mac_addr[ETH_ALEN];
2762 } mbss;
2763 __u8 mac_addr[ETH_ALEN];
2764 };
2765} __attribute__((packed));
2766
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002767#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
2768#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
2769#define MWL8K_MAC_TYPE_PRIMARY_AP 2
2770#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002771
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002772static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
2773 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002774{
2775 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002776 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002777 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002778 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002779 int rc;
2780
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002781 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2782 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
2783 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
2784 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
2785 else
2786 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
2787 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
2788 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
2789 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2790 else
2791 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
2792 }
2793
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002794 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2795 if (cmd == NULL)
2796 return -ENOMEM;
2797
2798 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2799 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2800 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002801 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002802 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2803 } else {
2804 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2805 }
2806
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002807 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002808 kfree(cmd);
2809
2810 return rc;
2811}
2812
2813/*
2814 * CMD_SET_RATEADAPT_MODE.
2815 */
2816struct mwl8k_cmd_set_rate_adapt_mode {
2817 struct mwl8k_cmd_pkt header;
2818 __le16 action;
2819 __le16 mode;
2820} __attribute__((packed));
2821
2822static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2823{
2824 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2825 int rc;
2826
2827 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2828 if (cmd == NULL)
2829 return -ENOMEM;
2830
2831 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2832 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2833 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2834 cmd->mode = cpu_to_le16(mode);
2835
2836 rc = mwl8k_post_cmd(hw, &cmd->header);
2837 kfree(cmd);
2838
2839 return rc;
2840}
2841
2842/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002843 * CMD_BSS_START.
2844 */
2845struct mwl8k_cmd_bss_start {
2846 struct mwl8k_cmd_pkt header;
2847 __le32 enable;
2848} __attribute__((packed));
2849
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002850static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
2851 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002852{
2853 struct mwl8k_cmd_bss_start *cmd;
2854 int rc;
2855
2856 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2857 if (cmd == NULL)
2858 return -ENOMEM;
2859
2860 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
2861 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2862 cmd->enable = cpu_to_le32(enable);
2863
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002864 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002865 kfree(cmd);
2866
2867 return rc;
2868}
2869
2870/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002871 * CMD_SET_NEW_STN.
2872 */
2873struct mwl8k_cmd_set_new_stn {
2874 struct mwl8k_cmd_pkt header;
2875 __le16 aid;
2876 __u8 mac_addr[6];
2877 __le16 stn_id;
2878 __le16 action;
2879 __le16 rsvd;
2880 __le32 legacy_rates;
2881 __u8 ht_rates[4];
2882 __le16 cap_info;
2883 __le16 ht_capabilities_info;
2884 __u8 mac_ht_param_info;
2885 __u8 rev;
2886 __u8 control_channel;
2887 __u8 add_channel;
2888 __le16 op_mode;
2889 __le16 stbc;
2890 __u8 add_qos_info;
2891 __u8 is_qos_sta;
2892 __le32 fw_sta_ptr;
2893} __attribute__((packed));
2894
2895#define MWL8K_STA_ACTION_ADD 0
2896#define MWL8K_STA_ACTION_REMOVE 2
2897
2898static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
2899 struct ieee80211_vif *vif,
2900 struct ieee80211_sta *sta)
2901{
2902 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002903 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002904 int rc;
2905
2906 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2907 if (cmd == NULL)
2908 return -ENOMEM;
2909
2910 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2911 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2912 cmd->aid = cpu_to_le16(sta->aid);
2913 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
2914 cmd->stn_id = cpu_to_le16(sta->aid);
2915 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002916 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
2917 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
2918 else
2919 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
2920 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002921 if (sta->ht_cap.ht_supported) {
2922 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
2923 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
2924 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
2925 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
2926 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
2927 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
2928 ((sta->ht_cap.ampdu_density & 7) << 2);
2929 cmd->is_qos_sta = 1;
2930 }
2931
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002932 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002933 kfree(cmd);
2934
2935 return rc;
2936}
2937
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002938static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
2939 struct ieee80211_vif *vif)
2940{
2941 struct mwl8k_cmd_set_new_stn *cmd;
2942 int rc;
2943
2944 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2945 if (cmd == NULL)
2946 return -ENOMEM;
2947
2948 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2949 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2950 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
2951
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002952 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002953 kfree(cmd);
2954
2955 return rc;
2956}
2957
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002958static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
2959 struct ieee80211_vif *vif, u8 *addr)
2960{
2961 struct mwl8k_cmd_set_new_stn *cmd;
2962 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 memcpy(cmd->mac_addr, addr, ETH_ALEN);
2971 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
2972
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002973 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002974 kfree(cmd);
2975
2976 return rc;
2977}
2978
2979/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002980 * CMD_UPDATE_STADB.
2981 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002982struct ewc_ht_info {
2983 __le16 control1;
2984 __le16 control2;
2985 __le16 control3;
2986} __attribute__((packed));
2987
2988struct peer_capability_info {
2989 /* Peer type - AP vs. STA. */
2990 __u8 peer_type;
2991
2992 /* Basic 802.11 capabilities from assoc resp. */
2993 __le16 basic_caps;
2994
2995 /* Set if peer supports 802.11n high throughput (HT). */
2996 __u8 ht_support;
2997
2998 /* Valid if HT is supported. */
2999 __le16 ht_caps;
3000 __u8 extended_ht_caps;
3001 struct ewc_ht_info ewc_info;
3002
3003 /* Legacy rate table. Intersection of our rates and peer rates. */
3004 __u8 legacy_rates[12];
3005
3006 /* HT rate table. Intersection of our rates and peer rates. */
3007 __u8 ht_rates[16];
3008 __u8 pad[16];
3009
3010 /* If set, interoperability mode, no proprietary extensions. */
3011 __u8 interop;
3012 __u8 pad2;
3013 __u8 station_id;
3014 __le16 amsdu_enabled;
3015} __attribute__((packed));
3016
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003017struct mwl8k_cmd_update_stadb {
3018 struct mwl8k_cmd_pkt header;
3019
3020 /* See STADB_ACTION_TYPE */
3021 __le32 action;
3022
3023 /* Peer MAC address */
3024 __u8 peer_addr[ETH_ALEN];
3025
3026 __le32 reserved;
3027
3028 /* Peer info - valid during add/update. */
3029 struct peer_capability_info peer_info;
3030} __attribute__((packed));
3031
Lennert Buytenheka6804002010-01-04 21:55:42 +01003032#define MWL8K_STA_DB_MODIFY_ENTRY 1
3033#define MWL8K_STA_DB_DEL_ENTRY 2
3034
3035/* Peer Entry flags - used to define the type of the peer node */
3036#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3037
3038static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003039 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003040 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003041{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003042 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003043 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003044 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003045 int rc;
3046
3047 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3048 if (cmd == NULL)
3049 return -ENOMEM;
3050
3051 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3052 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003053 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003054 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003055
Lennert Buytenheka6804002010-01-04 21:55:42 +01003056 p = &cmd->peer_info;
3057 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3058 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003059 p->ht_support = sta->ht_cap.ht_supported;
3060 p->ht_caps = sta->ht_cap.cap;
3061 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3062 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003063 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3064 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3065 else
3066 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3067 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003068 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003069 p->interop = 1;
3070 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003071
Lennert Buytenheka6804002010-01-04 21:55:42 +01003072 rc = mwl8k_post_cmd(hw, &cmd->header);
3073 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003074
Lennert Buytenheka6804002010-01-04 21:55:42 +01003075 return rc ? rc : p->station_id;
3076}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003077
Lennert Buytenheka6804002010-01-04 21:55:42 +01003078static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3079 struct ieee80211_vif *vif, u8 *addr)
3080{
3081 struct mwl8k_cmd_update_stadb *cmd;
3082 int rc;
3083
3084 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3085 if (cmd == NULL)
3086 return -ENOMEM;
3087
3088 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3089 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3090 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3091 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3092
3093 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003094 kfree(cmd);
3095
3096 return rc;
3097}
3098
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003099
3100/*
3101 * Interrupt handling.
3102 */
3103static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3104{
3105 struct ieee80211_hw *hw = dev_id;
3106 struct mwl8k_priv *priv = hw->priv;
3107 u32 status;
3108
3109 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003110 if (!status)
3111 return IRQ_NONE;
3112
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003113 if (status & MWL8K_A2H_INT_TX_DONE) {
3114 status &= ~MWL8K_A2H_INT_TX_DONE;
3115 tasklet_schedule(&priv->poll_tx_task);
3116 }
3117
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003118 if (status & MWL8K_A2H_INT_RX_READY) {
3119 status &= ~MWL8K_A2H_INT_RX_READY;
3120 tasklet_schedule(&priv->poll_rx_task);
3121 }
3122
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003123 if (status)
3124 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003125
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003126 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003127 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003128 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003129 }
3130
3131 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003132 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003133 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003134 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003135 }
3136
3137 return IRQ_HANDLED;
3138}
3139
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003140static void mwl8k_tx_poll(unsigned long data)
3141{
3142 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3143 struct mwl8k_priv *priv = hw->priv;
3144 int limit;
3145 int i;
3146
3147 limit = 32;
3148
3149 spin_lock_bh(&priv->tx_lock);
3150
3151 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3152 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3153
3154 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3155 complete(priv->tx_wait);
3156 priv->tx_wait = NULL;
3157 }
3158
3159 spin_unlock_bh(&priv->tx_lock);
3160
3161 if (limit) {
3162 writel(~MWL8K_A2H_INT_TX_DONE,
3163 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3164 } else {
3165 tasklet_schedule(&priv->poll_tx_task);
3166 }
3167}
3168
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003169static void mwl8k_rx_poll(unsigned long data)
3170{
3171 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3172 struct mwl8k_priv *priv = hw->priv;
3173 int limit;
3174
3175 limit = 32;
3176 limit -= rxq_process(hw, 0, limit);
3177 limit -= rxq_refill(hw, 0, limit);
3178
3179 if (limit) {
3180 writel(~MWL8K_A2H_INT_RX_READY,
3181 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3182 } else {
3183 tasklet_schedule(&priv->poll_rx_task);
3184 }
3185}
3186
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003187
3188/*
3189 * Core driver operations.
3190 */
3191static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3192{
3193 struct mwl8k_priv *priv = hw->priv;
3194 int index = skb_get_queue_mapping(skb);
3195 int rc;
3196
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003197 if (!priv->radio_on) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003198 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003199 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003200 dev_kfree_skb(skb);
3201 return NETDEV_TX_OK;
3202 }
3203
3204 rc = mwl8k_txq_xmit(hw, index, skb);
3205
3206 return rc;
3207}
3208
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003209static int mwl8k_start(struct ieee80211_hw *hw)
3210{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003211 struct mwl8k_priv *priv = hw->priv;
3212 int rc;
3213
Joe Perchesa0607fd2009-11-18 23:29:17 -08003214 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003215 IRQF_SHARED, MWL8K_NAME, hw);
3216 if (rc) {
3217 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003218 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003219 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003220 }
3221
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003222 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003223 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003224 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003225
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003226 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003227 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003228
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003229 rc = mwl8k_fw_lock(hw);
3230 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003231 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003232
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003233 if (!priv->ap_fw) {
3234 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003235 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003236
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003237 if (!rc)
3238 rc = mwl8k_cmd_set_pre_scan(hw);
3239
3240 if (!rc)
3241 rc = mwl8k_cmd_set_post_scan(hw,
3242 "\x00\x00\x00\x00\x00\x00");
3243 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003244
3245 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003246 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003247
3248 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003249 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003250
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003251 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003252 }
3253
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003254 if (rc) {
3255 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3256 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003257 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003258 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003259 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003260
3261 return rc;
3262}
3263
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003264static void mwl8k_stop(struct ieee80211_hw *hw)
3265{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003266 struct mwl8k_priv *priv = hw->priv;
3267 int i;
3268
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003269 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003270
3271 ieee80211_stop_queues(hw);
3272
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003273 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003274 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003275 free_irq(priv->pdev->irq, hw);
3276
3277 /* Stop finalize join worker */
3278 cancel_work_sync(&priv->finalize_join_worker);
3279 if (priv->beacon_skb != NULL)
3280 dev_kfree_skb(priv->beacon_skb);
3281
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003282 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003283 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003284 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003285
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003286 /* Return all skbs to mac80211 */
3287 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003288 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003289}
3290
3291static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003292 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003293{
3294 struct mwl8k_priv *priv = hw->priv;
3295 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003296 u32 macids_supported;
3297 int macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003298
3299 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003300 * Reject interface creation if sniffer mode is active, as
3301 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003302 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003303 */
3304 if (priv->sniffer_enabled) {
3305 printk(KERN_INFO "%s: unable to create STA "
3306 "interface due to sniffer mode being enabled\n",
3307 wiphy_name(hw->wiphy));
3308 return -EINVAL;
3309 }
3310
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003311
3312 switch (vif->type) {
3313 case NL80211_IFTYPE_AP:
3314 macids_supported = priv->ap_macids_supported;
3315 break;
3316 case NL80211_IFTYPE_STATION:
3317 macids_supported = priv->sta_macids_supported;
3318 break;
3319 default:
3320 return -EINVAL;
3321 }
3322
3323 macid = ffs(macids_supported & ~priv->macids_used);
3324 if (!macid--)
3325 return -EBUSY;
3326
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003327 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003328 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003329 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003330 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003331 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003332 mwl8k_vif->seqno = 0;
3333
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003334 /* Set the mac address. */
3335 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3336
3337 if (priv->ap_fw)
3338 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3339
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003340 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003341 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003342
3343 return 0;
3344}
3345
3346static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003347 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003348{
3349 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003350 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003351
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003352 if (priv->ap_fw)
3353 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3354
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003355 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003356
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003357 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003358 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003359}
3360
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003361static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003362{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003363 struct ieee80211_conf *conf = &hw->conf;
3364 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003365 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003366
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003367 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003368 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003369 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003370 }
3371
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003372 rc = mwl8k_fw_lock(hw);
3373 if (rc)
3374 return rc;
3375
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003376 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003377 if (rc)
3378 goto out;
3379
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003380 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003381 if (rc)
3382 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003383
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003384 if (conf->power_level > 18)
3385 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003386 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003387 if (rc)
3388 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003389
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003390 if (priv->ap_fw) {
3391 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3392 if (!rc)
3393 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3394 } else {
3395 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3396 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003397
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003398out:
3399 mwl8k_fw_unlock(hw);
3400
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003401 return rc;
3402}
3403
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003404static void
3405mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3406 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003407{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003408 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003409 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003410 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003411 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003412
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003413 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003414 return;
3415
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003416 /*
3417 * No need to capture a beacon if we're no longer associated.
3418 */
3419 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3420 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003421
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003422 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003423 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003424 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003425 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003426 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003427
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003428 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003429
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003430 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003431 if (ap == NULL) {
3432 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003433 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003434 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003435
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003436 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
3437 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
3438 } else {
3439 ap_legacy_rates =
3440 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3441 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003442 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003443
3444 rcu_read_unlock();
3445 }
3446
3447 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003448 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003449 if (rc)
3450 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003451
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003452 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003453 if (rc)
3454 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003455 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003456
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003457 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003458 rc = mwl8k_set_radio_preamble(hw,
3459 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003460 if (rc)
3461 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003462 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003463
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003464 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003465 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003466 if (rc)
3467 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003468 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003469
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003470 if (vif->bss_conf.assoc &&
3471 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
3472 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003473 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003474 if (rc)
3475 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003476 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003477
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003478 if (vif->bss_conf.assoc &&
3479 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003480 /*
3481 * Finalize the join. Tell rx handler to process
3482 * next beacon from our BSSID.
3483 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003484 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003485 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003486 }
3487
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003488out:
3489 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003490}
3491
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003492static void
3493mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3494 struct ieee80211_bss_conf *info, u32 changed)
3495{
3496 int rc;
3497
3498 if (mwl8k_fw_lock(hw))
3499 return;
3500
3501 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3502 rc = mwl8k_set_radio_preamble(hw,
3503 vif->bss_conf.use_short_preamble);
3504 if (rc)
3505 goto out;
3506 }
3507
3508 if (changed & BSS_CHANGED_BASIC_RATES) {
3509 int idx;
3510 int rate;
3511
3512 /*
3513 * Use lowest supported basic rate for multicasts
3514 * and management frames (such as probe responses --
3515 * beacons will always go out at 1 Mb/s).
3516 */
3517 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003518 if (idx)
3519 idx--;
3520
3521 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3522 rate = mwl8k_rates_24[idx].hw_value;
3523 else
3524 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003525
3526 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
3527 }
3528
3529 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
3530 struct sk_buff *skb;
3531
3532 skb = ieee80211_beacon_get(hw, vif);
3533 if (skb != NULL) {
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003534 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003535 kfree_skb(skb);
3536 }
3537 }
3538
3539 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003540 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003541
3542out:
3543 mwl8k_fw_unlock(hw);
3544}
3545
3546static void
3547mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3548 struct ieee80211_bss_conf *info, u32 changed)
3549{
3550 struct mwl8k_priv *priv = hw->priv;
3551
3552 if (!priv->ap_fw)
3553 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
3554 else
3555 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
3556}
3557
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003558static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3559 int mc_count, struct dev_addr_list *mclist)
3560{
3561 struct mwl8k_cmd_pkt *cmd;
3562
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003563 /*
3564 * Synthesize and return a command packet that programs the
3565 * hardware multicast address filter. At this point we don't
3566 * know whether FIF_ALLMULTI is being requested, but if it is,
3567 * we'll end up throwing this packet away and creating a new
3568 * one in mwl8k_configure_filter().
3569 */
3570 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003571
3572 return (unsigned long)cmd;
3573}
3574
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003575static int
3576mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3577 unsigned int changed_flags,
3578 unsigned int *total_flags)
3579{
3580 struct mwl8k_priv *priv = hw->priv;
3581
3582 /*
3583 * Hardware sniffer mode is mutually exclusive with STA
3584 * operation, so refuse to enable sniffer mode if a STA
3585 * interface is active.
3586 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003587 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003588 if (net_ratelimit())
3589 printk(KERN_INFO "%s: not enabling sniffer "
3590 "mode because STA interface is active\n",
3591 wiphy_name(hw->wiphy));
3592 return 0;
3593 }
3594
3595 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003596 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003597 return 0;
3598 priv->sniffer_enabled = true;
3599 }
3600
3601 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3602 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3603 FIF_OTHER_BSS;
3604
3605 return 1;
3606}
3607
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003608static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
3609{
3610 if (!list_empty(&priv->vif_list))
3611 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
3612
3613 return NULL;
3614}
3615
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003616static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3617 unsigned int changed_flags,
3618 unsigned int *total_flags,
3619 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003620{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003621 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003622 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3623
3624 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003625 * AP firmware doesn't allow fine-grained control over
3626 * the receive filter.
3627 */
3628 if (priv->ap_fw) {
3629 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3630 kfree(cmd);
3631 return;
3632 }
3633
3634 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003635 * Enable hardware sniffer mode if FIF_CONTROL or
3636 * FIF_OTHER_BSS is requested.
3637 */
3638 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3639 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3640 kfree(cmd);
3641 return;
3642 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003643
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003644 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003645 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003646
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003647 if (mwl8k_fw_lock(hw)) {
3648 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003649 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003650 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003651
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003652 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003653 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003654 priv->sniffer_enabled = false;
3655 }
3656
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003657 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003658 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3659 /*
3660 * Disable the BSS filter.
3661 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003662 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003663 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003664 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003665 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003666
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003667 /*
3668 * Enable the BSS filter.
3669 *
3670 * If there is an active STA interface, use that
3671 * interface's BSSID, otherwise use a dummy one
3672 * (where the OUI part needs to be nonzero for
3673 * the BSSID to be accepted by POST_SCAN).
3674 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003675 mwl8k_vif = mwl8k_first_vif(priv);
3676 if (mwl8k_vif != NULL)
3677 bssid = mwl8k_vif->vif->bss_conf.bssid;
3678 else
3679 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003680
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003681 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003682 }
3683 }
3684
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003685 /*
3686 * If FIF_ALLMULTI is being requested, throw away the command
3687 * packet that ->prepare_multicast() built and replace it with
3688 * a command packet that enables reception of all multicast
3689 * packets.
3690 */
3691 if (*total_flags & FIF_ALLMULTI) {
3692 kfree(cmd);
3693 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3694 }
3695
3696 if (cmd != NULL) {
3697 mwl8k_post_cmd(hw, cmd);
3698 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003699 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003700
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003701 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003702}
3703
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003704static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3705{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003706 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003707}
3708
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003709struct mwl8k_sta_notify_item
3710{
3711 struct list_head list;
3712 struct ieee80211_vif *vif;
3713 enum sta_notify_cmd cmd;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003714 struct ieee80211_sta sta;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003715};
3716
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003717static void
3718mwl8k_do_sta_notify(struct ieee80211_hw *hw, struct mwl8k_sta_notify_item *s)
3719{
3720 struct mwl8k_priv *priv = hw->priv;
3721
3722 /*
3723 * STA firmware uses UPDATE_STADB, AP firmware uses SET_NEW_STN.
3724 */
3725 if (!priv->ap_fw && s->cmd == STA_NOTIFY_ADD) {
3726 int rc;
3727
3728 rc = mwl8k_cmd_update_stadb_add(hw, s->vif, &s->sta);
3729 if (rc >= 0) {
3730 struct ieee80211_sta *sta;
3731
3732 rcu_read_lock();
3733 sta = ieee80211_find_sta(s->vif, s->sta.addr);
3734 if (sta != NULL)
3735 MWL8K_STA(sta)->peer_id = rc;
3736 rcu_read_unlock();
3737 }
3738 } else if (!priv->ap_fw && s->cmd == STA_NOTIFY_REMOVE) {
3739 mwl8k_cmd_update_stadb_del(hw, s->vif, s->sta.addr);
3740 } else if (priv->ap_fw && s->cmd == STA_NOTIFY_ADD) {
3741 mwl8k_cmd_set_new_stn_add(hw, s->vif, &s->sta);
3742 } else if (priv->ap_fw && s->cmd == STA_NOTIFY_REMOVE) {
3743 mwl8k_cmd_set_new_stn_del(hw, s->vif, s->sta.addr);
3744 }
3745}
3746
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003747static void mwl8k_sta_notify_worker(struct work_struct *work)
3748{
3749 struct mwl8k_priv *priv =
3750 container_of(work, struct mwl8k_priv, sta_notify_worker);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003751 struct ieee80211_hw *hw = priv->hw;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003752
3753 spin_lock_bh(&priv->sta_notify_list_lock);
3754 while (!list_empty(&priv->sta_notify_list)) {
3755 struct mwl8k_sta_notify_item *s;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003756
3757 s = list_entry(priv->sta_notify_list.next,
3758 struct mwl8k_sta_notify_item, list);
3759 list_del(&s->list);
3760
3761 spin_unlock_bh(&priv->sta_notify_list_lock);
3762
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003763 mwl8k_do_sta_notify(hw, s);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003764 kfree(s);
3765
3766 spin_lock_bh(&priv->sta_notify_list_lock);
3767 }
3768 spin_unlock_bh(&priv->sta_notify_list_lock);
3769}
3770
3771static void
3772mwl8k_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3773 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3774{
3775 struct mwl8k_priv *priv = hw->priv;
3776 struct mwl8k_sta_notify_item *s;
3777
3778 if (cmd != STA_NOTIFY_ADD && cmd != STA_NOTIFY_REMOVE)
3779 return;
3780
3781 s = kmalloc(sizeof(*s), GFP_ATOMIC);
3782 if (s != NULL) {
3783 s->vif = vif;
3784 s->cmd = cmd;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003785 s->sta = *sta;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003786
3787 spin_lock(&priv->sta_notify_list_lock);
3788 list_add_tail(&s->list, &priv->sta_notify_list);
3789 spin_unlock(&priv->sta_notify_list_lock);
3790
3791 ieee80211_queue_work(hw, &priv->sta_notify_worker);
3792 }
3793}
3794
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003795static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3796 const struct ieee80211_tx_queue_params *params)
3797{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003798 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003799 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003800
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003801 rc = mwl8k_fw_lock(hw);
3802 if (!rc) {
3803 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003804 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003805
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003806 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003807 rc = mwl8k_cmd_set_edca_params(hw, queue,
3808 params->cw_min,
3809 params->cw_max,
3810 params->aifs,
3811 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003812
3813 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003814 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003815
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003816 return rc;
3817}
3818
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003819static int mwl8k_get_stats(struct ieee80211_hw *hw,
3820 struct ieee80211_low_level_stats *stats)
3821{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003822 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003823}
3824
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003825static int
3826mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3827 enum ieee80211_ampdu_mlme_action action,
3828 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3829{
3830 switch (action) {
3831 case IEEE80211_AMPDU_RX_START:
3832 case IEEE80211_AMPDU_RX_STOP:
3833 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3834 return -ENOTSUPP;
3835 return 0;
3836 default:
3837 return -ENOTSUPP;
3838 }
3839}
3840
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003841static const struct ieee80211_ops mwl8k_ops = {
3842 .tx = mwl8k_tx,
3843 .start = mwl8k_start,
3844 .stop = mwl8k_stop,
3845 .add_interface = mwl8k_add_interface,
3846 .remove_interface = mwl8k_remove_interface,
3847 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003848 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003849 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003850 .configure_filter = mwl8k_configure_filter,
3851 .set_rts_threshold = mwl8k_set_rts_threshold,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003852 .sta_notify = mwl8k_sta_notify,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003853 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003854 .get_stats = mwl8k_get_stats,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003855 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003856};
3857
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003858static void mwl8k_finalize_join_worker(struct work_struct *work)
3859{
3860 struct mwl8k_priv *priv =
3861 container_of(work, struct mwl8k_priv, finalize_join_worker);
3862 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01003863 struct ieee80211_mgmt *mgmt = (void *)skb->data;
3864 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
3865 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
3866 mgmt->u.beacon.variable, len);
3867 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003868
Johannes Berg56007a02010-01-26 14:19:52 +01003869 if (tim && tim[1] >= 2)
3870 dtim_period = tim[3];
3871
3872 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003873
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003874 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003875 priv->beacon_skb = NULL;
3876}
3877
John W. Linvillebcb628d2009-11-06 16:40:16 -05003878enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003879 MWL8363 = 0,
3880 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003881 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003882};
3883
John W. Linvillebcb628d2009-11-06 16:40:16 -05003884static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003885 [MWL8363] = {
3886 .part_name = "88w8363",
3887 .helper_image = "mwl8k/helper_8363.fw",
3888 .fw_image = "mwl8k/fmimage_8363.fw",
3889 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003890 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003891 .part_name = "88w8687",
3892 .helper_image = "mwl8k/helper_8687.fw",
3893 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003894 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003895 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003896 .part_name = "88w8366",
3897 .helper_image = "mwl8k/helper_8366.fw",
3898 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003899 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003900 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003901};
3902
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01003903MODULE_FIRMWARE("mwl8k/helper_8363.fw");
3904MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
3905MODULE_FIRMWARE("mwl8k/helper_8687.fw");
3906MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
3907MODULE_FIRMWARE("mwl8k/helper_8366.fw");
3908MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
3909
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003910static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003911 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3912 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003913 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3914 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3915 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01003916 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003917 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003918};
3919MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3920
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003921static int __devinit mwl8k_probe(struct pci_dev *pdev,
3922 const struct pci_device_id *id)
3923{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003924 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003925 struct ieee80211_hw *hw;
3926 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003927 int rc;
3928 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003929
3930 if (!printed_version) {
3931 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3932 printed_version = 1;
3933 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003934
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003935
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003936 rc = pci_enable_device(pdev);
3937 if (rc) {
3938 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3939 MWL8K_NAME);
3940 return rc;
3941 }
3942
3943 rc = pci_request_regions(pdev, MWL8K_NAME);
3944 if (rc) {
3945 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3946 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003947 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003948 }
3949
3950 pci_set_master(pdev);
3951
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003952
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003953 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3954 if (hw == NULL) {
3955 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3956 rc = -ENOMEM;
3957 goto err_free_reg;
3958 }
3959
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003960 SET_IEEE80211_DEV(hw, &pdev->dev);
3961 pci_set_drvdata(pdev, hw);
3962
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003963 priv = hw->priv;
3964 priv->hw = hw;
3965 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003966 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003967
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003968
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003969 priv->sram = pci_iomap(pdev, 0, 0x10000);
3970 if (priv->sram == NULL) {
3971 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003972 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003973 goto err_iounmap;
3974 }
3975
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003976 /*
3977 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3978 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3979 */
3980 priv->regs = pci_iomap(pdev, 1, 0x10000);
3981 if (priv->regs == NULL) {
3982 priv->regs = pci_iomap(pdev, 2, 0x10000);
3983 if (priv->regs == NULL) {
3984 printk(KERN_ERR "%s: Cannot map device registers\n",
3985 wiphy_name(hw->wiphy));
3986 goto err_iounmap;
3987 }
3988 }
3989
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003990
3991 /* Reset firmware and hardware */
3992 mwl8k_hw_reset(priv);
3993
3994 /* Ask userland hotplug daemon for the device firmware */
3995 rc = mwl8k_request_firmware(priv);
3996 if (rc) {
3997 printk(KERN_ERR "%s: Firmware files not found\n",
3998 wiphy_name(hw->wiphy));
3999 goto err_stop_firmware;
4000 }
4001
4002 /* Load firmware into hardware */
4003 rc = mwl8k_load_firmware(hw);
4004 if (rc) {
4005 printk(KERN_ERR "%s: Cannot start firmware\n",
4006 wiphy_name(hw->wiphy));
4007 goto err_stop_firmware;
4008 }
4009
4010 /* Reclaim memory once firmware is successfully loaded */
4011 mwl8k_release_firmware(priv);
4012
4013
Lennert Buytenhek91942232010-01-04 21:53:54 +01004014 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004015 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01004016 if (priv->rxd_ops == NULL) {
4017 printk(KERN_ERR "%s: Driver does not have AP "
4018 "firmware image support for this hardware\n",
4019 wiphy_name(hw->wiphy));
4020 goto err_stop_firmware;
4021 }
4022 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004023 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01004024 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004025
4026 priv->sniffer_enabled = false;
4027 priv->wmm_enabled = false;
4028 priv->pending_tx_pkts = 0;
4029
4030
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004031 /*
4032 * Extra headroom is the size of the required DMA header
4033 * minus the size of the smallest 802.11 frame (CTS frame).
4034 */
4035 hw->extra_tx_headroom =
4036 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
4037
4038 hw->channel_change_time = 10;
4039
4040 hw->queues = MWL8K_TX_QUEUES;
4041
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004042 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004043 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004044 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004045 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004046
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004047 priv->macids_used = 0;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004048 INIT_LIST_HEAD(&priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004049
4050 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02004051 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02004052 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004053
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004054 /* Station database handling */
4055 INIT_WORK(&priv->sta_notify_worker, mwl8k_sta_notify_worker);
4056 spin_lock_init(&priv->sta_notify_list_lock);
4057 INIT_LIST_HEAD(&priv->sta_notify_list);
4058
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004059 /* Finalize join worker */
4060 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4061
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004062 /* TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004063 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4064 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004065 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4066 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004067
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004068 /* Power management cookie */
4069 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4070 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004071 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004072
4073 rc = mwl8k_rxq_init(hw, 0);
4074 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004075 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004076 rxq_refill(hw, 0, INT_MAX);
4077
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004078 mutex_init(&priv->fw_mutex);
4079 priv->fw_mutex_owner = NULL;
4080 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004081 priv->hostcmd_wait = NULL;
4082
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004083 spin_lock_init(&priv->tx_lock);
4084
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004085 priv->tx_wait = NULL;
4086
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004087 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4088 rc = mwl8k_txq_init(hw, i);
4089 if (rc)
4090 goto err_free_queues;
4091 }
4092
4093 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004094 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004095 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004096 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004097 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4098
Joe Perchesa0607fd2009-11-18 23:29:17 -08004099 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004100 IRQF_SHARED, MWL8K_NAME, hw);
4101 if (rc) {
4102 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004103 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004104 goto err_free_queues;
4105 }
4106
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004107 /*
4108 * Temporarily enable interrupts. Initial firmware host
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004109 * commands use interrupts and avoid polling. Disable
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004110 * interrupts when done.
4111 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004112 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004113
4114 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02004115 if (priv->ap_fw) {
4116 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4117 if (!rc)
4118 rc = mwl8k_cmd_set_hw_spec(hw);
4119 } else {
4120 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4121 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004122 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004123 printk(KERN_ERR "%s: Cannot initialise firmware\n",
4124 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004125 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004126 }
4127
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004128 hw->wiphy->interface_modes = 0;
4129 if (priv->ap_macids_supported)
4130 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4131 if (priv->sta_macids_supported)
4132 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4133
4134
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004135 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004136 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004137 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004138 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004139 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004140 }
4141
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004142 /* Clear MAC address */
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004143 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004144 if (rc) {
4145 printk(KERN_ERR "%s: Cannot clear MAC address\n",
4146 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004147 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004148 }
4149
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004150 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004151 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004152 free_irq(priv->pdev->irq, hw);
4153
4154 rc = ieee80211_register_hw(hw);
4155 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004156 printk(KERN_ERR "%s: Cannot register device\n",
4157 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01004158 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004159 }
4160
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02004161 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02004162 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004163 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02004164 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004165 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4166 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004167
4168 return 0;
4169
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004170err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004171 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004172 free_irq(priv->pdev->irq, hw);
4173
4174err_free_queues:
4175 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4176 mwl8k_txq_deinit(hw, i);
4177 mwl8k_rxq_deinit(hw, 0);
4178
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004179err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004180 if (priv->cookie != NULL)
4181 pci_free_consistent(priv->pdev, 4,
4182 priv->cookie, priv->cookie_dma);
4183
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004184err_stop_firmware:
4185 mwl8k_hw_reset(priv);
4186 mwl8k_release_firmware(priv);
4187
4188err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004189 if (priv->regs != NULL)
4190 pci_iounmap(pdev, priv->regs);
4191
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004192 if (priv->sram != NULL)
4193 pci_iounmap(pdev, priv->sram);
4194
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004195 pci_set_drvdata(pdev, NULL);
4196 ieee80211_free_hw(hw);
4197
4198err_free_reg:
4199 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004200
4201err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004202 pci_disable_device(pdev);
4203
4204 return rc;
4205}
4206
Joerg Albert230f7af2009-04-18 02:10:45 +02004207static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004208{
4209 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4210}
4211
Joerg Albert230f7af2009-04-18 02:10:45 +02004212static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004213{
4214 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4215 struct mwl8k_priv *priv;
4216 int i;
4217
4218 if (hw == NULL)
4219 return;
4220 priv = hw->priv;
4221
4222 ieee80211_stop_queues(hw);
4223
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004224 ieee80211_unregister_hw(hw);
4225
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004226 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004227 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004228 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004229
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004230 /* Stop hardware */
4231 mwl8k_hw_reset(priv);
4232
4233 /* Return all skbs to mac80211 */
4234 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004235 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004236
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004237 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4238 mwl8k_txq_deinit(hw, i);
4239
4240 mwl8k_rxq_deinit(hw, 0);
4241
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004242 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004243
4244 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004245 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004246 pci_set_drvdata(pdev, NULL);
4247 ieee80211_free_hw(hw);
4248 pci_release_regions(pdev);
4249 pci_disable_device(pdev);
4250}
4251
4252static struct pci_driver mwl8k_driver = {
4253 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004254 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004255 .probe = mwl8k_probe,
4256 .remove = __devexit_p(mwl8k_remove),
4257 .shutdown = __devexit_p(mwl8k_shutdown),
4258};
4259
4260static int __init mwl8k_init(void)
4261{
4262 return pci_register_driver(&mwl8k_driver);
4263}
4264
4265static void __exit mwl8k_exit(void)
4266{
4267 pci_unregister_driver(&mwl8k_driver);
4268}
4269
4270module_init(mwl8k_init);
4271module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004272
4273MODULE_DESCRIPTION(MWL8K_DESC);
4274MODULE_VERSION(MWL8K_VERSION);
4275MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
4276MODULE_LICENSE("GPL");