Merge tag 'iwlwifi-next-for-kalle-2014-12-30' of https://git.kernel.org/pub/scm/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 #define IWL_DENSE_EBS_SCAN_RATIO 5
76 #define IWL_SPARSE_EBS_SCAN_RATIO 1
77
78 struct iwl_mvm_scan_params {
79         u32 max_out_time;
80         u32 suspend_time;
81         bool passive_fragmented;
82         struct _dwell {
83                 u16 passive;
84                 u16 active;
85         } dwell[IEEE80211_NUM_BANDS];
86 };
87
88 enum iwl_umac_scan_uid_type {
89         IWL_UMAC_SCAN_UID_REG_SCAN      = BIT(0),
90         IWL_UMAC_SCAN_UID_SCHED_SCAN    = BIT(1),
91         IWL_UMAC_SCAN_UID_ALL           = IWL_UMAC_SCAN_UID_REG_SCAN |
92                                           IWL_UMAC_SCAN_UID_SCHED_SCAN,
93 };
94
95 static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
96                               enum iwl_umac_scan_uid_type type, bool notify);
97
98 static u8 iwl_mvm_scan_rx_ant(struct iwl_mvm *mvm)
99 {
100         if (mvm->scan_rx_ant != ANT_NONE)
101                 return mvm->scan_rx_ant;
102         return iwl_mvm_get_valid_rx_ant(mvm);
103 }
104
105 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
106 {
107         u16 rx_chain;
108         u8 rx_ant;
109
110         rx_ant = iwl_mvm_scan_rx_ant(mvm);
111         rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
112         rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
113         rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
114         rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
115         return cpu_to_le16(rx_chain);
116 }
117
118 static __le32 iwl_mvm_scan_rxon_flags(enum ieee80211_band band)
119 {
120         if (band == IEEE80211_BAND_2GHZ)
121                 return cpu_to_le32(PHY_BAND_24);
122         else
123                 return cpu_to_le32(PHY_BAND_5);
124 }
125
126 static inline __le32
127 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
128                           bool no_cck)
129 {
130         u32 tx_ant;
131
132         mvm->scan_last_antenna_idx =
133                 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
134                                      mvm->scan_last_antenna_idx);
135         tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
136
137         if (band == IEEE80211_BAND_2GHZ && !no_cck)
138                 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
139                                    tx_ant);
140         else
141                 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
142 }
143
144 /*
145  * We insert the SSIDs in an inverted order, because the FW will
146  * invert it back. The most prioritized SSID, which is first in the
147  * request list, is not copied here, but inserted directly to the probe
148  * request.
149  */
150 static void iwl_mvm_scan_fill_ssids(struct iwl_ssid_ie *cmd_ssid,
151                                     struct cfg80211_ssid *ssids,
152                                     int n_ssids, int first)
153 {
154         int fw_idx, req_idx;
155
156         for (req_idx = n_ssids - 1, fw_idx = 0; req_idx >= first;
157              req_idx--, fw_idx++) {
158                 cmd_ssid[fw_idx].id = WLAN_EID_SSID;
159                 cmd_ssid[fw_idx].len = ssids[req_idx].ssid_len;
160                 memcpy(cmd_ssid[fw_idx].ssid,
161                        ssids[req_idx].ssid,
162                        ssids[req_idx].ssid_len);
163         }
164 }
165
166 /*
167  * If req->n_ssids > 0, it means we should do an active scan.
168  * In case of active scan w/o directed scan, we receive a zero-length SSID
169  * just to notify that this scan is active and not passive.
170  * In order to notify the FW of the number of SSIDs we wish to scan (including
171  * the zero-length one), we need to set the corresponding bits in chan->type,
172  * one for each SSID, and set the active bit (first). If the first SSID is
173  * already included in the probe template, so we need to set only
174  * req->n_ssids - 1 bits in addition to the first bit.
175  */
176 static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
177 {
178         if (band == IEEE80211_BAND_2GHZ)
179                 return 20  + 3 * (n_ssids + 1);
180         return 10  + 2 * (n_ssids + 1);
181 }
182
183 static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
184 {
185         return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
186 }
187
188 static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
189                                        struct cfg80211_scan_request *req,
190                                        bool basic_ssid,
191                                        struct iwl_mvm_scan_params *params)
192 {
193         struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
194                 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
195         int i;
196         int type = BIT(req->n_ssids) - 1;
197         enum ieee80211_band band = req->channels[0]->band;
198
199         if (!basic_ssid)
200                 type |= BIT(req->n_ssids);
201
202         for (i = 0; i < cmd->channel_count; i++) {
203                 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
204                 chan->type = cpu_to_le32(type);
205                 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
206                         chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
207                 chan->active_dwell = cpu_to_le16(params->dwell[band].active);
208                 chan->passive_dwell = cpu_to_le16(params->dwell[band].passive);
209                 chan->iteration_count = cpu_to_le16(1);
210                 chan++;
211         }
212 }
213
214 /*
215  * Fill in probe request with the following parameters:
216  * TA is our vif HW address, which mac80211 ensures we have.
217  * Packet is broadcasted, so this is both SA and DA.
218  * The probe request IE is made out of two: first comes the most prioritized
219  * SSID if a directed scan is requested. Second comes whatever extra
220  * information was given to us as the scan request IE.
221  */
222 static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
223                                   int n_ssids, const u8 *ssid, int ssid_len,
224                                   const u8 *band_ie, int band_ie_len,
225                                   const u8 *common_ie, int common_ie_len,
226                                   int left)
227 {
228         int len = 0;
229         u8 *pos = NULL;
230
231         /* Make sure there is enough space for the probe request,
232          * two mandatory IEs and the data */
233         left -= 24;
234         if (left < 0)
235                 return 0;
236
237         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
238         eth_broadcast_addr(frame->da);
239         memcpy(frame->sa, ta, ETH_ALEN);
240         eth_broadcast_addr(frame->bssid);
241         frame->seq_ctrl = 0;
242
243         len += 24;
244
245         /* for passive scans, no need to fill anything */
246         if (n_ssids == 0)
247                 return (u16)len;
248
249         /* points to the payload of the request */
250         pos = &frame->u.probe_req.variable[0];
251
252         /* fill in our SSID IE */
253         left -= ssid_len + 2;
254         if (left < 0)
255                 return 0;
256         *pos++ = WLAN_EID_SSID;
257         *pos++ = ssid_len;
258         if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
259                 memcpy(pos, ssid, ssid_len);
260                 pos += ssid_len;
261         }
262
263         len += ssid_len + 2;
264
265         if (WARN_ON(left < band_ie_len + common_ie_len))
266                 return len;
267
268         if (band_ie && band_ie_len) {
269                 memcpy(pos, band_ie, band_ie_len);
270                 pos += band_ie_len;
271                 len += band_ie_len;
272         }
273
274         if (common_ie && common_ie_len) {
275                 memcpy(pos, common_ie, common_ie_len);
276                 pos += common_ie_len;
277                 len += common_ie_len;
278         }
279
280         return (u16)len;
281 }
282
283 static void iwl_mvm_scan_condition_iterator(void *data, u8 *mac,
284                                             struct ieee80211_vif *vif)
285 {
286         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
287         int *global_cnt = data;
288
289         if (vif->type != NL80211_IFTYPE_P2P_DEVICE && mvmvif->phy_ctxt &&
290             mvmvif->phy_ctxt->id < MAX_PHYS)
291                 *global_cnt += 1;
292 }
293
294 static void iwl_mvm_scan_calc_params(struct iwl_mvm *mvm,
295                                      struct ieee80211_vif *vif,
296                                      int n_ssids, u32 flags,
297                                      struct iwl_mvm_scan_params *params)
298 {
299         int global_cnt = 0;
300         enum ieee80211_band band;
301         u8 frag_passive_dwell = 0;
302
303         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
304                                             IEEE80211_IFACE_ITER_NORMAL,
305                                             iwl_mvm_scan_condition_iterator,
306                                             &global_cnt);
307
308         if (!global_cnt)
309                 goto not_bound;
310
311         params->suspend_time = 30;
312         params->max_out_time = 170;
313
314         if (iwl_mvm_low_latency(mvm)) {
315                 if (mvm->fw->ucode_capa.api[0] &
316                     IWL_UCODE_TLV_API_FRAGMENTED_SCAN) {
317                         params->suspend_time = 105;
318                         params->max_out_time = 70;
319                         /*
320                          * If there is more than one active interface make
321                          * passive scan more fragmented.
322                          */
323                         frag_passive_dwell = (global_cnt < 2) ? 40 : 20;
324                 } else {
325                         params->suspend_time = 120;
326                         params->max_out_time = 120;
327                 }
328         }
329
330         if (frag_passive_dwell && (mvm->fw->ucode_capa.api[0] &
331                                    IWL_UCODE_TLV_API_FRAGMENTED_SCAN)) {
332                 /*
333                  * P2P device scan should not be fragmented to avoid negative
334                  * impact on P2P device discovery. Configure max_out_time to be
335                  * equal to dwell time on passive channel. Take a longest
336                  * possible value, one that corresponds to 2GHz band
337                  */
338                 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
339                         u32 passive_dwell =
340                                 iwl_mvm_get_passive_dwell(IEEE80211_BAND_2GHZ);
341                         params->max_out_time = passive_dwell;
342                 } else {
343                         params->passive_fragmented = true;
344                 }
345         }
346
347         if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
348                 params->max_out_time = 200;
349
350 not_bound:
351
352         for (band = IEEE80211_BAND_2GHZ; band < IEEE80211_NUM_BANDS; band++) {
353                 if (params->passive_fragmented)
354                         params->dwell[band].passive = frag_passive_dwell;
355                 else
356                         params->dwell[band].passive =
357                                 iwl_mvm_get_passive_dwell(band);
358                 params->dwell[band].active = iwl_mvm_get_active_dwell(band,
359                                                                       n_ssids);
360         }
361 }
362
363 static inline bool iwl_mvm_rrm_scan_needed(struct iwl_mvm *mvm)
364 {
365         /* require rrm scan whenever the fw supports it */
366         return mvm->fw->ucode_capa.capa[0] &
367                IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT;
368 }
369
370 static int iwl_mvm_max_scan_ie_fw_cmd_room(struct iwl_mvm *mvm,
371                                            bool is_sched_scan)
372 {
373         int max_probe_len;
374
375         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
376                 max_probe_len = SCAN_OFFLOAD_PROBE_REQ_SIZE;
377         else
378                 max_probe_len = mvm->fw->ucode_capa.max_probe_length;
379
380         /* we create the 802.11 header and SSID element */
381         max_probe_len -= 24 + 2;
382
383         /* basic ssid is added only for hw_scan with and old api */
384         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID) &&
385             !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) &&
386             !is_sched_scan)
387                 max_probe_len -= 32;
388
389         /* DS parameter set element is added on 2.4GHZ band if required */
390         if (iwl_mvm_rrm_scan_needed(mvm))
391                 max_probe_len -= 3;
392
393         return max_probe_len;
394 }
395
396 int iwl_mvm_max_scan_ie_len(struct iwl_mvm *mvm, bool is_sched_scan)
397 {
398         int max_ie_len = iwl_mvm_max_scan_ie_fw_cmd_room(mvm, is_sched_scan);
399
400         if (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN))
401                 return max_ie_len;
402
403         /* TODO: [BUG] This function should return the maximum allowed size of
404          * scan IEs, however the LMAC scan api contains both 2GHZ and 5GHZ IEs
405          * in the same command. So the correct implementation of this function
406          * is just iwl_mvm_max_scan_ie_fw_cmd_room() / 2. Currently the scan
407          * command has only 512 bytes and it would leave us with about 240
408          * bytes for scan IEs, which is clearly not enough. So meanwhile
409          * we will report an incorrect value. This may result in a failure to
410          * issue a scan in unified_scan_lmac and unified_sched_scan_lmac
411          * functions with -ENOBUFS, if a large enough probe will be provided.
412          */
413         return max_ie_len;
414 }
415
416 int iwl_mvm_scan_request(struct iwl_mvm *mvm,
417                          struct ieee80211_vif *vif,
418                          struct cfg80211_scan_request *req)
419 {
420         struct iwl_host_cmd hcmd = {
421                 .id = SCAN_REQUEST_CMD,
422                 .len = { 0, },
423                 .data = { mvm->scan_cmd, },
424                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
425         };
426         struct iwl_scan_cmd *cmd = mvm->scan_cmd;
427         int ret;
428         u32 status;
429         int ssid_len = 0;
430         u8 *ssid = NULL;
431         bool basic_ssid = !(mvm->fw->ucode_capa.flags &
432                            IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
433         struct iwl_mvm_scan_params params = {};
434
435         lockdep_assert_held(&mvm->mutex);
436
437         /* we should have failed registration if scan_cmd was NULL */
438         if (WARN_ON(mvm->scan_cmd == NULL))
439                 return -ENOMEM;
440
441         IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
442         mvm->scan_status = IWL_MVM_SCAN_OS;
443         memset(cmd, 0, ksize(cmd));
444
445         cmd->channel_count = (u8)req->n_channels;
446         cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
447         cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
448         cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
449
450         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags, &params);
451         cmd->max_out_time = cpu_to_le32(params.max_out_time);
452         cmd->suspend_time = cpu_to_le32(params.suspend_time);
453         if (params.passive_fragmented)
454                 cmd->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
455
456         cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
457         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
458                                         MAC_FILTER_IN_BEACON);
459
460         if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
461                 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
462         else
463                 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
464
465         cmd->repeats = cpu_to_le32(1);
466
467         /*
468          * If the user asked for passive scan, don't change to active scan if
469          * you see any activity on the channel - remain passive.
470          */
471         if (req->n_ssids > 0) {
472                 cmd->passive2active = cpu_to_le16(1);
473                 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
474                 if (basic_ssid) {
475                         ssid = req->ssids[0].ssid;
476                         ssid_len = req->ssids[0].ssid_len;
477                 }
478         } else {
479                 cmd->passive2active = 0;
480                 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
481         }
482
483         iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->ssids, req->n_ssids,
484                                 basic_ssid ? 1 : 0);
485
486         cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
487                                            3 << TX_CMD_FLG_BT_PRIO_POS);
488
489         cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
490         cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
491         cmd->tx_cmd.rate_n_flags =
492                         iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
493                                                   req->no_cck);
494
495         cmd->tx_cmd.len =
496                 cpu_to_le16(iwl_mvm_fill_probe_req(
497                             (struct ieee80211_mgmt *)cmd->data,
498                             vif->addr,
499                             req->n_ssids, ssid, ssid_len,
500                             req->ie, req->ie_len, NULL, 0,
501                             mvm->fw->ucode_capa.max_probe_length));
502
503         iwl_mvm_scan_fill_channels(cmd, req, basic_ssid, &params);
504
505         cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
506                 le16_to_cpu(cmd->tx_cmd.len) +
507                 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
508         hcmd.len[0] = le16_to_cpu(cmd->len);
509
510         status = SCAN_RESPONSE_OK;
511         ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
512         if (!ret && status == SCAN_RESPONSE_OK) {
513                 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
514         } else {
515                 /*
516                  * If the scan failed, it usually means that the FW was unable
517                  * to allocate the time events. Warn on it, but maybe we
518                  * should try to send the command again with different params.
519                  */
520                 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
521                         status, ret);
522                 mvm->scan_status = IWL_MVM_SCAN_NONE;
523                 ret = -EIO;
524         }
525         return ret;
526 }
527
528 int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
529                           struct iwl_device_cmd *cmd)
530 {
531         struct iwl_rx_packet *pkt = rxb_addr(rxb);
532         struct iwl_cmd_response *resp = (void *)pkt->data;
533
534         IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
535                        le32_to_cpu(resp->status));
536         return 0;
537 }
538
539 int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
540                           struct iwl_device_cmd *cmd)
541 {
542         struct iwl_rx_packet *pkt = rxb_addr(rxb);
543         struct iwl_scan_complete_notif *notif = (void *)pkt->data;
544
545         lockdep_assert_held(&mvm->mutex);
546
547         IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
548                        notif->status, notif->scanned_channels);
549
550         if (mvm->scan_status == IWL_MVM_SCAN_OS)
551                 mvm->scan_status = IWL_MVM_SCAN_NONE;
552         ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
553
554         iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
555
556         return 0;
557 }
558
559 int iwl_mvm_rx_scan_offload_results(struct iwl_mvm *mvm,
560                                     struct iwl_rx_cmd_buffer *rxb,
561                                     struct iwl_device_cmd *cmd)
562 {
563         struct iwl_rx_packet *pkt = rxb_addr(rxb);
564
565         if (!(mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) &&
566             !(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
567                 struct iwl_sched_scan_results *notif = (void *)pkt->data;
568
569                 if (!(notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN))
570                         return 0;
571         }
572
573         IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
574         ieee80211_sched_scan_results(mvm->hw);
575
576         return 0;
577 }
578
579 static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
580                                      struct iwl_rx_packet *pkt, void *data)
581 {
582         struct iwl_mvm *mvm =
583                 container_of(notif_wait, struct iwl_mvm, notif_wait);
584         struct iwl_scan_complete_notif *notif;
585         u32 *resp;
586
587         switch (pkt->hdr.cmd) {
588         case SCAN_ABORT_CMD:
589                 resp = (void *)pkt->data;
590                 if (*resp == CAN_ABORT_STATUS) {
591                         IWL_DEBUG_SCAN(mvm,
592                                        "Scan can be aborted, wait until completion\n");
593                         return false;
594                 }
595
596                 /*
597                  * If scan cannot be aborted, it means that we had a
598                  * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
599                  * ieee80211_scan_completed already.
600                  */
601                 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
602                                *resp);
603                 return true;
604
605         case SCAN_COMPLETE_NOTIFICATION:
606                 notif = (void *)pkt->data;
607                 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
608                                notif->status);
609                 return true;
610
611         default:
612                 WARN_ON(1);
613                 return false;
614         };
615 }
616
617 static int iwl_mvm_cancel_regular_scan(struct iwl_mvm *mvm)
618 {
619         struct iwl_notification_wait wait_scan_abort;
620         static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
621                                                SCAN_COMPLETE_NOTIFICATION };
622         int ret;
623
624         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
625                                    scan_abort_notif,
626                                    ARRAY_SIZE(scan_abort_notif),
627                                    iwl_mvm_scan_abort_notif, NULL);
628
629         ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, 0, 0, NULL);
630         if (ret) {
631                 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
632                 /* mac80211's state will be cleaned in the nic_restart flow */
633                 goto out_remove_notif;
634         }
635
636         return iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, HZ);
637
638 out_remove_notif:
639         iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
640         return ret;
641 }
642
643 int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
644                                            struct iwl_rx_cmd_buffer *rxb,
645                                            struct iwl_device_cmd *cmd)
646 {
647         struct iwl_rx_packet *pkt = rxb_addr(rxb);
648         u8 status, ebs_status;
649
650         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) {
651                 struct iwl_periodic_scan_complete *scan_notif;
652
653                 scan_notif = (void *)pkt->data;
654                 status = scan_notif->status;
655                 ebs_status = scan_notif->ebs_status;
656         } else  {
657                 struct iwl_scan_offload_complete *scan_notif;
658
659                 scan_notif = (void *)pkt->data;
660                 status = scan_notif->status;
661                 ebs_status = scan_notif->ebs_status;
662         }
663         /* scan status must be locked for proper checking */
664         lockdep_assert_held(&mvm->mutex);
665
666         IWL_DEBUG_SCAN(mvm,
667                        "%s completed, status %s, EBS status %s\n",
668                        mvm->scan_status == IWL_MVM_SCAN_SCHED ?
669                                 "Scheduled scan" : "Scan",
670                        status == IWL_SCAN_OFFLOAD_COMPLETED ?
671                                 "completed" : "aborted",
672                        ebs_status == IWL_SCAN_EBS_SUCCESS ?
673                                 "success" : "failed");
674
675
676         /* only call mac80211 completion if the stop was initiated by FW */
677         if (mvm->scan_status == IWL_MVM_SCAN_SCHED) {
678                 mvm->scan_status = IWL_MVM_SCAN_NONE;
679                 ieee80211_sched_scan_stopped(mvm->hw);
680         } else if (mvm->scan_status == IWL_MVM_SCAN_OS) {
681                 mvm->scan_status = IWL_MVM_SCAN_NONE;
682                 ieee80211_scan_completed(mvm->hw,
683                                          status == IWL_SCAN_OFFLOAD_ABORTED);
684                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
685         }
686
687         mvm->last_ebs_successful = !ebs_status;
688
689         return 0;
690 }
691
692 static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
693                                           struct ieee80211_vif *vif,
694                                           struct ieee80211_scan_ies *ies,
695                                           enum ieee80211_band band,
696                                           struct iwl_tx_cmd *cmd,
697                                           u8 *data)
698 {
699         u16 cmd_len;
700
701         cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
702         cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
703         cmd->sta_id = mvm->aux_sta.sta_id;
704
705         cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
706
707         cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
708                                          vif->addr,
709                                          1, NULL, 0,
710                                          ies->ies[band], ies->len[band],
711                                          ies->common_ies, ies->common_ie_len,
712                                          SCAN_OFFLOAD_PROBE_REQ_SIZE);
713         cmd->len = cpu_to_le16(cmd_len);
714 }
715
716 static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
717                                struct ieee80211_vif *vif,
718                                struct cfg80211_sched_scan_request *req,
719                                struct iwl_scan_offload_cmd *scan,
720                                struct iwl_mvm_scan_params *params)
721 {
722         scan->channel_count = req->n_channels;
723         scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
724         scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
725         scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
726         scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
727
728         scan->max_out_time = cpu_to_le32(params->max_out_time);
729         scan->suspend_time = cpu_to_le32(params->suspend_time);
730
731         scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
732                                           MAC_FILTER_IN_BEACON);
733         scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
734         scan->rep_count = cpu_to_le32(1);
735
736         if (params->passive_fragmented)
737                 scan->scan_flags |= SCAN_FLAGS_FRAGMENTED_SCAN;
738 }
739
740 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
741 {
742         int i;
743
744         for (i = 0; i < PROBE_OPTION_MAX; i++) {
745                 if (!ssid_list[i].len)
746                         break;
747                 if (ssid_list[i].len == ssid_len &&
748                     !memcmp(ssid_list->ssid, ssid, ssid_len))
749                         return i;
750         }
751         return -1;
752 }
753
754 static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
755                                         struct iwl_ssid_ie *direct_scan,
756                                         u32 *ssid_bitmap, bool basic_ssid)
757 {
758         int i, j;
759         int index;
760
761         /*
762          * copy SSIDs from match list.
763          * iwl_config_sched_scan_profiles() uses the order of these ssids to
764          * config match list.
765          */
766         for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
767                 /* skip empty SSID matchsets */
768                 if (!req->match_sets[i].ssid.ssid_len)
769                         continue;
770                 direct_scan[i].id = WLAN_EID_SSID;
771                 direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
772                 memcpy(direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
773                        direct_scan[i].len);
774         }
775
776         /* add SSIDs from scan SSID list */
777         *ssid_bitmap = 0;
778         for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
779                 index = iwl_ssid_exist(req->ssids[j].ssid,
780                                        req->ssids[j].ssid_len,
781                                        direct_scan);
782                 if (index < 0) {
783                         if (!req->ssids[j].ssid_len && basic_ssid)
784                                 continue;
785                         direct_scan[i].id = WLAN_EID_SSID;
786                         direct_scan[i].len = req->ssids[j].ssid_len;
787                         memcpy(direct_scan[i].ssid, req->ssids[j].ssid,
788                                direct_scan[i].len);
789                         *ssid_bitmap |= BIT(i + 1);
790                         i++;
791                 } else {
792                         *ssid_bitmap |= BIT(index + 1);
793                 }
794         }
795 }
796
797 static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
798                                   struct cfg80211_sched_scan_request *req,
799                                   u8 *channels_buffer,
800                                   enum ieee80211_band band,
801                                   int *head,
802                                   u32 ssid_bitmap,
803                                   struct iwl_mvm_scan_params *params)
804 {
805         u32 n_channels = mvm->fw->ucode_capa.n_scan_channels;
806         __le32 *type = (__le32 *)channels_buffer;
807         __le16 *channel_number = (__le16 *)(type + n_channels);
808         __le16 *iter_count = channel_number + n_channels;
809         __le32 *iter_interval = (__le32 *)(iter_count + n_channels);
810         u8 *active_dwell = (u8 *)(iter_interval + n_channels);
811         u8 *passive_dwell = active_dwell + n_channels;
812         int i, index = 0;
813
814         for (i = 0; i < req->n_channels; i++) {
815                 struct ieee80211_channel *chan = req->channels[i];
816
817                 if (chan->band != band)
818                         continue;
819
820                 index = *head;
821                 (*head)++;
822
823                 channel_number[index] = cpu_to_le16(chan->hw_value);
824                 active_dwell[index] = params->dwell[band].active;
825                 passive_dwell[index] = params->dwell[band].passive;
826
827                 iter_count[index] = cpu_to_le16(1);
828                 iter_interval[index] = 0;
829
830                 if (!(chan->flags & IEEE80211_CHAN_NO_IR))
831                         type[index] |=
832                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
833
834                 type[index] |= cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL |
835                                            IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
836
837                 if (chan->flags & IEEE80211_CHAN_NO_HT40)
838                         type[index] |=
839                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
840
841                 /* scan for all SSIDs from req->ssids */
842                 type[index] |= cpu_to_le32(ssid_bitmap);
843         }
844 }
845
846 int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
847                               struct ieee80211_vif *vif,
848                               struct cfg80211_sched_scan_request *req,
849                               struct ieee80211_scan_ies *ies)
850 {
851         int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
852         int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
853         int head = 0;
854         u32 ssid_bitmap;
855         int cmd_len;
856         int ret;
857         u8 *probes;
858         bool basic_ssid = !(mvm->fw->ucode_capa.flags &
859                             IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
860
861         struct iwl_scan_offload_cfg *scan_cfg;
862         struct iwl_host_cmd cmd = {
863                 .id = SCAN_OFFLOAD_CONFIG_CMD,
864         };
865         struct iwl_mvm_scan_params params = {};
866
867         lockdep_assert_held(&mvm->mutex);
868
869         cmd_len = sizeof(struct iwl_scan_offload_cfg) +
870                   mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE +
871                   2 * SCAN_OFFLOAD_PROBE_REQ_SIZE;
872
873         scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
874         if (!scan_cfg)
875                 return -ENOMEM;
876
877         probes = scan_cfg->data +
878                 mvm->fw->ucode_capa.n_scan_channels * IWL_SCAN_CHAN_SIZE;
879
880         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
881         iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd, &params);
882         scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
883
884         iwl_scan_offload_build_ssid(req, scan_cfg->scan_cmd.direct_scan,
885                                     &ssid_bitmap, basic_ssid);
886         /* build tx frames for supported bands */
887         if (band_2ghz) {
888                 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
889                                               IEEE80211_BAND_2GHZ,
890                                               &scan_cfg->scan_cmd.tx_cmd[0],
891                                               probes);
892                 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
893                                       IEEE80211_BAND_2GHZ, &head,
894                                       ssid_bitmap, &params);
895         }
896         if (band_5ghz) {
897                 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
898                                               IEEE80211_BAND_5GHZ,
899                                               &scan_cfg->scan_cmd.tx_cmd[1],
900                                               probes +
901                                                 SCAN_OFFLOAD_PROBE_REQ_SIZE);
902                 iwl_build_channel_cfg(mvm, req, scan_cfg->data,
903                                       IEEE80211_BAND_5GHZ, &head,
904                                       ssid_bitmap, &params);
905         }
906
907         cmd.data[0] = scan_cfg;
908         cmd.len[0] = cmd_len;
909         cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
910
911         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
912
913         ret = iwl_mvm_send_cmd(mvm, &cmd);
914         kfree(scan_cfg);
915         return ret;
916 }
917
918 int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
919                                        struct cfg80211_sched_scan_request *req)
920 {
921         struct iwl_scan_offload_profile *profile;
922         struct iwl_scan_offload_profile_cfg *profile_cfg;
923         struct iwl_scan_offload_blacklist *blacklist;
924         struct iwl_host_cmd cmd = {
925                 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
926                 .len[1] = sizeof(*profile_cfg),
927                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
928                 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
929         };
930         int blacklist_len;
931         int i;
932         int ret;
933
934         if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
935                         return -EIO;
936
937         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
938                 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
939         else
940                 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
941
942         blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
943         if (!blacklist)
944                 return -ENOMEM;
945
946         profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
947         if (!profile_cfg) {
948                 ret = -ENOMEM;
949                 goto free_blacklist;
950         }
951
952         cmd.data[0] = blacklist;
953         cmd.len[0] = sizeof(*blacklist) * blacklist_len;
954         cmd.data[1] = profile_cfg;
955
956         /* No blacklist configuration */
957
958         profile_cfg->num_profiles = req->n_match_sets;
959         profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
960         profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
961         profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
962         if (!req->n_match_sets || !req->match_sets[0].ssid.ssid_len)
963                 profile_cfg->any_beacon_notify = SCAN_CLIENT_SCHED_SCAN;
964
965         for (i = 0; i < req->n_match_sets; i++) {
966                 profile = &profile_cfg->profiles[i];
967                 profile->ssid_index = i;
968                 /* Support any cipher and auth algorithm */
969                 profile->unicast_cipher = 0xff;
970                 profile->auth_alg = 0xff;
971                 profile->network_type = IWL_NETWORK_TYPE_ANY;
972                 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
973                 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
974         }
975
976         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
977
978         ret = iwl_mvm_send_cmd(mvm, &cmd);
979         kfree(profile_cfg);
980 free_blacklist:
981         kfree(blacklist);
982
983         return ret;
984 }
985
986 static bool iwl_mvm_scan_pass_all(struct iwl_mvm *mvm,
987                                   struct cfg80211_sched_scan_request *req)
988 {
989         if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
990                 IWL_DEBUG_SCAN(mvm,
991                                "Sending scheduled scan with filtering, n_match_sets %d\n",
992                                req->n_match_sets);
993                 return false;
994         }
995
996         IWL_DEBUG_SCAN(mvm, "Sending Scheduled scan without filtering\n");
997         return true;
998 }
999
1000 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
1001                              struct cfg80211_sched_scan_request *req)
1002 {
1003         struct iwl_scan_offload_req scan_req = {
1004                 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
1005
1006                 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
1007                 .schedule_line[0].delay = cpu_to_le16(req->interval / 1000),
1008                 .schedule_line[0].full_scan_mul = 1,
1009
1010                 .schedule_line[1].iterations = 0xff,
1011                 .schedule_line[1].delay = cpu_to_le16(req->interval / 1000),
1012                 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
1013         };
1014
1015         if (iwl_mvm_scan_pass_all(mvm, req))
1016                 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
1017
1018         if (mvm->last_ebs_successful &&
1019             mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT)
1020                 scan_req.flags |=
1021                         cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_EBS_ACCURATE_MODE);
1022
1023         return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, 0,
1024                                     sizeof(scan_req), &scan_req);
1025 }
1026
1027 int iwl_mvm_scan_offload_start(struct iwl_mvm *mvm,
1028                                struct ieee80211_vif *vif,
1029                                struct cfg80211_sched_scan_request *req,
1030                                struct ieee80211_scan_ies *ies)
1031 {
1032         int ret;
1033
1034         if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN) {
1035                 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1036                 if (ret)
1037                         return ret;
1038                 ret = iwl_mvm_sched_scan_umac(mvm, vif, req, ies);
1039         } else if ((mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)) {
1040                 mvm->scan_status = IWL_MVM_SCAN_SCHED;
1041                 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1042                 if (ret)
1043                         return ret;
1044                 ret = iwl_mvm_unified_sched_scan_lmac(mvm, vif, req, ies);
1045         } else {
1046                 mvm->scan_status = IWL_MVM_SCAN_SCHED;
1047                 ret = iwl_mvm_config_sched_scan(mvm, vif, req, ies);
1048                 if (ret)
1049                         return ret;
1050                 ret = iwl_mvm_config_sched_scan_profiles(mvm, req);
1051                 if (ret)
1052                         return ret;
1053                 ret = iwl_mvm_sched_scan_start(mvm, req);
1054         }
1055
1056         return ret;
1057 }
1058
1059 static int iwl_mvm_send_scan_offload_abort(struct iwl_mvm *mvm)
1060 {
1061         int ret;
1062         struct iwl_host_cmd cmd = {
1063                 .id = SCAN_OFFLOAD_ABORT_CMD,
1064         };
1065         u32 status;
1066
1067         /* Exit instantly with error when device is not ready
1068          * to receive scan abort command or it does not perform
1069          * scheduled scan currently */
1070         if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1071             (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1072              mvm->scan_status != IWL_MVM_SCAN_OS))
1073                 return -EIO;
1074
1075         ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
1076         if (ret)
1077                 return ret;
1078
1079         if (status != CAN_ABORT_STATUS) {
1080                 /*
1081                  * The scan abort will return 1 for success or
1082                  * 2 for "failure".  A failure condition can be
1083                  * due to simply not being in an active scan which
1084                  * can occur if we send the scan abort before the
1085                  * microcode has notified us that a scan is completed.
1086                  */
1087                 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
1088                 ret = -ENOENT;
1089         }
1090
1091         return ret;
1092 }
1093
1094 int iwl_mvm_scan_offload_stop(struct iwl_mvm *mvm, bool notify)
1095 {
1096         int ret;
1097         struct iwl_notification_wait wait_scan_done;
1098         static const u8 scan_done_notif[] = { SCAN_OFFLOAD_COMPLETE, };
1099         bool sched = mvm->scan_status == IWL_MVM_SCAN_SCHED;
1100
1101         lockdep_assert_held(&mvm->mutex);
1102
1103         if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1104                 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN,
1105                                           notify);
1106
1107         if (mvm->scan_status != IWL_MVM_SCAN_SCHED &&
1108             (!(mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN) ||
1109              mvm->scan_status != IWL_MVM_SCAN_OS)) {
1110                 IWL_DEBUG_SCAN(mvm, "No scan to stop\n");
1111                 return 0;
1112         }
1113
1114         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
1115                                    scan_done_notif,
1116                                    ARRAY_SIZE(scan_done_notif),
1117                                    NULL, NULL);
1118
1119         ret = iwl_mvm_send_scan_offload_abort(mvm);
1120         if (ret) {
1121                 IWL_DEBUG_SCAN(mvm, "Send stop %sscan failed %d\n",
1122                                sched ? "offloaded " : "", ret);
1123                 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
1124                 return ret;
1125         }
1126
1127         IWL_DEBUG_SCAN(mvm, "Successfully sent stop %sscan\n",
1128                        sched ? "offloaded " : "");
1129
1130         ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
1131         if (ret)
1132                 return ret;
1133
1134         /*
1135          * Clear the scan status so the next scan requests will succeed. This
1136          * also ensures the Rx handler doesn't do anything, as the scan was
1137          * stopped from above. Since the rx handler won't do anything now,
1138          * we have to release the scan reference here.
1139          */
1140         if (mvm->scan_status == IWL_MVM_SCAN_OS)
1141                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1142
1143         mvm->scan_status = IWL_MVM_SCAN_NONE;
1144
1145         if (notify) {
1146                 if (sched)
1147                         ieee80211_sched_scan_stopped(mvm->hw);
1148                 else
1149                         ieee80211_scan_completed(mvm->hw, true);
1150         }
1151
1152         return 0;
1153 }
1154
1155 static void iwl_mvm_unified_scan_fill_tx_cmd(struct iwl_mvm *mvm,
1156                                              struct iwl_scan_req_tx_cmd *tx_cmd,
1157                                              bool no_cck)
1158 {
1159         tx_cmd[0].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1160                                          TX_CMD_FLG_BT_DIS);
1161         tx_cmd[0].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1162                                                            IEEE80211_BAND_2GHZ,
1163                                                            no_cck);
1164         tx_cmd[0].sta_id = mvm->aux_sta.sta_id;
1165
1166         tx_cmd[1].tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
1167                                          TX_CMD_FLG_BT_DIS);
1168         tx_cmd[1].rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm,
1169                                                            IEEE80211_BAND_5GHZ,
1170                                                            no_cck);
1171         tx_cmd[1].sta_id = mvm->aux_sta.sta_id;
1172 }
1173
1174 static void
1175 iwl_mvm_lmac_scan_cfg_channels(struct iwl_mvm *mvm,
1176                                struct ieee80211_channel **channels,
1177                                int n_channels, u32 ssid_bitmap,
1178                                struct iwl_scan_req_unified_lmac *cmd)
1179 {
1180         struct iwl_scan_channel_cfg_lmac *channel_cfg = (void *)&cmd->data;
1181         int i;
1182
1183         for (i = 0; i < n_channels; i++) {
1184                 channel_cfg[i].channel_num =
1185                         cpu_to_le16(channels[i]->hw_value);
1186                 channel_cfg[i].iter_count = cpu_to_le16(1);
1187                 channel_cfg[i].iter_interval = 0;
1188                 channel_cfg[i].flags =
1189                         cpu_to_le32(IWL_UNIFIED_SCAN_CHANNEL_PARTIAL |
1190                                     ssid_bitmap);
1191         }
1192 }
1193
1194 static u8 *iwl_mvm_copy_and_insert_ds_elem(struct iwl_mvm *mvm, const u8 *ies,
1195                                            size_t len, u8 *const pos)
1196 {
1197         static const u8 before_ds_params[] = {
1198                         WLAN_EID_SSID,
1199                         WLAN_EID_SUPP_RATES,
1200                         WLAN_EID_REQUEST,
1201                         WLAN_EID_EXT_SUPP_RATES,
1202         };
1203         size_t offs;
1204         u8 *newpos = pos;
1205
1206         if (!iwl_mvm_rrm_scan_needed(mvm)) {
1207                 memcpy(newpos, ies, len);
1208                 return newpos + len;
1209         }
1210
1211         offs = ieee80211_ie_split(ies, len,
1212                                   before_ds_params,
1213                                   ARRAY_SIZE(before_ds_params),
1214                                   0);
1215
1216         memcpy(newpos, ies, offs);
1217         newpos += offs;
1218
1219         /* Add a placeholder for DS Parameter Set element */
1220         *newpos++ = WLAN_EID_DS_PARAMS;
1221         *newpos++ = 1;
1222         *newpos++ = 0;
1223
1224         memcpy(newpos, ies + offs, len - offs);
1225         newpos += len - offs;
1226
1227         return newpos;
1228 }
1229
1230 static void
1231 iwl_mvm_build_unified_scan_probe(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1232                                  struct ieee80211_scan_ies *ies,
1233                                  struct iwl_scan_probe_req *preq,
1234                                  const u8 *mac_addr, const u8 *mac_addr_mask)
1235 {
1236         struct ieee80211_mgmt *frame = (struct ieee80211_mgmt *)preq->buf;
1237         u8 *pos, *newpos;
1238
1239         /*
1240          * Unfortunately, right now the offload scan doesn't support randomising
1241          * within the firmware, so until the firmware API is ready we implement
1242          * it in the driver. This means that the scan iterations won't really be
1243          * random, only when it's restarted, but at least that helps a bit.
1244          */
1245         if (mac_addr)
1246                 get_random_mask_addr(frame->sa, mac_addr, mac_addr_mask);
1247         else
1248                 memcpy(frame->sa, vif->addr, ETH_ALEN);
1249
1250         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1251         eth_broadcast_addr(frame->da);
1252         eth_broadcast_addr(frame->bssid);
1253         frame->seq_ctrl = 0;
1254
1255         pos = frame->u.probe_req.variable;
1256         *pos++ = WLAN_EID_SSID;
1257         *pos++ = 0;
1258
1259         preq->mac_header.offset = 0;
1260         preq->mac_header.len = cpu_to_le16(24 + 2);
1261
1262         /* Insert ds parameter set element on 2.4 GHz band */
1263         newpos = iwl_mvm_copy_and_insert_ds_elem(mvm,
1264                                                  ies->ies[IEEE80211_BAND_2GHZ],
1265                                                  ies->len[IEEE80211_BAND_2GHZ],
1266                                                  pos);
1267         preq->band_data[0].offset = cpu_to_le16(pos - preq->buf);
1268         preq->band_data[0].len = cpu_to_le16(newpos - pos);
1269         pos = newpos;
1270
1271         memcpy(pos, ies->ies[IEEE80211_BAND_5GHZ],
1272                ies->len[IEEE80211_BAND_5GHZ]);
1273         preq->band_data[1].offset = cpu_to_le16(pos - preq->buf);
1274         preq->band_data[1].len = cpu_to_le16(ies->len[IEEE80211_BAND_5GHZ]);
1275         pos += ies->len[IEEE80211_BAND_5GHZ];
1276
1277         memcpy(pos, ies->common_ies, ies->common_ie_len);
1278         preq->common_data.offset = cpu_to_le16(pos - preq->buf);
1279         preq->common_data.len = cpu_to_le16(ies->common_ie_len);
1280 }
1281
1282 static void
1283 iwl_mvm_build_generic_unified_scan_cmd(struct iwl_mvm *mvm,
1284                                        struct iwl_scan_req_unified_lmac *cmd,
1285                                        struct iwl_mvm_scan_params *params)
1286 {
1287         memset(cmd, 0, ksize(cmd));
1288         cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1289         cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1290         if (params->passive_fragmented)
1291                 cmd->fragmented_dwell =
1292                                 params->dwell[IEEE80211_BAND_2GHZ].passive;
1293         cmd->rx_chain_select = iwl_mvm_scan_rx_chain(mvm);
1294         cmd->max_out_time = cpu_to_le32(params->max_out_time);
1295         cmd->suspend_time = cpu_to_le32(params->suspend_time);
1296         cmd->scan_prio = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1297         cmd->iter_num = cpu_to_le32(1);
1298
1299         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1300             mvm->last_ebs_successful) {
1301                 cmd->channel_opt[0].flags =
1302                         cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1303                                     IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1304                                     IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1305                 cmd->channel_opt[0].non_ebs_ratio =
1306                         cpu_to_le16(IWL_DENSE_EBS_SCAN_RATIO);
1307                 cmd->channel_opt[1].flags =
1308                         cpu_to_le16(IWL_SCAN_CHANNEL_FLAG_EBS |
1309                                     IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1310                                     IWL_SCAN_CHANNEL_FLAG_CACHE_ADD);
1311                 cmd->channel_opt[1].non_ebs_ratio =
1312                         cpu_to_le16(IWL_SPARSE_EBS_SCAN_RATIO);
1313         }
1314
1315         if (iwl_mvm_rrm_scan_needed(mvm))
1316                 cmd->scan_flags |=
1317                         cpu_to_le32(IWL_MVM_LMAC_SCAN_FLAGS_RRM_ENABLED);
1318 }
1319
1320 int iwl_mvm_unified_scan_lmac(struct iwl_mvm *mvm,
1321                               struct ieee80211_vif *vif,
1322                               struct ieee80211_scan_request *req)
1323 {
1324         struct iwl_host_cmd hcmd = {
1325                 .id = SCAN_OFFLOAD_REQUEST_CMD,
1326                 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1327                          sizeof(struct iwl_scan_channel_cfg_lmac) *
1328                                 mvm->fw->ucode_capa.n_scan_channels +
1329                          sizeof(struct iwl_scan_probe_req), },
1330                 .data = { mvm->scan_cmd, },
1331                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1332         };
1333         struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1334         struct iwl_scan_probe_req *preq;
1335         struct iwl_mvm_scan_params params = {};
1336         u32 flags;
1337         u32 ssid_bitmap = 0;
1338         int ret, i;
1339
1340         lockdep_assert_held(&mvm->mutex);
1341
1342         /* we should have failed registration if scan_cmd was NULL */
1343         if (WARN_ON(mvm->scan_cmd == NULL))
1344                 return -ENOMEM;
1345
1346         if (req->req.n_ssids > PROBE_OPTION_MAX ||
1347             req->ies.common_ie_len + req->ies.len[NL80211_BAND_2GHZ] +
1348             req->ies.len[NL80211_BAND_5GHZ] >
1349                 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, false) ||
1350             req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
1351                 return -ENOBUFS;
1352
1353         mvm->scan_status = IWL_MVM_SCAN_OS;
1354
1355         iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1356                                  &params);
1357
1358         iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1359
1360         cmd->n_channels = (u8)req->req.n_channels;
1361
1362         flags = IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1363
1364         if (req->req.n_ssids == 1 && req->req.ssids[0].ssid_len != 0)
1365                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1366
1367         if (params.passive_fragmented)
1368                 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1369
1370         if (req->req.n_ssids == 0)
1371                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1372
1373         cmd->scan_flags |= cpu_to_le32(flags);
1374
1375         cmd->flags = iwl_mvm_scan_rxon_flags(req->req.channels[0]->band);
1376         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1377                                         MAC_FILTER_IN_BEACON);
1378         iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, req->req.no_cck);
1379         iwl_mvm_scan_fill_ssids(cmd->direct_scan, req->req.ssids,
1380                                 req->req.n_ssids, 0);
1381
1382         cmd->schedule[0].delay = 0;
1383         cmd->schedule[0].iterations = 1;
1384         cmd->schedule[0].full_scan_mul = 0;
1385         cmd->schedule[1].delay = 0;
1386         cmd->schedule[1].iterations = 0;
1387         cmd->schedule[1].full_scan_mul = 0;
1388
1389         for (i = 1; i <= req->req.n_ssids; i++)
1390                 ssid_bitmap |= BIT(i);
1391
1392         iwl_mvm_lmac_scan_cfg_channels(mvm, req->req.channels,
1393                                        req->req.n_channels, ssid_bitmap,
1394                                        cmd);
1395
1396         preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1397                         mvm->fw->ucode_capa.n_scan_channels);
1398
1399         iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, preq,
1400                 req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1401                         req->req.mac_addr : NULL,
1402                 req->req.mac_addr_mask);
1403
1404         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1405         if (!ret) {
1406                 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
1407         } else {
1408                 /*
1409                  * If the scan failed, it usually means that the FW was unable
1410                  * to allocate the time events. Warn on it, but maybe we
1411                  * should try to send the command again with different params.
1412                  */
1413                 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1414                 mvm->scan_status = IWL_MVM_SCAN_NONE;
1415                 ret = -EIO;
1416         }
1417         return ret;
1418 }
1419
1420 int iwl_mvm_unified_sched_scan_lmac(struct iwl_mvm *mvm,
1421                                     struct ieee80211_vif *vif,
1422                                     struct cfg80211_sched_scan_request *req,
1423                                     struct ieee80211_scan_ies *ies)
1424 {
1425         struct iwl_host_cmd hcmd = {
1426                 .id = SCAN_OFFLOAD_REQUEST_CMD,
1427                 .len = { sizeof(struct iwl_scan_req_unified_lmac) +
1428                          sizeof(struct iwl_scan_channel_cfg_lmac) *
1429                                 mvm->fw->ucode_capa.n_scan_channels +
1430                          sizeof(struct iwl_scan_probe_req), },
1431                 .data = { mvm->scan_cmd, },
1432                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1433         };
1434         struct iwl_scan_req_unified_lmac *cmd = mvm->scan_cmd;
1435         struct iwl_scan_probe_req *preq;
1436         struct iwl_mvm_scan_params params = {};
1437         int ret;
1438         u32 flags = 0, ssid_bitmap = 0;
1439
1440         lockdep_assert_held(&mvm->mutex);
1441
1442         /* we should have failed registration if scan_cmd was NULL */
1443         if (WARN_ON(mvm->scan_cmd == NULL))
1444                 return -ENOMEM;
1445
1446         if (req->n_ssids > PROBE_OPTION_MAX ||
1447             ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1448             ies->len[NL80211_BAND_5GHZ] >
1449                 iwl_mvm_max_scan_ie_fw_cmd_room(mvm, true) ||
1450             req->n_channels > mvm->fw->ucode_capa.n_scan_channels)
1451                 return -ENOBUFS;
1452
1453         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, 0, &params);
1454
1455         iwl_mvm_build_generic_unified_scan_cmd(mvm, cmd, &params);
1456
1457         cmd->n_channels = (u8)req->n_channels;
1458
1459         if (iwl_mvm_scan_pass_all(mvm, req))
1460                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASS_ALL;
1461
1462         if (req->n_ssids == 1 && req->ssids[0].ssid_len != 0)
1463                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PRE_CONNECTION;
1464
1465         if (params.passive_fragmented)
1466                 flags |= IWL_MVM_LMAC_SCAN_FLAG_FRAGMENTED;
1467
1468         if (req->n_ssids == 0)
1469                 flags |= IWL_MVM_LMAC_SCAN_FLAG_PASSIVE;
1470
1471         cmd->scan_flags |= cpu_to_le32(flags);
1472
1473         cmd->flags = iwl_mvm_scan_rxon_flags(req->channels[0]->band);
1474         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
1475                                         MAC_FILTER_IN_BEACON);
1476         iwl_mvm_unified_scan_fill_tx_cmd(mvm, cmd->tx_cmd, false);
1477         iwl_scan_offload_build_ssid(req, cmd->direct_scan, &ssid_bitmap, false);
1478
1479         cmd->schedule[0].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
1480         cmd->schedule[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS;
1481         cmd->schedule[0].full_scan_mul = 1;
1482
1483         cmd->schedule[1].delay = cpu_to_le16(req->interval / MSEC_PER_SEC);
1484         cmd->schedule[1].iterations = 0xff;
1485         cmd->schedule[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER;
1486
1487         iwl_mvm_lmac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1488                                        ssid_bitmap, cmd);
1489
1490         preq = (void *)(cmd->data + sizeof(struct iwl_scan_channel_cfg_lmac) *
1491                         mvm->fw->ucode_capa.n_scan_channels);
1492
1493         iwl_mvm_build_unified_scan_probe(mvm, vif, ies, preq,
1494                 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1495                         req->mac_addr : NULL,
1496                 req->mac_addr_mask);
1497
1498         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1499         if (!ret) {
1500                 IWL_DEBUG_SCAN(mvm,
1501                                "Sched scan request was sent successfully\n");
1502         } else {
1503                 /*
1504                  * If the scan failed, it usually means that the FW was unable
1505                  * to allocate the time events. Warn on it, but maybe we
1506                  * should try to send the command again with different params.
1507                  */
1508                 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1509                 mvm->scan_status = IWL_MVM_SCAN_NONE;
1510                 ret = -EIO;
1511         }
1512         return ret;
1513 }
1514
1515
1516 int iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
1517 {
1518         if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
1519                 return iwl_umac_scan_stop(mvm, IWL_UMAC_SCAN_UID_REG_SCAN,
1520                                           true);
1521
1522         if (mvm->scan_status == IWL_MVM_SCAN_NONE)
1523                 return 0;
1524
1525         if (iwl_mvm_is_radio_killed(mvm)) {
1526                 ieee80211_scan_completed(mvm->hw, true);
1527                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1528                 mvm->scan_status = IWL_MVM_SCAN_NONE;
1529                 return 0;
1530         }
1531
1532         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
1533                 return iwl_mvm_scan_offload_stop(mvm, true);
1534         return iwl_mvm_cancel_regular_scan(mvm);
1535 }
1536
1537 /* UMAC scan API */
1538
1539 struct iwl_umac_scan_done {
1540         struct iwl_mvm *mvm;
1541         enum iwl_umac_scan_uid_type type;
1542 };
1543
1544 static int rate_to_scan_rate_flag(unsigned int rate)
1545 {
1546         static const int rate_to_scan_rate[IWL_RATE_COUNT] = {
1547                 [IWL_RATE_1M_INDEX]     = SCAN_CONFIG_RATE_1M,
1548                 [IWL_RATE_2M_INDEX]     = SCAN_CONFIG_RATE_2M,
1549                 [IWL_RATE_5M_INDEX]     = SCAN_CONFIG_RATE_5M,
1550                 [IWL_RATE_11M_INDEX]    = SCAN_CONFIG_RATE_11M,
1551                 [IWL_RATE_6M_INDEX]     = SCAN_CONFIG_RATE_6M,
1552                 [IWL_RATE_9M_INDEX]     = SCAN_CONFIG_RATE_9M,
1553                 [IWL_RATE_12M_INDEX]    = SCAN_CONFIG_RATE_12M,
1554                 [IWL_RATE_18M_INDEX]    = SCAN_CONFIG_RATE_18M,
1555                 [IWL_RATE_24M_INDEX]    = SCAN_CONFIG_RATE_24M,
1556                 [IWL_RATE_36M_INDEX]    = SCAN_CONFIG_RATE_36M,
1557                 [IWL_RATE_48M_INDEX]    = SCAN_CONFIG_RATE_48M,
1558                 [IWL_RATE_54M_INDEX]    = SCAN_CONFIG_RATE_54M,
1559         };
1560
1561         return rate_to_scan_rate[rate];
1562 }
1563
1564 static __le32 iwl_mvm_scan_config_rates(struct iwl_mvm *mvm)
1565 {
1566         struct ieee80211_supported_band *band;
1567         unsigned int rates = 0;
1568         int i;
1569
1570         band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1571         for (i = 0; i < band->n_bitrates; i++)
1572                 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1573         band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1574         for (i = 0; i < band->n_bitrates; i++)
1575                 rates |= rate_to_scan_rate_flag(band->bitrates[i].hw_value);
1576
1577         /* Set both basic rates and supported rates */
1578         rates |= SCAN_CONFIG_SUPPORTED_RATE(rates);
1579
1580         return cpu_to_le32(rates);
1581 }
1582
1583 int iwl_mvm_config_scan(struct iwl_mvm *mvm)
1584 {
1585
1586         struct iwl_scan_config *scan_config;
1587         struct ieee80211_supported_band *band;
1588         int num_channels =
1589                 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
1590                 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
1591         int ret, i, j = 0, cmd_size, data_size;
1592         struct iwl_host_cmd cmd = {
1593                 .id = SCAN_CFG_CMD,
1594         };
1595
1596         if (WARN_ON(num_channels > mvm->fw->ucode_capa.n_scan_channels))
1597                 return -ENOBUFS;
1598
1599         cmd_size = sizeof(*scan_config) + mvm->fw->ucode_capa.n_scan_channels;
1600
1601         scan_config = kzalloc(cmd_size, GFP_KERNEL);
1602         if (!scan_config)
1603                 return -ENOMEM;
1604
1605         data_size = cmd_size - sizeof(struct iwl_mvm_umac_cmd_hdr);
1606         scan_config->hdr.size = cpu_to_le16(data_size);
1607         scan_config->flags = cpu_to_le32(SCAN_CONFIG_FLAG_ACTIVATE |
1608                                          SCAN_CONFIG_FLAG_ALLOW_CHUB_REQS |
1609                                          SCAN_CONFIG_FLAG_SET_TX_CHAINS |
1610                                          SCAN_CONFIG_FLAG_SET_RX_CHAINS |
1611                                          SCAN_CONFIG_FLAG_SET_ALL_TIMES |
1612                                          SCAN_CONFIG_FLAG_SET_LEGACY_RATES |
1613                                          SCAN_CONFIG_FLAG_SET_MAC_ADDR |
1614                                          SCAN_CONFIG_FLAG_SET_CHANNEL_FLAGS|
1615                                          SCAN_CONFIG_N_CHANNELS(num_channels));
1616         scan_config->tx_chains = cpu_to_le32(iwl_mvm_get_valid_tx_ant(mvm));
1617         scan_config->rx_chains = cpu_to_le32(iwl_mvm_scan_rx_ant(mvm));
1618         scan_config->legacy_rates = iwl_mvm_scan_config_rates(mvm);
1619         scan_config->out_of_channel_time = cpu_to_le32(170);
1620         scan_config->suspend_time = cpu_to_le32(30);
1621         scan_config->dwell_active = 20;
1622         scan_config->dwell_passive = 110;
1623         scan_config->dwell_fragmented = 20;
1624
1625         memcpy(&scan_config->mac_addr, &mvm->addresses[0].addr, ETH_ALEN);
1626
1627         scan_config->bcast_sta_id = mvm->aux_sta.sta_id;
1628         scan_config->channel_flags = IWL_CHANNEL_FLAG_EBS |
1629                                      IWL_CHANNEL_FLAG_ACCURATE_EBS |
1630                                      IWL_CHANNEL_FLAG_EBS_ADD |
1631                                      IWL_CHANNEL_FLAG_PRE_SCAN_PASSIVE2ACTIVE;
1632
1633         band = &mvm->nvm_data->bands[IEEE80211_BAND_2GHZ];
1634         for (i = 0; i < band->n_channels; i++, j++)
1635                 scan_config->channel_array[j] = band->channels[i].center_freq;
1636         band = &mvm->nvm_data->bands[IEEE80211_BAND_5GHZ];
1637         for (i = 0; i < band->n_channels; i++, j++)
1638                 scan_config->channel_array[j] = band->channels[i].center_freq;
1639
1640         cmd.data[0] = scan_config;
1641         cmd.len[0] = cmd_size;
1642         cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
1643
1644         IWL_DEBUG_SCAN(mvm, "Sending UMAC scan config\n");
1645
1646         ret = iwl_mvm_send_cmd(mvm, &cmd);
1647
1648         kfree(scan_config);
1649         return ret;
1650 }
1651
1652 static int iwl_mvm_find_scan_uid(struct iwl_mvm *mvm, u32 uid)
1653 {
1654         int i;
1655
1656         for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
1657                 if (mvm->scan_uid[i] == uid)
1658                         return i;
1659
1660         return i;
1661 }
1662
1663 static int iwl_mvm_find_free_scan_uid(struct iwl_mvm *mvm)
1664 {
1665         return iwl_mvm_find_scan_uid(mvm, 0);
1666 }
1667
1668 static bool iwl_mvm_find_scan_type(struct iwl_mvm *mvm,
1669                                    enum iwl_umac_scan_uid_type type)
1670 {
1671         int i;
1672
1673         for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++)
1674                 if (mvm->scan_uid[i] & type)
1675                         return true;
1676
1677         return false;
1678 }
1679
1680 static u32 iwl_generate_scan_uid(struct iwl_mvm *mvm,
1681                                  enum iwl_umac_scan_uid_type type)
1682 {
1683         u32 uid;
1684
1685         /* make sure exactly one bit is on in scan type */
1686         WARN_ON(hweight8(type) != 1);
1687
1688         /*
1689          * Make sure scan uids are unique. If one scan lasts long time while
1690          * others are completing frequently, the seq number will wrap up and
1691          * we may have more than one scan with the same uid.
1692          */
1693         do {
1694                 uid = type | (mvm->scan_seq_num <<
1695                               IWL_UMAC_SCAN_UID_SEQ_OFFSET);
1696                 mvm->scan_seq_num++;
1697         } while (iwl_mvm_find_scan_uid(mvm, uid) <
1698                  IWL_MVM_MAX_SIMULTANEOUS_SCANS);
1699
1700         IWL_DEBUG_SCAN(mvm, "Generated scan UID %u\n", uid);
1701
1702         return uid;
1703 }
1704
1705 static void
1706 iwl_mvm_build_generic_umac_scan_cmd(struct iwl_mvm *mvm,
1707                                     struct iwl_scan_req_umac *cmd,
1708                                     struct iwl_mvm_scan_params *params)
1709 {
1710         memset(cmd, 0, ksize(cmd));
1711         cmd->hdr.size = cpu_to_le16(iwl_mvm_scan_size(mvm) -
1712                                     sizeof(struct iwl_mvm_umac_cmd_hdr));
1713         cmd->active_dwell = params->dwell[IEEE80211_BAND_2GHZ].active;
1714         cmd->passive_dwell = params->dwell[IEEE80211_BAND_2GHZ].passive;
1715         if (params->passive_fragmented)
1716                 cmd->fragmented_dwell =
1717                                 params->dwell[IEEE80211_BAND_2GHZ].passive;
1718         cmd->max_out_time = cpu_to_le32(params->max_out_time);
1719         cmd->suspend_time = cpu_to_le32(params->suspend_time);
1720         cmd->scan_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1721 }
1722
1723 static void
1724 iwl_mvm_umac_scan_cfg_channels(struct iwl_mvm *mvm,
1725                                struct ieee80211_channel **channels,
1726                                int n_channels, u32 ssid_bitmap,
1727                                struct iwl_scan_req_umac *cmd)
1728 {
1729         struct iwl_scan_channel_cfg_umac *channel_cfg = (void *)&cmd->data;
1730         int i;
1731
1732         for (i = 0; i < n_channels; i++) {
1733                 channel_cfg[i].flags = cpu_to_le32(ssid_bitmap);
1734                 channel_cfg[i].channel_num = channels[i]->hw_value;
1735                 channel_cfg[i].iter_count = 1;
1736                 channel_cfg[i].iter_interval = 0;
1737         }
1738 }
1739
1740 static u32 iwl_mvm_scan_umac_common_flags(struct iwl_mvm *mvm, int n_ssids,
1741                                           struct cfg80211_ssid *ssids,
1742                                           int fragmented)
1743 {
1744         int flags = 0;
1745
1746         if (n_ssids == 0)
1747                 flags = IWL_UMAC_SCAN_GEN_FLAGS_PASSIVE;
1748
1749         if (n_ssids == 1 && ssids[0].ssid_len != 0)
1750                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PRE_CONNECT;
1751
1752         if (fragmented)
1753                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_FRAGMENTED;
1754
1755         if (iwl_mvm_rrm_scan_needed(mvm))
1756                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_RRM_ENABLED;
1757
1758         return flags;
1759 }
1760
1761 int iwl_mvm_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1762                       struct ieee80211_scan_request *req)
1763 {
1764         struct iwl_host_cmd hcmd = {
1765                 .id = SCAN_REQ_UMAC,
1766                 .len = { iwl_mvm_scan_size(mvm), },
1767                 .data = { mvm->scan_cmd, },
1768                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1769         };
1770         struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1771         struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1772                 sizeof(struct iwl_scan_channel_cfg_umac) *
1773                         mvm->fw->ucode_capa.n_scan_channels;
1774         struct iwl_mvm_scan_params params = {};
1775         u32 uid, flags;
1776         u32 ssid_bitmap = 0;
1777         int ret, i, uid_idx;
1778
1779         lockdep_assert_held(&mvm->mutex);
1780
1781         uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1782         if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1783                 return -EBUSY;
1784
1785         /* we should have failed registration if scan_cmd was NULL */
1786         if (WARN_ON(mvm->scan_cmd == NULL))
1787                 return -ENOMEM;
1788
1789         if (WARN_ON(req->req.n_ssids > PROBE_OPTION_MAX ||
1790                     req->ies.common_ie_len +
1791                     req->ies.len[NL80211_BAND_2GHZ] +
1792                     req->ies.len[NL80211_BAND_5GHZ] + 24 + 2 >
1793                     SCAN_OFFLOAD_PROBE_REQ_SIZE || req->req.n_channels >
1794                     mvm->fw->ucode_capa.n_scan_channels))
1795                 return -ENOBUFS;
1796
1797         iwl_mvm_scan_calc_params(mvm, vif, req->req.n_ssids, req->req.flags,
1798                                  &params);
1799
1800         iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1801
1802         uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_REG_SCAN);
1803         mvm->scan_uid[uid_idx] = uid;
1804         cmd->uid = cpu_to_le32(uid);
1805
1806         cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_HIGH);
1807
1808         flags = iwl_mvm_scan_umac_common_flags(mvm, req->req.n_ssids,
1809                                                req->req.ssids,
1810                                                params.passive_fragmented);
1811
1812         flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1813
1814         cmd->general_flags = cpu_to_le32(flags);
1815         cmd->n_channels = req->req.n_channels;
1816
1817         for (i = 0; i < req->req.n_ssids; i++)
1818                 ssid_bitmap |= BIT(i);
1819
1820         iwl_mvm_umac_scan_cfg_channels(mvm, req->req.channels,
1821                                        req->req.n_channels, ssid_bitmap, cmd);
1822
1823         sec_part->schedule[0].iter_count = 1;
1824         sec_part->delay = 0;
1825
1826         iwl_mvm_build_unified_scan_probe(mvm, vif, &req->ies, &sec_part->preq,
1827                 req->req.flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1828                         req->req.mac_addr : NULL,
1829                 req->req.mac_addr_mask);
1830
1831         iwl_mvm_scan_fill_ssids(sec_part->direct_scan, req->req.ssids,
1832                                 req->req.n_ssids, 0);
1833
1834         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1835         if (!ret) {
1836                 IWL_DEBUG_SCAN(mvm,
1837                                "Scan request was sent successfully\n");
1838         } else {
1839                 /*
1840                  * If the scan failed, it usually means that the FW was unable
1841                  * to allocate the time events. Warn on it, but maybe we
1842                  * should try to send the command again with different params.
1843                  */
1844                 IWL_ERR(mvm, "Scan failed! ret %d\n", ret);
1845         }
1846         return ret;
1847 }
1848
1849 int iwl_mvm_sched_scan_umac(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1850                             struct cfg80211_sched_scan_request *req,
1851                             struct ieee80211_scan_ies *ies)
1852 {
1853
1854         struct iwl_host_cmd hcmd = {
1855                 .id = SCAN_REQ_UMAC,
1856                 .len = { iwl_mvm_scan_size(mvm), },
1857                 .data = { mvm->scan_cmd, },
1858                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
1859         };
1860         struct iwl_scan_req_umac *cmd = mvm->scan_cmd;
1861         struct iwl_scan_req_umac_tail *sec_part = (void *)&cmd->data +
1862                 sizeof(struct iwl_scan_channel_cfg_umac) *
1863                         mvm->fw->ucode_capa.n_scan_channels;
1864         struct iwl_mvm_scan_params params = {};
1865         u32 uid, flags;
1866         u32 ssid_bitmap = 0;
1867         int ret, uid_idx;
1868
1869         lockdep_assert_held(&mvm->mutex);
1870
1871         uid_idx = iwl_mvm_find_free_scan_uid(mvm);
1872         if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1873                 return -EBUSY;
1874
1875         /* we should have failed registration if scan_cmd was NULL */
1876         if (WARN_ON(mvm->scan_cmd == NULL))
1877                 return -ENOMEM;
1878
1879         if (WARN_ON(req->n_ssids > PROBE_OPTION_MAX ||
1880                     ies->common_ie_len + ies->len[NL80211_BAND_2GHZ] +
1881                     ies->len[NL80211_BAND_5GHZ] + 24 + 2 >
1882                     SCAN_OFFLOAD_PROBE_REQ_SIZE || req->n_channels >
1883                     mvm->fw->ucode_capa.n_scan_channels))
1884                 return -ENOBUFS;
1885
1886         iwl_mvm_scan_calc_params(mvm, vif, req->n_ssids, req->flags,
1887                                          &params);
1888
1889         iwl_mvm_build_generic_umac_scan_cmd(mvm, cmd, &params);
1890
1891         cmd->flags = cpu_to_le32(IWL_UMAC_SCAN_FLAG_PREEMPTIVE);
1892
1893         uid = iwl_generate_scan_uid(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN);
1894         mvm->scan_uid[uid_idx] = uid;
1895         cmd->uid = cpu_to_le32(uid);
1896
1897         cmd->ooc_priority = cpu_to_le32(IWL_SCAN_PRIORITY_LOW);
1898
1899         flags = iwl_mvm_scan_umac_common_flags(mvm, req->n_ssids, req->ssids,
1900                                                params.passive_fragmented);
1901
1902         flags |= IWL_UMAC_SCAN_GEN_FLAGS_PERIODIC;
1903
1904         if (iwl_mvm_scan_pass_all(mvm, req))
1905                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_PASS_ALL;
1906         else
1907                 flags |= IWL_UMAC_SCAN_GEN_FLAGS_MATCH;
1908
1909         cmd->general_flags = cpu_to_le32(flags);
1910
1911         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_EBS_SUPPORT &&
1912             mvm->last_ebs_successful)
1913                 cmd->channel_flags = IWL_SCAN_CHANNEL_FLAG_EBS |
1914                                      IWL_SCAN_CHANNEL_FLAG_EBS_ACCURATE |
1915                                      IWL_SCAN_CHANNEL_FLAG_CACHE_ADD;
1916
1917         cmd->n_channels = req->n_channels;
1918
1919         iwl_scan_offload_build_ssid(req, sec_part->direct_scan, &ssid_bitmap,
1920                                     false);
1921
1922         /* This API uses bits 0-19 instead of 1-20. */
1923         ssid_bitmap = ssid_bitmap >> 1;
1924
1925         iwl_mvm_umac_scan_cfg_channels(mvm, req->channels, req->n_channels,
1926                                        ssid_bitmap, cmd);
1927
1928         sec_part->schedule[0].interval =
1929                                 cpu_to_le16(req->interval / MSEC_PER_SEC);
1930         sec_part->schedule[0].iter_count = 0xff;
1931
1932         sec_part->delay = 0;
1933
1934         iwl_mvm_build_unified_scan_probe(mvm, vif, ies, &sec_part->preq,
1935                 req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR ?
1936                         req->mac_addr : NULL,
1937                 req->mac_addr_mask);
1938
1939         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1940         if (!ret) {
1941                 IWL_DEBUG_SCAN(mvm,
1942                                "Sched scan request was sent successfully\n");
1943         } else {
1944                 /*
1945                  * If the scan failed, it usually means that the FW was unable
1946                  * to allocate the time events. Warn on it, but maybe we
1947                  * should try to send the command again with different params.
1948                  */
1949                 IWL_ERR(mvm, "Sched scan failed! ret %d\n", ret);
1950         }
1951         return ret;
1952 }
1953
1954 int iwl_mvm_rx_umac_scan_complete_notif(struct iwl_mvm *mvm,
1955                                         struct iwl_rx_cmd_buffer *rxb,
1956                                         struct iwl_device_cmd *cmd)
1957 {
1958         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1959         struct iwl_umac_scan_complete *notif = (void *)pkt->data;
1960         u32 uid = __le32_to_cpu(notif->uid);
1961         bool sched = !!(uid & IWL_UMAC_SCAN_UID_SCHED_SCAN);
1962         int uid_idx = iwl_mvm_find_scan_uid(mvm, uid);
1963
1964         /*
1965          * Scan uid may be set to zero in case of scan abort request from above.
1966          */
1967         if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
1968                 return 0;
1969
1970         IWL_DEBUG_SCAN(mvm,
1971                        "Scan completed, uid %u type %s, status %s, EBS status %s\n",
1972                        uid, sched ? "sched" : "regular",
1973                        notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
1974                                 "completed" : "aborted",
1975                        notif->ebs_status == IWL_SCAN_EBS_SUCCESS ?
1976                                 "success" : "failed");
1977
1978         mvm->last_ebs_successful = !notif->ebs_status;
1979         mvm->scan_uid[uid_idx] = 0;
1980
1981         if (!sched) {
1982                 ieee80211_scan_completed(mvm->hw,
1983                                          notif->status ==
1984                                                 IWL_SCAN_OFFLOAD_ABORTED);
1985                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
1986         } else if (!iwl_mvm_find_scan_type(mvm, IWL_UMAC_SCAN_UID_SCHED_SCAN)) {
1987                 ieee80211_sched_scan_stopped(mvm->hw);
1988         } else {
1989                 IWL_DEBUG_SCAN(mvm, "Another sched scan is running\n");
1990         }
1991
1992         return 0;
1993 }
1994
1995 static bool iwl_scan_umac_done_check(struct iwl_notif_wait_data *notif_wait,
1996                                      struct iwl_rx_packet *pkt, void *data)
1997 {
1998         struct iwl_umac_scan_done *scan_done = data;
1999         struct iwl_umac_scan_complete *notif = (void *)pkt->data;
2000         u32 uid = __le32_to_cpu(notif->uid);
2001         int uid_idx = iwl_mvm_find_scan_uid(scan_done->mvm, uid);
2002
2003         if (WARN_ON(pkt->hdr.cmd != SCAN_COMPLETE_UMAC))
2004                 return false;
2005
2006         if (uid_idx >= IWL_MVM_MAX_SIMULTANEOUS_SCANS)
2007                 return false;
2008
2009         /*
2010          * Clear scan uid of scans that was aborted from above and completed
2011          * in FW so the RX handler does nothing.
2012          */
2013         scan_done->mvm->scan_uid[uid_idx] = 0;
2014
2015         return !iwl_mvm_find_scan_type(scan_done->mvm, scan_done->type);
2016 }
2017
2018 static int iwl_umac_scan_abort_one(struct iwl_mvm *mvm, u32 uid)
2019 {
2020         struct iwl_umac_scan_abort cmd = {
2021                 .hdr.size = cpu_to_le16(sizeof(struct iwl_umac_scan_abort) -
2022                                         sizeof(struct iwl_mvm_umac_cmd_hdr)),
2023                 .uid = cpu_to_le32(uid),
2024         };
2025
2026         lockdep_assert_held(&mvm->mutex);
2027
2028         IWL_DEBUG_SCAN(mvm, "Sending scan abort, uid %u\n", uid);
2029
2030         return iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_UMAC, 0, sizeof(cmd), &cmd);
2031 }
2032
2033 static int iwl_umac_scan_stop(struct iwl_mvm *mvm,
2034                               enum iwl_umac_scan_uid_type type, bool notify)
2035 {
2036         struct iwl_notification_wait wait_scan_done;
2037         static const u8 scan_done_notif[] = { SCAN_COMPLETE_UMAC, };
2038         struct iwl_umac_scan_done scan_done = {
2039                 .mvm = mvm,
2040                 .type = type,
2041         };
2042         int i, ret = -EIO;
2043
2044         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_done,
2045                                    scan_done_notif,
2046                                    ARRAY_SIZE(scan_done_notif),
2047                                    iwl_scan_umac_done_check, &scan_done);
2048
2049         IWL_DEBUG_SCAN(mvm, "Preparing to stop scan, type %x\n", type);
2050
2051         for (i = 0; i < IWL_MVM_MAX_SIMULTANEOUS_SCANS; i++) {
2052                 if (mvm->scan_uid[i] & type) {
2053                         int err;
2054
2055                         if (iwl_mvm_is_radio_killed(mvm) &&
2056                             (type & IWL_UMAC_SCAN_UID_REG_SCAN)) {
2057                                 ieee80211_scan_completed(mvm->hw, true);
2058                                 iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2059                                 break;
2060                         }
2061
2062                         err = iwl_umac_scan_abort_one(mvm, mvm->scan_uid[i]);
2063                         if (!err)
2064                                 ret = 0;
2065                 }
2066         }
2067
2068         if (ret) {
2069                 IWL_DEBUG_SCAN(mvm, "Couldn't stop scan\n");
2070                 iwl_remove_notification(&mvm->notif_wait, &wait_scan_done);
2071                 return ret;
2072         }
2073
2074         ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_done, 1 * HZ);
2075         if (ret)
2076                 return ret;
2077
2078         if (notify) {
2079                 if (type & IWL_UMAC_SCAN_UID_SCHED_SCAN)
2080                         ieee80211_sched_scan_stopped(mvm->hw);
2081                 if (type & IWL_UMAC_SCAN_UID_REG_SCAN) {
2082                         ieee80211_scan_completed(mvm->hw, true);
2083                         iwl_mvm_unref(mvm, IWL_MVM_REF_SCAN);
2084                 }
2085         }
2086
2087         return ret;
2088 }
2089
2090 int iwl_mvm_scan_size(struct iwl_mvm *mvm)
2091 {
2092         if (mvm->fw->ucode_capa.capa[0] & IWL_UCODE_TLV_CAPA_UMAC_SCAN)
2093                 return sizeof(struct iwl_scan_req_umac) +
2094                         sizeof(struct iwl_scan_channel_cfg_umac) *
2095                                 mvm->fw->ucode_capa.n_scan_channels +
2096                         sizeof(struct iwl_scan_req_umac_tail);
2097
2098         if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_API_LMAC_SCAN)
2099                 return sizeof(struct iwl_scan_req_unified_lmac) +
2100                         sizeof(struct iwl_scan_channel_cfg_lmac) *
2101                                 mvm->fw->ucode_capa.n_scan_channels +
2102                         sizeof(struct iwl_scan_probe_req);
2103
2104         return sizeof(struct iwl_scan_cmd) +
2105                 mvm->fw->ucode_capa.max_probe_length +
2106                         mvm->fw->ucode_capa.n_scan_channels *
2107                 sizeof(struct iwl_scan_channel);
2108 }