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