1 // SPDX-License-Identifier: GPL-2.0-only 2 /******************************************************************************* 3 This is the driver for the ST MAC 10/100/1000 on-chip Ethernet controllers. 4 ST Ethernet IPs are built around a Synopsys IP Core. 5 6 Copyright(C) 2007-2011 STMicroelectronics Ltd 7 8 9 Author: Giuseppe Cavallaro <peppe.cavallaro@st.com> 10 11 Documentation available at: 12 http://www.stlinux.com 13 Support available at: 14 https://bugzilla.stlinux.com/ 15 *******************************************************************************/ 16 17 #include <linux/clk.h> 18 #include <linux/kernel.h> 19 #include <linux/interrupt.h> 20 #include <linux/ip.h> 21 #include <linux/tcp.h> 22 #include <linux/skbuff.h> 23 #include <linux/ethtool.h> 24 #include <linux/if_ether.h> 25 #include <linux/crc32.h> 26 #include <linux/mii.h> 27 #include <linux/if.h> 28 #include <linux/if_vlan.h> 29 #include <linux/dma-mapping.h> 30 #include <linux/slab.h> 31 #include <linux/pm_runtime.h> 32 #include <linux/prefetch.h> 33 #include <linux/pinctrl/consumer.h> 34 #ifdef CONFIG_DEBUG_FS 35 #include <linux/debugfs.h> 36 #include <linux/seq_file.h> 37 #endif /* CONFIG_DEBUG_FS */ 38 #include <linux/net_tstamp.h> 39 #include <linux/phylink.h> 40 #include <linux/udp.h> 41 #include <linux/bpf_trace.h> 42 #include <net/pkt_cls.h> 43 #include <net/xdp_sock_drv.h> 44 #include "stmmac_ptp.h" 45 #include "stmmac.h" 46 #include "stmmac_xdp.h" 47 #include <linux/reset.h> 48 #include <linux/of_mdio.h> 49 #include "dwmac1000.h" 50 #include "dwxgmac2.h" 51 #include "hwif.h" 52 53 /* As long as the interface is active, we keep the timestamping counter enabled 54 * with fine resolution and binary rollover. This avoid non-monotonic behavior 55 * (clock jumps) when changing timestamping settings at runtime. 56 */ 57 #define STMMAC_HWTS_ACTIVE (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | \ 58 PTP_TCR_TSCTRLSSR) 59 60 #define STMMAC_ALIGN(x) ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16) 61 #define TSO_MAX_BUFF_SIZE (SZ_16K - 1) 62 63 /* Module parameters */ 64 #define TX_TIMEO 5000 65 static int watchdog = TX_TIMEO; 66 module_param(watchdog, int, 0644); 67 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)"); 68 69 static int debug = -1; 70 module_param(debug, int, 0644); 71 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)"); 72 73 static int phyaddr = -1; 74 module_param(phyaddr, int, 0444); 75 MODULE_PARM_DESC(phyaddr, "Physical device address"); 76 77 #define STMMAC_TX_THRESH(x) ((x)->dma_tx_size / 4) 78 #define STMMAC_RX_THRESH(x) ((x)->dma_rx_size / 4) 79 80 /* Limit to make sure XDP TX and slow path can coexist */ 81 #define STMMAC_XSK_TX_BUDGET_MAX 256 82 #define STMMAC_TX_XSK_AVAIL 16 83 #define STMMAC_RX_FILL_BATCH 16 84 85 #define STMMAC_XDP_PASS 0 86 #define STMMAC_XDP_CONSUMED BIT(0) 87 #define STMMAC_XDP_TX BIT(1) 88 #define STMMAC_XDP_REDIRECT BIT(2) 89 90 static int flow_ctrl = FLOW_AUTO; 91 module_param(flow_ctrl, int, 0644); 92 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]"); 93 94 static int pause = PAUSE_TIME; 95 module_param(pause, int, 0644); 96 MODULE_PARM_DESC(pause, "Flow Control Pause Time"); 97 98 #define TC_DEFAULT 64 99 static int tc = TC_DEFAULT; 100 module_param(tc, int, 0644); 101 MODULE_PARM_DESC(tc, "DMA threshold control value"); 102 103 #define DEFAULT_BUFSIZE 1536 104 static int buf_sz = DEFAULT_BUFSIZE; 105 module_param(buf_sz, int, 0644); 106 MODULE_PARM_DESC(buf_sz, "DMA buffer size"); 107 108 #define STMMAC_RX_COPYBREAK 256 109 110 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE | 111 NETIF_MSG_LINK | NETIF_MSG_IFUP | 112 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER); 113 114 #define STMMAC_DEFAULT_LPI_TIMER 1000 115 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER; 116 module_param(eee_timer, int, 0644); 117 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec"); 118 #define STMMAC_LPI_T(x) (jiffies + usecs_to_jiffies(x)) 119 120 /* By default the driver will use the ring mode to manage tx and rx descriptors, 121 * but allow user to force to use the chain instead of the ring 122 */ 123 static unsigned int chain_mode; 124 module_param(chain_mode, int, 0444); 125 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode"); 126 127 static irqreturn_t stmmac_interrupt(int irq, void *dev_id); 128 /* For MSI interrupts handling */ 129 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id); 130 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id); 131 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data); 132 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data); 133 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue); 134 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue); 135 136 #ifdef CONFIG_DEBUG_FS 137 static const struct net_device_ops stmmac_netdev_ops; 138 static void stmmac_init_fs(struct net_device *dev); 139 static void stmmac_exit_fs(struct net_device *dev); 140 #endif 141 142 #define STMMAC_COAL_TIMER(x) (ns_to_ktime((x) * NSEC_PER_USEC)) 143 144 int stmmac_bus_clks_config(struct stmmac_priv *priv, bool enabled) 145 { 146 int ret = 0; 147 148 if (enabled) { 149 ret = clk_prepare_enable(priv->plat->stmmac_clk); 150 if (ret) 151 return ret; 152 ret = clk_prepare_enable(priv->plat->pclk); 153 if (ret) { 154 clk_disable_unprepare(priv->plat->stmmac_clk); 155 return ret; 156 } 157 if (priv->plat->clks_config) { 158 ret = priv->plat->clks_config(priv->plat->bsp_priv, enabled); 159 if (ret) { 160 clk_disable_unprepare(priv->plat->stmmac_clk); 161 clk_disable_unprepare(priv->plat->pclk); 162 return ret; 163 } 164 } 165 } else { 166 clk_disable_unprepare(priv->plat->stmmac_clk); 167 clk_disable_unprepare(priv->plat->pclk); 168 if (priv->plat->clks_config) 169 priv->plat->clks_config(priv->plat->bsp_priv, enabled); 170 } 171 172 return ret; 173 } 174 EXPORT_SYMBOL_GPL(stmmac_bus_clks_config); 175 176 /** 177 * stmmac_verify_args - verify the driver parameters. 178 * Description: it checks the driver parameters and set a default in case of 179 * errors. 180 */ 181 static void stmmac_verify_args(void) 182 { 183 if (unlikely(watchdog < 0)) 184 watchdog = TX_TIMEO; 185 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB))) 186 buf_sz = DEFAULT_BUFSIZE; 187 if (unlikely(flow_ctrl > 1)) 188 flow_ctrl = FLOW_AUTO; 189 else if (likely(flow_ctrl < 0)) 190 flow_ctrl = FLOW_OFF; 191 if (unlikely((pause < 0) || (pause > 0xffff))) 192 pause = PAUSE_TIME; 193 if (eee_timer < 0) 194 eee_timer = STMMAC_DEFAULT_LPI_TIMER; 195 } 196 197 static void __stmmac_disable_all_queues(struct stmmac_priv *priv) 198 { 199 u32 rx_queues_cnt = priv->plat->rx_queues_to_use; 200 u32 tx_queues_cnt = priv->plat->tx_queues_to_use; 201 u32 maxq = max(rx_queues_cnt, tx_queues_cnt); 202 u32 queue; 203 204 for (queue = 0; queue < maxq; queue++) { 205 struct stmmac_channel *ch = &priv->channel[queue]; 206 207 if (stmmac_xdp_is_enabled(priv) && 208 test_bit(queue, priv->af_xdp_zc_qps)) { 209 napi_disable(&ch->rxtx_napi); 210 continue; 211 } 212 213 if (queue < rx_queues_cnt) 214 napi_disable(&ch->rx_napi); 215 if (queue < tx_queues_cnt) 216 napi_disable(&ch->tx_napi); 217 } 218 } 219 220 /** 221 * stmmac_disable_all_queues - Disable all queues 222 * @priv: driver private structure 223 */ 224 static void stmmac_disable_all_queues(struct stmmac_priv *priv) 225 { 226 u32 rx_queues_cnt = priv->plat->rx_queues_to_use; 227 struct stmmac_rx_queue *rx_q; 228 u32 queue; 229 230 /* synchronize_rcu() needed for pending XDP buffers to drain */ 231 for (queue = 0; queue < rx_queues_cnt; queue++) { 232 rx_q = &priv->rx_queue[queue]; 233 if (rx_q->xsk_pool) { 234 synchronize_rcu(); 235 break; 236 } 237 } 238 239 __stmmac_disable_all_queues(priv); 240 } 241 242 /** 243 * stmmac_enable_all_queues - Enable all queues 244 * @priv: driver private structure 245 */ 246 static void stmmac_enable_all_queues(struct stmmac_priv *priv) 247 { 248 u32 rx_queues_cnt = priv->plat->rx_queues_to_use; 249 u32 tx_queues_cnt = priv->plat->tx_queues_to_use; 250 u32 maxq = max(rx_queues_cnt, tx_queues_cnt); 251 u32 queue; 252 253 for (queue = 0; queue < maxq; queue++) { 254 struct stmmac_channel *ch = &priv->channel[queue]; 255 256 if (stmmac_xdp_is_enabled(priv) && 257 test_bit(queue, priv->af_xdp_zc_qps)) { 258 napi_enable(&ch->rxtx_napi); 259 continue; 260 } 261 262 if (queue < rx_queues_cnt) 263 napi_enable(&ch->rx_napi); 264 if (queue < tx_queues_cnt) 265 napi_enable(&ch->tx_napi); 266 } 267 } 268 269 static void stmmac_service_event_schedule(struct stmmac_priv *priv) 270 { 271 if (!test_bit(STMMAC_DOWN, &priv->state) && 272 !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state)) 273 queue_work(priv->wq, &priv->service_task); 274 } 275 276 static void stmmac_global_err(struct stmmac_priv *priv) 277 { 278 netif_carrier_off(priv->dev); 279 set_bit(STMMAC_RESET_REQUESTED, &priv->state); 280 stmmac_service_event_schedule(priv); 281 } 282 283 /** 284 * stmmac_clk_csr_set - dynamically set the MDC clock 285 * @priv: driver private structure 286 * Description: this is to dynamically set the MDC clock according to the csr 287 * clock input. 288 * Note: 289 * If a specific clk_csr value is passed from the platform 290 * this means that the CSR Clock Range selection cannot be 291 * changed at run-time and it is fixed (as reported in the driver 292 * documentation). Viceversa the driver will try to set the MDC 293 * clock dynamically according to the actual clock input. 294 */ 295 static void stmmac_clk_csr_set(struct stmmac_priv *priv) 296 { 297 u32 clk_rate; 298 299 clk_rate = clk_get_rate(priv->plat->stmmac_clk); 300 301 /* Platform provided default clk_csr would be assumed valid 302 * for all other cases except for the below mentioned ones. 303 * For values higher than the IEEE 802.3 specified frequency 304 * we can not estimate the proper divider as it is not known 305 * the frequency of clk_csr_i. So we do not change the default 306 * divider. 307 */ 308 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) { 309 if (clk_rate < CSR_F_35M) 310 priv->clk_csr = STMMAC_CSR_20_35M; 311 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M)) 312 priv->clk_csr = STMMAC_CSR_35_60M; 313 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M)) 314 priv->clk_csr = STMMAC_CSR_60_100M; 315 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M)) 316 priv->clk_csr = STMMAC_CSR_100_150M; 317 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M)) 318 priv->clk_csr = STMMAC_CSR_150_250M; 319 else if ((clk_rate >= CSR_F_250M) && (clk_rate <= CSR_F_300M)) 320 priv->clk_csr = STMMAC_CSR_250_300M; 321 } 322 323 if (priv->plat->has_sun8i) { 324 if (clk_rate > 160000000) 325 priv->clk_csr = 0x03; 326 else if (clk_rate > 80000000) 327 priv->clk_csr = 0x02; 328 else if (clk_rate > 40000000) 329 priv->clk_csr = 0x01; 330 else 331 priv->clk_csr = 0; 332 } 333 334 if (priv->plat->has_xgmac) { 335 if (clk_rate > 400000000) 336 priv->clk_csr = 0x5; 337 else if (clk_rate > 350000000) 338 priv->clk_csr = 0x4; 339 else if (clk_rate > 300000000) 340 priv->clk_csr = 0x3; 341 else if (clk_rate > 250000000) 342 priv->clk_csr = 0x2; 343 else if (clk_rate > 150000000) 344 priv->clk_csr = 0x1; 345 else 346 priv->clk_csr = 0x0; 347 } 348 } 349 350 static void print_pkt(unsigned char *buf, int len) 351 { 352 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf); 353 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len); 354 } 355 356 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue) 357 { 358 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 359 u32 avail; 360 361 if (tx_q->dirty_tx > tx_q->cur_tx) 362 avail = tx_q->dirty_tx - tx_q->cur_tx - 1; 363 else 364 avail = priv->dma_tx_size - tx_q->cur_tx + tx_q->dirty_tx - 1; 365 366 return avail; 367 } 368 369 /** 370 * stmmac_rx_dirty - Get RX queue dirty 371 * @priv: driver private structure 372 * @queue: RX queue index 373 */ 374 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue) 375 { 376 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 377 u32 dirty; 378 379 if (rx_q->dirty_rx <= rx_q->cur_rx) 380 dirty = rx_q->cur_rx - rx_q->dirty_rx; 381 else 382 dirty = priv->dma_rx_size - rx_q->dirty_rx + rx_q->cur_rx; 383 384 return dirty; 385 } 386 387 static void stmmac_lpi_entry_timer_config(struct stmmac_priv *priv, bool en) 388 { 389 int tx_lpi_timer; 390 391 /* Clear/set the SW EEE timer flag based on LPI ET enablement */ 392 priv->eee_sw_timer_en = en ? 0 : 1; 393 tx_lpi_timer = en ? priv->tx_lpi_timer : 0; 394 stmmac_set_eee_lpi_timer(priv, priv->hw, tx_lpi_timer); 395 } 396 397 /** 398 * stmmac_enable_eee_mode - check and enter in LPI mode 399 * @priv: driver private structure 400 * Description: this function is to verify and enter in LPI mode in case of 401 * EEE. 402 */ 403 static void stmmac_enable_eee_mode(struct stmmac_priv *priv) 404 { 405 u32 tx_cnt = priv->plat->tx_queues_to_use; 406 u32 queue; 407 408 /* check if all TX queues have the work finished */ 409 for (queue = 0; queue < tx_cnt; queue++) { 410 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 411 412 if (tx_q->dirty_tx != tx_q->cur_tx) 413 return; /* still unfinished work */ 414 } 415 416 /* Check and enter in LPI mode */ 417 if (!priv->tx_path_in_lpi_mode) 418 stmmac_set_eee_mode(priv, priv->hw, 419 priv->plat->en_tx_lpi_clockgating); 420 } 421 422 /** 423 * stmmac_disable_eee_mode - disable and exit from LPI mode 424 * @priv: driver private structure 425 * Description: this function is to exit and disable EEE in case of 426 * LPI state is true. This is called by the xmit. 427 */ 428 void stmmac_disable_eee_mode(struct stmmac_priv *priv) 429 { 430 if (!priv->eee_sw_timer_en) { 431 stmmac_lpi_entry_timer_config(priv, 0); 432 return; 433 } 434 435 stmmac_reset_eee_mode(priv, priv->hw); 436 del_timer_sync(&priv->eee_ctrl_timer); 437 priv->tx_path_in_lpi_mode = false; 438 } 439 440 /** 441 * stmmac_eee_ctrl_timer - EEE TX SW timer. 442 * @t: timer_list struct containing private info 443 * Description: 444 * if there is no data transfer and if we are not in LPI state, 445 * then MAC Transmitter can be moved to LPI state. 446 */ 447 static void stmmac_eee_ctrl_timer(struct timer_list *t) 448 { 449 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer); 450 451 stmmac_enable_eee_mode(priv); 452 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer)); 453 } 454 455 /** 456 * stmmac_eee_init - init EEE 457 * @priv: driver private structure 458 * Description: 459 * if the GMAC supports the EEE (from the HW cap reg) and the phy device 460 * can also manage EEE, this function enable the LPI state and start related 461 * timer. 462 */ 463 bool stmmac_eee_init(struct stmmac_priv *priv) 464 { 465 int eee_tw_timer = priv->eee_tw_timer; 466 467 /* Using PCS we cannot dial with the phy registers at this stage 468 * so we do not support extra feature like EEE. 469 */ 470 if (priv->hw->pcs == STMMAC_PCS_TBI || 471 priv->hw->pcs == STMMAC_PCS_RTBI) 472 return false; 473 474 /* Check if MAC core supports the EEE feature. */ 475 if (!priv->dma_cap.eee) 476 return false; 477 478 mutex_lock(&priv->lock); 479 480 /* Check if it needs to be deactivated */ 481 if (!priv->eee_active) { 482 if (priv->eee_enabled) { 483 netdev_dbg(priv->dev, "disable EEE\n"); 484 stmmac_lpi_entry_timer_config(priv, 0); 485 del_timer_sync(&priv->eee_ctrl_timer); 486 stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer); 487 if (priv->hw->xpcs) 488 xpcs_config_eee(priv->hw->xpcs, 489 priv->plat->mult_fact_100ns, 490 false); 491 } 492 mutex_unlock(&priv->lock); 493 return false; 494 } 495 496 if (priv->eee_active && !priv->eee_enabled) { 497 timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0); 498 stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS, 499 eee_tw_timer); 500 if (priv->hw->xpcs) 501 xpcs_config_eee(priv->hw->xpcs, 502 priv->plat->mult_fact_100ns, 503 true); 504 } 505 506 if (priv->plat->has_gmac4 && priv->tx_lpi_timer <= STMMAC_ET_MAX) { 507 del_timer_sync(&priv->eee_ctrl_timer); 508 priv->tx_path_in_lpi_mode = false; 509 stmmac_lpi_entry_timer_config(priv, 1); 510 } else { 511 stmmac_lpi_entry_timer_config(priv, 0); 512 mod_timer(&priv->eee_ctrl_timer, 513 STMMAC_LPI_T(priv->tx_lpi_timer)); 514 } 515 516 mutex_unlock(&priv->lock); 517 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n"); 518 return true; 519 } 520 521 static inline u32 stmmac_cdc_adjust(struct stmmac_priv *priv) 522 { 523 /* Correct the clk domain crossing(CDC) error */ 524 if (priv->plat->has_gmac4 && priv->plat->clk_ptp_rate) 525 return (2 * NSEC_PER_SEC) / priv->plat->clk_ptp_rate; 526 return 0; 527 } 528 529 /* stmmac_get_tx_hwtstamp - get HW TX timestamps 530 * @priv: driver private structure 531 * @p : descriptor pointer 532 * @skb : the socket buffer 533 * Description : 534 * This function will read timestamp from the descriptor & pass it to stack. 535 * and also perform some sanity checks. 536 */ 537 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv, 538 struct dma_desc *p, struct sk_buff *skb) 539 { 540 struct skb_shared_hwtstamps shhwtstamp; 541 bool found = false; 542 u64 ns = 0; 543 544 if (!priv->hwts_tx_en) 545 return; 546 547 /* exit if skb doesn't support hw tstamp */ 548 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))) 549 return; 550 551 /* check tx tstamp status */ 552 if (stmmac_get_tx_timestamp_status(priv, p)) { 553 stmmac_get_timestamp(priv, p, priv->adv_ts, &ns); 554 found = true; 555 } else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) { 556 found = true; 557 } 558 559 if (found) { 560 ns -= stmmac_cdc_adjust(priv); 561 562 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps)); 563 shhwtstamp.hwtstamp = ns_to_ktime(ns); 564 565 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns); 566 /* pass tstamp to stack */ 567 skb_tstamp_tx(skb, &shhwtstamp); 568 } 569 } 570 571 /* stmmac_get_rx_hwtstamp - get HW RX timestamps 572 * @priv: driver private structure 573 * @p : descriptor pointer 574 * @np : next descriptor pointer 575 * @skb : the socket buffer 576 * Description : 577 * This function will read received packet's timestamp from the descriptor 578 * and pass it to stack. It also perform some sanity checks. 579 */ 580 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p, 581 struct dma_desc *np, struct sk_buff *skb) 582 { 583 struct skb_shared_hwtstamps *shhwtstamp = NULL; 584 struct dma_desc *desc = p; 585 u64 ns = 0; 586 587 if (!priv->hwts_rx_en) 588 return; 589 /* For GMAC4, the valid timestamp is from CTX next desc. */ 590 if (priv->plat->has_gmac4 || priv->plat->has_xgmac) 591 desc = np; 592 593 /* Check if timestamp is available */ 594 if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) { 595 stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns); 596 597 ns -= stmmac_cdc_adjust(priv); 598 599 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns); 600 shhwtstamp = skb_hwtstamps(skb); 601 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps)); 602 shhwtstamp->hwtstamp = ns_to_ktime(ns); 603 } else { 604 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n"); 605 } 606 } 607 608 /** 609 * stmmac_hwtstamp_set - control hardware timestamping. 610 * @dev: device pointer. 611 * @ifr: An IOCTL specific structure, that can contain a pointer to 612 * a proprietary structure used to pass information to the driver. 613 * Description: 614 * This function configures the MAC to enable/disable both outgoing(TX) 615 * and incoming(RX) packets time stamping based on user input. 616 * Return Value: 617 * 0 on success and an appropriate -ve integer on failure. 618 */ 619 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr) 620 { 621 struct stmmac_priv *priv = netdev_priv(dev); 622 struct hwtstamp_config config; 623 u32 ptp_v2 = 0; 624 u32 tstamp_all = 0; 625 u32 ptp_over_ipv4_udp = 0; 626 u32 ptp_over_ipv6_udp = 0; 627 u32 ptp_over_ethernet = 0; 628 u32 snap_type_sel = 0; 629 u32 ts_master_en = 0; 630 u32 ts_event_en = 0; 631 632 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) { 633 netdev_alert(priv->dev, "No support for HW time stamping\n"); 634 priv->hwts_tx_en = 0; 635 priv->hwts_rx_en = 0; 636 637 return -EOPNOTSUPP; 638 } 639 640 if (copy_from_user(&config, ifr->ifr_data, 641 sizeof(config))) 642 return -EFAULT; 643 644 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n", 645 __func__, config.flags, config.tx_type, config.rx_filter); 646 647 /* reserved for future extensions */ 648 if (config.flags) 649 return -EINVAL; 650 651 if (config.tx_type != HWTSTAMP_TX_OFF && 652 config.tx_type != HWTSTAMP_TX_ON) 653 return -ERANGE; 654 655 if (priv->adv_ts) { 656 switch (config.rx_filter) { 657 case HWTSTAMP_FILTER_NONE: 658 /* time stamp no incoming packet at all */ 659 config.rx_filter = HWTSTAMP_FILTER_NONE; 660 break; 661 662 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 663 /* PTP v1, UDP, any kind of event packet */ 664 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; 665 /* 'xmac' hardware can support Sync, Pdelay_Req and 666 * Pdelay_resp by setting bit14 and bits17/16 to 01 667 * This leaves Delay_Req timestamps out. 668 * Enable all events *and* general purpose message 669 * timestamping 670 */ 671 snap_type_sel = PTP_TCR_SNAPTYPSEL_1; 672 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 673 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 674 break; 675 676 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 677 /* PTP v1, UDP, Sync packet */ 678 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC; 679 /* take time stamp for SYNC messages only */ 680 ts_event_en = PTP_TCR_TSEVNTENA; 681 682 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 683 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 684 break; 685 686 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 687 /* PTP v1, UDP, Delay_req packet */ 688 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ; 689 /* take time stamp for Delay_Req messages only */ 690 ts_master_en = PTP_TCR_TSMSTRENA; 691 ts_event_en = PTP_TCR_TSEVNTENA; 692 693 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 694 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 695 break; 696 697 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 698 /* PTP v2, UDP, any kind of event packet */ 699 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; 700 ptp_v2 = PTP_TCR_TSVER2ENA; 701 /* take time stamp for all event messages */ 702 snap_type_sel = PTP_TCR_SNAPTYPSEL_1; 703 704 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 705 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 706 break; 707 708 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 709 /* PTP v2, UDP, Sync packet */ 710 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC; 711 ptp_v2 = PTP_TCR_TSVER2ENA; 712 /* take time stamp for SYNC messages only */ 713 ts_event_en = PTP_TCR_TSEVNTENA; 714 715 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 716 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 717 break; 718 719 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 720 /* PTP v2, UDP, Delay_req packet */ 721 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ; 722 ptp_v2 = PTP_TCR_TSVER2ENA; 723 /* take time stamp for Delay_Req messages only */ 724 ts_master_en = PTP_TCR_TSMSTRENA; 725 ts_event_en = PTP_TCR_TSEVNTENA; 726 727 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 728 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 729 break; 730 731 case HWTSTAMP_FILTER_PTP_V2_EVENT: 732 /* PTP v2/802.AS1 any layer, any kind of event packet */ 733 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 734 ptp_v2 = PTP_TCR_TSVER2ENA; 735 snap_type_sel = PTP_TCR_SNAPTYPSEL_1; 736 if (priv->synopsys_id < DWMAC_CORE_4_10) 737 ts_event_en = PTP_TCR_TSEVNTENA; 738 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 739 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 740 ptp_over_ethernet = PTP_TCR_TSIPENA; 741 break; 742 743 case HWTSTAMP_FILTER_PTP_V2_SYNC: 744 /* PTP v2/802.AS1, any layer, Sync packet */ 745 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC; 746 ptp_v2 = PTP_TCR_TSVER2ENA; 747 /* take time stamp for SYNC messages only */ 748 ts_event_en = PTP_TCR_TSEVNTENA; 749 750 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 751 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 752 ptp_over_ethernet = PTP_TCR_TSIPENA; 753 break; 754 755 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 756 /* PTP v2/802.AS1, any layer, Delay_req packet */ 757 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ; 758 ptp_v2 = PTP_TCR_TSVER2ENA; 759 /* take time stamp for Delay_Req messages only */ 760 ts_master_en = PTP_TCR_TSMSTRENA; 761 ts_event_en = PTP_TCR_TSEVNTENA; 762 763 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 764 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 765 ptp_over_ethernet = PTP_TCR_TSIPENA; 766 break; 767 768 case HWTSTAMP_FILTER_NTP_ALL: 769 case HWTSTAMP_FILTER_ALL: 770 /* time stamp any incoming packet */ 771 config.rx_filter = HWTSTAMP_FILTER_ALL; 772 tstamp_all = PTP_TCR_TSENALL; 773 break; 774 775 default: 776 return -ERANGE; 777 } 778 } else { 779 switch (config.rx_filter) { 780 case HWTSTAMP_FILTER_NONE: 781 config.rx_filter = HWTSTAMP_FILTER_NONE; 782 break; 783 default: 784 /* PTP v1, UDP, any kind of event packet */ 785 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; 786 break; 787 } 788 } 789 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1); 790 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON; 791 792 priv->systime_flags = STMMAC_HWTS_ACTIVE; 793 794 if (priv->hwts_tx_en || priv->hwts_rx_en) { 795 priv->systime_flags |= tstamp_all | ptp_v2 | 796 ptp_over_ethernet | ptp_over_ipv6_udp | 797 ptp_over_ipv4_udp | ts_event_en | 798 ts_master_en | snap_type_sel; 799 } 800 801 stmmac_config_hw_tstamping(priv, priv->ptpaddr, priv->systime_flags); 802 803 memcpy(&priv->tstamp_config, &config, sizeof(config)); 804 805 return copy_to_user(ifr->ifr_data, &config, 806 sizeof(config)) ? -EFAULT : 0; 807 } 808 809 /** 810 * stmmac_hwtstamp_get - read hardware timestamping. 811 * @dev: device pointer. 812 * @ifr: An IOCTL specific structure, that can contain a pointer to 813 * a proprietary structure used to pass information to the driver. 814 * Description: 815 * This function obtain the current hardware timestamping settings 816 * as requested. 817 */ 818 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr) 819 { 820 struct stmmac_priv *priv = netdev_priv(dev); 821 struct hwtstamp_config *config = &priv->tstamp_config; 822 823 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp)) 824 return -EOPNOTSUPP; 825 826 return copy_to_user(ifr->ifr_data, config, 827 sizeof(*config)) ? -EFAULT : 0; 828 } 829 830 /** 831 * stmmac_init_tstamp_counter - init hardware timestamping counter 832 * @priv: driver private structure 833 * @systime_flags: timestamping flags 834 * Description: 835 * Initialize hardware counter for packet timestamping. 836 * This is valid as long as the interface is open and not suspended. 837 * Will be rerun after resuming from suspend, case in which the timestamping 838 * flags updated by stmmac_hwtstamp_set() also need to be restored. 839 */ 840 int stmmac_init_tstamp_counter(struct stmmac_priv *priv, u32 systime_flags) 841 { 842 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac; 843 struct timespec64 now; 844 u32 sec_inc = 0; 845 u64 temp = 0; 846 int ret; 847 848 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp)) 849 return -EOPNOTSUPP; 850 851 ret = clk_prepare_enable(priv->plat->clk_ptp_ref); 852 if (ret < 0) { 853 netdev_warn(priv->dev, 854 "failed to enable PTP reference clock: %pe\n", 855 ERR_PTR(ret)); 856 return ret; 857 } 858 859 stmmac_config_hw_tstamping(priv, priv->ptpaddr, systime_flags); 860 priv->systime_flags = systime_flags; 861 862 /* program Sub Second Increment reg */ 863 stmmac_config_sub_second_increment(priv, priv->ptpaddr, 864 priv->plat->clk_ptp_rate, 865 xmac, &sec_inc); 866 temp = div_u64(1000000000ULL, sec_inc); 867 868 /* Store sub second increment for later use */ 869 priv->sub_second_inc = sec_inc; 870 871 /* calculate default added value: 872 * formula is : 873 * addend = (2^32)/freq_div_ratio; 874 * where, freq_div_ratio = 1e9ns/sec_inc 875 */ 876 temp = (u64)(temp << 32); 877 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate); 878 stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend); 879 880 /* initialize system time */ 881 ktime_get_real_ts64(&now); 882 883 /* lower 32 bits of tv_sec are safe until y2106 */ 884 stmmac_init_systime(priv, priv->ptpaddr, (u32)now.tv_sec, now.tv_nsec); 885 886 return 0; 887 } 888 EXPORT_SYMBOL_GPL(stmmac_init_tstamp_counter); 889 890 /** 891 * stmmac_init_ptp - init PTP 892 * @priv: driver private structure 893 * Description: this is to verify if the HW supports the PTPv1 or PTPv2. 894 * This is done by looking at the HW cap. register. 895 * This function also registers the ptp driver. 896 */ 897 static int stmmac_init_ptp(struct stmmac_priv *priv) 898 { 899 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac; 900 int ret; 901 902 ret = stmmac_init_tstamp_counter(priv, STMMAC_HWTS_ACTIVE); 903 if (ret) 904 return ret; 905 906 priv->adv_ts = 0; 907 /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */ 908 if (xmac && priv->dma_cap.atime_stamp) 909 priv->adv_ts = 1; 910 /* Dwmac 3.x core with extend_desc can support adv_ts */ 911 else if (priv->extend_desc && priv->dma_cap.atime_stamp) 912 priv->adv_ts = 1; 913 914 if (priv->dma_cap.time_stamp) 915 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n"); 916 917 if (priv->adv_ts) 918 netdev_info(priv->dev, 919 "IEEE 1588-2008 Advanced Timestamp supported\n"); 920 921 priv->hwts_tx_en = 0; 922 priv->hwts_rx_en = 0; 923 924 stmmac_ptp_register(priv); 925 926 return 0; 927 } 928 929 static void stmmac_release_ptp(struct stmmac_priv *priv) 930 { 931 clk_disable_unprepare(priv->plat->clk_ptp_ref); 932 stmmac_ptp_unregister(priv); 933 } 934 935 /** 936 * stmmac_mac_flow_ctrl - Configure flow control in all queues 937 * @priv: driver private structure 938 * @duplex: duplex passed to the next function 939 * Description: It is used for configuring the flow control in all queues 940 */ 941 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex) 942 { 943 u32 tx_cnt = priv->plat->tx_queues_to_use; 944 945 stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl, 946 priv->pause, tx_cnt); 947 } 948 949 static void stmmac_validate(struct phylink_config *config, 950 unsigned long *supported, 951 struct phylink_link_state *state) 952 { 953 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev)); 954 __ETHTOOL_DECLARE_LINK_MODE_MASK(mac_supported) = { 0, }; 955 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 956 int tx_cnt = priv->plat->tx_queues_to_use; 957 int max_speed = priv->plat->max_speed; 958 959 phylink_set(mac_supported, 10baseT_Half); 960 phylink_set(mac_supported, 10baseT_Full); 961 phylink_set(mac_supported, 100baseT_Half); 962 phylink_set(mac_supported, 100baseT_Full); 963 phylink_set(mac_supported, 1000baseT_Half); 964 phylink_set(mac_supported, 1000baseT_Full); 965 phylink_set(mac_supported, 1000baseKX_Full); 966 967 phylink_set(mac_supported, Autoneg); 968 phylink_set(mac_supported, Pause); 969 phylink_set(mac_supported, Asym_Pause); 970 phylink_set_port_modes(mac_supported); 971 972 /* Cut down 1G if asked to */ 973 if ((max_speed > 0) && (max_speed < 1000)) { 974 phylink_set(mask, 1000baseT_Full); 975 phylink_set(mask, 1000baseX_Full); 976 } else if (priv->plat->has_gmac4) { 977 if (!max_speed || max_speed >= 2500) { 978 phylink_set(mac_supported, 2500baseT_Full); 979 phylink_set(mac_supported, 2500baseX_Full); 980 } 981 } else if (priv->plat->has_xgmac) { 982 if (!max_speed || (max_speed >= 2500)) { 983 phylink_set(mac_supported, 2500baseT_Full); 984 phylink_set(mac_supported, 2500baseX_Full); 985 } 986 if (!max_speed || (max_speed >= 5000)) { 987 phylink_set(mac_supported, 5000baseT_Full); 988 } 989 if (!max_speed || (max_speed >= 10000)) { 990 phylink_set(mac_supported, 10000baseSR_Full); 991 phylink_set(mac_supported, 10000baseLR_Full); 992 phylink_set(mac_supported, 10000baseER_Full); 993 phylink_set(mac_supported, 10000baseLRM_Full); 994 phylink_set(mac_supported, 10000baseT_Full); 995 phylink_set(mac_supported, 10000baseKX4_Full); 996 phylink_set(mac_supported, 10000baseKR_Full); 997 } 998 if (!max_speed || (max_speed >= 25000)) { 999 phylink_set(mac_supported, 25000baseCR_Full); 1000 phylink_set(mac_supported, 25000baseKR_Full); 1001 phylink_set(mac_supported, 25000baseSR_Full); 1002 } 1003 if (!max_speed || (max_speed >= 40000)) { 1004 phylink_set(mac_supported, 40000baseKR4_Full); 1005 phylink_set(mac_supported, 40000baseCR4_Full); 1006 phylink_set(mac_supported, 40000baseSR4_Full); 1007 phylink_set(mac_supported, 40000baseLR4_Full); 1008 } 1009 if (!max_speed || (max_speed >= 50000)) { 1010 phylink_set(mac_supported, 50000baseCR2_Full); 1011 phylink_set(mac_supported, 50000baseKR2_Full); 1012 phylink_set(mac_supported, 50000baseSR2_Full); 1013 phylink_set(mac_supported, 50000baseKR_Full); 1014 phylink_set(mac_supported, 50000baseSR_Full); 1015 phylink_set(mac_supported, 50000baseCR_Full); 1016 phylink_set(mac_supported, 50000baseLR_ER_FR_Full); 1017 phylink_set(mac_supported, 50000baseDR_Full); 1018 } 1019 if (!max_speed || (max_speed >= 100000)) { 1020 phylink_set(mac_supported, 100000baseKR4_Full); 1021 phylink_set(mac_supported, 100000baseSR4_Full); 1022 phylink_set(mac_supported, 100000baseCR4_Full); 1023 phylink_set(mac_supported, 100000baseLR4_ER4_Full); 1024 phylink_set(mac_supported, 100000baseKR2_Full); 1025 phylink_set(mac_supported, 100000baseSR2_Full); 1026 phylink_set(mac_supported, 100000baseCR2_Full); 1027 phylink_set(mac_supported, 100000baseLR2_ER2_FR2_Full); 1028 phylink_set(mac_supported, 100000baseDR2_Full); 1029 } 1030 } 1031 1032 /* Half-Duplex can only work with single queue */ 1033 if (tx_cnt > 1) { 1034 phylink_set(mask, 10baseT_Half); 1035 phylink_set(mask, 100baseT_Half); 1036 phylink_set(mask, 1000baseT_Half); 1037 } 1038 1039 linkmode_and(supported, supported, mac_supported); 1040 linkmode_andnot(supported, supported, mask); 1041 1042 linkmode_and(state->advertising, state->advertising, mac_supported); 1043 linkmode_andnot(state->advertising, state->advertising, mask); 1044 1045 /* If PCS is supported, check which modes it supports. */ 1046 if (priv->hw->xpcs) 1047 xpcs_validate(priv->hw->xpcs, supported, state); 1048 } 1049 1050 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode, 1051 const struct phylink_link_state *state) 1052 { 1053 /* Nothing to do, xpcs_config() handles everything */ 1054 } 1055 1056 static void stmmac_fpe_link_state_handle(struct stmmac_priv *priv, bool is_up) 1057 { 1058 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg; 1059 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state; 1060 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state; 1061 bool *hs_enable = &fpe_cfg->hs_enable; 1062 1063 if (is_up && *hs_enable) { 1064 stmmac_fpe_send_mpacket(priv, priv->ioaddr, MPACKET_VERIFY); 1065 } else { 1066 *lo_state = FPE_STATE_OFF; 1067 *lp_state = FPE_STATE_OFF; 1068 } 1069 } 1070 1071 static void stmmac_mac_link_down(struct phylink_config *config, 1072 unsigned int mode, phy_interface_t interface) 1073 { 1074 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev)); 1075 1076 stmmac_mac_set(priv, priv->ioaddr, false); 1077 priv->eee_active = false; 1078 priv->tx_lpi_enabled = false; 1079 priv->eee_enabled = stmmac_eee_init(priv); 1080 stmmac_set_eee_pls(priv, priv->hw, false); 1081 1082 if (priv->dma_cap.fpesel) 1083 stmmac_fpe_link_state_handle(priv, false); 1084 } 1085 1086 static void stmmac_mac_link_up(struct phylink_config *config, 1087 struct phy_device *phy, 1088 unsigned int mode, phy_interface_t interface, 1089 int speed, int duplex, 1090 bool tx_pause, bool rx_pause) 1091 { 1092 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev)); 1093 u32 ctrl; 1094 1095 ctrl = readl(priv->ioaddr + MAC_CTRL_REG); 1096 ctrl &= ~priv->hw->link.speed_mask; 1097 1098 if (interface == PHY_INTERFACE_MODE_USXGMII) { 1099 switch (speed) { 1100 case SPEED_10000: 1101 ctrl |= priv->hw->link.xgmii.speed10000; 1102 break; 1103 case SPEED_5000: 1104 ctrl |= priv->hw->link.xgmii.speed5000; 1105 break; 1106 case SPEED_2500: 1107 ctrl |= priv->hw->link.xgmii.speed2500; 1108 break; 1109 default: 1110 return; 1111 } 1112 } else if (interface == PHY_INTERFACE_MODE_XLGMII) { 1113 switch (speed) { 1114 case SPEED_100000: 1115 ctrl |= priv->hw->link.xlgmii.speed100000; 1116 break; 1117 case SPEED_50000: 1118 ctrl |= priv->hw->link.xlgmii.speed50000; 1119 break; 1120 case SPEED_40000: 1121 ctrl |= priv->hw->link.xlgmii.speed40000; 1122 break; 1123 case SPEED_25000: 1124 ctrl |= priv->hw->link.xlgmii.speed25000; 1125 break; 1126 case SPEED_10000: 1127 ctrl |= priv->hw->link.xgmii.speed10000; 1128 break; 1129 case SPEED_2500: 1130 ctrl |= priv->hw->link.speed2500; 1131 break; 1132 case SPEED_1000: 1133 ctrl |= priv->hw->link.speed1000; 1134 break; 1135 default: 1136 return; 1137 } 1138 } else { 1139 switch (speed) { 1140 case SPEED_2500: 1141 ctrl |= priv->hw->link.speed2500; 1142 break; 1143 case SPEED_1000: 1144 ctrl |= priv->hw->link.speed1000; 1145 break; 1146 case SPEED_100: 1147 ctrl |= priv->hw->link.speed100; 1148 break; 1149 case SPEED_10: 1150 ctrl |= priv->hw->link.speed10; 1151 break; 1152 default: 1153 return; 1154 } 1155 } 1156 1157 priv->speed = speed; 1158 1159 if (priv->plat->fix_mac_speed) 1160 priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed); 1161 1162 if (!duplex) 1163 ctrl &= ~priv->hw->link.duplex; 1164 else 1165 ctrl |= priv->hw->link.duplex; 1166 1167 /* Flow Control operation */ 1168 if (tx_pause && rx_pause) 1169 stmmac_mac_flow_ctrl(priv, duplex); 1170 1171 writel(ctrl, priv->ioaddr + MAC_CTRL_REG); 1172 1173 stmmac_mac_set(priv, priv->ioaddr, true); 1174 if (phy && priv->dma_cap.eee) { 1175 priv->eee_active = phy_init_eee(phy, 1) >= 0; 1176 priv->eee_enabled = stmmac_eee_init(priv); 1177 priv->tx_lpi_enabled = priv->eee_enabled; 1178 stmmac_set_eee_pls(priv, priv->hw, true); 1179 } 1180 1181 if (priv->dma_cap.fpesel) 1182 stmmac_fpe_link_state_handle(priv, true); 1183 } 1184 1185 static const struct phylink_mac_ops stmmac_phylink_mac_ops = { 1186 .validate = stmmac_validate, 1187 .mac_config = stmmac_mac_config, 1188 .mac_link_down = stmmac_mac_link_down, 1189 .mac_link_up = stmmac_mac_link_up, 1190 }; 1191 1192 /** 1193 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported 1194 * @priv: driver private structure 1195 * Description: this is to verify if the HW supports the PCS. 1196 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is 1197 * configured for the TBI, RTBI, or SGMII PHY interface. 1198 */ 1199 static void stmmac_check_pcs_mode(struct stmmac_priv *priv) 1200 { 1201 int interface = priv->plat->interface; 1202 1203 if (priv->dma_cap.pcs) { 1204 if ((interface == PHY_INTERFACE_MODE_RGMII) || 1205 (interface == PHY_INTERFACE_MODE_RGMII_ID) || 1206 (interface == PHY_INTERFACE_MODE_RGMII_RXID) || 1207 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) { 1208 netdev_dbg(priv->dev, "PCS RGMII support enabled\n"); 1209 priv->hw->pcs = STMMAC_PCS_RGMII; 1210 } else if (interface == PHY_INTERFACE_MODE_SGMII) { 1211 netdev_dbg(priv->dev, "PCS SGMII support enabled\n"); 1212 priv->hw->pcs = STMMAC_PCS_SGMII; 1213 } 1214 } 1215 } 1216 1217 /** 1218 * stmmac_init_phy - PHY initialization 1219 * @dev: net device structure 1220 * Description: it initializes the driver's PHY state, and attaches the PHY 1221 * to the mac driver. 1222 * Return value: 1223 * 0 on success 1224 */ 1225 static int stmmac_init_phy(struct net_device *dev) 1226 { 1227 struct stmmac_priv *priv = netdev_priv(dev); 1228 struct device_node *node; 1229 int ret; 1230 1231 node = priv->plat->phylink_node; 1232 1233 if (node) 1234 ret = phylink_of_phy_connect(priv->phylink, node, 0); 1235 1236 /* Some DT bindings do not set-up the PHY handle. Let's try to 1237 * manually parse it 1238 */ 1239 if (!node || ret) { 1240 int addr = priv->plat->phy_addr; 1241 struct phy_device *phydev; 1242 1243 phydev = mdiobus_get_phy(priv->mii, addr); 1244 if (!phydev) { 1245 netdev_err(priv->dev, "no phy at addr %d\n", addr); 1246 return -ENODEV; 1247 } 1248 1249 ret = phylink_connect_phy(priv->phylink, phydev); 1250 } 1251 1252 if (!priv->plat->pmt) { 1253 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; 1254 1255 phylink_ethtool_get_wol(priv->phylink, &wol); 1256 device_set_wakeup_capable(priv->device, !!wol.supported); 1257 } 1258 1259 return ret; 1260 } 1261 1262 static int stmmac_phy_setup(struct stmmac_priv *priv) 1263 { 1264 struct stmmac_mdio_bus_data *mdio_bus_data = priv->plat->mdio_bus_data; 1265 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node); 1266 int mode = priv->plat->phy_interface; 1267 struct phylink *phylink; 1268 1269 priv->phylink_config.dev = &priv->dev->dev; 1270 priv->phylink_config.type = PHYLINK_NETDEV; 1271 priv->phylink_config.pcs_poll = true; 1272 if (priv->plat->mdio_bus_data) 1273 priv->phylink_config.ovr_an_inband = 1274 mdio_bus_data->xpcs_an_inband; 1275 1276 if (!fwnode) 1277 fwnode = dev_fwnode(priv->device); 1278 1279 phylink = phylink_create(&priv->phylink_config, fwnode, 1280 mode, &stmmac_phylink_mac_ops); 1281 if (IS_ERR(phylink)) 1282 return PTR_ERR(phylink); 1283 1284 if (priv->hw->xpcs) 1285 phylink_set_pcs(phylink, &priv->hw->xpcs->pcs); 1286 1287 priv->phylink = phylink; 1288 return 0; 1289 } 1290 1291 static void stmmac_display_rx_rings(struct stmmac_priv *priv) 1292 { 1293 u32 rx_cnt = priv->plat->rx_queues_to_use; 1294 unsigned int desc_size; 1295 void *head_rx; 1296 u32 queue; 1297 1298 /* Display RX rings */ 1299 for (queue = 0; queue < rx_cnt; queue++) { 1300 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1301 1302 pr_info("\tRX Queue %u rings\n", queue); 1303 1304 if (priv->extend_desc) { 1305 head_rx = (void *)rx_q->dma_erx; 1306 desc_size = sizeof(struct dma_extended_desc); 1307 } else { 1308 head_rx = (void *)rx_q->dma_rx; 1309 desc_size = sizeof(struct dma_desc); 1310 } 1311 1312 /* Display RX ring */ 1313 stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true, 1314 rx_q->dma_rx_phy, desc_size); 1315 } 1316 } 1317 1318 static void stmmac_display_tx_rings(struct stmmac_priv *priv) 1319 { 1320 u32 tx_cnt = priv->plat->tx_queues_to_use; 1321 unsigned int desc_size; 1322 void *head_tx; 1323 u32 queue; 1324 1325 /* Display TX rings */ 1326 for (queue = 0; queue < tx_cnt; queue++) { 1327 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1328 1329 pr_info("\tTX Queue %d rings\n", queue); 1330 1331 if (priv->extend_desc) { 1332 head_tx = (void *)tx_q->dma_etx; 1333 desc_size = sizeof(struct dma_extended_desc); 1334 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) { 1335 head_tx = (void *)tx_q->dma_entx; 1336 desc_size = sizeof(struct dma_edesc); 1337 } else { 1338 head_tx = (void *)tx_q->dma_tx; 1339 desc_size = sizeof(struct dma_desc); 1340 } 1341 1342 stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false, 1343 tx_q->dma_tx_phy, desc_size); 1344 } 1345 } 1346 1347 static void stmmac_display_rings(struct stmmac_priv *priv) 1348 { 1349 /* Display RX ring */ 1350 stmmac_display_rx_rings(priv); 1351 1352 /* Display TX ring */ 1353 stmmac_display_tx_rings(priv); 1354 } 1355 1356 static int stmmac_set_bfsize(int mtu, int bufsize) 1357 { 1358 int ret = bufsize; 1359 1360 if (mtu >= BUF_SIZE_8KiB) 1361 ret = BUF_SIZE_16KiB; 1362 else if (mtu >= BUF_SIZE_4KiB) 1363 ret = BUF_SIZE_8KiB; 1364 else if (mtu >= BUF_SIZE_2KiB) 1365 ret = BUF_SIZE_4KiB; 1366 else if (mtu > DEFAULT_BUFSIZE) 1367 ret = BUF_SIZE_2KiB; 1368 else 1369 ret = DEFAULT_BUFSIZE; 1370 1371 return ret; 1372 } 1373 1374 /** 1375 * stmmac_clear_rx_descriptors - clear RX descriptors 1376 * @priv: driver private structure 1377 * @queue: RX queue index 1378 * Description: this function is called to clear the RX descriptors 1379 * in case of both basic and extended descriptors are used. 1380 */ 1381 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue) 1382 { 1383 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1384 int i; 1385 1386 /* Clear the RX descriptors */ 1387 for (i = 0; i < priv->dma_rx_size; i++) 1388 if (priv->extend_desc) 1389 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic, 1390 priv->use_riwt, priv->mode, 1391 (i == priv->dma_rx_size - 1), 1392 priv->dma_buf_sz); 1393 else 1394 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i], 1395 priv->use_riwt, priv->mode, 1396 (i == priv->dma_rx_size - 1), 1397 priv->dma_buf_sz); 1398 } 1399 1400 /** 1401 * stmmac_clear_tx_descriptors - clear tx descriptors 1402 * @priv: driver private structure 1403 * @queue: TX queue index. 1404 * Description: this function is called to clear the TX descriptors 1405 * in case of both basic and extended descriptors are used. 1406 */ 1407 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue) 1408 { 1409 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1410 int i; 1411 1412 /* Clear the TX descriptors */ 1413 for (i = 0; i < priv->dma_tx_size; i++) { 1414 int last = (i == (priv->dma_tx_size - 1)); 1415 struct dma_desc *p; 1416 1417 if (priv->extend_desc) 1418 p = &tx_q->dma_etx[i].basic; 1419 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 1420 p = &tx_q->dma_entx[i].basic; 1421 else 1422 p = &tx_q->dma_tx[i]; 1423 1424 stmmac_init_tx_desc(priv, p, priv->mode, last); 1425 } 1426 } 1427 1428 /** 1429 * stmmac_clear_descriptors - clear descriptors 1430 * @priv: driver private structure 1431 * Description: this function is called to clear the TX and RX descriptors 1432 * in case of both basic and extended descriptors are used. 1433 */ 1434 static void stmmac_clear_descriptors(struct stmmac_priv *priv) 1435 { 1436 u32 rx_queue_cnt = priv->plat->rx_queues_to_use; 1437 u32 tx_queue_cnt = priv->plat->tx_queues_to_use; 1438 u32 queue; 1439 1440 /* Clear the RX descriptors */ 1441 for (queue = 0; queue < rx_queue_cnt; queue++) 1442 stmmac_clear_rx_descriptors(priv, queue); 1443 1444 /* Clear the TX descriptors */ 1445 for (queue = 0; queue < tx_queue_cnt; queue++) 1446 stmmac_clear_tx_descriptors(priv, queue); 1447 } 1448 1449 /** 1450 * stmmac_init_rx_buffers - init the RX descriptor buffer. 1451 * @priv: driver private structure 1452 * @p: descriptor pointer 1453 * @i: descriptor index 1454 * @flags: gfp flag 1455 * @queue: RX queue index 1456 * Description: this function is called to allocate a receive buffer, perform 1457 * the DMA mapping and init the descriptor. 1458 */ 1459 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p, 1460 int i, gfp_t flags, u32 queue) 1461 { 1462 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1463 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i]; 1464 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN); 1465 1466 if (priv->dma_cap.addr64 <= 32) 1467 gfp |= GFP_DMA32; 1468 1469 if (!buf->page) { 1470 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp); 1471 if (!buf->page) 1472 return -ENOMEM; 1473 buf->page_offset = stmmac_rx_offset(priv); 1474 } 1475 1476 if (priv->sph && !buf->sec_page) { 1477 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp); 1478 if (!buf->sec_page) 1479 return -ENOMEM; 1480 1481 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page); 1482 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true); 1483 } else { 1484 buf->sec_page = NULL; 1485 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false); 1486 } 1487 1488 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset; 1489 1490 stmmac_set_desc_addr(priv, p, buf->addr); 1491 if (priv->dma_buf_sz == BUF_SIZE_16KiB) 1492 stmmac_init_desc3(priv, p); 1493 1494 return 0; 1495 } 1496 1497 /** 1498 * stmmac_free_rx_buffer - free RX dma buffers 1499 * @priv: private structure 1500 * @queue: RX queue index 1501 * @i: buffer index. 1502 */ 1503 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i) 1504 { 1505 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1506 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i]; 1507 1508 if (buf->page) 1509 page_pool_put_full_page(rx_q->page_pool, buf->page, false); 1510 buf->page = NULL; 1511 1512 if (buf->sec_page) 1513 page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false); 1514 buf->sec_page = NULL; 1515 } 1516 1517 /** 1518 * stmmac_free_tx_buffer - free RX dma buffers 1519 * @priv: private structure 1520 * @queue: RX queue index 1521 * @i: buffer index. 1522 */ 1523 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i) 1524 { 1525 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1526 1527 if (tx_q->tx_skbuff_dma[i].buf && 1528 tx_q->tx_skbuff_dma[i].buf_type != STMMAC_TXBUF_T_XDP_TX) { 1529 if (tx_q->tx_skbuff_dma[i].map_as_page) 1530 dma_unmap_page(priv->device, 1531 tx_q->tx_skbuff_dma[i].buf, 1532 tx_q->tx_skbuff_dma[i].len, 1533 DMA_TO_DEVICE); 1534 else 1535 dma_unmap_single(priv->device, 1536 tx_q->tx_skbuff_dma[i].buf, 1537 tx_q->tx_skbuff_dma[i].len, 1538 DMA_TO_DEVICE); 1539 } 1540 1541 if (tx_q->xdpf[i] && 1542 (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_TX || 1543 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XDP_NDO)) { 1544 xdp_return_frame(tx_q->xdpf[i]); 1545 tx_q->xdpf[i] = NULL; 1546 } 1547 1548 if (tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_XSK_TX) 1549 tx_q->xsk_frames_done++; 1550 1551 if (tx_q->tx_skbuff[i] && 1552 tx_q->tx_skbuff_dma[i].buf_type == STMMAC_TXBUF_T_SKB) { 1553 dev_kfree_skb_any(tx_q->tx_skbuff[i]); 1554 tx_q->tx_skbuff[i] = NULL; 1555 } 1556 1557 tx_q->tx_skbuff_dma[i].buf = 0; 1558 tx_q->tx_skbuff_dma[i].map_as_page = false; 1559 } 1560 1561 /** 1562 * dma_free_rx_skbufs - free RX dma buffers 1563 * @priv: private structure 1564 * @queue: RX queue index 1565 */ 1566 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue) 1567 { 1568 int i; 1569 1570 for (i = 0; i < priv->dma_rx_size; i++) 1571 stmmac_free_rx_buffer(priv, queue, i); 1572 } 1573 1574 static int stmmac_alloc_rx_buffers(struct stmmac_priv *priv, u32 queue, 1575 gfp_t flags) 1576 { 1577 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1578 int i; 1579 1580 for (i = 0; i < priv->dma_rx_size; i++) { 1581 struct dma_desc *p; 1582 int ret; 1583 1584 if (priv->extend_desc) 1585 p = &((rx_q->dma_erx + i)->basic); 1586 else 1587 p = rx_q->dma_rx + i; 1588 1589 ret = stmmac_init_rx_buffers(priv, p, i, flags, 1590 queue); 1591 if (ret) 1592 return ret; 1593 1594 rx_q->buf_alloc_num++; 1595 } 1596 1597 return 0; 1598 } 1599 1600 /** 1601 * dma_free_rx_xskbufs - free RX dma buffers from XSK pool 1602 * @priv: private structure 1603 * @queue: RX queue index 1604 */ 1605 static void dma_free_rx_xskbufs(struct stmmac_priv *priv, u32 queue) 1606 { 1607 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1608 int i; 1609 1610 for (i = 0; i < priv->dma_rx_size; i++) { 1611 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i]; 1612 1613 if (!buf->xdp) 1614 continue; 1615 1616 xsk_buff_free(buf->xdp); 1617 buf->xdp = NULL; 1618 } 1619 } 1620 1621 static int stmmac_alloc_rx_buffers_zc(struct stmmac_priv *priv, u32 queue) 1622 { 1623 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1624 int i; 1625 1626 for (i = 0; i < priv->dma_rx_size; i++) { 1627 struct stmmac_rx_buffer *buf; 1628 dma_addr_t dma_addr; 1629 struct dma_desc *p; 1630 1631 if (priv->extend_desc) 1632 p = (struct dma_desc *)(rx_q->dma_erx + i); 1633 else 1634 p = rx_q->dma_rx + i; 1635 1636 buf = &rx_q->buf_pool[i]; 1637 1638 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool); 1639 if (!buf->xdp) 1640 return -ENOMEM; 1641 1642 dma_addr = xsk_buff_xdp_get_dma(buf->xdp); 1643 stmmac_set_desc_addr(priv, p, dma_addr); 1644 rx_q->buf_alloc_num++; 1645 } 1646 1647 return 0; 1648 } 1649 1650 static struct xsk_buff_pool *stmmac_get_xsk_pool(struct stmmac_priv *priv, u32 queue) 1651 { 1652 if (!stmmac_xdp_is_enabled(priv) || !test_bit(queue, priv->af_xdp_zc_qps)) 1653 return NULL; 1654 1655 return xsk_get_pool_from_qid(priv->dev, queue); 1656 } 1657 1658 /** 1659 * __init_dma_rx_desc_rings - init the RX descriptor ring (per queue) 1660 * @priv: driver private structure 1661 * @queue: RX queue index 1662 * @flags: gfp flag. 1663 * Description: this function initializes the DMA RX descriptors 1664 * and allocates the socket buffers. It supports the chained and ring 1665 * modes. 1666 */ 1667 static int __init_dma_rx_desc_rings(struct stmmac_priv *priv, u32 queue, gfp_t flags) 1668 { 1669 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1670 int ret; 1671 1672 netif_dbg(priv, probe, priv->dev, 1673 "(%s) dma_rx_phy=0x%08x\n", __func__, 1674 (u32)rx_q->dma_rx_phy); 1675 1676 stmmac_clear_rx_descriptors(priv, queue); 1677 1678 xdp_rxq_info_unreg_mem_model(&rx_q->xdp_rxq); 1679 1680 rx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue); 1681 1682 if (rx_q->xsk_pool) { 1683 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq, 1684 MEM_TYPE_XSK_BUFF_POOL, 1685 NULL)); 1686 netdev_info(priv->dev, 1687 "Register MEM_TYPE_XSK_BUFF_POOL RxQ-%d\n", 1688 rx_q->queue_index); 1689 xsk_pool_set_rxq_info(rx_q->xsk_pool, &rx_q->xdp_rxq); 1690 } else { 1691 WARN_ON(xdp_rxq_info_reg_mem_model(&rx_q->xdp_rxq, 1692 MEM_TYPE_PAGE_POOL, 1693 rx_q->page_pool)); 1694 netdev_info(priv->dev, 1695 "Register MEM_TYPE_PAGE_POOL RxQ-%d\n", 1696 rx_q->queue_index); 1697 } 1698 1699 if (rx_q->xsk_pool) { 1700 /* RX XDP ZC buffer pool may not be populated, e.g. 1701 * xdpsock TX-only. 1702 */ 1703 stmmac_alloc_rx_buffers_zc(priv, queue); 1704 } else { 1705 ret = stmmac_alloc_rx_buffers(priv, queue, flags); 1706 if (ret < 0) 1707 return -ENOMEM; 1708 } 1709 1710 rx_q->cur_rx = 0; 1711 rx_q->dirty_rx = 0; 1712 1713 /* Setup the chained descriptor addresses */ 1714 if (priv->mode == STMMAC_CHAIN_MODE) { 1715 if (priv->extend_desc) 1716 stmmac_mode_init(priv, rx_q->dma_erx, 1717 rx_q->dma_rx_phy, 1718 priv->dma_rx_size, 1); 1719 else 1720 stmmac_mode_init(priv, rx_q->dma_rx, 1721 rx_q->dma_rx_phy, 1722 priv->dma_rx_size, 0); 1723 } 1724 1725 return 0; 1726 } 1727 1728 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags) 1729 { 1730 struct stmmac_priv *priv = netdev_priv(dev); 1731 u32 rx_count = priv->plat->rx_queues_to_use; 1732 u32 queue; 1733 int ret; 1734 1735 /* RX INITIALIZATION */ 1736 netif_dbg(priv, probe, priv->dev, 1737 "SKB addresses:\nskb\t\tskb data\tdma data\n"); 1738 1739 for (queue = 0; queue < rx_count; queue++) { 1740 ret = __init_dma_rx_desc_rings(priv, queue, flags); 1741 if (ret) 1742 goto err_init_rx_buffers; 1743 } 1744 1745 return 0; 1746 1747 err_init_rx_buffers: 1748 while (queue >= 0) { 1749 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1750 1751 if (rx_q->xsk_pool) 1752 dma_free_rx_xskbufs(priv, queue); 1753 else 1754 dma_free_rx_skbufs(priv, queue); 1755 1756 rx_q->buf_alloc_num = 0; 1757 rx_q->xsk_pool = NULL; 1758 1759 if (queue == 0) 1760 break; 1761 1762 queue--; 1763 } 1764 1765 return ret; 1766 } 1767 1768 /** 1769 * __init_dma_tx_desc_rings - init the TX descriptor ring (per queue) 1770 * @priv: driver private structure 1771 * @queue : TX queue index 1772 * Description: this function initializes the DMA TX descriptors 1773 * and allocates the socket buffers. It supports the chained and ring 1774 * modes. 1775 */ 1776 static int __init_dma_tx_desc_rings(struct stmmac_priv *priv, u32 queue) 1777 { 1778 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1779 int i; 1780 1781 netif_dbg(priv, probe, priv->dev, 1782 "(%s) dma_tx_phy=0x%08x\n", __func__, 1783 (u32)tx_q->dma_tx_phy); 1784 1785 /* Setup the chained descriptor addresses */ 1786 if (priv->mode == STMMAC_CHAIN_MODE) { 1787 if (priv->extend_desc) 1788 stmmac_mode_init(priv, tx_q->dma_etx, 1789 tx_q->dma_tx_phy, 1790 priv->dma_tx_size, 1); 1791 else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) 1792 stmmac_mode_init(priv, tx_q->dma_tx, 1793 tx_q->dma_tx_phy, 1794 priv->dma_tx_size, 0); 1795 } 1796 1797 tx_q->xsk_pool = stmmac_get_xsk_pool(priv, queue); 1798 1799 for (i = 0; i < priv->dma_tx_size; i++) { 1800 struct dma_desc *p; 1801 1802 if (priv->extend_desc) 1803 p = &((tx_q->dma_etx + i)->basic); 1804 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 1805 p = &((tx_q->dma_entx + i)->basic); 1806 else 1807 p = tx_q->dma_tx + i; 1808 1809 stmmac_clear_desc(priv, p); 1810 1811 tx_q->tx_skbuff_dma[i].buf = 0; 1812 tx_q->tx_skbuff_dma[i].map_as_page = false; 1813 tx_q->tx_skbuff_dma[i].len = 0; 1814 tx_q->tx_skbuff_dma[i].last_segment = false; 1815 tx_q->tx_skbuff[i] = NULL; 1816 } 1817 1818 tx_q->dirty_tx = 0; 1819 tx_q->cur_tx = 0; 1820 tx_q->mss = 0; 1821 1822 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue)); 1823 1824 return 0; 1825 } 1826 1827 static int init_dma_tx_desc_rings(struct net_device *dev) 1828 { 1829 struct stmmac_priv *priv = netdev_priv(dev); 1830 u32 tx_queue_cnt; 1831 u32 queue; 1832 1833 tx_queue_cnt = priv->plat->tx_queues_to_use; 1834 1835 for (queue = 0; queue < tx_queue_cnt; queue++) 1836 __init_dma_tx_desc_rings(priv, queue); 1837 1838 return 0; 1839 } 1840 1841 /** 1842 * init_dma_desc_rings - init the RX/TX descriptor rings 1843 * @dev: net device structure 1844 * @flags: gfp flag. 1845 * Description: this function initializes the DMA RX/TX descriptors 1846 * and allocates the socket buffers. It supports the chained and ring 1847 * modes. 1848 */ 1849 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags) 1850 { 1851 struct stmmac_priv *priv = netdev_priv(dev); 1852 int ret; 1853 1854 ret = init_dma_rx_desc_rings(dev, flags); 1855 if (ret) 1856 return ret; 1857 1858 ret = init_dma_tx_desc_rings(dev); 1859 1860 stmmac_clear_descriptors(priv); 1861 1862 if (netif_msg_hw(priv)) 1863 stmmac_display_rings(priv); 1864 1865 return ret; 1866 } 1867 1868 /** 1869 * dma_free_tx_skbufs - free TX dma buffers 1870 * @priv: private structure 1871 * @queue: TX queue index 1872 */ 1873 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue) 1874 { 1875 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1876 int i; 1877 1878 tx_q->xsk_frames_done = 0; 1879 1880 for (i = 0; i < priv->dma_tx_size; i++) 1881 stmmac_free_tx_buffer(priv, queue, i); 1882 1883 if (tx_q->xsk_pool && tx_q->xsk_frames_done) { 1884 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done); 1885 tx_q->xsk_frames_done = 0; 1886 tx_q->xsk_pool = NULL; 1887 } 1888 } 1889 1890 /** 1891 * stmmac_free_tx_skbufs - free TX skb buffers 1892 * @priv: private structure 1893 */ 1894 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv) 1895 { 1896 u32 tx_queue_cnt = priv->plat->tx_queues_to_use; 1897 u32 queue; 1898 1899 for (queue = 0; queue < tx_queue_cnt; queue++) 1900 dma_free_tx_skbufs(priv, queue); 1901 } 1902 1903 /** 1904 * __free_dma_rx_desc_resources - free RX dma desc resources (per queue) 1905 * @priv: private structure 1906 * @queue: RX queue index 1907 */ 1908 static void __free_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue) 1909 { 1910 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1911 1912 /* Release the DMA RX socket buffers */ 1913 if (rx_q->xsk_pool) 1914 dma_free_rx_xskbufs(priv, queue); 1915 else 1916 dma_free_rx_skbufs(priv, queue); 1917 1918 rx_q->buf_alloc_num = 0; 1919 rx_q->xsk_pool = NULL; 1920 1921 /* Free DMA regions of consistent memory previously allocated */ 1922 if (!priv->extend_desc) 1923 dma_free_coherent(priv->device, priv->dma_rx_size * 1924 sizeof(struct dma_desc), 1925 rx_q->dma_rx, rx_q->dma_rx_phy); 1926 else 1927 dma_free_coherent(priv->device, priv->dma_rx_size * 1928 sizeof(struct dma_extended_desc), 1929 rx_q->dma_erx, rx_q->dma_rx_phy); 1930 1931 if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq)) 1932 xdp_rxq_info_unreg(&rx_q->xdp_rxq); 1933 1934 kfree(rx_q->buf_pool); 1935 if (rx_q->page_pool) 1936 page_pool_destroy(rx_q->page_pool); 1937 } 1938 1939 static void free_dma_rx_desc_resources(struct stmmac_priv *priv) 1940 { 1941 u32 rx_count = priv->plat->rx_queues_to_use; 1942 u32 queue; 1943 1944 /* Free RX queue resources */ 1945 for (queue = 0; queue < rx_count; queue++) 1946 __free_dma_rx_desc_resources(priv, queue); 1947 } 1948 1949 /** 1950 * __free_dma_tx_desc_resources - free TX dma desc resources (per queue) 1951 * @priv: private structure 1952 * @queue: TX queue index 1953 */ 1954 static void __free_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue) 1955 { 1956 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1957 size_t size; 1958 void *addr; 1959 1960 /* Release the DMA TX socket buffers */ 1961 dma_free_tx_skbufs(priv, queue); 1962 1963 if (priv->extend_desc) { 1964 size = sizeof(struct dma_extended_desc); 1965 addr = tx_q->dma_etx; 1966 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) { 1967 size = sizeof(struct dma_edesc); 1968 addr = tx_q->dma_entx; 1969 } else { 1970 size = sizeof(struct dma_desc); 1971 addr = tx_q->dma_tx; 1972 } 1973 1974 size *= priv->dma_tx_size; 1975 1976 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy); 1977 1978 kfree(tx_q->tx_skbuff_dma); 1979 kfree(tx_q->tx_skbuff); 1980 } 1981 1982 static void free_dma_tx_desc_resources(struct stmmac_priv *priv) 1983 { 1984 u32 tx_count = priv->plat->tx_queues_to_use; 1985 u32 queue; 1986 1987 /* Free TX queue resources */ 1988 for (queue = 0; queue < tx_count; queue++) 1989 __free_dma_tx_desc_resources(priv, queue); 1990 } 1991 1992 /** 1993 * __alloc_dma_rx_desc_resources - alloc RX resources (per queue). 1994 * @priv: private structure 1995 * @queue: RX queue index 1996 * Description: according to which descriptor can be used (extend or basic) 1997 * this function allocates the resources for TX and RX paths. In case of 1998 * reception, for example, it pre-allocated the RX socket buffer in order to 1999 * allow zero-copy mechanism. 2000 */ 2001 static int __alloc_dma_rx_desc_resources(struct stmmac_priv *priv, u32 queue) 2002 { 2003 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 2004 struct stmmac_channel *ch = &priv->channel[queue]; 2005 bool xdp_prog = stmmac_xdp_is_enabled(priv); 2006 struct page_pool_params pp_params = { 0 }; 2007 unsigned int num_pages; 2008 unsigned int napi_id; 2009 int ret; 2010 2011 rx_q->queue_index = queue; 2012 rx_q->priv_data = priv; 2013 2014 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 2015 pp_params.pool_size = priv->dma_rx_size; 2016 num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE); 2017 pp_params.order = ilog2(num_pages); 2018 pp_params.nid = dev_to_node(priv->device); 2019 pp_params.dev = priv->device; 2020 pp_params.dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE; 2021 pp_params.offset = stmmac_rx_offset(priv); 2022 pp_params.max_len = STMMAC_MAX_RX_BUF_SIZE(num_pages); 2023 2024 rx_q->page_pool = page_pool_create(&pp_params); 2025 if (IS_ERR(rx_q->page_pool)) { 2026 ret = PTR_ERR(rx_q->page_pool); 2027 rx_q->page_pool = NULL; 2028 return ret; 2029 } 2030 2031 rx_q->buf_pool = kcalloc(priv->dma_rx_size, 2032 sizeof(*rx_q->buf_pool), 2033 GFP_KERNEL); 2034 if (!rx_q->buf_pool) 2035 return -ENOMEM; 2036 2037 if (priv->extend_desc) { 2038 rx_q->dma_erx = dma_alloc_coherent(priv->device, 2039 priv->dma_rx_size * 2040 sizeof(struct dma_extended_desc), 2041 &rx_q->dma_rx_phy, 2042 GFP_KERNEL); 2043 if (!rx_q->dma_erx) 2044 return -ENOMEM; 2045 2046 } else { 2047 rx_q->dma_rx = dma_alloc_coherent(priv->device, 2048 priv->dma_rx_size * 2049 sizeof(struct dma_desc), 2050 &rx_q->dma_rx_phy, 2051 GFP_KERNEL); 2052 if (!rx_q->dma_rx) 2053 return -ENOMEM; 2054 } 2055 2056 if (stmmac_xdp_is_enabled(priv) && 2057 test_bit(queue, priv->af_xdp_zc_qps)) 2058 napi_id = ch->rxtx_napi.napi_id; 2059 else 2060 napi_id = ch->rx_napi.napi_id; 2061 2062 ret = xdp_rxq_info_reg(&rx_q->xdp_rxq, priv->dev, 2063 rx_q->queue_index, 2064 napi_id); 2065 if (ret) { 2066 netdev_err(priv->dev, "Failed to register xdp rxq info\n"); 2067 return -EINVAL; 2068 } 2069 2070 return 0; 2071 } 2072 2073 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv) 2074 { 2075 u32 rx_count = priv->plat->rx_queues_to_use; 2076 u32 queue; 2077 int ret; 2078 2079 /* RX queues buffers and DMA */ 2080 for (queue = 0; queue < rx_count; queue++) { 2081 ret = __alloc_dma_rx_desc_resources(priv, queue); 2082 if (ret) 2083 goto err_dma; 2084 } 2085 2086 return 0; 2087 2088 err_dma: 2089 free_dma_rx_desc_resources(priv); 2090 2091 return ret; 2092 } 2093 2094 /** 2095 * __alloc_dma_tx_desc_resources - alloc TX resources (per queue). 2096 * @priv: private structure 2097 * @queue: TX queue index 2098 * Description: according to which descriptor can be used (extend or basic) 2099 * this function allocates the resources for TX and RX paths. In case of 2100 * reception, for example, it pre-allocated the RX socket buffer in order to 2101 * allow zero-copy mechanism. 2102 */ 2103 static int __alloc_dma_tx_desc_resources(struct stmmac_priv *priv, u32 queue) 2104 { 2105 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 2106 size_t size; 2107 void *addr; 2108 2109 tx_q->queue_index = queue; 2110 tx_q->priv_data = priv; 2111 2112 tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size, 2113 sizeof(*tx_q->tx_skbuff_dma), 2114 GFP_KERNEL); 2115 if (!tx_q->tx_skbuff_dma) 2116 return -ENOMEM; 2117 2118 tx_q->tx_skbuff = kcalloc(priv->dma_tx_size, 2119 sizeof(struct sk_buff *), 2120 GFP_KERNEL); 2121 if (!tx_q->tx_skbuff) 2122 return -ENOMEM; 2123 2124 if (priv->extend_desc) 2125 size = sizeof(struct dma_extended_desc); 2126 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 2127 size = sizeof(struct dma_edesc); 2128 else 2129 size = sizeof(struct dma_desc); 2130 2131 size *= priv->dma_tx_size; 2132 2133 addr = dma_alloc_coherent(priv->device, size, 2134 &tx_q->dma_tx_phy, GFP_KERNEL); 2135 if (!addr) 2136 return -ENOMEM; 2137 2138 if (priv->extend_desc) 2139 tx_q->dma_etx = addr; 2140 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 2141 tx_q->dma_entx = addr; 2142 else 2143 tx_q->dma_tx = addr; 2144 2145 return 0; 2146 } 2147 2148 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv) 2149 { 2150 u32 tx_count = priv->plat->tx_queues_to_use; 2151 u32 queue; 2152 int ret; 2153 2154 /* TX queues buffers and DMA */ 2155 for (queue = 0; queue < tx_count; queue++) { 2156 ret = __alloc_dma_tx_desc_resources(priv, queue); 2157 if (ret) 2158 goto err_dma; 2159 } 2160 2161 return 0; 2162 2163 err_dma: 2164 free_dma_tx_desc_resources(priv); 2165 return ret; 2166 } 2167 2168 /** 2169 * alloc_dma_desc_resources - alloc TX/RX resources. 2170 * @priv: private structure 2171 * Description: according to which descriptor can be used (extend or basic) 2172 * this function allocates the resources for TX and RX paths. In case of 2173 * reception, for example, it pre-allocated the RX socket buffer in order to 2174 * allow zero-copy mechanism. 2175 */ 2176 static int alloc_dma_desc_resources(struct stmmac_priv *priv) 2177 { 2178 /* RX Allocation */ 2179 int ret = alloc_dma_rx_desc_resources(priv); 2180 2181 if (ret) 2182 return ret; 2183 2184 ret = alloc_dma_tx_desc_resources(priv); 2185 2186 return ret; 2187 } 2188 2189 /** 2190 * free_dma_desc_resources - free dma desc resources 2191 * @priv: private structure 2192 */ 2193 static void free_dma_desc_resources(struct stmmac_priv *priv) 2194 { 2195 /* Release the DMA TX socket buffers */ 2196 free_dma_tx_desc_resources(priv); 2197 2198 /* Release the DMA RX socket buffers later 2199 * to ensure all pending XDP_TX buffers are returned. 2200 */ 2201 free_dma_rx_desc_resources(priv); 2202 } 2203 2204 /** 2205 * stmmac_mac_enable_rx_queues - Enable MAC rx queues 2206 * @priv: driver private structure 2207 * Description: It is used for enabling the rx queues in the MAC 2208 */ 2209 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv) 2210 { 2211 u32 rx_queues_count = priv->plat->rx_queues_to_use; 2212 int queue; 2213 u8 mode; 2214 2215 for (queue = 0; queue < rx_queues_count; queue++) { 2216 mode = priv->plat->rx_queues_cfg[queue].mode_to_use; 2217 stmmac_rx_queue_enable(priv, priv->hw, mode, queue); 2218 } 2219 } 2220 2221 /** 2222 * stmmac_start_rx_dma - start RX DMA channel 2223 * @priv: driver private structure 2224 * @chan: RX channel index 2225 * Description: 2226 * This starts a RX DMA channel 2227 */ 2228 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan) 2229 { 2230 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan); 2231 stmmac_start_rx(priv, priv->ioaddr, chan); 2232 } 2233 2234 /** 2235 * stmmac_start_tx_dma - start TX DMA channel 2236 * @priv: driver private structure 2237 * @chan: TX channel index 2238 * Description: 2239 * This starts a TX DMA channel 2240 */ 2241 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan) 2242 { 2243 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan); 2244 stmmac_start_tx(priv, priv->ioaddr, chan); 2245 } 2246 2247 /** 2248 * stmmac_stop_rx_dma - stop RX DMA channel 2249 * @priv: driver private structure 2250 * @chan: RX channel index 2251 * Description: 2252 * This stops a RX DMA channel 2253 */ 2254 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan) 2255 { 2256 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan); 2257 stmmac_stop_rx(priv, priv->ioaddr, chan); 2258 } 2259 2260 /** 2261 * stmmac_stop_tx_dma - stop TX DMA channel 2262 * @priv: driver private structure 2263 * @chan: TX channel index 2264 * Description: 2265 * This stops a TX DMA channel 2266 */ 2267 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan) 2268 { 2269 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan); 2270 stmmac_stop_tx(priv, priv->ioaddr, chan); 2271 } 2272 2273 /** 2274 * stmmac_start_all_dma - start all RX and TX DMA channels 2275 * @priv: driver private structure 2276 * Description: 2277 * This starts all the RX and TX DMA channels 2278 */ 2279 static void stmmac_start_all_dma(struct stmmac_priv *priv) 2280 { 2281 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2282 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2283 u32 chan = 0; 2284 2285 for (chan = 0; chan < rx_channels_count; chan++) 2286 stmmac_start_rx_dma(priv, chan); 2287 2288 for (chan = 0; chan < tx_channels_count; chan++) 2289 stmmac_start_tx_dma(priv, chan); 2290 } 2291 2292 /** 2293 * stmmac_stop_all_dma - stop all RX and TX DMA channels 2294 * @priv: driver private structure 2295 * Description: 2296 * This stops the RX and TX DMA channels 2297 */ 2298 static void stmmac_stop_all_dma(struct stmmac_priv *priv) 2299 { 2300 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2301 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2302 u32 chan = 0; 2303 2304 for (chan = 0; chan < rx_channels_count; chan++) 2305 stmmac_stop_rx_dma(priv, chan); 2306 2307 for (chan = 0; chan < tx_channels_count; chan++) 2308 stmmac_stop_tx_dma(priv, chan); 2309 } 2310 2311 /** 2312 * stmmac_dma_operation_mode - HW DMA operation mode 2313 * @priv: driver private structure 2314 * Description: it is used for configuring the DMA operation mode register in 2315 * order to program the tx/rx DMA thresholds or Store-And-Forward mode. 2316 */ 2317 static void stmmac_dma_operation_mode(struct stmmac_priv *priv) 2318 { 2319 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2320 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2321 int rxfifosz = priv->plat->rx_fifo_size; 2322 int txfifosz = priv->plat->tx_fifo_size; 2323 u32 txmode = 0; 2324 u32 rxmode = 0; 2325 u32 chan = 0; 2326 u8 qmode = 0; 2327 2328 if (rxfifosz == 0) 2329 rxfifosz = priv->dma_cap.rx_fifo_size; 2330 if (txfifosz == 0) 2331 txfifosz = priv->dma_cap.tx_fifo_size; 2332 2333 /* Adjust for real per queue fifo size */ 2334 rxfifosz /= rx_channels_count; 2335 txfifosz /= tx_channels_count; 2336 2337 if (priv->plat->force_thresh_dma_mode) { 2338 txmode = tc; 2339 rxmode = tc; 2340 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) { 2341 /* 2342 * In case of GMAC, SF mode can be enabled 2343 * to perform the TX COE in HW. This depends on: 2344 * 1) TX COE if actually supported 2345 * 2) There is no bugged Jumbo frame support 2346 * that needs to not insert csum in the TDES. 2347 */ 2348 txmode = SF_DMA_MODE; 2349 rxmode = SF_DMA_MODE; 2350 priv->xstats.threshold = SF_DMA_MODE; 2351 } else { 2352 txmode = tc; 2353 rxmode = SF_DMA_MODE; 2354 } 2355 2356 /* configure all channels */ 2357 for (chan = 0; chan < rx_channels_count; chan++) { 2358 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan]; 2359 u32 buf_size; 2360 2361 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use; 2362 2363 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, 2364 rxfifosz, qmode); 2365 2366 if (rx_q->xsk_pool) { 2367 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool); 2368 stmmac_set_dma_bfsize(priv, priv->ioaddr, 2369 buf_size, 2370 chan); 2371 } else { 2372 stmmac_set_dma_bfsize(priv, priv->ioaddr, 2373 priv->dma_buf_sz, 2374 chan); 2375 } 2376 } 2377 2378 for (chan = 0; chan < tx_channels_count; chan++) { 2379 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use; 2380 2381 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, 2382 txfifosz, qmode); 2383 } 2384 } 2385 2386 static bool stmmac_xdp_xmit_zc(struct stmmac_priv *priv, u32 queue, u32 budget) 2387 { 2388 struct netdev_queue *nq = netdev_get_tx_queue(priv->dev, queue); 2389 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 2390 struct xsk_buff_pool *pool = tx_q->xsk_pool; 2391 unsigned int entry = tx_q->cur_tx; 2392 struct dma_desc *tx_desc = NULL; 2393 struct xdp_desc xdp_desc; 2394 bool work_done = true; 2395 2396 /* Avoids TX time-out as we are sharing with slow path */ 2397 nq->trans_start = jiffies; 2398 2399 budget = min(budget, stmmac_tx_avail(priv, queue)); 2400 2401 while (budget-- > 0) { 2402 dma_addr_t dma_addr; 2403 bool set_ic; 2404 2405 /* We are sharing with slow path and stop XSK TX desc submission when 2406 * available TX ring is less than threshold. 2407 */ 2408 if (unlikely(stmmac_tx_avail(priv, queue) < STMMAC_TX_XSK_AVAIL) || 2409 !netif_carrier_ok(priv->dev)) { 2410 work_done = false; 2411 break; 2412 } 2413 2414 if (!xsk_tx_peek_desc(pool, &xdp_desc)) 2415 break; 2416 2417 if (likely(priv->extend_desc)) 2418 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry); 2419 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 2420 tx_desc = &tx_q->dma_entx[entry].basic; 2421 else 2422 tx_desc = tx_q->dma_tx + entry; 2423 2424 dma_addr = xsk_buff_raw_get_dma(pool, xdp_desc.addr); 2425 xsk_buff_raw_dma_sync_for_device(pool, dma_addr, xdp_desc.len); 2426 2427 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XSK_TX; 2428 2429 /* To return XDP buffer to XSK pool, we simple call 2430 * xsk_tx_completed(), so we don't need to fill up 2431 * 'buf' and 'xdpf'. 2432 */ 2433 tx_q->tx_skbuff_dma[entry].buf = 0; 2434 tx_q->xdpf[entry] = NULL; 2435 2436 tx_q->tx_skbuff_dma[entry].map_as_page = false; 2437 tx_q->tx_skbuff_dma[entry].len = xdp_desc.len; 2438 tx_q->tx_skbuff_dma[entry].last_segment = true; 2439 tx_q->tx_skbuff_dma[entry].is_jumbo = false; 2440 2441 stmmac_set_desc_addr(priv, tx_desc, dma_addr); 2442 2443 tx_q->tx_count_frames++; 2444 2445 if (!priv->tx_coal_frames[queue]) 2446 set_ic = false; 2447 else if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0) 2448 set_ic = true; 2449 else 2450 set_ic = false; 2451 2452 if (set_ic) { 2453 tx_q->tx_count_frames = 0; 2454 stmmac_set_tx_ic(priv, tx_desc); 2455 priv->xstats.tx_set_ic_bit++; 2456 } 2457 2458 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdp_desc.len, 2459 true, priv->mode, true, true, 2460 xdp_desc.len); 2461 2462 stmmac_enable_dma_transmission(priv, priv->ioaddr); 2463 2464 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size); 2465 entry = tx_q->cur_tx; 2466 } 2467 2468 if (tx_desc) { 2469 stmmac_flush_tx_descriptors(priv, queue); 2470 xsk_tx_release(pool); 2471 } 2472 2473 /* Return true if all of the 3 conditions are met 2474 * a) TX Budget is still available 2475 * b) work_done = true when XSK TX desc peek is empty (no more 2476 * pending XSK TX for transmission) 2477 */ 2478 return !!budget && work_done; 2479 } 2480 2481 /** 2482 * stmmac_tx_clean - to manage the transmission completion 2483 * @priv: driver private structure 2484 * @budget: napi budget limiting this functions packet handling 2485 * @queue: TX queue index 2486 * Description: it reclaims the transmit resources after transmission completes. 2487 */ 2488 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue) 2489 { 2490 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 2491 unsigned int bytes_compl = 0, pkts_compl = 0; 2492 unsigned int entry, xmits = 0, count = 0; 2493 2494 __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue)); 2495 2496 priv->xstats.tx_clean++; 2497 2498 tx_q->xsk_frames_done = 0; 2499 2500 entry = tx_q->dirty_tx; 2501 2502 /* Try to clean all TX complete frame in 1 shot */ 2503 while ((entry != tx_q->cur_tx) && count < priv->dma_tx_size) { 2504 struct xdp_frame *xdpf; 2505 struct sk_buff *skb; 2506 struct dma_desc *p; 2507 int status; 2508 2509 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX || 2510 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) { 2511 xdpf = tx_q->xdpf[entry]; 2512 skb = NULL; 2513 } else if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) { 2514 xdpf = NULL; 2515 skb = tx_q->tx_skbuff[entry]; 2516 } else { 2517 xdpf = NULL; 2518 skb = NULL; 2519 } 2520 2521 if (priv->extend_desc) 2522 p = (struct dma_desc *)(tx_q->dma_etx + entry); 2523 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 2524 p = &tx_q->dma_entx[entry].basic; 2525 else 2526 p = tx_q->dma_tx + entry; 2527 2528 status = stmmac_tx_status(priv, &priv->dev->stats, 2529 &priv->xstats, p, priv->ioaddr); 2530 /* Check if the descriptor is owned by the DMA */ 2531 if (unlikely(status & tx_dma_own)) 2532 break; 2533 2534 count++; 2535 2536 /* Make sure descriptor fields are read after reading 2537 * the own bit. 2538 */ 2539 dma_rmb(); 2540 2541 /* Just consider the last segment and ...*/ 2542 if (likely(!(status & tx_not_ls))) { 2543 /* ... verify the status error condition */ 2544 if (unlikely(status & tx_err)) { 2545 priv->dev->stats.tx_errors++; 2546 } else { 2547 priv->dev->stats.tx_packets++; 2548 priv->xstats.tx_pkt_n++; 2549 priv->xstats.txq_stats[queue].tx_pkt_n++; 2550 } 2551 if (skb) 2552 stmmac_get_tx_hwtstamp(priv, p, skb); 2553 } 2554 2555 if (likely(tx_q->tx_skbuff_dma[entry].buf && 2556 tx_q->tx_skbuff_dma[entry].buf_type != STMMAC_TXBUF_T_XDP_TX)) { 2557 if (tx_q->tx_skbuff_dma[entry].map_as_page) 2558 dma_unmap_page(priv->device, 2559 tx_q->tx_skbuff_dma[entry].buf, 2560 tx_q->tx_skbuff_dma[entry].len, 2561 DMA_TO_DEVICE); 2562 else 2563 dma_unmap_single(priv->device, 2564 tx_q->tx_skbuff_dma[entry].buf, 2565 tx_q->tx_skbuff_dma[entry].len, 2566 DMA_TO_DEVICE); 2567 tx_q->tx_skbuff_dma[entry].buf = 0; 2568 tx_q->tx_skbuff_dma[entry].len = 0; 2569 tx_q->tx_skbuff_dma[entry].map_as_page = false; 2570 } 2571 2572 stmmac_clean_desc3(priv, tx_q, p); 2573 2574 tx_q->tx_skbuff_dma[entry].last_segment = false; 2575 tx_q->tx_skbuff_dma[entry].is_jumbo = false; 2576 2577 if (xdpf && 2578 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_TX) { 2579 xdp_return_frame_rx_napi(xdpf); 2580 tx_q->xdpf[entry] = NULL; 2581 } 2582 2583 if (xdpf && 2584 tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XDP_NDO) { 2585 xdp_return_frame(xdpf); 2586 tx_q->xdpf[entry] = NULL; 2587 } 2588 2589 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_XSK_TX) 2590 tx_q->xsk_frames_done++; 2591 2592 if (tx_q->tx_skbuff_dma[entry].buf_type == STMMAC_TXBUF_T_SKB) { 2593 if (likely(skb)) { 2594 pkts_compl++; 2595 bytes_compl += skb->len; 2596 dev_consume_skb_any(skb); 2597 tx_q->tx_skbuff[entry] = NULL; 2598 } 2599 } 2600 2601 stmmac_release_tx_desc(priv, p, priv->mode); 2602 2603 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size); 2604 } 2605 tx_q->dirty_tx = entry; 2606 2607 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue), 2608 pkts_compl, bytes_compl); 2609 2610 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev, 2611 queue))) && 2612 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) { 2613 2614 netif_dbg(priv, tx_done, priv->dev, 2615 "%s: restart transmit\n", __func__); 2616 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue)); 2617 } 2618 2619 if (tx_q->xsk_pool) { 2620 bool work_done; 2621 2622 if (tx_q->xsk_frames_done) 2623 xsk_tx_completed(tx_q->xsk_pool, tx_q->xsk_frames_done); 2624 2625 if (xsk_uses_need_wakeup(tx_q->xsk_pool)) 2626 xsk_set_tx_need_wakeup(tx_q->xsk_pool); 2627 2628 /* For XSK TX, we try to send as many as possible. 2629 * If XSK work done (XSK TX desc empty and budget still 2630 * available), return "budget - 1" to reenable TX IRQ. 2631 * Else, return "budget" to make NAPI continue polling. 2632 */ 2633 work_done = stmmac_xdp_xmit_zc(priv, queue, 2634 STMMAC_XSK_TX_BUDGET_MAX); 2635 if (work_done) 2636 xmits = budget - 1; 2637 else 2638 xmits = budget; 2639 } 2640 2641 if (priv->eee_enabled && !priv->tx_path_in_lpi_mode && 2642 priv->eee_sw_timer_en) { 2643 stmmac_enable_eee_mode(priv); 2644 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer)); 2645 } 2646 2647 /* We still have pending packets, let's call for a new scheduling */ 2648 if (tx_q->dirty_tx != tx_q->cur_tx) 2649 hrtimer_start(&tx_q->txtimer, 2650 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]), 2651 HRTIMER_MODE_REL); 2652 2653 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue)); 2654 2655 /* Combine decisions from TX clean and XSK TX */ 2656 return max(count, xmits); 2657 } 2658 2659 /** 2660 * stmmac_tx_err - to manage the tx error 2661 * @priv: driver private structure 2662 * @chan: channel index 2663 * Description: it cleans the descriptors and restarts the transmission 2664 * in case of transmission errors. 2665 */ 2666 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan) 2667 { 2668 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 2669 2670 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan)); 2671 2672 stmmac_stop_tx_dma(priv, chan); 2673 dma_free_tx_skbufs(priv, chan); 2674 stmmac_clear_tx_descriptors(priv, chan); 2675 tx_q->dirty_tx = 0; 2676 tx_q->cur_tx = 0; 2677 tx_q->mss = 0; 2678 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan)); 2679 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg, 2680 tx_q->dma_tx_phy, chan); 2681 stmmac_start_tx_dma(priv, chan); 2682 2683 priv->dev->stats.tx_errors++; 2684 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan)); 2685 } 2686 2687 /** 2688 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel 2689 * @priv: driver private structure 2690 * @txmode: TX operating mode 2691 * @rxmode: RX operating mode 2692 * @chan: channel index 2693 * Description: it is used for configuring of the DMA operation mode in 2694 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward 2695 * mode. 2696 */ 2697 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode, 2698 u32 rxmode, u32 chan) 2699 { 2700 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use; 2701 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use; 2702 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2703 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2704 int rxfifosz = priv->plat->rx_fifo_size; 2705 int txfifosz = priv->plat->tx_fifo_size; 2706 2707 if (rxfifosz == 0) 2708 rxfifosz = priv->dma_cap.rx_fifo_size; 2709 if (txfifosz == 0) 2710 txfifosz = priv->dma_cap.tx_fifo_size; 2711 2712 /* Adjust for real per queue fifo size */ 2713 rxfifosz /= rx_channels_count; 2714 txfifosz /= tx_channels_count; 2715 2716 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode); 2717 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode); 2718 } 2719 2720 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv) 2721 { 2722 int ret; 2723 2724 ret = stmmac_safety_feat_irq_status(priv, priv->dev, 2725 priv->ioaddr, priv->dma_cap.asp, &priv->sstats); 2726 if (ret && (ret != -EINVAL)) { 2727 stmmac_global_err(priv); 2728 return true; 2729 } 2730 2731 return false; 2732 } 2733 2734 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan, u32 dir) 2735 { 2736 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr, 2737 &priv->xstats, chan, dir); 2738 struct stmmac_rx_queue *rx_q = &priv->rx_queue[chan]; 2739 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 2740 struct stmmac_channel *ch = &priv->channel[chan]; 2741 struct napi_struct *rx_napi; 2742 struct napi_struct *tx_napi; 2743 unsigned long flags; 2744 2745 rx_napi = rx_q->xsk_pool ? &ch->rxtx_napi : &ch->rx_napi; 2746 tx_napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi; 2747 2748 if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) { 2749 if (napi_schedule_prep(rx_napi)) { 2750 spin_lock_irqsave(&ch->lock, flags); 2751 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0); 2752 spin_unlock_irqrestore(&ch->lock, flags); 2753 __napi_schedule(rx_napi); 2754 } 2755 } 2756 2757 if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) { 2758 if (napi_schedule_prep(tx_napi)) { 2759 spin_lock_irqsave(&ch->lock, flags); 2760 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1); 2761 spin_unlock_irqrestore(&ch->lock, flags); 2762 __napi_schedule(tx_napi); 2763 } 2764 } 2765 2766 return status; 2767 } 2768 2769 /** 2770 * stmmac_dma_interrupt - DMA ISR 2771 * @priv: driver private structure 2772 * Description: this is the DMA ISR. It is called by the main ISR. 2773 * It calls the dwmac dma routine and schedule poll method in case of some 2774 * work can be done. 2775 */ 2776 static void stmmac_dma_interrupt(struct stmmac_priv *priv) 2777 { 2778 u32 tx_channel_count = priv->plat->tx_queues_to_use; 2779 u32 rx_channel_count = priv->plat->rx_queues_to_use; 2780 u32 channels_to_check = tx_channel_count > rx_channel_count ? 2781 tx_channel_count : rx_channel_count; 2782 u32 chan; 2783 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)]; 2784 2785 /* Make sure we never check beyond our status buffer. */ 2786 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status))) 2787 channels_to_check = ARRAY_SIZE(status); 2788 2789 for (chan = 0; chan < channels_to_check; chan++) 2790 status[chan] = stmmac_napi_check(priv, chan, 2791 DMA_DIR_RXTX); 2792 2793 for (chan = 0; chan < tx_channel_count; chan++) { 2794 if (unlikely(status[chan] & tx_hard_error_bump_tc)) { 2795 /* Try to bump up the dma threshold on this failure */ 2796 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) && 2797 (tc <= 256)) { 2798 tc += 64; 2799 if (priv->plat->force_thresh_dma_mode) 2800 stmmac_set_dma_operation_mode(priv, 2801 tc, 2802 tc, 2803 chan); 2804 else 2805 stmmac_set_dma_operation_mode(priv, 2806 tc, 2807 SF_DMA_MODE, 2808 chan); 2809 priv->xstats.threshold = tc; 2810 } 2811 } else if (unlikely(status[chan] == tx_hard_error)) { 2812 stmmac_tx_err(priv, chan); 2813 } 2814 } 2815 } 2816 2817 /** 2818 * stmmac_mmc_setup: setup the Mac Management Counters (MMC) 2819 * @priv: driver private structure 2820 * Description: this masks the MMC irq, in fact, the counters are managed in SW. 2821 */ 2822 static void stmmac_mmc_setup(struct stmmac_priv *priv) 2823 { 2824 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET | 2825 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET; 2826 2827 stmmac_mmc_intr_all_mask(priv, priv->mmcaddr); 2828 2829 if (priv->dma_cap.rmon) { 2830 stmmac_mmc_ctrl(priv, priv->mmcaddr, mode); 2831 memset(&priv->mmc, 0, sizeof(struct stmmac_counters)); 2832 } else 2833 netdev_info(priv->dev, "No MAC Management Counters available\n"); 2834 } 2835 2836 /** 2837 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register. 2838 * @priv: driver private structure 2839 * Description: 2840 * new GMAC chip generations have a new register to indicate the 2841 * presence of the optional feature/functions. 2842 * This can be also used to override the value passed through the 2843 * platform and necessary for old MAC10/100 and GMAC chips. 2844 */ 2845 static int stmmac_get_hw_features(struct stmmac_priv *priv) 2846 { 2847 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0; 2848 } 2849 2850 /** 2851 * stmmac_check_ether_addr - check if the MAC addr is valid 2852 * @priv: driver private structure 2853 * Description: 2854 * it is to verify if the MAC address is valid, in case of failures it 2855 * generates a random MAC address 2856 */ 2857 static void stmmac_check_ether_addr(struct stmmac_priv *priv) 2858 { 2859 u8 addr[ETH_ALEN]; 2860 2861 if (!is_valid_ether_addr(priv->dev->dev_addr)) { 2862 stmmac_get_umac_addr(priv, priv->hw, addr, 0); 2863 if (is_valid_ether_addr(addr)) 2864 eth_hw_addr_set(priv->dev, addr); 2865 else 2866 eth_hw_addr_random(priv->dev); 2867 dev_info(priv->device, "device MAC address %pM\n", 2868 priv->dev->dev_addr); 2869 } 2870 } 2871 2872 /** 2873 * stmmac_init_dma_engine - DMA init. 2874 * @priv: driver private structure 2875 * Description: 2876 * It inits the DMA invoking the specific MAC/GMAC callback. 2877 * Some DMA parameters can be passed from the platform; 2878 * in case of these are not passed a default is kept for the MAC or GMAC. 2879 */ 2880 static int stmmac_init_dma_engine(struct stmmac_priv *priv) 2881 { 2882 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2883 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2884 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count); 2885 struct stmmac_rx_queue *rx_q; 2886 struct stmmac_tx_queue *tx_q; 2887 u32 chan = 0; 2888 int atds = 0; 2889 int ret = 0; 2890 2891 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) { 2892 dev_err(priv->device, "Invalid DMA configuration\n"); 2893 return -EINVAL; 2894 } 2895 2896 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE)) 2897 atds = 1; 2898 2899 ret = stmmac_reset(priv, priv->ioaddr); 2900 if (ret) { 2901 dev_err(priv->device, "Failed to reset the dma\n"); 2902 return ret; 2903 } 2904 2905 /* DMA Configuration */ 2906 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds); 2907 2908 if (priv->plat->axi) 2909 stmmac_axi(priv, priv->ioaddr, priv->plat->axi); 2910 2911 /* DMA CSR Channel configuration */ 2912 for (chan = 0; chan < dma_csr_ch; chan++) 2913 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan); 2914 2915 /* DMA RX Channel Configuration */ 2916 for (chan = 0; chan < rx_channels_count; chan++) { 2917 rx_q = &priv->rx_queue[chan]; 2918 2919 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg, 2920 rx_q->dma_rx_phy, chan); 2921 2922 rx_q->rx_tail_addr = rx_q->dma_rx_phy + 2923 (rx_q->buf_alloc_num * 2924 sizeof(struct dma_desc)); 2925 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, 2926 rx_q->rx_tail_addr, chan); 2927 } 2928 2929 /* DMA TX Channel Configuration */ 2930 for (chan = 0; chan < tx_channels_count; chan++) { 2931 tx_q = &priv->tx_queue[chan]; 2932 2933 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg, 2934 tx_q->dma_tx_phy, chan); 2935 2936 tx_q->tx_tail_addr = tx_q->dma_tx_phy; 2937 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, 2938 tx_q->tx_tail_addr, chan); 2939 } 2940 2941 return ret; 2942 } 2943 2944 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue) 2945 { 2946 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 2947 2948 hrtimer_start(&tx_q->txtimer, 2949 STMMAC_COAL_TIMER(priv->tx_coal_timer[queue]), 2950 HRTIMER_MODE_REL); 2951 } 2952 2953 /** 2954 * stmmac_tx_timer - mitigation sw timer for tx. 2955 * @t: data pointer 2956 * Description: 2957 * This is the timer handler to directly invoke the stmmac_tx_clean. 2958 */ 2959 static enum hrtimer_restart stmmac_tx_timer(struct hrtimer *t) 2960 { 2961 struct stmmac_tx_queue *tx_q = container_of(t, struct stmmac_tx_queue, txtimer); 2962 struct stmmac_priv *priv = tx_q->priv_data; 2963 struct stmmac_channel *ch; 2964 struct napi_struct *napi; 2965 2966 ch = &priv->channel[tx_q->queue_index]; 2967 napi = tx_q->xsk_pool ? &ch->rxtx_napi : &ch->tx_napi; 2968 2969 if (likely(napi_schedule_prep(napi))) { 2970 unsigned long flags; 2971 2972 spin_lock_irqsave(&ch->lock, flags); 2973 stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1); 2974 spin_unlock_irqrestore(&ch->lock, flags); 2975 __napi_schedule(napi); 2976 } 2977 2978 return HRTIMER_NORESTART; 2979 } 2980 2981 /** 2982 * stmmac_init_coalesce - init mitigation options. 2983 * @priv: driver private structure 2984 * Description: 2985 * This inits the coalesce parameters: i.e. timer rate, 2986 * timer handler and default threshold used for enabling the 2987 * interrupt on completion bit. 2988 */ 2989 static void stmmac_init_coalesce(struct stmmac_priv *priv) 2990 { 2991 u32 tx_channel_count = priv->plat->tx_queues_to_use; 2992 u32 rx_channel_count = priv->plat->rx_queues_to_use; 2993 u32 chan; 2994 2995 for (chan = 0; chan < tx_channel_count; chan++) { 2996 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 2997 2998 priv->tx_coal_frames[chan] = STMMAC_TX_FRAMES; 2999 priv->tx_coal_timer[chan] = STMMAC_COAL_TX_TIMER; 3000 3001 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 3002 tx_q->txtimer.function = stmmac_tx_timer; 3003 } 3004 3005 for (chan = 0; chan < rx_channel_count; chan++) 3006 priv->rx_coal_frames[chan] = STMMAC_RX_FRAMES; 3007 } 3008 3009 static void stmmac_set_rings_length(struct stmmac_priv *priv) 3010 { 3011 u32 rx_channels_count = priv->plat->rx_queues_to_use; 3012 u32 tx_channels_count = priv->plat->tx_queues_to_use; 3013 u32 chan; 3014 3015 /* set TX ring length */ 3016 for (chan = 0; chan < tx_channels_count; chan++) 3017 stmmac_set_tx_ring_len(priv, priv->ioaddr, 3018 (priv->dma_tx_size - 1), chan); 3019 3020 /* set RX ring length */ 3021 for (chan = 0; chan < rx_channels_count; chan++) 3022 stmmac_set_rx_ring_len(priv, priv->ioaddr, 3023 (priv->dma_rx_size - 1), chan); 3024 } 3025 3026 /** 3027 * stmmac_set_tx_queue_weight - Set TX queue weight 3028 * @priv: driver private structure 3029 * Description: It is used for setting TX queues weight 3030 */ 3031 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv) 3032 { 3033 u32 tx_queues_count = priv->plat->tx_queues_to_use; 3034 u32 weight; 3035 u32 queue; 3036 3037 for (queue = 0; queue < tx_queues_count; queue++) { 3038 weight = priv->plat->tx_queues_cfg[queue].weight; 3039 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue); 3040 } 3041 } 3042 3043 /** 3044 * stmmac_configure_cbs - Configure CBS in TX queue 3045 * @priv: driver private structure 3046 * Description: It is used for configuring CBS in AVB TX queues 3047 */ 3048 static void stmmac_configure_cbs(struct stmmac_priv *priv) 3049 { 3050 u32 tx_queues_count = priv->plat->tx_queues_to_use; 3051 u32 mode_to_use; 3052 u32 queue; 3053 3054 /* queue 0 is reserved for legacy traffic */ 3055 for (queue = 1; queue < tx_queues_count; queue++) { 3056 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use; 3057 if (mode_to_use == MTL_QUEUE_DCB) 3058 continue; 3059 3060 stmmac_config_cbs(priv, priv->hw, 3061 priv->plat->tx_queues_cfg[queue].send_slope, 3062 priv->plat->tx_queues_cfg[queue].idle_slope, 3063 priv->plat->tx_queues_cfg[queue].high_credit, 3064 priv->plat->tx_queues_cfg[queue].low_credit, 3065 queue); 3066 } 3067 } 3068 3069 /** 3070 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel 3071 * @priv: driver private structure 3072 * Description: It is used for mapping RX queues to RX dma channels 3073 */ 3074 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv) 3075 { 3076 u32 rx_queues_count = priv->plat->rx_queues_to_use; 3077 u32 queue; 3078 u32 chan; 3079 3080 for (queue = 0; queue < rx_queues_count; queue++) { 3081 chan = priv->plat->rx_queues_cfg[queue].chan; 3082 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan); 3083 } 3084 } 3085 3086 /** 3087 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority 3088 * @priv: driver private structure 3089 * Description: It is used for configuring the RX Queue Priority 3090 */ 3091 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv) 3092 { 3093 u32 rx_queues_count = priv->plat->rx_queues_to_use; 3094 u32 queue; 3095 u32 prio; 3096 3097 for (queue = 0; queue < rx_queues_count; queue++) { 3098 if (!priv->plat->rx_queues_cfg[queue].use_prio) 3099 continue; 3100 3101 prio = priv->plat->rx_queues_cfg[queue].prio; 3102 stmmac_rx_queue_prio(priv, priv->hw, prio, queue); 3103 } 3104 } 3105 3106 /** 3107 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority 3108 * @priv: driver private structure 3109 * Description: It is used for configuring the TX Queue Priority 3110 */ 3111 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv) 3112 { 3113 u32 tx_queues_count = priv->plat->tx_queues_to_use; 3114 u32 queue; 3115 u32 prio; 3116 3117 for (queue = 0; queue < tx_queues_count; queue++) { 3118 if (!priv->plat->tx_queues_cfg[queue].use_prio) 3119 continue; 3120 3121 prio = priv->plat->tx_queues_cfg[queue].prio; 3122 stmmac_tx_queue_prio(priv, priv->hw, prio, queue); 3123 } 3124 } 3125 3126 /** 3127 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing 3128 * @priv: driver private structure 3129 * Description: It is used for configuring the RX queue routing 3130 */ 3131 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv) 3132 { 3133 u32 rx_queues_count = priv->plat->rx_queues_to_use; 3134 u32 queue; 3135 u8 packet; 3136 3137 for (queue = 0; queue < rx_queues_count; queue++) { 3138 /* no specific packet type routing specified for the queue */ 3139 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0) 3140 continue; 3141 3142 packet = priv->plat->rx_queues_cfg[queue].pkt_route; 3143 stmmac_rx_queue_routing(priv, priv->hw, packet, queue); 3144 } 3145 } 3146 3147 static void stmmac_mac_config_rss(struct stmmac_priv *priv) 3148 { 3149 if (!priv->dma_cap.rssen || !priv->plat->rss_en) { 3150 priv->rss.enable = false; 3151 return; 3152 } 3153 3154 if (priv->dev->features & NETIF_F_RXHASH) 3155 priv->rss.enable = true; 3156 else 3157 priv->rss.enable = false; 3158 3159 stmmac_rss_configure(priv, priv->hw, &priv->rss, 3160 priv->plat->rx_queues_to_use); 3161 } 3162 3163 /** 3164 * stmmac_mtl_configuration - Configure MTL 3165 * @priv: driver private structure 3166 * Description: It is used for configurring MTL 3167 */ 3168 static void stmmac_mtl_configuration(struct stmmac_priv *priv) 3169 { 3170 u32 rx_queues_count = priv->plat->rx_queues_to_use; 3171 u32 tx_queues_count = priv->plat->tx_queues_to_use; 3172 3173 if (tx_queues_count > 1) 3174 stmmac_set_tx_queue_weight(priv); 3175 3176 /* Configure MTL RX algorithms */ 3177 if (rx_queues_count > 1) 3178 stmmac_prog_mtl_rx_algorithms(priv, priv->hw, 3179 priv->plat->rx_sched_algorithm); 3180 3181 /* Configure MTL TX algorithms */ 3182 if (tx_queues_count > 1) 3183 stmmac_prog_mtl_tx_algorithms(priv, priv->hw, 3184 priv->plat->tx_sched_algorithm); 3185 3186 /* Configure CBS in AVB TX queues */ 3187 if (tx_queues_count > 1) 3188 stmmac_configure_cbs(priv); 3189 3190 /* Map RX MTL to DMA channels */ 3191 stmmac_rx_queue_dma_chan_map(priv); 3192 3193 /* Enable MAC RX Queues */ 3194 stmmac_mac_enable_rx_queues(priv); 3195 3196 /* Set RX priorities */ 3197 if (rx_queues_count > 1) 3198 stmmac_mac_config_rx_queues_prio(priv); 3199 3200 /* Set TX priorities */ 3201 if (tx_queues_count > 1) 3202 stmmac_mac_config_tx_queues_prio(priv); 3203 3204 /* Set RX routing */ 3205 if (rx_queues_count > 1) 3206 stmmac_mac_config_rx_queues_routing(priv); 3207 3208 /* Receive Side Scaling */ 3209 if (rx_queues_count > 1) 3210 stmmac_mac_config_rss(priv); 3211 } 3212 3213 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv) 3214 { 3215 if (priv->dma_cap.asp) { 3216 netdev_info(priv->dev, "Enabling Safety Features\n"); 3217 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp, 3218 priv->plat->safety_feat_cfg); 3219 } else { 3220 netdev_info(priv->dev, "No Safety Features support found\n"); 3221 } 3222 } 3223 3224 static int stmmac_fpe_start_wq(struct stmmac_priv *priv) 3225 { 3226 char *name; 3227 3228 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state); 3229 clear_bit(__FPE_REMOVING, &priv->fpe_task_state); 3230 3231 name = priv->wq_name; 3232 sprintf(name, "%s-fpe", priv->dev->name); 3233 3234 priv->fpe_wq = create_singlethread_workqueue(name); 3235 if (!priv->fpe_wq) { 3236 netdev_err(priv->dev, "%s: Failed to create workqueue\n", name); 3237 3238 return -ENOMEM; 3239 } 3240 netdev_info(priv->dev, "FPE workqueue start"); 3241 3242 return 0; 3243 } 3244 3245 /** 3246 * stmmac_hw_setup - setup mac in a usable state. 3247 * @dev : pointer to the device structure. 3248 * @init_ptp: initialize PTP if set 3249 * Description: 3250 * this is the main function to setup the HW in a usable state because the 3251 * dma engine is reset, the core registers are configured (e.g. AXI, 3252 * Checksum features, timers). The DMA is ready to start receiving and 3253 * transmitting. 3254 * Return value: 3255 * 0 on success and an appropriate (-)ve integer as defined in errno.h 3256 * file on failure. 3257 */ 3258 static int stmmac_hw_setup(struct net_device *dev, bool init_ptp) 3259 { 3260 struct stmmac_priv *priv = netdev_priv(dev); 3261 u32 rx_cnt = priv->plat->rx_queues_to_use; 3262 u32 tx_cnt = priv->plat->tx_queues_to_use; 3263 bool sph_en; 3264 u32 chan; 3265 int ret; 3266 3267 /* DMA initialization and SW reset */ 3268 ret = stmmac_init_dma_engine(priv); 3269 if (ret < 0) { 3270 netdev_err(priv->dev, "%s: DMA engine initialization failed\n", 3271 __func__); 3272 return ret; 3273 } 3274 3275 /* Copy the MAC addr into the HW */ 3276 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0); 3277 3278 /* PS and related bits will be programmed according to the speed */ 3279 if (priv->hw->pcs) { 3280 int speed = priv->plat->mac_port_sel_speed; 3281 3282 if ((speed == SPEED_10) || (speed == SPEED_100) || 3283 (speed == SPEED_1000)) { 3284 priv->hw->ps = speed; 3285 } else { 3286 dev_warn(priv->device, "invalid port speed\n"); 3287 priv->hw->ps = 0; 3288 } 3289 } 3290 3291 /* Initialize the MAC Core */ 3292 stmmac_core_init(priv, priv->hw, dev); 3293 3294 /* Initialize MTL*/ 3295 stmmac_mtl_configuration(priv); 3296 3297 /* Initialize Safety Features */ 3298 stmmac_safety_feat_configuration(priv); 3299 3300 ret = stmmac_rx_ipc(priv, priv->hw); 3301 if (!ret) { 3302 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n"); 3303 priv->plat->rx_coe = STMMAC_RX_COE_NONE; 3304 priv->hw->rx_csum = 0; 3305 } 3306 3307 /* Enable the MAC Rx/Tx */ 3308 stmmac_mac_set(priv, priv->ioaddr, true); 3309 3310 /* Set the HW DMA mode and the COE */ 3311 stmmac_dma_operation_mode(priv); 3312 3313 stmmac_mmc_setup(priv); 3314 3315 if (init_ptp) { 3316 ret = stmmac_init_ptp(priv); 3317 if (ret == -EOPNOTSUPP) 3318 netdev_warn(priv->dev, "PTP not supported by HW\n"); 3319 else if (ret) 3320 netdev_warn(priv->dev, "PTP init failed\n"); 3321 } 3322 3323 priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS; 3324 3325 /* Convert the timer from msec to usec */ 3326 if (!priv->tx_lpi_timer) 3327 priv->tx_lpi_timer = eee_timer * 1000; 3328 3329 if (priv->use_riwt) { 3330 u32 queue; 3331 3332 for (queue = 0; queue < rx_cnt; queue++) { 3333 if (!priv->rx_riwt[queue]) 3334 priv->rx_riwt[queue] = DEF_DMA_RIWT; 3335 3336 stmmac_rx_watchdog(priv, priv->ioaddr, 3337 priv->rx_riwt[queue], queue); 3338 } 3339 } 3340 3341 if (priv->hw->pcs) 3342 stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0); 3343 3344 /* set TX and RX rings length */ 3345 stmmac_set_rings_length(priv); 3346 3347 /* Enable TSO */ 3348 if (priv->tso) { 3349 for (chan = 0; chan < tx_cnt; chan++) { 3350 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 3351 3352 /* TSO and TBS cannot co-exist */ 3353 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3354 continue; 3355 3356 stmmac_enable_tso(priv, priv->ioaddr, 1, chan); 3357 } 3358 } 3359 3360 /* Enable Split Header */ 3361 sph_en = (priv->hw->rx_csum > 0) && priv->sph; 3362 for (chan = 0; chan < rx_cnt; chan++) 3363 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan); 3364 3365 3366 /* VLAN Tag Insertion */ 3367 if (priv->dma_cap.vlins) 3368 stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT); 3369 3370 /* TBS */ 3371 for (chan = 0; chan < tx_cnt; chan++) { 3372 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 3373 int enable = tx_q->tbs & STMMAC_TBS_AVAIL; 3374 3375 stmmac_enable_tbs(priv, priv->ioaddr, enable, chan); 3376 } 3377 3378 /* Configure real RX and TX queues */ 3379 netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use); 3380 netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use); 3381 3382 /* Start the ball rolling... */ 3383 stmmac_start_all_dma(priv); 3384 3385 if (priv->dma_cap.fpesel) { 3386 stmmac_fpe_start_wq(priv); 3387 3388 if (priv->plat->fpe_cfg->enable) 3389 stmmac_fpe_handshake(priv, true); 3390 } 3391 3392 return 0; 3393 } 3394 3395 static void stmmac_hw_teardown(struct net_device *dev) 3396 { 3397 struct stmmac_priv *priv = netdev_priv(dev); 3398 3399 clk_disable_unprepare(priv->plat->clk_ptp_ref); 3400 } 3401 3402 static void stmmac_free_irq(struct net_device *dev, 3403 enum request_irq_err irq_err, int irq_idx) 3404 { 3405 struct stmmac_priv *priv = netdev_priv(dev); 3406 int j; 3407 3408 switch (irq_err) { 3409 case REQ_IRQ_ERR_ALL: 3410 irq_idx = priv->plat->tx_queues_to_use; 3411 fallthrough; 3412 case REQ_IRQ_ERR_TX: 3413 for (j = irq_idx - 1; j >= 0; j--) { 3414 if (priv->tx_irq[j] > 0) { 3415 irq_set_affinity_hint(priv->tx_irq[j], NULL); 3416 free_irq(priv->tx_irq[j], &priv->tx_queue[j]); 3417 } 3418 } 3419 irq_idx = priv->plat->rx_queues_to_use; 3420 fallthrough; 3421 case REQ_IRQ_ERR_RX: 3422 for (j = irq_idx - 1; j >= 0; j--) { 3423 if (priv->rx_irq[j] > 0) { 3424 irq_set_affinity_hint(priv->rx_irq[j], NULL); 3425 free_irq(priv->rx_irq[j], &priv->rx_queue[j]); 3426 } 3427 } 3428 3429 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq) 3430 free_irq(priv->sfty_ue_irq, dev); 3431 fallthrough; 3432 case REQ_IRQ_ERR_SFTY_UE: 3433 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq) 3434 free_irq(priv->sfty_ce_irq, dev); 3435 fallthrough; 3436 case REQ_IRQ_ERR_SFTY_CE: 3437 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) 3438 free_irq(priv->lpi_irq, dev); 3439 fallthrough; 3440 case REQ_IRQ_ERR_LPI: 3441 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) 3442 free_irq(priv->wol_irq, dev); 3443 fallthrough; 3444 case REQ_IRQ_ERR_WOL: 3445 free_irq(dev->irq, dev); 3446 fallthrough; 3447 case REQ_IRQ_ERR_MAC: 3448 case REQ_IRQ_ERR_NO: 3449 /* If MAC IRQ request error, no more IRQ to free */ 3450 break; 3451 } 3452 } 3453 3454 static int stmmac_request_irq_multi_msi(struct net_device *dev) 3455 { 3456 struct stmmac_priv *priv = netdev_priv(dev); 3457 enum request_irq_err irq_err; 3458 cpumask_t cpu_mask; 3459 int irq_idx = 0; 3460 char *int_name; 3461 int ret; 3462 int i; 3463 3464 /* For common interrupt */ 3465 int_name = priv->int_name_mac; 3466 sprintf(int_name, "%s:%s", dev->name, "mac"); 3467 ret = request_irq(dev->irq, stmmac_mac_interrupt, 3468 0, int_name, dev); 3469 if (unlikely(ret < 0)) { 3470 netdev_err(priv->dev, 3471 "%s: alloc mac MSI %d (error: %d)\n", 3472 __func__, dev->irq, ret); 3473 irq_err = REQ_IRQ_ERR_MAC; 3474 goto irq_error; 3475 } 3476 3477 /* Request the Wake IRQ in case of another line 3478 * is used for WoL 3479 */ 3480 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) { 3481 int_name = priv->int_name_wol; 3482 sprintf(int_name, "%s:%s", dev->name, "wol"); 3483 ret = request_irq(priv->wol_irq, 3484 stmmac_mac_interrupt, 3485 0, int_name, dev); 3486 if (unlikely(ret < 0)) { 3487 netdev_err(priv->dev, 3488 "%s: alloc wol MSI %d (error: %d)\n", 3489 __func__, priv->wol_irq, ret); 3490 irq_err = REQ_IRQ_ERR_WOL; 3491 goto irq_error; 3492 } 3493 } 3494 3495 /* Request the LPI IRQ in case of another line 3496 * is used for LPI 3497 */ 3498 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) { 3499 int_name = priv->int_name_lpi; 3500 sprintf(int_name, "%s:%s", dev->name, "lpi"); 3501 ret = request_irq(priv->lpi_irq, 3502 stmmac_mac_interrupt, 3503 0, int_name, dev); 3504 if (unlikely(ret < 0)) { 3505 netdev_err(priv->dev, 3506 "%s: alloc lpi MSI %d (error: %d)\n", 3507 __func__, priv->lpi_irq, ret); 3508 irq_err = REQ_IRQ_ERR_LPI; 3509 goto irq_error; 3510 } 3511 } 3512 3513 /* Request the Safety Feature Correctible Error line in 3514 * case of another line is used 3515 */ 3516 if (priv->sfty_ce_irq > 0 && priv->sfty_ce_irq != dev->irq) { 3517 int_name = priv->int_name_sfty_ce; 3518 sprintf(int_name, "%s:%s", dev->name, "safety-ce"); 3519 ret = request_irq(priv->sfty_ce_irq, 3520 stmmac_safety_interrupt, 3521 0, int_name, dev); 3522 if (unlikely(ret < 0)) { 3523 netdev_err(priv->dev, 3524 "%s: alloc sfty ce MSI %d (error: %d)\n", 3525 __func__, priv->sfty_ce_irq, ret); 3526 irq_err = REQ_IRQ_ERR_SFTY_CE; 3527 goto irq_error; 3528 } 3529 } 3530 3531 /* Request the Safety Feature Uncorrectible Error line in 3532 * case of another line is used 3533 */ 3534 if (priv->sfty_ue_irq > 0 && priv->sfty_ue_irq != dev->irq) { 3535 int_name = priv->int_name_sfty_ue; 3536 sprintf(int_name, "%s:%s", dev->name, "safety-ue"); 3537 ret = request_irq(priv->sfty_ue_irq, 3538 stmmac_safety_interrupt, 3539 0, int_name, dev); 3540 if (unlikely(ret < 0)) { 3541 netdev_err(priv->dev, 3542 "%s: alloc sfty ue MSI %d (error: %d)\n", 3543 __func__, priv->sfty_ue_irq, ret); 3544 irq_err = REQ_IRQ_ERR_SFTY_UE; 3545 goto irq_error; 3546 } 3547 } 3548 3549 /* Request Rx MSI irq */ 3550 for (i = 0; i < priv->plat->rx_queues_to_use; i++) { 3551 if (i >= MTL_MAX_RX_QUEUES) 3552 break; 3553 if (priv->rx_irq[i] == 0) 3554 continue; 3555 3556 int_name = priv->int_name_rx_irq[i]; 3557 sprintf(int_name, "%s:%s-%d", dev->name, "rx", i); 3558 ret = request_irq(priv->rx_irq[i], 3559 stmmac_msi_intr_rx, 3560 0, int_name, &priv->rx_queue[i]); 3561 if (unlikely(ret < 0)) { 3562 netdev_err(priv->dev, 3563 "%s: alloc rx-%d MSI %d (error: %d)\n", 3564 __func__, i, priv->rx_irq[i], ret); 3565 irq_err = REQ_IRQ_ERR_RX; 3566 irq_idx = i; 3567 goto irq_error; 3568 } 3569 cpumask_clear(&cpu_mask); 3570 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask); 3571 irq_set_affinity_hint(priv->rx_irq[i], &cpu_mask); 3572 } 3573 3574 /* Request Tx MSI irq */ 3575 for (i = 0; i < priv->plat->tx_queues_to_use; i++) { 3576 if (i >= MTL_MAX_TX_QUEUES) 3577 break; 3578 if (priv->tx_irq[i] == 0) 3579 continue; 3580 3581 int_name = priv->int_name_tx_irq[i]; 3582 sprintf(int_name, "%s:%s-%d", dev->name, "tx", i); 3583 ret = request_irq(priv->tx_irq[i], 3584 stmmac_msi_intr_tx, 3585 0, int_name, &priv->tx_queue[i]); 3586 if (unlikely(ret < 0)) { 3587 netdev_err(priv->dev, 3588 "%s: alloc tx-%d MSI %d (error: %d)\n", 3589 __func__, i, priv->tx_irq[i], ret); 3590 irq_err = REQ_IRQ_ERR_TX; 3591 irq_idx = i; 3592 goto irq_error; 3593 } 3594 cpumask_clear(&cpu_mask); 3595 cpumask_set_cpu(i % num_online_cpus(), &cpu_mask); 3596 irq_set_affinity_hint(priv->tx_irq[i], &cpu_mask); 3597 } 3598 3599 return 0; 3600 3601 irq_error: 3602 stmmac_free_irq(dev, irq_err, irq_idx); 3603 return ret; 3604 } 3605 3606 static int stmmac_request_irq_single(struct net_device *dev) 3607 { 3608 struct stmmac_priv *priv = netdev_priv(dev); 3609 enum request_irq_err irq_err; 3610 int ret; 3611 3612 ret = request_irq(dev->irq, stmmac_interrupt, 3613 IRQF_SHARED, dev->name, dev); 3614 if (unlikely(ret < 0)) { 3615 netdev_err(priv->dev, 3616 "%s: ERROR: allocating the IRQ %d (error: %d)\n", 3617 __func__, dev->irq, ret); 3618 irq_err = REQ_IRQ_ERR_MAC; 3619 goto irq_error; 3620 } 3621 3622 /* Request the Wake IRQ in case of another line 3623 * is used for WoL 3624 */ 3625 if (priv->wol_irq > 0 && priv->wol_irq != dev->irq) { 3626 ret = request_irq(priv->wol_irq, stmmac_interrupt, 3627 IRQF_SHARED, dev->name, dev); 3628 if (unlikely(ret < 0)) { 3629 netdev_err(priv->dev, 3630 "%s: ERROR: allocating the WoL IRQ %d (%d)\n", 3631 __func__, priv->wol_irq, ret); 3632 irq_err = REQ_IRQ_ERR_WOL; 3633 goto irq_error; 3634 } 3635 } 3636 3637 /* Request the IRQ lines */ 3638 if (priv->lpi_irq > 0 && priv->lpi_irq != dev->irq) { 3639 ret = request_irq(priv->lpi_irq, stmmac_interrupt, 3640 IRQF_SHARED, dev->name, dev); 3641 if (unlikely(ret < 0)) { 3642 netdev_err(priv->dev, 3643 "%s: ERROR: allocating the LPI IRQ %d (%d)\n", 3644 __func__, priv->lpi_irq, ret); 3645 irq_err = REQ_IRQ_ERR_LPI; 3646 goto irq_error; 3647 } 3648 } 3649 3650 return 0; 3651 3652 irq_error: 3653 stmmac_free_irq(dev, irq_err, 0); 3654 return ret; 3655 } 3656 3657 static int stmmac_request_irq(struct net_device *dev) 3658 { 3659 struct stmmac_priv *priv = netdev_priv(dev); 3660 int ret; 3661 3662 /* Request the IRQ lines */ 3663 if (priv->plat->multi_msi_en) 3664 ret = stmmac_request_irq_multi_msi(dev); 3665 else 3666 ret = stmmac_request_irq_single(dev); 3667 3668 return ret; 3669 } 3670 3671 /** 3672 * stmmac_open - open entry point of the driver 3673 * @dev : pointer to the device structure. 3674 * Description: 3675 * This function is the open entry point of the driver. 3676 * Return value: 3677 * 0 on success and an appropriate (-)ve integer as defined in errno.h 3678 * file on failure. 3679 */ 3680 int stmmac_open(struct net_device *dev) 3681 { 3682 struct stmmac_priv *priv = netdev_priv(dev); 3683 int mode = priv->plat->phy_interface; 3684 int bfsize = 0; 3685 u32 chan; 3686 int ret; 3687 3688 ret = pm_runtime_get_sync(priv->device); 3689 if (ret < 0) { 3690 pm_runtime_put_noidle(priv->device); 3691 return ret; 3692 } 3693 3694 if (priv->hw->pcs != STMMAC_PCS_TBI && 3695 priv->hw->pcs != STMMAC_PCS_RTBI && 3696 (!priv->hw->xpcs || 3697 xpcs_get_an_mode(priv->hw->xpcs, mode) != DW_AN_C73)) { 3698 ret = stmmac_init_phy(dev); 3699 if (ret) { 3700 netdev_err(priv->dev, 3701 "%s: Cannot attach to PHY (error: %d)\n", 3702 __func__, ret); 3703 goto init_phy_error; 3704 } 3705 } 3706 3707 /* Extra statistics */ 3708 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats)); 3709 priv->xstats.threshold = tc; 3710 3711 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu); 3712 if (bfsize < 0) 3713 bfsize = 0; 3714 3715 if (bfsize < BUF_SIZE_16KiB) 3716 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz); 3717 3718 priv->dma_buf_sz = bfsize; 3719 buf_sz = bfsize; 3720 3721 priv->rx_copybreak = STMMAC_RX_COPYBREAK; 3722 3723 if (!priv->dma_tx_size) 3724 priv->dma_tx_size = DMA_DEFAULT_TX_SIZE; 3725 if (!priv->dma_rx_size) 3726 priv->dma_rx_size = DMA_DEFAULT_RX_SIZE; 3727 3728 /* Earlier check for TBS */ 3729 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) { 3730 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 3731 int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en; 3732 3733 /* Setup per-TXQ tbs flag before TX descriptor alloc */ 3734 tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0; 3735 } 3736 3737 ret = alloc_dma_desc_resources(priv); 3738 if (ret < 0) { 3739 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n", 3740 __func__); 3741 goto dma_desc_error; 3742 } 3743 3744 ret = init_dma_desc_rings(dev, GFP_KERNEL); 3745 if (ret < 0) { 3746 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n", 3747 __func__); 3748 goto init_error; 3749 } 3750 3751 ret = stmmac_hw_setup(dev, true); 3752 if (ret < 0) { 3753 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__); 3754 goto init_error; 3755 } 3756 3757 stmmac_init_coalesce(priv); 3758 3759 phylink_start(priv->phylink); 3760 /* We may have called phylink_speed_down before */ 3761 phylink_speed_up(priv->phylink); 3762 3763 ret = stmmac_request_irq(dev); 3764 if (ret) 3765 goto irq_error; 3766 3767 stmmac_enable_all_queues(priv); 3768 netif_tx_start_all_queues(priv->dev); 3769 3770 return 0; 3771 3772 irq_error: 3773 phylink_stop(priv->phylink); 3774 3775 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) 3776 hrtimer_cancel(&priv->tx_queue[chan].txtimer); 3777 3778 stmmac_hw_teardown(dev); 3779 init_error: 3780 free_dma_desc_resources(priv); 3781 dma_desc_error: 3782 phylink_disconnect_phy(priv->phylink); 3783 init_phy_error: 3784 pm_runtime_put(priv->device); 3785 return ret; 3786 } 3787 3788 static void stmmac_fpe_stop_wq(struct stmmac_priv *priv) 3789 { 3790 set_bit(__FPE_REMOVING, &priv->fpe_task_state); 3791 3792 if (priv->fpe_wq) 3793 destroy_workqueue(priv->fpe_wq); 3794 3795 netdev_info(priv->dev, "FPE workqueue stop"); 3796 } 3797 3798 /** 3799 * stmmac_release - close entry point of the driver 3800 * @dev : device pointer. 3801 * Description: 3802 * This is the stop entry point of the driver. 3803 */ 3804 int stmmac_release(struct net_device *dev) 3805 { 3806 struct stmmac_priv *priv = netdev_priv(dev); 3807 u32 chan; 3808 3809 netif_tx_disable(dev); 3810 3811 if (device_may_wakeup(priv->device)) 3812 phylink_speed_down(priv->phylink, false); 3813 /* Stop and disconnect the PHY */ 3814 phylink_stop(priv->phylink); 3815 phylink_disconnect_phy(priv->phylink); 3816 3817 stmmac_disable_all_queues(priv); 3818 3819 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) 3820 hrtimer_cancel(&priv->tx_queue[chan].txtimer); 3821 3822 /* Free the IRQ lines */ 3823 stmmac_free_irq(dev, REQ_IRQ_ERR_ALL, 0); 3824 3825 if (priv->eee_enabled) { 3826 priv->tx_path_in_lpi_mode = false; 3827 del_timer_sync(&priv->eee_ctrl_timer); 3828 } 3829 3830 /* Stop TX/RX DMA and clear the descriptors */ 3831 stmmac_stop_all_dma(priv); 3832 3833 /* Release and free the Rx/Tx resources */ 3834 free_dma_desc_resources(priv); 3835 3836 /* Disable the MAC Rx/Tx */ 3837 stmmac_mac_set(priv, priv->ioaddr, false); 3838 3839 netif_carrier_off(dev); 3840 3841 stmmac_release_ptp(priv); 3842 3843 pm_runtime_put(priv->device); 3844 3845 if (priv->dma_cap.fpesel) 3846 stmmac_fpe_stop_wq(priv); 3847 3848 return 0; 3849 } 3850 3851 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb, 3852 struct stmmac_tx_queue *tx_q) 3853 { 3854 u16 tag = 0x0, inner_tag = 0x0; 3855 u32 inner_type = 0x0; 3856 struct dma_desc *p; 3857 3858 if (!priv->dma_cap.vlins) 3859 return false; 3860 if (!skb_vlan_tag_present(skb)) 3861 return false; 3862 if (skb->vlan_proto == htons(ETH_P_8021AD)) { 3863 inner_tag = skb_vlan_tag_get(skb); 3864 inner_type = STMMAC_VLAN_INSERT; 3865 } 3866 3867 tag = skb_vlan_tag_get(skb); 3868 3869 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3870 p = &tx_q->dma_entx[tx_q->cur_tx].basic; 3871 else 3872 p = &tx_q->dma_tx[tx_q->cur_tx]; 3873 3874 if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type)) 3875 return false; 3876 3877 stmmac_set_tx_owner(priv, p); 3878 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size); 3879 return true; 3880 } 3881 3882 /** 3883 * stmmac_tso_allocator - close entry point of the driver 3884 * @priv: driver private structure 3885 * @des: buffer start address 3886 * @total_len: total length to fill in descriptors 3887 * @last_segment: condition for the last descriptor 3888 * @queue: TX queue index 3889 * Description: 3890 * This function fills descriptor and request new descriptors according to 3891 * buffer length to fill 3892 */ 3893 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des, 3894 int total_len, bool last_segment, u32 queue) 3895 { 3896 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 3897 struct dma_desc *desc; 3898 u32 buff_size; 3899 int tmp_len; 3900 3901 tmp_len = total_len; 3902 3903 while (tmp_len > 0) { 3904 dma_addr_t curr_addr; 3905 3906 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, 3907 priv->dma_tx_size); 3908 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]); 3909 3910 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3911 desc = &tx_q->dma_entx[tx_q->cur_tx].basic; 3912 else 3913 desc = &tx_q->dma_tx[tx_q->cur_tx]; 3914 3915 curr_addr = des + (total_len - tmp_len); 3916 if (priv->dma_cap.addr64 <= 32) 3917 desc->des0 = cpu_to_le32(curr_addr); 3918 else 3919 stmmac_set_desc_addr(priv, desc, curr_addr); 3920 3921 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ? 3922 TSO_MAX_BUFF_SIZE : tmp_len; 3923 3924 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size, 3925 0, 1, 3926 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE), 3927 0, 0); 3928 3929 tmp_len -= TSO_MAX_BUFF_SIZE; 3930 } 3931 } 3932 3933 static void stmmac_flush_tx_descriptors(struct stmmac_priv *priv, int queue) 3934 { 3935 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 3936 int desc_size; 3937 3938 if (likely(priv->extend_desc)) 3939 desc_size = sizeof(struct dma_extended_desc); 3940 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 3941 desc_size = sizeof(struct dma_edesc); 3942 else 3943 desc_size = sizeof(struct dma_desc); 3944 3945 /* The own bit must be the latest setting done when prepare the 3946 * descriptor and then barrier is needed to make sure that 3947 * all is coherent before granting the DMA engine. 3948 */ 3949 wmb(); 3950 3951 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size); 3952 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue); 3953 } 3954 3955 /** 3956 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO) 3957 * @skb : the socket buffer 3958 * @dev : device pointer 3959 * Description: this is the transmit function that is called on TSO frames 3960 * (support available on GMAC4 and newer chips). 3961 * Diagram below show the ring programming in case of TSO frames: 3962 * 3963 * First Descriptor 3964 * -------- 3965 * | DES0 |---> buffer1 = L2/L3/L4 header 3966 * | DES1 |---> TCP Payload (can continue on next descr...) 3967 * | DES2 |---> buffer 1 and 2 len 3968 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0] 3969 * -------- 3970 * | 3971 * ... 3972 * | 3973 * -------- 3974 * | DES0 | --| Split TCP Payload on Buffers 1 and 2 3975 * | DES1 | --| 3976 * | DES2 | --> buffer 1 and 2 len 3977 * | DES3 | 3978 * -------- 3979 * 3980 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field. 3981 */ 3982 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev) 3983 { 3984 struct dma_desc *desc, *first, *mss_desc = NULL; 3985 struct stmmac_priv *priv = netdev_priv(dev); 3986 int nfrags = skb_shinfo(skb)->nr_frags; 3987 u32 queue = skb_get_queue_mapping(skb); 3988 unsigned int first_entry, tx_packets; 3989 int tmp_pay_len = 0, first_tx; 3990 struct stmmac_tx_queue *tx_q; 3991 bool has_vlan, set_ic; 3992 u8 proto_hdr_len, hdr; 3993 u32 pay_len, mss; 3994 dma_addr_t des; 3995 int i; 3996 3997 tx_q = &priv->tx_queue[queue]; 3998 first_tx = tx_q->cur_tx; 3999 4000 /* Compute header lengths */ 4001 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 4002 proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr); 4003 hdr = sizeof(struct udphdr); 4004 } else { 4005 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 4006 hdr = tcp_hdrlen(skb); 4007 } 4008 4009 /* Desc availability based on threshold should be enough safe */ 4010 if (unlikely(stmmac_tx_avail(priv, queue) < 4011 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) { 4012 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) { 4013 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, 4014 queue)); 4015 /* This is a hard error, log it. */ 4016 netdev_err(priv->dev, 4017 "%s: Tx Ring full when queue awake\n", 4018 __func__); 4019 } 4020 return NETDEV_TX_BUSY; 4021 } 4022 4023 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */ 4024 4025 mss = skb_shinfo(skb)->gso_size; 4026 4027 /* set new MSS value if needed */ 4028 if (mss != tx_q->mss) { 4029 if (tx_q->tbs & STMMAC_TBS_AVAIL) 4030 mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic; 4031 else 4032 mss_desc = &tx_q->dma_tx[tx_q->cur_tx]; 4033 4034 stmmac_set_mss(priv, mss_desc, mss); 4035 tx_q->mss = mss; 4036 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, 4037 priv->dma_tx_size); 4038 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]); 4039 } 4040 4041 if (netif_msg_tx_queued(priv)) { 4042 pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n", 4043 __func__, hdr, proto_hdr_len, pay_len, mss); 4044 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len, 4045 skb->data_len); 4046 } 4047 4048 /* Check if VLAN can be inserted by HW */ 4049 has_vlan = stmmac_vlan_insert(priv, skb, tx_q); 4050 4051 first_entry = tx_q->cur_tx; 4052 WARN_ON(tx_q->tx_skbuff[first_entry]); 4053 4054 if (tx_q->tbs & STMMAC_TBS_AVAIL) 4055 desc = &tx_q->dma_entx[first_entry].basic; 4056 else 4057 desc = &tx_q->dma_tx[first_entry]; 4058 first = desc; 4059 4060 if (has_vlan) 4061 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT); 4062 4063 /* first descriptor: fill Headers on Buf1 */ 4064 des = dma_map_single(priv->device, skb->data, skb_headlen(skb), 4065 DMA_TO_DEVICE); 4066 if (dma_mapping_error(priv->device, des)) 4067 goto dma_map_err; 4068 4069 tx_q->tx_skbuff_dma[first_entry].buf = des; 4070 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb); 4071 tx_q->tx_skbuff_dma[first_entry].map_as_page = false; 4072 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB; 4073 4074 if (priv->dma_cap.addr64 <= 32) { 4075 first->des0 = cpu_to_le32(des); 4076 4077 /* Fill start of payload in buff2 of first descriptor */ 4078 if (pay_len) 4079 first->des1 = cpu_to_le32(des + proto_hdr_len); 4080 4081 /* If needed take extra descriptors to fill the remaining payload */ 4082 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE; 4083 } else { 4084 stmmac_set_desc_addr(priv, first, des); 4085 tmp_pay_len = pay_len; 4086 des += proto_hdr_len; 4087 pay_len = 0; 4088 } 4089 4090 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue); 4091 4092 /* Prepare fragments */ 4093 for (i = 0; i < nfrags; i++) { 4094 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 4095 4096 des = skb_frag_dma_map(priv->device, frag, 0, 4097 skb_frag_size(frag), 4098 DMA_TO_DEVICE); 4099 if (dma_mapping_error(priv->device, des)) 4100 goto dma_map_err; 4101 4102 stmmac_tso_allocator(priv, des, skb_frag_size(frag), 4103 (i == nfrags - 1), queue); 4104 4105 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des; 4106 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag); 4107 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true; 4108 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB; 4109 } 4110 4111 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true; 4112 4113 /* Only the last descriptor gets to point to the skb. */ 4114 tx_q->tx_skbuff[tx_q->cur_tx] = skb; 4115 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB; 4116 4117 /* Manage tx mitigation */ 4118 tx_packets = (tx_q->cur_tx + 1) - first_tx; 4119 tx_q->tx_count_frames += tx_packets; 4120 4121 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en) 4122 set_ic = true; 4123 else if (!priv->tx_coal_frames[queue]) 4124 set_ic = false; 4125 else if (tx_packets > priv->tx_coal_frames[queue]) 4126 set_ic = true; 4127 else if ((tx_q->tx_count_frames % 4128 priv->tx_coal_frames[queue]) < tx_packets) 4129 set_ic = true; 4130 else 4131 set_ic = false; 4132 4133 if (set_ic) { 4134 if (tx_q->tbs & STMMAC_TBS_AVAIL) 4135 desc = &tx_q->dma_entx[tx_q->cur_tx].basic; 4136 else 4137 desc = &tx_q->dma_tx[tx_q->cur_tx]; 4138 4139 tx_q->tx_count_frames = 0; 4140 stmmac_set_tx_ic(priv, desc); 4141 priv->xstats.tx_set_ic_bit++; 4142 } 4143 4144 /* We've used all descriptors we need for this skb, however, 4145 * advance cur_tx so that it references a fresh descriptor. 4146 * ndo_start_xmit will fill this descriptor the next time it's 4147 * called and stmmac_tx_clean may clean up to this descriptor. 4148 */ 4149 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size); 4150 4151 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) { 4152 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n", 4153 __func__); 4154 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue)); 4155 } 4156 4157 dev->stats.tx_bytes += skb->len; 4158 priv->xstats.tx_tso_frames++; 4159 priv->xstats.tx_tso_nfrags += nfrags; 4160 4161 if (priv->sarc_type) 4162 stmmac_set_desc_sarc(priv, first, priv->sarc_type); 4163 4164 skb_tx_timestamp(skb); 4165 4166 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 4167 priv->hwts_tx_en)) { 4168 /* declare that device is doing timestamping */ 4169 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 4170 stmmac_enable_tx_timestamp(priv, first); 4171 } 4172 4173 /* Complete the first descriptor before granting the DMA */ 4174 stmmac_prepare_tso_tx_desc(priv, first, 1, 4175 proto_hdr_len, 4176 pay_len, 4177 1, tx_q->tx_skbuff_dma[first_entry].last_segment, 4178 hdr / 4, (skb->len - proto_hdr_len)); 4179 4180 /* If context desc is used to change MSS */ 4181 if (mss_desc) { 4182 /* Make sure that first descriptor has been completely 4183 * written, including its own bit. This is because MSS is 4184 * actually before first descriptor, so we need to make 4185 * sure that MSS's own bit is the last thing written. 4186 */ 4187 dma_wmb(); 4188 stmmac_set_tx_owner(priv, mss_desc); 4189 } 4190 4191 if (netif_msg_pktdata(priv)) { 4192 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n", 4193 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry, 4194 tx_q->cur_tx, first, nfrags); 4195 pr_info(">>> frame to be transmitted: "); 4196 print_pkt(skb->data, skb_headlen(skb)); 4197 } 4198 4199 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len); 4200 4201 stmmac_flush_tx_descriptors(priv, queue); 4202 stmmac_tx_timer_arm(priv, queue); 4203 4204 return NETDEV_TX_OK; 4205 4206 dma_map_err: 4207 dev_err(priv->device, "Tx dma map failed\n"); 4208 dev_kfree_skb(skb); 4209 priv->dev->stats.tx_dropped++; 4210 return NETDEV_TX_OK; 4211 } 4212 4213 /** 4214 * stmmac_xmit - Tx entry point of the driver 4215 * @skb : the socket buffer 4216 * @dev : device pointer 4217 * Description : this is the tx entry point of the driver. 4218 * It programs the chain or the ring and supports oversized frames 4219 * and SG feature. 4220 */ 4221 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev) 4222 { 4223 unsigned int first_entry, tx_packets, enh_desc; 4224 struct stmmac_priv *priv = netdev_priv(dev); 4225 unsigned int nopaged_len = skb_headlen(skb); 4226 int i, csum_insertion = 0, is_jumbo = 0; 4227 u32 queue = skb_get_queue_mapping(skb); 4228 int nfrags = skb_shinfo(skb)->nr_frags; 4229 int gso = skb_shinfo(skb)->gso_type; 4230 struct dma_edesc *tbs_desc = NULL; 4231 struct dma_desc *desc, *first; 4232 struct stmmac_tx_queue *tx_q; 4233 bool has_vlan, set_ic; 4234 int entry, first_tx; 4235 dma_addr_t des; 4236 4237 tx_q = &priv->tx_queue[queue]; 4238 first_tx = tx_q->cur_tx; 4239 4240 if (priv->tx_path_in_lpi_mode && priv->eee_sw_timer_en) 4241 stmmac_disable_eee_mode(priv); 4242 4243 /* Manage oversized TCP frames for GMAC4 device */ 4244 if (skb_is_gso(skb) && priv->tso) { 4245 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) 4246 return stmmac_tso_xmit(skb, dev); 4247 if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4)) 4248 return stmmac_tso_xmit(skb, dev); 4249 } 4250 4251 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) { 4252 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) { 4253 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, 4254 queue)); 4255 /* This is a hard error, log it. */ 4256 netdev_err(priv->dev, 4257 "%s: Tx Ring full when queue awake\n", 4258 __func__); 4259 } 4260 return NETDEV_TX_BUSY; 4261 } 4262 4263 /* Check if VLAN can be inserted by HW */ 4264 has_vlan = stmmac_vlan_insert(priv, skb, tx_q); 4265 4266 entry = tx_q->cur_tx; 4267 first_entry = entry; 4268 WARN_ON(tx_q->tx_skbuff[first_entry]); 4269 4270 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL); 4271 4272 if (likely(priv->extend_desc)) 4273 desc = (struct dma_desc *)(tx_q->dma_etx + entry); 4274 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 4275 desc = &tx_q->dma_entx[entry].basic; 4276 else 4277 desc = tx_q->dma_tx + entry; 4278 4279 first = desc; 4280 4281 if (has_vlan) 4282 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT); 4283 4284 enh_desc = priv->plat->enh_desc; 4285 /* To program the descriptors according to the size of the frame */ 4286 if (enh_desc) 4287 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc); 4288 4289 if (unlikely(is_jumbo)) { 4290 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion); 4291 if (unlikely(entry < 0) && (entry != -EINVAL)) 4292 goto dma_map_err; 4293 } 4294 4295 for (i = 0; i < nfrags; i++) { 4296 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 4297 int len = skb_frag_size(frag); 4298 bool last_segment = (i == (nfrags - 1)); 4299 4300 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size); 4301 WARN_ON(tx_q->tx_skbuff[entry]); 4302 4303 if (likely(priv->extend_desc)) 4304 desc = (struct dma_desc *)(tx_q->dma_etx + entry); 4305 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 4306 desc = &tx_q->dma_entx[entry].basic; 4307 else 4308 desc = tx_q->dma_tx + entry; 4309 4310 des = skb_frag_dma_map(priv->device, frag, 0, len, 4311 DMA_TO_DEVICE); 4312 if (dma_mapping_error(priv->device, des)) 4313 goto dma_map_err; /* should reuse desc w/o issues */ 4314 4315 tx_q->tx_skbuff_dma[entry].buf = des; 4316 4317 stmmac_set_desc_addr(priv, desc, des); 4318 4319 tx_q->tx_skbuff_dma[entry].map_as_page = true; 4320 tx_q->tx_skbuff_dma[entry].len = len; 4321 tx_q->tx_skbuff_dma[entry].last_segment = last_segment; 4322 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB; 4323 4324 /* Prepare the descriptor and set the own bit too */ 4325 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion, 4326 priv->mode, 1, last_segment, skb->len); 4327 } 4328 4329 /* Only the last descriptor gets to point to the skb. */ 4330 tx_q->tx_skbuff[entry] = skb; 4331 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_SKB; 4332 4333 /* According to the coalesce parameter the IC bit for the latest 4334 * segment is reset and the timer re-started to clean the tx status. 4335 * This approach takes care about the fragments: desc is the first 4336 * element in case of no SG. 4337 */ 4338 tx_packets = (entry + 1) - first_tx; 4339 tx_q->tx_count_frames += tx_packets; 4340 4341 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en) 4342 set_ic = true; 4343 else if (!priv->tx_coal_frames[queue]) 4344 set_ic = false; 4345 else if (tx_packets > priv->tx_coal_frames[queue]) 4346 set_ic = true; 4347 else if ((tx_q->tx_count_frames % 4348 priv->tx_coal_frames[queue]) < tx_packets) 4349 set_ic = true; 4350 else 4351 set_ic = false; 4352 4353 if (set_ic) { 4354 if (likely(priv->extend_desc)) 4355 desc = &tx_q->dma_etx[entry].basic; 4356 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 4357 desc = &tx_q->dma_entx[entry].basic; 4358 else 4359 desc = &tx_q->dma_tx[entry]; 4360 4361 tx_q->tx_count_frames = 0; 4362 stmmac_set_tx_ic(priv, desc); 4363 priv->xstats.tx_set_ic_bit++; 4364 } 4365 4366 /* We've used all descriptors we need for this skb, however, 4367 * advance cur_tx so that it references a fresh descriptor. 4368 * ndo_start_xmit will fill this descriptor the next time it's 4369 * called and stmmac_tx_clean may clean up to this descriptor. 4370 */ 4371 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size); 4372 tx_q->cur_tx = entry; 4373 4374 if (netif_msg_pktdata(priv)) { 4375 netdev_dbg(priv->dev, 4376 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d", 4377 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry, 4378 entry, first, nfrags); 4379 4380 netdev_dbg(priv->dev, ">>> frame to be transmitted: "); 4381 print_pkt(skb->data, skb->len); 4382 } 4383 4384 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) { 4385 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n", 4386 __func__); 4387 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue)); 4388 } 4389 4390 dev->stats.tx_bytes += skb->len; 4391 4392 if (priv->sarc_type) 4393 stmmac_set_desc_sarc(priv, first, priv->sarc_type); 4394 4395 skb_tx_timestamp(skb); 4396 4397 /* Ready to fill the first descriptor and set the OWN bit w/o any 4398 * problems because all the descriptors are actually ready to be 4399 * passed to the DMA engine. 4400 */ 4401 if (likely(!is_jumbo)) { 4402 bool last_segment = (nfrags == 0); 4403 4404 des = dma_map_single(priv->device, skb->data, 4405 nopaged_len, DMA_TO_DEVICE); 4406 if (dma_mapping_error(priv->device, des)) 4407 goto dma_map_err; 4408 4409 tx_q->tx_skbuff_dma[first_entry].buf = des; 4410 tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB; 4411 tx_q->tx_skbuff_dma[first_entry].map_as_page = false; 4412 4413 stmmac_set_desc_addr(priv, first, des); 4414 4415 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len; 4416 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment; 4417 4418 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 4419 priv->hwts_tx_en)) { 4420 /* declare that device is doing timestamping */ 4421 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 4422 stmmac_enable_tx_timestamp(priv, first); 4423 } 4424 4425 /* Prepare the first descriptor setting the OWN bit too */ 4426 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len, 4427 csum_insertion, priv->mode, 0, last_segment, 4428 skb->len); 4429 } 4430 4431 if (tx_q->tbs & STMMAC_TBS_EN) { 4432 struct timespec64 ts = ns_to_timespec64(skb->tstamp); 4433 4434 tbs_desc = &tx_q->dma_entx[first_entry]; 4435 stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec); 4436 } 4437 4438 stmmac_set_tx_owner(priv, first); 4439 4440 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len); 4441 4442 stmmac_enable_dma_transmission(priv, priv->ioaddr); 4443 4444 stmmac_flush_tx_descriptors(priv, queue); 4445 stmmac_tx_timer_arm(priv, queue); 4446 4447 return NETDEV_TX_OK; 4448 4449 dma_map_err: 4450 netdev_err(priv->dev, "Tx DMA map failed\n"); 4451 dev_kfree_skb(skb); 4452 priv->dev->stats.tx_dropped++; 4453 return NETDEV_TX_OK; 4454 } 4455 4456 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb) 4457 { 4458 struct vlan_ethhdr *veth; 4459 __be16 vlan_proto; 4460 u16 vlanid; 4461 4462 veth = (struct vlan_ethhdr *)skb->data; 4463 vlan_proto = veth->h_vlan_proto; 4464 4465 if ((vlan_proto == htons(ETH_P_8021Q) && 4466 dev->features & NETIF_F_HW_VLAN_CTAG_RX) || 4467 (vlan_proto == htons(ETH_P_8021AD) && 4468 dev->features & NETIF_F_HW_VLAN_STAG_RX)) { 4469 /* pop the vlan tag */ 4470 vlanid = ntohs(veth->h_vlan_TCI); 4471 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2); 4472 skb_pull(skb, VLAN_HLEN); 4473 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid); 4474 } 4475 } 4476 4477 /** 4478 * stmmac_rx_refill - refill used skb preallocated buffers 4479 * @priv: driver private structure 4480 * @queue: RX queue index 4481 * Description : this is to reallocate the skb for the reception process 4482 * that is based on zero-copy. 4483 */ 4484 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue) 4485 { 4486 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 4487 int dirty = stmmac_rx_dirty(priv, queue); 4488 unsigned int entry = rx_q->dirty_rx; 4489 gfp_t gfp = (GFP_ATOMIC | __GFP_NOWARN); 4490 4491 if (priv->dma_cap.addr64 <= 32) 4492 gfp |= GFP_DMA32; 4493 4494 while (dirty-- > 0) { 4495 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry]; 4496 struct dma_desc *p; 4497 bool use_rx_wd; 4498 4499 if (priv->extend_desc) 4500 p = (struct dma_desc *)(rx_q->dma_erx + entry); 4501 else 4502 p = rx_q->dma_rx + entry; 4503 4504 if (!buf->page) { 4505 buf->page = page_pool_alloc_pages(rx_q->page_pool, gfp); 4506 if (!buf->page) 4507 break; 4508 } 4509 4510 if (priv->sph && !buf->sec_page) { 4511 buf->sec_page = page_pool_alloc_pages(rx_q->page_pool, gfp); 4512 if (!buf->sec_page) 4513 break; 4514 4515 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page); 4516 } 4517 4518 buf->addr = page_pool_get_dma_addr(buf->page) + buf->page_offset; 4519 4520 stmmac_set_desc_addr(priv, p, buf->addr); 4521 if (priv->sph) 4522 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true); 4523 else 4524 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false); 4525 stmmac_refill_desc3(priv, rx_q, p); 4526 4527 rx_q->rx_count_frames++; 4528 rx_q->rx_count_frames += priv->rx_coal_frames[queue]; 4529 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue]) 4530 rx_q->rx_count_frames = 0; 4531 4532 use_rx_wd = !priv->rx_coal_frames[queue]; 4533 use_rx_wd |= rx_q->rx_count_frames > 0; 4534 if (!priv->use_riwt) 4535 use_rx_wd = false; 4536 4537 dma_wmb(); 4538 stmmac_set_rx_owner(priv, p, use_rx_wd); 4539 4540 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size); 4541 } 4542 rx_q->dirty_rx = entry; 4543 rx_q->rx_tail_addr = rx_q->dma_rx_phy + 4544 (rx_q->dirty_rx * sizeof(struct dma_desc)); 4545 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue); 4546 } 4547 4548 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv, 4549 struct dma_desc *p, 4550 int status, unsigned int len) 4551 { 4552 unsigned int plen = 0, hlen = 0; 4553 int coe = priv->hw->rx_csum; 4554 4555 /* Not first descriptor, buffer is always zero */ 4556 if (priv->sph && len) 4557 return 0; 4558 4559 /* First descriptor, get split header length */ 4560 stmmac_get_rx_header_len(priv, p, &hlen); 4561 if (priv->sph && hlen) { 4562 priv->xstats.rx_split_hdr_pkt_n++; 4563 return hlen; 4564 } 4565 4566 /* First descriptor, not last descriptor and not split header */ 4567 if (status & rx_not_ls) 4568 return priv->dma_buf_sz; 4569 4570 plen = stmmac_get_rx_frame_len(priv, p, coe); 4571 4572 /* First descriptor and last descriptor and not split header */ 4573 return min_t(unsigned int, priv->dma_buf_sz, plen); 4574 } 4575 4576 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv, 4577 struct dma_desc *p, 4578 int status, unsigned int len) 4579 { 4580 int coe = priv->hw->rx_csum; 4581 unsigned int plen = 0; 4582 4583 /* Not split header, buffer is not available */ 4584 if (!priv->sph) 4585 return 0; 4586 4587 /* Not last descriptor */ 4588 if (status & rx_not_ls) 4589 return priv->dma_buf_sz; 4590 4591 plen = stmmac_get_rx_frame_len(priv, p, coe); 4592 4593 /* Last descriptor */ 4594 return plen - len; 4595 } 4596 4597 static int stmmac_xdp_xmit_xdpf(struct stmmac_priv *priv, int queue, 4598 struct xdp_frame *xdpf, bool dma_map) 4599 { 4600 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 4601 unsigned int entry = tx_q->cur_tx; 4602 struct dma_desc *tx_desc; 4603 dma_addr_t dma_addr; 4604 bool set_ic; 4605 4606 if (stmmac_tx_avail(priv, queue) < STMMAC_TX_THRESH(priv)) 4607 return STMMAC_XDP_CONSUMED; 4608 4609 if (likely(priv->extend_desc)) 4610 tx_desc = (struct dma_desc *)(tx_q->dma_etx + entry); 4611 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 4612 tx_desc = &tx_q->dma_entx[entry].basic; 4613 else 4614 tx_desc = tx_q->dma_tx + entry; 4615 4616 if (dma_map) { 4617 dma_addr = dma_map_single(priv->device, xdpf->data, 4618 xdpf->len, DMA_TO_DEVICE); 4619 if (dma_mapping_error(priv->device, dma_addr)) 4620 return STMMAC_XDP_CONSUMED; 4621 4622 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_NDO; 4623 } else { 4624 struct page *page = virt_to_page(xdpf->data); 4625 4626 dma_addr = page_pool_get_dma_addr(page) + sizeof(*xdpf) + 4627 xdpf->headroom; 4628 dma_sync_single_for_device(priv->device, dma_addr, 4629 xdpf->len, DMA_BIDIRECTIONAL); 4630 4631 tx_q->tx_skbuff_dma[entry].buf_type = STMMAC_TXBUF_T_XDP_TX; 4632 } 4633 4634 tx_q->tx_skbuff_dma[entry].buf = dma_addr; 4635 tx_q->tx_skbuff_dma[entry].map_as_page = false; 4636 tx_q->tx_skbuff_dma[entry].len = xdpf->len; 4637 tx_q->tx_skbuff_dma[entry].last_segment = true; 4638 tx_q->tx_skbuff_dma[entry].is_jumbo = false; 4639 4640 tx_q->xdpf[entry] = xdpf; 4641 4642 stmmac_set_desc_addr(priv, tx_desc, dma_addr); 4643 4644 stmmac_prepare_tx_desc(priv, tx_desc, 1, xdpf->len, 4645 true, priv->mode, true, true, 4646 xdpf->len); 4647 4648 tx_q->tx_count_frames++; 4649 4650 if (tx_q->tx_count_frames % priv->tx_coal_frames[queue] == 0) 4651 set_ic = true; 4652 else 4653 set_ic = false; 4654 4655 if (set_ic) { 4656 tx_q->tx_count_frames = 0; 4657 stmmac_set_tx_ic(priv, tx_desc); 4658 priv->xstats.tx_set_ic_bit++; 4659 } 4660 4661 stmmac_enable_dma_transmission(priv, priv->ioaddr); 4662 4663 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size); 4664 tx_q->cur_tx = entry; 4665 4666 return STMMAC_XDP_TX; 4667 } 4668 4669 static int stmmac_xdp_get_tx_queue(struct stmmac_priv *priv, 4670 int cpu) 4671 { 4672 int index = cpu; 4673 4674 if (unlikely(index < 0)) 4675 index = 0; 4676 4677 while (index >= priv->plat->tx_queues_to_use) 4678 index -= priv->plat->tx_queues_to_use; 4679 4680 return index; 4681 } 4682 4683 static int stmmac_xdp_xmit_back(struct stmmac_priv *priv, 4684 struct xdp_buff *xdp) 4685 { 4686 struct xdp_frame *xdpf = xdp_convert_buff_to_frame(xdp); 4687 int cpu = smp_processor_id(); 4688 struct netdev_queue *nq; 4689 int queue; 4690 int res; 4691 4692 if (unlikely(!xdpf)) 4693 return STMMAC_XDP_CONSUMED; 4694 4695 queue = stmmac_xdp_get_tx_queue(priv, cpu); 4696 nq = netdev_get_tx_queue(priv->dev, queue); 4697 4698 __netif_tx_lock(nq, cpu); 4699 /* Avoids TX time-out as we are sharing with slow path */ 4700 nq->trans_start = jiffies; 4701 4702 res = stmmac_xdp_xmit_xdpf(priv, queue, xdpf, false); 4703 if (res == STMMAC_XDP_TX) 4704 stmmac_flush_tx_descriptors(priv, queue); 4705 4706 __netif_tx_unlock(nq); 4707 4708 return res; 4709 } 4710 4711 static int __stmmac_xdp_run_prog(struct stmmac_priv *priv, 4712 struct bpf_prog *prog, 4713 struct xdp_buff *xdp) 4714 { 4715 u32 act; 4716 int res; 4717 4718 act = bpf_prog_run_xdp(prog, xdp); 4719 switch (act) { 4720 case XDP_PASS: 4721 res = STMMAC_XDP_PASS; 4722 break; 4723 case XDP_TX: 4724 res = stmmac_xdp_xmit_back(priv, xdp); 4725 break; 4726 case XDP_REDIRECT: 4727 if (xdp_do_redirect(priv->dev, xdp, prog) < 0) 4728 res = STMMAC_XDP_CONSUMED; 4729 else 4730 res = STMMAC_XDP_REDIRECT; 4731 break; 4732 default: 4733 bpf_warn_invalid_xdp_action(act); 4734 fallthrough; 4735 case XDP_ABORTED: 4736 trace_xdp_exception(priv->dev, prog, act); 4737 fallthrough; 4738 case XDP_DROP: 4739 res = STMMAC_XDP_CONSUMED; 4740 break; 4741 } 4742 4743 return res; 4744 } 4745 4746 static struct sk_buff *stmmac_xdp_run_prog(struct stmmac_priv *priv, 4747 struct xdp_buff *xdp) 4748 { 4749 struct bpf_prog *prog; 4750 int res; 4751 4752 prog = READ_ONCE(priv->xdp_prog); 4753 if (!prog) { 4754 res = STMMAC_XDP_PASS; 4755 goto out; 4756 } 4757 4758 res = __stmmac_xdp_run_prog(priv, prog, xdp); 4759 out: 4760 return ERR_PTR(-res); 4761 } 4762 4763 static void stmmac_finalize_xdp_rx(struct stmmac_priv *priv, 4764 int xdp_status) 4765 { 4766 int cpu = smp_processor_id(); 4767 int queue; 4768 4769 queue = stmmac_xdp_get_tx_queue(priv, cpu); 4770 4771 if (xdp_status & STMMAC_XDP_TX) 4772 stmmac_tx_timer_arm(priv, queue); 4773 4774 if (xdp_status & STMMAC_XDP_REDIRECT) 4775 xdp_do_flush(); 4776 } 4777 4778 static struct sk_buff *stmmac_construct_skb_zc(struct stmmac_channel *ch, 4779 struct xdp_buff *xdp) 4780 { 4781 unsigned int metasize = xdp->data - xdp->data_meta; 4782 unsigned int datasize = xdp->data_end - xdp->data; 4783 struct sk_buff *skb; 4784 4785 skb = __napi_alloc_skb(&ch->rxtx_napi, 4786 xdp->data_end - xdp->data_hard_start, 4787 GFP_ATOMIC | __GFP_NOWARN); 4788 if (unlikely(!skb)) 4789 return NULL; 4790 4791 skb_reserve(skb, xdp->data - xdp->data_hard_start); 4792 memcpy(__skb_put(skb, datasize), xdp->data, datasize); 4793 if (metasize) 4794 skb_metadata_set(skb, metasize); 4795 4796 return skb; 4797 } 4798 4799 static void stmmac_dispatch_skb_zc(struct stmmac_priv *priv, u32 queue, 4800 struct dma_desc *p, struct dma_desc *np, 4801 struct xdp_buff *xdp) 4802 { 4803 struct stmmac_channel *ch = &priv->channel[queue]; 4804 unsigned int len = xdp->data_end - xdp->data; 4805 enum pkt_hash_types hash_type; 4806 int coe = priv->hw->rx_csum; 4807 struct sk_buff *skb; 4808 u32 hash; 4809 4810 skb = stmmac_construct_skb_zc(ch, xdp); 4811 if (!skb) { 4812 priv->dev->stats.rx_dropped++; 4813 return; 4814 } 4815 4816 stmmac_get_rx_hwtstamp(priv, p, np, skb); 4817 stmmac_rx_vlan(priv->dev, skb); 4818 skb->protocol = eth_type_trans(skb, priv->dev); 4819 4820 if (unlikely(!coe)) 4821 skb_checksum_none_assert(skb); 4822 else 4823 skb->ip_summed = CHECKSUM_UNNECESSARY; 4824 4825 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type)) 4826 skb_set_hash(skb, hash, hash_type); 4827 4828 skb_record_rx_queue(skb, queue); 4829 napi_gro_receive(&ch->rxtx_napi, skb); 4830 4831 priv->dev->stats.rx_packets++; 4832 priv->dev->stats.rx_bytes += len; 4833 } 4834 4835 static bool stmmac_rx_refill_zc(struct stmmac_priv *priv, u32 queue, u32 budget) 4836 { 4837 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 4838 unsigned int entry = rx_q->dirty_rx; 4839 struct dma_desc *rx_desc = NULL; 4840 bool ret = true; 4841 4842 budget = min(budget, stmmac_rx_dirty(priv, queue)); 4843 4844 while (budget-- > 0 && entry != rx_q->cur_rx) { 4845 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry]; 4846 dma_addr_t dma_addr; 4847 bool use_rx_wd; 4848 4849 if (!buf->xdp) { 4850 buf->xdp = xsk_buff_alloc(rx_q->xsk_pool); 4851 if (!buf->xdp) { 4852 ret = false; 4853 break; 4854 } 4855 } 4856 4857 if (priv->extend_desc) 4858 rx_desc = (struct dma_desc *)(rx_q->dma_erx + entry); 4859 else 4860 rx_desc = rx_q->dma_rx + entry; 4861 4862 dma_addr = xsk_buff_xdp_get_dma(buf->xdp); 4863 stmmac_set_desc_addr(priv, rx_desc, dma_addr); 4864 stmmac_set_desc_sec_addr(priv, rx_desc, 0, false); 4865 stmmac_refill_desc3(priv, rx_q, rx_desc); 4866 4867 rx_q->rx_count_frames++; 4868 rx_q->rx_count_frames += priv->rx_coal_frames[queue]; 4869 if (rx_q->rx_count_frames > priv->rx_coal_frames[queue]) 4870 rx_q->rx_count_frames = 0; 4871 4872 use_rx_wd = !priv->rx_coal_frames[queue]; 4873 use_rx_wd |= rx_q->rx_count_frames > 0; 4874 if (!priv->use_riwt) 4875 use_rx_wd = false; 4876 4877 dma_wmb(); 4878 stmmac_set_rx_owner(priv, rx_desc, use_rx_wd); 4879 4880 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size); 4881 } 4882 4883 if (rx_desc) { 4884 rx_q->dirty_rx = entry; 4885 rx_q->rx_tail_addr = rx_q->dma_rx_phy + 4886 (rx_q->dirty_rx * sizeof(struct dma_desc)); 4887 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue); 4888 } 4889 4890 return ret; 4891 } 4892 4893 static int stmmac_rx_zc(struct stmmac_priv *priv, int limit, u32 queue) 4894 { 4895 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 4896 unsigned int count = 0, error = 0, len = 0; 4897 int dirty = stmmac_rx_dirty(priv, queue); 4898 unsigned int next_entry = rx_q->cur_rx; 4899 unsigned int desc_size; 4900 struct bpf_prog *prog; 4901 bool failure = false; 4902 int xdp_status = 0; 4903 int status = 0; 4904 4905 if (netif_msg_rx_status(priv)) { 4906 void *rx_head; 4907 4908 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__); 4909 if (priv->extend_desc) { 4910 rx_head = (void *)rx_q->dma_erx; 4911 desc_size = sizeof(struct dma_extended_desc); 4912 } else { 4913 rx_head = (void *)rx_q->dma_rx; 4914 desc_size = sizeof(struct dma_desc); 4915 } 4916 4917 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true, 4918 rx_q->dma_rx_phy, desc_size); 4919 } 4920 while (count < limit) { 4921 struct stmmac_rx_buffer *buf; 4922 unsigned int buf1_len = 0; 4923 struct dma_desc *np, *p; 4924 int entry; 4925 int res; 4926 4927 if (!count && rx_q->state_saved) { 4928 error = rx_q->state.error; 4929 len = rx_q->state.len; 4930 } else { 4931 rx_q->state_saved = false; 4932 error = 0; 4933 len = 0; 4934 } 4935 4936 if (count >= limit) 4937 break; 4938 4939 read_again: 4940 buf1_len = 0; 4941 entry = next_entry; 4942 buf = &rx_q->buf_pool[entry]; 4943 4944 if (dirty >= STMMAC_RX_FILL_BATCH) { 4945 failure = failure || 4946 !stmmac_rx_refill_zc(priv, queue, dirty); 4947 dirty = 0; 4948 } 4949 4950 if (priv->extend_desc) 4951 p = (struct dma_desc *)(rx_q->dma_erx + entry); 4952 else 4953 p = rx_q->dma_rx + entry; 4954 4955 /* read the status of the incoming frame */ 4956 status = stmmac_rx_status(priv, &priv->dev->stats, 4957 &priv->xstats, p); 4958 /* check if managed by the DMA otherwise go ahead */ 4959 if (unlikely(status & dma_own)) 4960 break; 4961 4962 /* Prefetch the next RX descriptor */ 4963 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx, 4964 priv->dma_rx_size); 4965 next_entry = rx_q->cur_rx; 4966 4967 if (priv->extend_desc) 4968 np = (struct dma_desc *)(rx_q->dma_erx + next_entry); 4969 else 4970 np = rx_q->dma_rx + next_entry; 4971 4972 prefetch(np); 4973 4974 /* Ensure a valid XSK buffer before proceed */ 4975 if (!buf->xdp) 4976 break; 4977 4978 if (priv->extend_desc) 4979 stmmac_rx_extended_status(priv, &priv->dev->stats, 4980 &priv->xstats, 4981 rx_q->dma_erx + entry); 4982 if (unlikely(status == discard_frame)) { 4983 xsk_buff_free(buf->xdp); 4984 buf->xdp = NULL; 4985 dirty++; 4986 error = 1; 4987 if (!priv->hwts_rx_en) 4988 priv->dev->stats.rx_errors++; 4989 } 4990 4991 if (unlikely(error && (status & rx_not_ls))) 4992 goto read_again; 4993 if (unlikely(error)) { 4994 count++; 4995 continue; 4996 } 4997 4998 /* XSK pool expects RX frame 1:1 mapped to XSK buffer */ 4999 if (likely(status & rx_not_ls)) { 5000 xsk_buff_free(buf->xdp); 5001 buf->xdp = NULL; 5002 dirty++; 5003 count++; 5004 goto read_again; 5005 } 5006 5007 /* XDP ZC Frame only support primary buffers for now */ 5008 buf1_len = stmmac_rx_buf1_len(priv, p, status, len); 5009 len += buf1_len; 5010 5011 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3 5012 * Type frames (LLC/LLC-SNAP) 5013 * 5014 * llc_snap is never checked in GMAC >= 4, so this ACS 5015 * feature is always disabled and packets need to be 5016 * stripped manually. 5017 */ 5018 if (likely(!(status & rx_not_ls)) && 5019 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) || 5020 unlikely(status != llc_snap))) { 5021 buf1_len -= ETH_FCS_LEN; 5022 len -= ETH_FCS_LEN; 5023 } 5024 5025 /* RX buffer is good and fit into a XSK pool buffer */ 5026 buf->xdp->data_end = buf->xdp->data + buf1_len; 5027 xsk_buff_dma_sync_for_cpu(buf->xdp, rx_q->xsk_pool); 5028 5029 prog = READ_ONCE(priv->xdp_prog); 5030 res = __stmmac_xdp_run_prog(priv, prog, buf->xdp); 5031 5032 switch (res) { 5033 case STMMAC_XDP_PASS: 5034 stmmac_dispatch_skb_zc(priv, queue, p, np, buf->xdp); 5035 xsk_buff_free(buf->xdp); 5036 break; 5037 case STMMAC_XDP_CONSUMED: 5038 xsk_buff_free(buf->xdp); 5039 priv->dev->stats.rx_dropped++; 5040 break; 5041 case STMMAC_XDP_TX: 5042 case STMMAC_XDP_REDIRECT: 5043 xdp_status |= res; 5044 break; 5045 } 5046 5047 buf->xdp = NULL; 5048 dirty++; 5049 count++; 5050 } 5051 5052 if (status & rx_not_ls) { 5053 rx_q->state_saved = true; 5054 rx_q->state.error = error; 5055 rx_q->state.len = len; 5056 } 5057 5058 stmmac_finalize_xdp_rx(priv, xdp_status); 5059 5060 priv->xstats.rx_pkt_n += count; 5061 priv->xstats.rxq_stats[queue].rx_pkt_n += count; 5062 5063 if (xsk_uses_need_wakeup(rx_q->xsk_pool)) { 5064 if (failure || stmmac_rx_dirty(priv, queue) > 0) 5065 xsk_set_rx_need_wakeup(rx_q->xsk_pool); 5066 else 5067 xsk_clear_rx_need_wakeup(rx_q->xsk_pool); 5068 5069 return (int)count; 5070 } 5071 5072 return failure ? limit : (int)count; 5073 } 5074 5075 /** 5076 * stmmac_rx - manage the receive process 5077 * @priv: driver private structure 5078 * @limit: napi bugget 5079 * @queue: RX queue index. 5080 * Description : this the function called by the napi poll method. 5081 * It gets all the frames inside the ring. 5082 */ 5083 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue) 5084 { 5085 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 5086 struct stmmac_channel *ch = &priv->channel[queue]; 5087 unsigned int count = 0, error = 0, len = 0; 5088 int status = 0, coe = priv->hw->rx_csum; 5089 unsigned int next_entry = rx_q->cur_rx; 5090 enum dma_data_direction dma_dir; 5091 unsigned int desc_size; 5092 struct sk_buff *skb = NULL; 5093 struct xdp_buff xdp; 5094 int xdp_status = 0; 5095 int buf_sz; 5096 5097 dma_dir = page_pool_get_dma_dir(rx_q->page_pool); 5098 buf_sz = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE; 5099 5100 if (netif_msg_rx_status(priv)) { 5101 void *rx_head; 5102 5103 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__); 5104 if (priv->extend_desc) { 5105 rx_head = (void *)rx_q->dma_erx; 5106 desc_size = sizeof(struct dma_extended_desc); 5107 } else { 5108 rx_head = (void *)rx_q->dma_rx; 5109 desc_size = sizeof(struct dma_desc); 5110 } 5111 5112 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true, 5113 rx_q->dma_rx_phy, desc_size); 5114 } 5115 while (count < limit) { 5116 unsigned int buf1_len = 0, buf2_len = 0; 5117 enum pkt_hash_types hash_type; 5118 struct stmmac_rx_buffer *buf; 5119 struct dma_desc *np, *p; 5120 int entry; 5121 u32 hash; 5122 5123 if (!count && rx_q->state_saved) { 5124 skb = rx_q->state.skb; 5125 error = rx_q->state.error; 5126 len = rx_q->state.len; 5127 } else { 5128 rx_q->state_saved = false; 5129 skb = NULL; 5130 error = 0; 5131 len = 0; 5132 } 5133 5134 if (count >= limit) 5135 break; 5136 5137 read_again: 5138 buf1_len = 0; 5139 buf2_len = 0; 5140 entry = next_entry; 5141 buf = &rx_q->buf_pool[entry]; 5142 5143 if (priv->extend_desc) 5144 p = (struct dma_desc *)(rx_q->dma_erx + entry); 5145 else 5146 p = rx_q->dma_rx + entry; 5147 5148 /* read the status of the incoming frame */ 5149 status = stmmac_rx_status(priv, &priv->dev->stats, 5150 &priv->xstats, p); 5151 /* check if managed by the DMA otherwise go ahead */ 5152 if (unlikely(status & dma_own)) 5153 break; 5154 5155 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx, 5156 priv->dma_rx_size); 5157 next_entry = rx_q->cur_rx; 5158 5159 if (priv->extend_desc) 5160 np = (struct dma_desc *)(rx_q->dma_erx + next_entry); 5161 else 5162 np = rx_q->dma_rx + next_entry; 5163 5164 prefetch(np); 5165 5166 if (priv->extend_desc) 5167 stmmac_rx_extended_status(priv, &priv->dev->stats, 5168 &priv->xstats, rx_q->dma_erx + entry); 5169 if (unlikely(status == discard_frame)) { 5170 page_pool_recycle_direct(rx_q->page_pool, buf->page); 5171 buf->page = NULL; 5172 error = 1; 5173 if (!priv->hwts_rx_en) 5174 priv->dev->stats.rx_errors++; 5175 } 5176 5177 if (unlikely(error && (status & rx_not_ls))) 5178 goto read_again; 5179 if (unlikely(error)) { 5180 dev_kfree_skb(skb); 5181 skb = NULL; 5182 count++; 5183 continue; 5184 } 5185 5186 /* Buffer is good. Go on. */ 5187 5188 prefetch(page_address(buf->page) + buf->page_offset); 5189 if (buf->sec_page) 5190 prefetch(page_address(buf->sec_page)); 5191 5192 buf1_len = stmmac_rx_buf1_len(priv, p, status, len); 5193 len += buf1_len; 5194 buf2_len = stmmac_rx_buf2_len(priv, p, status, len); 5195 len += buf2_len; 5196 5197 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3 5198 * Type frames (LLC/LLC-SNAP) 5199 * 5200 * llc_snap is never checked in GMAC >= 4, so this ACS 5201 * feature is always disabled and packets need to be 5202 * stripped manually. 5203 */ 5204 if (likely(!(status & rx_not_ls)) && 5205 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) || 5206 unlikely(status != llc_snap))) { 5207 if (buf2_len) { 5208 buf2_len -= ETH_FCS_LEN; 5209 len -= ETH_FCS_LEN; 5210 } else if (buf1_len) { 5211 buf1_len -= ETH_FCS_LEN; 5212 len -= ETH_FCS_LEN; 5213 } 5214 } 5215 5216 if (!skb) { 5217 unsigned int pre_len, sync_len; 5218 5219 dma_sync_single_for_cpu(priv->device, buf->addr, 5220 buf1_len, dma_dir); 5221 5222 xdp_init_buff(&xdp, buf_sz, &rx_q->xdp_rxq); 5223 xdp_prepare_buff(&xdp, page_address(buf->page), 5224 buf->page_offset, buf1_len, false); 5225 5226 pre_len = xdp.data_end - xdp.data_hard_start - 5227 buf->page_offset; 5228 skb = stmmac_xdp_run_prog(priv, &xdp); 5229 /* Due xdp_adjust_tail: DMA sync for_device 5230 * cover max len CPU touch 5231 */ 5232 sync_len = xdp.data_end - xdp.data_hard_start - 5233 buf->page_offset; 5234 sync_len = max(sync_len, pre_len); 5235 5236 /* For Not XDP_PASS verdict */ 5237 if (IS_ERR(skb)) { 5238 unsigned int xdp_res = -PTR_ERR(skb); 5239 5240 if (xdp_res & STMMAC_XDP_CONSUMED) { 5241 page_pool_put_page(rx_q->page_pool, 5242 virt_to_head_page(xdp.data), 5243 sync_len, true); 5244 buf->page = NULL; 5245 priv->dev->stats.rx_dropped++; 5246 5247 /* Clear skb as it was set as 5248 * status by XDP program. 5249 */ 5250 skb = NULL; 5251 5252 if (unlikely((status & rx_not_ls))) 5253 goto read_again; 5254 5255 count++; 5256 continue; 5257 } else if (xdp_res & (STMMAC_XDP_TX | 5258 STMMAC_XDP_REDIRECT)) { 5259 xdp_status |= xdp_res; 5260 buf->page = NULL; 5261 skb = NULL; 5262 count++; 5263 continue; 5264 } 5265 } 5266 } 5267 5268 if (!skb) { 5269 /* XDP program may expand or reduce tail */ 5270 buf1_len = xdp.data_end - xdp.data; 5271 5272 skb = napi_alloc_skb(&ch->rx_napi, buf1_len); 5273 if (!skb) { 5274 priv->dev->stats.rx_dropped++; 5275 count++; 5276 goto drain_data; 5277 } 5278 5279 /* XDP program may adjust header */ 5280 skb_copy_to_linear_data(skb, xdp.data, buf1_len); 5281 skb_put(skb, buf1_len); 5282 5283 /* Data payload copied into SKB, page ready for recycle */ 5284 page_pool_recycle_direct(rx_q->page_pool, buf->page); 5285 buf->page = NULL; 5286 } else if (buf1_len) { 5287 dma_sync_single_for_cpu(priv->device, buf->addr, 5288 buf1_len, dma_dir); 5289 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 5290 buf->page, buf->page_offset, buf1_len, 5291 priv->dma_buf_sz); 5292 5293 /* Data payload appended into SKB */ 5294 page_pool_release_page(rx_q->page_pool, buf->page); 5295 buf->page = NULL; 5296 } 5297 5298 if (buf2_len) { 5299 dma_sync_single_for_cpu(priv->device, buf->sec_addr, 5300 buf2_len, dma_dir); 5301 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 5302 buf->sec_page, 0, buf2_len, 5303 priv->dma_buf_sz); 5304 5305 /* Data payload appended into SKB */ 5306 page_pool_release_page(rx_q->page_pool, buf->sec_page); 5307 buf->sec_page = NULL; 5308 } 5309 5310 drain_data: 5311 if (likely(status & rx_not_ls)) 5312 goto read_again; 5313 if (!skb) 5314 continue; 5315 5316 /* Got entire packet into SKB. Finish it. */ 5317 5318 stmmac_get_rx_hwtstamp(priv, p, np, skb); 5319 stmmac_rx_vlan(priv->dev, skb); 5320 skb->protocol = eth_type_trans(skb, priv->dev); 5321 5322 if (unlikely(!coe)) 5323 skb_checksum_none_assert(skb); 5324 else 5325 skb->ip_summed = CHECKSUM_UNNECESSARY; 5326 5327 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type)) 5328 skb_set_hash(skb, hash, hash_type); 5329 5330 skb_record_rx_queue(skb, queue); 5331 napi_gro_receive(&ch->rx_napi, skb); 5332 skb = NULL; 5333 5334 priv->dev->stats.rx_packets++; 5335 priv->dev->stats.rx_bytes += len; 5336 count++; 5337 } 5338 5339 if (status & rx_not_ls || skb) { 5340 rx_q->state_saved = true; 5341 rx_q->state.skb = skb; 5342 rx_q->state.error = error; 5343 rx_q->state.len = len; 5344 } 5345 5346 stmmac_finalize_xdp_rx(priv, xdp_status); 5347 5348 stmmac_rx_refill(priv, queue); 5349 5350 priv->xstats.rx_pkt_n += count; 5351 priv->xstats.rxq_stats[queue].rx_pkt_n += count; 5352 5353 return count; 5354 } 5355 5356 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget) 5357 { 5358 struct stmmac_channel *ch = 5359 container_of(napi, struct stmmac_channel, rx_napi); 5360 struct stmmac_priv *priv = ch->priv_data; 5361 u32 chan = ch->index; 5362 int work_done; 5363 5364 priv->xstats.napi_poll++; 5365 5366 work_done = stmmac_rx(priv, budget, chan); 5367 if (work_done < budget && napi_complete_done(napi, work_done)) { 5368 unsigned long flags; 5369 5370 spin_lock_irqsave(&ch->lock, flags); 5371 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0); 5372 spin_unlock_irqrestore(&ch->lock, flags); 5373 } 5374 5375 return work_done; 5376 } 5377 5378 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget) 5379 { 5380 struct stmmac_channel *ch = 5381 container_of(napi, struct stmmac_channel, tx_napi); 5382 struct stmmac_priv *priv = ch->priv_data; 5383 u32 chan = ch->index; 5384 int work_done; 5385 5386 priv->xstats.napi_poll++; 5387 5388 work_done = stmmac_tx_clean(priv, budget, chan); 5389 work_done = min(work_done, budget); 5390 5391 if (work_done < budget && napi_complete_done(napi, work_done)) { 5392 unsigned long flags; 5393 5394 spin_lock_irqsave(&ch->lock, flags); 5395 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1); 5396 spin_unlock_irqrestore(&ch->lock, flags); 5397 } 5398 5399 return work_done; 5400 } 5401 5402 static int stmmac_napi_poll_rxtx(struct napi_struct *napi, int budget) 5403 { 5404 struct stmmac_channel *ch = 5405 container_of(napi, struct stmmac_channel, rxtx_napi); 5406 struct stmmac_priv *priv = ch->priv_data; 5407 int rx_done, tx_done, rxtx_done; 5408 u32 chan = ch->index; 5409 5410 priv->xstats.napi_poll++; 5411 5412 tx_done = stmmac_tx_clean(priv, budget, chan); 5413 tx_done = min(tx_done, budget); 5414 5415 rx_done = stmmac_rx_zc(priv, budget, chan); 5416 5417 rxtx_done = max(tx_done, rx_done); 5418 5419 /* If either TX or RX work is not complete, return budget 5420 * and keep pooling 5421 */ 5422 if (rxtx_done >= budget) 5423 return budget; 5424 5425 /* all work done, exit the polling mode */ 5426 if (napi_complete_done(napi, rxtx_done)) { 5427 unsigned long flags; 5428 5429 spin_lock_irqsave(&ch->lock, flags); 5430 /* Both RX and TX work done are compelte, 5431 * so enable both RX & TX IRQs. 5432 */ 5433 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 1); 5434 spin_unlock_irqrestore(&ch->lock, flags); 5435 } 5436 5437 return min(rxtx_done, budget - 1); 5438 } 5439 5440 /** 5441 * stmmac_tx_timeout 5442 * @dev : Pointer to net device structure 5443 * @txqueue: the index of the hanging transmit queue 5444 * Description: this function is called when a packet transmission fails to 5445 * complete within a reasonable time. The driver will mark the error in the 5446 * netdev structure and arrange for the device to be reset to a sane state 5447 * in order to transmit a new packet. 5448 */ 5449 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue) 5450 { 5451 struct stmmac_priv *priv = netdev_priv(dev); 5452 5453 stmmac_global_err(priv); 5454 } 5455 5456 /** 5457 * stmmac_set_rx_mode - entry point for multicast addressing 5458 * @dev : pointer to the device structure 5459 * Description: 5460 * This function is a driver entry point which gets called by the kernel 5461 * whenever multicast addresses must be enabled/disabled. 5462 * Return value: 5463 * void. 5464 */ 5465 static void stmmac_set_rx_mode(struct net_device *dev) 5466 { 5467 struct stmmac_priv *priv = netdev_priv(dev); 5468 5469 stmmac_set_filter(priv, priv->hw, dev); 5470 } 5471 5472 /** 5473 * stmmac_change_mtu - entry point to change MTU size for the device. 5474 * @dev : device pointer. 5475 * @new_mtu : the new MTU size for the device. 5476 * Description: the Maximum Transfer Unit (MTU) is used by the network layer 5477 * to drive packet transmission. Ethernet has an MTU of 1500 octets 5478 * (ETH_DATA_LEN). This value can be changed with ifconfig. 5479 * Return value: 5480 * 0 on success and an appropriate (-)ve integer as defined in errno.h 5481 * file on failure. 5482 */ 5483 static int stmmac_change_mtu(struct net_device *dev, int new_mtu) 5484 { 5485 struct stmmac_priv *priv = netdev_priv(dev); 5486 int txfifosz = priv->plat->tx_fifo_size; 5487 const int mtu = new_mtu; 5488 5489 if (txfifosz == 0) 5490 txfifosz = priv->dma_cap.tx_fifo_size; 5491 5492 txfifosz /= priv->plat->tx_queues_to_use; 5493 5494 if (netif_running(dev)) { 5495 netdev_err(priv->dev, "must be stopped to change its MTU\n"); 5496 return -EBUSY; 5497 } 5498 5499 if (stmmac_xdp_is_enabled(priv) && new_mtu > ETH_DATA_LEN) { 5500 netdev_dbg(priv->dev, "Jumbo frames not supported for XDP\n"); 5501 return -EINVAL; 5502 } 5503 5504 new_mtu = STMMAC_ALIGN(new_mtu); 5505 5506 /* If condition true, FIFO is too small or MTU too large */ 5507 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB)) 5508 return -EINVAL; 5509 5510 dev->mtu = mtu; 5511 5512 netdev_update_features(dev); 5513 5514 return 0; 5515 } 5516 5517 static netdev_features_t stmmac_fix_features(struct net_device *dev, 5518 netdev_features_t features) 5519 { 5520 struct stmmac_priv *priv = netdev_priv(dev); 5521 5522 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE) 5523 features &= ~NETIF_F_RXCSUM; 5524 5525 if (!priv->plat->tx_coe) 5526 features &= ~NETIF_F_CSUM_MASK; 5527 5528 /* Some GMAC devices have a bugged Jumbo frame support that 5529 * needs to have the Tx COE disabled for oversized frames 5530 * (due to limited buffer sizes). In this case we disable 5531 * the TX csum insertion in the TDES and not use SF. 5532 */ 5533 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN)) 5534 features &= ~NETIF_F_CSUM_MASK; 5535 5536 /* Disable tso if asked by ethtool */ 5537 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) { 5538 if (features & NETIF_F_TSO) 5539 priv->tso = true; 5540 else 5541 priv->tso = false; 5542 } 5543 5544 return features; 5545 } 5546 5547 static int stmmac_set_features(struct net_device *netdev, 5548 netdev_features_t features) 5549 { 5550 struct stmmac_priv *priv = netdev_priv(netdev); 5551 5552 /* Keep the COE Type in case of csum is supporting */ 5553 if (features & NETIF_F_RXCSUM) 5554 priv->hw->rx_csum = priv->plat->rx_coe; 5555 else 5556 priv->hw->rx_csum = 0; 5557 /* No check needed because rx_coe has been set before and it will be 5558 * fixed in case of issue. 5559 */ 5560 stmmac_rx_ipc(priv, priv->hw); 5561 5562 if (priv->sph_cap) { 5563 bool sph_en = (priv->hw->rx_csum > 0) && priv->sph; 5564 u32 chan; 5565 5566 for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++) 5567 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan); 5568 } 5569 5570 return 0; 5571 } 5572 5573 static void stmmac_fpe_event_status(struct stmmac_priv *priv, int status) 5574 { 5575 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg; 5576 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state; 5577 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state; 5578 bool *hs_enable = &fpe_cfg->hs_enable; 5579 5580 if (status == FPE_EVENT_UNKNOWN || !*hs_enable) 5581 return; 5582 5583 /* If LP has sent verify mPacket, LP is FPE capable */ 5584 if ((status & FPE_EVENT_RVER) == FPE_EVENT_RVER) { 5585 if (*lp_state < FPE_STATE_CAPABLE) 5586 *lp_state = FPE_STATE_CAPABLE; 5587 5588 /* If user has requested FPE enable, quickly response */ 5589 if (*hs_enable) 5590 stmmac_fpe_send_mpacket(priv, priv->ioaddr, 5591 MPACKET_RESPONSE); 5592 } 5593 5594 /* If Local has sent verify mPacket, Local is FPE capable */ 5595 if ((status & FPE_EVENT_TVER) == FPE_EVENT_TVER) { 5596 if (*lo_state < FPE_STATE_CAPABLE) 5597 *lo_state = FPE_STATE_CAPABLE; 5598 } 5599 5600 /* If LP has sent response mPacket, LP is entering FPE ON */ 5601 if ((status & FPE_EVENT_RRSP) == FPE_EVENT_RRSP) 5602 *lp_state = FPE_STATE_ENTERING_ON; 5603 5604 /* If Local has sent response mPacket, Local is entering FPE ON */ 5605 if ((status & FPE_EVENT_TRSP) == FPE_EVENT_TRSP) 5606 *lo_state = FPE_STATE_ENTERING_ON; 5607 5608 if (!test_bit(__FPE_REMOVING, &priv->fpe_task_state) && 5609 !test_and_set_bit(__FPE_TASK_SCHED, &priv->fpe_task_state) && 5610 priv->fpe_wq) { 5611 queue_work(priv->fpe_wq, &priv->fpe_task); 5612 } 5613 } 5614 5615 static void stmmac_common_interrupt(struct stmmac_priv *priv) 5616 { 5617 u32 rx_cnt = priv->plat->rx_queues_to_use; 5618 u32 tx_cnt = priv->plat->tx_queues_to_use; 5619 u32 queues_count; 5620 u32 queue; 5621 bool xmac; 5622 5623 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac; 5624 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt; 5625 5626 if (priv->irq_wake) 5627 pm_wakeup_event(priv->device, 0); 5628 5629 if (priv->dma_cap.estsel) 5630 stmmac_est_irq_status(priv, priv->ioaddr, priv->dev, 5631 &priv->xstats, tx_cnt); 5632 5633 if (priv->dma_cap.fpesel) { 5634 int status = stmmac_fpe_irq_status(priv, priv->ioaddr, 5635 priv->dev); 5636 5637 stmmac_fpe_event_status(priv, status); 5638 } 5639 5640 /* To handle GMAC own interrupts */ 5641 if ((priv->plat->has_gmac) || xmac) { 5642 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats); 5643 5644 if (unlikely(status)) { 5645 /* For LPI we need to save the tx status */ 5646 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE) 5647 priv->tx_path_in_lpi_mode = true; 5648 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE) 5649 priv->tx_path_in_lpi_mode = false; 5650 } 5651 5652 for (queue = 0; queue < queues_count; queue++) { 5653 status = stmmac_host_mtl_irq_status(priv, priv->hw, 5654 queue); 5655 } 5656 5657 /* PCS link status */ 5658 if (priv->hw->pcs) { 5659 if (priv->xstats.pcs_link) 5660 netif_carrier_on(priv->dev); 5661 else 5662 netif_carrier_off(priv->dev); 5663 } 5664 5665 stmmac_timestamp_interrupt(priv, priv); 5666 } 5667 } 5668 5669 /** 5670 * stmmac_interrupt - main ISR 5671 * @irq: interrupt number. 5672 * @dev_id: to pass the net device pointer. 5673 * Description: this is the main driver interrupt service routine. 5674 * It can call: 5675 * o DMA service routine (to manage incoming frame reception and transmission 5676 * status) 5677 * o Core interrupts to manage: remote wake-up, management counter, LPI 5678 * interrupts. 5679 */ 5680 static irqreturn_t stmmac_interrupt(int irq, void *dev_id) 5681 { 5682 struct net_device *dev = (struct net_device *)dev_id; 5683 struct stmmac_priv *priv = netdev_priv(dev); 5684 5685 /* Check if adapter is up */ 5686 if (test_bit(STMMAC_DOWN, &priv->state)) 5687 return IRQ_HANDLED; 5688 5689 /* Check if a fatal error happened */ 5690 if (stmmac_safety_feat_interrupt(priv)) 5691 return IRQ_HANDLED; 5692 5693 /* To handle Common interrupts */ 5694 stmmac_common_interrupt(priv); 5695 5696 /* To handle DMA interrupts */ 5697 stmmac_dma_interrupt(priv); 5698 5699 return IRQ_HANDLED; 5700 } 5701 5702 static irqreturn_t stmmac_mac_interrupt(int irq, void *dev_id) 5703 { 5704 struct net_device *dev = (struct net_device *)dev_id; 5705 struct stmmac_priv *priv = netdev_priv(dev); 5706 5707 if (unlikely(!dev)) { 5708 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__); 5709 return IRQ_NONE; 5710 } 5711 5712 /* Check if adapter is up */ 5713 if (test_bit(STMMAC_DOWN, &priv->state)) 5714 return IRQ_HANDLED; 5715 5716 /* To handle Common interrupts */ 5717 stmmac_common_interrupt(priv); 5718 5719 return IRQ_HANDLED; 5720 } 5721 5722 static irqreturn_t stmmac_safety_interrupt(int irq, void *dev_id) 5723 { 5724 struct net_device *dev = (struct net_device *)dev_id; 5725 struct stmmac_priv *priv = netdev_priv(dev); 5726 5727 if (unlikely(!dev)) { 5728 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__); 5729 return IRQ_NONE; 5730 } 5731 5732 /* Check if adapter is up */ 5733 if (test_bit(STMMAC_DOWN, &priv->state)) 5734 return IRQ_HANDLED; 5735 5736 /* Check if a fatal error happened */ 5737 stmmac_safety_feat_interrupt(priv); 5738 5739 return IRQ_HANDLED; 5740 } 5741 5742 static irqreturn_t stmmac_msi_intr_tx(int irq, void *data) 5743 { 5744 struct stmmac_tx_queue *tx_q = (struct stmmac_tx_queue *)data; 5745 int chan = tx_q->queue_index; 5746 struct stmmac_priv *priv; 5747 int status; 5748 5749 priv = container_of(tx_q, struct stmmac_priv, tx_queue[chan]); 5750 5751 if (unlikely(!data)) { 5752 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__); 5753 return IRQ_NONE; 5754 } 5755 5756 /* Check if adapter is up */ 5757 if (test_bit(STMMAC_DOWN, &priv->state)) 5758 return IRQ_HANDLED; 5759 5760 status = stmmac_napi_check(priv, chan, DMA_DIR_TX); 5761 5762 if (unlikely(status & tx_hard_error_bump_tc)) { 5763 /* Try to bump up the dma threshold on this failure */ 5764 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) && 5765 tc <= 256) { 5766 tc += 64; 5767 if (priv->plat->force_thresh_dma_mode) 5768 stmmac_set_dma_operation_mode(priv, 5769 tc, 5770 tc, 5771 chan); 5772 else 5773 stmmac_set_dma_operation_mode(priv, 5774 tc, 5775 SF_DMA_MODE, 5776 chan); 5777 priv->xstats.threshold = tc; 5778 } 5779 } else if (unlikely(status == tx_hard_error)) { 5780 stmmac_tx_err(priv, chan); 5781 } 5782 5783 return IRQ_HANDLED; 5784 } 5785 5786 static irqreturn_t stmmac_msi_intr_rx(int irq, void *data) 5787 { 5788 struct stmmac_rx_queue *rx_q = (struct stmmac_rx_queue *)data; 5789 int chan = rx_q->queue_index; 5790 struct stmmac_priv *priv; 5791 5792 priv = container_of(rx_q, struct stmmac_priv, rx_queue[chan]); 5793 5794 if (unlikely(!data)) { 5795 netdev_err(priv->dev, "%s: invalid dev pointer\n", __func__); 5796 return IRQ_NONE; 5797 } 5798 5799 /* Check if adapter is up */ 5800 if (test_bit(STMMAC_DOWN, &priv->state)) 5801 return IRQ_HANDLED; 5802 5803 stmmac_napi_check(priv, chan, DMA_DIR_RX); 5804 5805 return IRQ_HANDLED; 5806 } 5807 5808 #ifdef CONFIG_NET_POLL_CONTROLLER 5809 /* Polling receive - used by NETCONSOLE and other diagnostic tools 5810 * to allow network I/O with interrupts disabled. 5811 */ 5812 static void stmmac_poll_controller(struct net_device *dev) 5813 { 5814 struct stmmac_priv *priv = netdev_priv(dev); 5815 int i; 5816 5817 /* If adapter is down, do nothing */ 5818 if (test_bit(STMMAC_DOWN, &priv->state)) 5819 return; 5820 5821 if (priv->plat->multi_msi_en) { 5822 for (i = 0; i < priv->plat->rx_queues_to_use; i++) 5823 stmmac_msi_intr_rx(0, &priv->rx_queue[i]); 5824 5825 for (i = 0; i < priv->plat->tx_queues_to_use; i++) 5826 stmmac_msi_intr_tx(0, &priv->tx_queue[i]); 5827 } else { 5828 disable_irq(dev->irq); 5829 stmmac_interrupt(dev->irq, dev); 5830 enable_irq(dev->irq); 5831 } 5832 } 5833 #endif 5834 5835 /** 5836 * stmmac_ioctl - Entry point for the Ioctl 5837 * @dev: Device pointer. 5838 * @rq: An IOCTL specefic structure, that can contain a pointer to 5839 * a proprietary structure used to pass information to the driver. 5840 * @cmd: IOCTL command 5841 * Description: 5842 * Currently it supports the phy_mii_ioctl(...) and HW time stamping. 5843 */ 5844 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 5845 { 5846 struct stmmac_priv *priv = netdev_priv (dev); 5847 int ret = -EOPNOTSUPP; 5848 5849 if (!netif_running(dev)) 5850 return -EINVAL; 5851 5852 switch (cmd) { 5853 case SIOCGMIIPHY: 5854 case SIOCGMIIREG: 5855 case SIOCSMIIREG: 5856 ret = phylink_mii_ioctl(priv->phylink, rq, cmd); 5857 break; 5858 case SIOCSHWTSTAMP: 5859 ret = stmmac_hwtstamp_set(dev, rq); 5860 break; 5861 case SIOCGHWTSTAMP: 5862 ret = stmmac_hwtstamp_get(dev, rq); 5863 break; 5864 default: 5865 break; 5866 } 5867 5868 return ret; 5869 } 5870 5871 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 5872 void *cb_priv) 5873 { 5874 struct stmmac_priv *priv = cb_priv; 5875 int ret = -EOPNOTSUPP; 5876 5877 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data)) 5878 return ret; 5879 5880 __stmmac_disable_all_queues(priv); 5881 5882 switch (type) { 5883 case TC_SETUP_CLSU32: 5884 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data); 5885 break; 5886 case TC_SETUP_CLSFLOWER: 5887 ret = stmmac_tc_setup_cls(priv, priv, type_data); 5888 break; 5889 default: 5890 break; 5891 } 5892 5893 stmmac_enable_all_queues(priv); 5894 return ret; 5895 } 5896 5897 static LIST_HEAD(stmmac_block_cb_list); 5898 5899 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type, 5900 void *type_data) 5901 { 5902 struct stmmac_priv *priv = netdev_priv(ndev); 5903 5904 switch (type) { 5905 case TC_SETUP_BLOCK: 5906 return flow_block_cb_setup_simple(type_data, 5907 &stmmac_block_cb_list, 5908 stmmac_setup_tc_block_cb, 5909 priv, priv, true); 5910 case TC_SETUP_QDISC_CBS: 5911 return stmmac_tc_setup_cbs(priv, priv, type_data); 5912 case TC_SETUP_QDISC_TAPRIO: 5913 return stmmac_tc_setup_taprio(priv, priv, type_data); 5914 case TC_SETUP_QDISC_ETF: 5915 return stmmac_tc_setup_etf(priv, priv, type_data); 5916 default: 5917 return -EOPNOTSUPP; 5918 } 5919 } 5920 5921 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb, 5922 struct net_device *sb_dev) 5923 { 5924 int gso = skb_shinfo(skb)->gso_type; 5925 5926 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) { 5927 /* 5928 * There is no way to determine the number of TSO/USO 5929 * capable Queues. Let's use always the Queue 0 5930 * because if TSO/USO is supported then at least this 5931 * one will be capable. 5932 */ 5933 return 0; 5934 } 5935 5936 return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues; 5937 } 5938 5939 static int stmmac_set_mac_address(struct net_device *ndev, void *addr) 5940 { 5941 struct stmmac_priv *priv = netdev_priv(ndev); 5942 int ret = 0; 5943 5944 ret = pm_runtime_get_sync(priv->device); 5945 if (ret < 0) { 5946 pm_runtime_put_noidle(priv->device); 5947 return ret; 5948 } 5949 5950 ret = eth_mac_addr(ndev, addr); 5951 if (ret) 5952 goto set_mac_error; 5953 5954 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0); 5955 5956 set_mac_error: 5957 pm_runtime_put(priv->device); 5958 5959 return ret; 5960 } 5961 5962 #ifdef CONFIG_DEBUG_FS 5963 static struct dentry *stmmac_fs_dir; 5964 5965 static void sysfs_display_ring(void *head, int size, int extend_desc, 5966 struct seq_file *seq, dma_addr_t dma_phy_addr) 5967 { 5968 int i; 5969 struct dma_extended_desc *ep = (struct dma_extended_desc *)head; 5970 struct dma_desc *p = (struct dma_desc *)head; 5971 dma_addr_t dma_addr; 5972 5973 for (i = 0; i < size; i++) { 5974 if (extend_desc) { 5975 dma_addr = dma_phy_addr + i * sizeof(*ep); 5976 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n", 5977 i, &dma_addr, 5978 le32_to_cpu(ep->basic.des0), 5979 le32_to_cpu(ep->basic.des1), 5980 le32_to_cpu(ep->basic.des2), 5981 le32_to_cpu(ep->basic.des3)); 5982 ep++; 5983 } else { 5984 dma_addr = dma_phy_addr + i * sizeof(*p); 5985 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n", 5986 i, &dma_addr, 5987 le32_to_cpu(p->des0), le32_to_cpu(p->des1), 5988 le32_to_cpu(p->des2), le32_to_cpu(p->des3)); 5989 p++; 5990 } 5991 seq_printf(seq, "\n"); 5992 } 5993 } 5994 5995 static int stmmac_rings_status_show(struct seq_file *seq, void *v) 5996 { 5997 struct net_device *dev = seq->private; 5998 struct stmmac_priv *priv = netdev_priv(dev); 5999 u32 rx_count = priv->plat->rx_queues_to_use; 6000 u32 tx_count = priv->plat->tx_queues_to_use; 6001 u32 queue; 6002 6003 if ((dev->flags & IFF_UP) == 0) 6004 return 0; 6005 6006 for (queue = 0; queue < rx_count; queue++) { 6007 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 6008 6009 seq_printf(seq, "RX Queue %d:\n", queue); 6010 6011 if (priv->extend_desc) { 6012 seq_printf(seq, "Extended descriptor ring:\n"); 6013 sysfs_display_ring((void *)rx_q->dma_erx, 6014 priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy); 6015 } else { 6016 seq_printf(seq, "Descriptor ring:\n"); 6017 sysfs_display_ring((void *)rx_q->dma_rx, 6018 priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy); 6019 } 6020 } 6021 6022 for (queue = 0; queue < tx_count; queue++) { 6023 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 6024 6025 seq_printf(seq, "TX Queue %d:\n", queue); 6026 6027 if (priv->extend_desc) { 6028 seq_printf(seq, "Extended descriptor ring:\n"); 6029 sysfs_display_ring((void *)tx_q->dma_etx, 6030 priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy); 6031 } else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) { 6032 seq_printf(seq, "Descriptor ring:\n"); 6033 sysfs_display_ring((void *)tx_q->dma_tx, 6034 priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy); 6035 } 6036 } 6037 6038 return 0; 6039 } 6040 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status); 6041 6042 static int stmmac_dma_cap_show(struct seq_file *seq, void *v) 6043 { 6044 struct net_device *dev = seq->private; 6045 struct stmmac_priv *priv = netdev_priv(dev); 6046 6047 if (!priv->hw_cap_support) { 6048 seq_printf(seq, "DMA HW features not supported\n"); 6049 return 0; 6050 } 6051 6052 seq_printf(seq, "==============================\n"); 6053 seq_printf(seq, "\tDMA HW features\n"); 6054 seq_printf(seq, "==============================\n"); 6055 6056 seq_printf(seq, "\t10/100 Mbps: %s\n", 6057 (priv->dma_cap.mbps_10_100) ? "Y" : "N"); 6058 seq_printf(seq, "\t1000 Mbps: %s\n", 6059 (priv->dma_cap.mbps_1000) ? "Y" : "N"); 6060 seq_printf(seq, "\tHalf duplex: %s\n", 6061 (priv->dma_cap.half_duplex) ? "Y" : "N"); 6062 seq_printf(seq, "\tHash Filter: %s\n", 6063 (priv->dma_cap.hash_filter) ? "Y" : "N"); 6064 seq_printf(seq, "\tMultiple MAC address registers: %s\n", 6065 (priv->dma_cap.multi_addr) ? "Y" : "N"); 6066 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n", 6067 (priv->dma_cap.pcs) ? "Y" : "N"); 6068 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n", 6069 (priv->dma_cap.sma_mdio) ? "Y" : "N"); 6070 seq_printf(seq, "\tPMT Remote wake up: %s\n", 6071 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N"); 6072 seq_printf(seq, "\tPMT Magic Frame: %s\n", 6073 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N"); 6074 seq_printf(seq, "\tRMON module: %s\n", 6075 (priv->dma_cap.rmon) ? "Y" : "N"); 6076 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n", 6077 (priv->dma_cap.time_stamp) ? "Y" : "N"); 6078 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n", 6079 (priv->dma_cap.atime_stamp) ? "Y" : "N"); 6080 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n", 6081 (priv->dma_cap.eee) ? "Y" : "N"); 6082 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N"); 6083 seq_printf(seq, "\tChecksum Offload in TX: %s\n", 6084 (priv->dma_cap.tx_coe) ? "Y" : "N"); 6085 if (priv->synopsys_id >= DWMAC_CORE_4_00) { 6086 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n", 6087 (priv->dma_cap.rx_coe) ? "Y" : "N"); 6088 } else { 6089 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n", 6090 (priv->dma_cap.rx_coe_type1) ? "Y" : "N"); 6091 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n", 6092 (priv->dma_cap.rx_coe_type2) ? "Y" : "N"); 6093 } 6094 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n", 6095 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N"); 6096 seq_printf(seq, "\tNumber of Additional RX channel: %d\n", 6097 priv->dma_cap.number_rx_channel); 6098 seq_printf(seq, "\tNumber of Additional TX channel: %d\n", 6099 priv->dma_cap.number_tx_channel); 6100 seq_printf(seq, "\tNumber of Additional RX queues: %d\n", 6101 priv->dma_cap.number_rx_queues); 6102 seq_printf(seq, "\tNumber of Additional TX queues: %d\n", 6103 priv->dma_cap.number_tx_queues); 6104 seq_printf(seq, "\tEnhanced descriptors: %s\n", 6105 (priv->dma_cap.enh_desc) ? "Y" : "N"); 6106 seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size); 6107 seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size); 6108 seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz); 6109 seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N"); 6110 seq_printf(seq, "\tNumber of PPS Outputs: %d\n", 6111 priv->dma_cap.pps_out_num); 6112 seq_printf(seq, "\tSafety Features: %s\n", 6113 priv->dma_cap.asp ? "Y" : "N"); 6114 seq_printf(seq, "\tFlexible RX Parser: %s\n", 6115 priv->dma_cap.frpsel ? "Y" : "N"); 6116 seq_printf(seq, "\tEnhanced Addressing: %d\n", 6117 priv->dma_cap.addr64); 6118 seq_printf(seq, "\tReceive Side Scaling: %s\n", 6119 priv->dma_cap.rssen ? "Y" : "N"); 6120 seq_printf(seq, "\tVLAN Hash Filtering: %s\n", 6121 priv->dma_cap.vlhash ? "Y" : "N"); 6122 seq_printf(seq, "\tSplit Header: %s\n", 6123 priv->dma_cap.sphen ? "Y" : "N"); 6124 seq_printf(seq, "\tVLAN TX Insertion: %s\n", 6125 priv->dma_cap.vlins ? "Y" : "N"); 6126 seq_printf(seq, "\tDouble VLAN: %s\n", 6127 priv->dma_cap.dvlan ? "Y" : "N"); 6128 seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n", 6129 priv->dma_cap.l3l4fnum); 6130 seq_printf(seq, "\tARP Offloading: %s\n", 6131 priv->dma_cap.arpoffsel ? "Y" : "N"); 6132 seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n", 6133 priv->dma_cap.estsel ? "Y" : "N"); 6134 seq_printf(seq, "\tFrame Preemption (FPE): %s\n", 6135 priv->dma_cap.fpesel ? "Y" : "N"); 6136 seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n", 6137 priv->dma_cap.tbssel ? "Y" : "N"); 6138 return 0; 6139 } 6140 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap); 6141 6142 /* Use network device events to rename debugfs file entries. 6143 */ 6144 static int stmmac_device_event(struct notifier_block *unused, 6145 unsigned long event, void *ptr) 6146 { 6147 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 6148 struct stmmac_priv *priv = netdev_priv(dev); 6149 6150 if (dev->netdev_ops != &stmmac_netdev_ops) 6151 goto done; 6152 6153 switch (event) { 6154 case NETDEV_CHANGENAME: 6155 if (priv->dbgfs_dir) 6156 priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir, 6157 priv->dbgfs_dir, 6158 stmmac_fs_dir, 6159 dev->name); 6160 break; 6161 } 6162 done: 6163 return NOTIFY_DONE; 6164 } 6165 6166 static struct notifier_block stmmac_notifier = { 6167 .notifier_call = stmmac_device_event, 6168 }; 6169 6170 static void stmmac_init_fs(struct net_device *dev) 6171 { 6172 struct stmmac_priv *priv = netdev_priv(dev); 6173 6174 rtnl_lock(); 6175 6176 /* Create per netdev entries */ 6177 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir); 6178 6179 /* Entry to report DMA RX/TX rings */ 6180 debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev, 6181 &stmmac_rings_status_fops); 6182 6183 /* Entry to report the DMA HW features */ 6184 debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev, 6185 &stmmac_dma_cap_fops); 6186 6187 rtnl_unlock(); 6188 } 6189 6190 static void stmmac_exit_fs(struct net_device *dev) 6191 { 6192 struct stmmac_priv *priv = netdev_priv(dev); 6193 6194 debugfs_remove_recursive(priv->dbgfs_dir); 6195 } 6196 #endif /* CONFIG_DEBUG_FS */ 6197 6198 static u32 stmmac_vid_crc32_le(__le16 vid_le) 6199 { 6200 unsigned char *data = (unsigned char *)&vid_le; 6201 unsigned char data_byte = 0; 6202 u32 crc = ~0x0; 6203 u32 temp = 0; 6204 int i, bits; 6205 6206 bits = get_bitmask_order(VLAN_VID_MASK); 6207 for (i = 0; i < bits; i++) { 6208 if ((i % 8) == 0) 6209 data_byte = data[i / 8]; 6210 6211 temp = ((crc & 1) ^ data_byte) & 1; 6212 crc >>= 1; 6213 data_byte >>= 1; 6214 6215 if (temp) 6216 crc ^= 0xedb88320; 6217 } 6218 6219 return crc; 6220 } 6221 6222 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double) 6223 { 6224 u32 crc, hash = 0; 6225 __le16 pmatch = 0; 6226 int count = 0; 6227 u16 vid = 0; 6228 6229 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) { 6230 __le16 vid_le = cpu_to_le16(vid); 6231 crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28; 6232 hash |= (1 << crc); 6233 count++; 6234 } 6235 6236 if (!priv->dma_cap.vlhash) { 6237 if (count > 2) /* VID = 0 always passes filter */ 6238 return -EOPNOTSUPP; 6239 6240 pmatch = cpu_to_le16(vid); 6241 hash = 0; 6242 } 6243 6244 return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double); 6245 } 6246 6247 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid) 6248 { 6249 struct stmmac_priv *priv = netdev_priv(ndev); 6250 bool is_double = false; 6251 int ret; 6252 6253 if (be16_to_cpu(proto) == ETH_P_8021AD) 6254 is_double = true; 6255 6256 set_bit(vid, priv->active_vlans); 6257 ret = stmmac_vlan_update(priv, is_double); 6258 if (ret) { 6259 clear_bit(vid, priv->active_vlans); 6260 return ret; 6261 } 6262 6263 if (priv->hw->num_vlan) { 6264 ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid); 6265 if (ret) 6266 return ret; 6267 } 6268 6269 return 0; 6270 } 6271 6272 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid) 6273 { 6274 struct stmmac_priv *priv = netdev_priv(ndev); 6275 bool is_double = false; 6276 int ret; 6277 6278 ret = pm_runtime_get_sync(priv->device); 6279 if (ret < 0) { 6280 pm_runtime_put_noidle(priv->device); 6281 return ret; 6282 } 6283 6284 if (be16_to_cpu(proto) == ETH_P_8021AD) 6285 is_double = true; 6286 6287 clear_bit(vid, priv->active_vlans); 6288 6289 if (priv->hw->num_vlan) { 6290 ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid); 6291 if (ret) 6292 goto del_vlan_error; 6293 } 6294 6295 ret = stmmac_vlan_update(priv, is_double); 6296 6297 del_vlan_error: 6298 pm_runtime_put(priv->device); 6299 6300 return ret; 6301 } 6302 6303 static int stmmac_bpf(struct net_device *dev, struct netdev_bpf *bpf) 6304 { 6305 struct stmmac_priv *priv = netdev_priv(dev); 6306 6307 switch (bpf->command) { 6308 case XDP_SETUP_PROG: 6309 return stmmac_xdp_set_prog(priv, bpf->prog, bpf->extack); 6310 case XDP_SETUP_XSK_POOL: 6311 return stmmac_xdp_setup_pool(priv, bpf->xsk.pool, 6312 bpf->xsk.queue_id); 6313 default: 6314 return -EOPNOTSUPP; 6315 } 6316 } 6317 6318 static int stmmac_xdp_xmit(struct net_device *dev, int num_frames, 6319 struct xdp_frame **frames, u32 flags) 6320 { 6321 struct stmmac_priv *priv = netdev_priv(dev); 6322 int cpu = smp_processor_id(); 6323 struct netdev_queue *nq; 6324 int i, nxmit = 0; 6325 int queue; 6326 6327 if (unlikely(test_bit(STMMAC_DOWN, &priv->state))) 6328 return -ENETDOWN; 6329 6330 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 6331 return -EINVAL; 6332 6333 queue = stmmac_xdp_get_tx_queue(priv, cpu); 6334 nq = netdev_get_tx_queue(priv->dev, queue); 6335 6336 __netif_tx_lock(nq, cpu); 6337 /* Avoids TX time-out as we are sharing with slow path */ 6338 nq->trans_start = jiffies; 6339 6340 for (i = 0; i < num_frames; i++) { 6341 int res; 6342 6343 res = stmmac_xdp_xmit_xdpf(priv, queue, frames[i], true); 6344 if (res == STMMAC_XDP_CONSUMED) 6345 break; 6346 6347 nxmit++; 6348 } 6349 6350 if (flags & XDP_XMIT_FLUSH) { 6351 stmmac_flush_tx_descriptors(priv, queue); 6352 stmmac_tx_timer_arm(priv, queue); 6353 } 6354 6355 __netif_tx_unlock(nq); 6356 6357 return nxmit; 6358 } 6359 6360 void stmmac_disable_rx_queue(struct stmmac_priv *priv, u32 queue) 6361 { 6362 struct stmmac_channel *ch = &priv->channel[queue]; 6363 unsigned long flags; 6364 6365 spin_lock_irqsave(&ch->lock, flags); 6366 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 1, 0); 6367 spin_unlock_irqrestore(&ch->lock, flags); 6368 6369 stmmac_stop_rx_dma(priv, queue); 6370 __free_dma_rx_desc_resources(priv, queue); 6371 } 6372 6373 void stmmac_enable_rx_queue(struct stmmac_priv *priv, u32 queue) 6374 { 6375 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 6376 struct stmmac_channel *ch = &priv->channel[queue]; 6377 unsigned long flags; 6378 u32 buf_size; 6379 int ret; 6380 6381 ret = __alloc_dma_rx_desc_resources(priv, queue); 6382 if (ret) { 6383 netdev_err(priv->dev, "Failed to alloc RX desc.\n"); 6384 return; 6385 } 6386 6387 ret = __init_dma_rx_desc_rings(priv, queue, GFP_KERNEL); 6388 if (ret) { 6389 __free_dma_rx_desc_resources(priv, queue); 6390 netdev_err(priv->dev, "Failed to init RX desc.\n"); 6391 return; 6392 } 6393 6394 stmmac_clear_rx_descriptors(priv, queue); 6395 6396 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg, 6397 rx_q->dma_rx_phy, rx_q->queue_index); 6398 6399 rx_q->rx_tail_addr = rx_q->dma_rx_phy + (rx_q->buf_alloc_num * 6400 sizeof(struct dma_desc)); 6401 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, 6402 rx_q->rx_tail_addr, rx_q->queue_index); 6403 6404 if (rx_q->xsk_pool && rx_q->buf_alloc_num) { 6405 buf_size = xsk_pool_get_rx_frame_size(rx_q->xsk_pool); 6406 stmmac_set_dma_bfsize(priv, priv->ioaddr, 6407 buf_size, 6408 rx_q->queue_index); 6409 } else { 6410 stmmac_set_dma_bfsize(priv, priv->ioaddr, 6411 priv->dma_buf_sz, 6412 rx_q->queue_index); 6413 } 6414 6415 stmmac_start_rx_dma(priv, queue); 6416 6417 spin_lock_irqsave(&ch->lock, flags); 6418 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 1, 0); 6419 spin_unlock_irqrestore(&ch->lock, flags); 6420 } 6421 6422 void stmmac_disable_tx_queue(struct stmmac_priv *priv, u32 queue) 6423 { 6424 struct stmmac_channel *ch = &priv->channel[queue]; 6425 unsigned long flags; 6426 6427 spin_lock_irqsave(&ch->lock, flags); 6428 stmmac_disable_dma_irq(priv, priv->ioaddr, queue, 0, 1); 6429 spin_unlock_irqrestore(&ch->lock, flags); 6430 6431 stmmac_stop_tx_dma(priv, queue); 6432 __free_dma_tx_desc_resources(priv, queue); 6433 } 6434 6435 void stmmac_enable_tx_queue(struct stmmac_priv *priv, u32 queue) 6436 { 6437 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 6438 struct stmmac_channel *ch = &priv->channel[queue]; 6439 unsigned long flags; 6440 int ret; 6441 6442 ret = __alloc_dma_tx_desc_resources(priv, queue); 6443 if (ret) { 6444 netdev_err(priv->dev, "Failed to alloc TX desc.\n"); 6445 return; 6446 } 6447 6448 ret = __init_dma_tx_desc_rings(priv, queue); 6449 if (ret) { 6450 __free_dma_tx_desc_resources(priv, queue); 6451 netdev_err(priv->dev, "Failed to init TX desc.\n"); 6452 return; 6453 } 6454 6455 stmmac_clear_tx_descriptors(priv, queue); 6456 6457 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg, 6458 tx_q->dma_tx_phy, tx_q->queue_index); 6459 6460 if (tx_q->tbs & STMMAC_TBS_AVAIL) 6461 stmmac_enable_tbs(priv, priv->ioaddr, 1, tx_q->queue_index); 6462 6463 tx_q->tx_tail_addr = tx_q->dma_tx_phy; 6464 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, 6465 tx_q->tx_tail_addr, tx_q->queue_index); 6466 6467 stmmac_start_tx_dma(priv, queue); 6468 6469 spin_lock_irqsave(&ch->lock, flags); 6470 stmmac_enable_dma_irq(priv, priv->ioaddr, queue, 0, 1); 6471 spin_unlock_irqrestore(&ch->lock, flags); 6472 } 6473 6474 int stmmac_xsk_wakeup(struct net_device *dev, u32 queue, u32 flags) 6475 { 6476 struct stmmac_priv *priv = netdev_priv(dev); 6477 struct stmmac_rx_queue *rx_q; 6478 struct stmmac_tx_queue *tx_q; 6479 struct stmmac_channel *ch; 6480 6481 if (test_bit(STMMAC_DOWN, &priv->state) || 6482 !netif_carrier_ok(priv->dev)) 6483 return -ENETDOWN; 6484 6485 if (!stmmac_xdp_is_enabled(priv)) 6486 return -ENXIO; 6487 6488 if (queue >= priv->plat->rx_queues_to_use || 6489 queue >= priv->plat->tx_queues_to_use) 6490 return -EINVAL; 6491 6492 rx_q = &priv->rx_queue[queue]; 6493 tx_q = &priv->tx_queue[queue]; 6494 ch = &priv->channel[queue]; 6495 6496 if (!rx_q->xsk_pool && !tx_q->xsk_pool) 6497 return -ENXIO; 6498 6499 if (!napi_if_scheduled_mark_missed(&ch->rxtx_napi)) { 6500 /* EQoS does not have per-DMA channel SW interrupt, 6501 * so we schedule RX Napi straight-away. 6502 */ 6503 if (likely(napi_schedule_prep(&ch->rxtx_napi))) 6504 __napi_schedule(&ch->rxtx_napi); 6505 } 6506 6507 return 0; 6508 } 6509 6510 static const struct net_device_ops stmmac_netdev_ops = { 6511 .ndo_open = stmmac_open, 6512 .ndo_start_xmit = stmmac_xmit, 6513 .ndo_stop = stmmac_release, 6514 .ndo_change_mtu = stmmac_change_mtu, 6515 .ndo_fix_features = stmmac_fix_features, 6516 .ndo_set_features = stmmac_set_features, 6517 .ndo_set_rx_mode = stmmac_set_rx_mode, 6518 .ndo_tx_timeout = stmmac_tx_timeout, 6519 .ndo_eth_ioctl = stmmac_ioctl, 6520 .ndo_setup_tc = stmmac_setup_tc, 6521 .ndo_select_queue = stmmac_select_queue, 6522 #ifdef CONFIG_NET_POLL_CONTROLLER 6523 .ndo_poll_controller = stmmac_poll_controller, 6524 #endif 6525 .ndo_set_mac_address = stmmac_set_mac_address, 6526 .ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid, 6527 .ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid, 6528 .ndo_bpf = stmmac_bpf, 6529 .ndo_xdp_xmit = stmmac_xdp_xmit, 6530 .ndo_xsk_wakeup = stmmac_xsk_wakeup, 6531 }; 6532 6533 static void stmmac_reset_subtask(struct stmmac_priv *priv) 6534 { 6535 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state)) 6536 return; 6537 if (test_bit(STMMAC_DOWN, &priv->state)) 6538 return; 6539 6540 netdev_err(priv->dev, "Reset adapter.\n"); 6541 6542 rtnl_lock(); 6543 netif_trans_update(priv->dev); 6544 while (test_and_set_bit(STMMAC_RESETING, &priv->state)) 6545 usleep_range(1000, 2000); 6546 6547 set_bit(STMMAC_DOWN, &priv->state); 6548 dev_close(priv->dev); 6549 dev_open(priv->dev, NULL); 6550 clear_bit(STMMAC_DOWN, &priv->state); 6551 clear_bit(STMMAC_RESETING, &priv->state); 6552 rtnl_unlock(); 6553 } 6554 6555 static void stmmac_service_task(struct work_struct *work) 6556 { 6557 struct stmmac_priv *priv = container_of(work, struct stmmac_priv, 6558 service_task); 6559 6560 stmmac_reset_subtask(priv); 6561 clear_bit(STMMAC_SERVICE_SCHED, &priv->state); 6562 } 6563 6564 /** 6565 * stmmac_hw_init - Init the MAC device 6566 * @priv: driver private structure 6567 * Description: this function is to configure the MAC device according to 6568 * some platform parameters or the HW capability register. It prepares the 6569 * driver to use either ring or chain modes and to setup either enhanced or 6570 * normal descriptors. 6571 */ 6572 static int stmmac_hw_init(struct stmmac_priv *priv) 6573 { 6574 int ret; 6575 6576 /* dwmac-sun8i only work in chain mode */ 6577 if (priv->plat->has_sun8i) 6578 chain_mode = 1; 6579 priv->chain_mode = chain_mode; 6580 6581 /* Initialize HW Interface */ 6582 ret = stmmac_hwif_init(priv); 6583 if (ret) 6584 return ret; 6585 6586 /* Get the HW capability (new GMAC newer than 3.50a) */ 6587 priv->hw_cap_support = stmmac_get_hw_features(priv); 6588 if (priv->hw_cap_support) { 6589 dev_info(priv->device, "DMA HW capability register supported\n"); 6590 6591 /* We can override some gmac/dma configuration fields: e.g. 6592 * enh_desc, tx_coe (e.g. that are passed through the 6593 * platform) with the values from the HW capability 6594 * register (if supported). 6595 */ 6596 priv->plat->enh_desc = priv->dma_cap.enh_desc; 6597 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up && 6598 !priv->plat->use_phy_wol; 6599 priv->hw->pmt = priv->plat->pmt; 6600 if (priv->dma_cap.hash_tb_sz) { 6601 priv->hw->multicast_filter_bins = 6602 (BIT(priv->dma_cap.hash_tb_sz) << 5); 6603 priv->hw->mcast_bits_log2 = 6604 ilog2(priv->hw->multicast_filter_bins); 6605 } 6606 6607 /* TXCOE doesn't work in thresh DMA mode */ 6608 if (priv->plat->force_thresh_dma_mode) 6609 priv->plat->tx_coe = 0; 6610 else 6611 priv->plat->tx_coe = priv->dma_cap.tx_coe; 6612 6613 /* In case of GMAC4 rx_coe is from HW cap register. */ 6614 priv->plat->rx_coe = priv->dma_cap.rx_coe; 6615 6616 if (priv->dma_cap.rx_coe_type2) 6617 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2; 6618 else if (priv->dma_cap.rx_coe_type1) 6619 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1; 6620 6621 } else { 6622 dev_info(priv->device, "No HW DMA feature register supported\n"); 6623 } 6624 6625 if (priv->plat->rx_coe) { 6626 priv->hw->rx_csum = priv->plat->rx_coe; 6627 dev_info(priv->device, "RX Checksum Offload Engine supported\n"); 6628 if (priv->synopsys_id < DWMAC_CORE_4_00) 6629 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum); 6630 } 6631 if (priv->plat->tx_coe) 6632 dev_info(priv->device, "TX Checksum insertion supported\n"); 6633 6634 if (priv->plat->pmt) { 6635 dev_info(priv->device, "Wake-Up On Lan supported\n"); 6636 device_set_wakeup_capable(priv->device, 1); 6637 } 6638 6639 if (priv->dma_cap.tsoen) 6640 dev_info(priv->device, "TSO supported\n"); 6641 6642 priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en; 6643 priv->hw->vlan_fail_q = priv->plat->vlan_fail_q; 6644 6645 /* Run HW quirks, if any */ 6646 if (priv->hwif_quirks) { 6647 ret = priv->hwif_quirks(priv); 6648 if (ret) 6649 return ret; 6650 } 6651 6652 /* Rx Watchdog is available in the COREs newer than the 3.40. 6653 * In some case, for example on bugged HW this feature 6654 * has to be disable and this can be done by passing the 6655 * riwt_off field from the platform. 6656 */ 6657 if (((priv->synopsys_id >= DWMAC_CORE_3_50) || 6658 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) { 6659 priv->use_riwt = 1; 6660 dev_info(priv->device, 6661 "Enable RX Mitigation via HW Watchdog Timer\n"); 6662 } 6663 6664 return 0; 6665 } 6666 6667 static void stmmac_napi_add(struct net_device *dev) 6668 { 6669 struct stmmac_priv *priv = netdev_priv(dev); 6670 u32 queue, maxq; 6671 6672 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use); 6673 6674 for (queue = 0; queue < maxq; queue++) { 6675 struct stmmac_channel *ch = &priv->channel[queue]; 6676 6677 ch->priv_data = priv; 6678 ch->index = queue; 6679 spin_lock_init(&ch->lock); 6680 6681 if (queue < priv->plat->rx_queues_to_use) { 6682 netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx, 6683 NAPI_POLL_WEIGHT); 6684 } 6685 if (queue < priv->plat->tx_queues_to_use) { 6686 netif_tx_napi_add(dev, &ch->tx_napi, 6687 stmmac_napi_poll_tx, 6688 NAPI_POLL_WEIGHT); 6689 } 6690 if (queue < priv->plat->rx_queues_to_use && 6691 queue < priv->plat->tx_queues_to_use) { 6692 netif_napi_add(dev, &ch->rxtx_napi, 6693 stmmac_napi_poll_rxtx, 6694 NAPI_POLL_WEIGHT); 6695 } 6696 } 6697 } 6698 6699 static void stmmac_napi_del(struct net_device *dev) 6700 { 6701 struct stmmac_priv *priv = netdev_priv(dev); 6702 u32 queue, maxq; 6703 6704 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use); 6705 6706 for (queue = 0; queue < maxq; queue++) { 6707 struct stmmac_channel *ch = &priv->channel[queue]; 6708 6709 if (queue < priv->plat->rx_queues_to_use) 6710 netif_napi_del(&ch->rx_napi); 6711 if (queue < priv->plat->tx_queues_to_use) 6712 netif_napi_del(&ch->tx_napi); 6713 if (queue < priv->plat->rx_queues_to_use && 6714 queue < priv->plat->tx_queues_to_use) { 6715 netif_napi_del(&ch->rxtx_napi); 6716 } 6717 } 6718 } 6719 6720 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt) 6721 { 6722 struct stmmac_priv *priv = netdev_priv(dev); 6723 int ret = 0; 6724 6725 if (netif_running(dev)) 6726 stmmac_release(dev); 6727 6728 stmmac_napi_del(dev); 6729 6730 priv->plat->rx_queues_to_use = rx_cnt; 6731 priv->plat->tx_queues_to_use = tx_cnt; 6732 6733 stmmac_napi_add(dev); 6734 6735 if (netif_running(dev)) 6736 ret = stmmac_open(dev); 6737 6738 return ret; 6739 } 6740 6741 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size) 6742 { 6743 struct stmmac_priv *priv = netdev_priv(dev); 6744 int ret = 0; 6745 6746 if (netif_running(dev)) 6747 stmmac_release(dev); 6748 6749 priv->dma_rx_size = rx_size; 6750 priv->dma_tx_size = tx_size; 6751 6752 if (netif_running(dev)) 6753 ret = stmmac_open(dev); 6754 6755 return ret; 6756 } 6757 6758 #define SEND_VERIFY_MPAKCET_FMT "Send Verify mPacket lo_state=%d lp_state=%d\n" 6759 static void stmmac_fpe_lp_task(struct work_struct *work) 6760 { 6761 struct stmmac_priv *priv = container_of(work, struct stmmac_priv, 6762 fpe_task); 6763 struct stmmac_fpe_cfg *fpe_cfg = priv->plat->fpe_cfg; 6764 enum stmmac_fpe_state *lo_state = &fpe_cfg->lo_fpe_state; 6765 enum stmmac_fpe_state *lp_state = &fpe_cfg->lp_fpe_state; 6766 bool *hs_enable = &fpe_cfg->hs_enable; 6767 bool *enable = &fpe_cfg->enable; 6768 int retries = 20; 6769 6770 while (retries-- > 0) { 6771 /* Bail out immediately if FPE handshake is OFF */ 6772 if (*lo_state == FPE_STATE_OFF || !*hs_enable) 6773 break; 6774 6775 if (*lo_state == FPE_STATE_ENTERING_ON && 6776 *lp_state == FPE_STATE_ENTERING_ON) { 6777 stmmac_fpe_configure(priv, priv->ioaddr, 6778 priv->plat->tx_queues_to_use, 6779 priv->plat->rx_queues_to_use, 6780 *enable); 6781 6782 netdev_info(priv->dev, "configured FPE\n"); 6783 6784 *lo_state = FPE_STATE_ON; 6785 *lp_state = FPE_STATE_ON; 6786 netdev_info(priv->dev, "!!! BOTH FPE stations ON\n"); 6787 break; 6788 } 6789 6790 if ((*lo_state == FPE_STATE_CAPABLE || 6791 *lo_state == FPE_STATE_ENTERING_ON) && 6792 *lp_state != FPE_STATE_ON) { 6793 netdev_info(priv->dev, SEND_VERIFY_MPAKCET_FMT, 6794 *lo_state, *lp_state); 6795 stmmac_fpe_send_mpacket(priv, priv->ioaddr, 6796 MPACKET_VERIFY); 6797 } 6798 /* Sleep then retry */ 6799 msleep(500); 6800 } 6801 6802 clear_bit(__FPE_TASK_SCHED, &priv->fpe_task_state); 6803 } 6804 6805 void stmmac_fpe_handshake(struct stmmac_priv *priv, bool enable) 6806 { 6807 if (priv->plat->fpe_cfg->hs_enable != enable) { 6808 if (enable) { 6809 stmmac_fpe_send_mpacket(priv, priv->ioaddr, 6810 MPACKET_VERIFY); 6811 } else { 6812 priv->plat->fpe_cfg->lo_fpe_state = FPE_STATE_OFF; 6813 priv->plat->fpe_cfg->lp_fpe_state = FPE_STATE_OFF; 6814 } 6815 6816 priv->plat->fpe_cfg->hs_enable = enable; 6817 } 6818 } 6819 6820 /** 6821 * stmmac_dvr_probe 6822 * @device: device pointer 6823 * @plat_dat: platform data pointer 6824 * @res: stmmac resource pointer 6825 * Description: this is the main probe function used to 6826 * call the alloc_etherdev, allocate the priv structure. 6827 * Return: 6828 * returns 0 on success, otherwise errno. 6829 */ 6830 int stmmac_dvr_probe(struct device *device, 6831 struct plat_stmmacenet_data *plat_dat, 6832 struct stmmac_resources *res) 6833 { 6834 struct net_device *ndev = NULL; 6835 struct stmmac_priv *priv; 6836 u32 rxq; 6837 int i, ret = 0; 6838 6839 ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv), 6840 MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES); 6841 if (!ndev) 6842 return -ENOMEM; 6843 6844 SET_NETDEV_DEV(ndev, device); 6845 6846 priv = netdev_priv(ndev); 6847 priv->device = device; 6848 priv->dev = ndev; 6849 6850 stmmac_set_ethtool_ops(ndev); 6851 priv->pause = pause; 6852 priv->plat = plat_dat; 6853 priv->ioaddr = res->addr; 6854 priv->dev->base_addr = (unsigned long)res->addr; 6855 priv->plat->dma_cfg->multi_msi_en = priv->plat->multi_msi_en; 6856 6857 priv->dev->irq = res->irq; 6858 priv->wol_irq = res->wol_irq; 6859 priv->lpi_irq = res->lpi_irq; 6860 priv->sfty_ce_irq = res->sfty_ce_irq; 6861 priv->sfty_ue_irq = res->sfty_ue_irq; 6862 for (i = 0; i < MTL_MAX_RX_QUEUES; i++) 6863 priv->rx_irq[i] = res->rx_irq[i]; 6864 for (i = 0; i < MTL_MAX_TX_QUEUES; i++) 6865 priv->tx_irq[i] = res->tx_irq[i]; 6866 6867 if (!is_zero_ether_addr(res->mac)) 6868 eth_hw_addr_set(priv->dev, res->mac); 6869 6870 dev_set_drvdata(device, priv->dev); 6871 6872 /* Verify driver arguments */ 6873 stmmac_verify_args(); 6874 6875 priv->af_xdp_zc_qps = bitmap_zalloc(MTL_MAX_TX_QUEUES, GFP_KERNEL); 6876 if (!priv->af_xdp_zc_qps) 6877 return -ENOMEM; 6878 6879 /* Allocate workqueue */ 6880 priv->wq = create_singlethread_workqueue("stmmac_wq"); 6881 if (!priv->wq) { 6882 dev_err(priv->device, "failed to create workqueue\n"); 6883 return -ENOMEM; 6884 } 6885 6886 INIT_WORK(&priv->service_task, stmmac_service_task); 6887 6888 /* Initialize Link Partner FPE workqueue */ 6889 INIT_WORK(&priv->fpe_task, stmmac_fpe_lp_task); 6890 6891 /* Override with kernel parameters if supplied XXX CRS XXX 6892 * this needs to have multiple instances 6893 */ 6894 if ((phyaddr >= 0) && (phyaddr <= 31)) 6895 priv->plat->phy_addr = phyaddr; 6896 6897 if (priv->plat->stmmac_rst) { 6898 ret = reset_control_assert(priv->plat->stmmac_rst); 6899 reset_control_deassert(priv->plat->stmmac_rst); 6900 /* Some reset controllers have only reset callback instead of 6901 * assert + deassert callbacks pair. 6902 */ 6903 if (ret == -ENOTSUPP) 6904 reset_control_reset(priv->plat->stmmac_rst); 6905 } 6906 6907 ret = reset_control_deassert(priv->plat->stmmac_ahb_rst); 6908 if (ret == -ENOTSUPP) 6909 dev_err(priv->device, "unable to bring out of ahb reset: %pe\n", 6910 ERR_PTR(ret)); 6911 6912 /* Init MAC and get the capabilities */ 6913 ret = stmmac_hw_init(priv); 6914 if (ret) 6915 goto error_hw_init; 6916 6917 /* Only DWMAC core version 5.20 onwards supports HW descriptor prefetch. 6918 */ 6919 if (priv->synopsys_id < DWMAC_CORE_5_20) 6920 priv->plat->dma_cfg->dche = false; 6921 6922 stmmac_check_ether_addr(priv); 6923 6924 ndev->netdev_ops = &stmmac_netdev_ops; 6925 6926 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 6927 NETIF_F_RXCSUM; 6928 6929 ret = stmmac_tc_init(priv, priv); 6930 if (!ret) { 6931 ndev->hw_features |= NETIF_F_HW_TC; 6932 } 6933 6934 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) { 6935 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6; 6936 if (priv->plat->has_gmac4) 6937 ndev->hw_features |= NETIF_F_GSO_UDP_L4; 6938 priv->tso = true; 6939 dev_info(priv->device, "TSO feature enabled\n"); 6940 } 6941 6942 if (priv->dma_cap.sphen) { 6943 ndev->hw_features |= NETIF_F_GRO; 6944 priv->sph_cap = true; 6945 priv->sph = priv->sph_cap; 6946 dev_info(priv->device, "SPH feature enabled\n"); 6947 } 6948 6949 /* The current IP register MAC_HW_Feature1[ADDR64] only define 6950 * 32/40/64 bit width, but some SOC support others like i.MX8MP 6951 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64]. 6952 * So overwrite dma_cap.addr64 according to HW real design. 6953 */ 6954 if (priv->plat->addr64) 6955 priv->dma_cap.addr64 = priv->plat->addr64; 6956 6957 if (priv->dma_cap.addr64) { 6958 ret = dma_set_mask_and_coherent(device, 6959 DMA_BIT_MASK(priv->dma_cap.addr64)); 6960 if (!ret) { 6961 dev_info(priv->device, "Using %d bits DMA width\n", 6962 priv->dma_cap.addr64); 6963 6964 /* 6965 * If more than 32 bits can be addressed, make sure to 6966 * enable enhanced addressing mode. 6967 */ 6968 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT)) 6969 priv->plat->dma_cfg->eame = true; 6970 } else { 6971 ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32)); 6972 if (ret) { 6973 dev_err(priv->device, "Failed to set DMA Mask\n"); 6974 goto error_hw_init; 6975 } 6976 6977 priv->dma_cap.addr64 = 32; 6978 } 6979 } 6980 6981 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA; 6982 ndev->watchdog_timeo = msecs_to_jiffies(watchdog); 6983 #ifdef STMMAC_VLAN_TAG_USED 6984 /* Both mac100 and gmac support receive VLAN tag detection */ 6985 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX; 6986 if (priv->dma_cap.vlhash) { 6987 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 6988 ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER; 6989 } 6990 if (priv->dma_cap.vlins) { 6991 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; 6992 if (priv->dma_cap.dvlan) 6993 ndev->features |= NETIF_F_HW_VLAN_STAG_TX; 6994 } 6995 #endif 6996 priv->msg_enable = netif_msg_init(debug, default_msg_level); 6997 6998 /* Initialize RSS */ 6999 rxq = priv->plat->rx_queues_to_use; 7000 netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key)); 7001 for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++) 7002 priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq); 7003 7004 if (priv->dma_cap.rssen && priv->plat->rss_en) 7005 ndev->features |= NETIF_F_RXHASH; 7006 7007 /* MTU range: 46 - hw-specific max */ 7008 ndev->min_mtu = ETH_ZLEN - ETH_HLEN; 7009 if (priv->plat->has_xgmac) 7010 ndev->max_mtu = XGMAC_JUMBO_LEN; 7011 else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00)) 7012 ndev->max_mtu = JUMBO_LEN; 7013 else 7014 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN); 7015 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu 7016 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range. 7017 */ 7018 if ((priv->plat->maxmtu < ndev->max_mtu) && 7019 (priv->plat->maxmtu >= ndev->min_mtu)) 7020 ndev->max_mtu = priv->plat->maxmtu; 7021 else if (priv->plat->maxmtu < ndev->min_mtu) 7022 dev_warn(priv->device, 7023 "%s: warning: maxmtu having invalid value (%d)\n", 7024 __func__, priv->plat->maxmtu); 7025 7026 if (flow_ctrl) 7027 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */ 7028 7029 /* Setup channels NAPI */ 7030 stmmac_napi_add(ndev); 7031 7032 mutex_init(&priv->lock); 7033 7034 /* If a specific clk_csr value is passed from the platform 7035 * this means that the CSR Clock Range selection cannot be 7036 * changed at run-time and it is fixed. Viceversa the driver'll try to 7037 * set the MDC clock dynamically according to the csr actual 7038 * clock input. 7039 */ 7040 if (priv->plat->clk_csr >= 0) 7041 priv->clk_csr = priv->plat->clk_csr; 7042 else 7043 stmmac_clk_csr_set(priv); 7044 7045 stmmac_check_pcs_mode(priv); 7046 7047 pm_runtime_get_noresume(device); 7048 pm_runtime_set_active(device); 7049 pm_runtime_enable(device); 7050 7051 if (priv->hw->pcs != STMMAC_PCS_TBI && 7052 priv->hw->pcs != STMMAC_PCS_RTBI) { 7053 /* MDIO bus Registration */ 7054 ret = stmmac_mdio_register(ndev); 7055 if (ret < 0) { 7056 dev_err(priv->device, 7057 "%s: MDIO bus (id: %d) registration failed", 7058 __func__, priv->plat->bus_id); 7059 goto error_mdio_register; 7060 } 7061 } 7062 7063 if (priv->plat->speed_mode_2500) 7064 priv->plat->speed_mode_2500(ndev, priv->plat->bsp_priv); 7065 7066 if (priv->plat->mdio_bus_data && priv->plat->mdio_bus_data->has_xpcs) { 7067 ret = stmmac_xpcs_setup(priv->mii); 7068 if (ret) 7069 goto error_xpcs_setup; 7070 } 7071 7072 ret = stmmac_phy_setup(priv); 7073 if (ret) { 7074 netdev_err(ndev, "failed to setup phy (%d)\n", ret); 7075 goto error_phy_setup; 7076 } 7077 7078 ret = register_netdev(ndev); 7079 if (ret) { 7080 dev_err(priv->device, "%s: ERROR %i registering the device\n", 7081 __func__, ret); 7082 goto error_netdev_register; 7083 } 7084 7085 if (priv->plat->serdes_powerup) { 7086 ret = priv->plat->serdes_powerup(ndev, 7087 priv->plat->bsp_priv); 7088 7089 if (ret < 0) 7090 goto error_serdes_powerup; 7091 } 7092 7093 #ifdef CONFIG_DEBUG_FS 7094 stmmac_init_fs(ndev); 7095 #endif 7096 7097 /* Let pm_runtime_put() disable the clocks. 7098 * If CONFIG_PM is not enabled, the clocks will stay powered. 7099 */ 7100 pm_runtime_put(device); 7101 7102 return ret; 7103 7104 error_serdes_powerup: 7105 unregister_netdev(ndev); 7106 error_netdev_register: 7107 phylink_destroy(priv->phylink); 7108 error_xpcs_setup: 7109 error_phy_setup: 7110 if (priv->hw->pcs != STMMAC_PCS_TBI && 7111 priv->hw->pcs != STMMAC_PCS_RTBI) 7112 stmmac_mdio_unregister(ndev); 7113 error_mdio_register: 7114 stmmac_napi_del(ndev); 7115 error_hw_init: 7116 destroy_workqueue(priv->wq); 7117 bitmap_free(priv->af_xdp_zc_qps); 7118 7119 return ret; 7120 } 7121 EXPORT_SYMBOL_GPL(stmmac_dvr_probe); 7122 7123 /** 7124 * stmmac_dvr_remove 7125 * @dev: device pointer 7126 * Description: this function resets the TX/RX processes, disables the MAC RX/TX 7127 * changes the link status, releases the DMA descriptor rings. 7128 */ 7129 int stmmac_dvr_remove(struct device *dev) 7130 { 7131 struct net_device *ndev = dev_get_drvdata(dev); 7132 struct stmmac_priv *priv = netdev_priv(ndev); 7133 7134 netdev_info(priv->dev, "%s: removing driver", __func__); 7135 7136 stmmac_stop_all_dma(priv); 7137 stmmac_mac_set(priv, priv->ioaddr, false); 7138 netif_carrier_off(ndev); 7139 unregister_netdev(ndev); 7140 7141 /* Serdes power down needs to happen after VLAN filter 7142 * is deleted that is triggered by unregister_netdev(). 7143 */ 7144 if (priv->plat->serdes_powerdown) 7145 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv); 7146 7147 #ifdef CONFIG_DEBUG_FS 7148 stmmac_exit_fs(ndev); 7149 #endif 7150 phylink_destroy(priv->phylink); 7151 if (priv->plat->stmmac_rst) 7152 reset_control_assert(priv->plat->stmmac_rst); 7153 reset_control_assert(priv->plat->stmmac_ahb_rst); 7154 pm_runtime_put(dev); 7155 pm_runtime_disable(dev); 7156 if (priv->hw->pcs != STMMAC_PCS_TBI && 7157 priv->hw->pcs != STMMAC_PCS_RTBI) 7158 stmmac_mdio_unregister(ndev); 7159 destroy_workqueue(priv->wq); 7160 mutex_destroy(&priv->lock); 7161 bitmap_free(priv->af_xdp_zc_qps); 7162 7163 return 0; 7164 } 7165 EXPORT_SYMBOL_GPL(stmmac_dvr_remove); 7166 7167 /** 7168 * stmmac_suspend - suspend callback 7169 * @dev: device pointer 7170 * Description: this is the function to suspend the device and it is called 7171 * by the platform driver to stop the network queue, release the resources, 7172 * program the PMT register (for WoL), clean and release driver resources. 7173 */ 7174 int stmmac_suspend(struct device *dev) 7175 { 7176 struct net_device *ndev = dev_get_drvdata(dev); 7177 struct stmmac_priv *priv = netdev_priv(ndev); 7178 u32 chan; 7179 7180 if (!ndev || !netif_running(ndev)) 7181 return 0; 7182 7183 mutex_lock(&priv->lock); 7184 7185 netif_device_detach(ndev); 7186 7187 stmmac_disable_all_queues(priv); 7188 7189 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) 7190 hrtimer_cancel(&priv->tx_queue[chan].txtimer); 7191 7192 if (priv->eee_enabled) { 7193 priv->tx_path_in_lpi_mode = false; 7194 del_timer_sync(&priv->eee_ctrl_timer); 7195 } 7196 7197 /* Stop TX/RX DMA */ 7198 stmmac_stop_all_dma(priv); 7199 7200 if (priv->plat->serdes_powerdown) 7201 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv); 7202 7203 /* Enable Power down mode by programming the PMT regs */ 7204 if (device_may_wakeup(priv->device) && priv->plat->pmt) { 7205 stmmac_pmt(priv, priv->hw, priv->wolopts); 7206 priv->irq_wake = 1; 7207 } else { 7208 stmmac_mac_set(priv, priv->ioaddr, false); 7209 pinctrl_pm_select_sleep_state(priv->device); 7210 } 7211 7212 mutex_unlock(&priv->lock); 7213 7214 rtnl_lock(); 7215 if (device_may_wakeup(priv->device) && priv->plat->pmt) { 7216 phylink_suspend(priv->phylink, true); 7217 } else { 7218 if (device_may_wakeup(priv->device)) 7219 phylink_speed_down(priv->phylink, false); 7220 phylink_suspend(priv->phylink, false); 7221 } 7222 rtnl_unlock(); 7223 7224 if (priv->dma_cap.fpesel) { 7225 /* Disable FPE */ 7226 stmmac_fpe_configure(priv, priv->ioaddr, 7227 priv->plat->tx_queues_to_use, 7228 priv->plat->rx_queues_to_use, false); 7229 7230 stmmac_fpe_handshake(priv, false); 7231 stmmac_fpe_stop_wq(priv); 7232 } 7233 7234 priv->speed = SPEED_UNKNOWN; 7235 return 0; 7236 } 7237 EXPORT_SYMBOL_GPL(stmmac_suspend); 7238 7239 /** 7240 * stmmac_reset_queues_param - reset queue parameters 7241 * @priv: device pointer 7242 */ 7243 static void stmmac_reset_queues_param(struct stmmac_priv *priv) 7244 { 7245 u32 rx_cnt = priv->plat->rx_queues_to_use; 7246 u32 tx_cnt = priv->plat->tx_queues_to_use; 7247 u32 queue; 7248 7249 for (queue = 0; queue < rx_cnt; queue++) { 7250 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 7251 7252 rx_q->cur_rx = 0; 7253 rx_q->dirty_rx = 0; 7254 } 7255 7256 for (queue = 0; queue < tx_cnt; queue++) { 7257 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 7258 7259 tx_q->cur_tx = 0; 7260 tx_q->dirty_tx = 0; 7261 tx_q->mss = 0; 7262 7263 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue)); 7264 } 7265 } 7266 7267 /** 7268 * stmmac_resume - resume callback 7269 * @dev: device pointer 7270 * Description: when resume this function is invoked to setup the DMA and CORE 7271 * in a usable state. 7272 */ 7273 int stmmac_resume(struct device *dev) 7274 { 7275 struct net_device *ndev = dev_get_drvdata(dev); 7276 struct stmmac_priv *priv = netdev_priv(ndev); 7277 int ret; 7278 7279 if (!netif_running(ndev)) 7280 return 0; 7281 7282 /* Power Down bit, into the PM register, is cleared 7283 * automatically as soon as a magic packet or a Wake-up frame 7284 * is received. Anyway, it's better to manually clear 7285 * this bit because it can generate problems while resuming 7286 * from another devices (e.g. serial console). 7287 */ 7288 if (device_may_wakeup(priv->device) && priv->plat->pmt) { 7289 mutex_lock(&priv->lock); 7290 stmmac_pmt(priv, priv->hw, 0); 7291 mutex_unlock(&priv->lock); 7292 priv->irq_wake = 0; 7293 } else { 7294 pinctrl_pm_select_default_state(priv->device); 7295 /* reset the phy so that it's ready */ 7296 if (priv->mii) 7297 stmmac_mdio_reset(priv->mii); 7298 } 7299 7300 if (priv->plat->serdes_powerup) { 7301 ret = priv->plat->serdes_powerup(ndev, 7302 priv->plat->bsp_priv); 7303 7304 if (ret < 0) 7305 return ret; 7306 } 7307 7308 rtnl_lock(); 7309 if (device_may_wakeup(priv->device) && priv->plat->pmt) { 7310 phylink_resume(priv->phylink); 7311 } else { 7312 phylink_resume(priv->phylink); 7313 if (device_may_wakeup(priv->device)) 7314 phylink_speed_up(priv->phylink); 7315 } 7316 rtnl_unlock(); 7317 7318 rtnl_lock(); 7319 mutex_lock(&priv->lock); 7320 7321 stmmac_reset_queues_param(priv); 7322 7323 stmmac_free_tx_skbufs(priv); 7324 stmmac_clear_descriptors(priv); 7325 7326 stmmac_hw_setup(ndev, false); 7327 stmmac_init_coalesce(priv); 7328 stmmac_set_rx_mode(ndev); 7329 7330 stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw); 7331 7332 stmmac_enable_all_queues(priv); 7333 7334 mutex_unlock(&priv->lock); 7335 rtnl_unlock(); 7336 7337 netif_device_attach(ndev); 7338 7339 return 0; 7340 } 7341 EXPORT_SYMBOL_GPL(stmmac_resume); 7342 7343 #ifndef MODULE 7344 static int __init stmmac_cmdline_opt(char *str) 7345 { 7346 char *opt; 7347 7348 if (!str || !*str) 7349 return -EINVAL; 7350 while ((opt = strsep(&str, ",")) != NULL) { 7351 if (!strncmp(opt, "debug:", 6)) { 7352 if (kstrtoint(opt + 6, 0, &debug)) 7353 goto err; 7354 } else if (!strncmp(opt, "phyaddr:", 8)) { 7355 if (kstrtoint(opt + 8, 0, &phyaddr)) 7356 goto err; 7357 } else if (!strncmp(opt, "buf_sz:", 7)) { 7358 if (kstrtoint(opt + 7, 0, &buf_sz)) 7359 goto err; 7360 } else if (!strncmp(opt, "tc:", 3)) { 7361 if (kstrtoint(opt + 3, 0, &tc)) 7362 goto err; 7363 } else if (!strncmp(opt, "watchdog:", 9)) { 7364 if (kstrtoint(opt + 9, 0, &watchdog)) 7365 goto err; 7366 } else if (!strncmp(opt, "flow_ctrl:", 10)) { 7367 if (kstrtoint(opt + 10, 0, &flow_ctrl)) 7368 goto err; 7369 } else if (!strncmp(opt, "pause:", 6)) { 7370 if (kstrtoint(opt + 6, 0, &pause)) 7371 goto err; 7372 } else if (!strncmp(opt, "eee_timer:", 10)) { 7373 if (kstrtoint(opt + 10, 0, &eee_timer)) 7374 goto err; 7375 } else if (!strncmp(opt, "chain_mode:", 11)) { 7376 if (kstrtoint(opt + 11, 0, &chain_mode)) 7377 goto err; 7378 } 7379 } 7380 return 0; 7381 7382 err: 7383 pr_err("%s: ERROR broken module parameter conversion", __func__); 7384 return -EINVAL; 7385 } 7386 7387 __setup("stmmaceth=", stmmac_cmdline_opt); 7388 #endif /* MODULE */ 7389 7390 static int __init stmmac_init(void) 7391 { 7392 #ifdef CONFIG_DEBUG_FS 7393 /* Create debugfs main directory if it doesn't exist yet */ 7394 if (!stmmac_fs_dir) 7395 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL); 7396 register_netdevice_notifier(&stmmac_notifier); 7397 #endif 7398 7399 return 0; 7400 } 7401 7402 static void __exit stmmac_exit(void) 7403 { 7404 #ifdef CONFIG_DEBUG_FS 7405 unregister_netdevice_notifier(&stmmac_notifier); 7406 debugfs_remove_recursive(stmmac_fs_dir); 7407 #endif 7408 } 7409 7410 module_init(stmmac_init) 7411 module_exit(stmmac_exit) 7412 7413 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver"); 7414 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>"); 7415 MODULE_LICENSE("GPL"); 7416