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