blob: 89354c29f088c49fb9a06d5ed74eb4c371624520 [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka5fb2972010-01-12 13:51:38 +01005 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020015#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010016#include <linux/spinlock.h>
17#include <linux/list.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
20#include <linux/completion.h>
21#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010023#include <net/mac80211.h>
24#include <linux/moduleparam.h>
25#include <linux/firmware.h>
26#include <linux/workqueue.h>
27
28#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
29#define MWL8K_NAME KBUILD_MODNAME
Lennert Buytenheka5fb2972010-01-12 13:51:38 +010030#define MWL8K_VERSION "0.12"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031
Lennert Buytenheka66098d2009-03-10 10:13:33 +010032/* Register definitions */
33#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020034#define MWL8K_MODE_STA 0x0000005a
35#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010036#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020037#define MWL8K_FWSTA_READY 0xf0f1f2f4
38#define MWL8K_FWAP_READY 0xf1f2f4a5
39#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010040#define MWL8K_HIU_SCRATCH 0x00000c40
41
42/* Host->device communications */
43#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
44#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
45#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
46#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
47#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020048#define MWL8K_H2A_INT_DUMMY (1 << 20)
49#define MWL8K_H2A_INT_RESET (1 << 15)
50#define MWL8K_H2A_INT_DOORBELL (1 << 1)
51#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010052
53/* Device->host communications */
54#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
55#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
56#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
57#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
58#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020059#define MWL8K_A2H_INT_DUMMY (1 << 20)
60#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
61#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
62#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
63#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
64#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
65#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
66#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
67#define MWL8K_A2H_INT_RX_READY (1 << 1)
68#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010069
70#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
71 MWL8K_A2H_INT_CHNL_SWITCHED | \
72 MWL8K_A2H_INT_QUEUE_EMPTY | \
73 MWL8K_A2H_INT_RADAR_DETECT | \
74 MWL8K_A2H_INT_RADIO_ON | \
75 MWL8K_A2H_INT_RADIO_OFF | \
76 MWL8K_A2H_INT_MAC_EVENT | \
77 MWL8K_A2H_INT_OPC_DONE | \
78 MWL8K_A2H_INT_RX_READY | \
79 MWL8K_A2H_INT_TX_DONE)
80
Lennert Buytenheka66098d2009-03-10 10:13:33 +010081#define MWL8K_RX_QUEUES 1
82#define MWL8K_TX_QUEUES 4
83
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020084struct rxd_ops {
85 int rxd_size;
86 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
87 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010088 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
89 __le16 *qos);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020090};
91
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020092struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020093 char *part_name;
94 char *helper_image;
95 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010096 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020097};
98
Lennert Buytenheka66098d2009-03-10 10:13:33 +010099struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200100 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100101
102 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200103 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100104
105 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200106 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100107
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200108 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200109 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200110 struct {
111 struct sk_buff *skb;
112 DECLARE_PCI_UNMAP_ADDR(dma)
113 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100114};
115
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100116struct mwl8k_tx_queue {
117 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200118 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100119
120 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200121 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100122
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200123 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200124 struct mwl8k_tx_desc *txd;
125 dma_addr_t txd_dma;
126 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100127};
128
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100132
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200133 struct mwl8k_device_info *device_info;
134
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100135 void __iomem *sram;
136 void __iomem *regs;
137
138 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100139 struct firmware *fw_helper;
140 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100141
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100142 /* hardware/firmware parameters */
143 bool ap_fw;
144 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100145 struct ieee80211_supported_band band_24;
146 struct ieee80211_channel channels_24[14];
147 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100148 struct ieee80211_supported_band band_50;
149 struct ieee80211_channel channels_50[4];
150 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100151 u32 ap_macids_supported;
152 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100153
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200154 /* firmware access */
155 struct mutex fw_mutex;
156 struct task_struct *fw_mutex_owner;
157 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200158 struct completion *hostcmd_wait;
159
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100160 /* lock held over TX and TX reap */
161 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100162
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200163 /* TX quiesce completion, protected by fw_mutex and tx_lock */
164 struct completion *tx_wait;
165
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100166 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100167 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100168 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100169
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100170 /* power management status cookie from firmware */
171 u32 *cookie;
172 dma_addr_t cookie_dma;
173
174 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100175 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200176 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177
178 /*
179 * Running count of TX packets in flight, to avoid
180 * iterating over the transmit rings each time.
181 */
182 int pending_tx_pkts;
183
184 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
185 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
186
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200187 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200188 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200189 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200190 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100191
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100192 /* XXX need to convert this to handle multiple interfaces */
193 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200194 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 struct sk_buff *beacon_skb;
196
197 /*
198 * This FJ worker has to be global as it is scheduled from the
199 * RX handler. At this point we don't know which interface it
200 * belongs to until the list of bssids waiting to complete join
201 * is checked.
202 */
203 struct work_struct finalize_join_worker;
204
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100205 /* Tasklet to perform TX reclaim. */
206 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100207
208 /* Tasklet to perform RX. */
209 struct tasklet_struct poll_rx_task;
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 Buytenhek42fba21d2009-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 Buytenhekaa21d0f62010-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 Buytenhekaa21d0f62010-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 Buytenhekaa21d0f62010-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 Buytenhek42fba21d2009-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
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001136 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001137 txq->head = 0;
1138 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001139
1140 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1141
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001142 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1143 if (txq->txd == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001144 printk(KERN_ERR "%s: failed to alloc TX descriptors\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001145 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001146 return -ENOMEM;
1147 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001148 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001149
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001150 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1151 if (txq->skb == NULL) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001152 printk(KERN_ERR "%s: failed to alloc TX skbuff list\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001153 wiphy_name(hw->wiphy));
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001154 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001155 return -ENOMEM;
1156 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001157 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001158
1159 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1160 struct mwl8k_tx_desc *tx_desc;
1161 int nexti;
1162
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001163 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001164 nexti = (i + 1) % MWL8K_TX_DESCS;
1165
1166 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001167 tx_desc->next_txd_phys_addr =
1168 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001169 }
1170
1171 return 0;
1172}
1173
1174static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1175{
1176 iowrite32(MWL8K_H2A_INT_PPA_READY,
1177 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1178 iowrite32(MWL8K_H2A_INT_DUMMY,
1179 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1180 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1181}
1182
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001183static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001184{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001185 struct mwl8k_priv *priv = hw->priv;
1186 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001187
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001188 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1189 struct mwl8k_tx_queue *txq = priv->txq + i;
1190 int fw_owned = 0;
1191 int drv_owned = 0;
1192 int unused = 0;
1193 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001194
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001195 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001196 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1197 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001198
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001199 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001200 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001201 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001202 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001203 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001204
1205 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001206 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001207 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001208
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001209 printk(KERN_ERR "%s: txq[%d] len=%d head=%d tail=%d "
1210 "fw_owned=%d drv_owned=%d unused=%d\n",
1211 wiphy_name(hw->wiphy), i,
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001212 txq->len, txq->head, txq->tail,
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001213 fw_owned, drv_owned, unused);
1214 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001215}
1216
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001217/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001218 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001219 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001220#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001221
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001222static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001223{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001224 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001225 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001226 int retry;
1227 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001228
1229 might_sleep();
1230
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001231 /*
1232 * The TX queues are stopped at this point, so this test
1233 * doesn't need to take ->tx_lock.
1234 */
1235 if (!priv->pending_tx_pkts)
1236 return 0;
1237
1238 retry = 0;
1239 rc = 0;
1240
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001241 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001242 priv->tx_wait = &tx_wait;
1243 while (!rc) {
1244 int oldcount;
1245 unsigned long timeout;
1246
1247 oldcount = priv->pending_tx_pkts;
1248
1249 spin_unlock_bh(&priv->tx_lock);
1250 timeout = wait_for_completion_timeout(&tx_wait,
1251 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1252 spin_lock_bh(&priv->tx_lock);
1253
1254 if (timeout) {
1255 WARN_ON(priv->pending_tx_pkts);
1256 if (retry) {
1257 printk(KERN_NOTICE "%s: tx rings drained\n",
1258 wiphy_name(hw->wiphy));
1259 }
1260 break;
1261 }
1262
1263 if (priv->pending_tx_pkts < oldcount) {
Lennert Buytenhek9a2303b2010-01-04 21:54:25 +01001264 printk(KERN_NOTICE "%s: waiting for tx rings "
1265 "to drain (%d -> %d pkts)\n",
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001266 wiphy_name(hw->wiphy), oldcount,
1267 priv->pending_tx_pkts);
1268 retry = 1;
1269 continue;
1270 }
1271
1272 priv->tx_wait = NULL;
1273
1274 printk(KERN_ERR "%s: tx rings stuck for %d ms\n",
1275 wiphy_name(hw->wiphy), MWL8K_TX_WAIT_TIMEOUT_MS);
1276 mwl8k_dump_tx_rings(hw);
1277
1278 rc = -ETIMEDOUT;
1279 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001280 spin_unlock_bh(&priv->tx_lock);
1281
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001282 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001283}
1284
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001285#define MWL8K_TXD_SUCCESS(status) \
1286 ((status) & (MWL8K_TXD_STATUS_OK | \
1287 MWL8K_TXD_STATUS_OK_RETRY | \
1288 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001289
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001290static int
1291mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001292{
1293 struct mwl8k_priv *priv = hw->priv;
1294 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001295 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001296
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001297 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001298 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001300 struct mwl8k_tx_desc *tx_desc;
1301 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001302 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303 struct sk_buff *skb;
1304 struct ieee80211_tx_info *info;
1305 u32 status;
1306
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001307 tx = txq->head;
1308 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001309
1310 status = le32_to_cpu(tx_desc->status);
1311
1312 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1313 if (!force)
1314 break;
1315 tx_desc->status &=
1316 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1317 }
1318
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001319 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001320 BUG_ON(txq->len == 0);
1321 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001322 priv->pending_tx_pkts--;
1323
1324 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001325 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001326 skb = txq->skb[tx];
1327 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001328
1329 BUG_ON(skb == NULL);
1330 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1331
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001332 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001333
1334 /* Mark descriptor as unused */
1335 tx_desc->pkt_phys_addr = 0;
1336 tx_desc->pkt_len = 0;
1337
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001338 info = IEEE80211_SKB_CB(skb);
1339 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001340 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001342
1343 ieee80211_tx_status_irqsafe(hw, skb);
1344
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001345 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001346 }
1347
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001348 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001349 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001350
1351 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001352}
1353
1354/* must be called only when the card's transmit is completely halted */
1355static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1356{
1357 struct mwl8k_priv *priv = hw->priv;
1358 struct mwl8k_tx_queue *txq = priv->txq + index;
1359
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001360 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001361
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001362 kfree(txq->skb);
1363 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001364
1365 pci_free_consistent(priv->pdev,
1366 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001367 txq->txd, txq->txd_dma);
1368 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001369}
1370
1371static int
1372mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1373{
1374 struct mwl8k_priv *priv = hw->priv;
1375 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001376 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001377 struct ieee80211_hdr *wh;
1378 struct mwl8k_tx_queue *txq;
1379 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001380 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001381 u32 txstatus;
1382 u8 txdatarate;
1383 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001384
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001385 wh = (struct ieee80211_hdr *)skb->data;
1386 if (ieee80211_is_data_qos(wh->frame_control))
1387 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1388 else
1389 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001390
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001391 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001392 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001393
1394 tx_info = IEEE80211_SKB_CB(skb);
1395 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001396
1397 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001398 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001399 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1400 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001401 }
1402
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001403 /* Setup firmware control bit fields for each frame type. */
1404 txstatus = 0;
1405 txdatarate = 0;
1406 if (ieee80211_is_mgmt(wh->frame_control) ||
1407 ieee80211_is_ctl(wh->frame_control)) {
1408 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001409 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001410 } else if (ieee80211_is_data(wh->frame_control)) {
1411 txdatarate = 1;
1412 if (is_multicast_ether_addr(wh->addr1))
1413 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1414
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001415 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001416 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001417 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001418 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001419 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001420 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001421
1422 dma = pci_map_single(priv->pdev, skb->data,
1423 skb->len, PCI_DMA_TODEVICE);
1424
1425 if (pci_dma_mapping_error(priv->pdev, dma)) {
1426 printk(KERN_DEBUG "%s: failed to dma map skb, "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001427 "dropping TX frame.\n", wiphy_name(hw->wiphy));
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001428 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001429 return NETDEV_TX_OK;
1430 }
1431
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001432 spin_lock_bh(&priv->tx_lock);
1433
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001434 txq = priv->txq + index;
1435
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001436 BUG_ON(txq->skb[txq->tail] != NULL);
1437 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001438
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001439 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001440 tx->data_rate = txdatarate;
1441 tx->tx_priority = index;
1442 tx->qos_control = cpu_to_le16(qos);
1443 tx->pkt_phys_addr = cpu_to_le32(dma);
1444 tx->pkt_len = cpu_to_le16(skb->len);
1445 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001446 if (!priv->ap_fw && tx_info->control.sta != NULL)
1447 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1448 else
1449 tx->peer_id = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001450 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001451 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1452
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001453 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001454 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001455
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001456 txq->tail++;
1457 if (txq->tail == MWL8K_TX_DESCS)
1458 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001459
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001460 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001461 ieee80211_stop_queue(hw, index);
1462
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001463 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001464
1465 spin_unlock_bh(&priv->tx_lock);
1466
1467 return NETDEV_TX_OK;
1468}
1469
1470
1471/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001472 * Firmware access.
1473 *
1474 * We have the following requirements for issuing firmware commands:
1475 * - Some commands require that the packet transmit path is idle when
1476 * the command is issued. (For simplicity, we'll just quiesce the
1477 * transmit path for every command.)
1478 * - There are certain sequences of commands that need to be issued to
1479 * the hardware sequentially, with no other intervening commands.
1480 *
1481 * This leads to an implementation of a "firmware lock" as a mutex that
1482 * can be taken recursively, and which is taken by both the low-level
1483 * command submission function (mwl8k_post_cmd) as well as any users of
1484 * that function that require issuing of an atomic sequence of commands,
1485 * and quiesces the transmit path whenever it's taken.
1486 */
1487static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1488{
1489 struct mwl8k_priv *priv = hw->priv;
1490
1491 if (priv->fw_mutex_owner != current) {
1492 int rc;
1493
1494 mutex_lock(&priv->fw_mutex);
1495 ieee80211_stop_queues(hw);
1496
1497 rc = mwl8k_tx_wait_empty(hw);
1498 if (rc) {
1499 ieee80211_wake_queues(hw);
1500 mutex_unlock(&priv->fw_mutex);
1501
1502 return rc;
1503 }
1504
1505 priv->fw_mutex_owner = current;
1506 }
1507
1508 priv->fw_mutex_depth++;
1509
1510 return 0;
1511}
1512
1513static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1514{
1515 struct mwl8k_priv *priv = hw->priv;
1516
1517 if (!--priv->fw_mutex_depth) {
1518 ieee80211_wake_queues(hw);
1519 priv->fw_mutex_owner = NULL;
1520 mutex_unlock(&priv->fw_mutex);
1521 }
1522}
1523
1524
1525/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001526 * Command processing.
1527 */
1528
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001529/* Timeout firmware commands after 10s */
1530#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001531
1532static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1533{
1534 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1535 struct mwl8k_priv *priv = hw->priv;
1536 void __iomem *regs = priv->regs;
1537 dma_addr_t dma_addr;
1538 unsigned int dma_size;
1539 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001540 unsigned long timeout = 0;
1541 u8 buf[32];
1542
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001543 cmd->result = 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001544 dma_size = le16_to_cpu(cmd->length);
1545 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1546 PCI_DMA_BIDIRECTIONAL);
1547 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1548 return -ENOMEM;
1549
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001550 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001551 if (rc) {
1552 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1553 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001554 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001555 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001556
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001557 priv->hostcmd_wait = &cmd_wait;
1558 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1559 iowrite32(MWL8K_H2A_INT_DOORBELL,
1560 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1561 iowrite32(MWL8K_H2A_INT_DUMMY,
1562 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001563
1564 timeout = wait_for_completion_timeout(&cmd_wait,
1565 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1566
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001567 priv->hostcmd_wait = NULL;
1568
1569 mwl8k_fw_unlock(hw);
1570
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001571 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1572 PCI_DMA_BIDIRECTIONAL);
1573
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001574 if (!timeout) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001575 printk(KERN_ERR "%s: Command %s timeout after %u ms\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001576 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001577 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1578 MWL8K_CMD_TIMEOUT_MS);
1579 rc = -ETIMEDOUT;
1580 } else {
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001581 int ms;
1582
1583 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1584
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001585 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001586 if (rc)
1587 printk(KERN_ERR "%s: Command %s error 0x%x\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001588 wiphy_name(hw->wiphy),
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001589 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
Lennert Buytenhek76c962a2009-08-24 15:42:56 +02001590 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc6402009-11-30 18:12:49 +01001591 else if (ms > 2000)
1592 printk(KERN_NOTICE "%s: Command %s took %d ms\n",
1593 wiphy_name(hw->wiphy),
1594 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1595 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001596 }
1597
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001598 return rc;
1599}
1600
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001601static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1602 struct ieee80211_vif *vif,
1603 struct mwl8k_cmd_pkt *cmd)
1604{
1605 if (vif != NULL)
1606 cmd->macid = MWL8K_VIF(vif)->macid;
1607 return mwl8k_post_cmd(hw, cmd);
1608}
1609
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001610/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001611 * Setup code shared between STA and AP firmware images.
1612 */
1613static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1614{
1615 struct mwl8k_priv *priv = hw->priv;
1616
1617 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1618 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1619
1620 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1621 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1622
1623 priv->band_24.band = IEEE80211_BAND_2GHZ;
1624 priv->band_24.channels = priv->channels_24;
1625 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1626 priv->band_24.bitrates = priv->rates_24;
1627 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1628
1629 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1630}
1631
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001632static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1633{
1634 struct mwl8k_priv *priv = hw->priv;
1635
1636 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1637 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1638
1639 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1640 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1641
1642 priv->band_50.band = IEEE80211_BAND_5GHZ;
1643 priv->band_50.channels = priv->channels_50;
1644 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1645 priv->band_50.bitrates = priv->rates_50;
1646 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1647
1648 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1649}
1650
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001651/*
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001652 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001653 */
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001654struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001655 struct mwl8k_cmd_pkt header;
1656 __u8 hw_rev;
1657 __u8 host_interface;
1658 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001659 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001660 __le16 region_code;
1661 __le32 fw_rev;
1662 __le32 ps_cookie;
1663 __le32 caps;
1664 __u8 mcs_bitmap[16];
1665 __le32 rx_queue_ptr;
1666 __le32 num_tx_queues;
1667 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1668 __le32 caps2;
1669 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001670 __le32 total_rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001671} __attribute__((packed));
1672
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001673#define MWL8K_CAP_MAX_AMSDU 0x20000000
1674#define MWL8K_CAP_GREENFIELD 0x08000000
1675#define MWL8K_CAP_AMPDU 0x04000000
1676#define MWL8K_CAP_RX_STBC 0x01000000
1677#define MWL8K_CAP_TX_STBC 0x00800000
1678#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1679#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1680#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1681#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1682#define MWL8K_CAP_DELAY_BA 0x00003000
1683#define MWL8K_CAP_MIMO 0x00000200
1684#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001685#define MWL8K_CAP_BAND_MASK 0x00000007
1686#define MWL8K_CAP_5GHZ 0x00000004
1687#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001688
Lennert Buytenhek06953232010-01-12 13:49:41 +01001689static void
1690mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1691 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001692{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001693 int rx_streams;
1694 int tx_streams;
1695
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001696 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001697
1698 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001699 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001700 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001701 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001702 if (cap & MWL8K_CAP_AMPDU) {
1703 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001704 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1705 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001706 }
1707 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001708 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001709 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001710 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001711 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001712 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001713 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001714 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001715 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001716 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001717 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001718 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001719
1720 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1721 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1722
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001723 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001724 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001725 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001726 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001727 band->ht_cap.mcs.rx_mask[2] = 0xff;
1728 band->ht_cap.mcs.rx_mask[4] = 0x01;
1729 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001730
1731 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001732 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1733 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001734 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1735 }
1736}
1737
Lennert Buytenhek06953232010-01-12 13:49:41 +01001738static void
1739mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1740{
1741 struct mwl8k_priv *priv = hw->priv;
1742
1743 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1744 mwl8k_setup_2ghz_band(hw);
1745 if (caps & MWL8K_CAP_MIMO)
1746 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1747 }
1748
1749 if (caps & MWL8K_CAP_5GHZ) {
1750 mwl8k_setup_5ghz_band(hw);
1751 if (caps & MWL8K_CAP_MIMO)
1752 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1753 }
1754}
1755
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001756static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001757{
1758 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b12009-10-22 20:21:05 +02001759 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001760 int rc;
1761 int i;
1762
1763 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1764 if (cmd == NULL)
1765 return -ENOMEM;
1766
1767 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1768 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1769
1770 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1771 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001772 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001773 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001774 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001775 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001776 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001777 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001778
1779 rc = mwl8k_post_cmd(hw, &cmd->header);
1780
1781 if (!rc) {
1782 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1783 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001784 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001785 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001786 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001787 priv->ap_macids_supported = 0x00000000;
1788 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001789 }
1790
1791 kfree(cmd);
1792 return rc;
1793}
1794
1795/*
Lennert Buytenhek42fba21d2009-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 Buytenhekee0ddf12010-01-12 13:51:30 +01001843 priv->ap_macids_supported = 0x000000ff;
1844 priv->sta_macids_supported = 0x00000000;
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001845
1846 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
1847 iowrite32(cpu_to_le32(priv->txq[0].txd_dma), priv->sram + off);
1848
1849 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
1850 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1851
1852 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
1853 iowrite32(cpu_to_le32(priv->rxq[0].rxd_dma), priv->sram + off);
1854
1855 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
1856 iowrite32(cpu_to_le32(priv->txq[1].txd_dma), priv->sram + off);
1857
1858 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
1859 iowrite32(cpu_to_le32(priv->txq[2].txd_dma), priv->sram + off);
1860
1861 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
1862 iowrite32(cpu_to_le32(priv->txq[3].txd_dma), priv->sram + off);
1863 }
1864
1865 kfree(cmd);
1866 return rc;
1867}
1868
1869/*
1870 * CMD_SET_HW_SPEC.
1871 */
1872struct mwl8k_cmd_set_hw_spec {
1873 struct mwl8k_cmd_pkt header;
1874 __u8 hw_rev;
1875 __u8 host_interface;
1876 __le16 num_mcaddrs;
1877 __u8 perm_addr[ETH_ALEN];
1878 __le16 region_code;
1879 __le32 fw_rev;
1880 __le32 ps_cookie;
1881 __le32 caps;
1882 __le32 rx_queue_ptr;
1883 __le32 num_tx_queues;
1884 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1885 __le32 flags;
1886 __le32 num_tx_desc_per_queue;
1887 __le32 total_rxd;
1888} __attribute__((packed));
1889
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001890#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1891#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
1892#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001893
1894static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1895{
1896 struct mwl8k_priv *priv = hw->priv;
1897 struct mwl8k_cmd_set_hw_spec *cmd;
1898 int rc;
1899 int i;
1900
1901 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1902 if (cmd == NULL)
1903 return -ENOMEM;
1904
1905 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1906 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1907
1908 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1909 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1910 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1911 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1912 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001913 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
1914 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
1915 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02001916 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1917 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1918
1919 rc = mwl8k_post_cmd(hw, &cmd->header);
1920 kfree(cmd);
1921
1922 return rc;
1923}
1924
1925/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001926 * CMD_MAC_MULTICAST_ADR.
1927 */
1928struct mwl8k_cmd_mac_multicast_adr {
1929 struct mwl8k_cmd_pkt header;
1930 __le16 action;
1931 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001932 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001933};
1934
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001935#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1936#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1937#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1938#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001939
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001940static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001941__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001942 int mc_count, struct dev_addr_list *mclist)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001943{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001944 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001945 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001946 int size;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001947
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001948 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001949 allmulti = 1;
1950 mc_count = 0;
1951 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001952
1953 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1954
1955 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001956 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001957 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001958
1959 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1960 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001961 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1962 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001963
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001964 if (allmulti) {
1965 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1966 } else if (mc_count) {
1967 int i;
1968
1969 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1970 cmd->numaddr = cpu_to_le16(mc_count);
1971 for (i = 0; i < mc_count && mclist; i++) {
1972 if (mclist->da_addrlen != ETH_ALEN) {
1973 kfree(cmd);
1974 return NULL;
1975 }
1976 memcpy(cmd->addr[i], mclist->da_addr, ETH_ALEN);
1977 mclist = mclist->next;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001978 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001979 }
1980
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001981 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001982}
1983
1984/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001985 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001986 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001987struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001988 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001989 __le32 stats[64];
1990} __attribute__((packed));
1991
1992#define MWL8K_STAT_ACK_FAILURE 9
1993#define MWL8K_STAT_RTS_FAILURE 12
1994#define MWL8K_STAT_FCS_ERROR 24
1995#define MWL8K_STAT_RTS_SUCCESS 11
1996
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001997static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
1998 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001999{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002000 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002001 int rc;
2002
2003 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2004 if (cmd == NULL)
2005 return -ENOMEM;
2006
2007 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2008 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002009
2010 rc = mwl8k_post_cmd(hw, &cmd->header);
2011 if (!rc) {
2012 stats->dot11ACKFailureCount =
2013 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2014 stats->dot11RTSFailureCount =
2015 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2016 stats->dot11FCSErrorCount =
2017 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2018 stats->dot11RTSSuccessCount =
2019 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2020 }
2021 kfree(cmd);
2022
2023 return rc;
2024}
2025
2026/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002027 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002028 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002029struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002030 struct mwl8k_cmd_pkt header;
2031 __le16 action;
2032 __le16 control;
2033 __le16 radio_on;
2034} __attribute__((packed));
2035
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002036static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002037mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002038{
2039 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002040 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002041 int rc;
2042
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002043 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002044 return 0;
2045
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002046 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2047 if (cmd == NULL)
2048 return -ENOMEM;
2049
2050 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2051 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2052 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002053 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002054 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2055
2056 rc = mwl8k_post_cmd(hw, &cmd->header);
2057 kfree(cmd);
2058
2059 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002060 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002061
2062 return rc;
2063}
2064
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002065static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002066{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002067 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002068}
2069
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002070static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002071{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002072 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002073}
2074
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002075static int
2076mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2077{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002078 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002079
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002080 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002081
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002082 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002083}
2084
2085/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002086 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002087 */
2088#define MWL8K_TX_POWER_LEVEL_TOTAL 8
2089
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002090struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002091 struct mwl8k_cmd_pkt header;
2092 __le16 action;
2093 __le16 support_level;
2094 __le16 current_level;
2095 __le16 reserved;
2096 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2097} __attribute__((packed));
2098
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002099static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002100{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002101 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002102 int rc;
2103
2104 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2105 if (cmd == NULL)
2106 return -ENOMEM;
2107
2108 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2109 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2110 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2111 cmd->support_level = cpu_to_le16(dBm);
2112
2113 rc = mwl8k_post_cmd(hw, &cmd->header);
2114 kfree(cmd);
2115
2116 return rc;
2117}
2118
2119/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002120 * CMD_RF_ANTENNA.
2121 */
2122struct mwl8k_cmd_rf_antenna {
2123 struct mwl8k_cmd_pkt header;
2124 __le16 antenna;
2125 __le16 mode;
2126} __attribute__((packed));
2127
2128#define MWL8K_RF_ANTENNA_RX 1
2129#define MWL8K_RF_ANTENNA_TX 2
2130
2131static int
2132mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2133{
2134 struct mwl8k_cmd_rf_antenna *cmd;
2135 int rc;
2136
2137 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2138 if (cmd == NULL)
2139 return -ENOMEM;
2140
2141 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2142 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2143 cmd->antenna = cpu_to_le16(antenna);
2144 cmd->mode = cpu_to_le16(mask);
2145
2146 rc = mwl8k_post_cmd(hw, &cmd->header);
2147 kfree(cmd);
2148
2149 return rc;
2150}
2151
2152/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002153 * CMD_SET_BEACON.
2154 */
2155struct mwl8k_cmd_set_beacon {
2156 struct mwl8k_cmd_pkt header;
2157 __le16 beacon_len;
2158 __u8 beacon[0];
2159};
2160
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002161static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2162 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002163{
2164 struct mwl8k_cmd_set_beacon *cmd;
2165 int rc;
2166
2167 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2168 if (cmd == NULL)
2169 return -ENOMEM;
2170
2171 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2172 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2173 cmd->beacon_len = cpu_to_le16(len);
2174 memcpy(cmd->beacon, beacon, len);
2175
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002176 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002177 kfree(cmd);
2178
2179 return rc;
2180}
2181
2182/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002183 * CMD_SET_PRE_SCAN.
2184 */
2185struct mwl8k_cmd_set_pre_scan {
2186 struct mwl8k_cmd_pkt header;
2187} __attribute__((packed));
2188
2189static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2190{
2191 struct mwl8k_cmd_set_pre_scan *cmd;
2192 int rc;
2193
2194 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2195 if (cmd == NULL)
2196 return -ENOMEM;
2197
2198 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2199 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2200
2201 rc = mwl8k_post_cmd(hw, &cmd->header);
2202 kfree(cmd);
2203
2204 return rc;
2205}
2206
2207/*
2208 * CMD_SET_POST_SCAN.
2209 */
2210struct mwl8k_cmd_set_post_scan {
2211 struct mwl8k_cmd_pkt header;
2212 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002213 __u8 bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002214} __attribute__((packed));
2215
2216static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002217mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002218{
2219 struct mwl8k_cmd_set_post_scan *cmd;
2220 int rc;
2221
2222 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2223 if (cmd == NULL)
2224 return -ENOMEM;
2225
2226 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2227 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2228 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002229 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002230
2231 rc = mwl8k_post_cmd(hw, &cmd->header);
2232 kfree(cmd);
2233
2234 return rc;
2235}
2236
2237/*
2238 * CMD_SET_RF_CHANNEL.
2239 */
2240struct mwl8k_cmd_set_rf_channel {
2241 struct mwl8k_cmd_pkt header;
2242 __le16 action;
2243 __u8 current_channel;
2244 __le32 channel_flags;
2245} __attribute__((packed));
2246
2247static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002248 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002249{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002250 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002251 struct mwl8k_cmd_set_rf_channel *cmd;
2252 int rc;
2253
2254 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2255 if (cmd == NULL)
2256 return -ENOMEM;
2257
2258 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2259 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2260 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2261 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002262
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002263 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002264 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002265 else if (channel->band == IEEE80211_BAND_5GHZ)
2266 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002267
2268 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2269 conf->channel_type == NL80211_CHAN_HT20)
2270 cmd->channel_flags |= cpu_to_le32(0x00000080);
2271 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2272 cmd->channel_flags |= cpu_to_le32(0x000001900);
2273 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2274 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002275
2276 rc = mwl8k_post_cmd(hw, &cmd->header);
2277 kfree(cmd);
2278
2279 return rc;
2280}
2281
2282/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002283 * CMD_SET_AID.
2284 */
2285#define MWL8K_FRAME_PROT_DISABLED 0x00
2286#define MWL8K_FRAME_PROT_11G 0x07
2287#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2288#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2289
2290struct mwl8k_cmd_update_set_aid {
2291 struct mwl8k_cmd_pkt header;
2292 __le16 aid;
2293
2294 /* AP's MAC address (BSSID) */
2295 __u8 bssid[ETH_ALEN];
2296 __le16 protection_mode;
2297 __u8 supp_rates[14];
2298} __attribute__((packed));
2299
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002300static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2301{
2302 int i;
2303 int j;
2304
2305 /*
2306 * Clear nonstandard rates 4 and 13.
2307 */
2308 mask &= 0x1fef;
2309
2310 for (i = 0, j = 0; i < 14; i++) {
2311 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002312 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002313 }
2314}
2315
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002316static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002317mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2318 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002319{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002320 struct mwl8k_cmd_update_set_aid *cmd;
2321 u16 prot_mode;
2322 int rc;
2323
2324 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2325 if (cmd == NULL)
2326 return -ENOMEM;
2327
2328 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2329 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002330 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002331 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002332
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002333 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002334 prot_mode = MWL8K_FRAME_PROT_11G;
2335 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002336 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002337 IEEE80211_HT_OP_MODE_PROTECTION) {
2338 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2339 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2340 break;
2341 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2342 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2343 break;
2344 default:
2345 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2346 break;
2347 }
2348 }
2349 cmd->protection_mode = cpu_to_le16(prot_mode);
2350
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002351 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002352
2353 rc = mwl8k_post_cmd(hw, &cmd->header);
2354 kfree(cmd);
2355
2356 return rc;
2357}
2358
2359/*
2360 * CMD_SET_RATE.
2361 */
2362struct mwl8k_cmd_set_rate {
2363 struct mwl8k_cmd_pkt header;
2364 __u8 legacy_rates[14];
2365
2366 /* Bitmap for supported MCS codes. */
2367 __u8 mcs_set[16];
2368 __u8 reserved[16];
2369} __attribute__((packed));
2370
2371static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002372mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002373 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002374{
2375 struct mwl8k_cmd_set_rate *cmd;
2376 int rc;
2377
2378 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2379 if (cmd == NULL)
2380 return -ENOMEM;
2381
2382 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2383 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002384 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002385 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002386
2387 rc = mwl8k_post_cmd(hw, &cmd->header);
2388 kfree(cmd);
2389
2390 return rc;
2391}
2392
2393/*
2394 * CMD_FINALIZE_JOIN.
2395 */
2396#define MWL8K_FJ_BEACON_MAXLEN 128
2397
2398struct mwl8k_cmd_finalize_join {
2399 struct mwl8k_cmd_pkt header;
2400 __le32 sleep_interval; /* Number of beacon periods to sleep */
2401 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
2402} __attribute__((packed));
2403
2404static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2405 int framelen, int dtim)
2406{
2407 struct mwl8k_cmd_finalize_join *cmd;
2408 struct ieee80211_mgmt *payload = frame;
2409 int payload_len;
2410 int rc;
2411
2412 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2413 if (cmd == NULL)
2414 return -ENOMEM;
2415
2416 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2417 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2418 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2419
2420 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2421 if (payload_len < 0)
2422 payload_len = 0;
2423 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2424 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2425
2426 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2427
2428 rc = mwl8k_post_cmd(hw, &cmd->header);
2429 kfree(cmd);
2430
2431 return rc;
2432}
2433
2434/*
2435 * CMD_SET_RTS_THRESHOLD.
2436 */
2437struct mwl8k_cmd_set_rts_threshold {
2438 struct mwl8k_cmd_pkt header;
2439 __le16 action;
2440 __le16 threshold;
2441} __attribute__((packed));
2442
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002443static int
2444mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002445{
2446 struct mwl8k_cmd_set_rts_threshold *cmd;
2447 int rc;
2448
2449 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2450 if (cmd == NULL)
2451 return -ENOMEM;
2452
2453 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2454 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002455 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2456 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002457
2458 rc = mwl8k_post_cmd(hw, &cmd->header);
2459 kfree(cmd);
2460
2461 return rc;
2462}
2463
2464/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002465 * CMD_SET_SLOT.
2466 */
2467struct mwl8k_cmd_set_slot {
2468 struct mwl8k_cmd_pkt header;
2469 __le16 action;
2470 __u8 short_slot;
2471} __attribute__((packed));
2472
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002473static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002474{
2475 struct mwl8k_cmd_set_slot *cmd;
2476 int rc;
2477
2478 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2479 if (cmd == NULL)
2480 return -ENOMEM;
2481
2482 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2483 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2484 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002485 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002486
2487 rc = mwl8k_post_cmd(hw, &cmd->header);
2488 kfree(cmd);
2489
2490 return rc;
2491}
2492
2493/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002494 * CMD_SET_EDCA_PARAMS.
2495 */
2496struct mwl8k_cmd_set_edca_params {
2497 struct mwl8k_cmd_pkt header;
2498
2499 /* See MWL8K_SET_EDCA_XXX below */
2500 __le16 action;
2501
2502 /* TX opportunity in units of 32 us */
2503 __le16 txop;
2504
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002505 union {
2506 struct {
2507 /* Log exponent of max contention period: 0...15 */
2508 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002509
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002510 /* Log exponent of min contention period: 0...15 */
2511 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002512
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002513 /* Adaptive interframe spacing in units of 32us */
2514 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002515
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002516 /* TX queue to configure */
2517 __u8 txq;
2518 } ap;
2519 struct {
2520 /* Log exponent of max contention period: 0...15 */
2521 __u8 log_cw_max;
2522
2523 /* Log exponent of min contention period: 0...15 */
2524 __u8 log_cw_min;
2525
2526 /* Adaptive interframe spacing in units of 32us */
2527 __u8 aifs;
2528
2529 /* TX queue to configure */
2530 __u8 txq;
2531 } sta;
2532 };
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002533} __attribute__((packed));
2534
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002535#define MWL8K_SET_EDCA_CW 0x01
2536#define MWL8K_SET_EDCA_TXOP 0x02
2537#define MWL8K_SET_EDCA_AIFS 0x04
2538
2539#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2540 MWL8K_SET_EDCA_TXOP | \
2541 MWL8K_SET_EDCA_AIFS)
2542
2543static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002544mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2545 __u16 cw_min, __u16 cw_max,
2546 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002547{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002548 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002549 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002550 int rc;
2551
2552 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2553 if (cmd == NULL)
2554 return -ENOMEM;
2555
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002556 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2557 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002558 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2559 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002560 if (priv->ap_fw) {
2561 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2562 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2563 cmd->ap.aifs = aifs;
2564 cmd->ap.txq = qnum;
2565 } else {
2566 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2567 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2568 cmd->sta.aifs = aifs;
2569 cmd->sta.txq = qnum;
2570 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002571
2572 rc = mwl8k_post_cmd(hw, &cmd->header);
2573 kfree(cmd);
2574
2575 return rc;
2576}
2577
2578/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002579 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002580 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002581struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002582 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002583 __le16 action;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002584} __attribute__((packed));
2585
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002586static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002587{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002588 struct mwl8k_priv *priv = hw->priv;
2589 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002590 int rc;
2591
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002592 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2593 if (cmd == NULL)
2594 return -ENOMEM;
2595
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002596 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002597 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002598 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002599
2600 rc = mwl8k_post_cmd(hw, &cmd->header);
2601 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002602
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002603 if (!rc)
2604 priv->wmm_enabled = enable;
2605
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002606 return rc;
2607}
2608
2609/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002610 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002611 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002612struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002613 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002614 __le32 action;
2615 __u8 rx_antenna_map;
2616 __u8 tx_antenna_map;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002617} __attribute__((packed));
2618
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002619static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002620{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002621 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002622 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002623
2624 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2625 if (cmd == NULL)
2626 return -ENOMEM;
2627
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002628 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002629 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002630 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2631 cmd->rx_antenna_map = rx;
2632 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002633
2634 rc = mwl8k_post_cmd(hw, &cmd->header);
2635 kfree(cmd);
2636
2637 return rc;
2638}
2639
2640/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002641 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002642 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002643struct mwl8k_cmd_use_fixed_rate_sta {
2644 struct mwl8k_cmd_pkt header;
2645 __le32 action;
2646 __le32 allow_rate_drop;
2647 __le32 num_rates;
2648 struct {
2649 __le32 is_ht_rate;
2650 __le32 enable_retry;
2651 __le32 rate;
2652 __le32 retry_count;
2653 } rate_entry[8];
2654 __le32 rate_type;
2655 __le32 reserved1;
2656 __le32 reserved2;
2657} __attribute__((packed));
2658
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002659#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002660#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002661
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002662static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002663{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002664 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002665 int rc;
2666
2667 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2668 if (cmd == NULL)
2669 return -ENOMEM;
2670
2671 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2672 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002673 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2674 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002675
2676 rc = mwl8k_post_cmd(hw, &cmd->header);
2677 kfree(cmd);
2678
2679 return rc;
2680}
2681
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002682/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002683 * CMD_USE_FIXED_RATE (AP version).
2684 */
2685struct mwl8k_cmd_use_fixed_rate_ap {
2686 struct mwl8k_cmd_pkt header;
2687 __le32 action;
2688 __le32 allow_rate_drop;
2689 __le32 num_rates;
2690 struct mwl8k_rate_entry_ap {
2691 __le32 is_ht_rate;
2692 __le32 enable_retry;
2693 __le32 rate;
2694 __le32 retry_count;
2695 } rate_entry[4];
2696 u8 multicast_rate;
2697 u8 multicast_rate_type;
2698 u8 management_rate;
2699} __attribute__((packed));
2700
2701static int
2702mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2703{
2704 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2705 int rc;
2706
2707 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2708 if (cmd == NULL)
2709 return -ENOMEM;
2710
2711 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2712 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2713 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2714 cmd->multicast_rate = mcast;
2715 cmd->management_rate = mgmt;
2716
2717 rc = mwl8k_post_cmd(hw, &cmd->header);
2718 kfree(cmd);
2719
2720 return rc;
2721}
2722
2723/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002724 * CMD_ENABLE_SNIFFER.
2725 */
2726struct mwl8k_cmd_enable_sniffer {
2727 struct mwl8k_cmd_pkt header;
2728 __le32 action;
2729} __attribute__((packed));
2730
2731static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2732{
2733 struct mwl8k_cmd_enable_sniffer *cmd;
2734 int rc;
2735
2736 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2737 if (cmd == NULL)
2738 return -ENOMEM;
2739
2740 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2741 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2742 cmd->action = cpu_to_le32(!!enable);
2743
2744 rc = mwl8k_post_cmd(hw, &cmd->header);
2745 kfree(cmd);
2746
2747 return rc;
2748}
2749
2750/*
2751 * CMD_SET_MAC_ADDR.
2752 */
2753struct mwl8k_cmd_set_mac_addr {
2754 struct mwl8k_cmd_pkt header;
2755 union {
2756 struct {
2757 __le16 mac_type;
2758 __u8 mac_addr[ETH_ALEN];
2759 } mbss;
2760 __u8 mac_addr[ETH_ALEN];
2761 };
2762} __attribute__((packed));
2763
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002764#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
2765#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
2766#define MWL8K_MAC_TYPE_PRIMARY_AP 2
2767#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002768
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002769static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
2770 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002771{
2772 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002773 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002774 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002775 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002776 int rc;
2777
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002778 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2779 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
2780 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
2781 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
2782 else
2783 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
2784 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
2785 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
2786 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2787 else
2788 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
2789 }
2790
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002791 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2792 if (cmd == NULL)
2793 return -ENOMEM;
2794
2795 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2796 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2797 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002798 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002799 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2800 } else {
2801 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2802 }
2803
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002804 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002805 kfree(cmd);
2806
2807 return rc;
2808}
2809
2810/*
2811 * CMD_SET_RATEADAPT_MODE.
2812 */
2813struct mwl8k_cmd_set_rate_adapt_mode {
2814 struct mwl8k_cmd_pkt header;
2815 __le16 action;
2816 __le16 mode;
2817} __attribute__((packed));
2818
2819static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2820{
2821 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2822 int rc;
2823
2824 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2825 if (cmd == NULL)
2826 return -ENOMEM;
2827
2828 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2829 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2830 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2831 cmd->mode = cpu_to_le16(mode);
2832
2833 rc = mwl8k_post_cmd(hw, &cmd->header);
2834 kfree(cmd);
2835
2836 return rc;
2837}
2838
2839/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002840 * CMD_BSS_START.
2841 */
2842struct mwl8k_cmd_bss_start {
2843 struct mwl8k_cmd_pkt header;
2844 __le32 enable;
2845} __attribute__((packed));
2846
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002847static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
2848 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002849{
2850 struct mwl8k_cmd_bss_start *cmd;
2851 int rc;
2852
2853 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2854 if (cmd == NULL)
2855 return -ENOMEM;
2856
2857 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
2858 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2859 cmd->enable = cpu_to_le32(enable);
2860
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002861 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002862 kfree(cmd);
2863
2864 return rc;
2865}
2866
2867/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002868 * CMD_SET_NEW_STN.
2869 */
2870struct mwl8k_cmd_set_new_stn {
2871 struct mwl8k_cmd_pkt header;
2872 __le16 aid;
2873 __u8 mac_addr[6];
2874 __le16 stn_id;
2875 __le16 action;
2876 __le16 rsvd;
2877 __le32 legacy_rates;
2878 __u8 ht_rates[4];
2879 __le16 cap_info;
2880 __le16 ht_capabilities_info;
2881 __u8 mac_ht_param_info;
2882 __u8 rev;
2883 __u8 control_channel;
2884 __u8 add_channel;
2885 __le16 op_mode;
2886 __le16 stbc;
2887 __u8 add_qos_info;
2888 __u8 is_qos_sta;
2889 __le32 fw_sta_ptr;
2890} __attribute__((packed));
2891
2892#define MWL8K_STA_ACTION_ADD 0
2893#define MWL8K_STA_ACTION_REMOVE 2
2894
2895static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
2896 struct ieee80211_vif *vif,
2897 struct ieee80211_sta *sta)
2898{
2899 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002900 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002901 int rc;
2902
2903 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2904 if (cmd == NULL)
2905 return -ENOMEM;
2906
2907 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2908 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2909 cmd->aid = cpu_to_le16(sta->aid);
2910 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
2911 cmd->stn_id = cpu_to_le16(sta->aid);
2912 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002913 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
2914 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
2915 else
2916 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
2917 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002918 if (sta->ht_cap.ht_supported) {
2919 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
2920 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
2921 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
2922 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
2923 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
2924 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
2925 ((sta->ht_cap.ampdu_density & 7) << 2);
2926 cmd->is_qos_sta = 1;
2927 }
2928
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002929 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002930 kfree(cmd);
2931
2932 return rc;
2933}
2934
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002935static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
2936 struct ieee80211_vif *vif)
2937{
2938 struct mwl8k_cmd_set_new_stn *cmd;
2939 int rc;
2940
2941 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2942 if (cmd == NULL)
2943 return -ENOMEM;
2944
2945 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2946 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2947 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
2948
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002949 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002950 kfree(cmd);
2951
2952 return rc;
2953}
2954
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002955static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
2956 struct ieee80211_vif *vif, u8 *addr)
2957{
2958 struct mwl8k_cmd_set_new_stn *cmd;
2959 int rc;
2960
2961 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2962 if (cmd == NULL)
2963 return -ENOMEM;
2964
2965 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2966 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2967 memcpy(cmd->mac_addr, addr, ETH_ALEN);
2968 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
2969
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01002970 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002971 kfree(cmd);
2972
2973 return rc;
2974}
2975
2976/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002977 * CMD_UPDATE_STADB.
2978 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002979struct ewc_ht_info {
2980 __le16 control1;
2981 __le16 control2;
2982 __le16 control3;
2983} __attribute__((packed));
2984
2985struct peer_capability_info {
2986 /* Peer type - AP vs. STA. */
2987 __u8 peer_type;
2988
2989 /* Basic 802.11 capabilities from assoc resp. */
2990 __le16 basic_caps;
2991
2992 /* Set if peer supports 802.11n high throughput (HT). */
2993 __u8 ht_support;
2994
2995 /* Valid if HT is supported. */
2996 __le16 ht_caps;
2997 __u8 extended_ht_caps;
2998 struct ewc_ht_info ewc_info;
2999
3000 /* Legacy rate table. Intersection of our rates and peer rates. */
3001 __u8 legacy_rates[12];
3002
3003 /* HT rate table. Intersection of our rates and peer rates. */
3004 __u8 ht_rates[16];
3005 __u8 pad[16];
3006
3007 /* If set, interoperability mode, no proprietary extensions. */
3008 __u8 interop;
3009 __u8 pad2;
3010 __u8 station_id;
3011 __le16 amsdu_enabled;
3012} __attribute__((packed));
3013
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003014struct mwl8k_cmd_update_stadb {
3015 struct mwl8k_cmd_pkt header;
3016
3017 /* See STADB_ACTION_TYPE */
3018 __le32 action;
3019
3020 /* Peer MAC address */
3021 __u8 peer_addr[ETH_ALEN];
3022
3023 __le32 reserved;
3024
3025 /* Peer info - valid during add/update. */
3026 struct peer_capability_info peer_info;
3027} __attribute__((packed));
3028
Lennert Buytenheka6804002010-01-04 21:55:42 +01003029#define MWL8K_STA_DB_MODIFY_ENTRY 1
3030#define MWL8K_STA_DB_DEL_ENTRY 2
3031
3032/* Peer Entry flags - used to define the type of the peer node */
3033#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3034
3035static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003036 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003037 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003038{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003039 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003040 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003041 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003042 int rc;
3043
3044 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3045 if (cmd == NULL)
3046 return -ENOMEM;
3047
3048 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3049 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003050 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003051 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003052
Lennert Buytenheka6804002010-01-04 21:55:42 +01003053 p = &cmd->peer_info;
3054 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3055 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003056 p->ht_support = sta->ht_cap.ht_supported;
3057 p->ht_caps = sta->ht_cap.cap;
3058 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3059 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003060 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3061 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3062 else
3063 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3064 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003065 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003066 p->interop = 1;
3067 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003068
Lennert Buytenheka6804002010-01-04 21:55:42 +01003069 rc = mwl8k_post_cmd(hw, &cmd->header);
3070 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003071
Lennert Buytenheka6804002010-01-04 21:55:42 +01003072 return rc ? rc : p->station_id;
3073}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003074
Lennert Buytenheka6804002010-01-04 21:55:42 +01003075static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3076 struct ieee80211_vif *vif, u8 *addr)
3077{
3078 struct mwl8k_cmd_update_stadb *cmd;
3079 int rc;
3080
3081 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3082 if (cmd == NULL)
3083 return -ENOMEM;
3084
3085 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3086 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3087 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3088 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3089
3090 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003091 kfree(cmd);
3092
3093 return rc;
3094}
3095
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003096
3097/*
3098 * Interrupt handling.
3099 */
3100static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3101{
3102 struct ieee80211_hw *hw = dev_id;
3103 struct mwl8k_priv *priv = hw->priv;
3104 u32 status;
3105
3106 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003107 if (!status)
3108 return IRQ_NONE;
3109
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003110 if (status & MWL8K_A2H_INT_TX_DONE) {
3111 status &= ~MWL8K_A2H_INT_TX_DONE;
3112 tasklet_schedule(&priv->poll_tx_task);
3113 }
3114
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003115 if (status & MWL8K_A2H_INT_RX_READY) {
3116 status &= ~MWL8K_A2H_INT_RX_READY;
3117 tasklet_schedule(&priv->poll_rx_task);
3118 }
3119
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003120 if (status)
3121 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003122
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003123 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003124 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003125 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003126 }
3127
3128 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003129 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003130 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003131 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003132 }
3133
3134 return IRQ_HANDLED;
3135}
3136
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003137static void mwl8k_tx_poll(unsigned long data)
3138{
3139 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3140 struct mwl8k_priv *priv = hw->priv;
3141 int limit;
3142 int i;
3143
3144 limit = 32;
3145
3146 spin_lock_bh(&priv->tx_lock);
3147
3148 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3149 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3150
3151 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3152 complete(priv->tx_wait);
3153 priv->tx_wait = NULL;
3154 }
3155
3156 spin_unlock_bh(&priv->tx_lock);
3157
3158 if (limit) {
3159 writel(~MWL8K_A2H_INT_TX_DONE,
3160 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3161 } else {
3162 tasklet_schedule(&priv->poll_tx_task);
3163 }
3164}
3165
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003166static void mwl8k_rx_poll(unsigned long data)
3167{
3168 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3169 struct mwl8k_priv *priv = hw->priv;
3170 int limit;
3171
3172 limit = 32;
3173 limit -= rxq_process(hw, 0, limit);
3174 limit -= rxq_refill(hw, 0, limit);
3175
3176 if (limit) {
3177 writel(~MWL8K_A2H_INT_RX_READY,
3178 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3179 } else {
3180 tasklet_schedule(&priv->poll_rx_task);
3181 }
3182}
3183
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003184
3185/*
3186 * Core driver operations.
3187 */
3188static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3189{
3190 struct mwl8k_priv *priv = hw->priv;
3191 int index = skb_get_queue_mapping(skb);
3192 int rc;
3193
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003194 if (!priv->radio_on) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003195 printk(KERN_DEBUG "%s: dropped TX frame since radio "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003196 "disabled\n", wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003197 dev_kfree_skb(skb);
3198 return NETDEV_TX_OK;
3199 }
3200
3201 rc = mwl8k_txq_xmit(hw, index, skb);
3202
3203 return rc;
3204}
3205
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003206static int mwl8k_start(struct ieee80211_hw *hw)
3207{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003208 struct mwl8k_priv *priv = hw->priv;
3209 int rc;
3210
Joe Perchesa0607fd2009-11-18 23:29:17 -08003211 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003212 IRQF_SHARED, MWL8K_NAME, hw);
3213 if (rc) {
3214 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003215 wiphy_name(hw->wiphy));
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003216 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003217 }
3218
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003219 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003220 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003221 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003222
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003223 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003224 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003225
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003226 rc = mwl8k_fw_lock(hw);
3227 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003228 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003229
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003230 if (!priv->ap_fw) {
3231 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003232 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003233
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003234 if (!rc)
3235 rc = mwl8k_cmd_set_pre_scan(hw);
3236
3237 if (!rc)
3238 rc = mwl8k_cmd_set_post_scan(hw,
3239 "\x00\x00\x00\x00\x00\x00");
3240 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003241
3242 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003243 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003244
3245 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003246 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003247
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003248 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003249 }
3250
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003251 if (rc) {
3252 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3253 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003254 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003255 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003256 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003257
3258 return rc;
3259}
3260
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003261static void mwl8k_stop(struct ieee80211_hw *hw)
3262{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003263 struct mwl8k_priv *priv = hw->priv;
3264 int i;
3265
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003266 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003267
3268 ieee80211_stop_queues(hw);
3269
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003270 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003271 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003272 free_irq(priv->pdev->irq, hw);
3273
3274 /* Stop finalize join worker */
3275 cancel_work_sync(&priv->finalize_join_worker);
3276 if (priv->beacon_skb != NULL)
3277 dev_kfree_skb(priv->beacon_skb);
3278
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003279 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003280 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003281 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003282
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003283 /* Return all skbs to mac80211 */
3284 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003285 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003286}
3287
3288static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003289 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003290{
3291 struct mwl8k_priv *priv = hw->priv;
3292 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003293 u32 macids_supported;
3294 int macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003295
3296 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003297 * Reject interface creation if sniffer mode is active, as
3298 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003299 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003300 */
3301 if (priv->sniffer_enabled) {
3302 printk(KERN_INFO "%s: unable to create STA "
3303 "interface due to sniffer mode being enabled\n",
3304 wiphy_name(hw->wiphy));
3305 return -EINVAL;
3306 }
3307
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003308
3309 switch (vif->type) {
3310 case NL80211_IFTYPE_AP:
3311 macids_supported = priv->ap_macids_supported;
3312 break;
3313 case NL80211_IFTYPE_STATION:
3314 macids_supported = priv->sta_macids_supported;
3315 break;
3316 default:
3317 return -EINVAL;
3318 }
3319
3320 macid = ffs(macids_supported & ~priv->macids_used);
3321 if (!macid--)
3322 return -EBUSY;
3323
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003324 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003325 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003326 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003327 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003328 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003329 mwl8k_vif->seqno = 0;
3330
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003331 /* Set the mac address. */
3332 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3333
3334 if (priv->ap_fw)
3335 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3336
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003337 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003338 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003339
3340 return 0;
3341}
3342
3343static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003344 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003345{
3346 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003347 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003348
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003349 if (priv->ap_fw)
3350 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3351
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003352 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003353
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003354 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003355 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003356}
3357
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003358static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003359{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003360 struct ieee80211_conf *conf = &hw->conf;
3361 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003362 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003363
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003364 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003365 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003366 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003367 }
3368
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003369 rc = mwl8k_fw_lock(hw);
3370 if (rc)
3371 return rc;
3372
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003373 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003374 if (rc)
3375 goto out;
3376
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003377 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003378 if (rc)
3379 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003380
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003381 if (conf->power_level > 18)
3382 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003383 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003384 if (rc)
3385 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003386
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003387 if (priv->ap_fw) {
3388 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3389 if (!rc)
3390 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3391 } else {
3392 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3393 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003394
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003395out:
3396 mwl8k_fw_unlock(hw);
3397
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003398 return rc;
3399}
3400
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003401static void
3402mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3403 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003404{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003405 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003406 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003407 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003408 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003409
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003410 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003411 return;
3412
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003413 /*
3414 * No need to capture a beacon if we're no longer associated.
3415 */
3416 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3417 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003418
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003419 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003420 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003421 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003422 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003423 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003424
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003425 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003426
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003427 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003428 if (ap == NULL) {
3429 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003430 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003431 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003432
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003433 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
3434 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
3435 } else {
3436 ap_legacy_rates =
3437 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3438 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003439 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003440
3441 rcu_read_unlock();
3442 }
3443
3444 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003445 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003446 if (rc)
3447 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003448
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003449 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003450 if (rc)
3451 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003452 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003453
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003454 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003455 rc = mwl8k_set_radio_preamble(hw,
3456 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003457 if (rc)
3458 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003459 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003460
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003461 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003462 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003463 if (rc)
3464 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003465 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003466
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003467 if (vif->bss_conf.assoc &&
3468 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
3469 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003470 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003471 if (rc)
3472 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003473 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003474
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003475 if (vif->bss_conf.assoc &&
3476 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003477 /*
3478 * Finalize the join. Tell rx handler to process
3479 * next beacon from our BSSID.
3480 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003481 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003482 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003483 }
3484
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003485out:
3486 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003487}
3488
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003489static void
3490mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3491 struct ieee80211_bss_conf *info, u32 changed)
3492{
3493 int rc;
3494
3495 if (mwl8k_fw_lock(hw))
3496 return;
3497
3498 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3499 rc = mwl8k_set_radio_preamble(hw,
3500 vif->bss_conf.use_short_preamble);
3501 if (rc)
3502 goto out;
3503 }
3504
3505 if (changed & BSS_CHANGED_BASIC_RATES) {
3506 int idx;
3507 int rate;
3508
3509 /*
3510 * Use lowest supported basic rate for multicasts
3511 * and management frames (such as probe responses --
3512 * beacons will always go out at 1 Mb/s).
3513 */
3514 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003515 if (idx)
3516 idx--;
3517
3518 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3519 rate = mwl8k_rates_24[idx].hw_value;
3520 else
3521 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003522
3523 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
3524 }
3525
3526 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
3527 struct sk_buff *skb;
3528
3529 skb = ieee80211_beacon_get(hw, vif);
3530 if (skb != NULL) {
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003531 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003532 kfree_skb(skb);
3533 }
3534 }
3535
3536 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01003537 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003538
3539out:
3540 mwl8k_fw_unlock(hw);
3541}
3542
3543static void
3544mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3545 struct ieee80211_bss_conf *info, u32 changed)
3546{
3547 struct mwl8k_priv *priv = hw->priv;
3548
3549 if (!priv->ap_fw)
3550 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
3551 else
3552 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
3553}
3554
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003555static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
3556 int mc_count, struct dev_addr_list *mclist)
3557{
3558 struct mwl8k_cmd_pkt *cmd;
3559
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003560 /*
3561 * Synthesize and return a command packet that programs the
3562 * hardware multicast address filter. At this point we don't
3563 * know whether FIF_ALLMULTI is being requested, but if it is,
3564 * we'll end up throwing this packet away and creating a new
3565 * one in mwl8k_configure_filter().
3566 */
3567 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_count, mclist);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003568
3569 return (unsigned long)cmd;
3570}
3571
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003572static int
3573mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3574 unsigned int changed_flags,
3575 unsigned int *total_flags)
3576{
3577 struct mwl8k_priv *priv = hw->priv;
3578
3579 /*
3580 * Hardware sniffer mode is mutually exclusive with STA
3581 * operation, so refuse to enable sniffer mode if a STA
3582 * interface is active.
3583 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003584 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003585 if (net_ratelimit())
3586 printk(KERN_INFO "%s: not enabling sniffer "
3587 "mode because STA interface is active\n",
3588 wiphy_name(hw->wiphy));
3589 return 0;
3590 }
3591
3592 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003593 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003594 return 0;
3595 priv->sniffer_enabled = true;
3596 }
3597
3598 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3599 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3600 FIF_OTHER_BSS;
3601
3602 return 1;
3603}
3604
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003605static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
3606{
3607 if (!list_empty(&priv->vif_list))
3608 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
3609
3610 return NULL;
3611}
3612
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003613static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3614 unsigned int changed_flags,
3615 unsigned int *total_flags,
3616 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003617{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003618 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003619 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3620
3621 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003622 * AP firmware doesn't allow fine-grained control over
3623 * the receive filter.
3624 */
3625 if (priv->ap_fw) {
3626 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3627 kfree(cmd);
3628 return;
3629 }
3630
3631 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003632 * Enable hardware sniffer mode if FIF_CONTROL or
3633 * FIF_OTHER_BSS is requested.
3634 */
3635 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3636 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3637 kfree(cmd);
3638 return;
3639 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003640
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003641 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003642 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003643
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003644 if (mwl8k_fw_lock(hw)) {
3645 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003646 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003647 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003648
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003649 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003650 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003651 priv->sniffer_enabled = false;
3652 }
3653
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003654 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003655 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3656 /*
3657 * Disable the BSS filter.
3658 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003659 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003660 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003661 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003662 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003663
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003664 /*
3665 * Enable the BSS filter.
3666 *
3667 * If there is an active STA interface, use that
3668 * interface's BSSID, otherwise use a dummy one
3669 * (where the OUI part needs to be nonzero for
3670 * the BSSID to be accepted by POST_SCAN).
3671 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003672 mwl8k_vif = mwl8k_first_vif(priv);
3673 if (mwl8k_vif != NULL)
3674 bssid = mwl8k_vif->vif->bss_conf.bssid;
3675 else
3676 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003677
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003678 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003679 }
3680 }
3681
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003682 /*
3683 * If FIF_ALLMULTI is being requested, throw away the command
3684 * packet that ->prepare_multicast() built and replace it with
3685 * a command packet that enables reception of all multicast
3686 * packets.
3687 */
3688 if (*total_flags & FIF_ALLMULTI) {
3689 kfree(cmd);
3690 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, 0, NULL);
3691 }
3692
3693 if (cmd != NULL) {
3694 mwl8k_post_cmd(hw, cmd);
3695 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003696 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003697
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003698 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003699}
3700
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003701static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3702{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003703 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003704}
3705
Johannes Berg4a6967b2010-02-19 19:18:37 +01003706static int mwl8k_sta_remove(struct ieee80211_hw *hw,
3707 struct ieee80211_vif *vif,
3708 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003709{
3710 struct mwl8k_priv *priv = hw->priv;
3711
Johannes Berg4a6967b2010-02-19 19:18:37 +01003712 if (priv->ap_fw)
3713 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
3714 else
3715 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
3716}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003717
Johannes Berg4a6967b2010-02-19 19:18:37 +01003718static int mwl8k_sta_add(struct ieee80211_hw *hw,
3719 struct ieee80211_vif *vif,
3720 struct ieee80211_sta *sta)
3721{
3722 struct mwl8k_priv *priv = hw->priv;
3723 int ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003724
Johannes Berg4a6967b2010-02-19 19:18:37 +01003725 if (!priv->ap_fw) {
3726 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
3727 if (ret >= 0) {
3728 MWL8K_STA(sta)->peer_id = ret;
3729 return 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003730 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01003731
3732 return ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003733 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003734
Johannes Berg4a6967b2010-02-19 19:18:37 +01003735 return mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003736}
3737
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003738static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3739 const struct ieee80211_tx_queue_params *params)
3740{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003741 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003742 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003743
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003744 rc = mwl8k_fw_lock(hw);
3745 if (!rc) {
3746 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003747 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003748
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003749 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003750 rc = mwl8k_cmd_set_edca_params(hw, queue,
3751 params->cw_min,
3752 params->cw_max,
3753 params->aifs,
3754 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003755
3756 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003757 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003758
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003759 return rc;
3760}
3761
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003762static int mwl8k_get_stats(struct ieee80211_hw *hw,
3763 struct ieee80211_low_level_stats *stats)
3764{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003765 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003766}
3767
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003768static int
3769mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3770 enum ieee80211_ampdu_mlme_action action,
3771 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3772{
3773 switch (action) {
3774 case IEEE80211_AMPDU_RX_START:
3775 case IEEE80211_AMPDU_RX_STOP:
3776 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3777 return -ENOTSUPP;
3778 return 0;
3779 default:
3780 return -ENOTSUPP;
3781 }
3782}
3783
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003784static const struct ieee80211_ops mwl8k_ops = {
3785 .tx = mwl8k_tx,
3786 .start = mwl8k_start,
3787 .stop = mwl8k_stop,
3788 .add_interface = mwl8k_add_interface,
3789 .remove_interface = mwl8k_remove_interface,
3790 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003791 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003792 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003793 .configure_filter = mwl8k_configure_filter,
3794 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01003795 .sta_add = mwl8k_sta_add,
3796 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003797 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003798 .get_stats = mwl8k_get_stats,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003799 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003800};
3801
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003802static void mwl8k_finalize_join_worker(struct work_struct *work)
3803{
3804 struct mwl8k_priv *priv =
3805 container_of(work, struct mwl8k_priv, finalize_join_worker);
3806 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01003807 struct ieee80211_mgmt *mgmt = (void *)skb->data;
3808 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
3809 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
3810 mgmt->u.beacon.variable, len);
3811 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003812
Johannes Berg56007a02010-01-26 14:19:52 +01003813 if (tim && tim[1] >= 2)
3814 dtim_period = tim[3];
3815
3816 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003817
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003818 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003819 priv->beacon_skb = NULL;
3820}
3821
John W. Linvillebcb628d2009-11-06 16:40:16 -05003822enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003823 MWL8363 = 0,
3824 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003825 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003826};
3827
John W. Linvillebcb628d2009-11-06 16:40:16 -05003828static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003829 [MWL8363] = {
3830 .part_name = "88w8363",
3831 .helper_image = "mwl8k/helper_8363.fw",
3832 .fw_image = "mwl8k/fmimage_8363.fw",
3833 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003834 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003835 .part_name = "88w8687",
3836 .helper_image = "mwl8k/helper_8687.fw",
3837 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003838 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003839 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003840 .part_name = "88w8366",
3841 .helper_image = "mwl8k/helper_8366.fw",
3842 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003843 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003844 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003845};
3846
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01003847MODULE_FIRMWARE("mwl8k/helper_8363.fw");
3848MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
3849MODULE_FIRMWARE("mwl8k/helper_8687.fw");
3850MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
3851MODULE_FIRMWARE("mwl8k/helper_8366.fw");
3852MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
3853
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003854static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003855 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3856 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003857 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3858 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3859 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01003860 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003861 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003862};
3863MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3864
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003865static int __devinit mwl8k_probe(struct pci_dev *pdev,
3866 const struct pci_device_id *id)
3867{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003868 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003869 struct ieee80211_hw *hw;
3870 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003871 int rc;
3872 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003873
3874 if (!printed_version) {
3875 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3876 printed_version = 1;
3877 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003878
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003879
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003880 rc = pci_enable_device(pdev);
3881 if (rc) {
3882 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3883 MWL8K_NAME);
3884 return rc;
3885 }
3886
3887 rc = pci_request_regions(pdev, MWL8K_NAME);
3888 if (rc) {
3889 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3890 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003891 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003892 }
3893
3894 pci_set_master(pdev);
3895
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003896
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003897 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3898 if (hw == NULL) {
3899 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3900 rc = -ENOMEM;
3901 goto err_free_reg;
3902 }
3903
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003904 SET_IEEE80211_DEV(hw, &pdev->dev);
3905 pci_set_drvdata(pdev, hw);
3906
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003907 priv = hw->priv;
3908 priv->hw = hw;
3909 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003910 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003911
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003912
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003913 priv->sram = pci_iomap(pdev, 0, 0x10000);
3914 if (priv->sram == NULL) {
3915 printk(KERN_ERR "%s: Cannot map device SRAM\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02003916 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003917 goto err_iounmap;
3918 }
3919
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003920 /*
3921 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3922 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3923 */
3924 priv->regs = pci_iomap(pdev, 1, 0x10000);
3925 if (priv->regs == NULL) {
3926 priv->regs = pci_iomap(pdev, 2, 0x10000);
3927 if (priv->regs == NULL) {
3928 printk(KERN_ERR "%s: Cannot map device registers\n",
3929 wiphy_name(hw->wiphy));
3930 goto err_iounmap;
3931 }
3932 }
3933
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003934
3935 /* Reset firmware and hardware */
3936 mwl8k_hw_reset(priv);
3937
3938 /* Ask userland hotplug daemon for the device firmware */
3939 rc = mwl8k_request_firmware(priv);
3940 if (rc) {
3941 printk(KERN_ERR "%s: Firmware files not found\n",
3942 wiphy_name(hw->wiphy));
3943 goto err_stop_firmware;
3944 }
3945
3946 /* Load firmware into hardware */
3947 rc = mwl8k_load_firmware(hw);
3948 if (rc) {
3949 printk(KERN_ERR "%s: Cannot start firmware\n",
3950 wiphy_name(hw->wiphy));
3951 goto err_stop_firmware;
3952 }
3953
3954 /* Reclaim memory once firmware is successfully loaded */
3955 mwl8k_release_firmware(priv);
3956
3957
Lennert Buytenhek91942232010-01-04 21:53:54 +01003958 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003959 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003960 if (priv->rxd_ops == NULL) {
3961 printk(KERN_ERR "%s: Driver does not have AP "
3962 "firmware image support for this hardware\n",
3963 wiphy_name(hw->wiphy));
3964 goto err_stop_firmware;
3965 }
3966 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003967 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003968 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003969
3970 priv->sniffer_enabled = false;
3971 priv->wmm_enabled = false;
3972 priv->pending_tx_pkts = 0;
3973
3974
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003975 /*
3976 * Extra headroom is the size of the required DMA header
3977 * minus the size of the smallest 802.11 frame (CTS frame).
3978 */
3979 hw->extra_tx_headroom =
3980 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3981
3982 hw->channel_change_time = 10;
3983
3984 hw->queues = MWL8K_TX_QUEUES;
3985
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003986 /* Set rssi and noise values to dBm */
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003987 hw->flags |= IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_NOISE_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003988 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003989 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003990
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003991 priv->macids_used = 0;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003992 INIT_LIST_HEAD(&priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003993
3994 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02003995 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02003996 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003997
3998 /* Finalize join worker */
3999 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4000
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004001 /* TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004002 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4003 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004004 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4005 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004006
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004007 /* Power management cookie */
4008 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4009 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004010 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004011
4012 rc = mwl8k_rxq_init(hw, 0);
4013 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004014 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004015 rxq_refill(hw, 0, INT_MAX);
4016
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004017 mutex_init(&priv->fw_mutex);
4018 priv->fw_mutex_owner = NULL;
4019 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004020 priv->hostcmd_wait = NULL;
4021
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004022 spin_lock_init(&priv->tx_lock);
4023
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004024 priv->tx_wait = NULL;
4025
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004026 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4027 rc = mwl8k_txq_init(hw, i);
4028 if (rc)
4029 goto err_free_queues;
4030 }
4031
4032 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004033 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004034 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004035 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004036 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4037
Joe Perchesa0607fd2009-11-18 23:29:17 -08004038 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004039 IRQF_SHARED, MWL8K_NAME, hw);
4040 if (rc) {
4041 printk(KERN_ERR "%s: failed to register IRQ handler\n",
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004042 wiphy_name(hw->wiphy));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004043 goto err_free_queues;
4044 }
4045
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004046 /*
4047 * Temporarily enable interrupts. Initial firmware host
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004048 * commands use interrupts and avoid polling. Disable
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004049 * interrupts when done.
4050 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004051 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004052
4053 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba21d2009-10-22 20:21:30 +02004054 if (priv->ap_fw) {
4055 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4056 if (!rc)
4057 rc = mwl8k_cmd_set_hw_spec(hw);
4058 } else {
4059 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4060 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004061 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004062 printk(KERN_ERR "%s: Cannot initialise firmware\n",
4063 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004064 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004065 }
4066
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004067 hw->wiphy->interface_modes = 0;
4068 if (priv->ap_macids_supported)
4069 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4070 if (priv->sta_macids_supported)
4071 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4072
4073
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004074 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004075 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004076 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004077 printk(KERN_ERR "%s: Cannot disable\n", wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004078 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004079 }
4080
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004081 /* Clear MAC address */
Lennert Buytenhekaa21d0f62010-01-12 13:50:36 +01004082 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004083 if (rc) {
4084 printk(KERN_ERR "%s: Cannot clear MAC address\n",
4085 wiphy_name(hw->wiphy));
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004086 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004087 }
4088
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004089 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004090 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004091 free_irq(priv->pdev->irq, hw);
4092
4093 rc = ieee80211_register_hw(hw);
4094 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004095 printk(KERN_ERR "%s: Cannot register device\n",
4096 wiphy_name(hw->wiphy));
Lennert Buytenhek153458f2010-01-04 21:54:08 +01004097 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004098 }
4099
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02004100 printk(KERN_INFO "%s: %s v%d, %pM, %s firmware %u.%u.%u.%u\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +02004101 wiphy_name(hw->wiphy), priv->device_info->part_name,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004102 priv->hw_rev, hw->wiphy->perm_addr,
Lennert Buytenhekeae74e62009-10-22 20:20:53 +02004103 priv->ap_fw ? "AP" : "STA",
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02004104 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4105 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004106
4107 return 0;
4108
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004109err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004110 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004111 free_irq(priv->pdev->irq, hw);
4112
4113err_free_queues:
4114 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4115 mwl8k_txq_deinit(hw, i);
4116 mwl8k_rxq_deinit(hw, 0);
4117
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004118err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004119 if (priv->cookie != NULL)
4120 pci_free_consistent(priv->pdev, 4,
4121 priv->cookie, priv->cookie_dma);
4122
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004123err_stop_firmware:
4124 mwl8k_hw_reset(priv);
4125 mwl8k_release_firmware(priv);
4126
4127err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004128 if (priv->regs != NULL)
4129 pci_iounmap(pdev, priv->regs);
4130
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004131 if (priv->sram != NULL)
4132 pci_iounmap(pdev, priv->sram);
4133
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004134 pci_set_drvdata(pdev, NULL);
4135 ieee80211_free_hw(hw);
4136
4137err_free_reg:
4138 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004139
4140err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004141 pci_disable_device(pdev);
4142
4143 return rc;
4144}
4145
Joerg Albert230f7af2009-04-18 02:10:45 +02004146static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004147{
4148 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4149}
4150
Joerg Albert230f7af2009-04-18 02:10:45 +02004151static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004152{
4153 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4154 struct mwl8k_priv *priv;
4155 int i;
4156
4157 if (hw == NULL)
4158 return;
4159 priv = hw->priv;
4160
4161 ieee80211_stop_queues(hw);
4162
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004163 ieee80211_unregister_hw(hw);
4164
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004165 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004166 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004167 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004168
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004169 /* Stop hardware */
4170 mwl8k_hw_reset(priv);
4171
4172 /* Return all skbs to mac80211 */
4173 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004174 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004175
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004176 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4177 mwl8k_txq_deinit(hw, i);
4178
4179 mwl8k_rxq_deinit(hw, 0);
4180
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004181 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004182
4183 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004184 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004185 pci_set_drvdata(pdev, NULL);
4186 ieee80211_free_hw(hw);
4187 pci_release_regions(pdev);
4188 pci_disable_device(pdev);
4189}
4190
4191static struct pci_driver mwl8k_driver = {
4192 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004193 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004194 .probe = mwl8k_probe,
4195 .remove = __devexit_p(mwl8k_remove),
4196 .shutdown = __devexit_p(mwl8k_shutdown),
4197};
4198
4199static int __init mwl8k_init(void)
4200{
4201 return pci_register_driver(&mwl8k_driver);
4202}
4203
4204static void __exit mwl8k_exit(void)
4205{
4206 pci_unregister_driver(&mwl8k_driver);
4207}
4208
4209module_init(mwl8k_init);
4210module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004211
4212MODULE_DESCRIPTION(MWL8K_DESC);
4213MODULE_VERSION(MWL8K_VERSION);
4214MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
4215MODULE_LICENSE("GPL");