queue.c 7.9 KB

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