2 * Texas Instruments Ethernet Switch Driver
4 * Copyright (C) 2012 Texas Instruments
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.
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.
16 #include <linux/kernel.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>
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>
41 #include <linux/pinctrl/consumer.h>
42 #include <net/pkt_cls.h>
47 #include "davinci_cpdma.h"
49 #include <net/pkt_sched.h>
51 #define CPSW_DEBUG (NETIF_MSG_HW | NETIF_MSG_WOL | \
52 NETIF_MSG_DRV | NETIF_MSG_LINK | \
53 NETIF_MSG_IFUP | NETIF_MSG_INTR | \
54 NETIF_MSG_PROBE | NETIF_MSG_TIMER | \
55 NETIF_MSG_IFDOWN | NETIF_MSG_RX_ERR | \
56 NETIF_MSG_TX_ERR | NETIF_MSG_TX_DONE | \
57 NETIF_MSG_PKTDATA | NETIF_MSG_TX_QUEUED | \
60 #define cpsw_info(priv, type, format, ...) \
62 if (netif_msg_##type(priv) && net_ratelimit()) \
63 dev_info(priv->dev, format, ## __VA_ARGS__); \
66 #define cpsw_err(priv, type, format, ...) \
68 if (netif_msg_##type(priv) && net_ratelimit()) \
69 dev_err(priv->dev, format, ## __VA_ARGS__); \
72 #define cpsw_dbg(priv, type, format, ...) \
74 if (netif_msg_##type(priv) && net_ratelimit()) \
75 dev_dbg(priv->dev, format, ## __VA_ARGS__); \
78 #define cpsw_notice(priv, type, format, ...) \
80 if (netif_msg_##type(priv) && net_ratelimit()) \
81 dev_notice(priv->dev, format, ## __VA_ARGS__); \
84 #define ALE_ALL_PORTS 0x7
86 #define CPSW_MAJOR_VERSION(reg) (reg >> 8 & 0x7)
87 #define CPSW_MINOR_VERSION(reg) (reg & 0xff)
88 #define CPSW_RTL_VERSION(reg) ((reg >> 11) & 0x1f)
90 #define CPSW_VERSION_1 0x19010a
91 #define CPSW_VERSION_2 0x19010c
92 #define CPSW_VERSION_3 0x19010f
93 #define CPSW_VERSION_4 0x190112
95 #define HOST_PORT_NUM 0
96 #define CPSW_ALE_PORTS_NUM 3
97 #define SLIVER_SIZE 0x40
99 #define CPSW1_HOST_PORT_OFFSET 0x028
100 #define CPSW1_SLAVE_OFFSET 0x050
101 #define CPSW1_SLAVE_SIZE 0x040
102 #define CPSW1_CPDMA_OFFSET 0x100
103 #define CPSW1_STATERAM_OFFSET 0x200
104 #define CPSW1_HW_STATS 0x400
105 #define CPSW1_CPTS_OFFSET 0x500
106 #define CPSW1_ALE_OFFSET 0x600
107 #define CPSW1_SLIVER_OFFSET 0x700
109 #define CPSW2_HOST_PORT_OFFSET 0x108
110 #define CPSW2_SLAVE_OFFSET 0x200
111 #define CPSW2_SLAVE_SIZE 0x100
112 #define CPSW2_CPDMA_OFFSET 0x800
113 #define CPSW2_HW_STATS 0x900
114 #define CPSW2_STATERAM_OFFSET 0xa00
115 #define CPSW2_CPTS_OFFSET 0xc00
116 #define CPSW2_ALE_OFFSET 0xd00
117 #define CPSW2_SLIVER_OFFSET 0xd80
118 #define CPSW2_BD_OFFSET 0x2000
120 #define CPDMA_RXTHRESH 0x0c0
121 #define CPDMA_RXFREE 0x0e0
122 #define CPDMA_TXHDP 0x00
123 #define CPDMA_RXHDP 0x20
124 #define CPDMA_TXCP 0x40
125 #define CPDMA_RXCP 0x60
127 #define CPSW_POLL_WEIGHT 64
128 #define CPSW_RX_VLAN_ENCAP_HDR_SIZE 4
129 #define CPSW_MIN_PACKET_SIZE (VLAN_ETH_ZLEN)
130 #define CPSW_MAX_PACKET_SIZE (VLAN_ETH_FRAME_LEN +\
132 CPSW_RX_VLAN_ENCAP_HDR_SIZE)
134 #define RX_PRIORITY_MAPPING 0x76543210
135 #define TX_PRIORITY_MAPPING 0x33221100
136 #define CPDMA_TX_PRIORITY_MAP 0x76543210
138 #define CPSW_VLAN_AWARE BIT(1)
139 #define CPSW_RX_VLAN_ENCAP BIT(2)
140 #define CPSW_ALE_VLAN_AWARE 1
142 #define CPSW_FIFO_NORMAL_MODE (0 << 16)
143 #define CPSW_FIFO_DUAL_MAC_MODE (1 << 16)
144 #define CPSW_FIFO_RATE_LIMIT_MODE (2 << 16)
146 #define CPSW_INTPACEEN (0x3f << 16)
147 #define CPSW_INTPRESCALE_MASK (0x7FF << 0)
148 #define CPSW_CMINTMAX_CNT 63
149 #define CPSW_CMINTMIN_CNT 2
150 #define CPSW_CMINTMAX_INTVL (1000 / CPSW_CMINTMIN_CNT)
151 #define CPSW_CMINTMIN_INTVL ((1000 / CPSW_CMINTMAX_CNT) + 1)
153 #define cpsw_slave_index(cpsw, priv) \
154 ((cpsw->data.dual_emac) ? priv->emac_port : \
155 cpsw->data.active_slave)
157 #define CPSW_MAX_QUEUES 8
158 #define CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT 256
159 #define CPSW_FIFO_QUEUE_TYPE_SHIFT 16
160 #define CPSW_FIFO_SHAPE_EN_SHIFT 16
161 #define CPSW_FIFO_RATE_EN_SHIFT 20
162 #define CPSW_TC_NUM 4
163 #define CPSW_FIFO_SHAPERS_NUM (CPSW_TC_NUM - 1)
164 #define CPSW_PCT_MASK 0x7f
166 #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT 29
167 #define CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK GENMASK(2, 0)
168 #define CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT 16
169 #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT 8
170 #define CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK GENMASK(1, 0)
172 CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG = 0,
173 CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV,
174 CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG,
175 CPSW_RX_VLAN_ENCAP_HDR_PKT_UNTAG,
178 static int debug_level;
179 module_param(debug_level, int, 0);
180 MODULE_PARM_DESC(debug_level, "cpsw debug level (NETIF_MSG bits)");
182 static int ale_ageout = 10;
183 module_param(ale_ageout, int, 0);
184 MODULE_PARM_DESC(ale_ageout, "cpsw ale ageout interval (seconds)");
186 static int rx_packet_max = CPSW_MAX_PACKET_SIZE;
187 module_param(rx_packet_max, int, 0);
188 MODULE_PARM_DESC(rx_packet_max, "maximum receive packet size (bytes)");
190 static int descs_pool_size = CPSW_CPDMA_DESCS_POOL_SIZE_DEFAULT;
191 module_param(descs_pool_size, int, 0444);
192 MODULE_PARM_DESC(descs_pool_size, "Number of CPDMA CPPI descriptors in pool");
194 struct cpsw_wr_regs {
214 struct cpsw_ss_regs {
231 #define CPSW1_MAX_BLKS 0x00 /* Maximum FIFO Blocks */
232 #define CPSW1_BLK_CNT 0x04 /* FIFO Block Usage Count (Read Only) */
233 #define CPSW1_TX_IN_CTL 0x08 /* Transmit FIFO Control */
234 #define CPSW1_PORT_VLAN 0x0c /* VLAN Register */
235 #define CPSW1_TX_PRI_MAP 0x10 /* Tx Header Priority to Switch Pri Mapping */
236 #define CPSW1_TS_CTL 0x14 /* Time Sync Control */
237 #define CPSW1_TS_SEQ_LTYPE 0x18 /* Time Sync Sequence ID Offset and Msg Type */
238 #define CPSW1_TS_VLAN 0x1c /* Time Sync VLAN1 and VLAN2 */
241 #define CPSW2_CONTROL 0x00 /* Control Register */
242 #define CPSW2_MAX_BLKS 0x08 /* Maximum FIFO Blocks */
243 #define CPSW2_BLK_CNT 0x0c /* FIFO Block Usage Count (Read Only) */
244 #define CPSW2_TX_IN_CTL 0x10 /* Transmit FIFO Control */
245 #define CPSW2_PORT_VLAN 0x14 /* VLAN Register */
246 #define CPSW2_TX_PRI_MAP 0x18 /* Tx Header Priority to Switch Pri Mapping */
247 #define CPSW2_TS_SEQ_MTYPE 0x1c /* Time Sync Sequence ID Offset and Msg Type */
249 /* CPSW_PORT_V1 and V2 */
250 #define SA_LO 0x20 /* CPGMAC_SL Source Address Low */
251 #define SA_HI 0x24 /* CPGMAC_SL Source Address High */
252 #define SEND_PERCENT 0x28 /* Transmit Queue Send Percentages */
254 /* CPSW_PORT_V2 only */
255 #define RX_DSCP_PRI_MAP0 0x30 /* Rx DSCP Priority to Rx Packet Mapping */
256 #define RX_DSCP_PRI_MAP1 0x34 /* Rx DSCP Priority to Rx Packet Mapping */
257 #define RX_DSCP_PRI_MAP2 0x38 /* Rx DSCP Priority to Rx Packet Mapping */
258 #define RX_DSCP_PRI_MAP3 0x3c /* Rx DSCP Priority to Rx Packet Mapping */
259 #define RX_DSCP_PRI_MAP4 0x40 /* Rx DSCP Priority to Rx Packet Mapping */
260 #define RX_DSCP_PRI_MAP5 0x44 /* Rx DSCP Priority to Rx Packet Mapping */
261 #define RX_DSCP_PRI_MAP6 0x48 /* Rx DSCP Priority to Rx Packet Mapping */
262 #define RX_DSCP_PRI_MAP7 0x4c /* Rx DSCP Priority to Rx Packet Mapping */
264 /* Bit definitions for the CPSW2_CONTROL register */
265 #define PASS_PRI_TAGGED BIT(24) /* Pass Priority Tagged */
266 #define VLAN_LTYPE2_EN BIT(21) /* VLAN LTYPE 2 enable */
267 #define VLAN_LTYPE1_EN BIT(20) /* VLAN LTYPE 1 enable */
268 #define DSCP_PRI_EN BIT(16) /* DSCP Priority Enable */
269 #define TS_107 BIT(15) /* Tyme Sync Dest IP Address 107 */
270 #define TS_320 BIT(14) /* Time Sync Dest Port 320 enable */
271 #define TS_319 BIT(13) /* Time Sync Dest Port 319 enable */
272 #define TS_132 BIT(12) /* Time Sync Dest IP Addr 132 enable */
273 #define TS_131 BIT(11) /* Time Sync Dest IP Addr 131 enable */
274 #define TS_130 BIT(10) /* Time Sync Dest IP Addr 130 enable */
275 #define TS_129 BIT(9) /* Time Sync Dest IP Addr 129 enable */
276 #define TS_TTL_NONZERO BIT(8) /* Time Sync Time To Live Non-zero enable */
277 #define TS_ANNEX_F_EN BIT(6) /* Time Sync Annex F enable */
278 #define TS_ANNEX_D_EN BIT(4) /* Time Sync Annex D enable */
279 #define TS_LTYPE2_EN BIT(3) /* Time Sync LTYPE 2 enable */
280 #define TS_LTYPE1_EN BIT(2) /* Time Sync LTYPE 1 enable */
281 #define TS_TX_EN BIT(1) /* Time Sync Transmit Enable */
282 #define TS_RX_EN BIT(0) /* Time Sync Receive Enable */
284 #define CTRL_V2_TS_BITS \
285 (TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
286 TS_TTL_NONZERO | TS_ANNEX_D_EN | TS_LTYPE1_EN)
288 #define CTRL_V2_ALL_TS_MASK (CTRL_V2_TS_BITS | TS_TX_EN | TS_RX_EN)
289 #define CTRL_V2_TX_TS_BITS (CTRL_V2_TS_BITS | TS_TX_EN)
290 #define CTRL_V2_RX_TS_BITS (CTRL_V2_TS_BITS | TS_RX_EN)
293 #define CTRL_V3_TS_BITS \
294 (TS_107 | TS_320 | TS_319 | TS_132 | TS_131 | TS_130 | TS_129 |\
295 TS_TTL_NONZERO | TS_ANNEX_F_EN | TS_ANNEX_D_EN |\
298 #define CTRL_V3_ALL_TS_MASK (CTRL_V3_TS_BITS | TS_TX_EN | TS_RX_EN)
299 #define CTRL_V3_TX_TS_BITS (CTRL_V3_TS_BITS | TS_TX_EN)
300 #define CTRL_V3_RX_TS_BITS (CTRL_V3_TS_BITS | TS_RX_EN)
302 /* Bit definitions for the CPSW2_TS_SEQ_MTYPE register */
303 #define TS_SEQ_ID_OFFSET_SHIFT (16) /* Time Sync Sequence ID Offset */
304 #define TS_SEQ_ID_OFFSET_MASK (0x3f)
305 #define TS_MSG_TYPE_EN_SHIFT (0) /* Time Sync Message Type Enable */
306 #define TS_MSG_TYPE_EN_MASK (0xffff)
308 /* The PTP event messages - Sync, Delay_Req, Pdelay_Req, and Pdelay_Resp. */
309 #define EVENT_MSG_BITS ((1<<0) | (1<<1) | (1<<2) | (1<<3))
311 /* Bit definitions for the CPSW1_TS_CTL register */
312 #define CPSW_V1_TS_RX_EN BIT(0)
313 #define CPSW_V1_TS_TX_EN BIT(4)
314 #define CPSW_V1_MSG_TYPE_OFS 16
316 /* Bit definitions for the CPSW1_TS_SEQ_LTYPE register */
317 #define CPSW_V1_SEQ_ID_OFS_SHIFT 16
319 #define CPSW_MAX_BLKS_TX 15
320 #define CPSW_MAX_BLKS_TX_SHIFT 4
321 #define CPSW_MAX_BLKS_RX 5
323 struct cpsw_host_regs {
329 u32 cpdma_tx_pri_map;
330 u32 cpdma_rx_chan_map;
333 struct cpsw_sliver_regs {
346 struct cpsw_hw_stats {
348 u32 rxbroadcastframes;
349 u32 rxmulticastframes;
352 u32 rxaligncodeerrors;
353 u32 rxoversizedframes;
355 u32 rxundersizedframes;
360 u32 txbroadcastframes;
361 u32 txmulticastframes;
363 u32 txdeferredframes;
364 u32 txcollisionframes;
365 u32 txsinglecollframes;
366 u32 txmultcollframes;
367 u32 txexcessivecollisions;
368 u32 txlatecollisions;
370 u32 txcarriersenseerrors;
373 u32 octetframes65t127;
374 u32 octetframes128t255;
375 u32 octetframes256t511;
376 u32 octetframes512t1023;
377 u32 octetframes1024tup;
384 struct cpsw_slave_data {
385 struct device_node *phy_node;
386 char phy_id[MII_BUS_ID_SIZE];
388 u8 mac_addr[ETH_ALEN];
389 u16 dual_emac_res_vlan; /* Reserved VLAN for DualEMAC */
392 struct cpsw_platform_data {
393 struct cpsw_slave_data *slave_data;
394 u32 ss_reg_ofs; /* Subsystem control register offset */
395 u32 channels; /* number of cpdma channels (symmetric) */
396 u32 slaves; /* number of slave cpgmac ports */
397 u32 active_slave; /* time stamping, ethtool and SIOCGMIIPHY slave */
398 u32 ale_entries; /* ale table size */
399 u32 bd_ram_size; /*buffer descriptor ram size */
400 u32 mac_control; /* Mac control register */
401 u16 default_vlan; /* Def VLAN for ALE lookup in VLAN aware mode*/
402 bool dual_emac; /* Enable Dual EMAC mode */
407 struct cpsw_sliver_regs __iomem *sliver;
410 struct cpsw_slave_data *data;
411 struct phy_device *phy;
412 struct net_device *ndev;
416 static inline u32 slave_read(struct cpsw_slave *slave, u32 offset)
418 return readl_relaxed(slave->regs + offset);
421 static inline void slave_write(struct cpsw_slave *slave, u32 val, u32 offset)
423 writel_relaxed(val, slave->regs + offset);
427 struct cpdma_chan *ch;
433 struct cpsw_platform_data data;
434 struct napi_struct napi_rx;
435 struct napi_struct napi_tx;
436 struct cpsw_ss_regs __iomem *regs;
437 struct cpsw_wr_regs __iomem *wr_regs;
438 u8 __iomem *hw_stats;
439 struct cpsw_host_regs __iomem *host_port_regs;
444 struct cpsw_slave *slaves;
445 struct cpdma_ctlr *dma;
446 struct cpsw_vector txv[CPSW_MAX_QUEUES];
447 struct cpsw_vector rxv[CPSW_MAX_QUEUES];
448 struct cpsw_ale *ale;
450 bool rx_irq_disabled;
451 bool tx_irq_disabled;
452 u32 irqs_table[IRQ_NUM];
454 int rx_ch_num, tx_ch_num;
460 struct net_device *ndev;
463 u8 mac_addr[ETH_ALEN];
467 int fifo_bw[CPSW_TC_NUM];
470 struct cpsw_common *cpsw;
474 char stat_string[ETH_GSTRING_LEN];
486 #define CPSW_STAT(m) CPSW_STATS, \
487 FIELD_SIZEOF(struct cpsw_hw_stats, m), \
488 offsetof(struct cpsw_hw_stats, m)
489 #define CPDMA_RX_STAT(m) CPDMA_RX_STATS, \
490 FIELD_SIZEOF(struct cpdma_chan_stats, m), \
491 offsetof(struct cpdma_chan_stats, m)
492 #define CPDMA_TX_STAT(m) CPDMA_TX_STATS, \
493 FIELD_SIZEOF(struct cpdma_chan_stats, m), \
494 offsetof(struct cpdma_chan_stats, m)
496 static const struct cpsw_stats cpsw_gstrings_stats[] = {
497 { "Good Rx Frames", CPSW_STAT(rxgoodframes) },
498 { "Broadcast Rx Frames", CPSW_STAT(rxbroadcastframes) },
499 { "Multicast Rx Frames", CPSW_STAT(rxmulticastframes) },
500 { "Pause Rx Frames", CPSW_STAT(rxpauseframes) },
501 { "Rx CRC Errors", CPSW_STAT(rxcrcerrors) },
502 { "Rx Align/Code Errors", CPSW_STAT(rxaligncodeerrors) },
503 { "Oversize Rx Frames", CPSW_STAT(rxoversizedframes) },
504 { "Rx Jabbers", CPSW_STAT(rxjabberframes) },
505 { "Undersize (Short) Rx Frames", CPSW_STAT(rxundersizedframes) },
506 { "Rx Fragments", CPSW_STAT(rxfragments) },
507 { "Rx Octets", CPSW_STAT(rxoctets) },
508 { "Good Tx Frames", CPSW_STAT(txgoodframes) },
509 { "Broadcast Tx Frames", CPSW_STAT(txbroadcastframes) },
510 { "Multicast Tx Frames", CPSW_STAT(txmulticastframes) },
511 { "Pause Tx Frames", CPSW_STAT(txpauseframes) },
512 { "Deferred Tx Frames", CPSW_STAT(txdeferredframes) },
513 { "Collisions", CPSW_STAT(txcollisionframes) },
514 { "Single Collision Tx Frames", CPSW_STAT(txsinglecollframes) },
515 { "Multiple Collision Tx Frames", CPSW_STAT(txmultcollframes) },
516 { "Excessive Collisions", CPSW_STAT(txexcessivecollisions) },
517 { "Late Collisions", CPSW_STAT(txlatecollisions) },
518 { "Tx Underrun", CPSW_STAT(txunderrun) },
519 { "Carrier Sense Errors", CPSW_STAT(txcarriersenseerrors) },
520 { "Tx Octets", CPSW_STAT(txoctets) },
521 { "Rx + Tx 64 Octet Frames", CPSW_STAT(octetframes64) },
522 { "Rx + Tx 65-127 Octet Frames", CPSW_STAT(octetframes65t127) },
523 { "Rx + Tx 128-255 Octet Frames", CPSW_STAT(octetframes128t255) },
524 { "Rx + Tx 256-511 Octet Frames", CPSW_STAT(octetframes256t511) },
525 { "Rx + Tx 512-1023 Octet Frames", CPSW_STAT(octetframes512t1023) },
526 { "Rx + Tx 1024-Up Octet Frames", CPSW_STAT(octetframes1024tup) },
527 { "Net Octets", CPSW_STAT(netoctets) },
528 { "Rx Start of Frame Overruns", CPSW_STAT(rxsofoverruns) },
529 { "Rx Middle of Frame Overruns", CPSW_STAT(rxmofoverruns) },
530 { "Rx DMA Overruns", CPSW_STAT(rxdmaoverruns) },
533 static const struct cpsw_stats cpsw_gstrings_ch_stats[] = {
534 { "head_enqueue", CPDMA_RX_STAT(head_enqueue) },
535 { "tail_enqueue", CPDMA_RX_STAT(tail_enqueue) },
536 { "pad_enqueue", CPDMA_RX_STAT(pad_enqueue) },
537 { "misqueued", CPDMA_RX_STAT(misqueued) },
538 { "desc_alloc_fail", CPDMA_RX_STAT(desc_alloc_fail) },
539 { "pad_alloc_fail", CPDMA_RX_STAT(pad_alloc_fail) },
540 { "runt_receive_buf", CPDMA_RX_STAT(runt_receive_buff) },
541 { "runt_transmit_buf", CPDMA_RX_STAT(runt_transmit_buff) },
542 { "empty_dequeue", CPDMA_RX_STAT(empty_dequeue) },
543 { "busy_dequeue", CPDMA_RX_STAT(busy_dequeue) },
544 { "good_dequeue", CPDMA_RX_STAT(good_dequeue) },
545 { "requeue", CPDMA_RX_STAT(requeue) },
546 { "teardown_dequeue", CPDMA_RX_STAT(teardown_dequeue) },
549 #define CPSW_STATS_COMMON_LEN ARRAY_SIZE(cpsw_gstrings_stats)
550 #define CPSW_STATS_CH_LEN ARRAY_SIZE(cpsw_gstrings_ch_stats)
552 #define ndev_to_cpsw(ndev) (((struct cpsw_priv *)netdev_priv(ndev))->cpsw)
553 #define napi_to_cpsw(napi) container_of(napi, struct cpsw_common, napi)
554 #define for_each_slave(priv, func, arg...) \
556 struct cpsw_slave *slave; \
557 struct cpsw_common *cpsw = (priv)->cpsw; \
559 if (cpsw->data.dual_emac) \
560 (func)((cpsw)->slaves + priv->emac_port, ##arg);\
562 for (n = cpsw->data.slaves, \
563 slave = cpsw->slaves; \
565 (func)(slave++, ##arg); \
568 static inline int cpsw_get_slave_port(u32 slave_num)
570 return slave_num + 1;
573 static void cpsw_add_mcast(struct cpsw_priv *priv, const u8 *addr)
575 struct cpsw_common *cpsw = priv->cpsw;
577 if (cpsw->data.dual_emac) {
578 struct cpsw_slave *slave = cpsw->slaves + priv->emac_port;
580 cpsw_ale_add_mcast(cpsw->ale, addr, ALE_PORT_HOST,
581 ALE_VLAN, slave->port_vlan, 0);
585 cpsw_ale_add_mcast(cpsw->ale, addr, ALE_ALL_PORTS, 0, 0, 0);
588 static void cpsw_set_promiscious(struct net_device *ndev, bool enable)
590 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
591 struct cpsw_ale *ale = cpsw->ale;
594 if (cpsw->data.dual_emac) {
597 /* Enabling promiscuous mode for one interface will be
598 * common for both the interface as the interface shares
599 * the same hardware resource.
601 for (i = 0; i < cpsw->data.slaves; i++)
602 if (cpsw->slaves[i].ndev->flags & IFF_PROMISC)
605 if (!enable && flag) {
607 dev_err(&ndev->dev, "promiscuity not disabled as the other interface is still in promiscuity mode\n");
612 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 1);
614 dev_dbg(&ndev->dev, "promiscuity enabled\n");
617 cpsw_ale_control_set(ale, 0, ALE_BYPASS, 0);
618 dev_dbg(&ndev->dev, "promiscuity disabled\n");
622 unsigned long timeout = jiffies + HZ;
624 /* Disable Learn for all ports (host is port 0 and slaves are port 1 and up */
625 for (i = 0; i <= cpsw->data.slaves; i++) {
626 cpsw_ale_control_set(ale, i,
627 ALE_PORT_NOLEARN, 1);
628 cpsw_ale_control_set(ale, i,
629 ALE_PORT_NO_SA_UPDATE, 1);
632 /* Clear All Untouched entries */
633 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
636 if (cpsw_ale_control_get(ale, 0, ALE_AGEOUT))
638 } while (time_after(timeout, jiffies));
639 cpsw_ale_control_set(ale, 0, ALE_AGEOUT, 1);
641 /* Clear all mcast from ALE */
642 cpsw_ale_flush_multicast(ale, ALE_ALL_PORTS, -1);
644 /* Flood All Unicast Packets to Host port */
645 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 1);
646 dev_dbg(&ndev->dev, "promiscuity enabled\n");
648 /* Don't Flood All Unicast Packets to Host port */
649 cpsw_ale_control_set(ale, 0, ALE_P0_UNI_FLOOD, 0);
651 /* Enable Learn for all ports (host is port 0 and slaves are port 1 and up */
652 for (i = 0; i <= cpsw->data.slaves; i++) {
653 cpsw_ale_control_set(ale, i,
654 ALE_PORT_NOLEARN, 0);
655 cpsw_ale_control_set(ale, i,
656 ALE_PORT_NO_SA_UPDATE, 0);
658 dev_dbg(&ndev->dev, "promiscuity disabled\n");
663 static int cpsw_add_mc_addr(struct net_device *ndev, const u8 *addr)
665 struct cpsw_priv *priv = netdev_priv(ndev);
667 cpsw_add_mcast(priv, addr);
671 static int cpsw_del_mc_addr(struct net_device *ndev, const u8 *addr)
673 struct cpsw_priv *priv = netdev_priv(ndev);
674 struct cpsw_common *cpsw = priv->cpsw;
677 if (cpsw->data.dual_emac) {
678 vid = cpsw->slaves[priv->emac_port].port_vlan;
685 cpsw_ale_del_mcast(cpsw->ale, addr, 0, flags, vid);
689 static void cpsw_ndo_set_rx_mode(struct net_device *ndev)
691 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
693 if (ndev->flags & IFF_PROMISC) {
694 /* Enable promiscuous mode */
695 cpsw_set_promiscious(ndev, true);
696 cpsw_ale_set_allmulti(cpsw->ale, IFF_ALLMULTI);
699 /* Disable promiscuous mode */
700 cpsw_set_promiscious(ndev, false);
703 /* Restore allmulti on vlans if necessary */
704 cpsw_ale_set_allmulti(cpsw->ale, ndev->flags & IFF_ALLMULTI);
706 __dev_mc_sync(ndev, cpsw_add_mc_addr, cpsw_del_mc_addr);
709 static void cpsw_intr_enable(struct cpsw_common *cpsw)
711 writel_relaxed(0xFF, &cpsw->wr_regs->tx_en);
712 writel_relaxed(0xFF, &cpsw->wr_regs->rx_en);
714 cpdma_ctlr_int_ctrl(cpsw->dma, true);
718 static void cpsw_intr_disable(struct cpsw_common *cpsw)
720 writel_relaxed(0, &cpsw->wr_regs->tx_en);
721 writel_relaxed(0, &cpsw->wr_regs->rx_en);
723 cpdma_ctlr_int_ctrl(cpsw->dma, false);
727 static void cpsw_tx_handler(void *token, int len, int status)
729 struct netdev_queue *txq;
730 struct sk_buff *skb = token;
731 struct net_device *ndev = skb->dev;
732 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
734 /* Check whether the queue is stopped due to stalled tx dma, if the
735 * queue is stopped then start the queue as we have free desc for tx
737 txq = netdev_get_tx_queue(ndev, skb_get_queue_mapping(skb));
738 if (unlikely(netif_tx_queue_stopped(txq)))
739 netif_tx_wake_queue(txq);
741 cpts_tx_timestamp(cpsw->cpts, skb);
742 ndev->stats.tx_packets++;
743 ndev->stats.tx_bytes += len;
744 dev_kfree_skb_any(skb);
747 static void cpsw_rx_vlan_encap(struct sk_buff *skb)
749 struct cpsw_priv *priv = netdev_priv(skb->dev);
750 struct cpsw_common *cpsw = priv->cpsw;
751 u32 rx_vlan_encap_hdr = *((u32 *)skb->data);
752 u16 vtag, vid, prio, pkt_type;
754 /* Remove VLAN header encapsulation word */
755 skb_pull(skb, CPSW_RX_VLAN_ENCAP_HDR_SIZE);
757 pkt_type = (rx_vlan_encap_hdr >>
758 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT) &
759 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK;
760 /* Ignore unknown & Priority-tagged packets*/
761 if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV ||
762 pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG)
765 vid = (rx_vlan_encap_hdr >>
766 CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT) &
768 /* Ignore vid 0 and pass packet as is */
771 /* Ignore default vlans in dual mac mode */
772 if (cpsw->data.dual_emac &&
773 vid == cpsw->slaves[priv->emac_port].port_vlan)
776 prio = (rx_vlan_encap_hdr >>
777 CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT) &
778 CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK;
780 vtag = (prio << VLAN_PRIO_SHIFT) | vid;
781 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), vtag);
783 /* strip vlan tag for VLAN-tagged packet */
784 if (pkt_type == CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG) {
785 memmove(skb->data + VLAN_HLEN, skb->data, 2 * ETH_ALEN);
786 skb_pull(skb, VLAN_HLEN);
790 static void cpsw_rx_handler(void *token, int len, int status)
792 struct cpdma_chan *ch;
793 struct sk_buff *skb = token;
794 struct sk_buff *new_skb;
795 struct net_device *ndev = skb->dev;
797 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
799 if (cpsw->data.dual_emac) {
800 port = CPDMA_RX_SOURCE_PORT(status);
802 ndev = cpsw->slaves[--port].ndev;
807 if (unlikely(status < 0) || unlikely(!netif_running(ndev))) {
808 /* In dual emac mode check for all interfaces */
809 if (cpsw->data.dual_emac && cpsw->usage_count &&
811 /* The packet received is for the interface which
812 * is already down and the other interface is up
813 * and running, instead of freeing which results
814 * in reducing of the number of rx descriptor in
815 * DMA engine, requeue skb back to cpdma.
821 /* the interface is going down, skbs are purged */
822 dev_kfree_skb_any(skb);
826 new_skb = netdev_alloc_skb_ip_align(ndev, cpsw->rx_packet_max);
828 skb_copy_queue_mapping(new_skb, skb);
830 if (status & CPDMA_RX_VLAN_ENCAP)
831 cpsw_rx_vlan_encap(skb);
832 cpts_rx_timestamp(cpsw->cpts, skb);
833 skb->protocol = eth_type_trans(skb, ndev);
834 netif_receive_skb(skb);
835 ndev->stats.rx_bytes += len;
836 ndev->stats.rx_packets++;
837 kmemleak_not_leak(new_skb);
839 ndev->stats.rx_dropped++;
844 if (netif_dormant(ndev)) {
845 dev_kfree_skb_any(new_skb);
849 ch = cpsw->rxv[skb_get_queue_mapping(new_skb)].ch;
850 ret = cpdma_chan_submit(ch, new_skb, new_skb->data,
851 skb_tailroom(new_skb), 0);
852 if (WARN_ON(ret < 0))
853 dev_kfree_skb_any(new_skb);
856 static void cpsw_split_res(struct net_device *ndev)
858 struct cpsw_priv *priv = netdev_priv(ndev);
859 u32 consumed_rate = 0, bigest_rate = 0;
860 struct cpsw_common *cpsw = priv->cpsw;
861 struct cpsw_vector *txv = cpsw->txv;
862 int i, ch_weight, rlim_ch_num = 0;
863 int budget, bigest_rate_ch = 0;
864 u32 ch_rate, max_rate;
867 for (i = 0; i < cpsw->tx_ch_num; i++) {
868 ch_rate = cpdma_chan_get_rate(txv[i].ch);
873 consumed_rate += ch_rate;
876 if (cpsw->tx_ch_num == rlim_ch_num) {
877 max_rate = consumed_rate;
878 } else if (!rlim_ch_num) {
879 ch_budget = CPSW_POLL_WEIGHT / cpsw->tx_ch_num;
881 max_rate = consumed_rate;
883 max_rate = cpsw->speed * 1000;
885 /* if max_rate is less then expected due to reduced link speed,
886 * split proportionally according next potential max speed
888 if (max_rate < consumed_rate)
891 if (max_rate < consumed_rate)
894 ch_budget = (consumed_rate * CPSW_POLL_WEIGHT) / max_rate;
895 ch_budget = (CPSW_POLL_WEIGHT - ch_budget) /
896 (cpsw->tx_ch_num - rlim_ch_num);
897 bigest_rate = (max_rate - consumed_rate) /
898 (cpsw->tx_ch_num - rlim_ch_num);
901 /* split tx weight/budget */
902 budget = CPSW_POLL_WEIGHT;
903 for (i = 0; i < cpsw->tx_ch_num; i++) {
904 ch_rate = cpdma_chan_get_rate(txv[i].ch);
906 txv[i].budget = (ch_rate * CPSW_POLL_WEIGHT) / max_rate;
909 if (ch_rate > bigest_rate) {
911 bigest_rate = ch_rate;
914 ch_weight = (ch_rate * 100) / max_rate;
917 cpdma_chan_set_weight(cpsw->txv[i].ch, ch_weight);
919 txv[i].budget = ch_budget;
922 cpdma_chan_set_weight(cpsw->txv[i].ch, 0);
925 budget -= txv[i].budget;
929 txv[bigest_rate_ch].budget += budget;
931 /* split rx budget */
932 budget = CPSW_POLL_WEIGHT;
933 ch_budget = budget / cpsw->rx_ch_num;
934 for (i = 0; i < cpsw->rx_ch_num; i++) {
935 cpsw->rxv[i].budget = ch_budget;
940 cpsw->rxv[0].budget += budget;
943 static irqreturn_t cpsw_tx_interrupt(int irq, void *dev_id)
945 struct cpsw_common *cpsw = dev_id;
947 writel(0, &cpsw->wr_regs->tx_en);
948 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_TX);
950 if (cpsw->quirk_irq) {
951 disable_irq_nosync(cpsw->irqs_table[1]);
952 cpsw->tx_irq_disabled = true;
955 napi_schedule(&cpsw->napi_tx);
959 static irqreturn_t cpsw_rx_interrupt(int irq, void *dev_id)
961 struct cpsw_common *cpsw = dev_id;
963 cpdma_ctlr_eoi(cpsw->dma, CPDMA_EOI_RX);
964 writel(0, &cpsw->wr_regs->rx_en);
966 if (cpsw->quirk_irq) {
967 disable_irq_nosync(cpsw->irqs_table[0]);
968 cpsw->rx_irq_disabled = true;
971 napi_schedule(&cpsw->napi_rx);
975 static int cpsw_tx_mq_poll(struct napi_struct *napi_tx, int budget)
978 int num_tx, cur_budget, ch;
979 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
980 struct cpsw_vector *txv;
982 /* process every unprocessed channel */
983 ch_map = cpdma_ctrl_txchs_state(cpsw->dma);
984 for (ch = 0, num_tx = 0; ch_map & 0xff; ch_map <<= 1, ch++) {
985 if (!(ch_map & 0x80))
988 txv = &cpsw->txv[ch];
989 if (unlikely(txv->budget > budget - num_tx))
990 cur_budget = budget - num_tx;
992 cur_budget = txv->budget;
994 num_tx += cpdma_chan_process(txv->ch, cur_budget);
995 if (num_tx >= budget)
999 if (num_tx < budget) {
1000 napi_complete(napi_tx);
1001 writel(0xff, &cpsw->wr_regs->tx_en);
1007 static int cpsw_tx_poll(struct napi_struct *napi_tx, int budget)
1009 struct cpsw_common *cpsw = napi_to_cpsw(napi_tx);
1012 num_tx = cpdma_chan_process(cpsw->txv[0].ch, budget);
1013 if (num_tx < budget) {
1014 napi_complete(napi_tx);
1015 writel(0xff, &cpsw->wr_regs->tx_en);
1016 if (cpsw->tx_irq_disabled) {
1017 cpsw->tx_irq_disabled = false;
1018 enable_irq(cpsw->irqs_table[1]);
1025 static int cpsw_rx_mq_poll(struct napi_struct *napi_rx, int budget)
1028 int num_rx, cur_budget, ch;
1029 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
1030 struct cpsw_vector *rxv;
1032 /* process every unprocessed channel */
1033 ch_map = cpdma_ctrl_rxchs_state(cpsw->dma);
1034 for (ch = 0, num_rx = 0; ch_map; ch_map >>= 1, ch++) {
1035 if (!(ch_map & 0x01))
1038 rxv = &cpsw->rxv[ch];
1039 if (unlikely(rxv->budget > budget - num_rx))
1040 cur_budget = budget - num_rx;
1042 cur_budget = rxv->budget;
1044 num_rx += cpdma_chan_process(rxv->ch, cur_budget);
1045 if (num_rx >= budget)
1049 if (num_rx < budget) {
1050 napi_complete_done(napi_rx, num_rx);
1051 writel(0xff, &cpsw->wr_regs->rx_en);
1057 static int cpsw_rx_poll(struct napi_struct *napi_rx, int budget)
1059 struct cpsw_common *cpsw = napi_to_cpsw(napi_rx);
1062 num_rx = cpdma_chan_process(cpsw->rxv[0].ch, budget);
1063 if (num_rx < budget) {
1064 napi_complete_done(napi_rx, num_rx);
1065 writel(0xff, &cpsw->wr_regs->rx_en);
1066 if (cpsw->rx_irq_disabled) {
1067 cpsw->rx_irq_disabled = false;
1068 enable_irq(cpsw->irqs_table[0]);
1075 static inline void soft_reset(const char *module, void __iomem *reg)
1077 unsigned long timeout = jiffies + HZ;
1079 writel_relaxed(1, reg);
1082 } while ((readl_relaxed(reg) & 1) && time_after(timeout, jiffies));
1084 WARN(readl_relaxed(reg) & 1, "failed to soft-reset %s\n", module);
1087 static void cpsw_set_slave_mac(struct cpsw_slave *slave,
1088 struct cpsw_priv *priv)
1090 slave_write(slave, mac_hi(priv->mac_addr), SA_HI);
1091 slave_write(slave, mac_lo(priv->mac_addr), SA_LO);
1094 static bool cpsw_shp_is_off(struct cpsw_priv *priv)
1096 struct cpsw_common *cpsw = priv->cpsw;
1097 struct cpsw_slave *slave;
1098 u32 shift, mask, val;
1100 val = readl_relaxed(&cpsw->regs->ptype);
1102 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1103 shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
1110 static void cpsw_fifo_shp_on(struct cpsw_priv *priv, int fifo, int on)
1112 struct cpsw_common *cpsw = priv->cpsw;
1113 struct cpsw_slave *slave;
1114 u32 shift, mask, val;
1116 val = readl_relaxed(&cpsw->regs->ptype);
1118 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1119 shift = CPSW_FIFO_SHAPE_EN_SHIFT + 3 * slave->slave_num;
1120 mask = (1 << --fifo) << shift;
1121 val = on ? val | mask : val & ~mask;
1123 writel_relaxed(val, &cpsw->regs->ptype);
1126 static void _cpsw_adjust_link(struct cpsw_slave *slave,
1127 struct cpsw_priv *priv, bool *link)
1129 struct phy_device *phy = slave->phy;
1130 u32 mac_control = 0;
1132 struct cpsw_common *cpsw = priv->cpsw;
1137 slave_port = cpsw_get_slave_port(slave->slave_num);
1140 mac_control = cpsw->data.mac_control;
1142 /* enable forwarding */
1143 cpsw_ale_control_set(cpsw->ale, slave_port,
1144 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1146 if (phy->speed == 1000)
1147 mac_control |= BIT(7); /* GIGABITEN */
1149 mac_control |= BIT(0); /* FULLDUPLEXEN */
1151 /* set speed_in input in case RMII mode is used in 100Mbps */
1152 if (phy->speed == 100)
1153 mac_control |= BIT(15);
1154 /* in band mode only works in 10Mbps RGMII mode */
1155 else if ((phy->speed == 10) && phy_interface_is_rgmii(phy))
1156 mac_control |= BIT(18); /* In Band mode */
1159 mac_control |= BIT(3);
1162 mac_control |= BIT(4);
1166 if (priv->shp_cfg_speed &&
1167 priv->shp_cfg_speed != slave->phy->speed &&
1168 !cpsw_shp_is_off(priv))
1170 "Speed was changed, CBS shaper speeds are changed!");
1173 /* disable forwarding */
1174 cpsw_ale_control_set(cpsw->ale, slave_port,
1175 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1178 if (mac_control != slave->mac_control) {
1179 phy_print_status(phy);
1180 writel_relaxed(mac_control, &slave->sliver->mac_control);
1183 slave->mac_control = mac_control;
1186 static int cpsw_get_common_speed(struct cpsw_common *cpsw)
1190 for (i = 0, speed = 0; i < cpsw->data.slaves; i++)
1191 if (cpsw->slaves[i].phy && cpsw->slaves[i].phy->link)
1192 speed += cpsw->slaves[i].phy->speed;
1197 static int cpsw_need_resplit(struct cpsw_common *cpsw)
1202 /* re-split resources only in case speed was changed */
1203 speed = cpsw_get_common_speed(cpsw);
1204 if (speed == cpsw->speed || !speed)
1207 cpsw->speed = speed;
1209 for (i = 0, rlim_ch_num = 0; i < cpsw->tx_ch_num; i++) {
1210 ch_rate = cpdma_chan_get_rate(cpsw->txv[i].ch);
1217 /* cases not dependent on speed */
1218 if (!rlim_ch_num || rlim_ch_num == cpsw->tx_ch_num)
1224 static void cpsw_adjust_link(struct net_device *ndev)
1226 struct cpsw_priv *priv = netdev_priv(ndev);
1227 struct cpsw_common *cpsw = priv->cpsw;
1230 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
1233 if (cpsw_need_resplit(cpsw))
1234 cpsw_split_res(ndev);
1236 netif_carrier_on(ndev);
1237 if (netif_running(ndev))
1238 netif_tx_wake_all_queues(ndev);
1240 netif_carrier_off(ndev);
1241 netif_tx_stop_all_queues(ndev);
1245 static int cpsw_get_coalesce(struct net_device *ndev,
1246 struct ethtool_coalesce *coal)
1248 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1250 coal->rx_coalesce_usecs = cpsw->coal_intvl;
1254 static int cpsw_set_coalesce(struct net_device *ndev,
1255 struct ethtool_coalesce *coal)
1257 struct cpsw_priv *priv = netdev_priv(ndev);
1259 u32 num_interrupts = 0;
1263 struct cpsw_common *cpsw = priv->cpsw;
1265 coal_intvl = coal->rx_coalesce_usecs;
1267 int_ctrl = readl(&cpsw->wr_regs->int_control);
1268 prescale = cpsw->bus_freq_mhz * 4;
1270 if (!coal->rx_coalesce_usecs) {
1271 int_ctrl &= ~(CPSW_INTPRESCALE_MASK | CPSW_INTPACEEN);
1275 if (coal_intvl < CPSW_CMINTMIN_INTVL)
1276 coal_intvl = CPSW_CMINTMIN_INTVL;
1278 if (coal_intvl > CPSW_CMINTMAX_INTVL) {
1279 /* Interrupt pacer works with 4us Pulse, we can
1280 * throttle further by dilating the 4us pulse.
1282 addnl_dvdr = CPSW_INTPRESCALE_MASK / prescale;
1284 if (addnl_dvdr > 1) {
1285 prescale *= addnl_dvdr;
1286 if (coal_intvl > (CPSW_CMINTMAX_INTVL * addnl_dvdr))
1287 coal_intvl = (CPSW_CMINTMAX_INTVL
1291 coal_intvl = CPSW_CMINTMAX_INTVL;
1295 num_interrupts = (1000 * addnl_dvdr) / coal_intvl;
1296 writel(num_interrupts, &cpsw->wr_regs->rx_imax);
1297 writel(num_interrupts, &cpsw->wr_regs->tx_imax);
1299 int_ctrl |= CPSW_INTPACEEN;
1300 int_ctrl &= (~CPSW_INTPRESCALE_MASK);
1301 int_ctrl |= (prescale & CPSW_INTPRESCALE_MASK);
1304 writel(int_ctrl, &cpsw->wr_regs->int_control);
1306 cpsw_notice(priv, timer, "Set coalesce to %d usecs.\n", coal_intvl);
1307 cpsw->coal_intvl = coal_intvl;
1312 static int cpsw_get_sset_count(struct net_device *ndev, int sset)
1314 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1318 return (CPSW_STATS_COMMON_LEN +
1319 (cpsw->rx_ch_num + cpsw->tx_ch_num) *
1326 static void cpsw_add_ch_strings(u8 **p, int ch_num, int rx_dir)
1332 ch_stats_len = CPSW_STATS_CH_LEN * ch_num;
1333 for (i = 0; i < ch_stats_len; i++) {
1334 line = i % CPSW_STATS_CH_LEN;
1335 snprintf(*p, ETH_GSTRING_LEN,
1336 "%s DMA chan %ld: %s", rx_dir ? "Rx" : "Tx",
1337 (long)(i / CPSW_STATS_CH_LEN),
1338 cpsw_gstrings_ch_stats[line].stat_string);
1339 *p += ETH_GSTRING_LEN;
1343 static void cpsw_get_strings(struct net_device *ndev, u32 stringset, u8 *data)
1345 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1349 switch (stringset) {
1351 for (i = 0; i < CPSW_STATS_COMMON_LEN; i++) {
1352 memcpy(p, cpsw_gstrings_stats[i].stat_string,
1354 p += ETH_GSTRING_LEN;
1357 cpsw_add_ch_strings(&p, cpsw->rx_ch_num, 1);
1358 cpsw_add_ch_strings(&p, cpsw->tx_ch_num, 0);
1363 static void cpsw_get_ethtool_stats(struct net_device *ndev,
1364 struct ethtool_stats *stats, u64 *data)
1367 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
1368 struct cpdma_chan_stats ch_stats;
1371 /* Collect Davinci CPDMA stats for Rx and Tx Channel */
1372 for (l = 0; l < CPSW_STATS_COMMON_LEN; l++)
1373 data[l] = readl(cpsw->hw_stats +
1374 cpsw_gstrings_stats[l].stat_offset);
1376 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1377 cpdma_chan_get_stats(cpsw->rxv[ch].ch, &ch_stats);
1378 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1379 p = (u8 *)&ch_stats +
1380 cpsw_gstrings_ch_stats[i].stat_offset;
1381 data[l] = *(u32 *)p;
1385 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
1386 cpdma_chan_get_stats(cpsw->txv[ch].ch, &ch_stats);
1387 for (i = 0; i < CPSW_STATS_CH_LEN; i++, l++) {
1388 p = (u8 *)&ch_stats +
1389 cpsw_gstrings_ch_stats[i].stat_offset;
1390 data[l] = *(u32 *)p;
1395 static inline int cpsw_tx_packet_submit(struct cpsw_priv *priv,
1396 struct sk_buff *skb,
1397 struct cpdma_chan *txch)
1399 struct cpsw_common *cpsw = priv->cpsw;
1401 skb_tx_timestamp(skb);
1402 return cpdma_chan_submit(txch, skb, skb->data, skb->len,
1403 priv->emac_port + cpsw->data.dual_emac);
1406 static inline void cpsw_add_dual_emac_def_ale_entries(
1407 struct cpsw_priv *priv, struct cpsw_slave *slave,
1410 struct cpsw_common *cpsw = priv->cpsw;
1411 u32 port_mask = 1 << slave_port | ALE_PORT_HOST;
1413 if (cpsw->version == CPSW_VERSION_1)
1414 slave_write(slave, slave->port_vlan, CPSW1_PORT_VLAN);
1416 slave_write(slave, slave->port_vlan, CPSW2_PORT_VLAN);
1417 cpsw_ale_add_vlan(cpsw->ale, slave->port_vlan, port_mask,
1418 port_mask, port_mask, 0);
1419 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1420 ALE_PORT_HOST, ALE_VLAN, slave->port_vlan, 0);
1421 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
1422 HOST_PORT_NUM, ALE_VLAN |
1423 ALE_SECURE, slave->port_vlan);
1424 cpsw_ale_control_set(cpsw->ale, slave_port,
1425 ALE_PORT_DROP_UNKNOWN_VLAN, 1);
1428 static void soft_reset_slave(struct cpsw_slave *slave)
1432 snprintf(name, sizeof(name), "slave-%d", slave->slave_num);
1433 soft_reset(name, &slave->sliver->soft_reset);
1436 static void cpsw_slave_open(struct cpsw_slave *slave, struct cpsw_priv *priv)
1439 struct phy_device *phy;
1440 struct cpsw_common *cpsw = priv->cpsw;
1442 soft_reset_slave(slave);
1444 /* setup priority mapping */
1445 writel_relaxed(RX_PRIORITY_MAPPING, &slave->sliver->rx_pri_map);
1447 switch (cpsw->version) {
1448 case CPSW_VERSION_1:
1449 slave_write(slave, TX_PRIORITY_MAPPING, CPSW1_TX_PRI_MAP);
1450 /* Increase RX FIFO size to 5 for supporting fullduplex
1454 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1455 CPSW_MAX_BLKS_RX, CPSW1_MAX_BLKS);
1457 case CPSW_VERSION_2:
1458 case CPSW_VERSION_3:
1459 case CPSW_VERSION_4:
1460 slave_write(slave, TX_PRIORITY_MAPPING, CPSW2_TX_PRI_MAP);
1461 /* Increase RX FIFO size to 5 for supporting fullduplex
1465 (CPSW_MAX_BLKS_TX << CPSW_MAX_BLKS_TX_SHIFT) |
1466 CPSW_MAX_BLKS_RX, CPSW2_MAX_BLKS);
1470 /* setup max packet size, and mac address */
1471 writel_relaxed(cpsw->rx_packet_max, &slave->sliver->rx_maxlen);
1472 cpsw_set_slave_mac(slave, priv);
1474 slave->mac_control = 0; /* no link yet */
1476 slave_port = cpsw_get_slave_port(slave->slave_num);
1478 if (cpsw->data.dual_emac)
1479 cpsw_add_dual_emac_def_ale_entries(priv, slave, slave_port);
1481 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1482 1 << slave_port, 0, 0, ALE_MCAST_FWD_2);
1484 if (slave->data->phy_node) {
1485 phy = of_phy_connect(priv->ndev, slave->data->phy_node,
1486 &cpsw_adjust_link, 0, slave->data->phy_if);
1488 dev_err(priv->dev, "phy \"%pOF\" not found on slave %d\n",
1489 slave->data->phy_node,
1494 phy = phy_connect(priv->ndev, slave->data->phy_id,
1495 &cpsw_adjust_link, slave->data->phy_if);
1498 "phy \"%s\" not found on slave %d, err %ld\n",
1499 slave->data->phy_id, slave->slave_num,
1507 phy_attached_info(slave->phy);
1509 phy_start(slave->phy);
1511 /* Configure GMII_SEL register */
1512 cpsw_phy_sel(cpsw->dev, slave->phy->interface, slave->slave_num);
1515 static inline void cpsw_add_default_vlan(struct cpsw_priv *priv)
1517 struct cpsw_common *cpsw = priv->cpsw;
1518 const int vlan = cpsw->data.default_vlan;
1521 int unreg_mcast_mask;
1523 reg = (cpsw->version == CPSW_VERSION_1) ? CPSW1_PORT_VLAN :
1526 writel(vlan, &cpsw->host_port_regs->port_vlan);
1528 for (i = 0; i < cpsw->data.slaves; i++)
1529 slave_write(cpsw->slaves + i, vlan, reg);
1531 if (priv->ndev->flags & IFF_ALLMULTI)
1532 unreg_mcast_mask = ALE_ALL_PORTS;
1534 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
1536 cpsw_ale_add_vlan(cpsw->ale, vlan, ALE_ALL_PORTS,
1537 ALE_ALL_PORTS, ALE_ALL_PORTS,
1541 static void cpsw_init_host_port(struct cpsw_priv *priv)
1545 struct cpsw_common *cpsw = priv->cpsw;
1547 /* soft reset the controller and initialize ale */
1548 soft_reset("cpsw", &cpsw->regs->soft_reset);
1549 cpsw_ale_start(cpsw->ale);
1551 /* switch to vlan unaware mode */
1552 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM, ALE_VLAN_AWARE,
1553 CPSW_ALE_VLAN_AWARE);
1554 control_reg = readl(&cpsw->regs->control);
1555 control_reg |= CPSW_VLAN_AWARE | CPSW_RX_VLAN_ENCAP;
1556 writel(control_reg, &cpsw->regs->control);
1557 fifo_mode = (cpsw->data.dual_emac) ? CPSW_FIFO_DUAL_MAC_MODE :
1558 CPSW_FIFO_NORMAL_MODE;
1559 writel(fifo_mode, &cpsw->host_port_regs->tx_in_ctl);
1561 /* setup host port priority mapping */
1562 writel_relaxed(CPDMA_TX_PRIORITY_MAP,
1563 &cpsw->host_port_regs->cpdma_tx_pri_map);
1564 writel_relaxed(0, &cpsw->host_port_regs->cpdma_rx_chan_map);
1566 cpsw_ale_control_set(cpsw->ale, HOST_PORT_NUM,
1567 ALE_PORT_STATE, ALE_PORT_STATE_FORWARD);
1569 if (!cpsw->data.dual_emac) {
1570 cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
1572 cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
1573 ALE_PORT_HOST, 0, 0, ALE_MCAST_FWD_2);
1577 static int cpsw_fill_rx_channels(struct cpsw_priv *priv)
1579 struct cpsw_common *cpsw = priv->cpsw;
1580 struct sk_buff *skb;
1584 for (ch = 0; ch < cpsw->rx_ch_num; ch++) {
1585 ch_buf_num = cpdma_chan_get_rx_buf_num(cpsw->rxv[ch].ch);
1586 for (i = 0; i < ch_buf_num; i++) {
1587 skb = __netdev_alloc_skb_ip_align(priv->ndev,
1588 cpsw->rx_packet_max,
1591 cpsw_err(priv, ifup, "cannot allocate skb\n");
1595 skb_set_queue_mapping(skb, ch);
1596 ret = cpdma_chan_submit(cpsw->rxv[ch].ch, skb,
1597 skb->data, skb_tailroom(skb),
1600 cpsw_err(priv, ifup,
1601 "cannot submit skb to channel %d rx, error %d\n",
1606 kmemleak_not_leak(skb);
1609 cpsw_info(priv, ifup, "ch %d rx, submitted %d descriptors\n",
1616 static void cpsw_slave_stop(struct cpsw_slave *slave, struct cpsw_common *cpsw)
1620 slave_port = cpsw_get_slave_port(slave->slave_num);
1624 phy_stop(slave->phy);
1625 phy_disconnect(slave->phy);
1627 cpsw_ale_control_set(cpsw->ale, slave_port,
1628 ALE_PORT_STATE, ALE_PORT_STATE_DISABLE);
1629 soft_reset_slave(slave);
1632 static int cpsw_tc_to_fifo(int tc, int num_tc)
1634 if (tc == num_tc - 1)
1637 return CPSW_FIFO_SHAPERS_NUM - tc;
1640 static int cpsw_set_fifo_bw(struct cpsw_priv *priv, int fifo, int bw)
1642 struct cpsw_common *cpsw = priv->cpsw;
1643 u32 val = 0, send_pct, shift;
1644 struct cpsw_slave *slave;
1647 if (bw > priv->shp_cfg_speed * 1000)
1650 /* shaping has to stay enabled for highest fifos linearly
1651 * and fifo bw no more then interface can allow
1653 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1654 send_pct = slave_read(slave, SEND_PERCENT);
1655 for (i = CPSW_FIFO_SHAPERS_NUM; i > 0; i--) {
1657 if (i >= fifo || !priv->fifo_bw[i])
1660 dev_warn(priv->dev, "Prev FIFO%d is shaped", i);
1664 if (!priv->fifo_bw[i] && i > fifo) {
1665 dev_err(priv->dev, "Upper FIFO%d is not shaped", i);
1669 shift = (i - 1) * 8;
1671 send_pct &= ~(CPSW_PCT_MASK << shift);
1672 val = DIV_ROUND_UP(bw, priv->shp_cfg_speed * 10);
1676 send_pct |= val << shift;
1681 if (priv->fifo_bw[i])
1682 pct += (send_pct >> shift) & CPSW_PCT_MASK;
1688 slave_write(slave, send_pct, SEND_PERCENT);
1689 priv->fifo_bw[fifo] = bw;
1691 dev_warn(priv->dev, "set FIFO%d bw = %d\n", fifo,
1692 DIV_ROUND_CLOSEST(val * priv->shp_cfg_speed, 100));
1696 dev_err(priv->dev, "Bandwidth doesn't fit in tc configuration");
1700 static int cpsw_set_fifo_rlimit(struct cpsw_priv *priv, int fifo, int bw)
1702 struct cpsw_common *cpsw = priv->cpsw;
1703 struct cpsw_slave *slave;
1704 u32 tx_in_ctl_rg, val;
1707 ret = cpsw_set_fifo_bw(priv, fifo, bw);
1711 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1712 tx_in_ctl_rg = cpsw->version == CPSW_VERSION_1 ?
1713 CPSW1_TX_IN_CTL : CPSW2_TX_IN_CTL;
1716 cpsw_fifo_shp_on(priv, fifo, bw);
1718 val = slave_read(slave, tx_in_ctl_rg);
1719 if (cpsw_shp_is_off(priv)) {
1720 /* disable FIFOs rate limited queues */
1721 val &= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT);
1723 /* set type of FIFO queues to normal priority mode */
1724 val &= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT);
1726 /* set type of FIFO queues to be rate limited */
1728 val |= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT;
1730 priv->shp_cfg_speed = 0;
1733 /* toggle a FIFO rate limited queue */
1735 val |= BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
1737 val &= ~BIT(fifo + CPSW_FIFO_RATE_EN_SHIFT);
1738 slave_write(slave, val, tx_in_ctl_rg);
1740 /* FIFO transmit shape enable */
1741 cpsw_fifo_shp_on(priv, fifo, bw);
1748 * shaping for class A should be set first
1750 static int cpsw_set_cbs(struct net_device *ndev,
1751 struct tc_cbs_qopt_offload *qopt)
1753 struct cpsw_priv *priv = netdev_priv(ndev);
1754 struct cpsw_common *cpsw = priv->cpsw;
1755 struct cpsw_slave *slave;
1760 tc = netdev_txq_to_tc(priv->ndev, qopt->queue);
1762 /* enable channels in backward order, as highest FIFOs must be rate
1763 * limited first and for compliance with CPDMA rate limited channels
1764 * that also used in bacward order. FIFO0 cannot be rate limited.
1766 fifo = cpsw_tc_to_fifo(tc, ndev->num_tc);
1768 dev_err(priv->dev, "Last tc%d can't be rate limited", tc);
1772 /* do nothing, it's disabled anyway */
1773 if (!qopt->enable && !priv->fifo_bw[fifo])
1776 /* shapers can be set if link speed is known */
1777 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
1778 if (slave->phy && slave->phy->link) {
1779 if (priv->shp_cfg_speed &&
1780 priv->shp_cfg_speed != slave->phy->speed)
1781 prev_speed = priv->shp_cfg_speed;
1783 priv->shp_cfg_speed = slave->phy->speed;
1786 if (!priv->shp_cfg_speed) {
1787 dev_err(priv->dev, "Link speed is not known");
1791 ret = pm_runtime_get_sync(cpsw->dev);
1793 pm_runtime_put_noidle(cpsw->dev);
1797 bw = qopt->enable ? qopt->idleslope : 0;
1798 ret = cpsw_set_fifo_rlimit(priv, fifo, bw);
1800 priv->shp_cfg_speed = prev_speed;
1804 if (bw && prev_speed)
1806 "Speed was changed, CBS shaper speeds are changed!");
1808 pm_runtime_put_sync(cpsw->dev);
1812 static void cpsw_cbs_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
1816 for (fifo = CPSW_FIFO_SHAPERS_NUM; fifo > 0; fifo--) {
1817 bw = priv->fifo_bw[fifo];
1821 cpsw_set_fifo_rlimit(priv, fifo, bw);
1825 static void cpsw_mqprio_resume(struct cpsw_slave *slave, struct cpsw_priv *priv)
1827 struct cpsw_common *cpsw = priv->cpsw;
1828 u32 tx_prio_map = 0;
1832 if (!priv->mqprio_hw)
1835 for (i = 0; i < 8; i++) {
1836 tc = netdev_get_prio_tc_map(priv->ndev, i);
1837 fifo = CPSW_FIFO_SHAPERS_NUM - tc;
1838 tx_prio_map |= fifo << (4 * i);
1841 tx_prio_rg = cpsw->version == CPSW_VERSION_1 ?
1842 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
1844 slave_write(slave, tx_prio_map, tx_prio_rg);
1847 /* restore resources after port reset */
1848 static void cpsw_restore(struct cpsw_priv *priv)
1850 /* restore MQPRIO offload */
1851 for_each_slave(priv, cpsw_mqprio_resume, priv);
1853 /* restore CBS offload */
1854 for_each_slave(priv, cpsw_cbs_resume, priv);
1857 static int cpsw_ndo_open(struct net_device *ndev)
1859 struct cpsw_priv *priv = netdev_priv(ndev);
1860 struct cpsw_common *cpsw = priv->cpsw;
1864 ret = pm_runtime_get_sync(cpsw->dev);
1866 pm_runtime_put_noidle(cpsw->dev);
1870 netif_carrier_off(ndev);
1872 /* Notify the stack of the actual queue counts. */
1873 ret = netif_set_real_num_tx_queues(ndev, cpsw->tx_ch_num);
1875 dev_err(priv->dev, "cannot set real number of tx queues\n");
1879 ret = netif_set_real_num_rx_queues(ndev, cpsw->rx_ch_num);
1881 dev_err(priv->dev, "cannot set real number of rx queues\n");
1885 reg = cpsw->version;
1887 dev_info(priv->dev, "initializing cpsw version %d.%d (%d)\n",
1888 CPSW_MAJOR_VERSION(reg), CPSW_MINOR_VERSION(reg),
1889 CPSW_RTL_VERSION(reg));
1891 /* Initialize host and slave ports */
1892 if (!cpsw->usage_count)
1893 cpsw_init_host_port(priv);
1894 for_each_slave(priv, cpsw_slave_open, priv);
1896 /* Add default VLAN */
1897 if (!cpsw->data.dual_emac)
1898 cpsw_add_default_vlan(priv);
1900 cpsw_ale_add_vlan(cpsw->ale, cpsw->data.default_vlan,
1901 ALE_ALL_PORTS, ALE_ALL_PORTS, 0, 0);
1903 /* initialize shared resources for every ndev */
1904 if (!cpsw->usage_count) {
1905 /* disable priority elevation */
1906 writel_relaxed(0, &cpsw->regs->ptype);
1908 /* enable statistics collection only on all ports */
1909 writel_relaxed(0x7, &cpsw->regs->stat_port_en);
1911 /* Enable internal fifo flow control */
1912 writel(0x7, &cpsw->regs->flow_control);
1914 napi_enable(&cpsw->napi_rx);
1915 napi_enable(&cpsw->napi_tx);
1917 if (cpsw->tx_irq_disabled) {
1918 cpsw->tx_irq_disabled = false;
1919 enable_irq(cpsw->irqs_table[1]);
1922 if (cpsw->rx_irq_disabled) {
1923 cpsw->rx_irq_disabled = false;
1924 enable_irq(cpsw->irqs_table[0]);
1927 ret = cpsw_fill_rx_channels(priv);
1931 if (cpts_register(cpsw->cpts))
1932 dev_err(priv->dev, "error registering cpts device\n");
1938 /* Enable Interrupt pacing if configured */
1939 if (cpsw->coal_intvl != 0) {
1940 struct ethtool_coalesce coal;
1942 coal.rx_coalesce_usecs = cpsw->coal_intvl;
1943 cpsw_set_coalesce(ndev, &coal);
1946 cpdma_ctlr_start(cpsw->dma);
1947 cpsw_intr_enable(cpsw);
1948 cpsw->usage_count++;
1953 cpdma_ctlr_stop(cpsw->dma);
1954 for_each_slave(priv, cpsw_slave_stop, cpsw);
1955 pm_runtime_put_sync(cpsw->dev);
1956 netif_carrier_off(priv->ndev);
1960 static int cpsw_ndo_stop(struct net_device *ndev)
1962 struct cpsw_priv *priv = netdev_priv(ndev);
1963 struct cpsw_common *cpsw = priv->cpsw;
1965 cpsw_info(priv, ifdown, "shutting down cpsw device\n");
1966 __dev_mc_unsync(priv->ndev, cpsw_del_mc_addr);
1967 netif_tx_stop_all_queues(priv->ndev);
1968 netif_carrier_off(priv->ndev);
1970 if (cpsw->usage_count <= 1) {
1971 napi_disable(&cpsw->napi_rx);
1972 napi_disable(&cpsw->napi_tx);
1973 cpts_unregister(cpsw->cpts);
1974 cpsw_intr_disable(cpsw);
1975 cpdma_ctlr_stop(cpsw->dma);
1976 cpsw_ale_stop(cpsw->ale);
1978 for_each_slave(priv, cpsw_slave_stop, cpsw);
1980 if (cpsw_need_resplit(cpsw))
1981 cpsw_split_res(ndev);
1983 cpsw->usage_count--;
1984 pm_runtime_put_sync(cpsw->dev);
1988 static netdev_tx_t cpsw_ndo_start_xmit(struct sk_buff *skb,
1989 struct net_device *ndev)
1991 struct cpsw_priv *priv = netdev_priv(ndev);
1992 struct cpsw_common *cpsw = priv->cpsw;
1993 struct cpts *cpts = cpsw->cpts;
1994 struct netdev_queue *txq;
1995 struct cpdma_chan *txch;
1998 if (skb_padto(skb, CPSW_MIN_PACKET_SIZE)) {
1999 cpsw_err(priv, tx_err, "packet pad failed\n");
2000 ndev->stats.tx_dropped++;
2001 return NET_XMIT_DROP;
2004 if (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP &&
2005 cpts_is_tx_enabled(cpts) && cpts_can_timestamp(cpts, skb))
2006 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
2008 q_idx = skb_get_queue_mapping(skb);
2009 if (q_idx >= cpsw->tx_ch_num)
2010 q_idx = q_idx % cpsw->tx_ch_num;
2012 txch = cpsw->txv[q_idx].ch;
2013 txq = netdev_get_tx_queue(ndev, q_idx);
2014 ret = cpsw_tx_packet_submit(priv, skb, txch);
2015 if (unlikely(ret != 0)) {
2016 cpsw_err(priv, tx_err, "desc submit failed\n");
2020 /* If there is no more tx desc left free then we need to
2021 * tell the kernel to stop sending us tx frames.
2023 if (unlikely(!cpdma_check_free_tx_desc(txch))) {
2024 netif_tx_stop_queue(txq);
2026 /* Barrier, so that stop_queue visible to other cpus */
2027 smp_mb__after_atomic();
2029 if (cpdma_check_free_tx_desc(txch))
2030 netif_tx_wake_queue(txq);
2033 return NETDEV_TX_OK;
2035 ndev->stats.tx_dropped++;
2036 netif_tx_stop_queue(txq);
2038 /* Barrier, so that stop_queue visible to other cpus */
2039 smp_mb__after_atomic();
2041 if (cpdma_check_free_tx_desc(txch))
2042 netif_tx_wake_queue(txq);
2044 return NETDEV_TX_BUSY;
2047 #if IS_ENABLED(CONFIG_TI_CPTS)
2049 static void cpsw_hwtstamp_v1(struct cpsw_common *cpsw)
2051 struct cpsw_slave *slave = &cpsw->slaves[cpsw->data.active_slave];
2054 if (!cpts_is_tx_enabled(cpsw->cpts) &&
2055 !cpts_is_rx_enabled(cpsw->cpts)) {
2056 slave_write(slave, 0, CPSW1_TS_CTL);
2060 seq_id = (30 << CPSW_V1_SEQ_ID_OFS_SHIFT) | ETH_P_1588;
2061 ts_en = EVENT_MSG_BITS << CPSW_V1_MSG_TYPE_OFS;
2063 if (cpts_is_tx_enabled(cpsw->cpts))
2064 ts_en |= CPSW_V1_TS_TX_EN;
2066 if (cpts_is_rx_enabled(cpsw->cpts))
2067 ts_en |= CPSW_V1_TS_RX_EN;
2069 slave_write(slave, ts_en, CPSW1_TS_CTL);
2070 slave_write(slave, seq_id, CPSW1_TS_SEQ_LTYPE);
2073 static void cpsw_hwtstamp_v2(struct cpsw_priv *priv)
2075 struct cpsw_slave *slave;
2076 struct cpsw_common *cpsw = priv->cpsw;
2079 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
2081 ctrl = slave_read(slave, CPSW2_CONTROL);
2082 switch (cpsw->version) {
2083 case CPSW_VERSION_2:
2084 ctrl &= ~CTRL_V2_ALL_TS_MASK;
2086 if (cpts_is_tx_enabled(cpsw->cpts))
2087 ctrl |= CTRL_V2_TX_TS_BITS;
2089 if (cpts_is_rx_enabled(cpsw->cpts))
2090 ctrl |= CTRL_V2_RX_TS_BITS;
2092 case CPSW_VERSION_3:
2094 ctrl &= ~CTRL_V3_ALL_TS_MASK;
2096 if (cpts_is_tx_enabled(cpsw->cpts))
2097 ctrl |= CTRL_V3_TX_TS_BITS;
2099 if (cpts_is_rx_enabled(cpsw->cpts))
2100 ctrl |= CTRL_V3_RX_TS_BITS;
2104 mtype = (30 << TS_SEQ_ID_OFFSET_SHIFT) | EVENT_MSG_BITS;
2106 slave_write(slave, mtype, CPSW2_TS_SEQ_MTYPE);
2107 slave_write(slave, ctrl, CPSW2_CONTROL);
2108 writel_relaxed(ETH_P_1588, &cpsw->regs->ts_ltype);
2111 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2113 struct cpsw_priv *priv = netdev_priv(dev);
2114 struct hwtstamp_config cfg;
2115 struct cpsw_common *cpsw = priv->cpsw;
2116 struct cpts *cpts = cpsw->cpts;
2118 if (cpsw->version != CPSW_VERSION_1 &&
2119 cpsw->version != CPSW_VERSION_2 &&
2120 cpsw->version != CPSW_VERSION_3)
2123 if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg)))
2126 /* reserved for future extensions */
2130 if (cfg.tx_type != HWTSTAMP_TX_OFF && cfg.tx_type != HWTSTAMP_TX_ON)
2133 switch (cfg.rx_filter) {
2134 case HWTSTAMP_FILTER_NONE:
2135 cpts_rx_enable(cpts, 0);
2137 case HWTSTAMP_FILTER_ALL:
2138 case HWTSTAMP_FILTER_NTP_ALL:
2140 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
2141 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
2142 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
2143 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V1_L4_EVENT);
2144 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V1_L4_EVENT;
2146 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
2147 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
2148 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
2149 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
2150 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
2151 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
2152 case HWTSTAMP_FILTER_PTP_V2_EVENT:
2153 case HWTSTAMP_FILTER_PTP_V2_SYNC:
2154 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
2155 cpts_rx_enable(cpts, HWTSTAMP_FILTER_PTP_V2_EVENT);
2156 cfg.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
2162 cpts_tx_enable(cpts, cfg.tx_type == HWTSTAMP_TX_ON);
2164 switch (cpsw->version) {
2165 case CPSW_VERSION_1:
2166 cpsw_hwtstamp_v1(cpsw);
2168 case CPSW_VERSION_2:
2169 case CPSW_VERSION_3:
2170 cpsw_hwtstamp_v2(priv);
2176 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
2179 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2181 struct cpsw_common *cpsw = ndev_to_cpsw(dev);
2182 struct cpts *cpts = cpsw->cpts;
2183 struct hwtstamp_config cfg;
2185 if (cpsw->version != CPSW_VERSION_1 &&
2186 cpsw->version != CPSW_VERSION_2 &&
2187 cpsw->version != CPSW_VERSION_3)
2191 cfg.tx_type = cpts_is_tx_enabled(cpts) ?
2192 HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
2193 cfg.rx_filter = (cpts_is_rx_enabled(cpts) ?
2194 cpts->rx_enable : HWTSTAMP_FILTER_NONE);
2196 return copy_to_user(ifr->ifr_data, &cfg, sizeof(cfg)) ? -EFAULT : 0;
2199 static int cpsw_hwtstamp_get(struct net_device *dev, struct ifreq *ifr)
2204 static int cpsw_hwtstamp_set(struct net_device *dev, struct ifreq *ifr)
2208 #endif /*CONFIG_TI_CPTS*/
2210 static int cpsw_ndo_ioctl(struct net_device *dev, struct ifreq *req, int cmd)
2212 struct cpsw_priv *priv = netdev_priv(dev);
2213 struct cpsw_common *cpsw = priv->cpsw;
2214 int slave_no = cpsw_slave_index(cpsw, priv);
2216 if (!netif_running(dev))
2221 return cpsw_hwtstamp_set(dev, req);
2223 return cpsw_hwtstamp_get(dev, req);
2226 if (!cpsw->slaves[slave_no].phy)
2228 return phy_mii_ioctl(cpsw->slaves[slave_no].phy, req, cmd);
2231 static void cpsw_ndo_tx_timeout(struct net_device *ndev)
2233 struct cpsw_priv *priv = netdev_priv(ndev);
2234 struct cpsw_common *cpsw = priv->cpsw;
2237 cpsw_err(priv, tx_err, "transmit timeout, restarting dma\n");
2238 ndev->stats.tx_errors++;
2239 cpsw_intr_disable(cpsw);
2240 for (ch = 0; ch < cpsw->tx_ch_num; ch++) {
2241 cpdma_chan_stop(cpsw->txv[ch].ch);
2242 cpdma_chan_start(cpsw->txv[ch].ch);
2245 cpsw_intr_enable(cpsw);
2246 netif_trans_update(ndev);
2247 netif_tx_wake_all_queues(ndev);
2250 static int cpsw_ndo_set_mac_address(struct net_device *ndev, void *p)
2252 struct cpsw_priv *priv = netdev_priv(ndev);
2253 struct sockaddr *addr = (struct sockaddr *)p;
2254 struct cpsw_common *cpsw = priv->cpsw;
2259 if (!is_valid_ether_addr(addr->sa_data))
2260 return -EADDRNOTAVAIL;
2262 ret = pm_runtime_get_sync(cpsw->dev);
2264 pm_runtime_put_noidle(cpsw->dev);
2268 if (cpsw->data.dual_emac) {
2269 vid = cpsw->slaves[priv->emac_port].port_vlan;
2273 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, HOST_PORT_NUM,
2275 cpsw_ale_add_ucast(cpsw->ale, addr->sa_data, HOST_PORT_NUM,
2278 memcpy(priv->mac_addr, addr->sa_data, ETH_ALEN);
2279 memcpy(ndev->dev_addr, priv->mac_addr, ETH_ALEN);
2280 for_each_slave(priv, cpsw_set_slave_mac, priv);
2282 pm_runtime_put(cpsw->dev);
2287 #ifdef CONFIG_NET_POLL_CONTROLLER
2288 static void cpsw_ndo_poll_controller(struct net_device *ndev)
2290 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2292 cpsw_intr_disable(cpsw);
2293 cpsw_rx_interrupt(cpsw->irqs_table[0], cpsw);
2294 cpsw_tx_interrupt(cpsw->irqs_table[1], cpsw);
2295 cpsw_intr_enable(cpsw);
2299 static inline int cpsw_add_vlan_ale_entry(struct cpsw_priv *priv,
2303 int unreg_mcast_mask = 0;
2306 struct cpsw_common *cpsw = priv->cpsw;
2308 if (cpsw->data.dual_emac) {
2309 port_mask = (1 << (priv->emac_port + 1)) | ALE_PORT_HOST;
2311 mcast_mask = ALE_PORT_HOST;
2312 if (priv->ndev->flags & IFF_ALLMULTI)
2313 unreg_mcast_mask = mcast_mask;
2315 port_mask = ALE_ALL_PORTS;
2316 mcast_mask = port_mask;
2318 if (priv->ndev->flags & IFF_ALLMULTI)
2319 unreg_mcast_mask = ALE_ALL_PORTS;
2321 unreg_mcast_mask = ALE_PORT_1 | ALE_PORT_2;
2324 ret = cpsw_ale_add_vlan(cpsw->ale, vid, port_mask, 0, port_mask,
2329 ret = cpsw_ale_add_ucast(cpsw->ale, priv->mac_addr,
2330 HOST_PORT_NUM, ALE_VLAN, vid);
2334 ret = cpsw_ale_add_mcast(cpsw->ale, priv->ndev->broadcast,
2335 mcast_mask, ALE_VLAN, vid, 0);
2337 goto clean_vlan_ucast;
2341 cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
2342 HOST_PORT_NUM, ALE_VLAN, vid);
2344 cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2348 static int cpsw_ndo_vlan_rx_add_vid(struct net_device *ndev,
2349 __be16 proto, u16 vid)
2351 struct cpsw_priv *priv = netdev_priv(ndev);
2352 struct cpsw_common *cpsw = priv->cpsw;
2355 if (vid == cpsw->data.default_vlan)
2358 ret = pm_runtime_get_sync(cpsw->dev);
2360 pm_runtime_put_noidle(cpsw->dev);
2364 if (cpsw->data.dual_emac) {
2365 /* In dual EMAC, reserved VLAN id should not be used for
2366 * creating VLAN interfaces as this can break the dual
2367 * EMAC port separation
2371 for (i = 0; i < cpsw->data.slaves; i++) {
2372 if (vid == cpsw->slaves[i].port_vlan) {
2379 dev_info(priv->dev, "Adding vlanid %d to vlan filter\n", vid);
2380 ret = cpsw_add_vlan_ale_entry(priv, vid);
2382 pm_runtime_put(cpsw->dev);
2386 static int cpsw_ndo_vlan_rx_kill_vid(struct net_device *ndev,
2387 __be16 proto, u16 vid)
2389 struct cpsw_priv *priv = netdev_priv(ndev);
2390 struct cpsw_common *cpsw = priv->cpsw;
2393 if (vid == cpsw->data.default_vlan)
2396 ret = pm_runtime_get_sync(cpsw->dev);
2398 pm_runtime_put_noidle(cpsw->dev);
2402 if (cpsw->data.dual_emac) {
2405 for (i = 0; i < cpsw->data.slaves; i++) {
2406 if (vid == cpsw->slaves[i].port_vlan)
2411 dev_info(priv->dev, "removing vlanid %d from vlan filter\n", vid);
2412 ret = cpsw_ale_del_vlan(cpsw->ale, vid, 0);
2413 ret |= cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr,
2414 HOST_PORT_NUM, ALE_VLAN, vid);
2415 ret |= cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast,
2418 pm_runtime_put(cpsw->dev);
2422 static int cpsw_ndo_set_tx_maxrate(struct net_device *ndev, int queue, u32 rate)
2424 struct cpsw_priv *priv = netdev_priv(ndev);
2425 struct cpsw_common *cpsw = priv->cpsw;
2426 struct cpsw_slave *slave;
2431 ch_rate = netdev_get_tx_queue(ndev, queue)->tx_maxrate;
2432 if (ch_rate == rate)
2435 ch_rate = rate * 1000;
2436 min_rate = cpdma_chan_get_min_rate(cpsw->dma);
2437 if ((ch_rate < min_rate && ch_rate)) {
2438 dev_err(priv->dev, "The channel rate cannot be less than %dMbps",
2443 if (rate > cpsw->speed) {
2444 dev_err(priv->dev, "The channel rate cannot be more than 2Gbps");
2448 ret = pm_runtime_get_sync(cpsw->dev);
2450 pm_runtime_put_noidle(cpsw->dev);
2454 ret = cpdma_chan_set_rate(cpsw->txv[queue].ch, ch_rate);
2455 pm_runtime_put(cpsw->dev);
2460 /* update rates for slaves tx queues */
2461 for (i = 0; i < cpsw->data.slaves; i++) {
2462 slave = &cpsw->slaves[i];
2466 netdev_get_tx_queue(slave->ndev, queue)->tx_maxrate = rate;
2469 cpsw_split_res(ndev);
2473 static int cpsw_set_mqprio(struct net_device *ndev, void *type_data)
2475 struct tc_mqprio_qopt_offload *mqprio = type_data;
2476 struct cpsw_priv *priv = netdev_priv(ndev);
2477 struct cpsw_common *cpsw = priv->cpsw;
2478 int fifo, num_tc, count, offset;
2479 struct cpsw_slave *slave;
2480 u32 tx_prio_map = 0;
2483 num_tc = mqprio->qopt.num_tc;
2484 if (num_tc > CPSW_TC_NUM)
2487 if (mqprio->mode != TC_MQPRIO_MODE_DCB)
2490 ret = pm_runtime_get_sync(cpsw->dev);
2492 pm_runtime_put_noidle(cpsw->dev);
2497 for (i = 0; i < 8; i++) {
2498 tc = mqprio->qopt.prio_tc_map[i];
2499 fifo = cpsw_tc_to_fifo(tc, num_tc);
2500 tx_prio_map |= fifo << (4 * i);
2503 netdev_set_num_tc(ndev, num_tc);
2504 for (i = 0; i < num_tc; i++) {
2505 count = mqprio->qopt.count[i];
2506 offset = mqprio->qopt.offset[i];
2507 netdev_set_tc_queue(ndev, i, count, offset);
2511 if (!mqprio->qopt.hw) {
2512 /* restore default configuration */
2513 netdev_reset_tc(ndev);
2514 tx_prio_map = TX_PRIORITY_MAPPING;
2517 priv->mqprio_hw = mqprio->qopt.hw;
2519 offset = cpsw->version == CPSW_VERSION_1 ?
2520 CPSW1_TX_PRI_MAP : CPSW2_TX_PRI_MAP;
2522 slave = &cpsw->slaves[cpsw_slave_index(cpsw, priv)];
2523 slave_write(slave, tx_prio_map, offset);
2525 pm_runtime_put_sync(cpsw->dev);
2530 static int cpsw_ndo_setup_tc(struct net_device *ndev, enum tc_setup_type type,
2534 case TC_SETUP_QDISC_CBS:
2535 return cpsw_set_cbs(ndev, type_data);
2537 case TC_SETUP_QDISC_MQPRIO:
2538 return cpsw_set_mqprio(ndev, type_data);
2545 static const struct net_device_ops cpsw_netdev_ops = {
2546 .ndo_open = cpsw_ndo_open,
2547 .ndo_stop = cpsw_ndo_stop,
2548 .ndo_start_xmit = cpsw_ndo_start_xmit,
2549 .ndo_set_mac_address = cpsw_ndo_set_mac_address,
2550 .ndo_do_ioctl = cpsw_ndo_ioctl,
2551 .ndo_validate_addr = eth_validate_addr,
2552 .ndo_tx_timeout = cpsw_ndo_tx_timeout,
2553 .ndo_set_rx_mode = cpsw_ndo_set_rx_mode,
2554 .ndo_set_tx_maxrate = cpsw_ndo_set_tx_maxrate,
2555 #ifdef CONFIG_NET_POLL_CONTROLLER
2556 .ndo_poll_controller = cpsw_ndo_poll_controller,
2558 .ndo_vlan_rx_add_vid = cpsw_ndo_vlan_rx_add_vid,
2559 .ndo_vlan_rx_kill_vid = cpsw_ndo_vlan_rx_kill_vid,
2560 .ndo_setup_tc = cpsw_ndo_setup_tc,
2563 static int cpsw_get_regs_len(struct net_device *ndev)
2565 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2567 return cpsw->data.ale_entries * ALE_ENTRY_WORDS * sizeof(u32);
2570 static void cpsw_get_regs(struct net_device *ndev,
2571 struct ethtool_regs *regs, void *p)
2574 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2576 /* update CPSW IP version */
2577 regs->version = cpsw->version;
2579 cpsw_ale_dump(cpsw->ale, reg);
2582 static void cpsw_get_drvinfo(struct net_device *ndev,
2583 struct ethtool_drvinfo *info)
2585 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2586 struct platform_device *pdev = to_platform_device(cpsw->dev);
2588 strlcpy(info->driver, "cpsw", sizeof(info->driver));
2589 strlcpy(info->version, "1.0", sizeof(info->version));
2590 strlcpy(info->bus_info, pdev->name, sizeof(info->bus_info));
2593 static u32 cpsw_get_msglevel(struct net_device *ndev)
2595 struct cpsw_priv *priv = netdev_priv(ndev);
2596 return priv->msg_enable;
2599 static void cpsw_set_msglevel(struct net_device *ndev, u32 value)
2601 struct cpsw_priv *priv = netdev_priv(ndev);
2602 priv->msg_enable = value;
2605 #if IS_ENABLED(CONFIG_TI_CPTS)
2606 static int cpsw_get_ts_info(struct net_device *ndev,
2607 struct ethtool_ts_info *info)
2609 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2611 info->so_timestamping =
2612 SOF_TIMESTAMPING_TX_HARDWARE |
2613 SOF_TIMESTAMPING_TX_SOFTWARE |
2614 SOF_TIMESTAMPING_RX_HARDWARE |
2615 SOF_TIMESTAMPING_RX_SOFTWARE |
2616 SOF_TIMESTAMPING_SOFTWARE |
2617 SOF_TIMESTAMPING_RAW_HARDWARE;
2618 info->phc_index = cpsw->cpts->phc_index;
2620 (1 << HWTSTAMP_TX_OFF) |
2621 (1 << HWTSTAMP_TX_ON);
2623 (1 << HWTSTAMP_FILTER_NONE) |
2624 (1 << HWTSTAMP_FILTER_PTP_V1_L4_EVENT) |
2625 (1 << HWTSTAMP_FILTER_PTP_V2_EVENT);
2629 static int cpsw_get_ts_info(struct net_device *ndev,
2630 struct ethtool_ts_info *info)
2632 info->so_timestamping =
2633 SOF_TIMESTAMPING_TX_SOFTWARE |
2634 SOF_TIMESTAMPING_RX_SOFTWARE |
2635 SOF_TIMESTAMPING_SOFTWARE;
2636 info->phc_index = -1;
2638 info->rx_filters = 0;
2643 static int cpsw_get_link_ksettings(struct net_device *ndev,
2644 struct ethtool_link_ksettings *ecmd)
2646 struct cpsw_priv *priv = netdev_priv(ndev);
2647 struct cpsw_common *cpsw = priv->cpsw;
2648 int slave_no = cpsw_slave_index(cpsw, priv);
2650 if (!cpsw->slaves[slave_no].phy)
2653 phy_ethtool_ksettings_get(cpsw->slaves[slave_no].phy, ecmd);
2657 static int cpsw_set_link_ksettings(struct net_device *ndev,
2658 const struct ethtool_link_ksettings *ecmd)
2660 struct cpsw_priv *priv = netdev_priv(ndev);
2661 struct cpsw_common *cpsw = priv->cpsw;
2662 int slave_no = cpsw_slave_index(cpsw, priv);
2664 if (cpsw->slaves[slave_no].phy)
2665 return phy_ethtool_ksettings_set(cpsw->slaves[slave_no].phy,
2671 static void cpsw_get_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2673 struct cpsw_priv *priv = netdev_priv(ndev);
2674 struct cpsw_common *cpsw = priv->cpsw;
2675 int slave_no = cpsw_slave_index(cpsw, priv);
2680 if (cpsw->slaves[slave_no].phy)
2681 phy_ethtool_get_wol(cpsw->slaves[slave_no].phy, wol);
2684 static int cpsw_set_wol(struct net_device *ndev, struct ethtool_wolinfo *wol)
2686 struct cpsw_priv *priv = netdev_priv(ndev);
2687 struct cpsw_common *cpsw = priv->cpsw;
2688 int slave_no = cpsw_slave_index(cpsw, priv);
2690 if (cpsw->slaves[slave_no].phy)
2691 return phy_ethtool_set_wol(cpsw->slaves[slave_no].phy, wol);
2696 static void cpsw_get_pauseparam(struct net_device *ndev,
2697 struct ethtool_pauseparam *pause)
2699 struct cpsw_priv *priv = netdev_priv(ndev);
2701 pause->autoneg = AUTONEG_DISABLE;
2702 pause->rx_pause = priv->rx_pause ? true : false;
2703 pause->tx_pause = priv->tx_pause ? true : false;
2706 static int cpsw_set_pauseparam(struct net_device *ndev,
2707 struct ethtool_pauseparam *pause)
2709 struct cpsw_priv *priv = netdev_priv(ndev);
2712 priv->rx_pause = pause->rx_pause ? true : false;
2713 priv->tx_pause = pause->tx_pause ? true : false;
2715 for_each_slave(priv, _cpsw_adjust_link, priv, &link);
2719 static int cpsw_ethtool_op_begin(struct net_device *ndev)
2721 struct cpsw_priv *priv = netdev_priv(ndev);
2722 struct cpsw_common *cpsw = priv->cpsw;
2725 ret = pm_runtime_get_sync(cpsw->dev);
2727 cpsw_err(priv, drv, "ethtool begin failed %d\n", ret);
2728 pm_runtime_put_noidle(cpsw->dev);
2734 static void cpsw_ethtool_op_complete(struct net_device *ndev)
2736 struct cpsw_priv *priv = netdev_priv(ndev);
2739 ret = pm_runtime_put(priv->cpsw->dev);
2741 cpsw_err(priv, drv, "ethtool complete failed %d\n", ret);
2744 static void cpsw_get_channels(struct net_device *ndev,
2745 struct ethtool_channels *ch)
2747 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2749 ch->max_rx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2750 ch->max_tx = cpsw->quirk_irq ? 1 : CPSW_MAX_QUEUES;
2751 ch->max_combined = 0;
2753 ch->other_count = 0;
2754 ch->rx_count = cpsw->rx_ch_num;
2755 ch->tx_count = cpsw->tx_ch_num;
2756 ch->combined_count = 0;
2759 static int cpsw_check_ch_settings(struct cpsw_common *cpsw,
2760 struct ethtool_channels *ch)
2762 if (cpsw->quirk_irq) {
2763 dev_err(cpsw->dev, "Maximum one tx/rx queue is allowed");
2767 if (ch->combined_count)
2770 /* verify we have at least one channel in each direction */
2771 if (!ch->rx_count || !ch->tx_count)
2774 if (ch->rx_count > cpsw->data.channels ||
2775 ch->tx_count > cpsw->data.channels)
2781 static int cpsw_update_channels_res(struct cpsw_priv *priv, int ch_num, int rx)
2783 struct cpsw_common *cpsw = priv->cpsw;
2784 void (*handler)(void *, int, int);
2785 struct netdev_queue *queue;
2786 struct cpsw_vector *vec;
2790 ch = &cpsw->rx_ch_num;
2792 handler = cpsw_rx_handler;
2794 ch = &cpsw->tx_ch_num;
2796 handler = cpsw_tx_handler;
2799 while (*ch < ch_num) {
2800 vch = rx ? *ch : 7 - *ch;
2801 vec[*ch].ch = cpdma_chan_create(cpsw->dma, vch, handler, rx);
2802 queue = netdev_get_tx_queue(priv->ndev, *ch);
2803 queue->tx_maxrate = 0;
2805 if (IS_ERR(vec[*ch].ch))
2806 return PTR_ERR(vec[*ch].ch);
2811 cpsw_info(priv, ifup, "created new %d %s channel\n", *ch,
2812 (rx ? "rx" : "tx"));
2816 while (*ch > ch_num) {
2819 ret = cpdma_chan_destroy(vec[*ch].ch);
2823 cpsw_info(priv, ifup, "destroyed %d %s channel\n", *ch,
2824 (rx ? "rx" : "tx"));
2830 static int cpsw_update_channels(struct cpsw_priv *priv,
2831 struct ethtool_channels *ch)
2835 ret = cpsw_update_channels_res(priv, ch->rx_count, 1);
2839 ret = cpsw_update_channels_res(priv, ch->tx_count, 0);
2846 static void cpsw_suspend_data_pass(struct net_device *ndev)
2848 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
2849 struct cpsw_slave *slave;
2852 /* Disable NAPI scheduling */
2853 cpsw_intr_disable(cpsw);
2855 /* Stop all transmit queues for every network device.
2856 * Disable re-using rx descriptors with dormant_on.
2858 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2859 if (!(slave->ndev && netif_running(slave->ndev)))
2862 netif_tx_stop_all_queues(slave->ndev);
2863 netif_dormant_on(slave->ndev);
2866 /* Handle rest of tx packets and stop cpdma channels */
2867 cpdma_ctlr_stop(cpsw->dma);
2870 static int cpsw_resume_data_pass(struct net_device *ndev)
2872 struct cpsw_priv *priv = netdev_priv(ndev);
2873 struct cpsw_common *cpsw = priv->cpsw;
2874 struct cpsw_slave *slave;
2877 /* Allow rx packets handling */
2878 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2879 if (slave->ndev && netif_running(slave->ndev))
2880 netif_dormant_off(slave->ndev);
2882 /* After this receive is started */
2883 if (cpsw->usage_count) {
2884 ret = cpsw_fill_rx_channels(priv);
2888 cpdma_ctlr_start(cpsw->dma);
2889 cpsw_intr_enable(cpsw);
2892 /* Resume transmit for every affected interface */
2893 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++)
2894 if (slave->ndev && netif_running(slave->ndev))
2895 netif_tx_start_all_queues(slave->ndev);
2900 static int cpsw_set_channels(struct net_device *ndev,
2901 struct ethtool_channels *chs)
2903 struct cpsw_priv *priv = netdev_priv(ndev);
2904 struct cpsw_common *cpsw = priv->cpsw;
2905 struct cpsw_slave *slave;
2908 ret = cpsw_check_ch_settings(cpsw, chs);
2912 cpsw_suspend_data_pass(ndev);
2913 ret = cpsw_update_channels(priv, chs);
2917 for (i = cpsw->data.slaves, slave = cpsw->slaves; i; i--, slave++) {
2918 if (!(slave->ndev && netif_running(slave->ndev)))
2921 /* Inform stack about new count of queues */
2922 ret = netif_set_real_num_tx_queues(slave->ndev,
2925 dev_err(priv->dev, "cannot set real number of tx queues\n");
2929 ret = netif_set_real_num_rx_queues(slave->ndev,
2932 dev_err(priv->dev, "cannot set real number of rx queues\n");
2937 if (cpsw->usage_count)
2938 cpsw_split_res(ndev);
2940 ret = cpsw_resume_data_pass(ndev);
2944 dev_err(priv->dev, "cannot update channels number, closing device\n");
2949 static int cpsw_get_eee(struct net_device *ndev, struct ethtool_eee *edata)
2951 struct cpsw_priv *priv = netdev_priv(ndev);
2952 struct cpsw_common *cpsw = priv->cpsw;
2953 int slave_no = cpsw_slave_index(cpsw, priv);
2955 if (cpsw->slaves[slave_no].phy)
2956 return phy_ethtool_get_eee(cpsw->slaves[slave_no].phy, edata);
2961 static int cpsw_set_eee(struct net_device *ndev, struct ethtool_eee *edata)
2963 struct cpsw_priv *priv = netdev_priv(ndev);
2964 struct cpsw_common *cpsw = priv->cpsw;
2965 int slave_no = cpsw_slave_index(cpsw, priv);
2967 if (cpsw->slaves[slave_no].phy)
2968 return phy_ethtool_set_eee(cpsw->slaves[slave_no].phy, edata);
2973 static int cpsw_nway_reset(struct net_device *ndev)
2975 struct cpsw_priv *priv = netdev_priv(ndev);
2976 struct cpsw_common *cpsw = priv->cpsw;
2977 int slave_no = cpsw_slave_index(cpsw, priv);
2979 if (cpsw->slaves[slave_no].phy)
2980 return genphy_restart_aneg(cpsw->slaves[slave_no].phy);
2985 static void cpsw_get_ringparam(struct net_device *ndev,
2986 struct ethtool_ringparam *ering)
2988 struct cpsw_priv *priv = netdev_priv(ndev);
2989 struct cpsw_common *cpsw = priv->cpsw;
2992 ering->tx_max_pending = 0;
2993 ering->tx_pending = cpdma_get_num_tx_descs(cpsw->dma);
2994 ering->rx_max_pending = descs_pool_size - CPSW_MAX_QUEUES;
2995 ering->rx_pending = cpdma_get_num_rx_descs(cpsw->dma);
2998 static int cpsw_set_ringparam(struct net_device *ndev,
2999 struct ethtool_ringparam *ering)
3001 struct cpsw_priv *priv = netdev_priv(ndev);
3002 struct cpsw_common *cpsw = priv->cpsw;
3005 /* ignore ering->tx_pending - only rx_pending adjustment is supported */
3007 if (ering->rx_mini_pending || ering->rx_jumbo_pending ||
3008 ering->rx_pending < CPSW_MAX_QUEUES ||
3009 ering->rx_pending > (descs_pool_size - CPSW_MAX_QUEUES))
3012 if (ering->rx_pending == cpdma_get_num_rx_descs(cpsw->dma))
3015 cpsw_suspend_data_pass(ndev);
3017 cpdma_set_num_rx_descs(cpsw->dma, ering->rx_pending);
3019 if (cpsw->usage_count)
3020 cpdma_chan_split_pool(cpsw->dma);
3022 ret = cpsw_resume_data_pass(ndev);
3026 dev_err(&ndev->dev, "cannot set ring params, closing device\n");
3031 static const struct ethtool_ops cpsw_ethtool_ops = {
3032 .get_drvinfo = cpsw_get_drvinfo,
3033 .get_msglevel = cpsw_get_msglevel,
3034 .set_msglevel = cpsw_set_msglevel,
3035 .get_link = ethtool_op_get_link,
3036 .get_ts_info = cpsw_get_ts_info,
3037 .get_coalesce = cpsw_get_coalesce,
3038 .set_coalesce = cpsw_set_coalesce,
3039 .get_sset_count = cpsw_get_sset_count,
3040 .get_strings = cpsw_get_strings,
3041 .get_ethtool_stats = cpsw_get_ethtool_stats,
3042 .get_pauseparam = cpsw_get_pauseparam,
3043 .set_pauseparam = cpsw_set_pauseparam,
3044 .get_wol = cpsw_get_wol,
3045 .set_wol = cpsw_set_wol,
3046 .get_regs_len = cpsw_get_regs_len,
3047 .get_regs = cpsw_get_regs,
3048 .begin = cpsw_ethtool_op_begin,
3049 .complete = cpsw_ethtool_op_complete,
3050 .get_channels = cpsw_get_channels,
3051 .set_channels = cpsw_set_channels,
3052 .get_link_ksettings = cpsw_get_link_ksettings,
3053 .set_link_ksettings = cpsw_set_link_ksettings,
3054 .get_eee = cpsw_get_eee,
3055 .set_eee = cpsw_set_eee,
3056 .nway_reset = cpsw_nway_reset,
3057 .get_ringparam = cpsw_get_ringparam,
3058 .set_ringparam = cpsw_set_ringparam,
3061 static void cpsw_slave_init(struct cpsw_slave *slave, struct cpsw_common *cpsw,
3062 u32 slave_reg_ofs, u32 sliver_reg_ofs)
3064 void __iomem *regs = cpsw->regs;
3065 int slave_num = slave->slave_num;
3066 struct cpsw_slave_data *data = cpsw->data.slave_data + slave_num;
3069 slave->regs = regs + slave_reg_ofs;
3070 slave->sliver = regs + sliver_reg_ofs;
3071 slave->port_vlan = data->dual_emac_res_vlan;
3074 static int cpsw_probe_dt(struct cpsw_platform_data *data,
3075 struct platform_device *pdev)
3077 struct device_node *node = pdev->dev.of_node;
3078 struct device_node *slave_node;
3085 if (of_property_read_u32(node, "slaves", &prop)) {
3086 dev_err(&pdev->dev, "Missing slaves property in the DT.\n");
3089 data->slaves = prop;
3091 if (of_property_read_u32(node, "active_slave", &prop)) {
3092 dev_err(&pdev->dev, "Missing active_slave property in the DT.\n");
3095 data->active_slave = prop;
3097 data->slave_data = devm_kcalloc(&pdev->dev,
3099 sizeof(struct cpsw_slave_data),
3101 if (!data->slave_data)
3104 if (of_property_read_u32(node, "cpdma_channels", &prop)) {
3105 dev_err(&pdev->dev, "Missing cpdma_channels property in the DT.\n");
3108 data->channels = prop;
3110 if (of_property_read_u32(node, "ale_entries", &prop)) {
3111 dev_err(&pdev->dev, "Missing ale_entries property in the DT.\n");
3114 data->ale_entries = prop;
3116 if (of_property_read_u32(node, "bd_ram_size", &prop)) {
3117 dev_err(&pdev->dev, "Missing bd_ram_size property in the DT.\n");
3120 data->bd_ram_size = prop;
3122 if (of_property_read_u32(node, "mac_control", &prop)) {
3123 dev_err(&pdev->dev, "Missing mac_control property in the DT.\n");
3126 data->mac_control = prop;
3128 if (of_property_read_bool(node, "dual_emac"))
3129 data->dual_emac = 1;
3132 * Populate all the child nodes here...
3134 ret = of_platform_populate(node, NULL, NULL, &pdev->dev);
3135 /* We do not want to force this, as in some cases may not have child */
3137 dev_warn(&pdev->dev, "Doesn't have any child node\n");
3139 for_each_available_child_of_node(node, slave_node) {
3140 struct cpsw_slave_data *slave_data = data->slave_data + i;
3141 const void *mac_addr = NULL;
3145 /* This is no slave child node, continue */
3146 if (strcmp(slave_node->name, "slave"))
3149 slave_data->phy_node = of_parse_phandle(slave_node,
3151 parp = of_get_property(slave_node, "phy_id", &lenp);
3152 if (slave_data->phy_node) {
3154 "slave[%d] using phy-handle=\"%pOF\"\n",
3155 i, slave_data->phy_node);
3156 } else if (of_phy_is_fixed_link(slave_node)) {
3157 /* In the case of a fixed PHY, the DT node associated
3158 * to the PHY is the Ethernet MAC DT node.
3160 ret = of_phy_register_fixed_link(slave_node);
3162 if (ret != -EPROBE_DEFER)
3163 dev_err(&pdev->dev, "failed to register fixed-link phy: %d\n", ret);
3166 slave_data->phy_node = of_node_get(slave_node);
3169 struct device_node *mdio_node;
3170 struct platform_device *mdio;
3172 if (lenp != (sizeof(__be32) * 2)) {
3173 dev_err(&pdev->dev, "Invalid slave[%d] phy_id property\n", i);
3176 mdio_node = of_find_node_by_phandle(be32_to_cpup(parp));
3177 phyid = be32_to_cpup(parp+1);
3178 mdio = of_find_device_by_node(mdio_node);
3179 of_node_put(mdio_node);
3181 dev_err(&pdev->dev, "Missing mdio platform device\n");
3184 snprintf(slave_data->phy_id, sizeof(slave_data->phy_id),
3185 PHY_ID_FMT, mdio->name, phyid);
3186 put_device(&mdio->dev);
3189 "No slave[%d] phy_id, phy-handle, or fixed-link property\n",
3193 slave_data->phy_if = of_get_phy_mode(slave_node);
3194 if (slave_data->phy_if < 0) {
3195 dev_err(&pdev->dev, "Missing or malformed slave[%d] phy-mode property\n",
3197 return slave_data->phy_if;
3201 mac_addr = of_get_mac_address(slave_node);
3203 memcpy(slave_data->mac_addr, mac_addr, ETH_ALEN);
3205 ret = ti_cm_get_macid(&pdev->dev, i,
3206 slave_data->mac_addr);
3210 if (data->dual_emac) {
3211 if (of_property_read_u32(slave_node, "dual_emac_res_vlan",
3213 dev_err(&pdev->dev, "Missing dual_emac_res_vlan in DT.\n");
3214 slave_data->dual_emac_res_vlan = i+1;
3215 dev_err(&pdev->dev, "Using %d as Reserved VLAN for %d slave\n",
3216 slave_data->dual_emac_res_vlan, i);
3218 slave_data->dual_emac_res_vlan = prop;
3223 if (i == data->slaves)
3230 static void cpsw_remove_dt(struct platform_device *pdev)
3232 struct net_device *ndev = platform_get_drvdata(pdev);
3233 struct cpsw_common *cpsw = ndev_to_cpsw(ndev);
3234 struct cpsw_platform_data *data = &cpsw->data;
3235 struct device_node *node = pdev->dev.of_node;
3236 struct device_node *slave_node;
3239 for_each_available_child_of_node(node, slave_node) {
3240 struct cpsw_slave_data *slave_data = &data->slave_data[i];
3242 if (strcmp(slave_node->name, "slave"))
3245 if (of_phy_is_fixed_link(slave_node))
3246 of_phy_deregister_fixed_link(slave_node);
3248 of_node_put(slave_data->phy_node);
3251 if (i == data->slaves)
3255 of_platform_depopulate(&pdev->dev);
3258 static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
3260 struct cpsw_common *cpsw = priv->cpsw;
3261 struct cpsw_platform_data *data = &cpsw->data;
3262 struct net_device *ndev;
3263 struct cpsw_priv *priv_sl2;
3266 ndev = alloc_etherdev_mq(sizeof(struct cpsw_priv), CPSW_MAX_QUEUES);
3268 dev_err(cpsw->dev, "cpsw: error allocating net_device\n");
3272 priv_sl2 = netdev_priv(ndev);
3273 priv_sl2->cpsw = cpsw;
3274 priv_sl2->ndev = ndev;
3275 priv_sl2->dev = &ndev->dev;
3276 priv_sl2->msg_enable = netif_msg_init(debug_level, CPSW_DEBUG);
3278 if (is_valid_ether_addr(data->slave_data[1].mac_addr)) {
3279 memcpy(priv_sl2->mac_addr, data->slave_data[1].mac_addr,
3281 dev_info(cpsw->dev, "cpsw: Detected MACID = %pM\n",
3282 priv_sl2->mac_addr);
3284 eth_random_addr(priv_sl2->mac_addr);
3285 dev_info(cpsw->dev, "cpsw: Random MACID = %pM\n",
3286 priv_sl2->mac_addr);
3288 memcpy(ndev->dev_addr, priv_sl2->mac_addr, ETH_ALEN);
3290 priv_sl2->emac_port = 1;
3291 cpsw->slaves[1].ndev = ndev;
3292 ndev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_VLAN_CTAG_RX;
3294 ndev->netdev_ops = &cpsw_netdev_ops;
3295 ndev->ethtool_ops = &cpsw_ethtool_ops;
3297 /* register the network device */
3298 SET_NETDEV_DEV(ndev, cpsw->dev);
3299 ret = register_netdev(ndev);
3301 dev_err(cpsw->dev, "cpsw: error registering net device\n");