hc
2023-12-09 b22da3d8526a935aa31e086e63f60ff3246cb61c
kernel/drivers/net/ethernet/marvell/mvneta.c
....@@ -27,6 +27,7 @@
2727 #include <linux/of_irq.h>
2828 #include <linux/of_mdio.h>
2929 #include <linux/of_net.h>
30
+#include <linux/phy/phy.h>
3031 #include <linux/phy.h>
3132 #include <linux/phylink.h>
3233 #include <linux/platform_device.h>
....@@ -36,6 +37,8 @@
3637 #include <net/ip.h>
3738 #include <net/ipv6.h>
3839 #include <net/tso.h>
40
+#include <net/page_pool.h>
41
+#include <linux/bpf_trace.h>
3942
4043 /* Registers */
4144 #define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
....@@ -103,9 +106,11 @@
103106 #define MVNETA_TX_IN_PRGRS BIT(0)
104107 #define MVNETA_TX_FIFO_EMPTY BIT(8)
105108 #define MVNETA_RX_MIN_FRAME_SIZE 0x247c
109
+/* Only exists on Armada XP and Armada 370 */
106110 #define MVNETA_SERDES_CFG 0x24A0
107111 #define MVNETA_SGMII_SERDES_PROTO 0x0cc7
108112 #define MVNETA_QSGMII_SERDES_PROTO 0x0667
113
+#define MVNETA_HSGMII_SERDES_PROTO 0x1107
109114 #define MVNETA_TYPE_PRIO 0x24bc
110115 #define MVNETA_FORCE_UNI BIT(21)
111116 #define MVNETA_TXQ_CMD_1 0x24e4
....@@ -221,6 +226,8 @@
221226 #define MVNETA_GMAC_AN_FLOW_CTRL_EN BIT(11)
222227 #define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
223228 #define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
229
+#define MVNETA_GMAC_CTRL_4 0x2c90
230
+#define MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE BIT(1)
224231 #define MVNETA_MIB_COUNTERS_BASE 0x3000
225232 #define MVNETA_MIB_LATE_COLLISION 0x7c
226233 #define MVNETA_DA_FILT_SPEC_MCAST 0x3400
....@@ -319,6 +326,12 @@
319326 ETH_HLEN + ETH_FCS_LEN, \
320327 cache_line_size())
321328
329
+/* Driver assumes that the last 3 bits are 0 */
330
+#define MVNETA_SKB_HEADROOM ALIGN(max(NET_SKB_PAD, XDP_PACKET_HEADROOM), 8)
331
+#define MVNETA_SKB_PAD (SKB_DATA_ALIGN(sizeof(struct skb_shared_info) + \
332
+ MVNETA_SKB_HEADROOM))
333
+#define MVNETA_MAX_RX_BUF_SIZE (PAGE_SIZE - MVNETA_SKB_PAD)
334
+
322335 #define IS_TSO_HEADER(txq, addr) \
323336 ((addr >= txq->tso_hdrs_phys) && \
324337 (addr < txq->tso_hdrs_phys + txq->size * TSO_HEADER_SIZE))
....@@ -330,6 +343,13 @@
330343 ETHTOOL_STAT_EEE_WAKEUP,
331344 ETHTOOL_STAT_SKB_ALLOC_ERR,
332345 ETHTOOL_STAT_REFILL_ERR,
346
+ ETHTOOL_XDP_REDIRECT,
347
+ ETHTOOL_XDP_PASS,
348
+ ETHTOOL_XDP_DROP,
349
+ ETHTOOL_XDP_TX,
350
+ ETHTOOL_XDP_TX_ERR,
351
+ ETHTOOL_XDP_XMIT,
352
+ ETHTOOL_XDP_XMIT_ERR,
333353 ETHTOOL_MAX_STATS,
334354 };
335355
....@@ -342,6 +362,11 @@
342362 #define T_REG_32 32
343363 #define T_REG_64 64
344364 #define T_SW 1
365
+
366
+#define MVNETA_XDP_PASS 0
367
+#define MVNETA_XDP_DROPPED BIT(0)
368
+#define MVNETA_XDP_TX BIT(1)
369
+#define MVNETA_XDP_REDIR BIT(2)
345370
346371 static const struct mvneta_statistic mvneta_statistics[] = {
347372 { 0x3000, T_REG_64, "good_octets_received", },
....@@ -379,16 +404,42 @@
379404 { ETHTOOL_STAT_EEE_WAKEUP, T_SW, "eee_wakeup_errors", },
380405 { ETHTOOL_STAT_SKB_ALLOC_ERR, T_SW, "skb_alloc_errors", },
381406 { ETHTOOL_STAT_REFILL_ERR, T_SW, "refill_errors", },
407
+ { ETHTOOL_XDP_REDIRECT, T_SW, "rx_xdp_redirect", },
408
+ { ETHTOOL_XDP_PASS, T_SW, "rx_xdp_pass", },
409
+ { ETHTOOL_XDP_DROP, T_SW, "rx_xdp_drop", },
410
+ { ETHTOOL_XDP_TX, T_SW, "rx_xdp_tx", },
411
+ { ETHTOOL_XDP_TX_ERR, T_SW, "rx_xdp_tx_errors", },
412
+ { ETHTOOL_XDP_XMIT, T_SW, "tx_xdp_xmit", },
413
+ { ETHTOOL_XDP_XMIT_ERR, T_SW, "tx_xdp_xmit_errors", },
414
+};
415
+
416
+struct mvneta_stats {
417
+ u64 rx_packets;
418
+ u64 rx_bytes;
419
+ u64 tx_packets;
420
+ u64 tx_bytes;
421
+ /* xdp */
422
+ u64 xdp_redirect;
423
+ u64 xdp_pass;
424
+ u64 xdp_drop;
425
+ u64 xdp_xmit;
426
+ u64 xdp_xmit_err;
427
+ u64 xdp_tx;
428
+ u64 xdp_tx_err;
429
+};
430
+
431
+struct mvneta_ethtool_stats {
432
+ struct mvneta_stats ps;
433
+ u64 skb_alloc_error;
434
+ u64 refill_error;
382435 };
383436
384437 struct mvneta_pcpu_stats {
385
- struct u64_stats_sync syncp;
386
- u64 rx_packets;
387
- u64 rx_bytes;
438
+ struct u64_stats_sync syncp;
439
+
440
+ struct mvneta_ethtool_stats es;
388441 u64 rx_dropped;
389442 u64 rx_errors;
390
- u64 tx_packets;
391
- u64 tx_bytes;
392443 };
393444
394445 struct mvneta_pcpu_port {
....@@ -402,10 +453,16 @@
402453 u32 cause_rx_tx;
403454 };
404455
456
+enum {
457
+ __MVNETA_DOWN,
458
+};
459
+
405460 struct mvneta_port {
406461 u8 id;
407462 struct mvneta_pcpu_port __percpu *ports;
408463 struct mvneta_pcpu_stats __percpu *stats;
464
+
465
+ unsigned long state;
409466
410467 int pkt_size;
411468 void __iomem *base;
....@@ -424,6 +481,8 @@
424481 u32 cause_rx_tx;
425482 struct napi_struct napi;
426483
484
+ struct bpf_prog *xdp_prog;
485
+
427486 /* Core clock */
428487 struct clk *clk;
429488 /* AXI clock */
....@@ -436,6 +495,8 @@
436495 struct device_node *dn;
437496 unsigned int tx_csum_limit;
438497 struct phylink *phylink;
498
+ struct phylink_config phylink_config;
499
+ struct phy *comphy;
439500
440501 struct mvneta_bm *bm_priv;
441502 struct mvneta_bm_pool *pool_long;
....@@ -493,7 +554,7 @@
493554 #if defined(__LITTLE_ENDIAN)
494555 struct mvneta_tx_desc {
495556 u32 command; /* Options used by HW for packet transmitting.*/
496
- u16 reserverd1; /* csum_l4 (for future use) */
557
+ u16 reserved1; /* csum_l4 (for future use) */
497558 u16 data_size; /* Data size of transmitted packet in bytes */
498559 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
499560 u32 reserved2; /* hw_cmd - (for future use, PMT) */
....@@ -518,7 +579,7 @@
518579 #else
519580 struct mvneta_tx_desc {
520581 u16 data_size; /* Data size of transmitted packet in bytes */
521
- u16 reserverd1; /* csum_l4 (for future use) */
582
+ u16 reserved1; /* csum_l4 (for future use) */
522583 u32 command; /* Options used by HW for packet transmitting.*/
523584 u32 reserved2; /* hw_cmd - (for future use, PMT) */
524585 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
....@@ -542,6 +603,20 @@
542603 };
543604 #endif
544605
606
+enum mvneta_tx_buf_type {
607
+ MVNETA_TYPE_SKB,
608
+ MVNETA_TYPE_XDP_TX,
609
+ MVNETA_TYPE_XDP_NDO,
610
+};
611
+
612
+struct mvneta_tx_buf {
613
+ enum mvneta_tx_buf_type type;
614
+ union {
615
+ struct xdp_frame *xdpf;
616
+ struct sk_buff *skb;
617
+ };
618
+};
619
+
545620 struct mvneta_tx_queue {
546621 /* Number of this TX queue, in the range 0-7 */
547622 u8 id;
....@@ -557,8 +632,8 @@
557632 int tx_stop_threshold;
558633 int tx_wake_threshold;
559634
560
- /* Array of transmitted skb */
561
- struct sk_buff **tx_skb;
635
+ /* Array of transmitted buffers */
636
+ struct mvneta_tx_buf *buf;
562637
563638 /* Index of last TX DMA descriptor that was inserted */
564639 int txq_put_index;
....@@ -600,6 +675,10 @@
600675 u32 pkts_coal;
601676 u32 time_coal;
602677
678
+ /* page_pool */
679
+ struct page_pool *page_pool;
680
+ struct xdp_rxq_info xdp_rxq;
681
+
603682 /* Virtual address of the RX buffer */
604683 void **buf_virt_addr;
605684
....@@ -618,14 +697,6 @@
618697 /* Index of first RX DMA descriptor to refill */
619698 int first_to_refill;
620699 u32 refill_num;
621
-
622
- /* pointer to uncomplete skb buffer */
623
- struct sk_buff *skb;
624
- int left_size;
625
-
626
- /* error counters */
627
- u32 skb_alloc_err;
628
- u32 refill_err;
629700 };
630701
631702 static enum cpuhp_state online_hpstate;
....@@ -638,7 +709,6 @@
638709 static int rxq_def;
639710
640711 static int rx_copybreak __read_mostly = 256;
641
-static int rx_header_size __read_mostly = 128;
642712
643713 /* HW BM need that each port be identify by a unique ID */
644714 static int global_port_id;
....@@ -681,13 +751,12 @@
681751 static void mvneta_mib_counters_clear(struct mvneta_port *pp)
682752 {
683753 int i;
684
- u32 dummy;
685754
686755 /* Perform dummy reads from MIB counters */
687756 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
688
- dummy = mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
689
- dummy = mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
690
- dummy = mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
757
+ mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
758
+ mvreg_read(pp, MVNETA_RX_DISCARD_FRAME_COUNT);
759
+ mvreg_read(pp, MVNETA_OVERRUN_FRAME_COUNT);
691760 }
692761
693762 /* Get System Network Statistics */
....@@ -711,12 +780,12 @@
711780 cpu_stats = per_cpu_ptr(pp->stats, cpu);
712781 do {
713782 start = u64_stats_fetch_begin_irq(&cpu_stats->syncp);
714
- rx_packets = cpu_stats->rx_packets;
715
- rx_bytes = cpu_stats->rx_bytes;
783
+ rx_packets = cpu_stats->es.ps.rx_packets;
784
+ rx_bytes = cpu_stats->es.ps.rx_bytes;
716785 rx_dropped = cpu_stats->rx_dropped;
717786 rx_errors = cpu_stats->rx_errors;
718
- tx_packets = cpu_stats->tx_packets;
719
- tx_bytes = cpu_stats->tx_bytes;
787
+ tx_packets = cpu_stats->es.ps.tx_packets;
788
+ tx_bytes = cpu_stats->es.ps.tx_bytes;
720789 } while (u64_stats_fetch_retry_irq(&cpu_stats->syncp, start));
721790
722791 stats->rx_packets += rx_packets;
....@@ -1119,7 +1188,7 @@
11191188 SKB_DATA_ALIGN(MVNETA_RX_BUF_SIZE(bm_pool->pkt_size));
11201189
11211190 /* Fill entire long pool */
1122
- num = hwbm_pool_add(hwbm_pool, hwbm_pool->size, GFP_ATOMIC);
1191
+ num = hwbm_pool_add(hwbm_pool, hwbm_pool->size);
11231192 if (num != hwbm_pool->size) {
11241193 WARN(1, "pool %d: %d of %d allocated\n",
11251194 bm_pool->id, num, hwbm_pool->size);
....@@ -1134,6 +1203,7 @@
11341203 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_short, 1 << pp->id);
11351204
11361205 pp->bm_priv = NULL;
1206
+ pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
11371207 mvreg_write(pp, MVNETA_ACC_MODE, MVNETA_ACC_MODE_EXT1);
11381208 netdev_info(pp->dev, "fail to update MTU, fall back to software BM\n");
11391209 }
....@@ -1761,30 +1831,34 @@
17611831 /* Free tx queue skbuffs */
17621832 static void mvneta_txq_bufs_free(struct mvneta_port *pp,
17631833 struct mvneta_tx_queue *txq, int num,
1764
- struct netdev_queue *nq)
1834
+ struct netdev_queue *nq, bool napi)
17651835 {
17661836 unsigned int bytes_compl = 0, pkts_compl = 0;
17671837 int i;
17681838
17691839 for (i = 0; i < num; i++) {
1840
+ struct mvneta_tx_buf *buf = &txq->buf[txq->txq_get_index];
17701841 struct mvneta_tx_desc *tx_desc = txq->descs +
17711842 txq->txq_get_index;
1772
- struct sk_buff *skb = txq->tx_skb[txq->txq_get_index];
1773
-
1774
- if (skb) {
1775
- bytes_compl += skb->len;
1776
- pkts_compl++;
1777
- }
17781843
17791844 mvneta_txq_inc_get(txq);
17801845
1781
- if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr))
1846
+ if (!IS_TSO_HEADER(txq, tx_desc->buf_phys_addr) &&
1847
+ buf->type != MVNETA_TYPE_XDP_TX)
17821848 dma_unmap_single(pp->dev->dev.parent,
17831849 tx_desc->buf_phys_addr,
17841850 tx_desc->data_size, DMA_TO_DEVICE);
1785
- if (!skb)
1786
- continue;
1787
- dev_kfree_skb_any(skb);
1851
+ if (buf->type == MVNETA_TYPE_SKB && buf->skb) {
1852
+ bytes_compl += buf->skb->len;
1853
+ pkts_compl++;
1854
+ dev_kfree_skb_any(buf->skb);
1855
+ } else if (buf->type == MVNETA_TYPE_XDP_TX ||
1856
+ buf->type == MVNETA_TYPE_XDP_NDO) {
1857
+ if (napi && buf->type == MVNETA_TYPE_XDP_TX)
1858
+ xdp_return_frame_rx_napi(buf->xdpf);
1859
+ else
1860
+ xdp_return_frame(buf->xdpf);
1861
+ }
17881862 }
17891863
17901864 netdev_tx_completed_queue(nq, pkts_compl, bytes_compl);
....@@ -1801,7 +1875,7 @@
18011875 if (!tx_done)
18021876 return;
18031877
1804
- mvneta_txq_bufs_free(pp, txq, tx_done, nq);
1878
+ mvneta_txq_bufs_free(pp, txq, tx_done, nq, true);
18051879
18061880 txq->count -= tx_done;
18071881
....@@ -1821,20 +1895,14 @@
18211895 dma_addr_t phys_addr;
18221896 struct page *page;
18231897
1824
- page = __dev_alloc_page(gfp_mask);
1898
+ page = page_pool_alloc_pages(rxq->page_pool,
1899
+ gfp_mask | __GFP_NOWARN);
18251900 if (!page)
18261901 return -ENOMEM;
18271902
1828
- /* map page for use */
1829
- phys_addr = dma_map_page(pp->dev->dev.parent, page, 0, PAGE_SIZE,
1830
- DMA_FROM_DEVICE);
1831
- if (unlikely(dma_mapping_error(pp->dev->dev.parent, phys_addr))) {
1832
- __free_page(page);
1833
- return -ENOMEM;
1834
- }
1835
-
1836
- phys_addr += pp->rx_offset_correction;
1903
+ phys_addr = page_pool_get_dma_addr(page) + pp->rx_offset_correction;
18371904 mvneta_rx_desc_fill(rx_desc, phys_addr, page, rxq);
1905
+
18381906 return 0;
18391907 }
18401908
....@@ -1900,10 +1968,28 @@
19001968 if (!data || !(rx_desc->buf_phys_addr))
19011969 continue;
19021970
1903
- dma_unmap_page(pp->dev->dev.parent, rx_desc->buf_phys_addr,
1904
- PAGE_SIZE, DMA_FROM_DEVICE);
1905
- __free_page(data);
1971
+ page_pool_put_full_page(rxq->page_pool, data, false);
19061972 }
1973
+ if (xdp_rxq_info_is_reg(&rxq->xdp_rxq))
1974
+ xdp_rxq_info_unreg(&rxq->xdp_rxq);
1975
+ page_pool_destroy(rxq->page_pool);
1976
+ rxq->page_pool = NULL;
1977
+}
1978
+
1979
+static void
1980
+mvneta_update_stats(struct mvneta_port *pp,
1981
+ struct mvneta_stats *ps)
1982
+{
1983
+ struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
1984
+
1985
+ u64_stats_update_begin(&stats->syncp);
1986
+ stats->es.ps.rx_packets += ps->rx_packets;
1987
+ stats->es.ps.rx_bytes += ps->rx_bytes;
1988
+ /* xdp */
1989
+ stats->es.ps.xdp_redirect += ps->xdp_redirect;
1990
+ stats->es.ps.xdp_pass += ps->xdp_pass;
1991
+ stats->es.ps.xdp_drop += ps->xdp_drop;
1992
+ u64_stats_update_end(&stats->syncp);
19071993 }
19081994
19091995 static inline
....@@ -1917,9 +2003,15 @@
19172003 rx_desc = rxq->descs + curr_desc;
19182004 if (!(rx_desc->buf_phys_addr)) {
19192005 if (mvneta_rx_refill(pp, rx_desc, rxq, GFP_ATOMIC)) {
2006
+ struct mvneta_pcpu_stats *stats;
2007
+
19202008 pr_err("Can't refill queue %d. Done %d from %d\n",
19212009 rxq->id, i, rxq->refill_num);
1922
- rxq->refill_err++;
2010
+
2011
+ stats = this_cpu_ptr(pp->stats);
2012
+ u64_stats_update_begin(&stats->syncp);
2013
+ stats->es.refill_error++;
2014
+ u64_stats_update_end(&stats->syncp);
19232015 break;
19242016 }
19252017 }
....@@ -1931,38 +2023,346 @@
19312023 return i;
19322024 }
19332025
2026
+static void
2027
+mvneta_xdp_put_buff(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2028
+ struct xdp_buff *xdp, int sync_len, bool napi)
2029
+{
2030
+ struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2031
+ int i;
2032
+
2033
+ for (i = 0; i < sinfo->nr_frags; i++)
2034
+ page_pool_put_full_page(rxq->page_pool,
2035
+ skb_frag_page(&sinfo->frags[i]), napi);
2036
+ page_pool_put_page(rxq->page_pool, virt_to_head_page(xdp->data),
2037
+ sync_len, napi);
2038
+}
2039
+
2040
+static int
2041
+mvneta_xdp_submit_frame(struct mvneta_port *pp, struct mvneta_tx_queue *txq,
2042
+ struct xdp_frame *xdpf, bool dma_map)
2043
+{
2044
+ struct mvneta_tx_desc *tx_desc;
2045
+ struct mvneta_tx_buf *buf;
2046
+ dma_addr_t dma_addr;
2047
+
2048
+ if (txq->count >= txq->tx_stop_threshold)
2049
+ return MVNETA_XDP_DROPPED;
2050
+
2051
+ tx_desc = mvneta_txq_next_desc_get(txq);
2052
+
2053
+ buf = &txq->buf[txq->txq_put_index];
2054
+ if (dma_map) {
2055
+ /* ndo_xdp_xmit */
2056
+ dma_addr = dma_map_single(pp->dev->dev.parent, xdpf->data,
2057
+ xdpf->len, DMA_TO_DEVICE);
2058
+ if (dma_mapping_error(pp->dev->dev.parent, dma_addr)) {
2059
+ mvneta_txq_desc_put(txq);
2060
+ return MVNETA_XDP_DROPPED;
2061
+ }
2062
+ buf->type = MVNETA_TYPE_XDP_NDO;
2063
+ } else {
2064
+ struct page *page = virt_to_page(xdpf->data);
2065
+
2066
+ dma_addr = page_pool_get_dma_addr(page) +
2067
+ sizeof(*xdpf) + xdpf->headroom;
2068
+ dma_sync_single_for_device(pp->dev->dev.parent, dma_addr,
2069
+ xdpf->len, DMA_BIDIRECTIONAL);
2070
+ buf->type = MVNETA_TYPE_XDP_TX;
2071
+ }
2072
+ buf->xdpf = xdpf;
2073
+
2074
+ tx_desc->command = MVNETA_TXD_FLZ_DESC;
2075
+ tx_desc->buf_phys_addr = dma_addr;
2076
+ tx_desc->data_size = xdpf->len;
2077
+
2078
+ mvneta_txq_inc_put(txq);
2079
+ txq->pending++;
2080
+ txq->count++;
2081
+
2082
+ return MVNETA_XDP_TX;
2083
+}
2084
+
2085
+static int
2086
+mvneta_xdp_xmit_back(struct mvneta_port *pp, struct xdp_buff *xdp)
2087
+{
2088
+ struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2089
+ struct mvneta_tx_queue *txq;
2090
+ struct netdev_queue *nq;
2091
+ struct xdp_frame *xdpf;
2092
+ int cpu;
2093
+ u32 ret;
2094
+
2095
+ xdpf = xdp_convert_buff_to_frame(xdp);
2096
+ if (unlikely(!xdpf))
2097
+ return MVNETA_XDP_DROPPED;
2098
+
2099
+ cpu = smp_processor_id();
2100
+ txq = &pp->txqs[cpu % txq_number];
2101
+ nq = netdev_get_tx_queue(pp->dev, txq->id);
2102
+
2103
+ __netif_tx_lock(nq, cpu);
2104
+ ret = mvneta_xdp_submit_frame(pp, txq, xdpf, false);
2105
+ if (ret == MVNETA_XDP_TX) {
2106
+ u64_stats_update_begin(&stats->syncp);
2107
+ stats->es.ps.tx_bytes += xdpf->len;
2108
+ stats->es.ps.tx_packets++;
2109
+ stats->es.ps.xdp_tx++;
2110
+ u64_stats_update_end(&stats->syncp);
2111
+
2112
+ mvneta_txq_pend_desc_add(pp, txq, 0);
2113
+ } else {
2114
+ u64_stats_update_begin(&stats->syncp);
2115
+ stats->es.ps.xdp_tx_err++;
2116
+ u64_stats_update_end(&stats->syncp);
2117
+ }
2118
+ __netif_tx_unlock(nq);
2119
+
2120
+ return ret;
2121
+}
2122
+
2123
+static int
2124
+mvneta_xdp_xmit(struct net_device *dev, int num_frame,
2125
+ struct xdp_frame **frames, u32 flags)
2126
+{
2127
+ struct mvneta_port *pp = netdev_priv(dev);
2128
+ struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2129
+ int i, nxmit_byte = 0, nxmit = num_frame;
2130
+ int cpu = smp_processor_id();
2131
+ struct mvneta_tx_queue *txq;
2132
+ struct netdev_queue *nq;
2133
+ u32 ret;
2134
+
2135
+ if (unlikely(test_bit(__MVNETA_DOWN, &pp->state)))
2136
+ return -ENETDOWN;
2137
+
2138
+ if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK))
2139
+ return -EINVAL;
2140
+
2141
+ txq = &pp->txqs[cpu % txq_number];
2142
+ nq = netdev_get_tx_queue(pp->dev, txq->id);
2143
+
2144
+ __netif_tx_lock(nq, cpu);
2145
+ for (i = 0; i < num_frame; i++) {
2146
+ ret = mvneta_xdp_submit_frame(pp, txq, frames[i], true);
2147
+ if (ret == MVNETA_XDP_TX) {
2148
+ nxmit_byte += frames[i]->len;
2149
+ } else {
2150
+ xdp_return_frame_rx_napi(frames[i]);
2151
+ nxmit--;
2152
+ }
2153
+ }
2154
+
2155
+ if (unlikely(flags & XDP_XMIT_FLUSH))
2156
+ mvneta_txq_pend_desc_add(pp, txq, 0);
2157
+ __netif_tx_unlock(nq);
2158
+
2159
+ u64_stats_update_begin(&stats->syncp);
2160
+ stats->es.ps.tx_bytes += nxmit_byte;
2161
+ stats->es.ps.tx_packets += nxmit;
2162
+ stats->es.ps.xdp_xmit += nxmit;
2163
+ stats->es.ps.xdp_xmit_err += num_frame - nxmit;
2164
+ u64_stats_update_end(&stats->syncp);
2165
+
2166
+ return nxmit;
2167
+}
2168
+
2169
+static int
2170
+mvneta_run_xdp(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2171
+ struct bpf_prog *prog, struct xdp_buff *xdp,
2172
+ u32 frame_sz, struct mvneta_stats *stats)
2173
+{
2174
+ unsigned int len, data_len, sync;
2175
+ u32 ret, act;
2176
+
2177
+ len = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2178
+ data_len = xdp->data_end - xdp->data;
2179
+ act = bpf_prog_run_xdp(prog, xdp);
2180
+
2181
+ /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */
2182
+ sync = xdp->data_end - xdp->data_hard_start - pp->rx_offset_correction;
2183
+ sync = max(sync, len);
2184
+
2185
+ switch (act) {
2186
+ case XDP_PASS:
2187
+ stats->xdp_pass++;
2188
+ return MVNETA_XDP_PASS;
2189
+ case XDP_REDIRECT: {
2190
+ int err;
2191
+
2192
+ err = xdp_do_redirect(pp->dev, xdp, prog);
2193
+ if (unlikely(err)) {
2194
+ mvneta_xdp_put_buff(pp, rxq, xdp, sync, true);
2195
+ ret = MVNETA_XDP_DROPPED;
2196
+ } else {
2197
+ ret = MVNETA_XDP_REDIR;
2198
+ stats->xdp_redirect++;
2199
+ }
2200
+ break;
2201
+ }
2202
+ case XDP_TX:
2203
+ ret = mvneta_xdp_xmit_back(pp, xdp);
2204
+ if (ret != MVNETA_XDP_TX)
2205
+ mvneta_xdp_put_buff(pp, rxq, xdp, sync, true);
2206
+ break;
2207
+ default:
2208
+ bpf_warn_invalid_xdp_action(act);
2209
+ fallthrough;
2210
+ case XDP_ABORTED:
2211
+ trace_xdp_exception(pp->dev, prog, act);
2212
+ fallthrough;
2213
+ case XDP_DROP:
2214
+ mvneta_xdp_put_buff(pp, rxq, xdp, sync, true);
2215
+ ret = MVNETA_XDP_DROPPED;
2216
+ stats->xdp_drop++;
2217
+ break;
2218
+ }
2219
+
2220
+ stats->rx_bytes += frame_sz + xdp->data_end - xdp->data - data_len;
2221
+ stats->rx_packets++;
2222
+
2223
+ return ret;
2224
+}
2225
+
2226
+static void
2227
+mvneta_swbm_rx_frame(struct mvneta_port *pp,
2228
+ struct mvneta_rx_desc *rx_desc,
2229
+ struct mvneta_rx_queue *rxq,
2230
+ struct xdp_buff *xdp, int *size,
2231
+ struct page *page)
2232
+{
2233
+ unsigned char *data = page_address(page);
2234
+ int data_len = -MVNETA_MH_SIZE, len;
2235
+ struct net_device *dev = pp->dev;
2236
+ enum dma_data_direction dma_dir;
2237
+ struct skb_shared_info *sinfo;
2238
+
2239
+ if (*size > MVNETA_MAX_RX_BUF_SIZE) {
2240
+ len = MVNETA_MAX_RX_BUF_SIZE;
2241
+ data_len += len;
2242
+ } else {
2243
+ len = *size;
2244
+ data_len += len - ETH_FCS_LEN;
2245
+ }
2246
+ *size = *size - len;
2247
+
2248
+ dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2249
+ dma_sync_single_for_cpu(dev->dev.parent,
2250
+ rx_desc->buf_phys_addr,
2251
+ len, dma_dir);
2252
+
2253
+ rx_desc->buf_phys_addr = 0;
2254
+
2255
+ /* Prefetch header */
2256
+ prefetch(data);
2257
+
2258
+ xdp->data_hard_start = data;
2259
+ xdp->data = data + pp->rx_offset_correction + MVNETA_MH_SIZE;
2260
+ xdp->data_end = xdp->data + data_len;
2261
+ xdp_set_data_meta_invalid(xdp);
2262
+
2263
+ sinfo = xdp_get_shared_info_from_buff(xdp);
2264
+ sinfo->nr_frags = 0;
2265
+}
2266
+
2267
+static void
2268
+mvneta_swbm_add_rx_fragment(struct mvneta_port *pp,
2269
+ struct mvneta_rx_desc *rx_desc,
2270
+ struct mvneta_rx_queue *rxq,
2271
+ struct xdp_buff *xdp, int *size,
2272
+ struct page *page)
2273
+{
2274
+ struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2275
+ struct net_device *dev = pp->dev;
2276
+ enum dma_data_direction dma_dir;
2277
+ int data_len, len;
2278
+
2279
+ if (*size > MVNETA_MAX_RX_BUF_SIZE) {
2280
+ len = MVNETA_MAX_RX_BUF_SIZE;
2281
+ data_len = len;
2282
+ } else {
2283
+ len = *size;
2284
+ data_len = len - ETH_FCS_LEN;
2285
+ }
2286
+ dma_dir = page_pool_get_dma_dir(rxq->page_pool);
2287
+ dma_sync_single_for_cpu(dev->dev.parent,
2288
+ rx_desc->buf_phys_addr,
2289
+ len, dma_dir);
2290
+ rx_desc->buf_phys_addr = 0;
2291
+
2292
+ if (data_len > 0 && sinfo->nr_frags < MAX_SKB_FRAGS) {
2293
+ skb_frag_t *frag = &sinfo->frags[sinfo->nr_frags];
2294
+
2295
+ skb_frag_off_set(frag, pp->rx_offset_correction);
2296
+ skb_frag_size_set(frag, data_len);
2297
+ __skb_frag_set_page(frag, page);
2298
+ sinfo->nr_frags++;
2299
+ } else {
2300
+ page_pool_put_full_page(rxq->page_pool, page, true);
2301
+ }
2302
+ *size -= len;
2303
+}
2304
+
2305
+static struct sk_buff *
2306
+mvneta_swbm_build_skb(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
2307
+ struct xdp_buff *xdp, u32 desc_status)
2308
+{
2309
+ struct skb_shared_info *sinfo = xdp_get_shared_info_from_buff(xdp);
2310
+ int i, num_frags = sinfo->nr_frags;
2311
+ struct sk_buff *skb;
2312
+
2313
+ skb = build_skb(xdp->data_hard_start, PAGE_SIZE);
2314
+ if (!skb)
2315
+ return ERR_PTR(-ENOMEM);
2316
+
2317
+ page_pool_release_page(rxq->page_pool, virt_to_page(xdp->data));
2318
+
2319
+ skb_reserve(skb, xdp->data - xdp->data_hard_start);
2320
+ skb_put(skb, xdp->data_end - xdp->data);
2321
+ mvneta_rx_csum(pp, desc_status, skb);
2322
+
2323
+ for (i = 0; i < num_frags; i++) {
2324
+ skb_frag_t *frag = &sinfo->frags[i];
2325
+
2326
+ skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags,
2327
+ skb_frag_page(frag), skb_frag_off(frag),
2328
+ skb_frag_size(frag), PAGE_SIZE);
2329
+ page_pool_release_page(rxq->page_pool, skb_frag_page(frag));
2330
+ }
2331
+
2332
+ return skb;
2333
+}
2334
+
19342335 /* Main rx processing when using software buffer management */
19352336 static int mvneta_rx_swbm(struct napi_struct *napi,
19362337 struct mvneta_port *pp, int budget,
19372338 struct mvneta_rx_queue *rxq)
19382339 {
2340
+ int rx_proc = 0, rx_todo, refill, size = 0;
19392341 struct net_device *dev = pp->dev;
1940
- int rx_todo, rx_proc;
1941
- int refill = 0;
1942
- u32 rcvd_pkts = 0;
1943
- u32 rcvd_bytes = 0;
2342
+ struct xdp_buff xdp_buf = {
2343
+ .frame_sz = PAGE_SIZE,
2344
+ .rxq = &rxq->xdp_rxq,
2345
+ };
2346
+ struct mvneta_stats ps = {};
2347
+ struct bpf_prog *xdp_prog;
2348
+ u32 desc_status, frame_sz;
19442349
19452350 /* Get number of received packets */
19462351 rx_todo = mvneta_rxq_busy_desc_num_get(pp, rxq);
1947
- rx_proc = 0;
2352
+
2353
+ rcu_read_lock();
2354
+ xdp_prog = READ_ONCE(pp->xdp_prog);
19482355
19492356 /* Fairness NAPI loop */
1950
- while ((rcvd_pkts < budget) && (rx_proc < rx_todo)) {
2357
+ while (rx_proc < budget && rx_proc < rx_todo) {
19512358 struct mvneta_rx_desc *rx_desc = mvneta_rxq_next_desc_get(rxq);
1952
- unsigned char *data;
1953
- struct page *page;
1954
- dma_addr_t phys_addr;
19552359 u32 rx_status, index;
1956
- int rx_bytes, skb_size, copy_size;
1957
- int frag_num, frag_size, frag_offset;
2360
+ struct sk_buff *skb;
2361
+ struct page *page;
19582362
19592363 index = rx_desc - rxq->descs;
19602364 page = (struct page *)rxq->buf_virt_addr[index];
1961
- data = page_address(page);
1962
- /* Prefetch header */
1963
- prefetch(data);
19642365
1965
- phys_addr = rx_desc->buf_phys_addr;
19662366 rx_status = rx_desc->status;
19672367 rx_proc++;
19682368 rxq->refill_num++;
....@@ -1971,132 +2371,72 @@
19712371 /* Check errors only for FIRST descriptor */
19722372 if (rx_status & MVNETA_RXD_ERR_SUMMARY) {
19732373 mvneta_rx_error(pp, rx_desc);
1974
- /* leave the descriptor untouched */
1975
- continue;
2374
+ goto next;
19762375 }
1977
- rx_bytes = rx_desc->data_size -
1978
- (ETH_FCS_LEN + MVNETA_MH_SIZE);
19792376
1980
- /* Allocate small skb for each new packet */
1981
- skb_size = max(rx_copybreak, rx_header_size);
1982
- rxq->skb = netdev_alloc_skb_ip_align(dev, skb_size);
1983
- if (unlikely(!rxq->skb)) {
1984
- struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2377
+ size = rx_desc->data_size;
2378
+ frame_sz = size - ETH_FCS_LEN;
2379
+ desc_status = rx_status;
19852380
1986
- netdev_err(dev,
1987
- "Can't allocate skb on queue %d\n",
1988
- rxq->id);
1989
-
1990
- rxq->skb_alloc_err++;
1991
-
1992
- u64_stats_update_begin(&stats->syncp);
1993
- stats->rx_dropped++;
1994
- u64_stats_update_end(&stats->syncp);
1995
- continue;
1996
- }
1997
- copy_size = min(skb_size, rx_bytes);
1998
-
1999
- /* Copy data from buffer to SKB, skip Marvell header */
2000
- memcpy(rxq->skb->data, data + MVNETA_MH_SIZE,
2001
- copy_size);
2002
- skb_put(rxq->skb, copy_size);
2003
- rxq->left_size = rx_bytes - copy_size;
2004
-
2005
- mvneta_rx_csum(pp, rx_status, rxq->skb);
2006
- if (rxq->left_size == 0) {
2007
- int size = copy_size + MVNETA_MH_SIZE;
2008
-
2009
- dma_sync_single_range_for_cpu(dev->dev.parent,
2010
- phys_addr, 0,
2011
- size,
2012
- DMA_FROM_DEVICE);
2013
-
2014
- /* leave the descriptor and buffer untouched */
2015
- } else {
2016
- /* refill descriptor with new buffer later */
2017
- rx_desc->buf_phys_addr = 0;
2018
-
2019
- frag_num = 0;
2020
- frag_offset = copy_size + MVNETA_MH_SIZE;
2021
- frag_size = min(rxq->left_size,
2022
- (int)(PAGE_SIZE - frag_offset));
2023
- skb_add_rx_frag(rxq->skb, frag_num, page,
2024
- frag_offset, frag_size,
2025
- PAGE_SIZE);
2026
- dma_unmap_page(dev->dev.parent, phys_addr,
2027
- PAGE_SIZE, DMA_FROM_DEVICE);
2028
- rxq->left_size -= frag_size;
2029
- }
2381
+ mvneta_swbm_rx_frame(pp, rx_desc, rxq, &xdp_buf,
2382
+ &size, page);
20302383 } else {
2031
- /* Middle or Last descriptor */
2032
- if (unlikely(!rxq->skb)) {
2033
- pr_debug("no skb for rx_status 0x%x\n",
2034
- rx_status);
2384
+ if (unlikely(!xdp_buf.data_hard_start)) {
2385
+ rx_desc->buf_phys_addr = 0;
2386
+ page_pool_put_full_page(rxq->page_pool, page,
2387
+ true);
20352388 continue;
20362389 }
2037
- if (!rxq->left_size) {
2038
- /* last descriptor has only FCS */
2039
- /* and can be discarded */
2040
- dma_sync_single_range_for_cpu(dev->dev.parent,
2041
- phys_addr, 0,
2042
- ETH_FCS_LEN,
2043
- DMA_FROM_DEVICE);
2044
- /* leave the descriptor and buffer untouched */
2045
- } else {
2046
- /* refill descriptor with new buffer later */
2047
- rx_desc->buf_phys_addr = 0;
20482390
2049
- frag_num = skb_shinfo(rxq->skb)->nr_frags;
2050
- frag_offset = 0;
2051
- frag_size = min(rxq->left_size,
2052
- (int)(PAGE_SIZE - frag_offset));
2053
- skb_add_rx_frag(rxq->skb, frag_num, page,
2054
- frag_offset, frag_size,
2055
- PAGE_SIZE);
2056
-
2057
- dma_unmap_page(dev->dev.parent, phys_addr,
2058
- PAGE_SIZE, DMA_FROM_DEVICE);
2059
-
2060
- rxq->left_size -= frag_size;
2061
- }
2391
+ mvneta_swbm_add_rx_fragment(pp, rx_desc, rxq, &xdp_buf,
2392
+ &size, page);
20622393 } /* Middle or Last descriptor */
20632394
20642395 if (!(rx_status & MVNETA_RXD_LAST_DESC))
20652396 /* no last descriptor this time */
20662397 continue;
20672398
2068
- if (rxq->left_size) {
2069
- pr_err("get last desc, but left_size (%d) != 0\n",
2070
- rxq->left_size);
2071
- dev_kfree_skb_any(rxq->skb);
2072
- rxq->left_size = 0;
2073
- rxq->skb = NULL;
2074
- continue;
2399
+ if (size) {
2400
+ mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1, true);
2401
+ goto next;
20752402 }
2076
- rcvd_pkts++;
2077
- rcvd_bytes += rxq->skb->len;
20782403
2079
- /* Linux processing */
2080
- rxq->skb->protocol = eth_type_trans(rxq->skb, dev);
2404
+ if (xdp_prog &&
2405
+ mvneta_run_xdp(pp, rxq, xdp_prog, &xdp_buf, frame_sz, &ps))
2406
+ goto next;
20812407
2082
- if (dev->features & NETIF_F_GRO)
2083
- napi_gro_receive(napi, rxq->skb);
2084
- else
2085
- netif_receive_skb(rxq->skb);
2408
+ skb = mvneta_swbm_build_skb(pp, rxq, &xdp_buf, desc_status);
2409
+ if (IS_ERR(skb)) {
2410
+ struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
20862411
2087
- /* clean uncomplete skb pointer in queue */
2088
- rxq->skb = NULL;
2089
- rxq->left_size = 0;
2412
+ mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1, true);
2413
+
2414
+ u64_stats_update_begin(&stats->syncp);
2415
+ stats->es.skb_alloc_error++;
2416
+ stats->rx_dropped++;
2417
+ u64_stats_update_end(&stats->syncp);
2418
+
2419
+ goto next;
2420
+ }
2421
+
2422
+ ps.rx_bytes += skb->len;
2423
+ ps.rx_packets++;
2424
+
2425
+ skb->protocol = eth_type_trans(skb, dev);
2426
+ napi_gro_receive(napi, skb);
2427
+next:
2428
+ xdp_buf.data_hard_start = NULL;
20902429 }
2430
+ rcu_read_unlock();
20912431
2092
- if (rcvd_pkts) {
2093
- struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
2432
+ if (xdp_buf.data_hard_start)
2433
+ mvneta_xdp_put_buff(pp, rxq, &xdp_buf, -1, true);
20942434
2095
- u64_stats_update_begin(&stats->syncp);
2096
- stats->rx_packets += rcvd_pkts;
2097
- stats->rx_bytes += rcvd_bytes;
2098
- u64_stats_update_end(&stats->syncp);
2099
- }
2435
+ if (ps.xdp_redirect)
2436
+ xdp_do_flush_map();
2437
+
2438
+ if (ps.rx_packets)
2439
+ mvneta_update_stats(pp, &ps);
21002440
21012441 /* return some buffers to hardware queue, one at a time is too slow */
21022442 refill = mvneta_rx_refill_queue(pp, rxq);
....@@ -2104,7 +2444,7 @@
21042444 /* Update rxq management counters */
21052445 mvneta_rxq_desc_num_update(pp, rxq, rx_proc, refill);
21062446
2107
- return rcvd_pkts;
2447
+ return ps.rx_packets;
21082448 }
21092449
21102450 /* Main rx processing when using hardware buffer management */
....@@ -2188,8 +2528,15 @@
21882528 /* Refill processing */
21892529 err = hwbm_pool_refill(&bm_pool->hwbm_pool, GFP_ATOMIC);
21902530 if (err) {
2531
+ struct mvneta_pcpu_stats *stats;
2532
+
21912533 netdev_err(dev, "Linux processing - Can't refill\n");
2192
- rxq->refill_err++;
2534
+
2535
+ stats = this_cpu_ptr(pp->stats);
2536
+ u64_stats_update_begin(&stats->syncp);
2537
+ stats->es.refill_error++;
2538
+ u64_stats_update_end(&stats->syncp);
2539
+
21932540 goto err_drop_frame_ret_pool;
21942541 }
21952542
....@@ -2223,8 +2570,8 @@
22232570 struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
22242571
22252572 u64_stats_update_begin(&stats->syncp);
2226
- stats->rx_packets += rcvd_pkts;
2227
- stats->rx_bytes += rcvd_bytes;
2573
+ stats->es.ps.rx_packets += rcvd_pkts;
2574
+ stats->es.ps.rx_bytes += rcvd_bytes;
22282575 u64_stats_update_end(&stats->syncp);
22292576 }
22302577
....@@ -2238,16 +2585,19 @@
22382585 mvneta_tso_put_hdr(struct sk_buff *skb,
22392586 struct mvneta_port *pp, struct mvneta_tx_queue *txq)
22402587 {
2241
- struct mvneta_tx_desc *tx_desc;
22422588 int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
2589
+ struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
2590
+ struct mvneta_tx_desc *tx_desc;
22432591
2244
- txq->tx_skb[txq->txq_put_index] = NULL;
22452592 tx_desc = mvneta_txq_next_desc_get(txq);
22462593 tx_desc->data_size = hdr_len;
22472594 tx_desc->command = mvneta_skb_tx_csum(pp, skb);
22482595 tx_desc->command |= MVNETA_TXD_F_DESC;
22492596 tx_desc->buf_phys_addr = txq->tso_hdrs_phys +
22502597 txq->txq_put_index * TSO_HEADER_SIZE;
2598
+ buf->type = MVNETA_TYPE_SKB;
2599
+ buf->skb = NULL;
2600
+
22512601 mvneta_txq_inc_put(txq);
22522602 }
22532603
....@@ -2256,6 +2606,7 @@
22562606 struct sk_buff *skb, char *data, int size,
22572607 bool last_tcp, bool is_last)
22582608 {
2609
+ struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
22592610 struct mvneta_tx_desc *tx_desc;
22602611
22612612 tx_desc = mvneta_txq_next_desc_get(txq);
....@@ -2269,7 +2620,8 @@
22692620 }
22702621
22712622 tx_desc->command = 0;
2272
- txq->tx_skb[txq->txq_put_index] = NULL;
2623
+ buf->type = MVNETA_TYPE_SKB;
2624
+ buf->skb = NULL;
22732625
22742626 if (last_tcp) {
22752627 /* last descriptor in the TCP packet */
....@@ -2277,7 +2629,7 @@
22772629
22782630 /* last descriptor in SKB */
22792631 if (is_last)
2280
- txq->tx_skb[txq->txq_put_index] = skb;
2632
+ buf->skb = skb;
22812633 }
22822634 mvneta_txq_inc_put(txq);
22832635 return 0;
....@@ -2286,11 +2638,10 @@
22862638 static int mvneta_tx_tso(struct sk_buff *skb, struct net_device *dev,
22872639 struct mvneta_tx_queue *txq)
22882640 {
2289
- int total_len, data_left;
2641
+ int hdr_len, total_len, data_left;
22902642 int desc_count = 0;
22912643 struct mvneta_port *pp = netdev_priv(dev);
22922644 struct tso_t tso;
2293
- int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
22942645 int i;
22952646
22962647 /* Count needed descriptors */
....@@ -2303,7 +2654,7 @@
23032654 }
23042655
23052656 /* Initialize the TSO handler, and prepare the first payload */
2306
- tso_start(skb, &tso);
2657
+ hdr_len = tso_start(skb, &tso);
23072658
23082659 total_len = skb->len - hdr_len;
23092660 while (total_len > 0) {
....@@ -2362,11 +2713,12 @@
23622713 int i, nr_frags = skb_shinfo(skb)->nr_frags;
23632714
23642715 for (i = 0; i < nr_frags; i++) {
2716
+ struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
23652717 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2366
- void *addr = page_address(frag->page.p) + frag->page_offset;
2718
+ void *addr = skb_frag_address(frag);
23672719
23682720 tx_desc = mvneta_txq_next_desc_get(txq);
2369
- tx_desc->data_size = frag->size;
2721
+ tx_desc->data_size = skb_frag_size(frag);
23702722
23712723 tx_desc->buf_phys_addr =
23722724 dma_map_single(pp->dev->dev.parent, addr,
....@@ -2381,12 +2733,13 @@
23812733 if (i == nr_frags - 1) {
23822734 /* Last descriptor */
23832735 tx_desc->command = MVNETA_TXD_L_DESC | MVNETA_TXD_Z_PAD;
2384
- txq->tx_skb[txq->txq_put_index] = skb;
2736
+ buf->skb = skb;
23852737 } else {
23862738 /* Descriptor in the middle: Not First, Not Last */
23872739 tx_desc->command = 0;
2388
- txq->tx_skb[txq->txq_put_index] = NULL;
2740
+ buf->skb = NULL;
23892741 }
2742
+ buf->type = MVNETA_TYPE_SKB;
23902743 mvneta_txq_inc_put(txq);
23912744 }
23922745
....@@ -2414,6 +2767,7 @@
24142767 struct mvneta_port *pp = netdev_priv(dev);
24152768 u16 txq_id = skb_get_queue_mapping(skb);
24162769 struct mvneta_tx_queue *txq = &pp->txqs[txq_id];
2770
+ struct mvneta_tx_buf *buf = &txq->buf[txq->txq_put_index];
24172771 struct mvneta_tx_desc *tx_desc;
24182772 int len = skb->len;
24192773 int frags = 0;
....@@ -2446,16 +2800,17 @@
24462800 goto out;
24472801 }
24482802
2803
+ buf->type = MVNETA_TYPE_SKB;
24492804 if (frags == 1) {
24502805 /* First and Last descriptor */
24512806 tx_cmd |= MVNETA_TXD_FLZ_DESC;
24522807 tx_desc->command = tx_cmd;
2453
- txq->tx_skb[txq->txq_put_index] = skb;
2808
+ buf->skb = skb;
24542809 mvneta_txq_inc_put(txq);
24552810 } else {
24562811 /* First but not Last */
24572812 tx_cmd |= MVNETA_TXD_F_DESC;
2458
- txq->tx_skb[txq->txq_put_index] = NULL;
2813
+ buf->skb = NULL;
24592814 mvneta_txq_inc_put(txq);
24602815 tx_desc->command = tx_cmd;
24612816 /* Continue with other skb fragments */
....@@ -2472,8 +2827,8 @@
24722827
24732828 out:
24742829 if (frags > 0) {
2475
- struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
24762830 struct netdev_queue *nq = netdev_get_tx_queue(dev, txq_id);
2831
+ struct mvneta_pcpu_stats *stats = this_cpu_ptr(pp->stats);
24772832
24782833 netdev_tx_sent_queue(nq, len);
24792834
....@@ -2481,15 +2836,15 @@
24812836 if (txq->count >= txq->tx_stop_threshold)
24822837 netif_tx_stop_queue(nq);
24832838
2484
- if (!skb->xmit_more || netif_xmit_stopped(nq) ||
2839
+ if (!netdev_xmit_more() || netif_xmit_stopped(nq) ||
24852840 txq->pending + frags > MVNETA_TXQ_DEC_SENT_MASK)
24862841 mvneta_txq_pend_desc_add(pp, txq, frags);
24872842 else
24882843 txq->pending += frags;
24892844
24902845 u64_stats_update_begin(&stats->syncp);
2491
- stats->tx_packets++;
2492
- stats->tx_bytes += len;
2846
+ stats->es.ps.tx_bytes += len;
2847
+ stats->es.ps.tx_packets++;
24932848 u64_stats_update_end(&stats->syncp);
24942849 } else {
24952850 dev->stats.tx_dropped++;
....@@ -2508,7 +2863,7 @@
25082863 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
25092864 int tx_done = txq->count;
25102865
2511
- mvneta_txq_bufs_free(pp, txq, tx_done, nq);
2866
+ mvneta_txq_bufs_free(pp, txq, tx_done, nq, false);
25122867
25132868 /* reset txq */
25142869 txq->count = 0;
....@@ -2523,12 +2878,13 @@
25232878 {
25242879 struct mvneta_tx_queue *txq;
25252880 struct netdev_queue *nq;
2881
+ int cpu = smp_processor_id();
25262882
25272883 while (cause_tx_done) {
25282884 txq = mvneta_tx_done_policy(pp, cause_tx_done);
25292885
25302886 nq = netdev_get_tx_queue(pp->dev, txq->id);
2531
- __netif_tx_lock(nq, smp_processor_id());
2887
+ __netif_tx_lock(nq, cpu);
25322888
25332889 if (txq->count)
25342890 mvneta_txq_done(pp, txq);
....@@ -2841,11 +3197,57 @@
28413197 return rx_done;
28423198 }
28433199
3200
+static int mvneta_create_page_pool(struct mvneta_port *pp,
3201
+ struct mvneta_rx_queue *rxq, int size)
3202
+{
3203
+ struct bpf_prog *xdp_prog = READ_ONCE(pp->xdp_prog);
3204
+ struct page_pool_params pp_params = {
3205
+ .order = 0,
3206
+ .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV,
3207
+ .pool_size = size,
3208
+ .nid = NUMA_NO_NODE,
3209
+ .dev = pp->dev->dev.parent,
3210
+ .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE,
3211
+ .offset = pp->rx_offset_correction,
3212
+ .max_len = MVNETA_MAX_RX_BUF_SIZE,
3213
+ };
3214
+ int err;
3215
+
3216
+ rxq->page_pool = page_pool_create(&pp_params);
3217
+ if (IS_ERR(rxq->page_pool)) {
3218
+ err = PTR_ERR(rxq->page_pool);
3219
+ rxq->page_pool = NULL;
3220
+ return err;
3221
+ }
3222
+
3223
+ err = xdp_rxq_info_reg(&rxq->xdp_rxq, pp->dev, rxq->id);
3224
+ if (err < 0)
3225
+ goto err_free_pp;
3226
+
3227
+ err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL,
3228
+ rxq->page_pool);
3229
+ if (err)
3230
+ goto err_unregister_rxq;
3231
+
3232
+ return 0;
3233
+
3234
+err_unregister_rxq:
3235
+ xdp_rxq_info_unreg(&rxq->xdp_rxq);
3236
+err_free_pp:
3237
+ page_pool_destroy(rxq->page_pool);
3238
+ rxq->page_pool = NULL;
3239
+ return err;
3240
+}
3241
+
28443242 /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
28453243 static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
28463244 int num)
28473245 {
2848
- int i;
3246
+ int i, err;
3247
+
3248
+ err = mvneta_create_page_pool(pp, rxq, num);
3249
+ if (err < 0)
3250
+ return err;
28493251
28503252 for (i = 0; i < num; i++) {
28513253 memset(rxq->descs + i, 0, sizeof(struct mvneta_rx_desc));
....@@ -2919,7 +3321,7 @@
29193321 /* Set Offset */
29203322 mvneta_rxq_offset_set(pp, rxq, 0);
29213323 mvneta_rxq_buf_size_set(pp, rxq, PAGE_SIZE < SZ_64K ?
2922
- PAGE_SIZE :
3324
+ MVNETA_MAX_RX_BUF_SIZE :
29233325 MVNETA_RX_BUF_SIZE(pp->pkt_size));
29243326 mvneta_rxq_bm_disable(pp, rxq);
29253327 mvneta_rxq_fill(pp, rxq, rxq->size);
....@@ -2958,9 +3360,6 @@
29583360 {
29593361 mvneta_rxq_drop_pkts(pp, rxq);
29603362
2961
- if (rxq->skb)
2962
- dev_kfree_skb_any(rxq->skb);
2963
-
29643363 if (rxq->descs)
29653364 dma_free_coherent(pp->dev->dev.parent,
29663365 rxq->size * MVNETA_DESC_ALIGNED_SIZE,
....@@ -2973,8 +3372,6 @@
29733372 rxq->descs_phys = 0;
29743373 rxq->first_to_refill = 0;
29753374 rxq->refill_num = 0;
2976
- rxq->skb = NULL;
2977
- rxq->left_size = 0;
29783375 }
29793376
29803377 static int mvneta_txq_sw_init(struct mvneta_port *pp,
....@@ -3000,26 +3397,16 @@
30003397
30013398 txq->last_desc = txq->size - 1;
30023399
3003
- txq->tx_skb = kmalloc_array(txq->size, sizeof(*txq->tx_skb),
3004
- GFP_KERNEL);
3005
- if (!txq->tx_skb) {
3006
- dma_free_coherent(pp->dev->dev.parent,
3007
- txq->size * MVNETA_DESC_ALIGNED_SIZE,
3008
- txq->descs, txq->descs_phys);
3400
+ txq->buf = kmalloc_array(txq->size, sizeof(*txq->buf), GFP_KERNEL);
3401
+ if (!txq->buf)
30093402 return -ENOMEM;
3010
- }
30113403
30123404 /* Allocate DMA buffers for TSO MAC/IP/TCP headers */
30133405 txq->tso_hdrs = dma_alloc_coherent(pp->dev->dev.parent,
30143406 txq->size * TSO_HEADER_SIZE,
30153407 &txq->tso_hdrs_phys, GFP_KERNEL);
3016
- if (!txq->tso_hdrs) {
3017
- kfree(txq->tx_skb);
3018
- dma_free_coherent(pp->dev->dev.parent,
3019
- txq->size * MVNETA_DESC_ALIGNED_SIZE,
3020
- txq->descs, txq->descs_phys);
3408
+ if (!txq->tso_hdrs)
30213409 return -ENOMEM;
3022
- }
30233410
30243411 /* Setup XPS mapping */
30253412 if (pp->neta_armada3700)
....@@ -3069,7 +3456,7 @@
30693456 {
30703457 struct netdev_queue *nq = netdev_get_tx_queue(pp->dev, txq->id);
30713458
3072
- kfree(txq->tx_skb);
3459
+ kfree(txq->buf);
30733460
30743461 if (txq->tso_hdrs)
30753462 dma_free_coherent(pp->dev->dev.parent,
....@@ -3163,9 +3550,60 @@
31633550 return 0;
31643551 }
31653552
3553
+static int mvneta_comphy_init(struct mvneta_port *pp, phy_interface_t interface)
3554
+{
3555
+ int ret;
3556
+
3557
+ ret = phy_set_mode_ext(pp->comphy, PHY_MODE_ETHERNET, interface);
3558
+ if (ret)
3559
+ return ret;
3560
+
3561
+ return phy_power_on(pp->comphy);
3562
+}
3563
+
3564
+static int mvneta_config_interface(struct mvneta_port *pp,
3565
+ phy_interface_t interface)
3566
+{
3567
+ int ret = 0;
3568
+
3569
+ if (pp->comphy) {
3570
+ if (interface == PHY_INTERFACE_MODE_SGMII ||
3571
+ interface == PHY_INTERFACE_MODE_1000BASEX ||
3572
+ interface == PHY_INTERFACE_MODE_2500BASEX) {
3573
+ ret = mvneta_comphy_init(pp, interface);
3574
+ }
3575
+ } else {
3576
+ switch (interface) {
3577
+ case PHY_INTERFACE_MODE_QSGMII:
3578
+ mvreg_write(pp, MVNETA_SERDES_CFG,
3579
+ MVNETA_QSGMII_SERDES_PROTO);
3580
+ break;
3581
+
3582
+ case PHY_INTERFACE_MODE_SGMII:
3583
+ case PHY_INTERFACE_MODE_1000BASEX:
3584
+ mvreg_write(pp, MVNETA_SERDES_CFG,
3585
+ MVNETA_SGMII_SERDES_PROTO);
3586
+ break;
3587
+
3588
+ case PHY_INTERFACE_MODE_2500BASEX:
3589
+ mvreg_write(pp, MVNETA_SERDES_CFG,
3590
+ MVNETA_HSGMII_SERDES_PROTO);
3591
+ break;
3592
+ default:
3593
+ break;
3594
+ }
3595
+ }
3596
+
3597
+ pp->phy_interface = interface;
3598
+
3599
+ return ret;
3600
+}
3601
+
31663602 static void mvneta_start_dev(struct mvneta_port *pp)
31673603 {
31683604 int cpu;
3605
+
3606
+ WARN_ON(mvneta_config_interface(pp, pp->phy_interface));
31693607
31703608 mvneta_max_rx_size_set(pp, pp->pkt_size);
31713609 mvneta_txq_max_tx_size_set(pp, pp->pkt_size);
....@@ -3193,12 +3631,23 @@
31933631 MVNETA_CAUSE_LINK_CHANGE);
31943632
31953633 phylink_start(pp->phylink);
3634
+
3635
+ /* We may have called phylink_speed_down before */
3636
+ phylink_speed_up(pp->phylink);
3637
+
31963638 netif_tx_start_all_queues(pp->dev);
3639
+
3640
+ clear_bit(__MVNETA_DOWN, &pp->state);
31973641 }
31983642
31993643 static void mvneta_stop_dev(struct mvneta_port *pp)
32003644 {
32013645 unsigned int cpu;
3646
+
3647
+ set_bit(__MVNETA_DOWN, &pp->state);
3648
+
3649
+ if (device_may_wakeup(&pp->dev->dev))
3650
+ phylink_speed_down(pp->phylink, false);
32023651
32033652 phylink_stop(pp->phylink);
32043653
....@@ -3229,6 +3678,8 @@
32293678
32303679 mvneta_tx_reset(pp);
32313680 mvneta_rx_reset(pp);
3681
+
3682
+ WARN_ON(phy_power_off(pp->comphy));
32323683 }
32333684
32343685 static void mvneta_percpu_enable(void *arg)
....@@ -3255,6 +3706,11 @@
32553706 netdev_info(dev, "Illegal MTU value %d, rounding to %d\n",
32563707 mtu, ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8));
32573708 mtu = ALIGN(MVNETA_RX_PKT_SIZE(mtu), 8);
3709
+ }
3710
+
3711
+ if (pp->xdp_prog && mtu > MVNETA_MAX_RX_BUF_SIZE) {
3712
+ netdev_info(dev, "Illegal MTU value %d for XDP mode\n", mtu);
3713
+ return -EINVAL;
32583714 }
32593715
32603716 dev->mtu = mtu;
....@@ -3351,9 +3807,12 @@
33513807 return 0;
33523808 }
33533809
3354
-static void mvneta_validate(struct net_device *ndev, unsigned long *supported,
3810
+static void mvneta_validate(struct phylink_config *config,
3811
+ unsigned long *supported,
33553812 struct phylink_link_state *state)
33563813 {
3814
+ struct net_device *ndev = to_net_dev(config->dev);
3815
+ struct mvneta_port *pp = netdev_priv(ndev);
33573816 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
33583817
33593818 /* We only support QSGMII, SGMII, 802.3z and RGMII modes */
....@@ -3372,9 +3831,16 @@
33723831
33733832 /* Asymmetric pause is unsupported */
33743833 phylink_set(mask, Pause);
3834
+
33753835 /* Half-duplex at speeds higher than 100Mbit is unsupported */
3376
- phylink_set(mask, 1000baseT_Full);
3377
- phylink_set(mask, 1000baseX_Full);
3836
+ if (pp->comphy || state->interface != PHY_INTERFACE_MODE_2500BASEX) {
3837
+ phylink_set(mask, 1000baseT_Full);
3838
+ phylink_set(mask, 1000baseX_Full);
3839
+ }
3840
+ if (pp->comphy || state->interface == PHY_INTERFACE_MODE_2500BASEX) {
3841
+ phylink_set(mask, 2500baseT_Full);
3842
+ phylink_set(mask, 2500baseX_Full);
3843
+ }
33783844
33793845 if (!phy_interface_mode_is_8023z(state->interface)) {
33803846 /* 10M and 100M are only supported in non-802.3z mode */
....@@ -3388,18 +3854,26 @@
33883854 __ETHTOOL_LINK_MODE_MASK_NBITS);
33893855 bitmap_and(state->advertising, state->advertising, mask,
33903856 __ETHTOOL_LINK_MODE_MASK_NBITS);
3857
+
3858
+ /* We can only operate at 2500BaseX or 1000BaseX. If requested
3859
+ * to advertise both, only report advertising at 2500BaseX.
3860
+ */
3861
+ phylink_helper_basex_speed(state);
33913862 }
33923863
3393
-static int mvneta_mac_link_state(struct net_device *ndev,
3394
- struct phylink_link_state *state)
3864
+static void mvneta_mac_pcs_get_state(struct phylink_config *config,
3865
+ struct phylink_link_state *state)
33953866 {
3867
+ struct net_device *ndev = to_net_dev(config->dev);
33963868 struct mvneta_port *pp = netdev_priv(ndev);
33973869 u32 gmac_stat;
33983870
33993871 gmac_stat = mvreg_read(pp, MVNETA_GMAC_STATUS);
34003872
34013873 if (gmac_stat & MVNETA_GMAC_SPEED_1000)
3402
- state->speed = SPEED_1000;
3874
+ state->speed =
3875
+ state->interface == PHY_INTERFACE_MODE_2500BASEX ?
3876
+ SPEED_2500 : SPEED_1000;
34033877 else if (gmac_stat & MVNETA_GMAC_SPEED_100)
34043878 state->speed = SPEED_100;
34053879 else
....@@ -3414,12 +3888,11 @@
34143888 state->pause |= MLO_PAUSE_RX;
34153889 if (gmac_stat & MVNETA_GMAC_TX_FLOW_CTRL_ENABLE)
34163890 state->pause |= MLO_PAUSE_TX;
3417
-
3418
- return 1;
34193891 }
34203892
3421
-static void mvneta_mac_an_restart(struct net_device *ndev)
3893
+static void mvneta_mac_an_restart(struct phylink_config *config)
34223894 {
3895
+ struct net_device *ndev = to_net_dev(config->dev);
34233896 struct mvneta_port *pp = netdev_priv(ndev);
34243897 u32 gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
34253898
....@@ -3429,28 +3902,27 @@
34293902 gmac_an & ~MVNETA_GMAC_INBAND_RESTART_AN);
34303903 }
34313904
3432
-static void mvneta_mac_config(struct net_device *ndev, unsigned int mode,
3433
- const struct phylink_link_state *state)
3905
+static void mvneta_mac_config(struct phylink_config *config, unsigned int mode,
3906
+ const struct phylink_link_state *state)
34343907 {
3908
+ struct net_device *ndev = to_net_dev(config->dev);
34353909 struct mvneta_port *pp = netdev_priv(ndev);
34363910 u32 new_ctrl0, gmac_ctrl0 = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
34373911 u32 new_ctrl2, gmac_ctrl2 = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
3912
+ u32 new_ctrl4, gmac_ctrl4 = mvreg_read(pp, MVNETA_GMAC_CTRL_4);
34383913 u32 new_clk, gmac_clk = mvreg_read(pp, MVNETA_GMAC_CLOCK_DIVIDER);
34393914 u32 new_an, gmac_an = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
34403915
34413916 new_ctrl0 = gmac_ctrl0 & ~MVNETA_GMAC0_PORT_1000BASE_X;
34423917 new_ctrl2 = gmac_ctrl2 & ~(MVNETA_GMAC2_INBAND_AN_ENABLE |
34433918 MVNETA_GMAC2_PORT_RESET);
3919
+ new_ctrl4 = gmac_ctrl4 & ~(MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE);
34443920 new_clk = gmac_clk & ~MVNETA_GMAC_1MS_CLOCK_ENABLE;
34453921 new_an = gmac_an & ~(MVNETA_GMAC_INBAND_AN_ENABLE |
34463922 MVNETA_GMAC_INBAND_RESTART_AN |
3447
- MVNETA_GMAC_CONFIG_MII_SPEED |
3448
- MVNETA_GMAC_CONFIG_GMII_SPEED |
34493923 MVNETA_GMAC_AN_SPEED_EN |
34503924 MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL |
3451
- MVNETA_GMAC_CONFIG_FLOW_CTRL |
34523925 MVNETA_GMAC_AN_FLOW_CTRL_EN |
3453
- MVNETA_GMAC_CONFIG_FULL_DUPLEX |
34543926 MVNETA_GMAC_AN_DUPLEX_EN);
34553927
34563928 /* Even though it might look weird, when we're configured in
....@@ -3465,24 +3937,20 @@
34653937
34663938 if (phylink_test(state->advertising, Pause))
34673939 new_an |= MVNETA_GMAC_ADVERT_SYM_FLOW_CTRL;
3468
- if (state->pause & MLO_PAUSE_TXRX_MASK)
3469
- new_an |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
34703940
34713941 if (!phylink_autoneg_inband(mode)) {
3472
- /* Phy or fixed speed */
3473
- if (state->duplex)
3474
- new_an |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
3475
-
3476
- if (state->speed == SPEED_1000)
3477
- new_an |= MVNETA_GMAC_CONFIG_GMII_SPEED;
3478
- else if (state->speed == SPEED_100)
3479
- new_an |= MVNETA_GMAC_CONFIG_MII_SPEED;
3942
+ /* Phy or fixed speed - nothing to do, leave the
3943
+ * configured speed, duplex and flow control as-is.
3944
+ */
34803945 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
34813946 /* SGMII mode receives the state from the PHY */
34823947 new_ctrl2 |= MVNETA_GMAC2_INBAND_AN_ENABLE;
34833948 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
34843949 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3485
- MVNETA_GMAC_FORCE_LINK_PASS)) |
3950
+ MVNETA_GMAC_FORCE_LINK_PASS |
3951
+ MVNETA_GMAC_CONFIG_MII_SPEED |
3952
+ MVNETA_GMAC_CONFIG_GMII_SPEED |
3953
+ MVNETA_GMAC_CONFIG_FULL_DUPLEX)) |
34863954 MVNETA_GMAC_INBAND_AN_ENABLE |
34873955 MVNETA_GMAC_AN_SPEED_EN |
34883956 MVNETA_GMAC_AN_DUPLEX_EN;
....@@ -3491,7 +3959,8 @@
34913959 new_ctrl0 |= MVNETA_GMAC0_PORT_1000BASE_X;
34923960 new_clk |= MVNETA_GMAC_1MS_CLOCK_ENABLE;
34933961 new_an = (new_an & ~(MVNETA_GMAC_FORCE_LINK_DOWN |
3494
- MVNETA_GMAC_FORCE_LINK_PASS)) |
3962
+ MVNETA_GMAC_FORCE_LINK_PASS |
3963
+ MVNETA_GMAC_CONFIG_MII_SPEED)) |
34953964 MVNETA_GMAC_INBAND_AN_ENABLE |
34963965 MVNETA_GMAC_CONFIG_GMII_SPEED |
34973966 /* The MAC only supports FD mode */
....@@ -3512,10 +3981,25 @@
35123981 MVNETA_GMAC_FORCE_LINK_DOWN);
35133982 }
35143983
3984
+
3985
+ /* When at 2.5G, the link partner can send frames with shortened
3986
+ * preambles.
3987
+ */
3988
+ if (state->interface == PHY_INTERFACE_MODE_2500BASEX)
3989
+ new_ctrl4 |= MVNETA_GMAC4_SHORT_PREAMBLE_ENABLE;
3990
+
3991
+ if (pp->phy_interface != state->interface) {
3992
+ if (pp->comphy)
3993
+ WARN_ON(phy_power_off(pp->comphy));
3994
+ WARN_ON(mvneta_config_interface(pp, state->interface));
3995
+ }
3996
+
35153997 if (new_ctrl0 != gmac_ctrl0)
35163998 mvreg_write(pp, MVNETA_GMAC_CTRL_0, new_ctrl0);
35173999 if (new_ctrl2 != gmac_ctrl2)
35184000 mvreg_write(pp, MVNETA_GMAC_CTRL_2, new_ctrl2);
4001
+ if (new_ctrl4 != gmac_ctrl4)
4002
+ mvreg_write(pp, MVNETA_GMAC_CTRL_4, new_ctrl4);
35194003 if (new_clk != gmac_clk)
35204004 mvreg_write(pp, MVNETA_GMAC_CLOCK_DIVIDER, new_clk);
35214005 if (new_an != gmac_an)
....@@ -3540,9 +4024,10 @@
35404024 mvreg_write(pp, MVNETA_LPI_CTRL_1, lpi_ctl1);
35414025 }
35424026
3543
-static void mvneta_mac_link_down(struct net_device *ndev, unsigned int mode,
3544
- phy_interface_t interface)
4027
+static void mvneta_mac_link_down(struct phylink_config *config,
4028
+ unsigned int mode, phy_interface_t interface)
35454029 {
4030
+ struct net_device *ndev = to_net_dev(config->dev);
35464031 struct mvneta_port *pp = netdev_priv(ndev);
35474032 u32 val;
35484033
....@@ -3559,17 +4044,48 @@
35594044 mvneta_set_eee(pp, false);
35604045 }
35614046
3562
-static void mvneta_mac_link_up(struct net_device *ndev, unsigned int mode,
3563
- phy_interface_t interface,
3564
- struct phy_device *phy)
4047
+static void mvneta_mac_link_up(struct phylink_config *config,
4048
+ struct phy_device *phy,
4049
+ unsigned int mode, phy_interface_t interface,
4050
+ int speed, int duplex,
4051
+ bool tx_pause, bool rx_pause)
35654052 {
4053
+ struct net_device *ndev = to_net_dev(config->dev);
35664054 struct mvneta_port *pp = netdev_priv(ndev);
35674055 u32 val;
35684056
35694057 if (!phylink_autoneg_inband(mode)) {
35704058 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
3571
- val &= ~MVNETA_GMAC_FORCE_LINK_DOWN;
4059
+ val &= ~(MVNETA_GMAC_FORCE_LINK_DOWN |
4060
+ MVNETA_GMAC_CONFIG_MII_SPEED |
4061
+ MVNETA_GMAC_CONFIG_GMII_SPEED |
4062
+ MVNETA_GMAC_CONFIG_FLOW_CTRL |
4063
+ MVNETA_GMAC_CONFIG_FULL_DUPLEX);
35724064 val |= MVNETA_GMAC_FORCE_LINK_PASS;
4065
+
4066
+ if (speed == SPEED_1000 || speed == SPEED_2500)
4067
+ val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
4068
+ else if (speed == SPEED_100)
4069
+ val |= MVNETA_GMAC_CONFIG_MII_SPEED;
4070
+
4071
+ if (duplex == DUPLEX_FULL)
4072
+ val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
4073
+
4074
+ if (tx_pause || rx_pause)
4075
+ val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
4076
+
4077
+ mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
4078
+ } else {
4079
+ /* When inband doesn't cover flow control or flow control is
4080
+ * disabled, we need to manually configure it. This bit will
4081
+ * only have effect if MVNETA_GMAC_AN_FLOW_CTRL_EN is unset.
4082
+ */
4083
+ val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
4084
+ val &= ~MVNETA_GMAC_CONFIG_FLOW_CTRL;
4085
+
4086
+ if (tx_pause || rx_pause)
4087
+ val |= MVNETA_GMAC_CONFIG_FLOW_CTRL;
4088
+
35734089 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
35744090 }
35754091
....@@ -3583,7 +4099,7 @@
35834099
35844100 static const struct phylink_mac_ops mvneta_phylink_ops = {
35854101 .validate = mvneta_validate,
3586
- .mac_link_state = mvneta_mac_link_state,
4102
+ .mac_pcs_get_state = mvneta_mac_pcs_get_state,
35874103 .mac_an_restart = mvneta_mac_an_restart,
35884104 .mac_config = mvneta_mac_config,
35894105 .mac_link_down = mvneta_mac_link_down,
....@@ -3600,6 +4116,10 @@
36004116
36014117 phylink_ethtool_get_wol(pp->phylink, &wol);
36024118 device_set_wakeup_capable(&pp->dev->dev, !!wol.supported);
4119
+
4120
+ /* PHY WoL may be enabled but device wakeup disabled */
4121
+ if (wol.supported)
4122
+ device_set_wakeup_enable(&pp->dev->dev, !!wol.wolopts);
36034123
36044124 return err;
36054125 }
....@@ -3620,7 +4140,7 @@
36204140 /* Use the cpu associated to the rxq when it is online, in all
36214141 * the other cases, use the cpu 0 which can't be offline.
36224142 */
3623
- if (cpu_online(pp->rxq_def))
4143
+ if (pp->rxq_def < nr_cpu_ids && cpu_online(pp->rxq_def))
36244144 elected_cpu = pp->rxq_def;
36254145
36264146 max_cpu = num_present_cpus();
....@@ -3813,9 +4333,6 @@
38134333 goto err_free_online_hp;
38144334 }
38154335
3816
- /* In default link is down */
3817
- netif_carrier_off(pp->dev);
3818
-
38194336 ret = mvneta_mdio_probe(pp);
38204337 if (ret < 0) {
38214338 netdev_err(dev, "cannot probe MDIO bus\n");
....@@ -3889,6 +4406,48 @@
38894406 struct mvneta_port *pp = netdev_priv(dev);
38904407
38914408 return phylink_mii_ioctl(pp->phylink, ifr, cmd);
4409
+}
4410
+
4411
+static int mvneta_xdp_setup(struct net_device *dev, struct bpf_prog *prog,
4412
+ struct netlink_ext_ack *extack)
4413
+{
4414
+ bool need_update, running = netif_running(dev);
4415
+ struct mvneta_port *pp = netdev_priv(dev);
4416
+ struct bpf_prog *old_prog;
4417
+
4418
+ if (prog && dev->mtu > MVNETA_MAX_RX_BUF_SIZE) {
4419
+ NL_SET_ERR_MSG_MOD(extack, "MTU too large for XDP");
4420
+ return -EOPNOTSUPP;
4421
+ }
4422
+
4423
+ if (pp->bm_priv) {
4424
+ NL_SET_ERR_MSG_MOD(extack,
4425
+ "Hardware Buffer Management not supported on XDP");
4426
+ return -EOPNOTSUPP;
4427
+ }
4428
+
4429
+ need_update = !!pp->xdp_prog != !!prog;
4430
+ if (running && need_update)
4431
+ mvneta_stop(dev);
4432
+
4433
+ old_prog = xchg(&pp->xdp_prog, prog);
4434
+ if (old_prog)
4435
+ bpf_prog_put(old_prog);
4436
+
4437
+ if (running && need_update)
4438
+ return mvneta_open(dev);
4439
+
4440
+ return 0;
4441
+}
4442
+
4443
+static int mvneta_xdp(struct net_device *dev, struct netdev_bpf *xdp)
4444
+{
4445
+ switch (xdp->command) {
4446
+ case XDP_SETUP_PROG:
4447
+ return mvneta_xdp_setup(dev, xdp->prog, xdp->extack);
4448
+ default:
4449
+ return -EINVAL;
4450
+ }
38924451 }
38934452
38944453 /* Ethtool methods */
....@@ -4037,45 +4596,112 @@
40374596 }
40384597 }
40394598
4599
+static void
4600
+mvneta_ethtool_update_pcpu_stats(struct mvneta_port *pp,
4601
+ struct mvneta_ethtool_stats *es)
4602
+{
4603
+ unsigned int start;
4604
+ int cpu;
4605
+
4606
+ for_each_possible_cpu(cpu) {
4607
+ struct mvneta_pcpu_stats *stats;
4608
+ u64 skb_alloc_error;
4609
+ u64 refill_error;
4610
+ u64 xdp_redirect;
4611
+ u64 xdp_xmit_err;
4612
+ u64 xdp_tx_err;
4613
+ u64 xdp_pass;
4614
+ u64 xdp_drop;
4615
+ u64 xdp_xmit;
4616
+ u64 xdp_tx;
4617
+
4618
+ stats = per_cpu_ptr(pp->stats, cpu);
4619
+ do {
4620
+ start = u64_stats_fetch_begin_irq(&stats->syncp);
4621
+ skb_alloc_error = stats->es.skb_alloc_error;
4622
+ refill_error = stats->es.refill_error;
4623
+ xdp_redirect = stats->es.ps.xdp_redirect;
4624
+ xdp_pass = stats->es.ps.xdp_pass;
4625
+ xdp_drop = stats->es.ps.xdp_drop;
4626
+ xdp_xmit = stats->es.ps.xdp_xmit;
4627
+ xdp_xmit_err = stats->es.ps.xdp_xmit_err;
4628
+ xdp_tx = stats->es.ps.xdp_tx;
4629
+ xdp_tx_err = stats->es.ps.xdp_tx_err;
4630
+ } while (u64_stats_fetch_retry_irq(&stats->syncp, start));
4631
+
4632
+ es->skb_alloc_error += skb_alloc_error;
4633
+ es->refill_error += refill_error;
4634
+ es->ps.xdp_redirect += xdp_redirect;
4635
+ es->ps.xdp_pass += xdp_pass;
4636
+ es->ps.xdp_drop += xdp_drop;
4637
+ es->ps.xdp_xmit += xdp_xmit;
4638
+ es->ps.xdp_xmit_err += xdp_xmit_err;
4639
+ es->ps.xdp_tx += xdp_tx;
4640
+ es->ps.xdp_tx_err += xdp_tx_err;
4641
+ }
4642
+}
4643
+
40404644 static void mvneta_ethtool_update_stats(struct mvneta_port *pp)
40414645 {
4646
+ struct mvneta_ethtool_stats stats = {};
40424647 const struct mvneta_statistic *s;
40434648 void __iomem *base = pp->base;
40444649 u32 high, low;
40454650 u64 val;
40464651 int i;
40474652
4653
+ mvneta_ethtool_update_pcpu_stats(pp, &stats);
40484654 for (i = 0, s = mvneta_statistics;
40494655 s < mvneta_statistics + ARRAY_SIZE(mvneta_statistics);
40504656 s++, i++) {
4051
- val = 0;
4052
-
40534657 switch (s->type) {
40544658 case T_REG_32:
40554659 val = readl_relaxed(base + s->offset);
4660
+ pp->ethtool_stats[i] += val;
40564661 break;
40574662 case T_REG_64:
40584663 /* Docs say to read low 32-bit then high */
40594664 low = readl_relaxed(base + s->offset);
40604665 high = readl_relaxed(base + s->offset + 4);
40614666 val = (u64)high << 32 | low;
4667
+ pp->ethtool_stats[i] += val;
40624668 break;
40634669 case T_SW:
40644670 switch (s->offset) {
40654671 case ETHTOOL_STAT_EEE_WAKEUP:
40664672 val = phylink_get_eee_err(pp->phylink);
4673
+ pp->ethtool_stats[i] += val;
40674674 break;
40684675 case ETHTOOL_STAT_SKB_ALLOC_ERR:
4069
- val = pp->rxqs[0].skb_alloc_err;
4676
+ pp->ethtool_stats[i] = stats.skb_alloc_error;
40704677 break;
40714678 case ETHTOOL_STAT_REFILL_ERR:
4072
- val = pp->rxqs[0].refill_err;
4679
+ pp->ethtool_stats[i] = stats.refill_error;
4680
+ break;
4681
+ case ETHTOOL_XDP_REDIRECT:
4682
+ pp->ethtool_stats[i] = stats.ps.xdp_redirect;
4683
+ break;
4684
+ case ETHTOOL_XDP_PASS:
4685
+ pp->ethtool_stats[i] = stats.ps.xdp_pass;
4686
+ break;
4687
+ case ETHTOOL_XDP_DROP:
4688
+ pp->ethtool_stats[i] = stats.ps.xdp_drop;
4689
+ break;
4690
+ case ETHTOOL_XDP_TX:
4691
+ pp->ethtool_stats[i] = stats.ps.xdp_tx;
4692
+ break;
4693
+ case ETHTOOL_XDP_TX_ERR:
4694
+ pp->ethtool_stats[i] = stats.ps.xdp_tx_err;
4695
+ break;
4696
+ case ETHTOOL_XDP_XMIT:
4697
+ pp->ethtool_stats[i] = stats.ps.xdp_xmit;
4698
+ break;
4699
+ case ETHTOOL_XDP_XMIT_ERR:
4700
+ pp->ethtool_stats[i] = stats.ps.xdp_xmit_err;
40734701 break;
40744702 }
40754703 break;
40764704 }
4077
-
4078
- pp->ethtool_stats[i] += val;
40794705 }
40804706 }
40814707
....@@ -4261,8 +4887,7 @@
42614887
42624888 /* The Armada 37x documents do not give limits for this other than
42634889 * it being an 8-bit register. */
4264
- if (eee->tx_lpi_enabled &&
4265
- (eee->tx_lpi_timer < 0 || eee->tx_lpi_timer > 255))
4890
+ if (eee->tx_lpi_enabled && eee->tx_lpi_timer > 255)
42664891 return -EINVAL;
42674892
42684893 lpi_ctl0 = mvreg_read(pp, MVNETA_LPI_CTRL_0);
....@@ -4288,9 +4913,13 @@
42884913 .ndo_fix_features = mvneta_fix_features,
42894914 .ndo_get_stats64 = mvneta_get_stats64,
42904915 .ndo_do_ioctl = mvneta_ioctl,
4916
+ .ndo_bpf = mvneta_xdp,
4917
+ .ndo_xdp_xmit = mvneta_xdp_xmit,
42914918 };
42924919
42934920 static const struct ethtool_ops mvneta_eth_tool_ops = {
4921
+ .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS |
4922
+ ETHTOOL_COALESCE_MAX_FRAMES,
42944923 .nway_reset = mvneta_ethtool_nway_reset,
42954924 .get_link = ethtool_op_get_link,
42964925 .set_coalesce = mvneta_ethtool_set_coalesce,
....@@ -4415,12 +5044,10 @@
44155044 /* MAC Cause register should be cleared */
44165045 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
44175046
4418
- if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
4419
- mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
4420
- else if (phy_mode == PHY_INTERFACE_MODE_SGMII ||
4421
- phy_mode == PHY_INTERFACE_MODE_1000BASEX)
4422
- mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
4423
- else if (!phy_interface_mode_is_rgmii(phy_mode))
5047
+ if (phy_mode != PHY_INTERFACE_MODE_QSGMII &&
5048
+ phy_mode != PHY_INTERFACE_MODE_SGMII &&
5049
+ !phy_interface_mode_is_8023z(phy_mode) &&
5050
+ !phy_interface_mode_is_rgmii(phy_mode))
44245051 return -EINVAL;
44255052
44265053 return 0;
....@@ -4429,39 +5056,51 @@
44295056 /* Device initialization routine */
44305057 static int mvneta_probe(struct platform_device *pdev)
44315058 {
4432
- struct resource *res;
44335059 struct device_node *dn = pdev->dev.of_node;
44345060 struct device_node *bm_node;
44355061 struct mvneta_port *pp;
44365062 struct net_device *dev;
44375063 struct phylink *phylink;
5064
+ struct phy *comphy;
44385065 const char *dt_mac_addr;
44395066 char hw_mac_addr[ETH_ALEN];
5067
+ phy_interface_t phy_mode;
44405068 const char *mac_from;
44415069 int tx_csum_limit;
4442
- int phy_mode;
44435070 int err;
44445071 int cpu;
44455072
4446
- dev = alloc_etherdev_mqs(sizeof(struct mvneta_port), txq_number, rxq_number);
5073
+ dev = devm_alloc_etherdev_mqs(&pdev->dev, sizeof(struct mvneta_port),
5074
+ txq_number, rxq_number);
44475075 if (!dev)
44485076 return -ENOMEM;
44495077
44505078 dev->irq = irq_of_parse_and_map(dn, 0);
4451
- if (dev->irq == 0) {
4452
- err = -EINVAL;
4453
- goto err_free_netdev;
4454
- }
5079
+ if (dev->irq == 0)
5080
+ return -EINVAL;
44555081
4456
- phy_mode = of_get_phy_mode(dn);
4457
- if (phy_mode < 0) {
5082
+ err = of_get_phy_mode(dn, &phy_mode);
5083
+ if (err) {
44585084 dev_err(&pdev->dev, "incorrect phy-mode\n");
4459
- err = -EINVAL;
44605085 goto err_free_irq;
44615086 }
44625087
4463
- phylink = phylink_create(dev, pdev->dev.fwnode, phy_mode,
4464
- &mvneta_phylink_ops);
5088
+ comphy = devm_of_phy_get(&pdev->dev, dn, NULL);
5089
+ if (comphy == ERR_PTR(-EPROBE_DEFER)) {
5090
+ err = -EPROBE_DEFER;
5091
+ goto err_free_irq;
5092
+ } else if (IS_ERR(comphy)) {
5093
+ comphy = NULL;
5094
+ }
5095
+
5096
+ pp = netdev_priv(dev);
5097
+ spin_lock_init(&pp->lock);
5098
+
5099
+ pp->phylink_config.dev = &dev->dev;
5100
+ pp->phylink_config.type = PHYLINK_NETDEV;
5101
+
5102
+ phylink = phylink_create(&pp->phylink_config, pdev->dev.fwnode,
5103
+ phy_mode, &mvneta_phylink_ops);
44655104 if (IS_ERR(phylink)) {
44665105 err = PTR_ERR(phylink);
44675106 goto err_free_irq;
....@@ -4473,9 +5112,8 @@
44735112
44745113 dev->ethtool_ops = &mvneta_eth_tool_ops;
44755114
4476
- pp = netdev_priv(dev);
4477
- spin_lock_init(&pp->lock);
44785115 pp->phylink = phylink;
5116
+ pp->comphy = comphy;
44795117 pp->phy_interface = phy_mode;
44805118 pp->dn = dn;
44815119
....@@ -4500,8 +5138,7 @@
45005138 if (!IS_ERR(pp->clk_bus))
45015139 clk_prepare_enable(pp->clk_bus);
45025140
4503
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4504
- pp->base = devm_ioremap_resource(&pdev->dev, res);
5141
+ pp->base = devm_platform_ioremap_resource(pdev, 0);
45055142 if (IS_ERR(pp->base)) {
45065143 err = PTR_ERR(pp->base);
45075144 goto err_clk;
....@@ -4522,9 +5159,9 @@
45225159 }
45235160
45245161 dt_mac_addr = of_get_mac_address(dn);
4525
- if (dt_mac_addr) {
5162
+ if (!IS_ERR(dt_mac_addr)) {
45265163 mac_from = "device tree";
4527
- memcpy(dev->dev_addr, dt_mac_addr, ETH_ALEN);
5164
+ ether_addr_copy(dev->dev_addr, dt_mac_addr);
45285165 } else {
45295166 mvneta_get_mac_addr(pp, hw_mac_addr);
45305167 if (is_valid_ether_addr(hw_mac_addr)) {
....@@ -4567,7 +5204,6 @@
45675204 SET_NETDEV_DEV(dev, &pdev->dev);
45685205
45695206 pp->id = global_port_id++;
4570
- pp->rx_offset_correction = 0; /* not relevant for SW BM */
45715207
45725208 /* Obtain access to BM resources if enabled and already initialized */
45735209 bm_node = of_parse_phandle(dn, "buffer-manager", 0);
....@@ -4592,11 +5228,15 @@
45925228 }
45935229 of_node_put(bm_node);
45945230
5231
+ /* sw buffer management */
5232
+ if (!pp->bm_priv)
5233
+ pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
5234
+
45955235 err = mvneta_init(&pdev->dev, pp);
45965236 if (err < 0)
45975237 goto err_netdev;
45985238
4599
- err = mvneta_port_power_up(pp, phy_mode);
5239
+ err = mvneta_port_power_up(pp, pp->phy_interface);
46005240 if (err < 0) {
46015241 dev_err(&pdev->dev, "can't power up port\n");
46025242 goto err_netdev;
....@@ -4618,7 +5258,8 @@
46185258 }
46195259 }
46205260
4621
- dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | NETIF_F_TSO;
5261
+ dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
5262
+ NETIF_F_TSO | NETIF_F_RXCSUM;
46225263 dev->hw_features |= dev->features;
46235264 dev->vlan_features |= dev->features;
46245265 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
....@@ -4660,8 +5301,6 @@
46605301 phylink_destroy(pp->phylink);
46615302 err_free_irq:
46625303 irq_dispose_mapping(dev->irq);
4663
-err_free_netdev:
4664
- free_netdev(dev);
46655304 return err;
46665305 }
46675306
....@@ -4678,7 +5317,6 @@
46785317 free_percpu(pp->stats);
46795318 irq_dispose_mapping(dev->irq);
46805319 phylink_destroy(pp->phylink);
4681
- free_netdev(dev);
46825320
46835321 if (pp->bm_priv) {
46845322 mvneta_bm_pool_destroy(pp->bm_priv, pp->pool_long, 1 << pp->id);
....@@ -4751,6 +5389,7 @@
47515389 err = mvneta_bm_port_init(pdev, pp);
47525390 if (err < 0) {
47535391 dev_info(&pdev->dev, "use SW buffer management\n");
5392
+ pp->rx_offset_correction = MVNETA_SKB_HEADROOM;
47545393 pp->bm_priv = NULL;
47555394 }
47565395 }
....@@ -4823,7 +5462,7 @@
48235462 {
48245463 int ret;
48255464
4826
- ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvmeta:online",
5465
+ ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "net/mvneta:online",
48275466 mvneta_cpu_online,
48285467 mvneta_cpu_down_prepare);
48295468 if (ret < 0)