1f0553ca9SKalle Valo // SPDX-License-Identifier: ISC
25e3dd157SKalle Valo /*
35e3dd157SKalle Valo  * Copyright (c) 2005-2011 Atheros Communications Inc.
48b1083d6SKalle Valo  * Copyright (c) 2011-2016 Qualcomm Atheros, Inc.
5235b9c42SVenkateswara Naralasetty  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
65e3dd157SKalle Valo  */
75e3dd157SKalle Valo 
85e3dd157SKalle Valo #include "core.h"
95e3dd157SKalle Valo #include "txrx.h"
105e3dd157SKalle Valo #include "htt.h"
115e3dd157SKalle Valo #include "mac.h"
125e3dd157SKalle Valo #include "debug.h"
135e3dd157SKalle Valo 
ath10k_report_offchan_tx(struct ath10k * ar,struct sk_buff * skb)145e3dd157SKalle Valo static void ath10k_report_offchan_tx(struct ath10k *ar, struct sk_buff *skb)
155e3dd157SKalle Valo {
16e0813d34SMichal Kazior 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
17e0813d34SMichal Kazior 
18e0813d34SMichal Kazior 	if (likely(!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)))
19e0813d34SMichal Kazior 		return;
20e0813d34SMichal Kazior 
21e0813d34SMichal Kazior 	if (ath10k_mac_tx_frm_has_freq(ar))
225e3dd157SKalle Valo 		return;
235e3dd157SKalle Valo 
245e3dd157SKalle Valo 	/* If the original wait_for_completion() timed out before
255e3dd157SKalle Valo 	 * {data,mgmt}_tx_completed() was called then we could complete
265e3dd157SKalle Valo 	 * offchan_tx_completed for a different skb. Prevent this by using
27d6dfe25cSMarcin Rokicki 	 * offchan_tx_skb.
28d6dfe25cSMarcin Rokicki 	 */
295e3dd157SKalle Valo 	spin_lock_bh(&ar->data_lock);
305e3dd157SKalle Valo 	if (ar->offchan_tx_skb != skb) {
317aa7a72aSMichal Kazior 		ath10k_warn(ar, "completed old offchannel frame\n");
325e3dd157SKalle Valo 		goto out;
335e3dd157SKalle Valo 	}
345e3dd157SKalle Valo 
355e3dd157SKalle Valo 	complete(&ar->offchan_tx_completed);
365e3dd157SKalle Valo 	ar->offchan_tx_skb = NULL; /* just for sanity */
375e3dd157SKalle Valo 
3875b34800SMaharaja Kennadyrajan 	ath10k_dbg(ar, ATH10K_DBG_HTT, "completed offchannel skb %pK\n", skb);
395e3dd157SKalle Valo out:
405e3dd157SKalle Valo 	spin_unlock_bh(&ar->data_lock);
415e3dd157SKalle Valo }
425e3dd157SKalle Valo 
ath10k_txrx_tx_unref(struct ath10k_htt * htt,const struct htt_tx_done * tx_done)43cac08552SRajkumar Manoharan int ath10k_txrx_tx_unref(struct ath10k_htt *htt,
440a89f8a0SMichal Kazior 			 const struct htt_tx_done *tx_done)
455e3dd157SKalle Valo {
46*2587d519SSergey Ryazanov 	struct ieee80211_tx_status status;
477aa7a72aSMichal Kazior 	struct ath10k *ar = htt->ar;
487aa7a72aSMichal Kazior 	struct device *dev = ar->dev;
495e3dd157SKalle Valo 	struct ieee80211_tx_info *info;
503cc0fef6SMichal Kazior 	struct ieee80211_txq *txq;
511f8bb151SMichal Kazior 	struct ath10k_skb_cb *skb_cb;
523cc0fef6SMichal Kazior 	struct ath10k_txq *artxq;
531f8bb151SMichal Kazior 	struct sk_buff *msdu;
547b2531d9STamizh Chelvam 	u8 flags;
555e3dd157SKalle Valo 
5655314fc2SSujith Manoharan 	ath10k_dbg(ar, ATH10K_DBG_HTT,
5759465fe4SRajkumar Manoharan 		   "htt tx completion msdu_id %u status %d\n",
5859465fe4SRajkumar Manoharan 		   tx_done->msdu_id, tx_done->status);
595e3dd157SKalle Valo 
600a89f8a0SMichal Kazior 	if (tx_done->msdu_id >= htt->max_num_pending_tx) {
617aa7a72aSMichal Kazior 		ath10k_warn(ar, "warning: msdu_id %d too big, ignoring\n",
620a89f8a0SMichal Kazior 			    tx_done->msdu_id);
63cac08552SRajkumar Manoharan 		return -EINVAL;
640a89f8a0SMichal Kazior 	}
650a89f8a0SMichal Kazior 
66005fb161SQi Zhou 	spin_lock_bh(&htt->tx_lock);
6789d6d835SMichal Kazior 	msdu = idr_find(&htt->pending_tx, tx_done->msdu_id);
6889d6d835SMichal Kazior 	if (!msdu) {
6989d6d835SMichal Kazior 		ath10k_warn(ar, "received tx completion for invalid msdu_id: %d\n",
7089d6d835SMichal Kazior 			    tx_done->msdu_id);
71005fb161SQi Zhou 		spin_unlock_bh(&htt->tx_lock);
72cac08552SRajkumar Manoharan 		return -ENOENT;
7389d6d835SMichal Kazior 	}
747b7da0a0SVivek Natarajan 
75d668dbaeSMichal Kazior 	skb_cb = ATH10K_SKB_CB(msdu);
763cc0fef6SMichal Kazior 	txq = skb_cb->txq;
777b7da0a0SVivek Natarajan 
78a66cd733SBob Copeland 	if (txq) {
79a66cd733SBob Copeland 		artxq = (void *)txq->drv_priv;
803cc0fef6SMichal Kazior 		artxq->num_fw_queued--;
81a66cd733SBob Copeland 	}
823cc0fef6SMichal Kazior 
837b2531d9STamizh Chelvam 	flags = skb_cb->flags;
84005fb161SQi Zhou 	ath10k_htt_tx_free_msdu_id(htt, tx_done->msdu_id);
85cac08552SRajkumar Manoharan 	ath10k_htt_tx_dec_pending(htt);
86005fb161SQi Zhou 	spin_unlock_bh(&htt->tx_lock);
8789d6d835SMichal Kazior 
88acb31476SVenkateswara Naralasetty 	rcu_read_lock();
89bb31b7cbSManikanta Pubbisetty 	if (txq && txq->sta && skb_cb->airtime_est)
90d1ce37b7SKan Yan 		ieee80211_sta_register_airtime(txq->sta, txq->tid,
91d1ce37b7SKan Yan 					       skb_cb->airtime_est, 0);
92acb31476SVenkateswara Naralasetty 	rcu_read_unlock();
93d1ce37b7SKan Yan 
94de8781d7SGovind Singh 	if (ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL)
95767d34fcSMichal Kazior 		dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
965e3dd157SKalle Valo 
975e3dd157SKalle Valo 	ath10k_report_offchan_tx(htt->ar, msdu);
985e3dd157SKalle Valo 
995e3dd157SKalle Valo 	info = IEEE80211_SKB_CB(msdu);
1006d33a9a6SMichal Kazior 	memset(&info->status, 0, sizeof(info->status));
10105a11003SMiaoqing Pan 	info->status.rates[0].idx = -1;
10205a11003SMiaoqing Pan 
103d1e50f47SRajkumar Manoharan 	trace_ath10k_txrx_tx_unref(ar, tx_done->msdu_id);
1045e3dd157SKalle Valo 
1057b2531d9STamizh Chelvam 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
1067b2531d9STamizh Chelvam 	    !(flags & ATH10K_SKB_F_NOACK_TID))
1075e3dd157SKalle Valo 		info->flags |= IEEE80211_TX_STAT_ACK;
1085e3dd157SKalle Valo 
10959465fe4SRajkumar Manoharan 	if (tx_done->status == HTT_TX_COMPL_STATE_NOACK)
1105e3dd157SKalle Valo 		info->flags &= ~IEEE80211_TX_STAT_ACK;
1115e3dd157SKalle Valo 
11259465fe4SRajkumar Manoharan 	if ((tx_done->status == HTT_TX_COMPL_STATE_ACK) &&
1137b2531d9STamizh Chelvam 	    ((info->flags & IEEE80211_TX_CTL_NO_ACK) ||
1147b2531d9STamizh Chelvam 	    (flags & ATH10K_SKB_F_NOACK_TID)))
11555314fc2SSujith Manoharan 		info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
11655314fc2SSujith Manoharan 
117abb374feSIgnacio Nunez Hernanz 	if (tx_done->status == HTT_TX_COMPL_STATE_DISCARD) {
1187b2531d9STamizh Chelvam 		if ((info->flags & IEEE80211_TX_CTL_NO_ACK) ||
1197b2531d9STamizh Chelvam 		    (flags & ATH10K_SKB_F_NOACK_TID))
120abb374feSIgnacio Nunez Hernanz 			info->flags &= ~IEEE80211_TX_STAT_NOACK_TRANSMITTED;
121abb374feSIgnacio Nunez Hernanz 		else
122abb374feSIgnacio Nunez Hernanz 			info->flags &= ~IEEE80211_TX_STAT_ACK;
123abb374feSIgnacio Nunez Hernanz 	}
124abb374feSIgnacio Nunez Hernanz 
125235b9c42SVenkateswara Naralasetty 	if (tx_done->status == HTT_TX_COMPL_STATE_ACK &&
126235b9c42SVenkateswara Naralasetty 	    tx_done->ack_rssi != ATH10K_INVALID_RSSI) {
127235b9c42SVenkateswara Naralasetty 		info->status.ack_signal = ATH10K_DEFAULT_NOISE_FLOOR +
128235b9c42SVenkateswara Naralasetty 						tx_done->ack_rssi;
129ea5907dbSAvraham Stern 		info->status.flags |= IEEE80211_TX_STATUS_ACK_SIGNAL_VALID;
130235b9c42SVenkateswara Naralasetty 	}
131235b9c42SVenkateswara Naralasetty 
132*2587d519SSergey Ryazanov 	memset(&status, 0, sizeof(status));
133*2587d519SSergey Ryazanov 	status.skb = msdu;
134*2587d519SSergey Ryazanov 	status.info = info;
135*2587d519SSergey Ryazanov 
136*2587d519SSergey Ryazanov 	rcu_read_lock();
137*2587d519SSergey Ryazanov 
138*2587d519SSergey Ryazanov 	if (txq)
139*2587d519SSergey Ryazanov 		status.sta = txq->sta;
140*2587d519SSergey Ryazanov 
141*2587d519SSergey Ryazanov 	ieee80211_tx_status_ext(htt->ar->hw, &status);
142*2587d519SSergey Ryazanov 
143*2587d519SSergey Ryazanov 	rcu_read_unlock();
144*2587d519SSergey Ryazanov 
1455e3dd157SKalle Valo 	/* we do not own the msdu anymore */
1467a0adc83SMichal Kazior 
147cac08552SRajkumar Manoharan 	return 0;
1485e3dd157SKalle Valo }
1495e3dd157SKalle Valo 
ath10k_peer_find(struct ath10k * ar,int vdev_id,const u8 * addr)1505e3dd157SKalle Valo struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
1515e3dd157SKalle Valo 				     const u8 *addr)
1525e3dd157SKalle Valo {
1535e3dd157SKalle Valo 	struct ath10k_peer *peer;
1545e3dd157SKalle Valo 
1555e3dd157SKalle Valo 	lockdep_assert_held(&ar->data_lock);
1565e3dd157SKalle Valo 
1575e3dd157SKalle Valo 	list_for_each_entry(peer, &ar->peers, list) {
1585e3dd157SKalle Valo 		if (peer->vdev_id != vdev_id)
1595e3dd157SKalle Valo 			continue;
160c178da58SKalle Valo 		if (!ether_addr_equal(peer->addr, addr))
1615e3dd157SKalle Valo 			continue;
1625e3dd157SKalle Valo 
1635e3dd157SKalle Valo 		return peer;
1645e3dd157SKalle Valo 	}
1655e3dd157SKalle Valo 
1665e3dd157SKalle Valo 	return NULL;
1675e3dd157SKalle Valo }
1685e3dd157SKalle Valo 
ath10k_peer_find_by_id(struct ath10k * ar,int peer_id)169aa5b4fbcSMichal Kazior struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar, int peer_id)
1705e3dd157SKalle Valo {
1715e3dd157SKalle Valo 	struct ath10k_peer *peer;
1725e3dd157SKalle Valo 
17349ed34b8SWen Gong 	if (peer_id >= BITS_PER_TYPE(peer->peer_ids))
17449ed34b8SWen Gong 		return NULL;
17549ed34b8SWen Gong 
1765e3dd157SKalle Valo 	lockdep_assert_held(&ar->data_lock);
1775e3dd157SKalle Valo 
1785e3dd157SKalle Valo 	list_for_each_entry(peer, &ar->peers, list)
1795e3dd157SKalle Valo 		if (test_bit(peer_id, peer->peer_ids))
1805e3dd157SKalle Valo 			return peer;
1815e3dd157SKalle Valo 
1825e3dd157SKalle Valo 	return NULL;
1835e3dd157SKalle Valo }
1845e3dd157SKalle Valo 
ath10k_wait_for_peer_common(struct ath10k * ar,int vdev_id,const u8 * addr,bool expect_mapped)1855e3dd157SKalle Valo static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
1865e3dd157SKalle Valo 				       const u8 *addr, bool expect_mapped)
1875e3dd157SKalle Valo {
18871c47df4SNicholas Mc Guire 	long time_left;
1895e3dd157SKalle Valo 
19071c47df4SNicholas Mc Guire 	time_left = wait_event_timeout(ar->peer_mapping_wq, ({
1915e3dd157SKalle Valo 			bool mapped;
1925e3dd157SKalle Valo 
1935e3dd157SKalle Valo 			spin_lock_bh(&ar->data_lock);
1945e3dd157SKalle Valo 			mapped = !!ath10k_peer_find(ar, vdev_id, addr);
1955e3dd157SKalle Valo 			spin_unlock_bh(&ar->data_lock);
1965e3dd157SKalle Valo 
1977962b0d8SMichal Kazior 			(mapped == expect_mapped ||
1987962b0d8SMichal Kazior 			 test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags));
1995e3dd157SKalle Valo 		}), 3 * HZ);
2005e3dd157SKalle Valo 
20171c47df4SNicholas Mc Guire 	if (time_left == 0)
2025e3dd157SKalle Valo 		return -ETIMEDOUT;
2035e3dd157SKalle Valo 
2045e3dd157SKalle Valo 	return 0;
2055e3dd157SKalle Valo }
2065e3dd157SKalle Valo 
ath10k_wait_for_peer_created(struct ath10k * ar,int vdev_id,const u8 * addr)2075e3dd157SKalle Valo int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
2085e3dd157SKalle Valo {
2095e3dd157SKalle Valo 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
2105e3dd157SKalle Valo }
2115e3dd157SKalle Valo 
ath10k_wait_for_peer_deleted(struct ath10k * ar,int vdev_id,const u8 * addr)2125e3dd157SKalle Valo int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
2135e3dd157SKalle Valo {
2145e3dd157SKalle Valo 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
2155e3dd157SKalle Valo }
2165e3dd157SKalle Valo 
ath10k_peer_map_event(struct ath10k_htt * htt,struct htt_peer_map_event * ev)2175e3dd157SKalle Valo void ath10k_peer_map_event(struct ath10k_htt *htt,
2185e3dd157SKalle Valo 			   struct htt_peer_map_event *ev)
2195e3dd157SKalle Valo {
2205e3dd157SKalle Valo 	struct ath10k *ar = htt->ar;
2215e3dd157SKalle Valo 	struct ath10k_peer *peer;
2225e3dd157SKalle Valo 
223de72a20dSDan Carpenter 	if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
224de72a20dSDan Carpenter 		ath10k_warn(ar,
225779750bbSTom Rix 			    "received htt peer map event with idx out of bounds: %u\n",
226de72a20dSDan Carpenter 			    ev->peer_id);
227de72a20dSDan Carpenter 		return;
228de72a20dSDan Carpenter 	}
229de72a20dSDan Carpenter 
2305e3dd157SKalle Valo 	spin_lock_bh(&ar->data_lock);
2315e3dd157SKalle Valo 	peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
2325e3dd157SKalle Valo 	if (!peer) {
2335e3dd157SKalle Valo 		peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
2345e3dd157SKalle Valo 		if (!peer)
2355e3dd157SKalle Valo 			goto exit;
2365e3dd157SKalle Valo 
2375e3dd157SKalle Valo 		peer->vdev_id = ev->vdev_id;
238b25f32cbSKalle Valo 		ether_addr_copy(peer->addr, ev->addr);
2395e3dd157SKalle Valo 		list_add(&peer->list, &ar->peers);
2405e3dd157SKalle Valo 		wake_up(&ar->peer_mapping_wq);
2415e3dd157SKalle Valo 	}
2425e3dd157SKalle Valo 
2437aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
2445e3dd157SKalle Valo 		   ev->vdev_id, ev->addr, ev->peer_id);
2455e3dd157SKalle Valo 
246c5ace87aSBen Greear 	WARN_ON(ar->peer_map[ev->peer_id] && (ar->peer_map[ev->peer_id] != peer));
2476942726fSMichal Kazior 	ar->peer_map[ev->peer_id] = peer;
2485e3dd157SKalle Valo 	set_bit(ev->peer_id, peer->peer_ids);
2495e3dd157SKalle Valo exit:
2505e3dd157SKalle Valo 	spin_unlock_bh(&ar->data_lock);
2515e3dd157SKalle Valo }
2525e3dd157SKalle Valo 
ath10k_peer_unmap_event(struct ath10k_htt * htt,struct htt_peer_unmap_event * ev)2535e3dd157SKalle Valo void ath10k_peer_unmap_event(struct ath10k_htt *htt,
2545e3dd157SKalle Valo 			     struct htt_peer_unmap_event *ev)
2555e3dd157SKalle Valo {
2565e3dd157SKalle Valo 	struct ath10k *ar = htt->ar;
2575e3dd157SKalle Valo 	struct ath10k_peer *peer;
2585e3dd157SKalle Valo 
259de72a20dSDan Carpenter 	if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
260de72a20dSDan Carpenter 		ath10k_warn(ar,
261779750bbSTom Rix 			    "received htt peer unmap event with idx out of bounds: %u\n",
262de72a20dSDan Carpenter 			    ev->peer_id);
263de72a20dSDan Carpenter 		return;
264de72a20dSDan Carpenter 	}
265de72a20dSDan Carpenter 
2665e3dd157SKalle Valo 	spin_lock_bh(&ar->data_lock);
2675e3dd157SKalle Valo 	peer = ath10k_peer_find_by_id(ar, ev->peer_id);
2685e3dd157SKalle Valo 	if (!peer) {
2697aa7a72aSMichal Kazior 		ath10k_warn(ar, "peer-unmap-event: unknown peer id %d\n",
2709ba4c787SBen Greear 			    ev->peer_id);
2715e3dd157SKalle Valo 		goto exit;
2725e3dd157SKalle Valo 	}
2735e3dd157SKalle Valo 
2747aa7a72aSMichal Kazior 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
2755e3dd157SKalle Valo 		   peer->vdev_id, peer->addr, ev->peer_id);
2765e3dd157SKalle Valo 
2776942726fSMichal Kazior 	ar->peer_map[ev->peer_id] = NULL;
2785e3dd157SKalle Valo 	clear_bit(ev->peer_id, peer->peer_ids);
2795e3dd157SKalle Valo 
2805e3dd157SKalle Valo 	if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
2815e3dd157SKalle Valo 		list_del(&peer->list);
2825e3dd157SKalle Valo 		kfree(peer);
2835e3dd157SKalle Valo 		wake_up(&ar->peer_mapping_wq);
2845e3dd157SKalle Valo 	}
2855e3dd157SKalle Valo 
2865e3dd157SKalle Valo exit:
2875e3dd157SKalle Valo 	spin_unlock_bh(&ar->data_lock);
2885e3dd157SKalle Valo }
289