1
2
3
4
5
6
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
20static 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
35
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
45
46static 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
61 count = rates[i].count;
62 if (rateid % 2)
63 count <<= 4;
64 policy->rates[rateid / 2] |= count;
65 }
66}
67
68static 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
74static 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
88static 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
95static 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
105static 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
127
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
142static 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
157static 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
183void 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
192void 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
208
209static 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
220static 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
236static void wfx_tx_fixup_rates(struct ieee80211_tx_rate *rates)
237{
238 int i;
239 bool finished;
240
241
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
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
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;
278 rates[i].flags = rates[i - 1].flags &
279 IEEE80211_TX_RC_MCS;
280 break;
281 }
282 }
283
284 for (i = 1; i < IEEE80211_TX_MAX_RATES; i++)
285 rates[i].flags &= ~IEEE80211_TX_RC_SHORT_GI;
286}
287
288static 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
307static 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
317static 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
329static 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 wfx_tx_priv *tx_priv;
335 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
336 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
337 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
338 int queue_id = skb_get_queue_mapping(skb);
339 size_t offset = (size_t)skb->data & 3;
340 int wmsg_len = sizeof(struct hif_msg) +
341 sizeof(struct hif_req_tx) + offset;
342
343 WARN(queue_id >= IEEE80211_NUM_ACS, "unsupported queue_id");
344 wfx_tx_fixup_rates(tx_info->driver_rates);
345
346
347 memset(tx_info->rate_driver_data, 0, sizeof(struct wfx_tx_priv));
348
349 tx_priv = (struct wfx_tx_priv *)tx_info->rate_driver_data;
350 tx_priv->icv_size = wfx_tx_get_icv_len(hw_key);
351
352
353 WARN(skb_headroom(skb) < wmsg_len, "not enough space in skb");
354 WARN(offset & 1, "attempt to transmit an unaligned frame");
355 skb_put(skb, tx_priv->icv_size);
356 skb_push(skb, wmsg_len);
357 memset(skb->data, 0, wmsg_len);
358 hif_msg = (struct hif_msg *)skb->data;
359 hif_msg->len = cpu_to_le16(skb->len);
360 hif_msg->id = HIF_REQ_ID_TX;
361 hif_msg->interface = wvif->id;
362 if (skb->len > wvif->wdev->hw_caps.size_inp_ch_buf) {
363 dev_warn(wvif->wdev->dev,
364 "requested frame size (%d) is larger than maximum supported (%d)\n",
365 skb->len, wvif->wdev->hw_caps.size_inp_ch_buf);
366 skb_pull(skb, wmsg_len);
367 return -EIO;
368 }
369
370
371 req = (struct hif_req_tx *)hif_msg->body;
372
373
374
375 req->packet_id = atomic_add_return(1, &wvif->wdev->packet_id) & 0xFFFF;
376 req->packet_id |= IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl)) << 16;
377 req->packet_id |= queue_id << 28;
378
379 req->fc_offset = offset;
380 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
381 req->after_dtim = 1;
382 req->peer_sta_id = wfx_tx_get_link_id(wvif, sta, hdr);
383
384 req->queue_id = 3 - queue_id;
385 req->retry_policy_index = wfx_tx_get_rate_id(wvif, tx_info);
386 req->frame_format = wfx_tx_get_frame_format(tx_info);
387 if (tx_info->driver_rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
388 req->short_gi = 1;
389
390
391 wfx_tx_queues_put(wvif, skb);
392 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
393 schedule_work(&wvif->update_tim_work);
394 wfx_bh_request_tx(wvif->wdev);
395 return 0;
396}
397
398void wfx_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
399 struct sk_buff *skb)
400{
401 struct wfx_dev *wdev = hw->priv;
402 struct wfx_vif *wvif;
403 struct ieee80211_sta *sta = control ? control->sta : NULL;
404 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
405 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
406 size_t driver_data_room = sizeof_field(struct ieee80211_tx_info,
407 rate_driver_data);
408
409 compiletime_assert(sizeof(struct wfx_tx_priv) <= driver_data_room,
410 "struct tx_priv is too large");
411 WARN(skb->next || skb->prev, "skb is already member of a list");
412
413 if (tx_info->control.vif)
414 wvif = (struct wfx_vif *)tx_info->control.vif->drv_priv;
415 else
416 wvif = wvif_iterate(wdev, NULL);
417 if (WARN_ON(!wvif))
418 goto drop;
419
420
421 if (ieee80211_is_action_back(hdr)) {
422 dev_info(wdev->dev, "drop BA action\n");
423 goto drop;
424 }
425 if (wfx_tx_inner(wvif, sta, skb))
426 goto drop;
427
428 return;
429
430drop:
431 ieee80211_tx_status_irqsafe(wdev->hw, skb);
432}
433
434static void wfx_skb_dtor(struct wfx_vif *wvif, struct sk_buff *skb)
435{
436 struct hif_msg *hif = (struct hif_msg *)skb->data;
437 struct hif_req_tx *req = (struct hif_req_tx *)hif->body;
438 unsigned int offset = sizeof(struct hif_msg) +
439 sizeof(struct hif_req_tx) +
440 req->fc_offset;
441
442 if (!wvif) {
443 pr_warn("%s: vif associated with the skb does not exist anymore\n", __func__);
444 return;
445 }
446 wfx_tx_policy_put(wvif, req->retry_policy_index);
447 skb_pull(skb, offset);
448 ieee80211_tx_status_irqsafe(wvif->wdev->hw, skb);
449}
450
451static void wfx_tx_fill_rates(struct wfx_dev *wdev,
452 struct ieee80211_tx_info *tx_info,
453 const struct hif_cnf_tx *arg)
454{
455 struct ieee80211_tx_rate *rate;
456 int tx_count;
457 int i;
458
459 tx_count = arg->ack_failures;
460 if (!arg->status || arg->ack_failures)
461 tx_count += 1;
462 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
463 rate = &tx_info->status.rates[i];
464 if (rate->idx < 0)
465 break;
466 if (tx_count < rate->count &&
467 arg->status == HIF_STATUS_TX_FAIL_RETRIES &&
468 arg->ack_failures)
469 dev_dbg(wdev->dev, "all retries were not consumed: %d != %d\n",
470 rate->count, tx_count);
471 if (tx_count <= rate->count && tx_count &&
472 arg->txed_rate != wfx_get_hw_rate(wdev, rate))
473 dev_dbg(wdev->dev, "inconsistent tx_info rates: %d != %d\n",
474 arg->txed_rate, wfx_get_hw_rate(wdev, rate));
475 if (tx_count > rate->count) {
476 tx_count -= rate->count;
477 } else if (!tx_count) {
478 rate->count = 0;
479 rate->idx = -1;
480 } else {
481 rate->count = tx_count;
482 tx_count = 0;
483 }
484 }
485 if (tx_count)
486 dev_dbg(wdev->dev, "%d more retries than expected\n", tx_count);
487}
488
489void wfx_tx_confirm_cb(struct wfx_dev *wdev, const struct hif_cnf_tx *arg)
490{
491 const struct wfx_tx_priv *tx_priv;
492 struct ieee80211_tx_info *tx_info;
493 struct wfx_vif *wvif;
494 struct sk_buff *skb;
495
496 skb = wfx_pending_get(wdev, arg->packet_id);
497 if (!skb) {
498 dev_warn(wdev->dev, "received unknown packet_id (%#.8x) from chip\n",
499 arg->packet_id);
500 return;
501 }
502 tx_info = IEEE80211_SKB_CB(skb);
503 tx_priv = wfx_skb_tx_priv(skb);
504 wvif = wdev_to_wvif(wdev, ((struct hif_msg *)skb->data)->interface);
505 WARN_ON(!wvif);
506 if (!wvif)
507 return;
508
509
510 _trace_tx_stats(arg, skb, wfx_pending_get_pkt_us_delay(wdev, skb));
511 wfx_tx_fill_rates(wdev, tx_info, arg);
512 skb_trim(skb, skb->len - tx_priv->icv_size);
513
514
515
516
517 memset(tx_info->rate_driver_data, 0, sizeof(tx_info->rate_driver_data));
518 memset(tx_info->pad, 0, sizeof(tx_info->pad));
519
520 if (!arg->status) {
521 tx_info->status.tx_time =
522 le32_to_cpu(arg->media_delay) -
523 le32_to_cpu(arg->tx_queue_delay);
524 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
525 tx_info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
526 else
527 tx_info->flags |= IEEE80211_TX_STAT_ACK;
528 } else if (arg->status == HIF_STATUS_TX_FAIL_REQUEUE) {
529 WARN(!arg->requeue, "incoherent status and result_flags");
530 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
531 wvif->after_dtim_tx_allowed = false;
532 schedule_work(&wvif->update_tim_work);
533 }
534 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
535 }
536 wfx_skb_dtor(wvif, skb);
537}
538
539static void wfx_flush_vif(struct wfx_vif *wvif, u32 queues,
540 struct sk_buff_head *dropped)
541{
542 struct wfx_queue *queue;
543 int i;
544
545 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
546 if (!(BIT(i) & queues))
547 continue;
548 queue = &wvif->tx_queue[i];
549 if (dropped)
550 wfx_tx_queue_drop(wvif, queue, dropped);
551 }
552 if (wvif->wdev->chip_frozen)
553 return;
554 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
555 if (!(BIT(i) & queues))
556 continue;
557 queue = &wvif->tx_queue[i];
558 if (wait_event_timeout(wvif->wdev->tx_dequeue,
559 wfx_tx_queue_empty(wvif, queue),
560 msecs_to_jiffies(1000)) <= 0)
561 dev_warn(wvif->wdev->dev,
562 "frames queued while flushing tx queues?");
563 }
564}
565
566void wfx_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
567 u32 queues, bool drop)
568{
569 struct wfx_dev *wdev = hw->priv;
570 struct sk_buff_head dropped;
571 struct wfx_vif *wvif;
572 struct hif_msg *hif;
573 struct sk_buff *skb;
574
575 skb_queue_head_init(&dropped);
576 if (vif) {
577 wvif = (struct wfx_vif *)vif->drv_priv;
578 wfx_flush_vif(wvif, queues, drop ? &dropped : NULL);
579 } else {
580 wvif = NULL;
581 while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
582 wfx_flush_vif(wvif, queues, drop ? &dropped : NULL);
583 }
584 wfx_tx_flush(wdev);
585 if (wdev->chip_frozen)
586 wfx_pending_drop(wdev, &dropped);
587 while ((skb = skb_dequeue(&dropped)) != NULL) {
588 hif = (struct hif_msg *)skb->data;
589 wvif = wdev_to_wvif(wdev, hif->interface);
590 ieee80211_tx_info_clear_status(IEEE80211_SKB_CB(skb));
591 wfx_skb_dtor(wvif, skb);
592 }
593}
594