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