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>
21 int ath9k_wiphy_add(struct ath_softc
*sc
)
24 struct ath_wiphy
*aphy
;
25 struct ath_common
*common
= ath9k_hw_common(sc
->sc_ah
);
26 struct ieee80211_hw
*hw
;
29 hw
= ieee80211_alloc_hw(sizeof(struct ath_wiphy
), &ath9k_ops
);
33 spin_lock_bh(&sc
->wiphy_lock
);
34 for (i
= 0; i
< sc
->num_sec_wiphy
; i
++) {
35 if (sc
->sec_wiphy
[i
] == NULL
)
39 if (i
== sc
->num_sec_wiphy
) {
40 /* No empty slot available; increase array length */
42 n
= krealloc(sc
->sec_wiphy
,
43 (sc
->num_sec_wiphy
+ 1) *
44 sizeof(struct ath_wiphy
*),
47 spin_unlock_bh(&sc
->wiphy_lock
);
48 ieee80211_free_hw(hw
);
56 SET_IEEE80211_DEV(hw
, sc
->dev
);
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.
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
);
82 /* Make sure wiphy scheduler is started (if enabled) */
83 ath9k_wiphy_set_scheduler(sc
, sc
->wiphy_scheduler_int
);
89 int ath9k_wiphy_del(struct ath_wiphy
*aphy
)
91 struct ath_softc
*sc
= aphy
->sc
;
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
);
104 spin_unlock_bh(&sc
->wiphy_lock
);
108 static int ath9k_send_nullfunc(struct ath_wiphy
*aphy
,
109 struct ieee80211_vif
*vif
, const u8
*bssid
,
112 struct ath_softc
*sc
= aphy
->sc
;
113 struct ath_tx_control txctl
;
115 struct ieee80211_hdr
*hdr
;
117 struct ieee80211_tx_info
*info
;
119 skb
= dev_alloc_skb(24);
122 hdr
= (struct ieee80211_hdr
*) skb_put(skb
, 24);
124 fc
= cpu_to_le16(IEEE80211_FTYPE_DATA
| IEEE80211_STYPE_NULLFUNC
|
125 IEEE80211_FCTL_TODS
);
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)
150 dev_kfree_skb_any(skb
);
154 static bool __ath9k_wiphy_pausing(struct ath_softc
*sc
)
157 if (sc
->pri_wiphy
->state
== ATH_WIPHY_PAUSING
)
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
)
167 static bool ath9k_wiphy_pausing(struct ath_softc
*sc
)
170 spin_lock_bh(&sc
->wiphy_lock
);
171 ret
= __ath9k_wiphy_pausing(sc
);
172 spin_unlock_bh(&sc
->wiphy_lock
);
176 static bool __ath9k_wiphy_scanning(struct ath_softc
*sc
)
179 if (sc
->pri_wiphy
->state
== ATH_WIPHY_SCAN
)
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
)
189 bool ath9k_wiphy_scanning(struct ath_softc
*sc
)
192 spin_lock_bh(&sc
->wiphy_lock
);
193 ret
= __ath9k_wiphy_scanning(sc
);
194 spin_unlock_bh(&sc
->wiphy_lock
);
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
)
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
)
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
;
230 * All pending interfaces paused; ready to change
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 "
247 mutex_unlock(&sc
->mutex
);
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
273 aphy
->state
= ATH_WIPHY_PAUSED
;
274 if (!ath9k_wiphy_pausing(aphy
->sc
)) {
276 * Drop from tasklet to work to allow mutex for channel
279 ieee80211_queue_work(aphy
->sc
->hw
,
280 &aphy
->sc
->chan_work
);
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
;
301 case NL80211_IFTYPE_STATION
:
302 if (!vif
->bss_conf
.assoc
) {
303 ath9k_mark_paused(aphy
);
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",
310 ath9k_mark_paused(aphy
);
313 case NL80211_IFTYPE_AP
:
314 /* Beacon transmission is paused by aphy->state change */
315 ath9k_mark_paused(aphy
);
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
332 ieee80211_iterate_active_interfaces_atomic(aphy
->hw
, ath9k_pause_iter
,
337 int ath9k_wiphy_pause(struct ath_wiphy
*aphy
)
340 spin_lock_bh(&aphy
->sc
->wiphy_lock
);
341 ret
= __ath9k_wiphy_pause(aphy
);
342 spin_unlock_bh(&aphy
->sc
->wiphy_lock
);
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
;
352 case NL80211_IFTYPE_STATION
:
353 if (!vif
->bss_conf
.assoc
)
355 ath9k_send_nullfunc(aphy
, vif
, avp
->bssid
, 0);
357 case NL80211_IFTYPE_AP
:
358 /* Beacon transmission is re-enabled by aphy->state change */
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
);
375 int ath9k_wiphy_unpause(struct ath_wiphy
*aphy
)
378 spin_lock_bh(&aphy
->sc
->wiphy_lock
);
379 ret
= __ath9k_wiphy_unpause(aphy
);
380 spin_unlock_bh(&aphy
->sc
->wiphy_lock
);
384 static void __ath9k_wiphy_mark_all_paused(struct ath_softc
*sc
)
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
)
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
;
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
);
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
);
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.
478 bool ath9k_wiphy_started(struct ath_softc
*sc
)
481 spin_lock_bh(&sc
->wiphy_lock
);
482 if (sc
->pri_wiphy
->state
!= ATH_WIPHY_INACTIVE
) {
483 spin_unlock_bh(&sc
->wiphy_lock
);
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
);
493 spin_unlock_bh(&sc
->wiphy_lock
);
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
)
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
)
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
)
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
,
532 struct ath_wiphy
*aphy
= NULL
;
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
);
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
)
550 sc
->wiphy_scheduler_index
++;
554 sc
->wiphy_scheduler_index
= 0;
555 if (sc
->pri_wiphy
->state
== ATH_WIPHY_INACTIVE
) {
560 /* No wiphy is ready to be scheduled */
562 aphy
= sc
->pri_wiphy
;
565 spin_unlock_bh(&sc
->wiphy_lock
);
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 "
574 ieee80211_queue_delayed_work(sc
->hw
,
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
)
592 if (!sc
->pri_wiphy
->idle
)
594 for (i
= 0; i
< sc
->num_sec_wiphy
; i
++) {
595 struct ath_wiphy
*aphy
= sc
->sec_wiphy
[i
];
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
;
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
;
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
);
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
];
635 if (aphy
->state
!= ATH_WIPHY_ACTIVE
)
639 ieee80211_wake_queue(hw
, skb_queue
);
645 spin_unlock_bh(&sc
->wiphy_lock
);
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
;
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
];
666 ieee80211_stop_queue(hw
, skb_queue
);
668 spin_unlock_bh(&sc
->wiphy_lock
);