Loading...
1/*
2 * Copyright (c) 2010-2011 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "htc.h"
18
19/*************/
20/* Utilities */
21/*************/
22
23/* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
24static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
25 struct ath9k_channel *ichan)
26{
27 enum htc_phymode mode;
28
29 mode = -EINVAL;
30
31 switch (ichan->chanmode) {
32 case CHANNEL_G:
33 case CHANNEL_G_HT20:
34 case CHANNEL_G_HT40PLUS:
35 case CHANNEL_G_HT40MINUS:
36 mode = HTC_MODE_11NG;
37 break;
38 case CHANNEL_A:
39 case CHANNEL_A_HT20:
40 case CHANNEL_A_HT40PLUS:
41 case CHANNEL_A_HT40MINUS:
42 mode = HTC_MODE_11NA;
43 break;
44 default:
45 break;
46 }
47
48 WARN_ON(mode < 0);
49
50 return mode;
51}
52
53bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
54 enum ath9k_power_mode mode)
55{
56 bool ret;
57
58 mutex_lock(&priv->htc_pm_lock);
59 ret = ath9k_hw_setpower(priv->ah, mode);
60 mutex_unlock(&priv->htc_pm_lock);
61
62 return ret;
63}
64
65void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
66{
67 mutex_lock(&priv->htc_pm_lock);
68 if (++priv->ps_usecount != 1)
69 goto unlock;
70 ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);
71
72unlock:
73 mutex_unlock(&priv->htc_pm_lock);
74}
75
76void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
77{
78 mutex_lock(&priv->htc_pm_lock);
79 if (--priv->ps_usecount != 0)
80 goto unlock;
81
82 if (priv->ps_idle)
83 ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
84 else if (priv->ps_enabled)
85 ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
86
87unlock:
88 mutex_unlock(&priv->htc_pm_lock);
89}
90
91void ath9k_ps_work(struct work_struct *work)
92{
93 struct ath9k_htc_priv *priv =
94 container_of(work, struct ath9k_htc_priv,
95 ps_work);
96 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
97
98 /* The chip wakes up after receiving the first beacon
99 while network sleep is enabled. For the driver to
100 be in sync with the hw, set the chip to awake and
101 only then set it to sleep.
102 */
103 ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
104}
105
106static void ath9k_htc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
107{
108 struct ath9k_htc_priv *priv = data;
109 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
110
111 if ((vif->type == NL80211_IFTYPE_AP) && bss_conf->enable_beacon)
112 priv->reconfig_beacon = true;
113
114 if (bss_conf->assoc) {
115 priv->rearm_ani = true;
116 priv->reconfig_beacon = true;
117 }
118}
119
120static void ath9k_htc_vif_reconfig(struct ath9k_htc_priv *priv)
121{
122 priv->rearm_ani = false;
123 priv->reconfig_beacon = false;
124
125 ieee80211_iterate_active_interfaces_atomic(priv->hw,
126 ath9k_htc_vif_iter, priv);
127 if (priv->rearm_ani)
128 ath9k_htc_start_ani(priv);
129
130 if (priv->reconfig_beacon) {
131 ath9k_htc_ps_wakeup(priv);
132 ath9k_htc_beacon_reconfig(priv);
133 ath9k_htc_ps_restore(priv);
134 }
135}
136
137static void ath9k_htc_bssid_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
138{
139 struct ath9k_vif_iter_data *iter_data = data;
140 int i;
141
142 for (i = 0; i < ETH_ALEN; i++)
143 iter_data->mask[i] &= ~(iter_data->hw_macaddr[i] ^ mac[i]);
144}
145
146static void ath9k_htc_set_bssid_mask(struct ath9k_htc_priv *priv,
147 struct ieee80211_vif *vif)
148{
149 struct ath_common *common = ath9k_hw_common(priv->ah);
150 struct ath9k_vif_iter_data iter_data;
151
152 /*
153 * Use the hardware MAC address as reference, the hardware uses it
154 * together with the BSSID mask when matching addresses.
155 */
156 iter_data.hw_macaddr = common->macaddr;
157 memset(&iter_data.mask, 0xff, ETH_ALEN);
158
159 if (vif)
160 ath9k_htc_bssid_iter(&iter_data, vif->addr, vif);
161
162 /* Get list of all active MAC addresses */
163 ieee80211_iterate_active_interfaces_atomic(priv->hw, ath9k_htc_bssid_iter,
164 &iter_data);
165
166 memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
167 ath_hw_setbssidmask(common);
168}
169
170static void ath9k_htc_set_opmode(struct ath9k_htc_priv *priv)
171{
172 if (priv->num_ibss_vif)
173 priv->ah->opmode = NL80211_IFTYPE_ADHOC;
174 else if (priv->num_ap_vif)
175 priv->ah->opmode = NL80211_IFTYPE_AP;
176 else
177 priv->ah->opmode = NL80211_IFTYPE_STATION;
178
179 ath9k_hw_setopmode(priv->ah);
180}
181
182void ath9k_htc_reset(struct ath9k_htc_priv *priv)
183{
184 struct ath_hw *ah = priv->ah;
185 struct ath_common *common = ath9k_hw_common(ah);
186 struct ieee80211_channel *channel = priv->hw->conf.channel;
187 struct ath9k_hw_cal_data *caldata = NULL;
188 enum htc_phymode mode;
189 __be16 htc_mode;
190 u8 cmd_rsp;
191 int ret;
192
193 mutex_lock(&priv->mutex);
194 ath9k_htc_ps_wakeup(priv);
195
196 ath9k_htc_stop_ani(priv);
197 ieee80211_stop_queues(priv->hw);
198
199 del_timer_sync(&priv->tx.cleanup_timer);
200 ath9k_htc_tx_drain(priv);
201
202 WMI_CMD(WMI_DISABLE_INTR_CMDID);
203 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
204 WMI_CMD(WMI_STOP_RECV_CMDID);
205
206 ath9k_wmi_event_drain(priv);
207
208 caldata = &priv->caldata;
209 ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
210 if (ret) {
211 ath_err(common,
212 "Unable to reset device (%u Mhz) reset status %d\n",
213 channel->center_freq, ret);
214 }
215
216 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
217 &priv->curtxpow);
218
219 WMI_CMD(WMI_START_RECV_CMDID);
220 ath9k_host_rx_init(priv);
221
222 mode = ath9k_htc_get_curmode(priv, ah->curchan);
223 htc_mode = cpu_to_be16(mode);
224 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
225
226 WMI_CMD(WMI_ENABLE_INTR_CMDID);
227 htc_start(priv->htc);
228 ath9k_htc_vif_reconfig(priv);
229 ieee80211_wake_queues(priv->hw);
230
231 mod_timer(&priv->tx.cleanup_timer,
232 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
233
234 ath9k_htc_ps_restore(priv);
235 mutex_unlock(&priv->mutex);
236}
237
238static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
239 struct ieee80211_hw *hw,
240 struct ath9k_channel *hchan)
241{
242 struct ath_hw *ah = priv->ah;
243 struct ath_common *common = ath9k_hw_common(ah);
244 struct ieee80211_conf *conf = &common->hw->conf;
245 bool fastcc;
246 struct ieee80211_channel *channel = hw->conf.channel;
247 struct ath9k_hw_cal_data *caldata = NULL;
248 enum htc_phymode mode;
249 __be16 htc_mode;
250 u8 cmd_rsp;
251 int ret;
252
253 if (priv->op_flags & OP_INVALID)
254 return -EIO;
255
256 fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
257
258 ath9k_htc_ps_wakeup(priv);
259
260 del_timer_sync(&priv->tx.cleanup_timer);
261 ath9k_htc_tx_drain(priv);
262
263 WMI_CMD(WMI_DISABLE_INTR_CMDID);
264 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
265 WMI_CMD(WMI_STOP_RECV_CMDID);
266
267 ath9k_wmi_event_drain(priv);
268
269 ath_dbg(common, ATH_DBG_CONFIG,
270 "(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
271 priv->ah->curchan->channel,
272 channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
273 fastcc);
274
275 if (!fastcc)
276 caldata = &priv->caldata;
277
278 ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
279 if (ret) {
280 ath_err(common,
281 "Unable to reset channel (%u Mhz) reset status %d\n",
282 channel->center_freq, ret);
283 goto err;
284 }
285
286 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
287 &priv->curtxpow);
288
289 WMI_CMD(WMI_START_RECV_CMDID);
290 if (ret)
291 goto err;
292
293 ath9k_host_rx_init(priv);
294
295 mode = ath9k_htc_get_curmode(priv, hchan);
296 htc_mode = cpu_to_be16(mode);
297 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
298 if (ret)
299 goto err;
300
301 WMI_CMD(WMI_ENABLE_INTR_CMDID);
302 if (ret)
303 goto err;
304
305 htc_start(priv->htc);
306
307 if (!(priv->op_flags & OP_SCANNING) &&
308 !(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
309 ath9k_htc_vif_reconfig(priv);
310
311 mod_timer(&priv->tx.cleanup_timer,
312 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
313
314err:
315 ath9k_htc_ps_restore(priv);
316 return ret;
317}
318
319/*
320 * Monitor mode handling is a tad complicated because the firmware requires
321 * an interface to be created exclusively, while mac80211 doesn't associate
322 * an interface with the mode.
323 *
324 * So, for now, only one monitor interface can be configured.
325 */
326static void __ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
327{
328 struct ath_common *common = ath9k_hw_common(priv->ah);
329 struct ath9k_htc_target_vif hvif;
330 int ret = 0;
331 u8 cmd_rsp;
332
333 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
334 memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
335 hvif.index = priv->mon_vif_idx;
336 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
337 if (ret) {
338 ath_err(common, "Unable to remove monitor interface at idx: %d\n",
339 priv->mon_vif_idx);
340 }
341
342 priv->nvifs--;
343 priv->vif_slot &= ~(1 << priv->mon_vif_idx);
344}
345
346static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
347{
348 struct ath_common *common = ath9k_hw_common(priv->ah);
349 struct ath9k_htc_target_vif hvif;
350 struct ath9k_htc_target_sta tsta;
351 int ret = 0, sta_idx;
352 u8 cmd_rsp;
353
354 if ((priv->nvifs >= ATH9K_HTC_MAX_VIF) ||
355 (priv->nstations >= ATH9K_HTC_MAX_STA)) {
356 ret = -ENOBUFS;
357 goto err_vif;
358 }
359
360 sta_idx = ffz(priv->sta_slot);
361 if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA)) {
362 ret = -ENOBUFS;
363 goto err_vif;
364 }
365
366 /*
367 * Add an interface.
368 */
369 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
370 memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
371
372 hvif.opmode = HTC_M_MONITOR;
373 hvif.index = ffz(priv->vif_slot);
374
375 WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
376 if (ret)
377 goto err_vif;
378
379 /*
380 * Assign the monitor interface index as a special case here.
381 * This is needed when the interface is brought down.
382 */
383 priv->mon_vif_idx = hvif.index;
384 priv->vif_slot |= (1 << hvif.index);
385
386 /*
387 * Set the hardware mode to monitor only if there are no
388 * other interfaces.
389 */
390 if (!priv->nvifs)
391 priv->ah->opmode = NL80211_IFTYPE_MONITOR;
392
393 priv->nvifs++;
394
395 /*
396 * Associate a station with the interface for packet injection.
397 */
398 memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
399
400 memcpy(&tsta.macaddr, common->macaddr, ETH_ALEN);
401
402 tsta.is_vif_sta = 1;
403 tsta.sta_index = sta_idx;
404 tsta.vif_index = hvif.index;
405 tsta.maxampdu = cpu_to_be16(0xffff);
406
407 WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
408 if (ret) {
409 ath_err(common, "Unable to add station entry for monitor mode\n");
410 goto err_sta;
411 }
412
413 priv->sta_slot |= (1 << sta_idx);
414 priv->nstations++;
415 priv->vif_sta_pos[priv->mon_vif_idx] = sta_idx;
416 priv->ah->is_monitoring = true;
417
418 ath_dbg(common, ATH_DBG_CONFIG,
419 "Attached a monitor interface at idx: %d, sta idx: %d\n",
420 priv->mon_vif_idx, sta_idx);
421
422 return 0;
423
424err_sta:
425 /*
426 * Remove the interface from the target.
427 */
428 __ath9k_htc_remove_monitor_interface(priv);
429err_vif:
430 ath_dbg(common, ATH_DBG_FATAL, "Unable to attach a monitor interface\n");
431
432 return ret;
433}
434
435static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
436{
437 struct ath_common *common = ath9k_hw_common(priv->ah);
438 int ret = 0;
439 u8 cmd_rsp, sta_idx;
440
441 __ath9k_htc_remove_monitor_interface(priv);
442
443 sta_idx = priv->vif_sta_pos[priv->mon_vif_idx];
444
445 WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
446 if (ret) {
447 ath_err(common, "Unable to remove station entry for monitor mode\n");
448 return ret;
449 }
450
451 priv->sta_slot &= ~(1 << sta_idx);
452 priv->nstations--;
453 priv->ah->is_monitoring = false;
454
455 ath_dbg(common, ATH_DBG_CONFIG,
456 "Removed a monitor interface at idx: %d, sta idx: %d\n",
457 priv->mon_vif_idx, sta_idx);
458
459 return 0;
460}
461
462static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
463 struct ieee80211_vif *vif,
464 struct ieee80211_sta *sta)
465{
466 struct ath_common *common = ath9k_hw_common(priv->ah);
467 struct ath9k_htc_target_sta tsta;
468 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
469 struct ath9k_htc_sta *ista;
470 int ret, sta_idx;
471 u8 cmd_rsp;
472 u16 maxampdu;
473
474 if (priv->nstations >= ATH9K_HTC_MAX_STA)
475 return -ENOBUFS;
476
477 sta_idx = ffz(priv->sta_slot);
478 if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA))
479 return -ENOBUFS;
480
481 memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
482
483 if (sta) {
484 ista = (struct ath9k_htc_sta *) sta->drv_priv;
485 memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
486 memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
487 tsta.is_vif_sta = 0;
488 ista->index = sta_idx;
489 } else {
490 memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
491 tsta.is_vif_sta = 1;
492 }
493
494 tsta.sta_index = sta_idx;
495 tsta.vif_index = avp->index;
496
497 if (!sta) {
498 tsta.maxampdu = cpu_to_be16(0xffff);
499 } else {
500 maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
501 sta->ht_cap.ampdu_factor);
502 tsta.maxampdu = cpu_to_be16(maxampdu);
503 }
504
505 WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
506 if (ret) {
507 if (sta)
508 ath_err(common,
509 "Unable to add station entry for: %pM\n",
510 sta->addr);
511 return ret;
512 }
513
514 if (sta) {
515 ath_dbg(common, ATH_DBG_CONFIG,
516 "Added a station entry for: %pM (idx: %d)\n",
517 sta->addr, tsta.sta_index);
518 } else {
519 ath_dbg(common, ATH_DBG_CONFIG,
520 "Added a station entry for VIF %d (idx: %d)\n",
521 avp->index, tsta.sta_index);
522 }
523
524 priv->sta_slot |= (1 << sta_idx);
525 priv->nstations++;
526 if (!sta)
527 priv->vif_sta_pos[avp->index] = sta_idx;
528
529 return 0;
530}
531
532static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
533 struct ieee80211_vif *vif,
534 struct ieee80211_sta *sta)
535{
536 struct ath_common *common = ath9k_hw_common(priv->ah);
537 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
538 struct ath9k_htc_sta *ista;
539 int ret;
540 u8 cmd_rsp, sta_idx;
541
542 if (sta) {
543 ista = (struct ath9k_htc_sta *) sta->drv_priv;
544 sta_idx = ista->index;
545 } else {
546 sta_idx = priv->vif_sta_pos[avp->index];
547 }
548
549 WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
550 if (ret) {
551 if (sta)
552 ath_err(common,
553 "Unable to remove station entry for: %pM\n",
554 sta->addr);
555 return ret;
556 }
557
558 if (sta) {
559 ath_dbg(common, ATH_DBG_CONFIG,
560 "Removed a station entry for: %pM (idx: %d)\n",
561 sta->addr, sta_idx);
562 } else {
563 ath_dbg(common, ATH_DBG_CONFIG,
564 "Removed a station entry for VIF %d (idx: %d)\n",
565 avp->index, sta_idx);
566 }
567
568 priv->sta_slot &= ~(1 << sta_idx);
569 priv->nstations--;
570
571 return 0;
572}
573
574int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
575 u8 enable_coex)
576{
577 struct ath9k_htc_cap_target tcap;
578 int ret;
579 u8 cmd_rsp;
580
581 memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
582
583 tcap.ampdu_limit = cpu_to_be32(0xffff);
584 tcap.ampdu_subframes = 0xff;
585 tcap.enable_coex = enable_coex;
586 tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
587
588 WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
589
590 return ret;
591}
592
593static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
594 struct ieee80211_sta *sta,
595 struct ath9k_htc_target_rate *trate)
596{
597 struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
598 struct ieee80211_supported_band *sband;
599 u32 caps = 0;
600 int i, j;
601
602 sband = priv->hw->wiphy->bands[priv->hw->conf.channel->band];
603
604 for (i = 0, j = 0; i < sband->n_bitrates; i++) {
605 if (sta->supp_rates[sband->band] & BIT(i)) {
606 trate->rates.legacy_rates.rs_rates[j]
607 = (sband->bitrates[i].bitrate * 2) / 10;
608 j++;
609 }
610 }
611 trate->rates.legacy_rates.rs_nrates = j;
612
613 if (sta->ht_cap.ht_supported) {
614 for (i = 0, j = 0; i < 77; i++) {
615 if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
616 trate->rates.ht_rates.rs_rates[j++] = i;
617 if (j == ATH_HTC_RATE_MAX)
618 break;
619 }
620 trate->rates.ht_rates.rs_nrates = j;
621
622 caps = WLAN_RC_HT_FLAG;
623 if (sta->ht_cap.mcs.rx_mask[1])
624 caps |= WLAN_RC_DS_FLAG;
625 if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
626 (conf_is_ht40(&priv->hw->conf)))
627 caps |= WLAN_RC_40_FLAG;
628 if (conf_is_ht40(&priv->hw->conf) &&
629 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
630 caps |= WLAN_RC_SGI_FLAG;
631 else if (conf_is_ht20(&priv->hw->conf) &&
632 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
633 caps |= WLAN_RC_SGI_FLAG;
634 }
635
636 trate->sta_index = ista->index;
637 trate->isnew = 1;
638 trate->capflags = cpu_to_be32(caps);
639}
640
641static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
642 struct ath9k_htc_target_rate *trate)
643{
644 struct ath_common *common = ath9k_hw_common(priv->ah);
645 int ret;
646 u8 cmd_rsp;
647
648 WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
649 if (ret) {
650 ath_err(common,
651 "Unable to initialize Rate information on target\n");
652 }
653
654 return ret;
655}
656
657static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
658 struct ieee80211_sta *sta)
659{
660 struct ath_common *common = ath9k_hw_common(priv->ah);
661 struct ath9k_htc_target_rate trate;
662 int ret;
663
664 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
665 ath9k_htc_setup_rate(priv, sta, &trate);
666 ret = ath9k_htc_send_rate_cmd(priv, &trate);
667 if (!ret)
668 ath_dbg(common, ATH_DBG_CONFIG,
669 "Updated target sta: %pM, rate caps: 0x%X\n",
670 sta->addr, be32_to_cpu(trate.capflags));
671}
672
673static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
674 struct ieee80211_vif *vif,
675 struct ieee80211_bss_conf *bss_conf)
676{
677 struct ath_common *common = ath9k_hw_common(priv->ah);
678 struct ath9k_htc_target_rate trate;
679 struct ieee80211_sta *sta;
680 int ret;
681
682 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
683
684 rcu_read_lock();
685 sta = ieee80211_find_sta(vif, bss_conf->bssid);
686 if (!sta) {
687 rcu_read_unlock();
688 return;
689 }
690 ath9k_htc_setup_rate(priv, sta, &trate);
691 rcu_read_unlock();
692
693 ret = ath9k_htc_send_rate_cmd(priv, &trate);
694 if (!ret)
695 ath_dbg(common, ATH_DBG_CONFIG,
696 "Updated target sta: %pM, rate caps: 0x%X\n",
697 bss_conf->bssid, be32_to_cpu(trate.capflags));
698}
699
700static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
701 struct ieee80211_vif *vif,
702 struct ieee80211_sta *sta,
703 enum ieee80211_ampdu_mlme_action action,
704 u16 tid)
705{
706 struct ath_common *common = ath9k_hw_common(priv->ah);
707 struct ath9k_htc_target_aggr aggr;
708 struct ath9k_htc_sta *ista;
709 int ret = 0;
710 u8 cmd_rsp;
711
712 if (tid >= ATH9K_HTC_MAX_TID)
713 return -EINVAL;
714
715 memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
716 ista = (struct ath9k_htc_sta *) sta->drv_priv;
717
718 aggr.sta_index = ista->index;
719 aggr.tidno = tid & 0xf;
720 aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
721
722 WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
723 if (ret)
724 ath_dbg(common, ATH_DBG_CONFIG,
725 "Unable to %s TX aggregation for (%pM, %d)\n",
726 (aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
727 else
728 ath_dbg(common, ATH_DBG_CONFIG,
729 "%s TX aggregation for (%pM, %d)\n",
730 (aggr.aggr_enable) ? "Starting" : "Stopping",
731 sta->addr, tid);
732
733 spin_lock_bh(&priv->tx.tx_lock);
734 ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
735 spin_unlock_bh(&priv->tx.tx_lock);
736
737 return ret;
738}
739
740/*******/
741/* ANI */
742/*******/
743
744void ath9k_htc_start_ani(struct ath9k_htc_priv *priv)
745{
746 struct ath_common *common = ath9k_hw_common(priv->ah);
747 unsigned long timestamp = jiffies_to_msecs(jiffies);
748
749 common->ani.longcal_timer = timestamp;
750 common->ani.shortcal_timer = timestamp;
751 common->ani.checkani_timer = timestamp;
752
753 priv->op_flags |= OP_ANI_RUNNING;
754
755 ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
756 msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
757}
758
759void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv)
760{
761 cancel_delayed_work_sync(&priv->ani_work);
762 priv->op_flags &= ~OP_ANI_RUNNING;
763}
764
765void ath9k_htc_ani_work(struct work_struct *work)
766{
767 struct ath9k_htc_priv *priv =
768 container_of(work, struct ath9k_htc_priv, ani_work.work);
769 struct ath_hw *ah = priv->ah;
770 struct ath_common *common = ath9k_hw_common(ah);
771 bool longcal = false;
772 bool shortcal = false;
773 bool aniflag = false;
774 unsigned int timestamp = jiffies_to_msecs(jiffies);
775 u32 cal_interval, short_cal_interval;
776
777 short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
778 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
779
780 /* Only calibrate if awake */
781 if (ah->power_mode != ATH9K_PM_AWAKE)
782 goto set_timer;
783
784 /* Long calibration runs independently of short calibration. */
785 if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
786 longcal = true;
787 ath_dbg(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
788 common->ani.longcal_timer = timestamp;
789 }
790
791 /* Short calibration applies only while caldone is false */
792 if (!common->ani.caldone) {
793 if ((timestamp - common->ani.shortcal_timer) >=
794 short_cal_interval) {
795 shortcal = true;
796 ath_dbg(common, ATH_DBG_ANI,
797 "shortcal @%lu\n", jiffies);
798 common->ani.shortcal_timer = timestamp;
799 common->ani.resetcal_timer = timestamp;
800 }
801 } else {
802 if ((timestamp - common->ani.resetcal_timer) >=
803 ATH_RESTART_CALINTERVAL) {
804 common->ani.caldone = ath9k_hw_reset_calvalid(ah);
805 if (common->ani.caldone)
806 common->ani.resetcal_timer = timestamp;
807 }
808 }
809
810 /* Verify whether we must check ANI */
811 if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
812 aniflag = true;
813 common->ani.checkani_timer = timestamp;
814 }
815
816 /* Skip all processing if there's nothing to do. */
817 if (longcal || shortcal || aniflag) {
818
819 ath9k_htc_ps_wakeup(priv);
820
821 /* Call ANI routine if necessary */
822 if (aniflag)
823 ath9k_hw_ani_monitor(ah, ah->curchan);
824
825 /* Perform calibration if necessary */
826 if (longcal || shortcal)
827 common->ani.caldone =
828 ath9k_hw_calibrate(ah, ah->curchan,
829 common->rx_chainmask,
830 longcal);
831
832 ath9k_htc_ps_restore(priv);
833 }
834
835set_timer:
836 /*
837 * Set timer interval based on previous results.
838 * The interval must be the shortest necessary to satisfy ANI,
839 * short calibration and long calibration.
840 */
841 cal_interval = ATH_LONG_CALINTERVAL;
842 if (priv->ah->config.enable_ani)
843 cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
844 if (!common->ani.caldone)
845 cal_interval = min(cal_interval, (u32)short_cal_interval);
846
847 ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
848 msecs_to_jiffies(cal_interval));
849}
850
851/**********************/
852/* mac80211 Callbacks */
853/**********************/
854
855static void ath9k_htc_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
856{
857 struct ieee80211_hdr *hdr;
858 struct ath9k_htc_priv *priv = hw->priv;
859 struct ath_common *common = ath9k_hw_common(priv->ah);
860 int padpos, padsize, ret, slot;
861
862 hdr = (struct ieee80211_hdr *) skb->data;
863
864 /* Add the padding after the header if this is not already done */
865 padpos = ath9k_cmn_padpos(hdr->frame_control);
866 padsize = padpos & 3;
867 if (padsize && skb->len > padpos) {
868 if (skb_headroom(skb) < padsize) {
869 ath_dbg(common, ATH_DBG_XMIT, "No room for padding\n");
870 goto fail_tx;
871 }
872 skb_push(skb, padsize);
873 memmove(skb->data, skb->data + padsize, padpos);
874 }
875
876 slot = ath9k_htc_tx_get_slot(priv);
877 if (slot < 0) {
878 ath_dbg(common, ATH_DBG_XMIT, "No free TX slot\n");
879 goto fail_tx;
880 }
881
882 ret = ath9k_htc_tx_start(priv, skb, slot, false);
883 if (ret != 0) {
884 ath_dbg(common, ATH_DBG_XMIT, "Tx failed\n");
885 goto clear_slot;
886 }
887
888 ath9k_htc_check_stop_queues(priv);
889
890 return;
891
892clear_slot:
893 ath9k_htc_tx_clear_slot(priv, slot);
894fail_tx:
895 dev_kfree_skb_any(skb);
896}
897
898static int ath9k_htc_start(struct ieee80211_hw *hw)
899{
900 struct ath9k_htc_priv *priv = hw->priv;
901 struct ath_hw *ah = priv->ah;
902 struct ath_common *common = ath9k_hw_common(ah);
903 struct ieee80211_channel *curchan = hw->conf.channel;
904 struct ath9k_channel *init_channel;
905 int ret = 0;
906 enum htc_phymode mode;
907 __be16 htc_mode;
908 u8 cmd_rsp;
909
910 mutex_lock(&priv->mutex);
911
912 ath_dbg(common, ATH_DBG_CONFIG,
913 "Starting driver with initial channel: %d MHz\n",
914 curchan->center_freq);
915
916 /* Ensure that HW is awake before flushing RX */
917 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
918 WMI_CMD(WMI_FLUSH_RECV_CMDID);
919
920 /* setup initial channel */
921 init_channel = ath9k_cmn_get_curchannel(hw, ah);
922
923 ath9k_hw_htc_resetinit(ah);
924 ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
925 if (ret) {
926 ath_err(common,
927 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
928 ret, curchan->center_freq);
929 mutex_unlock(&priv->mutex);
930 return ret;
931 }
932
933 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
934 &priv->curtxpow);
935
936 mode = ath9k_htc_get_curmode(priv, init_channel);
937 htc_mode = cpu_to_be16(mode);
938 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
939 WMI_CMD(WMI_ATH_INIT_CMDID);
940 WMI_CMD(WMI_START_RECV_CMDID);
941
942 ath9k_host_rx_init(priv);
943
944 ret = ath9k_htc_update_cap_target(priv, 0);
945 if (ret)
946 ath_dbg(common, ATH_DBG_CONFIG,
947 "Failed to update capability in target\n");
948
949 priv->op_flags &= ~OP_INVALID;
950 htc_start(priv->htc);
951
952 spin_lock_bh(&priv->tx.tx_lock);
953 priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
954 spin_unlock_bh(&priv->tx.tx_lock);
955
956 ieee80211_wake_queues(hw);
957
958 mod_timer(&priv->tx.cleanup_timer,
959 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
960
961 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE) {
962 ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
963 AR_STOMP_LOW_WLAN_WGHT);
964 ath9k_hw_btcoex_enable(ah);
965 ath_htc_resume_btcoex_work(priv);
966 }
967 mutex_unlock(&priv->mutex);
968
969 return ret;
970}
971
972static void ath9k_htc_stop(struct ieee80211_hw *hw)
973{
974 struct ath9k_htc_priv *priv = hw->priv;
975 struct ath_hw *ah = priv->ah;
976 struct ath_common *common = ath9k_hw_common(ah);
977 int ret __attribute__ ((unused));
978 u8 cmd_rsp;
979
980 mutex_lock(&priv->mutex);
981
982 if (priv->op_flags & OP_INVALID) {
983 ath_dbg(common, ATH_DBG_ANY, "Device not present\n");
984 mutex_unlock(&priv->mutex);
985 return;
986 }
987
988 ath9k_htc_ps_wakeup(priv);
989
990 WMI_CMD(WMI_DISABLE_INTR_CMDID);
991 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
992 WMI_CMD(WMI_STOP_RECV_CMDID);
993
994 tasklet_kill(&priv->rx_tasklet);
995
996 del_timer_sync(&priv->tx.cleanup_timer);
997 ath9k_htc_tx_drain(priv);
998 ath9k_wmi_event_drain(priv);
999
1000 mutex_unlock(&priv->mutex);
1001
1002 /* Cancel all the running timers/work .. */
1003 cancel_work_sync(&priv->fatal_work);
1004 cancel_work_sync(&priv->ps_work);
1005
1006#ifdef CONFIG_MAC80211_LEDS
1007 cancel_work_sync(&priv->led_work);
1008#endif
1009 ath9k_htc_stop_ani(priv);
1010
1011 mutex_lock(&priv->mutex);
1012
1013 if (ah->btcoex_hw.enabled) {
1014 ath9k_hw_btcoex_disable(ah);
1015 if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
1016 ath_htc_cancel_btcoex_work(priv);
1017 }
1018
1019 /* Remove a monitor interface if it's present. */
1020 if (priv->ah->is_monitoring)
1021 ath9k_htc_remove_monitor_interface(priv);
1022
1023 ath9k_hw_phy_disable(ah);
1024 ath9k_hw_disable(ah);
1025 ath9k_htc_ps_restore(priv);
1026 ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1027
1028 priv->op_flags |= OP_INVALID;
1029
1030 ath_dbg(common, ATH_DBG_CONFIG, "Driver halt\n");
1031 mutex_unlock(&priv->mutex);
1032}
1033
1034static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1035 struct ieee80211_vif *vif)
1036{
1037 struct ath9k_htc_priv *priv = hw->priv;
1038 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1039 struct ath_common *common = ath9k_hw_common(priv->ah);
1040 struct ath9k_htc_target_vif hvif;
1041 int ret = 0;
1042 u8 cmd_rsp;
1043
1044 mutex_lock(&priv->mutex);
1045
1046 if (priv->nvifs >= ATH9K_HTC_MAX_VIF) {
1047 mutex_unlock(&priv->mutex);
1048 return -ENOBUFS;
1049 }
1050
1051 if (priv->num_ibss_vif ||
1052 (priv->nvifs && vif->type == NL80211_IFTYPE_ADHOC)) {
1053 ath_err(common, "IBSS coexistence with other modes is not allowed\n");
1054 mutex_unlock(&priv->mutex);
1055 return -ENOBUFS;
1056 }
1057
1058 if (((vif->type == NL80211_IFTYPE_AP) ||
1059 (vif->type == NL80211_IFTYPE_ADHOC)) &&
1060 ((priv->num_ap_vif + priv->num_ibss_vif) >= ATH9K_HTC_MAX_BCN_VIF)) {
1061 ath_err(common, "Max. number of beaconing interfaces reached\n");
1062 mutex_unlock(&priv->mutex);
1063 return -ENOBUFS;
1064 }
1065
1066 ath9k_htc_ps_wakeup(priv);
1067 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1068 memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1069
1070 switch (vif->type) {
1071 case NL80211_IFTYPE_STATION:
1072 hvif.opmode = HTC_M_STA;
1073 break;
1074 case NL80211_IFTYPE_ADHOC:
1075 hvif.opmode = HTC_M_IBSS;
1076 break;
1077 case NL80211_IFTYPE_AP:
1078 hvif.opmode = HTC_M_HOSTAP;
1079 break;
1080 default:
1081 ath_err(common,
1082 "Interface type %d not yet supported\n", vif->type);
1083 ret = -EOPNOTSUPP;
1084 goto out;
1085 }
1086
1087 /* Index starts from zero on the target */
1088 avp->index = hvif.index = ffz(priv->vif_slot);
1089 hvif.rtsthreshold = cpu_to_be16(2304);
1090 WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1091 if (ret)
1092 goto out;
1093
1094 /*
1095 * We need a node in target to tx mgmt frames
1096 * before association.
1097 */
1098 ret = ath9k_htc_add_station(priv, vif, NULL);
1099 if (ret) {
1100 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1101 goto out;
1102 }
1103
1104 ath9k_htc_set_bssid_mask(priv, vif);
1105
1106 priv->vif_slot |= (1 << avp->index);
1107 priv->nvifs++;
1108
1109 INC_VIF(priv, vif->type);
1110
1111 if ((vif->type == NL80211_IFTYPE_AP) ||
1112 (vif->type == NL80211_IFTYPE_ADHOC))
1113 ath9k_htc_assign_bslot(priv, vif);
1114
1115 ath9k_htc_set_opmode(priv);
1116
1117 if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1118 !(priv->op_flags & OP_ANI_RUNNING)) {
1119 ath9k_hw_set_tsfadjust(priv->ah, 1);
1120 ath9k_htc_start_ani(priv);
1121 }
1122
1123 ath_dbg(common, ATH_DBG_CONFIG,
1124 "Attach a VIF of type: %d at idx: %d\n", vif->type, avp->index);
1125
1126out:
1127 ath9k_htc_ps_restore(priv);
1128 mutex_unlock(&priv->mutex);
1129
1130 return ret;
1131}
1132
1133static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1134 struct ieee80211_vif *vif)
1135{
1136 struct ath9k_htc_priv *priv = hw->priv;
1137 struct ath_common *common = ath9k_hw_common(priv->ah);
1138 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1139 struct ath9k_htc_target_vif hvif;
1140 int ret = 0;
1141 u8 cmd_rsp;
1142
1143 mutex_lock(&priv->mutex);
1144 ath9k_htc_ps_wakeup(priv);
1145
1146 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1147 memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1148 hvif.index = avp->index;
1149 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1150 if (ret) {
1151 ath_err(common, "Unable to remove interface at idx: %d\n",
1152 avp->index);
1153 }
1154 priv->nvifs--;
1155 priv->vif_slot &= ~(1 << avp->index);
1156
1157 ath9k_htc_remove_station(priv, vif, NULL);
1158
1159 DEC_VIF(priv, vif->type);
1160
1161 if ((vif->type == NL80211_IFTYPE_AP) ||
1162 (vif->type == NL80211_IFTYPE_ADHOC))
1163 ath9k_htc_remove_bslot(priv, vif);
1164
1165 ath9k_htc_set_opmode(priv);
1166
1167 ath9k_htc_set_bssid_mask(priv, vif);
1168
1169 /*
1170 * Stop ANI only if there are no associated station interfaces.
1171 */
1172 if ((vif->type == NL80211_IFTYPE_AP) && (priv->num_ap_vif == 0)) {
1173 priv->rearm_ani = false;
1174 ieee80211_iterate_active_interfaces_atomic(priv->hw,
1175 ath9k_htc_vif_iter, priv);
1176 if (!priv->rearm_ani)
1177 ath9k_htc_stop_ani(priv);
1178 }
1179
1180 ath_dbg(common, ATH_DBG_CONFIG, "Detach Interface at idx: %d\n", avp->index);
1181
1182 ath9k_htc_ps_restore(priv);
1183 mutex_unlock(&priv->mutex);
1184}
1185
1186static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1187{
1188 struct ath9k_htc_priv *priv = hw->priv;
1189 struct ath_common *common = ath9k_hw_common(priv->ah);
1190 struct ieee80211_conf *conf = &hw->conf;
1191
1192 mutex_lock(&priv->mutex);
1193
1194 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1195 bool enable_radio = false;
1196 bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1197
1198 mutex_lock(&priv->htc_pm_lock);
1199 if (!idle && priv->ps_idle)
1200 enable_radio = true;
1201 priv->ps_idle = idle;
1202 mutex_unlock(&priv->htc_pm_lock);
1203
1204 if (enable_radio) {
1205 ath_dbg(common, ATH_DBG_CONFIG,
1206 "not-idle: enabling radio\n");
1207 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1208 ath9k_htc_radio_enable(hw);
1209 }
1210 }
1211
1212 /*
1213 * Monitor interface should be added before
1214 * IEEE80211_CONF_CHANGE_CHANNEL is handled.
1215 */
1216 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1217 if ((conf->flags & IEEE80211_CONF_MONITOR) &&
1218 !priv->ah->is_monitoring)
1219 ath9k_htc_add_monitor_interface(priv);
1220 else if (priv->ah->is_monitoring)
1221 ath9k_htc_remove_monitor_interface(priv);
1222 }
1223
1224 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1225 struct ieee80211_channel *curchan = hw->conf.channel;
1226 int pos = curchan->hw_value;
1227
1228 ath_dbg(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
1229 curchan->center_freq);
1230
1231 ath9k_cmn_update_ichannel(&priv->ah->channels[pos],
1232 hw->conf.channel,
1233 hw->conf.channel_type);
1234
1235 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1236 ath_err(common, "Unable to set channel\n");
1237 mutex_unlock(&priv->mutex);
1238 return -EINVAL;
1239 }
1240
1241 }
1242
1243 if (changed & IEEE80211_CONF_CHANGE_PS) {
1244 if (conf->flags & IEEE80211_CONF_PS) {
1245 ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1246 priv->ps_enabled = true;
1247 } else {
1248 priv->ps_enabled = false;
1249 cancel_work_sync(&priv->ps_work);
1250 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1251 }
1252 }
1253
1254 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1255 priv->txpowlimit = 2 * conf->power_level;
1256 ath9k_cmn_update_txpow(priv->ah, priv->curtxpow,
1257 priv->txpowlimit, &priv->curtxpow);
1258 }
1259
1260 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1261 mutex_lock(&priv->htc_pm_lock);
1262 if (!priv->ps_idle) {
1263 mutex_unlock(&priv->htc_pm_lock);
1264 goto out;
1265 }
1266 mutex_unlock(&priv->htc_pm_lock);
1267
1268 ath_dbg(common, ATH_DBG_CONFIG,
1269 "idle: disabling radio\n");
1270 ath9k_htc_radio_disable(hw);
1271 }
1272
1273out:
1274 mutex_unlock(&priv->mutex);
1275 return 0;
1276}
1277
1278#define SUPPORTED_FILTERS \
1279 (FIF_PROMISC_IN_BSS | \
1280 FIF_ALLMULTI | \
1281 FIF_CONTROL | \
1282 FIF_PSPOLL | \
1283 FIF_OTHER_BSS | \
1284 FIF_BCN_PRBRESP_PROMISC | \
1285 FIF_PROBE_REQ | \
1286 FIF_FCSFAIL)
1287
1288static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1289 unsigned int changed_flags,
1290 unsigned int *total_flags,
1291 u64 multicast)
1292{
1293 struct ath9k_htc_priv *priv = hw->priv;
1294 u32 rfilt;
1295
1296 mutex_lock(&priv->mutex);
1297 changed_flags &= SUPPORTED_FILTERS;
1298 *total_flags &= SUPPORTED_FILTERS;
1299
1300 if (priv->op_flags & OP_INVALID) {
1301 ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_ANY,
1302 "Unable to configure filter on invalid state\n");
1303 return;
1304 }
1305 ath9k_htc_ps_wakeup(priv);
1306
1307 priv->rxfilter = *total_flags;
1308 rfilt = ath9k_htc_calcrxfilter(priv);
1309 ath9k_hw_setrxfilter(priv->ah, rfilt);
1310
1311 ath_dbg(ath9k_hw_common(priv->ah), ATH_DBG_CONFIG,
1312 "Set HW RX filter: 0x%x\n", rfilt);
1313
1314 ath9k_htc_ps_restore(priv);
1315 mutex_unlock(&priv->mutex);
1316}
1317
1318static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1319 struct ieee80211_vif *vif,
1320 struct ieee80211_sta *sta)
1321{
1322 struct ath9k_htc_priv *priv = hw->priv;
1323 int ret;
1324
1325 mutex_lock(&priv->mutex);
1326 ath9k_htc_ps_wakeup(priv);
1327 ret = ath9k_htc_add_station(priv, vif, sta);
1328 if (!ret)
1329 ath9k_htc_init_rate(priv, sta);
1330 ath9k_htc_ps_restore(priv);
1331 mutex_unlock(&priv->mutex);
1332
1333 return ret;
1334}
1335
1336static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1337 struct ieee80211_vif *vif,
1338 struct ieee80211_sta *sta)
1339{
1340 struct ath9k_htc_priv *priv = hw->priv;
1341 struct ath9k_htc_sta *ista;
1342 int ret;
1343
1344 mutex_lock(&priv->mutex);
1345 ath9k_htc_ps_wakeup(priv);
1346 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1347 htc_sta_drain(priv->htc, ista->index);
1348 ret = ath9k_htc_remove_station(priv, vif, sta);
1349 ath9k_htc_ps_restore(priv);
1350 mutex_unlock(&priv->mutex);
1351
1352 return ret;
1353}
1354
1355static int ath9k_htc_conf_tx(struct ieee80211_hw *hw, u16 queue,
1356 const struct ieee80211_tx_queue_params *params)
1357{
1358 struct ath9k_htc_priv *priv = hw->priv;
1359 struct ath_common *common = ath9k_hw_common(priv->ah);
1360 struct ath9k_tx_queue_info qi;
1361 int ret = 0, qnum;
1362
1363 if (queue >= WME_NUM_AC)
1364 return 0;
1365
1366 mutex_lock(&priv->mutex);
1367 ath9k_htc_ps_wakeup(priv);
1368
1369 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1370
1371 qi.tqi_aifs = params->aifs;
1372 qi.tqi_cwmin = params->cw_min;
1373 qi.tqi_cwmax = params->cw_max;
1374 qi.tqi_burstTime = params->txop;
1375
1376 qnum = get_hw_qnum(queue, priv->hwq_map);
1377
1378 ath_dbg(common, ATH_DBG_CONFIG,
1379 "Configure tx [queue/hwq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1380 queue, qnum, params->aifs, params->cw_min,
1381 params->cw_max, params->txop);
1382
1383 ret = ath_htc_txq_update(priv, qnum, &qi);
1384 if (ret) {
1385 ath_err(common, "TXQ Update failed\n");
1386 goto out;
1387 }
1388
1389 if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1390 (qnum == priv->hwq_map[WME_AC_BE]))
1391 ath9k_htc_beaconq_config(priv);
1392out:
1393 ath9k_htc_ps_restore(priv);
1394 mutex_unlock(&priv->mutex);
1395
1396 return ret;
1397}
1398
1399static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1400 enum set_key_cmd cmd,
1401 struct ieee80211_vif *vif,
1402 struct ieee80211_sta *sta,
1403 struct ieee80211_key_conf *key)
1404{
1405 struct ath9k_htc_priv *priv = hw->priv;
1406 struct ath_common *common = ath9k_hw_common(priv->ah);
1407 int ret = 0;
1408
1409 if (htc_modparam_nohwcrypt)
1410 return -ENOSPC;
1411
1412 mutex_lock(&priv->mutex);
1413 ath_dbg(common, ATH_DBG_CONFIG, "Set HW Key\n");
1414 ath9k_htc_ps_wakeup(priv);
1415
1416 switch (cmd) {
1417 case SET_KEY:
1418 ret = ath_key_config(common, vif, sta, key);
1419 if (ret >= 0) {
1420 key->hw_key_idx = ret;
1421 /* push IV and Michael MIC generation to stack */
1422 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1423 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1424 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1425 if (priv->ah->sw_mgmt_crypto &&
1426 key->cipher == WLAN_CIPHER_SUITE_CCMP)
1427 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
1428 ret = 0;
1429 }
1430 break;
1431 case DISABLE_KEY:
1432 ath_key_delete(common, key);
1433 break;
1434 default:
1435 ret = -EINVAL;
1436 }
1437
1438 ath9k_htc_ps_restore(priv);
1439 mutex_unlock(&priv->mutex);
1440
1441 return ret;
1442}
1443
1444static void ath9k_htc_set_bssid(struct ath9k_htc_priv *priv)
1445{
1446 struct ath_common *common = ath9k_hw_common(priv->ah);
1447
1448 ath9k_hw_write_associd(priv->ah);
1449 ath_dbg(common, ATH_DBG_CONFIG,
1450 "BSSID: %pM aid: 0x%x\n",
1451 common->curbssid, common->curaid);
1452}
1453
1454static void ath9k_htc_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1455{
1456 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
1457 struct ath_common *common = ath9k_hw_common(priv->ah);
1458 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1459
1460 if ((vif->type == NL80211_IFTYPE_STATION) && bss_conf->assoc) {
1461 common->curaid = bss_conf->aid;
1462 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1463 }
1464}
1465
1466static void ath9k_htc_choose_set_bssid(struct ath9k_htc_priv *priv)
1467{
1468 if (priv->num_sta_assoc_vif == 1) {
1469 ieee80211_iterate_active_interfaces_atomic(priv->hw,
1470 ath9k_htc_bss_iter, priv);
1471 ath9k_htc_set_bssid(priv);
1472 }
1473}
1474
1475static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1476 struct ieee80211_vif *vif,
1477 struct ieee80211_bss_conf *bss_conf,
1478 u32 changed)
1479{
1480 struct ath9k_htc_priv *priv = hw->priv;
1481 struct ath_hw *ah = priv->ah;
1482 struct ath_common *common = ath9k_hw_common(ah);
1483
1484 mutex_lock(&priv->mutex);
1485 ath9k_htc_ps_wakeup(priv);
1486
1487 if (changed & BSS_CHANGED_ASSOC) {
1488 ath_dbg(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
1489 bss_conf->assoc);
1490
1491 bss_conf->assoc ?
1492 priv->num_sta_assoc_vif++ : priv->num_sta_assoc_vif--;
1493
1494 if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1495 if (bss_conf->assoc && (priv->num_sta_assoc_vif == 1))
1496 ath9k_htc_start_ani(priv);
1497 else if (priv->num_sta_assoc_vif == 0)
1498 ath9k_htc_stop_ani(priv);
1499 }
1500 }
1501
1502 if (changed & BSS_CHANGED_BSSID) {
1503 if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
1504 common->curaid = bss_conf->aid;
1505 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1506 ath9k_htc_set_bssid(priv);
1507 } else if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1508 ath9k_htc_choose_set_bssid(priv);
1509 }
1510 }
1511
1512 if ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon) {
1513 ath_dbg(common, ATH_DBG_CONFIG,
1514 "Beacon enabled for BSS: %pM\n", bss_conf->bssid);
1515 ath9k_htc_set_tsfadjust(priv, vif);
1516 priv->op_flags |= OP_ENABLE_BEACON;
1517 ath9k_htc_beacon_config(priv, vif);
1518 }
1519
1520 if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon) {
1521 /*
1522 * Disable SWBA interrupt only if there are no
1523 * AP/IBSS interfaces.
1524 */
1525 if ((priv->num_ap_vif <= 1) || priv->num_ibss_vif) {
1526 ath_dbg(common, ATH_DBG_CONFIG,
1527 "Beacon disabled for BSS: %pM\n",
1528 bss_conf->bssid);
1529 priv->op_flags &= ~OP_ENABLE_BEACON;
1530 ath9k_htc_beacon_config(priv, vif);
1531 }
1532 }
1533
1534 if (changed & BSS_CHANGED_BEACON_INT) {
1535 /*
1536 * Reset the HW TSF for the first AP interface.
1537 */
1538 if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1539 (priv->nvifs == 1) &&
1540 (priv->num_ap_vif == 1) &&
1541 (vif->type == NL80211_IFTYPE_AP)) {
1542 priv->op_flags |= OP_TSF_RESET;
1543 }
1544 ath_dbg(common, ATH_DBG_CONFIG,
1545 "Beacon interval changed for BSS: %pM\n",
1546 bss_conf->bssid);
1547 ath9k_htc_beacon_config(priv, vif);
1548 }
1549
1550 if (changed & BSS_CHANGED_ERP_SLOT) {
1551 if (bss_conf->use_short_slot)
1552 ah->slottime = 9;
1553 else
1554 ah->slottime = 20;
1555
1556 ath9k_hw_init_global_settings(ah);
1557 }
1558
1559 if (changed & BSS_CHANGED_HT)
1560 ath9k_htc_update_rate(priv, vif, bss_conf);
1561
1562 ath9k_htc_ps_restore(priv);
1563 mutex_unlock(&priv->mutex);
1564}
1565
1566static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw)
1567{
1568 struct ath9k_htc_priv *priv = hw->priv;
1569 u64 tsf;
1570
1571 mutex_lock(&priv->mutex);
1572 ath9k_htc_ps_wakeup(priv);
1573 tsf = ath9k_hw_gettsf64(priv->ah);
1574 ath9k_htc_ps_restore(priv);
1575 mutex_unlock(&priv->mutex);
1576
1577 return tsf;
1578}
1579
1580static void ath9k_htc_set_tsf(struct ieee80211_hw *hw, u64 tsf)
1581{
1582 struct ath9k_htc_priv *priv = hw->priv;
1583
1584 mutex_lock(&priv->mutex);
1585 ath9k_htc_ps_wakeup(priv);
1586 ath9k_hw_settsf64(priv->ah, tsf);
1587 ath9k_htc_ps_restore(priv);
1588 mutex_unlock(&priv->mutex);
1589}
1590
1591static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw)
1592{
1593 struct ath9k_htc_priv *priv = hw->priv;
1594
1595 mutex_lock(&priv->mutex);
1596 ath9k_htc_ps_wakeup(priv);
1597 ath9k_hw_reset_tsf(priv->ah);
1598 ath9k_htc_ps_restore(priv);
1599 mutex_unlock(&priv->mutex);
1600}
1601
1602static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1603 struct ieee80211_vif *vif,
1604 enum ieee80211_ampdu_mlme_action action,
1605 struct ieee80211_sta *sta,
1606 u16 tid, u16 *ssn, u8 buf_size)
1607{
1608 struct ath9k_htc_priv *priv = hw->priv;
1609 struct ath9k_htc_sta *ista;
1610 int ret = 0;
1611
1612 mutex_lock(&priv->mutex);
1613 ath9k_htc_ps_wakeup(priv);
1614
1615 switch (action) {
1616 case IEEE80211_AMPDU_RX_START:
1617 break;
1618 case IEEE80211_AMPDU_RX_STOP:
1619 break;
1620 case IEEE80211_AMPDU_TX_START:
1621 ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1622 if (!ret)
1623 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1624 break;
1625 case IEEE80211_AMPDU_TX_STOP:
1626 ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1627 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1628 break;
1629 case IEEE80211_AMPDU_TX_OPERATIONAL:
1630 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1631 spin_lock_bh(&priv->tx.tx_lock);
1632 ista->tid_state[tid] = AGGR_OPERATIONAL;
1633 spin_unlock_bh(&priv->tx.tx_lock);
1634 break;
1635 default:
1636 ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1637 }
1638
1639 ath9k_htc_ps_restore(priv);
1640 mutex_unlock(&priv->mutex);
1641
1642 return ret;
1643}
1644
1645static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw)
1646{
1647 struct ath9k_htc_priv *priv = hw->priv;
1648
1649 mutex_lock(&priv->mutex);
1650 spin_lock_bh(&priv->beacon_lock);
1651 priv->op_flags |= OP_SCANNING;
1652 spin_unlock_bh(&priv->beacon_lock);
1653 cancel_work_sync(&priv->ps_work);
1654 ath9k_htc_stop_ani(priv);
1655 mutex_unlock(&priv->mutex);
1656}
1657
1658static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw)
1659{
1660 struct ath9k_htc_priv *priv = hw->priv;
1661
1662 mutex_lock(&priv->mutex);
1663 spin_lock_bh(&priv->beacon_lock);
1664 priv->op_flags &= ~OP_SCANNING;
1665 spin_unlock_bh(&priv->beacon_lock);
1666 ath9k_htc_ps_wakeup(priv);
1667 ath9k_htc_vif_reconfig(priv);
1668 ath9k_htc_ps_restore(priv);
1669 mutex_unlock(&priv->mutex);
1670}
1671
1672static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1673{
1674 return 0;
1675}
1676
1677static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1678 u8 coverage_class)
1679{
1680 struct ath9k_htc_priv *priv = hw->priv;
1681
1682 mutex_lock(&priv->mutex);
1683 ath9k_htc_ps_wakeup(priv);
1684 priv->ah->coverage_class = coverage_class;
1685 ath9k_hw_init_global_settings(priv->ah);
1686 ath9k_htc_ps_restore(priv);
1687 mutex_unlock(&priv->mutex);
1688}
1689
1690/*
1691 * Currently, this is used only for selecting the minimum rate
1692 * for management frames, rate selection for data frames remain
1693 * unaffected.
1694 */
1695static int ath9k_htc_set_bitrate_mask(struct ieee80211_hw *hw,
1696 struct ieee80211_vif *vif,
1697 const struct cfg80211_bitrate_mask *mask)
1698{
1699 struct ath9k_htc_priv *priv = hw->priv;
1700 struct ath_common *common = ath9k_hw_common(priv->ah);
1701 struct ath9k_htc_target_rate_mask tmask;
1702 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1703 int ret = 0;
1704 u8 cmd_rsp;
1705
1706 memset(&tmask, 0, sizeof(struct ath9k_htc_target_rate_mask));
1707
1708 tmask.vif_index = avp->index;
1709 tmask.band = IEEE80211_BAND_2GHZ;
1710 tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_2GHZ].legacy);
1711
1712 WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1713 if (ret) {
1714 ath_err(common,
1715 "Unable to set 2G rate mask for "
1716 "interface at idx: %d\n", avp->index);
1717 goto out;
1718 }
1719
1720 tmask.band = IEEE80211_BAND_5GHZ;
1721 tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_5GHZ].legacy);
1722
1723 WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1724 if (ret) {
1725 ath_err(common,
1726 "Unable to set 5G rate mask for "
1727 "interface at idx: %d\n", avp->index);
1728 goto out;
1729 }
1730
1731 ath_dbg(common, ATH_DBG_CONFIG,
1732 "Set bitrate masks: 0x%x, 0x%x\n",
1733 mask->control[IEEE80211_BAND_2GHZ].legacy,
1734 mask->control[IEEE80211_BAND_5GHZ].legacy);
1735out:
1736 return ret;
1737}
1738
1739struct ieee80211_ops ath9k_htc_ops = {
1740 .tx = ath9k_htc_tx,
1741 .start = ath9k_htc_start,
1742 .stop = ath9k_htc_stop,
1743 .add_interface = ath9k_htc_add_interface,
1744 .remove_interface = ath9k_htc_remove_interface,
1745 .config = ath9k_htc_config,
1746 .configure_filter = ath9k_htc_configure_filter,
1747 .sta_add = ath9k_htc_sta_add,
1748 .sta_remove = ath9k_htc_sta_remove,
1749 .conf_tx = ath9k_htc_conf_tx,
1750 .bss_info_changed = ath9k_htc_bss_info_changed,
1751 .set_key = ath9k_htc_set_key,
1752 .get_tsf = ath9k_htc_get_tsf,
1753 .set_tsf = ath9k_htc_set_tsf,
1754 .reset_tsf = ath9k_htc_reset_tsf,
1755 .ampdu_action = ath9k_htc_ampdu_action,
1756 .sw_scan_start = ath9k_htc_sw_scan_start,
1757 .sw_scan_complete = ath9k_htc_sw_scan_complete,
1758 .set_rts_threshold = ath9k_htc_set_rts_threshold,
1759 .rfkill_poll = ath9k_htc_rfkill_poll_state,
1760 .set_coverage_class = ath9k_htc_set_coverage_class,
1761 .set_bitrate_mask = ath9k_htc_set_bitrate_mask,
1762};
1/*
2 * Copyright (c) 2010-2011 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "htc.h"
18
19/*************/
20/* Utilities */
21/*************/
22
23/* HACK Alert: Use 11NG for 2.4, use 11NA for 5 */
24static enum htc_phymode ath9k_htc_get_curmode(struct ath9k_htc_priv *priv,
25 struct ath9k_channel *ichan)
26{
27 if (IS_CHAN_5GHZ(ichan))
28 return HTC_MODE_11NA;
29
30 return HTC_MODE_11NG;
31}
32
33bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
34 enum ath9k_power_mode mode)
35{
36 bool ret;
37
38 mutex_lock(&priv->htc_pm_lock);
39 ret = ath9k_hw_setpower(priv->ah, mode);
40 mutex_unlock(&priv->htc_pm_lock);
41
42 return ret;
43}
44
45void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv)
46{
47 mutex_lock(&priv->htc_pm_lock);
48 if (++priv->ps_usecount != 1)
49 goto unlock;
50 ath9k_hw_setpower(priv->ah, ATH9K_PM_AWAKE);
51
52unlock:
53 mutex_unlock(&priv->htc_pm_lock);
54}
55
56void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv)
57{
58 bool reset;
59
60 mutex_lock(&priv->htc_pm_lock);
61 if (--priv->ps_usecount != 0)
62 goto unlock;
63
64 if (priv->ps_idle) {
65 ath9k_hw_setrxabort(priv->ah, true);
66 ath9k_hw_stopdmarecv(priv->ah, &reset);
67 ath9k_hw_setpower(priv->ah, ATH9K_PM_FULL_SLEEP);
68 } else if (priv->ps_enabled) {
69 ath9k_hw_setpower(priv->ah, ATH9K_PM_NETWORK_SLEEP);
70 }
71
72unlock:
73 mutex_unlock(&priv->htc_pm_lock);
74}
75
76void ath9k_ps_work(struct work_struct *work)
77{
78 struct ath9k_htc_priv *priv =
79 container_of(work, struct ath9k_htc_priv,
80 ps_work);
81 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
82
83 /* The chip wakes up after receiving the first beacon
84 while network sleep is enabled. For the driver to
85 be in sync with the hw, set the chip to awake and
86 only then set it to sleep.
87 */
88 ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
89}
90
91static void ath9k_htc_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
92{
93 struct ath9k_htc_priv *priv = data;
94 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
95
96 if ((vif->type == NL80211_IFTYPE_AP ||
97 vif->type == NL80211_IFTYPE_MESH_POINT) &&
98 bss_conf->enable_beacon) {
99 priv->reconfig_beacon = true;
100 priv->rearm_ani = true;
101 }
102
103 if (bss_conf->assoc) {
104 priv->rearm_ani = true;
105 priv->reconfig_beacon = true;
106 }
107}
108
109static void ath9k_htc_vif_reconfig(struct ath9k_htc_priv *priv)
110{
111 priv->rearm_ani = false;
112 priv->reconfig_beacon = false;
113
114 ieee80211_iterate_active_interfaces_atomic(
115 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
116 ath9k_htc_vif_iter, priv);
117 if (priv->rearm_ani)
118 ath9k_htc_start_ani(priv);
119
120 if (priv->reconfig_beacon) {
121 ath9k_htc_ps_wakeup(priv);
122 ath9k_htc_beacon_reconfig(priv);
123 ath9k_htc_ps_restore(priv);
124 }
125}
126
127static void ath9k_htc_bssid_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
128{
129 struct ath9k_vif_iter_data *iter_data = data;
130 int i;
131
132 if (iter_data->hw_macaddr != NULL) {
133 for (i = 0; i < ETH_ALEN; i++)
134 iter_data->mask[i] &= ~(iter_data->hw_macaddr[i] ^ mac[i]);
135 } else {
136 iter_data->hw_macaddr = mac;
137 }
138}
139
140static void ath9k_htc_set_mac_bssid_mask(struct ath9k_htc_priv *priv,
141 struct ieee80211_vif *vif)
142{
143 struct ath_common *common = ath9k_hw_common(priv->ah);
144 struct ath9k_vif_iter_data iter_data;
145
146 /*
147 * Pick the MAC address of the first interface as the new hardware
148 * MAC address. The hardware will use it together with the BSSID mask
149 * when matching addresses.
150 */
151 iter_data.hw_macaddr = NULL;
152 eth_broadcast_addr(iter_data.mask);
153
154 if (vif)
155 ath9k_htc_bssid_iter(&iter_data, vif->addr, vif);
156
157 /* Get list of all active MAC addresses */
158 ieee80211_iterate_active_interfaces_atomic(
159 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
160 ath9k_htc_bssid_iter, &iter_data);
161
162 memcpy(common->bssidmask, iter_data.mask, ETH_ALEN);
163
164 if (iter_data.hw_macaddr)
165 memcpy(common->macaddr, iter_data.hw_macaddr, ETH_ALEN);
166
167 ath_hw_setbssidmask(common);
168}
169
170static void ath9k_htc_set_opmode(struct ath9k_htc_priv *priv)
171{
172 if (priv->num_ibss_vif)
173 priv->ah->opmode = NL80211_IFTYPE_ADHOC;
174 else if (priv->num_ap_vif)
175 priv->ah->opmode = NL80211_IFTYPE_AP;
176 else if (priv->num_mbss_vif)
177 priv->ah->opmode = NL80211_IFTYPE_MESH_POINT;
178 else
179 priv->ah->opmode = NL80211_IFTYPE_STATION;
180
181 ath9k_hw_setopmode(priv->ah);
182}
183
184void ath9k_htc_reset(struct ath9k_htc_priv *priv)
185{
186 struct ath_hw *ah = priv->ah;
187 struct ath_common *common = ath9k_hw_common(ah);
188 struct ieee80211_channel *channel = priv->hw->conf.chandef.chan;
189 struct ath9k_hw_cal_data *caldata = NULL;
190 enum htc_phymode mode;
191 __be16 htc_mode;
192 u8 cmd_rsp;
193 int ret;
194
195 mutex_lock(&priv->mutex);
196 ath9k_htc_ps_wakeup(priv);
197
198 ath9k_htc_stop_ani(priv);
199 ieee80211_stop_queues(priv->hw);
200
201 del_timer_sync(&priv->tx.cleanup_timer);
202 ath9k_htc_tx_drain(priv);
203
204 WMI_CMD(WMI_DISABLE_INTR_CMDID);
205 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
206 WMI_CMD(WMI_STOP_RECV_CMDID);
207
208 ath9k_wmi_event_drain(priv);
209
210 caldata = &priv->caldata;
211 ret = ath9k_hw_reset(ah, ah->curchan, caldata, false);
212 if (ret) {
213 ath_err(common,
214 "Unable to reset device (%u Mhz) reset status %d\n",
215 channel->center_freq, ret);
216 }
217
218 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
219 &priv->curtxpow);
220
221 WMI_CMD(WMI_START_RECV_CMDID);
222 ath9k_host_rx_init(priv);
223
224 mode = ath9k_htc_get_curmode(priv, ah->curchan);
225 htc_mode = cpu_to_be16(mode);
226 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
227
228 WMI_CMD(WMI_ENABLE_INTR_CMDID);
229 htc_start(priv->htc);
230 ath9k_htc_vif_reconfig(priv);
231 ieee80211_wake_queues(priv->hw);
232
233 mod_timer(&priv->tx.cleanup_timer,
234 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
235
236 ath9k_htc_ps_restore(priv);
237 mutex_unlock(&priv->mutex);
238}
239
240static int ath9k_htc_set_channel(struct ath9k_htc_priv *priv,
241 struct ieee80211_hw *hw,
242 struct ath9k_channel *hchan)
243{
244 struct ath_hw *ah = priv->ah;
245 struct ath_common *common = ath9k_hw_common(ah);
246 struct ieee80211_conf *conf = &common->hw->conf;
247 bool fastcc;
248 struct ieee80211_channel *channel = hw->conf.chandef.chan;
249 struct ath9k_hw_cal_data *caldata = NULL;
250 enum htc_phymode mode;
251 __be16 htc_mode;
252 u8 cmd_rsp;
253 int ret;
254
255 if (test_bit(ATH_OP_INVALID, &common->op_flags))
256 return -EIO;
257
258 fastcc = !!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL);
259
260 ath9k_htc_ps_wakeup(priv);
261
262 ath9k_htc_stop_ani(priv);
263 del_timer_sync(&priv->tx.cleanup_timer);
264 ath9k_htc_tx_drain(priv);
265
266 WMI_CMD(WMI_DISABLE_INTR_CMDID);
267 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
268 WMI_CMD(WMI_STOP_RECV_CMDID);
269
270 ath9k_wmi_event_drain(priv);
271
272 ath_dbg(common, CONFIG,
273 "(%u MHz) -> (%u MHz), HT: %d, HT40: %d fastcc: %d\n",
274 priv->ah->curchan->channel,
275 channel->center_freq, conf_is_ht(conf), conf_is_ht40(conf),
276 fastcc);
277
278 if (!fastcc)
279 caldata = &priv->caldata;
280
281 ret = ath9k_hw_reset(ah, hchan, caldata, fastcc);
282 if (ret) {
283 ath_err(common,
284 "Unable to reset channel (%u Mhz) reset status %d\n",
285 channel->center_freq, ret);
286 goto err;
287 }
288
289 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
290 &priv->curtxpow);
291
292 WMI_CMD(WMI_START_RECV_CMDID);
293 if (ret)
294 goto err;
295
296 ath9k_host_rx_init(priv);
297
298 mode = ath9k_htc_get_curmode(priv, hchan);
299 htc_mode = cpu_to_be16(mode);
300 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
301 if (ret)
302 goto err;
303
304 WMI_CMD(WMI_ENABLE_INTR_CMDID);
305 if (ret)
306 goto err;
307
308 htc_start(priv->htc);
309
310 if (!test_bit(ATH_OP_SCANNING, &common->op_flags) &&
311 !(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL))
312 ath9k_htc_vif_reconfig(priv);
313
314 mod_timer(&priv->tx.cleanup_timer,
315 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
316
317 /* perform spectral scan if requested. */
318 if (test_bit(ATH_OP_SCANNING, &common->op_flags) &&
319 priv->spec_priv.spectral_mode == SPECTRAL_CHANSCAN)
320 ath9k_cmn_spectral_scan_trigger(common, &priv->spec_priv);
321err:
322 ath9k_htc_ps_restore(priv);
323 return ret;
324}
325
326/*
327 * Monitor mode handling is a tad complicated because the firmware requires
328 * an interface to be created exclusively, while mac80211 doesn't associate
329 * an interface with the mode.
330 *
331 * So, for now, only one monitor interface can be configured.
332 */
333static void __ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
334{
335 struct ath_common *common = ath9k_hw_common(priv->ah);
336 struct ath9k_htc_target_vif hvif;
337 int ret = 0;
338 u8 cmd_rsp;
339
340 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
341 memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
342 hvif.index = priv->mon_vif_idx;
343 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
344 if (ret) {
345 ath_err(common, "Unable to remove monitor interface at idx: %d\n",
346 priv->mon_vif_idx);
347 }
348
349 priv->nvifs--;
350 priv->vif_slot &= ~(1 << priv->mon_vif_idx);
351}
352
353static int ath9k_htc_add_monitor_interface(struct ath9k_htc_priv *priv)
354{
355 struct ath_common *common = ath9k_hw_common(priv->ah);
356 struct ath9k_htc_target_vif hvif;
357 struct ath9k_htc_target_sta tsta;
358 int ret = 0, sta_idx;
359 u8 cmd_rsp;
360
361 if ((priv->nvifs >= ATH9K_HTC_MAX_VIF) ||
362 (priv->nstations >= ATH9K_HTC_MAX_STA)) {
363 ret = -ENOBUFS;
364 goto err_vif;
365 }
366
367 sta_idx = ffz(priv->sta_slot);
368 if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA)) {
369 ret = -ENOBUFS;
370 goto err_vif;
371 }
372
373 /*
374 * Add an interface.
375 */
376 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
377 memcpy(&hvif.myaddr, common->macaddr, ETH_ALEN);
378
379 hvif.opmode = HTC_M_MONITOR;
380 hvif.index = ffz(priv->vif_slot);
381
382 WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
383 if (ret)
384 goto err_vif;
385
386 /*
387 * Assign the monitor interface index as a special case here.
388 * This is needed when the interface is brought down.
389 */
390 priv->mon_vif_idx = hvif.index;
391 priv->vif_slot |= (1 << hvif.index);
392
393 /*
394 * Set the hardware mode to monitor only if there are no
395 * other interfaces.
396 */
397 if (!priv->nvifs)
398 priv->ah->opmode = NL80211_IFTYPE_MONITOR;
399
400 priv->nvifs++;
401
402 /*
403 * Associate a station with the interface for packet injection.
404 */
405 memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
406
407 memcpy(&tsta.macaddr, common->macaddr, ETH_ALEN);
408
409 tsta.is_vif_sta = 1;
410 tsta.sta_index = sta_idx;
411 tsta.vif_index = hvif.index;
412 tsta.maxampdu = cpu_to_be16(0xffff);
413
414 WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
415 if (ret) {
416 ath_err(common, "Unable to add station entry for monitor mode\n");
417 goto err_sta;
418 }
419
420 priv->sta_slot |= (1 << sta_idx);
421 priv->nstations++;
422 priv->vif_sta_pos[priv->mon_vif_idx] = sta_idx;
423 priv->ah->is_monitoring = true;
424
425 ath_dbg(common, CONFIG,
426 "Attached a monitor interface at idx: %d, sta idx: %d\n",
427 priv->mon_vif_idx, sta_idx);
428
429 return 0;
430
431err_sta:
432 /*
433 * Remove the interface from the target.
434 */
435 __ath9k_htc_remove_monitor_interface(priv);
436err_vif:
437 ath_dbg(common, FATAL, "Unable to attach a monitor interface\n");
438
439 return ret;
440}
441
442static int ath9k_htc_remove_monitor_interface(struct ath9k_htc_priv *priv)
443{
444 struct ath_common *common = ath9k_hw_common(priv->ah);
445 int ret = 0;
446 u8 cmd_rsp, sta_idx;
447
448 __ath9k_htc_remove_monitor_interface(priv);
449
450 sta_idx = priv->vif_sta_pos[priv->mon_vif_idx];
451
452 WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
453 if (ret) {
454 ath_err(common, "Unable to remove station entry for monitor mode\n");
455 return ret;
456 }
457
458 priv->sta_slot &= ~(1 << sta_idx);
459 priv->nstations--;
460 priv->ah->is_monitoring = false;
461
462 ath_dbg(common, CONFIG,
463 "Removed a monitor interface at idx: %d, sta idx: %d\n",
464 priv->mon_vif_idx, sta_idx);
465
466 return 0;
467}
468
469static int ath9k_htc_add_station(struct ath9k_htc_priv *priv,
470 struct ieee80211_vif *vif,
471 struct ieee80211_sta *sta)
472{
473 struct ath_common *common = ath9k_hw_common(priv->ah);
474 struct ath9k_htc_target_sta tsta;
475 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
476 struct ath9k_htc_sta *ista;
477 int ret, sta_idx;
478 u8 cmd_rsp;
479 u16 maxampdu;
480
481 if (priv->nstations >= ATH9K_HTC_MAX_STA)
482 return -ENOBUFS;
483
484 sta_idx = ffz(priv->sta_slot);
485 if ((sta_idx < 0) || (sta_idx > ATH9K_HTC_MAX_STA))
486 return -ENOBUFS;
487
488 memset(&tsta, 0, sizeof(struct ath9k_htc_target_sta));
489
490 if (sta) {
491 ista = (struct ath9k_htc_sta *) sta->drv_priv;
492 memcpy(&tsta.macaddr, sta->addr, ETH_ALEN);
493 memcpy(&tsta.bssid, common->curbssid, ETH_ALEN);
494 ista->index = sta_idx;
495 tsta.is_vif_sta = 0;
496 maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
497 sta->ht_cap.ampdu_factor);
498 tsta.maxampdu = cpu_to_be16(maxampdu);
499 } else {
500 memcpy(&tsta.macaddr, vif->addr, ETH_ALEN);
501 tsta.is_vif_sta = 1;
502 tsta.maxampdu = cpu_to_be16(0xffff);
503 }
504
505 tsta.sta_index = sta_idx;
506 tsta.vif_index = avp->index;
507
508 WMI_CMD_BUF(WMI_NODE_CREATE_CMDID, &tsta);
509 if (ret) {
510 if (sta)
511 ath_err(common,
512 "Unable to add station entry for: %pM\n",
513 sta->addr);
514 return ret;
515 }
516
517 if (sta) {
518 ath_dbg(common, CONFIG,
519 "Added a station entry for: %pM (idx: %d)\n",
520 sta->addr, tsta.sta_index);
521 } else {
522 ath_dbg(common, CONFIG,
523 "Added a station entry for VIF %d (idx: %d)\n",
524 avp->index, tsta.sta_index);
525 }
526
527 priv->sta_slot |= (1 << sta_idx);
528 priv->nstations++;
529 if (!sta)
530 priv->vif_sta_pos[avp->index] = sta_idx;
531
532 return 0;
533}
534
535static int ath9k_htc_remove_station(struct ath9k_htc_priv *priv,
536 struct ieee80211_vif *vif,
537 struct ieee80211_sta *sta)
538{
539 struct ath_common *common = ath9k_hw_common(priv->ah);
540 struct ath9k_htc_vif *avp = (struct ath9k_htc_vif *) vif->drv_priv;
541 struct ath9k_htc_sta *ista;
542 int ret;
543 u8 cmd_rsp, sta_idx;
544
545 if (sta) {
546 ista = (struct ath9k_htc_sta *) sta->drv_priv;
547 sta_idx = ista->index;
548 } else {
549 sta_idx = priv->vif_sta_pos[avp->index];
550 }
551
552 WMI_CMD_BUF(WMI_NODE_REMOVE_CMDID, &sta_idx);
553 if (ret) {
554 if (sta)
555 ath_err(common,
556 "Unable to remove station entry for: %pM\n",
557 sta->addr);
558 return ret;
559 }
560
561 if (sta) {
562 ath_dbg(common, CONFIG,
563 "Removed a station entry for: %pM (idx: %d)\n",
564 sta->addr, sta_idx);
565 } else {
566 ath_dbg(common, CONFIG,
567 "Removed a station entry for VIF %d (idx: %d)\n",
568 avp->index, sta_idx);
569 }
570
571 priv->sta_slot &= ~(1 << sta_idx);
572 priv->nstations--;
573
574 return 0;
575}
576
577int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
578 u8 enable_coex)
579{
580 struct ath9k_htc_cap_target tcap;
581 int ret;
582 u8 cmd_rsp;
583
584 memset(&tcap, 0, sizeof(struct ath9k_htc_cap_target));
585
586 tcap.ampdu_limit = cpu_to_be32(0xffff);
587 tcap.ampdu_subframes = 0xff;
588 tcap.enable_coex = enable_coex;
589 tcap.tx_chainmask = priv->ah->caps.tx_chainmask;
590
591 WMI_CMD_BUF(WMI_TARGET_IC_UPDATE_CMDID, &tcap);
592
593 return ret;
594}
595
596static void ath9k_htc_setup_rate(struct ath9k_htc_priv *priv,
597 struct ieee80211_sta *sta,
598 struct ath9k_htc_target_rate *trate)
599{
600 struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
601 struct ieee80211_supported_band *sband;
602 u32 caps = 0;
603 int i, j;
604
605 sband = priv->hw->wiphy->bands[priv->hw->conf.chandef.chan->band];
606
607 for (i = 0, j = 0; i < sband->n_bitrates; i++) {
608 if (sta->supp_rates[sband->band] & BIT(i)) {
609 trate->rates.legacy_rates.rs_rates[j]
610 = (sband->bitrates[i].bitrate * 2) / 10;
611 j++;
612 }
613 }
614 trate->rates.legacy_rates.rs_nrates = j;
615
616 if (sta->ht_cap.ht_supported) {
617 for (i = 0, j = 0; i < 77; i++) {
618 if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
619 trate->rates.ht_rates.rs_rates[j++] = i;
620 if (j == ATH_HTC_RATE_MAX)
621 break;
622 }
623 trate->rates.ht_rates.rs_nrates = j;
624
625 caps = WLAN_RC_HT_FLAG;
626 if (sta->ht_cap.cap & IEEE80211_HT_CAP_RX_STBC)
627 caps |= ATH_RC_TX_STBC_FLAG;
628 if (sta->ht_cap.mcs.rx_mask[1])
629 caps |= WLAN_RC_DS_FLAG;
630 if ((sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) &&
631 (conf_is_ht40(&priv->hw->conf)))
632 caps |= WLAN_RC_40_FLAG;
633 if (conf_is_ht40(&priv->hw->conf) &&
634 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40))
635 caps |= WLAN_RC_SGI_FLAG;
636 else if (conf_is_ht20(&priv->hw->conf) &&
637 (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20))
638 caps |= WLAN_RC_SGI_FLAG;
639 }
640
641 trate->sta_index = ista->index;
642 trate->isnew = 1;
643 trate->capflags = cpu_to_be32(caps);
644}
645
646static int ath9k_htc_send_rate_cmd(struct ath9k_htc_priv *priv,
647 struct ath9k_htc_target_rate *trate)
648{
649 struct ath_common *common = ath9k_hw_common(priv->ah);
650 int ret;
651 u8 cmd_rsp;
652
653 WMI_CMD_BUF(WMI_RC_RATE_UPDATE_CMDID, trate);
654 if (ret) {
655 ath_err(common,
656 "Unable to initialize Rate information on target\n");
657 }
658
659 return ret;
660}
661
662static void ath9k_htc_init_rate(struct ath9k_htc_priv *priv,
663 struct ieee80211_sta *sta)
664{
665 struct ath_common *common = ath9k_hw_common(priv->ah);
666 struct ath9k_htc_target_rate trate;
667 int ret;
668
669 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
670 ath9k_htc_setup_rate(priv, sta, &trate);
671 ret = ath9k_htc_send_rate_cmd(priv, &trate);
672 if (!ret)
673 ath_dbg(common, CONFIG,
674 "Updated target sta: %pM, rate caps: 0x%X\n",
675 sta->addr, be32_to_cpu(trate.capflags));
676}
677
678static void ath9k_htc_update_rate(struct ath9k_htc_priv *priv,
679 struct ieee80211_vif *vif,
680 struct ieee80211_bss_conf *bss_conf)
681{
682 struct ath_common *common = ath9k_hw_common(priv->ah);
683 struct ath9k_htc_target_rate trate;
684 struct ieee80211_sta *sta;
685 int ret;
686
687 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
688
689 rcu_read_lock();
690 sta = ieee80211_find_sta(vif, bss_conf->bssid);
691 if (!sta) {
692 rcu_read_unlock();
693 return;
694 }
695 ath9k_htc_setup_rate(priv, sta, &trate);
696 rcu_read_unlock();
697
698 ret = ath9k_htc_send_rate_cmd(priv, &trate);
699 if (!ret)
700 ath_dbg(common, CONFIG,
701 "Updated target sta: %pM, rate caps: 0x%X\n",
702 bss_conf->bssid, be32_to_cpu(trate.capflags));
703}
704
705static int ath9k_htc_tx_aggr_oper(struct ath9k_htc_priv *priv,
706 struct ieee80211_vif *vif,
707 struct ieee80211_sta *sta,
708 enum ieee80211_ampdu_mlme_action action,
709 u16 tid)
710{
711 struct ath_common *common = ath9k_hw_common(priv->ah);
712 struct ath9k_htc_target_aggr aggr;
713 struct ath9k_htc_sta *ista;
714 int ret = 0;
715 u8 cmd_rsp;
716
717 if (tid >= ATH9K_HTC_MAX_TID)
718 return -EINVAL;
719
720 memset(&aggr, 0, sizeof(struct ath9k_htc_target_aggr));
721 ista = (struct ath9k_htc_sta *) sta->drv_priv;
722
723 aggr.sta_index = ista->index;
724 aggr.tidno = tid & 0xf;
725 aggr.aggr_enable = (action == IEEE80211_AMPDU_TX_START) ? true : false;
726
727 WMI_CMD_BUF(WMI_TX_AGGR_ENABLE_CMDID, &aggr);
728 if (ret)
729 ath_dbg(common, CONFIG,
730 "Unable to %s TX aggregation for (%pM, %d)\n",
731 (aggr.aggr_enable) ? "start" : "stop", sta->addr, tid);
732 else
733 ath_dbg(common, CONFIG,
734 "%s TX aggregation for (%pM, %d)\n",
735 (aggr.aggr_enable) ? "Starting" : "Stopping",
736 sta->addr, tid);
737
738 spin_lock_bh(&priv->tx.tx_lock);
739 ista->tid_state[tid] = (aggr.aggr_enable && !ret) ? AGGR_START : AGGR_STOP;
740 spin_unlock_bh(&priv->tx.tx_lock);
741
742 return ret;
743}
744
745/*******/
746/* ANI */
747/*******/
748
749void ath9k_htc_start_ani(struct ath9k_htc_priv *priv)
750{
751 struct ath_common *common = ath9k_hw_common(priv->ah);
752 unsigned long timestamp = jiffies_to_msecs(jiffies);
753
754 common->ani.longcal_timer = timestamp;
755 common->ani.shortcal_timer = timestamp;
756 common->ani.checkani_timer = timestamp;
757
758 set_bit(ATH_OP_ANI_RUN, &common->op_flags);
759
760 ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
761 msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
762}
763
764void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv)
765{
766 struct ath_common *common = ath9k_hw_common(priv->ah);
767 cancel_delayed_work_sync(&priv->ani_work);
768 clear_bit(ATH_OP_ANI_RUN, &common->op_flags);
769}
770
771void ath9k_htc_ani_work(struct work_struct *work)
772{
773 struct ath9k_htc_priv *priv =
774 container_of(work, struct ath9k_htc_priv, ani_work.work);
775 struct ath_hw *ah = priv->ah;
776 struct ath_common *common = ath9k_hw_common(ah);
777 bool longcal = false;
778 bool shortcal = false;
779 bool aniflag = false;
780 unsigned int timestamp = jiffies_to_msecs(jiffies);
781 u32 cal_interval, short_cal_interval;
782
783 short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
784 ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
785
786 /* Only calibrate if awake */
787 if (ah->power_mode != ATH9K_PM_AWAKE)
788 goto set_timer;
789
790 /* Long calibration runs independently of short calibration. */
791 if ((timestamp - common->ani.longcal_timer) >= ATH_LONG_CALINTERVAL) {
792 longcal = true;
793 ath_dbg(common, ANI, "longcal @%lu\n", jiffies);
794 common->ani.longcal_timer = timestamp;
795 }
796
797 /*
798 * Short calibration applies only while caldone
799 * is false or -ETIMEDOUT
800 */
801 if (common->ani.caldone <= 0) {
802 if ((timestamp - common->ani.shortcal_timer) >=
803 short_cal_interval) {
804 shortcal = true;
805 ath_dbg(common, ANI, "shortcal @%lu\n", jiffies);
806 common->ani.shortcal_timer = timestamp;
807 common->ani.resetcal_timer = timestamp;
808 }
809 } else {
810 if ((timestamp - common->ani.resetcal_timer) >=
811 ATH_RESTART_CALINTERVAL) {
812 common->ani.caldone = ath9k_hw_reset_calvalid(ah);
813 if (common->ani.caldone)
814 common->ani.resetcal_timer = timestamp;
815 }
816 }
817
818 /* Verify whether we must check ANI */
819 if ((timestamp - common->ani.checkani_timer) >= ATH_ANI_POLLINTERVAL) {
820 aniflag = true;
821 common->ani.checkani_timer = timestamp;
822 }
823
824 /* Skip all processing if there's nothing to do. */
825 if (longcal || shortcal || aniflag) {
826
827 ath9k_htc_ps_wakeup(priv);
828
829 /* Call ANI routine if necessary */
830 if (aniflag)
831 ath9k_hw_ani_monitor(ah, ah->curchan);
832
833 /* Perform calibration if necessary */
834 if (longcal || shortcal)
835 common->ani.caldone =
836 ath9k_hw_calibrate(ah, ah->curchan,
837 ah->rxchainmask, longcal) > 0;
838
839 ath9k_htc_ps_restore(priv);
840 }
841
842set_timer:
843 /*
844 * Set timer interval based on previous results.
845 * The interval must be the shortest necessary to satisfy ANI,
846 * short calibration and long calibration.
847 */
848 cal_interval = ATH_LONG_CALINTERVAL;
849 cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
850 /*
851 * Short calibration applies only while caldone
852 * is false or -ETIMEDOUT
853 */
854 if (common->ani.caldone <= 0)
855 cal_interval = min(cal_interval, (u32)short_cal_interval);
856
857 ieee80211_queue_delayed_work(common->hw, &priv->ani_work,
858 msecs_to_jiffies(cal_interval));
859}
860
861/**********************/
862/* mac80211 Callbacks */
863/**********************/
864
865static void ath9k_htc_tx(struct ieee80211_hw *hw,
866 struct ieee80211_tx_control *control,
867 struct sk_buff *skb)
868{
869 struct ieee80211_hdr *hdr;
870 struct ath9k_htc_priv *priv = hw->priv;
871 struct ath_common *common = ath9k_hw_common(priv->ah);
872 int padpos, padsize, ret, slot;
873
874 hdr = (struct ieee80211_hdr *) skb->data;
875
876 /* Add the padding after the header if this is not already done */
877 padpos = ieee80211_hdrlen(hdr->frame_control);
878 padsize = padpos & 3;
879 if (padsize && skb->len > padpos) {
880 if (skb_headroom(skb) < padsize) {
881 ath_dbg(common, XMIT, "No room for padding\n");
882 goto fail_tx;
883 }
884 skb_push(skb, padsize);
885 memmove(skb->data, skb->data + padsize, padpos);
886 }
887
888 slot = ath9k_htc_tx_get_slot(priv);
889 if (slot < 0) {
890 ath_dbg(common, XMIT, "No free TX slot\n");
891 goto fail_tx;
892 }
893
894 ret = ath9k_htc_tx_start(priv, control->sta, skb, slot, false);
895 if (ret != 0) {
896 ath_dbg(common, XMIT, "Tx failed\n");
897 goto clear_slot;
898 }
899
900 ath9k_htc_check_stop_queues(priv);
901
902 return;
903
904clear_slot:
905 ath9k_htc_tx_clear_slot(priv, slot);
906fail_tx:
907 dev_kfree_skb_any(skb);
908}
909
910static int ath9k_htc_start(struct ieee80211_hw *hw)
911{
912 struct ath9k_htc_priv *priv = hw->priv;
913 struct ath_hw *ah = priv->ah;
914 struct ath_common *common = ath9k_hw_common(ah);
915 struct ieee80211_channel *curchan = hw->conf.chandef.chan;
916 struct ath9k_channel *init_channel;
917 int ret = 0;
918 enum htc_phymode mode;
919 __be16 htc_mode;
920 u8 cmd_rsp;
921
922 mutex_lock(&priv->mutex);
923
924 ath_dbg(common, CONFIG,
925 "Starting driver with initial channel: %d MHz\n",
926 curchan->center_freq);
927
928 /* Ensure that HW is awake before flushing RX */
929 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
930 WMI_CMD(WMI_FLUSH_RECV_CMDID);
931
932 /* setup initial channel */
933 init_channel = ath9k_cmn_get_channel(hw, ah, &hw->conf.chandef);
934
935 ret = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
936 if (ret) {
937 ath_err(common,
938 "Unable to reset hardware; reset status %d (freq %u MHz)\n",
939 ret, curchan->center_freq);
940 mutex_unlock(&priv->mutex);
941 return ret;
942 }
943
944 ath9k_cmn_update_txpow(ah, priv->curtxpow, priv->txpowlimit,
945 &priv->curtxpow);
946
947 mode = ath9k_htc_get_curmode(priv, init_channel);
948 htc_mode = cpu_to_be16(mode);
949 WMI_CMD_BUF(WMI_SET_MODE_CMDID, &htc_mode);
950 WMI_CMD(WMI_ATH_INIT_CMDID);
951 WMI_CMD(WMI_START_RECV_CMDID);
952
953 ath9k_host_rx_init(priv);
954
955 ret = ath9k_htc_update_cap_target(priv, 0);
956 if (ret)
957 ath_dbg(common, CONFIG,
958 "Failed to update capability in target\n");
959
960 clear_bit(ATH_OP_INVALID, &common->op_flags);
961 htc_start(priv->htc);
962
963 spin_lock_bh(&priv->tx.tx_lock);
964 priv->tx.flags &= ~ATH9K_HTC_OP_TX_QUEUES_STOP;
965 spin_unlock_bh(&priv->tx.tx_lock);
966
967 ieee80211_wake_queues(hw);
968
969 mod_timer(&priv->tx.cleanup_timer,
970 jiffies + msecs_to_jiffies(ATH9K_HTC_TX_CLEANUP_INTERVAL));
971
972 ath9k_htc_start_btcoex(priv);
973
974 mutex_unlock(&priv->mutex);
975
976 return ret;
977}
978
979static void ath9k_htc_stop(struct ieee80211_hw *hw)
980{
981 struct ath9k_htc_priv *priv = hw->priv;
982 struct ath_hw *ah = priv->ah;
983 struct ath_common *common = ath9k_hw_common(ah);
984 int ret __attribute__ ((unused));
985 u8 cmd_rsp;
986
987 mutex_lock(&priv->mutex);
988
989 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
990 ath_dbg(common, ANY, "Device not present\n");
991 mutex_unlock(&priv->mutex);
992 return;
993 }
994
995 ath9k_htc_ps_wakeup(priv);
996
997 WMI_CMD(WMI_DISABLE_INTR_CMDID);
998 WMI_CMD(WMI_DRAIN_TXQ_ALL_CMDID);
999 WMI_CMD(WMI_STOP_RECV_CMDID);
1000
1001 tasklet_kill(&priv->rx_tasklet);
1002
1003 del_timer_sync(&priv->tx.cleanup_timer);
1004 ath9k_htc_tx_drain(priv);
1005 ath9k_wmi_event_drain(priv);
1006
1007 mutex_unlock(&priv->mutex);
1008
1009 /* Cancel all the running timers/work .. */
1010 cancel_work_sync(&priv->fatal_work);
1011 cancel_work_sync(&priv->ps_work);
1012
1013#ifdef CONFIG_MAC80211_LEDS
1014 cancel_work_sync(&priv->led_work);
1015#endif
1016 ath9k_htc_stop_ani(priv);
1017
1018 mutex_lock(&priv->mutex);
1019
1020 ath9k_htc_stop_btcoex(priv);
1021
1022 /* Remove a monitor interface if it's present. */
1023 if (priv->ah->is_monitoring)
1024 ath9k_htc_remove_monitor_interface(priv);
1025
1026 ath9k_hw_phy_disable(ah);
1027 ath9k_hw_disable(ah);
1028 ath9k_htc_ps_restore(priv);
1029 ath9k_htc_setpower(priv, ATH9K_PM_FULL_SLEEP);
1030
1031 set_bit(ATH_OP_INVALID, &common->op_flags);
1032
1033 ath_dbg(common, CONFIG, "Driver halt\n");
1034 mutex_unlock(&priv->mutex);
1035}
1036
1037static int ath9k_htc_add_interface(struct ieee80211_hw *hw,
1038 struct ieee80211_vif *vif)
1039{
1040 struct ath9k_htc_priv *priv = hw->priv;
1041 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1042 struct ath_common *common = ath9k_hw_common(priv->ah);
1043 struct ath9k_htc_target_vif hvif;
1044 int ret = 0;
1045 u8 cmd_rsp;
1046
1047 mutex_lock(&priv->mutex);
1048
1049 ath9k_htc_ps_wakeup(priv);
1050 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1051 memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1052
1053 switch (vif->type) {
1054 case NL80211_IFTYPE_STATION:
1055 hvif.opmode = HTC_M_STA;
1056 break;
1057 case NL80211_IFTYPE_ADHOC:
1058 hvif.opmode = HTC_M_IBSS;
1059 break;
1060 case NL80211_IFTYPE_AP:
1061 hvif.opmode = HTC_M_HOSTAP;
1062 break;
1063 case NL80211_IFTYPE_MESH_POINT:
1064 hvif.opmode = HTC_M_WDS; /* close enough */
1065 break;
1066 default:
1067 ath_err(common,
1068 "Interface type %d not yet supported\n", vif->type);
1069 ret = -EOPNOTSUPP;
1070 goto out;
1071 }
1072
1073 /* Index starts from zero on the target */
1074 avp->index = hvif.index = ffz(priv->vif_slot);
1075 hvif.rtsthreshold = cpu_to_be16(2304);
1076 WMI_CMD_BUF(WMI_VAP_CREATE_CMDID, &hvif);
1077 if (ret)
1078 goto out;
1079
1080 /*
1081 * We need a node in target to tx mgmt frames
1082 * before association.
1083 */
1084 ret = ath9k_htc_add_station(priv, vif, NULL);
1085 if (ret) {
1086 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1087 goto out;
1088 }
1089
1090 ath9k_htc_set_mac_bssid_mask(priv, vif);
1091
1092 priv->vif_slot |= (1 << avp->index);
1093 priv->nvifs++;
1094
1095 INC_VIF(priv, vif->type);
1096
1097 if ((vif->type == NL80211_IFTYPE_AP) ||
1098 (vif->type == NL80211_IFTYPE_MESH_POINT) ||
1099 (vif->type == NL80211_IFTYPE_ADHOC))
1100 ath9k_htc_assign_bslot(priv, vif);
1101
1102 ath9k_htc_set_opmode(priv);
1103
1104 if ((priv->ah->opmode == NL80211_IFTYPE_AP) &&
1105 !test_bit(ATH_OP_ANI_RUN, &common->op_flags)) {
1106 ath9k_hw_set_tsfadjust(priv->ah, true);
1107 ath9k_htc_start_ani(priv);
1108 }
1109
1110 ath_dbg(common, CONFIG, "Attach a VIF of type: %d at idx: %d\n",
1111 vif->type, avp->index);
1112
1113out:
1114 ath9k_htc_ps_restore(priv);
1115 mutex_unlock(&priv->mutex);
1116
1117 return ret;
1118}
1119
1120static void ath9k_htc_remove_interface(struct ieee80211_hw *hw,
1121 struct ieee80211_vif *vif)
1122{
1123 struct ath9k_htc_priv *priv = hw->priv;
1124 struct ath_common *common = ath9k_hw_common(priv->ah);
1125 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1126 struct ath9k_htc_target_vif hvif;
1127 int ret = 0;
1128 u8 cmd_rsp;
1129
1130 mutex_lock(&priv->mutex);
1131 ath9k_htc_ps_wakeup(priv);
1132
1133 memset(&hvif, 0, sizeof(struct ath9k_htc_target_vif));
1134 memcpy(&hvif.myaddr, vif->addr, ETH_ALEN);
1135 hvif.index = avp->index;
1136 WMI_CMD_BUF(WMI_VAP_REMOVE_CMDID, &hvif);
1137 if (ret) {
1138 ath_err(common, "Unable to remove interface at idx: %d\n",
1139 avp->index);
1140 }
1141 priv->nvifs--;
1142 priv->vif_slot &= ~(1 << avp->index);
1143
1144 if (priv->csa_vif == vif)
1145 priv->csa_vif = NULL;
1146
1147 ath9k_htc_remove_station(priv, vif, NULL);
1148
1149 DEC_VIF(priv, vif->type);
1150
1151 if ((vif->type == NL80211_IFTYPE_AP) ||
1152 vif->type == NL80211_IFTYPE_MESH_POINT ||
1153 (vif->type == NL80211_IFTYPE_ADHOC))
1154 ath9k_htc_remove_bslot(priv, vif);
1155
1156 ath9k_htc_set_opmode(priv);
1157
1158 ath9k_htc_set_mac_bssid_mask(priv, vif);
1159
1160 /*
1161 * Stop ANI only if there are no associated station interfaces.
1162 */
1163 if ((vif->type == NL80211_IFTYPE_AP) && (priv->num_ap_vif == 0)) {
1164 priv->rearm_ani = false;
1165 ieee80211_iterate_active_interfaces_atomic(
1166 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1167 ath9k_htc_vif_iter, priv);
1168 if (!priv->rearm_ani)
1169 ath9k_htc_stop_ani(priv);
1170 }
1171
1172 ath_dbg(common, CONFIG, "Detach Interface at idx: %d\n", avp->index);
1173
1174 ath9k_htc_ps_restore(priv);
1175 mutex_unlock(&priv->mutex);
1176}
1177
1178static int ath9k_htc_config(struct ieee80211_hw *hw, u32 changed)
1179{
1180 struct ath9k_htc_priv *priv = hw->priv;
1181 struct ath_common *common = ath9k_hw_common(priv->ah);
1182 struct ieee80211_conf *conf = &hw->conf;
1183 bool chip_reset = false;
1184 int ret = 0;
1185
1186 mutex_lock(&priv->mutex);
1187 ath9k_htc_ps_wakeup(priv);
1188
1189 if (changed & IEEE80211_CONF_CHANGE_IDLE) {
1190 mutex_lock(&priv->htc_pm_lock);
1191
1192 priv->ps_idle = !!(conf->flags & IEEE80211_CONF_IDLE);
1193 if (!priv->ps_idle)
1194 chip_reset = true;
1195
1196 mutex_unlock(&priv->htc_pm_lock);
1197 }
1198
1199 /*
1200 * Monitor interface should be added before
1201 * IEEE80211_CONF_CHANGE_CHANNEL is handled.
1202 */
1203 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1204 if ((conf->flags & IEEE80211_CONF_MONITOR) &&
1205 !priv->ah->is_monitoring)
1206 ath9k_htc_add_monitor_interface(priv);
1207 else if (priv->ah->is_monitoring)
1208 ath9k_htc_remove_monitor_interface(priv);
1209 }
1210
1211 if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || chip_reset) {
1212 struct ieee80211_channel *curchan = hw->conf.chandef.chan;
1213 int pos = curchan->hw_value;
1214
1215 ath_dbg(common, CONFIG, "Set channel: %d MHz\n",
1216 curchan->center_freq);
1217
1218 ath9k_cmn_get_channel(hw, priv->ah, &hw->conf.chandef);
1219 if (ath9k_htc_set_channel(priv, hw, &priv->ah->channels[pos]) < 0) {
1220 ath_err(common, "Unable to set channel\n");
1221 ret = -EINVAL;
1222 goto out;
1223 }
1224
1225 }
1226
1227 if (changed & IEEE80211_CONF_CHANGE_PS) {
1228 if (conf->flags & IEEE80211_CONF_PS) {
1229 ath9k_htc_setpower(priv, ATH9K_PM_NETWORK_SLEEP);
1230 priv->ps_enabled = true;
1231 } else {
1232 priv->ps_enabled = false;
1233 cancel_work_sync(&priv->ps_work);
1234 ath9k_htc_setpower(priv, ATH9K_PM_AWAKE);
1235 }
1236 }
1237
1238 if (changed & IEEE80211_CONF_CHANGE_POWER) {
1239 priv->txpowlimit = 2 * conf->power_level;
1240 ath9k_cmn_update_txpow(priv->ah, priv->curtxpow,
1241 priv->txpowlimit, &priv->curtxpow);
1242 }
1243
1244out:
1245 ath9k_htc_ps_restore(priv);
1246 mutex_unlock(&priv->mutex);
1247 return ret;
1248}
1249
1250#define SUPPORTED_FILTERS \
1251 (FIF_ALLMULTI | \
1252 FIF_CONTROL | \
1253 FIF_PSPOLL | \
1254 FIF_OTHER_BSS | \
1255 FIF_BCN_PRBRESP_PROMISC | \
1256 FIF_PROBE_REQ | \
1257 FIF_FCSFAIL)
1258
1259static void ath9k_htc_configure_filter(struct ieee80211_hw *hw,
1260 unsigned int changed_flags,
1261 unsigned int *total_flags,
1262 u64 multicast)
1263{
1264 struct ath9k_htc_priv *priv = hw->priv;
1265 struct ath_common *common = ath9k_hw_common(priv->ah);
1266 u32 rfilt;
1267
1268 mutex_lock(&priv->mutex);
1269 changed_flags &= SUPPORTED_FILTERS;
1270 *total_flags &= SUPPORTED_FILTERS;
1271
1272 if (test_bit(ATH_OP_INVALID, &common->op_flags)) {
1273 ath_dbg(ath9k_hw_common(priv->ah), ANY,
1274 "Unable to configure filter on invalid state\n");
1275 mutex_unlock(&priv->mutex);
1276 return;
1277 }
1278 ath9k_htc_ps_wakeup(priv);
1279
1280 priv->rxfilter = *total_flags;
1281 rfilt = ath9k_htc_calcrxfilter(priv);
1282 ath9k_hw_setrxfilter(priv->ah, rfilt);
1283
1284 ath_dbg(ath9k_hw_common(priv->ah), CONFIG, "Set HW RX filter: 0x%x\n",
1285 rfilt);
1286
1287 ath9k_htc_ps_restore(priv);
1288 mutex_unlock(&priv->mutex);
1289}
1290
1291static void ath9k_htc_sta_rc_update_work(struct work_struct *work)
1292{
1293 struct ath9k_htc_sta *ista =
1294 container_of(work, struct ath9k_htc_sta, rc_update_work);
1295 struct ieee80211_sta *sta =
1296 container_of((void *)ista, struct ieee80211_sta, drv_priv);
1297 struct ath9k_htc_priv *priv = ista->htc_priv;
1298 struct ath_common *common = ath9k_hw_common(priv->ah);
1299 struct ath9k_htc_target_rate trate;
1300
1301 mutex_lock(&priv->mutex);
1302 ath9k_htc_ps_wakeup(priv);
1303
1304 memset(&trate, 0, sizeof(struct ath9k_htc_target_rate));
1305 ath9k_htc_setup_rate(priv, sta, &trate);
1306 if (!ath9k_htc_send_rate_cmd(priv, &trate))
1307 ath_dbg(common, CONFIG,
1308 "Supported rates for sta: %pM updated, rate caps: 0x%X\n",
1309 sta->addr, be32_to_cpu(trate.capflags));
1310 else
1311 ath_dbg(common, CONFIG,
1312 "Unable to update supported rates for sta: %pM\n",
1313 sta->addr);
1314
1315 ath9k_htc_ps_restore(priv);
1316 mutex_unlock(&priv->mutex);
1317}
1318
1319static int ath9k_htc_sta_add(struct ieee80211_hw *hw,
1320 struct ieee80211_vif *vif,
1321 struct ieee80211_sta *sta)
1322{
1323 struct ath9k_htc_priv *priv = hw->priv;
1324 struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1325 int ret;
1326
1327 mutex_lock(&priv->mutex);
1328 ath9k_htc_ps_wakeup(priv);
1329 ret = ath9k_htc_add_station(priv, vif, sta);
1330 if (!ret) {
1331 INIT_WORK(&ista->rc_update_work, ath9k_htc_sta_rc_update_work);
1332 ista->htc_priv = priv;
1333 ath9k_htc_init_rate(priv, sta);
1334 }
1335 ath9k_htc_ps_restore(priv);
1336 mutex_unlock(&priv->mutex);
1337
1338 return ret;
1339}
1340
1341static int ath9k_htc_sta_remove(struct ieee80211_hw *hw,
1342 struct ieee80211_vif *vif,
1343 struct ieee80211_sta *sta)
1344{
1345 struct ath9k_htc_priv *priv = hw->priv;
1346 struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1347 int ret;
1348
1349 cancel_work_sync(&ista->rc_update_work);
1350
1351 mutex_lock(&priv->mutex);
1352 ath9k_htc_ps_wakeup(priv);
1353 htc_sta_drain(priv->htc, ista->index);
1354 ret = ath9k_htc_remove_station(priv, vif, sta);
1355 ath9k_htc_ps_restore(priv);
1356 mutex_unlock(&priv->mutex);
1357
1358 return ret;
1359}
1360
1361static void ath9k_htc_sta_rc_update(struct ieee80211_hw *hw,
1362 struct ieee80211_vif *vif,
1363 struct ieee80211_sta *sta, u32 changed)
1364{
1365 struct ath9k_htc_sta *ista = (struct ath9k_htc_sta *) sta->drv_priv;
1366
1367 if (!(changed & IEEE80211_RC_SUPP_RATES_CHANGED))
1368 return;
1369
1370 schedule_work(&ista->rc_update_work);
1371}
1372
1373static int ath9k_htc_conf_tx(struct ieee80211_hw *hw,
1374 struct ieee80211_vif *vif, u16 queue,
1375 const struct ieee80211_tx_queue_params *params)
1376{
1377 struct ath9k_htc_priv *priv = hw->priv;
1378 struct ath_common *common = ath9k_hw_common(priv->ah);
1379 struct ath9k_tx_queue_info qi;
1380 int ret = 0, qnum;
1381
1382 if (queue >= IEEE80211_NUM_ACS)
1383 return 0;
1384
1385 mutex_lock(&priv->mutex);
1386 ath9k_htc_ps_wakeup(priv);
1387
1388 memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
1389
1390 qi.tqi_aifs = params->aifs;
1391 qi.tqi_cwmin = params->cw_min;
1392 qi.tqi_cwmax = params->cw_max;
1393 qi.tqi_burstTime = params->txop * 32;
1394
1395 qnum = get_hw_qnum(queue, priv->hwq_map);
1396
1397 ath_dbg(common, CONFIG,
1398 "Configure tx [queue/hwq] [%d/%d], aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
1399 queue, qnum, params->aifs, params->cw_min,
1400 params->cw_max, params->txop);
1401
1402 ret = ath_htc_txq_update(priv, qnum, &qi);
1403 if (ret) {
1404 ath_err(common, "TXQ Update failed\n");
1405 goto out;
1406 }
1407
1408 if ((priv->ah->opmode == NL80211_IFTYPE_ADHOC) &&
1409 (qnum == priv->hwq_map[IEEE80211_AC_BE]))
1410 ath9k_htc_beaconq_config(priv);
1411out:
1412 ath9k_htc_ps_restore(priv);
1413 mutex_unlock(&priv->mutex);
1414
1415 return ret;
1416}
1417
1418static int ath9k_htc_set_key(struct ieee80211_hw *hw,
1419 enum set_key_cmd cmd,
1420 struct ieee80211_vif *vif,
1421 struct ieee80211_sta *sta,
1422 struct ieee80211_key_conf *key)
1423{
1424 struct ath9k_htc_priv *priv = hw->priv;
1425 struct ath_common *common = ath9k_hw_common(priv->ah);
1426 int ret = 0;
1427
1428 if (htc_modparam_nohwcrypt)
1429 return -ENOSPC;
1430
1431 if ((vif->type == NL80211_IFTYPE_ADHOC ||
1432 vif->type == NL80211_IFTYPE_MESH_POINT) &&
1433 (key->cipher == WLAN_CIPHER_SUITE_TKIP ||
1434 key->cipher == WLAN_CIPHER_SUITE_CCMP) &&
1435 !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
1436 /*
1437 * For now, disable hw crypto for the RSN IBSS group keys. This
1438 * could be optimized in the future to use a modified key cache
1439 * design to support per-STA RX GTK, but until that gets
1440 * implemented, use of software crypto for group addressed
1441 * frames is a acceptable to allow RSN IBSS to be used.
1442 */
1443 return -EOPNOTSUPP;
1444 }
1445
1446 mutex_lock(&priv->mutex);
1447 ath_dbg(common, CONFIG, "Set HW Key\n");
1448 ath9k_htc_ps_wakeup(priv);
1449
1450 switch (cmd) {
1451 case SET_KEY:
1452 ret = ath_key_config(common, vif, sta, key);
1453 if (ret >= 0) {
1454 key->hw_key_idx = ret;
1455 /* push IV and Michael MIC generation to stack */
1456 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1457 if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
1458 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1459 if (priv->ah->sw_mgmt_crypto_tx &&
1460 key->cipher == WLAN_CIPHER_SUITE_CCMP)
1461 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
1462 ret = 0;
1463 }
1464 break;
1465 case DISABLE_KEY:
1466 ath_key_delete(common, key);
1467 break;
1468 default:
1469 ret = -EINVAL;
1470 }
1471
1472 ath9k_htc_ps_restore(priv);
1473 mutex_unlock(&priv->mutex);
1474
1475 return ret;
1476}
1477
1478static void ath9k_htc_set_bssid(struct ath9k_htc_priv *priv)
1479{
1480 struct ath_common *common = ath9k_hw_common(priv->ah);
1481
1482 ath9k_hw_write_associd(priv->ah);
1483 ath_dbg(common, CONFIG, "BSSID: %pM aid: 0x%x\n",
1484 common->curbssid, common->curaid);
1485}
1486
1487static void ath9k_htc_bss_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
1488{
1489 struct ath9k_htc_priv *priv = (struct ath9k_htc_priv *)data;
1490 struct ath_common *common = ath9k_hw_common(priv->ah);
1491 struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
1492
1493 if ((vif->type == NL80211_IFTYPE_STATION) && bss_conf->assoc) {
1494 common->curaid = bss_conf->aid;
1495 common->last_rssi = ATH_RSSI_DUMMY_MARKER;
1496 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1497 set_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1498 }
1499}
1500
1501static void ath9k_htc_choose_set_bssid(struct ath9k_htc_priv *priv)
1502{
1503 if (priv->num_sta_assoc_vif == 1) {
1504 ieee80211_iterate_active_interfaces_atomic(
1505 priv->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1506 ath9k_htc_bss_iter, priv);
1507 ath9k_htc_set_bssid(priv);
1508 }
1509}
1510
1511static void ath9k_htc_bss_info_changed(struct ieee80211_hw *hw,
1512 struct ieee80211_vif *vif,
1513 struct ieee80211_bss_conf *bss_conf,
1514 u32 changed)
1515{
1516 struct ath9k_htc_priv *priv = hw->priv;
1517 struct ath_hw *ah = priv->ah;
1518 struct ath_common *common = ath9k_hw_common(ah);
1519 int slottime;
1520
1521 mutex_lock(&priv->mutex);
1522 ath9k_htc_ps_wakeup(priv);
1523
1524 if (changed & BSS_CHANGED_ASSOC) {
1525 ath_dbg(common, CONFIG, "BSS Changed ASSOC %d\n",
1526 bss_conf->assoc);
1527
1528 bss_conf->assoc ?
1529 priv->num_sta_assoc_vif++ : priv->num_sta_assoc_vif--;
1530
1531 if (!bss_conf->assoc)
1532 clear_bit(ATH_OP_PRIM_STA_VIF, &common->op_flags);
1533
1534 if (priv->ah->opmode == NL80211_IFTYPE_STATION) {
1535 ath9k_htc_choose_set_bssid(priv);
1536 if (bss_conf->assoc && (priv->num_sta_assoc_vif == 1))
1537 ath9k_htc_start_ani(priv);
1538 else if (priv->num_sta_assoc_vif == 0)
1539 ath9k_htc_stop_ani(priv);
1540 }
1541 }
1542
1543 if (changed & BSS_CHANGED_IBSS) {
1544 if (priv->ah->opmode == NL80211_IFTYPE_ADHOC) {
1545 common->curaid = bss_conf->aid;
1546 memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
1547 ath9k_htc_set_bssid(priv);
1548 }
1549 }
1550
1551 if ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon) {
1552 ath_dbg(common, CONFIG, "Beacon enabled for BSS: %pM\n",
1553 bss_conf->bssid);
1554 ath9k_htc_set_tsfadjust(priv, vif);
1555 priv->cur_beacon_conf.enable_beacon = 1;
1556 ath9k_htc_beacon_config(priv, vif);
1557 }
1558
1559 if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon) {
1560 /*
1561 * Disable SWBA interrupt only if there are no
1562 * concurrent AP/mesh or IBSS interfaces.
1563 */
1564 if ((priv->num_ap_vif + priv->num_mbss_vif <= 1) ||
1565 priv->num_ibss_vif) {
1566 ath_dbg(common, CONFIG,
1567 "Beacon disabled for BSS: %pM\n",
1568 bss_conf->bssid);
1569 priv->cur_beacon_conf.enable_beacon = 0;
1570 ath9k_htc_beacon_config(priv, vif);
1571 }
1572 }
1573
1574 if (changed & BSS_CHANGED_BEACON_INT) {
1575 /*
1576 * Reset the HW TSF for the first AP or mesh interface.
1577 */
1578 if (priv->nvifs == 1 &&
1579 ((priv->ah->opmode == NL80211_IFTYPE_AP &&
1580 vif->type == NL80211_IFTYPE_AP &&
1581 priv->num_ap_vif == 1) ||
1582 (priv->ah->opmode == NL80211_IFTYPE_MESH_POINT &&
1583 vif->type == NL80211_IFTYPE_MESH_POINT &&
1584 priv->num_mbss_vif == 1))) {
1585 set_bit(OP_TSF_RESET, &priv->op_flags);
1586 }
1587 ath_dbg(common, CONFIG,
1588 "Beacon interval changed for BSS: %pM\n",
1589 bss_conf->bssid);
1590 ath9k_htc_beacon_config(priv, vif);
1591 }
1592
1593 if (changed & BSS_CHANGED_ERP_SLOT) {
1594 if (bss_conf->use_short_slot)
1595 slottime = 9;
1596 else
1597 slottime = 20;
1598 if (vif->type == NL80211_IFTYPE_AP) {
1599 /*
1600 * Defer update, so that connected stations can adjust
1601 * their settings at the same time.
1602 * See beacon.c for more details
1603 */
1604 priv->beacon.slottime = slottime;
1605 priv->beacon.updateslot = UPDATE;
1606 } else {
1607 ah->slottime = slottime;
1608 ath9k_hw_init_global_settings(ah);
1609 }
1610 }
1611
1612 if (changed & BSS_CHANGED_HT)
1613 ath9k_htc_update_rate(priv, vif, bss_conf);
1614
1615 ath9k_htc_ps_restore(priv);
1616 mutex_unlock(&priv->mutex);
1617}
1618
1619static u64 ath9k_htc_get_tsf(struct ieee80211_hw *hw,
1620 struct ieee80211_vif *vif)
1621{
1622 struct ath9k_htc_priv *priv = hw->priv;
1623 u64 tsf;
1624
1625 mutex_lock(&priv->mutex);
1626 ath9k_htc_ps_wakeup(priv);
1627 tsf = ath9k_hw_gettsf64(priv->ah);
1628 ath9k_htc_ps_restore(priv);
1629 mutex_unlock(&priv->mutex);
1630
1631 return tsf;
1632}
1633
1634static void ath9k_htc_set_tsf(struct ieee80211_hw *hw,
1635 struct ieee80211_vif *vif, u64 tsf)
1636{
1637 struct ath9k_htc_priv *priv = hw->priv;
1638
1639 mutex_lock(&priv->mutex);
1640 ath9k_htc_ps_wakeup(priv);
1641 ath9k_hw_settsf64(priv->ah, tsf);
1642 ath9k_htc_ps_restore(priv);
1643 mutex_unlock(&priv->mutex);
1644}
1645
1646static void ath9k_htc_reset_tsf(struct ieee80211_hw *hw,
1647 struct ieee80211_vif *vif)
1648{
1649 struct ath9k_htc_priv *priv = hw->priv;
1650
1651 mutex_lock(&priv->mutex);
1652 ath9k_htc_ps_wakeup(priv);
1653 ath9k_hw_reset_tsf(priv->ah);
1654 ath9k_htc_ps_restore(priv);
1655 mutex_unlock(&priv->mutex);
1656}
1657
1658static int ath9k_htc_ampdu_action(struct ieee80211_hw *hw,
1659 struct ieee80211_vif *vif,
1660 struct ieee80211_ampdu_params *params)
1661{
1662 struct ath9k_htc_priv *priv = hw->priv;
1663 struct ath9k_htc_sta *ista;
1664 int ret = 0;
1665 struct ieee80211_sta *sta = params->sta;
1666 enum ieee80211_ampdu_mlme_action action = params->action;
1667 u16 tid = params->tid;
1668
1669 mutex_lock(&priv->mutex);
1670 ath9k_htc_ps_wakeup(priv);
1671
1672 switch (action) {
1673 case IEEE80211_AMPDU_RX_START:
1674 break;
1675 case IEEE80211_AMPDU_RX_STOP:
1676 break;
1677 case IEEE80211_AMPDU_TX_START:
1678 ret = ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1679 if (!ret)
1680 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1681 break;
1682 case IEEE80211_AMPDU_TX_STOP_CONT:
1683 case IEEE80211_AMPDU_TX_STOP_FLUSH:
1684 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1685 ath9k_htc_tx_aggr_oper(priv, vif, sta, action, tid);
1686 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1687 break;
1688 case IEEE80211_AMPDU_TX_OPERATIONAL:
1689 ista = (struct ath9k_htc_sta *) sta->drv_priv;
1690 spin_lock_bh(&priv->tx.tx_lock);
1691 ista->tid_state[tid] = AGGR_OPERATIONAL;
1692 spin_unlock_bh(&priv->tx.tx_lock);
1693 break;
1694 default:
1695 ath_err(ath9k_hw_common(priv->ah), "Unknown AMPDU action\n");
1696 }
1697
1698 ath9k_htc_ps_restore(priv);
1699 mutex_unlock(&priv->mutex);
1700
1701 return ret;
1702}
1703
1704static void ath9k_htc_sw_scan_start(struct ieee80211_hw *hw,
1705 struct ieee80211_vif *vif,
1706 const u8 *mac_addr)
1707{
1708 struct ath9k_htc_priv *priv = hw->priv;
1709 struct ath_common *common = ath9k_hw_common(priv->ah);
1710
1711 mutex_lock(&priv->mutex);
1712 spin_lock_bh(&priv->beacon_lock);
1713 set_bit(ATH_OP_SCANNING, &common->op_flags);
1714 spin_unlock_bh(&priv->beacon_lock);
1715 cancel_work_sync(&priv->ps_work);
1716 ath9k_htc_stop_ani(priv);
1717 mutex_unlock(&priv->mutex);
1718}
1719
1720static void ath9k_htc_sw_scan_complete(struct ieee80211_hw *hw,
1721 struct ieee80211_vif *vif)
1722{
1723 struct ath9k_htc_priv *priv = hw->priv;
1724 struct ath_common *common = ath9k_hw_common(priv->ah);
1725
1726 mutex_lock(&priv->mutex);
1727 spin_lock_bh(&priv->beacon_lock);
1728 clear_bit(ATH_OP_SCANNING, &common->op_flags);
1729 spin_unlock_bh(&priv->beacon_lock);
1730 ath9k_htc_ps_wakeup(priv);
1731 ath9k_htc_vif_reconfig(priv);
1732 ath9k_htc_ps_restore(priv);
1733 mutex_unlock(&priv->mutex);
1734}
1735
1736static int ath9k_htc_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
1737{
1738 return 0;
1739}
1740
1741static void ath9k_htc_set_coverage_class(struct ieee80211_hw *hw,
1742 s16 coverage_class)
1743{
1744 struct ath9k_htc_priv *priv = hw->priv;
1745
1746 mutex_lock(&priv->mutex);
1747 ath9k_htc_ps_wakeup(priv);
1748 priv->ah->coverage_class = coverage_class;
1749 ath9k_hw_init_global_settings(priv->ah);
1750 ath9k_htc_ps_restore(priv);
1751 mutex_unlock(&priv->mutex);
1752}
1753
1754/*
1755 * Currently, this is used only for selecting the minimum rate
1756 * for management frames, rate selection for data frames remain
1757 * unaffected.
1758 */
1759static int ath9k_htc_set_bitrate_mask(struct ieee80211_hw *hw,
1760 struct ieee80211_vif *vif,
1761 const struct cfg80211_bitrate_mask *mask)
1762{
1763 struct ath9k_htc_priv *priv = hw->priv;
1764 struct ath_common *common = ath9k_hw_common(priv->ah);
1765 struct ath9k_htc_target_rate_mask tmask;
1766 struct ath9k_htc_vif *avp = (void *)vif->drv_priv;
1767 int ret = 0;
1768 u8 cmd_rsp;
1769
1770 memset(&tmask, 0, sizeof(struct ath9k_htc_target_rate_mask));
1771
1772 tmask.vif_index = avp->index;
1773 tmask.band = IEEE80211_BAND_2GHZ;
1774 tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_2GHZ].legacy);
1775
1776 WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1777 if (ret) {
1778 ath_err(common,
1779 "Unable to set 2G rate mask for "
1780 "interface at idx: %d\n", avp->index);
1781 goto out;
1782 }
1783
1784 tmask.band = IEEE80211_BAND_5GHZ;
1785 tmask.mask = cpu_to_be32(mask->control[IEEE80211_BAND_5GHZ].legacy);
1786
1787 WMI_CMD_BUF(WMI_BITRATE_MASK_CMDID, &tmask);
1788 if (ret) {
1789 ath_err(common,
1790 "Unable to set 5G rate mask for "
1791 "interface at idx: %d\n", avp->index);
1792 goto out;
1793 }
1794
1795 ath_dbg(common, CONFIG, "Set bitrate masks: 0x%x, 0x%x\n",
1796 mask->control[IEEE80211_BAND_2GHZ].legacy,
1797 mask->control[IEEE80211_BAND_5GHZ].legacy);
1798out:
1799 return ret;
1800}
1801
1802
1803static int ath9k_htc_get_stats(struct ieee80211_hw *hw,
1804 struct ieee80211_low_level_stats *stats)
1805{
1806 struct ath9k_htc_priv *priv = hw->priv;
1807 struct ath_hw *ah = priv->ah;
1808 struct ath9k_mib_stats *mib_stats = &ah->ah_mibStats;
1809
1810 stats->dot11ACKFailureCount = mib_stats->ackrcv_bad;
1811 stats->dot11RTSFailureCount = mib_stats->rts_bad;
1812 stats->dot11FCSErrorCount = mib_stats->fcs_bad;
1813 stats->dot11RTSSuccessCount = mib_stats->rts_good;
1814
1815 return 0;
1816}
1817
1818struct base_eep_header *ath9k_htc_get_eeprom_base(struct ath9k_htc_priv *priv)
1819{
1820 struct base_eep_header *pBase = NULL;
1821 /*
1822 * This can be done since all the 3 EEPROM families have the
1823 * same base header upto a certain point, and we are interested in
1824 * the data only upto that point.
1825 */
1826
1827 if (AR_SREV_9271(priv->ah))
1828 pBase = (struct base_eep_header *)
1829 &priv->ah->eeprom.map4k.baseEepHeader;
1830 else if (priv->ah->hw_version.usbdev == AR9280_USB)
1831 pBase = (struct base_eep_header *)
1832 &priv->ah->eeprom.def.baseEepHeader;
1833 else if (priv->ah->hw_version.usbdev == AR9287_USB)
1834 pBase = (struct base_eep_header *)
1835 &priv->ah->eeprom.map9287.baseEepHeader;
1836 return pBase;
1837}
1838
1839
1840static int ath9k_htc_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant,
1841 u32 *rx_ant)
1842{
1843 struct ath9k_htc_priv *priv = hw->priv;
1844 struct base_eep_header *pBase = ath9k_htc_get_eeprom_base(priv);
1845 if (pBase) {
1846 *tx_ant = pBase->txMask;
1847 *rx_ant = pBase->rxMask;
1848 } else {
1849 *tx_ant = 0;
1850 *rx_ant = 0;
1851 }
1852 return 0;
1853}
1854
1855static void ath9k_htc_channel_switch_beacon(struct ieee80211_hw *hw,
1856 struct ieee80211_vif *vif,
1857 struct cfg80211_chan_def *chandef)
1858{
1859 struct ath9k_htc_priv *priv = hw->priv;
1860
1861 /* mac80211 does not support CSA in multi-if cases (yet) */
1862 if (WARN_ON(priv->csa_vif))
1863 return;
1864
1865 priv->csa_vif = vif;
1866}
1867
1868struct ieee80211_ops ath9k_htc_ops = {
1869 .tx = ath9k_htc_tx,
1870 .start = ath9k_htc_start,
1871 .stop = ath9k_htc_stop,
1872 .add_interface = ath9k_htc_add_interface,
1873 .remove_interface = ath9k_htc_remove_interface,
1874 .config = ath9k_htc_config,
1875 .configure_filter = ath9k_htc_configure_filter,
1876 .sta_add = ath9k_htc_sta_add,
1877 .sta_remove = ath9k_htc_sta_remove,
1878 .conf_tx = ath9k_htc_conf_tx,
1879 .sta_rc_update = ath9k_htc_sta_rc_update,
1880 .bss_info_changed = ath9k_htc_bss_info_changed,
1881 .set_key = ath9k_htc_set_key,
1882 .get_tsf = ath9k_htc_get_tsf,
1883 .set_tsf = ath9k_htc_set_tsf,
1884 .reset_tsf = ath9k_htc_reset_tsf,
1885 .ampdu_action = ath9k_htc_ampdu_action,
1886 .sw_scan_start = ath9k_htc_sw_scan_start,
1887 .sw_scan_complete = ath9k_htc_sw_scan_complete,
1888 .set_rts_threshold = ath9k_htc_set_rts_threshold,
1889 .rfkill_poll = ath9k_htc_rfkill_poll_state,
1890 .set_coverage_class = ath9k_htc_set_coverage_class,
1891 .set_bitrate_mask = ath9k_htc_set_bitrate_mask,
1892 .get_stats = ath9k_htc_get_stats,
1893 .get_antenna = ath9k_htc_get_antenna,
1894 .channel_switch_beacon = ath9k_htc_channel_switch_beacon,
1895
1896#ifdef CONFIG_ATH9K_HTC_DEBUGFS
1897 .get_et_sset_count = ath9k_htc_get_et_sset_count,
1898 .get_et_stats = ath9k_htc_get_et_stats,
1899 .get_et_strings = ath9k_htc_get_et_strings,
1900#endif
1901};