2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
6 * Copyright (C) 2009-2012 Cavium, Inc
9 #include <linux/platform_device.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/etherdevice.h>
12 #include <linux/capability.h>
13 #include <linux/net_tstamp.h>
14 #include <linux/interrupt.h>
15 #include <linux/netdevice.h>
16 #include <linux/spinlock.h>
17 #include <linux/if_vlan.h>
18 #include <linux/of_mdio.h>
19 #include <linux/module.h>
20 #include <linux/of_net.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/phy.h>
26 #include <asm/octeon/octeon.h>
27 #include <asm/octeon/cvmx-mixx-defs.h>
28 #include <asm/octeon/cvmx-agl-defs.h>
30 #define DRV_NAME "octeon_mgmt"
31 #define DRV_VERSION "2.0"
32 #define DRV_DESCRIPTION \
33 "Cavium Networks Octeon MII (management) port Network Driver"
35 #define OCTEON_MGMT_NAPI_WEIGHT 16
37 /* Ring sizes that are powers of two allow for more efficient modulo
40 #define OCTEON_MGMT_RX_RING_SIZE 512
41 #define OCTEON_MGMT_TX_RING_SIZE 128
43 /* Allow 8 bytes for vlan and FCS. */
44 #define OCTEON_MGMT_RX_HEADROOM (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN)
46 union mgmt_port_ring_entry
{
49 #define RING_ENTRY_CODE_DONE 0xf
50 #define RING_ENTRY_CODE_MORE 0x10
51 #ifdef __BIG_ENDIAN_BITFIELD
53 /* Length of the buffer/packet in bytes */
55 /* For TX, signals that the packet should be timestamped */
57 /* The RX error code */
59 /* Physical address of the buffer */
71 #define MIX_ORING1 0x0
72 #define MIX_ORING2 0x8
73 #define MIX_IRING1 0x10
74 #define MIX_IRING2 0x18
76 #define MIX_IRHWM 0x28
77 #define MIX_IRCNT 0x30
78 #define MIX_ORHWM 0x38
79 #define MIX_ORCNT 0x40
81 #define MIX_INTENA 0x50
82 #define MIX_REMCNT 0x58
85 #define AGL_GMX_PRT_CFG 0x10
86 #define AGL_GMX_RX_FRM_CTL 0x18
87 #define AGL_GMX_RX_FRM_MAX 0x30
88 #define AGL_GMX_RX_JABBER 0x38
89 #define AGL_GMX_RX_STATS_CTL 0x50
91 #define AGL_GMX_RX_STATS_PKTS_DRP 0xb0
92 #define AGL_GMX_RX_STATS_OCTS_DRP 0xb8
93 #define AGL_GMX_RX_STATS_PKTS_BAD 0xc0
95 #define AGL_GMX_RX_ADR_CTL 0x100
96 #define AGL_GMX_RX_ADR_CAM_EN 0x108
97 #define AGL_GMX_RX_ADR_CAM0 0x180
98 #define AGL_GMX_RX_ADR_CAM1 0x188
99 #define AGL_GMX_RX_ADR_CAM2 0x190
100 #define AGL_GMX_RX_ADR_CAM3 0x198
101 #define AGL_GMX_RX_ADR_CAM4 0x1a0
102 #define AGL_GMX_RX_ADR_CAM5 0x1a8
104 #define AGL_GMX_TX_CLK 0x208
105 #define AGL_GMX_TX_STATS_CTL 0x268
106 #define AGL_GMX_TX_CTL 0x270
107 #define AGL_GMX_TX_STAT0 0x280
108 #define AGL_GMX_TX_STAT1 0x288
109 #define AGL_GMX_TX_STAT2 0x290
110 #define AGL_GMX_TX_STAT3 0x298
111 #define AGL_GMX_TX_STAT4 0x2a0
112 #define AGL_GMX_TX_STAT5 0x2a8
113 #define AGL_GMX_TX_STAT6 0x2b0
114 #define AGL_GMX_TX_STAT7 0x2b8
115 #define AGL_GMX_TX_STAT8 0x2c0
116 #define AGL_GMX_TX_STAT9 0x2c8
119 struct net_device
*netdev
;
127 dma_addr_t tx_ring_handle
;
128 unsigned int tx_next
;
129 unsigned int tx_next_clean
;
130 unsigned int tx_current_fill
;
131 /* The tx_list lock also protects the ring related variables */
132 struct sk_buff_head tx_list
;
134 /* RX variables only touched in napi_poll. No locking necessary. */
136 dma_addr_t rx_ring_handle
;
137 unsigned int rx_next
;
138 unsigned int rx_next_fill
;
139 unsigned int rx_current_fill
;
140 struct sk_buff_head rx_list
;
143 unsigned int last_duplex
;
144 unsigned int last_link
;
145 unsigned int last_speed
;
147 struct napi_struct napi
;
148 struct tasklet_struct tx_clean_tasklet
;
149 struct phy_device
*phydev
;
150 struct device_node
*phy_np
;
151 resource_size_t mix_phys
;
152 resource_size_t mix_size
;
153 resource_size_t agl_phys
;
154 resource_size_t agl_size
;
155 resource_size_t agl_prt_ctl_phys
;
156 resource_size_t agl_prt_ctl_size
;
159 static void octeon_mgmt_set_rx_irq(struct octeon_mgmt
*p
, int enable
)
161 union cvmx_mixx_intena mix_intena
;
164 spin_lock_irqsave(&p
->lock
, flags
);
165 mix_intena
.u64
= cvmx_read_csr(p
->mix
+ MIX_INTENA
);
166 mix_intena
.s
.ithena
= enable
? 1 : 0;
167 cvmx_write_csr(p
->mix
+ MIX_INTENA
, mix_intena
.u64
);
168 spin_unlock_irqrestore(&p
->lock
, flags
);
171 static void octeon_mgmt_set_tx_irq(struct octeon_mgmt
*p
, int enable
)
173 union cvmx_mixx_intena mix_intena
;
176 spin_lock_irqsave(&p
->lock
, flags
);
177 mix_intena
.u64
= cvmx_read_csr(p
->mix
+ MIX_INTENA
);
178 mix_intena
.s
.othena
= enable
? 1 : 0;
179 cvmx_write_csr(p
->mix
+ MIX_INTENA
, mix_intena
.u64
);
180 spin_unlock_irqrestore(&p
->lock
, flags
);
183 static void octeon_mgmt_enable_rx_irq(struct octeon_mgmt
*p
)
185 octeon_mgmt_set_rx_irq(p
, 1);
188 static void octeon_mgmt_disable_rx_irq(struct octeon_mgmt
*p
)
190 octeon_mgmt_set_rx_irq(p
, 0);
193 static void octeon_mgmt_enable_tx_irq(struct octeon_mgmt
*p
)
195 octeon_mgmt_set_tx_irq(p
, 1);
198 static void octeon_mgmt_disable_tx_irq(struct octeon_mgmt
*p
)
200 octeon_mgmt_set_tx_irq(p
, 0);
203 static unsigned int ring_max_fill(unsigned int ring_size
)
205 return ring_size
- 8;
208 static unsigned int ring_size_to_bytes(unsigned int ring_size
)
210 return ring_size
* sizeof(union mgmt_port_ring_entry
);
213 static void octeon_mgmt_rx_fill_ring(struct net_device
*netdev
)
215 struct octeon_mgmt
*p
= netdev_priv(netdev
);
217 while (p
->rx_current_fill
< ring_max_fill(OCTEON_MGMT_RX_RING_SIZE
)) {
219 union mgmt_port_ring_entry re
;
222 /* CN56XX pass 1 needs 8 bytes of padding. */
223 size
= netdev
->mtu
+ OCTEON_MGMT_RX_HEADROOM
+ 8 + NET_IP_ALIGN
;
225 skb
= netdev_alloc_skb(netdev
, size
);
228 skb_reserve(skb
, NET_IP_ALIGN
);
229 __skb_queue_tail(&p
->rx_list
, skb
);
233 re
.s
.addr
= dma_map_single(p
->dev
, skb
->data
,
237 /* Put it in the ring. */
238 p
->rx_ring
[p
->rx_next_fill
] = re
.d64
;
239 dma_sync_single_for_device(p
->dev
, p
->rx_ring_handle
,
240 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
243 (p
->rx_next_fill
+ 1) % OCTEON_MGMT_RX_RING_SIZE
;
244 p
->rx_current_fill
++;
246 cvmx_write_csr(p
->mix
+ MIX_IRING2
, 1);
250 static void octeon_mgmt_clean_tx_buffers(struct octeon_mgmt
*p
)
252 union cvmx_mixx_orcnt mix_orcnt
;
253 union mgmt_port_ring_entry re
;
258 mix_orcnt
.u64
= cvmx_read_csr(p
->mix
+ MIX_ORCNT
);
259 while (mix_orcnt
.s
.orcnt
) {
260 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
262 mix_orcnt
.u64
= cvmx_read_csr(p
->mix
+ MIX_ORCNT
);
264 if (mix_orcnt
.s
.orcnt
== 0) {
265 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
269 dma_sync_single_for_cpu(p
->dev
, p
->tx_ring_handle
,
270 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
273 re
.d64
= p
->tx_ring
[p
->tx_next_clean
];
275 (p
->tx_next_clean
+ 1) % OCTEON_MGMT_TX_RING_SIZE
;
276 skb
= __skb_dequeue(&p
->tx_list
);
279 mix_orcnt
.s
.orcnt
= 1;
281 /* Acknowledge to hardware that we have the buffer. */
282 cvmx_write_csr(p
->mix
+ MIX_ORCNT
, mix_orcnt
.u64
);
283 p
->tx_current_fill
--;
285 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
287 dma_unmap_single(p
->dev
, re
.s
.addr
, re
.s
.len
,
290 /* Read the hardware TX timestamp if one was recorded */
291 if (unlikely(re
.s
.tstamp
)) {
292 struct skb_shared_hwtstamps ts
;
295 memset(&ts
, 0, sizeof(ts
));
296 /* Read the timestamp */
297 ns
= cvmx_read_csr(CVMX_MIXX_TSTAMP(p
->port
));
298 /* Remove the timestamp from the FIFO */
299 cvmx_write_csr(CVMX_MIXX_TSCTL(p
->port
), 0);
300 /* Tell the kernel about the timestamp */
301 ts
.hwtstamp
= ns_to_ktime(ns
);
302 skb_tstamp_tx(skb
, &ts
);
305 dev_kfree_skb_any(skb
);
308 mix_orcnt
.u64
= cvmx_read_csr(p
->mix
+ MIX_ORCNT
);
311 if (cleaned
&& netif_queue_stopped(p
->netdev
))
312 netif_wake_queue(p
->netdev
);
315 static void octeon_mgmt_clean_tx_tasklet(unsigned long arg
)
317 struct octeon_mgmt
*p
= (struct octeon_mgmt
*)arg
;
318 octeon_mgmt_clean_tx_buffers(p
);
319 octeon_mgmt_enable_tx_irq(p
);
322 static void octeon_mgmt_update_rx_stats(struct net_device
*netdev
)
324 struct octeon_mgmt
*p
= netdev_priv(netdev
);
328 /* These reads also clear the count registers. */
329 drop
= cvmx_read_csr(p
->agl
+ AGL_GMX_RX_STATS_PKTS_DRP
);
330 bad
= cvmx_read_csr(p
->agl
+ AGL_GMX_RX_STATS_PKTS_BAD
);
333 /* Do an atomic update. */
334 spin_lock_irqsave(&p
->lock
, flags
);
335 netdev
->stats
.rx_errors
+= bad
;
336 netdev
->stats
.rx_dropped
+= drop
;
337 spin_unlock_irqrestore(&p
->lock
, flags
);
341 static void octeon_mgmt_update_tx_stats(struct net_device
*netdev
)
343 struct octeon_mgmt
*p
= netdev_priv(netdev
);
346 union cvmx_agl_gmx_txx_stat0 s0
;
347 union cvmx_agl_gmx_txx_stat1 s1
;
349 /* These reads also clear the count registers. */
350 s0
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_TX_STAT0
);
351 s1
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_TX_STAT1
);
353 if (s0
.s
.xsdef
|| s0
.s
.xscol
|| s1
.s
.scol
|| s1
.s
.mcol
) {
354 /* Do an atomic update. */
355 spin_lock_irqsave(&p
->lock
, flags
);
356 netdev
->stats
.tx_errors
+= s0
.s
.xsdef
+ s0
.s
.xscol
;
357 netdev
->stats
.collisions
+= s1
.s
.scol
+ s1
.s
.mcol
;
358 spin_unlock_irqrestore(&p
->lock
, flags
);
363 * Dequeue a receive skb and its corresponding ring entry. The ring
364 * entry is returned, *pskb is updated to point to the skb.
366 static u64
octeon_mgmt_dequeue_rx_buffer(struct octeon_mgmt
*p
,
367 struct sk_buff
**pskb
)
369 union mgmt_port_ring_entry re
;
371 dma_sync_single_for_cpu(p
->dev
, p
->rx_ring_handle
,
372 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
375 re
.d64
= p
->rx_ring
[p
->rx_next
];
376 p
->rx_next
= (p
->rx_next
+ 1) % OCTEON_MGMT_RX_RING_SIZE
;
377 p
->rx_current_fill
--;
378 *pskb
= __skb_dequeue(&p
->rx_list
);
380 dma_unmap_single(p
->dev
, re
.s
.addr
,
381 ETH_FRAME_LEN
+ OCTEON_MGMT_RX_HEADROOM
,
388 static int octeon_mgmt_receive_one(struct octeon_mgmt
*p
)
390 struct net_device
*netdev
= p
->netdev
;
391 union cvmx_mixx_ircnt mix_ircnt
;
392 union mgmt_port_ring_entry re
;
394 struct sk_buff
*skb2
;
395 struct sk_buff
*skb_new
;
396 union mgmt_port_ring_entry re2
;
400 re
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb
);
401 if (likely(re
.s
.code
== RING_ENTRY_CODE_DONE
)) {
402 /* A good packet, send it up. */
403 skb_put(skb
, re
.s
.len
);
405 /* Process the RX timestamp if it was recorded */
406 if (p
->has_rx_tstamp
) {
407 /* The first 8 bytes are the timestamp */
408 u64 ns
= *(u64
*)skb
->data
;
409 struct skb_shared_hwtstamps
*ts
;
410 ts
= skb_hwtstamps(skb
);
411 ts
->hwtstamp
= ns_to_ktime(ns
);
414 skb
->protocol
= eth_type_trans(skb
, netdev
);
415 netdev
->stats
.rx_packets
++;
416 netdev
->stats
.rx_bytes
+= skb
->len
;
417 netif_receive_skb(skb
);
419 } else if (re
.s
.code
== RING_ENTRY_CODE_MORE
) {
420 /* Packet split across skbs. This can happen if we
421 * increase the MTU. Buffers that are already in the
422 * rx ring can then end up being too small. As the rx
423 * ring is refilled, buffers sized for the new MTU
424 * will be used and we should go back to the normal
427 skb_put(skb
, re
.s
.len
);
429 re2
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb2
);
430 if (re2
.s
.code
!= RING_ENTRY_CODE_MORE
431 && re2
.s
.code
!= RING_ENTRY_CODE_DONE
)
433 skb_put(skb2
, re2
.s
.len
);
434 skb_new
= skb_copy_expand(skb
, 0, skb2
->len
,
438 if (skb_copy_bits(skb2
, 0, skb_tail_pointer(skb_new
),
441 skb_put(skb_new
, skb2
->len
);
442 dev_kfree_skb_any(skb
);
443 dev_kfree_skb_any(skb2
);
445 } while (re2
.s
.code
== RING_ENTRY_CODE_MORE
);
448 /* Some other error, discard it. */
449 dev_kfree_skb_any(skb
);
450 /* Error statistics are accumulated in
451 * octeon_mgmt_update_rx_stats.
456 /* Discard the whole mess. */
457 dev_kfree_skb_any(skb
);
458 dev_kfree_skb_any(skb2
);
459 while (re2
.s
.code
== RING_ENTRY_CODE_MORE
) {
460 re2
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb2
);
461 dev_kfree_skb_any(skb2
);
463 netdev
->stats
.rx_errors
++;
466 /* Tell the hardware we processed a packet. */
468 mix_ircnt
.s
.ircnt
= 1;
469 cvmx_write_csr(p
->mix
+ MIX_IRCNT
, mix_ircnt
.u64
);
473 static int octeon_mgmt_receive_packets(struct octeon_mgmt
*p
, int budget
)
475 unsigned int work_done
= 0;
476 union cvmx_mixx_ircnt mix_ircnt
;
479 mix_ircnt
.u64
= cvmx_read_csr(p
->mix
+ MIX_IRCNT
);
480 while (work_done
< budget
&& mix_ircnt
.s
.ircnt
) {
482 rc
= octeon_mgmt_receive_one(p
);
486 /* Check for more packets. */
487 mix_ircnt
.u64
= cvmx_read_csr(p
->mix
+ MIX_IRCNT
);
490 octeon_mgmt_rx_fill_ring(p
->netdev
);
495 static int octeon_mgmt_napi_poll(struct napi_struct
*napi
, int budget
)
497 struct octeon_mgmt
*p
= container_of(napi
, struct octeon_mgmt
, napi
);
498 struct net_device
*netdev
= p
->netdev
;
499 unsigned int work_done
= 0;
501 work_done
= octeon_mgmt_receive_packets(p
, budget
);
503 if (work_done
< budget
) {
504 /* We stopped because no more packets were available. */
506 octeon_mgmt_enable_rx_irq(p
);
508 octeon_mgmt_update_rx_stats(netdev
);
513 /* Reset the hardware to clean state. */
514 static void octeon_mgmt_reset_hw(struct octeon_mgmt
*p
)
516 union cvmx_mixx_ctl mix_ctl
;
517 union cvmx_mixx_bist mix_bist
;
518 union cvmx_agl_gmx_bist agl_gmx_bist
;
521 cvmx_write_csr(p
->mix
+ MIX_CTL
, mix_ctl
.u64
);
523 mix_ctl
.u64
= cvmx_read_csr(p
->mix
+ MIX_CTL
);
524 } while (mix_ctl
.s
.busy
);
526 cvmx_write_csr(p
->mix
+ MIX_CTL
, mix_ctl
.u64
);
527 cvmx_read_csr(p
->mix
+ MIX_CTL
);
528 octeon_io_clk_delay(64);
530 mix_bist
.u64
= cvmx_read_csr(p
->mix
+ MIX_BIST
);
532 dev_warn(p
->dev
, "MIX failed BIST (0x%016llx)\n",
533 (unsigned long long)mix_bist
.u64
);
535 agl_gmx_bist
.u64
= cvmx_read_csr(CVMX_AGL_GMX_BIST
);
536 if (agl_gmx_bist
.u64
)
537 dev_warn(p
->dev
, "AGL failed BIST (0x%016llx)\n",
538 (unsigned long long)agl_gmx_bist
.u64
);
541 struct octeon_mgmt_cam_state
{
547 static void octeon_mgmt_cam_state_add(struct octeon_mgmt_cam_state
*cs
,
552 for (i
= 0; i
< 6; i
++)
553 cs
->cam
[i
] |= (u64
)addr
[i
] << (8 * (cs
->cam_index
));
554 cs
->cam_mask
|= (1ULL << cs
->cam_index
);
558 static void octeon_mgmt_set_rx_filtering(struct net_device
*netdev
)
560 struct octeon_mgmt
*p
= netdev_priv(netdev
);
561 union cvmx_agl_gmx_rxx_adr_ctl adr_ctl
;
562 union cvmx_agl_gmx_prtx_cfg agl_gmx_prtx
;
564 unsigned int prev_packet_enable
;
565 unsigned int cam_mode
= 1; /* 1 - Accept on CAM match */
566 unsigned int multicast_mode
= 1; /* 1 - Reject all multicast. */
567 struct octeon_mgmt_cam_state cam_state
;
568 struct netdev_hw_addr
*ha
;
569 int available_cam_entries
;
571 memset(&cam_state
, 0, sizeof(cam_state
));
573 if ((netdev
->flags
& IFF_PROMISC
) || netdev
->uc
.count
> 7) {
575 available_cam_entries
= 8;
577 /* One CAM entry for the primary address, leaves seven
578 * for the secondary addresses.
580 available_cam_entries
= 7 - netdev
->uc
.count
;
583 if (netdev
->flags
& IFF_MULTICAST
) {
584 if (cam_mode
== 0 || (netdev
->flags
& IFF_ALLMULTI
) ||
585 netdev_mc_count(netdev
) > available_cam_entries
)
586 multicast_mode
= 2; /* 2 - Accept all multicast. */
588 multicast_mode
= 0; /* 0 - Use CAM. */
592 /* Add primary address. */
593 octeon_mgmt_cam_state_add(&cam_state
, netdev
->dev_addr
);
594 netdev_for_each_uc_addr(ha
, netdev
)
595 octeon_mgmt_cam_state_add(&cam_state
, ha
->addr
);
597 if (multicast_mode
== 0) {
598 netdev_for_each_mc_addr(ha
, netdev
)
599 octeon_mgmt_cam_state_add(&cam_state
, ha
->addr
);
602 spin_lock_irqsave(&p
->lock
, flags
);
604 /* Disable packet I/O. */
605 agl_gmx_prtx
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_PRT_CFG
);
606 prev_packet_enable
= agl_gmx_prtx
.s
.en
;
607 agl_gmx_prtx
.s
.en
= 0;
608 cvmx_write_csr(p
->agl
+ AGL_GMX_PRT_CFG
, agl_gmx_prtx
.u64
);
611 adr_ctl
.s
.cam_mode
= cam_mode
;
612 adr_ctl
.s
.mcst
= multicast_mode
;
613 adr_ctl
.s
.bcst
= 1; /* Allow broadcast */
615 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_ADR_CTL
, adr_ctl
.u64
);
617 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_ADR_CAM0
, cam_state
.cam
[0]);
618 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_ADR_CAM1
, cam_state
.cam
[1]);
619 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_ADR_CAM2
, cam_state
.cam
[2]);
620 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_ADR_CAM3
, cam_state
.cam
[3]);
621 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_ADR_CAM4
, cam_state
.cam
[4]);
622 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_ADR_CAM5
, cam_state
.cam
[5]);
623 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_ADR_CAM_EN
, cam_state
.cam_mask
);
625 /* Restore packet I/O. */
626 agl_gmx_prtx
.s
.en
= prev_packet_enable
;
627 cvmx_write_csr(p
->agl
+ AGL_GMX_PRT_CFG
, agl_gmx_prtx
.u64
);
629 spin_unlock_irqrestore(&p
->lock
, flags
);
632 static int octeon_mgmt_set_mac_address(struct net_device
*netdev
, void *addr
)
634 int r
= eth_mac_addr(netdev
, addr
);
639 octeon_mgmt_set_rx_filtering(netdev
);
644 static int octeon_mgmt_change_mtu(struct net_device
*netdev
, int new_mtu
)
646 struct octeon_mgmt
*p
= netdev_priv(netdev
);
647 int size_without_fcs
= new_mtu
+ OCTEON_MGMT_RX_HEADROOM
;
649 /* Limit the MTU to make sure the ethernet packets are between
650 * 64 bytes and 16383 bytes.
652 if (size_without_fcs
< 64 || size_without_fcs
> 16383) {
653 dev_warn(p
->dev
, "MTU must be between %d and %d.\n",
654 64 - OCTEON_MGMT_RX_HEADROOM
,
655 16383 - OCTEON_MGMT_RX_HEADROOM
);
659 netdev
->mtu
= new_mtu
;
661 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_FRM_MAX
, size_without_fcs
);
662 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_JABBER
,
663 (size_without_fcs
+ 7) & 0xfff8);
668 static irqreturn_t
octeon_mgmt_interrupt(int cpl
, void *dev_id
)
670 struct net_device
*netdev
= dev_id
;
671 struct octeon_mgmt
*p
= netdev_priv(netdev
);
672 union cvmx_mixx_isr mixx_isr
;
674 mixx_isr
.u64
= cvmx_read_csr(p
->mix
+ MIX_ISR
);
676 /* Clear any pending interrupts */
677 cvmx_write_csr(p
->mix
+ MIX_ISR
, mixx_isr
.u64
);
678 cvmx_read_csr(p
->mix
+ MIX_ISR
);
680 if (mixx_isr
.s
.irthresh
) {
681 octeon_mgmt_disable_rx_irq(p
);
682 napi_schedule(&p
->napi
);
684 if (mixx_isr
.s
.orthresh
) {
685 octeon_mgmt_disable_tx_irq(p
);
686 tasklet_schedule(&p
->tx_clean_tasklet
);
692 static int octeon_mgmt_ioctl_hwtstamp(struct net_device
*netdev
,
693 struct ifreq
*rq
, int cmd
)
695 struct octeon_mgmt
*p
= netdev_priv(netdev
);
696 struct hwtstamp_config config
;
697 union cvmx_mio_ptp_clock_cfg ptp
;
698 union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl
;
699 bool have_hw_timestamps
= false;
701 if (copy_from_user(&config
, rq
->ifr_data
, sizeof(config
)))
704 if (config
.flags
) /* reserved for future extensions */
707 /* Check the status of hardware for tiemstamps */
708 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
709 /* Get the current state of the PTP clock */
710 ptp
.u64
= cvmx_read_csr(CVMX_MIO_PTP_CLOCK_CFG
);
711 if (!ptp
.s
.ext_clk_en
) {
712 /* The clock has not been configured to use an
713 * external source. Program it to use the main clock
716 u64 clock_comp
= (NSEC_PER_SEC
<< 32) / octeon_get_io_clock_rate();
718 cvmx_write_csr(CVMX_MIO_PTP_CLOCK_COMP
, clock_comp
);
719 pr_info("PTP Clock: Using sclk reference at %lld Hz\n",
720 (NSEC_PER_SEC
<< 32) / clock_comp
);
722 /* The clock is already programmed to use a GPIO */
723 u64 clock_comp
= cvmx_read_csr(CVMX_MIO_PTP_CLOCK_COMP
);
724 pr_info("PTP Clock: Using GPIO %d at %lld Hz\n",
726 (NSEC_PER_SEC
<< 32) / clock_comp
);
729 /* Enable the clock if it wasn't done already */
732 cvmx_write_csr(CVMX_MIO_PTP_CLOCK_CFG
, ptp
.u64
);
734 have_hw_timestamps
= true;
737 if (!have_hw_timestamps
)
740 switch (config
.tx_type
) {
741 case HWTSTAMP_TX_OFF
:
748 switch (config
.rx_filter
) {
749 case HWTSTAMP_FILTER_NONE
:
750 p
->has_rx_tstamp
= false;
751 rxx_frm_ctl
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_RX_FRM_CTL
);
752 rxx_frm_ctl
.s
.ptp_mode
= 0;
753 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_FRM_CTL
, rxx_frm_ctl
.u64
);
755 case HWTSTAMP_FILTER_ALL
:
756 case HWTSTAMP_FILTER_SOME
:
757 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT
:
758 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC
:
759 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ
:
760 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT
:
761 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC
:
762 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ
:
763 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT
:
764 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC
:
765 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ
:
766 case HWTSTAMP_FILTER_PTP_V2_EVENT
:
767 case HWTSTAMP_FILTER_PTP_V2_SYNC
:
768 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ
:
769 p
->has_rx_tstamp
= have_hw_timestamps
;
770 config
.rx_filter
= HWTSTAMP_FILTER_ALL
;
771 if (p
->has_rx_tstamp
) {
772 rxx_frm_ctl
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_RX_FRM_CTL
);
773 rxx_frm_ctl
.s
.ptp_mode
= 1;
774 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_FRM_CTL
, rxx_frm_ctl
.u64
);
781 if (copy_to_user(rq
->ifr_data
, &config
, sizeof(config
)))
787 static int octeon_mgmt_ioctl(struct net_device
*netdev
,
788 struct ifreq
*rq
, int cmd
)
790 struct octeon_mgmt
*p
= netdev_priv(netdev
);
794 return octeon_mgmt_ioctl_hwtstamp(netdev
, rq
, cmd
);
797 return phy_mii_ioctl(p
->phydev
, rq
, cmd
);
802 static void octeon_mgmt_disable_link(struct octeon_mgmt
*p
)
804 union cvmx_agl_gmx_prtx_cfg prtx_cfg
;
806 /* Disable GMX before we make any changes. */
807 prtx_cfg
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_PRT_CFG
);
809 prtx_cfg
.s
.tx_en
= 0;
810 prtx_cfg
.s
.rx_en
= 0;
811 cvmx_write_csr(p
->agl
+ AGL_GMX_PRT_CFG
, prtx_cfg
.u64
);
813 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
815 for (i
= 0; i
< 10; i
++) {
816 prtx_cfg
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_PRT_CFG
);
817 if (prtx_cfg
.s
.tx_idle
== 1 || prtx_cfg
.s
.rx_idle
== 1)
825 static void octeon_mgmt_enable_link(struct octeon_mgmt
*p
)
827 union cvmx_agl_gmx_prtx_cfg prtx_cfg
;
829 /* Restore the GMX enable state only if link is set */
830 prtx_cfg
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_PRT_CFG
);
831 prtx_cfg
.s
.tx_en
= 1;
832 prtx_cfg
.s
.rx_en
= 1;
834 cvmx_write_csr(p
->agl
+ AGL_GMX_PRT_CFG
, prtx_cfg
.u64
);
837 static void octeon_mgmt_update_link(struct octeon_mgmt
*p
)
839 union cvmx_agl_gmx_prtx_cfg prtx_cfg
;
841 prtx_cfg
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_PRT_CFG
);
843 if (!p
->phydev
->link
)
844 prtx_cfg
.s
.duplex
= 1;
846 prtx_cfg
.s
.duplex
= p
->phydev
->duplex
;
848 switch (p
->phydev
->speed
) {
850 prtx_cfg
.s
.speed
= 0;
851 prtx_cfg
.s
.slottime
= 0;
853 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
854 prtx_cfg
.s
.burst
= 1;
855 prtx_cfg
.s
.speed_msb
= 1;
859 prtx_cfg
.s
.speed
= 0;
860 prtx_cfg
.s
.slottime
= 0;
862 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
863 prtx_cfg
.s
.burst
= 1;
864 prtx_cfg
.s
.speed_msb
= 0;
868 /* 1000 MBits is only supported on 6XXX chips */
869 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
870 prtx_cfg
.s
.speed
= 1;
871 prtx_cfg
.s
.speed_msb
= 0;
872 /* Only matters for half-duplex */
873 prtx_cfg
.s
.slottime
= 1;
874 prtx_cfg
.s
.burst
= p
->phydev
->duplex
;
877 case 0: /* No link */
882 /* Write the new GMX setting with the port still disabled. */
883 cvmx_write_csr(p
->agl
+ AGL_GMX_PRT_CFG
, prtx_cfg
.u64
);
885 /* Read GMX CFG again to make sure the config is completed. */
886 prtx_cfg
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_PRT_CFG
);
888 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
)) {
889 union cvmx_agl_gmx_txx_clk agl_clk
;
890 union cvmx_agl_prtx_ctl prtx_ctl
;
892 prtx_ctl
.u64
= cvmx_read_csr(p
->agl_prt_ctl
);
893 agl_clk
.u64
= cvmx_read_csr(p
->agl
+ AGL_GMX_TX_CLK
);
894 /* MII (both speeds) and RGMII 1000 speed. */
895 agl_clk
.s
.clk_cnt
= 1;
896 if (prtx_ctl
.s
.mode
== 0) { /* RGMII mode */
897 if (p
->phydev
->speed
== 10)
898 agl_clk
.s
.clk_cnt
= 50;
899 else if (p
->phydev
->speed
== 100)
900 agl_clk
.s
.clk_cnt
= 5;
902 cvmx_write_csr(p
->agl
+ AGL_GMX_TX_CLK
, agl_clk
.u64
);
906 static void octeon_mgmt_adjust_link(struct net_device
*netdev
)
908 struct octeon_mgmt
*p
= netdev_priv(netdev
);
910 int link_changed
= 0;
915 spin_lock_irqsave(&p
->lock
, flags
);
918 if (!p
->phydev
->link
&& p
->last_link
)
922 && (p
->last_duplex
!= p
->phydev
->duplex
923 || p
->last_link
!= p
->phydev
->link
924 || p
->last_speed
!= p
->phydev
->speed
)) {
925 octeon_mgmt_disable_link(p
);
927 octeon_mgmt_update_link(p
);
928 octeon_mgmt_enable_link(p
);
931 p
->last_link
= p
->phydev
->link
;
932 p
->last_speed
= p
->phydev
->speed
;
933 p
->last_duplex
= p
->phydev
->duplex
;
935 spin_unlock_irqrestore(&p
->lock
, flags
);
937 if (link_changed
!= 0) {
938 if (link_changed
> 0) {
939 pr_info("%s: Link is up - %d/%s\n", netdev
->name
,
941 DUPLEX_FULL
== p
->phydev
->duplex
?
944 pr_info("%s: Link is down\n", netdev
->name
);
949 static int octeon_mgmt_init_phy(struct net_device
*netdev
)
951 struct octeon_mgmt
*p
= netdev_priv(netdev
);
953 if (octeon_is_simulation() || p
->phy_np
== NULL
) {
954 /* No PHYs in the simulator. */
955 netif_carrier_on(netdev
);
959 p
->phydev
= of_phy_connect(netdev
, p
->phy_np
,
960 octeon_mgmt_adjust_link
, 0,
961 PHY_INTERFACE_MODE_MII
);
969 static int octeon_mgmt_open(struct net_device
*netdev
)
971 struct octeon_mgmt
*p
= netdev_priv(netdev
);
972 union cvmx_mixx_ctl mix_ctl
;
973 union cvmx_agl_gmx_inf_mode agl_gmx_inf_mode
;
974 union cvmx_mixx_oring1 oring1
;
975 union cvmx_mixx_iring1 iring1
;
976 union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl
;
977 union cvmx_mixx_irhwm mix_irhwm
;
978 union cvmx_mixx_orhwm mix_orhwm
;
979 union cvmx_mixx_intena mix_intena
;
982 /* Allocate ring buffers. */
983 p
->tx_ring
= kzalloc(ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
988 dma_map_single(p
->dev
, p
->tx_ring
,
989 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
992 p
->tx_next_clean
= 0;
993 p
->tx_current_fill
= 0;
996 p
->rx_ring
= kzalloc(ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
1001 dma_map_single(p
->dev
, p
->rx_ring
,
1002 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
1006 p
->rx_next_fill
= 0;
1007 p
->rx_current_fill
= 0;
1009 octeon_mgmt_reset_hw(p
);
1011 mix_ctl
.u64
= cvmx_read_csr(p
->mix
+ MIX_CTL
);
1013 /* Bring it out of reset if needed. */
1014 if (mix_ctl
.s
.reset
) {
1015 mix_ctl
.s
.reset
= 0;
1016 cvmx_write_csr(p
->mix
+ MIX_CTL
, mix_ctl
.u64
);
1018 mix_ctl
.u64
= cvmx_read_csr(p
->mix
+ MIX_CTL
);
1019 } while (mix_ctl
.s
.reset
);
1022 if (OCTEON_IS_MODEL(OCTEON_CN5XXX
)) {
1023 agl_gmx_inf_mode
.u64
= 0;
1024 agl_gmx_inf_mode
.s
.en
= 1;
1025 cvmx_write_csr(CVMX_AGL_GMX_INF_MODE
, agl_gmx_inf_mode
.u64
);
1027 if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X
)
1028 || OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X
)) {
1029 /* Force compensation values, as they are not
1030 * determined properly by HW
1032 union cvmx_agl_gmx_drv_ctl drv_ctl
;
1034 drv_ctl
.u64
= cvmx_read_csr(CVMX_AGL_GMX_DRV_CTL
);
1036 drv_ctl
.s
.byp_en1
= 1;
1037 drv_ctl
.s
.nctl1
= 6;
1038 drv_ctl
.s
.pctl1
= 6;
1040 drv_ctl
.s
.byp_en
= 1;
1044 cvmx_write_csr(CVMX_AGL_GMX_DRV_CTL
, drv_ctl
.u64
);
1048 oring1
.s
.obase
= p
->tx_ring_handle
>> 3;
1049 oring1
.s
.osize
= OCTEON_MGMT_TX_RING_SIZE
;
1050 cvmx_write_csr(p
->mix
+ MIX_ORING1
, oring1
.u64
);
1053 iring1
.s
.ibase
= p
->rx_ring_handle
>> 3;
1054 iring1
.s
.isize
= OCTEON_MGMT_RX_RING_SIZE
;
1055 cvmx_write_csr(p
->mix
+ MIX_IRING1
, iring1
.u64
);
1057 memcpy(sa
.sa_data
, netdev
->dev_addr
, ETH_ALEN
);
1058 octeon_mgmt_set_mac_address(netdev
, &sa
);
1060 octeon_mgmt_change_mtu(netdev
, netdev
->mtu
);
1062 /* Enable the port HW. Packets are not allowed until
1063 * cvmx_mgmt_port_enable() is called.
1066 mix_ctl
.s
.crc_strip
= 1; /* Strip the ending CRC */
1067 mix_ctl
.s
.en
= 1; /* Enable the port */
1068 mix_ctl
.s
.nbtarb
= 0; /* Arbitration mode */
1069 /* MII CB-request FIFO programmable high watermark */
1070 mix_ctl
.s
.mrq_hwm
= 1;
1071 #ifdef __LITTLE_ENDIAN
1072 mix_ctl
.s
.lendian
= 1;
1074 cvmx_write_csr(p
->mix
+ MIX_CTL
, mix_ctl
.u64
);
1076 /* Read the PHY to find the mode of the interface. */
1077 if (octeon_mgmt_init_phy(netdev
)) {
1078 dev_err(p
->dev
, "Cannot initialize PHY on MIX%d.\n", p
->port
);
1082 /* Set the mode of the interface, RGMII/MII. */
1083 if (OCTEON_IS_MODEL(OCTEON_CN6XXX
) && p
->phydev
) {
1084 union cvmx_agl_prtx_ctl agl_prtx_ctl
;
1085 int rgmii_mode
= (p
->phydev
->supported
&
1086 (SUPPORTED_1000baseT_Half
| SUPPORTED_1000baseT_Full
)) != 0;
1088 agl_prtx_ctl
.u64
= cvmx_read_csr(p
->agl_prt_ctl
);
1089 agl_prtx_ctl
.s
.mode
= rgmii_mode
? 0 : 1;
1090 cvmx_write_csr(p
->agl_prt_ctl
, agl_prtx_ctl
.u64
);
1092 /* MII clocks counts are based on the 125Mhz
1093 * reference, which has an 8nS period. So our delays
1094 * need to be multiplied by this factor.
1096 #define NS_PER_PHY_CLK 8
1098 /* Take the DLL and clock tree out of reset */
1099 agl_prtx_ctl
.u64
= cvmx_read_csr(p
->agl_prt_ctl
);
1100 agl_prtx_ctl
.s
.clkrst
= 0;
1102 agl_prtx_ctl
.s
.dllrst
= 0;
1103 agl_prtx_ctl
.s
.clktx_byp
= 0;
1105 cvmx_write_csr(p
->agl_prt_ctl
, agl_prtx_ctl
.u64
);
1106 cvmx_read_csr(p
->agl_prt_ctl
); /* Force write out before wait */
1108 /* Wait for the DLL to lock. External 125 MHz
1109 * reference clock must be stable at this point.
1111 ndelay(256 * NS_PER_PHY_CLK
);
1113 /* Enable the interface */
1114 agl_prtx_ctl
.u64
= cvmx_read_csr(p
->agl_prt_ctl
);
1115 agl_prtx_ctl
.s
.enable
= 1;
1116 cvmx_write_csr(p
->agl_prt_ctl
, agl_prtx_ctl
.u64
);
1118 /* Read the value back to force the previous write */
1119 agl_prtx_ctl
.u64
= cvmx_read_csr(p
->agl_prt_ctl
);
1121 /* Enable the compensation controller */
1122 agl_prtx_ctl
.s
.comp
= 1;
1123 agl_prtx_ctl
.s
.drv_byp
= 0;
1124 cvmx_write_csr(p
->agl_prt_ctl
, agl_prtx_ctl
.u64
);
1125 /* Force write out before wait. */
1126 cvmx_read_csr(p
->agl_prt_ctl
);
1128 /* For compensation state to lock. */
1129 ndelay(1040 * NS_PER_PHY_CLK
);
1131 /* Default Interframe Gaps are too small. Recommended
1134 * AGL_GMX_TX_IFG[IFG1]=14
1135 * AGL_GMX_TX_IFG[IFG2]=10
1137 cvmx_write_csr(CVMX_AGL_GMX_TX_IFG
, 0xae);
1140 octeon_mgmt_rx_fill_ring(netdev
);
1142 /* Clear statistics. */
1143 /* Clear on read. */
1144 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_STATS_CTL
, 1);
1145 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_STATS_PKTS_DRP
, 0);
1146 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_STATS_PKTS_BAD
, 0);
1148 cvmx_write_csr(p
->agl
+ AGL_GMX_TX_STATS_CTL
, 1);
1149 cvmx_write_csr(p
->agl
+ AGL_GMX_TX_STAT0
, 0);
1150 cvmx_write_csr(p
->agl
+ AGL_GMX_TX_STAT1
, 0);
1152 /* Clear any pending interrupts */
1153 cvmx_write_csr(p
->mix
+ MIX_ISR
, cvmx_read_csr(p
->mix
+ MIX_ISR
));
1155 if (request_irq(p
->irq
, octeon_mgmt_interrupt
, 0, netdev
->name
,
1157 dev_err(p
->dev
, "request_irq(%d) failed.\n", p
->irq
);
1161 /* Interrupt every single RX packet */
1163 mix_irhwm
.s
.irhwm
= 0;
1164 cvmx_write_csr(p
->mix
+ MIX_IRHWM
, mix_irhwm
.u64
);
1166 /* Interrupt when we have 1 or more packets to clean. */
1168 mix_orhwm
.s
.orhwm
= 0;
1169 cvmx_write_csr(p
->mix
+ MIX_ORHWM
, mix_orhwm
.u64
);
1171 /* Enable receive and transmit interrupts */
1173 mix_intena
.s
.ithena
= 1;
1174 mix_intena
.s
.othena
= 1;
1175 cvmx_write_csr(p
->mix
+ MIX_INTENA
, mix_intena
.u64
);
1177 /* Enable packet I/O. */
1179 rxx_frm_ctl
.u64
= 0;
1180 rxx_frm_ctl
.s
.ptp_mode
= p
->has_rx_tstamp
? 1 : 0;
1181 rxx_frm_ctl
.s
.pre_align
= 1;
1182 /* When set, disables the length check for non-min sized pkts
1183 * with padding in the client data.
1185 rxx_frm_ctl
.s
.pad_len
= 1;
1186 /* When set, disables the length check for VLAN pkts */
1187 rxx_frm_ctl
.s
.vlan_len
= 1;
1188 /* When set, PREAMBLE checking is less strict */
1189 rxx_frm_ctl
.s
.pre_free
= 1;
1190 /* Control Pause Frames can match station SMAC */
1191 rxx_frm_ctl
.s
.ctl_smac
= 0;
1192 /* Control Pause Frames can match globally assign Multicast address */
1193 rxx_frm_ctl
.s
.ctl_mcst
= 1;
1194 /* Forward pause information to TX block */
1195 rxx_frm_ctl
.s
.ctl_bck
= 1;
1196 /* Drop Control Pause Frames */
1197 rxx_frm_ctl
.s
.ctl_drp
= 1;
1198 /* Strip off the preamble */
1199 rxx_frm_ctl
.s
.pre_strp
= 1;
1200 /* This port is configured to send PREAMBLE+SFD to begin every
1201 * frame. GMX checks that the PREAMBLE is sent correctly.
1203 rxx_frm_ctl
.s
.pre_chk
= 1;
1204 cvmx_write_csr(p
->agl
+ AGL_GMX_RX_FRM_CTL
, rxx_frm_ctl
.u64
);
1206 /* Configure the port duplex, speed and enables */
1207 octeon_mgmt_disable_link(p
);
1209 octeon_mgmt_update_link(p
);
1210 octeon_mgmt_enable_link(p
);
1214 /* PHY is not present in simulator. The carrier is enabled
1215 * while initializing the phy for simulator, leave it enabled.
1218 netif_carrier_off(netdev
);
1219 phy_start_aneg(p
->phydev
);
1222 netif_wake_queue(netdev
);
1223 napi_enable(&p
->napi
);
1227 octeon_mgmt_reset_hw(p
);
1228 dma_unmap_single(p
->dev
, p
->rx_ring_handle
,
1229 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
1233 dma_unmap_single(p
->dev
, p
->tx_ring_handle
,
1234 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
1240 static int octeon_mgmt_stop(struct net_device
*netdev
)
1242 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1244 napi_disable(&p
->napi
);
1245 netif_stop_queue(netdev
);
1248 phy_disconnect(p
->phydev
);
1251 netif_carrier_off(netdev
);
1253 octeon_mgmt_reset_hw(p
);
1255 free_irq(p
->irq
, netdev
);
1257 /* dma_unmap is a nop on Octeon, so just free everything. */
1258 skb_queue_purge(&p
->tx_list
);
1259 skb_queue_purge(&p
->rx_list
);
1261 dma_unmap_single(p
->dev
, p
->rx_ring_handle
,
1262 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
1266 dma_unmap_single(p
->dev
, p
->tx_ring_handle
,
1267 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
1274 static int octeon_mgmt_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
1276 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1277 union mgmt_port_ring_entry re
;
1278 unsigned long flags
;
1279 int rv
= NETDEV_TX_BUSY
;
1282 re
.s
.tstamp
= ((skb_shinfo(skb
)->tx_flags
& SKBTX_HW_TSTAMP
) != 0);
1283 re
.s
.len
= skb
->len
;
1284 re
.s
.addr
= dma_map_single(p
->dev
, skb
->data
,
1288 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
1290 if (unlikely(p
->tx_current_fill
>= ring_max_fill(OCTEON_MGMT_TX_RING_SIZE
) - 1)) {
1291 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
1292 netif_stop_queue(netdev
);
1293 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
1296 if (unlikely(p
->tx_current_fill
>=
1297 ring_max_fill(OCTEON_MGMT_TX_RING_SIZE
))) {
1298 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
1299 dma_unmap_single(p
->dev
, re
.s
.addr
, re
.s
.len
,
1304 __skb_queue_tail(&p
->tx_list
, skb
);
1306 /* Put it in the ring. */
1307 p
->tx_ring
[p
->tx_next
] = re
.d64
;
1308 p
->tx_next
= (p
->tx_next
+ 1) % OCTEON_MGMT_TX_RING_SIZE
;
1309 p
->tx_current_fill
++;
1311 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
1313 dma_sync_single_for_device(p
->dev
, p
->tx_ring_handle
,
1314 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
1317 netdev
->stats
.tx_packets
++;
1318 netdev
->stats
.tx_bytes
+= skb
->len
;
1320 /* Ring the bell. */
1321 cvmx_write_csr(p
->mix
+ MIX_ORING2
, 1);
1323 netdev
->trans_start
= jiffies
;
1326 octeon_mgmt_update_tx_stats(netdev
);
1330 #ifdef CONFIG_NET_POLL_CONTROLLER
1331 static void octeon_mgmt_poll_controller(struct net_device
*netdev
)
1333 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1335 octeon_mgmt_receive_packets(p
, 16);
1336 octeon_mgmt_update_rx_stats(netdev
);
1340 static void octeon_mgmt_get_drvinfo(struct net_device
*netdev
,
1341 struct ethtool_drvinfo
*info
)
1343 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
1344 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
1345 strlcpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
1346 strlcpy(info
->bus_info
, "N/A", sizeof(info
->bus_info
));
1349 static int octeon_mgmt_get_settings(struct net_device
*netdev
,
1350 struct ethtool_cmd
*cmd
)
1352 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1355 return phy_ethtool_gset(p
->phydev
, cmd
);
1360 static int octeon_mgmt_set_settings(struct net_device
*netdev
,
1361 struct ethtool_cmd
*cmd
)
1363 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1365 if (!capable(CAP_NET_ADMIN
))
1369 return phy_ethtool_sset(p
->phydev
, cmd
);
1374 static int octeon_mgmt_nway_reset(struct net_device
*dev
)
1376 struct octeon_mgmt
*p
= netdev_priv(dev
);
1378 if (!capable(CAP_NET_ADMIN
))
1382 return phy_start_aneg(p
->phydev
);
1387 static const struct ethtool_ops octeon_mgmt_ethtool_ops
= {
1388 .get_drvinfo
= octeon_mgmt_get_drvinfo
,
1389 .get_settings
= octeon_mgmt_get_settings
,
1390 .set_settings
= octeon_mgmt_set_settings
,
1391 .nway_reset
= octeon_mgmt_nway_reset
,
1392 .get_link
= ethtool_op_get_link
,
1395 static const struct net_device_ops octeon_mgmt_ops
= {
1396 .ndo_open
= octeon_mgmt_open
,
1397 .ndo_stop
= octeon_mgmt_stop
,
1398 .ndo_start_xmit
= octeon_mgmt_xmit
,
1399 .ndo_set_rx_mode
= octeon_mgmt_set_rx_filtering
,
1400 .ndo_set_mac_address
= octeon_mgmt_set_mac_address
,
1401 .ndo_do_ioctl
= octeon_mgmt_ioctl
,
1402 .ndo_change_mtu
= octeon_mgmt_change_mtu
,
1403 #ifdef CONFIG_NET_POLL_CONTROLLER
1404 .ndo_poll_controller
= octeon_mgmt_poll_controller
,
1408 static int octeon_mgmt_probe(struct platform_device
*pdev
)
1410 struct net_device
*netdev
;
1411 struct octeon_mgmt
*p
;
1414 struct resource
*res_mix
;
1415 struct resource
*res_agl
;
1416 struct resource
*res_agl_prt_ctl
;
1420 netdev
= alloc_etherdev(sizeof(struct octeon_mgmt
));
1424 SET_NETDEV_DEV(netdev
, &pdev
->dev
);
1426 platform_set_drvdata(pdev
, netdev
);
1427 p
= netdev_priv(netdev
);
1428 netif_napi_add(netdev
, &p
->napi
, octeon_mgmt_napi_poll
,
1429 OCTEON_MGMT_NAPI_WEIGHT
);
1432 p
->dev
= &pdev
->dev
;
1433 p
->has_rx_tstamp
= false;
1435 data
= of_get_property(pdev
->dev
.of_node
, "cell-index", &len
);
1436 if (data
&& len
== sizeof(*data
)) {
1437 p
->port
= be32_to_cpup(data
);
1439 dev_err(&pdev
->dev
, "no 'cell-index' property\n");
1444 snprintf(netdev
->name
, IFNAMSIZ
, "mgmt%d", p
->port
);
1446 result
= platform_get_irq(pdev
, 0);
1452 res_mix
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1453 if (res_mix
== NULL
) {
1454 dev_err(&pdev
->dev
, "no 'reg' resource\n");
1459 res_agl
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1460 if (res_agl
== NULL
) {
1461 dev_err(&pdev
->dev
, "no 'reg' resource\n");
1466 res_agl_prt_ctl
= platform_get_resource(pdev
, IORESOURCE_MEM
, 3);
1467 if (res_agl_prt_ctl
== NULL
) {
1468 dev_err(&pdev
->dev
, "no 'reg' resource\n");
1473 p
->mix_phys
= res_mix
->start
;
1474 p
->mix_size
= resource_size(res_mix
);
1475 p
->agl_phys
= res_agl
->start
;
1476 p
->agl_size
= resource_size(res_agl
);
1477 p
->agl_prt_ctl_phys
= res_agl_prt_ctl
->start
;
1478 p
->agl_prt_ctl_size
= resource_size(res_agl_prt_ctl
);
1481 if (!devm_request_mem_region(&pdev
->dev
, p
->mix_phys
, p
->mix_size
,
1483 dev_err(&pdev
->dev
, "request_mem_region (%s) failed\n",
1489 if (!devm_request_mem_region(&pdev
->dev
, p
->agl_phys
, p
->agl_size
,
1492 dev_err(&pdev
->dev
, "request_mem_region (%s) failed\n",
1497 if (!devm_request_mem_region(&pdev
->dev
, p
->agl_prt_ctl_phys
,
1498 p
->agl_prt_ctl_size
, res_agl_prt_ctl
->name
)) {
1500 dev_err(&pdev
->dev
, "request_mem_region (%s) failed\n",
1501 res_agl_prt_ctl
->name
);
1505 p
->mix
= (u64
)devm_ioremap(&pdev
->dev
, p
->mix_phys
, p
->mix_size
);
1506 p
->agl
= (u64
)devm_ioremap(&pdev
->dev
, p
->agl_phys
, p
->agl_size
);
1507 p
->agl_prt_ctl
= (u64
)devm_ioremap(&pdev
->dev
, p
->agl_prt_ctl_phys
,
1508 p
->agl_prt_ctl_size
);
1509 spin_lock_init(&p
->lock
);
1511 skb_queue_head_init(&p
->tx_list
);
1512 skb_queue_head_init(&p
->rx_list
);
1513 tasklet_init(&p
->tx_clean_tasklet
,
1514 octeon_mgmt_clean_tx_tasklet
, (unsigned long)p
);
1516 netdev
->priv_flags
|= IFF_UNICAST_FLT
;
1518 netdev
->netdev_ops
= &octeon_mgmt_ops
;
1519 netdev
->ethtool_ops
= &octeon_mgmt_ethtool_ops
;
1521 mac
= of_get_mac_address(pdev
->dev
.of_node
);
1524 memcpy(netdev
->dev_addr
, mac
, ETH_ALEN
);
1526 eth_hw_addr_random(netdev
);
1528 p
->phy_np
= of_parse_phandle(pdev
->dev
.of_node
, "phy-handle", 0);
1530 result
= dma_coerce_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
1534 netif_carrier_off(netdev
);
1535 result
= register_netdev(netdev
);
1539 dev_info(&pdev
->dev
, "Version " DRV_VERSION
"\n");
1543 free_netdev(netdev
);
1547 static int octeon_mgmt_remove(struct platform_device
*pdev
)
1549 struct net_device
*netdev
= platform_get_drvdata(pdev
);
1551 unregister_netdev(netdev
);
1552 free_netdev(netdev
);
1556 static const struct of_device_id octeon_mgmt_match
[] = {
1558 .compatible
= "cavium,octeon-5750-mix",
1562 MODULE_DEVICE_TABLE(of
, octeon_mgmt_match
);
1564 static struct platform_driver octeon_mgmt_driver
= {
1566 .name
= "octeon_mgmt",
1567 .of_match_table
= octeon_mgmt_match
,
1569 .probe
= octeon_mgmt_probe
,
1570 .remove
= octeon_mgmt_remove
,
1573 extern void octeon_mdiobus_force_mod_depencency(void);
1575 static int __init
octeon_mgmt_mod_init(void)
1577 /* Force our mdiobus driver module to be loaded first. */
1578 octeon_mdiobus_force_mod_depencency();
1579 return platform_driver_register(&octeon_mgmt_driver
);
1582 static void __exit
octeon_mgmt_mod_exit(void)
1584 platform_driver_unregister(&octeon_mgmt_driver
);
1587 module_init(octeon_mgmt_mod_init
);
1588 module_exit(octeon_mgmt_mod_exit
);
1590 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
1591 MODULE_AUTHOR("David Daney");
1592 MODULE_LICENSE("GPL");
1593 MODULE_VERSION(DRV_VERSION
);