Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.8.
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * O(1) TX queue with built-in allocator.
  4 *
  5 * Copyright (c) 2017-2019, Silicon Laboratories, Inc.
  6 * Copyright (c) 2010, ST-Ericsson
  7 */
  8#include <linux/sched.h>
  9#include <net/mac80211.h>
 10
 11#include "queue.h"
 12#include "wfx.h"
 13#include "sta.h"
 14#include "data_tx.h"
 15#include "traces.h"
 16
 17void wfx_tx_lock(struct wfx_dev *wdev)
 18{
 19	atomic_inc(&wdev->tx_lock);
 20}
 21
 22void wfx_tx_unlock(struct wfx_dev *wdev)
 23{
 24	int tx_lock = atomic_dec_return(&wdev->tx_lock);
 25
 26	WARN(tx_lock < 0, "inconsistent tx_lock value");
 27	if (!tx_lock)
 28		wfx_bh_request_tx(wdev);
 29}
 30
 31void wfx_tx_flush(struct wfx_dev *wdev)
 32{
 33	int ret;
 34
 35	// Do not wait for any reply if chip is frozen
 36	if (wdev->chip_frozen)
 37		return;
 38
 39	wfx_tx_lock(wdev);
 40	mutex_lock(&wdev->hif_cmd.lock);
 41	ret = wait_event_timeout(wdev->hif.tx_buffers_empty,
 42				 !wdev->hif.tx_buffers_used,
 43				 msecs_to_jiffies(3000));
 44	if (!ret) {
 45		dev_warn(wdev->dev, "cannot flush tx buffers (%d still busy)\n",
 46			 wdev->hif.tx_buffers_used);
 47		wfx_pending_dump_old_frames(wdev, 3000);
 48		// FIXME: drop pending frames here
 49		wdev->chip_frozen = true;
 50	}
 51	mutex_unlock(&wdev->hif_cmd.lock);
 52	wfx_tx_unlock(wdev);
 53}
 54
 55void wfx_tx_lock_flush(struct wfx_dev *wdev)
 56{
 57	wfx_tx_lock(wdev);
 58	wfx_tx_flush(wdev);
 59}
 60
 61void wfx_tx_queues_init(struct wfx_vif *wvif)
 62{
 63	int i;
 64
 65	for (i = 0; i < IEEE80211_NUM_ACS; ++i) {
 66		skb_queue_head_init(&wvif->tx_queue[i].normal);
 67		skb_queue_head_init(&wvif->tx_queue[i].cab);
 68	}
 69}
 70
 71void wfx_tx_queues_check_empty(struct wfx_vif *wvif)
 72{
 73	int i;
 74
 75	for (i = 0; i < IEEE80211_NUM_ACS; ++i) {
 76		WARN_ON(atomic_read(&wvif->tx_queue[i].pending_frames));
 77		WARN_ON(!skb_queue_empty_lockless(&wvif->tx_queue[i].normal));
 78		WARN_ON(!skb_queue_empty_lockless(&wvif->tx_queue[i].cab));
 79	}
 80}
 81
 82bool wfx_tx_queue_empty(struct wfx_vif *wvif, struct wfx_queue *queue)
 83{
 84	return skb_queue_empty(&queue->normal) && skb_queue_empty(&queue->cab);
 85}
 86
 87static void __wfx_tx_queue_drop(struct wfx_vif *wvif,
 88				struct sk_buff_head *skb_queue,
 89				struct sk_buff_head *dropped)
 90{
 91	struct sk_buff *skb, *tmp;
 92
 93	spin_lock_bh(&skb_queue->lock);
 94	skb_queue_walk_safe(skb_queue, skb, tmp) {
 95		__skb_unlink(skb, skb_queue);
 96		skb_queue_head(dropped, skb);
 97	}
 98	spin_unlock_bh(&skb_queue->lock);
 99}
