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/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/netdevice.h>
15 #include <linux/etherdevice.h>
17 #include <linux/if_vlan.h>
18 #include <linux/slab.h>
19 #include <linux/phy.h>
20 #include <linux/spinlock.h>
22 #include <asm/octeon/octeon.h>
23 #include <asm/octeon/cvmx-mixx-defs.h>
24 #include <asm/octeon/cvmx-agl-defs.h>
26 #define DRV_NAME "octeon_mgmt"
27 #define DRV_VERSION "2.0"
28 #define DRV_DESCRIPTION \
29 "Cavium Networks Octeon MII (management) port Network Driver"
31 #define OCTEON_MGMT_NAPI_WEIGHT 16
34 * Ring sizes that are powers of two allow for more efficient modulo
37 #define OCTEON_MGMT_RX_RING_SIZE 512
38 #define OCTEON_MGMT_TX_RING_SIZE 128
40 /* Allow 8 bytes for vlan and FCS. */
41 #define OCTEON_MGMT_RX_HEADROOM (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN)
43 union mgmt_port_ring_entry
{
47 /* Length of the buffer/packet in bytes */
49 /* For TX, signals that the packet should be timestamped */
51 /* The RX error code */
53 #define RING_ENTRY_CODE_DONE 0xf
54 #define RING_ENTRY_CODE_MORE 0x10
55 /* Physical address of the buffer */
61 struct net_device
*netdev
;
65 dma_addr_t tx_ring_handle
;
67 unsigned int tx_next_clean
;
68 unsigned int tx_current_fill
;
69 /* The tx_list lock also protects the ring related variables */
70 struct sk_buff_head tx_list
;
72 /* RX variables only touched in napi_poll. No locking necessary. */
74 dma_addr_t rx_ring_handle
;
76 unsigned int rx_next_fill
;
77 unsigned int rx_current_fill
;
78 struct sk_buff_head rx_list
;
81 unsigned int last_duplex
;
82 unsigned int last_link
;
84 struct napi_struct napi
;
85 struct tasklet_struct tx_clean_tasklet
;
86 struct phy_device
*phydev
;
89 static void octeon_mgmt_set_rx_irq(struct octeon_mgmt
*p
, int enable
)
92 union cvmx_mixx_intena mix_intena
;
95 spin_lock_irqsave(&p
->lock
, flags
);
96 mix_intena
.u64
= cvmx_read_csr(CVMX_MIXX_INTENA(port
));
97 mix_intena
.s
.ithena
= enable
? 1 : 0;
98 cvmx_write_csr(CVMX_MIXX_INTENA(port
), mix_intena
.u64
);
99 spin_unlock_irqrestore(&p
->lock
, flags
);
102 static void octeon_mgmt_set_tx_irq(struct octeon_mgmt
*p
, int enable
)
105 union cvmx_mixx_intena mix_intena
;
108 spin_lock_irqsave(&p
->lock
, flags
);
109 mix_intena
.u64
= cvmx_read_csr(CVMX_MIXX_INTENA(port
));
110 mix_intena
.s
.othena
= enable
? 1 : 0;
111 cvmx_write_csr(CVMX_MIXX_INTENA(port
), mix_intena
.u64
);
112 spin_unlock_irqrestore(&p
->lock
, flags
);
115 static inline void octeon_mgmt_enable_rx_irq(struct octeon_mgmt
*p
)
117 octeon_mgmt_set_rx_irq(p
, 1);
120 static inline void octeon_mgmt_disable_rx_irq(struct octeon_mgmt
*p
)
122 octeon_mgmt_set_rx_irq(p
, 0);
125 static inline void octeon_mgmt_enable_tx_irq(struct octeon_mgmt
*p
)
127 octeon_mgmt_set_tx_irq(p
, 1);
130 static inline void octeon_mgmt_disable_tx_irq(struct octeon_mgmt
*p
)
132 octeon_mgmt_set_tx_irq(p
, 0);
135 static unsigned int ring_max_fill(unsigned int ring_size
)
137 return ring_size
- 8;
140 static unsigned int ring_size_to_bytes(unsigned int ring_size
)
142 return ring_size
* sizeof(union mgmt_port_ring_entry
);
145 static void octeon_mgmt_rx_fill_ring(struct net_device
*netdev
)
147 struct octeon_mgmt
*p
= netdev_priv(netdev
);
150 while (p
->rx_current_fill
< ring_max_fill(OCTEON_MGMT_RX_RING_SIZE
)) {
152 union mgmt_port_ring_entry re
;
155 /* CN56XX pass 1 needs 8 bytes of padding. */
156 size
= netdev
->mtu
+ OCTEON_MGMT_RX_HEADROOM
+ 8 + NET_IP_ALIGN
;
158 skb
= netdev_alloc_skb(netdev
, size
);
161 skb_reserve(skb
, NET_IP_ALIGN
);
162 __skb_queue_tail(&p
->rx_list
, skb
);
166 re
.s
.addr
= dma_map_single(p
->dev
, skb
->data
,
170 /* Put it in the ring. */
171 p
->rx_ring
[p
->rx_next_fill
] = re
.d64
;
172 dma_sync_single_for_device(p
->dev
, p
->rx_ring_handle
,
173 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
176 (p
->rx_next_fill
+ 1) % OCTEON_MGMT_RX_RING_SIZE
;
177 p
->rx_current_fill
++;
179 cvmx_write_csr(CVMX_MIXX_IRING2(port
), 1);
183 static void octeon_mgmt_clean_tx_buffers(struct octeon_mgmt
*p
)
186 union cvmx_mixx_orcnt mix_orcnt
;
187 union mgmt_port_ring_entry re
;
192 mix_orcnt
.u64
= cvmx_read_csr(CVMX_MIXX_ORCNT(port
));
193 while (mix_orcnt
.s
.orcnt
) {
194 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
196 mix_orcnt
.u64
= cvmx_read_csr(CVMX_MIXX_ORCNT(port
));
198 if (mix_orcnt
.s
.orcnt
== 0) {
199 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
203 dma_sync_single_for_cpu(p
->dev
, p
->tx_ring_handle
,
204 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
207 re
.d64
= p
->tx_ring
[p
->tx_next_clean
];
209 (p
->tx_next_clean
+ 1) % OCTEON_MGMT_TX_RING_SIZE
;
210 skb
= __skb_dequeue(&p
->tx_list
);
213 mix_orcnt
.s
.orcnt
= 1;
215 /* Acknowledge to hardware that we have the buffer. */
216 cvmx_write_csr(CVMX_MIXX_ORCNT(port
), mix_orcnt
.u64
);
217 p
->tx_current_fill
--;
219 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
221 dma_unmap_single(p
->dev
, re
.s
.addr
, re
.s
.len
,
223 dev_kfree_skb_any(skb
);
226 mix_orcnt
.u64
= cvmx_read_csr(CVMX_MIXX_ORCNT(port
));
229 if (cleaned
&& netif_queue_stopped(p
->netdev
))
230 netif_wake_queue(p
->netdev
);
233 static void octeon_mgmt_clean_tx_tasklet(unsigned long arg
)
235 struct octeon_mgmt
*p
= (struct octeon_mgmt
*)arg
;
236 octeon_mgmt_clean_tx_buffers(p
);
237 octeon_mgmt_enable_tx_irq(p
);
240 static void octeon_mgmt_update_rx_stats(struct net_device
*netdev
)
242 struct octeon_mgmt
*p
= netdev_priv(netdev
);
247 /* These reads also clear the count registers. */
248 drop
= cvmx_read_csr(CVMX_AGL_GMX_RXX_STATS_PKTS_DRP(port
));
249 bad
= cvmx_read_csr(CVMX_AGL_GMX_RXX_STATS_PKTS_BAD(port
));
252 /* Do an atomic update. */
253 spin_lock_irqsave(&p
->lock
, flags
);
254 netdev
->stats
.rx_errors
+= bad
;
255 netdev
->stats
.rx_dropped
+= drop
;
256 spin_unlock_irqrestore(&p
->lock
, flags
);
260 static void octeon_mgmt_update_tx_stats(struct net_device
*netdev
)
262 struct octeon_mgmt
*p
= netdev_priv(netdev
);
266 union cvmx_agl_gmx_txx_stat0 s0
;
267 union cvmx_agl_gmx_txx_stat1 s1
;
269 /* These reads also clear the count registers. */
270 s0
.u64
= cvmx_read_csr(CVMX_AGL_GMX_TXX_STAT0(port
));
271 s1
.u64
= cvmx_read_csr(CVMX_AGL_GMX_TXX_STAT1(port
));
273 if (s0
.s
.xsdef
|| s0
.s
.xscol
|| s1
.s
.scol
|| s1
.s
.mcol
) {
274 /* Do an atomic update. */
275 spin_lock_irqsave(&p
->lock
, flags
);
276 netdev
->stats
.tx_errors
+= s0
.s
.xsdef
+ s0
.s
.xscol
;
277 netdev
->stats
.collisions
+= s1
.s
.scol
+ s1
.s
.mcol
;
278 spin_unlock_irqrestore(&p
->lock
, flags
);
283 * Dequeue a receive skb and its corresponding ring entry. The ring
284 * entry is returned, *pskb is updated to point to the skb.
286 static u64
octeon_mgmt_dequeue_rx_buffer(struct octeon_mgmt
*p
,
287 struct sk_buff
**pskb
)
289 union mgmt_port_ring_entry re
;
291 dma_sync_single_for_cpu(p
->dev
, p
->rx_ring_handle
,
292 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
295 re
.d64
= p
->rx_ring
[p
->rx_next
];
296 p
->rx_next
= (p
->rx_next
+ 1) % OCTEON_MGMT_RX_RING_SIZE
;
297 p
->rx_current_fill
--;
298 *pskb
= __skb_dequeue(&p
->rx_list
);
300 dma_unmap_single(p
->dev
, re
.s
.addr
,
301 ETH_FRAME_LEN
+ OCTEON_MGMT_RX_HEADROOM
,
308 static int octeon_mgmt_receive_one(struct octeon_mgmt
*p
)
311 struct net_device
*netdev
= p
->netdev
;
312 union cvmx_mixx_ircnt mix_ircnt
;
313 union mgmt_port_ring_entry re
;
315 struct sk_buff
*skb2
;
316 struct sk_buff
*skb_new
;
317 union mgmt_port_ring_entry re2
;
321 re
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb
);
322 if (likely(re
.s
.code
== RING_ENTRY_CODE_DONE
)) {
323 /* A good packet, send it up. */
324 skb_put(skb
, re
.s
.len
);
326 skb
->protocol
= eth_type_trans(skb
, netdev
);
327 netdev
->stats
.rx_packets
++;
328 netdev
->stats
.rx_bytes
+= skb
->len
;
329 netif_receive_skb(skb
);
331 } else if (re
.s
.code
== RING_ENTRY_CODE_MORE
) {
333 * Packet split across skbs. This can happen if we
334 * increase the MTU. Buffers that are already in the
335 * rx ring can then end up being too small. As the rx
336 * ring is refilled, buffers sized for the new MTU
337 * will be used and we should go back to the normal
340 skb_put(skb
, re
.s
.len
);
342 re2
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb2
);
343 if (re2
.s
.code
!= RING_ENTRY_CODE_MORE
344 && re2
.s
.code
!= RING_ENTRY_CODE_DONE
)
346 skb_put(skb2
, re2
.s
.len
);
347 skb_new
= skb_copy_expand(skb
, 0, skb2
->len
,
351 if (skb_copy_bits(skb2
, 0, skb_tail_pointer(skb_new
),
354 skb_put(skb_new
, skb2
->len
);
355 dev_kfree_skb_any(skb
);
356 dev_kfree_skb_any(skb2
);
358 } while (re2
.s
.code
== RING_ENTRY_CODE_MORE
);
361 /* Some other error, discard it. */
362 dev_kfree_skb_any(skb
);
364 * Error statistics are accumulated in
365 * octeon_mgmt_update_rx_stats.
370 /* Discard the whole mess. */
371 dev_kfree_skb_any(skb
);
372 dev_kfree_skb_any(skb2
);
373 while (re2
.s
.code
== RING_ENTRY_CODE_MORE
) {
374 re2
.d64
= octeon_mgmt_dequeue_rx_buffer(p
, &skb2
);
375 dev_kfree_skb_any(skb2
);
377 netdev
->stats
.rx_errors
++;
380 /* Tell the hardware we processed a packet. */
382 mix_ircnt
.s
.ircnt
= 1;
383 cvmx_write_csr(CVMX_MIXX_IRCNT(port
), mix_ircnt
.u64
);
387 static int octeon_mgmt_receive_packets(struct octeon_mgmt
*p
, int budget
)
390 unsigned int work_done
= 0;
391 union cvmx_mixx_ircnt mix_ircnt
;
394 mix_ircnt
.u64
= cvmx_read_csr(CVMX_MIXX_IRCNT(port
));
395 while (work_done
< budget
&& mix_ircnt
.s
.ircnt
) {
397 rc
= octeon_mgmt_receive_one(p
);
401 /* Check for more packets. */
402 mix_ircnt
.u64
= cvmx_read_csr(CVMX_MIXX_IRCNT(port
));
405 octeon_mgmt_rx_fill_ring(p
->netdev
);
410 static int octeon_mgmt_napi_poll(struct napi_struct
*napi
, int budget
)
412 struct octeon_mgmt
*p
= container_of(napi
, struct octeon_mgmt
, napi
);
413 struct net_device
*netdev
= p
->netdev
;
414 unsigned int work_done
= 0;
416 work_done
= octeon_mgmt_receive_packets(p
, budget
);
418 if (work_done
< budget
) {
419 /* We stopped because no more packets were available. */
421 octeon_mgmt_enable_rx_irq(p
);
423 octeon_mgmt_update_rx_stats(netdev
);
428 /* Reset the hardware to clean state. */
429 static void octeon_mgmt_reset_hw(struct octeon_mgmt
*p
)
431 union cvmx_mixx_ctl mix_ctl
;
432 union cvmx_mixx_bist mix_bist
;
433 union cvmx_agl_gmx_bist agl_gmx_bist
;
436 cvmx_write_csr(CVMX_MIXX_CTL(p
->port
), mix_ctl
.u64
);
438 mix_ctl
.u64
= cvmx_read_csr(CVMX_MIXX_CTL(p
->port
));
439 } while (mix_ctl
.s
.busy
);
441 cvmx_write_csr(CVMX_MIXX_CTL(p
->port
), mix_ctl
.u64
);
442 cvmx_read_csr(CVMX_MIXX_CTL(p
->port
));
445 mix_bist
.u64
= cvmx_read_csr(CVMX_MIXX_BIST(p
->port
));
447 dev_warn(p
->dev
, "MIX failed BIST (0x%016llx)\n",
448 (unsigned long long)mix_bist
.u64
);
450 agl_gmx_bist
.u64
= cvmx_read_csr(CVMX_AGL_GMX_BIST
);
451 if (agl_gmx_bist
.u64
)
452 dev_warn(p
->dev
, "AGL failed BIST (0x%016llx)\n",
453 (unsigned long long)agl_gmx_bist
.u64
);
456 struct octeon_mgmt_cam_state
{
462 static void octeon_mgmt_cam_state_add(struct octeon_mgmt_cam_state
*cs
,
467 for (i
= 0; i
< 6; i
++)
468 cs
->cam
[i
] |= (u64
)addr
[i
] << (8 * (cs
->cam_index
));
469 cs
->cam_mask
|= (1ULL << cs
->cam_index
);
473 static void octeon_mgmt_set_rx_filtering(struct net_device
*netdev
)
475 struct octeon_mgmt
*p
= netdev_priv(netdev
);
477 union cvmx_agl_gmx_rxx_adr_ctl adr_ctl
;
478 union cvmx_agl_gmx_prtx_cfg agl_gmx_prtx
;
480 unsigned int prev_packet_enable
;
481 unsigned int cam_mode
= 1; /* 1 - Accept on CAM match */
482 unsigned int multicast_mode
= 1; /* 1 - Reject all multicast. */
483 struct octeon_mgmt_cam_state cam_state
;
484 struct netdev_hw_addr
*ha
;
485 int available_cam_entries
;
487 memset(&cam_state
, 0, sizeof(cam_state
));
489 if ((netdev
->flags
& IFF_PROMISC
) || netdev
->uc
.count
> 7) {
491 available_cam_entries
= 8;
494 * One CAM entry for the primary address, leaves seven
495 * for the secondary addresses.
497 available_cam_entries
= 7 - netdev
->uc
.count
;
500 if (netdev
->flags
& IFF_MULTICAST
) {
501 if (cam_mode
== 0 || (netdev
->flags
& IFF_ALLMULTI
) ||
502 netdev_mc_count(netdev
) > available_cam_entries
)
503 multicast_mode
= 2; /* 2 - Accept all multicast. */
505 multicast_mode
= 0; /* 0 - Use CAM. */
509 /* Add primary address. */
510 octeon_mgmt_cam_state_add(&cam_state
, netdev
->dev_addr
);
511 netdev_for_each_uc_addr(ha
, netdev
)
512 octeon_mgmt_cam_state_add(&cam_state
, ha
->addr
);
514 if (multicast_mode
== 0) {
515 netdev_for_each_mc_addr(ha
, netdev
)
516 octeon_mgmt_cam_state_add(&cam_state
, ha
->addr
);
519 spin_lock_irqsave(&p
->lock
, flags
);
521 /* Disable packet I/O. */
522 agl_gmx_prtx
.u64
= cvmx_read_csr(CVMX_AGL_GMX_PRTX_CFG(port
));
523 prev_packet_enable
= agl_gmx_prtx
.s
.en
;
524 agl_gmx_prtx
.s
.en
= 0;
525 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
), agl_gmx_prtx
.u64
);
528 adr_ctl
.s
.cam_mode
= cam_mode
;
529 adr_ctl
.s
.mcst
= multicast_mode
;
530 adr_ctl
.s
.bcst
= 1; /* Allow broadcast */
532 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CTL(port
), adr_ctl
.u64
);
534 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM0(port
), cam_state
.cam
[0]);
535 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM1(port
), cam_state
.cam
[1]);
536 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM2(port
), cam_state
.cam
[2]);
537 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM3(port
), cam_state
.cam
[3]);
538 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM4(port
), cam_state
.cam
[4]);
539 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM5(port
), cam_state
.cam
[5]);
540 cvmx_write_csr(CVMX_AGL_GMX_RXX_ADR_CAM_EN(port
), cam_state
.cam_mask
);
542 /* Restore packet I/O. */
543 agl_gmx_prtx
.s
.en
= prev_packet_enable
;
544 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
), agl_gmx_prtx
.u64
);
546 spin_unlock_irqrestore(&p
->lock
, flags
);
549 static int octeon_mgmt_set_mac_address(struct net_device
*netdev
, void *addr
)
551 struct sockaddr
*sa
= addr
;
553 if (!is_valid_ether_addr(sa
->sa_data
))
554 return -EADDRNOTAVAIL
;
556 memcpy(netdev
->dev_addr
, sa
->sa_data
, ETH_ALEN
);
558 octeon_mgmt_set_rx_filtering(netdev
);
563 static int octeon_mgmt_change_mtu(struct net_device
*netdev
, int new_mtu
)
565 struct octeon_mgmt
*p
= netdev_priv(netdev
);
567 int size_without_fcs
= new_mtu
+ OCTEON_MGMT_RX_HEADROOM
;
570 * Limit the MTU to make sure the ethernet packets are between
571 * 64 bytes and 16383 bytes.
573 if (size_without_fcs
< 64 || size_without_fcs
> 16383) {
574 dev_warn(p
->dev
, "MTU must be between %d and %d.\n",
575 64 - OCTEON_MGMT_RX_HEADROOM
,
576 16383 - OCTEON_MGMT_RX_HEADROOM
);
580 netdev
->mtu
= new_mtu
;
582 cvmx_write_csr(CVMX_AGL_GMX_RXX_FRM_MAX(port
), size_without_fcs
);
583 cvmx_write_csr(CVMX_AGL_GMX_RXX_JABBER(port
),
584 (size_without_fcs
+ 7) & 0xfff8);
589 static irqreturn_t
octeon_mgmt_interrupt(int cpl
, void *dev_id
)
591 struct net_device
*netdev
= dev_id
;
592 struct octeon_mgmt
*p
= netdev_priv(netdev
);
594 union cvmx_mixx_isr mixx_isr
;
596 mixx_isr
.u64
= cvmx_read_csr(CVMX_MIXX_ISR(port
));
598 /* Clear any pending interrupts */
599 cvmx_write_csr(CVMX_MIXX_ISR(port
), mixx_isr
.u64
);
600 cvmx_read_csr(CVMX_MIXX_ISR(port
));
602 if (mixx_isr
.s
.irthresh
) {
603 octeon_mgmt_disable_rx_irq(p
);
604 napi_schedule(&p
->napi
);
606 if (mixx_isr
.s
.orthresh
) {
607 octeon_mgmt_disable_tx_irq(p
);
608 tasklet_schedule(&p
->tx_clean_tasklet
);
614 static int octeon_mgmt_ioctl(struct net_device
*netdev
,
615 struct ifreq
*rq
, int cmd
)
617 struct octeon_mgmt
*p
= netdev_priv(netdev
);
619 if (!netif_running(netdev
))
625 return phy_mii_ioctl(p
->phydev
, rq
, cmd
);
628 static void octeon_mgmt_adjust_link(struct net_device
*netdev
)
630 struct octeon_mgmt
*p
= netdev_priv(netdev
);
632 union cvmx_agl_gmx_prtx_cfg prtx_cfg
;
634 int link_changed
= 0;
636 spin_lock_irqsave(&p
->lock
, flags
);
637 if (p
->phydev
->link
) {
640 if (p
->last_duplex
!= p
->phydev
->duplex
) {
641 p
->last_duplex
= p
->phydev
->duplex
;
643 cvmx_read_csr(CVMX_AGL_GMX_PRTX_CFG(port
));
644 prtx_cfg
.s
.duplex
= p
->phydev
->duplex
;
645 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
),
652 p
->last_link
= p
->phydev
->link
;
653 spin_unlock_irqrestore(&p
->lock
, flags
);
655 if (link_changed
!= 0) {
656 if (link_changed
> 0) {
657 netif_carrier_on(netdev
);
658 pr_info("%s: Link is up - %d/%s\n", netdev
->name
,
660 DUPLEX_FULL
== p
->phydev
->duplex
?
663 netif_carrier_off(netdev
);
664 pr_info("%s: Link is down\n", netdev
->name
);
669 static int octeon_mgmt_init_phy(struct net_device
*netdev
)
671 struct octeon_mgmt
*p
= netdev_priv(netdev
);
674 if (octeon_is_simulation()) {
675 /* No PHYs in the simulator. */
676 netif_carrier_on(netdev
);
680 snprintf(phy_id
, sizeof(phy_id
), PHY_ID_FMT
, "0", p
->port
);
682 p
->phydev
= phy_connect(netdev
, phy_id
, octeon_mgmt_adjust_link
, 0,
683 PHY_INTERFACE_MODE_MII
);
685 if (IS_ERR(p
->phydev
)) {
690 phy_start_aneg(p
->phydev
);
695 static int octeon_mgmt_open(struct net_device
*netdev
)
697 struct octeon_mgmt
*p
= netdev_priv(netdev
);
699 union cvmx_mixx_ctl mix_ctl
;
700 union cvmx_agl_gmx_inf_mode agl_gmx_inf_mode
;
701 union cvmx_mixx_oring1 oring1
;
702 union cvmx_mixx_iring1 iring1
;
703 union cvmx_agl_gmx_prtx_cfg prtx_cfg
;
704 union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl
;
705 union cvmx_mixx_irhwm mix_irhwm
;
706 union cvmx_mixx_orhwm mix_orhwm
;
707 union cvmx_mixx_intena mix_intena
;
710 /* Allocate ring buffers. */
711 p
->tx_ring
= kzalloc(ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
716 dma_map_single(p
->dev
, p
->tx_ring
,
717 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
720 p
->tx_next_clean
= 0;
721 p
->tx_current_fill
= 0;
724 p
->rx_ring
= kzalloc(ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
729 dma_map_single(p
->dev
, p
->rx_ring
,
730 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
735 p
->rx_current_fill
= 0;
737 octeon_mgmt_reset_hw(p
);
739 mix_ctl
.u64
= cvmx_read_csr(CVMX_MIXX_CTL(port
));
741 /* Bring it out of reset if needed. */
742 if (mix_ctl
.s
.reset
) {
744 cvmx_write_csr(CVMX_MIXX_CTL(port
), mix_ctl
.u64
);
746 mix_ctl
.u64
= cvmx_read_csr(CVMX_MIXX_CTL(port
));
747 } while (mix_ctl
.s
.reset
);
750 agl_gmx_inf_mode
.u64
= 0;
751 agl_gmx_inf_mode
.s
.en
= 1;
752 cvmx_write_csr(CVMX_AGL_GMX_INF_MODE
, agl_gmx_inf_mode
.u64
);
755 oring1
.s
.obase
= p
->tx_ring_handle
>> 3;
756 oring1
.s
.osize
= OCTEON_MGMT_TX_RING_SIZE
;
757 cvmx_write_csr(CVMX_MIXX_ORING1(port
), oring1
.u64
);
760 iring1
.s
.ibase
= p
->rx_ring_handle
>> 3;
761 iring1
.s
.isize
= OCTEON_MGMT_RX_RING_SIZE
;
762 cvmx_write_csr(CVMX_MIXX_IRING1(port
), iring1
.u64
);
764 /* Disable packet I/O. */
765 prtx_cfg
.u64
= cvmx_read_csr(CVMX_AGL_GMX_PRTX_CFG(port
));
767 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
), prtx_cfg
.u64
);
769 memcpy(sa
.sa_data
, netdev
->dev_addr
, ETH_ALEN
);
770 octeon_mgmt_set_mac_address(netdev
, &sa
);
772 octeon_mgmt_change_mtu(netdev
, netdev
->mtu
);
775 * Enable the port HW. Packets are not allowed until
776 * cvmx_mgmt_port_enable() is called.
779 mix_ctl
.s
.crc_strip
= 1; /* Strip the ending CRC */
780 mix_ctl
.s
.en
= 1; /* Enable the port */
781 mix_ctl
.s
.nbtarb
= 0; /* Arbitration mode */
782 /* MII CB-request FIFO programmable high watermark */
783 mix_ctl
.s
.mrq_hwm
= 1;
784 cvmx_write_csr(CVMX_MIXX_CTL(port
), mix_ctl
.u64
);
786 if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X
)
787 || OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X
)) {
789 * Force compensation values, as they are not
790 * determined properly by HW
792 union cvmx_agl_gmx_drv_ctl drv_ctl
;
794 drv_ctl
.u64
= cvmx_read_csr(CVMX_AGL_GMX_DRV_CTL
);
796 drv_ctl
.s
.byp_en1
= 1;
800 drv_ctl
.s
.byp_en
= 1;
804 cvmx_write_csr(CVMX_AGL_GMX_DRV_CTL
, drv_ctl
.u64
);
807 octeon_mgmt_rx_fill_ring(netdev
);
809 /* Clear statistics. */
811 cvmx_write_csr(CVMX_AGL_GMX_RXX_STATS_CTL(port
), 1);
812 cvmx_write_csr(CVMX_AGL_GMX_RXX_STATS_PKTS_DRP(port
), 0);
813 cvmx_write_csr(CVMX_AGL_GMX_RXX_STATS_PKTS_BAD(port
), 0);
815 cvmx_write_csr(CVMX_AGL_GMX_TXX_STATS_CTL(port
), 1);
816 cvmx_write_csr(CVMX_AGL_GMX_TXX_STAT0(port
), 0);
817 cvmx_write_csr(CVMX_AGL_GMX_TXX_STAT1(port
), 0);
819 /* Clear any pending interrupts */
820 cvmx_write_csr(CVMX_MIXX_ISR(port
), cvmx_read_csr(CVMX_MIXX_ISR(port
)));
822 if (request_irq(p
->irq
, octeon_mgmt_interrupt
, 0, netdev
->name
,
824 dev_err(p
->dev
, "request_irq(%d) failed.\n", p
->irq
);
828 /* Interrupt every single RX packet */
830 mix_irhwm
.s
.irhwm
= 0;
831 cvmx_write_csr(CVMX_MIXX_IRHWM(port
), mix_irhwm
.u64
);
833 /* Interrupt when we have 1 or more packets to clean. */
835 mix_orhwm
.s
.orhwm
= 1;
836 cvmx_write_csr(CVMX_MIXX_ORHWM(port
), mix_orhwm
.u64
);
838 /* Enable receive and transmit interrupts */
840 mix_intena
.s
.ithena
= 1;
841 mix_intena
.s
.othena
= 1;
842 cvmx_write_csr(CVMX_MIXX_INTENA(port
), mix_intena
.u64
);
845 /* Enable packet I/O. */
848 rxx_frm_ctl
.s
.pre_align
= 1;
850 * When set, disables the length check for non-min sized pkts
851 * with padding in the client data.
853 rxx_frm_ctl
.s
.pad_len
= 1;
854 /* When set, disables the length check for VLAN pkts */
855 rxx_frm_ctl
.s
.vlan_len
= 1;
856 /* When set, PREAMBLE checking is less strict */
857 rxx_frm_ctl
.s
.pre_free
= 1;
858 /* Control Pause Frames can match station SMAC */
859 rxx_frm_ctl
.s
.ctl_smac
= 0;
860 /* Control Pause Frames can match globally assign Multicast address */
861 rxx_frm_ctl
.s
.ctl_mcst
= 1;
862 /* Forward pause information to TX block */
863 rxx_frm_ctl
.s
.ctl_bck
= 1;
864 /* Drop Control Pause Frames */
865 rxx_frm_ctl
.s
.ctl_drp
= 1;
866 /* Strip off the preamble */
867 rxx_frm_ctl
.s
.pre_strp
= 1;
869 * This port is configured to send PREAMBLE+SFD to begin every
870 * frame. GMX checks that the PREAMBLE is sent correctly.
872 rxx_frm_ctl
.s
.pre_chk
= 1;
873 cvmx_write_csr(CVMX_AGL_GMX_RXX_FRM_CTL(port
), rxx_frm_ctl
.u64
);
875 /* Enable the AGL block */
876 agl_gmx_inf_mode
.u64
= 0;
877 agl_gmx_inf_mode
.s
.en
= 1;
878 cvmx_write_csr(CVMX_AGL_GMX_INF_MODE
, agl_gmx_inf_mode
.u64
);
880 /* Configure the port duplex and enables */
881 prtx_cfg
.u64
= cvmx_read_csr(CVMX_AGL_GMX_PRTX_CFG(port
));
882 prtx_cfg
.s
.tx_en
= 1;
883 prtx_cfg
.s
.rx_en
= 1;
886 prtx_cfg
.s
.duplex
= p
->last_duplex
;
887 cvmx_write_csr(CVMX_AGL_GMX_PRTX_CFG(port
), prtx_cfg
.u64
);
890 netif_carrier_off(netdev
);
892 if (octeon_mgmt_init_phy(netdev
)) {
893 dev_err(p
->dev
, "Cannot initialize PHY.\n");
897 netif_wake_queue(netdev
);
898 napi_enable(&p
->napi
);
902 octeon_mgmt_reset_hw(p
);
903 dma_unmap_single(p
->dev
, p
->rx_ring_handle
,
904 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
908 dma_unmap_single(p
->dev
, p
->tx_ring_handle
,
909 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
915 static int octeon_mgmt_stop(struct net_device
*netdev
)
917 struct octeon_mgmt
*p
= netdev_priv(netdev
);
919 napi_disable(&p
->napi
);
920 netif_stop_queue(netdev
);
923 phy_disconnect(p
->phydev
);
925 netif_carrier_off(netdev
);
927 octeon_mgmt_reset_hw(p
);
929 free_irq(p
->irq
, netdev
);
931 /* dma_unmap is a nop on Octeon, so just free everything. */
932 skb_queue_purge(&p
->tx_list
);
933 skb_queue_purge(&p
->rx_list
);
935 dma_unmap_single(p
->dev
, p
->rx_ring_handle
,
936 ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE
),
940 dma_unmap_single(p
->dev
, p
->tx_ring_handle
,
941 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
948 static int octeon_mgmt_xmit(struct sk_buff
*skb
, struct net_device
*netdev
)
950 struct octeon_mgmt
*p
= netdev_priv(netdev
);
952 union mgmt_port_ring_entry re
;
954 int rv
= NETDEV_TX_BUSY
;
958 re
.s
.addr
= dma_map_single(p
->dev
, skb
->data
,
962 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
964 if (unlikely(p
->tx_current_fill
>= ring_max_fill(OCTEON_MGMT_TX_RING_SIZE
) - 1)) {
965 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
966 netif_stop_queue(netdev
);
967 spin_lock_irqsave(&p
->tx_list
.lock
, flags
);
970 if (unlikely(p
->tx_current_fill
>=
971 ring_max_fill(OCTEON_MGMT_TX_RING_SIZE
))) {
972 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
973 dma_unmap_single(p
->dev
, re
.s
.addr
, re
.s
.len
,
978 __skb_queue_tail(&p
->tx_list
, skb
);
980 /* Put it in the ring. */
981 p
->tx_ring
[p
->tx_next
] = re
.d64
;
982 p
->tx_next
= (p
->tx_next
+ 1) % OCTEON_MGMT_TX_RING_SIZE
;
983 p
->tx_current_fill
++;
985 spin_unlock_irqrestore(&p
->tx_list
.lock
, flags
);
987 dma_sync_single_for_device(p
->dev
, p
->tx_ring_handle
,
988 ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE
),
991 netdev
->stats
.tx_packets
++;
992 netdev
->stats
.tx_bytes
+= skb
->len
;
995 cvmx_write_csr(CVMX_MIXX_ORING2(port
), 1);
999 octeon_mgmt_update_tx_stats(netdev
);
1003 #ifdef CONFIG_NET_POLL_CONTROLLER
1004 static void octeon_mgmt_poll_controller(struct net_device
*netdev
)
1006 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1008 octeon_mgmt_receive_packets(p
, 16);
1009 octeon_mgmt_update_rx_stats(netdev
);
1013 static void octeon_mgmt_get_drvinfo(struct net_device
*netdev
,
1014 struct ethtool_drvinfo
*info
)
1016 strncpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
1017 strncpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
1018 strncpy(info
->fw_version
, "N/A", sizeof(info
->fw_version
));
1019 strncpy(info
->bus_info
, "N/A", sizeof(info
->bus_info
));
1021 info
->testinfo_len
= 0;
1022 info
->regdump_len
= 0;
1023 info
->eedump_len
= 0;
1026 static int octeon_mgmt_get_settings(struct net_device
*netdev
,
1027 struct ethtool_cmd
*cmd
)
1029 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1032 return phy_ethtool_gset(p
->phydev
, cmd
);
1037 static int octeon_mgmt_set_settings(struct net_device
*netdev
,
1038 struct ethtool_cmd
*cmd
)
1040 struct octeon_mgmt
*p
= netdev_priv(netdev
);
1042 if (!capable(CAP_NET_ADMIN
))
1046 return phy_ethtool_sset(p
->phydev
, cmd
);
1051 static const struct ethtool_ops octeon_mgmt_ethtool_ops
= {
1052 .get_drvinfo
= octeon_mgmt_get_drvinfo
,
1053 .get_link
= ethtool_op_get_link
,
1054 .get_settings
= octeon_mgmt_get_settings
,
1055 .set_settings
= octeon_mgmt_set_settings
1058 static const struct net_device_ops octeon_mgmt_ops
= {
1059 .ndo_open
= octeon_mgmt_open
,
1060 .ndo_stop
= octeon_mgmt_stop
,
1061 .ndo_start_xmit
= octeon_mgmt_xmit
,
1062 .ndo_set_rx_mode
= octeon_mgmt_set_rx_filtering
,
1063 .ndo_set_mac_address
= octeon_mgmt_set_mac_address
,
1064 .ndo_do_ioctl
= octeon_mgmt_ioctl
,
1065 .ndo_change_mtu
= octeon_mgmt_change_mtu
,
1066 #ifdef CONFIG_NET_POLL_CONTROLLER
1067 .ndo_poll_controller
= octeon_mgmt_poll_controller
,
1071 static int __devinit
octeon_mgmt_probe(struct platform_device
*pdev
)
1073 struct resource
*res_irq
;
1074 struct net_device
*netdev
;
1075 struct octeon_mgmt
*p
;
1078 netdev
= alloc_etherdev(sizeof(struct octeon_mgmt
));
1082 dev_set_drvdata(&pdev
->dev
, netdev
);
1083 p
= netdev_priv(netdev
);
1084 netif_napi_add(netdev
, &p
->napi
, octeon_mgmt_napi_poll
,
1085 OCTEON_MGMT_NAPI_WEIGHT
);
1088 p
->dev
= &pdev
->dev
;
1091 snprintf(netdev
->name
, IFNAMSIZ
, "mgmt%d", p
->port
);
1093 res_irq
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1097 p
->irq
= res_irq
->start
;
1098 spin_lock_init(&p
->lock
);
1100 skb_queue_head_init(&p
->tx_list
);
1101 skb_queue_head_init(&p
->rx_list
);
1102 tasklet_init(&p
->tx_clean_tasklet
,
1103 octeon_mgmt_clean_tx_tasklet
, (unsigned long)p
);
1105 netdev
->priv_flags
|= IFF_UNICAST_FLT
;
1107 netdev
->netdev_ops
= &octeon_mgmt_ops
;
1108 netdev
->ethtool_ops
= &octeon_mgmt_ethtool_ops
;
1110 /* The mgmt ports get the first N MACs. */
1111 for (i
= 0; i
< 6; i
++)
1112 netdev
->dev_addr
[i
] = octeon_bootinfo
->mac_addr_base
[i
];
1113 netdev
->dev_addr
[5] += p
->port
;
1115 if (p
->port
>= octeon_bootinfo
->mac_addr_count
)
1117 "Error %s: Using MAC outside of the assigned range: %pM\n",
1118 netdev
->name
, netdev
->dev_addr
);
1120 if (register_netdev(netdev
))
1123 dev_info(&pdev
->dev
, "Version " DRV_VERSION
"\n");
1126 free_netdev(netdev
);
1130 static int __devexit
octeon_mgmt_remove(struct platform_device
*pdev
)
1132 struct net_device
*netdev
= dev_get_drvdata(&pdev
->dev
);
1134 unregister_netdev(netdev
);
1135 free_netdev(netdev
);
1139 static struct platform_driver octeon_mgmt_driver
= {
1141 .name
= "octeon_mgmt",
1142 .owner
= THIS_MODULE
,
1144 .probe
= octeon_mgmt_probe
,
1145 .remove
= __devexit_p(octeon_mgmt_remove
),
1148 extern void octeon_mdiobus_force_mod_depencency(void);
1150 static int __init
octeon_mgmt_mod_init(void)
1152 /* Force our mdiobus driver module to be loaded first. */
1153 octeon_mdiobus_force_mod_depencency();
1154 return platform_driver_register(&octeon_mgmt_driver
);
1157 static void __exit
octeon_mgmt_mod_exit(void)
1159 platform_driver_unregister(&octeon_mgmt_driver
);
1162 module_init(octeon_mgmt_mod_init
);
1163 module_exit(octeon_mgmt_mod_exit
);
1165 MODULE_DESCRIPTION(DRV_DESCRIPTION
);
1166 MODULE_AUTHOR("David Daney");
1167 MODULE_LICENSE("GPL");
1168 MODULE_VERSION(DRV_VERSION
);