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/param.h> 106 #include <sys/bus.h> 107 #include <sys/conf.h> 108 #include <sys/endian.h> 109 #include <sys/firmware.h> 110 #include <sys/kernel.h> 111 #include <sys/malloc.h> 112 #include <sys/mbuf.h> 113 #include <sys/rman.h> 114 #include <sys/sysctl.h> 115 #include <sys/linker.h> 116 117 #include <machine/endian.h> 118 119 #include <bus/pci/pcivar.h> 120 #include <bus/pci/pcireg.h> 121 122 #include <net/bpf.h> 123 124 #include <net/if.h> 125 #include <net/if_var.h> 126 #include <net/if_arp.h> 127 #include <net/if_dl.h> 128 #include <net/if_media.h> 129 #include <net/if_types.h> 130 131 #include <netinet/in.h> 132 #include <netinet/in_systm.h> 133 #include <netinet/if_ether.h> 134 #include <netinet/ip.h> 135 136 #include <netproto/802_11/ieee80211_var.h> 137 #include <netproto/802_11/ieee80211_regdomain.h> 138 #include <netproto/802_11/ieee80211_ratectl.h> 139 #include <netproto/802_11/ieee80211_radiotap.h> 140 141 #include "if_iwmreg.h" 142 #include "if_iwmvar.h" 143 #include "if_iwm_debug.h" 144 #include "if_iwm_util.h" 145 #include "if_iwm_notif_wait.h" 146 #include "if_iwm_pcie_trans.h" 147 #include "if_iwm_time_event.h" 148 149 #define TU_TO_HZ(tu) (((uint64_t)(tu) * 1024 * hz) / 1000000) 150 151 static void 152 iwm_te_clear_data(struct iwm_softc *sc) 153 { 154 sc->sc_time_event_uid = 0; 155 sc->sc_time_event_duration = 0; 156 sc->sc_time_event_end_ticks = 0; 157 sc->sc_flags &= ~IWM_FLAG_TE_ACTIVE; 158 } 159 160 /* 161 * Handles a FW notification for an event that is known to the driver. 162 * 163 * @mvm: the mvm component 164 * @te_data: the time event data 165 * @notif: the notification data corresponding the time event data. 166 */ 167 static void 168 iwm_te_handle_notif(struct iwm_softc *sc, 169 struct iwm_time_event_notif *notif) 170 { 171 IWM_DPRINTF(sc, IWM_DEBUG_TE, 172 "Handle time event notif - UID = 0x%x action %d\n", 173 le32toh(notif->unique_id), 174 le32toh(notif->action)); 175 176 if (!le32toh(notif->status)) { 177 const char *msg; 178 179 if (notif->action & htole32(IWM_TE_V2_NOTIF_HOST_EVENT_START)) 180 msg = "Time Event start notification failure"; 181 else 182 msg = "Time Event end notification failure"; 183 184 IWM_DPRINTF(sc, IWM_DEBUG_TE, "%s\n", msg); 185 } 186 187 if (le32toh(notif->action) & IWM_TE_V2_NOTIF_HOST_EVENT_END) { 188 IWM_DPRINTF(sc, IWM_DEBUG_TE, 189 "TE ended - current time %d, estimated end %d\n", 190 ticks, sc->sc_time_event_end_ticks); 191 192 iwm_te_clear_data(sc); 193 } else if (le32toh(notif->action) & IWM_TE_V2_NOTIF_HOST_EVENT_START) { 194 sc->sc_time_event_end_ticks = 195 ticks + TU_TO_HZ(sc->sc_time_event_duration); 196 } else { 197 device_printf(sc->sc_dev, "Got TE with unknown action\n"); 198 } 199 } 200 201 /* 202 * The Rx handler for time event notifications 203 */ 204 void 205 iwm_rx_time_event_notif(struct iwm_softc *sc, struct iwm_rx_packet *pkt) 206 { 207 struct iwm_time_event_notif *notif = (void *)pkt->data; 208 209 IWM_DPRINTF(sc, IWM_DEBUG_TE, 210 "Time event notification - UID = 0x%x action %d\n", 211 le32toh(notif->unique_id), 212 le32toh(notif->action)); 213 214 iwm_te_handle_notif(sc, notif); 215 } 216 217 static int 218 iwm_te_notif(struct iwm_softc *sc, struct iwm_rx_packet *pkt, 219 void *data) 220 { 221 struct iwm_time_event_notif *resp; 222 int resp_len = iwm_rx_packet_payload_len(pkt); 223 224 if (pkt->hdr.code != IWM_TIME_EVENT_NOTIFICATION || 225 resp_len != sizeof(*resp)) { 226 IWM_DPRINTF(sc, IWM_DEBUG_TE, 227 "Invalid TIME_EVENT_NOTIFICATION response\n"); 228 return 1; 229 } 230 231 resp = (void *)pkt->data; 232 233 /* te_data->uid is already set in the TIME_EVENT_CMD response */ 234 if (le32toh(resp->unique_id) != sc->sc_time_event_uid) 235 return false; 236 237 IWM_DPRINTF(sc, IWM_DEBUG_TE, 238 "TIME_EVENT_NOTIFICATION response - UID = 0x%x\n", 239 sc->sc_time_event_uid); 240 if (!resp->status) { 241 IWM_DPRINTF(sc, IWM_DEBUG_TE, 242 "TIME_EVENT_NOTIFICATION received but not executed\n"); 243 } 244 245 return 1; 246 } 247 248 static int 249 iwm_time_event_response(struct iwm_softc *sc, struct iwm_rx_packet *pkt, 250 void *data) 251 { 252 struct iwm_time_event_resp *resp; 253 int resp_len = iwm_rx_packet_payload_len(pkt); 254 255 if (pkt->hdr.code != IWM_TIME_EVENT_CMD || 256 resp_len != sizeof(*resp)) { 257 IWM_DPRINTF(sc, IWM_DEBUG_TE, 258 "Invalid TIME_EVENT_CMD response\n"); 259 return 1; 260 } 261 262 resp = (void *)pkt->data; 263 264 /* we should never get a response to another TIME_EVENT_CMD here */ 265 if (le32toh(resp->id) != IWM_TE_BSS_STA_AGGRESSIVE_ASSOC) { 266 IWM_DPRINTF(sc, IWM_DEBUG_TE, 267 "Got TIME_EVENT_CMD response with wrong id: %d\n", 268 le32toh(resp->id)); 269 return 0; 270 } 271 272 sc->sc_time_event_uid = le32toh(resp->unique_id); 273 IWM_DPRINTF(sc, IWM_DEBUG_TE, 274 "TIME_EVENT_CMD response - UID = 0x%x\n", sc->sc_time_event_uid); 275 return 1; 276 } 277 278 279 /* XXX Use the te_data function argument properly, like in iwlwifi's code. */ 280 281 static int 282 iwm_time_event_send_add(struct iwm_softc *sc, struct iwm_vap *ivp, 283 void *te_data, struct iwm_time_event_cmd *te_cmd) 284 { 285 static const uint16_t time_event_response[] = { IWM_TIME_EVENT_CMD }; 286 struct iwm_notification_wait wait_time_event; 287 int ret; 288 289 IWM_DPRINTF(sc, IWM_DEBUG_TE, 290 "Add new TE, duration %d TU\n", le32toh(te_cmd->duration)); 291 292 sc->sc_time_event_duration = le32toh(te_cmd->duration); 293 294 /* 295 * Use a notification wait, which really just processes the 296 * command response and doesn't wait for anything, in order 297 * to be able to process the response and get the UID inside 298 * the RX path. Using CMD_WANT_SKB doesn't work because it 299 * stores the buffer and then wakes up this thread, by which 300 * time another notification (that the time event started) 301 * might already be processed unsuccessfully. 302 */ 303 iwm_init_notification_wait(sc->sc_notif_wait, &wait_time_event, 304 time_event_response, 305 nitems(time_event_response), 306 iwm_time_event_response, /*te_data*/NULL); 307 308 ret = iwm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, 0, sizeof(*te_cmd), 309 te_cmd); 310 if (ret) { 311 IWM_DPRINTF(sc, IWM_DEBUG_TE, 312 "%s: Couldn't send IWM_TIME_EVENT_CMD: %d\n", 313 __func__, ret); 314 iwm_remove_notification(sc->sc_notif_wait, &wait_time_event); 315 return ret; 316 } 317 318 /* No need to wait for anything, so just pass 1 (0 isn't valid) */ 319 IWM_UNLOCK(sc); 320 ret = iwm_wait_notification(sc->sc_notif_wait, &wait_time_event, 1); 321 IWM_LOCK(sc); 322 /* should never fail */ 323 if (ret) { 324 IWM_DPRINTF(sc, IWM_DEBUG_TE, 325 "%s: Failed to get response for IWM_TIME_EVENT_CMD: %d\n", 326 __func__, ret); 327 } 328 329 return ret; 330 } 331 332 void 333 iwm_protect_session(struct iwm_softc *sc, struct iwm_vap *ivp, 334 uint32_t duration, uint32_t max_delay, boolean_t wait_for_notif) 335 { 336 const uint16_t te_notif_response[] = { IWM_TIME_EVENT_NOTIFICATION }; 337 struct iwm_notification_wait wait_te_notif; 338 struct iwm_time_event_cmd time_cmd = {}; 339 340 /* Do nothing if a time event is already scheduled. */ 341 if (sc->sc_flags & IWM_FLAG_TE_ACTIVE) 342 return; 343 344 time_cmd.action = htole32(IWM_FW_CTXT_ACTION_ADD); 345 time_cmd.id_and_color = 346 htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color)); 347 time_cmd.id = htole32(IWM_TE_BSS_STA_AGGRESSIVE_ASSOC); 348 349 time_cmd.apply_time = htole32(0); 350 351 time_cmd.max_frags = IWM_TE_V2_FRAG_NONE; 352 time_cmd.max_delay = htole32(max_delay); 353 /* TODO: why do we need to interval = bi if it is not periodic? */ 354 time_cmd.interval = htole32(1); 355 time_cmd.duration = htole32(duration); 356 time_cmd.repeat = 1; 357 time_cmd.policy 358 = htole16(IWM_TE_V2_NOTIF_HOST_EVENT_START | 359 IWM_TE_V2_NOTIF_HOST_EVENT_END | 360 IWM_T2_V2_START_IMMEDIATELY); 361 362 if (!wait_for_notif) { 363 iwm_time_event_send_add(sc, ivp, /*te_data*/NULL, &time_cmd); 364 DELAY(100); 365 sc->sc_flags |= IWM_FLAG_TE_ACTIVE; 366 return; 367 } 368 369 /* 370 * Create notification_wait for the TIME_EVENT_NOTIFICATION to use 371 * right after we send the time event 372 */ 373 iwm_init_notification_wait(sc->sc_notif_wait, &wait_te_notif, 374 te_notif_response, nitems(te_notif_response), 375 iwm_te_notif, /*te_data*/NULL); 376 377 /* If TE was sent OK - wait for the notification that started */ 378 if (iwm_time_event_send_add(sc, ivp, /*te_data*/NULL, &time_cmd)) { 379 IWM_DPRINTF(sc, IWM_DEBUG_TE, 380 "%s: Failed to add TE to protect session\n", __func__); 381 iwm_remove_notification(sc->sc_notif_wait, &wait_te_notif); 382 } else { 383 sc->sc_flags |= IWM_FLAG_TE_ACTIVE; 384 IWM_UNLOCK(sc); 385 if (iwm_wait_notification(sc->sc_notif_wait, &wait_te_notif, 386 TU_TO_HZ(max_delay))) { 387 IWM_DPRINTF(sc, IWM_DEBUG_TE, 388 "%s: Failed to protect session until TE\n", 389 __func__); 390 } 391 IWM_LOCK(sc); 392 } 393 } 394 395 void 396 iwm_stop_session_protection(struct iwm_softc *sc, struct iwm_vap *ivp) 397 { 398 struct iwm_time_event_cmd time_cmd = {}; 399 400 /* Do nothing if the time event has already ended. */ 401 if ((sc->sc_flags & IWM_FLAG_TE_ACTIVE) == 0) 402 return; 403 404 time_cmd.id = htole32(sc->sc_time_event_uid); 405 time_cmd.action = htole32(IWM_FW_CTXT_ACTION_REMOVE); 406 time_cmd.id_and_color = 407 htole32(IWM_FW_CMD_ID_AND_COLOR(ivp->id, ivp->color)); 408 409 IWM_DPRINTF(sc, IWM_DEBUG_TE, 410 "%s: Removing TE 0x%x\n", __func__, le32toh(time_cmd.id)); 411 if (iwm_send_cmd_pdu(sc, IWM_TIME_EVENT_CMD, 0, sizeof(time_cmd), 412 &time_cmd) == 0) 413 iwm_te_clear_data(sc); 414 415 DELAY(100); 416 } 417