1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Texas Instruments Ethernet Switch Driver 4 * 5 * Copyright (C) 2019 Texas Instruments 6 */ 7 8 #include <linux/io.h> 9 #include <linux/clk.h> 10 #include <linux/platform_device.h> 11 #include <linux/timer.h> 12 #include <linux/module.h> 13 #include <linux/irqreturn.h> 14 #include <linux/interrupt.h> 15 #include <linux/if_ether.h> 16 #include <linux/etherdevice.h> 17 #include <linux/net_tstamp.h> 18 #include <linux/phy.h> 19 #include <linux/phy/phy.h> 20 #include <linux/delay.h> 21 #include <linux/pinctrl/consumer.h> 22 #include <linux/pm_runtime.h> 23 #include <linux/gpio/consumer.h> 24 #include <linux/of.h> 25 #include <linux/of_mdio.h> 26 #include <linux/of_net.h> 27 #include <linux/of_platform.h> 28 #include <linux/if_vlan.h> 29 #include <linux/kmemleak.h> 30 #include <linux/sys_soc.h> 31 32 #include <net/switchdev.h> 33 #include <net/page_pool.h> 34 #include <net/pkt_cls.h> 35 #include <net/devlink.h> 36 37 #include "cpsw.h" 38 #include "cpsw_ale.h" 39 #include "cpsw_priv.h" 40 #include "cpsw_sl.h" 41 #include "cpsw_switchdev.h" 42 #include "cpts.h" 43 #include "davinci_cpdma.h" 44 45 #include <net/pkt_sched.h> 46 47 static int debug_level; 48 static int ale_ageout = CPSW_ALE_AGEOUT_DEFAULT; 49 static int rx_packet_max = CPSW_MAX_PACKET_SIZE; 50 static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT; 51 52 struct cpsw_devlink { 53 struct cpsw_common *cpsw; 54 }; 55 56 enum cpsw_devlink_param_id { 57 CPSW_DEVLINK_PARAM_ID_BASE = DEVLINK_PARAM_GENERIC_ID_MAX, 58 CPSW_DL_PARAM_SWITCH_MODE, 59 CPSW_DL_PARAM_ALE_BYPASS, 60 }; 61 62 /* struct cpsw_common is not needed, kept here for compatibility 63 * reasons witrh the old driver 64 */ 65 static int cpsw_slave_index_priv(struct cpsw_common *cpsw, 66 struct cpsw_priv *priv) 67 { 68 if (priv->emac_port == HOST_PORT_NUM) 69 return -1; 70 71 return priv->emac_port - 1; 72 } 73 74 static bool cpsw_is_switch_en(struct cpsw_common *cpsw) 75 { 76 return !cpsw->data.dual_emac; 77 } 78 79 static void cpsw_set_promiscious(struct net_device *ndev, bool enable) 80 { 81 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 82 bool enable_uni = false; 83 int i; 84 85 if (cpsw_is_switch_en(cpsw)) 86 return; 87 88 /* Enabling promiscuous mode for one interface will be 89 * common for both the interface as the interface shares 90 * the same hardware resource. 91 */ 92 for (i = 0; i < cpsw->data.slaves; i++) 93 if (cpsw->slaves[i].ndev && 94 (cpsw->slaves[i].ndev->flags & IFF_PROMISC)) 95 enable_uni = true; 96 97 if (!enable && enable_uni) { 98 enable = enable_uni; 99 dev_dbg(cpsw->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n"); 100 } 101 102 if (enable) { 103 /* Enable unknown unicast, reg/unreg mcast */ 104 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, 105 ALE_P0_UNI_FLOOD, 1); 106 107 dev_dbg(cpsw->dev, "promiscuity enabled\n"); 108 } else { 109 /* Disable unknown unicast */ 110 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, 111 ALE_P0_UNI_FLOOD, 0); 112 dev_dbg(cpsw->dev, "promiscuity disabled\n"); 113 } 114 } 115 116 /** 117 * cpsw_set_mc - adds multicast entry to the table if it's not added or deletes 118 * if it's not deleted 119 * @ndev: device to sync 120 * @addr: address to be added or deleted 121 * @vid: vlan id, if vid < 0 set/unset address for real device 122 * @add: add address if the flag is set or remove otherwise 123 */ 124 static int cpsw_set_mc(struct net_device *ndev, const u8 *addr, 125 int vid, int add) 126 { 127 struct cpsw_priv *priv = netdev_priv(ndev); 128 struct cpsw_common *cpsw = priv->cpsw; 129 int mask, flags, ret, slave_no; 130 131 slave_no = cpsw_slave_index(cpsw, priv); 132 if (vid < 0) 133 vid = cpsw->slaves[slave_no].port_vlan; 134 135 mask = ALE_PORT_HOST; 136 flags = vid ? ALE_VLAN : 0; 137 138 if (add) 139 ret = cpsw_ale_add_mcast(cpsw->ale, addr, mask, flags, vid, 0); 140 else 141 ret = cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid); 142 143 return ret; 144 } 145 146 static int cpsw_update_vlan_mc(struct net_device *vdev, int vid, void *ctx) 147 { 148 struct addr_sync_ctx *sync_ctx = ctx; 149 struct netdev_hw_addr *ha; 150 int found = 0, ret = 0; 151 152 if (!vdev || !(vdev->flags & IFF_UP)) 153 return 0; 154 155 /* vlan address is relevant if its sync_cnt != 0 */ 156 netdev_for_each_mc_addr(ha, vdev) { 157 if (ether_addr_equal(ha->addr, sync_ctx->addr)) { 158 found = ha->sync_cnt; 159 break; 160 } 161 } 162 163 if (found) 164 sync_ctx->consumed++; 165 166 if (sync_ctx->flush) { 167 if (!found) 168 cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0); 169 return 0; 170 } 171 172 if (found) 173 ret = cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 1); 174 175 return ret; 176 } 177 178 static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr, int num) 179 { 180 struct addr_sync_ctx sync_ctx; 181 int ret; 182 183 sync_ctx.consumed = 0; 184 sync_ctx.addr = addr; 185 sync_ctx.ndev = ndev; 186 sync_ctx.flush = 0; 187 188 ret = vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx); 189 if (sync_ctx.consumed < num && !ret) 190 ret = cpsw_set_mc(ndev, addr, -1, 1); 191 192 return ret; 193 } 194 195 static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr, int num) 196 { 197 struct addr_sync_ctx sync_ctx; 198 199 sync_ctx.consumed = 0; 200 sync_ctx.addr = addr; 201 sync_ctx.ndev = ndev; 202 sync_ctx.flush = 1; 203 204 vlan_for_each(ndev, cpsw_update_vlan_mc, &sync_ctx); 205 if (sync_ctx.consumed == num) 206 cpsw_set_mc(ndev, addr, -1, 0); 207 208 return 0; 209 } 210 211 static int cpsw_purge_vlan_mc(struct net_device *vdev, int vid, void *ctx) 212 { 213 struct addr_sync_ctx *sync_ctx = ctx; 214 struct netdev_hw_addr *ha; 215 int found = 0; 216 217 if (!vdev || !(vdev->flags & IFF_UP)) 218 return 0; 219 220 /* vlan address is relevant if its sync_cnt != 0 */ 221 netdev_for_each_mc_addr(ha, vdev) { 222 if (ether_addr_equal(ha->addr, sync_ctx->addr)) { 223 found = ha->sync_cnt; 224 break; 225 } 226 } 227 228 if (!found) 229 return 0; 230 231 sync_ctx->consumed++; 232 cpsw_set_mc(sync_ctx->ndev, sync_ctx->addr, vid, 0); 233 return 0; 234 } 235 236 static int cpsw_purge_all_mc(struct net_device *ndev, const u8 *addr, int num) 237 { 238 struct addr_sync_ctx sync_ctx; 239 240 sync_ctx.addr = addr; 241 sync_ctx.ndev = ndev; 242 sync_ctx.consumed = 0; 243 244 vlan_for_each(ndev, cpsw_purge_vlan_mc, &sync_ctx); 245 if (sync_ctx.consumed < num) 246 cpsw_set_mc(ndev, addr, -1, 0); 247 248 return 0; 249 } 250 251 static void cpsw_ndo_set_rx_mode(struct net_device *ndev) 252 { 253 struct cpsw_priv *priv = netdev_priv(ndev); 254 struct cpsw_common *cpsw = priv->cpsw; 255 256 if (ndev->flags & IFF_PROMISC) { 257 /* Enable promiscuous mode */ 258 cpsw_set_promiscious(ndev, true); 259 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI, priv->emac_port); 260 return; 261 } 262 263 /* Disable promiscuous mode */ 264 cpsw_set_promiscious(ndev, false); 265 266 /* Restore allmulti on vlans if necessary */ 267 cpsw_ale_set_allmulti(cpsw->ale, 268 ndev->flags & IFF_ALLMULTI, priv->emac_port); 269 270 /* add/remove mcast address either for real netdev or for vlan */ 271 __hw_addr_ref_sync_dev(&ndev->mc, ndev, cpsw_add_mc_addr, 272 cpsw_del_mc_addr); 273 } 274 275 static unsigned int cpsw_rxbuf_total_len(unsigned int len) 276 { 277 len += CPSW_HEADROOM_NA; 278 len += SKB_DATA_ALIGN(sizeof(struct skb_shared_info)); 279 280 return SKB_DATA_ALIGN(len); 281 } 282 283 static void cpsw_rx_handler(void *token, int len, int status) 284 { 285 struct page *new_page, *page = token; 286 void *pa = page_address(page); 287 int headroom = CPSW_HEADROOM_NA; 288 struct cpsw_meta_xdp *xmeta; 289 struct cpsw_common *cpsw; 290 struct net_device *ndev; 291 int port, ch, pkt_size; 292 struct cpsw_priv *priv; 293 struct page_pool *pool; 294 struct sk_buff *skb; 295 struct xdp_buff xdp; 296 int ret = 0; 297 dma_addr_t dma; 298 299 xmeta = pa + CPSW_XMETA_OFFSET; 300 cpsw = ndev_to_cpsw(xmeta->ndev); 301 ndev = xmeta->ndev; 302 pkt_size = cpsw->rx_packet_max; 303 ch = xmeta->ch; 304 305 if (status >= 0) { 306 port = CPDMA_RX_SOURCE_PORT(status); 307 if (port) 308 ndev = cpsw->slaves[--port].ndev; 309 } 310 311 priv = netdev_priv(ndev); 312 pool = cpsw->page_pool[ch]; 313 314 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) { 315 /* In dual emac mode check for all interfaces */ 316 if (cpsw->usage_count && status >= 0) { 317 /* The packet received is for the interface which 318 * is already down and the other interface is up 319 * and running, instead of freeing which results 320 * in reducing of the number of rx descriptor in 321 * DMA engine, requeue page back to cpdma. 322 */ 323 new_page = page; 324 goto requeue; 325 } 326 327 /* the interface is going down, pages are purged */ 328 page_pool_recycle_direct(pool, page); 329 return; 330 } 331 332 new_page = page_pool_dev_alloc_pages(pool); 333 if (unlikely(!new_page)) { 334 new_page = page; 335 ndev->stats.rx_dropped++; 336 goto requeue; 337 } 338 339 if (priv->xdp_prog) { 340 int size = len; 341 342 xdp_init_buff(&xdp, PAGE_SIZE, &priv->xdp_rxq[ch]); 343 if (status & CPDMA_RX_VLAN_ENCAP) { 344 headroom += CPSW_RX_VLAN_ENCAP_HDR_SIZE; 345 size -= CPSW_RX_VLAN_ENCAP_HDR_SIZE; 346 } 347 348 xdp_prepare_buff(&xdp, pa, headroom, size, false); 349 350 ret = cpsw_run_xdp(priv, ch, &xdp, page, priv->emac_port, &len); 351 if (ret != CPSW_XDP_PASS) 352 goto requeue; 353 354 headroom = xdp.data - xdp.data_hard_start; 355 356 /* XDP prog can modify vlan tag, so can't use encap header */ 357 status &= ~CPDMA_RX_VLAN_ENCAP; 358 } 359 360 /* pass skb to netstack if no XDP prog or returned XDP_PASS */ 361 skb = build_skb(pa, cpsw_rxbuf_total_len(pkt_size)); 362 if (!skb) { 363 ndev->stats.rx_dropped++; 364 page_pool_recycle_direct(pool, page); 365 goto requeue; 366 } 367 368 skb->offload_fwd_mark = priv->offload_fwd_mark; 369 skb_reserve(skb, headroom); 370 skb_put(skb, len); 371 skb->dev = ndev; 372 if (status & CPDMA_RX_VLAN_ENCAP) 373 cpsw_rx_vlan_encap(skb); 374 if (priv->rx_ts_enabled) 375 cpts_rx_timestamp(cpsw->cpts, skb); 376 skb->protocol = eth_type_trans(skb, ndev); 377 378 /* mark skb for recycling */ 379 skb_mark_for_recycle(skb); 380 netif_receive_skb(skb); 381 382 ndev->stats.rx_bytes += len; 383 ndev->stats.rx_packets++; 384 385 requeue: 386 xmeta = page_address(new_page) + CPSW_XMETA_OFFSET; 387 xmeta->ndev = ndev; 388 xmeta->ch = ch; 389 390 dma = page_pool_get_dma_addr(new_page) + CPSW_HEADROOM_NA; 391 ret = cpdma_chan_submit_mapped(cpsw->rxv[ch].ch, new_page, dma, 392 pkt_size, 0); 393 if (ret < 0) { 394 WARN_ON(ret == -ENOMEM); 395 page_pool_recycle_direct(pool, new_page); 396 } 397 } 398 399 static int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv, 400 unsigned short vid) 401 { 402 struct cpsw_common *cpsw = priv->cpsw; 403 int unreg_mcast_mask = 0; 404 int mcast_mask; 405 u32 port_mask; 406 int ret; 407 408 port_mask = (1 << priv->emac_port) | ALE_PORT_HOST; 409 410 mcast_mask = ALE_PORT_HOST; 411 if (priv->ndev->flags & IFF_ALLMULTI) 412 unreg_mcast_mask = mcast_mask; 413 414 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask, 415 unreg_mcast_mask); 416 if (ret != 0) 417 return ret; 418 419 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, 420 HOST_PORT_NUM, ALE_VLAN, vid); 421 if (ret != 0) 422 goto clean_vid; 423 424 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 425 mcast_mask, ALE_VLAN, vid, 0); 426 if (ret != 0) 427 goto clean_vlan_ucast; 428 return 0; 429 430 clean_vlan_ucast: 431 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, 432 HOST_PORT_NUM, ALE_VLAN, vid); 433 clean_vid: 434 cpsw_ale_del_vlan(cpsw->ale, vid, 0); 435 return ret; 436 } 437 438 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev, 439 __be16 proto, u16 vid) 440 { 441 struct cpsw_priv *priv = netdev_priv(ndev); 442 struct cpsw_common *cpsw = priv->cpsw; 443 int ret, i; 444 445 if (cpsw_is_switch_en(cpsw)) { 446 dev_dbg(cpsw->dev, ".ndo_vlan_rx_add_vid called in switch mode\n"); 447 return 0; 448 } 449 450 if (vid == cpsw->data.default_vlan) 451 return 0; 452 453 ret = pm_runtime_resume_and_get(cpsw->dev); 454 if (ret < 0) 455 return ret; 456 457 /* In dual EMAC, reserved VLAN id should not be used for 458 * creating VLAN interfaces as this can break the dual 459 * EMAC port separation 460 */ 461 for (i = 0; i < cpsw->data.slaves; i++) { 462 if (cpsw->slaves[i].ndev && 463 vid == cpsw->slaves[i].port_vlan) { 464 ret = -EINVAL; 465 goto err; 466 } 467 } 468 469 dev_dbg(priv->dev, "Adding vlanid %d to vlan filter\n", vid); 470 ret = cpsw_add_vlan_ale_entry(priv, vid); 471 err: 472 pm_runtime_put(cpsw->dev); 473 return ret; 474 } 475 476 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg) 477 { 478 struct cpsw_priv *priv = arg; 479 480 if (!vdev || !vid) 481 return 0; 482 483 cpsw_ndo_vlan_rx_add_vid(priv->ndev, 0, vid); 484 return 0; 485 } 486 487 /* restore resources after port reset */ 488 static void cpsw_restore(struct cpsw_priv *priv) 489 { 490 struct cpsw_common *cpsw = priv->cpsw; 491 492 /* restore vlan configurations */ 493 vlan_for_each(priv->ndev, cpsw_restore_vlans, priv); 494 495 /* restore MQPRIO offload */ 496 cpsw_mqprio_resume(&cpsw->slaves[priv->emac_port - 1], priv); 497 498 /* restore CBS offload */ 499 cpsw_cbs_resume(&cpsw->slaves[priv->emac_port - 1], priv); 500 501 cpsw_qos_clsflower_resume(priv); 502 } 503 504 static void cpsw_init_stp_ale_entry(struct cpsw_common *cpsw) 505 { 506 static const char stpa[] = {0x01, 0x80, 0xc2, 0x0, 0x0, 0x0}; 507 508 cpsw_ale_add_mcast(cpsw->ale, stpa, 509 ALE_PORT_HOST, ALE_SUPER, 0, 510 ALE_MCAST_BLOCK_LEARN_FWD); 511 } 512 513 static void cpsw_init_host_port_switch(struct cpsw_common *cpsw) 514 { 515 int vlan = cpsw->data.default_vlan; 516 517 writel(CPSW_FIFO_NORMAL_MODE, &cpsw->host_port_regs->tx_in_ctl); 518 519 writel(vlan, &cpsw->host_port_regs->port_vlan); 520 521 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, 522 ALE_ALL_PORTS, ALE_ALL_PORTS, 523 ALE_PORT_1 | ALE_PORT_2); 524 525 cpsw_init_stp_ale_entry(cpsw); 526 527 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1); 528 dev_dbg(cpsw->dev, "Set P0_UNI_FLOOD\n"); 529 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0); 530 } 531 532 static void cpsw_init_host_port_dual_mac(struct cpsw_common *cpsw) 533 { 534 int vlan = cpsw->data.default_vlan; 535 536 writel(CPSW_FIFO_DUAL_MAC_MODE, &cpsw->host_port_regs->tx_in_ctl); 537 538 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0); 539 dev_dbg(cpsw->dev, "unset P0_UNI_FLOOD\n"); 540 541 writel(vlan, &cpsw->host_port_regs->port_vlan); 542 543 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0); 544 /* learning make no sense in dual_mac mode */ 545 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1); 546 } 547 548 static void cpsw_init_host_port(struct cpsw_priv *priv) 549 { 550 struct cpsw_common *cpsw = priv->cpsw; 551 u32 control_reg; 552 553 /* soft reset the controller and initialize ale */ 554 soft_reset("cpsw", &cpsw->regs->soft_reset); 555 cpsw_ale_start(cpsw->ale); 556 557 /* switch to vlan unaware mode */ 558 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 559 CPSW_ALE_VLAN_AWARE); 560 control_reg = readl(&cpsw->regs->control); 561 control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP; 562 writel(control_reg, &cpsw->regs->control); 563 564 /* setup host port priority mapping */ 565 writel_relaxed(CPDMA_TX_PRIORITY_MAP, 566 &cpsw->host_port_regs->cpdma_tx_pri_map); 567 writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map); 568 569 /* disable priority elevation */ 570 writel_relaxed(0, &cpsw->regs->ptype); 571 572 /* enable statistics collection only on all ports */ 573 writel_relaxed(0x7, &cpsw->regs->stat_port_en); 574 575 /* Enable internal fifo flow control */ 576 writel(0x7, &cpsw->regs->flow_control); 577 578 if (cpsw_is_switch_en(cpsw)) 579 cpsw_init_host_port_switch(cpsw); 580 else 581 cpsw_init_host_port_dual_mac(cpsw); 582 583 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, 584 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 585 } 586 587 static void cpsw_port_add_dual_emac_def_ale_entries(struct cpsw_priv *priv, 588 struct cpsw_slave *slave) 589 { 590 u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST; 591 struct cpsw_common *cpsw = priv->cpsw; 592 u32 reg; 593 594 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN : 595 CPSW2_PORT_VLAN; 596 slave_write(slave, slave->port_vlan, reg); 597 598 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask, 599 port_mask, port_mask, 0); 600 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 601 ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 602 ALE_MCAST_FWD); 603 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, 604 HOST_PORT_NUM, ALE_VLAN | 605 ALE_SECURE, slave->port_vlan); 606 cpsw_ale_control_set(cpsw->ale, priv->emac_port, 607 ALE_PORT_DROP_UNKNOWN_VLAN, 1); 608 /* learning make no sense in dual_mac mode */ 609 cpsw_ale_control_set(cpsw->ale, priv->emac_port, 610 ALE_PORT_NOLEARN, 1); 611 } 612 613 static void cpsw_port_add_switch_def_ale_entries(struct cpsw_priv *priv, 614 struct cpsw_slave *slave) 615 { 616 u32 port_mask = 1 << priv->emac_port | ALE_PORT_HOST; 617 struct cpsw_common *cpsw = priv->cpsw; 618 u32 reg; 619 620 cpsw_ale_control_set(cpsw->ale, priv->emac_port, 621 ALE_PORT_DROP_UNKNOWN_VLAN, 0); 622 cpsw_ale_control_set(cpsw->ale, priv->emac_port, 623 ALE_PORT_NOLEARN, 0); 624 /* disabling SA_UPDATE required to make stp work, without this setting 625 * Host MAC addresses will jump between ports. 626 * As per TRM MAC address can be defined as unicast supervisory (super) 627 * by setting both (ALE_BLOCKED | ALE_SECURE) which should prevent 628 * SA_UPDATE, but HW seems works incorrectly and setting ALE_SECURE 629 * causes STP packets to be dropped due to ingress filter 630 * if (source address found) and (secure) and 631 * (receive port number != port_number)) 632 * then discard the packet 633 */ 634 cpsw_ale_control_set(cpsw->ale, priv->emac_port, 635 ALE_PORT_NO_SA_UPDATE, 1); 636 637 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 638 port_mask, ALE_VLAN, slave->port_vlan, 639 ALE_MCAST_FWD_2); 640 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, 641 HOST_PORT_NUM, ALE_VLAN, slave->port_vlan); 642 643 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN : 644 CPSW2_PORT_VLAN; 645 slave_write(slave, slave->port_vlan, reg); 646 } 647 648 static void cpsw_adjust_link(struct net_device *ndev) 649 { 650 struct cpsw_priv *priv = netdev_priv(ndev); 651 struct cpsw_common *cpsw = priv->cpsw; 652 struct cpsw_slave *slave; 653 struct phy_device *phy; 654 u32 mac_control = 0; 655 656 slave = &cpsw->slaves[priv->emac_port - 1]; 657 phy = slave->phy; 658 659 if (!phy) 660 return; 661 662 if (phy->link) { 663 mac_control = CPSW_SL_CTL_GMII_EN; 664 665 if (phy->speed == 1000) 666 mac_control |= CPSW_SL_CTL_GIG; 667 if (phy->duplex) 668 mac_control |= CPSW_SL_CTL_FULLDUPLEX; 669 670 /* set speed_in input in case RMII mode is used in 100Mbps */ 671 if (phy->speed == 100) 672 mac_control |= CPSW_SL_CTL_IFCTL_A; 673 /* in band mode only works in 10Mbps RGMII mode */ 674 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy)) 675 mac_control |= CPSW_SL_CTL_EXT_EN; /* In Band mode */ 676 677 if (priv->rx_pause) 678 mac_control |= CPSW_SL_CTL_RX_FLOW_EN; 679 680 if (priv->tx_pause) 681 mac_control |= CPSW_SL_CTL_TX_FLOW_EN; 682 683 if (mac_control != slave->mac_control) 684 cpsw_sl_ctl_set(slave->mac_sl, mac_control); 685 686 /* enable forwarding */ 687 cpsw_ale_control_set(cpsw->ale, priv->emac_port, 688 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 689 690 netif_tx_wake_all_queues(ndev); 691 692 if (priv->shp_cfg_speed && 693 priv->shp_cfg_speed != slave->phy->speed && 694 !cpsw_shp_is_off(priv)) 695 dev_warn(priv->dev, "Speed was changed, CBS shaper speeds are changed!"); 696 } else { 697 netif_tx_stop_all_queues(ndev); 698 699 mac_control = 0; 700 /* disable forwarding */ 701 cpsw_ale_control_set(cpsw->ale, priv->emac_port, 702 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 703 704 cpsw_sl_wait_for_idle(slave->mac_sl, 100); 705 706 cpsw_sl_ctl_reset(slave->mac_sl); 707 } 708 709 if (mac_control != slave->mac_control) 710 phy_print_status(phy); 711 712 slave->mac_control = mac_control; 713 714 if (phy->link && cpsw_need_resplit(cpsw)) 715 cpsw_split_res(cpsw); 716 } 717 718 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv) 719 { 720 struct cpsw_common *cpsw = priv->cpsw; 721 struct phy_device *phy; 722 723 cpsw_sl_reset(slave->mac_sl, 100); 724 cpsw_sl_ctl_reset(slave->mac_sl); 725 726 /* setup priority mapping */ 727 cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_PRI_MAP, 728 RX_PRIORITY_MAPPING); 729 730 switch (cpsw->version) { 731 case CPSW_VERSION_1: 732 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP); 733 /* Increase RX FIFO size to 5 for supporting fullduplex 734 * flow control mode 735 */ 736 slave_write(slave, 737 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) | 738 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS); 739 break; 740 case CPSW_VERSION_2: 741 case CPSW_VERSION_3: 742 case CPSW_VERSION_4: 743 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP); 744 /* Increase RX FIFO size to 5 for supporting fullduplex 745 * flow control mode 746 */ 747 slave_write(slave, 748 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) | 749 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS); 750 break; 751 } 752 753 /* setup max packet size, and mac address */ 754 cpsw_sl_reg_write(slave->mac_sl, CPSW_SL_RX_MAXLEN, 755 cpsw->rx_packet_max); 756 cpsw_set_slave_mac(slave, priv); 757 758 slave->mac_control = 0; /* no link yet */ 759 760 if (cpsw_is_switch_en(cpsw)) 761 cpsw_port_add_switch_def_ale_entries(priv, slave); 762 else 763 cpsw_port_add_dual_emac_def_ale_entries(priv, slave); 764 765 if (!slave->data->phy_node) 766 dev_err(priv->dev, "no phy found on slave %d\n", 767 slave->slave_num); 768 phy = of_phy_connect(priv->ndev, slave->data->phy_node, 769 &cpsw_adjust_link, 0, slave->data->phy_if); 770 if (!phy) { 771 dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n", 772 slave->data->phy_node, 773 slave->slave_num); 774 return; 775 } 776 slave->phy = phy; 777 778 phy_attached_info(slave->phy); 779 780 phy_start(slave->phy); 781 782 /* Configure GMII_SEL register */ 783 phy_set_mode_ext(slave->data->ifphy, PHY_MODE_ETHERNET, 784 slave->data->phy_if); 785 } 786 787 static int cpsw_ndo_stop(struct net_device *ndev) 788 { 789 struct cpsw_priv *priv = netdev_priv(ndev); 790 struct cpsw_common *cpsw = priv->cpsw; 791 struct cpsw_slave *slave; 792 793 cpsw_info(priv, ifdown, "shutting down ndev\n"); 794 slave = &cpsw->slaves[priv->emac_port - 1]; 795 if (slave->phy) 796 phy_stop(slave->phy); 797 798 netif_tx_stop_all_queues(priv->ndev); 799 800 if (slave->phy) { 801 phy_disconnect(slave->phy); 802 slave->phy = NULL; 803 } 804 805 __hw_addr_ref_unsync_dev(&ndev->mc, ndev, cpsw_purge_all_mc); 806 807 if (cpsw->usage_count <= 1) { 808 napi_disable(&cpsw->napi_rx); 809 napi_disable(&cpsw->napi_tx); 810 cpts_unregister(cpsw->cpts); 811 cpsw_intr_disable(cpsw); 812 cpdma_ctlr_stop(cpsw->dma); 813 cpsw_ale_stop(cpsw->ale); 814 cpsw_destroy_xdp_rxqs(cpsw); 815 } 816 817 if (cpsw_need_resplit(cpsw)) 818 cpsw_split_res(cpsw); 819 820 cpsw->usage_count--; 821 pm_runtime_put_sync(cpsw->dev); 822 return 0; 823 } 824 825 static int cpsw_ndo_open(struct net_device *ndev) 826 { 827 struct cpsw_priv *priv = netdev_priv(ndev); 828 struct cpsw_common *cpsw = priv->cpsw; 829 int ret; 830 831 dev_info(priv->dev, "starting ndev. mode: %s\n", 832 cpsw_is_switch_en(cpsw) ? "switch" : "dual_mac"); 833 ret = pm_runtime_resume_and_get(cpsw->dev); 834 if (ret < 0) 835 return ret; 836 837 /* Notify the stack of the actual queue counts. */ 838 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num); 839 if (ret) { 840 dev_err(priv->dev, "cannot set real number of tx queues\n"); 841 goto pm_cleanup; 842 } 843 844 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num); 845 if (ret) { 846 dev_err(priv->dev, "cannot set real number of rx queues\n"); 847 goto pm_cleanup; 848 } 849 850 /* Initialize host and slave ports */ 851 if (!cpsw->usage_count) 852 cpsw_init_host_port(priv); 853 cpsw_slave_open(&cpsw->slaves[priv->emac_port - 1], priv); 854 855 /* initialize shared resources for every ndev */ 856 if (!cpsw->usage_count) { 857 /* create rxqs for both infs in dual mac as they use same pool 858 * and must be destroyed together when no users. 859 */ 860 ret = cpsw_create_xdp_rxqs(cpsw); 861 if (ret < 0) 862 goto err_cleanup; 863 864 ret = cpsw_fill_rx_channels(priv); 865 if (ret < 0) 866 goto err_cleanup; 867 868 if (cpsw->cpts) { 869 if (cpts_register(cpsw->cpts)) 870 dev_err(priv->dev, "error registering cpts device\n"); 871 else 872 writel(0x10, &cpsw->wr_regs->misc_en); 873 } 874 875 napi_enable(&cpsw->napi_rx); 876 napi_enable(&cpsw->napi_tx); 877 878 if (cpsw->tx_irq_disabled) { 879 cpsw->tx_irq_disabled = false; 880 enable_irq(cpsw->irqs_table[1]); 881 } 882 883 if (cpsw->rx_irq_disabled) { 884 cpsw->rx_irq_disabled = false; 885 enable_irq(cpsw->irqs_table[0]); 886 } 887 } 888 889 cpsw_restore(priv); 890 891 /* Enable Interrupt pacing if configured */ 892 if (cpsw->coal_intvl != 0) { 893 struct ethtool_coalesce coal; 894 895 coal.rx_coalesce_usecs = cpsw->coal_intvl; 896 cpsw_set_coalesce(ndev, &coal, NULL, NULL); 897 } 898 899 cpdma_ctlr_start(cpsw->dma); 900 cpsw_intr_enable(cpsw); 901 cpsw->usage_count++; 902 903 return 0; 904 905 err_cleanup: 906 cpsw_ndo_stop(ndev); 907 908 pm_cleanup: 909 pm_runtime_put_sync(cpsw->dev); 910 return ret; 911 } 912 913 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb, 914 struct net_device *ndev) 915 { 916 struct cpsw_priv *priv = netdev_priv(ndev); 917 struct cpsw_common *cpsw = priv->cpsw; 918 struct cpts *cpts = cpsw->cpts; 919 struct netdev_queue *txq; 920 struct cpdma_chan *txch; 921 int ret, q_idx; 922 923 if (skb_put_padto(skb, READ_ONCE(priv->tx_packet_min))) { 924 cpsw_err(priv, tx_err, "packet pad failed\n"); 925 ndev->stats.tx_dropped++; 926 return NET_XMIT_DROP; 927 } 928 929 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 930 priv->tx_ts_enabled && cpts_can_timestamp(cpts, skb)) 931 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 932 933 q_idx = skb_get_queue_mapping(skb); 934 if (q_idx >= cpsw->tx_ch_num) 935 q_idx = q_idx % cpsw->tx_ch_num; 936 937 txch = cpsw->txv[q_idx].ch; 938 txq = netdev_get_tx_queue(ndev, q_idx); 939 skb_tx_timestamp(skb); 940 ret = cpdma_chan_submit(txch, skb, skb->data, skb->len, 941 priv->emac_port); 942 if (unlikely(ret != 0)) { 943 cpsw_err(priv, tx_err, "desc submit failed\n"); 944 goto fail; 945 } 946 947 /* If there is no more tx desc left free then we need to 948 * tell the kernel to stop sending us tx frames. 949 */ 950 if (unlikely(!cpdma_check_free_tx_desc(txch))) { 951 netif_tx_stop_queue(txq); 952 953 /* Barrier, so that stop_queue visible to other cpus */ 954 smp_mb__after_atomic(); 955 956 if (cpdma_check_free_tx_desc(txch)) 957 netif_tx_wake_queue(txq); 958 } 959 960 return NETDEV_TX_OK; 961 fail: 962 ndev->stats.tx_dropped++; 963 netif_tx_stop_queue(txq); 964 965 /* Barrier, so that stop_queue visible to other cpus */ 966 smp_mb__after_atomic(); 967 968 if (cpdma_check_free_tx_desc(txch)) 969 netif_tx_wake_queue(txq); 970 971 return NETDEV_TX_BUSY; 972 } 973 974 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p) 975 { 976 struct sockaddr *addr = (struct sockaddr *)p; 977 struct cpsw_priv *priv = netdev_priv(ndev); 978 struct cpsw_common *cpsw = priv->cpsw; 979 int ret, slave_no; 980 int flags = 0; 981 u16 vid = 0; 982 983 slave_no = cpsw_slave_index(cpsw, priv); 984 if (!is_valid_ether_addr(addr->sa_data)) 985 return -EADDRNOTAVAIL; 986 987 ret = pm_runtime_resume_and_get(cpsw->dev); 988 if (ret < 0) 989 return ret; 990 991 vid = cpsw->slaves[slave_no].port_vlan; 992 flags = ALE_VLAN | ALE_SECURE; 993 994 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM, 995 flags, vid); 996 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM, 997 flags, vid); 998 999 ether_addr_copy(priv->mac_addr, addr->sa_data); 1000 eth_hw_addr_set(ndev, priv->mac_addr); 1001 cpsw_set_slave_mac(&cpsw->slaves[slave_no], priv); 1002 1003 pm_runtime_put(cpsw->dev); 1004 1005 return 0; 1006 } 1007 1008 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev, 1009 __be16 proto, u16 vid) 1010 { 1011 struct cpsw_priv *priv = netdev_priv(ndev); 1012 struct cpsw_common *cpsw = priv->cpsw; 1013 int ret; 1014 int i; 1015 1016 if (cpsw_is_switch_en(cpsw)) { 1017 dev_dbg(cpsw->dev, "ndo del vlan is called in switch mode\n"); 1018 return 0; 1019 } 1020 1021 if (vid == cpsw->data.default_vlan) 1022 return 0; 1023 1024 ret = pm_runtime_resume_and_get(cpsw->dev); 1025 if (ret < 0) 1026 return ret; 1027 1028 /* reset the return code as pm_runtime_get_sync() can return 1029 * non zero values as well. 1030 */ 1031 ret = 0; 1032 for (i = 0; i < cpsw->data.slaves; i++) { 1033 if (cpsw->slaves[i].ndev && 1034 vid == cpsw->slaves[i].port_vlan) { 1035 ret = -EINVAL; 1036 goto err; 1037 } 1038 } 1039 1040 dev_dbg(priv->dev, "removing vlanid %d from vlan filter\n", vid); 1041 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0); 1042 if (ret) 1043 dev_err(priv->dev, "cpsw_ale_del_vlan() failed: ret %d\n", ret); 1044 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, 1045 HOST_PORT_NUM, ALE_VLAN, vid); 1046 if (ret) 1047 dev_err(priv->dev, "cpsw_ale_del_ucast() failed: ret %d\n", 1048 ret); 1049 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast, 1050 0, ALE_VLAN, vid); 1051 if (ret) 1052 dev_err(priv->dev, "cpsw_ale_del_mcast failed. ret %d\n", 1053 ret); 1054 cpsw_ale_flush_multicast(cpsw->ale, ALE_PORT_HOST, vid); 1055 ret = 0; 1056 err: 1057 pm_runtime_put(cpsw->dev); 1058 return ret; 1059 } 1060 1061 static int cpsw_ndo_get_phys_port_name(struct net_device *ndev, char *name, 1062 size_t len) 1063 { 1064 struct cpsw_priv *priv = netdev_priv(ndev); 1065 int err; 1066 1067 err = snprintf(name, len, "p%d", priv->emac_port); 1068 1069 if (err >= len) 1070 return -EINVAL; 1071 1072 return 0; 1073 } 1074 1075 #ifdef CONFIG_NET_POLL_CONTROLLER 1076 static void cpsw_ndo_poll_controller(struct net_device *ndev) 1077 { 1078 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1079 1080 cpsw_intr_disable(cpsw); 1081 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw); 1082 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw); 1083 cpsw_intr_enable(cpsw); 1084 } 1085 #endif 1086 1087 static int cpsw_ndo_xdp_xmit(struct net_device *ndev, int n, 1088 struct xdp_frame **frames, u32 flags) 1089 { 1090 struct cpsw_priv *priv = netdev_priv(ndev); 1091 struct xdp_frame *xdpf; 1092 int i, nxmit = 0; 1093 1094 if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) 1095 return -EINVAL; 1096 1097 for (i = 0; i < n; i++) { 1098 xdpf = frames[i]; 1099 if (xdpf->len < READ_ONCE(priv->tx_packet_min)) 1100 break; 1101 1102 if (cpsw_xdp_tx_frame(priv, xdpf, NULL, priv->emac_port)) 1103 break; 1104 nxmit++; 1105 } 1106 1107 return nxmit; 1108 } 1109 1110 static int cpsw_get_port_parent_id(struct net_device *ndev, 1111 struct netdev_phys_item_id *ppid) 1112 { 1113 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1114 1115 ppid->id_len = sizeof(cpsw->base_mac); 1116 memcpy(&ppid->id, &cpsw->base_mac, ppid->id_len); 1117 1118 return 0; 1119 } 1120 1121 static const struct net_device_ops cpsw_netdev_ops = { 1122 .ndo_open = cpsw_ndo_open, 1123 .ndo_stop = cpsw_ndo_stop, 1124 .ndo_start_xmit = cpsw_ndo_start_xmit, 1125 .ndo_set_mac_address = cpsw_ndo_set_mac_address, 1126 .ndo_eth_ioctl = cpsw_ndo_ioctl, 1127 .ndo_validate_addr = eth_validate_addr, 1128 .ndo_tx_timeout = cpsw_ndo_tx_timeout, 1129 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode, 1130 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate, 1131 #ifdef CONFIG_NET_POLL_CONTROLLER 1132 .ndo_poll_controller = cpsw_ndo_poll_controller, 1133 #endif 1134 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid, 1135 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid, 1136 .ndo_setup_tc = cpsw_ndo_setup_tc, 1137 .ndo_get_phys_port_name = cpsw_ndo_get_phys_port_name, 1138 .ndo_bpf = cpsw_ndo_bpf, 1139 .ndo_xdp_xmit = cpsw_ndo_xdp_xmit, 1140 .ndo_get_port_parent_id = cpsw_get_port_parent_id, 1141 }; 1142 1143 static void cpsw_get_drvinfo(struct net_device *ndev, 1144 struct ethtool_drvinfo *info) 1145 { 1146 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1147 struct platform_device *pdev; 1148 1149 pdev = to_platform_device(cpsw->dev); 1150 strscpy(info->driver, "cpsw-switch", sizeof(info->driver)); 1151 strscpy(info->version, "2.0", sizeof(info->version)); 1152 strscpy(info->bus_info, pdev->name, sizeof(info->bus_info)); 1153 } 1154 1155 static int cpsw_set_pauseparam(struct net_device *ndev, 1156 struct ethtool_pauseparam *pause) 1157 { 1158 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1159 struct cpsw_priv *priv = netdev_priv(ndev); 1160 int slave_no; 1161 1162 slave_no = cpsw_slave_index(cpsw, priv); 1163 if (!cpsw->slaves[slave_no].phy) 1164 return -EINVAL; 1165 1166 if (!phy_validate_pause(cpsw->slaves[slave_no].phy, pause)) 1167 return -EINVAL; 1168 1169 priv->rx_pause = pause->rx_pause ? true : false; 1170 priv->tx_pause = pause->tx_pause ? true : false; 1171 1172 phy_set_asym_pause(cpsw->slaves[slave_no].phy, 1173 priv->rx_pause, priv->tx_pause); 1174 1175 return 0; 1176 } 1177 1178 static int cpsw_set_channels(struct net_device *ndev, 1179 struct ethtool_channels *chs) 1180 { 1181 return cpsw_set_channels_common(ndev, chs, cpsw_rx_handler); 1182 } 1183 1184 static const struct ethtool_ops cpsw_ethtool_ops = { 1185 .supported_coalesce_params = ETHTOOL_COALESCE_RX_USECS, 1186 .get_drvinfo = cpsw_get_drvinfo, 1187 .get_msglevel = cpsw_get_msglevel, 1188 .set_msglevel = cpsw_set_msglevel, 1189 .get_link = ethtool_op_get_link, 1190 .get_ts_info = cpsw_get_ts_info, 1191 .get_coalesce = cpsw_get_coalesce, 1192 .set_coalesce = cpsw_set_coalesce, 1193 .get_sset_count = cpsw_get_sset_count, 1194 .get_strings = cpsw_get_strings, 1195 .get_ethtool_stats = cpsw_get_ethtool_stats, 1196 .get_pauseparam = cpsw_get_pauseparam, 1197 .set_pauseparam = cpsw_set_pauseparam, 1198 .get_wol = cpsw_get_wol, 1199 .set_wol = cpsw_set_wol, 1200 .get_regs_len = cpsw_get_regs_len, 1201 .get_regs = cpsw_get_regs, 1202 .begin = cpsw_ethtool_op_begin, 1203 .complete = cpsw_ethtool_op_complete, 1204 .get_channels = cpsw_get_channels, 1205 .set_channels = cpsw_set_channels, 1206 .get_link_ksettings = cpsw_get_link_ksettings, 1207 .set_link_ksettings = cpsw_set_link_ksettings, 1208 .get_eee = cpsw_get_eee, 1209 .set_eee = cpsw_set_eee, 1210 .nway_reset = cpsw_nway_reset, 1211 .get_ringparam = cpsw_get_ringparam, 1212 .set_ringparam = cpsw_set_ringparam, 1213 }; 1214 1215 static int cpsw_probe_dt(struct cpsw_common *cpsw) 1216 { 1217 struct device_node *node = cpsw->dev->of_node, *tmp_node, *port_np; 1218 struct cpsw_platform_data *data = &cpsw->data; 1219 struct device *dev = cpsw->dev; 1220 int ret; 1221 u32 prop; 1222 1223 if (!node) 1224 return -EINVAL; 1225 1226 tmp_node = of_get_child_by_name(node, "ethernet-ports"); 1227 if (!tmp_node) 1228 return -ENOENT; 1229 data->slaves = of_get_child_count(tmp_node); 1230 if (data->slaves != CPSW_SLAVE_PORTS_NUM) { 1231 of_node_put(tmp_node); 1232 return -ENOENT; 1233 } 1234 1235 data->active_slave = 0; 1236 data->channels = CPSW_MAX_QUEUES; 1237 data->dual_emac = true; 1238 data->bd_ram_size = CPSW_BD_RAM_SIZE; 1239 data->mac_control = 0; 1240 1241 data->slave_data = devm_kcalloc(dev, CPSW_SLAVE_PORTS_NUM, 1242 sizeof(struct cpsw_slave_data), 1243 GFP_KERNEL); 1244 if (!data->slave_data) { 1245 of_node_put(tmp_node); 1246 return -ENOMEM; 1247 } 1248 1249 /* Populate all the child nodes here... 1250 */ 1251 ret = devm_of_platform_populate(dev); 1252 /* We do not want to force this, as in some cases may not have child */ 1253 if (ret) 1254 dev_warn(dev, "Doesn't have any child node\n"); 1255 1256 for_each_child_of_node(tmp_node, port_np) { 1257 struct cpsw_slave_data *slave_data; 1258 u32 port_id; 1259 1260 ret = of_property_read_u32(port_np, "reg", &port_id); 1261 if (ret < 0) { 1262 dev_err(dev, "%pOF error reading port_id %d\n", 1263 port_np, ret); 1264 goto err_node_put; 1265 } 1266 1267 if (!port_id || port_id > CPSW_SLAVE_PORTS_NUM) { 1268 dev_err(dev, "%pOF has invalid port_id %u\n", 1269 port_np, port_id); 1270 ret = -EINVAL; 1271 goto err_node_put; 1272 } 1273 1274 slave_data = &data->slave_data[port_id - 1]; 1275 1276 slave_data->disabled = !of_device_is_available(port_np); 1277 if (slave_data->disabled) 1278 continue; 1279 1280 slave_data->slave_node = port_np; 1281 slave_data->ifphy = devm_of_phy_get(dev, port_np, NULL); 1282 if (IS_ERR(slave_data->ifphy)) { 1283 ret = PTR_ERR(slave_data->ifphy); 1284 dev_err(dev, "%pOF: Error retrieving port phy: %d\n", 1285 port_np, ret); 1286 goto err_node_put; 1287 } 1288 1289 if (of_phy_is_fixed_link(port_np)) { 1290 ret = of_phy_register_fixed_link(port_np); 1291 if (ret) { 1292 dev_err_probe(dev, ret, "%pOF failed to register fixed-link phy\n", 1293 port_np); 1294 goto err_node_put; 1295 } 1296 slave_data->phy_node = of_node_get(port_np); 1297 } else { 1298 slave_data->phy_node = 1299 of_parse_phandle(port_np, "phy-handle", 0); 1300 } 1301 1302 if (!slave_data->phy_node) { 1303 dev_err(dev, "%pOF no phy found\n", port_np); 1304 ret = -ENODEV; 1305 goto err_node_put; 1306 } 1307 1308 ret = of_get_phy_mode(port_np, &slave_data->phy_if); 1309 if (ret) { 1310 dev_err(dev, "%pOF read phy-mode err %d\n", 1311 port_np, ret); 1312 goto err_node_put; 1313 } 1314 1315 ret = of_get_mac_address(port_np, slave_data->mac_addr); 1316 if (ret) { 1317 ret = ti_cm_get_macid(dev, port_id - 1, 1318 slave_data->mac_addr); 1319 if (ret) 1320 goto err_node_put; 1321 } 1322 1323 if (of_property_read_u32(port_np, "ti,dual-emac-pvid", 1324 &prop)) { 1325 dev_err(dev, "%pOF Missing dual_emac_res_vlan in DT.\n", 1326 port_np); 1327 slave_data->dual_emac_res_vlan = port_id; 1328 dev_err(dev, "%pOF Using %d as Reserved VLAN\n", 1329 port_np, slave_data->dual_emac_res_vlan); 1330 } else { 1331 slave_data->dual_emac_res_vlan = prop; 1332 } 1333 } 1334 1335 of_node_put(tmp_node); 1336 return 0; 1337 1338 err_node_put: 1339 of_node_put(port_np); 1340 of_node_put(tmp_node); 1341 return ret; 1342 } 1343 1344 static void cpsw_remove_dt(struct cpsw_common *cpsw) 1345 { 1346 struct cpsw_platform_data *data = &cpsw->data; 1347 int i = 0; 1348 1349 for (i = 0; i < cpsw->data.slaves; i++) { 1350 struct cpsw_slave_data *slave_data = &data->slave_data[i]; 1351 struct device_node *port_np = slave_data->phy_node; 1352 1353 if (port_np) { 1354 if (of_phy_is_fixed_link(port_np)) 1355 of_phy_deregister_fixed_link(port_np); 1356 1357 of_node_put(port_np); 1358 } 1359 } 1360 } 1361 1362 static int cpsw_create_ports(struct cpsw_common *cpsw) 1363 { 1364 struct cpsw_platform_data *data = &cpsw->data; 1365 struct net_device *ndev, *napi_ndev = NULL; 1366 struct device *dev = cpsw->dev; 1367 struct cpsw_priv *priv; 1368 int ret = 0, i = 0; 1369 1370 for (i = 0; i < cpsw->data.slaves; i++) { 1371 struct cpsw_slave_data *slave_data = &data->slave_data[i]; 1372 1373 if (slave_data->disabled) 1374 continue; 1375 1376 ndev = devm_alloc_etherdev_mqs(dev, sizeof(struct cpsw_priv), 1377 CPSW_MAX_QUEUES, 1378 CPSW_MAX_QUEUES); 1379 if (!ndev) { 1380 dev_err(dev, "error allocating net_device\n"); 1381 return -ENOMEM; 1382 } 1383 1384 priv = netdev_priv(ndev); 1385 priv->cpsw = cpsw; 1386 priv->ndev = ndev; 1387 priv->dev = dev; 1388 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); 1389 priv->emac_port = i + 1; 1390 priv->tx_packet_min = CPSW_MIN_PACKET_SIZE; 1391 1392 if (is_valid_ether_addr(slave_data->mac_addr)) { 1393 ether_addr_copy(priv->mac_addr, slave_data->mac_addr); 1394 dev_info(cpsw->dev, "Detected MACID = %pM\n", 1395 priv->mac_addr); 1396 } else { 1397 eth_random_addr(slave_data->mac_addr); 1398 dev_info(cpsw->dev, "Random MACID = %pM\n", 1399 priv->mac_addr); 1400 } 1401 eth_hw_addr_set(ndev, slave_data->mac_addr); 1402 ether_addr_copy(priv->mac_addr, slave_data->mac_addr); 1403 1404 cpsw->slaves[i].ndev = ndev; 1405 1406 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | 1407 NETIF_F_HW_VLAN_CTAG_RX | NETIF_F_NETNS_LOCAL | NETIF_F_HW_TC; 1408 1409 ndev->xdp_features = NETDEV_XDP_ACT_BASIC | 1410 NETDEV_XDP_ACT_REDIRECT | 1411 NETDEV_XDP_ACT_NDO_XMIT; 1412 1413 ndev->netdev_ops = &cpsw_netdev_ops; 1414 ndev->ethtool_ops = &cpsw_ethtool_ops; 1415 SET_NETDEV_DEV(ndev, dev); 1416 1417 if (!napi_ndev) { 1418 /* CPSW Host port CPDMA interface is shared between 1419 * ports and there is only one TX and one RX IRQs 1420 * available for all possible TX and RX channels 1421 * accordingly. 1422 */ 1423 netif_napi_add(ndev, &cpsw->napi_rx, 1424 cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll); 1425 netif_napi_add_tx(ndev, &cpsw->napi_tx, 1426 cpsw->quirk_irq ? 1427 cpsw_tx_poll : cpsw_tx_mq_poll); 1428 } 1429 1430 napi_ndev = ndev; 1431 } 1432 1433 return ret; 1434 } 1435 1436 static void cpsw_unregister_ports(struct cpsw_common *cpsw) 1437 { 1438 int i = 0; 1439 1440 for (i = 0; i < cpsw->data.slaves; i++) { 1441 if (!cpsw->slaves[i].ndev) 1442 continue; 1443 1444 unregister_netdev(cpsw->slaves[i].ndev); 1445 } 1446 } 1447 1448 static int cpsw_register_ports(struct cpsw_common *cpsw) 1449 { 1450 int ret = 0, i = 0; 1451 1452 for (i = 0; i < cpsw->data.slaves; i++) { 1453 if (!cpsw->slaves[i].ndev) 1454 continue; 1455 1456 /* register the network device */ 1457 ret = register_netdev(cpsw->slaves[i].ndev); 1458 if (ret) { 1459 dev_err(cpsw->dev, 1460 "cpsw: err registering net device%d\n", i); 1461 cpsw->slaves[i].ndev = NULL; 1462 break; 1463 } 1464 } 1465 1466 if (ret) 1467 cpsw_unregister_ports(cpsw); 1468 return ret; 1469 } 1470 1471 bool cpsw_port_dev_check(const struct net_device *ndev) 1472 { 1473 if (ndev->netdev_ops == &cpsw_netdev_ops) { 1474 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1475 1476 return !cpsw->data.dual_emac; 1477 } 1478 1479 return false; 1480 } 1481 1482 static void cpsw_port_offload_fwd_mark_update(struct cpsw_common *cpsw) 1483 { 1484 int set_val = 0; 1485 int i; 1486 1487 if (!cpsw->ale_bypass && 1488 (cpsw->br_members == (ALE_PORT_1 | ALE_PORT_2))) 1489 set_val = 1; 1490 1491 dev_dbg(cpsw->dev, "set offload_fwd_mark %d\n", set_val); 1492 1493 for (i = 0; i < cpsw->data.slaves; i++) { 1494 struct net_device *sl_ndev = cpsw->slaves[i].ndev; 1495 struct cpsw_priv *priv = netdev_priv(sl_ndev); 1496 1497 priv->offload_fwd_mark = set_val; 1498 } 1499 } 1500 1501 static int cpsw_netdevice_port_link(struct net_device *ndev, 1502 struct net_device *br_ndev, 1503 struct netlink_ext_ack *extack) 1504 { 1505 struct cpsw_priv *priv = netdev_priv(ndev); 1506 struct cpsw_common *cpsw = priv->cpsw; 1507 int err; 1508 1509 if (!cpsw->br_members) { 1510 cpsw->hw_bridge_dev = br_ndev; 1511 } else { 1512 /* This is adding the port to a second bridge, this is 1513 * unsupported 1514 */ 1515 if (cpsw->hw_bridge_dev != br_ndev) 1516 return -EOPNOTSUPP; 1517 } 1518 1519 err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL, 1520 false, extack); 1521 if (err) 1522 return err; 1523 1524 cpsw->br_members |= BIT(priv->emac_port); 1525 1526 cpsw_port_offload_fwd_mark_update(cpsw); 1527 1528 return NOTIFY_DONE; 1529 } 1530 1531 static void cpsw_netdevice_port_unlink(struct net_device *ndev) 1532 { 1533 struct cpsw_priv *priv = netdev_priv(ndev); 1534 struct cpsw_common *cpsw = priv->cpsw; 1535 1536 switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL); 1537 1538 cpsw->br_members &= ~BIT(priv->emac_port); 1539 1540 cpsw_port_offload_fwd_mark_update(cpsw); 1541 1542 if (!cpsw->br_members) 1543 cpsw->hw_bridge_dev = NULL; 1544 } 1545 1546 /* netdev notifier */ 1547 static int cpsw_netdevice_event(struct notifier_block *unused, 1548 unsigned long event, void *ptr) 1549 { 1550 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr); 1551 struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 1552 struct netdev_notifier_changeupper_info *info; 1553 int ret = NOTIFY_DONE; 1554 1555 if (!cpsw_port_dev_check(ndev)) 1556 return NOTIFY_DONE; 1557 1558 switch (event) { 1559 case NETDEV_CHANGEUPPER: 1560 info = ptr; 1561 1562 if (netif_is_bridge_master(info->upper_dev)) { 1563 if (info->linking) 1564 ret = cpsw_netdevice_port_link(ndev, 1565 info->upper_dev, 1566 extack); 1567 else 1568 cpsw_netdevice_port_unlink(ndev); 1569 } 1570 break; 1571 default: 1572 return NOTIFY_DONE; 1573 } 1574 1575 return notifier_from_errno(ret); 1576 } 1577 1578 static struct notifier_block cpsw_netdevice_nb __read_mostly = { 1579 .notifier_call = cpsw_netdevice_event, 1580 }; 1581 1582 static int cpsw_register_notifiers(struct cpsw_common *cpsw) 1583 { 1584 int ret = 0; 1585 1586 ret = register_netdevice_notifier(&cpsw_netdevice_nb); 1587 if (ret) { 1588 dev_err(cpsw->dev, "can't register netdevice notifier\n"); 1589 return ret; 1590 } 1591 1592 ret = cpsw_switchdev_register_notifiers(cpsw); 1593 if (ret) 1594 unregister_netdevice_notifier(&cpsw_netdevice_nb); 1595 1596 return ret; 1597 } 1598 1599 static void cpsw_unregister_notifiers(struct cpsw_common *cpsw) 1600 { 1601 cpsw_switchdev_unregister_notifiers(cpsw); 1602 unregister_netdevice_notifier(&cpsw_netdevice_nb); 1603 } 1604 1605 static const struct devlink_ops cpsw_devlink_ops = { 1606 }; 1607 1608 static int cpsw_dl_switch_mode_get(struct devlink *dl, u32 id, 1609 struct devlink_param_gset_ctx *ctx) 1610 { 1611 struct cpsw_devlink *dl_priv = devlink_priv(dl); 1612 struct cpsw_common *cpsw = dl_priv->cpsw; 1613 1614 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id); 1615 1616 if (id != CPSW_DL_PARAM_SWITCH_MODE) 1617 return -EOPNOTSUPP; 1618 1619 ctx->val.vbool = !cpsw->data.dual_emac; 1620 1621 return 0; 1622 } 1623 1624 static int cpsw_dl_switch_mode_set(struct devlink *dl, u32 id, 1625 struct devlink_param_gset_ctx *ctx) 1626 { 1627 struct cpsw_devlink *dl_priv = devlink_priv(dl); 1628 struct cpsw_common *cpsw = dl_priv->cpsw; 1629 int vlan = cpsw->data.default_vlan; 1630 bool switch_en = ctx->val.vbool; 1631 bool if_running = false; 1632 int i; 1633 1634 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id); 1635 1636 if (id != CPSW_DL_PARAM_SWITCH_MODE) 1637 return -EOPNOTSUPP; 1638 1639 if (switch_en == !cpsw->data.dual_emac) 1640 return 0; 1641 1642 if (!switch_en && cpsw->br_members) { 1643 dev_err(cpsw->dev, "Remove ports from BR before disabling switch mode\n"); 1644 return -EINVAL; 1645 } 1646 1647 rtnl_lock(); 1648 1649 for (i = 0; i < cpsw->data.slaves; i++) { 1650 struct cpsw_slave *slave = &cpsw->slaves[i]; 1651 struct net_device *sl_ndev = slave->ndev; 1652 1653 if (!sl_ndev || !netif_running(sl_ndev)) 1654 continue; 1655 1656 if_running = true; 1657 } 1658 1659 if (!if_running) { 1660 /* all ndevs are down */ 1661 cpsw->data.dual_emac = !switch_en; 1662 for (i = 0; i < cpsw->data.slaves; i++) { 1663 struct cpsw_slave *slave = &cpsw->slaves[i]; 1664 struct net_device *sl_ndev = slave->ndev; 1665 1666 if (!sl_ndev) 1667 continue; 1668 1669 if (switch_en) 1670 vlan = cpsw->data.default_vlan; 1671 else 1672 vlan = slave->data->dual_emac_res_vlan; 1673 slave->port_vlan = vlan; 1674 } 1675 goto exit; 1676 } 1677 1678 if (switch_en) { 1679 dev_info(cpsw->dev, "Enable switch mode\n"); 1680 1681 /* enable bypass - no forwarding; all traffic goes to Host */ 1682 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1); 1683 1684 /* clean up ALE table */ 1685 cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1); 1686 cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT); 1687 1688 cpsw_init_host_port_switch(cpsw); 1689 1690 for (i = 0; i < cpsw->data.slaves; i++) { 1691 struct cpsw_slave *slave = &cpsw->slaves[i]; 1692 struct net_device *sl_ndev = slave->ndev; 1693 struct cpsw_priv *priv; 1694 1695 if (!sl_ndev) 1696 continue; 1697 1698 priv = netdev_priv(sl_ndev); 1699 slave->port_vlan = vlan; 1700 WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE_VLAN); 1701 if (netif_running(sl_ndev)) 1702 cpsw_port_add_switch_def_ale_entries(priv, 1703 slave); 1704 } 1705 1706 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0); 1707 cpsw->data.dual_emac = false; 1708 } else { 1709 dev_info(cpsw->dev, "Disable switch mode\n"); 1710 1711 /* enable bypass - no forwarding; all traffic goes to Host */ 1712 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1); 1713 1714 cpsw_ale_control_set(cpsw->ale, 0, ALE_CLEAR, 1); 1715 cpsw_ale_control_get(cpsw->ale, 0, ALE_AGEOUT); 1716 1717 cpsw_init_host_port_dual_mac(cpsw); 1718 1719 for (i = 0; i < cpsw->data.slaves; i++) { 1720 struct cpsw_slave *slave = &cpsw->slaves[i]; 1721 struct net_device *sl_ndev = slave->ndev; 1722 struct cpsw_priv *priv; 1723 1724 if (!sl_ndev) 1725 continue; 1726 1727 priv = netdev_priv(slave->ndev); 1728 slave->port_vlan = slave->data->dual_emac_res_vlan; 1729 WRITE_ONCE(priv->tx_packet_min, CPSW_MIN_PACKET_SIZE); 1730 cpsw_port_add_dual_emac_def_ale_entries(priv, slave); 1731 } 1732 1733 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 0); 1734 cpsw->data.dual_emac = true; 1735 } 1736 exit: 1737 rtnl_unlock(); 1738 1739 return 0; 1740 } 1741 1742 static int cpsw_dl_ale_ctrl_get(struct devlink *dl, u32 id, 1743 struct devlink_param_gset_ctx *ctx) 1744 { 1745 struct cpsw_devlink *dl_priv = devlink_priv(dl); 1746 struct cpsw_common *cpsw = dl_priv->cpsw; 1747 1748 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id); 1749 1750 switch (id) { 1751 case CPSW_DL_PARAM_ALE_BYPASS: 1752 ctx->val.vbool = cpsw_ale_control_get(cpsw->ale, 0, ALE_BYPASS); 1753 break; 1754 default: 1755 return -EOPNOTSUPP; 1756 } 1757 1758 return 0; 1759 } 1760 1761 static int cpsw_dl_ale_ctrl_set(struct devlink *dl, u32 id, 1762 struct devlink_param_gset_ctx *ctx) 1763 { 1764 struct cpsw_devlink *dl_priv = devlink_priv(dl); 1765 struct cpsw_common *cpsw = dl_priv->cpsw; 1766 int ret = -EOPNOTSUPP; 1767 1768 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id); 1769 1770 switch (id) { 1771 case CPSW_DL_PARAM_ALE_BYPASS: 1772 ret = cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1773 ctx->val.vbool); 1774 if (!ret) { 1775 cpsw->ale_bypass = ctx->val.vbool; 1776 cpsw_port_offload_fwd_mark_update(cpsw); 1777 } 1778 break; 1779 default: 1780 return -EOPNOTSUPP; 1781 } 1782 1783 return 0; 1784 } 1785 1786 static const struct devlink_param cpsw_devlink_params[] = { 1787 DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_SWITCH_MODE, 1788 "switch_mode", DEVLINK_PARAM_TYPE_BOOL, 1789 BIT(DEVLINK_PARAM_CMODE_RUNTIME), 1790 cpsw_dl_switch_mode_get, cpsw_dl_switch_mode_set, 1791 NULL), 1792 DEVLINK_PARAM_DRIVER(CPSW_DL_PARAM_ALE_BYPASS, 1793 "ale_bypass", DEVLINK_PARAM_TYPE_BOOL, 1794 BIT(DEVLINK_PARAM_CMODE_RUNTIME), 1795 cpsw_dl_ale_ctrl_get, cpsw_dl_ale_ctrl_set, NULL), 1796 }; 1797 1798 static int cpsw_register_devlink(struct cpsw_common *cpsw) 1799 { 1800 struct device *dev = cpsw->dev; 1801 struct cpsw_devlink *dl_priv; 1802 int ret = 0; 1803 1804 cpsw->devlink = devlink_alloc(&cpsw_devlink_ops, sizeof(*dl_priv), dev); 1805 if (!cpsw->devlink) 1806 return -ENOMEM; 1807 1808 dl_priv = devlink_priv(cpsw->devlink); 1809 dl_priv->cpsw = cpsw; 1810 1811 ret = devlink_params_register(cpsw->devlink, cpsw_devlink_params, 1812 ARRAY_SIZE(cpsw_devlink_params)); 1813 if (ret) { 1814 dev_err(dev, "DL params reg fail ret:%d\n", ret); 1815 goto dl_unreg; 1816 } 1817 1818 devlink_register(cpsw->devlink); 1819 return ret; 1820 1821 dl_unreg: 1822 devlink_free(cpsw->devlink); 1823 return ret; 1824 } 1825 1826 static void cpsw_unregister_devlink(struct cpsw_common *cpsw) 1827 { 1828 devlink_unregister(cpsw->devlink); 1829 devlink_params_unregister(cpsw->devlink, cpsw_devlink_params, 1830 ARRAY_SIZE(cpsw_devlink_params)); 1831 devlink_free(cpsw->devlink); 1832 } 1833 1834 static const struct of_device_id cpsw_of_mtable[] = { 1835 { .compatible = "ti,cpsw-switch"}, 1836 { .compatible = "ti,am335x-cpsw-switch"}, 1837 { .compatible = "ti,am4372-cpsw-switch"}, 1838 { .compatible = "ti,dra7-cpsw-switch"}, 1839 { /* sentinel */ }, 1840 }; 1841 MODULE_DEVICE_TABLE(of, cpsw_of_mtable); 1842 1843 static const struct soc_device_attribute cpsw_soc_devices[] = { 1844 { .family = "AM33xx", .revision = "ES1.0"}, 1845 { /* sentinel */ } 1846 }; 1847 1848 static int cpsw_probe(struct platform_device *pdev) 1849 { 1850 const struct soc_device_attribute *soc; 1851 struct device *dev = &pdev->dev; 1852 struct cpsw_common *cpsw; 1853 struct resource *ss_res; 1854 struct gpio_descs *mode; 1855 void __iomem *ss_regs; 1856 int ret = 0, ch; 1857 struct clk *clk; 1858 int irq; 1859 1860 cpsw = devm_kzalloc(dev, sizeof(struct cpsw_common), GFP_KERNEL); 1861 if (!cpsw) 1862 return -ENOMEM; 1863 1864 cpsw_slave_index = cpsw_slave_index_priv; 1865 1866 cpsw->dev = dev; 1867 1868 cpsw->slaves = devm_kcalloc(dev, 1869 CPSW_SLAVE_PORTS_NUM, 1870 sizeof(struct cpsw_slave), 1871 GFP_KERNEL); 1872 if (!cpsw->slaves) 1873 return -ENOMEM; 1874 1875 mode = devm_gpiod_get_array_optional(dev, "mode", GPIOD_OUT_LOW); 1876 if (IS_ERR(mode)) { 1877 ret = PTR_ERR(mode); 1878 dev_err(dev, "gpio request failed, ret %d\n", ret); 1879 return ret; 1880 } 1881 1882 clk = devm_clk_get(dev, "fck"); 1883 if (IS_ERR(clk)) { 1884 ret = PTR_ERR(clk); 1885 dev_err(dev, "fck is not found %d\n", ret); 1886 return ret; 1887 } 1888 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000; 1889 1890 ss_regs = devm_platform_get_and_ioremap_resource(pdev, 0, &ss_res); 1891 if (IS_ERR(ss_regs)) { 1892 ret = PTR_ERR(ss_regs); 1893 return ret; 1894 } 1895 cpsw->regs = ss_regs; 1896 1897 irq = platform_get_irq_byname(pdev, "rx"); 1898 if (irq < 0) 1899 return irq; 1900 cpsw->irqs_table[0] = irq; 1901 1902 irq = platform_get_irq_byname(pdev, "tx"); 1903 if (irq < 0) 1904 return irq; 1905 cpsw->irqs_table[1] = irq; 1906 1907 irq = platform_get_irq_byname(pdev, "misc"); 1908 if (irq <= 0) 1909 return irq; 1910 cpsw->misc_irq = irq; 1911 1912 platform_set_drvdata(pdev, cpsw); 1913 /* This may be required here for child devices. */ 1914 pm_runtime_enable(dev); 1915 1916 /* Need to enable clocks with runtime PM api to access module 1917 * registers 1918 */ 1919 ret = pm_runtime_resume_and_get(dev); 1920 if (ret < 0) { 1921 pm_runtime_disable(dev); 1922 return ret; 1923 } 1924 1925 ret = cpsw_probe_dt(cpsw); 1926 if (ret) 1927 goto clean_dt_ret; 1928 1929 soc = soc_device_match(cpsw_soc_devices); 1930 if (soc) 1931 cpsw->quirk_irq = true; 1932 1933 cpsw->rx_packet_max = rx_packet_max; 1934 cpsw->descs_pool_size = descs_pool_size; 1935 eth_random_addr(cpsw->base_mac); 1936 1937 ret = cpsw_init_common(cpsw, ss_regs, ale_ageout, 1938 (u32 __force)ss_res->start + CPSW2_BD_OFFSET, 1939 descs_pool_size); 1940 if (ret) 1941 goto clean_dt_ret; 1942 1943 cpsw->wr_regs = cpsw->version == CPSW_VERSION_1 ? 1944 ss_regs + CPSW1_WR_OFFSET : 1945 ss_regs + CPSW2_WR_OFFSET; 1946 1947 ch = cpsw->quirk_irq ? 0 : 7; 1948 cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0); 1949 if (IS_ERR(cpsw->txv[0].ch)) { 1950 dev_err(dev, "error initializing tx dma channel\n"); 1951 ret = PTR_ERR(cpsw->txv[0].ch); 1952 goto clean_cpts; 1953 } 1954 1955 cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1); 1956 if (IS_ERR(cpsw->rxv[0].ch)) { 1957 dev_err(dev, "error initializing rx dma channel\n"); 1958 ret = PTR_ERR(cpsw->rxv[0].ch); 1959 goto clean_cpts; 1960 } 1961 cpsw_split_res(cpsw); 1962 1963 /* setup netdevs */ 1964 ret = cpsw_create_ports(cpsw); 1965 if (ret) 1966 goto clean_unregister_netdev; 1967 1968 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and 1969 * MISC IRQs which are always kept disabled with this driver so 1970 * we will not request them. 1971 * 1972 * If anyone wants to implement support for those, make sure to 1973 * first request and append them to irqs_table array. 1974 */ 1975 1976 ret = devm_request_irq(dev, cpsw->irqs_table[0], cpsw_rx_interrupt, 1977 0, dev_name(dev), cpsw); 1978 if (ret < 0) { 1979 dev_err(dev, "error attaching irq (%d)\n", ret); 1980 goto clean_unregister_netdev; 1981 } 1982 1983 ret = devm_request_irq(dev, cpsw->irqs_table[1], cpsw_tx_interrupt, 1984 0, dev_name(dev), cpsw); 1985 if (ret < 0) { 1986 dev_err(dev, "error attaching irq (%d)\n", ret); 1987 goto clean_unregister_netdev; 1988 } 1989 1990 if (!cpsw->cpts) 1991 goto skip_cpts; 1992 1993 ret = devm_request_irq(dev, cpsw->misc_irq, cpsw_misc_interrupt, 1994 0, dev_name(&pdev->dev), cpsw); 1995 if (ret < 0) { 1996 dev_err(dev, "error attaching misc irq (%d)\n", ret); 1997 goto clean_unregister_netdev; 1998 } 1999 2000 /* Enable misc CPTS evnt_pend IRQ */ 2001 cpts_set_irqpoll(cpsw->cpts, false); 2002 2003 skip_cpts: 2004 ret = cpsw_register_notifiers(cpsw); 2005 if (ret) 2006 goto clean_unregister_netdev; 2007 2008 ret = cpsw_register_devlink(cpsw); 2009 if (ret) 2010 goto clean_unregister_notifiers; 2011 2012 ret = cpsw_register_ports(cpsw); 2013 if (ret) 2014 goto clean_unregister_notifiers; 2015 2016 dev_notice(dev, "initialized (regs %pa, pool size %d) hw_ver:%08X %d.%d (%d)\n", 2017 &ss_res->start, descs_pool_size, 2018 cpsw->version, CPSW_MAJOR_VERSION(cpsw->version), 2019 CPSW_MINOR_VERSION(cpsw->version), 2020 CPSW_RTL_VERSION(cpsw->version)); 2021 2022 pm_runtime_put(dev); 2023 2024 return 0; 2025 2026 clean_unregister_notifiers: 2027 cpsw_unregister_notifiers(cpsw); 2028 clean_unregister_netdev: 2029 cpsw_unregister_ports(cpsw); 2030 clean_cpts: 2031 cpts_release(cpsw->cpts); 2032 cpdma_ctlr_destroy(cpsw->dma); 2033 clean_dt_ret: 2034 cpsw_remove_dt(cpsw); 2035 pm_runtime_put_sync(dev); 2036 pm_runtime_disable(dev); 2037 return ret; 2038 } 2039 2040 static int cpsw_remove(struct platform_device *pdev) 2041 { 2042 struct cpsw_common *cpsw = platform_get_drvdata(pdev); 2043 int ret; 2044 2045 ret = pm_runtime_resume_and_get(&pdev->dev); 2046 if (ret < 0) 2047 return ret; 2048 2049 cpsw_unregister_notifiers(cpsw); 2050 cpsw_unregister_devlink(cpsw); 2051 cpsw_unregister_ports(cpsw); 2052 2053 cpts_release(cpsw->cpts); 2054 cpdma_ctlr_destroy(cpsw->dma); 2055 cpsw_remove_dt(cpsw); 2056 pm_runtime_put_sync(&pdev->dev); 2057 pm_runtime_disable(&pdev->dev); 2058 return 0; 2059 } 2060 2061 static int __maybe_unused cpsw_suspend(struct device *dev) 2062 { 2063 struct cpsw_common *cpsw = dev_get_drvdata(dev); 2064 int i; 2065 2066 rtnl_lock(); 2067 2068 for (i = 0; i < cpsw->data.slaves; i++) { 2069 struct net_device *ndev = cpsw->slaves[i].ndev; 2070 2071 if (!(ndev && netif_running(ndev))) 2072 continue; 2073 2074 cpsw_ndo_stop(ndev); 2075 } 2076 2077 rtnl_unlock(); 2078 2079 /* Select sleep pin state */ 2080 pinctrl_pm_select_sleep_state(dev); 2081 2082 return 0; 2083 } 2084 2085 static int __maybe_unused cpsw_resume(struct device *dev) 2086 { 2087 struct cpsw_common *cpsw = dev_get_drvdata(dev); 2088 int i; 2089 2090 /* Select default pin state */ 2091 pinctrl_pm_select_default_state(dev); 2092 2093 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */ 2094 rtnl_lock(); 2095 2096 for (i = 0; i < cpsw->data.slaves; i++) { 2097 struct net_device *ndev = cpsw->slaves[i].ndev; 2098 2099 if (!(ndev && netif_running(ndev))) 2100 continue; 2101 2102 cpsw_ndo_open(ndev); 2103 } 2104 2105 rtnl_unlock(); 2106 2107 return 0; 2108 } 2109 2110 static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume); 2111 2112 static struct platform_driver cpsw_driver = { 2113 .driver = { 2114 .name = "cpsw-switch", 2115 .pm = &cpsw_pm_ops, 2116 .of_match_table = cpsw_of_mtable, 2117 }, 2118 .probe = cpsw_probe, 2119 .remove = cpsw_remove, 2120 }; 2121 2122 module_platform_driver(cpsw_driver); 2123 2124 MODULE_LICENSE("GPL"); 2125 MODULE_DESCRIPTION("TI CPSW switchdev Ethernet driver"); 2126