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