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