David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1 | /* |
| 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 |
| 4 | * for more details. |
| 5 | * |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 6 | * Copyright (C) 2009-2012 Cavium, Inc |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 9 | #include <linux/platform_device.h> |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 10 | #include <linux/dma-mapping.h> |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 11 | #include <linux/etherdevice.h> |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 12 | #include <linux/capability.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/spinlock.h> |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 16 | #include <linux/if_vlan.h> |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 17 | #include <linux/of_mdio.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_net.h> |
| 20 | #include <linux/init.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 22 | #include <linux/phy.h> |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 23 | #include <linux/io.h> |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 24 | |
| 25 | #include <asm/octeon/octeon.h> |
| 26 | #include <asm/octeon/cvmx-mixx-defs.h> |
| 27 | #include <asm/octeon/cvmx-agl-defs.h> |
| 28 | |
| 29 | #define DRV_NAME "octeon_mgmt" |
| 30 | #define DRV_VERSION "2.0" |
| 31 | #define DRV_DESCRIPTION \ |
| 32 | "Cavium Networks Octeon MII (management) port Network Driver" |
| 33 | |
| 34 | #define OCTEON_MGMT_NAPI_WEIGHT 16 |
| 35 | |
| 36 | /* |
| 37 | * Ring sizes that are powers of two allow for more efficient modulo |
| 38 | * opertions. |
| 39 | */ |
| 40 | #define OCTEON_MGMT_RX_RING_SIZE 512 |
| 41 | #define OCTEON_MGMT_TX_RING_SIZE 128 |
| 42 | |
| 43 | /* Allow 8 bytes for vlan and FCS. */ |
| 44 | #define OCTEON_MGMT_RX_HEADROOM (ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN) |
| 45 | |
| 46 | union mgmt_port_ring_entry { |
| 47 | u64 d64; |
| 48 | struct { |
| 49 | u64 reserved_62_63:2; |
| 50 | /* Length of the buffer/packet in bytes */ |
| 51 | u64 len:14; |
| 52 | /* For TX, signals that the packet should be timestamped */ |
| 53 | u64 tstamp:1; |
| 54 | /* The RX error code */ |
| 55 | u64 code:7; |
| 56 | #define RING_ENTRY_CODE_DONE 0xf |
| 57 | #define RING_ENTRY_CODE_MORE 0x10 |
| 58 | /* Physical address of the buffer */ |
| 59 | u64 addr:40; |
| 60 | } s; |
| 61 | }; |
| 62 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 63 | #define MIX_ORING1 0x0 |
| 64 | #define MIX_ORING2 0x8 |
| 65 | #define MIX_IRING1 0x10 |
| 66 | #define MIX_IRING2 0x18 |
| 67 | #define MIX_CTL 0x20 |
| 68 | #define MIX_IRHWM 0x28 |
| 69 | #define MIX_IRCNT 0x30 |
| 70 | #define MIX_ORHWM 0x38 |
| 71 | #define MIX_ORCNT 0x40 |
| 72 | #define MIX_ISR 0x48 |
| 73 | #define MIX_INTENA 0x50 |
| 74 | #define MIX_REMCNT 0x58 |
| 75 | #define MIX_BIST 0x78 |
| 76 | |
| 77 | #define AGL_GMX_PRT_CFG 0x10 |
| 78 | #define AGL_GMX_RX_FRM_CTL 0x18 |
| 79 | #define AGL_GMX_RX_FRM_MAX 0x30 |
| 80 | #define AGL_GMX_RX_JABBER 0x38 |
| 81 | #define AGL_GMX_RX_STATS_CTL 0x50 |
| 82 | |
| 83 | #define AGL_GMX_RX_STATS_PKTS_DRP 0xb0 |
| 84 | #define AGL_GMX_RX_STATS_OCTS_DRP 0xb8 |
| 85 | #define AGL_GMX_RX_STATS_PKTS_BAD 0xc0 |
| 86 | |
| 87 | #define AGL_GMX_RX_ADR_CTL 0x100 |
| 88 | #define AGL_GMX_RX_ADR_CAM_EN 0x108 |
| 89 | #define AGL_GMX_RX_ADR_CAM0 0x180 |
| 90 | #define AGL_GMX_RX_ADR_CAM1 0x188 |
| 91 | #define AGL_GMX_RX_ADR_CAM2 0x190 |
| 92 | #define AGL_GMX_RX_ADR_CAM3 0x198 |
| 93 | #define AGL_GMX_RX_ADR_CAM4 0x1a0 |
| 94 | #define AGL_GMX_RX_ADR_CAM5 0x1a8 |
| 95 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 96 | #define AGL_GMX_TX_CLK 0x208 |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 97 | #define AGL_GMX_TX_STATS_CTL 0x268 |
| 98 | #define AGL_GMX_TX_CTL 0x270 |
| 99 | #define AGL_GMX_TX_STAT0 0x280 |
| 100 | #define AGL_GMX_TX_STAT1 0x288 |
| 101 | #define AGL_GMX_TX_STAT2 0x290 |
| 102 | #define AGL_GMX_TX_STAT3 0x298 |
| 103 | #define AGL_GMX_TX_STAT4 0x2a0 |
| 104 | #define AGL_GMX_TX_STAT5 0x2a8 |
| 105 | #define AGL_GMX_TX_STAT6 0x2b0 |
| 106 | #define AGL_GMX_TX_STAT7 0x2b8 |
| 107 | #define AGL_GMX_TX_STAT8 0x2c0 |
| 108 | #define AGL_GMX_TX_STAT9 0x2c8 |
| 109 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 110 | struct octeon_mgmt { |
| 111 | struct net_device *netdev; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 112 | u64 mix; |
| 113 | u64 agl; |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 114 | u64 agl_prt_ctl; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 115 | int port; |
| 116 | int irq; |
| 117 | u64 *tx_ring; |
| 118 | dma_addr_t tx_ring_handle; |
| 119 | unsigned int tx_next; |
| 120 | unsigned int tx_next_clean; |
| 121 | unsigned int tx_current_fill; |
| 122 | /* The tx_list lock also protects the ring related variables */ |
| 123 | struct sk_buff_head tx_list; |
| 124 | |
| 125 | /* RX variables only touched in napi_poll. No locking necessary. */ |
| 126 | u64 *rx_ring; |
| 127 | dma_addr_t rx_ring_handle; |
| 128 | unsigned int rx_next; |
| 129 | unsigned int rx_next_fill; |
| 130 | unsigned int rx_current_fill; |
| 131 | struct sk_buff_head rx_list; |
| 132 | |
| 133 | spinlock_t lock; |
| 134 | unsigned int last_duplex; |
| 135 | unsigned int last_link; |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 136 | unsigned int last_speed; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 137 | struct device *dev; |
| 138 | struct napi_struct napi; |
| 139 | struct tasklet_struct tx_clean_tasklet; |
| 140 | struct phy_device *phydev; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 141 | struct device_node *phy_np; |
| 142 | resource_size_t mix_phys; |
| 143 | resource_size_t mix_size; |
| 144 | resource_size_t agl_phys; |
| 145 | resource_size_t agl_size; |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 146 | resource_size_t agl_prt_ctl_phys; |
| 147 | resource_size_t agl_prt_ctl_size; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static void octeon_mgmt_set_rx_irq(struct octeon_mgmt *p, int enable) |
| 151 | { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 152 | union cvmx_mixx_intena mix_intena; |
| 153 | unsigned long flags; |
| 154 | |
| 155 | spin_lock_irqsave(&p->lock, flags); |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 156 | mix_intena.u64 = cvmx_read_csr(p->mix + MIX_INTENA); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 157 | mix_intena.s.ithena = enable ? 1 : 0; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 158 | cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 159 | spin_unlock_irqrestore(&p->lock, flags); |
| 160 | } |
| 161 | |
| 162 | static void octeon_mgmt_set_tx_irq(struct octeon_mgmt *p, int enable) |
| 163 | { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 164 | union cvmx_mixx_intena mix_intena; |
| 165 | unsigned long flags; |
| 166 | |
| 167 | spin_lock_irqsave(&p->lock, flags); |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 168 | mix_intena.u64 = cvmx_read_csr(p->mix + MIX_INTENA); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 169 | mix_intena.s.othena = enable ? 1 : 0; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 170 | cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 171 | spin_unlock_irqrestore(&p->lock, flags); |
| 172 | } |
| 173 | |
| 174 | static inline void octeon_mgmt_enable_rx_irq(struct octeon_mgmt *p) |
| 175 | { |
| 176 | octeon_mgmt_set_rx_irq(p, 1); |
| 177 | } |
| 178 | |
| 179 | static inline void octeon_mgmt_disable_rx_irq(struct octeon_mgmt *p) |
| 180 | { |
| 181 | octeon_mgmt_set_rx_irq(p, 0); |
| 182 | } |
| 183 | |
| 184 | static inline void octeon_mgmt_enable_tx_irq(struct octeon_mgmt *p) |
| 185 | { |
| 186 | octeon_mgmt_set_tx_irq(p, 1); |
| 187 | } |
| 188 | |
| 189 | static inline void octeon_mgmt_disable_tx_irq(struct octeon_mgmt *p) |
| 190 | { |
| 191 | octeon_mgmt_set_tx_irq(p, 0); |
| 192 | } |
| 193 | |
| 194 | static unsigned int ring_max_fill(unsigned int ring_size) |
| 195 | { |
| 196 | return ring_size - 8; |
| 197 | } |
| 198 | |
| 199 | static unsigned int ring_size_to_bytes(unsigned int ring_size) |
| 200 | { |
| 201 | return ring_size * sizeof(union mgmt_port_ring_entry); |
| 202 | } |
| 203 | |
| 204 | static void octeon_mgmt_rx_fill_ring(struct net_device *netdev) |
| 205 | { |
| 206 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 207 | |
| 208 | while (p->rx_current_fill < ring_max_fill(OCTEON_MGMT_RX_RING_SIZE)) { |
| 209 | unsigned int size; |
| 210 | union mgmt_port_ring_entry re; |
| 211 | struct sk_buff *skb; |
| 212 | |
| 213 | /* CN56XX pass 1 needs 8 bytes of padding. */ |
| 214 | size = netdev->mtu + OCTEON_MGMT_RX_HEADROOM + 8 + NET_IP_ALIGN; |
| 215 | |
| 216 | skb = netdev_alloc_skb(netdev, size); |
| 217 | if (!skb) |
| 218 | break; |
| 219 | skb_reserve(skb, NET_IP_ALIGN); |
| 220 | __skb_queue_tail(&p->rx_list, skb); |
| 221 | |
| 222 | re.d64 = 0; |
| 223 | re.s.len = size; |
| 224 | re.s.addr = dma_map_single(p->dev, skb->data, |
| 225 | size, |
| 226 | DMA_FROM_DEVICE); |
| 227 | |
| 228 | /* Put it in the ring. */ |
| 229 | p->rx_ring[p->rx_next_fill] = re.d64; |
| 230 | dma_sync_single_for_device(p->dev, p->rx_ring_handle, |
| 231 | ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE), |
| 232 | DMA_BIDIRECTIONAL); |
| 233 | p->rx_next_fill = |
| 234 | (p->rx_next_fill + 1) % OCTEON_MGMT_RX_RING_SIZE; |
| 235 | p->rx_current_fill++; |
| 236 | /* Ring the bell. */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 237 | cvmx_write_csr(p->mix + MIX_IRING2, 1); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
| 241 | static void octeon_mgmt_clean_tx_buffers(struct octeon_mgmt *p) |
| 242 | { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 243 | union cvmx_mixx_orcnt mix_orcnt; |
| 244 | union mgmt_port_ring_entry re; |
| 245 | struct sk_buff *skb; |
| 246 | int cleaned = 0; |
| 247 | unsigned long flags; |
| 248 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 249 | mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 250 | while (mix_orcnt.s.orcnt) { |
David Daney | 4d30b80 | 2010-05-05 13:03:09 +0000 | [diff] [blame] | 251 | spin_lock_irqsave(&p->tx_list.lock, flags); |
| 252 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 253 | mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT); |
David Daney | 4d30b80 | 2010-05-05 13:03:09 +0000 | [diff] [blame] | 254 | |
| 255 | if (mix_orcnt.s.orcnt == 0) { |
| 256 | spin_unlock_irqrestore(&p->tx_list.lock, flags); |
| 257 | break; |
| 258 | } |
| 259 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 260 | dma_sync_single_for_cpu(p->dev, p->tx_ring_handle, |
| 261 | ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE), |
| 262 | DMA_BIDIRECTIONAL); |
| 263 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 264 | re.d64 = p->tx_ring[p->tx_next_clean]; |
| 265 | p->tx_next_clean = |
| 266 | (p->tx_next_clean + 1) % OCTEON_MGMT_TX_RING_SIZE; |
| 267 | skb = __skb_dequeue(&p->tx_list); |
| 268 | |
| 269 | mix_orcnt.u64 = 0; |
| 270 | mix_orcnt.s.orcnt = 1; |
| 271 | |
| 272 | /* Acknowledge to hardware that we have the buffer. */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 273 | cvmx_write_csr(p->mix + MIX_ORCNT, mix_orcnt.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 274 | p->tx_current_fill--; |
| 275 | |
| 276 | spin_unlock_irqrestore(&p->tx_list.lock, flags); |
| 277 | |
| 278 | dma_unmap_single(p->dev, re.s.addr, re.s.len, |
| 279 | DMA_TO_DEVICE); |
| 280 | dev_kfree_skb_any(skb); |
| 281 | cleaned++; |
| 282 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 283 | mix_orcnt.u64 = cvmx_read_csr(p->mix + MIX_ORCNT); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | if (cleaned && netif_queue_stopped(p->netdev)) |
| 287 | netif_wake_queue(p->netdev); |
| 288 | } |
| 289 | |
| 290 | static void octeon_mgmt_clean_tx_tasklet(unsigned long arg) |
| 291 | { |
| 292 | struct octeon_mgmt *p = (struct octeon_mgmt *)arg; |
| 293 | octeon_mgmt_clean_tx_buffers(p); |
| 294 | octeon_mgmt_enable_tx_irq(p); |
| 295 | } |
| 296 | |
| 297 | static void octeon_mgmt_update_rx_stats(struct net_device *netdev) |
| 298 | { |
| 299 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 300 | unsigned long flags; |
| 301 | u64 drop, bad; |
| 302 | |
| 303 | /* These reads also clear the count registers. */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 304 | drop = cvmx_read_csr(p->agl + AGL_GMX_RX_STATS_PKTS_DRP); |
| 305 | bad = cvmx_read_csr(p->agl + AGL_GMX_RX_STATS_PKTS_BAD); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 306 | |
| 307 | if (drop || bad) { |
| 308 | /* Do an atomic update. */ |
| 309 | spin_lock_irqsave(&p->lock, flags); |
| 310 | netdev->stats.rx_errors += bad; |
| 311 | netdev->stats.rx_dropped += drop; |
| 312 | spin_unlock_irqrestore(&p->lock, flags); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | static void octeon_mgmt_update_tx_stats(struct net_device *netdev) |
| 317 | { |
| 318 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 319 | unsigned long flags; |
| 320 | |
| 321 | union cvmx_agl_gmx_txx_stat0 s0; |
| 322 | union cvmx_agl_gmx_txx_stat1 s1; |
| 323 | |
| 324 | /* These reads also clear the count registers. */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 325 | s0.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_STAT0); |
| 326 | s1.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_STAT1); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 327 | |
| 328 | if (s0.s.xsdef || s0.s.xscol || s1.s.scol || s1.s.mcol) { |
| 329 | /* Do an atomic update. */ |
| 330 | spin_lock_irqsave(&p->lock, flags); |
| 331 | netdev->stats.tx_errors += s0.s.xsdef + s0.s.xscol; |
| 332 | netdev->stats.collisions += s1.s.scol + s1.s.mcol; |
| 333 | spin_unlock_irqrestore(&p->lock, flags); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * Dequeue a receive skb and its corresponding ring entry. The ring |
| 339 | * entry is returned, *pskb is updated to point to the skb. |
| 340 | */ |
| 341 | static u64 octeon_mgmt_dequeue_rx_buffer(struct octeon_mgmt *p, |
| 342 | struct sk_buff **pskb) |
| 343 | { |
| 344 | union mgmt_port_ring_entry re; |
| 345 | |
| 346 | dma_sync_single_for_cpu(p->dev, p->rx_ring_handle, |
| 347 | ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE), |
| 348 | DMA_BIDIRECTIONAL); |
| 349 | |
| 350 | re.d64 = p->rx_ring[p->rx_next]; |
| 351 | p->rx_next = (p->rx_next + 1) % OCTEON_MGMT_RX_RING_SIZE; |
| 352 | p->rx_current_fill--; |
| 353 | *pskb = __skb_dequeue(&p->rx_list); |
| 354 | |
| 355 | dma_unmap_single(p->dev, re.s.addr, |
| 356 | ETH_FRAME_LEN + OCTEON_MGMT_RX_HEADROOM, |
| 357 | DMA_FROM_DEVICE); |
| 358 | |
| 359 | return re.d64; |
| 360 | } |
| 361 | |
| 362 | |
| 363 | static int octeon_mgmt_receive_one(struct octeon_mgmt *p) |
| 364 | { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 365 | struct net_device *netdev = p->netdev; |
| 366 | union cvmx_mixx_ircnt mix_ircnt; |
| 367 | union mgmt_port_ring_entry re; |
| 368 | struct sk_buff *skb; |
| 369 | struct sk_buff *skb2; |
| 370 | struct sk_buff *skb_new; |
| 371 | union mgmt_port_ring_entry re2; |
| 372 | int rc = 1; |
| 373 | |
| 374 | |
| 375 | re.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb); |
| 376 | if (likely(re.s.code == RING_ENTRY_CODE_DONE)) { |
| 377 | /* A good packet, send it up. */ |
| 378 | skb_put(skb, re.s.len); |
| 379 | good: |
| 380 | skb->protocol = eth_type_trans(skb, netdev); |
| 381 | netdev->stats.rx_packets++; |
| 382 | netdev->stats.rx_bytes += skb->len; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 383 | netif_receive_skb(skb); |
| 384 | rc = 0; |
| 385 | } else if (re.s.code == RING_ENTRY_CODE_MORE) { |
| 386 | /* |
| 387 | * Packet split across skbs. This can happen if we |
| 388 | * increase the MTU. Buffers that are already in the |
| 389 | * rx ring can then end up being too small. As the rx |
| 390 | * ring is refilled, buffers sized for the new MTU |
| 391 | * will be used and we should go back to the normal |
| 392 | * non-split case. |
| 393 | */ |
| 394 | skb_put(skb, re.s.len); |
| 395 | do { |
| 396 | re2.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb2); |
| 397 | if (re2.s.code != RING_ENTRY_CODE_MORE |
| 398 | && re2.s.code != RING_ENTRY_CODE_DONE) |
| 399 | goto split_error; |
| 400 | skb_put(skb2, re2.s.len); |
| 401 | skb_new = skb_copy_expand(skb, 0, skb2->len, |
| 402 | GFP_ATOMIC); |
| 403 | if (!skb_new) |
| 404 | goto split_error; |
| 405 | if (skb_copy_bits(skb2, 0, skb_tail_pointer(skb_new), |
| 406 | skb2->len)) |
| 407 | goto split_error; |
| 408 | skb_put(skb_new, skb2->len); |
| 409 | dev_kfree_skb_any(skb); |
| 410 | dev_kfree_skb_any(skb2); |
| 411 | skb = skb_new; |
| 412 | } while (re2.s.code == RING_ENTRY_CODE_MORE); |
| 413 | goto good; |
| 414 | } else { |
| 415 | /* Some other error, discard it. */ |
| 416 | dev_kfree_skb_any(skb); |
| 417 | /* |
| 418 | * Error statistics are accumulated in |
| 419 | * octeon_mgmt_update_rx_stats. |
| 420 | */ |
| 421 | } |
| 422 | goto done; |
| 423 | split_error: |
| 424 | /* Discard the whole mess. */ |
| 425 | dev_kfree_skb_any(skb); |
| 426 | dev_kfree_skb_any(skb2); |
| 427 | while (re2.s.code == RING_ENTRY_CODE_MORE) { |
| 428 | re2.d64 = octeon_mgmt_dequeue_rx_buffer(p, &skb2); |
| 429 | dev_kfree_skb_any(skb2); |
| 430 | } |
| 431 | netdev->stats.rx_errors++; |
| 432 | |
| 433 | done: |
| 434 | /* Tell the hardware we processed a packet. */ |
| 435 | mix_ircnt.u64 = 0; |
| 436 | mix_ircnt.s.ircnt = 1; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 437 | cvmx_write_csr(p->mix + MIX_IRCNT, mix_ircnt.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 438 | return rc; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static int octeon_mgmt_receive_packets(struct octeon_mgmt *p, int budget) |
| 442 | { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 443 | unsigned int work_done = 0; |
| 444 | union cvmx_mixx_ircnt mix_ircnt; |
| 445 | int rc; |
| 446 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 447 | mix_ircnt.u64 = cvmx_read_csr(p->mix + MIX_IRCNT); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 448 | while (work_done < budget && mix_ircnt.s.ircnt) { |
| 449 | |
| 450 | rc = octeon_mgmt_receive_one(p); |
| 451 | if (!rc) |
| 452 | work_done++; |
| 453 | |
| 454 | /* Check for more packets. */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 455 | mix_ircnt.u64 = cvmx_read_csr(p->mix + MIX_IRCNT); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | octeon_mgmt_rx_fill_ring(p->netdev); |
| 459 | |
| 460 | return work_done; |
| 461 | } |
| 462 | |
| 463 | static int octeon_mgmt_napi_poll(struct napi_struct *napi, int budget) |
| 464 | { |
| 465 | struct octeon_mgmt *p = container_of(napi, struct octeon_mgmt, napi); |
| 466 | struct net_device *netdev = p->netdev; |
| 467 | unsigned int work_done = 0; |
| 468 | |
| 469 | work_done = octeon_mgmt_receive_packets(p, budget); |
| 470 | |
| 471 | if (work_done < budget) { |
| 472 | /* We stopped because no more packets were available. */ |
| 473 | napi_complete(napi); |
| 474 | octeon_mgmt_enable_rx_irq(p); |
| 475 | } |
| 476 | octeon_mgmt_update_rx_stats(netdev); |
| 477 | |
| 478 | return work_done; |
| 479 | } |
| 480 | |
| 481 | /* Reset the hardware to clean state. */ |
| 482 | static void octeon_mgmt_reset_hw(struct octeon_mgmt *p) |
| 483 | { |
| 484 | union cvmx_mixx_ctl mix_ctl; |
| 485 | union cvmx_mixx_bist mix_bist; |
| 486 | union cvmx_agl_gmx_bist agl_gmx_bist; |
| 487 | |
| 488 | mix_ctl.u64 = 0; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 489 | cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 490 | do { |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 491 | mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 492 | } while (mix_ctl.s.busy); |
| 493 | mix_ctl.s.reset = 1; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 494 | cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64); |
| 495 | cvmx_read_csr(p->mix + MIX_CTL); |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 496 | octeon_io_clk_delay(64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 497 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 498 | mix_bist.u64 = cvmx_read_csr(p->mix + MIX_BIST); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 499 | if (mix_bist.u64) |
| 500 | dev_warn(p->dev, "MIX failed BIST (0x%016llx)\n", |
| 501 | (unsigned long long)mix_bist.u64); |
| 502 | |
| 503 | agl_gmx_bist.u64 = cvmx_read_csr(CVMX_AGL_GMX_BIST); |
| 504 | if (agl_gmx_bist.u64) |
| 505 | dev_warn(p->dev, "AGL failed BIST (0x%016llx)\n", |
| 506 | (unsigned long long)agl_gmx_bist.u64); |
| 507 | } |
| 508 | |
| 509 | struct octeon_mgmt_cam_state { |
| 510 | u64 cam[6]; |
| 511 | u64 cam_mask; |
| 512 | int cam_index; |
| 513 | }; |
| 514 | |
| 515 | static void octeon_mgmt_cam_state_add(struct octeon_mgmt_cam_state *cs, |
| 516 | unsigned char *addr) |
| 517 | { |
| 518 | int i; |
| 519 | |
| 520 | for (i = 0; i < 6; i++) |
| 521 | cs->cam[i] |= (u64)addr[i] << (8 * (cs->cam_index)); |
| 522 | cs->cam_mask |= (1ULL << cs->cam_index); |
| 523 | cs->cam_index++; |
| 524 | } |
| 525 | |
| 526 | static void octeon_mgmt_set_rx_filtering(struct net_device *netdev) |
| 527 | { |
| 528 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 529 | union cvmx_agl_gmx_rxx_adr_ctl adr_ctl; |
| 530 | union cvmx_agl_gmx_prtx_cfg agl_gmx_prtx; |
| 531 | unsigned long flags; |
| 532 | unsigned int prev_packet_enable; |
| 533 | unsigned int cam_mode = 1; /* 1 - Accept on CAM match */ |
| 534 | unsigned int multicast_mode = 1; /* 1 - Reject all multicast. */ |
| 535 | struct octeon_mgmt_cam_state cam_state; |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 536 | struct netdev_hw_addr *ha; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 537 | int available_cam_entries; |
| 538 | |
| 539 | memset(&cam_state, 0, sizeof(cam_state)); |
| 540 | |
David Daney | 62538d2 | 2010-05-05 13:03:08 +0000 | [diff] [blame] | 541 | if ((netdev->flags & IFF_PROMISC) || netdev->uc.count > 7) { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 542 | cam_mode = 0; |
| 543 | available_cam_entries = 8; |
| 544 | } else { |
| 545 | /* |
| 546 | * One CAM entry for the primary address, leaves seven |
| 547 | * for the secondary addresses. |
| 548 | */ |
David Daney | 62538d2 | 2010-05-05 13:03:08 +0000 | [diff] [blame] | 549 | available_cam_entries = 7 - netdev->uc.count; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | if (netdev->flags & IFF_MULTICAST) { |
Jiri Pirko | 4cd24ea | 2010-02-08 04:30:35 +0000 | [diff] [blame] | 553 | if (cam_mode == 0 || (netdev->flags & IFF_ALLMULTI) || |
| 554 | netdev_mc_count(netdev) > available_cam_entries) |
David Daney | 62538d2 | 2010-05-05 13:03:08 +0000 | [diff] [blame] | 555 | multicast_mode = 2; /* 2 - Accept all multicast. */ |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 556 | else |
| 557 | multicast_mode = 0; /* 0 - Use CAM. */ |
| 558 | } |
| 559 | |
| 560 | if (cam_mode == 1) { |
| 561 | /* Add primary address. */ |
| 562 | octeon_mgmt_cam_state_add(&cam_state, netdev->dev_addr); |
David Daney | 62538d2 | 2010-05-05 13:03:08 +0000 | [diff] [blame] | 563 | netdev_for_each_uc_addr(ha, netdev) |
| 564 | octeon_mgmt_cam_state_add(&cam_state, ha->addr); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 565 | } |
| 566 | if (multicast_mode == 0) { |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 567 | netdev_for_each_mc_addr(ha, netdev) |
| 568 | octeon_mgmt_cam_state_add(&cam_state, ha->addr); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 569 | } |
| 570 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 571 | spin_lock_irqsave(&p->lock, flags); |
| 572 | |
| 573 | /* Disable packet I/O. */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 574 | agl_gmx_prtx.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 575 | prev_packet_enable = agl_gmx_prtx.s.en; |
| 576 | agl_gmx_prtx.s.en = 0; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 577 | cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, agl_gmx_prtx.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 578 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 579 | adr_ctl.u64 = 0; |
| 580 | adr_ctl.s.cam_mode = cam_mode; |
| 581 | adr_ctl.s.mcst = multicast_mode; |
| 582 | adr_ctl.s.bcst = 1; /* Allow broadcast */ |
| 583 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 584 | cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CTL, adr_ctl.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 585 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 586 | cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM0, cam_state.cam[0]); |
| 587 | cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM1, cam_state.cam[1]); |
| 588 | cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM2, cam_state.cam[2]); |
| 589 | cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM3, cam_state.cam[3]); |
| 590 | cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM4, cam_state.cam[4]); |
| 591 | cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM5, cam_state.cam[5]); |
| 592 | cvmx_write_csr(p->agl + AGL_GMX_RX_ADR_CAM_EN, cam_state.cam_mask); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 593 | |
| 594 | /* Restore packet I/O. */ |
| 595 | agl_gmx_prtx.s.en = prev_packet_enable; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 596 | cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, agl_gmx_prtx.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 597 | |
| 598 | spin_unlock_irqrestore(&p->lock, flags); |
| 599 | } |
| 600 | |
| 601 | static int octeon_mgmt_set_mac_address(struct net_device *netdev, void *addr) |
| 602 | { |
| 603 | struct sockaddr *sa = addr; |
| 604 | |
| 605 | if (!is_valid_ether_addr(sa->sa_data)) |
| 606 | return -EADDRNOTAVAIL; |
| 607 | |
| 608 | memcpy(netdev->dev_addr, sa->sa_data, ETH_ALEN); |
| 609 | |
| 610 | octeon_mgmt_set_rx_filtering(netdev); |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | static int octeon_mgmt_change_mtu(struct net_device *netdev, int new_mtu) |
| 616 | { |
| 617 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 618 | int size_without_fcs = new_mtu + OCTEON_MGMT_RX_HEADROOM; |
| 619 | |
| 620 | /* |
| 621 | * Limit the MTU to make sure the ethernet packets are between |
| 622 | * 64 bytes and 16383 bytes. |
| 623 | */ |
| 624 | if (size_without_fcs < 64 || size_without_fcs > 16383) { |
| 625 | dev_warn(p->dev, "MTU must be between %d and %d.\n", |
| 626 | 64 - OCTEON_MGMT_RX_HEADROOM, |
| 627 | 16383 - OCTEON_MGMT_RX_HEADROOM); |
| 628 | return -EINVAL; |
| 629 | } |
| 630 | |
| 631 | netdev->mtu = new_mtu; |
| 632 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 633 | cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_MAX, size_without_fcs); |
| 634 | cvmx_write_csr(p->agl + AGL_GMX_RX_JABBER, |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 635 | (size_without_fcs + 7) & 0xfff8); |
| 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
| 640 | static irqreturn_t octeon_mgmt_interrupt(int cpl, void *dev_id) |
| 641 | { |
| 642 | struct net_device *netdev = dev_id; |
| 643 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 644 | union cvmx_mixx_isr mixx_isr; |
| 645 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 646 | mixx_isr.u64 = cvmx_read_csr(p->mix + MIX_ISR); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 647 | |
| 648 | /* Clear any pending interrupts */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 649 | cvmx_write_csr(p->mix + MIX_ISR, mixx_isr.u64); |
| 650 | cvmx_read_csr(p->mix + MIX_ISR); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 651 | |
| 652 | if (mixx_isr.s.irthresh) { |
| 653 | octeon_mgmt_disable_rx_irq(p); |
| 654 | napi_schedule(&p->napi); |
| 655 | } |
| 656 | if (mixx_isr.s.orthresh) { |
| 657 | octeon_mgmt_disable_tx_irq(p); |
| 658 | tasklet_schedule(&p->tx_clean_tasklet); |
| 659 | } |
| 660 | |
| 661 | return IRQ_HANDLED; |
| 662 | } |
| 663 | |
| 664 | static int octeon_mgmt_ioctl(struct net_device *netdev, |
| 665 | struct ifreq *rq, int cmd) |
| 666 | { |
| 667 | struct octeon_mgmt *p = netdev_priv(netdev); |
| 668 | |
| 669 | if (!netif_running(netdev)) |
| 670 | return -EINVAL; |
| 671 | |
| 672 | if (!p->phydev) |
| 673 | return -EINVAL; |
| 674 | |
Richard Cochran | 28b0411 | 2010-07-17 08:48:55 +0000 | [diff] [blame] | 675 | return phy_mii_ioctl(p->phydev, rq, cmd); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 676 | } |
| 677 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 678 | static void octeon_mgmt_disable_link(struct octeon_mgmt *p) |
| 679 | { |
| 680 | union cvmx_agl_gmx_prtx_cfg prtx_cfg; |
| 681 | |
| 682 | /* Disable GMX before we make any changes. */ |
| 683 | prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG); |
| 684 | prtx_cfg.s.en = 0; |
| 685 | prtx_cfg.s.tx_en = 0; |
| 686 | prtx_cfg.s.rx_en = 0; |
| 687 | cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64); |
| 688 | |
| 689 | if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) { |
| 690 | int i; |
| 691 | for (i = 0; i < 10; i++) { |
| 692 | prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG); |
| 693 | if (prtx_cfg.s.tx_idle == 1 || prtx_cfg.s.rx_idle == 1) |
| 694 | break; |
| 695 | mdelay(1); |
| 696 | i++; |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | static void octeon_mgmt_enable_link(struct octeon_mgmt *p) |
| 702 | { |
| 703 | union cvmx_agl_gmx_prtx_cfg prtx_cfg; |
| 704 | |
| 705 | /* Restore the GMX enable state only if link is set */ |
| 706 | prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG); |
| 707 | prtx_cfg.s.tx_en = 1; |
| 708 | prtx_cfg.s.rx_en = 1; |
| 709 | prtx_cfg.s.en = 1; |
| 710 | cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64); |
| 711 | } |
| 712 | |
| 713 | static void octeon_mgmt_update_link(struct octeon_mgmt *p) |
| 714 | { |
| 715 | union cvmx_agl_gmx_prtx_cfg prtx_cfg; |
| 716 | |
| 717 | prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG); |
| 718 | |
| 719 | if (!p->phydev->link) |
| 720 | prtx_cfg.s.duplex = 1; |
| 721 | else |
| 722 | prtx_cfg.s.duplex = p->phydev->duplex; |
| 723 | |
| 724 | switch (p->phydev->speed) { |
| 725 | case 10: |
| 726 | prtx_cfg.s.speed = 0; |
| 727 | prtx_cfg.s.slottime = 0; |
| 728 | |
| 729 | if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) { |
| 730 | prtx_cfg.s.burst = 1; |
| 731 | prtx_cfg.s.speed_msb = 1; |
| 732 | } |
| 733 | break; |
| 734 | case 100: |
| 735 | prtx_cfg.s.speed = 0; |
| 736 | prtx_cfg.s.slottime = 0; |
| 737 | |
| 738 | if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) { |
| 739 | prtx_cfg.s.burst = 1; |
| 740 | prtx_cfg.s.speed_msb = 0; |
| 741 | } |
| 742 | break; |
| 743 | case 1000: |
| 744 | /* 1000 MBits is only supported on 6XXX chips */ |
| 745 | if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) { |
| 746 | prtx_cfg.s.speed = 1; |
| 747 | prtx_cfg.s.speed_msb = 0; |
| 748 | /* Only matters for half-duplex */ |
| 749 | prtx_cfg.s.slottime = 1; |
| 750 | prtx_cfg.s.burst = p->phydev->duplex; |
| 751 | } |
| 752 | break; |
| 753 | case 0: /* No link */ |
| 754 | default: |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | /* Write the new GMX setting with the port still disabled. */ |
| 759 | cvmx_write_csr(p->agl + AGL_GMX_PRT_CFG, prtx_cfg.u64); |
| 760 | |
| 761 | /* Read GMX CFG again to make sure the config is completed. */ |
| 762 | prtx_cfg.u64 = cvmx_read_csr(p->agl + AGL_GMX_PRT_CFG); |
| 763 | |
| 764 | if (OCTEON_IS_MODEL(OCTEON_CN6XXX)) { |
| 765 | union cvmx_agl_gmx_txx_clk agl_clk; |
| 766 | union cvmx_agl_prtx_ctl prtx_ctl; |
| 767 | |
| 768 | prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl); |
| 769 | agl_clk.u64 = cvmx_read_csr(p->agl + AGL_GMX_TX_CLK); |
| 770 | /* MII (both speeds) and RGMII 1000 speed. */ |
| 771 | agl_clk.s.clk_cnt = 1; |
| 772 | if (prtx_ctl.s.mode == 0) { /* RGMII mode */ |
| 773 | if (p->phydev->speed == 10) |
| 774 | agl_clk.s.clk_cnt = 50; |
| 775 | else if (p->phydev->speed == 100) |
| 776 | agl_clk.s.clk_cnt = 5; |
| 777 | } |
| 778 | cvmx_write_csr(p->agl + AGL_GMX_TX_CLK, agl_clk.u64); |
| 779 | } |
| 780 | } |
| 781 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 782 | static void octeon_mgmt_adjust_link(struct net_device *netdev) |
| 783 | { |
| 784 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 785 | unsigned long flags; |
| 786 | int link_changed = 0; |
| 787 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 788 | if (!p->phydev) |
| 789 | return; |
| 790 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 791 | spin_lock_irqsave(&p->lock, flags); |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 792 | |
| 793 | |
| 794 | if (!p->phydev->link && p->last_link) |
| 795 | link_changed = -1; |
| 796 | |
| 797 | if (p->phydev->link |
| 798 | && (p->last_duplex != p->phydev->duplex |
| 799 | || p->last_link != p->phydev->link |
| 800 | || p->last_speed != p->phydev->speed)) { |
| 801 | octeon_mgmt_disable_link(p); |
| 802 | link_changed = 1; |
| 803 | octeon_mgmt_update_link(p); |
| 804 | octeon_mgmt_enable_link(p); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 805 | } |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 806 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 807 | p->last_link = p->phydev->link; |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 808 | p->last_speed = p->phydev->speed; |
| 809 | p->last_duplex = p->phydev->duplex; |
| 810 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 811 | spin_unlock_irqrestore(&p->lock, flags); |
| 812 | |
| 813 | if (link_changed != 0) { |
| 814 | if (link_changed > 0) { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 815 | pr_info("%s: Link is up - %d/%s\n", netdev->name, |
| 816 | p->phydev->speed, |
| 817 | DUPLEX_FULL == p->phydev->duplex ? |
| 818 | "Full" : "Half"); |
| 819 | } else { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 820 | pr_info("%s: Link is down\n", netdev->name); |
| 821 | } |
| 822 | } |
| 823 | } |
| 824 | |
| 825 | static int octeon_mgmt_init_phy(struct net_device *netdev) |
| 826 | { |
| 827 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 828 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 829 | if (octeon_is_simulation() || p->phy_np == NULL) { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 830 | /* No PHYs in the simulator. */ |
| 831 | netif_carrier_on(netdev); |
| 832 | return 0; |
| 833 | } |
| 834 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 835 | p->phydev = of_phy_connect(netdev, p->phy_np, |
| 836 | octeon_mgmt_adjust_link, 0, |
| 837 | PHY_INTERFACE_MODE_MII); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 838 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 839 | if (p->phydev == NULL) |
| 840 | return -ENODEV; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | static int octeon_mgmt_open(struct net_device *netdev) |
| 846 | { |
| 847 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 848 | union cvmx_mixx_ctl mix_ctl; |
| 849 | union cvmx_agl_gmx_inf_mode agl_gmx_inf_mode; |
| 850 | union cvmx_mixx_oring1 oring1; |
| 851 | union cvmx_mixx_iring1 iring1; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 852 | union cvmx_agl_gmx_rxx_frm_ctl rxx_frm_ctl; |
| 853 | union cvmx_mixx_irhwm mix_irhwm; |
| 854 | union cvmx_mixx_orhwm mix_orhwm; |
| 855 | union cvmx_mixx_intena mix_intena; |
| 856 | struct sockaddr sa; |
| 857 | |
| 858 | /* Allocate ring buffers. */ |
| 859 | p->tx_ring = kzalloc(ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE), |
| 860 | GFP_KERNEL); |
| 861 | if (!p->tx_ring) |
| 862 | return -ENOMEM; |
| 863 | p->tx_ring_handle = |
| 864 | dma_map_single(p->dev, p->tx_ring, |
| 865 | ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE), |
| 866 | DMA_BIDIRECTIONAL); |
| 867 | p->tx_next = 0; |
| 868 | p->tx_next_clean = 0; |
| 869 | p->tx_current_fill = 0; |
| 870 | |
| 871 | |
| 872 | p->rx_ring = kzalloc(ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE), |
| 873 | GFP_KERNEL); |
| 874 | if (!p->rx_ring) |
| 875 | goto err_nomem; |
| 876 | p->rx_ring_handle = |
| 877 | dma_map_single(p->dev, p->rx_ring, |
| 878 | ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE), |
| 879 | DMA_BIDIRECTIONAL); |
| 880 | |
| 881 | p->rx_next = 0; |
| 882 | p->rx_next_fill = 0; |
| 883 | p->rx_current_fill = 0; |
| 884 | |
| 885 | octeon_mgmt_reset_hw(p); |
| 886 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 887 | mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 888 | |
| 889 | /* Bring it out of reset if needed. */ |
| 890 | if (mix_ctl.s.reset) { |
| 891 | mix_ctl.s.reset = 0; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 892 | cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 893 | do { |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 894 | mix_ctl.u64 = cvmx_read_csr(p->mix + MIX_CTL); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 895 | } while (mix_ctl.s.reset); |
| 896 | } |
| 897 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 898 | if (OCTEON_IS_MODEL(OCTEON_CN5XXX)) { |
| 899 | agl_gmx_inf_mode.u64 = 0; |
| 900 | agl_gmx_inf_mode.s.en = 1; |
| 901 | cvmx_write_csr(CVMX_AGL_GMX_INF_MODE, agl_gmx_inf_mode.u64); |
| 902 | } |
| 903 | if (OCTEON_IS_MODEL(OCTEON_CN56XX_PASS1_X) |
| 904 | || OCTEON_IS_MODEL(OCTEON_CN52XX_PASS1_X)) { |
| 905 | /* |
| 906 | * Force compensation values, as they are not |
| 907 | * determined properly by HW |
| 908 | */ |
| 909 | union cvmx_agl_gmx_drv_ctl drv_ctl; |
| 910 | |
| 911 | drv_ctl.u64 = cvmx_read_csr(CVMX_AGL_GMX_DRV_CTL); |
| 912 | if (p->port) { |
| 913 | drv_ctl.s.byp_en1 = 1; |
| 914 | drv_ctl.s.nctl1 = 6; |
| 915 | drv_ctl.s.pctl1 = 6; |
| 916 | } else { |
| 917 | drv_ctl.s.byp_en = 1; |
| 918 | drv_ctl.s.nctl = 6; |
| 919 | drv_ctl.s.pctl = 6; |
| 920 | } |
| 921 | cvmx_write_csr(CVMX_AGL_GMX_DRV_CTL, drv_ctl.u64); |
| 922 | } |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 923 | |
| 924 | oring1.u64 = 0; |
| 925 | oring1.s.obase = p->tx_ring_handle >> 3; |
| 926 | oring1.s.osize = OCTEON_MGMT_TX_RING_SIZE; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 927 | cvmx_write_csr(p->mix + MIX_ORING1, oring1.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 928 | |
| 929 | iring1.u64 = 0; |
| 930 | iring1.s.ibase = p->rx_ring_handle >> 3; |
| 931 | iring1.s.isize = OCTEON_MGMT_RX_RING_SIZE; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 932 | cvmx_write_csr(p->mix + MIX_IRING1, iring1.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 933 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 934 | memcpy(sa.sa_data, netdev->dev_addr, ETH_ALEN); |
| 935 | octeon_mgmt_set_mac_address(netdev, &sa); |
| 936 | |
| 937 | octeon_mgmt_change_mtu(netdev, netdev->mtu); |
| 938 | |
| 939 | /* |
| 940 | * Enable the port HW. Packets are not allowed until |
| 941 | * cvmx_mgmt_port_enable() is called. |
| 942 | */ |
| 943 | mix_ctl.u64 = 0; |
| 944 | mix_ctl.s.crc_strip = 1; /* Strip the ending CRC */ |
| 945 | mix_ctl.s.en = 1; /* Enable the port */ |
| 946 | mix_ctl.s.nbtarb = 0; /* Arbitration mode */ |
| 947 | /* MII CB-request FIFO programmable high watermark */ |
| 948 | mix_ctl.s.mrq_hwm = 1; |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 949 | #ifdef __LITTLE_ENDIAN |
| 950 | mix_ctl.s.lendian = 1; |
| 951 | #endif |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 952 | cvmx_write_csr(p->mix + MIX_CTL, mix_ctl.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 953 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 954 | /* Read the PHY to find the mode of the interface. */ |
| 955 | if (octeon_mgmt_init_phy(netdev)) { |
| 956 | dev_err(p->dev, "Cannot initialize PHY on MIX%d.\n", p->port); |
| 957 | goto err_noirq; |
| 958 | } |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 959 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 960 | /* Set the mode of the interface, RGMII/MII. */ |
| 961 | if (OCTEON_IS_MODEL(OCTEON_CN6XXX) && p->phydev) { |
| 962 | union cvmx_agl_prtx_ctl agl_prtx_ctl; |
| 963 | int rgmii_mode = (p->phydev->supported & |
| 964 | (SUPPORTED_1000baseT_Half | SUPPORTED_1000baseT_Full)) != 0; |
| 965 | |
| 966 | agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl); |
| 967 | agl_prtx_ctl.s.mode = rgmii_mode ? 0 : 1; |
| 968 | cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64); |
| 969 | |
| 970 | /* MII clocks counts are based on the 125Mhz |
| 971 | * reference, which has an 8nS period. So our delays |
| 972 | * need to be multiplied by this factor. |
| 973 | */ |
| 974 | #define NS_PER_PHY_CLK 8 |
| 975 | |
| 976 | /* Take the DLL and clock tree out of reset */ |
| 977 | agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl); |
| 978 | agl_prtx_ctl.s.clkrst = 0; |
| 979 | if (rgmii_mode) { |
| 980 | agl_prtx_ctl.s.dllrst = 0; |
| 981 | agl_prtx_ctl.s.clktx_byp = 0; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 982 | } |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 983 | cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64); |
| 984 | cvmx_read_csr(p->agl_prt_ctl); /* Force write out before wait */ |
| 985 | |
| 986 | /* Wait for the DLL to lock. External 125 MHz |
| 987 | * reference clock must be stable at this point. |
| 988 | */ |
| 989 | ndelay(256 * NS_PER_PHY_CLK); |
| 990 | |
| 991 | /* Enable the interface */ |
| 992 | agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl); |
| 993 | agl_prtx_ctl.s.enable = 1; |
| 994 | cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64); |
| 995 | |
| 996 | /* Read the value back to force the previous write */ |
| 997 | agl_prtx_ctl.u64 = cvmx_read_csr(p->agl_prt_ctl); |
| 998 | |
| 999 | /* Enable the compensation controller */ |
| 1000 | agl_prtx_ctl.s.comp = 1; |
| 1001 | agl_prtx_ctl.s.drv_byp = 0; |
| 1002 | cvmx_write_csr(p->agl_prt_ctl, agl_prtx_ctl.u64); |
| 1003 | /* Force write out before wait. */ |
| 1004 | cvmx_read_csr(p->agl_prt_ctl); |
| 1005 | |
| 1006 | /* For compensation state to lock. */ |
| 1007 | ndelay(1040 * NS_PER_PHY_CLK); |
| 1008 | |
| 1009 | /* Some Ethernet switches cannot handle standard |
| 1010 | * Interframe Gap, increase to 16 bytes. |
| 1011 | */ |
| 1012 | cvmx_write_csr(CVMX_AGL_GMX_TX_IFG, 0x88); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | octeon_mgmt_rx_fill_ring(netdev); |
| 1016 | |
| 1017 | /* Clear statistics. */ |
| 1018 | /* Clear on read. */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1019 | cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_CTL, 1); |
| 1020 | cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_PKTS_DRP, 0); |
| 1021 | cvmx_write_csr(p->agl + AGL_GMX_RX_STATS_PKTS_BAD, 0); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1022 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1023 | cvmx_write_csr(p->agl + AGL_GMX_TX_STATS_CTL, 1); |
| 1024 | cvmx_write_csr(p->agl + AGL_GMX_TX_STAT0, 0); |
| 1025 | cvmx_write_csr(p->agl + AGL_GMX_TX_STAT1, 0); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1026 | |
| 1027 | /* Clear any pending interrupts */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1028 | cvmx_write_csr(p->mix + MIX_ISR, cvmx_read_csr(p->mix + MIX_ISR)); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1029 | |
| 1030 | if (request_irq(p->irq, octeon_mgmt_interrupt, 0, netdev->name, |
| 1031 | netdev)) { |
| 1032 | dev_err(p->dev, "request_irq(%d) failed.\n", p->irq); |
| 1033 | goto err_noirq; |
| 1034 | } |
| 1035 | |
| 1036 | /* Interrupt every single RX packet */ |
| 1037 | mix_irhwm.u64 = 0; |
| 1038 | mix_irhwm.s.irhwm = 0; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1039 | cvmx_write_csr(p->mix + MIX_IRHWM, mix_irhwm.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1040 | |
David Daney | b635e06 | 2010-05-05 13:03:11 +0000 | [diff] [blame] | 1041 | /* Interrupt when we have 1 or more packets to clean. */ |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1042 | mix_orhwm.u64 = 0; |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1043 | mix_orhwm.s.orhwm = 0; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1044 | cvmx_write_csr(p->mix + MIX_ORHWM, mix_orhwm.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1045 | |
| 1046 | /* Enable receive and transmit interrupts */ |
| 1047 | mix_intena.u64 = 0; |
| 1048 | mix_intena.s.ithena = 1; |
| 1049 | mix_intena.s.othena = 1; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1050 | cvmx_write_csr(p->mix + MIX_INTENA, mix_intena.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1051 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1052 | /* Enable packet I/O. */ |
| 1053 | |
| 1054 | rxx_frm_ctl.u64 = 0; |
| 1055 | rxx_frm_ctl.s.pre_align = 1; |
| 1056 | /* |
| 1057 | * When set, disables the length check for non-min sized pkts |
| 1058 | * with padding in the client data. |
| 1059 | */ |
| 1060 | rxx_frm_ctl.s.pad_len = 1; |
| 1061 | /* When set, disables the length check for VLAN pkts */ |
| 1062 | rxx_frm_ctl.s.vlan_len = 1; |
| 1063 | /* When set, PREAMBLE checking is less strict */ |
| 1064 | rxx_frm_ctl.s.pre_free = 1; |
| 1065 | /* Control Pause Frames can match station SMAC */ |
| 1066 | rxx_frm_ctl.s.ctl_smac = 0; |
| 1067 | /* Control Pause Frames can match globally assign Multicast address */ |
| 1068 | rxx_frm_ctl.s.ctl_mcst = 1; |
| 1069 | /* Forward pause information to TX block */ |
| 1070 | rxx_frm_ctl.s.ctl_bck = 1; |
| 1071 | /* Drop Control Pause Frames */ |
| 1072 | rxx_frm_ctl.s.ctl_drp = 1; |
| 1073 | /* Strip off the preamble */ |
| 1074 | rxx_frm_ctl.s.pre_strp = 1; |
| 1075 | /* |
| 1076 | * This port is configured to send PREAMBLE+SFD to begin every |
| 1077 | * frame. GMX checks that the PREAMBLE is sent correctly. |
| 1078 | */ |
| 1079 | rxx_frm_ctl.s.pre_chk = 1; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1080 | cvmx_write_csr(p->agl + AGL_GMX_RX_FRM_CTL, rxx_frm_ctl.u64); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1081 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1082 | /* Configure the port duplex, speed and enables */ |
| 1083 | octeon_mgmt_disable_link(p); |
| 1084 | if (p->phydev) |
| 1085 | octeon_mgmt_update_link(p); |
| 1086 | octeon_mgmt_enable_link(p); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1087 | |
| 1088 | p->last_link = 0; |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1089 | p->last_speed = 0; |
| 1090 | /* PHY is not present in simulator. The carrier is enabled |
| 1091 | * while initializing the phy for simulator, leave it enabled. |
| 1092 | */ |
| 1093 | if (p->phydev) { |
| 1094 | netif_carrier_off(netdev); |
| 1095 | phy_start_aneg(p->phydev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1096 | } |
| 1097 | |
| 1098 | netif_wake_queue(netdev); |
| 1099 | napi_enable(&p->napi); |
| 1100 | |
| 1101 | return 0; |
| 1102 | err_noirq: |
| 1103 | octeon_mgmt_reset_hw(p); |
| 1104 | dma_unmap_single(p->dev, p->rx_ring_handle, |
| 1105 | ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE), |
| 1106 | DMA_BIDIRECTIONAL); |
| 1107 | kfree(p->rx_ring); |
| 1108 | err_nomem: |
| 1109 | dma_unmap_single(p->dev, p->tx_ring_handle, |
| 1110 | ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE), |
| 1111 | DMA_BIDIRECTIONAL); |
| 1112 | kfree(p->tx_ring); |
| 1113 | return -ENOMEM; |
| 1114 | } |
| 1115 | |
| 1116 | static int octeon_mgmt_stop(struct net_device *netdev) |
| 1117 | { |
| 1118 | struct octeon_mgmt *p = netdev_priv(netdev); |
| 1119 | |
| 1120 | napi_disable(&p->napi); |
| 1121 | netif_stop_queue(netdev); |
| 1122 | |
| 1123 | if (p->phydev) |
| 1124 | phy_disconnect(p->phydev); |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1125 | p->phydev = NULL; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1126 | |
| 1127 | netif_carrier_off(netdev); |
| 1128 | |
| 1129 | octeon_mgmt_reset_hw(p); |
| 1130 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1131 | free_irq(p->irq, netdev); |
| 1132 | |
| 1133 | /* dma_unmap is a nop on Octeon, so just free everything. */ |
| 1134 | skb_queue_purge(&p->tx_list); |
| 1135 | skb_queue_purge(&p->rx_list); |
| 1136 | |
| 1137 | dma_unmap_single(p->dev, p->rx_ring_handle, |
| 1138 | ring_size_to_bytes(OCTEON_MGMT_RX_RING_SIZE), |
| 1139 | DMA_BIDIRECTIONAL); |
| 1140 | kfree(p->rx_ring); |
| 1141 | |
| 1142 | dma_unmap_single(p->dev, p->tx_ring_handle, |
| 1143 | ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE), |
| 1144 | DMA_BIDIRECTIONAL); |
| 1145 | kfree(p->tx_ring); |
| 1146 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1147 | return 0; |
| 1148 | } |
| 1149 | |
| 1150 | static int octeon_mgmt_xmit(struct sk_buff *skb, struct net_device *netdev) |
| 1151 | { |
| 1152 | struct octeon_mgmt *p = netdev_priv(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1153 | union mgmt_port_ring_entry re; |
| 1154 | unsigned long flags; |
David Daney | 4e4a4f1 | 2010-05-05 13:03:12 +0000 | [diff] [blame] | 1155 | int rv = NETDEV_TX_BUSY; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1156 | |
| 1157 | re.d64 = 0; |
| 1158 | re.s.len = skb->len; |
| 1159 | re.s.addr = dma_map_single(p->dev, skb->data, |
| 1160 | skb->len, |
| 1161 | DMA_TO_DEVICE); |
| 1162 | |
| 1163 | spin_lock_irqsave(&p->tx_list.lock, flags); |
| 1164 | |
David Daney | 4e4a4f1 | 2010-05-05 13:03:12 +0000 | [diff] [blame] | 1165 | if (unlikely(p->tx_current_fill >= ring_max_fill(OCTEON_MGMT_TX_RING_SIZE) - 1)) { |
| 1166 | spin_unlock_irqrestore(&p->tx_list.lock, flags); |
| 1167 | netif_stop_queue(netdev); |
| 1168 | spin_lock_irqsave(&p->tx_list.lock, flags); |
| 1169 | } |
| 1170 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1171 | if (unlikely(p->tx_current_fill >= |
| 1172 | ring_max_fill(OCTEON_MGMT_TX_RING_SIZE))) { |
| 1173 | spin_unlock_irqrestore(&p->tx_list.lock, flags); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1174 | dma_unmap_single(p->dev, re.s.addr, re.s.len, |
| 1175 | DMA_TO_DEVICE); |
David Daney | 4e4a4f1 | 2010-05-05 13:03:12 +0000 | [diff] [blame] | 1176 | goto out; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1177 | } |
| 1178 | |
| 1179 | __skb_queue_tail(&p->tx_list, skb); |
| 1180 | |
| 1181 | /* Put it in the ring. */ |
| 1182 | p->tx_ring[p->tx_next] = re.d64; |
| 1183 | p->tx_next = (p->tx_next + 1) % OCTEON_MGMT_TX_RING_SIZE; |
| 1184 | p->tx_current_fill++; |
| 1185 | |
| 1186 | spin_unlock_irqrestore(&p->tx_list.lock, flags); |
| 1187 | |
| 1188 | dma_sync_single_for_device(p->dev, p->tx_ring_handle, |
| 1189 | ring_size_to_bytes(OCTEON_MGMT_TX_RING_SIZE), |
| 1190 | DMA_BIDIRECTIONAL); |
| 1191 | |
| 1192 | netdev->stats.tx_packets++; |
| 1193 | netdev->stats.tx_bytes += skb->len; |
| 1194 | |
| 1195 | /* Ring the bell. */ |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1196 | cvmx_write_csr(p->mix + MIX_ORING2, 1); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1197 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1198 | netdev->trans_start = jiffies; |
David Daney | 4e4a4f1 | 2010-05-05 13:03:12 +0000 | [diff] [blame] | 1199 | rv = NETDEV_TX_OK; |
| 1200 | out: |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1201 | octeon_mgmt_update_tx_stats(netdev); |
David Daney | 4e4a4f1 | 2010-05-05 13:03:12 +0000 | [diff] [blame] | 1202 | return rv; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1203 | } |
| 1204 | |
| 1205 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1206 | static void octeon_mgmt_poll_controller(struct net_device *netdev) |
| 1207 | { |
| 1208 | struct octeon_mgmt *p = netdev_priv(netdev); |
| 1209 | |
| 1210 | octeon_mgmt_receive_packets(p, 16); |
| 1211 | octeon_mgmt_update_rx_stats(netdev); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1212 | } |
| 1213 | #endif |
| 1214 | |
| 1215 | static void octeon_mgmt_get_drvinfo(struct net_device *netdev, |
| 1216 | struct ethtool_drvinfo *info) |
| 1217 | { |
| 1218 | strncpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 1219 | strncpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 1220 | strncpy(info->fw_version, "N/A", sizeof(info->fw_version)); |
| 1221 | strncpy(info->bus_info, "N/A", sizeof(info->bus_info)); |
| 1222 | info->n_stats = 0; |
| 1223 | info->testinfo_len = 0; |
| 1224 | info->regdump_len = 0; |
| 1225 | info->eedump_len = 0; |
| 1226 | } |
| 1227 | |
| 1228 | static int octeon_mgmt_get_settings(struct net_device *netdev, |
| 1229 | struct ethtool_cmd *cmd) |
| 1230 | { |
| 1231 | struct octeon_mgmt *p = netdev_priv(netdev); |
| 1232 | |
| 1233 | if (p->phydev) |
| 1234 | return phy_ethtool_gset(p->phydev, cmd); |
| 1235 | |
| 1236 | return -EINVAL; |
| 1237 | } |
| 1238 | |
| 1239 | static int octeon_mgmt_set_settings(struct net_device *netdev, |
| 1240 | struct ethtool_cmd *cmd) |
| 1241 | { |
| 1242 | struct octeon_mgmt *p = netdev_priv(netdev); |
| 1243 | |
| 1244 | if (!capable(CAP_NET_ADMIN)) |
| 1245 | return -EPERM; |
| 1246 | |
| 1247 | if (p->phydev) |
| 1248 | return phy_ethtool_sset(p->phydev, cmd); |
| 1249 | |
| 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | |
| 1253 | static const struct ethtool_ops octeon_mgmt_ethtool_ops = { |
| 1254 | .get_drvinfo = octeon_mgmt_get_drvinfo, |
| 1255 | .get_link = ethtool_op_get_link, |
| 1256 | .get_settings = octeon_mgmt_get_settings, |
| 1257 | .set_settings = octeon_mgmt_set_settings |
| 1258 | }; |
| 1259 | |
| 1260 | static const struct net_device_ops octeon_mgmt_ops = { |
| 1261 | .ndo_open = octeon_mgmt_open, |
| 1262 | .ndo_stop = octeon_mgmt_stop, |
| 1263 | .ndo_start_xmit = octeon_mgmt_xmit, |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1264 | .ndo_set_rx_mode = octeon_mgmt_set_rx_filtering, |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1265 | .ndo_set_mac_address = octeon_mgmt_set_mac_address, |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1266 | .ndo_do_ioctl = octeon_mgmt_ioctl, |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1267 | .ndo_change_mtu = octeon_mgmt_change_mtu, |
| 1268 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1269 | .ndo_poll_controller = octeon_mgmt_poll_controller, |
| 1270 | #endif |
| 1271 | }; |
| 1272 | |
David Daney | d30b181 | 2010-06-24 09:14:47 +0000 | [diff] [blame] | 1273 | static int __devinit octeon_mgmt_probe(struct platform_device *pdev) |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1274 | { |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1275 | struct net_device *netdev; |
| 1276 | struct octeon_mgmt *p; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1277 | const __be32 *data; |
| 1278 | const u8 *mac; |
| 1279 | struct resource *res_mix; |
| 1280 | struct resource *res_agl; |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1281 | struct resource *res_agl_prt_ctl; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1282 | int len; |
| 1283 | int result; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1284 | |
| 1285 | netdev = alloc_etherdev(sizeof(struct octeon_mgmt)); |
| 1286 | if (netdev == NULL) |
| 1287 | return -ENOMEM; |
| 1288 | |
| 1289 | dev_set_drvdata(&pdev->dev, netdev); |
| 1290 | p = netdev_priv(netdev); |
| 1291 | netif_napi_add(netdev, &p->napi, octeon_mgmt_napi_poll, |
| 1292 | OCTEON_MGMT_NAPI_WEIGHT); |
| 1293 | |
| 1294 | p->netdev = netdev; |
| 1295 | p->dev = &pdev->dev; |
| 1296 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1297 | data = of_get_property(pdev->dev.of_node, "cell-index", &len); |
| 1298 | if (data && len == sizeof(*data)) { |
| 1299 | p->port = be32_to_cpup(data); |
| 1300 | } else { |
| 1301 | dev_err(&pdev->dev, "no 'cell-index' property\n"); |
| 1302 | result = -ENXIO; |
| 1303 | goto err; |
| 1304 | } |
| 1305 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1306 | snprintf(netdev->name, IFNAMSIZ, "mgmt%d", p->port); |
| 1307 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1308 | result = platform_get_irq(pdev, 0); |
| 1309 | if (result < 0) |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1310 | goto err; |
| 1311 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1312 | p->irq = result; |
| 1313 | |
| 1314 | res_mix = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1315 | if (res_mix == NULL) { |
| 1316 | dev_err(&pdev->dev, "no 'reg' resource\n"); |
| 1317 | result = -ENXIO; |
| 1318 | goto err; |
| 1319 | } |
| 1320 | |
| 1321 | res_agl = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 1322 | if (res_agl == NULL) { |
| 1323 | dev_err(&pdev->dev, "no 'reg' resource\n"); |
| 1324 | result = -ENXIO; |
| 1325 | goto err; |
| 1326 | } |
| 1327 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1328 | res_agl_prt_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 3); |
| 1329 | if (res_agl_prt_ctl == NULL) { |
| 1330 | dev_err(&pdev->dev, "no 'reg' resource\n"); |
| 1331 | result = -ENXIO; |
| 1332 | goto err; |
| 1333 | } |
| 1334 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1335 | p->mix_phys = res_mix->start; |
| 1336 | p->mix_size = resource_size(res_mix); |
| 1337 | p->agl_phys = res_agl->start; |
| 1338 | p->agl_size = resource_size(res_agl); |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1339 | p->agl_prt_ctl_phys = res_agl_prt_ctl->start; |
| 1340 | p->agl_prt_ctl_size = resource_size(res_agl_prt_ctl); |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1341 | |
| 1342 | |
| 1343 | if (!devm_request_mem_region(&pdev->dev, p->mix_phys, p->mix_size, |
| 1344 | res_mix->name)) { |
| 1345 | dev_err(&pdev->dev, "request_mem_region (%s) failed\n", |
| 1346 | res_mix->name); |
| 1347 | result = -ENXIO; |
| 1348 | goto err; |
| 1349 | } |
| 1350 | |
| 1351 | if (!devm_request_mem_region(&pdev->dev, p->agl_phys, p->agl_size, |
| 1352 | res_agl->name)) { |
| 1353 | result = -ENXIO; |
| 1354 | dev_err(&pdev->dev, "request_mem_region (%s) failed\n", |
| 1355 | res_agl->name); |
| 1356 | goto err; |
| 1357 | } |
| 1358 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1359 | if (!devm_request_mem_region(&pdev->dev, p->agl_prt_ctl_phys, |
| 1360 | p->agl_prt_ctl_size, res_agl_prt_ctl->name)) { |
| 1361 | result = -ENXIO; |
| 1362 | dev_err(&pdev->dev, "request_mem_region (%s) failed\n", |
| 1363 | res_agl_prt_ctl->name); |
| 1364 | goto err; |
| 1365 | } |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1366 | |
| 1367 | p->mix = (u64)devm_ioremap(&pdev->dev, p->mix_phys, p->mix_size); |
| 1368 | p->agl = (u64)devm_ioremap(&pdev->dev, p->agl_phys, p->agl_size); |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1369 | p->agl_prt_ctl = (u64)devm_ioremap(&pdev->dev, p->agl_prt_ctl_phys, |
| 1370 | p->agl_prt_ctl_size); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1371 | spin_lock_init(&p->lock); |
| 1372 | |
| 1373 | skb_queue_head_init(&p->tx_list); |
| 1374 | skb_queue_head_init(&p->rx_list); |
| 1375 | tasklet_init(&p->tx_clean_tasklet, |
| 1376 | octeon_mgmt_clean_tx_tasklet, (unsigned long)p); |
| 1377 | |
Jiri Pirko | 0178934 | 2011-08-16 06:29:00 +0000 | [diff] [blame] | 1378 | netdev->priv_flags |= IFF_UNICAST_FLT; |
| 1379 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1380 | netdev->netdev_ops = &octeon_mgmt_ops; |
| 1381 | netdev->ethtool_ops = &octeon_mgmt_ethtool_ops; |
| 1382 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1383 | mac = of_get_mac_address(pdev->dev.of_node); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1384 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1385 | if (mac) |
| 1386 | memcpy(netdev->dev_addr, mac, 6); |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1387 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1388 | p->phy_np = of_parse_phandle(pdev->dev.of_node, "phy-handle", 0); |
| 1389 | |
| 1390 | pdev->dev.coherent_dma_mask = DMA_BIT_MASK(64); |
| 1391 | pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask; |
| 1392 | |
David Daney | eeae05a | 2012-08-21 11:45:06 -0700 | [diff] [blame^] | 1393 | netif_carrier_off(netdev); |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1394 | result = register_netdev(netdev); |
| 1395 | if (result) |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1396 | goto err; |
| 1397 | |
| 1398 | dev_info(&pdev->dev, "Version " DRV_VERSION "\n"); |
| 1399 | return 0; |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1400 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1401 | err: |
| 1402 | free_netdev(netdev); |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1403 | return result; |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1404 | } |
| 1405 | |
David Daney | d30b181 | 2010-06-24 09:14:47 +0000 | [diff] [blame] | 1406 | static int __devexit octeon_mgmt_remove(struct platform_device *pdev) |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1407 | { |
| 1408 | struct net_device *netdev = dev_get_drvdata(&pdev->dev); |
| 1409 | |
| 1410 | unregister_netdev(netdev); |
| 1411 | free_netdev(netdev); |
| 1412 | return 0; |
| 1413 | } |
| 1414 | |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1415 | static struct of_device_id octeon_mgmt_match[] = { |
| 1416 | { |
| 1417 | .compatible = "cavium,octeon-5750-mix", |
| 1418 | }, |
| 1419 | {}, |
| 1420 | }; |
| 1421 | MODULE_DEVICE_TABLE(of, octeon_mgmt_match); |
| 1422 | |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1423 | static struct platform_driver octeon_mgmt_driver = { |
| 1424 | .driver = { |
| 1425 | .name = "octeon_mgmt", |
| 1426 | .owner = THIS_MODULE, |
David Daney | 368bec0 | 2012-07-05 18:12:39 +0200 | [diff] [blame] | 1427 | .of_match_table = octeon_mgmt_match, |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1428 | }, |
| 1429 | .probe = octeon_mgmt_probe, |
David Daney | d30b181 | 2010-06-24 09:14:47 +0000 | [diff] [blame] | 1430 | .remove = __devexit_p(octeon_mgmt_remove), |
David Daney | d6aa60a | 2009-10-14 12:04:41 -0700 | [diff] [blame] | 1431 | }; |
| 1432 | |
| 1433 | extern void octeon_mdiobus_force_mod_depencency(void); |
| 1434 | |
| 1435 | static int __init octeon_mgmt_mod_init(void) |
| 1436 | { |
| 1437 | /* Force our mdiobus driver module to be loaded first. */ |
| 1438 | octeon_mdiobus_force_mod_depencency(); |
| 1439 | return platform_driver_register(&octeon_mgmt_driver); |
| 1440 | } |
| 1441 | |
| 1442 | static void __exit octeon_mgmt_mod_exit(void) |
| 1443 | { |
| 1444 | platform_driver_unregister(&octeon_mgmt_driver); |
| 1445 | } |
| 1446 | |
| 1447 | module_init(octeon_mgmt_mod_init); |
| 1448 | module_exit(octeon_mgmt_mod_exit); |
| 1449 | |
| 1450 | MODULE_DESCRIPTION(DRV_DESCRIPTION); |
| 1451 | MODULE_AUTHOR("David Daney"); |
| 1452 | MODULE_LICENSE("GPL"); |
| 1453 | MODULE_VERSION(DRV_VERSION); |