1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021 Gerhard Engleder <gerhard@engleder-embedded.com> */
4 /* TSN endpoint Ethernet MAC driver
6 * The TSN endpoint Ethernet MAC is a FPGA based network device for real-time
7 * communication. It is designed for endpoints within TSN (Time Sensitive
8 * Networking) networks; e.g., for PLCs in the industrial automation case.
10 * It supports multiple TX/RX queue pairs. The first TX/RX queue pair is used
13 * More information can be found here:
14 * - www.embedded-experts.at/tsn
15 * - www.engleder-embedded.com
21 #include <linux/module.h>
23 #include <linux/of_net.h>
24 #include <linux/of_mdio.h>
25 #include <linux/interrupt.h>
26 #include <linux/etherdevice.h>
27 #include <linux/phy.h>
28 #include <linux/iopoll.h>
29 #include <linux/bpf.h>
30 #include <linux/bpf_trace.h>
31 #include <net/page_pool/helpers.h>
32 #include <net/xdp_sock_drv.h>
34 #define TSNEP_RX_OFFSET (max(NET_SKB_PAD, XDP_PACKET_HEADROOM) + NET_IP_ALIGN)
35 #define TSNEP_HEADROOM ALIGN(TSNEP_RX_OFFSET, 4)
36 #define TSNEP_MAX_RX_BUF_SIZE (PAGE_SIZE - TSNEP_HEADROOM - \
37 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
38 /* XSK buffer shall store at least Q-in-Q frame */
39 #define TSNEP_XSK_RX_BUF_SIZE (ALIGN(TSNEP_RX_INLINE_METADATA_SIZE + \
40 ETH_FRAME_LEN + ETH_FCS_LEN + \
43 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
44 #define DMA_ADDR_HIGH(dma_addr) ((u32)(((dma_addr) >> 32) & 0xFFFFFFFF))
46 #define DMA_ADDR_HIGH(dma_addr) ((u32)(0))
48 #define DMA_ADDR_LOW(dma_addr) ((u32)((dma_addr) & 0xFFFFFFFF))
50 #define TSNEP_COALESCE_USECS_DEFAULT 64
51 #define TSNEP_COALESCE_USECS_MAX ((ECM_INT_DELAY_MASK >> ECM_INT_DELAY_SHIFT) * \
52 ECM_INT_DELAY_BASE_US + ECM_INT_DELAY_BASE_US - 1)
54 #define TSNEP_TX_TYPE_SKB BIT(0)
55 #define TSNEP_TX_TYPE_SKB_FRAG BIT(1)
56 #define TSNEP_TX_TYPE_XDP_TX BIT(2)
57 #define TSNEP_TX_TYPE_XDP_NDO BIT(3)
58 #define TSNEP_TX_TYPE_XDP (TSNEP_TX_TYPE_XDP_TX | TSNEP_TX_TYPE_XDP_NDO)
59 #define TSNEP_TX_TYPE_XSK BIT(4)
61 #define TSNEP_XDP_TX BIT(0)
62 #define TSNEP_XDP_REDIRECT BIT(1)
64 static void tsnep_enable_irq(struct tsnep_adapter *adapter, u32 mask)
66 iowrite32(mask, adapter->addr + ECM_INT_ENABLE);
69 static void tsnep_disable_irq(struct tsnep_adapter *adapter, u32 mask)
71 mask |= ECM_INT_DISABLE;
72 iowrite32(mask, adapter->addr + ECM_INT_ENABLE);
75 static irqreturn_t tsnep_irq(int irq, void *arg)
77 struct tsnep_adapter *adapter = arg;
78 u32 active = ioread32(adapter->addr + ECM_INT_ACTIVE);
80 /* acknowledge interrupt */
82 iowrite32(active, adapter->addr + ECM_INT_ACKNOWLEDGE);
84 /* handle link interrupt */
85 if ((active & ECM_INT_LINK) != 0)
86 phy_mac_interrupt(adapter->netdev->phydev);
88 /* handle TX/RX queue 0 interrupt */
89 if ((active & adapter->queue[0].irq_mask) != 0) {
90 if (napi_schedule_prep(&adapter->queue[0].napi)) {
91 tsnep_disable_irq(adapter, adapter->queue[0].irq_mask);
92 /* schedule after masking to avoid races */
93 __napi_schedule(&adapter->queue[0].napi);
100 static irqreturn_t tsnep_irq_txrx(int irq, void *arg)
102 struct tsnep_queue *queue = arg;
104 /* handle TX/RX queue interrupt */
105 if (napi_schedule_prep(&queue->napi)) {
106 tsnep_disable_irq(queue->adapter, queue->irq_mask);
107 /* schedule after masking to avoid races */
108 __napi_schedule(&queue->napi);
114 int tsnep_set_irq_coalesce(struct tsnep_queue *queue, u32 usecs)
116 if (usecs > TSNEP_COALESCE_USECS_MAX)
119 usecs /= ECM_INT_DELAY_BASE_US;
120 usecs <<= ECM_INT_DELAY_SHIFT;
121 usecs &= ECM_INT_DELAY_MASK;
123 queue->irq_delay &= ~ECM_INT_DELAY_MASK;
124 queue->irq_delay |= usecs;
125 iowrite8(queue->irq_delay, queue->irq_delay_addr);
130 u32 tsnep_get_irq_coalesce(struct tsnep_queue *queue)
134 usecs = (queue->irq_delay & ECM_INT_DELAY_MASK);
135 usecs >>= ECM_INT_DELAY_SHIFT;
136 usecs *= ECM_INT_DELAY_BASE_US;
141 static int tsnep_mdiobus_read(struct mii_bus *bus, int addr, int regnum)
143 struct tsnep_adapter *adapter = bus->priv;
148 if (!adapter->suppress_preamble)
149 md |= ECM_MD_PREAMBLE;
150 md |= (regnum << ECM_MD_ADDR_SHIFT) & ECM_MD_ADDR_MASK;
151 md |= (addr << ECM_MD_PHY_ADDR_SHIFT) & ECM_MD_PHY_ADDR_MASK;
152 iowrite32(md, adapter->addr + ECM_MD_CONTROL);
153 retval = readl_poll_timeout_atomic(adapter->addr + ECM_MD_STATUS, md,
154 !(md & ECM_MD_BUSY), 16, 1000);
158 return (md & ECM_MD_DATA_MASK) >> ECM_MD_DATA_SHIFT;
161 static int tsnep_mdiobus_write(struct mii_bus *bus, int addr, int regnum,
164 struct tsnep_adapter *adapter = bus->priv;
169 if (!adapter->suppress_preamble)
170 md |= ECM_MD_PREAMBLE;
171 md |= (regnum << ECM_MD_ADDR_SHIFT) & ECM_MD_ADDR_MASK;
172 md |= (addr << ECM_MD_PHY_ADDR_SHIFT) & ECM_MD_PHY_ADDR_MASK;
173 md |= ((u32)val << ECM_MD_DATA_SHIFT) & ECM_MD_DATA_MASK;
174 iowrite32(md, adapter->addr + ECM_MD_CONTROL);
175 retval = readl_poll_timeout_atomic(adapter->addr + ECM_MD_STATUS, md,
176 !(md & ECM_MD_BUSY), 16, 1000);
183 static void tsnep_set_link_mode(struct tsnep_adapter *adapter)
187 switch (adapter->phydev->speed) {
189 mode = ECM_LINK_MODE_100;
192 mode = ECM_LINK_MODE_1000;
195 mode = ECM_LINK_MODE_OFF;
198 iowrite32(mode, adapter->addr + ECM_STATUS);
201 static void tsnep_phy_link_status_change(struct net_device *netdev)
203 struct tsnep_adapter *adapter = netdev_priv(netdev);
204 struct phy_device *phydev = netdev->phydev;
207 tsnep_set_link_mode(adapter);
209 phy_print_status(netdev->phydev);
212 static int tsnep_phy_loopback(struct tsnep_adapter *adapter, bool enable)
216 retval = phy_loopback(adapter->phydev, enable);
218 /* PHY link state change is not signaled if loopback is enabled, it
219 * would delay a working loopback anyway, let's ensure that loopback
220 * is working immediately by setting link mode directly
222 if (!retval && enable)
223 tsnep_set_link_mode(adapter);
228 static int tsnep_phy_open(struct tsnep_adapter *adapter)
230 struct phy_device *phydev;
231 struct ethtool_eee ethtool_eee;
234 retval = phy_connect_direct(adapter->netdev, adapter->phydev,
235 tsnep_phy_link_status_change,
239 phydev = adapter->netdev->phydev;
241 /* MAC supports only 100Mbps|1000Mbps full duplex
242 * SPE (Single Pair Ethernet) is also an option but not implemented yet
244 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Half_BIT);
245 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_10baseT_Full_BIT);
246 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_100baseT_Half_BIT);
247 phy_remove_link_mode(phydev, ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
249 /* disable EEE autoneg, EEE not supported by TSNEP */
250 memset(ðtool_eee, 0, sizeof(ethtool_eee));
251 phy_ethtool_set_eee(adapter->phydev, ðtool_eee);
253 adapter->phydev->irq = PHY_MAC_INTERRUPT;
254 phy_start(adapter->phydev);
259 static void tsnep_phy_close(struct tsnep_adapter *adapter)
261 phy_stop(adapter->netdev->phydev);
262 phy_disconnect(adapter->netdev->phydev);
265 static void tsnep_tx_ring_cleanup(struct tsnep_tx *tx)
267 struct device *dmadev = tx->adapter->dmadev;
270 memset(tx->entry, 0, sizeof(tx->entry));
272 for (i = 0; i < TSNEP_RING_PAGE_COUNT; i++) {
274 dma_free_coherent(dmadev, PAGE_SIZE, tx->page[i],
282 static int tsnep_tx_ring_create(struct tsnep_tx *tx)
284 struct device *dmadev = tx->adapter->dmadev;
285 struct tsnep_tx_entry *entry;
286 struct tsnep_tx_entry *next_entry;
290 for (i = 0; i < TSNEP_RING_PAGE_COUNT; i++) {
292 dma_alloc_coherent(dmadev, PAGE_SIZE, &tx->page_dma[i],
298 for (j = 0; j < TSNEP_RING_ENTRIES_PER_PAGE; j++) {
299 entry = &tx->entry[TSNEP_RING_ENTRIES_PER_PAGE * i + j];
300 entry->desc_wb = (struct tsnep_tx_desc_wb *)
301 (((u8 *)tx->page[i]) + TSNEP_DESC_SIZE * j);
302 entry->desc = (struct tsnep_tx_desc *)
303 (((u8 *)entry->desc_wb) + TSNEP_DESC_OFFSET);
304 entry->desc_dma = tx->page_dma[i] + TSNEP_DESC_SIZE * j;
305 entry->owner_user_flag = false;
308 for (i = 0; i < TSNEP_RING_SIZE; i++) {
309 entry = &tx->entry[i];
310 next_entry = &tx->entry[(i + 1) & TSNEP_RING_MASK];
311 entry->desc->next = __cpu_to_le64(next_entry->desc_dma);
317 tsnep_tx_ring_cleanup(tx);
321 static void tsnep_tx_init(struct tsnep_tx *tx)
325 dma = tx->entry[0].desc_dma | TSNEP_RESET_OWNER_COUNTER;
326 iowrite32(DMA_ADDR_LOW(dma), tx->addr + TSNEP_TX_DESC_ADDR_LOW);
327 iowrite32(DMA_ADDR_HIGH(dma), tx->addr + TSNEP_TX_DESC_ADDR_HIGH);
330 tx->owner_counter = 1;
331 tx->increment_owner_counter = TSNEP_RING_SIZE - 1;
334 static void tsnep_tx_enable(struct tsnep_tx *tx)
336 struct netdev_queue *nq;
338 nq = netdev_get_tx_queue(tx->adapter->netdev, tx->queue_index);
340 __netif_tx_lock_bh(nq);
341 netif_tx_wake_queue(nq);
342 __netif_tx_unlock_bh(nq);
345 static void tsnep_tx_disable(struct tsnep_tx *tx, struct napi_struct *napi)
347 struct netdev_queue *nq;
350 nq = netdev_get_tx_queue(tx->adapter->netdev, tx->queue_index);
352 __netif_tx_lock_bh(nq);
353 netif_tx_stop_queue(nq);
354 __netif_tx_unlock_bh(nq);
356 /* wait until TX is done in hardware */
357 readx_poll_timeout(ioread32, tx->addr + TSNEP_CONTROL, val,
358 ((val & TSNEP_CONTROL_TX_ENABLE) == 0), 10000,
361 /* wait until TX is also done in software */
362 while (READ_ONCE(tx->read) != tx->write) {
364 napi_synchronize(napi);
368 static void tsnep_tx_activate(struct tsnep_tx *tx, int index, int length,
371 struct tsnep_tx_entry *entry = &tx->entry[index];
373 entry->properties = 0;
374 /* xdpf and zc are union with skb */
376 entry->properties = length & TSNEP_DESC_LENGTH_MASK;
377 entry->properties |= TSNEP_DESC_INTERRUPT_FLAG;
378 if ((entry->type & TSNEP_TX_TYPE_SKB) &&
379 (skb_shinfo(entry->skb)->tx_flags & SKBTX_IN_PROGRESS))
380 entry->properties |= TSNEP_DESC_EXTENDED_WRITEBACK_FLAG;
382 /* toggle user flag to prevent false acknowledge
384 * Only the first fragment is acknowledged. For all other
385 * fragments no acknowledge is done and the last written owner
386 * counter stays in the writeback descriptor. Therefore, it is
387 * possible that the last written owner counter is identical to
388 * the new incremented owner counter and a false acknowledge is
389 * detected before the real acknowledge has been done by
392 * The user flag is used to prevent this situation. The user
393 * flag is copied to the writeback descriptor by the hardware
394 * and is used as additional acknowledge data. By toggeling the
395 * user flag only for the first fragment (which is
396 * acknowledged), it is guaranteed that the last acknowledge
397 * done for this descriptor has used a different user flag and
398 * cannot be detected as false acknowledge.
400 entry->owner_user_flag = !entry->owner_user_flag;
403 entry->properties |= TSNEP_TX_DESC_LAST_FRAGMENT_FLAG;
404 if (index == tx->increment_owner_counter) {
406 if (tx->owner_counter == 4)
407 tx->owner_counter = 1;
408 tx->increment_owner_counter--;
409 if (tx->increment_owner_counter < 0)
410 tx->increment_owner_counter = TSNEP_RING_SIZE - 1;
413 (tx->owner_counter << TSNEP_DESC_OWNER_COUNTER_SHIFT) &
414 TSNEP_DESC_OWNER_COUNTER_MASK;
415 if (entry->owner_user_flag)
416 entry->properties |= TSNEP_TX_DESC_OWNER_USER_FLAG;
417 entry->desc->more_properties =
418 __cpu_to_le32(entry->len & TSNEP_DESC_LENGTH_MASK);
420 /* descriptor properties shall be written last, because valid data is
425 entry->desc->properties = __cpu_to_le32(entry->properties);
428 static int tsnep_tx_desc_available(struct tsnep_tx *tx)
430 if (tx->read <= tx->write)
431 return TSNEP_RING_SIZE - tx->write + tx->read - 1;
433 return tx->read - tx->write - 1;
436 static int tsnep_tx_map(struct sk_buff *skb, struct tsnep_tx *tx, int count)
438 struct device *dmadev = tx->adapter->dmadev;
439 struct tsnep_tx_entry *entry;
445 for (i = 0; i < count; i++) {
446 entry = &tx->entry[(tx->write + i) & TSNEP_RING_MASK];
449 len = skb_headlen(skb);
450 dma = dma_map_single(dmadev, skb->data, len,
453 entry->type = TSNEP_TX_TYPE_SKB;
455 len = skb_frag_size(&skb_shinfo(skb)->frags[i - 1]);
456 dma = skb_frag_dma_map(dmadev,
457 &skb_shinfo(skb)->frags[i - 1],
458 0, len, DMA_TO_DEVICE);
460 entry->type = TSNEP_TX_TYPE_SKB_FRAG;
462 if (dma_mapping_error(dmadev, dma))
466 dma_unmap_addr_set(entry, dma, dma);
468 entry->desc->tx = __cpu_to_le64(dma);
476 static int tsnep_tx_unmap(struct tsnep_tx *tx, int index, int count)
478 struct device *dmadev = tx->adapter->dmadev;
479 struct tsnep_tx_entry *entry;
483 for (i = 0; i < count; i++) {
484 entry = &tx->entry[(index + i) & TSNEP_RING_MASK];
487 if (entry->type & TSNEP_TX_TYPE_SKB)
488 dma_unmap_single(dmadev,
489 dma_unmap_addr(entry, dma),
490 dma_unmap_len(entry, len),
492 else if (entry->type &
493 (TSNEP_TX_TYPE_SKB_FRAG | TSNEP_TX_TYPE_XDP_NDO))
494 dma_unmap_page(dmadev,
495 dma_unmap_addr(entry, dma),
496 dma_unmap_len(entry, len),
498 map_len += entry->len;
506 static netdev_tx_t tsnep_xmit_frame_ring(struct sk_buff *skb,
510 struct tsnep_tx_entry *entry;
515 if (skb_shinfo(skb)->nr_frags > 0)
516 count += skb_shinfo(skb)->nr_frags;
518 if (tsnep_tx_desc_available(tx) < count) {
519 /* ring full, shall not happen because queue is stopped if full
522 netif_stop_subqueue(tx->adapter->netdev, tx->queue_index);
524 return NETDEV_TX_BUSY;
527 entry = &tx->entry[tx->write];
530 retval = tsnep_tx_map(skb, tx, count);
532 tsnep_tx_unmap(tx, tx->write, count);
533 dev_kfree_skb_any(entry->skb);
542 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)
543 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
545 for (i = 0; i < count; i++)
546 tsnep_tx_activate(tx, (tx->write + i) & TSNEP_RING_MASK, length,
548 tx->write = (tx->write + count) & TSNEP_RING_MASK;
550 skb_tx_timestamp(skb);
552 /* descriptor properties shall be valid before hardware is notified */
555 iowrite32(TSNEP_CONTROL_TX_ENABLE, tx->addr + TSNEP_CONTROL);
557 if (tsnep_tx_desc_available(tx) < (MAX_SKB_FRAGS + 1)) {
558 /* ring can get full with next frame */
559 netif_stop_subqueue(tx->adapter->netdev, tx->queue_index);
565 static int tsnep_xdp_tx_map(struct xdp_frame *xdpf, struct tsnep_tx *tx,
566 struct skb_shared_info *shinfo, int count, u32 type)
568 struct device *dmadev = tx->adapter->dmadev;
569 struct tsnep_tx_entry *entry;
580 for (i = 0; i < count; i++) {
581 entry = &tx->entry[(tx->write + i) & TSNEP_RING_MASK];
582 if (type & TSNEP_TX_TYPE_XDP_NDO) {
583 data = unlikely(frag) ? skb_frag_address(frag) :
585 dma = dma_map_single(dmadev, data, len, DMA_TO_DEVICE);
586 if (dma_mapping_error(dmadev, dma))
589 entry->type = TSNEP_TX_TYPE_XDP_NDO;
591 page = unlikely(frag) ? skb_frag_page(frag) :
592 virt_to_page(xdpf->data);
593 dma = page_pool_get_dma_addr(page);
595 dma += skb_frag_off(frag);
597 dma += sizeof(*xdpf) + xdpf->headroom;
598 dma_sync_single_for_device(dmadev, dma, len,
601 entry->type = TSNEP_TX_TYPE_XDP_TX;
605 dma_unmap_addr_set(entry, dma, dma);
607 entry->desc->tx = __cpu_to_le64(dma);
612 frag = &shinfo->frags[i];
613 len = skb_frag_size(frag);
620 /* This function requires __netif_tx_lock is held by the caller. */
621 static bool tsnep_xdp_xmit_frame_ring(struct xdp_frame *xdpf,
622 struct tsnep_tx *tx, u32 type)
624 struct skb_shared_info *shinfo = xdp_get_shared_info_from_frame(xdpf);
625 struct tsnep_tx_entry *entry;
626 int count, length, retval, i;
629 if (unlikely(xdp_frame_has_frags(xdpf)))
630 count += shinfo->nr_frags;
632 /* ensure that TX ring is not filled up by XDP, always MAX_SKB_FRAGS
633 * will be available for normal TX path and queue is stopped there if
636 if (tsnep_tx_desc_available(tx) < (MAX_SKB_FRAGS + 1 + count))
639 entry = &tx->entry[tx->write];
642 retval = tsnep_xdp_tx_map(xdpf, tx, shinfo, count, type);
644 tsnep_tx_unmap(tx, tx->write, count);
653 for (i = 0; i < count; i++)
654 tsnep_tx_activate(tx, (tx->write + i) & TSNEP_RING_MASK, length,
656 tx->write = (tx->write + count) & TSNEP_RING_MASK;
658 /* descriptor properties shall be valid before hardware is notified */
664 static void tsnep_xdp_xmit_flush(struct tsnep_tx *tx)
666 iowrite32(TSNEP_CONTROL_TX_ENABLE, tx->addr + TSNEP_CONTROL);
669 static bool tsnep_xdp_xmit_back(struct tsnep_adapter *adapter,
670 struct xdp_buff *xdp,
671 struct netdev_queue *tx_nq, struct tsnep_tx *tx)
673 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp);
679 __netif_tx_lock(tx_nq, smp_processor_id());
681 xmit = tsnep_xdp_xmit_frame_ring(xdpf, tx, TSNEP_TX_TYPE_XDP_TX);
683 /* Avoid transmit queue timeout since we share it with the slow path */
685 txq_trans_cond_update(tx_nq);
687 __netif_tx_unlock(tx_nq);
692 static int tsnep_xdp_tx_map_zc(struct xdp_desc *xdpd, struct tsnep_tx *tx)
694 struct tsnep_tx_entry *entry;
697 entry = &tx->entry[tx->write];
700 dma = xsk_buff_raw_get_dma(tx->xsk_pool, xdpd->addr);
701 xsk_buff_raw_dma_sync_for_device(tx->xsk_pool, dma, xdpd->len);
703 entry->type = TSNEP_TX_TYPE_XSK;
704 entry->len = xdpd->len;
706 entry->desc->tx = __cpu_to_le64(dma);
711 static void tsnep_xdp_xmit_frame_ring_zc(struct xdp_desc *xdpd,
716 length = tsnep_xdp_tx_map_zc(xdpd, tx);
718 tsnep_tx_activate(tx, tx->write, length, true);
719 tx->write = (tx->write + 1) & TSNEP_RING_MASK;
722 static void tsnep_xdp_xmit_zc(struct tsnep_tx *tx)
724 int desc_available = tsnep_tx_desc_available(tx);
725 struct xdp_desc *descs = tx->xsk_pool->tx_descs;
728 /* ensure that TX ring is not filled up by XDP, always MAX_SKB_FRAGS
729 * will be available for normal TX path and queue is stopped there if
732 if (desc_available <= (MAX_SKB_FRAGS + 1))
734 desc_available -= MAX_SKB_FRAGS + 1;
736 batch = xsk_tx_peek_release_desc_batch(tx->xsk_pool, desc_available);
737 for (i = 0; i < batch; i++)
738 tsnep_xdp_xmit_frame_ring_zc(&descs[i], tx);
741 /* descriptor properties shall be valid before hardware is
746 tsnep_xdp_xmit_flush(tx);
750 static bool tsnep_tx_poll(struct tsnep_tx *tx, int napi_budget)
752 struct tsnep_tx_entry *entry;
753 struct netdev_queue *nq;
759 nq = netdev_get_tx_queue(tx->adapter->netdev, tx->queue_index);
760 __netif_tx_lock(nq, smp_processor_id());
763 if (tx->read == tx->write)
766 entry = &tx->entry[tx->read];
767 if ((__le32_to_cpu(entry->desc_wb->properties) &
768 TSNEP_TX_DESC_OWNER_MASK) !=
769 (entry->properties & TSNEP_TX_DESC_OWNER_MASK))
772 /* descriptor properties shall be read first, because valid data
778 if ((entry->type & TSNEP_TX_TYPE_SKB) &&
779 skb_shinfo(entry->skb)->nr_frags > 0)
780 count += skb_shinfo(entry->skb)->nr_frags;
781 else if ((entry->type & TSNEP_TX_TYPE_XDP) &&
782 xdp_frame_has_frags(entry->xdpf))
783 count += xdp_get_shared_info_from_frame(entry->xdpf)->nr_frags;
785 length = tsnep_tx_unmap(tx, tx->read, count);
787 if ((entry->type & TSNEP_TX_TYPE_SKB) &&
788 (skb_shinfo(entry->skb)->tx_flags & SKBTX_IN_PROGRESS) &&
789 (__le32_to_cpu(entry->desc_wb->properties) &
790 TSNEP_DESC_EXTENDED_WRITEBACK_FLAG)) {
791 struct skb_shared_hwtstamps hwtstamps;
794 if (skb_shinfo(entry->skb)->tx_flags &
795 SKBTX_HW_TSTAMP_USE_CYCLES)
797 __le64_to_cpu(entry->desc_wb->counter);
800 __le64_to_cpu(entry->desc_wb->timestamp);
802 memset(&hwtstamps, 0, sizeof(hwtstamps));
803 hwtstamps.hwtstamp = ns_to_ktime(timestamp);
805 skb_tstamp_tx(entry->skb, &hwtstamps);
808 if (entry->type & TSNEP_TX_TYPE_SKB)
809 napi_consume_skb(entry->skb, napi_budget);
810 else if (entry->type & TSNEP_TX_TYPE_XDP)
811 xdp_return_frame_rx_napi(entry->xdpf);
814 /* xdpf and zc are union with skb */
817 tx->read = (tx->read + count) & TSNEP_RING_MASK;
820 tx->bytes += length + ETH_FCS_LEN;
823 } while (likely(budget));
827 xsk_tx_completed(tx->xsk_pool, xsk_frames);
828 if (xsk_uses_need_wakeup(tx->xsk_pool))
829 xsk_set_tx_need_wakeup(tx->xsk_pool);
830 tsnep_xdp_xmit_zc(tx);
833 if ((tsnep_tx_desc_available(tx) >= ((MAX_SKB_FRAGS + 1) * 2)) &&
834 netif_tx_queue_stopped(nq)) {
835 netif_tx_wake_queue(nq);
838 __netif_tx_unlock(nq);
843 static bool tsnep_tx_pending(struct tsnep_tx *tx)
845 struct tsnep_tx_entry *entry;
846 struct netdev_queue *nq;
847 bool pending = false;
849 nq = netdev_get_tx_queue(tx->adapter->netdev, tx->queue_index);
850 __netif_tx_lock(nq, smp_processor_id());
852 if (tx->read != tx->write) {
853 entry = &tx->entry[tx->read];
854 if ((__le32_to_cpu(entry->desc_wb->properties) &
855 TSNEP_TX_DESC_OWNER_MASK) ==
856 (entry->properties & TSNEP_TX_DESC_OWNER_MASK))
860 __netif_tx_unlock(nq);
865 static int tsnep_tx_open(struct tsnep_tx *tx)
869 retval = tsnep_tx_ring_create(tx);
878 static void tsnep_tx_close(struct tsnep_tx *tx)
880 tsnep_tx_ring_cleanup(tx);
883 static void tsnep_rx_ring_cleanup(struct tsnep_rx *rx)
885 struct device *dmadev = rx->adapter->dmadev;
886 struct tsnep_rx_entry *entry;
889 for (i = 0; i < TSNEP_RING_SIZE; i++) {
890 entry = &rx->entry[i];
891 if (!rx->xsk_pool && entry->page)
892 page_pool_put_full_page(rx->page_pool, entry->page,
894 if (rx->xsk_pool && entry->xdp)
895 xsk_buff_free(entry->xdp);
896 /* xdp is union with page */
901 page_pool_destroy(rx->page_pool);
903 memset(rx->entry, 0, sizeof(rx->entry));
905 for (i = 0; i < TSNEP_RING_PAGE_COUNT; i++) {
907 dma_free_coherent(dmadev, PAGE_SIZE, rx->page[i],
915 static int tsnep_rx_ring_create(struct tsnep_rx *rx)
917 struct device *dmadev = rx->adapter->dmadev;
918 struct tsnep_rx_entry *entry;
919 struct page_pool_params pp_params = { 0 };
920 struct tsnep_rx_entry *next_entry;
924 for (i = 0; i < TSNEP_RING_PAGE_COUNT; i++) {
926 dma_alloc_coherent(dmadev, PAGE_SIZE, &rx->page_dma[i],
932 for (j = 0; j < TSNEP_RING_ENTRIES_PER_PAGE; j++) {
933 entry = &rx->entry[TSNEP_RING_ENTRIES_PER_PAGE * i + j];
934 entry->desc_wb = (struct tsnep_rx_desc_wb *)
935 (((u8 *)rx->page[i]) + TSNEP_DESC_SIZE * j);
936 entry->desc = (struct tsnep_rx_desc *)
937 (((u8 *)entry->desc_wb) + TSNEP_DESC_OFFSET);
938 entry->desc_dma = rx->page_dma[i] + TSNEP_DESC_SIZE * j;
942 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
944 pp_params.pool_size = TSNEP_RING_SIZE;
945 pp_params.nid = dev_to_node(dmadev);
946 pp_params.dev = dmadev;
947 pp_params.dma_dir = DMA_BIDIRECTIONAL;
948 pp_params.max_len = TSNEP_MAX_RX_BUF_SIZE;
949 pp_params.offset = TSNEP_RX_OFFSET;
950 rx->page_pool = page_pool_create(&pp_params);
951 if (IS_ERR(rx->page_pool)) {
952 retval = PTR_ERR(rx->page_pool);
953 rx->page_pool = NULL;
957 for (i = 0; i < TSNEP_RING_SIZE; i++) {
958 entry = &rx->entry[i];
959 next_entry = &rx->entry[(i + 1) & TSNEP_RING_MASK];
960 entry->desc->next = __cpu_to_le64(next_entry->desc_dma);
966 tsnep_rx_ring_cleanup(rx);
970 static void tsnep_rx_init(struct tsnep_rx *rx)
974 dma = rx->entry[0].desc_dma | TSNEP_RESET_OWNER_COUNTER;
975 iowrite32(DMA_ADDR_LOW(dma), rx->addr + TSNEP_RX_DESC_ADDR_LOW);
976 iowrite32(DMA_ADDR_HIGH(dma), rx->addr + TSNEP_RX_DESC_ADDR_HIGH);
979 rx->owner_counter = 1;
980 rx->increment_owner_counter = TSNEP_RING_SIZE - 1;
983 static void tsnep_rx_enable(struct tsnep_rx *rx)
985 /* descriptor properties shall be valid before hardware is notified */
988 iowrite32(TSNEP_CONTROL_RX_ENABLE, rx->addr + TSNEP_CONTROL);
991 static void tsnep_rx_disable(struct tsnep_rx *rx)
995 iowrite32(TSNEP_CONTROL_RX_DISABLE, rx->addr + TSNEP_CONTROL);
996 readx_poll_timeout(ioread32, rx->addr + TSNEP_CONTROL, val,
997 ((val & TSNEP_CONTROL_RX_ENABLE) == 0), 10000,
1001 static int tsnep_rx_desc_available(struct tsnep_rx *rx)
1003 if (rx->read <= rx->write)
1004 return TSNEP_RING_SIZE - rx->write + rx->read - 1;
1006 return rx->read - rx->write - 1;
1009 static void tsnep_rx_free_page_buffer(struct tsnep_rx *rx)
1013 /* last entry of page_buffer is always zero, because ring cannot be
1016 page = rx->page_buffer;
1018 page_pool_put_full_page(rx->page_pool, *page, false);
1024 static int tsnep_rx_alloc_page_buffer(struct tsnep_rx *rx)
1028 /* alloc for all ring entries except the last one, because ring cannot
1029 * be filled completely
1031 for (i = 0; i < TSNEP_RING_SIZE - 1; i++) {
1032 rx->page_buffer[i] = page_pool_dev_alloc_pages(rx->page_pool);
1033 if (!rx->page_buffer[i]) {
1034 tsnep_rx_free_page_buffer(rx);
1043 static void tsnep_rx_set_page(struct tsnep_rx *rx, struct tsnep_rx_entry *entry,
1047 entry->len = TSNEP_MAX_RX_BUF_SIZE;
1048 entry->dma = page_pool_get_dma_addr(entry->page);
1049 entry->desc->rx = __cpu_to_le64(entry->dma + TSNEP_RX_OFFSET);
1052 static int tsnep_rx_alloc_buffer(struct tsnep_rx *rx, int index)
1054 struct tsnep_rx_entry *entry = &rx->entry[index];
1057 page = page_pool_dev_alloc_pages(rx->page_pool);
1058 if (unlikely(!page))
1060 tsnep_rx_set_page(rx, entry, page);
1065 static void tsnep_rx_reuse_buffer(struct tsnep_rx *rx, int index)
1067 struct tsnep_rx_entry *entry = &rx->entry[index];
1068 struct tsnep_rx_entry *read = &rx->entry[rx->read];
1070 tsnep_rx_set_page(rx, entry, read->page);
1074 static void tsnep_rx_activate(struct tsnep_rx *rx, int index)
1076 struct tsnep_rx_entry *entry = &rx->entry[index];
1078 /* TSNEP_MAX_RX_BUF_SIZE and TSNEP_XSK_RX_BUF_SIZE are multiple of 4 */
1079 entry->properties = entry->len & TSNEP_DESC_LENGTH_MASK;
1080 entry->properties |= TSNEP_DESC_INTERRUPT_FLAG;
1081 if (index == rx->increment_owner_counter) {
1082 rx->owner_counter++;
1083 if (rx->owner_counter == 4)
1084 rx->owner_counter = 1;
1085 rx->increment_owner_counter--;
1086 if (rx->increment_owner_counter < 0)
1087 rx->increment_owner_counter = TSNEP_RING_SIZE - 1;
1089 entry->properties |=
1090 (rx->owner_counter << TSNEP_DESC_OWNER_COUNTER_SHIFT) &
1091 TSNEP_DESC_OWNER_COUNTER_MASK;
1093 /* descriptor properties shall be written last, because valid data is
1098 entry->desc->properties = __cpu_to_le32(entry->properties);
1101 static int tsnep_rx_alloc(struct tsnep_rx *rx, int count, bool reuse)
1103 bool alloc_failed = false;
1106 for (i = 0; i < count && !alloc_failed; i++) {
1107 index = (rx->write + i) & TSNEP_RING_MASK;
1109 if (unlikely(tsnep_rx_alloc_buffer(rx, index))) {
1111 alloc_failed = true;
1113 /* reuse only if no other allocation was successful */
1114 if (i == 0 && reuse)
1115 tsnep_rx_reuse_buffer(rx, index);
1120 tsnep_rx_activate(rx, index);
1124 rx->write = (rx->write + i) & TSNEP_RING_MASK;
1129 static int tsnep_rx_refill(struct tsnep_rx *rx, int count, bool reuse)
1133 desc_refilled = tsnep_rx_alloc(rx, count, reuse);
1135 tsnep_rx_enable(rx);
1137 return desc_refilled;
1140 static void tsnep_rx_set_xdp(struct tsnep_rx *rx, struct tsnep_rx_entry *entry,
1141 struct xdp_buff *xdp)
1144 entry->len = TSNEP_XSK_RX_BUF_SIZE;
1145 entry->dma = xsk_buff_xdp_get_dma(entry->xdp);
1146 entry->desc->rx = __cpu_to_le64(entry->dma);
1149 static void tsnep_rx_reuse_buffer_zc(struct tsnep_rx *rx, int index)
1151 struct tsnep_rx_entry *entry = &rx->entry[index];
1152 struct tsnep_rx_entry *read = &rx->entry[rx->read];
1154 tsnep_rx_set_xdp(rx, entry, read->xdp);
1158 static int tsnep_rx_alloc_zc(struct tsnep_rx *rx, int count, bool reuse)
1163 allocated = xsk_buff_alloc_batch(rx->xsk_pool, rx->xdp_batch, count);
1164 for (i = 0; i < allocated; i++) {
1165 int index = (rx->write + i) & TSNEP_RING_MASK;
1166 struct tsnep_rx_entry *entry = &rx->entry[index];
1168 tsnep_rx_set_xdp(rx, entry, rx->xdp_batch[i]);
1169 tsnep_rx_activate(rx, index);
1175 tsnep_rx_reuse_buffer_zc(rx, rx->write);
1176 tsnep_rx_activate(rx, rx->write);
1181 rx->write = (rx->write + i) & TSNEP_RING_MASK;
1186 static void tsnep_rx_free_zc(struct tsnep_rx *rx)
1190 for (i = 0; i < TSNEP_RING_SIZE; i++) {
1191 struct tsnep_rx_entry *entry = &rx->entry[i];
1194 xsk_buff_free(entry->xdp);
1199 static int tsnep_rx_refill_zc(struct tsnep_rx *rx, int count, bool reuse)
1203 desc_refilled = tsnep_rx_alloc_zc(rx, count, reuse);
1205 tsnep_rx_enable(rx);
1207 return desc_refilled;
1210 static bool tsnep_xdp_run_prog(struct tsnep_rx *rx, struct bpf_prog *prog,
1211 struct xdp_buff *xdp, int *status,
1212 struct netdev_queue *tx_nq, struct tsnep_tx *tx)
1214 unsigned int length;
1218 length = xdp->data_end - xdp->data_hard_start - XDP_PACKET_HEADROOM;
1220 act = bpf_prog_run_xdp(prog, xdp);
1225 if (!tsnep_xdp_xmit_back(rx->adapter, xdp, tx_nq, tx))
1227 *status |= TSNEP_XDP_TX;
1230 if (xdp_do_redirect(rx->adapter->netdev, xdp, prog) < 0)
1232 *status |= TSNEP_XDP_REDIRECT;
1235 bpf_warn_invalid_xdp_action(rx->adapter->netdev, prog, act);
1239 trace_xdp_exception(rx->adapter->netdev, prog, act);
1242 /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU
1245 sync = xdp->data_end - xdp->data_hard_start -
1246 XDP_PACKET_HEADROOM;
1247 sync = max(sync, length);
1248 page_pool_put_page(rx->page_pool, virt_to_head_page(xdp->data),
1254 static bool tsnep_xdp_run_prog_zc(struct tsnep_rx *rx, struct bpf_prog *prog,
1255 struct xdp_buff *xdp, int *status,
1256 struct netdev_queue *tx_nq,
1257 struct tsnep_tx *tx)
1261 act = bpf_prog_run_xdp(prog, xdp);
1263 /* XDP_REDIRECT is the main action for zero-copy */
1264 if (likely(act == XDP_REDIRECT)) {
1265 if (xdp_do_redirect(rx->adapter->netdev, xdp, prog) < 0)
1267 *status |= TSNEP_XDP_REDIRECT;
1275 if (!tsnep_xdp_xmit_back(rx->adapter, xdp, tx_nq, tx))
1277 *status |= TSNEP_XDP_TX;
1280 bpf_warn_invalid_xdp_action(rx->adapter->netdev, prog, act);
1284 trace_xdp_exception(rx->adapter->netdev, prog, act);
1292 static void tsnep_finalize_xdp(struct tsnep_adapter *adapter, int status,
1293 struct netdev_queue *tx_nq, struct tsnep_tx *tx)
1295 if (status & TSNEP_XDP_TX) {
1296 __netif_tx_lock(tx_nq, smp_processor_id());
1297 tsnep_xdp_xmit_flush(tx);
1298 __netif_tx_unlock(tx_nq);
1301 if (status & TSNEP_XDP_REDIRECT)
1305 static struct sk_buff *tsnep_build_skb(struct tsnep_rx *rx, struct page *page,
1308 struct sk_buff *skb;
1310 skb = napi_build_skb(page_address(page), PAGE_SIZE);
1314 /* update pointers within the skb to store the data */
1315 skb_reserve(skb, TSNEP_RX_OFFSET + TSNEP_RX_INLINE_METADATA_SIZE);
1316 __skb_put(skb, length - ETH_FCS_LEN);
1318 if (rx->adapter->hwtstamp_config.rx_filter == HWTSTAMP_FILTER_ALL) {
1319 struct skb_shared_hwtstamps *hwtstamps = skb_hwtstamps(skb);
1320 struct tsnep_rx_inline *rx_inline =
1321 (struct tsnep_rx_inline *)(page_address(page) +
1324 skb_shinfo(skb)->tx_flags |=
1325 SKBTX_HW_TSTAMP_NETDEV;
1326 memset(hwtstamps, 0, sizeof(*hwtstamps));
1327 hwtstamps->netdev_data = rx_inline;
1330 skb_record_rx_queue(skb, rx->queue_index);
1331 skb->protocol = eth_type_trans(skb, rx->adapter->netdev);
1336 static void tsnep_rx_page(struct tsnep_rx *rx, struct napi_struct *napi,
1337 struct page *page, int length)
1339 struct sk_buff *skb;
1341 skb = tsnep_build_skb(rx, page, length);
1343 skb_mark_for_recycle(skb);
1346 rx->bytes += length;
1347 if (skb->pkt_type == PACKET_MULTICAST)
1350 napi_gro_receive(napi, skb);
1352 page_pool_recycle_direct(rx->page_pool, page);
1358 static int tsnep_rx_poll(struct tsnep_rx *rx, struct napi_struct *napi,
1361 struct device *dmadev = rx->adapter->dmadev;
1362 enum dma_data_direction dma_dir;
1363 struct tsnep_rx_entry *entry;
1364 struct netdev_queue *tx_nq;
1365 struct bpf_prog *prog;
1366 struct xdp_buff xdp;
1367 struct tsnep_tx *tx;
1373 desc_available = tsnep_rx_desc_available(rx);
1374 dma_dir = page_pool_get_dma_dir(rx->page_pool);
1375 prog = READ_ONCE(rx->adapter->xdp_prog);
1377 tx_nq = netdev_get_tx_queue(rx->adapter->netdev,
1378 rx->tx_queue_index);
1379 tx = &rx->adapter->tx[rx->tx_queue_index];
1381 xdp_init_buff(&xdp, PAGE_SIZE, &rx->xdp_rxq);
1384 while (likely(done < budget) && (rx->read != rx->write)) {
1385 entry = &rx->entry[rx->read];
1386 if ((__le32_to_cpu(entry->desc_wb->properties) &
1387 TSNEP_DESC_OWNER_COUNTER_MASK) !=
1388 (entry->properties & TSNEP_DESC_OWNER_COUNTER_MASK))
1392 if (desc_available >= TSNEP_RING_RX_REFILL) {
1393 bool reuse = desc_available >= TSNEP_RING_RX_REUSE;
1395 desc_available -= tsnep_rx_refill(rx, desc_available,
1398 /* buffer has been reused for refill to prevent
1399 * empty RX ring, thus buffer cannot be used for
1402 rx->read = (rx->read + 1) & TSNEP_RING_MASK;
1411 /* descriptor properties shall be read first, because valid data
1416 prefetch(page_address(entry->page) + TSNEP_RX_OFFSET);
1417 length = __le32_to_cpu(entry->desc_wb->properties) &
1418 TSNEP_DESC_LENGTH_MASK;
1419 dma_sync_single_range_for_cpu(dmadev, entry->dma,
1420 TSNEP_RX_OFFSET, length, dma_dir);
1422 /* RX metadata with timestamps is in front of actual data,
1423 * subtract metadata size to get length of actual data and
1424 * consider metadata size as offset of actual data during RX
1427 length -= TSNEP_RX_INLINE_METADATA_SIZE;
1429 rx->read = (rx->read + 1) & TSNEP_RING_MASK;
1435 xdp_prepare_buff(&xdp, page_address(entry->page),
1436 XDP_PACKET_HEADROOM + TSNEP_RX_INLINE_METADATA_SIZE,
1439 consume = tsnep_xdp_run_prog(rx, prog, &xdp,
1440 &xdp_status, tx_nq, tx);
1443 rx->bytes += length;
1451 tsnep_rx_page(rx, napi, entry->page, length);
1456 tsnep_finalize_xdp(rx->adapter, xdp_status, tx_nq, tx);
1459 tsnep_rx_refill(rx, desc_available, false);
1464 static int tsnep_rx_poll_zc(struct tsnep_rx *rx, struct napi_struct *napi,
1467 struct tsnep_rx_entry *entry;
1468 struct netdev_queue *tx_nq;
1469 struct bpf_prog *prog;
1470 struct tsnep_tx *tx;
1477 desc_available = tsnep_rx_desc_available(rx);
1478 prog = READ_ONCE(rx->adapter->xdp_prog);
1480 tx_nq = netdev_get_tx_queue(rx->adapter->netdev,
1481 rx->tx_queue_index);
1482 tx = &rx->adapter->tx[rx->tx_queue_index];
1485 while (likely(done < budget) && (rx->read != rx->write)) {
1486 entry = &rx->entry[rx->read];
1487 if ((__le32_to_cpu(entry->desc_wb->properties) &
1488 TSNEP_DESC_OWNER_COUNTER_MASK) !=
1489 (entry->properties & TSNEP_DESC_OWNER_COUNTER_MASK))
1493 if (desc_available >= TSNEP_RING_RX_REFILL) {
1494 bool reuse = desc_available >= TSNEP_RING_RX_REUSE;
1496 desc_available -= tsnep_rx_refill_zc(rx, desc_available,
1499 /* buffer has been reused for refill to prevent
1500 * empty RX ring, thus buffer cannot be used for
1503 rx->read = (rx->read + 1) & TSNEP_RING_MASK;
1512 /* descriptor properties shall be read first, because valid data
1517 prefetch(entry->xdp->data);
1518 length = __le32_to_cpu(entry->desc_wb->properties) &
1519 TSNEP_DESC_LENGTH_MASK;
1520 xsk_buff_set_size(entry->xdp, length);
1521 xsk_buff_dma_sync_for_cpu(entry->xdp, rx->xsk_pool);
1523 /* RX metadata with timestamps is in front of actual data,
1524 * subtract metadata size to get length of actual data and
1525 * consider metadata size as offset of actual data during RX
1528 length -= TSNEP_RX_INLINE_METADATA_SIZE;
1530 rx->read = (rx->read + 1) & TSNEP_RING_MASK;
1536 entry->xdp->data += TSNEP_RX_INLINE_METADATA_SIZE;
1537 entry->xdp->data_meta += TSNEP_RX_INLINE_METADATA_SIZE;
1539 consume = tsnep_xdp_run_prog_zc(rx, prog, entry->xdp,
1540 &xdp_status, tx_nq, tx);
1543 rx->bytes += length;
1551 page = page_pool_dev_alloc_pages(rx->page_pool);
1553 memcpy(page_address(page) + TSNEP_RX_OFFSET,
1554 entry->xdp->data - TSNEP_RX_INLINE_METADATA_SIZE,
1555 length + TSNEP_RX_INLINE_METADATA_SIZE);
1556 tsnep_rx_page(rx, napi, page, length);
1560 xsk_buff_free(entry->xdp);
1565 tsnep_finalize_xdp(rx->adapter, xdp_status, tx_nq, tx);
1568 desc_available -= tsnep_rx_refill_zc(rx, desc_available, false);
1570 if (xsk_uses_need_wakeup(rx->xsk_pool)) {
1572 xsk_set_rx_need_wakeup(rx->xsk_pool);
1574 xsk_clear_rx_need_wakeup(rx->xsk_pool);
1579 return desc_available ? budget : done;
1582 static bool tsnep_rx_pending(struct tsnep_rx *rx)
1584 struct tsnep_rx_entry *entry;
1586 if (rx->read != rx->write) {
1587 entry = &rx->entry[rx->read];
1588 if ((__le32_to_cpu(entry->desc_wb->properties) &
1589 TSNEP_DESC_OWNER_COUNTER_MASK) ==
1590 (entry->properties & TSNEP_DESC_OWNER_COUNTER_MASK))
1597 static int tsnep_rx_open(struct tsnep_rx *rx)
1602 retval = tsnep_rx_ring_create(rx);
1608 desc_available = tsnep_rx_desc_available(rx);
1610 retval = tsnep_rx_alloc_zc(rx, desc_available, false);
1612 retval = tsnep_rx_alloc(rx, desc_available, false);
1613 if (retval != desc_available) {
1619 /* prealloc pages to prevent allocation failures when XSK pool is
1620 * disabled at runtime
1623 retval = tsnep_rx_alloc_page_buffer(rx);
1631 tsnep_rx_ring_cleanup(rx);
1635 static void tsnep_rx_close(struct tsnep_rx *rx)
1638 tsnep_rx_free_page_buffer(rx);
1640 tsnep_rx_ring_cleanup(rx);
1643 static void tsnep_rx_reopen(struct tsnep_rx *rx)
1645 struct page **page = rx->page_buffer;
1650 for (i = 0; i < TSNEP_RING_SIZE; i++) {
1651 struct tsnep_rx_entry *entry = &rx->entry[i];
1653 /* defined initial values for properties are required for
1654 * correct owner counter checking
1656 entry->desc->properties = 0;
1657 entry->desc_wb->properties = 0;
1659 /* prevent allocation failures by reusing kept pages */
1661 tsnep_rx_set_page(rx, entry, *page);
1662 tsnep_rx_activate(rx, rx->write);
1671 static void tsnep_rx_reopen_xsk(struct tsnep_rx *rx)
1673 struct page **page = rx->page_buffer;
1679 /* alloc all ring entries except the last one, because ring cannot be
1680 * filled completely, as many buffers as possible is enough as wakeup is
1681 * done if new buffers are available
1683 allocated = xsk_buff_alloc_batch(rx->xsk_pool, rx->xdp_batch,
1684 TSNEP_RING_SIZE - 1);
1686 for (i = 0; i < TSNEP_RING_SIZE; i++) {
1687 struct tsnep_rx_entry *entry = &rx->entry[i];
1689 /* keep pages to prevent allocation failures when xsk is
1693 *page = entry->page;
1699 /* defined initial values for properties are required for
1700 * correct owner counter checking
1702 entry->desc->properties = 0;
1703 entry->desc_wb->properties = 0;
1706 tsnep_rx_set_xdp(rx, entry,
1707 rx->xdp_batch[allocated - 1]);
1708 tsnep_rx_activate(rx, rx->write);
1716 static bool tsnep_pending(struct tsnep_queue *queue)
1718 if (queue->tx && tsnep_tx_pending(queue->tx))
1721 if (queue->rx && tsnep_rx_pending(queue->rx))
1727 static int tsnep_poll(struct napi_struct *napi, int budget)
1729 struct tsnep_queue *queue = container_of(napi, struct tsnep_queue,
1731 bool complete = true;
1735 complete = tsnep_tx_poll(queue->tx, budget);
1737 /* handle case where we are called by netpoll with a budget of 0 */
1738 if (unlikely(budget <= 0))
1742 done = queue->rx->xsk_pool ?
1743 tsnep_rx_poll_zc(queue->rx, napi, budget) :
1744 tsnep_rx_poll(queue->rx, napi, budget);
1749 /* if all work not completed, return budget and keep polling */
1753 if (likely(napi_complete_done(napi, done))) {
1754 tsnep_enable_irq(queue->adapter, queue->irq_mask);
1756 /* reschedule if work is already pending, prevent rotten packets
1757 * which are transmitted or received after polling but before
1760 if (tsnep_pending(queue)) {
1761 tsnep_disable_irq(queue->adapter, queue->irq_mask);
1762 napi_schedule(napi);
1766 return min(done, budget - 1);
1769 static int tsnep_request_irq(struct tsnep_queue *queue, bool first)
1771 const char *name = netdev_name(queue->adapter->netdev);
1772 irq_handler_t handler;
1777 sprintf(queue->name, "%s-mac", name);
1778 handler = tsnep_irq;
1779 dev = queue->adapter;
1781 if (queue->tx && queue->rx)
1782 sprintf(queue->name, "%s-txrx-%d", name,
1783 queue->rx->queue_index);
1785 sprintf(queue->name, "%s-tx-%d", name,
1786 queue->tx->queue_index);
1788 sprintf(queue->name, "%s-rx-%d", name,
1789 queue->rx->queue_index);
1790 handler = tsnep_irq_txrx;
1794 retval = request_irq(queue->irq, handler, 0, queue->name, dev);
1796 /* if name is empty, then interrupt won't be freed */
1797 memset(queue->name, 0, sizeof(queue->name));
1803 static void tsnep_free_irq(struct tsnep_queue *queue, bool first)
1807 if (!strlen(queue->name))
1811 dev = queue->adapter;
1815 free_irq(queue->irq, dev);
1816 memset(queue->name, 0, sizeof(queue->name));
1819 static void tsnep_queue_close(struct tsnep_queue *queue, bool first)
1821 struct tsnep_rx *rx = queue->rx;
1823 tsnep_free_irq(queue, first);
1826 if (xdp_rxq_info_is_reg(&rx->xdp_rxq))
1827 xdp_rxq_info_unreg(&rx->xdp_rxq);
1828 if (xdp_rxq_info_is_reg(&rx->xdp_rxq_zc))
1829 xdp_rxq_info_unreg(&rx->xdp_rxq_zc);
1832 netif_napi_del(&queue->napi);
1835 static int tsnep_queue_open(struct tsnep_adapter *adapter,
1836 struct tsnep_queue *queue, bool first)
1838 struct tsnep_rx *rx = queue->rx;
1839 struct tsnep_tx *tx = queue->tx;
1842 netif_napi_add(adapter->netdev, &queue->napi, tsnep_poll);
1845 /* choose TX queue for XDP_TX */
1847 rx->tx_queue_index = tx->queue_index;
1848 else if (rx->queue_index < adapter->num_tx_queues)
1849 rx->tx_queue_index = rx->queue_index;
1851 rx->tx_queue_index = 0;
1853 /* prepare both memory models to eliminate possible registration
1854 * errors when memory model is switched between page pool and
1855 * XSK pool during runtime
1857 retval = xdp_rxq_info_reg(&rx->xdp_rxq, adapter->netdev,
1858 rx->queue_index, queue->napi.napi_id);
1861 retval = xdp_rxq_info_reg_mem_model(&rx->xdp_rxq,
1866 retval = xdp_rxq_info_reg(&rx->xdp_rxq_zc, adapter->netdev,
1867 rx->queue_index, queue->napi.napi_id);
1870 retval = xdp_rxq_info_reg_mem_model(&rx->xdp_rxq_zc,
1871 MEM_TYPE_XSK_BUFF_POOL,
1876 xsk_pool_set_rxq_info(rx->xsk_pool, &rx->xdp_rxq_zc);
1879 retval = tsnep_request_irq(queue, first);
1881 netif_err(adapter, drv, adapter->netdev,
1882 "can't get assigned irq %d.\n", queue->irq);
1889 tsnep_queue_close(queue, first);
1894 static void tsnep_queue_enable(struct tsnep_queue *queue)
1896 napi_enable(&queue->napi);
1897 tsnep_enable_irq(queue->adapter, queue->irq_mask);
1900 tsnep_tx_enable(queue->tx);
1903 tsnep_rx_enable(queue->rx);
1906 static void tsnep_queue_disable(struct tsnep_queue *queue)
1909 tsnep_tx_disable(queue->tx, &queue->napi);
1911 napi_disable(&queue->napi);
1912 tsnep_disable_irq(queue->adapter, queue->irq_mask);
1914 /* disable RX after NAPI polling has been disabled, because RX can be
1915 * enabled during NAPI polling
1918 tsnep_rx_disable(queue->rx);
1921 static int tsnep_netdev_open(struct net_device *netdev)
1923 struct tsnep_adapter *adapter = netdev_priv(netdev);
1926 for (i = 0; i < adapter->num_queues; i++) {
1927 if (adapter->queue[i].tx) {
1928 retval = tsnep_tx_open(adapter->queue[i].tx);
1932 if (adapter->queue[i].rx) {
1933 retval = tsnep_rx_open(adapter->queue[i].rx);
1938 retval = tsnep_queue_open(adapter, &adapter->queue[i], i == 0);
1943 retval = netif_set_real_num_tx_queues(adapter->netdev,
1944 adapter->num_tx_queues);
1947 retval = netif_set_real_num_rx_queues(adapter->netdev,
1948 adapter->num_rx_queues);
1952 tsnep_enable_irq(adapter, ECM_INT_LINK);
1953 retval = tsnep_phy_open(adapter);
1957 for (i = 0; i < adapter->num_queues; i++)
1958 tsnep_queue_enable(&adapter->queue[i]);
1963 tsnep_disable_irq(adapter, ECM_INT_LINK);
1965 for (i = 0; i < adapter->num_queues; i++) {
1966 tsnep_queue_close(&adapter->queue[i], i == 0);
1968 if (adapter->queue[i].rx)
1969 tsnep_rx_close(adapter->queue[i].rx);
1970 if (adapter->queue[i].tx)
1971 tsnep_tx_close(adapter->queue[i].tx);
1976 static int tsnep_netdev_close(struct net_device *netdev)
1978 struct tsnep_adapter *adapter = netdev_priv(netdev);
1981 tsnep_disable_irq(adapter, ECM_INT_LINK);
1982 tsnep_phy_close(adapter);
1984 for (i = 0; i < adapter->num_queues; i++) {
1985 tsnep_queue_disable(&adapter->queue[i]);
1987 tsnep_queue_close(&adapter->queue[i], i == 0);
1989 if (adapter->queue[i].rx)
1990 tsnep_rx_close(adapter->queue[i].rx);
1991 if (adapter->queue[i].tx)
1992 tsnep_tx_close(adapter->queue[i].tx);
1998 int tsnep_enable_xsk(struct tsnep_queue *queue, struct xsk_buff_pool *pool)
2000 bool running = netif_running(queue->adapter->netdev);
2003 frame_size = xsk_pool_get_rx_frame_size(pool);
2004 if (frame_size < TSNEP_XSK_RX_BUF_SIZE)
2007 queue->rx->page_buffer = kcalloc(TSNEP_RING_SIZE,
2008 sizeof(*queue->rx->page_buffer),
2010 if (!queue->rx->page_buffer)
2012 queue->rx->xdp_batch = kcalloc(TSNEP_RING_SIZE,
2013 sizeof(*queue->rx->xdp_batch),
2015 if (!queue->rx->xdp_batch) {
2016 kfree(queue->rx->page_buffer);
2017 queue->rx->page_buffer = NULL;
2022 xsk_pool_set_rxq_info(pool, &queue->rx->xdp_rxq_zc);
2025 tsnep_queue_disable(queue);
2027 queue->tx->xsk_pool = pool;
2028 queue->rx->xsk_pool = pool;
2031 tsnep_rx_reopen_xsk(queue->rx);
2032 tsnep_queue_enable(queue);
2038 void tsnep_disable_xsk(struct tsnep_queue *queue)
2040 bool running = netif_running(queue->adapter->netdev);
2043 tsnep_queue_disable(queue);
2045 tsnep_rx_free_zc(queue->rx);
2047 queue->rx->xsk_pool = NULL;
2048 queue->tx->xsk_pool = NULL;
2051 tsnep_rx_reopen(queue->rx);
2052 tsnep_queue_enable(queue);
2055 kfree(queue->rx->xdp_batch);
2056 queue->rx->xdp_batch = NULL;
2057 kfree(queue->rx->page_buffer);
2058 queue->rx->page_buffer = NULL;
2061 static netdev_tx_t tsnep_netdev_xmit_frame(struct sk_buff *skb,
2062 struct net_device *netdev)
2064 struct tsnep_adapter *adapter = netdev_priv(netdev);
2065 u16 queue_mapping = skb_get_queue_mapping(skb);
2067 if (queue_mapping >= adapter->num_tx_queues)
2070 return tsnep_xmit_frame_ring(skb, &adapter->tx[queue_mapping]);
2073 static int tsnep_netdev_ioctl(struct net_device *netdev, struct ifreq *ifr,
2076 if (!netif_running(netdev))
2078 if (cmd == SIOCSHWTSTAMP || cmd == SIOCGHWTSTAMP)
2079 return tsnep_ptp_ioctl(netdev, ifr, cmd);
2080 return phy_mii_ioctl(netdev->phydev, ifr, cmd);
2083 static void tsnep_netdev_set_multicast(struct net_device *netdev)
2085 struct tsnep_adapter *adapter = netdev_priv(netdev);
2089 /* configured MAC address and broadcasts are never filtered */
2090 if (netdev->flags & IFF_PROMISC) {
2091 rx_filter |= TSNEP_RX_FILTER_ACCEPT_ALL_MULTICASTS;
2092 rx_filter |= TSNEP_RX_FILTER_ACCEPT_ALL_UNICASTS;
2093 } else if (!netdev_mc_empty(netdev) || (netdev->flags & IFF_ALLMULTI)) {
2094 rx_filter |= TSNEP_RX_FILTER_ACCEPT_ALL_MULTICASTS;
2096 iowrite16(rx_filter, adapter->addr + TSNEP_RX_FILTER);
2099 static void tsnep_netdev_get_stats64(struct net_device *netdev,
2100 struct rtnl_link_stats64 *stats)
2102 struct tsnep_adapter *adapter = netdev_priv(netdev);
2107 for (i = 0; i < adapter->num_tx_queues; i++) {
2108 stats->tx_packets += adapter->tx[i].packets;
2109 stats->tx_bytes += adapter->tx[i].bytes;
2110 stats->tx_dropped += adapter->tx[i].dropped;
2112 for (i = 0; i < adapter->num_rx_queues; i++) {
2113 stats->rx_packets += adapter->rx[i].packets;
2114 stats->rx_bytes += adapter->rx[i].bytes;
2115 stats->rx_dropped += adapter->rx[i].dropped;
2116 stats->multicast += adapter->rx[i].multicast;
2118 reg = ioread32(adapter->addr + TSNEP_QUEUE(i) +
2119 TSNEP_RX_STATISTIC);
2120 val = (reg & TSNEP_RX_STATISTIC_NO_DESC_MASK) >>
2121 TSNEP_RX_STATISTIC_NO_DESC_SHIFT;
2122 stats->rx_dropped += val;
2123 val = (reg & TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_MASK) >>
2124 TSNEP_RX_STATISTIC_BUFFER_TOO_SMALL_SHIFT;
2125 stats->rx_dropped += val;
2126 val = (reg & TSNEP_RX_STATISTIC_FIFO_OVERFLOW_MASK) >>
2127 TSNEP_RX_STATISTIC_FIFO_OVERFLOW_SHIFT;
2128 stats->rx_errors += val;
2129 stats->rx_fifo_errors += val;
2130 val = (reg & TSNEP_RX_STATISTIC_INVALID_FRAME_MASK) >>
2131 TSNEP_RX_STATISTIC_INVALID_FRAME_SHIFT;
2132 stats->rx_errors += val;
2133 stats->rx_frame_errors += val;
2136 reg = ioread32(adapter->addr + ECM_STAT);
2137 val = (reg & ECM_STAT_RX_ERR_MASK) >> ECM_STAT_RX_ERR_SHIFT;
2138 stats->rx_errors += val;
2139 val = (reg & ECM_STAT_INV_FRM_MASK) >> ECM_STAT_INV_FRM_SHIFT;
2140 stats->rx_errors += val;
2141 stats->rx_crc_errors += val;
2142 val = (reg & ECM_STAT_FWD_RX_ERR_MASK) >> ECM_STAT_FWD_RX_ERR_SHIFT;
2143 stats->rx_errors += val;
2146 static void tsnep_mac_set_address(struct tsnep_adapter *adapter, u8 *addr)
2148 iowrite32(*(u32 *)addr, adapter->addr + TSNEP_MAC_ADDRESS_LOW);
2149 iowrite16(*(u16 *)(addr + sizeof(u32)),
2150 adapter->addr + TSNEP_MAC_ADDRESS_HIGH);
2152 ether_addr_copy(adapter->mac_address, addr);
2153 netif_info(adapter, drv, adapter->netdev, "MAC address set to %pM\n",
2157 static int tsnep_netdev_set_mac_address(struct net_device *netdev, void *addr)
2159 struct tsnep_adapter *adapter = netdev_priv(netdev);
2160 struct sockaddr *sock_addr = addr;
2163 retval = eth_prepare_mac_addr_change(netdev, sock_addr);
2166 eth_hw_addr_set(netdev, sock_addr->sa_data);
2167 tsnep_mac_set_address(adapter, sock_addr->sa_data);
2172 static int tsnep_netdev_set_features(struct net_device *netdev,
2173 netdev_features_t features)
2175 struct tsnep_adapter *adapter = netdev_priv(netdev);
2176 netdev_features_t changed = netdev->features ^ features;
2180 if (changed & NETIF_F_LOOPBACK) {
2181 enable = !!(features & NETIF_F_LOOPBACK);
2182 retval = tsnep_phy_loopback(adapter, enable);
2188 static ktime_t tsnep_netdev_get_tstamp(struct net_device *netdev,
2189 const struct skb_shared_hwtstamps *hwtstamps,
2192 struct tsnep_rx_inline *rx_inline = hwtstamps->netdev_data;
2196 timestamp = __le64_to_cpu(rx_inline->counter);
2198 timestamp = __le64_to_cpu(rx_inline->timestamp);
2200 return ns_to_ktime(timestamp);
2203 static int tsnep_netdev_bpf(struct net_device *dev, struct netdev_bpf *bpf)
2205 struct tsnep_adapter *adapter = netdev_priv(dev);
2207 switch (bpf->command) {
2208 case XDP_SETUP_PROG:
2209 return tsnep_xdp_setup_prog(adapter, bpf->prog, bpf->extack);
2210 case XDP_SETUP_XSK_POOL:
2211 return tsnep_xdp_setup_pool(adapter, bpf->xsk.pool,
2218 static struct tsnep_tx *tsnep_xdp_get_tx(struct tsnep_adapter *adapter, u32 cpu)
2220 if (cpu >= TSNEP_MAX_QUEUES)
2221 cpu &= TSNEP_MAX_QUEUES - 1;
2223 while (cpu >= adapter->num_tx_queues)
2224 cpu -= adapter->num_tx_queues;
2226 return &adapter->tx[cpu];
2229 static int tsnep_netdev_xdp_xmit(struct net_device *dev, int n,
2230 struct xdp_frame **xdp, u32 flags)
2232 struct tsnep_adapter *adapter = netdev_priv(dev);
2233 u32 cpu = smp_processor_id();
2234 struct netdev_queue *nq;
2235 struct tsnep_tx *tx;
2239 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2242 tx = tsnep_xdp_get_tx(adapter, cpu);
2243 nq = netdev_get_tx_queue(adapter->netdev, tx->queue_index);
2245 __netif_tx_lock(nq, cpu);
2247 for (nxmit = 0; nxmit < n; nxmit++) {
2248 xmit = tsnep_xdp_xmit_frame_ring(xdp[nxmit], tx,
2249 TSNEP_TX_TYPE_XDP_NDO);
2253 /* avoid transmit queue timeout since we share it with the slow
2256 txq_trans_cond_update(nq);
2259 if (flags & XDP_XMIT_FLUSH)
2260 tsnep_xdp_xmit_flush(tx);
2262 __netif_tx_unlock(nq);
2267 static int tsnep_netdev_xsk_wakeup(struct net_device *dev, u32 queue_id,
2270 struct tsnep_adapter *adapter = netdev_priv(dev);
2271 struct tsnep_queue *queue;
2273 if (queue_id >= adapter->num_rx_queues ||
2274 queue_id >= adapter->num_tx_queues)
2277 queue = &adapter->queue[queue_id];
2279 if (!napi_if_scheduled_mark_missed(&queue->napi))
2280 napi_schedule(&queue->napi);
2285 static const struct net_device_ops tsnep_netdev_ops = {
2286 .ndo_open = tsnep_netdev_open,
2287 .ndo_stop = tsnep_netdev_close,
2288 .ndo_start_xmit = tsnep_netdev_xmit_frame,
2289 .ndo_eth_ioctl = tsnep_netdev_ioctl,
2290 .ndo_set_rx_mode = tsnep_netdev_set_multicast,
2291 .ndo_get_stats64 = tsnep_netdev_get_stats64,
2292 .ndo_set_mac_address = tsnep_netdev_set_mac_address,
2293 .ndo_set_features = tsnep_netdev_set_features,
2294 .ndo_get_tstamp = tsnep_netdev_get_tstamp,
2295 .ndo_setup_tc = tsnep_tc_setup,
2296 .ndo_bpf = tsnep_netdev_bpf,
2297 .ndo_xdp_xmit = tsnep_netdev_xdp_xmit,
2298 .ndo_xsk_wakeup = tsnep_netdev_xsk_wakeup,
2301 static int tsnep_mac_init(struct tsnep_adapter *adapter)
2305 /* initialize RX filtering, at least configured MAC address and
2306 * broadcast are not filtered
2308 iowrite16(0, adapter->addr + TSNEP_RX_FILTER);
2310 /* try to get MAC address in the following order:
2312 * - valid MAC address already set
2313 * - MAC address register if valid
2314 * - random MAC address
2316 retval = of_get_mac_address(adapter->pdev->dev.of_node,
2317 adapter->mac_address);
2318 if (retval == -EPROBE_DEFER)
2320 if (retval && !is_valid_ether_addr(adapter->mac_address)) {
2321 *(u32 *)adapter->mac_address =
2322 ioread32(adapter->addr + TSNEP_MAC_ADDRESS_LOW);
2323 *(u16 *)(adapter->mac_address + sizeof(u32)) =
2324 ioread16(adapter->addr + TSNEP_MAC_ADDRESS_HIGH);
2325 if (!is_valid_ether_addr(adapter->mac_address))
2326 eth_random_addr(adapter->mac_address);
2329 tsnep_mac_set_address(adapter, adapter->mac_address);
2330 eth_hw_addr_set(adapter->netdev, adapter->mac_address);
2335 static int tsnep_mdio_init(struct tsnep_adapter *adapter)
2337 struct device_node *np = adapter->pdev->dev.of_node;
2341 np = of_get_child_by_name(np, "mdio");
2345 adapter->suppress_preamble =
2346 of_property_read_bool(np, "suppress-preamble");
2349 adapter->mdiobus = devm_mdiobus_alloc(&adapter->pdev->dev);
2350 if (!adapter->mdiobus) {
2356 adapter->mdiobus->priv = (void *)adapter;
2357 adapter->mdiobus->parent = &adapter->pdev->dev;
2358 adapter->mdiobus->read = tsnep_mdiobus_read;
2359 adapter->mdiobus->write = tsnep_mdiobus_write;
2360 adapter->mdiobus->name = TSNEP "-mdiobus";
2361 snprintf(adapter->mdiobus->id, MII_BUS_ID_SIZE, "%s",
2362 adapter->pdev->name);
2364 /* do not scan broadcast address */
2365 adapter->mdiobus->phy_mask = 0x0000001;
2367 retval = of_mdiobus_register(adapter->mdiobus, np);
2375 static int tsnep_phy_init(struct tsnep_adapter *adapter)
2377 struct device_node *phy_node;
2380 retval = of_get_phy_mode(adapter->pdev->dev.of_node,
2381 &adapter->phy_mode);
2383 adapter->phy_mode = PHY_INTERFACE_MODE_GMII;
2385 phy_node = of_parse_phandle(adapter->pdev->dev.of_node, "phy-handle",
2387 adapter->phydev = of_phy_find_device(phy_node);
2388 of_node_put(phy_node);
2389 if (!adapter->phydev && adapter->mdiobus)
2390 adapter->phydev = phy_find_first(adapter->mdiobus);
2391 if (!adapter->phydev)
2397 static int tsnep_queue_init(struct tsnep_adapter *adapter, int queue_count)
2399 u32 irq_mask = ECM_INT_TX_0 | ECM_INT_RX_0;
2404 /* one TX/RX queue pair for netdev is mandatory */
2405 if (platform_irq_count(adapter->pdev) == 1)
2406 retval = platform_get_irq(adapter->pdev, 0);
2408 retval = platform_get_irq_byname(adapter->pdev, "mac");
2411 adapter->num_tx_queues = 1;
2412 adapter->num_rx_queues = 1;
2413 adapter->num_queues = 1;
2414 adapter->queue[0].adapter = adapter;
2415 adapter->queue[0].irq = retval;
2416 adapter->queue[0].tx = &adapter->tx[0];
2417 adapter->queue[0].tx->adapter = adapter;
2418 adapter->queue[0].tx->addr = adapter->addr + TSNEP_QUEUE(0);
2419 adapter->queue[0].tx->queue_index = 0;
2420 adapter->queue[0].rx = &adapter->rx[0];
2421 adapter->queue[0].rx->adapter = adapter;
2422 adapter->queue[0].rx->addr = adapter->addr + TSNEP_QUEUE(0);
2423 adapter->queue[0].rx->queue_index = 0;
2424 adapter->queue[0].irq_mask = irq_mask;
2425 adapter->queue[0].irq_delay_addr = adapter->addr + ECM_INT_DELAY;
2426 retval = tsnep_set_irq_coalesce(&adapter->queue[0],
2427 TSNEP_COALESCE_USECS_DEFAULT);
2431 adapter->netdev->irq = adapter->queue[0].irq;
2433 /* add additional TX/RX queue pairs only if dedicated interrupt is
2436 for (i = 1; i < queue_count; i++) {
2437 sprintf(name, "txrx-%d", i);
2438 retval = platform_get_irq_byname_optional(adapter->pdev, name);
2442 adapter->num_tx_queues++;
2443 adapter->num_rx_queues++;
2444 adapter->num_queues++;
2445 adapter->queue[i].adapter = adapter;
2446 adapter->queue[i].irq = retval;
2447 adapter->queue[i].tx = &adapter->tx[i];
2448 adapter->queue[i].tx->adapter = adapter;
2449 adapter->queue[i].tx->addr = adapter->addr + TSNEP_QUEUE(i);
2450 adapter->queue[i].tx->queue_index = i;
2451 adapter->queue[i].rx = &adapter->rx[i];
2452 adapter->queue[i].rx->adapter = adapter;
2453 adapter->queue[i].rx->addr = adapter->addr + TSNEP_QUEUE(i);
2454 adapter->queue[i].rx->queue_index = i;
2455 adapter->queue[i].irq_mask =
2456 irq_mask << (ECM_INT_TXRX_SHIFT * i);
2457 adapter->queue[i].irq_delay_addr =
2458 adapter->addr + ECM_INT_DELAY + ECM_INT_DELAY_OFFSET * i;
2459 retval = tsnep_set_irq_coalesce(&adapter->queue[i],
2460 TSNEP_COALESCE_USECS_DEFAULT);
2468 static int tsnep_probe(struct platform_device *pdev)
2470 struct tsnep_adapter *adapter;
2471 struct net_device *netdev;
2472 struct resource *io;
2479 netdev = devm_alloc_etherdev_mqs(&pdev->dev,
2480 sizeof(struct tsnep_adapter),
2481 TSNEP_MAX_QUEUES, TSNEP_MAX_QUEUES);
2484 SET_NETDEV_DEV(netdev, &pdev->dev);
2485 adapter = netdev_priv(netdev);
2486 platform_set_drvdata(pdev, adapter);
2487 adapter->pdev = pdev;
2488 adapter->dmadev = &pdev->dev;
2489 adapter->netdev = netdev;
2490 adapter->msg_enable = NETIF_MSG_DRV | NETIF_MSG_PROBE |
2491 NETIF_MSG_LINK | NETIF_MSG_IFUP |
2492 NETIF_MSG_IFDOWN | NETIF_MSG_TX_QUEUED;
2494 netdev->min_mtu = ETH_MIN_MTU;
2495 netdev->max_mtu = TSNEP_MAX_FRAME_SIZE;
2497 mutex_init(&adapter->gate_control_lock);
2498 mutex_init(&adapter->rxnfc_lock);
2499 INIT_LIST_HEAD(&adapter->rxnfc_rules);
2501 io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2502 adapter->addr = devm_ioremap_resource(&pdev->dev, io);
2503 if (IS_ERR(adapter->addr))
2504 return PTR_ERR(adapter->addr);
2505 netdev->mem_start = io->start;
2506 netdev->mem_end = io->end;
2508 type = ioread32(adapter->addr + ECM_TYPE);
2509 revision = (type & ECM_REVISION_MASK) >> ECM_REVISION_SHIFT;
2510 version = (type & ECM_VERSION_MASK) >> ECM_VERSION_SHIFT;
2511 queue_count = (type & ECM_QUEUE_COUNT_MASK) >> ECM_QUEUE_COUNT_SHIFT;
2512 adapter->gate_control = type & ECM_GATE_CONTROL;
2513 adapter->rxnfc_max = TSNEP_RX_ASSIGN_ETHER_TYPE_COUNT;
2515 tsnep_disable_irq(adapter, ECM_INT_ALL);
2517 retval = tsnep_queue_init(adapter, queue_count);
2521 retval = dma_set_mask_and_coherent(&adapter->pdev->dev,
2524 dev_err(&adapter->pdev->dev, "no usable DMA configuration.\n");
2528 retval = tsnep_mac_init(adapter);
2532 retval = tsnep_mdio_init(adapter);
2534 goto mdio_init_failed;
2536 retval = tsnep_phy_init(adapter);
2538 goto phy_init_failed;
2540 retval = tsnep_ptp_init(adapter);
2542 goto ptp_init_failed;
2544 retval = tsnep_tc_init(adapter);
2546 goto tc_init_failed;
2548 retval = tsnep_rxnfc_init(adapter);
2550 goto rxnfc_init_failed;
2552 netdev->netdev_ops = &tsnep_netdev_ops;
2553 netdev->ethtool_ops = &tsnep_ethtool_ops;
2554 netdev->features = NETIF_F_SG;
2555 netdev->hw_features = netdev->features | NETIF_F_LOOPBACK;
2557 netdev->xdp_features = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT |
2558 NETDEV_XDP_ACT_NDO_XMIT |
2559 NETDEV_XDP_ACT_NDO_XMIT_SG |
2560 NETDEV_XDP_ACT_XSK_ZEROCOPY;
2562 /* carrier off reporting is important to ethtool even BEFORE open */
2563 netif_carrier_off(netdev);
2565 retval = register_netdev(netdev);
2567 goto register_failed;
2569 dev_info(&adapter->pdev->dev, "device version %d.%02d\n", version,
2571 if (adapter->gate_control)
2572 dev_info(&adapter->pdev->dev, "gate control detected\n");
2577 tsnep_rxnfc_cleanup(adapter);
2579 tsnep_tc_cleanup(adapter);
2581 tsnep_ptp_cleanup(adapter);
2584 if (adapter->mdiobus)
2585 mdiobus_unregister(adapter->mdiobus);
2590 static int tsnep_remove(struct platform_device *pdev)
2592 struct tsnep_adapter *adapter = platform_get_drvdata(pdev);
2594 unregister_netdev(adapter->netdev);
2596 tsnep_rxnfc_cleanup(adapter);
2598 tsnep_tc_cleanup(adapter);
2600 tsnep_ptp_cleanup(adapter);
2602 if (adapter->mdiobus)
2603 mdiobus_unregister(adapter->mdiobus);
2605 tsnep_disable_irq(adapter, ECM_INT_ALL);
2610 static const struct of_device_id tsnep_of_match[] = {
2611 { .compatible = "engleder,tsnep", },
2614 MODULE_DEVICE_TABLE(of, tsnep_of_match);
2616 static struct platform_driver tsnep_driver = {
2619 .of_match_table = tsnep_of_match,
2621 .probe = tsnep_probe,
2622 .remove = tsnep_remove,
2624 module_platform_driver(tsnep_driver);
2626 MODULE_AUTHOR("Gerhard Engleder <gerhard@engleder-embedded.com>");
2627 MODULE_DESCRIPTION("TSN endpoint Ethernet MAC driver");
2628 MODULE_LICENSE("GPL");