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
37 band = wdev->hw->wiphy->bands[NL80211_BAND_2GHZ];
38 if (rate->idx >= band->n_bitrates) {
39 WARN(1, "wrong rate->idx value: %d", rate->idx);
40 return -1;
41 }
42 return band->bitrates[rate->idx].hw_value;
43}
44
45
46
47static void wfx_tx_policy_build(struct wfx_vif *wvif, struct tx_policy *policy,
48 struct ieee80211_tx_rate *rates)
49{
50 struct wfx_dev *wdev = wvif->wdev;
51 int i, rateid;
52 u8 count;
53
54 WARN(rates[0].idx < 0, "invalid rate policy");
55 memset(policy, 0, sizeof(*policy));
56 for (i = 0; i < IEEE80211_TX_MAX_RATES; ++i) {
57 if (rates[i].idx < 0)
58 break;
59 WARN_ON(rates[i].count > 15);
60 rateid = wfx_get_hw_rate(wdev, &rates[i]);
61
62 count = rates[i].count;
63 if (rateid % 2)
64 count <<= 4;
65 policy->rates[rateid / 2] |= count;
66 }
67}
68
69static bool tx_policy_is_equal(const struct tx_policy *a,
70 const struct tx_policy *b)
71{
72 return !memcmp(a->rates, b->rates, sizeof(a->rates));
73}
74
75static int wfx_tx_policy_find(struct tx_policy_cache *cache,
76 struct tx_policy *wanted)
77{
78 struct tx_policy *it;
79
80 list_for_each_entry(it, &cache->used, link)
81 if (tx_policy_is_equal(wanted, it))
82 return it - cache->cache;
83 list_for_each_entry(it, &cache->free, link)
84 if (tx_policy_is_equal(wanted, it))
85 return it - cache->cache;
86 return -1;
87}
88
89static void wfx_tx_policy_use(struct tx_policy_cache *cache,
90 struct tx_policy *entry)
91{
92 ++entry->usage_count;
93 list_move(&entry->link, &cache->used);
94}
95
96static int wfx_tx_policy_release(struct tx_policy_cache *cache,
97 struct tx_policy *entry)
98{
99 int ret = --entry->usage_count;
100
101 if (!ret)
102 list_move(&entry->link, &cache->free);
103 return ret;
104}
105
106static int wfx_tx_policy_get(struct wfx_vif *wvif,
107 struct ieee80211_tx_rate *rates, bool *renew)
108{
109 int idx;
110 struct tx_policy_cache *cache = &wvif->tx_policy_cache;
111 struct tx_policy wanted;
112 struct tx_policy *entry;
113
114 wfx_tx_policy_build(wvif, &wanted, rates);
115
116 spin_lock_bh(&cache->lock);
117 if (list_empty(&cache->free)) {
118 WARN(1, "unable to get a valid Tx policy");
119 spin_unlock_bh(&cache->lock);
120 return HIF_TX_RETRY_POLICY_INVALID;
121 }
122 idx = wfx_tx_policy_find(cache, &wanted);
123 if (idx >= 0) {
124 *renew = false;
125 } else {
126
127
128
129 *renew = true;
130 entry = list_entry(cache->free.prev, struct tx_policy, link);
131 memcpy(entry->rates, wanted.rates, sizeof(entry->rates));
132 entry->uploaded = false;
133 entry->usage_count = 0;
134 idx = entry - cache->cache;
135 }
136 wfx_tx_policy_use(cache, &cache->cache[idx]);
137 if (list_empty(&cache->free))
138 ieee80211_stop_queues(wvif->wdev->hw);
139 spin_unlock_bh(&cache->lock);
140 return idx;
141}
142
143static void wfx_tx_policy_put(struct wfx_vif *wvif, int idx)
144{
145 int usage, locked;
146 struct tx_policy_cache *cache = &wvif->tx_policy_cache;
147
148 if (idx == HIF_TX_RETRY_POLICY_INVALID)
149 return;
150 spin_lock_bh(&cache->lock);
151 locked = list_empty(&cache->free);
152 usage = wfx_tx_policy_release(cache, &cache->cache[idx]);
153 if (locked && !usage)
154 ieee80211_wake_queues(wvif->wdev->hw);
155 spin_unlock_bh(&cache->lock);
156}
157
158static int wfx_tx_policy_upload(struct wfx_vif *wvif)
159{
160 struct tx_policy *policies = wvif->tx_policy_cache.cache;
161 u8 tmp_rates[12];
162 int i, is_used;
163
164 do {
165 spin_lock_bh(&wvif->tx_policy_cache.lock);
166 for (i = 0; i < ARRAY_SIZE(wvif->tx_policy_cache.cache); ++i) {
167 is_used = memzcmp(policies[i].rates,
168 sizeof(policies[i].rates));
169 if (!policies[i].uploaded && is_used)
170 break;
171 }
172 if (i < ARRAY_SIZE(wvif->tx_policy_cache.cache)) {
173 policies[i].uploaded = true;
174 memcpy(tmp_rates, policies[i].rates, sizeof(tmp_rates));
175 spin_unlock_bh(&wvif->tx_policy_cache.lock);
176 hif_set_tx_rate_retry_policy(wvif, i, tmp_rates);
177 } else {
178 spin_unlock_bh(&wvif->tx_policy_cache.lock);
179 }
180 } while (i < ARRAY_SIZE(wvif->tx_policy_cache.cache));
181 return 0;
182}
183
184void wfx_tx_policy_upload_work(struct work_struct *work)
185{
186 struct wfx_vif *wvif =
187 container_of(work, struct wfx_vif, tx_policy_upload_work);
188
189 wfx_tx_policy_upload(wvif);
190 wfx_tx_unlock(wvif->wdev);
191}
192
193void wfx_tx_policy_init(struct wfx_vif *wvif)
194{
195 struct tx_policy_cache *cache = &wvif->tx_policy_cache;
196 int i;
197
198 memset(cache, 0, sizeof(*cache));
199
200 spin_lock_init(&cache->lock);
201 INIT_LIST_HEAD(&cache->used);
202 INIT_LIST_HEAD(&cache->free);
203
204 for (i = 0; i < ARRAY_SIZE(cache->cache); ++i)
205 list_add(&cache->cache[i].link, &cache->free);
206}
207
208
209
210static bool ieee80211_is_action_back(struct ieee80211_hdr *hdr)
211{
212 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)hdr;
213
214 if (!ieee80211_is_action(mgmt->frame_control))
215 return false;
216 if (mgmt->u.action.category != WLAN_CATEGORY_BACK)
217 return false;
218 return true;
219}
220
221static u8 wfx_tx_get_link_id(struct wfx_vif *wvif, struct ieee80211_sta *sta,
222 struct ieee80211_hdr *hdr)
223{
224 struct wfx_sta_priv *sta_priv =
225 sta ? (struct wfx_sta_priv *)&sta->drv_priv : NULL;
226 const u8 *da = ieee80211_get_DA(hdr);
227
228 if (sta_priv && sta_priv->link_id)
229 return sta_priv->link_id;
230 if (wvif->vif->type != NL80211_IFTYPE_AP)
231 return 0;
232 if (is_multicast_ether_addr(da))
233 return 0;
234 return HIF_LINK_ID_NOT_ASSOCIATED;
235}
236
237static void wfx_tx_fixup_rates(struct ieee80211_tx_rate *rates)
238{
239 int i;
240 bool finished;
241
242
243 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
244 if (rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
245 rates[i].flags |= IEEE80211_TX_RC_SHORT_GI;
246 if (!(rates[0].flags & IEEE80211_TX_RC_SHORT_GI))
247 rates[i].flags &= ~IEEE80211_TX_RC_SHORT_GI;
248 if (!(rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS))
249 rates[i].flags &= ~IEEE80211_TX_RC_USE_RTS_CTS;
250 }
251
252
253 do {
254 finished = true;
255 for (i = 0; i < IEEE80211_TX_MAX_RATES - 1; i++) {
256 if (rates[i + 1].idx == rates[i].idx &&
257 rates[i].idx != -1) {
258 rates[i].count += rates[i + 1].count;
259 if (rates[i].count > 15)
260 rates[i].count = 15;
261 rates[i + 1].idx = -1;
262 rates[i + 1].count = 0;
263
264 finished = false;
265 }
266 if (rates[i + 1].idx > rates[i].idx) {
267 swap(rates[i + 1], rates[i]);
268 finished = false;
269 }
270 }
271 } while (!finished);
272
273 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
274 if (rates[i].idx == 0)
275 break;
276 if (rates[i].idx == -1) {
277 rates[i].idx = 0;
278 rates[i].count = 8;
279 rates[i].flags = rates[i - 1].flags &
280 IEEE80211_TX_RC_MCS;
281 break;
282 }
283 }
284
285 for (i = 1; i < IEEE80211_TX_MAX_RATES; i++)
286 rates[i].flags &= ~IEEE80211_TX_RC_SHORT_GI;
287}
288
289static u8 wfx_tx_get_retry_policy_id(struct wfx_vif *wvif,
290 struct ieee80211_tx_info *tx_info)
291{
292 bool tx_policy_renew = false;
293 u8 ret;
294
295 ret = wfx_tx_policy_get(wvif, tx_info->driver_rates, &tx_policy_renew);
296 if (ret == 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 ret;
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
376 req->packet_id = atomic_add_return(1, &wvif->wdev->packet_id) & 0xFFFF;
377 req->packet_id |= IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl)) << 16;
378 req->packet_id |= queue_id << 28;
379
380 req->fc_offset = offset;
381
382 req->queue_id = 3 - queue_id;
383 req->peer_sta_id = wfx_tx_get_link_id(wvif, sta, hdr);
384 req->retry_policy_index = wfx_tx_get_retry_policy_id(wvif, tx_info);
385 req->frame_format = wfx_tx_get_frame_format(tx_info);
386 if (tx_info->driver_rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
387 req->short_gi = 1;
388 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
389 req->after_dtim = 1;
390
391
392 wfx_tx_queues_put(wvif, skb);
393 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
394 schedule_work(&wvif->update_tim_work);
395 wfx_bh_request_tx(wvif->wdev);
396 return 0;
397}
398
399void wfx_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
400 struct sk_buff *skb)
401{
402 struct wfx_dev *wdev = hw->priv;
403 struct wfx_vif *wvif;
404 struct ieee80211_sta *sta = control ? control->sta : NULL;
405 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
406 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
407 size_t driver_data_room = sizeof_field(struct ieee80211_tx_info,
408 rate_driver_data);
409
410 compiletime_assert(sizeof(struct wfx_tx_priv) <= driver_data_room,
411 "struct tx_priv is too large");
412 WARN(skb->next || skb->prev, "skb is already member of a list");
413
414 if (tx_info->control.vif)
415 wvif = (struct wfx_vif *)tx_info->control.vif->drv_priv;
416 else
417 wvif = wvif_iterate(wdev, NULL);
418 if (WARN_ON(!wvif))
419 goto drop;
420
421
422
423 if (ieee80211_is_action_back(hdr)) {
424 dev_info(wdev->dev, "drop BA action\n");
425 goto drop;
426 }
427 if (wfx_tx_inner(wvif, sta, skb))
428 goto drop;
429
430 return;
431
432drop:
433 ieee80211_tx_status_irqsafe(wdev->hw, skb);
434}
435
436static void wfx_skb_dtor(struct wfx_vif *wvif, struct sk_buff *skb)
437{
438 struct hif_msg *hif = (struct hif_msg *)skb->data;
439 struct hif_req_tx *req = (struct hif_req_tx *)hif->body;
440 unsigned int offset = sizeof(struct hif_msg) +
441 sizeof(struct hif_req_tx) +
442 req->fc_offset;
443
444 if (!wvif) {
445 pr_warn("%s: vif associated with the skb does not exist anymore\n", __func__);
446 return;
447 }
448 wfx_tx_policy_put(wvif, req->retry_policy_index);
449 skb_pull(skb, offset);
450 ieee80211_tx_status_irqsafe(wvif->wdev->hw, skb);
451}
452
453static void wfx_tx_fill_rates(struct wfx_dev *wdev,
454 struct ieee80211_tx_info *tx_info,
455 const struct hif_cnf_tx *arg)
456{
457 struct ieee80211_tx_rate *rate;
458 int tx_count;
459 int i;
460
461 tx_count = arg->ack_failures;
462 if (!arg->status || arg->ack_failures)
463 tx_count += 1;
464 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
465 rate = &tx_info->status.rates[i];
466 if (rate->idx < 0)
467 break;
468 if (tx_count < rate->count &&
469 arg->status == HIF_STATUS_TX_FAIL_RETRIES &&
470 arg->ack_failures)
471 dev_dbg(wdev->dev, "all retries were not consumed: %d != %d\n",
472 rate->count, tx_count);
473 if (tx_count <= rate->count && tx_count &&
474 arg->txed_rate != wfx_get_hw_rate(wdev, rate))
475 dev_dbg(wdev->dev, "inconsistent tx_info rates: %d != %d\n",
476 arg->txed_rate, wfx_get_hw_rate(wdev, rate));
477 if (tx_count > rate->count) {
478 tx_count -= rate->count;
479 } else if (!tx_count) {
480 rate->count = 0;
481 rate->idx = -1;
482 } else {
483 rate->count = tx_count;
484 tx_count = 0;
485 }
486 }
487 if (tx_count)
488 dev_dbg(wdev->dev, "%d more retries than expected\n", tx_count);
489}
490
491void wfx_tx_confirm_cb(struct wfx_dev *wdev, const struct hif_cnf_tx *arg)
492{
493 const struct wfx_tx_priv *tx_priv;
494 struct ieee80211_tx_info *tx_info;
495 struct wfx_vif *wvif;
496 struct sk_buff *skb;
497
498 skb = wfx_pending_get(wdev, arg->packet_id);
499 if (!skb) {
500 dev_warn(wdev->dev, "received unknown packet_id (%#.8x) from chip\n",
501 arg->packet_id);
502 return;
503 }
504 tx_info = IEEE80211_SKB_CB(skb);
505 tx_priv = wfx_skb_tx_priv(skb);
506 wvif = wdev_to_wvif(wdev, ((struct hif_msg *)skb->data)->interface);
507 WARN_ON(!wvif);
508 if (!wvif)
509 return;
510
511
512 _trace_tx_stats(arg, skb, wfx_pending_get_pkt_us_delay(wdev, skb));
513 wfx_tx_fill_rates(wdev, tx_info, arg);
514 skb_trim(skb, skb->len - tx_priv->icv_size);
515
516
517
518
519
520 memset(tx_info->rate_driver_data, 0, sizeof(tx_info->rate_driver_data));
521 memset(tx_info->pad, 0, sizeof(tx_info->pad));
522
523 if (!arg->status) {
524 tx_info->status.tx_time =
525 le32_to_cpu(arg->media_delay) -
526 le32_to_cpu(arg->tx_queue_delay);
527 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
528 tx_info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
529 else
530 tx_info->flags |= IEEE80211_TX_STAT_ACK;
531 } else if (arg->status == HIF_STATUS_TX_FAIL_REQUEUE) {
532 WARN(!arg->requeue, "incoherent status and result_flags");
533 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
534 wvif->after_dtim_tx_allowed = false;
535 schedule_work(&wvif->update_tim_work);
536 }
537 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
538 }
539 wfx_skb_dtor(wvif, skb);
540}
541
542static void wfx_flush_vif(struct wfx_vif *wvif, u32 queues,
543 struct sk_buff_head *dropped)
544{
545 struct wfx_queue *queue;
546 int i;
547
548 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
549 if (!(BIT(i) & queues))
550 continue;
551 queue = &wvif->tx_queue[i];
552 if (dropped)
553 wfx_tx_queue_drop(wvif, queue, dropped);
554 }
555 if (wvif->wdev->chip_frozen)
556 return;
557 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
558 if (!(BIT(i) & queues))
559 continue;
560 queue = &wvif->tx_queue[i];
561 if (wait_event_timeout(wvif->wdev->tx_dequeue,
562 wfx_tx_queue_empty(wvif, queue),
563 msecs_to_jiffies(1000)) <= 0)
564 dev_warn(wvif->wdev->dev,
565 "frames queued while flushing tx queues?");
566 }
567}
568
569void wfx_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
570 u32 queues, bool drop)
571{
572 struct wfx_dev *wdev = hw->priv;
573 struct sk_buff_head dropped;
574 struct wfx_vif *wvif;
575 struct hif_msg *hif;
576 struct sk_buff *skb;
577
578 skb_queue_head_init(&dropped);
579 if (vif) {
580 wvif = (struct wfx_vif *)vif->drv_priv;
581 wfx_flush_vif(wvif, queues, drop ? &dropped : NULL);
582 } else {
583 wvif = NULL;
584 while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
585 wfx_flush_vif(wvif, queues, drop ? &dropped : NULL);
586 }
587 wfx_tx_flush(wdev);
588 if (wdev->chip_frozen)
589 wfx_pending_drop(wdev, &dropped);
590 while ((skb = skb_dequeue(&dropped)) != NULL) {
591 hif = (struct hif_msg *)skb->data;
592 wvif = wdev_to_wvif(wdev, hif->interface);
593 ieee80211_tx_info_clear_status(IEEE80211_SKB_CB(skb));
594 wfx_skb_dtor(wvif, skb);
595 }
596}
597