1 /* 2 * Texas Instruments Ethernet Switch Driver 3 * 4 * Copyright (C) 2012 Texas Instruments 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation version 2. 9 * 10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 11 * kind, whether express or implied; without even the implied warranty 12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 */ 15 16 #include <linux/kernel.h> 17 #include <linux/io.h> 18 #include <linux/clk.h> 19 #include <linux/timer.h> 20 #include <linux/module.h> 21 #include <linux/platform_device.h> 22 #include <linux/irqreturn.h> 23 #include <linux/interrupt.h> 24 #include <linux/if_ether.h> 25 #include <linux/etherdevice.h> 26 #include <linux/netdevice.h> 27 #include <linux/net_tstamp.h> 28 #include <linux/phy.h> 29 #include <linux/workqueue.h> 30 #include <linux/delay.h> 31 #include <linux/pm_runtime.h> 32 #include <linux/gpio/consumer.h> 33 #include <linux/of.h> 34 #include <linux/of_mdio.h> 35 #include <linux/of_net.h> 36 #include <linux/of_device.h> 37 #include <linux/if_vlan.h> 38 #include <linux/kmemleak.h> 39 #include <linux/sys_soc.h> 40 41 #include <linux/pinctrl/consumer.h> 42 43 #include "cpsw.h" 44 #include "cpsw_ale.h" 45 #include "cpts.h" 46 #include "davinci_cpdma.h" 47 48 #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \ 49 NETIF_MSG_DRV | NETIF_MSG_LINK | \ 50 NETIF_MSG_IFUP | NETIF_MSG_INTR | \ 51 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \ 52 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \ 53 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \ 54 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \ 55 NETIF_MSG_RX_STATUS) 56 57 #define cpsw_info(priv, type, format, ...) \ 58 do { \ 59 if (netif_msg_##type(priv) && net_ratelimit()) \ 60 dev_info(priv->dev, format, ## __VA_ARGS__); \ 61 } while (0) 62 63 #define cpsw_err(priv, type, format, ...) \ 64 do { \ 65 if (netif_msg_##type(priv) && net_ratelimit()) \ 66 dev_err(priv->dev, format, ## __VA_ARGS__); \ 67 } while (0) 68 69 #define cpsw_dbg(priv, type, format, ...) \ 70 do { \ 71 if (netif_msg_##type(priv) && net_ratelimit()) \ 72 dev_dbg(priv->dev, format, ## __VA_ARGS__); \ 73 } while (0) 74 75 #define cpsw_notice(priv, type, format, ...) \ 76 do { \ 77 if (netif_msg_##type(priv) && net_ratelimit()) \ 78 dev_notice(priv->dev, format, ## __VA_ARGS__); \ 79 } while (0) 80 81 #define ALE_ALL_PORTS 0x7 82 83 #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7) 84 #define CPSW_MINOR_VERSION(reg) (reg & 0xff) 85 #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f) 86 87 #define CPSW_VERSION_1 0x19010a 88 #define CPSW_VERSION_2 0x19010c 89 #define CPSW_VERSION_3 0x19010f 90 #define CPSW_VERSION_4 0x190112 91 92 #define HOST_PORT_NUM 0 93 #define CPSW_ALE_PORTS_NUM 3 94 #define SLIVER_SIZE 0x40 95 96 #define CPSW1_HOST_PORT_OFFSET 0x028 97 #define CPSW1_SLAVE_OFFSET 0x050 98 #define CPSW1_SLAVE_SIZE 0x040 99 #define CPSW1_CPDMA_OFFSET 0x100 100 #define CPSW1_STATERAM_OFFSET 0x200 101 #define CPSW1_HW_STATS 0x400 102 #define CPSW1_CPTS_OFFSET 0x500 103 #define CPSW1_ALE_OFFSET 0x600 104 #define CPSW1_SLIVER_OFFSET 0x700 105 106 #define CPSW2_HOST_PORT_OFFSET 0x108 107 #define CPSW2_SLAVE_OFFSET 0x200 108 #define CPSW2_SLAVE_SIZE 0x100 109 #define CPSW2_CPDMA_OFFSET 0x800 110 #define CPSW2_HW_STATS 0x900 111 #define CPSW2_STATERAM_OFFSET 0xa00 112 #define CPSW2_CPTS_OFFSET 0xc00 113 #define CPSW2_ALE_OFFSET 0xd00 114 #define CPSW2_SLIVER_OFFSET 0xd80 115 #define CPSW2_BD_OFFSET 0x2000 116 117 #define CPDMA_RXTHRESH 0x0c0 118 #define CPDMA_RXFREE 0x0e0 119 #define CPDMA_TXHDP 0x00 120 #define CPDMA_RXHDP 0x20 121 #define CPDMA_TXCP 0x40 122 #define CPDMA_RXCP 0x60 123 124 #define CPSW_POLL_WEIGHT 64 125 #define CPSW_RX_VLAN_ENCAP_HDR_SIZE 4 126 #define CPSW_MIN_PACKET_SIZE (VLAN_ETH_ZLEN) 127 #define CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN +\ 128 ETH_FCS_LEN +\ 129 CPSW_RX_VLAN_ENCAP_HDR_SIZE) 130 131 #define RX_PRIORITY_MAPPING 0x76543210 132 #define TX_PRIORITY_MAPPING 0x33221100 133 #define CPDMA_TX_PRIORITY_MAP 0x76543210 134 135 #define CPSW_VLAN_AWARE BIT(1) 136 #define CPSW_RX_VLAN_ENCAP BIT(2) 137 #define CPSW_ALE_VLAN_AWARE 1 138 139 #define CPSW_FIFO_NORMAL_MODE (0 << 16) 140 #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16) 141 #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16) 142 143 #define CPSW_INTPACEEN (0x3f << 16) 144 #define CPSW_INTPRESCALE_MASK (0x7FF << 0) 145 #define CPSW_CMINTMAX_CNT 63 146 #define CPSW_CMINTMIN_CNT 2 147 #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT) 148 #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1) 149 150 #define cpsw_slave_index(cpsw, priv) \ 151 ((cpsw->data.dual_emac) ? priv->emac_port : \ 152 cpsw->data.active_slave) 153 #define IRQ_NUM 2 154 #define CPSW_MAX_QUEUES 8 155 #define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256 156 157 #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT 29 158 #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK GENMASK(2, 0) 159 #define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT 16 160 #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT 8 161 #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK GENMASK(1, 0) 162 enum { 163 CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0, 164 CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV, 165 CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG, 166 CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG, 167 }; 168 169 static int debug_level; 170 module_param(debug_level, int, 0); 171 MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)"); 172 173 static int ale_ageout = 10; 174 module_param(ale_ageout, int, 0); 175 MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)"); 176 177 static int rx_packet_max = CPSW_MAX_PACKET_SIZE; 178 module_param(rx_packet_max, int, 0); 179 MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)"); 180 181 static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT; 182 module_param(descs_pool_size, int, 0444); 183 MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool"); 184 185 struct cpsw_wr_regs { 186 u32 id_ver; 187 u32 soft_reset; 188 u32 control; 189 u32 int_control; 190 u32 rx_thresh_en; 191 u32 rx_en; 192 u32 tx_en; 193 u32 misc_en; 194 u32 mem_allign1[8]; 195 u32 rx_thresh_stat; 196 u32 rx_stat; 197 u32 tx_stat; 198 u32 misc_stat; 199 u32 mem_allign2[8]; 200 u32 rx_imax; 201 u32 tx_imax; 202 203 }; 204 205 struct cpsw_ss_regs { 206 u32 id_ver; 207 u32 control; 208 u32 soft_reset; 209 u32 stat_port_en; 210 u32 ptype; 211 u32 soft_idle; 212 u32 thru_rate; 213 u32 gap_thresh; 214 u32 tx_start_wds; 215 u32 flow_control; 216 u32 vlan_ltype; 217 u32 ts_ltype; 218 u32 dlr_ltype; 219 }; 220 221 /* CPSW_PORT_V1 */ 222 #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */ 223 #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */ 224 #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */ 225 #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */ 226 #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */ 227 #define CPSW1_TS_CTL 0x14 /* Time Sync Control */ 228 #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */ 229 #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */ 230 231 /* CPSW_PORT_V2 */ 232 #define CPSW2_CONTROL 0x00 /* Control Register */ 233 #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */ 234 #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */ 235 #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */ 236 #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */ 237 #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */ 238 #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */ 239 240 /* CPSW_PORT_V1 and V2 */ 241 #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */ 242 #define SA_HI 0x24 /* CPGMAC_SL Source Address High */ 243 #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */ 244 245 /* CPSW_PORT_V2 only */ 246 #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */ 247 #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */ 248 #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */ 249 #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */ 250 #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */ 251 #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */ 252 #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */ 253 #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */ 254 255 /* Bit definitions for the CPSW2_CONTROL register */ 256 #define PASS_PRI_TAGGED BIT(24) /* Pass Priority Tagged */ 257 #define VLAN_LTYPE2_EN BIT(21) /* VLAN LTYPE 2 enable */ 258 #define VLAN_LTYPE1_EN BIT(20) /* VLAN LTYPE 1 enable */ 259 #define DSCP_PRI_EN BIT(16) /* DSCP Priority Enable */ 260 #define TS_107 BIT(15) /* Tyme Sync Dest IP Address 107 */ 261 #define TS_320 BIT(14) /* Time Sync Dest Port 320 enable */ 262 #define TS_319 BIT(13) /* Time Sync Dest Port 319 enable */ 263 #define TS_132 BIT(12) /* Time Sync Dest IP Addr 132 enable */ 264 #define TS_131 BIT(11) /* Time Sync Dest IP Addr 131 enable */ 265 #define TS_130 BIT(10) /* Time Sync Dest IP Addr 130 enable */ 266 #define TS_129 BIT(9) /* Time Sync Dest IP Addr 129 enable */ 267 #define TS_TTL_NONZERO BIT(8) /* Time Sync Time To Live Non-zero enable */ 268 #define TS_ANNEX_F_EN BIT(6) /* Time Sync Annex F enable */ 269 #define TS_ANNEX_D_EN BIT(4) /* Time Sync Annex D enable */ 270 #define TS_LTYPE2_EN BIT(3) /* Time Sync LTYPE 2 enable */ 271 #define TS_LTYPE1_EN BIT(2) /* Time Sync LTYPE 1 enable */ 272 #define TS_TX_EN BIT(1) /* Time Sync Transmit Enable */ 273 #define TS_RX_EN BIT(0) /* Time Sync Receive Enable */ 274 275 #define CTRL_V2_TS_BITS \ 276 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\ 277 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN) 278 279 #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN) 280 #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN) 281 #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN) 282 283 284 #define CTRL_V3_TS_BITS \ 285 (TS_107 | TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\ 286 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\ 287 TS_LTYPE1_EN) 288 289 #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN) 290 #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN) 291 #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN) 292 293 /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */ 294 #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */ 295 #define TS_SEQ_ID_OFFSET_MASK (0x3f) 296 #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */ 297 #define TS_MSG_TYPE_EN_MASK (0xffff) 298 299 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */ 300 #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3)) 301 302 /* Bit definitions for the CPSW1_TS_CTL register */ 303 #define CPSW_V1_TS_RX_EN BIT(0) 304 #define CPSW_V1_TS_TX_EN BIT(4) 305 #define CPSW_V1_MSG_TYPE_OFS 16 306 307 /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */ 308 #define CPSW_V1_SEQ_ID_OFS_SHIFT 16 309 310 #define CPSW_MAX_BLKS_TX 15 311 #define CPSW_MAX_BLKS_TX_SHIFT 4 312 #define CPSW_MAX_BLKS_RX 5 313 314 struct cpsw_host_regs { 315 u32 max_blks; 316 u32 blk_cnt; 317 u32 tx_in_ctl; 318 u32 port_vlan; 319 u32 tx_pri_map; 320 u32 cpdma_tx_pri_map; 321 u32 cpdma_rx_chan_map; 322 }; 323 324 struct cpsw_sliver_regs { 325 u32 id_ver; 326 u32 mac_control; 327 u32 mac_status; 328 u32 soft_reset; 329 u32 rx_maxlen; 330 u32 __reserved_0; 331 u32 rx_pause; 332 u32 tx_pause; 333 u32 __reserved_1; 334 u32 rx_pri_map; 335 }; 336 337 struct cpsw_hw_stats { 338 u32 rxgoodframes; 339 u32 rxbroadcastframes; 340 u32 rxmulticastframes; 341 u32 rxpauseframes; 342 u32 rxcrcerrors; 343 u32 rxaligncodeerrors; 344 u32 rxoversizedframes; 345 u32 rxjabberframes; 346 u32 rxundersizedframes; 347 u32 rxfragments; 348 u32 __pad_0[2]; 349 u32 rxoctets; 350 u32 txgoodframes; 351 u32 txbroadcastframes; 352 u32 txmulticastframes; 353 u32 txpauseframes; 354 u32 txdeferredframes; 355 u32 txcollisionframes; 356 u32 txsinglecollframes; 357 u32 txmultcollframes; 358 u32 txexcessivecollisions; 359 u32 txlatecollisions; 360 u32 txunderrun; 361 u32 txcarriersenseerrors; 362 u32 txoctets; 363 u32 octetframes64; 364 u32 octetframes65t127; 365 u32 octetframes128t255; 366 u32 octetframes256t511; 367 u32 octetframes512t1023; 368 u32 octetframes1024tup; 369 u32 netoctets; 370 u32 rxsofoverruns; 371 u32 rxmofoverruns; 372 u32 rxdmaoverruns; 373 }; 374 375 struct cpsw_slave_data { 376 struct device_node *phy_node; 377 char phy_id[MII_BUS_ID_SIZE]; 378 int phy_if; 379 u8 mac_addr[ETH_ALEN]; 380 u16 dual_emac_res_vlan; /* Reserved VLAN for DualEMAC */ 381 }; 382 383 struct cpsw_platform_data { 384 struct cpsw_slave_data *slave_data; 385 u32 ss_reg_ofs; /* Subsystem control register offset */ 386 u32 channels; /* number of cpdma channels (symmetric) */ 387 u32 slaves; /* number of slave cpgmac ports */ 388 u32 active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */ 389 u32 ale_entries; /* ale table size */ 390 u32 bd_ram_size; /*buffer descriptor ram size */ 391 u32 mac_control; /* Mac control register */ 392 u16 default_vlan; /* Def VLAN for ALE lookup in VLAN aware mode*/ 393 bool dual_emac; /* Enable Dual EMAC mode */ 394 }; 395 396 struct cpsw_slave { 397 void __iomem *regs; 398 struct cpsw_sliver_regs __iomem *sliver; 399 int slave_num; 400 u32 mac_control; 401 struct cpsw_slave_data *data; 402 struct phy_device *phy; 403 struct net_device *ndev; 404 u32 port_vlan; 405 }; 406 407 static inline u32 slave_read(struct cpsw_slave *slave, u32 offset) 408 { 409 return readl_relaxed(slave->regs + offset); 410 } 411 412 static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset) 413 { 414 writel_relaxed(val, slave->regs + offset); 415 } 416 417 struct cpsw_vector { 418 struct cpdma_chan *ch; 419 int budget; 420 }; 421 422 struct cpsw_common { 423 struct device *dev; 424 struct cpsw_platform_data data; 425 struct napi_struct napi_rx; 426 struct napi_struct napi_tx; 427 struct cpsw_ss_regs __iomem *regs; 428 struct cpsw_wr_regs __iomem *wr_regs; 429 u8 __iomem *hw_stats; 430 struct cpsw_host_regs __iomem *host_port_regs; 431 u32 version; 432 u32 coal_intvl; 433 u32 bus_freq_mhz; 434 int rx_packet_max; 435 struct cpsw_slave *slaves; 436 struct cpdma_ctlr *dma; 437 struct cpsw_vector txv[CPSW_MAX_QUEUES]; 438 struct cpsw_vector rxv[CPSW_MAX_QUEUES]; 439 struct cpsw_ale *ale; 440 bool quirk_irq; 441 bool rx_irq_disabled; 442 bool tx_irq_disabled; 443 u32 irqs_table[IRQ_NUM]; 444 struct cpts *cpts; 445 int rx_ch_num, tx_ch_num; 446 int speed; 447 int usage_count; 448 }; 449 450 struct cpsw_priv { 451 struct net_device *ndev; 452 struct device *dev; 453 u32 msg_enable; 454 u8 mac_addr[ETH_ALEN]; 455 bool rx_pause; 456 bool tx_pause; 457 u32 emac_port; 458 struct cpsw_common *cpsw; 459 }; 460 461 struct cpsw_stats { 462 char stat_string[ETH_GSTRING_LEN]; 463 int type; 464 int sizeof_stat; 465 int stat_offset; 466 }; 467 468 enum { 469 CPSW_STATS, 470 CPDMA_RX_STATS, 471 CPDMA_TX_STATS, 472 }; 473 474 #define CPSW_STAT(m) CPSW_STATS, \ 475 sizeof(((struct cpsw_hw_stats *)0)->m), \ 476 offsetof(struct cpsw_hw_stats, m) 477 #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \ 478 sizeof(((struct cpdma_chan_stats *)0)->m), \ 479 offsetof(struct cpdma_chan_stats, m) 480 #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \ 481 sizeof(((struct cpdma_chan_stats *)0)->m), \ 482 offsetof(struct cpdma_chan_stats, m) 483 484 static const struct cpsw_stats cpsw_gstrings_stats[] = { 485 { "Good Rx Frames", CPSW_STAT(rxgoodframes) }, 486 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) }, 487 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) }, 488 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) }, 489 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) }, 490 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) }, 491 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) }, 492 { "Rx Jabbers", CPSW_STAT(rxjabberframes) }, 493 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) }, 494 { "Rx Fragments", CPSW_STAT(rxfragments) }, 495 { "Rx Octets", CPSW_STAT(rxoctets) }, 496 { "Good Tx Frames", CPSW_STAT(txgoodframes) }, 497 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) }, 498 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) }, 499 { "Pause Tx Frames", CPSW_STAT(txpauseframes) }, 500 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) }, 501 { "Collisions", CPSW_STAT(txcollisionframes) }, 502 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) }, 503 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) }, 504 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) }, 505 { "Late Collisions", CPSW_STAT(txlatecollisions) }, 506 { "Tx Underrun", CPSW_STAT(txunderrun) }, 507 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) }, 508 { "Tx Octets", CPSW_STAT(txoctets) }, 509 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) }, 510 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) }, 511 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) }, 512 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) }, 513 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) }, 514 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) }, 515 { "Net Octets", CPSW_STAT(netoctets) }, 516 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) }, 517 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) }, 518 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) }, 519 }; 520 521 static const struct cpsw_stats cpsw_gstrings_ch_stats[] = { 522 { "head_enqueue", CPDMA_RX_STAT(head_enqueue) }, 523 { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) }, 524 { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) }, 525 { "misqueued", CPDMA_RX_STAT(misqueued) }, 526 { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) }, 527 { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) }, 528 { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) }, 529 { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) }, 530 { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) }, 531 { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) }, 532 { "good_dequeue", CPDMA_RX_STAT(good_dequeue) }, 533 { "requeue", CPDMA_RX_STAT(requeue) }, 534 { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) }, 535 }; 536 537 #define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats) 538 #define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats) 539 540 #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw) 541 #define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi) 542 #define for_each_slave(priv, func, arg...) \ 543 do { \ 544 struct cpsw_slave *slave; \ 545 struct cpsw_common *cpsw = (priv)->cpsw; \ 546 int n; \ 547 if (cpsw->data.dual_emac) \ 548 (func)((cpsw)->slaves + priv->emac_port, ##arg);\ 549 else \ 550 for (n = cpsw->data.slaves, \ 551 slave = cpsw->slaves; \ 552 n; n--) \ 553 (func)(slave++, ##arg); \ 554 } while (0) 555 556 #define cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb) \ 557 do { \ 558 if (!cpsw->data.dual_emac) \ 559 break; \ 560 if (CPDMA_RX_SOURCE_PORT(status) == 1) { \ 561 ndev = cpsw->slaves[0].ndev; \ 562 skb->dev = ndev; \ 563 } else if (CPDMA_RX_SOURCE_PORT(status) == 2) { \ 564 ndev = cpsw->slaves[1].ndev; \ 565 skb->dev = ndev; \ 566 } \ 567 } while (0) 568 #define cpsw_add_mcast(cpsw, priv, addr) \ 569 do { \ 570 if (cpsw->data.dual_emac) { \ 571 struct cpsw_slave *slave = cpsw->slaves + \ 572 priv->emac_port; \ 573 int slave_port = cpsw_get_slave_port( \ 574 slave->slave_num); \ 575 cpsw_ale_add_mcast(cpsw->ale, addr, \ 576 1 << slave_port | ALE_PORT_HOST, \ 577 ALE_VLAN, slave->port_vlan, 0); \ 578 } else { \ 579 cpsw_ale_add_mcast(cpsw->ale, addr, \ 580 ALE_ALL_PORTS, \ 581 0, 0, 0); \ 582 } \ 583 } while (0) 584 585 static inline int cpsw_get_slave_port(u32 slave_num) 586 { 587 return slave_num + 1; 588 } 589 590 static void cpsw_set_promiscious(struct net_device *ndev, bool enable) 591 { 592 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 593 struct cpsw_ale *ale = cpsw->ale; 594 int i; 595 596 if (cpsw->data.dual_emac) { 597 bool flag = false; 598 599 /* Enabling promiscuous mode for one interface will be 600 * common for both the interface as the interface shares 601 * the same hardware resource. 602 */ 603 for (i = 0; i < cpsw->data.slaves; i++) 604 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC) 605 flag = true; 606 607 if (!enable && flag) { 608 enable = true; 609 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n"); 610 } 611 612 if (enable) { 613 /* Enable Bypass */ 614 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1); 615 616 dev_dbg(&ndev->dev, "promiscuity enabled\n"); 617 } else { 618 /* Disable Bypass */ 619 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0); 620 dev_dbg(&ndev->dev, "promiscuity disabled\n"); 621 } 622 } else { 623 if (enable) { 624 unsigned long timeout = jiffies + HZ; 625 626 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */ 627 for (i = 0; i <= cpsw->data.slaves; i++) { 628 cpsw_ale_control_set(ale, i, 629 ALE_PORT_NOLEARN, 1); 630 cpsw_ale_control_set(ale, i, 631 ALE_PORT_NO_SA_UPDATE, 1); 632 } 633 634 /* Clear All Untouched entries */ 635 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1); 636 do { 637 cpu_relax(); 638 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT)) 639 break; 640 } while (time_after(timeout, jiffies)); 641 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1); 642 643 /* Clear all mcast from ALE */ 644 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1); 645 646 /* Flood All Unicast Packets to Host port */ 647 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1); 648 dev_dbg(&ndev->dev, "promiscuity enabled\n"); 649 } else { 650 /* Don't Flood All Unicast Packets to Host port */ 651 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0); 652 653 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */ 654 for (i = 0; i <= cpsw->data.slaves; i++) { 655 cpsw_ale_control_set(ale, i, 656 ALE_PORT_NOLEARN, 0); 657 cpsw_ale_control_set(ale, i, 658 ALE_PORT_NO_SA_UPDATE, 0); 659 } 660 dev_dbg(&ndev->dev, "promiscuity disabled\n"); 661 } 662 } 663 } 664 665 static void cpsw_ndo_set_rx_mode(struct net_device *ndev) 666 { 667 struct cpsw_priv *priv = netdev_priv(ndev); 668 struct cpsw_common *cpsw = priv->cpsw; 669 int vid; 670 671 if (cpsw->data.dual_emac) 672 vid = cpsw->slaves[priv->emac_port].port_vlan; 673 else 674 vid = cpsw->data.default_vlan; 675 676 if (ndev->flags & IFF_PROMISC) { 677 /* Enable promiscuous mode */ 678 cpsw_set_promiscious(ndev, true); 679 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI); 680 return; 681 } else { 682 /* Disable promiscuous mode */ 683 cpsw_set_promiscious(ndev, false); 684 } 685 686 /* Restore allmulti on vlans if necessary */ 687 cpsw_ale_set_allmulti(cpsw->ale, priv->ndev->flags & IFF_ALLMULTI); 688 689 /* Clear all mcast from ALE */ 690 cpsw_ale_flush_multicast(cpsw->ale, ALE_ALL_PORTS, vid); 691 692 if (!netdev_mc_empty(ndev)) { 693 struct netdev_hw_addr *ha; 694 695 /* program multicast address list into ALE register */ 696 netdev_for_each_mc_addr(ha, ndev) { 697 cpsw_add_mcast(cpsw, priv, (u8 *)ha->addr); 698 } 699 } 700 } 701 702 static void cpsw_intr_enable(struct cpsw_common *cpsw) 703 { 704 writel_relaxed(0xFF, &cpsw->wr_regs->tx_en); 705 writel_relaxed(0xFF, &cpsw->wr_regs->rx_en); 706 707 cpdma_ctlr_int_ctrl(cpsw->dma, true); 708 return; 709 } 710 711 static void cpsw_intr_disable(struct cpsw_common *cpsw) 712 { 713 writel_relaxed(0, &cpsw->wr_regs->tx_en); 714 writel_relaxed(0, &cpsw->wr_regs->rx_en); 715 716 cpdma_ctlr_int_ctrl(cpsw->dma, false); 717 return; 718 } 719 720 static void cpsw_tx_handler(void *token, int len, int status) 721 { 722 struct netdev_queue *txq; 723 struct sk_buff *skb = token; 724 struct net_device *ndev = skb->dev; 725 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 726 727 /* Check whether the queue is stopped due to stalled tx dma, if the 728 * queue is stopped then start the queue as we have free desc for tx 729 */ 730 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb)); 731 if (unlikely(netif_tx_queue_stopped(txq))) 732 netif_tx_wake_queue(txq); 733 734 cpts_tx_timestamp(cpsw->cpts, skb); 735 ndev->stats.tx_packets++; 736 ndev->stats.tx_bytes += len; 737 dev_kfree_skb_any(skb); 738 } 739 740 static void cpsw_rx_vlan_encap(struct sk_buff *skb) 741 { 742 struct cpsw_priv *priv = netdev_priv(skb->dev); 743 struct cpsw_common *cpsw = priv->cpsw; 744 u32 rx_vlan_encap_hdr = *((u32 *)skb->data); 745 u16 vtag, vid, prio, pkt_type; 746 747 /* Remove VLAN header encapsulation word */ 748 skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE); 749 750 pkt_type = (rx_vlan_encap_hdr >> 751 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) & 752 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK; 753 /* Ignore unknown & Priority-tagged packets*/ 754 if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV || 755 pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG) 756 return; 757 758 vid = (rx_vlan_encap_hdr >> 759 CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) & 760 VLAN_VID_MASK; 761 /* Ignore vid 0 and pass packet as is */ 762 if (!vid) 763 return; 764 /* Ignore default vlans in dual mac mode */ 765 if (cpsw->data.dual_emac && 766 vid == cpsw->slaves[priv->emac_port].port_vlan) 767 return; 768 769 prio = (rx_vlan_encap_hdr >> 770 CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) & 771 CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK; 772 773 vtag = (prio << VLAN_PRIO_SHIFT) | vid; 774 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag); 775 776 /* strip vlan tag for VLAN-tagged packet */ 777 if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) { 778 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN); 779 skb_pull(skb, VLAN_HLEN); 780 } 781 } 782 783 static void cpsw_rx_handler(void *token, int len, int status) 784 { 785 struct cpdma_chan *ch; 786 struct sk_buff *skb = token; 787 struct sk_buff *new_skb; 788 struct net_device *ndev = skb->dev; 789 int ret = 0; 790 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 791 792 cpsw_dual_emac_src_port_detect(cpsw, status, ndev, skb); 793 794 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) { 795 /* In dual emac mode check for all interfaces */ 796 if (cpsw->data.dual_emac && cpsw->usage_count && 797 (status >= 0)) { 798 /* The packet received is for the interface which 799 * is already down and the other interface is up 800 * and running, instead of freeing which results 801 * in reducing of the number of rx descriptor in 802 * DMA engine, requeue skb back to cpdma. 803 */ 804 new_skb = skb; 805 goto requeue; 806 } 807 808 /* the interface is going down, skbs are purged */ 809 dev_kfree_skb_any(skb); 810 return; 811 } 812 813 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max); 814 if (new_skb) { 815 skb_copy_queue_mapping(new_skb, skb); 816 skb_put(skb, len); 817 if (status & CPDMA_RX_VLAN_ENCAP) 818 cpsw_rx_vlan_encap(skb); 819 cpts_rx_timestamp(cpsw->cpts, skb); 820 skb->protocol = eth_type_trans(skb, ndev); 821 netif_receive_skb(skb); 822 ndev->stats.rx_bytes += len; 823 ndev->stats.rx_packets++; 824 kmemleak_not_leak(new_skb); 825 } else { 826 ndev->stats.rx_dropped++; 827 new_skb = skb; 828 } 829 830 requeue: 831 if (netif_dormant(ndev)) { 832 dev_kfree_skb_any(new_skb); 833 return; 834 } 835 836 ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch; 837 ret = cpdma_chan_submit(ch, new_skb, new_skb->data, 838 skb_tailroom(new_skb), 0); 839 if (WARN_ON(ret < 0)) 840 dev_kfree_skb_any(new_skb); 841 } 842 843 static void cpsw_split_res(struct net_device *ndev) 844 { 845 struct cpsw_priv *priv = netdev_priv(ndev); 846 u32 consumed_rate = 0, bigest_rate = 0; 847 struct cpsw_common *cpsw = priv->cpsw; 848 struct cpsw_vector *txv = cpsw->txv; 849 int i, ch_weight, rlim_ch_num = 0; 850 int budget, bigest_rate_ch = 0; 851 u32 ch_rate, max_rate; 852 int ch_budget = 0; 853 854 for (i = 0; i < cpsw->tx_ch_num; i++) { 855 ch_rate = cpdma_chan_get_rate(txv[i].ch); 856 if (!ch_rate) 857 continue; 858 859 rlim_ch_num++; 860 consumed_rate += ch_rate; 861 } 862 863 if (cpsw->tx_ch_num == rlim_ch_num) { 864 max_rate = consumed_rate; 865 } else if (!rlim_ch_num) { 866 ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num; 867 bigest_rate = 0; 868 max_rate = consumed_rate; 869 } else { 870 max_rate = cpsw->speed * 1000; 871 872 /* if max_rate is less then expected due to reduced link speed, 873 * split proportionally according next potential max speed 874 */ 875 if (max_rate < consumed_rate) 876 max_rate *= 10; 877 878 if (max_rate < consumed_rate) 879 max_rate *= 10; 880 881 ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate; 882 ch_budget = (CPSW_POLL_WEIGHT - ch_budget) / 883 (cpsw->tx_ch_num - rlim_ch_num); 884 bigest_rate = (max_rate - consumed_rate) / 885 (cpsw->tx_ch_num - rlim_ch_num); 886 } 887 888 /* split tx weight/budget */ 889 budget = CPSW_POLL_WEIGHT; 890 for (i = 0; i < cpsw->tx_ch_num; i++) { 891 ch_rate = cpdma_chan_get_rate(txv[i].ch); 892 if (ch_rate) { 893 txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate; 894 if (!txv[i].budget) 895 txv[i].budget++; 896 if (ch_rate > bigest_rate) { 897 bigest_rate_ch = i; 898 bigest_rate = ch_rate; 899 } 900 901 ch_weight = (ch_rate * 100) / max_rate; 902 if (!ch_weight) 903 ch_weight++; 904 cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight); 905 } else { 906 txv[i].budget = ch_budget; 907 if (!bigest_rate_ch) 908 bigest_rate_ch = i; 909 cpdma_chan_set_weight(cpsw->txv[i].ch, 0); 910 } 911 912 budget -= txv[i].budget; 913 } 914 915 if (budget) 916 txv[bigest_rate_ch].budget += budget; 917 918 /* split rx budget */ 919 budget = CPSW_POLL_WEIGHT; 920 ch_budget = budget / cpsw->rx_ch_num; 921 for (i = 0; i < cpsw->rx_ch_num; i++) { 922 cpsw->rxv[i].budget = ch_budget; 923 budget -= ch_budget; 924 } 925 926 if (budget) 927 cpsw->rxv[0].budget += budget; 928 } 929 930 static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id) 931 { 932 struct cpsw_common *cpsw = dev_id; 933 934 writel(0, &cpsw->wr_regs->tx_en); 935 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX); 936 937 if (cpsw->quirk_irq) { 938 disable_irq_nosync(cpsw->irqs_table[1]); 939 cpsw->tx_irq_disabled = true; 940 } 941 942 napi_schedule(&cpsw->napi_tx); 943 return IRQ_HANDLED; 944 } 945 946 static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id) 947 { 948 struct cpsw_common *cpsw = dev_id; 949 950 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX); 951 writel(0, &cpsw->wr_regs->rx_en); 952 953 if (cpsw->quirk_irq) { 954 disable_irq_nosync(cpsw->irqs_table[0]); 955 cpsw->rx_irq_disabled = true; 956 } 957 958 napi_schedule(&cpsw->napi_rx); 959 return IRQ_HANDLED; 960 } 961 962 static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget) 963 { 964 u32 ch_map; 965 int num_tx, cur_budget, ch; 966 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx); 967 struct cpsw_vector *txv; 968 969 /* process every unprocessed channel */ 970 ch_map = cpdma_ctrl_txchs_state(cpsw->dma); 971 for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) { 972 if (!(ch_map & 0x80)) 973 continue; 974 975 txv = &cpsw->txv[ch]; 976 if (unlikely(txv->budget > budget - num_tx)) 977 cur_budget = budget - num_tx; 978 else 979 cur_budget = txv->budget; 980 981 num_tx += cpdma_chan_process(txv->ch, cur_budget); 982 if (num_tx >= budget) 983 break; 984 } 985 986 if (num_tx < budget) { 987 napi_complete(napi_tx); 988 writel(0xff, &cpsw->wr_regs->tx_en); 989 } 990 991 return num_tx; 992 } 993 994 static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget) 995 { 996 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx); 997 int num_tx; 998 999 num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget); 1000 if (num_tx < budget) { 1001 napi_complete(napi_tx); 1002 writel(0xff, &cpsw->wr_regs->tx_en); 1003 if (cpsw->tx_irq_disabled) { 1004 cpsw->tx_irq_disabled = false; 1005 enable_irq(cpsw->irqs_table[1]); 1006 } 1007 } 1008 1009 return num_tx; 1010 } 1011 1012 static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget) 1013 { 1014 u32 ch_map; 1015 int num_rx, cur_budget, ch; 1016 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx); 1017 struct cpsw_vector *rxv; 1018 1019 /* process every unprocessed channel */ 1020 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma); 1021 for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) { 1022 if (!(ch_map & 0x01)) 1023 continue; 1024 1025 rxv = &cpsw->rxv[ch]; 1026 if (unlikely(rxv->budget > budget - num_rx)) 1027 cur_budget = budget - num_rx; 1028 else 1029 cur_budget = rxv->budget; 1030 1031 num_rx += cpdma_chan_process(rxv->ch, cur_budget); 1032 if (num_rx >= budget) 1033 break; 1034 } 1035 1036 if (num_rx < budget) { 1037 napi_complete_done(napi_rx, num_rx); 1038 writel(0xff, &cpsw->wr_regs->rx_en); 1039 } 1040 1041 return num_rx; 1042 } 1043 1044 static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget) 1045 { 1046 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx); 1047 int num_rx; 1048 1049 num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget); 1050 if (num_rx < budget) { 1051 napi_complete_done(napi_rx, num_rx); 1052 writel(0xff, &cpsw->wr_regs->rx_en); 1053 if (cpsw->rx_irq_disabled) { 1054 cpsw->rx_irq_disabled = false; 1055 enable_irq(cpsw->irqs_table[0]); 1056 } 1057 } 1058 1059 return num_rx; 1060 } 1061 1062 static inline void soft_reset(const char *module, void __iomem *reg) 1063 { 1064 unsigned long timeout = jiffies + HZ; 1065 1066 writel_relaxed(1, reg); 1067 do { 1068 cpu_relax(); 1069 } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies)); 1070 1071 WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module); 1072 } 1073 1074 static void cpsw_set_slave_mac(struct cpsw_slave *slave, 1075 struct cpsw_priv *priv) 1076 { 1077 slave_write(slave, mac_hi(priv->mac_addr), SA_HI); 1078 slave_write(slave, mac_lo(priv->mac_addr), SA_LO); 1079 } 1080 1081 static void _cpsw_adjust_link(struct cpsw_slave *slave, 1082 struct cpsw_priv *priv, bool *link) 1083 { 1084 struct phy_device *phy = slave->phy; 1085 u32 mac_control = 0; 1086 u32 slave_port; 1087 struct cpsw_common *cpsw = priv->cpsw; 1088 1089 if (!phy) 1090 return; 1091 1092 slave_port = cpsw_get_slave_port(slave->slave_num); 1093 1094 if (phy->link) { 1095 mac_control = cpsw->data.mac_control; 1096 1097 /* enable forwarding */ 1098 cpsw_ale_control_set(cpsw->ale, slave_port, 1099 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 1100 1101 if (phy->speed == 1000) 1102 mac_control |= BIT(7); /* GIGABITEN */ 1103 if (phy->duplex) 1104 mac_control |= BIT(0); /* FULLDUPLEXEN */ 1105 1106 /* set speed_in input in case RMII mode is used in 100Mbps */ 1107 if (phy->speed == 100) 1108 mac_control |= BIT(15); 1109 /* in band mode only works in 10Mbps RGMII mode */ 1110 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy)) 1111 mac_control |= BIT(18); /* In Band mode */ 1112 1113 if (priv->rx_pause) 1114 mac_control |= BIT(3); 1115 1116 if (priv->tx_pause) 1117 mac_control |= BIT(4); 1118 1119 *link = true; 1120 } else { 1121 mac_control = 0; 1122 /* disable forwarding */ 1123 cpsw_ale_control_set(cpsw->ale, slave_port, 1124 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 1125 } 1126 1127 if (mac_control != slave->mac_control) { 1128 phy_print_status(phy); 1129 writel_relaxed(mac_control, &slave->sliver->mac_control); 1130 } 1131 1132 slave->mac_control = mac_control; 1133 } 1134 1135 static int cpsw_get_common_speed(struct cpsw_common *cpsw) 1136 { 1137 int i, speed; 1138 1139 for (i = 0, speed = 0; i < cpsw->data.slaves; i++) 1140 if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link) 1141 speed += cpsw->slaves[i].phy->speed; 1142 1143 return speed; 1144 } 1145 1146 static int cpsw_need_resplit(struct cpsw_common *cpsw) 1147 { 1148 int i, rlim_ch_num; 1149 int speed, ch_rate; 1150 1151 /* re-split resources only in case speed was changed */ 1152 speed = cpsw_get_common_speed(cpsw); 1153 if (speed == cpsw->speed || !speed) 1154 return 0; 1155 1156 cpsw->speed = speed; 1157 1158 for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) { 1159 ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch); 1160 if (!ch_rate) 1161 break; 1162 1163 rlim_ch_num++; 1164 } 1165 1166 /* cases not dependent on speed */ 1167 if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num) 1168 return 0; 1169 1170 return 1; 1171 } 1172 1173 static void cpsw_adjust_link(struct net_device *ndev) 1174 { 1175 struct cpsw_priv *priv = netdev_priv(ndev); 1176 struct cpsw_common *cpsw = priv->cpsw; 1177 bool link = false; 1178 1179 for_each_slave(priv, _cpsw_adjust_link, priv, &link); 1180 1181 if (link) { 1182 if (cpsw_need_resplit(cpsw)) 1183 cpsw_split_res(ndev); 1184 1185 netif_carrier_on(ndev); 1186 if (netif_running(ndev)) 1187 netif_tx_wake_all_queues(ndev); 1188 } else { 1189 netif_carrier_off(ndev); 1190 netif_tx_stop_all_queues(ndev); 1191 } 1192 } 1193 1194 static int cpsw_get_coalesce(struct net_device *ndev, 1195 struct ethtool_coalesce *coal) 1196 { 1197 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1198 1199 coal->rx_coalesce_usecs = cpsw->coal_intvl; 1200 return 0; 1201 } 1202 1203 static int cpsw_set_coalesce(struct net_device *ndev, 1204 struct ethtool_coalesce *coal) 1205 { 1206 struct cpsw_priv *priv = netdev_priv(ndev); 1207 u32 int_ctrl; 1208 u32 num_interrupts = 0; 1209 u32 prescale = 0; 1210 u32 addnl_dvdr = 1; 1211 u32 coal_intvl = 0; 1212 struct cpsw_common *cpsw = priv->cpsw; 1213 1214 coal_intvl = coal->rx_coalesce_usecs; 1215 1216 int_ctrl = readl(&cpsw->wr_regs->int_control); 1217 prescale = cpsw->bus_freq_mhz * 4; 1218 1219 if (!coal->rx_coalesce_usecs) { 1220 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN); 1221 goto update_return; 1222 } 1223 1224 if (coal_intvl < CPSW_CMINTMIN_INTVL) 1225 coal_intvl = CPSW_CMINTMIN_INTVL; 1226 1227 if (coal_intvl > CPSW_CMINTMAX_INTVL) { 1228 /* Interrupt pacer works with 4us Pulse, we can 1229 * throttle further by dilating the 4us pulse. 1230 */ 1231 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale; 1232 1233 if (addnl_dvdr > 1) { 1234 prescale *= addnl_dvdr; 1235 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr)) 1236 coal_intvl = (CPSW_CMINTMAX_INTVL 1237 * addnl_dvdr); 1238 } else { 1239 addnl_dvdr = 1; 1240 coal_intvl = CPSW_CMINTMAX_INTVL; 1241 } 1242 } 1243 1244 num_interrupts = (1000 * addnl_dvdr) / coal_intvl; 1245 writel(num_interrupts, &cpsw->wr_regs->rx_imax); 1246 writel(num_interrupts, &cpsw->wr_regs->tx_imax); 1247 1248 int_ctrl |= CPSW_INTPACEEN; 1249 int_ctrl &= (~CPSW_INTPRESCALE_MASK); 1250 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK); 1251 1252 update_return: 1253 writel(int_ctrl, &cpsw->wr_regs->int_control); 1254 1255 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl); 1256 cpsw->coal_intvl = coal_intvl; 1257 1258 return 0; 1259 } 1260 1261 static int cpsw_get_sset_count(struct net_device *ndev, int sset) 1262 { 1263 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1264 1265 switch (sset) { 1266 case ETH_SS_STATS: 1267 return (CPSW_STATS_COMMON_LEN + 1268 (cpsw->rx_ch_num + cpsw->tx_ch_num) * 1269 CPSW_STATS_CH_LEN); 1270 default: 1271 return -EOPNOTSUPP; 1272 } 1273 } 1274 1275 static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir) 1276 { 1277 int ch_stats_len; 1278 int line; 1279 int i; 1280 1281 ch_stats_len = CPSW_STATS_CH_LEN * ch_num; 1282 for (i = 0; i < ch_stats_len; i++) { 1283 line = i % CPSW_STATS_CH_LEN; 1284 snprintf(*p, ETH_GSTRING_LEN, 1285 "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx", 1286 (long)(i / CPSW_STATS_CH_LEN), 1287 cpsw_gstrings_ch_stats[line].stat_string); 1288 *p += ETH_GSTRING_LEN; 1289 } 1290 } 1291 1292 static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data) 1293 { 1294 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1295 u8 *p = data; 1296 int i; 1297 1298 switch (stringset) { 1299 case ETH_SS_STATS: 1300 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) { 1301 memcpy(p, cpsw_gstrings_stats[i].stat_string, 1302 ETH_GSTRING_LEN); 1303 p += ETH_GSTRING_LEN; 1304 } 1305 1306 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1); 1307 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0); 1308 break; 1309 } 1310 } 1311 1312 static void cpsw_get_ethtool_stats(struct net_device *ndev, 1313 struct ethtool_stats *stats, u64 *data) 1314 { 1315 u8 *p; 1316 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 1317 struct cpdma_chan_stats ch_stats; 1318 int i, l, ch; 1319 1320 /* Collect Davinci CPDMA stats for Rx and Tx Channel */ 1321 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++) 1322 data[l] = readl(cpsw->hw_stats + 1323 cpsw_gstrings_stats[l].stat_offset); 1324 1325 for (ch = 0; ch < cpsw->rx_ch_num; ch++) { 1326 cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats); 1327 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) { 1328 p = (u8 *)&ch_stats + 1329 cpsw_gstrings_ch_stats[i].stat_offset; 1330 data[l] = *(u32 *)p; 1331 } 1332 } 1333 1334 for (ch = 0; ch < cpsw->tx_ch_num; ch++) { 1335 cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats); 1336 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) { 1337 p = (u8 *)&ch_stats + 1338 cpsw_gstrings_ch_stats[i].stat_offset; 1339 data[l] = *(u32 *)p; 1340 } 1341 } 1342 } 1343 1344 static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv, 1345 struct sk_buff *skb, 1346 struct cpdma_chan *txch) 1347 { 1348 struct cpsw_common *cpsw = priv->cpsw; 1349 1350 skb_tx_timestamp(skb); 1351 return cpdma_chan_submit(txch, skb, skb->data, skb->len, 1352 priv->emac_port + cpsw->data.dual_emac); 1353 } 1354 1355 static inline void cpsw_add_dual_emac_def_ale_entries( 1356 struct cpsw_priv *priv, struct cpsw_slave *slave, 1357 u32 slave_port) 1358 { 1359 struct cpsw_common *cpsw = priv->cpsw; 1360 u32 port_mask = 1 << slave_port | ALE_PORT_HOST; 1361 1362 if (cpsw->version == CPSW_VERSION_1) 1363 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN); 1364 else 1365 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN); 1366 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask, 1367 port_mask, port_mask, 0); 1368 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 1369 port_mask, ALE_VLAN, slave->port_vlan, 0); 1370 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, 1371 HOST_PORT_NUM, ALE_VLAN | 1372 ALE_SECURE, slave->port_vlan); 1373 cpsw_ale_control_set(cpsw->ale, slave_port, 1374 ALE_PORT_DROP_UNKNOWN_VLAN, 1); 1375 } 1376 1377 static void soft_reset_slave(struct cpsw_slave *slave) 1378 { 1379 char name[32]; 1380 1381 snprintf(name, sizeof(name), "slave-%d", slave->slave_num); 1382 soft_reset(name, &slave->sliver->soft_reset); 1383 } 1384 1385 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv) 1386 { 1387 u32 slave_port; 1388 struct phy_device *phy; 1389 struct cpsw_common *cpsw = priv->cpsw; 1390 1391 soft_reset_slave(slave); 1392 1393 /* setup priority mapping */ 1394 writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map); 1395 1396 switch (cpsw->version) { 1397 case CPSW_VERSION_1: 1398 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP); 1399 /* Increase RX FIFO size to 5 for supporting fullduplex 1400 * flow control mode 1401 */ 1402 slave_write(slave, 1403 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) | 1404 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS); 1405 break; 1406 case CPSW_VERSION_2: 1407 case CPSW_VERSION_3: 1408 case CPSW_VERSION_4: 1409 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP); 1410 /* Increase RX FIFO size to 5 for supporting fullduplex 1411 * flow control mode 1412 */ 1413 slave_write(slave, 1414 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) | 1415 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS); 1416 break; 1417 } 1418 1419 /* setup max packet size, and mac address */ 1420 writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen); 1421 cpsw_set_slave_mac(slave, priv); 1422 1423 slave->mac_control = 0; /* no link yet */ 1424 1425 slave_port = cpsw_get_slave_port(slave->slave_num); 1426 1427 if (cpsw->data.dual_emac) 1428 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port); 1429 else 1430 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 1431 1 << slave_port, 0, 0, ALE_MCAST_FWD_2); 1432 1433 if (slave->data->phy_node) { 1434 phy = of_phy_connect(priv->ndev, slave->data->phy_node, 1435 &cpsw_adjust_link, 0, slave->data->phy_if); 1436 if (!phy) { 1437 dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n", 1438 slave->data->phy_node, 1439 slave->slave_num); 1440 return; 1441 } 1442 } else { 1443 phy = phy_connect(priv->ndev, slave->data->phy_id, 1444 &cpsw_adjust_link, slave->data->phy_if); 1445 if (IS_ERR(phy)) { 1446 dev_err(priv->dev, 1447 "phy \"%s\" not found on slave %d, err %ld\n", 1448 slave->data->phy_id, slave->slave_num, 1449 PTR_ERR(phy)); 1450 return; 1451 } 1452 } 1453 1454 slave->phy = phy; 1455 1456 phy_attached_info(slave->phy); 1457 1458 phy_start(slave->phy); 1459 1460 /* Configure GMII_SEL register */ 1461 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num); 1462 } 1463 1464 static inline void cpsw_add_default_vlan(struct cpsw_priv *priv) 1465 { 1466 struct cpsw_common *cpsw = priv->cpsw; 1467 const int vlan = cpsw->data.default_vlan; 1468 u32 reg; 1469 int i; 1470 int unreg_mcast_mask; 1471 1472 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN : 1473 CPSW2_PORT_VLAN; 1474 1475 writel(vlan, &cpsw->host_port_regs->port_vlan); 1476 1477 for (i = 0; i < cpsw->data.slaves; i++) 1478 slave_write(cpsw->slaves + i, vlan, reg); 1479 1480 if (priv->ndev->flags & IFF_ALLMULTI) 1481 unreg_mcast_mask = ALE_ALL_PORTS; 1482 else 1483 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2; 1484 1485 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS, 1486 ALE_ALL_PORTS, ALE_ALL_PORTS, 1487 unreg_mcast_mask); 1488 } 1489 1490 static void cpsw_init_host_port(struct cpsw_priv *priv) 1491 { 1492 u32 fifo_mode; 1493 u32 control_reg; 1494 struct cpsw_common *cpsw = priv->cpsw; 1495 1496 /* soft reset the controller and initialize ale */ 1497 soft_reset("cpsw", &cpsw->regs->soft_reset); 1498 cpsw_ale_start(cpsw->ale); 1499 1500 /* switch to vlan unaware mode */ 1501 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE, 1502 CPSW_ALE_VLAN_AWARE); 1503 control_reg = readl(&cpsw->regs->control); 1504 control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP; 1505 writel(control_reg, &cpsw->regs->control); 1506 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE : 1507 CPSW_FIFO_NORMAL_MODE; 1508 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl); 1509 1510 /* setup host port priority mapping */ 1511 writel_relaxed(CPDMA_TX_PRIORITY_MAP, 1512 &cpsw->host_port_regs->cpdma_tx_pri_map); 1513 writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map); 1514 1515 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, 1516 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD); 1517 1518 if (!cpsw->data.dual_emac) { 1519 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM, 1520 0, 0); 1521 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 1522 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2); 1523 } 1524 } 1525 1526 static int cpsw_fill_rx_channels(struct cpsw_priv *priv) 1527 { 1528 struct cpsw_common *cpsw = priv->cpsw; 1529 struct sk_buff *skb; 1530 int ch_buf_num; 1531 int ch, i, ret; 1532 1533 for (ch = 0; ch < cpsw->rx_ch_num; ch++) { 1534 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch); 1535 for (i = 0; i < ch_buf_num; i++) { 1536 skb = __netdev_alloc_skb_ip_align(priv->ndev, 1537 cpsw->rx_packet_max, 1538 GFP_KERNEL); 1539 if (!skb) { 1540 cpsw_err(priv, ifup, "cannot allocate skb\n"); 1541 return -ENOMEM; 1542 } 1543 1544 skb_set_queue_mapping(skb, ch); 1545 ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb, 1546 skb->data, skb_tailroom(skb), 1547 0); 1548 if (ret < 0) { 1549 cpsw_err(priv, ifup, 1550 "cannot submit skb to channel %d rx, error %d\n", 1551 ch, ret); 1552 kfree_skb(skb); 1553 return ret; 1554 } 1555 kmemleak_not_leak(skb); 1556 } 1557 1558 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n", 1559 ch, ch_buf_num); 1560 } 1561 1562 return 0; 1563 } 1564 1565 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw) 1566 { 1567 u32 slave_port; 1568 1569 slave_port = cpsw_get_slave_port(slave->slave_num); 1570 1571 if (!slave->phy) 1572 return; 1573 phy_stop(slave->phy); 1574 phy_disconnect(slave->phy); 1575 slave->phy = NULL; 1576 cpsw_ale_control_set(cpsw->ale, slave_port, 1577 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE); 1578 soft_reset_slave(slave); 1579 } 1580 1581 static int cpsw_ndo_open(struct net_device *ndev) 1582 { 1583 struct cpsw_priv *priv = netdev_priv(ndev); 1584 struct cpsw_common *cpsw = priv->cpsw; 1585 int ret; 1586 u32 reg; 1587 1588 ret = pm_runtime_get_sync(cpsw->dev); 1589 if (ret < 0) { 1590 pm_runtime_put_noidle(cpsw->dev); 1591 return ret; 1592 } 1593 1594 netif_carrier_off(ndev); 1595 1596 /* Notify the stack of the actual queue counts. */ 1597 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num); 1598 if (ret) { 1599 dev_err(priv->dev, "cannot set real number of tx queues\n"); 1600 goto err_cleanup; 1601 } 1602 1603 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num); 1604 if (ret) { 1605 dev_err(priv->dev, "cannot set real number of rx queues\n"); 1606 goto err_cleanup; 1607 } 1608 1609 reg = cpsw->version; 1610 1611 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n", 1612 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg), 1613 CPSW_RTL_VERSION(reg)); 1614 1615 /* Initialize host and slave ports */ 1616 if (!cpsw->usage_count) 1617 cpsw_init_host_port(priv); 1618 for_each_slave(priv, cpsw_slave_open, priv); 1619 1620 /* Add default VLAN */ 1621 if (!cpsw->data.dual_emac) 1622 cpsw_add_default_vlan(priv); 1623 else 1624 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan, 1625 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0); 1626 1627 /* initialize shared resources for every ndev */ 1628 if (!cpsw->usage_count) { 1629 /* disable priority elevation */ 1630 writel_relaxed(0, &cpsw->regs->ptype); 1631 1632 /* enable statistics collection only on all ports */ 1633 writel_relaxed(0x7, &cpsw->regs->stat_port_en); 1634 1635 /* Enable internal fifo flow control */ 1636 writel(0x7, &cpsw->regs->flow_control); 1637 1638 napi_enable(&cpsw->napi_rx); 1639 napi_enable(&cpsw->napi_tx); 1640 1641 if (cpsw->tx_irq_disabled) { 1642 cpsw->tx_irq_disabled = false; 1643 enable_irq(cpsw->irqs_table[1]); 1644 } 1645 1646 if (cpsw->rx_irq_disabled) { 1647 cpsw->rx_irq_disabled = false; 1648 enable_irq(cpsw->irqs_table[0]); 1649 } 1650 1651 ret = cpsw_fill_rx_channels(priv); 1652 if (ret < 0) 1653 goto err_cleanup; 1654 1655 if (cpts_register(cpsw->cpts)) 1656 dev_err(priv->dev, "error registering cpts device\n"); 1657 1658 } 1659 1660 /* Enable Interrupt pacing if configured */ 1661 if (cpsw->coal_intvl != 0) { 1662 struct ethtool_coalesce coal; 1663 1664 coal.rx_coalesce_usecs = cpsw->coal_intvl; 1665 cpsw_set_coalesce(ndev, &coal); 1666 } 1667 1668 cpdma_ctlr_start(cpsw->dma); 1669 cpsw_intr_enable(cpsw); 1670 cpsw->usage_count++; 1671 1672 return 0; 1673 1674 err_cleanup: 1675 cpdma_ctlr_stop(cpsw->dma); 1676 for_each_slave(priv, cpsw_slave_stop, cpsw); 1677 pm_runtime_put_sync(cpsw->dev); 1678 netif_carrier_off(priv->ndev); 1679 return ret; 1680 } 1681 1682 static int cpsw_ndo_stop(struct net_device *ndev) 1683 { 1684 struct cpsw_priv *priv = netdev_priv(ndev); 1685 struct cpsw_common *cpsw = priv->cpsw; 1686 1687 cpsw_info(priv, ifdown, "shutting down cpsw device\n"); 1688 netif_tx_stop_all_queues(priv->ndev); 1689 netif_carrier_off(priv->ndev); 1690 1691 if (cpsw->usage_count <= 1) { 1692 napi_disable(&cpsw->napi_rx); 1693 napi_disable(&cpsw->napi_tx); 1694 cpts_unregister(cpsw->cpts); 1695 cpsw_intr_disable(cpsw); 1696 cpdma_ctlr_stop(cpsw->dma); 1697 cpsw_ale_stop(cpsw->ale); 1698 } 1699 for_each_slave(priv, cpsw_slave_stop, cpsw); 1700 1701 if (cpsw_need_resplit(cpsw)) 1702 cpsw_split_res(ndev); 1703 1704 cpsw->usage_count--; 1705 pm_runtime_put_sync(cpsw->dev); 1706 return 0; 1707 } 1708 1709 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb, 1710 struct net_device *ndev) 1711 { 1712 struct cpsw_priv *priv = netdev_priv(ndev); 1713 struct cpsw_common *cpsw = priv->cpsw; 1714 struct cpts *cpts = cpsw->cpts; 1715 struct netdev_queue *txq; 1716 struct cpdma_chan *txch; 1717 int ret, q_idx; 1718 1719 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) { 1720 cpsw_err(priv, tx_err, "packet pad failed\n"); 1721 ndev->stats.tx_dropped++; 1722 return NET_XMIT_DROP; 1723 } 1724 1725 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP && 1726 cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb)) 1727 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS; 1728 1729 q_idx = skb_get_queue_mapping(skb); 1730 if (q_idx >= cpsw->tx_ch_num) 1731 q_idx = q_idx % cpsw->tx_ch_num; 1732 1733 txch = cpsw->txv[q_idx].ch; 1734 txq = netdev_get_tx_queue(ndev, q_idx); 1735 ret = cpsw_tx_packet_submit(priv, skb, txch); 1736 if (unlikely(ret != 0)) { 1737 cpsw_err(priv, tx_err, "desc submit failed\n"); 1738 goto fail; 1739 } 1740 1741 /* If there is no more tx desc left free then we need to 1742 * tell the kernel to stop sending us tx frames. 1743 */ 1744 if (unlikely(!cpdma_check_free_tx_desc(txch))) { 1745 netif_tx_stop_queue(txq); 1746 1747 /* Barrier, so that stop_queue visible to other cpus */ 1748 smp_mb__after_atomic(); 1749 1750 if (cpdma_check_free_tx_desc(txch)) 1751 netif_tx_wake_queue(txq); 1752 } 1753 1754 return NETDEV_TX_OK; 1755 fail: 1756 ndev->stats.tx_dropped++; 1757 netif_tx_stop_queue(txq); 1758 1759 /* Barrier, so that stop_queue visible to other cpus */ 1760 smp_mb__after_atomic(); 1761 1762 if (cpdma_check_free_tx_desc(txch)) 1763 netif_tx_wake_queue(txq); 1764 1765 return NETDEV_TX_BUSY; 1766 } 1767 1768 #if IS_ENABLED(CONFIG_TI_CPTS) 1769 1770 static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw) 1771 { 1772 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave]; 1773 u32 ts_en, seq_id; 1774 1775 if (!cpts_is_tx_enabled(cpsw->cpts) && 1776 !cpts_is_rx_enabled(cpsw->cpts)) { 1777 slave_write(slave, 0, CPSW1_TS_CTL); 1778 return; 1779 } 1780 1781 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588; 1782 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS; 1783 1784 if (cpts_is_tx_enabled(cpsw->cpts)) 1785 ts_en |= CPSW_V1_TS_TX_EN; 1786 1787 if (cpts_is_rx_enabled(cpsw->cpts)) 1788 ts_en |= CPSW_V1_TS_RX_EN; 1789 1790 slave_write(slave, ts_en, CPSW1_TS_CTL); 1791 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE); 1792 } 1793 1794 static void cpsw_hwtstamp_v2(struct cpsw_priv *priv) 1795 { 1796 struct cpsw_slave *slave; 1797 struct cpsw_common *cpsw = priv->cpsw; 1798 u32 ctrl, mtype; 1799 1800 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)]; 1801 1802 ctrl = slave_read(slave, CPSW2_CONTROL); 1803 switch (cpsw->version) { 1804 case CPSW_VERSION_2: 1805 ctrl &= ~CTRL_V2_ALL_TS_MASK; 1806 1807 if (cpts_is_tx_enabled(cpsw->cpts)) 1808 ctrl |= CTRL_V2_TX_TS_BITS; 1809 1810 if (cpts_is_rx_enabled(cpsw->cpts)) 1811 ctrl |= CTRL_V2_RX_TS_BITS; 1812 break; 1813 case CPSW_VERSION_3: 1814 default: 1815 ctrl &= ~CTRL_V3_ALL_TS_MASK; 1816 1817 if (cpts_is_tx_enabled(cpsw->cpts)) 1818 ctrl |= CTRL_V3_TX_TS_BITS; 1819 1820 if (cpts_is_rx_enabled(cpsw->cpts)) 1821 ctrl |= CTRL_V3_RX_TS_BITS; 1822 break; 1823 } 1824 1825 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS; 1826 1827 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE); 1828 slave_write(slave, ctrl, CPSW2_CONTROL); 1829 writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype); 1830 } 1831 1832 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr) 1833 { 1834 struct cpsw_priv *priv = netdev_priv(dev); 1835 struct hwtstamp_config cfg; 1836 struct cpsw_common *cpsw = priv->cpsw; 1837 struct cpts *cpts = cpsw->cpts; 1838 1839 if (cpsw->version != CPSW_VERSION_1 && 1840 cpsw->version != CPSW_VERSION_2 && 1841 cpsw->version != CPSW_VERSION_3) 1842 return -EOPNOTSUPP; 1843 1844 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) 1845 return -EFAULT; 1846 1847 /* reserved for future extensions */ 1848 if (cfg.flags) 1849 return -EINVAL; 1850 1851 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON) 1852 return -ERANGE; 1853 1854 switch (cfg.rx_filter) { 1855 case HWTSTAMP_FILTER_NONE: 1856 cpts_rx_enable(cpts, 0); 1857 break; 1858 case HWTSTAMP_FILTER_ALL: 1859 case HWTSTAMP_FILTER_NTP_ALL: 1860 return -ERANGE; 1861 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 1862 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 1863 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 1864 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT); 1865 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT; 1866 break; 1867 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 1868 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 1869 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 1870 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 1871 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 1872 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 1873 case HWTSTAMP_FILTER_PTP_V2_EVENT: 1874 case HWTSTAMP_FILTER_PTP_V2_SYNC: 1875 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 1876 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT); 1877 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; 1878 break; 1879 default: 1880 return -ERANGE; 1881 } 1882 1883 cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON); 1884 1885 switch (cpsw->version) { 1886 case CPSW_VERSION_1: 1887 cpsw_hwtstamp_v1(cpsw); 1888 break; 1889 case CPSW_VERSION_2: 1890 case CPSW_VERSION_3: 1891 cpsw_hwtstamp_v2(priv); 1892 break; 1893 default: 1894 WARN_ON(1); 1895 } 1896 1897 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1898 } 1899 1900 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr) 1901 { 1902 struct cpsw_common *cpsw = ndev_to_cpsw(dev); 1903 struct cpts *cpts = cpsw->cpts; 1904 struct hwtstamp_config cfg; 1905 1906 if (cpsw->version != CPSW_VERSION_1 && 1907 cpsw->version != CPSW_VERSION_2 && 1908 cpsw->version != CPSW_VERSION_3) 1909 return -EOPNOTSUPP; 1910 1911 cfg.flags = 0; 1912 cfg.tx_type = cpts_is_tx_enabled(cpts) ? 1913 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF; 1914 cfg.rx_filter = (cpts_is_rx_enabled(cpts) ? 1915 cpts->rx_enable : HWTSTAMP_FILTER_NONE); 1916 1917 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0; 1918 } 1919 #else 1920 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr) 1921 { 1922 return -EOPNOTSUPP; 1923 } 1924 1925 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr) 1926 { 1927 return -EOPNOTSUPP; 1928 } 1929 #endif /*CONFIG_TI_CPTS*/ 1930 1931 static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd) 1932 { 1933 struct cpsw_priv *priv = netdev_priv(dev); 1934 struct cpsw_common *cpsw = priv->cpsw; 1935 int slave_no = cpsw_slave_index(cpsw, priv); 1936 1937 if (!netif_running(dev)) 1938 return -EINVAL; 1939 1940 switch (cmd) { 1941 case SIOCSHWTSTAMP: 1942 return cpsw_hwtstamp_set(dev, req); 1943 case SIOCGHWTSTAMP: 1944 return cpsw_hwtstamp_get(dev, req); 1945 } 1946 1947 if (!cpsw->slaves[slave_no].phy) 1948 return -EOPNOTSUPP; 1949 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd); 1950 } 1951 1952 static void cpsw_ndo_tx_timeout(struct net_device *ndev) 1953 { 1954 struct cpsw_priv *priv = netdev_priv(ndev); 1955 struct cpsw_common *cpsw = priv->cpsw; 1956 int ch; 1957 1958 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n"); 1959 ndev->stats.tx_errors++; 1960 cpsw_intr_disable(cpsw); 1961 for (ch = 0; ch < cpsw->tx_ch_num; ch++) { 1962 cpdma_chan_stop(cpsw->txv[ch].ch); 1963 cpdma_chan_start(cpsw->txv[ch].ch); 1964 } 1965 1966 cpsw_intr_enable(cpsw); 1967 netif_trans_update(ndev); 1968 netif_tx_wake_all_queues(ndev); 1969 } 1970 1971 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p) 1972 { 1973 struct cpsw_priv *priv = netdev_priv(ndev); 1974 struct sockaddr *addr = (struct sockaddr *)p; 1975 struct cpsw_common *cpsw = priv->cpsw; 1976 int flags = 0; 1977 u16 vid = 0; 1978 int ret; 1979 1980 if (!is_valid_ether_addr(addr->sa_data)) 1981 return -EADDRNOTAVAIL; 1982 1983 ret = pm_runtime_get_sync(cpsw->dev); 1984 if (ret < 0) { 1985 pm_runtime_put_noidle(cpsw->dev); 1986 return ret; 1987 } 1988 1989 if (cpsw->data.dual_emac) { 1990 vid = cpsw->slaves[priv->emac_port].port_vlan; 1991 flags = ALE_VLAN; 1992 } 1993 1994 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM, 1995 flags, vid); 1996 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM, 1997 flags, vid); 1998 1999 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN); 2000 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN); 2001 for_each_slave(priv, cpsw_set_slave_mac, priv); 2002 2003 pm_runtime_put(cpsw->dev); 2004 2005 return 0; 2006 } 2007 2008 #ifdef CONFIG_NET_POLL_CONTROLLER 2009 static void cpsw_ndo_poll_controller(struct net_device *ndev) 2010 { 2011 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2012 2013 cpsw_intr_disable(cpsw); 2014 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw); 2015 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw); 2016 cpsw_intr_enable(cpsw); 2017 } 2018 #endif 2019 2020 static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv, 2021 unsigned short vid) 2022 { 2023 int ret; 2024 int unreg_mcast_mask = 0; 2025 u32 port_mask; 2026 struct cpsw_common *cpsw = priv->cpsw; 2027 2028 if (cpsw->data.dual_emac) { 2029 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST; 2030 2031 if (priv->ndev->flags & IFF_ALLMULTI) 2032 unreg_mcast_mask = port_mask; 2033 } else { 2034 port_mask = ALE_ALL_PORTS; 2035 2036 if (priv->ndev->flags & IFF_ALLMULTI) 2037 unreg_mcast_mask = ALE_ALL_PORTS; 2038 else 2039 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2; 2040 } 2041 2042 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask, 2043 unreg_mcast_mask); 2044 if (ret != 0) 2045 return ret; 2046 2047 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, 2048 HOST_PORT_NUM, ALE_VLAN, vid); 2049 if (ret != 0) 2050 goto clean_vid; 2051 2052 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast, 2053 port_mask, ALE_VLAN, vid, 0); 2054 if (ret != 0) 2055 goto clean_vlan_ucast; 2056 return 0; 2057 2058 clean_vlan_ucast: 2059 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, 2060 HOST_PORT_NUM, ALE_VLAN, vid); 2061 clean_vid: 2062 cpsw_ale_del_vlan(cpsw->ale, vid, 0); 2063 return ret; 2064 } 2065 2066 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev, 2067 __be16 proto, u16 vid) 2068 { 2069 struct cpsw_priv *priv = netdev_priv(ndev); 2070 struct cpsw_common *cpsw = priv->cpsw; 2071 int ret; 2072 2073 if (vid == cpsw->data.default_vlan) 2074 return 0; 2075 2076 ret = pm_runtime_get_sync(cpsw->dev); 2077 if (ret < 0) { 2078 pm_runtime_put_noidle(cpsw->dev); 2079 return ret; 2080 } 2081 2082 if (cpsw->data.dual_emac) { 2083 /* In dual EMAC, reserved VLAN id should not be used for 2084 * creating VLAN interfaces as this can break the dual 2085 * EMAC port separation 2086 */ 2087 int i; 2088 2089 for (i = 0; i < cpsw->data.slaves; i++) { 2090 if (vid == cpsw->slaves[i].port_vlan) 2091 return -EINVAL; 2092 } 2093 } 2094 2095 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid); 2096 ret = cpsw_add_vlan_ale_entry(priv, vid); 2097 2098 pm_runtime_put(cpsw->dev); 2099 return ret; 2100 } 2101 2102 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev, 2103 __be16 proto, u16 vid) 2104 { 2105 struct cpsw_priv *priv = netdev_priv(ndev); 2106 struct cpsw_common *cpsw = priv->cpsw; 2107 int ret; 2108 2109 if (vid == cpsw->data.default_vlan) 2110 return 0; 2111 2112 ret = pm_runtime_get_sync(cpsw->dev); 2113 if (ret < 0) { 2114 pm_runtime_put_noidle(cpsw->dev); 2115 return ret; 2116 } 2117 2118 if (cpsw->data.dual_emac) { 2119 int i; 2120 2121 for (i = 0; i < cpsw->data.slaves; i++) { 2122 if (vid == cpsw->slaves[i].port_vlan) 2123 return -EINVAL; 2124 } 2125 } 2126 2127 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid); 2128 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0); 2129 if (ret != 0) 2130 return ret; 2131 2132 ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, 2133 HOST_PORT_NUM, ALE_VLAN, vid); 2134 if (ret != 0) 2135 return ret; 2136 2137 ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast, 2138 0, ALE_VLAN, vid); 2139 pm_runtime_put(cpsw->dev); 2140 return ret; 2141 } 2142 2143 static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate) 2144 { 2145 struct cpsw_priv *priv = netdev_priv(ndev); 2146 struct cpsw_common *cpsw = priv->cpsw; 2147 struct cpsw_slave *slave; 2148 u32 min_rate; 2149 u32 ch_rate; 2150 int i, ret; 2151 2152 ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate; 2153 if (ch_rate == rate) 2154 return 0; 2155 2156 ch_rate = rate * 1000; 2157 min_rate = cpdma_chan_get_min_rate(cpsw->dma); 2158 if ((ch_rate < min_rate && ch_rate)) { 2159 dev_err(priv->dev, "The channel rate cannot be less than %dMbps", 2160 min_rate); 2161 return -EINVAL; 2162 } 2163 2164 if (rate > cpsw->speed) { 2165 dev_err(priv->dev, "The channel rate cannot be more than 2Gbps"); 2166 return -EINVAL; 2167 } 2168 2169 ret = pm_runtime_get_sync(cpsw->dev); 2170 if (ret < 0) { 2171 pm_runtime_put_noidle(cpsw->dev); 2172 return ret; 2173 } 2174 2175 ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate); 2176 pm_runtime_put(cpsw->dev); 2177 2178 if (ret) 2179 return ret; 2180 2181 /* update rates for slaves tx queues */ 2182 for (i = 0; i < cpsw->data.slaves; i++) { 2183 slave = &cpsw->slaves[i]; 2184 if (!slave->ndev) 2185 continue; 2186 2187 netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate; 2188 } 2189 2190 cpsw_split_res(ndev); 2191 return ret; 2192 } 2193 2194 static const struct net_device_ops cpsw_netdev_ops = { 2195 .ndo_open = cpsw_ndo_open, 2196 .ndo_stop = cpsw_ndo_stop, 2197 .ndo_start_xmit = cpsw_ndo_start_xmit, 2198 .ndo_set_mac_address = cpsw_ndo_set_mac_address, 2199 .ndo_do_ioctl = cpsw_ndo_ioctl, 2200 .ndo_validate_addr = eth_validate_addr, 2201 .ndo_tx_timeout = cpsw_ndo_tx_timeout, 2202 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode, 2203 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate, 2204 #ifdef CONFIG_NET_POLL_CONTROLLER 2205 .ndo_poll_controller = cpsw_ndo_poll_controller, 2206 #endif 2207 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid, 2208 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid, 2209 }; 2210 2211 static int cpsw_get_regs_len(struct net_device *ndev) 2212 { 2213 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2214 2215 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32); 2216 } 2217 2218 static void cpsw_get_regs(struct net_device *ndev, 2219 struct ethtool_regs *regs, void *p) 2220 { 2221 u32 *reg = p; 2222 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2223 2224 /* update CPSW IP version */ 2225 regs->version = cpsw->version; 2226 2227 cpsw_ale_dump(cpsw->ale, reg); 2228 } 2229 2230 static void cpsw_get_drvinfo(struct net_device *ndev, 2231 struct ethtool_drvinfo *info) 2232 { 2233 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2234 struct platform_device *pdev = to_platform_device(cpsw->dev); 2235 2236 strlcpy(info->driver, "cpsw", sizeof(info->driver)); 2237 strlcpy(info->version, "1.0", sizeof(info->version)); 2238 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info)); 2239 } 2240 2241 static u32 cpsw_get_msglevel(struct net_device *ndev) 2242 { 2243 struct cpsw_priv *priv = netdev_priv(ndev); 2244 return priv->msg_enable; 2245 } 2246 2247 static void cpsw_set_msglevel(struct net_device *ndev, u32 value) 2248 { 2249 struct cpsw_priv *priv = netdev_priv(ndev); 2250 priv->msg_enable = value; 2251 } 2252 2253 #if IS_ENABLED(CONFIG_TI_CPTS) 2254 static int cpsw_get_ts_info(struct net_device *ndev, 2255 struct ethtool_ts_info *info) 2256 { 2257 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2258 2259 info->so_timestamping = 2260 SOF_TIMESTAMPING_TX_HARDWARE | 2261 SOF_TIMESTAMPING_TX_SOFTWARE | 2262 SOF_TIMESTAMPING_RX_HARDWARE | 2263 SOF_TIMESTAMPING_RX_SOFTWARE | 2264 SOF_TIMESTAMPING_SOFTWARE | 2265 SOF_TIMESTAMPING_RAW_HARDWARE; 2266 info->phc_index = cpsw->cpts->phc_index; 2267 info->tx_types = 2268 (1 << HWTSTAMP_TX_OFF) | 2269 (1 << HWTSTAMP_TX_ON); 2270 info->rx_filters = 2271 (1 << HWTSTAMP_FILTER_NONE) | 2272 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) | 2273 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT); 2274 return 0; 2275 } 2276 #else 2277 static int cpsw_get_ts_info(struct net_device *ndev, 2278 struct ethtool_ts_info *info) 2279 { 2280 info->so_timestamping = 2281 SOF_TIMESTAMPING_TX_SOFTWARE | 2282 SOF_TIMESTAMPING_RX_SOFTWARE | 2283 SOF_TIMESTAMPING_SOFTWARE; 2284 info->phc_index = -1; 2285 info->tx_types = 0; 2286 info->rx_filters = 0; 2287 return 0; 2288 } 2289 #endif 2290 2291 static int cpsw_get_link_ksettings(struct net_device *ndev, 2292 struct ethtool_link_ksettings *ecmd) 2293 { 2294 struct cpsw_priv *priv = netdev_priv(ndev); 2295 struct cpsw_common *cpsw = priv->cpsw; 2296 int slave_no = cpsw_slave_index(cpsw, priv); 2297 2298 if (!cpsw->slaves[slave_no].phy) 2299 return -EOPNOTSUPP; 2300 2301 phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd); 2302 return 0; 2303 } 2304 2305 static int cpsw_set_link_ksettings(struct net_device *ndev, 2306 const struct ethtool_link_ksettings *ecmd) 2307 { 2308 struct cpsw_priv *priv = netdev_priv(ndev); 2309 struct cpsw_common *cpsw = priv->cpsw; 2310 int slave_no = cpsw_slave_index(cpsw, priv); 2311 2312 if (cpsw->slaves[slave_no].phy) 2313 return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy, 2314 ecmd); 2315 else 2316 return -EOPNOTSUPP; 2317 } 2318 2319 static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2320 { 2321 struct cpsw_priv *priv = netdev_priv(ndev); 2322 struct cpsw_common *cpsw = priv->cpsw; 2323 int slave_no = cpsw_slave_index(cpsw, priv); 2324 2325 wol->supported = 0; 2326 wol->wolopts = 0; 2327 2328 if (cpsw->slaves[slave_no].phy) 2329 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol); 2330 } 2331 2332 static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol) 2333 { 2334 struct cpsw_priv *priv = netdev_priv(ndev); 2335 struct cpsw_common *cpsw = priv->cpsw; 2336 int slave_no = cpsw_slave_index(cpsw, priv); 2337 2338 if (cpsw->slaves[slave_no].phy) 2339 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol); 2340 else 2341 return -EOPNOTSUPP; 2342 } 2343 2344 static void cpsw_get_pauseparam(struct net_device *ndev, 2345 struct ethtool_pauseparam *pause) 2346 { 2347 struct cpsw_priv *priv = netdev_priv(ndev); 2348 2349 pause->autoneg = AUTONEG_DISABLE; 2350 pause->rx_pause = priv->rx_pause ? true : false; 2351 pause->tx_pause = priv->tx_pause ? true : false; 2352 } 2353 2354 static int cpsw_set_pauseparam(struct net_device *ndev, 2355 struct ethtool_pauseparam *pause) 2356 { 2357 struct cpsw_priv *priv = netdev_priv(ndev); 2358 bool link; 2359 2360 priv->rx_pause = pause->rx_pause ? true : false; 2361 priv->tx_pause = pause->tx_pause ? true : false; 2362 2363 for_each_slave(priv, _cpsw_adjust_link, priv, &link); 2364 return 0; 2365 } 2366 2367 static int cpsw_ethtool_op_begin(struct net_device *ndev) 2368 { 2369 struct cpsw_priv *priv = netdev_priv(ndev); 2370 struct cpsw_common *cpsw = priv->cpsw; 2371 int ret; 2372 2373 ret = pm_runtime_get_sync(cpsw->dev); 2374 if (ret < 0) { 2375 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret); 2376 pm_runtime_put_noidle(cpsw->dev); 2377 } 2378 2379 return ret; 2380 } 2381 2382 static void cpsw_ethtool_op_complete(struct net_device *ndev) 2383 { 2384 struct cpsw_priv *priv = netdev_priv(ndev); 2385 int ret; 2386 2387 ret = pm_runtime_put(priv->cpsw->dev); 2388 if (ret < 0) 2389 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret); 2390 } 2391 2392 static void cpsw_get_channels(struct net_device *ndev, 2393 struct ethtool_channels *ch) 2394 { 2395 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2396 2397 ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES; 2398 ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES; 2399 ch->max_combined = 0; 2400 ch->max_other = 0; 2401 ch->other_count = 0; 2402 ch->rx_count = cpsw->rx_ch_num; 2403 ch->tx_count = cpsw->tx_ch_num; 2404 ch->combined_count = 0; 2405 } 2406 2407 static int cpsw_check_ch_settings(struct cpsw_common *cpsw, 2408 struct ethtool_channels *ch) 2409 { 2410 if (cpsw->quirk_irq) { 2411 dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed"); 2412 return -EOPNOTSUPP; 2413 } 2414 2415 if (ch->combined_count) 2416 return -EINVAL; 2417 2418 /* verify we have at least one channel in each direction */ 2419 if (!ch->rx_count || !ch->tx_count) 2420 return -EINVAL; 2421 2422 if (ch->rx_count > cpsw->data.channels || 2423 ch->tx_count > cpsw->data.channels) 2424 return -EINVAL; 2425 2426 return 0; 2427 } 2428 2429 static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx) 2430 { 2431 struct cpsw_common *cpsw = priv->cpsw; 2432 void (*handler)(void *, int, int); 2433 struct netdev_queue *queue; 2434 struct cpsw_vector *vec; 2435 int ret, *ch, vch; 2436 2437 if (rx) { 2438 ch = &cpsw->rx_ch_num; 2439 vec = cpsw->rxv; 2440 handler = cpsw_rx_handler; 2441 } else { 2442 ch = &cpsw->tx_ch_num; 2443 vec = cpsw->txv; 2444 handler = cpsw_tx_handler; 2445 } 2446 2447 while (*ch < ch_num) { 2448 vch = rx ? *ch : 7 - *ch; 2449 vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx); 2450 queue = netdev_get_tx_queue(priv->ndev, *ch); 2451 queue->tx_maxrate = 0; 2452 2453 if (IS_ERR(vec[*ch].ch)) 2454 return PTR_ERR(vec[*ch].ch); 2455 2456 if (!vec[*ch].ch) 2457 return -EINVAL; 2458 2459 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch, 2460 (rx ? "rx" : "tx")); 2461 (*ch)++; 2462 } 2463 2464 while (*ch > ch_num) { 2465 (*ch)--; 2466 2467 ret = cpdma_chan_destroy(vec[*ch].ch); 2468 if (ret) 2469 return ret; 2470 2471 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch, 2472 (rx ? "rx" : "tx")); 2473 } 2474 2475 return 0; 2476 } 2477 2478 static int cpsw_update_channels(struct cpsw_priv *priv, 2479 struct ethtool_channels *ch) 2480 { 2481 int ret; 2482 2483 ret = cpsw_update_channels_res(priv, ch->rx_count, 1); 2484 if (ret) 2485 return ret; 2486 2487 ret = cpsw_update_channels_res(priv, ch->tx_count, 0); 2488 if (ret) 2489 return ret; 2490 2491 return 0; 2492 } 2493 2494 static void cpsw_suspend_data_pass(struct net_device *ndev) 2495 { 2496 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2497 struct cpsw_slave *slave; 2498 int i; 2499 2500 /* Disable NAPI scheduling */ 2501 cpsw_intr_disable(cpsw); 2502 2503 /* Stop all transmit queues for every network device. 2504 * Disable re-using rx descriptors with dormant_on. 2505 */ 2506 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) { 2507 if (!(slave->ndev && netif_running(slave->ndev))) 2508 continue; 2509 2510 netif_tx_stop_all_queues(slave->ndev); 2511 netif_dormant_on(slave->ndev); 2512 } 2513 2514 /* Handle rest of tx packets and stop cpdma channels */ 2515 cpdma_ctlr_stop(cpsw->dma); 2516 } 2517 2518 static int cpsw_resume_data_pass(struct net_device *ndev) 2519 { 2520 struct cpsw_priv *priv = netdev_priv(ndev); 2521 struct cpsw_common *cpsw = priv->cpsw; 2522 struct cpsw_slave *slave; 2523 int i, ret; 2524 2525 /* Allow rx packets handling */ 2526 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) 2527 if (slave->ndev && netif_running(slave->ndev)) 2528 netif_dormant_off(slave->ndev); 2529 2530 /* After this receive is started */ 2531 if (cpsw->usage_count) { 2532 ret = cpsw_fill_rx_channels(priv); 2533 if (ret) 2534 return ret; 2535 2536 cpdma_ctlr_start(cpsw->dma); 2537 cpsw_intr_enable(cpsw); 2538 } 2539 2540 /* Resume transmit for every affected interface */ 2541 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) 2542 if (slave->ndev && netif_running(slave->ndev)) 2543 netif_tx_start_all_queues(slave->ndev); 2544 2545 return 0; 2546 } 2547 2548 static int cpsw_set_channels(struct net_device *ndev, 2549 struct ethtool_channels *chs) 2550 { 2551 struct cpsw_priv *priv = netdev_priv(ndev); 2552 struct cpsw_common *cpsw = priv->cpsw; 2553 struct cpsw_slave *slave; 2554 int i, ret; 2555 2556 ret = cpsw_check_ch_settings(cpsw, chs); 2557 if (ret < 0) 2558 return ret; 2559 2560 cpsw_suspend_data_pass(ndev); 2561 ret = cpsw_update_channels(priv, chs); 2562 if (ret) 2563 goto err; 2564 2565 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) { 2566 if (!(slave->ndev && netif_running(slave->ndev))) 2567 continue; 2568 2569 /* Inform stack about new count of queues */ 2570 ret = netif_set_real_num_tx_queues(slave->ndev, 2571 cpsw->tx_ch_num); 2572 if (ret) { 2573 dev_err(priv->dev, "cannot set real number of tx queues\n"); 2574 goto err; 2575 } 2576 2577 ret = netif_set_real_num_rx_queues(slave->ndev, 2578 cpsw->rx_ch_num); 2579 if (ret) { 2580 dev_err(priv->dev, "cannot set real number of rx queues\n"); 2581 goto err; 2582 } 2583 } 2584 2585 if (cpsw->usage_count) 2586 cpsw_split_res(ndev); 2587 2588 ret = cpsw_resume_data_pass(ndev); 2589 if (!ret) 2590 return 0; 2591 err: 2592 dev_err(priv->dev, "cannot update channels number, closing device\n"); 2593 dev_close(ndev); 2594 return ret; 2595 } 2596 2597 static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata) 2598 { 2599 struct cpsw_priv *priv = netdev_priv(ndev); 2600 struct cpsw_common *cpsw = priv->cpsw; 2601 int slave_no = cpsw_slave_index(cpsw, priv); 2602 2603 if (cpsw->slaves[slave_no].phy) 2604 return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata); 2605 else 2606 return -EOPNOTSUPP; 2607 } 2608 2609 static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata) 2610 { 2611 struct cpsw_priv *priv = netdev_priv(ndev); 2612 struct cpsw_common *cpsw = priv->cpsw; 2613 int slave_no = cpsw_slave_index(cpsw, priv); 2614 2615 if (cpsw->slaves[slave_no].phy) 2616 return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata); 2617 else 2618 return -EOPNOTSUPP; 2619 } 2620 2621 static int cpsw_nway_reset(struct net_device *ndev) 2622 { 2623 struct cpsw_priv *priv = netdev_priv(ndev); 2624 struct cpsw_common *cpsw = priv->cpsw; 2625 int slave_no = cpsw_slave_index(cpsw, priv); 2626 2627 if (cpsw->slaves[slave_no].phy) 2628 return genphy_restart_aneg(cpsw->slaves[slave_no].phy); 2629 else 2630 return -EOPNOTSUPP; 2631 } 2632 2633 static void cpsw_get_ringparam(struct net_device *ndev, 2634 struct ethtool_ringparam *ering) 2635 { 2636 struct cpsw_priv *priv = netdev_priv(ndev); 2637 struct cpsw_common *cpsw = priv->cpsw; 2638 2639 /* not supported */ 2640 ering->tx_max_pending = 0; 2641 ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma); 2642 ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES; 2643 ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma); 2644 } 2645 2646 static int cpsw_set_ringparam(struct net_device *ndev, 2647 struct ethtool_ringparam *ering) 2648 { 2649 struct cpsw_priv *priv = netdev_priv(ndev); 2650 struct cpsw_common *cpsw = priv->cpsw; 2651 int ret; 2652 2653 /* ignore ering->tx_pending - only rx_pending adjustment is supported */ 2654 2655 if (ering->rx_mini_pending || ering->rx_jumbo_pending || 2656 ering->rx_pending < CPSW_MAX_QUEUES || 2657 ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES)) 2658 return -EINVAL; 2659 2660 if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma)) 2661 return 0; 2662 2663 cpsw_suspend_data_pass(ndev); 2664 2665 cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending); 2666 2667 if (cpsw->usage_count) 2668 cpdma_chan_split_pool(cpsw->dma); 2669 2670 ret = cpsw_resume_data_pass(ndev); 2671 if (!ret) 2672 return 0; 2673 2674 dev_err(&ndev->dev, "cannot set ring params, closing device\n"); 2675 dev_close(ndev); 2676 return ret; 2677 } 2678 2679 static const struct ethtool_ops cpsw_ethtool_ops = { 2680 .get_drvinfo = cpsw_get_drvinfo, 2681 .get_msglevel = cpsw_get_msglevel, 2682 .set_msglevel = cpsw_set_msglevel, 2683 .get_link = ethtool_op_get_link, 2684 .get_ts_info = cpsw_get_ts_info, 2685 .get_coalesce = cpsw_get_coalesce, 2686 .set_coalesce = cpsw_set_coalesce, 2687 .get_sset_count = cpsw_get_sset_count, 2688 .get_strings = cpsw_get_strings, 2689 .get_ethtool_stats = cpsw_get_ethtool_stats, 2690 .get_pauseparam = cpsw_get_pauseparam, 2691 .set_pauseparam = cpsw_set_pauseparam, 2692 .get_wol = cpsw_get_wol, 2693 .set_wol = cpsw_set_wol, 2694 .get_regs_len = cpsw_get_regs_len, 2695 .get_regs = cpsw_get_regs, 2696 .begin = cpsw_ethtool_op_begin, 2697 .complete = cpsw_ethtool_op_complete, 2698 .get_channels = cpsw_get_channels, 2699 .set_channels = cpsw_set_channels, 2700 .get_link_ksettings = cpsw_get_link_ksettings, 2701 .set_link_ksettings = cpsw_set_link_ksettings, 2702 .get_eee = cpsw_get_eee, 2703 .set_eee = cpsw_set_eee, 2704 .nway_reset = cpsw_nway_reset, 2705 .get_ringparam = cpsw_get_ringparam, 2706 .set_ringparam = cpsw_set_ringparam, 2707 }; 2708 2709 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw, 2710 u32 slave_reg_ofs, u32 sliver_reg_ofs) 2711 { 2712 void __iomem *regs = cpsw->regs; 2713 int slave_num = slave->slave_num; 2714 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num; 2715 2716 slave->data = data; 2717 slave->regs = regs + slave_reg_ofs; 2718 slave->sliver = regs + sliver_reg_ofs; 2719 slave->port_vlan = data->dual_emac_res_vlan; 2720 } 2721 2722 static int cpsw_probe_dt(struct cpsw_platform_data *data, 2723 struct platform_device *pdev) 2724 { 2725 struct device_node *node = pdev->dev.of_node; 2726 struct device_node *slave_node; 2727 int i = 0, ret; 2728 u32 prop; 2729 2730 if (!node) 2731 return -EINVAL; 2732 2733 if (of_property_read_u32(node, "slaves", &prop)) { 2734 dev_err(&pdev->dev, "Missing slaves property in the DT.\n"); 2735 return -EINVAL; 2736 } 2737 data->slaves = prop; 2738 2739 if (of_property_read_u32(node, "active_slave", &prop)) { 2740 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n"); 2741 return -EINVAL; 2742 } 2743 data->active_slave = prop; 2744 2745 data->slave_data = devm_kcalloc(&pdev->dev, 2746 data->slaves, 2747 sizeof(struct cpsw_slave_data), 2748 GFP_KERNEL); 2749 if (!data->slave_data) 2750 return -ENOMEM; 2751 2752 if (of_property_read_u32(node, "cpdma_channels", &prop)) { 2753 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n"); 2754 return -EINVAL; 2755 } 2756 data->channels = prop; 2757 2758 if (of_property_read_u32(node, "ale_entries", &prop)) { 2759 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n"); 2760 return -EINVAL; 2761 } 2762 data->ale_entries = prop; 2763 2764 if (of_property_read_u32(node, "bd_ram_size", &prop)) { 2765 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n"); 2766 return -EINVAL; 2767 } 2768 data->bd_ram_size = prop; 2769 2770 if (of_property_read_u32(node, "mac_control", &prop)) { 2771 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n"); 2772 return -EINVAL; 2773 } 2774 data->mac_control = prop; 2775 2776 if (of_property_read_bool(node, "dual_emac")) 2777 data->dual_emac = 1; 2778 2779 /* 2780 * Populate all the child nodes here... 2781 */ 2782 ret = of_platform_populate(node, NULL, NULL, &pdev->dev); 2783 /* We do not want to force this, as in some cases may not have child */ 2784 if (ret) 2785 dev_warn(&pdev->dev, "Doesn't have any child node\n"); 2786 2787 for_each_available_child_of_node(node, slave_node) { 2788 struct cpsw_slave_data *slave_data = data->slave_data + i; 2789 const void *mac_addr = NULL; 2790 int lenp; 2791 const __be32 *parp; 2792 2793 /* This is no slave child node, continue */ 2794 if (strcmp(slave_node->name, "slave")) 2795 continue; 2796 2797 slave_data->phy_node = of_parse_phandle(slave_node, 2798 "phy-handle", 0); 2799 parp = of_get_property(slave_node, "phy_id", &lenp); 2800 if (slave_data->phy_node) { 2801 dev_dbg(&pdev->dev, 2802 "slave[%d] using phy-handle=\"%pOF\"\n", 2803 i, slave_data->phy_node); 2804 } else if (of_phy_is_fixed_link(slave_node)) { 2805 /* In the case of a fixed PHY, the DT node associated 2806 * to the PHY is the Ethernet MAC DT node. 2807 */ 2808 ret = of_phy_register_fixed_link(slave_node); 2809 if (ret) { 2810 if (ret != -EPROBE_DEFER) 2811 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret); 2812 return ret; 2813 } 2814 slave_data->phy_node = of_node_get(slave_node); 2815 } else if (parp) { 2816 u32 phyid; 2817 struct device_node *mdio_node; 2818 struct platform_device *mdio; 2819 2820 if (lenp != (sizeof(__be32) * 2)) { 2821 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i); 2822 goto no_phy_slave; 2823 } 2824 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp)); 2825 phyid = be32_to_cpup(parp+1); 2826 mdio = of_find_device_by_node(mdio_node); 2827 of_node_put(mdio_node); 2828 if (!mdio) { 2829 dev_err(&pdev->dev, "Missing mdio platform device\n"); 2830 return -EINVAL; 2831 } 2832 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id), 2833 PHY_ID_FMT, mdio->name, phyid); 2834 put_device(&mdio->dev); 2835 } else { 2836 dev_err(&pdev->dev, 2837 "No slave[%d] phy_id, phy-handle, or fixed-link property\n", 2838 i); 2839 goto no_phy_slave; 2840 } 2841 slave_data->phy_if = of_get_phy_mode(slave_node); 2842 if (slave_data->phy_if < 0) { 2843 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n", 2844 i); 2845 return slave_data->phy_if; 2846 } 2847 2848 no_phy_slave: 2849 mac_addr = of_get_mac_address(slave_node); 2850 if (mac_addr) { 2851 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN); 2852 } else { 2853 ret = ti_cm_get_macid(&pdev->dev, i, 2854 slave_data->mac_addr); 2855 if (ret) 2856 return ret; 2857 } 2858 if (data->dual_emac) { 2859 if (of_property_read_u32(slave_node, "dual_emac_res_vlan", 2860 &prop)) { 2861 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n"); 2862 slave_data->dual_emac_res_vlan = i+1; 2863 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n", 2864 slave_data->dual_emac_res_vlan, i); 2865 } else { 2866 slave_data->dual_emac_res_vlan = prop; 2867 } 2868 } 2869 2870 i++; 2871 if (i == data->slaves) 2872 break; 2873 } 2874 2875 return 0; 2876 } 2877 2878 static void cpsw_remove_dt(struct platform_device *pdev) 2879 { 2880 struct net_device *ndev = platform_get_drvdata(pdev); 2881 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 2882 struct cpsw_platform_data *data = &cpsw->data; 2883 struct device_node *node = pdev->dev.of_node; 2884 struct device_node *slave_node; 2885 int i = 0; 2886 2887 for_each_available_child_of_node(node, slave_node) { 2888 struct cpsw_slave_data *slave_data = &data->slave_data[i]; 2889 2890 if (strcmp(slave_node->name, "slave")) 2891 continue; 2892 2893 if (of_phy_is_fixed_link(slave_node)) 2894 of_phy_deregister_fixed_link(slave_node); 2895 2896 of_node_put(slave_data->phy_node); 2897 2898 i++; 2899 if (i == data->slaves) 2900 break; 2901 } 2902 2903 of_platform_depopulate(&pdev->dev); 2904 } 2905 2906 static int cpsw_probe_dual_emac(struct cpsw_priv *priv) 2907 { 2908 struct cpsw_common *cpsw = priv->cpsw; 2909 struct cpsw_platform_data *data = &cpsw->data; 2910 struct net_device *ndev; 2911 struct cpsw_priv *priv_sl2; 2912 int ret = 0; 2913 2914 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES); 2915 if (!ndev) { 2916 dev_err(cpsw->dev, "cpsw: error allocating net_device\n"); 2917 return -ENOMEM; 2918 } 2919 2920 priv_sl2 = netdev_priv(ndev); 2921 priv_sl2->cpsw = cpsw; 2922 priv_sl2->ndev = ndev; 2923 priv_sl2->dev = &ndev->dev; 2924 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); 2925 2926 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) { 2927 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr, 2928 ETH_ALEN); 2929 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n", 2930 priv_sl2->mac_addr); 2931 } else { 2932 eth_random_addr(priv_sl2->mac_addr); 2933 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n", 2934 priv_sl2->mac_addr); 2935 } 2936 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN); 2937 2938 priv_sl2->emac_port = 1; 2939 cpsw->slaves[1].ndev = ndev; 2940 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER; 2941 2942 ndev->netdev_ops = &cpsw_netdev_ops; 2943 ndev->ethtool_ops = &cpsw_ethtool_ops; 2944 2945 /* register the network device */ 2946 SET_NETDEV_DEV(ndev, cpsw->dev); 2947 ret = register_netdev(ndev); 2948 if (ret) { 2949 dev_err(cpsw->dev, "cpsw: error registering net device\n"); 2950 free_netdev(ndev); 2951 ret = -ENODEV; 2952 } 2953 2954 return ret; 2955 } 2956 2957 static const struct of_device_id cpsw_of_mtable[] = { 2958 { .compatible = "ti,cpsw"}, 2959 { .compatible = "ti,am335x-cpsw"}, 2960 { .compatible = "ti,am4372-cpsw"}, 2961 { .compatible = "ti,dra7-cpsw"}, 2962 { /* sentinel */ }, 2963 }; 2964 MODULE_DEVICE_TABLE(of, cpsw_of_mtable); 2965 2966 static const struct soc_device_attribute cpsw_soc_devices[] = { 2967 { .family = "AM33xx", .revision = "ES1.0"}, 2968 { /* sentinel */ } 2969 }; 2970 2971 static int cpsw_probe(struct platform_device *pdev) 2972 { 2973 struct clk *clk; 2974 struct cpsw_platform_data *data; 2975 struct net_device *ndev; 2976 struct cpsw_priv *priv; 2977 struct cpdma_params dma_params; 2978 struct cpsw_ale_params ale_params; 2979 void __iomem *ss_regs; 2980 void __iomem *cpts_regs; 2981 struct resource *res, *ss_res; 2982 struct gpio_descs *mode; 2983 u32 slave_offset, sliver_offset, slave_size; 2984 const struct soc_device_attribute *soc; 2985 struct cpsw_common *cpsw; 2986 int ret = 0, i, ch; 2987 int irq; 2988 2989 cpsw = devm_kzalloc(&pdev->dev, sizeof(struct cpsw_common), GFP_KERNEL); 2990 if (!cpsw) 2991 return -ENOMEM; 2992 2993 cpsw->dev = &pdev->dev; 2994 2995 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES); 2996 if (!ndev) { 2997 dev_err(&pdev->dev, "error allocating net_device\n"); 2998 return -ENOMEM; 2999 } 3000 3001 platform_set_drvdata(pdev, ndev); 3002 priv = netdev_priv(ndev); 3003 priv->cpsw = cpsw; 3004 priv->ndev = ndev; 3005 priv->dev = &ndev->dev; 3006 priv->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG); 3007 cpsw->rx_packet_max = max(rx_packet_max, 128); 3008 3009 mode = devm_gpiod_get_array_optional(&pdev->dev, "mode", GPIOD_OUT_LOW); 3010 if (IS_ERR(mode)) { 3011 ret = PTR_ERR(mode); 3012 dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret); 3013 goto clean_ndev_ret; 3014 } 3015 3016 /* 3017 * This may be required here for child devices. 3018 */ 3019 pm_runtime_enable(&pdev->dev); 3020 3021 /* Select default pin state */ 3022 pinctrl_pm_select_default_state(&pdev->dev); 3023 3024 /* Need to enable clocks with runtime PM api to access module 3025 * registers 3026 */ 3027 ret = pm_runtime_get_sync(&pdev->dev); 3028 if (ret < 0) { 3029 pm_runtime_put_noidle(&pdev->dev); 3030 goto clean_runtime_disable_ret; 3031 } 3032 3033 ret = cpsw_probe_dt(&cpsw->data, pdev); 3034 if (ret) 3035 goto clean_dt_ret; 3036 3037 data = &cpsw->data; 3038 cpsw->rx_ch_num = 1; 3039 cpsw->tx_ch_num = 1; 3040 3041 if (is_valid_ether_addr(data->slave_data[0].mac_addr)) { 3042 memcpy(priv->mac_addr, data->slave_data[0].mac_addr, ETH_ALEN); 3043 dev_info(&pdev->dev, "Detected MACID = %pM\n", priv->mac_addr); 3044 } else { 3045 eth_random_addr(priv->mac_addr); 3046 dev_info(&pdev->dev, "Random MACID = %pM\n", priv->mac_addr); 3047 } 3048 3049 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN); 3050 3051 cpsw->slaves = devm_kcalloc(&pdev->dev, 3052 data->slaves, sizeof(struct cpsw_slave), 3053 GFP_KERNEL); 3054 if (!cpsw->slaves) { 3055 ret = -ENOMEM; 3056 goto clean_dt_ret; 3057 } 3058 for (i = 0; i < data->slaves; i++) 3059 cpsw->slaves[i].slave_num = i; 3060 3061 cpsw->slaves[0].ndev = ndev; 3062 priv->emac_port = 0; 3063 3064 clk = devm_clk_get(&pdev->dev, "fck"); 3065 if (IS_ERR(clk)) { 3066 dev_err(priv->dev, "fck is not found\n"); 3067 ret = -ENODEV; 3068 goto clean_dt_ret; 3069 } 3070 cpsw->bus_freq_mhz = clk_get_rate(clk) / 1000000; 3071 3072 ss_res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 3073 ss_regs = devm_ioremap_resource(&pdev->dev, ss_res); 3074 if (IS_ERR(ss_regs)) { 3075 ret = PTR_ERR(ss_regs); 3076 goto clean_dt_ret; 3077 } 3078 cpsw->regs = ss_regs; 3079 3080 cpsw->version = readl(&cpsw->regs->id_ver); 3081 3082 res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 3083 cpsw->wr_regs = devm_ioremap_resource(&pdev->dev, res); 3084 if (IS_ERR(cpsw->wr_regs)) { 3085 ret = PTR_ERR(cpsw->wr_regs); 3086 goto clean_dt_ret; 3087 } 3088 3089 memset(&dma_params, 0, sizeof(dma_params)); 3090 memset(&ale_params, 0, sizeof(ale_params)); 3091 3092 switch (cpsw->version) { 3093 case CPSW_VERSION_1: 3094 cpsw->host_port_regs = ss_regs + CPSW1_HOST_PORT_OFFSET; 3095 cpts_regs = ss_regs + CPSW1_CPTS_OFFSET; 3096 cpsw->hw_stats = ss_regs + CPSW1_HW_STATS; 3097 dma_params.dmaregs = ss_regs + CPSW1_CPDMA_OFFSET; 3098 dma_params.txhdp = ss_regs + CPSW1_STATERAM_OFFSET; 3099 ale_params.ale_regs = ss_regs + CPSW1_ALE_OFFSET; 3100 slave_offset = CPSW1_SLAVE_OFFSET; 3101 slave_size = CPSW1_SLAVE_SIZE; 3102 sliver_offset = CPSW1_SLIVER_OFFSET; 3103 dma_params.desc_mem_phys = 0; 3104 break; 3105 case CPSW_VERSION_2: 3106 case CPSW_VERSION_3: 3107 case CPSW_VERSION_4: 3108 cpsw->host_port_regs = ss_regs + CPSW2_HOST_PORT_OFFSET; 3109 cpts_regs = ss_regs + CPSW2_CPTS_OFFSET; 3110 cpsw->hw_stats = ss_regs + CPSW2_HW_STATS; 3111 dma_params.dmaregs = ss_regs + CPSW2_CPDMA_OFFSET; 3112 dma_params.txhdp = ss_regs + CPSW2_STATERAM_OFFSET; 3113 ale_params.ale_regs = ss_regs + CPSW2_ALE_OFFSET; 3114 slave_offset = CPSW2_SLAVE_OFFSET; 3115 slave_size = CPSW2_SLAVE_SIZE; 3116 sliver_offset = CPSW2_SLIVER_OFFSET; 3117 dma_params.desc_mem_phys = 3118 (u32 __force) ss_res->start + CPSW2_BD_OFFSET; 3119 break; 3120 default: 3121 dev_err(priv->dev, "unknown version 0x%08x\n", cpsw->version); 3122 ret = -ENODEV; 3123 goto clean_dt_ret; 3124 } 3125 for (i = 0; i < cpsw->data.slaves; i++) { 3126 struct cpsw_slave *slave = &cpsw->slaves[i]; 3127 3128 cpsw_slave_init(slave, cpsw, slave_offset, sliver_offset); 3129 slave_offset += slave_size; 3130 sliver_offset += SLIVER_SIZE; 3131 } 3132 3133 dma_params.dev = &pdev->dev; 3134 dma_params.rxthresh = dma_params.dmaregs + CPDMA_RXTHRESH; 3135 dma_params.rxfree = dma_params.dmaregs + CPDMA_RXFREE; 3136 dma_params.rxhdp = dma_params.txhdp + CPDMA_RXHDP; 3137 dma_params.txcp = dma_params.txhdp + CPDMA_TXCP; 3138 dma_params.rxcp = dma_params.txhdp + CPDMA_RXCP; 3139 3140 dma_params.num_chan = data->channels; 3141 dma_params.has_soft_reset = true; 3142 dma_params.min_packet_size = CPSW_MIN_PACKET_SIZE; 3143 dma_params.desc_mem_size = data->bd_ram_size; 3144 dma_params.desc_align = 16; 3145 dma_params.has_ext_regs = true; 3146 dma_params.desc_hw_addr = dma_params.desc_mem_phys; 3147 dma_params.bus_freq_mhz = cpsw->bus_freq_mhz; 3148 dma_params.descs_pool_size = descs_pool_size; 3149 3150 cpsw->dma = cpdma_ctlr_create(&dma_params); 3151 if (!cpsw->dma) { 3152 dev_err(priv->dev, "error initializing dma\n"); 3153 ret = -ENOMEM; 3154 goto clean_dt_ret; 3155 } 3156 3157 soc = soc_device_match(cpsw_soc_devices); 3158 if (soc) 3159 cpsw->quirk_irq = 1; 3160 3161 ch = cpsw->quirk_irq ? 0 : 7; 3162 cpsw->txv[0].ch = cpdma_chan_create(cpsw->dma, ch, cpsw_tx_handler, 0); 3163 if (IS_ERR(cpsw->txv[0].ch)) { 3164 dev_err(priv->dev, "error initializing tx dma channel\n"); 3165 ret = PTR_ERR(cpsw->txv[0].ch); 3166 goto clean_dma_ret; 3167 } 3168 3169 cpsw->rxv[0].ch = cpdma_chan_create(cpsw->dma, 0, cpsw_rx_handler, 1); 3170 if (IS_ERR(cpsw->rxv[0].ch)) { 3171 dev_err(priv->dev, "error initializing rx dma channel\n"); 3172 ret = PTR_ERR(cpsw->rxv[0].ch); 3173 goto clean_dma_ret; 3174 } 3175 3176 ale_params.dev = &pdev->dev; 3177 ale_params.ale_ageout = ale_ageout; 3178 ale_params.ale_entries = data->ale_entries; 3179 ale_params.ale_ports = CPSW_ALE_PORTS_NUM; 3180 3181 cpsw->ale = cpsw_ale_create(&ale_params); 3182 if (!cpsw->ale) { 3183 dev_err(priv->dev, "error initializing ale engine\n"); 3184 ret = -ENODEV; 3185 goto clean_dma_ret; 3186 } 3187 3188 cpsw->cpts = cpts_create(cpsw->dev, cpts_regs, cpsw->dev->of_node); 3189 if (IS_ERR(cpsw->cpts)) { 3190 ret = PTR_ERR(cpsw->cpts); 3191 goto clean_dma_ret; 3192 } 3193 3194 ndev->irq = platform_get_irq(pdev, 1); 3195 if (ndev->irq < 0) { 3196 dev_err(priv->dev, "error getting irq resource\n"); 3197 ret = ndev->irq; 3198 goto clean_dma_ret; 3199 } 3200 3201 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX; 3202 3203 ndev->netdev_ops = &cpsw_netdev_ops; 3204 ndev->ethtool_ops = &cpsw_ethtool_ops; 3205 netif_napi_add(ndev, &cpsw->napi_rx, 3206 cpsw->quirk_irq ? cpsw_rx_poll : cpsw_rx_mq_poll, 3207 CPSW_POLL_WEIGHT); 3208 netif_tx_napi_add(ndev, &cpsw->napi_tx, 3209 cpsw->quirk_irq ? cpsw_tx_poll : cpsw_tx_mq_poll, 3210 CPSW_POLL_WEIGHT); 3211 cpsw_split_res(ndev); 3212 3213 /* register the network device */ 3214 SET_NETDEV_DEV(ndev, &pdev->dev); 3215 ret = register_netdev(ndev); 3216 if (ret) { 3217 dev_err(priv->dev, "error registering net device\n"); 3218 ret = -ENODEV; 3219 goto clean_dma_ret; 3220 } 3221 3222 if (cpsw->data.dual_emac) { 3223 ret = cpsw_probe_dual_emac(priv); 3224 if (ret) { 3225 cpsw_err(priv, probe, "error probe slave 2 emac interface\n"); 3226 goto clean_unregister_netdev_ret; 3227 } 3228 } 3229 3230 /* Grab RX and TX IRQs. Note that we also have RX_THRESHOLD and 3231 * MISC IRQs which are always kept disabled with this driver so 3232 * we will not request them. 3233 * 3234 * If anyone wants to implement support for those, make sure to 3235 * first request and append them to irqs_table array. 3236 */ 3237 3238 /* RX IRQ */ 3239 irq = platform_get_irq(pdev, 1); 3240 if (irq < 0) { 3241 ret = irq; 3242 goto clean_dma_ret; 3243 } 3244 3245 cpsw->irqs_table[0] = irq; 3246 ret = devm_request_irq(&pdev->dev, irq, cpsw_rx_interrupt, 3247 0, dev_name(&pdev->dev), cpsw); 3248 if (ret < 0) { 3249 dev_err(priv->dev, "error attaching irq (%d)\n", ret); 3250 goto clean_dma_ret; 3251 } 3252 3253 /* TX IRQ */ 3254 irq = platform_get_irq(pdev, 2); 3255 if (irq < 0) { 3256 ret = irq; 3257 goto clean_dma_ret; 3258 } 3259 3260 cpsw->irqs_table[1] = irq; 3261 ret = devm_request_irq(&pdev->dev, irq, cpsw_tx_interrupt, 3262 0, dev_name(&pdev->dev), cpsw); 3263 if (ret < 0) { 3264 dev_err(priv->dev, "error attaching irq (%d)\n", ret); 3265 goto clean_dma_ret; 3266 } 3267 3268 cpsw_notice(priv, probe, 3269 "initialized device (regs %pa, irq %d, pool size %d)\n", 3270 &ss_res->start, ndev->irq, dma_params.descs_pool_size); 3271 3272 pm_runtime_put(&pdev->dev); 3273 3274 return 0; 3275 3276 clean_unregister_netdev_ret: 3277 unregister_netdev(ndev); 3278 clean_dma_ret: 3279 cpdma_ctlr_destroy(cpsw->dma); 3280 clean_dt_ret: 3281 cpsw_remove_dt(pdev); 3282 pm_runtime_put_sync(&pdev->dev); 3283 clean_runtime_disable_ret: 3284 pm_runtime_disable(&pdev->dev); 3285 clean_ndev_ret: 3286 free_netdev(priv->ndev); 3287 return ret; 3288 } 3289 3290 static int cpsw_remove(struct platform_device *pdev) 3291 { 3292 struct net_device *ndev = platform_get_drvdata(pdev); 3293 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 3294 int ret; 3295 3296 ret = pm_runtime_get_sync(&pdev->dev); 3297 if (ret < 0) { 3298 pm_runtime_put_noidle(&pdev->dev); 3299 return ret; 3300 } 3301 3302 if (cpsw->data.dual_emac) 3303 unregister_netdev(cpsw->slaves[1].ndev); 3304 unregister_netdev(ndev); 3305 3306 cpts_release(cpsw->cpts); 3307 cpdma_ctlr_destroy(cpsw->dma); 3308 cpsw_remove_dt(pdev); 3309 pm_runtime_put_sync(&pdev->dev); 3310 pm_runtime_disable(&pdev->dev); 3311 if (cpsw->data.dual_emac) 3312 free_netdev(cpsw->slaves[1].ndev); 3313 free_netdev(ndev); 3314 return 0; 3315 } 3316 3317 #ifdef CONFIG_PM_SLEEP 3318 static int cpsw_suspend(struct device *dev) 3319 { 3320 struct platform_device *pdev = to_platform_device(dev); 3321 struct net_device *ndev = platform_get_drvdata(pdev); 3322 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 3323 3324 if (cpsw->data.dual_emac) { 3325 int i; 3326 3327 for (i = 0; i < cpsw->data.slaves; i++) { 3328 if (netif_running(cpsw->slaves[i].ndev)) 3329 cpsw_ndo_stop(cpsw->slaves[i].ndev); 3330 } 3331 } else { 3332 if (netif_running(ndev)) 3333 cpsw_ndo_stop(ndev); 3334 } 3335 3336 /* Select sleep pin state */ 3337 pinctrl_pm_select_sleep_state(dev); 3338 3339 return 0; 3340 } 3341 3342 static int cpsw_resume(struct device *dev) 3343 { 3344 struct platform_device *pdev = to_platform_device(dev); 3345 struct net_device *ndev = platform_get_drvdata(pdev); 3346 struct cpsw_common *cpsw = ndev_to_cpsw(ndev); 3347 3348 /* Select default pin state */ 3349 pinctrl_pm_select_default_state(dev); 3350 3351 /* shut up ASSERT_RTNL() warning in netif_set_real_num_tx/rx_queues */ 3352 rtnl_lock(); 3353 if (cpsw->data.dual_emac) { 3354 int i; 3355 3356 for (i = 0; i < cpsw->data.slaves; i++) { 3357 if (netif_running(cpsw->slaves[i].ndev)) 3358 cpsw_ndo_open(cpsw->slaves[i].ndev); 3359 } 3360 } else { 3361 if (netif_running(ndev)) 3362 cpsw_ndo_open(ndev); 3363 } 3364 rtnl_unlock(); 3365 3366 return 0; 3367 } 3368 #endif 3369 3370 static SIMPLE_DEV_PM_OPS(cpsw_pm_ops, cpsw_suspend, cpsw_resume); 3371 3372 static struct platform_driver cpsw_driver = { 3373 .driver = { 3374 .name = "cpsw", 3375 .pm = &cpsw_pm_ops, 3376 .of_match_table = cpsw_of_mtable, 3377 }, 3378 .probe = cpsw_probe, 3379 .remove = cpsw_remove, 3380 }; 3381 3382 module_platform_driver(cpsw_driver); 3383 3384 MODULE_LICENSE("GPL"); 3385 MODULE_AUTHOR("Cyril Chemparathy <cyril@ti.com>"); 3386 MODULE_AUTHOR("Mugunthan V N <mugunthanvnm@ti.com>"); 3387 MODULE_DESCRIPTION("TI CPSW Ethernet driver"); 3388