1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc.
3 *
4 * Author: Felix Fietkau <nbd@nbd.name>
5 * Lorenzo Bianconi <lorenzo@kernel.org>
6 * Sean Wang <sean.wang@mediatek.com>
7 */
8
9 #include <linux/kernel.h>
10 #include <linux/iopoll.h>
11 #include <linux/module.h>
12
13 #include <linux/mmc/host.h>
14 #include <linux/mmc/sdio_ids.h>
15 #include <linux/mmc/sdio_func.h>
16
17 #include "../trace.h"
18 #include "mt7615.h"
19 #include "sdio.h"
20 #include "mac.h"
21
mt7663s_refill_sched_quota(struct mt76_dev * dev,u32 * data)22 static int mt7663s_refill_sched_quota(struct mt76_dev *dev, u32 *data)
23 {
24 u32 ple_ac_data_quota[] = {
25 FIELD_GET(TXQ_CNT_L, data[4]), /* VO */
26 FIELD_GET(TXQ_CNT_H, data[3]), /* VI */
27 FIELD_GET(TXQ_CNT_L, data[3]), /* BE */
28 FIELD_GET(TXQ_CNT_H, data[2]), /* BK */
29 };
30 u32 pse_ac_data_quota[] = {
31 FIELD_GET(TXQ_CNT_H, data[1]), /* VO */
32 FIELD_GET(TXQ_CNT_L, data[1]), /* VI */
33 FIELD_GET(TXQ_CNT_H, data[0]), /* BE */
34 FIELD_GET(TXQ_CNT_L, data[0]), /* BK */
35 };
36 u32 pse_mcu_quota = FIELD_GET(TXQ_CNT_L, data[2]);
37 u32 pse_data_quota = 0, ple_data_quota = 0;
38 struct mt76_sdio *sdio = &dev->sdio;
39 int i;
40
41 for (i = 0; i < ARRAY_SIZE(pse_ac_data_quota); i++) {
42 pse_data_quota += pse_ac_data_quota[i];
43 ple_data_quota += ple_ac_data_quota[i];
44 }
45
46 if (!pse_data_quota && !ple_data_quota && !pse_mcu_quota)
47 return 0;
48
49 mutex_lock(&sdio->sched.lock);
50 sdio->sched.pse_mcu_quota += pse_mcu_quota;
51 sdio->sched.pse_data_quota += pse_data_quota;
52 sdio->sched.ple_data_quota += ple_data_quota;
53 mutex_unlock(&sdio->sched.lock);
54
55 return pse_data_quota + ple_data_quota + pse_mcu_quota;
56 }
57
mt7663s_build_rx_skb(void * data,int data_len,int buf_len)58 static struct sk_buff *mt7663s_build_rx_skb(void *data, int data_len,
59 int buf_len)
60 {
61 int len = min_t(int, data_len, MT_SKB_HEAD_LEN);
62 struct sk_buff *skb;
63
64 skb = alloc_skb(len, GFP_KERNEL);
65 if (!skb)
66 return NULL;
67
68 skb_put_data(skb, data, len);
69 if (data_len > len) {
70 struct page *page;
71
72 data += len;
73 page = virt_to_head_page(data);
74 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
75 page, data - page_address(page),
76 data_len - len, buf_len);
77 get_page(page);
78 }
79
80 return skb;
81 }
82
mt7663s_rx_run_queue(struct mt76_dev * dev,enum mt76_rxq_id qid,struct mt76s_intr * intr)83 static int mt7663s_rx_run_queue(struct mt76_dev *dev, enum mt76_rxq_id qid,
84 struct mt76s_intr *intr)
85 {
86 struct mt76_queue *q = &dev->q_rx[qid];
87 struct mt76_sdio *sdio = &dev->sdio;
88 int len = 0, err, i, order;
89 struct page *page;
90 u8 *buf;
91
92 for (i = 0; i < intr->rx.num[qid]; i++)
93 len += round_up(intr->rx.len[qid][i] + 4, 4);
94
95 if (!len)
96 return 0;
97
98 if (len > sdio->func->cur_blksize)
99 len = roundup(len, sdio->func->cur_blksize);
100
101 order = get_order(len);
102 page = __dev_alloc_pages(GFP_KERNEL, order);
103 if (!page)
104 return -ENOMEM;
105
106 buf = page_address(page);
107
108 sdio_claim_host(sdio->func);
109 err = sdio_readsb(sdio->func, buf, MCR_WRDR(qid), len);
110 sdio_release_host(sdio->func);
111
112 if (err < 0) {
113 dev_err(dev->dev, "sdio read data failed:%d\n", err);
114 __free_pages(page, order);
115 return err;
116 }
117
118 for (i = 0; i < intr->rx.num[qid]; i++) {
119 int index = (q->head + i) % q->ndesc;
120 struct mt76_queue_entry *e = &q->entry[index];
121
122 len = intr->rx.len[qid][i];
123 e->skb = mt7663s_build_rx_skb(buf, len, round_up(len + 4, 4));
124 if (!e->skb)
125 break;
126
127 buf += round_up(len + 4, 4);
128 if (q->queued + i + 1 == q->ndesc)
129 break;
130 }
131 __free_pages(page, order);
132
133 spin_lock_bh(&q->lock);
134 q->head = (q->head + i) % q->ndesc;
135 q->queued += i;
136 spin_unlock_bh(&q->lock);
137
138 return i;
139 }
140
mt7663s_tx_pick_quota(struct mt76_sdio * sdio,enum mt76_txq_id qid,int buf_sz,int * pse_size,int * ple_size)141 static int mt7663s_tx_pick_quota(struct mt76_sdio *sdio, enum mt76_txq_id qid,
142 int buf_sz, int *pse_size, int *ple_size)
143 {
144 int pse_sz;
145
146 pse_sz = DIV_ROUND_UP(buf_sz + sdio->sched.deficit, MT_PSE_PAGE_SZ);
147
148 if (qid == MT_TXQ_MCU) {
149 if (sdio->sched.pse_mcu_quota < *pse_size + pse_sz)
150 return -EBUSY;
151 } else {
152 if (sdio->sched.pse_data_quota < *pse_size + pse_sz ||
153 sdio->sched.ple_data_quota < *ple_size)
154 return -EBUSY;
155
156 *ple_size = *ple_size + 1;
157 }
158 *pse_size = *pse_size + pse_sz;
159
160 return 0;
161 }
162
mt7663s_tx_update_quota(struct mt76_sdio * sdio,enum mt76_txq_id qid,int pse_size,int ple_size)163 static void mt7663s_tx_update_quota(struct mt76_sdio *sdio, enum mt76_txq_id qid,
164 int pse_size, int ple_size)
165 {
166 mutex_lock(&sdio->sched.lock);
167 if (qid == MT_TXQ_MCU) {
168 sdio->sched.pse_mcu_quota -= pse_size;
169 } else {
170 sdio->sched.pse_data_quota -= pse_size;
171 sdio->sched.ple_data_quota -= ple_size;
172 }
173 mutex_unlock(&sdio->sched.lock);
174 }
175
__mt7663s_xmit_queue(struct mt76_dev * dev,u8 * data,int len)176 static int __mt7663s_xmit_queue(struct mt76_dev *dev, u8 *data, int len)
177 {
178 struct mt76_sdio *sdio = &dev->sdio;
179 int err;
180
181 if (len > sdio->func->cur_blksize)
182 len = roundup(len, sdio->func->cur_blksize);
183
184 sdio_claim_host(sdio->func);
185 err = sdio_writesb(sdio->func, MCR_WTDR1, data, len);
186 sdio_release_host(sdio->func);
187
188 if (err)
189 dev_err(dev->dev, "sdio write failed: %d\n", err);
190
191 return err;
192 }
193
mt7663s_tx_run_queue(struct mt76_dev * dev,enum mt76_txq_id qid)194 static int mt7663s_tx_run_queue(struct mt76_dev *dev, enum mt76_txq_id qid)
195 {
196 int err, nframes = 0, len = 0, pse_sz = 0, ple_sz = 0;
197 struct mt76_queue *q = dev->q_tx[qid];
198 struct mt76_sdio *sdio = &dev->sdio;
199
200 while (q->first != q->head) {
201 struct mt76_queue_entry *e = &q->entry[q->first];
202 struct sk_buff *iter;
203
204 if (!test_bit(MT76_STATE_MCU_RUNNING, &dev->phy.state)) {
205 __skb_put_zero(e->skb, 4);
206 err = __mt7663s_xmit_queue(dev, e->skb->data,
207 e->skb->len);
208 if (err)
209 return err;
210
211 goto next;
212 }
213
214 if (len + e->skb->len + 4 > MT76S_XMIT_BUF_SZ)
215 break;
216
217 if (mt7663s_tx_pick_quota(sdio, qid, e->buf_sz, &pse_sz,
218 &ple_sz))
219 break;
220
221 memcpy(sdio->xmit_buf[qid] + len, e->skb->data,
222 skb_headlen(e->skb));
223 len += skb_headlen(e->skb);
224 nframes++;
225
226 skb_walk_frags(e->skb, iter) {
227 memcpy(sdio->xmit_buf[qid] + len, iter->data,
228 iter->len);
229 len += iter->len;
230 nframes++;
231 }
232 next:
233 q->first = (q->first + 1) % q->ndesc;
234 e->done = true;
235 }
236
237 if (nframes) {
238 memset(sdio->xmit_buf[qid] + len, 0, 4);
239 err = __mt7663s_xmit_queue(dev, sdio->xmit_buf[qid], len + 4);
240 if (err)
241 return err;
242 }
243 mt7663s_tx_update_quota(sdio, qid, pse_sz, ple_sz);
244
245 return nframes;
246 }
247
mt7663s_tx_work(struct work_struct * work)248 void mt7663s_tx_work(struct work_struct *work)
249 {
250 struct mt76_sdio *sdio = container_of(work, struct mt76_sdio,
251 tx.xmit_work);
252 struct mt76_dev *dev = container_of(sdio, struct mt76_dev, sdio);
253 int i, nframes = 0;
254
255 for (i = 0; i < MT_TXQ_MCU_WA; i++) {
256 int ret;
257
258 ret = mt7663s_tx_run_queue(dev, i);
259 if (ret < 0)
260 break;
261
262 nframes += ret;
263 }
264 if (nframes)
265 queue_work(sdio->txrx_wq, &sdio->tx.xmit_work);
266
267 queue_work(sdio->txrx_wq, &sdio->tx.status_work);
268 }
269
mt7663s_rx_work(struct work_struct * work)270 void mt7663s_rx_work(struct work_struct *work)
271 {
272 struct mt76_sdio *sdio = container_of(work, struct mt76_sdio,
273 rx.recv_work);
274 struct mt76_dev *dev = container_of(sdio, struct mt76_dev, sdio);
275 struct mt76s_intr *intr = sdio->intr_data;
276 int nframes = 0, ret;
277
278 /* disable interrupt */
279 sdio_claim_host(sdio->func);
280 sdio_writel(sdio->func, WHLPCR_INT_EN_CLR, MCR_WHLPCR, NULL);
281 ret = sdio_readsb(sdio->func, intr, MCR_WHISR, sizeof(*intr));
282 sdio_release_host(sdio->func);
283
284 if (ret < 0)
285 goto out;
286
287 trace_dev_irq(dev, intr->isr, 0);
288
289 if (intr->isr & WHIER_RX0_DONE_INT_EN) {
290 ret = mt7663s_rx_run_queue(dev, 0, intr);
291 if (ret > 0) {
292 queue_work(sdio->txrx_wq, &sdio->rx.net_work);
293 nframes += ret;
294 }
295 }
296
297 if (intr->isr & WHIER_RX1_DONE_INT_EN) {
298 ret = mt7663s_rx_run_queue(dev, 1, intr);
299 if (ret > 0) {
300 queue_work(sdio->txrx_wq, &sdio->rx.net_work);
301 nframes += ret;
302 }
303 }
304
305 if (mt7663s_refill_sched_quota(dev, intr->tx.wtqcr))
306 queue_work(sdio->txrx_wq, &sdio->tx.xmit_work);
307
308 if (nframes) {
309 queue_work(sdio->txrx_wq, &sdio->rx.recv_work);
310 return;
311 }
312 out:
313 /* enable interrupt */
314 sdio_claim_host(sdio->func);
315 sdio_writel(sdio->func, WHLPCR_INT_EN_SET, MCR_WHLPCR, NULL);
316 sdio_release_host(sdio->func);
317 }
318
mt7663s_sdio_irq(struct sdio_func * func)319 void mt7663s_sdio_irq(struct sdio_func *func)
320 {
321 struct mt7615_dev *dev = sdio_get_drvdata(func);
322 struct mt76_sdio *sdio = &dev->mt76.sdio;
323
324 if (!test_bit(MT76_STATE_INITIALIZED, &dev->mt76.phy.state))
325 return;
326
327 queue_work(sdio->txrx_wq, &sdio->rx.recv_work);
328 }
329