1 // SPDX-License-Identifier: GPL-2.0 2 /* Texas Instruments K3 AM65 Ethernet Switch SubSystem Driver 3 * 4 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com/ 5 * 6 */ 7 8 #include <linux/clk.h> 9 #include <linux/etherdevice.h> 10 #include <linux/if_vlan.h> 11 #include <linux/interrupt.h> 12 #include <linux/irqdomain.h> 13 #include <linux/kernel.h> 14 #include <linux/kmemleak.h> 15 #include <linux/module.h> 16 #include <linux/netdevice.h> 17 #include <linux/net_tstamp.h> 18 #include <linux/of.h> 19 #include <linux/of_mdio.h> 20 #include <linux/of_net.h> 21 #include <linux/of_device.h> 22 #include <linux/of_platform.h> 23 #include <linux/phylink.h> 24 #include <linux/phy/phy.h> 25 #include <linux/platform_device.h> 26 #include <linux/pm_runtime.h> 27 #include <linux/regmap.h> 28 #include <linux/rtnetlink.h> 29 #include <linux/mfd/syscon.h> 30 #include <linux/sys_soc.h> 31 #include <linux/dma/ti-cppi5.h> 32 #include <linux/dma/k3-udma-glue.h> 33 #include <net/switchdev.h> 34 35 #include "cpsw_ale.h" 36 #include "cpsw_sl.h" 37 #include "am65-cpsw-nuss.h" 38 #include "am65-cpsw-switchdev.h" 39 #include "k3-cppi-desc-pool.h" 40 #include "am65-cpts.h" 41 42 #define AM65_CPSW_SS_BASE 0x0 43 #define AM65_CPSW_SGMII_BASE 0x100 44 #define AM65_CPSW_XGMII_BASE 0x2100 45 #define AM65_CPSW_CPSW_NU_BASE 0x20000 46 #define AM65_CPSW_NU_PORTS_BASE 0x1000 47 #define AM65_CPSW_NU_FRAM_BASE 0x12000 48 #define AM65_CPSW_NU_STATS_BASE 0x1a000 49 #define AM65_CPSW_NU_ALE_BASE 0x1e000 50 #define AM65_CPSW_NU_CPTS_BASE 0x1d000 51 52 #define AM65_CPSW_NU_PORTS_OFFSET 0x1000 53 #define AM65_CPSW_NU_STATS_PORT_OFFSET 0x200 54 #define AM65_CPSW_NU_FRAM_PORT_OFFSET 0x200 55 56 #define AM65_CPSW_MAX_PORTS 8 57 58 #define AM65_CPSW_MIN_PACKET_SIZE VLAN_ETH_ZLEN 59 #define AM65_CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN + ETH_FCS_LEN) 60 61 #define AM65_CPSW_REG_CTL 0x004 62 #define AM65_CPSW_REG_STAT_PORT_EN 0x014 63 #define AM65_CPSW_REG_PTYPE 0x018 64 65 #define AM65_CPSW_P0_REG_CTL 0x004 66 #define AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET 0x008 67 68 #define AM65_CPSW_PORT_REG_PRI_CTL 0x01c 69 #define AM65_CPSW_PORT_REG_RX_PRI_MAP 0x020 70 #define AM65_CPSW_PORT_REG_RX_MAXLEN 0x024 71 72 #define AM65_CPSW_PORTN_REG_SA_L 0x308 73 #define AM65_CPSW_PORTN_REG_SA_H 0x30c 74 #define AM65_CPSW_PORTN_REG_TS_CTL 0x310 75 #define AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG 0x314 76 #define AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG 0x318 77 #define AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 0x31C 78 79 #define AM65_CPSW_SGMII_CONTROL_REG 0x010 80 #define AM65_CPSW_SGMII_MR_ADV_ABILITY_REG 0x018 81 #define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE BIT(0) 82 83 #define AM65_CPSW_CTL_VLAN_AWARE BIT(1) 84 #define AM65_CPSW_CTL_P0_ENABLE BIT(2) 85 #define AM65_CPSW_CTL_P0_TX_CRC_REMOVE BIT(13) 86 #define AM65_CPSW_CTL_P0_RX_PAD BIT(14) 87 88 /* AM65_CPSW_P0_REG_CTL */ 89 #define AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN BIT(0) 90 #define AM65_CPSW_P0_REG_CTL_RX_REMAP_VLAN BIT(16) 91 92 /* AM65_CPSW_PORT_REG_PRI_CTL */ 93 #define AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN BIT(8) 94 95 /* AM65_CPSW_PN_TS_CTL register fields */ 96 #define AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN BIT(4) 97 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN BIT(5) 98 #define AM65_CPSW_PN_TS_CTL_TX_VLAN_LT2_EN BIT(6) 99 #define AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN BIT(7) 100 #define AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN BIT(10) 101 #define AM65_CPSW_PN_TS_CTL_TX_HOST_TS_EN BIT(11) 102 #define AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT 16 103 104 /* AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG register fields */ 105 #define AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT 16 106 107 /* AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2 */ 108 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 BIT(16) 109 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 BIT(17) 110 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 BIT(18) 111 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 BIT(19) 112 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 BIT(20) 113 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 BIT(21) 114 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 BIT(22) 115 #define AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO BIT(23) 116 117 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */ 118 #define AM65_CPSW_TS_EVENT_MSG_TYPE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3)) 119 120 #define AM65_CPSW_TS_SEQ_ID_OFFSET (0x1e) 121 122 #define AM65_CPSW_TS_TX_ANX_ALL_EN \ 123 (AM65_CPSW_PN_TS_CTL_TX_ANX_D_EN | \ 124 AM65_CPSW_PN_TS_CTL_TX_ANX_E_EN | \ 125 AM65_CPSW_PN_TS_CTL_TX_ANX_F_EN) 126 127 #define AM65_CPSW_ALE_AGEOUT_DEFAULT 30 128 /* Number of TX/RX descriptors */ 129 #define AM65_CPSW_MAX_TX_DESC 500 130 #define AM65_CPSW_MAX_RX_DESC 500 131 132 #define AM65_CPSW_NAV_PS_DATA_SIZE 16 133 #define AM65_CPSW_NAV_SW_DATA_SIZE 16 134 135 #define AM65_CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_DRV | NETIF_MSG_LINK | \ 136 NETIF_MSG_IFUP | NETIF_MSG_PROBE | NETIF_MSG_IFDOWN | \ 137 NETIF_MSG_RX_ERR | NETIF_MSG_TX_ERR) 138 139 #define AM65_CPSW_DEFAULT_TX_CHNS 8 140 141 static void am65_cpsw_port_set_sl_mac(struct am65_cpsw_port *slave, 142 const u8 *dev_addr) 143 { 144 u32 mac_hi = (dev_addr[0] << 0) | (dev_addr[1] << 8) | 145 (dev_addr[2] << 16) | (dev_addr[3] << 24); 146 u32 mac_lo = (dev_addr[4] << 0) | (dev_addr[5] << 8); 147 148 writel(mac_hi, slave->port_base + AM65_CPSW_PORTN_REG_SA_H); 149 writel(mac_lo, slave->port_base + AM65_CPSW_PORTN_REG_SA_L); 150 } 151 152 static void am65_cpsw_sl_ctl_reset(struct am65_cpsw_port *port) 153 { 154 cpsw_sl_reset(port->slave.mac_sl, 100); 155 /* Max length register has to be restored after MAC SL reset */ 156 writel(AM65_CPSW_MAX_PACKET_SIZE, 157 port->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN); 158 } 159 160 static void am65_cpsw_nuss_get_ver(struct am65_cpsw_common *common) 161 { 162 common->nuss_ver = readl(common->ss_base); 163 common->cpsw_ver = readl(common->cpsw_base); 164 dev_info(common->dev, 165 "initializing am65 cpsw nuss version 0x%08X, cpsw version 0x%08X Ports: %u quirks:%08x\n", 166 common->nuss_ver, 167 common->cpsw_ver, 168 common->port_num + 1, 169 common->pdata.quirks); 170 } 171 172 static int am65_cpsw_nuss_ndo_slave_add_vid(struct net_device *ndev, 173 __be16 proto, u16 vid) 174 { 175 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 176 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 177 u32 port_mask, unreg_mcast = 0; 178 int ret; 179 180 if (!common->is_emac_mode) 181 return 0; 182 183 if (!netif_running(ndev) || !vid) 184 return 0; 185 186 ret = pm_runtime_resume_and_get(common->dev); 187 if (ret < 0) 188 return ret; 189 190 port_mask = BIT(port->port_id) | ALE_PORT_HOST; 191 if (!vid) 192 unreg_mcast = port_mask; 193 dev_info(common->dev, "Adding vlan %d to vlan filter\n", vid); 194 ret = cpsw_ale_vlan_add_modify(common->ale, vid, port_mask, 195 unreg_mcast, port_mask, 0); 196 197 pm_runtime_put(common->dev); 198 return ret; 199 } 200 201 static int am65_cpsw_nuss_ndo_slave_kill_vid(struct net_device *ndev, 202 __be16 proto, u16 vid) 203 { 204 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 205 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 206 int ret; 207 208 if (!common->is_emac_mode) 209 return 0; 210 211 if (!netif_running(ndev) || !vid) 212 return 0; 213 214 ret = pm_runtime_resume_and_get(common->dev); 215 if (ret < 0) 216 return ret; 217 218 dev_info(common->dev, "Removing vlan %d from vlan filter\n", vid); 219 ret = cpsw_ale_del_vlan(common->ale, vid, 220 BIT(port->port_id) | ALE_PORT_HOST); 221 222 pm_runtime_put(common->dev); 223 return ret; 224 } 225 226 static void am65_cpsw_slave_set_promisc(struct am65_cpsw_port *port, 227 bool promisc) 228 { 229 struct am65_cpsw_common *common = port->common; 230 231 if (promisc && !common->is_emac_mode) { 232 dev_dbg(common->dev, "promisc mode requested in switch mode"); 233 return; 234 } 235 236 if (promisc) { 237 /* Enable promiscuous mode */ 238 cpsw_ale_control_set(common->ale, port->port_id, 239 ALE_PORT_MACONLY_CAF, 1); 240 dev_dbg(common->dev, "promisc enabled\n"); 241 } else { 242 /* Disable promiscuous mode */ 243 cpsw_ale_control_set(common->ale, port->port_id, 244 ALE_PORT_MACONLY_CAF, 0); 245 dev_dbg(common->dev, "promisc disabled\n"); 246 } 247 } 248 249 static void am65_cpsw_nuss_ndo_slave_set_rx_mode(struct net_device *ndev) 250 { 251 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 252 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 253 u32 port_mask; 254 bool promisc; 255 256 promisc = !!(ndev->flags & IFF_PROMISC); 257 am65_cpsw_slave_set_promisc(port, promisc); 258 259 if (promisc) 260 return; 261 262 /* Restore allmulti on vlans if necessary */ 263 cpsw_ale_set_allmulti(common->ale, 264 ndev->flags & IFF_ALLMULTI, port->port_id); 265 266 port_mask = ALE_PORT_HOST; 267 /* Clear all mcast from ALE */ 268 cpsw_ale_flush_multicast(common->ale, port_mask, -1); 269 270 if (!netdev_mc_empty(ndev)) { 271 struct netdev_hw_addr *ha; 272 273 /* program multicast address list into ALE register */ 274 netdev_for_each_mc_addr(ha, ndev) { 275 cpsw_ale_add_mcast(common->ale, ha->addr, 276 port_mask, 0, 0, 0); 277 } 278 } 279 } 280 281 static void am65_cpsw_nuss_ndo_host_tx_timeout(struct net_device *ndev, 282 unsigned int txqueue) 283 { 284 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 285 struct am65_cpsw_tx_chn *tx_chn; 286 struct netdev_queue *netif_txq; 287 unsigned long trans_start; 288 289 netif_txq = netdev_get_tx_queue(ndev, txqueue); 290 tx_chn = &common->tx_chns[txqueue]; 291 trans_start = READ_ONCE(netif_txq->trans_start); 292 293 netdev_err(ndev, "txq:%d DRV_XOFF:%d tmo:%u dql_avail:%d free_desc:%zu\n", 294 txqueue, 295 netif_tx_queue_stopped(netif_txq), 296 jiffies_to_msecs(jiffies - trans_start), 297 dql_avail(&netif_txq->dql), 298 k3_cppi_desc_pool_avail(tx_chn->desc_pool)); 299 300 if (netif_tx_queue_stopped(netif_txq)) { 301 /* try recover if stopped by us */ 302 txq_trans_update(netif_txq); 303 netif_tx_wake_queue(netif_txq); 304 } 305 } 306 307 static int am65_cpsw_nuss_rx_push(struct am65_cpsw_common *common, 308 struct sk_buff *skb) 309 { 310 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 311 struct cppi5_host_desc_t *desc_rx; 312 struct device *dev = common->dev; 313 u32 pkt_len = skb_tailroom(skb); 314 dma_addr_t desc_dma; 315 dma_addr_t buf_dma; 316 void *swdata; 317 318 desc_rx = k3_cppi_desc_pool_alloc(rx_chn->desc_pool); 319 if (!desc_rx) { 320 dev_err(dev, "Failed to allocate RXFDQ descriptor\n"); 321 return -ENOMEM; 322 } 323 desc_dma = k3_cppi_desc_pool_virt2dma(rx_chn->desc_pool, desc_rx); 324 325 buf_dma = dma_map_single(rx_chn->dma_dev, skb->data, pkt_len, 326 DMA_FROM_DEVICE); 327 if (unlikely(dma_mapping_error(rx_chn->dma_dev, buf_dma))) { 328 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 329 dev_err(dev, "Failed to map rx skb buffer\n"); 330 return -EINVAL; 331 } 332 333 cppi5_hdesc_init(desc_rx, CPPI5_INFO0_HDESC_EPIB_PRESENT, 334 AM65_CPSW_NAV_PS_DATA_SIZE); 335 k3_udma_glue_rx_dma_to_cppi5_addr(rx_chn->rx_chn, &buf_dma); 336 cppi5_hdesc_attach_buf(desc_rx, buf_dma, skb_tailroom(skb), buf_dma, skb_tailroom(skb)); 337 swdata = cppi5_hdesc_get_swdata(desc_rx); 338 *((void **)swdata) = skb; 339 340 return k3_udma_glue_push_rx_chn(rx_chn->rx_chn, 0, desc_rx, desc_dma); 341 } 342 343 void am65_cpsw_nuss_set_p0_ptype(struct am65_cpsw_common *common) 344 { 345 struct am65_cpsw_host *host_p = am65_common_get_host(common); 346 u32 val, pri_map; 347 348 /* P0 set Receive Priority Type */ 349 val = readl(host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL); 350 351 if (common->pf_p0_rx_ptype_rrobin) { 352 val |= AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN; 353 /* Enet Ports fifos works in fixed priority mode only, so 354 * reset P0_Rx_Pri_Map so all packet will go in Enet fifo 0 355 */ 356 pri_map = 0x0; 357 } else { 358 val &= ~AM65_CPSW_PORT_REG_PRI_CTL_RX_PTYPE_RROBIN; 359 /* restore P0_Rx_Pri_Map */ 360 pri_map = 0x76543210; 361 } 362 363 writel(pri_map, host_p->port_base + AM65_CPSW_PORT_REG_RX_PRI_MAP); 364 writel(val, host_p->port_base + AM65_CPSW_PORT_REG_PRI_CTL); 365 } 366 367 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common); 368 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common); 369 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port); 370 static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port); 371 372 static void am65_cpsw_nuss_rx_cleanup(void *data, dma_addr_t desc_dma) 373 { 374 struct am65_cpsw_rx_chn *rx_chn = data; 375 struct cppi5_host_desc_t *desc_rx; 376 struct sk_buff *skb; 377 dma_addr_t buf_dma; 378 u32 buf_dma_len; 379 void **swdata; 380 381 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma); 382 swdata = cppi5_hdesc_get_swdata(desc_rx); 383 skb = *swdata; 384 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len); 385 k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma); 386 387 dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE); 388 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 389 390 dev_kfree_skb_any(skb); 391 } 392 393 static void am65_cpsw_nuss_xmit_free(struct am65_cpsw_tx_chn *tx_chn, 394 struct cppi5_host_desc_t *desc) 395 { 396 struct cppi5_host_desc_t *first_desc, *next_desc; 397 dma_addr_t buf_dma, next_desc_dma; 398 u32 buf_dma_len; 399 400 first_desc = desc; 401 next_desc = first_desc; 402 403 cppi5_hdesc_get_obuf(first_desc, &buf_dma, &buf_dma_len); 404 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma); 405 406 dma_unmap_single(tx_chn->dma_dev, buf_dma, buf_dma_len, DMA_TO_DEVICE); 407 408 next_desc_dma = cppi5_hdesc_get_next_hbdesc(first_desc); 409 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma); 410 while (next_desc_dma) { 411 next_desc = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, 412 next_desc_dma); 413 cppi5_hdesc_get_obuf(next_desc, &buf_dma, &buf_dma_len); 414 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &buf_dma); 415 416 dma_unmap_page(tx_chn->dma_dev, buf_dma, buf_dma_len, 417 DMA_TO_DEVICE); 418 419 next_desc_dma = cppi5_hdesc_get_next_hbdesc(next_desc); 420 k3_udma_glue_tx_cppi5_to_dma_addr(tx_chn->tx_chn, &next_desc_dma); 421 422 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc); 423 } 424 425 k3_cppi_desc_pool_free(tx_chn->desc_pool, first_desc); 426 } 427 428 static void am65_cpsw_nuss_tx_cleanup(void *data, dma_addr_t desc_dma) 429 { 430 struct am65_cpsw_tx_chn *tx_chn = data; 431 struct cppi5_host_desc_t *desc_tx; 432 struct sk_buff *skb; 433 void **swdata; 434 435 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, desc_dma); 436 swdata = cppi5_hdesc_get_swdata(desc_tx); 437 skb = *(swdata); 438 am65_cpsw_nuss_xmit_free(tx_chn, desc_tx); 439 440 dev_kfree_skb_any(skb); 441 } 442 443 static int am65_cpsw_nuss_common_open(struct am65_cpsw_common *common) 444 { 445 struct am65_cpsw_host *host_p = am65_common_get_host(common); 446 int port_idx, i, ret, tx; 447 struct sk_buff *skb; 448 u32 val, port_mask; 449 450 if (common->usage_count) 451 return 0; 452 453 /* Control register */ 454 writel(AM65_CPSW_CTL_P0_ENABLE | AM65_CPSW_CTL_P0_TX_CRC_REMOVE | 455 AM65_CPSW_CTL_VLAN_AWARE | AM65_CPSW_CTL_P0_RX_PAD, 456 common->cpsw_base + AM65_CPSW_REG_CTL); 457 /* Max length register */ 458 writel(AM65_CPSW_MAX_PACKET_SIZE, 459 host_p->port_base + AM65_CPSW_PORT_REG_RX_MAXLEN); 460 /* set base flow_id */ 461 writel(common->rx_flow_id_base, 462 host_p->port_base + AM65_CPSW_PORT0_REG_FLOW_ID_OFFSET); 463 writel(AM65_CPSW_P0_REG_CTL_RX_CHECKSUM_EN | AM65_CPSW_P0_REG_CTL_RX_REMAP_VLAN, 464 host_p->port_base + AM65_CPSW_P0_REG_CTL); 465 466 am65_cpsw_nuss_set_p0_ptype(common); 467 468 /* enable statistic */ 469 val = BIT(HOST_PORT_NUM); 470 for (port_idx = 0; port_idx < common->port_num; port_idx++) { 471 struct am65_cpsw_port *port = &common->ports[port_idx]; 472 473 if (!port->disabled) 474 val |= BIT(port->port_id); 475 } 476 writel(val, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN); 477 478 /* disable priority elevation */ 479 writel(0, common->cpsw_base + AM65_CPSW_REG_PTYPE); 480 481 cpsw_ale_start(common->ale); 482 483 /* limit to one RX flow only */ 484 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 485 ALE_DEFAULT_THREAD_ID, 0); 486 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 487 ALE_DEFAULT_THREAD_ENABLE, 1); 488 /* switch to vlan unaware mode */ 489 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1); 490 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 491 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 492 493 /* default vlan cfg: create mask based on enabled ports */ 494 port_mask = GENMASK(common->port_num, 0) & 495 ~common->disabled_ports_mask; 496 497 cpsw_ale_add_vlan(common->ale, 0, port_mask, 498 port_mask, port_mask, 499 port_mask & ~ALE_PORT_HOST); 500 501 if (common->is_emac_mode) 502 am65_cpsw_init_host_port_emac(common); 503 else 504 am65_cpsw_init_host_port_switch(common); 505 506 am65_cpsw_qos_tx_p0_rate_init(common); 507 508 for (i = 0; i < common->rx_chns.descs_num; i++) { 509 skb = __netdev_alloc_skb_ip_align(NULL, 510 AM65_CPSW_MAX_PACKET_SIZE, 511 GFP_KERNEL); 512 if (!skb) { 513 ret = -ENOMEM; 514 dev_err(common->dev, "cannot allocate skb\n"); 515 if (i) 516 goto fail_rx; 517 518 return ret; 519 } 520 521 ret = am65_cpsw_nuss_rx_push(common, skb); 522 if (ret < 0) { 523 dev_err(common->dev, 524 "cannot submit skb to channel rx, error %d\n", 525 ret); 526 kfree_skb(skb); 527 if (i) 528 goto fail_rx; 529 530 return ret; 531 } 532 } 533 534 ret = k3_udma_glue_enable_rx_chn(common->rx_chns.rx_chn); 535 if (ret) { 536 dev_err(common->dev, "couldn't enable rx chn: %d\n", ret); 537 goto fail_rx; 538 } 539 540 for (tx = 0; tx < common->tx_ch_num; tx++) { 541 ret = k3_udma_glue_enable_tx_chn(common->tx_chns[tx].tx_chn); 542 if (ret) { 543 dev_err(common->dev, "couldn't enable tx chn %d: %d\n", 544 tx, ret); 545 tx--; 546 goto fail_tx; 547 } 548 napi_enable(&common->tx_chns[tx].napi_tx); 549 } 550 551 napi_enable(&common->napi_rx); 552 if (common->rx_irq_disabled) { 553 common->rx_irq_disabled = false; 554 enable_irq(common->rx_chns.irq); 555 } 556 557 dev_dbg(common->dev, "cpsw_nuss started\n"); 558 return 0; 559 560 fail_tx: 561 while (tx >= 0) { 562 napi_disable(&common->tx_chns[tx].napi_tx); 563 k3_udma_glue_disable_tx_chn(common->tx_chns[tx].tx_chn); 564 tx--; 565 } 566 567 k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn); 568 569 fail_rx: 570 k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, 0, 571 &common->rx_chns, 572 am65_cpsw_nuss_rx_cleanup, 0); 573 return ret; 574 } 575 576 static int am65_cpsw_nuss_common_stop(struct am65_cpsw_common *common) 577 { 578 int i; 579 580 if (common->usage_count != 1) 581 return 0; 582 583 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, 584 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 585 586 /* shutdown tx channels */ 587 atomic_set(&common->tdown_cnt, common->tx_ch_num); 588 /* ensure new tdown_cnt value is visible */ 589 smp_mb__after_atomic(); 590 reinit_completion(&common->tdown_complete); 591 592 for (i = 0; i < common->tx_ch_num; i++) 593 k3_udma_glue_tdown_tx_chn(common->tx_chns[i].tx_chn, false); 594 595 i = wait_for_completion_timeout(&common->tdown_complete, 596 msecs_to_jiffies(1000)); 597 if (!i) 598 dev_err(common->dev, "tx timeout\n"); 599 for (i = 0; i < common->tx_ch_num; i++) 600 napi_disable(&common->tx_chns[i].napi_tx); 601 602 for (i = 0; i < common->tx_ch_num; i++) { 603 k3_udma_glue_reset_tx_chn(common->tx_chns[i].tx_chn, 604 &common->tx_chns[i], 605 am65_cpsw_nuss_tx_cleanup); 606 k3_udma_glue_disable_tx_chn(common->tx_chns[i].tx_chn); 607 } 608 609 reinit_completion(&common->tdown_complete); 610 k3_udma_glue_tdown_rx_chn(common->rx_chns.rx_chn, true); 611 612 if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ) { 613 i = wait_for_completion_timeout(&common->tdown_complete, msecs_to_jiffies(1000)); 614 if (!i) 615 dev_err(common->dev, "rx teardown timeout\n"); 616 } 617 618 napi_disable(&common->napi_rx); 619 620 for (i = 0; i < AM65_CPSW_MAX_RX_FLOWS; i++) 621 k3_udma_glue_reset_rx_chn(common->rx_chns.rx_chn, i, 622 &common->rx_chns, 623 am65_cpsw_nuss_rx_cleanup, !!i); 624 625 k3_udma_glue_disable_rx_chn(common->rx_chns.rx_chn); 626 627 cpsw_ale_stop(common->ale); 628 629 writel(0, common->cpsw_base + AM65_CPSW_REG_CTL); 630 writel(0, common->cpsw_base + AM65_CPSW_REG_STAT_PORT_EN); 631 632 dev_dbg(common->dev, "cpsw_nuss stopped\n"); 633 return 0; 634 } 635 636 static int am65_cpsw_nuss_ndo_slave_stop(struct net_device *ndev) 637 { 638 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 639 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 640 int ret; 641 642 phylink_stop(port->slave.phylink); 643 644 netif_tx_stop_all_queues(ndev); 645 646 phylink_disconnect_phy(port->slave.phylink); 647 648 ret = am65_cpsw_nuss_common_stop(common); 649 if (ret) 650 return ret; 651 652 common->usage_count--; 653 pm_runtime_put(common->dev); 654 return 0; 655 } 656 657 static int cpsw_restore_vlans(struct net_device *vdev, int vid, void *arg) 658 { 659 struct am65_cpsw_port *port = arg; 660 661 if (!vdev) 662 return 0; 663 664 return am65_cpsw_nuss_ndo_slave_add_vid(port->ndev, 0, vid); 665 } 666 667 static int am65_cpsw_nuss_ndo_slave_open(struct net_device *ndev) 668 { 669 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 670 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 671 int ret, i; 672 u32 reg; 673 674 ret = pm_runtime_resume_and_get(common->dev); 675 if (ret < 0) 676 return ret; 677 678 /* Idle MAC port */ 679 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE); 680 cpsw_sl_wait_for_idle(port->slave.mac_sl, 100); 681 cpsw_sl_ctl_reset(port->slave.mac_sl); 682 683 /* soft reset MAC */ 684 cpsw_sl_reg_write(port->slave.mac_sl, CPSW_SL_SOFT_RESET, 1); 685 mdelay(1); 686 reg = cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_SOFT_RESET); 687 if (reg) { 688 dev_err(common->dev, "soft RESET didn't complete\n"); 689 ret = -ETIMEDOUT; 690 goto runtime_put; 691 } 692 693 /* Notify the stack of the actual queue counts. */ 694 ret = netif_set_real_num_tx_queues(ndev, common->tx_ch_num); 695 if (ret) { 696 dev_err(common->dev, "cannot set real number of tx queues\n"); 697 goto runtime_put; 698 } 699 700 ret = netif_set_real_num_rx_queues(ndev, AM65_CPSW_MAX_RX_QUEUES); 701 if (ret) { 702 dev_err(common->dev, "cannot set real number of rx queues\n"); 703 goto runtime_put; 704 } 705 706 for (i = 0; i < common->tx_ch_num; i++) { 707 struct netdev_queue *txq = netdev_get_tx_queue(ndev, i); 708 709 netdev_tx_reset_queue(txq); 710 txq->tx_maxrate = common->tx_chns[i].rate_mbps; 711 } 712 713 ret = am65_cpsw_nuss_common_open(common); 714 if (ret) 715 goto runtime_put; 716 717 common->usage_count++; 718 719 am65_cpsw_port_set_sl_mac(port, ndev->dev_addr); 720 721 if (common->is_emac_mode) 722 am65_cpsw_init_port_emac_ale(port); 723 else 724 am65_cpsw_init_port_switch_ale(port); 725 726 /* mac_sl should be configured via phy-link interface */ 727 am65_cpsw_sl_ctl_reset(port); 728 729 ret = phylink_of_phy_connect(port->slave.phylink, port->slave.phy_node, 0); 730 if (ret) 731 goto error_cleanup; 732 733 /* restore vlan configurations */ 734 vlan_for_each(ndev, cpsw_restore_vlans, port); 735 736 phylink_start(port->slave.phylink); 737 738 return 0; 739 740 error_cleanup: 741 am65_cpsw_nuss_ndo_slave_stop(ndev); 742 return ret; 743 744 runtime_put: 745 pm_runtime_put(common->dev); 746 return ret; 747 } 748 749 static void am65_cpsw_nuss_rx_ts(struct sk_buff *skb, u32 *psdata) 750 { 751 struct skb_shared_hwtstamps *ssh; 752 u64 ns; 753 754 ns = ((u64)psdata[1] << 32) | psdata[0]; 755 756 ssh = skb_hwtstamps(skb); 757 memset(ssh, 0, sizeof(*ssh)); 758 ssh->hwtstamp = ns_to_ktime(ns); 759 } 760 761 /* RX psdata[2] word format - checksum information */ 762 #define AM65_CPSW_RX_PSD_CSUM_ADD GENMASK(15, 0) 763 #define AM65_CPSW_RX_PSD_CSUM_ERR BIT(16) 764 #define AM65_CPSW_RX_PSD_IS_FRAGMENT BIT(17) 765 #define AM65_CPSW_RX_PSD_IS_TCP BIT(18) 766 #define AM65_CPSW_RX_PSD_IPV6_VALID BIT(19) 767 #define AM65_CPSW_RX_PSD_IPV4_VALID BIT(20) 768 769 static void am65_cpsw_nuss_rx_csum(struct sk_buff *skb, u32 csum_info) 770 { 771 /* HW can verify IPv4/IPv6 TCP/UDP packets checksum 772 * csum information provides in psdata[2] word: 773 * AM65_CPSW_RX_PSD_CSUM_ERR bit - indicates csum error 774 * AM65_CPSW_RX_PSD_IPV6_VALID and AM65_CPSW_RX_PSD_IPV4_VALID 775 * bits - indicates IPv4/IPv6 packet 776 * AM65_CPSW_RX_PSD_IS_FRAGMENT bit - indicates fragmented packet 777 * AM65_CPSW_RX_PSD_CSUM_ADD has value 0xFFFF for non fragmented packets 778 * or csum value for fragmented packets if !AM65_CPSW_RX_PSD_CSUM_ERR 779 */ 780 skb_checksum_none_assert(skb); 781 782 if (unlikely(!(skb->dev->features & NETIF_F_RXCSUM))) 783 return; 784 785 if ((csum_info & (AM65_CPSW_RX_PSD_IPV6_VALID | 786 AM65_CPSW_RX_PSD_IPV4_VALID)) && 787 !(csum_info & AM65_CPSW_RX_PSD_CSUM_ERR)) { 788 /* csum for fragmented packets is unsupported */ 789 if (!(csum_info & AM65_CPSW_RX_PSD_IS_FRAGMENT)) 790 skb->ip_summed = CHECKSUM_UNNECESSARY; 791 } 792 } 793 794 static int am65_cpsw_nuss_rx_packets(struct am65_cpsw_common *common, 795 u32 flow_idx) 796 { 797 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 798 u32 buf_dma_len, pkt_len, port_id = 0, csum_info; 799 struct am65_cpsw_ndev_priv *ndev_priv; 800 struct am65_cpsw_ndev_stats *stats; 801 struct cppi5_host_desc_t *desc_rx; 802 struct device *dev = common->dev; 803 struct sk_buff *skb, *new_skb; 804 dma_addr_t desc_dma, buf_dma; 805 struct am65_cpsw_port *port; 806 struct net_device *ndev; 807 void **swdata; 808 u32 *psdata; 809 int ret = 0; 810 811 ret = k3_udma_glue_pop_rx_chn(rx_chn->rx_chn, flow_idx, &desc_dma); 812 if (ret) { 813 if (ret != -ENODATA) 814 dev_err(dev, "RX: pop chn fail %d\n", ret); 815 return ret; 816 } 817 818 if (cppi5_desc_is_tdcm(desc_dma)) { 819 dev_dbg(dev, "%s RX tdown flow: %u\n", __func__, flow_idx); 820 if (common->pdata.quirks & AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ) 821 complete(&common->tdown_complete); 822 return 0; 823 } 824 825 desc_rx = k3_cppi_desc_pool_dma2virt(rx_chn->desc_pool, desc_dma); 826 dev_dbg(dev, "%s flow_idx: %u desc %pad\n", 827 __func__, flow_idx, &desc_dma); 828 829 swdata = cppi5_hdesc_get_swdata(desc_rx); 830 skb = *swdata; 831 cppi5_hdesc_get_obuf(desc_rx, &buf_dma, &buf_dma_len); 832 k3_udma_glue_rx_cppi5_to_dma_addr(rx_chn->rx_chn, &buf_dma); 833 pkt_len = cppi5_hdesc_get_pktlen(desc_rx); 834 cppi5_desc_get_tags_ids(&desc_rx->hdr, &port_id, NULL); 835 dev_dbg(dev, "%s rx port_id:%d\n", __func__, port_id); 836 port = am65_common_get_port(common, port_id); 837 ndev = port->ndev; 838 skb->dev = ndev; 839 840 psdata = cppi5_hdesc_get_psdata(desc_rx); 841 /* add RX timestamp */ 842 if (port->rx_ts_enabled) 843 am65_cpsw_nuss_rx_ts(skb, psdata); 844 csum_info = psdata[2]; 845 dev_dbg(dev, "%s rx csum_info:%#x\n", __func__, csum_info); 846 847 dma_unmap_single(rx_chn->dma_dev, buf_dma, buf_dma_len, DMA_FROM_DEVICE); 848 849 k3_cppi_desc_pool_free(rx_chn->desc_pool, desc_rx); 850 851 new_skb = netdev_alloc_skb_ip_align(ndev, AM65_CPSW_MAX_PACKET_SIZE); 852 if (new_skb) { 853 ndev_priv = netdev_priv(ndev); 854 am65_cpsw_nuss_set_offload_fwd_mark(skb, ndev_priv->offload_fwd_mark); 855 skb_put(skb, pkt_len); 856 skb->protocol = eth_type_trans(skb, ndev); 857 am65_cpsw_nuss_rx_csum(skb, csum_info); 858 napi_gro_receive(&common->napi_rx, skb); 859 860 stats = this_cpu_ptr(ndev_priv->stats); 861 862 u64_stats_update_begin(&stats->syncp); 863 stats->rx_packets++; 864 stats->rx_bytes += pkt_len; 865 u64_stats_update_end(&stats->syncp); 866 kmemleak_not_leak(new_skb); 867 } else { 868 ndev->stats.rx_dropped++; 869 new_skb = skb; 870 } 871 872 if (netif_dormant(ndev)) { 873 dev_kfree_skb_any(new_skb); 874 ndev->stats.rx_dropped++; 875 return 0; 876 } 877 878 ret = am65_cpsw_nuss_rx_push(common, new_skb); 879 if (WARN_ON(ret < 0)) { 880 dev_kfree_skb_any(new_skb); 881 ndev->stats.rx_errors++; 882 ndev->stats.rx_dropped++; 883 } 884 885 return ret; 886 } 887 888 static int am65_cpsw_nuss_rx_poll(struct napi_struct *napi_rx, int budget) 889 { 890 struct am65_cpsw_common *common = am65_cpsw_napi_to_common(napi_rx); 891 int flow = AM65_CPSW_MAX_RX_FLOWS; 892 int cur_budget, ret; 893 int num_rx = 0; 894 895 /* process every flow */ 896 while (flow--) { 897 cur_budget = budget - num_rx; 898 899 while (cur_budget--) { 900 ret = am65_cpsw_nuss_rx_packets(common, flow); 901 if (ret) 902 break; 903 num_rx++; 904 } 905 906 if (num_rx >= budget) 907 break; 908 } 909 910 dev_dbg(common->dev, "%s num_rx:%d %d\n", __func__, num_rx, budget); 911 912 if (num_rx < budget && napi_complete_done(napi_rx, num_rx)) { 913 if (common->rx_irq_disabled) { 914 common->rx_irq_disabled = false; 915 enable_irq(common->rx_chns.irq); 916 } 917 } 918 919 return num_rx; 920 } 921 922 static struct sk_buff * 923 am65_cpsw_nuss_tx_compl_packet(struct am65_cpsw_tx_chn *tx_chn, 924 dma_addr_t desc_dma) 925 { 926 struct am65_cpsw_ndev_priv *ndev_priv; 927 struct am65_cpsw_ndev_stats *stats; 928 struct cppi5_host_desc_t *desc_tx; 929 struct net_device *ndev; 930 struct sk_buff *skb; 931 void **swdata; 932 933 desc_tx = k3_cppi_desc_pool_dma2virt(tx_chn->desc_pool, 934 desc_dma); 935 swdata = cppi5_hdesc_get_swdata(desc_tx); 936 skb = *(swdata); 937 am65_cpsw_nuss_xmit_free(tx_chn, desc_tx); 938 939 ndev = skb->dev; 940 941 am65_cpts_tx_timestamp(tx_chn->common->cpts, skb); 942 943 ndev_priv = netdev_priv(ndev); 944 stats = this_cpu_ptr(ndev_priv->stats); 945 u64_stats_update_begin(&stats->syncp); 946 stats->tx_packets++; 947 stats->tx_bytes += skb->len; 948 u64_stats_update_end(&stats->syncp); 949 950 return skb; 951 } 952 953 static void am65_cpsw_nuss_tx_wake(struct am65_cpsw_tx_chn *tx_chn, struct net_device *ndev, 954 struct netdev_queue *netif_txq) 955 { 956 if (netif_tx_queue_stopped(netif_txq)) { 957 /* Check whether the queue is stopped due to stalled 958 * tx dma, if the queue is stopped then wake the queue 959 * as we have free desc for tx 960 */ 961 __netif_tx_lock(netif_txq, smp_processor_id()); 962 if (netif_running(ndev) && 963 (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= MAX_SKB_FRAGS)) 964 netif_tx_wake_queue(netif_txq); 965 966 __netif_tx_unlock(netif_txq); 967 } 968 } 969 970 static int am65_cpsw_nuss_tx_compl_packets(struct am65_cpsw_common *common, 971 int chn, unsigned int budget) 972 { 973 struct device *dev = common->dev; 974 struct am65_cpsw_tx_chn *tx_chn; 975 struct netdev_queue *netif_txq; 976 unsigned int total_bytes = 0; 977 struct net_device *ndev; 978 struct sk_buff *skb; 979 dma_addr_t desc_dma; 980 int res, num_tx = 0; 981 982 tx_chn = &common->tx_chns[chn]; 983 984 while (true) { 985 spin_lock(&tx_chn->lock); 986 res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma); 987 spin_unlock(&tx_chn->lock); 988 if (res == -ENODATA) 989 break; 990 991 if (cppi5_desc_is_tdcm(desc_dma)) { 992 if (atomic_dec_and_test(&common->tdown_cnt)) 993 complete(&common->tdown_complete); 994 break; 995 } 996 997 skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma); 998 total_bytes = skb->len; 999 ndev = skb->dev; 1000 napi_consume_skb(skb, budget); 1001 num_tx++; 1002 1003 netif_txq = netdev_get_tx_queue(ndev, chn); 1004 1005 netdev_tx_completed_queue(netif_txq, num_tx, total_bytes); 1006 1007 am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq); 1008 } 1009 1010 dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx); 1011 1012 return num_tx; 1013 } 1014 1015 static int am65_cpsw_nuss_tx_compl_packets_2g(struct am65_cpsw_common *common, 1016 int chn, unsigned int budget) 1017 { 1018 struct device *dev = common->dev; 1019 struct am65_cpsw_tx_chn *tx_chn; 1020 struct netdev_queue *netif_txq; 1021 unsigned int total_bytes = 0; 1022 struct net_device *ndev; 1023 struct sk_buff *skb; 1024 dma_addr_t desc_dma; 1025 int res, num_tx = 0; 1026 1027 tx_chn = &common->tx_chns[chn]; 1028 1029 while (true) { 1030 res = k3_udma_glue_pop_tx_chn(tx_chn->tx_chn, &desc_dma); 1031 if (res == -ENODATA) 1032 break; 1033 1034 if (cppi5_desc_is_tdcm(desc_dma)) { 1035 if (atomic_dec_and_test(&common->tdown_cnt)) 1036 complete(&common->tdown_complete); 1037 break; 1038 } 1039 1040 skb = am65_cpsw_nuss_tx_compl_packet(tx_chn, desc_dma); 1041 1042 ndev = skb->dev; 1043 total_bytes += skb->len; 1044 napi_consume_skb(skb, budget); 1045 num_tx++; 1046 } 1047 1048 if (!num_tx) 1049 return 0; 1050 1051 netif_txq = netdev_get_tx_queue(ndev, chn); 1052 1053 netdev_tx_completed_queue(netif_txq, num_tx, total_bytes); 1054 1055 am65_cpsw_nuss_tx_wake(tx_chn, ndev, netif_txq); 1056 1057 dev_dbg(dev, "%s:%u pkt:%d\n", __func__, chn, num_tx); 1058 1059 return num_tx; 1060 } 1061 1062 static int am65_cpsw_nuss_tx_poll(struct napi_struct *napi_tx, int budget) 1063 { 1064 struct am65_cpsw_tx_chn *tx_chn = am65_cpsw_napi_to_tx_chn(napi_tx); 1065 int num_tx; 1066 1067 if (AM65_CPSW_IS_CPSW2G(tx_chn->common)) 1068 num_tx = am65_cpsw_nuss_tx_compl_packets_2g(tx_chn->common, tx_chn->id, budget); 1069 else 1070 num_tx = am65_cpsw_nuss_tx_compl_packets(tx_chn->common, tx_chn->id, budget); 1071 1072 if (num_tx >= budget) 1073 return budget; 1074 1075 if (napi_complete_done(napi_tx, num_tx)) 1076 enable_irq(tx_chn->irq); 1077 1078 return 0; 1079 } 1080 1081 static irqreturn_t am65_cpsw_nuss_rx_irq(int irq, void *dev_id) 1082 { 1083 struct am65_cpsw_common *common = dev_id; 1084 1085 common->rx_irq_disabled = true; 1086 disable_irq_nosync(irq); 1087 napi_schedule(&common->napi_rx); 1088 1089 return IRQ_HANDLED; 1090 } 1091 1092 static irqreturn_t am65_cpsw_nuss_tx_irq(int irq, void *dev_id) 1093 { 1094 struct am65_cpsw_tx_chn *tx_chn = dev_id; 1095 1096 disable_irq_nosync(irq); 1097 napi_schedule(&tx_chn->napi_tx); 1098 1099 return IRQ_HANDLED; 1100 } 1101 1102 static netdev_tx_t am65_cpsw_nuss_ndo_slave_xmit(struct sk_buff *skb, 1103 struct net_device *ndev) 1104 { 1105 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 1106 struct cppi5_host_desc_t *first_desc, *next_desc, *cur_desc; 1107 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1108 struct device *dev = common->dev; 1109 struct am65_cpsw_tx_chn *tx_chn; 1110 struct netdev_queue *netif_txq; 1111 dma_addr_t desc_dma, buf_dma; 1112 int ret, q_idx, i; 1113 void **swdata; 1114 u32 *psdata; 1115 u32 pkt_len; 1116 1117 /* padding enabled in hw */ 1118 pkt_len = skb_headlen(skb); 1119 1120 /* SKB TX timestamp */ 1121 if (port->tx_ts_enabled) 1122 am65_cpts_prep_tx_timestamp(common->cpts, skb); 1123 1124 q_idx = skb_get_queue_mapping(skb); 1125 dev_dbg(dev, "%s skb_queue:%d\n", __func__, q_idx); 1126 1127 tx_chn = &common->tx_chns[q_idx]; 1128 netif_txq = netdev_get_tx_queue(ndev, q_idx); 1129 1130 /* Map the linear buffer */ 1131 buf_dma = dma_map_single(tx_chn->dma_dev, skb->data, pkt_len, 1132 DMA_TO_DEVICE); 1133 if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) { 1134 dev_err(dev, "Failed to map tx skb buffer\n"); 1135 ndev->stats.tx_errors++; 1136 goto err_free_skb; 1137 } 1138 1139 first_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool); 1140 if (!first_desc) { 1141 dev_dbg(dev, "Failed to allocate descriptor\n"); 1142 dma_unmap_single(tx_chn->dma_dev, buf_dma, pkt_len, 1143 DMA_TO_DEVICE); 1144 goto busy_stop_q; 1145 } 1146 1147 cppi5_hdesc_init(first_desc, CPPI5_INFO0_HDESC_EPIB_PRESENT, 1148 AM65_CPSW_NAV_PS_DATA_SIZE); 1149 cppi5_desc_set_pktids(&first_desc->hdr, 0, 0x3FFF); 1150 cppi5_hdesc_set_pkttype(first_desc, 0x7); 1151 cppi5_desc_set_tags_ids(&first_desc->hdr, 0, port->port_id); 1152 1153 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma); 1154 cppi5_hdesc_attach_buf(first_desc, buf_dma, pkt_len, buf_dma, pkt_len); 1155 swdata = cppi5_hdesc_get_swdata(first_desc); 1156 *(swdata) = skb; 1157 psdata = cppi5_hdesc_get_psdata(first_desc); 1158 1159 /* HW csum offload if enabled */ 1160 psdata[2] = 0; 1161 if (likely(skb->ip_summed == CHECKSUM_PARTIAL)) { 1162 unsigned int cs_start, cs_offset; 1163 1164 cs_start = skb_transport_offset(skb); 1165 cs_offset = cs_start + skb->csum_offset; 1166 /* HW numerates bytes starting from 1 */ 1167 psdata[2] = ((cs_offset + 1) << 24) | 1168 ((cs_start + 1) << 16) | (skb->len - cs_start); 1169 dev_dbg(dev, "%s tx psdata:%#x\n", __func__, psdata[2]); 1170 } 1171 1172 if (!skb_is_nonlinear(skb)) 1173 goto done_tx; 1174 1175 dev_dbg(dev, "fragmented SKB\n"); 1176 1177 /* Handle the case where skb is fragmented in pages */ 1178 cur_desc = first_desc; 1179 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { 1180 skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; 1181 u32 frag_size = skb_frag_size(frag); 1182 1183 next_desc = k3_cppi_desc_pool_alloc(tx_chn->desc_pool); 1184 if (!next_desc) { 1185 dev_err(dev, "Failed to allocate descriptor\n"); 1186 goto busy_free_descs; 1187 } 1188 1189 buf_dma = skb_frag_dma_map(tx_chn->dma_dev, frag, 0, frag_size, 1190 DMA_TO_DEVICE); 1191 if (unlikely(dma_mapping_error(tx_chn->dma_dev, buf_dma))) { 1192 dev_err(dev, "Failed to map tx skb page\n"); 1193 k3_cppi_desc_pool_free(tx_chn->desc_pool, next_desc); 1194 ndev->stats.tx_errors++; 1195 goto err_free_descs; 1196 } 1197 1198 cppi5_hdesc_reset_hbdesc(next_desc); 1199 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &buf_dma); 1200 cppi5_hdesc_attach_buf(next_desc, 1201 buf_dma, frag_size, buf_dma, frag_size); 1202 1203 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, 1204 next_desc); 1205 k3_udma_glue_tx_dma_to_cppi5_addr(tx_chn->tx_chn, &desc_dma); 1206 cppi5_hdesc_link_hbdesc(cur_desc, desc_dma); 1207 1208 pkt_len += frag_size; 1209 cur_desc = next_desc; 1210 } 1211 WARN_ON(pkt_len != skb->len); 1212 1213 done_tx: 1214 skb_tx_timestamp(skb); 1215 1216 /* report bql before sending packet */ 1217 netdev_tx_sent_queue(netif_txq, pkt_len); 1218 1219 cppi5_hdesc_set_pktlen(first_desc, pkt_len); 1220 desc_dma = k3_cppi_desc_pool_virt2dma(tx_chn->desc_pool, first_desc); 1221 if (AM65_CPSW_IS_CPSW2G(common)) { 1222 ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma); 1223 } else { 1224 spin_lock_bh(&tx_chn->lock); 1225 ret = k3_udma_glue_push_tx_chn(tx_chn->tx_chn, first_desc, desc_dma); 1226 spin_unlock_bh(&tx_chn->lock); 1227 } 1228 if (ret) { 1229 dev_err(dev, "can't push desc %d\n", ret); 1230 /* inform bql */ 1231 netdev_tx_completed_queue(netif_txq, 1, pkt_len); 1232 ndev->stats.tx_errors++; 1233 goto err_free_descs; 1234 } 1235 1236 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) < MAX_SKB_FRAGS) { 1237 netif_tx_stop_queue(netif_txq); 1238 /* Barrier, so that stop_queue visible to other cpus */ 1239 smp_mb__after_atomic(); 1240 dev_dbg(dev, "netif_tx_stop_queue %d\n", q_idx); 1241 1242 /* re-check for smp */ 1243 if (k3_cppi_desc_pool_avail(tx_chn->desc_pool) >= 1244 MAX_SKB_FRAGS) { 1245 netif_tx_wake_queue(netif_txq); 1246 dev_dbg(dev, "netif_tx_wake_queue %d\n", q_idx); 1247 } 1248 } 1249 1250 return NETDEV_TX_OK; 1251 1252 err_free_descs: 1253 am65_cpsw_nuss_xmit_free(tx_chn, first_desc); 1254 err_free_skb: 1255 ndev->stats.tx_dropped++; 1256 dev_kfree_skb_any(skb); 1257 return NETDEV_TX_OK; 1258 1259 busy_free_descs: 1260 am65_cpsw_nuss_xmit_free(tx_chn, first_desc); 1261 busy_stop_q: 1262 netif_tx_stop_queue(netif_txq); 1263 return NETDEV_TX_BUSY; 1264 } 1265 1266 static int am65_cpsw_nuss_ndo_slave_set_mac_address(struct net_device *ndev, 1267 void *addr) 1268 { 1269 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 1270 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1271 struct sockaddr *sockaddr = (struct sockaddr *)addr; 1272 int ret; 1273 1274 ret = eth_prepare_mac_addr_change(ndev, addr); 1275 if (ret < 0) 1276 return ret; 1277 1278 ret = pm_runtime_resume_and_get(common->dev); 1279 if (ret < 0) 1280 return ret; 1281 1282 cpsw_ale_del_ucast(common->ale, ndev->dev_addr, 1283 HOST_PORT_NUM, 0, 0); 1284 cpsw_ale_add_ucast(common->ale, sockaddr->sa_data, 1285 HOST_PORT_NUM, ALE_SECURE, 0); 1286 1287 am65_cpsw_port_set_sl_mac(port, addr); 1288 eth_commit_mac_addr_change(ndev, sockaddr); 1289 1290 pm_runtime_put(common->dev); 1291 1292 return 0; 1293 } 1294 1295 static int am65_cpsw_nuss_hwtstamp_set(struct net_device *ndev, 1296 struct ifreq *ifr) 1297 { 1298 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 1299 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1300 u32 ts_ctrl, seq_id, ts_ctrl_ltype2, ts_vlan_ltype; 1301 struct hwtstamp_config cfg; 1302 1303 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1304 return -EOPNOTSUPP; 1305 1306 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 1307 return -EFAULT; 1308 1309 /* TX HW timestamp */ 1310 switch (cfg.tx_type) { 1311 case HWTSTAMP_TX_OFF: 1312 case HWTSTAMP_TX_ON: 1313 break; 1314 default: 1315 return -ERANGE; 1316 } 1317 1318 switch (cfg.rx_filter) { 1319 case HWTSTAMP_FILTER_NONE: 1320 port->rx_ts_enabled = false; 1321 break; 1322 case HWTSTAMP_FILTER_ALL: 1323 case HWTSTAMP_FILTER_SOME: 1324 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 1325 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 1326 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 1327 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 1328 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 1329 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 1330 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1331 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 1332 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 1333 case HWTSTAMP_FILTER_PTP_V2_EVENT: 1334 case HWTSTAMP_FILTER_PTP_V2_SYNC: 1335 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 1336 case HWTSTAMP_FILTER_NTP_ALL: 1337 port->rx_ts_enabled = true; 1338 cfg.rx_filter = HWTSTAMP_FILTER_ALL; 1339 break; 1340 default: 1341 return -ERANGE; 1342 } 1343 1344 port->tx_ts_enabled = (cfg.tx_type == HWTSTAMP_TX_ON); 1345 1346 /* cfg TX timestamp */ 1347 seq_id = (AM65_CPSW_TS_SEQ_ID_OFFSET << 1348 AM65_CPSW_PN_TS_SEQ_ID_OFFSET_SHIFT) | ETH_P_1588; 1349 1350 ts_vlan_ltype = ETH_P_8021Q; 1351 1352 ts_ctrl_ltype2 = ETH_P_1588 | 1353 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_107 | 1354 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_129 | 1355 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_130 | 1356 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_131 | 1357 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_132 | 1358 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_319 | 1359 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_320 | 1360 AM65_CPSW_PN_TS_CTL_LTYPE2_TS_TTL_NONZERO; 1361 1362 ts_ctrl = AM65_CPSW_TS_EVENT_MSG_TYPE_BITS << 1363 AM65_CPSW_PN_TS_CTL_MSG_TYPE_EN_SHIFT; 1364 1365 if (port->tx_ts_enabled) 1366 ts_ctrl |= AM65_CPSW_TS_TX_ANX_ALL_EN | 1367 AM65_CPSW_PN_TS_CTL_TX_VLAN_LT1_EN; 1368 1369 writel(seq_id, port->port_base + AM65_CPSW_PORTN_REG_TS_SEQ_LTYPE_REG); 1370 writel(ts_vlan_ltype, port->port_base + 1371 AM65_CPSW_PORTN_REG_TS_VLAN_LTYPE_REG); 1372 writel(ts_ctrl_ltype2, port->port_base + 1373 AM65_CPSW_PORTN_REG_TS_CTL_LTYPE2); 1374 writel(ts_ctrl, port->port_base + AM65_CPSW_PORTN_REG_TS_CTL); 1375 1376 /* en/dis RX timestamp */ 1377 am65_cpts_rx_enable(common->cpts, port->rx_ts_enabled); 1378 1379 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1380 } 1381 1382 static int am65_cpsw_nuss_hwtstamp_get(struct net_device *ndev, 1383 struct ifreq *ifr) 1384 { 1385 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1386 struct hwtstamp_config cfg; 1387 1388 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 1389 return -EOPNOTSUPP; 1390 1391 cfg.flags = 0; 1392 cfg.tx_type = port->tx_ts_enabled ? 1393 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 1394 cfg.rx_filter = port->rx_ts_enabled ? 1395 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE; 1396 1397 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1398 } 1399 1400 static int am65_cpsw_nuss_ndo_slave_ioctl(struct net_device *ndev, 1401 struct ifreq *req, int cmd) 1402 { 1403 struct am65_cpsw_port *port = am65_ndev_to_port(ndev); 1404 1405 if (!netif_running(ndev)) 1406 return -EINVAL; 1407 1408 switch (cmd) { 1409 case SIOCSHWTSTAMP: 1410 return am65_cpsw_nuss_hwtstamp_set(ndev, req); 1411 case SIOCGHWTSTAMP: 1412 return am65_cpsw_nuss_hwtstamp_get(ndev, req); 1413 } 1414 1415 return phylink_mii_ioctl(port->slave.phylink, req, cmd); 1416 } 1417 1418 static void am65_cpsw_nuss_ndo_get_stats(struct net_device *dev, 1419 struct rtnl_link_stats64 *stats) 1420 { 1421 struct am65_cpsw_ndev_priv *ndev_priv = netdev_priv(dev); 1422 unsigned int start; 1423 int cpu; 1424 1425 for_each_possible_cpu(cpu) { 1426 struct am65_cpsw_ndev_stats *cpu_stats; 1427 u64 rx_packets; 1428 u64 rx_bytes; 1429 u64 tx_packets; 1430 u64 tx_bytes; 1431 1432 cpu_stats = per_cpu_ptr(ndev_priv->stats, cpu); 1433 do { 1434 start = u64_stats_fetch_begin(&cpu_stats->syncp); 1435 rx_packets = cpu_stats->rx_packets; 1436 rx_bytes = cpu_stats->rx_bytes; 1437 tx_packets = cpu_stats->tx_packets; 1438 tx_bytes = cpu_stats->tx_bytes; 1439 } while (u64_stats_fetch_retry(&cpu_stats->syncp, start)); 1440 1441 stats->rx_packets += rx_packets; 1442 stats->rx_bytes += rx_bytes; 1443 stats->tx_packets += tx_packets; 1444 stats->tx_bytes += tx_bytes; 1445 } 1446 1447 stats->rx_errors = dev->stats.rx_errors; 1448 stats->rx_dropped = dev->stats.rx_dropped; 1449 stats->tx_dropped = dev->stats.tx_dropped; 1450 } 1451 1452 static const struct net_device_ops am65_cpsw_nuss_netdev_ops = { 1453 .ndo_open = am65_cpsw_nuss_ndo_slave_open, 1454 .ndo_stop = am65_cpsw_nuss_ndo_slave_stop, 1455 .ndo_start_xmit = am65_cpsw_nuss_ndo_slave_xmit, 1456 .ndo_set_rx_mode = am65_cpsw_nuss_ndo_slave_set_rx_mode, 1457 .ndo_get_stats64 = am65_cpsw_nuss_ndo_get_stats, 1458 .ndo_validate_addr = eth_validate_addr, 1459 .ndo_set_mac_address = am65_cpsw_nuss_ndo_slave_set_mac_address, 1460 .ndo_tx_timeout = am65_cpsw_nuss_ndo_host_tx_timeout, 1461 .ndo_vlan_rx_add_vid = am65_cpsw_nuss_ndo_slave_add_vid, 1462 .ndo_vlan_rx_kill_vid = am65_cpsw_nuss_ndo_slave_kill_vid, 1463 .ndo_eth_ioctl = am65_cpsw_nuss_ndo_slave_ioctl, 1464 .ndo_setup_tc = am65_cpsw_qos_ndo_setup_tc, 1465 .ndo_set_tx_maxrate = am65_cpsw_qos_ndo_tx_p0_set_maxrate, 1466 }; 1467 1468 static void am65_cpsw_disable_phy(struct phy *phy) 1469 { 1470 phy_power_off(phy); 1471 phy_exit(phy); 1472 } 1473 1474 static int am65_cpsw_enable_phy(struct phy *phy) 1475 { 1476 int ret; 1477 1478 ret = phy_init(phy); 1479 if (ret < 0) 1480 return ret; 1481 1482 ret = phy_power_on(phy); 1483 if (ret < 0) { 1484 phy_exit(phy); 1485 return ret; 1486 } 1487 1488 return 0; 1489 } 1490 1491 static void am65_cpsw_disable_serdes_phy(struct am65_cpsw_common *common) 1492 { 1493 struct am65_cpsw_port *port; 1494 struct phy *phy; 1495 int i; 1496 1497 for (i = 0; i < common->port_num; i++) { 1498 port = &common->ports[i]; 1499 phy = port->slave.serdes_phy; 1500 if (phy) 1501 am65_cpsw_disable_phy(phy); 1502 } 1503 } 1504 1505 static int am65_cpsw_init_serdes_phy(struct device *dev, struct device_node *port_np, 1506 struct am65_cpsw_port *port) 1507 { 1508 const char *name = "serdes"; 1509 struct phy *phy; 1510 int ret; 1511 1512 phy = devm_of_phy_optional_get(dev, port_np, name); 1513 if (IS_ERR_OR_NULL(phy)) 1514 return PTR_ERR_OR_ZERO(phy); 1515 1516 /* Serdes PHY exists. Store it. */ 1517 port->slave.serdes_phy = phy; 1518 1519 ret = am65_cpsw_enable_phy(phy); 1520 if (ret < 0) 1521 goto err_phy; 1522 1523 return 0; 1524 1525 err_phy: 1526 devm_phy_put(dev, phy); 1527 return ret; 1528 } 1529 1530 static void am65_cpsw_nuss_mac_config(struct phylink_config *config, unsigned int mode, 1531 const struct phylink_link_state *state) 1532 { 1533 struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 1534 phylink_config); 1535 struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 1536 struct am65_cpsw_common *common = port->common; 1537 1538 if (common->pdata.extra_modes & BIT(state->interface)) { 1539 if (state->interface == PHY_INTERFACE_MODE_SGMII) { 1540 writel(ADVERTISE_SGMII, 1541 port->sgmii_base + AM65_CPSW_SGMII_MR_ADV_ABILITY_REG); 1542 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_EXT_EN); 1543 } else { 1544 cpsw_sl_ctl_clr(port->slave.mac_sl, CPSW_SL_CTL_EXT_EN); 1545 } 1546 1547 if (state->interface == PHY_INTERFACE_MODE_USXGMII) { 1548 cpsw_sl_ctl_set(port->slave.mac_sl, 1549 CPSW_SL_CTL_XGIG | CPSW_SL_CTL_XGMII_EN); 1550 } else { 1551 cpsw_sl_ctl_clr(port->slave.mac_sl, 1552 CPSW_SL_CTL_XGIG | CPSW_SL_CTL_XGMII_EN); 1553 } 1554 1555 writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE, 1556 port->sgmii_base + AM65_CPSW_SGMII_CONTROL_REG); 1557 } 1558 } 1559 1560 static void am65_cpsw_nuss_mac_link_down(struct phylink_config *config, unsigned int mode, 1561 phy_interface_t interface) 1562 { 1563 struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 1564 phylink_config); 1565 struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 1566 struct am65_cpsw_common *common = port->common; 1567 struct net_device *ndev = port->ndev; 1568 u32 mac_control; 1569 int tmo; 1570 1571 /* disable forwarding */ 1572 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 1573 1574 cpsw_sl_ctl_set(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE); 1575 1576 tmo = cpsw_sl_wait_for_idle(port->slave.mac_sl, 100); 1577 dev_dbg(common->dev, "down msc_sl %08x tmo %d\n", 1578 cpsw_sl_reg_read(port->slave.mac_sl, CPSW_SL_MACSTATUS), tmo); 1579 1580 /* All the bits that am65_cpsw_nuss_mac_link_up() can possibly set */ 1581 mac_control = CPSW_SL_CTL_GMII_EN | CPSW_SL_CTL_GIG | CPSW_SL_CTL_IFCTL_A | 1582 CPSW_SL_CTL_FULLDUPLEX | CPSW_SL_CTL_RX_FLOW_EN | CPSW_SL_CTL_TX_FLOW_EN; 1583 /* If interface mode is RGMII, CPSW_SL_CTL_EXT_EN might have been set for 10 Mbps */ 1584 if (phy_interface_mode_is_rgmii(interface)) 1585 mac_control |= CPSW_SL_CTL_EXT_EN; 1586 /* Only clear those bits that can be set by am65_cpsw_nuss_mac_link_up() */ 1587 cpsw_sl_ctl_clr(port->slave.mac_sl, mac_control); 1588 1589 am65_cpsw_qos_link_down(ndev); 1590 netif_tx_stop_all_queues(ndev); 1591 } 1592 1593 static void am65_cpsw_nuss_mac_link_up(struct phylink_config *config, struct phy_device *phy, 1594 unsigned int mode, phy_interface_t interface, int speed, 1595 int duplex, bool tx_pause, bool rx_pause) 1596 { 1597 struct am65_cpsw_slave_data *slave = container_of(config, struct am65_cpsw_slave_data, 1598 phylink_config); 1599 struct am65_cpsw_port *port = container_of(slave, struct am65_cpsw_port, slave); 1600 struct am65_cpsw_common *common = port->common; 1601 u32 mac_control = CPSW_SL_CTL_GMII_EN; 1602 struct net_device *ndev = port->ndev; 1603 1604 /* Bring the port out of idle state */ 1605 cpsw_sl_ctl_clr(port->slave.mac_sl, CPSW_SL_CTL_CMD_IDLE); 1606 1607 if (speed == SPEED_1000) 1608 mac_control |= CPSW_SL_CTL_GIG; 1609 /* TODO: Verify whether in-band is necessary for 10 Mbps RGMII */ 1610 if (speed == SPEED_10 && phy_interface_mode_is_rgmii(interface)) 1611 /* Can be used with in band mode only */ 1612 mac_control |= CPSW_SL_CTL_EXT_EN; 1613 if (speed == SPEED_100 && interface == PHY_INTERFACE_MODE_RMII) 1614 mac_control |= CPSW_SL_CTL_IFCTL_A; 1615 if (duplex) 1616 mac_control |= CPSW_SL_CTL_FULLDUPLEX; 1617 1618 /* rx_pause/tx_pause */ 1619 if (rx_pause) 1620 mac_control |= CPSW_SL_CTL_TX_FLOW_EN; 1621 1622 if (tx_pause) 1623 mac_control |= CPSW_SL_CTL_RX_FLOW_EN; 1624 1625 cpsw_sl_ctl_set(port->slave.mac_sl, mac_control); 1626 1627 /* enable forwarding */ 1628 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 1629 1630 am65_cpsw_qos_link_up(ndev, speed); 1631 netif_tx_wake_all_queues(ndev); 1632 } 1633 1634 static const struct phylink_mac_ops am65_cpsw_phylink_mac_ops = { 1635 .mac_config = am65_cpsw_nuss_mac_config, 1636 .mac_link_down = am65_cpsw_nuss_mac_link_down, 1637 .mac_link_up = am65_cpsw_nuss_mac_link_up, 1638 }; 1639 1640 static void am65_cpsw_nuss_slave_disable_unused(struct am65_cpsw_port *port) 1641 { 1642 struct am65_cpsw_common *common = port->common; 1643 1644 if (!port->disabled) 1645 return; 1646 1647 cpsw_ale_control_set(common->ale, port->port_id, 1648 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 1649 1650 cpsw_sl_reset(port->slave.mac_sl, 100); 1651 cpsw_sl_ctl_reset(port->slave.mac_sl); 1652 } 1653 1654 static void am65_cpsw_nuss_free_tx_chns(void *data) 1655 { 1656 struct am65_cpsw_common *common = data; 1657 int i; 1658 1659 for (i = 0; i < common->tx_ch_num; i++) { 1660 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 1661 1662 if (!IS_ERR_OR_NULL(tx_chn->desc_pool)) 1663 k3_cppi_desc_pool_destroy(tx_chn->desc_pool); 1664 1665 if (!IS_ERR_OR_NULL(tx_chn->tx_chn)) 1666 k3_udma_glue_release_tx_chn(tx_chn->tx_chn); 1667 1668 memset(tx_chn, 0, sizeof(*tx_chn)); 1669 } 1670 } 1671 1672 void am65_cpsw_nuss_remove_tx_chns(struct am65_cpsw_common *common) 1673 { 1674 struct device *dev = common->dev; 1675 int i; 1676 1677 devm_remove_action(dev, am65_cpsw_nuss_free_tx_chns, common); 1678 1679 common->tx_ch_rate_msk = 0; 1680 for (i = 0; i < common->tx_ch_num; i++) { 1681 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 1682 1683 if (tx_chn->irq) 1684 devm_free_irq(dev, tx_chn->irq, tx_chn); 1685 1686 netif_napi_del(&tx_chn->napi_tx); 1687 1688 if (!IS_ERR_OR_NULL(tx_chn->desc_pool)) 1689 k3_cppi_desc_pool_destroy(tx_chn->desc_pool); 1690 1691 if (!IS_ERR_OR_NULL(tx_chn->tx_chn)) 1692 k3_udma_glue_release_tx_chn(tx_chn->tx_chn); 1693 1694 memset(tx_chn, 0, sizeof(*tx_chn)); 1695 } 1696 } 1697 1698 static int am65_cpsw_nuss_ndev_add_tx_napi(struct am65_cpsw_common *common) 1699 { 1700 struct device *dev = common->dev; 1701 int i, ret = 0; 1702 1703 for (i = 0; i < common->tx_ch_num; i++) { 1704 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 1705 1706 netif_napi_add_tx(common->dma_ndev, &tx_chn->napi_tx, 1707 am65_cpsw_nuss_tx_poll); 1708 1709 ret = devm_request_irq(dev, tx_chn->irq, 1710 am65_cpsw_nuss_tx_irq, 1711 IRQF_TRIGGER_HIGH, 1712 tx_chn->tx_chn_name, tx_chn); 1713 if (ret) { 1714 dev_err(dev, "failure requesting tx%u irq %u, %d\n", 1715 tx_chn->id, tx_chn->irq, ret); 1716 goto err; 1717 } 1718 } 1719 1720 err: 1721 return ret; 1722 } 1723 1724 static int am65_cpsw_nuss_init_tx_chns(struct am65_cpsw_common *common) 1725 { 1726 u32 max_desc_num = ALIGN(AM65_CPSW_MAX_TX_DESC, MAX_SKB_FRAGS); 1727 struct k3_udma_glue_tx_channel_cfg tx_cfg = { 0 }; 1728 struct device *dev = common->dev; 1729 struct k3_ring_cfg ring_cfg = { 1730 .elm_size = K3_RINGACC_RING_ELSIZE_8, 1731 .mode = K3_RINGACC_RING_MODE_RING, 1732 .flags = 0 1733 }; 1734 u32 hdesc_size; 1735 int i, ret = 0; 1736 1737 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE, 1738 AM65_CPSW_NAV_SW_DATA_SIZE); 1739 1740 tx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE; 1741 tx_cfg.tx_cfg = ring_cfg; 1742 tx_cfg.txcq_cfg = ring_cfg; 1743 tx_cfg.tx_cfg.size = max_desc_num; 1744 tx_cfg.txcq_cfg.size = max_desc_num; 1745 1746 for (i = 0; i < common->tx_ch_num; i++) { 1747 struct am65_cpsw_tx_chn *tx_chn = &common->tx_chns[i]; 1748 1749 snprintf(tx_chn->tx_chn_name, 1750 sizeof(tx_chn->tx_chn_name), "tx%d", i); 1751 1752 spin_lock_init(&tx_chn->lock); 1753 tx_chn->common = common; 1754 tx_chn->id = i; 1755 tx_chn->descs_num = max_desc_num; 1756 1757 tx_chn->tx_chn = 1758 k3_udma_glue_request_tx_chn(dev, 1759 tx_chn->tx_chn_name, 1760 &tx_cfg); 1761 if (IS_ERR(tx_chn->tx_chn)) { 1762 ret = dev_err_probe(dev, PTR_ERR(tx_chn->tx_chn), 1763 "Failed to request tx dma channel\n"); 1764 goto err; 1765 } 1766 tx_chn->dma_dev = k3_udma_glue_tx_get_dma_device(tx_chn->tx_chn); 1767 1768 tx_chn->desc_pool = k3_cppi_desc_pool_create_name(tx_chn->dma_dev, 1769 tx_chn->descs_num, 1770 hdesc_size, 1771 tx_chn->tx_chn_name); 1772 if (IS_ERR(tx_chn->desc_pool)) { 1773 ret = PTR_ERR(tx_chn->desc_pool); 1774 dev_err(dev, "Failed to create poll %d\n", ret); 1775 goto err; 1776 } 1777 1778 tx_chn->irq = k3_udma_glue_tx_get_irq(tx_chn->tx_chn); 1779 if (tx_chn->irq < 0) { 1780 dev_err(dev, "Failed to get tx dma irq %d\n", 1781 tx_chn->irq); 1782 ret = tx_chn->irq; 1783 goto err; 1784 } 1785 1786 snprintf(tx_chn->tx_chn_name, 1787 sizeof(tx_chn->tx_chn_name), "%s-tx%d", 1788 dev_name(dev), tx_chn->id); 1789 } 1790 1791 ret = am65_cpsw_nuss_ndev_add_tx_napi(common); 1792 if (ret) { 1793 dev_err(dev, "Failed to add tx NAPI %d\n", ret); 1794 goto err; 1795 } 1796 1797 err: 1798 i = devm_add_action(dev, am65_cpsw_nuss_free_tx_chns, common); 1799 if (i) { 1800 dev_err(dev, "Failed to add free_tx_chns action %d\n", i); 1801 return i; 1802 } 1803 1804 return ret; 1805 } 1806 1807 static void am65_cpsw_nuss_free_rx_chns(void *data) 1808 { 1809 struct am65_cpsw_common *common = data; 1810 struct am65_cpsw_rx_chn *rx_chn; 1811 1812 rx_chn = &common->rx_chns; 1813 1814 if (!IS_ERR_OR_NULL(rx_chn->desc_pool)) 1815 k3_cppi_desc_pool_destroy(rx_chn->desc_pool); 1816 1817 if (!IS_ERR_OR_NULL(rx_chn->rx_chn)) 1818 k3_udma_glue_release_rx_chn(rx_chn->rx_chn); 1819 } 1820 1821 static void am65_cpsw_nuss_remove_rx_chns(void *data) 1822 { 1823 struct am65_cpsw_common *common = data; 1824 struct am65_cpsw_rx_chn *rx_chn; 1825 struct device *dev = common->dev; 1826 1827 rx_chn = &common->rx_chns; 1828 devm_remove_action(dev, am65_cpsw_nuss_free_rx_chns, common); 1829 1830 if (!(rx_chn->irq < 0)) 1831 devm_free_irq(dev, rx_chn->irq, common); 1832 1833 netif_napi_del(&common->napi_rx); 1834 1835 if (!IS_ERR_OR_NULL(rx_chn->desc_pool)) 1836 k3_cppi_desc_pool_destroy(rx_chn->desc_pool); 1837 1838 if (!IS_ERR_OR_NULL(rx_chn->rx_chn)) 1839 k3_udma_glue_release_rx_chn(rx_chn->rx_chn); 1840 1841 common->rx_flow_id_base = -1; 1842 } 1843 1844 static int am65_cpsw_nuss_init_rx_chns(struct am65_cpsw_common *common) 1845 { 1846 struct am65_cpsw_rx_chn *rx_chn = &common->rx_chns; 1847 struct k3_udma_glue_rx_channel_cfg rx_cfg = { 0 }; 1848 u32 max_desc_num = AM65_CPSW_MAX_RX_DESC; 1849 struct device *dev = common->dev; 1850 u32 hdesc_size; 1851 u32 fdqring_id; 1852 int i, ret = 0; 1853 1854 hdesc_size = cppi5_hdesc_calc_size(true, AM65_CPSW_NAV_PS_DATA_SIZE, 1855 AM65_CPSW_NAV_SW_DATA_SIZE); 1856 1857 rx_cfg.swdata_size = AM65_CPSW_NAV_SW_DATA_SIZE; 1858 rx_cfg.flow_id_num = AM65_CPSW_MAX_RX_FLOWS; 1859 rx_cfg.flow_id_base = common->rx_flow_id_base; 1860 1861 /* init all flows */ 1862 rx_chn->dev = dev; 1863 rx_chn->descs_num = max_desc_num; 1864 1865 rx_chn->rx_chn = k3_udma_glue_request_rx_chn(dev, "rx", &rx_cfg); 1866 if (IS_ERR(rx_chn->rx_chn)) { 1867 ret = dev_err_probe(dev, PTR_ERR(rx_chn->rx_chn), 1868 "Failed to request rx dma channel\n"); 1869 goto err; 1870 } 1871 rx_chn->dma_dev = k3_udma_glue_rx_get_dma_device(rx_chn->rx_chn); 1872 1873 rx_chn->desc_pool = k3_cppi_desc_pool_create_name(rx_chn->dma_dev, 1874 rx_chn->descs_num, 1875 hdesc_size, "rx"); 1876 if (IS_ERR(rx_chn->desc_pool)) { 1877 ret = PTR_ERR(rx_chn->desc_pool); 1878 dev_err(dev, "Failed to create rx poll %d\n", ret); 1879 goto err; 1880 } 1881 1882 common->rx_flow_id_base = 1883 k3_udma_glue_rx_get_flow_id_base(rx_chn->rx_chn); 1884 dev_info(dev, "set new flow-id-base %u\n", common->rx_flow_id_base); 1885 1886 fdqring_id = K3_RINGACC_RING_ID_ANY; 1887 for (i = 0; i < rx_cfg.flow_id_num; i++) { 1888 struct k3_ring_cfg rxring_cfg = { 1889 .elm_size = K3_RINGACC_RING_ELSIZE_8, 1890 .mode = K3_RINGACC_RING_MODE_RING, 1891 .flags = 0, 1892 }; 1893 struct k3_ring_cfg fdqring_cfg = { 1894 .elm_size = K3_RINGACC_RING_ELSIZE_8, 1895 .flags = K3_RINGACC_RING_SHARED, 1896 }; 1897 struct k3_udma_glue_rx_flow_cfg rx_flow_cfg = { 1898 .rx_cfg = rxring_cfg, 1899 .rxfdq_cfg = fdqring_cfg, 1900 .ring_rxq_id = K3_RINGACC_RING_ID_ANY, 1901 .src_tag_lo_sel = 1902 K3_UDMA_GLUE_SRC_TAG_LO_USE_REMOTE_SRC_TAG, 1903 }; 1904 1905 rx_flow_cfg.ring_rxfdq0_id = fdqring_id; 1906 rx_flow_cfg.rx_cfg.size = max_desc_num; 1907 rx_flow_cfg.rxfdq_cfg.size = max_desc_num; 1908 rx_flow_cfg.rxfdq_cfg.mode = common->pdata.fdqring_mode; 1909 1910 ret = k3_udma_glue_rx_flow_init(rx_chn->rx_chn, 1911 i, &rx_flow_cfg); 1912 if (ret) { 1913 dev_err(dev, "Failed to init rx flow%d %d\n", i, ret); 1914 goto err; 1915 } 1916 if (!i) 1917 fdqring_id = 1918 k3_udma_glue_rx_flow_get_fdq_id(rx_chn->rx_chn, 1919 i); 1920 1921 rx_chn->irq = k3_udma_glue_rx_get_irq(rx_chn->rx_chn, i); 1922 1923 if (rx_chn->irq <= 0) { 1924 dev_err(dev, "Failed to get rx dma irq %d\n", 1925 rx_chn->irq); 1926 ret = -ENXIO; 1927 goto err; 1928 } 1929 } 1930 1931 netif_napi_add(common->dma_ndev, &common->napi_rx, 1932 am65_cpsw_nuss_rx_poll); 1933 1934 ret = devm_request_irq(dev, rx_chn->irq, 1935 am65_cpsw_nuss_rx_irq, 1936 IRQF_TRIGGER_HIGH, dev_name(dev), common); 1937 if (ret) { 1938 dev_err(dev, "failure requesting rx irq %u, %d\n", 1939 rx_chn->irq, ret); 1940 goto err; 1941 } 1942 1943 err: 1944 i = devm_add_action(dev, am65_cpsw_nuss_free_rx_chns, common); 1945 if (i) { 1946 dev_err(dev, "Failed to add free_rx_chns action %d\n", i); 1947 return i; 1948 } 1949 1950 return ret; 1951 } 1952 1953 static int am65_cpsw_nuss_init_host_p(struct am65_cpsw_common *common) 1954 { 1955 struct am65_cpsw_host *host_p = am65_common_get_host(common); 1956 1957 host_p->common = common; 1958 host_p->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE; 1959 host_p->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE; 1960 1961 return 0; 1962 } 1963 1964 static int am65_cpsw_am654_get_efuse_macid(struct device_node *of_node, 1965 int slave, u8 *mac_addr) 1966 { 1967 u32 mac_lo, mac_hi, offset; 1968 struct regmap *syscon; 1969 int ret; 1970 1971 syscon = syscon_regmap_lookup_by_phandle(of_node, "ti,syscon-efuse"); 1972 if (IS_ERR(syscon)) { 1973 if (PTR_ERR(syscon) == -ENODEV) 1974 return 0; 1975 return PTR_ERR(syscon); 1976 } 1977 1978 ret = of_property_read_u32_index(of_node, "ti,syscon-efuse", 1, 1979 &offset); 1980 if (ret) 1981 return ret; 1982 1983 regmap_read(syscon, offset, &mac_lo); 1984 regmap_read(syscon, offset + 4, &mac_hi); 1985 1986 mac_addr[0] = (mac_hi >> 8) & 0xff; 1987 mac_addr[1] = mac_hi & 0xff; 1988 mac_addr[2] = (mac_lo >> 24) & 0xff; 1989 mac_addr[3] = (mac_lo >> 16) & 0xff; 1990 mac_addr[4] = (mac_lo >> 8) & 0xff; 1991 mac_addr[5] = mac_lo & 0xff; 1992 1993 return 0; 1994 } 1995 1996 static int am65_cpsw_init_cpts(struct am65_cpsw_common *common) 1997 { 1998 struct device *dev = common->dev; 1999 struct device_node *node; 2000 struct am65_cpts *cpts; 2001 void __iomem *reg_base; 2002 2003 if (!IS_ENABLED(CONFIG_TI_K3_AM65_CPTS)) 2004 return 0; 2005 2006 node = of_get_child_by_name(dev->of_node, "cpts"); 2007 if (!node) { 2008 dev_err(dev, "%s cpts not found\n", __func__); 2009 return -ENOENT; 2010 } 2011 2012 reg_base = common->cpsw_base + AM65_CPSW_NU_CPTS_BASE; 2013 cpts = am65_cpts_create(dev, reg_base, node); 2014 if (IS_ERR(cpts)) { 2015 int ret = PTR_ERR(cpts); 2016 2017 of_node_put(node); 2018 dev_err(dev, "cpts create err %d\n", ret); 2019 return ret; 2020 } 2021 common->cpts = cpts; 2022 /* Forbid PM runtime if CPTS is running. 2023 * K3 CPSWxG modules may completely lose context during ON->OFF 2024 * transitions depending on integration. 2025 * AM65x/J721E MCU CPSW2G: false 2026 * J721E MAIN_CPSW9G: true 2027 */ 2028 pm_runtime_forbid(dev); 2029 2030 return 0; 2031 } 2032 2033 static int am65_cpsw_nuss_init_slave_ports(struct am65_cpsw_common *common) 2034 { 2035 struct device_node *node, *port_np; 2036 struct device *dev = common->dev; 2037 int ret; 2038 2039 node = of_get_child_by_name(dev->of_node, "ethernet-ports"); 2040 if (!node) 2041 return -ENOENT; 2042 2043 for_each_child_of_node(node, port_np) { 2044 struct am65_cpsw_port *port; 2045 u32 port_id; 2046 2047 /* it is not a slave port node, continue */ 2048 if (strcmp(port_np->name, "port")) 2049 continue; 2050 2051 ret = of_property_read_u32(port_np, "reg", &port_id); 2052 if (ret < 0) { 2053 dev_err(dev, "%pOF error reading port_id %d\n", 2054 port_np, ret); 2055 goto of_node_put; 2056 } 2057 2058 if (!port_id || port_id > common->port_num) { 2059 dev_err(dev, "%pOF has invalid port_id %u %s\n", 2060 port_np, port_id, port_np->name); 2061 ret = -EINVAL; 2062 goto of_node_put; 2063 } 2064 2065 port = am65_common_get_port(common, port_id); 2066 port->port_id = port_id; 2067 port->common = common; 2068 port->port_base = common->cpsw_base + AM65_CPSW_NU_PORTS_BASE + 2069 AM65_CPSW_NU_PORTS_OFFSET * (port_id); 2070 if (common->pdata.extra_modes) 2071 port->sgmii_base = common->ss_base + AM65_CPSW_SGMII_BASE * (port_id); 2072 port->stat_base = common->cpsw_base + AM65_CPSW_NU_STATS_BASE + 2073 (AM65_CPSW_NU_STATS_PORT_OFFSET * port_id); 2074 port->name = of_get_property(port_np, "label", NULL); 2075 port->fetch_ram_base = 2076 common->cpsw_base + AM65_CPSW_NU_FRAM_BASE + 2077 (AM65_CPSW_NU_FRAM_PORT_OFFSET * (port_id - 1)); 2078 2079 port->slave.mac_sl = cpsw_sl_get("am65", dev, port->port_base); 2080 if (IS_ERR(port->slave.mac_sl)) { 2081 ret = PTR_ERR(port->slave.mac_sl); 2082 goto of_node_put; 2083 } 2084 2085 port->disabled = !of_device_is_available(port_np); 2086 if (port->disabled) { 2087 common->disabled_ports_mask |= BIT(port->port_id); 2088 continue; 2089 } 2090 2091 port->slave.ifphy = devm_of_phy_get(dev, port_np, NULL); 2092 if (IS_ERR(port->slave.ifphy)) { 2093 ret = PTR_ERR(port->slave.ifphy); 2094 dev_err(dev, "%pOF error retrieving port phy: %d\n", 2095 port_np, ret); 2096 goto of_node_put; 2097 } 2098 2099 /* Initialize the Serdes PHY for the port */ 2100 ret = am65_cpsw_init_serdes_phy(dev, port_np, port); 2101 if (ret) 2102 goto of_node_put; 2103 2104 port->slave.mac_only = 2105 of_property_read_bool(port_np, "ti,mac-only"); 2106 2107 /* get phy/link info */ 2108 port->slave.phy_node = port_np; 2109 ret = of_get_phy_mode(port_np, &port->slave.phy_if); 2110 if (ret) { 2111 dev_err(dev, "%pOF read phy-mode err %d\n", 2112 port_np, ret); 2113 goto of_node_put; 2114 } 2115 2116 ret = phy_set_mode_ext(port->slave.ifphy, PHY_MODE_ETHERNET, port->slave.phy_if); 2117 if (ret) 2118 goto of_node_put; 2119 2120 ret = of_get_mac_address(port_np, port->slave.mac_addr); 2121 if (ret) { 2122 am65_cpsw_am654_get_efuse_macid(port_np, 2123 port->port_id, 2124 port->slave.mac_addr); 2125 if (!is_valid_ether_addr(port->slave.mac_addr)) { 2126 eth_random_addr(port->slave.mac_addr); 2127 dev_err(dev, "Use random MAC address\n"); 2128 } 2129 } 2130 } 2131 of_node_put(node); 2132 2133 /* is there at least one ext.port */ 2134 if (!(~common->disabled_ports_mask & GENMASK(common->port_num, 1))) { 2135 dev_err(dev, "No Ext. port are available\n"); 2136 return -ENODEV; 2137 } 2138 2139 return 0; 2140 2141 of_node_put: 2142 of_node_put(port_np); 2143 of_node_put(node); 2144 return ret; 2145 } 2146 2147 static void am65_cpsw_pcpu_stats_free(void *data) 2148 { 2149 struct am65_cpsw_ndev_stats __percpu *stats = data; 2150 2151 free_percpu(stats); 2152 } 2153 2154 static void am65_cpsw_nuss_phylink_cleanup(struct am65_cpsw_common *common) 2155 { 2156 struct am65_cpsw_port *port; 2157 int i; 2158 2159 for (i = 0; i < common->port_num; i++) { 2160 port = &common->ports[i]; 2161 if (port->slave.phylink) 2162 phylink_destroy(port->slave.phylink); 2163 } 2164 } 2165 2166 static int 2167 am65_cpsw_nuss_init_port_ndev(struct am65_cpsw_common *common, u32 port_idx) 2168 { 2169 struct am65_cpsw_ndev_priv *ndev_priv; 2170 struct device *dev = common->dev; 2171 struct am65_cpsw_port *port; 2172 struct phylink *phylink; 2173 int ret; 2174 2175 port = &common->ports[port_idx]; 2176 2177 if (port->disabled) 2178 return 0; 2179 2180 /* alloc netdev */ 2181 port->ndev = devm_alloc_etherdev_mqs(common->dev, 2182 sizeof(struct am65_cpsw_ndev_priv), 2183 AM65_CPSW_MAX_TX_QUEUES, 2184 AM65_CPSW_MAX_RX_QUEUES); 2185 if (!port->ndev) { 2186 dev_err(dev, "error allocating slave net_device %u\n", 2187 port->port_id); 2188 return -ENOMEM; 2189 } 2190 2191 ndev_priv = netdev_priv(port->ndev); 2192 ndev_priv->port = port; 2193 ndev_priv->msg_enable = AM65_CPSW_DEBUG; 2194 SET_NETDEV_DEV(port->ndev, dev); 2195 2196 eth_hw_addr_set(port->ndev, port->slave.mac_addr); 2197 2198 port->ndev->min_mtu = AM65_CPSW_MIN_PACKET_SIZE; 2199 port->ndev->max_mtu = AM65_CPSW_MAX_PACKET_SIZE; 2200 port->ndev->hw_features = NETIF_F_SG | 2201 NETIF_F_RXCSUM | 2202 NETIF_F_HW_CSUM | 2203 NETIF_F_HW_TC; 2204 port->ndev->features = port->ndev->hw_features | 2205 NETIF_F_HW_VLAN_CTAG_FILTER; 2206 port->ndev->vlan_features |= NETIF_F_SG; 2207 port->ndev->netdev_ops = &am65_cpsw_nuss_netdev_ops; 2208 port->ndev->ethtool_ops = &am65_cpsw_ethtool_ops_slave; 2209 2210 /* Configuring Phylink */ 2211 port->slave.phylink_config.dev = &port->ndev->dev; 2212 port->slave.phylink_config.type = PHYLINK_NETDEV; 2213 port->slave.phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_10 | MAC_100 | 2214 MAC_1000FD | MAC_5000FD; 2215 port->slave.phylink_config.mac_managed_pm = true; /* MAC does PM */ 2216 2217 switch (port->slave.phy_if) { 2218 case PHY_INTERFACE_MODE_RGMII: 2219 case PHY_INTERFACE_MODE_RGMII_ID: 2220 case PHY_INTERFACE_MODE_RGMII_RXID: 2221 case PHY_INTERFACE_MODE_RGMII_TXID: 2222 phy_interface_set_rgmii(port->slave.phylink_config.supported_interfaces); 2223 break; 2224 2225 case PHY_INTERFACE_MODE_RMII: 2226 __set_bit(PHY_INTERFACE_MODE_RMII, 2227 port->slave.phylink_config.supported_interfaces); 2228 break; 2229 2230 case PHY_INTERFACE_MODE_QSGMII: 2231 case PHY_INTERFACE_MODE_SGMII: 2232 case PHY_INTERFACE_MODE_USXGMII: 2233 if (common->pdata.extra_modes & BIT(port->slave.phy_if)) { 2234 __set_bit(port->slave.phy_if, 2235 port->slave.phylink_config.supported_interfaces); 2236 } else { 2237 dev_err(dev, "selected phy-mode is not supported\n"); 2238 return -EOPNOTSUPP; 2239 } 2240 break; 2241 2242 default: 2243 dev_err(dev, "selected phy-mode is not supported\n"); 2244 return -EOPNOTSUPP; 2245 } 2246 2247 phylink = phylink_create(&port->slave.phylink_config, 2248 of_node_to_fwnode(port->slave.phy_node), 2249 port->slave.phy_if, 2250 &am65_cpsw_phylink_mac_ops); 2251 if (IS_ERR(phylink)) 2252 return PTR_ERR(phylink); 2253 2254 port->slave.phylink = phylink; 2255 2256 /* Disable TX checksum offload by default due to HW bug */ 2257 if (common->pdata.quirks & AM65_CPSW_QUIRK_I2027_NO_TX_CSUM) 2258 port->ndev->features &= ~NETIF_F_HW_CSUM; 2259 2260 ndev_priv->stats = netdev_alloc_pcpu_stats(struct am65_cpsw_ndev_stats); 2261 if (!ndev_priv->stats) 2262 return -ENOMEM; 2263 2264 ret = devm_add_action_or_reset(dev, am65_cpsw_pcpu_stats_free, 2265 ndev_priv->stats); 2266 if (ret) 2267 dev_err(dev, "failed to add percpu stat free action %d\n", ret); 2268 2269 if (!common->dma_ndev) 2270 common->dma_ndev = port->ndev; 2271 2272 return ret; 2273 } 2274 2275 static int am65_cpsw_nuss_init_ndevs(struct am65_cpsw_common *common) 2276 { 2277 int ret; 2278 int i; 2279 2280 for (i = 0; i < common->port_num; i++) { 2281 ret = am65_cpsw_nuss_init_port_ndev(common, i); 2282 if (ret) 2283 return ret; 2284 } 2285 2286 return ret; 2287 } 2288 2289 static void am65_cpsw_nuss_cleanup_ndev(struct am65_cpsw_common *common) 2290 { 2291 struct am65_cpsw_port *port; 2292 int i; 2293 2294 for (i = 0; i < common->port_num; i++) { 2295 port = &common->ports[i]; 2296 if (port->ndev && port->ndev->reg_state == NETREG_REGISTERED) 2297 unregister_netdev(port->ndev); 2298 } 2299 } 2300 2301 static void am65_cpsw_port_offload_fwd_mark_update(struct am65_cpsw_common *common) 2302 { 2303 int set_val = 0; 2304 int i; 2305 2306 if (common->br_members == (GENMASK(common->port_num, 1) & ~common->disabled_ports_mask)) 2307 set_val = 1; 2308 2309 dev_dbg(common->dev, "set offload_fwd_mark %d\n", set_val); 2310 2311 for (i = 1; i <= common->port_num; i++) { 2312 struct am65_cpsw_port *port = am65_common_get_port(common, i); 2313 struct am65_cpsw_ndev_priv *priv; 2314 2315 if (!port->ndev) 2316 continue; 2317 2318 priv = am65_ndev_to_priv(port->ndev); 2319 priv->offload_fwd_mark = set_val; 2320 } 2321 } 2322 2323 bool am65_cpsw_port_dev_check(const struct net_device *ndev) 2324 { 2325 if (ndev->netdev_ops == &am65_cpsw_nuss_netdev_ops) { 2326 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 2327 2328 return !common->is_emac_mode; 2329 } 2330 2331 return false; 2332 } 2333 2334 static int am65_cpsw_netdevice_port_link(struct net_device *ndev, 2335 struct net_device *br_ndev, 2336 struct netlink_ext_ack *extack) 2337 { 2338 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 2339 struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev); 2340 int err; 2341 2342 if (!common->br_members) { 2343 common->hw_bridge_dev = br_ndev; 2344 } else { 2345 /* This is adding the port to a second bridge, this is 2346 * unsupported 2347 */ 2348 if (common->hw_bridge_dev != br_ndev) 2349 return -EOPNOTSUPP; 2350 } 2351 2352 err = switchdev_bridge_port_offload(ndev, ndev, NULL, NULL, NULL, 2353 false, extack); 2354 if (err) 2355 return err; 2356 2357 common->br_members |= BIT(priv->port->port_id); 2358 2359 am65_cpsw_port_offload_fwd_mark_update(common); 2360 2361 return NOTIFY_DONE; 2362 } 2363 2364 static void am65_cpsw_netdevice_port_unlink(struct net_device *ndev) 2365 { 2366 struct am65_cpsw_common *common = am65_ndev_to_common(ndev); 2367 struct am65_cpsw_ndev_priv *priv = am65_ndev_to_priv(ndev); 2368 2369 switchdev_bridge_port_unoffload(ndev, NULL, NULL, NULL); 2370 2371 common->br_members &= ~BIT(priv->port->port_id); 2372 2373 am65_cpsw_port_offload_fwd_mark_update(common); 2374 2375 if (!common->br_members) 2376 common->hw_bridge_dev = NULL; 2377 } 2378 2379 /* netdev notifier */ 2380 static int am65_cpsw_netdevice_event(struct notifier_block *unused, 2381 unsigned long event, void *ptr) 2382 { 2383 struct netlink_ext_ack *extack = netdev_notifier_info_to_extack(ptr); 2384 struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 2385 struct netdev_notifier_changeupper_info *info; 2386 int ret = NOTIFY_DONE; 2387 2388 if (!am65_cpsw_port_dev_check(ndev)) 2389 return NOTIFY_DONE; 2390 2391 switch (event) { 2392 case NETDEV_CHANGEUPPER: 2393 info = ptr; 2394 2395 if (netif_is_bridge_master(info->upper_dev)) { 2396 if (info->linking) 2397 ret = am65_cpsw_netdevice_port_link(ndev, 2398 info->upper_dev, 2399 extack); 2400 else 2401 am65_cpsw_netdevice_port_unlink(ndev); 2402 } 2403 break; 2404 default: 2405 return NOTIFY_DONE; 2406 } 2407 2408 return notifier_from_errno(ret); 2409 } 2410 2411 static int am65_cpsw_register_notifiers(struct am65_cpsw_common *cpsw) 2412 { 2413 int ret = 0; 2414 2415 if (AM65_CPSW_IS_CPSW2G(cpsw) || 2416 !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 2417 return 0; 2418 2419 cpsw->am65_cpsw_netdevice_nb.notifier_call = &am65_cpsw_netdevice_event; 2420 ret = register_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 2421 if (ret) { 2422 dev_err(cpsw->dev, "can't register netdevice notifier\n"); 2423 return ret; 2424 } 2425 2426 ret = am65_cpsw_switchdev_register_notifiers(cpsw); 2427 if (ret) 2428 unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 2429 2430 return ret; 2431 } 2432 2433 static void am65_cpsw_unregister_notifiers(struct am65_cpsw_common *cpsw) 2434 { 2435 if (AM65_CPSW_IS_CPSW2G(cpsw) || 2436 !IS_REACHABLE(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 2437 return; 2438 2439 am65_cpsw_switchdev_unregister_notifiers(cpsw); 2440 unregister_netdevice_notifier(&cpsw->am65_cpsw_netdevice_nb); 2441 } 2442 2443 static const struct devlink_ops am65_cpsw_devlink_ops = {}; 2444 2445 static void am65_cpsw_init_stp_ale_entry(struct am65_cpsw_common *cpsw) 2446 { 2447 cpsw_ale_add_mcast(cpsw->ale, eth_stp_addr, ALE_PORT_HOST, ALE_SUPER, 0, 2448 ALE_MCAST_BLOCK_LEARN_FWD); 2449 } 2450 2451 static void am65_cpsw_init_host_port_switch(struct am65_cpsw_common *common) 2452 { 2453 struct am65_cpsw_host *host = am65_common_get_host(common); 2454 2455 writel(common->default_vlan, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 2456 2457 am65_cpsw_init_stp_ale_entry(common); 2458 2459 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 1); 2460 dev_dbg(common->dev, "Set P0_UNI_FLOOD\n"); 2461 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 0); 2462 } 2463 2464 static void am65_cpsw_init_host_port_emac(struct am65_cpsw_common *common) 2465 { 2466 struct am65_cpsw_host *host = am65_common_get_host(common); 2467 2468 writel(0, host->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 2469 2470 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_P0_UNI_FLOOD, 0); 2471 dev_dbg(common->dev, "unset P0_UNI_FLOOD\n"); 2472 2473 /* learning make no sense in multi-mac mode */ 2474 cpsw_ale_control_set(common->ale, HOST_PORT_NUM, ALE_PORT_NOLEARN, 1); 2475 } 2476 2477 static int am65_cpsw_dl_switch_mode_get(struct devlink *dl, u32 id, 2478 struct devlink_param_gset_ctx *ctx) 2479 { 2480 struct am65_cpsw_devlink *dl_priv = devlink_priv(dl); 2481 struct am65_cpsw_common *common = dl_priv->common; 2482 2483 dev_dbg(common->dev, "%s id:%u\n", __func__, id); 2484 2485 if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE) 2486 return -EOPNOTSUPP; 2487 2488 ctx->val.vbool = !common->is_emac_mode; 2489 2490 return 0; 2491 } 2492 2493 static void am65_cpsw_init_port_emac_ale(struct am65_cpsw_port *port) 2494 { 2495 struct am65_cpsw_slave_data *slave = &port->slave; 2496 struct am65_cpsw_common *common = port->common; 2497 u32 port_mask; 2498 2499 writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 2500 2501 if (slave->mac_only) 2502 /* enable mac-only mode on port */ 2503 cpsw_ale_control_set(common->ale, port->port_id, 2504 ALE_PORT_MACONLY, 1); 2505 2506 cpsw_ale_control_set(common->ale, port->port_id, ALE_PORT_NOLEARN, 1); 2507 2508 port_mask = BIT(port->port_id) | ALE_PORT_HOST; 2509 2510 cpsw_ale_add_ucast(common->ale, port->ndev->dev_addr, 2511 HOST_PORT_NUM, ALE_SECURE, slave->port_vlan); 2512 cpsw_ale_add_mcast(common->ale, port->ndev->broadcast, 2513 port_mask, ALE_VLAN, slave->port_vlan, ALE_MCAST_FWD_2); 2514 } 2515 2516 static void am65_cpsw_init_port_switch_ale(struct am65_cpsw_port *port) 2517 { 2518 struct am65_cpsw_slave_data *slave = &port->slave; 2519 struct am65_cpsw_common *cpsw = port->common; 2520 u32 port_mask; 2521 2522 cpsw_ale_control_set(cpsw->ale, port->port_id, 2523 ALE_PORT_NOLEARN, 0); 2524 2525 cpsw_ale_add_ucast(cpsw->ale, port->ndev->dev_addr, 2526 HOST_PORT_NUM, ALE_SECURE | ALE_BLOCKED | ALE_VLAN, 2527 slave->port_vlan); 2528 2529 port_mask = BIT(port->port_id) | ALE_PORT_HOST; 2530 2531 cpsw_ale_add_mcast(cpsw->ale, port->ndev->broadcast, 2532 port_mask, ALE_VLAN, slave->port_vlan, 2533 ALE_MCAST_FWD_2); 2534 2535 writel(slave->port_vlan, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 2536 2537 cpsw_ale_control_set(cpsw->ale, port->port_id, 2538 ALE_PORT_MACONLY, 0); 2539 } 2540 2541 static int am65_cpsw_dl_switch_mode_set(struct devlink *dl, u32 id, 2542 struct devlink_param_gset_ctx *ctx) 2543 { 2544 struct am65_cpsw_devlink *dl_priv = devlink_priv(dl); 2545 struct am65_cpsw_common *cpsw = dl_priv->common; 2546 bool switch_en = ctx->val.vbool; 2547 bool if_running = false; 2548 int i; 2549 2550 dev_dbg(cpsw->dev, "%s id:%u\n", __func__, id); 2551 2552 if (id != AM65_CPSW_DL_PARAM_SWITCH_MODE) 2553 return -EOPNOTSUPP; 2554 2555 if (switch_en == !cpsw->is_emac_mode) 2556 return 0; 2557 2558 if (!switch_en && cpsw->br_members) { 2559 dev_err(cpsw->dev, "Remove ports from bridge before disabling switch mode\n"); 2560 return -EINVAL; 2561 } 2562 2563 rtnl_lock(); 2564 2565 cpsw->is_emac_mode = !switch_en; 2566 2567 for (i = 0; i < cpsw->port_num; i++) { 2568 struct net_device *sl_ndev = cpsw->ports[i].ndev; 2569 2570 if (!sl_ndev || !netif_running(sl_ndev)) 2571 continue; 2572 2573 if_running = true; 2574 } 2575 2576 if (!if_running) { 2577 /* all ndevs are down */ 2578 for (i = 0; i < cpsw->port_num; i++) { 2579 struct net_device *sl_ndev = cpsw->ports[i].ndev; 2580 struct am65_cpsw_slave_data *slave; 2581 2582 if (!sl_ndev) 2583 continue; 2584 2585 slave = am65_ndev_to_slave(sl_ndev); 2586 if (switch_en) 2587 slave->port_vlan = cpsw->default_vlan; 2588 else 2589 slave->port_vlan = 0; 2590 } 2591 2592 goto exit; 2593 } 2594 2595 cpsw_ale_control_set(cpsw->ale, 0, ALE_BYPASS, 1); 2596 /* clean up ALE table */ 2597 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_CLEAR, 1); 2598 cpsw_ale_control_get(cpsw->ale, HOST_PORT_NUM, ALE_AGEOUT); 2599 2600 if (switch_en) { 2601 dev_info(cpsw->dev, "Enable switch mode\n"); 2602 2603 am65_cpsw_init_host_port_switch(cpsw); 2604 2605 for (i = 0; i < cpsw->port_num; i++) { 2606 struct net_device *sl_ndev = cpsw->ports[i].ndev; 2607 struct am65_cpsw_slave_data *slave; 2608 struct am65_cpsw_port *port; 2609 2610 if (!sl_ndev) 2611 continue; 2612 2613 port = am65_ndev_to_port(sl_ndev); 2614 slave = am65_ndev_to_slave(sl_ndev); 2615 slave->port_vlan = cpsw->default_vlan; 2616 2617 if (netif_running(sl_ndev)) 2618 am65_cpsw_init_port_switch_ale(port); 2619 } 2620 2621 } else { 2622 dev_info(cpsw->dev, "Disable switch mode\n"); 2623 2624 am65_cpsw_init_host_port_emac(cpsw); 2625 2626 for (i = 0; i < cpsw->port_num; i++) { 2627 struct net_device *sl_ndev = cpsw->ports[i].ndev; 2628 struct am65_cpsw_port *port; 2629 2630 if (!sl_ndev) 2631 continue; 2632 2633 port = am65_ndev_to_port(sl_ndev); 2634 port->slave.port_vlan = 0; 2635 if (netif_running(sl_ndev)) 2636 am65_cpsw_init_port_emac_ale(port); 2637 } 2638 } 2639 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_BYPASS, 0); 2640 exit: 2641 rtnl_unlock(); 2642 2643 return 0; 2644 } 2645 2646 static const struct devlink_param am65_cpsw_devlink_params[] = { 2647 DEVLINK_PARAM_DRIVER(AM65_CPSW_DL_PARAM_SWITCH_MODE, "switch_mode", 2648 DEVLINK_PARAM_TYPE_BOOL, 2649 BIT(DEVLINK_PARAM_CMODE_RUNTIME), 2650 am65_cpsw_dl_switch_mode_get, 2651 am65_cpsw_dl_switch_mode_set, NULL), 2652 }; 2653 2654 static int am65_cpsw_nuss_register_devlink(struct am65_cpsw_common *common) 2655 { 2656 struct devlink_port_attrs attrs = {}; 2657 struct am65_cpsw_devlink *dl_priv; 2658 struct device *dev = common->dev; 2659 struct devlink_port *dl_port; 2660 struct am65_cpsw_port *port; 2661 int ret = 0; 2662 int i; 2663 2664 common->devlink = 2665 devlink_alloc(&am65_cpsw_devlink_ops, sizeof(*dl_priv), dev); 2666 if (!common->devlink) 2667 return -ENOMEM; 2668 2669 dl_priv = devlink_priv(common->devlink); 2670 dl_priv->common = common; 2671 2672 /* Provide devlink hook to switch mode when multiple external ports 2673 * are present NUSS switchdev driver is enabled. 2674 */ 2675 if (!AM65_CPSW_IS_CPSW2G(common) && 2676 IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) { 2677 ret = devlink_params_register(common->devlink, 2678 am65_cpsw_devlink_params, 2679 ARRAY_SIZE(am65_cpsw_devlink_params)); 2680 if (ret) { 2681 dev_err(dev, "devlink params reg fail ret:%d\n", ret); 2682 goto dl_unreg; 2683 } 2684 } 2685 2686 for (i = 1; i <= common->port_num; i++) { 2687 port = am65_common_get_port(common, i); 2688 dl_port = &port->devlink_port; 2689 2690 if (port->ndev) 2691 attrs.flavour = DEVLINK_PORT_FLAVOUR_PHYSICAL; 2692 else 2693 attrs.flavour = DEVLINK_PORT_FLAVOUR_UNUSED; 2694 attrs.phys.port_number = port->port_id; 2695 attrs.switch_id.id_len = sizeof(resource_size_t); 2696 memcpy(attrs.switch_id.id, common->switch_id, attrs.switch_id.id_len); 2697 devlink_port_attrs_set(dl_port, &attrs); 2698 2699 ret = devlink_port_register(common->devlink, dl_port, port->port_id); 2700 if (ret) { 2701 dev_err(dev, "devlink_port reg fail for port %d, ret:%d\n", 2702 port->port_id, ret); 2703 goto dl_port_unreg; 2704 } 2705 } 2706 devlink_register(common->devlink); 2707 return ret; 2708 2709 dl_port_unreg: 2710 for (i = i - 1; i >= 1; i--) { 2711 port = am65_common_get_port(common, i); 2712 dl_port = &port->devlink_port; 2713 2714 devlink_port_unregister(dl_port); 2715 } 2716 dl_unreg: 2717 devlink_free(common->devlink); 2718 return ret; 2719 } 2720 2721 static void am65_cpsw_unregister_devlink(struct am65_cpsw_common *common) 2722 { 2723 struct devlink_port *dl_port; 2724 struct am65_cpsw_port *port; 2725 int i; 2726 2727 devlink_unregister(common->devlink); 2728 2729 for (i = 1; i <= common->port_num; i++) { 2730 port = am65_common_get_port(common, i); 2731 dl_port = &port->devlink_port; 2732 2733 devlink_port_unregister(dl_port); 2734 } 2735 2736 if (!AM65_CPSW_IS_CPSW2G(common) && 2737 IS_ENABLED(CONFIG_TI_K3_AM65_CPSW_SWITCHDEV)) 2738 devlink_params_unregister(common->devlink, 2739 am65_cpsw_devlink_params, 2740 ARRAY_SIZE(am65_cpsw_devlink_params)); 2741 2742 devlink_free(common->devlink); 2743 } 2744 2745 static int am65_cpsw_nuss_register_ndevs(struct am65_cpsw_common *common) 2746 { 2747 struct device *dev = common->dev; 2748 struct am65_cpsw_port *port; 2749 int ret = 0, i; 2750 2751 /* init tx channels */ 2752 ret = am65_cpsw_nuss_init_tx_chns(common); 2753 if (ret) 2754 return ret; 2755 ret = am65_cpsw_nuss_init_rx_chns(common); 2756 if (ret) 2757 return ret; 2758 2759 ret = am65_cpsw_nuss_register_devlink(common); 2760 if (ret) 2761 return ret; 2762 2763 for (i = 0; i < common->port_num; i++) { 2764 port = &common->ports[i]; 2765 2766 if (!port->ndev) 2767 continue; 2768 2769 SET_NETDEV_DEVLINK_PORT(port->ndev, &port->devlink_port); 2770 2771 ret = register_netdev(port->ndev); 2772 if (ret) { 2773 dev_err(dev, "error registering slave net device%i %d\n", 2774 i, ret); 2775 goto err_cleanup_ndev; 2776 } 2777 } 2778 2779 ret = am65_cpsw_register_notifiers(common); 2780 if (ret) 2781 goto err_cleanup_ndev; 2782 2783 /* can't auto unregister ndev using devm_add_action() due to 2784 * devres release sequence in DD core for DMA 2785 */ 2786 2787 return 0; 2788 2789 err_cleanup_ndev: 2790 am65_cpsw_nuss_cleanup_ndev(common); 2791 am65_cpsw_unregister_devlink(common); 2792 2793 return ret; 2794 } 2795 2796 int am65_cpsw_nuss_update_tx_chns(struct am65_cpsw_common *common, int num_tx) 2797 { 2798 int ret; 2799 2800 common->tx_ch_num = num_tx; 2801 ret = am65_cpsw_nuss_init_tx_chns(common); 2802 2803 return ret; 2804 } 2805 2806 struct am65_cpsw_soc_pdata { 2807 u32 quirks_dis; 2808 }; 2809 2810 static const struct am65_cpsw_soc_pdata am65x_soc_sr2_0 = { 2811 .quirks_dis = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM, 2812 }; 2813 2814 static const struct soc_device_attribute am65_cpsw_socinfo[] = { 2815 { .family = "AM65X", 2816 .revision = "SR2.0", 2817 .data = &am65x_soc_sr2_0 2818 }, 2819 {/* sentinel */} 2820 }; 2821 2822 static const struct am65_cpsw_pdata am65x_sr1_0 = { 2823 .quirks = AM65_CPSW_QUIRK_I2027_NO_TX_CSUM, 2824 .ale_dev_id = "am65x-cpsw2g", 2825 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 2826 }; 2827 2828 static const struct am65_cpsw_pdata j721e_pdata = { 2829 .quirks = 0, 2830 .ale_dev_id = "am65x-cpsw2g", 2831 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 2832 }; 2833 2834 static const struct am65_cpsw_pdata am64x_cpswxg_pdata = { 2835 .quirks = AM64_CPSW_QUIRK_DMA_RX_TDOWN_IRQ, 2836 .ale_dev_id = "am64-cpswxg", 2837 .fdqring_mode = K3_RINGACC_RING_MODE_RING, 2838 }; 2839 2840 static const struct am65_cpsw_pdata j7200_cpswxg_pdata = { 2841 .quirks = 0, 2842 .ale_dev_id = "am64-cpswxg", 2843 .fdqring_mode = K3_RINGACC_RING_MODE_RING, 2844 .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII), 2845 }; 2846 2847 static const struct am65_cpsw_pdata j721e_cpswxg_pdata = { 2848 .quirks = 0, 2849 .ale_dev_id = "am64-cpswxg", 2850 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 2851 .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_SGMII), 2852 }; 2853 2854 static const struct am65_cpsw_pdata j784s4_cpswxg_pdata = { 2855 .quirks = 0, 2856 .ale_dev_id = "am64-cpswxg", 2857 .fdqring_mode = K3_RINGACC_RING_MODE_MESSAGE, 2858 .extra_modes = BIT(PHY_INTERFACE_MODE_QSGMII) | BIT(PHY_INTERFACE_MODE_USXGMII), 2859 }; 2860 2861 static const struct of_device_id am65_cpsw_nuss_of_mtable[] = { 2862 { .compatible = "ti,am654-cpsw-nuss", .data = &am65x_sr1_0}, 2863 { .compatible = "ti,j721e-cpsw-nuss", .data = &j721e_pdata}, 2864 { .compatible = "ti,am642-cpsw-nuss", .data = &am64x_cpswxg_pdata}, 2865 { .compatible = "ti,j7200-cpswxg-nuss", .data = &j7200_cpswxg_pdata}, 2866 { .compatible = "ti,j721e-cpswxg-nuss", .data = &j721e_cpswxg_pdata}, 2867 { .compatible = "ti,j784s4-cpswxg-nuss", .data = &j784s4_cpswxg_pdata}, 2868 { /* sentinel */ }, 2869 }; 2870 MODULE_DEVICE_TABLE(of, am65_cpsw_nuss_of_mtable); 2871 2872 static void am65_cpsw_nuss_apply_socinfo(struct am65_cpsw_common *common) 2873 { 2874 const struct soc_device_attribute *soc; 2875 2876 soc = soc_device_match(am65_cpsw_socinfo); 2877 if (soc && soc->data) { 2878 const struct am65_cpsw_soc_pdata *socdata = soc->data; 2879 2880 /* disable quirks */ 2881 common->pdata.quirks &= ~socdata->quirks_dis; 2882 } 2883 } 2884 2885 static int am65_cpsw_nuss_probe(struct platform_device *pdev) 2886 { 2887 struct cpsw_ale_params ale_params = { 0 }; 2888 const struct of_device_id *of_id; 2889 struct device *dev = &pdev->dev; 2890 struct am65_cpsw_common *common; 2891 struct device_node *node; 2892 struct resource *res; 2893 struct clk *clk; 2894 u64 id_temp; 2895 int ret, i; 2896 int ale_entries; 2897 2898 common = devm_kzalloc(dev, sizeof(struct am65_cpsw_common), GFP_KERNEL); 2899 if (!common) 2900 return -ENOMEM; 2901 common->dev = dev; 2902 2903 of_id = of_match_device(am65_cpsw_nuss_of_mtable, dev); 2904 if (!of_id) 2905 return -EINVAL; 2906 common->pdata = *(const struct am65_cpsw_pdata *)of_id->data; 2907 2908 am65_cpsw_nuss_apply_socinfo(common); 2909 2910 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cpsw_nuss"); 2911 common->ss_base = devm_ioremap_resource(&pdev->dev, res); 2912 if (IS_ERR(common->ss_base)) 2913 return PTR_ERR(common->ss_base); 2914 common->cpsw_base = common->ss_base + AM65_CPSW_CPSW_NU_BASE; 2915 /* Use device's physical base address as switch id */ 2916 id_temp = cpu_to_be64(res->start); 2917 memcpy(common->switch_id, &id_temp, sizeof(res->start)); 2918 2919 node = of_get_child_by_name(dev->of_node, "ethernet-ports"); 2920 if (!node) 2921 return -ENOENT; 2922 common->port_num = of_get_child_count(node); 2923 of_node_put(node); 2924 if (common->port_num < 1 || common->port_num > AM65_CPSW_MAX_PORTS) 2925 return -ENOENT; 2926 2927 common->rx_flow_id_base = -1; 2928 init_completion(&common->tdown_complete); 2929 common->tx_ch_num = AM65_CPSW_DEFAULT_TX_CHNS; 2930 common->pf_p0_rx_ptype_rrobin = false; 2931 common->default_vlan = 1; 2932 2933 common->ports = devm_kcalloc(dev, common->port_num, 2934 sizeof(*common->ports), 2935 GFP_KERNEL); 2936 if (!common->ports) 2937 return -ENOMEM; 2938 2939 clk = devm_clk_get(dev, "fck"); 2940 if (IS_ERR(clk)) 2941 return dev_err_probe(dev, PTR_ERR(clk), "getting fck clock\n"); 2942 common->bus_freq = clk_get_rate(clk); 2943 2944 pm_runtime_enable(dev); 2945 ret = pm_runtime_resume_and_get(dev); 2946 if (ret < 0) { 2947 pm_runtime_disable(dev); 2948 return ret; 2949 } 2950 2951 node = of_get_child_by_name(dev->of_node, "mdio"); 2952 if (!node) { 2953 dev_warn(dev, "MDIO node not found\n"); 2954 } else if (of_device_is_available(node)) { 2955 struct platform_device *mdio_pdev; 2956 2957 mdio_pdev = of_platform_device_create(node, NULL, dev); 2958 if (!mdio_pdev) { 2959 ret = -ENODEV; 2960 goto err_pm_clear; 2961 } 2962 2963 common->mdio_dev = &mdio_pdev->dev; 2964 } 2965 of_node_put(node); 2966 2967 am65_cpsw_nuss_get_ver(common); 2968 2969 ret = am65_cpsw_nuss_init_host_p(common); 2970 if (ret) 2971 goto err_of_clear; 2972 2973 ret = am65_cpsw_nuss_init_slave_ports(common); 2974 if (ret) 2975 goto err_of_clear; 2976 2977 /* init common data */ 2978 ale_params.dev = dev; 2979 ale_params.ale_ageout = AM65_CPSW_ALE_AGEOUT_DEFAULT; 2980 ale_params.ale_ports = common->port_num + 1; 2981 ale_params.ale_regs = common->cpsw_base + AM65_CPSW_NU_ALE_BASE; 2982 ale_params.dev_id = common->pdata.ale_dev_id; 2983 ale_params.bus_freq = common->bus_freq; 2984 2985 common->ale = cpsw_ale_create(&ale_params); 2986 if (IS_ERR(common->ale)) { 2987 dev_err(dev, "error initializing ale engine\n"); 2988 ret = PTR_ERR(common->ale); 2989 goto err_of_clear; 2990 } 2991 2992 ale_entries = common->ale->params.ale_entries; 2993 common->ale_context = devm_kzalloc(dev, 2994 ale_entries * ALE_ENTRY_WORDS * sizeof(u32), 2995 GFP_KERNEL); 2996 ret = am65_cpsw_init_cpts(common); 2997 if (ret) 2998 goto err_of_clear; 2999 3000 /* init ports */ 3001 for (i = 0; i < common->port_num; i++) 3002 am65_cpsw_nuss_slave_disable_unused(&common->ports[i]); 3003 3004 dev_set_drvdata(dev, common); 3005 3006 common->is_emac_mode = true; 3007 3008 ret = am65_cpsw_nuss_init_ndevs(common); 3009 if (ret) 3010 goto err_free_phylink; 3011 3012 ret = am65_cpsw_nuss_register_ndevs(common); 3013 if (ret) 3014 goto err_free_phylink; 3015 3016 pm_runtime_put(dev); 3017 return 0; 3018 3019 err_free_phylink: 3020 am65_cpsw_nuss_phylink_cleanup(common); 3021 am65_cpts_release(common->cpts); 3022 err_of_clear: 3023 if (common->mdio_dev) 3024 of_platform_device_destroy(common->mdio_dev, NULL); 3025 err_pm_clear: 3026 pm_runtime_put_sync(dev); 3027 pm_runtime_disable(dev); 3028 return ret; 3029 } 3030 3031 static void am65_cpsw_nuss_remove(struct platform_device *pdev) 3032 { 3033 struct device *dev = &pdev->dev; 3034 struct am65_cpsw_common *common; 3035 int ret; 3036 3037 common = dev_get_drvdata(dev); 3038 3039 ret = pm_runtime_resume_and_get(&pdev->dev); 3040 if (ret < 0) { 3041 /* Note, if this error path is taken, we're leaking some 3042 * resources. 3043 */ 3044 dev_err(&pdev->dev, "Failed to resume device (%pe)\n", 3045 ERR_PTR(ret)); 3046 return; 3047 } 3048 3049 am65_cpsw_unregister_devlink(common); 3050 am65_cpsw_unregister_notifiers(common); 3051 3052 /* must unregister ndevs here because DD release_driver routine calls 3053 * dma_deconfigure(dev) before devres_release_all(dev) 3054 */ 3055 am65_cpsw_nuss_cleanup_ndev(common); 3056 am65_cpsw_nuss_phylink_cleanup(common); 3057 am65_cpts_release(common->cpts); 3058 am65_cpsw_disable_serdes_phy(common); 3059 3060 if (common->mdio_dev) 3061 of_platform_device_destroy(common->mdio_dev, NULL); 3062 3063 pm_runtime_put_sync(&pdev->dev); 3064 pm_runtime_disable(&pdev->dev); 3065 } 3066 3067 static int am65_cpsw_nuss_suspend(struct device *dev) 3068 { 3069 struct am65_cpsw_common *common = dev_get_drvdata(dev); 3070 struct am65_cpsw_host *host_p = am65_common_get_host(common); 3071 struct am65_cpsw_port *port; 3072 struct net_device *ndev; 3073 int i, ret; 3074 3075 cpsw_ale_dump(common->ale, common->ale_context); 3076 host_p->vid_context = readl(host_p->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 3077 for (i = 0; i < common->port_num; i++) { 3078 port = &common->ports[i]; 3079 ndev = port->ndev; 3080 3081 if (!ndev) 3082 continue; 3083 3084 port->vid_context = readl(port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 3085 netif_device_detach(ndev); 3086 if (netif_running(ndev)) { 3087 rtnl_lock(); 3088 ret = am65_cpsw_nuss_ndo_slave_stop(ndev); 3089 rtnl_unlock(); 3090 if (ret < 0) { 3091 netdev_err(ndev, "failed to stop: %d", ret); 3092 return ret; 3093 } 3094 } 3095 } 3096 3097 am65_cpts_suspend(common->cpts); 3098 3099 am65_cpsw_nuss_remove_rx_chns(common); 3100 am65_cpsw_nuss_remove_tx_chns(common); 3101 3102 return 0; 3103 } 3104 3105 static int am65_cpsw_nuss_resume(struct device *dev) 3106 { 3107 struct am65_cpsw_common *common = dev_get_drvdata(dev); 3108 struct am65_cpsw_port *port; 3109 struct net_device *ndev; 3110 int i, ret; 3111 struct am65_cpsw_host *host_p = am65_common_get_host(common); 3112 3113 ret = am65_cpsw_nuss_init_tx_chns(common); 3114 if (ret) 3115 return ret; 3116 ret = am65_cpsw_nuss_init_rx_chns(common); 3117 if (ret) 3118 return ret; 3119 3120 /* If RX IRQ was disabled before suspend, keep it disabled */ 3121 if (common->rx_irq_disabled) 3122 disable_irq(common->rx_chns.irq); 3123 3124 am65_cpts_resume(common->cpts); 3125 3126 for (i = 0; i < common->port_num; i++) { 3127 port = &common->ports[i]; 3128 ndev = port->ndev; 3129 3130 if (!ndev) 3131 continue; 3132 3133 if (netif_running(ndev)) { 3134 rtnl_lock(); 3135 ret = am65_cpsw_nuss_ndo_slave_open(ndev); 3136 rtnl_unlock(); 3137 if (ret < 0) { 3138 netdev_err(ndev, "failed to start: %d", ret); 3139 return ret; 3140 } 3141 } 3142 3143 netif_device_attach(ndev); 3144 writel(port->vid_context, port->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 3145 } 3146 3147 writel(host_p->vid_context, host_p->port_base + AM65_CPSW_PORT_VLAN_REG_OFFSET); 3148 cpsw_ale_restore(common->ale, common->ale_context); 3149 3150 return 0; 3151 } 3152 3153 static const struct dev_pm_ops am65_cpsw_nuss_dev_pm_ops = { 3154 SYSTEM_SLEEP_PM_OPS(am65_cpsw_nuss_suspend, am65_cpsw_nuss_resume) 3155 }; 3156 3157 static struct platform_driver am65_cpsw_nuss_driver = { 3158 .driver = { 3159 .name = AM65_CPSW_DRV_NAME, 3160 .of_match_table = am65_cpsw_nuss_of_mtable, 3161 .pm = &am65_cpsw_nuss_dev_pm_ops, 3162 }, 3163 .probe = am65_cpsw_nuss_probe, 3164 .remove_new = am65_cpsw_nuss_remove, 3165 }; 3166 3167 module_platform_driver(am65_cpsw_nuss_driver); 3168 3169 MODULE_LICENSE("GPL v2"); 3170 MODULE_AUTHOR("Grygorii Strashko <grygorii.strashko@ti.com>"); 3171 MODULE_DESCRIPTION("TI AM65 CPSW Ethernet driver"); 3172