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 <net/pkt_cls.h> 42 #include "stmmac_ptp.h" 43 #include "stmmac.h" 44 #include <linux/reset.h> 45 #include <linux/of_mdio.h> 46 #include "dwmac1000.h" 47 #include "dwxgmac2.h" 48 #include "hwif.h" 49 50 #define STMMAC_ALIGN(x) ALIGN(ALIGN(x, SMP_CACHE_BYTES), 16) 51 #define TSO_MAX_BUFF_SIZE (SZ_16K - 1) 52 53 /* Module parameters */ 54 #define TX_TIMEO 5000 55 static int watchdog = TX_TIMEO; 56 module_param(watchdog, int, 0644); 57 MODULE_PARM_DESC(watchdog, "Transmit timeout in milliseconds (default 5s)"); 58 59 static int debug = -1; 60 module_param(debug, int, 0644); 61 MODULE_PARM_DESC(debug, "Message Level (-1: default, 0: no output, 16: all)"); 62 63 static int phyaddr = -1; 64 module_param(phyaddr, int, 0444); 65 MODULE_PARM_DESC(phyaddr, "Physical device address"); 66 67 #define STMMAC_TX_THRESH(x) ((x)->dma_tx_size / 4) 68 #define STMMAC_RX_THRESH(x) ((x)->dma_rx_size / 4) 69 70 static int flow_ctrl = FLOW_AUTO; 71 module_param(flow_ctrl, int, 0644); 72 MODULE_PARM_DESC(flow_ctrl, "Flow control ability [on/off]"); 73 74 static int pause = PAUSE_TIME; 75 module_param(pause, int, 0644); 76 MODULE_PARM_DESC(pause, "Flow Control Pause Time"); 77 78 #define TC_DEFAULT 64 79 static int tc = TC_DEFAULT; 80 module_param(tc, int, 0644); 81 MODULE_PARM_DESC(tc, "DMA threshold control value"); 82 83 #define DEFAULT_BUFSIZE 1536 84 static int buf_sz = DEFAULT_BUFSIZE; 85 module_param(buf_sz, int, 0644); 86 MODULE_PARM_DESC(buf_sz, "DMA buffer size"); 87 88 #define STMMAC_RX_COPYBREAK 256 89 90 static const u32 default_msg_level = (NETIF_MSG_DRV | NETIF_MSG_PROBE | 91 NETIF_MSG_LINK | NETIF_MSG_IFUP | 92 NETIF_MSG_IFDOWN | NETIF_MSG_TIMER); 93 94 #define STMMAC_DEFAULT_LPI_TIMER 1000 95 static int eee_timer = STMMAC_DEFAULT_LPI_TIMER; 96 module_param(eee_timer, int, 0644); 97 MODULE_PARM_DESC(eee_timer, "LPI tx expiration time in msec"); 98 #define STMMAC_LPI_T(x) (jiffies + usecs_to_jiffies(x)) 99 100 /* By default the driver will use the ring mode to manage tx and rx descriptors, 101 * but allow user to force to use the chain instead of the ring 102 */ 103 static unsigned int chain_mode; 104 module_param(chain_mode, int, 0444); 105 MODULE_PARM_DESC(chain_mode, "To use chain instead of ring mode"); 106 107 static irqreturn_t stmmac_interrupt(int irq, void *dev_id); 108 109 #ifdef CONFIG_DEBUG_FS 110 static const struct net_device_ops stmmac_netdev_ops; 111 static void stmmac_init_fs(struct net_device *dev); 112 static void stmmac_exit_fs(struct net_device *dev); 113 #endif 114 115 #define STMMAC_COAL_TIMER(x) (ns_to_ktime((x) * NSEC_PER_USEC)) 116 117 int stmmac_bus_clks_config(struct stmmac_priv *priv, bool enabled) 118 { 119 int ret = 0; 120 121 if (enabled) { 122 ret = clk_prepare_enable(priv->plat->stmmac_clk); 123 if (ret) 124 return ret; 125 ret = clk_prepare_enable(priv->plat->pclk); 126 if (ret) { 127 clk_disable_unprepare(priv->plat->stmmac_clk); 128 return ret; 129 } 130 if (priv->plat->clks_config) { 131 ret = priv->plat->clks_config(priv->plat->bsp_priv, enabled); 132 if (ret) { 133 clk_disable_unprepare(priv->plat->stmmac_clk); 134 clk_disable_unprepare(priv->plat->pclk); 135 return ret; 136 } 137 } 138 } else { 139 clk_disable_unprepare(priv->plat->stmmac_clk); 140 clk_disable_unprepare(priv->plat->pclk); 141 if (priv->plat->clks_config) 142 priv->plat->clks_config(priv->plat->bsp_priv, enabled); 143 } 144 145 return ret; 146 } 147 EXPORT_SYMBOL_GPL(stmmac_bus_clks_config); 148 149 /** 150 * stmmac_verify_args - verify the driver parameters. 151 * Description: it checks the driver parameters and set a default in case of 152 * errors. 153 */ 154 static void stmmac_verify_args(void) 155 { 156 if (unlikely(watchdog < 0)) 157 watchdog = TX_TIMEO; 158 if (unlikely((buf_sz < DEFAULT_BUFSIZE) || (buf_sz > BUF_SIZE_16KiB))) 159 buf_sz = DEFAULT_BUFSIZE; 160 if (unlikely(flow_ctrl > 1)) 161 flow_ctrl = FLOW_AUTO; 162 else if (likely(flow_ctrl < 0)) 163 flow_ctrl = FLOW_OFF; 164 if (unlikely((pause < 0) || (pause > 0xffff))) 165 pause = PAUSE_TIME; 166 if (eee_timer < 0) 167 eee_timer = STMMAC_DEFAULT_LPI_TIMER; 168 } 169 170 /** 171 * stmmac_disable_all_queues - Disable all queues 172 * @priv: driver private structure 173 */ 174 static void stmmac_disable_all_queues(struct stmmac_priv *priv) 175 { 176 u32 rx_queues_cnt = priv->plat->rx_queues_to_use; 177 u32 tx_queues_cnt = priv->plat->tx_queues_to_use; 178 u32 maxq = max(rx_queues_cnt, tx_queues_cnt); 179 u32 queue; 180 181 for (queue = 0; queue < maxq; queue++) { 182 struct stmmac_channel *ch = &priv->channel[queue]; 183 184 if (queue < rx_queues_cnt) 185 napi_disable(&ch->rx_napi); 186 if (queue < tx_queues_cnt) 187 napi_disable(&ch->tx_napi); 188 } 189 } 190 191 /** 192 * stmmac_enable_all_queues - Enable all queues 193 * @priv: driver private structure 194 */ 195 static void stmmac_enable_all_queues(struct stmmac_priv *priv) 196 { 197 u32 rx_queues_cnt = priv->plat->rx_queues_to_use; 198 u32 tx_queues_cnt = priv->plat->tx_queues_to_use; 199 u32 maxq = max(rx_queues_cnt, tx_queues_cnt); 200 u32 queue; 201 202 for (queue = 0; queue < maxq; queue++) { 203 struct stmmac_channel *ch = &priv->channel[queue]; 204 205 if (queue < rx_queues_cnt) 206 napi_enable(&ch->rx_napi); 207 if (queue < tx_queues_cnt) 208 napi_enable(&ch->tx_napi); 209 } 210 } 211 212 static void stmmac_service_event_schedule(struct stmmac_priv *priv) 213 { 214 if (!test_bit(STMMAC_DOWN, &priv->state) && 215 !test_and_set_bit(STMMAC_SERVICE_SCHED, &priv->state)) 216 queue_work(priv->wq, &priv->service_task); 217 } 218 219 static void stmmac_global_err(struct stmmac_priv *priv) 220 { 221 netif_carrier_off(priv->dev); 222 set_bit(STMMAC_RESET_REQUESTED, &priv->state); 223 stmmac_service_event_schedule(priv); 224 } 225 226 /** 227 * stmmac_clk_csr_set - dynamically set the MDC clock 228 * @priv: driver private structure 229 * Description: this is to dynamically set the MDC clock according to the csr 230 * clock input. 231 * Note: 232 * If a specific clk_csr value is passed from the platform 233 * this means that the CSR Clock Range selection cannot be 234 * changed at run-time and it is fixed (as reported in the driver 235 * documentation). Viceversa the driver will try to set the MDC 236 * clock dynamically according to the actual clock input. 237 */ 238 static void stmmac_clk_csr_set(struct stmmac_priv *priv) 239 { 240 u32 clk_rate; 241 242 clk_rate = clk_get_rate(priv->plat->stmmac_clk); 243 244 /* Platform provided default clk_csr would be assumed valid 245 * for all other cases except for the below mentioned ones. 246 * For values higher than the IEEE 802.3 specified frequency 247 * we can not estimate the proper divider as it is not known 248 * the frequency of clk_csr_i. So we do not change the default 249 * divider. 250 */ 251 if (!(priv->clk_csr & MAC_CSR_H_FRQ_MASK)) { 252 if (clk_rate < CSR_F_35M) 253 priv->clk_csr = STMMAC_CSR_20_35M; 254 else if ((clk_rate >= CSR_F_35M) && (clk_rate < CSR_F_60M)) 255 priv->clk_csr = STMMAC_CSR_35_60M; 256 else if ((clk_rate >= CSR_F_60M) && (clk_rate < CSR_F_100M)) 257 priv->clk_csr = STMMAC_CSR_60_100M; 258 else if ((clk_rate >= CSR_F_100M) && (clk_rate < CSR_F_150M)) 259 priv->clk_csr = STMMAC_CSR_100_150M; 260 else if ((clk_rate >= CSR_F_150M) && (clk_rate < CSR_F_250M)) 261 priv->clk_csr = STMMAC_CSR_150_250M; 262 else if ((clk_rate >= CSR_F_250M) && (clk_rate < CSR_F_300M)) 263 priv->clk_csr = STMMAC_CSR_250_300M; 264 } 265 266 if (priv->plat->has_sun8i) { 267 if (clk_rate > 160000000) 268 priv->clk_csr = 0x03; 269 else if (clk_rate > 80000000) 270 priv->clk_csr = 0x02; 271 else if (clk_rate > 40000000) 272 priv->clk_csr = 0x01; 273 else 274 priv->clk_csr = 0; 275 } 276 277 if (priv->plat->has_xgmac) { 278 if (clk_rate > 400000000) 279 priv->clk_csr = 0x5; 280 else if (clk_rate > 350000000) 281 priv->clk_csr = 0x4; 282 else if (clk_rate > 300000000) 283 priv->clk_csr = 0x3; 284 else if (clk_rate > 250000000) 285 priv->clk_csr = 0x2; 286 else if (clk_rate > 150000000) 287 priv->clk_csr = 0x1; 288 else 289 priv->clk_csr = 0x0; 290 } 291 } 292 293 static void print_pkt(unsigned char *buf, int len) 294 { 295 pr_debug("len = %d byte, buf addr: 0x%p\n", len, buf); 296 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len); 297 } 298 299 static inline u32 stmmac_tx_avail(struct stmmac_priv *priv, u32 queue) 300 { 301 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 302 u32 avail; 303 304 if (tx_q->dirty_tx > tx_q->cur_tx) 305 avail = tx_q->dirty_tx - tx_q->cur_tx - 1; 306 else 307 avail = priv->dma_tx_size - tx_q->cur_tx + tx_q->dirty_tx - 1; 308 309 return avail; 310 } 311 312 /** 313 * stmmac_rx_dirty - Get RX queue dirty 314 * @priv: driver private structure 315 * @queue: RX queue index 316 */ 317 static inline u32 stmmac_rx_dirty(struct stmmac_priv *priv, u32 queue) 318 { 319 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 320 u32 dirty; 321 322 if (rx_q->dirty_rx <= rx_q->cur_rx) 323 dirty = rx_q->cur_rx - rx_q->dirty_rx; 324 else 325 dirty = priv->dma_rx_size - rx_q->dirty_rx + rx_q->cur_rx; 326 327 return dirty; 328 } 329 330 static void stmmac_lpi_entry_timer_config(struct stmmac_priv *priv, bool en) 331 { 332 int tx_lpi_timer; 333 334 /* Clear/set the SW EEE timer flag based on LPI ET enablement */ 335 priv->eee_sw_timer_en = en ? 0 : 1; 336 tx_lpi_timer = en ? priv->tx_lpi_timer : 0; 337 stmmac_set_eee_lpi_timer(priv, priv->hw, tx_lpi_timer); 338 } 339 340 /** 341 * stmmac_enable_eee_mode - check and enter in LPI mode 342 * @priv: driver private structure 343 * Description: this function is to verify and enter in LPI mode in case of 344 * EEE. 345 */ 346 static void stmmac_enable_eee_mode(struct stmmac_priv *priv) 347 { 348 u32 tx_cnt = priv->plat->tx_queues_to_use; 349 u32 queue; 350 351 /* check if all TX queues have the work finished */ 352 for (queue = 0; queue < tx_cnt; queue++) { 353 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 354 355 if (tx_q->dirty_tx != tx_q->cur_tx) 356 return; /* still unfinished work */ 357 } 358 359 /* Check and enter in LPI mode */ 360 if (!priv->tx_path_in_lpi_mode) 361 stmmac_set_eee_mode(priv, priv->hw, 362 priv->plat->en_tx_lpi_clockgating); 363 } 364 365 /** 366 * stmmac_disable_eee_mode - disable and exit from LPI mode 367 * @priv: driver private structure 368 * Description: this function is to exit and disable EEE in case of 369 * LPI state is true. This is called by the xmit. 370 */ 371 void stmmac_disable_eee_mode(struct stmmac_priv *priv) 372 { 373 if (!priv->eee_sw_timer_en) { 374 stmmac_lpi_entry_timer_config(priv, 0); 375 return; 376 } 377 378 stmmac_reset_eee_mode(priv, priv->hw); 379 del_timer_sync(&priv->eee_ctrl_timer); 380 priv->tx_path_in_lpi_mode = false; 381 } 382 383 /** 384 * stmmac_eee_ctrl_timer - EEE TX SW timer. 385 * @t: timer_list struct containing private info 386 * Description: 387 * if there is no data transfer and if we are not in LPI state, 388 * then MAC Transmitter can be moved to LPI state. 389 */ 390 static void stmmac_eee_ctrl_timer(struct timer_list *t) 391 { 392 struct stmmac_priv *priv = from_timer(priv, t, eee_ctrl_timer); 393 394 stmmac_enable_eee_mode(priv); 395 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer)); 396 } 397 398 /** 399 * stmmac_eee_init - init EEE 400 * @priv: driver private structure 401 * Description: 402 * if the GMAC supports the EEE (from the HW cap reg) and the phy device 403 * can also manage EEE, this function enable the LPI state and start related 404 * timer. 405 */ 406 bool stmmac_eee_init(struct stmmac_priv *priv) 407 { 408 int eee_tw_timer = priv->eee_tw_timer; 409 410 /* Using PCS we cannot dial with the phy registers at this stage 411 * so we do not support extra feature like EEE. 412 */ 413 if (priv->hw->pcs == STMMAC_PCS_TBI || 414 priv->hw->pcs == STMMAC_PCS_RTBI) 415 return false; 416 417 /* Check if MAC core supports the EEE feature. */ 418 if (!priv->dma_cap.eee) 419 return false; 420 421 mutex_lock(&priv->lock); 422 423 /* Check if it needs to be deactivated */ 424 if (!priv->eee_active) { 425 if (priv->eee_enabled) { 426 netdev_dbg(priv->dev, "disable EEE\n"); 427 stmmac_lpi_entry_timer_config(priv, 0); 428 del_timer_sync(&priv->eee_ctrl_timer); 429 stmmac_set_eee_timer(priv, priv->hw, 0, eee_tw_timer); 430 } 431 mutex_unlock(&priv->lock); 432 return false; 433 } 434 435 if (priv->eee_active && !priv->eee_enabled) { 436 timer_setup(&priv->eee_ctrl_timer, stmmac_eee_ctrl_timer, 0); 437 stmmac_set_eee_timer(priv, priv->hw, STMMAC_DEFAULT_LIT_LS, 438 eee_tw_timer); 439 } 440 441 if (priv->plat->has_gmac4 && priv->tx_lpi_timer <= STMMAC_ET_MAX) { 442 del_timer_sync(&priv->eee_ctrl_timer); 443 priv->tx_path_in_lpi_mode = false; 444 stmmac_lpi_entry_timer_config(priv, 1); 445 } else { 446 stmmac_lpi_entry_timer_config(priv, 0); 447 mod_timer(&priv->eee_ctrl_timer, 448 STMMAC_LPI_T(priv->tx_lpi_timer)); 449 } 450 451 mutex_unlock(&priv->lock); 452 netdev_dbg(priv->dev, "Energy-Efficient Ethernet initialized\n"); 453 return true; 454 } 455 456 /* stmmac_get_tx_hwtstamp - get HW TX timestamps 457 * @priv: driver private structure 458 * @p : descriptor pointer 459 * @skb : the socket buffer 460 * Description : 461 * This function will read timestamp from the descriptor & pass it to stack. 462 * and also perform some sanity checks. 463 */ 464 static void stmmac_get_tx_hwtstamp(struct stmmac_priv *priv, 465 struct dma_desc *p, struct sk_buff *skb) 466 { 467 struct skb_shared_hwtstamps shhwtstamp; 468 bool found = false; 469 u64 ns = 0; 470 471 if (!priv->hwts_tx_en) 472 return; 473 474 /* exit if skb doesn't support hw tstamp */ 475 if (likely(!skb || !(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))) 476 return; 477 478 /* check tx tstamp status */ 479 if (stmmac_get_tx_timestamp_status(priv, p)) { 480 stmmac_get_timestamp(priv, p, priv->adv_ts, &ns); 481 found = true; 482 } else if (!stmmac_get_mac_tx_timestamp(priv, priv->hw, &ns)) { 483 found = true; 484 } 485 486 if (found) { 487 memset(&shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps)); 488 shhwtstamp.hwtstamp = ns_to_ktime(ns); 489 490 netdev_dbg(priv->dev, "get valid TX hw timestamp %llu\n", ns); 491 /* pass tstamp to stack */ 492 skb_tstamp_tx(skb, &shhwtstamp); 493 } 494 } 495 496 /* stmmac_get_rx_hwtstamp - get HW RX timestamps 497 * @priv: driver private structure 498 * @p : descriptor pointer 499 * @np : next descriptor pointer 500 * @skb : the socket buffer 501 * Description : 502 * This function will read received packet's timestamp from the descriptor 503 * and pass it to stack. It also perform some sanity checks. 504 */ 505 static void stmmac_get_rx_hwtstamp(struct stmmac_priv *priv, struct dma_desc *p, 506 struct dma_desc *np, struct sk_buff *skb) 507 { 508 struct skb_shared_hwtstamps *shhwtstamp = NULL; 509 struct dma_desc *desc = p; 510 u64 ns = 0; 511 512 if (!priv->hwts_rx_en) 513 return; 514 /* For GMAC4, the valid timestamp is from CTX next desc. */ 515 if (priv->plat->has_gmac4 || priv->plat->has_xgmac) 516 desc = np; 517 518 /* Check if timestamp is available */ 519 if (stmmac_get_rx_timestamp_status(priv, p, np, priv->adv_ts)) { 520 stmmac_get_timestamp(priv, desc, priv->adv_ts, &ns); 521 netdev_dbg(priv->dev, "get valid RX hw timestamp %llu\n", ns); 522 shhwtstamp = skb_hwtstamps(skb); 523 memset(shhwtstamp, 0, sizeof(struct skb_shared_hwtstamps)); 524 shhwtstamp->hwtstamp = ns_to_ktime(ns); 525 } else { 526 netdev_dbg(priv->dev, "cannot get RX hw timestamp\n"); 527 } 528 } 529 530 /** 531 * stmmac_hwtstamp_set - control hardware timestamping. 532 * @dev: device pointer. 533 * @ifr: An IOCTL specific structure, that can contain a pointer to 534 * a proprietary structure used to pass information to the driver. 535 * Description: 536 * This function configures the MAC to enable/disable both outgoing(TX) 537 * and incoming(RX) packets time stamping based on user input. 538 * Return Value: 539 * 0 on success and an appropriate -ve integer on failure. 540 */ 541 static int stmmac_hwtstamp_set(struct net_device *dev, struct ifreq *ifr) 542 { 543 struct stmmac_priv *priv = netdev_priv(dev); 544 struct hwtstamp_config config; 545 struct timespec64 now; 546 u64 temp = 0; 547 u32 ptp_v2 = 0; 548 u32 tstamp_all = 0; 549 u32 ptp_over_ipv4_udp = 0; 550 u32 ptp_over_ipv6_udp = 0; 551 u32 ptp_over_ethernet = 0; 552 u32 snap_type_sel = 0; 553 u32 ts_master_en = 0; 554 u32 ts_event_en = 0; 555 u32 sec_inc = 0; 556 u32 value = 0; 557 bool xmac; 558 559 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac; 560 561 if (!(priv->dma_cap.time_stamp || priv->adv_ts)) { 562 netdev_alert(priv->dev, "No support for HW time stamping\n"); 563 priv->hwts_tx_en = 0; 564 priv->hwts_rx_en = 0; 565 566 return -EOPNOTSUPP; 567 } 568 569 if (copy_from_user(&config, ifr->ifr_data, 570 sizeof(config))) 571 return -EFAULT; 572 573 netdev_dbg(priv->dev, "%s config flags:0x%x, tx_type:0x%x, rx_filter:0x%x\n", 574 __func__, config.flags, config.tx_type, config.rx_filter); 575 576 /* reserved for future extensions */ 577 if (config.flags) 578 return -EINVAL; 579 580 if (config.tx_type != HWTSTAMP_TX_OFF && 581 config.tx_type != HWTSTAMP_TX_ON) 582 return -ERANGE; 583 584 if (priv->adv_ts) { 585 switch (config.rx_filter) { 586 case HWTSTAMP_FILTER_NONE: 587 /* time stamp no incoming packet at all */ 588 config.rx_filter = HWTSTAMP_FILTER_NONE; 589 break; 590 591 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 592 /* PTP v1, UDP, any kind of event packet */ 593 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; 594 /* 'xmac' hardware can support Sync, Pdelay_Req and 595 * Pdelay_resp by setting bit14 and bits17/16 to 01 596 * This leaves Delay_Req timestamps out. 597 * Enable all events *and* general purpose message 598 * timestamping 599 */ 600 snap_type_sel = PTP_TCR_SNAPTYPSEL_1; 601 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 602 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 603 break; 604 605 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 606 /* PTP v1, UDP, Sync packet */ 607 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_SYNC; 608 /* take time stamp for SYNC messages only */ 609 ts_event_en = PTP_TCR_TSEVNTENA; 610 611 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 612 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 613 break; 614 615 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 616 /* PTP v1, UDP, Delay_req packet */ 617 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ; 618 /* take time stamp for Delay_Req messages only */ 619 ts_master_en = PTP_TCR_TSMSTRENA; 620 ts_event_en = PTP_TCR_TSEVNTENA; 621 622 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 623 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 624 break; 625 626 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 627 /* PTP v2, UDP, any kind of event packet */ 628 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_EVENT; 629 ptp_v2 = PTP_TCR_TSVER2ENA; 630 /* take time stamp for all event messages */ 631 snap_type_sel = PTP_TCR_SNAPTYPSEL_1; 632 633 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 634 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 635 break; 636 637 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 638 /* PTP v2, UDP, Sync packet */ 639 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_SYNC; 640 ptp_v2 = PTP_TCR_TSVER2ENA; 641 /* take time stamp for SYNC messages only */ 642 ts_event_en = PTP_TCR_TSEVNTENA; 643 644 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 645 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 646 break; 647 648 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 649 /* PTP v2, UDP, Delay_req packet */ 650 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ; 651 ptp_v2 = PTP_TCR_TSVER2ENA; 652 /* take time stamp for Delay_Req messages only */ 653 ts_master_en = PTP_TCR_TSMSTRENA; 654 ts_event_en = PTP_TCR_TSEVNTENA; 655 656 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 657 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 658 break; 659 660 case HWTSTAMP_FILTER_PTP_V2_EVENT: 661 /* PTP v2/802.AS1 any layer, any kind of event packet */ 662 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 663 ptp_v2 = PTP_TCR_TSVER2ENA; 664 snap_type_sel = PTP_TCR_SNAPTYPSEL_1; 665 if (priv->synopsys_id != DWMAC_CORE_5_10) 666 ts_event_en = PTP_TCR_TSEVNTENA; 667 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 668 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 669 ptp_over_ethernet = PTP_TCR_TSIPENA; 670 break; 671 672 case HWTSTAMP_FILTER_PTP_V2_SYNC: 673 /* PTP v2/802.AS1, any layer, Sync packet */ 674 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_SYNC; 675 ptp_v2 = PTP_TCR_TSVER2ENA; 676 /* take time stamp for SYNC messages only */ 677 ts_event_en = PTP_TCR_TSEVNTENA; 678 679 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 680 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 681 ptp_over_ethernet = PTP_TCR_TSIPENA; 682 break; 683 684 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 685 /* PTP v2/802.AS1, any layer, Delay_req packet */ 686 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_DELAY_REQ; 687 ptp_v2 = PTP_TCR_TSVER2ENA; 688 /* take time stamp for Delay_Req messages only */ 689 ts_master_en = PTP_TCR_TSMSTRENA; 690 ts_event_en = PTP_TCR_TSEVNTENA; 691 692 ptp_over_ipv4_udp = PTP_TCR_TSIPV4ENA; 693 ptp_over_ipv6_udp = PTP_TCR_TSIPV6ENA; 694 ptp_over_ethernet = PTP_TCR_TSIPENA; 695 break; 696 697 case HWTSTAMP_FILTER_NTP_ALL: 698 case HWTSTAMP_FILTER_ALL: 699 /* time stamp any incoming packet */ 700 config.rx_filter = HWTSTAMP_FILTER_ALL; 701 tstamp_all = PTP_TCR_TSENALL; 702 break; 703 704 default: 705 return -ERANGE; 706 } 707 } else { 708 switch (config.rx_filter) { 709 case HWTSTAMP_FILTER_NONE: 710 config.rx_filter = HWTSTAMP_FILTER_NONE; 711 break; 712 default: 713 /* PTP v1, UDP, any kind of event packet */ 714 config.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; 715 break; 716 } 717 } 718 priv->hwts_rx_en = ((config.rx_filter == HWTSTAMP_FILTER_NONE) ? 0 : 1); 719 priv->hwts_tx_en = config.tx_type == HWTSTAMP_TX_ON; 720 721 if (!priv->hwts_tx_en && !priv->hwts_rx_en) 722 stmmac_config_hw_tstamping(priv, priv->ptpaddr, 0); 723 else { 724 value = (PTP_TCR_TSENA | PTP_TCR_TSCFUPDT | PTP_TCR_TSCTRLSSR | 725 tstamp_all | ptp_v2 | ptp_over_ethernet | 726 ptp_over_ipv6_udp | ptp_over_ipv4_udp | ts_event_en | 727 ts_master_en | snap_type_sel); 728 stmmac_config_hw_tstamping(priv, priv->ptpaddr, value); 729 730 /* program Sub Second Increment reg */ 731 stmmac_config_sub_second_increment(priv, 732 priv->ptpaddr, priv->plat->clk_ptp_rate, 733 xmac, &sec_inc); 734 temp = div_u64(1000000000ULL, sec_inc); 735 736 /* Store sub second increment and flags for later use */ 737 priv->sub_second_inc = sec_inc; 738 priv->systime_flags = value; 739 740 /* calculate default added value: 741 * formula is : 742 * addend = (2^32)/freq_div_ratio; 743 * where, freq_div_ratio = 1e9ns/sec_inc 744 */ 745 temp = (u64)(temp << 32); 746 priv->default_addend = div_u64(temp, priv->plat->clk_ptp_rate); 747 stmmac_config_addend(priv, priv->ptpaddr, priv->default_addend); 748 749 /* initialize system time */ 750 ktime_get_real_ts64(&now); 751 752 /* lower 32 bits of tv_sec are safe until y2106 */ 753 stmmac_init_systime(priv, priv->ptpaddr, 754 (u32)now.tv_sec, now.tv_nsec); 755 } 756 757 memcpy(&priv->tstamp_config, &config, sizeof(config)); 758 759 return copy_to_user(ifr->ifr_data, &config, 760 sizeof(config)) ? -EFAULT : 0; 761 } 762 763 /** 764 * stmmac_hwtstamp_get - read hardware timestamping. 765 * @dev: device pointer. 766 * @ifr: An IOCTL specific structure, that can contain a pointer to 767 * a proprietary structure used to pass information to the driver. 768 * Description: 769 * This function obtain the current hardware timestamping settings 770 * as requested. 771 */ 772 static int stmmac_hwtstamp_get(struct net_device *dev, struct ifreq *ifr) 773 { 774 struct stmmac_priv *priv = netdev_priv(dev); 775 struct hwtstamp_config *config = &priv->tstamp_config; 776 777 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp)) 778 return -EOPNOTSUPP; 779 780 return copy_to_user(ifr->ifr_data, config, 781 sizeof(*config)) ? -EFAULT : 0; 782 } 783 784 /** 785 * stmmac_init_ptp - init PTP 786 * @priv: driver private structure 787 * Description: this is to verify if the HW supports the PTPv1 or PTPv2. 788 * This is done by looking at the HW cap. register. 789 * This function also registers the ptp driver. 790 */ 791 static int stmmac_init_ptp(struct stmmac_priv *priv) 792 { 793 bool xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac; 794 795 if (!(priv->dma_cap.time_stamp || priv->dma_cap.atime_stamp)) 796 return -EOPNOTSUPP; 797 798 priv->adv_ts = 0; 799 /* Check if adv_ts can be enabled for dwmac 4.x / xgmac core */ 800 if (xmac && priv->dma_cap.atime_stamp) 801 priv->adv_ts = 1; 802 /* Dwmac 3.x core with extend_desc can support adv_ts */ 803 else if (priv->extend_desc && priv->dma_cap.atime_stamp) 804 priv->adv_ts = 1; 805 806 if (priv->dma_cap.time_stamp) 807 netdev_info(priv->dev, "IEEE 1588-2002 Timestamp supported\n"); 808 809 if (priv->adv_ts) 810 netdev_info(priv->dev, 811 "IEEE 1588-2008 Advanced Timestamp supported\n"); 812 813 priv->hwts_tx_en = 0; 814 priv->hwts_rx_en = 0; 815 816 stmmac_ptp_register(priv); 817 818 return 0; 819 } 820 821 static void stmmac_release_ptp(struct stmmac_priv *priv) 822 { 823 clk_disable_unprepare(priv->plat->clk_ptp_ref); 824 stmmac_ptp_unregister(priv); 825 } 826 827 /** 828 * stmmac_mac_flow_ctrl - Configure flow control in all queues 829 * @priv: driver private structure 830 * @duplex: duplex passed to the next function 831 * Description: It is used for configuring the flow control in all queues 832 */ 833 static void stmmac_mac_flow_ctrl(struct stmmac_priv *priv, u32 duplex) 834 { 835 u32 tx_cnt = priv->plat->tx_queues_to_use; 836 837 stmmac_flow_ctrl(priv, priv->hw, duplex, priv->flow_ctrl, 838 priv->pause, tx_cnt); 839 } 840 841 static void stmmac_validate(struct phylink_config *config, 842 unsigned long *supported, 843 struct phylink_link_state *state) 844 { 845 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev)); 846 __ETHTOOL_DECLARE_LINK_MODE_MASK(mac_supported) = { 0, }; 847 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 848 int tx_cnt = priv->plat->tx_queues_to_use; 849 int max_speed = priv->plat->max_speed; 850 851 phylink_set(mac_supported, 10baseT_Half); 852 phylink_set(mac_supported, 10baseT_Full); 853 phylink_set(mac_supported, 100baseT_Half); 854 phylink_set(mac_supported, 100baseT_Full); 855 phylink_set(mac_supported, 1000baseT_Half); 856 phylink_set(mac_supported, 1000baseT_Full); 857 phylink_set(mac_supported, 1000baseKX_Full); 858 859 phylink_set(mac_supported, Autoneg); 860 phylink_set(mac_supported, Pause); 861 phylink_set(mac_supported, Asym_Pause); 862 phylink_set_port_modes(mac_supported); 863 864 /* Cut down 1G if asked to */ 865 if ((max_speed > 0) && (max_speed < 1000)) { 866 phylink_set(mask, 1000baseT_Full); 867 phylink_set(mask, 1000baseX_Full); 868 } else if (priv->plat->has_xgmac) { 869 if (!max_speed || (max_speed >= 2500)) { 870 phylink_set(mac_supported, 2500baseT_Full); 871 phylink_set(mac_supported, 2500baseX_Full); 872 } 873 if (!max_speed || (max_speed >= 5000)) { 874 phylink_set(mac_supported, 5000baseT_Full); 875 } 876 if (!max_speed || (max_speed >= 10000)) { 877 phylink_set(mac_supported, 10000baseSR_Full); 878 phylink_set(mac_supported, 10000baseLR_Full); 879 phylink_set(mac_supported, 10000baseER_Full); 880 phylink_set(mac_supported, 10000baseLRM_Full); 881 phylink_set(mac_supported, 10000baseT_Full); 882 phylink_set(mac_supported, 10000baseKX4_Full); 883 phylink_set(mac_supported, 10000baseKR_Full); 884 } 885 if (!max_speed || (max_speed >= 25000)) { 886 phylink_set(mac_supported, 25000baseCR_Full); 887 phylink_set(mac_supported, 25000baseKR_Full); 888 phylink_set(mac_supported, 25000baseSR_Full); 889 } 890 if (!max_speed || (max_speed >= 40000)) { 891 phylink_set(mac_supported, 40000baseKR4_Full); 892 phylink_set(mac_supported, 40000baseCR4_Full); 893 phylink_set(mac_supported, 40000baseSR4_Full); 894 phylink_set(mac_supported, 40000baseLR4_Full); 895 } 896 if (!max_speed || (max_speed >= 50000)) { 897 phylink_set(mac_supported, 50000baseCR2_Full); 898 phylink_set(mac_supported, 50000baseKR2_Full); 899 phylink_set(mac_supported, 50000baseSR2_Full); 900 phylink_set(mac_supported, 50000baseKR_Full); 901 phylink_set(mac_supported, 50000baseSR_Full); 902 phylink_set(mac_supported, 50000baseCR_Full); 903 phylink_set(mac_supported, 50000baseLR_ER_FR_Full); 904 phylink_set(mac_supported, 50000baseDR_Full); 905 } 906 if (!max_speed || (max_speed >= 100000)) { 907 phylink_set(mac_supported, 100000baseKR4_Full); 908 phylink_set(mac_supported, 100000baseSR4_Full); 909 phylink_set(mac_supported, 100000baseCR4_Full); 910 phylink_set(mac_supported, 100000baseLR4_ER4_Full); 911 phylink_set(mac_supported, 100000baseKR2_Full); 912 phylink_set(mac_supported, 100000baseSR2_Full); 913 phylink_set(mac_supported, 100000baseCR2_Full); 914 phylink_set(mac_supported, 100000baseLR2_ER2_FR2_Full); 915 phylink_set(mac_supported, 100000baseDR2_Full); 916 } 917 } 918 919 /* Half-Duplex can only work with single queue */ 920 if (tx_cnt > 1) { 921 phylink_set(mask, 10baseT_Half); 922 phylink_set(mask, 100baseT_Half); 923 phylink_set(mask, 1000baseT_Half); 924 } 925 926 linkmode_and(supported, supported, mac_supported); 927 linkmode_andnot(supported, supported, mask); 928 929 linkmode_and(state->advertising, state->advertising, mac_supported); 930 linkmode_andnot(state->advertising, state->advertising, mask); 931 932 /* If PCS is supported, check which modes it supports. */ 933 stmmac_xpcs_validate(priv, &priv->hw->xpcs_args, supported, state); 934 } 935 936 static void stmmac_mac_pcs_get_state(struct phylink_config *config, 937 struct phylink_link_state *state) 938 { 939 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev)); 940 941 state->link = 0; 942 stmmac_xpcs_get_state(priv, &priv->hw->xpcs_args, state); 943 } 944 945 static void stmmac_mac_config(struct phylink_config *config, unsigned int mode, 946 const struct phylink_link_state *state) 947 { 948 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev)); 949 950 stmmac_xpcs_config(priv, &priv->hw->xpcs_args, state); 951 } 952 953 static void stmmac_mac_an_restart(struct phylink_config *config) 954 { 955 /* Not Supported */ 956 } 957 958 static void stmmac_mac_link_down(struct phylink_config *config, 959 unsigned int mode, phy_interface_t interface) 960 { 961 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev)); 962 963 stmmac_mac_set(priv, priv->ioaddr, false); 964 priv->eee_active = false; 965 priv->tx_lpi_enabled = false; 966 stmmac_eee_init(priv); 967 stmmac_set_eee_pls(priv, priv->hw, false); 968 } 969 970 static void stmmac_mac_link_up(struct phylink_config *config, 971 struct phy_device *phy, 972 unsigned int mode, phy_interface_t interface, 973 int speed, int duplex, 974 bool tx_pause, bool rx_pause) 975 { 976 struct stmmac_priv *priv = netdev_priv(to_net_dev(config->dev)); 977 u32 ctrl; 978 979 stmmac_xpcs_link_up(priv, &priv->hw->xpcs_args, speed, interface); 980 981 ctrl = readl(priv->ioaddr + MAC_CTRL_REG); 982 ctrl &= ~priv->hw->link.speed_mask; 983 984 if (interface == PHY_INTERFACE_MODE_USXGMII) { 985 switch (speed) { 986 case SPEED_10000: 987 ctrl |= priv->hw->link.xgmii.speed10000; 988 break; 989 case SPEED_5000: 990 ctrl |= priv->hw->link.xgmii.speed5000; 991 break; 992 case SPEED_2500: 993 ctrl |= priv->hw->link.xgmii.speed2500; 994 break; 995 default: 996 return; 997 } 998 } else if (interface == PHY_INTERFACE_MODE_XLGMII) { 999 switch (speed) { 1000 case SPEED_100000: 1001 ctrl |= priv->hw->link.xlgmii.speed100000; 1002 break; 1003 case SPEED_50000: 1004 ctrl |= priv->hw->link.xlgmii.speed50000; 1005 break; 1006 case SPEED_40000: 1007 ctrl |= priv->hw->link.xlgmii.speed40000; 1008 break; 1009 case SPEED_25000: 1010 ctrl |= priv->hw->link.xlgmii.speed25000; 1011 break; 1012 case SPEED_10000: 1013 ctrl |= priv->hw->link.xgmii.speed10000; 1014 break; 1015 case SPEED_2500: 1016 ctrl |= priv->hw->link.speed2500; 1017 break; 1018 case SPEED_1000: 1019 ctrl |= priv->hw->link.speed1000; 1020 break; 1021 default: 1022 return; 1023 } 1024 } else { 1025 switch (speed) { 1026 case SPEED_2500: 1027 ctrl |= priv->hw->link.speed2500; 1028 break; 1029 case SPEED_1000: 1030 ctrl |= priv->hw->link.speed1000; 1031 break; 1032 case SPEED_100: 1033 ctrl |= priv->hw->link.speed100; 1034 break; 1035 case SPEED_10: 1036 ctrl |= priv->hw->link.speed10; 1037 break; 1038 default: 1039 return; 1040 } 1041 } 1042 1043 priv->speed = speed; 1044 1045 if (priv->plat->fix_mac_speed) 1046 priv->plat->fix_mac_speed(priv->plat->bsp_priv, speed); 1047 1048 if (!duplex) 1049 ctrl &= ~priv->hw->link.duplex; 1050 else 1051 ctrl |= priv->hw->link.duplex; 1052 1053 /* Flow Control operation */ 1054 if (tx_pause && rx_pause) 1055 stmmac_mac_flow_ctrl(priv, duplex); 1056 1057 writel(ctrl, priv->ioaddr + MAC_CTRL_REG); 1058 1059 stmmac_mac_set(priv, priv->ioaddr, true); 1060 if (phy && priv->dma_cap.eee) { 1061 priv->eee_active = phy_init_eee(phy, 1) >= 0; 1062 priv->eee_enabled = stmmac_eee_init(priv); 1063 priv->tx_lpi_enabled = priv->eee_enabled; 1064 stmmac_set_eee_pls(priv, priv->hw, true); 1065 } 1066 } 1067 1068 static const struct phylink_mac_ops stmmac_phylink_mac_ops = { 1069 .validate = stmmac_validate, 1070 .mac_pcs_get_state = stmmac_mac_pcs_get_state, 1071 .mac_config = stmmac_mac_config, 1072 .mac_an_restart = stmmac_mac_an_restart, 1073 .mac_link_down = stmmac_mac_link_down, 1074 .mac_link_up = stmmac_mac_link_up, 1075 }; 1076 1077 /** 1078 * stmmac_check_pcs_mode - verify if RGMII/SGMII is supported 1079 * @priv: driver private structure 1080 * Description: this is to verify if the HW supports the PCS. 1081 * Physical Coding Sublayer (PCS) interface that can be used when the MAC is 1082 * configured for the TBI, RTBI, or SGMII PHY interface. 1083 */ 1084 static void stmmac_check_pcs_mode(struct stmmac_priv *priv) 1085 { 1086 int interface = priv->plat->interface; 1087 1088 if (priv->dma_cap.pcs) { 1089 if ((interface == PHY_INTERFACE_MODE_RGMII) || 1090 (interface == PHY_INTERFACE_MODE_RGMII_ID) || 1091 (interface == PHY_INTERFACE_MODE_RGMII_RXID) || 1092 (interface == PHY_INTERFACE_MODE_RGMII_TXID)) { 1093 netdev_dbg(priv->dev, "PCS RGMII support enabled\n"); 1094 priv->hw->pcs = STMMAC_PCS_RGMII; 1095 } else if (interface == PHY_INTERFACE_MODE_SGMII) { 1096 netdev_dbg(priv->dev, "PCS SGMII support enabled\n"); 1097 priv->hw->pcs = STMMAC_PCS_SGMII; 1098 } 1099 } 1100 } 1101 1102 /** 1103 * stmmac_init_phy - PHY initialization 1104 * @dev: net device structure 1105 * Description: it initializes the driver's PHY state, and attaches the PHY 1106 * to the mac driver. 1107 * Return value: 1108 * 0 on success 1109 */ 1110 static int stmmac_init_phy(struct net_device *dev) 1111 { 1112 struct ethtool_wolinfo wol = { .cmd = ETHTOOL_GWOL }; 1113 struct stmmac_priv *priv = netdev_priv(dev); 1114 struct device_node *node; 1115 int ret; 1116 1117 node = priv->plat->phylink_node; 1118 1119 if (node) 1120 ret = phylink_of_phy_connect(priv->phylink, node, 0); 1121 1122 /* Some DT bindings do not set-up the PHY handle. Let's try to 1123 * manually parse it 1124 */ 1125 if (!node || ret) { 1126 int addr = priv->plat->phy_addr; 1127 struct phy_device *phydev; 1128 1129 phydev = mdiobus_get_phy(priv->mii, addr); 1130 if (!phydev) { 1131 netdev_err(priv->dev, "no phy at addr %d\n", addr); 1132 return -ENODEV; 1133 } 1134 1135 ret = phylink_connect_phy(priv->phylink, phydev); 1136 } 1137 1138 phylink_ethtool_get_wol(priv->phylink, &wol); 1139 device_set_wakeup_capable(priv->device, !!wol.supported); 1140 1141 return ret; 1142 } 1143 1144 static int stmmac_phy_setup(struct stmmac_priv *priv) 1145 { 1146 struct fwnode_handle *fwnode = of_fwnode_handle(priv->plat->phylink_node); 1147 int mode = priv->plat->phy_interface; 1148 struct phylink *phylink; 1149 1150 priv->phylink_config.dev = &priv->dev->dev; 1151 priv->phylink_config.type = PHYLINK_NETDEV; 1152 priv->phylink_config.pcs_poll = true; 1153 priv->phylink_config.ovr_an_inband = 1154 priv->plat->mdio_bus_data->xpcs_an_inband; 1155 1156 if (!fwnode) 1157 fwnode = dev_fwnode(priv->device); 1158 1159 phylink = phylink_create(&priv->phylink_config, fwnode, 1160 mode, &stmmac_phylink_mac_ops); 1161 if (IS_ERR(phylink)) 1162 return PTR_ERR(phylink); 1163 1164 priv->phylink = phylink; 1165 return 0; 1166 } 1167 1168 static void stmmac_display_rx_rings(struct stmmac_priv *priv) 1169 { 1170 u32 rx_cnt = priv->plat->rx_queues_to_use; 1171 unsigned int desc_size; 1172 void *head_rx; 1173 u32 queue; 1174 1175 /* Display RX rings */ 1176 for (queue = 0; queue < rx_cnt; queue++) { 1177 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1178 1179 pr_info("\tRX Queue %u rings\n", queue); 1180 1181 if (priv->extend_desc) { 1182 head_rx = (void *)rx_q->dma_erx; 1183 desc_size = sizeof(struct dma_extended_desc); 1184 } else { 1185 head_rx = (void *)rx_q->dma_rx; 1186 desc_size = sizeof(struct dma_desc); 1187 } 1188 1189 /* Display RX ring */ 1190 stmmac_display_ring(priv, head_rx, priv->dma_rx_size, true, 1191 rx_q->dma_rx_phy, desc_size); 1192 } 1193 } 1194 1195 static void stmmac_display_tx_rings(struct stmmac_priv *priv) 1196 { 1197 u32 tx_cnt = priv->plat->tx_queues_to_use; 1198 unsigned int desc_size; 1199 void *head_tx; 1200 u32 queue; 1201 1202 /* Display TX rings */ 1203 for (queue = 0; queue < tx_cnt; queue++) { 1204 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1205 1206 pr_info("\tTX Queue %d rings\n", queue); 1207 1208 if (priv->extend_desc) { 1209 head_tx = (void *)tx_q->dma_etx; 1210 desc_size = sizeof(struct dma_extended_desc); 1211 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) { 1212 head_tx = (void *)tx_q->dma_entx; 1213 desc_size = sizeof(struct dma_edesc); 1214 } else { 1215 head_tx = (void *)tx_q->dma_tx; 1216 desc_size = sizeof(struct dma_desc); 1217 } 1218 1219 stmmac_display_ring(priv, head_tx, priv->dma_tx_size, false, 1220 tx_q->dma_tx_phy, desc_size); 1221 } 1222 } 1223 1224 static void stmmac_display_rings(struct stmmac_priv *priv) 1225 { 1226 /* Display RX ring */ 1227 stmmac_display_rx_rings(priv); 1228 1229 /* Display TX ring */ 1230 stmmac_display_tx_rings(priv); 1231 } 1232 1233 static int stmmac_set_bfsize(int mtu, int bufsize) 1234 { 1235 int ret = bufsize; 1236 1237 if (mtu >= BUF_SIZE_8KiB) 1238 ret = BUF_SIZE_16KiB; 1239 else if (mtu >= BUF_SIZE_4KiB) 1240 ret = BUF_SIZE_8KiB; 1241 else if (mtu >= BUF_SIZE_2KiB) 1242 ret = BUF_SIZE_4KiB; 1243 else if (mtu > DEFAULT_BUFSIZE) 1244 ret = BUF_SIZE_2KiB; 1245 else 1246 ret = DEFAULT_BUFSIZE; 1247 1248 return ret; 1249 } 1250 1251 /** 1252 * stmmac_clear_rx_descriptors - clear RX descriptors 1253 * @priv: driver private structure 1254 * @queue: RX queue index 1255 * Description: this function is called to clear the RX descriptors 1256 * in case of both basic and extended descriptors are used. 1257 */ 1258 static void stmmac_clear_rx_descriptors(struct stmmac_priv *priv, u32 queue) 1259 { 1260 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1261 int i; 1262 1263 /* Clear the RX descriptors */ 1264 for (i = 0; i < priv->dma_rx_size; i++) 1265 if (priv->extend_desc) 1266 stmmac_init_rx_desc(priv, &rx_q->dma_erx[i].basic, 1267 priv->use_riwt, priv->mode, 1268 (i == priv->dma_rx_size - 1), 1269 priv->dma_buf_sz); 1270 else 1271 stmmac_init_rx_desc(priv, &rx_q->dma_rx[i], 1272 priv->use_riwt, priv->mode, 1273 (i == priv->dma_rx_size - 1), 1274 priv->dma_buf_sz); 1275 } 1276 1277 /** 1278 * stmmac_clear_tx_descriptors - clear tx descriptors 1279 * @priv: driver private structure 1280 * @queue: TX queue index. 1281 * Description: this function is called to clear the TX descriptors 1282 * in case of both basic and extended descriptors are used. 1283 */ 1284 static void stmmac_clear_tx_descriptors(struct stmmac_priv *priv, u32 queue) 1285 { 1286 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1287 int i; 1288 1289 /* Clear the TX descriptors */ 1290 for (i = 0; i < priv->dma_tx_size; i++) { 1291 int last = (i == (priv->dma_tx_size - 1)); 1292 struct dma_desc *p; 1293 1294 if (priv->extend_desc) 1295 p = &tx_q->dma_etx[i].basic; 1296 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 1297 p = &tx_q->dma_entx[i].basic; 1298 else 1299 p = &tx_q->dma_tx[i]; 1300 1301 stmmac_init_tx_desc(priv, p, priv->mode, last); 1302 } 1303 } 1304 1305 /** 1306 * stmmac_clear_descriptors - clear descriptors 1307 * @priv: driver private structure 1308 * Description: this function is called to clear the TX and RX descriptors 1309 * in case of both basic and extended descriptors are used. 1310 */ 1311 static void stmmac_clear_descriptors(struct stmmac_priv *priv) 1312 { 1313 u32 rx_queue_cnt = priv->plat->rx_queues_to_use; 1314 u32 tx_queue_cnt = priv->plat->tx_queues_to_use; 1315 u32 queue; 1316 1317 /* Clear the RX descriptors */ 1318 for (queue = 0; queue < rx_queue_cnt; queue++) 1319 stmmac_clear_rx_descriptors(priv, queue); 1320 1321 /* Clear the TX descriptors */ 1322 for (queue = 0; queue < tx_queue_cnt; queue++) 1323 stmmac_clear_tx_descriptors(priv, queue); 1324 } 1325 1326 /** 1327 * stmmac_init_rx_buffers - init the RX descriptor buffer. 1328 * @priv: driver private structure 1329 * @p: descriptor pointer 1330 * @i: descriptor index 1331 * @flags: gfp flag 1332 * @queue: RX queue index 1333 * Description: this function is called to allocate a receive buffer, perform 1334 * the DMA mapping and init the descriptor. 1335 */ 1336 static int stmmac_init_rx_buffers(struct stmmac_priv *priv, struct dma_desc *p, 1337 int i, gfp_t flags, u32 queue) 1338 { 1339 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1340 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i]; 1341 1342 buf->page = page_pool_dev_alloc_pages(rx_q->page_pool); 1343 if (!buf->page) 1344 return -ENOMEM; 1345 1346 if (priv->sph) { 1347 buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool); 1348 if (!buf->sec_page) 1349 return -ENOMEM; 1350 1351 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page); 1352 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true); 1353 } else { 1354 buf->sec_page = NULL; 1355 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false); 1356 } 1357 1358 buf->addr = page_pool_get_dma_addr(buf->page); 1359 stmmac_set_desc_addr(priv, p, buf->addr); 1360 if (priv->dma_buf_sz == BUF_SIZE_16KiB) 1361 stmmac_init_desc3(priv, p); 1362 1363 return 0; 1364 } 1365 1366 /** 1367 * stmmac_free_rx_buffer - free RX dma buffers 1368 * @priv: private structure 1369 * @queue: RX queue index 1370 * @i: buffer index. 1371 */ 1372 static void stmmac_free_rx_buffer(struct stmmac_priv *priv, u32 queue, int i) 1373 { 1374 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1375 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i]; 1376 1377 if (buf->page) 1378 page_pool_put_full_page(rx_q->page_pool, buf->page, false); 1379 buf->page = NULL; 1380 1381 if (buf->sec_page) 1382 page_pool_put_full_page(rx_q->page_pool, buf->sec_page, false); 1383 buf->sec_page = NULL; 1384 } 1385 1386 /** 1387 * stmmac_free_tx_buffer - free RX dma buffers 1388 * @priv: private structure 1389 * @queue: RX queue index 1390 * @i: buffer index. 1391 */ 1392 static void stmmac_free_tx_buffer(struct stmmac_priv *priv, u32 queue, int i) 1393 { 1394 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1395 1396 if (tx_q->tx_skbuff_dma[i].buf) { 1397 if (tx_q->tx_skbuff_dma[i].map_as_page) 1398 dma_unmap_page(priv->device, 1399 tx_q->tx_skbuff_dma[i].buf, 1400 tx_q->tx_skbuff_dma[i].len, 1401 DMA_TO_DEVICE); 1402 else 1403 dma_unmap_single(priv->device, 1404 tx_q->tx_skbuff_dma[i].buf, 1405 tx_q->tx_skbuff_dma[i].len, 1406 DMA_TO_DEVICE); 1407 } 1408 1409 if (tx_q->tx_skbuff[i]) { 1410 dev_kfree_skb_any(tx_q->tx_skbuff[i]); 1411 tx_q->tx_skbuff[i] = NULL; 1412 tx_q->tx_skbuff_dma[i].buf = 0; 1413 tx_q->tx_skbuff_dma[i].map_as_page = false; 1414 } 1415 } 1416 1417 /** 1418 * stmmac_reinit_rx_buffers - reinit the RX descriptor buffer. 1419 * @priv: driver private structure 1420 * Description: this function is called to re-allocate a receive buffer, perform 1421 * the DMA mapping and init the descriptor. 1422 */ 1423 static void stmmac_reinit_rx_buffers(struct stmmac_priv *priv) 1424 { 1425 u32 rx_count = priv->plat->rx_queues_to_use; 1426 u32 queue; 1427 int i; 1428 1429 for (queue = 0; queue < rx_count; queue++) { 1430 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1431 1432 for (i = 0; i < priv->dma_rx_size; i++) { 1433 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i]; 1434 1435 if (buf->page) { 1436 page_pool_recycle_direct(rx_q->page_pool, buf->page); 1437 buf->page = NULL; 1438 } 1439 1440 if (priv->sph && buf->sec_page) { 1441 page_pool_recycle_direct(rx_q->page_pool, buf->sec_page); 1442 buf->sec_page = NULL; 1443 } 1444 } 1445 } 1446 1447 for (queue = 0; queue < rx_count; queue++) { 1448 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1449 1450 for (i = 0; i < priv->dma_rx_size; i++) { 1451 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[i]; 1452 struct dma_desc *p; 1453 1454 if (priv->extend_desc) 1455 p = &((rx_q->dma_erx + i)->basic); 1456 else 1457 p = rx_q->dma_rx + i; 1458 1459 if (!buf->page) { 1460 buf->page = page_pool_dev_alloc_pages(rx_q->page_pool); 1461 if (!buf->page) 1462 goto err_reinit_rx_buffers; 1463 1464 buf->addr = page_pool_get_dma_addr(buf->page); 1465 } 1466 1467 if (priv->sph && !buf->sec_page) { 1468 buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool); 1469 if (!buf->sec_page) 1470 goto err_reinit_rx_buffers; 1471 1472 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page); 1473 } 1474 1475 stmmac_set_desc_addr(priv, p, buf->addr); 1476 if (priv->sph) 1477 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true); 1478 else 1479 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false); 1480 if (priv->dma_buf_sz == BUF_SIZE_16KiB) 1481 stmmac_init_desc3(priv, p); 1482 } 1483 } 1484 1485 return; 1486 1487 err_reinit_rx_buffers: 1488 do { 1489 while (--i >= 0) 1490 stmmac_free_rx_buffer(priv, queue, i); 1491 1492 if (queue == 0) 1493 break; 1494 1495 i = priv->dma_rx_size; 1496 } while (queue-- > 0); 1497 } 1498 1499 /** 1500 * init_dma_rx_desc_rings - init the RX descriptor rings 1501 * @dev: net device structure 1502 * @flags: gfp flag. 1503 * Description: this function initializes the DMA RX descriptors 1504 * and allocates the socket buffers. It supports the chained and ring 1505 * modes. 1506 */ 1507 static int init_dma_rx_desc_rings(struct net_device *dev, gfp_t flags) 1508 { 1509 struct stmmac_priv *priv = netdev_priv(dev); 1510 u32 rx_count = priv->plat->rx_queues_to_use; 1511 int ret = -ENOMEM; 1512 int queue; 1513 int i; 1514 1515 /* RX INITIALIZATION */ 1516 netif_dbg(priv, probe, priv->dev, 1517 "SKB addresses:\nskb\t\tskb data\tdma data\n"); 1518 1519 for (queue = 0; queue < rx_count; queue++) { 1520 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1521 1522 netif_dbg(priv, probe, priv->dev, 1523 "(%s) dma_rx_phy=0x%08x\n", __func__, 1524 (u32)rx_q->dma_rx_phy); 1525 1526 stmmac_clear_rx_descriptors(priv, queue); 1527 1528 for (i = 0; i < priv->dma_rx_size; i++) { 1529 struct dma_desc *p; 1530 1531 if (priv->extend_desc) 1532 p = &((rx_q->dma_erx + i)->basic); 1533 else 1534 p = rx_q->dma_rx + i; 1535 1536 ret = stmmac_init_rx_buffers(priv, p, i, flags, 1537 queue); 1538 if (ret) 1539 goto err_init_rx_buffers; 1540 } 1541 1542 rx_q->cur_rx = 0; 1543 rx_q->dirty_rx = (unsigned int)(i - priv->dma_rx_size); 1544 1545 /* Setup the chained descriptor addresses */ 1546 if (priv->mode == STMMAC_CHAIN_MODE) { 1547 if (priv->extend_desc) 1548 stmmac_mode_init(priv, rx_q->dma_erx, 1549 rx_q->dma_rx_phy, 1550 priv->dma_rx_size, 1); 1551 else 1552 stmmac_mode_init(priv, rx_q->dma_rx, 1553 rx_q->dma_rx_phy, 1554 priv->dma_rx_size, 0); 1555 } 1556 } 1557 1558 return 0; 1559 1560 err_init_rx_buffers: 1561 while (queue >= 0) { 1562 while (--i >= 0) 1563 stmmac_free_rx_buffer(priv, queue, i); 1564 1565 if (queue == 0) 1566 break; 1567 1568 i = priv->dma_rx_size; 1569 queue--; 1570 } 1571 1572 return ret; 1573 } 1574 1575 /** 1576 * init_dma_tx_desc_rings - init the TX descriptor rings 1577 * @dev: net device structure. 1578 * Description: this function initializes the DMA TX descriptors 1579 * and allocates the socket buffers. It supports the chained and ring 1580 * modes. 1581 */ 1582 static int init_dma_tx_desc_rings(struct net_device *dev) 1583 { 1584 struct stmmac_priv *priv = netdev_priv(dev); 1585 u32 tx_queue_cnt = priv->plat->tx_queues_to_use; 1586 u32 queue; 1587 int i; 1588 1589 for (queue = 0; queue < tx_queue_cnt; queue++) { 1590 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1591 1592 netif_dbg(priv, probe, priv->dev, 1593 "(%s) dma_tx_phy=0x%08x\n", __func__, 1594 (u32)tx_q->dma_tx_phy); 1595 1596 /* Setup the chained descriptor addresses */ 1597 if (priv->mode == STMMAC_CHAIN_MODE) { 1598 if (priv->extend_desc) 1599 stmmac_mode_init(priv, tx_q->dma_etx, 1600 tx_q->dma_tx_phy, 1601 priv->dma_tx_size, 1); 1602 else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) 1603 stmmac_mode_init(priv, tx_q->dma_tx, 1604 tx_q->dma_tx_phy, 1605 priv->dma_tx_size, 0); 1606 } 1607 1608 for (i = 0; i < priv->dma_tx_size; i++) { 1609 struct dma_desc *p; 1610 if (priv->extend_desc) 1611 p = &((tx_q->dma_etx + i)->basic); 1612 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 1613 p = &((tx_q->dma_entx + i)->basic); 1614 else 1615 p = tx_q->dma_tx + i; 1616 1617 stmmac_clear_desc(priv, p); 1618 1619 tx_q->tx_skbuff_dma[i].buf = 0; 1620 tx_q->tx_skbuff_dma[i].map_as_page = false; 1621 tx_q->tx_skbuff_dma[i].len = 0; 1622 tx_q->tx_skbuff_dma[i].last_segment = false; 1623 tx_q->tx_skbuff[i] = NULL; 1624 } 1625 1626 tx_q->dirty_tx = 0; 1627 tx_q->cur_tx = 0; 1628 tx_q->mss = 0; 1629 1630 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue)); 1631 } 1632 1633 return 0; 1634 } 1635 1636 /** 1637 * init_dma_desc_rings - init the RX/TX descriptor rings 1638 * @dev: net device structure 1639 * @flags: gfp flag. 1640 * Description: this function initializes the DMA RX/TX descriptors 1641 * and allocates the socket buffers. It supports the chained and ring 1642 * modes. 1643 */ 1644 static int init_dma_desc_rings(struct net_device *dev, gfp_t flags) 1645 { 1646 struct stmmac_priv *priv = netdev_priv(dev); 1647 int ret; 1648 1649 ret = init_dma_rx_desc_rings(dev, flags); 1650 if (ret) 1651 return ret; 1652 1653 ret = init_dma_tx_desc_rings(dev); 1654 1655 stmmac_clear_descriptors(priv); 1656 1657 if (netif_msg_hw(priv)) 1658 stmmac_display_rings(priv); 1659 1660 return ret; 1661 } 1662 1663 /** 1664 * dma_free_rx_skbufs - free RX dma buffers 1665 * @priv: private structure 1666 * @queue: RX queue index 1667 */ 1668 static void dma_free_rx_skbufs(struct stmmac_priv *priv, u32 queue) 1669 { 1670 int i; 1671 1672 for (i = 0; i < priv->dma_rx_size; i++) 1673 stmmac_free_rx_buffer(priv, queue, i); 1674 } 1675 1676 /** 1677 * dma_free_tx_skbufs - free TX dma buffers 1678 * @priv: private structure 1679 * @queue: TX queue index 1680 */ 1681 static void dma_free_tx_skbufs(struct stmmac_priv *priv, u32 queue) 1682 { 1683 int i; 1684 1685 for (i = 0; i < priv->dma_tx_size; i++) 1686 stmmac_free_tx_buffer(priv, queue, i); 1687 } 1688 1689 /** 1690 * stmmac_free_tx_skbufs - free TX skb buffers 1691 * @priv: private structure 1692 */ 1693 static void stmmac_free_tx_skbufs(struct stmmac_priv *priv) 1694 { 1695 u32 tx_queue_cnt = priv->plat->tx_queues_to_use; 1696 u32 queue; 1697 1698 for (queue = 0; queue < tx_queue_cnt; queue++) 1699 dma_free_tx_skbufs(priv, queue); 1700 } 1701 1702 /** 1703 * free_dma_rx_desc_resources - free RX dma desc resources 1704 * @priv: private structure 1705 */ 1706 static void free_dma_rx_desc_resources(struct stmmac_priv *priv) 1707 { 1708 u32 rx_count = priv->plat->rx_queues_to_use; 1709 u32 queue; 1710 1711 /* Free RX queue resources */ 1712 for (queue = 0; queue < rx_count; queue++) { 1713 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1714 1715 /* Release the DMA RX socket buffers */ 1716 dma_free_rx_skbufs(priv, queue); 1717 1718 /* Free DMA regions of consistent memory previously allocated */ 1719 if (!priv->extend_desc) 1720 dma_free_coherent(priv->device, priv->dma_rx_size * 1721 sizeof(struct dma_desc), 1722 rx_q->dma_rx, rx_q->dma_rx_phy); 1723 else 1724 dma_free_coherent(priv->device, priv->dma_rx_size * 1725 sizeof(struct dma_extended_desc), 1726 rx_q->dma_erx, rx_q->dma_rx_phy); 1727 1728 kfree(rx_q->buf_pool); 1729 if (rx_q->page_pool) 1730 page_pool_destroy(rx_q->page_pool); 1731 } 1732 } 1733 1734 /** 1735 * free_dma_tx_desc_resources - free TX dma desc resources 1736 * @priv: private structure 1737 */ 1738 static void free_dma_tx_desc_resources(struct stmmac_priv *priv) 1739 { 1740 u32 tx_count = priv->plat->tx_queues_to_use; 1741 u32 queue; 1742 1743 /* Free TX queue resources */ 1744 for (queue = 0; queue < tx_count; queue++) { 1745 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1746 size_t size; 1747 void *addr; 1748 1749 /* Release the DMA TX socket buffers */ 1750 dma_free_tx_skbufs(priv, queue); 1751 1752 if (priv->extend_desc) { 1753 size = sizeof(struct dma_extended_desc); 1754 addr = tx_q->dma_etx; 1755 } else if (tx_q->tbs & STMMAC_TBS_AVAIL) { 1756 size = sizeof(struct dma_edesc); 1757 addr = tx_q->dma_entx; 1758 } else { 1759 size = sizeof(struct dma_desc); 1760 addr = tx_q->dma_tx; 1761 } 1762 1763 size *= priv->dma_tx_size; 1764 1765 dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy); 1766 1767 kfree(tx_q->tx_skbuff_dma); 1768 kfree(tx_q->tx_skbuff); 1769 } 1770 } 1771 1772 /** 1773 * alloc_dma_rx_desc_resources - alloc RX resources. 1774 * @priv: private structure 1775 * Description: according to which descriptor can be used (extend or basic) 1776 * this function allocates the resources for TX and RX paths. In case of 1777 * reception, for example, it pre-allocated the RX socket buffer in order to 1778 * allow zero-copy mechanism. 1779 */ 1780 static int alloc_dma_rx_desc_resources(struct stmmac_priv *priv) 1781 { 1782 u32 rx_count = priv->plat->rx_queues_to_use; 1783 int ret = -ENOMEM; 1784 u32 queue; 1785 1786 /* RX queues buffers and DMA */ 1787 for (queue = 0; queue < rx_count; queue++) { 1788 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 1789 struct page_pool_params pp_params = { 0 }; 1790 unsigned int num_pages; 1791 1792 rx_q->queue_index = queue; 1793 rx_q->priv_data = priv; 1794 1795 pp_params.flags = PP_FLAG_DMA_MAP; 1796 pp_params.pool_size = priv->dma_rx_size; 1797 num_pages = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE); 1798 pp_params.order = ilog2(num_pages); 1799 pp_params.nid = dev_to_node(priv->device); 1800 pp_params.dev = priv->device; 1801 pp_params.dma_dir = DMA_FROM_DEVICE; 1802 1803 rx_q->page_pool = page_pool_create(&pp_params); 1804 if (IS_ERR(rx_q->page_pool)) { 1805 ret = PTR_ERR(rx_q->page_pool); 1806 rx_q->page_pool = NULL; 1807 goto err_dma; 1808 } 1809 1810 rx_q->buf_pool = kcalloc(priv->dma_rx_size, 1811 sizeof(*rx_q->buf_pool), 1812 GFP_KERNEL); 1813 if (!rx_q->buf_pool) 1814 goto err_dma; 1815 1816 if (priv->extend_desc) { 1817 rx_q->dma_erx = dma_alloc_coherent(priv->device, 1818 priv->dma_rx_size * 1819 sizeof(struct dma_extended_desc), 1820 &rx_q->dma_rx_phy, 1821 GFP_KERNEL); 1822 if (!rx_q->dma_erx) 1823 goto err_dma; 1824 1825 } else { 1826 rx_q->dma_rx = dma_alloc_coherent(priv->device, 1827 priv->dma_rx_size * 1828 sizeof(struct dma_desc), 1829 &rx_q->dma_rx_phy, 1830 GFP_KERNEL); 1831 if (!rx_q->dma_rx) 1832 goto err_dma; 1833 } 1834 } 1835 1836 return 0; 1837 1838 err_dma: 1839 free_dma_rx_desc_resources(priv); 1840 1841 return ret; 1842 } 1843 1844 /** 1845 * alloc_dma_tx_desc_resources - alloc TX resources. 1846 * @priv: private structure 1847 * Description: according to which descriptor can be used (extend or basic) 1848 * this function allocates the resources for TX and RX paths. In case of 1849 * reception, for example, it pre-allocated the RX socket buffer in order to 1850 * allow zero-copy mechanism. 1851 */ 1852 static int alloc_dma_tx_desc_resources(struct stmmac_priv *priv) 1853 { 1854 u32 tx_count = priv->plat->tx_queues_to_use; 1855 int ret = -ENOMEM; 1856 u32 queue; 1857 1858 /* TX queues buffers and DMA */ 1859 for (queue = 0; queue < tx_count; queue++) { 1860 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 1861 size_t size; 1862 void *addr; 1863 1864 tx_q->queue_index = queue; 1865 tx_q->priv_data = priv; 1866 1867 tx_q->tx_skbuff_dma = kcalloc(priv->dma_tx_size, 1868 sizeof(*tx_q->tx_skbuff_dma), 1869 GFP_KERNEL); 1870 if (!tx_q->tx_skbuff_dma) 1871 goto err_dma; 1872 1873 tx_q->tx_skbuff = kcalloc(priv->dma_tx_size, 1874 sizeof(struct sk_buff *), 1875 GFP_KERNEL); 1876 if (!tx_q->tx_skbuff) 1877 goto err_dma; 1878 1879 if (priv->extend_desc) 1880 size = sizeof(struct dma_extended_desc); 1881 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 1882 size = sizeof(struct dma_edesc); 1883 else 1884 size = sizeof(struct dma_desc); 1885 1886 size *= priv->dma_tx_size; 1887 1888 addr = dma_alloc_coherent(priv->device, size, 1889 &tx_q->dma_tx_phy, GFP_KERNEL); 1890 if (!addr) 1891 goto err_dma; 1892 1893 if (priv->extend_desc) 1894 tx_q->dma_etx = addr; 1895 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 1896 tx_q->dma_entx = addr; 1897 else 1898 tx_q->dma_tx = addr; 1899 } 1900 1901 return 0; 1902 1903 err_dma: 1904 free_dma_tx_desc_resources(priv); 1905 return ret; 1906 } 1907 1908 /** 1909 * alloc_dma_desc_resources - alloc TX/RX resources. 1910 * @priv: private structure 1911 * Description: according to which descriptor can be used (extend or basic) 1912 * this function allocates the resources for TX and RX paths. In case of 1913 * reception, for example, it pre-allocated the RX socket buffer in order to 1914 * allow zero-copy mechanism. 1915 */ 1916 static int alloc_dma_desc_resources(struct stmmac_priv *priv) 1917 { 1918 /* RX Allocation */ 1919 int ret = alloc_dma_rx_desc_resources(priv); 1920 1921 if (ret) 1922 return ret; 1923 1924 ret = alloc_dma_tx_desc_resources(priv); 1925 1926 return ret; 1927 } 1928 1929 /** 1930 * free_dma_desc_resources - free dma desc resources 1931 * @priv: private structure 1932 */ 1933 static void free_dma_desc_resources(struct stmmac_priv *priv) 1934 { 1935 /* Release the DMA RX socket buffers */ 1936 free_dma_rx_desc_resources(priv); 1937 1938 /* Release the DMA TX socket buffers */ 1939 free_dma_tx_desc_resources(priv); 1940 } 1941 1942 /** 1943 * stmmac_mac_enable_rx_queues - Enable MAC rx queues 1944 * @priv: driver private structure 1945 * Description: It is used for enabling the rx queues in the MAC 1946 */ 1947 static void stmmac_mac_enable_rx_queues(struct stmmac_priv *priv) 1948 { 1949 u32 rx_queues_count = priv->plat->rx_queues_to_use; 1950 int queue; 1951 u8 mode; 1952 1953 for (queue = 0; queue < rx_queues_count; queue++) { 1954 mode = priv->plat->rx_queues_cfg[queue].mode_to_use; 1955 stmmac_rx_queue_enable(priv, priv->hw, mode, queue); 1956 } 1957 } 1958 1959 /** 1960 * stmmac_start_rx_dma - start RX DMA channel 1961 * @priv: driver private structure 1962 * @chan: RX channel index 1963 * Description: 1964 * This starts a RX DMA channel 1965 */ 1966 static void stmmac_start_rx_dma(struct stmmac_priv *priv, u32 chan) 1967 { 1968 netdev_dbg(priv->dev, "DMA RX processes started in channel %d\n", chan); 1969 stmmac_start_rx(priv, priv->ioaddr, chan); 1970 } 1971 1972 /** 1973 * stmmac_start_tx_dma - start TX DMA channel 1974 * @priv: driver private structure 1975 * @chan: TX channel index 1976 * Description: 1977 * This starts a TX DMA channel 1978 */ 1979 static void stmmac_start_tx_dma(struct stmmac_priv *priv, u32 chan) 1980 { 1981 netdev_dbg(priv->dev, "DMA TX processes started in channel %d\n", chan); 1982 stmmac_start_tx(priv, priv->ioaddr, chan); 1983 } 1984 1985 /** 1986 * stmmac_stop_rx_dma - stop RX DMA channel 1987 * @priv: driver private structure 1988 * @chan: RX channel index 1989 * Description: 1990 * This stops a RX DMA channel 1991 */ 1992 static void stmmac_stop_rx_dma(struct stmmac_priv *priv, u32 chan) 1993 { 1994 netdev_dbg(priv->dev, "DMA RX processes stopped in channel %d\n", chan); 1995 stmmac_stop_rx(priv, priv->ioaddr, chan); 1996 } 1997 1998 /** 1999 * stmmac_stop_tx_dma - stop TX DMA channel 2000 * @priv: driver private structure 2001 * @chan: TX channel index 2002 * Description: 2003 * This stops a TX DMA channel 2004 */ 2005 static void stmmac_stop_tx_dma(struct stmmac_priv *priv, u32 chan) 2006 { 2007 netdev_dbg(priv->dev, "DMA TX processes stopped in channel %d\n", chan); 2008 stmmac_stop_tx(priv, priv->ioaddr, chan); 2009 } 2010 2011 /** 2012 * stmmac_start_all_dma - start all RX and TX DMA channels 2013 * @priv: driver private structure 2014 * Description: 2015 * This starts all the RX and TX DMA channels 2016 */ 2017 static void stmmac_start_all_dma(struct stmmac_priv *priv) 2018 { 2019 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2020 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2021 u32 chan = 0; 2022 2023 for (chan = 0; chan < rx_channels_count; chan++) 2024 stmmac_start_rx_dma(priv, chan); 2025 2026 for (chan = 0; chan < tx_channels_count; chan++) 2027 stmmac_start_tx_dma(priv, chan); 2028 } 2029 2030 /** 2031 * stmmac_stop_all_dma - stop all RX and TX DMA channels 2032 * @priv: driver private structure 2033 * Description: 2034 * This stops the RX and TX DMA channels 2035 */ 2036 static void stmmac_stop_all_dma(struct stmmac_priv *priv) 2037 { 2038 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2039 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2040 u32 chan = 0; 2041 2042 for (chan = 0; chan < rx_channels_count; chan++) 2043 stmmac_stop_rx_dma(priv, chan); 2044 2045 for (chan = 0; chan < tx_channels_count; chan++) 2046 stmmac_stop_tx_dma(priv, chan); 2047 } 2048 2049 /** 2050 * stmmac_dma_operation_mode - HW DMA operation mode 2051 * @priv: driver private structure 2052 * Description: it is used for configuring the DMA operation mode register in 2053 * order to program the tx/rx DMA thresholds or Store-And-Forward mode. 2054 */ 2055 static void stmmac_dma_operation_mode(struct stmmac_priv *priv) 2056 { 2057 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2058 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2059 int rxfifosz = priv->plat->rx_fifo_size; 2060 int txfifosz = priv->plat->tx_fifo_size; 2061 u32 txmode = 0; 2062 u32 rxmode = 0; 2063 u32 chan = 0; 2064 u8 qmode = 0; 2065 2066 if (rxfifosz == 0) 2067 rxfifosz = priv->dma_cap.rx_fifo_size; 2068 if (txfifosz == 0) 2069 txfifosz = priv->dma_cap.tx_fifo_size; 2070 2071 /* Adjust for real per queue fifo size */ 2072 rxfifosz /= rx_channels_count; 2073 txfifosz /= tx_channels_count; 2074 2075 if (priv->plat->force_thresh_dma_mode) { 2076 txmode = tc; 2077 rxmode = tc; 2078 } else if (priv->plat->force_sf_dma_mode || priv->plat->tx_coe) { 2079 /* 2080 * In case of GMAC, SF mode can be enabled 2081 * to perform the TX COE in HW. This depends on: 2082 * 1) TX COE if actually supported 2083 * 2) There is no bugged Jumbo frame support 2084 * that needs to not insert csum in the TDES. 2085 */ 2086 txmode = SF_DMA_MODE; 2087 rxmode = SF_DMA_MODE; 2088 priv->xstats.threshold = SF_DMA_MODE; 2089 } else { 2090 txmode = tc; 2091 rxmode = SF_DMA_MODE; 2092 } 2093 2094 /* configure all channels */ 2095 for (chan = 0; chan < rx_channels_count; chan++) { 2096 qmode = priv->plat->rx_queues_cfg[chan].mode_to_use; 2097 2098 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, 2099 rxfifosz, qmode); 2100 stmmac_set_dma_bfsize(priv, priv->ioaddr, priv->dma_buf_sz, 2101 chan); 2102 } 2103 2104 for (chan = 0; chan < tx_channels_count; chan++) { 2105 qmode = priv->plat->tx_queues_cfg[chan].mode_to_use; 2106 2107 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, 2108 txfifosz, qmode); 2109 } 2110 } 2111 2112 /** 2113 * stmmac_tx_clean - to manage the transmission completion 2114 * @priv: driver private structure 2115 * @budget: napi budget limiting this functions packet handling 2116 * @queue: TX queue index 2117 * Description: it reclaims the transmit resources after transmission completes. 2118 */ 2119 static int stmmac_tx_clean(struct stmmac_priv *priv, int budget, u32 queue) 2120 { 2121 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 2122 unsigned int bytes_compl = 0, pkts_compl = 0; 2123 unsigned int entry, count = 0; 2124 2125 __netif_tx_lock_bh(netdev_get_tx_queue(priv->dev, queue)); 2126 2127 priv->xstats.tx_clean++; 2128 2129 entry = tx_q->dirty_tx; 2130 while ((entry != tx_q->cur_tx) && (count < budget)) { 2131 struct sk_buff *skb = tx_q->tx_skbuff[entry]; 2132 struct dma_desc *p; 2133 int status; 2134 2135 if (priv->extend_desc) 2136 p = (struct dma_desc *)(tx_q->dma_etx + entry); 2137 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 2138 p = &tx_q->dma_entx[entry].basic; 2139 else 2140 p = tx_q->dma_tx + entry; 2141 2142 status = stmmac_tx_status(priv, &priv->dev->stats, 2143 &priv->xstats, p, priv->ioaddr); 2144 /* Check if the descriptor is owned by the DMA */ 2145 if (unlikely(status & tx_dma_own)) 2146 break; 2147 2148 count++; 2149 2150 /* Make sure descriptor fields are read after reading 2151 * the own bit. 2152 */ 2153 dma_rmb(); 2154 2155 /* Just consider the last segment and ...*/ 2156 if (likely(!(status & tx_not_ls))) { 2157 /* ... verify the status error condition */ 2158 if (unlikely(status & tx_err)) { 2159 priv->dev->stats.tx_errors++; 2160 } else { 2161 priv->dev->stats.tx_packets++; 2162 priv->xstats.tx_pkt_n++; 2163 } 2164 stmmac_get_tx_hwtstamp(priv, p, skb); 2165 } 2166 2167 if (likely(tx_q->tx_skbuff_dma[entry].buf)) { 2168 if (tx_q->tx_skbuff_dma[entry].map_as_page) 2169 dma_unmap_page(priv->device, 2170 tx_q->tx_skbuff_dma[entry].buf, 2171 tx_q->tx_skbuff_dma[entry].len, 2172 DMA_TO_DEVICE); 2173 else 2174 dma_unmap_single(priv->device, 2175 tx_q->tx_skbuff_dma[entry].buf, 2176 tx_q->tx_skbuff_dma[entry].len, 2177 DMA_TO_DEVICE); 2178 tx_q->tx_skbuff_dma[entry].buf = 0; 2179 tx_q->tx_skbuff_dma[entry].len = 0; 2180 tx_q->tx_skbuff_dma[entry].map_as_page = false; 2181 } 2182 2183 stmmac_clean_desc3(priv, tx_q, p); 2184 2185 tx_q->tx_skbuff_dma[entry].last_segment = false; 2186 tx_q->tx_skbuff_dma[entry].is_jumbo = false; 2187 2188 if (likely(skb != NULL)) { 2189 pkts_compl++; 2190 bytes_compl += skb->len; 2191 dev_consume_skb_any(skb); 2192 tx_q->tx_skbuff[entry] = NULL; 2193 } 2194 2195 stmmac_release_tx_desc(priv, p, priv->mode); 2196 2197 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size); 2198 } 2199 tx_q->dirty_tx = entry; 2200 2201 netdev_tx_completed_queue(netdev_get_tx_queue(priv->dev, queue), 2202 pkts_compl, bytes_compl); 2203 2204 if (unlikely(netif_tx_queue_stopped(netdev_get_tx_queue(priv->dev, 2205 queue))) && 2206 stmmac_tx_avail(priv, queue) > STMMAC_TX_THRESH(priv)) { 2207 2208 netif_dbg(priv, tx_done, priv->dev, 2209 "%s: restart transmit\n", __func__); 2210 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, queue)); 2211 } 2212 2213 if (priv->eee_enabled && !priv->tx_path_in_lpi_mode && 2214 priv->eee_sw_timer_en) { 2215 stmmac_enable_eee_mode(priv); 2216 mod_timer(&priv->eee_ctrl_timer, STMMAC_LPI_T(priv->tx_lpi_timer)); 2217 } 2218 2219 /* We still have pending packets, let's call for a new scheduling */ 2220 if (tx_q->dirty_tx != tx_q->cur_tx) 2221 hrtimer_start(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer), 2222 HRTIMER_MODE_REL); 2223 2224 __netif_tx_unlock_bh(netdev_get_tx_queue(priv->dev, queue)); 2225 2226 return count; 2227 } 2228 2229 /** 2230 * stmmac_tx_err - to manage the tx error 2231 * @priv: driver private structure 2232 * @chan: channel index 2233 * Description: it cleans the descriptors and restarts the transmission 2234 * in case of transmission errors. 2235 */ 2236 static void stmmac_tx_err(struct stmmac_priv *priv, u32 chan) 2237 { 2238 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 2239 2240 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, chan)); 2241 2242 stmmac_stop_tx_dma(priv, chan); 2243 dma_free_tx_skbufs(priv, chan); 2244 stmmac_clear_tx_descriptors(priv, chan); 2245 tx_q->dirty_tx = 0; 2246 tx_q->cur_tx = 0; 2247 tx_q->mss = 0; 2248 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, chan)); 2249 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg, 2250 tx_q->dma_tx_phy, chan); 2251 stmmac_start_tx_dma(priv, chan); 2252 2253 priv->dev->stats.tx_errors++; 2254 netif_tx_wake_queue(netdev_get_tx_queue(priv->dev, chan)); 2255 } 2256 2257 /** 2258 * stmmac_set_dma_operation_mode - Set DMA operation mode by channel 2259 * @priv: driver private structure 2260 * @txmode: TX operating mode 2261 * @rxmode: RX operating mode 2262 * @chan: channel index 2263 * Description: it is used for configuring of the DMA operation mode in 2264 * runtime in order to program the tx/rx DMA thresholds or Store-And-Forward 2265 * mode. 2266 */ 2267 static void stmmac_set_dma_operation_mode(struct stmmac_priv *priv, u32 txmode, 2268 u32 rxmode, u32 chan) 2269 { 2270 u8 rxqmode = priv->plat->rx_queues_cfg[chan].mode_to_use; 2271 u8 txqmode = priv->plat->tx_queues_cfg[chan].mode_to_use; 2272 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2273 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2274 int rxfifosz = priv->plat->rx_fifo_size; 2275 int txfifosz = priv->plat->tx_fifo_size; 2276 2277 if (rxfifosz == 0) 2278 rxfifosz = priv->dma_cap.rx_fifo_size; 2279 if (txfifosz == 0) 2280 txfifosz = priv->dma_cap.tx_fifo_size; 2281 2282 /* Adjust for real per queue fifo size */ 2283 rxfifosz /= rx_channels_count; 2284 txfifosz /= tx_channels_count; 2285 2286 stmmac_dma_rx_mode(priv, priv->ioaddr, rxmode, chan, rxfifosz, rxqmode); 2287 stmmac_dma_tx_mode(priv, priv->ioaddr, txmode, chan, txfifosz, txqmode); 2288 } 2289 2290 static bool stmmac_safety_feat_interrupt(struct stmmac_priv *priv) 2291 { 2292 int ret; 2293 2294 ret = stmmac_safety_feat_irq_status(priv, priv->dev, 2295 priv->ioaddr, priv->dma_cap.asp, &priv->sstats); 2296 if (ret && (ret != -EINVAL)) { 2297 stmmac_global_err(priv); 2298 return true; 2299 } 2300 2301 return false; 2302 } 2303 2304 static int stmmac_napi_check(struct stmmac_priv *priv, u32 chan) 2305 { 2306 int status = stmmac_dma_interrupt_status(priv, priv->ioaddr, 2307 &priv->xstats, chan); 2308 struct stmmac_channel *ch = &priv->channel[chan]; 2309 unsigned long flags; 2310 2311 if ((status & handle_rx) && (chan < priv->plat->rx_queues_to_use)) { 2312 if (napi_schedule_prep(&ch->rx_napi)) { 2313 spin_lock_irqsave(&ch->lock, flags); 2314 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 1, 0); 2315 spin_unlock_irqrestore(&ch->lock, flags); 2316 __napi_schedule(&ch->rx_napi); 2317 } 2318 } 2319 2320 if ((status & handle_tx) && (chan < priv->plat->tx_queues_to_use)) { 2321 if (napi_schedule_prep(&ch->tx_napi)) { 2322 spin_lock_irqsave(&ch->lock, flags); 2323 stmmac_disable_dma_irq(priv, priv->ioaddr, chan, 0, 1); 2324 spin_unlock_irqrestore(&ch->lock, flags); 2325 __napi_schedule(&ch->tx_napi); 2326 } 2327 } 2328 2329 return status; 2330 } 2331 2332 /** 2333 * stmmac_dma_interrupt - DMA ISR 2334 * @priv: driver private structure 2335 * Description: this is the DMA ISR. It is called by the main ISR. 2336 * It calls the dwmac dma routine and schedule poll method in case of some 2337 * work can be done. 2338 */ 2339 static void stmmac_dma_interrupt(struct stmmac_priv *priv) 2340 { 2341 u32 tx_channel_count = priv->plat->tx_queues_to_use; 2342 u32 rx_channel_count = priv->plat->rx_queues_to_use; 2343 u32 channels_to_check = tx_channel_count > rx_channel_count ? 2344 tx_channel_count : rx_channel_count; 2345 u32 chan; 2346 int status[max_t(u32, MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES)]; 2347 2348 /* Make sure we never check beyond our status buffer. */ 2349 if (WARN_ON_ONCE(channels_to_check > ARRAY_SIZE(status))) 2350 channels_to_check = ARRAY_SIZE(status); 2351 2352 for (chan = 0; chan < channels_to_check; chan++) 2353 status[chan] = stmmac_napi_check(priv, chan); 2354 2355 for (chan = 0; chan < tx_channel_count; chan++) { 2356 if (unlikely(status[chan] & tx_hard_error_bump_tc)) { 2357 /* Try to bump up the dma threshold on this failure */ 2358 if (unlikely(priv->xstats.threshold != SF_DMA_MODE) && 2359 (tc <= 256)) { 2360 tc += 64; 2361 if (priv->plat->force_thresh_dma_mode) 2362 stmmac_set_dma_operation_mode(priv, 2363 tc, 2364 tc, 2365 chan); 2366 else 2367 stmmac_set_dma_operation_mode(priv, 2368 tc, 2369 SF_DMA_MODE, 2370 chan); 2371 priv->xstats.threshold = tc; 2372 } 2373 } else if (unlikely(status[chan] == tx_hard_error)) { 2374 stmmac_tx_err(priv, chan); 2375 } 2376 } 2377 } 2378 2379 /** 2380 * stmmac_mmc_setup: setup the Mac Management Counters (MMC) 2381 * @priv: driver private structure 2382 * Description: this masks the MMC irq, in fact, the counters are managed in SW. 2383 */ 2384 static void stmmac_mmc_setup(struct stmmac_priv *priv) 2385 { 2386 unsigned int mode = MMC_CNTRL_RESET_ON_READ | MMC_CNTRL_COUNTER_RESET | 2387 MMC_CNTRL_PRESET | MMC_CNTRL_FULL_HALF_PRESET; 2388 2389 stmmac_mmc_intr_all_mask(priv, priv->mmcaddr); 2390 2391 if (priv->dma_cap.rmon) { 2392 stmmac_mmc_ctrl(priv, priv->mmcaddr, mode); 2393 memset(&priv->mmc, 0, sizeof(struct stmmac_counters)); 2394 } else 2395 netdev_info(priv->dev, "No MAC Management Counters available\n"); 2396 } 2397 2398 /** 2399 * stmmac_get_hw_features - get MAC capabilities from the HW cap. register. 2400 * @priv: driver private structure 2401 * Description: 2402 * new GMAC chip generations have a new register to indicate the 2403 * presence of the optional feature/functions. 2404 * This can be also used to override the value passed through the 2405 * platform and necessary for old MAC10/100 and GMAC chips. 2406 */ 2407 static int stmmac_get_hw_features(struct stmmac_priv *priv) 2408 { 2409 return stmmac_get_hw_feature(priv, priv->ioaddr, &priv->dma_cap) == 0; 2410 } 2411 2412 /** 2413 * stmmac_check_ether_addr - check if the MAC addr is valid 2414 * @priv: driver private structure 2415 * Description: 2416 * it is to verify if the MAC address is valid, in case of failures it 2417 * generates a random MAC address 2418 */ 2419 static void stmmac_check_ether_addr(struct stmmac_priv *priv) 2420 { 2421 if (!is_valid_ether_addr(priv->dev->dev_addr)) { 2422 stmmac_get_umac_addr(priv, priv->hw, priv->dev->dev_addr, 0); 2423 if (!is_valid_ether_addr(priv->dev->dev_addr)) 2424 eth_hw_addr_random(priv->dev); 2425 dev_info(priv->device, "device MAC address %pM\n", 2426 priv->dev->dev_addr); 2427 } 2428 } 2429 2430 /** 2431 * stmmac_init_dma_engine - DMA init. 2432 * @priv: driver private structure 2433 * Description: 2434 * It inits the DMA invoking the specific MAC/GMAC callback. 2435 * Some DMA parameters can be passed from the platform; 2436 * in case of these are not passed a default is kept for the MAC or GMAC. 2437 */ 2438 static int stmmac_init_dma_engine(struct stmmac_priv *priv) 2439 { 2440 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2441 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2442 u32 dma_csr_ch = max(rx_channels_count, tx_channels_count); 2443 struct stmmac_rx_queue *rx_q; 2444 struct stmmac_tx_queue *tx_q; 2445 u32 chan = 0; 2446 int atds = 0; 2447 int ret = 0; 2448 2449 if (!priv->plat->dma_cfg || !priv->plat->dma_cfg->pbl) { 2450 dev_err(priv->device, "Invalid DMA configuration\n"); 2451 return -EINVAL; 2452 } 2453 2454 if (priv->extend_desc && (priv->mode == STMMAC_RING_MODE)) 2455 atds = 1; 2456 2457 ret = stmmac_reset(priv, priv->ioaddr); 2458 if (ret) { 2459 dev_err(priv->device, "Failed to reset the dma\n"); 2460 return ret; 2461 } 2462 2463 /* DMA Configuration */ 2464 stmmac_dma_init(priv, priv->ioaddr, priv->plat->dma_cfg, atds); 2465 2466 if (priv->plat->axi) 2467 stmmac_axi(priv, priv->ioaddr, priv->plat->axi); 2468 2469 /* DMA CSR Channel configuration */ 2470 for (chan = 0; chan < dma_csr_ch; chan++) 2471 stmmac_init_chan(priv, priv->ioaddr, priv->plat->dma_cfg, chan); 2472 2473 /* DMA RX Channel Configuration */ 2474 for (chan = 0; chan < rx_channels_count; chan++) { 2475 rx_q = &priv->rx_queue[chan]; 2476 2477 stmmac_init_rx_chan(priv, priv->ioaddr, priv->plat->dma_cfg, 2478 rx_q->dma_rx_phy, chan); 2479 2480 rx_q->rx_tail_addr = rx_q->dma_rx_phy + 2481 (priv->dma_rx_size * 2482 sizeof(struct dma_desc)); 2483 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, 2484 rx_q->rx_tail_addr, chan); 2485 } 2486 2487 /* DMA TX Channel Configuration */ 2488 for (chan = 0; chan < tx_channels_count; chan++) { 2489 tx_q = &priv->tx_queue[chan]; 2490 2491 stmmac_init_tx_chan(priv, priv->ioaddr, priv->plat->dma_cfg, 2492 tx_q->dma_tx_phy, chan); 2493 2494 tx_q->tx_tail_addr = tx_q->dma_tx_phy; 2495 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, 2496 tx_q->tx_tail_addr, chan); 2497 } 2498 2499 return ret; 2500 } 2501 2502 static void stmmac_tx_timer_arm(struct stmmac_priv *priv, u32 queue) 2503 { 2504 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 2505 2506 hrtimer_start(&tx_q->txtimer, STMMAC_COAL_TIMER(priv->tx_coal_timer), 2507 HRTIMER_MODE_REL); 2508 } 2509 2510 /** 2511 * stmmac_tx_timer - mitigation sw timer for tx. 2512 * @t: data pointer 2513 * Description: 2514 * This is the timer handler to directly invoke the stmmac_tx_clean. 2515 */ 2516 static enum hrtimer_restart stmmac_tx_timer(struct hrtimer *t) 2517 { 2518 struct stmmac_tx_queue *tx_q = container_of(t, struct stmmac_tx_queue, txtimer); 2519 struct stmmac_priv *priv = tx_q->priv_data; 2520 struct stmmac_channel *ch; 2521 2522 ch = &priv->channel[tx_q->queue_index]; 2523 2524 if (likely(napi_schedule_prep(&ch->tx_napi))) { 2525 unsigned long flags; 2526 2527 spin_lock_irqsave(&ch->lock, flags); 2528 stmmac_disable_dma_irq(priv, priv->ioaddr, ch->index, 0, 1); 2529 spin_unlock_irqrestore(&ch->lock, flags); 2530 __napi_schedule(&ch->tx_napi); 2531 } 2532 2533 return HRTIMER_NORESTART; 2534 } 2535 2536 /** 2537 * stmmac_init_coalesce - init mitigation options. 2538 * @priv: driver private structure 2539 * Description: 2540 * This inits the coalesce parameters: i.e. timer rate, 2541 * timer handler and default threshold used for enabling the 2542 * interrupt on completion bit. 2543 */ 2544 static void stmmac_init_coalesce(struct stmmac_priv *priv) 2545 { 2546 u32 tx_channel_count = priv->plat->tx_queues_to_use; 2547 u32 chan; 2548 2549 priv->tx_coal_frames = STMMAC_TX_FRAMES; 2550 priv->tx_coal_timer = STMMAC_COAL_TX_TIMER; 2551 priv->rx_coal_frames = STMMAC_RX_FRAMES; 2552 2553 for (chan = 0; chan < tx_channel_count; chan++) { 2554 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 2555 2556 hrtimer_init(&tx_q->txtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); 2557 tx_q->txtimer.function = stmmac_tx_timer; 2558 } 2559 } 2560 2561 static void stmmac_set_rings_length(struct stmmac_priv *priv) 2562 { 2563 u32 rx_channels_count = priv->plat->rx_queues_to_use; 2564 u32 tx_channels_count = priv->plat->tx_queues_to_use; 2565 u32 chan; 2566 2567 /* set TX ring length */ 2568 for (chan = 0; chan < tx_channels_count; chan++) 2569 stmmac_set_tx_ring_len(priv, priv->ioaddr, 2570 (priv->dma_tx_size - 1), chan); 2571 2572 /* set RX ring length */ 2573 for (chan = 0; chan < rx_channels_count; chan++) 2574 stmmac_set_rx_ring_len(priv, priv->ioaddr, 2575 (priv->dma_rx_size - 1), chan); 2576 } 2577 2578 /** 2579 * stmmac_set_tx_queue_weight - Set TX queue weight 2580 * @priv: driver private structure 2581 * Description: It is used for setting TX queues weight 2582 */ 2583 static void stmmac_set_tx_queue_weight(struct stmmac_priv *priv) 2584 { 2585 u32 tx_queues_count = priv->plat->tx_queues_to_use; 2586 u32 weight; 2587 u32 queue; 2588 2589 for (queue = 0; queue < tx_queues_count; queue++) { 2590 weight = priv->plat->tx_queues_cfg[queue].weight; 2591 stmmac_set_mtl_tx_queue_weight(priv, priv->hw, weight, queue); 2592 } 2593 } 2594 2595 /** 2596 * stmmac_configure_cbs - Configure CBS in TX queue 2597 * @priv: driver private structure 2598 * Description: It is used for configuring CBS in AVB TX queues 2599 */ 2600 static void stmmac_configure_cbs(struct stmmac_priv *priv) 2601 { 2602 u32 tx_queues_count = priv->plat->tx_queues_to_use; 2603 u32 mode_to_use; 2604 u32 queue; 2605 2606 /* queue 0 is reserved for legacy traffic */ 2607 for (queue = 1; queue < tx_queues_count; queue++) { 2608 mode_to_use = priv->plat->tx_queues_cfg[queue].mode_to_use; 2609 if (mode_to_use == MTL_QUEUE_DCB) 2610 continue; 2611 2612 stmmac_config_cbs(priv, priv->hw, 2613 priv->plat->tx_queues_cfg[queue].send_slope, 2614 priv->plat->tx_queues_cfg[queue].idle_slope, 2615 priv->plat->tx_queues_cfg[queue].high_credit, 2616 priv->plat->tx_queues_cfg[queue].low_credit, 2617 queue); 2618 } 2619 } 2620 2621 /** 2622 * stmmac_rx_queue_dma_chan_map - Map RX queue to RX dma channel 2623 * @priv: driver private structure 2624 * Description: It is used for mapping RX queues to RX dma channels 2625 */ 2626 static void stmmac_rx_queue_dma_chan_map(struct stmmac_priv *priv) 2627 { 2628 u32 rx_queues_count = priv->plat->rx_queues_to_use; 2629 u32 queue; 2630 u32 chan; 2631 2632 for (queue = 0; queue < rx_queues_count; queue++) { 2633 chan = priv->plat->rx_queues_cfg[queue].chan; 2634 stmmac_map_mtl_to_dma(priv, priv->hw, queue, chan); 2635 } 2636 } 2637 2638 /** 2639 * stmmac_mac_config_rx_queues_prio - Configure RX Queue priority 2640 * @priv: driver private structure 2641 * Description: It is used for configuring the RX Queue Priority 2642 */ 2643 static void stmmac_mac_config_rx_queues_prio(struct stmmac_priv *priv) 2644 { 2645 u32 rx_queues_count = priv->plat->rx_queues_to_use; 2646 u32 queue; 2647 u32 prio; 2648 2649 for (queue = 0; queue < rx_queues_count; queue++) { 2650 if (!priv->plat->rx_queues_cfg[queue].use_prio) 2651 continue; 2652 2653 prio = priv->plat->rx_queues_cfg[queue].prio; 2654 stmmac_rx_queue_prio(priv, priv->hw, prio, queue); 2655 } 2656 } 2657 2658 /** 2659 * stmmac_mac_config_tx_queues_prio - Configure TX Queue priority 2660 * @priv: driver private structure 2661 * Description: It is used for configuring the TX Queue Priority 2662 */ 2663 static void stmmac_mac_config_tx_queues_prio(struct stmmac_priv *priv) 2664 { 2665 u32 tx_queues_count = priv->plat->tx_queues_to_use; 2666 u32 queue; 2667 u32 prio; 2668 2669 for (queue = 0; queue < tx_queues_count; queue++) { 2670 if (!priv->plat->tx_queues_cfg[queue].use_prio) 2671 continue; 2672 2673 prio = priv->plat->tx_queues_cfg[queue].prio; 2674 stmmac_tx_queue_prio(priv, priv->hw, prio, queue); 2675 } 2676 } 2677 2678 /** 2679 * stmmac_mac_config_rx_queues_routing - Configure RX Queue Routing 2680 * @priv: driver private structure 2681 * Description: It is used for configuring the RX queue routing 2682 */ 2683 static void stmmac_mac_config_rx_queues_routing(struct stmmac_priv *priv) 2684 { 2685 u32 rx_queues_count = priv->plat->rx_queues_to_use; 2686 u32 queue; 2687 u8 packet; 2688 2689 for (queue = 0; queue < rx_queues_count; queue++) { 2690 /* no specific packet type routing specified for the queue */ 2691 if (priv->plat->rx_queues_cfg[queue].pkt_route == 0x0) 2692 continue; 2693 2694 packet = priv->plat->rx_queues_cfg[queue].pkt_route; 2695 stmmac_rx_queue_routing(priv, priv->hw, packet, queue); 2696 } 2697 } 2698 2699 static void stmmac_mac_config_rss(struct stmmac_priv *priv) 2700 { 2701 if (!priv->dma_cap.rssen || !priv->plat->rss_en) { 2702 priv->rss.enable = false; 2703 return; 2704 } 2705 2706 if (priv->dev->features & NETIF_F_RXHASH) 2707 priv->rss.enable = true; 2708 else 2709 priv->rss.enable = false; 2710 2711 stmmac_rss_configure(priv, priv->hw, &priv->rss, 2712 priv->plat->rx_queues_to_use); 2713 } 2714 2715 /** 2716 * stmmac_mtl_configuration - Configure MTL 2717 * @priv: driver private structure 2718 * Description: It is used for configurring MTL 2719 */ 2720 static void stmmac_mtl_configuration(struct stmmac_priv *priv) 2721 { 2722 u32 rx_queues_count = priv->plat->rx_queues_to_use; 2723 u32 tx_queues_count = priv->plat->tx_queues_to_use; 2724 2725 if (tx_queues_count > 1) 2726 stmmac_set_tx_queue_weight(priv); 2727 2728 /* Configure MTL RX algorithms */ 2729 if (rx_queues_count > 1) 2730 stmmac_prog_mtl_rx_algorithms(priv, priv->hw, 2731 priv->plat->rx_sched_algorithm); 2732 2733 /* Configure MTL TX algorithms */ 2734 if (tx_queues_count > 1) 2735 stmmac_prog_mtl_tx_algorithms(priv, priv->hw, 2736 priv->plat->tx_sched_algorithm); 2737 2738 /* Configure CBS in AVB TX queues */ 2739 if (tx_queues_count > 1) 2740 stmmac_configure_cbs(priv); 2741 2742 /* Map RX MTL to DMA channels */ 2743 stmmac_rx_queue_dma_chan_map(priv); 2744 2745 /* Enable MAC RX Queues */ 2746 stmmac_mac_enable_rx_queues(priv); 2747 2748 /* Set RX priorities */ 2749 if (rx_queues_count > 1) 2750 stmmac_mac_config_rx_queues_prio(priv); 2751 2752 /* Set TX priorities */ 2753 if (tx_queues_count > 1) 2754 stmmac_mac_config_tx_queues_prio(priv); 2755 2756 /* Set RX routing */ 2757 if (rx_queues_count > 1) 2758 stmmac_mac_config_rx_queues_routing(priv); 2759 2760 /* Receive Side Scaling */ 2761 if (rx_queues_count > 1) 2762 stmmac_mac_config_rss(priv); 2763 } 2764 2765 static void stmmac_safety_feat_configuration(struct stmmac_priv *priv) 2766 { 2767 if (priv->dma_cap.asp) { 2768 netdev_info(priv->dev, "Enabling Safety Features\n"); 2769 stmmac_safety_feat_config(priv, priv->ioaddr, priv->dma_cap.asp); 2770 } else { 2771 netdev_info(priv->dev, "No Safety Features support found\n"); 2772 } 2773 } 2774 2775 /** 2776 * stmmac_hw_setup - setup mac in a usable state. 2777 * @dev : pointer to the device structure. 2778 * @init_ptp: initialize PTP if set 2779 * Description: 2780 * this is the main function to setup the HW in a usable state because the 2781 * dma engine is reset, the core registers are configured (e.g. AXI, 2782 * Checksum features, timers). The DMA is ready to start receiving and 2783 * transmitting. 2784 * Return value: 2785 * 0 on success and an appropriate (-)ve integer as defined in errno.h 2786 * file on failure. 2787 */ 2788 static int stmmac_hw_setup(struct net_device *dev, bool init_ptp) 2789 { 2790 struct stmmac_priv *priv = netdev_priv(dev); 2791 u32 rx_cnt = priv->plat->rx_queues_to_use; 2792 u32 tx_cnt = priv->plat->tx_queues_to_use; 2793 u32 chan; 2794 int ret; 2795 2796 /* DMA initialization and SW reset */ 2797 ret = stmmac_init_dma_engine(priv); 2798 if (ret < 0) { 2799 netdev_err(priv->dev, "%s: DMA engine initialization failed\n", 2800 __func__); 2801 return ret; 2802 } 2803 2804 /* Copy the MAC addr into the HW */ 2805 stmmac_set_umac_addr(priv, priv->hw, dev->dev_addr, 0); 2806 2807 /* PS and related bits will be programmed according to the speed */ 2808 if (priv->hw->pcs) { 2809 int speed = priv->plat->mac_port_sel_speed; 2810 2811 if ((speed == SPEED_10) || (speed == SPEED_100) || 2812 (speed == SPEED_1000)) { 2813 priv->hw->ps = speed; 2814 } else { 2815 dev_warn(priv->device, "invalid port speed\n"); 2816 priv->hw->ps = 0; 2817 } 2818 } 2819 2820 /* Initialize the MAC Core */ 2821 stmmac_core_init(priv, priv->hw, dev); 2822 2823 /* Initialize MTL*/ 2824 stmmac_mtl_configuration(priv); 2825 2826 /* Initialize Safety Features */ 2827 stmmac_safety_feat_configuration(priv); 2828 2829 ret = stmmac_rx_ipc(priv, priv->hw); 2830 if (!ret) { 2831 netdev_warn(priv->dev, "RX IPC Checksum Offload disabled\n"); 2832 priv->plat->rx_coe = STMMAC_RX_COE_NONE; 2833 priv->hw->rx_csum = 0; 2834 } 2835 2836 /* Enable the MAC Rx/Tx */ 2837 stmmac_mac_set(priv, priv->ioaddr, true); 2838 2839 /* Set the HW DMA mode and the COE */ 2840 stmmac_dma_operation_mode(priv); 2841 2842 stmmac_mmc_setup(priv); 2843 2844 if (init_ptp) { 2845 ret = clk_prepare_enable(priv->plat->clk_ptp_ref); 2846 if (ret < 0) 2847 netdev_warn(priv->dev, "failed to enable PTP reference clock: %d\n", ret); 2848 2849 ret = stmmac_init_ptp(priv); 2850 if (ret == -EOPNOTSUPP) 2851 netdev_warn(priv->dev, "PTP not supported by HW\n"); 2852 else if (ret) 2853 netdev_warn(priv->dev, "PTP init failed\n"); 2854 } 2855 2856 priv->eee_tw_timer = STMMAC_DEFAULT_TWT_LS; 2857 2858 /* Convert the timer from msec to usec */ 2859 if (!priv->tx_lpi_timer) 2860 priv->tx_lpi_timer = eee_timer * 1000; 2861 2862 if (priv->use_riwt) { 2863 if (!priv->rx_riwt) 2864 priv->rx_riwt = DEF_DMA_RIWT; 2865 2866 ret = stmmac_rx_watchdog(priv, priv->ioaddr, priv->rx_riwt, rx_cnt); 2867 } 2868 2869 if (priv->hw->pcs) 2870 stmmac_pcs_ctrl_ane(priv, priv->ioaddr, 1, priv->hw->ps, 0); 2871 2872 /* set TX and RX rings length */ 2873 stmmac_set_rings_length(priv); 2874 2875 /* Enable TSO */ 2876 if (priv->tso) { 2877 for (chan = 0; chan < tx_cnt; chan++) 2878 stmmac_enable_tso(priv, priv->ioaddr, 1, chan); 2879 } 2880 2881 /* Enable Split Header */ 2882 if (priv->sph && priv->hw->rx_csum) { 2883 for (chan = 0; chan < rx_cnt; chan++) 2884 stmmac_enable_sph(priv, priv->ioaddr, 1, chan); 2885 } 2886 2887 /* VLAN Tag Insertion */ 2888 if (priv->dma_cap.vlins) 2889 stmmac_enable_vlan(priv, priv->hw, STMMAC_VLAN_INSERT); 2890 2891 /* TBS */ 2892 for (chan = 0; chan < tx_cnt; chan++) { 2893 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 2894 int enable = tx_q->tbs & STMMAC_TBS_AVAIL; 2895 2896 stmmac_enable_tbs(priv, priv->ioaddr, enable, chan); 2897 } 2898 2899 /* Configure real RX and TX queues */ 2900 netif_set_real_num_rx_queues(dev, priv->plat->rx_queues_to_use); 2901 netif_set_real_num_tx_queues(dev, priv->plat->tx_queues_to_use); 2902 2903 /* Start the ball rolling... */ 2904 stmmac_start_all_dma(priv); 2905 2906 return 0; 2907 } 2908 2909 static void stmmac_hw_teardown(struct net_device *dev) 2910 { 2911 struct stmmac_priv *priv = netdev_priv(dev); 2912 2913 clk_disable_unprepare(priv->plat->clk_ptp_ref); 2914 } 2915 2916 /** 2917 * stmmac_open - open entry point of the driver 2918 * @dev : pointer to the device structure. 2919 * Description: 2920 * This function is the open entry point of the driver. 2921 * Return value: 2922 * 0 on success and an appropriate (-)ve integer as defined in errno.h 2923 * file on failure. 2924 */ 2925 static int stmmac_open(struct net_device *dev) 2926 { 2927 struct stmmac_priv *priv = netdev_priv(dev); 2928 int bfsize = 0; 2929 u32 chan; 2930 int ret; 2931 2932 ret = pm_runtime_get_sync(priv->device); 2933 if (ret < 0) { 2934 pm_runtime_put_noidle(priv->device); 2935 return ret; 2936 } 2937 2938 if (priv->hw->pcs != STMMAC_PCS_TBI && 2939 priv->hw->pcs != STMMAC_PCS_RTBI && 2940 priv->hw->xpcs_args.an_mode != DW_AN_C73) { 2941 ret = stmmac_init_phy(dev); 2942 if (ret) { 2943 netdev_err(priv->dev, 2944 "%s: Cannot attach to PHY (error: %d)\n", 2945 __func__, ret); 2946 goto init_phy_error; 2947 } 2948 } 2949 2950 /* Extra statistics */ 2951 memset(&priv->xstats, 0, sizeof(struct stmmac_extra_stats)); 2952 priv->xstats.threshold = tc; 2953 2954 bfsize = stmmac_set_16kib_bfsize(priv, dev->mtu); 2955 if (bfsize < 0) 2956 bfsize = 0; 2957 2958 if (bfsize < BUF_SIZE_16KiB) 2959 bfsize = stmmac_set_bfsize(dev->mtu, priv->dma_buf_sz); 2960 2961 priv->dma_buf_sz = bfsize; 2962 buf_sz = bfsize; 2963 2964 priv->rx_copybreak = STMMAC_RX_COPYBREAK; 2965 2966 if (!priv->dma_tx_size) 2967 priv->dma_tx_size = DMA_DEFAULT_TX_SIZE; 2968 if (!priv->dma_rx_size) 2969 priv->dma_rx_size = DMA_DEFAULT_RX_SIZE; 2970 2971 /* Earlier check for TBS */ 2972 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) { 2973 struct stmmac_tx_queue *tx_q = &priv->tx_queue[chan]; 2974 int tbs_en = priv->plat->tx_queues_cfg[chan].tbs_en; 2975 2976 tx_q->tbs |= tbs_en ? STMMAC_TBS_AVAIL : 0; 2977 if (stmmac_enable_tbs(priv, priv->ioaddr, tbs_en, chan)) 2978 tx_q->tbs &= ~STMMAC_TBS_AVAIL; 2979 } 2980 2981 ret = alloc_dma_desc_resources(priv); 2982 if (ret < 0) { 2983 netdev_err(priv->dev, "%s: DMA descriptors allocation failed\n", 2984 __func__); 2985 goto dma_desc_error; 2986 } 2987 2988 ret = init_dma_desc_rings(dev, GFP_KERNEL); 2989 if (ret < 0) { 2990 netdev_err(priv->dev, "%s: DMA descriptors initialization failed\n", 2991 __func__); 2992 goto init_error; 2993 } 2994 2995 ret = stmmac_hw_setup(dev, true); 2996 if (ret < 0) { 2997 netdev_err(priv->dev, "%s: Hw setup failed\n", __func__); 2998 goto init_error; 2999 } 3000 3001 stmmac_init_coalesce(priv); 3002 3003 phylink_start(priv->phylink); 3004 /* We may have called phylink_speed_down before */ 3005 phylink_speed_up(priv->phylink); 3006 3007 /* Request the IRQ lines */ 3008 ret = request_irq(dev->irq, stmmac_interrupt, 3009 IRQF_SHARED, dev->name, dev); 3010 if (unlikely(ret < 0)) { 3011 netdev_err(priv->dev, 3012 "%s: ERROR: allocating the IRQ %d (error: %d)\n", 3013 __func__, dev->irq, ret); 3014 goto irq_error; 3015 } 3016 3017 /* Request the Wake IRQ in case of another line is used for WoL */ 3018 if (priv->wol_irq != dev->irq) { 3019 ret = request_irq(priv->wol_irq, stmmac_interrupt, 3020 IRQF_SHARED, dev->name, dev); 3021 if (unlikely(ret < 0)) { 3022 netdev_err(priv->dev, 3023 "%s: ERROR: allocating the WoL IRQ %d (%d)\n", 3024 __func__, priv->wol_irq, ret); 3025 goto wolirq_error; 3026 } 3027 } 3028 3029 /* Request the IRQ lines */ 3030 if (priv->lpi_irq > 0) { 3031 ret = request_irq(priv->lpi_irq, stmmac_interrupt, IRQF_SHARED, 3032 dev->name, dev); 3033 if (unlikely(ret < 0)) { 3034 netdev_err(priv->dev, 3035 "%s: ERROR: allocating the LPI IRQ %d (%d)\n", 3036 __func__, priv->lpi_irq, ret); 3037 goto lpiirq_error; 3038 } 3039 } 3040 3041 stmmac_enable_all_queues(priv); 3042 netif_tx_start_all_queues(priv->dev); 3043 3044 return 0; 3045 3046 lpiirq_error: 3047 if (priv->wol_irq != dev->irq) 3048 free_irq(priv->wol_irq, dev); 3049 wolirq_error: 3050 free_irq(dev->irq, dev); 3051 irq_error: 3052 phylink_stop(priv->phylink); 3053 3054 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) 3055 hrtimer_cancel(&priv->tx_queue[chan].txtimer); 3056 3057 stmmac_hw_teardown(dev); 3058 init_error: 3059 free_dma_desc_resources(priv); 3060 dma_desc_error: 3061 phylink_disconnect_phy(priv->phylink); 3062 init_phy_error: 3063 pm_runtime_put(priv->device); 3064 return ret; 3065 } 3066 3067 /** 3068 * stmmac_release - close entry point of the driver 3069 * @dev : device pointer. 3070 * Description: 3071 * This is the stop entry point of the driver. 3072 */ 3073 static int stmmac_release(struct net_device *dev) 3074 { 3075 struct stmmac_priv *priv = netdev_priv(dev); 3076 u32 chan; 3077 3078 if (device_may_wakeup(priv->device)) 3079 phylink_speed_down(priv->phylink, false); 3080 /* Stop and disconnect the PHY */ 3081 phylink_stop(priv->phylink); 3082 phylink_disconnect_phy(priv->phylink); 3083 3084 stmmac_disable_all_queues(priv); 3085 3086 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) 3087 hrtimer_cancel(&priv->tx_queue[chan].txtimer); 3088 3089 /* Free the IRQ lines */ 3090 free_irq(dev->irq, dev); 3091 if (priv->wol_irq != dev->irq) 3092 free_irq(priv->wol_irq, dev); 3093 if (priv->lpi_irq > 0) 3094 free_irq(priv->lpi_irq, dev); 3095 3096 if (priv->eee_enabled) { 3097 priv->tx_path_in_lpi_mode = false; 3098 del_timer_sync(&priv->eee_ctrl_timer); 3099 } 3100 3101 /* Stop TX/RX DMA and clear the descriptors */ 3102 stmmac_stop_all_dma(priv); 3103 3104 /* Release and free the Rx/Tx resources */ 3105 free_dma_desc_resources(priv); 3106 3107 /* Disable the MAC Rx/Tx */ 3108 stmmac_mac_set(priv, priv->ioaddr, false); 3109 3110 netif_carrier_off(dev); 3111 3112 stmmac_release_ptp(priv); 3113 3114 pm_runtime_put(priv->device); 3115 3116 return 0; 3117 } 3118 3119 static bool stmmac_vlan_insert(struct stmmac_priv *priv, struct sk_buff *skb, 3120 struct stmmac_tx_queue *tx_q) 3121 { 3122 u16 tag = 0x0, inner_tag = 0x0; 3123 u32 inner_type = 0x0; 3124 struct dma_desc *p; 3125 3126 if (!priv->dma_cap.vlins) 3127 return false; 3128 if (!skb_vlan_tag_present(skb)) 3129 return false; 3130 if (skb->vlan_proto == htons(ETH_P_8021AD)) { 3131 inner_tag = skb_vlan_tag_get(skb); 3132 inner_type = STMMAC_VLAN_INSERT; 3133 } 3134 3135 tag = skb_vlan_tag_get(skb); 3136 3137 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3138 p = &tx_q->dma_entx[tx_q->cur_tx].basic; 3139 else 3140 p = &tx_q->dma_tx[tx_q->cur_tx]; 3141 3142 if (stmmac_set_desc_vlan_tag(priv, p, tag, inner_tag, inner_type)) 3143 return false; 3144 3145 stmmac_set_tx_owner(priv, p); 3146 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size); 3147 return true; 3148 } 3149 3150 /** 3151 * stmmac_tso_allocator - close entry point of the driver 3152 * @priv: driver private structure 3153 * @des: buffer start address 3154 * @total_len: total length to fill in descriptors 3155 * @last_segment: condition for the last descriptor 3156 * @queue: TX queue index 3157 * Description: 3158 * This function fills descriptor and request new descriptors according to 3159 * buffer length to fill 3160 */ 3161 static void stmmac_tso_allocator(struct stmmac_priv *priv, dma_addr_t des, 3162 int total_len, bool last_segment, u32 queue) 3163 { 3164 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 3165 struct dma_desc *desc; 3166 u32 buff_size; 3167 int tmp_len; 3168 3169 tmp_len = total_len; 3170 3171 while (tmp_len > 0) { 3172 dma_addr_t curr_addr; 3173 3174 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, 3175 priv->dma_tx_size); 3176 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]); 3177 3178 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3179 desc = &tx_q->dma_entx[tx_q->cur_tx].basic; 3180 else 3181 desc = &tx_q->dma_tx[tx_q->cur_tx]; 3182 3183 curr_addr = des + (total_len - tmp_len); 3184 if (priv->dma_cap.addr64 <= 32) 3185 desc->des0 = cpu_to_le32(curr_addr); 3186 else 3187 stmmac_set_desc_addr(priv, desc, curr_addr); 3188 3189 buff_size = tmp_len >= TSO_MAX_BUFF_SIZE ? 3190 TSO_MAX_BUFF_SIZE : tmp_len; 3191 3192 stmmac_prepare_tso_tx_desc(priv, desc, 0, buff_size, 3193 0, 1, 3194 (last_segment) && (tmp_len <= TSO_MAX_BUFF_SIZE), 3195 0, 0); 3196 3197 tmp_len -= TSO_MAX_BUFF_SIZE; 3198 } 3199 } 3200 3201 /** 3202 * stmmac_tso_xmit - Tx entry point of the driver for oversized frames (TSO) 3203 * @skb : the socket buffer 3204 * @dev : device pointer 3205 * Description: this is the transmit function that is called on TSO frames 3206 * (support available on GMAC4 and newer chips). 3207 * Diagram below show the ring programming in case of TSO frames: 3208 * 3209 * First Descriptor 3210 * -------- 3211 * | DES0 |---> buffer1 = L2/L3/L4 header 3212 * | DES1 |---> TCP Payload (can continue on next descr...) 3213 * | DES2 |---> buffer 1 and 2 len 3214 * | DES3 |---> must set TSE, TCP hdr len-> [22:19]. TCP payload len [17:0] 3215 * -------- 3216 * | 3217 * ... 3218 * | 3219 * -------- 3220 * | DES0 | --| Split TCP Payload on Buffers 1 and 2 3221 * | DES1 | --| 3222 * | DES2 | --> buffer 1 and 2 len 3223 * | DES3 | 3224 * -------- 3225 * 3226 * mss is fixed when enable tso, so w/o programming the TDES3 ctx field. 3227 */ 3228 static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev) 3229 { 3230 struct dma_desc *desc, *first, *mss_desc = NULL; 3231 struct stmmac_priv *priv = netdev_priv(dev); 3232 int desc_size, tmp_pay_len = 0, first_tx; 3233 int nfrags = skb_shinfo(skb)->nr_frags; 3234 u32 queue = skb_get_queue_mapping(skb); 3235 unsigned int first_entry, tx_packets; 3236 struct stmmac_tx_queue *tx_q; 3237 bool has_vlan, set_ic; 3238 u8 proto_hdr_len, hdr; 3239 u32 pay_len, mss; 3240 dma_addr_t des; 3241 int i; 3242 3243 tx_q = &priv->tx_queue[queue]; 3244 first_tx = tx_q->cur_tx; 3245 3246 /* Compute header lengths */ 3247 if (skb_shinfo(skb)->gso_type & SKB_GSO_UDP_L4) { 3248 proto_hdr_len = skb_transport_offset(skb) + sizeof(struct udphdr); 3249 hdr = sizeof(struct udphdr); 3250 } else { 3251 proto_hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 3252 hdr = tcp_hdrlen(skb); 3253 } 3254 3255 /* Desc availability based on threshold should be enough safe */ 3256 if (unlikely(stmmac_tx_avail(priv, queue) < 3257 (((skb->len - proto_hdr_len) / TSO_MAX_BUFF_SIZE + 1)))) { 3258 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) { 3259 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, 3260 queue)); 3261 /* This is a hard error, log it. */ 3262 netdev_err(priv->dev, 3263 "%s: Tx Ring full when queue awake\n", 3264 __func__); 3265 } 3266 return NETDEV_TX_BUSY; 3267 } 3268 3269 pay_len = skb_headlen(skb) - proto_hdr_len; /* no frags */ 3270 3271 mss = skb_shinfo(skb)->gso_size; 3272 3273 /* set new MSS value if needed */ 3274 if (mss != tx_q->mss) { 3275 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3276 mss_desc = &tx_q->dma_entx[tx_q->cur_tx].basic; 3277 else 3278 mss_desc = &tx_q->dma_tx[tx_q->cur_tx]; 3279 3280 stmmac_set_mss(priv, mss_desc, mss); 3281 tx_q->mss = mss; 3282 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, 3283 priv->dma_tx_size); 3284 WARN_ON(tx_q->tx_skbuff[tx_q->cur_tx]); 3285 } 3286 3287 if (netif_msg_tx_queued(priv)) { 3288 pr_info("%s: hdrlen %d, hdr_len %d, pay_len %d, mss %d\n", 3289 __func__, hdr, proto_hdr_len, pay_len, mss); 3290 pr_info("\tskb->len %d, skb->data_len %d\n", skb->len, 3291 skb->data_len); 3292 } 3293 3294 /* Check if VLAN can be inserted by HW */ 3295 has_vlan = stmmac_vlan_insert(priv, skb, tx_q); 3296 3297 first_entry = tx_q->cur_tx; 3298 WARN_ON(tx_q->tx_skbuff[first_entry]); 3299 3300 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3301 desc = &tx_q->dma_entx[first_entry].basic; 3302 else 3303 desc = &tx_q->dma_tx[first_entry]; 3304 first = desc; 3305 3306 if (has_vlan) 3307 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT); 3308 3309 /* first descriptor: fill Headers on Buf1 */ 3310 des = dma_map_single(priv->device, skb->data, skb_headlen(skb), 3311 DMA_TO_DEVICE); 3312 if (dma_mapping_error(priv->device, des)) 3313 goto dma_map_err; 3314 3315 tx_q->tx_skbuff_dma[first_entry].buf = des; 3316 tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb); 3317 3318 if (priv->dma_cap.addr64 <= 32) { 3319 first->des0 = cpu_to_le32(des); 3320 3321 /* Fill start of payload in buff2 of first descriptor */ 3322 if (pay_len) 3323 first->des1 = cpu_to_le32(des + proto_hdr_len); 3324 3325 /* If needed take extra descriptors to fill the remaining payload */ 3326 tmp_pay_len = pay_len - TSO_MAX_BUFF_SIZE; 3327 } else { 3328 stmmac_set_desc_addr(priv, first, des); 3329 tmp_pay_len = pay_len; 3330 des += proto_hdr_len; 3331 pay_len = 0; 3332 } 3333 3334 stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue); 3335 3336 /* Prepare fragments */ 3337 for (i = 0; i < nfrags; i++) { 3338 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3339 3340 des = skb_frag_dma_map(priv->device, frag, 0, 3341 skb_frag_size(frag), 3342 DMA_TO_DEVICE); 3343 if (dma_mapping_error(priv->device, des)) 3344 goto dma_map_err; 3345 3346 stmmac_tso_allocator(priv, des, skb_frag_size(frag), 3347 (i == nfrags - 1), queue); 3348 3349 tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des; 3350 tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_frag_size(frag); 3351 tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = true; 3352 } 3353 3354 tx_q->tx_skbuff_dma[tx_q->cur_tx].last_segment = true; 3355 3356 /* Only the last descriptor gets to point to the skb. */ 3357 tx_q->tx_skbuff[tx_q->cur_tx] = skb; 3358 3359 /* Manage tx mitigation */ 3360 tx_packets = (tx_q->cur_tx + 1) - first_tx; 3361 tx_q->tx_count_frames += tx_packets; 3362 3363 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en) 3364 set_ic = true; 3365 else if (!priv->tx_coal_frames) 3366 set_ic = false; 3367 else if (tx_packets > priv->tx_coal_frames) 3368 set_ic = true; 3369 else if ((tx_q->tx_count_frames % priv->tx_coal_frames) < tx_packets) 3370 set_ic = true; 3371 else 3372 set_ic = false; 3373 3374 if (set_ic) { 3375 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3376 desc = &tx_q->dma_entx[tx_q->cur_tx].basic; 3377 else 3378 desc = &tx_q->dma_tx[tx_q->cur_tx]; 3379 3380 tx_q->tx_count_frames = 0; 3381 stmmac_set_tx_ic(priv, desc); 3382 priv->xstats.tx_set_ic_bit++; 3383 } 3384 3385 /* We've used all descriptors we need for this skb, however, 3386 * advance cur_tx so that it references a fresh descriptor. 3387 * ndo_start_xmit will fill this descriptor the next time it's 3388 * called and stmmac_tx_clean may clean up to this descriptor. 3389 */ 3390 tx_q->cur_tx = STMMAC_GET_ENTRY(tx_q->cur_tx, priv->dma_tx_size); 3391 3392 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) { 3393 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n", 3394 __func__); 3395 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue)); 3396 } 3397 3398 dev->stats.tx_bytes += skb->len; 3399 priv->xstats.tx_tso_frames++; 3400 priv->xstats.tx_tso_nfrags += nfrags; 3401 3402 if (priv->sarc_type) 3403 stmmac_set_desc_sarc(priv, first, priv->sarc_type); 3404 3405 skb_tx_timestamp(skb); 3406 3407 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 3408 priv->hwts_tx_en)) { 3409 /* declare that device is doing timestamping */ 3410 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 3411 stmmac_enable_tx_timestamp(priv, first); 3412 } 3413 3414 /* Complete the first descriptor before granting the DMA */ 3415 stmmac_prepare_tso_tx_desc(priv, first, 1, 3416 proto_hdr_len, 3417 pay_len, 3418 1, tx_q->tx_skbuff_dma[first_entry].last_segment, 3419 hdr / 4, (skb->len - proto_hdr_len)); 3420 3421 /* If context desc is used to change MSS */ 3422 if (mss_desc) { 3423 /* Make sure that first descriptor has been completely 3424 * written, including its own bit. This is because MSS is 3425 * actually before first descriptor, so we need to make 3426 * sure that MSS's own bit is the last thing written. 3427 */ 3428 dma_wmb(); 3429 stmmac_set_tx_owner(priv, mss_desc); 3430 } 3431 3432 /* The own bit must be the latest setting done when prepare the 3433 * descriptor and then barrier is needed to make sure that 3434 * all is coherent before granting the DMA engine. 3435 */ 3436 wmb(); 3437 3438 if (netif_msg_pktdata(priv)) { 3439 pr_info("%s: curr=%d dirty=%d f=%d, e=%d, f_p=%p, nfrags %d\n", 3440 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry, 3441 tx_q->cur_tx, first, nfrags); 3442 pr_info(">>> frame to be transmitted: "); 3443 print_pkt(skb->data, skb_headlen(skb)); 3444 } 3445 3446 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len); 3447 3448 if (tx_q->tbs & STMMAC_TBS_AVAIL) 3449 desc_size = sizeof(struct dma_edesc); 3450 else 3451 desc_size = sizeof(struct dma_desc); 3452 3453 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size); 3454 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue); 3455 stmmac_tx_timer_arm(priv, queue); 3456 3457 return NETDEV_TX_OK; 3458 3459 dma_map_err: 3460 dev_err(priv->device, "Tx dma map failed\n"); 3461 dev_kfree_skb(skb); 3462 priv->dev->stats.tx_dropped++; 3463 return NETDEV_TX_OK; 3464 } 3465 3466 /** 3467 * stmmac_xmit - Tx entry point of the driver 3468 * @skb : the socket buffer 3469 * @dev : device pointer 3470 * Description : this is the tx entry point of the driver. 3471 * It programs the chain or the ring and supports oversized frames 3472 * and SG feature. 3473 */ 3474 static netdev_tx_t stmmac_xmit(struct sk_buff *skb, struct net_device *dev) 3475 { 3476 unsigned int first_entry, tx_packets, enh_desc; 3477 struct stmmac_priv *priv = netdev_priv(dev); 3478 unsigned int nopaged_len = skb_headlen(skb); 3479 int i, csum_insertion = 0, is_jumbo = 0; 3480 u32 queue = skb_get_queue_mapping(skb); 3481 int nfrags = skb_shinfo(skb)->nr_frags; 3482 int gso = skb_shinfo(skb)->gso_type; 3483 struct dma_edesc *tbs_desc = NULL; 3484 int entry, desc_size, first_tx; 3485 struct dma_desc *desc, *first; 3486 struct stmmac_tx_queue *tx_q; 3487 bool has_vlan, set_ic; 3488 dma_addr_t des; 3489 3490 tx_q = &priv->tx_queue[queue]; 3491 first_tx = tx_q->cur_tx; 3492 3493 if (priv->tx_path_in_lpi_mode && priv->eee_sw_timer_en) 3494 stmmac_disable_eee_mode(priv); 3495 3496 /* Manage oversized TCP frames for GMAC4 device */ 3497 if (skb_is_gso(skb) && priv->tso) { 3498 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) 3499 return stmmac_tso_xmit(skb, dev); 3500 if (priv->plat->has_gmac4 && (gso & SKB_GSO_UDP_L4)) 3501 return stmmac_tso_xmit(skb, dev); 3502 } 3503 3504 if (unlikely(stmmac_tx_avail(priv, queue) < nfrags + 1)) { 3505 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, queue))) { 3506 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, 3507 queue)); 3508 /* This is a hard error, log it. */ 3509 netdev_err(priv->dev, 3510 "%s: Tx Ring full when queue awake\n", 3511 __func__); 3512 } 3513 return NETDEV_TX_BUSY; 3514 } 3515 3516 /* Check if VLAN can be inserted by HW */ 3517 has_vlan = stmmac_vlan_insert(priv, skb, tx_q); 3518 3519 entry = tx_q->cur_tx; 3520 first_entry = entry; 3521 WARN_ON(tx_q->tx_skbuff[first_entry]); 3522 3523 csum_insertion = (skb->ip_summed == CHECKSUM_PARTIAL); 3524 3525 if (likely(priv->extend_desc)) 3526 desc = (struct dma_desc *)(tx_q->dma_etx + entry); 3527 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 3528 desc = &tx_q->dma_entx[entry].basic; 3529 else 3530 desc = tx_q->dma_tx + entry; 3531 3532 first = desc; 3533 3534 if (has_vlan) 3535 stmmac_set_desc_vlan(priv, first, STMMAC_VLAN_INSERT); 3536 3537 enh_desc = priv->plat->enh_desc; 3538 /* To program the descriptors according to the size of the frame */ 3539 if (enh_desc) 3540 is_jumbo = stmmac_is_jumbo_frm(priv, skb->len, enh_desc); 3541 3542 if (unlikely(is_jumbo)) { 3543 entry = stmmac_jumbo_frm(priv, tx_q, skb, csum_insertion); 3544 if (unlikely(entry < 0) && (entry != -EINVAL)) 3545 goto dma_map_err; 3546 } 3547 3548 for (i = 0; i < nfrags; i++) { 3549 const skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 3550 int len = skb_frag_size(frag); 3551 bool last_segment = (i == (nfrags - 1)); 3552 3553 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size); 3554 WARN_ON(tx_q->tx_skbuff[entry]); 3555 3556 if (likely(priv->extend_desc)) 3557 desc = (struct dma_desc *)(tx_q->dma_etx + entry); 3558 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 3559 desc = &tx_q->dma_entx[entry].basic; 3560 else 3561 desc = tx_q->dma_tx + entry; 3562 3563 des = skb_frag_dma_map(priv->device, frag, 0, len, 3564 DMA_TO_DEVICE); 3565 if (dma_mapping_error(priv->device, des)) 3566 goto dma_map_err; /* should reuse desc w/o issues */ 3567 3568 tx_q->tx_skbuff_dma[entry].buf = des; 3569 3570 stmmac_set_desc_addr(priv, desc, des); 3571 3572 tx_q->tx_skbuff_dma[entry].map_as_page = true; 3573 tx_q->tx_skbuff_dma[entry].len = len; 3574 tx_q->tx_skbuff_dma[entry].last_segment = last_segment; 3575 3576 /* Prepare the descriptor and set the own bit too */ 3577 stmmac_prepare_tx_desc(priv, desc, 0, len, csum_insertion, 3578 priv->mode, 1, last_segment, skb->len); 3579 } 3580 3581 /* Only the last descriptor gets to point to the skb. */ 3582 tx_q->tx_skbuff[entry] = skb; 3583 3584 /* According to the coalesce parameter the IC bit for the latest 3585 * segment is reset and the timer re-started to clean the tx status. 3586 * This approach takes care about the fragments: desc is the first 3587 * element in case of no SG. 3588 */ 3589 tx_packets = (entry + 1) - first_tx; 3590 tx_q->tx_count_frames += tx_packets; 3591 3592 if ((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && priv->hwts_tx_en) 3593 set_ic = true; 3594 else if (!priv->tx_coal_frames) 3595 set_ic = false; 3596 else if (tx_packets > priv->tx_coal_frames) 3597 set_ic = true; 3598 else if ((tx_q->tx_count_frames % priv->tx_coal_frames) < tx_packets) 3599 set_ic = true; 3600 else 3601 set_ic = false; 3602 3603 if (set_ic) { 3604 if (likely(priv->extend_desc)) 3605 desc = &tx_q->dma_etx[entry].basic; 3606 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 3607 desc = &tx_q->dma_entx[entry].basic; 3608 else 3609 desc = &tx_q->dma_tx[entry]; 3610 3611 tx_q->tx_count_frames = 0; 3612 stmmac_set_tx_ic(priv, desc); 3613 priv->xstats.tx_set_ic_bit++; 3614 } 3615 3616 /* We've used all descriptors we need for this skb, however, 3617 * advance cur_tx so that it references a fresh descriptor. 3618 * ndo_start_xmit will fill this descriptor the next time it's 3619 * called and stmmac_tx_clean may clean up to this descriptor. 3620 */ 3621 entry = STMMAC_GET_ENTRY(entry, priv->dma_tx_size); 3622 tx_q->cur_tx = entry; 3623 3624 if (netif_msg_pktdata(priv)) { 3625 netdev_dbg(priv->dev, 3626 "%s: curr=%d dirty=%d f=%d, e=%d, first=%p, nfrags=%d", 3627 __func__, tx_q->cur_tx, tx_q->dirty_tx, first_entry, 3628 entry, first, nfrags); 3629 3630 netdev_dbg(priv->dev, ">>> frame to be transmitted: "); 3631 print_pkt(skb->data, skb->len); 3632 } 3633 3634 if (unlikely(stmmac_tx_avail(priv, queue) <= (MAX_SKB_FRAGS + 1))) { 3635 netif_dbg(priv, hw, priv->dev, "%s: stop transmitted packets\n", 3636 __func__); 3637 netif_tx_stop_queue(netdev_get_tx_queue(priv->dev, queue)); 3638 } 3639 3640 dev->stats.tx_bytes += skb->len; 3641 3642 if (priv->sarc_type) 3643 stmmac_set_desc_sarc(priv, first, priv->sarc_type); 3644 3645 skb_tx_timestamp(skb); 3646 3647 /* Ready to fill the first descriptor and set the OWN bit w/o any 3648 * problems because all the descriptors are actually ready to be 3649 * passed to the DMA engine. 3650 */ 3651 if (likely(!is_jumbo)) { 3652 bool last_segment = (nfrags == 0); 3653 3654 des = dma_map_single(priv->device, skb->data, 3655 nopaged_len, DMA_TO_DEVICE); 3656 if (dma_mapping_error(priv->device, des)) 3657 goto dma_map_err; 3658 3659 tx_q->tx_skbuff_dma[first_entry].buf = des; 3660 3661 stmmac_set_desc_addr(priv, first, des); 3662 3663 tx_q->tx_skbuff_dma[first_entry].len = nopaged_len; 3664 tx_q->tx_skbuff_dma[first_entry].last_segment = last_segment; 3665 3666 if (unlikely((skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) && 3667 priv->hwts_tx_en)) { 3668 /* declare that device is doing timestamping */ 3669 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 3670 stmmac_enable_tx_timestamp(priv, first); 3671 } 3672 3673 /* Prepare the first descriptor setting the OWN bit too */ 3674 stmmac_prepare_tx_desc(priv, first, 1, nopaged_len, 3675 csum_insertion, priv->mode, 0, last_segment, 3676 skb->len); 3677 } 3678 3679 if (tx_q->tbs & STMMAC_TBS_EN) { 3680 struct timespec64 ts = ns_to_timespec64(skb->tstamp); 3681 3682 tbs_desc = &tx_q->dma_entx[first_entry]; 3683 stmmac_set_desc_tbs(priv, tbs_desc, ts.tv_sec, ts.tv_nsec); 3684 } 3685 3686 stmmac_set_tx_owner(priv, first); 3687 3688 /* The own bit must be the latest setting done when prepare the 3689 * descriptor and then barrier is needed to make sure that 3690 * all is coherent before granting the DMA engine. 3691 */ 3692 wmb(); 3693 3694 netdev_tx_sent_queue(netdev_get_tx_queue(dev, queue), skb->len); 3695 3696 stmmac_enable_dma_transmission(priv, priv->ioaddr); 3697 3698 if (likely(priv->extend_desc)) 3699 desc_size = sizeof(struct dma_extended_desc); 3700 else if (tx_q->tbs & STMMAC_TBS_AVAIL) 3701 desc_size = sizeof(struct dma_edesc); 3702 else 3703 desc_size = sizeof(struct dma_desc); 3704 3705 tx_q->tx_tail_addr = tx_q->dma_tx_phy + (tx_q->cur_tx * desc_size); 3706 stmmac_set_tx_tail_ptr(priv, priv->ioaddr, tx_q->tx_tail_addr, queue); 3707 stmmac_tx_timer_arm(priv, queue); 3708 3709 return NETDEV_TX_OK; 3710 3711 dma_map_err: 3712 netdev_err(priv->dev, "Tx DMA map failed\n"); 3713 dev_kfree_skb(skb); 3714 priv->dev->stats.tx_dropped++; 3715 return NETDEV_TX_OK; 3716 } 3717 3718 static void stmmac_rx_vlan(struct net_device *dev, struct sk_buff *skb) 3719 { 3720 struct vlan_ethhdr *veth; 3721 __be16 vlan_proto; 3722 u16 vlanid; 3723 3724 veth = (struct vlan_ethhdr *)skb->data; 3725 vlan_proto = veth->h_vlan_proto; 3726 3727 if ((vlan_proto == htons(ETH_P_8021Q) && 3728 dev->features & NETIF_F_HW_VLAN_CTAG_RX) || 3729 (vlan_proto == htons(ETH_P_8021AD) && 3730 dev->features & NETIF_F_HW_VLAN_STAG_RX)) { 3731 /* pop the vlan tag */ 3732 vlanid = ntohs(veth->h_vlan_TCI); 3733 memmove(skb->data + VLAN_HLEN, veth, ETH_ALEN * 2); 3734 skb_pull(skb, VLAN_HLEN); 3735 __vlan_hwaccel_put_tag(skb, vlan_proto, vlanid); 3736 } 3737 } 3738 3739 /** 3740 * stmmac_rx_refill - refill used skb preallocated buffers 3741 * @priv: driver private structure 3742 * @queue: RX queue index 3743 * Description : this is to reallocate the skb for the reception process 3744 * that is based on zero-copy. 3745 */ 3746 static inline void stmmac_rx_refill(struct stmmac_priv *priv, u32 queue) 3747 { 3748 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 3749 int len, dirty = stmmac_rx_dirty(priv, queue); 3750 unsigned int entry = rx_q->dirty_rx; 3751 3752 len = DIV_ROUND_UP(priv->dma_buf_sz, PAGE_SIZE) * PAGE_SIZE; 3753 3754 while (dirty-- > 0) { 3755 struct stmmac_rx_buffer *buf = &rx_q->buf_pool[entry]; 3756 struct dma_desc *p; 3757 bool use_rx_wd; 3758 3759 if (priv->extend_desc) 3760 p = (struct dma_desc *)(rx_q->dma_erx + entry); 3761 else 3762 p = rx_q->dma_rx + entry; 3763 3764 if (!buf->page) { 3765 buf->page = page_pool_dev_alloc_pages(rx_q->page_pool); 3766 if (!buf->page) 3767 break; 3768 } 3769 3770 if (priv->sph && !buf->sec_page) { 3771 buf->sec_page = page_pool_dev_alloc_pages(rx_q->page_pool); 3772 if (!buf->sec_page) 3773 break; 3774 3775 buf->sec_addr = page_pool_get_dma_addr(buf->sec_page); 3776 3777 dma_sync_single_for_device(priv->device, buf->sec_addr, 3778 len, DMA_FROM_DEVICE); 3779 } 3780 3781 buf->addr = page_pool_get_dma_addr(buf->page); 3782 3783 /* Sync whole allocation to device. This will invalidate old 3784 * data. 3785 */ 3786 dma_sync_single_for_device(priv->device, buf->addr, len, 3787 DMA_FROM_DEVICE); 3788 3789 stmmac_set_desc_addr(priv, p, buf->addr); 3790 if (priv->sph) 3791 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, true); 3792 else 3793 stmmac_set_desc_sec_addr(priv, p, buf->sec_addr, false); 3794 stmmac_refill_desc3(priv, rx_q, p); 3795 3796 rx_q->rx_count_frames++; 3797 rx_q->rx_count_frames += priv->rx_coal_frames; 3798 if (rx_q->rx_count_frames > priv->rx_coal_frames) 3799 rx_q->rx_count_frames = 0; 3800 3801 use_rx_wd = !priv->rx_coal_frames; 3802 use_rx_wd |= rx_q->rx_count_frames > 0; 3803 if (!priv->use_riwt) 3804 use_rx_wd = false; 3805 3806 dma_wmb(); 3807 stmmac_set_rx_owner(priv, p, use_rx_wd); 3808 3809 entry = STMMAC_GET_ENTRY(entry, priv->dma_rx_size); 3810 } 3811 rx_q->dirty_rx = entry; 3812 rx_q->rx_tail_addr = rx_q->dma_rx_phy + 3813 (rx_q->dirty_rx * sizeof(struct dma_desc)); 3814 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, rx_q->rx_tail_addr, queue); 3815 } 3816 3817 static unsigned int stmmac_rx_buf1_len(struct stmmac_priv *priv, 3818 struct dma_desc *p, 3819 int status, unsigned int len) 3820 { 3821 unsigned int plen = 0, hlen = 0; 3822 int coe = priv->hw->rx_csum; 3823 3824 /* Not first descriptor, buffer is always zero */ 3825 if (priv->sph && len) 3826 return 0; 3827 3828 /* First descriptor, get split header length */ 3829 stmmac_get_rx_header_len(priv, p, &hlen); 3830 if (priv->sph && hlen) { 3831 priv->xstats.rx_split_hdr_pkt_n++; 3832 return hlen; 3833 } 3834 3835 /* First descriptor, not last descriptor and not split header */ 3836 if (status & rx_not_ls) 3837 return priv->dma_buf_sz; 3838 3839 plen = stmmac_get_rx_frame_len(priv, p, coe); 3840 3841 /* First descriptor and last descriptor and not split header */ 3842 return min_t(unsigned int, priv->dma_buf_sz, plen); 3843 } 3844 3845 static unsigned int stmmac_rx_buf2_len(struct stmmac_priv *priv, 3846 struct dma_desc *p, 3847 int status, unsigned int len) 3848 { 3849 int coe = priv->hw->rx_csum; 3850 unsigned int plen = 0; 3851 3852 /* Not split header, buffer is not available */ 3853 if (!priv->sph) 3854 return 0; 3855 3856 /* Not last descriptor */ 3857 if (status & rx_not_ls) 3858 return priv->dma_buf_sz; 3859 3860 plen = stmmac_get_rx_frame_len(priv, p, coe); 3861 3862 /* Last descriptor */ 3863 return plen - len; 3864 } 3865 3866 /** 3867 * stmmac_rx - manage the receive process 3868 * @priv: driver private structure 3869 * @limit: napi bugget 3870 * @queue: RX queue index. 3871 * Description : this the function called by the napi poll method. 3872 * It gets all the frames inside the ring. 3873 */ 3874 static int stmmac_rx(struct stmmac_priv *priv, int limit, u32 queue) 3875 { 3876 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 3877 struct stmmac_channel *ch = &priv->channel[queue]; 3878 unsigned int count = 0, error = 0, len = 0; 3879 int status = 0, coe = priv->hw->rx_csum; 3880 unsigned int next_entry = rx_q->cur_rx; 3881 unsigned int desc_size; 3882 struct sk_buff *skb = NULL; 3883 3884 if (netif_msg_rx_status(priv)) { 3885 void *rx_head; 3886 3887 netdev_dbg(priv->dev, "%s: descriptor ring:\n", __func__); 3888 if (priv->extend_desc) { 3889 rx_head = (void *)rx_q->dma_erx; 3890 desc_size = sizeof(struct dma_extended_desc); 3891 } else { 3892 rx_head = (void *)rx_q->dma_rx; 3893 desc_size = sizeof(struct dma_desc); 3894 } 3895 3896 stmmac_display_ring(priv, rx_head, priv->dma_rx_size, true, 3897 rx_q->dma_rx_phy, desc_size); 3898 } 3899 while (count < limit) { 3900 unsigned int buf1_len = 0, buf2_len = 0; 3901 enum pkt_hash_types hash_type; 3902 struct stmmac_rx_buffer *buf; 3903 struct dma_desc *np, *p; 3904 int entry; 3905 u32 hash; 3906 3907 if (!count && rx_q->state_saved) { 3908 skb = rx_q->state.skb; 3909 error = rx_q->state.error; 3910 len = rx_q->state.len; 3911 } else { 3912 rx_q->state_saved = false; 3913 skb = NULL; 3914 error = 0; 3915 len = 0; 3916 } 3917 3918 if (count >= limit) 3919 break; 3920 3921 read_again: 3922 buf1_len = 0; 3923 buf2_len = 0; 3924 entry = next_entry; 3925 buf = &rx_q->buf_pool[entry]; 3926 3927 if (priv->extend_desc) 3928 p = (struct dma_desc *)(rx_q->dma_erx + entry); 3929 else 3930 p = rx_q->dma_rx + entry; 3931 3932 /* read the status of the incoming frame */ 3933 status = stmmac_rx_status(priv, &priv->dev->stats, 3934 &priv->xstats, p); 3935 /* check if managed by the DMA otherwise go ahead */ 3936 if (unlikely(status & dma_own)) 3937 break; 3938 3939 rx_q->cur_rx = STMMAC_GET_ENTRY(rx_q->cur_rx, 3940 priv->dma_rx_size); 3941 next_entry = rx_q->cur_rx; 3942 3943 if (priv->extend_desc) 3944 np = (struct dma_desc *)(rx_q->dma_erx + next_entry); 3945 else 3946 np = rx_q->dma_rx + next_entry; 3947 3948 prefetch(np); 3949 3950 if (priv->extend_desc) 3951 stmmac_rx_extended_status(priv, &priv->dev->stats, 3952 &priv->xstats, rx_q->dma_erx + entry); 3953 if (unlikely(status == discard_frame)) { 3954 page_pool_recycle_direct(rx_q->page_pool, buf->page); 3955 buf->page = NULL; 3956 error = 1; 3957 if (!priv->hwts_rx_en) 3958 priv->dev->stats.rx_errors++; 3959 } 3960 3961 if (unlikely(error && (status & rx_not_ls))) 3962 goto read_again; 3963 if (unlikely(error)) { 3964 dev_kfree_skb(skb); 3965 skb = NULL; 3966 count++; 3967 continue; 3968 } 3969 3970 /* Buffer is good. Go on. */ 3971 3972 prefetch(page_address(buf->page)); 3973 if (buf->sec_page) 3974 prefetch(page_address(buf->sec_page)); 3975 3976 buf1_len = stmmac_rx_buf1_len(priv, p, status, len); 3977 len += buf1_len; 3978 buf2_len = stmmac_rx_buf2_len(priv, p, status, len); 3979 len += buf2_len; 3980 3981 /* ACS is set; GMAC core strips PAD/FCS for IEEE 802.3 3982 * Type frames (LLC/LLC-SNAP) 3983 * 3984 * llc_snap is never checked in GMAC >= 4, so this ACS 3985 * feature is always disabled and packets need to be 3986 * stripped manually. 3987 */ 3988 if (likely(!(status & rx_not_ls)) && 3989 (likely(priv->synopsys_id >= DWMAC_CORE_4_00) || 3990 unlikely(status != llc_snap))) { 3991 if (buf2_len) 3992 buf2_len -= ETH_FCS_LEN; 3993 else 3994 buf1_len -= ETH_FCS_LEN; 3995 3996 len -= ETH_FCS_LEN; 3997 } 3998 3999 if (!skb) { 4000 skb = napi_alloc_skb(&ch->rx_napi, buf1_len); 4001 if (!skb) { 4002 priv->dev->stats.rx_dropped++; 4003 count++; 4004 goto drain_data; 4005 } 4006 4007 dma_sync_single_for_cpu(priv->device, buf->addr, 4008 buf1_len, DMA_FROM_DEVICE); 4009 skb_copy_to_linear_data(skb, page_address(buf->page), 4010 buf1_len); 4011 skb_put(skb, buf1_len); 4012 4013 /* Data payload copied into SKB, page ready for recycle */ 4014 page_pool_recycle_direct(rx_q->page_pool, buf->page); 4015 buf->page = NULL; 4016 } else if (buf1_len) { 4017 dma_sync_single_for_cpu(priv->device, buf->addr, 4018 buf1_len, DMA_FROM_DEVICE); 4019 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 4020 buf->page, 0, buf1_len, 4021 priv->dma_buf_sz); 4022 4023 /* Data payload appended into SKB */ 4024 page_pool_release_page(rx_q->page_pool, buf->page); 4025 buf->page = NULL; 4026 } 4027 4028 if (buf2_len) { 4029 dma_sync_single_for_cpu(priv->device, buf->sec_addr, 4030 buf2_len, DMA_FROM_DEVICE); 4031 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, 4032 buf->sec_page, 0, buf2_len, 4033 priv->dma_buf_sz); 4034 4035 /* Data payload appended into SKB */ 4036 page_pool_release_page(rx_q->page_pool, buf->sec_page); 4037 buf->sec_page = NULL; 4038 } 4039 4040 drain_data: 4041 if (likely(status & rx_not_ls)) 4042 goto read_again; 4043 if (!skb) 4044 continue; 4045 4046 /* Got entire packet into SKB. Finish it. */ 4047 4048 stmmac_get_rx_hwtstamp(priv, p, np, skb); 4049 stmmac_rx_vlan(priv->dev, skb); 4050 skb->protocol = eth_type_trans(skb, priv->dev); 4051 4052 if (unlikely(!coe)) 4053 skb_checksum_none_assert(skb); 4054 else 4055 skb->ip_summed = CHECKSUM_UNNECESSARY; 4056 4057 if (!stmmac_get_rx_hash(priv, p, &hash, &hash_type)) 4058 skb_set_hash(skb, hash, hash_type); 4059 4060 skb_record_rx_queue(skb, queue); 4061 napi_gro_receive(&ch->rx_napi, skb); 4062 skb = NULL; 4063 4064 priv->dev->stats.rx_packets++; 4065 priv->dev->stats.rx_bytes += len; 4066 count++; 4067 } 4068 4069 if (status & rx_not_ls || skb) { 4070 rx_q->state_saved = true; 4071 rx_q->state.skb = skb; 4072 rx_q->state.error = error; 4073 rx_q->state.len = len; 4074 } 4075 4076 stmmac_rx_refill(priv, queue); 4077 4078 priv->xstats.rx_pkt_n += count; 4079 4080 return count; 4081 } 4082 4083 static int stmmac_napi_poll_rx(struct napi_struct *napi, int budget) 4084 { 4085 struct stmmac_channel *ch = 4086 container_of(napi, struct stmmac_channel, rx_napi); 4087 struct stmmac_priv *priv = ch->priv_data; 4088 u32 chan = ch->index; 4089 int work_done; 4090 4091 priv->xstats.napi_poll++; 4092 4093 work_done = stmmac_rx(priv, budget, chan); 4094 if (work_done < budget && napi_complete_done(napi, work_done)) { 4095 unsigned long flags; 4096 4097 spin_lock_irqsave(&ch->lock, flags); 4098 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 1, 0); 4099 spin_unlock_irqrestore(&ch->lock, flags); 4100 } 4101 4102 return work_done; 4103 } 4104 4105 static int stmmac_napi_poll_tx(struct napi_struct *napi, int budget) 4106 { 4107 struct stmmac_channel *ch = 4108 container_of(napi, struct stmmac_channel, tx_napi); 4109 struct stmmac_priv *priv = ch->priv_data; 4110 u32 chan = ch->index; 4111 int work_done; 4112 4113 priv->xstats.napi_poll++; 4114 4115 work_done = stmmac_tx_clean(priv, priv->dma_tx_size, chan); 4116 work_done = min(work_done, budget); 4117 4118 if (work_done < budget && napi_complete_done(napi, work_done)) { 4119 unsigned long flags; 4120 4121 spin_lock_irqsave(&ch->lock, flags); 4122 stmmac_enable_dma_irq(priv, priv->ioaddr, chan, 0, 1); 4123 spin_unlock_irqrestore(&ch->lock, flags); 4124 } 4125 4126 return work_done; 4127 } 4128 4129 /** 4130 * stmmac_tx_timeout 4131 * @dev : Pointer to net device structure 4132 * @txqueue: the index of the hanging transmit queue 4133 * Description: this function is called when a packet transmission fails to 4134 * complete within a reasonable time. The driver will mark the error in the 4135 * netdev structure and arrange for the device to be reset to a sane state 4136 * in order to transmit a new packet. 4137 */ 4138 static void stmmac_tx_timeout(struct net_device *dev, unsigned int txqueue) 4139 { 4140 struct stmmac_priv *priv = netdev_priv(dev); 4141 4142 stmmac_global_err(priv); 4143 } 4144 4145 /** 4146 * stmmac_set_rx_mode - entry point for multicast addressing 4147 * @dev : pointer to the device structure 4148 * Description: 4149 * This function is a driver entry point which gets called by the kernel 4150 * whenever multicast addresses must be enabled/disabled. 4151 * Return value: 4152 * void. 4153 */ 4154 static void stmmac_set_rx_mode(struct net_device *dev) 4155 { 4156 struct stmmac_priv *priv = netdev_priv(dev); 4157 4158 stmmac_set_filter(priv, priv->hw, dev); 4159 } 4160 4161 /** 4162 * stmmac_change_mtu - entry point to change MTU size for the device. 4163 * @dev : device pointer. 4164 * @new_mtu : the new MTU size for the device. 4165 * Description: the Maximum Transfer Unit (MTU) is used by the network layer 4166 * to drive packet transmission. Ethernet has an MTU of 1500 octets 4167 * (ETH_DATA_LEN). This value can be changed with ifconfig. 4168 * Return value: 4169 * 0 on success and an appropriate (-)ve integer as defined in errno.h 4170 * file on failure. 4171 */ 4172 static int stmmac_change_mtu(struct net_device *dev, int new_mtu) 4173 { 4174 struct stmmac_priv *priv = netdev_priv(dev); 4175 int txfifosz = priv->plat->tx_fifo_size; 4176 const int mtu = new_mtu; 4177 4178 if (txfifosz == 0) 4179 txfifosz = priv->dma_cap.tx_fifo_size; 4180 4181 txfifosz /= priv->plat->tx_queues_to_use; 4182 4183 if (netif_running(dev)) { 4184 netdev_err(priv->dev, "must be stopped to change its MTU\n"); 4185 return -EBUSY; 4186 } 4187 4188 new_mtu = STMMAC_ALIGN(new_mtu); 4189 4190 /* If condition true, FIFO is too small or MTU too large */ 4191 if ((txfifosz < new_mtu) || (new_mtu > BUF_SIZE_16KiB)) 4192 return -EINVAL; 4193 4194 dev->mtu = mtu; 4195 4196 netdev_update_features(dev); 4197 4198 return 0; 4199 } 4200 4201 static netdev_features_t stmmac_fix_features(struct net_device *dev, 4202 netdev_features_t features) 4203 { 4204 struct stmmac_priv *priv = netdev_priv(dev); 4205 4206 if (priv->plat->rx_coe == STMMAC_RX_COE_NONE) 4207 features &= ~NETIF_F_RXCSUM; 4208 4209 if (!priv->plat->tx_coe) 4210 features &= ~NETIF_F_CSUM_MASK; 4211 4212 /* Some GMAC devices have a bugged Jumbo frame support that 4213 * needs to have the Tx COE disabled for oversized frames 4214 * (due to limited buffer sizes). In this case we disable 4215 * the TX csum insertion in the TDES and not use SF. 4216 */ 4217 if (priv->plat->bugged_jumbo && (dev->mtu > ETH_DATA_LEN)) 4218 features &= ~NETIF_F_CSUM_MASK; 4219 4220 /* Disable tso if asked by ethtool */ 4221 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) { 4222 if (features & NETIF_F_TSO) 4223 priv->tso = true; 4224 else 4225 priv->tso = false; 4226 } 4227 4228 return features; 4229 } 4230 4231 static int stmmac_set_features(struct net_device *netdev, 4232 netdev_features_t features) 4233 { 4234 struct stmmac_priv *priv = netdev_priv(netdev); 4235 bool sph_en; 4236 u32 chan; 4237 4238 /* Keep the COE Type in case of csum is supporting */ 4239 if (features & NETIF_F_RXCSUM) 4240 priv->hw->rx_csum = priv->plat->rx_coe; 4241 else 4242 priv->hw->rx_csum = 0; 4243 /* No check needed because rx_coe has been set before and it will be 4244 * fixed in case of issue. 4245 */ 4246 stmmac_rx_ipc(priv, priv->hw); 4247 4248 sph_en = (priv->hw->rx_csum > 0) && priv->sph; 4249 for (chan = 0; chan < priv->plat->rx_queues_to_use; chan++) 4250 stmmac_enable_sph(priv, priv->ioaddr, sph_en, chan); 4251 4252 return 0; 4253 } 4254 4255 /** 4256 * stmmac_interrupt - main ISR 4257 * @irq: interrupt number. 4258 * @dev_id: to pass the net device pointer (must be valid). 4259 * Description: this is the main driver interrupt service routine. 4260 * It can call: 4261 * o DMA service routine (to manage incoming frame reception and transmission 4262 * status) 4263 * o Core interrupts to manage: remote wake-up, management counter, LPI 4264 * interrupts. 4265 */ 4266 static irqreturn_t stmmac_interrupt(int irq, void *dev_id) 4267 { 4268 struct net_device *dev = (struct net_device *)dev_id; 4269 struct stmmac_priv *priv = netdev_priv(dev); 4270 u32 rx_cnt = priv->plat->rx_queues_to_use; 4271 u32 tx_cnt = priv->plat->tx_queues_to_use; 4272 u32 queues_count; 4273 u32 queue; 4274 bool xmac; 4275 4276 xmac = priv->plat->has_gmac4 || priv->plat->has_xgmac; 4277 queues_count = (rx_cnt > tx_cnt) ? rx_cnt : tx_cnt; 4278 4279 if (priv->irq_wake) 4280 pm_wakeup_event(priv->device, 0); 4281 4282 /* Check if adapter is up */ 4283 if (test_bit(STMMAC_DOWN, &priv->state)) 4284 return IRQ_HANDLED; 4285 /* Check if a fatal error happened */ 4286 if (stmmac_safety_feat_interrupt(priv)) 4287 return IRQ_HANDLED; 4288 4289 /* To handle GMAC own interrupts */ 4290 if ((priv->plat->has_gmac) || xmac) { 4291 int status = stmmac_host_irq_status(priv, priv->hw, &priv->xstats); 4292 int mtl_status; 4293 4294 if (unlikely(status)) { 4295 /* For LPI we need to save the tx status */ 4296 if (status & CORE_IRQ_TX_PATH_IN_LPI_MODE) 4297 priv->tx_path_in_lpi_mode = true; 4298 if (status & CORE_IRQ_TX_PATH_EXIT_LPI_MODE) 4299 priv->tx_path_in_lpi_mode = false; 4300 } 4301 4302 for (queue = 0; queue < queues_count; queue++) { 4303 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 4304 4305 mtl_status = stmmac_host_mtl_irq_status(priv, priv->hw, 4306 queue); 4307 if (mtl_status != -EINVAL) 4308 status |= mtl_status; 4309 4310 if (status & CORE_IRQ_MTL_RX_OVERFLOW) 4311 stmmac_set_rx_tail_ptr(priv, priv->ioaddr, 4312 rx_q->rx_tail_addr, 4313 queue); 4314 } 4315 4316 /* PCS link status */ 4317 if (priv->hw->pcs) { 4318 if (priv->xstats.pcs_link) 4319 netif_carrier_on(dev); 4320 else 4321 netif_carrier_off(dev); 4322 } 4323 } 4324 4325 /* To handle DMA interrupts */ 4326 stmmac_dma_interrupt(priv); 4327 4328 return IRQ_HANDLED; 4329 } 4330 4331 #ifdef CONFIG_NET_POLL_CONTROLLER 4332 /* Polling receive - used by NETCONSOLE and other diagnostic tools 4333 * to allow network I/O with interrupts disabled. 4334 */ 4335 static void stmmac_poll_controller(struct net_device *dev) 4336 { 4337 disable_irq(dev->irq); 4338 stmmac_interrupt(dev->irq, dev); 4339 enable_irq(dev->irq); 4340 } 4341 #endif 4342 4343 /** 4344 * stmmac_ioctl - Entry point for the Ioctl 4345 * @dev: Device pointer. 4346 * @rq: An IOCTL specefic structure, that can contain a pointer to 4347 * a proprietary structure used to pass information to the driver. 4348 * @cmd: IOCTL command 4349 * Description: 4350 * Currently it supports the phy_mii_ioctl(...) and HW time stamping. 4351 */ 4352 static int stmmac_ioctl(struct net_device *dev, struct ifreq *rq, int cmd) 4353 { 4354 struct stmmac_priv *priv = netdev_priv (dev); 4355 int ret = -EOPNOTSUPP; 4356 4357 if (!netif_running(dev)) 4358 return -EINVAL; 4359 4360 switch (cmd) { 4361 case SIOCGMIIPHY: 4362 case SIOCGMIIREG: 4363 case SIOCSMIIREG: 4364 ret = phylink_mii_ioctl(priv->phylink, rq, cmd); 4365 break; 4366 case SIOCSHWTSTAMP: 4367 ret = stmmac_hwtstamp_set(dev, rq); 4368 break; 4369 case SIOCGHWTSTAMP: 4370 ret = stmmac_hwtstamp_get(dev, rq); 4371 break; 4372 default: 4373 break; 4374 } 4375 4376 return ret; 4377 } 4378 4379 static int stmmac_setup_tc_block_cb(enum tc_setup_type type, void *type_data, 4380 void *cb_priv) 4381 { 4382 struct stmmac_priv *priv = cb_priv; 4383 int ret = -EOPNOTSUPP; 4384 4385 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data)) 4386 return ret; 4387 4388 stmmac_disable_all_queues(priv); 4389 4390 switch (type) { 4391 case TC_SETUP_CLSU32: 4392 ret = stmmac_tc_setup_cls_u32(priv, priv, type_data); 4393 break; 4394 case TC_SETUP_CLSFLOWER: 4395 ret = stmmac_tc_setup_cls(priv, priv, type_data); 4396 break; 4397 default: 4398 break; 4399 } 4400 4401 stmmac_enable_all_queues(priv); 4402 return ret; 4403 } 4404 4405 static LIST_HEAD(stmmac_block_cb_list); 4406 4407 static int stmmac_setup_tc(struct net_device *ndev, enum tc_setup_type type, 4408 void *type_data) 4409 { 4410 struct stmmac_priv *priv = netdev_priv(ndev); 4411 4412 switch (type) { 4413 case TC_SETUP_BLOCK: 4414 return flow_block_cb_setup_simple(type_data, 4415 &stmmac_block_cb_list, 4416 stmmac_setup_tc_block_cb, 4417 priv, priv, true); 4418 case TC_SETUP_QDISC_CBS: 4419 return stmmac_tc_setup_cbs(priv, priv, type_data); 4420 case TC_SETUP_QDISC_TAPRIO: 4421 return stmmac_tc_setup_taprio(priv, priv, type_data); 4422 case TC_SETUP_QDISC_ETF: 4423 return stmmac_tc_setup_etf(priv, priv, type_data); 4424 default: 4425 return -EOPNOTSUPP; 4426 } 4427 } 4428 4429 static u16 stmmac_select_queue(struct net_device *dev, struct sk_buff *skb, 4430 struct net_device *sb_dev) 4431 { 4432 int gso = skb_shinfo(skb)->gso_type; 4433 4434 if (gso & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6 | SKB_GSO_UDP_L4)) { 4435 /* 4436 * There is no way to determine the number of TSO/USO 4437 * capable Queues. Let's use always the Queue 0 4438 * because if TSO/USO is supported then at least this 4439 * one will be capable. 4440 */ 4441 return 0; 4442 } 4443 4444 return netdev_pick_tx(dev, skb, NULL) % dev->real_num_tx_queues; 4445 } 4446 4447 static int stmmac_set_mac_address(struct net_device *ndev, void *addr) 4448 { 4449 struct stmmac_priv *priv = netdev_priv(ndev); 4450 int ret = 0; 4451 4452 ret = eth_mac_addr(ndev, addr); 4453 if (ret) 4454 return ret; 4455 4456 stmmac_set_umac_addr(priv, priv->hw, ndev->dev_addr, 0); 4457 4458 return ret; 4459 } 4460 4461 #ifdef CONFIG_DEBUG_FS 4462 static struct dentry *stmmac_fs_dir; 4463 4464 static void sysfs_display_ring(void *head, int size, int extend_desc, 4465 struct seq_file *seq, dma_addr_t dma_phy_addr) 4466 { 4467 int i; 4468 struct dma_extended_desc *ep = (struct dma_extended_desc *)head; 4469 struct dma_desc *p = (struct dma_desc *)head; 4470 dma_addr_t dma_addr; 4471 4472 for (i = 0; i < size; i++) { 4473 if (extend_desc) { 4474 dma_addr = dma_phy_addr + i * sizeof(*ep); 4475 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n", 4476 i, &dma_addr, 4477 le32_to_cpu(ep->basic.des0), 4478 le32_to_cpu(ep->basic.des1), 4479 le32_to_cpu(ep->basic.des2), 4480 le32_to_cpu(ep->basic.des3)); 4481 ep++; 4482 } else { 4483 dma_addr = dma_phy_addr + i * sizeof(*p); 4484 seq_printf(seq, "%d [%pad]: 0x%x 0x%x 0x%x 0x%x\n", 4485 i, &dma_addr, 4486 le32_to_cpu(p->des0), le32_to_cpu(p->des1), 4487 le32_to_cpu(p->des2), le32_to_cpu(p->des3)); 4488 p++; 4489 } 4490 seq_printf(seq, "\n"); 4491 } 4492 } 4493 4494 static int stmmac_rings_status_show(struct seq_file *seq, void *v) 4495 { 4496 struct net_device *dev = seq->private; 4497 struct stmmac_priv *priv = netdev_priv(dev); 4498 u32 rx_count = priv->plat->rx_queues_to_use; 4499 u32 tx_count = priv->plat->tx_queues_to_use; 4500 u32 queue; 4501 4502 if ((dev->flags & IFF_UP) == 0) 4503 return 0; 4504 4505 for (queue = 0; queue < rx_count; queue++) { 4506 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 4507 4508 seq_printf(seq, "RX Queue %d:\n", queue); 4509 4510 if (priv->extend_desc) { 4511 seq_printf(seq, "Extended descriptor ring:\n"); 4512 sysfs_display_ring((void *)rx_q->dma_erx, 4513 priv->dma_rx_size, 1, seq, rx_q->dma_rx_phy); 4514 } else { 4515 seq_printf(seq, "Descriptor ring:\n"); 4516 sysfs_display_ring((void *)rx_q->dma_rx, 4517 priv->dma_rx_size, 0, seq, rx_q->dma_rx_phy); 4518 } 4519 } 4520 4521 for (queue = 0; queue < tx_count; queue++) { 4522 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 4523 4524 seq_printf(seq, "TX Queue %d:\n", queue); 4525 4526 if (priv->extend_desc) { 4527 seq_printf(seq, "Extended descriptor ring:\n"); 4528 sysfs_display_ring((void *)tx_q->dma_etx, 4529 priv->dma_tx_size, 1, seq, tx_q->dma_tx_phy); 4530 } else if (!(tx_q->tbs & STMMAC_TBS_AVAIL)) { 4531 seq_printf(seq, "Descriptor ring:\n"); 4532 sysfs_display_ring((void *)tx_q->dma_tx, 4533 priv->dma_tx_size, 0, seq, tx_q->dma_tx_phy); 4534 } 4535 } 4536 4537 return 0; 4538 } 4539 DEFINE_SHOW_ATTRIBUTE(stmmac_rings_status); 4540 4541 static int stmmac_dma_cap_show(struct seq_file *seq, void *v) 4542 { 4543 struct net_device *dev = seq->private; 4544 struct stmmac_priv *priv = netdev_priv(dev); 4545 4546 if (!priv->hw_cap_support) { 4547 seq_printf(seq, "DMA HW features not supported\n"); 4548 return 0; 4549 } 4550 4551 seq_printf(seq, "==============================\n"); 4552 seq_printf(seq, "\tDMA HW features\n"); 4553 seq_printf(seq, "==============================\n"); 4554 4555 seq_printf(seq, "\t10/100 Mbps: %s\n", 4556 (priv->dma_cap.mbps_10_100) ? "Y" : "N"); 4557 seq_printf(seq, "\t1000 Mbps: %s\n", 4558 (priv->dma_cap.mbps_1000) ? "Y" : "N"); 4559 seq_printf(seq, "\tHalf duplex: %s\n", 4560 (priv->dma_cap.half_duplex) ? "Y" : "N"); 4561 seq_printf(seq, "\tHash Filter: %s\n", 4562 (priv->dma_cap.hash_filter) ? "Y" : "N"); 4563 seq_printf(seq, "\tMultiple MAC address registers: %s\n", 4564 (priv->dma_cap.multi_addr) ? "Y" : "N"); 4565 seq_printf(seq, "\tPCS (TBI/SGMII/RTBI PHY interfaces): %s\n", 4566 (priv->dma_cap.pcs) ? "Y" : "N"); 4567 seq_printf(seq, "\tSMA (MDIO) Interface: %s\n", 4568 (priv->dma_cap.sma_mdio) ? "Y" : "N"); 4569 seq_printf(seq, "\tPMT Remote wake up: %s\n", 4570 (priv->dma_cap.pmt_remote_wake_up) ? "Y" : "N"); 4571 seq_printf(seq, "\tPMT Magic Frame: %s\n", 4572 (priv->dma_cap.pmt_magic_frame) ? "Y" : "N"); 4573 seq_printf(seq, "\tRMON module: %s\n", 4574 (priv->dma_cap.rmon) ? "Y" : "N"); 4575 seq_printf(seq, "\tIEEE 1588-2002 Time Stamp: %s\n", 4576 (priv->dma_cap.time_stamp) ? "Y" : "N"); 4577 seq_printf(seq, "\tIEEE 1588-2008 Advanced Time Stamp: %s\n", 4578 (priv->dma_cap.atime_stamp) ? "Y" : "N"); 4579 seq_printf(seq, "\t802.3az - Energy-Efficient Ethernet (EEE): %s\n", 4580 (priv->dma_cap.eee) ? "Y" : "N"); 4581 seq_printf(seq, "\tAV features: %s\n", (priv->dma_cap.av) ? "Y" : "N"); 4582 seq_printf(seq, "\tChecksum Offload in TX: %s\n", 4583 (priv->dma_cap.tx_coe) ? "Y" : "N"); 4584 if (priv->synopsys_id >= DWMAC_CORE_4_00) { 4585 seq_printf(seq, "\tIP Checksum Offload in RX: %s\n", 4586 (priv->dma_cap.rx_coe) ? "Y" : "N"); 4587 } else { 4588 seq_printf(seq, "\tIP Checksum Offload (type1) in RX: %s\n", 4589 (priv->dma_cap.rx_coe_type1) ? "Y" : "N"); 4590 seq_printf(seq, "\tIP Checksum Offload (type2) in RX: %s\n", 4591 (priv->dma_cap.rx_coe_type2) ? "Y" : "N"); 4592 } 4593 seq_printf(seq, "\tRXFIFO > 2048bytes: %s\n", 4594 (priv->dma_cap.rxfifo_over_2048) ? "Y" : "N"); 4595 seq_printf(seq, "\tNumber of Additional RX channel: %d\n", 4596 priv->dma_cap.number_rx_channel); 4597 seq_printf(seq, "\tNumber of Additional TX channel: %d\n", 4598 priv->dma_cap.number_tx_channel); 4599 seq_printf(seq, "\tNumber of Additional RX queues: %d\n", 4600 priv->dma_cap.number_rx_queues); 4601 seq_printf(seq, "\tNumber of Additional TX queues: %d\n", 4602 priv->dma_cap.number_tx_queues); 4603 seq_printf(seq, "\tEnhanced descriptors: %s\n", 4604 (priv->dma_cap.enh_desc) ? "Y" : "N"); 4605 seq_printf(seq, "\tTX Fifo Size: %d\n", priv->dma_cap.tx_fifo_size); 4606 seq_printf(seq, "\tRX Fifo Size: %d\n", priv->dma_cap.rx_fifo_size); 4607 seq_printf(seq, "\tHash Table Size: %d\n", priv->dma_cap.hash_tb_sz); 4608 seq_printf(seq, "\tTSO: %s\n", priv->dma_cap.tsoen ? "Y" : "N"); 4609 seq_printf(seq, "\tNumber of PPS Outputs: %d\n", 4610 priv->dma_cap.pps_out_num); 4611 seq_printf(seq, "\tSafety Features: %s\n", 4612 priv->dma_cap.asp ? "Y" : "N"); 4613 seq_printf(seq, "\tFlexible RX Parser: %s\n", 4614 priv->dma_cap.frpsel ? "Y" : "N"); 4615 seq_printf(seq, "\tEnhanced Addressing: %d\n", 4616 priv->dma_cap.addr64); 4617 seq_printf(seq, "\tReceive Side Scaling: %s\n", 4618 priv->dma_cap.rssen ? "Y" : "N"); 4619 seq_printf(seq, "\tVLAN Hash Filtering: %s\n", 4620 priv->dma_cap.vlhash ? "Y" : "N"); 4621 seq_printf(seq, "\tSplit Header: %s\n", 4622 priv->dma_cap.sphen ? "Y" : "N"); 4623 seq_printf(seq, "\tVLAN TX Insertion: %s\n", 4624 priv->dma_cap.vlins ? "Y" : "N"); 4625 seq_printf(seq, "\tDouble VLAN: %s\n", 4626 priv->dma_cap.dvlan ? "Y" : "N"); 4627 seq_printf(seq, "\tNumber of L3/L4 Filters: %d\n", 4628 priv->dma_cap.l3l4fnum); 4629 seq_printf(seq, "\tARP Offloading: %s\n", 4630 priv->dma_cap.arpoffsel ? "Y" : "N"); 4631 seq_printf(seq, "\tEnhancements to Scheduled Traffic (EST): %s\n", 4632 priv->dma_cap.estsel ? "Y" : "N"); 4633 seq_printf(seq, "\tFrame Preemption (FPE): %s\n", 4634 priv->dma_cap.fpesel ? "Y" : "N"); 4635 seq_printf(seq, "\tTime-Based Scheduling (TBS): %s\n", 4636 priv->dma_cap.tbssel ? "Y" : "N"); 4637 return 0; 4638 } 4639 DEFINE_SHOW_ATTRIBUTE(stmmac_dma_cap); 4640 4641 /* Use network device events to rename debugfs file entries. 4642 */ 4643 static int stmmac_device_event(struct notifier_block *unused, 4644 unsigned long event, void *ptr) 4645 { 4646 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 4647 struct stmmac_priv *priv = netdev_priv(dev); 4648 4649 if (dev->netdev_ops != &stmmac_netdev_ops) 4650 goto done; 4651 4652 switch (event) { 4653 case NETDEV_CHANGENAME: 4654 if (priv->dbgfs_dir) 4655 priv->dbgfs_dir = debugfs_rename(stmmac_fs_dir, 4656 priv->dbgfs_dir, 4657 stmmac_fs_dir, 4658 dev->name); 4659 break; 4660 } 4661 done: 4662 return NOTIFY_DONE; 4663 } 4664 4665 static struct notifier_block stmmac_notifier = { 4666 .notifier_call = stmmac_device_event, 4667 }; 4668 4669 static void stmmac_init_fs(struct net_device *dev) 4670 { 4671 struct stmmac_priv *priv = netdev_priv(dev); 4672 4673 rtnl_lock(); 4674 4675 /* Create per netdev entries */ 4676 priv->dbgfs_dir = debugfs_create_dir(dev->name, stmmac_fs_dir); 4677 4678 /* Entry to report DMA RX/TX rings */ 4679 debugfs_create_file("descriptors_status", 0444, priv->dbgfs_dir, dev, 4680 &stmmac_rings_status_fops); 4681 4682 /* Entry to report the DMA HW features */ 4683 debugfs_create_file("dma_cap", 0444, priv->dbgfs_dir, dev, 4684 &stmmac_dma_cap_fops); 4685 4686 rtnl_unlock(); 4687 } 4688 4689 static void stmmac_exit_fs(struct net_device *dev) 4690 { 4691 struct stmmac_priv *priv = netdev_priv(dev); 4692 4693 debugfs_remove_recursive(priv->dbgfs_dir); 4694 } 4695 #endif /* CONFIG_DEBUG_FS */ 4696 4697 static u32 stmmac_vid_crc32_le(__le16 vid_le) 4698 { 4699 unsigned char *data = (unsigned char *)&vid_le; 4700 unsigned char data_byte = 0; 4701 u32 crc = ~0x0; 4702 u32 temp = 0; 4703 int i, bits; 4704 4705 bits = get_bitmask_order(VLAN_VID_MASK); 4706 for (i = 0; i < bits; i++) { 4707 if ((i % 8) == 0) 4708 data_byte = data[i / 8]; 4709 4710 temp = ((crc & 1) ^ data_byte) & 1; 4711 crc >>= 1; 4712 data_byte >>= 1; 4713 4714 if (temp) 4715 crc ^= 0xedb88320; 4716 } 4717 4718 return crc; 4719 } 4720 4721 static int stmmac_vlan_update(struct stmmac_priv *priv, bool is_double) 4722 { 4723 u32 crc, hash = 0; 4724 __le16 pmatch = 0; 4725 int count = 0; 4726 u16 vid = 0; 4727 4728 for_each_set_bit(vid, priv->active_vlans, VLAN_N_VID) { 4729 __le16 vid_le = cpu_to_le16(vid); 4730 crc = bitrev32(~stmmac_vid_crc32_le(vid_le)) >> 28; 4731 hash |= (1 << crc); 4732 count++; 4733 } 4734 4735 if (!priv->dma_cap.vlhash) { 4736 if (count > 2) /* VID = 0 always passes filter */ 4737 return -EOPNOTSUPP; 4738 4739 pmatch = cpu_to_le16(vid); 4740 hash = 0; 4741 } 4742 4743 return stmmac_update_vlan_hash(priv, priv->hw, hash, pmatch, is_double); 4744 } 4745 4746 static int stmmac_vlan_rx_add_vid(struct net_device *ndev, __be16 proto, u16 vid) 4747 { 4748 struct stmmac_priv *priv = netdev_priv(ndev); 4749 bool is_double = false; 4750 int ret; 4751 4752 ret = pm_runtime_get_sync(priv->device); 4753 if (ret < 0) { 4754 pm_runtime_put_noidle(priv->device); 4755 return ret; 4756 } 4757 4758 if (be16_to_cpu(proto) == ETH_P_8021AD) 4759 is_double = true; 4760 4761 set_bit(vid, priv->active_vlans); 4762 ret = stmmac_vlan_update(priv, is_double); 4763 if (ret) { 4764 clear_bit(vid, priv->active_vlans); 4765 return ret; 4766 } 4767 4768 if (priv->hw->num_vlan) { 4769 ret = stmmac_add_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid); 4770 if (ret) 4771 return ret; 4772 } 4773 4774 return 0; 4775 } 4776 4777 static int stmmac_vlan_rx_kill_vid(struct net_device *ndev, __be16 proto, u16 vid) 4778 { 4779 struct stmmac_priv *priv = netdev_priv(ndev); 4780 bool is_double = false; 4781 int ret; 4782 4783 if (be16_to_cpu(proto) == ETH_P_8021AD) 4784 is_double = true; 4785 4786 clear_bit(vid, priv->active_vlans); 4787 4788 if (priv->hw->num_vlan) { 4789 ret = stmmac_del_hw_vlan_rx_fltr(priv, ndev, priv->hw, proto, vid); 4790 if (ret) 4791 goto del_vlan_error; 4792 } 4793 4794 ret = stmmac_vlan_update(priv, is_double); 4795 4796 del_vlan_error: 4797 pm_runtime_put(priv->device); 4798 4799 return ret; 4800 } 4801 4802 static const struct net_device_ops stmmac_netdev_ops = { 4803 .ndo_open = stmmac_open, 4804 .ndo_start_xmit = stmmac_xmit, 4805 .ndo_stop = stmmac_release, 4806 .ndo_change_mtu = stmmac_change_mtu, 4807 .ndo_fix_features = stmmac_fix_features, 4808 .ndo_set_features = stmmac_set_features, 4809 .ndo_set_rx_mode = stmmac_set_rx_mode, 4810 .ndo_tx_timeout = stmmac_tx_timeout, 4811 .ndo_do_ioctl = stmmac_ioctl, 4812 .ndo_setup_tc = stmmac_setup_tc, 4813 .ndo_select_queue = stmmac_select_queue, 4814 #ifdef CONFIG_NET_POLL_CONTROLLER 4815 .ndo_poll_controller = stmmac_poll_controller, 4816 #endif 4817 .ndo_set_mac_address = stmmac_set_mac_address, 4818 .ndo_vlan_rx_add_vid = stmmac_vlan_rx_add_vid, 4819 .ndo_vlan_rx_kill_vid = stmmac_vlan_rx_kill_vid, 4820 }; 4821 4822 static void stmmac_reset_subtask(struct stmmac_priv *priv) 4823 { 4824 if (!test_and_clear_bit(STMMAC_RESET_REQUESTED, &priv->state)) 4825 return; 4826 if (test_bit(STMMAC_DOWN, &priv->state)) 4827 return; 4828 4829 netdev_err(priv->dev, "Reset adapter.\n"); 4830 4831 rtnl_lock(); 4832 netif_trans_update(priv->dev); 4833 while (test_and_set_bit(STMMAC_RESETING, &priv->state)) 4834 usleep_range(1000, 2000); 4835 4836 set_bit(STMMAC_DOWN, &priv->state); 4837 dev_close(priv->dev); 4838 dev_open(priv->dev, NULL); 4839 clear_bit(STMMAC_DOWN, &priv->state); 4840 clear_bit(STMMAC_RESETING, &priv->state); 4841 rtnl_unlock(); 4842 } 4843 4844 static void stmmac_service_task(struct work_struct *work) 4845 { 4846 struct stmmac_priv *priv = container_of(work, struct stmmac_priv, 4847 service_task); 4848 4849 stmmac_reset_subtask(priv); 4850 clear_bit(STMMAC_SERVICE_SCHED, &priv->state); 4851 } 4852 4853 /** 4854 * stmmac_hw_init - Init the MAC device 4855 * @priv: driver private structure 4856 * Description: this function is to configure the MAC device according to 4857 * some platform parameters or the HW capability register. It prepares the 4858 * driver to use either ring or chain modes and to setup either enhanced or 4859 * normal descriptors. 4860 */ 4861 static int stmmac_hw_init(struct stmmac_priv *priv) 4862 { 4863 int ret; 4864 4865 /* dwmac-sun8i only work in chain mode */ 4866 if (priv->plat->has_sun8i) 4867 chain_mode = 1; 4868 priv->chain_mode = chain_mode; 4869 4870 /* Initialize HW Interface */ 4871 ret = stmmac_hwif_init(priv); 4872 if (ret) 4873 return ret; 4874 4875 /* Get the HW capability (new GMAC newer than 3.50a) */ 4876 priv->hw_cap_support = stmmac_get_hw_features(priv); 4877 if (priv->hw_cap_support) { 4878 dev_info(priv->device, "DMA HW capability register supported\n"); 4879 4880 /* We can override some gmac/dma configuration fields: e.g. 4881 * enh_desc, tx_coe (e.g. that are passed through the 4882 * platform) with the values from the HW capability 4883 * register (if supported). 4884 */ 4885 priv->plat->enh_desc = priv->dma_cap.enh_desc; 4886 priv->plat->pmt = priv->dma_cap.pmt_remote_wake_up; 4887 priv->hw->pmt = priv->plat->pmt; 4888 if (priv->dma_cap.hash_tb_sz) { 4889 priv->hw->multicast_filter_bins = 4890 (BIT(priv->dma_cap.hash_tb_sz) << 5); 4891 priv->hw->mcast_bits_log2 = 4892 ilog2(priv->hw->multicast_filter_bins); 4893 } 4894 4895 /* TXCOE doesn't work in thresh DMA mode */ 4896 if (priv->plat->force_thresh_dma_mode) 4897 priv->plat->tx_coe = 0; 4898 else 4899 priv->plat->tx_coe = priv->dma_cap.tx_coe; 4900 4901 /* In case of GMAC4 rx_coe is from HW cap register. */ 4902 priv->plat->rx_coe = priv->dma_cap.rx_coe; 4903 4904 if (priv->dma_cap.rx_coe_type2) 4905 priv->plat->rx_coe = STMMAC_RX_COE_TYPE2; 4906 else if (priv->dma_cap.rx_coe_type1) 4907 priv->plat->rx_coe = STMMAC_RX_COE_TYPE1; 4908 4909 } else { 4910 dev_info(priv->device, "No HW DMA feature register supported\n"); 4911 } 4912 4913 if (priv->plat->rx_coe) { 4914 priv->hw->rx_csum = priv->plat->rx_coe; 4915 dev_info(priv->device, "RX Checksum Offload Engine supported\n"); 4916 if (priv->synopsys_id < DWMAC_CORE_4_00) 4917 dev_info(priv->device, "COE Type %d\n", priv->hw->rx_csum); 4918 } 4919 if (priv->plat->tx_coe) 4920 dev_info(priv->device, "TX Checksum insertion supported\n"); 4921 4922 if (priv->plat->pmt) { 4923 dev_info(priv->device, "Wake-Up On Lan supported\n"); 4924 device_set_wakeup_capable(priv->device, 1); 4925 } 4926 4927 if (priv->dma_cap.tsoen) 4928 dev_info(priv->device, "TSO supported\n"); 4929 4930 priv->hw->vlan_fail_q_en = priv->plat->vlan_fail_q_en; 4931 priv->hw->vlan_fail_q = priv->plat->vlan_fail_q; 4932 4933 /* Run HW quirks, if any */ 4934 if (priv->hwif_quirks) { 4935 ret = priv->hwif_quirks(priv); 4936 if (ret) 4937 return ret; 4938 } 4939 4940 /* Rx Watchdog is available in the COREs newer than the 3.40. 4941 * In some case, for example on bugged HW this feature 4942 * has to be disable and this can be done by passing the 4943 * riwt_off field from the platform. 4944 */ 4945 if (((priv->synopsys_id >= DWMAC_CORE_3_50) || 4946 (priv->plat->has_xgmac)) && (!priv->plat->riwt_off)) { 4947 priv->use_riwt = 1; 4948 dev_info(priv->device, 4949 "Enable RX Mitigation via HW Watchdog Timer\n"); 4950 } 4951 4952 return 0; 4953 } 4954 4955 static void stmmac_napi_add(struct net_device *dev) 4956 { 4957 struct stmmac_priv *priv = netdev_priv(dev); 4958 u32 queue, maxq; 4959 4960 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use); 4961 4962 for (queue = 0; queue < maxq; queue++) { 4963 struct stmmac_channel *ch = &priv->channel[queue]; 4964 4965 ch->priv_data = priv; 4966 ch->index = queue; 4967 spin_lock_init(&ch->lock); 4968 4969 if (queue < priv->plat->rx_queues_to_use) { 4970 netif_napi_add(dev, &ch->rx_napi, stmmac_napi_poll_rx, 4971 NAPI_POLL_WEIGHT); 4972 } 4973 if (queue < priv->plat->tx_queues_to_use) { 4974 netif_tx_napi_add(dev, &ch->tx_napi, 4975 stmmac_napi_poll_tx, 4976 NAPI_POLL_WEIGHT); 4977 } 4978 } 4979 } 4980 4981 static void stmmac_napi_del(struct net_device *dev) 4982 { 4983 struct stmmac_priv *priv = netdev_priv(dev); 4984 u32 queue, maxq; 4985 4986 maxq = max(priv->plat->rx_queues_to_use, priv->plat->tx_queues_to_use); 4987 4988 for (queue = 0; queue < maxq; queue++) { 4989 struct stmmac_channel *ch = &priv->channel[queue]; 4990 4991 if (queue < priv->plat->rx_queues_to_use) 4992 netif_napi_del(&ch->rx_napi); 4993 if (queue < priv->plat->tx_queues_to_use) 4994 netif_napi_del(&ch->tx_napi); 4995 } 4996 } 4997 4998 int stmmac_reinit_queues(struct net_device *dev, u32 rx_cnt, u32 tx_cnt) 4999 { 5000 struct stmmac_priv *priv = netdev_priv(dev); 5001 int ret = 0; 5002 5003 if (netif_running(dev)) 5004 stmmac_release(dev); 5005 5006 stmmac_napi_del(dev); 5007 5008 priv->plat->rx_queues_to_use = rx_cnt; 5009 priv->plat->tx_queues_to_use = tx_cnt; 5010 5011 stmmac_napi_add(dev); 5012 5013 if (netif_running(dev)) 5014 ret = stmmac_open(dev); 5015 5016 return ret; 5017 } 5018 5019 int stmmac_reinit_ringparam(struct net_device *dev, u32 rx_size, u32 tx_size) 5020 { 5021 struct stmmac_priv *priv = netdev_priv(dev); 5022 int ret = 0; 5023 5024 if (netif_running(dev)) 5025 stmmac_release(dev); 5026 5027 priv->dma_rx_size = rx_size; 5028 priv->dma_tx_size = tx_size; 5029 5030 if (netif_running(dev)) 5031 ret = stmmac_open(dev); 5032 5033 return ret; 5034 } 5035 5036 /** 5037 * stmmac_dvr_probe 5038 * @device: device pointer 5039 * @plat_dat: platform data pointer 5040 * @res: stmmac resource pointer 5041 * Description: this is the main probe function used to 5042 * call the alloc_etherdev, allocate the priv structure. 5043 * Return: 5044 * returns 0 on success, otherwise errno. 5045 */ 5046 int stmmac_dvr_probe(struct device *device, 5047 struct plat_stmmacenet_data *plat_dat, 5048 struct stmmac_resources *res) 5049 { 5050 struct net_device *ndev = NULL; 5051 struct stmmac_priv *priv; 5052 u32 rxq; 5053 int i, ret = 0; 5054 5055 ndev = devm_alloc_etherdev_mqs(device, sizeof(struct stmmac_priv), 5056 MTL_MAX_TX_QUEUES, MTL_MAX_RX_QUEUES); 5057 if (!ndev) 5058 return -ENOMEM; 5059 5060 SET_NETDEV_DEV(ndev, device); 5061 5062 priv = netdev_priv(ndev); 5063 priv->device = device; 5064 priv->dev = ndev; 5065 5066 stmmac_set_ethtool_ops(ndev); 5067 priv->pause = pause; 5068 priv->plat = plat_dat; 5069 priv->ioaddr = res->addr; 5070 priv->dev->base_addr = (unsigned long)res->addr; 5071 5072 priv->dev->irq = res->irq; 5073 priv->wol_irq = res->wol_irq; 5074 priv->lpi_irq = res->lpi_irq; 5075 5076 if (!IS_ERR_OR_NULL(res->mac)) 5077 memcpy(priv->dev->dev_addr, res->mac, ETH_ALEN); 5078 5079 dev_set_drvdata(device, priv->dev); 5080 5081 /* Verify driver arguments */ 5082 stmmac_verify_args(); 5083 5084 /* Allocate workqueue */ 5085 priv->wq = create_singlethread_workqueue("stmmac_wq"); 5086 if (!priv->wq) { 5087 dev_err(priv->device, "failed to create workqueue\n"); 5088 return -ENOMEM; 5089 } 5090 5091 INIT_WORK(&priv->service_task, stmmac_service_task); 5092 5093 /* Override with kernel parameters if supplied XXX CRS XXX 5094 * this needs to have multiple instances 5095 */ 5096 if ((phyaddr >= 0) && (phyaddr <= 31)) 5097 priv->plat->phy_addr = phyaddr; 5098 5099 if (priv->plat->stmmac_rst) { 5100 ret = reset_control_assert(priv->plat->stmmac_rst); 5101 reset_control_deassert(priv->plat->stmmac_rst); 5102 /* Some reset controllers have only reset callback instead of 5103 * assert + deassert callbacks pair. 5104 */ 5105 if (ret == -ENOTSUPP) 5106 reset_control_reset(priv->plat->stmmac_rst); 5107 } 5108 5109 /* Init MAC and get the capabilities */ 5110 ret = stmmac_hw_init(priv); 5111 if (ret) 5112 goto error_hw_init; 5113 5114 stmmac_check_ether_addr(priv); 5115 5116 ndev->netdev_ops = &stmmac_netdev_ops; 5117 5118 ndev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM | 5119 NETIF_F_RXCSUM; 5120 5121 ret = stmmac_tc_init(priv, priv); 5122 if (!ret) { 5123 ndev->hw_features |= NETIF_F_HW_TC; 5124 } 5125 5126 if ((priv->plat->tso_en) && (priv->dma_cap.tsoen)) { 5127 ndev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6; 5128 if (priv->plat->has_gmac4) 5129 ndev->hw_features |= NETIF_F_GSO_UDP_L4; 5130 priv->tso = true; 5131 dev_info(priv->device, "TSO feature enabled\n"); 5132 } 5133 5134 if (priv->dma_cap.sphen) { 5135 ndev->hw_features |= NETIF_F_GRO; 5136 priv->sph = true; 5137 dev_info(priv->device, "SPH feature enabled\n"); 5138 } 5139 5140 /* The current IP register MAC_HW_Feature1[ADDR64] only define 5141 * 32/40/64 bit width, but some SOC support others like i.MX8MP 5142 * support 34 bits but it map to 40 bits width in MAC_HW_Feature1[ADDR64]. 5143 * So overwrite dma_cap.addr64 according to HW real design. 5144 */ 5145 if (priv->plat->addr64) 5146 priv->dma_cap.addr64 = priv->plat->addr64; 5147 5148 if (priv->dma_cap.addr64) { 5149 ret = dma_set_mask_and_coherent(device, 5150 DMA_BIT_MASK(priv->dma_cap.addr64)); 5151 if (!ret) { 5152 dev_info(priv->device, "Using %d bits DMA width\n", 5153 priv->dma_cap.addr64); 5154 5155 /* 5156 * If more than 32 bits can be addressed, make sure to 5157 * enable enhanced addressing mode. 5158 */ 5159 if (IS_ENABLED(CONFIG_ARCH_DMA_ADDR_T_64BIT)) 5160 priv->plat->dma_cfg->eame = true; 5161 } else { 5162 ret = dma_set_mask_and_coherent(device, DMA_BIT_MASK(32)); 5163 if (ret) { 5164 dev_err(priv->device, "Failed to set DMA Mask\n"); 5165 goto error_hw_init; 5166 } 5167 5168 priv->dma_cap.addr64 = 32; 5169 } 5170 } 5171 5172 ndev->features |= ndev->hw_features | NETIF_F_HIGHDMA; 5173 ndev->watchdog_timeo = msecs_to_jiffies(watchdog); 5174 #ifdef STMMAC_VLAN_TAG_USED 5175 /* Both mac100 and gmac support receive VLAN tag detection */ 5176 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_HW_VLAN_STAG_RX; 5177 if (priv->dma_cap.vlhash) { 5178 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 5179 ndev->features |= NETIF_F_HW_VLAN_STAG_FILTER; 5180 } 5181 if (priv->dma_cap.vlins) { 5182 ndev->features |= NETIF_F_HW_VLAN_CTAG_TX; 5183 if (priv->dma_cap.dvlan) 5184 ndev->features |= NETIF_F_HW_VLAN_STAG_TX; 5185 } 5186 #endif 5187 priv->msg_enable = netif_msg_init(debug, default_msg_level); 5188 5189 /* Initialize RSS */ 5190 rxq = priv->plat->rx_queues_to_use; 5191 netdev_rss_key_fill(priv->rss.key, sizeof(priv->rss.key)); 5192 for (i = 0; i < ARRAY_SIZE(priv->rss.table); i++) 5193 priv->rss.table[i] = ethtool_rxfh_indir_default(i, rxq); 5194 5195 if (priv->dma_cap.rssen && priv->plat->rss_en) 5196 ndev->features |= NETIF_F_RXHASH; 5197 5198 /* MTU range: 46 - hw-specific max */ 5199 ndev->min_mtu = ETH_ZLEN - ETH_HLEN; 5200 if (priv->plat->has_xgmac) 5201 ndev->max_mtu = XGMAC_JUMBO_LEN; 5202 else if ((priv->plat->enh_desc) || (priv->synopsys_id >= DWMAC_CORE_4_00)) 5203 ndev->max_mtu = JUMBO_LEN; 5204 else 5205 ndev->max_mtu = SKB_MAX_HEAD(NET_SKB_PAD + NET_IP_ALIGN); 5206 /* Will not overwrite ndev->max_mtu if plat->maxmtu > ndev->max_mtu 5207 * as well as plat->maxmtu < ndev->min_mtu which is a invalid range. 5208 */ 5209 if ((priv->plat->maxmtu < ndev->max_mtu) && 5210 (priv->plat->maxmtu >= ndev->min_mtu)) 5211 ndev->max_mtu = priv->plat->maxmtu; 5212 else if (priv->plat->maxmtu < ndev->min_mtu) 5213 dev_warn(priv->device, 5214 "%s: warning: maxmtu having invalid value (%d)\n", 5215 __func__, priv->plat->maxmtu); 5216 5217 if (flow_ctrl) 5218 priv->flow_ctrl = FLOW_AUTO; /* RX/TX pause on */ 5219 5220 /* Setup channels NAPI */ 5221 stmmac_napi_add(ndev); 5222 5223 mutex_init(&priv->lock); 5224 5225 /* If a specific clk_csr value is passed from the platform 5226 * this means that the CSR Clock Range selection cannot be 5227 * changed at run-time and it is fixed. Viceversa the driver'll try to 5228 * set the MDC clock dynamically according to the csr actual 5229 * clock input. 5230 */ 5231 if (priv->plat->clk_csr >= 0) 5232 priv->clk_csr = priv->plat->clk_csr; 5233 else 5234 stmmac_clk_csr_set(priv); 5235 5236 stmmac_check_pcs_mode(priv); 5237 5238 pm_runtime_get_noresume(device); 5239 pm_runtime_set_active(device); 5240 pm_runtime_enable(device); 5241 5242 if (priv->hw->pcs != STMMAC_PCS_TBI && 5243 priv->hw->pcs != STMMAC_PCS_RTBI) { 5244 /* MDIO bus Registration */ 5245 ret = stmmac_mdio_register(ndev); 5246 if (ret < 0) { 5247 dev_err(priv->device, 5248 "%s: MDIO bus (id: %d) registration failed", 5249 __func__, priv->plat->bus_id); 5250 goto error_mdio_register; 5251 } 5252 } 5253 5254 ret = stmmac_phy_setup(priv); 5255 if (ret) { 5256 netdev_err(ndev, "failed to setup phy (%d)\n", ret); 5257 goto error_phy_setup; 5258 } 5259 5260 ret = register_netdev(ndev); 5261 if (ret) { 5262 dev_err(priv->device, "%s: ERROR %i registering the device\n", 5263 __func__, ret); 5264 goto error_netdev_register; 5265 } 5266 5267 if (priv->plat->serdes_powerup) { 5268 ret = priv->plat->serdes_powerup(ndev, 5269 priv->plat->bsp_priv); 5270 5271 if (ret < 0) 5272 goto error_serdes_powerup; 5273 } 5274 5275 #ifdef CONFIG_DEBUG_FS 5276 stmmac_init_fs(ndev); 5277 #endif 5278 5279 /* Let pm_runtime_put() disable the clocks. 5280 * If CONFIG_PM is not enabled, the clocks will stay powered. 5281 */ 5282 pm_runtime_put(device); 5283 5284 return ret; 5285 5286 error_serdes_powerup: 5287 unregister_netdev(ndev); 5288 error_netdev_register: 5289 phylink_destroy(priv->phylink); 5290 error_phy_setup: 5291 if (priv->hw->pcs != STMMAC_PCS_TBI && 5292 priv->hw->pcs != STMMAC_PCS_RTBI) 5293 stmmac_mdio_unregister(ndev); 5294 error_mdio_register: 5295 stmmac_napi_del(ndev); 5296 error_hw_init: 5297 destroy_workqueue(priv->wq); 5298 stmmac_bus_clks_config(priv, false); 5299 5300 return ret; 5301 } 5302 EXPORT_SYMBOL_GPL(stmmac_dvr_probe); 5303 5304 /** 5305 * stmmac_dvr_remove 5306 * @dev: device pointer 5307 * Description: this function resets the TX/RX processes, disables the MAC RX/TX 5308 * changes the link status, releases the DMA descriptor rings. 5309 */ 5310 int stmmac_dvr_remove(struct device *dev) 5311 { 5312 struct net_device *ndev = dev_get_drvdata(dev); 5313 struct stmmac_priv *priv = netdev_priv(ndev); 5314 5315 netdev_info(priv->dev, "%s: removing driver", __func__); 5316 5317 stmmac_stop_all_dma(priv); 5318 stmmac_mac_set(priv, priv->ioaddr, false); 5319 netif_carrier_off(ndev); 5320 unregister_netdev(ndev); 5321 5322 /* Serdes power down needs to happen after VLAN filter 5323 * is deleted that is triggered by unregister_netdev(). 5324 */ 5325 if (priv->plat->serdes_powerdown) 5326 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv); 5327 5328 #ifdef CONFIG_DEBUG_FS 5329 stmmac_exit_fs(ndev); 5330 #endif 5331 phylink_destroy(priv->phylink); 5332 if (priv->plat->stmmac_rst) 5333 reset_control_assert(priv->plat->stmmac_rst); 5334 pm_runtime_put(dev); 5335 pm_runtime_disable(dev); 5336 if (priv->hw->pcs != STMMAC_PCS_TBI && 5337 priv->hw->pcs != STMMAC_PCS_RTBI) 5338 stmmac_mdio_unregister(ndev); 5339 destroy_workqueue(priv->wq); 5340 mutex_destroy(&priv->lock); 5341 5342 return 0; 5343 } 5344 EXPORT_SYMBOL_GPL(stmmac_dvr_remove); 5345 5346 /** 5347 * stmmac_suspend - suspend callback 5348 * @dev: device pointer 5349 * Description: this is the function to suspend the device and it is called 5350 * by the platform driver to stop the network queue, release the resources, 5351 * program the PMT register (for WoL), clean and release driver resources. 5352 */ 5353 int stmmac_suspend(struct device *dev) 5354 { 5355 struct net_device *ndev = dev_get_drvdata(dev); 5356 struct stmmac_priv *priv = netdev_priv(ndev); 5357 u32 chan; 5358 int ret; 5359 5360 if (!ndev || !netif_running(ndev)) 5361 return 0; 5362 5363 phylink_mac_change(priv->phylink, false); 5364 5365 mutex_lock(&priv->lock); 5366 5367 netif_device_detach(ndev); 5368 5369 stmmac_disable_all_queues(priv); 5370 5371 for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++) 5372 hrtimer_cancel(&priv->tx_queue[chan].txtimer); 5373 5374 if (priv->eee_enabled) { 5375 priv->tx_path_in_lpi_mode = false; 5376 del_timer_sync(&priv->eee_ctrl_timer); 5377 } 5378 5379 /* Stop TX/RX DMA */ 5380 stmmac_stop_all_dma(priv); 5381 5382 if (priv->plat->serdes_powerdown) 5383 priv->plat->serdes_powerdown(ndev, priv->plat->bsp_priv); 5384 5385 /* Enable Power down mode by programming the PMT regs */ 5386 if (device_may_wakeup(priv->device) && priv->plat->pmt) { 5387 stmmac_pmt(priv, priv->hw, priv->wolopts); 5388 priv->irq_wake = 1; 5389 } else { 5390 mutex_unlock(&priv->lock); 5391 rtnl_lock(); 5392 if (device_may_wakeup(priv->device)) 5393 phylink_speed_down(priv->phylink, false); 5394 phylink_stop(priv->phylink); 5395 rtnl_unlock(); 5396 mutex_lock(&priv->lock); 5397 5398 stmmac_mac_set(priv, priv->ioaddr, false); 5399 pinctrl_pm_select_sleep_state(priv->device); 5400 /* Disable clock in case of PWM is off */ 5401 clk_disable_unprepare(priv->plat->clk_ptp_ref); 5402 ret = pm_runtime_force_suspend(dev); 5403 if (ret) 5404 return ret; 5405 } 5406 mutex_unlock(&priv->lock); 5407 5408 priv->speed = SPEED_UNKNOWN; 5409 return 0; 5410 } 5411 EXPORT_SYMBOL_GPL(stmmac_suspend); 5412 5413 /** 5414 * stmmac_reset_queues_param - reset queue parameters 5415 * @priv: device pointer 5416 */ 5417 static void stmmac_reset_queues_param(struct stmmac_priv *priv) 5418 { 5419 u32 rx_cnt = priv->plat->rx_queues_to_use; 5420 u32 tx_cnt = priv->plat->tx_queues_to_use; 5421 u32 queue; 5422 5423 for (queue = 0; queue < rx_cnt; queue++) { 5424 struct stmmac_rx_queue *rx_q = &priv->rx_queue[queue]; 5425 5426 rx_q->cur_rx = 0; 5427 rx_q->dirty_rx = 0; 5428 } 5429 5430 for (queue = 0; queue < tx_cnt; queue++) { 5431 struct stmmac_tx_queue *tx_q = &priv->tx_queue[queue]; 5432 5433 tx_q->cur_tx = 0; 5434 tx_q->dirty_tx = 0; 5435 tx_q->mss = 0; 5436 5437 netdev_tx_reset_queue(netdev_get_tx_queue(priv->dev, queue)); 5438 } 5439 } 5440 5441 /** 5442 * stmmac_resume - resume callback 5443 * @dev: device pointer 5444 * Description: when resume this function is invoked to setup the DMA and CORE 5445 * in a usable state. 5446 */ 5447 int stmmac_resume(struct device *dev) 5448 { 5449 struct net_device *ndev = dev_get_drvdata(dev); 5450 struct stmmac_priv *priv = netdev_priv(ndev); 5451 int ret; 5452 5453 if (!netif_running(ndev)) 5454 return 0; 5455 5456 /* Power Down bit, into the PM register, is cleared 5457 * automatically as soon as a magic packet or a Wake-up frame 5458 * is received. Anyway, it's better to manually clear 5459 * this bit because it can generate problems while resuming 5460 * from another devices (e.g. serial console). 5461 */ 5462 if (device_may_wakeup(priv->device) && priv->plat->pmt) { 5463 mutex_lock(&priv->lock); 5464 stmmac_pmt(priv, priv->hw, 0); 5465 mutex_unlock(&priv->lock); 5466 priv->irq_wake = 0; 5467 } else { 5468 pinctrl_pm_select_default_state(priv->device); 5469 /* enable the clk previously disabled */ 5470 ret = pm_runtime_force_resume(dev); 5471 if (ret) 5472 return ret; 5473 if (priv->plat->clk_ptp_ref) 5474 clk_prepare_enable(priv->plat->clk_ptp_ref); 5475 /* reset the phy so that it's ready */ 5476 if (priv->mii) 5477 stmmac_mdio_reset(priv->mii); 5478 } 5479 5480 if (priv->plat->serdes_powerup) { 5481 ret = priv->plat->serdes_powerup(ndev, 5482 priv->plat->bsp_priv); 5483 5484 if (ret < 0) 5485 return ret; 5486 } 5487 5488 if (!device_may_wakeup(priv->device) || !priv->plat->pmt) { 5489 rtnl_lock(); 5490 phylink_start(priv->phylink); 5491 /* We may have called phylink_speed_down before */ 5492 phylink_speed_up(priv->phylink); 5493 rtnl_unlock(); 5494 } 5495 5496 rtnl_lock(); 5497 mutex_lock(&priv->lock); 5498 5499 stmmac_reset_queues_param(priv); 5500 stmmac_reinit_rx_buffers(priv); 5501 stmmac_free_tx_skbufs(priv); 5502 stmmac_clear_descriptors(priv); 5503 5504 stmmac_hw_setup(ndev, false); 5505 stmmac_init_coalesce(priv); 5506 stmmac_set_rx_mode(ndev); 5507 5508 stmmac_restore_hw_vlan_rx_fltr(priv, ndev, priv->hw); 5509 5510 stmmac_enable_all_queues(priv); 5511 5512 mutex_unlock(&priv->lock); 5513 rtnl_unlock(); 5514 5515 phylink_mac_change(priv->phylink, true); 5516 5517 netif_device_attach(ndev); 5518 5519 return 0; 5520 } 5521 EXPORT_SYMBOL_GPL(stmmac_resume); 5522 5523 #ifndef MODULE 5524 static int __init stmmac_cmdline_opt(char *str) 5525 { 5526 char *opt; 5527 5528 if (!str || !*str) 5529 return -EINVAL; 5530 while ((opt = strsep(&str, ",")) != NULL) { 5531 if (!strncmp(opt, "debug:", 6)) { 5532 if (kstrtoint(opt + 6, 0, &debug)) 5533 goto err; 5534 } else if (!strncmp(opt, "phyaddr:", 8)) { 5535 if (kstrtoint(opt + 8, 0, &phyaddr)) 5536 goto err; 5537 } else if (!strncmp(opt, "buf_sz:", 7)) { 5538 if (kstrtoint(opt + 7, 0, &buf_sz)) 5539 goto err; 5540 } else if (!strncmp(opt, "tc:", 3)) { 5541 if (kstrtoint(opt + 3, 0, &tc)) 5542 goto err; 5543 } else if (!strncmp(opt, "watchdog:", 9)) { 5544 if (kstrtoint(opt + 9, 0, &watchdog)) 5545 goto err; 5546 } else if (!strncmp(opt, "flow_ctrl:", 10)) { 5547 if (kstrtoint(opt + 10, 0, &flow_ctrl)) 5548 goto err; 5549 } else if (!strncmp(opt, "pause:", 6)) { 5550 if (kstrtoint(opt + 6, 0, &pause)) 5551 goto err; 5552 } else if (!strncmp(opt, "eee_timer:", 10)) { 5553 if (kstrtoint(opt + 10, 0, &eee_timer)) 5554 goto err; 5555 } else if (!strncmp(opt, "chain_mode:", 11)) { 5556 if (kstrtoint(opt + 11, 0, &chain_mode)) 5557 goto err; 5558 } 5559 } 5560 return 0; 5561 5562 err: 5563 pr_err("%s: ERROR broken module parameter conversion", __func__); 5564 return -EINVAL; 5565 } 5566 5567 __setup("stmmaceth=", stmmac_cmdline_opt); 5568 #endif /* MODULE */ 5569 5570 static int __init stmmac_init(void) 5571 { 5572 #ifdef CONFIG_DEBUG_FS 5573 /* Create debugfs main directory if it doesn't exist yet */ 5574 if (!stmmac_fs_dir) 5575 stmmac_fs_dir = debugfs_create_dir(STMMAC_RESOURCE_NAME, NULL); 5576 register_netdevice_notifier(&stmmac_notifier); 5577 #endif 5578 5579 return 0; 5580 } 5581 5582 static void __exit stmmac_exit(void) 5583 { 5584 #ifdef CONFIG_DEBUG_FS 5585 unregister_netdevice_notifier(&stmmac_notifier); 5586 debugfs_remove_recursive(stmmac_fs_dir); 5587 #endif 5588 } 5589 5590 module_init(stmmac_init) 5591 module_exit(stmmac_exit) 5592 5593 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet device driver"); 5594 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>"); 5595 MODULE_LICENSE("GPL"); 5596