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