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