]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/wireless/mediatek/mt76/mt76x2_dma.c
879ed91388419e44d57ffc49a1b9d527b67323c1
[linux.git] / drivers / net / wireless / mediatek / mt76 / mt76x2_dma.c
1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include "mt76x2.h"
18 #include "mt76x02_dma.h"
19
20 static int
21 mt76x2_init_tx_queue(struct mt76x2_dev *dev, struct mt76_queue *q,
22                      int idx, int n_desc)
23 {
24         int ret;
25
26         q->regs = dev->mt76.mmio.regs + MT_TX_RING_BASE + idx * MT_RING_SIZE;
27         q->ndesc = n_desc;
28         q->hw_idx = idx;
29
30         ret = mt76_queue_alloc(dev, q);
31         if (ret)
32                 return ret;
33
34         mt76x2_irq_enable(dev, MT_INT_TX_DONE(idx));
35
36         return 0;
37 }
38
39 static int
40 mt76x2_init_rx_queue(struct mt76x2_dev *dev, struct mt76_queue *q,
41                      int idx, int n_desc, int bufsize)
42 {
43         int ret;
44
45         q->regs = dev->mt76.mmio.regs + MT_RX_RING_BASE + idx * MT_RING_SIZE;
46         q->ndesc = n_desc;
47         q->buf_size = bufsize;
48
49         ret = mt76_queue_alloc(dev, q);
50         if (ret)
51                 return ret;
52
53         mt76x2_irq_enable(dev, MT_INT_RX_DONE(idx));
54
55         return 0;
56 }
57
58 static void
59 mt76x2_tx_tasklet(unsigned long data)
60 {
61         struct mt76x2_dev *dev = (struct mt76x2_dev *) data;
62         int i;
63
64         mt76x2_mac_process_tx_status_fifo(dev);
65
66         for (i = MT_TXQ_MCU; i >= 0; i--)
67                 mt76_queue_tx_cleanup(dev, i, false);
68
69         mt76x2_mac_poll_tx_status(dev, false);
70         mt76x2_irq_enable(dev, MT_INT_TX_DONE_ALL);
71 }
72
73 int mt76x2_dma_init(struct mt76x2_dev *dev)
74 {
75         int ret;
76         int i;
77         struct mt76_txwi_cache __maybe_unused *t;
78         struct mt76_queue *q;
79
80         BUILD_BUG_ON(sizeof(t->txwi) < sizeof(struct mt76x02_txwi));
81         BUILD_BUG_ON(sizeof(struct mt76x02_rxwi) > MT_RX_HEADROOM);
82
83         mt76_dma_attach(&dev->mt76);
84
85         tasklet_init(&dev->tx_tasklet, mt76x2_tx_tasklet, (unsigned long) dev);
86
87         mt76_wr(dev, MT_WPDMA_RST_IDX, ~0);
88
89         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
90                 ret = mt76x2_init_tx_queue(dev, &dev->mt76.q_tx[i],
91                                            mt76_ac_to_hwq(i), MT_TX_RING_SIZE);
92                 if (ret)
93                         return ret;
94         }
95
96         ret = mt76x2_init_tx_queue(dev, &dev->mt76.q_tx[MT_TXQ_PSD],
97                                    MT_TX_HW_QUEUE_MGMT, MT_TX_RING_SIZE);
98         if (ret)
99                 return ret;
100
101         ret = mt76x2_init_tx_queue(dev, &dev->mt76.q_tx[MT_TXQ_MCU],
102                                    MT_TX_HW_QUEUE_MCU, MT_MCU_RING_SIZE);
103         if (ret)
104                 return ret;
105
106         ret = mt76x2_init_rx_queue(dev, &dev->mt76.q_rx[MT_RXQ_MCU], 1,
107                                    MT_MCU_RING_SIZE, MT_RX_BUF_SIZE);
108         if (ret)
109                 return ret;
110
111         q = &dev->mt76.q_rx[MT_RXQ_MAIN];
112         q->buf_offset = MT_RX_HEADROOM - sizeof(struct mt76x02_rxwi);
113         ret = mt76x2_init_rx_queue(dev, q, 0, MT76x2_RX_RING_SIZE, MT_RX_BUF_SIZE);
114         if (ret)
115                 return ret;
116
117         return mt76_init_queues(dev);
118 }
119
120 void mt76x2_dma_cleanup(struct mt76x2_dev *dev)
121 {
122         tasklet_kill(&dev->tx_tasklet);
123         mt76_dma_cleanup(&dev->mt76);
124 }