1 // SPDX-License-Identifier: GPL-2.0
3 * Texas Instruments Ethernet Switch Driver
5 * Copyright (C) 2019 Texas Instruments
9 #include <linux/bpf_trace.h>
10 #include <linux/if_ether.h>
11 #include <linux/if_vlan.h>
12 #include <linux/kmemleak.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/net_tstamp.h>
17 #include <linux/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/skbuff.h>
21 #include <net/page_pool.h>
22 #include <net/pkt_cls.h>
27 #include "cpsw_priv.h"
29 #include "davinci_cpdma.h"
31 int (*cpsw_slave_index
)(struct cpsw_common
*cpsw
, struct cpsw_priv
*priv
);
33 void cpsw_intr_enable(struct cpsw_common
*cpsw
)
35 writel_relaxed(0xFF, &cpsw
->wr_regs
->tx_en
);
36 writel_relaxed(0xFF, &cpsw
->wr_regs
->rx_en
);
38 cpdma_ctlr_int_ctrl(cpsw
->dma
, true);
41 void cpsw_intr_disable(struct cpsw_common
*cpsw
)
43 writel_relaxed(0, &cpsw
->wr_regs
->tx_en
);
44 writel_relaxed(0, &cpsw
->wr_regs
->rx_en
);
46 cpdma_ctlr_int_ctrl(cpsw
->dma
, false);
49 void cpsw_tx_handler(void *token
, int len
, int status
)
51 struct cpsw_meta_xdp
*xmeta
;
52 struct xdp_frame
*xdpf
;
53 struct net_device
*ndev
;
54 struct netdev_queue
*txq
;
58 if (cpsw_is_xdpf_handle(token
)) {
59 xdpf
= cpsw_handle_to_xdpf(token
);
60 xmeta
= (void *)xdpf
+ CPSW_XMETA_OFFSET
;
63 xdp_return_frame(xdpf
);
67 ch
= skb_get_queue_mapping(skb
);
68 cpts_tx_timestamp(ndev_to_cpsw(ndev
)->cpts
, skb
);
69 dev_kfree_skb_any(skb
);
72 /* Check whether the queue is stopped due to stalled tx dma, if the
73 * queue is stopped then start the queue as we have free desc for tx
75 txq
= netdev_get_tx_queue(ndev
, ch
);
76 if (unlikely(netif_tx_queue_stopped(txq
)))
77 netif_tx_wake_queue(txq
);
79 ndev
->stats
.tx_packets
++;
80 ndev
->stats
.tx_bytes
+= len
;
83 irqreturn_t
cpsw_tx_interrupt(int irq
, void *dev_id
)
85 struct cpsw_common
*cpsw
= dev_id
;
87 writel(0, &cpsw
->wr_regs
->tx_en
);
88 cpdma_ctlr_eoi(cpsw
->dma
, CPDMA_EOI_TX
);
90 if (cpsw
->quirk_irq
) {
91 disable_irq_nosync(cpsw
->irqs_table
[1]);
92 cpsw
->tx_irq_disabled
= true;
95 napi_schedule(&cpsw
->napi_tx
);
99 irqreturn_t
cpsw_rx_interrupt(int irq
, void *dev_id
)
101 struct cpsw_common
*cpsw
= dev_id
;
103 writel(0, &cpsw
->wr_regs
->rx_en
);
104 cpdma_ctlr_eoi(cpsw
->dma
, CPDMA_EOI_RX
);
106 if (cpsw
->quirk_irq
) {
107 disable_irq_nosync(cpsw
->irqs_table
[0]);
108 cpsw
->rx_irq_disabled
= true;
111 napi_schedule(&cpsw
->napi_rx
);
115 int cpsw_tx_mq_poll(struct napi_struct
*napi_tx
, int budget
)
117 struct cpsw_common
*cpsw
= napi_to_cpsw(napi_tx
);
118 int num_tx
, cur_budget
, ch
;
120 struct cpsw_vector
*txv
;
122 /* process every unprocessed channel */
123 ch_map
= cpdma_ctrl_txchs_state(cpsw
->dma
);
124 for (ch
= 0, num_tx
= 0; ch_map
& 0xff; ch_map
<<= 1, ch
++) {
125 if (!(ch_map
& 0x80))
128 txv
= &cpsw
->txv
[ch
];
129 if (unlikely(txv
->budget
> budget
- num_tx
))
130 cur_budget
= budget
- num_tx
;
132 cur_budget
= txv
->budget
;
134 num_tx
+= cpdma_chan_process(txv
->ch
, cur_budget
);
135 if (num_tx
>= budget
)
139 if (num_tx
< budget
) {
140 napi_complete(napi_tx
);
141 writel(0xff, &cpsw
->wr_regs
->tx_en
);
147 int cpsw_tx_poll(struct napi_struct
*napi_tx
, int budget
)
149 struct cpsw_common
*cpsw
= napi_to_cpsw(napi_tx
);
152 num_tx
= cpdma_chan_process(cpsw
->txv
[0].ch
, budget
);
153 if (num_tx
< budget
) {
154 napi_complete(napi_tx
);
155 writel(0xff, &cpsw
->wr_regs
->tx_en
);
156 if (cpsw
->tx_irq_disabled
) {
157 cpsw
->tx_irq_disabled
= false;
158 enable_irq(cpsw
->irqs_table
[1]);
165 int cpsw_rx_mq_poll(struct napi_struct
*napi_rx
, int budget
)
167 struct cpsw_common
*cpsw
= napi_to_cpsw(napi_rx
);
168 int num_rx
, cur_budget
, ch
;
170 struct cpsw_vector
*rxv
;
172 /* process every unprocessed channel */
173 ch_map
= cpdma_ctrl_rxchs_state(cpsw
->dma
);
174 for (ch
= 0, num_rx
= 0; ch_map
; ch_map
>>= 1, ch
++) {
175 if (!(ch_map
& 0x01))
178 rxv
= &cpsw
->rxv
[ch
];
179 if (unlikely(rxv
->budget
> budget
- num_rx
))
180 cur_budget
= budget
- num_rx
;
182 cur_budget
= rxv
->budget
;
184 num_rx
+= cpdma_chan_process(rxv
->ch
, cur_budget
);
185 if (num_rx
>= budget
)
189 if (num_rx
< budget
) {
190 napi_complete_done(napi_rx
, num_rx
);
191 writel(0xff, &cpsw
->wr_regs
->rx_en
);
197 int cpsw_rx_poll(struct napi_struct
*napi_rx
, int budget
)
199 struct cpsw_common
*cpsw
= napi_to_cpsw(napi_rx
);
202 num_rx
= cpdma_chan_process(cpsw
->rxv
[0].ch
, budget
);
203 if (num_rx
< budget
) {
204 napi_complete_done(napi_rx
, num_rx
);
205 writel(0xff, &cpsw
->wr_regs
->rx_en
);
206 if (cpsw
->rx_irq_disabled
) {
207 cpsw
->rx_irq_disabled
= false;
208 enable_irq(cpsw
->irqs_table
[0]);
215 void cpsw_rx_vlan_encap(struct sk_buff
*skb
)
217 struct cpsw_priv
*priv
= netdev_priv(skb
->dev
);
218 u32 rx_vlan_encap_hdr
= *((u32
*)skb
->data
);
219 struct cpsw_common
*cpsw
= priv
->cpsw
;
220 u16 vtag
, vid
, prio
, pkt_type
;
222 /* Remove VLAN header encapsulation word */
223 skb_pull(skb
, CPSW_RX_VLAN_ENCAP_HDR_SIZE
);
225 pkt_type
= (rx_vlan_encap_hdr
>>
226 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_SHIFT
) &
227 CPSW_RX_VLAN_ENCAP_HDR_PKT_TYPE_MSK
;
228 /* Ignore unknown & Priority-tagged packets*/
229 if (pkt_type
== CPSW_RX_VLAN_ENCAP_HDR_PKT_RESERV
||
230 pkt_type
== CPSW_RX_VLAN_ENCAP_HDR_PKT_PRIO_TAG
)
233 vid
= (rx_vlan_encap_hdr
>>
234 CPSW_RX_VLAN_ENCAP_HDR_VID_SHIFT
) &
236 /* Ignore vid 0 and pass packet as is */
240 /* Untag P0 packets if set for vlan */
241 if (!cpsw_ale_get_vlan_p0_untag(cpsw
->ale
, vid
)) {
242 prio
= (rx_vlan_encap_hdr
>>
243 CPSW_RX_VLAN_ENCAP_HDR_PRIO_SHIFT
) &
244 CPSW_RX_VLAN_ENCAP_HDR_PRIO_MSK
;
246 vtag
= (prio
<< VLAN_PRIO_SHIFT
) | vid
;
247 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
), vtag
);
250 /* strip vlan tag for VLAN-tagged packet */
251 if (pkt_type
== CPSW_RX_VLAN_ENCAP_HDR_PKT_VLAN_TAG
) {
252 memmove(skb
->data
+ VLAN_HLEN
, skb
->data
, 2 * ETH_ALEN
);
253 skb_pull(skb
, VLAN_HLEN
);
257 void cpsw_set_slave_mac(struct cpsw_slave
*slave
, struct cpsw_priv
*priv
)
259 slave_write(slave
, mac_hi(priv
->mac_addr
), SA_HI
);
260 slave_write(slave
, mac_lo(priv
->mac_addr
), SA_LO
);
263 void soft_reset(const char *module
, void __iomem
*reg
)
265 unsigned long timeout
= jiffies
+ HZ
;
267 writel_relaxed(1, reg
);
270 } while ((readl_relaxed(reg
) & 1) && time_after(timeout
, jiffies
));
272 WARN(readl_relaxed(reg
) & 1, "failed to soft-reset %s\n", module
);
275 void cpsw_ndo_tx_timeout(struct net_device
*ndev
, unsigned int txqueue
)
277 struct cpsw_priv
*priv
= netdev_priv(ndev
);
278 struct cpsw_common
*cpsw
= priv
->cpsw
;
281 cpsw_err(priv
, tx_err
, "transmit timeout, restarting dma\n");
282 ndev
->stats
.tx_errors
++;
283 cpsw_intr_disable(cpsw
);
284 for (ch
= 0; ch
< cpsw
->tx_ch_num
; ch
++) {
285 cpdma_chan_stop(cpsw
->txv
[ch
].ch
);
286 cpdma_chan_start(cpsw
->txv
[ch
].ch
);
289 cpsw_intr_enable(cpsw
);
290 netif_trans_update(ndev
);
291 netif_tx_wake_all_queues(ndev
);
294 static int cpsw_get_common_speed(struct cpsw_common
*cpsw
)
298 for (i
= 0, speed
= 0; i
< cpsw
->data
.slaves
; i
++)
299 if (cpsw
->slaves
[i
].phy
&& cpsw
->slaves
[i
].phy
->link
)
300 speed
+= cpsw
->slaves
[i
].phy
->speed
;
305 int cpsw_need_resplit(struct cpsw_common
*cpsw
)
310 /* re-split resources only in case speed was changed */
311 speed
= cpsw_get_common_speed(cpsw
);
312 if (speed
== cpsw
->speed
|| !speed
)
317 for (i
= 0, rlim_ch_num
= 0; i
< cpsw
->tx_ch_num
; i
++) {
318 ch_rate
= cpdma_chan_get_rate(cpsw
->txv
[i
].ch
);
325 /* cases not dependent on speed */
326 if (!rlim_ch_num
|| rlim_ch_num
== cpsw
->tx_ch_num
)
332 void cpsw_split_res(struct cpsw_common
*cpsw
)
334 u32 consumed_rate
= 0, bigest_rate
= 0;
335 struct cpsw_vector
*txv
= cpsw
->txv
;
336 int i
, ch_weight
, rlim_ch_num
= 0;
337 int budget
, bigest_rate_ch
= 0;
338 u32 ch_rate
, max_rate
;
341 for (i
= 0; i
< cpsw
->tx_ch_num
; i
++) {
342 ch_rate
= cpdma_chan_get_rate(txv
[i
].ch
);
347 consumed_rate
+= ch_rate
;
350 if (cpsw
->tx_ch_num
== rlim_ch_num
) {
351 max_rate
= consumed_rate
;
352 } else if (!rlim_ch_num
) {
353 ch_budget
= CPSW_POLL_WEIGHT
/ cpsw
->tx_ch_num
;
355 max_rate
= consumed_rate
;
357 max_rate
= cpsw
->speed
* 1000;
359 /* if max_rate is less then expected due to reduced link speed,
360 * split proportionally according next potential max speed
362 if (max_rate
< consumed_rate
)
365 if (max_rate
< consumed_rate
)
368 ch_budget
= (consumed_rate
* CPSW_POLL_WEIGHT
) / max_rate
;
369 ch_budget
= (CPSW_POLL_WEIGHT
- ch_budget
) /
370 (cpsw
->tx_ch_num
- rlim_ch_num
);
371 bigest_rate
= (max_rate
- consumed_rate
) /
372 (cpsw
->tx_ch_num
- rlim_ch_num
);
375 /* split tx weight/budget */
376 budget
= CPSW_POLL_WEIGHT
;
377 for (i
= 0; i
< cpsw
->tx_ch_num
; i
++) {
378 ch_rate
= cpdma_chan_get_rate(txv
[i
].ch
);
380 txv
[i
].budget
= (ch_rate
* CPSW_POLL_WEIGHT
) / max_rate
;
383 if (ch_rate
> bigest_rate
) {
385 bigest_rate
= ch_rate
;
388 ch_weight
= (ch_rate
* 100) / max_rate
;
391 cpdma_chan_set_weight(cpsw
->txv
[i
].ch
, ch_weight
);
393 txv
[i
].budget
= ch_budget
;
396 cpdma_chan_set_weight(cpsw
->txv
[i
].ch
, 0);
399 budget
-= txv
[i
].budget
;
403 txv
[bigest_rate_ch
].budget
+= budget
;
405 /* split rx budget */
406 budget
= CPSW_POLL_WEIGHT
;
407 ch_budget
= budget
/ cpsw
->rx_ch_num
;
408 for (i
= 0; i
< cpsw
->rx_ch_num
; i
++) {
409 cpsw
->rxv
[i
].budget
= ch_budget
;
414 cpsw
->rxv
[0].budget
+= budget
;
417 int cpsw_init_common(struct cpsw_common
*cpsw
, void __iomem
*ss_regs
,
418 int ale_ageout
, phys_addr_t desc_mem_phys
,
421 u32 slave_offset
, sliver_offset
, slave_size
;
422 struct cpsw_ale_params ale_params
;
423 struct cpsw_platform_data
*data
;
424 struct cpdma_params dma_params
;
425 struct device
*dev
= cpsw
->dev
;
426 struct device_node
*cpts_node
;
427 void __iomem
*cpts_regs
;
434 cpsw
->version
= readl(&cpsw
->regs
->id_ver
);
436 memset(&dma_params
, 0, sizeof(dma_params
));
437 memset(&ale_params
, 0, sizeof(ale_params
));
439 switch (cpsw
->version
) {
441 cpsw
->host_port_regs
= ss_regs
+ CPSW1_HOST_PORT_OFFSET
;
442 cpts_regs
= ss_regs
+ CPSW1_CPTS_OFFSET
;
443 cpsw
->hw_stats
= ss_regs
+ CPSW1_HW_STATS
;
444 dma_params
.dmaregs
= ss_regs
+ CPSW1_CPDMA_OFFSET
;
445 dma_params
.txhdp
= ss_regs
+ CPSW1_STATERAM_OFFSET
;
446 ale_params
.ale_regs
= ss_regs
+ CPSW1_ALE_OFFSET
;
447 slave_offset
= CPSW1_SLAVE_OFFSET
;
448 slave_size
= CPSW1_SLAVE_SIZE
;
449 sliver_offset
= CPSW1_SLIVER_OFFSET
;
450 dma_params
.desc_mem_phys
= 0;
455 cpsw
->host_port_regs
= ss_regs
+ CPSW2_HOST_PORT_OFFSET
;
456 cpts_regs
= ss_regs
+ CPSW2_CPTS_OFFSET
;
457 cpsw
->hw_stats
= ss_regs
+ CPSW2_HW_STATS
;
458 dma_params
.dmaregs
= ss_regs
+ CPSW2_CPDMA_OFFSET
;
459 dma_params
.txhdp
= ss_regs
+ CPSW2_STATERAM_OFFSET
;
460 ale_params
.ale_regs
= ss_regs
+ CPSW2_ALE_OFFSET
;
461 slave_offset
= CPSW2_SLAVE_OFFSET
;
462 slave_size
= CPSW2_SLAVE_SIZE
;
463 sliver_offset
= CPSW2_SLIVER_OFFSET
;
464 dma_params
.desc_mem_phys
= desc_mem_phys
;
467 dev_err(dev
, "unknown version 0x%08x\n", cpsw
->version
);
471 for (i
= 0; i
< cpsw
->data
.slaves
; i
++) {
472 struct cpsw_slave
*slave
= &cpsw
->slaves
[i
];
473 void __iomem
*regs
= cpsw
->regs
;
475 slave
->slave_num
= i
;
476 slave
->data
= &cpsw
->data
.slave_data
[i
];
477 slave
->regs
= regs
+ slave_offset
;
478 slave
->port_vlan
= slave
->data
->dual_emac_res_vlan
;
479 slave
->mac_sl
= cpsw_sl_get("cpsw", dev
, regs
+ sliver_offset
);
480 if (IS_ERR(slave
->mac_sl
))
481 return PTR_ERR(slave
->mac_sl
);
483 slave_offset
+= slave_size
;
484 sliver_offset
+= SLIVER_SIZE
;
487 ale_params
.dev
= dev
;
488 ale_params
.ale_ageout
= ale_ageout
;
489 ale_params
.ale_entries
= data
->ale_entries
;
490 ale_params
.ale_ports
= CPSW_ALE_PORTS_NUM
;
492 cpsw
->ale
= cpsw_ale_create(&ale_params
);
494 dev_err(dev
, "error initializing ale engine\n");
498 dma_params
.dev
= dev
;
499 dma_params
.rxthresh
= dma_params
.dmaregs
+ CPDMA_RXTHRESH
;
500 dma_params
.rxfree
= dma_params
.dmaregs
+ CPDMA_RXFREE
;
501 dma_params
.rxhdp
= dma_params
.txhdp
+ CPDMA_RXHDP
;
502 dma_params
.txcp
= dma_params
.txhdp
+ CPDMA_TXCP
;
503 dma_params
.rxcp
= dma_params
.txhdp
+ CPDMA_RXCP
;
505 dma_params
.num_chan
= data
->channels
;
506 dma_params
.has_soft_reset
= true;
507 dma_params
.min_packet_size
= CPSW_MIN_PACKET_SIZE
;
508 dma_params
.desc_mem_size
= data
->bd_ram_size
;
509 dma_params
.desc_align
= 16;
510 dma_params
.has_ext_regs
= true;
511 dma_params
.desc_hw_addr
= dma_params
.desc_mem_phys
;
512 dma_params
.bus_freq_mhz
= cpsw
->bus_freq_mhz
;
513 dma_params
.descs_pool_size
= descs_pool_size
;
515 cpsw
->dma
= cpdma_ctlr_create(&dma_params
);
517 dev_err(dev
, "error initializing dma\n");
521 cpts_node
= of_get_child_by_name(cpsw
->dev
->of_node
, "cpts");
523 cpts_node
= cpsw
->dev
->of_node
;
525 cpsw
->cpts
= cpts_create(cpsw
->dev
, cpts_regs
, cpts_node
);
526 if (IS_ERR(cpsw
->cpts
)) {
527 ret
= PTR_ERR(cpsw
->cpts
);
528 cpdma_ctlr_destroy(cpsw
->dma
);
530 of_node_put(cpts_node
);
535 #if IS_ENABLED(CONFIG_TI_CPTS)
537 static void cpsw_hwtstamp_v1(struct cpsw_priv
*priv
)
539 struct cpsw_common
*cpsw
= priv
->cpsw
;
540 struct cpsw_slave
*slave
= &cpsw
->slaves
[cpsw_slave_index(cpsw
, priv
)];
543 if (!priv
->tx_ts_enabled
&& !priv
->rx_ts_enabled
) {
544 slave_write(slave
, 0, CPSW1_TS_CTL
);
548 seq_id
= (30 << CPSW_V1_SEQ_ID_OFS_SHIFT
) | ETH_P_1588
;
549 ts_en
= EVENT_MSG_BITS
<< CPSW_V1_MSG_TYPE_OFS
;
551 if (priv
->tx_ts_enabled
)
552 ts_en
|= CPSW_V1_TS_TX_EN
;
554 if (priv
->rx_ts_enabled
)
555 ts_en
|= CPSW_V1_TS_RX_EN
;
557 slave_write(slave
, ts_en
, CPSW1_TS_CTL
);
558 slave_write(slave
, seq_id
, CPSW1_TS_SEQ_LTYPE
);
561 static void cpsw_hwtstamp_v2(struct cpsw_priv
*priv
)
563 struct cpsw_common
*cpsw
= priv
->cpsw
;
564 struct cpsw_slave
*slave
;
567 slave
= &cpsw
->slaves
[cpsw_slave_index(cpsw
, priv
)];
569 ctrl
= slave_read(slave
, CPSW2_CONTROL
);
570 switch (cpsw
->version
) {
572 ctrl
&= ~CTRL_V2_ALL_TS_MASK
;
574 if (priv
->tx_ts_enabled
)
575 ctrl
|= CTRL_V2_TX_TS_BITS
;
577 if (priv
->rx_ts_enabled
)
578 ctrl
|= CTRL_V2_RX_TS_BITS
;
582 ctrl
&= ~CTRL_V3_ALL_TS_MASK
;
584 if (priv
->tx_ts_enabled
)
585 ctrl
|= CTRL_V3_TX_TS_BITS
;
587 if (priv
->rx_ts_enabled
)
588 ctrl
|= CTRL_V3_RX_TS_BITS
;
592 mtype
= (30 << TS_SEQ_ID_OFFSET_SHIFT
) | EVENT_MSG_BITS
;
594 slave_write(slave
, mtype
, CPSW2_TS_SEQ_MTYPE
);
595 slave_write(slave
, ctrl
, CPSW2_CONTROL
);
596 writel_relaxed(ETH_P_1588
, &cpsw
->regs
->ts_ltype
);
597 writel_relaxed(ETH_P_8021Q
, &cpsw
->regs
->vlan_ltype
);
600 static int cpsw_hwtstamp_set(struct net_device
*dev
, struct ifreq
*ifr
)
602 struct cpsw_priv
*priv
= netdev_priv(dev
);
603 struct cpsw_common
*cpsw
= priv
->cpsw
;
604 struct hwtstamp_config cfg
;
606 if (cpsw
->version
!= CPSW_VERSION_1
&&
607 cpsw
->version
!= CPSW_VERSION_2
&&
608 cpsw
->version
!= CPSW_VERSION_3
)
611 if (copy_from_user(&cfg
, ifr
->ifr_data
, sizeof(cfg
)))
614 /* reserved for future extensions */
618 if (cfg
.tx_type
!= HWTSTAMP_TX_OFF
&& cfg
.tx_type
!= HWTSTAMP_TX_ON
)
621 switch (cfg
.rx_filter
) {
622 case HWTSTAMP_FILTER_NONE
:
623 priv
->rx_ts_enabled
= 0;
625 case HWTSTAMP_FILTER_ALL
:
626 case HWTSTAMP_FILTER_NTP_ALL
:
628 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
629 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
630 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
631 priv
->rx_ts_enabled
= HWTSTAMP_FILTER_PTP_V1_L4_EVENT
;
632 cfg
.rx_filter
= HWTSTAMP_FILTER_PTP_V1_L4_EVENT
;
634 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
635 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
636 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
637 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
638 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
639 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
640 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
641 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
642 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
643 priv
->rx_ts_enabled
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
644 cfg
.rx_filter
= HWTSTAMP_FILTER_PTP_V2_EVENT
;
650 priv
->tx_ts_enabled
= cfg
.tx_type
== HWTSTAMP_TX_ON
;
652 switch (cpsw
->version
) {
654 cpsw_hwtstamp_v1(priv
);
658 cpsw_hwtstamp_v2(priv
);
664 return copy_to_user(ifr
->ifr_data
, &cfg
, sizeof(cfg
)) ? -EFAULT
: 0;
667 static int cpsw_hwtstamp_get(struct net_device
*dev
, struct ifreq
*ifr
)
669 struct cpsw_common
*cpsw
= ndev_to_cpsw(dev
);
670 struct cpsw_priv
*priv
= netdev_priv(dev
);
671 struct hwtstamp_config cfg
;
673 if (cpsw
->version
!= CPSW_VERSION_1
&&
674 cpsw
->version
!= CPSW_VERSION_2
&&
675 cpsw
->version
!= CPSW_VERSION_3
)
679 cfg
.tx_type
= priv
->tx_ts_enabled
? HWTSTAMP_TX_ON
: HWTSTAMP_TX_OFF
;
680 cfg
.rx_filter
= priv
->rx_ts_enabled
;
682 return copy_to_user(ifr
->ifr_data
, &cfg
, sizeof(cfg
)) ? -EFAULT
: 0;
685 static int cpsw_hwtstamp_get(struct net_device
*dev
, struct ifreq
*ifr
)
690 static int cpsw_hwtstamp_set(struct net_device
*dev
, struct ifreq
*ifr
)
694 #endif /*CONFIG_TI_CPTS*/
696 int cpsw_ndo_ioctl(struct net_device
*dev
, struct ifreq
*req
, int cmd
)
698 struct cpsw_priv
*priv
= netdev_priv(dev
);
699 struct cpsw_common
*cpsw
= priv
->cpsw
;
700 int slave_no
= cpsw_slave_index(cpsw
, priv
);
702 if (!netif_running(dev
))
707 return cpsw_hwtstamp_set(dev
, req
);
709 return cpsw_hwtstamp_get(dev
, req
);
712 if (!cpsw
->slaves
[slave_no
].phy
)
714 return phy_mii_ioctl(cpsw
->slaves
[slave_no
].phy
, req
, cmd
);
717 int cpsw_ndo_set_tx_maxrate(struct net_device
*ndev
, int queue
, u32 rate
)
719 struct cpsw_priv
*priv
= netdev_priv(ndev
);
720 struct cpsw_common
*cpsw
= priv
->cpsw
;
721 struct cpsw_slave
*slave
;
726 ch_rate
= netdev_get_tx_queue(ndev
, queue
)->tx_maxrate
;
730 ch_rate
= rate
* 1000;
731 min_rate
= cpdma_chan_get_min_rate(cpsw
->dma
);
732 if ((ch_rate
< min_rate
&& ch_rate
)) {
733 dev_err(priv
->dev
, "The channel rate cannot be less than %dMbps",
738 if (rate
> cpsw
->speed
) {
739 dev_err(priv
->dev
, "The channel rate cannot be more than 2Gbps");
743 ret
= pm_runtime_get_sync(cpsw
->dev
);
745 pm_runtime_put_noidle(cpsw
->dev
);
749 ret
= cpdma_chan_set_rate(cpsw
->txv
[queue
].ch
, ch_rate
);
750 pm_runtime_put(cpsw
->dev
);
755 /* update rates for slaves tx queues */
756 for (i
= 0; i
< cpsw
->data
.slaves
; i
++) {
757 slave
= &cpsw
->slaves
[i
];
761 netdev_get_tx_queue(slave
->ndev
, queue
)->tx_maxrate
= rate
;
764 cpsw_split_res(cpsw
);
768 static int cpsw_tc_to_fifo(int tc
, int num_tc
)
770 if (tc
== num_tc
- 1)
773 return CPSW_FIFO_SHAPERS_NUM
- tc
;
776 bool cpsw_shp_is_off(struct cpsw_priv
*priv
)
778 struct cpsw_common
*cpsw
= priv
->cpsw
;
779 struct cpsw_slave
*slave
;
780 u32 shift
, mask
, val
;
782 val
= readl_relaxed(&cpsw
->regs
->ptype
);
784 slave
= &cpsw
->slaves
[cpsw_slave_index(cpsw
, priv
)];
785 shift
= CPSW_FIFO_SHAPE_EN_SHIFT
+ 3 * slave
->slave_num
;
792 static void cpsw_fifo_shp_on(struct cpsw_priv
*priv
, int fifo
, int on
)
794 struct cpsw_common
*cpsw
= priv
->cpsw
;
795 struct cpsw_slave
*slave
;
796 u32 shift
, mask
, val
;
798 val
= readl_relaxed(&cpsw
->regs
->ptype
);
800 slave
= &cpsw
->slaves
[cpsw_slave_index(cpsw
, priv
)];
801 shift
= CPSW_FIFO_SHAPE_EN_SHIFT
+ 3 * slave
->slave_num
;
802 mask
= (1 << --fifo
) << shift
;
803 val
= on
? val
| mask
: val
& ~mask
;
805 writel_relaxed(val
, &cpsw
->regs
->ptype
);
808 static int cpsw_set_fifo_bw(struct cpsw_priv
*priv
, int fifo
, int bw
)
810 struct cpsw_common
*cpsw
= priv
->cpsw
;
811 u32 val
= 0, send_pct
, shift
;
812 struct cpsw_slave
*slave
;
815 if (bw
> priv
->shp_cfg_speed
* 1000)
818 /* shaping has to stay enabled for highest fifos linearly
819 * and fifo bw no more then interface can allow
821 slave
= &cpsw
->slaves
[cpsw_slave_index(cpsw
, priv
)];
822 send_pct
= slave_read(slave
, SEND_PERCENT
);
823 for (i
= CPSW_FIFO_SHAPERS_NUM
; i
> 0; i
--) {
825 if (i
>= fifo
|| !priv
->fifo_bw
[i
])
828 dev_warn(priv
->dev
, "Prev FIFO%d is shaped", i
);
832 if (!priv
->fifo_bw
[i
] && i
> fifo
) {
833 dev_err(priv
->dev
, "Upper FIFO%d is not shaped", i
);
839 send_pct
&= ~(CPSW_PCT_MASK
<< shift
);
840 val
= DIV_ROUND_UP(bw
, priv
->shp_cfg_speed
* 10);
844 send_pct
|= val
<< shift
;
849 if (priv
->fifo_bw
[i
])
850 pct
+= (send_pct
>> shift
) & CPSW_PCT_MASK
;
856 slave_write(slave
, send_pct
, SEND_PERCENT
);
857 priv
->fifo_bw
[fifo
] = bw
;
859 dev_warn(priv
->dev
, "set FIFO%d bw = %d\n", fifo
,
860 DIV_ROUND_CLOSEST(val
* priv
->shp_cfg_speed
, 100));
864 dev_err(priv
->dev
, "Bandwidth doesn't fit in tc configuration");
868 static int cpsw_set_fifo_rlimit(struct cpsw_priv
*priv
, int fifo
, int bw
)
870 struct cpsw_common
*cpsw
= priv
->cpsw
;
871 struct cpsw_slave
*slave
;
872 u32 tx_in_ctl_rg
, val
;
875 ret
= cpsw_set_fifo_bw(priv
, fifo
, bw
);
879 slave
= &cpsw
->slaves
[cpsw_slave_index(cpsw
, priv
)];
880 tx_in_ctl_rg
= cpsw
->version
== CPSW_VERSION_1
?
881 CPSW1_TX_IN_CTL
: CPSW2_TX_IN_CTL
;
884 cpsw_fifo_shp_on(priv
, fifo
, bw
);
886 val
= slave_read(slave
, tx_in_ctl_rg
);
887 if (cpsw_shp_is_off(priv
)) {
888 /* disable FIFOs rate limited queues */
889 val
&= ~(0xf << CPSW_FIFO_RATE_EN_SHIFT
);
891 /* set type of FIFO queues to normal priority mode */
892 val
&= ~(3 << CPSW_FIFO_QUEUE_TYPE_SHIFT
);
894 /* set type of FIFO queues to be rate limited */
896 val
|= 2 << CPSW_FIFO_QUEUE_TYPE_SHIFT
;
898 priv
->shp_cfg_speed
= 0;
901 /* toggle a FIFO rate limited queue */
903 val
|= BIT(fifo
+ CPSW_FIFO_RATE_EN_SHIFT
);
905 val
&= ~BIT(fifo
+ CPSW_FIFO_RATE_EN_SHIFT
);
906 slave_write(slave
, val
, tx_in_ctl_rg
);
908 /* FIFO transmit shape enable */
909 cpsw_fifo_shp_on(priv
, fifo
, bw
);
916 * shaping for class A should be set first
918 static int cpsw_set_cbs(struct net_device
*ndev
,
919 struct tc_cbs_qopt_offload
*qopt
)
921 struct cpsw_priv
*priv
= netdev_priv(ndev
);
922 struct cpsw_common
*cpsw
= priv
->cpsw
;
923 struct cpsw_slave
*slave
;
928 tc
= netdev_txq_to_tc(priv
->ndev
, qopt
->queue
);
930 /* enable channels in backward order, as highest FIFOs must be rate
931 * limited first and for compliance with CPDMA rate limited channels
932 * that also used in bacward order. FIFO0 cannot be rate limited.
934 fifo
= cpsw_tc_to_fifo(tc
, ndev
->num_tc
);
936 dev_err(priv
->dev
, "Last tc%d can't be rate limited", tc
);
940 /* do nothing, it's disabled anyway */
941 if (!qopt
->enable
&& !priv
->fifo_bw
[fifo
])
944 /* shapers can be set if link speed is known */
945 slave
= &cpsw
->slaves
[cpsw_slave_index(cpsw
, priv
)];
946 if (slave
->phy
&& slave
->phy
->link
) {
947 if (priv
->shp_cfg_speed
&&
948 priv
->shp_cfg_speed
!= slave
->phy
->speed
)
949 prev_speed
= priv
->shp_cfg_speed
;
951 priv
->shp_cfg_speed
= slave
->phy
->speed
;
954 if (!priv
->shp_cfg_speed
) {
955 dev_err(priv
->dev
, "Link speed is not known");
959 ret
= pm_runtime_get_sync(cpsw
->dev
);
961 pm_runtime_put_noidle(cpsw
->dev
);
965 bw
= qopt
->enable
? qopt
->idleslope
: 0;
966 ret
= cpsw_set_fifo_rlimit(priv
, fifo
, bw
);
968 priv
->shp_cfg_speed
= prev_speed
;
972 if (bw
&& prev_speed
)
974 "Speed was changed, CBS shaper speeds are changed!");
976 pm_runtime_put_sync(cpsw
->dev
);
980 static int cpsw_set_mqprio(struct net_device
*ndev
, void *type_data
)
982 struct tc_mqprio_qopt_offload
*mqprio
= type_data
;
983 struct cpsw_priv
*priv
= netdev_priv(ndev
);
984 struct cpsw_common
*cpsw
= priv
->cpsw
;
985 int fifo
, num_tc
, count
, offset
;
986 struct cpsw_slave
*slave
;
990 num_tc
= mqprio
->qopt
.num_tc
;
991 if (num_tc
> CPSW_TC_NUM
)
994 if (mqprio
->mode
!= TC_MQPRIO_MODE_DCB
)
997 ret
= pm_runtime_get_sync(cpsw
->dev
);
999 pm_runtime_put_noidle(cpsw
->dev
);
1004 for (i
= 0; i
< 8; i
++) {
1005 tc
= mqprio
->qopt
.prio_tc_map
[i
];
1006 fifo
= cpsw_tc_to_fifo(tc
, num_tc
);
1007 tx_prio_map
|= fifo
<< (4 * i
);
1010 netdev_set_num_tc(ndev
, num_tc
);
1011 for (i
= 0; i
< num_tc
; i
++) {
1012 count
= mqprio
->qopt
.count
[i
];
1013 offset
= mqprio
->qopt
.offset
[i
];
1014 netdev_set_tc_queue(ndev
, i
, count
, offset
);
1018 if (!mqprio
->qopt
.hw
) {
1019 /* restore default configuration */
1020 netdev_reset_tc(ndev
);
1021 tx_prio_map
= TX_PRIORITY_MAPPING
;
1024 priv
->mqprio_hw
= mqprio
->qopt
.hw
;
1026 offset
= cpsw
->version
== CPSW_VERSION_1
?
1027 CPSW1_TX_PRI_MAP
: CPSW2_TX_PRI_MAP
;
1029 slave
= &cpsw
->slaves
[cpsw_slave_index(cpsw
, priv
)];
1030 slave_write(slave
, tx_prio_map
, offset
);
1032 pm_runtime_put_sync(cpsw
->dev
);
1037 int cpsw_ndo_setup_tc(struct net_device
*ndev
, enum tc_setup_type type
,
1041 case TC_SETUP_QDISC_CBS
:
1042 return cpsw_set_cbs(ndev
, type_data
);
1044 case TC_SETUP_QDISC_MQPRIO
:
1045 return cpsw_set_mqprio(ndev
, type_data
);
1052 void cpsw_cbs_resume(struct cpsw_slave
*slave
, struct cpsw_priv
*priv
)
1056 for (fifo
= CPSW_FIFO_SHAPERS_NUM
; fifo
> 0; fifo
--) {
1057 bw
= priv
->fifo_bw
[fifo
];
1061 cpsw_set_fifo_rlimit(priv
, fifo
, bw
);
1065 void cpsw_mqprio_resume(struct cpsw_slave
*slave
, struct cpsw_priv
*priv
)
1067 struct cpsw_common
*cpsw
= priv
->cpsw
;
1068 u32 tx_prio_map
= 0;
1072 if (!priv
->mqprio_hw
)
1075 for (i
= 0; i
< 8; i
++) {
1076 tc
= netdev_get_prio_tc_map(priv
->ndev
, i
);
1077 fifo
= CPSW_FIFO_SHAPERS_NUM
- tc
;
1078 tx_prio_map
|= fifo
<< (4 * i
);
1081 tx_prio_rg
= cpsw
->version
== CPSW_VERSION_1
?
1082 CPSW1_TX_PRI_MAP
: CPSW2_TX_PRI_MAP
;
1084 slave_write(slave
, tx_prio_map
, tx_prio_rg
);
1087 int cpsw_fill_rx_channels(struct cpsw_priv
*priv
)
1089 struct cpsw_common
*cpsw
= priv
->cpsw
;
1090 struct cpsw_meta_xdp
*xmeta
;
1091 struct page_pool
*pool
;
1097 for (ch
= 0; ch
< cpsw
->rx_ch_num
; ch
++) {
1098 pool
= cpsw
->page_pool
[ch
];
1099 ch_buf_num
= cpdma_chan_get_rx_buf_num(cpsw
->rxv
[ch
].ch
);
1100 for (i
= 0; i
< ch_buf_num
; i
++) {
1101 page
= page_pool_dev_alloc_pages(pool
);
1103 cpsw_err(priv
, ifup
, "allocate rx page err\n");
1107 xmeta
= page_address(page
) + CPSW_XMETA_OFFSET
;
1108 xmeta
->ndev
= priv
->ndev
;
1111 dma
= page_pool_get_dma_addr(page
) + CPSW_HEADROOM
;
1112 ret
= cpdma_chan_idle_submit_mapped(cpsw
->rxv
[ch
].ch
,
1114 cpsw
->rx_packet_max
,
1117 cpsw_err(priv
, ifup
,
1118 "cannot submit page to channel %d rx, error %d\n",
1120 page_pool_recycle_direct(pool
, page
);
1125 cpsw_info(priv
, ifup
, "ch %d rx, submitted %d descriptors\n",
1132 static struct page_pool
*cpsw_create_page_pool(struct cpsw_common
*cpsw
,
1135 struct page_pool_params pp_params
;
1136 struct page_pool
*pool
;
1138 pp_params
.order
= 0;
1139 pp_params
.flags
= PP_FLAG_DMA_MAP
;
1140 pp_params
.pool_size
= size
;
1141 pp_params
.nid
= NUMA_NO_NODE
;
1142 pp_params
.dma_dir
= DMA_BIDIRECTIONAL
;
1143 pp_params
.dev
= cpsw
->dev
;
1145 pool
= page_pool_create(&pp_params
);
1147 dev_err(cpsw
->dev
, "cannot create rx page pool\n");
1152 static int cpsw_create_rx_pool(struct cpsw_common
*cpsw
, int ch
)
1154 struct page_pool
*pool
;
1155 int ret
= 0, pool_size
;
1157 pool_size
= cpdma_chan_get_rx_buf_num(cpsw
->rxv
[ch
].ch
);
1158 pool
= cpsw_create_page_pool(cpsw
, pool_size
);
1160 ret
= PTR_ERR(pool
);
1162 cpsw
->page_pool
[ch
] = pool
;
1167 static int cpsw_ndev_create_xdp_rxq(struct cpsw_priv
*priv
, int ch
)
1169 struct cpsw_common
*cpsw
= priv
->cpsw
;
1170 struct xdp_rxq_info
*rxq
;
1171 struct page_pool
*pool
;
1174 pool
= cpsw
->page_pool
[ch
];
1175 rxq
= &priv
->xdp_rxq
[ch
];
1177 ret
= xdp_rxq_info_reg(rxq
, priv
->ndev
, ch
);
1181 ret
= xdp_rxq_info_reg_mem_model(rxq
, MEM_TYPE_PAGE_POOL
, pool
);
1183 xdp_rxq_info_unreg(rxq
);
1188 static void cpsw_ndev_destroy_xdp_rxq(struct cpsw_priv
*priv
, int ch
)
1190 struct xdp_rxq_info
*rxq
= &priv
->xdp_rxq
[ch
];
1192 if (!xdp_rxq_info_is_reg(rxq
))
1195 xdp_rxq_info_unreg(rxq
);
1198 void cpsw_destroy_xdp_rxqs(struct cpsw_common
*cpsw
)
1200 struct net_device
*ndev
;
1203 for (ch
= 0; ch
< cpsw
->rx_ch_num
; ch
++) {
1204 for (i
= 0; i
< cpsw
->data
.slaves
; i
++) {
1205 ndev
= cpsw
->slaves
[i
].ndev
;
1209 cpsw_ndev_destroy_xdp_rxq(netdev_priv(ndev
), ch
);
1212 page_pool_destroy(cpsw
->page_pool
[ch
]);
1213 cpsw
->page_pool
[ch
] = NULL
;
1217 int cpsw_create_xdp_rxqs(struct cpsw_common
*cpsw
)
1219 struct net_device
*ndev
;
1222 for (ch
= 0; ch
< cpsw
->rx_ch_num
; ch
++) {
1223 ret
= cpsw_create_rx_pool(cpsw
, ch
);
1227 /* using same page pool is allowed as no running rx handlers
1228 * simultaneously for both ndevs
1230 for (i
= 0; i
< cpsw
->data
.slaves
; i
++) {
1231 ndev
= cpsw
->slaves
[i
].ndev
;
1235 ret
= cpsw_ndev_create_xdp_rxq(netdev_priv(ndev
), ch
);
1244 cpsw_destroy_xdp_rxqs(cpsw
);
1249 static int cpsw_xdp_prog_setup(struct cpsw_priv
*priv
, struct netdev_bpf
*bpf
)
1251 struct bpf_prog
*prog
= bpf
->prog
;
1253 if (!priv
->xdpi
.prog
&& !prog
)
1256 if (!xdp_attachment_flags_ok(&priv
->xdpi
, bpf
))
1259 WRITE_ONCE(priv
->xdp_prog
, prog
);
1261 xdp_attachment_setup(&priv
->xdpi
, bpf
);
1266 int cpsw_ndo_bpf(struct net_device
*ndev
, struct netdev_bpf
*bpf
)
1268 struct cpsw_priv
*priv
= netdev_priv(ndev
);
1270 switch (bpf
->command
) {
1271 case XDP_SETUP_PROG
:
1272 return cpsw_xdp_prog_setup(priv
, bpf
);
1274 case XDP_QUERY_PROG
:
1275 return xdp_attachment_query(&priv
->xdpi
, bpf
);
1282 int cpsw_xdp_tx_frame(struct cpsw_priv
*priv
, struct xdp_frame
*xdpf
,
1283 struct page
*page
, int port
)
1285 struct cpsw_common
*cpsw
= priv
->cpsw
;
1286 struct cpsw_meta_xdp
*xmeta
;
1287 struct cpdma_chan
*txch
;
1291 xmeta
= (void *)xdpf
+ CPSW_XMETA_OFFSET
;
1292 xmeta
->ndev
= priv
->ndev
;
1294 txch
= cpsw
->txv
[0].ch
;
1297 dma
= page_pool_get_dma_addr(page
);
1298 dma
+= xdpf
->headroom
+ sizeof(struct xdp_frame
);
1299 ret
= cpdma_chan_submit_mapped(txch
, cpsw_xdpf_to_handle(xdpf
),
1300 dma
, xdpf
->len
, port
);
1302 if (sizeof(*xmeta
) > xdpf
->headroom
) {
1303 xdp_return_frame_rx_napi(xdpf
);
1307 ret
= cpdma_chan_submit(txch
, cpsw_xdpf_to_handle(xdpf
),
1308 xdpf
->data
, xdpf
->len
, port
);
1312 priv
->ndev
->stats
.tx_dropped
++;
1313 xdp_return_frame_rx_napi(xdpf
);
1319 int cpsw_run_xdp(struct cpsw_priv
*priv
, int ch
, struct xdp_buff
*xdp
,
1320 struct page
*page
, int port
)
1322 struct cpsw_common
*cpsw
= priv
->cpsw
;
1323 struct net_device
*ndev
= priv
->ndev
;
1324 int ret
= CPSW_XDP_CONSUMED
;
1325 struct xdp_frame
*xdpf
;
1326 struct bpf_prog
*prog
;
1331 prog
= READ_ONCE(priv
->xdp_prog
);
1333 ret
= CPSW_XDP_PASS
;
1337 act
= bpf_prog_run_xdp(prog
, xdp
);
1340 ret
= CPSW_XDP_PASS
;
1343 xdpf
= convert_to_xdp_frame(xdp
);
1344 if (unlikely(!xdpf
))
1347 cpsw_xdp_tx_frame(priv
, xdpf
, page
, port
);
1350 if (xdp_do_redirect(ndev
, xdp
, prog
))
1353 /* Have to flush here, per packet, instead of doing it in bulk
1354 * at the end of the napi handler. The RX devices on this
1355 * particular hardware is sharing a common queue, so the
1356 * incoming device might change per packet.
1361 bpf_warn_invalid_xdp_action(act
);
1364 trace_xdp_exception(ndev
, prog
, act
);
1365 /* fall through -- handle aborts by dropping packet */
1374 page_pool_recycle_direct(cpsw
->page_pool
[ch
], page
);