spi: delete non-required instances of include <linux/init.h>
[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  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22  * USA
23  *
24  * The full GNU General Public License is included in this distribution
25  * in the file called COPYING.
26  *
27  * Contact Information:
28  *  Intel Linux Wireless <ilw@linux.intel.com>
29  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30  *
31  * BSD LICENSE
32  *
33  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  *
40  *  * Redistributions of source code must retain the above copyright
41  *    notice, this list of conditions and the following disclaimer.
42  *  * Redistributions in binary form must reproduce the above copyright
43  *    notice, this list of conditions and the following disclaimer in
44  *    the documentation and/or other materials provided with the
45  *    distribution.
46  *  * Neither the name Intel Corporation nor the names of its
47  *    contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  *****************************************************************************/
63
64 #include <linux/etherdevice.h>
65 #include <net/mac80211.h>
66
67 #include "mvm.h"
68 #include "iwl-eeprom-parse.h"
69 #include "fw-api-scan.h"
70
71 #define IWL_PLCP_QUIET_THRESH 1
72 #define IWL_ACTIVE_QUIET_TIME 10
73 #define LONG_OUT_TIME_PERIOD 600
74 #define SHORT_OUT_TIME_PERIOD 200
75 #define SUSPEND_TIME_PERIOD 100
76
77 static inline __le16 iwl_mvm_scan_rx_chain(struct iwl_mvm *mvm)
78 {
79         u16 rx_chain;
80         u8 rx_ant;
81
82         if (mvm->scan_rx_ant != ANT_NONE)
83                 rx_ant = mvm->scan_rx_ant;
84         else
85                 rx_ant = iwl_fw_valid_rx_ant(mvm->fw);
86         rx_chain = rx_ant << PHY_RX_CHAIN_VALID_POS;
87         rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_MIMO_SEL_POS;
88         rx_chain |= rx_ant << PHY_RX_CHAIN_FORCE_SEL_POS;
89         rx_chain |= 0x1 << PHY_RX_CHAIN_DRIVER_FORCE_POS;
90         return cpu_to_le16(rx_chain);
91 }
92
93 static inline __le32 iwl_mvm_scan_max_out_time(struct ieee80211_vif *vif,
94                                                u32 flags, bool is_assoc)
95 {
96         if (!is_assoc)
97                 return 0;
98         if (flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
99                 return cpu_to_le32(ieee80211_tu_to_usec(SHORT_OUT_TIME_PERIOD));
100         return cpu_to_le32(ieee80211_tu_to_usec(LONG_OUT_TIME_PERIOD));
101 }
102
103 static inline __le32 iwl_mvm_scan_suspend_time(struct ieee80211_vif *vif,
104                                                bool is_assoc)
105 {
106         if (!is_assoc)
107                 return 0;
108         return cpu_to_le32(ieee80211_tu_to_usec(SUSPEND_TIME_PERIOD));
109 }
110
111 static inline __le32
112 iwl_mvm_scan_rxon_flags(struct cfg80211_scan_request *req)
113 {
114         if (req->channels[0]->band == IEEE80211_BAND_2GHZ)
115                 return cpu_to_le32(PHY_BAND_24);
116         else
117                 return cpu_to_le32(PHY_BAND_5);
118 }
119
120 static inline __le32
121 iwl_mvm_scan_rate_n_flags(struct iwl_mvm *mvm, enum ieee80211_band band,
122                           bool no_cck)
123 {
124         u32 tx_ant;
125
126         mvm->scan_last_antenna_idx =
127                 iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
128                                      mvm->scan_last_antenna_idx);
129         tx_ant = BIT(mvm->scan_last_antenna_idx) << RATE_MCS_ANT_POS;
130
131         if (band == IEEE80211_BAND_2GHZ && !no_cck)
132                 return cpu_to_le32(IWL_RATE_1M_PLCP | RATE_MCS_CCK_MSK |
133                                    tx_ant);
134         else
135                 return cpu_to_le32(IWL_RATE_6M_PLCP | tx_ant);
136 }
137
138 /*
139  * We insert the SSIDs in an inverted order, because the FW will
140  * invert it back. The most prioritized SSID, which is first in the
141  * request list, is not copied here, but inserted directly to the probe
142  * request.
143  */
144 static void iwl_mvm_scan_fill_ssids(struct iwl_scan_cmd *cmd,
145                                     struct cfg80211_scan_request *req,
146                                     int first)
147 {
148         int fw_idx, req_idx;
149
150         for (req_idx = req->n_ssids - 1, fw_idx = 0; req_idx >= first;
151              req_idx--, fw_idx++) {
152                 cmd->direct_scan[fw_idx].id = WLAN_EID_SSID;
153                 cmd->direct_scan[fw_idx].len = req->ssids[req_idx].ssid_len;
154                 memcpy(cmd->direct_scan[fw_idx].ssid,
155                        req->ssids[req_idx].ssid,
156                        req->ssids[req_idx].ssid_len);
157         }
158 }
159
160 /*
161  * If req->n_ssids > 0, it means we should do an active scan.
162  * In case of active scan w/o directed scan, we receive a zero-length SSID
163  * just to notify that this scan is active and not passive.
164  * In order to notify the FW of the number of SSIDs we wish to scan (including
165  * the zero-length one), we need to set the corresponding bits in chan->type,
166  * one for each SSID, and set the active bit (first). If the first SSID is
167  * already included in the probe template, so we need to set only
168  * req->n_ssids - 1 bits in addition to the first bit.
169  */
170 static u16 iwl_mvm_get_active_dwell(enum ieee80211_band band, int n_ssids)
171 {
172         if (band == IEEE80211_BAND_2GHZ)
173                 return 30  + 3 * (n_ssids + 1);
174         return 20  + 2 * (n_ssids + 1);
175 }
176
177 static u16 iwl_mvm_get_passive_dwell(enum ieee80211_band band)
178 {
179         return band == IEEE80211_BAND_2GHZ ? 100 + 20 : 100 + 10;
180 }
181
182 static void iwl_mvm_scan_fill_channels(struct iwl_scan_cmd *cmd,
183                                        struct cfg80211_scan_request *req,
184                                        bool basic_ssid)
185 {
186         u16 passive_dwell = iwl_mvm_get_passive_dwell(req->channels[0]->band);
187         u16 active_dwell = iwl_mvm_get_active_dwell(req->channels[0]->band,
188                                                     req->n_ssids);
189         struct iwl_scan_channel *chan = (struct iwl_scan_channel *)
190                 (cmd->data + le16_to_cpu(cmd->tx_cmd.len));
191         int i;
192         int type = BIT(req->n_ssids) - 1;
193
194         if (!basic_ssid)
195                 type |= BIT(req->n_ssids);
196
197         for (i = 0; i < cmd->channel_count; i++) {
198                 chan->channel = cpu_to_le16(req->channels[i]->hw_value);
199                 chan->type = cpu_to_le32(type);
200                 if (req->channels[i]->flags & IEEE80211_CHAN_NO_IR)
201                         chan->type &= cpu_to_le32(~SCAN_CHANNEL_TYPE_ACTIVE);
202                 chan->active_dwell = cpu_to_le16(active_dwell);
203                 chan->passive_dwell = cpu_to_le16(passive_dwell);
204                 chan->iteration_count = cpu_to_le16(1);
205                 chan++;
206         }
207 }
208
209 /*
210  * Fill in probe request with the following parameters:
211  * TA is our vif HW address, which mac80211 ensures we have.
212  * Packet is broadcasted, so this is both SA and DA.
213  * The probe request IE is made out of two: first comes the most prioritized
214  * SSID if a directed scan is requested. Second comes whatever extra
215  * information was given to us as the scan request IE.
216  */
217 static u16 iwl_mvm_fill_probe_req(struct ieee80211_mgmt *frame, const u8 *ta,
218                                   int n_ssids, const u8 *ssid, int ssid_len,
219                                   const u8 *ie, int ie_len,
220                                   int left)
221 {
222         int len = 0;
223         u8 *pos = NULL;
224
225         /* Make sure there is enough space for the probe request,
226          * two mandatory IEs and the data */
227         left -= 24;
228         if (left < 0)
229                 return 0;
230
231         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
232         eth_broadcast_addr(frame->da);
233         memcpy(frame->sa, ta, ETH_ALEN);
234         eth_broadcast_addr(frame->bssid);
235         frame->seq_ctrl = 0;
236
237         len += 24;
238
239         /* for passive scans, no need to fill anything */
240         if (n_ssids == 0)
241                 return (u16)len;
242
243         /* points to the payload of the request */
244         pos = &frame->u.probe_req.variable[0];
245
246         /* fill in our SSID IE */
247         left -= ssid_len + 2;
248         if (left < 0)
249                 return 0;
250         *pos++ = WLAN_EID_SSID;
251         *pos++ = ssid_len;
252         if (ssid && ssid_len) { /* ssid_len may be == 0 even if ssid is valid */
253                 memcpy(pos, ssid, ssid_len);
254                 pos += ssid_len;
255         }
256
257         len += ssid_len + 2;
258
259         if (WARN_ON(left < ie_len))
260                 return len;
261
262         if (ie && ie_len) {
263                 memcpy(pos, ie, ie_len);
264                 len += ie_len;
265         }
266
267         return (u16)len;
268 }
269
270 static void iwl_mvm_vif_assoc_iterator(void *data, u8 *mac,
271                                        struct ieee80211_vif *vif)
272 {
273         bool *is_assoc = data;
274
275         if (vif->bss_conf.assoc)
276                 *is_assoc = true;
277 }
278
279 int iwl_mvm_scan_request(struct iwl_mvm *mvm,
280                          struct ieee80211_vif *vif,
281                          struct cfg80211_scan_request *req)
282 {
283         struct iwl_host_cmd hcmd = {
284                 .id = SCAN_REQUEST_CMD,
285                 .len = { 0, },
286                 .data = { mvm->scan_cmd, },
287                 .flags = CMD_SYNC,
288                 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
289         };
290         struct iwl_scan_cmd *cmd = mvm->scan_cmd;
291         bool is_assoc = false;
292         int ret;
293         u32 status;
294         int ssid_len = 0;
295         u8 *ssid = NULL;
296         bool basic_ssid = !(mvm->fw->ucode_capa.flags &
297                            IWL_UCODE_TLV_FLAGS_NO_BASIC_SSID);
298
299         lockdep_assert_held(&mvm->mutex);
300         BUG_ON(mvm->scan_cmd == NULL);
301
302         IWL_DEBUG_SCAN(mvm, "Handling mac80211 scan request\n");
303         mvm->scan_status = IWL_MVM_SCAN_OS;
304         memset(cmd, 0, sizeof(struct iwl_scan_cmd) +
305                mvm->fw->ucode_capa.max_probe_length +
306                (MAX_NUM_SCAN_CHANNELS * sizeof(struct iwl_scan_channel)));
307         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
308                                             IEEE80211_IFACE_ITER_NORMAL,
309                                             iwl_mvm_vif_assoc_iterator,
310                                             &is_assoc);
311         cmd->channel_count = (u8)req->n_channels;
312         cmd->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
313         cmd->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
314         cmd->rxchain_sel_flags = iwl_mvm_scan_rx_chain(mvm);
315         cmd->max_out_time = iwl_mvm_scan_max_out_time(vif, req->flags,
316                                                       is_assoc);
317         cmd->suspend_time = iwl_mvm_scan_suspend_time(vif, is_assoc);
318         cmd->rxon_flags = iwl_mvm_scan_rxon_flags(req);
319         cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
320                                         MAC_FILTER_IN_BEACON);
321
322         if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
323                 cmd->type = cpu_to_le32(SCAN_TYPE_DISCOVERY_FORCED);
324         else
325                 cmd->type = cpu_to_le32(SCAN_TYPE_FORCED);
326
327         cmd->repeats = cpu_to_le32(1);
328
329         /*
330          * If the user asked for passive scan, don't change to active scan if
331          * you see any activity on the channel - remain passive.
332          */
333         if (req->n_ssids > 0) {
334                 cmd->passive2active = cpu_to_le16(1);
335                 cmd->scan_flags |= SCAN_FLAGS_PASSIVE2ACTIVE;
336                 if (basic_ssid) {
337                         ssid = req->ssids[0].ssid;
338                         ssid_len = req->ssids[0].ssid_len;
339                 }
340         } else {
341                 cmd->passive2active = 0;
342                 cmd->scan_flags &= ~SCAN_FLAGS_PASSIVE2ACTIVE;
343         }
344
345         iwl_mvm_scan_fill_ssids(cmd, req, basic_ssid ? 1 : 0);
346
347         cmd->tx_cmd.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
348         cmd->tx_cmd.sta_id = mvm->aux_sta.sta_id;
349         cmd->tx_cmd.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
350         cmd->tx_cmd.rate_n_flags =
351                         iwl_mvm_scan_rate_n_flags(mvm, req->channels[0]->band,
352                                                   req->no_cck);
353
354         cmd->tx_cmd.len =
355                 cpu_to_le16(iwl_mvm_fill_probe_req(
356                             (struct ieee80211_mgmt *)cmd->data,
357                             vif->addr,
358                             req->n_ssids, ssid, ssid_len,
359                             req->ie, req->ie_len,
360                             mvm->fw->ucode_capa.max_probe_length));
361
362         iwl_mvm_scan_fill_channels(cmd, req, basic_ssid);
363
364         cmd->len = cpu_to_le16(sizeof(struct iwl_scan_cmd) +
365                 le16_to_cpu(cmd->tx_cmd.len) +
366                 (cmd->channel_count * sizeof(struct iwl_scan_channel)));
367         hcmd.len[0] = le16_to_cpu(cmd->len);
368
369         status = SCAN_RESPONSE_OK;
370         ret = iwl_mvm_send_cmd_status(mvm, &hcmd, &status);
371         if (!ret && status == SCAN_RESPONSE_OK) {
372                 IWL_DEBUG_SCAN(mvm, "Scan request was sent successfully\n");
373         } else {
374                 /*
375                  * If the scan failed, it usually means that the FW was unable
376                  * to allocate the time events. Warn on it, but maybe we
377                  * should try to send the command again with different params.
378                  */
379                 IWL_ERR(mvm, "Scan failed! status 0x%x ret %d\n",
380                         status, ret);
381                 mvm->scan_status = IWL_MVM_SCAN_NONE;
382                 ret = -EIO;
383         }
384         return ret;
385 }
386
387 int iwl_mvm_rx_scan_response(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
388                           struct iwl_device_cmd *cmd)
389 {
390         struct iwl_rx_packet *pkt = rxb_addr(rxb);
391         struct iwl_cmd_response *resp = (void *)pkt->data;
392
393         IWL_DEBUG_SCAN(mvm, "Scan response received. status 0x%x\n",
394                        le32_to_cpu(resp->status));
395         return 0;
396 }
397
398 int iwl_mvm_rx_scan_complete(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb,
399                           struct iwl_device_cmd *cmd)
400 {
401         struct iwl_rx_packet *pkt = rxb_addr(rxb);
402         struct iwl_scan_complete_notif *notif = (void *)pkt->data;
403
404         IWL_DEBUG_SCAN(mvm, "Scan complete: status=0x%x scanned channels=%d\n",
405                        notif->status, notif->scanned_channels);
406
407         mvm->scan_status = IWL_MVM_SCAN_NONE;
408         ieee80211_scan_completed(mvm->hw, notif->status != SCAN_COMP_STATUS_OK);
409
410         return 0;
411 }
412
413 int iwl_mvm_rx_sched_scan_results(struct iwl_mvm *mvm,
414                                   struct iwl_rx_cmd_buffer *rxb,
415                                   struct iwl_device_cmd *cmd)
416 {
417         struct iwl_rx_packet *pkt = rxb_addr(rxb);
418         struct iwl_sched_scan_results *notif = (void *)pkt->data;
419
420         if (notif->client_bitmap & SCAN_CLIENT_SCHED_SCAN) {
421                 IWL_DEBUG_SCAN(mvm, "Scheduled scan results\n");
422                 ieee80211_sched_scan_results(mvm->hw);
423         }
424
425         return 0;
426 }
427
428 static bool iwl_mvm_scan_abort_notif(struct iwl_notif_wait_data *notif_wait,
429                                      struct iwl_rx_packet *pkt, void *data)
430 {
431         struct iwl_mvm *mvm =
432                 container_of(notif_wait, struct iwl_mvm, notif_wait);
433         struct iwl_scan_complete_notif *notif;
434         u32 *resp;
435
436         switch (pkt->hdr.cmd) {
437         case SCAN_ABORT_CMD:
438                 resp = (void *)pkt->data;
439                 if (*resp == CAN_ABORT_STATUS) {
440                         IWL_DEBUG_SCAN(mvm,
441                                        "Scan can be aborted, wait until completion\n");
442                         return false;
443                 }
444
445                 /*
446                  * If scan cannot be aborted, it means that we had a
447                  * SCAN_COMPLETE_NOTIFICATION in the pipe and it called
448                  * ieee80211_scan_completed already.
449                  */
450                 IWL_DEBUG_SCAN(mvm, "Scan cannot be aborted, exit now: %d\n",
451                                *resp);
452                 return true;
453
454         case SCAN_COMPLETE_NOTIFICATION:
455                 notif = (void *)pkt->data;
456                 IWL_DEBUG_SCAN(mvm, "Scan aborted: status 0x%x\n",
457                                notif->status);
458                 return true;
459
460         default:
461                 WARN_ON(1);
462                 return false;
463         };
464 }
465
466 void iwl_mvm_cancel_scan(struct iwl_mvm *mvm)
467 {
468         struct iwl_notification_wait wait_scan_abort;
469         static const u8 scan_abort_notif[] = { SCAN_ABORT_CMD,
470                                                SCAN_COMPLETE_NOTIFICATION };
471         int ret;
472
473         if (mvm->scan_status == IWL_MVM_SCAN_NONE)
474                 return;
475
476         if (iwl_mvm_is_radio_killed(mvm)) {
477                 ieee80211_scan_completed(mvm->hw, true);
478                 mvm->scan_status = IWL_MVM_SCAN_NONE;
479                 return;
480         }
481
482         iwl_init_notification_wait(&mvm->notif_wait, &wait_scan_abort,
483                                    scan_abort_notif,
484                                    ARRAY_SIZE(scan_abort_notif),
485                                    iwl_mvm_scan_abort_notif, NULL);
486
487         ret = iwl_mvm_send_cmd_pdu(mvm, SCAN_ABORT_CMD, CMD_SYNC, 0, NULL);
488         if (ret) {
489                 IWL_ERR(mvm, "Couldn't send SCAN_ABORT_CMD: %d\n", ret);
490                 /* mac80211's state will be cleaned in the fw_restart flow */
491                 goto out_remove_notif;
492         }
493
494         ret = iwl_wait_notification(&mvm->notif_wait, &wait_scan_abort, 1 * HZ);
495         if (ret)
496                 IWL_ERR(mvm, "%s - failed on timeout\n", __func__);
497
498         return;
499
500 out_remove_notif:
501         iwl_remove_notification(&mvm->notif_wait, &wait_scan_abort);
502 }
503
504 int iwl_mvm_rx_scan_offload_complete_notif(struct iwl_mvm *mvm,
505                                            struct iwl_rx_cmd_buffer *rxb,
506                                            struct iwl_device_cmd *cmd)
507 {
508         struct iwl_rx_packet *pkt = rxb_addr(rxb);
509         struct iwl_scan_offload_complete *scan_notif = (void *)pkt->data;
510
511         IWL_DEBUG_SCAN(mvm, "Scheduled scan completed, status %s\n",
512                        scan_notif->status == IWL_SCAN_OFFLOAD_COMPLETED ?
513                        "completed" : "aborted");
514
515         mvm->scan_status = IWL_MVM_SCAN_NONE;
516         ieee80211_sched_scan_stopped(mvm->hw);
517
518         return 0;
519 }
520
521 static void iwl_scan_offload_build_tx_cmd(struct iwl_mvm *mvm,
522                                           struct ieee80211_vif *vif,
523                                           struct ieee80211_sched_scan_ies *ies,
524                                           enum ieee80211_band band,
525                                           struct iwl_tx_cmd *cmd,
526                                           u8 *data)
527 {
528         u16 cmd_len;
529
530         cmd->tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL);
531         cmd->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
532         cmd->sta_id = mvm->aux_sta.sta_id;
533
534         cmd->rate_n_flags = iwl_mvm_scan_rate_n_flags(mvm, band, false);
535
536         cmd_len = iwl_mvm_fill_probe_req((struct ieee80211_mgmt *)data,
537                                          vif->addr,
538                                          1, NULL, 0,
539                                          ies->ie[band], ies->len[band],
540                                          SCAN_OFFLOAD_PROBE_REQ_SIZE);
541         cmd->len = cpu_to_le16(cmd_len);
542 }
543
544 static void iwl_build_scan_cmd(struct iwl_mvm *mvm,
545                                struct ieee80211_vif *vif,
546                                struct cfg80211_sched_scan_request *req,
547                                struct iwl_scan_offload_cmd *scan)
548 {
549         bool is_assoc = false;
550
551         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
552                                             IEEE80211_IFACE_ITER_NORMAL,
553                                             iwl_mvm_vif_assoc_iterator,
554                                             &is_assoc);
555         scan->channel_count =
556                 mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels +
557                 mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
558         scan->quiet_time = cpu_to_le16(IWL_ACTIVE_QUIET_TIME);
559         scan->quiet_plcp_th = cpu_to_le16(IWL_PLCP_QUIET_THRESH);
560         scan->good_CRC_th = IWL_GOOD_CRC_TH_DEFAULT;
561         scan->rx_chain = iwl_mvm_scan_rx_chain(mvm);
562         scan->max_out_time = iwl_mvm_scan_max_out_time(vif, req->flags,
563                                                        is_assoc);
564         scan->suspend_time = iwl_mvm_scan_suspend_time(vif, is_assoc);
565         scan->filter_flags |= cpu_to_le32(MAC_FILTER_ACCEPT_GRP |
566                                           MAC_FILTER_IN_BEACON);
567         scan->scan_type = cpu_to_le32(SCAN_TYPE_BACKGROUND);
568         scan->rep_count = cpu_to_le32(1);
569 }
570
571 static int iwl_ssid_exist(u8 *ssid, u8 ssid_len, struct iwl_ssid_ie *ssid_list)
572 {
573         int i;
574
575         for (i = 0; i < PROBE_OPTION_MAX; i++) {
576                 if (!ssid_list[i].len)
577                         break;
578                 if (ssid_list[i].len == ssid_len &&
579                     !memcmp(ssid_list->ssid, ssid, ssid_len))
580                         return i;
581         }
582         return -1;
583 }
584
585 static void iwl_scan_offload_build_ssid(struct cfg80211_sched_scan_request *req,
586                                         struct iwl_scan_offload_cmd *scan,
587                                         u32 *ssid_bitmap)
588 {
589         int i, j;
590         int index;
591
592         /*
593          * copy SSIDs from match list.
594          * iwl_config_sched_scan_profiles() uses the order of these ssids to
595          * config match list.
596          */
597         for (i = 0; i < req->n_match_sets && i < PROBE_OPTION_MAX; i++) {
598                 scan->direct_scan[i].id = WLAN_EID_SSID;
599                 scan->direct_scan[i].len = req->match_sets[i].ssid.ssid_len;
600                 memcpy(scan->direct_scan[i].ssid, req->match_sets[i].ssid.ssid,
601                        scan->direct_scan[i].len);
602         }
603
604         /* add SSIDs from scan SSID list */
605         *ssid_bitmap = 0;
606         for (j = 0; j < req->n_ssids && i < PROBE_OPTION_MAX; j++) {
607                 index = iwl_ssid_exist(req->ssids[j].ssid,
608                                        req->ssids[j].ssid_len,
609                                        scan->direct_scan);
610                 if (index < 0) {
611                         if (!req->ssids[j].ssid_len)
612                                 continue;
613                         scan->direct_scan[i].id = WLAN_EID_SSID;
614                         scan->direct_scan[i].len = req->ssids[j].ssid_len;
615                         memcpy(scan->direct_scan[i].ssid, req->ssids[j].ssid,
616                                scan->direct_scan[i].len);
617                         *ssid_bitmap |= BIT(i + 1);
618                         i++;
619                 } else {
620                         *ssid_bitmap |= BIT(index + 1);
621                 }
622         }
623 }
624
625 static void iwl_build_channel_cfg(struct iwl_mvm *mvm,
626                                   struct cfg80211_sched_scan_request *req,
627                                   struct iwl_scan_channel_cfg *channels,
628                                   enum ieee80211_band band,
629                                   int *head, int *tail,
630                                   u32 ssid_bitmap)
631 {
632         struct ieee80211_supported_band *s_band;
633         int n_probes = req->n_ssids;
634         int n_channels = req->n_channels;
635         u8 active_dwell, passive_dwell;
636         int i, j, index = 0;
637         bool partial;
638
639         /*
640          * We have to configure all supported channels, even if we don't want to
641          * scan on them, but we have to send channels in the order that we want
642          * to scan. So add requested channels to head of the list and others to
643          * the end.
644         */
645         active_dwell = iwl_mvm_get_active_dwell(band, n_probes);
646         passive_dwell = iwl_mvm_get_passive_dwell(band);
647         s_band = &mvm->nvm_data->bands[band];
648
649         for (i = 0; i < s_band->n_channels && *head <= *tail; i++) {
650                 partial = false;
651                 for (j = 0; j < n_channels; j++)
652                         if (s_band->channels[i].center_freq ==
653                                                 req->channels[j]->center_freq) {
654                                 index = *head;
655                                 (*head)++;
656                                 /*
657                                  * Channels that came with the request will be
658                                  * in partial scan .
659                                  */
660                                 partial = true;
661                                 break;
662                         }
663                 if (!partial) {
664                         index = *tail;
665                         (*tail)--;
666                 }
667                 channels->channel_number[index] =
668                         cpu_to_le16(ieee80211_frequency_to_channel(
669                                         s_band->channels[i].center_freq));
670                 channels->dwell_time[index][0] = active_dwell;
671                 channels->dwell_time[index][1] = passive_dwell;
672
673                 channels->iter_count[index] = cpu_to_le16(1);
674                 channels->iter_interval[index] = 0;
675
676                 if (!(s_band->channels[i].flags & IEEE80211_CHAN_NO_IR))
677                         channels->type[index] |=
678                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_ACTIVE);
679
680                 channels->type[index] |=
681                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_FULL);
682                 if (partial)
683                         channels->type[index] |=
684                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_PARTIAL);
685
686                 if (s_band->channels[i].flags & IEEE80211_CHAN_NO_HT40)
687                         channels->type[index] |=
688                                 cpu_to_le32(IWL_SCAN_OFFLOAD_CHANNEL_NARROW);
689
690                 /* scan for all SSIDs from req->ssids */
691                 channels->type[index] |= cpu_to_le32(ssid_bitmap);
692         }
693 }
694
695 int iwl_mvm_config_sched_scan(struct iwl_mvm *mvm,
696                               struct ieee80211_vif *vif,
697                               struct cfg80211_sched_scan_request *req,
698                               struct ieee80211_sched_scan_ies *ies)
699 {
700         int supported_bands = 0;
701         int band_2ghz = mvm->nvm_data->bands[IEEE80211_BAND_2GHZ].n_channels;
702         int band_5ghz = mvm->nvm_data->bands[IEEE80211_BAND_5GHZ].n_channels;
703         int head = 0;
704         int tail = band_2ghz + band_5ghz;
705         u32 ssid_bitmap;
706         int cmd_len;
707         int ret;
708
709         struct iwl_scan_offload_cfg *scan_cfg;
710         struct iwl_host_cmd cmd = {
711                 .id = SCAN_OFFLOAD_CONFIG_CMD,
712                 .flags = CMD_SYNC,
713         };
714
715         lockdep_assert_held(&mvm->mutex);
716
717         if (band_2ghz)
718                 supported_bands++;
719         if (band_5ghz)
720                 supported_bands++;
721
722         cmd_len = sizeof(struct iwl_scan_offload_cfg) +
723                                 supported_bands * SCAN_OFFLOAD_PROBE_REQ_SIZE;
724
725         scan_cfg = kzalloc(cmd_len, GFP_KERNEL);
726         if (!scan_cfg)
727                 return -ENOMEM;
728
729         iwl_build_scan_cmd(mvm, vif, req, &scan_cfg->scan_cmd);
730         scan_cfg->scan_cmd.len = cpu_to_le16(cmd_len);
731
732         iwl_scan_offload_build_ssid(req, &scan_cfg->scan_cmd, &ssid_bitmap);
733         /* build tx frames for supported bands */
734         if (band_2ghz) {
735                 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
736                                               IEEE80211_BAND_2GHZ,
737                                               &scan_cfg->scan_cmd.tx_cmd[0],
738                                               scan_cfg->data);
739                 iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
740                                       IEEE80211_BAND_2GHZ, &head, &tail,
741                                       ssid_bitmap);
742         }
743         if (band_5ghz) {
744                 iwl_scan_offload_build_tx_cmd(mvm, vif, ies,
745                                               IEEE80211_BAND_5GHZ,
746                                               &scan_cfg->scan_cmd.tx_cmd[1],
747                                               scan_cfg->data +
748                                                 SCAN_OFFLOAD_PROBE_REQ_SIZE);
749                 iwl_build_channel_cfg(mvm, req, &scan_cfg->channel_cfg,
750                                       IEEE80211_BAND_5GHZ, &head, &tail,
751                                       ssid_bitmap);
752         }
753
754         cmd.data[0] = scan_cfg;
755         cmd.len[0] = cmd_len;
756         cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
757
758         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan config\n");
759
760         ret = iwl_mvm_send_cmd(mvm, &cmd);
761         kfree(scan_cfg);
762         return ret;
763 }
764
765 int iwl_mvm_config_sched_scan_profiles(struct iwl_mvm *mvm,
766                                        struct cfg80211_sched_scan_request *req)
767 {
768         struct iwl_scan_offload_profile *profile;
769         struct iwl_scan_offload_profile_cfg *profile_cfg;
770         struct iwl_scan_offload_blacklist *blacklist;
771         struct iwl_host_cmd cmd = {
772                 .id = SCAN_OFFLOAD_UPDATE_PROFILES_CMD,
773                 .flags = CMD_SYNC,
774                 .len[1] = sizeof(*profile_cfg),
775                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
776                 .dataflags[1] = IWL_HCMD_DFL_NOCOPY,
777         };
778         int blacklist_len;
779         int i;
780         int ret;
781
782         if (WARN_ON(req->n_match_sets > IWL_SCAN_MAX_PROFILES))
783                         return -EIO;
784
785         if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_SHORT_BL)
786                 blacklist_len = IWL_SCAN_SHORT_BLACKLIST_LEN;
787         else
788                 blacklist_len = IWL_SCAN_MAX_BLACKLIST_LEN;
789
790         blacklist = kzalloc(sizeof(*blacklist) * blacklist_len, GFP_KERNEL);
791         if (!blacklist)
792                 return -ENOMEM;
793
794         profile_cfg = kzalloc(sizeof(*profile_cfg), GFP_KERNEL);
795         if (!profile_cfg) {
796                 ret = -ENOMEM;
797                 goto free_blacklist;
798         }
799
800         cmd.data[0] = blacklist;
801         cmd.len[0] = sizeof(*blacklist) * blacklist_len;
802         cmd.data[1] = profile_cfg;
803
804         /* No blacklist configuration */
805
806         profile_cfg->num_profiles = req->n_match_sets;
807         profile_cfg->active_clients = SCAN_CLIENT_SCHED_SCAN;
808         profile_cfg->pass_match = SCAN_CLIENT_SCHED_SCAN;
809         profile_cfg->match_notify = SCAN_CLIENT_SCHED_SCAN;
810
811         for (i = 0; i < req->n_match_sets; i++) {
812                 profile = &profile_cfg->profiles[i];
813                 profile->ssid_index = i;
814                 /* Support any cipher and auth algorithm */
815                 profile->unicast_cipher = 0xff;
816                 profile->auth_alg = 0xff;
817                 profile->network_type = IWL_NETWORK_TYPE_ANY;
818                 profile->band_selection = IWL_SCAN_OFFLOAD_SELECT_ANY;
819                 profile->client_bitmap = SCAN_CLIENT_SCHED_SCAN;
820         }
821
822         IWL_DEBUG_SCAN(mvm, "Sending scheduled scan profile config\n");
823
824         ret = iwl_mvm_send_cmd(mvm, &cmd);
825         kfree(profile_cfg);
826 free_blacklist:
827         kfree(blacklist);
828
829         return ret;
830 }
831
832 int iwl_mvm_sched_scan_start(struct iwl_mvm *mvm,
833                              struct cfg80211_sched_scan_request *req)
834 {
835         struct iwl_scan_offload_req scan_req = {
836                 .watchdog = IWL_SCHED_SCAN_WATCHDOG,
837
838                 .schedule_line[0].iterations = IWL_FAST_SCHED_SCAN_ITERATIONS,
839                 .schedule_line[0].delay = req->interval / 1000,
840                 .schedule_line[0].full_scan_mul = 1,
841
842                 .schedule_line[1].iterations = 0xff,
843                 .schedule_line[1].delay = req->interval / 1000,
844                 .schedule_line[1].full_scan_mul = IWL_FULL_SCAN_MULTIPLIER,
845         };
846
847         if (req->n_match_sets && req->match_sets[0].ssid.ssid_len) {
848                 IWL_DEBUG_SCAN(mvm,
849                                "Sending scheduled scan with filtering, filter len %d\n",
850                                req->n_match_sets);
851         } else {
852                 IWL_DEBUG_SCAN(mvm,
853                                "Sending Scheduled scan without filtering\n");
854                 scan_req.flags |= cpu_to_le16(IWL_SCAN_OFFLOAD_FLAG_PASS_ALL);
855         }
856
857         return iwl_mvm_send_cmd_pdu(mvm, SCAN_OFFLOAD_REQUEST_CMD, CMD_SYNC,
858                                     sizeof(scan_req), &scan_req);
859 }
860
861 static int iwl_mvm_send_sched_scan_abort(struct iwl_mvm *mvm)
862 {
863         int ret;
864         struct iwl_host_cmd cmd = {
865                 .id = SCAN_OFFLOAD_ABORT_CMD,
866                 .flags = CMD_SYNC,
867         };
868         u32 status;
869
870         /* Exit instantly with error when device is not ready
871          * to receive scan abort command or it does not perform
872          * scheduled scan currently */
873         if (mvm->scan_status != IWL_MVM_SCAN_SCHED)
874                 return -EIO;
875
876         ret = iwl_mvm_send_cmd_status(mvm, &cmd, &status);
877         if (ret)
878                 return ret;
879
880         if (status != CAN_ABORT_STATUS) {
881                 /*
882                  * The scan abort will return 1 for success or
883                  * 2 for "failure".  A failure condition can be
884                  * due to simply not being in an active scan which
885                  * can occur if we send the scan abort before the
886                  * microcode has notified us that a scan is completed.
887                  */
888                 IWL_DEBUG_SCAN(mvm, "SCAN OFFLOAD ABORT ret %d.\n", status);
889                 ret = -EIO;
890         }
891
892         return ret;
893 }
894
895 void iwl_mvm_sched_scan_stop(struct iwl_mvm *mvm)
896 {
897         int ret;
898
899         lockdep_assert_held(&mvm->mutex);
900
901         if (mvm->scan_status != IWL_MVM_SCAN_SCHED) {
902                 IWL_DEBUG_SCAN(mvm, "No offloaded scan to stop\n");
903                 return;
904         }
905
906         ret = iwl_mvm_send_sched_scan_abort(mvm);
907         if (ret)
908                 IWL_DEBUG_SCAN(mvm, "Send stop offload scan failed %d\n", ret);
909         else
910                 IWL_DEBUG_SCAN(mvm, "Successfully sent stop offload scan\n");
911 }