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