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