blob: 1bbcd7c1d02ab5d704bccdef122ac4ab3d1f5a69 [file] [log] [blame]
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001/*
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02002 * drivers/net/wireless/mwl8k.c
3 * Driver for Marvell TOPDOG 802.11 Wireless cards
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004 *
Lennert Buytenheka5fb2972010-01-12 13:51:38 +01005 * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01006 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <linux/init.h>
13#include <linux/module.h>
14#include <linux/kernel.h>
Lennert Buytenhek3d76e822009-10-22 20:20:16 +020015#include <linux/sched.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010016#include <linux/spinlock.h>
17#include <linux/list.h>
18#include <linux/pci.h>
19#include <linux/delay.h>
20#include <linux/completion.h>
21#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Lennert Buytenheka66098d2009-03-10 10:13:33 +010023#include <net/mac80211.h>
24#include <linux/moduleparam.h>
25#include <linux/firmware.h>
26#include <linux/workqueue.h>
27
28#define MWL8K_DESC "Marvell TOPDOG(R) 802.11 Wireless Network Driver"
29#define MWL8K_NAME KBUILD_MODNAME
Lennert Buytenheka5fb2972010-01-12 13:51:38 +010030#define MWL8K_VERSION "0.12"
Lennert Buytenheka66098d2009-03-10 10:13:33 +010031
Lennert Buytenheka66098d2009-03-10 10:13:33 +010032/* Register definitions */
33#define MWL8K_HIU_GEN_PTR 0x00000c10
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020034#define MWL8K_MODE_STA 0x0000005a
35#define MWL8K_MODE_AP 0x000000a5
Lennert Buytenheka66098d2009-03-10 10:13:33 +010036#define MWL8K_HIU_INT_CODE 0x00000c14
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020037#define MWL8K_FWSTA_READY 0xf0f1f2f4
38#define MWL8K_FWAP_READY 0xf1f2f4a5
39#define MWL8K_INT_CODE_CMD_FINISHED 0x00000005
Lennert Buytenheka66098d2009-03-10 10:13:33 +010040#define MWL8K_HIU_SCRATCH 0x00000c40
41
42/* Host->device communications */
43#define MWL8K_HIU_H2A_INTERRUPT_EVENTS 0x00000c18
44#define MWL8K_HIU_H2A_INTERRUPT_STATUS 0x00000c1c
45#define MWL8K_HIU_H2A_INTERRUPT_MASK 0x00000c20
46#define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL 0x00000c24
47#define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK 0x00000c28
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020048#define MWL8K_H2A_INT_DUMMY (1 << 20)
49#define MWL8K_H2A_INT_RESET (1 << 15)
50#define MWL8K_H2A_INT_DOORBELL (1 << 1)
51#define MWL8K_H2A_INT_PPA_READY (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010052
53/* Device->host communications */
54#define MWL8K_HIU_A2H_INTERRUPT_EVENTS 0x00000c2c
55#define MWL8K_HIU_A2H_INTERRUPT_STATUS 0x00000c30
56#define MWL8K_HIU_A2H_INTERRUPT_MASK 0x00000c34
57#define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL 0x00000c38
58#define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK 0x00000c3c
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +020059#define MWL8K_A2H_INT_DUMMY (1 << 20)
60#define MWL8K_A2H_INT_CHNL_SWITCHED (1 << 11)
61#define MWL8K_A2H_INT_QUEUE_EMPTY (1 << 10)
62#define MWL8K_A2H_INT_RADAR_DETECT (1 << 7)
63#define MWL8K_A2H_INT_RADIO_ON (1 << 6)
64#define MWL8K_A2H_INT_RADIO_OFF (1 << 5)
65#define MWL8K_A2H_INT_MAC_EVENT (1 << 3)
66#define MWL8K_A2H_INT_OPC_DONE (1 << 2)
67#define MWL8K_A2H_INT_RX_READY (1 << 1)
68#define MWL8K_A2H_INT_TX_DONE (1 << 0)
Lennert Buytenheka66098d2009-03-10 10:13:33 +010069
70#define MWL8K_A2H_EVENTS (MWL8K_A2H_INT_DUMMY | \
71 MWL8K_A2H_INT_CHNL_SWITCHED | \
72 MWL8K_A2H_INT_QUEUE_EMPTY | \
73 MWL8K_A2H_INT_RADAR_DETECT | \
74 MWL8K_A2H_INT_RADIO_ON | \
75 MWL8K_A2H_INT_RADIO_OFF | \
76 MWL8K_A2H_INT_MAC_EVENT | \
77 MWL8K_A2H_INT_OPC_DONE | \
78 MWL8K_A2H_INT_RX_READY | \
79 MWL8K_A2H_INT_TX_DONE)
80
Lennert Buytenheka66098d2009-03-10 10:13:33 +010081#define MWL8K_RX_QUEUES 1
82#define MWL8K_TX_QUEUES 4
83
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020084struct rxd_ops {
85 int rxd_size;
86 void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
87 void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +010088 int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -040089 __le16 *qos, s8 *noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +020090};
91
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020092struct mwl8k_device_info {
Lennert Buytenheka74b2952009-10-22 20:20:50 +020093 char *part_name;
94 char *helper_image;
95 char *fw_image;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +010096 struct rxd_ops *ap_rxd_ops;
Lennert Buytenhek45a390d2009-10-22 20:20:47 +020097};
98
Lennert Buytenheka66098d2009-03-10 10:13:33 +010099struct mwl8k_rx_queue {
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200100 int rxd_count;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100101
102 /* hw receives here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200103 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100104
105 /* refill descs here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200106 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100107
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200108 void *rxd;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200109 dma_addr_t rxd_dma;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200110 struct {
111 struct sk_buff *skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900112 DEFINE_DMA_UNMAP_ADDR(dma);
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200113 } *buf;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100114};
115
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100116struct mwl8k_tx_queue {
117 /* hw transmits here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200118 int head;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100119
120 /* sw appends here */
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200121 int tail;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100122
Kalle Valo8ccbc3b2010-02-07 10:20:52 +0200123 unsigned int len;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200124 struct mwl8k_tx_desc *txd;
125 dma_addr_t txd_dma;
126 struct sk_buff **skb;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100127};
128
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100129struct mwl8k_priv {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100130 struct ieee80211_hw *hw;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100131 struct pci_dev *pdev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100132
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200133 struct mwl8k_device_info *device_info;
134
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100135 void __iomem *sram;
136 void __iomem *regs;
137
138 /* firmware */
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100139 struct firmware *fw_helper;
140 struct firmware *fw_ucode;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100141
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100142 /* hardware/firmware parameters */
143 bool ap_fw;
144 struct rxd_ops *rxd_ops;
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100145 struct ieee80211_supported_band band_24;
146 struct ieee80211_channel channels_24[14];
147 struct ieee80211_rate rates_24[14];
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100148 struct ieee80211_supported_band band_50;
149 struct ieee80211_channel channels_50[4];
150 struct ieee80211_rate rates_50[9];
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100151 u32 ap_macids_supported;
152 u32 sta_macids_supported;
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +0100153
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200154 /* firmware access */
155 struct mutex fw_mutex;
156 struct task_struct *fw_mutex_owner;
157 int fw_mutex_depth;
Lennert Buytenhek618952a2009-08-18 03:18:01 +0200158 struct completion *hostcmd_wait;
159
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100160 /* lock held over TX and TX reap */
161 spinlock_t tx_lock;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100162
Lennert Buytenhek88de754a2009-10-22 20:19:37 +0200163 /* TX quiesce completion, protected by fw_mutex and tx_lock */
164 struct completion *tx_wait;
165
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100166 /* List of interfaces. */
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +0100167 u32 macids_used;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100168 struct list_head vif_list;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100169
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100170 /* power management status cookie from firmware */
171 u32 *cookie;
172 dma_addr_t cookie_dma;
173
174 u16 num_mcaddrs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100175 u8 hw_rev;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +0200176 u32 fw_rev;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100177
178 /*
179 * Running count of TX packets in flight, to avoid
180 * iterating over the transmit rings each time.
181 */
182 int pending_tx_pkts;
183
184 struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
185 struct mwl8k_tx_queue txq[MWL8K_TX_QUEUES];
186
Lennert Buytenhekc46563b2009-07-16 12:14:58 +0200187 bool radio_on;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +0200188 bool radio_short_preamble;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +0200189 bool sniffer_enabled;
Lennert Buytenhek0439b1f2009-07-16 12:34:02 +0200190 bool wmm_enabled;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100191
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100192 /* XXX need to convert this to handle multiple interfaces */
193 bool capture_beacon;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +0200194 u8 capture_bssid[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100195 struct sk_buff *beacon_skb;
196
197 /*
198 * This FJ worker has to be global as it is scheduled from the
199 * RX handler. At this point we don't know which interface it
200 * belongs to until the list of bssids waiting to complete join
201 * is checked.
202 */
203 struct work_struct finalize_join_worker;
204
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +0100205 /* Tasklet to perform TX reclaim. */
206 struct tasklet_struct poll_tx_task;
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +0100207
208 /* Tasklet to perform RX. */
209 struct tasklet_struct poll_rx_task;
John W. Linville0d462bb2010-07-28 14:04:24 -0400210
211 /* Most recently reported noise in dBm */
212 s8 noise;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100213};
214
215/* Per interface specific private data */
216struct mwl8k_vif {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +0100217 struct list_head list;
218 struct ieee80211_vif *vif;
219
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100220 /* Firmware macid for this vif. */
221 int macid;
222
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +0100223 /* Non AMPDU sequence number assigned by driver. */
Lennert Buytenheka6804002010-01-04 21:55:42 +0100224 u16 seqno;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100225};
Lennert Buytenheka94cc972009-08-03 21:58:57 +0200226#define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100227
Lennert Buytenheka6804002010-01-04 21:55:42 +0100228struct mwl8k_sta {
229 /* Index into station database. Returned by UPDATE_STADB. */
230 u8 peer_id;
231};
232#define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
233
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100234static const struct ieee80211_channel mwl8k_channels_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100235 { .center_freq = 2412, .hw_value = 1, },
236 { .center_freq = 2417, .hw_value = 2, },
237 { .center_freq = 2422, .hw_value = 3, },
238 { .center_freq = 2427, .hw_value = 4, },
239 { .center_freq = 2432, .hw_value = 5, },
240 { .center_freq = 2437, .hw_value = 6, },
241 { .center_freq = 2442, .hw_value = 7, },
242 { .center_freq = 2447, .hw_value = 8, },
243 { .center_freq = 2452, .hw_value = 9, },
244 { .center_freq = 2457, .hw_value = 10, },
245 { .center_freq = 2462, .hw_value = 11, },
Lennert Buytenhek647ca6b2009-11-30 18:32:20 +0100246 { .center_freq = 2467, .hw_value = 12, },
247 { .center_freq = 2472, .hw_value = 13, },
248 { .center_freq = 2484, .hw_value = 14, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100249};
250
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100251static const struct ieee80211_rate mwl8k_rates_24[] = {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100252 { .bitrate = 10, .hw_value = 2, },
253 { .bitrate = 20, .hw_value = 4, },
254 { .bitrate = 55, .hw_value = 11, },
Lennert Buytenhek5dfd3e22009-10-22 20:20:29 +0200255 { .bitrate = 110, .hw_value = 22, },
256 { .bitrate = 220, .hw_value = 44, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100257 { .bitrate = 60, .hw_value = 12, },
258 { .bitrate = 90, .hw_value = 18, },
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100259 { .bitrate = 120, .hw_value = 24, },
260 { .bitrate = 180, .hw_value = 36, },
261 { .bitrate = 240, .hw_value = 48, },
262 { .bitrate = 360, .hw_value = 72, },
263 { .bitrate = 480, .hw_value = 96, },
264 { .bitrate = 540, .hw_value = 108, },
Lennert Buytenhek140eb5e2009-11-30 18:11:44 +0100265 { .bitrate = 720, .hw_value = 144, },
266};
267
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +0100268static const struct ieee80211_channel mwl8k_channels_50[] = {
269 { .center_freq = 5180, .hw_value = 36, },
270 { .center_freq = 5200, .hw_value = 40, },
271 { .center_freq = 5220, .hw_value = 44, },
272 { .center_freq = 5240, .hw_value = 48, },
273};
274
275static const struct ieee80211_rate mwl8k_rates_50[] = {
276 { .bitrate = 60, .hw_value = 12, },
277 { .bitrate = 90, .hw_value = 18, },
278 { .bitrate = 120, .hw_value = 24, },
279 { .bitrate = 180, .hw_value = 36, },
280 { .bitrate = 240, .hw_value = 48, },
281 { .bitrate = 360, .hw_value = 72, },
282 { .bitrate = 480, .hw_value = 96, },
283 { .bitrate = 540, .hw_value = 108, },
284 { .bitrate = 720, .hw_value = 144, },
285};
286
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100287/* Set or get info from Firmware */
288#define MWL8K_CMD_SET 0x0001
289#define MWL8K_CMD_GET 0x0000
290
291/* Firmware command codes */
292#define MWL8K_CMD_CODE_DNLD 0x0001
293#define MWL8K_CMD_GET_HW_SPEC 0x0003
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200294#define MWL8K_CMD_SET_HW_SPEC 0x0004
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100295#define MWL8K_CMD_MAC_MULTICAST_ADR 0x0010
296#define MWL8K_CMD_GET_STAT 0x0014
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200297#define MWL8K_CMD_RADIO_CONTROL 0x001c
298#define MWL8K_CMD_RF_TX_POWER 0x001e
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200299#define MWL8K_CMD_RF_ANTENNA 0x0020
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100300#define MWL8K_CMD_SET_BEACON 0x0100 /* per-vif */
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100301#define MWL8K_CMD_SET_PRE_SCAN 0x0107
302#define MWL8K_CMD_SET_POST_SCAN 0x0108
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200303#define MWL8K_CMD_SET_RF_CHANNEL 0x010a
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100304#define MWL8K_CMD_SET_AID 0x010d
305#define MWL8K_CMD_SET_RATE 0x0110
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200306#define MWL8K_CMD_SET_FINALIZE_JOIN 0x0111
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100307#define MWL8K_CMD_RTS_THRESHOLD 0x0113
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200308#define MWL8K_CMD_SET_SLOT 0x0114
309#define MWL8K_CMD_SET_EDCA_PARAMS 0x0115
310#define MWL8K_CMD_SET_WMM_MODE 0x0123
311#define MWL8K_CMD_MIMO_CONFIG 0x0125
312#define MWL8K_CMD_USE_FIXED_RATE 0x0126
313#define MWL8K_CMD_ENABLE_SNIFFER 0x0150
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100314#define MWL8K_CMD_SET_MAC_ADDR 0x0202 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200315#define MWL8K_CMD_SET_RATEADAPT_MODE 0x0203
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +0100316#define MWL8K_CMD_BSS_START 0x1100 /* per-vif */
317#define MWL8K_CMD_SET_NEW_STN 0x1111 /* per-vif */
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200318#define MWL8K_CMD_UPDATE_STADB 0x1123
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100319
John W. Linvilleb6037422010-07-20 13:55:00 -0400320static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100321{
John W. Linvilleb6037422010-07-20 13:55:00 -0400322 u16 command = le16_to_cpu(cmd);
323
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100324#define MWL8K_CMDNAME(x) case MWL8K_CMD_##x: do {\
325 snprintf(buf, bufsize, "%s", #x);\
326 return buf;\
327 } while (0)
John W. Linvilleb6037422010-07-20 13:55:00 -0400328 switch (command & ~0x8000) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100329 MWL8K_CMDNAME(CODE_DNLD);
330 MWL8K_CMDNAME(GET_HW_SPEC);
Lennert Buytenhek42fba212009-10-22 20:21:30 +0200331 MWL8K_CMDNAME(SET_HW_SPEC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100332 MWL8K_CMDNAME(MAC_MULTICAST_ADR);
333 MWL8K_CMDNAME(GET_STAT);
334 MWL8K_CMDNAME(RADIO_CONTROL);
335 MWL8K_CMDNAME(RF_TX_POWER);
Lennert Buytenhek08b06342009-10-22 20:21:33 +0200336 MWL8K_CMDNAME(RF_ANTENNA);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100337 MWL8K_CMDNAME(SET_BEACON);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100338 MWL8K_CMDNAME(SET_PRE_SCAN);
339 MWL8K_CMDNAME(SET_POST_SCAN);
340 MWL8K_CMDNAME(SET_RF_CHANNEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100341 MWL8K_CMDNAME(SET_AID);
342 MWL8K_CMDNAME(SET_RATE);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200343 MWL8K_CMDNAME(SET_FINALIZE_JOIN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100344 MWL8K_CMDNAME(RTS_THRESHOLD);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200345 MWL8K_CMDNAME(SET_SLOT);
346 MWL8K_CMDNAME(SET_EDCA_PARAMS);
347 MWL8K_CMDNAME(SET_WMM_MODE);
348 MWL8K_CMDNAME(MIMO_CONFIG);
349 MWL8K_CMDNAME(USE_FIXED_RATE);
350 MWL8K_CMDNAME(ENABLE_SNIFFER);
Lennert Buytenhek32060e12009-10-22 20:20:04 +0200351 MWL8K_CMDNAME(SET_MAC_ADDR);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200352 MWL8K_CMDNAME(SET_RATEADAPT_MODE);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +0100353 MWL8K_CMDNAME(BSS_START);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +0100354 MWL8K_CMDNAME(SET_NEW_STN);
Lennert Buytenhekff45fc62009-07-16 11:50:36 +0200355 MWL8K_CMDNAME(UPDATE_STADB);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100356 default:
357 snprintf(buf, bufsize, "0x%x", cmd);
358 }
359#undef MWL8K_CMDNAME
360
361 return buf;
362}
363
364/* Hardware and firmware reset */
365static void mwl8k_hw_reset(struct mwl8k_priv *priv)
366{
367 iowrite32(MWL8K_H2A_INT_RESET,
368 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
369 iowrite32(MWL8K_H2A_INT_RESET,
370 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
371 msleep(20);
372}
373
374/* Release fw image */
375static void mwl8k_release_fw(struct firmware **fw)
376{
377 if (*fw == NULL)
378 return;
379 release_firmware(*fw);
380 *fw = NULL;
381}
382
383static void mwl8k_release_firmware(struct mwl8k_priv *priv)
384{
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100385 mwl8k_release_fw(&priv->fw_ucode);
386 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100387}
388
389/* Request fw image */
390static int mwl8k_request_fw(struct mwl8k_priv *priv,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200391 const char *fname, struct firmware **fw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100392{
393 /* release current image */
394 if (*fw != NULL)
395 mwl8k_release_fw(fw);
396
397 return request_firmware((const struct firmware **)fw,
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200398 fname, &priv->pdev->dev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100399}
400
Lennert Buytenhek45a390d2009-10-22 20:20:47 +0200401static int mwl8k_request_firmware(struct mwl8k_priv *priv)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100402{
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200403 struct mwl8k_device_info *di = priv->device_info;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100404 int rc;
405
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200406 if (di->helper_image != NULL) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100407 rc = mwl8k_request_fw(priv, di->helper_image, &priv->fw_helper);
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200408 if (rc) {
409 printk(KERN_ERR "%s: Error requesting helper "
410 "firmware file %s\n", pci_name(priv->pdev),
411 di->helper_image);
412 return rc;
413 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100414 }
415
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100416 rc = mwl8k_request_fw(priv, di->fw_image, &priv->fw_ucode);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100417 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200418 printk(KERN_ERR "%s: Error requesting firmware file %s\n",
Lennert Buytenheka74b2952009-10-22 20:20:50 +0200419 pci_name(priv->pdev), di->fw_image);
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100420 mwl8k_release_fw(&priv->fw_helper);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100421 return rc;
422 }
423
424 return 0;
425}
426
427struct mwl8k_cmd_pkt {
428 __le16 code;
429 __le16 length;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100430 __u8 seq_num;
431 __u8 macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100432 __le16 result;
433 char payload[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000434} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100435
436/*
437 * Firmware loading.
438 */
439static int
440mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
441{
442 void __iomem *regs = priv->regs;
443 dma_addr_t dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100444 int loops;
445
446 dma_addr = pci_map_single(priv->pdev, data, length, PCI_DMA_TODEVICE);
447 if (pci_dma_mapping_error(priv->pdev, dma_addr))
448 return -ENOMEM;
449
450 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
451 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
452 iowrite32(MWL8K_H2A_INT_DOORBELL,
453 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
454 iowrite32(MWL8K_H2A_INT_DUMMY,
455 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
456
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100457 loops = 1000;
458 do {
459 u32 int_code;
460
461 int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
462 if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
463 iowrite32(0, regs + MWL8K_HIU_INT_CODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100464 break;
465 }
466
Lennert Buytenhek3d76e822009-10-22 20:20:16 +0200467 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100468 udelay(1);
469 } while (--loops);
470
471 pci_unmap_single(priv->pdev, dma_addr, length, PCI_DMA_TODEVICE);
472
Lennert Buytenhekd4b705702009-07-16 12:44:45 +0200473 return loops ? 0 : -ETIMEDOUT;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100474}
475
476static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
477 const u8 *data, size_t length)
478{
479 struct mwl8k_cmd_pkt *cmd;
480 int done;
481 int rc = 0;
482
483 cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
484 if (cmd == NULL)
485 return -ENOMEM;
486
487 cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
488 cmd->seq_num = 0;
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +0100489 cmd->macid = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100490 cmd->result = 0;
491
492 done = 0;
493 while (length) {
494 int block_size = length > 256 ? 256 : length;
495
496 memcpy(cmd->payload, data + done, block_size);
497 cmd->length = cpu_to_le16(block_size);
498
499 rc = mwl8k_send_fw_load_cmd(priv, cmd,
500 sizeof(*cmd) + block_size);
501 if (rc)
502 break;
503
504 done += block_size;
505 length -= block_size;
506 }
507
508 if (!rc) {
509 cmd->length = 0;
510 rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
511 }
512
513 kfree(cmd);
514
515 return rc;
516}
517
518static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
519 const u8 *data, size_t length)
520{
521 unsigned char *buffer;
522 int may_continue, rc = 0;
523 u32 done, prev_block_size;
524
525 buffer = kmalloc(1024, GFP_KERNEL);
526 if (buffer == NULL)
527 return -ENOMEM;
528
529 done = 0;
530 prev_block_size = 0;
531 may_continue = 1000;
532 while (may_continue > 0) {
533 u32 block_size;
534
535 block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
536 if (block_size & 1) {
537 block_size &= ~1;
538 may_continue--;
539 } else {
540 done += prev_block_size;
541 length -= prev_block_size;
542 }
543
544 if (block_size > 1024 || block_size > length) {
545 rc = -EOVERFLOW;
546 break;
547 }
548
549 if (length == 0) {
550 rc = 0;
551 break;
552 }
553
554 if (block_size == 0) {
555 rc = -EPROTO;
556 may_continue--;
557 udelay(1);
558 continue;
559 }
560
561 prev_block_size = block_size;
562 memcpy(buffer, data + done, block_size);
563
564 rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
565 if (rc)
566 break;
567 }
568
569 if (!rc && length != 0)
570 rc = -EREMOTEIO;
571
572 kfree(buffer);
573
574 return rc;
575}
576
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200577static int mwl8k_load_firmware(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100578{
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200579 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100580 struct firmware *fw = priv->fw_ucode;
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200581 int rc;
582 int loops;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100583
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200584 if (!memcmp(fw->data, "\x01\x00\x00\x00", 4)) {
Lennert Buytenhek22be40d2009-11-30 18:32:38 +0100585 struct firmware *helper = priv->fw_helper;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100586
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200587 if (helper == NULL) {
588 printk(KERN_ERR "%s: helper image needed but none "
589 "given\n", pci_name(priv->pdev));
590 return -EINVAL;
591 }
592
593 rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100594 if (rc) {
595 printk(KERN_ERR "%s: unable to load firmware "
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200596 "helper image\n", pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100597 return rc;
598 }
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100599 msleep(5);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100600
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200601 rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100602 } else {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200603 rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100604 }
605
606 if (rc) {
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +0200607 printk(KERN_ERR "%s: unable to load firmware image\n",
608 pci_name(priv->pdev));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100609 return rc;
610 }
611
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100612 iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100613
Lennert Buytenhek89b872e2009-11-30 18:13:20 +0100614 loops = 500000;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100615 do {
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200616 u32 ready_code;
617
618 ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
619 if (ready_code == MWL8K_FWAP_READY) {
620 priv->ap_fw = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100621 break;
Lennert Buytenhekeae74e62009-10-22 20:20:53 +0200622 } else if (ready_code == MWL8K_FWSTA_READY) {
623 priv->ap_fw = 0;
624 break;
625 }
626
627 cond_resched();
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100628 udelay(1);
629 } while (--loops);
630
631 return loops ? 0 : -ETIMEDOUT;
632}
633
634
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100635/* DMA header used by firmware and hardware. */
636struct mwl8k_dma_data {
637 __le16 fwlen;
638 struct ieee80211_hdr wh;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100639 char data[0];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000640} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100641
642/* Routines to add/remove DMA header from skb. */
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100643static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100644{
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100645 struct mwl8k_dma_data *tr;
646 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100647
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100648 tr = (struct mwl8k_dma_data *)skb->data;
649 hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
650
651 if (hdrlen != sizeof(tr->wh)) {
652 if (ieee80211_is_data_qos(tr->wh.frame_control)) {
653 memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
654 *((__le16 *)(tr->data - 2)) = qos;
655 } else {
656 memmove(tr->data - hdrlen, &tr->wh, hdrlen);
657 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100658 }
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100659
660 if (hdrlen != sizeof(*tr))
661 skb_pull(skb, sizeof(*tr) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100662}
663
Lennert Buytenhek76266b22009-07-16 11:07:09 +0200664static inline void mwl8k_add_dma_header(struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100665{
666 struct ieee80211_hdr *wh;
Lennert Buytenhekca009302009-11-30 18:12:20 +0100667 int hdrlen;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100668 struct mwl8k_dma_data *tr;
669
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100670 /*
Lennert Buytenhekca009302009-11-30 18:12:20 +0100671 * Add a firmware DMA header; the firmware requires that we
672 * present a 2-byte payload length followed by a 4-address
673 * header (without QoS field), followed (optionally) by any
674 * WEP/ExtIV header (but only filled in for CCMP).
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100675 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100676 wh = (struct ieee80211_hdr *)skb->data;
677
678 hdrlen = ieee80211_hdrlen(wh->frame_control);
679 if (hdrlen != sizeof(*tr))
680 skb_push(skb, sizeof(*tr) - hdrlen);
681
682 if (ieee80211_is_data_qos(wh->frame_control))
683 hdrlen -= 2;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100684
685 tr = (struct mwl8k_dma_data *)skb->data;
686 if (wh != &tr->wh)
687 memmove(&tr->wh, wh, hdrlen);
Lennert Buytenhekca009302009-11-30 18:12:20 +0100688 if (hdrlen != sizeof(tr->wh))
689 memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100690
691 /*
692 * Firmware length is the length of the fully formed "802.11
693 * payload". That is, everything except for the 802.11 header.
694 * This includes all crypto material including the MIC.
695 */
Lennert Buytenhekca009302009-11-30 18:12:20 +0100696 tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100697}
698
699
700/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100701 * Packet reception for 88w8366 AP firmware.
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200702 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100703struct mwl8k_rxd_8366_ap {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200704 __le16 pkt_len;
705 __u8 sq2;
706 __u8 rate;
707 __le32 pkt_phys_addr;
708 __le32 next_rxd_phys_addr;
709 __le16 qos_control;
710 __le16 htsig2;
711 __le32 hw_rssi_info;
712 __le32 hw_noise_floor_info;
713 __u8 noise_floor;
714 __u8 pad0[3];
715 __u8 rssi;
716 __u8 rx_status;
717 __u8 channel;
718 __u8 rx_ctrl;
Eric Dumazetba2d3582010-06-02 18:10:09 +0000719} __packed;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200720
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100721#define MWL8K_8366_AP_RATE_INFO_MCS_FORMAT 0x80
722#define MWL8K_8366_AP_RATE_INFO_40MHZ 0x40
723#define MWL8K_8366_AP_RATE_INFO_RATEID(x) ((x) & 0x3f)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100724
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100725#define MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST 0x80
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200726
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100727static void mwl8k_rxd_8366_ap_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200728{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100729 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200730
731 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100732 rxd->rx_ctrl = MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200733}
734
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100735static void mwl8k_rxd_8366_ap_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200736{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100737 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200738
739 rxd->pkt_len = cpu_to_le16(len);
740 rxd->pkt_phys_addr = cpu_to_le32(addr);
741 wmb();
742 rxd->rx_ctrl = 0;
743}
744
745static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100746mwl8k_rxd_8366_ap_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400747 __le16 *qos, s8 *noise)
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200748{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100749 struct mwl8k_rxd_8366_ap *rxd = _rxd;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200750
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100751 if (!(rxd->rx_ctrl & MWL8K_8366_AP_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200752 return -1;
753 rmb();
754
755 memset(status, 0, sizeof(*status));
756
757 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400758 *noise = -rxd->noise_floor;
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200759
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100760 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_MCS_FORMAT) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200761 status->flag |= RX_FLAG_HT;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100762 if (rxd->rate & MWL8K_8366_AP_RATE_INFO_40MHZ)
Lennert Buytenhek8e9f33f2009-11-30 18:12:35 +0100763 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100764 status->rate_idx = MWL8K_8366_AP_RATE_INFO_RATEID(rxd->rate);
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200765 } else {
766 int i;
767
Lennert Buytenhek777ad372010-01-12 13:48:04 +0100768 for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
769 if (mwl8k_rates_24[i].hw_value == rxd->rate) {
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200770 status->rate_idx = i;
771 break;
772 }
773 }
774 }
775
Lennert Buytenhek85478342010-01-12 13:48:56 +0100776 if (rxd->channel > 14) {
777 status->band = IEEE80211_BAND_5GHZ;
778 if (!(status->flag & RX_FLAG_HT))
779 status->rate_idx -= 5;
780 } else {
781 status->band = IEEE80211_BAND_2GHZ;
782 }
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200783 status->freq = ieee80211_channel_to_frequency(rxd->channel);
784
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100785 *qos = rxd->qos_control;
786
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200787 return le16_to_cpu(rxd->pkt_len);
788}
789
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100790static struct rxd_ops rxd_8366_ap_ops = {
791 .rxd_size = sizeof(struct mwl8k_rxd_8366_ap),
792 .rxd_init = mwl8k_rxd_8366_ap_init,
793 .rxd_refill = mwl8k_rxd_8366_ap_refill,
794 .rxd_process = mwl8k_rxd_8366_ap_process,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +0200795};
796
797/*
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100798 * Packet reception for STA firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100799 */
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100800struct mwl8k_rxd_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100801 __le16 pkt_len;
802 __u8 link_quality;
803 __u8 noise_level;
804 __le32 pkt_phys_addr;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200805 __le32 next_rxd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100806 __le16 qos_control;
807 __le16 rate_info;
808 __le32 pad0[4];
809 __u8 rssi;
810 __u8 channel;
811 __le16 pad1;
812 __u8 rx_ctrl;
813 __u8 rx_status;
814 __u8 pad2[2];
Eric Dumazetba2d3582010-06-02 18:10:09 +0000815} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100816
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100817#define MWL8K_STA_RATE_INFO_SHORTPRE 0x8000
818#define MWL8K_STA_RATE_INFO_ANTSELECT(x) (((x) >> 11) & 0x3)
819#define MWL8K_STA_RATE_INFO_RATEID(x) (((x) >> 3) & 0x3f)
820#define MWL8K_STA_RATE_INFO_40MHZ 0x0004
821#define MWL8K_STA_RATE_INFO_SHORTGI 0x0002
822#define MWL8K_STA_RATE_INFO_MCS_FORMAT 0x0001
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200823
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100824#define MWL8K_STA_RX_CTRL_OWNED_BY_HOST 0x02
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200825
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100826static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200827{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100828 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200829
830 rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100831 rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200832}
833
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100834static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200835{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100836 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200837
838 rxd->pkt_len = cpu_to_le16(len);
839 rxd->pkt_phys_addr = cpu_to_le32(addr);
840 wmb();
841 rxd->rx_ctrl = 0;
842}
843
844static int
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100845mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
John W. Linville0d462bb2010-07-28 14:04:24 -0400846 __le16 *qos, s8 *noise)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200847{
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100848 struct mwl8k_rxd_sta *rxd = _rxd;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200849 u16 rate_info;
850
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100851 if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200852 return -1;
853 rmb();
854
855 rate_info = le16_to_cpu(rxd->rate_info);
856
857 memset(status, 0, sizeof(*status));
858
859 status->signal = -rxd->rssi;
John W. Linville0d462bb2010-07-28 14:04:24 -0400860 *noise = -rxd->noise_level;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100861 status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
862 status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200863
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100864 if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200865 status->flag |= RX_FLAG_SHORTPRE;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100866 if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200867 status->flag |= RX_FLAG_40MHZ;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100868 if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200869 status->flag |= RX_FLAG_SHORT_GI;
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100870 if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200871 status->flag |= RX_FLAG_HT;
872
Lennert Buytenhek85478342010-01-12 13:48:56 +0100873 if (rxd->channel > 14) {
874 status->band = IEEE80211_BAND_5GHZ;
875 if (!(status->flag & RX_FLAG_HT))
876 status->rate_idx -= 5;
877 } else {
878 status->band = IEEE80211_BAND_2GHZ;
879 }
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200880 status->freq = ieee80211_channel_to_frequency(rxd->channel);
881
Lennert Buytenhek20f09c32009-11-30 18:12:08 +0100882 *qos = rxd->qos_control;
883
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200884 return le16_to_cpu(rxd->pkt_len);
885}
886
Lennert Buytenhek89a91f42009-11-30 18:32:54 +0100887static struct rxd_ops rxd_sta_ops = {
888 .rxd_size = sizeof(struct mwl8k_rxd_sta),
889 .rxd_init = mwl8k_rxd_sta_init,
890 .rxd_refill = mwl8k_rxd_sta_refill,
891 .rxd_process = mwl8k_rxd_sta_process,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200892};
893
894
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100895#define MWL8K_RX_DESCS 256
896#define MWL8K_RX_MAXSZ 3800
897
898static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
899{
900 struct mwl8k_priv *priv = hw->priv;
901 struct mwl8k_rx_queue *rxq = priv->rxq + index;
902 int size;
903 int i;
904
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200905 rxq->rxd_count = 0;
906 rxq->head = 0;
907 rxq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100908
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200909 size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100910
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200911 rxq->rxd = pci_alloc_consistent(priv->pdev, size, &rxq->rxd_dma);
912 if (rxq->rxd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -0700913 wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100914 return -ENOMEM;
915 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200916 memset(rxq->rxd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100917
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200918 rxq->buf = kmalloc(MWL8K_RX_DESCS * sizeof(*rxq->buf), GFP_KERNEL);
919 if (rxq->buf == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -0700920 wiphy_err(hw->wiphy, "failed to alloc RX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200921 pci_free_consistent(priv->pdev, size, rxq->rxd, rxq->rxd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100922 return -ENOMEM;
923 }
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200924 memset(rxq->buf, 0, MWL8K_RX_DESCS * sizeof(*rxq->buf));
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100925
926 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200927 int desc_size;
928 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100929 int nexti;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200930 dma_addr_t next_dma_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100931
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200932 desc_size = priv->rxd_ops->rxd_size;
933 rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100934
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200935 nexti = i + 1;
936 if (nexti == MWL8K_RX_DESCS)
937 nexti = 0;
938 next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
939
940 priv->rxd_ops->rxd_init(rxd, next_dma_addr);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100941 }
942
943 return 0;
944}
945
946static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
947{
948 struct mwl8k_priv *priv = hw->priv;
949 struct mwl8k_rx_queue *rxq = priv->rxq + index;
950 int refilled;
951
952 refilled = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +0200953 while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100954 struct sk_buff *skb;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200955 dma_addr_t addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100956 int rx;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200957 void *rxd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100958
959 skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
960 if (skb == NULL)
961 break;
962
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200963 addr = pci_map_single(priv->pdev, skb->data,
964 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100965
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200966 rxq->rxd_count++;
967 rx = rxq->tail++;
968 if (rxq->tail == MWL8K_RX_DESCS)
969 rxq->tail = 0;
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200970 rxq->buf[rx].skb = skb;
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900971 dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +0200972
973 rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
974 priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100975
976 refilled++;
977 }
978
979 return refilled;
980}
981
982/* Must be called only when the card's reception is completely halted */
983static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
984{
985 struct mwl8k_priv *priv = hw->priv;
986 struct mwl8k_rx_queue *rxq = priv->rxq + index;
987 int i;
988
989 for (i = 0; i < MWL8K_RX_DESCS; i++) {
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200990 if (rxq->buf[i].skb != NULL) {
991 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900992 dma_unmap_addr(&rxq->buf[i], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200993 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +0900994 dma_unmap_addr_set(&rxq->buf[i], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100995
Lennert Buytenhek788838e2009-10-22 20:20:56 +0200996 kfree_skb(rxq->buf[i].skb);
997 rxq->buf[i].skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +0100998 }
999 }
1000
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001001 kfree(rxq->buf);
1002 rxq->buf = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001003
1004 pci_free_consistent(priv->pdev,
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001005 MWL8K_RX_DESCS * priv->rxd_ops->rxd_size,
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001006 rxq->rxd, rxq->rxd_dma);
1007 rxq->rxd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001008}
1009
1010
1011/*
1012 * Scan a list of BSSIDs to process for finalize join.
1013 * Allows for extension to process multiple BSSIDs.
1014 */
1015static inline int
1016mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1017{
1018 return priv->capture_beacon &&
1019 ieee80211_is_beacon(wh->frame_control) &&
1020 !compare_ether_addr(wh->addr3, priv->capture_bssid);
1021}
1022
Lennert Buytenhek37797522009-10-22 20:19:45 +02001023static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1024 struct sk_buff *skb)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001025{
Lennert Buytenhek37797522009-10-22 20:19:45 +02001026 struct mwl8k_priv *priv = hw->priv;
1027
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001028 priv->capture_beacon = false;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001029 memset(priv->capture_bssid, 0, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001030
1031 /*
1032 * Use GFP_ATOMIC as rxq_process is called from
1033 * the primary interrupt handler, memory allocation call
1034 * must not sleep.
1035 */
1036 priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1037 if (priv->beacon_skb != NULL)
Lennert Buytenhek37797522009-10-22 20:19:45 +02001038 ieee80211_queue_work(hw, &priv->finalize_join_worker);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001039}
1040
1041static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1042{
1043 struct mwl8k_priv *priv = hw->priv;
1044 struct mwl8k_rx_queue *rxq = priv->rxq + index;
1045 int processed;
1046
1047 processed = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001048 while (rxq->rxd_count && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001049 struct sk_buff *skb;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001050 void *rxd;
1051 int pkt_len;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001052 struct ieee80211_rx_status status;
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001053 __le16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001054
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001055 skb = rxq->buf[rxq->head].skb;
Lennert Buytenhekd25f9f12009-08-03 21:58:26 +02001056 if (skb == NULL)
1057 break;
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001058
1059 rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1060
John W. Linville0d462bb2010-07-28 14:04:24 -04001061 pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1062 &priv->noise);
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001063 if (pkt_len < 0)
1064 break;
1065
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001066 rxq->buf[rxq->head].skb = NULL;
1067
1068 pci_unmap_single(priv->pdev,
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001069 dma_unmap_addr(&rxq->buf[rxq->head], dma),
Lennert Buytenhek788838e2009-10-22 20:20:56 +02001070 MWL8K_RX_MAXSZ, PCI_DMA_FROMDEVICE);
FUJITA Tomonori53b1b3e2010-04-02 13:10:38 +09001071 dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001072
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001073 rxq->head++;
1074 if (rxq->head == MWL8K_RX_DESCS)
1075 rxq->head = 0;
1076
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001077 rxq->rxd_count--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001078
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001079 skb_put(skb, pkt_len);
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001080 mwl8k_remove_dma_header(skb, qos);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001081
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001082 /*
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02001083 * Check for a pending join operation. Save a
1084 * copy of the beacon and schedule a tasklet to
1085 * send a FINALIZE_JOIN command to the firmware.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001086 */
Lennert Buytenhek54bc3a02009-10-22 20:20:59 +02001087 if (mwl8k_capture_bssid(priv, (void *)skb->data))
Lennert Buytenhek37797522009-10-22 20:19:45 +02001088 mwl8k_save_beacon(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001089
Johannes Bergf1d58c22009-06-17 13:13:00 +02001090 memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1091 ieee80211_rx_irqsafe(hw, skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001092
1093 processed++;
1094 }
1095
1096 return processed;
1097}
1098
1099
1100/*
1101 * Packet transmission.
1102 */
1103
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001104#define MWL8K_TXD_STATUS_OK 0x00000001
1105#define MWL8K_TXD_STATUS_OK_RETRY 0x00000002
1106#define MWL8K_TXD_STATUS_OK_MORE_RETRY 0x00000004
1107#define MWL8K_TXD_STATUS_MULTICAST_TX 0x00000008
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001108#define MWL8K_TXD_STATUS_FW_OWNED 0x80000000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001109
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001110#define MWL8K_QOS_QLEN_UNSPEC 0xff00
1111#define MWL8K_QOS_ACK_POLICY_MASK 0x0060
1112#define MWL8K_QOS_ACK_POLICY_NORMAL 0x0000
1113#define MWL8K_QOS_ACK_POLICY_BLOCKACK 0x0060
1114#define MWL8K_QOS_EOSP 0x0010
1115
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001116struct mwl8k_tx_desc {
1117 __le32 status;
1118 __u8 data_rate;
1119 __u8 tx_priority;
1120 __le16 qos_control;
1121 __le32 pkt_phys_addr;
1122 __le16 pkt_len;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001123 __u8 dest_MAC_addr[ETH_ALEN];
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001124 __le32 next_txd_phys_addr;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001125 __le32 reserved;
1126 __le16 rate_info;
1127 __u8 peer_id;
Nishant Sarmukadamc0bf9ca2010-11-01 17:55:48 -07001128 __u8 xmitcontrol;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001129} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001130
1131#define MWL8K_TX_DESCS 128
Nishant Sarmukadamc0bf9ca2010-11-01 17:55:48 -07001132#define MWL8K_XMITCONTROL_NON_AMPDU 0x04
1133
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001134
1135static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1136{
1137 struct mwl8k_priv *priv = hw->priv;
1138 struct mwl8k_tx_queue *txq = priv->txq + index;
1139 int size;
1140 int i;
1141
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001142 txq->len = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001143 txq->head = 0;
1144 txq->tail = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001145
1146 size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1147
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001148 txq->txd = pci_alloc_consistent(priv->pdev, size, &txq->txd_dma);
1149 if (txq->txd == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001150 wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001151 return -ENOMEM;
1152 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001153 memset(txq->txd, 0, size);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001154
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001155 txq->skb = kmalloc(MWL8K_TX_DESCS * sizeof(*txq->skb), GFP_KERNEL);
1156 if (txq->skb == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07001157 wiphy_err(hw->wiphy, "failed to alloc TX skbuff list\n");
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001158 pci_free_consistent(priv->pdev, size, txq->txd, txq->txd_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001159 return -ENOMEM;
1160 }
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001161 memset(txq->skb, 0, MWL8K_TX_DESCS * sizeof(*txq->skb));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001162
1163 for (i = 0; i < MWL8K_TX_DESCS; i++) {
1164 struct mwl8k_tx_desc *tx_desc;
1165 int nexti;
1166
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001167 tx_desc = txq->txd + i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001168 nexti = (i + 1) % MWL8K_TX_DESCS;
1169
1170 tx_desc->status = 0;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001171 tx_desc->next_txd_phys_addr =
1172 cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001173 }
1174
1175 return 0;
1176}
1177
1178static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1179{
1180 iowrite32(MWL8K_H2A_INT_PPA_READY,
1181 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1182 iowrite32(MWL8K_H2A_INT_DUMMY,
1183 priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1184 ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1185}
1186
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001187static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001188{
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001189 struct mwl8k_priv *priv = hw->priv;
1190 int i;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001191
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001192 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
1193 struct mwl8k_tx_queue *txq = priv->txq + i;
1194 int fw_owned = 0;
1195 int drv_owned = 0;
1196 int unused = 0;
1197 int desc;
Lennert Buytenhekc3f967d2009-08-18 04:15:22 +02001198
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001199 for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001200 struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1201 u32 status;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001202
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001203 status = le32_to_cpu(tx_desc->status);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001204 if (status & MWL8K_TXD_STATUS_FW_OWNED)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001205 fw_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001206 else
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001207 drv_owned++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001208
1209 if (tx_desc->pkt_len == 0)
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001210 unused++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001211 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001212
Joe Perchesc96c31e2010-07-26 14:39:58 -07001213 wiphy_err(hw->wiphy,
1214 "txq[%d] len=%d head=%d tail=%d "
1215 "fw_owned=%d drv_owned=%d unused=%d\n",
1216 i,
1217 txq->len, txq->head, txq->tail,
1218 fw_owned, drv_owned, unused);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001219 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001220}
1221
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001222/*
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001223 * Must be called with priv->fw_mutex held and tx queues stopped.
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001224 */
Lennert Buytenhek62abd3c2010-01-08 18:28:34 +01001225#define MWL8K_TX_WAIT_TIMEOUT_MS 5000
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001226
Lennert Buytenhek950d5b02009-07-17 01:48:41 +02001227static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001228{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001229 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02001230 DECLARE_COMPLETION_ONSTACK(tx_wait);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001231 int retry;
1232 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001233
1234 might_sleep();
1235
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001236 /*
1237 * The TX queues are stopped at this point, so this test
1238 * doesn't need to take ->tx_lock.
1239 */
1240 if (!priv->pending_tx_pkts)
1241 return 0;
1242
1243 retry = 0;
1244 rc = 0;
1245
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001246 spin_lock_bh(&priv->tx_lock);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001247 priv->tx_wait = &tx_wait;
1248 while (!rc) {
1249 int oldcount;
1250 unsigned long timeout;
1251
1252 oldcount = priv->pending_tx_pkts;
1253
1254 spin_unlock_bh(&priv->tx_lock);
1255 timeout = wait_for_completion_timeout(&tx_wait,
1256 msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1257 spin_lock_bh(&priv->tx_lock);
1258
1259 if (timeout) {
1260 WARN_ON(priv->pending_tx_pkts);
1261 if (retry) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001262 wiphy_notice(hw->wiphy, "tx rings drained\n");
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001263 }
1264 break;
1265 }
1266
1267 if (priv->pending_tx_pkts < oldcount) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001268 wiphy_notice(hw->wiphy,
1269 "waiting for tx rings to drain (%d -> %d pkts)\n",
1270 oldcount, priv->pending_tx_pkts);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001271 retry = 1;
1272 continue;
1273 }
1274
1275 priv->tx_wait = NULL;
1276
Joe Perchesc96c31e2010-07-26 14:39:58 -07001277 wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1278 MWL8K_TX_WAIT_TIMEOUT_MS);
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001279 mwl8k_dump_tx_rings(hw);
1280
1281 rc = -ETIMEDOUT;
1282 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001283 spin_unlock_bh(&priv->tx_lock);
1284
Lennert Buytenhek7e1112d2009-11-30 18:13:04 +01001285 return rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001286}
1287
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02001288#define MWL8K_TXD_SUCCESS(status) \
1289 ((status) & (MWL8K_TXD_STATUS_OK | \
1290 MWL8K_TXD_STATUS_OK_RETRY | \
1291 MWL8K_TXD_STATUS_OK_MORE_RETRY))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001292
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001293static int
1294mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001295{
1296 struct mwl8k_priv *priv = hw->priv;
1297 struct mwl8k_tx_queue *txq = priv->txq + index;
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001298 int processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001299
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001300 processed = 0;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001301 while (txq->len > 0 && limit--) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001302 int tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001303 struct mwl8k_tx_desc *tx_desc;
1304 unsigned long addr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001305 int size;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001306 struct sk_buff *skb;
1307 struct ieee80211_tx_info *info;
1308 u32 status;
1309
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001310 tx = txq->head;
1311 tx_desc = txq->txd + tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001312
1313 status = le32_to_cpu(tx_desc->status);
1314
1315 if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1316 if (!force)
1317 break;
1318 tx_desc->status &=
1319 ~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1320 }
1321
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001322 txq->head = (tx + 1) % MWL8K_TX_DESCS;
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001323 BUG_ON(txq->len == 0);
1324 txq->len--;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001325 priv->pending_tx_pkts--;
1326
1327 addr = le32_to_cpu(tx_desc->pkt_phys_addr);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001328 size = le16_to_cpu(tx_desc->pkt_len);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001329 skb = txq->skb[tx];
1330 txq->skb[tx] = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001331
1332 BUG_ON(skb == NULL);
1333 pci_unmap_single(priv->pdev, addr, size, PCI_DMA_TODEVICE);
1334
Lennert Buytenhek20f09c32009-11-30 18:12:08 +01001335 mwl8k_remove_dma_header(skb, tx_desc->qos_control);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001336
1337 /* Mark descriptor as unused */
1338 tx_desc->pkt_phys_addr = 0;
1339 tx_desc->pkt_len = 0;
1340
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001341 info = IEEE80211_SKB_CB(skb);
1342 ieee80211_tx_info_clear_status(info);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001343 if (MWL8K_TXD_SUCCESS(status))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001344 info->flags |= IEEE80211_TX_STAT_ACK;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001345
1346 ieee80211_tx_status_irqsafe(hw, skb);
1347
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001348 processed++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001349 }
1350
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001351 if (processed && priv->radio_on && !mutex_is_locked(&priv->fw_mutex))
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001352 ieee80211_wake_queue(hw, index);
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001353
1354 return processed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001355}
1356
1357/* must be called only when the card's transmit is completely halted */
1358static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1359{
1360 struct mwl8k_priv *priv = hw->priv;
1361 struct mwl8k_tx_queue *txq = priv->txq + index;
1362
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01001363 mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001364
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001365 kfree(txq->skb);
1366 txq->skb = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001367
1368 pci_free_consistent(priv->pdev,
1369 MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001370 txq->txd, txq->txd_dma);
1371 txq->txd = NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001372}
1373
1374static int
1375mwl8k_txq_xmit(struct ieee80211_hw *hw, int index, struct sk_buff *skb)
1376{
1377 struct mwl8k_priv *priv = hw->priv;
1378 struct ieee80211_tx_info *tx_info;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001379 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001380 struct ieee80211_hdr *wh;
1381 struct mwl8k_tx_queue *txq;
1382 struct mwl8k_tx_desc *tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001383 dma_addr_t dma;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001384 u32 txstatus;
1385 u8 txdatarate;
1386 u16 qos;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001387
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001388 wh = (struct ieee80211_hdr *)skb->data;
1389 if (ieee80211_is_data_qos(wh->frame_control))
1390 qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1391 else
1392 qos = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001393
Lennert Buytenhek76266b22009-07-16 11:07:09 +02001394 mwl8k_add_dma_header(skb);
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001395 wh = &((struct mwl8k_dma_data *)skb->data)->wh;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001396
1397 tx_info = IEEE80211_SKB_CB(skb);
1398 mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001399
1400 if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001401 wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
Lennert Buytenhek657232b2010-01-12 13:47:47 +01001402 wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1403 mwl8k_vif->seqno += 0x10;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001404 }
1405
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001406 /* Setup firmware control bit fields for each frame type. */
1407 txstatus = 0;
1408 txdatarate = 0;
1409 if (ieee80211_is_mgmt(wh->frame_control) ||
1410 ieee80211_is_ctl(wh->frame_control)) {
1411 txdatarate = 0;
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001412 qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001413 } else if (ieee80211_is_data(wh->frame_control)) {
1414 txdatarate = 1;
1415 if (is_multicast_ether_addr(wh->addr1))
1416 txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1417
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001418 qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001419 if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001420 qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001421 else
Lennert Buytenheke0493a82009-11-30 18:32:00 +01001422 qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001423 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001424
1425 dma = pci_map_single(priv->pdev, skb->data,
1426 skb->len, PCI_DMA_TODEVICE);
1427
1428 if (pci_dma_mapping_error(priv->pdev, dma)) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07001429 wiphy_debug(hw->wiphy,
1430 "failed to dma map skb, dropping TX frame.\n");
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001431 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001432 return NETDEV_TX_OK;
1433 }
1434
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001435 spin_lock_bh(&priv->tx_lock);
1436
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001437 txq = priv->txq + index;
1438
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001439 BUG_ON(txq->skb[txq->tail] != NULL);
1440 txq->skb[txq->tail] = skb;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001441
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001442 tx = txq->txd + txq->tail;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001443 tx->data_rate = txdatarate;
1444 tx->tx_priority = index;
1445 tx->qos_control = cpu_to_le16(qos);
1446 tx->pkt_phys_addr = cpu_to_le32(dma);
1447 tx->pkt_len = cpu_to_le16(skb->len);
1448 tx->rate_info = 0;
Lennert Buytenheka6804002010-01-04 21:55:42 +01001449 if (!priv->ap_fw && tx_info->control.sta != NULL)
1450 tx->peer_id = MWL8K_STA(tx_info->control.sta)->peer_id;
1451 else
1452 tx->peer_id = 0;
Nishant Sarmukadamc0bf9ca2010-11-01 17:55:48 -07001453
1454 if (priv->ap_fw)
1455 tx->xmitcontrol = MWL8K_XMITCONTROL_NON_AMPDU;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001456 wmb();
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001457 tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
1458
Kalle Valo8ccbc3b2010-02-07 10:20:52 +02001459 txq->len++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001460 priv->pending_tx_pkts++;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001461
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001462 txq->tail++;
1463 if (txq->tail == MWL8K_TX_DESCS)
1464 txq->tail = 0;
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001465
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001466 if (txq->head == txq->tail)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001467 ieee80211_stop_queue(hw, index);
1468
Lennert Buytenhek23b33902009-07-17 05:21:04 +02001469 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001470
1471 spin_unlock_bh(&priv->tx_lock);
1472
1473 return NETDEV_TX_OK;
1474}
1475
1476
1477/*
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001478 * Firmware access.
1479 *
1480 * We have the following requirements for issuing firmware commands:
1481 * - Some commands require that the packet transmit path is idle when
1482 * the command is issued. (For simplicity, we'll just quiesce the
1483 * transmit path for every command.)
1484 * - There are certain sequences of commands that need to be issued to
1485 * the hardware sequentially, with no other intervening commands.
1486 *
1487 * This leads to an implementation of a "firmware lock" as a mutex that
1488 * can be taken recursively, and which is taken by both the low-level
1489 * command submission function (mwl8k_post_cmd) as well as any users of
1490 * that function that require issuing of an atomic sequence of commands,
1491 * and quiesces the transmit path whenever it's taken.
1492 */
1493static int mwl8k_fw_lock(struct ieee80211_hw *hw)
1494{
1495 struct mwl8k_priv *priv = hw->priv;
1496
1497 if (priv->fw_mutex_owner != current) {
1498 int rc;
1499
1500 mutex_lock(&priv->fw_mutex);
1501 ieee80211_stop_queues(hw);
1502
1503 rc = mwl8k_tx_wait_empty(hw);
1504 if (rc) {
1505 ieee80211_wake_queues(hw);
1506 mutex_unlock(&priv->fw_mutex);
1507
1508 return rc;
1509 }
1510
1511 priv->fw_mutex_owner = current;
1512 }
1513
1514 priv->fw_mutex_depth++;
1515
1516 return 0;
1517}
1518
1519static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
1520{
1521 struct mwl8k_priv *priv = hw->priv;
1522
1523 if (!--priv->fw_mutex_depth) {
1524 ieee80211_wake_queues(hw);
1525 priv->fw_mutex_owner = NULL;
1526 mutex_unlock(&priv->fw_mutex);
1527 }
1528}
1529
1530
1531/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001532 * Command processing.
1533 */
1534
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001535/* Timeout firmware commands after 10s */
1536#define MWL8K_CMD_TIMEOUT_MS 10000
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001537
1538static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt *cmd)
1539{
1540 DECLARE_COMPLETION_ONSTACK(cmd_wait);
1541 struct mwl8k_priv *priv = hw->priv;
1542 void __iomem *regs = priv->regs;
1543 dma_addr_t dma_addr;
1544 unsigned int dma_size;
1545 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001546 unsigned long timeout = 0;
1547 u8 buf[32];
1548
John W. Linvilleb6037422010-07-20 13:55:00 -04001549 cmd->result = (__force __le16) 0xffff;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001550 dma_size = le16_to_cpu(cmd->length);
1551 dma_addr = pci_map_single(priv->pdev, cmd, dma_size,
1552 PCI_DMA_BIDIRECTIONAL);
1553 if (pci_dma_mapping_error(priv->pdev, dma_addr))
1554 return -ENOMEM;
1555
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001556 rc = mwl8k_fw_lock(hw);
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001557 if (rc) {
1558 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1559 PCI_DMA_BIDIRECTIONAL);
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001560 return rc;
Lennert Buytenhek39a1e422009-08-24 15:42:46 +02001561 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001562
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001563 priv->hostcmd_wait = &cmd_wait;
1564 iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
1565 iowrite32(MWL8K_H2A_INT_DOORBELL,
1566 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1567 iowrite32(MWL8K_H2A_INT_DUMMY,
1568 regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001569
1570 timeout = wait_for_completion_timeout(&cmd_wait,
1571 msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
1572
Lennert Buytenhek618952a2009-08-18 03:18:01 +02001573 priv->hostcmd_wait = NULL;
1574
1575 mwl8k_fw_unlock(hw);
1576
Lennert Buytenhek37055bd2009-08-03 21:58:47 +02001577 pci_unmap_single(priv->pdev, dma_addr, dma_size,
1578 PCI_DMA_BIDIRECTIONAL);
1579
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001580 if (!timeout) {
Joe Perches5db55842010-08-11 19:11:19 -07001581 wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001582 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1583 MWL8K_CMD_TIMEOUT_MS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001584 rc = -ETIMEDOUT;
1585 } else {
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001586 int ms;
1587
1588 ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
1589
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001590 rc = cmd->result ? -EINVAL : 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001591 if (rc)
Joe Perches5db55842010-08-11 19:11:19 -07001592 wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001593 mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
1594 le16_to_cpu(cmd->result));
Lennert Buytenhek0c9cc642009-11-30 18:12:49 +01001595 else if (ms > 2000)
Joe Perches5db55842010-08-11 19:11:19 -07001596 wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
Joe Perchesc96c31e2010-07-26 14:39:58 -07001597 mwl8k_cmd_name(cmd->code,
1598 buf, sizeof(buf)),
1599 ms);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001600 }
1601
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001602 return rc;
1603}
1604
Lennert Buytenhekf57ca9c2010-01-12 13:50:14 +01001605static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
1606 struct ieee80211_vif *vif,
1607 struct mwl8k_cmd_pkt *cmd)
1608{
1609 if (vif != NULL)
1610 cmd->macid = MWL8K_VIF(vif)->macid;
1611 return mwl8k_post_cmd(hw, cmd);
1612}
1613
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001614/*
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001615 * Setup code shared between STA and AP firmware images.
1616 */
1617static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
1618{
1619 struct mwl8k_priv *priv = hw->priv;
1620
1621 BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
1622 memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
1623
1624 BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
1625 memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
1626
1627 priv->band_24.band = IEEE80211_BAND_2GHZ;
1628 priv->band_24.channels = priv->channels_24;
1629 priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
1630 priv->band_24.bitrates = priv->rates_24;
1631 priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
1632
1633 hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->band_24;
1634}
1635
Lennert Buytenhek4eae9ed2010-01-12 13:48:32 +01001636static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
1637{
1638 struct mwl8k_priv *priv = hw->priv;
1639
1640 BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
1641 memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
1642
1643 BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
1644 memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
1645
1646 priv->band_50.band = IEEE80211_BAND_5GHZ;
1647 priv->band_50.channels = priv->channels_50;
1648 priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
1649 priv->band_50.bitrates = priv->rates_50;
1650 priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
1651
1652 hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->band_50;
1653}
1654
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001655/*
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001656 * CMD_GET_HW_SPEC (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001657 */
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001658struct mwl8k_cmd_get_hw_spec_sta {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001659 struct mwl8k_cmd_pkt header;
1660 __u8 hw_rev;
1661 __u8 host_interface;
1662 __le16 num_mcaddrs;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02001663 __u8 perm_addr[ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001664 __le16 region_code;
1665 __le32 fw_rev;
1666 __le32 ps_cookie;
1667 __le32 caps;
1668 __u8 mcs_bitmap[16];
1669 __le32 rx_queue_ptr;
1670 __le32 num_tx_queues;
1671 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1672 __le32 caps2;
1673 __le32 num_tx_desc_per_queue;
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001674 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001675} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001676
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001677#define MWL8K_CAP_MAX_AMSDU 0x20000000
1678#define MWL8K_CAP_GREENFIELD 0x08000000
1679#define MWL8K_CAP_AMPDU 0x04000000
1680#define MWL8K_CAP_RX_STBC 0x01000000
1681#define MWL8K_CAP_TX_STBC 0x00800000
1682#define MWL8K_CAP_SHORTGI_40MHZ 0x00400000
1683#define MWL8K_CAP_SHORTGI_20MHZ 0x00200000
1684#define MWL8K_CAP_RX_ANTENNA_MASK 0x000e0000
1685#define MWL8K_CAP_TX_ANTENNA_MASK 0x0001c000
1686#define MWL8K_CAP_DELAY_BA 0x00003000
1687#define MWL8K_CAP_MIMO 0x00000200
1688#define MWL8K_CAP_40MHZ 0x00000100
Lennert Buytenhek06953232010-01-12 13:49:41 +01001689#define MWL8K_CAP_BAND_MASK 0x00000007
1690#define MWL8K_CAP_5GHZ 0x00000004
1691#define MWL8K_CAP_2GHZ4 0x00000001
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001692
Lennert Buytenhek06953232010-01-12 13:49:41 +01001693static void
1694mwl8k_set_ht_caps(struct ieee80211_hw *hw,
1695 struct ieee80211_supported_band *band, u32 cap)
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001696{
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001697 int rx_streams;
1698 int tx_streams;
1699
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001700 band->ht_cap.ht_supported = 1;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001701
1702 if (cap & MWL8K_CAP_MAX_AMSDU)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001703 band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001704 if (cap & MWL8K_CAP_GREENFIELD)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001705 band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001706 if (cap & MWL8K_CAP_AMPDU) {
1707 hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001708 band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
1709 band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001710 }
1711 if (cap & MWL8K_CAP_RX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001712 band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001713 if (cap & MWL8K_CAP_TX_STBC)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001714 band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001715 if (cap & MWL8K_CAP_SHORTGI_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001716 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001717 if (cap & MWL8K_CAP_SHORTGI_20MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001718 band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001719 if (cap & MWL8K_CAP_DELAY_BA)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001720 band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001721 if (cap & MWL8K_CAP_40MHZ)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001722 band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001723
1724 rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
1725 tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
1726
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001727 band->ht_cap.mcs.rx_mask[0] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001728 if (rx_streams >= 2)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001729 band->ht_cap.mcs.rx_mask[1] = 0xff;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001730 if (rx_streams >= 3)
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001731 band->ht_cap.mcs.rx_mask[2] = 0xff;
1732 band->ht_cap.mcs.rx_mask[4] = 0x01;
1733 band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001734
1735 if (rx_streams != tx_streams) {
Lennert Buytenhek777ad372010-01-12 13:48:04 +01001736 band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
1737 band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
Lennert Buytenhek341c9792010-01-04 21:58:40 +01001738 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1739 }
1740}
1741
Lennert Buytenhek06953232010-01-12 13:49:41 +01001742static void
1743mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
1744{
1745 struct mwl8k_priv *priv = hw->priv;
1746
1747 if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
1748 mwl8k_setup_2ghz_band(hw);
1749 if (caps & MWL8K_CAP_MIMO)
1750 mwl8k_set_ht_caps(hw, &priv->band_24, caps);
1751 }
1752
1753 if (caps & MWL8K_CAP_5GHZ) {
1754 mwl8k_setup_5ghz_band(hw);
1755 if (caps & MWL8K_CAP_MIMO)
1756 mwl8k_set_ht_caps(hw, &priv->band_50, caps);
1757 }
1758}
1759
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001760static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001761{
1762 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek04b147b2009-10-22 20:21:05 +02001763 struct mwl8k_cmd_get_hw_spec_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001764 int rc;
1765 int i;
1766
1767 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1768 if (cmd == NULL)
1769 return -ENOMEM;
1770
1771 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1772 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1773
1774 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1775 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001776 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001777 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001778 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001779 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001780 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
Lennert Buytenhek45eb4002009-10-22 20:20:40 +02001781 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001782
1783 rc = mwl8k_post_cmd(hw, &cmd->header);
1784
1785 if (!rc) {
1786 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1787 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
Lennert Buytenhek4ff64322009-08-03 21:58:39 +02001788 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001789 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek06953232010-01-12 13:49:41 +01001790 mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001791 priv->ap_macids_supported = 0x00000000;
1792 priv->sta_macids_supported = 0x00000001;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001793 }
1794
1795 kfree(cmd);
1796 return rc;
1797}
1798
1799/*
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001800 * CMD_GET_HW_SPEC (AP version).
1801 */
1802struct mwl8k_cmd_get_hw_spec_ap {
1803 struct mwl8k_cmd_pkt header;
1804 __u8 hw_rev;
1805 __u8 host_interface;
1806 __le16 num_wcb;
1807 __le16 num_mcaddrs;
1808 __u8 perm_addr[ETH_ALEN];
1809 __le16 region_code;
1810 __le16 num_antenna;
1811 __le32 fw_rev;
1812 __le32 wcbbase0;
1813 __le32 rxwrptr;
1814 __le32 rxrdptr;
1815 __le32 ps_cookie;
1816 __le32 wcbbase1;
1817 __le32 wcbbase2;
1818 __le32 wcbbase3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001819} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001820
1821static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
1822{
1823 struct mwl8k_priv *priv = hw->priv;
1824 struct mwl8k_cmd_get_hw_spec_ap *cmd;
1825 int rc;
1826
1827 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1828 if (cmd == NULL)
1829 return -ENOMEM;
1830
1831 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
1832 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1833
1834 memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
1835 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1836
1837 rc = mwl8k_post_cmd(hw, &cmd->header);
1838
1839 if (!rc) {
1840 int off;
1841
1842 SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
1843 priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
1844 priv->fw_rev = le32_to_cpu(cmd->fw_rev);
1845 priv->hw_rev = cmd->hw_rev;
Lennert Buytenhek1349ad22010-01-12 13:48:17 +01001846 mwl8k_setup_2ghz_band(hw);
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01001847 priv->ap_macids_supported = 0x000000ff;
1848 priv->sta_macids_supported = 0x00000000;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001849
1850 off = le32_to_cpu(cmd->wcbbase0) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001851 iowrite32(priv->txq[0].txd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001852
1853 off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001854 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001855
1856 off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001857 iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001858
1859 off = le32_to_cpu(cmd->wcbbase1) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001860 iowrite32(priv->txq[1].txd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001861
1862 off = le32_to_cpu(cmd->wcbbase2) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001863 iowrite32(priv->txq[2].txd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001864
1865 off = le32_to_cpu(cmd->wcbbase3) & 0xffff;
John W. Linvilleb6037422010-07-20 13:55:00 -04001866 iowrite32(priv->txq[3].txd_dma, priv->sram + off);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001867 }
1868
1869 kfree(cmd);
1870 return rc;
1871}
1872
1873/*
1874 * CMD_SET_HW_SPEC.
1875 */
1876struct mwl8k_cmd_set_hw_spec {
1877 struct mwl8k_cmd_pkt header;
1878 __u8 hw_rev;
1879 __u8 host_interface;
1880 __le16 num_mcaddrs;
1881 __u8 perm_addr[ETH_ALEN];
1882 __le16 region_code;
1883 __le32 fw_rev;
1884 __le32 ps_cookie;
1885 __le32 caps;
1886 __le32 rx_queue_ptr;
1887 __le32 num_tx_queues;
1888 __le32 tx_queue_ptrs[MWL8K_TX_QUEUES];
1889 __le32 flags;
1890 __le32 num_tx_desc_per_queue;
1891 __le32 total_rxd;
Eric Dumazetba2d3582010-06-02 18:10:09 +00001892} __packed;
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001893
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001894#define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT 0x00000080
1895#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP 0x00000020
1896#define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON 0x00000010
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001897
1898static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
1899{
1900 struct mwl8k_priv *priv = hw->priv;
1901 struct mwl8k_cmd_set_hw_spec *cmd;
1902 int rc;
1903 int i;
1904
1905 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
1906 if (cmd == NULL)
1907 return -ENOMEM;
1908
1909 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
1910 cmd->header.length = cpu_to_le16(sizeof(*cmd));
1911
1912 cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
1913 cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
1914 cmd->num_tx_queues = cpu_to_le32(MWL8K_TX_QUEUES);
1915 for (i = 0; i < MWL8K_TX_QUEUES; i++)
1916 cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01001917 cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
1918 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
1919 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON);
Lennert Buytenhek42fba212009-10-22 20:21:30 +02001920 cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
1921 cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
1922
1923 rc = mwl8k_post_cmd(hw, &cmd->header);
1924 kfree(cmd);
1925
1926 return rc;
1927}
1928
1929/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001930 * CMD_MAC_MULTICAST_ADR.
1931 */
1932struct mwl8k_cmd_mac_multicast_adr {
1933 struct mwl8k_cmd_pkt header;
1934 __le16 action;
1935 __le16 numaddr;
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001936 __u8 addr[0][ETH_ALEN];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001937};
1938
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001939#define MWL8K_ENABLE_RX_DIRECTED 0x0001
1940#define MWL8K_ENABLE_RX_MULTICAST 0x0002
1941#define MWL8K_ENABLE_RX_ALL_MULTICAST 0x0004
1942#define MWL8K_ENABLE_RX_BROADCAST 0x0008
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001943
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001944static struct mwl8k_cmd_pkt *
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001945__mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
Jiri Pirko22bedad2010-04-01 21:22:57 +00001946 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001947{
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001948 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001949 struct mwl8k_cmd_mac_multicast_adr *cmd;
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001950 int size;
Jiri Pirko22bedad2010-04-01 21:22:57 +00001951 int mc_count = 0;
1952
1953 if (mc_list)
1954 mc_count = netdev_hw_addr_list_count(mc_list);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001955
Lennert Buytenhek447ced02009-10-22 20:19:50 +02001956 if (allmulti || mc_count > priv->num_mcaddrs) {
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001957 allmulti = 1;
1958 mc_count = 0;
1959 }
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001960
1961 size = sizeof(*cmd) + mc_count * ETH_ALEN;
1962
1963 cmd = kzalloc(size, GFP_ATOMIC);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001964 if (cmd == NULL)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001965 return NULL;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001966
1967 cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
1968 cmd->header.length = cpu_to_le16(size);
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001969 cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
1970 MWL8K_ENABLE_RX_BROADCAST);
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02001971
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001972 if (allmulti) {
1973 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
1974 } else if (mc_count) {
Jiri Pirko22bedad2010-04-01 21:22:57 +00001975 struct netdev_hw_addr *ha;
1976 int i = 0;
Lennert Buytenhekd5e30842009-10-22 20:19:41 +02001977
1978 cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
1979 cmd->numaddr = cpu_to_le16(mc_count);
Jiri Pirko22bedad2010-04-01 21:22:57 +00001980 netdev_hw_addr_list_for_each(ha, mc_list) {
1981 memcpy(cmd->addr[i], ha->addr, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001982 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001983 }
1984
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02001985 return &cmd->header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001986}
1987
1988/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001989 * CMD_GET_STAT.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001990 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01001991struct mwl8k_cmd_get_stat {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001992 struct mwl8k_cmd_pkt header;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001993 __le32 stats[64];
Eric Dumazetba2d3582010-06-02 18:10:09 +00001994} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01001995
1996#define MWL8K_STAT_ACK_FAILURE 9
1997#define MWL8K_STAT_RTS_FAILURE 12
1998#define MWL8K_STAT_FCS_ERROR 24
1999#define MWL8K_STAT_RTS_SUCCESS 11
2000
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002001static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2002 struct ieee80211_low_level_stats *stats)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002003{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002004 struct mwl8k_cmd_get_stat *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002005 int rc;
2006
2007 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2008 if (cmd == NULL)
2009 return -ENOMEM;
2010
2011 cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2012 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002013
2014 rc = mwl8k_post_cmd(hw, &cmd->header);
2015 if (!rc) {
2016 stats->dot11ACKFailureCount =
2017 le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2018 stats->dot11RTSFailureCount =
2019 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2020 stats->dot11FCSErrorCount =
2021 le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2022 stats->dot11RTSSuccessCount =
2023 le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2024 }
2025 kfree(cmd);
2026
2027 return rc;
2028}
2029
2030/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002031 * CMD_RADIO_CONTROL.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002032 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002033struct mwl8k_cmd_radio_control {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002034 struct mwl8k_cmd_pkt header;
2035 __le16 action;
2036 __le16 control;
2037 __le16 radio_on;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002038} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002039
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002040static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002041mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002042{
2043 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002044 struct mwl8k_cmd_radio_control *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002045 int rc;
2046
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002047 if (enable == priv->radio_on && !force)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002048 return 0;
2049
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002050 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2051 if (cmd == NULL)
2052 return -ENOMEM;
2053
2054 cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2055 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2056 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002057 cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002058 cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2059
2060 rc = mwl8k_post_cmd(hw, &cmd->header);
2061 kfree(cmd);
2062
2063 if (!rc)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002064 priv->radio_on = enable;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002065
2066 return rc;
2067}
2068
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002069static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002070{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002071 return mwl8k_cmd_radio_control(hw, 0, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002072}
2073
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002074static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002075{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002076 return mwl8k_cmd_radio_control(hw, 1, 0);
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02002077}
2078
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002079static int
2080mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2081{
Lennert Buytenhek99200a992009-11-30 18:31:40 +01002082 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002083
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02002084 priv->radio_short_preamble = short_preamble;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002085
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002086 return mwl8k_cmd_radio_control(hw, 1, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002087}
2088
2089/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002090 * CMD_RF_TX_POWER.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002091 */
2092#define MWL8K_TX_POWER_LEVEL_TOTAL 8
2093
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002094struct mwl8k_cmd_rf_tx_power {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002095 struct mwl8k_cmd_pkt header;
2096 __le16 action;
2097 __le16 support_level;
2098 __le16 current_level;
2099 __le16 reserved;
2100 __le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002101} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002102
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002103static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002104{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002105 struct mwl8k_cmd_rf_tx_power *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002106 int rc;
2107
2108 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2109 if (cmd == NULL)
2110 return -ENOMEM;
2111
2112 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2113 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2114 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2115 cmd->support_level = cpu_to_le16(dBm);
2116
2117 rc = mwl8k_post_cmd(hw, &cmd->header);
2118 kfree(cmd);
2119
2120 return rc;
2121}
2122
2123/*
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002124 * CMD_RF_ANTENNA.
2125 */
2126struct mwl8k_cmd_rf_antenna {
2127 struct mwl8k_cmd_pkt header;
2128 __le16 antenna;
2129 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002130} __packed;
Lennert Buytenhek08b06342009-10-22 20:21:33 +02002131
2132#define MWL8K_RF_ANTENNA_RX 1
2133#define MWL8K_RF_ANTENNA_TX 2
2134
2135static int
2136mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2137{
2138 struct mwl8k_cmd_rf_antenna *cmd;
2139 int rc;
2140
2141 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2142 if (cmd == NULL)
2143 return -ENOMEM;
2144
2145 cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2146 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2147 cmd->antenna = cpu_to_le16(antenna);
2148 cmd->mode = cpu_to_le16(mask);
2149
2150 rc = mwl8k_post_cmd(hw, &cmd->header);
2151 kfree(cmd);
2152
2153 return rc;
2154}
2155
2156/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002157 * CMD_SET_BEACON.
2158 */
2159struct mwl8k_cmd_set_beacon {
2160 struct mwl8k_cmd_pkt header;
2161 __le16 beacon_len;
2162 __u8 beacon[0];
2163};
2164
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002165static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2166 struct ieee80211_vif *vif, u8 *beacon, int len)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002167{
2168 struct mwl8k_cmd_set_beacon *cmd;
2169 int rc;
2170
2171 cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2172 if (cmd == NULL)
2173 return -ENOMEM;
2174
2175 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2176 cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2177 cmd->beacon_len = cpu_to_le16(len);
2178 memcpy(cmd->beacon, beacon, len);
2179
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002180 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002181 kfree(cmd);
2182
2183 return rc;
2184}
2185
2186/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002187 * CMD_SET_PRE_SCAN.
2188 */
2189struct mwl8k_cmd_set_pre_scan {
2190 struct mwl8k_cmd_pkt header;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002191} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002192
2193static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2194{
2195 struct mwl8k_cmd_set_pre_scan *cmd;
2196 int rc;
2197
2198 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2199 if (cmd == NULL)
2200 return -ENOMEM;
2201
2202 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
2203 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2204
2205 rc = mwl8k_post_cmd(hw, &cmd->header);
2206 kfree(cmd);
2207
2208 return rc;
2209}
2210
2211/*
2212 * CMD_SET_POST_SCAN.
2213 */
2214struct mwl8k_cmd_set_post_scan {
2215 struct mwl8k_cmd_pkt header;
2216 __le32 isibss;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002217 __u8 bssid[ETH_ALEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002218} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002219
2220static int
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002221mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002222{
2223 struct mwl8k_cmd_set_post_scan *cmd;
2224 int rc;
2225
2226 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2227 if (cmd == NULL)
2228 return -ENOMEM;
2229
2230 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
2231 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2232 cmd->isibss = 0;
Lennert Buytenhekd89173f2009-07-16 09:54:27 +02002233 memcpy(cmd->bssid, mac, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002234
2235 rc = mwl8k_post_cmd(hw, &cmd->header);
2236 kfree(cmd);
2237
2238 return rc;
2239}
2240
2241/*
2242 * CMD_SET_RF_CHANNEL.
2243 */
2244struct mwl8k_cmd_set_rf_channel {
2245 struct mwl8k_cmd_pkt header;
2246 __le16 action;
2247 __u8 current_channel;
2248 __le32 channel_flags;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002249} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002250
2251static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002252 struct ieee80211_conf *conf)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002253{
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002254 struct ieee80211_channel *channel = conf->channel;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002255 struct mwl8k_cmd_set_rf_channel *cmd;
2256 int rc;
2257
2258 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2259 if (cmd == NULL)
2260 return -ENOMEM;
2261
2262 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
2263 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2264 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2265 cmd->current_channel = channel->hw_value;
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002266
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002267 if (channel->band == IEEE80211_BAND_2GHZ)
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002268 cmd->channel_flags |= cpu_to_le32(0x00000001);
Lennert Buytenhek42574ea2010-01-12 13:49:18 +01002269 else if (channel->band == IEEE80211_BAND_5GHZ)
2270 cmd->channel_flags |= cpu_to_le32(0x00000004);
Lennert Buytenhek610677d2010-01-04 21:56:46 +01002271
2272 if (conf->channel_type == NL80211_CHAN_NO_HT ||
2273 conf->channel_type == NL80211_CHAN_HT20)
2274 cmd->channel_flags |= cpu_to_le32(0x00000080);
2275 else if (conf->channel_type == NL80211_CHAN_HT40MINUS)
2276 cmd->channel_flags |= cpu_to_le32(0x000001900);
2277 else if (conf->channel_type == NL80211_CHAN_HT40PLUS)
2278 cmd->channel_flags |= cpu_to_le32(0x000000900);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002279
2280 rc = mwl8k_post_cmd(hw, &cmd->header);
2281 kfree(cmd);
2282
2283 return rc;
2284}
2285
2286/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002287 * CMD_SET_AID.
2288 */
2289#define MWL8K_FRAME_PROT_DISABLED 0x00
2290#define MWL8K_FRAME_PROT_11G 0x07
2291#define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY 0x02
2292#define MWL8K_FRAME_PROT_11N_HT_ALL 0x06
2293
2294struct mwl8k_cmd_update_set_aid {
2295 struct mwl8k_cmd_pkt header;
2296 __le16 aid;
2297
2298 /* AP's MAC address (BSSID) */
2299 __u8 bssid[ETH_ALEN];
2300 __le16 protection_mode;
2301 __u8 supp_rates[14];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002302} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002303
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002304static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
2305{
2306 int i;
2307 int j;
2308
2309 /*
2310 * Clear nonstandard rates 4 and 13.
2311 */
2312 mask &= 0x1fef;
2313
2314 for (i = 0, j = 0; i < 14; i++) {
2315 if (mask & (1 << i))
Lennert Buytenhek777ad372010-01-12 13:48:04 +01002316 rates[j++] = mwl8k_rates_24[i].hw_value;
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002317 }
2318}
2319
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002320static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002321mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
2322 struct ieee80211_vif *vif, u32 legacy_rate_mask)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002323{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002324 struct mwl8k_cmd_update_set_aid *cmd;
2325 u16 prot_mode;
2326 int rc;
2327
2328 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2329 if (cmd == NULL)
2330 return -ENOMEM;
2331
2332 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
2333 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002334 cmd->aid = cpu_to_le16(vif->bss_conf.aid);
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01002335 memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002336
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002337 if (vif->bss_conf.use_cts_prot) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002338 prot_mode = MWL8K_FRAME_PROT_11G;
2339 } else {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01002340 switch (vif->bss_conf.ht_operation_mode &
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002341 IEEE80211_HT_OP_MODE_PROTECTION) {
2342 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
2343 prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
2344 break;
2345 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
2346 prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
2347 break;
2348 default:
2349 prot_mode = MWL8K_FRAME_PROT_DISABLED;
2350 break;
2351 }
2352 }
2353 cmd->protection_mode = cpu_to_le16(prot_mode);
2354
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002355 legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002356
2357 rc = mwl8k_post_cmd(hw, &cmd->header);
2358 kfree(cmd);
2359
2360 return rc;
2361}
2362
2363/*
2364 * CMD_SET_RATE.
2365 */
2366struct mwl8k_cmd_set_rate {
2367 struct mwl8k_cmd_pkt header;
2368 __u8 legacy_rates[14];
2369
2370 /* Bitmap for supported MCS codes. */
2371 __u8 mcs_set[16];
2372 __u8 reserved[16];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002373} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002374
2375static int
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002376mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002377 u32 legacy_rate_mask, u8 *mcs_rates)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002378{
2379 struct mwl8k_cmd_set_rate *cmd;
2380 int rc;
2381
2382 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2383 if (cmd == NULL)
2384 return -ENOMEM;
2385
2386 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
2387 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01002388 legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01002389 memcpy(cmd->mcs_set, mcs_rates, 16);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002390
2391 rc = mwl8k_post_cmd(hw, &cmd->header);
2392 kfree(cmd);
2393
2394 return rc;
2395}
2396
2397/*
2398 * CMD_FINALIZE_JOIN.
2399 */
2400#define MWL8K_FJ_BEACON_MAXLEN 128
2401
2402struct mwl8k_cmd_finalize_join {
2403 struct mwl8k_cmd_pkt header;
2404 __le32 sleep_interval; /* Number of beacon periods to sleep */
2405 __u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
Eric Dumazetba2d3582010-06-02 18:10:09 +00002406} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002407
2408static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
2409 int framelen, int dtim)
2410{
2411 struct mwl8k_cmd_finalize_join *cmd;
2412 struct ieee80211_mgmt *payload = frame;
2413 int payload_len;
2414 int rc;
2415
2416 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2417 if (cmd == NULL)
2418 return -ENOMEM;
2419
2420 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
2421 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2422 cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
2423
2424 payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
2425 if (payload_len < 0)
2426 payload_len = 0;
2427 else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
2428 payload_len = MWL8K_FJ_BEACON_MAXLEN;
2429
2430 memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
2431
2432 rc = mwl8k_post_cmd(hw, &cmd->header);
2433 kfree(cmd);
2434
2435 return rc;
2436}
2437
2438/*
2439 * CMD_SET_RTS_THRESHOLD.
2440 */
2441struct mwl8k_cmd_set_rts_threshold {
2442 struct mwl8k_cmd_pkt header;
2443 __le16 action;
2444 __le16 threshold;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002445} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002446
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002447static int
2448mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002449{
2450 struct mwl8k_cmd_set_rts_threshold *cmd;
2451 int rc;
2452
2453 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2454 if (cmd == NULL)
2455 return -ENOMEM;
2456
2457 cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
2458 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01002459 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2460 cmd->threshold = cpu_to_le16(rts_thresh);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002461
2462 rc = mwl8k_post_cmd(hw, &cmd->header);
2463 kfree(cmd);
2464
2465 return rc;
2466}
2467
2468/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002469 * CMD_SET_SLOT.
2470 */
2471struct mwl8k_cmd_set_slot {
2472 struct mwl8k_cmd_pkt header;
2473 __le16 action;
2474 __u8 short_slot;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002475} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002476
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002477static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002478{
2479 struct mwl8k_cmd_set_slot *cmd;
2480 int rc;
2481
2482 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2483 if (cmd == NULL)
2484 return -ENOMEM;
2485
2486 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
2487 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2488 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
Lennert Buytenhek5539bb52009-07-16 16:06:53 +02002489 cmd->short_slot = short_slot_time;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002490
2491 rc = mwl8k_post_cmd(hw, &cmd->header);
2492 kfree(cmd);
2493
2494 return rc;
2495}
2496
2497/*
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002498 * CMD_SET_EDCA_PARAMS.
2499 */
2500struct mwl8k_cmd_set_edca_params {
2501 struct mwl8k_cmd_pkt header;
2502
2503 /* See MWL8K_SET_EDCA_XXX below */
2504 __le16 action;
2505
2506 /* TX opportunity in units of 32 us */
2507 __le16 txop;
2508
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002509 union {
2510 struct {
2511 /* Log exponent of max contention period: 0...15 */
2512 __le32 log_cw_max;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002513
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002514 /* Log exponent of min contention period: 0...15 */
2515 __le32 log_cw_min;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002516
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002517 /* Adaptive interframe spacing in units of 32us */
2518 __u8 aifs;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002519
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002520 /* TX queue to configure */
2521 __u8 txq;
2522 } ap;
2523 struct {
2524 /* Log exponent of max contention period: 0...15 */
2525 __u8 log_cw_max;
2526
2527 /* Log exponent of min contention period: 0...15 */
2528 __u8 log_cw_min;
2529
2530 /* Adaptive interframe spacing in units of 32us */
2531 __u8 aifs;
2532
2533 /* TX queue to configure */
2534 __u8 txq;
2535 } sta;
2536 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002537} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002538
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002539#define MWL8K_SET_EDCA_CW 0x01
2540#define MWL8K_SET_EDCA_TXOP 0x02
2541#define MWL8K_SET_EDCA_AIFS 0x04
2542
2543#define MWL8K_SET_EDCA_ALL (MWL8K_SET_EDCA_CW | \
2544 MWL8K_SET_EDCA_TXOP | \
2545 MWL8K_SET_EDCA_AIFS)
2546
2547static int
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002548mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
2549 __u16 cw_min, __u16 cw_max,
2550 __u8 aifs, __u16 txop)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002551{
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002552 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002553 struct mwl8k_cmd_set_edca_params *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002554 int rc;
2555
2556 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2557 if (cmd == NULL)
2558 return -ENOMEM;
2559
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002560 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
2561 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002562 cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
2563 cmd->txop = cpu_to_le16(txop);
Lennert Buytenhek2e484c82009-10-22 20:21:43 +02002564 if (priv->ap_fw) {
2565 cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
2566 cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
2567 cmd->ap.aifs = aifs;
2568 cmd->ap.txq = qnum;
2569 } else {
2570 cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
2571 cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
2572 cmd->sta.aifs = aifs;
2573 cmd->sta.txq = qnum;
2574 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002575
2576 rc = mwl8k_post_cmd(hw, &cmd->header);
2577 kfree(cmd);
2578
2579 return rc;
2580}
2581
2582/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002583 * CMD_SET_WMM_MODE.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002584 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002585struct mwl8k_cmd_set_wmm_mode {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002586 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002587 __le16 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002588} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002589
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002590static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002591{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002592 struct mwl8k_priv *priv = hw->priv;
2593 struct mwl8k_cmd_set_wmm_mode *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002594 int rc;
2595
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002596 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2597 if (cmd == NULL)
2598 return -ENOMEM;
2599
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002600 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002601 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002602 cmd->action = cpu_to_le16(!!enable);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002603
2604 rc = mwl8k_post_cmd(hw, &cmd->header);
2605 kfree(cmd);
Lennert Buytenhek16cec432009-11-30 18:14:23 +01002606
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002607 if (!rc)
2608 priv->wmm_enabled = enable;
2609
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002610 return rc;
2611}
2612
2613/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002614 * CMD_MIMO_CONFIG.
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002615 */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002616struct mwl8k_cmd_mimo_config {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002617 struct mwl8k_cmd_pkt header;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002618 __le32 action;
2619 __u8 rx_antenna_map;
2620 __u8 tx_antenna_map;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002621} __packed;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002622
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002623static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002624{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002625 struct mwl8k_cmd_mimo_config *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002626 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002627
2628 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2629 if (cmd == NULL)
2630 return -ENOMEM;
2631
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002632 cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002633 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002634 cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
2635 cmd->rx_antenna_map = rx;
2636 cmd->tx_antenna_map = tx;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002637
2638 rc = mwl8k_post_cmd(hw, &cmd->header);
2639 kfree(cmd);
2640
2641 return rc;
2642}
2643
2644/*
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002645 * CMD_USE_FIXED_RATE (STA version).
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002646 */
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002647struct mwl8k_cmd_use_fixed_rate_sta {
2648 struct mwl8k_cmd_pkt header;
2649 __le32 action;
2650 __le32 allow_rate_drop;
2651 __le32 num_rates;
2652 struct {
2653 __le32 is_ht_rate;
2654 __le32 enable_retry;
2655 __le32 rate;
2656 __le32 retry_count;
2657 } rate_entry[8];
2658 __le32 rate_type;
2659 __le32 reserved1;
2660 __le32 reserved2;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002661} __packed;
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002662
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002663#define MWL8K_USE_AUTO_RATE 0x0002
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002664#define MWL8K_UCAST_RATE 0
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002665
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002666static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002667{
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002668 struct mwl8k_cmd_use_fixed_rate_sta *cmd;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002669 int rc;
2670
2671 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2672 if (cmd == NULL)
2673 return -ENOMEM;
2674
2675 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2676 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01002677 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2678 cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01002679
2680 rc = mwl8k_post_cmd(hw, &cmd->header);
2681 kfree(cmd);
2682
2683 return rc;
2684}
2685
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002686/*
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002687 * CMD_USE_FIXED_RATE (AP version).
2688 */
2689struct mwl8k_cmd_use_fixed_rate_ap {
2690 struct mwl8k_cmd_pkt header;
2691 __le32 action;
2692 __le32 allow_rate_drop;
2693 __le32 num_rates;
2694 struct mwl8k_rate_entry_ap {
2695 __le32 is_ht_rate;
2696 __le32 enable_retry;
2697 __le32 rate;
2698 __le32 retry_count;
2699 } rate_entry[4];
2700 u8 multicast_rate;
2701 u8 multicast_rate_type;
2702 u8 management_rate;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002703} __packed;
Lennert Buytenhek088aab82010-01-08 18:30:36 +01002704
2705static int
2706mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
2707{
2708 struct mwl8k_cmd_use_fixed_rate_ap *cmd;
2709 int rc;
2710
2711 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2712 if (cmd == NULL)
2713 return -ENOMEM;
2714
2715 cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
2716 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2717 cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
2718 cmd->multicast_rate = mcast;
2719 cmd->management_rate = mgmt;
2720
2721 rc = mwl8k_post_cmd(hw, &cmd->header);
2722 kfree(cmd);
2723
2724 return rc;
2725}
2726
2727/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002728 * CMD_ENABLE_SNIFFER.
2729 */
2730struct mwl8k_cmd_enable_sniffer {
2731 struct mwl8k_cmd_pkt header;
2732 __le32 action;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002733} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002734
2735static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
2736{
2737 struct mwl8k_cmd_enable_sniffer *cmd;
2738 int rc;
2739
2740 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2741 if (cmd == NULL)
2742 return -ENOMEM;
2743
2744 cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
2745 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2746 cmd->action = cpu_to_le32(!!enable);
2747
2748 rc = mwl8k_post_cmd(hw, &cmd->header);
2749 kfree(cmd);
2750
2751 return rc;
2752}
2753
2754/*
2755 * CMD_SET_MAC_ADDR.
2756 */
2757struct mwl8k_cmd_set_mac_addr {
2758 struct mwl8k_cmd_pkt header;
2759 union {
2760 struct {
2761 __le16 mac_type;
2762 __u8 mac_addr[ETH_ALEN];
2763 } mbss;
2764 __u8 mac_addr[ETH_ALEN];
2765 };
Eric Dumazetba2d3582010-06-02 18:10:09 +00002766} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002767
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002768#define MWL8K_MAC_TYPE_PRIMARY_CLIENT 0
2769#define MWL8K_MAC_TYPE_SECONDARY_CLIENT 1
2770#define MWL8K_MAC_TYPE_PRIMARY_AP 2
2771#define MWL8K_MAC_TYPE_SECONDARY_AP 3
Lennert Buytenheka9e00b12010-01-08 18:31:30 +01002772
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002773static int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
2774 struct ieee80211_vif *vif, u8 *mac)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002775{
2776 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002777 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002778 struct mwl8k_cmd_set_mac_addr *cmd;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002779 int mac_type;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002780 int rc;
2781
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002782 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2783 if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
2784 if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
2785 mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
2786 else
2787 mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
2788 } else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
2789 if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
2790 mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
2791 else
2792 mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
2793 }
2794
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002795 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2796 if (cmd == NULL)
2797 return -ENOMEM;
2798
2799 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
2800 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2801 if (priv->ap_fw) {
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01002802 cmd->mbss.mac_type = cpu_to_le16(mac_type);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002803 memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
2804 } else {
2805 memcpy(cmd->mac_addr, mac, ETH_ALEN);
2806 }
2807
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002808 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002809 kfree(cmd);
2810
2811 return rc;
2812}
2813
2814/*
2815 * CMD_SET_RATEADAPT_MODE.
2816 */
2817struct mwl8k_cmd_set_rate_adapt_mode {
2818 struct mwl8k_cmd_pkt header;
2819 __le16 action;
2820 __le16 mode;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002821} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002822
2823static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
2824{
2825 struct mwl8k_cmd_set_rate_adapt_mode *cmd;
2826 int rc;
2827
2828 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2829 if (cmd == NULL)
2830 return -ENOMEM;
2831
2832 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
2833 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2834 cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2835 cmd->mode = cpu_to_le16(mode);
2836
2837 rc = mwl8k_post_cmd(hw, &cmd->header);
2838 kfree(cmd);
2839
2840 return rc;
2841}
2842
2843/*
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002844 * CMD_BSS_START.
2845 */
2846struct mwl8k_cmd_bss_start {
2847 struct mwl8k_cmd_pkt header;
2848 __le32 enable;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002849} __packed;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002850
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002851static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
2852 struct ieee80211_vif *vif, int enable)
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002853{
2854 struct mwl8k_cmd_bss_start *cmd;
2855 int rc;
2856
2857 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2858 if (cmd == NULL)
2859 return -ENOMEM;
2860
2861 cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
2862 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2863 cmd->enable = cpu_to_le32(enable);
2864
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002865 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002866 kfree(cmd);
2867
2868 return rc;
2869}
2870
2871/*
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002872 * CMD_SET_NEW_STN.
2873 */
2874struct mwl8k_cmd_set_new_stn {
2875 struct mwl8k_cmd_pkt header;
2876 __le16 aid;
2877 __u8 mac_addr[6];
2878 __le16 stn_id;
2879 __le16 action;
2880 __le16 rsvd;
2881 __le32 legacy_rates;
2882 __u8 ht_rates[4];
2883 __le16 cap_info;
2884 __le16 ht_capabilities_info;
2885 __u8 mac_ht_param_info;
2886 __u8 rev;
2887 __u8 control_channel;
2888 __u8 add_channel;
2889 __le16 op_mode;
2890 __le16 stbc;
2891 __u8 add_qos_info;
2892 __u8 is_qos_sta;
2893 __le32 fw_sta_ptr;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002894} __packed;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002895
2896#define MWL8K_STA_ACTION_ADD 0
2897#define MWL8K_STA_ACTION_REMOVE 2
2898
2899static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
2900 struct ieee80211_vif *vif,
2901 struct ieee80211_sta *sta)
2902{
2903 struct mwl8k_cmd_set_new_stn *cmd;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002904 u32 rates;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002905 int rc;
2906
2907 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2908 if (cmd == NULL)
2909 return -ENOMEM;
2910
2911 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2912 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2913 cmd->aid = cpu_to_le16(sta->aid);
2914 memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
2915 cmd->stn_id = cpu_to_le16(sta->aid);
2916 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01002917 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
2918 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
2919 else
2920 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
2921 cmd->legacy_rates = cpu_to_le32(rates);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002922 if (sta->ht_cap.ht_supported) {
2923 cmd->ht_rates[0] = sta->ht_cap.mcs.rx_mask[0];
2924 cmd->ht_rates[1] = sta->ht_cap.mcs.rx_mask[1];
2925 cmd->ht_rates[2] = sta->ht_cap.mcs.rx_mask[2];
2926 cmd->ht_rates[3] = sta->ht_cap.mcs.rx_mask[3];
2927 cmd->ht_capabilities_info = cpu_to_le16(sta->ht_cap.cap);
2928 cmd->mac_ht_param_info = (sta->ht_cap.ampdu_factor & 3) |
2929 ((sta->ht_cap.ampdu_density & 7) << 2);
2930 cmd->is_qos_sta = 1;
2931 }
2932
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002933 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002934 kfree(cmd);
2935
2936 return rc;
2937}
2938
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002939static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
2940 struct ieee80211_vif *vif)
2941{
2942 struct mwl8k_cmd_set_new_stn *cmd;
2943 int rc;
2944
2945 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2946 if (cmd == NULL)
2947 return -ENOMEM;
2948
2949 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2950 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2951 memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
2952
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002953 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01002954 kfree(cmd);
2955
2956 return rc;
2957}
2958
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002959static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
2960 struct ieee80211_vif *vif, u8 *addr)
2961{
2962 struct mwl8k_cmd_set_new_stn *cmd;
2963 int rc;
2964
2965 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2966 if (cmd == NULL)
2967 return -ENOMEM;
2968
2969 cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
2970 cmd->header.length = cpu_to_le16(sizeof(*cmd));
2971 memcpy(cmd->mac_addr, addr, ETH_ALEN);
2972 cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
2973
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01002974 rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01002975 kfree(cmd);
2976
2977 return rc;
2978}
2979
2980/*
Lennert Buytenhek55489b62009-11-30 18:31:33 +01002981 * CMD_UPDATE_STADB.
2982 */
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002983struct ewc_ht_info {
2984 __le16 control1;
2985 __le16 control2;
2986 __le16 control3;
Eric Dumazetba2d3582010-06-02 18:10:09 +00002987} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01002988
2989struct peer_capability_info {
2990 /* Peer type - AP vs. STA. */
2991 __u8 peer_type;
2992
2993 /* Basic 802.11 capabilities from assoc resp. */
2994 __le16 basic_caps;
2995
2996 /* Set if peer supports 802.11n high throughput (HT). */
2997 __u8 ht_support;
2998
2999 /* Valid if HT is supported. */
3000 __le16 ht_caps;
3001 __u8 extended_ht_caps;
3002 struct ewc_ht_info ewc_info;
3003
3004 /* Legacy rate table. Intersection of our rates and peer rates. */
3005 __u8 legacy_rates[12];
3006
3007 /* HT rate table. Intersection of our rates and peer rates. */
3008 __u8 ht_rates[16];
3009 __u8 pad[16];
3010
3011 /* If set, interoperability mode, no proprietary extensions. */
3012 __u8 interop;
3013 __u8 pad2;
3014 __u8 station_id;
3015 __le16 amsdu_enabled;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003016} __packed;
Lennert Buytenhek25d81b12010-01-04 21:54:41 +01003017
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003018struct mwl8k_cmd_update_stadb {
3019 struct mwl8k_cmd_pkt header;
3020
3021 /* See STADB_ACTION_TYPE */
3022 __le32 action;
3023
3024 /* Peer MAC address */
3025 __u8 peer_addr[ETH_ALEN];
3026
3027 __le32 reserved;
3028
3029 /* Peer info - valid during add/update. */
3030 struct peer_capability_info peer_info;
Eric Dumazetba2d3582010-06-02 18:10:09 +00003031} __packed;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003032
Lennert Buytenheka6804002010-01-04 21:55:42 +01003033#define MWL8K_STA_DB_MODIFY_ENTRY 1
3034#define MWL8K_STA_DB_DEL_ENTRY 2
3035
3036/* Peer Entry flags - used to define the type of the peer node */
3037#define MWL8K_PEER_TYPE_ACCESSPOINT 2
3038
3039static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003040 struct ieee80211_vif *vif,
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003041 struct ieee80211_sta *sta)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003042{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003043 struct mwl8k_cmd_update_stadb *cmd;
Lennert Buytenheka6804002010-01-04 21:55:42 +01003044 struct peer_capability_info *p;
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003045 u32 rates;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003046 int rc;
3047
3048 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3049 if (cmd == NULL)
3050 return -ENOMEM;
3051
3052 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3053 cmd->header.length = cpu_to_le16(sizeof(*cmd));
Lennert Buytenheka6804002010-01-04 21:55:42 +01003054 cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003055 memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003056
Lennert Buytenheka6804002010-01-04 21:55:42 +01003057 p = &cmd->peer_info;
3058 p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
3059 p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003060 p->ht_support = sta->ht_cap.ht_supported;
John W. Linvilleb6037422010-07-20 13:55:00 -04003061 p->ht_caps = cpu_to_le16(sta->ht_cap.cap);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003062 p->extended_ht_caps = (sta->ht_cap.ampdu_factor & 3) |
3063 ((sta->ht_cap.ampdu_density & 7) << 2);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003064 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3065 rates = sta->supp_rates[IEEE80211_BAND_2GHZ];
3066 else
3067 rates = sta->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3068 legacy_rate_mask_to_array(p->legacy_rates, rates);
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003069 memcpy(p->ht_rates, sta->ht_cap.mcs.rx_mask, 16);
Lennert Buytenheka6804002010-01-04 21:55:42 +01003070 p->interop = 1;
3071 p->amsdu_enabled = 0;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003072
Lennert Buytenheka6804002010-01-04 21:55:42 +01003073 rc = mwl8k_post_cmd(hw, &cmd->header);
3074 kfree(cmd);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003075
Lennert Buytenheka6804002010-01-04 21:55:42 +01003076 return rc ? rc : p->station_id;
3077}
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003078
Lennert Buytenheka6804002010-01-04 21:55:42 +01003079static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
3080 struct ieee80211_vif *vif, u8 *addr)
3081{
3082 struct mwl8k_cmd_update_stadb *cmd;
3083 int rc;
3084
3085 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3086 if (cmd == NULL)
3087 return -ENOMEM;
3088
3089 cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
3090 cmd->header.length = cpu_to_le16(sizeof(*cmd));
3091 cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
3092 memcpy(cmd->peer_addr, addr, ETH_ALEN);
3093
3094 rc = mwl8k_post_cmd(hw, &cmd->header);
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003095 kfree(cmd);
3096
3097 return rc;
3098}
3099
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003100
3101/*
3102 * Interrupt handling.
3103 */
3104static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
3105{
3106 struct ieee80211_hw *hw = dev_id;
3107 struct mwl8k_priv *priv = hw->priv;
3108 u32 status;
3109
3110 status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003111 if (!status)
3112 return IRQ_NONE;
3113
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003114 if (status & MWL8K_A2H_INT_TX_DONE) {
3115 status &= ~MWL8K_A2H_INT_TX_DONE;
3116 tasklet_schedule(&priv->poll_tx_task);
3117 }
3118
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003119 if (status & MWL8K_A2H_INT_RX_READY) {
3120 status &= ~MWL8K_A2H_INT_RX_READY;
3121 tasklet_schedule(&priv->poll_rx_task);
3122 }
3123
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003124 if (status)
3125 iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003126
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003127 if (status & MWL8K_A2H_INT_OPC_DONE) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003128 if (priv->hostcmd_wait != NULL)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003129 complete(priv->hostcmd_wait);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003130 }
3131
3132 if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003133 if (!mutex_is_locked(&priv->fw_mutex) &&
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02003134 priv->radio_on && priv->pending_tx_pkts)
Lennert Buytenhek618952a2009-08-18 03:18:01 +02003135 mwl8k_tx_start(priv);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003136 }
3137
3138 return IRQ_HANDLED;
3139}
3140
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003141static void mwl8k_tx_poll(unsigned long data)
3142{
3143 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3144 struct mwl8k_priv *priv = hw->priv;
3145 int limit;
3146 int i;
3147
3148 limit = 32;
3149
3150 spin_lock_bh(&priv->tx_lock);
3151
3152 for (i = 0; i < MWL8K_TX_QUEUES; i++)
3153 limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
3154
3155 if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
3156 complete(priv->tx_wait);
3157 priv->tx_wait = NULL;
3158 }
3159
3160 spin_unlock_bh(&priv->tx_lock);
3161
3162 if (limit) {
3163 writel(~MWL8K_A2H_INT_TX_DONE,
3164 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3165 } else {
3166 tasklet_schedule(&priv->poll_tx_task);
3167 }
3168}
3169
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003170static void mwl8k_rx_poll(unsigned long data)
3171{
3172 struct ieee80211_hw *hw = (struct ieee80211_hw *)data;
3173 struct mwl8k_priv *priv = hw->priv;
3174 int limit;
3175
3176 limit = 32;
3177 limit -= rxq_process(hw, 0, limit);
3178 limit -= rxq_refill(hw, 0, limit);
3179
3180 if (limit) {
3181 writel(~MWL8K_A2H_INT_RX_READY,
3182 priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
3183 } else {
3184 tasklet_schedule(&priv->poll_rx_task);
3185 }
3186}
3187
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003188
3189/*
3190 * Core driver operations.
3191 */
3192static int mwl8k_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3193{
3194 struct mwl8k_priv *priv = hw->priv;
3195 int index = skb_get_queue_mapping(skb);
3196 int rc;
3197
Lennert Buytenhek9189c102010-01-12 13:47:32 +01003198 if (!priv->radio_on) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003199 wiphy_debug(hw->wiphy,
3200 "dropped TX frame since radio disabled\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003201 dev_kfree_skb(skb);
3202 return NETDEV_TX_OK;
3203 }
3204
3205 rc = mwl8k_txq_xmit(hw, index, skb);
3206
3207 return rc;
3208}
3209
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003210static int mwl8k_start(struct ieee80211_hw *hw)
3211{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003212 struct mwl8k_priv *priv = hw->priv;
3213 int rc;
3214
Joe Perchesa0607fd2009-11-18 23:29:17 -08003215 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003216 IRQF_SHARED, MWL8K_NAME, hw);
3217 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07003218 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003219 return -EIO;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003220 }
3221
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003222 /* Enable TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003223 tasklet_enable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003224 tasklet_enable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003225
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003226 /* Enable interrupts */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02003227 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003228
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003229 rc = mwl8k_fw_lock(hw);
3230 if (!rc) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003231 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003232
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003233 if (!priv->ap_fw) {
3234 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003235 rc = mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003236
Lennert Buytenhek5e4cf162009-10-22 20:21:38 +02003237 if (!rc)
3238 rc = mwl8k_cmd_set_pre_scan(hw);
3239
3240 if (!rc)
3241 rc = mwl8k_cmd_set_post_scan(hw,
3242 "\x00\x00\x00\x00\x00\x00");
3243 }
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003244
3245 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003246 rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003247
3248 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003249 rc = mwl8k_cmd_set_wmm_mode(hw, 0);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003250
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003251 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003252 }
3253
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003254 if (rc) {
3255 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
3256 free_irq(priv->pdev->irq, hw);
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003257 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003258 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenhek2ec610c2009-07-17 07:11:37 +02003259 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003260
3261 return rc;
3262}
3263
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003264static void mwl8k_stop(struct ieee80211_hw *hw)
3265{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003266 struct mwl8k_priv *priv = hw->priv;
3267 int i;
3268
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003269 mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003270
3271 ieee80211_stop_queues(hw);
3272
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003273 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003274 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003275 free_irq(priv->pdev->irq, hw);
3276
3277 /* Stop finalize join worker */
3278 cancel_work_sync(&priv->finalize_join_worker);
3279 if (priv->beacon_skb != NULL)
3280 dev_kfree_skb(priv->beacon_skb);
3281
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003282 /* Stop TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01003283 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01003284 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003285
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003286 /* Return all skbs to mac80211 */
3287 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01003288 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003289}
3290
3291static int mwl8k_add_interface(struct ieee80211_hw *hw,
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003292 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003293{
3294 struct mwl8k_priv *priv = hw->priv;
3295 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003296 u32 macids_supported;
3297 int macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003298
3299 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003300 * Reject interface creation if sniffer mode is active, as
3301 * STA operation is mutually exclusive with hardware sniffer
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003302 * mode. (Sniffer mode is only used on STA firmware.)
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003303 */
3304 if (priv->sniffer_enabled) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003305 wiphy_info(hw->wiphy,
3306 "unable to create STA interface because sniffer mode is enabled\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003307 return -EINVAL;
3308 }
3309
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003310
3311 switch (vif->type) {
3312 case NL80211_IFTYPE_AP:
3313 macids_supported = priv->ap_macids_supported;
3314 break;
3315 case NL80211_IFTYPE_STATION:
3316 macids_supported = priv->sta_macids_supported;
3317 break;
3318 default:
3319 return -EINVAL;
3320 }
3321
3322 macid = ffs(macids_supported & ~priv->macids_used);
3323 if (!macid--)
3324 return -EBUSY;
3325
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003326 /* Setup driver private area. */
Johannes Berg1ed32e42009-12-23 13:15:45 +01003327 mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003328 memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003329 mwl8k_vif->vif = vif;
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003330 mwl8k_vif->macid = macid;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003331 mwl8k_vif->seqno = 0;
3332
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003333 /* Set the mac address. */
3334 mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
3335
3336 if (priv->ap_fw)
3337 mwl8k_cmd_set_new_stn_add_self(hw, vif);
3338
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003339 priv->macids_used |= 1 << mwl8k_vif->macid;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003340 list_add_tail(&mwl8k_vif->list, &priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003341
3342 return 0;
3343}
3344
3345static void mwl8k_remove_interface(struct ieee80211_hw *hw,
Johannes Berg1ed32e42009-12-23 13:15:45 +01003346 struct ieee80211_vif *vif)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003347{
3348 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003349 struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003350
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003351 if (priv->ap_fw)
3352 mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
3353
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003354 mwl8k_cmd_set_mac_addr(hw, vif, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02003355
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01003356 priv->macids_used &= ~(1 << mwl8k_vif->macid);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003357 list_del(&mwl8k_vif->list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003358}
3359
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003360static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003361{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003362 struct ieee80211_conf *conf = &hw->conf;
3363 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003364 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003365
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003366 if (conf->flags & IEEE80211_CONF_IDLE) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003367 mwl8k_cmd_radio_disable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003368 return 0;
Lennert Buytenhek7595d672009-08-17 23:59:40 +02003369 }
3370
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003371 rc = mwl8k_fw_lock(hw);
3372 if (rc)
3373 return rc;
3374
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003375 rc = mwl8k_cmd_radio_enable(hw);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003376 if (rc)
3377 goto out;
3378
Lennert Buytenhek610677d2010-01-04 21:56:46 +01003379 rc = mwl8k_cmd_set_rf_channel(hw, conf);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003380 if (rc)
3381 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003382
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003383 if (conf->power_level > 18)
3384 conf->power_level = 18;
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003385 rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003386 if (rc)
3387 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003388
Lennert Buytenhek08b06342009-10-22 20:21:33 +02003389 if (priv->ap_fw) {
3390 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x7);
3391 if (!rc)
3392 rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
3393 } else {
3394 rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
3395 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003396
Lennert Buytenhekee03a932009-07-17 07:19:37 +02003397out:
3398 mwl8k_fw_unlock(hw);
3399
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003400 return rc;
3401}
3402
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003403static void
3404mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3405 struct ieee80211_bss_conf *info, u32 changed)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003406{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003407 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003408 u32 ap_legacy_rates;
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003409 u8 ap_mcs_rates[16];
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003410 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003411
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003412 if (mwl8k_fw_lock(hw))
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003413 return;
3414
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003415 /*
3416 * No need to capture a beacon if we're no longer associated.
3417 */
3418 if ((changed & BSS_CHANGED_ASSOC) && !vif->bss_conf.assoc)
3419 priv->capture_beacon = false;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003420
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003421 /*
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003422 * Get the AP's legacy and MCS rates.
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003423 */
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003424 if (vif->bss_conf.assoc) {
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003425 struct ieee80211_sta *ap;
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003426
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003427 rcu_read_lock();
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003428
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003429 ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003430 if (ap == NULL) {
3431 rcu_read_unlock();
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003432 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003433 }
Lennert Buytenhekc6e96012010-01-04 21:55:52 +01003434
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003435 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ) {
3436 ap_legacy_rates = ap->supp_rates[IEEE80211_BAND_2GHZ];
3437 } else {
3438 ap_legacy_rates =
3439 ap->supp_rates[IEEE80211_BAND_5GHZ] << 5;
3440 }
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003441 memcpy(ap_mcs_rates, ap->ht_cap.mcs.rx_mask, 16);
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003442
3443 rcu_read_unlock();
3444 }
3445
3446 if ((changed & BSS_CHANGED_ASSOC) && vif->bss_conf.assoc) {
Lennert Buytenhek13935e22010-01-04 21:57:59 +01003447 rc = mwl8k_cmd_set_rate(hw, vif, ap_legacy_rates, ap_mcs_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003448 if (rc)
3449 goto out;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003450
Lennert Buytenhekb71ed2c2010-01-08 18:30:16 +01003451 rc = mwl8k_cmd_use_fixed_rate_sta(hw);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003452 if (rc)
3453 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003454 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003455
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003456 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003457 rc = mwl8k_set_radio_preamble(hw,
3458 vif->bss_conf.use_short_preamble);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003459 if (rc)
3460 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003461 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003462
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003463 if (changed & BSS_CHANGED_ERP_SLOT) {
Lennert Buytenhek7dc6a7a2009-11-30 18:33:09 +01003464 rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003465 if (rc)
3466 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003467 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003468
Lennert Buytenhekc97470d2010-01-12 13:47:37 +01003469 if (vif->bss_conf.assoc &&
3470 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
3471 BSS_CHANGED_HT))) {
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003472 rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003473 if (rc)
3474 goto out;
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003475 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003476
Lennert Buytenhekc3cbbe82010-01-04 21:56:07 +01003477 if (vif->bss_conf.assoc &&
3478 (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003479 /*
3480 * Finalize the join. Tell rx handler to process
3481 * next beacon from our BSSID.
3482 */
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003483 memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003484 priv->capture_beacon = true;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003485 }
3486
Lennert Buytenhek3a980d02009-07-17 07:21:46 +02003487out:
3488 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003489}
3490
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003491static void
3492mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3493 struct ieee80211_bss_conf *info, u32 changed)
3494{
3495 int rc;
3496
3497 if (mwl8k_fw_lock(hw))
3498 return;
3499
3500 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3501 rc = mwl8k_set_radio_preamble(hw,
3502 vif->bss_conf.use_short_preamble);
3503 if (rc)
3504 goto out;
3505 }
3506
3507 if (changed & BSS_CHANGED_BASIC_RATES) {
3508 int idx;
3509 int rate;
3510
3511 /*
3512 * Use lowest supported basic rate for multicasts
3513 * and management frames (such as probe responses --
3514 * beacons will always go out at 1 Mb/s).
3515 */
3516 idx = ffs(vif->bss_conf.basic_rates);
Lennert Buytenhek8707d022010-01-12 13:49:15 +01003517 if (idx)
3518 idx--;
3519
3520 if (hw->conf.channel->band == IEEE80211_BAND_2GHZ)
3521 rate = mwl8k_rates_24[idx].hw_value;
3522 else
3523 rate = mwl8k_rates_50[idx].hw_value;
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003524
3525 mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
3526 }
3527
3528 if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
3529 struct sk_buff *skb;
3530
3531 skb = ieee80211_beacon_get(hw, vif);
3532 if (skb != NULL) {
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003533 mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003534 kfree_skb(skb);
3535 }
3536 }
3537
3538 if (changed & BSS_CHANGED_BEACON_ENABLED)
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01003539 mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
Lennert Buytenhekb64fe612010-01-08 18:31:39 +01003540
3541out:
3542 mwl8k_fw_unlock(hw);
3543}
3544
3545static void
3546mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3547 struct ieee80211_bss_conf *info, u32 changed)
3548{
3549 struct mwl8k_priv *priv = hw->priv;
3550
3551 if (!priv->ap_fw)
3552 mwl8k_bss_info_changed_sta(hw, vif, info, changed);
3553 else
3554 mwl8k_bss_info_changed_ap(hw, vif, info, changed);
3555}
3556
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003557static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
Jiri Pirko22bedad2010-04-01 21:22:57 +00003558 struct netdev_hw_addr_list *mc_list)
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003559{
3560 struct mwl8k_cmd_pkt *cmd;
3561
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003562 /*
3563 * Synthesize and return a command packet that programs the
3564 * hardware multicast address filter. At this point we don't
3565 * know whether FIF_ALLMULTI is being requested, but if it is,
3566 * we'll end up throwing this packet away and creating a new
3567 * one in mwl8k_configure_filter().
3568 */
Jiri Pirko22bedad2010-04-01 21:22:57 +00003569 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
Lennert Buytenheke81cd2d2009-08-18 03:55:42 +02003570
3571 return (unsigned long)cmd;
3572}
3573
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003574static int
3575mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
3576 unsigned int changed_flags,
3577 unsigned int *total_flags)
3578{
3579 struct mwl8k_priv *priv = hw->priv;
3580
3581 /*
3582 * Hardware sniffer mode is mutually exclusive with STA
3583 * operation, so refuse to enable sniffer mode if a STA
3584 * interface is active.
3585 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003586 if (!list_empty(&priv->vif_list)) {
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003587 if (net_ratelimit())
Joe Perchesc96c31e2010-07-26 14:39:58 -07003588 wiphy_info(hw->wiphy,
3589 "not enabling sniffer mode because STA interface is active\n");
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003590 return 0;
3591 }
3592
3593 if (!priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003594 if (mwl8k_cmd_enable_sniffer(hw, 1))
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003595 return 0;
3596 priv->sniffer_enabled = true;
3597 }
3598
3599 *total_flags &= FIF_PROMISC_IN_BSS | FIF_ALLMULTI |
3600 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
3601 FIF_OTHER_BSS;
3602
3603 return 1;
3604}
3605
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003606static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
3607{
3608 if (!list_empty(&priv->vif_list))
3609 return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
3610
3611 return NULL;
3612}
3613
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003614static void mwl8k_configure_filter(struct ieee80211_hw *hw,
3615 unsigned int changed_flags,
3616 unsigned int *total_flags,
3617 u64 multicast)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003618{
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003619 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003620 struct mwl8k_cmd_pkt *cmd = (void *)(unsigned long)multicast;
3621
3622 /*
Lennert Buytenhekc0adae22009-10-22 20:21:36 +02003623 * AP firmware doesn't allow fine-grained control over
3624 * the receive filter.
3625 */
3626 if (priv->ap_fw) {
3627 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
3628 kfree(cmd);
3629 return;
3630 }
3631
3632 /*
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003633 * Enable hardware sniffer mode if FIF_CONTROL or
3634 * FIF_OTHER_BSS is requested.
3635 */
3636 if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
3637 mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
3638 kfree(cmd);
3639 return;
3640 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003641
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003642 /* Clear unsupported feature flags */
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003643 *total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003644
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003645 if (mwl8k_fw_lock(hw)) {
3646 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003647 return;
Lennert Buytenhek90852f72010-01-02 10:31:42 +01003648 }
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003649
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003650 if (priv->sniffer_enabled) {
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003651 mwl8k_cmd_enable_sniffer(hw, 0);
Lennert Buytenheka43c49a2009-10-22 20:20:32 +02003652 priv->sniffer_enabled = false;
3653 }
3654
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003655 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003656 if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
3657 /*
3658 * Disable the BSS filter.
3659 */
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003660 mwl8k_cmd_set_pre_scan(hw);
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003661 } else {
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003662 struct mwl8k_vif *mwl8k_vif;
Lennert Buytenhek0a11dfc2010-01-04 21:55:21 +01003663 const u8 *bssid;
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003664
Lennert Buytenhek77165d82009-10-22 20:19:53 +02003665 /*
3666 * Enable the BSS filter.
3667 *
3668 * If there is an active STA interface, use that
3669 * interface's BSSID, otherwise use a dummy one
3670 * (where the OUI part needs to be nonzero for
3671 * the BSSID to be accepted by POST_SCAN).
3672 */
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003673 mwl8k_vif = mwl8k_first_vif(priv);
3674 if (mwl8k_vif != NULL)
3675 bssid = mwl8k_vif->vif->bss_conf.bssid;
3676 else
3677 bssid = "\x01\x00\x00\x00\x00\x00";
Lennert Buytenheka94cc972009-08-03 21:58:57 +02003678
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003679 mwl8k_cmd_set_post_scan(hw, bssid);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003680 }
3681 }
3682
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003683 /*
3684 * If FIF_ALLMULTI is being requested, throw away the command
3685 * packet that ->prepare_multicast() built and replace it with
3686 * a command packet that enables reception of all multicast
3687 * packets.
3688 */
3689 if (*total_flags & FIF_ALLMULTI) {
3690 kfree(cmd);
Jiri Pirko22bedad2010-04-01 21:22:57 +00003691 cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
Lennert Buytenhek447ced02009-10-22 20:19:50 +02003692 }
3693
3694 if (cmd != NULL) {
3695 mwl8k_post_cmd(hw, cmd);
3696 kfree(cmd);
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003697 }
Lennert Buytenhekce9e2e12009-07-16 14:00:45 +02003698
Lennert Buytenheke6935ea2009-08-18 04:06:20 +02003699 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003700}
3701
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003702static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3703{
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01003704 return mwl8k_cmd_set_rts_threshold(hw, value);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003705}
3706
Johannes Berg4a6967b2010-02-19 19:18:37 +01003707static int mwl8k_sta_remove(struct ieee80211_hw *hw,
3708 struct ieee80211_vif *vif,
3709 struct ieee80211_sta *sta)
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003710{
3711 struct mwl8k_priv *priv = hw->priv;
3712
Johannes Berg4a6967b2010-02-19 19:18:37 +01003713 if (priv->ap_fw)
3714 return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
3715 else
3716 return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
3717}
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003718
Johannes Berg4a6967b2010-02-19 19:18:37 +01003719static int mwl8k_sta_add(struct ieee80211_hw *hw,
3720 struct ieee80211_vif *vif,
3721 struct ieee80211_sta *sta)
3722{
3723 struct mwl8k_priv *priv = hw->priv;
3724 int ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003725
Johannes Berg4a6967b2010-02-19 19:18:37 +01003726 if (!priv->ap_fw) {
3727 ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
3728 if (ret >= 0) {
3729 MWL8K_STA(sta)->peer_id = ret;
3730 return 0;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003731 }
Johannes Berg4a6967b2010-02-19 19:18:37 +01003732
3733 return ret;
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003734 }
Lennert Buytenhek3f5610f2010-01-08 18:30:58 +01003735
Johannes Berg4a6967b2010-02-19 19:18:37 +01003736 return mwl8k_cmd_set_new_stn_add(hw, vif, sta);
Lennert Buytenhekbbfd9122010-01-04 21:55:12 +01003737}
3738
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003739static int mwl8k_conf_tx(struct ieee80211_hw *hw, u16 queue,
3740 const struct ieee80211_tx_queue_params *params)
3741{
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003742 struct mwl8k_priv *priv = hw->priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003743 int rc;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003744
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003745 rc = mwl8k_fw_lock(hw);
3746 if (!rc) {
3747 if (!priv->wmm_enabled)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003748 rc = mwl8k_cmd_set_wmm_mode(hw, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003749
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003750 if (!rc)
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003751 rc = mwl8k_cmd_set_edca_params(hw, queue,
3752 params->cw_min,
3753 params->cw_max,
3754 params->aifs,
3755 params->txop);
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003756
3757 mwl8k_fw_unlock(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003758 }
Lennert Buytenhek3e4f5422009-07-17 07:25:59 +02003759
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003760 return rc;
3761}
3762
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003763static int mwl8k_get_stats(struct ieee80211_hw *hw,
3764 struct ieee80211_low_level_stats *stats)
3765{
Lennert Buytenhek55489b62009-11-30 18:31:33 +01003766 return mwl8k_cmd_get_stat(hw, stats);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003767}
3768
John W. Linville0d462bb2010-07-28 14:04:24 -04003769static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
3770 struct survey_info *survey)
3771{
3772 struct mwl8k_priv *priv = hw->priv;
3773 struct ieee80211_conf *conf = &hw->conf;
3774
3775 if (idx != 0)
3776 return -ENOENT;
3777
3778 survey->channel = conf->channel;
3779 survey->filled = SURVEY_INFO_NOISE_DBM;
3780 survey->noise = priv->noise;
3781
3782 return 0;
3783}
3784
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003785static int
3786mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3787 enum ieee80211_ampdu_mlme_action action,
3788 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
3789{
3790 switch (action) {
3791 case IEEE80211_AMPDU_RX_START:
3792 case IEEE80211_AMPDU_RX_STOP:
3793 if (!(hw->flags & IEEE80211_HW_AMPDU_AGGREGATION))
3794 return -ENOTSUPP;
3795 return 0;
3796 default:
3797 return -ENOTSUPP;
3798 }
3799}
3800
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003801static const struct ieee80211_ops mwl8k_ops = {
3802 .tx = mwl8k_tx,
3803 .start = mwl8k_start,
3804 .stop = mwl8k_stop,
3805 .add_interface = mwl8k_add_interface,
3806 .remove_interface = mwl8k_remove_interface,
3807 .config = mwl8k_config,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003808 .bss_info_changed = mwl8k_bss_info_changed,
Johannes Berg3ac64be2009-08-17 16:16:53 +02003809 .prepare_multicast = mwl8k_prepare_multicast,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003810 .configure_filter = mwl8k_configure_filter,
3811 .set_rts_threshold = mwl8k_set_rts_threshold,
Johannes Berg4a6967b2010-02-19 19:18:37 +01003812 .sta_add = mwl8k_sta_add,
3813 .sta_remove = mwl8k_sta_remove,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003814 .conf_tx = mwl8k_conf_tx,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003815 .get_stats = mwl8k_get_stats,
John W. Linville0d462bb2010-07-28 14:04:24 -04003816 .get_survey = mwl8k_get_survey,
Lennert Buytenheka2292d82010-01-04 21:58:12 +01003817 .ampdu_action = mwl8k_ampdu_action,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003818};
3819
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003820static void mwl8k_finalize_join_worker(struct work_struct *work)
3821{
3822 struct mwl8k_priv *priv =
3823 container_of(work, struct mwl8k_priv, finalize_join_worker);
3824 struct sk_buff *skb = priv->beacon_skb;
Johannes Berg56007a02010-01-26 14:19:52 +01003825 struct ieee80211_mgmt *mgmt = (void *)skb->data;
3826 int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
3827 const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
3828 mgmt->u.beacon.variable, len);
3829 int dtim_period = 1;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003830
Johannes Berg56007a02010-01-26 14:19:52 +01003831 if (tim && tim[1] >= 2)
3832 dtim_period = tim[3];
3833
3834 mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01003835
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003836 dev_kfree_skb(skb);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003837 priv->beacon_skb = NULL;
3838}
3839
John W. Linvillebcb628d2009-11-06 16:40:16 -05003840enum {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003841 MWL8363 = 0,
3842 MWL8687,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003843 MWL8366,
Lennert Buytenhek6f6d1e92009-10-22 20:21:48 +02003844};
3845
John W. Linvillebcb628d2009-11-06 16:40:16 -05003846static struct mwl8k_device_info mwl8k_info_tbl[] __devinitdata = {
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003847 [MWL8363] = {
3848 .part_name = "88w8363",
3849 .helper_image = "mwl8k/helper_8363.fw",
3850 .fw_image = "mwl8k/fmimage_8363.fw",
3851 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003852 [MWL8687] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003853 .part_name = "88w8687",
3854 .helper_image = "mwl8k/helper_8687.fw",
3855 .fw_image = "mwl8k/fmimage_8687.fw",
John W. Linvillebcb628d2009-11-06 16:40:16 -05003856 },
Lennert Buytenhek49eb6912009-11-30 18:32:13 +01003857 [MWL8366] = {
John W. Linvillebcb628d2009-11-06 16:40:16 -05003858 .part_name = "88w8366",
3859 .helper_image = "mwl8k/helper_8366.fw",
3860 .fw_image = "mwl8k/fmimage_8366.fw",
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003861 .ap_rxd_ops = &rxd_8366_ap_ops,
John W. Linvillebcb628d2009-11-06 16:40:16 -05003862 },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003863};
3864
Lennert Buytenhekc92d4ed2010-01-12 13:47:22 +01003865MODULE_FIRMWARE("mwl8k/helper_8363.fw");
3866MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
3867MODULE_FIRMWARE("mwl8k/helper_8687.fw");
3868MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
3869MODULE_FIRMWARE("mwl8k/helper_8366.fw");
3870MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
3871
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003872static DEFINE_PCI_DEVICE_TABLE(mwl8k_pci_id_table) = {
Benjamin Larssone5868ba2010-03-19 01:46:10 +01003873 { PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
Lennert Buytenhek9e1b17e2010-01-04 21:56:19 +01003874 { PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
3875 { PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003876 { PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
3877 { PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
3878 { PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
Lennert Buytenhekca665272010-01-12 13:47:53 +01003879 { PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
John W. Linvillebcb628d2009-11-06 16:40:16 -05003880 { },
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02003881};
3882MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
3883
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003884static int __devinit mwl8k_probe(struct pci_dev *pdev,
3885 const struct pci_device_id *id)
3886{
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003887 static int printed_version = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003888 struct ieee80211_hw *hw;
3889 struct mwl8k_priv *priv;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003890 int rc;
3891 int i;
Lennert Buytenhek2aa7b012009-08-24 15:48:07 +02003892
3893 if (!printed_version) {
3894 printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
3895 printed_version = 1;
3896 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003897
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003898
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003899 rc = pci_enable_device(pdev);
3900 if (rc) {
3901 printk(KERN_ERR "%s: Cannot enable new PCI device\n",
3902 MWL8K_NAME);
3903 return rc;
3904 }
3905
3906 rc = pci_request_regions(pdev, MWL8K_NAME);
3907 if (rc) {
3908 printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
3909 MWL8K_NAME);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01003910 goto err_disable_device;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003911 }
3912
3913 pci_set_master(pdev);
3914
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003915
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003916 hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
3917 if (hw == NULL) {
3918 printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
3919 rc = -ENOMEM;
3920 goto err_free_reg;
3921 }
3922
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003923 SET_IEEE80211_DEV(hw, &pdev->dev);
3924 pci_set_drvdata(pdev, hw);
3925
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003926 priv = hw->priv;
3927 priv->hw = hw;
3928 priv->pdev = pdev;
John W. Linvillebcb628d2009-11-06 16:40:16 -05003929 priv->device_info = &mwl8k_info_tbl[id->driver_data];
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003930
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003931
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003932 priv->sram = pci_iomap(pdev, 0, 0x10000);
3933 if (priv->sram == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07003934 wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003935 goto err_iounmap;
3936 }
3937
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003938 /*
3939 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
3940 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
3941 */
3942 priv->regs = pci_iomap(pdev, 1, 0x10000);
3943 if (priv->regs == NULL) {
3944 priv->regs = pci_iomap(pdev, 2, 0x10000);
3945 if (priv->regs == NULL) {
Joe Perches5db55842010-08-11 19:11:19 -07003946 wiphy_err(hw->wiphy, "Cannot map device registers\n");
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02003947 goto err_iounmap;
3948 }
3949 }
3950
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003951
3952 /* Reset firmware and hardware */
3953 mwl8k_hw_reset(priv);
3954
3955 /* Ask userland hotplug daemon for the device firmware */
3956 rc = mwl8k_request_firmware(priv);
3957 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07003958 wiphy_err(hw->wiphy, "Firmware files not found\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003959 goto err_stop_firmware;
3960 }
3961
3962 /* Load firmware into hardware */
3963 rc = mwl8k_load_firmware(hw);
3964 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07003965 wiphy_err(hw->wiphy, "Cannot start firmware\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003966 goto err_stop_firmware;
3967 }
3968
3969 /* Reclaim memory once firmware is successfully loaded */
3970 mwl8k_release_firmware(priv);
3971
3972
Lennert Buytenhek91942232010-01-04 21:53:54 +01003973 if (priv->ap_fw) {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003974 priv->rxd_ops = priv->device_info->ap_rxd_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003975 if (priv->rxd_ops == NULL) {
Joe Perchesc96c31e2010-07-26 14:39:58 -07003976 wiphy_err(hw->wiphy,
3977 "Driver does not have AP firmware image support for this hardware\n");
Lennert Buytenhek91942232010-01-04 21:53:54 +01003978 goto err_stop_firmware;
3979 }
3980 } else {
Lennert Buytenhek89a91f42009-11-30 18:32:54 +01003981 priv->rxd_ops = &rxd_sta_ops;
Lennert Buytenhek91942232010-01-04 21:53:54 +01003982 }
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01003983
3984 priv->sniffer_enabled = false;
3985 priv->wmm_enabled = false;
3986 priv->pending_tx_pkts = 0;
3987
3988
Lennert Buytenheka66098d2009-03-10 10:13:33 +01003989 /*
3990 * Extra headroom is the size of the required DMA header
3991 * minus the size of the smallest 802.11 frame (CTS frame).
3992 */
3993 hw->extra_tx_headroom =
3994 sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
3995
3996 hw->channel_change_time = 10;
3997
3998 hw->queues = MWL8K_TX_QUEUES;
3999
John W. Linvillef5c044e2010-04-30 15:37:00 -04004000 /* Set rssi values to dBm */
4001 hw->flags |= IEEE80211_HW_SIGNAL_DBM;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004002 hw->vif_data_size = sizeof(struct mwl8k_vif);
Lennert Buytenheka6804002010-01-04 21:55:42 +01004003 hw->sta_data_size = sizeof(struct mwl8k_sta);
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004004
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004005 priv->macids_used = 0;
Lennert Buytenhekf5bb87c2010-01-12 13:49:51 +01004006 INIT_LIST_HEAD(&priv->vif_list);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004007
4008 /* Set default radio state and preamble */
Lennert Buytenhekc46563b2009-07-16 12:14:58 +02004009 priv->radio_on = 0;
Lennert Buytenhek68ce3882009-07-16 12:26:57 +02004010 priv->radio_short_preamble = 0;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004011
4012 /* Finalize join worker */
4013 INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
4014
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004015 /* TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004016 tasklet_init(&priv->poll_tx_task, mwl8k_tx_poll, (unsigned long)hw);
4017 tasklet_disable(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004018 tasklet_init(&priv->poll_rx_task, mwl8k_rx_poll, (unsigned long)hw);
4019 tasklet_disable(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004020
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004021 /* Power management cookie */
4022 priv->cookie = pci_alloc_consistent(priv->pdev, 4, &priv->cookie_dma);
4023 if (priv->cookie == NULL)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004024 goto err_stop_firmware;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004025
4026 rc = mwl8k_rxq_init(hw, 0);
4027 if (rc)
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004028 goto err_free_cookie;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004029 rxq_refill(hw, 0, INT_MAX);
4030
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004031 mutex_init(&priv->fw_mutex);
4032 priv->fw_mutex_owner = NULL;
4033 priv->fw_mutex_depth = 0;
Lennert Buytenhek618952a2009-08-18 03:18:01 +02004034 priv->hostcmd_wait = NULL;
4035
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004036 spin_lock_init(&priv->tx_lock);
4037
Lennert Buytenhek88de754a2009-10-22 20:19:37 +02004038 priv->tx_wait = NULL;
4039
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004040 for (i = 0; i < MWL8K_TX_QUEUES; i++) {
4041 rc = mwl8k_txq_init(hw, i);
4042 if (rc)
4043 goto err_free_queues;
4044 }
4045
4046 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004047 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004048 iowrite32(MWL8K_A2H_INT_TX_DONE | MWL8K_A2H_INT_RX_READY,
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004049 priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004050 iowrite32(0xffffffff, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4051
Joe Perchesa0607fd2009-11-18 23:29:17 -08004052 rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004053 IRQF_SHARED, MWL8K_NAME, hw);
4054 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004055 wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004056 goto err_free_queues;
4057 }
4058
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004059 /*
4060 * Temporarily enable interrupts. Initial firmware host
Lennert Buytenhekc2c2b122010-01-08 18:27:59 +01004061 * commands use interrupts and avoid polling. Disable
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004062 * interrupts when done.
4063 */
Lennert Buytenhekc23b5a62009-07-16 13:49:55 +02004064 iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004065
4066 /* Get config data, mac addrs etc */
Lennert Buytenhek42fba212009-10-22 20:21:30 +02004067 if (priv->ap_fw) {
4068 rc = mwl8k_cmd_get_hw_spec_ap(hw);
4069 if (!rc)
4070 rc = mwl8k_cmd_set_hw_spec(hw);
4071 } else {
4072 rc = mwl8k_cmd_get_hw_spec_sta(hw);
4073 }
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004074 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004075 wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004076 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004077 }
4078
Lennert Buytenhekee0ddf12010-01-12 13:51:30 +01004079 hw->wiphy->interface_modes = 0;
4080 if (priv->ap_macids_supported)
4081 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
4082 if (priv->sta_macids_supported)
4083 hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
4084
4085
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004086 /* Turn radio off */
Lennert Buytenhek55489b62009-11-30 18:31:33 +01004087 rc = mwl8k_cmd_radio_disable(hw);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004088 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004089 wiphy_err(hw->wiphy, "Cannot disable\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004090 goto err_free_irq;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004091 }
4092
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004093 /* Clear MAC address */
Lennert Buytenhekaa21d0f2010-01-12 13:50:36 +01004094 rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004095 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004096 wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004097 goto err_free_irq;
Lennert Buytenhek32060e12009-10-22 20:20:04 +02004098 }
4099
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004100 /* Disable interrupts */
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004101 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004102 free_irq(priv->pdev->irq, hw);
4103
4104 rc = ieee80211_register_hw(hw);
4105 if (rc) {
Joe Perches5db55842010-08-11 19:11:19 -07004106 wiphy_err(hw->wiphy, "Cannot register device\n");
Lennert Buytenhek153458f2010-01-04 21:54:08 +01004107 goto err_free_queues;
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004108 }
4109
Joe Perchesc96c31e2010-07-26 14:39:58 -07004110 wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
4111 priv->device_info->part_name,
4112 priv->hw_rev, hw->wiphy->perm_addr,
4113 priv->ap_fw ? "AP" : "STA",
4114 (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
4115 (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004116
4117 return 0;
4118
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004119err_free_irq:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004120 iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004121 free_irq(priv->pdev->irq, hw);
4122
4123err_free_queues:
4124 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4125 mwl8k_txq_deinit(hw, i);
4126 mwl8k_rxq_deinit(hw, 0);
4127
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004128err_free_cookie:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004129 if (priv->cookie != NULL)
4130 pci_free_consistent(priv->pdev, 4,
4131 priv->cookie, priv->cookie_dma);
4132
Lennert Buytenhekbe695fc2009-11-30 18:32:46 +01004133err_stop_firmware:
4134 mwl8k_hw_reset(priv);
4135 mwl8k_release_firmware(priv);
4136
4137err_iounmap:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004138 if (priv->regs != NULL)
4139 pci_iounmap(pdev, priv->regs);
4140
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004141 if (priv->sram != NULL)
4142 pci_iounmap(pdev, priv->sram);
4143
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004144 pci_set_drvdata(pdev, NULL);
4145 ieee80211_free_hw(hw);
4146
4147err_free_reg:
4148 pci_release_regions(pdev);
Lennert Buytenhek3db95e52009-11-30 18:13:34 +01004149
4150err_disable_device:
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004151 pci_disable_device(pdev);
4152
4153 return rc;
4154}
4155
Joerg Albert230f7af2009-04-18 02:10:45 +02004156static void __devexit mwl8k_shutdown(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004157{
4158 printk(KERN_ERR "===>%s(%u)\n", __func__, __LINE__);
4159}
4160
Joerg Albert230f7af2009-04-18 02:10:45 +02004161static void __devexit mwl8k_remove(struct pci_dev *pdev)
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004162{
4163 struct ieee80211_hw *hw = pci_get_drvdata(pdev);
4164 struct mwl8k_priv *priv;
4165 int i;
4166
4167 if (hw == NULL)
4168 return;
4169 priv = hw->priv;
4170
4171 ieee80211_stop_queues(hw);
4172
Lennert Buytenhek60aa5692009-08-03 21:59:09 +02004173 ieee80211_unregister_hw(hw);
4174
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004175 /* Remove TX reclaim and RX tasklets. */
Lennert Buytenhek1e9f9de32010-01-08 18:32:01 +01004176 tasklet_kill(&priv->poll_tx_task);
Lennert Buytenhek67e2eb22010-01-08 18:32:18 +01004177 tasklet_kill(&priv->poll_rx_task);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004178
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004179 /* Stop hardware */
4180 mwl8k_hw_reset(priv);
4181
4182 /* Return all skbs to mac80211 */
4183 for (i = 0; i < MWL8K_TX_QUEUES; i++)
Lennert Buytenhekefb7c492010-01-08 18:31:47 +01004184 mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004185
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004186 for (i = 0; i < MWL8K_TX_QUEUES; i++)
4187 mwl8k_txq_deinit(hw, i);
4188
4189 mwl8k_rxq_deinit(hw, 0);
4190
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004191 pci_free_consistent(priv->pdev, 4, priv->cookie, priv->cookie_dma);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004192
4193 pci_iounmap(pdev, priv->regs);
Lennert Buytenhek5b9482d2009-10-22 20:20:43 +02004194 pci_iounmap(pdev, priv->sram);
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004195 pci_set_drvdata(pdev, NULL);
4196 ieee80211_free_hw(hw);
4197 pci_release_regions(pdev);
4198 pci_disable_device(pdev);
4199}
4200
4201static struct pci_driver mwl8k_driver = {
4202 .name = MWL8K_NAME,
Lennert Buytenhek45a390d2009-10-22 20:20:47 +02004203 .id_table = mwl8k_pci_id_table,
Lennert Buytenheka66098d2009-03-10 10:13:33 +01004204 .probe = mwl8k_probe,
4205 .remove = __devexit_p(mwl8k_remove),
4206 .shutdown = __devexit_p(mwl8k_shutdown),
4207};
4208
4209static int __init mwl8k_init(void)
4210{
4211 return pci_register_driver(&mwl8k_driver);
4212}
4213
4214static void __exit mwl8k_exit(void)
4215{
4216 pci_unregister_driver(&mwl8k_driver);
4217}
4218
4219module_init(mwl8k_init);
4220module_exit(mwl8k_exit);
Lennert Buytenhekc2c357c2009-10-22 20:19:33 +02004221
4222MODULE_DESCRIPTION(MWL8K_DESC);
4223MODULE_VERSION(MWL8K_VERSION);
4224MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
4225MODULE_LICENSE("GPL");