xref: /dragonfly/sys/dev/netif/iwm/if_iwm_power.c (revision 9309d6f1)
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/mutex.h>
100 #include <sys/module.h>
101 #include <sys/proc.h>
102 #include <sys/rman.h>
103 #include <sys/socket.h>
104 #include <sys/sockio.h>
105 #include <sys/sysctl.h>
106 #include <sys/linker.h>
107 
108 #include <machine/endian.h>
109 
110 #include <bus/pci/pcivar.h>
111 #include <bus/pci/pcireg.h>
112 
113 #include <net/bpf.h>
114 
115 #include <net/if.h>
116 #include <net/if_var.h>
117 #include <net/if_arp.h>
118 #include <net/ethernet.h>
119 #include <net/if_dl.h>
120 #include <net/if_media.h>
121 #include <net/if_types.h>
122 #include <net/ifq_var.h>
123 
124 #include <netinet/in.h>
125 #include <netinet/in_systm.h>
126 #include <netinet/if_ether.h>
127 #include <netinet/ip.h>
128 
129 #include <netproto/802_11/ieee80211_var.h>
130 #include <netproto/802_11/ieee80211_regdomain.h>
131 #include <netproto/802_11/ieee80211_ratectl.h>
132 #include <netproto/802_11/ieee80211_radiotap.h>
133 
134 #include "if_iwmreg.h"
135 #include "if_iwmvar.h"
136 #include "if_iwm_debug.h"
137 #include "if_iwm_util.h"
138 #include "if_iwm_power.h"
139 
140 #define IWM_POWER_KEEP_ALIVE_PERIOD_SEC    25
141 
142 static int
143 iwm_mvm_beacon_filter_send_cmd(struct iwm_softc *sc,
144 	struct iwm_beacon_filter_cmd *cmd)
145 {
146 	int ret;
147 
148 	ret = iwm_mvm_send_cmd_pdu(sc, IWM_REPLY_BEACON_FILTERING_CMD,
149 	    IWM_CMD_SYNC, sizeof(struct iwm_beacon_filter_cmd), cmd);
150 
151 	if (!ret) {
152 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
153 		    "ba_enable_beacon_abort is: %d\n",
154 		    le32toh(cmd->ba_enable_beacon_abort));
155 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
156 		    "ba_escape_timer is: %d\n",
157 		    le32toh(cmd->ba_escape_timer));
158 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
159 		    "bf_debug_flag is: %d\n",
160 		    le32toh(cmd->bf_debug_flag));
161 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
162 		    "bf_enable_beacon_filter is: %d\n",
163 		    le32toh(cmd->bf_enable_beacon_filter));
164 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
165 		    "bf_energy_delta is: %d\n",
166 		    le32toh(cmd->bf_energy_delta));
167 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
168 		    "bf_escape_timer is: %d\n",
169 		    le32toh(cmd->bf_escape_timer));
170 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
171 		    "bf_roaming_energy_delta is: %d\n",
172 		    le32toh(cmd->bf_roaming_energy_delta));
173 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
174 		    "bf_roaming_state is: %d\n",
175 		    le32toh(cmd->bf_roaming_state));
176 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
177 		    "bf_temp_threshold is: %d\n",
178 		    le32toh(cmd->bf_temp_threshold));
179 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
180 		    "bf_temp_fast_filter is: %d\n",
181 		    le32toh(cmd->bf_temp_fast_filter));
182 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
183 		    "bf_temp_slow_filter is: %d\n",
184 		    le32toh(cmd->bf_temp_slow_filter));
185 	}
186 	return ret;
187 }
188 
189 static void
190 iwm_mvm_beacon_filter_set_cqm_params(struct iwm_softc *sc,
191 	struct iwm_node *in, struct iwm_beacon_filter_cmd *cmd)
192 {
193 	cmd->ba_enable_beacon_abort = htole32(sc->sc_bf.ba_enabled);
194 }
195 
196 static int
197 iwm_mvm_update_beacon_abort(struct iwm_softc *sc, struct iwm_node *in,
198 	int enable)
199 {
200 	struct iwm_beacon_filter_cmd cmd = {
201 		IWM_BF_CMD_CONFIG_DEFAULTS,
202 		.bf_enable_beacon_filter = htole32(1),
203 		.ba_enable_beacon_abort = htole32(enable),
204 	};
205 
206 	if (!sc->sc_bf.bf_enabled)
207 		return 0;
208 
209 	sc->sc_bf.ba_enabled = enable;
210 	iwm_mvm_beacon_filter_set_cqm_params(sc, in, &cmd);
211 	return iwm_mvm_beacon_filter_send_cmd(sc, &cmd);
212 }
213 
214 static void
215 iwm_mvm_power_log(struct iwm_softc *sc, struct iwm_mac_power_cmd *cmd)
216 {
217 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
218 	    "Sending power table command on mac id 0x%X for "
219 	    "power level %d, flags = 0x%X\n",
220 	    cmd->id_and_color, IWM_POWER_SCHEME_CAM, le16toh(cmd->flags));
221 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
222 	    "Keep alive = %u sec\n", le16toh(cmd->keep_alive_seconds));
223 
224 	if (!(cmd->flags & htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
225 		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
226 		    "Disable power management\n");
227 		return;
228 	}
229 	KASSERT(0, ("unhandled power management"));
230 
231 #if 0
232 	DPRINTF(mvm, "Rx timeout = %u usec\n",
233 			le32_to_cpu(cmd->rx_data_timeout));
234 	DPRINTF(mvm, "Tx timeout = %u usec\n",
235 			le32_to_cpu(cmd->tx_data_timeout));
236 	if (cmd->flags & cpu_to_le16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK))
237 		DPRINTF(mvm, "DTIM periods to skip = %u\n",
238 				cmd->skip_dtim_periods);
239 	if (cmd->flags & cpu_to_le16(IWM_POWER_FLAGS_LPRX_ENA_MSK))
240 		DPRINTF(mvm, "LP RX RSSI threshold = %u\n",
241 				cmd->lprx_rssi_threshold);
242 	if (cmd->flags & cpu_to_le16(IWM_POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
243 		DPRINTF(mvm, "uAPSD enabled\n");
244 		DPRINTF(mvm, "Rx timeout (uAPSD) = %u usec\n",
245 				le32_to_cpu(cmd->rx_data_timeout_uapsd));
246 		DPRINTF(mvm, "Tx timeout (uAPSD) = %u usec\n",
247 				le32_to_cpu(cmd->tx_data_timeout_uapsd));
248 		DPRINTF(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
249 		DPRINTF(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
250 		DPRINTF(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
251 	}
252 #endif
253 }
254 
255 static void
256 iwm_mvm_power_build_cmd(struct iwm_softc *sc, struct iwm_node *in,
257 	struct iwm_mac_power_cmd *cmd)
258 {
259 	struct ieee80211_node *ni = &in->in_ni;
260 	int dtimper, dtimper_msec;
261 	int keep_alive;
262 	struct ieee80211com *ic = sc->sc_ic;
263 	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
264 
265 	cmd->id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(IWM_DEFAULT_MACID,
266 	    IWM_DEFAULT_COLOR));
267 	dtimper = vap->iv_dtim_period ?: 1;
268 
269 	/*
270 	 * Regardless of power management state the driver must set
271 	 * keep alive period. FW will use it for sending keep alive NDPs
272 	 * immediately after association. Check that keep alive period
273 	 * is at least 3 * DTIM
274 	 */
275 	dtimper_msec = dtimper * ni->ni_intval;
276 	keep_alive
277 	    = MAX(3 * dtimper_msec, 1000 * IWM_POWER_KEEP_ALIVE_PERIOD_SEC);
278 	keep_alive = roundup(keep_alive, 1000) / 1000;
279 	cmd->keep_alive_seconds = htole16(keep_alive);
280 }
281 
282 int
283 iwm_mvm_power_mac_update_mode(struct iwm_softc *sc, struct iwm_node *in)
284 {
285 	int ret;
286 	int ba_enable;
287 	struct iwm_mac_power_cmd cmd;
288 
289 	memset(&cmd, 0, sizeof(cmd));
290 
291 	iwm_mvm_power_build_cmd(sc, in, &cmd);
292 	iwm_mvm_power_log(sc, &cmd);
293 
294 	if ((ret = iwm_mvm_send_cmd_pdu(sc, IWM_MAC_PM_POWER_TABLE,
295 	    IWM_CMD_SYNC, sizeof(cmd), &cmd)) != 0)
296 		return ret;
297 
298 	ba_enable = !!(cmd.flags &
299 	    htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK));
300 	return iwm_mvm_update_beacon_abort(sc, in, ba_enable);
301 }
302 
303 int
304 iwm_mvm_power_update_device(struct iwm_softc *sc)
305 {
306 	struct iwm_device_power_cmd cmd = {
307 		.flags = htole16(IWM_DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK),
308 	};
309 
310 	if (!(sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_DEVICE_PS_CMD))
311 		return 0;
312 
313 	cmd.flags |= htole16(IWM_DEVICE_POWER_FLAGS_CAM_MSK);
314 	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
315 	    "Sending device power command with flags = 0x%X\n", cmd.flags);
316 
317 	return iwm_mvm_send_cmd_pdu(sc,
318 	    IWM_POWER_TABLE_CMD, IWM_CMD_SYNC, sizeof(cmd), &cmd);
319 }
320 
321 int
322 iwm_mvm_enable_beacon_filter(struct iwm_softc *sc, struct iwm_node *in)
323 {
324 	struct iwm_beacon_filter_cmd cmd = {
325 		IWM_BF_CMD_CONFIG_DEFAULTS,
326 		.bf_enable_beacon_filter = htole32(1),
327 	};
328 	int ret;
329 
330 	iwm_mvm_beacon_filter_set_cqm_params(sc, in, &cmd);
331 	ret = iwm_mvm_beacon_filter_send_cmd(sc, &cmd);
332 
333 	if (ret == 0)
334 		sc->sc_bf.bf_enabled = 1;
335 
336 	return ret;
337 }
338 
339 int
340 iwm_mvm_disable_beacon_filter(struct iwm_softc *sc)
341 {
342 	struct iwm_beacon_filter_cmd cmd;
343 	int ret;
344 
345 	memset(&cmd, 0, sizeof(cmd));
346 	if ((sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_BF_UPDATED) == 0)
347 		return 0;
348 
349 	ret = iwm_mvm_beacon_filter_send_cmd(sc, &cmd);
350 	if (ret == 0)
351 		sc->sc_bf.bf_enabled = 0;
352 
353 	return ret;
354 }
355 
356 #if 0
357 static int
358 iwm_mvm_update_beacon_filter(struct iwm_softc *sc, struct iwm_node *in)
359 {
360 	if (!sc->sc_bf.bf_enabled)
361 		return 0;
362 
363 	return iwm_mvm_enable_beacon_filter(sc, in);
364 }
365 #endif
366