xref: /linux/drivers/net/wireless/ath/carl9170/main.c (revision 91ec2035134982b98fab0609a9fd8480e8217dc1)
1 /*
2  * Atheros CARL9170 driver
3  *
4  * mac80211 interaction code
5  *
6  * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7  * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; see the file COPYING.  If not, see
21  * http://www.gnu.org/licenses/.
22  *
23  * This file incorporates work covered by the following copyright and
24  * permission notice:
25  *    Copyright (c) 2007-2008 Atheros Communications, Inc.
26  *
27  *    Permission to use, copy, modify, and/or distribute this software for any
28  *    purpose with or without fee is hereby granted, provided that the above
29  *    copyright notice and this permission notice appear in all copies.
30  *
31  *    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32  *    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33  *    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34  *    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35  *    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36  *    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37  *    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38  */
39 
40 #include <linux/slab.h>
41 #include <linux/module.h>
42 #include <linux/etherdevice.h>
43 #include <linux/random.h>
44 #include <net/mac80211.h>
45 #include <net/cfg80211.h>
46 #include "hw.h"
47 #include "carl9170.h"
48 #include "cmd.h"
49 
50 static bool modparam_nohwcrypt;
51 module_param_named(nohwcrypt, modparam_nohwcrypt, bool, 0444);
52 MODULE_PARM_DESC(nohwcrypt, "Disable hardware crypto offload.");
53 
54 int modparam_noht;
55 module_param_named(noht, modparam_noht, int, 0444);
56 MODULE_PARM_DESC(noht, "Disable MPDU aggregation.");
57 
58 #define RATE(_bitrate, _hw_rate, _txpidx, _flags) {	\
59 	.bitrate	= (_bitrate),			\
60 	.flags		= (_flags),			\
61 	.hw_value	= (_hw_rate) | (_txpidx) << 4,	\
62 }
63 
64 struct ieee80211_rate __carl9170_ratetable[] = {
65 	RATE(10, 0, 0, 0),
66 	RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE),
67 	RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE),
68 	RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE),
69 	RATE(60, 0xb, 0, 0),
70 	RATE(90, 0xf, 0, 0),
71 	RATE(120, 0xa, 0, 0),
72 	RATE(180, 0xe, 0, 0),
73 	RATE(240, 0x9, 0, 0),
74 	RATE(360, 0xd, 1, 0),
75 	RATE(480, 0x8, 2, 0),
76 	RATE(540, 0xc, 3, 0),
77 };
78 #undef RATE
79 
80 #define carl9170_g_ratetable	(__carl9170_ratetable + 0)
81 #define carl9170_g_ratetable_size	12
82 #define carl9170_a_ratetable	(__carl9170_ratetable + 4)
83 #define carl9170_a_ratetable_size	8
84 
85 /*
86  * NB: The hw_value is used as an index into the carl9170_phy_freq_params
87  *     array in phy.c so that we don't have to do frequency lookups!
88  */
89 #define CHAN(_freq, _idx) {		\
90 	.center_freq	= (_freq),	\
91 	.hw_value	= (_idx),	\
92 	.max_power	= 18, /* XXX */	\
93 }
94 
95 static struct ieee80211_channel carl9170_2ghz_chantable[] = {
96 	CHAN(2412,  0),
97 	CHAN(2417,  1),
98 	CHAN(2422,  2),
99 	CHAN(2427,  3),
100 	CHAN(2432,  4),
101 	CHAN(2437,  5),
102 	CHAN(2442,  6),
103 	CHAN(2447,  7),
104 	CHAN(2452,  8),
105 	CHAN(2457,  9),
106 	CHAN(2462, 10),
107 	CHAN(2467, 11),
108 	CHAN(2472, 12),
109 	CHAN(2484, 13),
110 };
111 
112 static struct ieee80211_channel carl9170_5ghz_chantable[] = {
113 	CHAN(4920, 14),
114 	CHAN(4940, 15),
115 	CHAN(4960, 16),
116 	CHAN(4980, 17),
117 	CHAN(5040, 18),
118 	CHAN(5060, 19),
119 	CHAN(5080, 20),
120 	CHAN(5180, 21),
121 	CHAN(5200, 22),
122 	CHAN(5220, 23),
123 	CHAN(5240, 24),
124 	CHAN(5260, 25),
125 	CHAN(5280, 26),
126 	CHAN(5300, 27),
127 	CHAN(5320, 28),
128 	CHAN(5500, 29),
129 	CHAN(5520, 30),
130 	CHAN(5540, 31),
131 	CHAN(5560, 32),
132 	CHAN(5580, 33),
133 	CHAN(5600, 34),
134 	CHAN(5620, 35),
135 	CHAN(5640, 36),
136 	CHAN(5660, 37),
137 	CHAN(5680, 38),
138 	CHAN(5700, 39),
139 	CHAN(5745, 40),
140 	CHAN(5765, 41),
141 	CHAN(5785, 42),
142 	CHAN(5805, 43),
143 	CHAN(5825, 44),
144 	CHAN(5170, 45),
145 	CHAN(5190, 46),
146 	CHAN(5210, 47),
147 	CHAN(5230, 48),
148 };
149 #undef CHAN
150 
151 #define CARL9170_HT_CAP							\
152 {									\
153 	.ht_supported	= true,						\
154 	.cap		= IEEE80211_HT_CAP_MAX_AMSDU |			\
155 			  IEEE80211_HT_CAP_SUP_WIDTH_20_40 |		\
156 			  IEEE80211_HT_CAP_SGI_40 |			\
157 			  IEEE80211_HT_CAP_DSSSCCK40 |			\
158 			  IEEE80211_HT_CAP_SM_PS,			\
159 	.ampdu_factor	= IEEE80211_HT_MAX_AMPDU_64K,			\
160 	.ampdu_density	= IEEE80211_HT_MPDU_DENSITY_8,			\
161 	.mcs		= {						\
162 		.rx_mask = { 0xff, 0xff, 0, 0, 0x1, 0, 0, 0, 0, 0, },	\
163 		.rx_highest = cpu_to_le16(300),				\
164 		.tx_params = IEEE80211_HT_MCS_TX_DEFINED,		\
165 	},								\
166 }
167 
168 static struct ieee80211_supported_band carl9170_band_2GHz = {
169 	.channels	= carl9170_2ghz_chantable,
170 	.n_channels	= ARRAY_SIZE(carl9170_2ghz_chantable),
171 	.bitrates	= carl9170_g_ratetable,
172 	.n_bitrates	= carl9170_g_ratetable_size,
173 	.ht_cap		= CARL9170_HT_CAP,
174 };
175 
176 static struct ieee80211_supported_band carl9170_band_5GHz = {
177 	.channels	= carl9170_5ghz_chantable,
178 	.n_channels	= ARRAY_SIZE(carl9170_5ghz_chantable),
179 	.bitrates	= carl9170_a_ratetable,
180 	.n_bitrates	= carl9170_a_ratetable_size,
181 	.ht_cap		= CARL9170_HT_CAP,
182 };
183 
carl9170_ampdu_gc(struct ar9170 * ar)184 static void carl9170_ampdu_gc(struct ar9170 *ar)
185 {
186 	struct carl9170_sta_tid *tid_info;
187 	LIST_HEAD(tid_gc);
188 
189 	rcu_read_lock();
190 	list_for_each_entry_rcu(tid_info, &ar->tx_ampdu_list, list) {
191 		spin_lock_bh(&ar->tx_ampdu_list_lock);
192 		if (tid_info->state == CARL9170_TID_STATE_SHUTDOWN) {
193 			tid_info->state = CARL9170_TID_STATE_KILLED;
194 			list_del_rcu(&tid_info->list);
195 			ar->tx_ampdu_list_len--;
196 			list_add_tail(&tid_info->tmp_list, &tid_gc);
197 		}
198 		spin_unlock_bh(&ar->tx_ampdu_list_lock);
199 
200 	}
201 	rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
202 	rcu_read_unlock();
203 
204 	synchronize_rcu();
205 
206 	while (!list_empty(&tid_gc)) {
207 		struct sk_buff *skb;
208 		tid_info = list_first_entry(&tid_gc, struct carl9170_sta_tid,
209 					    tmp_list);
210 
211 		while ((skb = __skb_dequeue(&tid_info->queue)))
212 			carl9170_tx_status(ar, skb, false);
213 
214 		list_del_init(&tid_info->tmp_list);
215 		kfree(tid_info);
216 	}
217 }
218 
carl9170_flush(struct ar9170 * ar,bool drop_queued)219 static void carl9170_flush(struct ar9170 *ar, bool drop_queued)
220 {
221 	if (drop_queued) {
222 		int i;
223 
224 		/*
225 		 * We can only drop frames which have not been uploaded
226 		 * to the device yet.
227 		 */
228 
229 		for (i = 0; i < ar->hw->queues; i++) {
230 			struct sk_buff *skb;
231 
232 			while ((skb = skb_dequeue(&ar->tx_pending[i]))) {
233 				struct ieee80211_tx_info *info;
234 
235 				info = IEEE80211_SKB_CB(skb);
236 				if (info->flags & IEEE80211_TX_CTL_AMPDU)
237 					atomic_dec(&ar->tx_ampdu_upload);
238 
239 				carl9170_tx_status(ar, skb, false);
240 			}
241 		}
242 	}
243 
244 	/* Wait for all other outstanding frames to timeout. */
245 	if (atomic_read(&ar->tx_total_queued))
246 		WARN_ON(wait_for_completion_timeout(&ar->tx_flush, HZ) == 0);
247 }
248 
carl9170_flush_ba(struct ar9170 * ar)249 static void carl9170_flush_ba(struct ar9170 *ar)
250 {
251 	struct sk_buff_head free;
252 	struct carl9170_sta_tid *tid_info;
253 	struct sk_buff *skb;
254 
255 	__skb_queue_head_init(&free);
256 
257 	rcu_read_lock();
258 	spin_lock_bh(&ar->tx_ampdu_list_lock);
259 	list_for_each_entry_rcu(tid_info, &ar->tx_ampdu_list, list) {
260 		if (tid_info->state > CARL9170_TID_STATE_SUSPEND) {
261 			tid_info->state = CARL9170_TID_STATE_SUSPEND;
262 
263 			spin_lock(&tid_info->lock);
264 			while ((skb = __skb_dequeue(&tid_info->queue)))
265 				__skb_queue_tail(&free, skb);
266 			spin_unlock(&tid_info->lock);
267 		}
268 	}
269 	spin_unlock_bh(&ar->tx_ampdu_list_lock);
270 	rcu_read_unlock();
271 
272 	while ((skb = __skb_dequeue(&free)))
273 		carl9170_tx_status(ar, skb, false);
274 }
275 
carl9170_zap_queues(struct ar9170 * ar)276 static void carl9170_zap_queues(struct ar9170 *ar)
277 {
278 	struct carl9170_vif_info *cvif;
279 	unsigned int i;
280 
281 	carl9170_ampdu_gc(ar);
282 
283 	carl9170_flush_ba(ar);
284 	carl9170_flush(ar, true);
285 
286 	for (i = 0; i < ar->hw->queues; i++) {
287 		spin_lock_bh(&ar->tx_status[i].lock);
288 		while (!skb_queue_empty(&ar->tx_status[i])) {
289 			struct sk_buff *skb;
290 
291 			skb = skb_peek(&ar->tx_status[i]);
292 			carl9170_tx_get_skb(skb);
293 			spin_unlock_bh(&ar->tx_status[i].lock);
294 			carl9170_tx_drop(ar, skb);
295 			spin_lock_bh(&ar->tx_status[i].lock);
296 			carl9170_tx_put_skb(skb);
297 		}
298 		spin_unlock_bh(&ar->tx_status[i].lock);
299 	}
300 
301 	BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_SOFT < 1);
302 	BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD < CARL9170_NUM_TX_LIMIT_SOFT);
303 	BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD >= CARL9170_BAW_BITS);
304 
305 	/* reinitialize queues statistics */
306 	memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
307 	for (i = 0; i < ar->hw->queues; i++)
308 		ar->tx_stats[i].limit = CARL9170_NUM_TX_LIMIT_HARD;
309 
310 	bitmap_zero(ar->mem_bitmap, ar->fw.mem_blocks);
311 
312 	rcu_read_lock();
313 	list_for_each_entry_rcu(cvif, &ar->vif_list, list) {
314 		spin_lock_bh(&ar->beacon_lock);
315 		dev_kfree_skb_any(cvif->beacon);
316 		cvif->beacon = NULL;
317 		spin_unlock_bh(&ar->beacon_lock);
318 	}
319 	rcu_read_unlock();
320 
321 	atomic_set(&ar->tx_ampdu_upload, 0);
322 	atomic_set(&ar->tx_ampdu_scheduler, 0);
323 	atomic_set(&ar->tx_total_pending, 0);
324 	atomic_set(&ar->tx_total_queued, 0);
325 	atomic_set(&ar->mem_free_blocks, ar->fw.mem_blocks);
326 }
327 
328 #define CARL9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop)		\
329 do {									\
330 	queue.aifs = ai_fs;						\
331 	queue.cw_min = cwmin;						\
332 	queue.cw_max = cwmax;						\
333 	queue.txop = _txop;						\
334 } while (0)
335 
carl9170_op_start(struct ieee80211_hw * hw)336 static int carl9170_op_start(struct ieee80211_hw *hw)
337 {
338 	struct ar9170 *ar = hw->priv;
339 	int err, i;
340 
341 	mutex_lock(&ar->mutex);
342 
343 	carl9170_zap_queues(ar);
344 
345 	/* reset QoS defaults */
346 	CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_VO], 2, 3,     7, 47);
347 	CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_VI], 2, 7,    15, 94);
348 	CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_BE], 3, 15, 1023,  0);
349 	CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_BK], 7, 15, 1023,  0);
350 	CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_SPECIAL], 2, 3, 7, 0);
351 
352 	ar->current_factor = ar->current_density = -1;
353 	/* "The first key is unique." */
354 	ar->usedkeys = 1;
355 	ar->filter_state = 0;
356 	ar->ps.last_action = jiffies;
357 	ar->ps.last_slept = jiffies;
358 	ar->erp_mode = CARL9170_ERP_AUTO;
359 
360 	/* Set "disable hw crypto offload" whenever the module parameter
361 	 * nohwcrypt is true or if the firmware does not support it.
362 	 */
363 	ar->disable_offload = modparam_nohwcrypt |
364 		ar->fw.disable_offload_fw;
365 	ar->rx_software_decryption = ar->disable_offload;
366 
367 	for (i = 0; i < ar->hw->queues; i++) {
368 		ar->queue_stop_timeout[i] = jiffies;
369 		ar->max_queue_stop_timeout[i] = 0;
370 	}
371 
372 	atomic_set(&ar->mem_allocs, 0);
373 
374 	err = carl9170_usb_open(ar);
375 	if (err)
376 		goto out;
377 
378 	err = carl9170_init_mac(ar);
379 	if (err)
380 		goto out;
381 
382 	err = carl9170_set_qos(ar);
383 	if (err)
384 		goto out;
385 
386 	if (ar->fw.rx_filter) {
387 		err = carl9170_rx_filter(ar, CARL9170_RX_FILTER_OTHER_RA |
388 			CARL9170_RX_FILTER_CTL_OTHER | CARL9170_RX_FILTER_BAD);
389 		if (err)
390 			goto out;
391 	}
392 
393 	err = carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER,
394 				 AR9170_DMA_TRIGGER_RXQ);
395 	if (err)
396 		goto out;
397 
398 	/* Clear key-cache */
399 	for (i = 0; i < AR9170_CAM_MAX_USER + 4; i++) {
400 		err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
401 					  0, NULL, 0);
402 		if (err)
403 			goto out;
404 
405 		err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
406 					  1, NULL, 0);
407 		if (err)
408 			goto out;
409 
410 		if (i < AR9170_CAM_MAX_USER) {
411 			err = carl9170_disable_key(ar, i);
412 			if (err)
413 				goto out;
414 		}
415 	}
416 
417 	carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STARTED);
418 
419 	ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
420 		round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
421 
422 	ieee80211_wake_queues(ar->hw);
423 	err = 0;
424 
425 out:
426 	mutex_unlock(&ar->mutex);
427 	return err;
428 }
429 
carl9170_cancel_worker(struct ar9170 * ar)430 static void carl9170_cancel_worker(struct ar9170 *ar)
431 {
432 	cancel_delayed_work_sync(&ar->stat_work);
433 	cancel_delayed_work_sync(&ar->tx_janitor);
434 #ifdef CONFIG_CARL9170_LEDS
435 	cancel_delayed_work_sync(&ar->led_work);
436 #endif /* CONFIG_CARL9170_LEDS */
437 	cancel_work_sync(&ar->ps_work);
438 	cancel_work_sync(&ar->ping_work);
439 	cancel_work_sync(&ar->ampdu_work);
440 }
441 
carl9170_op_stop(struct ieee80211_hw * hw,bool suspend)442 static void carl9170_op_stop(struct ieee80211_hw *hw, bool suspend)
443 {
444 	struct ar9170 *ar = hw->priv;
445 
446 	carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
447 
448 	ieee80211_stop_queues(ar->hw);
449 
450 	mutex_lock(&ar->mutex);
451 	if (IS_ACCEPTING_CMD(ar)) {
452 		RCU_INIT_POINTER(ar->beacon_iter, NULL);
453 
454 		carl9170_led_set_state(ar, 0);
455 
456 		/* stop DMA */
457 		carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER, 0);
458 		carl9170_usb_stop(ar);
459 	}
460 
461 	carl9170_zap_queues(ar);
462 	mutex_unlock(&ar->mutex);
463 
464 	carl9170_cancel_worker(ar);
465 }
466 
carl9170_restart_work(struct work_struct * work)467 static void carl9170_restart_work(struct work_struct *work)
468 {
469 	struct ar9170 *ar = container_of(work, struct ar9170,
470 					 restart_work);
471 	int err = -EIO;
472 
473 	ar->usedkeys = 0;
474 	ar->filter_state = 0;
475 	carl9170_cancel_worker(ar);
476 
477 	mutex_lock(&ar->mutex);
478 	if (!ar->force_usb_reset) {
479 		err = carl9170_usb_restart(ar);
480 		if (net_ratelimit()) {
481 			if (err)
482 				dev_err(&ar->udev->dev, "Failed to restart device (%d).\n", err);
483 			else
484 				dev_info(&ar->udev->dev, "device restarted successfully.\n");
485 		}
486 	}
487 	carl9170_zap_queues(ar);
488 	mutex_unlock(&ar->mutex);
489 
490 	if (!err && !ar->force_usb_reset) {
491 		ar->restart_counter++;
492 		atomic_set(&ar->pending_restarts, 0);
493 
494 		ieee80211_restart_hw(ar->hw);
495 	} else {
496 		/*
497 		 * The reset was unsuccessful and the device seems to
498 		 * be dead. But there's still one option: a low-level
499 		 * usb subsystem reset...
500 		 */
501 
502 		carl9170_usb_reset(ar);
503 	}
504 }
505 
carl9170_restart(struct ar9170 * ar,const enum carl9170_restart_reasons r)506 void carl9170_restart(struct ar9170 *ar, const enum carl9170_restart_reasons r)
507 {
508 	carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
509 
510 	/*
511 	 * Sometimes, an error can trigger several different reset events.
512 	 * By ignoring these *surplus* reset events, the device won't be
513 	 * killed again, right after it has recovered.
514 	 */
515 	if (atomic_inc_return(&ar->pending_restarts) > 1) {
516 		dev_dbg(&ar->udev->dev, "ignoring restart (%d)\n", r);
517 		return;
518 	}
519 
520 	ieee80211_stop_queues(ar->hw);
521 
522 	dev_err(&ar->udev->dev, "restart device (%d)\n", r);
523 
524 	if (!WARN_ON(r == CARL9170_RR_NO_REASON) ||
525 	    !WARN_ON(r >= __CARL9170_RR_LAST))
526 		ar->last_reason = r;
527 
528 	if (!ar->registered)
529 		return;
530 
531 	if (!IS_ACCEPTING_CMD(ar) || ar->needs_full_reset)
532 		ar->force_usb_reset = true;
533 
534 	ieee80211_queue_work(ar->hw, &ar->restart_work);
535 
536 	/*
537 	 * At this point, the device instance might have vanished/disabled.
538 	 * So, don't put any code which access the ar9170 struct
539 	 * without proper protection.
540 	 */
541 }
542 
carl9170_ping_work(struct work_struct * work)543 static void carl9170_ping_work(struct work_struct *work)
544 {
545 	struct ar9170 *ar = container_of(work, struct ar9170, ping_work);
546 	int err;
547 
548 	if (!IS_STARTED(ar))
549 		return;
550 
551 	mutex_lock(&ar->mutex);
552 	err = carl9170_echo_test(ar, 0xdeadbeef);
553 	if (err)
554 		carl9170_restart(ar, CARL9170_RR_UNRESPONSIVE_DEVICE);
555 	mutex_unlock(&ar->mutex);
556 }
557 
carl9170_init_interface(struct ar9170 * ar,struct ieee80211_vif * vif)558 static int carl9170_init_interface(struct ar9170 *ar,
559 				   struct ieee80211_vif *vif)
560 {
561 	struct ath_common *common = &ar->common;
562 	int err;
563 
564 	if (!vif) {
565 		WARN_ON_ONCE(IS_STARTED(ar));
566 		return 0;
567 	}
568 
569 	memcpy(common->macaddr, vif->addr, ETH_ALEN);
570 
571 	/* We have to fall back to software crypto, whenever
572 	 * the user choose to participates in an IBSS. HW
573 	 * offload for IBSS RSN is not supported by this driver.
574 	 *
575 	 * NOTE: If the previous main interface has already
576 	 * disabled hw crypto offload, we have to keep this
577 	 * previous disable_offload setting as it was.
578 	 * Altough ideally, we should notify mac80211 and tell
579 	 * it to forget about any HW crypto offload for now.
580 	 */
581 	ar->disable_offload |= ((vif->type != NL80211_IFTYPE_STATION) &&
582 	    (vif->type != NL80211_IFTYPE_AP));
583 
584 	/* The driver used to have P2P GO+CLIENT support,
585 	 * but since this was dropped and we don't know if
586 	 * there are any gremlins lurking in the shadows,
587 	 * so best we keep HW offload disabled for P2P.
588 	 */
589 	ar->disable_offload |= vif->p2p;
590 
591 	ar->rx_software_decryption = ar->disable_offload;
592 
593 	err = carl9170_set_operating_mode(ar);
594 	return err;
595 }
596 
carl9170_op_add_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)597 static int carl9170_op_add_interface(struct ieee80211_hw *hw,
598 				     struct ieee80211_vif *vif)
599 {
600 	struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
601 	struct ieee80211_vif *main_vif, *old_main = NULL;
602 	struct ar9170 *ar = hw->priv;
603 	int vif_id = -1, err = 0;
604 
605 	mutex_lock(&ar->mutex);
606 	rcu_read_lock();
607 	if (vif_priv->active) {
608 		/*
609 		 * Skip the interface structure initialization,
610 		 * if the vif survived the _restart call.
611 		 */
612 		vif_id = vif_priv->id;
613 		vif_priv->enable_beacon = false;
614 
615 		spin_lock_bh(&ar->beacon_lock);
616 		dev_kfree_skb_any(vif_priv->beacon);
617 		vif_priv->beacon = NULL;
618 		spin_unlock_bh(&ar->beacon_lock);
619 
620 		goto init;
621 	}
622 
623 	/* Because the AR9170 HW's MAC doesn't provide full support for
624 	 * multiple, independent interfaces [of different operation modes].
625 	 * We have to select ONE main interface [main mode of HW], but we
626 	 * can have multiple slaves [AKA: entry in the ACK-table].
627 	 *
628 	 * The first (from HEAD/TOP) interface in the ar->vif_list is
629 	 * always the main intf. All following intfs in this list
630 	 * are considered to be slave intfs.
631 	 */
632 	main_vif = carl9170_get_main_vif(ar);
633 
634 	if (main_vif) {
635 		switch (main_vif->type) {
636 		case NL80211_IFTYPE_STATION:
637 			if (vif->type == NL80211_IFTYPE_STATION)
638 				break;
639 
640 			err = -EBUSY;
641 			rcu_read_unlock();
642 
643 			goto unlock;
644 
645 		case NL80211_IFTYPE_MESH_POINT:
646 		case NL80211_IFTYPE_AP:
647 			if ((vif->type == NL80211_IFTYPE_STATION) ||
648 			    (vif->type == NL80211_IFTYPE_AP) ||
649 			    (vif->type == NL80211_IFTYPE_MESH_POINT))
650 				break;
651 
652 			err = -EBUSY;
653 			rcu_read_unlock();
654 			goto unlock;
655 
656 		default:
657 			rcu_read_unlock();
658 			goto unlock;
659 		}
660 	}
661 
662 	vif_id = bitmap_find_free_region(&ar->vif_bitmap, ar->fw.vif_num, 0);
663 
664 	if (vif_id < 0) {
665 		rcu_read_unlock();
666 
667 		err = -ENOSPC;
668 		goto unlock;
669 	}
670 
671 	BUG_ON(ar->vif_priv[vif_id].id != vif_id);
672 
673 	vif_priv->active = true;
674 	vif_priv->id = vif_id;
675 	vif_priv->enable_beacon = false;
676 	ar->vifs++;
677 	if (old_main) {
678 		/* We end up in here, if the main interface is being replaced.
679 		 * Put the new main interface at the HEAD of the list and the
680 		 * previous inteface will automatically become second in line.
681 		 */
682 		list_add_rcu(&vif_priv->list, &ar->vif_list);
683 	} else {
684 		/* Add new inteface. If the list is empty, it will become the
685 		 * main inteface, otherwise it will be slave.
686 		 */
687 		list_add_tail_rcu(&vif_priv->list, &ar->vif_list);
688 	}
689 	rcu_assign_pointer(ar->vif_priv[vif_id].vif, vif);
690 
691 init:
692 	main_vif = carl9170_get_main_vif(ar);
693 
694 	if (main_vif == vif) {
695 		rcu_assign_pointer(ar->beacon_iter, vif_priv);
696 		rcu_read_unlock();
697 
698 		if (old_main) {
699 			struct carl9170_vif_info *old_main_priv =
700 				(void *) old_main->drv_priv;
701 			/* downgrade old main intf to slave intf.
702 			 * NOTE: We are no longer under rcu_read_lock.
703 			 * But we are still holding ar->mutex, so the
704 			 * vif data [id, addr] is safe.
705 			 */
706 			err = carl9170_mod_virtual_mac(ar, old_main_priv->id,
707 						       old_main->addr);
708 			if (err)
709 				goto unlock;
710 		}
711 
712 		err = carl9170_init_interface(ar, vif);
713 		if (err)
714 			goto unlock;
715 	} else {
716 		rcu_read_unlock();
717 		err = carl9170_mod_virtual_mac(ar, vif_id, vif->addr);
718 
719 		if (err)
720 			goto unlock;
721 	}
722 
723 	if (ar->fw.tx_seq_table) {
724 		err = carl9170_write_reg(ar, ar->fw.tx_seq_table + vif_id * 4,
725 					 0);
726 		if (err)
727 			goto unlock;
728 	}
729 
730 unlock:
731 	if (err && (vif_id >= 0)) {
732 		vif_priv->active = false;
733 		bitmap_release_region(&ar->vif_bitmap, vif_id, 0);
734 		ar->vifs--;
735 		RCU_INIT_POINTER(ar->vif_priv[vif_id].vif, NULL);
736 		list_del_rcu(&vif_priv->list);
737 		mutex_unlock(&ar->mutex);
738 		synchronize_rcu();
739 	} else {
740 		if (ar->vifs > 1)
741 			ar->ps.off_override |= PS_OFF_VIF;
742 
743 		mutex_unlock(&ar->mutex);
744 	}
745 
746 	return err;
747 }
748 
carl9170_op_remove_interface(struct ieee80211_hw * hw,struct ieee80211_vif * vif)749 static void carl9170_op_remove_interface(struct ieee80211_hw *hw,
750 					 struct ieee80211_vif *vif)
751 {
752 	struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
753 	struct ieee80211_vif *main_vif;
754 	struct ar9170 *ar = hw->priv;
755 	unsigned int id;
756 
757 	mutex_lock(&ar->mutex);
758 
759 	if (WARN_ON_ONCE(!vif_priv->active))
760 		goto unlock;
761 
762 	ar->vifs--;
763 
764 	rcu_read_lock();
765 	main_vif = carl9170_get_main_vif(ar);
766 
767 	id = vif_priv->id;
768 
769 	vif_priv->active = false;
770 	WARN_ON(vif_priv->enable_beacon);
771 	vif_priv->enable_beacon = false;
772 	list_del_rcu(&vif_priv->list);
773 	RCU_INIT_POINTER(ar->vif_priv[id].vif, NULL);
774 
775 	if (vif == main_vif) {
776 		rcu_read_unlock();
777 
778 		if (ar->vifs) {
779 			WARN_ON(carl9170_init_interface(ar,
780 					carl9170_get_main_vif(ar)));
781 		} else {
782 			carl9170_set_operating_mode(ar);
783 		}
784 	} else {
785 		rcu_read_unlock();
786 
787 		WARN_ON(carl9170_mod_virtual_mac(ar, id, NULL));
788 	}
789 
790 	carl9170_update_beacon(ar, false);
791 	carl9170_flush_cab(ar, id);
792 
793 	spin_lock_bh(&ar->beacon_lock);
794 	dev_kfree_skb_any(vif_priv->beacon);
795 	vif_priv->beacon = NULL;
796 	spin_unlock_bh(&ar->beacon_lock);
797 
798 	bitmap_release_region(&ar->vif_bitmap, id, 0);
799 
800 	carl9170_set_beacon_timers(ar);
801 
802 	if (ar->vifs == 1)
803 		ar->ps.off_override &= ~PS_OFF_VIF;
804 
805 unlock:
806 	mutex_unlock(&ar->mutex);
807 
808 	synchronize_rcu();
809 }
810 
carl9170_ps_check(struct ar9170 * ar)811 void carl9170_ps_check(struct ar9170 *ar)
812 {
813 	ieee80211_queue_work(ar->hw, &ar->ps_work);
814 }
815 
816 /* caller must hold ar->mutex */
carl9170_ps_update(struct ar9170 * ar)817 static int carl9170_ps_update(struct ar9170 *ar)
818 {
819 	bool ps = false;
820 	int err = 0;
821 
822 	if (!ar->ps.off_override)
823 		ps = (ar->hw->conf.flags & IEEE80211_CONF_PS);
824 
825 	if (ps != ar->ps.state) {
826 		err = carl9170_powersave(ar, ps);
827 		if (err)
828 			return err;
829 
830 		if (ar->ps.state && !ps) {
831 			ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
832 				ar->ps.last_action);
833 		}
834 
835 		if (ps)
836 			ar->ps.last_slept = jiffies;
837 
838 		ar->ps.last_action = jiffies;
839 		ar->ps.state = ps;
840 	}
841 
842 	return 0;
843 }
844 
carl9170_ps_work(struct work_struct * work)845 static void carl9170_ps_work(struct work_struct *work)
846 {
847 	struct ar9170 *ar = container_of(work, struct ar9170,
848 					 ps_work);
849 	mutex_lock(&ar->mutex);
850 	if (IS_STARTED(ar))
851 		WARN_ON_ONCE(carl9170_ps_update(ar) != 0);
852 	mutex_unlock(&ar->mutex);
853 }
854 
carl9170_update_survey(struct ar9170 * ar,bool flush,bool noise)855 static int carl9170_update_survey(struct ar9170 *ar, bool flush, bool noise)
856 {
857 	int err;
858 
859 	if (noise) {
860 		err = carl9170_get_noisefloor(ar);
861 		if (err)
862 			return err;
863 	}
864 
865 	if (ar->fw.hw_counters) {
866 		err = carl9170_collect_tally(ar);
867 		if (err)
868 			return err;
869 	}
870 
871 	if (flush)
872 		memset(&ar->tally, 0, sizeof(ar->tally));
873 
874 	return 0;
875 }
876 
carl9170_stat_work(struct work_struct * work)877 static void carl9170_stat_work(struct work_struct *work)
878 {
879 	struct ar9170 *ar = container_of(work, struct ar9170, stat_work.work);
880 	int err;
881 
882 	mutex_lock(&ar->mutex);
883 	err = carl9170_update_survey(ar, false, true);
884 	mutex_unlock(&ar->mutex);
885 
886 	if (err)
887 		return;
888 
889 	ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
890 		round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
891 }
892 
carl9170_op_config(struct ieee80211_hw * hw,int radio_idx,u32 changed)893 static int carl9170_op_config(struct ieee80211_hw *hw, int radio_idx, u32 changed)
894 {
895 	struct ar9170 *ar = hw->priv;
896 	int err = 0;
897 
898 	mutex_lock(&ar->mutex);
899 	if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
900 		/* TODO */
901 		err = 0;
902 	}
903 
904 	if (changed & IEEE80211_CONF_CHANGE_PS) {
905 		err = carl9170_ps_update(ar);
906 		if (err)
907 			goto out;
908 	}
909 
910 	if (changed & IEEE80211_CONF_CHANGE_SMPS) {
911 		/*
912 		 * We advertise SM_PS disabled (all chains active).
913 		 * mac80211 may still request mode changes, which we
914 		 * accept but only support OFF (both chains active).
915 		 * Static/dynamic SMPS would require firmware support
916 		 * for chain control that the AR9170 does not provide.
917 		 */
918 		err = 0;
919 	}
920 
921 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
922 		enum nl80211_channel_type channel_type =
923 			cfg80211_get_chandef_type(&hw->conf.chandef);
924 
925 		/* adjust slot time for 5 GHz */
926 		err = carl9170_set_slot_time(ar);
927 		if (err)
928 			goto out;
929 
930 		err = carl9170_update_survey(ar, true, false);
931 		if (err)
932 			goto out;
933 
934 		err = carl9170_set_channel(ar, hw->conf.chandef.chan,
935 					   channel_type);
936 		if (err)
937 			goto out;
938 
939 		err = carl9170_update_survey(ar, false, true);
940 		if (err)
941 			goto out;
942 
943 		err = carl9170_set_dyn_sifs_ack(ar);
944 		if (err)
945 			goto out;
946 
947 		err = carl9170_set_rts_cts_rate(ar);
948 		if (err)
949 			goto out;
950 	}
951 
952 	if (changed & IEEE80211_CONF_CHANGE_POWER) {
953 		err = carl9170_set_mac_tpc(ar, ar->hw->conf.chandef.chan);
954 		if (err)
955 			goto out;
956 	}
957 
958 out:
959 	mutex_unlock(&ar->mutex);
960 	return err;
961 }
962 
carl9170_op_prepare_multicast(struct ieee80211_hw * hw,struct netdev_hw_addr_list * mc_list)963 static u64 carl9170_op_prepare_multicast(struct ieee80211_hw *hw,
964 					 struct netdev_hw_addr_list *mc_list)
965 {
966 	struct netdev_hw_addr *ha;
967 	u64 mchash;
968 
969 	/* always get broadcast frames */
970 	mchash = 1ULL << (0xff >> 2);
971 
972 	netdev_hw_addr_list_for_each(ha, mc_list)
973 		mchash |= 1ULL << (ha->addr[5] >> 2);
974 
975 	return mchash;
976 }
977 
carl9170_op_configure_filter(struct ieee80211_hw * hw,unsigned int changed_flags,unsigned int * new_flags,u64 multicast)978 static void carl9170_op_configure_filter(struct ieee80211_hw *hw,
979 					 unsigned int changed_flags,
980 					 unsigned int *new_flags,
981 					 u64 multicast)
982 {
983 	struct ar9170 *ar = hw->priv;
984 
985 	/* mask supported flags */
986 	*new_flags &= FIF_ALLMULTI | ar->rx_filter_caps;
987 
988 	if (!IS_ACCEPTING_CMD(ar))
989 		return;
990 
991 	mutex_lock(&ar->mutex);
992 
993 	ar->filter_state = *new_flags;
994 	/*
995 	 * We can support more by setting the sniffer bit and
996 	 * then checking the error flags, later.
997 	 */
998 
999 	if (*new_flags & FIF_ALLMULTI)
1000 		multicast = ~0ULL;
1001 
1002 	if (multicast != ar->cur_mc_hash)
1003 		WARN_ON(carl9170_update_multicast(ar, multicast));
1004 
1005 	if (changed_flags & FIF_OTHER_BSS) {
1006 		ar->sniffer_enabled = !!(*new_flags & FIF_OTHER_BSS);
1007 
1008 		WARN_ON(carl9170_set_operating_mode(ar));
1009 	}
1010 
1011 	if (ar->fw.rx_filter && changed_flags & ar->rx_filter_caps) {
1012 		u32 rx_filter = 0;
1013 
1014 		if (!ar->fw.ba_filter)
1015 			rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
1016 
1017 		if (!(*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL)))
1018 			rx_filter |= CARL9170_RX_FILTER_BAD;
1019 
1020 		if (!(*new_flags & FIF_CONTROL))
1021 			rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
1022 
1023 		if (!(*new_flags & FIF_PSPOLL))
1024 			rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
1025 
1026 		if (!(*new_flags & FIF_OTHER_BSS)) {
1027 			rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
1028 			rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
1029 		}
1030 
1031 		WARN_ON(carl9170_rx_filter(ar, rx_filter));
1032 	}
1033 
1034 	mutex_unlock(&ar->mutex);
1035 }
1036 
1037 
carl9170_op_bss_info_changed(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_bss_conf * bss_conf,u64 changed)1038 static void carl9170_op_bss_info_changed(struct ieee80211_hw *hw,
1039 					 struct ieee80211_vif *vif,
1040 					 struct ieee80211_bss_conf *bss_conf,
1041 					 u64 changed)
1042 {
1043 	struct ar9170 *ar = hw->priv;
1044 	struct ath_common *common = &ar->common;
1045 	int err = 0;
1046 	struct carl9170_vif_info *vif_priv;
1047 	struct ieee80211_vif *main_vif;
1048 
1049 	mutex_lock(&ar->mutex);
1050 	vif_priv = (void *) vif->drv_priv;
1051 	main_vif = carl9170_get_main_vif(ar);
1052 	if (WARN_ON(!main_vif))
1053 		goto out;
1054 
1055 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
1056 		struct carl9170_vif_info *iter;
1057 		int i = 0;
1058 
1059 		vif_priv->enable_beacon = bss_conf->enable_beacon;
1060 		rcu_read_lock();
1061 		list_for_each_entry_rcu(iter, &ar->vif_list, list) {
1062 			if (iter->active && iter->enable_beacon)
1063 				i++;
1064 
1065 		}
1066 		rcu_read_unlock();
1067 
1068 		ar->beacon_enabled = i;
1069 	}
1070 
1071 	if (changed & BSS_CHANGED_BEACON) {
1072 		err = carl9170_update_beacon(ar, false);
1073 		if (err)
1074 			goto out;
1075 	}
1076 
1077 	if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
1078 		       BSS_CHANGED_BEACON_INT)) {
1079 
1080 		if (main_vif != vif) {
1081 			bss_conf->beacon_int = main_vif->bss_conf.beacon_int;
1082 			bss_conf->dtim_period = main_vif->bss_conf.dtim_period;
1083 		}
1084 
1085 		/*
1086 		 * Therefore a hard limit for the broadcast traffic should
1087 		 * prevent false alarms.
1088 		 */
1089 		if (vif->type != NL80211_IFTYPE_STATION &&
1090 		    (bss_conf->beacon_int * bss_conf->dtim_period >=
1091 		     (CARL9170_QUEUE_STUCK_TIMEOUT / 2))) {
1092 			err = -EINVAL;
1093 			goto out;
1094 		}
1095 
1096 		err = carl9170_set_beacon_timers(ar);
1097 		if (err)
1098 			goto out;
1099 	}
1100 
1101 	if (changed & BSS_CHANGED_HT) {
1102 		/* TODO */
1103 		err = 0;
1104 		if (err)
1105 			goto out;
1106 	}
1107 
1108 	if (main_vif != vif)
1109 		goto out;
1110 
1111 	/*
1112 	 * The following settings can only be changed by the
1113 	 * master interface.
1114 	 */
1115 
1116 	if (changed & BSS_CHANGED_BSSID) {
1117 		memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1118 		err = carl9170_set_operating_mode(ar);
1119 		if (err)
1120 			goto out;
1121 	}
1122 
1123 	if (changed & BSS_CHANGED_ASSOC) {
1124 		ar->common.curaid = vif->cfg.aid;
1125 		err = carl9170_set_beacon_timers(ar);
1126 		if (err)
1127 			goto out;
1128 	}
1129 
1130 	if (changed & BSS_CHANGED_ERP_SLOT) {
1131 		err = carl9170_set_slot_time(ar);
1132 		if (err)
1133 			goto out;
1134 	}
1135 
1136 	if (changed & BSS_CHANGED_BASIC_RATES) {
1137 		err = carl9170_set_mac_rates(ar);
1138 		if (err)
1139 			goto out;
1140 	}
1141 
1142 out:
1143 	WARN_ON_ONCE(err && IS_STARTED(ar));
1144 	mutex_unlock(&ar->mutex);
1145 }
1146 
carl9170_op_get_tsf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)1147 static u64 carl9170_op_get_tsf(struct ieee80211_hw *hw,
1148 			       struct ieee80211_vif *vif)
1149 {
1150 	struct ar9170 *ar = hw->priv;
1151 	struct carl9170_tsf_rsp tsf;
1152 	int err;
1153 
1154 	mutex_lock(&ar->mutex);
1155 	err = carl9170_exec_cmd(ar, CARL9170_CMD_READ_TSF,
1156 				0, NULL, sizeof(tsf), &tsf);
1157 	mutex_unlock(&ar->mutex);
1158 	if (WARN_ON(err))
1159 		return 0;
1160 
1161 	return le64_to_cpu(tsf.tsf_64);
1162 }
1163 
carl9170_op_set_key(struct ieee80211_hw * hw,enum set_key_cmd cmd,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)1164 static int carl9170_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1165 			       struct ieee80211_vif *vif,
1166 			       struct ieee80211_sta *sta,
1167 			       struct ieee80211_key_conf *key)
1168 {
1169 	struct ar9170 *ar = hw->priv;
1170 	int err = 0, i;
1171 	u8 ktype;
1172 
1173 	if (ar->disable_offload || !vif)
1174 		return -EOPNOTSUPP;
1175 
1176 	/* Fall back to software encryption whenever the driver is connected
1177 	 * to more than one network.
1178 	 *
1179 	 * This is very unfortunate, because some machines cannot handle
1180 	 * the high througput speed in 802.11n networks.
1181 	 */
1182 
1183 	if (!is_main_vif(ar, vif)) {
1184 		mutex_lock(&ar->mutex);
1185 		goto err_softw;
1186 	}
1187 
1188 	/*
1189 	 * While the hardware supports *catch-all* key, for offloading
1190 	 * group-key en-/de-cryption. The way of how the hardware
1191 	 * decides which keyId maps to which key, remains a mystery...
1192 	 */
1193 	if ((vif->type != NL80211_IFTYPE_STATION &&
1194 	     vif->type != NL80211_IFTYPE_ADHOC) &&
1195 	    !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1196 		return -EOPNOTSUPP;
1197 
1198 	switch (key->cipher) {
1199 	case WLAN_CIPHER_SUITE_WEP40:
1200 		ktype = AR9170_ENC_ALG_WEP64;
1201 		break;
1202 	case WLAN_CIPHER_SUITE_WEP104:
1203 		ktype = AR9170_ENC_ALG_WEP128;
1204 		break;
1205 	case WLAN_CIPHER_SUITE_TKIP:
1206 		ktype = AR9170_ENC_ALG_TKIP;
1207 		break;
1208 	case WLAN_CIPHER_SUITE_CCMP:
1209 		ktype = AR9170_ENC_ALG_AESCCMP;
1210 		key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1211 		break;
1212 	default:
1213 		return -EOPNOTSUPP;
1214 	}
1215 
1216 	mutex_lock(&ar->mutex);
1217 	if (cmd == SET_KEY) {
1218 		if (!IS_STARTED(ar)) {
1219 			err = -EOPNOTSUPP;
1220 			goto out;
1221 		}
1222 
1223 		if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1224 			sta = NULL;
1225 
1226 			i = 64 + key->keyidx;
1227 		} else {
1228 			for (i = 0; i < 64; i++)
1229 				if (!(ar->usedkeys & BIT(i)))
1230 					break;
1231 			if (i == 64)
1232 				goto err_softw;
1233 		}
1234 
1235 		key->hw_key_idx = i;
1236 
1237 		err = carl9170_upload_key(ar, i, sta ? sta->addr : NULL,
1238 					  ktype, 0, key->key,
1239 					  min_t(u8, 16, key->keylen));
1240 		if (err)
1241 			goto out;
1242 
1243 		if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1244 			err = carl9170_upload_key(ar, i, sta ? sta->addr :
1245 						  NULL, ktype, 1,
1246 						  key->key + 16, 16);
1247 			if (err)
1248 				goto out;
1249 
1250 			/*
1251 			 * hardware is not capable generating MMIC
1252 			 * of fragmented frames!
1253 			 */
1254 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1255 		}
1256 
1257 		if (i < 64)
1258 			ar->usedkeys |= BIT(i);
1259 
1260 		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1261 	} else {
1262 		if (!IS_STARTED(ar)) {
1263 			/* The device is gone... together with the key ;-) */
1264 			err = 0;
1265 			goto out;
1266 		}
1267 
1268 		if (key->hw_key_idx < 64) {
1269 			ar->usedkeys &= ~BIT(key->hw_key_idx);
1270 		} else {
1271 			err = carl9170_upload_key(ar, key->hw_key_idx, NULL,
1272 						  AR9170_ENC_ALG_NONE, 0,
1273 						  NULL, 0);
1274 			if (err)
1275 				goto out;
1276 
1277 			if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
1278 				err = carl9170_upload_key(ar, key->hw_key_idx,
1279 							  NULL,
1280 							  AR9170_ENC_ALG_NONE,
1281 							  1, NULL, 0);
1282 				if (err)
1283 					goto out;
1284 			}
1285 
1286 		}
1287 
1288 		err = carl9170_disable_key(ar, key->hw_key_idx);
1289 		if (err)
1290 			goto out;
1291 	}
1292 
1293 out:
1294 	mutex_unlock(&ar->mutex);
1295 	return err;
1296 
1297 err_softw:
1298 	if (!ar->rx_software_decryption) {
1299 		ar->rx_software_decryption = true;
1300 		carl9170_set_operating_mode(ar);
1301 	}
1302 	mutex_unlock(&ar->mutex);
1303 	return -ENOSPC;
1304 }
1305 
carl9170_op_sta_add(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1306 static int carl9170_op_sta_add(struct ieee80211_hw *hw,
1307 			       struct ieee80211_vif *vif,
1308 			       struct ieee80211_sta *sta)
1309 {
1310 	struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1311 	unsigned int i;
1312 
1313 	atomic_set(&sta_info->pending_frames, 0);
1314 
1315 	if (sta->deflink.ht_cap.ht_supported) {
1316 		if (sta->deflink.ht_cap.ampdu_density > 6) {
1317 			/*
1318 			 * HW does support 16us AMPDU density.
1319 			 * No HT-Xmit for station.
1320 			 */
1321 
1322 			return 0;
1323 		}
1324 
1325 		for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++)
1326 			RCU_INIT_POINTER(sta_info->agg[i], NULL);
1327 
1328 		sta_info->ampdu_max_len = 1 << (3 + sta->deflink.ht_cap.ampdu_factor);
1329 		sta_info->ht_sta = true;
1330 	}
1331 
1332 	return 0;
1333 }
1334 
carl9170_op_sta_remove(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)1335 static int carl9170_op_sta_remove(struct ieee80211_hw *hw,
1336 				struct ieee80211_vif *vif,
1337 				struct ieee80211_sta *sta)
1338 {
1339 	struct ar9170 *ar = hw->priv;
1340 	struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1341 	unsigned int i;
1342 	bool cleanup = false;
1343 
1344 	if (sta->deflink.ht_cap.ht_supported) {
1345 
1346 		sta_info->ht_sta = false;
1347 
1348 		rcu_read_lock();
1349 		for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++) {
1350 			struct carl9170_sta_tid *tid_info;
1351 
1352 			tid_info = rcu_dereference(sta_info->agg[i]);
1353 			RCU_INIT_POINTER(sta_info->agg[i], NULL);
1354 
1355 			if (!tid_info)
1356 				continue;
1357 
1358 			spin_lock_bh(&ar->tx_ampdu_list_lock);
1359 			if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1360 				tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1361 			spin_unlock_bh(&ar->tx_ampdu_list_lock);
1362 			cleanup = true;
1363 		}
1364 		rcu_read_unlock();
1365 
1366 		if (cleanup)
1367 			carl9170_ampdu_gc(ar);
1368 	}
1369 
1370 	return 0;
1371 }
1372 
carl9170_op_conf_tx(struct ieee80211_hw * hw,struct ieee80211_vif * vif,unsigned int link_id,u16 queue,const struct ieee80211_tx_queue_params * param)1373 static int carl9170_op_conf_tx(struct ieee80211_hw *hw,
1374 			       struct ieee80211_vif *vif,
1375 			       unsigned int link_id, u16 queue,
1376 			       const struct ieee80211_tx_queue_params *param)
1377 {
1378 	struct ar9170 *ar = hw->priv;
1379 	int ret;
1380 
1381 	mutex_lock(&ar->mutex);
1382 	memcpy(&ar->edcf[ar9170_qmap(queue)], param, sizeof(*param));
1383 	ret = carl9170_set_qos(ar);
1384 	mutex_unlock(&ar->mutex);
1385 	return ret;
1386 }
1387 
carl9170_ampdu_work(struct work_struct * work)1388 static void carl9170_ampdu_work(struct work_struct *work)
1389 {
1390 	struct ar9170 *ar = container_of(work, struct ar9170,
1391 					 ampdu_work);
1392 
1393 	if (!IS_STARTED(ar))
1394 		return;
1395 
1396 	mutex_lock(&ar->mutex);
1397 	carl9170_ampdu_gc(ar);
1398 	mutex_unlock(&ar->mutex);
1399 }
1400 
carl9170_op_ampdu_action(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_ampdu_params * params)1401 static int carl9170_op_ampdu_action(struct ieee80211_hw *hw,
1402 				    struct ieee80211_vif *vif,
1403 				    struct ieee80211_ampdu_params *params)
1404 {
1405 	struct ieee80211_sta *sta = params->sta;
1406 	enum ieee80211_ampdu_mlme_action action = params->action;
1407 	u16 tid = params->tid;
1408 	u16 *ssn = &params->ssn;
1409 	struct ar9170 *ar = hw->priv;
1410 	struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1411 	struct carl9170_sta_tid *tid_info;
1412 
1413 	if (modparam_noht)
1414 		return -EOPNOTSUPP;
1415 
1416 	switch (action) {
1417 	case IEEE80211_AMPDU_TX_START:
1418 		if (!sta_info->ht_sta)
1419 			return -EOPNOTSUPP;
1420 
1421 		tid_info = kzalloc_obj(struct carl9170_sta_tid);
1422 		if (!tid_info)
1423 			return -ENOMEM;
1424 
1425 		tid_info->hsn = tid_info->bsn = tid_info->snx = (*ssn);
1426 		tid_info->state = CARL9170_TID_STATE_PROGRESS;
1427 		tid_info->tid = tid;
1428 		tid_info->max = sta_info->ampdu_max_len;
1429 		tid_info->sta = sta;
1430 		tid_info->vif = vif;
1431 
1432 		INIT_LIST_HEAD(&tid_info->list);
1433 		INIT_LIST_HEAD(&tid_info->tmp_list);
1434 		skb_queue_head_init(&tid_info->queue);
1435 		spin_lock_init(&tid_info->lock);
1436 
1437 		spin_lock_bh(&ar->tx_ampdu_list_lock);
1438 		ar->tx_ampdu_list_len++;
1439 		list_add_tail_rcu(&tid_info->list, &ar->tx_ampdu_list);
1440 		rcu_assign_pointer(sta_info->agg[tid], tid_info);
1441 		spin_unlock_bh(&ar->tx_ampdu_list_lock);
1442 
1443 		return IEEE80211_AMPDU_TX_START_IMMEDIATE;
1444 
1445 	case IEEE80211_AMPDU_TX_STOP_CONT:
1446 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1447 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1448 		rcu_read_lock();
1449 		tid_info = rcu_dereference(sta_info->agg[tid]);
1450 		if (tid_info) {
1451 			spin_lock_bh(&ar->tx_ampdu_list_lock);
1452 			if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
1453 				tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
1454 			spin_unlock_bh(&ar->tx_ampdu_list_lock);
1455 		}
1456 
1457 		RCU_INIT_POINTER(sta_info->agg[tid], NULL);
1458 		rcu_read_unlock();
1459 
1460 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1461 		ieee80211_queue_work(ar->hw, &ar->ampdu_work);
1462 		break;
1463 
1464 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1465 		rcu_read_lock();
1466 		tid_info = rcu_dereference(sta_info->agg[tid]);
1467 
1468 		sta_info->stats[tid].clear = true;
1469 		sta_info->stats[tid].req = false;
1470 
1471 		if (tid_info) {
1472 			bitmap_zero(tid_info->bitmap, CARL9170_BAW_SIZE);
1473 			tid_info->state = CARL9170_TID_STATE_IDLE;
1474 		}
1475 		rcu_read_unlock();
1476 
1477 		if (WARN_ON_ONCE(!tid_info))
1478 			return -EFAULT;
1479 
1480 		break;
1481 
1482 	case IEEE80211_AMPDU_RX_START:
1483 	case IEEE80211_AMPDU_RX_STOP:
1484 		/* Handled by hardware */
1485 		break;
1486 
1487 	default:
1488 		return -EOPNOTSUPP;
1489 	}
1490 
1491 	return 0;
1492 }
1493 
1494 #ifdef CONFIG_CARL9170_WPC
carl9170_register_wps_button(struct ar9170 * ar)1495 static int carl9170_register_wps_button(struct ar9170 *ar)
1496 {
1497 	struct input_dev *input;
1498 	int err;
1499 
1500 	if (!(ar->features & CARL9170_WPS_BUTTON))
1501 		return 0;
1502 
1503 	input = devm_input_allocate_device(&ar->udev->dev);
1504 	if (!input)
1505 		return -ENOMEM;
1506 
1507 	snprintf(ar->wps.name, sizeof(ar->wps.name), "%s WPS Button",
1508 		 wiphy_name(ar->hw->wiphy));
1509 
1510 	snprintf(ar->wps.phys, sizeof(ar->wps.phys),
1511 		 "ieee80211/%s/input0", wiphy_name(ar->hw->wiphy));
1512 
1513 	input->name = ar->wps.name;
1514 	input->phys = ar->wps.phys;
1515 	input->id.bustype = BUS_USB;
1516 	input->dev.parent = &ar->hw->wiphy->dev;
1517 
1518 	input_set_capability(input, EV_KEY, KEY_WPS_BUTTON);
1519 
1520 	err = input_register_device(input);
1521 	if (err)
1522 		return err;
1523 
1524 	ar->wps.pbc = input;
1525 	return 0;
1526 }
1527 #endif /* CONFIG_CARL9170_WPC */
1528 
1529 #ifdef CONFIG_CARL9170_HWRNG
carl9170_rng_get(struct ar9170 * ar)1530 static int carl9170_rng_get(struct ar9170 *ar)
1531 {
1532 
1533 #define RW	(CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
1534 #define RB	(CARL9170_MAX_CMD_PAYLOAD_LEN)
1535 
1536 	static const __le32 rng_load[RW] = {
1537 		[0 ... (RW - 1)] = cpu_to_le32(AR9170_RAND_REG_NUM)};
1538 
1539 	u32 buf[RW];
1540 
1541 	unsigned int i, off = 0, transfer, count;
1542 	int err;
1543 
1544 	BUILD_BUG_ON(RB > CARL9170_MAX_CMD_PAYLOAD_LEN);
1545 
1546 	if (!IS_ACCEPTING_CMD(ar))
1547 		return -EAGAIN;
1548 
1549 	count = ARRAY_SIZE(ar->rng.cache);
1550 	while (count) {
1551 		err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1552 					RB, (u8 *) rng_load,
1553 					RB, (u8 *) buf);
1554 		if (err)
1555 			return err;
1556 
1557 		transfer = min_t(unsigned int, count, RW);
1558 		for (i = 0; i < transfer; i++)
1559 			ar->rng.cache[off + i] = buf[i];
1560 
1561 		off += transfer;
1562 		count -= transfer;
1563 	}
1564 
1565 	ar->rng.cache_idx = 0;
1566 
1567 #undef RW
1568 #undef RB
1569 	return 0;
1570 }
1571 
carl9170_rng_read(struct hwrng * rng,u32 * data)1572 static int carl9170_rng_read(struct hwrng *rng, u32 *data)
1573 {
1574 	struct ar9170 *ar = (struct ar9170 *)rng->priv;
1575 	int ret = -EIO;
1576 
1577 	mutex_lock(&ar->mutex);
1578 	if (ar->rng.cache_idx >= ARRAY_SIZE(ar->rng.cache)) {
1579 		ret = carl9170_rng_get(ar);
1580 		if (ret) {
1581 			mutex_unlock(&ar->mutex);
1582 			return ret;
1583 		}
1584 	}
1585 
1586 	*data = ar->rng.cache[ar->rng.cache_idx++];
1587 	mutex_unlock(&ar->mutex);
1588 
1589 	return sizeof(u16);
1590 }
1591 
carl9170_register_hwrng(struct ar9170 * ar)1592 static int carl9170_register_hwrng(struct ar9170 *ar)
1593 {
1594 	int err;
1595 
1596 	snprintf(ar->rng.name, ARRAY_SIZE(ar->rng.name),
1597 		 "%s_%s", KBUILD_MODNAME, wiphy_name(ar->hw->wiphy));
1598 	ar->rng.rng.name = ar->rng.name;
1599 	ar->rng.rng.data_read = carl9170_rng_read;
1600 	ar->rng.rng.priv = (unsigned long)ar;
1601 
1602 	err = devm_hwrng_register(&ar->udev->dev, &ar->rng.rng);
1603 	if (err) {
1604 		dev_err(&ar->udev->dev, "Failed to register the random "
1605 			"number generator (%d)\n", err);
1606 		return err;
1607 	}
1608 
1609 	return carl9170_rng_get(ar);
1610 }
1611 #endif /* CONFIG_CARL9170_HWRNG */
1612 
carl9170_op_get_survey(struct ieee80211_hw * hw,int idx,struct survey_info * survey)1613 static int carl9170_op_get_survey(struct ieee80211_hw *hw, int idx,
1614 				struct survey_info *survey)
1615 {
1616 	struct ar9170 *ar = hw->priv;
1617 	struct ieee80211_channel *chan;
1618 	struct ieee80211_supported_band *band;
1619 	int err, b, i;
1620 
1621 	chan = ar->channel;
1622 	if (!chan)
1623 		return -ENODEV;
1624 
1625 	if (idx == chan->hw_value) {
1626 		mutex_lock(&ar->mutex);
1627 		err = carl9170_update_survey(ar, false, true);
1628 		mutex_unlock(&ar->mutex);
1629 		if (err)
1630 			return err;
1631 	}
1632 
1633 	for (b = 0; b < NUM_NL80211_BANDS; b++) {
1634 		band = ar->hw->wiphy->bands[b];
1635 
1636 		if (!band)
1637 			continue;
1638 
1639 		for (i = 0; i < band->n_channels; i++) {
1640 			if (band->channels[i].hw_value == idx) {
1641 				chan = &band->channels[i];
1642 				goto found;
1643 			}
1644 		}
1645 	}
1646 	return -ENOENT;
1647 
1648 found:
1649 	memcpy(survey, &ar->survey[idx], sizeof(*survey));
1650 
1651 	survey->channel = chan;
1652 	survey->filled = SURVEY_INFO_NOISE_DBM;
1653 
1654 	if (ar->channel == chan)
1655 		survey->filled |= SURVEY_INFO_IN_USE;
1656 
1657 	if (ar->fw.hw_counters) {
1658 		survey->filled |= SURVEY_INFO_TIME |
1659 				  SURVEY_INFO_TIME_BUSY |
1660 				  SURVEY_INFO_TIME_TX;
1661 	}
1662 
1663 	return 0;
1664 }
1665 
carl9170_op_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)1666 static void carl9170_op_flush(struct ieee80211_hw *hw,
1667 			      struct ieee80211_vif *vif,
1668 			      u32 queues, bool drop)
1669 {
1670 	struct ar9170 *ar = hw->priv;
1671 	unsigned int vid;
1672 
1673 	mutex_lock(&ar->mutex);
1674 	for_each_set_bit(vid, &ar->vif_bitmap, ar->fw.vif_num)
1675 		carl9170_flush_cab(ar, vid);
1676 
1677 	carl9170_flush(ar, drop);
1678 	mutex_unlock(&ar->mutex);
1679 }
1680 
carl9170_op_get_stats(struct ieee80211_hw * hw,struct ieee80211_low_level_stats * stats)1681 static int carl9170_op_get_stats(struct ieee80211_hw *hw,
1682 				 struct ieee80211_low_level_stats *stats)
1683 {
1684 	struct ar9170 *ar = hw->priv;
1685 
1686 	memset(stats, 0, sizeof(*stats));
1687 	stats->dot11ACKFailureCount = ar->tx_ack_failures;
1688 	stats->dot11FCSErrorCount = ar->tx_fcs_errors;
1689 	return 0;
1690 }
1691 
carl9170_op_sta_notify(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum sta_notify_cmd cmd,struct ieee80211_sta * sta)1692 static void carl9170_op_sta_notify(struct ieee80211_hw *hw,
1693 				   struct ieee80211_vif *vif,
1694 				   enum sta_notify_cmd cmd,
1695 				   struct ieee80211_sta *sta)
1696 {
1697 	struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
1698 
1699 	switch (cmd) {
1700 	case STA_NOTIFY_SLEEP:
1701 		sta_info->sleeping = true;
1702 		if (atomic_read(&sta_info->pending_frames))
1703 			ieee80211_sta_block_awake(hw, sta, true);
1704 		break;
1705 
1706 	case STA_NOTIFY_AWAKE:
1707 		sta_info->sleeping = false;
1708 		break;
1709 	}
1710 }
1711 
carl9170_tx_frames_pending(struct ieee80211_hw * hw)1712 static bool carl9170_tx_frames_pending(struct ieee80211_hw *hw)
1713 {
1714 	struct ar9170 *ar = hw->priv;
1715 
1716 	return !!atomic_read(&ar->tx_total_queued);
1717 }
1718 
1719 static const struct ieee80211_ops carl9170_ops = {
1720 	.add_chanctx = ieee80211_emulate_add_chanctx,
1721 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
1722 	.change_chanctx = ieee80211_emulate_change_chanctx,
1723 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
1724 	.start			= carl9170_op_start,
1725 	.stop			= carl9170_op_stop,
1726 	.tx			= carl9170_op_tx,
1727 	.wake_tx_queue		= ieee80211_handle_wake_tx_queue,
1728 	.flush			= carl9170_op_flush,
1729 	.add_interface		= carl9170_op_add_interface,
1730 	.remove_interface	= carl9170_op_remove_interface,
1731 	.config			= carl9170_op_config,
1732 	.prepare_multicast	= carl9170_op_prepare_multicast,
1733 	.configure_filter	= carl9170_op_configure_filter,
1734 	.conf_tx		= carl9170_op_conf_tx,
1735 	.bss_info_changed	= carl9170_op_bss_info_changed,
1736 	.get_tsf		= carl9170_op_get_tsf,
1737 	.set_key		= carl9170_op_set_key,
1738 	.sta_add		= carl9170_op_sta_add,
1739 	.sta_remove		= carl9170_op_sta_remove,
1740 	.sta_notify		= carl9170_op_sta_notify,
1741 	.get_survey		= carl9170_op_get_survey,
1742 	.get_stats		= carl9170_op_get_stats,
1743 	.ampdu_action		= carl9170_op_ampdu_action,
1744 	.tx_frames_pending	= carl9170_tx_frames_pending,
1745 };
1746 
carl9170_alloc(size_t priv_size)1747 void *carl9170_alloc(size_t priv_size)
1748 {
1749 	struct ieee80211_hw *hw;
1750 	struct ar9170 *ar;
1751 	struct sk_buff *skb;
1752 	int i;
1753 
1754 	/*
1755 	 * this buffer is used for rx stream reconstruction.
1756 	 * Under heavy load this device (or the transport layer?)
1757 	 * tends to split the streams into separate rx descriptors.
1758 	 */
1759 
1760 	skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
1761 	if (!skb)
1762 		goto err_nomem;
1763 
1764 	hw = ieee80211_alloc_hw(priv_size, &carl9170_ops);
1765 	if (!hw)
1766 		goto err_nomem;
1767 
1768 	ar = hw->priv;
1769 	ar->hw = hw;
1770 	ar->rx_failover = skb;
1771 
1772 	memset(&ar->rx_plcp, 0, sizeof(struct ar9170_rx_head));
1773 	ar->rx_has_plcp = false;
1774 
1775 	/*
1776 	 * Here's a hidden pitfall!
1777 	 *
1778 	 * All 4 AC queues work perfectly well under _legacy_ operation.
1779 	 * However as soon as aggregation is enabled, the traffic flow
1780 	 * gets very bumpy. Therefore we have to _switch_ to a
1781 	 * software AC with a single HW queue.
1782 	 */
1783 	hw->queues = __AR9170_NUM_TXQ;
1784 
1785 	mutex_init(&ar->mutex);
1786 	spin_lock_init(&ar->beacon_lock);
1787 	spin_lock_init(&ar->cmd_lock);
1788 	spin_lock_init(&ar->tx_stats_lock);
1789 	spin_lock_init(&ar->tx_ampdu_list_lock);
1790 	spin_lock_init(&ar->mem_lock);
1791 	spin_lock_init(&ar->state_lock);
1792 	atomic_set(&ar->pending_restarts, 0);
1793 	ar->vifs = 0;
1794 	for (i = 0; i < ar->hw->queues; i++) {
1795 		skb_queue_head_init(&ar->tx_status[i]);
1796 		skb_queue_head_init(&ar->tx_pending[i]);
1797 
1798 		INIT_LIST_HEAD(&ar->bar_list[i]);
1799 		spin_lock_init(&ar->bar_list_lock[i]);
1800 	}
1801 	INIT_WORK(&ar->ps_work, carl9170_ps_work);
1802 	INIT_WORK(&ar->ping_work, carl9170_ping_work);
1803 	INIT_WORK(&ar->restart_work, carl9170_restart_work);
1804 	INIT_WORK(&ar->ampdu_work, carl9170_ampdu_work);
1805 	INIT_DELAYED_WORK(&ar->stat_work, carl9170_stat_work);
1806 	INIT_DELAYED_WORK(&ar->tx_janitor, carl9170_tx_janitor);
1807 	INIT_LIST_HEAD(&ar->tx_ampdu_list);
1808 	rcu_assign_pointer(ar->tx_ampdu_iter,
1809 			   (struct carl9170_sta_tid *) &ar->tx_ampdu_list);
1810 
1811 	bitmap_zero(&ar->vif_bitmap, ar->fw.vif_num);
1812 	INIT_LIST_HEAD(&ar->vif_list);
1813 	init_completion(&ar->tx_flush);
1814 
1815 	/* firmware decides which modes we support */
1816 	hw->wiphy->interface_modes = 0;
1817 
1818 	ieee80211_hw_set(hw, RX_INCLUDES_FCS);
1819 	ieee80211_hw_set(hw, MFP_CAPABLE);
1820 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
1821 	ieee80211_hw_set(hw, SUPPORTS_PS);
1822 	ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
1823 	ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
1824 	ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
1825 	ieee80211_hw_set(hw, SIGNAL_DBM);
1826 	ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
1827 
1828 	if (!modparam_noht) {
1829 		/*
1830 		 * see the comment above, why we allow the user
1831 		 * to disable HT by a module parameter.
1832 		 */
1833 		ieee80211_hw_set(hw, AMPDU_AGGREGATION);
1834 	}
1835 
1836 	hw->extra_tx_headroom = sizeof(struct _carl9170_tx_superframe);
1837 	hw->sta_data_size = sizeof(struct carl9170_sta_info);
1838 	hw->vif_data_size = sizeof(struct carl9170_vif_info);
1839 
1840 	hw->max_rates = CARL9170_TX_MAX_RATES;
1841 	hw->max_rate_tries = CARL9170_TX_USER_RATE_TRIES;
1842 
1843 	for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
1844 		ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
1845 
1846 	wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
1847 
1848 	return ar;
1849 
1850 err_nomem:
1851 	kfree_skb(skb);
1852 	return ERR_PTR(-ENOMEM);
1853 }
1854 
carl9170_read_eeprom(struct ar9170 * ar)1855 static int carl9170_read_eeprom(struct ar9170 *ar)
1856 {
1857 #define RW	8	/* number of words to read at once */
1858 #define RB	(sizeof(u32) * RW)
1859 	u8 *eeprom = (void *)&ar->eeprom;
1860 	__le32 offsets[RW];
1861 	int i, j, err;
1862 
1863 	BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
1864 
1865 	BUILD_BUG_ON(RB > CARL9170_MAX_CMD_LEN - 4);
1866 #ifndef __CHECKER__
1867 	/* don't want to handle trailing remains */
1868 	BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
1869 #endif
1870 
1871 	for (i = 0; i < sizeof(ar->eeprom) / RB; i++) {
1872 		for (j = 0; j < RW; j++)
1873 			offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
1874 						 RB * i + 4 * j);
1875 
1876 		err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
1877 					RB, (u8 *) &offsets,
1878 					RB, eeprom + RB * i);
1879 		if (err)
1880 			return err;
1881 	}
1882 
1883 #undef RW
1884 #undef RB
1885 	return 0;
1886 }
1887 
carl9170_parse_eeprom(struct ar9170 * ar)1888 static int carl9170_parse_eeprom(struct ar9170 *ar)
1889 {
1890 	struct ath_regulatory *regulatory = &ar->common.regulatory;
1891 	unsigned int rx_streams, tx_streams, tx_params = 0;
1892 	int bands = 0;
1893 	int chans = 0;
1894 
1895 	if (ar->eeprom.length == cpu_to_le16(0xffff))
1896 		return -ENODATA;
1897 
1898 	rx_streams = hweight8(ar->eeprom.rx_mask);
1899 	tx_streams = hweight8(ar->eeprom.tx_mask);
1900 
1901 	if (rx_streams != tx_streams) {
1902 		tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
1903 
1904 		WARN_ON(!(tx_streams >= 1 && tx_streams <=
1905 			IEEE80211_HT_MCS_TX_MAX_STREAMS));
1906 
1907 		tx_params |= (tx_streams - 1) <<
1908 			    IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1909 
1910 		carl9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
1911 		carl9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
1912 	}
1913 
1914 	if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
1915 		ar->hw->wiphy->bands[NL80211_BAND_2GHZ] =
1916 			&carl9170_band_2GHz;
1917 		chans += carl9170_band_2GHz.n_channels;
1918 		bands++;
1919 	}
1920 	if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
1921 		ar->hw->wiphy->bands[NL80211_BAND_5GHZ] =
1922 			&carl9170_band_5GHz;
1923 		chans += carl9170_band_5GHz.n_channels;
1924 		bands++;
1925 	}
1926 
1927 	if (!bands)
1928 		return -EINVAL;
1929 
1930 	ar->survey = devm_kcalloc(&ar->udev->dev, chans,
1931 				  sizeof(struct survey_info), GFP_KERNEL);
1932 	if (!ar->survey)
1933 		return -ENOMEM;
1934 	ar->num_channels = chans;
1935 
1936 	regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
1937 
1938 	/* second part of wiphy init */
1939 	SET_IEEE80211_PERM_ADDR(ar->hw, ar->eeprom.mac_address);
1940 
1941 	return 0;
1942 }
1943 
carl9170_reg_notifier(struct wiphy * wiphy,struct regulatory_request * request)1944 static void carl9170_reg_notifier(struct wiphy *wiphy,
1945 				  struct regulatory_request *request)
1946 {
1947 	struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
1948 	struct ar9170 *ar = hw->priv;
1949 
1950 	ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
1951 }
1952 
carl9170_register(struct ar9170 * ar)1953 int carl9170_register(struct ar9170 *ar)
1954 {
1955 	struct ath_regulatory *regulatory = &ar->common.regulatory;
1956 	int err = 0, i;
1957 
1958 	ar->mem_bitmap = devm_bitmap_zalloc(&ar->udev->dev, ar->fw.mem_blocks, GFP_KERNEL);
1959 	if (!ar->mem_bitmap)
1960 		return -ENOMEM;
1961 
1962 	/* try to read EEPROM, init MAC addr */
1963 	err = carl9170_read_eeprom(ar);
1964 	if (err)
1965 		return err;
1966 
1967 	err = carl9170_parse_eeprom(ar);
1968 	if (err)
1969 		return err;
1970 
1971 	err = ath_regd_init(regulatory, ar->hw->wiphy,
1972 			    carl9170_reg_notifier);
1973 	if (err)
1974 		return err;
1975 
1976 	if (modparam_noht) {
1977 		carl9170_band_2GHz.ht_cap.ht_supported = false;
1978 		carl9170_band_5GHz.ht_cap.ht_supported = false;
1979 	}
1980 
1981 	for (i = 0; i < ar->fw.vif_num; i++) {
1982 		ar->vif_priv[i].id = i;
1983 		ar->vif_priv[i].vif = NULL;
1984 	}
1985 
1986 	err = ieee80211_register_hw(ar->hw);
1987 	if (err)
1988 		return err;
1989 
1990 	/* mac80211 interface is now registered */
1991 	ar->registered = true;
1992 
1993 	if (!ath_is_world_regd(regulatory))
1994 		regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
1995 
1996 #ifdef CONFIG_CARL9170_DEBUGFS
1997 	carl9170_debugfs_register(ar);
1998 #endif /* CONFIG_CARL9170_DEBUGFS */
1999 
2000 	err = carl9170_led_init(ar);
2001 	if (err)
2002 		goto err_unreg;
2003 
2004 #ifdef CONFIG_CARL9170_LEDS
2005 	err = carl9170_led_register(ar);
2006 	if (err)
2007 		goto err_unreg;
2008 #endif /* CONFIG_CARL9170_LEDS */
2009 
2010 #ifdef CONFIG_CARL9170_WPC
2011 	err = carl9170_register_wps_button(ar);
2012 	if (err)
2013 		goto err_unreg;
2014 #endif /* CONFIG_CARL9170_WPC */
2015 
2016 #ifdef CONFIG_CARL9170_HWRNG
2017 	err = carl9170_register_hwrng(ar);
2018 	if (err)
2019 		goto err_unreg;
2020 #endif /* CONFIG_CARL9170_HWRNG */
2021 
2022 	dev_info(&ar->udev->dev, "Atheros AR9170 is registered as '%s'\n",
2023 		 wiphy_name(ar->hw->wiphy));
2024 
2025 	return 0;
2026 
2027 err_unreg:
2028 	carl9170_unregister(ar);
2029 	return err;
2030 }
2031 
carl9170_unregister(struct ar9170 * ar)2032 void carl9170_unregister(struct ar9170 *ar)
2033 {
2034 	if (!ar->registered)
2035 		return;
2036 
2037 	ar->registered = false;
2038 
2039 #ifdef CONFIG_CARL9170_LEDS
2040 	carl9170_led_unregister(ar);
2041 #endif /* CONFIG_CARL9170_LEDS */
2042 
2043 #ifdef CONFIG_CARL9170_DEBUGFS
2044 	carl9170_debugfs_unregister(ar);
2045 #endif /* CONFIG_CARL9170_DEBUGFS */
2046 
2047 	carl9170_cancel_worker(ar);
2048 	cancel_work_sync(&ar->restart_work);
2049 
2050 	ieee80211_unregister_hw(ar->hw);
2051 }
2052 
carl9170_free(struct ar9170 * ar)2053 void carl9170_free(struct ar9170 *ar)
2054 {
2055 	WARN_ON(ar->registered);
2056 	WARN_ON(IS_INITIALIZED(ar));
2057 
2058 	kfree_skb(ar->rx_failover);
2059 	ar->rx_failover = NULL;
2060 
2061 	mutex_destroy(&ar->mutex);
2062 
2063 	ieee80211_free_hw(ar->hw);
2064 }
2065