xref: /linux/drivers/net/wireless/marvell/mwl8k.c (revision 2a52ca7c98960aafb0eca9ef96b2d0c932171357)
1 /*
2  * drivers/net/wireless/mwl8k.c
3  * Driver for Marvell TOPDOG 802.11 Wireless cards
4  *
5  * Copyright (C) 2008, 2009, 2010 Marvell Semiconductor Inc.
6  *
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/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/spinlock.h>
17 #include <linux/list.h>
18 #include <linux/pci.h>
19 #include <linux/delay.h>
20 #include <linux/completion.h>
21 #include <linux/etherdevice.h>
22 #include <linux/slab.h>
23 #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
30 #define MWL8K_VERSION	"0.13"
31 
32 /* Module parameters */
33 static bool ap_mode_default;
34 module_param(ap_mode_default, bool, 0);
35 MODULE_PARM_DESC(ap_mode_default,
36 		 "Set to 1 to make ap mode the default instead of sta mode");
37 
38 /* Register definitions */
39 #define MWL8K_HIU_GEN_PTR			0x00000c10
40 #define  MWL8K_MODE_STA				 0x0000005a
41 #define  MWL8K_MODE_AP				 0x000000a5
42 #define MWL8K_HIU_INT_CODE			0x00000c14
43 #define  MWL8K_FWSTA_READY			 0xf0f1f2f4
44 #define  MWL8K_FWAP_READY			 0xf1f2f4a5
45 #define  MWL8K_INT_CODE_CMD_FINISHED		 0x00000005
46 #define MWL8K_HIU_SCRATCH			0x00000c40
47 
48 /* Host->device communications */
49 #define MWL8K_HIU_H2A_INTERRUPT_EVENTS		0x00000c18
50 #define MWL8K_HIU_H2A_INTERRUPT_STATUS		0x00000c1c
51 #define MWL8K_HIU_H2A_INTERRUPT_MASK		0x00000c20
52 #define MWL8K_HIU_H2A_INTERRUPT_CLEAR_SEL	0x00000c24
53 #define MWL8K_HIU_H2A_INTERRUPT_STATUS_MASK	0x00000c28
54 #define  MWL8K_H2A_INT_DUMMY			 (1 << 20)
55 #define  MWL8K_H2A_INT_RESET			 (1 << 15)
56 #define  MWL8K_H2A_INT_DOORBELL			 (1 << 1)
57 #define  MWL8K_H2A_INT_PPA_READY		 (1 << 0)
58 
59 /* Device->host communications */
60 #define MWL8K_HIU_A2H_INTERRUPT_EVENTS		0x00000c2c
61 #define MWL8K_HIU_A2H_INTERRUPT_STATUS		0x00000c30
62 #define MWL8K_HIU_A2H_INTERRUPT_MASK		0x00000c34
63 #define MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL	0x00000c38
64 #define MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK	0x00000c3c
65 #define  MWL8K_A2H_INT_DUMMY			 (1 << 20)
66 #define  MWL8K_A2H_INT_BA_WATCHDOG		 (1 << 14)
67 #define  MWL8K_A2H_INT_CHNL_SWITCHED		 (1 << 11)
68 #define  MWL8K_A2H_INT_QUEUE_EMPTY		 (1 << 10)
69 #define  MWL8K_A2H_INT_RADAR_DETECT		 (1 << 7)
70 #define  MWL8K_A2H_INT_RADIO_ON			 (1 << 6)
71 #define  MWL8K_A2H_INT_RADIO_OFF		 (1 << 5)
72 #define  MWL8K_A2H_INT_MAC_EVENT		 (1 << 3)
73 #define  MWL8K_A2H_INT_OPC_DONE			 (1 << 2)
74 #define  MWL8K_A2H_INT_RX_READY			 (1 << 1)
75 #define  MWL8K_A2H_INT_TX_DONE			 (1 << 0)
76 
77 /* HW micro second timer register
78  * located at offset 0xA600. This
79  * will be used to timestamp tx
80  * packets.
81  */
82 
83 #define	MWL8K_HW_TIMER_REGISTER			0x0000a600
84 #define BBU_RXRDY_CNT_REG			0x0000a860
85 #define NOK_CCA_CNT_REG				0x0000a6a0
86 #define BBU_AVG_NOISE_VAL			0x67
87 
88 #define MWL8K_A2H_EVENTS	(MWL8K_A2H_INT_DUMMY | \
89 				 MWL8K_A2H_INT_CHNL_SWITCHED | \
90 				 MWL8K_A2H_INT_QUEUE_EMPTY | \
91 				 MWL8K_A2H_INT_RADAR_DETECT | \
92 				 MWL8K_A2H_INT_RADIO_ON | \
93 				 MWL8K_A2H_INT_RADIO_OFF | \
94 				 MWL8K_A2H_INT_MAC_EVENT | \
95 				 MWL8K_A2H_INT_OPC_DONE | \
96 				 MWL8K_A2H_INT_RX_READY | \
97 				 MWL8K_A2H_INT_TX_DONE | \
98 				 MWL8K_A2H_INT_BA_WATCHDOG)
99 
100 #define MWL8K_RX_QUEUES		1
101 #define MWL8K_TX_WMM_QUEUES	4
102 #define MWL8K_MAX_AMPDU_QUEUES	8
103 #define MWL8K_MAX_TX_QUEUES	(MWL8K_TX_WMM_QUEUES + MWL8K_MAX_AMPDU_QUEUES)
104 #define mwl8k_tx_queues(priv)	(MWL8K_TX_WMM_QUEUES + (priv)->num_ampdu_queues)
105 
106 /* txpriorities are mapped with hw queues.
107  * Each hw queue has a txpriority.
108  */
109 #define TOTAL_HW_TX_QUEUES	8
110 
111 /* Each HW queue can have one AMPDU stream.
112  * But, because one of the hw queue is reserved,
113  * maximum AMPDU queues that can be created are
114  * one short of total tx queues.
115  */
116 #define MWL8K_NUM_AMPDU_STREAMS	(TOTAL_HW_TX_QUEUES - 1)
117 
118 #define MWL8K_NUM_CHANS 18
119 
120 struct rxd_ops {
121 	int rxd_size;
122 	void (*rxd_init)(void *rxd, dma_addr_t next_dma_addr);
123 	void (*rxd_refill)(void *rxd, dma_addr_t addr, int len);
124 	int (*rxd_process)(void *rxd, struct ieee80211_rx_status *status,
125 			   __le16 *qos, s8 *noise);
126 };
127 
128 struct mwl8k_device_info {
129 	char *part_name;
130 	char *helper_image;
131 	char *fw_image_sta;
132 	char *fw_image_ap;
133 	struct rxd_ops *ap_rxd_ops;
134 	u32 fw_api_ap;
135 };
136 
137 struct mwl8k_rx_queue {
138 	int rxd_count;
139 
140 	/* hw receives here */
141 	int head;
142 
143 	/* refill descs here */
144 	int tail;
145 
146 	void *rxd;
147 	dma_addr_t rxd_dma;
148 	struct {
149 		struct sk_buff *skb;
150 		DEFINE_DMA_UNMAP_ADDR(dma);
151 	} *buf;
152 };
153 
154 struct mwl8k_tx_queue {
155 	/* hw transmits here */
156 	int head;
157 
158 	/* sw appends here */
159 	int tail;
160 
161 	unsigned int len;
162 	struct mwl8k_tx_desc *txd;
163 	dma_addr_t txd_dma;
164 	struct sk_buff **skb;
165 };
166 
167 enum {
168 	AMPDU_NO_STREAM,
169 	AMPDU_STREAM_NEW,
170 	AMPDU_STREAM_IN_PROGRESS,
171 	AMPDU_STREAM_ACTIVE,
172 };
173 
174 struct mwl8k_ampdu_stream {
175 	struct ieee80211_sta *sta;
176 	u8 tid;
177 	u8 state;
178 	u8 idx;
179 };
180 
181 struct mwl8k_priv {
182 	struct ieee80211_hw *hw;
183 	struct pci_dev *pdev;
184 	int irq;
185 
186 	struct mwl8k_device_info *device_info;
187 
188 	void __iomem *sram;
189 	void __iomem *regs;
190 
191 	/* firmware */
192 	const struct firmware *fw_helper;
193 	const struct firmware *fw_ucode;
194 
195 	/* hardware/firmware parameters */
196 	bool ap_fw;
197 	struct rxd_ops *rxd_ops;
198 	struct ieee80211_supported_band band_24;
199 	struct ieee80211_channel channels_24[14];
200 	struct ieee80211_rate rates_24[13];
201 	struct ieee80211_supported_band band_50;
202 	struct ieee80211_channel channels_50[9];
203 	struct ieee80211_rate rates_50[8];
204 	u32 ap_macids_supported;
205 	u32 sta_macids_supported;
206 
207 	/* Ampdu stream information */
208 	u8 num_ampdu_queues;
209 	spinlock_t stream_lock;
210 	struct mwl8k_ampdu_stream ampdu[MWL8K_MAX_AMPDU_QUEUES];
211 	struct work_struct watchdog_ba_handle;
212 
213 	/* firmware access */
214 	struct mutex fw_mutex;
215 	struct task_struct *fw_mutex_owner;
216 	struct task_struct *hw_restart_owner;
217 	int fw_mutex_depth;
218 	struct completion *hostcmd_wait;
219 
220 	atomic_t watchdog_event_pending;
221 
222 	/* lock held over TX and TX reap */
223 	spinlock_t tx_lock;
224 
225 	/* TX quiesce completion, protected by fw_mutex and tx_lock */
226 	struct completion *tx_wait;
227 
228 	/* List of interfaces.  */
229 	u32 macids_used;
230 	struct list_head vif_list;
231 
232 	/* power management status cookie from firmware */
233 	u32 *cookie;
234 	dma_addr_t cookie_dma;
235 
236 	u16 num_mcaddrs;
237 	u8 hw_rev;
238 	u32 fw_rev;
239 	u32 caps;
240 
241 	/*
242 	 * Running count of TX packets in flight, to avoid
243 	 * iterating over the transmit rings each time.
244 	 */
245 	int pending_tx_pkts;
246 
247 	struct mwl8k_rx_queue rxq[MWL8K_RX_QUEUES];
248 	struct mwl8k_tx_queue txq[MWL8K_MAX_TX_QUEUES];
249 	u32 txq_offset[MWL8K_MAX_TX_QUEUES];
250 
251 	bool radio_on;
252 	bool radio_short_preamble;
253 	bool sniffer_enabled;
254 	bool wmm_enabled;
255 
256 	/* XXX need to convert this to handle multiple interfaces */
257 	bool capture_beacon;
258 	u8 capture_bssid[ETH_ALEN];
259 	struct sk_buff *beacon_skb;
260 
261 	/*
262 	 * This FJ worker has to be global as it is scheduled from the
263 	 * RX handler.  At this point we don't know which interface it
264 	 * belongs to until the list of bssids waiting to complete join
265 	 * is checked.
266 	 */
267 	struct work_struct finalize_join_worker;
268 
269 	/* Tasklet to perform TX reclaim.  */
270 	struct tasklet_struct poll_tx_task;
271 
272 	/* Tasklet to perform RX.  */
273 	struct tasklet_struct poll_rx_task;
274 
275 	/* Most recently reported noise in dBm */
276 	s8 noise;
277 
278 	/*
279 	 * preserve the queue configurations so they can be restored if/when
280 	 * the firmware image is swapped.
281 	 */
282 	struct ieee80211_tx_queue_params wmm_params[MWL8K_TX_WMM_QUEUES];
283 
284 	/* To perform the task of reloading the firmware */
285 	struct work_struct fw_reload;
286 	bool hw_restart_in_progress;
287 
288 	/* async firmware loading state */
289 	unsigned fw_state;
290 	char *fw_pref;
291 	char *fw_alt;
292 	bool is_8764;
293 	struct completion firmware_loading_complete;
294 
295 	/* bitmap of running BSSes */
296 	u32 running_bsses;
297 
298 	/* ACS related */
299 	bool sw_scan_start;
300 	struct ieee80211_channel *acs_chan;
301 	unsigned long channel_time;
302 	struct survey_info survey[MWL8K_NUM_CHANS];
303 };
304 
305 #define MAX_WEP_KEY_LEN         13
306 #define NUM_WEP_KEYS            4
307 
308 /* Per interface specific private data */
309 struct mwl8k_vif {
310 	struct list_head list;
311 	struct ieee80211_vif *vif;
312 
313 	/* Firmware macid for this vif.  */
314 	int macid;
315 
316 	/* Non AMPDU sequence number assigned by driver.  */
317 	u16 seqno;
318 
319 	/* Saved WEP keys */
320 	struct {
321 		u8 enabled;
322 		u8 key[sizeof(struct ieee80211_key_conf) + MAX_WEP_KEY_LEN];
323 	} wep_key_conf[NUM_WEP_KEYS];
324 
325 	/* BSSID */
326 	u8 bssid[ETH_ALEN];
327 
328 	/* A flag to indicate is HW crypto is enabled for this bssid */
329 	bool is_hw_crypto_enabled;
330 };
331 #define MWL8K_VIF(_vif) ((struct mwl8k_vif *)&((_vif)->drv_priv))
332 #define IEEE80211_KEY_CONF(_u8) ((struct ieee80211_key_conf *)(_u8))
333 
334 struct tx_traffic_info {
335 	u32 start_time;
336 	u32 pkts;
337 };
338 
339 #define MWL8K_MAX_TID 8
340 struct mwl8k_sta {
341 	/* Index into station database. Returned by UPDATE_STADB.  */
342 	u8 peer_id;
343 	u8 is_ampdu_allowed;
344 	struct tx_traffic_info tx_stats[MWL8K_MAX_TID];
345 };
346 #define MWL8K_STA(_sta) ((struct mwl8k_sta *)&((_sta)->drv_priv))
347 
348 static const struct ieee80211_channel mwl8k_channels_24[] = {
349 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2412, .hw_value = 1, },
350 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2417, .hw_value = 2, },
351 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2422, .hw_value = 3, },
352 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2427, .hw_value = 4, },
353 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2432, .hw_value = 5, },
354 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2437, .hw_value = 6, },
355 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2442, .hw_value = 7, },
356 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2447, .hw_value = 8, },
357 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2452, .hw_value = 9, },
358 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2457, .hw_value = 10, },
359 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2462, .hw_value = 11, },
360 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2467, .hw_value = 12, },
361 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2472, .hw_value = 13, },
362 	{ .band = NL80211_BAND_2GHZ, .center_freq = 2484, .hw_value = 14, },
363 };
364 
365 static const struct ieee80211_rate mwl8k_rates_24[] = {
366 	{ .bitrate = 10, .hw_value = 2, },
367 	{ .bitrate = 20, .hw_value = 4, },
368 	{ .bitrate = 55, .hw_value = 11, },
369 	{ .bitrate = 110, .hw_value = 22, },
370 	{ .bitrate = 220, .hw_value = 44, },
371 	{ .bitrate = 60, .hw_value = 12, },
372 	{ .bitrate = 90, .hw_value = 18, },
373 	{ .bitrate = 120, .hw_value = 24, },
374 	{ .bitrate = 180, .hw_value = 36, },
375 	{ .bitrate = 240, .hw_value = 48, },
376 	{ .bitrate = 360, .hw_value = 72, },
377 	{ .bitrate = 480, .hw_value = 96, },
378 	{ .bitrate = 540, .hw_value = 108, },
379 };
380 
381 static const struct ieee80211_channel mwl8k_channels_50[] = {
382 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5180, .hw_value = 36, },
383 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5200, .hw_value = 40, },
384 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5220, .hw_value = 44, },
385 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5240, .hw_value = 48, },
386 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5745, .hw_value = 149, },
387 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5765, .hw_value = 153, },
388 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5785, .hw_value = 157, },
389 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5805, .hw_value = 161, },
390 	{ .band = NL80211_BAND_5GHZ, .center_freq = 5825, .hw_value = 165, },
391 };
392 
393 static const struct ieee80211_rate mwl8k_rates_50[] = {
394 	{ .bitrate = 60, .hw_value = 12, },
395 	{ .bitrate = 90, .hw_value = 18, },
396 	{ .bitrate = 120, .hw_value = 24, },
397 	{ .bitrate = 180, .hw_value = 36, },
398 	{ .bitrate = 240, .hw_value = 48, },
399 	{ .bitrate = 360, .hw_value = 72, },
400 	{ .bitrate = 480, .hw_value = 96, },
401 	{ .bitrate = 540, .hw_value = 108, },
402 };
403 
404 /* Set or get info from Firmware */
405 #define MWL8K_CMD_GET			0x0000
406 #define MWL8K_CMD_SET			0x0001
407 #define MWL8K_CMD_SET_LIST		0x0002
408 
409 /* Firmware command codes */
410 #define MWL8K_CMD_CODE_DNLD		0x0001
411 #define MWL8K_CMD_GET_HW_SPEC		0x0003
412 #define MWL8K_CMD_SET_HW_SPEC		0x0004
413 #define MWL8K_CMD_MAC_MULTICAST_ADR	0x0010
414 #define MWL8K_CMD_GET_STAT		0x0014
415 #define MWL8K_CMD_BBP_REG_ACCESS	0x001a
416 #define MWL8K_CMD_RADIO_CONTROL		0x001c
417 #define MWL8K_CMD_RF_TX_POWER		0x001e
418 #define MWL8K_CMD_TX_POWER		0x001f
419 #define MWL8K_CMD_RF_ANTENNA		0x0020
420 #define MWL8K_CMD_SET_BEACON		0x0100		/* per-vif */
421 #define MWL8K_CMD_SET_PRE_SCAN		0x0107
422 #define MWL8K_CMD_SET_POST_SCAN		0x0108
423 #define MWL8K_CMD_SET_RF_CHANNEL	0x010a
424 #define MWL8K_CMD_SET_AID		0x010d
425 #define MWL8K_CMD_SET_RATE		0x0110
426 #define MWL8K_CMD_SET_FINALIZE_JOIN	0x0111
427 #define MWL8K_CMD_RTS_THRESHOLD		0x0113
428 #define MWL8K_CMD_SET_SLOT		0x0114
429 #define MWL8K_CMD_SET_EDCA_PARAMS	0x0115
430 #define MWL8K_CMD_SET_WMM_MODE		0x0123
431 #define MWL8K_CMD_MIMO_CONFIG		0x0125
432 #define MWL8K_CMD_USE_FIXED_RATE	0x0126
433 #define MWL8K_CMD_ENABLE_SNIFFER	0x0150
434 #define MWL8K_CMD_SET_MAC_ADDR		0x0202		/* per-vif */
435 #define MWL8K_CMD_SET_RATEADAPT_MODE	0x0203
436 #define MWL8K_CMD_GET_WATCHDOG_BITMAP	0x0205
437 #define MWL8K_CMD_DEL_MAC_ADDR		0x0206		/* per-vif */
438 #define MWL8K_CMD_BSS_START		0x1100		/* per-vif */
439 #define MWL8K_CMD_SET_NEW_STN		0x1111		/* per-vif */
440 #define MWL8K_CMD_UPDATE_ENCRYPTION	0x1122		/* per-vif */
441 #define MWL8K_CMD_UPDATE_STADB		0x1123
442 #define MWL8K_CMD_BASTREAM		0x1125
443 
444 #define MWL8K_LEGACY_5G_RATE_OFFSET \
445 	(ARRAY_SIZE(mwl8k_rates_24) - ARRAY_SIZE(mwl8k_rates_50))
446 
447 static const char *mwl8k_cmd_name(__le16 cmd, char *buf, int bufsize)
448 {
449 	u16 command = le16_to_cpu(cmd);
450 
451 #define MWL8K_CMDNAME(x)	case MWL8K_CMD_##x: do {\
452 					snprintf(buf, bufsize, "%s", #x);\
453 					return buf;\
454 					} while (0)
455 	switch (command & ~0x8000) {
456 		MWL8K_CMDNAME(CODE_DNLD);
457 		MWL8K_CMDNAME(GET_HW_SPEC);
458 		MWL8K_CMDNAME(SET_HW_SPEC);
459 		MWL8K_CMDNAME(MAC_MULTICAST_ADR);
460 		MWL8K_CMDNAME(GET_STAT);
461 		MWL8K_CMDNAME(RADIO_CONTROL);
462 		MWL8K_CMDNAME(RF_TX_POWER);
463 		MWL8K_CMDNAME(TX_POWER);
464 		MWL8K_CMDNAME(RF_ANTENNA);
465 		MWL8K_CMDNAME(SET_BEACON);
466 		MWL8K_CMDNAME(SET_PRE_SCAN);
467 		MWL8K_CMDNAME(SET_POST_SCAN);
468 		MWL8K_CMDNAME(SET_RF_CHANNEL);
469 		MWL8K_CMDNAME(SET_AID);
470 		MWL8K_CMDNAME(SET_RATE);
471 		MWL8K_CMDNAME(SET_FINALIZE_JOIN);
472 		MWL8K_CMDNAME(RTS_THRESHOLD);
473 		MWL8K_CMDNAME(SET_SLOT);
474 		MWL8K_CMDNAME(SET_EDCA_PARAMS);
475 		MWL8K_CMDNAME(SET_WMM_MODE);
476 		MWL8K_CMDNAME(MIMO_CONFIG);
477 		MWL8K_CMDNAME(USE_FIXED_RATE);
478 		MWL8K_CMDNAME(ENABLE_SNIFFER);
479 		MWL8K_CMDNAME(SET_MAC_ADDR);
480 		MWL8K_CMDNAME(SET_RATEADAPT_MODE);
481 		MWL8K_CMDNAME(BSS_START);
482 		MWL8K_CMDNAME(SET_NEW_STN);
483 		MWL8K_CMDNAME(UPDATE_ENCRYPTION);
484 		MWL8K_CMDNAME(UPDATE_STADB);
485 		MWL8K_CMDNAME(BASTREAM);
486 		MWL8K_CMDNAME(GET_WATCHDOG_BITMAP);
487 	default:
488 		snprintf(buf, bufsize, "0x%x", cmd);
489 	}
490 #undef MWL8K_CMDNAME
491 
492 	return buf;
493 }
494 
495 /* Hardware and firmware reset */
496 static void mwl8k_hw_reset(struct mwl8k_priv *priv)
497 {
498 	iowrite32(MWL8K_H2A_INT_RESET,
499 		priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
500 	iowrite32(MWL8K_H2A_INT_RESET,
501 		priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
502 	msleep(20);
503 }
504 
505 /* Release fw image */
506 static void mwl8k_release_fw(const struct firmware **fw)
507 {
508 	if (*fw == NULL)
509 		return;
510 	release_firmware(*fw);
511 	*fw = NULL;
512 }
513 
514 static void mwl8k_release_firmware(struct mwl8k_priv *priv)
515 {
516 	mwl8k_release_fw(&priv->fw_ucode);
517 	mwl8k_release_fw(&priv->fw_helper);
518 }
519 
520 /* states for asynchronous f/w loading */
521 static void mwl8k_fw_state_machine(const struct firmware *fw, void *context);
522 enum {
523 	FW_STATE_INIT = 0,
524 	FW_STATE_LOADING_PREF,
525 	FW_STATE_LOADING_ALT,
526 	FW_STATE_ERROR,
527 };
528 
529 /* Request fw image */
530 static int mwl8k_request_fw(struct mwl8k_priv *priv,
531 			    const char *fname, const struct firmware **fw,
532 			    bool nowait)
533 {
534 	/* release current image */
535 	if (*fw != NULL)
536 		mwl8k_release_fw(fw);
537 
538 	if (nowait)
539 		return request_firmware_nowait(THIS_MODULE, 1, fname,
540 					       &priv->pdev->dev, GFP_KERNEL,
541 					       priv, mwl8k_fw_state_machine);
542 	else
543 		return request_firmware(fw, fname, &priv->pdev->dev);
544 }
545 
546 static int mwl8k_request_firmware(struct mwl8k_priv *priv, char *fw_image,
547 				  bool nowait)
548 {
549 	struct mwl8k_device_info *di = priv->device_info;
550 	int rc;
551 
552 	if (di->helper_image != NULL) {
553 		if (nowait)
554 			rc = mwl8k_request_fw(priv, di->helper_image,
555 					      &priv->fw_helper, true);
556 		else
557 			rc = mwl8k_request_fw(priv, di->helper_image,
558 					      &priv->fw_helper, false);
559 		if (rc)
560 			printk(KERN_ERR "%s: Error requesting helper fw %s\n",
561 			       pci_name(priv->pdev), di->helper_image);
562 
563 		if (rc || nowait)
564 			return rc;
565 	}
566 
567 	if (nowait) {
568 		/*
569 		 * if we get here, no helper image is needed.  Skip the
570 		 * FW_STATE_INIT state.
571 		 */
572 		priv->fw_state = FW_STATE_LOADING_PREF;
573 		rc = mwl8k_request_fw(priv, fw_image,
574 				      &priv->fw_ucode,
575 				      true);
576 	} else
577 		rc = mwl8k_request_fw(priv, fw_image,
578 				      &priv->fw_ucode, false);
579 	if (rc) {
580 		printk(KERN_ERR "%s: Error requesting firmware file %s\n",
581 		       pci_name(priv->pdev), fw_image);
582 		mwl8k_release_fw(&priv->fw_helper);
583 		return rc;
584 	}
585 
586 	return 0;
587 }
588 
589 struct mwl8k_cmd_pkt {
590 	__struct_group(mwl8k_cmd_pkt_hdr, hdr, __packed,
591 		__le16	code;
592 		__le16	length;
593 		__u8	seq_num;
594 		__u8	macid;
595 		__le16	result;
596 	);
597 	char payload[];
598 } __packed;
599 
600 /*
601  * Firmware loading.
602  */
603 static int
604 mwl8k_send_fw_load_cmd(struct mwl8k_priv *priv, void *data, int length)
605 {
606 	void __iomem *regs = priv->regs;
607 	dma_addr_t dma_addr;
608 	int loops;
609 
610 	dma_addr = dma_map_single(&priv->pdev->dev, data, length,
611 				  DMA_TO_DEVICE);
612 	if (dma_mapping_error(&priv->pdev->dev, dma_addr))
613 		return -ENOMEM;
614 
615 	iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
616 	iowrite32(0, regs + MWL8K_HIU_INT_CODE);
617 	iowrite32(MWL8K_H2A_INT_DOORBELL,
618 		regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
619 	iowrite32(MWL8K_H2A_INT_DUMMY,
620 		regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
621 
622 	loops = 1000;
623 	do {
624 		u32 int_code;
625 		if (priv->is_8764) {
626 			int_code = ioread32(regs +
627 					    MWL8K_HIU_H2A_INTERRUPT_STATUS);
628 			if (int_code == 0)
629 				break;
630 		} else {
631 			int_code = ioread32(regs + MWL8K_HIU_INT_CODE);
632 			if (int_code == MWL8K_INT_CODE_CMD_FINISHED) {
633 				iowrite32(0, regs + MWL8K_HIU_INT_CODE);
634 				break;
635 			}
636 		}
637 		cond_resched();
638 		udelay(1);
639 	} while (--loops);
640 
641 	dma_unmap_single(&priv->pdev->dev, dma_addr, length, DMA_TO_DEVICE);
642 
643 	return loops ? 0 : -ETIMEDOUT;
644 }
645 
646 static int mwl8k_load_fw_image(struct mwl8k_priv *priv,
647 				const u8 *data, size_t length)
648 {
649 	struct mwl8k_cmd_pkt *cmd;
650 	int done;
651 	int rc = 0;
652 
653 	cmd = kmalloc(sizeof(*cmd) + 256, GFP_KERNEL);
654 	if (cmd == NULL)
655 		return -ENOMEM;
656 
657 	cmd->code = cpu_to_le16(MWL8K_CMD_CODE_DNLD);
658 	cmd->seq_num = 0;
659 	cmd->macid = 0;
660 	cmd->result = 0;
661 
662 	done = 0;
663 	while (length) {
664 		int block_size = length > 256 ? 256 : length;
665 
666 		memcpy(cmd->payload, data + done, block_size);
667 		cmd->length = cpu_to_le16(block_size);
668 
669 		rc = mwl8k_send_fw_load_cmd(priv, cmd,
670 						sizeof(*cmd) + block_size);
671 		if (rc)
672 			break;
673 
674 		done += block_size;
675 		length -= block_size;
676 	}
677 
678 	if (!rc) {
679 		cmd->length = 0;
680 		rc = mwl8k_send_fw_load_cmd(priv, cmd, sizeof(*cmd));
681 	}
682 
683 	kfree(cmd);
684 
685 	return rc;
686 }
687 
688 static int mwl8k_feed_fw_image(struct mwl8k_priv *priv,
689 				const u8 *data, size_t length)
690 {
691 	unsigned char *buffer;
692 	int may_continue, rc = 0;
693 	u32 done, prev_block_size;
694 
695 	buffer = kmalloc(1024, GFP_KERNEL);
696 	if (buffer == NULL)
697 		return -ENOMEM;
698 
699 	done = 0;
700 	prev_block_size = 0;
701 	may_continue = 1000;
702 	while (may_continue > 0) {
703 		u32 block_size;
704 
705 		block_size = ioread32(priv->regs + MWL8K_HIU_SCRATCH);
706 		if (block_size & 1) {
707 			block_size &= ~1;
708 			may_continue--;
709 		} else {
710 			done += prev_block_size;
711 			length -= prev_block_size;
712 		}
713 
714 		if (block_size > 1024 || block_size > length) {
715 			rc = -EOVERFLOW;
716 			break;
717 		}
718 
719 		if (length == 0) {
720 			rc = 0;
721 			break;
722 		}
723 
724 		if (block_size == 0) {
725 			rc = -EPROTO;
726 			may_continue--;
727 			udelay(1);
728 			continue;
729 		}
730 
731 		prev_block_size = block_size;
732 		memcpy(buffer, data + done, block_size);
733 
734 		rc = mwl8k_send_fw_load_cmd(priv, buffer, block_size);
735 		if (rc)
736 			break;
737 	}
738 
739 	if (!rc && length != 0)
740 		rc = -EREMOTEIO;
741 
742 	kfree(buffer);
743 
744 	return rc;
745 }
746 
747 static int mwl8k_load_firmware(struct ieee80211_hw *hw)
748 {
749 	struct mwl8k_priv *priv = hw->priv;
750 	const struct firmware *fw = priv->fw_ucode;
751 	int rc;
752 	int loops;
753 
754 	if (!memcmp(fw->data, "\x01\x00\x00\x00", 4) && !priv->is_8764) {
755 		const struct firmware *helper = priv->fw_helper;
756 
757 		if (helper == NULL) {
758 			printk(KERN_ERR "%s: helper image needed but none "
759 			       "given\n", pci_name(priv->pdev));
760 			return -EINVAL;
761 		}
762 
763 		rc = mwl8k_load_fw_image(priv, helper->data, helper->size);
764 		if (rc) {
765 			printk(KERN_ERR "%s: unable to load firmware "
766 			       "helper image\n", pci_name(priv->pdev));
767 			return rc;
768 		}
769 		msleep(20);
770 
771 		rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
772 	} else {
773 		if (priv->is_8764)
774 			rc = mwl8k_feed_fw_image(priv, fw->data, fw->size);
775 		else
776 			rc = mwl8k_load_fw_image(priv, fw->data, fw->size);
777 	}
778 
779 	if (rc) {
780 		printk(KERN_ERR "%s: unable to load firmware image\n",
781 		       pci_name(priv->pdev));
782 		return rc;
783 	}
784 
785 	iowrite32(MWL8K_MODE_STA, priv->regs + MWL8K_HIU_GEN_PTR);
786 
787 	loops = 500000;
788 	do {
789 		u32 ready_code;
790 
791 		ready_code = ioread32(priv->regs + MWL8K_HIU_INT_CODE);
792 		if (ready_code == MWL8K_FWAP_READY) {
793 			priv->ap_fw = true;
794 			break;
795 		} else if (ready_code == MWL8K_FWSTA_READY) {
796 			priv->ap_fw = false;
797 			break;
798 		}
799 
800 		cond_resched();
801 		udelay(1);
802 	} while (--loops);
803 
804 	return loops ? 0 : -ETIMEDOUT;
805 }
806 
807 
808 /* DMA header used by firmware and hardware.  */
809 struct mwl8k_dma_data {
810 	__le16 fwlen;
811 	struct ieee80211_hdr wh;
812 	char data[];
813 } __packed __aligned(2);
814 
815 /* Routines to add/remove DMA header from skb.  */
816 static inline void mwl8k_remove_dma_header(struct sk_buff *skb, __le16 qos)
817 {
818 	struct mwl8k_dma_data *tr;
819 	int hdrlen;
820 
821 	tr = (struct mwl8k_dma_data *)skb->data;
822 	hdrlen = ieee80211_hdrlen(tr->wh.frame_control);
823 
824 	if (hdrlen != sizeof(tr->wh)) {
825 		if (ieee80211_is_data_qos(tr->wh.frame_control)) {
826 			memmove(tr->data - hdrlen, &tr->wh, hdrlen - 2);
827 			*((__le16 *)(tr->data - 2)) = qos;
828 		} else {
829 			memmove(tr->data - hdrlen, &tr->wh, hdrlen);
830 		}
831 	}
832 
833 	if (hdrlen != sizeof(*tr))
834 		skb_pull(skb, sizeof(*tr) - hdrlen);
835 }
836 
837 #define REDUCED_TX_HEADROOM	8
838 
839 static void
840 mwl8k_add_dma_header(struct mwl8k_priv *priv, struct sk_buff *skb,
841 						int head_pad, int tail_pad)
842 {
843 	struct ieee80211_hdr *wh;
844 	int hdrlen;
845 	int reqd_hdrlen;
846 	struct mwl8k_dma_data *tr;
847 
848 	/*
849 	 * Add a firmware DMA header; the firmware requires that we
850 	 * present a 2-byte payload length followed by a 4-address
851 	 * header (without QoS field), followed (optionally) by any
852 	 * WEP/ExtIV header (but only filled in for CCMP).
853 	 */
854 	wh = (struct ieee80211_hdr *)skb->data;
855 
856 	hdrlen = ieee80211_hdrlen(wh->frame_control);
857 
858 	/*
859 	 * Check if skb_resize is required because of
860 	 * tx_headroom adjustment.
861 	 */
862 	if (priv->ap_fw && (hdrlen < (sizeof(struct ieee80211_cts)
863 						+ REDUCED_TX_HEADROOM))) {
864 		if (pskb_expand_head(skb, REDUCED_TX_HEADROOM, 0, GFP_ATOMIC)) {
865 
866 			wiphy_err(priv->hw->wiphy,
867 					"Failed to reallocate TX buffer\n");
868 			return;
869 		}
870 		skb->truesize += REDUCED_TX_HEADROOM;
871 	}
872 
873 	reqd_hdrlen = sizeof(*tr) + head_pad;
874 
875 	if (hdrlen != reqd_hdrlen)
876 		skb_push(skb, reqd_hdrlen - hdrlen);
877 
878 	if (ieee80211_is_data_qos(wh->frame_control))
879 		hdrlen -= IEEE80211_QOS_CTL_LEN;
880 
881 	tr = (struct mwl8k_dma_data *)skb->data;
882 	if (wh != &tr->wh)
883 		memmove(&tr->wh, wh, hdrlen);
884 	if (hdrlen != sizeof(tr->wh))
885 		memset(((void *)&tr->wh) + hdrlen, 0, sizeof(tr->wh) - hdrlen);
886 
887 	/*
888 	 * Firmware length is the length of the fully formed "802.11
889 	 * payload".  That is, everything except for the 802.11 header.
890 	 * This includes all crypto material including the MIC.
891 	 */
892 	tr->fwlen = cpu_to_le16(skb->len - sizeof(*tr) + tail_pad);
893 }
894 
895 static void mwl8k_encapsulate_tx_frame(struct mwl8k_priv *priv,
896 		struct sk_buff *skb)
897 {
898 	struct ieee80211_hdr *wh;
899 	struct ieee80211_tx_info *tx_info;
900 	struct ieee80211_key_conf *key_conf;
901 	int data_pad;
902 	int head_pad = 0;
903 
904 	wh = (struct ieee80211_hdr *)skb->data;
905 
906 	tx_info = IEEE80211_SKB_CB(skb);
907 
908 	key_conf = NULL;
909 	if (ieee80211_is_data(wh->frame_control))
910 		key_conf = tx_info->control.hw_key;
911 
912 	/*
913 	 * Make sure the packet header is in the DMA header format (4-address
914 	 * without QoS), and add head & tail padding when HW crypto is enabled.
915 	 *
916 	 * We have the following trailer padding requirements:
917 	 * - WEP: 4 trailer bytes (ICV)
918 	 * - TKIP: 12 trailer bytes (8 MIC + 4 ICV)
919 	 * - CCMP: 8 trailer bytes (MIC)
920 	 */
921 	data_pad = 0;
922 	if (key_conf != NULL) {
923 		head_pad = key_conf->iv_len;
924 		switch (key_conf->cipher) {
925 		case WLAN_CIPHER_SUITE_WEP40:
926 		case WLAN_CIPHER_SUITE_WEP104:
927 			data_pad = 4;
928 			break;
929 		case WLAN_CIPHER_SUITE_TKIP:
930 			data_pad = 12;
931 			break;
932 		case WLAN_CIPHER_SUITE_CCMP:
933 			data_pad = 8;
934 			break;
935 		}
936 	}
937 	mwl8k_add_dma_header(priv, skb, head_pad, data_pad);
938 }
939 
940 /*
941  * Packet reception for 88w8366/88w8764 AP firmware.
942  */
943 struct mwl8k_rxd_ap {
944 	__le16 pkt_len;
945 	__u8 sq2;
946 	__u8 rate;
947 	__le32 pkt_phys_addr;
948 	__le32 next_rxd_phys_addr;
949 	__le16 qos_control;
950 	__le16 htsig2;
951 	__le32 hw_rssi_info;
952 	__le32 hw_noise_floor_info;
953 	__u8 noise_floor;
954 	__u8 pad0[3];
955 	__u8 rssi;
956 	__u8 rx_status;
957 	__u8 channel;
958 	__u8 rx_ctrl;
959 } __packed;
960 
961 #define MWL8K_AP_RATE_INFO_MCS_FORMAT		0x80
962 #define MWL8K_AP_RATE_INFO_40MHZ		0x40
963 #define MWL8K_AP_RATE_INFO_RATEID(x)		((x) & 0x3f)
964 
965 #define MWL8K_AP_RX_CTRL_OWNED_BY_HOST		0x80
966 
967 /* 8366/8764 AP rx_status bits */
968 #define MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK		0x80
969 #define MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR		0xFF
970 #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR		0x02
971 #define MWL8K_AP_RXSTAT_WEP_DECRYPT_ICV_ERR		0x04
972 #define MWL8K_AP_RXSTAT_TKIP_DECRYPT_ICV_ERR		0x08
973 
974 static void mwl8k_rxd_ap_init(void *_rxd, dma_addr_t next_dma_addr)
975 {
976 	struct mwl8k_rxd_ap *rxd = _rxd;
977 
978 	rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
979 	rxd->rx_ctrl = MWL8K_AP_RX_CTRL_OWNED_BY_HOST;
980 }
981 
982 static void mwl8k_rxd_ap_refill(void *_rxd, dma_addr_t addr, int len)
983 {
984 	struct mwl8k_rxd_ap *rxd = _rxd;
985 
986 	rxd->pkt_len = cpu_to_le16(len);
987 	rxd->pkt_phys_addr = cpu_to_le32(addr);
988 	wmb();
989 	rxd->rx_ctrl = 0;
990 }
991 
992 static int
993 mwl8k_rxd_ap_process(void *_rxd, struct ieee80211_rx_status *status,
994 		     __le16 *qos, s8 *noise)
995 {
996 	struct mwl8k_rxd_ap *rxd = _rxd;
997 
998 	if (!(rxd->rx_ctrl & MWL8K_AP_RX_CTRL_OWNED_BY_HOST))
999 		return -1;
1000 	rmb();
1001 
1002 	memset(status, 0, sizeof(*status));
1003 
1004 	status->signal = -rxd->rssi;
1005 	*noise = -rxd->noise_floor;
1006 
1007 	if (rxd->rate & MWL8K_AP_RATE_INFO_MCS_FORMAT) {
1008 		status->encoding = RX_ENC_HT;
1009 		if (rxd->rate & MWL8K_AP_RATE_INFO_40MHZ)
1010 			status->bw = RATE_INFO_BW_40;
1011 		status->rate_idx = MWL8K_AP_RATE_INFO_RATEID(rxd->rate);
1012 	} else {
1013 		int i;
1014 
1015 		for (i = 0; i < ARRAY_SIZE(mwl8k_rates_24); i++) {
1016 			if (mwl8k_rates_24[i].hw_value == rxd->rate) {
1017 				status->rate_idx = i;
1018 				break;
1019 			}
1020 		}
1021 	}
1022 
1023 	if (rxd->channel > 14) {
1024 		status->band = NL80211_BAND_5GHZ;
1025 		if (!(status->encoding == RX_ENC_HT) &&
1026 		    status->rate_idx >= MWL8K_LEGACY_5G_RATE_OFFSET)
1027 			status->rate_idx -= MWL8K_LEGACY_5G_RATE_OFFSET;
1028 	} else {
1029 		status->band = NL80211_BAND_2GHZ;
1030 	}
1031 	status->freq = ieee80211_channel_to_frequency(rxd->channel,
1032 						      status->band);
1033 
1034 	*qos = rxd->qos_control;
1035 
1036 	if ((rxd->rx_status != MWL8K_AP_RXSTAT_GENERAL_DECRYPT_ERR) &&
1037 	    (rxd->rx_status & MWL8K_AP_RXSTAT_DECRYPT_ERR_MASK) &&
1038 	    (rxd->rx_status & MWL8K_AP_RXSTAT_TKIP_DECRYPT_MIC_ERR))
1039 		status->flag |= RX_FLAG_MMIC_ERROR;
1040 
1041 	return le16_to_cpu(rxd->pkt_len);
1042 }
1043 
1044 static struct rxd_ops rxd_ap_ops = {
1045 	.rxd_size	= sizeof(struct mwl8k_rxd_ap),
1046 	.rxd_init	= mwl8k_rxd_ap_init,
1047 	.rxd_refill	= mwl8k_rxd_ap_refill,
1048 	.rxd_process	= mwl8k_rxd_ap_process,
1049 };
1050 
1051 /*
1052  * Packet reception for STA firmware.
1053  */
1054 struct mwl8k_rxd_sta {
1055 	__le16 pkt_len;
1056 	__u8 link_quality;
1057 	__u8 noise_level;
1058 	__le32 pkt_phys_addr;
1059 	__le32 next_rxd_phys_addr;
1060 	__le16 qos_control;
1061 	__le16 rate_info;
1062 	__le32 pad0[4];
1063 	__u8 rssi;
1064 	__u8 channel;
1065 	__le16 pad1;
1066 	__u8 rx_ctrl;
1067 	__u8 rx_status;
1068 	__u8 pad2[2];
1069 } __packed;
1070 
1071 #define MWL8K_STA_RATE_INFO_SHORTPRE		0x8000
1072 #define MWL8K_STA_RATE_INFO_ANTSELECT(x)	(((x) >> 11) & 0x3)
1073 #define MWL8K_STA_RATE_INFO_RATEID(x)		(((x) >> 3) & 0x3f)
1074 #define MWL8K_STA_RATE_INFO_40MHZ		0x0004
1075 #define MWL8K_STA_RATE_INFO_SHORTGI		0x0002
1076 #define MWL8K_STA_RATE_INFO_MCS_FORMAT		0x0001
1077 
1078 #define MWL8K_STA_RX_CTRL_OWNED_BY_HOST		0x02
1079 #define MWL8K_STA_RX_CTRL_DECRYPT_ERROR		0x04
1080 /* ICV=0 or MIC=1 */
1081 #define MWL8K_STA_RX_CTRL_DEC_ERR_TYPE		0x08
1082 /* Key is uploaded only in failure case */
1083 #define MWL8K_STA_RX_CTRL_KEY_INDEX			0x30
1084 
1085 static void mwl8k_rxd_sta_init(void *_rxd, dma_addr_t next_dma_addr)
1086 {
1087 	struct mwl8k_rxd_sta *rxd = _rxd;
1088 
1089 	rxd->next_rxd_phys_addr = cpu_to_le32(next_dma_addr);
1090 	rxd->rx_ctrl = MWL8K_STA_RX_CTRL_OWNED_BY_HOST;
1091 }
1092 
1093 static void mwl8k_rxd_sta_refill(void *_rxd, dma_addr_t addr, int len)
1094 {
1095 	struct mwl8k_rxd_sta *rxd = _rxd;
1096 
1097 	rxd->pkt_len = cpu_to_le16(len);
1098 	rxd->pkt_phys_addr = cpu_to_le32(addr);
1099 	wmb();
1100 	rxd->rx_ctrl = 0;
1101 }
1102 
1103 static int
1104 mwl8k_rxd_sta_process(void *_rxd, struct ieee80211_rx_status *status,
1105 		       __le16 *qos, s8 *noise)
1106 {
1107 	struct mwl8k_rxd_sta *rxd = _rxd;
1108 	u16 rate_info;
1109 
1110 	if (!(rxd->rx_ctrl & MWL8K_STA_RX_CTRL_OWNED_BY_HOST))
1111 		return -1;
1112 	rmb();
1113 
1114 	rate_info = le16_to_cpu(rxd->rate_info);
1115 
1116 	memset(status, 0, sizeof(*status));
1117 
1118 	status->signal = -rxd->rssi;
1119 	*noise = -rxd->noise_level;
1120 	status->antenna = MWL8K_STA_RATE_INFO_ANTSELECT(rate_info);
1121 	status->rate_idx = MWL8K_STA_RATE_INFO_RATEID(rate_info);
1122 
1123 	if (rate_info & MWL8K_STA_RATE_INFO_SHORTPRE)
1124 		status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
1125 	if (rate_info & MWL8K_STA_RATE_INFO_40MHZ)
1126 		status->bw = RATE_INFO_BW_40;
1127 	if (rate_info & MWL8K_STA_RATE_INFO_SHORTGI)
1128 		status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
1129 	if (rate_info & MWL8K_STA_RATE_INFO_MCS_FORMAT)
1130 		status->encoding = RX_ENC_HT;
1131 
1132 	if (rxd->channel > 14) {
1133 		status->band = NL80211_BAND_5GHZ;
1134 		if (!(status->encoding == RX_ENC_HT) &&
1135 		    status->rate_idx >= MWL8K_LEGACY_5G_RATE_OFFSET)
1136 			status->rate_idx -= MWL8K_LEGACY_5G_RATE_OFFSET;
1137 	} else {
1138 		status->band = NL80211_BAND_2GHZ;
1139 	}
1140 	status->freq = ieee80211_channel_to_frequency(rxd->channel,
1141 						      status->band);
1142 
1143 	*qos = rxd->qos_control;
1144 	if ((rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DECRYPT_ERROR) &&
1145 	    (rxd->rx_ctrl & MWL8K_STA_RX_CTRL_DEC_ERR_TYPE))
1146 		status->flag |= RX_FLAG_MMIC_ERROR;
1147 
1148 	return le16_to_cpu(rxd->pkt_len);
1149 }
1150 
1151 static struct rxd_ops rxd_sta_ops = {
1152 	.rxd_size	= sizeof(struct mwl8k_rxd_sta),
1153 	.rxd_init	= mwl8k_rxd_sta_init,
1154 	.rxd_refill	= mwl8k_rxd_sta_refill,
1155 	.rxd_process	= mwl8k_rxd_sta_process,
1156 };
1157 
1158 
1159 #define MWL8K_RX_DESCS		256
1160 #define MWL8K_RX_MAXSZ		3800
1161 
1162 static int mwl8k_rxq_init(struct ieee80211_hw *hw, int index)
1163 {
1164 	struct mwl8k_priv *priv = hw->priv;
1165 	struct mwl8k_rx_queue *rxq = priv->rxq + index;
1166 	int size;
1167 	int i;
1168 
1169 	rxq->rxd_count = 0;
1170 	rxq->head = 0;
1171 	rxq->tail = 0;
1172 
1173 	size = MWL8K_RX_DESCS * priv->rxd_ops->rxd_size;
1174 
1175 	rxq->rxd = dma_alloc_coherent(&priv->pdev->dev, size, &rxq->rxd_dma,
1176 				      GFP_KERNEL);
1177 	if (rxq->rxd == NULL) {
1178 		wiphy_err(hw->wiphy, "failed to alloc RX descriptors\n");
1179 		return -ENOMEM;
1180 	}
1181 
1182 	rxq->buf = kcalloc(MWL8K_RX_DESCS, sizeof(*rxq->buf), GFP_KERNEL);
1183 	if (rxq->buf == NULL) {
1184 		dma_free_coherent(&priv->pdev->dev, size, rxq->rxd,
1185 				  rxq->rxd_dma);
1186 		return -ENOMEM;
1187 	}
1188 
1189 	for (i = 0; i < MWL8K_RX_DESCS; i++) {
1190 		int desc_size;
1191 		void *rxd;
1192 		int nexti;
1193 		dma_addr_t next_dma_addr;
1194 
1195 		desc_size = priv->rxd_ops->rxd_size;
1196 		rxd = rxq->rxd + (i * priv->rxd_ops->rxd_size);
1197 
1198 		nexti = i + 1;
1199 		if (nexti == MWL8K_RX_DESCS)
1200 			nexti = 0;
1201 		next_dma_addr = rxq->rxd_dma + (nexti * desc_size);
1202 
1203 		priv->rxd_ops->rxd_init(rxd, next_dma_addr);
1204 	}
1205 
1206 	return 0;
1207 }
1208 
1209 static int rxq_refill(struct ieee80211_hw *hw, int index, int limit)
1210 {
1211 	struct mwl8k_priv *priv = hw->priv;
1212 	struct mwl8k_rx_queue *rxq = priv->rxq + index;
1213 	int refilled = 0;
1214 
1215 	while (rxq->rxd_count < MWL8K_RX_DESCS && limit--) {
1216 		struct sk_buff *skb;
1217 		dma_addr_t addr;
1218 		int rx;
1219 		void *rxd;
1220 
1221 		skb = dev_alloc_skb(MWL8K_RX_MAXSZ);
1222 		if (skb == NULL)
1223 			break;
1224 
1225 		addr = dma_map_single(&priv->pdev->dev, skb->data,
1226 				      MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
1227 
1228 		rxq->rxd_count++;
1229 		rx = rxq->tail++;
1230 		if (rxq->tail == MWL8K_RX_DESCS)
1231 			rxq->tail = 0;
1232 		rxq->buf[rx].skb = skb;
1233 		dma_unmap_addr_set(&rxq->buf[rx], dma, addr);
1234 
1235 		rxd = rxq->rxd + (rx * priv->rxd_ops->rxd_size);
1236 		priv->rxd_ops->rxd_refill(rxd, addr, MWL8K_RX_MAXSZ);
1237 
1238 		refilled++;
1239 	}
1240 
1241 	return refilled;
1242 }
1243 
1244 /* Must be called only when the card's reception is completely halted */
1245 static void mwl8k_rxq_deinit(struct ieee80211_hw *hw, int index)
1246 {
1247 	struct mwl8k_priv *priv = hw->priv;
1248 	struct mwl8k_rx_queue *rxq = priv->rxq + index;
1249 	int i;
1250 
1251 	if (rxq->rxd == NULL)
1252 		return;
1253 
1254 	for (i = 0; i < MWL8K_RX_DESCS; i++) {
1255 		if (rxq->buf[i].skb != NULL) {
1256 			dma_unmap_single(&priv->pdev->dev,
1257 					 dma_unmap_addr(&rxq->buf[i], dma),
1258 					 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
1259 			dma_unmap_addr_set(&rxq->buf[i], dma, 0);
1260 
1261 			kfree_skb(rxq->buf[i].skb);
1262 			rxq->buf[i].skb = NULL;
1263 		}
1264 	}
1265 
1266 	kfree(rxq->buf);
1267 	rxq->buf = NULL;
1268 
1269 	dma_free_coherent(&priv->pdev->dev,
1270 			  MWL8K_RX_DESCS * priv->rxd_ops->rxd_size, rxq->rxd,
1271 			  rxq->rxd_dma);
1272 	rxq->rxd = NULL;
1273 }
1274 
1275 
1276 /*
1277  * Scan a list of BSSIDs to process for finalize join.
1278  * Allows for extension to process multiple BSSIDs.
1279  */
1280 static inline int
1281 mwl8k_capture_bssid(struct mwl8k_priv *priv, struct ieee80211_hdr *wh)
1282 {
1283 	return priv->capture_beacon &&
1284 		ieee80211_is_beacon(wh->frame_control) &&
1285 		ether_addr_equal_64bits(wh->addr3, priv->capture_bssid);
1286 }
1287 
1288 static inline void mwl8k_save_beacon(struct ieee80211_hw *hw,
1289 				     struct sk_buff *skb)
1290 {
1291 	struct mwl8k_priv *priv = hw->priv;
1292 
1293 	priv->capture_beacon = false;
1294 	eth_zero_addr(priv->capture_bssid);
1295 
1296 	/*
1297 	 * Use GFP_ATOMIC as rxq_process is called from
1298 	 * the primary interrupt handler, memory allocation call
1299 	 * must not sleep.
1300 	 */
1301 	priv->beacon_skb = skb_copy(skb, GFP_ATOMIC);
1302 	if (priv->beacon_skb != NULL)
1303 		ieee80211_queue_work(hw, &priv->finalize_join_worker);
1304 }
1305 
1306 static inline struct mwl8k_vif *mwl8k_find_vif_bss(struct list_head *vif_list,
1307 						   u8 *bssid)
1308 {
1309 	struct mwl8k_vif *mwl8k_vif;
1310 
1311 	list_for_each_entry(mwl8k_vif,
1312 			    vif_list, list) {
1313 		if (memcmp(bssid, mwl8k_vif->bssid,
1314 			   ETH_ALEN) == 0)
1315 			return mwl8k_vif;
1316 	}
1317 
1318 	return NULL;
1319 }
1320 
1321 static int rxq_process(struct ieee80211_hw *hw, int index, int limit)
1322 {
1323 	struct mwl8k_priv *priv = hw->priv;
1324 	struct mwl8k_vif *mwl8k_vif = NULL;
1325 	struct mwl8k_rx_queue *rxq = priv->rxq + index;
1326 	int processed;
1327 
1328 	processed = 0;
1329 	while (rxq->rxd_count && limit--) {
1330 		struct sk_buff *skb;
1331 		void *rxd;
1332 		int pkt_len;
1333 		struct ieee80211_rx_status status;
1334 		struct ieee80211_hdr *wh;
1335 		__le16 qos;
1336 
1337 		skb = rxq->buf[rxq->head].skb;
1338 		if (skb == NULL)
1339 			break;
1340 
1341 		rxd = rxq->rxd + (rxq->head * priv->rxd_ops->rxd_size);
1342 
1343 		pkt_len = priv->rxd_ops->rxd_process(rxd, &status, &qos,
1344 							&priv->noise);
1345 		if (pkt_len < 0)
1346 			break;
1347 
1348 		rxq->buf[rxq->head].skb = NULL;
1349 
1350 		dma_unmap_single(&priv->pdev->dev,
1351 				 dma_unmap_addr(&rxq->buf[rxq->head], dma),
1352 				 MWL8K_RX_MAXSZ, DMA_FROM_DEVICE);
1353 		dma_unmap_addr_set(&rxq->buf[rxq->head], dma, 0);
1354 
1355 		rxq->head++;
1356 		if (rxq->head == MWL8K_RX_DESCS)
1357 			rxq->head = 0;
1358 
1359 		rxq->rxd_count--;
1360 
1361 		wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1362 
1363 		/*
1364 		 * Check for a pending join operation.  Save a
1365 		 * copy of the beacon and schedule a tasklet to
1366 		 * send a FINALIZE_JOIN command to the firmware.
1367 		 */
1368 		if (mwl8k_capture_bssid(priv, (void *)skb->data))
1369 			mwl8k_save_beacon(hw, skb);
1370 
1371 		if (ieee80211_has_protected(wh->frame_control)) {
1372 
1373 			/* Check if hw crypto has been enabled for
1374 			 * this bss. If yes, set the status flags
1375 			 * accordingly
1376 			 */
1377 			mwl8k_vif = mwl8k_find_vif_bss(&priv->vif_list,
1378 								wh->addr1);
1379 
1380 			if (mwl8k_vif != NULL &&
1381 			    mwl8k_vif->is_hw_crypto_enabled) {
1382 				/*
1383 				 * When MMIC ERROR is encountered
1384 				 * by the firmware, payload is
1385 				 * dropped and only 32 bytes of
1386 				 * mwl8k Firmware header is sent
1387 				 * to the host.
1388 				 *
1389 				 * We need to add four bytes of
1390 				 * key information.  In it
1391 				 * MAC80211 expects keyidx set to
1392 				 * 0 for triggering Counter
1393 				 * Measure of MMIC failure.
1394 				 */
1395 				if (status.flag & RX_FLAG_MMIC_ERROR) {
1396 					struct mwl8k_dma_data *tr;
1397 					tr = (struct mwl8k_dma_data *)skb->data;
1398 					memset((void *)&(tr->data), 0, 4);
1399 					pkt_len += 4;
1400 				}
1401 
1402 				if (!ieee80211_is_auth(wh->frame_control))
1403 					status.flag |= RX_FLAG_IV_STRIPPED |
1404 						       RX_FLAG_DECRYPTED |
1405 						       RX_FLAG_MMIC_STRIPPED;
1406 			}
1407 		}
1408 
1409 		skb_put(skb, pkt_len);
1410 		mwl8k_remove_dma_header(skb, qos);
1411 		memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
1412 		ieee80211_rx_irqsafe(hw, skb);
1413 
1414 		processed++;
1415 	}
1416 
1417 	return processed;
1418 }
1419 
1420 
1421 /*
1422  * Packet transmission.
1423  */
1424 
1425 #define MWL8K_TXD_STATUS_OK			0x00000001
1426 #define MWL8K_TXD_STATUS_OK_RETRY		0x00000002
1427 #define MWL8K_TXD_STATUS_OK_MORE_RETRY		0x00000004
1428 #define MWL8K_TXD_STATUS_MULTICAST_TX		0x00000008
1429 #define MWL8K_TXD_STATUS_FW_OWNED		0x80000000
1430 
1431 #define MWL8K_QOS_QLEN_UNSPEC			0xff00
1432 #define MWL8K_QOS_ACK_POLICY_MASK		0x0060
1433 #define MWL8K_QOS_ACK_POLICY_NORMAL		0x0000
1434 #define MWL8K_QOS_ACK_POLICY_BLOCKACK		0x0060
1435 #define MWL8K_QOS_EOSP				0x0010
1436 
1437 struct mwl8k_tx_desc {
1438 	__le32 status;
1439 	__u8 data_rate;
1440 	__u8 tx_priority;
1441 	__le16 qos_control;
1442 	__le32 pkt_phys_addr;
1443 	__le16 pkt_len;
1444 	__u8 dest_MAC_addr[ETH_ALEN];
1445 	__le32 next_txd_phys_addr;
1446 	__le32 timestamp;
1447 	__le16 rate_info;
1448 	__u8 peer_id;
1449 	__u8 tx_frag_cnt;
1450 } __packed;
1451 
1452 #define MWL8K_TX_DESCS		128
1453 
1454 static int mwl8k_txq_init(struct ieee80211_hw *hw, int index)
1455 {
1456 	struct mwl8k_priv *priv = hw->priv;
1457 	struct mwl8k_tx_queue *txq = priv->txq + index;
1458 	int size;
1459 	int i;
1460 
1461 	txq->len = 0;
1462 	txq->head = 0;
1463 	txq->tail = 0;
1464 
1465 	size = MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc);
1466 
1467 	txq->txd = dma_alloc_coherent(&priv->pdev->dev, size, &txq->txd_dma,
1468 				      GFP_KERNEL);
1469 	if (txq->txd == NULL) {
1470 		wiphy_err(hw->wiphy, "failed to alloc TX descriptors\n");
1471 		return -ENOMEM;
1472 	}
1473 
1474 	txq->skb = kcalloc(MWL8K_TX_DESCS, sizeof(*txq->skb), GFP_KERNEL);
1475 	if (txq->skb == NULL) {
1476 		dma_free_coherent(&priv->pdev->dev, size, txq->txd,
1477 				  txq->txd_dma);
1478 		txq->txd = NULL;
1479 		return -ENOMEM;
1480 	}
1481 
1482 	for (i = 0; i < MWL8K_TX_DESCS; i++) {
1483 		struct mwl8k_tx_desc *tx_desc;
1484 		int nexti;
1485 
1486 		tx_desc = txq->txd + i;
1487 		nexti = (i + 1) % MWL8K_TX_DESCS;
1488 
1489 		tx_desc->status = 0;
1490 		tx_desc->next_txd_phys_addr =
1491 			cpu_to_le32(txq->txd_dma + nexti * sizeof(*tx_desc));
1492 	}
1493 
1494 	return 0;
1495 }
1496 
1497 static inline void mwl8k_tx_start(struct mwl8k_priv *priv)
1498 {
1499 	iowrite32(MWL8K_H2A_INT_PPA_READY,
1500 		priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1501 	iowrite32(MWL8K_H2A_INT_DUMMY,
1502 		priv->regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
1503 	ioread32(priv->regs + MWL8K_HIU_INT_CODE);
1504 }
1505 
1506 static void mwl8k_dump_tx_rings(struct ieee80211_hw *hw)
1507 {
1508 	struct mwl8k_priv *priv = hw->priv;
1509 	int i;
1510 
1511 	for (i = 0; i < mwl8k_tx_queues(priv); i++) {
1512 		struct mwl8k_tx_queue *txq = priv->txq + i;
1513 		int fw_owned = 0;
1514 		int drv_owned = 0;
1515 		int unused = 0;
1516 		int desc;
1517 
1518 		for (desc = 0; desc < MWL8K_TX_DESCS; desc++) {
1519 			struct mwl8k_tx_desc *tx_desc = txq->txd + desc;
1520 			u32 status;
1521 
1522 			status = le32_to_cpu(tx_desc->status);
1523 			if (status & MWL8K_TXD_STATUS_FW_OWNED)
1524 				fw_owned++;
1525 			else
1526 				drv_owned++;
1527 
1528 			if (tx_desc->pkt_len == 0)
1529 				unused++;
1530 		}
1531 
1532 		wiphy_err(hw->wiphy,
1533 			  "txq[%d] len=%d head=%d tail=%d "
1534 			  "fw_owned=%d drv_owned=%d unused=%d\n",
1535 			  i,
1536 			  txq->len, txq->head, txq->tail,
1537 			  fw_owned, drv_owned, unused);
1538 	}
1539 }
1540 
1541 /*
1542  * Must be called with priv->fw_mutex held and tx queues stopped.
1543  */
1544 #define MWL8K_TX_WAIT_TIMEOUT_MS	5000
1545 
1546 static int mwl8k_tx_wait_empty(struct ieee80211_hw *hw)
1547 {
1548 	struct mwl8k_priv *priv = hw->priv;
1549 	DECLARE_COMPLETION_ONSTACK(tx_wait);
1550 	int retry;
1551 	int rc;
1552 
1553 	might_sleep();
1554 
1555 	/* Since fw restart is in progress, allow only the firmware
1556 	 * commands from the restart code and block the other
1557 	 * commands since they are going to fail in any case since
1558 	 * the firmware has crashed
1559 	 */
1560 	if (priv->hw_restart_in_progress) {
1561 		if (priv->hw_restart_owner == current)
1562 			return 0;
1563 		else
1564 			return -EBUSY;
1565 	}
1566 
1567 	if (atomic_read(&priv->watchdog_event_pending))
1568 		return 0;
1569 
1570 	/*
1571 	 * The TX queues are stopped at this point, so this test
1572 	 * doesn't need to take ->tx_lock.
1573 	 */
1574 	if (!priv->pending_tx_pkts)
1575 		return 0;
1576 
1577 	retry = 1;
1578 	rc = 0;
1579 
1580 	spin_lock_bh(&priv->tx_lock);
1581 	priv->tx_wait = &tx_wait;
1582 	while (!rc) {
1583 		int oldcount;
1584 		unsigned long timeout;
1585 
1586 		oldcount = priv->pending_tx_pkts;
1587 
1588 		spin_unlock_bh(&priv->tx_lock);
1589 		timeout = wait_for_completion_timeout(&tx_wait,
1590 			    msecs_to_jiffies(MWL8K_TX_WAIT_TIMEOUT_MS));
1591 
1592 		if (atomic_read(&priv->watchdog_event_pending)) {
1593 			spin_lock_bh(&priv->tx_lock);
1594 			priv->tx_wait = NULL;
1595 			spin_unlock_bh(&priv->tx_lock);
1596 			return 0;
1597 		}
1598 
1599 		spin_lock_bh(&priv->tx_lock);
1600 
1601 		if (timeout || !priv->pending_tx_pkts) {
1602 			WARN_ON(priv->pending_tx_pkts);
1603 			if (retry)
1604 				wiphy_notice(hw->wiphy, "tx rings drained\n");
1605 			break;
1606 		}
1607 
1608 		if (retry) {
1609 			mwl8k_tx_start(priv);
1610 			retry = 0;
1611 			continue;
1612 		}
1613 
1614 		if (priv->pending_tx_pkts < oldcount) {
1615 			wiphy_notice(hw->wiphy,
1616 				     "waiting for tx rings to drain (%d -> %d pkts)\n",
1617 				     oldcount, priv->pending_tx_pkts);
1618 			retry = 1;
1619 			continue;
1620 		}
1621 
1622 		priv->tx_wait = NULL;
1623 
1624 		wiphy_err(hw->wiphy, "tx rings stuck for %d ms\n",
1625 			  MWL8K_TX_WAIT_TIMEOUT_MS);
1626 		mwl8k_dump_tx_rings(hw);
1627 		priv->hw_restart_in_progress = true;
1628 		ieee80211_queue_work(hw, &priv->fw_reload);
1629 
1630 		rc = -ETIMEDOUT;
1631 	}
1632 	priv->tx_wait = NULL;
1633 	spin_unlock_bh(&priv->tx_lock);
1634 
1635 	return rc;
1636 }
1637 
1638 #define MWL8K_TXD_SUCCESS(status)				\
1639 	((status) & (MWL8K_TXD_STATUS_OK |			\
1640 		     MWL8K_TXD_STATUS_OK_RETRY |		\
1641 		     MWL8K_TXD_STATUS_OK_MORE_RETRY))
1642 
1643 static int mwl8k_tid_queue_mapping(u8 tid)
1644 {
1645 	BUG_ON(tid > 7);
1646 
1647 	switch (tid) {
1648 	case 0:
1649 	case 3:
1650 		return IEEE80211_AC_BE;
1651 	case 1:
1652 	case 2:
1653 		return IEEE80211_AC_BK;
1654 	case 4:
1655 	case 5:
1656 		return IEEE80211_AC_VI;
1657 	case 6:
1658 	case 7:
1659 		return IEEE80211_AC_VO;
1660 	default:
1661 		return -1;
1662 	}
1663 }
1664 
1665 /* The firmware will fill in the rate information
1666  * for each packet that gets queued in the hardware
1667  * and these macros will interpret that info.
1668  */
1669 
1670 #define RI_FORMAT(a)		  (a & 0x0001)
1671 #define RI_RATE_ID_MCS(a)	 ((a & 0x01f8) >> 3)
1672 
1673 static int
1674 mwl8k_txq_reclaim(struct ieee80211_hw *hw, int index, int limit, int force)
1675 {
1676 	struct mwl8k_priv *priv = hw->priv;
1677 	struct mwl8k_tx_queue *txq = priv->txq + index;
1678 	int processed;
1679 
1680 	processed = 0;
1681 	while (txq->len > 0 && limit--) {
1682 		int tx;
1683 		struct mwl8k_tx_desc *tx_desc;
1684 		unsigned long addr;
1685 		int size;
1686 		struct sk_buff *skb;
1687 		struct ieee80211_tx_info *info;
1688 		u32 status;
1689 		struct ieee80211_sta *sta;
1690 		struct mwl8k_sta *sta_info = NULL;
1691 		u16 rate_info;
1692 		struct ieee80211_hdr *wh;
1693 
1694 		tx = txq->head;
1695 		tx_desc = txq->txd + tx;
1696 
1697 		status = le32_to_cpu(tx_desc->status);
1698 
1699 		if (status & MWL8K_TXD_STATUS_FW_OWNED) {
1700 			if (!force)
1701 				break;
1702 			tx_desc->status &=
1703 				~cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED);
1704 		}
1705 
1706 		txq->head = (tx + 1) % MWL8K_TX_DESCS;
1707 		BUG_ON(txq->len == 0);
1708 		txq->len--;
1709 		priv->pending_tx_pkts--;
1710 
1711 		addr = le32_to_cpu(tx_desc->pkt_phys_addr);
1712 		size = le16_to_cpu(tx_desc->pkt_len);
1713 		skb = txq->skb[tx];
1714 		txq->skb[tx] = NULL;
1715 
1716 		BUG_ON(skb == NULL);
1717 		dma_unmap_single(&priv->pdev->dev, addr, size, DMA_TO_DEVICE);
1718 
1719 		mwl8k_remove_dma_header(skb, tx_desc->qos_control);
1720 
1721 		wh = (struct ieee80211_hdr *) skb->data;
1722 
1723 		/* Mark descriptor as unused */
1724 		tx_desc->pkt_phys_addr = 0;
1725 		tx_desc->pkt_len = 0;
1726 
1727 		info = IEEE80211_SKB_CB(skb);
1728 		if (ieee80211_is_data(wh->frame_control)) {
1729 			rcu_read_lock();
1730 			sta = ieee80211_find_sta_by_ifaddr(hw, wh->addr1,
1731 							   wh->addr2);
1732 			if (sta) {
1733 				sta_info = MWL8K_STA(sta);
1734 				BUG_ON(sta_info == NULL);
1735 				rate_info = le16_to_cpu(tx_desc->rate_info);
1736 				/* If rate is < 6.5 Mpbs for an ht station
1737 				 * do not form an ampdu. If the station is a
1738 				 * legacy station (format = 0), do not form an
1739 				 * ampdu
1740 				 */
1741 				if (RI_RATE_ID_MCS(rate_info) < 1 ||
1742 				    RI_FORMAT(rate_info) == 0) {
1743 					sta_info->is_ampdu_allowed = false;
1744 				} else {
1745 					sta_info->is_ampdu_allowed = true;
1746 				}
1747 			}
1748 			rcu_read_unlock();
1749 		}
1750 
1751 		ieee80211_tx_info_clear_status(info);
1752 
1753 		/* Rate control is happening in the firmware.
1754 		 * Ensure no tx rate is being reported.
1755 		 */
1756 		info->status.rates[0].idx = -1;
1757 		info->status.rates[0].count = 1;
1758 
1759 		if (MWL8K_TXD_SUCCESS(status))
1760 			info->flags |= IEEE80211_TX_STAT_ACK;
1761 
1762 		ieee80211_tx_status_irqsafe(hw, skb);
1763 
1764 		processed++;
1765 	}
1766 
1767 	return processed;
1768 }
1769 
1770 /* must be called only when the card's transmit is completely halted */
1771 static void mwl8k_txq_deinit(struct ieee80211_hw *hw, int index)
1772 {
1773 	struct mwl8k_priv *priv = hw->priv;
1774 	struct mwl8k_tx_queue *txq = priv->txq + index;
1775 
1776 	if (txq->txd == NULL)
1777 		return;
1778 
1779 	mwl8k_txq_reclaim(hw, index, INT_MAX, 1);
1780 
1781 	kfree(txq->skb);
1782 	txq->skb = NULL;
1783 
1784 	dma_free_coherent(&priv->pdev->dev,
1785 			  MWL8K_TX_DESCS * sizeof(struct mwl8k_tx_desc),
1786 			  txq->txd, txq->txd_dma);
1787 	txq->txd = NULL;
1788 }
1789 
1790 /* caller must hold priv->stream_lock when calling the stream functions */
1791 static struct mwl8k_ampdu_stream *
1792 mwl8k_add_stream(struct ieee80211_hw *hw, struct ieee80211_sta *sta, u8 tid)
1793 {
1794 	struct mwl8k_ampdu_stream *stream;
1795 	struct mwl8k_priv *priv = hw->priv;
1796 	int i;
1797 
1798 	for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
1799 		stream = &priv->ampdu[i];
1800 		if (stream->state == AMPDU_NO_STREAM) {
1801 			stream->sta = sta;
1802 			stream->state = AMPDU_STREAM_NEW;
1803 			stream->tid = tid;
1804 			stream->idx = i;
1805 			wiphy_debug(hw->wiphy, "Added a new stream for %pM %d",
1806 				    sta->addr, tid);
1807 			return stream;
1808 		}
1809 	}
1810 	return NULL;
1811 }
1812 
1813 static int
1814 mwl8k_start_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1815 {
1816 	int ret;
1817 
1818 	/* if the stream has already been started, don't start it again */
1819 	if (stream->state != AMPDU_STREAM_NEW)
1820 		return 0;
1821 	ret = ieee80211_start_tx_ba_session(stream->sta, stream->tid, 0);
1822 	if (ret)
1823 		wiphy_debug(hw->wiphy, "Failed to start stream for %pM %d: "
1824 			    "%d\n", stream->sta->addr, stream->tid, ret);
1825 	else
1826 		wiphy_debug(hw->wiphy, "Started stream for %pM %d\n",
1827 			    stream->sta->addr, stream->tid);
1828 	return ret;
1829 }
1830 
1831 static void
1832 mwl8k_remove_stream(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream)
1833 {
1834 	wiphy_debug(hw->wiphy, "Remove stream for %pM %d\n", stream->sta->addr,
1835 		    stream->tid);
1836 	memset(stream, 0, sizeof(*stream));
1837 }
1838 
1839 static struct mwl8k_ampdu_stream *
1840 mwl8k_lookup_stream(struct ieee80211_hw *hw, u8 *addr, u8 tid)
1841 {
1842 	struct mwl8k_priv *priv = hw->priv;
1843 	int i;
1844 
1845 	for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
1846 		struct mwl8k_ampdu_stream *stream;
1847 		stream = &priv->ampdu[i];
1848 		if (stream->state == AMPDU_NO_STREAM)
1849 			continue;
1850 		if (!memcmp(stream->sta->addr, addr, ETH_ALEN) &&
1851 		    stream->tid == tid)
1852 			return stream;
1853 	}
1854 	return NULL;
1855 }
1856 
1857 #define MWL8K_AMPDU_PACKET_THRESHOLD 64
1858 static inline bool mwl8k_ampdu_allowed(struct ieee80211_sta *sta, u8 tid)
1859 {
1860 	struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1861 	struct tx_traffic_info *tx_stats;
1862 
1863 	BUG_ON(tid >= MWL8K_MAX_TID);
1864 	tx_stats = &sta_info->tx_stats[tid];
1865 
1866 	return sta_info->is_ampdu_allowed &&
1867 		tx_stats->pkts > MWL8K_AMPDU_PACKET_THRESHOLD;
1868 }
1869 
1870 static inline void mwl8k_tx_count_packet(struct ieee80211_sta *sta, u8 tid)
1871 {
1872 	struct mwl8k_sta *sta_info = MWL8K_STA(sta);
1873 	struct tx_traffic_info *tx_stats;
1874 
1875 	BUG_ON(tid >= MWL8K_MAX_TID);
1876 	tx_stats = &sta_info->tx_stats[tid];
1877 
1878 	if (tx_stats->start_time == 0)
1879 		tx_stats->start_time = jiffies;
1880 
1881 	/* reset the packet count after each second elapses.  If the number of
1882 	 * packets ever exceeds the ampdu_min_traffic threshold, we will allow
1883 	 * an ampdu stream to be started.
1884 	 */
1885 	if (time_after(jiffies, (unsigned long)tx_stats->start_time + HZ)) {
1886 		tx_stats->pkts = 0;
1887 		tx_stats->start_time = 0;
1888 	} else
1889 		tx_stats->pkts++;
1890 }
1891 
1892 /* The hardware ampdu queues start from 5.
1893  * txpriorities for ampdu queues are
1894  * 5 6 7 0 1 2 3 4 ie., queue 5 is highest
1895  * and queue 3 is lowest (queue 4 is reserved)
1896  */
1897 #define BA_QUEUE		5
1898 
1899 static void
1900 mwl8k_txq_xmit(struct ieee80211_hw *hw,
1901 	       int index,
1902 	       struct ieee80211_sta *sta,
1903 	       struct sk_buff *skb)
1904 {
1905 	struct mwl8k_priv *priv = hw->priv;
1906 	struct ieee80211_tx_info *tx_info;
1907 	struct mwl8k_vif *mwl8k_vif;
1908 	struct ieee80211_hdr *wh;
1909 	struct mwl8k_tx_queue *txq;
1910 	struct mwl8k_tx_desc *tx;
1911 	dma_addr_t dma;
1912 	u32 txstatus;
1913 	u8 txdatarate;
1914 	u16 qos;
1915 	int txpriority;
1916 	u8 tid = 0;
1917 	struct mwl8k_ampdu_stream *stream = NULL;
1918 	bool start_ba_session = false;
1919 	bool mgmtframe = false;
1920 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1921 	bool eapol_frame = false;
1922 
1923 	wh = (struct ieee80211_hdr *)skb->data;
1924 	if (ieee80211_is_data_qos(wh->frame_control))
1925 		qos = le16_to_cpu(*((__le16 *)ieee80211_get_qos_ctl(wh)));
1926 	else
1927 		qos = 0;
1928 
1929 	if (skb->protocol == cpu_to_be16(ETH_P_PAE))
1930 		eapol_frame = true;
1931 
1932 	if (ieee80211_is_mgmt(wh->frame_control))
1933 		mgmtframe = true;
1934 
1935 	if (priv->ap_fw)
1936 		mwl8k_encapsulate_tx_frame(priv, skb);
1937 	else
1938 		mwl8k_add_dma_header(priv, skb, 0, 0);
1939 
1940 	wh = &((struct mwl8k_dma_data *)skb->data)->wh;
1941 
1942 	tx_info = IEEE80211_SKB_CB(skb);
1943 	mwl8k_vif = MWL8K_VIF(tx_info->control.vif);
1944 
1945 	if (tx_info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
1946 		wh->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
1947 		wh->seq_ctrl |= cpu_to_le16(mwl8k_vif->seqno);
1948 		mwl8k_vif->seqno += 0x10;
1949 	}
1950 
1951 	/* Setup firmware control bit fields for each frame type.  */
1952 	txstatus = 0;
1953 	txdatarate = 0;
1954 	if (ieee80211_is_mgmt(wh->frame_control) ||
1955 	    ieee80211_is_ctl(wh->frame_control)) {
1956 		txdatarate = 0;
1957 		qos |= MWL8K_QOS_QLEN_UNSPEC | MWL8K_QOS_EOSP;
1958 	} else if (ieee80211_is_data(wh->frame_control)) {
1959 		txdatarate = 1;
1960 		if (is_multicast_ether_addr(wh->addr1))
1961 			txstatus |= MWL8K_TXD_STATUS_MULTICAST_TX;
1962 
1963 		qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
1964 		if (tx_info->flags & IEEE80211_TX_CTL_AMPDU)
1965 			qos |= MWL8K_QOS_ACK_POLICY_BLOCKACK;
1966 		else
1967 			qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
1968 	}
1969 
1970 	/* Queue ADDBA request in the respective data queue.  While setting up
1971 	 * the ampdu stream, mac80211 queues further packets for that
1972 	 * particular ra/tid pair.  However, packets piled up in the hardware
1973 	 * for that ra/tid pair will still go out. ADDBA request and the
1974 	 * related data packets going out from different queues asynchronously
1975 	 * will cause a shift in the receiver window which might result in
1976 	 * ampdu packets getting dropped at the receiver after the stream has
1977 	 * been setup.
1978 	 */
1979 	if (unlikely(ieee80211_is_action(wh->frame_control) &&
1980 	    mgmt->u.action.category == WLAN_CATEGORY_BACK &&
1981 	    mgmt->u.action.u.addba_req.action_code == WLAN_ACTION_ADDBA_REQ &&
1982 	    priv->ap_fw)) {
1983 		u16 capab = le16_to_cpu(mgmt->u.action.u.addba_req.capab);
1984 		tid = (capab & IEEE80211_ADDBA_PARAM_TID_MASK) >> 2;
1985 		index = mwl8k_tid_queue_mapping(tid);
1986 	}
1987 
1988 	txpriority = index;
1989 
1990 	if (priv->ap_fw && sta && sta->deflink.ht_cap.ht_supported && !eapol_frame &&
1991 	    ieee80211_is_data_qos(wh->frame_control)) {
1992 		tid = qos & 0xf;
1993 		mwl8k_tx_count_packet(sta, tid);
1994 		spin_lock(&priv->stream_lock);
1995 		stream = mwl8k_lookup_stream(hw, sta->addr, tid);
1996 		if (stream != NULL) {
1997 			if (stream->state == AMPDU_STREAM_ACTIVE) {
1998 				WARN_ON(!(qos & MWL8K_QOS_ACK_POLICY_BLOCKACK));
1999 				txpriority = (BA_QUEUE + stream->idx) %
2000 					     TOTAL_HW_TX_QUEUES;
2001 				if (stream->idx <= 1)
2002 					index = stream->idx +
2003 						MWL8K_TX_WMM_QUEUES;
2004 
2005 			} else if (stream->state == AMPDU_STREAM_NEW) {
2006 				/* We get here if the driver sends us packets
2007 				 * after we've initiated a stream, but before
2008 				 * our ampdu_action routine has been called
2009 				 * with IEEE80211_AMPDU_TX_START to get the SSN
2010 				 * for the ADDBA request.  So this packet can
2011 				 * go out with no risk of sequence number
2012 				 * mismatch.  No special handling is required.
2013 				 */
2014 			} else {
2015 				/* Drop packets that would go out after the
2016 				 * ADDBA request was sent but before the ADDBA
2017 				 * response is received.  If we don't do this,
2018 				 * the recipient would probably receive it
2019 				 * after the ADDBA request with SSN 0.  This
2020 				 * will cause the recipient's BA receive window
2021 				 * to shift, which would cause the subsequent
2022 				 * packets in the BA stream to be discarded.
2023 				 * mac80211 queues our packets for us in this
2024 				 * case, so this is really just a safety check.
2025 				 */
2026 				wiphy_warn(hw->wiphy,
2027 					   "Cannot send packet while ADDBA "
2028 					   "dialog is underway.\n");
2029 				spin_unlock(&priv->stream_lock);
2030 				dev_kfree_skb(skb);
2031 				return;
2032 			}
2033 		} else {
2034 			/* Defer calling mwl8k_start_stream so that the current
2035 			 * skb can go out before the ADDBA request.  This
2036 			 * prevents sequence number mismatch at the recepient
2037 			 * as described above.
2038 			 */
2039 			if (mwl8k_ampdu_allowed(sta, tid)) {
2040 				stream = mwl8k_add_stream(hw, sta, tid);
2041 				if (stream != NULL)
2042 					start_ba_session = true;
2043 			}
2044 		}
2045 		spin_unlock(&priv->stream_lock);
2046 	} else {
2047 		qos &= ~MWL8K_QOS_ACK_POLICY_MASK;
2048 		qos |= MWL8K_QOS_ACK_POLICY_NORMAL;
2049 	}
2050 
2051 	dma = dma_map_single(&priv->pdev->dev, skb->data, skb->len,
2052 			     DMA_TO_DEVICE);
2053 
2054 	if (dma_mapping_error(&priv->pdev->dev, dma)) {
2055 		wiphy_debug(hw->wiphy,
2056 			    "failed to dma map skb, dropping TX frame.\n");
2057 		if (start_ba_session) {
2058 			spin_lock(&priv->stream_lock);
2059 			mwl8k_remove_stream(hw, stream);
2060 			spin_unlock(&priv->stream_lock);
2061 		}
2062 		dev_kfree_skb(skb);
2063 		return;
2064 	}
2065 
2066 	spin_lock_bh(&priv->tx_lock);
2067 
2068 	txq = priv->txq + index;
2069 
2070 	/* Mgmt frames that go out frequently are probe
2071 	 * responses. Other mgmt frames got out relatively
2072 	 * infrequently. Hence reserve 2 buffers so that
2073 	 * other mgmt frames do not get dropped due to an
2074 	 * already queued probe response in one of the
2075 	 * reserved buffers.
2076 	 */
2077 
2078 	if (txq->len >= MWL8K_TX_DESCS - 2) {
2079 		if (!mgmtframe || txq->len == MWL8K_TX_DESCS) {
2080 			if (start_ba_session) {
2081 				spin_lock(&priv->stream_lock);
2082 				mwl8k_remove_stream(hw, stream);
2083 				spin_unlock(&priv->stream_lock);
2084 			}
2085 			mwl8k_tx_start(priv);
2086 			spin_unlock_bh(&priv->tx_lock);
2087 			dma_unmap_single(&priv->pdev->dev, dma, skb->len,
2088 					 DMA_TO_DEVICE);
2089 			dev_kfree_skb(skb);
2090 			return;
2091 		}
2092 	}
2093 
2094 	BUG_ON(txq->skb[txq->tail] != NULL);
2095 	txq->skb[txq->tail] = skb;
2096 
2097 	tx = txq->txd + txq->tail;
2098 	tx->data_rate = txdatarate;
2099 	tx->tx_priority = txpriority;
2100 	tx->qos_control = cpu_to_le16(qos);
2101 	tx->pkt_phys_addr = cpu_to_le32(dma);
2102 	tx->pkt_len = cpu_to_le16(skb->len);
2103 	tx->rate_info = 0;
2104 	if (!priv->ap_fw && sta != NULL)
2105 		tx->peer_id = MWL8K_STA(sta)->peer_id;
2106 	else
2107 		tx->peer_id = 0;
2108 
2109 	if (priv->ap_fw && ieee80211_is_data(wh->frame_control) && !eapol_frame)
2110 		tx->timestamp = cpu_to_le32(ioread32(priv->regs +
2111 						MWL8K_HW_TIMER_REGISTER));
2112 	else
2113 		tx->timestamp = 0;
2114 
2115 	wmb();
2116 	tx->status = cpu_to_le32(MWL8K_TXD_STATUS_FW_OWNED | txstatus);
2117 
2118 	txq->len++;
2119 	priv->pending_tx_pkts++;
2120 
2121 	txq->tail++;
2122 	if (txq->tail == MWL8K_TX_DESCS)
2123 		txq->tail = 0;
2124 
2125 	mwl8k_tx_start(priv);
2126 
2127 	spin_unlock_bh(&priv->tx_lock);
2128 
2129 	/* Initiate the ampdu session here */
2130 	if (start_ba_session) {
2131 		spin_lock(&priv->stream_lock);
2132 		if (mwl8k_start_stream(hw, stream))
2133 			mwl8k_remove_stream(hw, stream);
2134 		spin_unlock(&priv->stream_lock);
2135 	}
2136 }
2137 
2138 
2139 /*
2140  * Firmware access.
2141  *
2142  * We have the following requirements for issuing firmware commands:
2143  * - Some commands require that the packet transmit path is idle when
2144  *   the command is issued.  (For simplicity, we'll just quiesce the
2145  *   transmit path for every command.)
2146  * - There are certain sequences of commands that need to be issued to
2147  *   the hardware sequentially, with no other intervening commands.
2148  *
2149  * This leads to an implementation of a "firmware lock" as a mutex that
2150  * can be taken recursively, and which is taken by both the low-level
2151  * command submission function (mwl8k_post_cmd) as well as any users of
2152  * that function that require issuing of an atomic sequence of commands,
2153  * and quiesces the transmit path whenever it's taken.
2154  */
2155 static int mwl8k_fw_lock(struct ieee80211_hw *hw)
2156 {
2157 	struct mwl8k_priv *priv = hw->priv;
2158 
2159 	if (priv->fw_mutex_owner != current) {
2160 		int rc;
2161 
2162 		mutex_lock(&priv->fw_mutex);
2163 		ieee80211_stop_queues(hw);
2164 
2165 		rc = mwl8k_tx_wait_empty(hw);
2166 		if (rc) {
2167 			if (!priv->hw_restart_in_progress)
2168 				ieee80211_wake_queues(hw);
2169 
2170 			mutex_unlock(&priv->fw_mutex);
2171 
2172 			return rc;
2173 		}
2174 
2175 		priv->fw_mutex_owner = current;
2176 	}
2177 
2178 	priv->fw_mutex_depth++;
2179 
2180 	return 0;
2181 }
2182 
2183 static void mwl8k_fw_unlock(struct ieee80211_hw *hw)
2184 {
2185 	struct mwl8k_priv *priv = hw->priv;
2186 
2187 	if (!--priv->fw_mutex_depth) {
2188 		if (!priv->hw_restart_in_progress)
2189 			ieee80211_wake_queues(hw);
2190 
2191 		priv->fw_mutex_owner = NULL;
2192 		mutex_unlock(&priv->fw_mutex);
2193 	}
2194 }
2195 
2196 static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable,
2197 			       u32 bitmap);
2198 
2199 /*
2200  * Command processing.
2201  */
2202 
2203 /* Timeout firmware commands after 10s */
2204 #define MWL8K_CMD_TIMEOUT_MS	10000
2205 
2206 static int mwl8k_post_cmd(struct ieee80211_hw *hw, struct mwl8k_cmd_pkt_hdr *cmd)
2207 {
2208 	DECLARE_COMPLETION_ONSTACK(cmd_wait);
2209 	struct mwl8k_priv *priv = hw->priv;
2210 	void __iomem *regs = priv->regs;
2211 	dma_addr_t dma_addr;
2212 	unsigned int dma_size;
2213 	int rc;
2214 	unsigned long timeout = 0;
2215 	u8 buf[32];
2216 	u32 bitmap = 0;
2217 
2218 	wiphy_dbg(hw->wiphy, "Posting %s [%d]\n",
2219 		  mwl8k_cmd_name(cmd->code, buf, sizeof(buf)), cmd->macid);
2220 
2221 	/* Before posting firmware commands that could change the hardware
2222 	 * characteristics, make sure that all BSSes are stopped temporary.
2223 	 * Enable these stopped BSSes after completion of the commands
2224 	 */
2225 
2226 	rc = mwl8k_fw_lock(hw);
2227 	if (rc)
2228 		return rc;
2229 
2230 	if (priv->ap_fw && priv->running_bsses) {
2231 		switch (le16_to_cpu(cmd->code)) {
2232 		case MWL8K_CMD_SET_RF_CHANNEL:
2233 		case MWL8K_CMD_RADIO_CONTROL:
2234 		case MWL8K_CMD_RF_TX_POWER:
2235 		case MWL8K_CMD_TX_POWER:
2236 		case MWL8K_CMD_RF_ANTENNA:
2237 		case MWL8K_CMD_RTS_THRESHOLD:
2238 		case MWL8K_CMD_MIMO_CONFIG:
2239 			bitmap = priv->running_bsses;
2240 			mwl8k_enable_bsses(hw, false, bitmap);
2241 			break;
2242 		}
2243 	}
2244 
2245 	cmd->result = (__force __le16) 0xffff;
2246 	dma_size = le16_to_cpu(cmd->length);
2247 	dma_addr = dma_map_single(&priv->pdev->dev, cmd, dma_size,
2248 				  DMA_BIDIRECTIONAL);
2249 	if (dma_mapping_error(&priv->pdev->dev, dma_addr)) {
2250 		rc = -ENOMEM;
2251 		goto exit;
2252 	}
2253 
2254 	priv->hostcmd_wait = &cmd_wait;
2255 	iowrite32(dma_addr, regs + MWL8K_HIU_GEN_PTR);
2256 	iowrite32(MWL8K_H2A_INT_DOORBELL,
2257 		regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2258 	iowrite32(MWL8K_H2A_INT_DUMMY,
2259 		regs + MWL8K_HIU_H2A_INTERRUPT_EVENTS);
2260 
2261 	timeout = wait_for_completion_timeout(&cmd_wait,
2262 				msecs_to_jiffies(MWL8K_CMD_TIMEOUT_MS));
2263 
2264 	priv->hostcmd_wait = NULL;
2265 
2266 
2267 	dma_unmap_single(&priv->pdev->dev, dma_addr, dma_size,
2268 			 DMA_BIDIRECTIONAL);
2269 
2270 	if (!timeout) {
2271 		wiphy_err(hw->wiphy, "Command %s timeout after %u ms\n",
2272 			  mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2273 			  MWL8K_CMD_TIMEOUT_MS);
2274 		rc = -ETIMEDOUT;
2275 	} else {
2276 		int ms;
2277 
2278 		ms = MWL8K_CMD_TIMEOUT_MS - jiffies_to_msecs(timeout);
2279 
2280 		rc = cmd->result ? -EINVAL : 0;
2281 		if (rc)
2282 			wiphy_err(hw->wiphy, "Command %s error 0x%x\n",
2283 				  mwl8k_cmd_name(cmd->code, buf, sizeof(buf)),
2284 				  le16_to_cpu(cmd->result));
2285 		else if (ms > 2000)
2286 			wiphy_notice(hw->wiphy, "Command %s took %d ms\n",
2287 				     mwl8k_cmd_name(cmd->code,
2288 						    buf, sizeof(buf)),
2289 				     ms);
2290 	}
2291 
2292 exit:
2293 	if (bitmap)
2294 		mwl8k_enable_bsses(hw, true, bitmap);
2295 
2296 	mwl8k_fw_unlock(hw);
2297 
2298 	return rc;
2299 }
2300 
2301 static int mwl8k_post_pervif_cmd(struct ieee80211_hw *hw,
2302 				 struct ieee80211_vif *vif,
2303 				 struct mwl8k_cmd_pkt_hdr *cmd)
2304 {
2305 	if (vif != NULL)
2306 		cmd->macid = MWL8K_VIF(vif)->macid;
2307 	return mwl8k_post_cmd(hw, cmd);
2308 }
2309 
2310 /*
2311  * Setup code shared between STA and AP firmware images.
2312  */
2313 static void mwl8k_setup_2ghz_band(struct ieee80211_hw *hw)
2314 {
2315 	struct mwl8k_priv *priv = hw->priv;
2316 
2317 	BUILD_BUG_ON(sizeof(priv->channels_24) != sizeof(mwl8k_channels_24));
2318 	memcpy(priv->channels_24, mwl8k_channels_24, sizeof(mwl8k_channels_24));
2319 
2320 	BUILD_BUG_ON(sizeof(priv->rates_24) != sizeof(mwl8k_rates_24));
2321 	memcpy(priv->rates_24, mwl8k_rates_24, sizeof(mwl8k_rates_24));
2322 
2323 	priv->band_24.band = NL80211_BAND_2GHZ;
2324 	priv->band_24.channels = priv->channels_24;
2325 	priv->band_24.n_channels = ARRAY_SIZE(mwl8k_channels_24);
2326 	priv->band_24.bitrates = priv->rates_24;
2327 	priv->band_24.n_bitrates = ARRAY_SIZE(mwl8k_rates_24);
2328 
2329 	hw->wiphy->bands[NL80211_BAND_2GHZ] = &priv->band_24;
2330 }
2331 
2332 static void mwl8k_setup_5ghz_band(struct ieee80211_hw *hw)
2333 {
2334 	struct mwl8k_priv *priv = hw->priv;
2335 
2336 	BUILD_BUG_ON(sizeof(priv->channels_50) != sizeof(mwl8k_channels_50));
2337 	memcpy(priv->channels_50, mwl8k_channels_50, sizeof(mwl8k_channels_50));
2338 
2339 	BUILD_BUG_ON(sizeof(priv->rates_50) != sizeof(mwl8k_rates_50));
2340 	memcpy(priv->rates_50, mwl8k_rates_50, sizeof(mwl8k_rates_50));
2341 
2342 	priv->band_50.band = NL80211_BAND_5GHZ;
2343 	priv->band_50.channels = priv->channels_50;
2344 	priv->band_50.n_channels = ARRAY_SIZE(mwl8k_channels_50);
2345 	priv->band_50.bitrates = priv->rates_50;
2346 	priv->band_50.n_bitrates = ARRAY_SIZE(mwl8k_rates_50);
2347 
2348 	hw->wiphy->bands[NL80211_BAND_5GHZ] = &priv->band_50;
2349 }
2350 
2351 /*
2352  * CMD_GET_HW_SPEC (STA version).
2353  */
2354 struct mwl8k_cmd_get_hw_spec_sta {
2355 	struct mwl8k_cmd_pkt_hdr header;
2356 	__u8 hw_rev;
2357 	__u8 host_interface;
2358 	__le16 num_mcaddrs;
2359 	__u8 perm_addr[ETH_ALEN];
2360 	__le16 region_code;
2361 	__le32 fw_rev;
2362 	__le32 ps_cookie;
2363 	__le32 caps;
2364 	__u8 mcs_bitmap[16];
2365 	__le32 rx_queue_ptr;
2366 	__le32 num_tx_queues;
2367 	__le32 tx_queue_ptrs[MWL8K_TX_WMM_QUEUES];
2368 	__le32 caps2;
2369 	__le32 num_tx_desc_per_queue;
2370 	__le32 total_rxd;
2371 } __packed;
2372 
2373 #define MWL8K_CAP_MAX_AMSDU		0x20000000
2374 #define MWL8K_CAP_GREENFIELD		0x08000000
2375 #define MWL8K_CAP_AMPDU			0x04000000
2376 #define MWL8K_CAP_RX_STBC		0x01000000
2377 #define MWL8K_CAP_TX_STBC		0x00800000
2378 #define MWL8K_CAP_SHORTGI_40MHZ		0x00400000
2379 #define MWL8K_CAP_SHORTGI_20MHZ		0x00200000
2380 #define MWL8K_CAP_RX_ANTENNA_MASK	0x000e0000
2381 #define MWL8K_CAP_TX_ANTENNA_MASK	0x0001c000
2382 #define MWL8K_CAP_DELAY_BA		0x00003000
2383 #define MWL8K_CAP_MIMO			0x00000200
2384 #define MWL8K_CAP_40MHZ			0x00000100
2385 #define MWL8K_CAP_BAND_MASK		0x00000007
2386 #define MWL8K_CAP_5GHZ			0x00000004
2387 #define MWL8K_CAP_2GHZ4			0x00000001
2388 
2389 static void
2390 mwl8k_set_ht_caps(struct ieee80211_hw *hw,
2391 		  struct ieee80211_supported_band *band, u32 cap)
2392 {
2393 	int rx_streams;
2394 	int tx_streams;
2395 
2396 	band->ht_cap.ht_supported = 1;
2397 
2398 	if (cap & MWL8K_CAP_MAX_AMSDU)
2399 		band->ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
2400 	if (cap & MWL8K_CAP_GREENFIELD)
2401 		band->ht_cap.cap |= IEEE80211_HT_CAP_GRN_FLD;
2402 	if (cap & MWL8K_CAP_AMPDU) {
2403 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2404 		band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
2405 		band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_NONE;
2406 	}
2407 	if (cap & MWL8K_CAP_RX_STBC)
2408 		band->ht_cap.cap |= IEEE80211_HT_CAP_RX_STBC;
2409 	if (cap & MWL8K_CAP_TX_STBC)
2410 		band->ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
2411 	if (cap & MWL8K_CAP_SHORTGI_40MHZ)
2412 		band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
2413 	if (cap & MWL8K_CAP_SHORTGI_20MHZ)
2414 		band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
2415 	if (cap & MWL8K_CAP_DELAY_BA)
2416 		band->ht_cap.cap |= IEEE80211_HT_CAP_DELAY_BA;
2417 	if (cap & MWL8K_CAP_40MHZ)
2418 		band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
2419 
2420 	rx_streams = hweight32(cap & MWL8K_CAP_RX_ANTENNA_MASK);
2421 	tx_streams = hweight32(cap & MWL8K_CAP_TX_ANTENNA_MASK);
2422 
2423 	band->ht_cap.mcs.rx_mask[0] = 0xff;
2424 	if (rx_streams >= 2)
2425 		band->ht_cap.mcs.rx_mask[1] = 0xff;
2426 	if (rx_streams >= 3)
2427 		band->ht_cap.mcs.rx_mask[2] = 0xff;
2428 	band->ht_cap.mcs.rx_mask[4] = 0x01;
2429 	band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
2430 
2431 	if (rx_streams != tx_streams) {
2432 		band->ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
2433 		band->ht_cap.mcs.tx_params |= (tx_streams - 1) <<
2434 				IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2435 	}
2436 }
2437 
2438 static void
2439 mwl8k_set_caps(struct ieee80211_hw *hw, u32 caps)
2440 {
2441 	struct mwl8k_priv *priv = hw->priv;
2442 
2443 	if (priv->caps)
2444 		return;
2445 
2446 	if ((caps & MWL8K_CAP_2GHZ4) || !(caps & MWL8K_CAP_BAND_MASK)) {
2447 		mwl8k_setup_2ghz_band(hw);
2448 		if (caps & MWL8K_CAP_MIMO)
2449 			mwl8k_set_ht_caps(hw, &priv->band_24, caps);
2450 	}
2451 
2452 	if (caps & MWL8K_CAP_5GHZ) {
2453 		mwl8k_setup_5ghz_band(hw);
2454 		if (caps & MWL8K_CAP_MIMO)
2455 			mwl8k_set_ht_caps(hw, &priv->band_50, caps);
2456 	}
2457 
2458 	priv->caps = caps;
2459 }
2460 
2461 static int mwl8k_cmd_get_hw_spec_sta(struct ieee80211_hw *hw)
2462 {
2463 	struct mwl8k_priv *priv = hw->priv;
2464 	struct mwl8k_cmd_get_hw_spec_sta *cmd;
2465 	int rc;
2466 	int i;
2467 
2468 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2469 	if (cmd == NULL)
2470 		return -ENOMEM;
2471 
2472 	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2473 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2474 
2475 	memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2476 	cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2477 	cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2478 	cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2479 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
2480 		cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[i].txd_dma);
2481 	cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2482 	cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2483 
2484 	rc = mwl8k_post_cmd(hw, &cmd->header);
2485 
2486 	if (!rc) {
2487 		SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2488 		priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2489 		priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2490 		priv->hw_rev = cmd->hw_rev;
2491 		mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
2492 		priv->ap_macids_supported = 0x00000000;
2493 		priv->sta_macids_supported = 0x00000001;
2494 	}
2495 
2496 	kfree(cmd);
2497 	return rc;
2498 }
2499 
2500 /*
2501  * CMD_GET_HW_SPEC (AP version).
2502  */
2503 struct mwl8k_cmd_get_hw_spec_ap {
2504 	struct mwl8k_cmd_pkt_hdr header;
2505 	__u8 hw_rev;
2506 	__u8 host_interface;
2507 	__le16 num_wcb;
2508 	__le16 num_mcaddrs;
2509 	__u8 perm_addr[ETH_ALEN];
2510 	__le16 region_code;
2511 	__le16 num_antenna;
2512 	__le32 fw_rev;
2513 	__le32 wcbbase0;
2514 	__le32 rxwrptr;
2515 	__le32 rxrdptr;
2516 	__le32 ps_cookie;
2517 	__le32 wcbbase1;
2518 	__le32 wcbbase2;
2519 	__le32 wcbbase3;
2520 	__le32 fw_api_version;
2521 	__le32 caps;
2522 	__le32 num_of_ampdu_queues;
2523 	__le32 wcbbase_ampdu[MWL8K_MAX_AMPDU_QUEUES];
2524 } __packed;
2525 
2526 static int mwl8k_cmd_get_hw_spec_ap(struct ieee80211_hw *hw)
2527 {
2528 	struct mwl8k_priv *priv = hw->priv;
2529 	struct mwl8k_cmd_get_hw_spec_ap *cmd;
2530 	int rc, i;
2531 	u32 api_version;
2532 
2533 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2534 	if (cmd == NULL)
2535 		return -ENOMEM;
2536 
2537 	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_HW_SPEC);
2538 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2539 
2540 	memset(cmd->perm_addr, 0xff, sizeof(cmd->perm_addr));
2541 	cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2542 
2543 	rc = mwl8k_post_cmd(hw, &cmd->header);
2544 
2545 	if (!rc) {
2546 		int off;
2547 
2548 		api_version = le32_to_cpu(cmd->fw_api_version);
2549 		if (priv->device_info->fw_api_ap != api_version) {
2550 			printk(KERN_ERR "%s: Unsupported fw API version for %s."
2551 			       "  Expected %d got %d.\n", MWL8K_NAME,
2552 			       priv->device_info->part_name,
2553 			       priv->device_info->fw_api_ap,
2554 			       api_version);
2555 			rc = -EINVAL;
2556 			goto done;
2557 		}
2558 		SET_IEEE80211_PERM_ADDR(hw, cmd->perm_addr);
2559 		priv->num_mcaddrs = le16_to_cpu(cmd->num_mcaddrs);
2560 		priv->fw_rev = le32_to_cpu(cmd->fw_rev);
2561 		priv->hw_rev = cmd->hw_rev;
2562 		mwl8k_set_caps(hw, le32_to_cpu(cmd->caps));
2563 		priv->ap_macids_supported = 0x000000ff;
2564 		priv->sta_macids_supported = 0x00000100;
2565 		priv->num_ampdu_queues = le32_to_cpu(cmd->num_of_ampdu_queues);
2566 		if (priv->num_ampdu_queues > MWL8K_MAX_AMPDU_QUEUES) {
2567 			wiphy_warn(hw->wiphy, "fw reported %d ampdu queues"
2568 				   " but we only support %d.\n",
2569 				   priv->num_ampdu_queues,
2570 				   MWL8K_MAX_AMPDU_QUEUES);
2571 			priv->num_ampdu_queues = MWL8K_MAX_AMPDU_QUEUES;
2572 		}
2573 		off = le32_to_cpu(cmd->rxwrptr) & 0xffff;
2574 		iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
2575 
2576 		off = le32_to_cpu(cmd->rxrdptr) & 0xffff;
2577 		iowrite32(priv->rxq[0].rxd_dma, priv->sram + off);
2578 
2579 		priv->txq_offset[0] = le32_to_cpu(cmd->wcbbase0) & 0xffff;
2580 		priv->txq_offset[1] = le32_to_cpu(cmd->wcbbase1) & 0xffff;
2581 		priv->txq_offset[2] = le32_to_cpu(cmd->wcbbase2) & 0xffff;
2582 		priv->txq_offset[3] = le32_to_cpu(cmd->wcbbase3) & 0xffff;
2583 
2584 		for (i = 0; i < priv->num_ampdu_queues; i++)
2585 			priv->txq_offset[i + MWL8K_TX_WMM_QUEUES] =
2586 				le32_to_cpu(cmd->wcbbase_ampdu[i]) & 0xffff;
2587 	}
2588 
2589 done:
2590 	kfree(cmd);
2591 	return rc;
2592 }
2593 
2594 /*
2595  * CMD_SET_HW_SPEC.
2596  */
2597 struct mwl8k_cmd_set_hw_spec {
2598 	struct mwl8k_cmd_pkt_hdr header;
2599 	__u8 hw_rev;
2600 	__u8 host_interface;
2601 	__le16 num_mcaddrs;
2602 	__u8 perm_addr[ETH_ALEN];
2603 	__le16 region_code;
2604 	__le32 fw_rev;
2605 	__le32 ps_cookie;
2606 	__le32 caps;
2607 	__le32 rx_queue_ptr;
2608 	__le32 num_tx_queues;
2609 	__le32 tx_queue_ptrs[MWL8K_MAX_TX_QUEUES];
2610 	__le32 flags;
2611 	__le32 num_tx_desc_per_queue;
2612 	__le32 total_rxd;
2613 } __packed;
2614 
2615 /* If enabled, MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY will cause
2616  * packets to expire 500 ms after the timestamp in the tx descriptor.  That is,
2617  * the packets that are queued for more than 500ms, will be dropped in the
2618  * hardware. This helps minimizing the issues caused due to head-of-line
2619  * blocking where a slow client can hog the bandwidth and affect traffic to a
2620  * faster client.
2621  */
2622 #define MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY	0x00000400
2623 #define MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR	0x00000200
2624 #define MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT		0x00000080
2625 #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP	0x00000020
2626 #define MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON		0x00000010
2627 
2628 static int mwl8k_cmd_set_hw_spec(struct ieee80211_hw *hw)
2629 {
2630 	struct mwl8k_priv *priv = hw->priv;
2631 	struct mwl8k_cmd_set_hw_spec *cmd;
2632 	int rc;
2633 	int i;
2634 
2635 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2636 	if (cmd == NULL)
2637 		return -ENOMEM;
2638 
2639 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_HW_SPEC);
2640 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2641 
2642 	cmd->ps_cookie = cpu_to_le32(priv->cookie_dma);
2643 	cmd->rx_queue_ptr = cpu_to_le32(priv->rxq[0].rxd_dma);
2644 	cmd->num_tx_queues = cpu_to_le32(mwl8k_tx_queues(priv));
2645 
2646 	/*
2647 	 * Mac80211 stack has Q0 as highest priority and Q3 as lowest in
2648 	 * that order. Firmware has Q3 as highest priority and Q0 as lowest
2649 	 * in that order. Map Q3 of mac80211 to Q0 of firmware so that the
2650 	 * priority is interpreted the right way in firmware.
2651 	 */
2652 	for (i = 0; i < mwl8k_tx_queues(priv); i++) {
2653 		int j = mwl8k_tx_queues(priv) - 1 - i;
2654 		cmd->tx_queue_ptrs[i] = cpu_to_le32(priv->txq[j].txd_dma);
2655 	}
2656 
2657 	cmd->flags = cpu_to_le32(MWL8K_SET_HW_SPEC_FLAG_HOST_DECR_MGMT |
2658 				 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_PROBERESP |
2659 				 MWL8K_SET_HW_SPEC_FLAG_HOSTFORM_BEACON |
2660 				 MWL8K_SET_HW_SPEC_FLAG_ENABLE_LIFE_TIME_EXPIRY |
2661 				 MWL8K_SET_HW_SPEC_FLAG_GENERATE_CCMP_HDR);
2662 	cmd->num_tx_desc_per_queue = cpu_to_le32(MWL8K_TX_DESCS);
2663 	cmd->total_rxd = cpu_to_le32(MWL8K_RX_DESCS);
2664 
2665 	rc = mwl8k_post_cmd(hw, &cmd->header);
2666 	kfree(cmd);
2667 
2668 	return rc;
2669 }
2670 
2671 /*
2672  * CMD_MAC_MULTICAST_ADR.
2673  */
2674 struct mwl8k_cmd_mac_multicast_adr {
2675 	struct mwl8k_cmd_pkt_hdr header;
2676 	__le16 action;
2677 	__le16 numaddr;
2678 	__u8 addr[][ETH_ALEN];
2679 };
2680 
2681 #define MWL8K_ENABLE_RX_DIRECTED	0x0001
2682 #define MWL8K_ENABLE_RX_MULTICAST	0x0002
2683 #define MWL8K_ENABLE_RX_ALL_MULTICAST	0x0004
2684 #define MWL8K_ENABLE_RX_BROADCAST	0x0008
2685 
2686 static struct mwl8k_cmd_pkt_hdr *
2687 __mwl8k_cmd_mac_multicast_adr(struct ieee80211_hw *hw, int allmulti,
2688 			      struct netdev_hw_addr_list *mc_list)
2689 {
2690 	struct mwl8k_priv *priv = hw->priv;
2691 	struct mwl8k_cmd_mac_multicast_adr *cmd;
2692 	int size;
2693 	int mc_count = 0;
2694 
2695 	if (mc_list)
2696 		mc_count = netdev_hw_addr_list_count(mc_list);
2697 
2698 	if (allmulti || mc_count > priv->num_mcaddrs) {
2699 		allmulti = 1;
2700 		mc_count = 0;
2701 	}
2702 
2703 	size = sizeof(*cmd) + mc_count * ETH_ALEN;
2704 
2705 	cmd = kzalloc(size, GFP_ATOMIC);
2706 	if (cmd == NULL)
2707 		return NULL;
2708 
2709 	cmd->header.code = cpu_to_le16(MWL8K_CMD_MAC_MULTICAST_ADR);
2710 	cmd->header.length = cpu_to_le16(size);
2711 	cmd->action = cpu_to_le16(MWL8K_ENABLE_RX_DIRECTED |
2712 				  MWL8K_ENABLE_RX_BROADCAST);
2713 
2714 	if (allmulti) {
2715 		cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_ALL_MULTICAST);
2716 	} else if (mc_count) {
2717 		struct netdev_hw_addr *ha;
2718 		int i = 0;
2719 
2720 		cmd->action |= cpu_to_le16(MWL8K_ENABLE_RX_MULTICAST);
2721 		cmd->numaddr = cpu_to_le16(mc_count);
2722 		netdev_hw_addr_list_for_each(ha, mc_list) {
2723 			memcpy(cmd->addr[i++], ha->addr, ETH_ALEN);
2724 		}
2725 	}
2726 
2727 	return &cmd->header;
2728 }
2729 
2730 /*
2731  * CMD_GET_STAT.
2732  */
2733 struct mwl8k_cmd_get_stat {
2734 	struct mwl8k_cmd_pkt_hdr header;
2735 	__le32 stats[64];
2736 } __packed;
2737 
2738 #define MWL8K_STAT_ACK_FAILURE	9
2739 #define MWL8K_STAT_RTS_FAILURE	12
2740 #define MWL8K_STAT_FCS_ERROR	24
2741 #define MWL8K_STAT_RTS_SUCCESS	11
2742 
2743 static int mwl8k_cmd_get_stat(struct ieee80211_hw *hw,
2744 			      struct ieee80211_low_level_stats *stats)
2745 {
2746 	struct mwl8k_cmd_get_stat *cmd;
2747 	int rc;
2748 
2749 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2750 	if (cmd == NULL)
2751 		return -ENOMEM;
2752 
2753 	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_STAT);
2754 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2755 
2756 	rc = mwl8k_post_cmd(hw, &cmd->header);
2757 	if (!rc) {
2758 		stats->dot11ACKFailureCount =
2759 			le32_to_cpu(cmd->stats[MWL8K_STAT_ACK_FAILURE]);
2760 		stats->dot11RTSFailureCount =
2761 			le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_FAILURE]);
2762 		stats->dot11FCSErrorCount =
2763 			le32_to_cpu(cmd->stats[MWL8K_STAT_FCS_ERROR]);
2764 		stats->dot11RTSSuccessCount =
2765 			le32_to_cpu(cmd->stats[MWL8K_STAT_RTS_SUCCESS]);
2766 	}
2767 	kfree(cmd);
2768 
2769 	return rc;
2770 }
2771 
2772 /*
2773  * CMD_RADIO_CONTROL.
2774  */
2775 struct mwl8k_cmd_radio_control {
2776 	struct mwl8k_cmd_pkt_hdr header;
2777 	__le16 action;
2778 	__le16 control;
2779 	__le16 radio_on;
2780 } __packed;
2781 
2782 static int
2783 mwl8k_cmd_radio_control(struct ieee80211_hw *hw, bool enable, bool force)
2784 {
2785 	struct mwl8k_priv *priv = hw->priv;
2786 	struct mwl8k_cmd_radio_control *cmd;
2787 	int rc;
2788 
2789 	if (enable == priv->radio_on && !force)
2790 		return 0;
2791 
2792 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2793 	if (cmd == NULL)
2794 		return -ENOMEM;
2795 
2796 	cmd->header.code = cpu_to_le16(MWL8K_CMD_RADIO_CONTROL);
2797 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2798 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2799 	cmd->control = cpu_to_le16(priv->radio_short_preamble ? 3 : 1);
2800 	cmd->radio_on = cpu_to_le16(enable ? 0x0001 : 0x0000);
2801 
2802 	rc = mwl8k_post_cmd(hw, &cmd->header);
2803 	kfree(cmd);
2804 
2805 	if (!rc)
2806 		priv->radio_on = enable;
2807 
2808 	return rc;
2809 }
2810 
2811 static int mwl8k_cmd_radio_disable(struct ieee80211_hw *hw)
2812 {
2813 	return mwl8k_cmd_radio_control(hw, 0, 0);
2814 }
2815 
2816 static int mwl8k_cmd_radio_enable(struct ieee80211_hw *hw)
2817 {
2818 	return mwl8k_cmd_radio_control(hw, 1, 0);
2819 }
2820 
2821 static int
2822 mwl8k_set_radio_preamble(struct ieee80211_hw *hw, bool short_preamble)
2823 {
2824 	struct mwl8k_priv *priv = hw->priv;
2825 
2826 	priv->radio_short_preamble = short_preamble;
2827 
2828 	return mwl8k_cmd_radio_control(hw, 1, 1);
2829 }
2830 
2831 /*
2832  * CMD_RF_TX_POWER.
2833  */
2834 #define MWL8K_RF_TX_POWER_LEVEL_TOTAL	8
2835 
2836 struct mwl8k_cmd_rf_tx_power {
2837 	struct mwl8k_cmd_pkt_hdr header;
2838 	__le16 action;
2839 	__le16 support_level;
2840 	__le16 current_level;
2841 	__le16 reserved;
2842 	__le16 power_level_list[MWL8K_RF_TX_POWER_LEVEL_TOTAL];
2843 } __packed;
2844 
2845 static int mwl8k_cmd_rf_tx_power(struct ieee80211_hw *hw, int dBm)
2846 {
2847 	struct mwl8k_cmd_rf_tx_power *cmd;
2848 	int rc;
2849 
2850 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2851 	if (cmd == NULL)
2852 		return -ENOMEM;
2853 
2854 	cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_TX_POWER);
2855 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2856 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
2857 	cmd->support_level = cpu_to_le16(dBm);
2858 
2859 	rc = mwl8k_post_cmd(hw, &cmd->header);
2860 	kfree(cmd);
2861 
2862 	return rc;
2863 }
2864 
2865 /*
2866  * CMD_TX_POWER.
2867  */
2868 #define MWL8K_TX_POWER_LEVEL_TOTAL      12
2869 
2870 struct mwl8k_cmd_tx_power {
2871 	struct mwl8k_cmd_pkt_hdr header;
2872 	__le16 action;
2873 	__le16 band;
2874 	__le16 channel;
2875 	__le16 bw;
2876 	__le16 sub_ch;
2877 	__le16 power_level_list[MWL8K_TX_POWER_LEVEL_TOTAL];
2878 } __packed;
2879 
2880 static int mwl8k_cmd_tx_power(struct ieee80211_hw *hw,
2881 				     struct ieee80211_conf *conf,
2882 				     unsigned short pwr)
2883 {
2884 	struct ieee80211_channel *channel = conf->chandef.chan;
2885 	enum nl80211_channel_type channel_type =
2886 		cfg80211_get_chandef_type(&conf->chandef);
2887 	struct mwl8k_cmd_tx_power *cmd;
2888 	int rc;
2889 	int i;
2890 
2891 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2892 	if (cmd == NULL)
2893 		return -ENOMEM;
2894 
2895 	cmd->header.code = cpu_to_le16(MWL8K_CMD_TX_POWER);
2896 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2897 	cmd->action = cpu_to_le16(MWL8K_CMD_SET_LIST);
2898 
2899 	if (channel->band == NL80211_BAND_2GHZ)
2900 		cmd->band = cpu_to_le16(0x1);
2901 	else if (channel->band == NL80211_BAND_5GHZ)
2902 		cmd->band = cpu_to_le16(0x4);
2903 
2904 	cmd->channel = cpu_to_le16(channel->hw_value);
2905 
2906 	if (channel_type == NL80211_CHAN_NO_HT ||
2907 	    channel_type == NL80211_CHAN_HT20) {
2908 		cmd->bw = cpu_to_le16(0x2);
2909 	} else {
2910 		cmd->bw = cpu_to_le16(0x4);
2911 		if (channel_type == NL80211_CHAN_HT40MINUS)
2912 			cmd->sub_ch = cpu_to_le16(0x3);
2913 		else if (channel_type == NL80211_CHAN_HT40PLUS)
2914 			cmd->sub_ch = cpu_to_le16(0x1);
2915 	}
2916 
2917 	for (i = 0; i < MWL8K_TX_POWER_LEVEL_TOTAL; i++)
2918 		cmd->power_level_list[i] = cpu_to_le16(pwr);
2919 
2920 	rc = mwl8k_post_cmd(hw, &cmd->header);
2921 	kfree(cmd);
2922 
2923 	return rc;
2924 }
2925 
2926 /*
2927  * CMD_RF_ANTENNA.
2928  */
2929 struct mwl8k_cmd_rf_antenna {
2930 	struct mwl8k_cmd_pkt_hdr header;
2931 	__le16 antenna;
2932 	__le16 mode;
2933 } __packed;
2934 
2935 #define MWL8K_RF_ANTENNA_RX		1
2936 #define MWL8K_RF_ANTENNA_TX		2
2937 
2938 static int
2939 mwl8k_cmd_rf_antenna(struct ieee80211_hw *hw, int antenna, int mask)
2940 {
2941 	struct mwl8k_cmd_rf_antenna *cmd;
2942 	int rc;
2943 
2944 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
2945 	if (cmd == NULL)
2946 		return -ENOMEM;
2947 
2948 	cmd->header.code = cpu_to_le16(MWL8K_CMD_RF_ANTENNA);
2949 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
2950 	cmd->antenna = cpu_to_le16(antenna);
2951 	cmd->mode = cpu_to_le16(mask);
2952 
2953 	rc = mwl8k_post_cmd(hw, &cmd->header);
2954 	kfree(cmd);
2955 
2956 	return rc;
2957 }
2958 
2959 /*
2960  * CMD_SET_BEACON.
2961  */
2962 struct mwl8k_cmd_set_beacon {
2963 	struct mwl8k_cmd_pkt_hdr header;
2964 	__le16 beacon_len;
2965 	__u8 beacon[];
2966 };
2967 
2968 static int mwl8k_cmd_set_beacon(struct ieee80211_hw *hw,
2969 				struct ieee80211_vif *vif, u8 *beacon, int len)
2970 {
2971 	struct mwl8k_cmd_set_beacon *cmd;
2972 	int rc;
2973 
2974 	cmd = kzalloc(sizeof(*cmd) + len, GFP_KERNEL);
2975 	if (cmd == NULL)
2976 		return -ENOMEM;
2977 
2978 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_BEACON);
2979 	cmd->header.length = cpu_to_le16(sizeof(*cmd) + len);
2980 	cmd->beacon_len = cpu_to_le16(len);
2981 	memcpy(cmd->beacon, beacon, len);
2982 
2983 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
2984 	kfree(cmd);
2985 
2986 	return rc;
2987 }
2988 
2989 /*
2990  * CMD_SET_PRE_SCAN.
2991  */
2992 struct mwl8k_cmd_set_pre_scan {
2993 	struct mwl8k_cmd_pkt_hdr header;
2994 } __packed;
2995 
2996 static int mwl8k_cmd_set_pre_scan(struct ieee80211_hw *hw)
2997 {
2998 	struct mwl8k_cmd_set_pre_scan *cmd;
2999 	int rc;
3000 
3001 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3002 	if (cmd == NULL)
3003 		return -ENOMEM;
3004 
3005 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_PRE_SCAN);
3006 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3007 
3008 	rc = mwl8k_post_cmd(hw, &cmd->header);
3009 	kfree(cmd);
3010 
3011 	return rc;
3012 }
3013 
3014 /*
3015  * CMD_BBP_REG_ACCESS.
3016  */
3017 struct mwl8k_cmd_bbp_reg_access {
3018 	struct mwl8k_cmd_pkt_hdr header;
3019 	__le16 action;
3020 	__le16 offset;
3021 	u8 value;
3022 	u8 rsrv[3];
3023 } __packed;
3024 
3025 static int
3026 mwl8k_cmd_bbp_reg_access(struct ieee80211_hw *hw,
3027 			 u16 action,
3028 			 u16 offset,
3029 			 u8 *value)
3030 {
3031 	struct mwl8k_cmd_bbp_reg_access *cmd;
3032 	int rc;
3033 
3034 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3035 	if (cmd == NULL)
3036 		return -ENOMEM;
3037 
3038 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BBP_REG_ACCESS);
3039 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3040 	cmd->action = cpu_to_le16(action);
3041 	cmd->offset = cpu_to_le16(offset);
3042 
3043 	rc = mwl8k_post_cmd(hw, &cmd->header);
3044 
3045 	if (!rc)
3046 		*value = cmd->value;
3047 	else
3048 		*value = 0;
3049 
3050 	kfree(cmd);
3051 
3052 	return rc;
3053 }
3054 
3055 /*
3056  * CMD_SET_POST_SCAN.
3057  */
3058 struct mwl8k_cmd_set_post_scan {
3059 	struct mwl8k_cmd_pkt_hdr header;
3060 	__le32 isibss;
3061 	__u8 bssid[ETH_ALEN];
3062 } __packed;
3063 
3064 static int
3065 mwl8k_cmd_set_post_scan(struct ieee80211_hw *hw, const __u8 *mac)
3066 {
3067 	struct mwl8k_cmd_set_post_scan *cmd;
3068 	int rc;
3069 
3070 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3071 	if (cmd == NULL)
3072 		return -ENOMEM;
3073 
3074 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_POST_SCAN);
3075 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3076 	cmd->isibss = 0;
3077 	memcpy(cmd->bssid, mac, ETH_ALEN);
3078 
3079 	rc = mwl8k_post_cmd(hw, &cmd->header);
3080 	kfree(cmd);
3081 
3082 	return rc;
3083 }
3084 
3085 static int freq_to_idx(struct mwl8k_priv *priv, int freq)
3086 {
3087 	struct ieee80211_supported_band *sband;
3088 	int band, ch, idx = 0;
3089 
3090 	for (band = NL80211_BAND_2GHZ; band < NUM_NL80211_BANDS; band++) {
3091 		sband = priv->hw->wiphy->bands[band];
3092 		if (!sband)
3093 			continue;
3094 
3095 		for (ch = 0; ch < sband->n_channels; ch++, idx++)
3096 			if (sband->channels[ch].center_freq == freq)
3097 				goto exit;
3098 	}
3099 
3100 exit:
3101 	return idx;
3102 }
3103 
3104 static void mwl8k_update_survey(struct mwl8k_priv *priv,
3105 				struct ieee80211_channel *channel)
3106 {
3107 	u32 cca_cnt, rx_rdy;
3108 	s8 nf = 0, idx;
3109 	struct survey_info *survey;
3110 
3111 	idx = freq_to_idx(priv, priv->acs_chan->center_freq);
3112 	if (idx >= MWL8K_NUM_CHANS) {
3113 		wiphy_err(priv->hw->wiphy, "Failed to update survey\n");
3114 		return;
3115 	}
3116 
3117 	survey = &priv->survey[idx];
3118 
3119 	cca_cnt = ioread32(priv->regs + NOK_CCA_CNT_REG);
3120 	cca_cnt /= 1000; /* uSecs to mSecs */
3121 	survey->time_busy = (u64) cca_cnt;
3122 
3123 	rx_rdy = ioread32(priv->regs + BBU_RXRDY_CNT_REG);
3124 	rx_rdy /= 1000; /* uSecs to mSecs */
3125 	survey->time_rx = (u64) rx_rdy;
3126 
3127 	priv->channel_time = jiffies - priv->channel_time;
3128 	survey->time = jiffies_to_msecs(priv->channel_time);
3129 
3130 	survey->channel = channel;
3131 
3132 	mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &nf);
3133 
3134 	/* Make sure sign is negative else ACS  at hostapd fails */
3135 	survey->noise = nf * -1;
3136 
3137 	survey->filled = SURVEY_INFO_NOISE_DBM |
3138 			 SURVEY_INFO_TIME |
3139 			 SURVEY_INFO_TIME_BUSY |
3140 			 SURVEY_INFO_TIME_RX;
3141 }
3142 
3143 /*
3144  * CMD_SET_RF_CHANNEL.
3145  */
3146 struct mwl8k_cmd_set_rf_channel {
3147 	struct mwl8k_cmd_pkt_hdr header;
3148 	__le16 action;
3149 	__u8 current_channel;
3150 	__le32 channel_flags;
3151 } __packed;
3152 
3153 static int mwl8k_cmd_set_rf_channel(struct ieee80211_hw *hw,
3154 				    struct ieee80211_conf *conf)
3155 {
3156 	struct ieee80211_channel *channel = conf->chandef.chan;
3157 	enum nl80211_channel_type channel_type =
3158 		cfg80211_get_chandef_type(&conf->chandef);
3159 	struct mwl8k_cmd_set_rf_channel *cmd;
3160 	struct mwl8k_priv *priv = hw->priv;
3161 	int rc;
3162 
3163 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3164 	if (cmd == NULL)
3165 		return -ENOMEM;
3166 
3167 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RF_CHANNEL);
3168 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3169 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3170 	cmd->current_channel = channel->hw_value;
3171 
3172 	if (channel->band == NL80211_BAND_2GHZ)
3173 		cmd->channel_flags |= cpu_to_le32(0x00000001);
3174 	else if (channel->band == NL80211_BAND_5GHZ)
3175 		cmd->channel_flags |= cpu_to_le32(0x00000004);
3176 
3177 	if (!priv->sw_scan_start) {
3178 		if (channel_type == NL80211_CHAN_NO_HT ||
3179 		    channel_type == NL80211_CHAN_HT20)
3180 			cmd->channel_flags |= cpu_to_le32(0x00000080);
3181 		else if (channel_type == NL80211_CHAN_HT40MINUS)
3182 			cmd->channel_flags |= cpu_to_le32(0x000001900);
3183 		else if (channel_type == NL80211_CHAN_HT40PLUS)
3184 			cmd->channel_flags |= cpu_to_le32(0x000000900);
3185 	} else {
3186 		cmd->channel_flags |= cpu_to_le32(0x00000080);
3187 	}
3188 
3189 	if (priv->sw_scan_start) {
3190 		/* Store current channel stats
3191 		 * before switching to newer one.
3192 		 * This will be processed only for AP fw.
3193 		 */
3194 		if (priv->channel_time != 0)
3195 			mwl8k_update_survey(priv, priv->acs_chan);
3196 
3197 		priv->channel_time = jiffies;
3198 		priv->acs_chan =  channel;
3199 	}
3200 
3201 	rc = mwl8k_post_cmd(hw, &cmd->header);
3202 	kfree(cmd);
3203 
3204 	return rc;
3205 }
3206 
3207 /*
3208  * CMD_SET_AID.
3209  */
3210 #define MWL8K_FRAME_PROT_DISABLED			0x00
3211 #define MWL8K_FRAME_PROT_11G				0x07
3212 #define MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY		0x02
3213 #define MWL8K_FRAME_PROT_11N_HT_ALL			0x06
3214 
3215 struct mwl8k_cmd_update_set_aid {
3216 	struct mwl8k_cmd_pkt_hdr header;
3217 	__le16	aid;
3218 
3219 	 /* AP's MAC address (BSSID) */
3220 	__u8	bssid[ETH_ALEN];
3221 	__le16	protection_mode;
3222 	__u8	supp_rates[14];
3223 } __packed;
3224 
3225 static void legacy_rate_mask_to_array(u8 *rates, u32 mask)
3226 {
3227 	int i;
3228 	int j;
3229 
3230 	/*
3231 	 * Clear nonstandard rate 4.
3232 	 */
3233 	mask &= 0x1fef;
3234 
3235 	for (i = 0, j = 0; i < 13; i++) {
3236 		if (mask & (1 << i))
3237 			rates[j++] = mwl8k_rates_24[i].hw_value;
3238 	}
3239 }
3240 
3241 static int
3242 mwl8k_cmd_set_aid(struct ieee80211_hw *hw,
3243 		  struct ieee80211_vif *vif, u32 legacy_rate_mask)
3244 {
3245 	struct mwl8k_cmd_update_set_aid *cmd;
3246 	u16 prot_mode;
3247 	int rc;
3248 
3249 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3250 	if (cmd == NULL)
3251 		return -ENOMEM;
3252 
3253 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_AID);
3254 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3255 	cmd->aid = cpu_to_le16(vif->cfg.aid);
3256 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
3257 
3258 	if (vif->bss_conf.use_cts_prot) {
3259 		prot_mode = MWL8K_FRAME_PROT_11G;
3260 	} else {
3261 		switch (vif->bss_conf.ht_operation_mode &
3262 			IEEE80211_HT_OP_MODE_PROTECTION) {
3263 		case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
3264 			prot_mode = MWL8K_FRAME_PROT_11N_HT_40MHZ_ONLY;
3265 			break;
3266 		case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
3267 			prot_mode = MWL8K_FRAME_PROT_11N_HT_ALL;
3268 			break;
3269 		default:
3270 			prot_mode = MWL8K_FRAME_PROT_DISABLED;
3271 			break;
3272 		}
3273 	}
3274 	cmd->protection_mode = cpu_to_le16(prot_mode);
3275 
3276 	legacy_rate_mask_to_array(cmd->supp_rates, legacy_rate_mask);
3277 
3278 	rc = mwl8k_post_cmd(hw, &cmd->header);
3279 	kfree(cmd);
3280 
3281 	return rc;
3282 }
3283 
3284 /*
3285  * CMD_SET_RATE.
3286  */
3287 struct mwl8k_cmd_set_rate {
3288 	struct mwl8k_cmd_pkt_hdr header;
3289 	__u8	legacy_rates[14];
3290 
3291 	/* Bitmap for supported MCS codes.  */
3292 	__u8	mcs_set[16];
3293 	__u8	reserved[16];
3294 } __packed;
3295 
3296 static int
3297 mwl8k_cmd_set_rate(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3298 		   u32 legacy_rate_mask, u8 *mcs_rates)
3299 {
3300 	struct mwl8k_cmd_set_rate *cmd;
3301 	int rc;
3302 
3303 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3304 	if (cmd == NULL)
3305 		return -ENOMEM;
3306 
3307 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATE);
3308 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3309 	legacy_rate_mask_to_array(cmd->legacy_rates, legacy_rate_mask);
3310 	memcpy(cmd->mcs_set, mcs_rates, 16);
3311 
3312 	rc = mwl8k_post_cmd(hw, &cmd->header);
3313 	kfree(cmd);
3314 
3315 	return rc;
3316 }
3317 
3318 /*
3319  * CMD_FINALIZE_JOIN.
3320  */
3321 #define MWL8K_FJ_BEACON_MAXLEN	128
3322 
3323 struct mwl8k_cmd_finalize_join {
3324 	struct mwl8k_cmd_pkt_hdr header;
3325 	__le32 sleep_interval;	/* Number of beacon periods to sleep */
3326 	__u8 beacon_data[MWL8K_FJ_BEACON_MAXLEN];
3327 } __packed;
3328 
3329 static int mwl8k_cmd_finalize_join(struct ieee80211_hw *hw, void *frame,
3330 				   int framelen, int dtim)
3331 {
3332 	struct mwl8k_cmd_finalize_join *cmd;
3333 	struct ieee80211_mgmt *payload = frame;
3334 	int payload_len;
3335 	int rc;
3336 
3337 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3338 	if (cmd == NULL)
3339 		return -ENOMEM;
3340 
3341 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_FINALIZE_JOIN);
3342 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3343 	cmd->sleep_interval = cpu_to_le32(dtim ? dtim : 1);
3344 
3345 	payload_len = framelen - ieee80211_hdrlen(payload->frame_control);
3346 	if (payload_len < 0)
3347 		payload_len = 0;
3348 	else if (payload_len > MWL8K_FJ_BEACON_MAXLEN)
3349 		payload_len = MWL8K_FJ_BEACON_MAXLEN;
3350 
3351 	memcpy(cmd->beacon_data, &payload->u.beacon, payload_len);
3352 
3353 	rc = mwl8k_post_cmd(hw, &cmd->header);
3354 	kfree(cmd);
3355 
3356 	return rc;
3357 }
3358 
3359 /*
3360  * CMD_SET_RTS_THRESHOLD.
3361  */
3362 struct mwl8k_cmd_set_rts_threshold {
3363 	struct mwl8k_cmd_pkt_hdr header;
3364 	__le16 action;
3365 	__le16 threshold;
3366 } __packed;
3367 
3368 static int
3369 mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int rts_thresh)
3370 {
3371 	struct mwl8k_cmd_set_rts_threshold *cmd;
3372 	int rc;
3373 
3374 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3375 	if (cmd == NULL)
3376 		return -ENOMEM;
3377 
3378 	cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3379 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3380 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3381 	cmd->threshold = cpu_to_le16(rts_thresh);
3382 
3383 	rc = mwl8k_post_cmd(hw, &cmd->header);
3384 	kfree(cmd);
3385 
3386 	return rc;
3387 }
3388 
3389 /*
3390  * CMD_SET_SLOT.
3391  */
3392 struct mwl8k_cmd_set_slot {
3393 	struct mwl8k_cmd_pkt_hdr header;
3394 	__le16 action;
3395 	__u8 short_slot;
3396 } __packed;
3397 
3398 static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
3399 {
3400 	struct mwl8k_cmd_set_slot *cmd;
3401 	int rc;
3402 
3403 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3404 	if (cmd == NULL)
3405 		return -ENOMEM;
3406 
3407 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3408 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3409 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3410 	cmd->short_slot = short_slot_time;
3411 
3412 	rc = mwl8k_post_cmd(hw, &cmd->header);
3413 	kfree(cmd);
3414 
3415 	return rc;
3416 }
3417 
3418 /*
3419  * CMD_SET_EDCA_PARAMS.
3420  */
3421 struct mwl8k_cmd_set_edca_params {
3422 	struct mwl8k_cmd_pkt_hdr header;
3423 
3424 	/* See MWL8K_SET_EDCA_XXX below */
3425 	__le16 action;
3426 
3427 	/* TX opportunity in units of 32 us */
3428 	__le16 txop;
3429 
3430 	union {
3431 		struct {
3432 			/* Log exponent of max contention period: 0...15 */
3433 			__le32 log_cw_max;
3434 
3435 			/* Log exponent of min contention period: 0...15 */
3436 			__le32 log_cw_min;
3437 
3438 			/* Adaptive interframe spacing in units of 32us */
3439 			__u8 aifs;
3440 
3441 			/* TX queue to configure */
3442 			__u8 txq;
3443 		} ap;
3444 		struct {
3445 			/* Log exponent of max contention period: 0...15 */
3446 			__u8 log_cw_max;
3447 
3448 			/* Log exponent of min contention period: 0...15 */
3449 			__u8 log_cw_min;
3450 
3451 			/* Adaptive interframe spacing in units of 32us */
3452 			__u8 aifs;
3453 
3454 			/* TX queue to configure */
3455 			__u8 txq;
3456 		} sta;
3457 	};
3458 } __packed;
3459 
3460 #define MWL8K_SET_EDCA_CW	0x01
3461 #define MWL8K_SET_EDCA_TXOP	0x02
3462 #define MWL8K_SET_EDCA_AIFS	0x04
3463 
3464 #define MWL8K_SET_EDCA_ALL	(MWL8K_SET_EDCA_CW | \
3465 				 MWL8K_SET_EDCA_TXOP | \
3466 				 MWL8K_SET_EDCA_AIFS)
3467 
3468 static int
3469 mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3470 			  __u16 cw_min, __u16 cw_max,
3471 			  __u8 aifs, __u16 txop)
3472 {
3473 	struct mwl8k_priv *priv = hw->priv;
3474 	struct mwl8k_cmd_set_edca_params *cmd;
3475 	int rc;
3476 
3477 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3478 	if (cmd == NULL)
3479 		return -ENOMEM;
3480 
3481 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3482 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3483 	cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3484 	cmd->txop = cpu_to_le16(txop);
3485 	if (priv->ap_fw) {
3486 		cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3487 		cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3488 		cmd->ap.aifs = aifs;
3489 		cmd->ap.txq = qnum;
3490 	} else {
3491 		cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3492 		cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3493 		cmd->sta.aifs = aifs;
3494 		cmd->sta.txq = qnum;
3495 	}
3496 
3497 	rc = mwl8k_post_cmd(hw, &cmd->header);
3498 	kfree(cmd);
3499 
3500 	return rc;
3501 }
3502 
3503 /*
3504  * CMD_SET_WMM_MODE.
3505  */
3506 struct mwl8k_cmd_set_wmm_mode {
3507 	struct mwl8k_cmd_pkt_hdr header;
3508 	__le16 action;
3509 } __packed;
3510 
3511 static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
3512 {
3513 	struct mwl8k_priv *priv = hw->priv;
3514 	struct mwl8k_cmd_set_wmm_mode *cmd;
3515 	int rc;
3516 
3517 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3518 	if (cmd == NULL)
3519 		return -ENOMEM;
3520 
3521 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
3522 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3523 	cmd->action = cpu_to_le16(!!enable);
3524 
3525 	rc = mwl8k_post_cmd(hw, &cmd->header);
3526 	kfree(cmd);
3527 
3528 	if (!rc)
3529 		priv->wmm_enabled = enable;
3530 
3531 	return rc;
3532 }
3533 
3534 /*
3535  * CMD_MIMO_CONFIG.
3536  */
3537 struct mwl8k_cmd_mimo_config {
3538 	struct mwl8k_cmd_pkt_hdr header;
3539 	__le32 action;
3540 	__u8 rx_antenna_map;
3541 	__u8 tx_antenna_map;
3542 } __packed;
3543 
3544 static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
3545 {
3546 	struct mwl8k_cmd_mimo_config *cmd;
3547 	int rc;
3548 
3549 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3550 	if (cmd == NULL)
3551 		return -ENOMEM;
3552 
3553 	cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
3554 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3555 	cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3556 	cmd->rx_antenna_map = rx;
3557 	cmd->tx_antenna_map = tx;
3558 
3559 	rc = mwl8k_post_cmd(hw, &cmd->header);
3560 	kfree(cmd);
3561 
3562 	return rc;
3563 }
3564 
3565 /*
3566  * CMD_USE_FIXED_RATE (STA version).
3567  */
3568 struct mwl8k_cmd_use_fixed_rate_sta {
3569 	struct mwl8k_cmd_pkt_hdr header;
3570 	__le32 action;
3571 	__le32 allow_rate_drop;
3572 	__le32 num_rates;
3573 	struct {
3574 		__le32 is_ht_rate;
3575 		__le32 enable_retry;
3576 		__le32 rate;
3577 		__le32 retry_count;
3578 	} rate_entry[8];
3579 	__le32 rate_type;
3580 	__le32 reserved1;
3581 	__le32 reserved2;
3582 } __packed;
3583 
3584 #define MWL8K_USE_AUTO_RATE	0x0002
3585 #define MWL8K_UCAST_RATE	0
3586 
3587 static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
3588 {
3589 	struct mwl8k_cmd_use_fixed_rate_sta *cmd;
3590 	int rc;
3591 
3592 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3593 	if (cmd == NULL)
3594 		return -ENOMEM;
3595 
3596 	cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3597 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3598 	cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3599 	cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
3600 
3601 	rc = mwl8k_post_cmd(hw, &cmd->header);
3602 	kfree(cmd);
3603 
3604 	return rc;
3605 }
3606 
3607 /*
3608  * CMD_USE_FIXED_RATE (AP version).
3609  */
3610 struct mwl8k_cmd_use_fixed_rate_ap {
3611 	struct mwl8k_cmd_pkt_hdr header;
3612 	__le32 action;
3613 	__le32 allow_rate_drop;
3614 	__le32 num_rates;
3615 	struct mwl8k_rate_entry_ap {
3616 		__le32 is_ht_rate;
3617 		__le32 enable_retry;
3618 		__le32 rate;
3619 		__le32 retry_count;
3620 	} rate_entry[4];
3621 	u8 multicast_rate;
3622 	u8 multicast_rate_type;
3623 	u8 management_rate;
3624 } __packed;
3625 
3626 static int
3627 mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3628 {
3629 	struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3630 	int rc;
3631 
3632 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3633 	if (cmd == NULL)
3634 		return -ENOMEM;
3635 
3636 	cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3637 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3638 	cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3639 	cmd->multicast_rate = mcast;
3640 	cmd->management_rate = mgmt;
3641 
3642 	rc = mwl8k_post_cmd(hw, &cmd->header);
3643 	kfree(cmd);
3644 
3645 	return rc;
3646 }
3647 
3648 /*
3649  * CMD_ENABLE_SNIFFER.
3650  */
3651 struct mwl8k_cmd_enable_sniffer {
3652 	struct mwl8k_cmd_pkt_hdr header;
3653 	__le32 action;
3654 } __packed;
3655 
3656 static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3657 {
3658 	struct mwl8k_cmd_enable_sniffer *cmd;
3659 	int rc;
3660 
3661 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3662 	if (cmd == NULL)
3663 		return -ENOMEM;
3664 
3665 	cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3666 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3667 	cmd->action = cpu_to_le32(!!enable);
3668 
3669 	rc = mwl8k_post_cmd(hw, &cmd->header);
3670 	kfree(cmd);
3671 
3672 	return rc;
3673 }
3674 
3675 struct mwl8k_cmd_update_mac_addr {
3676 	struct mwl8k_cmd_pkt_hdr header;
3677 	union {
3678 		struct {
3679 			__le16 mac_type;
3680 			__u8 mac_addr[ETH_ALEN];
3681 		} mbss;
3682 		__u8 mac_addr[ETH_ALEN];
3683 	};
3684 } __packed;
3685 
3686 #define MWL8K_MAC_TYPE_PRIMARY_CLIENT		0
3687 #define MWL8K_MAC_TYPE_SECONDARY_CLIENT		1
3688 #define MWL8K_MAC_TYPE_PRIMARY_AP		2
3689 #define MWL8K_MAC_TYPE_SECONDARY_AP		3
3690 
3691 static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3692 				  struct ieee80211_vif *vif, u8 *mac, bool set)
3693 {
3694 	struct mwl8k_priv *priv = hw->priv;
3695 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3696 	struct mwl8k_cmd_update_mac_addr *cmd;
3697 	int mac_type;
3698 	int rc;
3699 
3700 	mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3701 	if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3702 		if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3703 			if (priv->ap_fw)
3704 				mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3705 			else
3706 				mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3707 		else
3708 			mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3709 	} else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3710 		if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3711 			mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3712 		else
3713 			mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3714 	}
3715 
3716 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3717 	if (cmd == NULL)
3718 		return -ENOMEM;
3719 
3720 	if (set)
3721 		cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3722 	else
3723 		cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3724 
3725 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3726 	if (priv->ap_fw) {
3727 		cmd->mbss.mac_type = cpu_to_le16(mac_type);
3728 		memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3729 	} else {
3730 		memcpy(cmd->mac_addr, mac, ETH_ALEN);
3731 	}
3732 
3733 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3734 	kfree(cmd);
3735 
3736 	return rc;
3737 }
3738 
3739 /*
3740  * MWL8K_CMD_SET_MAC_ADDR.
3741  */
3742 static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3743 				  struct ieee80211_vif *vif, u8 *mac)
3744 {
3745 	return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3746 }
3747 
3748 /*
3749  * MWL8K_CMD_DEL_MAC_ADDR.
3750  */
3751 static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3752 				  struct ieee80211_vif *vif, u8 *mac)
3753 {
3754 	return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3755 }
3756 
3757 /*
3758  * CMD_SET_RATEADAPT_MODE.
3759  */
3760 struct mwl8k_cmd_set_rate_adapt_mode {
3761 	struct mwl8k_cmd_pkt_hdr header;
3762 	__le16 action;
3763 	__le16 mode;
3764 } __packed;
3765 
3766 static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3767 {
3768 	struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3769 	int rc;
3770 
3771 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3772 	if (cmd == NULL)
3773 		return -ENOMEM;
3774 
3775 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3776 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3777 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3778 	cmd->mode = cpu_to_le16(mode);
3779 
3780 	rc = mwl8k_post_cmd(hw, &cmd->header);
3781 	kfree(cmd);
3782 
3783 	return rc;
3784 }
3785 
3786 /*
3787  * CMD_GET_WATCHDOG_BITMAP.
3788  */
3789 struct mwl8k_cmd_get_watchdog_bitmap {
3790 	struct mwl8k_cmd_pkt_hdr header;
3791 	u8	bitmap;
3792 } __packed;
3793 
3794 static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3795 {
3796 	struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3797 	int rc;
3798 
3799 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3800 	if (cmd == NULL)
3801 		return -ENOMEM;
3802 
3803 	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3804 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3805 
3806 	rc = mwl8k_post_cmd(hw, &cmd->header);
3807 	if (!rc)
3808 		*bitmap = cmd->bitmap;
3809 
3810 	kfree(cmd);
3811 
3812 	return rc;
3813 }
3814 
3815 #define MWL8K_WMM_QUEUE_NUMBER	3
3816 
3817 static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3818 			     u8 idx);
3819 
3820 static void mwl8k_watchdog_ba_events(struct work_struct *work)
3821 {
3822 	int rc;
3823 	u8 bitmap = 0, stream_index;
3824 	struct mwl8k_ampdu_stream *streams;
3825 	struct mwl8k_priv *priv =
3826 		container_of(work, struct mwl8k_priv, watchdog_ba_handle);
3827 	struct ieee80211_hw *hw = priv->hw;
3828 	int i;
3829 	u32 status = 0;
3830 
3831 	mwl8k_fw_lock(hw);
3832 
3833 	rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3834 	if (rc)
3835 		goto done;
3836 
3837 	spin_lock(&priv->stream_lock);
3838 
3839 	/* the bitmap is the hw queue number.  Map it to the ampdu queue. */
3840 	for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
3841 		if (bitmap & (1 << i)) {
3842 			stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
3843 				       TOTAL_HW_TX_QUEUES;
3844 			streams = &priv->ampdu[stream_index];
3845 			if (streams->state == AMPDU_STREAM_ACTIVE) {
3846 				ieee80211_stop_tx_ba_session(streams->sta,
3847 							     streams->tid);
3848 				spin_unlock(&priv->stream_lock);
3849 				mwl8k_destroy_ba(hw, stream_index);
3850 				spin_lock(&priv->stream_lock);
3851 			}
3852 		}
3853 	}
3854 
3855 	spin_unlock(&priv->stream_lock);
3856 done:
3857 	atomic_dec(&priv->watchdog_event_pending);
3858 	status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3859 	iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
3860 		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3861 	mwl8k_fw_unlock(hw);
3862 	return;
3863 }
3864 
3865 
3866 /*
3867  * CMD_BSS_START.
3868  */
3869 struct mwl8k_cmd_bss_start {
3870 	struct mwl8k_cmd_pkt_hdr header;
3871 	__le32 enable;
3872 } __packed;
3873 
3874 static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3875 			       struct ieee80211_vif *vif, int enable)
3876 {
3877 	struct mwl8k_cmd_bss_start *cmd;
3878 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3879 	struct mwl8k_priv *priv = hw->priv;
3880 	int rc;
3881 
3882 	if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
3883 		return 0;
3884 
3885 	if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
3886 		return 0;
3887 
3888 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3889 	if (cmd == NULL)
3890 		return -ENOMEM;
3891 
3892 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3893 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3894 	cmd->enable = cpu_to_le32(enable);
3895 
3896 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3897 	kfree(cmd);
3898 
3899 	if (!rc) {
3900 		if (enable)
3901 			priv->running_bsses |= (1 << mwl8k_vif->macid);
3902 		else
3903 			priv->running_bsses &= ~(1 << mwl8k_vif->macid);
3904 	}
3905 	return rc;
3906 }
3907 
3908 static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
3909 {
3910 	struct mwl8k_priv *priv = hw->priv;
3911 	struct mwl8k_vif *mwl8k_vif, *tmp_vif;
3912 	struct ieee80211_vif *vif;
3913 
3914 	list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
3915 		vif = mwl8k_vif->vif;
3916 
3917 		if (!(bitmap & (1 << mwl8k_vif->macid)))
3918 			continue;
3919 
3920 		if (vif->type == NL80211_IFTYPE_AP)
3921 			mwl8k_cmd_bss_start(hw, vif, enable);
3922 	}
3923 }
3924 /*
3925  * CMD_BASTREAM.
3926  */
3927 
3928 /*
3929  * UPSTREAM is tx direction
3930  */
3931 #define BASTREAM_FLAG_DIRECTION_UPSTREAM	0x00
3932 #define BASTREAM_FLAG_IMMEDIATE_TYPE		0x01
3933 
3934 enum ba_stream_action_type {
3935 	MWL8K_BA_CREATE,
3936 	MWL8K_BA_UPDATE,
3937 	MWL8K_BA_DESTROY,
3938 	MWL8K_BA_FLUSH,
3939 	MWL8K_BA_CHECK,
3940 };
3941 
3942 
3943 struct mwl8k_create_ba_stream {
3944 	__le32	flags;
3945 	__le32	idle_thrs;
3946 	__le32	bar_thrs;
3947 	__le32	window_size;
3948 	u8	peer_mac_addr[6];
3949 	u8	dialog_token;
3950 	u8	tid;
3951 	u8	queue_id;
3952 	u8	param_info;
3953 	__le32	ba_context;
3954 	u8	reset_seq_no_flag;
3955 	__le16	curr_seq_no;
3956 	u8	sta_src_mac_addr[6];
3957 } __packed;
3958 
3959 struct mwl8k_destroy_ba_stream {
3960 	__le32	flags;
3961 	__le32	ba_context;
3962 } __packed;
3963 
3964 struct mwl8k_cmd_bastream {
3965 	struct mwl8k_cmd_pkt_hdr	header;
3966 	__le32	action;
3967 	union {
3968 		struct mwl8k_create_ba_stream	create_params;
3969 		struct mwl8k_destroy_ba_stream	destroy_params;
3970 	};
3971 } __packed;
3972 
3973 static int
3974 mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3975 	       struct ieee80211_vif *vif)
3976 {
3977 	struct mwl8k_cmd_bastream *cmd;
3978 	int rc;
3979 
3980 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3981 	if (cmd == NULL)
3982 		return -ENOMEM;
3983 
3984 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3985 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3986 
3987 	cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3988 
3989 	cmd->create_params.queue_id = stream->idx;
3990 	memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3991 	       ETH_ALEN);
3992 	cmd->create_params.tid = stream->tid;
3993 
3994 	cmd->create_params.flags =
3995 		cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
3996 		cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
3997 
3998 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3999 
4000 	kfree(cmd);
4001 
4002 	return rc;
4003 }
4004 
4005 static int
4006 mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
4007 		u8 buf_size, struct ieee80211_vif *vif)
4008 {
4009 	struct mwl8k_cmd_bastream *cmd;
4010 	int rc;
4011 
4012 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4013 	if (cmd == NULL)
4014 		return -ENOMEM;
4015 
4016 
4017 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
4018 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4019 
4020 	cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
4021 
4022 	cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
4023 	cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
4024 	cmd->create_params.queue_id = stream->idx;
4025 
4026 	memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
4027 	cmd->create_params.tid = stream->tid;
4028 	cmd->create_params.curr_seq_no = cpu_to_le16(0);
4029 	cmd->create_params.reset_seq_no_flag = 1;
4030 
4031 	cmd->create_params.param_info =
4032 		(stream->sta->deflink.ht_cap.ampdu_factor &
4033 		 IEEE80211_HT_AMPDU_PARM_FACTOR) |
4034 		((stream->sta->deflink.ht_cap.ampdu_density << 2) &
4035 		 IEEE80211_HT_AMPDU_PARM_DENSITY);
4036 
4037 	cmd->create_params.flags =
4038 		cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
4039 					BASTREAM_FLAG_DIRECTION_UPSTREAM);
4040 
4041 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4042 
4043 	wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
4044 		stream->sta->addr, stream->tid);
4045 	kfree(cmd);
4046 
4047 	return rc;
4048 }
4049 
4050 static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
4051 			     u8 idx)
4052 {
4053 	struct mwl8k_cmd_bastream *cmd;
4054 
4055 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4056 	if (cmd == NULL)
4057 		return;
4058 
4059 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
4060 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4061 	cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
4062 
4063 	cmd->destroy_params.ba_context = cpu_to_le32(idx);
4064 	mwl8k_post_cmd(hw, &cmd->header);
4065 
4066 	wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
4067 
4068 	kfree(cmd);
4069 }
4070 
4071 /*
4072  * CMD_SET_NEW_STN.
4073  */
4074 struct mwl8k_cmd_set_new_stn {
4075 	struct mwl8k_cmd_pkt_hdr header;
4076 	__le16 aid;
4077 	__u8 mac_addr[6];
4078 	__le16 stn_id;
4079 	__le16 action;
4080 	__le16 rsvd;
4081 	__le32 legacy_rates;
4082 	__u8 ht_rates[4];
4083 	__le16 cap_info;
4084 	__le16 ht_capabilities_info;
4085 	__u8 mac_ht_param_info;
4086 	__u8 rev;
4087 	__u8 control_channel;
4088 	__u8 add_channel;
4089 	__le16 op_mode;
4090 	__le16 stbc;
4091 	__u8 add_qos_info;
4092 	__u8 is_qos_sta;
4093 	__le32 fw_sta_ptr;
4094 } __packed;
4095 
4096 #define MWL8K_STA_ACTION_ADD		0
4097 #define MWL8K_STA_ACTION_REMOVE		2
4098 
4099 static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
4100 				     struct ieee80211_vif *vif,
4101 				     struct ieee80211_sta *sta)
4102 {
4103 	struct mwl8k_cmd_set_new_stn *cmd;
4104 	u32 rates;
4105 	int rc;
4106 
4107 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4108 	if (cmd == NULL)
4109 		return -ENOMEM;
4110 
4111 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4112 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4113 	cmd->aid = cpu_to_le16(sta->aid);
4114 	memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
4115 	cmd->stn_id = cpu_to_le16(sta->aid);
4116 	cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
4117 	if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
4118 		rates = sta->deflink.supp_rates[NL80211_BAND_2GHZ];
4119 	else
4120 		rates = sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
4121 	cmd->legacy_rates = cpu_to_le32(rates);
4122 	if (sta->deflink.ht_cap.ht_supported) {
4123 		cmd->ht_rates[0] = sta->deflink.ht_cap.mcs.rx_mask[0];
4124 		cmd->ht_rates[1] = sta->deflink.ht_cap.mcs.rx_mask[1];
4125 		cmd->ht_rates[2] = sta->deflink.ht_cap.mcs.rx_mask[2];
4126 		cmd->ht_rates[3] = sta->deflink.ht_cap.mcs.rx_mask[3];
4127 		cmd->ht_capabilities_info = cpu_to_le16(sta->deflink.ht_cap.cap);
4128 		cmd->mac_ht_param_info = (sta->deflink.ht_cap.ampdu_factor & 3) |
4129 			((sta->deflink.ht_cap.ampdu_density & 7) << 2);
4130 		cmd->is_qos_sta = 1;
4131 	}
4132 
4133 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4134 	kfree(cmd);
4135 
4136 	return rc;
4137 }
4138 
4139 static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
4140 					  struct ieee80211_vif *vif)
4141 {
4142 	struct mwl8k_cmd_set_new_stn *cmd;
4143 	int rc;
4144 
4145 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4146 	if (cmd == NULL)
4147 		return -ENOMEM;
4148 
4149 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4150 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4151 	memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
4152 
4153 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4154 	kfree(cmd);
4155 
4156 	return rc;
4157 }
4158 
4159 static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
4160 				     struct ieee80211_vif *vif, u8 *addr)
4161 {
4162 	struct mwl8k_cmd_set_new_stn *cmd;
4163 	struct mwl8k_priv *priv = hw->priv;
4164 	int rc, i;
4165 	u8 idx;
4166 
4167 	spin_lock(&priv->stream_lock);
4168 	/* Destroy any active ampdu streams for this sta */
4169 	for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
4170 		struct mwl8k_ampdu_stream *s;
4171 		s = &priv->ampdu[i];
4172 		if (s->state != AMPDU_NO_STREAM) {
4173 			if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
4174 				if (s->state == AMPDU_STREAM_ACTIVE) {
4175 					idx = s->idx;
4176 					spin_unlock(&priv->stream_lock);
4177 					mwl8k_destroy_ba(hw, idx);
4178 					spin_lock(&priv->stream_lock);
4179 				} else if (s->state == AMPDU_STREAM_NEW) {
4180 					mwl8k_remove_stream(hw, s);
4181 				}
4182 			}
4183 		}
4184 	}
4185 
4186 	spin_unlock(&priv->stream_lock);
4187 
4188 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4189 	if (cmd == NULL)
4190 		return -ENOMEM;
4191 
4192 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4193 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4194 	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4195 	cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
4196 
4197 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4198 	kfree(cmd);
4199 
4200 	return rc;
4201 }
4202 
4203 /*
4204  * CMD_UPDATE_ENCRYPTION.
4205  */
4206 
4207 #define MAX_ENCR_KEY_LENGTH	16
4208 #define MIC_KEY_LENGTH		8
4209 
4210 struct mwl8k_cmd_update_encryption {
4211 	struct mwl8k_cmd_pkt_hdr header;
4212 
4213 	__le32 action;
4214 	__le32 reserved;
4215 	__u8 mac_addr[6];
4216 	__u8 encr_type;
4217 
4218 } __packed;
4219 
4220 struct mwl8k_cmd_set_key {
4221 	struct mwl8k_cmd_pkt_hdr header;
4222 
4223 	__le32 action;
4224 	__le32 reserved;
4225 	__le16 length;
4226 	__le16 key_type_id;
4227 	__le32 key_info;
4228 	__le32 key_id;
4229 	__le16 key_len;
4230 	struct {
4231 		__u8 key_material[MAX_ENCR_KEY_LENGTH];
4232 		__u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
4233 		__u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
4234 	} tkip;
4235 	__le16 tkip_rsc_low;
4236 	__le32 tkip_rsc_high;
4237 	__le16 tkip_tsc_low;
4238 	__le32 tkip_tsc_high;
4239 	__u8 mac_addr[6];
4240 } __packed;
4241 
4242 enum {
4243 	MWL8K_ENCR_ENABLE,
4244 	MWL8K_ENCR_SET_KEY,
4245 	MWL8K_ENCR_REMOVE_KEY,
4246 	MWL8K_ENCR_SET_GROUP_KEY,
4247 };
4248 
4249 #define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP	0
4250 #define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE	1
4251 #define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP	4
4252 #define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED	7
4253 #define MWL8K_UPDATE_ENCRYPTION_TYPE_AES	8
4254 
4255 enum {
4256 	MWL8K_ALG_WEP,
4257 	MWL8K_ALG_TKIP,
4258 	MWL8K_ALG_CCMP,
4259 };
4260 
4261 #define MWL8K_KEY_FLAG_TXGROUPKEY	0x00000004
4262 #define MWL8K_KEY_FLAG_PAIRWISE		0x00000008
4263 #define MWL8K_KEY_FLAG_TSC_VALID	0x00000040
4264 #define MWL8K_KEY_FLAG_WEP_TXKEY	0x01000000
4265 #define MWL8K_KEY_FLAG_MICKEY_VALID	0x02000000
4266 
4267 static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
4268 					      struct ieee80211_vif *vif,
4269 					      u8 *addr,
4270 					      u8 encr_type)
4271 {
4272 	struct mwl8k_cmd_update_encryption *cmd;
4273 	int rc;
4274 
4275 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4276 	if (cmd == NULL)
4277 		return -ENOMEM;
4278 
4279 	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4280 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4281 	cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
4282 	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4283 	cmd->encr_type = encr_type;
4284 
4285 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4286 	kfree(cmd);
4287 
4288 	return rc;
4289 }
4290 
4291 static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4292 						u8 *addr,
4293 						struct ieee80211_key_conf *key)
4294 {
4295 	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4296 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4297 	cmd->length = cpu_to_le16(sizeof(*cmd) -
4298 				offsetof(struct mwl8k_cmd_set_key, length));
4299 	cmd->key_id = cpu_to_le32(key->keyidx);
4300 	cmd->key_len = cpu_to_le16(key->keylen);
4301 	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4302 
4303 	switch (key->cipher) {
4304 	case WLAN_CIPHER_SUITE_WEP40:
4305 	case WLAN_CIPHER_SUITE_WEP104:
4306 		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4307 		if (key->keyidx == 0)
4308 			cmd->key_info =	cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4309 
4310 		break;
4311 	case WLAN_CIPHER_SUITE_TKIP:
4312 		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4313 		cmd->key_info =	(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4314 			? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4315 			: cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4316 		cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4317 						| MWL8K_KEY_FLAG_TSC_VALID);
4318 		break;
4319 	case WLAN_CIPHER_SUITE_CCMP:
4320 		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4321 		cmd->key_info =	(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4322 			? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4323 			: cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4324 		break;
4325 	default:
4326 		return -ENOTSUPP;
4327 	}
4328 
4329 	return 0;
4330 }
4331 
4332 static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4333 						struct ieee80211_vif *vif,
4334 						u8 *addr,
4335 						struct ieee80211_key_conf *key)
4336 {
4337 	struct mwl8k_cmd_set_key *cmd;
4338 	int rc;
4339 	int keymlen;
4340 	u32 action;
4341 	u8 idx;
4342 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4343 
4344 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4345 	if (cmd == NULL)
4346 		return -ENOMEM;
4347 
4348 	rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4349 	if (rc < 0)
4350 		goto done;
4351 
4352 	idx = key->keyidx;
4353 
4354 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4355 		action = MWL8K_ENCR_SET_KEY;
4356 	else
4357 		action = MWL8K_ENCR_SET_GROUP_KEY;
4358 
4359 	switch (key->cipher) {
4360 	case WLAN_CIPHER_SUITE_WEP40:
4361 	case WLAN_CIPHER_SUITE_WEP104:
4362 		if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4363 			memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4364 						sizeof(*key) + key->keylen);
4365 			mwl8k_vif->wep_key_conf[idx].enabled = 1;
4366 		}
4367 
4368 		keymlen = key->keylen;
4369 		action = MWL8K_ENCR_SET_KEY;
4370 		break;
4371 	case WLAN_CIPHER_SUITE_TKIP:
4372 		keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4373 		break;
4374 	case WLAN_CIPHER_SUITE_CCMP:
4375 		keymlen = key->keylen;
4376 		break;
4377 	default:
4378 		rc = -ENOTSUPP;
4379 		goto done;
4380 	}
4381 
4382 	memcpy(&cmd->tkip, key->key, keymlen);
4383 	cmd->action = cpu_to_le32(action);
4384 
4385 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4386 done:
4387 	kfree(cmd);
4388 
4389 	return rc;
4390 }
4391 
4392 static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4393 						struct ieee80211_vif *vif,
4394 						u8 *addr,
4395 						struct ieee80211_key_conf *key)
4396 {
4397 	struct mwl8k_cmd_set_key *cmd;
4398 	int rc;
4399 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4400 
4401 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4402 	if (cmd == NULL)
4403 		return -ENOMEM;
4404 
4405 	rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4406 	if (rc < 0)
4407 		goto done;
4408 
4409 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4410 			key->cipher == WLAN_CIPHER_SUITE_WEP104)
4411 		mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4412 
4413 	cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4414 
4415 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4416 done:
4417 	kfree(cmd);
4418 
4419 	return rc;
4420 }
4421 
4422 static int mwl8k_set_key(struct ieee80211_hw *hw,
4423 			 enum set_key_cmd cmd_param,
4424 			 struct ieee80211_vif *vif,
4425 			 struct ieee80211_sta *sta,
4426 			 struct ieee80211_key_conf *key)
4427 {
4428 	int rc = 0;
4429 	u8 encr_type;
4430 	u8 *addr;
4431 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4432 	struct mwl8k_priv *priv = hw->priv;
4433 
4434 	if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
4435 		return -EOPNOTSUPP;
4436 
4437 	if (sta == NULL)
4438 		addr = vif->addr;
4439 	else
4440 		addr = sta->addr;
4441 
4442 	if (cmd_param == SET_KEY) {
4443 		rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4444 		if (rc)
4445 			goto out;
4446 
4447 		if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4448 				|| (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4449 			encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4450 		else
4451 			encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4452 
4453 		rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4454 								encr_type);
4455 		if (rc)
4456 			goto out;
4457 
4458 		mwl8k_vif->is_hw_crypto_enabled = true;
4459 
4460 	} else {
4461 		rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4462 
4463 		if (rc)
4464 			goto out;
4465 	}
4466 out:
4467 	return rc;
4468 }
4469 
4470 /*
4471  * CMD_UPDATE_STADB.
4472  */
4473 struct ewc_ht_info {
4474 	__le16	control1;
4475 	__le16	control2;
4476 	__le16	control3;
4477 } __packed;
4478 
4479 struct peer_capability_info {
4480 	/* Peer type - AP vs. STA.  */
4481 	__u8	peer_type;
4482 
4483 	/* Basic 802.11 capabilities from assoc resp.  */
4484 	__le16	basic_caps;
4485 
4486 	/* Set if peer supports 802.11n high throughput (HT).  */
4487 	__u8	ht_support;
4488 
4489 	/* Valid if HT is supported.  */
4490 	__le16	ht_caps;
4491 	__u8	extended_ht_caps;
4492 	struct ewc_ht_info	ewc_info;
4493 
4494 	/* Legacy rate table. Intersection of our rates and peer rates.  */
4495 	__u8	legacy_rates[12];
4496 
4497 	/* HT rate table. Intersection of our rates and peer rates.  */
4498 	__u8	ht_rates[16];
4499 	__u8	pad[16];
4500 
4501 	/* If set, interoperability mode, no proprietary extensions.  */
4502 	__u8	interop;
4503 	__u8	pad2;
4504 	__u8	station_id;
4505 	__le16	amsdu_enabled;
4506 } __packed;
4507 
4508 struct mwl8k_cmd_update_stadb {
4509 	struct mwl8k_cmd_pkt_hdr header;
4510 
4511 	/* See STADB_ACTION_TYPE */
4512 	__le32	action;
4513 
4514 	/* Peer MAC address */
4515 	__u8	peer_addr[ETH_ALEN];
4516 
4517 	__le32	reserved;
4518 
4519 	/* Peer info - valid during add/update.  */
4520 	struct peer_capability_info	peer_info;
4521 } __packed;
4522 
4523 #define MWL8K_STA_DB_MODIFY_ENTRY	1
4524 #define MWL8K_STA_DB_DEL_ENTRY		2
4525 
4526 /* Peer Entry flags - used to define the type of the peer node */
4527 #define MWL8K_PEER_TYPE_ACCESSPOINT	2
4528 
4529 static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
4530 				      struct ieee80211_vif *vif,
4531 				      struct ieee80211_sta *sta)
4532 {
4533 	struct mwl8k_cmd_update_stadb *cmd;
4534 	struct peer_capability_info *p;
4535 	u32 rates;
4536 	int rc;
4537 
4538 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4539 	if (cmd == NULL)
4540 		return -ENOMEM;
4541 
4542 	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4543 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4544 	cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
4545 	memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
4546 
4547 	p = &cmd->peer_info;
4548 	p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4549 	p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
4550 	p->ht_support = sta->deflink.ht_cap.ht_supported;
4551 	p->ht_caps = cpu_to_le16(sta->deflink.ht_cap.cap);
4552 	p->extended_ht_caps = (sta->deflink.ht_cap.ampdu_factor & 3) |
4553 		((sta->deflink.ht_cap.ampdu_density & 7) << 2);
4554 	if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
4555 		rates = sta->deflink.supp_rates[NL80211_BAND_2GHZ];
4556 	else
4557 		rates = sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
4558 	legacy_rate_mask_to_array(p->legacy_rates, rates);
4559 	memcpy(p->ht_rates, &sta->deflink.ht_cap.mcs, 16);
4560 	p->interop = 1;
4561 	p->amsdu_enabled = 0;
4562 
4563 	rc = mwl8k_post_cmd(hw, &cmd->header);
4564 	if (!rc)
4565 		rc = p->station_id;
4566 	kfree(cmd);
4567 
4568 	return rc;
4569 }
4570 
4571 static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4572 				      struct ieee80211_vif *vif, u8 *addr)
4573 {
4574 	struct mwl8k_cmd_update_stadb *cmd;
4575 	int rc;
4576 
4577 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4578 	if (cmd == NULL)
4579 		return -ENOMEM;
4580 
4581 	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4582 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4583 	cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4584 	memcpy(cmd->peer_addr, addr, ETH_ALEN);
4585 
4586 	rc = mwl8k_post_cmd(hw, &cmd->header);
4587 	kfree(cmd);
4588 
4589 	return rc;
4590 }
4591 
4592 
4593 /*
4594  * Interrupt handling.
4595  */
4596 static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4597 {
4598 	struct ieee80211_hw *hw = dev_id;
4599 	struct mwl8k_priv *priv = hw->priv;
4600 	u32 status;
4601 
4602 	status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4603 	if (!status)
4604 		return IRQ_NONE;
4605 
4606 	if (status & MWL8K_A2H_INT_TX_DONE) {
4607 		status &= ~MWL8K_A2H_INT_TX_DONE;
4608 		tasklet_schedule(&priv->poll_tx_task);
4609 	}
4610 
4611 	if (status & MWL8K_A2H_INT_RX_READY) {
4612 		status &= ~MWL8K_A2H_INT_RX_READY;
4613 		tasklet_schedule(&priv->poll_rx_task);
4614 	}
4615 
4616 	if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
4617 		iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
4618 			  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4619 
4620 		atomic_inc(&priv->watchdog_event_pending);
4621 		status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4622 		ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4623 	}
4624 
4625 	if (status)
4626 		iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4627 
4628 	if (status & MWL8K_A2H_INT_OPC_DONE) {
4629 		if (priv->hostcmd_wait != NULL)
4630 			complete(priv->hostcmd_wait);
4631 	}
4632 
4633 	if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
4634 		if (!mutex_is_locked(&priv->fw_mutex) &&
4635 		    priv->radio_on && priv->pending_tx_pkts)
4636 			mwl8k_tx_start(priv);
4637 	}
4638 
4639 	return IRQ_HANDLED;
4640 }
4641 
4642 static void mwl8k_tx_poll(struct tasklet_struct *t)
4643 {
4644 	struct mwl8k_priv *priv = from_tasklet(priv, t, poll_tx_task);
4645 	struct ieee80211_hw *hw = pci_get_drvdata(priv->pdev);
4646 	int limit;
4647 	int i;
4648 
4649 	limit = 32;
4650 
4651 	spin_lock(&priv->tx_lock);
4652 
4653 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
4654 		limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4655 
4656 	if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4657 		complete(priv->tx_wait);
4658 		priv->tx_wait = NULL;
4659 	}
4660 
4661 	spin_unlock(&priv->tx_lock);
4662 
4663 	if (limit) {
4664 		writel(~MWL8K_A2H_INT_TX_DONE,
4665 		       priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4666 	} else {
4667 		tasklet_schedule(&priv->poll_tx_task);
4668 	}
4669 }
4670 
4671 static void mwl8k_rx_poll(struct tasklet_struct *t)
4672 {
4673 	struct mwl8k_priv *priv = from_tasklet(priv, t, poll_rx_task);
4674 	struct ieee80211_hw *hw = pci_get_drvdata(priv->pdev);
4675 	int limit;
4676 
4677 	limit = 32;
4678 	limit -= rxq_process(hw, 0, limit);
4679 	limit -= rxq_refill(hw, 0, limit);
4680 
4681 	if (limit) {
4682 		writel(~MWL8K_A2H_INT_RX_READY,
4683 		       priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4684 	} else {
4685 		tasklet_schedule(&priv->poll_rx_task);
4686 	}
4687 }
4688 
4689 
4690 /*
4691  * Core driver operations.
4692  */
4693 static void mwl8k_tx(struct ieee80211_hw *hw,
4694 		     struct ieee80211_tx_control *control,
4695 		     struct sk_buff *skb)
4696 {
4697 	struct mwl8k_priv *priv = hw->priv;
4698 	int index = skb_get_queue_mapping(skb);
4699 
4700 	if (!priv->radio_on) {
4701 		wiphy_debug(hw->wiphy,
4702 			    "dropped TX frame since radio disabled\n");
4703 		dev_kfree_skb(skb);
4704 		return;
4705 	}
4706 
4707 	mwl8k_txq_xmit(hw, index, control->sta, skb);
4708 }
4709 
4710 static int mwl8k_start(struct ieee80211_hw *hw)
4711 {
4712 	struct mwl8k_priv *priv = hw->priv;
4713 	int rc;
4714 
4715 	rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4716 			 IRQF_SHARED, MWL8K_NAME, hw);
4717 	if (rc) {
4718 		priv->irq = -1;
4719 		wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4720 		return -EIO;
4721 	}
4722 	priv->irq = priv->pdev->irq;
4723 
4724 	/* Enable TX reclaim and RX tasklets.  */
4725 	tasklet_enable(&priv->poll_tx_task);
4726 	tasklet_enable(&priv->poll_rx_task);
4727 
4728 	/* Enable interrupts */
4729 	iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4730 	iowrite32(MWL8K_A2H_EVENTS,
4731 		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4732 
4733 	rc = mwl8k_fw_lock(hw);
4734 	if (!rc) {
4735 		rc = mwl8k_cmd_radio_enable(hw);
4736 
4737 		if (!priv->ap_fw) {
4738 			if (!rc)
4739 				rc = mwl8k_cmd_enable_sniffer(hw, 0);
4740 
4741 			if (!rc)
4742 				rc = mwl8k_cmd_set_pre_scan(hw);
4743 
4744 			if (!rc)
4745 				rc = mwl8k_cmd_set_post_scan(hw,
4746 						"\x00\x00\x00\x00\x00\x00");
4747 		}
4748 
4749 		if (!rc)
4750 			rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
4751 
4752 		if (!rc)
4753 			rc = mwl8k_cmd_set_wmm_mode(hw, 0);
4754 
4755 		mwl8k_fw_unlock(hw);
4756 	}
4757 
4758 	if (rc) {
4759 		iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4760 		free_irq(priv->pdev->irq, hw);
4761 		priv->irq = -1;
4762 		tasklet_disable(&priv->poll_tx_task);
4763 		tasklet_disable(&priv->poll_rx_task);
4764 	} else {
4765 		ieee80211_wake_queues(hw);
4766 	}
4767 
4768 	return rc;
4769 }
4770 
4771 static void mwl8k_stop(struct ieee80211_hw *hw)
4772 {
4773 	struct mwl8k_priv *priv = hw->priv;
4774 	int i;
4775 
4776 	if (!priv->hw_restart_in_progress)
4777 		mwl8k_cmd_radio_disable(hw);
4778 
4779 	ieee80211_stop_queues(hw);
4780 
4781 	/* Disable interrupts */
4782 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4783 	if (priv->irq != -1) {
4784 		free_irq(priv->pdev->irq, hw);
4785 		priv->irq = -1;
4786 	}
4787 
4788 	/* Stop finalize join worker */
4789 	cancel_work_sync(&priv->finalize_join_worker);
4790 	cancel_work_sync(&priv->watchdog_ba_handle);
4791 	if (priv->beacon_skb != NULL)
4792 		dev_kfree_skb(priv->beacon_skb);
4793 
4794 	/* Stop TX reclaim and RX tasklets.  */
4795 	tasklet_disable(&priv->poll_tx_task);
4796 	tasklet_disable(&priv->poll_rx_task);
4797 
4798 	/* Return all skbs to mac80211 */
4799 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
4800 		mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
4801 }
4802 
4803 static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4804 
4805 static int mwl8k_add_interface(struct ieee80211_hw *hw,
4806 			       struct ieee80211_vif *vif)
4807 {
4808 	struct mwl8k_priv *priv = hw->priv;
4809 	struct mwl8k_vif *mwl8k_vif;
4810 	u32 macids_supported;
4811 	int macid, rc;
4812 	struct mwl8k_device_info *di;
4813 
4814 	/*
4815 	 * Reject interface creation if sniffer mode is active, as
4816 	 * STA operation is mutually exclusive with hardware sniffer
4817 	 * mode.  (Sniffer mode is only used on STA firmware.)
4818 	 */
4819 	if (priv->sniffer_enabled) {
4820 		wiphy_info(hw->wiphy,
4821 			   "unable to create STA interface because sniffer mode is enabled\n");
4822 		return -EINVAL;
4823 	}
4824 
4825 	di = priv->device_info;
4826 	switch (vif->type) {
4827 	case NL80211_IFTYPE_AP:
4828 		if (!priv->ap_fw && di->fw_image_ap) {
4829 			/* we must load the ap fw to meet this request */
4830 			if (!list_empty(&priv->vif_list))
4831 				return -EBUSY;
4832 			rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4833 			if (rc)
4834 				return rc;
4835 		}
4836 		macids_supported = priv->ap_macids_supported;
4837 		break;
4838 	case NL80211_IFTYPE_STATION:
4839 		if (priv->ap_fw && di->fw_image_sta) {
4840 			if (!list_empty(&priv->vif_list)) {
4841 				wiphy_warn(hw->wiphy, "AP interface is running.\n"
4842 					   "Adding STA interface for WDS");
4843 			} else {
4844 				/* we must load the sta fw to
4845 				 * meet this request.
4846 				 */
4847 				rc = mwl8k_reload_firmware(hw,
4848 							   di->fw_image_sta);
4849 				if (rc)
4850 					return rc;
4851 			}
4852 		}
4853 		macids_supported = priv->sta_macids_supported;
4854 		break;
4855 	default:
4856 		return -EINVAL;
4857 	}
4858 
4859 	macid = ffs(macids_supported & ~priv->macids_used);
4860 	if (!macid--)
4861 		return -EBUSY;
4862 
4863 	/* Setup driver private area. */
4864 	mwl8k_vif = MWL8K_VIF(vif);
4865 	memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
4866 	mwl8k_vif->vif = vif;
4867 	mwl8k_vif->macid = macid;
4868 	mwl8k_vif->seqno = 0;
4869 	memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4870 	mwl8k_vif->is_hw_crypto_enabled = false;
4871 
4872 	/* Set the mac address.  */
4873 	mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4874 
4875 	if (vif->type == NL80211_IFTYPE_AP)
4876 		mwl8k_cmd_set_new_stn_add_self(hw, vif);
4877 
4878 	priv->macids_used |= 1 << mwl8k_vif->macid;
4879 	list_add_tail(&mwl8k_vif->list, &priv->vif_list);
4880 
4881 	return 0;
4882 }
4883 
4884 static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4885 {
4886 	/* Has ieee80211_restart_hw re-added the removed interfaces? */
4887 	if (!priv->macids_used)
4888 		return;
4889 
4890 	priv->macids_used &= ~(1 << vif->macid);
4891 	list_del(&vif->list);
4892 }
4893 
4894 static void mwl8k_remove_interface(struct ieee80211_hw *hw,
4895 				   struct ieee80211_vif *vif)
4896 {
4897 	struct mwl8k_priv *priv = hw->priv;
4898 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4899 
4900 	if (vif->type == NL80211_IFTYPE_AP)
4901 		mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4902 
4903 	mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
4904 
4905 	mwl8k_remove_vif(priv, mwl8k_vif);
4906 }
4907 
4908 static void mwl8k_hw_restart_work(struct work_struct *work)
4909 {
4910 	struct mwl8k_priv *priv =
4911 		container_of(work, struct mwl8k_priv, fw_reload);
4912 	struct ieee80211_hw *hw = priv->hw;
4913 	struct mwl8k_device_info *di;
4914 	int rc;
4915 
4916 	/* If some command is waiting for a response, clear it */
4917 	if (priv->hostcmd_wait != NULL) {
4918 		complete(priv->hostcmd_wait);
4919 		priv->hostcmd_wait = NULL;
4920 	}
4921 
4922 	priv->hw_restart_owner = current;
4923 	di = priv->device_info;
4924 	mwl8k_fw_lock(hw);
4925 
4926 	if (priv->ap_fw)
4927 		rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4928 	else
4929 		rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4930 
4931 	if (rc)
4932 		goto fail;
4933 
4934 	priv->hw_restart_owner = NULL;
4935 	priv->hw_restart_in_progress = false;
4936 
4937 	/*
4938 	 * This unlock will wake up the queues and
4939 	 * also opens the command path for other
4940 	 * commands
4941 	 */
4942 	mwl8k_fw_unlock(hw);
4943 
4944 	ieee80211_restart_hw(hw);
4945 
4946 	wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
4947 
4948 	return;
4949 fail:
4950 	mwl8k_fw_unlock(hw);
4951 
4952 	wiphy_err(hw->wiphy, "Firmware restart failed\n");
4953 }
4954 
4955 static int mwl8k_config(struct ieee80211_hw *hw, u32 changed)
4956 {
4957 	struct ieee80211_conf *conf = &hw->conf;
4958 	struct mwl8k_priv *priv = hw->priv;
4959 	int rc;
4960 
4961 	rc = mwl8k_fw_lock(hw);
4962 	if (rc)
4963 		return rc;
4964 
4965 	if (conf->flags & IEEE80211_CONF_IDLE)
4966 		rc = mwl8k_cmd_radio_disable(hw);
4967 	else
4968 		rc = mwl8k_cmd_radio_enable(hw);
4969 	if (rc)
4970 		goto out;
4971 
4972 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
4973 		rc = mwl8k_cmd_set_rf_channel(hw, conf);
4974 		if (rc)
4975 			goto out;
4976 	}
4977 
4978 	if (conf->power_level > 18)
4979 		conf->power_level = 18;
4980 
4981 	if (priv->ap_fw) {
4982 
4983 		if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
4984 			rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4985 			if (rc)
4986 				goto out;
4987 		}
4988 
4989 
4990 	} else {
4991 		rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
4992 		if (rc)
4993 			goto out;
4994 		rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
4995 	}
4996 
4997 out:
4998 	mwl8k_fw_unlock(hw);
4999 
5000 	return rc;
5001 }
5002 
5003 static void
5004 mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5005 			   struct ieee80211_bss_conf *info, u32 changed)
5006 {
5007 	struct mwl8k_priv *priv = hw->priv;
5008 	u32 ap_legacy_rates = 0;
5009 	u8 ap_mcs_rates[16];
5010 	int rc;
5011 
5012 	if (mwl8k_fw_lock(hw))
5013 		return;
5014 
5015 	/*
5016 	 * No need to capture a beacon if we're no longer associated.
5017 	 */
5018 	if ((changed & BSS_CHANGED_ASSOC) && !vif->cfg.assoc)
5019 		priv->capture_beacon = false;
5020 
5021 	/*
5022 	 * Get the AP's legacy and MCS rates.
5023 	 */
5024 	if (vif->cfg.assoc) {
5025 		struct ieee80211_sta *ap;
5026 
5027 		rcu_read_lock();
5028 
5029 		ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
5030 		if (ap == NULL) {
5031 			rcu_read_unlock();
5032 			goto out;
5033 		}
5034 
5035 		if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ) {
5036 			ap_legacy_rates = ap->deflink.supp_rates[NL80211_BAND_2GHZ];
5037 		} else {
5038 			ap_legacy_rates =
5039 				ap->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
5040 		}
5041 		memcpy(ap_mcs_rates, &ap->deflink.ht_cap.mcs, 16);
5042 
5043 		rcu_read_unlock();
5044 
5045 		if (changed & BSS_CHANGED_ASSOC) {
5046 			if (!priv->ap_fw) {
5047 				rc = mwl8k_cmd_set_rate(hw, vif,
5048 							ap_legacy_rates,
5049 							ap_mcs_rates);
5050 				if (rc)
5051 					goto out;
5052 
5053 				rc = mwl8k_cmd_use_fixed_rate_sta(hw);
5054 				if (rc)
5055 					goto out;
5056 			} else {
5057 				int idx;
5058 				int rate;
5059 
5060 				/* Use AP firmware specific rate command.
5061 				 */
5062 				idx = ffs(vif->bss_conf.basic_rates);
5063 				if (idx)
5064 					idx--;
5065 
5066 				if (hw->conf.chandef.chan->band ==
5067 				    NL80211_BAND_2GHZ)
5068 					rate = mwl8k_rates_24[idx].hw_value;
5069 				else
5070 					rate = mwl8k_rates_50[idx].hw_value;
5071 
5072 				mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
5073 			}
5074 		}
5075 	}
5076 
5077 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5078 		rc = mwl8k_set_radio_preamble(hw,
5079 				vif->bss_conf.use_short_preamble);
5080 		if (rc)
5081 			goto out;
5082 	}
5083 
5084 	if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw)  {
5085 		rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
5086 		if (rc)
5087 			goto out;
5088 	}
5089 
5090 	if (vif->cfg.assoc && !priv->ap_fw &&
5091 	    (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
5092 			BSS_CHANGED_HT))) {
5093 		rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
5094 		if (rc)
5095 			goto out;
5096 	}
5097 
5098 	if (vif->cfg.assoc &&
5099 	    (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
5100 		/*
5101 		 * Finalize the join.  Tell rx handler to process
5102 		 * next beacon from our BSSID.
5103 		 */
5104 		memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
5105 		priv->capture_beacon = true;
5106 	}
5107 
5108 out:
5109 	mwl8k_fw_unlock(hw);
5110 }
5111 
5112 static void
5113 mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5114 			  struct ieee80211_bss_conf *info, u32 changed)
5115 {
5116 	int rc;
5117 
5118 	if (mwl8k_fw_lock(hw))
5119 		return;
5120 
5121 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5122 		rc = mwl8k_set_radio_preamble(hw,
5123 				vif->bss_conf.use_short_preamble);
5124 		if (rc)
5125 			goto out;
5126 	}
5127 
5128 	if (changed & BSS_CHANGED_BASIC_RATES) {
5129 		int idx;
5130 		int rate;
5131 
5132 		/*
5133 		 * Use lowest supported basic rate for multicasts
5134 		 * and management frames (such as probe responses --
5135 		 * beacons will always go out at 1 Mb/s).
5136 		 */
5137 		idx = ffs(vif->bss_conf.basic_rates);
5138 		if (idx)
5139 			idx--;
5140 
5141 		if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
5142 			rate = mwl8k_rates_24[idx].hw_value;
5143 		else
5144 			rate = mwl8k_rates_50[idx].hw_value;
5145 
5146 		mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
5147 	}
5148 
5149 	if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
5150 		struct sk_buff *skb;
5151 
5152 		skb = ieee80211_beacon_get(hw, vif, 0);
5153 		if (skb != NULL) {
5154 			mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
5155 			kfree_skb(skb);
5156 		}
5157 	}
5158 
5159 	if (changed & BSS_CHANGED_BEACON_ENABLED)
5160 		mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
5161 
5162 out:
5163 	mwl8k_fw_unlock(hw);
5164 }
5165 
5166 static void
5167 mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5168 		       struct ieee80211_bss_conf *info, u64 changed)
5169 {
5170 	if (vif->type == NL80211_IFTYPE_STATION)
5171 		mwl8k_bss_info_changed_sta(hw, vif, info, changed);
5172 	if (vif->type == NL80211_IFTYPE_AP)
5173 		mwl8k_bss_info_changed_ap(hw, vif, info, changed);
5174 }
5175 
5176 static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
5177 				   struct netdev_hw_addr_list *mc_list)
5178 {
5179 	struct mwl8k_cmd_pkt_hdr *cmd;
5180 
5181 	/*
5182 	 * Synthesize and return a command packet that programs the
5183 	 * hardware multicast address filter.  At this point we don't
5184 	 * know whether FIF_ALLMULTI is being requested, but if it is,
5185 	 * we'll end up throwing this packet away and creating a new
5186 	 * one in mwl8k_configure_filter().
5187 	 */
5188 	cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
5189 
5190 	return (unsigned long)cmd;
5191 }
5192 
5193 static int
5194 mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
5195 			       unsigned int changed_flags,
5196 			       unsigned int *total_flags)
5197 {
5198 	struct mwl8k_priv *priv = hw->priv;
5199 
5200 	/*
5201 	 * Hardware sniffer mode is mutually exclusive with STA
5202 	 * operation, so refuse to enable sniffer mode if a STA
5203 	 * interface is active.
5204 	 */
5205 	if (!list_empty(&priv->vif_list)) {
5206 		if (net_ratelimit())
5207 			wiphy_info(hw->wiphy,
5208 				   "not enabling sniffer mode because STA interface is active\n");
5209 		return 0;
5210 	}
5211 
5212 	if (!priv->sniffer_enabled) {
5213 		if (mwl8k_cmd_enable_sniffer(hw, 1))
5214 			return 0;
5215 		priv->sniffer_enabled = true;
5216 	}
5217 
5218 	*total_flags &=	FIF_ALLMULTI |
5219 			FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
5220 			FIF_OTHER_BSS;
5221 
5222 	return 1;
5223 }
5224 
5225 static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
5226 {
5227 	if (!list_empty(&priv->vif_list))
5228 		return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
5229 
5230 	return NULL;
5231 }
5232 
5233 static void mwl8k_configure_filter(struct ieee80211_hw *hw,
5234 				   unsigned int changed_flags,
5235 				   unsigned int *total_flags,
5236 				   u64 multicast)
5237 {
5238 	struct mwl8k_priv *priv = hw->priv;
5239 	struct mwl8k_cmd_pkt_hdr *cmd = (void *)(unsigned long)multicast;
5240 
5241 	/*
5242 	 * AP firmware doesn't allow fine-grained control over
5243 	 * the receive filter.
5244 	 */
5245 	if (priv->ap_fw) {
5246 		*total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5247 		kfree(cmd);
5248 		return;
5249 	}
5250 
5251 	/*
5252 	 * Enable hardware sniffer mode if FIF_CONTROL or
5253 	 * FIF_OTHER_BSS is requested.
5254 	 */
5255 	if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
5256 	    mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
5257 		kfree(cmd);
5258 		return;
5259 	}
5260 
5261 	/* Clear unsupported feature flags */
5262 	*total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5263 
5264 	if (mwl8k_fw_lock(hw)) {
5265 		kfree(cmd);
5266 		return;
5267 	}
5268 
5269 	if (priv->sniffer_enabled) {
5270 		mwl8k_cmd_enable_sniffer(hw, 0);
5271 		priv->sniffer_enabled = false;
5272 	}
5273 
5274 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
5275 		if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
5276 			/*
5277 			 * Disable the BSS filter.
5278 			 */
5279 			mwl8k_cmd_set_pre_scan(hw);
5280 		} else {
5281 			struct mwl8k_vif *mwl8k_vif;
5282 			const u8 *bssid;
5283 
5284 			/*
5285 			 * Enable the BSS filter.
5286 			 *
5287 			 * If there is an active STA interface, use that
5288 			 * interface's BSSID, otherwise use a dummy one
5289 			 * (where the OUI part needs to be nonzero for
5290 			 * the BSSID to be accepted by POST_SCAN).
5291 			 */
5292 			mwl8k_vif = mwl8k_first_vif(priv);
5293 			if (mwl8k_vif != NULL)
5294 				bssid = mwl8k_vif->vif->bss_conf.bssid;
5295 			else
5296 				bssid = "\x01\x00\x00\x00\x00\x00";
5297 
5298 			mwl8k_cmd_set_post_scan(hw, bssid);
5299 		}
5300 	}
5301 
5302 	/*
5303 	 * If FIF_ALLMULTI is being requested, throw away the command
5304 	 * packet that ->prepare_multicast() built and replace it with
5305 	 * a command packet that enables reception of all multicast
5306 	 * packets.
5307 	 */
5308 	if (*total_flags & FIF_ALLMULTI) {
5309 		kfree(cmd);
5310 		cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
5311 	}
5312 
5313 	if (cmd != NULL) {
5314 		mwl8k_post_cmd(hw, cmd);
5315 		kfree(cmd);
5316 	}
5317 
5318 	mwl8k_fw_unlock(hw);
5319 }
5320 
5321 static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
5322 {
5323 	return mwl8k_cmd_set_rts_threshold(hw, value);
5324 }
5325 
5326 static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5327 			    struct ieee80211_vif *vif,
5328 			    struct ieee80211_sta *sta)
5329 {
5330 	struct mwl8k_priv *priv = hw->priv;
5331 
5332 	if (priv->ap_fw)
5333 		return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5334 	else
5335 		return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5336 }
5337 
5338 static int mwl8k_sta_add(struct ieee80211_hw *hw,
5339 			 struct ieee80211_vif *vif,
5340 			 struct ieee80211_sta *sta)
5341 {
5342 	struct mwl8k_priv *priv = hw->priv;
5343 	int ret;
5344 	int i;
5345 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5346 	struct ieee80211_key_conf *key;
5347 
5348 	if (!priv->ap_fw) {
5349 		ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5350 		if (ret >= 0) {
5351 			MWL8K_STA(sta)->peer_id = ret;
5352 			if (sta->deflink.ht_cap.ht_supported)
5353 				MWL8K_STA(sta)->is_ampdu_allowed = true;
5354 			ret = 0;
5355 		}
5356 
5357 	} else {
5358 		ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
5359 	}
5360 
5361 	for (i = 0; i < NUM_WEP_KEYS; i++) {
5362 		key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5363 		if (mwl8k_vif->wep_key_conf[i].enabled)
5364 			mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5365 	}
5366 	return ret;
5367 }
5368 
5369 static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5370 			 struct ieee80211_vif *vif,
5371 			 unsigned int link_id, u16 queue,
5372 			 const struct ieee80211_tx_queue_params *params)
5373 {
5374 	struct mwl8k_priv *priv = hw->priv;
5375 	int rc;
5376 
5377 	rc = mwl8k_fw_lock(hw);
5378 	if (!rc) {
5379 		BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
5380 		memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5381 
5382 		if (!priv->wmm_enabled)
5383 			rc = mwl8k_cmd_set_wmm_mode(hw, 1);
5384 
5385 		if (!rc) {
5386 			int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
5387 			rc = mwl8k_cmd_set_edca_params(hw, q,
5388 						       params->cw_min,
5389 						       params->cw_max,
5390 						       params->aifs,
5391 						       params->txop);
5392 		}
5393 
5394 		mwl8k_fw_unlock(hw);
5395 	}
5396 
5397 	return rc;
5398 }
5399 
5400 static int mwl8k_get_stats(struct ieee80211_hw *hw,
5401 			   struct ieee80211_low_level_stats *stats)
5402 {
5403 	return mwl8k_cmd_get_stat(hw, stats);
5404 }
5405 
5406 static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5407 				struct survey_info *survey)
5408 {
5409 	struct mwl8k_priv *priv = hw->priv;
5410 	struct ieee80211_conf *conf = &hw->conf;
5411 	struct ieee80211_supported_band *sband;
5412 
5413 	if (priv->ap_fw) {
5414 		sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
5415 
5416 		if (sband && idx >= sband->n_channels) {
5417 			idx -= sband->n_channels;
5418 			sband = NULL;
5419 		}
5420 
5421 		if (!sband)
5422 			sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
5423 
5424 		if (!sband || idx >= sband->n_channels)
5425 			return -ENOENT;
5426 
5427 		memcpy(survey, &priv->survey[idx], sizeof(*survey));
5428 		survey->channel = &sband->channels[idx];
5429 
5430 		return 0;
5431 	}
5432 
5433 	if (idx != 0)
5434 		return -ENOENT;
5435 
5436 	survey->channel = conf->chandef.chan;
5437 	survey->filled = SURVEY_INFO_NOISE_DBM;
5438 	survey->noise = priv->noise;
5439 
5440 	return 0;
5441 }
5442 
5443 #define MAX_AMPDU_ATTEMPTS 5
5444 
5445 static int
5446 mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5447 		   struct ieee80211_ampdu_params *params)
5448 {
5449 	struct ieee80211_sta *sta = params->sta;
5450 	enum ieee80211_ampdu_mlme_action action = params->action;
5451 	u16 tid = params->tid;
5452 	u16 *ssn = &params->ssn;
5453 	u8 buf_size = params->buf_size;
5454 	int i, rc = 0;
5455 	struct mwl8k_priv *priv = hw->priv;
5456 	struct mwl8k_ampdu_stream *stream;
5457 	u8 *addr = sta->addr, idx;
5458 	struct mwl8k_sta *sta_info = MWL8K_STA(sta);
5459 
5460 	if (!ieee80211_hw_check(hw, AMPDU_AGGREGATION))
5461 		return -ENOTSUPP;
5462 
5463 	spin_lock(&priv->stream_lock);
5464 	stream = mwl8k_lookup_stream(hw, addr, tid);
5465 
5466 	switch (action) {
5467 	case IEEE80211_AMPDU_RX_START:
5468 	case IEEE80211_AMPDU_RX_STOP:
5469 		break;
5470 	case IEEE80211_AMPDU_TX_START:
5471 		/* By the time we get here the hw queues may contain outgoing
5472 		 * packets for this RA/TID that are not part of this BA
5473 		 * session.  The hw will assign sequence numbers to these
5474 		 * packets as they go out.  So if we query the hw for its next
5475 		 * sequence number and use that for the SSN here, it may end up
5476 		 * being wrong, which will lead to sequence number mismatch at
5477 		 * the recipient.  To avoid this, we reset the sequence number
5478 		 * to O for the first MPDU in this BA stream.
5479 		 */
5480 		*ssn = 0;
5481 		if (stream == NULL) {
5482 			/* This means that somebody outside this driver called
5483 			 * ieee80211_start_tx_ba_session.  This is unexpected
5484 			 * because we do our own rate control.  Just warn and
5485 			 * move on.
5486 			 */
5487 			wiphy_warn(hw->wiphy, "Unexpected call to %s.  "
5488 				   "Proceeding anyway.\n", __func__);
5489 			stream = mwl8k_add_stream(hw, sta, tid);
5490 		}
5491 		if (stream == NULL) {
5492 			wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5493 			rc = -EBUSY;
5494 			break;
5495 		}
5496 		stream->state = AMPDU_STREAM_IN_PROGRESS;
5497 
5498 		/* Release the lock before we do the time consuming stuff */
5499 		spin_unlock(&priv->stream_lock);
5500 		for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
5501 
5502 			/* Check if link is still valid */
5503 			if (!sta_info->is_ampdu_allowed) {
5504 				spin_lock(&priv->stream_lock);
5505 				mwl8k_remove_stream(hw, stream);
5506 				spin_unlock(&priv->stream_lock);
5507 				return -EBUSY;
5508 			}
5509 
5510 			rc = mwl8k_check_ba(hw, stream, vif);
5511 
5512 			/* If HW restart is in progress mwl8k_post_cmd will
5513 			 * return -EBUSY. Avoid retrying mwl8k_check_ba in
5514 			 * such cases
5515 			 */
5516 			if (!rc || rc == -EBUSY)
5517 				break;
5518 			/*
5519 			 * HW queues take time to be flushed, give them
5520 			 * sufficient time
5521 			 */
5522 
5523 			msleep(1000);
5524 		}
5525 		spin_lock(&priv->stream_lock);
5526 		if (rc) {
5527 			wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5528 				" attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5529 			mwl8k_remove_stream(hw, stream);
5530 			rc = -EBUSY;
5531 			break;
5532 		}
5533 		rc = IEEE80211_AMPDU_TX_START_IMMEDIATE;
5534 		break;
5535 	case IEEE80211_AMPDU_TX_STOP_CONT:
5536 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
5537 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5538 		if (stream) {
5539 			if (stream->state == AMPDU_STREAM_ACTIVE) {
5540 				idx = stream->idx;
5541 				spin_unlock(&priv->stream_lock);
5542 				mwl8k_destroy_ba(hw, idx);
5543 				spin_lock(&priv->stream_lock);
5544 			}
5545 			mwl8k_remove_stream(hw, stream);
5546 		}
5547 		ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5548 		break;
5549 	case IEEE80211_AMPDU_TX_OPERATIONAL:
5550 		BUG_ON(stream == NULL);
5551 		BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5552 		spin_unlock(&priv->stream_lock);
5553 		rc = mwl8k_create_ba(hw, stream, buf_size, vif);
5554 		spin_lock(&priv->stream_lock);
5555 		if (!rc)
5556 			stream->state = AMPDU_STREAM_ACTIVE;
5557 		else {
5558 			idx = stream->idx;
5559 			spin_unlock(&priv->stream_lock);
5560 			mwl8k_destroy_ba(hw, idx);
5561 			spin_lock(&priv->stream_lock);
5562 			wiphy_debug(hw->wiphy,
5563 				"Failed adding stream for sta %pM tid %d\n",
5564 				addr, tid);
5565 			mwl8k_remove_stream(hw, stream);
5566 		}
5567 		break;
5568 
5569 	default:
5570 		rc = -ENOTSUPP;
5571 	}
5572 
5573 	spin_unlock(&priv->stream_lock);
5574 	return rc;
5575 }
5576 
5577 static void mwl8k_sw_scan_start(struct ieee80211_hw *hw,
5578 				struct ieee80211_vif *vif,
5579 				const u8 *mac_addr)
5580 {
5581 	struct mwl8k_priv *priv = hw->priv;
5582 	u8 tmp;
5583 
5584 	if (!priv->ap_fw)
5585 		return;
5586 
5587 	/* clear all stats */
5588 	priv->channel_time = 0;
5589 	ioread32(priv->regs + BBU_RXRDY_CNT_REG);
5590 	ioread32(priv->regs + NOK_CCA_CNT_REG);
5591 	mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &tmp);
5592 
5593 	priv->sw_scan_start = true;
5594 }
5595 
5596 static void mwl8k_sw_scan_complete(struct ieee80211_hw *hw,
5597 				   struct ieee80211_vif *vif)
5598 {
5599 	struct mwl8k_priv *priv = hw->priv;
5600 	u8 tmp;
5601 
5602 	if (!priv->ap_fw)
5603 		return;
5604 
5605 	priv->sw_scan_start = false;
5606 
5607 	/* clear all stats */
5608 	priv->channel_time = 0;
5609 	ioread32(priv->regs + BBU_RXRDY_CNT_REG);
5610 	ioread32(priv->regs + NOK_CCA_CNT_REG);
5611 	mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &tmp);
5612 }
5613 
5614 static const struct ieee80211_ops mwl8k_ops = {
5615 	.add_chanctx = ieee80211_emulate_add_chanctx,
5616 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
5617 	.change_chanctx = ieee80211_emulate_change_chanctx,
5618 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
5619 	.tx			= mwl8k_tx,
5620 	.wake_tx_queue		= ieee80211_handle_wake_tx_queue,
5621 	.start			= mwl8k_start,
5622 	.stop			= mwl8k_stop,
5623 	.add_interface		= mwl8k_add_interface,
5624 	.remove_interface	= mwl8k_remove_interface,
5625 	.config			= mwl8k_config,
5626 	.bss_info_changed	= mwl8k_bss_info_changed,
5627 	.prepare_multicast	= mwl8k_prepare_multicast,
5628 	.configure_filter	= mwl8k_configure_filter,
5629 	.set_key                = mwl8k_set_key,
5630 	.set_rts_threshold	= mwl8k_set_rts_threshold,
5631 	.sta_add		= mwl8k_sta_add,
5632 	.sta_remove		= mwl8k_sta_remove,
5633 	.conf_tx		= mwl8k_conf_tx,
5634 	.get_stats		= mwl8k_get_stats,
5635 	.get_survey		= mwl8k_get_survey,
5636 	.ampdu_action		= mwl8k_ampdu_action,
5637 	.sw_scan_start		= mwl8k_sw_scan_start,
5638 	.sw_scan_complete	= mwl8k_sw_scan_complete,
5639 };
5640 
5641 static void mwl8k_finalize_join_worker(struct work_struct *work)
5642 {
5643 	struct mwl8k_priv *priv =
5644 		container_of(work, struct mwl8k_priv, finalize_join_worker);
5645 	struct sk_buff *skb = priv->beacon_skb;
5646 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
5647 	int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5648 	const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5649 					 mgmt->u.beacon.variable, len);
5650 	int dtim_period = 1;
5651 
5652 	if (tim && tim[1] >= 2)
5653 		dtim_period = tim[3];
5654 
5655 	mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
5656 
5657 	dev_kfree_skb(skb);
5658 	priv->beacon_skb = NULL;
5659 }
5660 
5661 enum {
5662 	MWL8363 = 0,
5663 	MWL8687,
5664 	MWL8366,
5665 	MWL8764,
5666 };
5667 
5668 #define MWL8K_8366_AP_FW_API 3
5669 #define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5670 #define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5671 
5672 #define MWL8K_8764_AP_FW_API 1
5673 #define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
5674 #define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
5675 
5676 static struct mwl8k_device_info mwl8k_info_tbl[] = {
5677 	[MWL8363] = {
5678 		.part_name	= "88w8363",
5679 		.helper_image	= "mwl8k/helper_8363.fw",
5680 		.fw_image_sta	= "mwl8k/fmimage_8363.fw",
5681 	},
5682 	[MWL8687] = {
5683 		.part_name	= "88w8687",
5684 		.helper_image	= "mwl8k/helper_8687.fw",
5685 		.fw_image_sta	= "mwl8k/fmimage_8687.fw",
5686 	},
5687 	[MWL8366] = {
5688 		.part_name	= "88w8366",
5689 		.helper_image	= "mwl8k/helper_8366.fw",
5690 		.fw_image_sta	= "mwl8k/fmimage_8366.fw",
5691 		.fw_image_ap	= MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5692 		.fw_api_ap	= MWL8K_8366_AP_FW_API,
5693 		.ap_rxd_ops	= &rxd_ap_ops,
5694 	},
5695 	[MWL8764] = {
5696 		.part_name	= "88w8764",
5697 		.fw_image_ap	= MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
5698 		.fw_api_ap	= MWL8K_8764_AP_FW_API,
5699 		.ap_rxd_ops	= &rxd_ap_ops,
5700 	},
5701 };
5702 
5703 MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5704 MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5705 MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5706 MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5707 MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5708 MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
5709 MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
5710 
5711 static const struct pci_device_id mwl8k_pci_id_table[] = {
5712 	{ PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
5713 	{ PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5714 	{ PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
5715 	{ PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5716 	{ PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5717 	{ PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
5718 	{ PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
5719 	{ PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
5720 	{ PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
5721 	{ PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
5722 	{ },
5723 };
5724 MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5725 
5726 static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5727 {
5728 	int rc;
5729 	printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5730 	       "Trying alternative firmware %s\n", pci_name(priv->pdev),
5731 	       priv->fw_pref, priv->fw_alt);
5732 	rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5733 	if (rc) {
5734 		printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5735 		       pci_name(priv->pdev), priv->fw_alt);
5736 		return rc;
5737 	}
5738 	return 0;
5739 }
5740 
5741 static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
5742 static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5743 {
5744 	struct mwl8k_priv *priv = context;
5745 	struct mwl8k_device_info *di = priv->device_info;
5746 	int rc;
5747 
5748 	switch (priv->fw_state) {
5749 	case FW_STATE_INIT:
5750 		if (!fw) {
5751 			printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5752 			       pci_name(priv->pdev), di->helper_image);
5753 			goto fail;
5754 		}
5755 		priv->fw_helper = fw;
5756 		rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5757 				      true);
5758 		if (rc && priv->fw_alt) {
5759 			rc = mwl8k_request_alt_fw(priv);
5760 			if (rc)
5761 				goto fail;
5762 			priv->fw_state = FW_STATE_LOADING_ALT;
5763 		} else if (rc)
5764 			goto fail;
5765 		else
5766 			priv->fw_state = FW_STATE_LOADING_PREF;
5767 		break;
5768 
5769 	case FW_STATE_LOADING_PREF:
5770 		if (!fw) {
5771 			if (priv->fw_alt) {
5772 				rc = mwl8k_request_alt_fw(priv);
5773 				if (rc)
5774 					goto fail;
5775 				priv->fw_state = FW_STATE_LOADING_ALT;
5776 			} else
5777 				goto fail;
5778 		} else {
5779 			priv->fw_ucode = fw;
5780 			rc = mwl8k_firmware_load_success(priv);
5781 			if (rc)
5782 				goto fail;
5783 			else
5784 				complete(&priv->firmware_loading_complete);
5785 		}
5786 		break;
5787 
5788 	case FW_STATE_LOADING_ALT:
5789 		if (!fw) {
5790 			printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5791 			       pci_name(priv->pdev), di->helper_image);
5792 			goto fail;
5793 		}
5794 		priv->fw_ucode = fw;
5795 		rc = mwl8k_firmware_load_success(priv);
5796 		if (rc)
5797 			goto fail;
5798 		else
5799 			complete(&priv->firmware_loading_complete);
5800 		break;
5801 
5802 	default:
5803 		printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5804 		       MWL8K_NAME, priv->fw_state);
5805 		BUG_ON(1);
5806 	}
5807 
5808 	return;
5809 
5810 fail:
5811 	priv->fw_state = FW_STATE_ERROR;
5812 	complete(&priv->firmware_loading_complete);
5813 	mwl8k_release_firmware(priv);
5814 	device_release_driver(&priv->pdev->dev);
5815 }
5816 
5817 #define MAX_RESTART_ATTEMPTS 1
5818 static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5819 			       bool nowait)
5820 {
5821 	struct mwl8k_priv *priv = hw->priv;
5822 	int rc;
5823 	int count = MAX_RESTART_ATTEMPTS;
5824 
5825 retry:
5826 	/* Reset firmware and hardware */
5827 	mwl8k_hw_reset(priv);
5828 
5829 	/* Ask userland hotplug daemon for the device firmware */
5830 	rc = mwl8k_request_firmware(priv, fw_image, nowait);
5831 	if (rc) {
5832 		wiphy_err(hw->wiphy, "Firmware files not found\n");
5833 		return rc;
5834 	}
5835 
5836 	if (nowait)
5837 		return rc;
5838 
5839 	/* Load firmware into hardware */
5840 	rc = mwl8k_load_firmware(hw);
5841 	if (rc)
5842 		wiphy_err(hw->wiphy, "Cannot start firmware\n");
5843 
5844 	/* Reclaim memory once firmware is successfully loaded */
5845 	mwl8k_release_firmware(priv);
5846 
5847 	if (rc && count) {
5848 		/* FW did not start successfully;
5849 		 * lets try one more time
5850 		 */
5851 		count--;
5852 		wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5853 		msleep(20);
5854 		goto retry;
5855 	}
5856 
5857 	return rc;
5858 }
5859 
5860 static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5861 {
5862 	struct mwl8k_priv *priv = hw->priv;
5863 	int rc = 0;
5864 	int i;
5865 
5866 	for (i = 0; i < mwl8k_tx_queues(priv); i++) {
5867 		rc = mwl8k_txq_init(hw, i);
5868 		if (rc)
5869 			break;
5870 		if (priv->ap_fw)
5871 			iowrite32(priv->txq[i].txd_dma,
5872 				  priv->sram + priv->txq_offset[i]);
5873 	}
5874 	return rc;
5875 }
5876 
5877 /* initialize hw after successfully loading a firmware image */
5878 static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5879 {
5880 	struct mwl8k_priv *priv = hw->priv;
5881 	int rc = 0;
5882 	int i;
5883 
5884 	if (priv->ap_fw) {
5885 		priv->rxd_ops = priv->device_info->ap_rxd_ops;
5886 		if (priv->rxd_ops == NULL) {
5887 			wiphy_err(hw->wiphy,
5888 				  "Driver does not have AP firmware image support for this hardware\n");
5889 			rc = -ENOENT;
5890 			goto err_stop_firmware;
5891 		}
5892 	} else {
5893 		priv->rxd_ops = &rxd_sta_ops;
5894 	}
5895 
5896 	priv->sniffer_enabled = false;
5897 	priv->wmm_enabled = false;
5898 	priv->pending_tx_pkts = 0;
5899 	atomic_set(&priv->watchdog_event_pending, 0);
5900 
5901 	rc = mwl8k_rxq_init(hw, 0);
5902 	if (rc)
5903 		goto err_stop_firmware;
5904 	rxq_refill(hw, 0, INT_MAX);
5905 
5906 	/* For the sta firmware, we need to know the dma addresses of tx queues
5907 	 * before sending MWL8K_CMD_GET_HW_SPEC.  So we must initialize them
5908 	 * prior to issuing this command.  But for the AP case, we learn the
5909 	 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5910 	 * case we must initialize the tx queues after.
5911 	 */
5912 	priv->num_ampdu_queues = 0;
5913 	if (!priv->ap_fw) {
5914 		rc = mwl8k_init_txqs(hw);
5915 		if (rc)
5916 			goto err_free_queues;
5917 	}
5918 
5919 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5920 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5921 	iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5922 		  MWL8K_A2H_INT_BA_WATCHDOG,
5923 		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
5924 	iowrite32(MWL8K_A2H_INT_OPC_DONE,
5925 		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
5926 
5927 	rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5928 			 IRQF_SHARED, MWL8K_NAME, hw);
5929 	if (rc) {
5930 		wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5931 		goto err_free_queues;
5932 	}
5933 
5934 	/*
5935 	 * When hw restart is requested,
5936 	 * mac80211 will take care of clearing
5937 	 * the ampdu streams, so do not clear
5938 	 * the ampdu state here
5939 	 */
5940 	if (!priv->hw_restart_in_progress)
5941 		memset(priv->ampdu, 0, sizeof(priv->ampdu));
5942 
5943 	/*
5944 	 * Temporarily enable interrupts.  Initial firmware host
5945 	 * commands use interrupts and avoid polling.  Disable
5946 	 * interrupts when done.
5947 	 */
5948 	iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5949 
5950 	/* Get config data, mac addrs etc */
5951 	if (priv->ap_fw) {
5952 		rc = mwl8k_cmd_get_hw_spec_ap(hw);
5953 		if (!rc)
5954 			rc = mwl8k_init_txqs(hw);
5955 		if (!rc)
5956 			rc = mwl8k_cmd_set_hw_spec(hw);
5957 	} else {
5958 		rc = mwl8k_cmd_get_hw_spec_sta(hw);
5959 	}
5960 	if (rc) {
5961 		wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5962 		goto err_free_irq;
5963 	}
5964 
5965 	/* Turn radio off */
5966 	rc = mwl8k_cmd_radio_disable(hw);
5967 	if (rc) {
5968 		wiphy_err(hw->wiphy, "Cannot disable\n");
5969 		goto err_free_irq;
5970 	}
5971 
5972 	/* Clear MAC address */
5973 	rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5974 	if (rc) {
5975 		wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5976 		goto err_free_irq;
5977 	}
5978 
5979 	/* Configure Antennas */
5980 	rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
5981 	if (rc)
5982 		wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
5983 	rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
5984 	if (rc)
5985 		wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
5986 
5987 
5988 	/* Disable interrupts */
5989 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5990 	free_irq(priv->pdev->irq, hw);
5991 
5992 	wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
5993 		   priv->device_info->part_name,
5994 		   priv->hw_rev, hw->wiphy->perm_addr,
5995 		   priv->ap_fw ? "AP" : "STA",
5996 		   (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
5997 		   (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
5998 
5999 	return 0;
6000 
6001 err_free_irq:
6002 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
6003 	free_irq(priv->pdev->irq, hw);
6004 
6005 err_free_queues:
6006 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6007 		mwl8k_txq_deinit(hw, i);
6008 	mwl8k_rxq_deinit(hw, 0);
6009 
6010 err_stop_firmware:
6011 	mwl8k_hw_reset(priv);
6012 
6013 	return rc;
6014 }
6015 
6016 /*
6017  * invoke mwl8k_reload_firmware to change the firmware image after the device
6018  * has already been registered
6019  */
6020 static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
6021 {
6022 	int i, rc = 0;
6023 	struct mwl8k_priv *priv = hw->priv;
6024 	struct mwl8k_vif *vif, *tmp_vif;
6025 
6026 	mwl8k_stop(hw);
6027 	mwl8k_rxq_deinit(hw, 0);
6028 
6029 	/*
6030 	 * All the existing interfaces are re-added by the ieee80211_reconfig;
6031 	 * which means driver should remove existing interfaces before calling
6032 	 * ieee80211_restart_hw
6033 	 */
6034 	if (priv->hw_restart_in_progress)
6035 		list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
6036 			mwl8k_remove_vif(priv, vif);
6037 
6038 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6039 		mwl8k_txq_deinit(hw, i);
6040 
6041 	rc = mwl8k_init_firmware(hw, fw_image, false);
6042 	if (rc)
6043 		goto fail;
6044 
6045 	rc = mwl8k_probe_hw(hw);
6046 	if (rc)
6047 		goto fail;
6048 
6049 	if (priv->hw_restart_in_progress)
6050 		return rc;
6051 
6052 	rc = mwl8k_start(hw);
6053 	if (rc)
6054 		goto fail;
6055 
6056 	rc = mwl8k_config(hw, ~0);
6057 	if (rc)
6058 		goto fail;
6059 
6060 	for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
6061 		rc = mwl8k_conf_tx(hw, NULL, 0, i, &priv->wmm_params[i]);
6062 		if (rc)
6063 			goto fail;
6064 	}
6065 
6066 	return rc;
6067 
6068 fail:
6069 	printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
6070 	return rc;
6071 }
6072 
6073 static const struct ieee80211_iface_limit ap_if_limits[] = {
6074 	{ .max = 8,	.types = BIT(NL80211_IFTYPE_AP) },
6075 	{ .max = 1,	.types = BIT(NL80211_IFTYPE_STATION) },
6076 };
6077 
6078 static const struct ieee80211_iface_combination ap_if_comb = {
6079 	.limits = ap_if_limits,
6080 	.n_limits = ARRAY_SIZE(ap_if_limits),
6081 	.max_interfaces = 8,
6082 	.num_different_channels = 1,
6083 };
6084 
6085 
6086 static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
6087 {
6088 	struct ieee80211_hw *hw = priv->hw;
6089 	int i, rc;
6090 
6091 	rc = mwl8k_load_firmware(hw);
6092 	mwl8k_release_firmware(priv);
6093 	if (rc) {
6094 		wiphy_err(hw->wiphy, "Cannot start firmware\n");
6095 		return rc;
6096 	}
6097 
6098 	/*
6099 	 * Extra headroom is the size of the required DMA header
6100 	 * minus the size of the smallest 802.11 frame (CTS frame).
6101 	 */
6102 	hw->extra_tx_headroom =
6103 		sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
6104 
6105 	hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
6106 
6107 	hw->queues = MWL8K_TX_WMM_QUEUES;
6108 
6109 	/* Set rssi values to dBm */
6110 	ieee80211_hw_set(hw, SIGNAL_DBM);
6111 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
6112 
6113 	/*
6114 	 * Ask mac80211 to not to trigger PS mode
6115 	 * based on PM bit of incoming frames.
6116 	 */
6117 	if (priv->ap_fw)
6118 		ieee80211_hw_set(hw, AP_LINK_PS);
6119 
6120 	hw->vif_data_size = sizeof(struct mwl8k_vif);
6121 	hw->sta_data_size = sizeof(struct mwl8k_sta);
6122 
6123 	priv->macids_used = 0;
6124 	INIT_LIST_HEAD(&priv->vif_list);
6125 
6126 	/* Set default radio state and preamble */
6127 	priv->radio_on = false;
6128 	priv->radio_short_preamble = false;
6129 
6130 	/* Finalize join worker */
6131 	INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
6132 	/* Handle watchdog ba events */
6133 	INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
6134 	/* To reload the firmware if it crashes */
6135 	INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
6136 
6137 	/* TX reclaim and RX tasklets.  */
6138 	tasklet_setup(&priv->poll_tx_task, mwl8k_tx_poll);
6139 	tasklet_disable(&priv->poll_tx_task);
6140 	tasklet_setup(&priv->poll_rx_task, mwl8k_rx_poll);
6141 	tasklet_disable(&priv->poll_rx_task);
6142 
6143 	/* Power management cookie */
6144 	priv->cookie = dma_alloc_coherent(&priv->pdev->dev, 4,
6145 					  &priv->cookie_dma, GFP_KERNEL);
6146 	if (priv->cookie == NULL)
6147 		return -ENOMEM;
6148 
6149 	mutex_init(&priv->fw_mutex);
6150 	priv->fw_mutex_owner = NULL;
6151 	priv->fw_mutex_depth = 0;
6152 	priv->hostcmd_wait = NULL;
6153 
6154 	spin_lock_init(&priv->tx_lock);
6155 
6156 	spin_lock_init(&priv->stream_lock);
6157 
6158 	priv->tx_wait = NULL;
6159 
6160 	rc = mwl8k_probe_hw(hw);
6161 	if (rc)
6162 		goto err_free_cookie;
6163 
6164 	hw->wiphy->interface_modes = 0;
6165 
6166 	if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
6167 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
6168 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
6169 		hw->wiphy->iface_combinations = &ap_if_comb;
6170 		hw->wiphy->n_iface_combinations = 1;
6171 	}
6172 
6173 	if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
6174 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
6175 
6176 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6177 
6178 	rc = ieee80211_register_hw(hw);
6179 	if (rc) {
6180 		wiphy_err(hw->wiphy, "Cannot register device\n");
6181 		goto err_unprobe_hw;
6182 	}
6183 
6184 	return 0;
6185 
6186 err_unprobe_hw:
6187 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6188 		mwl8k_txq_deinit(hw, i);
6189 	mwl8k_rxq_deinit(hw, 0);
6190 
6191 err_free_cookie:
6192 	if (priv->cookie != NULL)
6193 		dma_free_coherent(&priv->pdev->dev, 4, priv->cookie,
6194 				  priv->cookie_dma);
6195 
6196 	return rc;
6197 }
6198 static int mwl8k_probe(struct pci_dev *pdev,
6199 				 const struct pci_device_id *id)
6200 {
6201 	static int printed_version;
6202 	struct ieee80211_hw *hw;
6203 	struct mwl8k_priv *priv;
6204 	struct mwl8k_device_info *di;
6205 	int rc;
6206 
6207 	if (!printed_version) {
6208 		printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
6209 		printed_version = 1;
6210 	}
6211 
6212 
6213 	rc = pci_enable_device(pdev);
6214 	if (rc) {
6215 		printk(KERN_ERR "%s: Cannot enable new PCI device\n",
6216 		       MWL8K_NAME);
6217 		return rc;
6218 	}
6219 
6220 	rc = pci_request_regions(pdev, MWL8K_NAME);
6221 	if (rc) {
6222 		printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
6223 		       MWL8K_NAME);
6224 		goto err_disable_device;
6225 	}
6226 
6227 	pci_set_master(pdev);
6228 
6229 
6230 	hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
6231 	if (hw == NULL) {
6232 		printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
6233 		rc = -ENOMEM;
6234 		goto err_free_reg;
6235 	}
6236 
6237 	SET_IEEE80211_DEV(hw, &pdev->dev);
6238 	pci_set_drvdata(pdev, hw);
6239 
6240 	priv = hw->priv;
6241 	priv->hw = hw;
6242 	priv->pdev = pdev;
6243 	priv->device_info = &mwl8k_info_tbl[id->driver_data];
6244 
6245 	if (id->driver_data == MWL8764)
6246 		priv->is_8764 = true;
6247 
6248 	priv->sram = pci_iomap(pdev, 0, 0x10000);
6249 	if (priv->sram == NULL) {
6250 		wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
6251 		rc = -EIO;
6252 		goto err_iounmap;
6253 	}
6254 
6255 	/*
6256 	 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
6257 	 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
6258 	 */
6259 	priv->regs = pci_iomap(pdev, 1, 0x10000);
6260 	if (priv->regs == NULL) {
6261 		priv->regs = pci_iomap(pdev, 2, 0x10000);
6262 		if (priv->regs == NULL) {
6263 			wiphy_err(hw->wiphy, "Cannot map device registers\n");
6264 			rc = -EIO;
6265 			goto err_iounmap;
6266 		}
6267 	}
6268 
6269 	/*
6270 	 * Choose the initial fw image depending on user input.  If a second
6271 	 * image is available, make it the alternative image that will be
6272 	 * loaded if the first one fails.
6273 	 */
6274 	init_completion(&priv->firmware_loading_complete);
6275 	di = priv->device_info;
6276 	if (ap_mode_default && di->fw_image_ap) {
6277 		priv->fw_pref = di->fw_image_ap;
6278 		priv->fw_alt = di->fw_image_sta;
6279 	} else if (!ap_mode_default && di->fw_image_sta) {
6280 		priv->fw_pref = di->fw_image_sta;
6281 		priv->fw_alt = di->fw_image_ap;
6282 	} else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
6283 		printk(KERN_WARNING "AP fw is unavailable.  Using STA fw.");
6284 		priv->fw_pref = di->fw_image_sta;
6285 	} else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
6286 		printk(KERN_WARNING "STA fw is unavailable.  Using AP fw.");
6287 		priv->fw_pref = di->fw_image_ap;
6288 	}
6289 	rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
6290 	if (rc)
6291 		goto err_stop_firmware;
6292 
6293 	priv->hw_restart_in_progress = false;
6294 
6295 	priv->running_bsses = 0;
6296 
6297 	return rc;
6298 
6299 err_stop_firmware:
6300 	mwl8k_hw_reset(priv);
6301 
6302 err_iounmap:
6303 	if (priv->regs != NULL)
6304 		pci_iounmap(pdev, priv->regs);
6305 
6306 	if (priv->sram != NULL)
6307 		pci_iounmap(pdev, priv->sram);
6308 
6309 	ieee80211_free_hw(hw);
6310 
6311 err_free_reg:
6312 	pci_release_regions(pdev);
6313 
6314 err_disable_device:
6315 	pci_disable_device(pdev);
6316 
6317 	return rc;
6318 }
6319 
6320 static void mwl8k_remove(struct pci_dev *pdev)
6321 {
6322 	struct ieee80211_hw *hw = pci_get_drvdata(pdev);
6323 	struct mwl8k_priv *priv;
6324 	int i;
6325 
6326 	if (hw == NULL)
6327 		return;
6328 	priv = hw->priv;
6329 
6330 	wait_for_completion(&priv->firmware_loading_complete);
6331 
6332 	if (priv->fw_state == FW_STATE_ERROR) {
6333 		mwl8k_hw_reset(priv);
6334 		goto unmap;
6335 	}
6336 
6337 	ieee80211_stop_queues(hw);
6338 
6339 	ieee80211_unregister_hw(hw);
6340 
6341 	/* Remove TX reclaim and RX tasklets.  */
6342 	tasklet_kill(&priv->poll_tx_task);
6343 	tasklet_kill(&priv->poll_rx_task);
6344 
6345 	/* Stop hardware */
6346 	mwl8k_hw_reset(priv);
6347 
6348 	/* Return all skbs to mac80211 */
6349 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6350 		mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
6351 
6352 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6353 		mwl8k_txq_deinit(hw, i);
6354 
6355 	mwl8k_rxq_deinit(hw, 0);
6356 
6357 	dma_free_coherent(&priv->pdev->dev, 4, priv->cookie, priv->cookie_dma);
6358 
6359 unmap:
6360 	pci_iounmap(pdev, priv->regs);
6361 	pci_iounmap(pdev, priv->sram);
6362 	ieee80211_free_hw(hw);
6363 	pci_release_regions(pdev);
6364 	pci_disable_device(pdev);
6365 }
6366 
6367 static struct pci_driver mwl8k_driver = {
6368 	.name		= MWL8K_NAME,
6369 	.id_table	= mwl8k_pci_id_table,
6370 	.probe		= mwl8k_probe,
6371 	.remove		= mwl8k_remove,
6372 };
6373 
6374 module_pci_driver(mwl8k_driver);
6375 
6376 MODULE_DESCRIPTION(MWL8K_DESC);
6377 MODULE_VERSION(MWL8K_VERSION);
6378 MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
6379 MODULE_LICENSE("GPL");
6380