100
101void wfx_tx_queue_drop(struct wfx_vif *wvif, struct wfx_queue *queue,
102		       struct sk_buff_head *dropped)
103{
104	__wfx_tx_queue_drop(wvif, &queue->cab, dropped);
105	__wfx_tx_queue_drop(wvif, &queue->normal, dropped);
106	wake_up(&wvif->wdev->tx_dequeue);
107}
108
109void wfx_tx_queues_put(struct wfx_vif *wvif, struct sk_buff *skb)
110{
111	struct wfx_queue *queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
112	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
113
114	if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
115		skb_queue_tail(&queue->cab, skb);
116	else
117		skb_queue_tail(&queue->normal, skb);
118}
119
120void wfx_pending_drop(struct wfx_dev *wdev, struct sk_buff_head *dropped)
121{
122	struct wfx_queue *queue;
123	struct wfx_vif *wvif;
124	struct hif_msg *hif;
125	struct sk_buff *skb;
126
127	WARN(!wdev->chip_frozen, "%s should only be used to recover a frozen device",
128	     __func__);
129	while ((skb = skb_dequeue(&wdev->tx_pending)) != NULL) {
130		hif = (struct hif_msg *)skb->data;
131		wvif = wdev_to_wvif(wdev, hif->interface);
132		if (wvif) {
133			queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
134			WARN_ON(skb_get_queue_mapping(skb) > 3);
135			WARN_ON(!atomic_read(&queue->pending_frames));
136			atomic_dec(&queue->pending_frames);
137		}
138		skb_queue_head(dropped, skb);
139	}
140}
141
142struct sk_buff *wfx_pending_get(struct wfx_dev *wdev, u32 packet_id)
143{
144	struct wfx_queue *queue;
145	struct hif_req_tx *req;
146	struct wfx_vif *wvif;
147	struct hif_msg *hif;
148	struct sk_buff *skb;
149
150	spin_lock_bh(&wdev->tx_pending.lock);
151	skb_queue_walk(&wdev->tx_pending, skb) {
152		hif = (struct hif_msg *)skb->data;
153		req = (struct hif_req_tx *)hif->body;
154		if (req->packet_id != packet_id)
155			continue;
156		spin_unlock_bh(&wdev->tx_pending.lock);
157		wvif = wdev_to_wvif(wdev, hif->interface);
158		if (wvif) {
159			queue = &wvif->tx_queue[skb_get_queue_mapping(skb)];
160			WARN_ON(skb_get_queue_mapping(skb) > 3);
161			WARN_ON(!atomic_read(&queue->pending_frames));
162			atomic_dec(&queue->pending_frames);
163		}
164		skb_unlink(skb, &wdev->tx_pending);
165		return skb;
166	}
167	spin_unlock_bh(&wdev->tx_pending.lock);
168	WARN(1, "cannot find packet in pending queue");
169	return NULL;
170}
171
172void wfx_pending_dump_old_frames(struct wfx_dev *wdev, unsigned int limit_ms)
173{
174	ktime_t now = ktime_get();
175	struct wfx_tx_priv *tx_priv;
176	struct hif_req_tx *req;
177	struct sk_buff *skb;
178	bool first = true;
179
180	spin_lock_bh(&wdev->tx_pending.lock);
181	skb_queue_walk(&wdev->tx_pending, skb) {
182		tx_priv = wfx_skb_tx_priv(skb);
183		req = wfx_skb_txreq(skb);
184		if (ktime_after(now, ktime_add_ms(tx_priv->xmit_timestamp,
185						  limit_ms))) {
186			if (first) {
187				dev_info(wdev->dev, "frames stuck in firmware since %dms or more:\n",
188					 limit_ms);
189				first = false;
190			}
191			dev_info(wdev->dev, "   id %08x sent %lldms ago\n",
192				 req->packet_id,
193				 ktime_ms_delta(now, tx_priv->xmit_timestamp));
194		}
195	}
196	spin_unlock_bh(&wdev->tx_pending.lock);
197}
198
199unsigned int wfx_pending_get_pkt_us_delay(struct wfx_dev *wdev,
200					  struct sk_buff *skb)
201{
202	ktime_t now = ktime_get();
203	struct wfx_tx_priv *tx_priv = wfx_skb_tx_priv(skb);
204
205	return ktime_us_delta(now, tx_priv->xmit_timestamp);
206}
207
208bool wfx_tx_queues_has_cab(struct wfx_vif *wvif)
209{
210	int i;
211
212	if (wvif->vif->type != NL80211_IFTYPE_AP)
213		return false;
214	for (i = 0; i < IEEE80211_NUM_ACS; ++i)
215		// Note: since only AP can have mcast frames in queue and only
216		// one vif can be AP, all queued frames has same interface id
217		if (!skb_queue_empty_lockless(&wvif->tx_queue[i].cab))
218			return true;
219	return false;
220}
221
222static struct sk_buff *wfx_tx_queues_get_skb(struct wfx_dev *wdev)
223{
224	struct wfx_queue *queues[IEEE80211_NUM_ACS * ARRAY_SIZE(wdev->vif)];
225	int i, j, num_queues = 0;
226	struct wfx_vif *wvif;
227	struct hif_msg *hif;
228	struct sk_buff *skb;
229
230	// sort the queues
231	wvif = NULL;
232	while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
233		for (i = 0; i < IEEE80211_NUM_ACS; i++) {
234			WARN_ON(num_queues >= ARRAY_SIZE(queues));
235			queues[num_queues] = &wvif->tx_queue[i];
236			for (j = num_queues; j > 0; j--)
237				if (atomic_read(&queues[j]->pending_frames) <
238				    atomic_read(&queues[j - 1]->pending_frames))
239					swap(queues[j - 1], queues[j]);
240			num_queues++;
241		}
242	}
243
244	wvif = NULL;
245	while ((wvif = wvif_iterate(wdev, wvif)) != NULL) {
246		if (!wvif->after_dtim_tx_allowed)
247			continue;
248		for (i = 0; i < num_queues; i++) {
249			skb = skb_dequeue(&queues[i]->cab);
250			if (!skb)
251				continue;
252			// Note: since only AP can have mcast frames in queue
253			// and only one vif can be AP, all queued frames has
254			// same interface id
255			hif = (struct hif_msg *)skb->data;
256			WARN_ON(hif->interface != wvif->id);
257			WARN_ON(queues[i] !=
258				&wvif->tx_queue[skb_get_queue_mapping(skb)]);
259			atomic_inc(&queues[i]->pending_frames);
260			trace_queues_stats(wdev, queues[i]);
261			return skb;
262		}
263		// No more multicast to sent
264		wvif->after_dtim_tx_allowed = false;
265		schedule_work(&wvif->update_tim_work);
266	}
267
268	for (i = 0; i < num_queues; i++) {
269		skb = skb_dequeue(&queues[i]->normal);
270		if (skb) {
271			atomic_inc(&queues[i]->pending_frames);
272			trace_queues_stats(wdev, queues[i]);
273			return skb;
274		}
275	}
276	return NULL;
277}
278
279struct hif_msg *wfx_tx_queues_get(struct wfx_dev *wdev)
280{
281	struct wfx_tx_priv *tx_priv;
282	struct sk_buff *skb;
283
284	if (atomic_read(&wdev->tx_lock))
285		return NULL;
286	skb = wfx_tx_queues_get_skb(wdev);
287	if (!skb)
288		return NULL;
289	skb_queue_tail(&wdev->tx_pending, skb);
290	wake_up(&wdev->tx_dequeue);
291	tx_priv = wfx_skb_tx_priv(skb);
292	tx_priv->xmit_timestamp = ktime_get();
293	return (struct hif_msg *)skb->data;
294}