ath9k: Fix up hardware mode and beacons with multiple vifs.
[linux-2.6/btrfs-unstable.git] / drivers / net / wireless / ath / ath9k / virtual.c
blobd205c66cd972ecff4f88bac24f63ca830b8468d1
1 /*
2 * Copyright (c) 2008-2009 Atheros Communications Inc.
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.
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.
17 #include <linux/slab.h>
19 #include "ath9k.h"
21 int ath9k_wiphy_add(struct ath_softc *sc)
23 int i, error;
24 struct ath_wiphy *aphy;
25 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
26 struct ieee80211_hw *hw;
27 u8 addr[ETH_ALEN];
29 hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy), &ath9k_ops);
30 if (hw == NULL)
31 return -ENOMEM;
33 spin_lock_bh(&sc->wiphy_lock);
34 for (i = 0; i < sc->num_sec_wiphy; i++) {
35 if (sc->sec_wiphy[i] == NULL)
36 break;
39 if (i == sc->num_sec_wiphy) {
40 /* No empty slot available; increase array length */
41 struct ath_wiphy **n;
42 n = krealloc(sc->sec_wiphy,
43 (sc->num_sec_wiphy + 1) *
44 sizeof(struct ath_wiphy *),
45 GFP_ATOMIC);
46 if (n == NULL) {
47 spin_unlock_bh(&sc->wiphy_lock);
48 ieee80211_free_hw(hw);
49 return -ENOMEM;
51 n[i] = NULL;
52 sc->sec_wiphy = n;
53 sc->num_sec_wiphy++;
56 SET_IEEE80211_DEV(hw, sc->dev);
58 aphy = hw->priv;
59 aphy->sc = sc;
60 aphy->hw = hw;
61 sc->sec_wiphy[i] = aphy;
62 aphy->last_rssi = ATH_RSSI_DUMMY_MARKER;
63 spin_unlock_bh(&sc->wiphy_lock);
65 memcpy(addr, common->macaddr, ETH_ALEN);
66 addr[0] |= 0x02; /* Locally managed address */
68 * XOR virtual wiphy index into the least significant bits to generate
69 * a different MAC address for each virtual wiphy.
71 addr[5] ^= i & 0xff;
72 addr[4] ^= (i & 0xff00) >> 8;
73 addr[3] ^= (i & 0xff0000) >> 16;
75 SET_IEEE80211_PERM_ADDR(hw, addr);
77 ath9k_set_hw_capab(sc, hw);
79 error = ieee80211_register_hw(hw);
81 if (error == 0) {
82 /* Make sure wiphy scheduler is started (if enabled) */
83 ath9k_wiphy_set_scheduler(sc, sc->wiphy_scheduler_int);
86 return error;
89 int ath9k_wiphy_del(struct ath_wiphy *aphy)
91 struct ath_softc *sc = aphy->sc;
92 int i;
94 spin_lock_bh(&sc->wiphy_lock);
95 for (i = 0; i < sc->num_sec_wiphy; i++) {
96 if (aphy == sc->sec_wiphy[i]) {
97 sc->sec_wiphy[i] = NULL;
98 spin_unlock_bh(&sc->wiphy_lock);
99 ieee80211_unregister_hw(aphy->hw);
100 ieee80211_free_hw(aphy->hw);
101 return 0;
104 spin_unlock_bh(&sc->wiphy_lock);
105 return -ENOENT;
108 static int ath9k_send_nullfunc(struct ath_wiphy *aphy,
109 struct ieee80211_vif *vif, const u8 *bssid,
110 int ps)
112 struct ath_softc *sc = aphy->sc;
113 struct ath_tx_control txctl;
114 struct sk_buff *skb;
115 struct ieee80211_hdr *hdr;
116 __le16 fc;
117 struct ieee80211_tx_info *info;
119 skb = dev_alloc_skb(24);
120 if (skb == NULL)
121 return -ENOMEM;
122 hdr = (struct ieee80211_hdr *) skb_put(skb, 24);
123 memset(hdr, 0, 24);
124 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
125 IEEE80211_FCTL_TODS);
126 if (ps)
127 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
128 hdr->frame_control = fc;
129 memcpy(hdr->addr1, bssid, ETH_ALEN);
130 memcpy(hdr->addr2, aphy->hw->wiphy->perm_addr, ETH_ALEN);
131 memcpy(hdr->addr3, bssid, ETH_ALEN);
133 info = IEEE80211_SKB_CB(skb);
134 memset(info, 0, sizeof(*info));
135 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS;
136 info->control.vif = vif;
137 info->control.rates[0].idx = 0;
138 info->control.rates[0].count = 4;
139 info->control.rates[1].idx = -1;
141 memset(&txctl, 0, sizeof(struct ath_tx_control));
142 txctl.txq = sc->tx.txq_map[WME_AC_VO];
143 txctl.frame_type = ps ? ATH9K_IFT_PAUSE : ATH9K_IFT_UNPAUSE;
145 if (ath_tx_start(aphy->hw, skb, &txctl) != 0)
146 goto exit;
148 return 0;
149 exit:
150 dev_kfree_skb_any(skb);
151 return -1;
154 static bool __ath9k_wiphy_pausing(struct ath_softc *sc)
156 int i;
157 if (sc->pri_wiphy->state == ATH_WIPHY_PAUSING)
158 return true;
159 for (i = 0; i < sc->num_sec_wiphy; i++) {
160 if (sc->sec_wiphy[i] &&
161 sc->sec_wiphy[i]->state == ATH_WIPHY_PAUSING)
162 return true;
164 return false;
167 static bool ath9k_wiphy_pausing(struct ath_softc *sc)
169 bool ret;
170 spin_lock_bh(&sc->wiphy_lock);
171 ret = __ath9k_wiphy_pausing(sc);
172 spin_unlock_bh(&sc->wiphy_lock);
173 return ret;
176 static bool __ath9k_wiphy_scanning(struct ath_softc *sc)
178 int i;
179 if (sc->pri_wiphy->state == ATH_WIPHY_SCAN)
180 return true;
181 for (i = 0; i < sc->num_sec_wiphy; i++) {
182 if (sc->sec_wiphy[i] &&
183 sc->sec_wiphy[i]->state == ATH_WIPHY_SCAN)
184 return true;
186 return false;
189 bool ath9k_wiphy_scanning(struct ath_softc *sc)
191 bool ret;
192 spin_lock_bh(&sc->wiphy_lock);
193 ret = __ath9k_wiphy_scanning(sc);
194 spin_unlock_bh(&sc->wiphy_lock);
195 return ret;
198 static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy);
200 /* caller must hold wiphy_lock */
201 static void __ath9k_wiphy_unpause_ch(struct ath_wiphy *aphy)
203 if (aphy == NULL)
204 return;
205 if (aphy->chan_idx != aphy->sc->chan_idx)
206 return; /* wiphy not on the selected channel */
207 __ath9k_wiphy_unpause(aphy);
210 static void ath9k_wiphy_unpause_channel(struct ath_softc *sc)
212 int i;
213 spin_lock_bh(&sc->wiphy_lock);
214 __ath9k_wiphy_unpause_ch(sc->pri_wiphy);
215 for (i = 0; i < sc->num_sec_wiphy; i++)
216 __ath9k_wiphy_unpause_ch(sc->sec_wiphy[i]);
217 spin_unlock_bh(&sc->wiphy_lock);
220 void ath9k_wiphy_chan_work(struct work_struct *work)
222 struct ath_softc *sc = container_of(work, struct ath_softc, chan_work);
223 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
224 struct ath_wiphy *aphy = sc->next_wiphy;
226 if (aphy == NULL)
227 return;
230 * All pending interfaces paused; ready to change
231 * channels.
234 /* Change channels */
235 mutex_lock(&sc->mutex);
236 /* XXX: remove me eventually */
237 ath9k_update_ichannel(sc, aphy->hw,
238 &sc->sc_ah->channels[sc->chan_idx]);
240 /* sync hw configuration for hw code */
241 common->hw = aphy->hw;
243 if (ath_set_channel(sc, aphy->hw,
244 &sc->sc_ah->channels[sc->chan_idx]) < 0) {
245 printk(KERN_DEBUG "ath9k: Failed to set channel for new "
246 "virtual wiphy\n");
247 mutex_unlock(&sc->mutex);
248 return;
250 mutex_unlock(&sc->mutex);
252 ath9k_wiphy_unpause_channel(sc);
256 * ath9k version of ieee80211_tx_status() for TX frames that are generated
257 * internally in the driver.
259 void ath9k_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb, int ftype)
261 struct ath_wiphy *aphy = hw->priv;
262 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
264 if (ftype == ATH9K_IFT_PAUSE && aphy->state == ATH_WIPHY_PAUSING) {
265 if (!(tx_info->flags & IEEE80211_TX_STAT_ACK)) {
266 printk(KERN_DEBUG "ath9k: %s: no ACK for pause "
267 "frame\n", wiphy_name(hw->wiphy));
269 * The AP did not reply; ignore this to allow us to
270 * continue.
273 aphy->state = ATH_WIPHY_PAUSED;
274 if (!ath9k_wiphy_pausing(aphy->sc)) {
276 * Drop from tasklet to work to allow mutex for channel
277 * change.
279 ieee80211_queue_work(aphy->sc->hw,
280 &aphy->sc->chan_work);
284 dev_kfree_skb(skb);
287 static void ath9k_mark_paused(struct ath_wiphy *aphy)
289 struct ath_softc *sc = aphy->sc;
290 aphy->state = ATH_WIPHY_PAUSED;
291 if (!__ath9k_wiphy_pausing(sc))
292 ieee80211_queue_work(sc->hw, &sc->chan_work);
295 static void ath9k_pause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
297 struct ath_wiphy *aphy = data;
298 struct ath_vif *avp = (void *) vif->drv_priv;
300 switch (vif->type) {
301 case NL80211_IFTYPE_STATION:
302 if (!vif->bss_conf.assoc) {
303 ath9k_mark_paused(aphy);
304 break;
306 /* TODO: could avoid this if already in PS mode */
307 if (ath9k_send_nullfunc(aphy, vif, avp->bssid, 1)) {
308 printk(KERN_DEBUG "%s: failed to send PS nullfunc\n",
309 __func__);
310 ath9k_mark_paused(aphy);
312 break;
313 case NL80211_IFTYPE_AP:
314 /* Beacon transmission is paused by aphy->state change */
315 ath9k_mark_paused(aphy);
316 break;
317 default:
318 break;
322 /* caller must hold wiphy_lock */
323 static int __ath9k_wiphy_pause(struct ath_wiphy *aphy)
325 ieee80211_stop_queues(aphy->hw);
326 aphy->state = ATH_WIPHY_PAUSING;
328 * TODO: handle PAUSING->PAUSED for the case where there are multiple
329 * active vifs (now we do it on the first vif getting ready; should be
330 * on the last)
332 ieee80211_iterate_active_interfaces_atomic(aphy->hw, ath9k_pause_iter,
333 aphy);
334 return 0;
337 int ath9k_wiphy_pause(struct ath_wiphy *aphy)
339 int ret;
340 spin_lock_bh(&aphy->sc->wiphy_lock);
341 ret = __ath9k_wiphy_pause(aphy);
342 spin_unlock_bh(&aphy->sc->wiphy_lock);
343 return ret;
346 static void ath9k_unpause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
348 struct ath_wiphy *aphy = data;
349 struct ath_vif *avp = (void *) vif->drv_priv;
351 switch (vif->type) {
352 case NL80211_IFTYPE_STATION:
353 if (!vif->bss_conf.assoc)
354 break;
355 ath9k_send_nullfunc(aphy, vif, avp->bssid, 0);
356 break;
357 case NL80211_IFTYPE_AP:
358 /* Beacon transmission is re-enabled by aphy->state change */
359 break;
360 default:
361 break;
365 /* caller must hold wiphy_lock */
366 static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy)
368 ieee80211_iterate_active_interfaces_atomic(aphy->hw,
369 ath9k_unpause_iter, aphy);
370 aphy->state = ATH_WIPHY_ACTIVE;
371 ieee80211_wake_queues(aphy->hw);
372 return 0;
375 int ath9k_wiphy_unpause(struct ath_wiphy *aphy)
377 int ret;
378 spin_lock_bh(&aphy->sc->wiphy_lock);
379 ret = __ath9k_wiphy_unpause(aphy);
380 spin_unlock_bh(&aphy->sc->wiphy_lock);
381 return ret;
384 static void __ath9k_wiphy_mark_all_paused(struct ath_softc *sc)
386 int i;
387 if (sc->pri_wiphy->state != ATH_WIPHY_INACTIVE)
388 sc->pri_wiphy->state = ATH_WIPHY_PAUSED;
389 for (i = 0; i < sc->num_sec_wiphy; i++) {
390 if (sc->sec_wiphy[i] &&
391 sc->sec_wiphy[i]->state != ATH_WIPHY_INACTIVE)
392 sc->sec_wiphy[i]->state = ATH_WIPHY_PAUSED;
396 /* caller must hold wiphy_lock */
397 static void __ath9k_wiphy_pause_all(struct ath_softc *sc)
399 int i;
400 if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE)
401 __ath9k_wiphy_pause(sc->pri_wiphy);
402 for (i = 0; i < sc->num_sec_wiphy; i++) {
403 if (sc->sec_wiphy[i] &&
404 sc->sec_wiphy[i]->state == ATH_WIPHY_ACTIVE)
405 __ath9k_wiphy_pause(sc->sec_wiphy[i]);
409 int ath9k_wiphy_select(struct ath_wiphy *aphy)
411 struct ath_softc *sc = aphy->sc;
412 bool now;
414 spin_lock_bh(&sc->wiphy_lock);
415 if (__ath9k_wiphy_scanning(sc)) {
417 * For now, we are using mac80211 sw scan and it expects to
418 * have full control over channel changes, so avoid wiphy
419 * scheduling during a scan. This could be optimized if the
420 * scanning control were moved into the driver.
422 spin_unlock_bh(&sc->wiphy_lock);
423 return -EBUSY;
425 if (__ath9k_wiphy_pausing(sc)) {
426 if (sc->wiphy_select_failures == 0)
427 sc->wiphy_select_first_fail = jiffies;
428 sc->wiphy_select_failures++;
429 if (time_after(jiffies, sc->wiphy_select_first_fail + HZ / 2))
431 printk(KERN_DEBUG "ath9k: Previous wiphy select timed "
432 "out; disable/enable hw to recover\n");
433 __ath9k_wiphy_mark_all_paused(sc);
435 * TODO: this workaround to fix hardware is unlikely to
436 * be specific to virtual wiphy changes. It can happen
437 * on normal channel change, too, and as such, this
438 * should really be made more generic. For example,
439 * tricker radio disable/enable on GTT interrupt burst
440 * (say, 10 GTT interrupts received without any TX
441 * frame being completed)
443 spin_unlock_bh(&sc->wiphy_lock);
444 ath_radio_disable(sc, aphy->hw);
445 ath_radio_enable(sc, aphy->hw);
446 /* Only the primary wiphy hw is used for queuing work */
447 ieee80211_queue_work(aphy->sc->hw,
448 &aphy->sc->chan_work);
449 return -EBUSY; /* previous select still in progress */
451 spin_unlock_bh(&sc->wiphy_lock);
452 return -EBUSY; /* previous select still in progress */
454 sc->wiphy_select_failures = 0;
456 /* Store the new channel */
457 sc->chan_idx = aphy->chan_idx;
458 sc->chan_is_ht = aphy->chan_is_ht;
459 sc->next_wiphy = aphy;
461 __ath9k_wiphy_pause_all(sc);
462 now = !__ath9k_wiphy_pausing(aphy->sc);
463 spin_unlock_bh(&sc->wiphy_lock);
465 if (now) {
466 /* Ready to request channel change immediately */
467 ieee80211_queue_work(aphy->sc->hw, &aphy->sc->chan_work);
471 * wiphys will be unpaused in ath9k_tx_status() once channel has been
472 * changed if any wiphy needs time to become paused.
475 return 0;
478 bool ath9k_wiphy_started(struct ath_softc *sc)
480 int i;
481 spin_lock_bh(&sc->wiphy_lock);
482 if (sc->pri_wiphy->state != ATH_WIPHY_INACTIVE) {
483 spin_unlock_bh(&sc->wiphy_lock);
484 return true;
486 for (i = 0; i < sc->num_sec_wiphy; i++) {
487 if (sc->sec_wiphy[i] &&
488 sc->sec_wiphy[i]->state != ATH_WIPHY_INACTIVE) {
489 spin_unlock_bh(&sc->wiphy_lock);
490 return true;
493 spin_unlock_bh(&sc->wiphy_lock);
494 return false;
497 static void ath9k_wiphy_pause_chan(struct ath_wiphy *aphy,
498 struct ath_wiphy *selected)
500 if (selected->state == ATH_WIPHY_SCAN) {
501 if (aphy == selected)
502 return;
504 * Pause all other wiphys for the duration of the scan even if
505 * they are on the current channel now.
507 } else if (aphy->chan_idx == selected->chan_idx)
508 return;
509 aphy->state = ATH_WIPHY_PAUSED;
510 ieee80211_stop_queues(aphy->hw);
513 void ath9k_wiphy_pause_all_forced(struct ath_softc *sc,
514 struct ath_wiphy *selected)
516 int i;
517 spin_lock_bh(&sc->wiphy_lock);
518 if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE)
519 ath9k_wiphy_pause_chan(sc->pri_wiphy, selected);
520 for (i = 0; i < sc->num_sec_wiphy; i++) {
521 if (sc->sec_wiphy[i] &&
522 sc->sec_wiphy[i]->state == ATH_WIPHY_ACTIVE)
523 ath9k_wiphy_pause_chan(sc->sec_wiphy[i], selected);
525 spin_unlock_bh(&sc->wiphy_lock);
528 void ath9k_wiphy_work(struct work_struct *work)
530 struct ath_softc *sc = container_of(work, struct ath_softc,
531 wiphy_work.work);
532 struct ath_wiphy *aphy = NULL;
533 bool first = true;
535 spin_lock_bh(&sc->wiphy_lock);
537 if (sc->wiphy_scheduler_int == 0) {
538 /* wiphy scheduler is disabled */
539 spin_unlock_bh(&sc->wiphy_lock);
540 return;
543 try_again:
544 sc->wiphy_scheduler_index++;
545 while (sc->wiphy_scheduler_index <= sc->num_sec_wiphy) {
546 aphy = sc->sec_wiphy[sc->wiphy_scheduler_index - 1];
547 if (aphy && aphy->state != ATH_WIPHY_INACTIVE)
548 break;
550 sc->wiphy_scheduler_index++;
551 aphy = NULL;
553 if (aphy == NULL) {
554 sc->wiphy_scheduler_index = 0;
555 if (sc->pri_wiphy->state == ATH_WIPHY_INACTIVE) {
556 if (first) {
557 first = false;
558 goto try_again;
560 /* No wiphy is ready to be scheduled */
561 } else
562 aphy = sc->pri_wiphy;
565 spin_unlock_bh(&sc->wiphy_lock);
567 if (aphy &&
568 aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN &&
569 ath9k_wiphy_select(aphy)) {
570 printk(KERN_DEBUG "ath9k: Failed to schedule virtual wiphy "
571 "change\n");
574 ieee80211_queue_delayed_work(sc->hw,
575 &sc->wiphy_work,
576 sc->wiphy_scheduler_int);
579 void ath9k_wiphy_set_scheduler(struct ath_softc *sc, unsigned int msec_int)
581 cancel_delayed_work_sync(&sc->wiphy_work);
582 sc->wiphy_scheduler_int = msecs_to_jiffies(msec_int);
583 if (sc->wiphy_scheduler_int)
584 ieee80211_queue_delayed_work(sc->hw, &sc->wiphy_work,
585 sc->wiphy_scheduler_int);
588 /* caller must hold wiphy_lock */
589 bool ath9k_all_wiphys_idle(struct ath_softc *sc)
591 unsigned int i;
592 if (!sc->pri_wiphy->idle)
593 return false;
594 for (i = 0; i < sc->num_sec_wiphy; i++) {
595 struct ath_wiphy *aphy = sc->sec_wiphy[i];
596 if (!aphy)
597 continue;
598 if (!aphy->idle)
599 return false;
601 return true;
604 /* caller must hold wiphy_lock */
605 void ath9k_set_wiphy_idle(struct ath_wiphy *aphy, bool idle)
607 struct ath_softc *sc = aphy->sc;
609 aphy->idle = idle;
610 ath_dbg(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
611 "Marking %s as %sidle\n",
612 wiphy_name(aphy->hw->wiphy), idle ? "" : "not-");
614 /* Only bother starting a queue on an active virtual wiphy */
615 bool ath_mac80211_start_queue(struct ath_softc *sc, u16 skb_queue)
617 struct ieee80211_hw *hw = sc->pri_wiphy->hw;
618 unsigned int i;
619 bool txq_started = false;
621 spin_lock_bh(&sc->wiphy_lock);
623 /* Start the primary wiphy */
624 if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE) {
625 ieee80211_wake_queue(hw, skb_queue);
626 txq_started = true;
627 goto unlock;
630 /* Now start the secondary wiphy queues */
631 for (i = 0; i < sc->num_sec_wiphy; i++) {
632 struct ath_wiphy *aphy = sc->sec_wiphy[i];
633 if (!aphy)
634 continue;
635 if (aphy->state != ATH_WIPHY_ACTIVE)
636 continue;
638 hw = aphy->hw;
639 ieee80211_wake_queue(hw, skb_queue);
640 txq_started = true;
641 break;
644 unlock:
645 spin_unlock_bh(&sc->wiphy_lock);
646 return txq_started;
649 /* Go ahead and propagate information to all virtual wiphys, it won't hurt */
650 void ath_mac80211_stop_queue(struct ath_softc *sc, u16 skb_queue)
652 struct ieee80211_hw *hw = sc->pri_wiphy->hw;
653 unsigned int i;
655 spin_lock_bh(&sc->wiphy_lock);
657 /* Stop the primary wiphy */
658 ieee80211_stop_queue(hw, skb_queue);
660 /* Now stop the secondary wiphy queues */
661 for (i = 0; i < sc->num_sec_wiphy; i++) {
662 struct ath_wiphy *aphy = sc->sec_wiphy[i];
663 if (!aphy)
664 continue;
665 hw = aphy->hw;
666 ieee80211_stop_queue(hw, skb_queue);
668 spin_unlock_bh(&sc->wiphy_lock);