1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * This file is part of wl18xx
4 *
5 * Copyright (C) 2011 Texas Instruments Inc.
6 */
7
8#include "../wlcore/wlcore.h"
9#include "../wlcore/cmd.h"
10#include "../wlcore/debug.h"
11#include "../wlcore/acx.h"
12#include "../wlcore/tx.h"
13
14#include "wl18xx.h"
15#include "tx.h"
16
17static
18void wl18xx_get_last_tx_rate(struct wl1271 *wl, struct ieee80211_vif *vif,
19			     u8 band, struct ieee80211_tx_rate *rate, u8 hlid)
20{
21	u8 fw_rate = wl->links[hlid].fw_rate_idx;
22
23	if (fw_rate > CONF_HW_RATE_INDEX_MAX) {
24		wl1271_error("last Tx rate invalid: %d", fw_rate);
25		rate->idx = 0;
26		rate->flags = 0;
27		return;
28	}
29
30	if (fw_rate <= CONF_HW_RATE_INDEX_54MBPS) {
31		rate->idx = fw_rate;
32		if (band == NL80211_BAND_5GHZ)
33			rate->idx -= CONF_HW_RATE_INDEX_6MBPS;
34		rate->flags = 0;
35	} else {
36		rate->flags = IEEE80211_TX_RC_MCS;
37		rate->idx = fw_rate - CONF_HW_RATE_INDEX_MCS0;
38
39		/* SGI modifier is counted as a separate rate */
40		if (fw_rate >= CONF_HW_RATE_INDEX_MCS7_SGI)
41			(rate->idx)--;
42		if (fw_rate == CONF_HW_RATE_INDEX_MCS15_SGI)
43			(rate->idx)--;
44
45		/* this also covers the 40Mhz SGI case (= MCS15) */
46		if (fw_rate == CONF_HW_RATE_INDEX_MCS7_SGI ||
47		    fw_rate == CONF_HW_RATE_INDEX_MCS15_SGI)
48			rate->flags |= IEEE80211_TX_RC_SHORT_GI;
49
50		if (fw_rate > CONF_HW_RATE_INDEX_MCS7_SGI && vif) {
51			struct wl12xx_vif *wlvif = wl12xx_vif_to_data(vif);
52			if (wlvif->channel_type == NL80211_CHAN_HT40MINUS ||
53			    wlvif->channel_type == NL80211_CHAN_HT40PLUS) {
54				/* adjustment needed for range 0-7 */
55				rate->idx -= 8;
56				rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
57			}
58		}
59	}
60}
61
62static void wl18xx_tx_complete_packet(struct wl1271 *wl, u8 tx_stat_byte)
63{
64	struct ieee80211_tx_info *info;
65	struct sk_buff *skb;
66	int id = tx_stat_byte & WL18XX_TX_STATUS_DESC_ID_MASK;
67	bool tx_success;
68	struct wl1271_tx_hw_descr *tx_desc;
69
70	/* check for id legality */
71	if (unlikely(id >= wl->num_tx_desc || wl->tx_frames[id] == NULL)) {
72		wl1271_warning("illegal id in tx completion: %d", id);
73		return;
74	}
75
76	/* a zero bit indicates Tx success */
77	tx_success = !(tx_stat_byte & BIT(WL18XX_TX_STATUS_STAT_BIT_IDX));
78
79	skb = wl->tx_frames[id];
80	info = IEEE80211_SKB_CB(skb);
81	tx_desc = (struct wl1271_tx_hw_descr *)skb->data;
82
83	if (wl12xx_is_dummy_packet(wl, skb)) {
84		wl1271_free_tx_id(wl, id);
85		return;
86	}
87
88	/* update the TX status info */
89	if (tx_success && !(info->flags & IEEE80211_TX_CTL_NO_ACK))
90		info->flags |= IEEE80211_TX_STAT_ACK;
91	/*
92	 * first pass info->control.vif while it's valid, and then fill out
93	 * the info->status structures
94	 */
95	wl18xx_get_last_tx_rate(wl, info->control.vif,
96				info->band,
97				&info->status.rates[0],
98				tx_desc->hlid);
99
100	info->status.rates[0].count = 1; /* no data about retries */
101	info->status.ack_signal = -1;
102
103	if (!tx_success)
104		wl->stats.retry_count++;
105
106	/*
107	 * TODO: update sequence number for encryption? seems to be
108	 * unsupported for now. needed for recovery with encryption.
109	 */
110
111	/* remove private header from packet */
112	skb_pull(skb, sizeof(struct wl1271_tx_hw_descr));
113
114	/* remove TKIP header space if present */
115	if ((wl->quirks & WLCORE_QUIRK_TKIP_HEADER_SPACE) &&
116	    info->control.hw_key &&
117	    info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
118		int hdrlen = ieee80211_get_hdrlen_from_skb(skb);
119		memmove(skb->data + WL1271_EXTRA_SPACE_TKIP, skb->data, hdrlen);
120		skb_pull(skb, WL1271_EXTRA_SPACE_TKIP);
121	}
122
123	wl1271_debug(DEBUG_TX, "tx status id %u skb 0x%p success %d",
124		     id, skb, tx_success);
125
126	/* return the packet to the stack */
127	skb_queue_tail(&wl->deferred_tx_queue, skb);
128	queue_work(wl->freezable_wq, &wl->netstack_work);
129	wl1271_free_tx_id(wl, id);
130}
131
132void wl18xx_tx_immediate_complete(struct wl1271 *wl)
133{
134	struct wl18xx_fw_status_priv *status_priv =
135		(struct wl18xx_fw_status_priv *)wl->fw_status->priv;
136	struct wl18xx_priv *priv = wl->priv;
137	u8 i, hlid;
138
139	/* nothing to do here */
140	if (priv->last_fw_rls_idx == status_priv->fw_release_idx)
141		return;
142
143	/* update rates per link */
144	hlid = wl->fw_status->counters.hlid;
145
146	if (hlid < WLCORE_MAX_LINKS) {
147		wl->links[hlid].fw_rate_idx =
148				wl->fw_status->counters.tx_last_rate;
149		wl->links[hlid].fw_rate_mbps =
150				wl->fw_status->counters.tx_last_rate_mbps;
151	}
152
153	/* freed Tx descriptors */
154	wl1271_debug(DEBUG_TX, "last released desc = %d, current idx = %d",
155		     priv->last_fw_rls_idx, status_priv->fw_release_idx);
156
157	if (status_priv->fw_release_idx >= WL18XX_FW_MAX_TX_STATUS_DESC) {
158		wl1271_error("invalid desc release index %d",
159			     status_priv->fw_release_idx);
160		WARN_ON(1);
161		return;
162	}
163
164	for (i = priv->last_fw_rls_idx;
165	     i != status_priv->fw_release_idx;
166	     i = (i + 1) % WL18XX_FW_MAX_TX_STATUS_DESC) {
167		wl18xx_tx_complete_packet(wl,
168			status_priv->released_tx_desc[i]);
169
170		wl->tx_results_count++;
171	}
172
173	priv->last_fw_rls_idx = status_priv->fw_release_idx;
174}
175