blob: 69a871ba051f694dedec209b9dfdbe17c8306796 [file] [log] [blame]
Jouni Malinen8ca21f02009-03-03 19:23:27 +02001/*
2 * Copyright (c) 2008-2009 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 "ath9k.h"
18
19struct ath9k_vif_iter_data {
20 int count;
21 u8 *addr;
22};
23
24static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
25{
26 struct ath9k_vif_iter_data *iter_data = data;
27 u8 *nbuf;
28
29 nbuf = krealloc(iter_data->addr, (iter_data->count + 1) * ETH_ALEN,
30 GFP_ATOMIC);
31 if (nbuf == NULL)
32 return;
33
34 memcpy(nbuf + iter_data->count * ETH_ALEN, mac, ETH_ALEN);
35 iter_data->addr = nbuf;
36 iter_data->count++;
37}
38
39void ath9k_set_bssid_mask(struct ieee80211_hw *hw)
40{
Jouni Malinenbce048d2009-03-03 19:23:28 +020041 struct ath_wiphy *aphy = hw->priv;
42 struct ath_softc *sc = aphy->sc;
Luis R. Rodriguez15107182009-09-10 09:22:37 -070043 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinen8ca21f02009-03-03 19:23:27 +020044 struct ath9k_vif_iter_data iter_data;
45 int i, j;
46 u8 mask[ETH_ALEN];
47
48 /*
49 * Add primary MAC address even if it is not in active use since it
50 * will be configured to the hardware as the starting point and the
51 * BSSID mask will need to be changed if another address is active.
52 */
53 iter_data.addr = kmalloc(ETH_ALEN, GFP_ATOMIC);
54 if (iter_data.addr) {
Luis R. Rodriguez15107182009-09-10 09:22:37 -070055 memcpy(iter_data.addr, common->macaddr, ETH_ALEN);
Jouni Malinen8ca21f02009-03-03 19:23:27 +020056 iter_data.count = 1;
57 } else
58 iter_data.count = 0;
59
60 /* Get list of all active MAC addresses */
Jouni Malinenc52f33d2009-03-03 19:23:29 +020061 spin_lock_bh(&sc->wiphy_lock);
62 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath9k_vif_iter,
Jouni Malinen8ca21f02009-03-03 19:23:27 +020063 &iter_data);
Jouni Malinenc52f33d2009-03-03 19:23:29 +020064 for (i = 0; i < sc->num_sec_wiphy; i++) {
65 if (sc->sec_wiphy[i] == NULL)
66 continue;
67 ieee80211_iterate_active_interfaces_atomic(
68 sc->sec_wiphy[i]->hw, ath9k_vif_iter, &iter_data);
69 }
70 spin_unlock_bh(&sc->wiphy_lock);
Jouni Malinen8ca21f02009-03-03 19:23:27 +020071
72 /* Generate an address mask to cover all active addresses */
73 memset(mask, 0, ETH_ALEN);
74 for (i = 0; i < iter_data.count; i++) {
75 u8 *a1 = iter_data.addr + i * ETH_ALEN;
76 for (j = i + 1; j < iter_data.count; j++) {
77 u8 *a2 = iter_data.addr + j * ETH_ALEN;
78 mask[0] |= a1[0] ^ a2[0];
79 mask[1] |= a1[1] ^ a2[1];
80 mask[2] |= a1[2] ^ a2[2];
81 mask[3] |= a1[3] ^ a2[3];
82 mask[4] |= a1[4] ^ a2[4];
83 mask[5] |= a1[5] ^ a2[5];
84 }
85 }
86
87 kfree(iter_data.addr);
88
89 /* Invert the mask and configure hardware */
Luis R. Rodriguez15107182009-09-10 09:22:37 -070090 common->bssidmask[0] = ~mask[0];
91 common->bssidmask[1] = ~mask[1];
92 common->bssidmask[2] = ~mask[2];
93 common->bssidmask[3] = ~mask[3];
94 common->bssidmask[4] = ~mask[4];
95 common->bssidmask[5] = ~mask[5];
Jouni Malinen8ca21f02009-03-03 19:23:27 +020096
Luis R. Rodriguez13b81552009-09-10 17:52:45 -070097 ath_hw_setbssidmask(common);
Jouni Malinen8ca21f02009-03-03 19:23:27 +020098}
Jouni Malinenc52f33d2009-03-03 19:23:29 +020099
100int ath9k_wiphy_add(struct ath_softc *sc)
101{
102 int i, error;
103 struct ath_wiphy *aphy;
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700104 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinenc52f33d2009-03-03 19:23:29 +0200105 struct ieee80211_hw *hw;
106 u8 addr[ETH_ALEN];
107
108 hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy), &ath9k_ops);
109 if (hw == NULL)
110 return -ENOMEM;
111
112 spin_lock_bh(&sc->wiphy_lock);
113 for (i = 0; i < sc->num_sec_wiphy; i++) {
114 if (sc->sec_wiphy[i] == NULL)
115 break;
116 }
117
118 if (i == sc->num_sec_wiphy) {
119 /* No empty slot available; increase array length */
120 struct ath_wiphy **n;
121 n = krealloc(sc->sec_wiphy,
122 (sc->num_sec_wiphy + 1) *
123 sizeof(struct ath_wiphy *),
124 GFP_ATOMIC);
125 if (n == NULL) {
126 spin_unlock_bh(&sc->wiphy_lock);
127 ieee80211_free_hw(hw);
128 return -ENOMEM;
129 }
130 n[i] = NULL;
131 sc->sec_wiphy = n;
132 sc->num_sec_wiphy++;
133 }
134
135 SET_IEEE80211_DEV(hw, sc->dev);
136
137 aphy = hw->priv;
138 aphy->sc = sc;
139 aphy->hw = hw;
140 sc->sec_wiphy[i] = aphy;
141 spin_unlock_bh(&sc->wiphy_lock);
142
Luis R. Rodriguez15107182009-09-10 09:22:37 -0700143 memcpy(addr, common->macaddr, ETH_ALEN);
Jouni Malinenc52f33d2009-03-03 19:23:29 +0200144 addr[0] |= 0x02; /* Locally managed address */
145 /*
146 * XOR virtual wiphy index into the least significant bits to generate
147 * a different MAC address for each virtual wiphy.
148 */
149 addr[5] ^= i & 0xff;
150 addr[4] ^= (i & 0xff00) >> 8;
151 addr[3] ^= (i & 0xff0000) >> 16;
152
153 SET_IEEE80211_PERM_ADDR(hw, addr);
154
155 ath_set_hw_capab(sc, hw);
156
157 error = ieee80211_register_hw(hw);
158
Jouni Malinenf98c3bd2009-03-03 19:23:39 +0200159 if (error == 0) {
160 /* Make sure wiphy scheduler is started (if enabled) */
161 ath9k_wiphy_set_scheduler(sc, sc->wiphy_scheduler_int);
162 }
163
Jouni Malinenc52f33d2009-03-03 19:23:29 +0200164 return error;
165}
166
167int ath9k_wiphy_del(struct ath_wiphy *aphy)
168{
169 struct ath_softc *sc = aphy->sc;
170 int i;
171
172 spin_lock_bh(&sc->wiphy_lock);
173 for (i = 0; i < sc->num_sec_wiphy; i++) {
174 if (aphy == sc->sec_wiphy[i]) {
175 sc->sec_wiphy[i] = NULL;
176 spin_unlock_bh(&sc->wiphy_lock);
177 ieee80211_unregister_hw(aphy->hw);
178 ieee80211_free_hw(aphy->hw);
179 return 0;
180 }
181 }
182 spin_unlock_bh(&sc->wiphy_lock);
183 return -ENOENT;
184}
Jouni Malinenf0ed85c2009-03-03 19:23:31 +0200185
186static int ath9k_send_nullfunc(struct ath_wiphy *aphy,
187 struct ieee80211_vif *vif, const u8 *bssid,
188 int ps)
189{
190 struct ath_softc *sc = aphy->sc;
191 struct ath_tx_control txctl;
192 struct sk_buff *skb;
193 struct ieee80211_hdr *hdr;
194 __le16 fc;
195 struct ieee80211_tx_info *info;
196
197 skb = dev_alloc_skb(24);
198 if (skb == NULL)
199 return -ENOMEM;
200 hdr = (struct ieee80211_hdr *) skb_put(skb, 24);
201 memset(hdr, 0, 24);
202 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
203 IEEE80211_FCTL_TODS);
204 if (ps)
205 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
206 hdr->frame_control = fc;
207 memcpy(hdr->addr1, bssid, ETH_ALEN);
208 memcpy(hdr->addr2, aphy->hw->wiphy->perm_addr, ETH_ALEN);
209 memcpy(hdr->addr3, bssid, ETH_ALEN);
210
211 info = IEEE80211_SKB_CB(skb);
212 memset(info, 0, sizeof(*info));
213 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS;
214 info->control.vif = vif;
215 info->control.rates[0].idx = 0;
216 info->control.rates[0].count = 4;
217 info->control.rates[1].idx = -1;
218
219 memset(&txctl, 0, sizeof(struct ath_tx_control));
220 txctl.txq = &sc->tx.txq[sc->tx.hwq_map[ATH9K_WME_AC_VO]];
221 txctl.frame_type = ps ? ATH9K_INT_PAUSE : ATH9K_INT_UNPAUSE;
222
223 if (ath_tx_start(aphy->hw, skb, &txctl) != 0)
224 goto exit;
225
226 return 0;
227exit:
228 dev_kfree_skb_any(skb);
229 return -1;
230}
231
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200232static bool __ath9k_wiphy_pausing(struct ath_softc *sc)
233{
234 int i;
235 if (sc->pri_wiphy->state == ATH_WIPHY_PAUSING)
236 return true;
237 for (i = 0; i < sc->num_sec_wiphy; i++) {
238 if (sc->sec_wiphy[i] &&
239 sc->sec_wiphy[i]->state == ATH_WIPHY_PAUSING)
240 return true;
241 }
242 return false;
243}
244
245static bool ath9k_wiphy_pausing(struct ath_softc *sc)
246{
247 bool ret;
248 spin_lock_bh(&sc->wiphy_lock);
249 ret = __ath9k_wiphy_pausing(sc);
250 spin_unlock_bh(&sc->wiphy_lock);
251 return ret;
252}
253
Jouni Malinen8089cc42009-03-03 19:23:38 +0200254static bool __ath9k_wiphy_scanning(struct ath_softc *sc)
255{
256 int i;
257 if (sc->pri_wiphy->state == ATH_WIPHY_SCAN)
258 return true;
259 for (i = 0; i < sc->num_sec_wiphy; i++) {
260 if (sc->sec_wiphy[i] &&
261 sc->sec_wiphy[i]->state == ATH_WIPHY_SCAN)
262 return true;
263 }
264 return false;
265}
266
267bool ath9k_wiphy_scanning(struct ath_softc *sc)
268{
269 bool ret;
270 spin_lock_bh(&sc->wiphy_lock);
271 ret = __ath9k_wiphy_scanning(sc);
272 spin_unlock_bh(&sc->wiphy_lock);
273 return ret;
274}
275
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200276static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy);
277
278/* caller must hold wiphy_lock */
279static void __ath9k_wiphy_unpause_ch(struct ath_wiphy *aphy)
280{
281 if (aphy == NULL)
282 return;
283 if (aphy->chan_idx != aphy->sc->chan_idx)
284 return; /* wiphy not on the selected channel */
285 __ath9k_wiphy_unpause(aphy);
286}
287
288static void ath9k_wiphy_unpause_channel(struct ath_softc *sc)
289{
290 int i;
291 spin_lock_bh(&sc->wiphy_lock);
292 __ath9k_wiphy_unpause_ch(sc->pri_wiphy);
293 for (i = 0; i < sc->num_sec_wiphy; i++)
294 __ath9k_wiphy_unpause_ch(sc->sec_wiphy[i]);
295 spin_unlock_bh(&sc->wiphy_lock);
296}
297
298void ath9k_wiphy_chan_work(struct work_struct *work)
299{
300 struct ath_softc *sc = container_of(work, struct ath_softc, chan_work);
Luis R. Rodriguez1bdf6c32009-10-28 13:39:40 -0700301 struct ath_common *common = ath9k_hw_common(sc->sc_ah);
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200302 struct ath_wiphy *aphy = sc->next_wiphy;
303
304 if (aphy == NULL)
305 return;
306
307 /*
308 * All pending interfaces paused; ready to change
309 * channels.
310 */
311
312 /* Change channels */
313 mutex_lock(&sc->mutex);
314 /* XXX: remove me eventually */
315 ath9k_update_ichannel(sc, aphy->hw,
316 &sc->sc_ah->channels[sc->chan_idx]);
Luis R. Rodriguez1bdf6c32009-10-28 13:39:40 -0700317
318 /* sync hw configuration for hw code */
319 common->hw = aphy->hw;
320
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200321 ath_update_chainmask(sc, sc->chan_is_ht);
322 if (ath_set_channel(sc, aphy->hw,
323 &sc->sc_ah->channels[sc->chan_idx]) < 0) {
324 printk(KERN_DEBUG "ath9k: Failed to set channel for new "
325 "virtual wiphy\n");
326 mutex_unlock(&sc->mutex);
327 return;
328 }
329 mutex_unlock(&sc->mutex);
330
331 ath9k_wiphy_unpause_channel(sc);
332}
333
Jouni Malinenf0ed85c2009-03-03 19:23:31 +0200334/*
335 * ath9k version of ieee80211_tx_status() for TX frames that are generated
336 * internally in the driver.
337 */
338void ath9k_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
339{
340 struct ath_wiphy *aphy = hw->priv;
341 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
342 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
343 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
344
345 if (tx_info_priv && tx_info_priv->frame_type == ATH9K_INT_PAUSE &&
346 aphy->state == ATH_WIPHY_PAUSING) {
347 if (!(info->flags & IEEE80211_TX_STAT_ACK)) {
348 printk(KERN_DEBUG "ath9k: %s: no ACK for pause "
349 "frame\n", wiphy_name(hw->wiphy));
350 /*
351 * The AP did not reply; ignore this to allow us to
352 * continue.
353 */
354 }
355 aphy->state = ATH_WIPHY_PAUSED;
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200356 if (!ath9k_wiphy_pausing(aphy->sc)) {
357 /*
358 * Drop from tasklet to work to allow mutex for channel
359 * change.
360 */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400361 ieee80211_queue_work(aphy->sc->hw,
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200362 &aphy->sc->chan_work);
363 }
Jouni Malinenf0ed85c2009-03-03 19:23:31 +0200364 }
365
366 kfree(tx_info_priv);
367 tx_info->rate_driver_data[0] = NULL;
368
369 dev_kfree_skb(skb);
370}
371
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200372static void ath9k_mark_paused(struct ath_wiphy *aphy)
373{
374 struct ath_softc *sc = aphy->sc;
375 aphy->state = ATH_WIPHY_PAUSED;
376 if (!__ath9k_wiphy_pausing(sc))
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400377 ieee80211_queue_work(sc->hw, &sc->chan_work);
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200378}
379
Jouni Malinenf0ed85c2009-03-03 19:23:31 +0200380static void ath9k_pause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
381{
382 struct ath_wiphy *aphy = data;
383 struct ath_vif *avp = (void *) vif->drv_priv;
384
385 switch (vif->type) {
386 case NL80211_IFTYPE_STATION:
387 if (!vif->bss_conf.assoc) {
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200388 ath9k_mark_paused(aphy);
Jouni Malinenf0ed85c2009-03-03 19:23:31 +0200389 break;
390 }
391 /* TODO: could avoid this if already in PS mode */
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200392 if (ath9k_send_nullfunc(aphy, vif, avp->bssid, 1)) {
393 printk(KERN_DEBUG "%s: failed to send PS nullfunc\n",
394 __func__);
395 ath9k_mark_paused(aphy);
396 }
Jouni Malinenf0ed85c2009-03-03 19:23:31 +0200397 break;
398 case NL80211_IFTYPE_AP:
399 /* Beacon transmission is paused by aphy->state change */
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200400 ath9k_mark_paused(aphy);
Jouni Malinenf0ed85c2009-03-03 19:23:31 +0200401 break;
402 default:
403 break;
404 }
405}
406
407/* caller must hold wiphy_lock */
408static int __ath9k_wiphy_pause(struct ath_wiphy *aphy)
409{
410 ieee80211_stop_queues(aphy->hw);
411 aphy->state = ATH_WIPHY_PAUSING;
412 /*
413 * TODO: handle PAUSING->PAUSED for the case where there are multiple
414 * active vifs (now we do it on the first vif getting ready; should be
415 * on the last)
416 */
417 ieee80211_iterate_active_interfaces_atomic(aphy->hw, ath9k_pause_iter,
418 aphy);
419 return 0;
420}
421
422int ath9k_wiphy_pause(struct ath_wiphy *aphy)
423{
424 int ret;
425 spin_lock_bh(&aphy->sc->wiphy_lock);
426 ret = __ath9k_wiphy_pause(aphy);
427 spin_unlock_bh(&aphy->sc->wiphy_lock);
428 return ret;
429}
430
431static void ath9k_unpause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
432{
433 struct ath_wiphy *aphy = data;
434 struct ath_vif *avp = (void *) vif->drv_priv;
435
436 switch (vif->type) {
437 case NL80211_IFTYPE_STATION:
438 if (!vif->bss_conf.assoc)
439 break;
440 ath9k_send_nullfunc(aphy, vif, avp->bssid, 0);
441 break;
442 case NL80211_IFTYPE_AP:
443 /* Beacon transmission is re-enabled by aphy->state change */
444 break;
445 default:
446 break;
447 }
448}
449
450/* caller must hold wiphy_lock */
451static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy)
452{
453 ieee80211_iterate_active_interfaces_atomic(aphy->hw,
454 ath9k_unpause_iter, aphy);
455 aphy->state = ATH_WIPHY_ACTIVE;
456 ieee80211_wake_queues(aphy->hw);
457 return 0;
458}
459
460int ath9k_wiphy_unpause(struct ath_wiphy *aphy)
461{
462 int ret;
463 spin_lock_bh(&aphy->sc->wiphy_lock);
464 ret = __ath9k_wiphy_unpause(aphy);
465 spin_unlock_bh(&aphy->sc->wiphy_lock);
466 return ret;
467}
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200468
Jouni Malinen7ec3e512009-03-03 19:23:37 +0200469static void __ath9k_wiphy_mark_all_paused(struct ath_softc *sc)
470{
471 int i;
472 if (sc->pri_wiphy->state != ATH_WIPHY_INACTIVE)
473 sc->pri_wiphy->state = ATH_WIPHY_PAUSED;
474 for (i = 0; i < sc->num_sec_wiphy; i++) {
475 if (sc->sec_wiphy[i] &&
476 sc->sec_wiphy[i]->state != ATH_WIPHY_INACTIVE)
477 sc->sec_wiphy[i]->state = ATH_WIPHY_PAUSED;
478 }
479}
480
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200481/* caller must hold wiphy_lock */
482static void __ath9k_wiphy_pause_all(struct ath_softc *sc)
483{
484 int i;
485 if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE)
486 __ath9k_wiphy_pause(sc->pri_wiphy);
487 for (i = 0; i < sc->num_sec_wiphy; i++) {
488 if (sc->sec_wiphy[i] &&
489 sc->sec_wiphy[i]->state == ATH_WIPHY_ACTIVE)
490 __ath9k_wiphy_pause(sc->sec_wiphy[i]);
491 }
492}
493
494int ath9k_wiphy_select(struct ath_wiphy *aphy)
495{
496 struct ath_softc *sc = aphy->sc;
497 bool now;
498
499 spin_lock_bh(&sc->wiphy_lock);
Jouni Malinen8089cc42009-03-03 19:23:38 +0200500 if (__ath9k_wiphy_scanning(sc)) {
501 /*
502 * For now, we are using mac80211 sw scan and it expects to
503 * have full control over channel changes, so avoid wiphy
504 * scheduling during a scan. This could be optimized if the
505 * scanning control were moved into the driver.
506 */
507 spin_unlock_bh(&sc->wiphy_lock);
508 return -EBUSY;
509 }
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200510 if (__ath9k_wiphy_pausing(sc)) {
Jouni Malinen7ec3e512009-03-03 19:23:37 +0200511 if (sc->wiphy_select_failures == 0)
512 sc->wiphy_select_first_fail = jiffies;
513 sc->wiphy_select_failures++;
514 if (time_after(jiffies, sc->wiphy_select_first_fail + HZ / 2))
515 {
516 printk(KERN_DEBUG "ath9k: Previous wiphy select timed "
517 "out; disable/enable hw to recover\n");
518 __ath9k_wiphy_mark_all_paused(sc);
519 /*
520 * TODO: this workaround to fix hardware is unlikely to
521 * be specific to virtual wiphy changes. It can happen
522 * on normal channel change, too, and as such, this
523 * should really be made more generic. For example,
524 * tricker radio disable/enable on GTT interrupt burst
525 * (say, 10 GTT interrupts received without any TX
526 * frame being completed)
527 */
528 spin_unlock_bh(&sc->wiphy_lock);
Luis R. Rodriguez68a89112009-11-02 14:35:42 -0800529 ath_radio_disable(sc, aphy->hw);
530 ath_radio_enable(sc, aphy->hw);
531 /* Only the primary wiphy hw is used for queuing work */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400532 ieee80211_queue_work(aphy->sc->hw,
Jouni Malinen7ec3e512009-03-03 19:23:37 +0200533 &aphy->sc->chan_work);
534 return -EBUSY; /* previous select still in progress */
535 }
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200536 spin_unlock_bh(&sc->wiphy_lock);
537 return -EBUSY; /* previous select still in progress */
538 }
Jouni Malinen7ec3e512009-03-03 19:23:37 +0200539 sc->wiphy_select_failures = 0;
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200540
541 /* Store the new channel */
542 sc->chan_idx = aphy->chan_idx;
543 sc->chan_is_ht = aphy->chan_is_ht;
544 sc->next_wiphy = aphy;
545
546 __ath9k_wiphy_pause_all(sc);
547 now = !__ath9k_wiphy_pausing(aphy->sc);
548 spin_unlock_bh(&sc->wiphy_lock);
549
550 if (now) {
551 /* Ready to request channel change immediately */
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400552 ieee80211_queue_work(aphy->sc->hw, &aphy->sc->chan_work);
Jouni Malinen0e2dedf2009-03-03 19:23:32 +0200553 }
554
555 /*
556 * wiphys will be unpaused in ath9k_tx_status() once channel has been
557 * changed if any wiphy needs time to become paused.
558 */
559
560 return 0;
561}
Jouni Malinen9580a222009-03-03 19:23:33 +0200562
563bool ath9k_wiphy_started(struct ath_softc *sc)
564{
565 int i;
566 spin_lock_bh(&sc->wiphy_lock);
567 if (sc->pri_wiphy->state != ATH_WIPHY_INACTIVE) {
568 spin_unlock_bh(&sc->wiphy_lock);
569 return true;
570 }
571 for (i = 0; i < sc->num_sec_wiphy; i++) {
572 if (sc->sec_wiphy[i] &&
573 sc->sec_wiphy[i]->state != ATH_WIPHY_INACTIVE) {
574 spin_unlock_bh(&sc->wiphy_lock);
575 return true;
576 }
577 }
578 spin_unlock_bh(&sc->wiphy_lock);
579 return false;
580}
Jouni Malinen18eb62f2009-03-03 19:23:35 +0200581
582static void ath9k_wiphy_pause_chan(struct ath_wiphy *aphy,
583 struct ath_wiphy *selected)
584{
Jouni Malinen8089cc42009-03-03 19:23:38 +0200585 if (selected->state == ATH_WIPHY_SCAN) {
586 if (aphy == selected)
587 return;
588 /*
589 * Pause all other wiphys for the duration of the scan even if
590 * they are on the current channel now.
591 */
592 } else if (aphy->chan_idx == selected->chan_idx)
Jouni Malinen18eb62f2009-03-03 19:23:35 +0200593 return;
594 aphy->state = ATH_WIPHY_PAUSED;
595 ieee80211_stop_queues(aphy->hw);
596}
597
598void ath9k_wiphy_pause_all_forced(struct ath_softc *sc,
599 struct ath_wiphy *selected)
600{
601 int i;
602 spin_lock_bh(&sc->wiphy_lock);
603 if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE)
604 ath9k_wiphy_pause_chan(sc->pri_wiphy, selected);
605 for (i = 0; i < sc->num_sec_wiphy; i++) {
606 if (sc->sec_wiphy[i] &&
607 sc->sec_wiphy[i]->state == ATH_WIPHY_ACTIVE)
608 ath9k_wiphy_pause_chan(sc->sec_wiphy[i], selected);
609 }
610 spin_unlock_bh(&sc->wiphy_lock);
611}
Jouni Malinenf98c3bd2009-03-03 19:23:39 +0200612
613void ath9k_wiphy_work(struct work_struct *work)
614{
615 struct ath_softc *sc = container_of(work, struct ath_softc,
616 wiphy_work.work);
617 struct ath_wiphy *aphy = NULL;
618 bool first = true;
619
620 spin_lock_bh(&sc->wiphy_lock);
621
622 if (sc->wiphy_scheduler_int == 0) {
623 /* wiphy scheduler is disabled */
624 spin_unlock_bh(&sc->wiphy_lock);
625 return;
626 }
627
628try_again:
629 sc->wiphy_scheduler_index++;
630 while (sc->wiphy_scheduler_index <= sc->num_sec_wiphy) {
631 aphy = sc->sec_wiphy[sc->wiphy_scheduler_index - 1];
632 if (aphy && aphy->state != ATH_WIPHY_INACTIVE)
633 break;
634
635 sc->wiphy_scheduler_index++;
636 aphy = NULL;
637 }
638 if (aphy == NULL) {
639 sc->wiphy_scheduler_index = 0;
640 if (sc->pri_wiphy->state == ATH_WIPHY_INACTIVE) {
641 if (first) {
642 first = false;
643 goto try_again;
644 }
645 /* No wiphy is ready to be scheduled */
646 } else
647 aphy = sc->pri_wiphy;
648 }
649
650 spin_unlock_bh(&sc->wiphy_lock);
651
652 if (aphy &&
653 aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN &&
654 ath9k_wiphy_select(aphy)) {
655 printk(KERN_DEBUG "ath9k: Failed to schedule virtual wiphy "
656 "change\n");
657 }
658
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400659 ieee80211_queue_delayed_work(sc->hw,
660 &sc->wiphy_work,
661 sc->wiphy_scheduler_int);
Jouni Malinenf98c3bd2009-03-03 19:23:39 +0200662}
663
664void ath9k_wiphy_set_scheduler(struct ath_softc *sc, unsigned int msec_int)
665{
666 cancel_delayed_work_sync(&sc->wiphy_work);
667 sc->wiphy_scheduler_int = msecs_to_jiffies(msec_int);
668 if (sc->wiphy_scheduler_int)
Luis R. Rodriguez42935ec2009-07-29 20:08:07 -0400669 ieee80211_queue_delayed_work(sc->hw, &sc->wiphy_work,
670 sc->wiphy_scheduler_int);
Jouni Malinenf98c3bd2009-03-03 19:23:39 +0200671}
Luis R. Rodriguez64839172009-07-14 20:22:53 -0400672
673/* caller must hold wiphy_lock */
674bool ath9k_all_wiphys_idle(struct ath_softc *sc)
675{
676 unsigned int i;
Luis R. Rodriguez194b7c12009-10-29 10:41:15 -0700677 if (!sc->pri_wiphy->idle)
Luis R. Rodriguez64839172009-07-14 20:22:53 -0400678 return false;
Luis R. Rodriguez64839172009-07-14 20:22:53 -0400679 for (i = 0; i < sc->num_sec_wiphy; i++) {
680 struct ath_wiphy *aphy = sc->sec_wiphy[i];
681 if (!aphy)
682 continue;
Luis R. Rodriguez194b7c12009-10-29 10:41:15 -0700683 if (!aphy->idle)
Luis R. Rodriguez64839172009-07-14 20:22:53 -0400684 return false;
685 }
686 return true;
687}
Luis R. Rodriguez194b7c12009-10-29 10:41:15 -0700688
689/* caller must hold wiphy_lock */
690void ath9k_set_wiphy_idle(struct ath_wiphy *aphy, bool idle)
691{
692 struct ath_softc *sc = aphy->sc;
693
694 aphy->idle = idle;
695 ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
696 "Marking %s as %s\n",
697 wiphy_name(aphy->hw->wiphy),
698 idle ? "idle" : "not-idle");
699}