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/ethernet.h> 135 #include <net/if_dl.h> 136 #include <net/if_media.h> 137 #include <net/if_types.h> 138 #include <net/ifq_var.h> 139 140 #include <netinet/in.h> 141 #include <netinet/in_systm.h> 142 #include <netinet/if_ether.h> 143 #include <netinet/ip.h> 144 145 #include <netproto/802_11/ieee80211_var.h> 146 #include <netproto/802_11/ieee80211_regdomain.h> 147 #include <netproto/802_11/ieee80211_ratectl.h> 148 #include <netproto/802_11/ieee80211_radiotap.h> 149 150 #include "if_iwmreg.h" 151 #include "if_iwmvar.h" 152 #include "if_iwm_debug.h" 153 #include "if_iwm_util.h" 154 #include "if_iwm_scan.h" 155 156 #define IWM_PLCP_QUIET_THRESH 1 157 #define IWM_ACTIVE_QUIET_TIME 10 158 #define LONG_OUT_TIME_PERIOD (600 * IEEE80211_DUR_TU) 159 #define SHORT_OUT_TIME_PERIOD (200 * IEEE80211_DUR_TU) 160 #define SUSPEND_TIME_PERIOD (100 * IEEE80211_DUR_TU) 161 162 static uint16_t 163 iwm_mvm_scan_rx_chain(struct iwm_softc *sc) 164 { 165 uint16_t rx_chain; 166 uint8_t rx_ant; 167 168 rx_ant = IWM_FW_VALID_RX_ANT(sc); 169 rx_chain = rx_ant << IWM_PHY_RX_CHAIN_VALID_POS; 170 rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_MIMO_SEL_POS; 171 rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_SEL_POS; 172 rx_chain |= 0x1 << IWM_PHY_RX_CHAIN_DRIVER_FORCE_POS; 173 return htole16(rx_chain); 174 } 175 176 static uint32_t 177 iwm_mvm_scan_max_out_time(struct iwm_softc *sc, uint32_t flags, int is_assoc) 178 { 179 if (!is_assoc) 180 return 0; 181 if (flags & 0x1) 182 return htole32(SHORT_OUT_TIME_PERIOD); 183 return htole32(LONG_OUT_TIME_PERIOD); 184 } 185 186 static uint32_t 187 iwm_mvm_scan_suspend_time(struct iwm_softc *sc, int is_assoc) 188 { 189 if (!is_assoc) 190 return 0; 191 return htole32(SUSPEND_TIME_PERIOD); 192 } 193 194 static uint32_t 195 iwm_mvm_scan_rxon_flags(struct iwm_softc *sc, int flags) 196 { 197 if (flags & IEEE80211_CHAN_2GHZ) 198 return htole32(IWM_PHY_BAND_24); 199 else 200 return htole32(IWM_PHY_BAND_5); 201 } 202 203 static uint32_t 204 iwm_mvm_scan_rate_n_flags(struct iwm_softc *sc, int flags, int no_cck) 205 { 206 uint32_t tx_ant; 207 int i, ind; 208 209 for (i = 0, ind = sc->sc_scan_last_antenna; 210 i < IWM_RATE_MCS_ANT_NUM; i++) { 211 ind = (ind + 1) % IWM_RATE_MCS_ANT_NUM; 212 if (IWM_FW_VALID_TX_ANT(sc) & (1 << ind)) { 213 sc->sc_scan_last_antenna = ind; 214 break; 215 } 216 } 217 tx_ant = (1 << sc->sc_scan_last_antenna) << IWM_RATE_MCS_ANT_POS; 218 219 if ((flags & IEEE80211_CHAN_2GHZ) && !no_cck) 220 return htole32(IWM_RATE_1M_PLCP | IWM_RATE_MCS_CCK_MSK | 221 tx_ant); 222 else 223 return htole32(IWM_RATE_6M_PLCP | tx_ant); 224 } 225 226 /* 227 * If req->n_ssids > 0, it means we should do an active scan. 228 * In case of active scan w/o directed scan, we receive a zero-length SSID 229 * just to notify that this scan is active and not passive. 230 * In order to notify the FW of the number of SSIDs we wish to scan (including 231 * the zero-length one), we need to set the corresponding bits in chan->type, 232 * one for each SSID, and set the active bit (first). If the first SSID is 233 * already included in the probe template, so we need to set only 234 * req->n_ssids - 1 bits in addition to the first bit. 235 */ 236 static uint16_t 237 iwm_mvm_get_active_dwell(struct iwm_softc *sc, int flags, int n_ssids) 238 { 239 if (flags & IEEE80211_CHAN_2GHZ) 240 return 30 + 3 * (n_ssids + 1); 241 return 20 + 2 * (n_ssids + 1); 242 } 243 244 static uint16_t 245 iwm_mvm_get_passive_dwell(struct iwm_softc *sc, int flags) 246 { 247 return (flags & IEEE80211_CHAN_2GHZ) ? 100 + 20 : 100 + 10; 248 } 249 250 static int 251 iwm_mvm_scan_fill_channels(struct iwm_softc *sc, struct iwm_scan_cmd *cmd, 252 int flags, int n_ssids, int basic_ssid) 253 { 254 struct ieee80211com *ic = sc->sc_ic; 255 uint16_t passive_dwell = iwm_mvm_get_passive_dwell(sc, flags); 256 uint16_t active_dwell = iwm_mvm_get_active_dwell(sc, flags, n_ssids); 257 struct iwm_scan_channel *chan = (struct iwm_scan_channel *) 258 (cmd->data + le16toh(cmd->tx_cmd.len)); 259 int type = (1 << n_ssids) - 1; 260 struct ieee80211_channel *c; 261 int nchan, j; 262 263 if (!basic_ssid) 264 type |= (1 << n_ssids); 265 266 for (nchan = j = 0; j < ic->ic_nchans; j++) { 267 c = &ic->ic_channels[j]; 268 /* For 2GHz, only populate 11b channels */ 269 /* For 5GHz, only populate 11a channels */ 270 /* 271 * Catch other channels, in case we have 900MHz channels or 272 * something in the chanlist. 273 */ 274 if ((flags & IEEE80211_CHAN_2GHZ) && (! IEEE80211_IS_CHAN_B(c))) { 275 continue; 276 } else if ((flags & IEEE80211_CHAN_5GHZ) && (! IEEE80211_IS_CHAN_A(c))) { 277 continue; 278 } else { 279 IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM, 280 "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n", 281 __func__, 282 c->ic_freq, 283 c->ic_ieee, 284 c->ic_flags); 285 } 286 IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM, 287 "Adding channel %d (%d Mhz) to the list\n", 288 nchan, c->ic_freq); 289 chan->channel = htole16(ieee80211_mhz2ieee(c->ic_freq, flags)); 290 chan->type = htole32(type); 291 if (c->ic_flags & IEEE80211_CHAN_PASSIVE) 292 chan->type &= htole32(~IWM_SCAN_CHANNEL_TYPE_ACTIVE); 293 chan->active_dwell = htole16(active_dwell); 294 chan->passive_dwell = htole16(passive_dwell); 295 chan->iteration_count = htole16(1); 296 chan++; 297 nchan++; 298 } 299 if (nchan == 0) 300 device_printf(sc->sc_dev, 301 "%s: NO CHANNEL!\n", __func__); 302 return nchan; 303 } 304 305 /* 306 * Fill in probe request with the following parameters: 307 * TA is our vif HW address, which mac80211 ensures we have. 308 * Packet is broadcasted, so this is both SA and DA. 309 * The probe request IE is made out of two: first comes the most prioritized 310 * SSID if a directed scan is requested. Second comes whatever extra 311 * information was given to us as the scan request IE. 312 */ 313 static uint16_t 314 iwm_mvm_fill_probe_req(struct iwm_softc *sc, struct ieee80211_frame *frame, 315 const uint8_t *ta, int n_ssids, const uint8_t *ssid, int ssid_len, 316 const uint8_t *ie, int ie_len, int left) 317 { 318 uint8_t *pos = NULL; 319 320 /* Make sure there is enough space for the probe request, 321 * two mandatory IEs and the data */ 322 left -= sizeof(*frame); 323 if (left < 0) 324 return 0; 325 326 frame->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 327 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 328 frame->i_fc[1] = IEEE80211_FC1_DIR_NODS; 329 IEEE80211_ADDR_COPY(frame->i_addr1, ieee80211broadcastaddr); 330 IEEE80211_ADDR_COPY(frame->i_addr2, ta); 331 IEEE80211_ADDR_COPY(frame->i_addr3, ieee80211broadcastaddr); 332 333 /* for passive scans, no need to fill anything */ 334 if (n_ssids == 0) 335 return sizeof(*frame); 336 337 /* points to the payload of the request */ 338 pos = (uint8_t *)frame + sizeof(*frame); 339 340 /* fill in our SSID IE */ 341 left -= ssid_len + 2; 342 if (left < 0) 343 return 0; 344 345 pos = ieee80211_add_ssid(pos, ssid, ssid_len); 346 347 if (ie && ie_len && left >= ie_len) { 348 memcpy(pos, ie, ie_len); 349 pos += ie_len; 350 } 351 352 return pos - (uint8_t *)frame; 353 } 354 355 int 356 iwm_mvm_scan_request(struct iwm_softc *sc, int flags, 357 int n_ssids, uint8_t *ssid, int ssid_len) 358 { 359 struct iwm_host_cmd hcmd = { 360 .id = IWM_SCAN_REQUEST_CMD, 361 .len = { 0, }, 362 .data = { sc->sc_scan_cmd, }, 363 .flags = IWM_CMD_SYNC, 364 .dataflags = { IWM_HCMD_DFL_NOCOPY, }, 365 }; 366 struct iwm_scan_cmd *cmd = sc->sc_scan_cmd; 367 int is_assoc = 0; 368 int ret; 369 uint32_t status; 370 int basic_ssid = 371 !(sc->sc_capaflags & IWM_UCODE_TLV_FLAGS_NO_BASIC_SSID); 372 373 sc->sc_scanband = flags & (IEEE80211_CHAN_2GHZ | IEEE80211_CHAN_5GHZ); 374 375 IWM_DPRINTF(sc, IWM_DEBUG_SCAN, 376 "Handling ieee80211 scan request\n"); 377 memset(cmd, 0, sc->sc_scan_cmd_len); 378 379 cmd->quiet_time = htole16(IWM_ACTIVE_QUIET_TIME); 380 cmd->quiet_plcp_th = htole16(IWM_PLCP_QUIET_THRESH); 381 cmd->rxchain_sel_flags = iwm_mvm_scan_rx_chain(sc); 382 cmd->max_out_time = iwm_mvm_scan_max_out_time(sc, 0, is_assoc); 383 cmd->suspend_time = iwm_mvm_scan_suspend_time(sc, is_assoc); 384 cmd->rxon_flags = iwm_mvm_scan_rxon_flags(sc, flags); 385 cmd->filter_flags = htole32(IWM_MAC_FILTER_ACCEPT_GRP | 386 IWM_MAC_FILTER_IN_BEACON); 387 388 cmd->type = htole32(IWM_SCAN_TYPE_FORCED); 389 cmd->repeats = htole32(1); 390 391 /* 392 * If the user asked for passive scan, don't change to active scan if 393 * you see any activity on the channel - remain passive. 394 */ 395 if (n_ssids > 0) { 396 cmd->passive2active = htole16(1); 397 cmd->scan_flags |= IWM_SCAN_FLAGS_PASSIVE2ACTIVE; 398 #if 0 399 if (basic_ssid) { 400 ssid = req->ssids[0].ssid; 401 ssid_len = req->ssids[0].ssid_len; 402 } 403 #endif 404 } else { 405 cmd->passive2active = 0; 406 cmd->scan_flags &= ~IWM_SCAN_FLAGS_PASSIVE2ACTIVE; 407 } 408 409 cmd->tx_cmd.tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL | 410 IWM_TX_CMD_FLG_BT_DIS); 411 cmd->tx_cmd.sta_id = sc->sc_aux_sta.sta_id; 412 cmd->tx_cmd.life_time = htole32(IWM_TX_CMD_LIFE_TIME_INFINITE); 413 cmd->tx_cmd.rate_n_flags = iwm_mvm_scan_rate_n_flags(sc, flags, 1/*XXX*/); 414 415 cmd->tx_cmd.len = htole16(iwm_mvm_fill_probe_req(sc, 416 (struct ieee80211_frame *)cmd->data, 417 sc->sc_bssid, n_ssids, ssid, ssid_len, 418 NULL, 0, sc->sc_capa_max_probe_len)); 419 420 cmd->channel_count 421 = iwm_mvm_scan_fill_channels(sc, cmd, flags, n_ssids, basic_ssid); 422 423 cmd->len = htole16(sizeof(struct iwm_scan_cmd) + 424 le16toh(cmd->tx_cmd.len) + 425 (cmd->channel_count * sizeof(struct iwm_scan_channel))); 426 hcmd.len[0] = le16toh(cmd->len); 427 428 status = IWM_SCAN_RESPONSE_OK; 429 ret = iwm_mvm_send_cmd_status(sc, &hcmd, &status); 430 if (!ret && status == IWM_SCAN_RESPONSE_OK) { 431 IWM_DPRINTF(sc, IWM_DEBUG_SCAN, 432 "Scan request was sent successfully\n"); 433 } else { 434 /* 435 * If the scan failed, it usually means that the FW was unable 436 * to allocate the time events. Warn on it, but maybe we 437 * should try to send the command again with different params. 438 */ 439 sc->sc_scanband = 0; 440 ret = EIO; 441 } 442 return ret; 443 } 444