2 * Copyright (c) 2015-2017 Qualcomm Atheros, Inc.
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <net/mac80211.h>
27 static const struct wiphy_wowlan_support ath10k_wowlan_support = {
28 .flags = WIPHY_WOWLAN_DISCONNECT |
29 WIPHY_WOWLAN_MAGIC_PKT,
30 .pattern_min_len = WOW_MIN_PATTERN_SIZE,
31 .pattern_max_len = WOW_MAX_PATTERN_SIZE,
32 .max_pkt_offset = WOW_MAX_PKT_OFFSET,
35 static int ath10k_wow_vif_cleanup(struct ath10k_vif *arvif)
37 struct ath10k *ar = arvif->ar;
40 for (i = 0; i < WOW_EVENT_MAX; i++) {
41 ret = ath10k_wmi_wow_add_wakeup_event(ar, arvif->vdev_id, i, 0);
43 ath10k_warn(ar, "failed to issue wow wakeup for event %s on vdev %i: %d\n",
44 wow_wakeup_event(i), arvif->vdev_id, ret);
49 for (i = 0; i < ar->wow.max_num_patterns; i++) {
50 ret = ath10k_wmi_wow_del_pattern(ar, arvif->vdev_id, i);
52 ath10k_warn(ar, "failed to delete wow pattern %d for vdev %i: %d\n",
53 i, arvif->vdev_id, ret);
61 static int ath10k_wow_cleanup(struct ath10k *ar)
63 struct ath10k_vif *arvif;
66 lockdep_assert_held(&ar->conf_mutex);
68 list_for_each_entry(arvif, &ar->arvifs, list) {
69 ret = ath10k_wow_vif_cleanup(arvif);
71 ath10k_warn(ar, "failed to clean wow wakeups on vdev %i: %d\n",
81 * Convert a 802.3 format to a 802.11 format.
82 * +------------+-----------+--------+----------------+
83 * 802.3: |dest mac(6B)|src mac(6B)|type(2B)| body... |
84 * +------------+-----------+--------+----------------+
85 * |__ |_______ |____________ |________
87 * +--+------------+----+-----------+---------------+-----------+
88 * 802.11: |4B|dest mac(6B)| 6B |src mac(6B)| 8B |type(2B)| body... |
89 * +--+------------+----+-----------+---------------+-----------+
91 static void ath10k_wow_convert_8023_to_80211
92 (struct cfg80211_pkt_pattern *new,
93 const struct cfg80211_pkt_pattern *old)
95 u8 hdr_8023_pattern[ETH_HLEN] = {};
96 u8 hdr_8023_bit_mask[ETH_HLEN] = {};
97 u8 hdr_80211_pattern[WOW_HDR_LEN] = {};
98 u8 hdr_80211_bit_mask[WOW_HDR_LEN] = {};
100 int total_len = old->pkt_offset + old->pattern_len;
101 int hdr_80211_end_offset;
103 struct ieee80211_hdr_3addr *new_hdr_pattern =
104 (struct ieee80211_hdr_3addr *)hdr_80211_pattern;
105 struct ieee80211_hdr_3addr *new_hdr_mask =
106 (struct ieee80211_hdr_3addr *)hdr_80211_bit_mask;
107 struct ethhdr *old_hdr_pattern = (struct ethhdr *)hdr_8023_pattern;
108 struct ethhdr *old_hdr_mask = (struct ethhdr *)hdr_8023_bit_mask;
109 int hdr_len = sizeof(*new_hdr_pattern);
111 struct rfc1042_hdr *new_rfc_pattern =
112 (struct rfc1042_hdr *)(hdr_80211_pattern + hdr_len);
113 struct rfc1042_hdr *new_rfc_mask =
114 (struct rfc1042_hdr *)(hdr_80211_bit_mask + hdr_len);
115 int rfc_len = sizeof(*new_rfc_pattern);
117 memcpy(hdr_8023_pattern + old->pkt_offset,
118 old->pattern, ETH_HLEN - old->pkt_offset);
119 memcpy(hdr_8023_bit_mask + old->pkt_offset,
120 old->mask, ETH_HLEN - old->pkt_offset);
122 /* Copy destination address */
123 memcpy(new_hdr_pattern->addr1, old_hdr_pattern->h_dest, ETH_ALEN);
124 memcpy(new_hdr_mask->addr1, old_hdr_mask->h_dest, ETH_ALEN);
126 /* Copy source address */
127 memcpy(new_hdr_pattern->addr3, old_hdr_pattern->h_source, ETH_ALEN);
128 memcpy(new_hdr_mask->addr3, old_hdr_mask->h_source, ETH_ALEN);
130 /* Copy logic link type */
131 memcpy(&new_rfc_pattern->snap_type,
132 &old_hdr_pattern->h_proto,
133 sizeof(old_hdr_pattern->h_proto));
134 memcpy(&new_rfc_mask->snap_type,
135 &old_hdr_mask->h_proto,
136 sizeof(old_hdr_mask->h_proto));
138 /* Caculate new pkt_offset */
139 if (old->pkt_offset < ETH_ALEN)
140 new->pkt_offset = old->pkt_offset +
141 offsetof(struct ieee80211_hdr_3addr, addr1);
142 else if (old->pkt_offset < offsetof(struct ethhdr, h_proto))
143 new->pkt_offset = old->pkt_offset +
144 offsetof(struct ieee80211_hdr_3addr, addr3) -
145 offsetof(struct ethhdr, h_source);
147 new->pkt_offset = old->pkt_offset + hdr_len + rfc_len - ETH_HLEN;
149 /* Caculate new hdr end offset */
150 if (total_len > ETH_HLEN)
151 hdr_80211_end_offset = hdr_len + rfc_len;
152 else if (total_len > offsetof(struct ethhdr, h_proto))
153 hdr_80211_end_offset = hdr_len + rfc_len + total_len - ETH_HLEN;
154 else if (total_len > ETH_ALEN)
155 hdr_80211_end_offset = total_len - ETH_ALEN +
156 offsetof(struct ieee80211_hdr_3addr, addr3);
158 hdr_80211_end_offset = total_len +
159 offsetof(struct ieee80211_hdr_3addr, addr1);
161 new->pattern_len = hdr_80211_end_offset - new->pkt_offset;
163 memcpy((u8 *)new->pattern,
164 hdr_80211_pattern + new->pkt_offset,
166 memcpy((u8 *)new->mask,
167 hdr_80211_bit_mask + new->pkt_offset,
170 if (total_len > ETH_HLEN) {
171 /* Copy frame body */
172 memcpy((u8 *)new->pattern + new->pattern_len,
173 (void *)old->pattern + ETH_HLEN - old->pkt_offset,
174 total_len - ETH_HLEN);
175 memcpy((u8 *)new->mask + new->pattern_len,
176 (void *)old->mask + ETH_HLEN - old->pkt_offset,
177 total_len - ETH_HLEN);
179 new->pattern_len += total_len - ETH_HLEN;
183 static int ath10k_vif_wow_set_wakeups(struct ath10k_vif *arvif,
184 struct cfg80211_wowlan *wowlan)
187 unsigned long wow_mask = 0;
188 struct ath10k *ar = arvif->ar;
189 const struct cfg80211_pkt_pattern *patterns = wowlan->patterns;
192 /* Setup requested WOW features */
193 switch (arvif->vdev_type) {
194 case WMI_VDEV_TYPE_IBSS:
195 __set_bit(WOW_BEACON_EVENT, &wow_mask);
197 case WMI_VDEV_TYPE_AP:
198 __set_bit(WOW_DEAUTH_RECVD_EVENT, &wow_mask);
199 __set_bit(WOW_DISASSOC_RECVD_EVENT, &wow_mask);
200 __set_bit(WOW_PROBE_REQ_WPS_IE_EVENT, &wow_mask);
201 __set_bit(WOW_AUTH_REQ_EVENT, &wow_mask);
202 __set_bit(WOW_ASSOC_REQ_EVENT, &wow_mask);
203 __set_bit(WOW_HTT_EVENT, &wow_mask);
204 __set_bit(WOW_RA_MATCH_EVENT, &wow_mask);
206 case WMI_VDEV_TYPE_STA:
207 if (wowlan->disconnect) {
208 __set_bit(WOW_DEAUTH_RECVD_EVENT, &wow_mask);
209 __set_bit(WOW_DISASSOC_RECVD_EVENT, &wow_mask);
210 __set_bit(WOW_BMISS_EVENT, &wow_mask);
211 __set_bit(WOW_CSA_IE_EVENT, &wow_mask);
214 if (wowlan->magic_pkt)
215 __set_bit(WOW_MAGIC_PKT_RECVD_EVENT, &wow_mask);
221 for (i = 0; i < wowlan->n_patterns; i++) {
222 u8 bitmask[WOW_MAX_PATTERN_SIZE] = {};
223 u8 ath_pattern[WOW_MAX_PATTERN_SIZE] = {};
224 u8 ath_bitmask[WOW_MAX_PATTERN_SIZE] = {};
225 struct cfg80211_pkt_pattern new_pattern = {};
226 struct cfg80211_pkt_pattern old_pattern = patterns[i];
229 new_pattern.pattern = ath_pattern;
230 new_pattern.mask = ath_bitmask;
231 if (patterns[i].pattern_len > WOW_MAX_PATTERN_SIZE)
233 /* convert bytemask to bitmask */
234 for (j = 0; j < patterns[i].pattern_len; j++)
235 if (patterns[i].mask[j / 8] & BIT(j % 8))
237 old_pattern.mask = bitmask;
238 new_pattern = old_pattern;
240 if (ar->wmi.rx_decap_mode == ATH10K_HW_TXRX_NATIVE_WIFI) {
241 if (patterns[i].pkt_offset < ETH_HLEN)
242 ath10k_wow_convert_8023_to_80211(&new_pattern,
245 new_pattern.pkt_offset += WOW_HDR_LEN - ETH_HLEN;
248 if (WARN_ON(new_pattern.pattern_len > WOW_MAX_PATTERN_SIZE))
251 ret = ath10k_wmi_wow_add_pattern(ar, arvif->vdev_id,
255 new_pattern.pattern_len,
256 new_pattern.pkt_offset);
258 ath10k_warn(ar, "failed to add pattern %i to vdev %i: %d\n",
260 arvif->vdev_id, ret);
265 __set_bit(WOW_PATTERN_MATCH_EVENT, &wow_mask);
268 for (i = 0; i < WOW_EVENT_MAX; i++) {
269 if (!test_bit(i, &wow_mask))
271 ret = ath10k_wmi_wow_add_wakeup_event(ar, arvif->vdev_id, i, 1);
273 ath10k_warn(ar, "failed to enable wakeup event %s on vdev %i: %d\n",
274 wow_wakeup_event(i), arvif->vdev_id, ret);
282 static int ath10k_wow_set_wakeups(struct ath10k *ar,
283 struct cfg80211_wowlan *wowlan)
285 struct ath10k_vif *arvif;
288 lockdep_assert_held(&ar->conf_mutex);
290 list_for_each_entry(arvif, &ar->arvifs, list) {
291 ret = ath10k_vif_wow_set_wakeups(arvif, wowlan);
293 ath10k_warn(ar, "failed to set wow wakeups on vdev %i: %d\n",
294 arvif->vdev_id, ret);
302 static int ath10k_wow_enable(struct ath10k *ar)
306 lockdep_assert_held(&ar->conf_mutex);
308 reinit_completion(&ar->target_suspend);
310 ret = ath10k_wmi_wow_enable(ar);
312 ath10k_warn(ar, "failed to issue wow enable: %d\n", ret);
316 ret = wait_for_completion_timeout(&ar->target_suspend, 3 * HZ);
318 ath10k_warn(ar, "timed out while waiting for suspend completion\n");
325 static int ath10k_wow_wakeup(struct ath10k *ar)
329 lockdep_assert_held(&ar->conf_mutex);
331 reinit_completion(&ar->wow.wakeup_completed);
333 ret = ath10k_wmi_wow_host_wakeup_ind(ar);
335 ath10k_warn(ar, "failed to send wow wakeup indication: %d\n",
340 ret = wait_for_completion_timeout(&ar->wow.wakeup_completed, 3 * HZ);
342 ath10k_warn(ar, "timed out while waiting for wow wakeup completion\n");
349 int ath10k_wow_op_suspend(struct ieee80211_hw *hw,
350 struct cfg80211_wowlan *wowlan)
352 struct ath10k *ar = hw->priv;
355 mutex_lock(&ar->conf_mutex);
357 if (WARN_ON(!test_bit(ATH10K_FW_FEATURE_WOWLAN_SUPPORT,
358 ar->running_fw->fw_file.fw_features))) {
363 ret = ath10k_wow_cleanup(ar);
365 ath10k_warn(ar, "failed to clear wow wakeup events: %d\n",
370 ret = ath10k_wow_set_wakeups(ar, wowlan);
372 ath10k_warn(ar, "failed to set wow wakeup events: %d\n",
377 ret = ath10k_wow_enable(ar);
379 ath10k_warn(ar, "failed to start wow: %d\n", ret);
383 ret = ath10k_hif_suspend(ar);
385 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
392 ath10k_wow_wakeup(ar);
395 ath10k_wow_cleanup(ar);
398 mutex_unlock(&ar->conf_mutex);
402 void ath10k_wow_op_set_wakeup(struct ieee80211_hw *hw, bool enabled)
404 struct ath10k *ar = hw->priv;
406 mutex_lock(&ar->conf_mutex);
407 if (test_bit(ATH10K_FW_FEATURE_WOWLAN_SUPPORT,
408 ar->running_fw->fw_file.fw_features)) {
409 device_set_wakeup_enable(ar->dev, enabled);
411 mutex_unlock(&ar->conf_mutex);
414 int ath10k_wow_op_resume(struct ieee80211_hw *hw)
416 struct ath10k *ar = hw->priv;
419 mutex_lock(&ar->conf_mutex);
421 if (WARN_ON(!test_bit(ATH10K_FW_FEATURE_WOWLAN_SUPPORT,
422 ar->running_fw->fw_file.fw_features))) {
427 ret = ath10k_hif_resume(ar);
429 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
433 ret = ath10k_wow_wakeup(ar);
435 ath10k_warn(ar, "failed to wakeup from wow: %d\n", ret);
440 case ATH10K_STATE_ON:
441 ar->state = ATH10K_STATE_RESTARTING;
444 case ATH10K_STATE_OFF:
445 case ATH10K_STATE_RESTARTING:
446 case ATH10K_STATE_RESTARTED:
447 case ATH10K_STATE_UTF:
448 case ATH10K_STATE_WEDGED:
449 ath10k_warn(ar, "encountered unexpected device state %d on resume, cannot recover\n",
456 mutex_unlock(&ar->conf_mutex);
460 int ath10k_wow_init(struct ath10k *ar)
462 if (!test_bit(ATH10K_FW_FEATURE_WOWLAN_SUPPORT,
463 ar->running_fw->fw_file.fw_features))
466 if (WARN_ON(!test_bit(WMI_SERVICE_WOW, ar->wmi.svc_map)))
469 ar->wow.wowlan_support = ath10k_wowlan_support;
471 if (ar->wmi.rx_decap_mode == ATH10K_HW_TXRX_NATIVE_WIFI) {
472 ar->wow.wowlan_support.pattern_max_len -= WOW_MAX_REDUCE;
473 ar->wow.wowlan_support.max_pkt_offset -= WOW_MAX_REDUCE;
476 ar->wow.wowlan_support.n_patterns = ar->wow.max_num_patterns;
477 ar->hw->wiphy->wowlan = &ar->wow.wowlan_support;
479 device_set_wakeup_capable(ar->dev, true);