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 Cavium Networks
9 #include <linux/capability.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/interrupt.h>
14 #include <linux/platform_device.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
18 #include <linux/if_vlan.h>
19 #include <linux/slab.h>
20 #include <linux/phy.h>
21 #include <linux/spinlock.h>
23 #include <asm/octeon/octeon.h>
24 #include <asm/octeon/cvmx-mixx-defs.h>
25 #include <asm/octeon/cvmx-agl-defs.h>
27 #define DRV_NAME "octeon_mgmt"
28 #define DRV_VERSION "2.0"
29 #define DRV_DESCRIPTION \
30 "Cavium Networks Octeon MII (management) port Network Driver"
32 #define OCTEON_MGMT_NAPI_WEIGHT 16
35 * Ring sizes that are powers of two allow for more efficient modulo
38 #define OCTEON_MGMT_RX_RING_SIZE 512
39 #define OCTEON_MGMT_TX_RING_SIZE 128
41 /* Allow 8 bytes for vlan and FCS. */
42 #define OCTEON_MGMT_RX_HEADROOM (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN)
44 union mgmt_port_ring_entry
{
48 /* Length of the buffer/packet in bytes */
50 /* For TX, signals that the packet should be timestamped */
52 /* The RX error code */
54 #define RING_ENTRY_CODE_DONE 0xf
55 #define RING_ENTRY_CODE_MORE 0x10
56 /* Physical address of the buffer */
62 struct net_device
*netdev
;
66 dma_addr_t tx_ring_handle
;
68 unsigned int tx_next_clean
;
69 unsigned int tx_current_fill
;
70 /* The tx_list lock also protects the ring related variables */
71 struct sk_buff_head tx_list
;
73 /* RX variables only touched in napi_poll. No locking necessary. */
75 dma_addr_t rx_ring_handle
;
77 unsigned int rx_next_fill
;
78 unsigned int rx_current_fill
;
79 struct sk_buff_head rx_list
;
82 unsigned int last_duplex
;
83 unsigned int last_link
;
85 struct napi_struct napi
;
86 struct tasklet_struct tx_clean_tasklet
;
87 struct phy_device
*phydev
;
90 static void octeon_mgmt_set_rx_irq(struct octeon_mgmt
*p
, int enable
)
93 union cvmx_mixx_intena mix_intena
;
96 spin_lock_irqsave(&p
->lock
, flags
);
97 mix_intena
.u64
= cvmx_read_csr(CVMX_MIXX_INTENA(port
));
98 mix_intena
.s
.ithena
= enable
? 1 : 0;
99 cvmx_write_csr(CVMX_MIXX_INTENA(port
), mix_intena
.u64
);
100 spin_unlock_irqrestore(&p
->lock
, flags
);
103 static void octeon_mgmt_set_tx_irq(struct octeon_mgmt
*p
, int enable
)
106 union cvmx_mixx_intena mix_intena
;
109 spin_lock_irqsave(&p
->lock
, flags
);
110 mix_intena
.u64
= cvmx_read_csr(CVMX_MIXX_INTENA(port
));
111 mix_intena
.s
.othena
= enable
? 1 : 0;
112 cvmx_write_csr(CVMX_MIXX_INTENA(port
), mix_intena
.u64
);
113 spin_unlock_irqrestore(&p
->lock
, flags
);
116 static inline void octeon_mgmt_enable_rx_irq(struct octeon_mgmt
*p
)
118 octeon_mgmt_set_rx_irq(p
, 1);
121 static inline void octeon_mgmt_disable_rx_irq(struct octeon_mgmt
*p
)
123 octeon_mgmt_set_rx_irq(p
, 0);
126 static inline void octeon_mgmt_enable_tx_irq(struct octeon_mgmt
*p
)
128 octeon_mgmt_set_tx_irq(p
, 1);
131 static inline void octeon_mgmt_disable_tx_irq(struct octeon_mgmt
*p
)
133 octeon_mgmt_set_tx_irq(p
, 0);
136 static unsigned int ring_max_fill(unsigned int ring_size
)
138 return ring_size
- 8;
141 static unsigned int ring_size_to_bytes(unsigned int ring_size
)
143 return ring_size
* sizeof(union mgmt_port_ring_entry
);
146 static void octeon_mgmt_rx_fill_ring(struct net_device
*netdev
)
148 struct octeon_mgmt
*p
= netdev_priv(netdev
);
151 while (p
->rx_current_fill
< ring_max_fill(OCTEON_MGMT_RX_RING_SIZE
)) {
153 union mgmt_port_ring_entry re
;
156 /* CN56XX pass 1 needs 8 bytes of padding. */
157 size
= netdev
->mtu
+ OCTEON_MGMT_RX_HEADROOM
+ 8 + NET_IP_ALIGN
;
159 skb
= netdev_alloc_skb(netdev
, size
);
162 skb_reserve(skb
, NET_IP_ALIGN
);
163 __skb_queue_tail(&p
->rx_list
, skb
);
167 re
.s
.addr
= dma_map_single(p
->dev
, skb
->data
,
171 /* Put it in the ring. */
172 p
->rx_ring
[p
->rx_next_fill
] = re
.d64
;
173 dma_sync_single_for_device(p
->dev
, p
->rx_ring_handle
,
174 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
177 (p
->rx_next_fill
+ 1) % OCTEON_MGMT_RX_RING_SIZE
;
178 p
->rx_current_fill
++;
180 cvmx_write_csr(CVMX_MIXX_IRING2(port
), 1);
184 static void octeon_mgmt_clean_tx_buffers(struct octeon_mgmt
*p
)
187 union cvmx_mixx_orcnt mix_orcnt
;
188 union mgmt_port_ring_entry re
;
193 mix_orcnt
.u64
= cvmx_read_csr(CVMX_MIXX_ORCNT(port
));
194 while (mix_orcnt
.s
.orcnt
) {
195 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
197 mix_orcnt
.u64
= cvmx_read_csr(CVMX_MIXX_ORCNT(port
));
199 if (mix_orcnt
.s
.orcnt
== 0) {
200 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
204 dma_sync_single_for_cpu(p
->dev
, p
->tx_ring_handle
,
205 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
208 re
.d64
= p
->tx_ring
[p
->tx_next_clean
];
210 (p
->tx_next_clean
+ 1) % OCTEON_MGMT_TX_RING_SIZE
;
211 skb
= __skb_dequeue(&p
->tx_list
);
214 mix_orcnt
.s
.orcnt
= 1;
216 /* Acknowledge to hardware that we have the buffer. */
217 cvmx_write_csr(CVMX_MIXX_ORCNT(port
), mix_orcnt
.u64
);
218 p
->tx_current_fill
--;
220 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
222 dma_unmap_single(p
->dev
, re
.s
.addr
, re
.s
.len
,
224 dev_kfree_skb_any(skb
);
227 mix_orcnt
.u64
= cvmx_read_csr(CVMX_MIXX_ORCNT(port
));
230 if (cleaned
&& netif_queue_stopped(p
->netdev
))
231 netif_wake_queue(p
->netdev
);
234 static void octeon_mgmt_clean_tx_tasklet(unsigned long arg
)
236 struct octeon_mgmt
*p
= (struct octeon_mgmt
*)arg
;
237 octeon_mgmt_clean_tx_buffers(p
);
238 octeon_mgmt_enable_tx_irq(p
);
241 static void octeon_mgmt_update_rx_stats(struct net_device
*netdev
)
243 struct octeon_mgmt
*p
= netdev_priv(netdev
);
248 /* These reads also clear the count registers. */
249 drop
= cvmx_read_csr(CVMX_AGL_GMX_RXX_STATS_PKTS_DRP(port
));
250 bad
= cvmx_read_csr(CVMX_AGL_GMX_RXX_STATS_PKTS_BAD(port
));
253 /* Do an atomic update. */
254 spin_lock_irqsave(&p
->lock
, flags
);
255 netdev
->stats
.rx_errors
+= bad
;
256 netdev
->stats
.rx_dropped
+= drop
;
257 spin_unlock_irqrestore(&p
->lock
, flags
);
261 static void octeon_mgmt_update_tx_stats(struct net_device
*netdev
)
263 struct octeon_mgmt
*p
= netdev_priv(netdev
);
267 union cvmx_agl_gmx_txx_stat0 s0
;
268 union cvmx_agl_gmx_txx_stat1 s1
;
270 /* These reads also clear the count registers. */
271 s0
.u64
= cvmx_read_csr(CVMX_AGL_GMX_TXX_STAT0(port
));
272 s1
.u64
= cvmx_read_csr(CVMX_AGL_GMX_TXX_STAT1(port
));
274 if (s0
.s
.xsdef
|| s0
.s
.xscol
|| s1
.s
.scol
|| s1
.s
.mcol
) {
275 /* Do an atomic update. */
276 spin_lock_irqsave(&p
->lock
, flags
);
277 netdev
->stats
.tx_errors
+= s0
.s
.xsdef
+ s0
.s
.xscol
;
278 netdev
->stats
.collisions
+= s1
.s
.scol
+ s1
.s
.mcol
;
279 spin_unlock_irqrestore(&p
->lock
, flags
);
284 * Dequeue a receive skb and its corresponding ring entry. The ring
285 * entry is returned, *pskb is updated to point to the skb.
287 static u64
octeon_mgmt_dequeue_rx_buffer(struct octeon_mgmt
*p
,
288 struct sk_buff
**pskb
)
290 union mgmt_port_ring_entry re
;
292 dma_sync_single_for_cpu(p
->dev
, p
->rx_ring_handle
,
293 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
296 re
.d64
= p
->rx_ring
[p
->rx_next
];
297 p
->rx_next
= (p
->rx_next
+ 1) % OCTEON_MGMT_RX_RING_SIZE
;
298 p
->rx_current_fill
--;
299 *pskb
= __skb_dequeue(&p
->rx_list
);
301 dma_unmap_single(p
->dev
, re
.s
.addr
,
302 ETH_FRAME_LEN
+ OCTEON_MGMT_RX_HEADROOM
,
309 static int octeon_mgmt_receive_one(struct octeon_mgmt
*p
)
312 struct net_device
*netdev
= p
->netdev
;
313 union cvmx_mixx_ircnt mix_ircnt
;
314 union mgmt_port_ring_entry re
;
316 struct sk_buff
*skb2
;
317 struct sk_buff
*skb_new
;
318 union mgmt_port_ring_entry re2
;
322 re
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb
);
323 if (likely(re
.s
.code
== RING_ENTRY_CODE_DONE
)) {
324 /* A good packet, send it up. */
325 skb_put(skb
, re
.s
.len
);
327 skb
->protocol
= eth_type_trans(skb
, netdev
);
328 netdev
->stats
.rx_packets
++;
329 netdev
->stats
.rx_bytes
+= skb
->len
;
330 netif_receive_skb(skb
);
332 } else if (re
.s
.code
== RING_ENTRY_CODE_MORE
) {
334 * Packet split across skbs. This can happen if we
335 * increase the MTU. Buffers that are already in the
336 * rx ring can then end up being too small. As the rx
337 * ring is refilled, buffers sized for the new MTU
338 * will be used and we should go back to the normal
341 skb_put(skb
, re
.s
.len
);
343 re2
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb2
);
344 if (re2
.s
.code
!= RING_ENTRY_CODE_MORE
345 && re2
.s
.code
!= RING_ENTRY_CODE_DONE
)
347 skb_put(skb2
, re2
.s
.len
);
348 skb_new
= skb_copy_expand(skb
, 0, skb2
->len
,
352 if (skb_copy_bits(skb2
, 0, skb_tail_pointer(skb_new
),
355 skb_put(skb_new
, skb2
->len
);
356 dev_kfree_skb_any(skb
);
357 dev_kfree_skb_any(skb2
);
359 } while (re2
.s
.code
== RING_ENTRY_CODE_MORE
);
362 /* Some other error, discard it. */
363 dev_kfree_skb_any(skb
);
365 * Error statistics are accumulated in
366 * octeon_mgmt_update_rx_stats.
371 /* Discard the whole mess. */
372 dev_kfree_skb_any(skb
);
373 dev_kfree_skb_any(skb2
);
374 while (re2
.s
.code
== RING_ENTRY_CODE_MORE
) {
375 re2
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb2
);
376 dev_kfree_skb_any(skb2
);
378 netdev
->stats
.rx_errors
++;
381 /* Tell the hardware we processed a packet. */
383 mix_ircnt
.s
.ircnt
= 1;
384 cvmx_write_csr(CVMX_MIXX_IRCNT(port
), mix_ircnt
.u64
);
388 static int octeon_mgmt_receive_packets(struct octeon_mgmt
*p
, int budget
)
391 unsigned int work_done
= 0;
392 union cvmx_mixx_ircnt mix_ircnt
;
395 mix_ircnt
.u64
= cvmx_read_csr(CVMX_MIXX_IRCNT(port
));
396 while (work_done
< budget
&& mix_ircnt
.s
.ircnt
) {
398 rc
= octeon_mgmt_receive_one(p
);
402 /* Check for more packets. */
403 mix_ircnt
.u64
= cvmx_read_csr(CVMX_MIXX_IRCNT(port
));
406 octeon_mgmt_rx_fill_ring(p
->netdev
);
411 static int octeon_mgmt_napi_poll(struct napi_struct
*napi
, int budget
)
413 struct octeon_mgmt
*p
= container_of(napi
, struct octeon_mgmt
, napi
);
414 struct net_device
*netdev
= p
->netdev
;
415 unsigned int work_done
= 0;
417 work_done
= octeon_mgmt_receive_packets(p
, budget
);
419 if (work_done
< budget
) {
420 /* We stopped because no more packets were available. */
422 octeon_mgmt_enable_rx_irq(p
);
424 octeon_mgmt_update_rx_stats(netdev
);
429 /* Reset the hardware to clean state. */
430 static void octeon_mgmt_reset_hw(struct octeon_mgmt
*p
)
432 union cvmx_mixx_ctl mix_ctl
;
433 union cvmx_mixx_bist mix_bist
;
434 union cvmx_agl_gmx_bist agl_gmx_bist
;
437 cvmx_write_csr(CVMX_MIXX_CTL(p
->port
), mix_ctl
.u64
);
439 mix_ctl
.u64
= cvmx_read_csr(CVMX_MIXX_CTL(p
->port
));
440 } while (mix_ctl
.s
.busy
);
442 cvmx_write_csr(CVMX_MIXX_CTL(p
->port
), mix_ctl
.u64
);
443 cvmx_read_csr(CVMX_MIXX_CTL(p
->port
));
446 mix_bist
.u64
= cvmx_read_csr(CVMX_MIXX_BIST(p
->port
));
448 dev_warn(p
->dev
, "MIX failed BIST (0x%016llx)\n",
449 (unsigned long long)mix_bist
.u64
);
451 agl_gmx_bist
.u64
= cvmx_read_csr(CVMX_AGL_GMX_BIST
);
452 if (agl_gmx_bist
.u64
)
453 dev_warn(p
->dev
, "AGL failed BIST (0x%016llx)\n",
454 (unsigned long long)agl_gmx_bist
.u64
);
457 struct octeon_mgmt_cam_state
{
463 static void octeon_mgmt_cam_state_add(struct octeon_mgmt_cam_state
*cs
,
468 for (i
= 0; i
< 6; i
++)
469 cs
->cam
[i
] |= (u64
)addr
[i
] << (8 * (cs
->cam_index
));
470 cs
->cam_mask
|= (1ULL << cs
->cam_index
);
474 static void octeon_mgmt_set_rx_filtering(struct net_device
*netdev
)
476 struct octeon_mgmt
*p
= netdev_priv(netdev
);
478 union cvmx_agl_gmx_rxx_adr_ctl adr_ctl
;
479 union cvmx_agl_gmx_prtx_cfg agl_gmx_prtx
;
481 unsigned int prev_packet_enable
;
482 unsigned int cam_mode
= 1; /* 1 - Accept on CAM match */
483 unsigned int multicast_mode
= 1; /* 1 - Reject all multicast. */
484 struct octeon_mgmt_cam_state cam_state
;
485 struct netdev_hw_addr
*ha
;
486 int available_cam_entries
;
488 memset(&cam_state
, 0, sizeof(cam_state
));
490 if ((netdev
->flags
& IFF_PROMISC
) || netdev
->uc
.count
> 7) {
492 available_cam_entries
= 8;
495 * One CAM entry for the primary address, leaves seven
496 * for the secondary addresses.
498 available_cam_entries
= 7 - netdev
->uc
.count
;
501 if (netdev
->flags
& IFF_MULTICAST
) {
502 if (cam_mode
== 0 || (netdev
->flags
& IFF_ALLMULTI
) ||
503 netdev_mc_count(netdev
) > available_cam_entries
)
504 multicast_mode
= 2; /* 2 - Accept all multicast. */
506 multicast_mode
= 0; /* 0 - Use CAM. */
510 /* Add primary address. */
511 octeon_mgmt_cam_state_add(&cam_state
, netdev
->dev_addr
);
512 netdev_for_each_uc_addr(ha
, netdev
)
513 octeon_mgmt_cam_state_add(&cam_state
, ha
->addr
);
515 if (multicast_mode
== 0) {
516 netdev_for_each_mc_addr(ha
, netdev
)
517 octeon_mgmt_cam_state_add(&cam_state
, ha
->addr
);
520 spin_lock_irqsave(&p
->lock
, flags
);
522 /* Disable packet I/O. */
523 agl_gmx_prtx
.u64
= cvmx_read_csr(CVMX_AGL_GMX_PRTX_CFG(port
));
524 prev_packet_enable
= agl_gmx_prtx
.s
.en
;
525 agl_gmx_prtx
.s
.en
= 0;
526 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
), agl_gmx_prtx
.u64
);
529 adr_ctl
.s
.cam_mode
= cam_mode
;
530 adr_ctl
.s
.mcst
= multicast_mode
;
531 adr_ctl
.s
.bcst
= 1; /* Allow broadcast */
533 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CTL(port
), adr_ctl
.u64
);
535 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM0(port
), cam_state
.cam
[0]);
536 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM1(port
), cam_state
.cam
[1]);
537 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM2(port
), cam_state
.cam
[2]);
538 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM3(port
), cam_state
.cam
[3]);
539 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM4(port
), cam_state
.cam
[4]);
540 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM5(port
), cam_state
.cam
[5]);
541 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM_EN(port
), cam_state
.cam_mask
);
543 /* Restore packet I/O. */
544 agl_gmx_prtx
.s
.en
= prev_packet_enable
;
545 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
), agl_gmx_prtx
.u64
);
547 spin_unlock_irqrestore(&p
->lock
, flags
);
550 static int octeon_mgmt_set_mac_address(struct net_device
*netdev
, void *addr
)
552 struct sockaddr
*sa
= addr
;
554 if (!is_valid_ether_addr(sa
->sa_data
))
555 return -EADDRNOTAVAIL
;
557 memcpy(netdev
->dev_addr
, sa
->sa_data
, ETH_ALEN
);
559 octeon_mgmt_set_rx_filtering(netdev
);
564 static int octeon_mgmt_change_mtu(struct net_device
*netdev
, int new_mtu
)
566 struct octeon_mgmt
*p
= netdev_priv(netdev
);
568 int size_without_fcs
= new_mtu
+ OCTEON_MGMT_RX_HEADROOM
;
571 * Limit the MTU to make sure the ethernet packets are between
572 * 64 bytes and 16383 bytes.
574 if (size_without_fcs
< 64 || size_without_fcs
> 16383) {
575 dev_warn(p
->dev
, "MTU must be between %d and %d.\n",
576 64 - OCTEON_MGMT_RX_HEADROOM
,
577 16383 - OCTEON_MGMT_RX_HEADROOM
);
581 netdev
->mtu
= new_mtu
;
583 cvmx_write_csr(CVMX_AGL_GMX_RXX_FRM_MAX(port
), size_without_fcs
);
584 cvmx_write_csr(CVMX_AGL_GMX_RXX_JABBER(port
),
585 (size_without_fcs
+ 7) & 0xfff8);
590 static irqreturn_t
octeon_mgmt_interrupt(int cpl
, void *dev_id
)
592 struct net_device
*netdev
= dev_id
;
593 struct octeon_mgmt
*p
= netdev_priv(netdev
);
595 union cvmx_mixx_isr mixx_isr
;
597 mixx_isr
.u64
= cvmx_read_csr(CVMX_MIXX_ISR(port
));
599 /* Clear any pending interrupts */
600 cvmx_write_csr(CVMX_MIXX_ISR(port
), mixx_isr
.u64
);
601 cvmx_read_csr(CVMX_MIXX_ISR(port
));
603 if (mixx_isr
.s
.irthresh
) {
604 octeon_mgmt_disable_rx_irq(p
);
605 napi_schedule(&p
->napi
);
607 if (mixx_isr
.s
.orthresh
) {
608 octeon_mgmt_disable_tx_irq(p
);
609 tasklet_schedule(&p
->tx_clean_tasklet
);
615 static int octeon_mgmt_ioctl(struct net_device
*netdev
,
616 struct ifreq
*rq
, int cmd
)
618 struct octeon_mgmt
*p
= netdev_priv(netdev
);
620 if (!netif_running(netdev
))
626 return phy_mii_ioctl(p
->phydev
, rq
, cmd
);
629 static void octeon_mgmt_adjust_link(struct net_device
*netdev
)
631 struct octeon_mgmt
*p
= netdev_priv(netdev
);
633 union cvmx_agl_gmx_prtx_cfg prtx_cfg
;
635 int link_changed
= 0;
637 spin_lock_irqsave(&p
->lock
, flags
);
638 if (p
->phydev
->link
) {
641 if (p
->last_duplex
!= p
->phydev
->duplex
) {
642 p
->last_duplex
= p
->phydev
->duplex
;
644 cvmx_read_csr(CVMX_AGL_GMX_PRTX_CFG(port
));
645 prtx_cfg
.s
.duplex
= p
->phydev
->duplex
;
646 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
),
653 p
->last_link
= p
->phydev
->link
;
654 spin_unlock_irqrestore(&p
->lock
, flags
);
656 if (link_changed
!= 0) {
657 if (link_changed
> 0) {
658 netif_carrier_on(netdev
);
659 pr_info("%s: Link is up - %d/%s\n", netdev
->name
,
661 DUPLEX_FULL
== p
->phydev
->duplex
?
664 netif_carrier_off(netdev
);
665 pr_info("%s: Link is down\n", netdev
->name
);
670 static int octeon_mgmt_init_phy(struct net_device
*netdev
)
672 struct octeon_mgmt
*p
= netdev_priv(netdev
);
673 char phy_id
[MII_BUS_ID_SIZE
+ 3];
675 if (octeon_is_simulation()) {
676 /* No PHYs in the simulator. */
677 netif_carrier_on(netdev
);
681 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
, "mdio-octeon-0", p
->port
);
683 p
->phydev
= phy_connect(netdev
, phy_id
, octeon_mgmt_adjust_link
, 0,
684 PHY_INTERFACE_MODE_MII
);
686 if (IS_ERR(p
->phydev
)) {
691 phy_start_aneg(p
->phydev
);
696 static int octeon_mgmt_open(struct net_device
*netdev
)
698 struct octeon_mgmt
*p
= netdev_priv(netdev
);
700 union cvmx_mixx_ctl mix_ctl
;
701 union cvmx_agl_gmx_inf_mode agl_gmx_inf_mode
;
702 union cvmx_mixx_oring1 oring1
;
703 union cvmx_mixx_iring1 iring1
;
704 union cvmx_agl_gmx_prtx_cfg prtx_cfg
;
705 union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl
;
706 union cvmx_mixx_irhwm mix_irhwm
;
707 union cvmx_mixx_orhwm mix_orhwm
;
708 union cvmx_mixx_intena mix_intena
;
711 /* Allocate ring buffers. */
712 p
->tx_ring
= kzalloc(ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
717 dma_map_single(p
->dev
, p
->tx_ring
,
718 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
721 p
->tx_next_clean
= 0;
722 p
->tx_current_fill
= 0;
725 p
->rx_ring
= kzalloc(ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
730 dma_map_single(p
->dev
, p
->rx_ring
,
731 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
736 p
->rx_current_fill
= 0;
738 octeon_mgmt_reset_hw(p
);
740 mix_ctl
.u64
= cvmx_read_csr(CVMX_MIXX_CTL(port
));
742 /* Bring it out of reset if needed. */
743 if (mix_ctl
.s
.reset
) {
745 cvmx_write_csr(CVMX_MIXX_CTL(port
), mix_ctl
.u64
);
747 mix_ctl
.u64
= cvmx_read_csr(CVMX_MIXX_CTL(port
));
748 } while (mix_ctl
.s
.reset
);
751 agl_gmx_inf_mode
.u64
= 0;
752 agl_gmx_inf_mode
.s
.en
= 1;
753 cvmx_write_csr(CVMX_AGL_GMX_INF_MODE
, agl_gmx_inf_mode
.u64
);
756 oring1
.s
.obase
= p
->tx_ring_handle
>> 3;
757 oring1
.s
.osize
= OCTEON_MGMT_TX_RING_SIZE
;
758 cvmx_write_csr(CVMX_MIXX_ORING1(port
), oring1
.u64
);
761 iring1
.s
.ibase
= p
->rx_ring_handle
>> 3;
762 iring1
.s
.isize
= OCTEON_MGMT_RX_RING_SIZE
;
763 cvmx_write_csr(CVMX_MIXX_IRING1(port
), iring1
.u64
);
765 /* Disable packet I/O. */
766 prtx_cfg
.u64
= cvmx_read_csr(CVMX_AGL_GMX_PRTX_CFG(port
));
768 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
), prtx_cfg
.u64
);
770 memcpy(sa
.sa_data
, netdev
->dev_addr
, ETH_ALEN
);
771 octeon_mgmt_set_mac_address(netdev
, &sa
);
773 octeon_mgmt_change_mtu(netdev
, netdev
->mtu
);
776 * Enable the port HW. Packets are not allowed until
777 * cvmx_mgmt_port_enable() is called.
780 mix_ctl
.s
.crc_strip
= 1; /* Strip the ending CRC */
781 mix_ctl
.s
.en
= 1; /* Enable the port */
782 mix_ctl
.s
.nbtarb
= 0; /* Arbitration mode */
783 /* MII CB-request FIFO programmable high watermark */
784 mix_ctl
.s
.mrq_hwm
= 1;
785 cvmx_write_csr(CVMX_MIXX_CTL(port
), mix_ctl
.u64
);
787 if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X
)
788 || OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X
)) {
790 * Force compensation values, as they are not
791 * determined properly by HW
793 union cvmx_agl_gmx_drv_ctl drv_ctl
;
795 drv_ctl
.u64
= cvmx_read_csr(CVMX_AGL_GMX_DRV_CTL
);
797 drv_ctl
.s
.byp_en1
= 1;
801 drv_ctl
.s
.byp_en
= 1;
805 cvmx_write_csr(CVMX_AGL_GMX_DRV_CTL
, drv_ctl
.u64
);
808 octeon_mgmt_rx_fill_ring(netdev
);
810 /* Clear statistics. */
812 cvmx_write_csr(CVMX_AGL_GMX_RXX_STATS_CTL(port
), 1);
813 cvmx_write_csr(CVMX_AGL_GMX_RXX_STATS_PKTS_DRP(port
), 0);
814 cvmx_write_csr(CVMX_AGL_GMX_RXX_STATS_PKTS_BAD(port
), 0);
816 cvmx_write_csr(CVMX_AGL_GMX_TXX_STATS_CTL(port
), 1);
817 cvmx_write_csr(CVMX_AGL_GMX_TXX_STAT0(port
), 0);
818 cvmx_write_csr(CVMX_AGL_GMX_TXX_STAT1(port
), 0);
820 /* Clear any pending interrupts */
821 cvmx_write_csr(CVMX_MIXX_ISR(port
), cvmx_read_csr(CVMX_MIXX_ISR(port
)));
823 if (request_irq(p
->irq
, octeon_mgmt_interrupt
, 0, netdev
->name
,
825 dev_err(p
->dev
, "request_irq(%d) failed.\n", p
->irq
);
829 /* Interrupt every single RX packet */
831 mix_irhwm
.s
.irhwm
= 0;
832 cvmx_write_csr(CVMX_MIXX_IRHWM(port
), mix_irhwm
.u64
);
834 /* Interrupt when we have 1 or more packets to clean. */
836 mix_orhwm
.s
.orhwm
= 1;
837 cvmx_write_csr(CVMX_MIXX_ORHWM(port
), mix_orhwm
.u64
);
839 /* Enable receive and transmit interrupts */
841 mix_intena
.s
.ithena
= 1;
842 mix_intena
.s
.othena
= 1;
843 cvmx_write_csr(CVMX_MIXX_INTENA(port
), mix_intena
.u64
);
846 /* Enable packet I/O. */
849 rxx_frm_ctl
.s
.pre_align
= 1;
851 * When set, disables the length check for non-min sized pkts
852 * with padding in the client data.
854 rxx_frm_ctl
.s
.pad_len
= 1;
855 /* When set, disables the length check for VLAN pkts */
856 rxx_frm_ctl
.s
.vlan_len
= 1;
857 /* When set, PREAMBLE checking is less strict */
858 rxx_frm_ctl
.s
.pre_free
= 1;
859 /* Control Pause Frames can match station SMAC */
860 rxx_frm_ctl
.s
.ctl_smac
= 0;
861 /* Control Pause Frames can match globally assign Multicast address */
862 rxx_frm_ctl
.s
.ctl_mcst
= 1;
863 /* Forward pause information to TX block */
864 rxx_frm_ctl
.s
.ctl_bck
= 1;
865 /* Drop Control Pause Frames */
866 rxx_frm_ctl
.s
.ctl_drp
= 1;
867 /* Strip off the preamble */
868 rxx_frm_ctl
.s
.pre_strp
= 1;
870 * This port is configured to send PREAMBLE+SFD to begin every
871 * frame. GMX checks that the PREAMBLE is sent correctly.
873 rxx_frm_ctl
.s
.pre_chk
= 1;
874 cvmx_write_csr(CVMX_AGL_GMX_RXX_FRM_CTL(port
), rxx_frm_ctl
.u64
);
876 /* Enable the AGL block */
877 agl_gmx_inf_mode
.u64
= 0;
878 agl_gmx_inf_mode
.s
.en
= 1;
879 cvmx_write_csr(CVMX_AGL_GMX_INF_MODE
, agl_gmx_inf_mode
.u64
);
881 /* Configure the port duplex and enables */
882 prtx_cfg
.u64
= cvmx_read_csr(CVMX_AGL_GMX_PRTX_CFG(port
));
883 prtx_cfg
.s
.tx_en
= 1;
884 prtx_cfg
.s
.rx_en
= 1;
887 prtx_cfg
.s
.duplex
= p
->last_duplex
;
888 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
), prtx_cfg
.u64
);
891 netif_carrier_off(netdev
);
893 if (octeon_mgmt_init_phy(netdev
)) {
894 dev_err(p
->dev
, "Cannot initialize PHY.\n");
898 netif_wake_queue(netdev
);
899 napi_enable(&p
->napi
);
903 octeon_mgmt_reset_hw(p
);
904 dma_unmap_single(p
->dev
, p
->rx_ring_handle
,
905 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
909 dma_unmap_single(p
->dev
, p
->tx_ring_handle
,
910 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
916 static int octeon_mgmt_stop(struct net_device
*netdev
)
918 struct octeon_mgmt
*p
= netdev_priv(netdev
);
920 napi_disable(&p
->napi
);
921 netif_stop_queue(netdev
);
924 phy_disconnect(p
->phydev
);
926 netif_carrier_off(netdev
);
928 octeon_mgmt_reset_hw(p
);
930 free_irq(p
->irq
, netdev
);
932 /* dma_unmap is a nop on Octeon, so just free everything. */
933 skb_queue_purge(&p
->tx_list
);
934 skb_queue_purge(&p
->rx_list
);
936 dma_unmap_single(p
->dev
, p
->rx_ring_handle
,
937 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
941 dma_unmap_single(p
->dev
, p
->tx_ring_handle
,
942 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
949 static int octeon_mgmt_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
951 struct octeon_mgmt
*p
= netdev_priv(netdev
);
953 union mgmt_port_ring_entry re
;
955 int rv
= NETDEV_TX_BUSY
;
959 re
.s
.addr
= dma_map_single(p
->dev
, skb
->data
,
963 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
965 if (unlikely(p
->tx_current_fill
>= ring_max_fill(OCTEON_MGMT_TX_RING_SIZE
) - 1)) {
966 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
967 netif_stop_queue(netdev
);
968 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
971 if (unlikely(p
->tx_current_fill
>=
972 ring_max_fill(OCTEON_MGMT_TX_RING_SIZE
))) {
973 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
974 dma_unmap_single(p
->dev
, re
.s
.addr
, re
.s
.len
,
979 __skb_queue_tail(&p
->tx_list
, skb
);
981 /* Put it in the ring. */
982 p
->tx_ring
[p
->tx_next
] = re
.d64
;
983 p
->tx_next
= (p
->tx_next
+ 1) % OCTEON_MGMT_TX_RING_SIZE
;
984 p
->tx_current_fill
++;
986 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
988 dma_sync_single_for_device(p
->dev
, p
->tx_ring_handle
,
989 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
992 netdev
->stats
.tx_packets
++;
993 netdev
->stats
.tx_bytes
+= skb
->len
;
996 cvmx_write_csr(CVMX_MIXX_ORING2(port
), 1);
1000 octeon_mgmt_update_tx_stats(netdev
);
1004 #ifdef CONFIG_NET_POLL_CONTROLLER
1005 static void octeon_mgmt_poll_controller(struct net_device
*netdev
)
1007 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1009 octeon_mgmt_receive_packets(p
, 16);
1010 octeon_mgmt_update_rx_stats(netdev
);
1014 static void octeon_mgmt_get_drvinfo(struct net_device
*netdev
,
1015 struct ethtool_drvinfo
*info
)
1017 strncpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
1018 strncpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
1019 strncpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
1020 strncpy(info
->bus_info
, "N/A", sizeof(info
->bus_info
));
1022 info
->testinfo_len
= 0;
1023 info
->regdump_len
= 0;
1024 info
->eedump_len
= 0;
1027 static int octeon_mgmt_get_settings(struct net_device
*netdev
,
1028 struct ethtool_cmd
*cmd
)
1030 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1033 return phy_ethtool_gset(p
->phydev
, cmd
);
1038 static int octeon_mgmt_set_settings(struct net_device
*netdev
,
1039 struct ethtool_cmd
*cmd
)
1041 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1043 if (!capable(CAP_NET_ADMIN
))
1047 return phy_ethtool_sset(p
->phydev
, cmd
);
1052 static const struct ethtool_ops octeon_mgmt_ethtool_ops
= {
1053 .get_drvinfo
= octeon_mgmt_get_drvinfo
,
1054 .get_link
= ethtool_op_get_link
,
1055 .get_settings
= octeon_mgmt_get_settings
,
1056 .set_settings
= octeon_mgmt_set_settings
1059 static const struct net_device_ops octeon_mgmt_ops
= {
1060 .ndo_open
= octeon_mgmt_open
,
1061 .ndo_stop
= octeon_mgmt_stop
,
1062 .ndo_start_xmit
= octeon_mgmt_xmit
,
1063 .ndo_set_rx_mode
= octeon_mgmt_set_rx_filtering
,
1064 .ndo_set_mac_address
= octeon_mgmt_set_mac_address
,
1065 .ndo_do_ioctl
= octeon_mgmt_ioctl
,
1066 .ndo_change_mtu
= octeon_mgmt_change_mtu
,
1067 #ifdef CONFIG_NET_POLL_CONTROLLER
1068 .ndo_poll_controller
= octeon_mgmt_poll_controller
,
1072 static int __devinit
octeon_mgmt_probe(struct platform_device
*pdev
)
1074 struct resource
*res_irq
;
1075 struct net_device
*netdev
;
1076 struct octeon_mgmt
*p
;
1079 netdev
= alloc_etherdev(sizeof(struct octeon_mgmt
));
1083 dev_set_drvdata(&pdev
->dev
, netdev
);
1084 p
= netdev_priv(netdev
);
1085 netif_napi_add(netdev
, &p
->napi
, octeon_mgmt_napi_poll
,
1086 OCTEON_MGMT_NAPI_WEIGHT
);
1089 p
->dev
= &pdev
->dev
;
1092 snprintf(netdev
->name
, IFNAMSIZ
, "mgmt%d", p
->port
);
1094 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1098 p
->irq
= res_irq
->start
;
1099 spin_lock_init(&p
->lock
);
1101 skb_queue_head_init(&p
->tx_list
);
1102 skb_queue_head_init(&p
->rx_list
);
1103 tasklet_init(&p
->tx_clean_tasklet
,
1104 octeon_mgmt_clean_tx_tasklet
, (unsigned long)p
);
1106 netdev
->priv_flags
|= IFF_UNICAST_FLT
;
1108 netdev
->netdev_ops
= &octeon_mgmt_ops
;
1109 netdev
->ethtool_ops
= &octeon_mgmt_ethtool_ops
;
1111 /* The mgmt ports get the first N MACs. */
1112 for (i
= 0; i
< 6; i
++)
1113 netdev
->dev_addr
[i
] = octeon_bootinfo
->mac_addr_base
[i
];
1114 netdev
->dev_addr
[5] += p
->port
;
1116 if (p
->port
>= octeon_bootinfo
->mac_addr_count
)
1118 "Error %s: Using MAC outside of the assigned range: %pM\n",
1119 netdev
->name
, netdev
->dev_addr
);
1121 if (register_netdev(netdev
))
1124 dev_info(&pdev
->dev
, "Version " DRV_VERSION
"\n");
1127 free_netdev(netdev
);
1131 static int __devexit
octeon_mgmt_remove(struct platform_device
*pdev
)
1133 struct net_device
*netdev
= dev_get_drvdata(&pdev
->dev
);
1135 unregister_netdev(netdev
);
1136 free_netdev(netdev
);
1140 static struct platform_driver octeon_mgmt_driver
= {
1142 .name
= "octeon_mgmt",
1143 .owner
= THIS_MODULE
,
1145 .probe
= octeon_mgmt_probe
,
1146 .remove
= __devexit_p(octeon_mgmt_remove
),
1149 extern void octeon_mdiobus_force_mod_depencency(void);
1151 static int __init
octeon_mgmt_mod_init(void)
1153 /* Force our mdiobus driver module to be loaded first. */
1154 octeon_mdiobus_force_mod_depencency();
1155 return platform_driver_register(&octeon_mgmt_driver
);
1158 static void __exit
octeon_mgmt_mod_exit(void)
1160 platform_driver_unregister(&octeon_mgmt_driver
);
1163 module_init(octeon_mgmt_mod_init
);
1164 module_exit(octeon_mgmt_mod_exit
);
1166 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
1167 MODULE_AUTHOR("David Daney");
1168 MODULE_LICENSE("GPL");
1169 MODULE_VERSION(DRV_VERSION
);