]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/iwm/if_iwm_scan.c
Pull down pjdfstest 0.1
[FreeBSD/FreeBSD.git] / sys / dev / 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 "opt_wlan.h"
109 #include "opt_iwm.h"
110
111 #include <sys/param.h>
112 #include <sys/bus.h>
113 #include <sys/conf.h>
114 #include <sys/endian.h>
115 #include <sys/firmware.h>
116 #include <sys/kernel.h>
117 #include <sys/malloc.h>
118 #include <sys/mbuf.h>
119 #include <sys/mutex.h>
120 #include <sys/module.h>
121 #include <sys/proc.h>
122 #include <sys/rman.h>
123 #include <sys/socket.h>
124 #include <sys/sockio.h>
125 #include <sys/sysctl.h>
126 #include <sys/linker.h>
127
128 #include <machine/bus.h>
129 #include <machine/endian.h>
130 #include <machine/resource.h>
131
132 #include <dev/pci/pcivar.h>
133 #include <dev/pci/pcireg.h>
134
135 #include <net/bpf.h>
136
137 #include <net/if.h>
138 #include <net/if_var.h>
139 #include <net/if_arp.h>
140 #include <net/if_dl.h>
141 #include <net/if_media.h>
142 #include <net/if_types.h>
143
144 #include <netinet/in.h>
145 #include <netinet/in_systm.h>
146 #include <netinet/if_ether.h>
147 #include <netinet/ip.h>
148
149 #include <net80211/ieee80211_var.h>
150 #include <net80211/ieee80211_regdomain.h>
151 #include <net80211/ieee80211_ratectl.h>
152 #include <net80211/ieee80211_radiotap.h>
153
154 #include <dev/iwm/if_iwmreg.h>
155 #include <dev/iwm/if_iwmvar.h>
156 #include <dev/iwm/if_iwm_debug.h>
157 #include <dev/iwm/if_iwm_notif_wait.h>
158 #include <dev/iwm/if_iwm_util.h>
159 #include <dev/iwm/if_iwm_scan.h>
160
161 /*
162  * BEGIN mvm/scan.c
163  */
164
165 #define IWM_DENSE_EBS_SCAN_RATIO 5
166 #define IWM_SPARSE_EBS_SCAN_RATIO 1
167
168 static uint16_t
169 iwm_mvm_scan_rx_chain(struct iwm_softc *sc)
170 {
171         uint16_t rx_chain;
172         uint8_t rx_ant;
173
174         rx_ant = iwm_mvm_get_valid_rx_ant(sc);
175         rx_chain = rx_ant << IWM_PHY_RX_CHAIN_VALID_POS;
176         rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
177         rx_chain |= rx_ant << IWM_PHY_RX_CHAIN_FORCE_SEL_POS;
178         rx_chain |= 0x1 << IWM_PHY_RX_CHAIN_DRIVER_FORCE_POS;
179         return htole16(rx_chain);
180 }
181
182 static uint32_t
183 iwm_mvm_scan_rate_n_flags(struct iwm_softc *sc, int flags, int no_cck)
184 {
185         uint32_t tx_ant;
186         int i, ind;
187
188         for (i = 0, ind = sc->sc_scan_last_antenna;
189             i < IWM_RATE_MCS_ANT_NUM; i++) {
190                 ind = (ind + 1) % IWM_RATE_MCS_ANT_NUM;
191                 if (iwm_mvm_get_valid_tx_ant(sc) & (1 << ind)) {
192                         sc->sc_scan_last_antenna = ind;
193                         break;
194                 }
195         }
196         tx_ant = (1 << sc->sc_scan_last_antenna) << IWM_RATE_MCS_ANT_POS;
197
198         if ((flags & IEEE80211_CHAN_2GHZ) && !no_cck)
199                 return htole32(IWM_RATE_1M_PLCP | IWM_RATE_MCS_CCK_MSK |
200                                    tx_ant);
201         else
202                 return htole32(IWM_RATE_6M_PLCP | tx_ant);
203 }
204
205 #ifdef IWM_DEBUG
206 static const char *
207 iwm_mvm_ebs_status_str(enum iwm_scan_ebs_status status)
208 {
209         switch (status) {
210         case IWM_SCAN_EBS_SUCCESS:
211                 return "successful";
212         case IWM_SCAN_EBS_INACTIVE:
213                 return "inactive";
214         case IWM_SCAN_EBS_FAILED:
215         case IWM_SCAN_EBS_CHAN_NOT_FOUND:
216         default:
217                 return "failed";
218         }
219 }
220
221 static const char *
222 iwm_mvm_offload_status_str(enum iwm_scan_offload_complete_status status)
223 {
224         return (status == IWM_SCAN_OFFLOAD_ABORTED) ? "aborted" : "completed";
225 }
226 #endif
227
228 void
229 iwm_mvm_rx_lmac_scan_complete_notif(struct iwm_softc *sc,
230     struct iwm_rx_packet *pkt)
231 {
232         struct iwm_periodic_scan_complete *scan_notif = (void *)pkt->data;
233
234         /* If this happens, the firmware has mistakenly sent an LMAC
235          * notification during UMAC scans -- warn and ignore it.
236          */
237         if (fw_has_capa(&sc->ucode_capa, IWM_UCODE_TLV_CAPA_UMAC_SCAN)) {
238                 device_printf(sc->sc_dev,
239                     "%s: Mistakenly got LMAC notification during UMAC scan\n",
240                     __func__);
241                 return;
242         }
243
244         IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Regular scan %s, EBS status %s (FW)\n",
245             iwm_mvm_offload_status_str(scan_notif->status),
246             iwm_mvm_ebs_status_str(scan_notif->ebs_status));
247
248         sc->last_ebs_successful =
249                         scan_notif->ebs_status == IWM_SCAN_EBS_SUCCESS ||
250                         scan_notif->ebs_status == IWM_SCAN_EBS_INACTIVE;
251
252 }
253
254 void
255 iwm_mvm_rx_umac_scan_complete_notif(struct iwm_softc *sc,
256     struct iwm_rx_packet *pkt)
257 {
258         struct iwm_umac_scan_complete *notif = (void *)pkt->data;
259
260         IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
261             "Scan completed, uid %u, status %s, EBS status %s\n",
262             le32toh(notif->uid),
263             iwm_mvm_offload_status_str(notif->status),
264             iwm_mvm_ebs_status_str(notif->ebs_status));
265
266         if (notif->ebs_status != IWM_SCAN_EBS_SUCCESS &&
267             notif->ebs_status != IWM_SCAN_EBS_INACTIVE)
268                 sc->last_ebs_successful = FALSE;
269 }
270
271 static int
272 iwm_mvm_scan_skip_channel(struct ieee80211_channel *c)
273 {
274         if (IEEE80211_IS_CHAN_2GHZ(c) && IEEE80211_IS_CHAN_B(c))
275                 return 0;
276         else if (IEEE80211_IS_CHAN_5GHZ(c) && IEEE80211_IS_CHAN_A(c))
277                 return 0;
278         else
279                 return 1;
280 }
281
282 static uint8_t
283 iwm_mvm_lmac_scan_fill_channels(struct iwm_softc *sc,
284     struct iwm_scan_channel_cfg_lmac *chan, int n_ssids)
285 {
286         struct ieee80211com *ic = &sc->sc_ic;
287         struct ieee80211_scan_state *ss = ic->ic_scan;
288         struct ieee80211_channel *c;
289         uint8_t nchan;
290         int j;
291
292         for (nchan = j = 0;
293             j < ic->ic_nchans && nchan < sc->ucode_capa.n_scan_channels; j++) {
294                 c = &ic->ic_channels[j];
295                 /* For 2GHz, only populate 11b channels */
296                 /* For 5GHz, only populate 11a channels */
297                 /*
298                  * Catch other channels, in case we have 900MHz channels or
299                  * something in the chanlist.
300                  */
301                 if (iwm_mvm_scan_skip_channel(c)) {
302                         IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
303                             "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
304                             __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
305                         continue;
306                 }
307
308                 IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
309                     "Adding channel %d (%d Mhz) to the list\n",
310                     nchan, c->ic_freq);
311                 chan->channel_num = htole16(ieee80211_mhz2ieee(c->ic_freq, 0));
312                 chan->iter_count = htole16(1);
313                 chan->iter_interval = htole32(0);
314                 chan->flags = htole32(IWM_UNIFIED_SCAN_CHANNEL_PARTIAL);
315                 chan->flags |= htole32(IWM_SCAN_CHANNEL_NSSIDS(n_ssids));
316                 /* XXX IEEE80211_SCAN_NOBCAST flag is never set. */
317                 if (!IEEE80211_IS_CHAN_PASSIVE(c) &&
318                     (!(ss->ss_flags & IEEE80211_SCAN_NOBCAST) || n_ssids != 0))
319                         chan->flags |= htole32(IWM_SCAN_CHANNEL_TYPE_ACTIVE);
320                 chan++;
321                 nchan++;
322         }
323
324         return nchan;
325 }
326
327 static uint8_t
328 iwm_mvm_umac_scan_fill_channels(struct iwm_softc *sc,
329     struct iwm_scan_channel_cfg_umac *chan, int n_ssids)
330 {
331         struct ieee80211com *ic = &sc->sc_ic;
332         struct ieee80211_channel *c;
333         uint8_t nchan;
334         int j;
335
336         for (nchan = j = 0;
337             j < ic->ic_nchans && nchan < sc->ucode_capa.n_scan_channels; j++) {
338                 c = &ic->ic_channels[j];
339                 /* For 2GHz, only populate 11b channels */
340                 /* For 5GHz, only populate 11a channels */
341                 /*
342                  * Catch other channels, in case we have 900MHz channels or
343                  * something in the chanlist.
344                  */
345                 if (iwm_mvm_scan_skip_channel(c)) {
346                         IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
347                             "%s: skipping channel (freq=%d, ieee=%d, flags=0x%08x)\n",
348                             __func__, c->ic_freq, c->ic_ieee, c->ic_flags);
349                         continue;
350                 }
351
352                 IWM_DPRINTF(sc, IWM_DEBUG_RESET | IWM_DEBUG_EEPROM,
353                     "Adding channel %d (%d Mhz) to the list\n",
354                     nchan, c->ic_freq);
355                 chan->channel_num = ieee80211_mhz2ieee(c->ic_freq, 0);
356                 chan->iter_count = 1;
357                 chan->iter_interval = htole16(0);
358                 chan->flags = htole32(IWM_SCAN_CHANNEL_UMAC_NSSIDS(n_ssids));
359                 chan++;
360                 nchan++;
361         }
362
363         return nchan;
364 }
365
366 static int
367 iwm_mvm_fill_probe_req(struct iwm_softc *sc, struct iwm_scan_probe_req *preq)
368 {
369         struct ieee80211com *ic = &sc->sc_ic;
370         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
371         struct ieee80211_frame *wh = (struct ieee80211_frame *)preq->buf;
372         struct ieee80211_rateset *rs;
373         size_t remain = sizeof(preq->buf);
374         uint8_t *frm, *pos;
375
376         memset(preq, 0, sizeof(*preq));
377
378         /* Ensure enough space for header and SSID IE. */
379         if (remain < sizeof(*wh) + 2)
380                 return ENOBUFS;
381
382         /*
383          * Build a probe request frame.  Most of the following code is a
384          * copy & paste of what is done in net80211.
385          */
386         wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT |
387             IEEE80211_FC0_SUBTYPE_PROBE_REQ;
388         wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
389         IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
390         IEEE80211_ADDR_COPY(wh->i_addr2, vap ? vap->iv_myaddr : ic->ic_macaddr);
391         IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
392         *(uint16_t *)&wh->i_dur[0] = 0; /* filled by HW */
393         *(uint16_t *)&wh->i_seq[0] = 0; /* filled by HW */
394
395         frm = (uint8_t *)(wh + 1);
396         frm = ieee80211_add_ssid(frm, NULL, 0);
397
398         /* Tell the firmware where the MAC header is. */
399         preq->mac_header.offset = 0;
400         preq->mac_header.len = htole16(frm - (uint8_t *)wh);
401         remain -= frm - (uint8_t *)wh;
402
403         /* Fill in 2GHz IEs and tell firmware where they are. */
404         rs = &ic->ic_sup_rates[IEEE80211_MODE_11G];
405         if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
406                 if (remain < 4 + rs->rs_nrates)
407                         return ENOBUFS;
408         } else if (remain < 2 + rs->rs_nrates) {
409                 return ENOBUFS;
410         }
411         preq->band_data[0].offset = htole16(frm - (uint8_t *)wh);
412         pos = frm;
413         frm = ieee80211_add_rates(frm, rs);
414         if (rs->rs_nrates > IEEE80211_RATE_SIZE)
415                 frm = ieee80211_add_xrates(frm, rs);
416         preq->band_data[0].len = htole16(frm - pos);
417         remain -= frm - pos;
418
419         if (fw_has_capa(&sc->ucode_capa,
420             IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT)) {
421                 if (remain < 3)
422                         return ENOBUFS;
423                 *frm++ = IEEE80211_ELEMID_DSPARMS;
424                 *frm++ = 1;
425                 *frm++ = 0;
426                 remain -= 3;
427         }
428
429         if (sc->nvm_data->sku_cap_band_52GHz_enable) {
430                 /* Fill in 5GHz IEs. */
431                 rs = &ic->ic_sup_rates[IEEE80211_MODE_11A];
432                 if (rs->rs_nrates > IEEE80211_RATE_SIZE) {
433                         if (remain < 4 + rs->rs_nrates)
434                                 return ENOBUFS;
435                 } else if (remain < 2 + rs->rs_nrates) {
436                         return ENOBUFS;
437                 }
438                 preq->band_data[1].offset = htole16(frm - (uint8_t *)wh);
439                 pos = frm;
440                 frm = ieee80211_add_rates(frm, rs);
441                 if (rs->rs_nrates > IEEE80211_RATE_SIZE)
442                         frm = ieee80211_add_xrates(frm, rs);
443                 preq->band_data[1].len = htole16(frm - pos);
444                 remain -= frm - pos;
445         }
446
447         /* Send 11n IEs on both 2GHz and 5GHz bands. */
448         preq->common_data.offset = htole16(frm - (uint8_t *)wh);
449         pos = frm;
450 #if 0
451         if (ic->ic_flags & IEEE80211_F_HTON) {
452                 if (remain < 28)
453                         return ENOBUFS;
454                 frm = ieee80211_add_htcaps(frm, ic);
455                 /* XXX add WME info? */
456         }
457 #endif
458         preq->common_data.len = htole16(frm - pos);
459
460         return 0;
461 }
462
463 int
464 iwm_mvm_config_umac_scan(struct iwm_softc *sc)
465 {
466         struct ieee80211com *ic = &sc->sc_ic;
467         struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps);
468
469         struct iwm_scan_config *scan_config;
470         int ret, j, nchan;
471         size_t cmd_size;
472         struct ieee80211_channel *c;
473         struct iwm_host_cmd hcmd = {
474                 .id = iwm_cmd_id(IWM_SCAN_CFG_CMD, IWM_ALWAYS_LONG_GROUP, 0),
475                 .flags = IWM_CMD_SYNC,
476         };
477         static const uint32_t rates = (IWM_SCAN_CONFIG_RATE_1M |
478             IWM_SCAN_CONFIG_RATE_2M | IWM_SCAN_CONFIG_RATE_5M |
479             IWM_SCAN_CONFIG_RATE_11M | IWM_SCAN_CONFIG_RATE_6M |
480             IWM_SCAN_CONFIG_RATE_9M | IWM_SCAN_CONFIG_RATE_12M |
481             IWM_SCAN_CONFIG_RATE_18M | IWM_SCAN_CONFIG_RATE_24M |
482             IWM_SCAN_CONFIG_RATE_36M | IWM_SCAN_CONFIG_RATE_48M |
483             IWM_SCAN_CONFIG_RATE_54M);
484
485         cmd_size = sizeof(*scan_config) + sc->ucode_capa.n_scan_channels;
486
487         scan_config = malloc(cmd_size, M_DEVBUF, M_NOWAIT | M_ZERO);
488         if (scan_config == NULL)
489                 return ENOMEM;
490
491         scan_config->tx_chains = htole32(iwm_mvm_get_valid_tx_ant(sc));
492         scan_config->rx_chains = htole32(iwm_mvm_get_valid_rx_ant(sc));
493         scan_config->legacy_rates = htole32(rates |
494             IWM_SCAN_CONFIG_SUPPORTED_RATE(rates));
495
496         /* These timings correspond to iwlwifi's UNASSOC scan. */
497         scan_config->dwell_active = 10;
498         scan_config->dwell_passive = 110;
499         scan_config->dwell_fragmented = 44;
500         scan_config->dwell_extended = 90;
501         scan_config->out_of_channel_time = htole32(0);
502         scan_config->suspend_time = htole32(0);
503
504         IEEE80211_ADDR_COPY(scan_config->mac_addr,
505             vap ? vap->iv_myaddr : ic->ic_macaddr);
506
507         scan_config->bcast_sta_id = sc->sc_aux_sta.sta_id;
508         scan_config->channel_flags = IWM_CHANNEL_FLAG_EBS |
509             IWM_CHANNEL_FLAG_ACCURATE_EBS | IWM_CHANNEL_FLAG_EBS_ADD |
510             IWM_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
511
512         for (nchan = j = 0;
513             j < ic->ic_nchans && nchan < sc->ucode_capa.n_scan_channels; j++) {
514                 c = &ic->ic_channels[j];
515                 /* For 2GHz, only populate 11b channels */
516                 /* For 5GHz, only populate 11a channels */
517                 /*
518                  * Catch other channels, in case we have 900MHz channels or
519                  * something in the chanlist.
520                  */
521                 if (iwm_mvm_scan_skip_channel(c))
522                         continue;
523                 scan_config->channel_array[nchan++] =
524                     ieee80211_mhz2ieee(c->ic_freq, 0);
525         }
526
527         scan_config->flags = htole32(IWM_SCAN_CONFIG_FLAG_ACTIVATE |
528             IWM_SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
529             IWM_SCAN_CONFIG_FLAG_SET_TX_CHAINS |
530             IWM_SCAN_CONFIG_FLAG_SET_RX_CHAINS |
531             IWM_SCAN_CONFIG_FLAG_SET_AUX_STA_ID |
532             IWM_SCAN_CONFIG_FLAG_SET_ALL_TIMES |
533             IWM_SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
534             IWM_SCAN_CONFIG_FLAG_SET_MAC_ADDR |
535             IWM_SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
536             IWM_SCAN_CONFIG_N_CHANNELS(nchan) |
537             IWM_SCAN_CONFIG_FLAG_CLEAR_FRAGMENTED);
538
539         hcmd.data[0] = scan_config;
540         hcmd.len[0] = cmd_size;
541
542         IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending UMAC scan config\n");
543
544         ret = iwm_send_cmd(sc, &hcmd);
545         if (!ret)
546                 IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
547                     "UMAC scan config was sent successfully\n");
548
549         free(scan_config, M_DEVBUF);
550         return ret;
551 }
552
553 static boolean_t
554 iwm_mvm_scan_use_ebs(struct iwm_softc *sc)
555 {
556         const struct iwm_ucode_capabilities *capa = &sc->ucode_capa;
557
558         /* We can only use EBS if:
559          *      1. the feature is supported;
560          *      2. the last EBS was successful;
561          *      3. if only single scan, the single scan EBS API is supported;
562          *      4. it's not a p2p find operation.
563          */
564         return ((capa->flags & IWM_UCODE_TLV_FLAGS_EBS_SUPPORT) &&
565                 sc->last_ebs_successful);
566 }
567
568 int
569 iwm_mvm_umac_scan(struct iwm_softc *sc)
570 {
571         struct iwm_host_cmd hcmd = {
572                 .id = iwm_cmd_id(IWM_SCAN_REQ_UMAC, IWM_ALWAYS_LONG_GROUP, 0),
573                 .len = { 0, },
574                 .data = { NULL, },
575                 .flags = IWM_CMD_SYNC,
576         };
577         struct ieee80211_scan_state *ss = sc->sc_ic.ic_scan;
578         struct iwm_scan_req_umac *req;
579         struct iwm_scan_req_umac_tail *tail;
580         size_t req_len;
581         uint8_t i, nssid;
582         int ret;
583
584         req_len = sizeof(struct iwm_scan_req_umac) +
585             (sizeof(struct iwm_scan_channel_cfg_umac) *
586             sc->ucode_capa.n_scan_channels) +
587             sizeof(struct iwm_scan_req_umac_tail);
588         if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
589                 return ENOMEM;
590         req = malloc(req_len, M_DEVBUF, M_NOWAIT | M_ZERO);
591         if (req == NULL)
592                 return ENOMEM;
593
594         hcmd.len[0] = (uint16_t)req_len;
595         hcmd.data[0] = (void *)req;
596
597         IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Handling ieee80211 scan request\n");
598
599         /* These timings correspond to iwlwifi's UNASSOC scan. */
600         req->active_dwell = 10;
601         req->passive_dwell = 110;
602         req->fragmented_dwell = 44;
603         req->extended_dwell = 90;
604         req->max_out_time = 0;
605         req->suspend_time = 0;
606
607         req->scan_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
608         req->ooc_priority = htole32(IWM_SCAN_PRIORITY_HIGH);
609
610         nssid = MIN(ss->ss_nssid, IWM_PROBE_OPTION_MAX);
611         req->n_channels = iwm_mvm_umac_scan_fill_channels(sc,
612             (struct iwm_scan_channel_cfg_umac *)req->data, nssid);
613
614         req->general_flags = htole32(IWM_UMAC_SCAN_GEN_FLAGS_PASS_ALL |
615             IWM_UMAC_SCAN_GEN_FLAGS_ITER_COMPLETE |
616             IWM_UMAC_SCAN_GEN_FLAGS_EXTENDED_DWELL);
617
618         tail = (void *)((char *)&req->data +
619                 sizeof(struct iwm_scan_channel_cfg_umac) *
620                         sc->ucode_capa.n_scan_channels);
621
622         /* Check if we're doing an active directed scan. */
623         for (i = 0; i < nssid; i++) {
624                 tail->direct_scan[i].id = IEEE80211_ELEMID_SSID;
625                 tail->direct_scan[i].len = MIN(ss->ss_ssid[i].len,
626                     IEEE80211_NWID_LEN);
627                 memcpy(tail->direct_scan[i].ssid, ss->ss_ssid[i].ssid,
628                     tail->direct_scan[i].len);
629                 /* XXX debug */
630         }
631         if (nssid != 0) {
632                 req->general_flags |=
633                     htole32(IWM_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT);
634         } else
635                 req->general_flags |= htole32(IWM_UMAC_SCAN_GEN_FLAGS_PASSIVE);
636
637         if (iwm_mvm_scan_use_ebs(sc))
638                 req->channel_flags = IWM_SCAN_CHANNEL_FLAG_EBS |
639                                      IWM_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
640                                      IWM_SCAN_CHANNEL_FLAG_CACHE_ADD;
641
642         if (fw_has_capa(&sc->ucode_capa,
643             IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
644                 req->general_flags |=
645                     htole32(IWM_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED);
646
647         ret = iwm_mvm_fill_probe_req(sc, &tail->preq);
648         if (ret) {
649                 free(req, M_DEVBUF);
650                 return ret;
651         }
652
653         /* Specify the scan plan: We'll do one iteration. */
654         tail->schedule[0].interval = 0;
655         tail->schedule[0].iter_count = 1;
656
657         ret = iwm_send_cmd(sc, &hcmd);
658         if (!ret)
659                 IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
660                     "Scan request was sent successfully\n");
661         free(req, M_DEVBUF);
662         return ret;
663 }
664
665 int
666 iwm_mvm_lmac_scan(struct iwm_softc *sc)
667 {
668         struct iwm_host_cmd hcmd = {
669                 .id = IWM_SCAN_OFFLOAD_REQUEST_CMD,
670                 .len = { 0, },
671                 .data = { NULL, },
672                 .flags = IWM_CMD_SYNC,
673         };
674         struct ieee80211_scan_state *ss = sc->sc_ic.ic_scan;
675         struct iwm_scan_req_lmac *req;
676         size_t req_len;
677         uint8_t i, nssid;
678         int ret;
679
680         IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
681             "Handling ieee80211 scan request\n");
682
683         req_len = sizeof(struct iwm_scan_req_lmac) +
684             (sizeof(struct iwm_scan_channel_cfg_lmac) *
685             sc->ucode_capa.n_scan_channels) + sizeof(struct iwm_scan_probe_req);
686         if (req_len > IWM_MAX_CMD_PAYLOAD_SIZE)
687                 return ENOMEM;
688         req = malloc(req_len, M_DEVBUF, M_NOWAIT | M_ZERO);
689         if (req == NULL)
690                 return ENOMEM;
691
692         hcmd.len[0] = (uint16_t)req_len;
693         hcmd.data[0] = (void *)req;
694
695         /* These timings correspond to iwlwifi's UNASSOC scan. */
696         req->active_dwell = 10;
697         req->passive_dwell = 110;
698         req->fragmented_dwell = 44;
699         req->extended_dwell = 90;
700         req->max_out_time = 0;
701         req->suspend_time = 0;
702
703         req->scan_prio = htole32(IWM_SCAN_PRIORITY_HIGH);
704         req->rx_chain_select = iwm_mvm_scan_rx_chain(sc);
705         req->iter_num = htole32(1);
706         req->delay = 0;
707
708         req->scan_flags = htole32(IWM_MVM_LMAC_SCAN_FLAG_PASS_ALL |
709             IWM_MVM_LMAC_SCAN_FLAG_ITER_COMPLETE |
710             IWM_MVM_LMAC_SCAN_FLAG_EXTENDED_DWELL);
711         if (fw_has_capa(&sc->ucode_capa,
712             IWM_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
713                 req->scan_flags |= htole32(IWM_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
714
715         req->flags = htole32(IWM_PHY_BAND_24);
716         if (sc->nvm_data->sku_cap_band_52GHz_enable)
717                 req->flags |= htole32(IWM_PHY_BAND_5);
718         req->filter_flags =
719             htole32(IWM_MAC_FILTER_ACCEPT_GRP | IWM_MAC_FILTER_IN_BEACON);
720
721         /* Tx flags 2 GHz. */
722         req->tx_cmd[0].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
723             IWM_TX_CMD_FLG_BT_DIS);
724         req->tx_cmd[0].rate_n_flags =
725             iwm_mvm_scan_rate_n_flags(sc, IEEE80211_CHAN_2GHZ, 1/*XXX*/);
726         req->tx_cmd[0].sta_id = sc->sc_aux_sta.sta_id;
727
728         /* Tx flags 5 GHz. */
729         req->tx_cmd[1].tx_flags = htole32(IWM_TX_CMD_FLG_SEQ_CTL |
730             IWM_TX_CMD_FLG_BT_DIS);
731         req->tx_cmd[1].rate_n_flags =
732             iwm_mvm_scan_rate_n_flags(sc, IEEE80211_CHAN_5GHZ, 1/*XXX*/);
733         req->tx_cmd[1].sta_id = sc->sc_aux_sta.sta_id;
734
735         /* Check if we're doing an active directed scan. */
736         nssid = MIN(ss->ss_nssid, IWM_PROBE_OPTION_MAX);
737         for (i = 0; i < nssid; i++) {
738                 req->direct_scan[i].id = IEEE80211_ELEMID_SSID;
739                 req->direct_scan[i].len = MIN(ss->ss_ssid[i].len,
740                     IEEE80211_NWID_LEN);
741                 memcpy(req->direct_scan[i].ssid, ss->ss_ssid[i].ssid,
742                     req->direct_scan[i].len);
743                 /* XXX debug */
744         }
745         if (nssid != 0) {
746                 req->scan_flags |=
747                     htole32(IWM_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION);
748         } else
749                 req->scan_flags |= htole32(IWM_MVM_LMAC_SCAN_FLAG_PASSIVE);
750
751         req->n_channels = iwm_mvm_lmac_scan_fill_channels(sc,
752             (struct iwm_scan_channel_cfg_lmac *)req->data, nssid);
753
754         ret = iwm_mvm_fill_probe_req(sc,
755                             (struct iwm_scan_probe_req *)(req->data +
756                             (sizeof(struct iwm_scan_channel_cfg_lmac) *
757                             sc->ucode_capa.n_scan_channels)));
758         if (ret) {
759                 free(req, M_DEVBUF);
760                 return ret;
761         }
762
763         /* Specify the scan plan: We'll do one iteration. */
764         req->schedule[0].iterations = 1;
765         req->schedule[0].full_scan_mul = 1;
766
767         if (iwm_mvm_scan_use_ebs(sc)) {
768                 req->channel_opt[0].flags =
769                         htole16(IWM_SCAN_CHANNEL_FLAG_EBS |
770                                 IWM_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
771                                 IWM_SCAN_CHANNEL_FLAG_CACHE_ADD);
772                 req->channel_opt[0].non_ebs_ratio =
773                         htole16(IWM_DENSE_EBS_SCAN_RATIO);
774                 req->channel_opt[1].flags =
775                         htole16(IWM_SCAN_CHANNEL_FLAG_EBS |
776                                 IWM_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
777                                 IWM_SCAN_CHANNEL_FLAG_CACHE_ADD);
778                 req->channel_opt[1].non_ebs_ratio =
779                         htole16(IWM_SPARSE_EBS_SCAN_RATIO);
780         }
781
782         ret = iwm_send_cmd(sc, &hcmd);
783         if (!ret) {
784                 IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
785                     "Scan request was sent successfully\n");
786         }
787         free(req, M_DEVBUF);
788         return ret;
789 }
790
791 static int
792 iwm_mvm_lmac_scan_abort(struct iwm_softc *sc)
793 {
794         int ret;
795         struct iwm_host_cmd hcmd = {
796                 .id = IWM_SCAN_OFFLOAD_ABORT_CMD,
797                 .len = { 0, },
798                 .data = { NULL, },
799                 .flags = IWM_CMD_SYNC,
800         };
801         uint32_t status;
802
803         ret = iwm_mvm_send_cmd_status(sc, &hcmd, &status);
804         if (ret)
805                 return ret;
806
807         if (status != IWM_CAN_ABORT_STATUS) {
808                 /*
809                  * The scan abort will return 1 for success or
810                  * 2 for "failure".  A failure condition can be
811                  * due to simply not being in an active scan which
812                  * can occur if we send the scan abort before the
813                  * microcode has notified us that a scan is completed.
814                  */
815                 IWM_DPRINTF(sc, IWM_DEBUG_SCAN,
816                     "SCAN OFFLOAD ABORT ret %d.\n", status);
817                 ret = ENOENT;
818         }
819
820         return ret;
821 }
822
823 static int
824 iwm_mvm_umac_scan_abort(struct iwm_softc *sc)
825 {
826         struct iwm_umac_scan_abort cmd = {};
827         int uid, ret;
828
829         uid = 0;
830         cmd.uid = htole32(uid);
831
832         IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Sending scan abort, uid %u\n", uid);
833
834         ret = iwm_mvm_send_cmd_pdu(sc,
835                                    iwm_cmd_id(IWM_SCAN_ABORT_UMAC,
836                                               IWM_ALWAYS_LONG_GROUP, 0),
837                                    0, sizeof(cmd), &cmd);
838
839         return ret;
840 }
841
842 int
843 iwm_mvm_scan_stop_wait(struct iwm_softc *sc)
844 {
845         struct iwm_notification_wait wait_scan_done;
846         static const uint16_t scan_done_notif[] = { IWM_SCAN_COMPLETE_UMAC,
847                                                    IWM_SCAN_OFFLOAD_COMPLETE, };
848         int ret;
849
850         iwm_init_notification_wait(sc->sc_notif_wait, &wait_scan_done,
851                                    scan_done_notif, nitems(scan_done_notif),
852                                    NULL, NULL);
853
854         IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "Preparing to stop scan\n");
855
856         if (fw_has_capa(&sc->ucode_capa, IWM_UCODE_TLV_CAPA_UMAC_SCAN))
857                 ret = iwm_mvm_umac_scan_abort(sc);
858         else
859                 ret = iwm_mvm_lmac_scan_abort(sc);
860
861         if (ret) {
862                 IWM_DPRINTF(sc, IWM_DEBUG_SCAN, "couldn't stop scan\n");
863                 iwm_remove_notification(sc->sc_notif_wait, &wait_scan_done);
864                 return ret;
865         }
866
867         IWM_UNLOCK(sc);
868         ret = iwm_wait_notification(sc->sc_notif_wait, &wait_scan_done, hz);
869         IWM_LOCK(sc);
870
871         return ret;
872 }