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