virtual.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 "ath9k.h"
  17. struct ath9k_vif_iter_data {
  18. int count;
  19. u8 *addr;
  20. };
  21. static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  22. {
  23. struct ath9k_vif_iter_data *iter_data = data;
  24. u8 *nbuf;
  25. nbuf = krealloc(iter_data->addr, (iter_data->count + 1) * ETH_ALEN,
  26. GFP_ATOMIC);
  27. if (nbuf == NULL)
  28. return;
  29. memcpy(nbuf + iter_data->count * ETH_ALEN, mac, ETH_ALEN);
  30. iter_data->addr = nbuf;
  31. iter_data->count++;
  32. }
  33. void ath9k_set_bssid_mask(struct ieee80211_hw *hw)
  34. {
  35. struct ath_wiphy *aphy = hw->priv;
  36. struct ath_softc *sc = aphy->sc;
  37. struct ath9k_vif_iter_data iter_data;
  38. int i, j;
  39. u8 mask[ETH_ALEN];
  40. /*
  41. * Add primary MAC address even if it is not in active use since it
  42. * will be configured to the hardware as the starting point and the
  43. * BSSID mask will need to be changed if another address is active.
  44. */
  45. iter_data.addr = kmalloc(ETH_ALEN, GFP_ATOMIC);
  46. if (iter_data.addr) {
  47. memcpy(iter_data.addr, sc->sc_ah->macaddr, ETH_ALEN);
  48. iter_data.count = 1;
  49. } else
  50. iter_data.count = 0;
  51. /* Get list of all active MAC addresses */
  52. spin_lock_bh(&sc->wiphy_lock);
  53. ieee80211_iterate_active_interfaces_atomic(sc->hw, ath9k_vif_iter,
  54. &iter_data);
  55. for (i = 0; i < sc->num_sec_wiphy; i++) {
  56. if (sc->sec_wiphy[i] == NULL)
  57. continue;
  58. ieee80211_iterate_active_interfaces_atomic(
  59. sc->sec_wiphy[i]->hw, ath9k_vif_iter, &iter_data);
  60. }
  61. spin_unlock_bh(&sc->wiphy_lock);
  62. /* Generate an address mask to cover all active addresses */
  63. memset(mask, 0, ETH_ALEN);
  64. for (i = 0; i < iter_data.count; i++) {
  65. u8 *a1 = iter_data.addr + i * ETH_ALEN;
  66. for (j = i + 1; j < iter_data.count; j++) {
  67. u8 *a2 = iter_data.addr + j * ETH_ALEN;
  68. mask[0] |= a1[0] ^ a2[0];
  69. mask[1] |= a1[1] ^ a2[1];
  70. mask[2] |= a1[2] ^ a2[2];
  71. mask[3] |= a1[3] ^ a2[3];
  72. mask[4] |= a1[4] ^ a2[4];
  73. mask[5] |= a1[5] ^ a2[5];
  74. }
  75. }
  76. kfree(iter_data.addr);
  77. /* Invert the mask and configure hardware */
  78. sc->bssidmask[0] = ~mask[0];
  79. sc->bssidmask[1] = ~mask[1];
  80. sc->bssidmask[2] = ~mask[2];
  81. sc->bssidmask[3] = ~mask[3];
  82. sc->bssidmask[4] = ~mask[4];
  83. sc->bssidmask[5] = ~mask[5];
  84. ath9k_hw_setbssidmask(sc);
  85. }
  86. int ath9k_wiphy_add(struct ath_softc *sc)
  87. {
  88. int i, error;
  89. struct ath_wiphy *aphy;
  90. struct ieee80211_hw *hw;
  91. u8 addr[ETH_ALEN];
  92. hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy), &ath9k_ops);
  93. if (hw == NULL)
  94. return -ENOMEM;
  95. spin_lock_bh(&sc->wiphy_lock);
  96. for (i = 0; i < sc->num_sec_wiphy; i++) {
  97. if (sc->sec_wiphy[i] == NULL)
  98. break;
  99. }
  100. if (i == sc->num_sec_wiphy) {
  101. /* No empty slot available; increase array length */
  102. struct ath_wiphy **n;
  103. n = krealloc(sc->sec_wiphy,
  104. (sc->num_sec_wiphy + 1) *
  105. sizeof(struct ath_wiphy *),
  106. GFP_ATOMIC);
  107. if (n == NULL) {
  108. spin_unlock_bh(&sc->wiphy_lock);
  109. ieee80211_free_hw(hw);
  110. return -ENOMEM;
  111. }
  112. n[i] = NULL;
  113. sc->sec_wiphy = n;
  114. sc->num_sec_wiphy++;
  115. }
  116. SET_IEEE80211_DEV(hw, sc->dev);
  117. aphy = hw->priv;
  118. aphy->sc = sc;
  119. aphy->hw = hw;
  120. sc->sec_wiphy[i] = aphy;
  121. spin_unlock_bh(&sc->wiphy_lock);
  122. memcpy(addr, sc->sc_ah->macaddr, ETH_ALEN);
  123. addr[0] |= 0x02; /* Locally managed address */
  124. /*
  125. * XOR virtual wiphy index into the least significant bits to generate
  126. * a different MAC address for each virtual wiphy.
  127. */
  128. addr[5] ^= i & 0xff;
  129. addr[4] ^= (i & 0xff00) >> 8;
  130. addr[3] ^= (i & 0xff0000) >> 16;
  131. SET_IEEE80211_PERM_ADDR(hw, addr);
  132. ath_set_hw_capab(sc, hw);
  133. error = ieee80211_register_hw(hw);
  134. return error;
  135. }
  136. int ath9k_wiphy_del(struct ath_wiphy *aphy)
  137. {
  138. struct ath_softc *sc = aphy->sc;
  139. int i;
  140. spin_lock_bh(&sc->wiphy_lock);
  141. for (i = 0; i < sc->num_sec_wiphy; i++) {
  142. if (aphy == sc->sec_wiphy[i]) {
  143. sc->sec_wiphy[i] = NULL;
  144. spin_unlock_bh(&sc->wiphy_lock);
  145. ieee80211_unregister_hw(aphy->hw);
  146. ieee80211_free_hw(aphy->hw);
  147. return 0;
  148. }
  149. }
  150. spin_unlock_bh(&sc->wiphy_lock);
  151. return -ENOENT;
  152. }
  153. static int ath9k_send_nullfunc(struct ath_wiphy *aphy,
  154. struct ieee80211_vif *vif, const u8 *bssid,
  155. int ps)
  156. {
  157. struct ath_softc *sc = aphy->sc;
  158. struct ath_tx_control txctl;
  159. struct sk_buff *skb;
  160. struct ieee80211_hdr *hdr;
  161. __le16 fc;
  162. struct ieee80211_tx_info *info;
  163. skb = dev_alloc_skb(24);
  164. if (skb == NULL)
  165. return -ENOMEM;
  166. hdr = (struct ieee80211_hdr *) skb_put(skb, 24);
  167. memset(hdr, 0, 24);
  168. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  169. IEEE80211_FCTL_TODS);
  170. if (ps)
  171. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  172. hdr->frame_control = fc;
  173. memcpy(hdr->addr1, bssid, ETH_ALEN);
  174. memcpy(hdr->addr2, aphy->hw->wiphy->perm_addr, ETH_ALEN);
  175. memcpy(hdr->addr3, bssid, ETH_ALEN);
  176. info = IEEE80211_SKB_CB(skb);
  177. memset(info, 0, sizeof(*info));
  178. info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS;
  179. info->control.vif = vif;
  180. info->control.rates[0].idx = 0;
  181. info->control.rates[0].count = 4;
  182. info->control.rates[1].idx = -1;
  183. memset(&txctl, 0, sizeof(struct ath_tx_control));
  184. txctl.txq = &sc->tx.txq[sc->tx.hwq_map[ATH9K_WME_AC_VO]];
  185. txctl.frame_type = ps ? ATH9K_INT_PAUSE : ATH9K_INT_UNPAUSE;
  186. if (ath_tx_start(aphy->hw, skb, &txctl) != 0)
  187. goto exit;
  188. return 0;
  189. exit:
  190. dev_kfree_skb_any(skb);
  191. return -1;
  192. }
  193. static bool __ath9k_wiphy_pausing(struct ath_softc *sc)
  194. {
  195. int i;
  196. if (sc->pri_wiphy->state == ATH_WIPHY_PAUSING)
  197. return true;
  198. for (i = 0; i < sc->num_sec_wiphy; i++) {
  199. if (sc->sec_wiphy[i] &&
  200. sc->sec_wiphy[i]->state == ATH_WIPHY_PAUSING)
  201. return true;
  202. }
  203. return false;
  204. }
  205. static bool ath9k_wiphy_pausing(struct ath_softc *sc)
  206. {
  207. bool ret;
  208. spin_lock_bh(&sc->wiphy_lock);
  209. ret = __ath9k_wiphy_pausing(sc);
  210. spin_unlock_bh(&sc->wiphy_lock);
  211. return ret;
  212. }
  213. static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy);
  214. /* caller must hold wiphy_lock */
  215. static void __ath9k_wiphy_unpause_ch(struct ath_wiphy *aphy)
  216. {
  217. if (aphy == NULL)
  218. return;
  219. if (aphy->chan_idx != aphy->sc->chan_idx)
  220. return; /* wiphy not on the selected channel */
  221. __ath9k_wiphy_unpause(aphy);
  222. }
  223. static void ath9k_wiphy_unpause_channel(struct ath_softc *sc)
  224. {
  225. int i;
  226. spin_lock_bh(&sc->wiphy_lock);
  227. __ath9k_wiphy_unpause_ch(sc->pri_wiphy);
  228. for (i = 0; i < sc->num_sec_wiphy; i++)
  229. __ath9k_wiphy_unpause_ch(sc->sec_wiphy[i]);
  230. spin_unlock_bh(&sc->wiphy_lock);
  231. }
  232. void ath9k_wiphy_chan_work(struct work_struct *work)
  233. {
  234. struct ath_softc *sc = container_of(work, struct ath_softc, chan_work);
  235. struct ath_wiphy *aphy = sc->next_wiphy;
  236. if (aphy == NULL)
  237. return;
  238. /*
  239. * All pending interfaces paused; ready to change
  240. * channels.
  241. */
  242. /* Change channels */
  243. mutex_lock(&sc->mutex);
  244. /* XXX: remove me eventually */
  245. ath9k_update_ichannel(sc, aphy->hw,
  246. &sc->sc_ah->channels[sc->chan_idx]);
  247. ath_update_chainmask(sc, sc->chan_is_ht);
  248. if (ath_set_channel(sc, aphy->hw,
  249. &sc->sc_ah->channels[sc->chan_idx]) < 0) {
  250. printk(KERN_DEBUG "ath9k: Failed to set channel for new "
  251. "virtual wiphy\n");
  252. mutex_unlock(&sc->mutex);
  253. return;
  254. }
  255. mutex_unlock(&sc->mutex);
  256. ath9k_wiphy_unpause_channel(sc);
  257. }
  258. /*
  259. * ath9k version of ieee80211_tx_status() for TX frames that are generated
  260. * internally in the driver.
  261. */
  262. void ath9k_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  263. {
  264. struct ath_wiphy *aphy = hw->priv;
  265. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  266. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  267. struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
  268. if (tx_info_priv && tx_info_priv->frame_type == ATH9K_INT_PAUSE &&
  269. aphy->state == ATH_WIPHY_PAUSING) {
  270. if (!(info->flags & IEEE80211_TX_STAT_ACK)) {
  271. printk(KERN_DEBUG "ath9k: %s: no ACK for pause "
  272. "frame\n", wiphy_name(hw->wiphy));
  273. /*
  274. * The AP did not reply; ignore this to allow us to
  275. * continue.
  276. */
  277. }
  278. aphy->state = ATH_WIPHY_PAUSED;
  279. if (!ath9k_wiphy_pausing(aphy->sc)) {
  280. /*
  281. * Drop from tasklet to work to allow mutex for channel
  282. * change.
  283. */
  284. queue_work(aphy->sc->hw->workqueue,
  285. &aphy->sc->chan_work);
  286. }
  287. }
  288. kfree(tx_info_priv);
  289. tx_info->rate_driver_data[0] = NULL;
  290. dev_kfree_skb(skb);
  291. }
  292. static void ath9k_mark_paused(struct ath_wiphy *aphy)
  293. {
  294. struct ath_softc *sc = aphy->sc;
  295. aphy->state = ATH_WIPHY_PAUSED;
  296. if (!__ath9k_wiphy_pausing(sc))
  297. queue_work(sc->hw->workqueue, &sc->chan_work);
  298. }
  299. static void ath9k_pause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  300. {
  301. struct ath_wiphy *aphy = data;
  302. struct ath_vif *avp = (void *) vif->drv_priv;
  303. switch (vif->type) {
  304. case NL80211_IFTYPE_STATION:
  305. if (!vif->bss_conf.assoc) {
  306. ath9k_mark_paused(aphy);
  307. break;
  308. }
  309. /* TODO: could avoid this if already in PS mode */
  310. if (ath9k_send_nullfunc(aphy, vif, avp->bssid, 1)) {
  311. printk(KERN_DEBUG "%s: failed to send PS nullfunc\n",
  312. __func__);
  313. ath9k_mark_paused(aphy);
  314. }
  315. break;
  316. case NL80211_IFTYPE_AP:
  317. /* Beacon transmission is paused by aphy->state change */
  318. ath9k_mark_paused(aphy);
  319. break;
  320. default:
  321. break;
  322. }
  323. }
  324. /* caller must hold wiphy_lock */
  325. static int __ath9k_wiphy_pause(struct ath_wiphy *aphy)
  326. {
  327. ieee80211_stop_queues(aphy->hw);
  328. aphy->state = ATH_WIPHY_PAUSING;
  329. /*
  330. * TODO: handle PAUSING->PAUSED for the case where there are multiple
  331. * active vifs (now we do it on the first vif getting ready; should be
  332. * on the last)
  333. */
  334. ieee80211_iterate_active_interfaces_atomic(aphy->hw, ath9k_pause_iter,
  335. aphy);
  336. return 0;
  337. }
  338. int ath9k_wiphy_pause(struct ath_wiphy *aphy)
  339. {
  340. int ret;
  341. spin_lock_bh(&aphy->sc->wiphy_lock);
  342. ret = __ath9k_wiphy_pause(aphy);
  343. spin_unlock_bh(&aphy->sc->wiphy_lock);
  344. return ret;
  345. }
  346. static void ath9k_unpause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  347. {
  348. struct ath_wiphy *aphy = data;
  349. struct ath_vif *avp = (void *) vif->drv_priv;
  350. switch (vif->type) {
  351. case NL80211_IFTYPE_STATION:
  352. if (!vif->bss_conf.assoc)
  353. break;
  354. ath9k_send_nullfunc(aphy, vif, avp->bssid, 0);
  355. break;
  356. case NL80211_IFTYPE_AP:
  357. /* Beacon transmission is re-enabled by aphy->state change */
  358. break;
  359. default:
  360. break;
  361. }
  362. }
  363. /* caller must hold wiphy_lock */
  364. static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy)
  365. {
  366. ieee80211_iterate_active_interfaces_atomic(aphy->hw,
  367. ath9k_unpause_iter, aphy);
  368. aphy->state = ATH_WIPHY_ACTIVE;
  369. ieee80211_wake_queues(aphy->hw);
  370. return 0;
  371. }
  372. int ath9k_wiphy_unpause(struct ath_wiphy *aphy)
  373. {
  374. int ret;
  375. spin_lock_bh(&aphy->sc->wiphy_lock);
  376. ret = __ath9k_wiphy_unpause(aphy);
  377. spin_unlock_bh(&aphy->sc->wiphy_lock);
  378. return ret;
  379. }
  380. /* caller must hold wiphy_lock */
  381. static void __ath9k_wiphy_pause_all(struct ath_softc *sc)
  382. {
  383. int i;
  384. if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE)
  385. __ath9k_wiphy_pause(sc->pri_wiphy);
  386. for (i = 0; i < sc->num_sec_wiphy; i++) {
  387. if (sc->sec_wiphy[i] &&
  388. sc->sec_wiphy[i]->state == ATH_WIPHY_ACTIVE)
  389. __ath9k_wiphy_pause(sc->sec_wiphy[i]);
  390. }
  391. }
  392. int ath9k_wiphy_select(struct ath_wiphy *aphy)
  393. {
  394. struct ath_softc *sc = aphy->sc;
  395. bool now;
  396. spin_lock_bh(&sc->wiphy_lock);
  397. if (__ath9k_wiphy_pausing(sc)) {
  398. spin_unlock_bh(&sc->wiphy_lock);
  399. return -EBUSY; /* previous select still in progress */
  400. }
  401. /* Store the new channel */
  402. sc->chan_idx = aphy->chan_idx;
  403. sc->chan_is_ht = aphy->chan_is_ht;
  404. sc->next_wiphy = aphy;
  405. __ath9k_wiphy_pause_all(sc);
  406. now = !__ath9k_wiphy_pausing(aphy->sc);
  407. spin_unlock_bh(&sc->wiphy_lock);
  408. if (now) {
  409. /* Ready to request channel change immediately */
  410. queue_work(aphy->sc->hw->workqueue, &aphy->sc->chan_work);
  411. }
  412. /*
  413. * wiphys will be unpaused in ath9k_tx_status() once channel has been
  414. * changed if any wiphy needs time to become paused.
  415. */
  416. return 0;
  417. }
  418. bool ath9k_wiphy_started(struct ath_softc *sc)
  419. {
  420. int i;
  421. spin_lock_bh(&sc->wiphy_lock);
  422. if (sc->pri_wiphy->state != ATH_WIPHY_INACTIVE) {
  423. spin_unlock_bh(&sc->wiphy_lock);
  424. return true;
  425. }
  426. for (i = 0; i < sc->num_sec_wiphy; i++) {
  427. if (sc->sec_wiphy[i] &&
  428. sc->sec_wiphy[i]->state != ATH_WIPHY_INACTIVE) {
  429. spin_unlock_bh(&sc->wiphy_lock);
  430. return true;
  431. }
  432. }
  433. spin_unlock_bh(&sc->wiphy_lock);
  434. return false;
  435. }
  436. static void ath9k_wiphy_pause_chan(struct ath_wiphy *aphy,
  437. struct ath_wiphy *selected)
  438. {
  439. if (aphy->chan_idx == selected->chan_idx)
  440. return;
  441. aphy->state = ATH_WIPHY_PAUSED;
  442. ieee80211_stop_queues(aphy->hw);
  443. }
  444. void ath9k_wiphy_pause_all_forced(struct ath_softc *sc,
  445. struct ath_wiphy *selected)
  446. {
  447. int i;
  448. spin_lock_bh(&sc->wiphy_lock);
  449. if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE)
  450. ath9k_wiphy_pause_chan(sc->pri_wiphy, selected);
  451. for (i = 0; i < sc->num_sec_wiphy; i++) {
  452. if (sc->sec_wiphy[i] &&
  453. sc->sec_wiphy[i]->state == ATH_WIPHY_ACTIVE)
  454. ath9k_wiphy_pause_chan(sc->sec_wiphy[i], selected);
  455. }
  456. spin_unlock_bh(&sc->wiphy_lock);
  457. }