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