1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Fast Ethernet Controller (FEC) driver for Motorola MPC8xx. 4 * Copyright (c) 1997 Dan Malek (dmalek@jlc.net) 5 * 6 * Right now, I am very wasteful with the buffers. I allocate memory 7 * pages and then divide them into 2K frame buffers. This way I know I 8 * have buffers large enough to hold one frame within one buffer descriptor. 9 * Once I get this working, I will use 64 or 128 byte CPM buffers, which 10 * will be much more memory efficient and will easily handle lots of 11 * small packets. 12 * 13 * Much better multiple PHY support by Magnus Damm. 14 * Copyright (c) 2000 Ericsson Radio Systems AB. 15 * 16 * Support for FEC controller of ColdFire processors. 17 * Copyright (c) 2001-2005 Greg Ungerer (gerg@snapgear.com) 18 * 19 * Bug fixes and cleanup by Philippe De Muyter (phdm@macqel.be) 20 * Copyright (c) 2004-2006 Macq Electronique SA. 21 * 22 * Copyright (C) 2010-2011 Freescale Semiconductor, Inc. 23 */ 24 25 #include <linux/module.h> 26 #include <linux/kernel.h> 27 #include <linux/string.h> 28 #include <linux/pm_runtime.h> 29 #include <linux/ptrace.h> 30 #include <linux/errno.h> 31 #include <linux/ioport.h> 32 #include <linux/slab.h> 33 #include <linux/interrupt.h> 34 #include <linux/delay.h> 35 #include <linux/netdevice.h> 36 #include <linux/etherdevice.h> 37 #include <linux/skbuff.h> 38 #include <linux/in.h> 39 #include <linux/ip.h> 40 #include <net/ip.h> 41 #include <net/selftests.h> 42 #include <net/tso.h> 43 #include <linux/tcp.h> 44 #include <linux/udp.h> 45 #include <linux/icmp.h> 46 #include <linux/spinlock.h> 47 #include <linux/workqueue.h> 48 #include <linux/bitops.h> 49 #include <linux/io.h> 50 #include <linux/irq.h> 51 #include <linux/clk.h> 52 #include <linux/crc32.h> 53 #include <linux/platform_device.h> 54 #include <linux/mdio.h> 55 #include <linux/phy.h> 56 #include <linux/fec.h> 57 #include <linux/of.h> 58 #include <linux/of_device.h> 59 #include <linux/of_mdio.h> 60 #include <linux/of_net.h> 61 #include <linux/regulator/consumer.h> 62 #include <linux/if_vlan.h> 63 #include <linux/pinctrl/consumer.h> 64 #include <linux/gpio/consumer.h> 65 #include <linux/prefetch.h> 66 #include <linux/mfd/syscon.h> 67 #include <linux/regmap.h> 68 #include <soc/imx/cpuidle.h> 69 #include <linux/filter.h> 70 #include <linux/bpf.h> 71 72 #include <asm/cacheflush.h> 73 74 #include "fec.h" 75 76 static void set_multicast_list(struct net_device *ndev); 77 static void fec_enet_itr_coal_set(struct net_device *ndev); 78 79 #define DRIVER_NAME "fec" 80 81 static const u16 fec_enet_vlan_pri_to_queue[8] = {0, 0, 1, 1, 1, 2, 2, 2}; 82 83 /* Pause frame feild and FIFO threshold */ 84 #define FEC_ENET_FCE (1 << 5) 85 #define FEC_ENET_RSEM_V 0x84 86 #define FEC_ENET_RSFL_V 16 87 #define FEC_ENET_RAEM_V 0x8 88 #define FEC_ENET_RAFL_V 0x8 89 #define FEC_ENET_OPD_V 0xFFF0 90 #define FEC_MDIO_PM_TIMEOUT 100 /* ms */ 91 92 #define FEC_ENET_XDP_PASS 0 93 #define FEC_ENET_XDP_CONSUMED BIT(0) 94 #define FEC_ENET_XDP_TX BIT(1) 95 #define FEC_ENET_XDP_REDIR BIT(2) 96 97 struct fec_devinfo { 98 u32 quirks; 99 }; 100 101 static const struct fec_devinfo fec_imx25_info = { 102 .quirks = FEC_QUIRK_USE_GASKET | FEC_QUIRK_MIB_CLEAR | 103 FEC_QUIRK_HAS_FRREG | FEC_QUIRK_HAS_MDIO_C45, 104 }; 105 106 static const struct fec_devinfo fec_imx27_info = { 107 .quirks = FEC_QUIRK_MIB_CLEAR | FEC_QUIRK_HAS_FRREG | 108 FEC_QUIRK_HAS_MDIO_C45, 109 }; 110 111 static const struct fec_devinfo fec_imx28_info = { 112 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_SWAP_FRAME | 113 FEC_QUIRK_SINGLE_MDIO | FEC_QUIRK_HAS_RACC | 114 FEC_QUIRK_HAS_FRREG | FEC_QUIRK_CLEAR_SETUP_MII | 115 FEC_QUIRK_NO_HARD_RESET | FEC_QUIRK_HAS_MDIO_C45, 116 }; 117 118 static const struct fec_devinfo fec_imx6q_info = { 119 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 120 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 121 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR006358 | 122 FEC_QUIRK_HAS_RACC | FEC_QUIRK_CLEAR_SETUP_MII | 123 FEC_QUIRK_HAS_PMQOS | FEC_QUIRK_HAS_MDIO_C45, 124 }; 125 126 static const struct fec_devinfo fec_mvf600_info = { 127 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_RACC | 128 FEC_QUIRK_HAS_MDIO_C45, 129 }; 130 131 static const struct fec_devinfo fec_imx6x_info = { 132 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 133 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 134 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 135 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 136 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE | 137 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES | 138 FEC_QUIRK_HAS_MDIO_C45, 139 }; 140 141 static const struct fec_devinfo fec_imx6ul_info = { 142 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 143 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 144 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_ERR007885 | 145 FEC_QUIRK_BUG_CAPTURE | FEC_QUIRK_HAS_RACC | 146 FEC_QUIRK_HAS_COALESCE | FEC_QUIRK_CLEAR_SETUP_MII | 147 FEC_QUIRK_HAS_MDIO_C45, 148 }; 149 150 static const struct fec_devinfo fec_imx8mq_info = { 151 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 152 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 153 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 154 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 155 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE | 156 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES | 157 FEC_QUIRK_HAS_EEE | FEC_QUIRK_WAKEUP_FROM_INT2 | 158 FEC_QUIRK_HAS_MDIO_C45, 159 }; 160 161 static const struct fec_devinfo fec_imx8qm_info = { 162 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 163 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 164 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 165 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 166 FEC_QUIRK_HAS_RACC | FEC_QUIRK_HAS_COALESCE | 167 FEC_QUIRK_CLEAR_SETUP_MII | FEC_QUIRK_HAS_MULTI_QUEUES | 168 FEC_QUIRK_DELAYED_CLKS_SUPPORT | FEC_QUIRK_HAS_MDIO_C45, 169 }; 170 171 static const struct fec_devinfo fec_s32v234_info = { 172 .quirks = FEC_QUIRK_ENET_MAC | FEC_QUIRK_HAS_GBIT | 173 FEC_QUIRK_HAS_BUFDESC_EX | FEC_QUIRK_HAS_CSUM | 174 FEC_QUIRK_HAS_VLAN | FEC_QUIRK_HAS_AVB | 175 FEC_QUIRK_ERR007885 | FEC_QUIRK_BUG_CAPTURE | 176 FEC_QUIRK_HAS_MDIO_C45, 177 }; 178 179 static struct platform_device_id fec_devtype[] = { 180 { 181 /* keep it for coldfire */ 182 .name = DRIVER_NAME, 183 .driver_data = 0, 184 }, { 185 .name = "imx25-fec", 186 .driver_data = (kernel_ulong_t)&fec_imx25_info, 187 }, { 188 .name = "imx27-fec", 189 .driver_data = (kernel_ulong_t)&fec_imx27_info, 190 }, { 191 .name = "imx28-fec", 192 .driver_data = (kernel_ulong_t)&fec_imx28_info, 193 }, { 194 .name = "imx6q-fec", 195 .driver_data = (kernel_ulong_t)&fec_imx6q_info, 196 }, { 197 .name = "mvf600-fec", 198 .driver_data = (kernel_ulong_t)&fec_mvf600_info, 199 }, { 200 .name = "imx6sx-fec", 201 .driver_data = (kernel_ulong_t)&fec_imx6x_info, 202 }, { 203 .name = "imx6ul-fec", 204 .driver_data = (kernel_ulong_t)&fec_imx6ul_info, 205 }, { 206 .name = "imx8mq-fec", 207 .driver_data = (kernel_ulong_t)&fec_imx8mq_info, 208 }, { 209 .name = "imx8qm-fec", 210 .driver_data = (kernel_ulong_t)&fec_imx8qm_info, 211 }, { 212 .name = "s32v234-fec", 213 .driver_data = (kernel_ulong_t)&fec_s32v234_info, 214 }, { 215 /* sentinel */ 216 } 217 }; 218 MODULE_DEVICE_TABLE(platform, fec_devtype); 219 220 enum imx_fec_type { 221 IMX25_FEC = 1, /* runs on i.mx25/50/53 */ 222 IMX27_FEC, /* runs on i.mx27/35/51 */ 223 IMX28_FEC, 224 IMX6Q_FEC, 225 MVF600_FEC, 226 IMX6SX_FEC, 227 IMX6UL_FEC, 228 IMX8MQ_FEC, 229 IMX8QM_FEC, 230 S32V234_FEC, 231 }; 232 233 static const struct of_device_id fec_dt_ids[] = { 234 { .compatible = "fsl,imx25-fec", .data = &fec_devtype[IMX25_FEC], }, 235 { .compatible = "fsl,imx27-fec", .data = &fec_devtype[IMX27_FEC], }, 236 { .compatible = "fsl,imx28-fec", .data = &fec_devtype[IMX28_FEC], }, 237 { .compatible = "fsl,imx6q-fec", .data = &fec_devtype[IMX6Q_FEC], }, 238 { .compatible = "fsl,mvf600-fec", .data = &fec_devtype[MVF600_FEC], }, 239 { .compatible = "fsl,imx6sx-fec", .data = &fec_devtype[IMX6SX_FEC], }, 240 { .compatible = "fsl,imx6ul-fec", .data = &fec_devtype[IMX6UL_FEC], }, 241 { .compatible = "fsl,imx8mq-fec", .data = &fec_devtype[IMX8MQ_FEC], }, 242 { .compatible = "fsl,imx8qm-fec", .data = &fec_devtype[IMX8QM_FEC], }, 243 { .compatible = "fsl,s32v234-fec", .data = &fec_devtype[S32V234_FEC], }, 244 { /* sentinel */ } 245 }; 246 MODULE_DEVICE_TABLE(of, fec_dt_ids); 247 248 static unsigned char macaddr[ETH_ALEN]; 249 module_param_array(macaddr, byte, NULL, 0); 250 MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address"); 251 252 #if defined(CONFIG_M5272) 253 /* 254 * Some hardware gets it MAC address out of local flash memory. 255 * if this is non-zero then assume it is the address to get MAC from. 256 */ 257 #if defined(CONFIG_NETtel) 258 #define FEC_FLASHMAC 0xf0006006 259 #elif defined(CONFIG_GILBARCONAP) || defined(CONFIG_SCALES) 260 #define FEC_FLASHMAC 0xf0006000 261 #elif defined(CONFIG_CANCam) 262 #define FEC_FLASHMAC 0xf0020000 263 #elif defined (CONFIG_M5272C3) 264 #define FEC_FLASHMAC (0xffe04000 + 4) 265 #elif defined(CONFIG_MOD5272) 266 #define FEC_FLASHMAC 0xffc0406b 267 #else 268 #define FEC_FLASHMAC 0 269 #endif 270 #endif /* CONFIG_M5272 */ 271 272 /* The FEC stores dest/src/type/vlan, data, and checksum for receive packets. 273 * 274 * 2048 byte skbufs are allocated. However, alignment requirements 275 * varies between FEC variants. Worst case is 64, so round down by 64. 276 */ 277 #define PKT_MAXBUF_SIZE (round_down(2048 - 64, 64)) 278 #define PKT_MINBUF_SIZE 64 279 280 /* FEC receive acceleration */ 281 #define FEC_RACC_IPDIS (1 << 1) 282 #define FEC_RACC_PRODIS (1 << 2) 283 #define FEC_RACC_SHIFT16 BIT(7) 284 #define FEC_RACC_OPTIONS (FEC_RACC_IPDIS | FEC_RACC_PRODIS) 285 286 /* MIB Control Register */ 287 #define FEC_MIB_CTRLSTAT_DISABLE BIT(31) 288 289 /* 290 * The 5270/5271/5280/5282/532x RX control register also contains maximum frame 291 * size bits. Other FEC hardware does not, so we need to take that into 292 * account when setting it. 293 */ 294 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 295 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 296 defined(CONFIG_ARM64) 297 #define OPT_FRAME_SIZE (PKT_MAXBUF_SIZE << 16) 298 #else 299 #define OPT_FRAME_SIZE 0 300 #endif 301 302 /* FEC MII MMFR bits definition */ 303 #define FEC_MMFR_ST (1 << 30) 304 #define FEC_MMFR_ST_C45 (0) 305 #define FEC_MMFR_OP_READ (2 << 28) 306 #define FEC_MMFR_OP_READ_C45 (3 << 28) 307 #define FEC_MMFR_OP_WRITE (1 << 28) 308 #define FEC_MMFR_OP_ADDR_WRITE (0) 309 #define FEC_MMFR_PA(v) ((v & 0x1f) << 23) 310 #define FEC_MMFR_RA(v) ((v & 0x1f) << 18) 311 #define FEC_MMFR_TA (2 << 16) 312 #define FEC_MMFR_DATA(v) (v & 0xffff) 313 /* FEC ECR bits definition */ 314 #define FEC_ECR_MAGICEN (1 << 2) 315 #define FEC_ECR_SLEEP (1 << 3) 316 317 #define FEC_MII_TIMEOUT 30000 /* us */ 318 319 /* Transmitter timeout */ 320 #define TX_TIMEOUT (2 * HZ) 321 322 #define FEC_PAUSE_FLAG_AUTONEG 0x1 323 #define FEC_PAUSE_FLAG_ENABLE 0x2 324 #define FEC_WOL_HAS_MAGIC_PACKET (0x1 << 0) 325 #define FEC_WOL_FLAG_ENABLE (0x1 << 1) 326 #define FEC_WOL_FLAG_SLEEP_ON (0x1 << 2) 327 328 /* Max number of allowed TCP segments for software TSO */ 329 #define FEC_MAX_TSO_SEGS 100 330 #define FEC_MAX_SKB_DESCS (FEC_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) 331 332 #define IS_TSO_HEADER(txq, addr) \ 333 ((addr >= txq->tso_hdrs_dma) && \ 334 (addr < txq->tso_hdrs_dma + txq->bd.ring_size * TSO_HEADER_SIZE)) 335 336 static int mii_cnt; 337 338 static struct bufdesc *fec_enet_get_nextdesc(struct bufdesc *bdp, 339 struct bufdesc_prop *bd) 340 { 341 return (bdp >= bd->last) ? bd->base 342 : (struct bufdesc *)(((void *)bdp) + bd->dsize); 343 } 344 345 static struct bufdesc *fec_enet_get_prevdesc(struct bufdesc *bdp, 346 struct bufdesc_prop *bd) 347 { 348 return (bdp <= bd->base) ? bd->last 349 : (struct bufdesc *)(((void *)bdp) - bd->dsize); 350 } 351 352 static int fec_enet_get_bd_index(struct bufdesc *bdp, 353 struct bufdesc_prop *bd) 354 { 355 return ((const char *)bdp - (const char *)bd->base) >> bd->dsize_log2; 356 } 357 358 static int fec_enet_get_free_txdesc_num(struct fec_enet_priv_tx_q *txq) 359 { 360 int entries; 361 362 entries = (((const char *)txq->dirty_tx - 363 (const char *)txq->bd.cur) >> txq->bd.dsize_log2) - 1; 364 365 return entries >= 0 ? entries : entries + txq->bd.ring_size; 366 } 367 368 static void swap_buffer(void *bufaddr, int len) 369 { 370 int i; 371 unsigned int *buf = bufaddr; 372 373 for (i = 0; i < len; i += 4, buf++) 374 swab32s(buf); 375 } 376 377 static void fec_dump(struct net_device *ndev) 378 { 379 struct fec_enet_private *fep = netdev_priv(ndev); 380 struct bufdesc *bdp; 381 struct fec_enet_priv_tx_q *txq; 382 int index = 0; 383 384 netdev_info(ndev, "TX ring dump\n"); 385 pr_info("Nr SC addr len SKB\n"); 386 387 txq = fep->tx_queue[0]; 388 bdp = txq->bd.base; 389 390 do { 391 pr_info("%3u %c%c 0x%04x 0x%08x %4u %p\n", 392 index, 393 bdp == txq->bd.cur ? 'S' : ' ', 394 bdp == txq->dirty_tx ? 'H' : ' ', 395 fec16_to_cpu(bdp->cbd_sc), 396 fec32_to_cpu(bdp->cbd_bufaddr), 397 fec16_to_cpu(bdp->cbd_datlen), 398 txq->tx_buf[index].skb); 399 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 400 index++; 401 } while (bdp != txq->bd.base); 402 } 403 404 static inline bool is_ipv4_pkt(struct sk_buff *skb) 405 { 406 return skb->protocol == htons(ETH_P_IP) && ip_hdr(skb)->version == 4; 407 } 408 409 static int 410 fec_enet_clear_csum(struct sk_buff *skb, struct net_device *ndev) 411 { 412 /* Only run for packets requiring a checksum. */ 413 if (skb->ip_summed != CHECKSUM_PARTIAL) 414 return 0; 415 416 if (unlikely(skb_cow_head(skb, 0))) 417 return -1; 418 419 if (is_ipv4_pkt(skb)) 420 ip_hdr(skb)->check = 0; 421 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) = 0; 422 423 return 0; 424 } 425 426 static int 427 fec_enet_create_page_pool(struct fec_enet_private *fep, 428 struct fec_enet_priv_rx_q *rxq, int size) 429 { 430 struct bpf_prog *xdp_prog = READ_ONCE(fep->xdp_prog); 431 struct page_pool_params pp_params = { 432 .order = 0, 433 .flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV, 434 .pool_size = size, 435 .nid = dev_to_node(&fep->pdev->dev), 436 .dev = &fep->pdev->dev, 437 .dma_dir = xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE, 438 .offset = FEC_ENET_XDP_HEADROOM, 439 .max_len = FEC_ENET_RX_FRSIZE, 440 }; 441 int err; 442 443 rxq->page_pool = page_pool_create(&pp_params); 444 if (IS_ERR(rxq->page_pool)) { 445 err = PTR_ERR(rxq->page_pool); 446 rxq->page_pool = NULL; 447 return err; 448 } 449 450 err = xdp_rxq_info_reg(&rxq->xdp_rxq, fep->netdev, rxq->id, 0); 451 if (err < 0) 452 goto err_free_pp; 453 454 err = xdp_rxq_info_reg_mem_model(&rxq->xdp_rxq, MEM_TYPE_PAGE_POOL, 455 rxq->page_pool); 456 if (err) 457 goto err_unregister_rxq; 458 459 return 0; 460 461 err_unregister_rxq: 462 xdp_rxq_info_unreg(&rxq->xdp_rxq); 463 err_free_pp: 464 page_pool_destroy(rxq->page_pool); 465 rxq->page_pool = NULL; 466 return err; 467 } 468 469 static struct bufdesc * 470 fec_enet_txq_submit_frag_skb(struct fec_enet_priv_tx_q *txq, 471 struct sk_buff *skb, 472 struct net_device *ndev) 473 { 474 struct fec_enet_private *fep = netdev_priv(ndev); 475 struct bufdesc *bdp = txq->bd.cur; 476 struct bufdesc_ex *ebdp; 477 int nr_frags = skb_shinfo(skb)->nr_frags; 478 int frag, frag_len; 479 unsigned short status; 480 unsigned int estatus = 0; 481 skb_frag_t *this_frag; 482 unsigned int index; 483 void *bufaddr; 484 dma_addr_t addr; 485 int i; 486 487 for (frag = 0; frag < nr_frags; frag++) { 488 this_frag = &skb_shinfo(skb)->frags[frag]; 489 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 490 ebdp = (struct bufdesc_ex *)bdp; 491 492 status = fec16_to_cpu(bdp->cbd_sc); 493 status &= ~BD_ENET_TX_STATS; 494 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 495 frag_len = skb_frag_size(&skb_shinfo(skb)->frags[frag]); 496 497 /* Handle the last BD specially */ 498 if (frag == nr_frags - 1) { 499 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 500 if (fep->bufdesc_ex) { 501 estatus |= BD_ENET_TX_INT; 502 if (unlikely(skb_shinfo(skb)->tx_flags & 503 SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 504 estatus |= BD_ENET_TX_TS; 505 } 506 } 507 508 if (fep->bufdesc_ex) { 509 if (fep->quirks & FEC_QUIRK_HAS_AVB) 510 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 511 if (skb->ip_summed == CHECKSUM_PARTIAL) 512 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 513 514 ebdp->cbd_bdu = 0; 515 ebdp->cbd_esc = cpu_to_fec32(estatus); 516 } 517 518 bufaddr = skb_frag_address(this_frag); 519 520 index = fec_enet_get_bd_index(bdp, &txq->bd); 521 if (((unsigned long) bufaddr) & fep->tx_align || 522 fep->quirks & FEC_QUIRK_SWAP_FRAME) { 523 memcpy(txq->tx_bounce[index], bufaddr, frag_len); 524 bufaddr = txq->tx_bounce[index]; 525 526 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 527 swap_buffer(bufaddr, frag_len); 528 } 529 530 addr = dma_map_single(&fep->pdev->dev, bufaddr, frag_len, 531 DMA_TO_DEVICE); 532 if (dma_mapping_error(&fep->pdev->dev, addr)) { 533 if (net_ratelimit()) 534 netdev_err(ndev, "Tx DMA memory map failed\n"); 535 goto dma_mapping_error; 536 } 537 538 bdp->cbd_bufaddr = cpu_to_fec32(addr); 539 bdp->cbd_datlen = cpu_to_fec16(frag_len); 540 /* Make sure the updates to rest of the descriptor are 541 * performed before transferring ownership. 542 */ 543 wmb(); 544 bdp->cbd_sc = cpu_to_fec16(status); 545 } 546 547 return bdp; 548 dma_mapping_error: 549 bdp = txq->bd.cur; 550 for (i = 0; i < frag; i++) { 551 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 552 dma_unmap_single(&fep->pdev->dev, fec32_to_cpu(bdp->cbd_bufaddr), 553 fec16_to_cpu(bdp->cbd_datlen), DMA_TO_DEVICE); 554 } 555 return ERR_PTR(-ENOMEM); 556 } 557 558 static int fec_enet_txq_submit_skb(struct fec_enet_priv_tx_q *txq, 559 struct sk_buff *skb, struct net_device *ndev) 560 { 561 struct fec_enet_private *fep = netdev_priv(ndev); 562 int nr_frags = skb_shinfo(skb)->nr_frags; 563 struct bufdesc *bdp, *last_bdp; 564 void *bufaddr; 565 dma_addr_t addr; 566 unsigned short status; 567 unsigned short buflen; 568 unsigned int estatus = 0; 569 unsigned int index; 570 int entries_free; 571 572 entries_free = fec_enet_get_free_txdesc_num(txq); 573 if (entries_free < MAX_SKB_FRAGS + 1) { 574 dev_kfree_skb_any(skb); 575 if (net_ratelimit()) 576 netdev_err(ndev, "NOT enough BD for SG!\n"); 577 return NETDEV_TX_OK; 578 } 579 580 /* Protocol checksum off-load for TCP and UDP. */ 581 if (fec_enet_clear_csum(skb, ndev)) { 582 dev_kfree_skb_any(skb); 583 return NETDEV_TX_OK; 584 } 585 586 /* Fill in a Tx ring entry */ 587 bdp = txq->bd.cur; 588 last_bdp = bdp; 589 status = fec16_to_cpu(bdp->cbd_sc); 590 status &= ~BD_ENET_TX_STATS; 591 592 /* Set buffer length and buffer pointer */ 593 bufaddr = skb->data; 594 buflen = skb_headlen(skb); 595 596 index = fec_enet_get_bd_index(bdp, &txq->bd); 597 if (((unsigned long) bufaddr) & fep->tx_align || 598 fep->quirks & FEC_QUIRK_SWAP_FRAME) { 599 memcpy(txq->tx_bounce[index], skb->data, buflen); 600 bufaddr = txq->tx_bounce[index]; 601 602 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 603 swap_buffer(bufaddr, buflen); 604 } 605 606 /* Push the data cache so the CPM does not get stale memory data. */ 607 addr = dma_map_single(&fep->pdev->dev, bufaddr, buflen, DMA_TO_DEVICE); 608 if (dma_mapping_error(&fep->pdev->dev, addr)) { 609 dev_kfree_skb_any(skb); 610 if (net_ratelimit()) 611 netdev_err(ndev, "Tx DMA memory map failed\n"); 612 return NETDEV_TX_OK; 613 } 614 615 if (nr_frags) { 616 last_bdp = fec_enet_txq_submit_frag_skb(txq, skb, ndev); 617 if (IS_ERR(last_bdp)) { 618 dma_unmap_single(&fep->pdev->dev, addr, 619 buflen, DMA_TO_DEVICE); 620 dev_kfree_skb_any(skb); 621 return NETDEV_TX_OK; 622 } 623 } else { 624 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 625 if (fep->bufdesc_ex) { 626 estatus = BD_ENET_TX_INT; 627 if (unlikely(skb_shinfo(skb)->tx_flags & 628 SKBTX_HW_TSTAMP && fep->hwts_tx_en)) 629 estatus |= BD_ENET_TX_TS; 630 } 631 } 632 bdp->cbd_bufaddr = cpu_to_fec32(addr); 633 bdp->cbd_datlen = cpu_to_fec16(buflen); 634 635 if (fep->bufdesc_ex) { 636 637 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 638 639 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 640 fep->hwts_tx_en)) 641 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 642 643 if (fep->quirks & FEC_QUIRK_HAS_AVB) 644 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 645 646 if (skb->ip_summed == CHECKSUM_PARTIAL) 647 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 648 649 ebdp->cbd_bdu = 0; 650 ebdp->cbd_esc = cpu_to_fec32(estatus); 651 } 652 653 index = fec_enet_get_bd_index(last_bdp, &txq->bd); 654 /* Save skb pointer */ 655 txq->tx_buf[index].skb = skb; 656 657 /* Make sure the updates to rest of the descriptor are performed before 658 * transferring ownership. 659 */ 660 wmb(); 661 662 /* Send it on its way. Tell FEC it's ready, interrupt when done, 663 * it's the last BD of the frame, and to put the CRC on the end. 664 */ 665 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC); 666 bdp->cbd_sc = cpu_to_fec16(status); 667 668 /* If this was the last BD in the ring, start at the beginning again. */ 669 bdp = fec_enet_get_nextdesc(last_bdp, &txq->bd); 670 671 skb_tx_timestamp(skb); 672 673 /* Make sure the update to bdp is performed before txq->bd.cur. */ 674 wmb(); 675 txq->bd.cur = bdp; 676 677 /* Trigger transmission start */ 678 writel(0, txq->bd.reg_desc_active); 679 680 return 0; 681 } 682 683 static int 684 fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb, 685 struct net_device *ndev, 686 struct bufdesc *bdp, int index, char *data, 687 int size, bool last_tcp, bool is_last) 688 { 689 struct fec_enet_private *fep = netdev_priv(ndev); 690 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 691 unsigned short status; 692 unsigned int estatus = 0; 693 dma_addr_t addr; 694 695 status = fec16_to_cpu(bdp->cbd_sc); 696 status &= ~BD_ENET_TX_STATS; 697 698 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 699 700 if (((unsigned long) data) & fep->tx_align || 701 fep->quirks & FEC_QUIRK_SWAP_FRAME) { 702 memcpy(txq->tx_bounce[index], data, size); 703 data = txq->tx_bounce[index]; 704 705 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 706 swap_buffer(data, size); 707 } 708 709 addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE); 710 if (dma_mapping_error(&fep->pdev->dev, addr)) { 711 dev_kfree_skb_any(skb); 712 if (net_ratelimit()) 713 netdev_err(ndev, "Tx DMA memory map failed\n"); 714 return NETDEV_TX_OK; 715 } 716 717 bdp->cbd_datlen = cpu_to_fec16(size); 718 bdp->cbd_bufaddr = cpu_to_fec32(addr); 719 720 if (fep->bufdesc_ex) { 721 if (fep->quirks & FEC_QUIRK_HAS_AVB) 722 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 723 if (skb->ip_summed == CHECKSUM_PARTIAL) 724 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 725 ebdp->cbd_bdu = 0; 726 ebdp->cbd_esc = cpu_to_fec32(estatus); 727 } 728 729 /* Handle the last BD specially */ 730 if (last_tcp) 731 status |= (BD_ENET_TX_LAST | BD_ENET_TX_TC); 732 if (is_last) { 733 status |= BD_ENET_TX_INTR; 734 if (fep->bufdesc_ex) 735 ebdp->cbd_esc |= cpu_to_fec32(BD_ENET_TX_INT); 736 } 737 738 bdp->cbd_sc = cpu_to_fec16(status); 739 740 return 0; 741 } 742 743 static int 744 fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq, 745 struct sk_buff *skb, struct net_device *ndev, 746 struct bufdesc *bdp, int index) 747 { 748 struct fec_enet_private *fep = netdev_priv(ndev); 749 int hdr_len = skb_tcp_all_headers(skb); 750 struct bufdesc_ex *ebdp = container_of(bdp, struct bufdesc_ex, desc); 751 void *bufaddr; 752 unsigned long dmabuf; 753 unsigned short status; 754 unsigned int estatus = 0; 755 756 status = fec16_to_cpu(bdp->cbd_sc); 757 status &= ~BD_ENET_TX_STATS; 758 status |= (BD_ENET_TX_TC | BD_ENET_TX_READY); 759 760 bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 761 dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE; 762 if (((unsigned long)bufaddr) & fep->tx_align || 763 fep->quirks & FEC_QUIRK_SWAP_FRAME) { 764 memcpy(txq->tx_bounce[index], skb->data, hdr_len); 765 bufaddr = txq->tx_bounce[index]; 766 767 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 768 swap_buffer(bufaddr, hdr_len); 769 770 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr, 771 hdr_len, DMA_TO_DEVICE); 772 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) { 773 dev_kfree_skb_any(skb); 774 if (net_ratelimit()) 775 netdev_err(ndev, "Tx DMA memory map failed\n"); 776 return NETDEV_TX_OK; 777 } 778 } 779 780 bdp->cbd_bufaddr = cpu_to_fec32(dmabuf); 781 bdp->cbd_datlen = cpu_to_fec16(hdr_len); 782 783 if (fep->bufdesc_ex) { 784 if (fep->quirks & FEC_QUIRK_HAS_AVB) 785 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 786 if (skb->ip_summed == CHECKSUM_PARTIAL) 787 estatus |= BD_ENET_TX_PINS | BD_ENET_TX_IINS; 788 ebdp->cbd_bdu = 0; 789 ebdp->cbd_esc = cpu_to_fec32(estatus); 790 } 791 792 bdp->cbd_sc = cpu_to_fec16(status); 793 794 return 0; 795 } 796 797 static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq, 798 struct sk_buff *skb, 799 struct net_device *ndev) 800 { 801 struct fec_enet_private *fep = netdev_priv(ndev); 802 int hdr_len, total_len, data_left; 803 struct bufdesc *bdp = txq->bd.cur; 804 struct tso_t tso; 805 unsigned int index = 0; 806 int ret; 807 808 if (tso_count_descs(skb) >= fec_enet_get_free_txdesc_num(txq)) { 809 dev_kfree_skb_any(skb); 810 if (net_ratelimit()) 811 netdev_err(ndev, "NOT enough BD for TSO!\n"); 812 return NETDEV_TX_OK; 813 } 814 815 /* Protocol checksum off-load for TCP and UDP. */ 816 if (fec_enet_clear_csum(skb, ndev)) { 817 dev_kfree_skb_any(skb); 818 return NETDEV_TX_OK; 819 } 820 821 /* Initialize the TSO handler, and prepare the first payload */ 822 hdr_len = tso_start(skb, &tso); 823 824 total_len = skb->len - hdr_len; 825 while (total_len > 0) { 826 char *hdr; 827 828 index = fec_enet_get_bd_index(bdp, &txq->bd); 829 data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); 830 total_len -= data_left; 831 832 /* prepare packet headers: MAC + IP + TCP */ 833 hdr = txq->tso_hdrs + index * TSO_HEADER_SIZE; 834 tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); 835 ret = fec_enet_txq_put_hdr_tso(txq, skb, ndev, bdp, index); 836 if (ret) 837 goto err_release; 838 839 while (data_left > 0) { 840 int size; 841 842 size = min_t(int, tso.size, data_left); 843 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 844 index = fec_enet_get_bd_index(bdp, &txq->bd); 845 ret = fec_enet_txq_put_data_tso(txq, skb, ndev, 846 bdp, index, 847 tso.data, size, 848 size == data_left, 849 total_len == 0); 850 if (ret) 851 goto err_release; 852 853 data_left -= size; 854 tso_build_data(skb, &tso, size); 855 } 856 857 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 858 } 859 860 /* Save skb pointer */ 861 txq->tx_buf[index].skb = skb; 862 863 skb_tx_timestamp(skb); 864 txq->bd.cur = bdp; 865 866 /* Trigger transmission start */ 867 if (!(fep->quirks & FEC_QUIRK_ERR007885) || 868 !readl(txq->bd.reg_desc_active) || 869 !readl(txq->bd.reg_desc_active) || 870 !readl(txq->bd.reg_desc_active) || 871 !readl(txq->bd.reg_desc_active)) 872 writel(0, txq->bd.reg_desc_active); 873 874 return 0; 875 876 err_release: 877 /* TODO: Release all used data descriptors for TSO */ 878 return ret; 879 } 880 881 static netdev_tx_t 882 fec_enet_start_xmit(struct sk_buff *skb, struct net_device *ndev) 883 { 884 struct fec_enet_private *fep = netdev_priv(ndev); 885 int entries_free; 886 unsigned short queue; 887 struct fec_enet_priv_tx_q *txq; 888 struct netdev_queue *nq; 889 int ret; 890 891 queue = skb_get_queue_mapping(skb); 892 txq = fep->tx_queue[queue]; 893 nq = netdev_get_tx_queue(ndev, queue); 894 895 if (skb_is_gso(skb)) 896 ret = fec_enet_txq_submit_tso(txq, skb, ndev); 897 else 898 ret = fec_enet_txq_submit_skb(txq, skb, ndev); 899 if (ret) 900 return ret; 901 902 entries_free = fec_enet_get_free_txdesc_num(txq); 903 if (entries_free <= txq->tx_stop_threshold) 904 netif_tx_stop_queue(nq); 905 906 return NETDEV_TX_OK; 907 } 908 909 /* Init RX & TX buffer descriptors 910 */ 911 static void fec_enet_bd_init(struct net_device *dev) 912 { 913 struct fec_enet_private *fep = netdev_priv(dev); 914 struct fec_enet_priv_tx_q *txq; 915 struct fec_enet_priv_rx_q *rxq; 916 struct bufdesc *bdp; 917 unsigned int i; 918 unsigned int q; 919 920 for (q = 0; q < fep->num_rx_queues; q++) { 921 /* Initialize the receive buffer descriptors. */ 922 rxq = fep->rx_queue[q]; 923 bdp = rxq->bd.base; 924 925 for (i = 0; i < rxq->bd.ring_size; i++) { 926 927 /* Initialize the BD for every fragment in the page. */ 928 if (bdp->cbd_bufaddr) 929 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY); 930 else 931 bdp->cbd_sc = cpu_to_fec16(0); 932 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 933 } 934 935 /* Set the last buffer to wrap */ 936 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd); 937 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 938 939 rxq->bd.cur = rxq->bd.base; 940 } 941 942 for (q = 0; q < fep->num_tx_queues; q++) { 943 /* ...and the same for transmit */ 944 txq = fep->tx_queue[q]; 945 bdp = txq->bd.base; 946 txq->bd.cur = bdp; 947 948 for (i = 0; i < txq->bd.ring_size; i++) { 949 /* Initialize the BD for every fragment in the page. */ 950 bdp->cbd_sc = cpu_to_fec16(0); 951 if (txq->tx_buf[i].type == FEC_TXBUF_T_SKB) { 952 if (bdp->cbd_bufaddr && 953 !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr))) 954 dma_unmap_single(&fep->pdev->dev, 955 fec32_to_cpu(bdp->cbd_bufaddr), 956 fec16_to_cpu(bdp->cbd_datlen), 957 DMA_TO_DEVICE); 958 if (txq->tx_buf[i].skb) { 959 dev_kfree_skb_any(txq->tx_buf[i].skb); 960 txq->tx_buf[i].skb = NULL; 961 } 962 } else { 963 if (bdp->cbd_bufaddr) 964 dma_unmap_single(&fep->pdev->dev, 965 fec32_to_cpu(bdp->cbd_bufaddr), 966 fec16_to_cpu(bdp->cbd_datlen), 967 DMA_TO_DEVICE); 968 969 if (txq->tx_buf[i].xdp) { 970 xdp_return_frame(txq->tx_buf[i].xdp); 971 txq->tx_buf[i].xdp = NULL; 972 } 973 974 /* restore default tx buffer type: FEC_TXBUF_T_SKB */ 975 txq->tx_buf[i].type = FEC_TXBUF_T_SKB; 976 } 977 978 bdp->cbd_bufaddr = cpu_to_fec32(0); 979 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 980 } 981 982 /* Set the last buffer to wrap */ 983 bdp = fec_enet_get_prevdesc(bdp, &txq->bd); 984 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 985 txq->dirty_tx = bdp; 986 } 987 } 988 989 static void fec_enet_active_rxring(struct net_device *ndev) 990 { 991 struct fec_enet_private *fep = netdev_priv(ndev); 992 int i; 993 994 for (i = 0; i < fep->num_rx_queues; i++) 995 writel(0, fep->rx_queue[i]->bd.reg_desc_active); 996 } 997 998 static void fec_enet_enable_ring(struct net_device *ndev) 999 { 1000 struct fec_enet_private *fep = netdev_priv(ndev); 1001 struct fec_enet_priv_tx_q *txq; 1002 struct fec_enet_priv_rx_q *rxq; 1003 int i; 1004 1005 for (i = 0; i < fep->num_rx_queues; i++) { 1006 rxq = fep->rx_queue[i]; 1007 writel(rxq->bd.dma, fep->hwp + FEC_R_DES_START(i)); 1008 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_R_BUFF_SIZE(i)); 1009 1010 /* enable DMA1/2 */ 1011 if (i) 1012 writel(RCMR_MATCHEN | RCMR_CMP(i), 1013 fep->hwp + FEC_RCMR(i)); 1014 } 1015 1016 for (i = 0; i < fep->num_tx_queues; i++) { 1017 txq = fep->tx_queue[i]; 1018 writel(txq->bd.dma, fep->hwp + FEC_X_DES_START(i)); 1019 1020 /* enable DMA1/2 */ 1021 if (i) 1022 writel(DMA_CLASS_EN | IDLE_SLOPE(i), 1023 fep->hwp + FEC_DMA_CFG(i)); 1024 } 1025 } 1026 1027 /* 1028 * This function is called to start or restart the FEC during a link 1029 * change, transmit timeout, or to reconfigure the FEC. The network 1030 * packet processing for this device must be stopped before this call. 1031 */ 1032 static void 1033 fec_restart(struct net_device *ndev) 1034 { 1035 struct fec_enet_private *fep = netdev_priv(ndev); 1036 u32 temp_mac[2]; 1037 u32 rcntl = OPT_FRAME_SIZE | 0x04; 1038 u32 ecntl = 0x2; /* ETHEREN */ 1039 1040 /* Whack a reset. We should wait for this. 1041 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 1042 * instead of reset MAC itself. 1043 */ 1044 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES || 1045 ((fep->quirks & FEC_QUIRK_NO_HARD_RESET) && fep->link)) { 1046 writel(0, fep->hwp + FEC_ECNTRL); 1047 } else { 1048 writel(1, fep->hwp + FEC_ECNTRL); 1049 udelay(10); 1050 } 1051 1052 /* 1053 * enet-mac reset will reset mac address registers too, 1054 * so need to reconfigure it. 1055 */ 1056 memcpy(&temp_mac, ndev->dev_addr, ETH_ALEN); 1057 writel((__force u32)cpu_to_be32(temp_mac[0]), 1058 fep->hwp + FEC_ADDR_LOW); 1059 writel((__force u32)cpu_to_be32(temp_mac[1]), 1060 fep->hwp + FEC_ADDR_HIGH); 1061 1062 /* Clear any outstanding interrupt, except MDIO. */ 1063 writel((0xffffffff & ~FEC_ENET_MII), fep->hwp + FEC_IEVENT); 1064 1065 fec_enet_bd_init(ndev); 1066 1067 fec_enet_enable_ring(ndev); 1068 1069 /* Enable MII mode */ 1070 if (fep->full_duplex == DUPLEX_FULL) { 1071 /* FD enable */ 1072 writel(0x04, fep->hwp + FEC_X_CNTRL); 1073 } else { 1074 /* No Rcv on Xmit */ 1075 rcntl |= 0x02; 1076 writel(0x0, fep->hwp + FEC_X_CNTRL); 1077 } 1078 1079 /* Set MII speed */ 1080 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 1081 1082 #if !defined(CONFIG_M5272) 1083 if (fep->quirks & FEC_QUIRK_HAS_RACC) { 1084 u32 val = readl(fep->hwp + FEC_RACC); 1085 1086 /* align IP header */ 1087 val |= FEC_RACC_SHIFT16; 1088 if (fep->csum_flags & FLAG_RX_CSUM_ENABLED) 1089 /* set RX checksum */ 1090 val |= FEC_RACC_OPTIONS; 1091 else 1092 val &= ~FEC_RACC_OPTIONS; 1093 writel(val, fep->hwp + FEC_RACC); 1094 writel(PKT_MAXBUF_SIZE, fep->hwp + FEC_FTRL); 1095 } 1096 #endif 1097 1098 /* 1099 * The phy interface and speed need to get configured 1100 * differently on enet-mac. 1101 */ 1102 if (fep->quirks & FEC_QUIRK_ENET_MAC) { 1103 /* Enable flow control and length check */ 1104 rcntl |= 0x40000000 | 0x00000020; 1105 1106 /* RGMII, RMII or MII */ 1107 if (fep->phy_interface == PHY_INTERFACE_MODE_RGMII || 1108 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_ID || 1109 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID || 1110 fep->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) 1111 rcntl |= (1 << 6); 1112 else if (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 1113 rcntl |= (1 << 8); 1114 else 1115 rcntl &= ~(1 << 8); 1116 1117 /* 1G, 100M or 10M */ 1118 if (ndev->phydev) { 1119 if (ndev->phydev->speed == SPEED_1000) 1120 ecntl |= (1 << 5); 1121 else if (ndev->phydev->speed == SPEED_100) 1122 rcntl &= ~(1 << 9); 1123 else 1124 rcntl |= (1 << 9); 1125 } 1126 } else { 1127 #ifdef FEC_MIIGSK_ENR 1128 if (fep->quirks & FEC_QUIRK_USE_GASKET) { 1129 u32 cfgr; 1130 /* disable the gasket and wait */ 1131 writel(0, fep->hwp + FEC_MIIGSK_ENR); 1132 while (readl(fep->hwp + FEC_MIIGSK_ENR) & 4) 1133 udelay(1); 1134 1135 /* 1136 * configure the gasket: 1137 * RMII, 50 MHz, no loopback, no echo 1138 * MII, 25 MHz, no loopback, no echo 1139 */ 1140 cfgr = (fep->phy_interface == PHY_INTERFACE_MODE_RMII) 1141 ? BM_MIIGSK_CFGR_RMII : BM_MIIGSK_CFGR_MII; 1142 if (ndev->phydev && ndev->phydev->speed == SPEED_10) 1143 cfgr |= BM_MIIGSK_CFGR_FRCONT_10M; 1144 writel(cfgr, fep->hwp + FEC_MIIGSK_CFGR); 1145 1146 /* re-enable the gasket */ 1147 writel(2, fep->hwp + FEC_MIIGSK_ENR); 1148 } 1149 #endif 1150 } 1151 1152 #if !defined(CONFIG_M5272) 1153 /* enable pause frame*/ 1154 if ((fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) || 1155 ((fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) && 1156 ndev->phydev && ndev->phydev->pause)) { 1157 rcntl |= FEC_ENET_FCE; 1158 1159 /* set FIFO threshold parameter to reduce overrun */ 1160 writel(FEC_ENET_RSEM_V, fep->hwp + FEC_R_FIFO_RSEM); 1161 writel(FEC_ENET_RSFL_V, fep->hwp + FEC_R_FIFO_RSFL); 1162 writel(FEC_ENET_RAEM_V, fep->hwp + FEC_R_FIFO_RAEM); 1163 writel(FEC_ENET_RAFL_V, fep->hwp + FEC_R_FIFO_RAFL); 1164 1165 /* OPD */ 1166 writel(FEC_ENET_OPD_V, fep->hwp + FEC_OPD); 1167 } else { 1168 rcntl &= ~FEC_ENET_FCE; 1169 } 1170 #endif /* !defined(CONFIG_M5272) */ 1171 1172 writel(rcntl, fep->hwp + FEC_R_CNTRL); 1173 1174 /* Setup multicast filter. */ 1175 set_multicast_list(ndev); 1176 #ifndef CONFIG_M5272 1177 writel(0, fep->hwp + FEC_HASH_TABLE_HIGH); 1178 writel(0, fep->hwp + FEC_HASH_TABLE_LOW); 1179 #endif 1180 1181 if (fep->quirks & FEC_QUIRK_ENET_MAC) { 1182 /* enable ENET endian swap */ 1183 ecntl |= (1 << 8); 1184 /* enable ENET store and forward mode */ 1185 writel(1 << 8, fep->hwp + FEC_X_WMRK); 1186 } 1187 1188 if (fep->bufdesc_ex) 1189 ecntl |= (1 << 4); 1190 1191 if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT && 1192 fep->rgmii_txc_dly) 1193 ecntl |= FEC_ENET_TXC_DLY; 1194 if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT && 1195 fep->rgmii_rxc_dly) 1196 ecntl |= FEC_ENET_RXC_DLY; 1197 1198 #ifndef CONFIG_M5272 1199 /* Enable the MIB statistic event counters */ 1200 writel(0 << 31, fep->hwp + FEC_MIB_CTRLSTAT); 1201 #endif 1202 1203 /* And last, enable the transmit and receive processing */ 1204 writel(ecntl, fep->hwp + FEC_ECNTRL); 1205 fec_enet_active_rxring(ndev); 1206 1207 if (fep->bufdesc_ex) 1208 fec_ptp_start_cyclecounter(ndev); 1209 1210 /* Enable interrupts we wish to service */ 1211 if (fep->link) 1212 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1213 else 1214 writel(0, fep->hwp + FEC_IMASK); 1215 1216 /* Init the interrupt coalescing */ 1217 if (fep->quirks & FEC_QUIRK_HAS_COALESCE) 1218 fec_enet_itr_coal_set(ndev); 1219 } 1220 1221 static int fec_enet_ipc_handle_init(struct fec_enet_private *fep) 1222 { 1223 if (!(of_machine_is_compatible("fsl,imx8qm") || 1224 of_machine_is_compatible("fsl,imx8qxp") || 1225 of_machine_is_compatible("fsl,imx8dxl"))) 1226 return 0; 1227 1228 return imx_scu_get_handle(&fep->ipc_handle); 1229 } 1230 1231 static void fec_enet_ipg_stop_set(struct fec_enet_private *fep, bool enabled) 1232 { 1233 struct device_node *np = fep->pdev->dev.of_node; 1234 u32 rsrc_id, val; 1235 int idx; 1236 1237 if (!np || !fep->ipc_handle) 1238 return; 1239 1240 idx = of_alias_get_id(np, "ethernet"); 1241 if (idx < 0) 1242 idx = 0; 1243 rsrc_id = idx ? IMX_SC_R_ENET_1 : IMX_SC_R_ENET_0; 1244 1245 val = enabled ? 1 : 0; 1246 imx_sc_misc_set_control(fep->ipc_handle, rsrc_id, IMX_SC_C_IPG_STOP, val); 1247 } 1248 1249 static void fec_enet_stop_mode(struct fec_enet_private *fep, bool enabled) 1250 { 1251 struct fec_platform_data *pdata = fep->pdev->dev.platform_data; 1252 struct fec_stop_mode_gpr *stop_gpr = &fep->stop_gpr; 1253 1254 if (stop_gpr->gpr) { 1255 if (enabled) 1256 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg, 1257 BIT(stop_gpr->bit), 1258 BIT(stop_gpr->bit)); 1259 else 1260 regmap_update_bits(stop_gpr->gpr, stop_gpr->reg, 1261 BIT(stop_gpr->bit), 0); 1262 } else if (pdata && pdata->sleep_mode_enable) { 1263 pdata->sleep_mode_enable(enabled); 1264 } else { 1265 fec_enet_ipg_stop_set(fep, enabled); 1266 } 1267 } 1268 1269 static void fec_irqs_disable(struct net_device *ndev) 1270 { 1271 struct fec_enet_private *fep = netdev_priv(ndev); 1272 1273 writel(0, fep->hwp + FEC_IMASK); 1274 } 1275 1276 static void fec_irqs_disable_except_wakeup(struct net_device *ndev) 1277 { 1278 struct fec_enet_private *fep = netdev_priv(ndev); 1279 1280 writel(0, fep->hwp + FEC_IMASK); 1281 writel(FEC_ENET_WAKEUP, fep->hwp + FEC_IMASK); 1282 } 1283 1284 static void 1285 fec_stop(struct net_device *ndev) 1286 { 1287 struct fec_enet_private *fep = netdev_priv(ndev); 1288 u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8); 1289 u32 val; 1290 1291 /* We cannot expect a graceful transmit stop without link !!! */ 1292 if (fep->link) { 1293 writel(1, fep->hwp + FEC_X_CNTRL); /* Graceful transmit stop */ 1294 udelay(10); 1295 if (!(readl(fep->hwp + FEC_IEVENT) & FEC_ENET_GRA)) 1296 netdev_err(ndev, "Graceful transmit stop did not complete!\n"); 1297 } 1298 1299 /* Whack a reset. We should wait for this. 1300 * For i.MX6SX SOC, enet use AXI bus, we use disable MAC 1301 * instead of reset MAC itself. 1302 */ 1303 if (!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) { 1304 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) { 1305 writel(0, fep->hwp + FEC_ECNTRL); 1306 } else { 1307 writel(1, fep->hwp + FEC_ECNTRL); 1308 udelay(10); 1309 } 1310 } else { 1311 val = readl(fep->hwp + FEC_ECNTRL); 1312 val |= (FEC_ECR_MAGICEN | FEC_ECR_SLEEP); 1313 writel(val, fep->hwp + FEC_ECNTRL); 1314 } 1315 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 1316 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1317 1318 /* We have to keep ENET enabled to have MII interrupt stay working */ 1319 if (fep->quirks & FEC_QUIRK_ENET_MAC && 1320 !(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) { 1321 writel(2, fep->hwp + FEC_ECNTRL); 1322 writel(rmii_mode, fep->hwp + FEC_R_CNTRL); 1323 } 1324 } 1325 1326 1327 static void 1328 fec_timeout(struct net_device *ndev, unsigned int txqueue) 1329 { 1330 struct fec_enet_private *fep = netdev_priv(ndev); 1331 1332 fec_dump(ndev); 1333 1334 ndev->stats.tx_errors++; 1335 1336 schedule_work(&fep->tx_timeout_work); 1337 } 1338 1339 static void fec_enet_timeout_work(struct work_struct *work) 1340 { 1341 struct fec_enet_private *fep = 1342 container_of(work, struct fec_enet_private, tx_timeout_work); 1343 struct net_device *ndev = fep->netdev; 1344 1345 rtnl_lock(); 1346 if (netif_device_present(ndev) || netif_running(ndev)) { 1347 napi_disable(&fep->napi); 1348 netif_tx_lock_bh(ndev); 1349 fec_restart(ndev); 1350 netif_tx_wake_all_queues(ndev); 1351 netif_tx_unlock_bh(ndev); 1352 napi_enable(&fep->napi); 1353 } 1354 rtnl_unlock(); 1355 } 1356 1357 static void 1358 fec_enet_hwtstamp(struct fec_enet_private *fep, unsigned ts, 1359 struct skb_shared_hwtstamps *hwtstamps) 1360 { 1361 unsigned long flags; 1362 u64 ns; 1363 1364 spin_lock_irqsave(&fep->tmreg_lock, flags); 1365 ns = timecounter_cyc2time(&fep->tc, ts); 1366 spin_unlock_irqrestore(&fep->tmreg_lock, flags); 1367 1368 memset(hwtstamps, 0, sizeof(*hwtstamps)); 1369 hwtstamps->hwtstamp = ns_to_ktime(ns); 1370 } 1371 1372 static void 1373 fec_enet_tx_queue(struct net_device *ndev, u16 queue_id, int budget) 1374 { 1375 struct fec_enet_private *fep; 1376 struct xdp_frame *xdpf; 1377 struct bufdesc *bdp; 1378 unsigned short status; 1379 struct sk_buff *skb; 1380 struct fec_enet_priv_tx_q *txq; 1381 struct netdev_queue *nq; 1382 int index = 0; 1383 int entries_free; 1384 1385 fep = netdev_priv(ndev); 1386 1387 txq = fep->tx_queue[queue_id]; 1388 /* get next bdp of dirty_tx */ 1389 nq = netdev_get_tx_queue(ndev, queue_id); 1390 bdp = txq->dirty_tx; 1391 1392 /* get next bdp of dirty_tx */ 1393 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 1394 1395 while (bdp != READ_ONCE(txq->bd.cur)) { 1396 /* Order the load of bd.cur and cbd_sc */ 1397 rmb(); 1398 status = fec16_to_cpu(READ_ONCE(bdp->cbd_sc)); 1399 if (status & BD_ENET_TX_READY) 1400 break; 1401 1402 index = fec_enet_get_bd_index(bdp, &txq->bd); 1403 1404 if (txq->tx_buf[index].type == FEC_TXBUF_T_SKB) { 1405 skb = txq->tx_buf[index].skb; 1406 txq->tx_buf[index].skb = NULL; 1407 if (bdp->cbd_bufaddr && 1408 !IS_TSO_HEADER(txq, fec32_to_cpu(bdp->cbd_bufaddr))) 1409 dma_unmap_single(&fep->pdev->dev, 1410 fec32_to_cpu(bdp->cbd_bufaddr), 1411 fec16_to_cpu(bdp->cbd_datlen), 1412 DMA_TO_DEVICE); 1413 bdp->cbd_bufaddr = cpu_to_fec32(0); 1414 if (!skb) 1415 goto tx_buf_done; 1416 } else { 1417 /* Tx processing cannot call any XDP (or page pool) APIs if 1418 * the "budget" is 0. Because NAPI is called with budget of 1419 * 0 (such as netpoll) indicates we may be in an IRQ context, 1420 * however, we can't use the page pool from IRQ context. 1421 */ 1422 if (unlikely(!budget)) 1423 break; 1424 1425 xdpf = txq->tx_buf[index].xdp; 1426 if (bdp->cbd_bufaddr) 1427 dma_unmap_single(&fep->pdev->dev, 1428 fec32_to_cpu(bdp->cbd_bufaddr), 1429 fec16_to_cpu(bdp->cbd_datlen), 1430 DMA_TO_DEVICE); 1431 bdp->cbd_bufaddr = cpu_to_fec32(0); 1432 if (!xdpf) { 1433 txq->tx_buf[index].type = FEC_TXBUF_T_SKB; 1434 goto tx_buf_done; 1435 } 1436 } 1437 1438 /* Check for errors. */ 1439 if (status & (BD_ENET_TX_HB | BD_ENET_TX_LC | 1440 BD_ENET_TX_RL | BD_ENET_TX_UN | 1441 BD_ENET_TX_CSL)) { 1442 ndev->stats.tx_errors++; 1443 if (status & BD_ENET_TX_HB) /* No heartbeat */ 1444 ndev->stats.tx_heartbeat_errors++; 1445 if (status & BD_ENET_TX_LC) /* Late collision */ 1446 ndev->stats.tx_window_errors++; 1447 if (status & BD_ENET_TX_RL) /* Retrans limit */ 1448 ndev->stats.tx_aborted_errors++; 1449 if (status & BD_ENET_TX_UN) /* Underrun */ 1450 ndev->stats.tx_fifo_errors++; 1451 if (status & BD_ENET_TX_CSL) /* Carrier lost */ 1452 ndev->stats.tx_carrier_errors++; 1453 } else { 1454 ndev->stats.tx_packets++; 1455 1456 if (txq->tx_buf[index].type == FEC_TXBUF_T_SKB) 1457 ndev->stats.tx_bytes += skb->len; 1458 else 1459 ndev->stats.tx_bytes += xdpf->len; 1460 } 1461 1462 /* Deferred means some collisions occurred during transmit, 1463 * but we eventually sent the packet OK. 1464 */ 1465 if (status & BD_ENET_TX_DEF) 1466 ndev->stats.collisions++; 1467 1468 if (txq->tx_buf[index].type == FEC_TXBUF_T_SKB) { 1469 /* NOTE: SKBTX_IN_PROGRESS being set does not imply it's we who 1470 * are to time stamp the packet, so we still need to check time 1471 * stamping enabled flag. 1472 */ 1473 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS && 1474 fep->hwts_tx_en) && fep->bufdesc_ex) { 1475 struct skb_shared_hwtstamps shhwtstamps; 1476 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1477 1478 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), &shhwtstamps); 1479 skb_tstamp_tx(skb, &shhwtstamps); 1480 } 1481 1482 /* Free the sk buffer associated with this last transmit */ 1483 dev_kfree_skb_any(skb); 1484 } else { 1485 xdp_return_frame(xdpf); 1486 1487 txq->tx_buf[index].xdp = NULL; 1488 /* restore default tx buffer type: FEC_TXBUF_T_SKB */ 1489 txq->tx_buf[index].type = FEC_TXBUF_T_SKB; 1490 } 1491 1492 tx_buf_done: 1493 /* Make sure the update to bdp and tx_buf are performed 1494 * before dirty_tx 1495 */ 1496 wmb(); 1497 txq->dirty_tx = bdp; 1498 1499 /* Update pointer to next buffer descriptor to be transmitted */ 1500 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 1501 1502 /* Since we have freed up a buffer, the ring is no longer full 1503 */ 1504 if (netif_tx_queue_stopped(nq)) { 1505 entries_free = fec_enet_get_free_txdesc_num(txq); 1506 if (entries_free >= txq->tx_wake_threshold) 1507 netif_tx_wake_queue(nq); 1508 } 1509 } 1510 1511 /* ERR006358: Keep the transmitter going */ 1512 if (bdp != txq->bd.cur && 1513 readl(txq->bd.reg_desc_active) == 0) 1514 writel(0, txq->bd.reg_desc_active); 1515 } 1516 1517 static void fec_enet_tx(struct net_device *ndev, int budget) 1518 { 1519 struct fec_enet_private *fep = netdev_priv(ndev); 1520 int i; 1521 1522 /* Make sure that AVB queues are processed first. */ 1523 for (i = fep->num_tx_queues - 1; i >= 0; i--) 1524 fec_enet_tx_queue(ndev, i, budget); 1525 } 1526 1527 static void fec_enet_update_cbd(struct fec_enet_priv_rx_q *rxq, 1528 struct bufdesc *bdp, int index) 1529 { 1530 struct page *new_page; 1531 dma_addr_t phys_addr; 1532 1533 new_page = page_pool_dev_alloc_pages(rxq->page_pool); 1534 WARN_ON(!new_page); 1535 rxq->rx_skb_info[index].page = new_page; 1536 1537 rxq->rx_skb_info[index].offset = FEC_ENET_XDP_HEADROOM; 1538 phys_addr = page_pool_get_dma_addr(new_page) + FEC_ENET_XDP_HEADROOM; 1539 bdp->cbd_bufaddr = cpu_to_fec32(phys_addr); 1540 } 1541 1542 static u32 1543 fec_enet_run_xdp(struct fec_enet_private *fep, struct bpf_prog *prog, 1544 struct xdp_buff *xdp, struct fec_enet_priv_rx_q *rxq, int index) 1545 { 1546 unsigned int sync, len = xdp->data_end - xdp->data; 1547 u32 ret = FEC_ENET_XDP_PASS; 1548 struct page *page; 1549 int err; 1550 u32 act; 1551 1552 act = bpf_prog_run_xdp(prog, xdp); 1553 1554 /* Due xdp_adjust_tail: DMA sync for_device cover max len CPU touch */ 1555 sync = xdp->data_end - xdp->data_hard_start - FEC_ENET_XDP_HEADROOM; 1556 sync = max(sync, len); 1557 1558 switch (act) { 1559 case XDP_PASS: 1560 rxq->stats[RX_XDP_PASS]++; 1561 ret = FEC_ENET_XDP_PASS; 1562 break; 1563 1564 case XDP_REDIRECT: 1565 rxq->stats[RX_XDP_REDIRECT]++; 1566 err = xdp_do_redirect(fep->netdev, xdp, prog); 1567 if (!err) { 1568 ret = FEC_ENET_XDP_REDIR; 1569 } else { 1570 ret = FEC_ENET_XDP_CONSUMED; 1571 page = virt_to_head_page(xdp->data); 1572 page_pool_put_page(rxq->page_pool, page, sync, true); 1573 } 1574 break; 1575 1576 default: 1577 bpf_warn_invalid_xdp_action(fep->netdev, prog, act); 1578 fallthrough; 1579 1580 case XDP_TX: 1581 bpf_warn_invalid_xdp_action(fep->netdev, prog, act); 1582 fallthrough; 1583 1584 case XDP_ABORTED: 1585 fallthrough; /* handle aborts by dropping packet */ 1586 1587 case XDP_DROP: 1588 rxq->stats[RX_XDP_DROP]++; 1589 ret = FEC_ENET_XDP_CONSUMED; 1590 page = virt_to_head_page(xdp->data); 1591 page_pool_put_page(rxq->page_pool, page, sync, true); 1592 break; 1593 } 1594 1595 return ret; 1596 } 1597 1598 /* During a receive, the bd_rx.cur points to the current incoming buffer. 1599 * When we update through the ring, if the next incoming buffer has 1600 * not been given to the system, we just set the empty indicator, 1601 * effectively tossing the packet. 1602 */ 1603 static int 1604 fec_enet_rx_queue(struct net_device *ndev, int budget, u16 queue_id) 1605 { 1606 struct fec_enet_private *fep = netdev_priv(ndev); 1607 struct fec_enet_priv_rx_q *rxq; 1608 struct bufdesc *bdp; 1609 unsigned short status; 1610 struct sk_buff *skb; 1611 ushort pkt_len; 1612 __u8 *data; 1613 int pkt_received = 0; 1614 struct bufdesc_ex *ebdp = NULL; 1615 bool vlan_packet_rcvd = false; 1616 u16 vlan_tag; 1617 int index = 0; 1618 bool need_swap = fep->quirks & FEC_QUIRK_SWAP_FRAME; 1619 struct bpf_prog *xdp_prog = READ_ONCE(fep->xdp_prog); 1620 u32 ret, xdp_result = FEC_ENET_XDP_PASS; 1621 u32 data_start = FEC_ENET_XDP_HEADROOM; 1622 struct xdp_buff xdp; 1623 struct page *page; 1624 u32 sub_len = 4; 1625 1626 #if !defined(CONFIG_M5272) 1627 /*If it has the FEC_QUIRK_HAS_RACC quirk property, the bit of 1628 * FEC_RACC_SHIFT16 is set by default in the probe function. 1629 */ 1630 if (fep->quirks & FEC_QUIRK_HAS_RACC) { 1631 data_start += 2; 1632 sub_len += 2; 1633 } 1634 #endif 1635 1636 #ifdef CONFIG_M532x 1637 flush_cache_all(); 1638 #endif 1639 rxq = fep->rx_queue[queue_id]; 1640 1641 /* First, grab all of the stats for the incoming packet. 1642 * These get messed up if we get called due to a busy condition. 1643 */ 1644 bdp = rxq->bd.cur; 1645 xdp_init_buff(&xdp, PAGE_SIZE, &rxq->xdp_rxq); 1646 1647 while (!((status = fec16_to_cpu(bdp->cbd_sc)) & BD_ENET_RX_EMPTY)) { 1648 1649 if (pkt_received >= budget) 1650 break; 1651 pkt_received++; 1652 1653 writel(FEC_ENET_RXF_GET(queue_id), fep->hwp + FEC_IEVENT); 1654 1655 /* Check for errors. */ 1656 status ^= BD_ENET_RX_LAST; 1657 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH | BD_ENET_RX_NO | 1658 BD_ENET_RX_CR | BD_ENET_RX_OV | BD_ENET_RX_LAST | 1659 BD_ENET_RX_CL)) { 1660 ndev->stats.rx_errors++; 1661 if (status & BD_ENET_RX_OV) { 1662 /* FIFO overrun */ 1663 ndev->stats.rx_fifo_errors++; 1664 goto rx_processing_done; 1665 } 1666 if (status & (BD_ENET_RX_LG | BD_ENET_RX_SH 1667 | BD_ENET_RX_LAST)) { 1668 /* Frame too long or too short. */ 1669 ndev->stats.rx_length_errors++; 1670 if (status & BD_ENET_RX_LAST) 1671 netdev_err(ndev, "rcv is not +last\n"); 1672 } 1673 if (status & BD_ENET_RX_CR) /* CRC Error */ 1674 ndev->stats.rx_crc_errors++; 1675 /* Report late collisions as a frame error. */ 1676 if (status & (BD_ENET_RX_NO | BD_ENET_RX_CL)) 1677 ndev->stats.rx_frame_errors++; 1678 goto rx_processing_done; 1679 } 1680 1681 /* Process the incoming frame. */ 1682 ndev->stats.rx_packets++; 1683 pkt_len = fec16_to_cpu(bdp->cbd_datlen); 1684 ndev->stats.rx_bytes += pkt_len; 1685 1686 index = fec_enet_get_bd_index(bdp, &rxq->bd); 1687 page = rxq->rx_skb_info[index].page; 1688 dma_sync_single_for_cpu(&fep->pdev->dev, 1689 fec32_to_cpu(bdp->cbd_bufaddr), 1690 pkt_len, 1691 DMA_FROM_DEVICE); 1692 prefetch(page_address(page)); 1693 fec_enet_update_cbd(rxq, bdp, index); 1694 1695 if (xdp_prog) { 1696 xdp_buff_clear_frags_flag(&xdp); 1697 /* subtract 16bit shift and FCS */ 1698 xdp_prepare_buff(&xdp, page_address(page), 1699 data_start, pkt_len - sub_len, false); 1700 ret = fec_enet_run_xdp(fep, xdp_prog, &xdp, rxq, index); 1701 xdp_result |= ret; 1702 if (ret != FEC_ENET_XDP_PASS) 1703 goto rx_processing_done; 1704 } 1705 1706 /* The packet length includes FCS, but we don't want to 1707 * include that when passing upstream as it messes up 1708 * bridging applications. 1709 */ 1710 skb = build_skb(page_address(page), PAGE_SIZE); 1711 if (unlikely(!skb)) { 1712 page_pool_recycle_direct(rxq->page_pool, page); 1713 ndev->stats.rx_dropped++; 1714 1715 netdev_err_once(ndev, "build_skb failed!\n"); 1716 goto rx_processing_done; 1717 } 1718 1719 skb_reserve(skb, data_start); 1720 skb_put(skb, pkt_len - sub_len); 1721 skb_mark_for_recycle(skb); 1722 1723 if (unlikely(need_swap)) { 1724 data = page_address(page) + FEC_ENET_XDP_HEADROOM; 1725 swap_buffer(data, pkt_len); 1726 } 1727 data = skb->data; 1728 1729 /* Extract the enhanced buffer descriptor */ 1730 ebdp = NULL; 1731 if (fep->bufdesc_ex) 1732 ebdp = (struct bufdesc_ex *)bdp; 1733 1734 /* If this is a VLAN packet remove the VLAN Tag */ 1735 vlan_packet_rcvd = false; 1736 if ((ndev->features & NETIF_F_HW_VLAN_CTAG_RX) && 1737 fep->bufdesc_ex && 1738 (ebdp->cbd_esc & cpu_to_fec32(BD_ENET_RX_VLAN))) { 1739 /* Push and remove the vlan tag */ 1740 struct vlan_hdr *vlan_header = 1741 (struct vlan_hdr *) (data + ETH_HLEN); 1742 vlan_tag = ntohs(vlan_header->h_vlan_TCI); 1743 1744 vlan_packet_rcvd = true; 1745 1746 memmove(skb->data + VLAN_HLEN, data, ETH_ALEN * 2); 1747 skb_pull(skb, VLAN_HLEN); 1748 } 1749 1750 skb->protocol = eth_type_trans(skb, ndev); 1751 1752 /* Get receive timestamp from the skb */ 1753 if (fep->hwts_rx_en && fep->bufdesc_ex) 1754 fec_enet_hwtstamp(fep, fec32_to_cpu(ebdp->ts), 1755 skb_hwtstamps(skb)); 1756 1757 if (fep->bufdesc_ex && 1758 (fep->csum_flags & FLAG_RX_CSUM_ENABLED)) { 1759 if (!(ebdp->cbd_esc & cpu_to_fec32(FLAG_RX_CSUM_ERROR))) { 1760 /* don't check it */ 1761 skb->ip_summed = CHECKSUM_UNNECESSARY; 1762 } else { 1763 skb_checksum_none_assert(skb); 1764 } 1765 } 1766 1767 /* Handle received VLAN packets */ 1768 if (vlan_packet_rcvd) 1769 __vlan_hwaccel_put_tag(skb, 1770 htons(ETH_P_8021Q), 1771 vlan_tag); 1772 1773 skb_record_rx_queue(skb, queue_id); 1774 napi_gro_receive(&fep->napi, skb); 1775 1776 rx_processing_done: 1777 /* Clear the status flags for this buffer */ 1778 status &= ~BD_ENET_RX_STATS; 1779 1780 /* Mark the buffer empty */ 1781 status |= BD_ENET_RX_EMPTY; 1782 1783 if (fep->bufdesc_ex) { 1784 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 1785 1786 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT); 1787 ebdp->cbd_prot = 0; 1788 ebdp->cbd_bdu = 0; 1789 } 1790 /* Make sure the updates to rest of the descriptor are 1791 * performed before transferring ownership. 1792 */ 1793 wmb(); 1794 bdp->cbd_sc = cpu_to_fec16(status); 1795 1796 /* Update BD pointer to next entry */ 1797 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 1798 1799 /* Doing this here will keep the FEC running while we process 1800 * incoming frames. On a heavily loaded network, we should be 1801 * able to keep up at the expense of system resources. 1802 */ 1803 writel(0, rxq->bd.reg_desc_active); 1804 } 1805 rxq->bd.cur = bdp; 1806 1807 if (xdp_result & FEC_ENET_XDP_REDIR) 1808 xdp_do_flush_map(); 1809 1810 return pkt_received; 1811 } 1812 1813 static int fec_enet_rx(struct net_device *ndev, int budget) 1814 { 1815 struct fec_enet_private *fep = netdev_priv(ndev); 1816 int i, done = 0; 1817 1818 /* Make sure that AVB queues are processed first. */ 1819 for (i = fep->num_rx_queues - 1; i >= 0; i--) 1820 done += fec_enet_rx_queue(ndev, budget - done, i); 1821 1822 return done; 1823 } 1824 1825 static bool fec_enet_collect_events(struct fec_enet_private *fep) 1826 { 1827 uint int_events; 1828 1829 int_events = readl(fep->hwp + FEC_IEVENT); 1830 1831 /* Don't clear MDIO events, we poll for those */ 1832 int_events &= ~FEC_ENET_MII; 1833 1834 writel(int_events, fep->hwp + FEC_IEVENT); 1835 1836 return int_events != 0; 1837 } 1838 1839 static irqreturn_t 1840 fec_enet_interrupt(int irq, void *dev_id) 1841 { 1842 struct net_device *ndev = dev_id; 1843 struct fec_enet_private *fep = netdev_priv(ndev); 1844 irqreturn_t ret = IRQ_NONE; 1845 1846 if (fec_enet_collect_events(fep) && fep->link) { 1847 ret = IRQ_HANDLED; 1848 1849 if (napi_schedule_prep(&fep->napi)) { 1850 /* Disable interrupts */ 1851 writel(0, fep->hwp + FEC_IMASK); 1852 __napi_schedule(&fep->napi); 1853 } 1854 } 1855 1856 return ret; 1857 } 1858 1859 static int fec_enet_rx_napi(struct napi_struct *napi, int budget) 1860 { 1861 struct net_device *ndev = napi->dev; 1862 struct fec_enet_private *fep = netdev_priv(ndev); 1863 int done = 0; 1864 1865 do { 1866 done += fec_enet_rx(ndev, budget - done); 1867 fec_enet_tx(ndev, budget); 1868 } while ((done < budget) && fec_enet_collect_events(fep)); 1869 1870 if (done < budget) { 1871 napi_complete_done(napi, done); 1872 writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK); 1873 } 1874 1875 return done; 1876 } 1877 1878 /* ------------------------------------------------------------------------- */ 1879 static int fec_get_mac(struct net_device *ndev) 1880 { 1881 struct fec_enet_private *fep = netdev_priv(ndev); 1882 unsigned char *iap, tmpaddr[ETH_ALEN]; 1883 int ret; 1884 1885 /* 1886 * try to get mac address in following order: 1887 * 1888 * 1) module parameter via kernel command line in form 1889 * fec.macaddr=0x00,0x04,0x9f,0x01,0x30,0xe0 1890 */ 1891 iap = macaddr; 1892 1893 /* 1894 * 2) from device tree data 1895 */ 1896 if (!is_valid_ether_addr(iap)) { 1897 struct device_node *np = fep->pdev->dev.of_node; 1898 if (np) { 1899 ret = of_get_mac_address(np, tmpaddr); 1900 if (!ret) 1901 iap = tmpaddr; 1902 else if (ret == -EPROBE_DEFER) 1903 return ret; 1904 } 1905 } 1906 1907 /* 1908 * 3) from flash or fuse (via platform data) 1909 */ 1910 if (!is_valid_ether_addr(iap)) { 1911 #ifdef CONFIG_M5272 1912 if (FEC_FLASHMAC) 1913 iap = (unsigned char *)FEC_FLASHMAC; 1914 #else 1915 struct fec_platform_data *pdata = dev_get_platdata(&fep->pdev->dev); 1916 1917 if (pdata) 1918 iap = (unsigned char *)&pdata->mac; 1919 #endif 1920 } 1921 1922 /* 1923 * 4) FEC mac registers set by bootloader 1924 */ 1925 if (!is_valid_ether_addr(iap)) { 1926 *((__be32 *) &tmpaddr[0]) = 1927 cpu_to_be32(readl(fep->hwp + FEC_ADDR_LOW)); 1928 *((__be16 *) &tmpaddr[4]) = 1929 cpu_to_be16(readl(fep->hwp + FEC_ADDR_HIGH) >> 16); 1930 iap = &tmpaddr[0]; 1931 } 1932 1933 /* 1934 * 5) random mac address 1935 */ 1936 if (!is_valid_ether_addr(iap)) { 1937 /* Report it and use a random ethernet address instead */ 1938 dev_err(&fep->pdev->dev, "Invalid MAC address: %pM\n", iap); 1939 eth_hw_addr_random(ndev); 1940 dev_info(&fep->pdev->dev, "Using random MAC address: %pM\n", 1941 ndev->dev_addr); 1942 return 0; 1943 } 1944 1945 /* Adjust MAC if using macaddr */ 1946 eth_hw_addr_gen(ndev, iap, iap == macaddr ? fep->dev_id : 0); 1947 1948 return 0; 1949 } 1950 1951 /* ------------------------------------------------------------------------- */ 1952 1953 /* 1954 * Phy section 1955 */ 1956 static void fec_enet_adjust_link(struct net_device *ndev) 1957 { 1958 struct fec_enet_private *fep = netdev_priv(ndev); 1959 struct phy_device *phy_dev = ndev->phydev; 1960 int status_change = 0; 1961 1962 /* 1963 * If the netdev is down, or is going down, we're not interested 1964 * in link state events, so just mark our idea of the link as down 1965 * and ignore the event. 1966 */ 1967 if (!netif_running(ndev) || !netif_device_present(ndev)) { 1968 fep->link = 0; 1969 } else if (phy_dev->link) { 1970 if (!fep->link) { 1971 fep->link = phy_dev->link; 1972 status_change = 1; 1973 } 1974 1975 if (fep->full_duplex != phy_dev->duplex) { 1976 fep->full_duplex = phy_dev->duplex; 1977 status_change = 1; 1978 } 1979 1980 if (phy_dev->speed != fep->speed) { 1981 fep->speed = phy_dev->speed; 1982 status_change = 1; 1983 } 1984 1985 /* if any of the above changed restart the FEC */ 1986 if (status_change) { 1987 napi_disable(&fep->napi); 1988 netif_tx_lock_bh(ndev); 1989 fec_restart(ndev); 1990 netif_tx_wake_all_queues(ndev); 1991 netif_tx_unlock_bh(ndev); 1992 napi_enable(&fep->napi); 1993 } 1994 } else { 1995 if (fep->link) { 1996 napi_disable(&fep->napi); 1997 netif_tx_lock_bh(ndev); 1998 fec_stop(ndev); 1999 netif_tx_unlock_bh(ndev); 2000 napi_enable(&fep->napi); 2001 fep->link = phy_dev->link; 2002 status_change = 1; 2003 } 2004 } 2005 2006 if (status_change) 2007 phy_print_status(phy_dev); 2008 } 2009 2010 static int fec_enet_mdio_wait(struct fec_enet_private *fep) 2011 { 2012 uint ievent; 2013 int ret; 2014 2015 ret = readl_poll_timeout_atomic(fep->hwp + FEC_IEVENT, ievent, 2016 ievent & FEC_ENET_MII, 2, 30000); 2017 2018 if (!ret) 2019 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT); 2020 2021 return ret; 2022 } 2023 2024 static int fec_enet_mdio_read_c22(struct mii_bus *bus, int mii_id, int regnum) 2025 { 2026 struct fec_enet_private *fep = bus->priv; 2027 struct device *dev = &fep->pdev->dev; 2028 int ret = 0, frame_start, frame_addr, frame_op; 2029 2030 ret = pm_runtime_resume_and_get(dev); 2031 if (ret < 0) 2032 return ret; 2033 2034 /* C22 read */ 2035 frame_op = FEC_MMFR_OP_READ; 2036 frame_start = FEC_MMFR_ST; 2037 frame_addr = regnum; 2038 2039 /* start a read op */ 2040 writel(frame_start | frame_op | 2041 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 2042 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA); 2043 2044 /* wait for end of transfer */ 2045 ret = fec_enet_mdio_wait(fep); 2046 if (ret) { 2047 netdev_err(fep->netdev, "MDIO read timeout\n"); 2048 goto out; 2049 } 2050 2051 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA)); 2052 2053 out: 2054 pm_runtime_mark_last_busy(dev); 2055 pm_runtime_put_autosuspend(dev); 2056 2057 return ret; 2058 } 2059 2060 static int fec_enet_mdio_read_c45(struct mii_bus *bus, int mii_id, 2061 int devad, int regnum) 2062 { 2063 struct fec_enet_private *fep = bus->priv; 2064 struct device *dev = &fep->pdev->dev; 2065 int ret = 0, frame_start, frame_op; 2066 2067 ret = pm_runtime_resume_and_get(dev); 2068 if (ret < 0) 2069 return ret; 2070 2071 frame_start = FEC_MMFR_ST_C45; 2072 2073 /* write address */ 2074 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE | 2075 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) | 2076 FEC_MMFR_TA | (regnum & 0xFFFF), 2077 fep->hwp + FEC_MII_DATA); 2078 2079 /* wait for end of transfer */ 2080 ret = fec_enet_mdio_wait(fep); 2081 if (ret) { 2082 netdev_err(fep->netdev, "MDIO address write timeout\n"); 2083 goto out; 2084 } 2085 2086 frame_op = FEC_MMFR_OP_READ_C45; 2087 2088 /* start a read op */ 2089 writel(frame_start | frame_op | 2090 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) | 2091 FEC_MMFR_TA, fep->hwp + FEC_MII_DATA); 2092 2093 /* wait for end of transfer */ 2094 ret = fec_enet_mdio_wait(fep); 2095 if (ret) { 2096 netdev_err(fep->netdev, "MDIO read timeout\n"); 2097 goto out; 2098 } 2099 2100 ret = FEC_MMFR_DATA(readl(fep->hwp + FEC_MII_DATA)); 2101 2102 out: 2103 pm_runtime_mark_last_busy(dev); 2104 pm_runtime_put_autosuspend(dev); 2105 2106 return ret; 2107 } 2108 2109 static int fec_enet_mdio_write_c22(struct mii_bus *bus, int mii_id, int regnum, 2110 u16 value) 2111 { 2112 struct fec_enet_private *fep = bus->priv; 2113 struct device *dev = &fep->pdev->dev; 2114 int ret, frame_start, frame_addr; 2115 2116 ret = pm_runtime_resume_and_get(dev); 2117 if (ret < 0) 2118 return ret; 2119 2120 /* C22 write */ 2121 frame_start = FEC_MMFR_ST; 2122 frame_addr = regnum; 2123 2124 /* start a write op */ 2125 writel(frame_start | FEC_MMFR_OP_WRITE | 2126 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(frame_addr) | 2127 FEC_MMFR_TA | FEC_MMFR_DATA(value), 2128 fep->hwp + FEC_MII_DATA); 2129 2130 /* wait for end of transfer */ 2131 ret = fec_enet_mdio_wait(fep); 2132 if (ret) 2133 netdev_err(fep->netdev, "MDIO write timeout\n"); 2134 2135 pm_runtime_mark_last_busy(dev); 2136 pm_runtime_put_autosuspend(dev); 2137 2138 return ret; 2139 } 2140 2141 static int fec_enet_mdio_write_c45(struct mii_bus *bus, int mii_id, 2142 int devad, int regnum, u16 value) 2143 { 2144 struct fec_enet_private *fep = bus->priv; 2145 struct device *dev = &fep->pdev->dev; 2146 int ret, frame_start; 2147 2148 ret = pm_runtime_resume_and_get(dev); 2149 if (ret < 0) 2150 return ret; 2151 2152 frame_start = FEC_MMFR_ST_C45; 2153 2154 /* write address */ 2155 writel(frame_start | FEC_MMFR_OP_ADDR_WRITE | 2156 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) | 2157 FEC_MMFR_TA | (regnum & 0xFFFF), 2158 fep->hwp + FEC_MII_DATA); 2159 2160 /* wait for end of transfer */ 2161 ret = fec_enet_mdio_wait(fep); 2162 if (ret) { 2163 netdev_err(fep->netdev, "MDIO address write timeout\n"); 2164 goto out; 2165 } 2166 2167 /* start a write op */ 2168 writel(frame_start | FEC_MMFR_OP_WRITE | 2169 FEC_MMFR_PA(mii_id) | FEC_MMFR_RA(devad) | 2170 FEC_MMFR_TA | FEC_MMFR_DATA(value), 2171 fep->hwp + FEC_MII_DATA); 2172 2173 /* wait for end of transfer */ 2174 ret = fec_enet_mdio_wait(fep); 2175 if (ret) 2176 netdev_err(fep->netdev, "MDIO write timeout\n"); 2177 2178 out: 2179 pm_runtime_mark_last_busy(dev); 2180 pm_runtime_put_autosuspend(dev); 2181 2182 return ret; 2183 } 2184 2185 static void fec_enet_phy_reset_after_clk_enable(struct net_device *ndev) 2186 { 2187 struct fec_enet_private *fep = netdev_priv(ndev); 2188 struct phy_device *phy_dev = ndev->phydev; 2189 2190 if (phy_dev) { 2191 phy_reset_after_clk_enable(phy_dev); 2192 } else if (fep->phy_node) { 2193 /* 2194 * If the PHY still is not bound to the MAC, but there is 2195 * OF PHY node and a matching PHY device instance already, 2196 * use the OF PHY node to obtain the PHY device instance, 2197 * and then use that PHY device instance when triggering 2198 * the PHY reset. 2199 */ 2200 phy_dev = of_phy_find_device(fep->phy_node); 2201 phy_reset_after_clk_enable(phy_dev); 2202 put_device(&phy_dev->mdio.dev); 2203 } 2204 } 2205 2206 static int fec_enet_clk_enable(struct net_device *ndev, bool enable) 2207 { 2208 struct fec_enet_private *fep = netdev_priv(ndev); 2209 int ret; 2210 2211 if (enable) { 2212 ret = clk_prepare_enable(fep->clk_enet_out); 2213 if (ret) 2214 return ret; 2215 2216 if (fep->clk_ptp) { 2217 mutex_lock(&fep->ptp_clk_mutex); 2218 ret = clk_prepare_enable(fep->clk_ptp); 2219 if (ret) { 2220 mutex_unlock(&fep->ptp_clk_mutex); 2221 goto failed_clk_ptp; 2222 } else { 2223 fep->ptp_clk_on = true; 2224 } 2225 mutex_unlock(&fep->ptp_clk_mutex); 2226 } 2227 2228 ret = clk_prepare_enable(fep->clk_ref); 2229 if (ret) 2230 goto failed_clk_ref; 2231 2232 ret = clk_prepare_enable(fep->clk_2x_txclk); 2233 if (ret) 2234 goto failed_clk_2x_txclk; 2235 2236 fec_enet_phy_reset_after_clk_enable(ndev); 2237 } else { 2238 clk_disable_unprepare(fep->clk_enet_out); 2239 if (fep->clk_ptp) { 2240 mutex_lock(&fep->ptp_clk_mutex); 2241 clk_disable_unprepare(fep->clk_ptp); 2242 fep->ptp_clk_on = false; 2243 mutex_unlock(&fep->ptp_clk_mutex); 2244 } 2245 clk_disable_unprepare(fep->clk_ref); 2246 clk_disable_unprepare(fep->clk_2x_txclk); 2247 } 2248 2249 return 0; 2250 2251 failed_clk_2x_txclk: 2252 if (fep->clk_ref) 2253 clk_disable_unprepare(fep->clk_ref); 2254 failed_clk_ref: 2255 if (fep->clk_ptp) { 2256 mutex_lock(&fep->ptp_clk_mutex); 2257 clk_disable_unprepare(fep->clk_ptp); 2258 fep->ptp_clk_on = false; 2259 mutex_unlock(&fep->ptp_clk_mutex); 2260 } 2261 failed_clk_ptp: 2262 clk_disable_unprepare(fep->clk_enet_out); 2263 2264 return ret; 2265 } 2266 2267 static int fec_enet_parse_rgmii_delay(struct fec_enet_private *fep, 2268 struct device_node *np) 2269 { 2270 u32 rgmii_tx_delay, rgmii_rx_delay; 2271 2272 /* For rgmii tx internal delay, valid values are 0ps and 2000ps */ 2273 if (!of_property_read_u32(np, "tx-internal-delay-ps", &rgmii_tx_delay)) { 2274 if (rgmii_tx_delay != 0 && rgmii_tx_delay != 2000) { 2275 dev_err(&fep->pdev->dev, "The only allowed RGMII TX delay values are: 0ps, 2000ps"); 2276 return -EINVAL; 2277 } else if (rgmii_tx_delay == 2000) { 2278 fep->rgmii_txc_dly = true; 2279 } 2280 } 2281 2282 /* For rgmii rx internal delay, valid values are 0ps and 2000ps */ 2283 if (!of_property_read_u32(np, "rx-internal-delay-ps", &rgmii_rx_delay)) { 2284 if (rgmii_rx_delay != 0 && rgmii_rx_delay != 2000) { 2285 dev_err(&fep->pdev->dev, "The only allowed RGMII RX delay values are: 0ps, 2000ps"); 2286 return -EINVAL; 2287 } else if (rgmii_rx_delay == 2000) { 2288 fep->rgmii_rxc_dly = true; 2289 } 2290 } 2291 2292 return 0; 2293 } 2294 2295 static int fec_enet_mii_probe(struct net_device *ndev) 2296 { 2297 struct fec_enet_private *fep = netdev_priv(ndev); 2298 struct phy_device *phy_dev = NULL; 2299 char mdio_bus_id[MII_BUS_ID_SIZE]; 2300 char phy_name[MII_BUS_ID_SIZE + 3]; 2301 int phy_id; 2302 int dev_id = fep->dev_id; 2303 2304 if (fep->phy_node) { 2305 phy_dev = of_phy_connect(ndev, fep->phy_node, 2306 &fec_enet_adjust_link, 0, 2307 fep->phy_interface); 2308 if (!phy_dev) { 2309 netdev_err(ndev, "Unable to connect to phy\n"); 2310 return -ENODEV; 2311 } 2312 } else { 2313 /* check for attached phy */ 2314 for (phy_id = 0; (phy_id < PHY_MAX_ADDR); phy_id++) { 2315 if (!mdiobus_is_registered_device(fep->mii_bus, phy_id)) 2316 continue; 2317 if (dev_id--) 2318 continue; 2319 strscpy(mdio_bus_id, fep->mii_bus->id, MII_BUS_ID_SIZE); 2320 break; 2321 } 2322 2323 if (phy_id >= PHY_MAX_ADDR) { 2324 netdev_info(ndev, "no PHY, assuming direct connection to switch\n"); 2325 strscpy(mdio_bus_id, "fixed-0", MII_BUS_ID_SIZE); 2326 phy_id = 0; 2327 } 2328 2329 snprintf(phy_name, sizeof(phy_name), 2330 PHY_ID_FMT, mdio_bus_id, phy_id); 2331 phy_dev = phy_connect(ndev, phy_name, &fec_enet_adjust_link, 2332 fep->phy_interface); 2333 } 2334 2335 if (IS_ERR(phy_dev)) { 2336 netdev_err(ndev, "could not attach to PHY\n"); 2337 return PTR_ERR(phy_dev); 2338 } 2339 2340 /* mask with MAC supported features */ 2341 if (fep->quirks & FEC_QUIRK_HAS_GBIT) { 2342 phy_set_max_speed(phy_dev, 1000); 2343 phy_remove_link_mode(phy_dev, 2344 ETHTOOL_LINK_MODE_1000baseT_Half_BIT); 2345 #if !defined(CONFIG_M5272) 2346 phy_support_sym_pause(phy_dev); 2347 #endif 2348 } 2349 else 2350 phy_set_max_speed(phy_dev, 100); 2351 2352 fep->link = 0; 2353 fep->full_duplex = 0; 2354 2355 phy_dev->mac_managed_pm = true; 2356 2357 phy_attached_info(phy_dev); 2358 2359 return 0; 2360 } 2361 2362 static int fec_enet_mii_init(struct platform_device *pdev) 2363 { 2364 static struct mii_bus *fec0_mii_bus; 2365 struct net_device *ndev = platform_get_drvdata(pdev); 2366 struct fec_enet_private *fep = netdev_priv(ndev); 2367 bool suppress_preamble = false; 2368 struct device_node *node; 2369 int err = -ENXIO; 2370 u32 mii_speed, holdtime; 2371 u32 bus_freq; 2372 2373 /* 2374 * The i.MX28 dual fec interfaces are not equal. 2375 * Here are the differences: 2376 * 2377 * - fec0 supports MII & RMII modes while fec1 only supports RMII 2378 * - fec0 acts as the 1588 time master while fec1 is slave 2379 * - external phys can only be configured by fec0 2380 * 2381 * That is to say fec1 can not work independently. It only works 2382 * when fec0 is working. The reason behind this design is that the 2383 * second interface is added primarily for Switch mode. 2384 * 2385 * Because of the last point above, both phys are attached on fec0 2386 * mdio interface in board design, and need to be configured by 2387 * fec0 mii_bus. 2388 */ 2389 if ((fep->quirks & FEC_QUIRK_SINGLE_MDIO) && fep->dev_id > 0) { 2390 /* fec1 uses fec0 mii_bus */ 2391 if (mii_cnt && fec0_mii_bus) { 2392 fep->mii_bus = fec0_mii_bus; 2393 mii_cnt++; 2394 return 0; 2395 } 2396 return -ENOENT; 2397 } 2398 2399 bus_freq = 2500000; /* 2.5MHz by default */ 2400 node = of_get_child_by_name(pdev->dev.of_node, "mdio"); 2401 if (node) { 2402 of_property_read_u32(node, "clock-frequency", &bus_freq); 2403 suppress_preamble = of_property_read_bool(node, 2404 "suppress-preamble"); 2405 } 2406 2407 /* 2408 * Set MII speed (= clk_get_rate() / 2 * phy_speed) 2409 * 2410 * The formula for FEC MDC is 'ref_freq / (MII_SPEED x 2)' while 2411 * for ENET-MAC is 'ref_freq / ((MII_SPEED + 1) x 2)'. The i.MX28 2412 * Reference Manual has an error on this, and gets fixed on i.MX6Q 2413 * document. 2414 */ 2415 mii_speed = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), bus_freq * 2); 2416 if (fep->quirks & FEC_QUIRK_ENET_MAC) 2417 mii_speed--; 2418 if (mii_speed > 63) { 2419 dev_err(&pdev->dev, 2420 "fec clock (%lu) too fast to get right mii speed\n", 2421 clk_get_rate(fep->clk_ipg)); 2422 err = -EINVAL; 2423 goto err_out; 2424 } 2425 2426 /* 2427 * The i.MX28 and i.MX6 types have another filed in the MSCR (aka 2428 * MII_SPEED) register that defines the MDIO output hold time. Earlier 2429 * versions are RAZ there, so just ignore the difference and write the 2430 * register always. 2431 * The minimal hold time according to IEE802.3 (clause 22) is 10 ns. 2432 * HOLDTIME + 1 is the number of clk cycles the fec is holding the 2433 * output. 2434 * The HOLDTIME bitfield takes values between 0 and 7 (inclusive). 2435 * Given that ceil(clkrate / 5000000) <= 64, the calculation for 2436 * holdtime cannot result in a value greater than 3. 2437 */ 2438 holdtime = DIV_ROUND_UP(clk_get_rate(fep->clk_ipg), 100000000) - 1; 2439 2440 fep->phy_speed = mii_speed << 1 | holdtime << 8; 2441 2442 if (suppress_preamble) 2443 fep->phy_speed |= BIT(7); 2444 2445 if (fep->quirks & FEC_QUIRK_CLEAR_SETUP_MII) { 2446 /* Clear MMFR to avoid to generate MII event by writing MSCR. 2447 * MII event generation condition: 2448 * - writing MSCR: 2449 * - mmfr[31:0]_not_zero & mscr[7:0]_is_zero & 2450 * mscr_reg_data_in[7:0] != 0 2451 * - writing MMFR: 2452 * - mscr[7:0]_not_zero 2453 */ 2454 writel(0, fep->hwp + FEC_MII_DATA); 2455 } 2456 2457 writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED); 2458 2459 /* Clear any pending transaction complete indication */ 2460 writel(FEC_ENET_MII, fep->hwp + FEC_IEVENT); 2461 2462 fep->mii_bus = mdiobus_alloc(); 2463 if (fep->mii_bus == NULL) { 2464 err = -ENOMEM; 2465 goto err_out; 2466 } 2467 2468 fep->mii_bus->name = "fec_enet_mii_bus"; 2469 fep->mii_bus->read = fec_enet_mdio_read_c22; 2470 fep->mii_bus->write = fec_enet_mdio_write_c22; 2471 if (fep->quirks & FEC_QUIRK_HAS_MDIO_C45) { 2472 fep->mii_bus->read_c45 = fec_enet_mdio_read_c45; 2473 fep->mii_bus->write_c45 = fec_enet_mdio_write_c45; 2474 } 2475 snprintf(fep->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x", 2476 pdev->name, fep->dev_id + 1); 2477 fep->mii_bus->priv = fep; 2478 fep->mii_bus->parent = &pdev->dev; 2479 2480 err = of_mdiobus_register(fep->mii_bus, node); 2481 if (err) 2482 goto err_out_free_mdiobus; 2483 of_node_put(node); 2484 2485 mii_cnt++; 2486 2487 /* save fec0 mii_bus */ 2488 if (fep->quirks & FEC_QUIRK_SINGLE_MDIO) 2489 fec0_mii_bus = fep->mii_bus; 2490 2491 return 0; 2492 2493 err_out_free_mdiobus: 2494 mdiobus_free(fep->mii_bus); 2495 err_out: 2496 of_node_put(node); 2497 return err; 2498 } 2499 2500 static void fec_enet_mii_remove(struct fec_enet_private *fep) 2501 { 2502 if (--mii_cnt == 0) { 2503 mdiobus_unregister(fep->mii_bus); 2504 mdiobus_free(fep->mii_bus); 2505 } 2506 } 2507 2508 static void fec_enet_get_drvinfo(struct net_device *ndev, 2509 struct ethtool_drvinfo *info) 2510 { 2511 struct fec_enet_private *fep = netdev_priv(ndev); 2512 2513 strscpy(info->driver, fep->pdev->dev.driver->name, 2514 sizeof(info->driver)); 2515 strscpy(info->bus_info, dev_name(&ndev->dev), sizeof(info->bus_info)); 2516 } 2517 2518 static int fec_enet_get_regs_len(struct net_device *ndev) 2519 { 2520 struct fec_enet_private *fep = netdev_priv(ndev); 2521 struct resource *r; 2522 int s = 0; 2523 2524 r = platform_get_resource(fep->pdev, IORESOURCE_MEM, 0); 2525 if (r) 2526 s = resource_size(r); 2527 2528 return s; 2529 } 2530 2531 /* List of registers that can be safety be read to dump them with ethtool */ 2532 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 2533 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 2534 defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST) 2535 static __u32 fec_enet_register_version = 2; 2536 static u32 fec_enet_register_offset[] = { 2537 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0, 2538 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL, 2539 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_TXIC1, 2540 FEC_TXIC2, FEC_RXIC0, FEC_RXIC1, FEC_RXIC2, FEC_HASH_TABLE_HIGH, 2541 FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, 2542 FEC_X_WMRK, FEC_R_BOUND, FEC_R_FSTART, FEC_R_DES_START_1, 2543 FEC_X_DES_START_1, FEC_R_BUFF_SIZE_1, FEC_R_DES_START_2, 2544 FEC_X_DES_START_2, FEC_R_BUFF_SIZE_2, FEC_R_DES_START_0, 2545 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM, 2546 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, FEC_RCMR_1, FEC_RCMR_2, 2547 FEC_DMA_CFG_1, FEC_DMA_CFG_2, FEC_R_DES_ACTIVE_1, FEC_X_DES_ACTIVE_1, 2548 FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_2, FEC_QOS_SCHEME, 2549 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT, 2550 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG, 2551 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255, 2552 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047, 2553 RMON_T_P_GTE2048, RMON_T_OCTETS, 2554 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF, 2555 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE, 2556 IEEE_T_FDXFC, IEEE_T_OCTETS_OK, 2557 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN, 2558 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB, 2559 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255, 2560 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047, 2561 RMON_R_P_GTE2048, RMON_R_OCTETS, 2562 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR, 2563 IEEE_R_FDXFC, IEEE_R_OCTETS_OK 2564 }; 2565 /* for i.MX6ul */ 2566 static u32 fec_enet_register_offset_6ul[] = { 2567 FEC_IEVENT, FEC_IMASK, FEC_R_DES_ACTIVE_0, FEC_X_DES_ACTIVE_0, 2568 FEC_ECNTRL, FEC_MII_DATA, FEC_MII_SPEED, FEC_MIB_CTRLSTAT, FEC_R_CNTRL, 2569 FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, FEC_OPD, FEC_TXIC0, FEC_RXIC0, 2570 FEC_HASH_TABLE_HIGH, FEC_HASH_TABLE_LOW, FEC_GRP_HASH_TABLE_HIGH, 2571 FEC_GRP_HASH_TABLE_LOW, FEC_X_WMRK, FEC_R_DES_START_0, 2572 FEC_X_DES_START_0, FEC_R_BUFF_SIZE_0, FEC_R_FIFO_RSFL, FEC_R_FIFO_RSEM, 2573 FEC_R_FIFO_RAEM, FEC_R_FIFO_RAFL, FEC_RACC, 2574 RMON_T_DROP, RMON_T_PACKETS, RMON_T_BC_PKT, RMON_T_MC_PKT, 2575 RMON_T_CRC_ALIGN, RMON_T_UNDERSIZE, RMON_T_OVERSIZE, RMON_T_FRAG, 2576 RMON_T_JAB, RMON_T_COL, RMON_T_P64, RMON_T_P65TO127, RMON_T_P128TO255, 2577 RMON_T_P256TO511, RMON_T_P512TO1023, RMON_T_P1024TO2047, 2578 RMON_T_P_GTE2048, RMON_T_OCTETS, 2579 IEEE_T_DROP, IEEE_T_FRAME_OK, IEEE_T_1COL, IEEE_T_MCOL, IEEE_T_DEF, 2580 IEEE_T_LCOL, IEEE_T_EXCOL, IEEE_T_MACERR, IEEE_T_CSERR, IEEE_T_SQE, 2581 IEEE_T_FDXFC, IEEE_T_OCTETS_OK, 2582 RMON_R_PACKETS, RMON_R_BC_PKT, RMON_R_MC_PKT, RMON_R_CRC_ALIGN, 2583 RMON_R_UNDERSIZE, RMON_R_OVERSIZE, RMON_R_FRAG, RMON_R_JAB, 2584 RMON_R_RESVD_O, RMON_R_P64, RMON_R_P65TO127, RMON_R_P128TO255, 2585 RMON_R_P256TO511, RMON_R_P512TO1023, RMON_R_P1024TO2047, 2586 RMON_R_P_GTE2048, RMON_R_OCTETS, 2587 IEEE_R_DROP, IEEE_R_FRAME_OK, IEEE_R_CRC, IEEE_R_ALIGN, IEEE_R_MACERR, 2588 IEEE_R_FDXFC, IEEE_R_OCTETS_OK 2589 }; 2590 #else 2591 static __u32 fec_enet_register_version = 1; 2592 static u32 fec_enet_register_offset[] = { 2593 FEC_ECNTRL, FEC_IEVENT, FEC_IMASK, FEC_IVEC, FEC_R_DES_ACTIVE_0, 2594 FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2, FEC_X_DES_ACTIVE_0, 2595 FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2, FEC_MII_DATA, FEC_MII_SPEED, 2596 FEC_R_BOUND, FEC_R_FSTART, FEC_X_WMRK, FEC_X_FSTART, FEC_R_CNTRL, 2597 FEC_MAX_FRM_LEN, FEC_X_CNTRL, FEC_ADDR_LOW, FEC_ADDR_HIGH, 2598 FEC_GRP_HASH_TABLE_HIGH, FEC_GRP_HASH_TABLE_LOW, FEC_R_DES_START_0, 2599 FEC_R_DES_START_1, FEC_R_DES_START_2, FEC_X_DES_START_0, 2600 FEC_X_DES_START_1, FEC_X_DES_START_2, FEC_R_BUFF_SIZE_0, 2601 FEC_R_BUFF_SIZE_1, FEC_R_BUFF_SIZE_2 2602 }; 2603 #endif 2604 2605 static void fec_enet_get_regs(struct net_device *ndev, 2606 struct ethtool_regs *regs, void *regbuf) 2607 { 2608 struct fec_enet_private *fep = netdev_priv(ndev); 2609 u32 __iomem *theregs = (u32 __iomem *)fep->hwp; 2610 struct device *dev = &fep->pdev->dev; 2611 u32 *buf = (u32 *)regbuf; 2612 u32 i, off; 2613 int ret; 2614 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \ 2615 defined(CONFIG_M520x) || defined(CONFIG_M532x) || defined(CONFIG_ARM) || \ 2616 defined(CONFIG_ARM64) || defined(CONFIG_COMPILE_TEST) 2617 u32 *reg_list; 2618 u32 reg_cnt; 2619 2620 if (!of_machine_is_compatible("fsl,imx6ul")) { 2621 reg_list = fec_enet_register_offset; 2622 reg_cnt = ARRAY_SIZE(fec_enet_register_offset); 2623 } else { 2624 reg_list = fec_enet_register_offset_6ul; 2625 reg_cnt = ARRAY_SIZE(fec_enet_register_offset_6ul); 2626 } 2627 #else 2628 /* coldfire */ 2629 static u32 *reg_list = fec_enet_register_offset; 2630 static const u32 reg_cnt = ARRAY_SIZE(fec_enet_register_offset); 2631 #endif 2632 ret = pm_runtime_resume_and_get(dev); 2633 if (ret < 0) 2634 return; 2635 2636 regs->version = fec_enet_register_version; 2637 2638 memset(buf, 0, regs->len); 2639 2640 for (i = 0; i < reg_cnt; i++) { 2641 off = reg_list[i]; 2642 2643 if ((off == FEC_R_BOUND || off == FEC_R_FSTART) && 2644 !(fep->quirks & FEC_QUIRK_HAS_FRREG)) 2645 continue; 2646 2647 off >>= 2; 2648 buf[off] = readl(&theregs[off]); 2649 } 2650 2651 pm_runtime_mark_last_busy(dev); 2652 pm_runtime_put_autosuspend(dev); 2653 } 2654 2655 static int fec_enet_get_ts_info(struct net_device *ndev, 2656 struct ethtool_ts_info *info) 2657 { 2658 struct fec_enet_private *fep = netdev_priv(ndev); 2659 2660 if (fep->bufdesc_ex) { 2661 2662 info->so_timestamping = SOF_TIMESTAMPING_TX_SOFTWARE | 2663 SOF_TIMESTAMPING_RX_SOFTWARE | 2664 SOF_TIMESTAMPING_SOFTWARE | 2665 SOF_TIMESTAMPING_TX_HARDWARE | 2666 SOF_TIMESTAMPING_RX_HARDWARE | 2667 SOF_TIMESTAMPING_RAW_HARDWARE; 2668 if (fep->ptp_clock) 2669 info->phc_index = ptp_clock_index(fep->ptp_clock); 2670 else 2671 info->phc_index = -1; 2672 2673 info->tx_types = (1 << HWTSTAMP_TX_OFF) | 2674 (1 << HWTSTAMP_TX_ON); 2675 2676 info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 2677 (1 << HWTSTAMP_FILTER_ALL); 2678 return 0; 2679 } else { 2680 return ethtool_op_get_ts_info(ndev, info); 2681 } 2682 } 2683 2684 #if !defined(CONFIG_M5272) 2685 2686 static void fec_enet_get_pauseparam(struct net_device *ndev, 2687 struct ethtool_pauseparam *pause) 2688 { 2689 struct fec_enet_private *fep = netdev_priv(ndev); 2690 2691 pause->autoneg = (fep->pause_flag & FEC_PAUSE_FLAG_AUTONEG) != 0; 2692 pause->tx_pause = (fep->pause_flag & FEC_PAUSE_FLAG_ENABLE) != 0; 2693 pause->rx_pause = pause->tx_pause; 2694 } 2695 2696 static int fec_enet_set_pauseparam(struct net_device *ndev, 2697 struct ethtool_pauseparam *pause) 2698 { 2699 struct fec_enet_private *fep = netdev_priv(ndev); 2700 2701 if (!ndev->phydev) 2702 return -ENODEV; 2703 2704 if (pause->tx_pause != pause->rx_pause) { 2705 netdev_info(ndev, 2706 "hardware only support enable/disable both tx and rx"); 2707 return -EINVAL; 2708 } 2709 2710 fep->pause_flag = 0; 2711 2712 /* tx pause must be same as rx pause */ 2713 fep->pause_flag |= pause->rx_pause ? FEC_PAUSE_FLAG_ENABLE : 0; 2714 fep->pause_flag |= pause->autoneg ? FEC_PAUSE_FLAG_AUTONEG : 0; 2715 2716 phy_set_sym_pause(ndev->phydev, pause->rx_pause, pause->tx_pause, 2717 pause->autoneg); 2718 2719 if (pause->autoneg) { 2720 if (netif_running(ndev)) 2721 fec_stop(ndev); 2722 phy_start_aneg(ndev->phydev); 2723 } 2724 if (netif_running(ndev)) { 2725 napi_disable(&fep->napi); 2726 netif_tx_lock_bh(ndev); 2727 fec_restart(ndev); 2728 netif_tx_wake_all_queues(ndev); 2729 netif_tx_unlock_bh(ndev); 2730 napi_enable(&fep->napi); 2731 } 2732 2733 return 0; 2734 } 2735 2736 static const struct fec_stat { 2737 char name[ETH_GSTRING_LEN]; 2738 u16 offset; 2739 } fec_stats[] = { 2740 /* RMON TX */ 2741 { "tx_dropped", RMON_T_DROP }, 2742 { "tx_packets", RMON_T_PACKETS }, 2743 { "tx_broadcast", RMON_T_BC_PKT }, 2744 { "tx_multicast", RMON_T_MC_PKT }, 2745 { "tx_crc_errors", RMON_T_CRC_ALIGN }, 2746 { "tx_undersize", RMON_T_UNDERSIZE }, 2747 { "tx_oversize", RMON_T_OVERSIZE }, 2748 { "tx_fragment", RMON_T_FRAG }, 2749 { "tx_jabber", RMON_T_JAB }, 2750 { "tx_collision", RMON_T_COL }, 2751 { "tx_64byte", RMON_T_P64 }, 2752 { "tx_65to127byte", RMON_T_P65TO127 }, 2753 { "tx_128to255byte", RMON_T_P128TO255 }, 2754 { "tx_256to511byte", RMON_T_P256TO511 }, 2755 { "tx_512to1023byte", RMON_T_P512TO1023 }, 2756 { "tx_1024to2047byte", RMON_T_P1024TO2047 }, 2757 { "tx_GTE2048byte", RMON_T_P_GTE2048 }, 2758 { "tx_octets", RMON_T_OCTETS }, 2759 2760 /* IEEE TX */ 2761 { "IEEE_tx_drop", IEEE_T_DROP }, 2762 { "IEEE_tx_frame_ok", IEEE_T_FRAME_OK }, 2763 { "IEEE_tx_1col", IEEE_T_1COL }, 2764 { "IEEE_tx_mcol", IEEE_T_MCOL }, 2765 { "IEEE_tx_def", IEEE_T_DEF }, 2766 { "IEEE_tx_lcol", IEEE_T_LCOL }, 2767 { "IEEE_tx_excol", IEEE_T_EXCOL }, 2768 { "IEEE_tx_macerr", IEEE_T_MACERR }, 2769 { "IEEE_tx_cserr", IEEE_T_CSERR }, 2770 { "IEEE_tx_sqe", IEEE_T_SQE }, 2771 { "IEEE_tx_fdxfc", IEEE_T_FDXFC }, 2772 { "IEEE_tx_octets_ok", IEEE_T_OCTETS_OK }, 2773 2774 /* RMON RX */ 2775 { "rx_packets", RMON_R_PACKETS }, 2776 { "rx_broadcast", RMON_R_BC_PKT }, 2777 { "rx_multicast", RMON_R_MC_PKT }, 2778 { "rx_crc_errors", RMON_R_CRC_ALIGN }, 2779 { "rx_undersize", RMON_R_UNDERSIZE }, 2780 { "rx_oversize", RMON_R_OVERSIZE }, 2781 { "rx_fragment", RMON_R_FRAG }, 2782 { "rx_jabber", RMON_R_JAB }, 2783 { "rx_64byte", RMON_R_P64 }, 2784 { "rx_65to127byte", RMON_R_P65TO127 }, 2785 { "rx_128to255byte", RMON_R_P128TO255 }, 2786 { "rx_256to511byte", RMON_R_P256TO511 }, 2787 { "rx_512to1023byte", RMON_R_P512TO1023 }, 2788 { "rx_1024to2047byte", RMON_R_P1024TO2047 }, 2789 { "rx_GTE2048byte", RMON_R_P_GTE2048 }, 2790 { "rx_octets", RMON_R_OCTETS }, 2791 2792 /* IEEE RX */ 2793 { "IEEE_rx_drop", IEEE_R_DROP }, 2794 { "IEEE_rx_frame_ok", IEEE_R_FRAME_OK }, 2795 { "IEEE_rx_crc", IEEE_R_CRC }, 2796 { "IEEE_rx_align", IEEE_R_ALIGN }, 2797 { "IEEE_rx_macerr", IEEE_R_MACERR }, 2798 { "IEEE_rx_fdxfc", IEEE_R_FDXFC }, 2799 { "IEEE_rx_octets_ok", IEEE_R_OCTETS_OK }, 2800 }; 2801 2802 #define FEC_STATS_SIZE (ARRAY_SIZE(fec_stats) * sizeof(u64)) 2803 2804 static const char *fec_xdp_stat_strs[XDP_STATS_TOTAL] = { 2805 "rx_xdp_redirect", /* RX_XDP_REDIRECT = 0, */ 2806 "rx_xdp_pass", /* RX_XDP_PASS, */ 2807 "rx_xdp_drop", /* RX_XDP_DROP, */ 2808 "rx_xdp_tx", /* RX_XDP_TX, */ 2809 "rx_xdp_tx_errors", /* RX_XDP_TX_ERRORS, */ 2810 "tx_xdp_xmit", /* TX_XDP_XMIT, */ 2811 "tx_xdp_xmit_errors", /* TX_XDP_XMIT_ERRORS, */ 2812 }; 2813 2814 static void fec_enet_update_ethtool_stats(struct net_device *dev) 2815 { 2816 struct fec_enet_private *fep = netdev_priv(dev); 2817 int i; 2818 2819 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 2820 fep->ethtool_stats[i] = readl(fep->hwp + fec_stats[i].offset); 2821 } 2822 2823 static void fec_enet_get_xdp_stats(struct fec_enet_private *fep, u64 *data) 2824 { 2825 u64 xdp_stats[XDP_STATS_TOTAL] = { 0 }; 2826 struct fec_enet_priv_rx_q *rxq; 2827 int i, j; 2828 2829 for (i = fep->num_rx_queues - 1; i >= 0; i--) { 2830 rxq = fep->rx_queue[i]; 2831 2832 for (j = 0; j < XDP_STATS_TOTAL; j++) 2833 xdp_stats[j] += rxq->stats[j]; 2834 } 2835 2836 memcpy(data, xdp_stats, sizeof(xdp_stats)); 2837 } 2838 2839 static void fec_enet_page_pool_stats(struct fec_enet_private *fep, u64 *data) 2840 { 2841 #ifdef CONFIG_PAGE_POOL_STATS 2842 struct page_pool_stats stats = {}; 2843 struct fec_enet_priv_rx_q *rxq; 2844 int i; 2845 2846 for (i = fep->num_rx_queues - 1; i >= 0; i--) { 2847 rxq = fep->rx_queue[i]; 2848 2849 if (!rxq->page_pool) 2850 continue; 2851 2852 page_pool_get_stats(rxq->page_pool, &stats); 2853 } 2854 2855 page_pool_ethtool_stats_get(data, &stats); 2856 #endif 2857 } 2858 2859 static void fec_enet_get_ethtool_stats(struct net_device *dev, 2860 struct ethtool_stats *stats, u64 *data) 2861 { 2862 struct fec_enet_private *fep = netdev_priv(dev); 2863 2864 if (netif_running(dev)) 2865 fec_enet_update_ethtool_stats(dev); 2866 2867 memcpy(data, fep->ethtool_stats, FEC_STATS_SIZE); 2868 data += FEC_STATS_SIZE / sizeof(u64); 2869 2870 fec_enet_get_xdp_stats(fep, data); 2871 data += XDP_STATS_TOTAL; 2872 2873 fec_enet_page_pool_stats(fep, data); 2874 } 2875 2876 static void fec_enet_get_strings(struct net_device *netdev, 2877 u32 stringset, u8 *data) 2878 { 2879 int i; 2880 switch (stringset) { 2881 case ETH_SS_STATS: 2882 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) { 2883 memcpy(data, fec_stats[i].name, ETH_GSTRING_LEN); 2884 data += ETH_GSTRING_LEN; 2885 } 2886 for (i = 0; i < ARRAY_SIZE(fec_xdp_stat_strs); i++) { 2887 strncpy(data, fec_xdp_stat_strs[i], ETH_GSTRING_LEN); 2888 data += ETH_GSTRING_LEN; 2889 } 2890 page_pool_ethtool_stats_get_strings(data); 2891 2892 break; 2893 case ETH_SS_TEST: 2894 net_selftest_get_strings(data); 2895 break; 2896 } 2897 } 2898 2899 static int fec_enet_get_sset_count(struct net_device *dev, int sset) 2900 { 2901 int count; 2902 2903 switch (sset) { 2904 case ETH_SS_STATS: 2905 count = ARRAY_SIZE(fec_stats) + XDP_STATS_TOTAL; 2906 count += page_pool_ethtool_stats_get_count(); 2907 return count; 2908 2909 case ETH_SS_TEST: 2910 return net_selftest_get_count(); 2911 default: 2912 return -EOPNOTSUPP; 2913 } 2914 } 2915 2916 static void fec_enet_clear_ethtool_stats(struct net_device *dev) 2917 { 2918 struct fec_enet_private *fep = netdev_priv(dev); 2919 struct fec_enet_priv_rx_q *rxq; 2920 int i, j; 2921 2922 /* Disable MIB statistics counters */ 2923 writel(FEC_MIB_CTRLSTAT_DISABLE, fep->hwp + FEC_MIB_CTRLSTAT); 2924 2925 for (i = 0; i < ARRAY_SIZE(fec_stats); i++) 2926 writel(0, fep->hwp + fec_stats[i].offset); 2927 2928 for (i = fep->num_rx_queues - 1; i >= 0; i--) { 2929 rxq = fep->rx_queue[i]; 2930 for (j = 0; j < XDP_STATS_TOTAL; j++) 2931 rxq->stats[j] = 0; 2932 } 2933 2934 /* Don't disable MIB statistics counters */ 2935 writel(0, fep->hwp + FEC_MIB_CTRLSTAT); 2936 } 2937 2938 #else /* !defined(CONFIG_M5272) */ 2939 #define FEC_STATS_SIZE 0 2940 static inline void fec_enet_update_ethtool_stats(struct net_device *dev) 2941 { 2942 } 2943 2944 static inline void fec_enet_clear_ethtool_stats(struct net_device *dev) 2945 { 2946 } 2947 #endif /* !defined(CONFIG_M5272) */ 2948 2949 /* ITR clock source is enet system clock (clk_ahb). 2950 * TCTT unit is cycle_ns * 64 cycle 2951 * So, the ICTT value = X us / (cycle_ns * 64) 2952 */ 2953 static int fec_enet_us_to_itr_clock(struct net_device *ndev, int us) 2954 { 2955 struct fec_enet_private *fep = netdev_priv(ndev); 2956 2957 return us * (fep->itr_clk_rate / 64000) / 1000; 2958 } 2959 2960 /* Set threshold for interrupt coalescing */ 2961 static void fec_enet_itr_coal_set(struct net_device *ndev) 2962 { 2963 struct fec_enet_private *fep = netdev_priv(ndev); 2964 int rx_itr, tx_itr; 2965 2966 /* Must be greater than zero to avoid unpredictable behavior */ 2967 if (!fep->rx_time_itr || !fep->rx_pkts_itr || 2968 !fep->tx_time_itr || !fep->tx_pkts_itr) 2969 return; 2970 2971 /* Select enet system clock as Interrupt Coalescing 2972 * timer Clock Source 2973 */ 2974 rx_itr = FEC_ITR_CLK_SEL; 2975 tx_itr = FEC_ITR_CLK_SEL; 2976 2977 /* set ICFT and ICTT */ 2978 rx_itr |= FEC_ITR_ICFT(fep->rx_pkts_itr); 2979 rx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->rx_time_itr)); 2980 tx_itr |= FEC_ITR_ICFT(fep->tx_pkts_itr); 2981 tx_itr |= FEC_ITR_ICTT(fec_enet_us_to_itr_clock(ndev, fep->tx_time_itr)); 2982 2983 rx_itr |= FEC_ITR_EN; 2984 tx_itr |= FEC_ITR_EN; 2985 2986 writel(tx_itr, fep->hwp + FEC_TXIC0); 2987 writel(rx_itr, fep->hwp + FEC_RXIC0); 2988 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) { 2989 writel(tx_itr, fep->hwp + FEC_TXIC1); 2990 writel(rx_itr, fep->hwp + FEC_RXIC1); 2991 writel(tx_itr, fep->hwp + FEC_TXIC2); 2992 writel(rx_itr, fep->hwp + FEC_RXIC2); 2993 } 2994 } 2995 2996 static int fec_enet_get_coalesce(struct net_device *ndev, 2997 struct ethtool_coalesce *ec, 2998 struct kernel_ethtool_coalesce *kernel_coal, 2999 struct netlink_ext_ack *extack) 3000 { 3001 struct fec_enet_private *fep = netdev_priv(ndev); 3002 3003 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE)) 3004 return -EOPNOTSUPP; 3005 3006 ec->rx_coalesce_usecs = fep->rx_time_itr; 3007 ec->rx_max_coalesced_frames = fep->rx_pkts_itr; 3008 3009 ec->tx_coalesce_usecs = fep->tx_time_itr; 3010 ec->tx_max_coalesced_frames = fep->tx_pkts_itr; 3011 3012 return 0; 3013 } 3014 3015 static int fec_enet_set_coalesce(struct net_device *ndev, 3016 struct ethtool_coalesce *ec, 3017 struct kernel_ethtool_coalesce *kernel_coal, 3018 struct netlink_ext_ack *extack) 3019 { 3020 struct fec_enet_private *fep = netdev_priv(ndev); 3021 struct device *dev = &fep->pdev->dev; 3022 unsigned int cycle; 3023 3024 if (!(fep->quirks & FEC_QUIRK_HAS_COALESCE)) 3025 return -EOPNOTSUPP; 3026 3027 if (ec->rx_max_coalesced_frames > 255) { 3028 dev_err(dev, "Rx coalesced frames exceed hardware limitation\n"); 3029 return -EINVAL; 3030 } 3031 3032 if (ec->tx_max_coalesced_frames > 255) { 3033 dev_err(dev, "Tx coalesced frame exceed hardware limitation\n"); 3034 return -EINVAL; 3035 } 3036 3037 cycle = fec_enet_us_to_itr_clock(ndev, ec->rx_coalesce_usecs); 3038 if (cycle > 0xFFFF) { 3039 dev_err(dev, "Rx coalesced usec exceed hardware limitation\n"); 3040 return -EINVAL; 3041 } 3042 3043 cycle = fec_enet_us_to_itr_clock(ndev, ec->tx_coalesce_usecs); 3044 if (cycle > 0xFFFF) { 3045 dev_err(dev, "Tx coalesced usec exceed hardware limitation\n"); 3046 return -EINVAL; 3047 } 3048 3049 fep->rx_time_itr = ec->rx_coalesce_usecs; 3050 fep->rx_pkts_itr = ec->rx_max_coalesced_frames; 3051 3052 fep->tx_time_itr = ec->tx_coalesce_usecs; 3053 fep->tx_pkts_itr = ec->tx_max_coalesced_frames; 3054 3055 fec_enet_itr_coal_set(ndev); 3056 3057 return 0; 3058 } 3059 3060 /* LPI Sleep Ts count base on tx clk (clk_ref). 3061 * The lpi sleep cnt value = X us / (cycle_ns). 3062 */ 3063 static int fec_enet_us_to_tx_cycle(struct net_device *ndev, int us) 3064 { 3065 struct fec_enet_private *fep = netdev_priv(ndev); 3066 3067 return us * (fep->clk_ref_rate / 1000) / 1000; 3068 } 3069 3070 static int fec_enet_eee_mode_set(struct net_device *ndev, bool enable) 3071 { 3072 struct fec_enet_private *fep = netdev_priv(ndev); 3073 struct ethtool_eee *p = &fep->eee; 3074 unsigned int sleep_cycle, wake_cycle; 3075 int ret = 0; 3076 3077 if (enable) { 3078 ret = phy_init_eee(ndev->phydev, false); 3079 if (ret) 3080 return ret; 3081 3082 sleep_cycle = fec_enet_us_to_tx_cycle(ndev, p->tx_lpi_timer); 3083 wake_cycle = sleep_cycle; 3084 } else { 3085 sleep_cycle = 0; 3086 wake_cycle = 0; 3087 } 3088 3089 p->tx_lpi_enabled = enable; 3090 p->eee_enabled = enable; 3091 p->eee_active = enable; 3092 3093 writel(sleep_cycle, fep->hwp + FEC_LPI_SLEEP); 3094 writel(wake_cycle, fep->hwp + FEC_LPI_WAKE); 3095 3096 return 0; 3097 } 3098 3099 static int 3100 fec_enet_get_eee(struct net_device *ndev, struct ethtool_eee *edata) 3101 { 3102 struct fec_enet_private *fep = netdev_priv(ndev); 3103 struct ethtool_eee *p = &fep->eee; 3104 3105 if (!(fep->quirks & FEC_QUIRK_HAS_EEE)) 3106 return -EOPNOTSUPP; 3107 3108 if (!netif_running(ndev)) 3109 return -ENETDOWN; 3110 3111 edata->eee_enabled = p->eee_enabled; 3112 edata->eee_active = p->eee_active; 3113 edata->tx_lpi_timer = p->tx_lpi_timer; 3114 edata->tx_lpi_enabled = p->tx_lpi_enabled; 3115 3116 return phy_ethtool_get_eee(ndev->phydev, edata); 3117 } 3118 3119 static int 3120 fec_enet_set_eee(struct net_device *ndev, struct ethtool_eee *edata) 3121 { 3122 struct fec_enet_private *fep = netdev_priv(ndev); 3123 struct ethtool_eee *p = &fep->eee; 3124 int ret = 0; 3125 3126 if (!(fep->quirks & FEC_QUIRK_HAS_EEE)) 3127 return -EOPNOTSUPP; 3128 3129 if (!netif_running(ndev)) 3130 return -ENETDOWN; 3131 3132 p->tx_lpi_timer = edata->tx_lpi_timer; 3133 3134 if (!edata->eee_enabled || !edata->tx_lpi_enabled || 3135 !edata->tx_lpi_timer) 3136 ret = fec_enet_eee_mode_set(ndev, false); 3137 else 3138 ret = fec_enet_eee_mode_set(ndev, true); 3139 3140 if (ret) 3141 return ret; 3142 3143 return phy_ethtool_set_eee(ndev->phydev, edata); 3144 } 3145 3146 static void 3147 fec_enet_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 3148 { 3149 struct fec_enet_private *fep = netdev_priv(ndev); 3150 3151 if (fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET) { 3152 wol->supported = WAKE_MAGIC; 3153 wol->wolopts = fep->wol_flag & FEC_WOL_FLAG_ENABLE ? WAKE_MAGIC : 0; 3154 } else { 3155 wol->supported = wol->wolopts = 0; 3156 } 3157 } 3158 3159 static int 3160 fec_enet_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 3161 { 3162 struct fec_enet_private *fep = netdev_priv(ndev); 3163 3164 if (!(fep->wol_flag & FEC_WOL_HAS_MAGIC_PACKET)) 3165 return -EINVAL; 3166 3167 if (wol->wolopts & ~WAKE_MAGIC) 3168 return -EINVAL; 3169 3170 device_set_wakeup_enable(&ndev->dev, wol->wolopts & WAKE_MAGIC); 3171 if (device_may_wakeup(&ndev->dev)) 3172 fep->wol_flag |= FEC_WOL_FLAG_ENABLE; 3173 else 3174 fep->wol_flag &= (~FEC_WOL_FLAG_ENABLE); 3175 3176 return 0; 3177 } 3178 3179 static const struct ethtool_ops fec_enet_ethtool_ops = { 3180 .supported_coalesce_params = ETHTOOL_COALESCE_USECS | 3181 ETHTOOL_COALESCE_MAX_FRAMES, 3182 .get_drvinfo = fec_enet_get_drvinfo, 3183 .get_regs_len = fec_enet_get_regs_len, 3184 .get_regs = fec_enet_get_regs, 3185 .nway_reset = phy_ethtool_nway_reset, 3186 .get_link = ethtool_op_get_link, 3187 .get_coalesce = fec_enet_get_coalesce, 3188 .set_coalesce = fec_enet_set_coalesce, 3189 #ifndef CONFIG_M5272 3190 .get_pauseparam = fec_enet_get_pauseparam, 3191 .set_pauseparam = fec_enet_set_pauseparam, 3192 .get_strings = fec_enet_get_strings, 3193 .get_ethtool_stats = fec_enet_get_ethtool_stats, 3194 .get_sset_count = fec_enet_get_sset_count, 3195 #endif 3196 .get_ts_info = fec_enet_get_ts_info, 3197 .get_wol = fec_enet_get_wol, 3198 .set_wol = fec_enet_set_wol, 3199 .get_eee = fec_enet_get_eee, 3200 .set_eee = fec_enet_set_eee, 3201 .get_link_ksettings = phy_ethtool_get_link_ksettings, 3202 .set_link_ksettings = phy_ethtool_set_link_ksettings, 3203 .self_test = net_selftest, 3204 }; 3205 3206 static int fec_enet_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd) 3207 { 3208 struct fec_enet_private *fep = netdev_priv(ndev); 3209 struct phy_device *phydev = ndev->phydev; 3210 3211 if (!netif_running(ndev)) 3212 return -EINVAL; 3213 3214 if (!phydev) 3215 return -ENODEV; 3216 3217 if (fep->bufdesc_ex) { 3218 bool use_fec_hwts = !phy_has_hwtstamp(phydev); 3219 3220 if (cmd == SIOCSHWTSTAMP) { 3221 if (use_fec_hwts) 3222 return fec_ptp_set(ndev, rq); 3223 fec_ptp_disable_hwts(ndev); 3224 } else if (cmd == SIOCGHWTSTAMP) { 3225 if (use_fec_hwts) 3226 return fec_ptp_get(ndev, rq); 3227 } 3228 } 3229 3230 return phy_mii_ioctl(phydev, rq, cmd); 3231 } 3232 3233 static void fec_enet_free_buffers(struct net_device *ndev) 3234 { 3235 struct fec_enet_private *fep = netdev_priv(ndev); 3236 unsigned int i; 3237 struct sk_buff *skb; 3238 struct fec_enet_priv_tx_q *txq; 3239 struct fec_enet_priv_rx_q *rxq; 3240 unsigned int q; 3241 3242 for (q = 0; q < fep->num_rx_queues; q++) { 3243 rxq = fep->rx_queue[q]; 3244 for (i = 0; i < rxq->bd.ring_size; i++) 3245 page_pool_put_full_page(rxq->page_pool, rxq->rx_skb_info[i].page, false); 3246 3247 for (i = 0; i < XDP_STATS_TOTAL; i++) 3248 rxq->stats[i] = 0; 3249 3250 if (xdp_rxq_info_is_reg(&rxq->xdp_rxq)) 3251 xdp_rxq_info_unreg(&rxq->xdp_rxq); 3252 page_pool_destroy(rxq->page_pool); 3253 rxq->page_pool = NULL; 3254 } 3255 3256 for (q = 0; q < fep->num_tx_queues; q++) { 3257 txq = fep->tx_queue[q]; 3258 for (i = 0; i < txq->bd.ring_size; i++) { 3259 kfree(txq->tx_bounce[i]); 3260 txq->tx_bounce[i] = NULL; 3261 3262 if (txq->tx_buf[i].type == FEC_TXBUF_T_SKB) { 3263 skb = txq->tx_buf[i].skb; 3264 txq->tx_buf[i].skb = NULL; 3265 dev_kfree_skb(skb); 3266 } else { 3267 if (txq->tx_buf[i].xdp) { 3268 xdp_return_frame(txq->tx_buf[i].xdp); 3269 txq->tx_buf[i].xdp = NULL; 3270 } 3271 3272 txq->tx_buf[i].type = FEC_TXBUF_T_SKB; 3273 } 3274 } 3275 } 3276 } 3277 3278 static void fec_enet_free_queue(struct net_device *ndev) 3279 { 3280 struct fec_enet_private *fep = netdev_priv(ndev); 3281 int i; 3282 struct fec_enet_priv_tx_q *txq; 3283 3284 for (i = 0; i < fep->num_tx_queues; i++) 3285 if (fep->tx_queue[i] && fep->tx_queue[i]->tso_hdrs) { 3286 txq = fep->tx_queue[i]; 3287 dma_free_coherent(&fep->pdev->dev, 3288 txq->bd.ring_size * TSO_HEADER_SIZE, 3289 txq->tso_hdrs, 3290 txq->tso_hdrs_dma); 3291 } 3292 3293 for (i = 0; i < fep->num_rx_queues; i++) 3294 kfree(fep->rx_queue[i]); 3295 for (i = 0; i < fep->num_tx_queues; i++) 3296 kfree(fep->tx_queue[i]); 3297 } 3298 3299 static int fec_enet_alloc_queue(struct net_device *ndev) 3300 { 3301 struct fec_enet_private *fep = netdev_priv(ndev); 3302 int i; 3303 int ret = 0; 3304 struct fec_enet_priv_tx_q *txq; 3305 3306 for (i = 0; i < fep->num_tx_queues; i++) { 3307 txq = kzalloc(sizeof(*txq), GFP_KERNEL); 3308 if (!txq) { 3309 ret = -ENOMEM; 3310 goto alloc_failed; 3311 } 3312 3313 fep->tx_queue[i] = txq; 3314 txq->bd.ring_size = TX_RING_SIZE; 3315 fep->total_tx_ring_size += fep->tx_queue[i]->bd.ring_size; 3316 3317 txq->tx_stop_threshold = FEC_MAX_SKB_DESCS; 3318 txq->tx_wake_threshold = FEC_MAX_SKB_DESCS + 2 * MAX_SKB_FRAGS; 3319 3320 txq->tso_hdrs = dma_alloc_coherent(&fep->pdev->dev, 3321 txq->bd.ring_size * TSO_HEADER_SIZE, 3322 &txq->tso_hdrs_dma, 3323 GFP_KERNEL); 3324 if (!txq->tso_hdrs) { 3325 ret = -ENOMEM; 3326 goto alloc_failed; 3327 } 3328 } 3329 3330 for (i = 0; i < fep->num_rx_queues; i++) { 3331 fep->rx_queue[i] = kzalloc(sizeof(*fep->rx_queue[i]), 3332 GFP_KERNEL); 3333 if (!fep->rx_queue[i]) { 3334 ret = -ENOMEM; 3335 goto alloc_failed; 3336 } 3337 3338 fep->rx_queue[i]->bd.ring_size = RX_RING_SIZE; 3339 fep->total_rx_ring_size += fep->rx_queue[i]->bd.ring_size; 3340 } 3341 return ret; 3342 3343 alloc_failed: 3344 fec_enet_free_queue(ndev); 3345 return ret; 3346 } 3347 3348 static int 3349 fec_enet_alloc_rxq_buffers(struct net_device *ndev, unsigned int queue) 3350 { 3351 struct fec_enet_private *fep = netdev_priv(ndev); 3352 struct fec_enet_priv_rx_q *rxq; 3353 dma_addr_t phys_addr; 3354 struct bufdesc *bdp; 3355 struct page *page; 3356 int i, err; 3357 3358 rxq = fep->rx_queue[queue]; 3359 bdp = rxq->bd.base; 3360 3361 err = fec_enet_create_page_pool(fep, rxq, rxq->bd.ring_size); 3362 if (err < 0) { 3363 netdev_err(ndev, "%s failed queue %d (%d)\n", __func__, queue, err); 3364 return err; 3365 } 3366 3367 for (i = 0; i < rxq->bd.ring_size; i++) { 3368 page = page_pool_dev_alloc_pages(rxq->page_pool); 3369 if (!page) 3370 goto err_alloc; 3371 3372 phys_addr = page_pool_get_dma_addr(page) + FEC_ENET_XDP_HEADROOM; 3373 bdp->cbd_bufaddr = cpu_to_fec32(phys_addr); 3374 3375 rxq->rx_skb_info[i].page = page; 3376 rxq->rx_skb_info[i].offset = FEC_ENET_XDP_HEADROOM; 3377 bdp->cbd_sc = cpu_to_fec16(BD_ENET_RX_EMPTY); 3378 3379 if (fep->bufdesc_ex) { 3380 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 3381 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_RX_INT); 3382 } 3383 3384 bdp = fec_enet_get_nextdesc(bdp, &rxq->bd); 3385 } 3386 3387 /* Set the last buffer to wrap. */ 3388 bdp = fec_enet_get_prevdesc(bdp, &rxq->bd); 3389 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 3390 return 0; 3391 3392 err_alloc: 3393 fec_enet_free_buffers(ndev); 3394 return -ENOMEM; 3395 } 3396 3397 static int 3398 fec_enet_alloc_txq_buffers(struct net_device *ndev, unsigned int queue) 3399 { 3400 struct fec_enet_private *fep = netdev_priv(ndev); 3401 unsigned int i; 3402 struct bufdesc *bdp; 3403 struct fec_enet_priv_tx_q *txq; 3404 3405 txq = fep->tx_queue[queue]; 3406 bdp = txq->bd.base; 3407 for (i = 0; i < txq->bd.ring_size; i++) { 3408 txq->tx_bounce[i] = kmalloc(FEC_ENET_TX_FRSIZE, GFP_KERNEL); 3409 if (!txq->tx_bounce[i]) 3410 goto err_alloc; 3411 3412 bdp->cbd_sc = cpu_to_fec16(0); 3413 bdp->cbd_bufaddr = cpu_to_fec32(0); 3414 3415 if (fep->bufdesc_ex) { 3416 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 3417 ebdp->cbd_esc = cpu_to_fec32(BD_ENET_TX_INT); 3418 } 3419 3420 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 3421 } 3422 3423 /* Set the last buffer to wrap. */ 3424 bdp = fec_enet_get_prevdesc(bdp, &txq->bd); 3425 bdp->cbd_sc |= cpu_to_fec16(BD_SC_WRAP); 3426 3427 return 0; 3428 3429 err_alloc: 3430 fec_enet_free_buffers(ndev); 3431 return -ENOMEM; 3432 } 3433 3434 static int fec_enet_alloc_buffers(struct net_device *ndev) 3435 { 3436 struct fec_enet_private *fep = netdev_priv(ndev); 3437 unsigned int i; 3438 3439 for (i = 0; i < fep->num_rx_queues; i++) 3440 if (fec_enet_alloc_rxq_buffers(ndev, i)) 3441 return -ENOMEM; 3442 3443 for (i = 0; i < fep->num_tx_queues; i++) 3444 if (fec_enet_alloc_txq_buffers(ndev, i)) 3445 return -ENOMEM; 3446 return 0; 3447 } 3448 3449 static int 3450 fec_enet_open(struct net_device *ndev) 3451 { 3452 struct fec_enet_private *fep = netdev_priv(ndev); 3453 int ret; 3454 bool reset_again; 3455 3456 ret = pm_runtime_resume_and_get(&fep->pdev->dev); 3457 if (ret < 0) 3458 return ret; 3459 3460 pinctrl_pm_select_default_state(&fep->pdev->dev); 3461 ret = fec_enet_clk_enable(ndev, true); 3462 if (ret) 3463 goto clk_enable; 3464 3465 /* During the first fec_enet_open call the PHY isn't probed at this 3466 * point. Therefore the phy_reset_after_clk_enable() call within 3467 * fec_enet_clk_enable() fails. As we need this reset in order to be 3468 * sure the PHY is working correctly we check if we need to reset again 3469 * later when the PHY is probed 3470 */ 3471 if (ndev->phydev && ndev->phydev->drv) 3472 reset_again = false; 3473 else 3474 reset_again = true; 3475 3476 /* I should reset the ring buffers here, but I don't yet know 3477 * a simple way to do that. 3478 */ 3479 3480 ret = fec_enet_alloc_buffers(ndev); 3481 if (ret) 3482 goto err_enet_alloc; 3483 3484 /* Init MAC prior to mii bus probe */ 3485 fec_restart(ndev); 3486 3487 /* Call phy_reset_after_clk_enable() again if it failed during 3488 * phy_reset_after_clk_enable() before because the PHY wasn't probed. 3489 */ 3490 if (reset_again) 3491 fec_enet_phy_reset_after_clk_enable(ndev); 3492 3493 /* Probe and connect to PHY when open the interface */ 3494 ret = fec_enet_mii_probe(ndev); 3495 if (ret) 3496 goto err_enet_mii_probe; 3497 3498 if (fep->quirks & FEC_QUIRK_ERR006687) 3499 imx6q_cpuidle_fec_irqs_used(); 3500 3501 if (fep->quirks & FEC_QUIRK_HAS_PMQOS) 3502 cpu_latency_qos_add_request(&fep->pm_qos_req, 0); 3503 3504 napi_enable(&fep->napi); 3505 phy_start(ndev->phydev); 3506 netif_tx_start_all_queues(ndev); 3507 3508 device_set_wakeup_enable(&ndev->dev, fep->wol_flag & 3509 FEC_WOL_FLAG_ENABLE); 3510 3511 return 0; 3512 3513 err_enet_mii_probe: 3514 fec_enet_free_buffers(ndev); 3515 err_enet_alloc: 3516 fec_enet_clk_enable(ndev, false); 3517 clk_enable: 3518 pm_runtime_mark_last_busy(&fep->pdev->dev); 3519 pm_runtime_put_autosuspend(&fep->pdev->dev); 3520 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 3521 return ret; 3522 } 3523 3524 static int 3525 fec_enet_close(struct net_device *ndev) 3526 { 3527 struct fec_enet_private *fep = netdev_priv(ndev); 3528 3529 phy_stop(ndev->phydev); 3530 3531 if (netif_device_present(ndev)) { 3532 napi_disable(&fep->napi); 3533 netif_tx_disable(ndev); 3534 fec_stop(ndev); 3535 } 3536 3537 phy_disconnect(ndev->phydev); 3538 3539 if (fep->quirks & FEC_QUIRK_ERR006687) 3540 imx6q_cpuidle_fec_irqs_unused(); 3541 3542 fec_enet_update_ethtool_stats(ndev); 3543 3544 fec_enet_clk_enable(ndev, false); 3545 if (fep->quirks & FEC_QUIRK_HAS_PMQOS) 3546 cpu_latency_qos_remove_request(&fep->pm_qos_req); 3547 3548 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 3549 pm_runtime_mark_last_busy(&fep->pdev->dev); 3550 pm_runtime_put_autosuspend(&fep->pdev->dev); 3551 3552 fec_enet_free_buffers(ndev); 3553 3554 return 0; 3555 } 3556 3557 /* Set or clear the multicast filter for this adaptor. 3558 * Skeleton taken from sunlance driver. 3559 * The CPM Ethernet implementation allows Multicast as well as individual 3560 * MAC address filtering. Some of the drivers check to make sure it is 3561 * a group multicast address, and discard those that are not. I guess I 3562 * will do the same for now, but just remove the test if you want 3563 * individual filtering as well (do the upper net layers want or support 3564 * this kind of feature?). 3565 */ 3566 3567 #define FEC_HASH_BITS 6 /* #bits in hash */ 3568 3569 static void set_multicast_list(struct net_device *ndev) 3570 { 3571 struct fec_enet_private *fep = netdev_priv(ndev); 3572 struct netdev_hw_addr *ha; 3573 unsigned int crc, tmp; 3574 unsigned char hash; 3575 unsigned int hash_high = 0, hash_low = 0; 3576 3577 if (ndev->flags & IFF_PROMISC) { 3578 tmp = readl(fep->hwp + FEC_R_CNTRL); 3579 tmp |= 0x8; 3580 writel(tmp, fep->hwp + FEC_R_CNTRL); 3581 return; 3582 } 3583 3584 tmp = readl(fep->hwp + FEC_R_CNTRL); 3585 tmp &= ~0x8; 3586 writel(tmp, fep->hwp + FEC_R_CNTRL); 3587 3588 if (ndev->flags & IFF_ALLMULTI) { 3589 /* Catch all multicast addresses, so set the 3590 * filter to all 1's 3591 */ 3592 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 3593 writel(0xffffffff, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 3594 3595 return; 3596 } 3597 3598 /* Add the addresses in hash register */ 3599 netdev_for_each_mc_addr(ha, ndev) { 3600 /* calculate crc32 value of mac address */ 3601 crc = ether_crc_le(ndev->addr_len, ha->addr); 3602 3603 /* only upper 6 bits (FEC_HASH_BITS) are used 3604 * which point to specific bit in the hash registers 3605 */ 3606 hash = (crc >> (32 - FEC_HASH_BITS)) & 0x3f; 3607 3608 if (hash > 31) 3609 hash_high |= 1 << (hash - 32); 3610 else 3611 hash_low |= 1 << hash; 3612 } 3613 3614 writel(hash_high, fep->hwp + FEC_GRP_HASH_TABLE_HIGH); 3615 writel(hash_low, fep->hwp + FEC_GRP_HASH_TABLE_LOW); 3616 } 3617 3618 /* Set a MAC change in hardware. */ 3619 static int 3620 fec_set_mac_address(struct net_device *ndev, void *p) 3621 { 3622 struct fec_enet_private *fep = netdev_priv(ndev); 3623 struct sockaddr *addr = p; 3624 3625 if (addr) { 3626 if (!is_valid_ether_addr(addr->sa_data)) 3627 return -EADDRNOTAVAIL; 3628 eth_hw_addr_set(ndev, addr->sa_data); 3629 } 3630 3631 /* Add netif status check here to avoid system hang in below case: 3632 * ifconfig ethx down; ifconfig ethx hw ether xx:xx:xx:xx:xx:xx; 3633 * After ethx down, fec all clocks are gated off and then register 3634 * access causes system hang. 3635 */ 3636 if (!netif_running(ndev)) 3637 return 0; 3638 3639 writel(ndev->dev_addr[3] | (ndev->dev_addr[2] << 8) | 3640 (ndev->dev_addr[1] << 16) | (ndev->dev_addr[0] << 24), 3641 fep->hwp + FEC_ADDR_LOW); 3642 writel((ndev->dev_addr[5] << 16) | (ndev->dev_addr[4] << 24), 3643 fep->hwp + FEC_ADDR_HIGH); 3644 return 0; 3645 } 3646 3647 #ifdef CONFIG_NET_POLL_CONTROLLER 3648 /** 3649 * fec_poll_controller - FEC Poll controller function 3650 * @dev: The FEC network adapter 3651 * 3652 * Polled functionality used by netconsole and others in non interrupt mode 3653 * 3654 */ 3655 static void fec_poll_controller(struct net_device *dev) 3656 { 3657 int i; 3658 struct fec_enet_private *fep = netdev_priv(dev); 3659 3660 for (i = 0; i < FEC_IRQ_NUM; i++) { 3661 if (fep->irq[i] > 0) { 3662 disable_irq(fep->irq[i]); 3663 fec_enet_interrupt(fep->irq[i], dev); 3664 enable_irq(fep->irq[i]); 3665 } 3666 } 3667 } 3668 #endif 3669 3670 static inline void fec_enet_set_netdev_features(struct net_device *netdev, 3671 netdev_features_t features) 3672 { 3673 struct fec_enet_private *fep = netdev_priv(netdev); 3674 netdev_features_t changed = features ^ netdev->features; 3675 3676 netdev->features = features; 3677 3678 /* Receive checksum has been changed */ 3679 if (changed & NETIF_F_RXCSUM) { 3680 if (features & NETIF_F_RXCSUM) 3681 fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 3682 else 3683 fep->csum_flags &= ~FLAG_RX_CSUM_ENABLED; 3684 } 3685 } 3686 3687 static int fec_set_features(struct net_device *netdev, 3688 netdev_features_t features) 3689 { 3690 struct fec_enet_private *fep = netdev_priv(netdev); 3691 netdev_features_t changed = features ^ netdev->features; 3692 3693 if (netif_running(netdev) && changed & NETIF_F_RXCSUM) { 3694 napi_disable(&fep->napi); 3695 netif_tx_lock_bh(netdev); 3696 fec_stop(netdev); 3697 fec_enet_set_netdev_features(netdev, features); 3698 fec_restart(netdev); 3699 netif_tx_wake_all_queues(netdev); 3700 netif_tx_unlock_bh(netdev); 3701 napi_enable(&fep->napi); 3702 } else { 3703 fec_enet_set_netdev_features(netdev, features); 3704 } 3705 3706 return 0; 3707 } 3708 3709 static u16 fec_enet_get_raw_vlan_tci(struct sk_buff *skb) 3710 { 3711 struct vlan_ethhdr *vhdr; 3712 unsigned short vlan_TCI = 0; 3713 3714 if (skb->protocol == htons(ETH_P_ALL)) { 3715 vhdr = (struct vlan_ethhdr *)(skb->data); 3716 vlan_TCI = ntohs(vhdr->h_vlan_TCI); 3717 } 3718 3719 return vlan_TCI; 3720 } 3721 3722 static u16 fec_enet_select_queue(struct net_device *ndev, struct sk_buff *skb, 3723 struct net_device *sb_dev) 3724 { 3725 struct fec_enet_private *fep = netdev_priv(ndev); 3726 u16 vlan_tag; 3727 3728 if (!(fep->quirks & FEC_QUIRK_HAS_AVB)) 3729 return netdev_pick_tx(ndev, skb, NULL); 3730 3731 vlan_tag = fec_enet_get_raw_vlan_tci(skb); 3732 if (!vlan_tag) 3733 return vlan_tag; 3734 3735 return fec_enet_vlan_pri_to_queue[vlan_tag >> 13]; 3736 } 3737 3738 static int fec_enet_bpf(struct net_device *dev, struct netdev_bpf *bpf) 3739 { 3740 struct fec_enet_private *fep = netdev_priv(dev); 3741 bool is_run = netif_running(dev); 3742 struct bpf_prog *old_prog; 3743 3744 switch (bpf->command) { 3745 case XDP_SETUP_PROG: 3746 /* No need to support the SoCs that require to 3747 * do the frame swap because the performance wouldn't be 3748 * better than the skb mode. 3749 */ 3750 if (fep->quirks & FEC_QUIRK_SWAP_FRAME) 3751 return -EOPNOTSUPP; 3752 3753 if (!bpf->prog) 3754 xdp_features_clear_redirect_target(dev); 3755 3756 if (is_run) { 3757 napi_disable(&fep->napi); 3758 netif_tx_disable(dev); 3759 } 3760 3761 old_prog = xchg(&fep->xdp_prog, bpf->prog); 3762 if (old_prog) 3763 bpf_prog_put(old_prog); 3764 3765 fec_restart(dev); 3766 3767 if (is_run) { 3768 napi_enable(&fep->napi); 3769 netif_tx_start_all_queues(dev); 3770 } 3771 3772 if (bpf->prog) 3773 xdp_features_set_redirect_target(dev, false); 3774 3775 return 0; 3776 3777 case XDP_SETUP_XSK_POOL: 3778 return -EOPNOTSUPP; 3779 3780 default: 3781 return -EOPNOTSUPP; 3782 } 3783 } 3784 3785 static int 3786 fec_enet_xdp_get_tx_queue(struct fec_enet_private *fep, int index) 3787 { 3788 if (unlikely(index < 0)) 3789 return 0; 3790 3791 return (index % fep->num_tx_queues); 3792 } 3793 3794 static int fec_enet_txq_xmit_frame(struct fec_enet_private *fep, 3795 struct fec_enet_priv_tx_q *txq, 3796 struct xdp_frame *frame) 3797 { 3798 unsigned int index, status, estatus; 3799 struct bufdesc *bdp; 3800 dma_addr_t dma_addr; 3801 int entries_free; 3802 3803 entries_free = fec_enet_get_free_txdesc_num(txq); 3804 if (entries_free < MAX_SKB_FRAGS + 1) { 3805 netdev_err_once(fep->netdev, "NOT enough BD for SG!\n"); 3806 return -EBUSY; 3807 } 3808 3809 /* Fill in a Tx ring entry */ 3810 bdp = txq->bd.cur; 3811 status = fec16_to_cpu(bdp->cbd_sc); 3812 status &= ~BD_ENET_TX_STATS; 3813 3814 index = fec_enet_get_bd_index(bdp, &txq->bd); 3815 3816 dma_addr = dma_map_single(&fep->pdev->dev, frame->data, 3817 frame->len, DMA_TO_DEVICE); 3818 if (dma_mapping_error(&fep->pdev->dev, dma_addr)) 3819 return -ENOMEM; 3820 3821 status |= (BD_ENET_TX_INTR | BD_ENET_TX_LAST); 3822 if (fep->bufdesc_ex) 3823 estatus = BD_ENET_TX_INT; 3824 3825 bdp->cbd_bufaddr = cpu_to_fec32(dma_addr); 3826 bdp->cbd_datlen = cpu_to_fec16(frame->len); 3827 3828 if (fep->bufdesc_ex) { 3829 struct bufdesc_ex *ebdp = (struct bufdesc_ex *)bdp; 3830 3831 if (fep->quirks & FEC_QUIRK_HAS_AVB) 3832 estatus |= FEC_TX_BD_FTYPE(txq->bd.qid); 3833 3834 ebdp->cbd_bdu = 0; 3835 ebdp->cbd_esc = cpu_to_fec32(estatus); 3836 } 3837 3838 txq->tx_buf[index].type = FEC_TXBUF_T_XDP_NDO; 3839 txq->tx_buf[index].xdp = frame; 3840 3841 /* Make sure the updates to rest of the descriptor are performed before 3842 * transferring ownership. 3843 */ 3844 dma_wmb(); 3845 3846 /* Send it on its way. Tell FEC it's ready, interrupt when done, 3847 * it's the last BD of the frame, and to put the CRC on the end. 3848 */ 3849 status |= (BD_ENET_TX_READY | BD_ENET_TX_TC); 3850 bdp->cbd_sc = cpu_to_fec16(status); 3851 3852 /* If this was the last BD in the ring, start at the beginning again. */ 3853 bdp = fec_enet_get_nextdesc(bdp, &txq->bd); 3854 3855 /* Make sure the update to bdp are performed before txq->bd.cur. */ 3856 dma_wmb(); 3857 3858 txq->bd.cur = bdp; 3859 3860 /* Trigger transmission start */ 3861 writel(0, txq->bd.reg_desc_active); 3862 3863 return 0; 3864 } 3865 3866 static int fec_enet_xdp_xmit(struct net_device *dev, 3867 int num_frames, 3868 struct xdp_frame **frames, 3869 u32 flags) 3870 { 3871 struct fec_enet_private *fep = netdev_priv(dev); 3872 struct fec_enet_priv_tx_q *txq; 3873 int cpu = smp_processor_id(); 3874 unsigned int sent_frames = 0; 3875 struct netdev_queue *nq; 3876 unsigned int queue; 3877 int i; 3878 3879 queue = fec_enet_xdp_get_tx_queue(fep, cpu); 3880 txq = fep->tx_queue[queue]; 3881 nq = netdev_get_tx_queue(fep->netdev, queue); 3882 3883 __netif_tx_lock(nq, cpu); 3884 3885 /* Avoid tx timeout as XDP shares the queue with kernel stack */ 3886 txq_trans_cond_update(nq); 3887 for (i = 0; i < num_frames; i++) { 3888 if (fec_enet_txq_xmit_frame(fep, txq, frames[i]) < 0) 3889 break; 3890 sent_frames++; 3891 } 3892 3893 __netif_tx_unlock(nq); 3894 3895 return sent_frames; 3896 } 3897 3898 static const struct net_device_ops fec_netdev_ops = { 3899 .ndo_open = fec_enet_open, 3900 .ndo_stop = fec_enet_close, 3901 .ndo_start_xmit = fec_enet_start_xmit, 3902 .ndo_select_queue = fec_enet_select_queue, 3903 .ndo_set_rx_mode = set_multicast_list, 3904 .ndo_validate_addr = eth_validate_addr, 3905 .ndo_tx_timeout = fec_timeout, 3906 .ndo_set_mac_address = fec_set_mac_address, 3907 .ndo_eth_ioctl = fec_enet_ioctl, 3908 #ifdef CONFIG_NET_POLL_CONTROLLER 3909 .ndo_poll_controller = fec_poll_controller, 3910 #endif 3911 .ndo_set_features = fec_set_features, 3912 .ndo_bpf = fec_enet_bpf, 3913 .ndo_xdp_xmit = fec_enet_xdp_xmit, 3914 }; 3915 3916 static const unsigned short offset_des_active_rxq[] = { 3917 FEC_R_DES_ACTIVE_0, FEC_R_DES_ACTIVE_1, FEC_R_DES_ACTIVE_2 3918 }; 3919 3920 static const unsigned short offset_des_active_txq[] = { 3921 FEC_X_DES_ACTIVE_0, FEC_X_DES_ACTIVE_1, FEC_X_DES_ACTIVE_2 3922 }; 3923 3924 /* 3925 * XXX: We need to clean up on failure exits here. 3926 * 3927 */ 3928 static int fec_enet_init(struct net_device *ndev) 3929 { 3930 struct fec_enet_private *fep = netdev_priv(ndev); 3931 struct bufdesc *cbd_base; 3932 dma_addr_t bd_dma; 3933 int bd_size; 3934 unsigned int i; 3935 unsigned dsize = fep->bufdesc_ex ? sizeof(struct bufdesc_ex) : 3936 sizeof(struct bufdesc); 3937 unsigned dsize_log2 = __fls(dsize); 3938 int ret; 3939 3940 WARN_ON(dsize != (1 << dsize_log2)); 3941 #if defined(CONFIG_ARM) || defined(CONFIG_ARM64) 3942 fep->rx_align = 0xf; 3943 fep->tx_align = 0xf; 3944 #else 3945 fep->rx_align = 0x3; 3946 fep->tx_align = 0x3; 3947 #endif 3948 fep->rx_pkts_itr = FEC_ITR_ICFT_DEFAULT; 3949 fep->tx_pkts_itr = FEC_ITR_ICFT_DEFAULT; 3950 fep->rx_time_itr = FEC_ITR_ICTT_DEFAULT; 3951 fep->tx_time_itr = FEC_ITR_ICTT_DEFAULT; 3952 3953 /* Check mask of the streaming and coherent API */ 3954 ret = dma_set_mask_and_coherent(&fep->pdev->dev, DMA_BIT_MASK(32)); 3955 if (ret < 0) { 3956 dev_warn(&fep->pdev->dev, "No suitable DMA available\n"); 3957 return ret; 3958 } 3959 3960 ret = fec_enet_alloc_queue(ndev); 3961 if (ret) 3962 return ret; 3963 3964 bd_size = (fep->total_tx_ring_size + fep->total_rx_ring_size) * dsize; 3965 3966 /* Allocate memory for buffer descriptors. */ 3967 cbd_base = dmam_alloc_coherent(&fep->pdev->dev, bd_size, &bd_dma, 3968 GFP_KERNEL); 3969 if (!cbd_base) { 3970 ret = -ENOMEM; 3971 goto free_queue_mem; 3972 } 3973 3974 /* Get the Ethernet address */ 3975 ret = fec_get_mac(ndev); 3976 if (ret) 3977 goto free_queue_mem; 3978 3979 /* Set receive and transmit descriptor base. */ 3980 for (i = 0; i < fep->num_rx_queues; i++) { 3981 struct fec_enet_priv_rx_q *rxq = fep->rx_queue[i]; 3982 unsigned size = dsize * rxq->bd.ring_size; 3983 3984 rxq->bd.qid = i; 3985 rxq->bd.base = cbd_base; 3986 rxq->bd.cur = cbd_base; 3987 rxq->bd.dma = bd_dma; 3988 rxq->bd.dsize = dsize; 3989 rxq->bd.dsize_log2 = dsize_log2; 3990 rxq->bd.reg_desc_active = fep->hwp + offset_des_active_rxq[i]; 3991 bd_dma += size; 3992 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size); 3993 rxq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize); 3994 } 3995 3996 for (i = 0; i < fep->num_tx_queues; i++) { 3997 struct fec_enet_priv_tx_q *txq = fep->tx_queue[i]; 3998 unsigned size = dsize * txq->bd.ring_size; 3999 4000 txq->bd.qid = i; 4001 txq->bd.base = cbd_base; 4002 txq->bd.cur = cbd_base; 4003 txq->bd.dma = bd_dma; 4004 txq->bd.dsize = dsize; 4005 txq->bd.dsize_log2 = dsize_log2; 4006 txq->bd.reg_desc_active = fep->hwp + offset_des_active_txq[i]; 4007 bd_dma += size; 4008 cbd_base = (struct bufdesc *)(((void *)cbd_base) + size); 4009 txq->bd.last = (struct bufdesc *)(((void *)cbd_base) - dsize); 4010 } 4011 4012 4013 /* The FEC Ethernet specific entries in the device structure */ 4014 ndev->watchdog_timeo = TX_TIMEOUT; 4015 ndev->netdev_ops = &fec_netdev_ops; 4016 ndev->ethtool_ops = &fec_enet_ethtool_ops; 4017 4018 writel(FEC_RX_DISABLED_IMASK, fep->hwp + FEC_IMASK); 4019 netif_napi_add(ndev, &fep->napi, fec_enet_rx_napi); 4020 4021 if (fep->quirks & FEC_QUIRK_HAS_VLAN) 4022 /* enable hw VLAN support */ 4023 ndev->features |= NETIF_F_HW_VLAN_CTAG_RX; 4024 4025 if (fep->quirks & FEC_QUIRK_HAS_CSUM) { 4026 netif_set_tso_max_segs(ndev, FEC_MAX_TSO_SEGS); 4027 4028 /* enable hw accelerator */ 4029 ndev->features |= (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM 4030 | NETIF_F_RXCSUM | NETIF_F_SG | NETIF_F_TSO); 4031 fep->csum_flags |= FLAG_RX_CSUM_ENABLED; 4032 } 4033 4034 if (fep->quirks & FEC_QUIRK_HAS_MULTI_QUEUES) { 4035 fep->tx_align = 0; 4036 fep->rx_align = 0x3f; 4037 } 4038 4039 ndev->hw_features = ndev->features; 4040 4041 if (!(fep->quirks & FEC_QUIRK_SWAP_FRAME)) 4042 ndev->xdp_features = NETDEV_XDP_ACT_BASIC | 4043 NETDEV_XDP_ACT_REDIRECT; 4044 4045 fec_restart(ndev); 4046 4047 if (fep->quirks & FEC_QUIRK_MIB_CLEAR) 4048 fec_enet_clear_ethtool_stats(ndev); 4049 else 4050 fec_enet_update_ethtool_stats(ndev); 4051 4052 return 0; 4053 4054 free_queue_mem: 4055 fec_enet_free_queue(ndev); 4056 return ret; 4057 } 4058 4059 #ifdef CONFIG_OF 4060 static int fec_reset_phy(struct platform_device *pdev) 4061 { 4062 struct gpio_desc *phy_reset; 4063 int msec = 1, phy_post_delay = 0; 4064 struct device_node *np = pdev->dev.of_node; 4065 int err; 4066 4067 if (!np) 4068 return 0; 4069 4070 err = of_property_read_u32(np, "phy-reset-duration", &msec); 4071 /* A sane reset duration should not be longer than 1s */ 4072 if (!err && msec > 1000) 4073 msec = 1; 4074 4075 err = of_property_read_u32(np, "phy-reset-post-delay", &phy_post_delay); 4076 /* valid reset duration should be less than 1s */ 4077 if (!err && phy_post_delay > 1000) 4078 return -EINVAL; 4079 4080 phy_reset = devm_gpiod_get_optional(&pdev->dev, "phy-reset", 4081 GPIOD_OUT_HIGH); 4082 if (IS_ERR(phy_reset)) 4083 return dev_err_probe(&pdev->dev, PTR_ERR(phy_reset), 4084 "failed to get phy-reset-gpios\n"); 4085 4086 if (!phy_reset) 4087 return 0; 4088 4089 if (msec > 20) 4090 msleep(msec); 4091 else 4092 usleep_range(msec * 1000, msec * 1000 + 1000); 4093 4094 gpiod_set_value_cansleep(phy_reset, 0); 4095 4096 if (!phy_post_delay) 4097 return 0; 4098 4099 if (phy_post_delay > 20) 4100 msleep(phy_post_delay); 4101 else 4102 usleep_range(phy_post_delay * 1000, 4103 phy_post_delay * 1000 + 1000); 4104 4105 return 0; 4106 } 4107 #else /* CONFIG_OF */ 4108 static int fec_reset_phy(struct platform_device *pdev) 4109 { 4110 /* 4111 * In case of platform probe, the reset has been done 4112 * by machine code. 4113 */ 4114 return 0; 4115 } 4116 #endif /* CONFIG_OF */ 4117 4118 static void 4119 fec_enet_get_queue_num(struct platform_device *pdev, int *num_tx, int *num_rx) 4120 { 4121 struct device_node *np = pdev->dev.of_node; 4122 4123 *num_tx = *num_rx = 1; 4124 4125 if (!np || !of_device_is_available(np)) 4126 return; 4127 4128 /* parse the num of tx and rx queues */ 4129 of_property_read_u32(np, "fsl,num-tx-queues", num_tx); 4130 4131 of_property_read_u32(np, "fsl,num-rx-queues", num_rx); 4132 4133 if (*num_tx < 1 || *num_tx > FEC_ENET_MAX_TX_QS) { 4134 dev_warn(&pdev->dev, "Invalid num_tx(=%d), fall back to 1\n", 4135 *num_tx); 4136 *num_tx = 1; 4137 return; 4138 } 4139 4140 if (*num_rx < 1 || *num_rx > FEC_ENET_MAX_RX_QS) { 4141 dev_warn(&pdev->dev, "Invalid num_rx(=%d), fall back to 1\n", 4142 *num_rx); 4143 *num_rx = 1; 4144 return; 4145 } 4146 4147 } 4148 4149 static int fec_enet_get_irq_cnt(struct platform_device *pdev) 4150 { 4151 int irq_cnt = platform_irq_count(pdev); 4152 4153 if (irq_cnt > FEC_IRQ_NUM) 4154 irq_cnt = FEC_IRQ_NUM; /* last for pps */ 4155 else if (irq_cnt == 2) 4156 irq_cnt = 1; /* last for pps */ 4157 else if (irq_cnt <= 0) 4158 irq_cnt = 1; /* At least 1 irq is needed */ 4159 return irq_cnt; 4160 } 4161 4162 static void fec_enet_get_wakeup_irq(struct platform_device *pdev) 4163 { 4164 struct net_device *ndev = platform_get_drvdata(pdev); 4165 struct fec_enet_private *fep = netdev_priv(ndev); 4166 4167 if (fep->quirks & FEC_QUIRK_WAKEUP_FROM_INT2) 4168 fep->wake_irq = fep->irq[2]; 4169 else 4170 fep->wake_irq = fep->irq[0]; 4171 } 4172 4173 static int fec_enet_init_stop_mode(struct fec_enet_private *fep, 4174 struct device_node *np) 4175 { 4176 struct device_node *gpr_np; 4177 u32 out_val[3]; 4178 int ret = 0; 4179 4180 gpr_np = of_parse_phandle(np, "fsl,stop-mode", 0); 4181 if (!gpr_np) 4182 return 0; 4183 4184 ret = of_property_read_u32_array(np, "fsl,stop-mode", out_val, 4185 ARRAY_SIZE(out_val)); 4186 if (ret) { 4187 dev_dbg(&fep->pdev->dev, "no stop mode property\n"); 4188 goto out; 4189 } 4190 4191 fep->stop_gpr.gpr = syscon_node_to_regmap(gpr_np); 4192 if (IS_ERR(fep->stop_gpr.gpr)) { 4193 dev_err(&fep->pdev->dev, "could not find gpr regmap\n"); 4194 ret = PTR_ERR(fep->stop_gpr.gpr); 4195 fep->stop_gpr.gpr = NULL; 4196 goto out; 4197 } 4198 4199 fep->stop_gpr.reg = out_val[1]; 4200 fep->stop_gpr.bit = out_val[2]; 4201 4202 out: 4203 of_node_put(gpr_np); 4204 4205 return ret; 4206 } 4207 4208 static int 4209 fec_probe(struct platform_device *pdev) 4210 { 4211 struct fec_enet_private *fep; 4212 struct fec_platform_data *pdata; 4213 phy_interface_t interface; 4214 struct net_device *ndev; 4215 int i, irq, ret = 0; 4216 const struct of_device_id *of_id; 4217 static int dev_id; 4218 struct device_node *np = pdev->dev.of_node, *phy_node; 4219 int num_tx_qs; 4220 int num_rx_qs; 4221 char irq_name[8]; 4222 int irq_cnt; 4223 struct fec_devinfo *dev_info; 4224 4225 fec_enet_get_queue_num(pdev, &num_tx_qs, &num_rx_qs); 4226 4227 /* Init network device */ 4228 ndev = alloc_etherdev_mqs(sizeof(struct fec_enet_private) + 4229 FEC_STATS_SIZE, num_tx_qs, num_rx_qs); 4230 if (!ndev) 4231 return -ENOMEM; 4232 4233 SET_NETDEV_DEV(ndev, &pdev->dev); 4234 4235 /* setup board info structure */ 4236 fep = netdev_priv(ndev); 4237 4238 of_id = of_match_device(fec_dt_ids, &pdev->dev); 4239 if (of_id) 4240 pdev->id_entry = of_id->data; 4241 dev_info = (struct fec_devinfo *)pdev->id_entry->driver_data; 4242 if (dev_info) 4243 fep->quirks = dev_info->quirks; 4244 4245 fep->netdev = ndev; 4246 fep->num_rx_queues = num_rx_qs; 4247 fep->num_tx_queues = num_tx_qs; 4248 4249 #if !defined(CONFIG_M5272) 4250 /* default enable pause frame auto negotiation */ 4251 if (fep->quirks & FEC_QUIRK_HAS_GBIT) 4252 fep->pause_flag |= FEC_PAUSE_FLAG_AUTONEG; 4253 #endif 4254 4255 /* Select default pin state */ 4256 pinctrl_pm_select_default_state(&pdev->dev); 4257 4258 fep->hwp = devm_platform_ioremap_resource(pdev, 0); 4259 if (IS_ERR(fep->hwp)) { 4260 ret = PTR_ERR(fep->hwp); 4261 goto failed_ioremap; 4262 } 4263 4264 fep->pdev = pdev; 4265 fep->dev_id = dev_id++; 4266 4267 platform_set_drvdata(pdev, ndev); 4268 4269 if ((of_machine_is_compatible("fsl,imx6q") || 4270 of_machine_is_compatible("fsl,imx6dl")) && 4271 !of_property_read_bool(np, "fsl,err006687-workaround-present")) 4272 fep->quirks |= FEC_QUIRK_ERR006687; 4273 4274 ret = fec_enet_ipc_handle_init(fep); 4275 if (ret) 4276 goto failed_ipc_init; 4277 4278 if (of_property_read_bool(np, "fsl,magic-packet")) 4279 fep->wol_flag |= FEC_WOL_HAS_MAGIC_PACKET; 4280 4281 ret = fec_enet_init_stop_mode(fep, np); 4282 if (ret) 4283 goto failed_stop_mode; 4284 4285 phy_node = of_parse_phandle(np, "phy-handle", 0); 4286 if (!phy_node && of_phy_is_fixed_link(np)) { 4287 ret = of_phy_register_fixed_link(np); 4288 if (ret < 0) { 4289 dev_err(&pdev->dev, 4290 "broken fixed-link specification\n"); 4291 goto failed_phy; 4292 } 4293 phy_node = of_node_get(np); 4294 } 4295 fep->phy_node = phy_node; 4296 4297 ret = of_get_phy_mode(pdev->dev.of_node, &interface); 4298 if (ret) { 4299 pdata = dev_get_platdata(&pdev->dev); 4300 if (pdata) 4301 fep->phy_interface = pdata->phy; 4302 else 4303 fep->phy_interface = PHY_INTERFACE_MODE_MII; 4304 } else { 4305 fep->phy_interface = interface; 4306 } 4307 4308 ret = fec_enet_parse_rgmii_delay(fep, np); 4309 if (ret) 4310 goto failed_rgmii_delay; 4311 4312 fep->clk_ipg = devm_clk_get(&pdev->dev, "ipg"); 4313 if (IS_ERR(fep->clk_ipg)) { 4314 ret = PTR_ERR(fep->clk_ipg); 4315 goto failed_clk; 4316 } 4317 4318 fep->clk_ahb = devm_clk_get(&pdev->dev, "ahb"); 4319 if (IS_ERR(fep->clk_ahb)) { 4320 ret = PTR_ERR(fep->clk_ahb); 4321 goto failed_clk; 4322 } 4323 4324 fep->itr_clk_rate = clk_get_rate(fep->clk_ahb); 4325 4326 /* enet_out is optional, depends on board */ 4327 fep->clk_enet_out = devm_clk_get_optional(&pdev->dev, "enet_out"); 4328 if (IS_ERR(fep->clk_enet_out)) { 4329 ret = PTR_ERR(fep->clk_enet_out); 4330 goto failed_clk; 4331 } 4332 4333 fep->ptp_clk_on = false; 4334 mutex_init(&fep->ptp_clk_mutex); 4335 4336 /* clk_ref is optional, depends on board */ 4337 fep->clk_ref = devm_clk_get_optional(&pdev->dev, "enet_clk_ref"); 4338 if (IS_ERR(fep->clk_ref)) { 4339 ret = PTR_ERR(fep->clk_ref); 4340 goto failed_clk; 4341 } 4342 fep->clk_ref_rate = clk_get_rate(fep->clk_ref); 4343 4344 /* clk_2x_txclk is optional, depends on board */ 4345 if (fep->rgmii_txc_dly || fep->rgmii_rxc_dly) { 4346 fep->clk_2x_txclk = devm_clk_get(&pdev->dev, "enet_2x_txclk"); 4347 if (IS_ERR(fep->clk_2x_txclk)) 4348 fep->clk_2x_txclk = NULL; 4349 } 4350 4351 fep->bufdesc_ex = fep->quirks & FEC_QUIRK_HAS_BUFDESC_EX; 4352 fep->clk_ptp = devm_clk_get(&pdev->dev, "ptp"); 4353 if (IS_ERR(fep->clk_ptp)) { 4354 fep->clk_ptp = NULL; 4355 fep->bufdesc_ex = false; 4356 } 4357 4358 ret = fec_enet_clk_enable(ndev, true); 4359 if (ret) 4360 goto failed_clk; 4361 4362 ret = clk_prepare_enable(fep->clk_ipg); 4363 if (ret) 4364 goto failed_clk_ipg; 4365 ret = clk_prepare_enable(fep->clk_ahb); 4366 if (ret) 4367 goto failed_clk_ahb; 4368 4369 fep->reg_phy = devm_regulator_get_optional(&pdev->dev, "phy"); 4370 if (!IS_ERR(fep->reg_phy)) { 4371 ret = regulator_enable(fep->reg_phy); 4372 if (ret) { 4373 dev_err(&pdev->dev, 4374 "Failed to enable phy regulator: %d\n", ret); 4375 goto failed_regulator; 4376 } 4377 } else { 4378 if (PTR_ERR(fep->reg_phy) == -EPROBE_DEFER) { 4379 ret = -EPROBE_DEFER; 4380 goto failed_regulator; 4381 } 4382 fep->reg_phy = NULL; 4383 } 4384 4385 pm_runtime_set_autosuspend_delay(&pdev->dev, FEC_MDIO_PM_TIMEOUT); 4386 pm_runtime_use_autosuspend(&pdev->dev); 4387 pm_runtime_get_noresume(&pdev->dev); 4388 pm_runtime_set_active(&pdev->dev); 4389 pm_runtime_enable(&pdev->dev); 4390 4391 ret = fec_reset_phy(pdev); 4392 if (ret) 4393 goto failed_reset; 4394 4395 irq_cnt = fec_enet_get_irq_cnt(pdev); 4396 if (fep->bufdesc_ex) 4397 fec_ptp_init(pdev, irq_cnt); 4398 4399 ret = fec_enet_init(ndev); 4400 if (ret) 4401 goto failed_init; 4402 4403 for (i = 0; i < irq_cnt; i++) { 4404 snprintf(irq_name, sizeof(irq_name), "int%d", i); 4405 irq = platform_get_irq_byname_optional(pdev, irq_name); 4406 if (irq < 0) 4407 irq = platform_get_irq(pdev, i); 4408 if (irq < 0) { 4409 ret = irq; 4410 goto failed_irq; 4411 } 4412 ret = devm_request_irq(&pdev->dev, irq, fec_enet_interrupt, 4413 0, pdev->name, ndev); 4414 if (ret) 4415 goto failed_irq; 4416 4417 fep->irq[i] = irq; 4418 } 4419 4420 /* Decide which interrupt line is wakeup capable */ 4421 fec_enet_get_wakeup_irq(pdev); 4422 4423 ret = fec_enet_mii_init(pdev); 4424 if (ret) 4425 goto failed_mii_init; 4426 4427 /* Carrier starts down, phylib will bring it up */ 4428 netif_carrier_off(ndev); 4429 fec_enet_clk_enable(ndev, false); 4430 pinctrl_pm_select_sleep_state(&pdev->dev); 4431 4432 ndev->max_mtu = PKT_MAXBUF_SIZE - ETH_HLEN - ETH_FCS_LEN; 4433 4434 ret = register_netdev(ndev); 4435 if (ret) 4436 goto failed_register; 4437 4438 device_init_wakeup(&ndev->dev, fep->wol_flag & 4439 FEC_WOL_HAS_MAGIC_PACKET); 4440 4441 if (fep->bufdesc_ex && fep->ptp_clock) 4442 netdev_info(ndev, "registered PHC device %d\n", fep->dev_id); 4443 4444 INIT_WORK(&fep->tx_timeout_work, fec_enet_timeout_work); 4445 4446 pm_runtime_mark_last_busy(&pdev->dev); 4447 pm_runtime_put_autosuspend(&pdev->dev); 4448 4449 return 0; 4450 4451 failed_register: 4452 fec_enet_mii_remove(fep); 4453 failed_mii_init: 4454 failed_irq: 4455 failed_init: 4456 fec_ptp_stop(pdev); 4457 failed_reset: 4458 pm_runtime_put_noidle(&pdev->dev); 4459 pm_runtime_disable(&pdev->dev); 4460 if (fep->reg_phy) 4461 regulator_disable(fep->reg_phy); 4462 failed_regulator: 4463 clk_disable_unprepare(fep->clk_ahb); 4464 failed_clk_ahb: 4465 clk_disable_unprepare(fep->clk_ipg); 4466 failed_clk_ipg: 4467 fec_enet_clk_enable(ndev, false); 4468 failed_clk: 4469 failed_rgmii_delay: 4470 if (of_phy_is_fixed_link(np)) 4471 of_phy_deregister_fixed_link(np); 4472 of_node_put(phy_node); 4473 failed_stop_mode: 4474 failed_ipc_init: 4475 failed_phy: 4476 dev_id--; 4477 failed_ioremap: 4478 free_netdev(ndev); 4479 4480 return ret; 4481 } 4482 4483 static void 4484 fec_drv_remove(struct platform_device *pdev) 4485 { 4486 struct net_device *ndev = platform_get_drvdata(pdev); 4487 struct fec_enet_private *fep = netdev_priv(ndev); 4488 struct device_node *np = pdev->dev.of_node; 4489 int ret; 4490 4491 ret = pm_runtime_get_sync(&pdev->dev); 4492 if (ret < 0) 4493 dev_err(&pdev->dev, 4494 "Failed to resume device in remove callback (%pe)\n", 4495 ERR_PTR(ret)); 4496 4497 cancel_work_sync(&fep->tx_timeout_work); 4498 fec_ptp_stop(pdev); 4499 unregister_netdev(ndev); 4500 fec_enet_mii_remove(fep); 4501 if (fep->reg_phy) 4502 regulator_disable(fep->reg_phy); 4503 4504 if (of_phy_is_fixed_link(np)) 4505 of_phy_deregister_fixed_link(np); 4506 of_node_put(fep->phy_node); 4507 4508 /* After pm_runtime_get_sync() failed, the clks are still off, so skip 4509 * disabling them again. 4510 */ 4511 if (ret >= 0) { 4512 clk_disable_unprepare(fep->clk_ahb); 4513 clk_disable_unprepare(fep->clk_ipg); 4514 } 4515 pm_runtime_put_noidle(&pdev->dev); 4516 pm_runtime_disable(&pdev->dev); 4517 4518 free_netdev(ndev); 4519 } 4520 4521 static int __maybe_unused fec_suspend(struct device *dev) 4522 { 4523 struct net_device *ndev = dev_get_drvdata(dev); 4524 struct fec_enet_private *fep = netdev_priv(ndev); 4525 int ret; 4526 4527 rtnl_lock(); 4528 if (netif_running(ndev)) { 4529 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) 4530 fep->wol_flag |= FEC_WOL_FLAG_SLEEP_ON; 4531 phy_stop(ndev->phydev); 4532 napi_disable(&fep->napi); 4533 netif_tx_lock_bh(ndev); 4534 netif_device_detach(ndev); 4535 netif_tx_unlock_bh(ndev); 4536 fec_stop(ndev); 4537 if (!(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) { 4538 fec_irqs_disable(ndev); 4539 pinctrl_pm_select_sleep_state(&fep->pdev->dev); 4540 } else { 4541 fec_irqs_disable_except_wakeup(ndev); 4542 if (fep->wake_irq > 0) { 4543 disable_irq(fep->wake_irq); 4544 enable_irq_wake(fep->wake_irq); 4545 } 4546 fec_enet_stop_mode(fep, true); 4547 } 4548 /* It's safe to disable clocks since interrupts are masked */ 4549 fec_enet_clk_enable(ndev, false); 4550 4551 fep->rpm_active = !pm_runtime_status_suspended(dev); 4552 if (fep->rpm_active) { 4553 ret = pm_runtime_force_suspend(dev); 4554 if (ret < 0) { 4555 rtnl_unlock(); 4556 return ret; 4557 } 4558 } 4559 } 4560 rtnl_unlock(); 4561 4562 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) 4563 regulator_disable(fep->reg_phy); 4564 4565 /* SOC supply clock to phy, when clock is disabled, phy link down 4566 * SOC control phy regulator, when regulator is disabled, phy link down 4567 */ 4568 if (fep->clk_enet_out || fep->reg_phy) 4569 fep->link = 0; 4570 4571 return 0; 4572 } 4573 4574 static int __maybe_unused fec_resume(struct device *dev) 4575 { 4576 struct net_device *ndev = dev_get_drvdata(dev); 4577 struct fec_enet_private *fep = netdev_priv(ndev); 4578 int ret; 4579 int val; 4580 4581 if (fep->reg_phy && !(fep->wol_flag & FEC_WOL_FLAG_ENABLE)) { 4582 ret = regulator_enable(fep->reg_phy); 4583 if (ret) 4584 return ret; 4585 } 4586 4587 rtnl_lock(); 4588 if (netif_running(ndev)) { 4589 if (fep->rpm_active) 4590 pm_runtime_force_resume(dev); 4591 4592 ret = fec_enet_clk_enable(ndev, true); 4593 if (ret) { 4594 rtnl_unlock(); 4595 goto failed_clk; 4596 } 4597 if (fep->wol_flag & FEC_WOL_FLAG_ENABLE) { 4598 fec_enet_stop_mode(fep, false); 4599 if (fep->wake_irq) { 4600 disable_irq_wake(fep->wake_irq); 4601 enable_irq(fep->wake_irq); 4602 } 4603 4604 val = readl(fep->hwp + FEC_ECNTRL); 4605 val &= ~(FEC_ECR_MAGICEN | FEC_ECR_SLEEP); 4606 writel(val, fep->hwp + FEC_ECNTRL); 4607 fep->wol_flag &= ~FEC_WOL_FLAG_SLEEP_ON; 4608 } else { 4609 pinctrl_pm_select_default_state(&fep->pdev->dev); 4610 } 4611 fec_restart(ndev); 4612 netif_tx_lock_bh(ndev); 4613 netif_device_attach(ndev); 4614 netif_tx_unlock_bh(ndev); 4615 napi_enable(&fep->napi); 4616 phy_init_hw(ndev->phydev); 4617 phy_start(ndev->phydev); 4618 } 4619 rtnl_unlock(); 4620 4621 return 0; 4622 4623 failed_clk: 4624 if (fep->reg_phy) 4625 regulator_disable(fep->reg_phy); 4626 return ret; 4627 } 4628 4629 static int __maybe_unused fec_runtime_suspend(struct device *dev) 4630 { 4631 struct net_device *ndev = dev_get_drvdata(dev); 4632 struct fec_enet_private *fep = netdev_priv(ndev); 4633 4634 clk_disable_unprepare(fep->clk_ahb); 4635 clk_disable_unprepare(fep->clk_ipg); 4636 4637 return 0; 4638 } 4639 4640 static int __maybe_unused fec_runtime_resume(struct device *dev) 4641 { 4642 struct net_device *ndev = dev_get_drvdata(dev); 4643 struct fec_enet_private *fep = netdev_priv(ndev); 4644 int ret; 4645 4646 ret = clk_prepare_enable(fep->clk_ahb); 4647 if (ret) 4648 return ret; 4649 ret = clk_prepare_enable(fep->clk_ipg); 4650 if (ret) 4651 goto failed_clk_ipg; 4652 4653 return 0; 4654 4655 failed_clk_ipg: 4656 clk_disable_unprepare(fep->clk_ahb); 4657 return ret; 4658 } 4659 4660 static const struct dev_pm_ops fec_pm_ops = { 4661 SET_SYSTEM_SLEEP_PM_OPS(fec_suspend, fec_resume) 4662 SET_RUNTIME_PM_OPS(fec_runtime_suspend, fec_runtime_resume, NULL) 4663 }; 4664 4665 static struct platform_driver fec_driver = { 4666 .driver = { 4667 .name = DRIVER_NAME, 4668 .pm = &fec_pm_ops, 4669 .of_match_table = fec_dt_ids, 4670 .suppress_bind_attrs = true, 4671 }, 4672 .id_table = fec_devtype, 4673 .probe = fec_probe, 4674 .remove_new = fec_drv_remove, 4675 }; 4676 4677 module_platform_driver(fec_driver); 4678 4679 MODULE_LICENSE("GPL"); 4680