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 90 /*- 91 * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr> 92 * 93 * Permission to use, copy, modify, and distribute this software for any 94 * purpose with or without fee is hereby granted, provided that the above 95 * copyright notice and this permission notice appear in all copies. 96 * 97 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 98 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 99 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 100 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 101 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 102 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 103 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 104 */ 105 #include <sys/cdefs.h> 106 __FBSDID("$FreeBSD$"); 107 108 #include <sys/param.h> 109 #include <sys/bus.h> 110 #include <sys/endian.h> 111 #include <sys/firmware.h> 112 #include <sys/kernel.h> 113 #include <sys/malloc.h> 114 #include <sys/mbuf.h> 115 #include <sys/mutex.h> 116 #include <sys/module.h> 117 #include <sys/proc.h> 118 #include <sys/rman.h> 119 #include <sys/socket.h> 120 #include <sys/sockio.h> 121 #include <sys/sysctl.h> 122 #include <sys/linker.h> 123 124 #include <machine/endian.h> 125 126 #include <bus/pci/pcivar.h> 127 #include <bus/pci/pcireg.h> 128 129 #include <net/bpf.h> 130 131 #include <net/if.h> 132 #include <net/if_var.h> 133 #include <net/if_arp.h> 134 #include <net/if_dl.h> 135 #include <net/if_media.h> 136 #include <net/if_types.h> 137 138 #include <netinet/in.h> 139 #include <netinet/in_systm.h> 140 #include <netinet/if_ether.h> 141 #include <netinet/ip.h> 142 143 #include <netproto/802_11/ieee80211_var.h> 144 #include <netproto/802_11/ieee80211_regdomain.h> 145 #include <netproto/802_11/ieee80211_ratectl.h> 146 #include <netproto/802_11/ieee80211_radiotap.h> 147 148 #include "if_iwmreg.h" 149 #include "if_iwmvar.h" 150 #include "if_iwm_debug.h" 151 #include "if_iwm_util.h" 152 #include "if_iwm_pcie_trans.h" 153 #include "if_iwm_time_event.h" 154 155 /* 156 * For the high priority TE use a time event type that has similar priority to 157 * the FW's action scan priority. 158 */ 159 #define IWM_MVM_ROC_TE_TYPE_NORMAL IWM_TE_P2P_DEVICE_DISCOVERABLE 160 #define IWM_MVM_ROC_TE_TYPE_MGMT_TX IWM_TE_P2P_CLIENT_ASSOC 161 162 /* used to convert from time event API v2 to v1 */ 163 #define IWM_TE_V2_DEP_POLICY_MSK (IWM_TE_V2_DEP_OTHER | IWM_TE_V2_DEP_TSF |\ 164 IWM_TE_V2_EVENT_SOCIOPATHIC) 165 static inline uint16_t 166 iwm_te_v2_get_notify(uint16_t policy) 167 { 168 return le16toh(policy) & IWM_TE_V2_NOTIF_MSK; 169 } 170 171 static inline uint16_t 172 iwm_te_v2_get_dep_policy(uint16_t policy) 173 { 174 return (le16toh(policy) & IWM_TE_V2_DEP_POLICY_MSK) >> 175 IWM_TE_V2_PLACEMENT_POS; 176 } 177 178 static inline uint16_t 179 iwm_te_v2_get_absence(uint16_t policy) 180 { 181 return (le16toh(policy) & IWM_TE_V2_ABSENCE) >> IWM_TE_V2_ABSENCE_POS; 182 } 183 184 static void 185 iwm_mvm_te_v2_to_v1(const struct iwm_time_event_cmd_v2 *cmd_v2, 186 struct iwm_time_event_cmd_v1 *cmd_v1) 187 { 188 cmd_v1->id_and_color = cmd_v2->id_and_color; 189 cmd_v1->action = cmd_v2->action; 190 cmd_v1->id = cmd_v2->id; 191 cmd_v1->apply_time = cmd_v2->apply_time; 192 cmd_v1->max_delay = cmd_v2->max_delay; 193 cmd_v1->depends_on = cmd_v2->depends_on; 194 cmd_v1->interval = cmd_v2->interval; 195 cmd_v1->duration = cmd_v2->duration; 196 if (cmd_v2->repeat == IWM_TE_V2_REPEAT_ENDLESS) 197 cmd_v1->repeat = htole32(IWM_TE_V1_REPEAT_ENDLESS); 198 else 199 cmd_v1->repeat = htole32(cmd_v2->repeat); 200 cmd_v1->max_frags = htole32(cmd_v2->max_frags); 201 cmd_v1->interval_reciprocal = 0; /* unused */ 202 203 cmd_v1->dep_policy = htole32(iwm_te_v2_get_dep_policy(cmd_v2->policy)); 204 cmd_v1->is_present = htole32(!iwm_te_v2_get_absence(cmd_v2->policy)); 205 cmd_v1->notify = htole32(iwm_te_v2_get_notify(cmd_v2->policy)); 206 } 207 208 static int 209 iwm_mvm_send_time_event_cmd(struct iwm_softc *sc, 210 const struct iwm_time_event_cmd_v2 *cmd) 211 { 212 struct iwm_time_event_cmd_v1 cmd_v1; 213 214 if (sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_TIME_EVENT_API_V2) 215 return iwm_mvm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, 216 IWM_CMD_SYNC, sizeof(*cmd), cmd); 217 218 iwm_mvm_te_v2_to_v1(cmd, &cmd_v1); 219 return iwm_mvm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, IWM_CMD_SYNC, 220 sizeof(cmd_v1), &cmd_v1); 221 } 222 223 static int 224 iwm_mvm_time_event_send_add(struct iwm_softc *sc, struct iwm_node *in, 225 void *te_data, struct iwm_time_event_cmd_v2 *te_cmd) 226 { 227 int ret; 228 229 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 230 "Add new TE, duration %d TU\n", le32toh(te_cmd->duration)); 231 232 ret = iwm_mvm_send_time_event_cmd(sc, te_cmd); 233 if (ret) { 234 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 235 "%s: Couldn't send IWM_TIME_EVENT_CMD: %d\n", 236 __func__, ret); 237 } 238 239 return ret; 240 } 241 242 void 243 iwm_mvm_protect_session(struct iwm_softc *sc, struct iwm_node *in, 244 uint32_t duration, uint32_t max_delay) 245 { 246 struct iwm_time_event_cmd_v2 time_cmd; 247 248 memset(&time_cmd, 0, sizeof(time_cmd)); 249 250 time_cmd.action = htole32(IWM_FW_CTXT_ACTION_ADD); 251 time_cmd.id_and_color = 252 htole32(IWM_FW_CMD_ID_AND_COLOR(IWM_DEFAULT_MACID, IWM_DEFAULT_COLOR)); 253 time_cmd.id = htole32(IWM_TE_BSS_STA_AGGRESSIVE_ASSOC); 254 255 time_cmd.apply_time = htole32(0); 256 257 time_cmd.max_frags = IWM_TE_V2_FRAG_NONE; 258 time_cmd.max_delay = htole32(max_delay); 259 /* TODO: why do we need to interval = bi if it is not periodic? */ 260 time_cmd.interval = htole32(1); 261 time_cmd.duration = htole32(duration); 262 time_cmd.repeat = 1; 263 time_cmd.policy 264 = htole32(IWM_TE_V2_NOTIF_HOST_EVENT_START | 265 IWM_TE_V2_NOTIF_HOST_EVENT_END | 266 IWM_T2_V2_START_IMMEDIATELY); 267 268 iwm_mvm_time_event_send_add(sc, in, /*te_data*/NULL, &time_cmd); 269 } 270