xref: /dragonfly/sys/dev/netif/iwm/if_iwm_power.c (revision 44753b81)
1 /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
2 
3 /*
4  * Copyright (c) 2014 genua mbh <info@genua.de>
5  * Copyright (c) 2014 Fixup Software Ltd.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*-
21  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22  * which were used as the reference documentation for this implementation.
23  *
24  * Driver version we are currently based off of is
25  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26  *
27  ***********************************************************************
28  *
29  * This file is provided under a dual BSD/GPLv2 license.  When using or
30  * redistributing this file, you may do so under either license.
31  *
32  * GPL LICENSE SUMMARY
33  *
34  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35  *
36  * This program is free software; you can redistribute it and/or modify
37  * it under the terms of version 2 of the GNU General Public License as
38  * published by the Free Software Foundation.
39  *
40  * This program is distributed in the hope that it will be useful, but
41  * WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43  * General Public License for more details.
44  *
45  * You should have received a copy of the GNU General Public License
46  * along with this program; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48  * USA
49  *
50  * The full GNU General Public License is included in this distribution
51  * in the file called COPYING.
52  *
53  * Contact Information:
54  *  Intel Linux Wireless <ilw@linux.intel.com>
55  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56  *
57  *
58  * BSD LICENSE
59  *
60  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61  * All rights reserved.
62  *
63  * Redistribution and use in source and binary forms, with or without
64  * modification, are permitted provided that the following conditions
65  * are met:
66  *
67  *  * Redistributions of source code must retain the above copyright
68  *    notice, this list of conditions and the following disclaimer.
69  *  * Redistributions in binary form must reproduce the above copyright
70  *    notice, this list of conditions and the following disclaimer in
71  *    the documentation and/or other materials provided with the
72  *    distribution.
73  *  * Neither the name Intel Corporation nor the names of its
74  *    contributors may be used to endorse or promote products derived
75  *    from this software without specific prior written permission.
76  *
77  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88  */
89 #include <sys/cdefs.h>
90 __FBSDID("$FreeBSD$");
91 
92 #include <sys/param.h>
93 #include <sys/bus.h>
94 #include <sys/endian.h>
95 #include <sys/firmware.h>
96 #include <sys/kernel.h>
97 #include <sys/malloc.h>
98 #include <sys/mbuf.h>
99 #include <sys/rman.h>
100 #include <sys/sysctl.h>
101 #include <sys/linker.h>
102 
103 #include <machine/endian.h>
104 
105 #include <bus/pci/pcivar.h>
106 #include <bus/pci/pcireg.h>
107 
108 #include <net/bpf.h>
109 
110 #include <net/if.h>
111 #include <net/if_var.h>
112 #include <net/if_arp.h>
113 #include <net/if_dl.h>
114 #include <net/if_media.h>
115 #include <net/if_types.h>
116 
117 #include <netinet/in.h>
118 #include <netinet/in_systm.h>
119 #include <netinet/if_ether.h>
120 #include <netinet/ip.h>
121 
122 #include <netproto/802_11/ieee80211_var.h>
123 #include <netproto/802_11/ieee80211_regdomain.h>
124 #include <netproto/802_11/ieee80211_ratectl.h>
125 #include <netproto/802_11/ieee80211_radiotap.h>
126 
127 #include "if_iwmreg.h"
128 #include "if_iwmvar.h"
129 #include "if_iwm_debug.h"
130 #include "if_iwm_constants.h"
131 #include "if_iwm_util.h"
132 #include "if_iwm_power.h"
133 
134 #define IWM_POWER_KEEP_ALIVE_PERIOD_SEC    25
135 
136 static int iwm_power_scheme = IWM_POWER_SCHEME_BPS;
137 
138 TUNABLE_INT("hw.iwm.power_scheme", &iwm_power_scheme);
139 
140 static int
141 iwm_mvm_beacon_filter_send_cmd(struct iwm_softc *sc,
142 	struct iwm_beacon_filter_cmd *cmd)
143 {
144 	int ret;
145 
146 	ret = iwm_mvm_send_cmd_pdu(sc, IWM_REPLY_BEACON_FILTERING_CMD,
147 	    0, sizeof(struct iwm_beacon_filter_cmd), cmd);
148 
149 	if (!ret) {
150 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
151 		    "ba_enable_beacon_abort is: %d\n",
152 		    le32toh(cmd->ba_enable_beacon_abort));
153 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
154 		    "ba_escape_timer is: %d\n",
155 		    le32toh(cmd->ba_escape_timer));
156 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
157 		    "bf_debug_flag is: %d\n",
158 		    le32toh(cmd->bf_debug_flag));
159 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
160 		    "bf_enable_beacon_filter is: %d\n",
161 		    le32toh(cmd->bf_enable_beacon_filter));
162 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
163 		    "bf_energy_delta is: %d\n",
164 		    le32toh(cmd->bf_energy_delta));
165 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
166 		    "bf_escape_timer is: %d\n",
167 		    le32toh(cmd->bf_escape_timer));
168 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
169 		    "bf_roaming_energy_delta is: %d\n",
170 		    le32toh(cmd->bf_roaming_energy_delta));
171 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
172 		    "bf_roaming_state is: %d\n",
173 		    le32toh(cmd->bf_roaming_state));
174 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
175 		    "bf_temp_threshold is: %d\n",
176 		    le32toh(cmd->bf_temp_threshold));
177 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
178 		    "bf_temp_fast_filter is: %d\n",
179 		    le32toh(cmd->bf_temp_fast_filter));
180 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
181 		    "bf_temp_slow_filter is: %d\n",
182 		    le32toh(cmd->bf_temp_slow_filter));
183 	}
184 	return ret;
185 }
186 
187 static void
188 iwm_mvm_beacon_filter_set_cqm_params(struct iwm_softc *sc,
189 	struct iwm_node *in, struct iwm_beacon_filter_cmd *cmd)
190 {
191 	cmd->ba_enable_beacon_abort = htole32(sc->sc_bf.ba_enabled);
192 }
193 
194 static void
195 iwm_mvm_power_log(struct iwm_softc *sc, struct iwm_mac_power_cmd *cmd)
196 {
197 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
198 	    "Sending power table command on mac id 0x%X for "
199 	    "power level %d, flags = 0x%X\n",
200 	    cmd->id_and_color, IWM_POWER_SCHEME_CAM, le16toh(cmd->flags));
201 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
202 	    "Keep alive = %u sec\n", le16toh(cmd->keep_alive_seconds));
203 
204 	if (!(cmd->flags & htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
205 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
206 		    "Disable power management\n");
207 		return;
208 	}
209 
210 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
211 	    "Rx timeout = %u usec\n", le32toh(cmd->rx_data_timeout));
212 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
213 	    "Tx timeout = %u usec\n", le32toh(cmd->tx_data_timeout));
214 	if (cmd->flags & htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK))
215 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
216 		    "DTIM periods to skip = %u\n", cmd->skip_dtim_periods);
217 }
218 
219 static boolean_t
220 iwm_mvm_power_is_radar(struct iwm_softc *sc)
221 {
222 	struct ieee80211com *ic = &sc->sc_ic;
223 	struct ieee80211_channel *chan;
224 	boolean_t radar_detect = FALSE;
225 
226 	chan = ic->ic_bsschan;
227 	if (chan == IEEE80211_CHAN_ANYC ||
228 	    (chan->ic_flags & IEEE80211_CHAN_DFS) != 0) {
229 		radar_detect = TRUE;
230 	}
231 
232         return radar_detect;
233 }
234 
235 static void
236 iwm_mvm_power_config_skip_dtim(struct iwm_softc *sc,
237 	struct iwm_mac_power_cmd *cmd)
238 {
239 	struct ieee80211com *ic = &sc->sc_ic;
240 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
241 	int dtimper = vap->iv_dtim_period ?: 1;
242 	int skip;
243 
244 	/* disable, in case we're supposed to override */
245 	cmd->skip_dtim_periods = 0;
246 	cmd->flags &= ~htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK);
247 
248         if (iwm_mvm_power_is_radar(sc))
249                 return;
250 
251 	if (dtimper >= 10)
252 		return;
253 
254 	/* TODO: check that multicast wake lock is off */
255 
256 	if (iwm_power_scheme != IWM_POWER_SCHEME_LP)
257 		return;
258 	skip = 2;
259 
260 	/* the firmware really expects "look at every X DTIMs", so add 1 */
261 	cmd->skip_dtim_periods = 1 + skip;
262 	cmd->flags |= htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK);
263 }
264 
265 static void
266 iwm_mvm_power_build_cmd(struct iwm_softc *sc, struct iwm_node *in,
267 	struct iwm_mac_power_cmd *cmd)
268 {
269 	struct ieee80211_node *ni = &in->in_ni;
270 	int dtimper, dtimper_msec;
271 	int keep_alive;
272 	struct ieee80211com *ic = &sc->sc_ic;
273 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
274 	struct iwm_vap *ivp = IWM_VAP(vap);
275 
276 	cmd->id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id,
277 	    ivp->color));
278 	dtimper = vap->iv_dtim_period ?: 1;
279 
280 	/*
281 	 * Regardless of power management state the driver must set
282 	 * keep alive period. FW will use it for sending keep alive NDPs
283 	 * immediately after association. Check that keep alive period
284 	 * is at least 3 * DTIM
285 	 */
286 	dtimper_msec = dtimper * ni->ni_intval;
287 	keep_alive
288 	    = imax(3 * dtimper_msec, 1000 * IWM_POWER_KEEP_ALIVE_PERIOD_SEC);
289 	keep_alive = roundup(keep_alive, 1000) / 1000;
290 	cmd->keep_alive_seconds = htole16(keep_alive);
291 
292 	if (sc->sc_ps_disabled)
293 		return;
294 
295 	cmd->flags |= htole16(IWM_POWER_FLAGS_POWER_SAVE_ENA_MSK);
296 	cmd->flags |= htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
297 
298 	iwm_mvm_power_config_skip_dtim(sc, cmd);
299 
300 	cmd->rx_data_timeout =
301 		htole32(IWM_MVM_DEFAULT_PS_RX_DATA_TIMEOUT);
302 	cmd->tx_data_timeout =
303 		htole32(IWM_MVM_DEFAULT_PS_TX_DATA_TIMEOUT);
304 }
305 
306 static int
307 iwm_mvm_power_send_cmd(struct iwm_softc *sc, struct iwm_node *in)
308 {
309 	struct iwm_mac_power_cmd cmd = {};
310 
311 	iwm_mvm_power_build_cmd(sc, in, &cmd);
312 	iwm_mvm_power_log(sc, &cmd);
313 
314 	return iwm_mvm_send_cmd_pdu(sc, IWM_MAC_PM_POWER_TABLE, 0,
315 	    sizeof(cmd), &cmd);
316 }
317 
318 static int
319 _iwm_mvm_enable_beacon_filter(struct iwm_softc *sc, struct iwm_node *in,
320 	struct iwm_beacon_filter_cmd *cmd)
321 {
322 	int ret;
323 
324 	iwm_mvm_beacon_filter_set_cqm_params(sc, in, cmd);
325 	ret = iwm_mvm_beacon_filter_send_cmd(sc, cmd);
326 
327 	if (!ret)
328 		sc->sc_bf.bf_enabled = 1;
329 
330 	return ret;
331 }
332 
333 int
334 iwm_mvm_enable_beacon_filter(struct iwm_softc *sc, struct iwm_node *in)
335 {
336 	struct iwm_beacon_filter_cmd cmd = {
337 		IWM_BF_CMD_CONFIG_DEFAULTS,
338 		.bf_enable_beacon_filter = htole32(1),
339 	};
340 
341 	return _iwm_mvm_enable_beacon_filter(sc, in, &cmd);
342 }
343 
344 int
345 iwm_mvm_disable_beacon_filter(struct iwm_softc *sc)
346 {
347 	struct iwm_beacon_filter_cmd cmd = {};
348 	int ret;
349 
350 	ret = iwm_mvm_beacon_filter_send_cmd(sc, &cmd);
351 	if (ret == 0)
352 		sc->sc_bf.bf_enabled = 0;
353 
354 	return ret;
355 }
356 
357 static int
358 iwm_mvm_power_set_ps(struct iwm_softc *sc)
359 {
360 	struct ieee80211vap *vap = TAILQ_FIRST(&sc->sc_ic.ic_vaps);
361 	boolean_t disable_ps;
362 	int ret;
363 
364 	/* disable PS if CAM */
365 	disable_ps = (iwm_power_scheme == IWM_POWER_SCHEME_CAM);
366 	/* ...or if any of the vifs require PS to be off */
367 	if (vap != NULL && (vap->iv_flags & IEEE80211_F_PMGTON) == 0)
368 		disable_ps = TRUE;
369 
370 	/* update device power state if it has changed */
371 	if (sc->sc_ps_disabled != disable_ps) {
372 		boolean_t old_ps_disabled = sc->sc_ps_disabled;
373 
374 		sc->sc_ps_disabled = disable_ps;
375 		ret = iwm_mvm_power_update_device(sc);
376 		if (ret) {
377 			sc->sc_ps_disabled = old_ps_disabled;
378 			return ret;
379 		}
380 	}
381 
382 	return 0;
383 }
384 
385 static int
386 iwm_mvm_power_set_ba(struct iwm_softc *sc, struct iwm_node *in)
387 {
388 	struct iwm_beacon_filter_cmd cmd = {
389 		IWM_BF_CMD_CONFIG_DEFAULTS,
390 		.bf_enable_beacon_filter = htole32(1),
391 	};
392 
393 	if (!sc->sc_bf.bf_enabled)
394 		return 0;
395 
396 	sc->sc_bf.ba_enabled = !sc->sc_ps_disabled;
397 
398 	return _iwm_mvm_enable_beacon_filter(sc, in, &cmd);
399 }
400 
401 int
402 iwm_mvm_power_update_ps(struct iwm_softc *sc)
403 {
404 	struct ieee80211vap *vap = TAILQ_FIRST(&sc->sc_ic.ic_vaps);
405 	int ret;
406 
407 	ret = iwm_mvm_power_set_ps(sc);
408 	if (ret)
409 		return ret;
410 
411 	if (vap != NULL)
412 		return iwm_mvm_power_set_ba(sc, IWM_NODE(vap->iv_bss));
413 
414 	return 0;
415 }
416 
417 int
418 iwm_mvm_power_update_mac(struct iwm_softc *sc)
419 {
420 	struct ieee80211vap *vap = TAILQ_FIRST(&sc->sc_ic.ic_vaps);
421 	int ret;
422 
423 	ret = iwm_mvm_power_set_ps(sc);
424 	if (ret)
425 		return ret;
426 
427 	if (vap != NULL) {
428 		ret = iwm_mvm_power_send_cmd(sc, IWM_NODE(vap->iv_bss));
429 		if (ret)
430 			return ret;
431 	}
432 
433 	if (vap != NULL)
434 		return iwm_mvm_power_set_ba(sc, IWM_NODE(vap->iv_bss));
435 
436 	return 0;
437 }
438 
439 int
440 iwm_mvm_power_update_device(struct iwm_softc *sc)
441 {
442 	struct iwm_device_power_cmd cmd = {
443 		.flags = 0,
444 	};
445 
446 	if (iwm_power_scheme == IWM_POWER_SCHEME_CAM)
447 		sc->sc_ps_disabled = TRUE;
448 
449 	if (!sc->sc_ps_disabled)
450 		cmd.flags |= htole16(IWM_DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
451 
452 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
453 	    "Sending device power command with flags = 0x%X\n", cmd.flags);
454 
455 	return iwm_mvm_send_cmd_pdu(sc,
456 	    IWM_POWER_TABLE_CMD, 0, sizeof(cmd), &cmd);
457 }
458