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/param.h>
90 #include <sys/bus.h>
91 #include <sys/conf.h>
92 #include <sys/endian.h>
93 #include <sys/firmware.h>
94 #include <sys/kernel.h>
95 #include <sys/malloc.h>
96 #include <sys/mbuf.h>
97 #include <sys/rman.h>
98 #include <sys/sysctl.h>
99 #include <sys/linker.h>
100
101 #include <machine/endian.h>
102
103 #include <bus/pci/pcivar.h>
104 #include <bus/pci/pcireg.h>
105
106 #include <net/bpf.h>
107
108 #include <net/if.h>
109 #include <net/if_var.h>
110 #include <net/if_arp.h>
111 #include <net/if_dl.h>
112 #include <net/if_media.h>
113 #include <net/if_types.h>
114
115 #include <netinet/in.h>
116 #include <netinet/in_systm.h>
117 #include <netinet/if_ether.h>
118 #include <netinet/ip.h>
119
120 #include <netproto/802_11/ieee80211_var.h>
121 #include <netproto/802_11/ieee80211_regdomain.h>
122 #include <netproto/802_11/ieee80211_ratectl.h>
123 #include <netproto/802_11/ieee80211_radiotap.h>
124
125 #include "if_iwmreg.h"
126 #include "if_iwmvar.h"
127 #include "if_iwm_debug.h"
128 #include "if_iwm_constants.h"
129 #include "if_iwm_util.h"
130 #include "if_iwm_power.h"
131
132 static int iwm_power_scheme = IWM_POWER_SCHEME_BPS;
133
134 TUNABLE_INT("hw.iwm.power_scheme", &iwm_power_scheme);
135
136 /*
137 * BEGIN mvm/power.c
138 */
139
140 #define IWM_POWER_KEEP_ALIVE_PERIOD_SEC 25
141
142 static int
iwm_beacon_filter_send_cmd(struct iwm_softc * sc,struct iwm_beacon_filter_cmd * cmd)143 iwm_beacon_filter_send_cmd(struct iwm_softc *sc,
144 struct iwm_beacon_filter_cmd *cmd)
145 {
146 int ret;
147
148 ret = iwm_send_cmd_pdu(sc, IWM_REPLY_BEACON_FILTERING_CMD,
149 0, 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
iwm_beacon_filter_set_cqm_params(struct iwm_softc * sc,struct iwm_vap * ivp,struct iwm_beacon_filter_cmd * cmd)190 iwm_beacon_filter_set_cqm_params(struct iwm_softc *sc,
191 struct iwm_vap *ivp, struct iwm_beacon_filter_cmd *cmd)
192 {
193 cmd->ba_enable_beacon_abort = htole32(sc->sc_bf.ba_enabled);
194 }
195
196 static void
iwm_power_log(struct iwm_softc * sc,struct iwm_mac_power_cmd * cmd)197 iwm_power_log(struct iwm_softc *sc, struct iwm_mac_power_cmd *cmd)
198 {
199 IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
200 "Sending power table command on mac id 0x%X for "
201 "power level %d, flags = 0x%X\n",
202 cmd->id_and_color, iwm_power_scheme, le16toh(cmd->flags));
203 IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
204 "Keep alive = %u sec\n", le16toh(cmd->keep_alive_seconds));
205
206 if (!(cmd->flags & htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK))) {
207 IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
208 "Disable power management\n");
209 return;
210 }
211
212 IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
213 "Rx timeout = %u usec\n", le32toh(cmd->rx_data_timeout));
214 IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
215 "Tx timeout = %u usec\n", le32toh(cmd->tx_data_timeout));
216 if (cmd->flags & htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK))
217 IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
218 "DTIM periods to skip = %u\n", cmd->skip_dtim_periods);
219 }
220
221 static boolean_t
iwm_power_is_radar(struct iwm_softc * sc)222 iwm_power_is_radar(struct iwm_softc *sc)
223 {
224 struct ieee80211com *ic = &sc->sc_ic;
225 struct ieee80211_channel *chan;
226 boolean_t radar_detect = FALSE;
227
228 chan = ic->ic_bsschan;
229 if (chan == IEEE80211_CHAN_ANYC ||
230 (chan->ic_flags & IEEE80211_CHAN_DFS) != 0) {
231 radar_detect = TRUE;
232 }
233
234 return radar_detect;
235 }
236
237 static void
iwm_power_config_skip_dtim(struct iwm_softc * sc,struct iwm_mac_power_cmd * cmd)238 iwm_power_config_skip_dtim(struct iwm_softc *sc,
239 struct iwm_mac_power_cmd *cmd)
240 {
241 struct ieee80211com *ic = &sc->sc_ic;
242 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
243 int dtimper = vap->iv_dtim_period ?: 1;
244 int skip;
245
246 /* disable, in case we're supposed to override */
247 cmd->skip_dtim_periods = 0;
248 cmd->flags &= ~htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK);
249
250 if (iwm_power_is_radar(sc))
251 return;
252
253 if (dtimper >= 10)
254 return;
255
256 /* TODO: check that multicast wake lock is off */
257
258 if (iwm_power_scheme != IWM_POWER_SCHEME_LP)
259 return;
260 skip = 2;
261
262 /* the firmware really expects "look at every X DTIMs", so add 1 */
263 cmd->skip_dtim_periods = 1 + skip;
264 cmd->flags |= htole16(IWM_POWER_FLAGS_SKIP_OVER_DTIM_MSK);
265 }
266
267 static void
iwm_power_build_cmd(struct iwm_softc * sc,struct iwm_vap * ivp,struct iwm_mac_power_cmd * cmd)268 iwm_power_build_cmd(struct iwm_softc *sc, struct iwm_vap *ivp,
269 struct iwm_mac_power_cmd *cmd)
270 {
271 struct ieee80211vap *vap = &ivp->iv_vap;
272 struct ieee80211_node *ni = vap->iv_bss;
273 int dtimper, dtimper_msec;
274 int keep_alive;
275 boolean_t bss_conf_ps = FALSE;
276
277 cmd->id_and_color = htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id,
278 ivp->color));
279 dtimper = vap->iv_dtim_period ?: 1;
280
281 /*
282 * Regardless of power management state the driver must set
283 * keep alive period. FW will use it for sending keep alive NDPs
284 * immediately after association. Check that keep alive period
285 * is at least 3 * DTIM
286 */
287 dtimper_msec = dtimper * ni->ni_intval;
288 keep_alive
289 = imax(3 * dtimper_msec, 1000 * IWM_POWER_KEEP_ALIVE_PERIOD_SEC);
290 keep_alive = roundup(keep_alive, 1000) / 1000;
291 cmd->keep_alive_seconds = htole16(keep_alive);
292
293 if (sc->sc_ps_disabled)
294 return;
295
296 cmd->flags |= htole16(IWM_POWER_FLAGS_POWER_SAVE_ENA_MSK);
297
298 if (IWM_NODE(ni)->in_assoc &&
299 (vap->iv_flags & IEEE80211_F_PMGTON) != 0) {
300 bss_conf_ps = TRUE;
301 }
302 if (!bss_conf_ps)
303 return;
304
305 cmd->flags |= htole16(IWM_POWER_FLAGS_POWER_MANAGEMENT_ENA_MSK);
306
307 iwm_power_config_skip_dtim(sc, cmd);
308
309 cmd->rx_data_timeout =
310 htole32(IWM_DEFAULT_PS_RX_DATA_TIMEOUT);
311 cmd->tx_data_timeout =
312 htole32(IWM_DEFAULT_PS_TX_DATA_TIMEOUT);
313 }
314
315 static int
iwm_power_send_cmd(struct iwm_softc * sc,struct iwm_vap * ivp)316 iwm_power_send_cmd(struct iwm_softc *sc, struct iwm_vap *ivp)
317 {
318 struct iwm_mac_power_cmd cmd = {};
319
320 iwm_power_build_cmd(sc, ivp, &cmd);
321 iwm_power_log(sc, &cmd);
322
323 return iwm_send_cmd_pdu(sc, IWM_MAC_PM_POWER_TABLE, 0,
324 sizeof(cmd), &cmd);
325 }
326
327 static int
_iwm_enable_beacon_filter(struct iwm_softc * sc,struct iwm_vap * ivp,struct iwm_beacon_filter_cmd * cmd)328 _iwm_enable_beacon_filter(struct iwm_softc *sc, struct iwm_vap *ivp,
329 struct iwm_beacon_filter_cmd *cmd)
330 {
331 int ret;
332
333 iwm_beacon_filter_set_cqm_params(sc, ivp, cmd);
334 ret = iwm_beacon_filter_send_cmd(sc, cmd);
335
336 if (!ret)
337 sc->sc_bf.bf_enabled = 1;
338
339 return ret;
340 }
341
342 int
iwm_enable_beacon_filter(struct iwm_softc * sc,struct iwm_vap * ivp)343 iwm_enable_beacon_filter(struct iwm_softc *sc, struct iwm_vap *ivp)
344 {
345 struct iwm_beacon_filter_cmd cmd = {
346 IWM_BF_CMD_CONFIG_DEFAULTS,
347 .bf_enable_beacon_filter = htole32(1),
348 };
349
350 return _iwm_enable_beacon_filter(sc, ivp, &cmd);
351 }
352
353 int
iwm_disable_beacon_filter(struct iwm_softc * sc)354 iwm_disable_beacon_filter(struct iwm_softc *sc)
355 {
356 struct iwm_beacon_filter_cmd cmd = {};
357 int ret;
358
359 ret = iwm_beacon_filter_send_cmd(sc, &cmd);
360 if (ret == 0)
361 sc->sc_bf.bf_enabled = 0;
362
363 return ret;
364 }
365
366 static int
iwm_power_set_ps(struct iwm_softc * sc)367 iwm_power_set_ps(struct iwm_softc *sc)
368 {
369 struct ieee80211vap *vap;
370 boolean_t disable_ps;
371 int ret;
372
373 /* disable PS if CAM */
374 disable_ps = (iwm_power_scheme == IWM_POWER_SCHEME_CAM);
375 /* ...or if any of the vifs require PS to be off */
376 TAILQ_FOREACH(vap, &sc->sc_ic.ic_vaps, iv_next) {
377 struct iwm_vap *ivp = IWM_VAP(vap);
378 if (ivp->phy_ctxt != NULL && ivp->ps_disabled)
379 disable_ps = TRUE;
380 }
381
382 /* update device power state if it has changed */
383 if (sc->sc_ps_disabled != disable_ps) {
384 boolean_t old_ps_disabled = sc->sc_ps_disabled;
385
386 sc->sc_ps_disabled = disable_ps;
387 ret = iwm_power_update_device(sc);
388 if (ret) {
389 sc->sc_ps_disabled = old_ps_disabled;
390 return ret;
391 }
392 }
393
394 return 0;
395 }
396
397 static int
iwm_power_set_ba(struct iwm_softc * sc,struct iwm_vap * ivp)398 iwm_power_set_ba(struct iwm_softc *sc, struct iwm_vap *ivp)
399 {
400 struct iwm_beacon_filter_cmd cmd = {
401 IWM_BF_CMD_CONFIG_DEFAULTS,
402 .bf_enable_beacon_filter = htole32(1),
403 };
404 struct ieee80211vap *vap = &ivp->iv_vap;
405 struct ieee80211_node *ni = vap->iv_bss;
406 boolean_t bss_conf_ps = FALSE;
407
408 if (!sc->sc_bf.bf_enabled)
409 return 0;
410
411 if (ni != NULL && IWM_NODE(ni)->in_assoc &&
412 (vap->iv_flags & IEEE80211_F_PMGTON) != 0) {
413 bss_conf_ps = TRUE;
414 }
415 sc->sc_bf.ba_enabled = !sc->sc_ps_disabled && bss_conf_ps;
416
417 return _iwm_enable_beacon_filter(sc, ivp, &cmd);
418 }
419
420 int
iwm_power_update_ps(struct iwm_softc * sc)421 iwm_power_update_ps(struct iwm_softc *sc)
422 {
423 struct ieee80211vap *vap = TAILQ_FIRST(&sc->sc_ic.ic_vaps);
424 int ret;
425
426 ret = iwm_power_set_ps(sc);
427 if (ret)
428 return ret;
429
430 if (vap != NULL)
431 return iwm_power_set_ba(sc, IWM_VAP(vap));
432
433 return 0;
434 }
435
436 int
iwm_power_update_mac(struct iwm_softc * sc)437 iwm_power_update_mac(struct iwm_softc *sc)
438 {
439 struct ieee80211vap *vap = TAILQ_FIRST(&sc->sc_ic.ic_vaps);
440 int ret;
441
442 ret = iwm_power_set_ps(sc);
443 if (ret)
444 return ret;
445
446 if (vap != NULL) {
447 ret = iwm_power_send_cmd(sc, IWM_VAP(vap));
448 if (ret)
449 return ret;
450 }
451
452 if (vap != NULL)
453 return iwm_power_set_ba(sc, IWM_VAP(vap));
454
455 return 0;
456 }
457
458 int
iwm_power_update_device(struct iwm_softc * sc)459 iwm_power_update_device(struct iwm_softc *sc)
460 {
461 struct iwm_device_power_cmd cmd = {
462 .flags = 0,
463 };
464
465 if (iwm_power_scheme == IWM_POWER_SCHEME_CAM)
466 sc->sc_ps_disabled = TRUE;
467
468 if (!sc->sc_ps_disabled)
469 cmd.flags |= htole16(IWM_DEVICE_POWER_FLAGS_POWER_SAVE_ENA_MSK);
470
471 IWM_DPRINTF(sc, IWM_DEBUG_PWRSAVE | IWM_DEBUG_CMD,
472 "Sending device power command with flags = 0x%X\n", cmd.flags);
473
474 return iwm_send_cmd_pdu(sc,
475 IWM_POWER_TABLE_CMD, 0, sizeof(cmd), &cmd);
476 }
477