blob: d8cf43853de1a801af59f2394811d2c664261e5e [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 Buytenheka145d572009-08-18 04:34:26 +02005 * Copyright (C) 2008-2009 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 Buytenhek6976b662010-01-02 10:31:50 +010029#define MWL8K_VERSION "0.11"
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
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200122 struct ieee80211_tx_queue_stats stats;
123 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 Buytenhekbe695fc2009-11-30 18:32:46 +0100150
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200151 /* firmware access */
152 struct mutex fw_mutex;
153 struct task_struct *fw_mutex_owner;
154 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200155 struct completion *hostcmd_wait;
156
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100157 /* lock held over TX and TX reap */
158 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100159
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200160 /* TX quiesce completion, protected by fw_mutex and tx_lock */
161 struct completion *tx_wait;
162
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100163 /* List of interfaces. */
164 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100165
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100166 /* power management status cookie from firmware */
167 u32 *cookie;
168 dma_addr_t cookie_dma;
169
170 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100171 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200172 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100173
174 /*
175 * Running count of TX packets in flight, to avoid
176 * iterating over the transmit rings each time.
177 */
178 int pending_tx_pkts;
179
180 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
181 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
182
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200183 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200184 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200185 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200186 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100187
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +0100188 struct work_struct sta_notify_worker;
189 spinlock_t sta_notify_list_lock;
190 struct list_head sta_notify_list;
191
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100192 /* XXX need to convert this to handle multiple interfaces */
193 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200194 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 struct sk_buff *beacon_skb;
196
197 /*
198 * This FJ worker has to be global as it is scheduled from the
199 * RX handler. At this point we don't know which interface it
200 * belongs to until the list of bssids waiting to complete join
201 * is checked.
202 */
203 struct work_struct finalize_join_worker;
204
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100205 /* Tasklet to perform TX reclaim. */
206 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100207
208 /* Tasklet to perform RX. */
209 struct tasklet_struct poll_rx_task;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100210};
211
212/* Per interface specific private data */
213struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100214 struct list_head list;
215 struct ieee80211_vif *vif;
216
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100217 /* Firmware macid for this vif. */
218 int macid;
219
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100220 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100221 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100222};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200223#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100224
Lennert Buytenheka6804002010-01-04 21:55:42 +0100225struct mwl8k_sta {
226 /* Index into station database. Returned by UPDATE_STADB. */
227 u8 peer_id;
228};
229#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
230
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100231static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100232 { .center_freq = 2412, .hw_value = 1, },
233 { .center_freq = 2417, .hw_value = 2, },
234 { .center_freq = 2422, .hw_value = 3, },
235 { .center_freq = 2427, .hw_value = 4, },
236 { .center_freq = 2432, .hw_value = 5, },
237 { .center_freq = 2437, .hw_value = 6, },
238 { .center_freq = 2442, .hw_value = 7, },
239 { .center_freq = 2447, .hw_value = 8, },
240 { .center_freq = 2452, .hw_value = 9, },
241 { .center_freq = 2457, .hw_value = 10, },
242 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100243 { .center_freq = 2467, .hw_value = 12, },
244 { .center_freq = 2472, .hw_value = 13, },
245 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100246};
247
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100248static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100249 { .bitrate = 10, .hw_value = 2, },
250 { .bitrate = 20, .hw_value = 4, },
251 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200252 { .bitrate = 110, .hw_value = 22, },
253 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100254 { .bitrate = 60, .hw_value = 12, },
255 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100256 { .bitrate = 120, .hw_value = 24, },
257 { .bitrate = 180, .hw_value = 36, },
258 { .bitrate = 240, .hw_value = 48, },
259 { .bitrate = 360, .hw_value = 72, },
260 { .bitrate = 480, .hw_value = 96, },
261 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100262 { .bitrate = 720, .hw_value = 144, },
263};
264
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100265static const struct ieee80211_channel mwl8k_channels_50[] = {
266 { .center_freq = 5180, .hw_value = 36, },
267 { .center_freq = 5200, .hw_value = 40, },
268 { .center_freq = 5220, .hw_value = 44, },
269 { .center_freq = 5240, .hw_value = 48, },
270};
271
272static const struct ieee80211_rate mwl8k_rates_50[] = {
273 { .bitrate = 60, .hw_value = 12, },
274 { .bitrate = 90, .hw_value = 18, },
275 { .bitrate = 120, .hw_value = 24, },
276 { .bitrate = 180, .hw_value = 36, },
277 { .bitrate = 240, .hw_value = 48, },
278 { .bitrate = 360, .hw_value = 72, },
279 { .bitrate = 480, .hw_value = 96, },
280 { .bitrate = 540, .hw_value = 108, },
281 { .bitrate = 720, .hw_value = 144, },
282};
283
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100284/* Set or get info from Firmware */
285#define MWL8K_CMD_SET 0x0001
286#define MWL8K_CMD_GET 0x0000
287
288/* Firmware command codes */
289#define MWL8K_CMD_CODE_DNLD 0x0001
290#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200291#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100292#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
293#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200294#define MWL8K_CMD_RADIO_CONTROL 0x001c
295#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200296#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100297#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100298#define MWL8K_CMD_SET_PRE_SCAN 0x0107
299#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200300#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301#define MWL8K_CMD_SET_AID 0x010d
302#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200303#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100304#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200305#define MWL8K_CMD_SET_SLOT 0x0114
306#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
307#define MWL8K_CMD_SET_WMM_MODE 0x0123
308#define MWL8K_CMD_MIMO_CONFIG 0x0125
309#define MWL8K_CMD_USE_FIXED_RATE 0x0126
310#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100311#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200312#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100313#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
314#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200315#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100316
317static const char *mwl8k_cmd_name(u16 cmd, char *buf, int bufsize)
318{
319#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
320 snprintf(buf, bufsize, "%s", #x);\
321 return buf;\
322 } while (0)
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +0200323 switch (cmd & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100324 MWL8K_CMDNAME(CODE_DNLD);
325 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200326 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100327 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
328 MWL8K_CMDNAME(GET_STAT);
329 MWL8K_CMDNAME(RADIO_CONTROL);
330 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200331 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100332 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100333 MWL8K_CMDNAME(SET_PRE_SCAN);
334 MWL8K_CMDNAME(SET_POST_SCAN);
335 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100336 MWL8K_CMDNAME(SET_AID);
337 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200338 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100339 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200340 MWL8K_CMDNAME(SET_SLOT);
341 MWL8K_CMDNAME(SET_EDCA_PARAMS);
342 MWL8K_CMDNAME(SET_WMM_MODE);
343 MWL8K_CMDNAME(MIMO_CONFIG);
344 MWL8K_CMDNAME(USE_FIXED_RATE);
345 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200346 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200347 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100348 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100349 MWL8K_CMDNAME(SET_NEW_STN);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200350 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100351 default:
352 snprintf(buf, bufsize, "0x%x", cmd);
353 }
354#undef MWL8K_CMDNAME
355
356 return buf;
357}
358
359/* Hardware and firmware reset */
360static void mwl8k_hw_reset(struct mwl8k_priv *priv)
361{
362 iowrite32(MWL8K_H2A_INT_RESET,
363 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
364 iowrite32(MWL8K_H2A_INT_RESET,
365 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
366 msleep(20);
367}
368
369/* Release fw image */
370static void mwl8k_release_fw(struct firmware **fw)
371{
372 if (*fw == NULL)
373 return;
374 release_firmware(*fw);
375 *fw = NULL;
376}
377
378static void mwl8k_release_firmware(struct mwl8k_priv *priv)
379{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100380 mwl8k_release_fw(&priv->fw_ucode);
381 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100382}
383
384/* Request fw image */
385static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200386 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100387{
388 /* release current image */
389 if (*fw != NULL)
390 mwl8k_release_fw(fw);
391
392 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200393 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100394}
395
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200396static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100397{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200398 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100399 int rc;
400
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200401 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100402 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200403 if (rc) {
404 printk(KERN_ERR "%s: Error requesting helper "
405 "firmware file %s\n", pci_name(priv->pdev),
406 di->helper_image);
407 return rc;
408 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100409 }
410
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100411 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100412 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200413 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200414 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100415 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100416 return rc;
417 }
418
419 return 0;
420}
421
422struct mwl8k_cmd_pkt {
423 __le16 code;
424 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100425 __u8 seq_num;
426 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100427 __le16 result;
428 char payload[0];
429} __attribute__((packed));
430
431/*
432 * Firmware loading.
433 */
434static int
435mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
436{
437 void __iomem *regs = priv->regs;
438 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100439 int loops;
440
441 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
442 if (pci_dma_mapping_error(priv->pdev, dma_addr))
443 return -ENOMEM;
444
445 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
446 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
447 iowrite32(MWL8K_H2A_INT_DOORBELL,
448 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
449 iowrite32(MWL8K_H2A_INT_DUMMY,
450 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
451
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100452 loops = 1000;
453 do {
454 u32 int_code;
455
456 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
457 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
458 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100459 break;
460 }
461
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200462 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100463 udelay(1);
464 } while (--loops);
465
466 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
467
Lennert Buytenhekd4b70572009-07-16 12:44:45 +0200468 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100469}
470
471static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
472 const u8 *data, size_t length)
473{
474 struct mwl8k_cmd_pkt *cmd;
475 int done;
476 int rc = 0;
477
478 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
479 if (cmd == NULL)
480 return -ENOMEM;
481
482 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
483 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100484 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100485 cmd->result = 0;
486
487 done = 0;
488 while (length) {
489 int block_size = length > 256 ? 256 : length;
490
491 memcpy(cmd->payload, data + done, block_size);
492 cmd->length = cpu_to_le16(block_size);
493
494 rc = mwl8k_send_fw_load_cmd(priv, cmd,
495 sizeof(*cmd) + block_size);
496 if (rc)
497 break;
498
499 done += block_size;
500 length -= block_size;
501 }
502
503 if (!rc) {
504 cmd->length = 0;
505 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
506 }
507
508 kfree(cmd);
509
510 return rc;
511}
512
513static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
514 const u8 *data, size_t length)
515{
516 unsigned char *buffer;
517 int may_continue, rc = 0;
518 u32 done, prev_block_size;
519
520 buffer = kmalloc(1024, GFP_KERNEL);
521 if (buffer == NULL)
522 return -ENOMEM;
523
524 done = 0;
525 prev_block_size = 0;
526 may_continue = 1000;
527 while (may_continue > 0) {
528 u32 block_size;
529
530 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
531 if (block_size & 1) {
532 block_size &= ~1;
533 may_continue--;
534 } else {
535 done += prev_block_size;
536 length -= prev_block_size;
537 }
538
539 if (block_size > 1024 || block_size > length) {
540 rc = -EOVERFLOW;
541 break;
542 }
543
544 if (length == 0) {
545 rc = 0;
546 break;
547 }
548
549 if (block_size == 0) {
550 rc = -EPROTO;
551 may_continue--;
552 udelay(1);
553 continue;
554 }
555
556 prev_block_size = block_size;
557 memcpy(buffer, data + done, block_size);
558
559 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
560 if (rc)
561 break;
562 }
563
564 if (!rc && length != 0)
565 rc = -EREMOTEIO;
566
567 kfree(buffer);
568
569 return rc;
570}
571
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200572static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100573{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200574 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100575 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200576 int rc;
577 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100578
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200579 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100580 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100581
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200582 if (helper == NULL) {
583 printk(KERN_ERR "%s: helper image needed but none "
584 "given\n", pci_name(priv->pdev));
585 return -EINVAL;
586 }
587
588 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100589 if (rc) {
590 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200591 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100592 return rc;
593 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100594 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100595
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200596 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100597 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200598 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100599 }
600
601 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200602 printk(KERN_ERR "%s: unable to load firmware image\n",
603 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100604 return rc;
605 }
606
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100607 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100608
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100609 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100610 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200611 u32 ready_code;
612
613 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
614 if (ready_code == MWL8K_FWAP_READY) {
615 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100616 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200617 } else if (ready_code == MWL8K_FWSTA_READY) {
618 priv->ap_fw = 0;
619 break;
620 }
621
622 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100623 udelay(1);
624 } while (--loops);
625
626 return loops ? 0 : -ETIMEDOUT;
627}
628
629
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100630/* DMA header used by firmware and hardware. */
631struct mwl8k_dma_data {
632 __le16 fwlen;
633 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100634 char data[0];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100635} __attribute__((packed));
636
637/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100638static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100639{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100640 struct mwl8k_dma_data *tr;
641 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100642
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100643 tr = (struct mwl8k_dma_data *)skb->data;
644 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
645
646 if (hdrlen != sizeof(tr->wh)) {
647 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
648 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
649 *((__le16 *)(tr->data - 2)) = qos;
650 } else {
651 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
652 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100653 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100654
655 if (hdrlen != sizeof(*tr))
656 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100657}
658
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200659static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100660{
661 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100662 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100663 struct mwl8k_dma_data *tr;
664
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100665 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100666 * Add a firmware DMA header; the firmware requires that we
667 * present a 2-byte payload length followed by a 4-address
668 * header (without QoS field), followed (optionally) by any
669 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100670 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100671 wh = (struct ieee80211_hdr *)skb->data;
672
673 hdrlen = ieee80211_hdrlen(wh->frame_control);
674 if (hdrlen != sizeof(*tr))
675 skb_push(skb, sizeof(*tr) - hdrlen);
676
677 if (ieee80211_is_data_qos(wh->frame_control))
678 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100679
680 tr = (struct mwl8k_dma_data *)skb->data;
681 if (wh != &tr->wh)
682 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100683 if (hdrlen != sizeof(tr->wh))
684 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100685
686 /*
687 * Firmware length is the length of the fully formed "802.11
688 * payload". That is, everything except for the 802.11 header.
689 * This includes all crypto material including the MIC.
690 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100691 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100692}
693
694
695/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100696 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200697 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100698struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200699 __le16 pkt_len;
700 __u8 sq2;
701 __u8 rate;
702 __le32 pkt_phys_addr;
703 __le32 next_rxd_phys_addr;
704 __le16 qos_control;
705 __le16 htsig2;
706 __le32 hw_rssi_info;
707 __le32 hw_noise_floor_info;
708 __u8 noise_floor;
709 __u8 pad0[3];
710 __u8 rssi;
711 __u8 rx_status;
712 __u8 channel;
713 __u8 rx_ctrl;
714} __attribute__((packed));
715
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100716#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
717#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
718#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100719
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100720#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200721
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100722static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200723{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100724 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200725
726 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100727 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200728}
729
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100730static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200731{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100732 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200733
734 rxd->pkt_len = cpu_to_le16(len);
735 rxd->pkt_phys_addr = cpu_to_le32(addr);
736 wmb();
737 rxd->rx_ctrl = 0;
738}
739
740static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100741mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
742 __le16 *qos)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200743{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100744 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200745
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100746 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200747 return -1;
748 rmb();
749
750 memset(status, 0, sizeof(*status));
751
752 status->signal = -rxd->rssi;
753 status->noise = -rxd->noise_floor;
754
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100755 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200756 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100757 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100758 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100759 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200760 } else {
761 int i;
762
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100763 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
764 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200765 status->rate_idx = i;
766 break;
767 }
768 }
769 }
770
Lennert Buytenhek85478342010-01-12 13:48:56 +0100771 if (rxd->channel > 14) {
772 status->band = IEEE80211_BAND_5GHZ;
773 if (!(status->flag & RX_FLAG_HT))
774 status->rate_idx -= 5;
775 } else {
776 status->band = IEEE80211_BAND_2GHZ;
777 }
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200778 status->freq = ieee80211_channel_to_frequency(rxd->channel);
779
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100780 *qos = rxd->qos_control;
781
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200782 return le16_to_cpu(rxd->pkt_len);
783}
784
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100785static struct rxd_ops rxd_8366_ap_ops = {
786 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
787 .rxd_init = mwl8k_rxd_8366_ap_init,
788 .rxd_refill = mwl8k_rxd_8366_ap_refill,
789 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200790};
791
792/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100793 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100794 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100795struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100796 __le16 pkt_len;
797 __u8 link_quality;
798 __u8 noise_level;
799 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200800 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100801 __le16 qos_control;
802 __le16 rate_info;
803 __le32 pad0[4];
804 __u8 rssi;
805 __u8 channel;
806 __le16 pad1;
807 __u8 rx_ctrl;
808 __u8 rx_status;
809 __u8 pad2[2];
810} __attribute__((packed));
811
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100812#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
813#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
814#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
815#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
816#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
817#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200818
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100819#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200820
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100821static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200822{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100823 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200824
825 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100826 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200827}
828
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100829static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200830{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100831 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200832
833 rxd->pkt_len = cpu_to_le16(len);
834 rxd->pkt_phys_addr = cpu_to_le32(addr);
835 wmb();
836 rxd->rx_ctrl = 0;
837}
838
839static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100840mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100841 __le16 *qos)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200842{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100843 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200844 u16 rate_info;
845
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100846 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200847 return -1;
848 rmb();
849
850 rate_info = le16_to_cpu(rxd->rate_info);
851
852 memset(status, 0, sizeof(*status));
853
854 status->signal = -rxd->rssi;
855 status->noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100856 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
857 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200858
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100859 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200860 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100861 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200862 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100863 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200864 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100865 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200866 status->flag |= RX_FLAG_HT;
867
Lennert Buytenhek85478342010-01-12 13:48:56 +0100868 if (rxd->channel > 14) {
869 status->band = IEEE80211_BAND_5GHZ;
870 if (!(status->flag & RX_FLAG_HT))
871 status->rate_idx -= 5;
872 } else {
873 status->band = IEEE80211_BAND_2GHZ;
874 }
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200875 status->freq = ieee80211_channel_to_frequency(rxd->channel);
876
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100877 *qos = rxd->qos_control;
878
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200879 return le16_to_cpu(rxd->pkt_len);
880}
881
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100882static struct rxd_ops rxd_sta_ops = {
883 .rxd_size = sizeof(struct mwl8k_rxd_sta),
884 .rxd_init = mwl8k_rxd_sta_init,
885 .rxd_refill = mwl8k_rxd_sta_refill,
886 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200887};
888
889
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100890#define MWL8K_RX_DESCS 256
891#define MWL8K_RX_MAXSZ 3800
892
893static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
894{
895 struct mwl8k_priv *priv = hw->priv;
896 struct mwl8k_rx_queue *rxq = priv->rxq + index;
897 int size;
898 int i;
899
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200900 rxq->rxd_count = 0;
901 rxq->head = 0;
902 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100903
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200904 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100905
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200906 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
907 if (rxq->rxd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100908 printk(KERN_ERR "%s: failed to alloc RX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200909 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100910 return -ENOMEM;
911 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200912 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100913
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200914 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
915 if (rxq->buf == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100916 printk(KERN_ERR "%s: failed to alloc RX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200917 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200918 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100919 return -ENOMEM;
920 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200921 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100922
923 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200924 int desc_size;
925 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100926 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200927 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100928
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200929 desc_size = priv->rxd_ops->rxd_size;
930 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100931
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200932 nexti = i + 1;
933 if (nexti == MWL8K_RX_DESCS)
934 nexti = 0;
935 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
936
937 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100938 }
939
940 return 0;
941}
942
943static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
944{
945 struct mwl8k_priv *priv = hw->priv;
946 struct mwl8k_rx_queue *rxq = priv->rxq + index;
947 int refilled;
948
949 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200950 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100951 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200952 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100953 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200954 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100955
956 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
957 if (skb == NULL)
958 break;
959
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200960 addr = pci_map_single(priv->pdev, skb->data,
961 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100962
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200963 rxq->rxd_count++;
964 rx = rxq->tail++;
965 if (rxq->tail == MWL8K_RX_DESCS)
966 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200967 rxq->buf[rx].skb = skb;
968 pci_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200969
970 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
971 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100972
973 refilled++;
974 }
975
976 return refilled;
977}
978
979/* Must be called only when the card's reception is completely halted */
980static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
981{
982 struct mwl8k_priv *priv = hw->priv;
983 struct mwl8k_rx_queue *rxq = priv->rxq + index;
984 int i;
985
986 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200987 if (rxq->buf[i].skb != NULL) {
988 pci_unmap_single(priv->pdev,
989 pci_unmap_addr(&rxq->buf[i], dma),
990 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
991 pci_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100992
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200993 kfree_skb(rxq->buf[i].skb);
994 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100995 }
996 }
997
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200998 kfree(rxq->buf);
999 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001000
1001 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001002 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001003 rxq->rxd, rxq->rxd_dma);
1004 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001005}
1006
1007
1008/*
1009 * Scan a list of BSSIDs to process for finalize join.
1010 * Allows for extension to process multiple BSSIDs.
1011 */
1012static inline int
1013mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1014{
1015 return priv->capture_beacon &&
1016 ieee80211_is_beacon(wh->frame_control) &&
1017 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1018}
1019
Lennert Buytenhek37797522009-10-22 20:19:45 +02001020static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1021 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001022{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001023 struct mwl8k_priv *priv = hw->priv;
1024
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001025 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001026 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001027
1028 /*
1029 * Use GFP_ATOMIC as rxq_process is called from
1030 * the primary interrupt handler, memory allocation call
1031 * must not sleep.
1032 */
1033 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1034 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001035 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001036}
1037
1038static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1039{
1040 struct mwl8k_priv *priv = hw->priv;
1041 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1042 int processed;
1043
1044 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001045 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001046 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001047 void *rxd;
1048 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001049 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001050 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001051
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001052 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001053 if (skb == NULL)
1054 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001055
1056 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1057
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001058 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001059 if (pkt_len < 0)
1060 break;
1061
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001062 rxq->buf[rxq->head].skb = NULL;
1063
1064 pci_unmap_single(priv->pdev,
1065 pci_unmap_addr(&rxq->buf[rxq->head], dma),
1066 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
1067 pci_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001068
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001069 rxq->head++;
1070 if (rxq->head == MWL8K_RX_DESCS)
1071 rxq->head = 0;
1072
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001073 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001074
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001075 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001076 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001077
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001078 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001079 * Check for a pending join operation. Save a
1080 * copy of the beacon and schedule a tasklet to
1081 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001082 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001083 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001084 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001085
Johannes Bergf1d58c22009-06-17 13:13:00 +02001086 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1087 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001088
1089 processed++;
1090 }
1091
1092 return processed;
1093}
1094
1095
1096/*
1097 * Packet transmission.
1098 */
1099
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001100#define MWL8K_TXD_STATUS_OK 0x00000001
1101#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1102#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1103#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001104#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001105
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001106#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1107#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1108#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1109#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1110#define MWL8K_QOS_EOSP 0x0010
1111
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001112struct mwl8k_tx_desc {
1113 __le32 status;
1114 __u8 data_rate;
1115 __u8 tx_priority;
1116 __le16 qos_control;
1117 __le32 pkt_phys_addr;
1118 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001119 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001120 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001121 __le32 reserved;
1122 __le16 rate_info;
1123 __u8 peer_id;
1124 __u8 tx_frag_cnt;
1125} __attribute__((packed));
1126
1127#define MWL8K_TX_DESCS 128
1128
1129static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1130{
1131 struct mwl8k_priv *priv = hw->priv;
1132 struct mwl8k_tx_queue *txq = priv->txq + index;
1133 int size;
1134 int i;
1135
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001136 memset(&txq->stats, 0, sizeof(struct ieee80211_tx_queue_stats));
1137 txq->stats.limit = MWL8K_TX_DESCS;
1138 txq->head = 0;
1139 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001140
1141 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1142
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001143 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1144 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001145 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001146 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001147 return -ENOMEM;
1148 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001149 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001150
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001151 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1152 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001153 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001154 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001155 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001156 return -ENOMEM;
1157 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001158 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159
1160 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1161 struct mwl8k_tx_desc *tx_desc;
1162 int nexti;
1163
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001164 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001165 nexti = (i + 1) % MWL8K_TX_DESCS;
1166
1167 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001168 tx_desc->next_txd_phys_addr =
1169 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001170 }
1171
1172 return 0;
1173}
1174
1175static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1176{
1177 iowrite32(MWL8K_H2A_INT_PPA_READY,
1178 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1179 iowrite32(MWL8K_H2A_INT_DUMMY,
1180 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1181 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1182}
1183
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001184static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001185{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001186 struct mwl8k_priv *priv = hw->priv;
1187 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001188
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001189 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1190 struct mwl8k_tx_queue *txq = priv->txq + i;
1191 int fw_owned = 0;
1192 int drv_owned = 0;
1193 int unused = 0;
1194 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001195
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001196 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001197 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1198 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001199
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001200 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001201 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001202 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001203 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001204 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001205
1206 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001207 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001208 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001209
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001210 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1211 "fw_owned=%d drv_owned=%d unused=%d\n",
1212 wiphy_name(hw->wiphy), i,
1213 txq->stats.len, txq->head, txq->tail,
1214 fw_owned, drv_owned, unused);
1215 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001216}
1217
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001218/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001219 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001220 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001221#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001222
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001223static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001224{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001225 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001226 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001227 int retry;
1228 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001229
1230 might_sleep();
1231
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001232 /*
1233 * The TX queues are stopped at this point, so this test
1234 * doesn't need to take ->tx_lock.
1235 */
1236 if (!priv->pending_tx_pkts)
1237 return 0;
1238
1239 retry = 0;
1240 rc = 0;
1241
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001242 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001243 priv->tx_wait = &tx_wait;
1244 while (!rc) {
1245 int oldcount;
1246 unsigned long timeout;
1247
1248 oldcount = priv->pending_tx_pkts;
1249
1250 spin_unlock_bh(&priv->tx_lock);
1251 timeout = wait_for_completion_timeout(&tx_wait,
1252 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1253 spin_lock_bh(&priv->tx_lock);
1254
1255 if (timeout) {
1256 WARN_ON(priv->pending_tx_pkts);
1257 if (retry) {
1258 printk(KERN_NOTICE "%s: tx rings drained\n",
1259 wiphy_name(hw->wiphy));
1260 }
1261 break;
1262 }
1263
1264 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001265 printk(KERN_NOTICE "%s: waiting for tx rings "
1266 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001267 wiphy_name(hw->wiphy), oldcount,
1268 priv->pending_tx_pkts);
1269 retry = 1;
1270 continue;
1271 }
1272
1273 priv->tx_wait = NULL;
1274
1275 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1276 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1277 mwl8k_dump_tx_rings(hw);
1278
1279 rc = -ETIMEDOUT;
1280 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001281 spin_unlock_bh(&priv->tx_lock);
1282
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001283 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001284}
1285
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001286#define MWL8K_TXD_SUCCESS(status) \
1287 ((status) & (MWL8K_TXD_STATUS_OK | \
1288 MWL8K_TXD_STATUS_OK_RETRY | \
1289 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001290
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001291static int
1292mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001293{
1294 struct mwl8k_priv *priv = hw->priv;
1295 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001296 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001297
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001298 processed = 0;
1299 while (txq->stats.len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001301 struct mwl8k_tx_desc *tx_desc;
1302 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001303 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001304 struct sk_buff *skb;
1305 struct ieee80211_tx_info *info;
1306 u32 status;
1307
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001308 tx = txq->head;
1309 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001310
1311 status = le32_to_cpu(tx_desc->status);
1312
1313 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1314 if (!force)
1315 break;
1316 tx_desc->status &=
1317 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1318 }
1319
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001320 txq->head = (tx + 1) % MWL8K_TX_DESCS;
1321 BUG_ON(txq->stats.len == 0);
1322 txq->stats.len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001323 priv->pending_tx_pkts--;
1324
1325 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001326 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001327 skb = txq->skb[tx];
1328 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001329
1330 BUG_ON(skb == NULL);
1331 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1332
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001333 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001334
1335 /* Mark descriptor as unused */
1336 tx_desc->pkt_phys_addr = 0;
1337 tx_desc->pkt_len = 0;
1338
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001339 info = IEEE80211_SKB_CB(skb);
1340 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001341 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001342 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001343
1344 ieee80211_tx_status_irqsafe(hw, skb);
1345
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001346 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001347 }
1348
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001349 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001350 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001351
1352 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001353}
1354
1355/* must be called only when the card's transmit is completely halted */
1356static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1357{
1358 struct mwl8k_priv *priv = hw->priv;
1359 struct mwl8k_tx_queue *txq = priv->txq + index;
1360
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001361 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001362
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001363 kfree(txq->skb);
1364 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001365
1366 pci_free_consistent(priv->pdev,
1367 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001368 txq->txd, txq->txd_dma);
1369 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001370}
1371
1372static int
1373mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1374{
1375 struct mwl8k_priv *priv = hw->priv;
1376 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001377 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001378 struct ieee80211_hdr *wh;
1379 struct mwl8k_tx_queue *txq;
1380 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001381 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001382 u32 txstatus;
1383 u8 txdatarate;
1384 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001385
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001386 wh = (struct ieee80211_hdr *)skb->data;
1387 if (ieee80211_is_data_qos(wh->frame_control))
1388 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1389 else
1390 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001391
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001392 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001393 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001394
1395 tx_info = IEEE80211_SKB_CB(skb);
1396 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001397
1398 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001399 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001400 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1401 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001402 }
1403
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001404 /* Setup firmware control bit fields for each frame type. */
1405 txstatus = 0;
1406 txdatarate = 0;
1407 if (ieee80211_is_mgmt(wh->frame_control) ||
1408 ieee80211_is_ctl(wh->frame_control)) {
1409 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001410 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001411 } else if (ieee80211_is_data(wh->frame_control)) {
1412 txdatarate = 1;
1413 if (is_multicast_ether_addr(wh->addr1))
1414 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1415
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001416 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001417 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001418 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001419 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001420 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001421 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001422
1423 dma = pci_map_single(priv->pdev, skb->data,
1424 skb->len, PCI_DMA_TODEVICE);
1425
1426 if (pci_dma_mapping_error(priv->pdev, dma)) {
1427 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001428 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001429 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001430 return NETDEV_TX_OK;
1431 }
1432
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001433 spin_lock_bh(&priv->tx_lock);
1434
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001435 txq = priv->txq + index;
1436
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001437 BUG_ON(txq->skb[txq->tail] != NULL);
1438 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001439
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001440 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001441 tx->data_rate = txdatarate;
1442 tx->tx_priority = index;
1443 tx->qos_control = cpu_to_le16(qos);
1444 tx->pkt_phys_addr = cpu_to_le32(dma);
1445 tx->pkt_len = cpu_to_le16(skb->len);
1446 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001447 if (!priv->ap_fw && tx_info->control.sta != NULL)
1448 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1449 else
1450 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001451 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001452 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1453
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001454 txq->stats.count++;
1455 txq->stats.len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001456 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001457
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001458 txq->tail++;
1459 if (txq->tail == MWL8K_TX_DESCS)
1460 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001461
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001462 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001463 ieee80211_stop_queue(hw, index);
1464
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001465 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001466
1467 spin_unlock_bh(&priv->tx_lock);
1468
1469 return NETDEV_TX_OK;
1470}
1471
1472
1473/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001474 * Firmware access.
1475 *
1476 * We have the following requirements for issuing firmware commands:
1477 * - Some commands require that the packet transmit path is idle when
1478 * the command is issued. (For simplicity, we'll just quiesce the
1479 * transmit path for every command.)
1480 * - There are certain sequences of commands that need to be issued to
1481 * the hardware sequentially, with no other intervening commands.
1482 *
1483 * This leads to an implementation of a "firmware lock" as a mutex that
1484 * can be taken recursively, and which is taken by both the low-level
1485 * command submission function (mwl8k_post_cmd) as well as any users of
1486 * that function that require issuing of an atomic sequence of commands,
1487 * and quiesces the transmit path whenever it's taken.
1488 */
1489static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1490{
1491 struct mwl8k_priv *priv = hw->priv;
1492
1493 if (priv->fw_mutex_owner != current) {
1494 int rc;
1495
1496 mutex_lock(&priv->fw_mutex);
1497 ieee80211_stop_queues(hw);
1498
1499 rc = mwl8k_tx_wait_empty(hw);
1500 if (rc) {
1501 ieee80211_wake_queues(hw);
1502 mutex_unlock(&priv->fw_mutex);
1503
1504 return rc;
1505 }
1506
1507 priv->fw_mutex_owner = current;
1508 }
1509
1510 priv->fw_mutex_depth++;
1511
1512 return 0;
1513}
1514
1515static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1516{
1517 struct mwl8k_priv *priv = hw->priv;
1518
1519 if (!--priv->fw_mutex_depth) {
1520 ieee80211_wake_queues(hw);
1521 priv->fw_mutex_owner = NULL;
1522 mutex_unlock(&priv->fw_mutex);
1523 }
1524}
1525
1526
1527/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001528 * Command processing.
1529 */
1530
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001531/* Timeout firmware commands after 10s */
1532#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001533
1534static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1535{
1536 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1537 struct mwl8k_priv *priv = hw->priv;
1538 void __iomem *regs = priv->regs;
1539 dma_addr_t dma_addr;
1540 unsigned int dma_size;
1541 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001542 unsigned long timeout = 0;
1543 u8 buf[32];
1544
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001545 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001546 dma_size = le16_to_cpu(cmd->length);
1547 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1548 PCI_DMA_BIDIRECTIONAL);
1549 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1550 return -ENOMEM;
1551
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001552 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001553 if (rc) {
1554 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1555 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001556 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001557 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001558
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001559 priv->hostcmd_wait = &cmd_wait;
1560 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1561 iowrite32(MWL8K_H2A_INT_DOORBELL,
1562 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1563 iowrite32(MWL8K_H2A_INT_DUMMY,
1564 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001565
1566 timeout = wait_for_completion_timeout(&cmd_wait,
1567 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1568
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001569 priv->hostcmd_wait = NULL;
1570
1571 mwl8k_fw_unlock(hw);
1572
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001573 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1574 PCI_DMA_BIDIRECTIONAL);
1575
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001576 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001577 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001578 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001579 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1580 MWL8K_CMD_TIMEOUT_MS);
1581 rc = -ETIMEDOUT;
1582 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001583 int ms;
1584
1585 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1586
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001587 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001588 if (rc)
1589 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001590 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001591 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001592 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001593 else if (ms > 2000)
1594 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1595 wiphy_name(hw->wiphy),
1596 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1597 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001598 }
1599
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001600 return rc;
1601}
1602
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001603static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1604 struct ieee80211_vif *vif,
1605 struct mwl8k_cmd_pkt *cmd)
1606{
1607 if (vif != NULL)
1608 cmd->macid = MWL8K_VIF(vif)->macid;
1609 return mwl8k_post_cmd(hw, cmd);
1610}
1611
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001612/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001613 * Setup code shared between STA and AP firmware images.
1614 */
1615static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1616{
1617 struct mwl8k_priv *priv = hw->priv;
1618
1619 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1620 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1621
1622 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1623 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1624
1625 priv->band_24.band = IEEE80211_BAND_2GHZ;
1626 priv->band_24.channels = priv->channels_24;
1627 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1628 priv->band_24.bitrates = priv->rates_24;
1629 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1630
1631 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1632}
1633
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001634static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1635{
1636 struct mwl8k_priv *priv = hw->priv;
1637
1638 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1639 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1640
1641 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1642 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1643
1644 priv->band_50.band = IEEE80211_BAND_5GHZ;
1645 priv->band_50.channels = priv->channels_50;
1646 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1647 priv->band_50.bitrates = priv->rates_50;
1648 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1649
1650 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1651}
1652
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001653/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001654 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001655 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001656struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 struct mwl8k_cmd_pkt header;
1658 __u8 hw_rev;
1659 __u8 host_interface;
1660 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001661 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001662 __le16 region_code;
1663 __le32 fw_rev;
1664 __le32 ps_cookie;
1665 __le32 caps;
1666 __u8 mcs_bitmap[16];
1667 __le32 rx_queue_ptr;
1668 __le32 num_tx_queues;
1669 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1670 __le32 caps2;
1671 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001672 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001673} __attribute__((packed));
1674
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001675#define MWL8K_CAP_MAX_AMSDU 0x20000000
1676#define MWL8K_CAP_GREENFIELD 0x08000000
1677#define MWL8K_CAP_AMPDU 0x04000000
1678#define MWL8K_CAP_RX_STBC 0x01000000
1679#define MWL8K_CAP_TX_STBC 0x00800000
1680#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1681#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1682#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1683#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1684#define MWL8K_CAP_DELAY_BA 0x00003000
1685#define MWL8K_CAP_MIMO 0x00000200
1686#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001687#define MWL8K_CAP_BAND_MASK 0x00000007
1688#define MWL8K_CAP_5GHZ 0x00000004
1689#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001690
Lennert Buytenhek06953232010-01-12 13:49:41 +01001691static void
1692mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1693 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001694{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001695 int rx_streams;
1696 int tx_streams;
1697
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001698 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001699
1700 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001701 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001702 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001703 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001704 if (cap & MWL8K_CAP_AMPDU) {
1705 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001706 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1707 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001708 }
1709 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001710 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001711 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001712 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001713 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001714 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001715 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001716 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001717 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001718 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001719 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001720 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001721
1722 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1723 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1724
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001725 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001726 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001727 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001728 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001729 band->ht_cap.mcs.rx_mask[2] = 0xff;
1730 band->ht_cap.mcs.rx_mask[4] = 0x01;
1731 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001732
1733 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001734 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1735 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001736 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1737 }
1738}
1739
Lennert Buytenhek06953232010-01-12 13:49:41 +01001740static void
1741mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1742{
1743 struct mwl8k_priv *priv = hw->priv;
1744
1745 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1746 mwl8k_setup_2ghz_band(hw);
1747 if (caps & MWL8K_CAP_MIMO)
1748 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1749 }
1750
1751 if (caps & MWL8K_CAP_5GHZ) {
1752 mwl8k_setup_5ghz_band(hw);
1753 if (caps & MWL8K_CAP_MIMO)
1754 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1755 }
1756}
1757
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001758static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001759{
1760 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001761 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001762 int rc;
1763 int i;
1764
1765 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1766 if (cmd == NULL)
1767 return -ENOMEM;
1768
1769 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1770 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1771
1772 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1773 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001774 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001775 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001776 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001777 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001778 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001779 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001780
1781 rc = mwl8k_post_cmd(hw, &cmd->header);
1782
1783 if (!rc) {
1784 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1785 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001786 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001787 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001788 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001789 }
1790
1791 kfree(cmd);
1792 return rc;
1793}
1794
1795/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001796 * CMD_GET_HW_SPEC (AP version).
1797 */
1798struct mwl8k_cmd_get_hw_spec_ap {
1799 struct mwl8k_cmd_pkt header;
1800 __u8 hw_rev;
1801 __u8 host_interface;
1802 __le16 num_wcb;
1803 __le16 num_mcaddrs;
1804 __u8 perm_addr[ETH_ALEN];
1805 __le16 region_code;
1806 __le16 num_antenna;
1807 __le32 fw_rev;
1808 __le32 wcbbase0;
1809 __le32 rxwrptr;
1810 __le32 rxrdptr;
1811 __le32 ps_cookie;
1812 __le32 wcbbase1;
1813 __le32 wcbbase2;
1814 __le32 wcbbase3;
1815} __attribute__((packed));
1816
1817static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1818{
1819 struct mwl8k_priv *priv = hw->priv;
1820 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1821 int rc;
1822
1823 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1824 if (cmd == NULL)
1825 return -ENOMEM;
1826
1827 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1828 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1829
1830 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1831 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1832
1833 rc = mwl8k_post_cmd(hw, &cmd->header);
1834
1835 if (!rc) {
1836 int off;
1837
1838 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1839 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1840 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1841 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001842 mwl8k_setup_2ghz_band(hw);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001843
1844 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1845 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1846
1847 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1848 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1849
1850 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1851 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1852
1853 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1854 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1855
1856 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1857 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1858
1859 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1860 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1861 }
1862
1863 kfree(cmd);
1864 return rc;
1865}
1866
1867/*
1868 * CMD_SET_HW_SPEC.
1869 */
1870struct mwl8k_cmd_set_hw_spec {
1871 struct mwl8k_cmd_pkt header;
1872 __u8 hw_rev;
1873 __u8 host_interface;
1874 __le16 num_mcaddrs;
1875 __u8 perm_addr[ETH_ALEN];
1876 __le16 region_code;
1877 __le32 fw_rev;
1878 __le32 ps_cookie;
1879 __le32 caps;
1880 __le32 rx_queue_ptr;
1881 __le32 num_tx_queues;
1882 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1883 __le32 flags;
1884 __le32 num_tx_desc_per_queue;
1885 __le32 total_rxd;
1886} __attribute__((packed));
1887
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001888#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1889#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
1890#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001891
1892static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1893{
1894 struct mwl8k_priv *priv = hw->priv;
1895 struct mwl8k_cmd_set_hw_spec *cmd;
1896 int rc;
1897 int i;
1898
1899 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1900 if (cmd == NULL)
1901 return -ENOMEM;
1902
1903 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1904 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1905
1906 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1907 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1908 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1909 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1910 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001911 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
1912 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
1913 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001914 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1915 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1916
1917 rc = mwl8k_post_cmd(hw, &cmd->header);
1918 kfree(cmd);
1919
1920 return rc;
1921}
1922
1923/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001924 * CMD_MAC_MULTICAST_ADR.
1925 */
1926struct mwl8k_cmd_mac_multicast_adr {
1927 struct mwl8k_cmd_pkt header;
1928 __le16 action;
1929 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001930 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001931};
1932
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001933#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1934#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1935#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1936#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001937
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001938static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001939__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001940 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001941{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001942 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001943 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001944 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001945
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001946 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001947 allmulti = 1;
1948 mc_count = 0;
1949 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001950
1951 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1952
1953 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001954 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001955 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001956
1957 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1958 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001959 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1960 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001961
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001962 if (allmulti) {
1963 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1964 } else if (mc_count) {
1965 int i;
1966
1967 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1968 cmd->numaddr = cpu_to_le16(mc_count);
1969 for (i = 0; i < mc_count && mclist; i++) {
1970 if (mclist->da_addrlen != ETH_ALEN) {
1971 kfree(cmd);
1972 return NULL;
1973 }
1974 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1975 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001976 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001977 }
1978
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001979 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001980}
1981
1982/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001983 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001984 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001985struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001986 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001987 __le32 stats[64];
1988} __attribute__((packed));
1989
1990#define MWL8K_STAT_ACK_FAILURE 9
1991#define MWL8K_STAT_RTS_FAILURE 12
1992#define MWL8K_STAT_FCS_ERROR 24
1993#define MWL8K_STAT_RTS_SUCCESS 11
1994
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001995static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1996 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001997{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001998 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001999 int rc;
2000
2001 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2002 if (cmd == NULL)
2003 return -ENOMEM;
2004
2005 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2006 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002007
2008 rc = mwl8k_post_cmd(hw, &cmd->header);
2009 if (!rc) {
2010 stats->dot11ACKFailureCount =
2011 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2012 stats->dot11RTSFailureCount =
2013 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2014 stats->dot11FCSErrorCount =
2015 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2016 stats->dot11RTSSuccessCount =
2017 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2018 }
2019 kfree(cmd);
2020
2021 return rc;
2022}
2023
2024/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002025 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002026 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002027struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002028 struct mwl8k_cmd_pkt header;
2029 __le16 action;
2030 __le16 control;
2031 __le16 radio_on;
2032} __attribute__((packed));
2033
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002034static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002035mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002036{
2037 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002038 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002039 int rc;
2040
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002041 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002042 return 0;
2043
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002044 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2045 if (cmd == NULL)
2046 return -ENOMEM;
2047
2048 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2049 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2050 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002051 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002052 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2053
2054 rc = mwl8k_post_cmd(hw, &cmd->header);
2055 kfree(cmd);
2056
2057 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002058 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002059
2060 return rc;
2061}
2062
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002063static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002064{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002065 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002066}
2067
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002068static int mwl8k_cmd_radio_enable(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, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002071}
2072
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002073static int
2074mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2075{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002076 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002077
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002078 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002079
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002080 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002081}
2082
2083/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002084 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002085 */
2086#define MWL8K_TX_POWER_LEVEL_TOTAL 8
2087
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002088struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002089 struct mwl8k_cmd_pkt header;
2090 __le16 action;
2091 __le16 support_level;
2092 __le16 current_level;
2093 __le16 reserved;
2094 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2095} __attribute__((packed));
2096
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002097static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002098{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002099 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002100 int rc;
2101
2102 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2103 if (cmd == NULL)
2104 return -ENOMEM;
2105
2106 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2107 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2108 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2109 cmd->support_level = cpu_to_le16(dBm);
2110
2111 rc = mwl8k_post_cmd(hw, &cmd->header);
2112 kfree(cmd);
2113
2114 return rc;
2115}
2116
2117/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002118 * CMD_RF_ANTENNA.
2119 */
2120struct mwl8k_cmd_rf_antenna {
2121 struct mwl8k_cmd_pkt header;
2122 __le16 antenna;
2123 __le16 mode;
2124} __attribute__((packed));
2125
2126#define MWL8K_RF_ANTENNA_RX 1
2127#define MWL8K_RF_ANTENNA_TX 2
2128
2129static int
2130mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2131{
2132 struct mwl8k_cmd_rf_antenna *cmd;
2133 int rc;
2134
2135 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2136 if (cmd == NULL)
2137 return -ENOMEM;
2138
2139 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2140 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2141 cmd->antenna = cpu_to_le16(antenna);
2142 cmd->mode = cpu_to_le16(mask);
2143
2144 rc = mwl8k_post_cmd(hw, &cmd->header);
2145 kfree(cmd);
2146
2147 return rc;
2148}
2149
2150/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002151 * CMD_SET_BEACON.
2152 */
2153struct mwl8k_cmd_set_beacon {
2154 struct mwl8k_cmd_pkt header;
2155 __le16 beacon_len;
2156 __u8 beacon[0];
2157};
2158
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002159static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2160 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002161{
2162 struct mwl8k_cmd_set_beacon *cmd;
2163 int rc;
2164
2165 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2166 if (cmd == NULL)
2167 return -ENOMEM;
2168
2169 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2170 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2171 cmd->beacon_len = cpu_to_le16(len);
2172 memcpy(cmd->beacon, beacon, len);
2173
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002174 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002175 kfree(cmd);
2176
2177 return rc;
2178}
2179
2180/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002181 * CMD_SET_PRE_SCAN.
2182 */
2183struct mwl8k_cmd_set_pre_scan {
2184 struct mwl8k_cmd_pkt header;
2185} __attribute__((packed));
2186
2187static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2188{
2189 struct mwl8k_cmd_set_pre_scan *cmd;
2190 int rc;
2191
2192 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2193 if (cmd == NULL)
2194 return -ENOMEM;
2195
2196 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2197 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2198
2199 rc = mwl8k_post_cmd(hw, &cmd->header);
2200 kfree(cmd);
2201
2202 return rc;
2203}
2204
2205/*
2206 * CMD_SET_POST_SCAN.
2207 */
2208struct mwl8k_cmd_set_post_scan {
2209 struct mwl8k_cmd_pkt header;
2210 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002211 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002212} __attribute__((packed));
2213
2214static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002215mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002216{
2217 struct mwl8k_cmd_set_post_scan *cmd;
2218 int rc;
2219
2220 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2221 if (cmd == NULL)
2222 return -ENOMEM;
2223
2224 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2225 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2226 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002227 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002228
2229 rc = mwl8k_post_cmd(hw, &cmd->header);
2230 kfree(cmd);
2231
2232 return rc;
2233}
2234
2235/*
2236 * CMD_SET_RF_CHANNEL.
2237 */
2238struct mwl8k_cmd_set_rf_channel {
2239 struct mwl8k_cmd_pkt header;
2240 __le16 action;
2241 __u8 current_channel;
2242 __le32 channel_flags;
2243} __attribute__((packed));
2244
2245static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002246 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002247{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002248 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002249 struct mwl8k_cmd_set_rf_channel *cmd;
2250 int rc;
2251
2252 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2253 if (cmd == NULL)
2254 return -ENOMEM;
2255
2256 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2257 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2258 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2259 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002260
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002261 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002262 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002263 else if (channel->band == IEEE80211_BAND_5GHZ)
2264 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002265
2266 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2267 conf->channel_type == NL80211_CHAN_HT20)
2268 cmd->channel_flags |= cpu_to_le32(0x00000080);
2269 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2270 cmd->channel_flags |= cpu_to_le32(0x000001900);
2271 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2272 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002273
2274 rc = mwl8k_post_cmd(hw, &cmd->header);
2275 kfree(cmd);
2276
2277 return rc;
2278}
2279
2280/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002281 * CMD_SET_AID.
2282 */
2283#define MWL8K_FRAME_PROT_DISABLED 0x00
2284#define MWL8K_FRAME_PROT_11G 0x07
2285#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2286#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2287
2288struct mwl8k_cmd_update_set_aid {
2289 struct mwl8k_cmd_pkt header;
2290 __le16 aid;
2291
2292 /* AP's MAC address (BSSID) */
2293 __u8 bssid[ETH_ALEN];
2294 __le16 protection_mode;
2295 __u8 supp_rates[14];
2296} __attribute__((packed));
2297
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002298static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2299{
2300 int i;
2301 int j;
2302
2303 /*
2304 * Clear nonstandard rates 4 and 13.
2305 */
2306 mask &= 0x1fef;
2307
2308 for (i = 0, j = 0; i < 14; i++) {
2309 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002310 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002311 }
2312}
2313
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002314static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002315mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2316 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002317{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002318 struct mwl8k_cmd_update_set_aid *cmd;
2319 u16 prot_mode;
2320 int rc;
2321
2322 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2323 if (cmd == NULL)
2324 return -ENOMEM;
2325
2326 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2327 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002328 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002329 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002330
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002331 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002332 prot_mode = MWL8K_FRAME_PROT_11G;
2333 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002334 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002335 IEEE80211_HT_OP_MODE_PROTECTION) {
2336 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2337 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2338 break;
2339 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2340 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2341 break;
2342 default:
2343 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2344 break;
2345 }
2346 }
2347 cmd->protection_mode = cpu_to_le16(prot_mode);
2348
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002349 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002350
2351 rc = mwl8k_post_cmd(hw, &cmd->header);
2352 kfree(cmd);
2353
2354 return rc;
2355}
2356
2357/*
2358 * CMD_SET_RATE.
2359 */
2360struct mwl8k_cmd_set_rate {
2361 struct mwl8k_cmd_pkt header;
2362 __u8 legacy_rates[14];
2363
2364 /* Bitmap for supported MCS codes. */
2365 __u8 mcs_set[16];
2366 __u8 reserved[16];
2367} __attribute__((packed));
2368
2369static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002370mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002371 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002372{
2373 struct mwl8k_cmd_set_rate *cmd;
2374 int rc;
2375
2376 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2377 if (cmd == NULL)
2378 return -ENOMEM;
2379
2380 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2381 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002382 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002383 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002384
2385 rc = mwl8k_post_cmd(hw, &cmd->header);
2386 kfree(cmd);
2387
2388 return rc;
2389}
2390
2391/*
2392 * CMD_FINALIZE_JOIN.
2393 */
2394#define MWL8K_FJ_BEACON_MAXLEN 128
2395
2396struct mwl8k_cmd_finalize_join {
2397 struct mwl8k_cmd_pkt header;
2398 __le32 sleep_interval; /* Number of beacon periods to sleep */
2399 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2400} __attribute__((packed));
2401
2402static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2403 int framelen, int dtim)
2404{
2405 struct mwl8k_cmd_finalize_join *cmd;
2406 struct ieee80211_mgmt *payload = frame;
2407 int payload_len;
2408 int rc;
2409
2410 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2411 if (cmd == NULL)
2412 return -ENOMEM;
2413
2414 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2415 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2416 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2417
2418 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2419 if (payload_len < 0)
2420 payload_len = 0;
2421 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2422 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2423
2424 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2425
2426 rc = mwl8k_post_cmd(hw, &cmd->header);
2427 kfree(cmd);
2428
2429 return rc;
2430}
2431
2432/*
2433 * CMD_SET_RTS_THRESHOLD.
2434 */
2435struct mwl8k_cmd_set_rts_threshold {
2436 struct mwl8k_cmd_pkt header;
2437 __le16 action;
2438 __le16 threshold;
2439} __attribute__((packed));
2440
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002441static int
2442mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002443{
2444 struct mwl8k_cmd_set_rts_threshold *cmd;
2445 int rc;
2446
2447 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2448 if (cmd == NULL)
2449 return -ENOMEM;
2450
2451 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2452 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002453 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2454 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002455
2456 rc = mwl8k_post_cmd(hw, &cmd->header);
2457 kfree(cmd);
2458
2459 return rc;
2460}
2461
2462/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002463 * CMD_SET_SLOT.
2464 */
2465struct mwl8k_cmd_set_slot {
2466 struct mwl8k_cmd_pkt header;
2467 __le16 action;
2468 __u8 short_slot;
2469} __attribute__((packed));
2470
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002471static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002472{
2473 struct mwl8k_cmd_set_slot *cmd;
2474 int rc;
2475
2476 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2477 if (cmd == NULL)
2478 return -ENOMEM;
2479
2480 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2481 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2482 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002483 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002484
2485 rc = mwl8k_post_cmd(hw, &cmd->header);
2486 kfree(cmd);
2487
2488 return rc;
2489}
2490
2491/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002492 * CMD_SET_EDCA_PARAMS.
2493 */
2494struct mwl8k_cmd_set_edca_params {
2495 struct mwl8k_cmd_pkt header;
2496
2497 /* See MWL8K_SET_EDCA_XXX below */
2498 __le16 action;
2499
2500 /* TX opportunity in units of 32 us */
2501 __le16 txop;
2502
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002503 union {
2504 struct {
2505 /* Log exponent of max contention period: 0...15 */
2506 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002507
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002508 /* Log exponent of min contention period: 0...15 */
2509 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002510
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002511 /* Adaptive interframe spacing in units of 32us */
2512 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002513
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002514 /* TX queue to configure */
2515 __u8 txq;
2516 } ap;
2517 struct {
2518 /* Log exponent of max contention period: 0...15 */
2519 __u8 log_cw_max;
2520
2521 /* Log exponent of min contention period: 0...15 */
2522 __u8 log_cw_min;
2523
2524 /* Adaptive interframe spacing in units of 32us */
2525 __u8 aifs;
2526
2527 /* TX queue to configure */
2528 __u8 txq;
2529 } sta;
2530 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002531} __attribute__((packed));
2532
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002533#define MWL8K_SET_EDCA_CW 0x01
2534#define MWL8K_SET_EDCA_TXOP 0x02
2535#define MWL8K_SET_EDCA_AIFS 0x04
2536
2537#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2538 MWL8K_SET_EDCA_TXOP | \
2539 MWL8K_SET_EDCA_AIFS)
2540
2541static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002542mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2543 __u16 cw_min, __u16 cw_max,
2544 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002545{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002546 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002547 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002548 int rc;
2549
2550 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2551 if (cmd == NULL)
2552 return -ENOMEM;
2553
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002554 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2555 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002556 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2557 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002558 if (priv->ap_fw) {
2559 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2560 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2561 cmd->ap.aifs = aifs;
2562 cmd->ap.txq = qnum;
2563 } else {
2564 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2565 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2566 cmd->sta.aifs = aifs;
2567 cmd->sta.txq = qnum;
2568 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002569
2570 rc = mwl8k_post_cmd(hw, &cmd->header);
2571 kfree(cmd);
2572
2573 return rc;
2574}
2575
2576/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002577 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002578 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002579struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002580 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002581 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002582} __attribute__((packed));
2583
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002584static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002585{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002586 struct mwl8k_priv *priv = hw->priv;
2587 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002588 int rc;
2589
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002590 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2591 if (cmd == NULL)
2592 return -ENOMEM;
2593
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002594 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002595 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002596 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002597
2598 rc = mwl8k_post_cmd(hw, &cmd->header);
2599 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002600
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002601 if (!rc)
2602 priv->wmm_enabled = enable;
2603
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002604 return rc;
2605}
2606
2607/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002608 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002609 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002610struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002611 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002612 __le32 action;
2613 __u8 rx_antenna_map;
2614 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002615} __attribute__((packed));
2616
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002617static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002618{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002619 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002620 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002621
2622 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2623 if (cmd == NULL)
2624 return -ENOMEM;
2625
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002626 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002627 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002628 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2629 cmd->rx_antenna_map = rx;
2630 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002631
2632 rc = mwl8k_post_cmd(hw, &cmd->header);
2633 kfree(cmd);
2634
2635 return rc;
2636}
2637
2638/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002639 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002640 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002641struct mwl8k_cmd_use_fixed_rate_sta {
2642 struct mwl8k_cmd_pkt header;
2643 __le32 action;
2644 __le32 allow_rate_drop;
2645 __le32 num_rates;
2646 struct {
2647 __le32 is_ht_rate;
2648 __le32 enable_retry;
2649 __le32 rate;
2650 __le32 retry_count;
2651 } rate_entry[8];
2652 __le32 rate_type;
2653 __le32 reserved1;
2654 __le32 reserved2;
2655} __attribute__((packed));
2656
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002657#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002658#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002659
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002660static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002661{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002662 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002663 int rc;
2664
2665 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2666 if (cmd == NULL)
2667 return -ENOMEM;
2668
2669 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2670 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002671 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2672 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002673
2674 rc = mwl8k_post_cmd(hw, &cmd->header);
2675 kfree(cmd);
2676
2677 return rc;
2678}
2679
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002680/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002681 * CMD_USE_FIXED_RATE (AP version).
2682 */
2683struct mwl8k_cmd_use_fixed_rate_ap {
2684 struct mwl8k_cmd_pkt header;
2685 __le32 action;
2686 __le32 allow_rate_drop;
2687 __le32 num_rates;
2688 struct mwl8k_rate_entry_ap {
2689 __le32 is_ht_rate;
2690 __le32 enable_retry;
2691 __le32 rate;
2692 __le32 retry_count;
2693 } rate_entry[4];
2694 u8 multicast_rate;
2695 u8 multicast_rate_type;
2696 u8 management_rate;
2697} __attribute__((packed));
2698
2699static int
2700mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2701{
2702 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2703 int rc;
2704
2705 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2706 if (cmd == NULL)
2707 return -ENOMEM;
2708
2709 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2710 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2711 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2712 cmd->multicast_rate = mcast;
2713 cmd->management_rate = mgmt;
2714
2715 rc = mwl8k_post_cmd(hw, &cmd->header);
2716 kfree(cmd);
2717
2718 return rc;
2719}
2720
2721/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002722 * CMD_ENABLE_SNIFFER.
2723 */
2724struct mwl8k_cmd_enable_sniffer {
2725 struct mwl8k_cmd_pkt header;
2726 __le32 action;
2727} __attribute__((packed));
2728
2729static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2730{
2731 struct mwl8k_cmd_enable_sniffer *cmd;
2732 int rc;
2733
2734 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2735 if (cmd == NULL)
2736 return -ENOMEM;
2737
2738 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2739 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2740 cmd->action = cpu_to_le32(!!enable);
2741
2742 rc = mwl8k_post_cmd(hw, &cmd->header);
2743 kfree(cmd);
2744
2745 return rc;
2746}
2747
2748/*
2749 * CMD_SET_MAC_ADDR.
2750 */
2751struct mwl8k_cmd_set_mac_addr {
2752 struct mwl8k_cmd_pkt header;
2753 union {
2754 struct {
2755 __le16 mac_type;
2756 __u8 mac_addr[ETH_ALEN];
2757 } mbss;
2758 __u8 mac_addr[ETH_ALEN];
2759 };
2760} __attribute__((packed));
2761
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002762#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
2763#define MWL8K_MAC_TYPE_PRIMARY_AP 2
2764
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002765static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
2766 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002767{
2768 struct mwl8k_priv *priv = hw->priv;
2769 struct mwl8k_cmd_set_mac_addr *cmd;
2770 int rc;
2771
2772 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2773 if (cmd == NULL)
2774 return -ENOMEM;
2775
2776 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2777 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2778 if (priv->ap_fw) {
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002779 cmd->mbss.mac_type = cpu_to_le16(MWL8K_MAC_TYPE_PRIMARY_AP);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002780 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2781 } else {
2782 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2783 }
2784
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002785 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002786 kfree(cmd);
2787
2788 return rc;
2789}
2790
2791/*
2792 * CMD_SET_RATEADAPT_MODE.
2793 */
2794struct mwl8k_cmd_set_rate_adapt_mode {
2795 struct mwl8k_cmd_pkt header;
2796 __le16 action;
2797 __le16 mode;
2798} __attribute__((packed));
2799
2800static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2801{
2802 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2803 int rc;
2804
2805 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2806 if (cmd == NULL)
2807 return -ENOMEM;
2808
2809 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2810 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2811 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2812 cmd->mode = cpu_to_le16(mode);
2813
2814 rc = mwl8k_post_cmd(hw, &cmd->header);
2815 kfree(cmd);
2816
2817 return rc;
2818}
2819
2820/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002821 * CMD_BSS_START.
2822 */
2823struct mwl8k_cmd_bss_start {
2824 struct mwl8k_cmd_pkt header;
2825 __le32 enable;
2826} __attribute__((packed));
2827
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002828static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
2829 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002830{
2831 struct mwl8k_cmd_bss_start *cmd;
2832 int rc;
2833
2834 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2835 if (cmd == NULL)
2836 return -ENOMEM;
2837
2838 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
2839 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2840 cmd->enable = cpu_to_le32(enable);
2841
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002842 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002843 kfree(cmd);
2844
2845 return rc;
2846}
2847
2848/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002849 * CMD_SET_NEW_STN.
2850 */
2851struct mwl8k_cmd_set_new_stn {
2852 struct mwl8k_cmd_pkt header;
2853 __le16 aid;
2854 __u8 mac_addr[6];
2855 __le16 stn_id;
2856 __le16 action;
2857 __le16 rsvd;
2858 __le32 legacy_rates;
2859 __u8 ht_rates[4];
2860 __le16 cap_info;
2861 __le16 ht_capabilities_info;
2862 __u8 mac_ht_param_info;
2863 __u8 rev;
2864 __u8 control_channel;
2865 __u8 add_channel;
2866 __le16 op_mode;
2867 __le16 stbc;
2868 __u8 add_qos_info;
2869 __u8 is_qos_sta;
2870 __le32 fw_sta_ptr;
2871} __attribute__((packed));
2872
2873#define MWL8K_STA_ACTION_ADD 0
2874#define MWL8K_STA_ACTION_REMOVE 2
2875
2876static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
2877 struct ieee80211_vif *vif,
2878 struct ieee80211_sta *sta)
2879{
2880 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002881 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002882 int rc;
2883
2884 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2885 if (cmd == NULL)
2886 return -ENOMEM;
2887
2888 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2889 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2890 cmd->aid = cpu_to_le16(sta->aid);
2891 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
2892 cmd->stn_id = cpu_to_le16(sta->aid);
2893 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002894 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
2895 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
2896 else
2897 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
2898 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002899 if (sta->ht_cap.ht_supported) {
2900 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
2901 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
2902 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
2903 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
2904 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
2905 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
2906 ((sta->ht_cap.ampdu_density & 7) << 2);
2907 cmd->is_qos_sta = 1;
2908 }
2909
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002910 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002911 kfree(cmd);
2912
2913 return rc;
2914}
2915
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002916static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
2917 struct ieee80211_vif *vif)
2918{
2919 struct mwl8k_cmd_set_new_stn *cmd;
2920 int rc;
2921
2922 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2923 if (cmd == NULL)
2924 return -ENOMEM;
2925
2926 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2927 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2928 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
2929
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002930 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002931 kfree(cmd);
2932
2933 return rc;
2934}
2935
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002936static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
2937 struct ieee80211_vif *vif, u8 *addr)
2938{
2939 struct mwl8k_cmd_set_new_stn *cmd;
2940 int rc;
2941
2942 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2943 if (cmd == NULL)
2944 return -ENOMEM;
2945
2946 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2947 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2948 memcpy(cmd->mac_addr, addr, ETH_ALEN);
2949 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
2950
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002951 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002952 kfree(cmd);
2953
2954 return rc;
2955}
2956
2957/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002958 * CMD_UPDATE_STADB.
2959 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002960struct ewc_ht_info {
2961 __le16 control1;
2962 __le16 control2;
2963 __le16 control3;
2964} __attribute__((packed));
2965
2966struct peer_capability_info {
2967 /* Peer type - AP vs. STA. */
2968 __u8 peer_type;
2969
2970 /* Basic 802.11 capabilities from assoc resp. */
2971 __le16 basic_caps;
2972
2973 /* Set if peer supports 802.11n high throughput (HT). */
2974 __u8 ht_support;
2975
2976 /* Valid if HT is supported. */
2977 __le16 ht_caps;
2978 __u8 extended_ht_caps;
2979 struct ewc_ht_info ewc_info;
2980
2981 /* Legacy rate table. Intersection of our rates and peer rates. */
2982 __u8 legacy_rates[12];
2983
2984 /* HT rate table. Intersection of our rates and peer rates. */
2985 __u8 ht_rates[16];
2986 __u8 pad[16];
2987
2988 /* If set, interoperability mode, no proprietary extensions. */
2989 __u8 interop;
2990 __u8 pad2;
2991 __u8 station_id;
2992 __le16 amsdu_enabled;
2993} __attribute__((packed));
2994
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002995struct mwl8k_cmd_update_stadb {
2996 struct mwl8k_cmd_pkt header;
2997
2998 /* See STADB_ACTION_TYPE */
2999 __le32 action;
3000
3001 /* Peer MAC address */
3002 __u8 peer_addr[ETH_ALEN];
3003
3004 __le32 reserved;
3005
3006 /* Peer info - valid during add/update. */
3007 struct peer_capability_info peer_info;
3008} __attribute__((packed));
3009
Lennert Buytenheka6804002010-01-04 21:55:42 +01003010#define MWL8K_STA_DB_MODIFY_ENTRY 1
3011#define MWL8K_STA_DB_DEL_ENTRY 2
3012
3013/* Peer Entry flags - used to define the type of the peer node */
3014#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3015
3016static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003017 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003018 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003019{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003020 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003021 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003022 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003023 int rc;
3024
3025 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3026 if (cmd == NULL)
3027 return -ENOMEM;
3028
3029 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3030 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003031 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003032 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003033
Lennert Buytenheka6804002010-01-04 21:55:42 +01003034 p = &cmd->peer_info;
3035 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3036 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003037 p->ht_support = sta->ht_cap.ht_supported;
3038 p->ht_caps = sta->ht_cap.cap;
3039 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3040 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003041 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3042 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3043 else
3044 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3045 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003046 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003047 p->interop = 1;
3048 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003049
Lennert Buytenheka6804002010-01-04 21:55:42 +01003050 rc = mwl8k_post_cmd(hw, &cmd->header);
3051 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003052
Lennert Buytenheka6804002010-01-04 21:55:42 +01003053 return rc ? rc : p->station_id;
3054}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003055
Lennert Buytenheka6804002010-01-04 21:55:42 +01003056static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3057 struct ieee80211_vif *vif, u8 *addr)
3058{
3059 struct mwl8k_cmd_update_stadb *cmd;
3060 int rc;
3061
3062 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3063 if (cmd == NULL)
3064 return -ENOMEM;
3065
3066 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3067 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3068 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3069 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3070
3071 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003072 kfree(cmd);
3073
3074 return rc;
3075}
3076
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003077
3078/*
3079 * Interrupt handling.
3080 */
3081static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3082{
3083 struct ieee80211_hw *hw = dev_id;
3084 struct mwl8k_priv *priv = hw->priv;
3085 u32 status;
3086
3087 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003088 if (!status)
3089 return IRQ_NONE;
3090
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003091 if (status & MWL8K_A2H_INT_TX_DONE) {
3092 status &= ~MWL8K_A2H_INT_TX_DONE;
3093 tasklet_schedule(&priv->poll_tx_task);
3094 }
3095
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003096 if (status & MWL8K_A2H_INT_RX_READY) {
3097 status &= ~MWL8K_A2H_INT_RX_READY;
3098 tasklet_schedule(&priv->poll_rx_task);
3099 }
3100
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003101 if (status)
3102 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003103
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003104 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003105 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003106 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003107 }
3108
3109 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003110 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003111 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003112 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003113 }
3114
3115 return IRQ_HANDLED;
3116}
3117
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003118static void mwl8k_tx_poll(unsigned long data)
3119{
3120 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3121 struct mwl8k_priv *priv = hw->priv;
3122 int limit;
3123 int i;
3124
3125 limit = 32;
3126
3127 spin_lock_bh(&priv->tx_lock);
3128
3129 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3130 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3131
3132 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3133 complete(priv->tx_wait);
3134 priv->tx_wait = NULL;
3135 }
3136
3137 spin_unlock_bh(&priv->tx_lock);
3138
3139 if (limit) {
3140 writel(~MWL8K_A2H_INT_TX_DONE,
3141 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3142 } else {
3143 tasklet_schedule(&priv->poll_tx_task);
3144 }
3145}
3146
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003147static void mwl8k_rx_poll(unsigned long data)
3148{
3149 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3150 struct mwl8k_priv *priv = hw->priv;
3151 int limit;
3152
3153 limit = 32;
3154 limit -= rxq_process(hw, 0, limit);
3155 limit -= rxq_refill(hw, 0, limit);
3156
3157 if (limit) {
3158 writel(~MWL8K_A2H_INT_RX_READY,
3159 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3160 } else {
3161 tasklet_schedule(&priv->poll_rx_task);
3162 }
3163}
3164
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003165
3166/*
3167 * Core driver operations.
3168 */
3169static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3170{
3171 struct mwl8k_priv *priv = hw->priv;
3172 int index = skb_get_queue_mapping(skb);
3173 int rc;
3174
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003175 if (!priv->radio_on) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003176 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003177 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003178 dev_kfree_skb(skb);
3179 return NETDEV_TX_OK;
3180 }
3181
3182 rc = mwl8k_txq_xmit(hw, index, skb);
3183
3184 return rc;
3185}
3186
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003187static int mwl8k_start(struct ieee80211_hw *hw)
3188{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003189 struct mwl8k_priv *priv = hw->priv;
3190 int rc;
3191
Joe Perchesa0607fd2009-11-18 23:29:17 -08003192 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003193 IRQF_SHARED, MWL8K_NAME, hw);
3194 if (rc) {
3195 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003196 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003197 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003198 }
3199
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003200 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003201 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003202 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003203
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003204 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003205 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003206
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003207 rc = mwl8k_fw_lock(hw);
3208 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003209 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003210
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003211 if (!priv->ap_fw) {
3212 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003213 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003214
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003215 if (!rc)
3216 rc = mwl8k_cmd_set_pre_scan(hw);
3217
3218 if (!rc)
3219 rc = mwl8k_cmd_set_post_scan(hw,
3220 "\x00\x00\x00\x00\x00\x00");
3221 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003222
3223 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003224 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003225
3226 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003227 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003228
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003229 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003230 }
3231
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003232 if (rc) {
3233 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3234 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003235 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003236 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003237 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003238
3239 return rc;
3240}
3241
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003242static void mwl8k_stop(struct ieee80211_hw *hw)
3243{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003244 struct mwl8k_priv *priv = hw->priv;
3245 int i;
3246
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003247 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003248
3249 ieee80211_stop_queues(hw);
3250
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003251 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003252 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003253 free_irq(priv->pdev->irq, hw);
3254
3255 /* Stop finalize join worker */
3256 cancel_work_sync(&priv->finalize_join_worker);
3257 if (priv->beacon_skb != NULL)
3258 dev_kfree_skb(priv->beacon_skb);
3259
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003260 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003261 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003262 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003263
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003264 /* Return all skbs to mac80211 */
3265 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003266 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003267}
3268
3269static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003270 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003271{
3272 struct mwl8k_priv *priv = hw->priv;
3273 struct mwl8k_vif *mwl8k_vif;
3274
3275 /*
3276 * We only support one active interface at a time.
3277 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003278 if (!list_empty(&priv->vif_list))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003279 return -EBUSY;
3280
3281 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003282 * Reject interface creation if sniffer mode is active, as
3283 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003284 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003285 */
3286 if (priv->sniffer_enabled) {
3287 printk(KERN_INFO "%s: unable to create STA "
3288 "interface due to sniffer mode being enabled\n",
3289 wiphy_name(hw->wiphy));
3290 return -EINVAL;
3291 }
3292
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003293 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003294 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003295 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003296 mwl8k_vif->vif = vif;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01003297 mwl8k_vif->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003298 mwl8k_vif->seqno = 0;
3299
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003300 /* Set the mac address. */
3301 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3302
3303 if (priv->ap_fw)
3304 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3305
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003306 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003307
3308 return 0;
3309}
3310
3311static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003312 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003313{
3314 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003315 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003316
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003317 if (priv->ap_fw)
3318 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3319
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003320 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003321
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003322 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003323}
3324
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003325static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003326{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003327 struct ieee80211_conf *conf = &hw->conf;
3328 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003329 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003330
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003331 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003332 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003333 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003334 }
3335
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003336 rc = mwl8k_fw_lock(hw);
3337 if (rc)
3338 return rc;
3339
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003340 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003341 if (rc)
3342 goto out;
3343
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003344 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003345 if (rc)
3346 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003347
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003348 if (conf->power_level > 18)
3349 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003350 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003351 if (rc)
3352 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003353
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003354 if (priv->ap_fw) {
3355 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3356 if (!rc)
3357 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3358 } else {
3359 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3360 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003361
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003362out:
3363 mwl8k_fw_unlock(hw);
3364
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003365 return rc;
3366}
3367
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003368static void
3369mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3370 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003371{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003372 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003373 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003374 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003375 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003376
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003377 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003378 return;
3379
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003380 /*
3381 * No need to capture a beacon if we're no longer associated.
3382 */
3383 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3384 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003385
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003386 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003387 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003388 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003389 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003390 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003391
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003392 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003393
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003394 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003395 if (ap == NULL) {
3396 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003397 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003398 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003399
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003400 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
3401 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
3402 } else {
3403 ap_legacy_rates =
3404 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3405 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003406 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003407
3408 rcu_read_unlock();
3409 }
3410
3411 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003412 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003413 if (rc)
3414 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003415
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003416 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003417 if (rc)
3418 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003419 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003420
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003421 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003422 rc = mwl8k_set_radio_preamble(hw,
3423 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003424 if (rc)
3425 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003426 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003427
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003428 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003429 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003430 if (rc)
3431 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003432 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003433
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003434 if (vif->bss_conf.assoc &&
3435 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
3436 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003437 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003438 if (rc)
3439 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003440 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003441
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003442 if (vif->bss_conf.assoc &&
3443 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003444 /*
3445 * Finalize the join. Tell rx handler to process
3446 * next beacon from our BSSID.
3447 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003448 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003449 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003450 }
3451
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003452out:
3453 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003454}
3455
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003456static void
3457mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3458 struct ieee80211_bss_conf *info, u32 changed)
3459{
3460 int rc;
3461
3462 if (mwl8k_fw_lock(hw))
3463 return;
3464
3465 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3466 rc = mwl8k_set_radio_preamble(hw,
3467 vif->bss_conf.use_short_preamble);
3468 if (rc)
3469 goto out;
3470 }
3471
3472 if (changed & BSS_CHANGED_BASIC_RATES) {
3473 int idx;
3474 int rate;
3475
3476 /*
3477 * Use lowest supported basic rate for multicasts
3478 * and management frames (such as probe responses --
3479 * beacons will always go out at 1 Mb/s).
3480 */
3481 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003482 if (idx)
3483 idx--;
3484
3485 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3486 rate = mwl8k_rates_24[idx].hw_value;
3487 else
3488 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003489
3490 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
3491 }
3492
3493 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
3494 struct sk_buff *skb;
3495
3496 skb = ieee80211_beacon_get(hw, vif);
3497 if (skb != NULL) {
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003498 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003499 kfree_skb(skb);
3500 }
3501 }
3502
3503 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003504 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003505
3506out:
3507 mwl8k_fw_unlock(hw);
3508}
3509
3510static void
3511mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3512 struct ieee80211_bss_conf *info, u32 changed)
3513{
3514 struct mwl8k_priv *priv = hw->priv;
3515
3516 if (!priv->ap_fw)
3517 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
3518 else
3519 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
3520}
3521
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003522static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3523 int mc_count, struct dev_addr_list *mclist)
3524{
3525 struct mwl8k_cmd_pkt *cmd;
3526
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003527 /*
3528 * Synthesize and return a command packet that programs the
3529 * hardware multicast address filter. At this point we don't
3530 * know whether FIF_ALLMULTI is being requested, but if it is,
3531 * we'll end up throwing this packet away and creating a new
3532 * one in mwl8k_configure_filter().
3533 */
3534 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003535
3536 return (unsigned long)cmd;
3537}
3538
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003539static int
3540mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3541 unsigned int changed_flags,
3542 unsigned int *total_flags)
3543{
3544 struct mwl8k_priv *priv = hw->priv;
3545
3546 /*
3547 * Hardware sniffer mode is mutually exclusive with STA
3548 * operation, so refuse to enable sniffer mode if a STA
3549 * interface is active.
3550 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003551 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003552 if (net_ratelimit())
3553 printk(KERN_INFO "%s: not enabling sniffer "
3554 "mode because STA interface is active\n",
3555 wiphy_name(hw->wiphy));
3556 return 0;
3557 }
3558
3559 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003560 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003561 return 0;
3562 priv->sniffer_enabled = true;
3563 }
3564
3565 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3566 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3567 FIF_OTHER_BSS;
3568
3569 return 1;
3570}
3571
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003572static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
3573{
3574 if (!list_empty(&priv->vif_list))
3575 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
3576
3577 return NULL;
3578}
3579
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003580static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3581 unsigned int changed_flags,
3582 unsigned int *total_flags,
3583 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003584{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003585 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003586 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3587
3588 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003589 * AP firmware doesn't allow fine-grained control over
3590 * the receive filter.
3591 */
3592 if (priv->ap_fw) {
3593 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3594 kfree(cmd);
3595 return;
3596 }
3597
3598 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003599 * Enable hardware sniffer mode if FIF_CONTROL or
3600 * FIF_OTHER_BSS is requested.
3601 */
3602 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3603 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3604 kfree(cmd);
3605 return;
3606 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003607
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003608 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003609 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003610
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003611 if (mwl8k_fw_lock(hw)) {
3612 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003613 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003614 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003615
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003616 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003617 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003618 priv->sniffer_enabled = false;
3619 }
3620
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003621 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003622 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3623 /*
3624 * Disable the BSS filter.
3625 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003626 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003627 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003628 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003629 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003630
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003631 /*
3632 * Enable the BSS filter.
3633 *
3634 * If there is an active STA interface, use that
3635 * interface's BSSID, otherwise use a dummy one
3636 * (where the OUI part needs to be nonzero for
3637 * the BSSID to be accepted by POST_SCAN).
3638 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003639 mwl8k_vif = mwl8k_first_vif(priv);
3640 if (mwl8k_vif != NULL)
3641 bssid = mwl8k_vif->vif->bss_conf.bssid;
3642 else
3643 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003644
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003645 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003646 }
3647 }
3648
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003649 /*
3650 * If FIF_ALLMULTI is being requested, throw away the command
3651 * packet that ->prepare_multicast() built and replace it with
3652 * a command packet that enables reception of all multicast
3653 * packets.
3654 */
3655 if (*total_flags & FIF_ALLMULTI) {
3656 kfree(cmd);
3657 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3658 }
3659
3660 if (cmd != NULL) {
3661 mwl8k_post_cmd(hw, cmd);
3662 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003663 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003664
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003665 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003666}
3667
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003668static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3669{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003670 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003671}
3672
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003673struct mwl8k_sta_notify_item
3674{
3675 struct list_head list;
3676 struct ieee80211_vif *vif;
3677 enum sta_notify_cmd cmd;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003678 struct ieee80211_sta sta;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003679};
3680
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003681static void
3682mwl8k_do_sta_notify(struct ieee80211_hw *hw, struct mwl8k_sta_notify_item *s)
3683{
3684 struct mwl8k_priv *priv = hw->priv;
3685
3686 /*
3687 * STA firmware uses UPDATE_STADB, AP firmware uses SET_NEW_STN.
3688 */
3689 if (!priv->ap_fw && s->cmd == STA_NOTIFY_ADD) {
3690 int rc;
3691
3692 rc = mwl8k_cmd_update_stadb_add(hw, s->vif, &s->sta);
3693 if (rc >= 0) {
3694 struct ieee80211_sta *sta;
3695
3696 rcu_read_lock();
3697 sta = ieee80211_find_sta(s->vif, s->sta.addr);
3698 if (sta != NULL)
3699 MWL8K_STA(sta)->peer_id = rc;
3700 rcu_read_unlock();
3701 }
3702 } else if (!priv->ap_fw && s->cmd == STA_NOTIFY_REMOVE) {
3703 mwl8k_cmd_update_stadb_del(hw, s->vif, s->sta.addr);
3704 } else if (priv->ap_fw && s->cmd == STA_NOTIFY_ADD) {
3705 mwl8k_cmd_set_new_stn_add(hw, s->vif, &s->sta);
3706 } else if (priv->ap_fw && s->cmd == STA_NOTIFY_REMOVE) {
3707 mwl8k_cmd_set_new_stn_del(hw, s->vif, s->sta.addr);
3708 }
3709}
3710
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003711static void mwl8k_sta_notify_worker(struct work_struct *work)
3712{
3713 struct mwl8k_priv *priv =
3714 container_of(work, struct mwl8k_priv, sta_notify_worker);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003715 struct ieee80211_hw *hw = priv->hw;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003716
3717 spin_lock_bh(&priv->sta_notify_list_lock);
3718 while (!list_empty(&priv->sta_notify_list)) {
3719 struct mwl8k_sta_notify_item *s;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003720
3721 s = list_entry(priv->sta_notify_list.next,
3722 struct mwl8k_sta_notify_item, list);
3723 list_del(&s->list);
3724
3725 spin_unlock_bh(&priv->sta_notify_list_lock);
3726
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003727 mwl8k_do_sta_notify(hw, s);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003728 kfree(s);
3729
3730 spin_lock_bh(&priv->sta_notify_list_lock);
3731 }
3732 spin_unlock_bh(&priv->sta_notify_list_lock);
3733}
3734
3735static void
3736mwl8k_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3737 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3738{
3739 struct mwl8k_priv *priv = hw->priv;
3740 struct mwl8k_sta_notify_item *s;
3741
3742 if (cmd != STA_NOTIFY_ADD && cmd != STA_NOTIFY_REMOVE)
3743 return;
3744
3745 s = kmalloc(sizeof(*s), GFP_ATOMIC);
3746 if (s != NULL) {
3747 s->vif = vif;
3748 s->cmd = cmd;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003749 s->sta = *sta;
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003750
3751 spin_lock(&priv->sta_notify_list_lock);
3752 list_add_tail(&s->list, &priv->sta_notify_list);
3753 spin_unlock(&priv->sta_notify_list_lock);
3754
3755 ieee80211_queue_work(hw, &priv->sta_notify_worker);
3756 }
3757}
3758
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003759static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3760 const struct ieee80211_tx_queue_params *params)
3761{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003762 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003763 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003764
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003765 rc = mwl8k_fw_lock(hw);
3766 if (!rc) {
3767 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003768 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003769
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003770 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003771 rc = mwl8k_cmd_set_edca_params(hw, queue,
3772 params->cw_min,
3773 params->cw_max,
3774 params->aifs,
3775 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003776
3777 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003778 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003779
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003780 return rc;
3781}
3782
3783static int mwl8k_get_tx_stats(struct ieee80211_hw *hw,
3784 struct ieee80211_tx_queue_stats *stats)
3785{
3786 struct mwl8k_priv *priv = hw->priv;
3787 struct mwl8k_tx_queue *txq;
3788 int index;
3789
3790 spin_lock_bh(&priv->tx_lock);
3791 for (index = 0; index < MWL8K_TX_QUEUES; index++) {
3792 txq = priv->txq + index;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02003793 memcpy(&stats[index], &txq->stats,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003794 sizeof(struct ieee80211_tx_queue_stats));
3795 }
3796 spin_unlock_bh(&priv->tx_lock);
Lennert Buytenhek954ef502009-07-17 07:26:27 +02003797
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003798 return 0;
3799}
3800
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003801static int mwl8k_get_stats(struct ieee80211_hw *hw,
3802 struct ieee80211_low_level_stats *stats)
3803{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003804 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003805}
3806
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003807static int
3808mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3809 enum ieee80211_ampdu_mlme_action action,
3810 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3811{
3812 switch (action) {
3813 case IEEE80211_AMPDU_RX_START:
3814 case IEEE80211_AMPDU_RX_STOP:
3815 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3816 return -ENOTSUPP;
3817 return 0;
3818 default:
3819 return -ENOTSUPP;
3820 }
3821}
3822
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003823static const struct ieee80211_ops mwl8k_ops = {
3824 .tx = mwl8k_tx,
3825 .start = mwl8k_start,
3826 .stop = mwl8k_stop,
3827 .add_interface = mwl8k_add_interface,
3828 .remove_interface = mwl8k_remove_interface,
3829 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003830 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003831 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003832 .configure_filter = mwl8k_configure_filter,
3833 .set_rts_threshold = mwl8k_set_rts_threshold,
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003834 .sta_notify = mwl8k_sta_notify,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003835 .conf_tx = mwl8k_conf_tx,
3836 .get_tx_stats = mwl8k_get_tx_stats,
3837 .get_stats = mwl8k_get_stats,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003838 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003839};
3840
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003841static void mwl8k_finalize_join_worker(struct work_struct *work)
3842{
3843 struct mwl8k_priv *priv =
3844 container_of(work, struct mwl8k_priv, finalize_join_worker);
3845 struct sk_buff *skb = priv->beacon_skb;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003846 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003847
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003848 mwl8k_vif = mwl8k_first_vif(priv);
3849 if (mwl8k_vif != NULL)
3850 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len,
3851 mwl8k_vif->vif->bss_conf.dtim_period);
3852
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003853 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003854 priv->beacon_skb = NULL;
3855}
3856
John W. Linvillebcb628d2009-11-06 16:40:16 -05003857enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003858 MWL8363 = 0,
3859 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003860 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003861};
3862
John W. Linvillebcb628d2009-11-06 16:40:16 -05003863static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003864 [MWL8363] = {
3865 .part_name = "88w8363",
3866 .helper_image = "mwl8k/helper_8363.fw",
3867 .fw_image = "mwl8k/fmimage_8363.fw",
3868 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003869 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003870 .part_name = "88w8687",
3871 .helper_image = "mwl8k/helper_8687.fw",
3872 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003873 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003874 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003875 .part_name = "88w8366",
3876 .helper_image = "mwl8k/helper_8366.fw",
3877 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003878 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003879 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003880};
3881
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01003882MODULE_FIRMWARE("mwl8k/helper_8363.fw");
3883MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
3884MODULE_FIRMWARE("mwl8k/helper_8687.fw");
3885MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
3886MODULE_FIRMWARE("mwl8k/helper_8366.fw");
3887MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
3888
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003889static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003890 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3891 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003892 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3893 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3894 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01003895 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003896 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003897};
3898MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3899
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003900static int __devinit mwl8k_probe(struct pci_dev *pdev,
3901 const struct pci_device_id *id)
3902{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003903 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003904 struct ieee80211_hw *hw;
3905 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003906 int rc;
3907 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003908
3909 if (!printed_version) {
3910 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3911 printed_version = 1;
3912 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003913
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003914
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003915 rc = pci_enable_device(pdev);
3916 if (rc) {
3917 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3918 MWL8K_NAME);
3919 return rc;
3920 }
3921
3922 rc = pci_request_regions(pdev, MWL8K_NAME);
3923 if (rc) {
3924 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3925 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003926 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003927 }
3928
3929 pci_set_master(pdev);
3930
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003931
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003932 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3933 if (hw == NULL) {
3934 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3935 rc = -ENOMEM;
3936 goto err_free_reg;
3937 }
3938
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003939 SET_IEEE80211_DEV(hw, &pdev->dev);
3940 pci_set_drvdata(pdev, hw);
3941
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003942 priv = hw->priv;
3943 priv->hw = hw;
3944 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003945 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003946
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003947
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003948 priv->sram = pci_iomap(pdev, 0, 0x10000);
3949 if (priv->sram == NULL) {
3950 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003951 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003952 goto err_iounmap;
3953 }
3954
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003955 /*
3956 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3957 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3958 */
3959 priv->regs = pci_iomap(pdev, 1, 0x10000);
3960 if (priv->regs == NULL) {
3961 priv->regs = pci_iomap(pdev, 2, 0x10000);
3962 if (priv->regs == NULL) {
3963 printk(KERN_ERR "%s: Cannot map device registers\n",
3964 wiphy_name(hw->wiphy));
3965 goto err_iounmap;
3966 }
3967 }
3968
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003969
3970 /* Reset firmware and hardware */
3971 mwl8k_hw_reset(priv);
3972
3973 /* Ask userland hotplug daemon for the device firmware */
3974 rc = mwl8k_request_firmware(priv);
3975 if (rc) {
3976 printk(KERN_ERR "%s: Firmware files not found\n",
3977 wiphy_name(hw->wiphy));
3978 goto err_stop_firmware;
3979 }
3980
3981 /* Load firmware into hardware */
3982 rc = mwl8k_load_firmware(hw);
3983 if (rc) {
3984 printk(KERN_ERR "%s: Cannot start firmware\n",
3985 wiphy_name(hw->wiphy));
3986 goto err_stop_firmware;
3987 }
3988
3989 /* Reclaim memory once firmware is successfully loaded */
3990 mwl8k_release_firmware(priv);
3991
3992
Lennert Buytenhek91942232010-01-04 21:53:54 +01003993 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003994 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003995 if (priv->rxd_ops == NULL) {
3996 printk(KERN_ERR "%s: Driver does not have AP "
3997 "firmware image support for this hardware\n",
3998 wiphy_name(hw->wiphy));
3999 goto err_stop_firmware;
4000 }
4001 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004002 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01004003 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004004
4005 priv->sniffer_enabled = false;
4006 priv->wmm_enabled = false;
4007 priv->pending_tx_pkts = 0;
4008
4009
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004010 /*
4011 * Extra headroom is the size of the required DMA header
4012 * minus the size of the smallest 802.11 frame (CTS frame).
4013 */
4014 hw->extra_tx_headroom =
4015 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
4016
4017 hw->channel_change_time = 10;
4018
4019 hw->queues = MWL8K_TX_QUEUES;
4020
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004021 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02004022 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004023 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004024 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004025
4026 INIT_LIST_HEAD(&priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004027
4028 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02004029 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02004030 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004031
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01004032 /* Station database handling */
4033 INIT_WORK(&priv->sta_notify_worker, mwl8k_sta_notify_worker);
4034 spin_lock_init(&priv->sta_notify_list_lock);
4035 INIT_LIST_HEAD(&priv->sta_notify_list);
4036
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004037 /* Finalize join worker */
4038 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4039
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004040 /* TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004041 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4042 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004043 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4044 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004045
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004046 /* Power management cookie */
4047 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4048 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004049 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004050
4051 rc = mwl8k_rxq_init(hw, 0);
4052 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004053 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004054 rxq_refill(hw, 0, INT_MAX);
4055
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004056 mutex_init(&priv->fw_mutex);
4057 priv->fw_mutex_owner = NULL;
4058 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004059 priv->hostcmd_wait = NULL;
4060
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004061 spin_lock_init(&priv->tx_lock);
4062
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004063 priv->tx_wait = NULL;
4064
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004065 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4066 rc = mwl8k_txq_init(hw, i);
4067 if (rc)
4068 goto err_free_queues;
4069 }
4070
4071 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004072 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004073 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004074 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004075 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4076
Joe Perchesa0607fd2009-11-18 23:29:17 -08004077 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004078 IRQF_SHARED, MWL8K_NAME, hw);
4079 if (rc) {
4080 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004081 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004082 goto err_free_queues;
4083 }
4084
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004085 /*
4086 * Temporarily enable interrupts. Initial firmware host
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004087 * commands use interrupts and avoid polling. Disable
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004088 * interrupts when done.
4089 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004090 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004091
4092 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02004093 if (priv->ap_fw) {
4094 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4095 if (!rc)
4096 rc = mwl8k_cmd_set_hw_spec(hw);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01004097
4098 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_AP);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02004099 } else {
4100 rc = mwl8k_cmd_get_hw_spec_sta(hw);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01004101
4102 hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02004103 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004104 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004105 printk(KERN_ERR "%s: Cannot initialise firmware\n",
4106 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004107 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004108 }
4109
4110 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004111 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004112 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004113 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004114 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004115 }
4116
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004117 /* Clear MAC address */
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004118 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004119 if (rc) {
4120 printk(KERN_ERR "%s: Cannot clear MAC address\n",
4121 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004122 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004123 }
4124
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004125 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004126 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004127 free_irq(priv->pdev->irq, hw);
4128
4129 rc = ieee80211_register_hw(hw);
4130 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004131 printk(KERN_ERR "%s: Cannot register device\n",
4132 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01004133 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004134 }
4135
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02004136 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02004137 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004138 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02004139 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004140 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4141 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004142
4143 return 0;
4144
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004145err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004146 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004147 free_irq(priv->pdev->irq, hw);
4148
4149err_free_queues:
4150 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4151 mwl8k_txq_deinit(hw, i);
4152 mwl8k_rxq_deinit(hw, 0);
4153
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004154err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004155 if (priv->cookie != NULL)
4156 pci_free_consistent(priv->pdev, 4,
4157 priv->cookie, priv->cookie_dma);
4158
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004159err_stop_firmware:
4160 mwl8k_hw_reset(priv);
4161 mwl8k_release_firmware(priv);
4162
4163err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004164 if (priv->regs != NULL)
4165 pci_iounmap(pdev, priv->regs);
4166
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004167 if (priv->sram != NULL)
4168 pci_iounmap(pdev, priv->sram);
4169
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004170 pci_set_drvdata(pdev, NULL);
4171 ieee80211_free_hw(hw);
4172
4173err_free_reg:
4174 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004175
4176err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004177 pci_disable_device(pdev);
4178
4179 return rc;
4180}
4181
Joerg Albert230f7af2009-04-18 02:10:45 +02004182static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004183{
4184 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4185}
4186
Joerg Albert230f7af2009-04-18 02:10:45 +02004187static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004188{
4189 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4190 struct mwl8k_priv *priv;
4191 int i;
4192
4193 if (hw == NULL)
4194 return;
4195 priv = hw->priv;
4196
4197 ieee80211_stop_queues(hw);
4198
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004199 ieee80211_unregister_hw(hw);
4200
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004201 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004202 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004203 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004204
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004205 /* Stop hardware */
4206 mwl8k_hw_reset(priv);
4207
4208 /* Return all skbs to mac80211 */
4209 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004210 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004211
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004212 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4213 mwl8k_txq_deinit(hw, i);
4214
4215 mwl8k_rxq_deinit(hw, 0);
4216
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004217 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004218
4219 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004220 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004221 pci_set_drvdata(pdev, NULL);
4222 ieee80211_free_hw(hw);
4223 pci_release_regions(pdev);
4224 pci_disable_device(pdev);
4225}
4226
4227static struct pci_driver mwl8k_driver = {
4228 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004229 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004230 .probe = mwl8k_probe,
4231 .remove = __devexit_p(mwl8k_remove),
4232 .shutdown = __devexit_p(mwl8k_shutdown),
4233};
4234
4235static int __init mwl8k_init(void)
4236{
4237 return pci_register_driver(&mwl8k_driver);
4238}
4239
4240static void __exit mwl8k_exit(void)
4241{
4242 pci_unregister_driver(&mwl8k_driver);
4243}
4244
4245module_init(mwl8k_init);
4246module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004247
4248MODULE_DESCRIPTION(MWL8K_DESC);
4249MODULE_VERSION(MWL8K_VERSION);
4250MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
4251MODULE_LICENSE("GPL");