xref: /linux/drivers/net/wireless/marvell/mwl8k.c (revision 8be4d31cb8aaeea27bde4b7ddb26e28a89062ebf)
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 radio_idx,int rts_thresh)3376 mwl8k_cmd_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
3377 			    int rts_thresh)
3378 {
3379 	struct mwl8k_cmd_set_rts_threshold *cmd;
3380 	int rc;
3381 
3382 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3383 	if (cmd == NULL)
3384 		return -ENOMEM;
3385 
3386 	cmd->header.code = cpu_to_le16(MWL8K_CMD_RTS_THRESHOLD);
3387 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3388 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3389 	cmd->threshold = cpu_to_le16(rts_thresh);
3390 
3391 	rc = mwl8k_post_cmd(hw, &cmd->header);
3392 	kfree(cmd);
3393 
3394 	return rc;
3395 }
3396 
3397 /*
3398  * CMD_SET_SLOT.
3399  */
3400 struct mwl8k_cmd_set_slot {
3401 	struct mwl8k_cmd_pkt_hdr header;
3402 	__le16 action;
3403 	__u8 short_slot;
3404 } __packed;
3405 
mwl8k_cmd_set_slot(struct ieee80211_hw * hw,bool short_slot_time)3406 static int mwl8k_cmd_set_slot(struct ieee80211_hw *hw, bool short_slot_time)
3407 {
3408 	struct mwl8k_cmd_set_slot *cmd;
3409 	int rc;
3410 
3411 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3412 	if (cmd == NULL)
3413 		return -ENOMEM;
3414 
3415 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_SLOT);
3416 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3417 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3418 	cmd->short_slot = short_slot_time;
3419 
3420 	rc = mwl8k_post_cmd(hw, &cmd->header);
3421 	kfree(cmd);
3422 
3423 	return rc;
3424 }
3425 
3426 /*
3427  * CMD_SET_EDCA_PARAMS.
3428  */
3429 struct mwl8k_cmd_set_edca_params {
3430 	struct mwl8k_cmd_pkt_hdr header;
3431 
3432 	/* See MWL8K_SET_EDCA_XXX below */
3433 	__le16 action;
3434 
3435 	/* TX opportunity in units of 32 us */
3436 	__le16 txop;
3437 
3438 	union {
3439 		struct {
3440 			/* Log exponent of max contention period: 0...15 */
3441 			__le32 log_cw_max;
3442 
3443 			/* Log exponent of min contention period: 0...15 */
3444 			__le32 log_cw_min;
3445 
3446 			/* Adaptive interframe spacing in units of 32us */
3447 			__u8 aifs;
3448 
3449 			/* TX queue to configure */
3450 			__u8 txq;
3451 		} ap;
3452 		struct {
3453 			/* Log exponent of max contention period: 0...15 */
3454 			__u8 log_cw_max;
3455 
3456 			/* Log exponent of min contention period: 0...15 */
3457 			__u8 log_cw_min;
3458 
3459 			/* Adaptive interframe spacing in units of 32us */
3460 			__u8 aifs;
3461 
3462 			/* TX queue to configure */
3463 			__u8 txq;
3464 		} sta;
3465 	};
3466 } __packed;
3467 
3468 #define MWL8K_SET_EDCA_CW	0x01
3469 #define MWL8K_SET_EDCA_TXOP	0x02
3470 #define MWL8K_SET_EDCA_AIFS	0x04
3471 
3472 #define MWL8K_SET_EDCA_ALL	(MWL8K_SET_EDCA_CW | \
3473 				 MWL8K_SET_EDCA_TXOP | \
3474 				 MWL8K_SET_EDCA_AIFS)
3475 
3476 static int
mwl8k_cmd_set_edca_params(struct ieee80211_hw * hw,__u8 qnum,__u16 cw_min,__u16 cw_max,__u8 aifs,__u16 txop)3477 mwl8k_cmd_set_edca_params(struct ieee80211_hw *hw, __u8 qnum,
3478 			  __u16 cw_min, __u16 cw_max,
3479 			  __u8 aifs, __u16 txop)
3480 {
3481 	struct mwl8k_priv *priv = hw->priv;
3482 	struct mwl8k_cmd_set_edca_params *cmd;
3483 	int rc;
3484 
3485 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3486 	if (cmd == NULL)
3487 		return -ENOMEM;
3488 
3489 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_EDCA_PARAMS);
3490 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3491 	cmd->action = cpu_to_le16(MWL8K_SET_EDCA_ALL);
3492 	cmd->txop = cpu_to_le16(txop);
3493 	if (priv->ap_fw) {
3494 		cmd->ap.log_cw_max = cpu_to_le32(ilog2(cw_max + 1));
3495 		cmd->ap.log_cw_min = cpu_to_le32(ilog2(cw_min + 1));
3496 		cmd->ap.aifs = aifs;
3497 		cmd->ap.txq = qnum;
3498 	} else {
3499 		cmd->sta.log_cw_max = (u8)ilog2(cw_max + 1);
3500 		cmd->sta.log_cw_min = (u8)ilog2(cw_min + 1);
3501 		cmd->sta.aifs = aifs;
3502 		cmd->sta.txq = qnum;
3503 	}
3504 
3505 	rc = mwl8k_post_cmd(hw, &cmd->header);
3506 	kfree(cmd);
3507 
3508 	return rc;
3509 }
3510 
3511 /*
3512  * CMD_SET_WMM_MODE.
3513  */
3514 struct mwl8k_cmd_set_wmm_mode {
3515 	struct mwl8k_cmd_pkt_hdr header;
3516 	__le16 action;
3517 } __packed;
3518 
mwl8k_cmd_set_wmm_mode(struct ieee80211_hw * hw,bool enable)3519 static int mwl8k_cmd_set_wmm_mode(struct ieee80211_hw *hw, bool enable)
3520 {
3521 	struct mwl8k_priv *priv = hw->priv;
3522 	struct mwl8k_cmd_set_wmm_mode *cmd;
3523 	int rc;
3524 
3525 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3526 	if (cmd == NULL)
3527 		return -ENOMEM;
3528 
3529 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_WMM_MODE);
3530 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3531 	cmd->action = cpu_to_le16(!!enable);
3532 
3533 	rc = mwl8k_post_cmd(hw, &cmd->header);
3534 	kfree(cmd);
3535 
3536 	if (!rc)
3537 		priv->wmm_enabled = enable;
3538 
3539 	return rc;
3540 }
3541 
3542 /*
3543  * CMD_MIMO_CONFIG.
3544  */
3545 struct mwl8k_cmd_mimo_config {
3546 	struct mwl8k_cmd_pkt_hdr header;
3547 	__le32 action;
3548 	__u8 rx_antenna_map;
3549 	__u8 tx_antenna_map;
3550 } __packed;
3551 
mwl8k_cmd_mimo_config(struct ieee80211_hw * hw,__u8 rx,__u8 tx)3552 static int mwl8k_cmd_mimo_config(struct ieee80211_hw *hw, __u8 rx, __u8 tx)
3553 {
3554 	struct mwl8k_cmd_mimo_config *cmd;
3555 	int rc;
3556 
3557 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3558 	if (cmd == NULL)
3559 		return -ENOMEM;
3560 
3561 	cmd->header.code = cpu_to_le16(MWL8K_CMD_MIMO_CONFIG);
3562 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3563 	cmd->action = cpu_to_le32((u32)MWL8K_CMD_SET);
3564 	cmd->rx_antenna_map = rx;
3565 	cmd->tx_antenna_map = tx;
3566 
3567 	rc = mwl8k_post_cmd(hw, &cmd->header);
3568 	kfree(cmd);
3569 
3570 	return rc;
3571 }
3572 
3573 /*
3574  * CMD_USE_FIXED_RATE (STA version).
3575  */
3576 struct mwl8k_cmd_use_fixed_rate_sta {
3577 	struct mwl8k_cmd_pkt_hdr header;
3578 	__le32 action;
3579 	__le32 allow_rate_drop;
3580 	__le32 num_rates;
3581 	struct {
3582 		__le32 is_ht_rate;
3583 		__le32 enable_retry;
3584 		__le32 rate;
3585 		__le32 retry_count;
3586 	} rate_entry[8];
3587 	__le32 rate_type;
3588 	__le32 reserved1;
3589 	__le32 reserved2;
3590 } __packed;
3591 
3592 #define MWL8K_USE_AUTO_RATE	0x0002
3593 #define MWL8K_UCAST_RATE	0
3594 
mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw * hw)3595 static int mwl8k_cmd_use_fixed_rate_sta(struct ieee80211_hw *hw)
3596 {
3597 	struct mwl8k_cmd_use_fixed_rate_sta *cmd;
3598 	int rc;
3599 
3600 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3601 	if (cmd == NULL)
3602 		return -ENOMEM;
3603 
3604 	cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3605 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3606 	cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3607 	cmd->rate_type = cpu_to_le32(MWL8K_UCAST_RATE);
3608 
3609 	rc = mwl8k_post_cmd(hw, &cmd->header);
3610 	kfree(cmd);
3611 
3612 	return rc;
3613 }
3614 
3615 /*
3616  * CMD_USE_FIXED_RATE (AP version).
3617  */
3618 struct mwl8k_cmd_use_fixed_rate_ap {
3619 	struct mwl8k_cmd_pkt_hdr header;
3620 	__le32 action;
3621 	__le32 allow_rate_drop;
3622 	__le32 num_rates;
3623 	struct mwl8k_rate_entry_ap {
3624 		__le32 is_ht_rate;
3625 		__le32 enable_retry;
3626 		__le32 rate;
3627 		__le32 retry_count;
3628 	} rate_entry[4];
3629 	u8 multicast_rate;
3630 	u8 multicast_rate_type;
3631 	u8 management_rate;
3632 } __packed;
3633 
3634 static int
mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw * hw,int mcast,int mgmt)3635 mwl8k_cmd_use_fixed_rate_ap(struct ieee80211_hw *hw, int mcast, int mgmt)
3636 {
3637 	struct mwl8k_cmd_use_fixed_rate_ap *cmd;
3638 	int rc;
3639 
3640 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3641 	if (cmd == NULL)
3642 		return -ENOMEM;
3643 
3644 	cmd->header.code = cpu_to_le16(MWL8K_CMD_USE_FIXED_RATE);
3645 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3646 	cmd->action = cpu_to_le32(MWL8K_USE_AUTO_RATE);
3647 	cmd->multicast_rate = mcast;
3648 	cmd->management_rate = mgmt;
3649 
3650 	rc = mwl8k_post_cmd(hw, &cmd->header);
3651 	kfree(cmd);
3652 
3653 	return rc;
3654 }
3655 
3656 /*
3657  * CMD_ENABLE_SNIFFER.
3658  */
3659 struct mwl8k_cmd_enable_sniffer {
3660 	struct mwl8k_cmd_pkt_hdr header;
3661 	__le32 action;
3662 } __packed;
3663 
mwl8k_cmd_enable_sniffer(struct ieee80211_hw * hw,bool enable)3664 static int mwl8k_cmd_enable_sniffer(struct ieee80211_hw *hw, bool enable)
3665 {
3666 	struct mwl8k_cmd_enable_sniffer *cmd;
3667 	int rc;
3668 
3669 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3670 	if (cmd == NULL)
3671 		return -ENOMEM;
3672 
3673 	cmd->header.code = cpu_to_le16(MWL8K_CMD_ENABLE_SNIFFER);
3674 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3675 	cmd->action = cpu_to_le32(!!enable);
3676 
3677 	rc = mwl8k_post_cmd(hw, &cmd->header);
3678 	kfree(cmd);
3679 
3680 	return rc;
3681 }
3682 
3683 struct mwl8k_cmd_update_mac_addr {
3684 	struct mwl8k_cmd_pkt_hdr header;
3685 	union {
3686 		struct {
3687 			__le16 mac_type;
3688 			__u8 mac_addr[ETH_ALEN];
3689 		} mbss;
3690 		__u8 mac_addr[ETH_ALEN];
3691 	};
3692 } __packed;
3693 
3694 #define MWL8K_MAC_TYPE_PRIMARY_CLIENT		0
3695 #define MWL8K_MAC_TYPE_SECONDARY_CLIENT		1
3696 #define MWL8K_MAC_TYPE_PRIMARY_AP		2
3697 #define MWL8K_MAC_TYPE_SECONDARY_AP		3
3698 
mwl8k_cmd_update_mac_addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 * mac,bool set)3699 static int mwl8k_cmd_update_mac_addr(struct ieee80211_hw *hw,
3700 				  struct ieee80211_vif *vif, u8 *mac, bool set)
3701 {
3702 	struct mwl8k_priv *priv = hw->priv;
3703 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3704 	struct mwl8k_cmd_update_mac_addr *cmd;
3705 	int mac_type;
3706 	int rc;
3707 
3708 	mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3709 	if (vif != NULL && vif->type == NL80211_IFTYPE_STATION) {
3710 		if (mwl8k_vif->macid + 1 == ffs(priv->sta_macids_supported))
3711 			if (priv->ap_fw)
3712 				mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3713 			else
3714 				mac_type = MWL8K_MAC_TYPE_PRIMARY_CLIENT;
3715 		else
3716 			mac_type = MWL8K_MAC_TYPE_SECONDARY_CLIENT;
3717 	} else if (vif != NULL && vif->type == NL80211_IFTYPE_AP) {
3718 		if (mwl8k_vif->macid + 1 == ffs(priv->ap_macids_supported))
3719 			mac_type = MWL8K_MAC_TYPE_PRIMARY_AP;
3720 		else
3721 			mac_type = MWL8K_MAC_TYPE_SECONDARY_AP;
3722 	}
3723 
3724 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3725 	if (cmd == NULL)
3726 		return -ENOMEM;
3727 
3728 	if (set)
3729 		cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_MAC_ADDR);
3730 	else
3731 		cmd->header.code = cpu_to_le16(MWL8K_CMD_DEL_MAC_ADDR);
3732 
3733 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3734 	if (priv->ap_fw) {
3735 		cmd->mbss.mac_type = cpu_to_le16(mac_type);
3736 		memcpy(cmd->mbss.mac_addr, mac, ETH_ALEN);
3737 	} else {
3738 		memcpy(cmd->mac_addr, mac, ETH_ALEN);
3739 	}
3740 
3741 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3742 	kfree(cmd);
3743 
3744 	return rc;
3745 }
3746 
3747 /*
3748  * MWL8K_CMD_SET_MAC_ADDR.
3749  */
mwl8k_cmd_set_mac_addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 * mac)3750 static inline int mwl8k_cmd_set_mac_addr(struct ieee80211_hw *hw,
3751 				  struct ieee80211_vif *vif, u8 *mac)
3752 {
3753 	return mwl8k_cmd_update_mac_addr(hw, vif, mac, true);
3754 }
3755 
3756 /*
3757  * MWL8K_CMD_DEL_MAC_ADDR.
3758  */
mwl8k_cmd_del_mac_addr(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 * mac)3759 static inline int mwl8k_cmd_del_mac_addr(struct ieee80211_hw *hw,
3760 				  struct ieee80211_vif *vif, u8 *mac)
3761 {
3762 	return mwl8k_cmd_update_mac_addr(hw, vif, mac, false);
3763 }
3764 
3765 /*
3766  * CMD_SET_RATEADAPT_MODE.
3767  */
3768 struct mwl8k_cmd_set_rate_adapt_mode {
3769 	struct mwl8k_cmd_pkt_hdr header;
3770 	__le16 action;
3771 	__le16 mode;
3772 } __packed;
3773 
mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw * hw,__u16 mode)3774 static int mwl8k_cmd_set_rateadapt_mode(struct ieee80211_hw *hw, __u16 mode)
3775 {
3776 	struct mwl8k_cmd_set_rate_adapt_mode *cmd;
3777 	int rc;
3778 
3779 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3780 	if (cmd == NULL)
3781 		return -ENOMEM;
3782 
3783 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_RATEADAPT_MODE);
3784 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3785 	cmd->action = cpu_to_le16(MWL8K_CMD_SET);
3786 	cmd->mode = cpu_to_le16(mode);
3787 
3788 	rc = mwl8k_post_cmd(hw, &cmd->header);
3789 	kfree(cmd);
3790 
3791 	return rc;
3792 }
3793 
3794 /*
3795  * CMD_GET_WATCHDOG_BITMAP.
3796  */
3797 struct mwl8k_cmd_get_watchdog_bitmap {
3798 	struct mwl8k_cmd_pkt_hdr header;
3799 	u8	bitmap;
3800 } __packed;
3801 
mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw * hw,u8 * bitmap)3802 static int mwl8k_cmd_get_watchdog_bitmap(struct ieee80211_hw *hw, u8 *bitmap)
3803 {
3804 	struct mwl8k_cmd_get_watchdog_bitmap *cmd;
3805 	int rc;
3806 
3807 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3808 	if (cmd == NULL)
3809 		return -ENOMEM;
3810 
3811 	cmd->header.code = cpu_to_le16(MWL8K_CMD_GET_WATCHDOG_BITMAP);
3812 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3813 
3814 	rc = mwl8k_post_cmd(hw, &cmd->header);
3815 	if (!rc)
3816 		*bitmap = cmd->bitmap;
3817 
3818 	kfree(cmd);
3819 
3820 	return rc;
3821 }
3822 
3823 #define MWL8K_WMM_QUEUE_NUMBER	3
3824 
3825 static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
3826 			     u8 idx);
3827 
mwl8k_watchdog_ba_events(struct work_struct * work)3828 static void mwl8k_watchdog_ba_events(struct work_struct *work)
3829 {
3830 	int rc;
3831 	u8 bitmap = 0, stream_index;
3832 	struct mwl8k_ampdu_stream *streams;
3833 	struct mwl8k_priv *priv =
3834 		container_of(work, struct mwl8k_priv, watchdog_ba_handle);
3835 	struct ieee80211_hw *hw = priv->hw;
3836 	int i;
3837 	u32 status = 0;
3838 
3839 	mwl8k_fw_lock(hw);
3840 
3841 	rc = mwl8k_cmd_get_watchdog_bitmap(priv->hw, &bitmap);
3842 	if (rc)
3843 		goto done;
3844 
3845 	spin_lock(&priv->stream_lock);
3846 
3847 	/* the bitmap is the hw queue number.  Map it to the ampdu queue. */
3848 	for (i = 0; i < TOTAL_HW_TX_QUEUES; i++) {
3849 		if (bitmap & (1 << i)) {
3850 			stream_index = (i + MWL8K_WMM_QUEUE_NUMBER) %
3851 				       TOTAL_HW_TX_QUEUES;
3852 			streams = &priv->ampdu[stream_index];
3853 			if (streams->state == AMPDU_STREAM_ACTIVE) {
3854 				ieee80211_stop_tx_ba_session(streams->sta,
3855 							     streams->tid);
3856 				spin_unlock(&priv->stream_lock);
3857 				mwl8k_destroy_ba(hw, stream_index);
3858 				spin_lock(&priv->stream_lock);
3859 			}
3860 		}
3861 	}
3862 
3863 	spin_unlock(&priv->stream_lock);
3864 done:
3865 	atomic_dec(&priv->watchdog_event_pending);
3866 	status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3867 	iowrite32((status | MWL8K_A2H_INT_BA_WATCHDOG),
3868 		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
3869 	mwl8k_fw_unlock(hw);
3870 	return;
3871 }
3872 
3873 
3874 /*
3875  * CMD_BSS_START.
3876  */
3877 struct mwl8k_cmd_bss_start {
3878 	struct mwl8k_cmd_pkt_hdr header;
3879 	__le32 enable;
3880 } __packed;
3881 
mwl8k_cmd_bss_start(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int enable)3882 static int mwl8k_cmd_bss_start(struct ieee80211_hw *hw,
3883 			       struct ieee80211_vif *vif, int enable)
3884 {
3885 	struct mwl8k_cmd_bss_start *cmd;
3886 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
3887 	struct mwl8k_priv *priv = hw->priv;
3888 	int rc;
3889 
3890 	if (enable && (priv->running_bsses & (1 << mwl8k_vif->macid)))
3891 		return 0;
3892 
3893 	if (!enable && !(priv->running_bsses & (1 << mwl8k_vif->macid)))
3894 		return 0;
3895 
3896 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3897 	if (cmd == NULL)
3898 		return -ENOMEM;
3899 
3900 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BSS_START);
3901 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3902 	cmd->enable = cpu_to_le32(enable);
3903 
3904 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
3905 	kfree(cmd);
3906 
3907 	if (!rc) {
3908 		if (enable)
3909 			priv->running_bsses |= (1 << mwl8k_vif->macid);
3910 		else
3911 			priv->running_bsses &= ~(1 << mwl8k_vif->macid);
3912 	}
3913 	return rc;
3914 }
3915 
mwl8k_enable_bsses(struct ieee80211_hw * hw,bool enable,u32 bitmap)3916 static void mwl8k_enable_bsses(struct ieee80211_hw *hw, bool enable, u32 bitmap)
3917 {
3918 	struct mwl8k_priv *priv = hw->priv;
3919 	struct mwl8k_vif *mwl8k_vif, *tmp_vif;
3920 	struct ieee80211_vif *vif;
3921 
3922 	list_for_each_entry_safe(mwl8k_vif, tmp_vif, &priv->vif_list, list) {
3923 		vif = mwl8k_vif->vif;
3924 
3925 		if (!(bitmap & (1 << mwl8k_vif->macid)))
3926 			continue;
3927 
3928 		if (vif->type == NL80211_IFTYPE_AP)
3929 			mwl8k_cmd_bss_start(hw, vif, enable);
3930 	}
3931 }
3932 /*
3933  * CMD_BASTREAM.
3934  */
3935 
3936 /*
3937  * UPSTREAM is tx direction
3938  */
3939 #define BASTREAM_FLAG_DIRECTION_UPSTREAM	0x00
3940 #define BASTREAM_FLAG_IMMEDIATE_TYPE		0x01
3941 
3942 enum ba_stream_action_type {
3943 	MWL8K_BA_CREATE,
3944 	MWL8K_BA_UPDATE,
3945 	MWL8K_BA_DESTROY,
3946 	MWL8K_BA_FLUSH,
3947 	MWL8K_BA_CHECK,
3948 };
3949 
3950 
3951 struct mwl8k_create_ba_stream {
3952 	__le32	flags;
3953 	__le32	idle_thrs;
3954 	__le32	bar_thrs;
3955 	__le32	window_size;
3956 	u8	peer_mac_addr[6];
3957 	u8	dialog_token;
3958 	u8	tid;
3959 	u8	queue_id;
3960 	u8	param_info;
3961 	__le32	ba_context;
3962 	u8	reset_seq_no_flag;
3963 	__le16	curr_seq_no;
3964 	u8	sta_src_mac_addr[6];
3965 } __packed;
3966 
3967 struct mwl8k_destroy_ba_stream {
3968 	__le32	flags;
3969 	__le32	ba_context;
3970 } __packed;
3971 
3972 struct mwl8k_cmd_bastream {
3973 	struct mwl8k_cmd_pkt_hdr	header;
3974 	__le32	action;
3975 	union {
3976 		struct mwl8k_create_ba_stream	create_params;
3977 		struct mwl8k_destroy_ba_stream	destroy_params;
3978 	};
3979 } __packed;
3980 
3981 static int
mwl8k_check_ba(struct ieee80211_hw * hw,struct mwl8k_ampdu_stream * stream,struct ieee80211_vif * vif)3982 mwl8k_check_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
3983 	       struct ieee80211_vif *vif)
3984 {
3985 	struct mwl8k_cmd_bastream *cmd;
3986 	int rc;
3987 
3988 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
3989 	if (cmd == NULL)
3990 		return -ENOMEM;
3991 
3992 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
3993 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
3994 
3995 	cmd->action = cpu_to_le32(MWL8K_BA_CHECK);
3996 
3997 	cmd->create_params.queue_id = stream->idx;
3998 	memcpy(&cmd->create_params.peer_mac_addr[0], stream->sta->addr,
3999 	       ETH_ALEN);
4000 	cmd->create_params.tid = stream->tid;
4001 
4002 	cmd->create_params.flags =
4003 		cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE) |
4004 		cpu_to_le32(BASTREAM_FLAG_DIRECTION_UPSTREAM);
4005 
4006 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4007 
4008 	kfree(cmd);
4009 
4010 	return rc;
4011 }
4012 
4013 static int
mwl8k_create_ba(struct ieee80211_hw * hw,struct mwl8k_ampdu_stream * stream,u8 buf_size,struct ieee80211_vif * vif)4014 mwl8k_create_ba(struct ieee80211_hw *hw, struct mwl8k_ampdu_stream *stream,
4015 		u8 buf_size, struct ieee80211_vif *vif)
4016 {
4017 	struct mwl8k_cmd_bastream *cmd;
4018 	int rc;
4019 
4020 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4021 	if (cmd == NULL)
4022 		return -ENOMEM;
4023 
4024 
4025 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
4026 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4027 
4028 	cmd->action = cpu_to_le32(MWL8K_BA_CREATE);
4029 
4030 	cmd->create_params.bar_thrs = cpu_to_le32((u32)buf_size);
4031 	cmd->create_params.window_size = cpu_to_le32((u32)buf_size);
4032 	cmd->create_params.queue_id = stream->idx;
4033 
4034 	memcpy(cmd->create_params.peer_mac_addr, stream->sta->addr, ETH_ALEN);
4035 	cmd->create_params.tid = stream->tid;
4036 	cmd->create_params.curr_seq_no = cpu_to_le16(0);
4037 	cmd->create_params.reset_seq_no_flag = 1;
4038 
4039 	cmd->create_params.param_info =
4040 		(stream->sta->deflink.ht_cap.ampdu_factor &
4041 		 IEEE80211_HT_AMPDU_PARM_FACTOR) |
4042 		((stream->sta->deflink.ht_cap.ampdu_density << 2) &
4043 		 IEEE80211_HT_AMPDU_PARM_DENSITY);
4044 
4045 	cmd->create_params.flags =
4046 		cpu_to_le32(BASTREAM_FLAG_IMMEDIATE_TYPE |
4047 					BASTREAM_FLAG_DIRECTION_UPSTREAM);
4048 
4049 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4050 
4051 	wiphy_debug(hw->wiphy, "Created a BA stream for %pM : tid %d\n",
4052 		stream->sta->addr, stream->tid);
4053 	kfree(cmd);
4054 
4055 	return rc;
4056 }
4057 
mwl8k_destroy_ba(struct ieee80211_hw * hw,u8 idx)4058 static void mwl8k_destroy_ba(struct ieee80211_hw *hw,
4059 			     u8 idx)
4060 {
4061 	struct mwl8k_cmd_bastream *cmd;
4062 
4063 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4064 	if (cmd == NULL)
4065 		return;
4066 
4067 	cmd->header.code = cpu_to_le16(MWL8K_CMD_BASTREAM);
4068 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4069 	cmd->action = cpu_to_le32(MWL8K_BA_DESTROY);
4070 
4071 	cmd->destroy_params.ba_context = cpu_to_le32(idx);
4072 	mwl8k_post_cmd(hw, &cmd->header);
4073 
4074 	wiphy_debug(hw->wiphy, "Deleted BA stream index %d\n", idx);
4075 
4076 	kfree(cmd);
4077 }
4078 
4079 /*
4080  * CMD_SET_NEW_STN.
4081  */
4082 struct mwl8k_cmd_set_new_stn {
4083 	struct mwl8k_cmd_pkt_hdr header;
4084 	__le16 aid;
4085 	__u8 mac_addr[6];
4086 	__le16 stn_id;
4087 	__le16 action;
4088 	__le16 rsvd;
4089 	__le32 legacy_rates;
4090 	__u8 ht_rates[4];
4091 	__le16 cap_info;
4092 	__le16 ht_capabilities_info;
4093 	__u8 mac_ht_param_info;
4094 	__u8 rev;
4095 	__u8 control_channel;
4096 	__u8 add_channel;
4097 	__le16 op_mode;
4098 	__le16 stbc;
4099 	__u8 add_qos_info;
4100 	__u8 is_qos_sta;
4101 	__le32 fw_sta_ptr;
4102 } __packed;
4103 
4104 #define MWL8K_STA_ACTION_ADD		0
4105 #define MWL8K_STA_ACTION_REMOVE		2
4106 
mwl8k_cmd_set_new_stn_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)4107 static int mwl8k_cmd_set_new_stn_add(struct ieee80211_hw *hw,
4108 				     struct ieee80211_vif *vif,
4109 				     struct ieee80211_sta *sta)
4110 {
4111 	struct mwl8k_cmd_set_new_stn *cmd;
4112 	u32 rates;
4113 	int rc;
4114 
4115 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4116 	if (cmd == NULL)
4117 		return -ENOMEM;
4118 
4119 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4120 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4121 	cmd->aid = cpu_to_le16(sta->aid);
4122 	memcpy(cmd->mac_addr, sta->addr, ETH_ALEN);
4123 	cmd->stn_id = cpu_to_le16(sta->aid);
4124 	cmd->action = cpu_to_le16(MWL8K_STA_ACTION_ADD);
4125 	if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
4126 		rates = sta->deflink.supp_rates[NL80211_BAND_2GHZ];
4127 	else
4128 		rates = sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
4129 	cmd->legacy_rates = cpu_to_le32(rates);
4130 	if (sta->deflink.ht_cap.ht_supported) {
4131 		cmd->ht_rates[0] = sta->deflink.ht_cap.mcs.rx_mask[0];
4132 		cmd->ht_rates[1] = sta->deflink.ht_cap.mcs.rx_mask[1];
4133 		cmd->ht_rates[2] = sta->deflink.ht_cap.mcs.rx_mask[2];
4134 		cmd->ht_rates[3] = sta->deflink.ht_cap.mcs.rx_mask[3];
4135 		cmd->ht_capabilities_info = cpu_to_le16(sta->deflink.ht_cap.cap);
4136 		cmd->mac_ht_param_info = (sta->deflink.ht_cap.ampdu_factor & 3) |
4137 			((sta->deflink.ht_cap.ampdu_density & 7) << 2);
4138 		cmd->is_qos_sta = 1;
4139 	}
4140 
4141 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4142 	kfree(cmd);
4143 
4144 	return rc;
4145 }
4146 
mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4147 static int mwl8k_cmd_set_new_stn_add_self(struct ieee80211_hw *hw,
4148 					  struct ieee80211_vif *vif)
4149 {
4150 	struct mwl8k_cmd_set_new_stn *cmd;
4151 	int rc;
4152 
4153 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4154 	if (cmd == NULL)
4155 		return -ENOMEM;
4156 
4157 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4158 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4159 	memcpy(cmd->mac_addr, vif->addr, ETH_ALEN);
4160 
4161 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4162 	kfree(cmd);
4163 
4164 	return rc;
4165 }
4166 
mwl8k_cmd_set_new_stn_del(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 * addr)4167 static int mwl8k_cmd_set_new_stn_del(struct ieee80211_hw *hw,
4168 				     struct ieee80211_vif *vif, u8 *addr)
4169 {
4170 	struct mwl8k_cmd_set_new_stn *cmd;
4171 	struct mwl8k_priv *priv = hw->priv;
4172 	int rc, i;
4173 	u8 idx;
4174 
4175 	spin_lock(&priv->stream_lock);
4176 	/* Destroy any active ampdu streams for this sta */
4177 	for (i = 0; i < MWL8K_NUM_AMPDU_STREAMS; i++) {
4178 		struct mwl8k_ampdu_stream *s;
4179 		s = &priv->ampdu[i];
4180 		if (s->state != AMPDU_NO_STREAM) {
4181 			if (memcmp(s->sta->addr, addr, ETH_ALEN) == 0) {
4182 				if (s->state == AMPDU_STREAM_ACTIVE) {
4183 					idx = s->idx;
4184 					spin_unlock(&priv->stream_lock);
4185 					mwl8k_destroy_ba(hw, idx);
4186 					spin_lock(&priv->stream_lock);
4187 				} else if (s->state == AMPDU_STREAM_NEW) {
4188 					mwl8k_remove_stream(hw, s);
4189 				}
4190 			}
4191 		}
4192 	}
4193 
4194 	spin_unlock(&priv->stream_lock);
4195 
4196 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4197 	if (cmd == NULL)
4198 		return -ENOMEM;
4199 
4200 	cmd->header.code = cpu_to_le16(MWL8K_CMD_SET_NEW_STN);
4201 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4202 	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4203 	cmd->action = cpu_to_le16(MWL8K_STA_ACTION_REMOVE);
4204 
4205 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4206 	kfree(cmd);
4207 
4208 	return rc;
4209 }
4210 
4211 /*
4212  * CMD_UPDATE_ENCRYPTION.
4213  */
4214 
4215 #define MAX_ENCR_KEY_LENGTH	16
4216 #define MIC_KEY_LENGTH		8
4217 
4218 struct mwl8k_cmd_update_encryption {
4219 	struct mwl8k_cmd_pkt_hdr header;
4220 
4221 	__le32 action;
4222 	__le32 reserved;
4223 	__u8 mac_addr[6];
4224 	__u8 encr_type;
4225 
4226 } __packed;
4227 
4228 struct mwl8k_cmd_set_key {
4229 	struct mwl8k_cmd_pkt_hdr header;
4230 
4231 	__le32 action;
4232 	__le32 reserved;
4233 	__le16 length;
4234 	__le16 key_type_id;
4235 	__le32 key_info;
4236 	__le32 key_id;
4237 	__le16 key_len;
4238 	struct {
4239 		__u8 key_material[MAX_ENCR_KEY_LENGTH];
4240 		__u8 tkip_tx_mic_key[MIC_KEY_LENGTH];
4241 		__u8 tkip_rx_mic_key[MIC_KEY_LENGTH];
4242 	} tkip;
4243 	__le16 tkip_rsc_low;
4244 	__le32 tkip_rsc_high;
4245 	__le16 tkip_tsc_low;
4246 	__le32 tkip_tsc_high;
4247 	__u8 mac_addr[6];
4248 } __packed;
4249 
4250 enum {
4251 	MWL8K_ENCR_ENABLE,
4252 	MWL8K_ENCR_SET_KEY,
4253 	MWL8K_ENCR_REMOVE_KEY,
4254 	MWL8K_ENCR_SET_GROUP_KEY,
4255 };
4256 
4257 #define MWL8K_UPDATE_ENCRYPTION_TYPE_WEP	0
4258 #define MWL8K_UPDATE_ENCRYPTION_TYPE_DISABLE	1
4259 #define MWL8K_UPDATE_ENCRYPTION_TYPE_TKIP	4
4260 #define MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED	7
4261 #define MWL8K_UPDATE_ENCRYPTION_TYPE_AES	8
4262 
4263 enum {
4264 	MWL8K_ALG_WEP,
4265 	MWL8K_ALG_TKIP,
4266 	MWL8K_ALG_CCMP,
4267 };
4268 
4269 #define MWL8K_KEY_FLAG_TXGROUPKEY	0x00000004
4270 #define MWL8K_KEY_FLAG_PAIRWISE		0x00000008
4271 #define MWL8K_KEY_FLAG_TSC_VALID	0x00000040
4272 #define MWL8K_KEY_FLAG_WEP_TXKEY	0x01000000
4273 #define MWL8K_KEY_FLAG_MICKEY_VALID	0x02000000
4274 
mwl8k_cmd_update_encryption_enable(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 * addr,u8 encr_type)4275 static int mwl8k_cmd_update_encryption_enable(struct ieee80211_hw *hw,
4276 					      struct ieee80211_vif *vif,
4277 					      u8 *addr,
4278 					      u8 encr_type)
4279 {
4280 	struct mwl8k_cmd_update_encryption *cmd;
4281 	int rc;
4282 
4283 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4284 	if (cmd == NULL)
4285 		return -ENOMEM;
4286 
4287 	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4288 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4289 	cmd->action = cpu_to_le32(MWL8K_ENCR_ENABLE);
4290 	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4291 	cmd->encr_type = encr_type;
4292 
4293 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4294 	kfree(cmd);
4295 
4296 	return rc;
4297 }
4298 
mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key * cmd,u8 * addr,struct ieee80211_key_conf * key)4299 static int mwl8k_encryption_set_cmd_info(struct mwl8k_cmd_set_key *cmd,
4300 						u8 *addr,
4301 						struct ieee80211_key_conf *key)
4302 {
4303 	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_ENCRYPTION);
4304 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4305 	cmd->length = cpu_to_le16(sizeof(*cmd) -
4306 				offsetof(struct mwl8k_cmd_set_key, length));
4307 	cmd->key_id = cpu_to_le32(key->keyidx);
4308 	cmd->key_len = cpu_to_le16(key->keylen);
4309 	memcpy(cmd->mac_addr, addr, ETH_ALEN);
4310 
4311 	switch (key->cipher) {
4312 	case WLAN_CIPHER_SUITE_WEP40:
4313 	case WLAN_CIPHER_SUITE_WEP104:
4314 		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_WEP);
4315 		if (key->keyidx == 0)
4316 			cmd->key_info =	cpu_to_le32(MWL8K_KEY_FLAG_WEP_TXKEY);
4317 
4318 		break;
4319 	case WLAN_CIPHER_SUITE_TKIP:
4320 		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_TKIP);
4321 		cmd->key_info =	(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4322 			? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4323 			: cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4324 		cmd->key_info |= cpu_to_le32(MWL8K_KEY_FLAG_MICKEY_VALID
4325 						| MWL8K_KEY_FLAG_TSC_VALID);
4326 		break;
4327 	case WLAN_CIPHER_SUITE_CCMP:
4328 		cmd->key_type_id = cpu_to_le16(MWL8K_ALG_CCMP);
4329 		cmd->key_info =	(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4330 			? cpu_to_le32(MWL8K_KEY_FLAG_PAIRWISE)
4331 			: cpu_to_le32(MWL8K_KEY_FLAG_TXGROUPKEY);
4332 		break;
4333 	default:
4334 		return -ENOTSUPP;
4335 	}
4336 
4337 	return 0;
4338 }
4339 
mwl8k_cmd_encryption_set_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 * addr,struct ieee80211_key_conf * key)4340 static int mwl8k_cmd_encryption_set_key(struct ieee80211_hw *hw,
4341 						struct ieee80211_vif *vif,
4342 						u8 *addr,
4343 						struct ieee80211_key_conf *key)
4344 {
4345 	struct mwl8k_cmd_set_key *cmd;
4346 	int rc;
4347 	int keymlen;
4348 	u32 action;
4349 	u8 idx;
4350 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4351 
4352 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4353 	if (cmd == NULL)
4354 		return -ENOMEM;
4355 
4356 	rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4357 	if (rc < 0)
4358 		goto done;
4359 
4360 	idx = key->keyidx;
4361 
4362 	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4363 		action = MWL8K_ENCR_SET_KEY;
4364 	else
4365 		action = MWL8K_ENCR_SET_GROUP_KEY;
4366 
4367 	switch (key->cipher) {
4368 	case WLAN_CIPHER_SUITE_WEP40:
4369 	case WLAN_CIPHER_SUITE_WEP104:
4370 		if (!mwl8k_vif->wep_key_conf[idx].enabled) {
4371 			memcpy(mwl8k_vif->wep_key_conf[idx].key, key,
4372 						sizeof(*key) + key->keylen);
4373 			mwl8k_vif->wep_key_conf[idx].enabled = 1;
4374 		}
4375 
4376 		keymlen = key->keylen;
4377 		action = MWL8K_ENCR_SET_KEY;
4378 		break;
4379 	case WLAN_CIPHER_SUITE_TKIP:
4380 		keymlen = MAX_ENCR_KEY_LENGTH + 2 * MIC_KEY_LENGTH;
4381 		break;
4382 	case WLAN_CIPHER_SUITE_CCMP:
4383 		keymlen = key->keylen;
4384 		break;
4385 	default:
4386 		rc = -ENOTSUPP;
4387 		goto done;
4388 	}
4389 
4390 	memcpy(&cmd->tkip, key->key, keymlen);
4391 	cmd->action = cpu_to_le32(action);
4392 
4393 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4394 done:
4395 	kfree(cmd);
4396 
4397 	return rc;
4398 }
4399 
mwl8k_cmd_encryption_remove_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 * addr,struct ieee80211_key_conf * key)4400 static int mwl8k_cmd_encryption_remove_key(struct ieee80211_hw *hw,
4401 						struct ieee80211_vif *vif,
4402 						u8 *addr,
4403 						struct ieee80211_key_conf *key)
4404 {
4405 	struct mwl8k_cmd_set_key *cmd;
4406 	int rc;
4407 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4408 
4409 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4410 	if (cmd == NULL)
4411 		return -ENOMEM;
4412 
4413 	rc = mwl8k_encryption_set_cmd_info(cmd, addr, key);
4414 	if (rc < 0)
4415 		goto done;
4416 
4417 	if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4418 			key->cipher == WLAN_CIPHER_SUITE_WEP104)
4419 		mwl8k_vif->wep_key_conf[key->keyidx].enabled = 0;
4420 
4421 	cmd->action = cpu_to_le32(MWL8K_ENCR_REMOVE_KEY);
4422 
4423 	rc = mwl8k_post_pervif_cmd(hw, vif, &cmd->header);
4424 done:
4425 	kfree(cmd);
4426 
4427 	return rc;
4428 }
4429 
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)4430 static int mwl8k_set_key(struct ieee80211_hw *hw,
4431 			 enum set_key_cmd cmd_param,
4432 			 struct ieee80211_vif *vif,
4433 			 struct ieee80211_sta *sta,
4434 			 struct ieee80211_key_conf *key)
4435 {
4436 	int rc = 0;
4437 	u8 encr_type;
4438 	u8 *addr;
4439 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4440 	struct mwl8k_priv *priv = hw->priv;
4441 
4442 	if (vif->type == NL80211_IFTYPE_STATION && !priv->ap_fw)
4443 		return -EOPNOTSUPP;
4444 
4445 	if (sta == NULL)
4446 		addr = vif->addr;
4447 	else
4448 		addr = sta->addr;
4449 
4450 	if (cmd_param == SET_KEY) {
4451 		rc = mwl8k_cmd_encryption_set_key(hw, vif, addr, key);
4452 		if (rc)
4453 			goto out;
4454 
4455 		if ((key->cipher == WLAN_CIPHER_SUITE_WEP40)
4456 				|| (key->cipher == WLAN_CIPHER_SUITE_WEP104))
4457 			encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_WEP;
4458 		else
4459 			encr_type = MWL8K_UPDATE_ENCRYPTION_TYPE_MIXED;
4460 
4461 		rc = mwl8k_cmd_update_encryption_enable(hw, vif, addr,
4462 								encr_type);
4463 		if (rc)
4464 			goto out;
4465 
4466 		mwl8k_vif->is_hw_crypto_enabled = true;
4467 
4468 	} else {
4469 		rc = mwl8k_cmd_encryption_remove_key(hw, vif, addr, key);
4470 
4471 		if (rc)
4472 			goto out;
4473 	}
4474 out:
4475 	return rc;
4476 }
4477 
4478 /*
4479  * CMD_UPDATE_STADB.
4480  */
4481 struct ewc_ht_info {
4482 	__le16	control1;
4483 	__le16	control2;
4484 	__le16	control3;
4485 } __packed;
4486 
4487 struct peer_capability_info {
4488 	/* Peer type - AP vs. STA.  */
4489 	__u8	peer_type;
4490 
4491 	/* Basic 802.11 capabilities from assoc resp.  */
4492 	__le16	basic_caps;
4493 
4494 	/* Set if peer supports 802.11n high throughput (HT).  */
4495 	__u8	ht_support;
4496 
4497 	/* Valid if HT is supported.  */
4498 	__le16	ht_caps;
4499 	__u8	extended_ht_caps;
4500 	struct ewc_ht_info	ewc_info;
4501 
4502 	/* Legacy rate table. Intersection of our rates and peer rates.  */
4503 	__u8	legacy_rates[12];
4504 
4505 	/* HT rate table. Intersection of our rates and peer rates.  */
4506 	__u8	ht_rates[16];
4507 	__u8	pad[16];
4508 
4509 	/* If set, interoperability mode, no proprietary extensions.  */
4510 	__u8	interop;
4511 	__u8	pad2;
4512 	__u8	station_id;
4513 	__le16	amsdu_enabled;
4514 } __packed;
4515 
4516 struct mwl8k_cmd_update_stadb {
4517 	struct mwl8k_cmd_pkt_hdr header;
4518 
4519 	/* See STADB_ACTION_TYPE */
4520 	__le32	action;
4521 
4522 	/* Peer MAC address */
4523 	__u8	peer_addr[ETH_ALEN];
4524 
4525 	__le32	reserved;
4526 
4527 	/* Peer info - valid during add/update.  */
4528 	struct peer_capability_info	peer_info;
4529 } __packed;
4530 
4531 #define MWL8K_STA_DB_MODIFY_ENTRY	1
4532 #define MWL8K_STA_DB_DEL_ENTRY		2
4533 
4534 /* Peer Entry flags - used to define the type of the peer node */
4535 #define MWL8K_PEER_TYPE_ACCESSPOINT	2
4536 
mwl8k_cmd_update_stadb_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)4537 static int mwl8k_cmd_update_stadb_add(struct ieee80211_hw *hw,
4538 				      struct ieee80211_vif *vif,
4539 				      struct ieee80211_sta *sta)
4540 {
4541 	struct mwl8k_cmd_update_stadb *cmd;
4542 	struct peer_capability_info *p;
4543 	u32 rates;
4544 	int rc;
4545 
4546 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4547 	if (cmd == NULL)
4548 		return -ENOMEM;
4549 
4550 	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4551 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4552 	cmd->action = cpu_to_le32(MWL8K_STA_DB_MODIFY_ENTRY);
4553 	memcpy(cmd->peer_addr, sta->addr, ETH_ALEN);
4554 
4555 	p = &cmd->peer_info;
4556 	p->peer_type = MWL8K_PEER_TYPE_ACCESSPOINT;
4557 	p->basic_caps = cpu_to_le16(vif->bss_conf.assoc_capability);
4558 	p->ht_support = sta->deflink.ht_cap.ht_supported;
4559 	p->ht_caps = cpu_to_le16(sta->deflink.ht_cap.cap);
4560 	p->extended_ht_caps = (sta->deflink.ht_cap.ampdu_factor & 3) |
4561 		((sta->deflink.ht_cap.ampdu_density & 7) << 2);
4562 	if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
4563 		rates = sta->deflink.supp_rates[NL80211_BAND_2GHZ];
4564 	else
4565 		rates = sta->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
4566 	legacy_rate_mask_to_array(p->legacy_rates, rates);
4567 	memcpy(p->ht_rates, &sta->deflink.ht_cap.mcs, 16);
4568 	p->interop = 1;
4569 	p->amsdu_enabled = 0;
4570 
4571 	rc = mwl8k_post_cmd(hw, &cmd->header);
4572 	if (!rc)
4573 		rc = p->station_id;
4574 	kfree(cmd);
4575 
4576 	return rc;
4577 }
4578 
mwl8k_cmd_update_stadb_del(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u8 * addr)4579 static int mwl8k_cmd_update_stadb_del(struct ieee80211_hw *hw,
4580 				      struct ieee80211_vif *vif, u8 *addr)
4581 {
4582 	struct mwl8k_cmd_update_stadb *cmd;
4583 	int rc;
4584 
4585 	cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
4586 	if (cmd == NULL)
4587 		return -ENOMEM;
4588 
4589 	cmd->header.code = cpu_to_le16(MWL8K_CMD_UPDATE_STADB);
4590 	cmd->header.length = cpu_to_le16(sizeof(*cmd));
4591 	cmd->action = cpu_to_le32(MWL8K_STA_DB_DEL_ENTRY);
4592 	memcpy(cmd->peer_addr, addr, ETH_ALEN);
4593 
4594 	rc = mwl8k_post_cmd(hw, &cmd->header);
4595 	kfree(cmd);
4596 
4597 	return rc;
4598 }
4599 
4600 
4601 /*
4602  * Interrupt handling.
4603  */
mwl8k_interrupt(int irq,void * dev_id)4604 static irqreturn_t mwl8k_interrupt(int irq, void *dev_id)
4605 {
4606 	struct ieee80211_hw *hw = dev_id;
4607 	struct mwl8k_priv *priv = hw->priv;
4608 	u32 status;
4609 
4610 	status = ioread32(priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4611 	if (!status)
4612 		return IRQ_NONE;
4613 
4614 	if (status & MWL8K_A2H_INT_TX_DONE) {
4615 		status &= ~MWL8K_A2H_INT_TX_DONE;
4616 		tasklet_schedule(&priv->poll_tx_task);
4617 	}
4618 
4619 	if (status & MWL8K_A2H_INT_RX_READY) {
4620 		status &= ~MWL8K_A2H_INT_RX_READY;
4621 		tasklet_schedule(&priv->poll_rx_task);
4622 	}
4623 
4624 	if (status & MWL8K_A2H_INT_BA_WATCHDOG) {
4625 		iowrite32(~MWL8K_A2H_INT_BA_WATCHDOG,
4626 			  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4627 
4628 		atomic_inc(&priv->watchdog_event_pending);
4629 		status &= ~MWL8K_A2H_INT_BA_WATCHDOG;
4630 		ieee80211_queue_work(hw, &priv->watchdog_ba_handle);
4631 	}
4632 
4633 	if (status)
4634 		iowrite32(~status, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4635 
4636 	if (status & MWL8K_A2H_INT_OPC_DONE) {
4637 		if (priv->hostcmd_wait != NULL)
4638 			complete(priv->hostcmd_wait);
4639 	}
4640 
4641 	if (status & MWL8K_A2H_INT_QUEUE_EMPTY) {
4642 		if (!mutex_is_locked(&priv->fw_mutex) &&
4643 		    priv->radio_on && priv->pending_tx_pkts)
4644 			mwl8k_tx_start(priv);
4645 	}
4646 
4647 	return IRQ_HANDLED;
4648 }
4649 
mwl8k_tx_poll(struct tasklet_struct * t)4650 static void mwl8k_tx_poll(struct tasklet_struct *t)
4651 {
4652 	struct mwl8k_priv *priv = from_tasklet(priv, t, poll_tx_task);
4653 	struct ieee80211_hw *hw = pci_get_drvdata(priv->pdev);
4654 	int limit;
4655 	int i;
4656 
4657 	limit = 32;
4658 
4659 	spin_lock(&priv->tx_lock);
4660 
4661 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
4662 		limit -= mwl8k_txq_reclaim(hw, i, limit, 0);
4663 
4664 	if (!priv->pending_tx_pkts && priv->tx_wait != NULL) {
4665 		complete(priv->tx_wait);
4666 		priv->tx_wait = NULL;
4667 	}
4668 
4669 	spin_unlock(&priv->tx_lock);
4670 
4671 	if (limit) {
4672 		writel(~MWL8K_A2H_INT_TX_DONE,
4673 		       priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4674 	} else {
4675 		tasklet_schedule(&priv->poll_tx_task);
4676 	}
4677 }
4678 
mwl8k_rx_poll(struct tasklet_struct * t)4679 static void mwl8k_rx_poll(struct tasklet_struct *t)
4680 {
4681 	struct mwl8k_priv *priv = from_tasklet(priv, t, poll_rx_task);
4682 	struct ieee80211_hw *hw = pci_get_drvdata(priv->pdev);
4683 	int limit;
4684 
4685 	limit = 32;
4686 	limit -= rxq_process(hw, 0, limit);
4687 	limit -= rxq_refill(hw, 0, limit);
4688 
4689 	if (limit) {
4690 		writel(~MWL8K_A2H_INT_RX_READY,
4691 		       priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
4692 	} else {
4693 		tasklet_schedule(&priv->poll_rx_task);
4694 	}
4695 }
4696 
4697 
4698 /*
4699  * Core driver operations.
4700  */
mwl8k_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)4701 static void mwl8k_tx(struct ieee80211_hw *hw,
4702 		     struct ieee80211_tx_control *control,
4703 		     struct sk_buff *skb)
4704 {
4705 	struct mwl8k_priv *priv = hw->priv;
4706 	int index = skb_get_queue_mapping(skb);
4707 
4708 	if (!priv->radio_on) {
4709 		wiphy_debug(hw->wiphy,
4710 			    "dropped TX frame since radio disabled\n");
4711 		dev_kfree_skb(skb);
4712 		return;
4713 	}
4714 
4715 	mwl8k_txq_xmit(hw, index, control->sta, skb);
4716 }
4717 
mwl8k_start(struct ieee80211_hw * hw)4718 static int mwl8k_start(struct ieee80211_hw *hw)
4719 {
4720 	struct mwl8k_priv *priv = hw->priv;
4721 	int rc;
4722 
4723 	rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
4724 			 IRQF_SHARED, MWL8K_NAME, hw);
4725 	if (rc) {
4726 		priv->irq = -1;
4727 		wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
4728 		return -EIO;
4729 	}
4730 	priv->irq = priv->pdev->irq;
4731 
4732 	/* Enable TX reclaim and RX tasklets.  */
4733 	tasklet_enable(&priv->poll_tx_task);
4734 	tasklet_enable(&priv->poll_rx_task);
4735 
4736 	/* Enable interrupts */
4737 	iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4738 	iowrite32(MWL8K_A2H_EVENTS,
4739 		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
4740 
4741 	rc = mwl8k_fw_lock(hw);
4742 	if (!rc) {
4743 		rc = mwl8k_cmd_radio_enable(hw);
4744 
4745 		if (!priv->ap_fw) {
4746 			if (!rc)
4747 				rc = mwl8k_cmd_enable_sniffer(hw, 0);
4748 
4749 			if (!rc)
4750 				rc = mwl8k_cmd_set_pre_scan(hw);
4751 
4752 			if (!rc)
4753 				rc = mwl8k_cmd_set_post_scan(hw,
4754 						"\x00\x00\x00\x00\x00\x00");
4755 		}
4756 
4757 		if (!rc)
4758 			rc = mwl8k_cmd_set_rateadapt_mode(hw, 0);
4759 
4760 		if (!rc)
4761 			rc = mwl8k_cmd_set_wmm_mode(hw, 0);
4762 
4763 		mwl8k_fw_unlock(hw);
4764 	}
4765 
4766 	if (rc) {
4767 		iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4768 		free_irq(priv->pdev->irq, hw);
4769 		priv->irq = -1;
4770 		tasklet_disable(&priv->poll_tx_task);
4771 		tasklet_disable(&priv->poll_rx_task);
4772 	} else {
4773 		ieee80211_wake_queues(hw);
4774 	}
4775 
4776 	return rc;
4777 }
4778 
mwl8k_stop(struct ieee80211_hw * hw,bool suspend)4779 static void mwl8k_stop(struct ieee80211_hw *hw, bool suspend)
4780 {
4781 	struct mwl8k_priv *priv = hw->priv;
4782 	int i;
4783 
4784 	if (!priv->hw_restart_in_progress)
4785 		mwl8k_cmd_radio_disable(hw);
4786 
4787 	ieee80211_stop_queues(hw);
4788 
4789 	/* Disable interrupts */
4790 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
4791 	if (priv->irq != -1) {
4792 		free_irq(priv->pdev->irq, hw);
4793 		priv->irq = -1;
4794 	}
4795 
4796 	/* Stop finalize join worker */
4797 	cancel_work_sync(&priv->finalize_join_worker);
4798 	cancel_work_sync(&priv->watchdog_ba_handle);
4799 	if (priv->beacon_skb != NULL)
4800 		dev_kfree_skb(priv->beacon_skb);
4801 
4802 	/* Stop TX reclaim and RX tasklets.  */
4803 	tasklet_disable(&priv->poll_tx_task);
4804 	tasklet_disable(&priv->poll_rx_task);
4805 
4806 	/* Return all skbs to mac80211 */
4807 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
4808 		mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
4809 }
4810 
4811 static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image);
4812 
mwl8k_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4813 static int mwl8k_add_interface(struct ieee80211_hw *hw,
4814 			       struct ieee80211_vif *vif)
4815 {
4816 	struct mwl8k_priv *priv = hw->priv;
4817 	struct mwl8k_vif *mwl8k_vif;
4818 	u32 macids_supported;
4819 	int macid, rc;
4820 	struct mwl8k_device_info *di;
4821 
4822 	/*
4823 	 * Reject interface creation if sniffer mode is active, as
4824 	 * STA operation is mutually exclusive with hardware sniffer
4825 	 * mode.  (Sniffer mode is only used on STA firmware.)
4826 	 */
4827 	if (priv->sniffer_enabled) {
4828 		wiphy_info(hw->wiphy,
4829 			   "unable to create STA interface because sniffer mode is enabled\n");
4830 		return -EINVAL;
4831 	}
4832 
4833 	di = priv->device_info;
4834 	switch (vif->type) {
4835 	case NL80211_IFTYPE_AP:
4836 		if (!priv->ap_fw && di->fw_image_ap) {
4837 			/* we must load the ap fw to meet this request */
4838 			if (!list_empty(&priv->vif_list))
4839 				return -EBUSY;
4840 			rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4841 			if (rc)
4842 				return rc;
4843 		}
4844 		macids_supported = priv->ap_macids_supported;
4845 		break;
4846 	case NL80211_IFTYPE_STATION:
4847 		if (priv->ap_fw && di->fw_image_sta) {
4848 			if (!list_empty(&priv->vif_list)) {
4849 				wiphy_warn(hw->wiphy, "AP interface is running.\n"
4850 					   "Adding STA interface for WDS");
4851 			} else {
4852 				/* we must load the sta fw to
4853 				 * meet this request.
4854 				 */
4855 				rc = mwl8k_reload_firmware(hw,
4856 							   di->fw_image_sta);
4857 				if (rc)
4858 					return rc;
4859 			}
4860 		}
4861 		macids_supported = priv->sta_macids_supported;
4862 		break;
4863 	default:
4864 		return -EINVAL;
4865 	}
4866 
4867 	macid = ffs(macids_supported & ~priv->macids_used);
4868 	if (!macid--)
4869 		return -EBUSY;
4870 
4871 	/* Setup driver private area. */
4872 	mwl8k_vif = MWL8K_VIF(vif);
4873 	memset(mwl8k_vif, 0, sizeof(*mwl8k_vif));
4874 	mwl8k_vif->vif = vif;
4875 	mwl8k_vif->macid = macid;
4876 	mwl8k_vif->seqno = 0;
4877 	memcpy(mwl8k_vif->bssid, vif->addr, ETH_ALEN);
4878 	mwl8k_vif->is_hw_crypto_enabled = false;
4879 
4880 	/* Set the mac address.  */
4881 	mwl8k_cmd_set_mac_addr(hw, vif, vif->addr);
4882 
4883 	if (vif->type == NL80211_IFTYPE_AP)
4884 		mwl8k_cmd_set_new_stn_add_self(hw, vif);
4885 
4886 	priv->macids_used |= 1 << mwl8k_vif->macid;
4887 	list_add_tail(&mwl8k_vif->list, &priv->vif_list);
4888 
4889 	return 0;
4890 }
4891 
mwl8k_remove_vif(struct mwl8k_priv * priv,struct mwl8k_vif * vif)4892 static void mwl8k_remove_vif(struct mwl8k_priv *priv, struct mwl8k_vif *vif)
4893 {
4894 	/* Has ieee80211_restart_hw re-added the removed interfaces? */
4895 	if (!priv->macids_used)
4896 		return;
4897 
4898 	priv->macids_used &= ~(1 << vif->macid);
4899 	list_del(&vif->list);
4900 }
4901 
mwl8k_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)4902 static void mwl8k_remove_interface(struct ieee80211_hw *hw,
4903 				   struct ieee80211_vif *vif)
4904 {
4905 	struct mwl8k_priv *priv = hw->priv;
4906 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
4907 
4908 	if (vif->type == NL80211_IFTYPE_AP)
4909 		mwl8k_cmd_set_new_stn_del(hw, vif, vif->addr);
4910 
4911 	mwl8k_cmd_del_mac_addr(hw, vif, vif->addr);
4912 
4913 	mwl8k_remove_vif(priv, mwl8k_vif);
4914 }
4915 
mwl8k_hw_restart_work(struct work_struct * work)4916 static void mwl8k_hw_restart_work(struct work_struct *work)
4917 {
4918 	struct mwl8k_priv *priv =
4919 		container_of(work, struct mwl8k_priv, fw_reload);
4920 	struct ieee80211_hw *hw = priv->hw;
4921 	struct mwl8k_device_info *di;
4922 	int rc;
4923 
4924 	/* If some command is waiting for a response, clear it */
4925 	if (priv->hostcmd_wait != NULL) {
4926 		complete(priv->hostcmd_wait);
4927 		priv->hostcmd_wait = NULL;
4928 	}
4929 
4930 	priv->hw_restart_owner = current;
4931 	di = priv->device_info;
4932 	mwl8k_fw_lock(hw);
4933 
4934 	if (priv->ap_fw)
4935 		rc = mwl8k_reload_firmware(hw, di->fw_image_ap);
4936 	else
4937 		rc = mwl8k_reload_firmware(hw, di->fw_image_sta);
4938 
4939 	if (rc)
4940 		goto fail;
4941 
4942 	priv->hw_restart_owner = NULL;
4943 	priv->hw_restart_in_progress = false;
4944 
4945 	/*
4946 	 * This unlock will wake up the queues and
4947 	 * also opens the command path for other
4948 	 * commands
4949 	 */
4950 	mwl8k_fw_unlock(hw);
4951 
4952 	ieee80211_restart_hw(hw);
4953 
4954 	wiphy_err(hw->wiphy, "Firmware restarted successfully\n");
4955 
4956 	return;
4957 fail:
4958 	mwl8k_fw_unlock(hw);
4959 
4960 	wiphy_err(hw->wiphy, "Firmware restart failed\n");
4961 }
4962 
mwl8k_config(struct ieee80211_hw * hw,int radio_idx,u32 changed)4963 static int mwl8k_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
4964 {
4965 	struct ieee80211_conf *conf = &hw->conf;
4966 	struct mwl8k_priv *priv = hw->priv;
4967 	int rc;
4968 
4969 	rc = mwl8k_fw_lock(hw);
4970 	if (rc)
4971 		return rc;
4972 
4973 	if (conf->flags & IEEE80211_CONF_IDLE)
4974 		rc = mwl8k_cmd_radio_disable(hw);
4975 	else
4976 		rc = mwl8k_cmd_radio_enable(hw);
4977 	if (rc)
4978 		goto out;
4979 
4980 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
4981 		rc = mwl8k_cmd_set_rf_channel(hw, conf);
4982 		if (rc)
4983 			goto out;
4984 	}
4985 
4986 	if (conf->power_level > 18)
4987 		conf->power_level = 18;
4988 
4989 	if (priv->ap_fw) {
4990 
4991 		if (conf->flags & IEEE80211_CONF_CHANGE_POWER) {
4992 			rc = mwl8k_cmd_tx_power(hw, conf, conf->power_level);
4993 			if (rc)
4994 				goto out;
4995 		}
4996 
4997 
4998 	} else {
4999 		rc = mwl8k_cmd_rf_tx_power(hw, conf->power_level);
5000 		if (rc)
5001 			goto out;
5002 		rc = mwl8k_cmd_mimo_config(hw, 0x7, 0x7);
5003 	}
5004 
5005 out:
5006 	mwl8k_fw_unlock(hw);
5007 
5008 	return rc;
5009 }
5010 
5011 static void
mwl8k_bss_info_changed_sta(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)5012 mwl8k_bss_info_changed_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5013 			   struct ieee80211_bss_conf *info, u32 changed)
5014 {
5015 	struct mwl8k_priv *priv = hw->priv;
5016 	u32 ap_legacy_rates = 0;
5017 	u8 ap_mcs_rates[16];
5018 	int rc;
5019 
5020 	if (mwl8k_fw_lock(hw))
5021 		return;
5022 
5023 	/*
5024 	 * No need to capture a beacon if we're no longer associated.
5025 	 */
5026 	if ((changed & BSS_CHANGED_ASSOC) && !vif->cfg.assoc)
5027 		priv->capture_beacon = false;
5028 
5029 	/*
5030 	 * Get the AP's legacy and MCS rates.
5031 	 */
5032 	if (vif->cfg.assoc) {
5033 		struct ieee80211_sta *ap;
5034 
5035 		rcu_read_lock();
5036 
5037 		ap = ieee80211_find_sta(vif, vif->bss_conf.bssid);
5038 		if (ap == NULL) {
5039 			rcu_read_unlock();
5040 			goto out;
5041 		}
5042 
5043 		if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ) {
5044 			ap_legacy_rates = ap->deflink.supp_rates[NL80211_BAND_2GHZ];
5045 		} else {
5046 			ap_legacy_rates =
5047 				ap->deflink.supp_rates[NL80211_BAND_5GHZ] << 5;
5048 		}
5049 		memcpy(ap_mcs_rates, &ap->deflink.ht_cap.mcs, 16);
5050 
5051 		rcu_read_unlock();
5052 
5053 		if (changed & BSS_CHANGED_ASSOC) {
5054 			if (!priv->ap_fw) {
5055 				rc = mwl8k_cmd_set_rate(hw, vif,
5056 							ap_legacy_rates,
5057 							ap_mcs_rates);
5058 				if (rc)
5059 					goto out;
5060 
5061 				rc = mwl8k_cmd_use_fixed_rate_sta(hw);
5062 				if (rc)
5063 					goto out;
5064 			} else {
5065 				int idx;
5066 				int rate;
5067 
5068 				/* Use AP firmware specific rate command.
5069 				 */
5070 				idx = ffs(vif->bss_conf.basic_rates);
5071 				if (idx)
5072 					idx--;
5073 
5074 				if (hw->conf.chandef.chan->band ==
5075 				    NL80211_BAND_2GHZ)
5076 					rate = mwl8k_rates_24[idx].hw_value;
5077 				else
5078 					rate = mwl8k_rates_50[idx].hw_value;
5079 
5080 				mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
5081 			}
5082 		}
5083 	}
5084 
5085 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5086 		rc = mwl8k_set_radio_preamble(hw,
5087 				vif->bss_conf.use_short_preamble);
5088 		if (rc)
5089 			goto out;
5090 	}
5091 
5092 	if ((changed & BSS_CHANGED_ERP_SLOT) && !priv->ap_fw)  {
5093 		rc = mwl8k_cmd_set_slot(hw, vif->bss_conf.use_short_slot);
5094 		if (rc)
5095 			goto out;
5096 	}
5097 
5098 	if (vif->cfg.assoc && !priv->ap_fw &&
5099 	    (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_ERP_CTS_PROT |
5100 			BSS_CHANGED_HT))) {
5101 		rc = mwl8k_cmd_set_aid(hw, vif, ap_legacy_rates);
5102 		if (rc)
5103 			goto out;
5104 	}
5105 
5106 	if (vif->cfg.assoc &&
5107 	    (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INT))) {
5108 		/*
5109 		 * Finalize the join.  Tell rx handler to process
5110 		 * next beacon from our BSSID.
5111 		 */
5112 		memcpy(priv->capture_bssid, vif->bss_conf.bssid, ETH_ALEN);
5113 		priv->capture_beacon = true;
5114 	}
5115 
5116 out:
5117 	mwl8k_fw_unlock(hw);
5118 }
5119 
5120 static void
mwl8k_bss_info_changed_ap(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u32 changed)5121 mwl8k_bss_info_changed_ap(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5122 			  struct ieee80211_bss_conf *info, u32 changed)
5123 {
5124 	int rc;
5125 
5126 	if (mwl8k_fw_lock(hw))
5127 		return;
5128 
5129 	if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5130 		rc = mwl8k_set_radio_preamble(hw,
5131 				vif->bss_conf.use_short_preamble);
5132 		if (rc)
5133 			goto out;
5134 	}
5135 
5136 	if (changed & BSS_CHANGED_BASIC_RATES) {
5137 		int idx;
5138 		int rate;
5139 
5140 		/*
5141 		 * Use lowest supported basic rate for multicasts
5142 		 * and management frames (such as probe responses --
5143 		 * beacons will always go out at 1 Mb/s).
5144 		 */
5145 		idx = ffs(vif->bss_conf.basic_rates);
5146 		if (idx)
5147 			idx--;
5148 
5149 		if (hw->conf.chandef.chan->band == NL80211_BAND_2GHZ)
5150 			rate = mwl8k_rates_24[idx].hw_value;
5151 		else
5152 			rate = mwl8k_rates_50[idx].hw_value;
5153 
5154 		mwl8k_cmd_use_fixed_rate_ap(hw, rate, rate);
5155 	}
5156 
5157 	if (changed & (BSS_CHANGED_BEACON_INT | BSS_CHANGED_BEACON)) {
5158 		struct sk_buff *skb;
5159 
5160 		skb = ieee80211_beacon_get(hw, vif, 0);
5161 		if (skb != NULL) {
5162 			mwl8k_cmd_set_beacon(hw, vif, skb->data, skb->len);
5163 			kfree_skb(skb);
5164 		}
5165 	}
5166 
5167 	if (changed & BSS_CHANGED_BEACON_ENABLED)
5168 		mwl8k_cmd_bss_start(hw, vif, info->enable_beacon);
5169 
5170 out:
5171 	mwl8k_fw_unlock(hw);
5172 }
5173 
5174 static void
mwl8k_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * info,u64 changed)5175 mwl8k_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5176 		       struct ieee80211_bss_conf *info, u64 changed)
5177 {
5178 	if (vif->type == NL80211_IFTYPE_STATION)
5179 		mwl8k_bss_info_changed_sta(hw, vif, info, changed);
5180 	if (vif->type == NL80211_IFTYPE_AP)
5181 		mwl8k_bss_info_changed_ap(hw, vif, info, changed);
5182 }
5183 
mwl8k_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)5184 static u64 mwl8k_prepare_multicast(struct ieee80211_hw *hw,
5185 				   struct netdev_hw_addr_list *mc_list)
5186 {
5187 	struct mwl8k_cmd_pkt_hdr *cmd;
5188 
5189 	/*
5190 	 * Synthesize and return a command packet that programs the
5191 	 * hardware multicast address filter.  At this point we don't
5192 	 * know whether FIF_ALLMULTI is being requested, but if it is,
5193 	 * we'll end up throwing this packet away and creating a new
5194 	 * one in mwl8k_configure_filter().
5195 	 */
5196 	cmd = __mwl8k_cmd_mac_multicast_adr(hw, 0, mc_list);
5197 
5198 	return (unsigned long)cmd;
5199 }
5200 
5201 static int
mwl8k_configure_filter_sniffer(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags)5202 mwl8k_configure_filter_sniffer(struct ieee80211_hw *hw,
5203 			       unsigned int changed_flags,
5204 			       unsigned int *total_flags)
5205 {
5206 	struct mwl8k_priv *priv = hw->priv;
5207 
5208 	/*
5209 	 * Hardware sniffer mode is mutually exclusive with STA
5210 	 * operation, so refuse to enable sniffer mode if a STA
5211 	 * interface is active.
5212 	 */
5213 	if (!list_empty(&priv->vif_list)) {
5214 		if (net_ratelimit())
5215 			wiphy_info(hw->wiphy,
5216 				   "not enabling sniffer mode because STA interface is active\n");
5217 		return 0;
5218 	}
5219 
5220 	if (!priv->sniffer_enabled) {
5221 		if (mwl8k_cmd_enable_sniffer(hw, 1))
5222 			return 0;
5223 		priv->sniffer_enabled = true;
5224 	}
5225 
5226 	*total_flags &=	FIF_ALLMULTI |
5227 			FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL |
5228 			FIF_OTHER_BSS;
5229 
5230 	return 1;
5231 }
5232 
mwl8k_first_vif(struct mwl8k_priv * priv)5233 static struct mwl8k_vif *mwl8k_first_vif(struct mwl8k_priv *priv)
5234 {
5235 	if (!list_empty(&priv->vif_list))
5236 		return list_entry(priv->vif_list.next, struct mwl8k_vif, list);
5237 
5238 	return NULL;
5239 }
5240 
mwl8k_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * total_flags,u64 multicast)5241 static void mwl8k_configure_filter(struct ieee80211_hw *hw,
5242 				   unsigned int changed_flags,
5243 				   unsigned int *total_flags,
5244 				   u64 multicast)
5245 {
5246 	struct mwl8k_priv *priv = hw->priv;
5247 	struct mwl8k_cmd_pkt_hdr *cmd = (void *)(unsigned long)multicast;
5248 
5249 	/*
5250 	 * AP firmware doesn't allow fine-grained control over
5251 	 * the receive filter.
5252 	 */
5253 	if (priv->ap_fw) {
5254 		*total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5255 		kfree(cmd);
5256 		return;
5257 	}
5258 
5259 	/*
5260 	 * Enable hardware sniffer mode if FIF_CONTROL or
5261 	 * FIF_OTHER_BSS is requested.
5262 	 */
5263 	if (*total_flags & (FIF_CONTROL | FIF_OTHER_BSS) &&
5264 	    mwl8k_configure_filter_sniffer(hw, changed_flags, total_flags)) {
5265 		kfree(cmd);
5266 		return;
5267 	}
5268 
5269 	/* Clear unsupported feature flags */
5270 	*total_flags &= FIF_ALLMULTI | FIF_BCN_PRBRESP_PROMISC;
5271 
5272 	if (mwl8k_fw_lock(hw)) {
5273 		kfree(cmd);
5274 		return;
5275 	}
5276 
5277 	if (priv->sniffer_enabled) {
5278 		mwl8k_cmd_enable_sniffer(hw, 0);
5279 		priv->sniffer_enabled = false;
5280 	}
5281 
5282 	if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
5283 		if (*total_flags & FIF_BCN_PRBRESP_PROMISC) {
5284 			/*
5285 			 * Disable the BSS filter.
5286 			 */
5287 			mwl8k_cmd_set_pre_scan(hw);
5288 		} else {
5289 			struct mwl8k_vif *mwl8k_vif;
5290 			const u8 *bssid;
5291 
5292 			/*
5293 			 * Enable the BSS filter.
5294 			 *
5295 			 * If there is an active STA interface, use that
5296 			 * interface's BSSID, otherwise use a dummy one
5297 			 * (where the OUI part needs to be nonzero for
5298 			 * the BSSID to be accepted by POST_SCAN).
5299 			 */
5300 			mwl8k_vif = mwl8k_first_vif(priv);
5301 			if (mwl8k_vif != NULL)
5302 				bssid = mwl8k_vif->vif->bss_conf.bssid;
5303 			else
5304 				bssid = "\x01\x00\x00\x00\x00\x00";
5305 
5306 			mwl8k_cmd_set_post_scan(hw, bssid);
5307 		}
5308 	}
5309 
5310 	/*
5311 	 * If FIF_ALLMULTI is being requested, throw away the command
5312 	 * packet that ->prepare_multicast() built and replace it with
5313 	 * a command packet that enables reception of all multicast
5314 	 * packets.
5315 	 */
5316 	if (*total_flags & FIF_ALLMULTI) {
5317 		kfree(cmd);
5318 		cmd = __mwl8k_cmd_mac_multicast_adr(hw, 1, NULL);
5319 	}
5320 
5321 	if (cmd != NULL) {
5322 		mwl8k_post_cmd(hw, cmd);
5323 		kfree(cmd);
5324 	}
5325 
5326 	mwl8k_fw_unlock(hw);
5327 }
5328 
mwl8k_set_rts_threshold(struct ieee80211_hw * hw,int radio_idx,u32 value)5329 static int mwl8k_set_rts_threshold(struct ieee80211_hw *hw, int radio_idx,
5330 				   u32 value)
5331 {
5332 	return mwl8k_cmd_set_rts_threshold(hw, radio_idx, value);
5333 }
5334 
mwl8k_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)5335 static int mwl8k_sta_remove(struct ieee80211_hw *hw,
5336 			    struct ieee80211_vif *vif,
5337 			    struct ieee80211_sta *sta)
5338 {
5339 	struct mwl8k_priv *priv = hw->priv;
5340 
5341 	if (priv->ap_fw)
5342 		return mwl8k_cmd_set_new_stn_del(hw, vif, sta->addr);
5343 	else
5344 		return mwl8k_cmd_update_stadb_del(hw, vif, sta->addr);
5345 }
5346 
mwl8k_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)5347 static int mwl8k_sta_add(struct ieee80211_hw *hw,
5348 			 struct ieee80211_vif *vif,
5349 			 struct ieee80211_sta *sta)
5350 {
5351 	struct mwl8k_priv *priv = hw->priv;
5352 	int ret;
5353 	int i;
5354 	struct mwl8k_vif *mwl8k_vif = MWL8K_VIF(vif);
5355 	struct ieee80211_key_conf *key;
5356 
5357 	if (!priv->ap_fw) {
5358 		ret = mwl8k_cmd_update_stadb_add(hw, vif, sta);
5359 		if (ret >= 0) {
5360 			MWL8K_STA(sta)->peer_id = ret;
5361 			if (sta->deflink.ht_cap.ht_supported)
5362 				MWL8K_STA(sta)->is_ampdu_allowed = true;
5363 			ret = 0;
5364 		}
5365 
5366 	} else {
5367 		ret = mwl8k_cmd_set_new_stn_add(hw, vif, sta);
5368 	}
5369 
5370 	for (i = 0; i < NUM_WEP_KEYS; i++) {
5371 		key = IEEE80211_KEY_CONF(mwl8k_vif->wep_key_conf[i].key);
5372 		if (mwl8k_vif->wep_key_conf[i].enabled)
5373 			mwl8k_set_key(hw, SET_KEY, vif, sta, key);
5374 	}
5375 	return ret;
5376 }
5377 
mwl8k_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * params)5378 static int mwl8k_conf_tx(struct ieee80211_hw *hw,
5379 			 struct ieee80211_vif *vif,
5380 			 unsigned int link_id, u16 queue,
5381 			 const struct ieee80211_tx_queue_params *params)
5382 {
5383 	struct mwl8k_priv *priv = hw->priv;
5384 	int rc;
5385 
5386 	rc = mwl8k_fw_lock(hw);
5387 	if (!rc) {
5388 		BUG_ON(queue > MWL8K_TX_WMM_QUEUES - 1);
5389 		memcpy(&priv->wmm_params[queue], params, sizeof(*params));
5390 
5391 		if (!priv->wmm_enabled)
5392 			rc = mwl8k_cmd_set_wmm_mode(hw, 1);
5393 
5394 		if (!rc) {
5395 			int q = MWL8K_TX_WMM_QUEUES - 1 - queue;
5396 			rc = mwl8k_cmd_set_edca_params(hw, q,
5397 						       params->cw_min,
5398 						       params->cw_max,
5399 						       params->aifs,
5400 						       params->txop);
5401 		}
5402 
5403 		mwl8k_fw_unlock(hw);
5404 	}
5405 
5406 	return rc;
5407 }
5408 
mwl8k_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)5409 static int mwl8k_get_stats(struct ieee80211_hw *hw,
5410 			   struct ieee80211_low_level_stats *stats)
5411 {
5412 	return mwl8k_cmd_get_stat(hw, stats);
5413 }
5414 
mwl8k_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)5415 static int mwl8k_get_survey(struct ieee80211_hw *hw, int idx,
5416 				struct survey_info *survey)
5417 {
5418 	struct mwl8k_priv *priv = hw->priv;
5419 	struct ieee80211_conf *conf = &hw->conf;
5420 	struct ieee80211_supported_band *sband;
5421 
5422 	if (priv->ap_fw) {
5423 		sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
5424 
5425 		if (sband && idx >= sband->n_channels) {
5426 			idx -= sband->n_channels;
5427 			sband = NULL;
5428 		}
5429 
5430 		if (!sband)
5431 			sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
5432 
5433 		if (!sband || idx >= sband->n_channels)
5434 			return -ENOENT;
5435 
5436 		memcpy(survey, &priv->survey[idx], sizeof(*survey));
5437 		survey->channel = &sband->channels[idx];
5438 
5439 		return 0;
5440 	}
5441 
5442 	if (idx != 0)
5443 		return -ENOENT;
5444 
5445 	survey->channel = conf->chandef.chan;
5446 	survey->filled = SURVEY_INFO_NOISE_DBM;
5447 	survey->noise = priv->noise;
5448 
5449 	return 0;
5450 }
5451 
5452 #define MAX_AMPDU_ATTEMPTS 5
5453 
5454 static int
mwl8k_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)5455 mwl8k_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5456 		   struct ieee80211_ampdu_params *params)
5457 {
5458 	struct ieee80211_sta *sta = params->sta;
5459 	enum ieee80211_ampdu_mlme_action action = params->action;
5460 	u16 tid = params->tid;
5461 	u16 *ssn = &params->ssn;
5462 	u8 buf_size = params->buf_size;
5463 	int i, rc = 0;
5464 	struct mwl8k_priv *priv = hw->priv;
5465 	struct mwl8k_ampdu_stream *stream;
5466 	u8 *addr = sta->addr, idx;
5467 	struct mwl8k_sta *sta_info = MWL8K_STA(sta);
5468 
5469 	if (!ieee80211_hw_check(hw, AMPDU_AGGREGATION))
5470 		return -ENOTSUPP;
5471 
5472 	spin_lock(&priv->stream_lock);
5473 	stream = mwl8k_lookup_stream(hw, addr, tid);
5474 
5475 	switch (action) {
5476 	case IEEE80211_AMPDU_RX_START:
5477 	case IEEE80211_AMPDU_RX_STOP:
5478 		break;
5479 	case IEEE80211_AMPDU_TX_START:
5480 		/* By the time we get here the hw queues may contain outgoing
5481 		 * packets for this RA/TID that are not part of this BA
5482 		 * session.  The hw will assign sequence numbers to these
5483 		 * packets as they go out.  So if we query the hw for its next
5484 		 * sequence number and use that for the SSN here, it may end up
5485 		 * being wrong, which will lead to sequence number mismatch at
5486 		 * the recipient.  To avoid this, we reset the sequence number
5487 		 * to O for the first MPDU in this BA stream.
5488 		 */
5489 		*ssn = 0;
5490 		if (stream == NULL) {
5491 			/* This means that somebody outside this driver called
5492 			 * ieee80211_start_tx_ba_session.  This is unexpected
5493 			 * because we do our own rate control.  Just warn and
5494 			 * move on.
5495 			 */
5496 			wiphy_warn(hw->wiphy, "Unexpected call to %s.  "
5497 				   "Proceeding anyway.\n", __func__);
5498 			stream = mwl8k_add_stream(hw, sta, tid);
5499 		}
5500 		if (stream == NULL) {
5501 			wiphy_debug(hw->wiphy, "no free AMPDU streams\n");
5502 			rc = -EBUSY;
5503 			break;
5504 		}
5505 		stream->state = AMPDU_STREAM_IN_PROGRESS;
5506 
5507 		/* Release the lock before we do the time consuming stuff */
5508 		spin_unlock(&priv->stream_lock);
5509 		for (i = 0; i < MAX_AMPDU_ATTEMPTS; i++) {
5510 
5511 			/* Check if link is still valid */
5512 			if (!sta_info->is_ampdu_allowed) {
5513 				spin_lock(&priv->stream_lock);
5514 				mwl8k_remove_stream(hw, stream);
5515 				spin_unlock(&priv->stream_lock);
5516 				return -EBUSY;
5517 			}
5518 
5519 			rc = mwl8k_check_ba(hw, stream, vif);
5520 
5521 			/* If HW restart is in progress mwl8k_post_cmd will
5522 			 * return -EBUSY. Avoid retrying mwl8k_check_ba in
5523 			 * such cases
5524 			 */
5525 			if (!rc || rc == -EBUSY)
5526 				break;
5527 			/*
5528 			 * HW queues take time to be flushed, give them
5529 			 * sufficient time
5530 			 */
5531 
5532 			msleep(1000);
5533 		}
5534 		spin_lock(&priv->stream_lock);
5535 		if (rc) {
5536 			wiphy_err(hw->wiphy, "Stream for tid %d busy after %d"
5537 				" attempts\n", tid, MAX_AMPDU_ATTEMPTS);
5538 			mwl8k_remove_stream(hw, stream);
5539 			rc = -EBUSY;
5540 			break;
5541 		}
5542 		rc = IEEE80211_AMPDU_TX_START_IMMEDIATE;
5543 		break;
5544 	case IEEE80211_AMPDU_TX_STOP_CONT:
5545 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
5546 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5547 		if (stream) {
5548 			if (stream->state == AMPDU_STREAM_ACTIVE) {
5549 				idx = stream->idx;
5550 				spin_unlock(&priv->stream_lock);
5551 				mwl8k_destroy_ba(hw, idx);
5552 				spin_lock(&priv->stream_lock);
5553 			}
5554 			mwl8k_remove_stream(hw, stream);
5555 		}
5556 		ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
5557 		break;
5558 	case IEEE80211_AMPDU_TX_OPERATIONAL:
5559 		BUG_ON(stream == NULL);
5560 		BUG_ON(stream->state != AMPDU_STREAM_IN_PROGRESS);
5561 		spin_unlock(&priv->stream_lock);
5562 		rc = mwl8k_create_ba(hw, stream, buf_size, vif);
5563 		spin_lock(&priv->stream_lock);
5564 		if (!rc)
5565 			stream->state = AMPDU_STREAM_ACTIVE;
5566 		else {
5567 			idx = stream->idx;
5568 			spin_unlock(&priv->stream_lock);
5569 			mwl8k_destroy_ba(hw, idx);
5570 			spin_lock(&priv->stream_lock);
5571 			wiphy_debug(hw->wiphy,
5572 				"Failed adding stream for sta %pM tid %d\n",
5573 				addr, tid);
5574 			mwl8k_remove_stream(hw, stream);
5575 		}
5576 		break;
5577 
5578 	default:
5579 		rc = -ENOTSUPP;
5580 	}
5581 
5582 	spin_unlock(&priv->stream_lock);
5583 	return rc;
5584 }
5585 
mwl8k_sw_scan_start(struct ieee80211_hw * hw,struct ieee80211_vif * vif,const u8 * mac_addr)5586 static void mwl8k_sw_scan_start(struct ieee80211_hw *hw,
5587 				struct ieee80211_vif *vif,
5588 				const u8 *mac_addr)
5589 {
5590 	struct mwl8k_priv *priv = hw->priv;
5591 	u8 tmp;
5592 
5593 	if (!priv->ap_fw)
5594 		return;
5595 
5596 	/* clear all stats */
5597 	priv->channel_time = 0;
5598 	ioread32(priv->regs + BBU_RXRDY_CNT_REG);
5599 	ioread32(priv->regs + NOK_CCA_CNT_REG);
5600 	mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &tmp);
5601 
5602 	priv->sw_scan_start = true;
5603 }
5604 
mwl8k_sw_scan_complete(struct ieee80211_hw * hw,struct ieee80211_vif * vif)5605 static void mwl8k_sw_scan_complete(struct ieee80211_hw *hw,
5606 				   struct ieee80211_vif *vif)
5607 {
5608 	struct mwl8k_priv *priv = hw->priv;
5609 	u8 tmp;
5610 
5611 	if (!priv->ap_fw)
5612 		return;
5613 
5614 	priv->sw_scan_start = false;
5615 
5616 	/* clear all stats */
5617 	priv->channel_time = 0;
5618 	ioread32(priv->regs + BBU_RXRDY_CNT_REG);
5619 	ioread32(priv->regs + NOK_CCA_CNT_REG);
5620 	mwl8k_cmd_bbp_reg_access(priv->hw, 0, BBU_AVG_NOISE_VAL, &tmp);
5621 }
5622 
5623 static const struct ieee80211_ops mwl8k_ops = {
5624 	.add_chanctx = ieee80211_emulate_add_chanctx,
5625 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
5626 	.change_chanctx = ieee80211_emulate_change_chanctx,
5627 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
5628 	.tx			= mwl8k_tx,
5629 	.wake_tx_queue		= ieee80211_handle_wake_tx_queue,
5630 	.start			= mwl8k_start,
5631 	.stop			= mwl8k_stop,
5632 	.add_interface		= mwl8k_add_interface,
5633 	.remove_interface	= mwl8k_remove_interface,
5634 	.config			= mwl8k_config,
5635 	.bss_info_changed	= mwl8k_bss_info_changed,
5636 	.prepare_multicast	= mwl8k_prepare_multicast,
5637 	.configure_filter	= mwl8k_configure_filter,
5638 	.set_key                = mwl8k_set_key,
5639 	.set_rts_threshold	= mwl8k_set_rts_threshold,
5640 	.sta_add		= mwl8k_sta_add,
5641 	.sta_remove		= mwl8k_sta_remove,
5642 	.conf_tx		= mwl8k_conf_tx,
5643 	.get_stats		= mwl8k_get_stats,
5644 	.get_survey		= mwl8k_get_survey,
5645 	.ampdu_action		= mwl8k_ampdu_action,
5646 	.sw_scan_start		= mwl8k_sw_scan_start,
5647 	.sw_scan_complete	= mwl8k_sw_scan_complete,
5648 };
5649 
mwl8k_finalize_join_worker(struct work_struct * work)5650 static void mwl8k_finalize_join_worker(struct work_struct *work)
5651 {
5652 	struct mwl8k_priv *priv =
5653 		container_of(work, struct mwl8k_priv, finalize_join_worker);
5654 	struct sk_buff *skb = priv->beacon_skb;
5655 	struct ieee80211_mgmt *mgmt = (void *)skb->data;
5656 	int len = skb->len - offsetof(struct ieee80211_mgmt, u.beacon.variable);
5657 	const u8 *tim = cfg80211_find_ie(WLAN_EID_TIM,
5658 					 mgmt->u.beacon.variable, len);
5659 	int dtim_period = 1;
5660 
5661 	if (tim && tim[1] >= 2)
5662 		dtim_period = tim[3];
5663 
5664 	mwl8k_cmd_finalize_join(priv->hw, skb->data, skb->len, dtim_period);
5665 
5666 	dev_kfree_skb(skb);
5667 	priv->beacon_skb = NULL;
5668 }
5669 
5670 enum {
5671 	MWL8363 = 0,
5672 	MWL8687,
5673 	MWL8366,
5674 	MWL8764,
5675 };
5676 
5677 #define MWL8K_8366_AP_FW_API 3
5678 #define _MWL8K_8366_AP_FW(api) "mwl8k/fmimage_8366_ap-" #api ".fw"
5679 #define MWL8K_8366_AP_FW(api) _MWL8K_8366_AP_FW(api)
5680 
5681 #define MWL8K_8764_AP_FW_API 1
5682 #define _MWL8K_8764_AP_FW(api) "mwl8k/fmimage_8764_ap-" #api ".fw"
5683 #define MWL8K_8764_AP_FW(api) _MWL8K_8764_AP_FW(api)
5684 
5685 static struct mwl8k_device_info mwl8k_info_tbl[] = {
5686 	[MWL8363] = {
5687 		.part_name	= "88w8363",
5688 		.helper_image	= "mwl8k/helper_8363.fw",
5689 		.fw_image_sta	= "mwl8k/fmimage_8363.fw",
5690 	},
5691 	[MWL8687] = {
5692 		.part_name	= "88w8687",
5693 		.helper_image	= "mwl8k/helper_8687.fw",
5694 		.fw_image_sta	= "mwl8k/fmimage_8687.fw",
5695 	},
5696 	[MWL8366] = {
5697 		.part_name	= "88w8366",
5698 		.helper_image	= "mwl8k/helper_8366.fw",
5699 		.fw_image_sta	= "mwl8k/fmimage_8366.fw",
5700 		.fw_image_ap	= MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API),
5701 		.fw_api_ap	= MWL8K_8366_AP_FW_API,
5702 		.ap_rxd_ops	= &rxd_ap_ops,
5703 	},
5704 	[MWL8764] = {
5705 		.part_name	= "88w8764",
5706 		.fw_image_ap	= MWL8K_8764_AP_FW(MWL8K_8764_AP_FW_API),
5707 		.fw_api_ap	= MWL8K_8764_AP_FW_API,
5708 		.ap_rxd_ops	= &rxd_ap_ops,
5709 	},
5710 };
5711 
5712 MODULE_FIRMWARE("mwl8k/helper_8363.fw");
5713 MODULE_FIRMWARE("mwl8k/fmimage_8363.fw");
5714 MODULE_FIRMWARE("mwl8k/helper_8687.fw");
5715 MODULE_FIRMWARE("mwl8k/fmimage_8687.fw");
5716 MODULE_FIRMWARE("mwl8k/helper_8366.fw");
5717 MODULE_FIRMWARE("mwl8k/fmimage_8366.fw");
5718 MODULE_FIRMWARE(MWL8K_8366_AP_FW(MWL8K_8366_AP_FW_API));
5719 
5720 static const struct pci_device_id mwl8k_pci_id_table[] = {
5721 	{ PCI_VDEVICE(MARVELL, 0x2a0a), .driver_data = MWL8363, },
5722 	{ PCI_VDEVICE(MARVELL, 0x2a0c), .driver_data = MWL8363, },
5723 	{ PCI_VDEVICE(MARVELL, 0x2a24), .driver_data = MWL8363, },
5724 	{ PCI_VDEVICE(MARVELL, 0x2a2b), .driver_data = MWL8687, },
5725 	{ PCI_VDEVICE(MARVELL, 0x2a30), .driver_data = MWL8687, },
5726 	{ PCI_VDEVICE(MARVELL, 0x2a40), .driver_data = MWL8366, },
5727 	{ PCI_VDEVICE(MARVELL, 0x2a41), .driver_data = MWL8366, },
5728 	{ PCI_VDEVICE(MARVELL, 0x2a42), .driver_data = MWL8366, },
5729 	{ PCI_VDEVICE(MARVELL, 0x2a43), .driver_data = MWL8366, },
5730 	{ PCI_VDEVICE(MARVELL, 0x2b36), .driver_data = MWL8764, },
5731 	{ },
5732 };
5733 MODULE_DEVICE_TABLE(pci, mwl8k_pci_id_table);
5734 
mwl8k_request_alt_fw(struct mwl8k_priv * priv)5735 static int mwl8k_request_alt_fw(struct mwl8k_priv *priv)
5736 {
5737 	int rc;
5738 	printk(KERN_ERR "%s: Error requesting preferred fw %s.\n"
5739 	       "Trying alternative firmware %s\n", pci_name(priv->pdev),
5740 	       priv->fw_pref, priv->fw_alt);
5741 	rc = mwl8k_request_fw(priv, priv->fw_alt, &priv->fw_ucode, true);
5742 	if (rc) {
5743 		printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5744 		       pci_name(priv->pdev), priv->fw_alt);
5745 		return rc;
5746 	}
5747 	return 0;
5748 }
5749 
5750 static int mwl8k_firmware_load_success(struct mwl8k_priv *priv);
mwl8k_fw_state_machine(const struct firmware * fw,void * context)5751 static void mwl8k_fw_state_machine(const struct firmware *fw, void *context)
5752 {
5753 	struct mwl8k_priv *priv = context;
5754 	struct mwl8k_device_info *di = priv->device_info;
5755 	int rc;
5756 
5757 	switch (priv->fw_state) {
5758 	case FW_STATE_INIT:
5759 		if (!fw) {
5760 			printk(KERN_ERR "%s: Error requesting helper fw %s\n",
5761 			       pci_name(priv->pdev), di->helper_image);
5762 			goto fail;
5763 		}
5764 		priv->fw_helper = fw;
5765 		rc = mwl8k_request_fw(priv, priv->fw_pref, &priv->fw_ucode,
5766 				      true);
5767 		if (rc && priv->fw_alt) {
5768 			rc = mwl8k_request_alt_fw(priv);
5769 			if (rc)
5770 				goto fail;
5771 			priv->fw_state = FW_STATE_LOADING_ALT;
5772 		} else if (rc)
5773 			goto fail;
5774 		else
5775 			priv->fw_state = FW_STATE_LOADING_PREF;
5776 		break;
5777 
5778 	case FW_STATE_LOADING_PREF:
5779 		if (!fw) {
5780 			if (priv->fw_alt) {
5781 				rc = mwl8k_request_alt_fw(priv);
5782 				if (rc)
5783 					goto fail;
5784 				priv->fw_state = FW_STATE_LOADING_ALT;
5785 			} else
5786 				goto fail;
5787 		} else {
5788 			priv->fw_ucode = fw;
5789 			rc = mwl8k_firmware_load_success(priv);
5790 			if (rc)
5791 				goto fail;
5792 			else
5793 				complete(&priv->firmware_loading_complete);
5794 		}
5795 		break;
5796 
5797 	case FW_STATE_LOADING_ALT:
5798 		if (!fw) {
5799 			printk(KERN_ERR "%s: Error requesting alt fw %s\n",
5800 			       pci_name(priv->pdev), di->helper_image);
5801 			goto fail;
5802 		}
5803 		priv->fw_ucode = fw;
5804 		rc = mwl8k_firmware_load_success(priv);
5805 		if (rc)
5806 			goto fail;
5807 		else
5808 			complete(&priv->firmware_loading_complete);
5809 		break;
5810 
5811 	default:
5812 		printk(KERN_ERR "%s: Unexpected firmware loading state: %d\n",
5813 		       MWL8K_NAME, priv->fw_state);
5814 		BUG_ON(1);
5815 	}
5816 
5817 	return;
5818 
5819 fail:
5820 	priv->fw_state = FW_STATE_ERROR;
5821 	complete(&priv->firmware_loading_complete);
5822 	mwl8k_release_firmware(priv);
5823 	device_release_driver(&priv->pdev->dev);
5824 }
5825 
5826 #define MAX_RESTART_ATTEMPTS 1
mwl8k_init_firmware(struct ieee80211_hw * hw,char * fw_image,bool nowait)5827 static int mwl8k_init_firmware(struct ieee80211_hw *hw, char *fw_image,
5828 			       bool nowait)
5829 {
5830 	struct mwl8k_priv *priv = hw->priv;
5831 	int rc;
5832 	int count = MAX_RESTART_ATTEMPTS;
5833 
5834 retry:
5835 	/* Reset firmware and hardware */
5836 	mwl8k_hw_reset(priv);
5837 
5838 	/* Ask userland hotplug daemon for the device firmware */
5839 	rc = mwl8k_request_firmware(priv, fw_image, nowait);
5840 	if (rc) {
5841 		wiphy_err(hw->wiphy, "Firmware files not found\n");
5842 		return rc;
5843 	}
5844 
5845 	if (nowait)
5846 		return rc;
5847 
5848 	/* Load firmware into hardware */
5849 	rc = mwl8k_load_firmware(hw);
5850 	if (rc)
5851 		wiphy_err(hw->wiphy, "Cannot start firmware\n");
5852 
5853 	/* Reclaim memory once firmware is successfully loaded */
5854 	mwl8k_release_firmware(priv);
5855 
5856 	if (rc && count) {
5857 		/* FW did not start successfully;
5858 		 * lets try one more time
5859 		 */
5860 		count--;
5861 		wiphy_err(hw->wiphy, "Trying to reload the firmware again\n");
5862 		msleep(20);
5863 		goto retry;
5864 	}
5865 
5866 	return rc;
5867 }
5868 
mwl8k_init_txqs(struct ieee80211_hw * hw)5869 static int mwl8k_init_txqs(struct ieee80211_hw *hw)
5870 {
5871 	struct mwl8k_priv *priv = hw->priv;
5872 	int rc = 0;
5873 	int i;
5874 
5875 	for (i = 0; i < mwl8k_tx_queues(priv); i++) {
5876 		rc = mwl8k_txq_init(hw, i);
5877 		if (rc)
5878 			break;
5879 		if (priv->ap_fw)
5880 			iowrite32(priv->txq[i].txd_dma,
5881 				  priv->sram + priv->txq_offset[i]);
5882 	}
5883 	return rc;
5884 }
5885 
5886 /* initialize hw after successfully loading a firmware image */
mwl8k_probe_hw(struct ieee80211_hw * hw)5887 static int mwl8k_probe_hw(struct ieee80211_hw *hw)
5888 {
5889 	struct mwl8k_priv *priv = hw->priv;
5890 	int rc = 0;
5891 	int i;
5892 
5893 	if (priv->ap_fw) {
5894 		priv->rxd_ops = priv->device_info->ap_rxd_ops;
5895 		if (priv->rxd_ops == NULL) {
5896 			wiphy_err(hw->wiphy,
5897 				  "Driver does not have AP firmware image support for this hardware\n");
5898 			rc = -ENOENT;
5899 			goto err_stop_firmware;
5900 		}
5901 	} else {
5902 		priv->rxd_ops = &rxd_sta_ops;
5903 	}
5904 
5905 	priv->sniffer_enabled = false;
5906 	priv->wmm_enabled = false;
5907 	priv->pending_tx_pkts = 0;
5908 	atomic_set(&priv->watchdog_event_pending, 0);
5909 
5910 	rc = mwl8k_rxq_init(hw, 0);
5911 	if (rc)
5912 		goto err_stop_firmware;
5913 	rxq_refill(hw, 0, INT_MAX);
5914 
5915 	/* For the sta firmware, we need to know the dma addresses of tx queues
5916 	 * before sending MWL8K_CMD_GET_HW_SPEC.  So we must initialize them
5917 	 * prior to issuing this command.  But for the AP case, we learn the
5918 	 * total number of queues from the result CMD_GET_HW_SPEC, so for this
5919 	 * case we must initialize the tx queues after.
5920 	 */
5921 	priv->num_ampdu_queues = 0;
5922 	if (!priv->ap_fw) {
5923 		rc = mwl8k_init_txqs(hw);
5924 		if (rc)
5925 			goto err_free_queues;
5926 	}
5927 
5928 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS);
5929 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5930 	iowrite32(MWL8K_A2H_INT_TX_DONE|MWL8K_A2H_INT_RX_READY|
5931 		  MWL8K_A2H_INT_BA_WATCHDOG,
5932 		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_CLEAR_SEL);
5933 	iowrite32(MWL8K_A2H_INT_OPC_DONE,
5934 		  priv->regs + MWL8K_HIU_A2H_INTERRUPT_STATUS_MASK);
5935 
5936 	rc = request_irq(priv->pdev->irq, mwl8k_interrupt,
5937 			 IRQF_SHARED, MWL8K_NAME, hw);
5938 	if (rc) {
5939 		wiphy_err(hw->wiphy, "failed to register IRQ handler\n");
5940 		goto err_free_queues;
5941 	}
5942 
5943 	/*
5944 	 * When hw restart is requested,
5945 	 * mac80211 will take care of clearing
5946 	 * the ampdu streams, so do not clear
5947 	 * the ampdu state here
5948 	 */
5949 	if (!priv->hw_restart_in_progress)
5950 		memset(priv->ampdu, 0, sizeof(priv->ampdu));
5951 
5952 	/*
5953 	 * Temporarily enable interrupts.  Initial firmware host
5954 	 * commands use interrupts and avoid polling.  Disable
5955 	 * interrupts when done.
5956 	 */
5957 	iowrite32(MWL8K_A2H_EVENTS, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5958 
5959 	/* Get config data, mac addrs etc */
5960 	if (priv->ap_fw) {
5961 		rc = mwl8k_cmd_get_hw_spec_ap(hw);
5962 		if (!rc)
5963 			rc = mwl8k_init_txqs(hw);
5964 		if (!rc)
5965 			rc = mwl8k_cmd_set_hw_spec(hw);
5966 	} else {
5967 		rc = mwl8k_cmd_get_hw_spec_sta(hw);
5968 	}
5969 	if (rc) {
5970 		wiphy_err(hw->wiphy, "Cannot initialise firmware\n");
5971 		goto err_free_irq;
5972 	}
5973 
5974 	/* Turn radio off */
5975 	rc = mwl8k_cmd_radio_disable(hw);
5976 	if (rc) {
5977 		wiphy_err(hw->wiphy, "Cannot disable\n");
5978 		goto err_free_irq;
5979 	}
5980 
5981 	/* Clear MAC address */
5982 	rc = mwl8k_cmd_set_mac_addr(hw, NULL, "\x00\x00\x00\x00\x00\x00");
5983 	if (rc) {
5984 		wiphy_err(hw->wiphy, "Cannot clear MAC address\n");
5985 		goto err_free_irq;
5986 	}
5987 
5988 	/* Configure Antennas */
5989 	rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_RX, 0x3);
5990 	if (rc)
5991 		wiphy_warn(hw->wiphy, "failed to set # of RX antennas");
5992 	rc = mwl8k_cmd_rf_antenna(hw, MWL8K_RF_ANTENNA_TX, 0x7);
5993 	if (rc)
5994 		wiphy_warn(hw->wiphy, "failed to set # of TX antennas");
5995 
5996 
5997 	/* Disable interrupts */
5998 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
5999 	free_irq(priv->pdev->irq, hw);
6000 
6001 	wiphy_info(hw->wiphy, "%s v%d, %pm, %s firmware %u.%u.%u.%u\n",
6002 		   priv->device_info->part_name,
6003 		   priv->hw_rev, hw->wiphy->perm_addr,
6004 		   priv->ap_fw ? "AP" : "STA",
6005 		   (priv->fw_rev >> 24) & 0xff, (priv->fw_rev >> 16) & 0xff,
6006 		   (priv->fw_rev >> 8) & 0xff, priv->fw_rev & 0xff);
6007 
6008 	return 0;
6009 
6010 err_free_irq:
6011 	iowrite32(0, priv->regs + MWL8K_HIU_A2H_INTERRUPT_MASK);
6012 	free_irq(priv->pdev->irq, hw);
6013 
6014 err_free_queues:
6015 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6016 		mwl8k_txq_deinit(hw, i);
6017 	mwl8k_rxq_deinit(hw, 0);
6018 
6019 err_stop_firmware:
6020 	mwl8k_hw_reset(priv);
6021 
6022 	return rc;
6023 }
6024 
6025 /*
6026  * invoke mwl8k_reload_firmware to change the firmware image after the device
6027  * has already been registered
6028  */
mwl8k_reload_firmware(struct ieee80211_hw * hw,char * fw_image)6029 static int mwl8k_reload_firmware(struct ieee80211_hw *hw, char *fw_image)
6030 {
6031 	int i, rc = 0;
6032 	struct mwl8k_priv *priv = hw->priv;
6033 	struct mwl8k_vif *vif, *tmp_vif;
6034 
6035 	mwl8k_stop(hw, false);
6036 	mwl8k_rxq_deinit(hw, 0);
6037 
6038 	/*
6039 	 * All the existing interfaces are re-added by the ieee80211_reconfig;
6040 	 * which means driver should remove existing interfaces before calling
6041 	 * ieee80211_restart_hw
6042 	 */
6043 	if (priv->hw_restart_in_progress)
6044 		list_for_each_entry_safe(vif, tmp_vif, &priv->vif_list, list)
6045 			mwl8k_remove_vif(priv, vif);
6046 
6047 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6048 		mwl8k_txq_deinit(hw, i);
6049 
6050 	rc = mwl8k_init_firmware(hw, fw_image, false);
6051 	if (rc)
6052 		goto fail;
6053 
6054 	rc = mwl8k_probe_hw(hw);
6055 	if (rc)
6056 		goto fail;
6057 
6058 	if (priv->hw_restart_in_progress)
6059 		return rc;
6060 
6061 	rc = mwl8k_start(hw);
6062 	if (rc)
6063 		goto fail;
6064 
6065 	rc = mwl8k_config(hw, -1, ~0);
6066 	if (rc)
6067 		goto fail;
6068 
6069 	for (i = 0; i < MWL8K_TX_WMM_QUEUES; i++) {
6070 		rc = mwl8k_conf_tx(hw, NULL, 0, i, &priv->wmm_params[i]);
6071 		if (rc)
6072 			goto fail;
6073 	}
6074 
6075 	return rc;
6076 
6077 fail:
6078 	printk(KERN_WARNING "mwl8k: Failed to reload firmware image.\n");
6079 	return rc;
6080 }
6081 
6082 static const struct ieee80211_iface_limit ap_if_limits[] = {
6083 	{ .max = 8,	.types = BIT(NL80211_IFTYPE_AP) },
6084 	{ .max = 1,	.types = BIT(NL80211_IFTYPE_STATION) },
6085 };
6086 
6087 static const struct ieee80211_iface_combination ap_if_comb = {
6088 	.limits = ap_if_limits,
6089 	.n_limits = ARRAY_SIZE(ap_if_limits),
6090 	.max_interfaces = 8,
6091 	.num_different_channels = 1,
6092 };
6093 
6094 
mwl8k_firmware_load_success(struct mwl8k_priv * priv)6095 static int mwl8k_firmware_load_success(struct mwl8k_priv *priv)
6096 {
6097 	struct ieee80211_hw *hw = priv->hw;
6098 	int i, rc;
6099 
6100 	rc = mwl8k_load_firmware(hw);
6101 	mwl8k_release_firmware(priv);
6102 	if (rc) {
6103 		wiphy_err(hw->wiphy, "Cannot start firmware\n");
6104 		return rc;
6105 	}
6106 
6107 	/*
6108 	 * Extra headroom is the size of the required DMA header
6109 	 * minus the size of the smallest 802.11 frame (CTS frame).
6110 	 */
6111 	hw->extra_tx_headroom =
6112 		sizeof(struct mwl8k_dma_data) - sizeof(struct ieee80211_cts);
6113 
6114 	hw->extra_tx_headroom -= priv->ap_fw ? REDUCED_TX_HEADROOM : 0;
6115 
6116 	hw->queues = MWL8K_TX_WMM_QUEUES;
6117 
6118 	/* Set rssi values to dBm */
6119 	ieee80211_hw_set(hw, SIGNAL_DBM);
6120 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
6121 
6122 	/*
6123 	 * Ask mac80211 to not to trigger PS mode
6124 	 * based on PM bit of incoming frames.
6125 	 */
6126 	if (priv->ap_fw)
6127 		ieee80211_hw_set(hw, AP_LINK_PS);
6128 
6129 	hw->vif_data_size = sizeof(struct mwl8k_vif);
6130 	hw->sta_data_size = sizeof(struct mwl8k_sta);
6131 
6132 	priv->macids_used = 0;
6133 	INIT_LIST_HEAD(&priv->vif_list);
6134 
6135 	/* Set default radio state and preamble */
6136 	priv->radio_on = false;
6137 	priv->radio_short_preamble = false;
6138 
6139 	/* Finalize join worker */
6140 	INIT_WORK(&priv->finalize_join_worker, mwl8k_finalize_join_worker);
6141 	/* Handle watchdog ba events */
6142 	INIT_WORK(&priv->watchdog_ba_handle, mwl8k_watchdog_ba_events);
6143 	/* To reload the firmware if it crashes */
6144 	INIT_WORK(&priv->fw_reload, mwl8k_hw_restart_work);
6145 
6146 	/* TX reclaim and RX tasklets.  */
6147 	tasklet_setup(&priv->poll_tx_task, mwl8k_tx_poll);
6148 	tasklet_disable(&priv->poll_tx_task);
6149 	tasklet_setup(&priv->poll_rx_task, mwl8k_rx_poll);
6150 	tasklet_disable(&priv->poll_rx_task);
6151 
6152 	/* Power management cookie */
6153 	priv->cookie = dma_alloc_coherent(&priv->pdev->dev, 4,
6154 					  &priv->cookie_dma, GFP_KERNEL);
6155 	if (priv->cookie == NULL)
6156 		return -ENOMEM;
6157 
6158 	mutex_init(&priv->fw_mutex);
6159 	priv->fw_mutex_owner = NULL;
6160 	priv->fw_mutex_depth = 0;
6161 	priv->hostcmd_wait = NULL;
6162 
6163 	spin_lock_init(&priv->tx_lock);
6164 
6165 	spin_lock_init(&priv->stream_lock);
6166 
6167 	priv->tx_wait = NULL;
6168 
6169 	rc = mwl8k_probe_hw(hw);
6170 	if (rc)
6171 		goto err_free_cookie;
6172 
6173 	hw->wiphy->interface_modes = 0;
6174 
6175 	if (priv->ap_macids_supported || priv->device_info->fw_image_ap) {
6176 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP);
6177 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
6178 		hw->wiphy->iface_combinations = &ap_if_comb;
6179 		hw->wiphy->n_iface_combinations = 1;
6180 	}
6181 
6182 	if (priv->sta_macids_supported || priv->device_info->fw_image_sta)
6183 		hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_STATION);
6184 
6185 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
6186 
6187 	rc = ieee80211_register_hw(hw);
6188 	if (rc) {
6189 		wiphy_err(hw->wiphy, "Cannot register device\n");
6190 		goto err_unprobe_hw;
6191 	}
6192 
6193 	return 0;
6194 
6195 err_unprobe_hw:
6196 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6197 		mwl8k_txq_deinit(hw, i);
6198 	mwl8k_rxq_deinit(hw, 0);
6199 
6200 err_free_cookie:
6201 	if (priv->cookie != NULL)
6202 		dma_free_coherent(&priv->pdev->dev, 4, priv->cookie,
6203 				  priv->cookie_dma);
6204 
6205 	return rc;
6206 }
mwl8k_probe(struct pci_dev * pdev,const struct pci_device_id * id)6207 static int mwl8k_probe(struct pci_dev *pdev,
6208 				 const struct pci_device_id *id)
6209 {
6210 	static int printed_version;
6211 	struct ieee80211_hw *hw;
6212 	struct mwl8k_priv *priv;
6213 	struct mwl8k_device_info *di;
6214 	int rc;
6215 
6216 	if (!printed_version) {
6217 		printk(KERN_INFO "%s version %s\n", MWL8K_DESC, MWL8K_VERSION);
6218 		printed_version = 1;
6219 	}
6220 
6221 
6222 	rc = pci_enable_device(pdev);
6223 	if (rc) {
6224 		printk(KERN_ERR "%s: Cannot enable new PCI device\n",
6225 		       MWL8K_NAME);
6226 		return rc;
6227 	}
6228 
6229 	rc = pci_request_regions(pdev, MWL8K_NAME);
6230 	if (rc) {
6231 		printk(KERN_ERR "%s: Cannot obtain PCI resources\n",
6232 		       MWL8K_NAME);
6233 		goto err_disable_device;
6234 	}
6235 
6236 	pci_set_master(pdev);
6237 
6238 
6239 	hw = ieee80211_alloc_hw(sizeof(*priv), &mwl8k_ops);
6240 	if (hw == NULL) {
6241 		printk(KERN_ERR "%s: ieee80211 alloc failed\n", MWL8K_NAME);
6242 		rc = -ENOMEM;
6243 		goto err_free_reg;
6244 	}
6245 
6246 	SET_IEEE80211_DEV(hw, &pdev->dev);
6247 	pci_set_drvdata(pdev, hw);
6248 
6249 	priv = hw->priv;
6250 	priv->hw = hw;
6251 	priv->pdev = pdev;
6252 	priv->device_info = &mwl8k_info_tbl[id->driver_data];
6253 
6254 	if (id->driver_data == MWL8764)
6255 		priv->is_8764 = true;
6256 
6257 	priv->sram = pci_iomap(pdev, 0, 0x10000);
6258 	if (priv->sram == NULL) {
6259 		wiphy_err(hw->wiphy, "Cannot map device SRAM\n");
6260 		rc = -EIO;
6261 		goto err_iounmap;
6262 	}
6263 
6264 	/*
6265 	 * If BAR0 is a 32 bit BAR, the register BAR will be BAR1.
6266 	 * If BAR0 is a 64 bit BAR, the register BAR will be BAR2.
6267 	 */
6268 	priv->regs = pci_iomap(pdev, 1, 0x10000);
6269 	if (priv->regs == NULL) {
6270 		priv->regs = pci_iomap(pdev, 2, 0x10000);
6271 		if (priv->regs == NULL) {
6272 			wiphy_err(hw->wiphy, "Cannot map device registers\n");
6273 			rc = -EIO;
6274 			goto err_iounmap;
6275 		}
6276 	}
6277 
6278 	/*
6279 	 * Choose the initial fw image depending on user input.  If a second
6280 	 * image is available, make it the alternative image that will be
6281 	 * loaded if the first one fails.
6282 	 */
6283 	init_completion(&priv->firmware_loading_complete);
6284 	di = priv->device_info;
6285 	if (ap_mode_default && di->fw_image_ap) {
6286 		priv->fw_pref = di->fw_image_ap;
6287 		priv->fw_alt = di->fw_image_sta;
6288 	} else if (!ap_mode_default && di->fw_image_sta) {
6289 		priv->fw_pref = di->fw_image_sta;
6290 		priv->fw_alt = di->fw_image_ap;
6291 	} else if (ap_mode_default && !di->fw_image_ap && di->fw_image_sta) {
6292 		printk(KERN_WARNING "AP fw is unavailable.  Using STA fw.");
6293 		priv->fw_pref = di->fw_image_sta;
6294 	} else if (!ap_mode_default && !di->fw_image_sta && di->fw_image_ap) {
6295 		printk(KERN_WARNING "STA fw is unavailable.  Using AP fw.");
6296 		priv->fw_pref = di->fw_image_ap;
6297 	}
6298 	rc = mwl8k_init_firmware(hw, priv->fw_pref, true);
6299 	if (rc)
6300 		goto err_stop_firmware;
6301 
6302 	priv->hw_restart_in_progress = false;
6303 
6304 	priv->running_bsses = 0;
6305 
6306 	return rc;
6307 
6308 err_stop_firmware:
6309 	mwl8k_hw_reset(priv);
6310 
6311 err_iounmap:
6312 	if (priv->regs != NULL)
6313 		pci_iounmap(pdev, priv->regs);
6314 
6315 	if (priv->sram != NULL)
6316 		pci_iounmap(pdev, priv->sram);
6317 
6318 	ieee80211_free_hw(hw);
6319 
6320 err_free_reg:
6321 	pci_release_regions(pdev);
6322 
6323 err_disable_device:
6324 	pci_disable_device(pdev);
6325 
6326 	return rc;
6327 }
6328 
mwl8k_remove(struct pci_dev * pdev)6329 static void mwl8k_remove(struct pci_dev *pdev)
6330 {
6331 	struct ieee80211_hw *hw = pci_get_drvdata(pdev);
6332 	struct mwl8k_priv *priv;
6333 	int i;
6334 
6335 	if (hw == NULL)
6336 		return;
6337 	priv = hw->priv;
6338 
6339 	wait_for_completion(&priv->firmware_loading_complete);
6340 
6341 	if (priv->fw_state == FW_STATE_ERROR) {
6342 		mwl8k_hw_reset(priv);
6343 		goto unmap;
6344 	}
6345 
6346 	ieee80211_stop_queues(hw);
6347 
6348 	ieee80211_unregister_hw(hw);
6349 
6350 	/* Remove TX reclaim and RX tasklets.  */
6351 	tasklet_kill(&priv->poll_tx_task);
6352 	tasklet_kill(&priv->poll_rx_task);
6353 
6354 	/* Stop hardware */
6355 	mwl8k_hw_reset(priv);
6356 
6357 	/* Return all skbs to mac80211 */
6358 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6359 		mwl8k_txq_reclaim(hw, i, INT_MAX, 1);
6360 
6361 	for (i = 0; i < mwl8k_tx_queues(priv); i++)
6362 		mwl8k_txq_deinit(hw, i);
6363 
6364 	mwl8k_rxq_deinit(hw, 0);
6365 
6366 	dma_free_coherent(&priv->pdev->dev, 4, priv->cookie, priv->cookie_dma);
6367 
6368 unmap:
6369 	pci_iounmap(pdev, priv->regs);
6370 	pci_iounmap(pdev, priv->sram);
6371 	ieee80211_free_hw(hw);
6372 	pci_release_regions(pdev);
6373 	pci_disable_device(pdev);
6374 }
6375 
6376 static struct pci_driver mwl8k_driver = {
6377 	.name		= MWL8K_NAME,
6378 	.id_table	= mwl8k_pci_id_table,
6379 	.probe		= mwl8k_probe,
6380 	.remove		= mwl8k_remove,
6381 };
6382 
6383 module_pci_driver(mwl8k_driver);
6384 
6385 MODULE_DESCRIPTION(MWL8K_DESC);
6386 MODULE_VERSION(MWL8K_VERSION);
6387 MODULE_AUTHOR("Lennert Buytenhek <buytenh@marvell.com>");
6388 MODULE_LICENSE("GPL");
6389