1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Datapath implementation.
4  *
5  * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6  * Copyright (c) 2010, ST-Ericsson
7  */
8 #include <net/mac80211.h>
9 #include <linux/etherdevice.h>
10 
11 #include "data_tx.h"
12 #include "wfx.h"
13 #include "bh.h"
14 #include "sta.h"
15 #include "queue.h"
16 #include "debug.h"
17 #include "traces.h"
18 #include "hif_tx_mib.h"
19 
wfx_get_hw_rate(struct wfx_dev * wdev,const struct ieee80211_tx_rate * rate)20 static int wfx_get_hw_rate(struct wfx_dev *wdev,
21 			   const struct ieee80211_tx_rate *rate)
22 {
23 	struct ieee80211_supported_band *band;
24 
25 	if (rate->idx < 0)
26 		return -1;
27 	if (rate->flags & IEEE80211_TX_RC_MCS) {
28 		if (rate->idx > 7) {
29 			WARN(1, "wrong rate->idx value: %d", rate->idx);
30 			return -1;
31 		}
32 		return rate->idx + 14;
33 	}
34 	// WFx only support 2GHz, else band information should be retrieved
35 	// from ieee80211_tx_info
36 	band = wdev->hw->wiphy->bands[NL80211_BAND_2GHZ];
37 	if (rate->idx >= band->n_bitrates) {
38 		WARN(1, "wrong rate->idx value: %d", rate->idx);
39 		return -1;
40 	}
41 	return band->bitrates[rate->idx].hw_value;
42 }
43 
44 /* TX policy cache implementation */
45 
wfx_tx_policy_build(struct wfx_vif * wvif,struct tx_policy * policy,struct ieee80211_tx_rate * rates)46 static void wfx_tx_policy_build(struct wfx_vif *wvif, struct tx_policy *policy,
47 				struct ieee80211_tx_rate *rates)
48 {
49 	struct wfx_dev *wdev = wvif->wdev;
50 	int i, rateid;
51 	u8 count;
52 
53 	WARN(rates[0].idx < 0, "invalid rate policy");
54 	memset(policy, 0, sizeof(*policy));
55 	for (i = 0; i < IEEE80211_TX_MAX_RATES; ++i) {
56 		if (rates[i].idx < 0)
57 			break;
58 		WARN_ON(rates[i].count > 15);
59 		rateid = wfx_get_hw_rate(wdev, &rates[i]);
60 		// Pack two values in each byte of policy->rates
61 		count = rates[i].count;
62 		if (rateid % 2)
63 			count <<= 4;
64 		policy->rates[rateid / 2] |= count;
65 	}
66 }
67 
tx_policy_is_equal(const struct tx_policy * a,const struct tx_policy * b)68 static bool tx_policy_is_equal(const struct tx_policy *a,
69 			       const struct tx_policy *b)
70 {
71 	return !memcmp(a->rates, b->rates, sizeof(a->rates));
72 }
73 
wfx_tx_policy_find(struct tx_policy_cache * cache,struct tx_policy * wanted)74 static int wfx_tx_policy_find(struct tx_policy_cache *cache,
75 			      struct tx_policy *wanted)
76 {
77 	struct tx_policy *it;
78 
79 	list_for_each_entry(it, &cache->used, link)
80 		if (tx_policy_is_equal(wanted, it))
81 			return it - cache->cache;
82 	list_for_each_entry(it, &cache->free, link)
83 		if (tx_policy_is_equal(wanted, it))
84 			return it - cache->cache;
85 	return -1;
86 }
87 
wfx_tx_policy_use(struct tx_policy_cache * cache,struct tx_policy * entry)88 static void wfx_tx_policy_use(struct tx_policy_cache *cache,
89 			      struct tx_policy *entry)
90 {
91 	++entry->usage_count;
92 	list_move(&entry->link, &cache->used);
93 }
94 
wfx_tx_policy_release(struct tx_policy_cache * cache,struct tx_policy * entry)95 static int wfx_tx_policy_release(struct tx_policy_cache *cache,
96 				 struct tx_policy *entry)
97 {
98 	int ret = --entry->usage_count;
99 
100 	if (!ret)
101 		list_move(&entry->link, &cache->free);
102 	return ret;
103 }
104 
wfx_tx_policy_get(struct wfx_vif * wvif,struct ieee80211_tx_rate * rates,bool * renew)105 static int wfx_tx_policy_get(struct wfx_vif *wvif,
106 			     struct ieee80211_tx_rate *rates, bool *renew)
107 {
108 	int idx;
109 	struct tx_policy_cache *cache = &wvif->tx_policy_cache;
110 	struct tx_policy wanted;
111 
112 	wfx_tx_policy_build(wvif, &wanted, rates);
113 
114 	spin_lock_bh(&cache->lock);
115 	if (list_empty(&cache->free)) {
116 		WARN(1, "unable to get a valid Tx policy");
117 		spin_unlock_bh(&cache->lock);
118 		return HIF_TX_RETRY_POLICY_INVALID;
119 	}
120 	idx = wfx_tx_policy_find(cache, &wanted);
121 	if (idx >= 0) {
122 		*renew = false;
123 	} else {
124 		struct tx_policy *entry;
125 		*renew = true;
126 		/* If policy is not found create a new one
127 		 * using the oldest entry in "free" list
128 		 */
129 		entry = list_entry(cache->free.prev, struct tx_policy, link);
130 		memcpy(entry->rates, wanted.rates, sizeof(entry->rates));
131 		entry->uploaded = false;
132 		entry->usage_count = 0;
133 		idx = entry - cache->cache;
134 	}
135 	wfx_tx_policy_use(cache, &cache->cache[idx]);
136 	if (list_empty(&cache->free))
137 		ieee80211_stop_queues(wvif->wdev->hw);
138 	spin_unlock_bh(&cache->lock);
139 	return idx;
140 }
141 
wfx_tx_policy_put(struct wfx_vif * wvif,int idx)142 static void wfx_tx_policy_put(struct wfx_vif *wvif, int idx)
143 {
144 	int usage, locked;
145 	struct tx_policy_cache *cache = &wvif->tx_policy_cache;
146 
147 	if (idx == HIF_TX_RETRY_POLICY_INVALID)
148 		return;
149 	spin_lock_bh(&cache->lock);
150 	locked = list_empty(&cache->free);
151 	usage = wfx_tx_policy_release(cache, &cache->cache[idx]);
152 	if (locked && !usage)
153 		ieee80211_wake_queues(wvif->wdev->hw);
154 	spin_unlock_bh(&cache->lock);
155 }
156 
wfx_tx_policy_upload(struct wfx_vif * wvif)157 static int wfx_tx_policy_upload(struct wfx_vif *wvif)
158 {
159 	struct tx_policy *policies = wvif->tx_policy_cache.cache;
160 	u8 tmp_rates[12];
161 	int i, is_used;
162 
163 	do {
164 		spin_lock_bh(&wvif->tx_policy_cache.lock);
165 		for (i = 0; i < ARRAY_SIZE(wvif->tx_policy_cache.cache); ++i) {
166 			is_used = memzcmp(policies[i].rates,
167 					  sizeof(policies[i].rates));
168 			if (!policies[i].uploaded && is_used)
169 				break;
170 		}
171 		if (i < ARRAY_SIZE(wvif->tx_policy_cache.cache)) {
172 			policies[i].uploaded = true;
173 			memcpy(tmp_rates, policies[i].rates, sizeof(tmp_rates));
174 			spin_unlock_bh(&wvif->tx_policy_cache.lock);
175 			hif_set_tx_rate_retry_policy(wvif, i, tmp_rates);
176 		} else {
177 			spin_unlock_bh(&wvif->tx_policy_cache.lock);
178 		}
179 	} while (i < ARRAY_SIZE(wvif->tx_policy_cache.cache));
180 	return 0;
181 }
182 
wfx_tx_policy_upload_work(struct work_struct * work)183 void wfx_tx_policy_upload_work(struct work_struct *work)
184 {
185 	struct wfx_vif *wvif =
186 		container_of(work, struct wfx_vif, tx_policy_upload_work);
187 
188 	wfx_tx_policy_upload(wvif);
189 	wfx_tx_unlock(wvif->wdev);
190 }
191 
wfx_tx_policy_init(struct wfx_vif * wvif)192 void wfx_tx_policy_init(struct wfx_vif *wvif)
193 {
194 	struct tx_policy_cache *cache = &wvif->tx_policy_cache;
195 	int i;
196 
197 	memset(cache, 0, sizeof(*cache));
198 
199 	spin_lock_init(&cache->lock);
200 	INIT_LIST_HEAD(&cache->used);
201 	INIT_LIST_HEAD(&cache->free);
202 
203 	for (i = 0; i < ARRAY_SIZE(cache->cache); ++i)
204 		list_add(&cache->cache[i].link, &cache->free);
205 }
206 
207 /* Tx implementation */
208 
ieee80211_is_action_back(struct ieee80211_hdr * hdr)209 static bool ieee80211_is_action_back(struct ieee80211_hdr *hdr)
210 {
211 	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)hdr;
212 
213 	if (!ieee80211_is_action(mgmt->frame_control))
214 		return false;
215 	if (mgmt->u.action.category != WLAN_CATEGORY_BACK)
216 		return false;
217 	return true;
218 }
219 
wfx_tx_get_link_id(struct wfx_vif * wvif,struct ieee80211_sta * sta,struct ieee80211_hdr * hdr)220 static u8 wfx_tx_get_link_id(struct wfx_vif *wvif, struct ieee80211_sta *sta,
221 			     struct ieee80211_hdr *hdr)
222 {
223 	struct wfx_sta_priv *sta_priv =
224 		sta ? (struct wfx_sta_priv *)&sta->drv_priv : NULL;
225 	const u8 *da = ieee80211_get_DA(hdr);
226 
227 	if (sta_priv && sta_priv->link_id)
228 		return sta_priv->link_id;
229 	if (wvif->vif->type != NL80211_IFTYPE_AP)
230 		return 0;
231 	if (is_multicast_ether_addr(da))
232 		return 0;
233 	return HIF_LINK_ID_NOT_ASSOCIATED;
234 }
235 
wfx_tx_fixup_rates(struct ieee80211_tx_rate * rates)236 static void wfx_tx_fixup_rates(struct ieee80211_tx_rate *rates)
237 {
238 	int i;
239 	bool finished;
240 
241 	// Firmware is not able to mix rates with different flags
242 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
243 		if (rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
244 			rates[i].flags |= IEEE80211_TX_RC_SHORT_GI;
245 		if (!(rates[0].flags & IEEE80211_TX_RC_SHORT_GI))
246 			rates[i].flags &= ~IEEE80211_TX_RC_SHORT_GI;
247 		if (!(rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS))
248 			rates[i].flags &= ~IEEE80211_TX_RC_USE_RTS_CTS;
249 	}
250 
251 	// Sort rates and remove duplicates
252 	do {
253 		finished = true;
254 		for (i = 0; i < IEEE80211_TX_MAX_RATES - 1; i++) {
255 			if (rates[i + 1].idx == rates[i].idx &&
256 			    rates[i].idx != -1) {
257 				rates[i].count += rates[i + 1].count;
258 				if (rates[i].count > 15)
259 					rates[i].count = 15;
260 				rates[i + 1].idx = -1;
261 				rates[i + 1].count = 0;
262 
263 				finished = false;
264 			}
265 			if (rates[i + 1].idx > rates[i].idx) {
266 				swap(rates[i + 1], rates[i]);
267 				finished = false;
268 			}
269 		}
270 	} while (!finished);
271 	// Ensure that MCS0 or 1Mbps is present at the end of the retry list
272 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
273 		if (rates[i].idx == 0)
274 			break;
275 		if (rates[i].idx == -1) {
276 			rates[i].idx = 0;
277 			rates[i].count = 8; // == hw->max_rate_tries
278 			rates[i].flags = rates[i - 1].flags &
279 					 IEEE80211_TX_RC_MCS;
280 			break;
281 		}
282 	}
283 	// All retries use long GI
284 	for (i = 1; i < IEEE80211_TX_MAX_RATES; i++)
285 		rates[i].flags &= ~IEEE80211_TX_RC_SHORT_GI;
286 }
287 
wfx_tx_get_rate_id(struct wfx_vif * wvif,struct ieee80211_tx_info * tx_info)288 static u8 wfx_tx_get_rate_id(struct wfx_vif *wvif,
289 			     struct ieee80211_tx_info *tx_info)
290 {
291 	bool tx_policy_renew = false;
292 	u8 rate_id;
293 
294 	rate_id = wfx_tx_policy_get(wvif,
295 				    tx_info->driver_rates, &tx_policy_renew);
296 	if (rate_id == HIF_TX_RETRY_POLICY_INVALID)
297 		dev_warn(wvif->wdev->dev, "unable to get a valid Tx policy");
298 
299 	if (tx_policy_renew) {
300 		wfx_tx_lock(wvif->wdev);
301 		if (!schedule_work(&wvif->tx_policy_upload_work))
302 			wfx_tx_unlock(wvif->wdev);
303 	}
304 	return rate_id;
305 }
306 
wfx_tx_get_frame_format(struct ieee80211_tx_info * tx_info)307 static int wfx_tx_get_frame_format(struct ieee80211_tx_info *tx_info)
308 {
309 	if (!(tx_info->driver_rates[0].flags & IEEE80211_TX_RC_MCS))
310 		return HIF_FRAME_FORMAT_NON_HT;
311 	else if (!(tx_info->driver_rates[0].flags & IEEE80211_TX_RC_GREEN_FIELD))
312 		return HIF_FRAME_FORMAT_MIXED_FORMAT_HT;
313 	else
314 		return HIF_FRAME_FORMAT_GF_HT_11N;
315 }
316 
wfx_tx_get_icv_len(struct ieee80211_key_conf * hw_key)317 static int wfx_tx_get_icv_len(struct ieee80211_key_conf *hw_key)
318 {
319 	int mic_space;
320 
321 	if (!hw_key)
322 		return 0;
323 	if (hw_key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
324 		return 0;
325 	mic_space = (hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) ? 8 : 0;
326 	return hw_key->icv_len + mic_space;
327 }
328 
wfx_tx_inner(struct wfx_vif * wvif,struct ieee80211_sta * sta,struct sk_buff * skb)329 static int wfx_tx_inner(struct wfx_vif *wvif, struct ieee80211_sta *sta,
330 			struct sk_buff *skb)
331 {
332 	struct hif_msg *hif_msg;
333 	struct hif_req_tx *req;
334 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
335 	struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
336 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
337 	int queue_id = skb_get_queue_mapping(skb);
338 	size_t offset = (size_t)skb->data & 3;
339 	int wmsg_len = sizeof(struct hif_msg) +
340 			sizeof(struct hif_req_tx) + offset;
341 
342 	WARN(queue_id >= IEEE80211_NUM_ACS, "unsupported queue_id");
343 	wfx_tx_fixup_rates(tx_info->driver_rates);
344 
345 	// From now tx_info->control is unusable
346 	memset(tx_info->rate_driver_data, 0, sizeof(struct wfx_tx_priv));
347 
348 	// Fill hif_msg
349 	WARN(skb_headroom(skb) < wmsg_len, "not enough space in skb");
350 	WARN(offset & 1, "attempt to transmit an unaligned frame");
351 	skb_put(skb, wfx_tx_get_icv_len(hw_key));
352 	skb_push(skb, wmsg_len);
353 	memset(skb->data, 0, wmsg_len);
354 	hif_msg = (struct hif_msg *)skb->data;
355 	hif_msg->len = cpu_to_le16(skb->len);
356 	hif_msg->id = HIF_REQ_ID_TX;
357 	hif_msg->interface = wvif->id;
358 	if (skb->len > wvif->wdev->hw_caps.size_inp_ch_buf) {
359 		dev_warn(wvif->wdev->dev,
360 			 "requested frame size (%d) is larger than maximum supported (%d)\n",
361 			 skb->len, wvif->wdev->hw_caps.size_inp_ch_buf);
362 		skb_pull(skb, wmsg_len);
363 		return -EIO;
364 	}
365 
366 	// Fill tx request
367 	req = (struct hif_req_tx *)hif_msg->body;
368 	// packet_id just need to be unique on device. 32bits are more than
369 	// necessary for that task, so we tae advantage of it to add some extra
370 	// data for debug.
371 	req->packet_id = atomic_add_return(1, &wvif->wdev->packet_id) & 0xFFFF;
372 	req->packet_id |= IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl)) << 16;
373 	req->packet_id |= queue_id << 28;
374 
375 	req->fc_offset = offset;
376 	if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
377 		req->after_dtim = 1;
378 	req->peer_sta_id = wfx_tx_get_link_id(wvif, sta, hdr);
379 	// Queue index are inverted between firmware and Linux
380 	req->queue_id = 3 - queue_id;
381 	req->retry_policy_index = wfx_tx_get_rate_id(wvif, tx_info);
382 	req->frame_format = wfx_tx_get_frame_format(tx_info);
383 	if (tx_info->driver_rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
384 		req->short_gi = 1;
385 
386 	// Auxiliary operations
387 	wfx_tx_queues_put(wvif, skb);
388 	if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
389 		schedule_work(&wvif->update_tim_work);
390 	wfx_bh_request_tx(wvif->wdev);
391 	return 0;
392 }
393 
wfx_tx(struct ieee80211_hw * hw,struct ieee80211_tx_control * control,struct sk_buff * skb)394 void wfx_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
395 	    struct sk_buff *skb)
396 {
397 	struct wfx_dev *wdev = hw->priv;
398 	struct wfx_vif *wvif;
399 	struct ieee80211_sta *sta = control ? control->sta : NULL;
400 	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
401 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
402 	size_t driver_data_room = sizeof_field(struct ieee80211_tx_info,
403 					       rate_driver_data);
404 
405 	compiletime_assert(sizeof(struct wfx_tx_priv) <= driver_data_room,
406 			   "struct tx_priv is too large");
407 	WARN(skb->next || skb->prev, "skb is already member of a list");
408 	// control.vif can be NULL for injected frames
409 	if (tx_info->control.vif)
410 		wvif = (struct wfx_vif *)tx_info->control.vif->drv_priv;
411 	else
412 		wvif = wvif_iterate(wdev, NULL);
413 	if (WARN_ON(!wvif))
414 		goto drop;
415 	// Because of TX_AMPDU_SETUP_IN_HW, mac80211 does not try to send any
416 	// BlockAck session management frame. The check below exist just in case.
417 	if (ieee80211_is_action_back(hdr)) {
418 		dev_info(wdev->dev, "drop BA action\n");
419 		goto drop;
420 	}
421 	if (wfx_tx_inner(wvif, sta, skb))
422 		goto drop;
423 
424 	return;
425 
426 drop:
427 	ieee80211_tx_status_irqsafe(wdev->hw, skb);
428 }
429 
wfx_skb_dtor(struct wfx_vif * wvif,struct sk_buff * skb)430 static void wfx_skb_dtor(struct wfx_vif *wvif, struct sk_buff *skb)
431 {
432 	struct hif_msg *hif = (struct hif_msg *)skb->data;
433 	struct hif_req_tx *req = (struct hif_req_tx *)hif->body;
434 	unsigned int offset = sizeof(struct hif_msg) +
435 			      sizeof(struct hif_req_tx) +
436 			      req->fc_offset;
437 
438 	if (!wvif) {
439 		pr_warn("%s: vif associated with the skb does not exist anymore\n", __func__);
440 		return;
441 	}
442 	wfx_tx_policy_put(wvif, req->retry_policy_index);
443 	skb_pull(skb, offset);
444 	ieee80211_tx_status_irqsafe(wvif->wdev->hw, skb);
445 }
446 
wfx_tx_fill_rates(struct wfx_dev * wdev,struct ieee80211_tx_info * tx_info,const struct hif_cnf_tx * arg)447 static void wfx_tx_fill_rates(struct wfx_dev *wdev,
448 			      struct ieee80211_tx_info *tx_info,
449 			      const struct hif_cnf_tx *arg)
450 {
451 	struct ieee80211_tx_rate *rate;
452 	int tx_count;
453 	int i;
454 
455 	tx_count = arg->ack_failures;
456 	if (!arg->status || arg->ack_failures)
457 		tx_count += 1; // Also report success
458 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
459 		rate = &tx_info->status.rates[i];
460 		if (rate->idx < 0)
461 			break;
462 		if (tx_count < rate->count &&
463 		    arg->status == HIF_STATUS_TX_FAIL_RETRIES &&
464 		    arg->ack_failures)
465 			dev_dbg(wdev->dev, "all retries were not consumed: %d != %d\n",
466 				rate->count, tx_count);
467 		if (tx_count <= rate->count && tx_count &&
468 		    arg->txed_rate != wfx_get_hw_rate(wdev, rate))
469 			dev_dbg(wdev->dev, "inconsistent tx_info rates: %d != %d\n",
470 				arg->txed_rate, wfx_get_hw_rate(wdev, rate));
471 		if (tx_count > rate->count) {
472 			tx_count -= rate->count;
473 		} else if (!tx_count) {
474 			rate->count = 0;
475 			rate->idx = -1;
476 		} else {
477 			rate->count = tx_count;
478 			tx_count = 0;
479 		}
480 	}
481 	if (tx_count)
482 		dev_dbg(wdev->dev, "%d more retries than expected\n", tx_count);
483 }
484 
wfx_tx_confirm_cb(struct wfx_dev * wdev,const struct hif_cnf_tx * arg)485 void wfx_tx_confirm_cb(struct wfx_dev *wdev, const struct hif_cnf_tx *arg)
486 {
487 	struct ieee80211_tx_info *tx_info;
488 	struct wfx_vif *wvif;
489 	struct sk_buff *skb;
490 
491 	skb = wfx_pending_get(wdev, arg->packet_id);
492 	if (!skb) {
493 		dev_warn(wdev->dev, "received unknown packet_id (%#.8x) from chip\n",
494 			 arg->packet_id);
495 		return;
496 	}
497 	tx_info = IEEE80211_SKB_CB(skb);
498 	wvif = wdev_to_wvif(wdev, ((struct hif_msg *)skb->data)->interface);
499 	WARN_ON(!wvif);
500 	if (!wvif)
501 		return;
502 
503 	// Note that wfx_pending_get_pkt_us_delay() get data from tx_info
504 	_trace_tx_stats(arg, skb, wfx_pending_get_pkt_us_delay(wdev, skb));
505 	wfx_tx_fill_rates(wdev, tx_info, arg);
506 	// From now, you can touch to tx_info->status, but do not touch to
507 	// tx_priv anymore
508 	// FIXME: use ieee80211_tx_info_clear_status()
509 	memset(tx_info->rate_driver_data, 0, sizeof(tx_info->rate_driver_data));
510 	memset(tx_info->pad, 0, sizeof(tx_info->pad));
511 
512 	if (!arg->status) {
513 		tx_info->status.tx_time =
514 			le32_to_cpu(arg->media_delay) -
515 			le32_to_cpu(arg->tx_queue_delay);
516 		if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
517 			tx_info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
518 		else
519 			tx_info->flags |= IEEE80211_TX_STAT_ACK;
520 	} else if (arg->status == HIF_STATUS_TX_FAIL_REQUEUE) {
521 		WARN(!arg->requeue, "incoherent status and result_flags");
522 		if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
523 			wvif->after_dtim_tx_allowed = false; // DTIM period elapsed
524 			schedule_work(&wvif->update_tim_work);
525 		}
526 		tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
527 	}
528 	wfx_skb_dtor(wvif, skb);
529 }
530 
wfx_flush_vif(struct wfx_vif * wvif,u32 queues,struct sk_buff_head * dropped)531 static void wfx_flush_vif(struct wfx_vif *wvif, u32 queues,
532 			  struct sk_buff_head *dropped)
533 {
534 	struct wfx_queue *queue;
535 	int i;
536 
537 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
538 		if (!(BIT(i) & queues))
539 			continue;
540 		queue = &wvif->tx_queue[i];
541 		if (dropped)
542 			wfx_tx_queue_drop(wvif, queue, dropped);
543 	}
544 	if (wvif->wdev->chip_frozen)
545 		return;
546 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
547 		if (!(BIT(i) & queues))
548 			continue;
549 		queue = &wvif->tx_queue[i];
550 		if (wait_event_timeout(wvif->wdev->tx_dequeue,
551 				       wfx_tx_queue_empty(wvif, queue),
552 				       msecs_to_jiffies(1000)) <= 0)
553 			dev_warn(wvif->wdev->dev,
554 				 "frames queued while flushing tx queues?");
555 	}
556 }
557 
wfx_flush(struct ieee80211_hw * hw,struct ieee80211_vif * vif,u32 queues,bool drop)558 void wfx_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
559 	       u32 queues, bool drop)
560 {
561 	struct wfx_dev *wdev = hw->priv;
562 	struct sk_buff_head dropped;
563 	struct wfx_vif *wvif;
564 	struct hif_msg *hif;
565 	struct sk_buff *skb;
566 
567 	skb_queue_head_init(&dropped);
568 	if (vif) {
569 		wvif = (struct wfx_vif *)vif->drv_priv;
570 		wfx_flush_vif(wvif, queues, drop ? &dropped : NULL);
571 	} else {
572 		wvif = NULL;
573 		while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
574 			wfx_flush_vif(wvif, queues, drop ? &dropped : NULL);
575 	}
576 	wfx_tx_flush(wdev);
577 	if (wdev->chip_frozen)
578 		wfx_pending_drop(wdev, &dropped);
579 	while ((skb = skb_dequeue(&dropped)) != NULL) {
580 		hif = (struct hif_msg *)skb->data;
581 		wvif = wdev_to_wvif(wdev, hif->interface);
582 		ieee80211_tx_info_clear_status(IEEE80211_SKB_CB(skb));
583 		wfx_skb_dtor(wvif, skb);
584 	}
585 }
586