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.
19 struct ath9k_vif_iter_data {
24 static void ath9k_vif_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
26 struct ath9k_vif_iter_data *iter_data = data;
29 nbuf = krealloc(iter_data->addr, (iter_data->count + 1) * ETH_ALEN,
34 memcpy(nbuf + iter_data->count * ETH_ALEN, mac, ETH_ALEN);
35 iter_data->addr = nbuf;
39 void ath9k_set_bssid_mask(struct ieee80211_hw *hw)
41 struct ath_wiphy *aphy = hw->priv;
42 struct ath_softc *sc = aphy->sc;
43 struct ath9k_vif_iter_data iter_data;
48 * Add primary MAC address even if it is not in active use since it
49 * will be configured to the hardware as the starting point and the
50 * BSSID mask will need to be changed if another address is active.
52 iter_data.addr = kmalloc(ETH_ALEN, GFP_ATOMIC);
54 memcpy(iter_data.addr, sc->sc_ah->macaddr, ETH_ALEN);
59 /* Get list of all active MAC addresses */
60 spin_lock_bh(&sc->wiphy_lock);
61 ieee80211_iterate_active_interfaces_atomic(sc->hw, ath9k_vif_iter,
63 for (i = 0; i < sc->num_sec_wiphy; i++) {
64 if (sc->sec_wiphy[i] == NULL)
66 ieee80211_iterate_active_interfaces_atomic(
67 sc->sec_wiphy[i]->hw, ath9k_vif_iter, &iter_data);
69 spin_unlock_bh(&sc->wiphy_lock);
71 /* Generate an address mask to cover all active addresses */
72 memset(mask, 0, ETH_ALEN);
73 for (i = 0; i < iter_data.count; i++) {
74 u8 *a1 = iter_data.addr + i * ETH_ALEN;
75 for (j = i + 1; j < iter_data.count; j++) {
76 u8 *a2 = iter_data.addr + j * ETH_ALEN;
77 mask[0] |= a1[0] ^ a2[0];
78 mask[1] |= a1[1] ^ a2[1];
79 mask[2] |= a1[2] ^ a2[2];
80 mask[3] |= a1[3] ^ a2[3];
81 mask[4] |= a1[4] ^ a2[4];
82 mask[5] |= a1[5] ^ a2[5];
86 kfree(iter_data.addr);
88 /* Invert the mask and configure hardware */
89 sc->bssidmask[0] = ~mask[0];
90 sc->bssidmask[1] = ~mask[1];
91 sc->bssidmask[2] = ~mask[2];
92 sc->bssidmask[3] = ~mask[3];
93 sc->bssidmask[4] = ~mask[4];
94 sc->bssidmask[5] = ~mask[5];
96 ath9k_hw_setbssidmask(sc);
99 int ath9k_wiphy_add(struct ath_softc *sc)
102 struct ath_wiphy *aphy;
103 struct ieee80211_hw *hw;
106 hw = ieee80211_alloc_hw(sizeof(struct ath_wiphy), &ath9k_ops);
110 spin_lock_bh(&sc->wiphy_lock);
111 for (i = 0; i < sc->num_sec_wiphy; i++) {
112 if (sc->sec_wiphy[i] == NULL)
116 if (i == sc->num_sec_wiphy) {
117 /* No empty slot available; increase array length */
118 struct ath_wiphy **n;
119 n = krealloc(sc->sec_wiphy,
120 (sc->num_sec_wiphy + 1) *
121 sizeof(struct ath_wiphy *),
124 spin_unlock_bh(&sc->wiphy_lock);
125 ieee80211_free_hw(hw);
133 SET_IEEE80211_DEV(hw, sc->dev);
138 sc->sec_wiphy[i] = aphy;
139 spin_unlock_bh(&sc->wiphy_lock);
141 memcpy(addr, sc->sc_ah->macaddr, ETH_ALEN);
142 addr[0] |= 0x02; /* Locally managed address */
144 * XOR virtual wiphy index into the least significant bits to generate
145 * a different MAC address for each virtual wiphy.
148 addr[4] ^= (i & 0xff00) >> 8;
149 addr[3] ^= (i & 0xff0000) >> 16;
151 SET_IEEE80211_PERM_ADDR(hw, addr);
153 ath_set_hw_capab(sc, hw);
155 error = ieee80211_register_hw(hw);
160 int ath9k_wiphy_del(struct ath_wiphy *aphy)
162 struct ath_softc *sc = aphy->sc;
165 spin_lock_bh(&sc->wiphy_lock);
166 for (i = 0; i < sc->num_sec_wiphy; i++) {
167 if (aphy == sc->sec_wiphy[i]) {
168 sc->sec_wiphy[i] = NULL;
169 spin_unlock_bh(&sc->wiphy_lock);
170 ieee80211_unregister_hw(aphy->hw);
171 ieee80211_free_hw(aphy->hw);
175 spin_unlock_bh(&sc->wiphy_lock);
179 static int ath9k_send_nullfunc(struct ath_wiphy *aphy,
180 struct ieee80211_vif *vif, const u8 *bssid,
183 struct ath_softc *sc = aphy->sc;
184 struct ath_tx_control txctl;
186 struct ieee80211_hdr *hdr;
188 struct ieee80211_tx_info *info;
190 skb = dev_alloc_skb(24);
193 hdr = (struct ieee80211_hdr *) skb_put(skb, 24);
195 fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
196 IEEE80211_FCTL_TODS);
198 fc |= cpu_to_le16(IEEE80211_FCTL_PM);
199 hdr->frame_control = fc;
200 memcpy(hdr->addr1, bssid, ETH_ALEN);
201 memcpy(hdr->addr2, aphy->hw->wiphy->perm_addr, ETH_ALEN);
202 memcpy(hdr->addr3, bssid, ETH_ALEN);
204 info = IEEE80211_SKB_CB(skb);
205 memset(info, 0, sizeof(*info));
206 info->flags = IEEE80211_TX_CTL_REQ_TX_STATUS;
207 info->control.vif = vif;
208 info->control.rates[0].idx = 0;
209 info->control.rates[0].count = 4;
210 info->control.rates[1].idx = -1;
212 memset(&txctl, 0, sizeof(struct ath_tx_control));
213 txctl.txq = &sc->tx.txq[sc->tx.hwq_map[ATH9K_WME_AC_VO]];
214 txctl.frame_type = ps ? ATH9K_INT_PAUSE : ATH9K_INT_UNPAUSE;
216 if (ath_tx_start(aphy->hw, skb, &txctl) != 0)
221 dev_kfree_skb_any(skb);
225 static bool __ath9k_wiphy_pausing(struct ath_softc *sc)
228 if (sc->pri_wiphy->state == ATH_WIPHY_PAUSING)
230 for (i = 0; i < sc->num_sec_wiphy; i++) {
231 if (sc->sec_wiphy[i] &&
232 sc->sec_wiphy[i]->state == ATH_WIPHY_PAUSING)
238 static bool ath9k_wiphy_pausing(struct ath_softc *sc)
241 spin_lock_bh(&sc->wiphy_lock);
242 ret = __ath9k_wiphy_pausing(sc);
243 spin_unlock_bh(&sc->wiphy_lock);
247 static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy);
249 /* caller must hold wiphy_lock */
250 static void __ath9k_wiphy_unpause_ch(struct ath_wiphy *aphy)
254 if (aphy->chan_idx != aphy->sc->chan_idx)
255 return; /* wiphy not on the selected channel */
256 __ath9k_wiphy_unpause(aphy);
259 static void ath9k_wiphy_unpause_channel(struct ath_softc *sc)
262 spin_lock_bh(&sc->wiphy_lock);
263 __ath9k_wiphy_unpause_ch(sc->pri_wiphy);
264 for (i = 0; i < sc->num_sec_wiphy; i++)
265 __ath9k_wiphy_unpause_ch(sc->sec_wiphy[i]);
266 spin_unlock_bh(&sc->wiphy_lock);
269 void ath9k_wiphy_chan_work(struct work_struct *work)
271 struct ath_softc *sc = container_of(work, struct ath_softc, chan_work);
272 struct ath_wiphy *aphy = sc->next_wiphy;
278 * All pending interfaces paused; ready to change
282 /* Change channels */
283 mutex_lock(&sc->mutex);
284 /* XXX: remove me eventually */
285 ath9k_update_ichannel(sc, aphy->hw,
286 &sc->sc_ah->channels[sc->chan_idx]);
287 ath_update_chainmask(sc, sc->chan_is_ht);
288 if (ath_set_channel(sc, aphy->hw,
289 &sc->sc_ah->channels[sc->chan_idx]) < 0) {
290 printk(KERN_DEBUG "ath9k: Failed to set channel for new "
292 mutex_unlock(&sc->mutex);
295 mutex_unlock(&sc->mutex);
297 ath9k_wiphy_unpause_channel(sc);
301 * ath9k version of ieee80211_tx_status() for TX frames that are generated
302 * internally in the driver.
304 void ath9k_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
306 struct ath_wiphy *aphy = hw->priv;
307 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
308 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
309 struct ath_tx_info_priv *tx_info_priv = ATH_TX_INFO_PRIV(tx_info);
311 if (tx_info_priv && tx_info_priv->frame_type == ATH9K_INT_PAUSE &&
312 aphy->state == ATH_WIPHY_PAUSING) {
313 if (!(info->flags & IEEE80211_TX_STAT_ACK)) {
314 printk(KERN_DEBUG "ath9k: %s: no ACK for pause "
315 "frame\n", wiphy_name(hw->wiphy));
317 * The AP did not reply; ignore this to allow us to
321 aphy->state = ATH_WIPHY_PAUSED;
322 if (!ath9k_wiphy_pausing(aphy->sc)) {
324 * Drop from tasklet to work to allow mutex for channel
327 queue_work(aphy->sc->hw->workqueue,
328 &aphy->sc->chan_work);
333 tx_info->rate_driver_data[0] = NULL;
338 static void ath9k_mark_paused(struct ath_wiphy *aphy)
340 struct ath_softc *sc = aphy->sc;
341 aphy->state = ATH_WIPHY_PAUSED;
342 if (!__ath9k_wiphy_pausing(sc))
343 queue_work(sc->hw->workqueue, &sc->chan_work);
346 static void ath9k_pause_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) {
354 ath9k_mark_paused(aphy);
357 /* TODO: could avoid this if already in PS mode */
358 if (ath9k_send_nullfunc(aphy, vif, avp->bssid, 1)) {
359 printk(KERN_DEBUG "%s: failed to send PS nullfunc\n",
361 ath9k_mark_paused(aphy);
364 case NL80211_IFTYPE_AP:
365 /* Beacon transmission is paused by aphy->state change */
366 ath9k_mark_paused(aphy);
373 /* caller must hold wiphy_lock */
374 static int __ath9k_wiphy_pause(struct ath_wiphy *aphy)
376 ieee80211_stop_queues(aphy->hw);
377 aphy->state = ATH_WIPHY_PAUSING;
379 * TODO: handle PAUSING->PAUSED for the case where there are multiple
380 * active vifs (now we do it on the first vif getting ready; should be
383 ieee80211_iterate_active_interfaces_atomic(aphy->hw, ath9k_pause_iter,
388 int ath9k_wiphy_pause(struct ath_wiphy *aphy)
391 spin_lock_bh(&aphy->sc->wiphy_lock);
392 ret = __ath9k_wiphy_pause(aphy);
393 spin_unlock_bh(&aphy->sc->wiphy_lock);
397 static void ath9k_unpause_iter(void *data, u8 *mac, struct ieee80211_vif *vif)
399 struct ath_wiphy *aphy = data;
400 struct ath_vif *avp = (void *) vif->drv_priv;
403 case NL80211_IFTYPE_STATION:
404 if (!vif->bss_conf.assoc)
406 ath9k_send_nullfunc(aphy, vif, avp->bssid, 0);
408 case NL80211_IFTYPE_AP:
409 /* Beacon transmission is re-enabled by aphy->state change */
416 /* caller must hold wiphy_lock */
417 static int __ath9k_wiphy_unpause(struct ath_wiphy *aphy)
419 ieee80211_iterate_active_interfaces_atomic(aphy->hw,
420 ath9k_unpause_iter, aphy);
421 aphy->state = ATH_WIPHY_ACTIVE;
422 ieee80211_wake_queues(aphy->hw);
426 int ath9k_wiphy_unpause(struct ath_wiphy *aphy)
429 spin_lock_bh(&aphy->sc->wiphy_lock);
430 ret = __ath9k_wiphy_unpause(aphy);
431 spin_unlock_bh(&aphy->sc->wiphy_lock);
435 /* caller must hold wiphy_lock */
436 static void __ath9k_wiphy_pause_all(struct ath_softc *sc)
439 if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE)
440 __ath9k_wiphy_pause(sc->pri_wiphy);
441 for (i = 0; i < sc->num_sec_wiphy; i++) {
442 if (sc->sec_wiphy[i] &&
443 sc->sec_wiphy[i]->state == ATH_WIPHY_ACTIVE)
444 __ath9k_wiphy_pause(sc->sec_wiphy[i]);
448 int ath9k_wiphy_select(struct ath_wiphy *aphy)
450 struct ath_softc *sc = aphy->sc;
453 spin_lock_bh(&sc->wiphy_lock);
454 if (__ath9k_wiphy_pausing(sc)) {
455 spin_unlock_bh(&sc->wiphy_lock);
456 return -EBUSY; /* previous select still in progress */
459 /* Store the new channel */
460 sc->chan_idx = aphy->chan_idx;
461 sc->chan_is_ht = aphy->chan_is_ht;
462 sc->next_wiphy = aphy;
464 __ath9k_wiphy_pause_all(sc);
465 now = !__ath9k_wiphy_pausing(aphy->sc);
466 spin_unlock_bh(&sc->wiphy_lock);
469 /* Ready to request channel change immediately */
470 queue_work(aphy->sc->hw->workqueue, &aphy->sc->chan_work);
474 * wiphys will be unpaused in ath9k_tx_status() once channel has been
475 * changed if any wiphy needs time to become paused.
481 bool ath9k_wiphy_started(struct ath_softc *sc)
484 spin_lock_bh(&sc->wiphy_lock);
485 if (sc->pri_wiphy->state != ATH_WIPHY_INACTIVE) {
486 spin_unlock_bh(&sc->wiphy_lock);
489 for (i = 0; i < sc->num_sec_wiphy; i++) {
490 if (sc->sec_wiphy[i] &&
491 sc->sec_wiphy[i]->state != ATH_WIPHY_INACTIVE) {
492 spin_unlock_bh(&sc->wiphy_lock);
496 spin_unlock_bh(&sc->wiphy_lock);
500 static void ath9k_wiphy_pause_chan(struct ath_wiphy *aphy,
501 struct ath_wiphy *selected)
503 if (aphy->chan_idx == selected->chan_idx)
505 aphy->state = ATH_WIPHY_PAUSED;
506 ieee80211_stop_queues(aphy->hw);
509 void ath9k_wiphy_pause_all_forced(struct ath_softc *sc,
510 struct ath_wiphy *selected)
513 spin_lock_bh(&sc->wiphy_lock);
514 if (sc->pri_wiphy->state == ATH_WIPHY_ACTIVE)
515 ath9k_wiphy_pause_chan(sc->pri_wiphy, selected);
516 for (i = 0; i < sc->num_sec_wiphy; i++) {
517 if (sc->sec_wiphy[i] &&
518 sc->sec_wiphy[i]->state == ATH_WIPHY_ACTIVE)
519 ath9k_wiphy_pause_chan(sc->sec_wiphy[i], selected);
521 spin_unlock_bh(&sc->wiphy_lock);