virtual.c 19 KB

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