Lines Matching +full:rx +full:- +full:slots
1 // SPDX-License-Identifier: GPL-2.0-only
45 #define ENET_RX_BUF_DMA_SIZE (ENET_RX_SKB_BUF_SIZE - ENET_RX_BUF_DMA_OFFSET)
54 void *buf; /* RX */
76 struct bcm4908_enet_dma_ring_slot *slots; member
95 return readl(enet->base + offset); in enet_read()
100 writel(value, enet->base + offset); in enet_write()
150 enet_write(enet, ring->cfg_block + ENET_DMA_CH_CFG_INT_MASK, ENET_DMA_INT_DEFAULTS); in bcm4908_enet_dma_ring_intrs_on()
156 enet_write(enet, ring->cfg_block + ENET_DMA_CH_CFG_INT_MASK, 0); in bcm4908_enet_dma_ring_intrs_off()
162 enet_write(enet, ring->cfg_block + ENET_DMA_CH_CFG_INT_STAT, ENET_DMA_INT_DEFAULTS); in bcm4908_enet_dma_ring_intrs_ack()
172 int size = ring->length * sizeof(struct bcm4908_enet_dma_ring_bd); in bcm4908_dma_alloc_buf_descs()
173 struct device *dev = enet->dev; in bcm4908_dma_alloc_buf_descs()
175 ring->cpu_addr = dma_alloc_coherent(dev, size, &ring->dma_addr, GFP_KERNEL); in bcm4908_dma_alloc_buf_descs()
176 if (!ring->cpu_addr) in bcm4908_dma_alloc_buf_descs()
177 return -ENOMEM; in bcm4908_dma_alloc_buf_descs()
179 if (((uintptr_t)ring->cpu_addr) & (0x40 - 1)) { in bcm4908_dma_alloc_buf_descs()
184 ring->slots = kcalloc(ring->length, sizeof(*ring->slots), GFP_KERNEL); in bcm4908_dma_alloc_buf_descs()
185 if (!ring->slots) in bcm4908_dma_alloc_buf_descs()
191 dma_free_coherent(dev, size, ring->cpu_addr, ring->dma_addr); in bcm4908_dma_alloc_buf_descs()
192 ring->cpu_addr = NULL; in bcm4908_dma_alloc_buf_descs()
193 return -ENOMEM; in bcm4908_dma_alloc_buf_descs()
198 struct bcm4908_enet_dma_ring *tx_ring = &enet->tx_ring; in bcm4908_enet_dma_free()
199 struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring; in bcm4908_enet_dma_free()
200 struct device *dev = enet->dev; in bcm4908_enet_dma_free()
203 size = rx_ring->length * sizeof(struct bcm4908_enet_dma_ring_bd); in bcm4908_enet_dma_free()
204 if (rx_ring->cpu_addr) in bcm4908_enet_dma_free()
205 dma_free_coherent(dev, size, rx_ring->cpu_addr, rx_ring->dma_addr); in bcm4908_enet_dma_free()
206 kfree(rx_ring->slots); in bcm4908_enet_dma_free()
208 size = tx_ring->length * sizeof(struct bcm4908_enet_dma_ring_bd); in bcm4908_enet_dma_free()
209 if (tx_ring->cpu_addr) in bcm4908_enet_dma_free()
210 dma_free_coherent(dev, size, tx_ring->cpu_addr, tx_ring->dma_addr); in bcm4908_enet_dma_free()
211 kfree(tx_ring->slots); in bcm4908_enet_dma_free()
216 struct bcm4908_enet_dma_ring *tx_ring = &enet->tx_ring; in bcm4908_enet_dma_alloc()
217 struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring; in bcm4908_enet_dma_alloc()
218 struct device *dev = enet->dev; in bcm4908_enet_dma_alloc()
221 tx_ring->length = ENET_TX_BDS_NUM; in bcm4908_enet_dma_alloc()
222 tx_ring->is_tx = 1; in bcm4908_enet_dma_alloc()
223 tx_ring->cfg_block = ENET_DMA_CH_TX_CFG; in bcm4908_enet_dma_alloc()
224 tx_ring->st_ram_block = ENET_DMA_CH_TX_STATE_RAM; in bcm4908_enet_dma_alloc()
231 rx_ring->length = ENET_RX_BDS_NUM; in bcm4908_enet_dma_alloc()
232 rx_ring->is_tx = 0; in bcm4908_enet_dma_alloc()
233 rx_ring->cfg_block = ENET_DMA_CH_RX_CFG; in bcm4908_enet_dma_alloc()
234 rx_ring->st_ram_block = ENET_DMA_CH_RX_STATE_RAM; in bcm4908_enet_dma_alloc()
237 dev_err(dev, "Failed to alloc RX buf descriptors: %d\n", err); in bcm4908_enet_dma_alloc()
247 struct bcm4908_enet_dma_ring *rings[] = { &enet->rx_ring, &enet->tx_ring }; in bcm4908_enet_dma_reset()
252 enet_write(enet, rings[i]->cfg_block + ENET_DMA_CH_CFG, 0); in bcm4908_enet_dma_reset()
259 enet_write(enet, ring->st_ram_block + ENET_DMA_CH_STATE_RAM_BASE_DESC_PTR, 0); in bcm4908_enet_dma_reset()
260 enet_write(enet, ring->st_ram_block + ENET_DMA_CH_STATE_RAM_STATE_DATA, 0); in bcm4908_enet_dma_reset()
261 enet_write(enet, ring->st_ram_block + ENET_DMA_CH_STATE_RAM_DESC_LEN_STATUS, 0); in bcm4908_enet_dma_reset()
262 enet_write(enet, ring->st_ram_block + ENET_DMA_CH_STATE_RAM_DESC_BASE_BUFPTR, 0); in bcm4908_enet_dma_reset()
268 struct bcm4908_enet_dma_ring_bd *buf_desc = &enet->rx_ring.buf_desc[idx]; in bcm4908_enet_dma_alloc_rx_buf()
269 struct bcm4908_enet_dma_ring_slot *slot = &enet->rx_ring.slots[idx]; in bcm4908_enet_dma_alloc_rx_buf()
270 struct device *dev = enet->dev; in bcm4908_enet_dma_alloc_rx_buf()
274 slot->buf = napi_alloc_frag(ENET_RX_SKB_BUF_ALLOC_SIZE); in bcm4908_enet_dma_alloc_rx_buf()
275 if (!slot->buf) in bcm4908_enet_dma_alloc_rx_buf()
276 return -ENOMEM; in bcm4908_enet_dma_alloc_rx_buf()
278 slot->dma_addr = dma_map_single(dev, slot->buf + ENET_RX_BUF_DMA_OFFSET, in bcm4908_enet_dma_alloc_rx_buf()
280 err = dma_mapping_error(dev, slot->dma_addr); in bcm4908_enet_dma_alloc_rx_buf()
283 skb_free_frag(slot->buf); in bcm4908_enet_dma_alloc_rx_buf()
284 slot->buf = NULL; in bcm4908_enet_dma_alloc_rx_buf()
290 if (idx == enet->rx_ring.length - 1) in bcm4908_enet_dma_alloc_rx_buf()
292 buf_desc->ctl = cpu_to_le32(tmp); in bcm4908_enet_dma_alloc_rx_buf()
293 buf_desc->addr = cpu_to_le32(slot->dma_addr); in bcm4908_enet_dma_alloc_rx_buf()
301 int reset_channel = 0; /* We support only 1 main channel (with TX and RX) */ in bcm4908_enet_dma_ring_init()
302 int reset_subch = ring->is_tx ? 1 : 0; in bcm4908_enet_dma_ring_init()
308 enet_write(enet, ring->cfg_block + ENET_DMA_CH_CFG, 0); in bcm4908_enet_dma_ring_init()
309 enet_write(enet, ring->cfg_block + ENET_DMA_CH_CFG_MAX_BURST, ENET_DMA_MAX_BURST_LEN); in bcm4908_enet_dma_ring_init()
310 enet_write(enet, ring->cfg_block + ENET_DMA_CH_CFG_INT_MASK, 0); in bcm4908_enet_dma_ring_init()
312 enet_write(enet, ring->st_ram_block + ENET_DMA_CH_STATE_RAM_BASE_DESC_PTR, in bcm4908_enet_dma_ring_init()
313 (uint32_t)ring->dma_addr); in bcm4908_enet_dma_ring_init()
315 ring->read_idx = 0; in bcm4908_enet_dma_ring_init()
316 ring->write_idx = 0; in bcm4908_enet_dma_ring_init()
321 struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring; in bcm4908_enet_dma_uninit()
323 struct device *dev = enet->dev; in bcm4908_enet_dma_uninit()
326 for (i = rx_ring->length - 1; i >= 0; i--) { in bcm4908_enet_dma_uninit()
327 slot = &rx_ring->slots[i]; in bcm4908_enet_dma_uninit()
328 if (!slot->buf) in bcm4908_enet_dma_uninit()
330 dma_unmap_single(dev, slot->dma_addr, slot->len, DMA_FROM_DEVICE); in bcm4908_enet_dma_uninit()
331 skb_free_frag(slot->buf); in bcm4908_enet_dma_uninit()
332 slot->buf = NULL; in bcm4908_enet_dma_uninit()
338 struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring; in bcm4908_enet_dma_init()
339 struct device *dev = enet->dev; in bcm4908_enet_dma_init()
343 for (i = 0; i < rx_ring->length; i++) { in bcm4908_enet_dma_init()
346 dev_err(dev, "Failed to alloc RX buffer: %d\n", err); in bcm4908_enet_dma_init()
352 bcm4908_enet_dma_ring_init(enet, &enet->tx_ring); in bcm4908_enet_dma_init()
353 bcm4908_enet_dma_ring_init(enet, &enet->rx_ring); in bcm4908_enet_dma_init()
361 enet_write(enet, ring->cfg_block + ENET_DMA_CH_CFG, ENET_DMA_CH_CFG_ENABLE); in bcm4908_enet_dma_tx_ring_enable()
367 enet_write(enet, ring->cfg_block + ENET_DMA_CH_CFG, 0); in bcm4908_enet_dma_tx_ring_disable()
373 enet_set(enet, ring->cfg_block + ENET_DMA_CH_CFG, ENET_DMA_CH_CFG_ENABLE); in bcm4908_enet_dma_rx_ring_enable()
382 enet_maskset(enet, ring->cfg_block + ENET_DMA_CH_CFG, ENET_DMA_CH_CFG_ENABLE, 0); in bcm4908_enet_dma_rx_ring_disable()
386 tmp = enet_read(enet, ring->cfg_block + ENET_DMA_CH_CFG); in bcm4908_enet_dma_rx_ring_disable()
389 enet_maskset(enet, ring->cfg_block + ENET_DMA_CH_CFG, ENET_DMA_CH_CFG_ENABLE, 0); in bcm4908_enet_dma_rx_ring_disable()
393 dev_warn(enet->dev, "Timeout waiting for DMA TX stop\n"); in bcm4908_enet_dma_rx_ring_disable()
404 bcm4908_enet_set_mtu(enet, enet->netdev->mtu); in bcm4908_enet_gmac_init()
438 ring = (irq == enet->irq_tx) ? &enet->tx_ring : &enet->rx_ring; in bcm4908_enet_irq_handler()
443 napi_schedule(&ring->napi); in bcm4908_enet_irq_handler()
451 struct bcm4908_enet_dma_ring *tx_ring = &enet->tx_ring; in bcm4908_enet_open()
452 struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring; in bcm4908_enet_open()
453 struct device *dev = enet->dev; in bcm4908_enet_open()
456 err = request_irq(netdev->irq, bcm4908_enet_irq_handler, 0, "enet", enet); in bcm4908_enet_open()
458 dev_err(dev, "Failed to request IRQ %d: %d\n", netdev->irq, err); in bcm4908_enet_open()
462 if (enet->irq_tx > 0) { in bcm4908_enet_open()
463 err = request_irq(enet->irq_tx, bcm4908_enet_irq_handler, 0, in bcm4908_enet_open()
467 enet->irq_tx, err); in bcm4908_enet_open()
468 free_irq(netdev->irq, enet); in bcm4908_enet_open()
482 if (enet->irq_tx > 0) { in bcm4908_enet_open()
483 napi_enable(&tx_ring->napi); in bcm4908_enet_open()
489 napi_enable(&rx_ring->napi); in bcm4908_enet_open()
501 struct bcm4908_enet_dma_ring *tx_ring = &enet->tx_ring; in bcm4908_enet_stop()
502 struct bcm4908_enet_dma_ring *rx_ring = &enet->rx_ring; in bcm4908_enet_stop()
506 napi_disable(&rx_ring->napi); in bcm4908_enet_stop()
507 napi_disable(&tx_ring->napi); in bcm4908_enet_stop()
510 bcm4908_enet_dma_rx_ring_disable(enet, &enet->rx_ring); in bcm4908_enet_stop()
511 bcm4908_enet_dma_tx_ring_disable(enet, &enet->tx_ring); in bcm4908_enet_stop()
515 free_irq(enet->irq_tx, enet); in bcm4908_enet_stop()
516 free_irq(enet->netdev->irq, enet); in bcm4908_enet_stop()
524 struct bcm4908_enet_dma_ring *ring = &enet->tx_ring; in bcm4908_enet_start_xmit()
526 struct device *dev = enet->dev; in bcm4908_enet_start_xmit()
532 if (enet->irq_tx < 0 && in bcm4908_enet_start_xmit()
533 !(le32_to_cpu(ring->buf_desc[ring->read_idx].ctl) & DMA_CTL_STATUS_OWN)) in bcm4908_enet_start_xmit()
534 napi_schedule(&enet->tx_ring.napi); in bcm4908_enet_start_xmit()
537 if (ring->read_idx <= ring->write_idx) in bcm4908_enet_start_xmit()
538 free_buf_descs = ring->read_idx - ring->write_idx + ring->length; in bcm4908_enet_start_xmit()
540 free_buf_descs = ring->read_idx - ring->write_idx; in bcm4908_enet_start_xmit()
547 buf_desc = &ring->buf_desc[ring->write_idx]; in bcm4908_enet_start_xmit()
548 if (unlikely(le32_to_cpu(buf_desc->ctl) & DMA_CTL_STATUS_OWN)) { in bcm4908_enet_start_xmit()
553 slot = &ring->slots[ring->write_idx]; in bcm4908_enet_start_xmit()
554 slot->skb = skb; in bcm4908_enet_start_xmit()
555 slot->len = skb->len; in bcm4908_enet_start_xmit()
556 slot->dma_addr = dma_map_single(dev, skb->data, skb->len, DMA_TO_DEVICE); in bcm4908_enet_start_xmit()
557 if (unlikely(dma_mapping_error(dev, slot->dma_addr))) in bcm4908_enet_start_xmit()
560 tmp = skb->len << DMA_CTL_LEN_DESC_BUFLENGTH_SHIFT; in bcm4908_enet_start_xmit()
565 if (ring->write_idx + 1 == ring->length - 1) in bcm4908_enet_start_xmit()
568 netdev_sent_queue(enet->netdev, skb->len); in bcm4908_enet_start_xmit()
570 buf_desc->addr = cpu_to_le32((uint32_t)slot->dma_addr); in bcm4908_enet_start_xmit()
571 buf_desc->ctl = cpu_to_le32(tmp); in bcm4908_enet_start_xmit()
573 bcm4908_enet_dma_tx_ring_enable(enet, &enet->tx_ring); in bcm4908_enet_start_xmit()
575 if (++ring->write_idx == ring->length - 1) in bcm4908_enet_start_xmit()
576 ring->write_idx = 0; in bcm4908_enet_start_xmit()
585 struct device *dev = enet->dev; in bcm4908_enet_poll_rx()
596 buf_desc = &enet->rx_ring.buf_desc[enet->rx_ring.read_idx]; in bcm4908_enet_poll_rx()
597 ctl = le32_to_cpu(buf_desc->ctl); in bcm4908_enet_poll_rx()
601 slot = enet->rx_ring.slots[enet->rx_ring.read_idx]; in bcm4908_enet_poll_rx()
604 err = bcm4908_enet_dma_alloc_rx_buf(enet, enet->rx_ring.read_idx); in bcm4908_enet_poll_rx()
608 if (++enet->rx_ring.read_idx == enet->rx_ring.length) in bcm4908_enet_poll_rx()
609 enet->rx_ring.read_idx = 0; in bcm4908_enet_poll_rx()
616 enet->netdev->stats.rx_dropped++; in bcm4908_enet_poll_rx()
625 enet->netdev->stats.rx_dropped++; in bcm4908_enet_poll_rx()
629 skb_put(skb, len - ETH_FCS_LEN); in bcm4908_enet_poll_rx()
630 skb->protocol = eth_type_trans(skb, enet->netdev); in bcm4908_enet_poll_rx()
634 enet->netdev->stats.rx_packets++; in bcm4908_enet_poll_rx()
635 enet->netdev->stats.rx_bytes += len; in bcm4908_enet_poll_rx()
646 bcm4908_enet_dma_rx_ring_enable(enet, &enet->rx_ring); in bcm4908_enet_poll_rx()
657 struct device *dev = enet->dev; in bcm4908_enet_poll_tx()
661 while (handled < weight && tx_ring->read_idx != tx_ring->write_idx) { in bcm4908_enet_poll_tx()
662 buf_desc = &tx_ring->buf_desc[tx_ring->read_idx]; in bcm4908_enet_poll_tx()
663 if (le32_to_cpu(buf_desc->ctl) & DMA_CTL_STATUS_OWN) in bcm4908_enet_poll_tx()
665 slot = &tx_ring->slots[tx_ring->read_idx]; in bcm4908_enet_poll_tx()
667 dma_unmap_single(dev, slot->dma_addr, slot->len, DMA_TO_DEVICE); in bcm4908_enet_poll_tx()
668 dev_kfree_skb(slot->skb); in bcm4908_enet_poll_tx()
671 bytes += slot->len; in bcm4908_enet_poll_tx()
673 if (++tx_ring->read_idx == tx_ring->length) in bcm4908_enet_poll_tx()
674 tx_ring->read_idx = 0; in bcm4908_enet_poll_tx()
677 netdev_completed_queue(enet->netdev, handled, bytes); in bcm4908_enet_poll_tx()
678 enet->netdev->stats.tx_packets += handled; in bcm4908_enet_poll_tx()
679 enet->netdev->stats.tx_bytes += bytes; in bcm4908_enet_poll_tx()
686 if (netif_queue_stopped(enet->netdev)) in bcm4908_enet_poll_tx()
687 netif_wake_queue(enet->netdev); in bcm4908_enet_poll_tx()
711 struct device *dev = &pdev->dev; in bcm4908_enet_probe()
718 return -ENOMEM; in bcm4908_enet_probe()
721 enet->dev = dev; in bcm4908_enet_probe()
722 enet->netdev = netdev; in bcm4908_enet_probe()
724 enet->base = devm_platform_ioremap_resource(pdev, 0); in bcm4908_enet_probe()
725 if (IS_ERR(enet->base)) { in bcm4908_enet_probe()
726 dev_err(dev, "Failed to map registers: %ld\n", PTR_ERR(enet->base)); in bcm4908_enet_probe()
727 return PTR_ERR(enet->base); in bcm4908_enet_probe()
730 netdev->irq = platform_get_irq_byname(pdev, "rx"); in bcm4908_enet_probe()
731 if (netdev->irq < 0) in bcm4908_enet_probe()
732 return netdev->irq; in bcm4908_enet_probe()
734 enet->irq_tx = platform_get_irq_byname(pdev, "tx"); in bcm4908_enet_probe()
744 SET_NETDEV_DEV(netdev, &pdev->dev); in bcm4908_enet_probe()
745 err = of_get_ethdev_address(dev->of_node, netdev); in bcm4908_enet_probe()
746 if (err == -EPROBE_DEFER) in bcm4908_enet_probe()
750 netdev->netdev_ops = &bcm4908_enet_netdev_ops; in bcm4908_enet_probe()
751 netdev->min_mtu = ETH_ZLEN; in bcm4908_enet_probe()
752 netdev->mtu = ETH_DATA_LEN; in bcm4908_enet_probe()
753 netdev->max_mtu = ENET_MTU_MAX; in bcm4908_enet_probe()
754 netif_napi_add_tx(netdev, &enet->tx_ring.napi, bcm4908_enet_poll_tx); in bcm4908_enet_probe()
755 netif_napi_add(netdev, &enet->rx_ring.napi, bcm4908_enet_poll_rx); in bcm4908_enet_probe()
775 unregister_netdev(enet->netdev); in bcm4908_enet_remove()
776 netif_napi_del(&enet->rx_ring.napi); in bcm4908_enet_remove()
777 netif_napi_del(&enet->tx_ring.napi); in bcm4908_enet_remove()
784 { .compatible = "brcm,bcm4908-enet"},