Merge tag 'mac80211-next-for-davem-2016-02-26' into next2
[deliverable/linux.git] / drivers / net / wireless / intel / iwlwifi / mvm / d3.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 - 2015 Intel Mobile Communications GmbH
10 * Copyright(c) 2016 Intel Deutschland GmbH
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * USA
25 *
26 * The full GNU General Public License is included in this distribution
27 * in the file called COPYING.
28 *
29 * Contact Information:
30 * Intel Linux Wireless <linuxwifi@intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
35 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
36 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
37 * Copyright(c) 2016 Intel Deutschland GmbH
38 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
49 * distribution.
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 *****************************************************************************/
67
68 #include <linux/etherdevice.h>
69 #include <linux/ip.h>
70 #include <linux/fs.h>
71 #include <net/cfg80211.h>
72 #include <net/ipv6.h>
73 #include <net/tcp.h>
74 #include <net/addrconf.h>
75 #include "iwl-modparams.h"
76 #include "fw-api.h"
77 #include "mvm.h"
78
79 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
80 struct ieee80211_vif *vif,
81 struct cfg80211_gtk_rekey_data *data)
82 {
83 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
84 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
85
86 if (iwlwifi_mod_params.sw_crypto)
87 return;
88
89 mutex_lock(&mvm->mutex);
90
91 memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
92 memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
93 mvmvif->rekey_data.replay_ctr =
94 cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
95 mvmvif->rekey_data.valid = true;
96
97 mutex_unlock(&mvm->mutex);
98 }
99
100 #if IS_ENABLED(CONFIG_IPV6)
101 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
102 struct ieee80211_vif *vif,
103 struct inet6_dev *idev)
104 {
105 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
106 struct inet6_ifaddr *ifa;
107 int idx = 0;
108
109 memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
110
111 read_lock_bh(&idev->lock);
112 list_for_each_entry(ifa, &idev->addr_list, if_list) {
113 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
114 if (ifa->flags & IFA_F_TENTATIVE)
115 __set_bit(idx, mvmvif->tentative_addrs);
116 idx++;
117 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
118 break;
119 }
120 read_unlock_bh(&idev->lock);
121
122 mvmvif->num_target_ipv6_addrs = idx;
123 }
124 #endif
125
126 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
127 struct ieee80211_vif *vif, int idx)
128 {
129 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
130
131 mvmvif->tx_key_idx = idx;
132 }
133
134 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
135 {
136 int i;
137
138 for (i = 0; i < IWL_P1K_SIZE; i++)
139 out[i] = cpu_to_le16(p1k[i]);
140 }
141
142 static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
143 struct iwl_mvm_key_pn *ptk_pn,
144 struct ieee80211_key_seq *seq,
145 int tid, int queues)
146 {
147 const u8 *ret = seq->ccmp.pn;
148 int i;
149
150 /* get the PN from mac80211, used on the default queue */
151 ieee80211_get_key_rx_seq(key, tid, seq);
152
153 /* and use the internal data for the other queues */
154 for (i = 1; i < queues; i++) {
155 const u8 *tmp = ptk_pn->q[i].pn[tid];
156
157 if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
158 ret = tmp;
159 }
160
161 return ret;
162 }
163
164 struct wowlan_key_data {
165 struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
166 struct iwl_wowlan_tkip_params_cmd *tkip;
167 bool error, use_rsc_tsc, use_tkip, configure_keys;
168 int wep_key_idx;
169 };
170
171 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
172 struct ieee80211_vif *vif,
173 struct ieee80211_sta *sta,
174 struct ieee80211_key_conf *key,
175 void *_data)
176 {
177 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
178 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
179 struct wowlan_key_data *data = _data;
180 struct aes_sc *aes_sc, *aes_tx_sc = NULL;
181 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
182 struct iwl_p1k_cache *rx_p1ks;
183 u8 *rx_mic_key;
184 struct ieee80211_key_seq seq;
185 u32 cur_rx_iv32 = 0;
186 u16 p1k[IWL_P1K_SIZE];
187 int ret, i;
188
189 switch (key->cipher) {
190 case WLAN_CIPHER_SUITE_WEP40:
191 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
192 struct {
193 struct iwl_mvm_wep_key_cmd wep_key_cmd;
194 struct iwl_mvm_wep_key wep_key;
195 } __packed wkc = {
196 .wep_key_cmd.mac_id_n_color =
197 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
198 mvmvif->color)),
199 .wep_key_cmd.num_keys = 1,
200 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
201 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
202 .wep_key.key_index = key->keyidx,
203 .wep_key.key_size = key->keylen,
204 };
205
206 /*
207 * This will fail -- the key functions don't set support
208 * pairwise WEP keys. However, that's better than silently
209 * failing WoWLAN. Or maybe not?
210 */
211 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
212 break;
213
214 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
215 if (key->keyidx == mvmvif->tx_key_idx) {
216 /* TX key must be at offset 0 */
217 wkc.wep_key.key_offset = 0;
218 } else {
219 /* others start at 1 */
220 data->wep_key_idx++;
221 wkc.wep_key.key_offset = data->wep_key_idx;
222 }
223
224 if (data->configure_keys) {
225 mutex_lock(&mvm->mutex);
226 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0,
227 sizeof(wkc), &wkc);
228 data->error = ret != 0;
229
230 mvm->ptk_ivlen = key->iv_len;
231 mvm->ptk_icvlen = key->icv_len;
232 mvm->gtk_ivlen = key->iv_len;
233 mvm->gtk_icvlen = key->icv_len;
234 mutex_unlock(&mvm->mutex);
235 }
236
237 /* don't upload key again */
238 return;
239 }
240 default:
241 data->error = true;
242 return;
243 case WLAN_CIPHER_SUITE_AES_CMAC:
244 /*
245 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
246 * but we also shouldn't abort suspend due to that. It does have
247 * support for the IGTK key renewal, but doesn't really use the
248 * IGTK for anything. This means we could spuriously wake up or
249 * be deauthenticated, but that was considered acceptable.
250 */
251 return;
252 case WLAN_CIPHER_SUITE_TKIP:
253 if (sta) {
254 u64 pn64;
255
256 tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
257 tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
258
259 rx_p1ks = data->tkip->rx_uni;
260
261 pn64 = atomic64_read(&key->tx_pn);
262 tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
263 tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
264
265 ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
266 p1k);
267 iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
268
269 memcpy(data->tkip->mic_keys.tx,
270 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
271 IWL_MIC_KEY_SIZE);
272
273 rx_mic_key = data->tkip->mic_keys.rx_unicast;
274 } else {
275 tkip_sc =
276 data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
277 rx_p1ks = data->tkip->rx_multi;
278 rx_mic_key = data->tkip->mic_keys.rx_mcast;
279 }
280
281 /*
282 * For non-QoS this relies on the fact that both the uCode and
283 * mac80211 use TID 0 (as they need to to avoid replay attacks)
284 * for checking the IV in the frames.
285 */
286 for (i = 0; i < IWL_NUM_RSC; i++) {
287 ieee80211_get_key_rx_seq(key, i, &seq);
288 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
289 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
290 /* wrapping isn't allowed, AP must rekey */
291 if (seq.tkip.iv32 > cur_rx_iv32)
292 cur_rx_iv32 = seq.tkip.iv32;
293 }
294
295 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
296 cur_rx_iv32, p1k);
297 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
298 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
299 cur_rx_iv32 + 1, p1k);
300 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
301
302 memcpy(rx_mic_key,
303 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
304 IWL_MIC_KEY_SIZE);
305
306 data->use_tkip = true;
307 data->use_rsc_tsc = true;
308 break;
309 case WLAN_CIPHER_SUITE_CCMP:
310 if (sta) {
311 u64 pn64;
312
313 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
314 aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
315
316 pn64 = atomic64_read(&key->tx_pn);
317 aes_tx_sc->pn = cpu_to_le64(pn64);
318 } else {
319 aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
320 }
321
322 /*
323 * For non-QoS this relies on the fact that both the uCode and
324 * mac80211/our RX code use TID 0 for checking the PN.
325 */
326 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
327 struct iwl_mvm_sta *mvmsta;
328 struct iwl_mvm_key_pn *ptk_pn;
329 const u8 *pn;
330
331 mvmsta = iwl_mvm_sta_from_mac80211(sta);
332 ptk_pn = rcu_dereference_protected(
333 mvmsta->ptk_pn[key->keyidx],
334 lockdep_is_held(&mvm->mutex));
335 if (WARN_ON(!ptk_pn))
336 break;
337
338 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
339 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
340 mvm->trans->num_rx_queues);
341 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
342 ((u64)pn[4] << 8) |
343 ((u64)pn[3] << 16) |
344 ((u64)pn[2] << 24) |
345 ((u64)pn[1] << 32) |
346 ((u64)pn[0] << 40));
347 }
348 } else {
349 for (i = 0; i < IWL_NUM_RSC; i++) {
350 u8 *pn = seq.ccmp.pn;
351
352 ieee80211_get_key_rx_seq(key, i, &seq);
353 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
354 ((u64)pn[4] << 8) |
355 ((u64)pn[3] << 16) |
356 ((u64)pn[2] << 24) |
357 ((u64)pn[1] << 32) |
358 ((u64)pn[0] << 40));
359 }
360 }
361 data->use_rsc_tsc = true;
362 break;
363 }
364
365 if (data->configure_keys) {
366 mutex_lock(&mvm->mutex);
367 /*
368 * The D3 firmware hardcodes the key offset 0 as the key it
369 * uses to transmit packets to the AP, i.e. the PTK.
370 */
371 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
372 mvm->ptk_ivlen = key->iv_len;
373 mvm->ptk_icvlen = key->icv_len;
374 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
375 } else {
376 /*
377 * firmware only supports TSC/RSC for a single key,
378 * so if there are multiple keep overwriting them
379 * with new ones -- this relies on mac80211 doing
380 * list_add_tail().
381 */
382 mvm->gtk_ivlen = key->iv_len;
383 mvm->gtk_icvlen = key->icv_len;
384 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
385 }
386 mutex_unlock(&mvm->mutex);
387 data->error = ret != 0;
388 }
389 }
390
391 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
392 struct cfg80211_wowlan *wowlan)
393 {
394 struct iwl_wowlan_patterns_cmd *pattern_cmd;
395 struct iwl_host_cmd cmd = {
396 .id = WOWLAN_PATTERNS,
397 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
398 };
399 int i, err;
400
401 if (!wowlan->n_patterns)
402 return 0;
403
404 cmd.len[0] = sizeof(*pattern_cmd) +
405 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
406
407 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
408 if (!pattern_cmd)
409 return -ENOMEM;
410
411 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
412
413 for (i = 0; i < wowlan->n_patterns; i++) {
414 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
415
416 memcpy(&pattern_cmd->patterns[i].mask,
417 wowlan->patterns[i].mask, mask_len);
418 memcpy(&pattern_cmd->patterns[i].pattern,
419 wowlan->patterns[i].pattern,
420 wowlan->patterns[i].pattern_len);
421 pattern_cmd->patterns[i].mask_size = mask_len;
422 pattern_cmd->patterns[i].pattern_size =
423 wowlan->patterns[i].pattern_len;
424 }
425
426 cmd.data[0] = pattern_cmd;
427 err = iwl_mvm_send_cmd(mvm, &cmd);
428 kfree(pattern_cmd);
429 return err;
430 }
431
432 enum iwl_mvm_tcp_packet_type {
433 MVM_TCP_TX_SYN,
434 MVM_TCP_RX_SYNACK,
435 MVM_TCP_TX_DATA,
436 MVM_TCP_RX_ACK,
437 MVM_TCP_RX_WAKE,
438 MVM_TCP_TX_FIN,
439 };
440
441 static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
442 {
443 __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
444 return cpu_to_le16(be16_to_cpu((__force __be16)check));
445 }
446
447 static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
448 struct cfg80211_wowlan_tcp *tcp,
449 void *_pkt, u8 *mask,
450 __le16 *pseudo_hdr_csum,
451 enum iwl_mvm_tcp_packet_type ptype)
452 {
453 struct {
454 struct ethhdr eth;
455 struct iphdr ip;
456 struct tcphdr tcp;
457 u8 data[];
458 } __packed *pkt = _pkt;
459 u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
460 int i;
461
462 pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
463 pkt->ip.version = 4;
464 pkt->ip.ihl = 5;
465 pkt->ip.protocol = IPPROTO_TCP;
466
467 switch (ptype) {
468 case MVM_TCP_TX_SYN:
469 case MVM_TCP_TX_DATA:
470 case MVM_TCP_TX_FIN:
471 memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
472 memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
473 pkt->ip.ttl = 128;
474 pkt->ip.saddr = tcp->src;
475 pkt->ip.daddr = tcp->dst;
476 pkt->tcp.source = cpu_to_be16(tcp->src_port);
477 pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
478 /* overwritten for TX SYN later */
479 pkt->tcp.doff = sizeof(struct tcphdr) / 4;
480 pkt->tcp.window = cpu_to_be16(65000);
481 break;
482 case MVM_TCP_RX_SYNACK:
483 case MVM_TCP_RX_ACK:
484 case MVM_TCP_RX_WAKE:
485 memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
486 memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
487 pkt->ip.saddr = tcp->dst;
488 pkt->ip.daddr = tcp->src;
489 pkt->tcp.source = cpu_to_be16(tcp->dst_port);
490 pkt->tcp.dest = cpu_to_be16(tcp->src_port);
491 break;
492 default:
493 WARN_ON(1);
494 return;
495 }
496
497 switch (ptype) {
498 case MVM_TCP_TX_SYN:
499 /* firmware assumes 8 option bytes - 8 NOPs for now */
500 memset(pkt->data, 0x01, 8);
501 ip_tot_len += 8;
502 pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
503 pkt->tcp.syn = 1;
504 break;
505 case MVM_TCP_TX_DATA:
506 ip_tot_len += tcp->payload_len;
507 memcpy(pkt->data, tcp->payload, tcp->payload_len);
508 pkt->tcp.psh = 1;
509 pkt->tcp.ack = 1;
510 break;
511 case MVM_TCP_TX_FIN:
512 pkt->tcp.fin = 1;
513 pkt->tcp.ack = 1;
514 break;
515 case MVM_TCP_RX_SYNACK:
516 pkt->tcp.syn = 1;
517 pkt->tcp.ack = 1;
518 break;
519 case MVM_TCP_RX_ACK:
520 pkt->tcp.ack = 1;
521 break;
522 case MVM_TCP_RX_WAKE:
523 ip_tot_len += tcp->wake_len;
524 pkt->tcp.psh = 1;
525 pkt->tcp.ack = 1;
526 memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
527 break;
528 }
529
530 switch (ptype) {
531 case MVM_TCP_TX_SYN:
532 case MVM_TCP_TX_DATA:
533 case MVM_TCP_TX_FIN:
534 pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
535 pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
536 break;
537 case MVM_TCP_RX_WAKE:
538 for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
539 u8 tmp = tcp->wake_mask[i];
540 mask[i + 6] |= tmp << 6;
541 if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
542 mask[i + 7] = tmp >> 2;
543 }
544 /* fall through for ethernet/IP/TCP headers mask */
545 case MVM_TCP_RX_SYNACK:
546 case MVM_TCP_RX_ACK:
547 mask[0] = 0xff; /* match ethernet */
548 /*
549 * match ethernet, ip.version, ip.ihl
550 * the ip.ihl half byte is really masked out by firmware
551 */
552 mask[1] = 0x7f;
553 mask[2] = 0x80; /* match ip.protocol */
554 mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
555 mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
556 mask[5] = 0x80; /* match tcp flags */
557 /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
558 break;
559 };
560
561 *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
562 pkt->ip.saddr, pkt->ip.daddr);
563 }
564
565 static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
566 struct ieee80211_vif *vif,
567 struct cfg80211_wowlan_tcp *tcp)
568 {
569 struct iwl_wowlan_remote_wake_config *cfg;
570 struct iwl_host_cmd cmd = {
571 .id = REMOTE_WAKE_CONFIG_CMD,
572 .len = { sizeof(*cfg), },
573 .dataflags = { IWL_HCMD_DFL_NOCOPY, },
574 };
575 int ret;
576
577 if (!tcp)
578 return 0;
579
580 cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
581 if (!cfg)
582 return -ENOMEM;
583 cmd.data[0] = cfg;
584
585 cfg->max_syn_retries = 10;
586 cfg->max_data_retries = 10;
587 cfg->tcp_syn_ack_timeout = 1; /* seconds */
588 cfg->tcp_ack_timeout = 1; /* seconds */
589
590 /* SYN (TX) */
591 iwl_mvm_build_tcp_packet(
592 vif, tcp, cfg->syn_tx.data, NULL,
593 &cfg->syn_tx.info.tcp_pseudo_header_checksum,
594 MVM_TCP_TX_SYN);
595 cfg->syn_tx.info.tcp_payload_length = 0;
596
597 /* SYN/ACK (RX) */
598 iwl_mvm_build_tcp_packet(
599 vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
600 &cfg->synack_rx.info.tcp_pseudo_header_checksum,
601 MVM_TCP_RX_SYNACK);
602 cfg->synack_rx.info.tcp_payload_length = 0;
603
604 /* KEEPALIVE/ACK (TX) */
605 iwl_mvm_build_tcp_packet(
606 vif, tcp, cfg->keepalive_tx.data, NULL,
607 &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
608 MVM_TCP_TX_DATA);
609 cfg->keepalive_tx.info.tcp_payload_length =
610 cpu_to_le16(tcp->payload_len);
611 cfg->sequence_number_offset = tcp->payload_seq.offset;
612 /* length must be 0..4, the field is little endian */
613 cfg->sequence_number_length = tcp->payload_seq.len;
614 cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
615 cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
616 if (tcp->payload_tok.len) {
617 cfg->token_offset = tcp->payload_tok.offset;
618 cfg->token_length = tcp->payload_tok.len;
619 cfg->num_tokens =
620 cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
621 memcpy(cfg->tokens, tcp->payload_tok.token_stream,
622 tcp->tokens_size);
623 } else {
624 /* set tokens to max value to almost never run out */
625 cfg->num_tokens = cpu_to_le16(65535);
626 }
627
628 /* ACK (RX) */
629 iwl_mvm_build_tcp_packet(
630 vif, tcp, cfg->keepalive_ack_rx.data,
631 cfg->keepalive_ack_rx.rx_mask,
632 &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
633 MVM_TCP_RX_ACK);
634 cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
635
636 /* WAKEUP (RX) */
637 iwl_mvm_build_tcp_packet(
638 vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
639 &cfg->wake_rx.info.tcp_pseudo_header_checksum,
640 MVM_TCP_RX_WAKE);
641 cfg->wake_rx.info.tcp_payload_length =
642 cpu_to_le16(tcp->wake_len);
643
644 /* FIN */
645 iwl_mvm_build_tcp_packet(
646 vif, tcp, cfg->fin_tx.data, NULL,
647 &cfg->fin_tx.info.tcp_pseudo_header_checksum,
648 MVM_TCP_TX_FIN);
649 cfg->fin_tx.info.tcp_payload_length = 0;
650
651 ret = iwl_mvm_send_cmd(mvm, &cmd);
652 kfree(cfg);
653
654 return ret;
655 }
656
657 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
658 struct ieee80211_sta *ap_sta)
659 {
660 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
661 struct ieee80211_chanctx_conf *ctx;
662 u8 chains_static, chains_dynamic;
663 struct cfg80211_chan_def chandef;
664 int ret, i;
665 struct iwl_binding_cmd binding_cmd = {};
666 struct iwl_time_quota_cmd quota_cmd = {};
667 u32 status;
668
669 /* add back the PHY */
670 if (WARN_ON(!mvmvif->phy_ctxt))
671 return -EINVAL;
672
673 rcu_read_lock();
674 ctx = rcu_dereference(vif->chanctx_conf);
675 if (WARN_ON(!ctx)) {
676 rcu_read_unlock();
677 return -EINVAL;
678 }
679 chandef = ctx->def;
680 chains_static = ctx->rx_chains_static;
681 chains_dynamic = ctx->rx_chains_dynamic;
682 rcu_read_unlock();
683
684 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
685 chains_static, chains_dynamic);
686 if (ret)
687 return ret;
688
689 /* add back the MAC */
690 mvmvif->uploaded = false;
691
692 if (WARN_ON(!vif->bss_conf.assoc))
693 return -EINVAL;
694
695 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
696 if (ret)
697 return ret;
698
699 /* add back binding - XXX refactor? */
700 binding_cmd.id_and_color =
701 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
702 mvmvif->phy_ctxt->color));
703 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
704 binding_cmd.phy =
705 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
706 mvmvif->phy_ctxt->color));
707 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
708 mvmvif->color));
709 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
710 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
711
712 status = 0;
713 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
714 sizeof(binding_cmd), &binding_cmd,
715 &status);
716 if (ret) {
717 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
718 return ret;
719 }
720
721 if (status) {
722 IWL_ERR(mvm, "Binding command failed: %u\n", status);
723 return -EIO;
724 }
725
726 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
727 if (ret)
728 return ret;
729 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
730
731 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
732 if (ret)
733 return ret;
734
735 /* and some quota */
736 quota_cmd.quotas[0].id_and_color =
737 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
738 mvmvif->phy_ctxt->color));
739 quota_cmd.quotas[0].quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
740 quota_cmd.quotas[0].max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
741
742 for (i = 1; i < MAX_BINDINGS; i++)
743 quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
744
745 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
746 sizeof(quota_cmd), &quota_cmd);
747 if (ret)
748 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
749
750 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm))
751 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
752
753 return 0;
754 }
755
756 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
757 struct ieee80211_vif *vif)
758 {
759 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
760 struct iwl_nonqos_seq_query_cmd query_cmd = {
761 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
762 .mac_id_n_color =
763 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
764 mvmvif->color)),
765 };
766 struct iwl_host_cmd cmd = {
767 .id = NON_QOS_TX_COUNTER_CMD,
768 .flags = CMD_WANT_SKB,
769 };
770 int err;
771 u32 size;
772
773 cmd.data[0] = &query_cmd;
774 cmd.len[0] = sizeof(query_cmd);
775
776 err = iwl_mvm_send_cmd(mvm, &cmd);
777 if (err)
778 return err;
779
780 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
781 if (size < sizeof(__le16)) {
782 err = -EINVAL;
783 } else {
784 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
785 /* firmware returns next, not last-used seqno */
786 err = (u16) (err - 0x10);
787 }
788
789 iwl_free_resp(&cmd);
790 return err;
791 }
792
793 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
794 {
795 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
796 struct iwl_nonqos_seq_query_cmd query_cmd = {
797 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
798 .mac_id_n_color =
799 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
800 mvmvif->color)),
801 .value = cpu_to_le16(mvmvif->seqno),
802 };
803
804 /* return if called during restart, not resume from D3 */
805 if (!mvmvif->seqno_valid)
806 return;
807
808 mvmvif->seqno_valid = false;
809
810 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
811 sizeof(query_cmd), &query_cmd))
812 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
813 }
814
815 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
816 {
817 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
818
819 iwl_trans_stop_device(mvm->trans);
820
821 /*
822 * Set the HW restart bit -- this is mostly true as we're
823 * going to load new firmware and reprogram that, though
824 * the reprogramming is going to be manual to avoid adding
825 * all the MACs that aren't support.
826 * We don't have to clear up everything though because the
827 * reprogramming is manual. When we resume, we'll actually
828 * go through a proper restart sequence again to switch
829 * back to the runtime firmware image.
830 */
831 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
832
833 /* the fw is reset, so all the keys are cleared */
834 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
835
836 mvm->ptk_ivlen = 0;
837 mvm->ptk_icvlen = 0;
838 mvm->ptk_ivlen = 0;
839 mvm->ptk_icvlen = 0;
840
841 return iwl_mvm_load_d3_fw(mvm);
842 }
843
844 static int
845 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
846 struct cfg80211_wowlan *wowlan,
847 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
848 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
849 struct ieee80211_sta *ap_sta)
850 {
851 int ret;
852 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
853
854 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
855
856 wowlan_config_cmd->is_11n_connection =
857 ap_sta->ht_cap.ht_supported;
858 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
859 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING |
860 ENABLE_STORE_BEACON;
861
862 /* Query the last used seqno and set it */
863 ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
864 if (ret < 0)
865 return ret;
866
867 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
868
869 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
870
871 if (wowlan->disconnect)
872 wowlan_config_cmd->wakeup_filter |=
873 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
874 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
875 if (wowlan->magic_pkt)
876 wowlan_config_cmd->wakeup_filter |=
877 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
878 if (wowlan->gtk_rekey_failure)
879 wowlan_config_cmd->wakeup_filter |=
880 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
881 if (wowlan->eap_identity_req)
882 wowlan_config_cmd->wakeup_filter |=
883 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
884 if (wowlan->four_way_handshake)
885 wowlan_config_cmd->wakeup_filter |=
886 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
887 if (wowlan->n_patterns)
888 wowlan_config_cmd->wakeup_filter |=
889 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
890
891 if (wowlan->rfkill_release)
892 wowlan_config_cmd->wakeup_filter |=
893 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
894
895 if (wowlan->tcp) {
896 /*
897 * Set the "link change" (really "link lost") flag as well
898 * since that implies losing the TCP connection.
899 */
900 wowlan_config_cmd->wakeup_filter |=
901 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
902 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
903 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
904 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
905 }
906
907 return 0;
908 }
909
910 static void
911 iwl_mvm_iter_d0i3_ap_keys(struct iwl_mvm *mvm,
912 struct ieee80211_vif *vif,
913 void (*iter)(struct ieee80211_hw *hw,
914 struct ieee80211_vif *vif,
915 struct ieee80211_sta *sta,
916 struct ieee80211_key_conf *key,
917 void *data),
918 void *data)
919 {
920 struct ieee80211_sta *ap_sta;
921
922 rcu_read_lock();
923
924 ap_sta = rcu_dereference(mvm->fw_id_to_mac_id[mvm->d0i3_ap_sta_id]);
925 if (IS_ERR_OR_NULL(ap_sta))
926 goto out;
927
928 ieee80211_iter_keys_rcu(mvm->hw, vif, iter, data);
929 out:
930 rcu_read_unlock();
931 }
932
933 int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
934 struct ieee80211_vif *vif,
935 bool d0i3,
936 u32 cmd_flags)
937 {
938 struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
939 struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
940 struct wowlan_key_data key_data = {
941 .configure_keys = !d0i3,
942 .use_rsc_tsc = false,
943 .tkip = &tkip_cmd,
944 .use_tkip = false,
945 };
946 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
947 int ret;
948
949 key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
950 if (!key_data.rsc_tsc)
951 return -ENOMEM;
952
953 /*
954 * if we have to configure keys, call ieee80211_iter_keys(),
955 * as we need non-atomic context in order to take the
956 * required locks.
957 * for the d0i3 we can't use ieee80211_iter_keys(), as
958 * taking (almost) any mutex might result in deadlock.
959 */
960 if (!d0i3) {
961 /*
962 * Note that currently we don't propagate cmd_flags
963 * to the iterator. In case of key_data.configure_keys,
964 * all the configured commands are SYNC, and
965 * iwl_mvm_wowlan_program_keys() will take care of
966 * locking/unlocking mvm->mutex.
967 */
968 ieee80211_iter_keys(mvm->hw, vif,
969 iwl_mvm_wowlan_program_keys,
970 &key_data);
971 } else {
972 iwl_mvm_iter_d0i3_ap_keys(mvm, vif,
973 iwl_mvm_wowlan_program_keys,
974 &key_data);
975 }
976
977 if (key_data.error) {
978 ret = -EIO;
979 goto out;
980 }
981
982 if (key_data.use_rsc_tsc) {
983 ret = iwl_mvm_send_cmd_pdu(mvm,
984 WOWLAN_TSC_RSC_PARAM, cmd_flags,
985 sizeof(*key_data.rsc_tsc),
986 key_data.rsc_tsc);
987 if (ret)
988 goto out;
989 }
990
991 if (key_data.use_tkip) {
992 ret = iwl_mvm_send_cmd_pdu(mvm,
993 WOWLAN_TKIP_PARAM,
994 cmd_flags, sizeof(tkip_cmd),
995 &tkip_cmd);
996 if (ret)
997 goto out;
998 }
999
1000 /* configure rekey data only if offloaded rekey is supported (d3) */
1001 if (mvmvif->rekey_data.valid && !d0i3) {
1002 memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
1003 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1004 NL80211_KCK_LEN);
1005 kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
1006 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1007 NL80211_KEK_LEN);
1008 kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
1009 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1010
1011 ret = iwl_mvm_send_cmd_pdu(mvm,
1012 WOWLAN_KEK_KCK_MATERIAL, cmd_flags,
1013 sizeof(kek_kck_cmd),
1014 &kek_kck_cmd);
1015 if (ret)
1016 goto out;
1017 }
1018 ret = 0;
1019 out:
1020 kfree(key_data.rsc_tsc);
1021 return ret;
1022 }
1023
1024 static int
1025 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1026 struct cfg80211_wowlan *wowlan,
1027 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1028 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1029 struct ieee80211_sta *ap_sta)
1030 {
1031 int ret;
1032 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1033 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1034
1035 if (!unified_image) {
1036 ret = iwl_mvm_switch_to_d3(mvm);
1037 if (ret)
1038 return ret;
1039
1040 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1041 if (ret)
1042 return ret;
1043 }
1044
1045 if (!iwlwifi_mod_params.sw_crypto) {
1046 /*
1047 * This needs to be unlocked due to lock ordering
1048 * constraints. Since we're in the suspend path
1049 * that isn't really a problem though.
1050 */
1051 mutex_unlock(&mvm->mutex);
1052 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, false,
1053 CMD_ASYNC);
1054 mutex_lock(&mvm->mutex);
1055 if (ret)
1056 return ret;
1057 }
1058
1059 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1060 sizeof(*wowlan_config_cmd),
1061 wowlan_config_cmd);
1062 if (ret)
1063 return ret;
1064
1065 ret = iwl_mvm_send_patterns(mvm, wowlan);
1066 if (ret)
1067 return ret;
1068
1069 ret = iwl_mvm_send_proto_offload(mvm, vif, false, true, 0);
1070 if (ret)
1071 return ret;
1072
1073 ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
1074 return ret;
1075 }
1076
1077 static int
1078 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1079 struct cfg80211_wowlan *wowlan,
1080 struct cfg80211_sched_scan_request *nd_config,
1081 struct ieee80211_vif *vif)
1082 {
1083 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1084 int ret;
1085 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1086 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1087
1088 if (!unified_image) {
1089 ret = iwl_mvm_switch_to_d3(mvm);
1090 if (ret)
1091 return ret;
1092 }
1093
1094 /* rfkill release can be either for wowlan or netdetect */
1095 if (wowlan->rfkill_release)
1096 wowlan_config_cmd.wakeup_filter |=
1097 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
1098
1099 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1100 sizeof(wowlan_config_cmd),
1101 &wowlan_config_cmd);
1102 if (ret)
1103 return ret;
1104
1105 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1106 IWL_MVM_SCAN_NETDETECT);
1107 if (ret)
1108 return ret;
1109
1110 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1111 return -EBUSY;
1112
1113 /* save the sched scan matchsets... */
1114 if (nd_config->n_match_sets) {
1115 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1116 sizeof(*nd_config->match_sets) *
1117 nd_config->n_match_sets,
1118 GFP_KERNEL);
1119 if (mvm->nd_match_sets)
1120 mvm->n_nd_match_sets = nd_config->n_match_sets;
1121 }
1122
1123 /* ...and the sched scan channels for later reporting */
1124 mvm->nd_channels = kmemdup(nd_config->channels,
1125 sizeof(*nd_config->channels) *
1126 nd_config->n_channels,
1127 GFP_KERNEL);
1128 if (mvm->nd_channels)
1129 mvm->n_nd_channels = nd_config->n_channels;
1130
1131 return 0;
1132 }
1133
1134 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1135 {
1136 kfree(mvm->nd_match_sets);
1137 mvm->nd_match_sets = NULL;
1138 mvm->n_nd_match_sets = 0;
1139 kfree(mvm->nd_channels);
1140 mvm->nd_channels = NULL;
1141 mvm->n_nd_channels = 0;
1142 }
1143
1144 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1145 struct cfg80211_wowlan *wowlan,
1146 bool test)
1147 {
1148 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1149 struct ieee80211_vif *vif = NULL;
1150 struct iwl_mvm_vif *mvmvif = NULL;
1151 struct ieee80211_sta *ap_sta = NULL;
1152 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1153 /*
1154 * Program the minimum sleep time to 10 seconds, as many
1155 * platforms have issues processing a wakeup signal while
1156 * still being in the process of suspending.
1157 */
1158 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1159 };
1160 struct iwl_host_cmd d3_cfg_cmd = {
1161 .id = D3_CONFIG_CMD,
1162 .flags = CMD_WANT_SKB,
1163 .data[0] = &d3_cfg_cmd_data,
1164 .len[0] = sizeof(d3_cfg_cmd_data),
1165 };
1166 int ret;
1167 int len __maybe_unused;
1168 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1169 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1170
1171 if (!wowlan) {
1172 /*
1173 * mac80211 shouldn't get here, but for D3 test
1174 * it doesn't warrant a warning
1175 */
1176 WARN_ON(!test);
1177 return -EINVAL;
1178 }
1179
1180 mutex_lock(&mvm->mutex);
1181
1182 vif = iwl_mvm_get_bss_vif(mvm);
1183 if (IS_ERR_OR_NULL(vif)) {
1184 ret = 1;
1185 goto out_noreset;
1186 }
1187
1188 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1189
1190 if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT) {
1191 /* if we're not associated, this must be netdetect */
1192 if (!wowlan->nd_config) {
1193 ret = 1;
1194 goto out_noreset;
1195 }
1196
1197 ret = iwl_mvm_netdetect_config(
1198 mvm, wowlan, wowlan->nd_config, vif);
1199 if (ret)
1200 goto out;
1201
1202 mvm->net_detect = true;
1203 } else {
1204 struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
1205
1206 ap_sta = rcu_dereference_protected(
1207 mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
1208 lockdep_is_held(&mvm->mutex));
1209 if (IS_ERR_OR_NULL(ap_sta)) {
1210 ret = -EINVAL;
1211 goto out_noreset;
1212 }
1213
1214 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1215 vif, mvmvif, ap_sta);
1216 if (ret)
1217 goto out_noreset;
1218 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1219 vif, mvmvif, ap_sta);
1220 if (ret)
1221 goto out;
1222
1223 mvm->net_detect = false;
1224 }
1225
1226 ret = iwl_mvm_power_update_device(mvm);
1227 if (ret)
1228 goto out;
1229
1230 ret = iwl_mvm_power_update_mac(mvm);
1231 if (ret)
1232 goto out;
1233
1234 #ifdef CONFIG_IWLWIFI_DEBUGFS
1235 if (mvm->d3_wake_sysassert)
1236 d3_cfg_cmd_data.wakeup_flags |=
1237 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1238 #endif
1239
1240 /* must be last -- this switches firmware state */
1241 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1242 if (ret)
1243 goto out;
1244 #ifdef CONFIG_IWLWIFI_DEBUGFS
1245 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1246 if (len >= sizeof(u32)) {
1247 mvm->d3_test_pme_ptr =
1248 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1249 }
1250 #endif
1251 iwl_free_resp(&d3_cfg_cmd);
1252
1253 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1254
1255 iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1256 out:
1257 if (ret < 0) {
1258 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
1259 ieee80211_restart_hw(mvm->hw);
1260 iwl_mvm_free_nd(mvm);
1261 }
1262 out_noreset:
1263 mutex_unlock(&mvm->mutex);
1264
1265 return ret;
1266 }
1267
1268 static int iwl_mvm_enter_d0i3_sync(struct iwl_mvm *mvm)
1269 {
1270 struct iwl_notification_wait wait_d3;
1271 static const u16 d3_notif[] = { D3_CONFIG_CMD };
1272 int ret;
1273
1274 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3,
1275 d3_notif, ARRAY_SIZE(d3_notif),
1276 NULL, NULL);
1277
1278 ret = iwl_mvm_enter_d0i3(mvm->hw->priv);
1279 if (ret)
1280 goto remove_notif;
1281
1282 ret = iwl_wait_notification(&mvm->notif_wait, &wait_d3, HZ);
1283 WARN_ON_ONCE(ret);
1284 return ret;
1285
1286 remove_notif:
1287 iwl_remove_notification(&mvm->notif_wait, &wait_d3);
1288 return ret;
1289 }
1290
1291 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1292 {
1293 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1294 struct iwl_trans *trans = mvm->trans;
1295 int ret;
1296
1297 /* make sure the d0i3 exit work is not pending */
1298 flush_work(&mvm->d0i3_exit_work);
1299
1300 ret = iwl_trans_suspend(trans);
1301 if (ret)
1302 return ret;
1303
1304 if (wowlan->any) {
1305 trans->system_pm_mode = IWL_PLAT_PM_MODE_D0I3;
1306
1307 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
1308 ret = iwl_mvm_enter_d0i3_sync(mvm);
1309
1310 if (ret)
1311 return ret;
1312 }
1313
1314 mutex_lock(&mvm->d0i3_suspend_mutex);
1315 __set_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
1316 mutex_unlock(&mvm->d0i3_suspend_mutex);
1317
1318 iwl_trans_d3_suspend(trans, false, false);
1319
1320 return 0;
1321 }
1322
1323 trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1324
1325 return __iwl_mvm_suspend(hw, wowlan, false);
1326 }
1327
1328 /* converted data from the different status responses */
1329 struct iwl_wowlan_status_data {
1330 u16 pattern_number;
1331 u16 qos_seq_ctr[8];
1332 u32 wakeup_reasons;
1333 u32 wake_packet_length;
1334 u32 wake_packet_bufsize;
1335 const u8 *wake_packet;
1336 };
1337
1338 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1339 struct ieee80211_vif *vif,
1340 struct iwl_wowlan_status_data *status)
1341 {
1342 struct sk_buff *pkt = NULL;
1343 struct cfg80211_wowlan_wakeup wakeup = {
1344 .pattern_idx = -1,
1345 };
1346 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1347 u32 reasons = status->wakeup_reasons;
1348
1349 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1350 wakeup_report = NULL;
1351 goto report;
1352 }
1353
1354 pm_wakeup_event(mvm->dev, 0);
1355
1356 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1357 wakeup.magic_pkt = true;
1358
1359 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1360 wakeup.pattern_idx =
1361 status->pattern_number;
1362
1363 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1364 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
1365 wakeup.disconnect = true;
1366
1367 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1368 wakeup.gtk_rekey_failure = true;
1369
1370 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1371 wakeup.rfkill_release = true;
1372
1373 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1374 wakeup.eap_identity_req = true;
1375
1376 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1377 wakeup.four_way_handshake = true;
1378
1379 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1380 wakeup.tcp_connlost = true;
1381
1382 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1383 wakeup.tcp_nomoretokens = true;
1384
1385 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1386 wakeup.tcp_match = true;
1387
1388 if (status->wake_packet_bufsize) {
1389 int pktsize = status->wake_packet_bufsize;
1390 int pktlen = status->wake_packet_length;
1391 const u8 *pktdata = status->wake_packet;
1392 struct ieee80211_hdr *hdr = (void *)pktdata;
1393 int truncated = pktlen - pktsize;
1394
1395 /* this would be a firmware bug */
1396 if (WARN_ON_ONCE(truncated < 0))
1397 truncated = 0;
1398
1399 if (ieee80211_is_data(hdr->frame_control)) {
1400 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1401 int ivlen = 0, icvlen = 4; /* also FCS */
1402
1403 pkt = alloc_skb(pktsize, GFP_KERNEL);
1404 if (!pkt)
1405 goto report;
1406
1407 memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
1408 pktdata += hdrlen;
1409 pktsize -= hdrlen;
1410
1411 if (ieee80211_has_protected(hdr->frame_control)) {
1412 /*
1413 * This is unlocked and using gtk_i(c)vlen,
1414 * but since everything is under RTNL still
1415 * that's not really a problem - changing
1416 * it would be difficult.
1417 */
1418 if (is_multicast_ether_addr(hdr->addr1)) {
1419 ivlen = mvm->gtk_ivlen;
1420 icvlen += mvm->gtk_icvlen;
1421 } else {
1422 ivlen = mvm->ptk_ivlen;
1423 icvlen += mvm->ptk_icvlen;
1424 }
1425 }
1426
1427 /* if truncated, FCS/ICV is (partially) gone */
1428 if (truncated >= icvlen) {
1429 icvlen = 0;
1430 truncated -= icvlen;
1431 } else {
1432 icvlen -= truncated;
1433 truncated = 0;
1434 }
1435
1436 pktsize -= ivlen + icvlen;
1437 pktdata += ivlen;
1438
1439 memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
1440
1441 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1442 goto report;
1443 wakeup.packet = pkt->data;
1444 wakeup.packet_present_len = pkt->len;
1445 wakeup.packet_len = pkt->len - truncated;
1446 wakeup.packet_80211 = false;
1447 } else {
1448 int fcslen = 4;
1449
1450 if (truncated >= 4) {
1451 truncated -= 4;
1452 fcslen = 0;
1453 } else {
1454 fcslen -= truncated;
1455 truncated = 0;
1456 }
1457 pktsize -= fcslen;
1458 wakeup.packet = status->wake_packet;
1459 wakeup.packet_present_len = pktsize;
1460 wakeup.packet_len = pktlen - truncated;
1461 wakeup.packet_80211 = true;
1462 }
1463 }
1464
1465 report:
1466 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1467 kfree_skb(pkt);
1468 }
1469
1470 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1471 struct ieee80211_key_seq *seq)
1472 {
1473 u64 pn;
1474
1475 pn = le64_to_cpu(sc->pn);
1476 seq->ccmp.pn[0] = pn >> 40;
1477 seq->ccmp.pn[1] = pn >> 32;
1478 seq->ccmp.pn[2] = pn >> 24;
1479 seq->ccmp.pn[3] = pn >> 16;
1480 seq->ccmp.pn[4] = pn >> 8;
1481 seq->ccmp.pn[5] = pn;
1482 }
1483
1484 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1485 struct ieee80211_key_seq *seq)
1486 {
1487 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1488 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1489 }
1490
1491 static void iwl_mvm_set_aes_rx_seq(struct iwl_mvm *mvm, struct aes_sc *scs,
1492 struct ieee80211_sta *sta,
1493 struct ieee80211_key_conf *key)
1494 {
1495 int tid;
1496
1497 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1498
1499 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
1500 struct iwl_mvm_sta *mvmsta;
1501 struct iwl_mvm_key_pn *ptk_pn;
1502
1503 mvmsta = iwl_mvm_sta_from_mac80211(sta);
1504
1505 ptk_pn = rcu_dereference_protected(mvmsta->ptk_pn[key->keyidx],
1506 lockdep_is_held(&mvm->mutex));
1507 if (WARN_ON(!ptk_pn))
1508 return;
1509
1510 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1511 struct ieee80211_key_seq seq = {};
1512 int i;
1513
1514 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1515 ieee80211_set_key_rx_seq(key, tid, &seq);
1516 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1517 memcpy(ptk_pn->q[i].pn[tid],
1518 seq.ccmp.pn, IEEE80211_CCMP_PN_LEN);
1519 }
1520 } else {
1521 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1522 struct ieee80211_key_seq seq = {};
1523
1524 iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
1525 ieee80211_set_key_rx_seq(key, tid, &seq);
1526 }
1527 }
1528 }
1529
1530 static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
1531 struct ieee80211_key_conf *key)
1532 {
1533 int tid;
1534
1535 BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
1536
1537 for (tid = 0; tid < IWL_NUM_RSC; tid++) {
1538 struct ieee80211_key_seq seq = {};
1539
1540 iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
1541 ieee80211_set_key_rx_seq(key, tid, &seq);
1542 }
1543 }
1544
1545 static void iwl_mvm_set_key_rx_seq(struct iwl_mvm *mvm,
1546 struct ieee80211_key_conf *key,
1547 struct iwl_wowlan_status *status)
1548 {
1549 union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
1550
1551 switch (key->cipher) {
1552 case WLAN_CIPHER_SUITE_CCMP:
1553 iwl_mvm_set_aes_rx_seq(mvm, rsc->aes.multicast_rsc, NULL, key);
1554 break;
1555 case WLAN_CIPHER_SUITE_TKIP:
1556 iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
1557 break;
1558 default:
1559 WARN_ON(1);
1560 }
1561 }
1562
1563 struct iwl_mvm_d3_gtk_iter_data {
1564 struct iwl_mvm *mvm;
1565 struct iwl_wowlan_status *status;
1566 void *last_gtk;
1567 u32 cipher;
1568 bool find_phase, unhandled_cipher;
1569 int num_keys;
1570 };
1571
1572 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1573 struct ieee80211_vif *vif,
1574 struct ieee80211_sta *sta,
1575 struct ieee80211_key_conf *key,
1576 void *_data)
1577 {
1578 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1579
1580 if (data->unhandled_cipher)
1581 return;
1582
1583 switch (key->cipher) {
1584 case WLAN_CIPHER_SUITE_WEP40:
1585 case WLAN_CIPHER_SUITE_WEP104:
1586 /* ignore WEP completely, nothing to do */
1587 return;
1588 case WLAN_CIPHER_SUITE_CCMP:
1589 case WLAN_CIPHER_SUITE_TKIP:
1590 /* we support these */
1591 break;
1592 default:
1593 /* everything else (even CMAC for MFP) - disconnect from AP */
1594 data->unhandled_cipher = true;
1595 return;
1596 }
1597
1598 data->num_keys++;
1599
1600 /*
1601 * pairwise key - update sequence counters only;
1602 * note that this assumes no TDLS sessions are active
1603 */
1604 if (sta) {
1605 struct ieee80211_key_seq seq = {};
1606 union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
1607
1608 if (data->find_phase)
1609 return;
1610
1611 switch (key->cipher) {
1612 case WLAN_CIPHER_SUITE_CCMP:
1613 iwl_mvm_set_aes_rx_seq(data->mvm, sc->aes.unicast_rsc,
1614 sta, key);
1615 atomic64_set(&key->tx_pn, le64_to_cpu(sc->aes.tsc.pn));
1616 break;
1617 case WLAN_CIPHER_SUITE_TKIP:
1618 iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
1619 iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
1620 atomic64_set(&key->tx_pn,
1621 (u64)seq.tkip.iv16 |
1622 ((u64)seq.tkip.iv32 << 16));
1623 break;
1624 }
1625
1626 /* that's it for this key */
1627 return;
1628 }
1629
1630 if (data->find_phase) {
1631 data->last_gtk = key;
1632 data->cipher = key->cipher;
1633 return;
1634 }
1635
1636 if (data->status->num_of_gtk_rekeys)
1637 ieee80211_remove_key(key);
1638 else if (data->last_gtk == key)
1639 iwl_mvm_set_key_rx_seq(data->mvm, key, data->status);
1640 }
1641
1642 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
1643 struct ieee80211_vif *vif,
1644 struct iwl_wowlan_status *status)
1645 {
1646 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1647 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1648 .mvm = mvm,
1649 .status = status,
1650 };
1651 u32 disconnection_reasons =
1652 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1653 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
1654
1655 if (!status || !vif->bss_conf.bssid)
1656 return false;
1657
1658 if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
1659 return false;
1660
1661 /* find last GTK that we used initially, if any */
1662 gtkdata.find_phase = true;
1663 ieee80211_iter_keys(mvm->hw, vif,
1664 iwl_mvm_d3_update_keys, &gtkdata);
1665 /* not trying to keep connections with MFP/unhandled ciphers */
1666 if (gtkdata.unhandled_cipher)
1667 return false;
1668 if (!gtkdata.num_keys)
1669 goto out;
1670 if (!gtkdata.last_gtk)
1671 return false;
1672
1673 /*
1674 * invalidate all other GTKs that might still exist and update
1675 * the one that we used
1676 */
1677 gtkdata.find_phase = false;
1678 ieee80211_iter_keys(mvm->hw, vif,
1679 iwl_mvm_d3_update_keys, &gtkdata);
1680
1681 if (status->num_of_gtk_rekeys) {
1682 struct ieee80211_key_conf *key;
1683 struct {
1684 struct ieee80211_key_conf conf;
1685 u8 key[32];
1686 } conf = {
1687 .conf.cipher = gtkdata.cipher,
1688 .conf.keyidx = status->gtk.key_index,
1689 };
1690
1691 switch (gtkdata.cipher) {
1692 case WLAN_CIPHER_SUITE_CCMP:
1693 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
1694 memcpy(conf.conf.key, status->gtk.decrypt_key,
1695 WLAN_KEY_LEN_CCMP);
1696 break;
1697 case WLAN_CIPHER_SUITE_TKIP:
1698 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
1699 memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
1700 /* leave TX MIC key zeroed, we don't use it anyway */
1701 memcpy(conf.conf.key +
1702 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1703 status->gtk.tkip_mic_key, 8);
1704 break;
1705 }
1706
1707 key = ieee80211_gtk_rekey_add(vif, &conf.conf);
1708 if (IS_ERR(key))
1709 return false;
1710 iwl_mvm_set_key_rx_seq(mvm, key, status);
1711 }
1712
1713 if (status->num_of_gtk_rekeys) {
1714 __be64 replay_ctr =
1715 cpu_to_be64(le64_to_cpu(status->replay_ctr));
1716 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
1717 (void *)&replay_ctr, GFP_KERNEL);
1718 }
1719
1720 out:
1721 mvmvif->seqno_valid = true;
1722 /* +0x10 because the set API expects next-to-use, not last-used */
1723 mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
1724
1725 return true;
1726 }
1727
1728 static struct iwl_wowlan_status *
1729 iwl_mvm_get_wakeup_status(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1730 {
1731 u32 base = mvm->error_event_table;
1732 struct error_table_start {
1733 /* cf. struct iwl_error_event_table */
1734 u32 valid;
1735 u32 error_id;
1736 } err_info;
1737 struct iwl_host_cmd cmd = {
1738 .id = WOWLAN_GET_STATUSES,
1739 .flags = CMD_WANT_SKB,
1740 };
1741 struct iwl_wowlan_status *status, *fw_status;
1742 int ret, len, status_size;
1743
1744 iwl_trans_read_mem_bytes(mvm->trans, base,
1745 &err_info, sizeof(err_info));
1746
1747 if (err_info.valid) {
1748 IWL_INFO(mvm, "error table is valid (%d) with error (%d)\n",
1749 err_info.valid, err_info.error_id);
1750 if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
1751 struct cfg80211_wowlan_wakeup wakeup = {
1752 .rfkill_release = true,
1753 };
1754 ieee80211_report_wowlan_wakeup(vif, &wakeup,
1755 GFP_KERNEL);
1756 }
1757 return ERR_PTR(-EIO);
1758 }
1759
1760 /* only for tracing for now */
1761 ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, 0, 0, NULL);
1762 if (ret)
1763 IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
1764
1765 ret = iwl_mvm_send_cmd(mvm, &cmd);
1766 if (ret) {
1767 IWL_ERR(mvm, "failed to query status (%d)\n", ret);
1768 return ERR_PTR(ret);
1769 }
1770
1771 /* RF-kill already asserted again... */
1772 if (!cmd.resp_pkt) {
1773 fw_status = ERR_PTR(-ERFKILL);
1774 goto out_free_resp;
1775 }
1776
1777 status_size = sizeof(*fw_status);
1778
1779 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1780 if (len < status_size) {
1781 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1782 fw_status = ERR_PTR(-EIO);
1783 goto out_free_resp;
1784 }
1785
1786 status = (void *)cmd.resp_pkt->data;
1787 if (len != (status_size +
1788 ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4))) {
1789 IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
1790 fw_status = ERR_PTR(-EIO);
1791 goto out_free_resp;
1792 }
1793
1794 fw_status = kmemdup(status, len, GFP_KERNEL);
1795
1796 out_free_resp:
1797 iwl_free_resp(&cmd);
1798 return fw_status;
1799 }
1800
1801 /* releases the MVM mutex */
1802 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
1803 struct ieee80211_vif *vif)
1804 {
1805 struct iwl_wowlan_status_data status;
1806 struct iwl_wowlan_status *fw_status;
1807 int i;
1808 bool keep;
1809 struct ieee80211_sta *ap_sta;
1810 struct iwl_mvm_sta *mvm_ap_sta;
1811
1812 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1813 if (IS_ERR_OR_NULL(fw_status))
1814 goto out_unlock;
1815
1816 status.pattern_number = le16_to_cpu(fw_status->pattern_number);
1817 for (i = 0; i < 8; i++)
1818 status.qos_seq_ctr[i] =
1819 le16_to_cpu(fw_status->qos_seq_ctr[i]);
1820 status.wakeup_reasons = le32_to_cpu(fw_status->wakeup_reasons);
1821 status.wake_packet_length =
1822 le32_to_cpu(fw_status->wake_packet_length);
1823 status.wake_packet_bufsize =
1824 le32_to_cpu(fw_status->wake_packet_bufsize);
1825 status.wake_packet = fw_status->wake_packet;
1826
1827 /* still at hard-coded place 0 for D3 image */
1828 ap_sta = rcu_dereference_protected(
1829 mvm->fw_id_to_mac_id[0],
1830 lockdep_is_held(&mvm->mutex));
1831 if (IS_ERR_OR_NULL(ap_sta))
1832 goto out_free;
1833
1834 mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
1835 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1836 u16 seq = status.qos_seq_ctr[i];
1837 /* firmware stores last-used value, we store next value */
1838 seq += 0x10;
1839 mvm_ap_sta->tid_data[i].seq_number = seq;
1840 }
1841
1842 /* now we have all the data we need, unlock to avoid mac80211 issues */
1843 mutex_unlock(&mvm->mutex);
1844
1845 iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
1846
1847 keep = iwl_mvm_setup_connection_keep(mvm, vif, fw_status);
1848
1849 kfree(fw_status);
1850 return keep;
1851
1852 out_free:
1853 kfree(fw_status);
1854 out_unlock:
1855 mutex_unlock(&mvm->mutex);
1856 return false;
1857 }
1858
1859 void iwl_mvm_d0i3_update_keys(struct iwl_mvm *mvm,
1860 struct ieee80211_vif *vif,
1861 struct iwl_wowlan_status *status)
1862 {
1863 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
1864 .mvm = mvm,
1865 .status = status,
1866 };
1867
1868 /*
1869 * rekey handling requires taking locks that can't be taken now.
1870 * however, d0i3 doesn't offload rekey, so we're fine.
1871 */
1872 if (WARN_ON_ONCE(status->num_of_gtk_rekeys))
1873 return;
1874
1875 /* find last GTK that we used initially, if any */
1876 gtkdata.find_phase = true;
1877 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1878
1879 gtkdata.find_phase = false;
1880 iwl_mvm_iter_d0i3_ap_keys(mvm, vif, iwl_mvm_d3_update_keys, &gtkdata);
1881 }
1882
1883 struct iwl_mvm_nd_query_results {
1884 u32 matched_profiles;
1885 struct iwl_scan_offload_profile_match matches[IWL_SCAN_MAX_PROFILES];
1886 };
1887
1888 static int
1889 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
1890 struct iwl_mvm_nd_query_results *results)
1891 {
1892 struct iwl_scan_offload_profiles_query *query;
1893 struct iwl_host_cmd cmd = {
1894 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
1895 .flags = CMD_WANT_SKB,
1896 };
1897 int ret, len;
1898
1899 ret = iwl_mvm_send_cmd(mvm, &cmd);
1900 if (ret) {
1901 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
1902 return ret;
1903 }
1904
1905 /* RF-kill already asserted again... */
1906 if (!cmd.resp_pkt) {
1907 ret = -ERFKILL;
1908 goto out_free_resp;
1909 }
1910
1911 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
1912 if (len < sizeof(*query)) {
1913 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
1914 ret = -EIO;
1915 goto out_free_resp;
1916 }
1917
1918 query = (void *)cmd.resp_pkt->data;
1919
1920 results->matched_profiles = le32_to_cpu(query->matched_profiles);
1921 memcpy(results->matches, query->matches, sizeof(results->matches));
1922
1923 #ifdef CONFIG_IWLWIFI_DEBUGFS
1924 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
1925 #endif
1926
1927 out_free_resp:
1928 iwl_free_resp(&cmd);
1929 return ret;
1930 }
1931
1932 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
1933 struct ieee80211_vif *vif)
1934 {
1935 struct cfg80211_wowlan_nd_info *net_detect = NULL;
1936 struct cfg80211_wowlan_wakeup wakeup = {
1937 .pattern_idx = -1,
1938 };
1939 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1940 struct iwl_mvm_nd_query_results query;
1941 struct iwl_wowlan_status *fw_status;
1942 unsigned long matched_profiles;
1943 u32 reasons = 0;
1944 int i, j, n_matches, ret;
1945
1946 fw_status = iwl_mvm_get_wakeup_status(mvm, vif);
1947 if (!IS_ERR_OR_NULL(fw_status)) {
1948 reasons = le32_to_cpu(fw_status->wakeup_reasons);
1949 kfree(fw_status);
1950 }
1951
1952 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1953 wakeup.rfkill_release = true;
1954
1955 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
1956 goto out;
1957
1958 ret = iwl_mvm_netdetect_query_results(mvm, &query);
1959 if (ret || !query.matched_profiles) {
1960 wakeup_report = NULL;
1961 goto out;
1962 }
1963
1964 matched_profiles = query.matched_profiles;
1965 if (mvm->n_nd_match_sets) {
1966 n_matches = hweight_long(matched_profiles);
1967 } else {
1968 IWL_ERR(mvm, "no net detect match information available\n");
1969 n_matches = 0;
1970 }
1971
1972 net_detect = kzalloc(sizeof(*net_detect) +
1973 (n_matches * sizeof(net_detect->matches[0])),
1974 GFP_KERNEL);
1975 if (!net_detect || !n_matches)
1976 goto out_report_nd;
1977
1978 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
1979 struct iwl_scan_offload_profile_match *fw_match;
1980 struct cfg80211_wowlan_nd_match *match;
1981 int idx, n_channels = 0;
1982
1983 fw_match = &query.matches[i];
1984
1985 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; j++)
1986 n_channels += hweight8(fw_match->matching_channels[j]);
1987
1988 match = kzalloc(sizeof(*match) +
1989 (n_channels * sizeof(*match->channels)),
1990 GFP_KERNEL);
1991 if (!match)
1992 goto out_report_nd;
1993
1994 net_detect->matches[net_detect->n_matches++] = match;
1995
1996 /* We inverted the order of the SSIDs in the scan
1997 * request, so invert the index here.
1998 */
1999 idx = mvm->n_nd_match_sets - i - 1;
2000 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2001 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2002 match->ssid.ssid_len);
2003
2004 if (mvm->n_nd_channels < n_channels)
2005 continue;
2006
2007 for (j = 0; j < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; j++)
2008 if (fw_match->matching_channels[j / 8] & (BIT(j % 8)))
2009 match->channels[match->n_channels++] =
2010 mvm->nd_channels[j]->center_freq;
2011 }
2012
2013 out_report_nd:
2014 wakeup.net_detect = net_detect;
2015 out:
2016 iwl_mvm_free_nd(mvm);
2017
2018 mutex_unlock(&mvm->mutex);
2019 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
2020
2021 if (net_detect) {
2022 for (i = 0; i < net_detect->n_matches; i++)
2023 kfree(net_detect->matches[i]);
2024 kfree(net_detect);
2025 }
2026 }
2027
2028 static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
2029 {
2030 #ifdef CONFIG_IWLWIFI_DEBUGFS
2031 const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
2032 u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
2033 u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
2034
2035 if (!mvm->store_d3_resume_sram)
2036 return;
2037
2038 if (!mvm->d3_resume_sram) {
2039 mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
2040 if (!mvm->d3_resume_sram)
2041 return;
2042 }
2043
2044 iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
2045 #endif
2046 }
2047
2048 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
2049 struct ieee80211_vif *vif)
2050 {
2051 /* skip the one we keep connection on */
2052 if (data == vif)
2053 return;
2054
2055 if (vif->type == NL80211_IFTYPE_STATION)
2056 ieee80211_resume_disconnect(vif);
2057 }
2058
2059 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
2060 {
2061 struct ieee80211_vif *vif = NULL;
2062 int ret = 1;
2063 enum iwl_d3_status d3_status;
2064 bool keep = false;
2065 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
2066 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
2067
2068 u32 flags = CMD_ASYNC | CMD_HIGH_PRIO | CMD_SEND_IN_IDLE |
2069 CMD_WAKE_UP_TRANS;
2070
2071 mutex_lock(&mvm->mutex);
2072
2073 /* get the BSS vif pointer again */
2074 vif = iwl_mvm_get_bss_vif(mvm);
2075 if (IS_ERR_OR_NULL(vif))
2076 goto err;
2077
2078 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !unified_image);
2079 if (ret)
2080 goto err;
2081
2082 if (d3_status != IWL_D3_STATUS_ALIVE) {
2083 IWL_INFO(mvm, "Device was reset during suspend\n");
2084 goto err;
2085 }
2086
2087 /* query SRAM first in case we want event logging */
2088 iwl_mvm_read_d3_sram(mvm);
2089
2090 /*
2091 * Query the current location and source from the D3 firmware so we
2092 * can play it back when we re-intiailize the D0 firmware
2093 */
2094 iwl_mvm_update_changed_regdom(mvm);
2095
2096 if (mvm->net_detect) {
2097 iwl_mvm_query_netdetect_reasons(mvm, vif);
2098 /* has unlocked the mutex, so skip that */
2099 goto out;
2100 } else {
2101 keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
2102 #ifdef CONFIG_IWLWIFI_DEBUGFS
2103 if (keep)
2104 mvm->keep_vif = vif;
2105 #endif
2106 /* has unlocked the mutex, so skip that */
2107 goto out_iterate;
2108 }
2109
2110 err:
2111 iwl_mvm_free_nd(mvm);
2112 mutex_unlock(&mvm->mutex);
2113
2114 out_iterate:
2115 if (!test)
2116 ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
2117 IEEE80211_IFACE_ITER_NORMAL,
2118 iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
2119
2120 out:
2121 if (unified_image && !ret) {
2122 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, flags, 0, NULL);
2123 if (!ret) /* D3 ended successfully - no need to reset device */
2124 return 0;
2125 }
2126
2127 /*
2128 * Reconfigure the device in one of the following cases:
2129 * 1. We are not using a unified image
2130 * 2. We are using a unified image but had an error while exiting D3
2131 */
2132 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
2133 set_bit(IWL_MVM_STATUS_D3_RECONFIG, &mvm->status);
2134 /*
2135 * When switching images we return 1, which causes mac80211
2136 * to do a reconfig with IEEE80211_RECONFIG_TYPE_RESTART.
2137 * This type of reconfig calls iwl_mvm_restart_complete(),
2138 * where we unref the IWL_MVM_REF_UCODE_DOWN, so we need
2139 * to take the reference here.
2140 */
2141 iwl_mvm_ref(mvm, IWL_MVM_REF_UCODE_DOWN);
2142
2143 return 1;
2144 }
2145
2146 static int iwl_mvm_resume_d3(struct iwl_mvm *mvm)
2147 {
2148 iwl_trans_resume(mvm->trans);
2149
2150 return __iwl_mvm_resume(mvm, false);
2151 }
2152
2153 static int iwl_mvm_resume_d0i3(struct iwl_mvm *mvm)
2154 {
2155 bool exit_now;
2156 enum iwl_d3_status d3_status;
2157 struct iwl_trans *trans = mvm->trans;
2158
2159 iwl_trans_d3_resume(trans, &d3_status, false, false);
2160
2161 /*
2162 * make sure to clear D0I3_DEFER_WAKEUP before
2163 * calling iwl_trans_resume(), which might wait
2164 * for d0i3 exit completion.
2165 */
2166 mutex_lock(&mvm->d0i3_suspend_mutex);
2167 __clear_bit(D0I3_DEFER_WAKEUP, &mvm->d0i3_suspend_flags);
2168 exit_now = __test_and_clear_bit(D0I3_PENDING_WAKEUP,
2169 &mvm->d0i3_suspend_flags);
2170 mutex_unlock(&mvm->d0i3_suspend_mutex);
2171 if (exit_now) {
2172 IWL_DEBUG_RPM(mvm, "Run deferred d0i3 exit\n");
2173 _iwl_mvm_exit_d0i3(mvm);
2174 }
2175
2176 iwl_trans_resume(trans);
2177
2178 if (iwl_mvm_enter_d0i3_on_suspend(mvm)) {
2179 int ret = iwl_mvm_exit_d0i3(mvm->hw->priv);
2180
2181 if (ret)
2182 return ret;
2183 /*
2184 * d0i3 exit will be deferred until reconfig_complete.
2185 * make sure there we are out of d0i3.
2186 */
2187 }
2188 return 0;
2189 }
2190
2191 int iwl_mvm_resume(struct ieee80211_hw *hw)
2192 {
2193 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2194 int ret;
2195
2196 if (mvm->trans->system_pm_mode == IWL_PLAT_PM_MODE_D0I3)
2197 ret = iwl_mvm_resume_d0i3(mvm);
2198 else
2199 ret = iwl_mvm_resume_d3(mvm);
2200
2201 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2202
2203 return ret;
2204 }
2205
2206 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
2207 {
2208 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2209
2210 device_set_wakeup_enable(mvm->trans->dev, enabled);
2211 }
2212
2213 #ifdef CONFIG_IWLWIFI_DEBUGFS
2214 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
2215 {
2216 struct iwl_mvm *mvm = inode->i_private;
2217 int err;
2218
2219 if (mvm->d3_test_active)
2220 return -EBUSY;
2221
2222 file->private_data = inode->i_private;
2223
2224 ieee80211_stop_queues(mvm->hw);
2225 synchronize_net();
2226
2227 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
2228
2229 /* start pseudo D3 */
2230 rtnl_lock();
2231 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
2232 rtnl_unlock();
2233 if (err > 0)
2234 err = -EINVAL;
2235 if (err) {
2236 ieee80211_wake_queues(mvm->hw);
2237 return err;
2238 }
2239 mvm->d3_test_active = true;
2240 mvm->keep_vif = NULL;
2241 return 0;
2242 }
2243
2244 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
2245 size_t count, loff_t *ppos)
2246 {
2247 struct iwl_mvm *mvm = file->private_data;
2248 u32 pme_asserted;
2249
2250 while (true) {
2251 /* read pme_ptr if available */
2252 if (mvm->d3_test_pme_ptr) {
2253 pme_asserted = iwl_trans_read_mem32(mvm->trans,
2254 mvm->d3_test_pme_ptr);
2255 if (pme_asserted)
2256 break;
2257 }
2258
2259 if (msleep_interruptible(100))
2260 break;
2261 }
2262
2263 return 0;
2264 }
2265
2266 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
2267 struct ieee80211_vif *vif)
2268 {
2269 /* skip the one we keep connection on */
2270 if (_data == vif)
2271 return;
2272
2273 if (vif->type == NL80211_IFTYPE_STATION)
2274 ieee80211_connection_loss(vif);
2275 }
2276
2277 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
2278 {
2279 struct iwl_mvm *mvm = inode->i_private;
2280 int remaining_time = 10;
2281
2282 mvm->d3_test_active = false;
2283
2284 rtnl_lock();
2285 __iwl_mvm_resume(mvm, true);
2286 rtnl_unlock();
2287
2288 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
2289
2290 iwl_abort_notification_waits(&mvm->notif_wait);
2291 ieee80211_restart_hw(mvm->hw);
2292
2293 /* wait for restart and disconnect all interfaces */
2294 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2295 remaining_time > 0) {
2296 remaining_time--;
2297 msleep(1000);
2298 }
2299
2300 if (remaining_time == 0)
2301 IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
2302
2303 ieee80211_iterate_active_interfaces_atomic(
2304 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2305 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
2306
2307 ieee80211_wake_queues(mvm->hw);
2308
2309 return 0;
2310 }
2311
2312 const struct file_operations iwl_dbgfs_d3_test_ops = {
2313 .llseek = no_llseek,
2314 .open = iwl_mvm_d3_test_open,
2315 .read = iwl_mvm_d3_test_read,
2316 .release = iwl_mvm_d3_test_release,
2317 };
2318 #endif
This page took 0.07856 seconds and 6 git commands to generate.