txrx.c 7.2 KB
Newer Older
1 2
/*
 * Copyright (c) 2005-2011 Atheros Communications Inc.
K
Kalle Valo 已提交
3
 * Copyright (c) 2011-2016 Qualcomm Atheros, Inc.
4
 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "core.h"
#include "txrx.h"
#include "htt.h"
#include "mac.h"
#include "debug.h"

static void ath10k_report_offchan_tx(struct ath10k *ar, struct sk_buff *skb)
{
M
Michal Kazior 已提交
27 28 29 30 31 32
	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);

	if (likely(!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)))
		return;

	if (ath10k_mac_tx_frm_has_freq(ar))
33 34 35 36 37
		return;

	/* If the original wait_for_completion() timed out before
	 * {data,mgmt}_tx_completed() was called then we could complete
	 * offchan_tx_completed for a different skb. Prevent this by using
38 39
	 * offchan_tx_skb.
	 */
40 41
	spin_lock_bh(&ar->data_lock);
	if (ar->offchan_tx_skb != skb) {
42
		ath10k_warn(ar, "completed old offchannel frame\n");
43 44 45 46 47 48
		goto out;
	}

	complete(&ar->offchan_tx_completed);
	ar->offchan_tx_skb = NULL; /* just for sanity */

49
	ath10k_dbg(ar, ATH10K_DBG_HTT, "completed offchannel skb %pK\n", skb);
50 51 52 53
out:
	spin_unlock_bh(&ar->data_lock);
}

54 55
int ath10k_txrx_tx_unref(struct ath10k_htt *htt,
			 const struct htt_tx_done *tx_done)
56
{
57 58
	struct ath10k *ar = htt->ar;
	struct device *dev = ar->dev;
59
	struct ieee80211_tx_info *info;
60
	struct ieee80211_txq *txq;
61
	struct ath10k_skb_cb *skb_cb;
62
	struct ath10k_txq *artxq;
63
	struct sk_buff *msdu;
64

65
	ath10k_dbg(ar, ATH10K_DBG_HTT,
66 67
		   "htt tx completion msdu_id %u status %d\n",
		   tx_done->msdu_id, tx_done->status);
68

69
	if (tx_done->msdu_id >= htt->max_num_pending_tx) {
70
		ath10k_warn(ar, "warning: msdu_id %d too big, ignoring\n",
71
			    tx_done->msdu_id);
72
		return -EINVAL;
73 74
	}

75
	spin_lock_bh(&htt->tx_lock);
M
Michal Kazior 已提交
76 77 78 79
	msdu = idr_find(&htt->pending_tx, tx_done->msdu_id);
	if (!msdu) {
		ath10k_warn(ar, "received tx completion for invalid msdu_id: %d\n",
			    tx_done->msdu_id);
80
		spin_unlock_bh(&htt->tx_lock);
81
		return -ENOENT;
M
Michal Kazior 已提交
82
	}
83

M
Michal Kazior 已提交
84
	skb_cb = ATH10K_SKB_CB(msdu);
85
	txq = skb_cb->txq;
86

87 88
	if (txq) {
		artxq = (void *)txq->drv_priv;
89
		artxq->num_fw_queued--;
90
	}
91

92
	ath10k_htt_tx_free_msdu_id(htt, tx_done->msdu_id);
93
	ath10k_htt_tx_dec_pending(htt);
94 95 96
	if (htt->num_pending_tx == 0)
		wake_up(&htt->empty_tx_wq);
	spin_unlock_bh(&htt->tx_lock);
M
Michal Kazior 已提交
97

98
	dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
99 100 101 102

	ath10k_report_offchan_tx(htt->ar, msdu);

	info = IEEE80211_SKB_CB(msdu);
M
Michal Kazior 已提交
103
	memset(&info->status, 0, sizeof(info->status));
104
	trace_ath10k_txrx_tx_unref(ar, tx_done->msdu_id);
105 106 107 108

	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
		info->flags |= IEEE80211_TX_STAT_ACK;

109
	if (tx_done->status == HTT_TX_COMPL_STATE_NOACK)
110 111
		info->flags &= ~IEEE80211_TX_STAT_ACK;

112 113
	if ((tx_done->status == HTT_TX_COMPL_STATE_ACK) &&
	    (info->flags & IEEE80211_TX_CTL_NO_ACK))
114 115
		info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;

116 117 118 119 120 121 122
	if (tx_done->status == HTT_TX_COMPL_STATE_DISCARD) {
		if (info->flags & IEEE80211_TX_CTL_NO_ACK)
			info->flags &= ~IEEE80211_TX_STAT_NOACK_TRANSMITTED;
		else
			info->flags &= ~IEEE80211_TX_STAT_ACK;
	}

123 124 125 126 127 128 129
	if (tx_done->status == HTT_TX_COMPL_STATE_ACK &&
	    tx_done->ack_rssi != ATH10K_INVALID_RSSI) {
		info->status.ack_signal = ATH10K_DEFAULT_NOISE_FLOOR +
						tx_done->ack_rssi;
		info->status.is_valid_ack_signal = true;
	}

130 131
	ieee80211_tx_status(htt->ar->hw, msdu);
	/* we do not own the msdu anymore */
M
Michal Kazior 已提交
132

133
	return 0;
134 135 136 137 138 139 140 141 142 143 144 145
}

struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
				     const u8 *addr)
{
	struct ath10k_peer *peer;

	lockdep_assert_held(&ar->data_lock);

	list_for_each_entry(peer, &ar->peers, list) {
		if (peer->vdev_id != vdev_id)
			continue;
146
		if (!ether_addr_equal(peer->addr, addr))
147 148 149 150 151 152 153 154
			continue;

		return peer;
	}

	return NULL;
}

155
struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar, int peer_id)
156 157 158
{
	struct ath10k_peer *peer;

159 160 161
	if (peer_id >= BITS_PER_TYPE(peer->peer_ids))
		return NULL;

162 163 164 165 166 167 168 169 170 171 172 173
	lockdep_assert_held(&ar->data_lock);

	list_for_each_entry(peer, &ar->peers, list)
		if (test_bit(peer_id, peer->peer_ids))
			return peer;

	return NULL;
}

static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
				       const u8 *addr, bool expect_mapped)
{
174
	long time_left;
175

176
	time_left = wait_event_timeout(ar->peer_mapping_wq, ({
177 178 179 180 181 182
			bool mapped;

			spin_lock_bh(&ar->data_lock);
			mapped = !!ath10k_peer_find(ar, vdev_id, addr);
			spin_unlock_bh(&ar->data_lock);

M
Michal Kazior 已提交
183 184
			(mapped == expect_mapped ||
			 test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags));
185
		}), 3 * HZ);
186

187
	if (time_left == 0)
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
		return -ETIMEDOUT;

	return 0;
}

int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
{
	return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
}

int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
{
	return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
}

void ath10k_peer_map_event(struct ath10k_htt *htt,
			   struct htt_peer_map_event *ev)
{
	struct ath10k *ar = htt->ar;
	struct ath10k_peer *peer;

209 210 211 212 213 214 215
	if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
		ath10k_warn(ar,
			    "received htt peer map event with idx out of bounds: %hu\n",
			    ev->peer_id);
		return;
	}

216 217 218 219 220 221 222 223
	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
	if (!peer) {
		peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
		if (!peer)
			goto exit;

		peer->vdev_id = ev->vdev_id;
K
Kalle Valo 已提交
224
		ether_addr_copy(peer->addr, ev->addr);
225 226 227 228
		list_add(&peer->list, &ar->peers);
		wake_up(&ar->peer_mapping_wq);
	}

229
	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
230 231
		   ev->vdev_id, ev->addr, ev->peer_id);

232
	WARN_ON(ar->peer_map[ev->peer_id] && (ar->peer_map[ev->peer_id] != peer));
M
Michal Kazior 已提交
233
	ar->peer_map[ev->peer_id] = peer;
234 235 236 237 238 239 240 241 242 243 244
	set_bit(ev->peer_id, peer->peer_ids);
exit:
	spin_unlock_bh(&ar->data_lock);
}

void ath10k_peer_unmap_event(struct ath10k_htt *htt,
			     struct htt_peer_unmap_event *ev)
{
	struct ath10k *ar = htt->ar;
	struct ath10k_peer *peer;

245 246 247 248 249 250 251
	if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
		ath10k_warn(ar,
			    "received htt peer unmap event with idx out of bounds: %hu\n",
			    ev->peer_id);
		return;
	}

252 253 254
	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find_by_id(ar, ev->peer_id);
	if (!peer) {
255
		ath10k_warn(ar, "peer-unmap-event: unknown peer id %d\n",
256
			    ev->peer_id);
257 258 259
		goto exit;
	}

260
	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
261 262
		   peer->vdev_id, peer->addr, ev->peer_id);

M
Michal Kazior 已提交
263
	ar->peer_map[ev->peer_id] = NULL;
264 265 266 267 268 269 270 271 272 273 274
	clear_bit(ev->peer_id, peer->peer_ids);

	if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
		list_del(&peer->list);
		kfree(peer);
		wake_up(&ar->peer_mapping_wq);
	}

exit:
	spin_unlock_bh(&ar->data_lock);
}