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