virtual.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /*
  194. * ath9k version of ieee80211_tx_status() for TX frames that are generated
  195. * internally in the driver.
  196. */
  197. void ath9k_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
  198. {
  199. struct ath_wiphy *aphy = hw->priv;
  200. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  201. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  202. struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
  203. if (tx_info_priv && tx_info_priv->frame_type == ATH9K_INT_PAUSE &&
  204. aphy->state == ATH_WIPHY_PAUSING) {
  205. if (!(info->flags & IEEE80211_TX_STAT_ACK)) {
  206. printk(KERN_DEBUG "ath9k: %s: no ACK for pause "
  207. "frame\n", wiphy_name(hw->wiphy));
  208. /*
  209. * The AP did not reply; ignore this to allow us to
  210. * continue.
  211. */
  212. }
  213. aphy->state = ATH_WIPHY_PAUSED;
  214. }
  215. kfree(tx_info_priv);
  216. tx_info->rate_driver_data[0] = NULL;
  217. dev_kfree_skb(skb);
  218. }
  219. static void ath9k_pause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  220. {
  221. struct ath_wiphy *aphy = data;
  222. struct ath_vif *avp = (void *) vif->drv_priv;
  223. switch (vif->type) {
  224. case NL80211_IFTYPE_STATION:
  225. if (!vif->bss_conf.assoc) {
  226. aphy->state = ATH_WIPHY_PAUSED;
  227. break;
  228. }
  229. /* TODO: could avoid this if already in PS mode */
  230. ath9k_send_nullfunc(aphy, vif, avp->bssid, 1);
  231. break;
  232. case NL80211_IFTYPE_AP:
  233. /* Beacon transmission is paused by aphy->state change */
  234. aphy->state = ATH_WIPHY_PAUSED;
  235. break;
  236. default:
  237. break;
  238. }
  239. }
  240. /* caller must hold wiphy_lock */
  241. static int __ath9k_wiphy_pause(struct ath_wiphy *aphy)
  242. {
  243. ieee80211_stop_queues(aphy->hw);
  244. aphy->state = ATH_WIPHY_PAUSING;
  245. /*
  246. * TODO: handle PAUSING->PAUSED for the case where there are multiple
  247. * active vifs (now we do it on the first vif getting ready; should be
  248. * on the last)
  249. */
  250. ieee80211_iterate_active_interfaces_atomic(aphy->hw, ath9k_pause_iter,
  251. aphy);
  252. return 0;
  253. }
  254. int ath9k_wiphy_pause(struct ath_wiphy *aphy)
  255. {
  256. int ret;
  257. spin_lock_bh(&aphy->sc->wiphy_lock);
  258. ret = __ath9k_wiphy_pause(aphy);
  259. spin_unlock_bh(&aphy->sc->wiphy_lock);
  260. return ret;
  261. }
  262. static void ath9k_unpause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
  263. {
  264. struct ath_wiphy *aphy = data;
  265. struct ath_vif *avp = (void *) vif->drv_priv;
  266. switch (vif->type) {
  267. case NL80211_IFTYPE_STATION:
  268. if (!vif->bss_conf.assoc)
  269. break;
  270. ath9k_send_nullfunc(aphy, vif, avp->bssid, 0);
  271. break;
  272. case NL80211_IFTYPE_AP:
  273. /* Beacon transmission is re-enabled by aphy->state change */
  274. break;
  275. default:
  276. break;
  277. }
  278. }
  279. /* caller must hold wiphy_lock */
  280. static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy)
  281. {
  282. ieee80211_iterate_active_interfaces_atomic(aphy->hw,
  283. ath9k_unpause_iter, aphy);
  284. aphy->state = ATH_WIPHY_ACTIVE;
  285. ieee80211_wake_queues(aphy->hw);
  286. return 0;
  287. }
  288. int ath9k_wiphy_unpause(struct ath_wiphy *aphy)
  289. {
  290. int ret;
  291. spin_lock_bh(&aphy->sc->wiphy_lock);
  292. ret = __ath9k_wiphy_unpause(aphy);
  293. spin_unlock_bh(&aphy->sc->wiphy_lock);
  294. return ret;
  295. }