developer | b11a539 | 2022-03-31 00:34:47 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: ISC |
| 2 | /* |
| 3 | * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/dma-mapping.h> |
| 7 | #include "mt76.h" |
| 8 | #include "dma.h" |
| 9 | |
| 10 | static struct mt76_txwi_cache * |
| 11 | mt76_alloc_txwi(struct mt76_dev *dev) |
| 12 | { |
| 13 | struct mt76_txwi_cache *t; |
| 14 | dma_addr_t addr; |
| 15 | u8 *txwi; |
| 16 | int size; |
| 17 | |
| 18 | size = L1_CACHE_ALIGN(dev->drv->txwi_size + sizeof(*t)); |
| 19 | txwi = kzalloc(size, GFP_ATOMIC); |
| 20 | if (!txwi) |
| 21 | return NULL; |
| 22 | |
| 23 | addr = dma_map_single(dev->dev, txwi, dev->drv->txwi_size, |
| 24 | DMA_TO_DEVICE); |
| 25 | t = (struct mt76_txwi_cache *)(txwi + dev->drv->txwi_size); |
| 26 | t->dma_addr = addr; |
| 27 | |
| 28 | return t; |
| 29 | } |
| 30 | |
| 31 | static struct mt76_txwi_cache * |
| 32 | __mt76_get_txwi(struct mt76_dev *dev) |
| 33 | { |
| 34 | struct mt76_txwi_cache *t = NULL; |
| 35 | |
| 36 | spin_lock(&dev->lock); |
| 37 | if (!list_empty(&dev->txwi_cache)) { |
| 38 | t = list_first_entry(&dev->txwi_cache, struct mt76_txwi_cache, |
| 39 | list); |
| 40 | list_del(&t->list); |
| 41 | } |
| 42 | spin_unlock(&dev->lock); |
| 43 | |
| 44 | return t; |
| 45 | } |
| 46 | |
| 47 | static struct mt76_txwi_cache * |
| 48 | mt76_get_txwi(struct mt76_dev *dev) |
| 49 | { |
| 50 | struct mt76_txwi_cache *t = __mt76_get_txwi(dev); |
| 51 | |
| 52 | if (t) |
| 53 | return t; |
| 54 | |
| 55 | return mt76_alloc_txwi(dev); |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | mt76_put_txwi(struct mt76_dev *dev, struct mt76_txwi_cache *t) |
| 60 | { |
| 61 | if (!t) |
| 62 | return; |
| 63 | |
| 64 | spin_lock(&dev->lock); |
| 65 | list_add(&t->list, &dev->txwi_cache); |
| 66 | spin_unlock(&dev->lock); |
| 67 | } |
| 68 | EXPORT_SYMBOL_GPL(mt76_put_txwi); |
| 69 | |
| 70 | static void |
| 71 | mt76_free_pending_txwi(struct mt76_dev *dev) |
| 72 | { |
| 73 | struct mt76_txwi_cache *t; |
| 74 | |
| 75 | local_bh_disable(); |
| 76 | while ((t = __mt76_get_txwi(dev)) != NULL) { |
| 77 | dma_unmap_single(dev->dev, t->dma_addr, dev->drv->txwi_size, |
| 78 | DMA_TO_DEVICE); |
| 79 | kfree(mt76_get_txwi_ptr(dev, t)); |
| 80 | } |
| 81 | local_bh_enable(); |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | mt76_dma_sync_idx(struct mt76_dev *dev, struct mt76_queue *q) |
| 86 | { |
| 87 | writel(q->desc_dma, &q->regs->desc_base); |
| 88 | writel(q->ndesc, &q->regs->ring_size); |
| 89 | q->head = readl(&q->regs->dma_idx); |
| 90 | q->tail = q->head; |
| 91 | } |
| 92 | |
| 93 | static void |
| 94 | mt76_dma_queue_reset(struct mt76_dev *dev, struct mt76_queue *q) |
| 95 | { |
| 96 | int i; |
| 97 | |
| 98 | if (!q || !q->ndesc) |
| 99 | return; |
| 100 | |
| 101 | /* clear descriptors */ |
| 102 | for (i = 0; i < q->ndesc; i++) |
| 103 | q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE); |
| 104 | |
| 105 | writel(0, &q->regs->cpu_idx); |
| 106 | writel(0, &q->regs->dma_idx); |
| 107 | mt76_dma_sync_idx(dev, q); |
| 108 | } |
| 109 | |
| 110 | static int |
| 111 | mt76_dma_alloc_queue(struct mt76_dev *dev, struct mt76_queue *q, |
| 112 | int idx, int n_desc, int bufsize, |
| 113 | u32 ring_base) |
| 114 | { |
| 115 | int size; |
| 116 | |
| 117 | spin_lock_init(&q->lock); |
| 118 | spin_lock_init(&q->cleanup_lock); |
| 119 | |
| 120 | q->regs = dev->mmio.regs + ring_base + idx * MT_RING_SIZE; |
| 121 | q->ndesc = n_desc; |
| 122 | q->buf_size = bufsize; |
| 123 | q->hw_idx = idx; |
| 124 | |
| 125 | size = q->ndesc * sizeof(struct mt76_desc); |
| 126 | q->desc = dmam_alloc_coherent(dev->dev, size, &q->desc_dma, GFP_KERNEL); |
| 127 | if (!q->desc) |
| 128 | return -ENOMEM; |
| 129 | |
| 130 | size = q->ndesc * sizeof(*q->entry); |
| 131 | q->entry = devm_kzalloc(dev->dev, size, GFP_KERNEL); |
| 132 | if (!q->entry) |
| 133 | return -ENOMEM; |
| 134 | |
| 135 | mt76_dma_queue_reset(dev, q); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int |
| 141 | mt76_dma_add_buf(struct mt76_dev *dev, struct mt76_queue *q, |
| 142 | struct mt76_queue_buf *buf, int nbufs, u32 info, |
| 143 | struct sk_buff *skb, void *txwi) |
| 144 | { |
| 145 | struct mt76_queue_entry *entry; |
| 146 | struct mt76_desc *desc; |
| 147 | u32 ctrl; |
| 148 | int i, idx = -1; |
| 149 | |
| 150 | if (txwi) { |
| 151 | q->entry[q->head].txwi = DMA_DUMMY_DATA; |
| 152 | q->entry[q->head].skip_buf0 = true; |
| 153 | } |
| 154 | |
| 155 | for (i = 0; i < nbufs; i += 2, buf += 2) { |
| 156 | u32 buf0 = buf[0].addr, buf1 = 0; |
| 157 | |
| 158 | idx = q->head; |
| 159 | q->head = (q->head + 1) % q->ndesc; |
| 160 | |
| 161 | desc = &q->desc[idx]; |
| 162 | entry = &q->entry[idx]; |
| 163 | |
| 164 | if (buf[0].skip_unmap) |
| 165 | entry->skip_buf0 = true; |
| 166 | entry->skip_buf1 = i == nbufs - 1; |
| 167 | |
| 168 | entry->dma_addr[0] = buf[0].addr; |
| 169 | entry->dma_len[0] = buf[0].len; |
| 170 | |
| 171 | ctrl = FIELD_PREP(MT_DMA_CTL_SD_LEN0, buf[0].len); |
| 172 | if (i < nbufs - 1) { |
| 173 | entry->dma_addr[1] = buf[1].addr; |
| 174 | entry->dma_len[1] = buf[1].len; |
| 175 | buf1 = buf[1].addr; |
| 176 | ctrl |= FIELD_PREP(MT_DMA_CTL_SD_LEN1, buf[1].len); |
| 177 | if (buf[1].skip_unmap) |
| 178 | entry->skip_buf1 = true; |
| 179 | } |
| 180 | |
| 181 | if (i == nbufs - 1) |
| 182 | ctrl |= MT_DMA_CTL_LAST_SEC0; |
| 183 | else if (i == nbufs - 2) |
| 184 | ctrl |= MT_DMA_CTL_LAST_SEC1; |
| 185 | |
| 186 | WRITE_ONCE(desc->buf0, cpu_to_le32(buf0)); |
| 187 | WRITE_ONCE(desc->buf1, cpu_to_le32(buf1)); |
| 188 | WRITE_ONCE(desc->info, cpu_to_le32(info)); |
| 189 | WRITE_ONCE(desc->ctrl, cpu_to_le32(ctrl)); |
| 190 | |
| 191 | q->queued++; |
| 192 | } |
| 193 | |
| 194 | q->entry[idx].txwi = txwi; |
| 195 | q->entry[idx].skb = skb; |
| 196 | q->entry[idx].wcid = 0xffff; |
| 197 | |
| 198 | return idx; |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | mt76_dma_tx_cleanup_idx(struct mt76_dev *dev, struct mt76_queue *q, int idx, |
| 203 | struct mt76_queue_entry *prev_e) |
| 204 | { |
| 205 | struct mt76_queue_entry *e = &q->entry[idx]; |
| 206 | |
| 207 | if (!e->skip_buf0) |
| 208 | dma_unmap_single(dev->dev, e->dma_addr[0], e->dma_len[0], |
| 209 | DMA_TO_DEVICE); |
| 210 | |
| 211 | if (!e->skip_buf1) |
| 212 | dma_unmap_single(dev->dev, e->dma_addr[1], e->dma_len[1], |
| 213 | DMA_TO_DEVICE); |
| 214 | |
| 215 | if (e->txwi == DMA_DUMMY_DATA) |
| 216 | e->txwi = NULL; |
| 217 | |
| 218 | if (e->skb == DMA_DUMMY_DATA) |
| 219 | e->skb = NULL; |
| 220 | |
| 221 | *prev_e = *e; |
| 222 | memset(e, 0, sizeof(*e)); |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | mt76_dma_kick_queue(struct mt76_dev *dev, struct mt76_queue *q) |
| 227 | { |
| 228 | wmb(); |
| 229 | writel(q->head, &q->regs->cpu_idx); |
| 230 | } |
| 231 | |
| 232 | static void |
| 233 | mt76_dma_tx_cleanup(struct mt76_dev *dev, struct mt76_queue *q, bool flush) |
| 234 | { |
| 235 | struct mt76_queue_entry entry; |
| 236 | int last; |
| 237 | |
| 238 | if (!q || !q->ndesc) |
| 239 | return; |
| 240 | |
| 241 | spin_lock_bh(&q->cleanup_lock); |
| 242 | if (flush) |
| 243 | last = -1; |
| 244 | else |
| 245 | last = readl(&q->regs->dma_idx); |
| 246 | |
| 247 | while (q->queued > 0 && q->tail != last) { |
| 248 | mt76_dma_tx_cleanup_idx(dev, q, q->tail, &entry); |
| 249 | mt76_queue_tx_complete(dev, q, &entry); |
| 250 | |
| 251 | if (entry.txwi) { |
| 252 | if (!(dev->drv->drv_flags & MT_DRV_TXWI_NO_FREE)) |
| 253 | mt76_put_txwi(dev, entry.txwi); |
| 254 | } |
| 255 | |
| 256 | if (!flush && q->tail == last) |
| 257 | last = readl(&q->regs->dma_idx); |
| 258 | |
| 259 | } |
| 260 | spin_unlock_bh(&q->cleanup_lock); |
| 261 | |
| 262 | if (flush) { |
| 263 | spin_lock_bh(&q->lock); |
| 264 | mt76_dma_sync_idx(dev, q); |
| 265 | mt76_dma_kick_queue(dev, q); |
| 266 | spin_unlock_bh(&q->lock); |
| 267 | } |
| 268 | |
| 269 | if (!q->queued) |
| 270 | wake_up(&dev->tx_wait); |
| 271 | } |
| 272 | |
| 273 | static void * |
| 274 | mt76_dma_get_buf(struct mt76_dev *dev, struct mt76_queue *q, int idx, |
| 275 | int *len, u32 *info, bool *more) |
| 276 | { |
| 277 | struct mt76_queue_entry *e = &q->entry[idx]; |
| 278 | struct mt76_desc *desc = &q->desc[idx]; |
| 279 | dma_addr_t buf_addr; |
| 280 | void *buf = e->buf; |
| 281 | int buf_len = SKB_WITH_OVERHEAD(q->buf_size); |
| 282 | |
| 283 | buf_addr = e->dma_addr[0]; |
| 284 | if (len) { |
| 285 | u32 ctl = le32_to_cpu(READ_ONCE(desc->ctrl)); |
| 286 | *len = FIELD_GET(MT_DMA_CTL_SD_LEN0, ctl); |
| 287 | *more = !(ctl & MT_DMA_CTL_LAST_SEC0); |
| 288 | } |
| 289 | |
| 290 | if (info) |
| 291 | *info = le32_to_cpu(desc->info); |
| 292 | |
| 293 | dma_unmap_single(dev->dev, buf_addr, buf_len, DMA_FROM_DEVICE); |
| 294 | e->buf = NULL; |
| 295 | |
| 296 | return buf; |
| 297 | } |
| 298 | |
| 299 | static void * |
| 300 | mt76_dma_dequeue(struct mt76_dev *dev, struct mt76_queue *q, bool flush, |
| 301 | int *len, u32 *info, bool *more) |
| 302 | { |
| 303 | int idx = q->tail; |
| 304 | |
| 305 | *more = false; |
| 306 | if (!q->queued) |
| 307 | return NULL; |
| 308 | |
| 309 | if (flush) |
| 310 | q->desc[idx].ctrl |= cpu_to_le32(MT_DMA_CTL_DMA_DONE); |
| 311 | else if (!(q->desc[idx].ctrl & cpu_to_le32(MT_DMA_CTL_DMA_DONE))) |
| 312 | return NULL; |
| 313 | |
| 314 | q->tail = (q->tail + 1) % q->ndesc; |
| 315 | q->queued--; |
| 316 | |
| 317 | return mt76_dma_get_buf(dev, q, idx, len, info, more); |
| 318 | } |
| 319 | |
| 320 | static int |
| 321 | mt76_dma_tx_queue_skb_raw(struct mt76_dev *dev, struct mt76_queue *q, |
| 322 | struct sk_buff *skb, u32 tx_info) |
| 323 | { |
| 324 | struct mt76_queue_buf buf = {}; |
| 325 | dma_addr_t addr; |
| 326 | |
| 327 | if (q->queued + 1 >= q->ndesc - 1) |
| 328 | goto error; |
| 329 | |
| 330 | addr = dma_map_single(dev->dev, skb->data, skb->len, |
| 331 | DMA_TO_DEVICE); |
| 332 | if (unlikely(dma_mapping_error(dev->dev, addr))) |
| 333 | goto error; |
| 334 | |
| 335 | buf.addr = addr; |
| 336 | buf.len = skb->len; |
| 337 | |
| 338 | spin_lock_bh(&q->lock); |
| 339 | mt76_dma_add_buf(dev, q, &buf, 1, tx_info, skb, NULL); |
| 340 | mt76_dma_kick_queue(dev, q); |
| 341 | spin_unlock_bh(&q->lock); |
| 342 | |
| 343 | return 0; |
| 344 | |
| 345 | error: |
| 346 | dev_kfree_skb(skb); |
| 347 | return -ENOMEM; |
| 348 | } |
| 349 | |
| 350 | static int |
| 351 | mt76_dma_tx_queue_skb(struct mt76_dev *dev, struct mt76_queue *q, |
| 352 | struct sk_buff *skb, struct mt76_wcid *wcid, |
| 353 | struct ieee80211_sta *sta) |
| 354 | { |
| 355 | struct ieee80211_tx_status status = { |
| 356 | .sta = sta, |
| 357 | }; |
| 358 | struct mt76_tx_info tx_info = { |
| 359 | .skb = skb, |
| 360 | }; |
| 361 | struct ieee80211_hw *hw; |
| 362 | int len, n = 0, ret = -ENOMEM; |
| 363 | struct mt76_txwi_cache *t; |
| 364 | struct sk_buff *iter; |
| 365 | dma_addr_t addr; |
| 366 | u8 *txwi; |
| 367 | |
| 368 | t = mt76_get_txwi(dev); |
| 369 | if (!t) |
| 370 | goto free_skb; |
| 371 | |
| 372 | txwi = mt76_get_txwi_ptr(dev, t); |
| 373 | |
| 374 | skb->prev = skb->next = NULL; |
| 375 | if (dev->drv->drv_flags & MT_DRV_TX_ALIGNED4_SKBS) |
| 376 | mt76_insert_hdr_pad(skb); |
| 377 | |
| 378 | len = skb_headlen(skb); |
| 379 | addr = dma_map_single(dev->dev, skb->data, len, DMA_TO_DEVICE); |
| 380 | if (unlikely(dma_mapping_error(dev->dev, addr))) |
| 381 | goto free; |
| 382 | |
| 383 | tx_info.buf[n].addr = t->dma_addr; |
| 384 | tx_info.buf[n++].len = dev->drv->txwi_size; |
| 385 | tx_info.buf[n].addr = addr; |
| 386 | tx_info.buf[n++].len = len; |
| 387 | |
| 388 | skb_walk_frags(skb, iter) { |
| 389 | if (n == ARRAY_SIZE(tx_info.buf)) |
| 390 | goto unmap; |
| 391 | |
| 392 | addr = dma_map_single(dev->dev, iter->data, iter->len, |
| 393 | DMA_TO_DEVICE); |
| 394 | if (unlikely(dma_mapping_error(dev->dev, addr))) |
| 395 | goto unmap; |
| 396 | |
| 397 | tx_info.buf[n].addr = addr; |
| 398 | tx_info.buf[n++].len = iter->len; |
| 399 | } |
| 400 | tx_info.nbuf = n; |
| 401 | |
| 402 | if (q->queued + (tx_info.nbuf + 1) / 2 >= q->ndesc - 1) { |
| 403 | ret = -ENOMEM; |
| 404 | goto unmap; |
| 405 | } |
| 406 | |
| 407 | dma_sync_single_for_cpu(dev->dev, t->dma_addr, dev->drv->txwi_size, |
| 408 | DMA_TO_DEVICE); |
| 409 | ret = dev->drv->tx_prepare_skb(dev, txwi, q->qid, wcid, sta, &tx_info); |
| 410 | dma_sync_single_for_device(dev->dev, t->dma_addr, dev->drv->txwi_size, |
| 411 | DMA_TO_DEVICE); |
| 412 | if (ret < 0) |
| 413 | goto unmap; |
| 414 | |
| 415 | return mt76_dma_add_buf(dev, q, tx_info.buf, tx_info.nbuf, |
| 416 | tx_info.info, tx_info.skb, t); |
| 417 | |
| 418 | unmap: |
| 419 | for (n--; n > 0; n--) |
| 420 | dma_unmap_single(dev->dev, tx_info.buf[n].addr, |
| 421 | tx_info.buf[n].len, DMA_TO_DEVICE); |
| 422 | |
| 423 | free: |
| 424 | #ifdef CONFIG_NL80211_TESTMODE |
| 425 | /* fix tx_done accounting on queue overflow */ |
| 426 | if (mt76_is_testmode_skb(dev, skb, &hw)) { |
| 427 | struct mt76_phy *phy = hw->priv; |
| 428 | |
| 429 | if (tx_info.skb == phy->test.tx_skb) |
| 430 | phy->test.tx_done--; |
| 431 | } |
| 432 | #endif |
| 433 | |
| 434 | mt76_put_txwi(dev, t); |
| 435 | |
| 436 | free_skb: |
| 437 | status.skb = tx_info.skb; |
| 438 | hw = mt76_tx_status_get_hw(dev, tx_info.skb); |
| 439 | ieee80211_tx_status_ext(hw, &status); |
| 440 | |
| 441 | return ret; |
| 442 | } |
| 443 | |
| 444 | static int |
| 445 | mt76_dma_rx_fill(struct mt76_dev *dev, struct mt76_queue *q) |
| 446 | { |
| 447 | dma_addr_t addr; |
| 448 | void *buf; |
| 449 | int frames = 0; |
| 450 | int len = SKB_WITH_OVERHEAD(q->buf_size); |
| 451 | int offset = q->buf_offset; |
| 452 | |
| 453 | if (!q->ndesc) |
| 454 | return 0; |
| 455 | |
| 456 | spin_lock_bh(&q->lock); |
| 457 | |
| 458 | while (q->queued < q->ndesc - 1) { |
| 459 | struct mt76_queue_buf qbuf; |
| 460 | |
| 461 | buf = page_frag_alloc(&q->rx_page, q->buf_size, GFP_ATOMIC); |
| 462 | if (!buf) |
| 463 | break; |
| 464 | |
| 465 | addr = dma_map_single(dev->dev, buf, len, DMA_FROM_DEVICE); |
| 466 | if (unlikely(dma_mapping_error(dev->dev, addr))) { |
| 467 | skb_free_frag(buf); |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | qbuf.addr = addr + offset; |
| 472 | qbuf.len = len - offset; |
| 473 | qbuf.skip_unmap = false; |
| 474 | mt76_dma_add_buf(dev, q, &qbuf, 1, 0, buf, NULL); |
| 475 | frames++; |
| 476 | } |
| 477 | |
| 478 | if (frames) |
| 479 | mt76_dma_kick_queue(dev, q); |
| 480 | |
| 481 | spin_unlock_bh(&q->lock); |
| 482 | |
| 483 | return frames; |
| 484 | } |
| 485 | |
| 486 | static void |
| 487 | mt76_dma_rx_cleanup(struct mt76_dev *dev, struct mt76_queue *q) |
| 488 | { |
| 489 | struct page *page; |
| 490 | void *buf; |
| 491 | bool more; |
| 492 | |
| 493 | if (!q->ndesc) |
| 494 | return; |
| 495 | |
| 496 | spin_lock_bh(&q->lock); |
| 497 | do { |
| 498 | buf = mt76_dma_dequeue(dev, q, true, NULL, NULL, &more); |
| 499 | if (!buf) |
| 500 | break; |
| 501 | |
| 502 | skb_free_frag(buf); |
| 503 | } while (1); |
| 504 | spin_unlock_bh(&q->lock); |
| 505 | |
| 506 | if (!q->rx_page.va) |
| 507 | return; |
| 508 | |
| 509 | page = virt_to_page(q->rx_page.va); |
| 510 | __page_frag_cache_drain(page, q->rx_page.pagecnt_bias); |
| 511 | memset(&q->rx_page, 0, sizeof(q->rx_page)); |
| 512 | } |
| 513 | |
| 514 | static void |
| 515 | mt76_dma_rx_reset(struct mt76_dev *dev, enum mt76_rxq_id qid) |
| 516 | { |
| 517 | struct mt76_queue *q = &dev->q_rx[qid]; |
| 518 | int i; |
| 519 | |
| 520 | if (!q->ndesc) |
| 521 | return; |
| 522 | |
| 523 | for (i = 0; i < q->ndesc; i++) |
| 524 | q->desc[i].ctrl = cpu_to_le32(MT_DMA_CTL_DMA_DONE); |
| 525 | |
| 526 | mt76_dma_rx_cleanup(dev, q); |
| 527 | mt76_dma_sync_idx(dev, q); |
| 528 | mt76_dma_rx_fill(dev, q); |
| 529 | |
| 530 | if (!q->rx_head) |
| 531 | return; |
| 532 | |
| 533 | dev_kfree_skb(q->rx_head); |
| 534 | q->rx_head = NULL; |
| 535 | } |
| 536 | |
| 537 | static void |
| 538 | mt76_add_fragment(struct mt76_dev *dev, struct mt76_queue *q, void *data, |
| 539 | int len, bool more) |
| 540 | { |
| 541 | struct sk_buff *skb = q->rx_head; |
| 542 | struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 543 | int nr_frags = shinfo->nr_frags; |
| 544 | |
| 545 | if (nr_frags < ARRAY_SIZE(shinfo->frags)) { |
| 546 | struct page *page = virt_to_head_page(data); |
| 547 | int offset = data - page_address(page) + q->buf_offset; |
| 548 | |
| 549 | skb_add_rx_frag(skb, nr_frags, page, offset, len, q->buf_size); |
| 550 | } else { |
| 551 | skb_free_frag(data); |
| 552 | } |
| 553 | |
| 554 | if (more) |
| 555 | return; |
| 556 | |
| 557 | q->rx_head = NULL; |
| 558 | if (nr_frags < ARRAY_SIZE(shinfo->frags)) |
| 559 | dev->drv->rx_skb(dev, q - dev->q_rx, skb); |
| 560 | else |
| 561 | dev_kfree_skb(skb); |
| 562 | } |
| 563 | |
| 564 | static int |
| 565 | mt76_dma_rx_process(struct mt76_dev *dev, struct mt76_queue *q, int budget) |
| 566 | { |
| 567 | int len, data_len, done = 0; |
| 568 | struct sk_buff *skb; |
| 569 | unsigned char *data; |
| 570 | bool more; |
| 571 | |
| 572 | while (done < budget) { |
| 573 | u32 info; |
| 574 | |
| 575 | data = mt76_dma_dequeue(dev, q, false, &len, &info, &more); |
| 576 | if (!data) |
| 577 | break; |
| 578 | |
| 579 | if (q->rx_head) |
| 580 | data_len = q->buf_size; |
| 581 | else |
| 582 | data_len = SKB_WITH_OVERHEAD(q->buf_size); |
| 583 | |
| 584 | if (data_len < len + q->buf_offset) { |
| 585 | dev_kfree_skb(q->rx_head); |
| 586 | q->rx_head = NULL; |
| 587 | goto free_frag; |
| 588 | } |
| 589 | |
| 590 | if (q->rx_head) { |
| 591 | mt76_add_fragment(dev, q, data, len, more); |
| 592 | continue; |
| 593 | } |
| 594 | |
| 595 | if (!more && dev->drv->rx_check && |
| 596 | !(dev->drv->rx_check(dev, q - dev->q_rx, data, len))) |
| 597 | goto free_frag; |
| 598 | |
| 599 | skb = build_skb(data, q->buf_size); |
| 600 | if (!skb) |
| 601 | goto free_frag; |
| 602 | |
| 603 | skb_reserve(skb, q->buf_offset); |
| 604 | |
| 605 | if (q == &dev->q_rx[MT_RXQ_MCU]) { |
| 606 | u32 *rxfce = (u32 *)skb->cb; |
| 607 | *rxfce = info; |
| 608 | } |
| 609 | |
| 610 | __skb_put(skb, len); |
| 611 | done++; |
| 612 | |
| 613 | if (more) { |
| 614 | q->rx_head = skb; |
| 615 | continue; |
| 616 | } |
| 617 | |
| 618 | dev->drv->rx_skb(dev, q - dev->q_rx, skb); |
| 619 | continue; |
| 620 | |
| 621 | free_frag: |
| 622 | skb_free_frag(data); |
| 623 | } |
| 624 | |
| 625 | mt76_dma_rx_fill(dev, q); |
| 626 | return done; |
| 627 | } |
| 628 | |
| 629 | int mt76_dma_rx_poll(struct napi_struct *napi, int budget) |
| 630 | { |
| 631 | struct mt76_dev *dev; |
| 632 | int qid, done = 0, cur; |
| 633 | |
| 634 | dev = container_of(napi->dev, struct mt76_dev, napi_dev); |
| 635 | qid = napi - dev->napi; |
| 636 | |
| 637 | rcu_read_lock(); |
| 638 | |
| 639 | do { |
| 640 | cur = mt76_dma_rx_process(dev, &dev->q_rx[qid], budget - done); |
| 641 | mt76_rx_poll_complete(dev, qid, napi); |
| 642 | done += cur; |
| 643 | } while (cur && done < budget); |
| 644 | |
| 645 | rcu_read_unlock(); |
| 646 | |
| 647 | if (done < budget && napi_complete(napi)) |
| 648 | dev->drv->rx_poll_complete(dev, qid); |
| 649 | |
| 650 | return done; |
| 651 | } |
| 652 | EXPORT_SYMBOL_GPL(mt76_dma_rx_poll); |
| 653 | |
| 654 | static int |
| 655 | mt76_dma_init(struct mt76_dev *dev, |
| 656 | int (*poll)(struct napi_struct *napi, int budget)) |
| 657 | { |
| 658 | int i; |
| 659 | |
| 660 | init_dummy_netdev(&dev->napi_dev); |
| 661 | init_dummy_netdev(&dev->tx_napi_dev); |
| 662 | snprintf(dev->napi_dev.name, sizeof(dev->napi_dev.name), "%s", |
| 663 | wiphy_name(dev->hw->wiphy)); |
| 664 | dev->napi_dev.threaded = 1; |
| 665 | |
| 666 | mt76_for_each_q_rx(dev, i) { |
| 667 | netif_napi_add(&dev->napi_dev, &dev->napi[i], poll, 64); |
| 668 | mt76_dma_rx_fill(dev, &dev->q_rx[i]); |
| 669 | napi_enable(&dev->napi[i]); |
| 670 | } |
| 671 | |
| 672 | return 0; |
| 673 | } |
| 674 | |
| 675 | static const struct mt76_queue_ops mt76_dma_ops = { |
| 676 | .init = mt76_dma_init, |
| 677 | .alloc = mt76_dma_alloc_queue, |
| 678 | .reset_q = mt76_dma_queue_reset, |
| 679 | .tx_queue_skb_raw = mt76_dma_tx_queue_skb_raw, |
| 680 | .tx_queue_skb = mt76_dma_tx_queue_skb, |
| 681 | .tx_cleanup = mt76_dma_tx_cleanup, |
| 682 | .rx_cleanup = mt76_dma_rx_cleanup, |
| 683 | .rx_reset = mt76_dma_rx_reset, |
| 684 | .kick = mt76_dma_kick_queue, |
| 685 | }; |
| 686 | |
| 687 | void mt76_dma_attach(struct mt76_dev *dev) |
| 688 | { |
| 689 | dev->queue_ops = &mt76_dma_ops; |
| 690 | } |
| 691 | EXPORT_SYMBOL_GPL(mt76_dma_attach); |
| 692 | |
| 693 | void mt76_dma_cleanup(struct mt76_dev *dev) |
| 694 | { |
| 695 | int i; |
| 696 | |
| 697 | mt76_worker_disable(&dev->tx_worker); |
| 698 | netif_napi_del(&dev->tx_napi); |
| 699 | |
| 700 | for (i = 0; i < ARRAY_SIZE(dev->phy.q_tx); i++) { |
| 701 | mt76_dma_tx_cleanup(dev, dev->phy.q_tx[i], true); |
| 702 | if (dev->phy2) |
| 703 | mt76_dma_tx_cleanup(dev, dev->phy2->q_tx[i], true); |
| 704 | if (dev->phy3) |
| 705 | mt76_dma_tx_cleanup(dev, dev->phy3->q_tx[i], true); |
| 706 | } |
| 707 | |
| 708 | for (i = 0; i < ARRAY_SIZE(dev->q_mcu); i++) |
| 709 | mt76_dma_tx_cleanup(dev, dev->q_mcu[i], true); |
| 710 | |
| 711 | mt76_for_each_q_rx(dev, i) { |
| 712 | netif_napi_del(&dev->napi[i]); |
| 713 | mt76_dma_rx_cleanup(dev, &dev->q_rx[i]); |
| 714 | } |
| 715 | |
| 716 | mt76_free_pending_txwi(dev); |
| 717 | } |
| 718 | EXPORT_SYMBOL_GPL(mt76_dma_cleanup); |