Merge tag 'fbdev-3.18' of git://git.kernel.org/pub/scm/linux/kernel/git/tomba/linux
[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 (mvmvif->phy_ctxt && mvmvif->phy_ctxt->id < MAX_PHYS)
274                 *global_bound = true;
275 }
276
277 static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
278                                      struct ieee80211_vif *vif,
279                                      int n_ssids, u32 flags,
280                                      struct iwl_mvm_scan_params *params)
281 {
282         bool global_bound = false;
283         enum ieee80211_band band;
284         u8 frag_passive_dwell = 0;
285
286         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
287                                             IEEE80211_IFACE_ITER_NORMAL,
288                                             iwl_mvm_scan_condition_iterator,
289                                             &global_bound);
290
291         if (!global_bound)
292                 goto not_bound;
293
294         params->suspend_time = 30;
295         params->max_out_time = 170;
296
297         if (iwl_mvm_low_latency(mvm)) {
298                 if (mvm->fw->ucode_capa.api[0] &
299                     IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
300                         params->suspend_time = 105;
301                         params->max_out_time = 70;
302                         frag_passive_dwell = 20;
303                 } else {
304                         params->suspend_time = 120;
305                         params->max_out_time = 120;
306                 }
307         }
308
309         if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
310                                    IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
311                 /*
312                  * P2P device scan should not be fragmented to avoid negative
313                  * impact on P2P device discovery. Configure max_out_time to be
314                  * equal to dwell time on passive channel. Take a longest
315                  * possible value, one that corresponds to 2GHz band
316                  */
317                 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
318                         u32 passive_dwell =
319                                 iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
320                         params->max_out_time = passive_dwell;
321                 } else {
322                         params->passive_fragmented = true;
323                 }
324         }
325
326         if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
327                 params->max_out_time = 200;
328
329 not_bound:
330
331         for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
332                 if (params->passive_fragmented)
333                         params->dwell[band].passive = frag_passive_dwell;
334                 else
335                         params->dwell[band].passive =
336                                 iwl_mvm_get_passive_dwell(band);
337                 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
338                                                                       n_ssids);
339         }
340 }
341
342 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
343 {
344         /* require rrm scan whenever the fw supports it */
345         return mvm->fw->ucode_capa.capa[0] &
346                IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
347 }
348
349 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
350                                            bool is_sched_scan)
351 {
352         int max_probe_len;
353
354         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
355                 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
356         else
357                 max_probe_len = mvm->fw->ucode_capa.max_probe_length;
358
359         /* we create the 802.11 header and SSID element */
360         max_probe_len -= 24 + 2;
361
362         /* basic ssid is added only for hw_scan with and old api */
363         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
364             !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
365             !is_sched_scan)
366                 max_probe_len -= 32;
367
368         return max_probe_len;
369 }
370
371 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
372 {
373         int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
374
375         if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
376                 return max_ie_len;
377
378         /* TODO: [BUG] This function should return the maximum allowed size of
379          * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
380          * in the same command. So the correct implementation of this function
381          * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
382          * command has only 512 bytes and it would leave us with about 240
383          * bytes for scan IEs, which is clearly not enough. So meanwhile
384          * we will report an incorrect value. This may result in a failure to
385          * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
386          * functions with -ENOBUFS, if a large enough probe will be provided.
387          */
388         return max_ie_len;
389 }
390
391 int iwl_mvm_scan_request(struct iwl_mvm *mvm,
392                          struct ieee80211_vif *vif,
393                          struct cfg80211_scan_request *req)
394 {
395         struct iwl_host_cmd hcmd = {
396                 .id = SCAN_REQUEST_CMD,
397                 .len = { 0, },
398                 .data = { mvm->scan_cmd, },
399                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
400         };
401         struct iwl_scan_cmd *cmd = mvm->scan_cmd;
402         int ret;
403         u32 status;
404         int ssid_len = 0;
405         u8 *ssid = NULL;
406         bool basic_ssid = !(mvm->fw->ucode_capa.flags &
407                            IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
408         struct iwl_mvm_scan_params params = {};
409
410         lockdep_assert_held(&mvm->mutex);
411
412         /* we should have failed registration if scan_cmd was NULL */
413         if (WARN_ON(mvm->scan_cmd == NULL))
414                 return -ENOMEM;
415
416         IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
417         mvm->scan_status = IWL_MVM_SCAN_OS;
418         memset(cmd, 0, ksize(cmd));
419
420         cmd->channel_count = (u8)req->n_channels;
421         cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
422         cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
423         cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
424
425         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
426         cmd->max_out_time = cpu_to_le32(params.max_out_time);
427         cmd->suspend_time = cpu_to_le32(params.suspend_time);
428         if (params.passive_fragmented)
429                 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
430
431         cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
432         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
433                                         MAC_FILTER_IN_BEACON);
434
435         if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
436                 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
437         else
438                 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
439
440         cmd->repeats = cpu_to_le32(1);
441
442         /*
443          * If the user asked for passive scan, don't change to active scan if
444          * you see any activity on the channel - remain passive.
445          */
446         if (req->n_ssids > 0) {
447                 cmd->passive2active = cpu_to_le16(1);
448                 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
449                 if (basic_ssid) {
450                         ssid = req->ssids[0].ssid;
451                         ssid_len = req->ssids[0].ssid_len;
452                 }
453         } else {
454                 cmd->passive2active = 0;
455                 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
456         }
457
458         iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
459                                 basic_ssid ? 1 : 0);
460
461         cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
462                                            TX_CMD_FLG_BT_DIS);
463         cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
464         cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
465         cmd->tx_cmd.rate_n_flags =
466                         iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
467                                                   req->no_cck);
468
469         cmd->tx_cmd.len =
470                 cpu_to_le16(iwl_mvm_fill_probe_req(
471                             (struct ieee80211_mgmt *)cmd->data,
472                             vif->addr,
473                             req->n_ssids, ssid, ssid_len,
474                             req->ie, req->ie_len, NULL, 0,
475                             mvm->fw->ucode_capa.max_probe_length));
476
477         iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
478
479         cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
480                 le16_to_cpu(cmd->tx_cmd.len) +
481                 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
482         hcmd.len[0] = le16_to_cpu(cmd->len);
483
484         status = SCAN_RESPONSE_OK;
485         ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
486         if (!ret && status == SCAN_RESPONSE_OK) {
487                 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
488         } else {
489                 /*
490                  * If the scan failed, it usually means that the FW was unable
491                  * to allocate the time events. Warn on it, but maybe we
492                  * should try to send the command again with different params.
493                  */
494                 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
495                         status, ret);
496                 mvm->scan_status = IWL_MVM_SCAN_NONE;
497                 ret = -EIO;
498         }
499         return ret;
500 }
501
502 int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
503                           struct iwl_device_cmd *cmd)
504 {
505         struct iwl_rx_packet *pkt = rxb_addr(rxb);
506         struct iwl_cmd_response *resp = (void *)pkt->data;
507
508         IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
509                        le32_to_cpu(resp->status));
510         return 0;
511 }
512
513 int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
514                           struct iwl_device_cmd *cmd)
515 {
516         struct iwl_rx_packet *pkt = rxb_addr(rxb);
517         struct iwl_scan_complete_notif *notif = (void *)pkt->data;
518
519         lockdep_assert_held(&mvm->mutex);
520
521         IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
522                        notif->status, notif->scanned_channels);
523
524         if (mvm->scan_status == IWL_MVM_SCAN_OS)
525                 mvm->scan_status = IWL_MVM_SCAN_NONE;
526         ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
527
528         iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
529
530         return 0;
531 }
532
533 int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
534                                     struct iwl_rx_cmd_buffer *rxb,
535                                     struct iwl_device_cmd *cmd)
536 {
537         struct iwl_rx_packet *pkt = rxb_addr(rxb);
538         u8 client_bitmap = 0;
539
540         if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
541                 struct iwl_sched_scan_results *notif = (void *)pkt->data;
542
543                 client_bitmap = notif->client_bitmap;
544         }
545
546         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN ||
547             client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
548                 if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
549                         IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
550                         ieee80211_sched_scan_results(mvm->hw);
551                 } else {
552                         IWL_DEBUG_SCAN(mvm, "Scan results\n");
553                 }
554         }
555
556         return 0;
557 }
558
559 static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
560                                      struct iwl_rx_packet *pkt, void *data)
561 {
562         struct iwl_mvm *mvm =
563                 container_of(notif_wait, struct iwl_mvm, notif_wait);
564         struct iwl_scan_complete_notif *notif;
565         u32 *resp;
566
567         switch (pkt->hdr.cmd) {
568         case SCAN_ABORT_CMD:
569                 resp = (void *)pkt->data;
570                 if (*resp == CAN_ABORT_STATUS) {
571                         IWL_DEBUG_SCAN(mvm,
572                                        "Scan can be aborted, wait until completion\n");
573                         return false;
574                 }
575
576                 /*
577                  * If scan cannot be aborted, it means that we had a
578                  * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
579                  * ieee80211_scan_completed already.
580                  */
581                 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
582                                *resp);
583                 return true;
584
585         case SCAN_COMPLETE_NOTIFICATION:
586                 notif = (void *)pkt->data;
587                 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
588                                notif->status);
589                 return true;
590
591         default:
592                 WARN_ON(1);
593                 return false;
594         };
595 }
596
597 static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
598 {
599         struct iwl_notification_wait wait_scan_abort;
600         static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
601                                                SCAN_COMPLETE_NOTIFICATION };
602         int ret;
603
604         if (mvm->scan_status == IWL_MVM_SCAN_NONE)
605                 return 0;
606
607         if (iwl_mvm_is_radio_killed(mvm)) {
608                 ieee80211_scan_completed(mvm->hw, true);
609                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
610                 mvm->scan_status = IWL_MVM_SCAN_NONE;
611                 return 0;
612         }
613
614         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
615                                    scan_abort_notif,
616                                    ARRAY_SIZE(scan_abort_notif),
617                                    iwl_mvm_scan_abort_notif, NULL);
618
619         ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
620         if (ret) {
621                 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
622                 /* mac80211's state will be cleaned in the nic_restart flow */
623                 goto out_remove_notif;
624         }
625
626         return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
627
628 out_remove_notif:
629         iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
630         return ret;
631 }
632
633 int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
634                                            struct iwl_rx_cmd_buffer *rxb,
635                                            struct iwl_device_cmd *cmd)
636 {
637         struct iwl_rx_packet *pkt = rxb_addr(rxb);
638         u8 status, ebs_status;
639
640         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
641                 struct iwl_periodic_scan_complete *scan_notif;
642
643                 scan_notif = (void *)pkt->data;
644                 status = scan_notif->status;
645                 ebs_status = scan_notif->ebs_status;
646         } else  {
647                 struct iwl_scan_offload_complete *scan_notif;
648
649                 scan_notif = (void *)pkt->data;
650                 status = scan_notif->status;
651                 ebs_status = scan_notif->ebs_status;
652         }
653         /* scan status must be locked for proper checking */
654         lockdep_assert_held(&mvm->mutex);
655
656         IWL_DEBUG_SCAN(mvm,
657                        "%s completed, status %s, EBS status %s\n",
658                        mvm->scan_status == IWL_MVM_SCAN_SCHED ?
659                                 "Scheduled scan" : "Scan",
660                        status == IWL_SCAN_OFFLOAD_COMPLETED ?
661                                 "completed" : "aborted",
662                        ebs_status == IWL_SCAN_EBS_SUCCESS ?
663                                 "success" : "failed");
664
665
666         /* only call mac80211 completion if the stop was initiated by FW */
667         if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
668                 mvm->scan_status = IWL_MVM_SCAN_NONE;
669                 ieee80211_sched_scan_stopped(mvm->hw);
670         } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
671                 mvm->scan_status = IWL_MVM_SCAN_NONE;
672                 ieee80211_scan_completed(mvm->hw,
673                                          status == IWL_SCAN_OFFLOAD_ABORTED);
674         }
675
676         mvm->last_ebs_successful = !ebs_status;
677
678         return 0;
679 }
680
681 static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
682                                           struct ieee80211_vif *vif,
683                                           struct ieee80211_scan_ies *ies,
684                                           enum ieee80211_band band,
685                                           struct iwl_tx_cmd *cmd,
686                                           u8 *data)
687 {
688         u16 cmd_len;
689
690         cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
691         cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
692         cmd->sta_id = mvm->aux_sta.sta_id;
693
694         cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
695
696         cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
697                                          vif->addr,
698                                          1, NULL, 0,
699                                          ies->ies[band], ies->len[band],
700                                          ies->common_ies, ies->common_ie_len,
701                                          SCAN_OFFLOAD_PROBE_REQ_SIZE);
702         cmd->len = cpu_to_le16(cmd_len);
703 }
704
705 static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
706                                struct ieee80211_vif *vif,
707                                struct cfg80211_sched_scan_request *req,
708                                struct iwl_scan_offload_cmd *scan,
709                                struct iwl_mvm_scan_params *params)
710 {
711         scan->channel_count = req->n_channels;
712         scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
713         scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
714         scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
715         scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
716
717         scan->max_out_time = cpu_to_le32(params->max_out_time);
718         scan->suspend_time = cpu_to_le32(params->suspend_time);
719
720         scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
721                                           MAC_FILTER_IN_BEACON);
722         scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
723         scan->rep_count = cpu_to_le32(1);
724
725         if (params->passive_fragmented)
726                 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
727 }
728
729 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
730 {
731         int i;
732
733         for (i = 0; i < PROBE_OPTION_MAX; i++) {
734                 if (!ssid_list[i].len)
735                         break;
736                 if (ssid_list[i].len == ssid_len &&
737                     !memcmp(ssid_list->ssid, ssid, ssid_len))
738                         return i;
739         }
740         return -1;
741 }
742
743 static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
744                                         struct iwl_ssid_ie *direct_scan,
745                                         u32 *ssid_bitmap, bool basic_ssid)
746 {
747         int i, j;
748         int index;
749
750         /*
751          * copy SSIDs from match list.
752          * iwl_config_sched_scan_profiles() uses the order of these ssids to
753          * config match list.
754          */
755         for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
756                 /* skip empty SSID matchsets */
757                 if (!req->match_sets[i].ssid.ssid_len)
758                         continue;
759                 direct_scan[i].id = WLAN_EID_SSID;
760                 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
761                 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
762                        direct_scan[i].len);
763         }
764
765         /* add SSIDs from scan SSID list */
766         *ssid_bitmap = 0;
767         for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
768                 index = iwl_ssid_exist(req->ssids[j].ssid,
769                                        req->ssids[j].ssid_len,
770                                        direct_scan);
771                 if (index < 0) {
772                         if (!req->ssids[j].ssid_len && basic_ssid)
773                                 continue;
774                         direct_scan[i].id = WLAN_EID_SSID;
775                         direct_scan[i].len = req->ssids[j].ssid_len;
776                         memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
777                                direct_scan[i].len);
778                         *ssid_bitmap |= BIT(i + 1);
779                         i++;
780                 } else {
781                         *ssid_bitmap |= BIT(index + 1);
782                 }
783         }
784 }
785
786 static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
787                                   struct cfg80211_sched_scan_request *req,
788                                   u8 *channels_buffer,
789                                   enum ieee80211_band band,
790                                   int *head,
791                                   u32 ssid_bitmap,
792                                   struct iwl_mvm_scan_params *params)
793 {
794         u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
795         __le32 *type = (__le32 *)channels_buffer;
796         __le16 *channel_number = (__le16 *)(type + n_channels);
797         __le16 *iter_count = channel_number + n_channels;
798         __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
799         u8 *active_dwell = (u8 *)(iter_interval + n_channels);
800         u8 *passive_dwell = active_dwell + n_channels;
801         int i, index = 0;
802
803         for (i = 0; i < req->n_channels; i++) {
804                 struct ieee80211_channel *chan = req->channels[i];
805
806                 if (chan->band != band)
807                         continue;
808
809                 index = *head;
810                 (*head)++;
811
812                 channel_number[index] = cpu_to_le16(chan->hw_value);
813                 active_dwell[index] = params->dwell[band].active;
814                 passive_dwell[index] = params->dwell[band].passive;
815
816                 iter_count[index] = cpu_to_le16(1);
817                 iter_interval[index] = 0;
818
819                 if (!(chan->flags & IEEE80211_CHAN_NO_IR))
820                         type[index] |=
821                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
822
823                 type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
824                                            IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
825
826                 if (chan->flags & IEEE80211_CHAN_NO_HT40)
827                         type[index] |=
828                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
829
830                 /* scan for all SSIDs from req->ssids */
831                 type[index] |= cpu_to_le32(ssid_bitmap);
832         }
833 }
834
835 int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
836                               struct ieee80211_vif *vif,
837                               struct cfg80211_sched_scan_request *req,
838                               struct ieee80211_scan_ies *ies)
839 {
840         int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
841         int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
842         int head = 0;
843         u32 ssid_bitmap;
844         int cmd_len;
845         int ret;
846         u8 *probes;
847         bool basic_ssid = !(mvm->fw->ucode_capa.flags &
848                             IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
849
850         struct iwl_scan_offload_cfg *scan_cfg;
851         struct iwl_host_cmd cmd = {
852                 .id = SCAN_OFFLOAD_CONFIG_CMD,
853         };
854         struct iwl_mvm_scan_params params = {};
855
856         lockdep_assert_held(&mvm->mutex);
857
858         cmd_len = sizeof(struct iwl_scan_offload_cfg) +
859                   mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
860                   2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
861
862         scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
863         if (!scan_cfg)
864                 return -ENOMEM;
865
866         probes = scan_cfg->data +
867                 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
868
869         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
870         iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
871         scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
872
873         iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
874                                     &ssid_bitmap, basic_ssid);
875         /* build tx frames for supported bands */
876         if (band_2ghz) {
877                 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
878                                               IEEE80211_BAND_2GHZ,
879                                               &scan_cfg->scan_cmd.tx_cmd[0],
880                                               probes);
881                 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
882                                       IEEE80211_BAND_2GHZ, &head,
883                                       ssid_bitmap, &params);
884         }
885         if (band_5ghz) {
886                 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
887                                               IEEE80211_BAND_5GHZ,
888                                               &scan_cfg->scan_cmd.tx_cmd[1],
889                                               probes +
890                                                 SCAN_OFFLOAD_PROBE_REQ_SIZE);
891                 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
892                                       IEEE80211_BAND_5GHZ, &head,
893                                       ssid_bitmap, &params);
894         }
895
896         cmd.data[0] = scan_cfg;
897         cmd.len[0] = cmd_len;
898         cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
899
900         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
901
902         ret = iwl_mvm_send_cmd(mvm, &cmd);
903         kfree(scan_cfg);
904         return ret;
905 }
906
907 int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
908                                        struct cfg80211_sched_scan_request *req)
909 {
910         struct iwl_scan_offload_profile *profile;
911         struct iwl_scan_offload_profile_cfg *profile_cfg;
912         struct iwl_scan_offload_blacklist *blacklist;
913         struct iwl_host_cmd cmd = {
914                 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
915                 .len[1] = sizeof(*profile_cfg),
916                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
917                 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
918         };
919         int blacklist_len;
920         int i;
921         int ret;
922
923         if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
924                         return -EIO;
925
926         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
927                 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
928         else
929                 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
930
931         blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
932         if (!blacklist)
933                 return -ENOMEM;
934
935         profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
936         if (!profile_cfg) {
937                 ret = -ENOMEM;
938                 goto free_blacklist;
939         }
940
941         cmd.data[0] = blacklist;
942         cmd.len[0] = sizeof(*blacklist) * blacklist_len;
943         cmd.data[1] = profile_cfg;
944
945         /* No blacklist configuration */
946
947         profile_cfg->num_profiles = req->n_match_sets;
948         profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
949         profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
950         profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
951         if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
952                 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
953
954         for (i = 0; i < req->n_match_sets; i++) {
955                 profile = &profile_cfg->profiles[i];
956                 profile->ssid_index = i;
957                 /* Support any cipher and auth algorithm */
958                 profile->unicast_cipher = 0xff;
959                 profile->auth_alg = 0xff;
960                 profile->network_type = IWL_NETWORK_TYPE_ANY;
961                 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
962                 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
963         }
964
965         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
966
967         ret = iwl_mvm_send_cmd(mvm, &cmd);
968         kfree(profile_cfg);
969 free_blacklist:
970         kfree(blacklist);
971
972         return ret;
973 }
974
975 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
976                              struct cfg80211_sched_scan_request *req)
977 {
978         struct iwl_scan_offload_req scan_req = {
979                 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
980
981                 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
982                 .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
983                 .schedule_line[0].full_scan_mul = 1,
984
985                 .schedule_line[1].iterations = 0xff,
986                 .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
987                 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
988         };
989
990         if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
991                 IWL_DEBUG_SCAN(mvm,
992                                "Sending scheduled scan with filtering, filter len %d\n",
993                                req->n_match_sets);
994         } else {
995                 IWL_DEBUG_SCAN(mvm,
996                                "Sending Scheduled scan without filtering\n");
997                 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
998         }
999
1000         if (mvm->last_ebs_successful &&
1001             mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
1002                 scan_req.flags |=
1003                         cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
1004
1005         return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
1006                                     sizeof(scan_req), &scan_req);
1007 }
1008
1009 static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
1010 {
1011         int ret;
1012         struct iwl_host_cmd cmd = {
1013                 .id = SCAN_OFFLOAD_ABORT_CMD,
1014         };
1015         u32 status;
1016
1017         /* Exit instantly with error when device is not ready
1018          * to receive scan abort command or it does not perform
1019          * scheduled scan currently */
1020         if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1021             (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1022              mvm->scan_status != IWL_MVM_SCAN_OS))
1023                 return -EIO;
1024
1025         ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
1026         if (ret)
1027                 return ret;
1028
1029         if (status != CAN_ABORT_STATUS) {
1030                 /*
1031                  * The scan abort will return 1 for success or
1032                  * 2 for "failure".  A failure condition can be
1033                  * due to simply not being in an active scan which
1034                  * can occur if we send the scan abort before the
1035                  * microcode has notified us that a scan is completed.
1036                  */
1037                 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
1038                 ret = -ENOENT;
1039         }
1040
1041         return ret;
1042 }
1043
1044 int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
1045 {
1046         int ret;
1047         struct iwl_notification_wait wait_scan_done;
1048         static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
1049         bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
1050
1051         lockdep_assert_held(&mvm->mutex);
1052
1053         if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1054             (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1055              mvm->scan_status != IWL_MVM_SCAN_OS)) {
1056                 IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
1057                 return 0;
1058         }
1059
1060         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1061                                    scan_done_notif,
1062                                    ARRAY_SIZE(scan_done_notif),
1063                                    NULL, NULL);
1064
1065         ret = iwl_mvm_send_scan_offload_abort(mvm);
1066         if (ret) {
1067                 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
1068                                sched ? "offloaded " : "", ret);
1069                 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1070                 return ret;
1071         }
1072
1073         IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
1074                        sched ? "offloaded " : "");
1075
1076         ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1077         if (ret)
1078                 return ret;
1079
1080         /*
1081          * Clear the scan status so the next scan requests will succeed. This
1082          * also ensures the Rx handler doesn't do anything, as the scan was
1083          * stopped from above.
1084          */
1085         mvm->scan_status = IWL_MVM_SCAN_NONE;
1086
1087         if (notify) {
1088                 if (sched)
1089                         ieee80211_sched_scan_stopped(mvm->hw);
1090                 else
1091                         ieee80211_scan_completed(mvm->hw, true);
1092         }
1093
1094         return 0;
1095 }
1096
1097 static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
1098                                              struct iwl_scan_req_tx_cmd *tx_cmd,
1099                                              bool no_cck)
1100 {
1101         tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1102                                          TX_CMD_FLG_BT_DIS);
1103         tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1104                                                            IEEE80211_BAND_2GHZ,
1105                                                            no_cck);
1106         tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
1107
1108         tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1109                                          TX_CMD_FLG_BT_DIS);
1110         tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1111                                                            IEEE80211_BAND_5GHZ,
1112                                                            no_cck);
1113         tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
1114 }
1115
1116 static void
1117 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
1118                                struct ieee80211_channel **channels,
1119                                int n_channels, u32 ssid_bitmap,
1120                                struct iwl_scan_req_unified_lmac *cmd)
1121 {
1122         struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
1123         int i;
1124
1125         for (i = 0; i < n_channels; i++) {
1126                 channel_cfg[i].channel_num =
1127                         cpu_to_le16(channels[i]->hw_value);
1128                 channel_cfg[i].iter_count = cpu_to_le16(1);
1129                 channel_cfg[i].iter_interval = 0;
1130                 channel_cfg[i].flags =
1131                         cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
1132                                     ssid_bitmap);
1133         }
1134 }
1135
1136 static void
1137 iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1138                                  struct ieee80211_scan_ies *ies,
1139                                  struct iwl_scan_req_unified_lmac *cmd)
1140 {
1141         struct iwl_scan_probe_req *preq = (void *)(cmd->data +
1142                 sizeof(struct iwl_scan_channel_cfg_lmac) *
1143                         mvm->fw->ucode_capa.n_scan_channels);
1144         struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
1145         u8 *pos;
1146
1147         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1148         eth_broadcast_addr(frame->da);
1149         memcpy(frame->sa, vif->addr, ETH_ALEN);
1150         eth_broadcast_addr(frame->bssid);
1151         frame->seq_ctrl = 0;
1152
1153         pos = frame->u.probe_req.variable;
1154         *pos++ = WLAN_EID_SSID;
1155         *pos++ = 0;
1156
1157         preq->mac_header.offset = 0;
1158         preq->mac_header.len = cpu_to_le16(24 + 2);
1159
1160         memcpy(pos, ies->ies[IEEE80211_BAND_2GHZ],
1161                ies->len[IEEE80211_BAND_2GHZ]);
1162         preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
1163         preq->band_data[0].len = cpu_to_le16(ies->len[IEEE80211_BAND_2GHZ]);
1164         pos += ies->len[IEEE80211_BAND_2GHZ];
1165
1166         memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
1167                ies->len[IEEE80211_BAND_5GHZ]);
1168         preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
1169         preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
1170         pos += ies->len[IEEE80211_BAND_5GHZ];
1171
1172         memcpy(pos, ies->common_ies, ies->common_ie_len);
1173         preq->common_data.offset = cpu_to_le16(pos - preq->buf);
1174         preq->common_data.len = cpu_to_le16(ies->common_ie_len);
1175 }
1176
1177 static void
1178 iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
1179                                        struct iwl_scan_req_unified_lmac *cmd,
1180                                        struct iwl_mvm_scan_params *params)
1181 {
1182         memset(cmd, 0, ksize(cmd));
1183         cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1184         cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1185         if (params->passive_fragmented)
1186                 cmd->fragmented_dwell =
1187                                 params->dwell[IEEE80211_BAND_2GHZ].passive;
1188         cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
1189         cmd->max_out_time = cpu_to_le32(params->max_out_time);
1190         cmd->suspend_time = cpu_to_le32(params->suspend_time);
1191         cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1192         cmd->iter_num = cpu_to_le32(1);
1193
1194         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1195             mvm->last_ebs_successful) {
1196                 cmd->channel_opt[0].flags =
1197                         cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1198                                     IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1199                                     IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1200                 cmd->channel_opt[1].flags =
1201                         cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1202                                     IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1203                                     IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1204         }
1205
1206         if (iwl_mvm_rrm_scan_needed(mvm))
1207                 cmd->scan_flags |=
1208                         cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
1209 }
1210
1211 int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
1212                               struct ieee80211_vif *vif,
1213                               struct ieee80211_scan_request *req)
1214 {
1215         struct iwl_host_cmd hcmd = {
1216                 .id = SCAN_OFFLOAD_REQUEST_CMD,
1217                 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1218                          sizeof(struct iwl_scan_channel_cfg_lmac) *
1219                                 mvm->fw->ucode_capa.n_scan_channels +
1220                          sizeof(struct iwl_scan_probe_req), },
1221                 .data = { mvm->scan_cmd, },
1222                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1223         };
1224         struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1225         struct iwl_mvm_scan_params params = {};
1226         u32 flags;
1227         int ssid_bitmap = 0;
1228         int ret, i;
1229
1230         lockdep_assert_held(&mvm->mutex);
1231
1232         /* we should have failed registration if scan_cmd was NULL */
1233         if (WARN_ON(mvm->scan_cmd == NULL))
1234                 return -ENOMEM;
1235
1236         if (req->req.n_ssids > PROBE_OPTION_MAX ||
1237             req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
1238             req->ies.len[NL80211_BAND_5GHZ] >
1239                 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
1240             req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
1241                 return -ENOBUFS;
1242
1243         mvm->scan_status = IWL_MVM_SCAN_OS;
1244
1245         iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1246                                  &params);
1247
1248         iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1249
1250         cmd->n_channels = (u8)req->req.n_channels;
1251
1252         flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1253
1254         if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
1255                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1256
1257         if (params.passive_fragmented)
1258                 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1259
1260         if (req->req.n_ssids == 0)
1261                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1262
1263         cmd->scan_flags |= cpu_to_le32(flags);
1264
1265         cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
1266         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1267                                         MAC_FILTER_IN_BEACON);
1268         iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
1269         iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
1270                                 req->req.n_ssids, 0);
1271
1272         cmd->schedule[0].delay = 0;
1273         cmd->schedule[0].iterations = 1;
1274         cmd->schedule[0].full_scan_mul = 0;
1275         cmd->schedule[1].delay = 0;
1276         cmd->schedule[1].iterations = 0;
1277         cmd->schedule[1].full_scan_mul = 0;
1278
1279         for (i = 1; i <= req->req.n_ssids; i++)
1280                 ssid_bitmap |= BIT(i);
1281
1282         iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
1283                                        req->req.n_channels, ssid_bitmap,
1284                                        cmd);
1285
1286         iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, cmd);
1287
1288         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1289         if (!ret) {
1290                 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1291         } else {
1292                 /*
1293                  * If the scan failed, it usually means that the FW was unable
1294                  * to allocate the time events. Warn on it, but maybe we
1295                  * should try to send the command again with different params.
1296                  */
1297                 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1298                 mvm->scan_status = IWL_MVM_SCAN_NONE;
1299                 ret = -EIO;
1300         }
1301         return ret;
1302 }
1303
1304 int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
1305                                     struct ieee80211_vif *vif,
1306                                     struct cfg80211_sched_scan_request *req,
1307                                     struct ieee80211_scan_ies *ies)
1308 {
1309         struct iwl_host_cmd hcmd = {
1310                 .id = SCAN_OFFLOAD_REQUEST_CMD,
1311                 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1312                          sizeof(struct iwl_scan_channel_cfg_lmac) *
1313                                 mvm->fw->ucode_capa.n_scan_channels +
1314                          sizeof(struct iwl_scan_probe_req), },
1315                 .data = { mvm->scan_cmd, },
1316                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1317         };
1318         struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1319         struct iwl_mvm_scan_params params = {};
1320         int ret;
1321         u32 flags = 0, ssid_bitmap = 0;
1322
1323         lockdep_assert_held(&mvm->mutex);
1324
1325         /* we should have failed registration if scan_cmd was NULL */
1326         if (WARN_ON(mvm->scan_cmd == NULL))
1327                 return -ENOMEM;
1328
1329         if (req->n_ssids > PROBE_OPTION_MAX ||
1330             ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1331             ies->len[NL80211_BAND_5GHZ] >
1332                 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
1333             req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
1334                 return -ENOBUFS;
1335
1336         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
1337
1338         iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1339
1340         cmd->n_channels = (u8)req->n_channels;
1341
1342         if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
1343                 IWL_DEBUG_SCAN(mvm,
1344                                "Sending scheduled scan with filtering, n_match_sets %d\n",
1345                                req->n_match_sets);
1346         } else {
1347                 IWL_DEBUG_SCAN(mvm,
1348                                "Sending Scheduled scan without filtering\n");
1349                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1350         }
1351
1352         if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
1353                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1354
1355         if (params.passive_fragmented)
1356                 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1357
1358         if (req->n_ssids == 0)
1359                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1360
1361         cmd->scan_flags |= cpu_to_le32(flags);
1362
1363         cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
1364         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1365                                         MAC_FILTER_IN_BEACON);
1366         iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
1367         iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
1368
1369         cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
1370         cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1371         cmd->schedule[0].full_scan_mul = 1;
1372
1373         cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
1374         cmd->schedule[1].iterations = 0xff;
1375         cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1376
1377         iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1378                                        ssid_bitmap, cmd);
1379
1380         iwl_mvm_build_unified_scan_probe(mvm, vif, ies, cmd);
1381
1382         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1383         if (!ret) {
1384                 IWL_DEBUG_SCAN(mvm,
1385                                "Sched scan request was sent successfully\n");
1386         } else {
1387                 /*
1388                  * If the scan failed, it usually means that the FW was unable
1389                  * to allocate the time events. Warn on it, but maybe we
1390                  * should try to send the command again with different params.
1391                  */
1392                 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1393                 mvm->scan_status = IWL_MVM_SCAN_NONE;
1394                 ret = -EIO;
1395         }
1396         return ret;
1397 }
1398
1399
1400 int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1401 {
1402         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1403                 return iwl_mvm_scan_offload_stop(mvm, true);
1404         return iwl_mvm_cancel_regular_scan(mvm);
1405 }