Merge branch 'for-upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/bluetoot...
[cascardo/linux.git] / drivers / net / wireless / iwlwifi / mvm / scan.c
1 /******************************************************************************
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
9  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23  * USA
24  *
25  * The full GNU General Public License is included in this distribution
26  * in the file called COPYING.
27  *
28  * Contact Information:
29  *  Intel Linux Wireless <ilw@linux.intel.com>
30  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31  *
32  * BSD LICENSE
33  *
34  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
35  * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
36  * All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  *
42  *  * Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions and the following disclaimer.
44  *  * Redistributions in binary form must reproduce the above copyright
45  *    notice, this list of conditions and the following disclaimer in
46  *    the documentation and/or other materials provided with the
47  *    distribution.
48  *  * Neither the name Intel Corporation nor the names of its
49  *    contributors may be used to endorse or promote products derived
50  *    from this software without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63  *
64  *****************************************************************************/
65
66 #include <linux/etherdevice.h>
67 #include <net/mac80211.h>
68
69 #include "mvm.h"
70 #include "iwl-eeprom-parse.h"
71 #include "fw-api-scan.h"
72
73 #define IWL_PLCP_QUIET_THRESH 1
74 #define IWL_ACTIVE_QUIET_TIME 10
75
76 struct iwl_mvm_scan_params {
77         u32 max_out_time;
78         u32 suspend_time;
79         bool passive_fragmented;
80         struct _dwell {
81                 u16 passive;
82                 u16 active;
83         } dwell[IEEE80211_NUM_BANDS];
84 };
85
86 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
87 {
88         u16 rx_chain;
89         u8 rx_ant;
90
91         if (mvm->scan_rx_ant != ANT_NONE)
92                 rx_ant = mvm->scan_rx_ant;
93         else
94                 rx_ant = mvm->fw->valid_rx_ant;
95         rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
96         rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
97         rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
98         rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
99         return cpu_to_le16(rx_chain);
100 }
101
102 static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
103 {
104         if (band == IEEE80211_BAND_2GHZ)
105                 return cpu_to_le32(PHY_BAND_24);
106         else
107                 return cpu_to_le32(PHY_BAND_5);
108 }
109
110 static inline __le32
111 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
112                           bool no_cck)
113 {
114         u32 tx_ant;
115
116         mvm->scan_last_antenna_idx =
117                 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
118                                      mvm->scan_last_antenna_idx);
119         tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
120
121         if (band == IEEE80211_BAND_2GHZ && !no_cck)
122                 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
123                                    tx_ant);
124         else
125                 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
126 }
127
128 /*
129  * We insert the SSIDs in an inverted order, because the FW will
130  * invert it back. The most prioritized SSID, which is first in the
131  * request list, is not copied here, but inserted directly to the probe
132  * request.
133  */
134 static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
135                                     struct cfg80211_ssid *ssids,
136                                     int n_ssids, int first)
137 {
138         int fw_idx, req_idx;
139
140         for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
141              req_idx--, fw_idx++) {
142                 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
143                 cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
144                 memcpy(cmd_ssid[fw_idx].ssid,
145                        ssids[req_idx].ssid,
146                        ssids[req_idx].ssid_len);
147         }
148 }
149
150 /*
151  * If req->n_ssids > 0, it means we should do an active scan.
152  * In case of active scan w/o directed scan, we receive a zero-length SSID
153  * just to notify that this scan is active and not passive.
154  * In order to notify the FW of the number of SSIDs we wish to scan (including
155  * the zero-length one), we need to set the corresponding bits in chan->type,
156  * one for each SSID, and set the active bit (first). If the first SSID is
157  * already included in the probe template, so we need to set only
158  * req->n_ssids - 1 bits in addition to the first bit.
159  */
160 static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
161 {
162         if (band == IEEE80211_BAND_2GHZ)
163                 return 20  + 3 * (n_ssids + 1);
164         return 10  + 2 * (n_ssids + 1);
165 }
166
167 static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
168 {
169         return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
170 }
171
172 static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
173                                        struct cfg80211_scan_request *req,
174                                        bool basic_ssid,
175                                        struct iwl_mvm_scan_params *params)
176 {
177         struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
178                 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
179         int i;
180         int type = BIT(req->n_ssids) - 1;
181         enum ieee80211_band band = req->channels[0]->band;
182
183         if (!basic_ssid)
184                 type |= BIT(req->n_ssids);
185
186         for (i = 0; i < cmd->channel_count; i++) {
187                 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
188                 chan->type = cpu_to_le32(type);
189                 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
190                         chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
191                 chan->active_dwell = cpu_to_le16(params->dwell[band].active);
192                 chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
193                 chan->iteration_count = cpu_to_le16(1);
194                 chan++;
195         }
196 }
197
198 /*
199  * Fill in probe request with the following parameters:
200  * TA is our vif HW address, which mac80211 ensures we have.
201  * Packet is broadcasted, so this is both SA and DA.
202  * The probe request IE is made out of two: first comes the most prioritized
203  * SSID if a directed scan is requested. Second comes whatever extra
204  * information was given to us as the scan request IE.
205  */
206 static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
207                                   int n_ssids, const u8 *ssid, int ssid_len,
208                                   const u8 *band_ie, int band_ie_len,
209                                   const u8 *common_ie, int common_ie_len,
210                                   int left)
211 {
212         int len = 0;
213         u8 *pos = NULL;
214
215         /* Make sure there is enough space for the probe request,
216          * two mandatory IEs and the data */
217         left -= 24;
218         if (left < 0)
219                 return 0;
220
221         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
222         eth_broadcast_addr(frame->da);
223         memcpy(frame->sa, ta, ETH_ALEN);
224         eth_broadcast_addr(frame->bssid);
225         frame->seq_ctrl = 0;
226
227         len += 24;
228
229         /* for passive scans, no need to fill anything */
230         if (n_ssids == 0)
231                 return (u16)len;
232
233         /* points to the payload of the request */
234         pos = &frame->u.probe_req.variable[0];
235
236         /* fill in our SSID IE */
237         left -= ssid_len + 2;
238         if (left < 0)
239                 return 0;
240         *pos++ = WLAN_EID_SSID;
241         *pos++ = ssid_len;
242         if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
243                 memcpy(pos, ssid, ssid_len);
244                 pos += ssid_len;
245         }
246
247         len += ssid_len + 2;
248
249         if (WARN_ON(left < band_ie_len + common_ie_len))
250                 return len;
251
252         if (band_ie && band_ie_len) {
253                 memcpy(pos, band_ie, band_ie_len);
254                 pos += band_ie_len;
255                 len += band_ie_len;
256         }
257
258         if (common_ie && common_ie_len) {
259                 memcpy(pos, common_ie, common_ie_len);
260                 pos += common_ie_len;
261                 len += common_ie_len;
262         }
263
264         return (u16)len;
265 }
266
267 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
268                                             struct ieee80211_vif *vif)
269 {
270         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
271         bool *global_bound = data;
272
273         if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
274             mvmvif->phy_ctxt->id < MAX_PHYS)
275                 *global_bound = true;
276 }
277
278 static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
279                                      struct ieee80211_vif *vif,
280                                      int n_ssids, u32 flags,
281                                      struct iwl_mvm_scan_params *params)
282 {
283         bool global_bound = false;
284         enum ieee80211_band band;
285         u8 frag_passive_dwell = 0;
286
287         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
288                                             IEEE80211_IFACE_ITER_NORMAL,
289                                             iwl_mvm_scan_condition_iterator,
290                                             &global_bound);
291
292         if (!global_bound)
293                 goto not_bound;
294
295         params->suspend_time = 30;
296         params->max_out_time = 170;
297
298         if (iwl_mvm_low_latency(mvm)) {
299                 if (mvm->fw->ucode_capa.api[0] &
300                     IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
301                         params->suspend_time = 105;
302                         params->max_out_time = 70;
303                         frag_passive_dwell = 20;
304                 } else {
305                         params->suspend_time = 120;
306                         params->max_out_time = 120;
307                 }
308         }
309
310         if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
311                                    IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
312                 /*
313                  * P2P device scan should not be fragmented to avoid negative
314                  * impact on P2P device discovery. Configure max_out_time to be
315                  * equal to dwell time on passive channel. Take a longest
316                  * possible value, one that corresponds to 2GHz band
317                  */
318                 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
319                         u32 passive_dwell =
320                                 iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
321                         params->max_out_time = passive_dwell;
322                 } else {
323                         params->passive_fragmented = true;
324                 }
325         }
326
327         if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
328                 params->max_out_time = 200;
329
330 not_bound:
331
332         for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
333                 if (params->passive_fragmented)
334                         params->dwell[band].passive = frag_passive_dwell;
335                 else
336                         params->dwell[band].passive =
337                                 iwl_mvm_get_passive_dwell(band);
338                 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
339                                                                       n_ssids);
340         }
341 }
342
343 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
344 {
345         /* require rrm scan whenever the fw supports it */
346         return mvm->fw->ucode_capa.capa[0] &
347                IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
348 }
349
350 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
351                                            bool is_sched_scan)
352 {
353         int max_probe_len;
354
355         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
356                 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
357         else
358                 max_probe_len = mvm->fw->ucode_capa.max_probe_length;
359
360         /* we create the 802.11 header and SSID element */
361         max_probe_len -= 24 + 2;
362
363         /* basic ssid is added only for hw_scan with and old api */
364         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
365             !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
366             !is_sched_scan)
367                 max_probe_len -= 32;
368
369         return max_probe_len;
370 }
371
372 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
373 {
374         int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
375
376         if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
377                 return max_ie_len;
378
379         /* TODO: [BUG] This function should return the maximum allowed size of
380          * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
381          * in the same command. So the correct implementation of this function
382          * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
383          * command has only 512 bytes and it would leave us with about 240
384          * bytes for scan IEs, which is clearly not enough. So meanwhile
385          * we will report an incorrect value. This may result in a failure to
386          * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
387          * functions with -ENOBUFS, if a large enough probe will be provided.
388          */
389         return max_ie_len;
390 }
391
392 int iwl_mvm_scan_request(struct iwl_mvm *mvm,
393                          struct ieee80211_vif *vif,
394                          struct cfg80211_scan_request *req)
395 {
396         struct iwl_host_cmd hcmd = {
397                 .id = SCAN_REQUEST_CMD,
398                 .len = { 0, },
399                 .data = { mvm->scan_cmd, },
400                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
401         };
402         struct iwl_scan_cmd *cmd = mvm->scan_cmd;
403         int ret;
404         u32 status;
405         int ssid_len = 0;
406         u8 *ssid = NULL;
407         bool basic_ssid = !(mvm->fw->ucode_capa.flags &
408                            IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
409         struct iwl_mvm_scan_params params = {};
410
411         lockdep_assert_held(&mvm->mutex);
412
413         /* we should have failed registration if scan_cmd was NULL */
414         if (WARN_ON(mvm->scan_cmd == NULL))
415                 return -ENOMEM;
416
417         IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
418         mvm->scan_status = IWL_MVM_SCAN_OS;
419         memset(cmd, 0, ksize(cmd));
420
421         cmd->channel_count = (u8)req->n_channels;
422         cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
423         cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
424         cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
425
426         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
427         cmd->max_out_time = cpu_to_le32(params.max_out_time);
428         cmd->suspend_time = cpu_to_le32(params.suspend_time);
429         if (params.passive_fragmented)
430                 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
431
432         cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
433         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
434                                         MAC_FILTER_IN_BEACON);
435
436         if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
437                 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
438         else
439                 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
440
441         cmd->repeats = cpu_to_le32(1);
442
443         /*
444          * If the user asked for passive scan, don't change to active scan if
445          * you see any activity on the channel - remain passive.
446          */
447         if (req->n_ssids > 0) {
448                 cmd->passive2active = cpu_to_le16(1);
449                 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
450                 if (basic_ssid) {
451                         ssid = req->ssids[0].ssid;
452                         ssid_len = req->ssids[0].ssid_len;
453                 }
454         } else {
455                 cmd->passive2active = 0;
456                 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
457         }
458
459         iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
460                                 basic_ssid ? 1 : 0);
461
462         cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
463                                            3 << TX_CMD_FLG_BT_PRIO_POS);
464
465         cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
466         cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
467         cmd->tx_cmd.rate_n_flags =
468                         iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
469                                                   req->no_cck);
470
471         cmd->tx_cmd.len =
472                 cpu_to_le16(iwl_mvm_fill_probe_req(
473                             (struct ieee80211_mgmt *)cmd->data,
474                             vif->addr,
475                             req->n_ssids, ssid, ssid_len,
476                             req->ie, req->ie_len, NULL, 0,
477                             mvm->fw->ucode_capa.max_probe_length));
478
479         iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
480
481         cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
482                 le16_to_cpu(cmd->tx_cmd.len) +
483                 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
484         hcmd.len[0] = le16_to_cpu(cmd->len);
485
486         status = SCAN_RESPONSE_OK;
487         ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
488         if (!ret && status == SCAN_RESPONSE_OK) {
489                 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
490         } else {
491                 /*
492                  * If the scan failed, it usually means that the FW was unable
493                  * to allocate the time events. Warn on it, but maybe we
494                  * should try to send the command again with different params.
495                  */
496                 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
497                         status, ret);
498                 mvm->scan_status = IWL_MVM_SCAN_NONE;
499                 ret = -EIO;
500         }
501         return ret;
502 }
503
504 int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
505                           struct iwl_device_cmd *cmd)
506 {
507         struct iwl_rx_packet *pkt = rxb_addr(rxb);
508         struct iwl_cmd_response *resp = (void *)pkt->data;
509
510         IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
511                        le32_to_cpu(resp->status));
512         return 0;
513 }
514
515 int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
516                           struct iwl_device_cmd *cmd)
517 {
518         struct iwl_rx_packet *pkt = rxb_addr(rxb);
519         struct iwl_scan_complete_notif *notif = (void *)pkt->data;
520
521         lockdep_assert_held(&mvm->mutex);
522
523         IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
524                        notif->status, notif->scanned_channels);
525
526         if (mvm->scan_status == IWL_MVM_SCAN_OS)
527                 mvm->scan_status = IWL_MVM_SCAN_NONE;
528         ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
529
530         iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
531
532         return 0;
533 }
534
535 int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
536                                     struct iwl_rx_cmd_buffer *rxb,
537                                     struct iwl_device_cmd *cmd)
538 {
539         struct iwl_rx_packet *pkt = rxb_addr(rxb);
540         u8 client_bitmap = 0;
541
542         if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
543                 struct iwl_sched_scan_results *notif = (void *)pkt->data;
544
545                 client_bitmap = notif->client_bitmap;
546         }
547
548         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
549             client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
550                 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
551                         IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
552                         ieee80211_sched_scan_results(mvm->hw);
553                 } else {
554                         IWL_DEBUG_SCAN(mvm, "Scan results\n");
555                 }
556         }
557
558         return 0;
559 }
560
561 static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
562                                      struct iwl_rx_packet *pkt, void *data)
563 {
564         struct iwl_mvm *mvm =
565                 container_of(notif_wait, struct iwl_mvm, notif_wait);
566         struct iwl_scan_complete_notif *notif;
567         u32 *resp;
568
569         switch (pkt->hdr.cmd) {
570         case SCAN_ABORT_CMD:
571                 resp = (void *)pkt->data;
572                 if (*resp == CAN_ABORT_STATUS) {
573                         IWL_DEBUG_SCAN(mvm,
574                                        "Scan can be aborted, wait until completion\n");
575                         return false;
576                 }
577
578                 /*
579                  * If scan cannot be aborted, it means that we had a
580                  * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
581                  * ieee80211_scan_completed already.
582                  */
583                 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
584                                *resp);
585                 return true;
586
587         case SCAN_COMPLETE_NOTIFICATION:
588                 notif = (void *)pkt->data;
589                 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
590                                notif->status);
591                 return true;
592
593         default:
594                 WARN_ON(1);
595                 return false;
596         };
597 }
598
599 static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
600 {
601         struct iwl_notification_wait wait_scan_abort;
602         static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
603                                                SCAN_COMPLETE_NOTIFICATION };
604         int ret;
605
606         if (mvm->scan_status == IWL_MVM_SCAN_NONE)
607                 return 0;
608
609         if (iwl_mvm_is_radio_killed(mvm)) {
610                 ieee80211_scan_completed(mvm->hw, true);
611                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
612                 mvm->scan_status = IWL_MVM_SCAN_NONE;
613                 return 0;
614         }
615
616         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
617                                    scan_abort_notif,
618                                    ARRAY_SIZE(scan_abort_notif),
619                                    iwl_mvm_scan_abort_notif, NULL);
620
621         ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
622         if (ret) {
623                 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
624                 /* mac80211's state will be cleaned in the nic_restart flow */
625                 goto out_remove_notif;
626         }
627
628         return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
629
630 out_remove_notif:
631         iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
632         return ret;
633 }
634
635 int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
636                                            struct iwl_rx_cmd_buffer *rxb,
637                                            struct iwl_device_cmd *cmd)
638 {
639         struct iwl_rx_packet *pkt = rxb_addr(rxb);
640         u8 status, ebs_status;
641
642         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
643                 struct iwl_periodic_scan_complete *scan_notif;
644
645                 scan_notif = (void *)pkt->data;
646                 status = scan_notif->status;
647                 ebs_status = scan_notif->ebs_status;
648         } else  {
649                 struct iwl_scan_offload_complete *scan_notif;
650
651                 scan_notif = (void *)pkt->data;
652                 status = scan_notif->status;
653                 ebs_status = scan_notif->ebs_status;
654         }
655         /* scan status must be locked for proper checking */
656         lockdep_assert_held(&mvm->mutex);
657
658         IWL_DEBUG_SCAN(mvm,
659                        "%s completed, status %s, EBS status %s\n",
660                        mvm->scan_status == IWL_MVM_SCAN_SCHED ?
661                                 "Scheduled scan" : "Scan",
662                        status == IWL_SCAN_OFFLOAD_COMPLETED ?
663                                 "completed" : "aborted",
664                        ebs_status == IWL_SCAN_EBS_SUCCESS ?
665                                 "success" : "failed");
666
667
668         /* only call mac80211 completion if the stop was initiated by FW */
669         if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
670                 mvm->scan_status = IWL_MVM_SCAN_NONE;
671                 ieee80211_sched_scan_stopped(mvm->hw);
672         } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
673                 mvm->scan_status = IWL_MVM_SCAN_NONE;
674                 ieee80211_scan_completed(mvm->hw,
675                                          status == IWL_SCAN_OFFLOAD_ABORTED);
676                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
677         }
678
679         mvm->last_ebs_successful = !ebs_status;
680
681         return 0;
682 }
683
684 static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
685                                           struct ieee80211_vif *vif,
686                                           struct ieee80211_scan_ies *ies,
687                                           enum ieee80211_band band,
688                                           struct iwl_tx_cmd *cmd,
689                                           u8 *data)
690 {
691         u16 cmd_len;
692
693         cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
694         cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
695         cmd->sta_id = mvm->aux_sta.sta_id;
696
697         cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
698
699         cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
700                                          vif->addr,
701                                          1, NULL, 0,
702                                          ies->ies[band], ies->len[band],
703                                          ies->common_ies, ies->common_ie_len,
704                                          SCAN_OFFLOAD_PROBE_REQ_SIZE);
705         cmd->len = cpu_to_le16(cmd_len);
706 }
707
708 static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
709                                struct ieee80211_vif *vif,
710                                struct cfg80211_sched_scan_request *req,
711                                struct iwl_scan_offload_cmd *scan,
712                                struct iwl_mvm_scan_params *params)
713 {
714         scan->channel_count = req->n_channels;
715         scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
716         scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
717         scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
718         scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
719
720         scan->max_out_time = cpu_to_le32(params->max_out_time);
721         scan->suspend_time = cpu_to_le32(params->suspend_time);
722
723         scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
724                                           MAC_FILTER_IN_BEACON);
725         scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
726         scan->rep_count = cpu_to_le32(1);
727
728         if (params->passive_fragmented)
729                 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
730 }
731
732 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
733 {
734         int i;
735
736         for (i = 0; i < PROBE_OPTION_MAX; i++) {
737                 if (!ssid_list[i].len)
738                         break;
739                 if (ssid_list[i].len == ssid_len &&
740                     !memcmp(ssid_list->ssid, ssid, ssid_len))
741                         return i;
742         }
743         return -1;
744 }
745
746 static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
747                                         struct iwl_ssid_ie *direct_scan,
748                                         u32 *ssid_bitmap, bool basic_ssid)
749 {
750         int i, j;
751         int index;
752
753         /*
754          * copy SSIDs from match list.
755          * iwl_config_sched_scan_profiles() uses the order of these ssids to
756          * config match list.
757          */
758         for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
759                 /* skip empty SSID matchsets */
760                 if (!req->match_sets[i].ssid.ssid_len)
761                         continue;
762                 direct_scan[i].id = WLAN_EID_SSID;
763                 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
764                 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
765                        direct_scan[i].len);
766         }
767
768         /* add SSIDs from scan SSID list */
769         *ssid_bitmap = 0;
770         for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
771                 index = iwl_ssid_exist(req->ssids[j].ssid,
772                                        req->ssids[j].ssid_len,
773                                        direct_scan);
774                 if (index < 0) {
775                         if (!req->ssids[j].ssid_len && basic_ssid)
776                                 continue;
777                         direct_scan[i].id = WLAN_EID_SSID;
778                         direct_scan[i].len = req->ssids[j].ssid_len;
779                         memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
780                                direct_scan[i].len);
781                         *ssid_bitmap |= BIT(i + 1);
782                         i++;
783                 } else {
784                         *ssid_bitmap |= BIT(index + 1);
785                 }
786         }
787 }
788
789 static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
790                                   struct cfg80211_sched_scan_request *req,
791                                   u8 *channels_buffer,
792                                   enum ieee80211_band band,
793                                   int *head,
794                                   u32 ssid_bitmap,
795                                   struct iwl_mvm_scan_params *params)
796 {
797         u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
798         __le32 *type = (__le32 *)channels_buffer;
799         __le16 *channel_number = (__le16 *)(type + n_channels);
800         __le16 *iter_count = channel_number + n_channels;
801         __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
802         u8 *active_dwell = (u8 *)(iter_interval + n_channels);
803         u8 *passive_dwell = active_dwell + n_channels;
804         int i, index = 0;
805
806         for (i = 0; i < req->n_channels; i++) {
807                 struct ieee80211_channel *chan = req->channels[i];
808
809                 if (chan->band != band)
810                         continue;
811
812                 index = *head;
813                 (*head)++;
814
815                 channel_number[index] = cpu_to_le16(chan->hw_value);
816                 active_dwell[index] = params->dwell[band].active;
817                 passive_dwell[index] = params->dwell[band].passive;
818
819                 iter_count[index] = cpu_to_le16(1);
820                 iter_interval[index] = 0;
821
822                 if (!(chan->flags & IEEE80211_CHAN_NO_IR))
823                         type[index] |=
824                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
825
826                 type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
827                                            IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
828
829                 if (chan->flags & IEEE80211_CHAN_NO_HT40)
830                         type[index] |=
831                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
832
833                 /* scan for all SSIDs from req->ssids */
834                 type[index] |= cpu_to_le32(ssid_bitmap);
835         }
836 }
837
838 int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
839                               struct ieee80211_vif *vif,
840                               struct cfg80211_sched_scan_request *req,
841                               struct ieee80211_scan_ies *ies)
842 {
843         int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
844         int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
845         int head = 0;
846         u32 ssid_bitmap;
847         int cmd_len;
848         int ret;
849         u8 *probes;
850         bool basic_ssid = !(mvm->fw->ucode_capa.flags &
851                             IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
852
853         struct iwl_scan_offload_cfg *scan_cfg;
854         struct iwl_host_cmd cmd = {
855                 .id = SCAN_OFFLOAD_CONFIG_CMD,
856         };
857         struct iwl_mvm_scan_params params = {};
858
859         lockdep_assert_held(&mvm->mutex);
860
861         cmd_len = sizeof(struct iwl_scan_offload_cfg) +
862                   mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
863                   2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
864
865         scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
866         if (!scan_cfg)
867                 return -ENOMEM;
868
869         probes = scan_cfg->data +
870                 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
871
872         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
873         iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
874         scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
875
876         iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
877                                     &ssid_bitmap, basic_ssid);
878         /* build tx frames for supported bands */
879         if (band_2ghz) {
880                 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
881                                               IEEE80211_BAND_2GHZ,
882                                               &scan_cfg->scan_cmd.tx_cmd[0],
883                                               probes);
884                 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
885                                       IEEE80211_BAND_2GHZ, &head,
886                                       ssid_bitmap, &params);
887         }
888         if (band_5ghz) {
889                 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
890                                               IEEE80211_BAND_5GHZ,
891                                               &scan_cfg->scan_cmd.tx_cmd[1],
892                                               probes +
893                                                 SCAN_OFFLOAD_PROBE_REQ_SIZE);
894                 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
895                                       IEEE80211_BAND_5GHZ, &head,
896                                       ssid_bitmap, &params);
897         }
898
899         cmd.data[0] = scan_cfg;
900         cmd.len[0] = cmd_len;
901         cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
902
903         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
904
905         ret = iwl_mvm_send_cmd(mvm, &cmd);
906         kfree(scan_cfg);
907         return ret;
908 }
909
910 int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
911                                        struct cfg80211_sched_scan_request *req)
912 {
913         struct iwl_scan_offload_profile *profile;
914         struct iwl_scan_offload_profile_cfg *profile_cfg;
915         struct iwl_scan_offload_blacklist *blacklist;
916         struct iwl_host_cmd cmd = {
917                 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
918                 .len[1] = sizeof(*profile_cfg),
919                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
920                 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
921         };
922         int blacklist_len;
923         int i;
924         int ret;
925
926         if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
927                         return -EIO;
928
929         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
930                 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
931         else
932                 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
933
934         blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
935         if (!blacklist)
936                 return -ENOMEM;
937
938         profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
939         if (!profile_cfg) {
940                 ret = -ENOMEM;
941                 goto free_blacklist;
942         }
943
944         cmd.data[0] = blacklist;
945         cmd.len[0] = sizeof(*blacklist) * blacklist_len;
946         cmd.data[1] = profile_cfg;
947
948         /* No blacklist configuration */
949
950         profile_cfg->num_profiles = req->n_match_sets;
951         profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
952         profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
953         profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
954         if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
955                 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
956
957         for (i = 0; i < req->n_match_sets; i++) {
958                 profile = &profile_cfg->profiles[i];
959                 profile->ssid_index = i;
960                 /* Support any cipher and auth algorithm */
961                 profile->unicast_cipher = 0xff;
962                 profile->auth_alg = 0xff;
963                 profile->network_type = IWL_NETWORK_TYPE_ANY;
964                 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
965                 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
966         }
967
968         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
969
970         ret = iwl_mvm_send_cmd(mvm, &cmd);
971         kfree(profile_cfg);
972 free_blacklist:
973         kfree(blacklist);
974
975         return ret;
976 }
977
978 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
979                              struct cfg80211_sched_scan_request *req)
980 {
981         struct iwl_scan_offload_req scan_req = {
982                 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
983
984                 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
985                 .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
986                 .schedule_line[0].full_scan_mul = 1,
987
988                 .schedule_line[1].iterations = 0xff,
989                 .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
990                 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
991         };
992
993         if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
994                 IWL_DEBUG_SCAN(mvm,
995                                "Sending scheduled scan with filtering, filter len %d\n",
996                                req->n_match_sets);
997         } else {
998                 IWL_DEBUG_SCAN(mvm,
999                                "Sending Scheduled scan without filtering\n");
1000                 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
1001         }
1002
1003         if (mvm->last_ebs_successful &&
1004             mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
1005                 scan_req.flags |=
1006                         cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
1007
1008         return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
1009                                     sizeof(scan_req), &scan_req);
1010 }
1011
1012 int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
1013                                struct ieee80211_vif *vif,
1014                                struct cfg80211_sched_scan_request *req,
1015                                struct ieee80211_scan_ies *ies)
1016 {
1017         int ret;
1018
1019         if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
1020                 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1021                 if (ret)
1022                         return ret;
1023                 ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
1024         } else {
1025                 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1026                 if (ret)
1027                         return ret;
1028                 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1029                 if (ret)
1030                         return ret;
1031                 ret = iwl_mvm_sched_scan_start(mvm, req);
1032         }
1033
1034         return ret;
1035 }
1036
1037 static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
1038 {
1039         int ret;
1040         struct iwl_host_cmd cmd = {
1041                 .id = SCAN_OFFLOAD_ABORT_CMD,
1042         };
1043         u32 status;
1044
1045         /* Exit instantly with error when device is not ready
1046          * to receive scan abort command or it does not perform
1047          * scheduled scan currently */
1048         if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1049             (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1050              mvm->scan_status != IWL_MVM_SCAN_OS))
1051                 return -EIO;
1052
1053         ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
1054         if (ret)
1055                 return ret;
1056
1057         if (status != CAN_ABORT_STATUS) {
1058                 /*
1059                  * The scan abort will return 1 for success or
1060                  * 2 for "failure".  A failure condition can be
1061                  * due to simply not being in an active scan which
1062                  * can occur if we send the scan abort before the
1063                  * microcode has notified us that a scan is completed.
1064                  */
1065                 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
1066                 ret = -ENOENT;
1067         }
1068
1069         return ret;
1070 }
1071
1072 int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
1073 {
1074         int ret;
1075         struct iwl_notification_wait wait_scan_done;
1076         static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
1077         bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
1078
1079         lockdep_assert_held(&mvm->mutex);
1080
1081         if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1082             (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1083              mvm->scan_status != IWL_MVM_SCAN_OS)) {
1084                 IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
1085                 return 0;
1086         }
1087
1088         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1089                                    scan_done_notif,
1090                                    ARRAY_SIZE(scan_done_notif),
1091                                    NULL, NULL);
1092
1093         ret = iwl_mvm_send_scan_offload_abort(mvm);
1094         if (ret) {
1095                 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
1096                                sched ? "offloaded " : "", ret);
1097                 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1098                 return ret;
1099         }
1100
1101         IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
1102                        sched ? "offloaded " : "");
1103
1104         ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1105         if (ret)
1106                 return ret;
1107
1108         /*
1109          * Clear the scan status so the next scan requests will succeed. This
1110          * also ensures the Rx handler doesn't do anything, as the scan was
1111          * stopped from above. Since the rx handler won't do anything now,
1112          * we have to release the scan reference here.
1113          */
1114         if (mvm->scan_status == IWL_MVM_SCAN_OS)
1115                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1116
1117         mvm->scan_status = IWL_MVM_SCAN_NONE;
1118
1119         if (notify) {
1120                 if (sched)
1121                         ieee80211_sched_scan_stopped(mvm->hw);
1122                 else
1123                         ieee80211_scan_completed(mvm->hw, true);
1124         }
1125
1126         return 0;
1127 }
1128
1129 static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
1130                                              struct iwl_scan_req_tx_cmd *tx_cmd,
1131                                              bool no_cck)
1132 {
1133         tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1134                                          TX_CMD_FLG_BT_DIS);
1135         tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1136                                                            IEEE80211_BAND_2GHZ,
1137                                                            no_cck);
1138         tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
1139
1140         tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1141                                          TX_CMD_FLG_BT_DIS);
1142         tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1143                                                            IEEE80211_BAND_5GHZ,
1144                                                            no_cck);
1145         tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
1146 }
1147
1148 static void
1149 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
1150                                struct ieee80211_channel **channels,
1151                                int n_channels, u32 ssid_bitmap,
1152                                struct iwl_scan_req_unified_lmac *cmd)
1153 {
1154         struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
1155         int i;
1156
1157         for (i = 0; i < n_channels; i++) {
1158                 channel_cfg[i].channel_num =
1159                         cpu_to_le16(channels[i]->hw_value);
1160                 channel_cfg[i].iter_count = cpu_to_le16(1);
1161                 channel_cfg[i].iter_interval = 0;
1162                 channel_cfg[i].flags =
1163                         cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
1164                                     ssid_bitmap);
1165         }
1166 }
1167
1168 static void
1169 iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1170                                  struct ieee80211_scan_ies *ies,
1171                                  struct iwl_scan_req_unified_lmac *cmd)
1172 {
1173         struct iwl_scan_probe_req *preq = (void *)(cmd->data +
1174                 sizeof(struct iwl_scan_channel_cfg_lmac) *
1175                         mvm->fw->ucode_capa.n_scan_channels);
1176         struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
1177         u8 *pos;
1178
1179         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1180         eth_broadcast_addr(frame->da);
1181         memcpy(frame->sa, vif->addr, ETH_ALEN);
1182         eth_broadcast_addr(frame->bssid);
1183         frame->seq_ctrl = 0;
1184
1185         pos = frame->u.probe_req.variable;
1186         *pos++ = WLAN_EID_SSID;
1187         *pos++ = 0;
1188
1189         preq->mac_header.offset = 0;
1190         preq->mac_header.len = cpu_to_le16(24 + 2);
1191
1192         memcpy(pos, ies->ies[IEEE80211_BAND_2GHZ],
1193                ies->len[IEEE80211_BAND_2GHZ]);
1194         preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
1195         preq->band_data[0].len = cpu_to_le16(ies->len[IEEE80211_BAND_2GHZ]);
1196         pos += ies->len[IEEE80211_BAND_2GHZ];
1197
1198         memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
1199                ies->len[IEEE80211_BAND_5GHZ]);
1200         preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
1201         preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
1202         pos += ies->len[IEEE80211_BAND_5GHZ];
1203
1204         memcpy(pos, ies->common_ies, ies->common_ie_len);
1205         preq->common_data.offset = cpu_to_le16(pos - preq->buf);
1206         preq->common_data.len = cpu_to_le16(ies->common_ie_len);
1207 }
1208
1209 static void
1210 iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
1211                                        struct iwl_scan_req_unified_lmac *cmd,
1212                                        struct iwl_mvm_scan_params *params)
1213 {
1214         memset(cmd, 0, ksize(cmd));
1215         cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1216         cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1217         if (params->passive_fragmented)
1218                 cmd->fragmented_dwell =
1219                                 params->dwell[IEEE80211_BAND_2GHZ].passive;
1220         cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
1221         cmd->max_out_time = cpu_to_le32(params->max_out_time);
1222         cmd->suspend_time = cpu_to_le32(params->suspend_time);
1223         cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1224         cmd->iter_num = cpu_to_le32(1);
1225
1226         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1227             mvm->last_ebs_successful) {
1228                 cmd->channel_opt[0].flags =
1229                         cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1230                                     IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1231                                     IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1232                 cmd->channel_opt[1].flags =
1233                         cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1234                                     IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1235                                     IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1236         }
1237
1238         if (iwl_mvm_rrm_scan_needed(mvm))
1239                 cmd->scan_flags |=
1240                         cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
1241 }
1242
1243 int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
1244                               struct ieee80211_vif *vif,
1245                               struct ieee80211_scan_request *req)
1246 {
1247         struct iwl_host_cmd hcmd = {
1248                 .id = SCAN_OFFLOAD_REQUEST_CMD,
1249                 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1250                          sizeof(struct iwl_scan_channel_cfg_lmac) *
1251                                 mvm->fw->ucode_capa.n_scan_channels +
1252                          sizeof(struct iwl_scan_probe_req), },
1253                 .data = { mvm->scan_cmd, },
1254                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1255         };
1256         struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1257         struct iwl_mvm_scan_params params = {};
1258         u32 flags;
1259         int ssid_bitmap = 0;
1260         int ret, i;
1261
1262         lockdep_assert_held(&mvm->mutex);
1263
1264         /* we should have failed registration if scan_cmd was NULL */
1265         if (WARN_ON(mvm->scan_cmd == NULL))
1266                 return -ENOMEM;
1267
1268         if (req->req.n_ssids > PROBE_OPTION_MAX ||
1269             req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
1270             req->ies.len[NL80211_BAND_5GHZ] >
1271                 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
1272             req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
1273                 return -ENOBUFS;
1274
1275         mvm->scan_status = IWL_MVM_SCAN_OS;
1276
1277         iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1278                                  &params);
1279
1280         iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1281
1282         cmd->n_channels = (u8)req->req.n_channels;
1283
1284         flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1285
1286         if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
1287                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1288
1289         if (params.passive_fragmented)
1290                 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1291
1292         if (req->req.n_ssids == 0)
1293                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1294
1295         cmd->scan_flags |= cpu_to_le32(flags);
1296
1297         cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
1298         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1299                                         MAC_FILTER_IN_BEACON);
1300         iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
1301         iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
1302                                 req->req.n_ssids, 0);
1303
1304         cmd->schedule[0].delay = 0;
1305         cmd->schedule[0].iterations = 1;
1306         cmd->schedule[0].full_scan_mul = 0;
1307         cmd->schedule[1].delay = 0;
1308         cmd->schedule[1].iterations = 0;
1309         cmd->schedule[1].full_scan_mul = 0;
1310
1311         for (i = 1; i <= req->req.n_ssids; i++)
1312                 ssid_bitmap |= BIT(i);
1313
1314         iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
1315                                        req->req.n_channels, ssid_bitmap,
1316                                        cmd);
1317
1318         iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
1319
1320         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1321         if (!ret) {
1322                 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1323         } else {
1324                 /*
1325                  * If the scan failed, it usually means that the FW was unable
1326                  * to allocate the time events. Warn on it, but maybe we
1327                  * should try to send the command again with different params.
1328                  */
1329                 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1330                 mvm->scan_status = IWL_MVM_SCAN_NONE;
1331                 ret = -EIO;
1332         }
1333         return ret;
1334 }
1335
1336 int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
1337                                     struct ieee80211_vif *vif,
1338                                     struct cfg80211_sched_scan_request *req,
1339                                     struct ieee80211_scan_ies *ies)
1340 {
1341         struct iwl_host_cmd hcmd = {
1342                 .id = SCAN_OFFLOAD_REQUEST_CMD,
1343                 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1344                          sizeof(struct iwl_scan_channel_cfg_lmac) *
1345                                 mvm->fw->ucode_capa.n_scan_channels +
1346                          sizeof(struct iwl_scan_probe_req), },
1347                 .data = { mvm->scan_cmd, },
1348                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1349         };
1350         struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1351         struct iwl_mvm_scan_params params = {};
1352         int ret;
1353         u32 flags = 0, ssid_bitmap = 0;
1354
1355         lockdep_assert_held(&mvm->mutex);
1356
1357         /* we should have failed registration if scan_cmd was NULL */
1358         if (WARN_ON(mvm->scan_cmd == NULL))
1359                 return -ENOMEM;
1360
1361         if (req->n_ssids > PROBE_OPTION_MAX ||
1362             ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1363             ies->len[NL80211_BAND_5GHZ] >
1364                 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
1365             req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
1366                 return -ENOBUFS;
1367
1368         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
1369
1370         iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1371
1372         cmd->n_channels = (u8)req->n_channels;
1373
1374         if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
1375                 IWL_DEBUG_SCAN(mvm,
1376                                "Sending scheduled scan with filtering, n_match_sets %d\n",
1377                                req->n_match_sets);
1378         } else {
1379                 IWL_DEBUG_SCAN(mvm,
1380                                "Sending Scheduled scan without filtering\n");
1381                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1382         }
1383
1384         if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
1385                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1386
1387         if (params.passive_fragmented)
1388                 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1389
1390         if (req->n_ssids == 0)
1391                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1392
1393         cmd->scan_flags |= cpu_to_le32(flags);
1394
1395         cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
1396         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1397                                         MAC_FILTER_IN_BEACON);
1398         iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
1399         iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
1400
1401         cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
1402         cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1403         cmd->schedule[0].full_scan_mul = 1;
1404
1405         cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
1406         cmd->schedule[1].iterations = 0xff;
1407         cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1408
1409         iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1410                                        ssid_bitmap, cmd);
1411
1412         iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
1413
1414         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1415         if (!ret) {
1416                 IWL_DEBUG_SCAN(mvm,
1417                                "Sched scan request was sent successfully\n");
1418         } else {
1419                 /*
1420                  * If the scan failed, it usually means that the FW was unable
1421                  * to allocate the time events. Warn on it, but maybe we
1422                  * should try to send the command again with different params.
1423                  */
1424                 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1425                 mvm->scan_status = IWL_MVM_SCAN_NONE;
1426                 ret = -EIO;
1427         }
1428         return ret;
1429 }
1430
1431
1432 int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1433 {
1434         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1435                 return iwl_mvm_scan_offload_stop(mvm, true);
1436         return iwl_mvm_cancel_regular_scan(mvm);
1437 }