1d4886179SRui Paulo /* $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */ 2d4886179SRui Paulo 3d4886179SRui Paulo /* 4d4886179SRui Paulo * Copyright (c) 2014 genua mbh <info@genua.de> 5d4886179SRui Paulo * Copyright (c) 2014 Fixup Software Ltd. 6d4886179SRui Paulo * 7d4886179SRui Paulo * Permission to use, copy, modify, and distribute this software for any 8d4886179SRui Paulo * purpose with or without fee is hereby granted, provided that the above 9d4886179SRui Paulo * copyright notice and this permission notice appear in all copies. 10d4886179SRui Paulo * 11d4886179SRui Paulo * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12d4886179SRui Paulo * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13d4886179SRui Paulo * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14d4886179SRui Paulo * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15d4886179SRui Paulo * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16d4886179SRui Paulo * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17d4886179SRui Paulo * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18d4886179SRui Paulo */ 19d4886179SRui Paulo 20d4886179SRui Paulo /*- 21d4886179SRui Paulo * Based on BSD-licensed source modules in the Linux iwlwifi driver, 22d4886179SRui Paulo * which were used as the reference documentation for this implementation. 23d4886179SRui Paulo * 24d4886179SRui Paulo * Driver version we are currently based off of is 25d4886179SRui Paulo * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd) 26d4886179SRui Paulo * 27d4886179SRui Paulo *********************************************************************** 28d4886179SRui Paulo * 29d4886179SRui Paulo * This file is provided under a dual BSD/GPLv2 license. When using or 30d4886179SRui Paulo * redistributing this file, you may do so under either license. 31d4886179SRui Paulo * 32d4886179SRui Paulo * GPL LICENSE SUMMARY 33d4886179SRui Paulo * 34d4886179SRui Paulo * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved. 35d4886179SRui Paulo * 36d4886179SRui Paulo * This program is free software; you can redistribute it and/or modify 37d4886179SRui Paulo * it under the terms of version 2 of the GNU General Public License as 38d4886179SRui Paulo * published by the Free Software Foundation. 39d4886179SRui Paulo * 40d4886179SRui Paulo * This program is distributed in the hope that it will be useful, but 41d4886179SRui Paulo * WITHOUT ANY WARRANTY; without even the implied warranty of 42d4886179SRui Paulo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 43d4886179SRui Paulo * General Public License for more details. 44d4886179SRui Paulo * 45d4886179SRui Paulo * You should have received a copy of the GNU General Public License 46d4886179SRui Paulo * along with this program; if not, write to the Free Software 47d4886179SRui Paulo * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 48d4886179SRui Paulo * USA 49d4886179SRui Paulo * 50d4886179SRui Paulo * The full GNU General Public License is included in this distribution 51d4886179SRui Paulo * in the file called COPYING. 52d4886179SRui Paulo * 53d4886179SRui Paulo * Contact Information: 54d4886179SRui Paulo * Intel Linux Wireless <ilw@linux.intel.com> 55d4886179SRui Paulo * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 56d4886179SRui Paulo * 57d4886179SRui Paulo * 58d4886179SRui Paulo * BSD LICENSE 59d4886179SRui Paulo * 60d4886179SRui Paulo * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved. 61d4886179SRui Paulo * All rights reserved. 62d4886179SRui Paulo * 63d4886179SRui Paulo * Redistribution and use in source and binary forms, with or without 64d4886179SRui Paulo * modification, are permitted provided that the following conditions 65d4886179SRui Paulo * are met: 66d4886179SRui Paulo * 67d4886179SRui Paulo * * Redistributions of source code must retain the above copyright 68d4886179SRui Paulo * notice, this list of conditions and the following disclaimer. 69d4886179SRui Paulo * * Redistributions in binary form must reproduce the above copyright 70d4886179SRui Paulo * notice, this list of conditions and the following disclaimer in 71d4886179SRui Paulo * the documentation and/or other materials provided with the 72d4886179SRui Paulo * distribution. 73d4886179SRui Paulo * * Neither the name Intel Corporation nor the names of its 74d4886179SRui Paulo * contributors may be used to endorse or promote products derived 75d4886179SRui Paulo * from this software without specific prior written permission. 76d4886179SRui Paulo * 77d4886179SRui Paulo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 78d4886179SRui Paulo * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 79d4886179SRui Paulo * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 80d4886179SRui Paulo * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 81d4886179SRui Paulo * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 82d4886179SRui Paulo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 83d4886179SRui Paulo * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 84d4886179SRui Paulo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 85d4886179SRui Paulo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 86d4886179SRui Paulo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 87d4886179SRui Paulo * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 88d4886179SRui Paulo */ 89d4886179SRui Paulo 90d4886179SRui Paulo /*- 91d4886179SRui Paulo * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr> 92d4886179SRui Paulo * 93d4886179SRui Paulo * Permission to use, copy, modify, and distribute this software for any 94d4886179SRui Paulo * purpose with or without fee is hereby granted, provided that the above 95d4886179SRui Paulo * copyright notice and this permission notice appear in all copies. 96d4886179SRui Paulo * 97d4886179SRui Paulo * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 98d4886179SRui Paulo * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 99d4886179SRui Paulo * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 100d4886179SRui Paulo * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 101d4886179SRui Paulo * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 102d4886179SRui Paulo * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 103d4886179SRui Paulo * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 104d4886179SRui Paulo */ 105d4886179SRui Paulo #include <sys/cdefs.h> 106d4886179SRui Paulo __FBSDID("$FreeBSD$"); 107d4886179SRui Paulo 108*b789292fSAndriy Voskoboinyk #include "opt_wlan.h" 109*b789292fSAndriy Voskoboinyk 110d4886179SRui Paulo #include <sys/param.h> 111d4886179SRui Paulo #include <sys/bus.h> 112d4886179SRui Paulo #include <sys/conf.h> 113d4886179SRui Paulo #include <sys/endian.h> 114d4886179SRui Paulo #include <sys/firmware.h> 115d4886179SRui Paulo #include <sys/kernel.h> 116d4886179SRui Paulo #include <sys/malloc.h> 117d4886179SRui Paulo #include <sys/mbuf.h> 118d4886179SRui Paulo #include <sys/mutex.h> 119d4886179SRui Paulo #include <sys/module.h> 120d4886179SRui Paulo #include <sys/proc.h> 121d4886179SRui Paulo #include <sys/rman.h> 122d4886179SRui Paulo #include <sys/socket.h> 123d4886179SRui Paulo #include <sys/sockio.h> 124d4886179SRui Paulo #include <sys/sysctl.h> 125d4886179SRui Paulo #include <sys/linker.h> 126d4886179SRui Paulo 127d4886179SRui Paulo #include <machine/bus.h> 128d4886179SRui Paulo #include <machine/endian.h> 129d4886179SRui Paulo #include <machine/resource.h> 130d4886179SRui Paulo 131d4886179SRui Paulo #include <dev/pci/pcivar.h> 132d4886179SRui Paulo #include <dev/pci/pcireg.h> 133d4886179SRui Paulo 134d4886179SRui Paulo #include <net/bpf.h> 135d4886179SRui Paulo 136d4886179SRui Paulo #include <net/if.h> 137d4886179SRui Paulo #include <net/if_var.h> 138d4886179SRui Paulo #include <net/if_arp.h> 139d4886179SRui Paulo #include <net/if_dl.h> 140d4886179SRui Paulo #include <net/if_media.h> 141d4886179SRui Paulo #include <net/if_types.h> 142d4886179SRui Paulo 143d4886179SRui Paulo #include <netinet/in.h> 144d4886179SRui Paulo #include <netinet/in_systm.h> 145d4886179SRui Paulo #include <netinet/if_ether.h> 146d4886179SRui Paulo #include <netinet/ip.h> 147d4886179SRui Paulo 148d4886179SRui Paulo #include <net80211/ieee80211_var.h> 149d4886179SRui Paulo #include <net80211/ieee80211_regdomain.h> 150d4886179SRui Paulo #include <net80211/ieee80211_ratectl.h> 151d4886179SRui Paulo #include <net80211/ieee80211_radiotap.h> 152d4886179SRui Paulo 15349fdbf0aSRui Paulo #include <dev/iwm/if_iwmreg.h> 15449fdbf0aSRui Paulo #include <dev/iwm/if_iwmvar.h> 15549fdbf0aSRui Paulo #include <dev/iwm/if_iwm_debug.h> 15649fdbf0aSRui Paulo #include <dev/iwm/if_iwm_util.h> 15749fdbf0aSRui Paulo #include <dev/iwm/if_iwm_pcie_trans.h> 15849fdbf0aSRui Paulo #include <dev/iwm/if_iwm_time_event.h> 159d4886179SRui Paulo 160d4886179SRui Paulo /* 161d4886179SRui Paulo * For the high priority TE use a time event type that has similar priority to 162d4886179SRui Paulo * the FW's action scan priority. 163d4886179SRui Paulo */ 164d4886179SRui Paulo #define IWM_MVM_ROC_TE_TYPE_NORMAL IWM_TE_P2P_DEVICE_DISCOVERABLE 165d4886179SRui Paulo #define IWM_MVM_ROC_TE_TYPE_MGMT_TX IWM_TE_P2P_CLIENT_ASSOC 166d4886179SRui Paulo 167d4886179SRui Paulo /* used to convert from time event API v2 to v1 */ 168d4886179SRui Paulo #define IWM_TE_V2_DEP_POLICY_MSK (IWM_TE_V2_DEP_OTHER | IWM_TE_V2_DEP_TSF |\ 169d4886179SRui Paulo IWM_TE_V2_EVENT_SOCIOPATHIC) 170d4886179SRui Paulo static inline uint16_t 171d4886179SRui Paulo iwm_te_v2_get_notify(uint16_t policy) 172d4886179SRui Paulo { 173d4886179SRui Paulo return le16toh(policy) & IWM_TE_V2_NOTIF_MSK; 174d4886179SRui Paulo } 175d4886179SRui Paulo 176d4886179SRui Paulo static inline uint16_t 177d4886179SRui Paulo iwm_te_v2_get_dep_policy(uint16_t policy) 178d4886179SRui Paulo { 179d4886179SRui Paulo return (le16toh(policy) & IWM_TE_V2_DEP_POLICY_MSK) >> 180d4886179SRui Paulo IWM_TE_V2_PLACEMENT_POS; 181d4886179SRui Paulo } 182d4886179SRui Paulo 183d4886179SRui Paulo static inline uint16_t 184d4886179SRui Paulo iwm_te_v2_get_absence(uint16_t policy) 185d4886179SRui Paulo { 186d4886179SRui Paulo return (le16toh(policy) & IWM_TE_V2_ABSENCE) >> IWM_TE_V2_ABSENCE_POS; 187d4886179SRui Paulo } 188d4886179SRui Paulo 189d4886179SRui Paulo static void 190d4886179SRui Paulo iwm_mvm_te_v2_to_v1(const struct iwm_time_event_cmd_v2 *cmd_v2, 191d4886179SRui Paulo struct iwm_time_event_cmd_v1 *cmd_v1) 192d4886179SRui Paulo { 193d4886179SRui Paulo cmd_v1->id_and_color = cmd_v2->id_and_color; 194d4886179SRui Paulo cmd_v1->action = cmd_v2->action; 195d4886179SRui Paulo cmd_v1->id = cmd_v2->id; 196d4886179SRui Paulo cmd_v1->apply_time = cmd_v2->apply_time; 197d4886179SRui Paulo cmd_v1->max_delay = cmd_v2->max_delay; 198d4886179SRui Paulo cmd_v1->depends_on = cmd_v2->depends_on; 199d4886179SRui Paulo cmd_v1->interval = cmd_v2->interval; 200d4886179SRui Paulo cmd_v1->duration = cmd_v2->duration; 201d4886179SRui Paulo if (cmd_v2->repeat == IWM_TE_V2_REPEAT_ENDLESS) 202d4886179SRui Paulo cmd_v1->repeat = htole32(IWM_TE_V1_REPEAT_ENDLESS); 203d4886179SRui Paulo else 204d4886179SRui Paulo cmd_v1->repeat = htole32(cmd_v2->repeat); 205d4886179SRui Paulo cmd_v1->max_frags = htole32(cmd_v2->max_frags); 206d4886179SRui Paulo cmd_v1->interval_reciprocal = 0; /* unused */ 207d4886179SRui Paulo 208d4886179SRui Paulo cmd_v1->dep_policy = htole32(iwm_te_v2_get_dep_policy(cmd_v2->policy)); 209d4886179SRui Paulo cmd_v1->is_present = htole32(!iwm_te_v2_get_absence(cmd_v2->policy)); 210d4886179SRui Paulo cmd_v1->notify = htole32(iwm_te_v2_get_notify(cmd_v2->policy)); 211d4886179SRui Paulo } 212d4886179SRui Paulo 213d4886179SRui Paulo static int 214d4886179SRui Paulo iwm_mvm_send_time_event_cmd(struct iwm_softc *sc, 215d4886179SRui Paulo const struct iwm_time_event_cmd_v2 *cmd) 216d4886179SRui Paulo { 217d4886179SRui Paulo struct iwm_time_event_cmd_v1 cmd_v1; 218d4886179SRui Paulo 219d4886179SRui Paulo if (sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_TIME_EVENT_API_V2) 220d4886179SRui Paulo return iwm_mvm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, 221d4886179SRui Paulo IWM_CMD_SYNC, sizeof(*cmd), cmd); 222d4886179SRui Paulo 223d4886179SRui Paulo iwm_mvm_te_v2_to_v1(cmd, &cmd_v1); 224d4886179SRui Paulo return iwm_mvm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, IWM_CMD_SYNC, 225d4886179SRui Paulo sizeof(cmd_v1), &cmd_v1); 226d4886179SRui Paulo } 227d4886179SRui Paulo 228d4886179SRui Paulo static int 229d4886179SRui Paulo iwm_mvm_time_event_send_add(struct iwm_softc *sc, struct iwm_node *in, 230d4886179SRui Paulo void *te_data, struct iwm_time_event_cmd_v2 *te_cmd) 231d4886179SRui Paulo { 232d4886179SRui Paulo int ret; 233d4886179SRui Paulo 234d4886179SRui Paulo IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 235d4886179SRui Paulo "Add new TE, duration %d TU\n", le32toh(te_cmd->duration)); 236d4886179SRui Paulo 237d4886179SRui Paulo ret = iwm_mvm_send_time_event_cmd(sc, te_cmd); 238d4886179SRui Paulo if (ret) { 239d4886179SRui Paulo IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 240d4886179SRui Paulo "%s: Couldn't send IWM_TIME_EVENT_CMD: %d\n", 241d4886179SRui Paulo __func__, ret); 242d4886179SRui Paulo } 243d4886179SRui Paulo 244d4886179SRui Paulo return ret; 245d4886179SRui Paulo } 246d4886179SRui Paulo 247d4886179SRui Paulo void 248d4886179SRui Paulo iwm_mvm_protect_session(struct iwm_softc *sc, struct iwm_node *in, 2498d170e6aSAdrian Chadd uint32_t duration, uint32_t max_delay) 250d4886179SRui Paulo { 251d4886179SRui Paulo struct iwm_time_event_cmd_v2 time_cmd; 252d4886179SRui Paulo 253d4886179SRui Paulo memset(&time_cmd, 0, sizeof(time_cmd)); 254d4886179SRui Paulo 255d4886179SRui Paulo time_cmd.action = htole32(IWM_FW_CTXT_ACTION_ADD); 256d4886179SRui Paulo time_cmd.id_and_color = 257d4886179SRui Paulo htole32(IWM_FW_CMD_ID_AND_COLOR(IWM_DEFAULT_MACID, IWM_DEFAULT_COLOR)); 258d4886179SRui Paulo time_cmd.id = htole32(IWM_TE_BSS_STA_AGGRESSIVE_ASSOC); 259d4886179SRui Paulo 260d4886179SRui Paulo time_cmd.apply_time = htole32(iwm_read_prph(sc, 261d4886179SRui Paulo IWM_DEVICE_SYSTEM_TIME_REG)); 262d4886179SRui Paulo 263d4886179SRui Paulo time_cmd.max_frags = IWM_TE_V2_FRAG_NONE; 264d4886179SRui Paulo time_cmd.max_delay = htole32(max_delay); 265d4886179SRui Paulo /* TODO: why do we need to interval = bi if it is not periodic? */ 266d4886179SRui Paulo time_cmd.interval = htole32(1); 267d4886179SRui Paulo time_cmd.duration = htole32(duration); 268d4886179SRui Paulo time_cmd.repeat = 1; 269d4886179SRui Paulo time_cmd.policy 270d4886179SRui Paulo = htole32(IWM_TE_V2_NOTIF_HOST_EVENT_START | 271d4886179SRui Paulo IWM_TE_V2_NOTIF_HOST_EVENT_END); 272d4886179SRui Paulo 273d4886179SRui Paulo iwm_mvm_time_event_send_add(sc, in, /*te_data*/NULL, &time_cmd); 274d4886179SRui Paulo } 275