Commit 520eb820 authored by Kalle Valo's avatar Kalle Valo Committed by John W. Linville

mac80211: implement dynamic power save

This patch implements dynamic power save for mac80211. Basically it
means enabling power save mode after an idle period. Implementing it
dynamically gives a good compromise of low power consumption and low
latency. Some hardware have support for this in firmware, but some
require the host to do it.

The dynamic power save is implemented by adding an timeout to
ieee80211_subif_start_xmit(). The timeout can be enabled from userspace
with Wireless Extensions. For example, the command below enables the
dynamic power save and sets the time timeout to 500 ms:

iwconfig wlan0 power timeout 500m

Power save now only works with devices which handle power save in firmware.
It's also disabled by default and the heuristics when and how to enable is
considered as a policy decision and will be left for the userspace to handle.
In case the firmware has support for this, drivers can disable this feature
with IEEE80211_HW_NO_STACK_DYNAMIC_PS.

Big thanks to Johannes Berg for the help with the design and code.
Signed-off-by: default avatarKalle Valo <kalle.valo@nokia.com>
Acked-by: default avatarJohannes Berg <johannes@sipsolutions.net>
Signed-off-by: default avatarJohn W. Linville <linville@tuxdriver.com>
parent ce7c9111
...@@ -854,6 +854,11 @@ enum ieee80211_tkip_key_type { ...@@ -854,6 +854,11 @@ enum ieee80211_tkip_key_type {
* *
* @IEEE80211_HW_AMPDU_AGGREGATION: * @IEEE80211_HW_AMPDU_AGGREGATION:
* Hardware supports 11n A-MPDU aggregation. * Hardware supports 11n A-MPDU aggregation.
*
* @IEEE80211_HW_NO_STACK_DYNAMIC_PS:
* Hardware which has dynamic power save support, meaning
* that power save is enabled in idle periods, and don't need support
* from stack.
*/ */
enum ieee80211_hw_flags { enum ieee80211_hw_flags {
IEEE80211_HW_RX_INCLUDES_FCS = 1<<1, IEEE80211_HW_RX_INCLUDES_FCS = 1<<1,
...@@ -866,6 +871,7 @@ enum ieee80211_hw_flags { ...@@ -866,6 +871,7 @@ enum ieee80211_hw_flags {
IEEE80211_HW_NOISE_DBM = 1<<8, IEEE80211_HW_NOISE_DBM = 1<<8,
IEEE80211_HW_SPECTRUM_MGMT = 1<<9, IEEE80211_HW_SPECTRUM_MGMT = 1<<9,
IEEE80211_HW_AMPDU_AGGREGATION = 1<<10, IEEE80211_HW_AMPDU_AGGREGATION = 1<<10,
IEEE80211_HW_NO_STACK_DYNAMIC_PS = 1<<11,
}; };
/** /**
......
...@@ -540,6 +540,7 @@ enum { ...@@ -540,6 +540,7 @@ enum {
enum queue_stop_reason { enum queue_stop_reason {
IEEE80211_QUEUE_STOP_REASON_DRIVER, IEEE80211_QUEUE_STOP_REASON_DRIVER,
IEEE80211_QUEUE_STOP_REASON_PS,
}; };
/* maximum number of hardware queues we support. */ /* maximum number of hardware queues we support. */
...@@ -693,7 +694,12 @@ struct ieee80211_local { ...@@ -693,7 +694,12 @@ struct ieee80211_local {
*/ */
int wifi_wme_noack_test; int wifi_wme_noack_test;
unsigned int wmm_acm; /* bit field of ACM bits (BIT(802.1D tag)) */ unsigned int wmm_acm; /* bit field of ACM bits (BIT(802.1D tag)) */
bool powersave; bool powersave;
int dynamic_ps_timeout;
struct work_struct dynamic_ps_enable_work;
struct work_struct dynamic_ps_disable_work;
struct timer_list dynamic_ps_timer;
#ifdef CONFIG_MAC80211_DEBUGFS #ifdef CONFIG_MAC80211_DEBUGFS
struct local_debugfsdentries { struct local_debugfsdentries {
...@@ -977,6 +983,10 @@ int ieee80211_set_freq(struct ieee80211_sub_if_data *sdata, int freq); ...@@ -977,6 +983,10 @@ int ieee80211_set_freq(struct ieee80211_sub_if_data *sdata, int freq);
u64 ieee80211_mandatory_rates(struct ieee80211_local *local, u64 ieee80211_mandatory_rates(struct ieee80211_local *local,
enum ieee80211_band band); enum ieee80211_band band);
void ieee80211_dynamic_ps_enable_work(struct work_struct *work);
void ieee80211_dynamic_ps_disable_work(struct work_struct *work);
void ieee80211_dynamic_ps_timer(unsigned long data);
void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw, void ieee80211_wake_queues_by_reason(struct ieee80211_hw *hw,
enum queue_stop_reason reason); enum queue_stop_reason reason);
void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw, void ieee80211_stop_queues_by_reason(struct ieee80211_hw *hw,
......
...@@ -729,6 +729,13 @@ struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len, ...@@ -729,6 +729,13 @@ struct ieee80211_hw *ieee80211_alloc_hw(size_t priv_data_len,
INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work); INIT_DELAYED_WORK(&local->scan_work, ieee80211_scan_work);
INIT_WORK(&local->dynamic_ps_enable_work,
ieee80211_dynamic_ps_enable_work);
INIT_WORK(&local->dynamic_ps_disable_work,
ieee80211_dynamic_ps_disable_work);
setup_timer(&local->dynamic_ps_timer,
ieee80211_dynamic_ps_timer, (unsigned long) local);
sta_info_init(local); sta_info_init(local);
tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending, tasklet_init(&local->tx_pending_tasklet, ieee80211_tx_pending,
......
...@@ -745,8 +745,14 @@ static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata, ...@@ -745,8 +745,14 @@ static void ieee80211_set_associated(struct ieee80211_sub_if_data *sdata,
ieee80211_bss_info_change_notify(sdata, bss_info_changed); ieee80211_bss_info_change_notify(sdata, bss_info_changed);
if (local->powersave) { if (local->powersave) {
local->hw.conf.flags |= IEEE80211_CONF_PS; if (local->dynamic_ps_timeout > 0)
ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); mod_timer(&local->dynamic_ps_timer, jiffies +
msecs_to_jiffies(local->dynamic_ps_timeout));
else {
conf->flags |= IEEE80211_CONF_PS;
ieee80211_hw_config(local,
IEEE80211_CONF_CHANGE_PS);
}
} }
netif_tx_start_all_queues(sdata->dev); netif_tx_start_all_queues(sdata->dev);
...@@ -866,6 +872,9 @@ static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata, ...@@ -866,6 +872,9 @@ static void ieee80211_set_disassoc(struct ieee80211_sub_if_data *sdata,
local->oper_channel_type = NL80211_CHAN_NO_HT; local->oper_channel_type = NL80211_CHAN_NO_HT;
config_changed |= IEEE80211_CONF_CHANGE_HT; config_changed |= IEEE80211_CONF_CHANGE_HT;
del_timer_sync(&local->dynamic_ps_timer);
cancel_work_sync(&local->dynamic_ps_enable_work);
if (local->hw.conf.flags & IEEE80211_CONF_PS) { if (local->hw.conf.flags & IEEE80211_CONF_PS) {
local->hw.conf.flags &= ~IEEE80211_CONF_PS; local->hw.conf.flags &= ~IEEE80211_CONF_PS;
config_changed |= IEEE80211_CONF_CHANGE_PS; config_changed |= IEEE80211_CONF_CHANGE_PS;
...@@ -2593,3 +2602,39 @@ void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local) ...@@ -2593,3 +2602,39 @@ void ieee80211_mlme_notify_scan_completed(struct ieee80211_local *local)
ieee80211_restart_sta_timer(sdata); ieee80211_restart_sta_timer(sdata);
rcu_read_unlock(); rcu_read_unlock();
} }
void ieee80211_dynamic_ps_disable_work(struct work_struct *work)
{
struct ieee80211_local *local =
container_of(work, struct ieee80211_local,
dynamic_ps_disable_work);
if (local->hw.conf.flags & IEEE80211_CONF_PS) {
local->hw.conf.flags &= ~IEEE80211_CONF_PS;
ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
}
ieee80211_wake_queues_by_reason(&local->hw,
IEEE80211_QUEUE_STOP_REASON_PS);
}
void ieee80211_dynamic_ps_enable_work(struct work_struct *work)
{
struct ieee80211_local *local =
container_of(work, struct ieee80211_local,
dynamic_ps_enable_work);
if (local->hw.conf.flags & IEEE80211_CONF_PS)
return;
local->hw.conf.flags |= IEEE80211_CONF_PS;
ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
}
void ieee80211_dynamic_ps_timer(unsigned long data)
{
struct ieee80211_local *local = (void *) data;
queue_work(local->hw.workqueue, &local->dynamic_ps_enable_work);
}
...@@ -1473,6 +1473,19 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb, ...@@ -1473,6 +1473,19 @@ int ieee80211_subif_start_xmit(struct sk_buff *skb,
goto fail; goto fail;
} }
if (!(local->hw.flags & IEEE80211_HW_NO_STACK_DYNAMIC_PS) &&
local->dynamic_ps_timeout > 0) {
if (local->hw.conf.flags & IEEE80211_CONF_PS) {
ieee80211_stop_queues_by_reason(&local->hw,
IEEE80211_QUEUE_STOP_REASON_PS);
queue_work(local->hw.workqueue,
&local->dynamic_ps_disable_work);
}
mod_timer(&local->dynamic_ps_timer, jiffies +
msecs_to_jiffies(local->dynamic_ps_timeout));
}
nh_pos = skb_network_header(skb) - skb->data; nh_pos = skb_network_header(skb) - skb->data;
h_pos = skb_transport_header(skb) - skb->data; h_pos = skb_transport_header(skb) - skb->data;
......
...@@ -833,7 +833,7 @@ static int ieee80211_ioctl_siwpower(struct net_device *dev, ...@@ -833,7 +833,7 @@ static int ieee80211_ioctl_siwpower(struct net_device *dev,
struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev); struct ieee80211_sub_if_data *sdata = IEEE80211_DEV_TO_SUB_IF(dev);
struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr); struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
struct ieee80211_conf *conf = &local->hw.conf; struct ieee80211_conf *conf = &local->hw.conf;
int ret = 0; int ret = 0, timeout = 0;
bool ps; bool ps;
if (sdata->vif.type != NL80211_IFTYPE_STATION) if (sdata->vif.type != NL80211_IFTYPE_STATION)
...@@ -841,6 +841,7 @@ static int ieee80211_ioctl_siwpower(struct net_device *dev, ...@@ -841,6 +841,7 @@ static int ieee80211_ioctl_siwpower(struct net_device *dev,
if (wrq->disabled) { if (wrq->disabled) {
ps = false; ps = false;
timeout = 0;
goto set; goto set;
} }
...@@ -850,22 +851,31 @@ static int ieee80211_ioctl_siwpower(struct net_device *dev, ...@@ -850,22 +851,31 @@ static int ieee80211_ioctl_siwpower(struct net_device *dev,
case IW_POWER_ALL_R: /* If explicitely state all */ case IW_POWER_ALL_R: /* If explicitely state all */
ps = true; ps = true;
break; break;
default: /* Otherwise we don't support it */ default: /* Otherwise we ignore */
return -EINVAL; break;
} }
if (ps == local->powersave) if (wrq->flags & IW_POWER_TIMEOUT)
return ret; timeout = wrq->value / 1000;
set: set:
if (ps == local->powersave && timeout == local->dynamic_ps_timeout)
return ret;
local->powersave = ps; local->powersave = ps;
local->dynamic_ps_timeout = timeout;
if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) { if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
if (!(local->hw.flags & IEEE80211_HW_NO_STACK_DYNAMIC_PS) &&
local->dynamic_ps_timeout > 0)
mod_timer(&local->dynamic_ps_timer, jiffies +
msecs_to_jiffies(local->dynamic_ps_timeout));
else {
if (local->powersave) if (local->powersave)
conf->flags |= IEEE80211_CONF_PS; conf->flags |= IEEE80211_CONF_PS;
else else
conf->flags &= ~IEEE80211_CONF_PS; conf->flags &= ~IEEE80211_CONF_PS;
}
ret = ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS); ret = ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment