queue.c 7.9 KB

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