1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Ryder Lee <ryder.lee@mediatek.com> 5 * Roy Luo <royluo@google.com> 6 * Lorenzo Bianconi <lorenzo@kernel.org> 7 * Felix Fietkau <nbd@nbd.name> 8 */ 9 10 #include "mt7615.h" 11 #include "../dma.h" 12 #include "mac.h" 13 14 static int 15 mt7615_init_tx_queue(struct mt7615_dev *dev, struct mt76_sw_queue *q, 16 int idx, int n_desc) 17 { 18 struct mt76_queue *hwq; 19 int err; 20 21 hwq = devm_kzalloc(dev->mt76.dev, sizeof(*hwq), GFP_KERNEL); 22 if (!hwq) 23 return -ENOMEM; 24 25 err = mt76_queue_alloc(dev, hwq, idx, n_desc, 0, MT_TX_RING_BASE); 26 if (err < 0) 27 return err; 28 29 INIT_LIST_HEAD(&q->swq); 30 q->q = hwq; 31 32 return 0; 33 } 34 35 static int 36 mt7622_init_tx_queues_multi(struct mt7615_dev *dev) 37 { 38 static const u8 wmm_queue_map[] = { 39 MT7622_TXQ_AC0, 40 MT7622_TXQ_AC1, 41 MT7622_TXQ_AC2, 42 MT7622_TXQ_AC3, 43 }; 44 int ret; 45 int i; 46 47 for (i = 0; i < ARRAY_SIZE(wmm_queue_map); i++) { 48 ret = mt7615_init_tx_queue(dev, &dev->mt76.q_tx[i], 49 wmm_queue_map[i], 50 MT7615_TX_RING_SIZE / 2); 51 if (ret) 52 return ret; 53 } 54 55 ret = mt7615_init_tx_queue(dev, &dev->mt76.q_tx[MT_TXQ_PSD], 56 MT7622_TXQ_MGMT, MT7615_TX_MGMT_RING_SIZE); 57 if (ret) 58 return ret; 59 60 ret = mt7615_init_tx_queue(dev, &dev->mt76.q_tx[MT_TXQ_MCU], 61 MT7622_TXQ_MCU, MT7615_TX_MCU_RING_SIZE); 62 return ret; 63 } 64 65 static int 66 mt7615_init_tx_queues(struct mt7615_dev *dev) 67 { 68 struct mt76_sw_queue *q; 69 int ret, i; 70 71 ret = mt7615_init_tx_queue(dev, &dev->mt76.q_tx[MT_TXQ_FWDL], 72 MT7615_TXQ_FWDL, 73 MT7615_TX_FWDL_RING_SIZE); 74 if (ret) 75 return ret; 76 77 if (!is_mt7615(&dev->mt76)) 78 return mt7622_init_tx_queues_multi(dev); 79 80 ret = mt7615_init_tx_queue(dev, &dev->mt76.q_tx[0], 0, 81 MT7615_TX_RING_SIZE); 82 if (ret) 83 return ret; 84 85 for (i = 1; i < MT_TXQ_MCU; i++) { 86 q = &dev->mt76.q_tx[i]; 87 INIT_LIST_HEAD(&q->swq); 88 q->q = dev->mt76.q_tx[0].q; 89 } 90 91 ret = mt7615_init_tx_queue(dev, &dev->mt76.q_tx[MT_TXQ_MCU], 92 MT7615_TXQ_MCU, 93 MT7615_TX_MCU_RING_SIZE); 94 return 0; 95 } 96 97 static void 98 mt7615_tx_cleanup(struct mt7615_dev *dev) 99 { 100 int i; 101 102 mt76_queue_tx_cleanup(dev, MT_TXQ_MCU, false); 103 if (is_mt7615(&dev->mt76)) { 104 mt76_queue_tx_cleanup(dev, MT_TXQ_BE, false); 105 } else { 106 for (i = 0; i < IEEE80211_NUM_ACS; i++) 107 mt76_queue_tx_cleanup(dev, i, false); 108 } 109 } 110 111 static int mt7615_poll_tx(struct napi_struct *napi, int budget) 112 { 113 struct mt7615_dev *dev; 114 115 dev = container_of(napi, struct mt7615_dev, mt76.tx_napi); 116 117 mt7615_tx_cleanup(dev); 118 119 if (napi_complete_done(napi, 0)) 120 mt7615_irq_enable(dev, MT_INT_TX_DONE_ALL); 121 122 mt7615_tx_cleanup(dev); 123 124 rcu_read_lock(); 125 mt7615_mac_sta_poll(dev); 126 rcu_read_unlock(); 127 128 tasklet_schedule(&dev->mt76.tx_tasklet); 129 130 return 0; 131 } 132 133 int mt7615_wait_pdma_busy(struct mt7615_dev *dev) 134 { 135 struct mt76_dev *mdev = &dev->mt76; 136 137 if (!is_mt7663(mdev)) { 138 u32 mask = MT_PDMA_TX_BUSY | MT_PDMA_RX_BUSY; 139 u32 reg = mt7615_reg_map(dev, MT_PDMA_BUSY); 140 141 if (!mt76_poll_msec(dev, reg, mask, 0, 1000)) { 142 dev_err(mdev->dev, "PDMA engine busy\n"); 143 return -EIO; 144 } 145 146 return 0; 147 } 148 149 if (!mt76_poll_msec(dev, MT_PDMA_BUSY_STATUS, 150 MT_PDMA_TX_IDX_BUSY, 0, 1000)) { 151 dev_err(mdev->dev, "PDMA engine tx busy\n"); 152 return -EIO; 153 } 154 155 if (!mt76_poll_msec(dev, MT_PSE_PG_INFO, 156 MT_PSE_SRC_CNT, 0, 1000)) { 157 dev_err(mdev->dev, "PSE engine busy\n"); 158 return -EIO; 159 } 160 161 if (!mt76_poll_msec(dev, MT_PDMA_BUSY_STATUS, 162 MT_PDMA_BUSY_IDX, 0, 1000)) { 163 dev_err(mdev->dev, "PDMA engine busy\n"); 164 return -EIO; 165 } 166 167 return 0; 168 } 169 170 static void mt7622_dma_sched_init(struct mt7615_dev *dev) 171 { 172 u32 reg = mt7615_reg_map(dev, MT_DMASHDL_BASE); 173 int i; 174 175 mt76_rmw(dev, reg + MT_DMASHDL_PKT_MAX_SIZE, 176 MT_DMASHDL_PKT_MAX_SIZE_PLE | MT_DMASHDL_PKT_MAX_SIZE_PSE, 177 FIELD_PREP(MT_DMASHDL_PKT_MAX_SIZE_PLE, 1) | 178 FIELD_PREP(MT_DMASHDL_PKT_MAX_SIZE_PSE, 8)); 179 180 for (i = 0; i <= 5; i++) 181 mt76_wr(dev, reg + MT_DMASHDL_GROUP_QUOTA(i), 182 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MIN, 0x10) | 183 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MAX, 0x800)); 184 185 mt76_wr(dev, reg + MT_DMASHDL_Q_MAP(0), 0x42104210); 186 mt76_wr(dev, reg + MT_DMASHDL_Q_MAP(1), 0x42104210); 187 mt76_wr(dev, reg + MT_DMASHDL_Q_MAP(2), 0x5); 188 mt76_wr(dev, reg + MT_DMASHDL_Q_MAP(3), 0); 189 190 mt76_wr(dev, reg + MT_DMASHDL_SCHED_SET0, 0x6012345f); 191 mt76_wr(dev, reg + MT_DMASHDL_SCHED_SET1, 0xedcba987); 192 } 193 194 static void mt7663_dma_sched_init(struct mt7615_dev *dev) 195 { 196 int i; 197 198 mt76_rmw(dev, MT_DMA_SHDL(MT_DMASHDL_PKT_MAX_SIZE), 199 MT_DMASHDL_PKT_MAX_SIZE_PLE | MT_DMASHDL_PKT_MAX_SIZE_PSE, 200 FIELD_PREP(MT_DMASHDL_PKT_MAX_SIZE_PLE, 1) | 201 FIELD_PREP(MT_DMASHDL_PKT_MAX_SIZE_PSE, 8)); 202 203 /* enable refill control group 0, 1, 2, 4, 5 */ 204 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_REFILL), 0xffc80000); 205 /* enable group 0, 1, 2, 4, 5, 15 */ 206 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_OPTIONAL), 0x70068037); 207 208 /* each group min quota must larger then PLE_PKT_MAX_SIZE_NUM */ 209 for (i = 0; i < 5; i++) 210 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_GROUP_QUOTA(i)), 211 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MIN, 0x40) | 212 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MAX, 0x800)); 213 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_GROUP_QUOTA(5)), 214 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MIN, 0x40) | 215 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MAX, 0x40)); 216 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_GROUP_QUOTA(15)), 217 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MIN, 0x20) | 218 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MAX, 0x20)); 219 220 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_Q_MAP(0)), 0x42104210); 221 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_Q_MAP(1)), 0x42104210); 222 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_Q_MAP(2)), 0x00050005); 223 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_Q_MAP(3)), 0); 224 /* ALTX0 and ALTX1 QID mapping to group 5 */ 225 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_SCHED_SET0), 0x6012345f); 226 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_SCHED_SET1), 0xedcba987); 227 } 228 229 int mt7615_dma_init(struct mt7615_dev *dev) 230 { 231 int rx_ring_size = MT7615_RX_RING_SIZE; 232 int rx_buf_size = MT_RX_BUF_SIZE; 233 int ret; 234 235 /* Increase buffer size to receive large VHT MPDUs */ 236 if (dev->mt76.cap.has_5ghz) 237 rx_buf_size *= 2; 238 239 mt76_dma_attach(&dev->mt76); 240 241 mt76_wr(dev, MT_WPDMA_GLO_CFG, 242 MT_WPDMA_GLO_CFG_TX_WRITEBACK_DONE | 243 MT_WPDMA_GLO_CFG_FIFO_LITTLE_ENDIAN | 244 MT_WPDMA_GLO_CFG_OMIT_TX_INFO); 245 246 mt76_rmw_field(dev, MT_WPDMA_GLO_CFG, 247 MT_WPDMA_GLO_CFG_TX_BT_SIZE_BIT0, 0x1); 248 249 mt76_rmw_field(dev, MT_WPDMA_GLO_CFG, 250 MT_WPDMA_GLO_CFG_TX_BT_SIZE_BIT21, 0x1); 251 252 mt76_rmw_field(dev, MT_WPDMA_GLO_CFG, 253 MT_WPDMA_GLO_CFG_DMA_BURST_SIZE, 0x3); 254 255 mt76_rmw_field(dev, MT_WPDMA_GLO_CFG, 256 MT_WPDMA_GLO_CFG_MULTI_DMA_EN, 0x3); 257 258 if (is_mt7615(&dev->mt76)) { 259 mt76_set(dev, MT_WPDMA_GLO_CFG, 260 MT_WPDMA_GLO_CFG_FIRST_TOKEN_ONLY); 261 262 mt76_wr(dev, MT_WPDMA_GLO_CFG1, 0x1); 263 mt76_wr(dev, MT_WPDMA_TX_PRE_CFG, 0xf0000); 264 mt76_wr(dev, MT_WPDMA_RX_PRE_CFG, 0xf7f0000); 265 mt76_wr(dev, MT_WPDMA_ABT_CFG, 0x4000026); 266 mt76_wr(dev, MT_WPDMA_ABT_CFG1, 0x18811881); 267 mt76_set(dev, 0x7158, BIT(16)); 268 mt76_clear(dev, 0x7000, BIT(23)); 269 } 270 271 mt76_wr(dev, MT_WPDMA_RST_IDX, ~0); 272 273 ret = mt7615_init_tx_queues(dev); 274 if (ret) 275 return ret; 276 277 /* init rx queues */ 278 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MCU], 1, 279 MT7615_RX_MCU_RING_SIZE, rx_buf_size, 280 MT_RX_RING_BASE); 281 if (ret) 282 return ret; 283 284 if (!is_mt7615(&dev->mt76)) 285 rx_ring_size /= 2; 286 287 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MAIN], 0, 288 rx_ring_size, rx_buf_size, MT_RX_RING_BASE); 289 if (ret) 290 return ret; 291 292 mt76_wr(dev, MT_DELAY_INT_CFG, 0); 293 294 ret = mt76_init_queues(dev); 295 if (ret < 0) 296 return ret; 297 298 netif_tx_napi_add(&dev->mt76.napi_dev, &dev->mt76.tx_napi, 299 mt7615_poll_tx, NAPI_POLL_WEIGHT); 300 napi_enable(&dev->mt76.tx_napi); 301 302 mt76_poll(dev, MT_WPDMA_GLO_CFG, 303 MT_WPDMA_GLO_CFG_TX_DMA_BUSY | 304 MT_WPDMA_GLO_CFG_RX_DMA_BUSY, 0, 1000); 305 306 /* start dma engine */ 307 mt76_set(dev, MT_WPDMA_GLO_CFG, 308 MT_WPDMA_GLO_CFG_TX_DMA_EN | 309 MT_WPDMA_GLO_CFG_RX_DMA_EN); 310 311 /* enable interrupts for TX/RX rings */ 312 mt7615_irq_enable(dev, MT_INT_RX_DONE_ALL | MT_INT_TX_DONE_ALL | 313 MT_INT_MCU_CMD); 314 315 if (is_mt7622(&dev->mt76)) 316 mt7622_dma_sched_init(dev); 317 318 if (is_mt7663(&dev->mt76)) 319 mt7663_dma_sched_init(dev); 320 321 return 0; 322 } 323 324 void mt7615_dma_cleanup(struct mt7615_dev *dev) 325 { 326 mt76_clear(dev, MT_WPDMA_GLO_CFG, 327 MT_WPDMA_GLO_CFG_TX_DMA_EN | 328 MT_WPDMA_GLO_CFG_RX_DMA_EN); 329 mt76_set(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_SW_RESET); 330 331 tasklet_kill(&dev->mt76.tx_tasklet); 332 mt76_dma_cleanup(&dev->mt76); 333 } 334