1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Copyright (c) 2025 AIROHA Inc
* Author: Lorenzo Bianconi <lorenzo@kernel.org>
*/
#include <linux/kernel.h>
#include <net/flow_offload.h>
#include <net/pkt_cls.h>
#include "mt76.h"
#include "dma.h"
#include "mt76_connac.h"
#define MT76_NPU_RX_BUF_SIZE (1800 + \
SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
int mt76_npu_fill_rx_queue(struct mt76_dev *dev, struct mt76_queue *q)
{
int nframes = 0;
while (q->queued < q->ndesc - 1) {
struct airoha_npu_rx_dma_desc *desc = (void *)q->desc;
struct mt76_queue_entry *e = &q->entry[q->head];
struct page *page;
int offset;
e->buf = mt76_get_page_pool_buf(q, &offset, q->buf_size);
if (!e->buf)
break;
e->dma_len[0] = SKB_WITH_OVERHEAD(q->buf_size);
page = virt_to_head_page(e->buf);
e->dma_addr[0] = page_pool_get_dma_addr(page) + offset;
memset(&desc[q->head], 0, sizeof(*desc));
desc[q->head].addr = e->dma_addr[0];
q->head = (q->head + 1) % q->ndesc;
q->queued++;
nframes++;
}
return nframes;
}
void mt76_npu_queue_cleanup(struct mt76_dev *dev, struct mt76_queue *q)
{
spin_lock_bh(&q->lock);
while (q->queued > 0) {
struct mt76_queue_entry *e = &q->entry[q->tail];
dma_sync_single_for_cpu(dev->dma_dev, e->dma_addr[0],
e->dma_len[0],
page_pool_get_dma_dir(q->page_pool));
mt76_put_page_pool_buf(e->buf, false);
q->tail = (q->tail + 1) % q->ndesc;
q->queued--;
}
spin_unlock_bh(&q->lock);
}
static struct sk_buff *mt76_npu_dequeue(struct mt76_dev *dev,
struct mt76_queue *q,
u32 *info)
{
struct airoha_npu_rx_dma_desc *desc = (void *)q->desc;
int i, nframes, index = q->tail;
struct sk_buff *skb = NULL;
nframes = FIELD_GET(NPU_RX_DMA_PKT_COUNT_MASK, desc[index].info);
nframes = max_t(int, nframes, 1);
for (i = 0; i < nframes; i++) {
struct mt76_queue_entry *e = &q->entry[index];
int len = FIELD_GET(NPU_RX_DMA_DESC_CUR_LEN_MASK,
desc[index].ctrl);
if (!FIELD_GET(NPU_RX_DMA_DESC_DONE_MASK, desc[index].ctrl)) {
dev_kfree_skb(skb);
return NULL;
}
dma_sync_single_for_cpu(dev->dma_dev, e->dma_addr[0],
e->dma_len[0],
page_pool_get_dma_dir(q->page_pool));
if (!skb) {
skb = napi_build_skb(e->buf, q->buf_size);
if (!skb)
return NULL;
__skb_put(skb, len);
skb_reset_mac_header(skb);
skb_mark_for_recycle(skb);
} else {
struct skb_shared_info *shinfo = skb_shinfo(skb);
struct page *page = virt_to_head_page(e->buf);
int nr_frags = shinfo->nr_frags;
if (nr_frags < ARRAY_SIZE(shinfo->frags))
skb_add_rx_frag(skb, nr_frags, page,
e->buf - page_address(page),
len, q->buf_size);
}
*info = desc[index].info;
index = (index + 1) % q->ndesc;
}
q->tail = index;
q->queued -= i;
Q_WRITE(q, dma_idx, q->tail);
return skb;
}
void mt76_npu_check_ppe(struct mt76_dev *dev, struct sk_buff *skb,
u32 info)
{
struct airoha_ppe_dev *ppe_dev;
u16 reason, hash;
if (!mt76_npu_device_active(dev))
return;
rcu_read_lock();
ppe_dev = rcu_dereference(dev->mmio.ppe_dev);
if (!ppe_dev)
goto out;
hash = FIELD_GET(NPU_RX_DMA_FOE_ID_MASK, info);
skb_set_hash(skb, hash, PKT_HASH_TYPE_L4);
reason = FIELD_GET(NPU_RX_DMA_CRSN_MASK, info);
if (reason == PPE_CPU_REASON_HIT_UNBIND_RATE_REACHED) {
skb_set_mac_header(skb, 0);
airoha_ppe_dev_check_skb(ppe_dev, skb, hash, true);
}
out:
rcu_read_unlock();
}
EXPORT_SYMBOL_GPL(mt76_npu_check_ppe);
static int mt76_npu_rx_poll(struct napi_struct *napi, int budget)
{
struct mt76_dev *dev = mt76_priv(napi->dev);
enum mt76_rxq_id qid = napi - dev->napi;
struct airoha_npu *npu;
int done = 0;
rcu_read_lock();
npu = rcu_dereference(dev->mmio.npu);
if (!npu)
goto out;
while (done < budget) {
struct sk_buff *skb;
u32 info = 0;
skb = mt76_npu_dequeue(dev, &dev->q_rx[qid], &info);
if (!skb)
break;
dev->drv->rx_skb(dev, qid, skb, &info);
mt76_rx_poll_complete(dev, qid, napi);
done++;
}
mt76_npu_fill_rx_queue(dev, &dev->q_rx[qid]);
out:
if (done < budget && napi_complete(napi))
dev->drv->rx_poll_complete(dev, qid);
rcu_read_unlock();
return done;
}
static irqreturn_t mt76_npu_irq_handler(int irq, void *q_instance)
{
struct mt76_queue *q = q_instance;
struct mt76_dev *dev = q->dev;
int qid = q - &dev->q_rx[0];
int index = qid - MT_RXQ_NPU0;
struct airoha_npu *npu;
u32 status;
rcu_read_lock();
npu = rcu_dereference(dev->mmio.npu);
if (!npu)
goto out;
status = airoha_npu_wlan_get_irq_status(npu, index);
airoha_npu_wlan_set_irq_status(npu, status);
airoha_npu_wlan_disable_irq(npu, index);
napi_schedule(&dev->napi[qid]);
out:
rcu_read_unlock();
return IRQ_HANDLED;
}
int mt76_npu_dma_add_buf(struct mt76_phy *phy, struct mt76_queue *q,
struct sk_buff *skb, struct mt76_queue_buf *buf,
void *txwi_ptr)
{
u16 txwi_len = min_t(u16, phy->dev->drv->txwi_size, NPU_TXWI_LEN);
struct airoha_npu_tx_dma_desc *desc = (void *)q->desc;
int ret;
/* TODO: Take into account unlinear skbs */
memcpy(desc[q->head].txwi, txwi_ptr, txwi_len);
desc[q->head].addr = buf->addr;
desc[q->head].ctrl = FIELD_PREP(NPU_TX_DMA_DESC_VEND_LEN_MASK, txwi_len) |
FIELD_PREP(NPU_TX_DMA_DESC_LEN_MASK, skb->len) |
NPU_TX_DMA_DESC_DONE_MASK;
ret = q->head;
q->entry[q->head].skip_buf0 = true;
q->entry[q->head].skip_buf1 = true;
q->entry[q->head].txwi = NULL;
q->entry[q->head].skb = NULL;
q->entry[q->head].wcid = 0xffff;
q->head = (q->head + 1) % q->ndesc;
q->queued++;
return ret;
}
void mt76_npu_txdesc_cleanup(struct mt76_queue *q, int index)
{
struct airoha_npu_tx_dma_desc *desc = (void *)q->desc;
if (!mt76_queue_is_npu_tx(q))
return;
desc[index].ctrl &= ~NPU_TX_DMA_DESC_DONE_MASK;
}
void mt76_npu_queue_setup(struct mt76_dev *dev, struct mt76_queue *q)
{
int qid = FIELD_GET(MT_QFLAG_WED_RING, q->flags);
bool xmit = mt76_queue_is_npu_tx(q);
struct airoha_npu *npu;
if (!mt76_queue_is_npu(q))
return;
npu = rcu_dereference_protected(dev->mmio.npu, &dev->mutex);
if (npu)
q->wed_regs = airoha_npu_wlan_get_queue_addr(npu, qid, xmit);
}
int mt76_npu_rx_queue_init(struct mt76_dev *dev, struct mt76_queue *q)
{
int err, irq, qid = q - &dev->q_rx[0];
int size, index = qid - MT_RXQ_NPU0;
struct airoha_npu *npu;
const char *name;
mutex_lock(&dev->mutex);
npu = rcu_dereference_protected(dev->mmio.npu, &dev->mutex);
irq = npu && index < ARRAY_SIZE(npu->irqs) ? npu->irqs[index]
: -EINVAL;
if (irq < 0) {
err = irq;
goto out;
}
q->flags = MT_NPU_Q_RX(index);
size = qid == MT_RXQ_NPU1 ? NPU_RX1_DESC_NUM : NPU_RX0_DESC_NUM;
err = dev->queue_ops->alloc(dev, q, 0, size,
MT76_NPU_RX_BUF_SIZE, 0);
if (err)
goto out;
name = devm_kasprintf(dev->dev, GFP_KERNEL, "mt76-npu.%d", index);
if (!name) {
err = -ENOMEM;
goto out;
}
err = devm_request_irq(dev->dev, irq, mt76_npu_irq_handler,
IRQF_SHARED, name, q);
if (err)
goto out;
netif_napi_add(dev->napi_dev, &dev->napi[qid], mt76_npu_rx_poll);
mt76_npu_fill_rx_queue(dev, q);
napi_enable(&dev->napi[qid]);
out:
mutex_unlock(&dev->mutex);
return err;
}
EXPORT_SYMBOL_GPL(mt76_npu_rx_queue_init);
static int mt76_npu_setup_tc_block_cb(enum tc_setup_type type,
void *type_data, void *cb_priv)
{
struct mt76_phy *phy = cb_priv;
struct mt76_dev *dev = phy->dev;
struct airoha_ppe_dev *ppe_dev;
int err = -EOPNOTSUPP;
if (type != TC_SETUP_CLSFLOWER)
return -EOPNOTSUPP;
mutex_lock(&dev->mutex);
ppe_dev = rcu_dereference_protected(dev->mmio.ppe_dev, &dev->mutex);
if (ppe_dev)
err = airoha_ppe_dev_setup_tc_block_cb(ppe_dev, type_data);
mutex_unlock(&dev->mutex);
return err;
}
static int mt76_npu_setup_tc_block(struct mt76_phy *phy,
struct net_device *dev,
struct flow_block_offload *f)
{
flow_setup_cb_t *cb = mt76_npu_setup_tc_block_cb;
static LIST_HEAD(block_cb_list);
struct flow_block_cb *block_cb;
if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
return -EOPNOTSUPP;
if (!tc_can_offload(dev))
return -EOPNOTSUPP;
f->driver_block_list = &block_cb_list;
switch (f->command) {
case FLOW_BLOCK_BIND:
block_cb = flow_block_cb_lookup(f->block, cb, dev);
if (block_cb) {
flow_block_cb_incref(block_cb);
return 0;
}
block_cb = flow_block_cb_alloc(cb, dev, phy, NULL);
if (IS_ERR(block_cb))
return PTR_ERR(block_cb);
flow_block_cb_incref(block_cb);
flow_block_cb_add(block_cb, f);
list_add_tail(&block_cb->driver_list, &block_cb_list);
return 0;
case FLOW_BLOCK_UNBIND:
block_cb = flow_block_cb_lookup(f->block, cb, dev);
if (!block_cb)
return -ENOENT;
if (!flow_block_cb_decref(block_cb)) {
flow_block_cb_remove(block_cb, f);
list_del(&block_cb->driver_list);
}
return 0;
default:
return -EOPNOTSUPP;
}
}
int mt76_npu_net_setup_tc(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
struct net_device *dev, enum tc_setup_type type,
void *type_data)
{
struct mt76_phy *phy = hw->priv;
if (!tc_can_offload(dev))
return -EOPNOTSUPP;
if (!mt76_npu_device_active(phy->dev))
return -EOPNOTSUPP;
switch (type) {
case TC_SETUP_BLOCK:
case TC_SETUP_FT:
return mt76_npu_setup_tc_block(phy, dev, type_data);
default:
return -EOPNOTSUPP;
}
}
EXPORT_SYMBOL_GPL(mt76_npu_net_setup_tc);
void mt76_npu_disable_irqs(struct mt76_dev *dev)
{
struct airoha_npu *npu;
int i;
rcu_read_lock();
npu = rcu_dereference(dev->mmio.npu);
if (!npu)
goto unlock;
for (i = MT_RXQ_NPU0; i <= MT_RXQ_NPU1; i++) {
int qid = i - MT_RXQ_NPU0;
u32 status;
status = airoha_npu_wlan_get_irq_status(npu, qid);
airoha_npu_wlan_set_irq_status(npu, status);
airoha_npu_wlan_disable_irq(npu, qid);
}
unlock:
rcu_read_unlock();
}
EXPORT_SYMBOL_GPL(mt76_npu_disable_irqs);
int mt76_npu_init(struct mt76_dev *dev, phys_addr_t phy_addr, int type)
{
struct airoha_ppe_dev *ppe_dev;
struct airoha_npu *npu;
int err = 0;
/* NPU offloading is only supported by MT7992 */
if (!is_mt7992(dev))
return 0;
mutex_lock(&dev->mutex);
npu = airoha_npu_get(dev->dev);
if (IS_ERR(npu)) {
request_module("airoha-npu");
npu = airoha_npu_get(dev->dev);
}
if (IS_ERR(npu)) {
err = PTR_ERR(npu);
goto error_unlock;
}
ppe_dev = airoha_ppe_get_dev(dev->dev);
if (IS_ERR(ppe_dev)) {
request_module("airoha-eth");
ppe_dev = airoha_ppe_get_dev(dev->dev);
}
if (IS_ERR(ppe_dev)) {
err = PTR_ERR(ppe_dev);
goto error_npu_put;
}
err = airoha_npu_wlan_init_reserved_memory(npu);
if (err)
goto error_ppe_put;
dev->dma_dev = npu->dev;
dev->mmio.phy_addr = phy_addr;
dev->mmio.npu_type = type;
/* NPU offloading requires HW-RRO for RX packet reordering. */
dev->hwrro_mode = MT76_HWRRO_V3_1;
rcu_assign_pointer(dev->mmio.npu, npu);
rcu_assign_pointer(dev->mmio.ppe_dev, ppe_dev);
synchronize_rcu();
mutex_unlock(&dev->mutex);
return 0;
error_ppe_put:
airoha_ppe_put_dev(ppe_dev);
error_npu_put:
airoha_npu_put(npu);
error_unlock:
mutex_unlock(&dev->mutex);
return err;
}
EXPORT_SYMBOL_GPL(mt76_npu_init);
void mt76_npu_deinit(struct mt76_dev *dev)
{
struct airoha_ppe_dev *ppe_dev;
struct airoha_npu *npu;
mutex_lock(&dev->mutex);
npu = rcu_replace_pointer(dev->mmio.npu, NULL,
lockdep_is_held(&dev->mutex));
if (npu)
airoha_npu_put(npu);
ppe_dev = rcu_replace_pointer(dev->mmio.ppe_dev, NULL,
lockdep_is_held(&dev->mutex));
if (ppe_dev)
airoha_ppe_put_dev(ppe_dev);
mutex_unlock(&dev->mutex);
mt76_npu_queue_cleanup(dev, &dev->q_rx[MT_RXQ_NPU0]);
mt76_npu_queue_cleanup(dev, &dev->q_rx[MT_RXQ_NPU1]);
}
|