1 // SPDX-License-Identifier: GPL-2.0-only
3 * Datapath implementation.
5 * Copyright (c) 2017-2020, Silicon Laboratories, Inc.
6 * Copyright (c) 2010, ST-Ericsson
8 #include <net/mac80211.h>
9 #include <linux/etherdevice.h>
18 #include "hif_tx_mib.h"
20 static int wfx_get_hw_rate(struct wfx_dev *wdev,
21 const struct ieee80211_tx_rate *rate)
23 struct ieee80211_supported_band *band;
27 if (rate->flags & IEEE80211_TX_RC_MCS) {
29 WARN(1, "wrong rate->idx value: %d", rate->idx);
32 return rate->idx + 14;
34 // WFx only support 2GHz, else band information should be retrieved
35 // from ieee80211_tx_info
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);
41 return band->bitrates[rate->idx].hw_value;
44 /* TX policy cache implementation */
46 static void wfx_tx_policy_build(struct wfx_vif *wvif, struct tx_policy *policy,
47 struct ieee80211_tx_rate *rates)
49 struct wfx_dev *wdev = wvif->wdev;
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) {
58 WARN_ON(rates[i].count > 15);
59 rateid = wfx_get_hw_rate(wdev, &rates[i]);
60 // Pack two values in each byte of policy->rates
61 count = rates[i].count;
64 policy->rates[rateid / 2] |= count;
68 static bool tx_policy_is_equal(const struct tx_policy *a,
69 const struct tx_policy *b)
71 return !memcmp(a->rates, b->rates, sizeof(a->rates));
74 static int wfx_tx_policy_find(struct tx_policy_cache *cache,
75 struct tx_policy *wanted)
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;
88 static void wfx_tx_policy_use(struct tx_policy_cache *cache,
89 struct tx_policy *entry)
92 list_move(&entry->link, &cache->used);
95 static int wfx_tx_policy_release(struct tx_policy_cache *cache,
96 struct tx_policy *entry)
98 int ret = --entry->usage_count;
101 list_move(&entry->link, &cache->free);
105 static int wfx_tx_policy_get(struct wfx_vif *wvif,
106 struct ieee80211_tx_rate *rates, bool *renew)
109 struct tx_policy_cache *cache = &wvif->tx_policy_cache;
110 struct tx_policy wanted;
112 wfx_tx_policy_build(wvif, &wanted, rates);
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;
120 idx = wfx_tx_policy_find(cache, &wanted);
124 struct tx_policy *entry;
126 /* If policy is not found create a new one
127 * using the oldest entry in "free" list
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;
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);
142 static void wfx_tx_policy_put(struct wfx_vif *wvif, int idx)
145 struct tx_policy_cache *cache = &wvif->tx_policy_cache;
147 if (idx == HIF_TX_RETRY_POLICY_INVALID)
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);
157 static int wfx_tx_policy_upload(struct wfx_vif *wvif)
159 struct tx_policy *policies = wvif->tx_policy_cache.cache;
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)
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);
177 spin_unlock_bh(&wvif->tx_policy_cache.lock);
179 } while (i < ARRAY_SIZE(wvif->tx_policy_cache.cache));
183 void wfx_tx_policy_upload_work(struct work_struct *work)
185 struct wfx_vif *wvif =
186 container_of(work, struct wfx_vif, tx_policy_upload_work);
188 wfx_tx_policy_upload(wvif);
189 wfx_tx_unlock(wvif->wdev);
192 void wfx_tx_policy_init(struct wfx_vif *wvif)
194 struct tx_policy_cache *cache = &wvif->tx_policy_cache;
197 memset(cache, 0, sizeof(*cache));
199 spin_lock_init(&cache->lock);
200 INIT_LIST_HEAD(&cache->used);
201 INIT_LIST_HEAD(&cache->free);
203 for (i = 0; i < ARRAY_SIZE(cache->cache); ++i)
204 list_add(&cache->cache[i].link, &cache->free);
207 /* Tx implementation */
209 static bool ieee80211_is_action_back(struct ieee80211_hdr *hdr)
211 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)hdr;
213 if (!ieee80211_is_action(mgmt->frame_control))
215 if (mgmt->u.action.category != WLAN_CATEGORY_BACK)
220 static u8 wfx_tx_get_link_id(struct wfx_vif *wvif, struct ieee80211_sta *sta,
221 struct ieee80211_hdr *hdr)
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);
227 if (sta_priv && sta_priv->link_id)
228 return sta_priv->link_id;
229 if (wvif->vif->type != NL80211_IFTYPE_AP)
231 if (is_multicast_ether_addr(da))
233 return HIF_LINK_ID_NOT_ASSOCIATED;
236 static void wfx_tx_fixup_rates(struct ieee80211_tx_rate *rates)
241 // Firmware is not able to mix rates with different flags
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;
251 // Sort rates and remove duplicates
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)
260 rates[i + 1].idx = -1;
261 rates[i + 1].count = 0;
265 if (rates[i + 1].idx > rates[i].idx) {
266 swap(rates[i + 1], rates[i]);
271 // Ensure that MCS0 or 1Mbps is present at the end of the retry list
272 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
273 if (rates[i].idx == 0)
275 if (rates[i].idx == -1) {
277 rates[i].count = 8; // == hw->max_rate_tries
278 rates[i].flags = rates[i - 1].flags &
283 // All retries use long GI
284 for (i = 1; i < IEEE80211_TX_MAX_RATES; i++)
285 rates[i].flags &= ~IEEE80211_TX_RC_SHORT_GI;
288 static u8 wfx_tx_get_rate_id(struct wfx_vif *wvif,
289 struct ieee80211_tx_info *tx_info)
291 bool tx_policy_renew = false;
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");
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);
307 static int wfx_tx_get_frame_format(struct ieee80211_tx_info *tx_info)
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;
314 return HIF_FRAME_FORMAT_GF_HT_11N;
317 static int wfx_tx_get_icv_len(struct ieee80211_key_conf *hw_key)
323 if (hw_key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
325 mic_space = (hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) ? 8 : 0;
326 return hw_key->icv_len + mic_space;
329 static int wfx_tx_inner(struct wfx_vif *wvif, struct ieee80211_sta *sta,
332 struct hif_msg *hif_msg;
333 struct hif_req_tx *req;
334 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
335 struct ieee80211_key_conf *hw_key = tx_info->control.hw_key;
336 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
337 int queue_id = skb_get_queue_mapping(skb);
338 size_t offset = (size_t)skb->data & 3;
339 int wmsg_len = sizeof(struct hif_msg) +
340 sizeof(struct hif_req_tx) + offset;
342 WARN(queue_id >= IEEE80211_NUM_ACS, "unsupported queue_id");
343 wfx_tx_fixup_rates(tx_info->driver_rates);
345 // From now tx_info->control is unusable
346 memset(tx_info->rate_driver_data, 0, sizeof(struct wfx_tx_priv));
349 WARN(skb_headroom(skb) < wmsg_len, "not enough space in skb");
350 WARN(offset & 1, "attempt to transmit an unaligned frame");
351 skb_put(skb, wfx_tx_get_icv_len(hw_key));
352 skb_push(skb, wmsg_len);
353 memset(skb->data, 0, wmsg_len);
354 hif_msg = (struct hif_msg *)skb->data;
355 hif_msg->len = cpu_to_le16(skb->len);
356 hif_msg->id = HIF_REQ_ID_TX;
357 hif_msg->interface = wvif->id;
358 if (skb->len > wvif->wdev->hw_caps.size_inp_ch_buf) {
359 dev_warn(wvif->wdev->dev,
360 "requested frame size (%d) is larger than maximum supported (%d)\n",
361 skb->len, wvif->wdev->hw_caps.size_inp_ch_buf);
362 skb_pull(skb, wmsg_len);
367 req = (struct hif_req_tx *)hif_msg->body;
368 // packet_id just need to be unique on device. 32bits are more than
369 // necessary for that task, so we tae advantage of it to add some extra
371 req->packet_id = atomic_add_return(1, &wvif->wdev->packet_id) & 0xFFFF;
372 req->packet_id |= IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl)) << 16;
373 req->packet_id |= queue_id << 28;
375 req->fc_offset = offset;
376 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
378 req->peer_sta_id = wfx_tx_get_link_id(wvif, sta, hdr);
379 // Queue index are inverted between firmware and Linux
380 req->queue_id = 3 - queue_id;
381 req->retry_policy_index = wfx_tx_get_rate_id(wvif, tx_info);
382 req->frame_format = wfx_tx_get_frame_format(tx_info);
383 if (tx_info->driver_rates[0].flags & IEEE80211_TX_RC_SHORT_GI)
386 // Auxiliary operations
387 wfx_tx_queues_put(wvif, skb);
388 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
389 schedule_work(&wvif->update_tim_work);
390 wfx_bh_request_tx(wvif->wdev);
394 void wfx_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *control,
397 struct wfx_dev *wdev = hw->priv;
398 struct wfx_vif *wvif;
399 struct ieee80211_sta *sta = control ? control->sta : NULL;
400 struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
401 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
402 size_t driver_data_room = sizeof_field(struct ieee80211_tx_info,
405 compiletime_assert(sizeof(struct wfx_tx_priv) <= driver_data_room,
406 "struct tx_priv is too large");
407 WARN(skb->next || skb->prev, "skb is already member of a list");
408 // control.vif can be NULL for injected frames
409 if (tx_info->control.vif)
410 wvif = (struct wfx_vif *)tx_info->control.vif->drv_priv;
412 wvif = wvif_iterate(wdev, NULL);
415 // Because of TX_AMPDU_SETUP_IN_HW, mac80211 does not try to send any
416 // BlockAck session management frame. The check below exist just in case.
417 if (ieee80211_is_action_back(hdr)) {
418 dev_info(wdev->dev, "drop BA action\n");
421 if (wfx_tx_inner(wvif, sta, skb))
427 ieee80211_tx_status_irqsafe(wdev->hw, skb);
430 static void wfx_skb_dtor(struct wfx_vif *wvif, struct sk_buff *skb)
432 struct hif_msg *hif = (struct hif_msg *)skb->data;
433 struct hif_req_tx *req = (struct hif_req_tx *)hif->body;
434 unsigned int offset = sizeof(struct hif_msg) +
435 sizeof(struct hif_req_tx) +
439 pr_warn("%s: vif associated with the skb does not exist anymore\n", __func__);
442 wfx_tx_policy_put(wvif, req->retry_policy_index);
443 skb_pull(skb, offset);
444 ieee80211_tx_status_irqsafe(wvif->wdev->hw, skb);
447 static void wfx_tx_fill_rates(struct wfx_dev *wdev,
448 struct ieee80211_tx_info *tx_info,
449 const struct hif_cnf_tx *arg)
451 struct ieee80211_tx_rate *rate;
455 tx_count = arg->ack_failures;
456 if (!arg->status || arg->ack_failures)
457 tx_count += 1; // Also report success
458 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
459 rate = &tx_info->status.rates[i];
462 if (tx_count < rate->count &&
463 arg->status == HIF_STATUS_TX_FAIL_RETRIES &&
465 dev_dbg(wdev->dev, "all retries were not consumed: %d != %d\n",
466 rate->count, tx_count);
467 if (tx_count <= rate->count && tx_count &&
468 arg->txed_rate != wfx_get_hw_rate(wdev, rate))
469 dev_dbg(wdev->dev, "inconsistent tx_info rates: %d != %d\n",
470 arg->txed_rate, wfx_get_hw_rate(wdev, rate));
471 if (tx_count > rate->count) {
472 tx_count -= rate->count;
473 } else if (!tx_count) {
477 rate->count = tx_count;
482 dev_dbg(wdev->dev, "%d more retries than expected\n", tx_count);
485 void wfx_tx_confirm_cb(struct wfx_dev *wdev, const struct hif_cnf_tx *arg)
487 struct ieee80211_tx_info *tx_info;
488 struct wfx_vif *wvif;
491 skb = wfx_pending_get(wdev, arg->packet_id);
493 dev_warn(wdev->dev, "received unknown packet_id (%#.8x) from chip\n",
497 tx_info = IEEE80211_SKB_CB(skb);
498 wvif = wdev_to_wvif(wdev, ((struct hif_msg *)skb->data)->interface);
503 // Note that wfx_pending_get_pkt_us_delay() get data from tx_info
504 _trace_tx_stats(arg, skb, wfx_pending_get_pkt_us_delay(wdev, skb));
505 wfx_tx_fill_rates(wdev, tx_info, arg);
506 // From now, you can touch to tx_info->status, but do not touch to
508 // FIXME: use ieee80211_tx_info_clear_status()
509 memset(tx_info->rate_driver_data, 0, sizeof(tx_info->rate_driver_data));
510 memset(tx_info->pad, 0, sizeof(tx_info->pad));
513 tx_info->status.tx_time =
514 le32_to_cpu(arg->media_delay) -
515 le32_to_cpu(arg->tx_queue_delay);
516 if (tx_info->flags & IEEE80211_TX_CTL_NO_ACK)
517 tx_info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
519 tx_info->flags |= IEEE80211_TX_STAT_ACK;
520 } else if (arg->status == HIF_STATUS_TX_FAIL_REQUEUE) {
521 WARN(!arg->requeue, "incoherent status and result_flags");
522 if (tx_info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
523 wvif->after_dtim_tx_allowed = false; // DTIM period elapsed
524 schedule_work(&wvif->update_tim_work);
526 tx_info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
528 wfx_skb_dtor(wvif, skb);
531 static void wfx_flush_vif(struct wfx_vif *wvif, u32 queues,
532 struct sk_buff_head *dropped)
534 struct wfx_queue *queue;
537 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
538 if (!(BIT(i) & queues))
540 queue = &wvif->tx_queue[i];
542 wfx_tx_queue_drop(wvif, queue, dropped);
544 if (wvif->wdev->chip_frozen)
546 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
547 if (!(BIT(i) & queues))
549 queue = &wvif->tx_queue[i];
550 if (wait_event_timeout(wvif->wdev->tx_dequeue,
551 wfx_tx_queue_empty(wvif, queue),
552 msecs_to_jiffies(1000)) <= 0)
553 dev_warn(wvif->wdev->dev,
554 "frames queued while flushing tx queues?");
558 void wfx_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
559 u32 queues, bool drop)
561 struct wfx_dev *wdev = hw->priv;
562 struct sk_buff_head dropped;
563 struct wfx_vif *wvif;
567 skb_queue_head_init(&dropped);
569 wvif = (struct wfx_vif *)vif->drv_priv;
570 wfx_flush_vif(wvif, queues, drop ? &dropped : NULL);
573 while ((wvif = wvif_iterate(wdev, wvif)) != NULL)
574 wfx_flush_vif(wvif, queues, drop ? &dropped : NULL);
577 if (wdev->chip_frozen)
578 wfx_pending_drop(wdev, &dropped);
579 while ((skb = skb_dequeue(&dropped)) != NULL) {
580 hif = (struct hif_msg *)skb->data;
581 wvif = wdev_to_wvif(wdev, hif->interface);
582 ieee80211_tx_info_clear_status(IEEE80211_SKB_CB(skb));
583 wfx_skb_dtor(wvif, skb);