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 [IEEE80211_AC_BK] = MT7622_TXQ_AC0, 40 [IEEE80211_AC_BE] = MT7622_TXQ_AC1, 41 [IEEE80211_AC_VI] = MT7622_TXQ_AC2, 42 [IEEE80211_AC_VO] = 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 mt76_queue_tx_cleanup(dev, MT_TXQ_PSD, false); 104 if (is_mt7615(&dev->mt76)) { 105 mt76_queue_tx_cleanup(dev, MT_TXQ_BE, false); 106 } else { 107 for (i = 0; i < IEEE80211_NUM_ACS; i++) 108 mt76_queue_tx_cleanup(dev, i, false); 109 } 110 } 111 112 static int mt7615_poll_tx(struct napi_struct *napi, int budget) 113 { 114 struct mt7615_dev *dev; 115 116 dev = container_of(napi, struct mt7615_dev, mt76.tx_napi); 117 118 mt7615_tx_cleanup(dev); 119 120 if (napi_complete_done(napi, 0)) 121 mt7615_irq_enable(dev, MT_INT_TX_DONE_ALL); 122 123 mt7615_tx_cleanup(dev); 124 125 tasklet_schedule(&dev->mt76.tx_tasklet); 126 127 return 0; 128 } 129 130 int mt7615_wait_pdma_busy(struct mt7615_dev *dev) 131 { 132 struct mt76_dev *mdev = &dev->mt76; 133 134 if (!is_mt7663(mdev)) { 135 u32 mask = MT_PDMA_TX_BUSY | MT_PDMA_RX_BUSY; 136 u32 reg = mt7615_reg_map(dev, MT_PDMA_BUSY); 137 138 if (!mt76_poll_msec(dev, reg, mask, 0, 1000)) { 139 dev_err(mdev->dev, "PDMA engine busy\n"); 140 return -EIO; 141 } 142 143 return 0; 144 } 145 146 if (!mt76_poll_msec(dev, MT_PDMA_BUSY_STATUS, 147 MT_PDMA_TX_IDX_BUSY, 0, 1000)) { 148 dev_err(mdev->dev, "PDMA engine tx busy\n"); 149 return -EIO; 150 } 151 152 if (!mt76_poll_msec(dev, MT_PSE_PG_INFO, 153 MT_PSE_SRC_CNT, 0, 1000)) { 154 dev_err(mdev->dev, "PSE engine busy\n"); 155 return -EIO; 156 } 157 158 if (!mt76_poll_msec(dev, MT_PDMA_BUSY_STATUS, 159 MT_PDMA_BUSY_IDX, 0, 1000)) { 160 dev_err(mdev->dev, "PDMA engine busy\n"); 161 return -EIO; 162 } 163 164 return 0; 165 } 166 167 static void mt7622_dma_sched_init(struct mt7615_dev *dev) 168 { 169 u32 reg = mt7615_reg_map(dev, MT_DMASHDL_BASE); 170 int i; 171 172 mt76_rmw(dev, reg + MT_DMASHDL_PKT_MAX_SIZE, 173 MT_DMASHDL_PKT_MAX_SIZE_PLE | MT_DMASHDL_PKT_MAX_SIZE_PSE, 174 FIELD_PREP(MT_DMASHDL_PKT_MAX_SIZE_PLE, 1) | 175 FIELD_PREP(MT_DMASHDL_PKT_MAX_SIZE_PSE, 8)); 176 177 for (i = 0; i <= 5; i++) 178 mt76_wr(dev, reg + MT_DMASHDL_GROUP_QUOTA(i), 179 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MIN, 0x10) | 180 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MAX, 0x800)); 181 182 mt76_wr(dev, reg + MT_DMASHDL_Q_MAP(0), 0x42104210); 183 mt76_wr(dev, reg + MT_DMASHDL_Q_MAP(1), 0x42104210); 184 mt76_wr(dev, reg + MT_DMASHDL_Q_MAP(2), 0x5); 185 mt76_wr(dev, reg + MT_DMASHDL_Q_MAP(3), 0); 186 187 mt76_wr(dev, reg + MT_DMASHDL_SCHED_SET0, 0x6012345f); 188 mt76_wr(dev, reg + MT_DMASHDL_SCHED_SET1, 0xedcba987); 189 } 190 191 static void mt7663_dma_sched_init(struct mt7615_dev *dev) 192 { 193 int i; 194 195 mt76_rmw(dev, MT_DMA_SHDL(MT_DMASHDL_PKT_MAX_SIZE), 196 MT_DMASHDL_PKT_MAX_SIZE_PLE | MT_DMASHDL_PKT_MAX_SIZE_PSE, 197 FIELD_PREP(MT_DMASHDL_PKT_MAX_SIZE_PLE, 1) | 198 FIELD_PREP(MT_DMASHDL_PKT_MAX_SIZE_PSE, 8)); 199 200 /* enable refill control group 0, 1, 2, 4, 5 */ 201 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_REFILL), 0xffc80000); 202 /* enable group 0, 1, 2, 4, 5, 15 */ 203 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_OPTIONAL), 0x70068037); 204 205 /* each group min quota must larger then PLE_PKT_MAX_SIZE_NUM */ 206 for (i = 0; i < 5; i++) 207 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_GROUP_QUOTA(i)), 208 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MIN, 0x40) | 209 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MAX, 0x800)); 210 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_GROUP_QUOTA(5)), 211 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MIN, 0x40) | 212 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MAX, 0x40)); 213 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_GROUP_QUOTA(15)), 214 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MIN, 0x20) | 215 FIELD_PREP(MT_DMASHDL_GROUP_QUOTA_MAX, 0x20)); 216 217 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_Q_MAP(0)), 0x42104210); 218 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_Q_MAP(1)), 0x42104210); 219 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_Q_MAP(2)), 0x00050005); 220 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_Q_MAP(3)), 0); 221 /* ALTX0 and ALTX1 QID mapping to group 5 */ 222 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_SCHED_SET0), 0x6012345f); 223 mt76_wr(dev, MT_DMA_SHDL(MT_DMASHDL_SCHED_SET1), 0xedcba987); 224 } 225 226 int mt7615_dma_init(struct mt7615_dev *dev) 227 { 228 int rx_ring_size = MT7615_RX_RING_SIZE; 229 int rx_buf_size = MT_RX_BUF_SIZE; 230 int ret; 231 232 /* Increase buffer size to receive large VHT MPDUs */ 233 if (dev->mt76.cap.has_5ghz) 234 rx_buf_size *= 2; 235 236 mt76_dma_attach(&dev->mt76); 237 238 mt76_wr(dev, MT_WPDMA_GLO_CFG, 239 MT_WPDMA_GLO_CFG_TX_WRITEBACK_DONE | 240 MT_WPDMA_GLO_CFG_FIFO_LITTLE_ENDIAN | 241 MT_WPDMA_GLO_CFG_OMIT_TX_INFO); 242 243 mt76_rmw_field(dev, MT_WPDMA_GLO_CFG, 244 MT_WPDMA_GLO_CFG_TX_BT_SIZE_BIT0, 0x1); 245 246 mt76_rmw_field(dev, MT_WPDMA_GLO_CFG, 247 MT_WPDMA_GLO_CFG_TX_BT_SIZE_BIT21, 0x1); 248 249 mt76_rmw_field(dev, MT_WPDMA_GLO_CFG, 250 MT_WPDMA_GLO_CFG_DMA_BURST_SIZE, 0x3); 251 252 mt76_rmw_field(dev, MT_WPDMA_GLO_CFG, 253 MT_WPDMA_GLO_CFG_MULTI_DMA_EN, 0x3); 254 255 if (is_mt7615(&dev->mt76)) { 256 mt76_set(dev, MT_WPDMA_GLO_CFG, 257 MT_WPDMA_GLO_CFG_FIRST_TOKEN_ONLY); 258 259 mt76_wr(dev, MT_WPDMA_GLO_CFG1, 0x1); 260 mt76_wr(dev, MT_WPDMA_TX_PRE_CFG, 0xf0000); 261 mt76_wr(dev, MT_WPDMA_RX_PRE_CFG, 0xf7f0000); 262 mt76_wr(dev, MT_WPDMA_ABT_CFG, 0x4000026); 263 mt76_wr(dev, MT_WPDMA_ABT_CFG1, 0x18811881); 264 mt76_set(dev, 0x7158, BIT(16)); 265 mt76_clear(dev, 0x7000, BIT(23)); 266 } 267 268 mt76_wr(dev, MT_WPDMA_RST_IDX, ~0); 269 270 ret = mt7615_init_tx_queues(dev); 271 if (ret) 272 return ret; 273 274 /* init rx queues */ 275 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MCU], 1, 276 MT7615_RX_MCU_RING_SIZE, rx_buf_size, 277 MT_RX_RING_BASE); 278 if (ret) 279 return ret; 280 281 if (!is_mt7615(&dev->mt76)) 282 rx_ring_size /= 2; 283 284 ret = mt76_queue_alloc(dev, &dev->mt76.q_rx[MT_RXQ_MAIN], 0, 285 rx_ring_size, rx_buf_size, MT_RX_RING_BASE); 286 if (ret) 287 return ret; 288 289 mt76_wr(dev, MT_DELAY_INT_CFG, 0); 290 291 ret = mt76_init_queues(dev); 292 if (ret < 0) 293 return ret; 294 295 netif_tx_napi_add(&dev->mt76.napi_dev, &dev->mt76.tx_napi, 296 mt7615_poll_tx, NAPI_POLL_WEIGHT); 297 napi_enable(&dev->mt76.tx_napi); 298 299 mt76_poll(dev, MT_WPDMA_GLO_CFG, 300 MT_WPDMA_GLO_CFG_TX_DMA_BUSY | 301 MT_WPDMA_GLO_CFG_RX_DMA_BUSY, 0, 1000); 302 303 /* start dma engine */ 304 mt76_set(dev, MT_WPDMA_GLO_CFG, 305 MT_WPDMA_GLO_CFG_TX_DMA_EN | 306 MT_WPDMA_GLO_CFG_RX_DMA_EN); 307 308 /* enable interrupts for TX/RX rings */ 309 mt7615_irq_enable(dev, MT_INT_RX_DONE_ALL | MT_INT_TX_DONE_ALL | 310 MT_INT_MCU_CMD); 311 312 if (is_mt7622(&dev->mt76)) 313 mt7622_dma_sched_init(dev); 314 315 if (is_mt7663(&dev->mt76)) 316 mt7663_dma_sched_init(dev); 317 318 return 0; 319 } 320 321 void mt7615_dma_cleanup(struct mt7615_dev *dev) 322 { 323 mt76_clear(dev, MT_WPDMA_GLO_CFG, 324 MT_WPDMA_GLO_CFG_TX_DMA_EN | 325 MT_WPDMA_GLO_CFG_RX_DMA_EN); 326 mt76_set(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_SW_RESET); 327 328 tasklet_kill(&dev->mt76.tx_tasklet); 329 mt76_dma_cleanup(&dev->mt76); 330 } 331