1 /* Synopsys DesignWare Core Enterprise Ethernet (XLGMAC) Driver
3 * Copyright (c) 2017 Synopsys, Inc. (www.synopsys.com)
5 * This program is dual-licensed; you may select either version 2 of
6 * the GNU General Public License ("GPL") or BSD license ("BSD").
8 * This Synopsys DWC XLGMAC software driver and associated documentation
9 * (hereinafter the "Software") is an unsupported proprietary work of
10 * Synopsys, Inc. unless otherwise expressly agreed to in writing between
11 * Synopsys and you. The Software IS NOT an item of Licensed Software or a
12 * Licensed Product under any End User Software License Agreement or
13 * Agreement for Licensed Products with Synopsys or any supplement thereto.
14 * Synopsys is a registered trademark of Synopsys, Inc. Other names included
15 * in the SOFTWARE may be the trademarks of their respective owners.
18 #include <linux/netdevice.h>
19 #include <linux/tcp.h>
20 #include <linux/interrupt.h>
22 #include "dwc-xlgmac.h"
23 #include "dwc-xlgmac-reg.h"
25 static int xlgmac_one_poll(struct napi_struct
*, int);
26 static int xlgmac_all_poll(struct napi_struct
*, int);
28 static inline unsigned int xlgmac_tx_avail_desc(struct xlgmac_ring
*ring
)
30 return (ring
->dma_desc_count
- (ring
->cur
- ring
->dirty
));
33 static inline unsigned int xlgmac_rx_dirty_desc(struct xlgmac_ring
*ring
)
35 return (ring
->cur
- ring
->dirty
);
38 static int xlgmac_maybe_stop_tx_queue(
39 struct xlgmac_channel
*channel
,
40 struct xlgmac_ring
*ring
,
43 struct xlgmac_pdata
*pdata
= channel
->pdata
;
45 if (count
> xlgmac_tx_avail_desc(ring
)) {
46 netif_info(pdata
, drv
, pdata
->netdev
,
47 "Tx queue stopped, not enough descriptors available\n");
48 netif_stop_subqueue(pdata
->netdev
, channel
->queue_index
);
49 ring
->tx
.queue_stopped
= 1;
51 /* If we haven't notified the hardware because of xmit_more
52 * support, tell it now
54 if (ring
->tx
.xmit_more
)
55 pdata
->hw_ops
.tx_start_xmit(channel
, ring
);
57 return NETDEV_TX_BUSY
;
63 static void xlgmac_prep_vlan(struct sk_buff
*skb
,
64 struct xlgmac_pkt_info
*pkt_info
)
66 if (skb_vlan_tag_present(skb
))
67 pkt_info
->vlan_ctag
= skb_vlan_tag_get(skb
);
70 static int xlgmac_prep_tso(struct sk_buff
*skb
,
71 struct xlgmac_pkt_info
*pkt_info
)
75 if (!XLGMAC_GET_REG_BITS(pkt_info
->attributes
,
76 TX_PACKET_ATTRIBUTES_TSO_ENABLE_POS
,
77 TX_PACKET_ATTRIBUTES_TSO_ENABLE_LEN
))
80 ret
= skb_cow_head(skb
, 0);
84 pkt_info
->header_len
= skb_tcp_all_headers(skb
);
85 pkt_info
->tcp_header_len
= tcp_hdrlen(skb
);
86 pkt_info
->tcp_payload_len
= skb
->len
- pkt_info
->header_len
;
87 pkt_info
->mss
= skb_shinfo(skb
)->gso_size
;
89 XLGMAC_PR("header_len=%u\n", pkt_info
->header_len
);
90 XLGMAC_PR("tcp_header_len=%u, tcp_payload_len=%u\n",
91 pkt_info
->tcp_header_len
, pkt_info
->tcp_payload_len
);
92 XLGMAC_PR("mss=%u\n", pkt_info
->mss
);
94 /* Update the number of packets that will ultimately be transmitted
95 * along with the extra bytes for each extra packet
97 pkt_info
->tx_packets
= skb_shinfo(skb
)->gso_segs
;
98 pkt_info
->tx_bytes
+= (pkt_info
->tx_packets
- 1) * pkt_info
->header_len
;
103 static int xlgmac_is_tso(struct sk_buff
*skb
)
105 if (skb
->ip_summed
!= CHECKSUM_PARTIAL
)
108 if (!skb_is_gso(skb
))
114 static void xlgmac_prep_tx_pkt(struct xlgmac_pdata
*pdata
,
115 struct xlgmac_ring
*ring
,
117 struct xlgmac_pkt_info
*pkt_info
)
120 unsigned int context_desc
;
127 pkt_info
->desc_count
= 0;
129 pkt_info
->tx_packets
= 1;
130 pkt_info
->tx_bytes
= skb
->len
;
132 if (xlgmac_is_tso(skb
)) {
133 /* TSO requires an extra descriptor if mss is different */
134 if (skb_shinfo(skb
)->gso_size
!= ring
->tx
.cur_mss
) {
136 pkt_info
->desc_count
++;
139 /* TSO requires an extra descriptor for TSO header */
140 pkt_info
->desc_count
++;
142 pkt_info
->attributes
= XLGMAC_SET_REG_BITS(
143 pkt_info
->attributes
,
144 TX_PACKET_ATTRIBUTES_TSO_ENABLE_POS
,
145 TX_PACKET_ATTRIBUTES_TSO_ENABLE_LEN
,
147 pkt_info
->attributes
= XLGMAC_SET_REG_BITS(
148 pkt_info
->attributes
,
149 TX_PACKET_ATTRIBUTES_CSUM_ENABLE_POS
,
150 TX_PACKET_ATTRIBUTES_CSUM_ENABLE_LEN
,
152 } else if (skb
->ip_summed
== CHECKSUM_PARTIAL
)
153 pkt_info
->attributes
= XLGMAC_SET_REG_BITS(
154 pkt_info
->attributes
,
155 TX_PACKET_ATTRIBUTES_CSUM_ENABLE_POS
,
156 TX_PACKET_ATTRIBUTES_CSUM_ENABLE_LEN
,
159 if (skb_vlan_tag_present(skb
)) {
160 /* VLAN requires an extra descriptor if tag is different */
161 if (skb_vlan_tag_get(skb
) != ring
->tx
.cur_vlan_ctag
)
162 /* We can share with the TSO context descriptor */
165 pkt_info
->desc_count
++;
168 pkt_info
->attributes
= XLGMAC_SET_REG_BITS(
169 pkt_info
->attributes
,
170 TX_PACKET_ATTRIBUTES_VLAN_CTAG_POS
,
171 TX_PACKET_ATTRIBUTES_VLAN_CTAG_LEN
,
175 for (len
= skb_headlen(skb
); len
;) {
176 pkt_info
->desc_count
++;
177 len
-= min_t(unsigned int, len
, XLGMAC_TX_MAX_BUF_SIZE
);
180 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
181 frag
= &skb_shinfo(skb
)->frags
[i
];
182 for (len
= skb_frag_size(frag
); len
; ) {
183 pkt_info
->desc_count
++;
184 len
-= min_t(unsigned int, len
, XLGMAC_TX_MAX_BUF_SIZE
);
189 static int xlgmac_calc_rx_buf_size(struct net_device
*netdev
, unsigned int mtu
)
191 unsigned int rx_buf_size
;
193 if (mtu
> XLGMAC_JUMBO_PACKET_MTU
) {
194 netdev_alert(netdev
, "MTU exceeds maximum supported value\n");
198 rx_buf_size
= mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ VLAN_HLEN
;
199 rx_buf_size
= clamp_val(rx_buf_size
, XLGMAC_RX_MIN_BUF_SIZE
, PAGE_SIZE
);
201 rx_buf_size
= (rx_buf_size
+ XLGMAC_RX_BUF_ALIGN
- 1) &
202 ~(XLGMAC_RX_BUF_ALIGN
- 1);
207 static void xlgmac_enable_rx_tx_ints(struct xlgmac_pdata
*pdata
)
209 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
210 struct xlgmac_channel
*channel
;
211 enum xlgmac_int int_id
;
214 channel
= pdata
->channel_head
;
215 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
216 if (channel
->tx_ring
&& channel
->rx_ring
)
217 int_id
= XLGMAC_INT_DMA_CH_SR_TI_RI
;
218 else if (channel
->tx_ring
)
219 int_id
= XLGMAC_INT_DMA_CH_SR_TI
;
220 else if (channel
->rx_ring
)
221 int_id
= XLGMAC_INT_DMA_CH_SR_RI
;
225 hw_ops
->enable_int(channel
, int_id
);
229 static void xlgmac_disable_rx_tx_ints(struct xlgmac_pdata
*pdata
)
231 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
232 struct xlgmac_channel
*channel
;
233 enum xlgmac_int int_id
;
236 channel
= pdata
->channel_head
;
237 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
238 if (channel
->tx_ring
&& channel
->rx_ring
)
239 int_id
= XLGMAC_INT_DMA_CH_SR_TI_RI
;
240 else if (channel
->tx_ring
)
241 int_id
= XLGMAC_INT_DMA_CH_SR_TI
;
242 else if (channel
->rx_ring
)
243 int_id
= XLGMAC_INT_DMA_CH_SR_RI
;
247 hw_ops
->disable_int(channel
, int_id
);
251 static irqreturn_t
xlgmac_isr(int irq
, void *data
)
253 unsigned int dma_isr
, dma_ch_isr
, mac_isr
;
254 struct xlgmac_pdata
*pdata
= data
;
255 struct xlgmac_channel
*channel
;
256 struct xlgmac_hw_ops
*hw_ops
;
257 unsigned int i
, ti
, ri
;
259 hw_ops
= &pdata
->hw_ops
;
261 /* The DMA interrupt status register also reports MAC and MTL
262 * interrupts. So for polling mode, we just need to check for
263 * this register to be non-zero
265 dma_isr
= readl(pdata
->mac_regs
+ DMA_ISR
);
269 netif_dbg(pdata
, intr
, pdata
->netdev
, "DMA_ISR=%#010x\n", dma_isr
);
271 for (i
= 0; i
< pdata
->channel_count
; i
++) {
272 if (!(dma_isr
& (1 << i
)))
275 channel
= pdata
->channel_head
+ i
;
277 dma_ch_isr
= readl(XLGMAC_DMA_REG(channel
, DMA_CH_SR
));
278 netif_dbg(pdata
, intr
, pdata
->netdev
, "DMA_CH%u_ISR=%#010x\n",
281 /* The TI or RI interrupt bits may still be set even if using
282 * per channel DMA interrupts. Check to be sure those are not
283 * enabled before using the private data napi structure.
285 ti
= XLGMAC_GET_REG_BITS(dma_ch_isr
, DMA_CH_SR_TI_POS
,
287 ri
= XLGMAC_GET_REG_BITS(dma_ch_isr
, DMA_CH_SR_RI_POS
,
289 if (!pdata
->per_channel_irq
&& (ti
|| ri
)) {
290 if (napi_schedule_prep(&pdata
->napi
)) {
291 /* Disable Tx and Rx interrupts */
292 xlgmac_disable_rx_tx_ints(pdata
);
294 pdata
->stats
.napi_poll_isr
++;
295 /* Turn on polling */
296 __napi_schedule_irqoff(&pdata
->napi
);
300 if (XLGMAC_GET_REG_BITS(dma_ch_isr
, DMA_CH_SR_TPS_POS
,
302 pdata
->stats
.tx_process_stopped
++;
304 if (XLGMAC_GET_REG_BITS(dma_ch_isr
, DMA_CH_SR_RPS_POS
,
306 pdata
->stats
.rx_process_stopped
++;
308 if (XLGMAC_GET_REG_BITS(dma_ch_isr
, DMA_CH_SR_TBU_POS
,
310 pdata
->stats
.tx_buffer_unavailable
++;
312 if (XLGMAC_GET_REG_BITS(dma_ch_isr
, DMA_CH_SR_RBU_POS
,
314 pdata
->stats
.rx_buffer_unavailable
++;
316 /* Restart the device on a Fatal Bus Error */
317 if (XLGMAC_GET_REG_BITS(dma_ch_isr
, DMA_CH_SR_FBE_POS
,
318 DMA_CH_SR_FBE_LEN
)) {
319 pdata
->stats
.fatal_bus_error
++;
320 schedule_work(&pdata
->restart_work
);
323 /* Clear all interrupt signals */
324 writel(dma_ch_isr
, XLGMAC_DMA_REG(channel
, DMA_CH_SR
));
327 if (XLGMAC_GET_REG_BITS(dma_isr
, DMA_ISR_MACIS_POS
,
328 DMA_ISR_MACIS_LEN
)) {
329 mac_isr
= readl(pdata
->mac_regs
+ MAC_ISR
);
331 if (XLGMAC_GET_REG_BITS(mac_isr
, MAC_ISR_MMCTXIS_POS
,
332 MAC_ISR_MMCTXIS_LEN
))
333 hw_ops
->tx_mmc_int(pdata
);
335 if (XLGMAC_GET_REG_BITS(mac_isr
, MAC_ISR_MMCRXIS_POS
,
336 MAC_ISR_MMCRXIS_LEN
))
337 hw_ops
->rx_mmc_int(pdata
);
343 static irqreturn_t
xlgmac_dma_isr(int irq
, void *data
)
345 struct xlgmac_channel
*channel
= data
;
347 /* Per channel DMA interrupts are enabled, so we use the per
348 * channel napi structure and not the private data napi structure
350 if (napi_schedule_prep(&channel
->napi
)) {
351 /* Disable Tx and Rx interrupts */
352 disable_irq_nosync(channel
->dma_irq
);
354 /* Turn on polling */
355 __napi_schedule_irqoff(&channel
->napi
);
361 static void xlgmac_tx_timer(struct timer_list
*t
)
363 struct xlgmac_channel
*channel
= from_timer(channel
, t
, tx_timer
);
364 struct xlgmac_pdata
*pdata
= channel
->pdata
;
365 struct napi_struct
*napi
;
367 napi
= (pdata
->per_channel_irq
) ? &channel
->napi
: &pdata
->napi
;
369 if (napi_schedule_prep(napi
)) {
370 /* Disable Tx and Rx interrupts */
371 if (pdata
->per_channel_irq
)
372 disable_irq_nosync(channel
->dma_irq
);
374 xlgmac_disable_rx_tx_ints(pdata
);
376 pdata
->stats
.napi_poll_txtimer
++;
377 /* Turn on polling */
378 __napi_schedule(napi
);
381 channel
->tx_timer_active
= 0;
384 static void xlgmac_init_timers(struct xlgmac_pdata
*pdata
)
386 struct xlgmac_channel
*channel
;
389 channel
= pdata
->channel_head
;
390 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
391 if (!channel
->tx_ring
)
394 timer_setup(&channel
->tx_timer
, xlgmac_tx_timer
, 0);
398 static void xlgmac_stop_timers(struct xlgmac_pdata
*pdata
)
400 struct xlgmac_channel
*channel
;
403 channel
= pdata
->channel_head
;
404 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
405 if (!channel
->tx_ring
)
408 del_timer_sync(&channel
->tx_timer
);
412 static void xlgmac_napi_enable(struct xlgmac_pdata
*pdata
, unsigned int add
)
414 struct xlgmac_channel
*channel
;
417 if (pdata
->per_channel_irq
) {
418 channel
= pdata
->channel_head
;
419 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
421 netif_napi_add(pdata
->netdev
, &channel
->napi
,
424 napi_enable(&channel
->napi
);
428 netif_napi_add(pdata
->netdev
, &pdata
->napi
,
431 napi_enable(&pdata
->napi
);
435 static void xlgmac_napi_disable(struct xlgmac_pdata
*pdata
, unsigned int del
)
437 struct xlgmac_channel
*channel
;
440 if (pdata
->per_channel_irq
) {
441 channel
= pdata
->channel_head
;
442 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
443 napi_disable(&channel
->napi
);
446 netif_napi_del(&channel
->napi
);
449 napi_disable(&pdata
->napi
);
452 netif_napi_del(&pdata
->napi
);
456 static int xlgmac_request_irqs(struct xlgmac_pdata
*pdata
)
458 struct net_device
*netdev
= pdata
->netdev
;
459 struct xlgmac_channel
*channel
;
463 ret
= devm_request_irq(pdata
->dev
, pdata
->dev_irq
, xlgmac_isr
,
464 IRQF_SHARED
, netdev
->name
, pdata
);
466 netdev_alert(netdev
, "error requesting irq %d\n",
471 if (!pdata
->per_channel_irq
)
474 channel
= pdata
->channel_head
;
475 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
476 snprintf(channel
->dma_irq_name
,
477 sizeof(channel
->dma_irq_name
) - 1,
478 "%s-TxRx-%u", netdev_name(netdev
),
479 channel
->queue_index
);
481 ret
= devm_request_irq(pdata
->dev
, channel
->dma_irq
,
483 channel
->dma_irq_name
, channel
);
485 netdev_alert(netdev
, "error requesting irq %d\n",
494 /* Using an unsigned int, 'i' will go to UINT_MAX and exit */
495 for (i
--, channel
--; i
< pdata
->channel_count
; i
--, channel
--)
496 devm_free_irq(pdata
->dev
, channel
->dma_irq
, channel
);
498 devm_free_irq(pdata
->dev
, pdata
->dev_irq
, pdata
);
503 static void xlgmac_free_irqs(struct xlgmac_pdata
*pdata
)
505 struct xlgmac_channel
*channel
;
508 devm_free_irq(pdata
->dev
, pdata
->dev_irq
, pdata
);
510 if (!pdata
->per_channel_irq
)
513 channel
= pdata
->channel_head
;
514 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++)
515 devm_free_irq(pdata
->dev
, channel
->dma_irq
, channel
);
518 static void xlgmac_free_tx_data(struct xlgmac_pdata
*pdata
)
520 struct xlgmac_desc_ops
*desc_ops
= &pdata
->desc_ops
;
521 struct xlgmac_desc_data
*desc_data
;
522 struct xlgmac_channel
*channel
;
523 struct xlgmac_ring
*ring
;
526 channel
= pdata
->channel_head
;
527 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
528 ring
= channel
->tx_ring
;
532 for (j
= 0; j
< ring
->dma_desc_count
; j
++) {
533 desc_data
= XLGMAC_GET_DESC_DATA(ring
, j
);
534 desc_ops
->unmap_desc_data(pdata
, desc_data
);
539 static void xlgmac_free_rx_data(struct xlgmac_pdata
*pdata
)
541 struct xlgmac_desc_ops
*desc_ops
= &pdata
->desc_ops
;
542 struct xlgmac_desc_data
*desc_data
;
543 struct xlgmac_channel
*channel
;
544 struct xlgmac_ring
*ring
;
547 channel
= pdata
->channel_head
;
548 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
549 ring
= channel
->rx_ring
;
553 for (j
= 0; j
< ring
->dma_desc_count
; j
++) {
554 desc_data
= XLGMAC_GET_DESC_DATA(ring
, j
);
555 desc_ops
->unmap_desc_data(pdata
, desc_data
);
560 static int xlgmac_start(struct xlgmac_pdata
*pdata
)
562 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
563 struct net_device
*netdev
= pdata
->netdev
;
567 xlgmac_napi_enable(pdata
, 1);
569 ret
= xlgmac_request_irqs(pdata
);
573 hw_ops
->enable_tx(pdata
);
574 hw_ops
->enable_rx(pdata
);
575 netif_tx_start_all_queues(netdev
);
580 xlgmac_napi_disable(pdata
, 1);
586 static void xlgmac_stop(struct xlgmac_pdata
*pdata
)
588 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
589 struct net_device
*netdev
= pdata
->netdev
;
590 struct xlgmac_channel
*channel
;
591 struct netdev_queue
*txq
;
594 netif_tx_stop_all_queues(netdev
);
595 xlgmac_stop_timers(pdata
);
596 hw_ops
->disable_tx(pdata
);
597 hw_ops
->disable_rx(pdata
);
598 xlgmac_free_irqs(pdata
);
599 xlgmac_napi_disable(pdata
, 1);
602 channel
= pdata
->channel_head
;
603 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
604 if (!channel
->tx_ring
)
607 txq
= netdev_get_tx_queue(netdev
, channel
->queue_index
);
608 netdev_tx_reset_queue(txq
);
612 static void xlgmac_restart_dev(struct xlgmac_pdata
*pdata
)
614 /* If not running, "restart" will happen on open */
615 if (!netif_running(pdata
->netdev
))
620 xlgmac_free_tx_data(pdata
);
621 xlgmac_free_rx_data(pdata
);
626 static void xlgmac_restart(struct work_struct
*work
)
628 struct xlgmac_pdata
*pdata
= container_of(work
,
634 xlgmac_restart_dev(pdata
);
639 static int xlgmac_open(struct net_device
*netdev
)
641 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
642 struct xlgmac_desc_ops
*desc_ops
;
645 desc_ops
= &pdata
->desc_ops
;
647 /* TODO: Initialize the phy */
649 /* Calculate the Rx buffer size before allocating rings */
650 ret
= xlgmac_calc_rx_buf_size(netdev
, netdev
->mtu
);
653 pdata
->rx_buf_size
= ret
;
655 /* Allocate the channels and rings */
656 ret
= desc_ops
->alloc_channels_and_rings(pdata
);
660 INIT_WORK(&pdata
->restart_work
, xlgmac_restart
);
661 xlgmac_init_timers(pdata
);
663 ret
= xlgmac_start(pdata
);
665 goto err_channels_and_rings
;
669 err_channels_and_rings
:
670 desc_ops
->free_channels_and_rings(pdata
);
675 static int xlgmac_close(struct net_device
*netdev
)
677 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
678 struct xlgmac_desc_ops
*desc_ops
;
680 desc_ops
= &pdata
->desc_ops
;
682 /* Stop the device */
685 /* Free the channels and rings */
686 desc_ops
->free_channels_and_rings(pdata
);
691 static void xlgmac_tx_timeout(struct net_device
*netdev
, unsigned int txqueue
)
693 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
695 netdev_warn(netdev
, "tx timeout, device restarting\n");
696 schedule_work(&pdata
->restart_work
);
699 static netdev_tx_t
xlgmac_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
701 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
702 struct xlgmac_pkt_info
*tx_pkt_info
;
703 struct xlgmac_desc_ops
*desc_ops
;
704 struct xlgmac_channel
*channel
;
705 struct xlgmac_hw_ops
*hw_ops
;
706 struct netdev_queue
*txq
;
707 struct xlgmac_ring
*ring
;
710 desc_ops
= &pdata
->desc_ops
;
711 hw_ops
= &pdata
->hw_ops
;
713 XLGMAC_PR("skb->len = %d\n", skb
->len
);
715 channel
= pdata
->channel_head
+ skb
->queue_mapping
;
716 txq
= netdev_get_tx_queue(netdev
, channel
->queue_index
);
717 ring
= channel
->tx_ring
;
718 tx_pkt_info
= &ring
->pkt_info
;
721 netif_err(pdata
, tx_err
, netdev
,
722 "empty skb received from stack\n");
723 dev_kfree_skb_any(skb
);
727 /* Prepare preliminary packet info for TX */
728 memset(tx_pkt_info
, 0, sizeof(*tx_pkt_info
));
729 xlgmac_prep_tx_pkt(pdata
, ring
, skb
, tx_pkt_info
);
731 /* Check that there are enough descriptors available */
732 ret
= xlgmac_maybe_stop_tx_queue(channel
, ring
,
733 tx_pkt_info
->desc_count
);
737 ret
= xlgmac_prep_tso(skb
, tx_pkt_info
);
739 netif_err(pdata
, tx_err
, netdev
,
740 "error processing TSO packet\n");
741 dev_kfree_skb_any(skb
);
744 xlgmac_prep_vlan(skb
, tx_pkt_info
);
746 if (!desc_ops
->map_tx_skb(channel
, skb
)) {
747 dev_kfree_skb_any(skb
);
751 /* Report on the actual number of bytes (to be) sent */
752 netdev_tx_sent_queue(txq
, tx_pkt_info
->tx_bytes
);
754 /* Configure required descriptor fields for transmission */
755 hw_ops
->dev_xmit(channel
);
757 if (netif_msg_pktdata(pdata
))
758 xlgmac_print_pkt(netdev
, skb
, true);
760 /* Stop the queue in advance if there may not be enough descriptors */
761 xlgmac_maybe_stop_tx_queue(channel
, ring
, XLGMAC_TX_MAX_DESC_NR
);
766 static void xlgmac_get_stats64(struct net_device
*netdev
,
767 struct rtnl_link_stats64
*s
)
769 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
770 struct xlgmac_stats
*pstats
= &pdata
->stats
;
772 pdata
->hw_ops
.read_mmc_stats(pdata
);
774 s
->rx_packets
= pstats
->rxframecount_gb
;
775 s
->rx_bytes
= pstats
->rxoctetcount_gb
;
776 s
->rx_errors
= pstats
->rxframecount_gb
-
777 pstats
->rxbroadcastframes_g
-
778 pstats
->rxmulticastframes_g
-
779 pstats
->rxunicastframes_g
;
780 s
->multicast
= pstats
->rxmulticastframes_g
;
781 s
->rx_length_errors
= pstats
->rxlengtherror
;
782 s
->rx_crc_errors
= pstats
->rxcrcerror
;
783 s
->rx_fifo_errors
= pstats
->rxfifooverflow
;
785 s
->tx_packets
= pstats
->txframecount_gb
;
786 s
->tx_bytes
= pstats
->txoctetcount_gb
;
787 s
->tx_errors
= pstats
->txframecount_gb
- pstats
->txframecount_g
;
788 s
->tx_dropped
= netdev
->stats
.tx_dropped
;
791 static int xlgmac_set_mac_address(struct net_device
*netdev
, void *addr
)
793 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
794 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
795 struct sockaddr
*saddr
= addr
;
797 if (!is_valid_ether_addr(saddr
->sa_data
))
798 return -EADDRNOTAVAIL
;
800 eth_hw_addr_set(netdev
, saddr
->sa_data
);
802 hw_ops
->set_mac_address(pdata
, netdev
->dev_addr
);
807 static int xlgmac_ioctl(struct net_device
*netdev
,
808 struct ifreq
*ifreq
, int cmd
)
810 if (!netif_running(netdev
))
816 static int xlgmac_change_mtu(struct net_device
*netdev
, int mtu
)
818 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
821 ret
= xlgmac_calc_rx_buf_size(netdev
, mtu
);
825 pdata
->rx_buf_size
= ret
;
826 WRITE_ONCE(netdev
->mtu
, mtu
);
828 xlgmac_restart_dev(pdata
);
833 static int xlgmac_vlan_rx_add_vid(struct net_device
*netdev
,
837 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
838 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
840 set_bit(vid
, pdata
->active_vlans
);
841 hw_ops
->update_vlan_hash_table(pdata
);
846 static int xlgmac_vlan_rx_kill_vid(struct net_device
*netdev
,
850 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
851 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
853 clear_bit(vid
, pdata
->active_vlans
);
854 hw_ops
->update_vlan_hash_table(pdata
);
859 #ifdef CONFIG_NET_POLL_CONTROLLER
860 static void xlgmac_poll_controller(struct net_device
*netdev
)
862 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
863 struct xlgmac_channel
*channel
;
866 if (pdata
->per_channel_irq
) {
867 channel
= pdata
->channel_head
;
868 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++)
869 xlgmac_dma_isr(channel
->dma_irq
, channel
);
871 disable_irq(pdata
->dev_irq
);
872 xlgmac_isr(pdata
->dev_irq
, pdata
);
873 enable_irq(pdata
->dev_irq
);
876 #endif /* CONFIG_NET_POLL_CONTROLLER */
878 static int xlgmac_set_features(struct net_device
*netdev
,
879 netdev_features_t features
)
881 netdev_features_t rxhash
, rxcsum
, rxvlan
, rxvlan_filter
;
882 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
883 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
886 rxhash
= pdata
->netdev_features
& NETIF_F_RXHASH
;
887 rxcsum
= pdata
->netdev_features
& NETIF_F_RXCSUM
;
888 rxvlan
= pdata
->netdev_features
& NETIF_F_HW_VLAN_CTAG_RX
;
889 rxvlan_filter
= pdata
->netdev_features
& NETIF_F_HW_VLAN_CTAG_FILTER
;
891 if ((features
& NETIF_F_RXHASH
) && !rxhash
)
892 ret
= hw_ops
->enable_rss(pdata
);
893 else if (!(features
& NETIF_F_RXHASH
) && rxhash
)
894 ret
= hw_ops
->disable_rss(pdata
);
898 if ((features
& NETIF_F_RXCSUM
) && !rxcsum
)
899 hw_ops
->enable_rx_csum(pdata
);
900 else if (!(features
& NETIF_F_RXCSUM
) && rxcsum
)
901 hw_ops
->disable_rx_csum(pdata
);
903 if ((features
& NETIF_F_HW_VLAN_CTAG_RX
) && !rxvlan
)
904 hw_ops
->enable_rx_vlan_stripping(pdata
);
905 else if (!(features
& NETIF_F_HW_VLAN_CTAG_RX
) && rxvlan
)
906 hw_ops
->disable_rx_vlan_stripping(pdata
);
908 if ((features
& NETIF_F_HW_VLAN_CTAG_FILTER
) && !rxvlan_filter
)
909 hw_ops
->enable_rx_vlan_filtering(pdata
);
910 else if (!(features
& NETIF_F_HW_VLAN_CTAG_FILTER
) && rxvlan_filter
)
911 hw_ops
->disable_rx_vlan_filtering(pdata
);
913 pdata
->netdev_features
= features
;
918 static void xlgmac_set_rx_mode(struct net_device
*netdev
)
920 struct xlgmac_pdata
*pdata
= netdev_priv(netdev
);
921 struct xlgmac_hw_ops
*hw_ops
= &pdata
->hw_ops
;
923 hw_ops
->config_rx_mode(pdata
);
926 static const struct net_device_ops xlgmac_netdev_ops
= {
927 .ndo_open
= xlgmac_open
,
928 .ndo_stop
= xlgmac_close
,
929 .ndo_start_xmit
= xlgmac_xmit
,
930 .ndo_tx_timeout
= xlgmac_tx_timeout
,
931 .ndo_get_stats64
= xlgmac_get_stats64
,
932 .ndo_change_mtu
= xlgmac_change_mtu
,
933 .ndo_set_mac_address
= xlgmac_set_mac_address
,
934 .ndo_validate_addr
= eth_validate_addr
,
935 .ndo_eth_ioctl
= xlgmac_ioctl
,
936 .ndo_vlan_rx_add_vid
= xlgmac_vlan_rx_add_vid
,
937 .ndo_vlan_rx_kill_vid
= xlgmac_vlan_rx_kill_vid
,
938 #ifdef CONFIG_NET_POLL_CONTROLLER
939 .ndo_poll_controller
= xlgmac_poll_controller
,
941 .ndo_set_features
= xlgmac_set_features
,
942 .ndo_set_rx_mode
= xlgmac_set_rx_mode
,
945 const struct net_device_ops
*xlgmac_get_netdev_ops(void)
947 return &xlgmac_netdev_ops
;
950 static void xlgmac_rx_refresh(struct xlgmac_channel
*channel
)
952 struct xlgmac_pdata
*pdata
= channel
->pdata
;
953 struct xlgmac_ring
*ring
= channel
->rx_ring
;
954 struct xlgmac_desc_data
*desc_data
;
955 struct xlgmac_desc_ops
*desc_ops
;
956 struct xlgmac_hw_ops
*hw_ops
;
958 desc_ops
= &pdata
->desc_ops
;
959 hw_ops
= &pdata
->hw_ops
;
961 while (ring
->dirty
!= ring
->cur
) {
962 desc_data
= XLGMAC_GET_DESC_DATA(ring
, ring
->dirty
);
964 /* Reset desc_data values */
965 desc_ops
->unmap_desc_data(pdata
, desc_data
);
967 if (desc_ops
->map_rx_buffer(pdata
, ring
, desc_data
))
970 hw_ops
->rx_desc_reset(pdata
, desc_data
, ring
->dirty
);
975 /* Make sure everything is written before the register write */
978 /* Update the Rx Tail Pointer Register with address of
979 * the last cleaned entry
981 desc_data
= XLGMAC_GET_DESC_DATA(ring
, ring
->dirty
- 1);
982 writel(lower_32_bits(desc_data
->dma_desc_addr
),
983 XLGMAC_DMA_REG(channel
, DMA_CH_RDTR_LO
));
986 static struct sk_buff
*xlgmac_create_skb(struct xlgmac_pdata
*pdata
,
987 struct napi_struct
*napi
,
988 struct xlgmac_desc_data
*desc_data
,
991 unsigned int copy_len
;
995 skb
= napi_alloc_skb(napi
, desc_data
->rx
.hdr
.dma_len
);
999 /* Start with the header buffer which may contain just the header
1000 * or the header plus data
1002 dma_sync_single_range_for_cpu(pdata
->dev
, desc_data
->rx
.hdr
.dma_base
,
1003 desc_data
->rx
.hdr
.dma_off
,
1004 desc_data
->rx
.hdr
.dma_len
,
1007 packet
= page_address(desc_data
->rx
.hdr
.pa
.pages
) +
1008 desc_data
->rx
.hdr
.pa
.pages_offset
;
1009 copy_len
= (desc_data
->rx
.hdr_len
) ? desc_data
->rx
.hdr_len
: len
;
1010 copy_len
= min(desc_data
->rx
.hdr
.dma_len
, copy_len
);
1011 skb_copy_to_linear_data(skb
, packet
, copy_len
);
1012 skb_put(skb
, copy_len
);
1016 /* Add the remaining data as a frag */
1017 dma_sync_single_range_for_cpu(pdata
->dev
,
1018 desc_data
->rx
.buf
.dma_base
,
1019 desc_data
->rx
.buf
.dma_off
,
1020 desc_data
->rx
.buf
.dma_len
,
1023 skb_add_rx_frag(skb
, skb_shinfo(skb
)->nr_frags
,
1024 desc_data
->rx
.buf
.pa
.pages
,
1025 desc_data
->rx
.buf
.pa
.pages_offset
,
1026 len
, desc_data
->rx
.buf
.dma_len
);
1027 desc_data
->rx
.buf
.pa
.pages
= NULL
;
1033 static int xlgmac_tx_poll(struct xlgmac_channel
*channel
)
1035 struct xlgmac_pdata
*pdata
= channel
->pdata
;
1036 struct xlgmac_ring
*ring
= channel
->tx_ring
;
1037 struct net_device
*netdev
= pdata
->netdev
;
1038 unsigned int tx_packets
= 0, tx_bytes
= 0;
1039 struct xlgmac_desc_data
*desc_data
;
1040 struct xlgmac_dma_desc
*dma_desc
;
1041 struct xlgmac_desc_ops
*desc_ops
;
1042 struct xlgmac_hw_ops
*hw_ops
;
1043 struct netdev_queue
*txq
;
1047 desc_ops
= &pdata
->desc_ops
;
1048 hw_ops
= &pdata
->hw_ops
;
1050 /* Nothing to do if there isn't a Tx ring for this channel */
1056 /* Be sure we get ring->cur before accessing descriptor data */
1059 txq
= netdev_get_tx_queue(netdev
, channel
->queue_index
);
1061 while ((processed
< XLGMAC_TX_DESC_MAX_PROC
) &&
1062 (ring
->dirty
!= cur
)) {
1063 desc_data
= XLGMAC_GET_DESC_DATA(ring
, ring
->dirty
);
1064 dma_desc
= desc_data
->dma_desc
;
1066 if (!hw_ops
->tx_complete(dma_desc
))
1069 /* Make sure descriptor fields are read after reading
1074 if (netif_msg_tx_done(pdata
))
1075 xlgmac_dump_tx_desc(pdata
, ring
, ring
->dirty
, 1, 0);
1077 if (hw_ops
->is_last_desc(dma_desc
)) {
1078 tx_packets
+= desc_data
->tx
.packets
;
1079 tx_bytes
+= desc_data
->tx
.bytes
;
1082 /* Free the SKB and reset the descriptor for re-use */
1083 desc_ops
->unmap_desc_data(pdata
, desc_data
);
1084 hw_ops
->tx_desc_reset(desc_data
);
1093 netdev_tx_completed_queue(txq
, tx_packets
, tx_bytes
);
1095 if ((ring
->tx
.queue_stopped
== 1) &&
1096 (xlgmac_tx_avail_desc(ring
) > XLGMAC_TX_DESC_MIN_FREE
)) {
1097 ring
->tx
.queue_stopped
= 0;
1098 netif_tx_wake_queue(txq
);
1101 XLGMAC_PR("processed=%d\n", processed
);
1106 static int xlgmac_rx_poll(struct xlgmac_channel
*channel
, int budget
)
1108 struct xlgmac_pdata
*pdata
= channel
->pdata
;
1109 struct xlgmac_ring
*ring
= channel
->rx_ring
;
1110 struct net_device
*netdev
= pdata
->netdev
;
1111 unsigned int len
, dma_desc_len
, max_len
;
1112 unsigned int context_next
, context
;
1113 struct xlgmac_desc_data
*desc_data
;
1114 struct xlgmac_pkt_info
*pkt_info
;
1115 unsigned int incomplete
, error
;
1116 struct xlgmac_hw_ops
*hw_ops
;
1117 unsigned int received
= 0;
1118 struct napi_struct
*napi
;
1119 struct sk_buff
*skb
;
1120 int packet_count
= 0;
1122 hw_ops
= &pdata
->hw_ops
;
1124 /* Nothing to do if there isn't a Rx ring for this channel */
1131 napi
= (pdata
->per_channel_irq
) ? &channel
->napi
: &pdata
->napi
;
1133 desc_data
= XLGMAC_GET_DESC_DATA(ring
, ring
->cur
);
1134 pkt_info
= &ring
->pkt_info
;
1135 while (packet_count
< budget
) {
1136 /* First time in loop see if we need to restore state */
1137 if (!received
&& desc_data
->state_saved
) {
1138 skb
= desc_data
->state
.skb
;
1139 error
= desc_data
->state
.error
;
1140 len
= desc_data
->state
.len
;
1142 memset(pkt_info
, 0, sizeof(*pkt_info
));
1149 desc_data
= XLGMAC_GET_DESC_DATA(ring
, ring
->cur
);
1151 if (xlgmac_rx_dirty_desc(ring
) > XLGMAC_RX_DESC_MAX_DIRTY
)
1152 xlgmac_rx_refresh(channel
);
1154 if (hw_ops
->dev_read(channel
))
1160 incomplete
= XLGMAC_GET_REG_BITS(
1161 pkt_info
->attributes
,
1162 RX_PACKET_ATTRIBUTES_INCOMPLETE_POS
,
1163 RX_PACKET_ATTRIBUTES_INCOMPLETE_LEN
);
1164 context_next
= XLGMAC_GET_REG_BITS(
1165 pkt_info
->attributes
,
1166 RX_PACKET_ATTRIBUTES_CONTEXT_NEXT_POS
,
1167 RX_PACKET_ATTRIBUTES_CONTEXT_NEXT_LEN
);
1168 context
= XLGMAC_GET_REG_BITS(
1169 pkt_info
->attributes
,
1170 RX_PACKET_ATTRIBUTES_CONTEXT_POS
,
1171 RX_PACKET_ATTRIBUTES_CONTEXT_LEN
);
1173 /* Earlier error, just drain the remaining data */
1174 if ((incomplete
|| context_next
) && error
)
1177 if (error
|| pkt_info
->errors
) {
1178 if (pkt_info
->errors
)
1179 netif_err(pdata
, rx_err
, netdev
,
1180 "error in received packet\n");
1186 /* Length is cumulative, get this descriptor's length */
1187 dma_desc_len
= desc_data
->rx
.len
- len
;
1188 len
+= dma_desc_len
;
1190 if (dma_desc_len
&& !skb
) {
1191 skb
= xlgmac_create_skb(pdata
, napi
, desc_data
,
1195 } else if (dma_desc_len
) {
1196 dma_sync_single_range_for_cpu(
1198 desc_data
->rx
.buf
.dma_base
,
1199 desc_data
->rx
.buf
.dma_off
,
1200 desc_data
->rx
.buf
.dma_len
,
1204 skb
, skb_shinfo(skb
)->nr_frags
,
1205 desc_data
->rx
.buf
.pa
.pages
,
1206 desc_data
->rx
.buf
.pa
.pages_offset
,
1208 desc_data
->rx
.buf
.dma_len
);
1209 desc_data
->rx
.buf
.pa
.pages
= NULL
;
1213 if (incomplete
|| context_next
)
1219 /* Be sure we don't exceed the configured MTU */
1220 max_len
= netdev
->mtu
+ ETH_HLEN
;
1221 if (!(netdev
->features
& NETIF_F_HW_VLAN_CTAG_RX
) &&
1222 (skb
->protocol
== htons(ETH_P_8021Q
)))
1223 max_len
+= VLAN_HLEN
;
1225 if (skb
->len
> max_len
) {
1226 netif_err(pdata
, rx_err
, netdev
,
1227 "packet length exceeds configured MTU\n");
1232 if (netif_msg_pktdata(pdata
))
1233 xlgmac_print_pkt(netdev
, skb
, false);
1235 skb_checksum_none_assert(skb
);
1236 if (XLGMAC_GET_REG_BITS(pkt_info
->attributes
,
1237 RX_PACKET_ATTRIBUTES_CSUM_DONE_POS
,
1238 RX_PACKET_ATTRIBUTES_CSUM_DONE_LEN
))
1239 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1241 if (XLGMAC_GET_REG_BITS(pkt_info
->attributes
,
1242 RX_PACKET_ATTRIBUTES_VLAN_CTAG_POS
,
1243 RX_PACKET_ATTRIBUTES_VLAN_CTAG_LEN
)) {
1244 __vlan_hwaccel_put_tag(skb
, htons(ETH_P_8021Q
),
1245 pkt_info
->vlan_ctag
);
1246 pdata
->stats
.rx_vlan_packets
++;
1249 if (XLGMAC_GET_REG_BITS(pkt_info
->attributes
,
1250 RX_PACKET_ATTRIBUTES_RSS_HASH_POS
,
1251 RX_PACKET_ATTRIBUTES_RSS_HASH_LEN
))
1252 skb_set_hash(skb
, pkt_info
->rss_hash
,
1253 pkt_info
->rss_hash_type
);
1256 skb
->protocol
= eth_type_trans(skb
, netdev
);
1257 skb_record_rx_queue(skb
, channel
->queue_index
);
1259 napi_gro_receive(napi
, skb
);
1265 /* Check if we need to save state before leaving */
1266 if (received
&& (incomplete
|| context_next
)) {
1267 desc_data
= XLGMAC_GET_DESC_DATA(ring
, ring
->cur
);
1268 desc_data
->state_saved
= 1;
1269 desc_data
->state
.skb
= skb
;
1270 desc_data
->state
.len
= len
;
1271 desc_data
->state
.error
= error
;
1274 XLGMAC_PR("packet_count = %d\n", packet_count
);
1276 return packet_count
;
1279 static int xlgmac_one_poll(struct napi_struct
*napi
, int budget
)
1281 struct xlgmac_channel
*channel
= container_of(napi
,
1282 struct xlgmac_channel
,
1286 XLGMAC_PR("budget=%d\n", budget
);
1288 /* Cleanup Tx ring first */
1289 xlgmac_tx_poll(channel
);
1291 /* Process Rx ring next */
1292 processed
= xlgmac_rx_poll(channel
, budget
);
1294 /* If we processed everything, we are done */
1295 if (processed
< budget
) {
1296 /* Turn off polling */
1297 napi_complete_done(napi
, processed
);
1299 /* Enable Tx and Rx interrupts */
1300 enable_irq(channel
->dma_irq
);
1303 XLGMAC_PR("received = %d\n", processed
);
1308 static int xlgmac_all_poll(struct napi_struct
*napi
, int budget
)
1310 struct xlgmac_pdata
*pdata
= container_of(napi
,
1311 struct xlgmac_pdata
,
1313 struct xlgmac_channel
*channel
;
1314 int processed
, last_processed
;
1318 XLGMAC_PR("budget=%d\n", budget
);
1321 ring_budget
= budget
/ pdata
->rx_ring_count
;
1323 last_processed
= processed
;
1325 channel
= pdata
->channel_head
;
1326 for (i
= 0; i
< pdata
->channel_count
; i
++, channel
++) {
1327 /* Cleanup Tx ring first */
1328 xlgmac_tx_poll(channel
);
1330 /* Process Rx ring next */
1331 if (ring_budget
> (budget
- processed
))
1332 ring_budget
= budget
- processed
;
1333 processed
+= xlgmac_rx_poll(channel
, ring_budget
);
1335 } while ((processed
< budget
) && (processed
!= last_processed
));
1337 /* If we processed everything, we are done */
1338 if (processed
< budget
) {
1339 /* Turn off polling */
1340 napi_complete_done(napi
, processed
);
1342 /* Enable Tx and Rx interrupts */
1343 xlgmac_enable_rx_tx_ints(pdata
);
1346 XLGMAC_PR("received = %d\n", processed
);