xref: /dragonfly/sys/dev/netif/iwm/if_iwm_power.c (revision 6acbba79)
124a8d46aSMatthew Dillon /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
224a8d46aSMatthew Dillon 
324a8d46aSMatthew Dillon /*
424a8d46aSMatthew Dillon  * Copyright (c) 2014 genua mbh <info@genua.de>
524a8d46aSMatthew Dillon  * Copyright (c) 2014 Fixup Software Ltd.
624a8d46aSMatthew Dillon  *
724a8d46aSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
824a8d46aSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
924a8d46aSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
1024a8d46aSMatthew Dillon  *
1124a8d46aSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1224a8d46aSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1324a8d46aSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1424a8d46aSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1524a8d46aSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1624a8d46aSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1724a8d46aSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1824a8d46aSMatthew Dillon  */
1924a8d46aSMatthew Dillon 
2024a8d46aSMatthew Dillon /*-
2124a8d46aSMatthew Dillon  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
2224a8d46aSMatthew Dillon  * which were used as the reference documentation for this implementation.
2324a8d46aSMatthew Dillon  *
2424a8d46aSMatthew Dillon  * Driver version we are currently based off of is
2524a8d46aSMatthew Dillon  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
2624a8d46aSMatthew Dillon  *
2724a8d46aSMatthew Dillon  ***********************************************************************
2824a8d46aSMatthew Dillon  *
2924a8d46aSMatthew Dillon  * This file is provided under a dual BSD/GPLv2 license.  When using or
3024a8d46aSMatthew Dillon  * redistributing this file, you may do so under either license.
3124a8d46aSMatthew Dillon  *
3224a8d46aSMatthew Dillon  * GPL LICENSE SUMMARY
3324a8d46aSMatthew Dillon  *
3424a8d46aSMatthew Dillon  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
3524a8d46aSMatthew Dillon  *
3624a8d46aSMatthew Dillon  * This program is free software; you can redistribute it and/or modify
3724a8d46aSMatthew Dillon  * it under the terms of version 2 of the GNU General Public License as
3824a8d46aSMatthew Dillon  * published by the Free Software Foundation.
3924a8d46aSMatthew Dillon  *
4024a8d46aSMatthew Dillon  * This program is distributed in the hope that it will be useful, but
4124a8d46aSMatthew Dillon  * WITHOUT ANY WARRANTY; without even the implied warranty of
4224a8d46aSMatthew Dillon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4324a8d46aSMatthew Dillon  * General Public License for more details.
4424a8d46aSMatthew Dillon  *
4524a8d46aSMatthew Dillon  * You should have received a copy of the GNU General Public License
4624a8d46aSMatthew Dillon  * along with this program; if not, write to the Free Software
4724a8d46aSMatthew Dillon  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
4824a8d46aSMatthew Dillon  * USA
4924a8d46aSMatthew Dillon  *
5024a8d46aSMatthew Dillon  * The full GNU General Public License is included in this distribution
5124a8d46aSMatthew Dillon  * in the file called COPYING.
5224a8d46aSMatthew Dillon  *
5324a8d46aSMatthew Dillon  * Contact Information:
5424a8d46aSMatthew Dillon  *  Intel Linux Wireless <ilw@linux.intel.com>
5524a8d46aSMatthew Dillon  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
5624a8d46aSMatthew Dillon  *
5724a8d46aSMatthew Dillon  *
5824a8d46aSMatthew Dillon  * BSD LICENSE
5924a8d46aSMatthew Dillon  *
6024a8d46aSMatthew Dillon  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
6124a8d46aSMatthew Dillon  * All rights reserved.
6224a8d46aSMatthew Dillon  *
6324a8d46aSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6424a8d46aSMatthew Dillon  * modification, are permitted provided that the following conditions
6524a8d46aSMatthew Dillon  * are met:
6624a8d46aSMatthew Dillon  *
6724a8d46aSMatthew Dillon  *  * Redistributions of source code must retain the above copyright
6824a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
6924a8d46aSMatthew Dillon  *  * Redistributions in binary form must reproduce the above copyright
7024a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
7124a8d46aSMatthew Dillon  *    the documentation and/or other materials provided with the
7224a8d46aSMatthew Dillon  *    distribution.
7324a8d46aSMatthew Dillon  *  * Neither the name Intel Corporation nor the names of its
7424a8d46aSMatthew Dillon  *    contributors may be used to endorse or promote products derived
7524a8d46aSMatthew Dillon  *    from this software without specific prior written permission.
7624a8d46aSMatthew Dillon  *
7724a8d46aSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7824a8d46aSMatthew Dillon  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7924a8d46aSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8024a8d46aSMatthew Dillon  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8124a8d46aSMatthew Dillon  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8224a8d46aSMatthew Dillon  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8324a8d46aSMatthew Dillon  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8424a8d46aSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8524a8d46aSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8624a8d46aSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8724a8d46aSMatthew Dillon  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8824a8d46aSMatthew Dillon  */
8924a8d46aSMatthew Dillon #include <sys/param.h>
9024a8d46aSMatthew Dillon #include <sys/bus.h>
91*6acbba79SMatthew Dillon #include <sys/conf.h>
9224a8d46aSMatthew Dillon #include <sys/endian.h>
9324a8d46aSMatthew Dillon #include <sys/firmware.h>
9424a8d46aSMatthew Dillon #include <sys/kernel.h>
9524a8d46aSMatthew Dillon #include <sys/malloc.h>
9624a8d46aSMatthew Dillon #include <sys/mbuf.h>
9724a8d46aSMatthew Dillon #include <sys/rman.h>
9824a8d46aSMatthew Dillon #include <sys/sysctl.h>
9924a8d46aSMatthew Dillon #include <sys/linker.h>
10024a8d46aSMatthew Dillon 
10124a8d46aSMatthew Dillon #include <machine/endian.h>
10224a8d46aSMatthew Dillon 
10345bc40b1SMatthew Dillon #include <bus/pci/pcivar.h>
10445bc40b1SMatthew Dillon #include <bus/pci/pcireg.h>
10524a8d46aSMatthew Dillon 
10624a8d46aSMatthew Dillon #include <net/bpf.h>
10724a8d46aSMatthew Dillon 
10824a8d46aSMatthew Dillon #include <net/if.h>
10924a8d46aSMatthew Dillon #include <net/if_var.h>
11024a8d46aSMatthew Dillon #include <net/if_arp.h>
11124a8d46aSMatthew Dillon #include <net/if_dl.h>
11224a8d46aSMatthew Dillon #include <net/if_media.h>
11324a8d46aSMatthew Dillon #include <net/if_types.h>
11424a8d46aSMatthew Dillon 
11524a8d46aSMatthew Dillon #include <netinet/in.h>
11624a8d46aSMatthew Dillon #include <netinet/in_systm.h>
11724a8d46aSMatthew Dillon #include <netinet/if_ether.h>
11824a8d46aSMatthew Dillon #include <netinet/ip.h>
11924a8d46aSMatthew Dillon 
12045bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
12145bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_regdomain.h>
12245bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_ratectl.h>
12345bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_radiotap.h>
12424a8d46aSMatthew Dillon 
12545bc40b1SMatthew Dillon #include "if_iwmreg.h"
12645bc40b1SMatthew Dillon #include "if_iwmvar.h"
12745bc40b1SMatthew Dillon #include "if_iwm_debug.h"
128d7002a79SImre Vadász #include "if_iwm_constants.h"
12945bc40b1SMatthew Dillon #include "if_iwm_util.h"
13045bc40b1SMatthew Dillon #include "if_iwm_power.h"
13124a8d46aSMatthew Dillon 
132d7002a79SImre Vadász static int iwm_power_scheme = IWM_POWER_SCHEME_BPS;
133d7002a79SImre Vadász 
134d7002a79SImre Vadász TUNABLE_INT("hw.iwm.power_scheme", &iwm_power_scheme);
135d7002a79SImre Vadász 
136*6acbba79SMatthew Dillon /*
137*6acbba79SMatthew Dillon  * BEGIN mvm/power.c
138*6acbba79SMatthew Dillon  */
139*6acbba79SMatthew Dillon 
140*6acbba79SMatthew Dillon #define IWM_POWER_KEEP_ALIVE_PERIOD_SEC    25
141*6acbba79SMatthew Dillon 
14224a8d46aSMatthew Dillon static int
iwm_beacon_filter_send_cmd(struct iwm_softc * sc,struct iwm_beacon_filter_cmd * cmd)143*6acbba79SMatthew Dillon iwm_beacon_filter_send_cmd(struct iwm_softc *sc,
14424a8d46aSMatthew Dillon 	struct iwm_beacon_filter_cmd *cmd)
14524a8d46aSMatthew Dillon {
14624a8d46aSMatthew Dillon 	int ret;
14724a8d46aSMatthew Dillon 
148*6acbba79SMatthew Dillon 	ret = iwm_send_cmd_pdu(sc, IWM_REPLY_BEACON_FILTERING_CMD,
149d7002a79SImre Vadász 	    0, sizeof(struct iwm_beacon_filter_cmd), cmd);
15024a8d46aSMatthew Dillon 
15124a8d46aSMatthew Dillon 	if (!ret) {
15224a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
15324a8d46aSMatthew Dillon 		    "ba_enable_beacon_abort is: %d\n",
15424a8d46aSMatthew Dillon 		    le32toh(cmd->ba_enable_beacon_abort));
15524a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
15624a8d46aSMatthew Dillon 		    "ba_escape_timer is: %d\n",
15724a8d46aSMatthew Dillon 		    le32toh(cmd->ba_escape_timer));
15824a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
15924a8d46aSMatthew Dillon 		    "bf_debug_flag is: %d\n",
16024a8d46aSMatthew Dillon 		    le32toh(cmd->bf_debug_flag));
16124a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
16224a8d46aSMatthew Dillon 		    "bf_enable_beacon_filter is: %d\n",
16324a8d46aSMatthew Dillon 		    le32toh(cmd->bf_enable_beacon_filter));
16424a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
16524a8d46aSMatthew Dillon 		    "bf_energy_delta is: %d\n",
16624a8d46aSMatthew Dillon 		    le32toh(cmd->bf_energy_delta));
16724a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
16824a8d46aSMatthew Dillon 		    "bf_escape_timer is: %d\n",
16924a8d46aSMatthew Dillon 		    le32toh(cmd->bf_escape_timer));
17024a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
17124a8d46aSMatthew Dillon 		    "bf_roaming_energy_delta is: %d\n",
17224a8d46aSMatthew Dillon 		    le32toh(cmd->bf_roaming_energy_delta));
17324a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
17424a8d46aSMatthew Dillon 		    "bf_roaming_state is: %d\n",
17524a8d46aSMatthew Dillon 		    le32toh(cmd->bf_roaming_state));
17624a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
17724a8d46aSMatthew Dillon 		    "bf_temp_threshold is: %d\n",
17824a8d46aSMatthew Dillon 		    le32toh(cmd->bf_temp_threshold));
17924a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
18024a8d46aSMatthew Dillon 		    "bf_temp_fast_filter is: %d\n",
18124a8d46aSMatthew Dillon 		    le32toh(cmd->bf_temp_fast_filter));
18224a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
18324a8d46aSMatthew Dillon 		    "bf_temp_slow_filter is: %d\n",
18424a8d46aSMatthew Dillon 		    le32toh(cmd->bf_temp_slow_filter));
18524a8d46aSMatthew Dillon 	}
18624a8d46aSMatthew Dillon 	return ret;
18724a8d46aSMatthew Dillon }
18824a8d46aSMatthew Dillon 
18924a8d46aSMatthew Dillon static void
iwm_beacon_filter_set_cqm_params(struct iwm_softc * sc,struct iwm_vap * ivp,struct iwm_beacon_filter_cmd * cmd)190*6acbba79SMatthew Dillon iwm_beacon_filter_set_cqm_params(struct iwm_softc *sc,
19162a4e795SImre Vadász 	struct iwm_vap *ivp, struct iwm_beacon_filter_cmd *cmd)
19224a8d46aSMatthew Dillon {
19324a8d46aSMatthew Dillon 	cmd->ba_enable_beacon_abort = htole32(sc->sc_bf.ba_enabled);
19424a8d46aSMatthew Dillon }
19524a8d46aSMatthew Dillon 
19624a8d46aSMatthew Dillon static void
iwm_power_log(struct iwm_softc * sc,struct iwm_mac_power_cmd * cmd)197*6acbba79SMatthew Dillon iwm_power_log(struct iwm_softc *sc, struct iwm_mac_power_cmd *cmd)
19824a8d46aSMatthew Dillon {
19924a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
20024a8d46aSMatthew Dillon 	    "Sending power table command on mac id 0x%X for "
20124a8d46aSMatthew Dillon 	    "power level %d, flags = 0x%X\n",
20252c3adbeSImre Vadász 	    cmd->id_and_color, iwm_power_scheme, le16toh(cmd->flags));
20324a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
20424a8d46aSMatthew Dillon 	    "Keep alive = %u sec\n", le16toh(cmd->keep_alive_seconds));
20524a8d46aSMatthew Dillon 
20624a8d46aSMatthew Dillon 	if (!(cmd->flags & htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
20724a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
20824a8d46aSMatthew Dillon 		    "Disable power management\n");
20924a8d46aSMatthew Dillon 		return;
21024a8d46aSMatthew Dillon 	}
211d7002a79SImre Vadász 
212d7002a79SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
213d7002a79SImre Vadász 	    "Rx timeout = %u usec\n", le32toh(cmd->rx_data_timeout));
214d7002a79SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
215d7002a79SImre Vadász 	    "Tx timeout = %u usec\n", le32toh(cmd->tx_data_timeout));
216d7002a79SImre Vadász 	if (cmd->flags & htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK))
217d7002a79SImre Vadász 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
218d7002a79SImre Vadász 		    "DTIM periods to skip = %u\n", cmd->skip_dtim_periods);
219d7002a79SImre Vadász }
220d7002a79SImre Vadász 
221d7002a79SImre Vadász static boolean_t
iwm_power_is_radar(struct iwm_softc * sc)222*6acbba79SMatthew Dillon iwm_power_is_radar(struct iwm_softc *sc)
223d7002a79SImre Vadász {
224d7002a79SImre Vadász 	struct ieee80211com *ic = &sc->sc_ic;
225d7002a79SImre Vadász 	struct ieee80211_channel *chan;
226d7002a79SImre Vadász 	boolean_t radar_detect = FALSE;
227d7002a79SImre Vadász 
228d7002a79SImre Vadász 	chan = ic->ic_bsschan;
229d7002a79SImre Vadász 	if (chan == IEEE80211_CHAN_ANYC ||
230d7002a79SImre Vadász 	    (chan->ic_flags & IEEE80211_CHAN_DFS) != 0) {
231d7002a79SImre Vadász 		radar_detect = TRUE;
232d7002a79SImre Vadász 	}
233d7002a79SImre Vadász 
234d7002a79SImre Vadász         return radar_detect;
235d7002a79SImre Vadász }
236d7002a79SImre Vadász 
237d7002a79SImre Vadász static void
iwm_power_config_skip_dtim(struct iwm_softc * sc,struct iwm_mac_power_cmd * cmd)238*6acbba79SMatthew Dillon iwm_power_config_skip_dtim(struct iwm_softc *sc,
239d7002a79SImre Vadász 	struct iwm_mac_power_cmd *cmd)
240d7002a79SImre Vadász {
241d7002a79SImre Vadász 	struct ieee80211com *ic = &sc->sc_ic;
242d7002a79SImre Vadász 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
243d7002a79SImre Vadász 	int dtimper = vap->iv_dtim_period ?: 1;
244d7002a79SImre Vadász 	int skip;
245d7002a79SImre Vadász 
246d7002a79SImre Vadász 	/* disable, in case we're supposed to override */
247d7002a79SImre Vadász 	cmd->skip_dtim_periods = 0;
248d7002a79SImre Vadász 	cmd->flags &= ~htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK);
249d7002a79SImre Vadász 
250*6acbba79SMatthew Dillon         if (iwm_power_is_radar(sc))
251d7002a79SImre Vadász                 return;
252d7002a79SImre Vadász 
253d7002a79SImre Vadász 	if (dtimper >= 10)
254d7002a79SImre Vadász 		return;
255d7002a79SImre Vadász 
256d7002a79SImre Vadász 	/* TODO: check that multicast wake lock is off */
257d7002a79SImre Vadász 
258d7002a79SImre Vadász 	if (iwm_power_scheme != IWM_POWER_SCHEME_LP)
259d7002a79SImre Vadász 		return;
260d7002a79SImre Vadász 	skip = 2;
261d7002a79SImre Vadász 
262d7002a79SImre Vadász 	/* the firmware really expects "look at every X DTIMs", so add 1 */
263d7002a79SImre Vadász 	cmd->skip_dtim_periods = 1 + skip;
264d7002a79SImre Vadász 	cmd->flags |= htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK);
26524a8d46aSMatthew Dillon }
26624a8d46aSMatthew Dillon 
26724a8d46aSMatthew Dillon static void
iwm_power_build_cmd(struct iwm_softc * sc,struct iwm_vap * ivp,struct iwm_mac_power_cmd * cmd)268*6acbba79SMatthew Dillon iwm_power_build_cmd(struct iwm_softc *sc, struct iwm_vap *ivp,
26924a8d46aSMatthew Dillon 	struct iwm_mac_power_cmd *cmd)
27024a8d46aSMatthew Dillon {
27152c3adbeSImre Vadász 	struct ieee80211vap *vap = &ivp->iv_vap;
27252c3adbeSImre Vadász 	struct ieee80211_node *ni = vap->iv_bss;
27324a8d46aSMatthew Dillon 	int dtimper, dtimper_msec;
27424a8d46aSMatthew Dillon 	int keep_alive;
275dc2e69bdSImre Vadász 	boolean_t bss_conf_ps = FALSE;
27624a8d46aSMatthew Dillon 
277f16ef749SImre Vadász 	cmd->id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id,
278f16ef749SImre Vadász 	    ivp->color));
27924a8d46aSMatthew Dillon 	dtimper = vap->iv_dtim_period ?: 1;
28024a8d46aSMatthew Dillon 
28124a8d46aSMatthew Dillon 	/*
28224a8d46aSMatthew Dillon 	 * Regardless of power management state the driver must set
28324a8d46aSMatthew Dillon 	 * keep alive period. FW will use it for sending keep alive NDPs
28424a8d46aSMatthew Dillon 	 * immediately after association. Check that keep alive period
28524a8d46aSMatthew Dillon 	 * is at least 3 * DTIM
28624a8d46aSMatthew Dillon 	 */
28724a8d46aSMatthew Dillon 	dtimper_msec = dtimper * ni->ni_intval;
28824a8d46aSMatthew Dillon 	keep_alive
289d7002a79SImre Vadász 	    = imax(3 * dtimper_msec, 1000 * IWM_POWER_KEEP_ALIVE_PERIOD_SEC);
29024a8d46aSMatthew Dillon 	keep_alive = roundup(keep_alive, 1000) / 1000;
29124a8d46aSMatthew Dillon 	cmd->keep_alive_seconds = htole16(keep_alive);
292d7002a79SImre Vadász 
293d7002a79SImre Vadász 	if (sc->sc_ps_disabled)
294d7002a79SImre Vadász 		return;
295d7002a79SImre Vadász 
296d7002a79SImre Vadász 	cmd->flags |= htole16(IWM_POWER_FLAGS_POWER_SAVE_ENA_MSK);
297dc2e69bdSImre Vadász 
298dc2e69bdSImre Vadász 	if (IWM_NODE(ni)->in_assoc &&
299dc2e69bdSImre Vadász 	    (vap->iv_flags & IEEE80211_F_PMGTON) != 0) {
300dc2e69bdSImre Vadász 		bss_conf_ps = TRUE;
301dc2e69bdSImre Vadász 	}
302dc2e69bdSImre Vadász 	if (!bss_conf_ps)
303dc2e69bdSImre Vadász 		return;
304dc2e69bdSImre Vadász 
305d7002a79SImre Vadász 	cmd->flags |= htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
306d7002a79SImre Vadász 
307*6acbba79SMatthew Dillon 	iwm_power_config_skip_dtim(sc, cmd);
308d7002a79SImre Vadász 
309d7002a79SImre Vadász 	cmd->rx_data_timeout =
310*6acbba79SMatthew Dillon 		htole32(IWM_DEFAULT_PS_RX_DATA_TIMEOUT);
311d7002a79SImre Vadász 	cmd->tx_data_timeout =
312*6acbba79SMatthew Dillon 		htole32(IWM_DEFAULT_PS_TX_DATA_TIMEOUT);
31324a8d46aSMatthew Dillon }
31424a8d46aSMatthew Dillon 
315d7002a79SImre Vadász static int
iwm_power_send_cmd(struct iwm_softc * sc,struct iwm_vap * ivp)316*6acbba79SMatthew Dillon iwm_power_send_cmd(struct iwm_softc *sc, struct iwm_vap *ivp)
31724a8d46aSMatthew Dillon {
318d7002a79SImre Vadász 	struct iwm_mac_power_cmd cmd = {};
31924a8d46aSMatthew Dillon 
320*6acbba79SMatthew Dillon 	iwm_power_build_cmd(sc, ivp, &cmd);
321*6acbba79SMatthew Dillon 	iwm_power_log(sc, &cmd);
32224a8d46aSMatthew Dillon 
323*6acbba79SMatthew Dillon 	return iwm_send_cmd_pdu(sc, IWM_MAC_PM_POWER_TABLE, 0,
324d7002a79SImre Vadász 	    sizeof(cmd), &cmd);
32524a8d46aSMatthew Dillon }
32624a8d46aSMatthew Dillon 
327d7002a79SImre Vadász static int
_iwm_enable_beacon_filter(struct iwm_softc * sc,struct iwm_vap * ivp,struct iwm_beacon_filter_cmd * cmd)328*6acbba79SMatthew Dillon _iwm_enable_beacon_filter(struct iwm_softc *sc, struct iwm_vap *ivp,
329d7002a79SImre Vadász 	struct iwm_beacon_filter_cmd *cmd)
33024a8d46aSMatthew Dillon {
331d7002a79SImre Vadász 	int ret;
33224a8d46aSMatthew Dillon 
333*6acbba79SMatthew Dillon 	iwm_beacon_filter_set_cqm_params(sc, ivp, cmd);
334*6acbba79SMatthew Dillon 	ret = iwm_beacon_filter_send_cmd(sc, cmd);
33524a8d46aSMatthew Dillon 
336d7002a79SImre Vadász 	if (!ret)
337d7002a79SImre Vadász 		sc->sc_bf.bf_enabled = 1;
338d7002a79SImre Vadász 
339d7002a79SImre Vadász 	return ret;
34024a8d46aSMatthew Dillon }
34124a8d46aSMatthew Dillon 
34224a8d46aSMatthew Dillon int
iwm_enable_beacon_filter(struct iwm_softc * sc,struct iwm_vap * ivp)343*6acbba79SMatthew Dillon iwm_enable_beacon_filter(struct iwm_softc *sc, struct iwm_vap *ivp)
34424a8d46aSMatthew Dillon {
34524a8d46aSMatthew Dillon 	struct iwm_beacon_filter_cmd cmd = {
34624a8d46aSMatthew Dillon 		IWM_BF_CMD_CONFIG_DEFAULTS,
34724a8d46aSMatthew Dillon 		.bf_enable_beacon_filter = htole32(1),
34824a8d46aSMatthew Dillon 	};
34924a8d46aSMatthew Dillon 
350*6acbba79SMatthew Dillon 	return _iwm_enable_beacon_filter(sc, ivp, &cmd);
35124a8d46aSMatthew Dillon }
35224a8d46aSMatthew Dillon 
35324a8d46aSMatthew Dillon int
iwm_disable_beacon_filter(struct iwm_softc * sc)354*6acbba79SMatthew Dillon iwm_disable_beacon_filter(struct iwm_softc *sc)
35524a8d46aSMatthew Dillon {
356d078c812SImre Vadász 	struct iwm_beacon_filter_cmd cmd = {};
35724a8d46aSMatthew Dillon 	int ret;
35824a8d46aSMatthew Dillon 
359*6acbba79SMatthew Dillon 	ret = iwm_beacon_filter_send_cmd(sc, &cmd);
36024a8d46aSMatthew Dillon 	if (ret == 0)
36124a8d46aSMatthew Dillon 		sc->sc_bf.bf_enabled = 0;
36224a8d46aSMatthew Dillon 
36324a8d46aSMatthew Dillon 	return ret;
36424a8d46aSMatthew Dillon }
365d7002a79SImre Vadász 
366d7002a79SImre Vadász static int
iwm_power_set_ps(struct iwm_softc * sc)367*6acbba79SMatthew Dillon iwm_power_set_ps(struct iwm_softc *sc)
368d7002a79SImre Vadász {
369dc2e69bdSImre Vadász 	struct ieee80211vap *vap;
370d7002a79SImre Vadász 	boolean_t disable_ps;
371d7002a79SImre Vadász 	int ret;
372d7002a79SImre Vadász 
373d7002a79SImre Vadász 	/* disable PS if CAM */
374d7002a79SImre Vadász 	disable_ps = (iwm_power_scheme == IWM_POWER_SCHEME_CAM);
375d7002a79SImre Vadász 	/* ...or if any of the vifs require PS to be off */
376dc2e69bdSImre Vadász 	TAILQ_FOREACH(vap, &sc->sc_ic.ic_vaps, iv_next) {
377dc2e69bdSImre Vadász 		struct iwm_vap *ivp = IWM_VAP(vap);
378dc2e69bdSImre Vadász 		if (ivp->phy_ctxt != NULL && ivp->ps_disabled)
379d7002a79SImre Vadász 			disable_ps = TRUE;
380dc2e69bdSImre Vadász 	}
381d7002a79SImre Vadász 
382d7002a79SImre Vadász 	/* update device power state if it has changed */
383d7002a79SImre Vadász 	if (sc->sc_ps_disabled != disable_ps) {
384d7002a79SImre Vadász 		boolean_t old_ps_disabled = sc->sc_ps_disabled;
385d7002a79SImre Vadász 
386d7002a79SImre Vadász 		sc->sc_ps_disabled = disable_ps;
387*6acbba79SMatthew Dillon 		ret = iwm_power_update_device(sc);
388d7002a79SImre Vadász 		if (ret) {
389d7002a79SImre Vadász 			sc->sc_ps_disabled = old_ps_disabled;
390d7002a79SImre Vadász 			return ret;
391d7002a79SImre Vadász 		}
392d7002a79SImre Vadász 	}
393d7002a79SImre Vadász 
394d7002a79SImre Vadász 	return 0;
395d7002a79SImre Vadász }
396d7002a79SImre Vadász 
397d7002a79SImre Vadász static int
iwm_power_set_ba(struct iwm_softc * sc,struct iwm_vap * ivp)398*6acbba79SMatthew Dillon iwm_power_set_ba(struct iwm_softc *sc, struct iwm_vap *ivp)
399d7002a79SImre Vadász {
400d7002a79SImre Vadász 	struct iwm_beacon_filter_cmd cmd = {
401d7002a79SImre Vadász 		IWM_BF_CMD_CONFIG_DEFAULTS,
402d7002a79SImre Vadász 		.bf_enable_beacon_filter = htole32(1),
403d7002a79SImre Vadász 	};
404dc2e69bdSImre Vadász 	struct ieee80211vap *vap = &ivp->iv_vap;
405dc2e69bdSImre Vadász 	struct ieee80211_node *ni = vap->iv_bss;
406dc2e69bdSImre Vadász 	boolean_t bss_conf_ps = FALSE;
407d7002a79SImre Vadász 
408d7002a79SImre Vadász 	if (!sc->sc_bf.bf_enabled)
409d7002a79SImre Vadász 		return 0;
410d7002a79SImre Vadász 
411dc2e69bdSImre Vadász 	if (ni != NULL && IWM_NODE(ni)->in_assoc &&
412dc2e69bdSImre Vadász 	    (vap->iv_flags & IEEE80211_F_PMGTON) != 0) {
413dc2e69bdSImre Vadász 		bss_conf_ps = TRUE;
414dc2e69bdSImre Vadász 	}
415dc2e69bdSImre Vadász 	sc->sc_bf.ba_enabled = !sc->sc_ps_disabled && bss_conf_ps;
416d7002a79SImre Vadász 
417*6acbba79SMatthew Dillon 	return _iwm_enable_beacon_filter(sc, ivp, &cmd);
418d7002a79SImre Vadász }
419d7002a79SImre Vadász 
420d7002a79SImre Vadász int
iwm_power_update_ps(struct iwm_softc * sc)421*6acbba79SMatthew Dillon iwm_power_update_ps(struct iwm_softc *sc)
422d7002a79SImre Vadász {
423d7002a79SImre Vadász 	struct ieee80211vap *vap = TAILQ_FIRST(&sc->sc_ic.ic_vaps);
424d7002a79SImre Vadász 	int ret;
425d7002a79SImre Vadász 
426*6acbba79SMatthew Dillon 	ret = iwm_power_set_ps(sc);
427d7002a79SImre Vadász 	if (ret)
428d7002a79SImre Vadász 		return ret;
429d7002a79SImre Vadász 
430d7002a79SImre Vadász 	if (vap != NULL)
431*6acbba79SMatthew Dillon 		return iwm_power_set_ba(sc, IWM_VAP(vap));
432d7002a79SImre Vadász 
433d7002a79SImre Vadász 	return 0;
434d7002a79SImre Vadász }
435d7002a79SImre Vadász 
436d7002a79SImre Vadász int
iwm_power_update_mac(struct iwm_softc * sc)437*6acbba79SMatthew Dillon iwm_power_update_mac(struct iwm_softc *sc)
438d7002a79SImre Vadász {
439d7002a79SImre Vadász 	struct ieee80211vap *vap = TAILQ_FIRST(&sc->sc_ic.ic_vaps);
440d7002a79SImre Vadász 	int ret;
441d7002a79SImre Vadász 
442*6acbba79SMatthew Dillon 	ret = iwm_power_set_ps(sc);
443d7002a79SImre Vadász 	if (ret)
444d7002a79SImre Vadász 		return ret;
445d7002a79SImre Vadász 
446d7002a79SImre Vadász 	if (vap != NULL) {
447*6acbba79SMatthew Dillon 		ret = iwm_power_send_cmd(sc, IWM_VAP(vap));
448d7002a79SImre Vadász 		if (ret)
449d7002a79SImre Vadász 			return ret;
450d7002a79SImre Vadász 	}
451d7002a79SImre Vadász 
452d7002a79SImre Vadász 	if (vap != NULL)
453*6acbba79SMatthew Dillon 		return iwm_power_set_ba(sc, IWM_VAP(vap));
454d7002a79SImre Vadász 
455d7002a79SImre Vadász 	return 0;
456d7002a79SImre Vadász }
457d7002a79SImre Vadász 
458d7002a79SImre Vadász int
iwm_power_update_device(struct iwm_softc * sc)459*6acbba79SMatthew Dillon iwm_power_update_device(struct iwm_softc *sc)
460d7002a79SImre Vadász {
461d7002a79SImre Vadász 	struct iwm_device_power_cmd cmd = {
462d7002a79SImre Vadász 		.flags = 0,
463d7002a79SImre Vadász 	};
464d7002a79SImre Vadász 
465d7002a79SImre Vadász 	if (iwm_power_scheme == IWM_POWER_SCHEME_CAM)
466d7002a79SImre Vadász 		sc->sc_ps_disabled = TRUE;
467d7002a79SImre Vadász 
468d7002a79SImre Vadász 	if (!sc->sc_ps_disabled)
469d7002a79SImre Vadász 		cmd.flags |= htole16(IWM_DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
470d7002a79SImre Vadász 
471d7002a79SImre Vadász 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
472d7002a79SImre Vadász 	    "Sending device power command with flags = 0x%X\n", cmd.flags);
473d7002a79SImre Vadász 
474*6acbba79SMatthew Dillon 	return iwm_send_cmd_pdu(sc,
475d7002a79SImre Vadász 	    IWM_POWER_TABLE_CMD, 0, sizeof(cmd), &cmd);
476d7002a79SImre Vadász }
477