if_iwm_power.c revision 300248
156893Sfenner/*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
2127668Sbms
3127668Sbms/*
456893Sfenner * Copyright (c) 2014 genua mbh <info@genua.de>
556893Sfenner * Copyright (c) 2014 Fixup Software Ltd.
656893Sfenner *
756893Sfenner * Permission to use, copy, modify, and distribute this software for any
856893Sfenner * purpose with or without fee is hereby granted, provided that the above
956893Sfenner * copyright notice and this permission notice appear in all copies.
1056893Sfenner *
1156893Sfenner * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1256893Sfenner * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1356893Sfenner * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1456893Sfenner * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15127668Sbms * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1656893Sfenner * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1756893Sfenner * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1856893Sfenner */
1956893Sfenner
20127668Sbms/*-
2156893Sfenner * Based on BSD-licensed source modules in the Linux iwlwifi driver,
2256893Sfenner * which were used as the reference documentation for this implementation.
2356893Sfenner *
24127668Sbms * Driver version we are currently based off of is
25190207Srpaulo * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
2656893Sfenner *
2756893Sfenner ***********************************************************************
2856893Sfenner *
2956893Sfenner * This file is provided under a dual BSD/GPLv2 license.  When using or
3056893Sfenner * redistributing this file, you may do so under either license.
3156893Sfenner *
3298524Sfenner * GPL LICENSE SUMMARY
3398524Sfenner *
34127668Sbms * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
3556893Sfenner *
3656893Sfenner * This program is free software; you can redistribute it and/or modify
3756893Sfenner * it under the terms of version 2 of the GNU General Public License as
3856893Sfenner * published by the Free Software Foundation.
3956893Sfenner *
4056893Sfenner * This program is distributed in the hope that it will be useful, but
4175115Sfenner * WITHOUT ANY WARRANTY; without even the implied warranty of
4275115Sfenner * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4356893Sfenner * General Public License for more details.
44127668Sbms *
4556893Sfenner * You should have received a copy of the GNU General Public License
4656893Sfenner * along with this program; if not, write to the Free Software
4775115Sfenner * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
4875115Sfenner * USA
4975115Sfenner *
5075115Sfenner * The full GNU General Public License is included in this distribution
5175115Sfenner * in the file called COPYING.
5275115Sfenner *
5356893Sfenner * Contact Information:
5456893Sfenner *  Intel Linux Wireless <ilw@linux.intel.com>
55127668Sbms * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
5656893Sfenner *
5798524Sfenner *
5898524Sfenner * BSD LICENSE
5998524Sfenner *
6098524Sfenner * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
6156893Sfenner * All rights reserved.
6256893Sfenner *
6356893Sfenner * Redistribution and use in source and binary forms, with or without
64127668Sbms * modification, are permitted provided that the following conditions
65127668Sbms * are met:
66146773Ssam *
6756893Sfenner *  * Redistributions of source code must retain the above copyright
6856893Sfenner *    notice, this list of conditions and the following disclaimer.
69127668Sbms *  * Redistributions in binary form must reproduce the above copyright
70127668Sbms *    notice, this list of conditions and the following disclaimer in
7156893Sfenner *    the documentation and/or other materials provided with the
7298524Sfenner *    distribution.
7398524Sfenner *  * Neither the name Intel Corporation nor the names of its
7456893Sfenner *    contributors may be used to endorse or promote products derived
7556893Sfenner *    from this software without specific prior written permission.
7656893Sfenner *
7756893Sfenner * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78127668Sbms * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7956893Sfenner * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8056893Sfenner * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8156893Sfenner * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82127668Sbms * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8356893Sfenner * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8498524Sfenner * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8598524Sfenner * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8698524Sfenner * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8798524Sfenner * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8875115Sfenner */
8998524Sfenner#include <sys/cdefs.h>
9056893Sfenner__FBSDID("$FreeBSD: head/sys/dev/iwm/if_iwm_power.c 300248 2016-05-19 23:31:00Z avos $");
9156893Sfenner
92127668Sbms#include "opt_wlan.h"
9356893Sfenner
9456893Sfenner#include <sys/param.h>
9556893Sfenner#include <sys/bus.h>
9656893Sfenner#include <sys/conf.h>
97162017Ssam#include <sys/endian.h>
9856893Sfenner#include <sys/firmware.h>
9956893Sfenner#include <sys/kernel.h>
10098524Sfenner#include <sys/malloc.h>
10198524Sfenner#include <sys/mbuf.h>
10298524Sfenner#include <sys/mutex.h>
10398524Sfenner#include <sys/module.h>
104146773Ssam#include <sys/proc.h>
10556893Sfenner#include <sys/rman.h>
10698524Sfenner#include <sys/socket.h>
107127668Sbms#include <sys/sockio.h>
10856893Sfenner#include <sys/sysctl.h>
109146773Ssam#include <sys/linker.h>
110146773Ssam
111146773Ssam#include <machine/bus.h>
112146773Ssam#include <machine/endian.h>
113146773Ssam#include <machine/resource.h>
114146773Ssam
115146773Ssam#include <dev/pci/pcivar.h>
116146773Ssam#include <dev/pci/pcireg.h>
117
118#include <net/bpf.h>
119
120#include <net/if.h>
121#include <net/if_var.h>
122#include <net/if_arp.h>
123#include <net/if_dl.h>
124#include <net/if_media.h>
125#include <net/if_types.h>
126
127#include <netinet/in.h>
128#include <netinet/in_systm.h>
129#include <netinet/if_ether.h>
130#include <netinet/ip.h>
131
132#include <net80211/ieee80211_var.h>
133#include <net80211/ieee80211_regdomain.h>
134#include <net80211/ieee80211_ratectl.h>
135#include <net80211/ieee80211_radiotap.h>
136
137#include <dev/iwm/if_iwmreg.h>
138#include <dev/iwm/if_iwmvar.h>
139#include <dev/iwm/if_iwm_debug.h>
140#include <dev/iwm/if_iwm_util.h>
141#include <dev/iwm/if_iwm_power.h>
142
143/*
144 * BEGIN mvm/power.c
145 */
146
147#define IWM_POWER_KEEP_ALIVE_PERIOD_SEC    25
148
149static int
150iwm_mvm_beacon_filter_send_cmd(struct iwm_softc *sc,
151	struct iwm_beacon_filter_cmd *cmd)
152{
153	int ret;
154
155	ret = iwm_mvm_send_cmd_pdu(sc, IWM_REPLY_BEACON_FILTERING_CMD,
156	    IWM_CMD_SYNC, sizeof(struct iwm_beacon_filter_cmd), cmd);
157
158	if (!ret) {
159		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
160		    "ba_enable_beacon_abort is: %d\n",
161		    le32toh(cmd->ba_enable_beacon_abort));
162		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
163		    "ba_escape_timer is: %d\n",
164		    le32toh(cmd->ba_escape_timer));
165		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
166		    "bf_debug_flag is: %d\n",
167		    le32toh(cmd->bf_debug_flag));
168		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
169		    "bf_enable_beacon_filter is: %d\n",
170		    le32toh(cmd->bf_enable_beacon_filter));
171		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
172		    "bf_energy_delta is: %d\n",
173		    le32toh(cmd->bf_energy_delta));
174		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
175		    "bf_escape_timer is: %d\n",
176		    le32toh(cmd->bf_escape_timer));
177		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
178		    "bf_roaming_energy_delta is: %d\n",
179		    le32toh(cmd->bf_roaming_energy_delta));
180		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
181		    "bf_roaming_state is: %d\n",
182		    le32toh(cmd->bf_roaming_state));
183		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
184		    "bf_temp_threshold is: %d\n",
185		    le32toh(cmd->bf_temp_threshold));
186		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
187		    "bf_temp_fast_filter is: %d\n",
188		    le32toh(cmd->bf_temp_fast_filter));
189		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
190		    "bf_temp_slow_filter is: %d\n",
191		    le32toh(cmd->bf_temp_slow_filter));
192	}
193	return ret;
194}
195
196static void
197iwm_mvm_beacon_filter_set_cqm_params(struct iwm_softc *sc,
198	struct iwm_node *in, struct iwm_beacon_filter_cmd *cmd)
199{
200	cmd->ba_enable_beacon_abort = htole32(sc->sc_bf.ba_enabled);
201}
202
203static int
204iwm_mvm_update_beacon_abort(struct iwm_softc *sc, struct iwm_node *in,
205	int enable)
206{
207	struct iwm_beacon_filter_cmd cmd = {
208		IWM_BF_CMD_CONFIG_DEFAULTS,
209		.bf_enable_beacon_filter = htole32(1),
210		.ba_enable_beacon_abort = htole32(enable),
211	};
212
213	if (!sc->sc_bf.bf_enabled)
214		return 0;
215
216	sc->sc_bf.ba_enabled = enable;
217	iwm_mvm_beacon_filter_set_cqm_params(sc, in, &cmd);
218	return iwm_mvm_beacon_filter_send_cmd(sc, &cmd);
219}
220
221static void
222iwm_mvm_power_log(struct iwm_softc *sc, struct iwm_mac_power_cmd *cmd)
223{
224	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
225	    "Sending power table command on mac id 0x%X for "
226	    "power level %d, flags = 0x%X\n",
227	    cmd->id_and_color, IWM_POWER_SCHEME_CAM, le16toh(cmd->flags));
228	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
229	    "Keep alive = %u sec\n", le16toh(cmd->keep_alive_seconds));
230
231	if (!(cmd->flags & htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
232		IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
233		    "Disable power management\n");
234		return;
235	}
236	KASSERT(0, ("unhandled power management"));
237
238#if 0
239	DPRINTF(mvm, "Rx timeout = %u usec\n",
240			le32_to_cpu(cmd->rx_data_timeout));
241	DPRINTF(mvm, "Tx timeout = %u usec\n",
242			le32_to_cpu(cmd->tx_data_timeout));
243	if (cmd->flags & cpu_to_le16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK))
244		DPRINTF(mvm, "DTIM periods to skip = %u\n",
245				cmd->skip_dtim_periods);
246	if (cmd->flags & cpu_to_le16(IWM_POWER_FLAGS_LPRX_ENA_MSK))
247		DPRINTF(mvm, "LP RX RSSI threshold = %u\n",
248				cmd->lprx_rssi_threshold);
249	if (cmd->flags & cpu_to_le16(IWM_POWER_FLAGS_ADVANCE_PM_ENA_MSK)) {
250		DPRINTF(mvm, "uAPSD enabled\n");
251		DPRINTF(mvm, "Rx timeout (uAPSD) = %u usec\n",
252				le32_to_cpu(cmd->rx_data_timeout_uapsd));
253		DPRINTF(mvm, "Tx timeout (uAPSD) = %u usec\n",
254				le32_to_cpu(cmd->tx_data_timeout_uapsd));
255		DPRINTF(mvm, "QNDP TID = %d\n", cmd->qndp_tid);
256		DPRINTF(mvm, "ACs flags = 0x%x\n", cmd->uapsd_ac_flags);
257		DPRINTF(mvm, "Max SP = %d\n", cmd->uapsd_max_sp);
258	}
259#endif
260}
261
262static void
263iwm_mvm_power_build_cmd(struct iwm_softc *sc, struct iwm_node *in,
264	struct iwm_mac_power_cmd *cmd)
265{
266	struct ieee80211_node *ni = &in->in_ni;
267	int dtimper, dtimper_msec;
268	int keep_alive;
269	struct ieee80211com *ic = &sc->sc_ic;
270	struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
271
272	cmd->id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(IWM_DEFAULT_MACID,
273	    IWM_DEFAULT_COLOR));
274	dtimper = vap->iv_dtim_period ?: 1;
275
276	/*
277	 * Regardless of power management state the driver must set
278	 * keep alive period. FW will use it for sending keep alive NDPs
279	 * immediately after association. Check that keep alive period
280	 * is at least 3 * DTIM
281	 */
282	dtimper_msec = dtimper * ni->ni_intval;
283	keep_alive
284	    = MAX(3 * dtimper_msec, 1000 * IWM_POWER_KEEP_ALIVE_PERIOD_SEC);
285	keep_alive = roundup(keep_alive, 1000) / 1000;
286	cmd->keep_alive_seconds = htole16(keep_alive);
287}
288
289int
290iwm_mvm_power_mac_update_mode(struct iwm_softc *sc, struct iwm_node *in)
291{
292	int ret;
293	int ba_enable;
294	struct iwm_mac_power_cmd cmd;
295
296	memset(&cmd, 0, sizeof(cmd));
297
298	iwm_mvm_power_build_cmd(sc, in, &cmd);
299	iwm_mvm_power_log(sc, &cmd);
300
301	if ((ret = iwm_mvm_send_cmd_pdu(sc, IWM_MAC_PM_POWER_TABLE,
302	    IWM_CMD_SYNC, sizeof(cmd), &cmd)) != 0)
303		return ret;
304
305	ba_enable = !!(cmd.flags &
306	    htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK));
307	return iwm_mvm_update_beacon_abort(sc, in, ba_enable);
308}
309
310int
311iwm_mvm_power_update_device(struct iwm_softc *sc)
312{
313	struct iwm_device_power_cmd cmd = {
314		.flags = htole16(IWM_DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK),
315	};
316
317	if (!(sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_DEVICE_PS_CMD))
318		return 0;
319
320	cmd.flags |= htole16(IWM_DEVICE_POWER_FLAGS_CAM_MSK);
321	IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
322	    "Sending device power command with flags = 0x%X\n", cmd.flags);
323
324	return iwm_mvm_send_cmd_pdu(sc,
325	    IWM_POWER_TABLE_CMD, IWM_CMD_SYNC, sizeof(cmd), &cmd);
326}
327
328int
329iwm_mvm_enable_beacon_filter(struct iwm_softc *sc, struct iwm_node *in)
330{
331	struct iwm_beacon_filter_cmd cmd = {
332		IWM_BF_CMD_CONFIG_DEFAULTS,
333		.bf_enable_beacon_filter = htole32(1),
334	};
335	int ret;
336
337	iwm_mvm_beacon_filter_set_cqm_params(sc, in, &cmd);
338	ret = iwm_mvm_beacon_filter_send_cmd(sc, &cmd);
339
340	if (ret == 0)
341		sc->sc_bf.bf_enabled = 1;
342
343	return ret;
344}
345
346int
347iwm_mvm_disable_beacon_filter(struct iwm_softc *sc)
348{
349	struct iwm_beacon_filter_cmd cmd;
350	int ret;
351
352	memset(&cmd, 0, sizeof(cmd));
353	if ((sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_BF_UPDATED) == 0)
354		return 0;
355
356	ret = iwm_mvm_beacon_filter_send_cmd(sc, &cmd);
357	if (ret == 0)
358		sc->sc_bf.bf_enabled = 0;
359
360	return ret;
361}
362
363#if 0
364static int
365iwm_mvm_update_beacon_filter(struct iwm_softc *sc, struct iwm_node *in)
366{
367	if (!sc->sc_bf.bf_enabled)
368		return 0;
369
370	return iwm_mvm_enable_beacon_filter(sc, in);
371}
372#endif
373