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