1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc.
3  *
4  * Author: Ryder Lee <ryder.lee@mediatek.com>
5  *         Roy Luo <royluo@google.com>
6  *         Felix Fietkau <nbd@nbd.name>
7  *         Lorenzo Bianconi <lorenzo@kernel.org>
8  */
9 
10 #include <linux/etherdevice.h>
11 #include <linux/timekeeping.h>
12 
13 #include "mt7615.h"
14 #include "../dma.h"
15 #include "mac.h"
16 
17 void mt7615_tx_complete_skb(struct mt76_dev *mdev, enum mt76_txq_id qid,
18 			    struct mt76_queue_entry *e)
19 {
20 	if (!e->txwi) {
21 		dev_kfree_skb_any(e->skb);
22 		return;
23 	}
24 
25 	/* error path */
26 	if (e->skb == DMA_DUMMY_DATA) {
27 		struct mt76_txwi_cache *t;
28 		struct mt7615_dev *dev;
29 		struct mt7615_txp_common *txp;
30 		u16 token;
31 
32 		dev = container_of(mdev, struct mt7615_dev, mt76);
33 		txp = mt7615_txwi_to_txp(mdev, e->txwi);
34 
35 		if (is_mt7615(&dev->mt76))
36 			token = le16_to_cpu(txp->fw.token);
37 		else
38 			token = le16_to_cpu(txp->hw.msdu_id[0]) &
39 				~MT_MSDU_ID_VALID;
40 
41 		spin_lock_bh(&dev->token_lock);
42 		t = idr_remove(&dev->token, token);
43 		spin_unlock_bh(&dev->token_lock);
44 		e->skb = t ? t->skb : NULL;
45 	}
46 
47 	if (e->skb)
48 		mt76_tx_complete_skb(mdev, e->skb);
49 }
50 
51 static void
52 mt7615_write_hw_txp(struct mt7615_dev *dev, struct mt76_tx_info *tx_info,
53 		    void *txp_ptr, u32 id)
54 {
55 	struct mt7615_hw_txp *txp = txp_ptr;
56 	struct mt7615_txp_ptr *ptr = &txp->ptr[0];
57 	int i, nbuf = tx_info->nbuf - 1;
58 	u32 last_mask;
59 
60 	tx_info->buf[0].len = MT_TXD_SIZE + sizeof(*txp);
61 	tx_info->nbuf = 1;
62 
63 	txp->msdu_id[0] = cpu_to_le16(id | MT_MSDU_ID_VALID);
64 
65 	if (is_mt7663(&dev->mt76))
66 		last_mask = MT_TXD_LEN_LAST;
67 	else
68 		last_mask = MT_TXD_LEN_AMSDU_LAST |
69 			    MT_TXD_LEN_MSDU_LAST;
70 
71 	for (i = 0; i < nbuf; i++) {
72 		u16 len = tx_info->buf[i + 1].len & MT_TXD_LEN_MASK;
73 		u32 addr = tx_info->buf[i + 1].addr;
74 
75 		if (i == nbuf - 1)
76 			len |= last_mask;
77 
78 		if (i & 1) {
79 			ptr->buf1 = cpu_to_le32(addr);
80 			ptr->len1 = cpu_to_le16(len);
81 			ptr++;
82 		} else {
83 			ptr->buf0 = cpu_to_le32(addr);
84 			ptr->len0 = cpu_to_le16(len);
85 		}
86 	}
87 }
88 
89 static void
90 mt7615_write_fw_txp(struct mt7615_dev *dev, struct mt76_tx_info *tx_info,
91 		    void *txp_ptr, u32 id)
92 {
93 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)tx_info->skb->data;
94 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);
95 	struct ieee80211_key_conf *key = info->control.hw_key;
96 	struct ieee80211_vif *vif = info->control.vif;
97 	struct mt7615_fw_txp *txp = txp_ptr;
98 	int nbuf = tx_info->nbuf - 1;
99 	int i;
100 
101 	for (i = 0; i < nbuf; i++) {
102 		txp->buf[i] = cpu_to_le32(tx_info->buf[i + 1].addr);
103 		txp->len[i] = cpu_to_le16(tx_info->buf[i + 1].len);
104 	}
105 	txp->nbuf = nbuf;
106 
107 	/* pass partial skb header to fw */
108 	tx_info->buf[0].len = MT_TXD_SIZE + sizeof(*txp);
109 	tx_info->buf[1].len = MT_CT_PARSE_LEN;
110 	tx_info->nbuf = MT_CT_DMA_BUF_NUM;
111 
112 	txp->flags = cpu_to_le16(MT_CT_INFO_APPLY_TXD);
113 
114 	if (!key)
115 		txp->flags |= cpu_to_le16(MT_CT_INFO_NONE_CIPHER_FRAME);
116 
117 	if (ieee80211_is_mgmt(hdr->frame_control))
118 		txp->flags |= cpu_to_le16(MT_CT_INFO_MGMT_FRAME);
119 
120 	if (vif) {
121 		struct mt7615_vif *mvif = (struct mt7615_vif *)vif->drv_priv;
122 
123 		txp->bss_idx = mvif->idx;
124 	}
125 
126 	txp->token = cpu_to_le16(id);
127 	txp->rept_wds_wcid = 0xff;
128 }
129 
130 int mt7615_tx_prepare_skb(struct mt76_dev *mdev, void *txwi_ptr,
131 			  enum mt76_txq_id qid, struct mt76_wcid *wcid,
132 			  struct ieee80211_sta *sta,
133 			  struct mt76_tx_info *tx_info)
134 {
135 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
136 	struct mt7615_sta *msta = container_of(wcid, struct mt7615_sta, wcid);
137 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(tx_info->skb);
138 	struct ieee80211_key_conf *key = info->control.hw_key;
139 	int pid, id;
140 	u8 *txwi = (u8 *)txwi_ptr;
141 	struct mt76_txwi_cache *t;
142 	void *txp;
143 
144 	if (!wcid)
145 		wcid = &dev->mt76.global_wcid;
146 
147 	pid = mt76_tx_status_skb_add(mdev, wcid, tx_info->skb);
148 
149 	if (info->flags & IEEE80211_TX_CTL_RATE_CTRL_PROBE) {
150 		struct mt7615_phy *phy = &dev->phy;
151 
152 		if ((info->hw_queue & MT_TX_HW_QUEUE_EXT_PHY) && mdev->phy2)
153 			phy = mdev->phy2->priv;
154 
155 		spin_lock_bh(&dev->mt76.lock);
156 		mt7615_mac_set_rates(phy, msta, &info->control.rates[0],
157 				     msta->rates);
158 		spin_unlock_bh(&dev->mt76.lock);
159 	}
160 
161 	t = (struct mt76_txwi_cache *)(txwi + mdev->drv->txwi_size);
162 	t->skb = tx_info->skb;
163 
164 	spin_lock_bh(&dev->token_lock);
165 	id = idr_alloc(&dev->token, t, 0, MT7615_TOKEN_SIZE, GFP_ATOMIC);
166 	spin_unlock_bh(&dev->token_lock);
167 	if (id < 0)
168 		return id;
169 
170 	mt7615_mac_write_txwi(dev, txwi_ptr, tx_info->skb, wcid, sta,
171 			      pid, key, false);
172 
173 	txp = txwi + MT_TXD_SIZE;
174 	memset(txp, 0, sizeof(struct mt7615_txp_common));
175 	if (is_mt7615(&dev->mt76))
176 		mt7615_write_fw_txp(dev, tx_info, txp, id);
177 	else
178 		mt7615_write_hw_txp(dev, tx_info, txp, id);
179 
180 	tx_info->skb = DMA_DUMMY_DATA;
181 
182 	return 0;
183 }
184