Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/net/mv643xx_eth.c - Driver for MV643XX ethernet ports |
| 3 | * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com> |
| 4 | * |
| 5 | * Based on the 64360 driver from: |
| 6 | * Copyright (C) 2002 rabeeh@galileo.co.il |
| 7 | * |
| 8 | * Copyright (C) 2003 PMC-Sierra, Inc., |
Olaf Hering | 3bb8a18 | 2006-01-05 22:45:45 -0800 | [diff] [blame] | 9 | * written by Manish Lachwani |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org> |
| 12 | * |
| 13 | * Copyright (C) 2004-2005 MontaVista Software, Inc. |
| 14 | * Dale Farnsworth <dale@farnsworth.org> |
| 15 | * |
| 16 | * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com> |
| 17 | * <sjhill@realitydiluted.com> |
| 18 | * |
| 19 | * This program is free software; you can redistribute it and/or |
| 20 | * modify it under the terms of the GNU General Public License |
| 21 | * as published by the Free Software Foundation; either version 2 |
| 22 | * of the License, or (at your option) any later version. |
| 23 | * |
| 24 | * This program is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU General Public License |
| 30 | * along with this program; if not, write to the Free Software |
| 31 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 32 | */ |
| 33 | #include <linux/init.h> |
| 34 | #include <linux/dma-mapping.h> |
| 35 | #include <linux/tcp.h> |
| 36 | #include <linux/udp.h> |
| 37 | #include <linux/etherdevice.h> |
Olaf Hering | 78a5e53 | 2006-01-16 16:47:00 -0700 | [diff] [blame^] | 38 | #include <linux/in.h> |
| 39 | #include <linux/ip.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
| 41 | #include <linux/bitops.h> |
| 42 | #include <linux/delay.h> |
| 43 | #include <linux/ethtool.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 44 | #include <linux/platform_device.h> |
| 45 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/io.h> |
| 47 | #include <asm/types.h> |
| 48 | #include <asm/pgtable.h> |
| 49 | #include <asm/system.h> |
| 50 | #include <asm/delay.h> |
| 51 | #include "mv643xx_eth.h" |
| 52 | |
| 53 | /* |
| 54 | * The first part is the high level driver of the gigE ethernet ports. |
| 55 | */ |
| 56 | |
| 57 | /* Constants */ |
| 58 | #define VLAN_HLEN 4 |
| 59 | #define FCS_LEN 4 |
| 60 | #define WRAP NET_IP_ALIGN + ETH_HLEN + VLAN_HLEN + FCS_LEN |
| 61 | #define RX_SKB_SIZE ((dev->mtu + WRAP + 7) & ~0x7) |
| 62 | |
| 63 | #define INT_CAUSE_UNMASK_ALL 0x0007ffff |
| 64 | #define INT_CAUSE_UNMASK_ALL_EXT 0x0011ffff |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #define INT_CAUSE_MASK_ALL 0x00000000 |
Dale Farnsworth | 63c9e54 | 2005-09-02 13:49:10 -0700 | [diff] [blame] | 66 | #define INT_CAUSE_MASK_ALL_EXT 0x00000000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | #define INT_CAUSE_CHECK_BITS INT_CAUSE_UNMASK_ALL |
| 68 | #define INT_CAUSE_CHECK_BITS_EXT INT_CAUSE_UNMASK_ALL_EXT |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | #ifdef MV643XX_CHECKSUM_OFFLOAD_TX |
| 71 | #define MAX_DESCS_PER_SKB (MAX_SKB_FRAGS + 1) |
| 72 | #else |
| 73 | #define MAX_DESCS_PER_SKB 1 |
| 74 | #endif |
| 75 | |
| 76 | #define PHY_WAIT_ITERATIONS 1000 /* 1000 iterations * 10uS = 10mS max */ |
| 77 | #define PHY_WAIT_MICRO_SECONDS 10 |
| 78 | |
| 79 | /* Static function declarations */ |
| 80 | static int eth_port_link_is_up(unsigned int eth_port_num); |
| 81 | static void eth_port_uc_addr_get(struct net_device *dev, |
| 82 | unsigned char *MacAddr); |
| 83 | static int mv643xx_eth_real_open(struct net_device *); |
| 84 | static int mv643xx_eth_real_stop(struct net_device *); |
| 85 | static int mv643xx_eth_change_mtu(struct net_device *, int); |
| 86 | static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *); |
| 87 | static void eth_port_init_mac_tables(unsigned int eth_port_num); |
| 88 | #ifdef MV643XX_NAPI |
| 89 | static int mv643xx_poll(struct net_device *dev, int *budget); |
| 90 | #endif |
| 91 | static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr); |
| 92 | static int ethernet_phy_detect(unsigned int eth_port_num); |
| 93 | static struct ethtool_ops mv643xx_ethtool_ops; |
| 94 | |
| 95 | static char mv643xx_driver_name[] = "mv643xx_eth"; |
| 96 | static char mv643xx_driver_version[] = "1.0"; |
| 97 | |
| 98 | static void __iomem *mv643xx_eth_shared_base; |
| 99 | |
| 100 | /* used to protect MV643XX_ETH_SMI_REG, which is shared across ports */ |
Ingo Molnar | a9f6a0d | 2005-09-09 13:10:41 -0700 | [diff] [blame] | 101 | static DEFINE_SPINLOCK(mv643xx_eth_phy_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | static inline u32 mv_read(int offset) |
| 104 | { |
Al Viro | dc074a8 | 2005-04-25 07:55:58 -0700 | [diff] [blame] | 105 | void __iomem *reg_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
| 107 | reg_base = mv643xx_eth_shared_base - MV643XX_ETH_SHARED_REGS; |
| 108 | |
| 109 | return readl(reg_base + offset); |
| 110 | } |
| 111 | |
| 112 | static inline void mv_write(int offset, u32 data) |
| 113 | { |
Al Viro | dc074a8 | 2005-04-25 07:55:58 -0700 | [diff] [blame] | 114 | void __iomem *reg_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | reg_base = mv643xx_eth_shared_base - MV643XX_ETH_SHARED_REGS; |
| 117 | writel(data, reg_base + offset); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * Changes MTU (maximum transfer unit) of the gigabit ethenret port |
| 122 | * |
| 123 | * Input : pointer to ethernet interface network device structure |
| 124 | * new mtu size |
| 125 | * Output : 0 upon success, -EINVAL upon failure |
| 126 | */ |
| 127 | static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu) |
| 128 | { |
| 129 | struct mv643xx_private *mp = netdev_priv(dev); |
| 130 | unsigned long flags; |
| 131 | |
| 132 | spin_lock_irqsave(&mp->lock, flags); |
| 133 | |
| 134 | if ((new_mtu > 9500) || (new_mtu < 64)) { |
| 135 | spin_unlock_irqrestore(&mp->lock, flags); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | dev->mtu = new_mtu; |
| 140 | /* |
| 141 | * Stop then re-open the interface. This will allocate RX skb's with |
| 142 | * the new MTU. |
| 143 | * There is a possible danger that the open will not successed, due |
| 144 | * to memory is full, which might fail the open function. |
| 145 | */ |
| 146 | if (netif_running(dev)) { |
| 147 | if (mv643xx_eth_real_stop(dev)) |
| 148 | printk(KERN_ERR |
| 149 | "%s: Fatal error on stopping device\n", |
| 150 | dev->name); |
| 151 | if (mv643xx_eth_real_open(dev)) |
| 152 | printk(KERN_ERR |
| 153 | "%s: Fatal error on opening device\n", |
| 154 | dev->name); |
| 155 | } |
| 156 | |
| 157 | spin_unlock_irqrestore(&mp->lock, flags); |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | /* |
| 162 | * mv643xx_eth_rx_task |
| 163 | * |
| 164 | * Fills / refills RX queue on a certain gigabit ethernet port |
| 165 | * |
| 166 | * Input : pointer to ethernet interface network device structure |
| 167 | * Output : N/A |
| 168 | */ |
| 169 | static void mv643xx_eth_rx_task(void *data) |
| 170 | { |
| 171 | struct net_device *dev = (struct net_device *)data; |
| 172 | struct mv643xx_private *mp = netdev_priv(dev); |
| 173 | struct pkt_info pkt_info; |
| 174 | struct sk_buff *skb; |
| 175 | |
| 176 | if (test_and_set_bit(0, &mp->rx_task_busy)) |
| 177 | panic("%s: Error in test_set_bit / clear_bit", dev->name); |
| 178 | |
| 179 | while (mp->rx_ring_skbs < (mp->rx_ring_size - 5)) { |
| 180 | skb = dev_alloc_skb(RX_SKB_SIZE); |
| 181 | if (!skb) |
| 182 | break; |
| 183 | mp->rx_ring_skbs++; |
| 184 | pkt_info.cmd_sts = ETH_RX_ENABLE_INTERRUPT; |
| 185 | pkt_info.byte_cnt = RX_SKB_SIZE; |
| 186 | pkt_info.buf_ptr = dma_map_single(NULL, skb->data, RX_SKB_SIZE, |
| 187 | DMA_FROM_DEVICE); |
| 188 | pkt_info.return_info = skb; |
| 189 | if (eth_rx_return_buff(mp, &pkt_info) != ETH_OK) { |
| 190 | printk(KERN_ERR |
| 191 | "%s: Error allocating RX Ring\n", dev->name); |
| 192 | break; |
| 193 | } |
| 194 | skb_reserve(skb, 2); |
| 195 | } |
| 196 | clear_bit(0, &mp->rx_task_busy); |
| 197 | /* |
| 198 | * If RX ring is empty of SKB, set a timer to try allocating |
| 199 | * again in a later time . |
| 200 | */ |
| 201 | if ((mp->rx_ring_skbs == 0) && (mp->rx_timer_flag == 0)) { |
| 202 | printk(KERN_INFO "%s: Rx ring is empty\n", dev->name); |
| 203 | /* After 100mSec */ |
| 204 | mp->timeout.expires = jiffies + (HZ / 10); |
| 205 | add_timer(&mp->timeout); |
| 206 | mp->rx_timer_flag = 1; |
| 207 | } |
| 208 | #ifdef MV643XX_RX_QUEUE_FILL_ON_TASK |
| 209 | else { |
| 210 | /* Return interrupts */ |
| 211 | mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(mp->port_num), |
| 212 | INT_CAUSE_UNMASK_ALL); |
| 213 | } |
| 214 | #endif |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * mv643xx_eth_rx_task_timer_wrapper |
| 219 | * |
| 220 | * Timer routine to wake up RX queue filling task. This function is |
| 221 | * used only in case the RX queue is empty, and all alloc_skb has |
| 222 | * failed (due to out of memory event). |
| 223 | * |
| 224 | * Input : pointer to ethernet interface network device structure |
| 225 | * Output : N/A |
| 226 | */ |
| 227 | static void mv643xx_eth_rx_task_timer_wrapper(unsigned long data) |
| 228 | { |
| 229 | struct net_device *dev = (struct net_device *)data; |
| 230 | struct mv643xx_private *mp = netdev_priv(dev); |
| 231 | |
| 232 | mp->rx_timer_flag = 0; |
| 233 | mv643xx_eth_rx_task((void *)data); |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * mv643xx_eth_update_mac_address |
| 238 | * |
| 239 | * Update the MAC address of the port in the address table |
| 240 | * |
| 241 | * Input : pointer to ethernet interface network device structure |
| 242 | * Output : N/A |
| 243 | */ |
| 244 | static void mv643xx_eth_update_mac_address(struct net_device *dev) |
| 245 | { |
| 246 | struct mv643xx_private *mp = netdev_priv(dev); |
| 247 | unsigned int port_num = mp->port_num; |
| 248 | |
| 249 | eth_port_init_mac_tables(port_num); |
| 250 | memcpy(mp->port_mac_addr, dev->dev_addr, 6); |
| 251 | eth_port_uc_addr_set(port_num, mp->port_mac_addr); |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * mv643xx_eth_set_rx_mode |
| 256 | * |
| 257 | * Change from promiscuos to regular rx mode |
| 258 | * |
| 259 | * Input : pointer to ethernet interface network device structure |
| 260 | * Output : N/A |
| 261 | */ |
| 262 | static void mv643xx_eth_set_rx_mode(struct net_device *dev) |
| 263 | { |
| 264 | struct mv643xx_private *mp = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | if (dev->flags & IFF_PROMISC) |
Dale Farnsworth | 7342cd8 | 2005-09-02 12:36:48 -0700 | [diff] [blame] | 267 | mp->port_config |= (u32) MV643XX_ETH_UNICAST_PROMISCUOUS_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | else |
Dale Farnsworth | 7342cd8 | 2005-09-02 12:36:48 -0700 | [diff] [blame] | 269 | mp->port_config &= ~(u32) MV643XX_ETH_UNICAST_PROMISCUOUS_MODE; |
| 270 | |
| 271 | mv_write(MV643XX_ETH_PORT_CONFIG_REG(mp->port_num), mp->port_config); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /* |
| 275 | * mv643xx_eth_set_mac_address |
| 276 | * |
| 277 | * Change the interface's mac address. |
| 278 | * No special hardware thing should be done because interface is always |
| 279 | * put in promiscuous mode. |
| 280 | * |
| 281 | * Input : pointer to ethernet interface network device structure and |
| 282 | * a pointer to the designated entry to be added to the cache. |
| 283 | * Output : zero upon success, negative upon failure |
| 284 | */ |
| 285 | static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr) |
| 286 | { |
| 287 | int i; |
| 288 | |
| 289 | for (i = 0; i < 6; i++) |
| 290 | /* +2 is for the offset of the HW addr type */ |
| 291 | dev->dev_addr[i] = ((unsigned char *)addr)[i + 2]; |
| 292 | mv643xx_eth_update_mac_address(dev); |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * mv643xx_eth_tx_timeout |
| 298 | * |
| 299 | * Called upon a timeout on transmitting a packet |
| 300 | * |
| 301 | * Input : pointer to ethernet interface network device structure. |
| 302 | * Output : N/A |
| 303 | */ |
| 304 | static void mv643xx_eth_tx_timeout(struct net_device *dev) |
| 305 | { |
| 306 | struct mv643xx_private *mp = netdev_priv(dev); |
| 307 | |
| 308 | printk(KERN_INFO "%s: TX timeout ", dev->name); |
| 309 | |
| 310 | /* Do the reset outside of interrupt context */ |
| 311 | schedule_work(&mp->tx_timeout_task); |
| 312 | } |
| 313 | |
| 314 | /* |
| 315 | * mv643xx_eth_tx_timeout_task |
| 316 | * |
| 317 | * Actual routine to reset the adapter when a timeout on Tx has occurred |
| 318 | */ |
| 319 | static void mv643xx_eth_tx_timeout_task(struct net_device *dev) |
| 320 | { |
| 321 | struct mv643xx_private *mp = netdev_priv(dev); |
| 322 | |
| 323 | netif_device_detach(dev); |
| 324 | eth_port_reset(mp->port_num); |
| 325 | eth_port_start(mp); |
| 326 | netif_device_attach(dev); |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * mv643xx_eth_free_tx_queue |
| 331 | * |
| 332 | * Input : dev - a pointer to the required interface |
| 333 | * |
| 334 | * Output : 0 if was able to release skb , nonzero otherwise |
| 335 | */ |
| 336 | static int mv643xx_eth_free_tx_queue(struct net_device *dev, |
| 337 | unsigned int eth_int_cause_ext) |
| 338 | { |
| 339 | struct mv643xx_private *mp = netdev_priv(dev); |
| 340 | struct net_device_stats *stats = &mp->stats; |
| 341 | struct pkt_info pkt_info; |
| 342 | int released = 1; |
| 343 | |
| 344 | if (!(eth_int_cause_ext & (BIT0 | BIT8))) |
| 345 | return released; |
| 346 | |
| 347 | spin_lock(&mp->lock); |
| 348 | |
| 349 | /* Check only queue 0 */ |
| 350 | while (eth_tx_return_desc(mp, &pkt_info) == ETH_OK) { |
| 351 | if (pkt_info.cmd_sts & BIT0) { |
| 352 | printk("%s: Error in TX\n", dev->name); |
| 353 | stats->tx_errors++; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * If return_info is different than 0, release the skb. |
| 358 | * The case where return_info is not 0 is only in case |
| 359 | * when transmitted a scatter/gather packet, where only |
| 360 | * last skb releases the whole chain. |
| 361 | */ |
| 362 | if (pkt_info.return_info) { |
| 363 | if (skb_shinfo(pkt_info.return_info)->nr_frags) |
| 364 | dma_unmap_page(NULL, pkt_info.buf_ptr, |
| 365 | pkt_info.byte_cnt, |
| 366 | DMA_TO_DEVICE); |
| 367 | else |
| 368 | dma_unmap_single(NULL, pkt_info.buf_ptr, |
| 369 | pkt_info.byte_cnt, |
| 370 | DMA_TO_DEVICE); |
| 371 | |
| 372 | dev_kfree_skb_irq(pkt_info.return_info); |
| 373 | released = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | } else |
| 375 | dma_unmap_page(NULL, pkt_info.buf_ptr, |
| 376 | pkt_info.byte_cnt, DMA_TO_DEVICE); |
| 377 | } |
| 378 | |
| 379 | spin_unlock(&mp->lock); |
| 380 | |
| 381 | return released; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * mv643xx_eth_receive |
| 386 | * |
| 387 | * This function is forward packets that are received from the port's |
| 388 | * queues toward kernel core or FastRoute them to another interface. |
| 389 | * |
| 390 | * Input : dev - a pointer to the required interface |
| 391 | * max - maximum number to receive (0 means unlimted) |
| 392 | * |
| 393 | * Output : number of served packets |
| 394 | */ |
| 395 | #ifdef MV643XX_NAPI |
| 396 | static int mv643xx_eth_receive_queue(struct net_device *dev, int budget) |
| 397 | #else |
| 398 | static int mv643xx_eth_receive_queue(struct net_device *dev) |
| 399 | #endif |
| 400 | { |
| 401 | struct mv643xx_private *mp = netdev_priv(dev); |
| 402 | struct net_device_stats *stats = &mp->stats; |
| 403 | unsigned int received_packets = 0; |
| 404 | struct sk_buff *skb; |
| 405 | struct pkt_info pkt_info; |
| 406 | |
| 407 | #ifdef MV643XX_NAPI |
Dale Farnsworth | b1dd9ca | 2005-09-01 09:59:23 -0700 | [diff] [blame] | 408 | while (budget-- > 0 && eth_port_receive(mp, &pkt_info) == ETH_OK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | #else |
| 410 | while (eth_port_receive(mp, &pkt_info) == ETH_OK) { |
| 411 | #endif |
| 412 | mp->rx_ring_skbs--; |
| 413 | received_packets++; |
Dale Farnsworth | b1dd9ca | 2005-09-01 09:59:23 -0700 | [diff] [blame] | 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | /* Update statistics. Note byte count includes 4 byte CRC count */ |
| 416 | stats->rx_packets++; |
| 417 | stats->rx_bytes += pkt_info.byte_cnt; |
| 418 | skb = pkt_info.return_info; |
| 419 | /* |
| 420 | * In case received a packet without first / last bits on OR |
| 421 | * the error summary bit is on, the packets needs to be dropeed. |
| 422 | */ |
| 423 | if (((pkt_info.cmd_sts |
| 424 | & (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) != |
| 425 | (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) |
| 426 | || (pkt_info.cmd_sts & ETH_ERROR_SUMMARY)) { |
| 427 | stats->rx_dropped++; |
| 428 | if ((pkt_info.cmd_sts & (ETH_RX_FIRST_DESC | |
| 429 | ETH_RX_LAST_DESC)) != |
| 430 | (ETH_RX_FIRST_DESC | ETH_RX_LAST_DESC)) { |
| 431 | if (net_ratelimit()) |
| 432 | printk(KERN_ERR |
| 433 | "%s: Received packet spread " |
| 434 | "on multiple descriptors\n", |
| 435 | dev->name); |
| 436 | } |
| 437 | if (pkt_info.cmd_sts & ETH_ERROR_SUMMARY) |
| 438 | stats->rx_errors++; |
| 439 | |
| 440 | dev_kfree_skb_irq(skb); |
| 441 | } else { |
| 442 | /* |
| 443 | * The -4 is for the CRC in the trailer of the |
| 444 | * received packet |
| 445 | */ |
| 446 | skb_put(skb, pkt_info.byte_cnt - 4); |
| 447 | skb->dev = dev; |
| 448 | |
| 449 | if (pkt_info.cmd_sts & ETH_LAYER_4_CHECKSUM_OK) { |
| 450 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 451 | skb->csum = htons( |
| 452 | (pkt_info.cmd_sts & 0x0007fff8) >> 3); |
| 453 | } |
| 454 | skb->protocol = eth_type_trans(skb, dev); |
| 455 | #ifdef MV643XX_NAPI |
| 456 | netif_receive_skb(skb); |
| 457 | #else |
| 458 | netif_rx(skb); |
| 459 | #endif |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | return received_packets; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * mv643xx_eth_int_handler |
| 468 | * |
| 469 | * Main interrupt handler for the gigbit ethernet ports |
| 470 | * |
| 471 | * Input : irq - irq number (not used) |
| 472 | * dev_id - a pointer to the required interface's data structure |
| 473 | * regs - not used |
| 474 | * Output : N/A |
| 475 | */ |
| 476 | |
| 477 | static irqreturn_t mv643xx_eth_int_handler(int irq, void *dev_id, |
| 478 | struct pt_regs *regs) |
| 479 | { |
| 480 | struct net_device *dev = (struct net_device *)dev_id; |
| 481 | struct mv643xx_private *mp = netdev_priv(dev); |
| 482 | u32 eth_int_cause, eth_int_cause_ext = 0; |
| 483 | unsigned int port_num = mp->port_num; |
| 484 | |
| 485 | /* Read interrupt cause registers */ |
| 486 | eth_int_cause = mv_read(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num)) & |
| 487 | INT_CAUSE_UNMASK_ALL; |
| 488 | |
| 489 | if (eth_int_cause & BIT1) |
| 490 | eth_int_cause_ext = mv_read( |
| 491 | MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num)) & |
| 492 | INT_CAUSE_UNMASK_ALL_EXT; |
| 493 | |
| 494 | #ifdef MV643XX_NAPI |
| 495 | if (!(eth_int_cause & 0x0007fffd)) { |
| 496 | /* Dont ack the Rx interrupt */ |
| 497 | #endif |
| 498 | /* |
| 499 | * Clear specific ethernet port intrerrupt registers by |
| 500 | * acknowleding relevant bits. |
| 501 | */ |
| 502 | mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), |
| 503 | ~eth_int_cause); |
| 504 | if (eth_int_cause_ext != 0x0) |
| 505 | mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG |
| 506 | (port_num), ~eth_int_cause_ext); |
| 507 | |
| 508 | /* UDP change : We may need this */ |
| 509 | if ((eth_int_cause_ext & 0x0000ffff) && |
| 510 | (mv643xx_eth_free_tx_queue(dev, eth_int_cause_ext) == 0) && |
| 511 | (mp->tx_ring_size > mp->tx_ring_skbs + MAX_DESCS_PER_SKB)) |
| 512 | netif_wake_queue(dev); |
| 513 | #ifdef MV643XX_NAPI |
| 514 | } else { |
| 515 | if (netif_rx_schedule_prep(dev)) { |
| 516 | /* Mask all the interrupts */ |
| 517 | mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), 0); |
| 518 | mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG |
| 519 | (port_num), 0); |
| 520 | __netif_rx_schedule(dev); |
| 521 | } |
| 522 | #else |
| 523 | if (eth_int_cause & (BIT2 | BIT11)) |
| 524 | mv643xx_eth_receive_queue(dev, 0); |
| 525 | |
| 526 | /* |
| 527 | * After forwarded received packets to upper layer, add a task |
| 528 | * in an interrupts enabled context that refills the RX ring |
| 529 | * with skb's. |
| 530 | */ |
| 531 | #ifdef MV643XX_RX_QUEUE_FILL_ON_TASK |
| 532 | /* Unmask all interrupts on ethernet port */ |
| 533 | mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), |
| 534 | INT_CAUSE_MASK_ALL); |
| 535 | queue_task(&mp->rx_task, &tq_immediate); |
| 536 | mark_bh(IMMEDIATE_BH); |
| 537 | #else |
| 538 | mp->rx_task.func(dev); |
| 539 | #endif |
| 540 | #endif |
| 541 | } |
| 542 | /* PHY status changed */ |
| 543 | if (eth_int_cause_ext & (BIT16 | BIT20)) { |
| 544 | if (eth_port_link_is_up(port_num)) { |
| 545 | netif_carrier_on(dev); |
| 546 | netif_wake_queue(dev); |
| 547 | /* Start TX queue */ |
| 548 | mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG |
| 549 | (port_num), 1); |
| 550 | } else { |
| 551 | netif_carrier_off(dev); |
| 552 | netif_stop_queue(dev); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /* |
| 557 | * If no real interrupt occured, exit. |
| 558 | * This can happen when using gigE interrupt coalescing mechanism. |
| 559 | */ |
| 560 | if ((eth_int_cause == 0x0) && (eth_int_cause_ext == 0x0)) |
| 561 | return IRQ_NONE; |
| 562 | |
| 563 | return IRQ_HANDLED; |
| 564 | } |
| 565 | |
| 566 | #ifdef MV643XX_COAL |
| 567 | |
| 568 | /* |
| 569 | * eth_port_set_rx_coal - Sets coalescing interrupt mechanism on RX path |
| 570 | * |
| 571 | * DESCRIPTION: |
| 572 | * This routine sets the RX coalescing interrupt mechanism parameter. |
| 573 | * This parameter is a timeout counter, that counts in 64 t_clk |
| 574 | * chunks ; that when timeout event occurs a maskable interrupt |
| 575 | * occurs. |
| 576 | * The parameter is calculated using the tClk of the MV-643xx chip |
| 577 | * , and the required delay of the interrupt in usec. |
| 578 | * |
| 579 | * INPUT: |
| 580 | * unsigned int eth_port_num Ethernet port number |
| 581 | * unsigned int t_clk t_clk of the MV-643xx chip in HZ units |
| 582 | * unsigned int delay Delay in usec |
| 583 | * |
| 584 | * OUTPUT: |
| 585 | * Interrupt coalescing mechanism value is set in MV-643xx chip. |
| 586 | * |
| 587 | * RETURN: |
| 588 | * The interrupt coalescing value set in the gigE port. |
| 589 | * |
| 590 | */ |
| 591 | static unsigned int eth_port_set_rx_coal(unsigned int eth_port_num, |
| 592 | unsigned int t_clk, unsigned int delay) |
| 593 | { |
| 594 | unsigned int coal = ((t_clk / 1000000) * delay) / 64; |
| 595 | |
| 596 | /* Set RX Coalescing mechanism */ |
| 597 | mv_write(MV643XX_ETH_SDMA_CONFIG_REG(eth_port_num), |
| 598 | ((coal & 0x3fff) << 8) | |
| 599 | (mv_read(MV643XX_ETH_SDMA_CONFIG_REG(eth_port_num)) |
| 600 | & 0xffc000ff)); |
| 601 | |
| 602 | return coal; |
| 603 | } |
| 604 | #endif |
| 605 | |
| 606 | /* |
| 607 | * eth_port_set_tx_coal - Sets coalescing interrupt mechanism on TX path |
| 608 | * |
| 609 | * DESCRIPTION: |
| 610 | * This routine sets the TX coalescing interrupt mechanism parameter. |
| 611 | * This parameter is a timeout counter, that counts in 64 t_clk |
| 612 | * chunks ; that when timeout event occurs a maskable interrupt |
| 613 | * occurs. |
| 614 | * The parameter is calculated using the t_cLK frequency of the |
| 615 | * MV-643xx chip and the required delay in the interrupt in uSec |
| 616 | * |
| 617 | * INPUT: |
| 618 | * unsigned int eth_port_num Ethernet port number |
| 619 | * unsigned int t_clk t_clk of the MV-643xx chip in HZ units |
| 620 | * unsigned int delay Delay in uSeconds |
| 621 | * |
| 622 | * OUTPUT: |
| 623 | * Interrupt coalescing mechanism value is set in MV-643xx chip. |
| 624 | * |
| 625 | * RETURN: |
| 626 | * The interrupt coalescing value set in the gigE port. |
| 627 | * |
| 628 | */ |
| 629 | static unsigned int eth_port_set_tx_coal(unsigned int eth_port_num, |
| 630 | unsigned int t_clk, unsigned int delay) |
| 631 | { |
| 632 | unsigned int coal; |
| 633 | coal = ((t_clk / 1000000) * delay) / 64; |
| 634 | /* Set TX Coalescing mechanism */ |
| 635 | mv_write(MV643XX_ETH_TX_FIFO_URGENT_THRESHOLD_REG(eth_port_num), |
| 636 | coal << 4); |
| 637 | return coal; |
| 638 | } |
| 639 | |
| 640 | /* |
| 641 | * mv643xx_eth_open |
| 642 | * |
| 643 | * This function is called when openning the network device. The function |
| 644 | * should initialize all the hardware, initialize cyclic Rx/Tx |
| 645 | * descriptors chain and buffers and allocate an IRQ to the network |
| 646 | * device. |
| 647 | * |
| 648 | * Input : a pointer to the network device structure |
| 649 | * |
| 650 | * Output : zero of success , nonzero if fails. |
| 651 | */ |
| 652 | |
| 653 | static int mv643xx_eth_open(struct net_device *dev) |
| 654 | { |
| 655 | struct mv643xx_private *mp = netdev_priv(dev); |
| 656 | unsigned int port_num = mp->port_num; |
| 657 | int err; |
| 658 | |
| 659 | spin_lock_irq(&mp->lock); |
| 660 | |
| 661 | err = request_irq(dev->irq, mv643xx_eth_int_handler, |
Benjamin Herrenschmidt | 16b8175 | 2005-04-16 15:24:30 -0700 | [diff] [blame] | 662 | SA_SHIRQ | SA_SAMPLE_RANDOM, dev->name, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | |
| 664 | if (err) { |
| 665 | printk(KERN_ERR "Can not assign IRQ number to MV643XX_eth%d\n", |
| 666 | port_num); |
| 667 | err = -EAGAIN; |
| 668 | goto out; |
| 669 | } |
| 670 | |
| 671 | if (mv643xx_eth_real_open(dev)) { |
| 672 | printk("%s: Error opening interface\n", dev->name); |
| 673 | err = -EBUSY; |
| 674 | goto out_free; |
| 675 | } |
| 676 | |
| 677 | spin_unlock_irq(&mp->lock); |
| 678 | |
| 679 | return 0; |
| 680 | |
| 681 | out_free: |
| 682 | free_irq(dev->irq, dev); |
| 683 | |
| 684 | out: |
| 685 | spin_unlock_irq(&mp->lock); |
| 686 | |
| 687 | return err; |
| 688 | } |
| 689 | |
| 690 | /* |
| 691 | * ether_init_rx_desc_ring - Curve a Rx chain desc list and buffer in memory. |
| 692 | * |
| 693 | * DESCRIPTION: |
| 694 | * This function prepares a Rx chained list of descriptors and packet |
| 695 | * buffers in a form of a ring. The routine must be called after port |
| 696 | * initialization routine and before port start routine. |
| 697 | * The Ethernet SDMA engine uses CPU bus addresses to access the various |
| 698 | * devices in the system (i.e. DRAM). This function uses the ethernet |
| 699 | * struct 'virtual to physical' routine (set by the user) to set the ring |
| 700 | * with physical addresses. |
| 701 | * |
| 702 | * INPUT: |
| 703 | * struct mv643xx_private *mp Ethernet Port Control srtuct. |
| 704 | * |
| 705 | * OUTPUT: |
| 706 | * The routine updates the Ethernet port control struct with information |
| 707 | * regarding the Rx descriptors and buffers. |
| 708 | * |
| 709 | * RETURN: |
| 710 | * None. |
| 711 | */ |
| 712 | static void ether_init_rx_desc_ring(struct mv643xx_private *mp) |
| 713 | { |
| 714 | volatile struct eth_rx_desc *p_rx_desc; |
| 715 | int rx_desc_num = mp->rx_ring_size; |
| 716 | int i; |
| 717 | |
| 718 | /* initialize the next_desc_ptr links in the Rx descriptors ring */ |
| 719 | p_rx_desc = (struct eth_rx_desc *)mp->p_rx_desc_area; |
| 720 | for (i = 0; i < rx_desc_num; i++) { |
| 721 | p_rx_desc[i].next_desc_ptr = mp->rx_desc_dma + |
| 722 | ((i + 1) % rx_desc_num) * sizeof(struct eth_rx_desc); |
| 723 | } |
| 724 | |
| 725 | /* Save Rx desc pointer to driver struct. */ |
| 726 | mp->rx_curr_desc_q = 0; |
| 727 | mp->rx_used_desc_q = 0; |
| 728 | |
| 729 | mp->rx_desc_area_size = rx_desc_num * sizeof(struct eth_rx_desc); |
| 730 | |
| 731 | /* Add the queue to the list of RX queues of this port */ |
| 732 | mp->port_rx_queue_command |= 1; |
| 733 | } |
| 734 | |
| 735 | /* |
| 736 | * ether_init_tx_desc_ring - Curve a Tx chain desc list and buffer in memory. |
| 737 | * |
| 738 | * DESCRIPTION: |
| 739 | * This function prepares a Tx chained list of descriptors and packet |
| 740 | * buffers in a form of a ring. The routine must be called after port |
| 741 | * initialization routine and before port start routine. |
| 742 | * The Ethernet SDMA engine uses CPU bus addresses to access the various |
| 743 | * devices in the system (i.e. DRAM). This function uses the ethernet |
| 744 | * struct 'virtual to physical' routine (set by the user) to set the ring |
| 745 | * with physical addresses. |
| 746 | * |
| 747 | * INPUT: |
| 748 | * struct mv643xx_private *mp Ethernet Port Control srtuct. |
| 749 | * |
| 750 | * OUTPUT: |
| 751 | * The routine updates the Ethernet port control struct with information |
| 752 | * regarding the Tx descriptors and buffers. |
| 753 | * |
| 754 | * RETURN: |
| 755 | * None. |
| 756 | */ |
| 757 | static void ether_init_tx_desc_ring(struct mv643xx_private *mp) |
| 758 | { |
| 759 | int tx_desc_num = mp->tx_ring_size; |
| 760 | struct eth_tx_desc *p_tx_desc; |
| 761 | int i; |
| 762 | |
| 763 | /* Initialize the next_desc_ptr links in the Tx descriptors ring */ |
| 764 | p_tx_desc = (struct eth_tx_desc *)mp->p_tx_desc_area; |
| 765 | for (i = 0; i < tx_desc_num; i++) { |
| 766 | p_tx_desc[i].next_desc_ptr = mp->tx_desc_dma + |
| 767 | ((i + 1) % tx_desc_num) * sizeof(struct eth_tx_desc); |
| 768 | } |
| 769 | |
| 770 | mp->tx_curr_desc_q = 0; |
| 771 | mp->tx_used_desc_q = 0; |
| 772 | #ifdef MV643XX_CHECKSUM_OFFLOAD_TX |
| 773 | mp->tx_first_desc_q = 0; |
| 774 | #endif |
| 775 | |
| 776 | mp->tx_desc_area_size = tx_desc_num * sizeof(struct eth_tx_desc); |
| 777 | |
| 778 | /* Add the queue to the list of Tx queues of this port */ |
| 779 | mp->port_tx_queue_command |= 1; |
| 780 | } |
| 781 | |
| 782 | /* Helper function for mv643xx_eth_open */ |
| 783 | static int mv643xx_eth_real_open(struct net_device *dev) |
| 784 | { |
| 785 | struct mv643xx_private *mp = netdev_priv(dev); |
| 786 | unsigned int port_num = mp->port_num; |
| 787 | unsigned int size; |
| 788 | |
| 789 | /* Stop RX Queues */ |
| 790 | mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), 0x0000ff00); |
| 791 | |
| 792 | /* Clear the ethernet port interrupts */ |
| 793 | mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0); |
| 794 | mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0); |
| 795 | |
| 796 | /* Unmask RX buffer and TX end interrupt */ |
| 797 | mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), |
| 798 | INT_CAUSE_UNMASK_ALL); |
| 799 | |
| 800 | /* Unmask phy and link status changes interrupts */ |
| 801 | mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num), |
| 802 | INT_CAUSE_UNMASK_ALL_EXT); |
| 803 | |
| 804 | /* Set the MAC Address */ |
| 805 | memcpy(mp->port_mac_addr, dev->dev_addr, 6); |
| 806 | |
| 807 | eth_port_init(mp); |
| 808 | |
| 809 | INIT_WORK(&mp->rx_task, (void (*)(void *))mv643xx_eth_rx_task, dev); |
| 810 | |
| 811 | memset(&mp->timeout, 0, sizeof(struct timer_list)); |
| 812 | mp->timeout.function = mv643xx_eth_rx_task_timer_wrapper; |
| 813 | mp->timeout.data = (unsigned long)dev; |
| 814 | |
| 815 | mp->rx_task_busy = 0; |
| 816 | mp->rx_timer_flag = 0; |
| 817 | |
| 818 | /* Allocate RX and TX skb rings */ |
| 819 | mp->rx_skb = kmalloc(sizeof(*mp->rx_skb) * mp->rx_ring_size, |
| 820 | GFP_KERNEL); |
| 821 | if (!mp->rx_skb) { |
| 822 | printk(KERN_ERR "%s: Cannot allocate Rx skb ring\n", dev->name); |
| 823 | return -ENOMEM; |
| 824 | } |
| 825 | mp->tx_skb = kmalloc(sizeof(*mp->tx_skb) * mp->tx_ring_size, |
| 826 | GFP_KERNEL); |
| 827 | if (!mp->tx_skb) { |
| 828 | printk(KERN_ERR "%s: Cannot allocate Tx skb ring\n", dev->name); |
| 829 | kfree(mp->rx_skb); |
| 830 | return -ENOMEM; |
| 831 | } |
| 832 | |
| 833 | /* Allocate TX ring */ |
| 834 | mp->tx_ring_skbs = 0; |
| 835 | size = mp->tx_ring_size * sizeof(struct eth_tx_desc); |
| 836 | mp->tx_desc_area_size = size; |
| 837 | |
| 838 | if (mp->tx_sram_size) { |
| 839 | mp->p_tx_desc_area = ioremap(mp->tx_sram_addr, |
| 840 | mp->tx_sram_size); |
| 841 | mp->tx_desc_dma = mp->tx_sram_addr; |
| 842 | } else |
| 843 | mp->p_tx_desc_area = dma_alloc_coherent(NULL, size, |
| 844 | &mp->tx_desc_dma, |
| 845 | GFP_KERNEL); |
| 846 | |
| 847 | if (!mp->p_tx_desc_area) { |
| 848 | printk(KERN_ERR "%s: Cannot allocate Tx Ring (size %d bytes)\n", |
| 849 | dev->name, size); |
| 850 | kfree(mp->rx_skb); |
| 851 | kfree(mp->tx_skb); |
| 852 | return -ENOMEM; |
| 853 | } |
| 854 | BUG_ON((u32) mp->p_tx_desc_area & 0xf); /* check 16-byte alignment */ |
| 855 | memset((void *)mp->p_tx_desc_area, 0, mp->tx_desc_area_size); |
| 856 | |
| 857 | ether_init_tx_desc_ring(mp); |
| 858 | |
| 859 | /* Allocate RX ring */ |
| 860 | mp->rx_ring_skbs = 0; |
| 861 | size = mp->rx_ring_size * sizeof(struct eth_rx_desc); |
| 862 | mp->rx_desc_area_size = size; |
| 863 | |
| 864 | if (mp->rx_sram_size) { |
| 865 | mp->p_rx_desc_area = ioremap(mp->rx_sram_addr, |
| 866 | mp->rx_sram_size); |
| 867 | mp->rx_desc_dma = mp->rx_sram_addr; |
| 868 | } else |
| 869 | mp->p_rx_desc_area = dma_alloc_coherent(NULL, size, |
| 870 | &mp->rx_desc_dma, |
| 871 | GFP_KERNEL); |
| 872 | |
| 873 | if (!mp->p_rx_desc_area) { |
| 874 | printk(KERN_ERR "%s: Cannot allocate Rx ring (size %d bytes)\n", |
| 875 | dev->name, size); |
| 876 | printk(KERN_ERR "%s: Freeing previously allocated TX queues...", |
| 877 | dev->name); |
| 878 | if (mp->rx_sram_size) |
| 879 | iounmap(mp->p_rx_desc_area); |
| 880 | else |
| 881 | dma_free_coherent(NULL, mp->tx_desc_area_size, |
| 882 | mp->p_tx_desc_area, mp->tx_desc_dma); |
| 883 | kfree(mp->rx_skb); |
| 884 | kfree(mp->tx_skb); |
| 885 | return -ENOMEM; |
| 886 | } |
| 887 | memset((void *)mp->p_rx_desc_area, 0, size); |
| 888 | |
| 889 | ether_init_rx_desc_ring(mp); |
| 890 | |
| 891 | mv643xx_eth_rx_task(dev); /* Fill RX ring with skb's */ |
| 892 | |
| 893 | eth_port_start(mp); |
| 894 | |
| 895 | /* Interrupt Coalescing */ |
| 896 | |
| 897 | #ifdef MV643XX_COAL |
| 898 | mp->rx_int_coal = |
| 899 | eth_port_set_rx_coal(port_num, 133000000, MV643XX_RX_COAL); |
| 900 | #endif |
| 901 | |
| 902 | mp->tx_int_coal = |
| 903 | eth_port_set_tx_coal(port_num, 133000000, MV643XX_TX_COAL); |
| 904 | |
| 905 | netif_start_queue(dev); |
| 906 | |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | static void mv643xx_eth_free_tx_rings(struct net_device *dev) |
| 911 | { |
| 912 | struct mv643xx_private *mp = netdev_priv(dev); |
| 913 | unsigned int port_num = mp->port_num; |
| 914 | unsigned int curr; |
| 915 | |
| 916 | /* Stop Tx Queues */ |
| 917 | mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num), 0x0000ff00); |
| 918 | |
| 919 | /* Free outstanding skb's on TX rings */ |
| 920 | for (curr = 0; mp->tx_ring_skbs && curr < mp->tx_ring_size; curr++) { |
| 921 | if (mp->tx_skb[curr]) { |
| 922 | dev_kfree_skb(mp->tx_skb[curr]); |
| 923 | mp->tx_ring_skbs--; |
| 924 | } |
| 925 | } |
| 926 | if (mp->tx_ring_skbs) |
| 927 | printk("%s: Error on Tx descriptor free - could not free %d" |
| 928 | " descriptors\n", dev->name, mp->tx_ring_skbs); |
| 929 | |
| 930 | /* Free TX ring */ |
| 931 | if (mp->tx_sram_size) |
| 932 | iounmap(mp->p_tx_desc_area); |
| 933 | else |
| 934 | dma_free_coherent(NULL, mp->tx_desc_area_size, |
| 935 | mp->p_tx_desc_area, mp->tx_desc_dma); |
| 936 | } |
| 937 | |
| 938 | static void mv643xx_eth_free_rx_rings(struct net_device *dev) |
| 939 | { |
| 940 | struct mv643xx_private *mp = netdev_priv(dev); |
| 941 | unsigned int port_num = mp->port_num; |
| 942 | int curr; |
| 943 | |
| 944 | /* Stop RX Queues */ |
| 945 | mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), 0x0000ff00); |
| 946 | |
| 947 | /* Free preallocated skb's on RX rings */ |
| 948 | for (curr = 0; mp->rx_ring_skbs && curr < mp->rx_ring_size; curr++) { |
| 949 | if (mp->rx_skb[curr]) { |
| 950 | dev_kfree_skb(mp->rx_skb[curr]); |
| 951 | mp->rx_ring_skbs--; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | if (mp->rx_ring_skbs) |
| 956 | printk(KERN_ERR |
| 957 | "%s: Error in freeing Rx Ring. %d skb's still" |
| 958 | " stuck in RX Ring - ignoring them\n", dev->name, |
| 959 | mp->rx_ring_skbs); |
| 960 | /* Free RX ring */ |
| 961 | if (mp->rx_sram_size) |
| 962 | iounmap(mp->p_rx_desc_area); |
| 963 | else |
| 964 | dma_free_coherent(NULL, mp->rx_desc_area_size, |
| 965 | mp->p_rx_desc_area, mp->rx_desc_dma); |
| 966 | } |
| 967 | |
| 968 | /* |
| 969 | * mv643xx_eth_stop |
| 970 | * |
| 971 | * This function is used when closing the network device. |
| 972 | * It updates the hardware, |
| 973 | * release all memory that holds buffers and descriptors and release the IRQ. |
| 974 | * Input : a pointer to the device structure |
| 975 | * Output : zero if success , nonzero if fails |
| 976 | */ |
| 977 | |
| 978 | /* Helper function for mv643xx_eth_stop */ |
| 979 | |
| 980 | static int mv643xx_eth_real_stop(struct net_device *dev) |
| 981 | { |
| 982 | struct mv643xx_private *mp = netdev_priv(dev); |
| 983 | unsigned int port_num = mp->port_num; |
| 984 | |
| 985 | netif_carrier_off(dev); |
| 986 | netif_stop_queue(dev); |
| 987 | |
| 988 | mv643xx_eth_free_tx_rings(dev); |
| 989 | mv643xx_eth_free_rx_rings(dev); |
| 990 | |
| 991 | eth_port_reset(mp->port_num); |
| 992 | |
| 993 | /* Disable ethernet port interrupts */ |
| 994 | mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0); |
| 995 | mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0); |
| 996 | |
| 997 | /* Mask RX buffer and TX end interrupt */ |
| 998 | mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), 0); |
| 999 | |
| 1000 | /* Mask phy and link status changes interrupts */ |
| 1001 | mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num), 0); |
| 1002 | |
| 1003 | return 0; |
| 1004 | } |
| 1005 | |
| 1006 | static int mv643xx_eth_stop(struct net_device *dev) |
| 1007 | { |
| 1008 | struct mv643xx_private *mp = netdev_priv(dev); |
| 1009 | |
| 1010 | spin_lock_irq(&mp->lock); |
| 1011 | |
| 1012 | mv643xx_eth_real_stop(dev); |
| 1013 | |
| 1014 | free_irq(dev->irq, dev); |
| 1015 | spin_unlock_irq(&mp->lock); |
| 1016 | |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | #ifdef MV643XX_NAPI |
| 1021 | static void mv643xx_tx(struct net_device *dev) |
| 1022 | { |
| 1023 | struct mv643xx_private *mp = netdev_priv(dev); |
| 1024 | struct pkt_info pkt_info; |
| 1025 | |
| 1026 | while (eth_tx_return_desc(mp, &pkt_info) == ETH_OK) { |
| 1027 | if (pkt_info.return_info) { |
| 1028 | if (skb_shinfo(pkt_info.return_info)->nr_frags) |
| 1029 | dma_unmap_page(NULL, pkt_info.buf_ptr, |
| 1030 | pkt_info.byte_cnt, |
| 1031 | DMA_TO_DEVICE); |
| 1032 | else |
| 1033 | dma_unmap_single(NULL, pkt_info.buf_ptr, |
| 1034 | pkt_info.byte_cnt, |
| 1035 | DMA_TO_DEVICE); |
| 1036 | |
| 1037 | dev_kfree_skb_irq(pkt_info.return_info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } else |
| 1039 | dma_unmap_page(NULL, pkt_info.buf_ptr, |
| 1040 | pkt_info.byte_cnt, DMA_TO_DEVICE); |
| 1041 | } |
| 1042 | |
| 1043 | if (netif_queue_stopped(dev) && |
| 1044 | mp->tx_ring_size > mp->tx_ring_skbs + MAX_DESCS_PER_SKB) |
| 1045 | netif_wake_queue(dev); |
| 1046 | } |
| 1047 | |
| 1048 | /* |
| 1049 | * mv643xx_poll |
| 1050 | * |
| 1051 | * This function is used in case of NAPI |
| 1052 | */ |
| 1053 | static int mv643xx_poll(struct net_device *dev, int *budget) |
| 1054 | { |
| 1055 | struct mv643xx_private *mp = netdev_priv(dev); |
| 1056 | int done = 1, orig_budget, work_done; |
| 1057 | unsigned int port_num = mp->port_num; |
| 1058 | unsigned long flags; |
| 1059 | |
| 1060 | #ifdef MV643XX_TX_FAST_REFILL |
| 1061 | if (++mp->tx_clean_threshold > 5) { |
| 1062 | spin_lock_irqsave(&mp->lock, flags); |
| 1063 | mv643xx_tx(dev); |
| 1064 | mp->tx_clean_threshold = 0; |
| 1065 | spin_unlock_irqrestore(&mp->lock, flags); |
| 1066 | } |
| 1067 | #endif |
| 1068 | |
| 1069 | if ((mv_read(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num))) |
| 1070 | != (u32) mp->rx_used_desc_q) { |
| 1071 | orig_budget = *budget; |
| 1072 | if (orig_budget > dev->quota) |
| 1073 | orig_budget = dev->quota; |
| 1074 | work_done = mv643xx_eth_receive_queue(dev, orig_budget); |
| 1075 | mp->rx_task.func(dev); |
| 1076 | *budget -= work_done; |
| 1077 | dev->quota -= work_done; |
| 1078 | if (work_done >= orig_budget) |
| 1079 | done = 0; |
| 1080 | } |
| 1081 | |
| 1082 | if (done) { |
| 1083 | spin_lock_irqsave(&mp->lock, flags); |
| 1084 | __netif_rx_complete(dev); |
| 1085 | mv_write(MV643XX_ETH_INTERRUPT_CAUSE_REG(port_num), 0); |
| 1086 | mv_write(MV643XX_ETH_INTERRUPT_CAUSE_EXTEND_REG(port_num), 0); |
| 1087 | mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), |
| 1088 | INT_CAUSE_UNMASK_ALL); |
| 1089 | mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num), |
| 1090 | INT_CAUSE_UNMASK_ALL_EXT); |
| 1091 | spin_unlock_irqrestore(&mp->lock, flags); |
| 1092 | } |
| 1093 | |
| 1094 | return done ? 0 : 1; |
| 1095 | } |
| 1096 | #endif |
| 1097 | |
| 1098 | /* |
| 1099 | * mv643xx_eth_start_xmit |
| 1100 | * |
| 1101 | * This function is queues a packet in the Tx descriptor for |
| 1102 | * required port. |
| 1103 | * |
| 1104 | * Input : skb - a pointer to socket buffer |
| 1105 | * dev - a pointer to the required port |
| 1106 | * |
| 1107 | * Output : zero upon success |
| 1108 | */ |
| 1109 | static int mv643xx_eth_start_xmit(struct sk_buff *skb, struct net_device *dev) |
| 1110 | { |
| 1111 | struct mv643xx_private *mp = netdev_priv(dev); |
| 1112 | struct net_device_stats *stats = &mp->stats; |
| 1113 | ETH_FUNC_RET_STATUS status; |
| 1114 | unsigned long flags; |
| 1115 | struct pkt_info pkt_info; |
| 1116 | |
| 1117 | if (netif_queue_stopped(dev)) { |
| 1118 | printk(KERN_ERR |
| 1119 | "%s: Tried sending packet when interface is stopped\n", |
| 1120 | dev->name); |
| 1121 | return 1; |
| 1122 | } |
| 1123 | |
| 1124 | /* This is a hard error, log it. */ |
| 1125 | if ((mp->tx_ring_size - mp->tx_ring_skbs) <= |
| 1126 | (skb_shinfo(skb)->nr_frags + 1)) { |
| 1127 | netif_stop_queue(dev); |
| 1128 | printk(KERN_ERR |
| 1129 | "%s: Bug in mv643xx_eth - Trying to transmit when" |
| 1130 | " queue full !\n", dev->name); |
| 1131 | return 1; |
| 1132 | } |
| 1133 | |
| 1134 | /* Paranoid check - this shouldn't happen */ |
| 1135 | if (skb == NULL) { |
| 1136 | stats->tx_dropped++; |
| 1137 | printk(KERN_ERR "mv64320_eth paranoid check failed\n"); |
| 1138 | return 1; |
| 1139 | } |
| 1140 | |
| 1141 | spin_lock_irqsave(&mp->lock, flags); |
| 1142 | |
| 1143 | /* Update packet info data structure -- DMA owned, first last */ |
| 1144 | #ifdef MV643XX_CHECKSUM_OFFLOAD_TX |
| 1145 | if (!skb_shinfo(skb)->nr_frags) { |
| 1146 | linear: |
| 1147 | if (skb->ip_summed != CHECKSUM_HW) { |
Dale Farnsworth | 2600636 | 2005-08-22 15:53:29 -0700 | [diff] [blame] | 1148 | /* Errata BTS #50, IHL must be 5 if no HW checksum */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT | |
Dale Farnsworth | 2600636 | 2005-08-22 15:53:29 -0700 | [diff] [blame] | 1150 | ETH_TX_FIRST_DESC | |
| 1151 | ETH_TX_LAST_DESC | |
| 1152 | 5 << ETH_TX_IHL_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | pkt_info.l4i_chk = 0; |
| 1154 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | |
| 1156 | pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT | |
Dale Farnsworth | 2600636 | 2005-08-22 15:53:29 -0700 | [diff] [blame] | 1157 | ETH_TX_FIRST_DESC | |
| 1158 | ETH_TX_LAST_DESC | |
| 1159 | ETH_GEN_TCP_UDP_CHECKSUM | |
| 1160 | ETH_GEN_IP_V_4_CHECKSUM | |
| 1161 | skb->nh.iph->ihl << ETH_TX_IHL_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | /* CPU already calculated pseudo header checksum. */ |
| 1163 | if (skb->nh.iph->protocol == IPPROTO_UDP) { |
| 1164 | pkt_info.cmd_sts |= ETH_UDP_FRAME; |
| 1165 | pkt_info.l4i_chk = skb->h.uh->check; |
| 1166 | } else if (skb->nh.iph->protocol == IPPROTO_TCP) |
| 1167 | pkt_info.l4i_chk = skb->h.th->check; |
| 1168 | else { |
| 1169 | printk(KERN_ERR |
| 1170 | "%s: chksum proto != TCP or UDP\n", |
| 1171 | dev->name); |
| 1172 | spin_unlock_irqrestore(&mp->lock, flags); |
| 1173 | return 1; |
| 1174 | } |
| 1175 | } |
| 1176 | pkt_info.byte_cnt = skb->len; |
| 1177 | pkt_info.buf_ptr = dma_map_single(NULL, skb->data, skb->len, |
| 1178 | DMA_TO_DEVICE); |
| 1179 | pkt_info.return_info = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | status = eth_port_send(mp, &pkt_info); |
| 1181 | if ((status == ETH_ERROR) || (status == ETH_QUEUE_FULL)) |
| 1182 | printk(KERN_ERR "%s: Error on transmitting packet\n", |
| 1183 | dev->name); |
| 1184 | stats->tx_bytes += pkt_info.byte_cnt; |
| 1185 | } else { |
| 1186 | unsigned int frag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | /* Since hardware can't handle unaligned fragments smaller |
| 1189 | * than 9 bytes, if we find any, we linearize the skb |
| 1190 | * and start again. When I've seen it, it's always been |
| 1191 | * the first frag (probably near the end of the page), |
| 1192 | * but we check all frags to be safe. |
| 1193 | */ |
| 1194 | for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) { |
| 1195 | skb_frag_t *fragp; |
| 1196 | |
| 1197 | fragp = &skb_shinfo(skb)->frags[frag]; |
| 1198 | if (fragp->size <= 8 && fragp->page_offset & 0x7) { |
| 1199 | skb_linearize(skb, GFP_ATOMIC); |
| 1200 | printk(KERN_DEBUG "%s: unaligned tiny fragment" |
| 1201 | "%d of %d, fixed\n", |
| 1202 | dev->name, frag, |
| 1203 | skb_shinfo(skb)->nr_frags); |
| 1204 | goto linear; |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | /* first frag which is skb header */ |
| 1209 | pkt_info.byte_cnt = skb_headlen(skb); |
| 1210 | pkt_info.buf_ptr = dma_map_single(NULL, skb->data, |
| 1211 | skb_headlen(skb), |
| 1212 | DMA_TO_DEVICE); |
| 1213 | pkt_info.l4i_chk = 0; |
| 1214 | pkt_info.return_info = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1215 | |
Dale Farnsworth | 2600636 | 2005-08-22 15:53:29 -0700 | [diff] [blame] | 1216 | if (skb->ip_summed != CHECKSUM_HW) |
| 1217 | /* Errata BTS #50, IHL must be 5 if no HW checksum */ |
| 1218 | pkt_info.cmd_sts = ETH_TX_FIRST_DESC | |
| 1219 | 5 << ETH_TX_IHL_SHIFT; |
| 1220 | else { |
| 1221 | pkt_info.cmd_sts = ETH_TX_FIRST_DESC | |
| 1222 | ETH_GEN_TCP_UDP_CHECKSUM | |
| 1223 | ETH_GEN_IP_V_4_CHECKSUM | |
| 1224 | skb->nh.iph->ihl << ETH_TX_IHL_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | /* CPU already calculated pseudo header checksum. */ |
| 1226 | if (skb->nh.iph->protocol == IPPROTO_UDP) { |
| 1227 | pkt_info.cmd_sts |= ETH_UDP_FRAME; |
| 1228 | pkt_info.l4i_chk = skb->h.uh->check; |
| 1229 | } else if (skb->nh.iph->protocol == IPPROTO_TCP) |
| 1230 | pkt_info.l4i_chk = skb->h.th->check; |
| 1231 | else { |
| 1232 | printk(KERN_ERR |
| 1233 | "%s: chksum proto != TCP or UDP\n", |
| 1234 | dev->name); |
| 1235 | spin_unlock_irqrestore(&mp->lock, flags); |
| 1236 | return 1; |
| 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | status = eth_port_send(mp, &pkt_info); |
| 1241 | if (status != ETH_OK) { |
| 1242 | if ((status == ETH_ERROR)) |
| 1243 | printk(KERN_ERR |
| 1244 | "%s: Error on transmitting packet\n", |
| 1245 | dev->name); |
| 1246 | if (status == ETH_QUEUE_FULL) |
| 1247 | printk("Error on Queue Full \n"); |
| 1248 | if (status == ETH_QUEUE_LAST_RESOURCE) |
| 1249 | printk("Tx resource error \n"); |
| 1250 | } |
| 1251 | stats->tx_bytes += pkt_info.byte_cnt; |
| 1252 | |
| 1253 | /* Check for the remaining frags */ |
| 1254 | for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) { |
| 1255 | skb_frag_t *this_frag = &skb_shinfo(skb)->frags[frag]; |
| 1256 | pkt_info.l4i_chk = 0x0000; |
| 1257 | pkt_info.cmd_sts = 0x00000000; |
| 1258 | |
| 1259 | /* Last Frag enables interrupt and frees the skb */ |
| 1260 | if (frag == (skb_shinfo(skb)->nr_frags - 1)) { |
| 1261 | pkt_info.cmd_sts |= ETH_TX_ENABLE_INTERRUPT | |
| 1262 | ETH_TX_LAST_DESC; |
| 1263 | pkt_info.return_info = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | } else { |
| 1265 | pkt_info.return_info = 0; |
| 1266 | } |
| 1267 | pkt_info.l4i_chk = 0; |
| 1268 | pkt_info.byte_cnt = this_frag->size; |
| 1269 | |
| 1270 | pkt_info.buf_ptr = dma_map_page(NULL, this_frag->page, |
| 1271 | this_frag->page_offset, |
| 1272 | this_frag->size, |
| 1273 | DMA_TO_DEVICE); |
| 1274 | |
| 1275 | status = eth_port_send(mp, &pkt_info); |
| 1276 | |
| 1277 | if (status != ETH_OK) { |
| 1278 | if ((status == ETH_ERROR)) |
| 1279 | printk(KERN_ERR "%s: Error on " |
| 1280 | "transmitting packet\n", |
| 1281 | dev->name); |
| 1282 | |
| 1283 | if (status == ETH_QUEUE_LAST_RESOURCE) |
| 1284 | printk("Tx resource error \n"); |
| 1285 | |
| 1286 | if (status == ETH_QUEUE_FULL) |
| 1287 | printk("Queue is full \n"); |
| 1288 | } |
| 1289 | stats->tx_bytes += pkt_info.byte_cnt; |
| 1290 | } |
| 1291 | } |
| 1292 | #else |
| 1293 | pkt_info.cmd_sts = ETH_TX_ENABLE_INTERRUPT | ETH_TX_FIRST_DESC | |
| 1294 | ETH_TX_LAST_DESC; |
| 1295 | pkt_info.l4i_chk = 0; |
| 1296 | pkt_info.byte_cnt = skb->len; |
| 1297 | pkt_info.buf_ptr = dma_map_single(NULL, skb->data, skb->len, |
| 1298 | DMA_TO_DEVICE); |
| 1299 | pkt_info.return_info = skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | status = eth_port_send(mp, &pkt_info); |
| 1301 | if ((status == ETH_ERROR) || (status == ETH_QUEUE_FULL)) |
| 1302 | printk(KERN_ERR "%s: Error on transmitting packet\n", |
| 1303 | dev->name); |
| 1304 | stats->tx_bytes += pkt_info.byte_cnt; |
| 1305 | #endif |
| 1306 | |
| 1307 | /* Check if TX queue can handle another skb. If not, then |
| 1308 | * signal higher layers to stop requesting TX |
| 1309 | */ |
| 1310 | if (mp->tx_ring_size <= (mp->tx_ring_skbs + MAX_DESCS_PER_SKB)) |
| 1311 | /* |
| 1312 | * Stop getting skb's from upper layers. |
| 1313 | * Getting skb's from upper layers will be enabled again after |
| 1314 | * packets are released. |
| 1315 | */ |
| 1316 | netif_stop_queue(dev); |
| 1317 | |
| 1318 | /* Update statistics and start of transmittion time */ |
| 1319 | stats->tx_packets++; |
| 1320 | dev->trans_start = jiffies; |
| 1321 | |
| 1322 | spin_unlock_irqrestore(&mp->lock, flags); |
| 1323 | |
| 1324 | return 0; /* success */ |
| 1325 | } |
| 1326 | |
| 1327 | /* |
| 1328 | * mv643xx_eth_get_stats |
| 1329 | * |
| 1330 | * Returns a pointer to the interface statistics. |
| 1331 | * |
| 1332 | * Input : dev - a pointer to the required interface |
| 1333 | * |
| 1334 | * Output : a pointer to the interface's statistics |
| 1335 | */ |
| 1336 | |
| 1337 | static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev) |
| 1338 | { |
| 1339 | struct mv643xx_private *mp = netdev_priv(dev); |
| 1340 | |
| 1341 | return &mp->stats; |
| 1342 | } |
| 1343 | |
Dale Farnsworth | 63c9e54 | 2005-09-02 13:49:10 -0700 | [diff] [blame] | 1344 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1345 | static inline void mv643xx_enable_irq(struct mv643xx_private *mp) |
| 1346 | { |
| 1347 | int port_num = mp->port_num; |
| 1348 | unsigned long flags; |
| 1349 | |
| 1350 | spin_lock_irqsave(&mp->lock, flags); |
| 1351 | mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), |
| 1352 | INT_CAUSE_UNMASK_ALL); |
| 1353 | mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num), |
| 1354 | INT_CAUSE_UNMASK_ALL_EXT); |
| 1355 | spin_unlock_irqrestore(&mp->lock, flags); |
| 1356 | } |
| 1357 | |
| 1358 | static inline void mv643xx_disable_irq(struct mv643xx_private *mp) |
| 1359 | { |
| 1360 | int port_num = mp->port_num; |
| 1361 | unsigned long flags; |
| 1362 | |
| 1363 | spin_lock_irqsave(&mp->lock, flags); |
| 1364 | mv_write(MV643XX_ETH_INTERRUPT_MASK_REG(port_num), |
| 1365 | INT_CAUSE_MASK_ALL); |
| 1366 | mv_write(MV643XX_ETH_INTERRUPT_EXTEND_MASK_REG(port_num), |
| 1367 | INT_CAUSE_MASK_ALL_EXT); |
| 1368 | spin_unlock_irqrestore(&mp->lock, flags); |
| 1369 | } |
| 1370 | |
| 1371 | static void mv643xx_netpoll(struct net_device *netdev) |
| 1372 | { |
| 1373 | struct mv643xx_private *mp = netdev_priv(netdev); |
| 1374 | |
| 1375 | mv643xx_disable_irq(mp); |
| 1376 | mv643xx_eth_int_handler(netdev->irq, netdev, NULL); |
| 1377 | mv643xx_enable_irq(mp); |
| 1378 | } |
| 1379 | #endif |
| 1380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | /*/ |
| 1382 | * mv643xx_eth_probe |
| 1383 | * |
| 1384 | * First function called after registering the network device. |
| 1385 | * It's purpose is to initialize the device as an ethernet device, |
| 1386 | * fill the ethernet device structure with pointers * to functions, |
| 1387 | * and set the MAC address of the interface |
| 1388 | * |
| 1389 | * Input : struct device * |
| 1390 | * Output : -ENOMEM if failed , 0 if success |
| 1391 | */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1392 | static int mv643xx_eth_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | struct mv643xx_eth_platform_data *pd; |
| 1395 | int port_num = pdev->id; |
| 1396 | struct mv643xx_private *mp; |
| 1397 | struct net_device *dev; |
| 1398 | u8 *p; |
| 1399 | struct resource *res; |
| 1400 | int err; |
| 1401 | |
| 1402 | dev = alloc_etherdev(sizeof(struct mv643xx_private)); |
| 1403 | if (!dev) |
| 1404 | return -ENOMEM; |
| 1405 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1406 | platform_set_drvdata(pdev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1407 | |
| 1408 | mp = netdev_priv(dev); |
| 1409 | |
| 1410 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 1411 | BUG_ON(!res); |
| 1412 | dev->irq = res->start; |
| 1413 | |
| 1414 | mp->port_num = port_num; |
| 1415 | |
| 1416 | dev->open = mv643xx_eth_open; |
| 1417 | dev->stop = mv643xx_eth_stop; |
| 1418 | dev->hard_start_xmit = mv643xx_eth_start_xmit; |
| 1419 | dev->get_stats = mv643xx_eth_get_stats; |
| 1420 | dev->set_mac_address = mv643xx_eth_set_mac_address; |
| 1421 | dev->set_multicast_list = mv643xx_eth_set_rx_mode; |
| 1422 | |
| 1423 | /* No need to Tx Timeout */ |
| 1424 | dev->tx_timeout = mv643xx_eth_tx_timeout; |
| 1425 | #ifdef MV643XX_NAPI |
| 1426 | dev->poll = mv643xx_poll; |
| 1427 | dev->weight = 64; |
| 1428 | #endif |
| 1429 | |
Dale Farnsworth | 63c9e54 | 2005-09-02 13:49:10 -0700 | [diff] [blame] | 1430 | #ifdef CONFIG_NET_POLL_CONTROLLER |
| 1431 | dev->poll_controller = mv643xx_netpoll; |
| 1432 | #endif |
| 1433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | dev->watchdog_timeo = 2 * HZ; |
| 1435 | dev->tx_queue_len = mp->tx_ring_size; |
| 1436 | dev->base_addr = 0; |
| 1437 | dev->change_mtu = mv643xx_eth_change_mtu; |
| 1438 | SET_ETHTOOL_OPS(dev, &mv643xx_ethtool_ops); |
| 1439 | |
| 1440 | #ifdef MV643XX_CHECKSUM_OFFLOAD_TX |
| 1441 | #ifdef MAX_SKB_FRAGS |
| 1442 | /* |
| 1443 | * Zero copy can only work if we use Discovery II memory. Else, we will |
| 1444 | * have to map the buffers to ISA memory which is only 16 MB |
| 1445 | */ |
| 1446 | dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_HW_CSUM; |
| 1447 | #endif |
| 1448 | #endif |
| 1449 | |
| 1450 | /* Configure the timeout task */ |
| 1451 | INIT_WORK(&mp->tx_timeout_task, |
| 1452 | (void (*)(void *))mv643xx_eth_tx_timeout_task, dev); |
| 1453 | |
| 1454 | spin_lock_init(&mp->lock); |
| 1455 | |
| 1456 | /* set default config values */ |
| 1457 | eth_port_uc_addr_get(dev, dev->dev_addr); |
| 1458 | mp->port_config = MV643XX_ETH_PORT_CONFIG_DEFAULT_VALUE; |
| 1459 | mp->port_config_extend = MV643XX_ETH_PORT_CONFIG_EXTEND_DEFAULT_VALUE; |
| 1460 | mp->port_sdma_config = MV643XX_ETH_PORT_SDMA_CONFIG_DEFAULT_VALUE; |
| 1461 | mp->port_serial_control = MV643XX_ETH_PORT_SERIAL_CONTROL_DEFAULT_VALUE; |
| 1462 | mp->rx_ring_size = MV643XX_ETH_PORT_DEFAULT_RECEIVE_QUEUE_SIZE; |
| 1463 | mp->tx_ring_size = MV643XX_ETH_PORT_DEFAULT_TRANSMIT_QUEUE_SIZE; |
| 1464 | |
| 1465 | pd = pdev->dev.platform_data; |
| 1466 | if (pd) { |
| 1467 | if (pd->mac_addr != NULL) |
| 1468 | memcpy(dev->dev_addr, pd->mac_addr, 6); |
| 1469 | |
| 1470 | if (pd->phy_addr || pd->force_phy_addr) |
| 1471 | ethernet_phy_set(port_num, pd->phy_addr); |
| 1472 | |
| 1473 | if (pd->port_config || pd->force_port_config) |
| 1474 | mp->port_config = pd->port_config; |
| 1475 | |
| 1476 | if (pd->port_config_extend || pd->force_port_config_extend) |
| 1477 | mp->port_config_extend = pd->port_config_extend; |
| 1478 | |
| 1479 | if (pd->port_sdma_config || pd->force_port_sdma_config) |
| 1480 | mp->port_sdma_config = pd->port_sdma_config; |
| 1481 | |
| 1482 | if (pd->port_serial_control || pd->force_port_serial_control) |
| 1483 | mp->port_serial_control = pd->port_serial_control; |
| 1484 | |
| 1485 | if (pd->rx_queue_size) |
| 1486 | mp->rx_ring_size = pd->rx_queue_size; |
| 1487 | |
| 1488 | if (pd->tx_queue_size) |
| 1489 | mp->tx_ring_size = pd->tx_queue_size; |
| 1490 | |
| 1491 | if (pd->tx_sram_size) { |
| 1492 | mp->tx_sram_size = pd->tx_sram_size; |
| 1493 | mp->tx_sram_addr = pd->tx_sram_addr; |
| 1494 | } |
| 1495 | |
| 1496 | if (pd->rx_sram_size) { |
| 1497 | mp->rx_sram_size = pd->rx_sram_size; |
| 1498 | mp->rx_sram_addr = pd->rx_sram_addr; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | err = ethernet_phy_detect(port_num); |
| 1503 | if (err) { |
| 1504 | pr_debug("MV643xx ethernet port %d: " |
| 1505 | "No PHY detected at addr %d\n", |
| 1506 | port_num, ethernet_phy_get(port_num)); |
| 1507 | return err; |
| 1508 | } |
| 1509 | |
| 1510 | err = register_netdev(dev); |
| 1511 | if (err) |
| 1512 | goto out; |
| 1513 | |
| 1514 | p = dev->dev_addr; |
| 1515 | printk(KERN_NOTICE |
| 1516 | "%s: port %d with MAC address %02x:%02x:%02x:%02x:%02x:%02x\n", |
| 1517 | dev->name, port_num, p[0], p[1], p[2], p[3], p[4], p[5]); |
| 1518 | |
| 1519 | if (dev->features & NETIF_F_SG) |
| 1520 | printk(KERN_NOTICE "%s: Scatter Gather Enabled\n", dev->name); |
| 1521 | |
| 1522 | if (dev->features & NETIF_F_IP_CSUM) |
| 1523 | printk(KERN_NOTICE "%s: TX TCP/IP Checksumming Supported\n", |
| 1524 | dev->name); |
| 1525 | |
| 1526 | #ifdef MV643XX_CHECKSUM_OFFLOAD_TX |
| 1527 | printk(KERN_NOTICE "%s: RX TCP/UDP Checksum Offload ON \n", dev->name); |
| 1528 | #endif |
| 1529 | |
| 1530 | #ifdef MV643XX_COAL |
| 1531 | printk(KERN_NOTICE "%s: TX and RX Interrupt Coalescing ON \n", |
| 1532 | dev->name); |
| 1533 | #endif |
| 1534 | |
| 1535 | #ifdef MV643XX_NAPI |
| 1536 | printk(KERN_NOTICE "%s: RX NAPI Enabled \n", dev->name); |
| 1537 | #endif |
| 1538 | |
Nicolas DET | b152987 | 2005-10-28 17:46:30 -0700 | [diff] [blame] | 1539 | if (mp->tx_sram_size > 0) |
| 1540 | printk(KERN_NOTICE "%s: Using SRAM\n", dev->name); |
| 1541 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | return 0; |
| 1543 | |
| 1544 | out: |
| 1545 | free_netdev(dev); |
| 1546 | |
| 1547 | return err; |
| 1548 | } |
| 1549 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1550 | static int mv643xx_eth_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1552 | struct net_device *dev = platform_get_drvdata(pdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
| 1554 | unregister_netdev(dev); |
| 1555 | flush_scheduled_work(); |
| 1556 | |
| 1557 | free_netdev(dev); |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1558 | platform_set_drvdata(pdev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1562 | static int mv643xx_eth_shared_probe(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1563 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | struct resource *res; |
| 1565 | |
| 1566 | printk(KERN_NOTICE "MV-643xx 10/100/1000 Ethernet Driver\n"); |
| 1567 | |
| 1568 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1569 | if (res == NULL) |
| 1570 | return -ENODEV; |
| 1571 | |
| 1572 | mv643xx_eth_shared_base = ioremap(res->start, |
| 1573 | MV643XX_ETH_SHARED_REGS_SIZE); |
| 1574 | if (mv643xx_eth_shared_base == NULL) |
| 1575 | return -ENOMEM; |
| 1576 | |
| 1577 | return 0; |
| 1578 | |
| 1579 | } |
| 1580 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1581 | static int mv643xx_eth_shared_remove(struct platform_device *pdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1582 | { |
| 1583 | iounmap(mv643xx_eth_shared_base); |
| 1584 | mv643xx_eth_shared_base = NULL; |
| 1585 | |
| 1586 | return 0; |
| 1587 | } |
| 1588 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1589 | static struct platform_driver mv643xx_eth_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | .probe = mv643xx_eth_probe, |
| 1591 | .remove = mv643xx_eth_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1592 | .driver = { |
| 1593 | .name = MV643XX_ETH_NAME, |
| 1594 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | }; |
| 1596 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1597 | static struct platform_driver mv643xx_eth_shared_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | .probe = mv643xx_eth_shared_probe, |
| 1599 | .remove = mv643xx_eth_shared_remove, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1600 | .driver = { |
| 1601 | .name = MV643XX_ETH_SHARED_NAME, |
| 1602 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | }; |
| 1604 | |
| 1605 | /* |
| 1606 | * mv643xx_init_module |
| 1607 | * |
| 1608 | * Registers the network drivers into the Linux kernel |
| 1609 | * |
| 1610 | * Input : N/A |
| 1611 | * |
| 1612 | * Output : N/A |
| 1613 | */ |
| 1614 | static int __init mv643xx_init_module(void) |
| 1615 | { |
| 1616 | int rc; |
| 1617 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1618 | rc = platform_driver_register(&mv643xx_eth_shared_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1619 | if (!rc) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1620 | rc = platform_driver_register(&mv643xx_eth_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1621 | if (rc) |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1622 | platform_driver_unregister(&mv643xx_eth_shared_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1623 | } |
| 1624 | return rc; |
| 1625 | } |
| 1626 | |
| 1627 | /* |
| 1628 | * mv643xx_cleanup_module |
| 1629 | * |
| 1630 | * Registers the network drivers into the Linux kernel |
| 1631 | * |
| 1632 | * Input : N/A |
| 1633 | * |
| 1634 | * Output : N/A |
| 1635 | */ |
| 1636 | static void __exit mv643xx_cleanup_module(void) |
| 1637 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1638 | platform_driver_unregister(&mv643xx_eth_driver); |
| 1639 | platform_driver_unregister(&mv643xx_eth_shared_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | } |
| 1641 | |
| 1642 | module_init(mv643xx_init_module); |
| 1643 | module_exit(mv643xx_cleanup_module); |
| 1644 | |
| 1645 | MODULE_LICENSE("GPL"); |
| 1646 | MODULE_AUTHOR( "Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, Manish Lachwani" |
| 1647 | " and Dale Farnsworth"); |
| 1648 | MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX"); |
| 1649 | |
| 1650 | /* |
| 1651 | * The second part is the low level driver of the gigE ethernet ports. |
| 1652 | */ |
| 1653 | |
| 1654 | /* |
| 1655 | * Marvell's Gigabit Ethernet controller low level driver |
| 1656 | * |
| 1657 | * DESCRIPTION: |
| 1658 | * This file introduce low level API to Marvell's Gigabit Ethernet |
| 1659 | * controller. This Gigabit Ethernet Controller driver API controls |
| 1660 | * 1) Operations (i.e. port init, start, reset etc'). |
| 1661 | * 2) Data flow (i.e. port send, receive etc'). |
| 1662 | * Each Gigabit Ethernet port is controlled via |
| 1663 | * struct mv643xx_private. |
| 1664 | * This struct includes user configuration information as well as |
| 1665 | * driver internal data needed for its operations. |
| 1666 | * |
| 1667 | * Supported Features: |
| 1668 | * - This low level driver is OS independent. Allocating memory for |
| 1669 | * the descriptor rings and buffers are not within the scope of |
| 1670 | * this driver. |
| 1671 | * - The user is free from Rx/Tx queue managing. |
| 1672 | * - This low level driver introduce functionality API that enable |
| 1673 | * the to operate Marvell's Gigabit Ethernet Controller in a |
| 1674 | * convenient way. |
| 1675 | * - Simple Gigabit Ethernet port operation API. |
| 1676 | * - Simple Gigabit Ethernet port data flow API. |
| 1677 | * - Data flow and operation API support per queue functionality. |
| 1678 | * - Support cached descriptors for better performance. |
| 1679 | * - Enable access to all four DRAM banks and internal SRAM memory |
| 1680 | * spaces. |
| 1681 | * - PHY access and control API. |
| 1682 | * - Port control register configuration API. |
| 1683 | * - Full control over Unicast and Multicast MAC configurations. |
| 1684 | * |
| 1685 | * Operation flow: |
| 1686 | * |
| 1687 | * Initialization phase |
| 1688 | * This phase complete the initialization of the the |
| 1689 | * mv643xx_private struct. |
| 1690 | * User information regarding port configuration has to be set |
| 1691 | * prior to calling the port initialization routine. |
| 1692 | * |
| 1693 | * In this phase any port Tx/Rx activity is halted, MIB counters |
| 1694 | * are cleared, PHY address is set according to user parameter and |
| 1695 | * access to DRAM and internal SRAM memory spaces. |
| 1696 | * |
| 1697 | * Driver ring initialization |
| 1698 | * Allocating memory for the descriptor rings and buffers is not |
| 1699 | * within the scope of this driver. Thus, the user is required to |
| 1700 | * allocate memory for the descriptors ring and buffers. Those |
| 1701 | * memory parameters are used by the Rx and Tx ring initialization |
| 1702 | * routines in order to curve the descriptor linked list in a form |
| 1703 | * of a ring. |
| 1704 | * Note: Pay special attention to alignment issues when using |
| 1705 | * cached descriptors/buffers. In this phase the driver store |
| 1706 | * information in the mv643xx_private struct regarding each queue |
| 1707 | * ring. |
| 1708 | * |
| 1709 | * Driver start |
| 1710 | * This phase prepares the Ethernet port for Rx and Tx activity. |
| 1711 | * It uses the information stored in the mv643xx_private struct to |
| 1712 | * initialize the various port registers. |
| 1713 | * |
| 1714 | * Data flow: |
| 1715 | * All packet references to/from the driver are done using |
| 1716 | * struct pkt_info. |
| 1717 | * This struct is a unified struct used with Rx and Tx operations. |
| 1718 | * This way the user is not required to be familiar with neither |
| 1719 | * Tx nor Rx descriptors structures. |
| 1720 | * The driver's descriptors rings are management by indexes. |
| 1721 | * Those indexes controls the ring resources and used to indicate |
| 1722 | * a SW resource error: |
| 1723 | * 'current' |
| 1724 | * This index points to the current available resource for use. For |
| 1725 | * example in Rx process this index will point to the descriptor |
| 1726 | * that will be passed to the user upon calling the receive |
| 1727 | * routine. In Tx process, this index will point to the descriptor |
| 1728 | * that will be assigned with the user packet info and transmitted. |
| 1729 | * 'used' |
| 1730 | * This index points to the descriptor that need to restore its |
| 1731 | * resources. For example in Rx process, using the Rx buffer return |
| 1732 | * API will attach the buffer returned in packet info to the |
| 1733 | * descriptor pointed by 'used'. In Tx process, using the Tx |
| 1734 | * descriptor return will merely return the user packet info with |
| 1735 | * the command status of the transmitted buffer pointed by the |
| 1736 | * 'used' index. Nevertheless, it is essential to use this routine |
| 1737 | * to update the 'used' index. |
| 1738 | * 'first' |
| 1739 | * This index supports Tx Scatter-Gather. It points to the first |
| 1740 | * descriptor of a packet assembled of multiple buffers. For |
| 1741 | * example when in middle of Such packet we have a Tx resource |
| 1742 | * error the 'curr' index get the value of 'first' to indicate |
| 1743 | * that the ring returned to its state before trying to transmit |
| 1744 | * this packet. |
| 1745 | * |
| 1746 | * Receive operation: |
| 1747 | * The eth_port_receive API set the packet information struct, |
| 1748 | * passed by the caller, with received information from the |
| 1749 | * 'current' SDMA descriptor. |
| 1750 | * It is the user responsibility to return this resource back |
| 1751 | * to the Rx descriptor ring to enable the reuse of this source. |
| 1752 | * Return Rx resource is done using the eth_rx_return_buff API. |
| 1753 | * |
| 1754 | * Transmit operation: |
| 1755 | * The eth_port_send API supports Scatter-Gather which enables to |
| 1756 | * send a packet spanned over multiple buffers. This means that |
| 1757 | * for each packet info structure given by the user and put into |
| 1758 | * the Tx descriptors ring, will be transmitted only if the 'LAST' |
| 1759 | * bit will be set in the packet info command status field. This |
| 1760 | * API also consider restriction regarding buffer alignments and |
| 1761 | * sizes. |
| 1762 | * The user must return a Tx resource after ensuring the buffer |
| 1763 | * has been transmitted to enable the Tx ring indexes to update. |
| 1764 | * |
| 1765 | * BOARD LAYOUT |
| 1766 | * This device is on-board. No jumper diagram is necessary. |
| 1767 | * |
| 1768 | * EXTERNAL INTERFACE |
| 1769 | * |
| 1770 | * Prior to calling the initialization routine eth_port_init() the user |
| 1771 | * must set the following fields under mv643xx_private struct: |
| 1772 | * port_num User Ethernet port number. |
| 1773 | * port_mac_addr[6] User defined port MAC address. |
| 1774 | * port_config User port configuration value. |
| 1775 | * port_config_extend User port config extend value. |
| 1776 | * port_sdma_config User port SDMA config value. |
| 1777 | * port_serial_control User port serial control value. |
| 1778 | * |
| 1779 | * This driver data flow is done using the struct pkt_info which |
| 1780 | * is a unified struct for Rx and Tx operations: |
| 1781 | * |
| 1782 | * byte_cnt Tx/Rx descriptor buffer byte count. |
| 1783 | * l4i_chk CPU provided TCP Checksum. For Tx operation |
| 1784 | * only. |
| 1785 | * cmd_sts Tx/Rx descriptor command status. |
| 1786 | * buf_ptr Tx/Rx descriptor buffer pointer. |
| 1787 | * return_info Tx/Rx user resource return information. |
| 1788 | */ |
| 1789 | |
| 1790 | /* defines */ |
| 1791 | /* SDMA command macros */ |
| 1792 | #define ETH_ENABLE_TX_QUEUE(eth_port) \ |
| 1793 | mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(eth_port), 1) |
| 1794 | |
| 1795 | /* locals */ |
| 1796 | |
| 1797 | /* PHY routines */ |
| 1798 | static int ethernet_phy_get(unsigned int eth_port_num); |
| 1799 | static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr); |
| 1800 | |
| 1801 | /* Ethernet Port routines */ |
| 1802 | static int eth_port_uc_addr(unsigned int eth_port_num, unsigned char uc_nibble, |
| 1803 | int option); |
| 1804 | |
| 1805 | /* |
| 1806 | * eth_port_init - Initialize the Ethernet port driver |
| 1807 | * |
| 1808 | * DESCRIPTION: |
| 1809 | * This function prepares the ethernet port to start its activity: |
| 1810 | * 1) Completes the ethernet port driver struct initialization toward port |
| 1811 | * start routine. |
| 1812 | * 2) Resets the device to a quiescent state in case of warm reboot. |
| 1813 | * 3) Enable SDMA access to all four DRAM banks as well as internal SRAM. |
| 1814 | * 4) Clean MAC tables. The reset status of those tables is unknown. |
| 1815 | * 5) Set PHY address. |
| 1816 | * Note: Call this routine prior to eth_port_start routine and after |
| 1817 | * setting user values in the user fields of Ethernet port control |
| 1818 | * struct. |
| 1819 | * |
| 1820 | * INPUT: |
| 1821 | * struct mv643xx_private *mp Ethernet port control struct |
| 1822 | * |
| 1823 | * OUTPUT: |
| 1824 | * See description. |
| 1825 | * |
| 1826 | * RETURN: |
| 1827 | * None. |
| 1828 | */ |
| 1829 | static void eth_port_init(struct mv643xx_private *mp) |
| 1830 | { |
| 1831 | mp->port_rx_queue_command = 0; |
| 1832 | mp->port_tx_queue_command = 0; |
| 1833 | |
| 1834 | mp->rx_resource_err = 0; |
| 1835 | mp->tx_resource_err = 0; |
| 1836 | |
| 1837 | eth_port_reset(mp->port_num); |
| 1838 | |
| 1839 | eth_port_init_mac_tables(mp->port_num); |
| 1840 | |
| 1841 | ethernet_phy_reset(mp->port_num); |
| 1842 | } |
| 1843 | |
| 1844 | /* |
| 1845 | * eth_port_start - Start the Ethernet port activity. |
| 1846 | * |
| 1847 | * DESCRIPTION: |
| 1848 | * This routine prepares the Ethernet port for Rx and Tx activity: |
| 1849 | * 1. Initialize Tx and Rx Current Descriptor Pointer for each queue that |
| 1850 | * has been initialized a descriptor's ring (using |
| 1851 | * ether_init_tx_desc_ring for Tx and ether_init_rx_desc_ring for Rx) |
| 1852 | * 2. Initialize and enable the Ethernet configuration port by writing to |
| 1853 | * the port's configuration and command registers. |
| 1854 | * 3. Initialize and enable the SDMA by writing to the SDMA's |
| 1855 | * configuration and command registers. After completing these steps, |
| 1856 | * the ethernet port SDMA can starts to perform Rx and Tx activities. |
| 1857 | * |
| 1858 | * Note: Each Rx and Tx queue descriptor's list must be initialized prior |
| 1859 | * to calling this function (use ether_init_tx_desc_ring for Tx queues |
| 1860 | * and ether_init_rx_desc_ring for Rx queues). |
| 1861 | * |
| 1862 | * INPUT: |
| 1863 | * struct mv643xx_private *mp Ethernet port control struct |
| 1864 | * |
| 1865 | * OUTPUT: |
| 1866 | * Ethernet port is ready to receive and transmit. |
| 1867 | * |
| 1868 | * RETURN: |
| 1869 | * None. |
| 1870 | */ |
| 1871 | static void eth_port_start(struct mv643xx_private *mp) |
| 1872 | { |
| 1873 | unsigned int port_num = mp->port_num; |
| 1874 | int tx_curr_desc, rx_curr_desc; |
| 1875 | |
| 1876 | /* Assignment of Tx CTRP of given queue */ |
| 1877 | tx_curr_desc = mp->tx_curr_desc_q; |
| 1878 | mv_write(MV643XX_ETH_TX_CURRENT_QUEUE_DESC_PTR_0(port_num), |
| 1879 | (u32)((struct eth_tx_desc *)mp->tx_desc_dma + tx_curr_desc)); |
| 1880 | |
| 1881 | /* Assignment of Rx CRDP of given queue */ |
| 1882 | rx_curr_desc = mp->rx_curr_desc_q; |
| 1883 | mv_write(MV643XX_ETH_RX_CURRENT_QUEUE_DESC_PTR_0(port_num), |
| 1884 | (u32)((struct eth_rx_desc *)mp->rx_desc_dma + rx_curr_desc)); |
| 1885 | |
| 1886 | /* Add the assigned Ethernet address to the port's address table */ |
| 1887 | eth_port_uc_addr_set(port_num, mp->port_mac_addr); |
| 1888 | |
| 1889 | /* Assign port configuration and command. */ |
| 1890 | mv_write(MV643XX_ETH_PORT_CONFIG_REG(port_num), mp->port_config); |
| 1891 | |
| 1892 | mv_write(MV643XX_ETH_PORT_CONFIG_EXTEND_REG(port_num), |
| 1893 | mp->port_config_extend); |
| 1894 | |
| 1895 | |
| 1896 | /* Increase the Rx side buffer size if supporting GigE */ |
| 1897 | if (mp->port_serial_control & MV643XX_ETH_SET_GMII_SPEED_TO_1000) |
| 1898 | mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), |
| 1899 | (mp->port_serial_control & 0xfff1ffff) | (0x5 << 17)); |
| 1900 | else |
| 1901 | mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), |
| 1902 | mp->port_serial_control); |
| 1903 | |
| 1904 | mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), |
| 1905 | mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num)) | |
| 1906 | MV643XX_ETH_SERIAL_PORT_ENABLE); |
| 1907 | |
| 1908 | /* Assign port SDMA configuration */ |
| 1909 | mv_write(MV643XX_ETH_SDMA_CONFIG_REG(port_num), |
| 1910 | mp->port_sdma_config); |
| 1911 | |
| 1912 | /* Enable port Rx. */ |
| 1913 | mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), |
| 1914 | mp->port_rx_queue_command); |
Dale Farnsworth | 8f54371 | 2005-09-02 12:34:35 -0700 | [diff] [blame] | 1915 | |
| 1916 | /* Disable port bandwidth limits by clearing MTU register */ |
| 1917 | mv_write(MV643XX_ETH_MAXIMUM_TRANSMIT_UNIT(port_num), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | /* |
| 1921 | * eth_port_uc_addr_set - This function Set the port Unicast address. |
| 1922 | * |
| 1923 | * DESCRIPTION: |
| 1924 | * This function Set the port Ethernet MAC address. |
| 1925 | * |
| 1926 | * INPUT: |
| 1927 | * unsigned int eth_port_num Port number. |
| 1928 | * char * p_addr Address to be set |
| 1929 | * |
| 1930 | * OUTPUT: |
| 1931 | * Set MAC address low and high registers. also calls eth_port_uc_addr() |
| 1932 | * To set the unicast table with the proper information. |
| 1933 | * |
| 1934 | * RETURN: |
| 1935 | * N/A. |
| 1936 | * |
| 1937 | */ |
| 1938 | static void eth_port_uc_addr_set(unsigned int eth_port_num, |
| 1939 | unsigned char *p_addr) |
| 1940 | { |
| 1941 | unsigned int mac_h; |
| 1942 | unsigned int mac_l; |
| 1943 | |
| 1944 | mac_l = (p_addr[4] << 8) | (p_addr[5]); |
| 1945 | mac_h = (p_addr[0] << 24) | (p_addr[1] << 16) | (p_addr[2] << 8) | |
| 1946 | (p_addr[3] << 0); |
| 1947 | |
| 1948 | mv_write(MV643XX_ETH_MAC_ADDR_LOW(eth_port_num), mac_l); |
| 1949 | mv_write(MV643XX_ETH_MAC_ADDR_HIGH(eth_port_num), mac_h); |
| 1950 | |
| 1951 | /* Accept frames of this address */ |
| 1952 | eth_port_uc_addr(eth_port_num, p_addr[5], ACCEPT_MAC_ADDR); |
| 1953 | |
| 1954 | return; |
| 1955 | } |
| 1956 | |
| 1957 | /* |
| 1958 | * eth_port_uc_addr_get - This function retrieves the port Unicast address |
| 1959 | * (MAC address) from the ethernet hw registers. |
| 1960 | * |
| 1961 | * DESCRIPTION: |
| 1962 | * This function retrieves the port Ethernet MAC address. |
| 1963 | * |
| 1964 | * INPUT: |
| 1965 | * unsigned int eth_port_num Port number. |
| 1966 | * char *MacAddr pointer where the MAC address is stored |
| 1967 | * |
| 1968 | * OUTPUT: |
| 1969 | * Copy the MAC address to the location pointed to by MacAddr |
| 1970 | * |
| 1971 | * RETURN: |
| 1972 | * N/A. |
| 1973 | * |
| 1974 | */ |
| 1975 | static void eth_port_uc_addr_get(struct net_device *dev, unsigned char *p_addr) |
| 1976 | { |
| 1977 | struct mv643xx_private *mp = netdev_priv(dev); |
| 1978 | unsigned int mac_h; |
| 1979 | unsigned int mac_l; |
| 1980 | |
| 1981 | mac_h = mv_read(MV643XX_ETH_MAC_ADDR_HIGH(mp->port_num)); |
| 1982 | mac_l = mv_read(MV643XX_ETH_MAC_ADDR_LOW(mp->port_num)); |
| 1983 | |
| 1984 | p_addr[0] = (mac_h >> 24) & 0xff; |
| 1985 | p_addr[1] = (mac_h >> 16) & 0xff; |
| 1986 | p_addr[2] = (mac_h >> 8) & 0xff; |
| 1987 | p_addr[3] = mac_h & 0xff; |
| 1988 | p_addr[4] = (mac_l >> 8) & 0xff; |
| 1989 | p_addr[5] = mac_l & 0xff; |
| 1990 | } |
| 1991 | |
| 1992 | /* |
| 1993 | * eth_port_uc_addr - This function Set the port unicast address table |
| 1994 | * |
| 1995 | * DESCRIPTION: |
| 1996 | * This function locates the proper entry in the Unicast table for the |
| 1997 | * specified MAC nibble and sets its properties according to function |
| 1998 | * parameters. |
| 1999 | * |
| 2000 | * INPUT: |
| 2001 | * unsigned int eth_port_num Port number. |
| 2002 | * unsigned char uc_nibble Unicast MAC Address last nibble. |
| 2003 | * int option 0 = Add, 1 = remove address. |
| 2004 | * |
| 2005 | * OUTPUT: |
| 2006 | * This function add/removes MAC addresses from the port unicast address |
| 2007 | * table. |
| 2008 | * |
| 2009 | * RETURN: |
| 2010 | * true is output succeeded. |
| 2011 | * false if option parameter is invalid. |
| 2012 | * |
| 2013 | */ |
| 2014 | static int eth_port_uc_addr(unsigned int eth_port_num, unsigned char uc_nibble, |
| 2015 | int option) |
| 2016 | { |
| 2017 | unsigned int unicast_reg; |
| 2018 | unsigned int tbl_offset; |
| 2019 | unsigned int reg_offset; |
| 2020 | |
| 2021 | /* Locate the Unicast table entry */ |
| 2022 | uc_nibble = (0xf & uc_nibble); |
| 2023 | tbl_offset = (uc_nibble / 4) * 4; /* Register offset from unicast table base */ |
| 2024 | reg_offset = uc_nibble % 4; /* Entry offset within the above register */ |
| 2025 | |
| 2026 | switch (option) { |
| 2027 | case REJECT_MAC_ADDR: |
| 2028 | /* Clear accepts frame bit at given unicast DA table entry */ |
| 2029 | unicast_reg = mv_read((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE |
| 2030 | (eth_port_num) + tbl_offset)); |
| 2031 | |
| 2032 | unicast_reg &= (0x0E << (8 * reg_offset)); |
| 2033 | |
| 2034 | mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE |
| 2035 | (eth_port_num) + tbl_offset), unicast_reg); |
| 2036 | break; |
| 2037 | |
| 2038 | case ACCEPT_MAC_ADDR: |
| 2039 | /* Set accepts frame bit at unicast DA filter table entry */ |
| 2040 | unicast_reg = |
| 2041 | mv_read((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE |
| 2042 | (eth_port_num) + tbl_offset)); |
| 2043 | |
| 2044 | unicast_reg |= (0x01 << (8 * reg_offset)); |
| 2045 | |
| 2046 | mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE |
| 2047 | (eth_port_num) + tbl_offset), unicast_reg); |
| 2048 | |
| 2049 | break; |
| 2050 | |
| 2051 | default: |
| 2052 | return 0; |
| 2053 | } |
| 2054 | |
| 2055 | return 1; |
| 2056 | } |
| 2057 | |
| 2058 | /* |
| 2059 | * eth_port_init_mac_tables - Clear all entrance in the UC, SMC and OMC tables |
| 2060 | * |
| 2061 | * DESCRIPTION: |
| 2062 | * Go through all the DA filter tables (Unicast, Special Multicast & |
| 2063 | * Other Multicast) and set each entry to 0. |
| 2064 | * |
| 2065 | * INPUT: |
| 2066 | * unsigned int eth_port_num Ethernet Port number. |
| 2067 | * |
| 2068 | * OUTPUT: |
| 2069 | * Multicast and Unicast packets are rejected. |
| 2070 | * |
| 2071 | * RETURN: |
| 2072 | * None. |
| 2073 | */ |
| 2074 | static void eth_port_init_mac_tables(unsigned int eth_port_num) |
| 2075 | { |
| 2076 | int table_index; |
| 2077 | |
| 2078 | /* Clear DA filter unicast table (Ex_dFUT) */ |
| 2079 | for (table_index = 0; table_index <= 0xC; table_index += 4) |
| 2080 | mv_write((MV643XX_ETH_DA_FILTER_UNICAST_TABLE_BASE |
| 2081 | (eth_port_num) + table_index), 0); |
| 2082 | |
| 2083 | for (table_index = 0; table_index <= 0xFC; table_index += 4) { |
| 2084 | /* Clear DA filter special multicast table (Ex_dFSMT) */ |
| 2085 | mv_write((MV643XX_ETH_DA_FILTER_SPECIAL_MULTICAST_TABLE_BASE |
| 2086 | (eth_port_num) + table_index), 0); |
| 2087 | /* Clear DA filter other multicast table (Ex_dFOMT) */ |
| 2088 | mv_write((MV643XX_ETH_DA_FILTER_OTHER_MULTICAST_TABLE_BASE |
| 2089 | (eth_port_num) + table_index), 0); |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | /* |
| 2094 | * eth_clear_mib_counters - Clear all MIB counters |
| 2095 | * |
| 2096 | * DESCRIPTION: |
| 2097 | * This function clears all MIB counters of a specific ethernet port. |
| 2098 | * A read from the MIB counter will reset the counter. |
| 2099 | * |
| 2100 | * INPUT: |
| 2101 | * unsigned int eth_port_num Ethernet Port number. |
| 2102 | * |
| 2103 | * OUTPUT: |
| 2104 | * After reading all MIB counters, the counters resets. |
| 2105 | * |
| 2106 | * RETURN: |
| 2107 | * MIB counter value. |
| 2108 | * |
| 2109 | */ |
| 2110 | static void eth_clear_mib_counters(unsigned int eth_port_num) |
| 2111 | { |
| 2112 | int i; |
| 2113 | |
| 2114 | /* Perform dummy reads from MIB counters */ |
| 2115 | for (i = ETH_MIB_GOOD_OCTETS_RECEIVED_LOW; i < ETH_MIB_LATE_COLLISION; |
| 2116 | i += 4) |
| 2117 | mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(eth_port_num) + i); |
| 2118 | } |
| 2119 | |
| 2120 | static inline u32 read_mib(struct mv643xx_private *mp, int offset) |
| 2121 | { |
| 2122 | return mv_read(MV643XX_ETH_MIB_COUNTERS_BASE(mp->port_num) + offset); |
| 2123 | } |
| 2124 | |
| 2125 | static void eth_update_mib_counters(struct mv643xx_private *mp) |
| 2126 | { |
| 2127 | struct mv643xx_mib_counters *p = &mp->mib_counters; |
| 2128 | int offset; |
| 2129 | |
| 2130 | p->good_octets_received += |
| 2131 | read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_LOW); |
| 2132 | p->good_octets_received += |
| 2133 | (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_RECEIVED_HIGH) << 32; |
| 2134 | |
| 2135 | for (offset = ETH_MIB_BAD_OCTETS_RECEIVED; |
| 2136 | offset <= ETH_MIB_FRAMES_1024_TO_MAX_OCTETS; |
| 2137 | offset += 4) |
| 2138 | *(u32 *)((char *)p + offset) = read_mib(mp, offset); |
| 2139 | |
| 2140 | p->good_octets_sent += read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_LOW); |
| 2141 | p->good_octets_sent += |
| 2142 | (u64)read_mib(mp, ETH_MIB_GOOD_OCTETS_SENT_HIGH) << 32; |
| 2143 | |
| 2144 | for (offset = ETH_MIB_GOOD_FRAMES_SENT; |
| 2145 | offset <= ETH_MIB_LATE_COLLISION; |
| 2146 | offset += 4) |
| 2147 | *(u32 *)((char *)p + offset) = read_mib(mp, offset); |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * ethernet_phy_detect - Detect whether a phy is present |
| 2152 | * |
| 2153 | * DESCRIPTION: |
| 2154 | * This function tests whether there is a PHY present on |
| 2155 | * the specified port. |
| 2156 | * |
| 2157 | * INPUT: |
| 2158 | * unsigned int eth_port_num Ethernet Port number. |
| 2159 | * |
| 2160 | * OUTPUT: |
| 2161 | * None |
| 2162 | * |
| 2163 | * RETURN: |
| 2164 | * 0 on success |
| 2165 | * -ENODEV on failure |
| 2166 | * |
| 2167 | */ |
| 2168 | static int ethernet_phy_detect(unsigned int port_num) |
| 2169 | { |
| 2170 | unsigned int phy_reg_data0; |
| 2171 | int auto_neg; |
| 2172 | |
| 2173 | eth_port_read_smi_reg(port_num, 0, &phy_reg_data0); |
| 2174 | auto_neg = phy_reg_data0 & 0x1000; |
| 2175 | phy_reg_data0 ^= 0x1000; /* invert auto_neg */ |
| 2176 | eth_port_write_smi_reg(port_num, 0, phy_reg_data0); |
| 2177 | |
| 2178 | eth_port_read_smi_reg(port_num, 0, &phy_reg_data0); |
| 2179 | if ((phy_reg_data0 & 0x1000) == auto_neg) |
| 2180 | return -ENODEV; /* change didn't take */ |
| 2181 | |
| 2182 | phy_reg_data0 ^= 0x1000; |
| 2183 | eth_port_write_smi_reg(port_num, 0, phy_reg_data0); |
| 2184 | return 0; |
| 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * ethernet_phy_get - Get the ethernet port PHY address. |
| 2189 | * |
| 2190 | * DESCRIPTION: |
| 2191 | * This routine returns the given ethernet port PHY address. |
| 2192 | * |
| 2193 | * INPUT: |
| 2194 | * unsigned int eth_port_num Ethernet Port number. |
| 2195 | * |
| 2196 | * OUTPUT: |
| 2197 | * None. |
| 2198 | * |
| 2199 | * RETURN: |
| 2200 | * PHY address. |
| 2201 | * |
| 2202 | */ |
| 2203 | static int ethernet_phy_get(unsigned int eth_port_num) |
| 2204 | { |
| 2205 | unsigned int reg_data; |
| 2206 | |
| 2207 | reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG); |
| 2208 | |
| 2209 | return ((reg_data >> (5 * eth_port_num)) & 0x1f); |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * ethernet_phy_set - Set the ethernet port PHY address. |
| 2214 | * |
| 2215 | * DESCRIPTION: |
| 2216 | * This routine sets the given ethernet port PHY address. |
| 2217 | * |
| 2218 | * INPUT: |
| 2219 | * unsigned int eth_port_num Ethernet Port number. |
| 2220 | * int phy_addr PHY address. |
| 2221 | * |
| 2222 | * OUTPUT: |
| 2223 | * None. |
| 2224 | * |
| 2225 | * RETURN: |
| 2226 | * None. |
| 2227 | * |
| 2228 | */ |
| 2229 | static void ethernet_phy_set(unsigned int eth_port_num, int phy_addr) |
| 2230 | { |
| 2231 | u32 reg_data; |
| 2232 | int addr_shift = 5 * eth_port_num; |
| 2233 | |
| 2234 | reg_data = mv_read(MV643XX_ETH_PHY_ADDR_REG); |
| 2235 | reg_data &= ~(0x1f << addr_shift); |
| 2236 | reg_data |= (phy_addr & 0x1f) << addr_shift; |
| 2237 | mv_write(MV643XX_ETH_PHY_ADDR_REG, reg_data); |
| 2238 | } |
| 2239 | |
| 2240 | /* |
| 2241 | * ethernet_phy_reset - Reset Ethernet port PHY. |
| 2242 | * |
| 2243 | * DESCRIPTION: |
| 2244 | * This routine utilizes the SMI interface to reset the ethernet port PHY. |
| 2245 | * |
| 2246 | * INPUT: |
| 2247 | * unsigned int eth_port_num Ethernet Port number. |
| 2248 | * |
| 2249 | * OUTPUT: |
| 2250 | * The PHY is reset. |
| 2251 | * |
| 2252 | * RETURN: |
| 2253 | * None. |
| 2254 | * |
| 2255 | */ |
| 2256 | static void ethernet_phy_reset(unsigned int eth_port_num) |
| 2257 | { |
| 2258 | unsigned int phy_reg_data; |
| 2259 | |
| 2260 | /* Reset the PHY */ |
| 2261 | eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data); |
| 2262 | phy_reg_data |= 0x8000; /* Set bit 15 to reset the PHY */ |
| 2263 | eth_port_write_smi_reg(eth_port_num, 0, phy_reg_data); |
| 2264 | } |
| 2265 | |
| 2266 | /* |
| 2267 | * eth_port_reset - Reset Ethernet port |
| 2268 | * |
| 2269 | * DESCRIPTION: |
| 2270 | * This routine resets the chip by aborting any SDMA engine activity and |
| 2271 | * clearing the MIB counters. The Receiver and the Transmit unit are in |
| 2272 | * idle state after this command is performed and the port is disabled. |
| 2273 | * |
| 2274 | * INPUT: |
| 2275 | * unsigned int eth_port_num Ethernet Port number. |
| 2276 | * |
| 2277 | * OUTPUT: |
| 2278 | * Channel activity is halted. |
| 2279 | * |
| 2280 | * RETURN: |
| 2281 | * None. |
| 2282 | * |
| 2283 | */ |
| 2284 | static void eth_port_reset(unsigned int port_num) |
| 2285 | { |
| 2286 | unsigned int reg_data; |
| 2287 | |
| 2288 | /* Stop Tx port activity. Check port Tx activity. */ |
| 2289 | reg_data = mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num)); |
| 2290 | |
| 2291 | if (reg_data & 0xFF) { |
| 2292 | /* Issue stop command for active channels only */ |
| 2293 | mv_write(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num), |
| 2294 | (reg_data << 8)); |
| 2295 | |
| 2296 | /* Wait for all Tx activity to terminate. */ |
| 2297 | /* Check port cause register that all Tx queues are stopped */ |
| 2298 | while (mv_read(MV643XX_ETH_TRANSMIT_QUEUE_COMMAND_REG(port_num)) |
| 2299 | & 0xFF) |
| 2300 | udelay(10); |
| 2301 | } |
| 2302 | |
| 2303 | /* Stop Rx port activity. Check port Rx activity. */ |
| 2304 | reg_data = mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num)); |
| 2305 | |
| 2306 | if (reg_data & 0xFF) { |
| 2307 | /* Issue stop command for active channels only */ |
| 2308 | mv_write(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num), |
| 2309 | (reg_data << 8)); |
| 2310 | |
| 2311 | /* Wait for all Rx activity to terminate. */ |
| 2312 | /* Check port cause register that all Rx queues are stopped */ |
| 2313 | while (mv_read(MV643XX_ETH_RECEIVE_QUEUE_COMMAND_REG(port_num)) |
| 2314 | & 0xFF) |
| 2315 | udelay(10); |
| 2316 | } |
| 2317 | |
| 2318 | /* Clear all MIB counters */ |
| 2319 | eth_clear_mib_counters(port_num); |
| 2320 | |
| 2321 | /* Reset the Enable bit in the Configuration Register */ |
| 2322 | reg_data = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num)); |
| 2323 | reg_data &= ~MV643XX_ETH_SERIAL_PORT_ENABLE; |
| 2324 | mv_write(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num), reg_data); |
| 2325 | } |
| 2326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
| 2328 | static int eth_port_autoneg_supported(unsigned int eth_port_num) |
| 2329 | { |
| 2330 | unsigned int phy_reg_data0; |
| 2331 | |
| 2332 | eth_port_read_smi_reg(eth_port_num, 0, &phy_reg_data0); |
| 2333 | |
| 2334 | return phy_reg_data0 & 0x1000; |
| 2335 | } |
| 2336 | |
| 2337 | static int eth_port_link_is_up(unsigned int eth_port_num) |
| 2338 | { |
| 2339 | unsigned int phy_reg_data1; |
| 2340 | |
| 2341 | eth_port_read_smi_reg(eth_port_num, 1, &phy_reg_data1); |
| 2342 | |
| 2343 | if (eth_port_autoneg_supported(eth_port_num)) { |
| 2344 | if (phy_reg_data1 & 0x20) /* auto-neg complete */ |
| 2345 | return 1; |
| 2346 | } else if (phy_reg_data1 & 0x4) /* link up */ |
| 2347 | return 1; |
| 2348 | |
| 2349 | return 0; |
| 2350 | } |
| 2351 | |
| 2352 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | * eth_port_read_smi_reg - Read PHY registers |
| 2354 | * |
| 2355 | * DESCRIPTION: |
| 2356 | * This routine utilize the SMI interface to interact with the PHY in |
| 2357 | * order to perform PHY register read. |
| 2358 | * |
| 2359 | * INPUT: |
| 2360 | * unsigned int port_num Ethernet Port number. |
| 2361 | * unsigned int phy_reg PHY register address offset. |
| 2362 | * unsigned int *value Register value buffer. |
| 2363 | * |
| 2364 | * OUTPUT: |
| 2365 | * Write the value of a specified PHY register into given buffer. |
| 2366 | * |
| 2367 | * RETURN: |
| 2368 | * false if the PHY is busy or read data is not in valid state. |
| 2369 | * true otherwise. |
| 2370 | * |
| 2371 | */ |
| 2372 | static void eth_port_read_smi_reg(unsigned int port_num, |
| 2373 | unsigned int phy_reg, unsigned int *value) |
| 2374 | { |
| 2375 | int phy_addr = ethernet_phy_get(port_num); |
| 2376 | unsigned long flags; |
| 2377 | int i; |
| 2378 | |
| 2379 | /* the SMI register is a shared resource */ |
| 2380 | spin_lock_irqsave(&mv643xx_eth_phy_lock, flags); |
| 2381 | |
| 2382 | /* wait for the SMI register to become available */ |
| 2383 | for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) { |
| 2384 | if (i == PHY_WAIT_ITERATIONS) { |
| 2385 | printk("mv643xx PHY busy timeout, port %d\n", port_num); |
| 2386 | goto out; |
| 2387 | } |
| 2388 | udelay(PHY_WAIT_MICRO_SECONDS); |
| 2389 | } |
| 2390 | |
| 2391 | mv_write(MV643XX_ETH_SMI_REG, |
| 2392 | (phy_addr << 16) | (phy_reg << 21) | ETH_SMI_OPCODE_READ); |
| 2393 | |
| 2394 | /* now wait for the data to be valid */ |
| 2395 | for (i = 0; !(mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_READ_VALID); i++) { |
| 2396 | if (i == PHY_WAIT_ITERATIONS) { |
| 2397 | printk("mv643xx PHY read timeout, port %d\n", port_num); |
| 2398 | goto out; |
| 2399 | } |
| 2400 | udelay(PHY_WAIT_MICRO_SECONDS); |
| 2401 | } |
| 2402 | |
| 2403 | *value = mv_read(MV643XX_ETH_SMI_REG) & 0xffff; |
| 2404 | out: |
| 2405 | spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags); |
| 2406 | } |
| 2407 | |
| 2408 | /* |
| 2409 | * eth_port_write_smi_reg - Write to PHY registers |
| 2410 | * |
| 2411 | * DESCRIPTION: |
| 2412 | * This routine utilize the SMI interface to interact with the PHY in |
| 2413 | * order to perform writes to PHY registers. |
| 2414 | * |
| 2415 | * INPUT: |
| 2416 | * unsigned int eth_port_num Ethernet Port number. |
| 2417 | * unsigned int phy_reg PHY register address offset. |
| 2418 | * unsigned int value Register value. |
| 2419 | * |
| 2420 | * OUTPUT: |
| 2421 | * Write the given value to the specified PHY register. |
| 2422 | * |
| 2423 | * RETURN: |
| 2424 | * false if the PHY is busy. |
| 2425 | * true otherwise. |
| 2426 | * |
| 2427 | */ |
| 2428 | static void eth_port_write_smi_reg(unsigned int eth_port_num, |
| 2429 | unsigned int phy_reg, unsigned int value) |
| 2430 | { |
| 2431 | int phy_addr; |
| 2432 | int i; |
| 2433 | unsigned long flags; |
| 2434 | |
| 2435 | phy_addr = ethernet_phy_get(eth_port_num); |
| 2436 | |
| 2437 | /* the SMI register is a shared resource */ |
| 2438 | spin_lock_irqsave(&mv643xx_eth_phy_lock, flags); |
| 2439 | |
| 2440 | /* wait for the SMI register to become available */ |
| 2441 | for (i = 0; mv_read(MV643XX_ETH_SMI_REG) & ETH_SMI_BUSY; i++) { |
| 2442 | if (i == PHY_WAIT_ITERATIONS) { |
| 2443 | printk("mv643xx PHY busy timeout, port %d\n", |
| 2444 | eth_port_num); |
| 2445 | goto out; |
| 2446 | } |
| 2447 | udelay(PHY_WAIT_MICRO_SECONDS); |
| 2448 | } |
| 2449 | |
| 2450 | mv_write(MV643XX_ETH_SMI_REG, (phy_addr << 16) | (phy_reg << 21) | |
| 2451 | ETH_SMI_OPCODE_WRITE | (value & 0xffff)); |
| 2452 | out: |
| 2453 | spin_unlock_irqrestore(&mv643xx_eth_phy_lock, flags); |
| 2454 | } |
| 2455 | |
| 2456 | /* |
| 2457 | * eth_port_send - Send an Ethernet packet |
| 2458 | * |
| 2459 | * DESCRIPTION: |
| 2460 | * This routine send a given packet described by p_pktinfo parameter. It |
| 2461 | * supports transmitting of a packet spaned over multiple buffers. The |
| 2462 | * routine updates 'curr' and 'first' indexes according to the packet |
| 2463 | * segment passed to the routine. In case the packet segment is first, |
| 2464 | * the 'first' index is update. In any case, the 'curr' index is updated. |
| 2465 | * If the routine get into Tx resource error it assigns 'curr' index as |
| 2466 | * 'first'. This way the function can abort Tx process of multiple |
| 2467 | * descriptors per packet. |
| 2468 | * |
| 2469 | * INPUT: |
| 2470 | * struct mv643xx_private *mp Ethernet Port Control srtuct. |
| 2471 | * struct pkt_info *p_pkt_info User packet buffer. |
| 2472 | * |
| 2473 | * OUTPUT: |
| 2474 | * Tx ring 'curr' and 'first' indexes are updated. |
| 2475 | * |
| 2476 | * RETURN: |
| 2477 | * ETH_QUEUE_FULL in case of Tx resource error. |
| 2478 | * ETH_ERROR in case the routine can not access Tx desc ring. |
| 2479 | * ETH_QUEUE_LAST_RESOURCE if the routine uses the last Tx resource. |
| 2480 | * ETH_OK otherwise. |
| 2481 | * |
| 2482 | */ |
| 2483 | #ifdef MV643XX_CHECKSUM_OFFLOAD_TX |
| 2484 | /* |
| 2485 | * Modified to include the first descriptor pointer in case of SG |
| 2486 | */ |
| 2487 | static ETH_FUNC_RET_STATUS eth_port_send(struct mv643xx_private *mp, |
| 2488 | struct pkt_info *p_pkt_info) |
| 2489 | { |
| 2490 | int tx_desc_curr, tx_desc_used, tx_first_desc, tx_next_desc; |
| 2491 | struct eth_tx_desc *current_descriptor; |
| 2492 | struct eth_tx_desc *first_descriptor; |
| 2493 | u32 command; |
| 2494 | |
| 2495 | /* Do not process Tx ring in case of Tx ring resource error */ |
| 2496 | if (mp->tx_resource_err) |
| 2497 | return ETH_QUEUE_FULL; |
| 2498 | |
| 2499 | /* |
| 2500 | * The hardware requires that each buffer that is <= 8 bytes |
| 2501 | * in length must be aligned on an 8 byte boundary. |
| 2502 | */ |
| 2503 | if (p_pkt_info->byte_cnt <= 8 && p_pkt_info->buf_ptr & 0x7) { |
| 2504 | printk(KERN_ERR |
| 2505 | "mv643xx_eth port %d: packet size <= 8 problem\n", |
| 2506 | mp->port_num); |
| 2507 | return ETH_ERROR; |
| 2508 | } |
| 2509 | |
Dale Farnsworth | b111ceb | 2005-09-02 10:25:24 -0700 | [diff] [blame] | 2510 | mp->tx_ring_skbs++; |
| 2511 | BUG_ON(mp->tx_ring_skbs > mp->tx_ring_size); |
| 2512 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | /* Get the Tx Desc ring indexes */ |
| 2514 | tx_desc_curr = mp->tx_curr_desc_q; |
| 2515 | tx_desc_used = mp->tx_used_desc_q; |
| 2516 | |
| 2517 | current_descriptor = &mp->p_tx_desc_area[tx_desc_curr]; |
| 2518 | |
| 2519 | tx_next_desc = (tx_desc_curr + 1) % mp->tx_ring_size; |
| 2520 | |
| 2521 | current_descriptor->buf_ptr = p_pkt_info->buf_ptr; |
| 2522 | current_descriptor->byte_cnt = p_pkt_info->byte_cnt; |
| 2523 | current_descriptor->l4i_chk = p_pkt_info->l4i_chk; |
| 2524 | mp->tx_skb[tx_desc_curr] = p_pkt_info->return_info; |
| 2525 | |
| 2526 | command = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC | |
| 2527 | ETH_BUFFER_OWNED_BY_DMA; |
| 2528 | if (command & ETH_TX_FIRST_DESC) { |
| 2529 | tx_first_desc = tx_desc_curr; |
| 2530 | mp->tx_first_desc_q = tx_first_desc; |
| 2531 | first_descriptor = current_descriptor; |
| 2532 | mp->tx_first_command = command; |
| 2533 | } else { |
| 2534 | tx_first_desc = mp->tx_first_desc_q; |
| 2535 | first_descriptor = &mp->p_tx_desc_area[tx_first_desc]; |
| 2536 | BUG_ON(first_descriptor == NULL); |
| 2537 | current_descriptor->cmd_sts = command; |
| 2538 | } |
| 2539 | |
| 2540 | if (command & ETH_TX_LAST_DESC) { |
| 2541 | wmb(); |
| 2542 | first_descriptor->cmd_sts = mp->tx_first_command; |
| 2543 | |
| 2544 | wmb(); |
| 2545 | ETH_ENABLE_TX_QUEUE(mp->port_num); |
| 2546 | |
| 2547 | /* |
| 2548 | * Finish Tx packet. Update first desc in case of Tx resource |
| 2549 | * error */ |
| 2550 | tx_first_desc = tx_next_desc; |
| 2551 | mp->tx_first_desc_q = tx_first_desc; |
| 2552 | } |
| 2553 | |
| 2554 | /* Check for ring index overlap in the Tx desc ring */ |
| 2555 | if (tx_next_desc == tx_desc_used) { |
| 2556 | mp->tx_resource_err = 1; |
| 2557 | mp->tx_curr_desc_q = tx_first_desc; |
| 2558 | |
| 2559 | return ETH_QUEUE_LAST_RESOURCE; |
| 2560 | } |
| 2561 | |
| 2562 | mp->tx_curr_desc_q = tx_next_desc; |
| 2563 | |
| 2564 | return ETH_OK; |
| 2565 | } |
| 2566 | #else |
| 2567 | static ETH_FUNC_RET_STATUS eth_port_send(struct mv643xx_private *mp, |
| 2568 | struct pkt_info *p_pkt_info) |
| 2569 | { |
| 2570 | int tx_desc_curr; |
| 2571 | int tx_desc_used; |
| 2572 | struct eth_tx_desc *current_descriptor; |
| 2573 | unsigned int command_status; |
| 2574 | |
| 2575 | /* Do not process Tx ring in case of Tx ring resource error */ |
| 2576 | if (mp->tx_resource_err) |
| 2577 | return ETH_QUEUE_FULL; |
| 2578 | |
Dale Farnsworth | b111ceb | 2005-09-02 10:25:24 -0700 | [diff] [blame] | 2579 | mp->tx_ring_skbs++; |
| 2580 | BUG_ON(mp->tx_ring_skbs > mp->tx_ring_size); |
| 2581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | /* Get the Tx Desc ring indexes */ |
| 2583 | tx_desc_curr = mp->tx_curr_desc_q; |
| 2584 | tx_desc_used = mp->tx_used_desc_q; |
| 2585 | current_descriptor = &mp->p_tx_desc_area[tx_desc_curr]; |
| 2586 | |
| 2587 | command_status = p_pkt_info->cmd_sts | ETH_ZERO_PADDING | ETH_GEN_CRC; |
| 2588 | current_descriptor->buf_ptr = p_pkt_info->buf_ptr; |
| 2589 | current_descriptor->byte_cnt = p_pkt_info->byte_cnt; |
| 2590 | mp->tx_skb[tx_desc_curr] = p_pkt_info->return_info; |
| 2591 | |
| 2592 | /* Set last desc with DMA ownership and interrupt enable. */ |
| 2593 | wmb(); |
| 2594 | current_descriptor->cmd_sts = command_status | |
| 2595 | ETH_BUFFER_OWNED_BY_DMA | ETH_TX_ENABLE_INTERRUPT; |
| 2596 | |
| 2597 | wmb(); |
| 2598 | ETH_ENABLE_TX_QUEUE(mp->port_num); |
| 2599 | |
| 2600 | /* Finish Tx packet. Update first desc in case of Tx resource error */ |
| 2601 | tx_desc_curr = (tx_desc_curr + 1) % mp->tx_ring_size; |
| 2602 | |
| 2603 | /* Update the current descriptor */ |
| 2604 | mp->tx_curr_desc_q = tx_desc_curr; |
| 2605 | |
| 2606 | /* Check for ring index overlap in the Tx desc ring */ |
| 2607 | if (tx_desc_curr == tx_desc_used) { |
| 2608 | mp->tx_resource_err = 1; |
| 2609 | return ETH_QUEUE_LAST_RESOURCE; |
| 2610 | } |
| 2611 | |
| 2612 | return ETH_OK; |
| 2613 | } |
| 2614 | #endif |
| 2615 | |
| 2616 | /* |
| 2617 | * eth_tx_return_desc - Free all used Tx descriptors |
| 2618 | * |
| 2619 | * DESCRIPTION: |
| 2620 | * This routine returns the transmitted packet information to the caller. |
| 2621 | * It uses the 'first' index to support Tx desc return in case a transmit |
| 2622 | * of a packet spanned over multiple buffer still in process. |
| 2623 | * In case the Tx queue was in "resource error" condition, where there are |
| 2624 | * no available Tx resources, the function resets the resource error flag. |
| 2625 | * |
| 2626 | * INPUT: |
| 2627 | * struct mv643xx_private *mp Ethernet Port Control srtuct. |
| 2628 | * struct pkt_info *p_pkt_info User packet buffer. |
| 2629 | * |
| 2630 | * OUTPUT: |
| 2631 | * Tx ring 'first' and 'used' indexes are updated. |
| 2632 | * |
| 2633 | * RETURN: |
| 2634 | * ETH_ERROR in case the routine can not access Tx desc ring. |
| 2635 | * ETH_RETRY in case there is transmission in process. |
| 2636 | * ETH_END_OF_JOB if the routine has nothing to release. |
| 2637 | * ETH_OK otherwise. |
| 2638 | * |
| 2639 | */ |
| 2640 | static ETH_FUNC_RET_STATUS eth_tx_return_desc(struct mv643xx_private *mp, |
| 2641 | struct pkt_info *p_pkt_info) |
| 2642 | { |
| 2643 | int tx_desc_used; |
| 2644 | #ifdef MV643XX_CHECKSUM_OFFLOAD_TX |
| 2645 | int tx_busy_desc = mp->tx_first_desc_q; |
| 2646 | #else |
| 2647 | int tx_busy_desc = mp->tx_curr_desc_q; |
| 2648 | #endif |
| 2649 | struct eth_tx_desc *p_tx_desc_used; |
| 2650 | unsigned int command_status; |
| 2651 | |
| 2652 | /* Get the Tx Desc ring indexes */ |
| 2653 | tx_desc_used = mp->tx_used_desc_q; |
| 2654 | |
| 2655 | p_tx_desc_used = &mp->p_tx_desc_area[tx_desc_used]; |
| 2656 | |
| 2657 | /* Sanity check */ |
| 2658 | if (p_tx_desc_used == NULL) |
| 2659 | return ETH_ERROR; |
| 2660 | |
| 2661 | /* Stop release. About to overlap the current available Tx descriptor */ |
| 2662 | if (tx_desc_used == tx_busy_desc && !mp->tx_resource_err) |
| 2663 | return ETH_END_OF_JOB; |
| 2664 | |
| 2665 | command_status = p_tx_desc_used->cmd_sts; |
| 2666 | |
| 2667 | /* Still transmitting... */ |
| 2668 | if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) |
| 2669 | return ETH_RETRY; |
| 2670 | |
| 2671 | /* Pass the packet information to the caller */ |
| 2672 | p_pkt_info->cmd_sts = command_status; |
| 2673 | p_pkt_info->return_info = mp->tx_skb[tx_desc_used]; |
| 2674 | mp->tx_skb[tx_desc_used] = NULL; |
| 2675 | |
| 2676 | /* Update the next descriptor to release. */ |
| 2677 | mp->tx_used_desc_q = (tx_desc_used + 1) % mp->tx_ring_size; |
| 2678 | |
| 2679 | /* Any Tx return cancels the Tx resource error status */ |
| 2680 | mp->tx_resource_err = 0; |
| 2681 | |
Dale Farnsworth | b111ceb | 2005-09-02 10:25:24 -0700 | [diff] [blame] | 2682 | BUG_ON(mp->tx_ring_skbs == 0); |
| 2683 | mp->tx_ring_skbs--; |
| 2684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | return ETH_OK; |
| 2686 | } |
| 2687 | |
| 2688 | /* |
| 2689 | * eth_port_receive - Get received information from Rx ring. |
| 2690 | * |
| 2691 | * DESCRIPTION: |
| 2692 | * This routine returns the received data to the caller. There is no |
| 2693 | * data copying during routine operation. All information is returned |
| 2694 | * using pointer to packet information struct passed from the caller. |
| 2695 | * If the routine exhausts Rx ring resources then the resource error flag |
| 2696 | * is set. |
| 2697 | * |
| 2698 | * INPUT: |
| 2699 | * struct mv643xx_private *mp Ethernet Port Control srtuct. |
| 2700 | * struct pkt_info *p_pkt_info User packet buffer. |
| 2701 | * |
| 2702 | * OUTPUT: |
| 2703 | * Rx ring current and used indexes are updated. |
| 2704 | * |
| 2705 | * RETURN: |
| 2706 | * ETH_ERROR in case the routine can not access Rx desc ring. |
| 2707 | * ETH_QUEUE_FULL if Rx ring resources are exhausted. |
| 2708 | * ETH_END_OF_JOB if there is no received data. |
| 2709 | * ETH_OK otherwise. |
| 2710 | */ |
| 2711 | static ETH_FUNC_RET_STATUS eth_port_receive(struct mv643xx_private *mp, |
| 2712 | struct pkt_info *p_pkt_info) |
| 2713 | { |
| 2714 | int rx_next_curr_desc, rx_curr_desc, rx_used_desc; |
| 2715 | volatile struct eth_rx_desc *p_rx_desc; |
| 2716 | unsigned int command_status; |
| 2717 | |
| 2718 | /* Do not process Rx ring in case of Rx ring resource error */ |
| 2719 | if (mp->rx_resource_err) |
| 2720 | return ETH_QUEUE_FULL; |
| 2721 | |
| 2722 | /* Get the Rx Desc ring 'curr and 'used' indexes */ |
| 2723 | rx_curr_desc = mp->rx_curr_desc_q; |
| 2724 | rx_used_desc = mp->rx_used_desc_q; |
| 2725 | |
| 2726 | p_rx_desc = &mp->p_rx_desc_area[rx_curr_desc]; |
| 2727 | |
| 2728 | /* The following parameters are used to save readings from memory */ |
| 2729 | command_status = p_rx_desc->cmd_sts; |
| 2730 | rmb(); |
| 2731 | |
| 2732 | /* Nothing to receive... */ |
| 2733 | if (command_status & (ETH_BUFFER_OWNED_BY_DMA)) |
| 2734 | return ETH_END_OF_JOB; |
| 2735 | |
| 2736 | p_pkt_info->byte_cnt = (p_rx_desc->byte_cnt) - RX_BUF_OFFSET; |
| 2737 | p_pkt_info->cmd_sts = command_status; |
| 2738 | p_pkt_info->buf_ptr = (p_rx_desc->buf_ptr) + RX_BUF_OFFSET; |
| 2739 | p_pkt_info->return_info = mp->rx_skb[rx_curr_desc]; |
| 2740 | p_pkt_info->l4i_chk = p_rx_desc->buf_size; |
| 2741 | |
| 2742 | /* Clean the return info field to indicate that the packet has been */ |
| 2743 | /* moved to the upper layers */ |
| 2744 | mp->rx_skb[rx_curr_desc] = NULL; |
| 2745 | |
| 2746 | /* Update current index in data structure */ |
| 2747 | rx_next_curr_desc = (rx_curr_desc + 1) % mp->rx_ring_size; |
| 2748 | mp->rx_curr_desc_q = rx_next_curr_desc; |
| 2749 | |
| 2750 | /* Rx descriptors exhausted. Set the Rx ring resource error flag */ |
| 2751 | if (rx_next_curr_desc == rx_used_desc) |
| 2752 | mp->rx_resource_err = 1; |
| 2753 | |
| 2754 | return ETH_OK; |
| 2755 | } |
| 2756 | |
| 2757 | /* |
| 2758 | * eth_rx_return_buff - Returns a Rx buffer back to the Rx ring. |
| 2759 | * |
| 2760 | * DESCRIPTION: |
| 2761 | * This routine returns a Rx buffer back to the Rx ring. It retrieves the |
| 2762 | * next 'used' descriptor and attached the returned buffer to it. |
| 2763 | * In case the Rx ring was in "resource error" condition, where there are |
| 2764 | * no available Rx resources, the function resets the resource error flag. |
| 2765 | * |
| 2766 | * INPUT: |
| 2767 | * struct mv643xx_private *mp Ethernet Port Control srtuct. |
| 2768 | * struct pkt_info *p_pkt_info Information on returned buffer. |
| 2769 | * |
| 2770 | * OUTPUT: |
| 2771 | * New available Rx resource in Rx descriptor ring. |
| 2772 | * |
| 2773 | * RETURN: |
| 2774 | * ETH_ERROR in case the routine can not access Rx desc ring. |
| 2775 | * ETH_OK otherwise. |
| 2776 | */ |
| 2777 | static ETH_FUNC_RET_STATUS eth_rx_return_buff(struct mv643xx_private *mp, |
| 2778 | struct pkt_info *p_pkt_info) |
| 2779 | { |
| 2780 | int used_rx_desc; /* Where to return Rx resource */ |
| 2781 | volatile struct eth_rx_desc *p_used_rx_desc; |
| 2782 | |
| 2783 | /* Get 'used' Rx descriptor */ |
| 2784 | used_rx_desc = mp->rx_used_desc_q; |
| 2785 | p_used_rx_desc = &mp->p_rx_desc_area[used_rx_desc]; |
| 2786 | |
| 2787 | p_used_rx_desc->buf_ptr = p_pkt_info->buf_ptr; |
| 2788 | p_used_rx_desc->buf_size = p_pkt_info->byte_cnt; |
| 2789 | mp->rx_skb[used_rx_desc] = p_pkt_info->return_info; |
| 2790 | |
| 2791 | /* Flush the write pipe */ |
| 2792 | |
| 2793 | /* Return the descriptor to DMA ownership */ |
| 2794 | wmb(); |
| 2795 | p_used_rx_desc->cmd_sts = |
| 2796 | ETH_BUFFER_OWNED_BY_DMA | ETH_RX_ENABLE_INTERRUPT; |
| 2797 | wmb(); |
| 2798 | |
| 2799 | /* Move the used descriptor pointer to the next descriptor */ |
| 2800 | mp->rx_used_desc_q = (used_rx_desc + 1) % mp->rx_ring_size; |
| 2801 | |
| 2802 | /* Any Rx return cancels the Rx resource error status */ |
| 2803 | mp->rx_resource_err = 0; |
| 2804 | |
| 2805 | return ETH_OK; |
| 2806 | } |
| 2807 | |
| 2808 | /************* Begin ethtool support *************************/ |
| 2809 | |
| 2810 | struct mv643xx_stats { |
| 2811 | char stat_string[ETH_GSTRING_LEN]; |
| 2812 | int sizeof_stat; |
| 2813 | int stat_offset; |
| 2814 | }; |
| 2815 | |
| 2816 | #define MV643XX_STAT(m) sizeof(((struct mv643xx_private *)0)->m), \ |
| 2817 | offsetof(struct mv643xx_private, m) |
| 2818 | |
| 2819 | static const struct mv643xx_stats mv643xx_gstrings_stats[] = { |
| 2820 | { "rx_packets", MV643XX_STAT(stats.rx_packets) }, |
| 2821 | { "tx_packets", MV643XX_STAT(stats.tx_packets) }, |
| 2822 | { "rx_bytes", MV643XX_STAT(stats.rx_bytes) }, |
| 2823 | { "tx_bytes", MV643XX_STAT(stats.tx_bytes) }, |
| 2824 | { "rx_errors", MV643XX_STAT(stats.rx_errors) }, |
| 2825 | { "tx_errors", MV643XX_STAT(stats.tx_errors) }, |
| 2826 | { "rx_dropped", MV643XX_STAT(stats.rx_dropped) }, |
| 2827 | { "tx_dropped", MV643XX_STAT(stats.tx_dropped) }, |
| 2828 | { "good_octets_received", MV643XX_STAT(mib_counters.good_octets_received) }, |
| 2829 | { "bad_octets_received", MV643XX_STAT(mib_counters.bad_octets_received) }, |
| 2830 | { "internal_mac_transmit_err", MV643XX_STAT(mib_counters.internal_mac_transmit_err) }, |
| 2831 | { "good_frames_received", MV643XX_STAT(mib_counters.good_frames_received) }, |
| 2832 | { "bad_frames_received", MV643XX_STAT(mib_counters.bad_frames_received) }, |
| 2833 | { "broadcast_frames_received", MV643XX_STAT(mib_counters.broadcast_frames_received) }, |
| 2834 | { "multicast_frames_received", MV643XX_STAT(mib_counters.multicast_frames_received) }, |
| 2835 | { "frames_64_octets", MV643XX_STAT(mib_counters.frames_64_octets) }, |
| 2836 | { "frames_65_to_127_octets", MV643XX_STAT(mib_counters.frames_65_to_127_octets) }, |
| 2837 | { "frames_128_to_255_octets", MV643XX_STAT(mib_counters.frames_128_to_255_octets) }, |
| 2838 | { "frames_256_to_511_octets", MV643XX_STAT(mib_counters.frames_256_to_511_octets) }, |
| 2839 | { "frames_512_to_1023_octets", MV643XX_STAT(mib_counters.frames_512_to_1023_octets) }, |
| 2840 | { "frames_1024_to_max_octets", MV643XX_STAT(mib_counters.frames_1024_to_max_octets) }, |
| 2841 | { "good_octets_sent", MV643XX_STAT(mib_counters.good_octets_sent) }, |
| 2842 | { "good_frames_sent", MV643XX_STAT(mib_counters.good_frames_sent) }, |
| 2843 | { "excessive_collision", MV643XX_STAT(mib_counters.excessive_collision) }, |
| 2844 | { "multicast_frames_sent", MV643XX_STAT(mib_counters.multicast_frames_sent) }, |
| 2845 | { "broadcast_frames_sent", MV643XX_STAT(mib_counters.broadcast_frames_sent) }, |
| 2846 | { "unrec_mac_control_received", MV643XX_STAT(mib_counters.unrec_mac_control_received) }, |
| 2847 | { "fc_sent", MV643XX_STAT(mib_counters.fc_sent) }, |
| 2848 | { "good_fc_received", MV643XX_STAT(mib_counters.good_fc_received) }, |
| 2849 | { "bad_fc_received", MV643XX_STAT(mib_counters.bad_fc_received) }, |
| 2850 | { "undersize_received", MV643XX_STAT(mib_counters.undersize_received) }, |
| 2851 | { "fragments_received", MV643XX_STAT(mib_counters.fragments_received) }, |
| 2852 | { "oversize_received", MV643XX_STAT(mib_counters.oversize_received) }, |
| 2853 | { "jabber_received", MV643XX_STAT(mib_counters.jabber_received) }, |
| 2854 | { "mac_receive_error", MV643XX_STAT(mib_counters.mac_receive_error) }, |
| 2855 | { "bad_crc_event", MV643XX_STAT(mib_counters.bad_crc_event) }, |
| 2856 | { "collision", MV643XX_STAT(mib_counters.collision) }, |
| 2857 | { "late_collision", MV643XX_STAT(mib_counters.late_collision) }, |
| 2858 | }; |
| 2859 | |
| 2860 | #define MV643XX_STATS_LEN \ |
| 2861 | sizeof(mv643xx_gstrings_stats) / sizeof(struct mv643xx_stats) |
| 2862 | |
| 2863 | static int |
| 2864 | mv643xx_get_settings(struct net_device *netdev, struct ethtool_cmd *ecmd) |
| 2865 | { |
| 2866 | struct mv643xx_private *mp = netdev->priv; |
| 2867 | int port_num = mp->port_num; |
| 2868 | int autoneg = eth_port_autoneg_supported(port_num); |
| 2869 | int mode_10_bit; |
| 2870 | int auto_duplex; |
| 2871 | int half_duplex = 0; |
| 2872 | int full_duplex = 0; |
| 2873 | int auto_speed; |
| 2874 | int speed_10 = 0; |
| 2875 | int speed_100 = 0; |
| 2876 | int speed_1000 = 0; |
| 2877 | |
| 2878 | u32 pcs = mv_read(MV643XX_ETH_PORT_SERIAL_CONTROL_REG(port_num)); |
| 2879 | u32 psr = mv_read(MV643XX_ETH_PORT_STATUS_REG(port_num)); |
| 2880 | |
| 2881 | mode_10_bit = psr & MV643XX_ETH_PORT_STATUS_MODE_10_BIT; |
| 2882 | |
| 2883 | if (mode_10_bit) { |
| 2884 | ecmd->supported = SUPPORTED_10baseT_Half; |
| 2885 | } else { |
| 2886 | ecmd->supported = (SUPPORTED_10baseT_Half | |
| 2887 | SUPPORTED_10baseT_Full | |
| 2888 | SUPPORTED_100baseT_Half | |
| 2889 | SUPPORTED_100baseT_Full | |
| 2890 | SUPPORTED_1000baseT_Full | |
| 2891 | (autoneg ? SUPPORTED_Autoneg : 0) | |
| 2892 | SUPPORTED_TP); |
| 2893 | |
| 2894 | auto_duplex = !(pcs & MV643XX_ETH_DISABLE_AUTO_NEG_FOR_DUPLX); |
| 2895 | auto_speed = !(pcs & MV643XX_ETH_DISABLE_AUTO_NEG_SPEED_GMII); |
| 2896 | |
| 2897 | ecmd->advertising = ADVERTISED_TP; |
| 2898 | |
| 2899 | if (autoneg) { |
| 2900 | ecmd->advertising |= ADVERTISED_Autoneg; |
| 2901 | |
| 2902 | if (auto_duplex) { |
| 2903 | half_duplex = 1; |
| 2904 | full_duplex = 1; |
| 2905 | } else { |
| 2906 | if (pcs & MV643XX_ETH_SET_FULL_DUPLEX_MODE) |
| 2907 | full_duplex = 1; |
| 2908 | else |
| 2909 | half_duplex = 1; |
| 2910 | } |
| 2911 | |
| 2912 | if (auto_speed) { |
| 2913 | speed_10 = 1; |
| 2914 | speed_100 = 1; |
| 2915 | speed_1000 = 1; |
| 2916 | } else { |
| 2917 | if (pcs & MV643XX_ETH_SET_GMII_SPEED_TO_1000) |
| 2918 | speed_1000 = 1; |
| 2919 | else if (pcs & MV643XX_ETH_SET_MII_SPEED_TO_100) |
| 2920 | speed_100 = 1; |
| 2921 | else |
| 2922 | speed_10 = 1; |
| 2923 | } |
| 2924 | |
| 2925 | if (speed_10 & half_duplex) |
| 2926 | ecmd->advertising |= ADVERTISED_10baseT_Half; |
| 2927 | if (speed_10 & full_duplex) |
| 2928 | ecmd->advertising |= ADVERTISED_10baseT_Full; |
| 2929 | if (speed_100 & half_duplex) |
| 2930 | ecmd->advertising |= ADVERTISED_100baseT_Half; |
| 2931 | if (speed_100 & full_duplex) |
| 2932 | ecmd->advertising |= ADVERTISED_100baseT_Full; |
| 2933 | if (speed_1000) |
| 2934 | ecmd->advertising |= ADVERTISED_1000baseT_Full; |
| 2935 | } |
| 2936 | } |
| 2937 | |
| 2938 | ecmd->port = PORT_TP; |
| 2939 | ecmd->phy_address = ethernet_phy_get(port_num); |
| 2940 | |
| 2941 | ecmd->transceiver = XCVR_EXTERNAL; |
| 2942 | |
| 2943 | if (netif_carrier_ok(netdev)) { |
| 2944 | if (mode_10_bit) |
| 2945 | ecmd->speed = SPEED_10; |
| 2946 | else { |
| 2947 | if (psr & MV643XX_ETH_PORT_STATUS_GMII_1000) |
| 2948 | ecmd->speed = SPEED_1000; |
| 2949 | else if (psr & MV643XX_ETH_PORT_STATUS_MII_100) |
| 2950 | ecmd->speed = SPEED_100; |
| 2951 | else |
| 2952 | ecmd->speed = SPEED_10; |
| 2953 | } |
| 2954 | |
| 2955 | if (psr & MV643XX_ETH_PORT_STATUS_FULL_DUPLEX) |
| 2956 | ecmd->duplex = DUPLEX_FULL; |
| 2957 | else |
| 2958 | ecmd->duplex = DUPLEX_HALF; |
| 2959 | } else { |
| 2960 | ecmd->speed = -1; |
| 2961 | ecmd->duplex = -1; |
| 2962 | } |
| 2963 | |
| 2964 | ecmd->autoneg = autoneg ? AUTONEG_ENABLE : AUTONEG_DISABLE; |
| 2965 | return 0; |
| 2966 | } |
| 2967 | |
| 2968 | static void |
| 2969 | mv643xx_get_drvinfo(struct net_device *netdev, |
| 2970 | struct ethtool_drvinfo *drvinfo) |
| 2971 | { |
| 2972 | strncpy(drvinfo->driver, mv643xx_driver_name, 32); |
| 2973 | strncpy(drvinfo->version, mv643xx_driver_version, 32); |
| 2974 | strncpy(drvinfo->fw_version, "N/A", 32); |
| 2975 | strncpy(drvinfo->bus_info, "mv643xx", 32); |
| 2976 | drvinfo->n_stats = MV643XX_STATS_LEN; |
| 2977 | } |
| 2978 | |
| 2979 | static int |
| 2980 | mv643xx_get_stats_count(struct net_device *netdev) |
| 2981 | { |
| 2982 | return MV643XX_STATS_LEN; |
| 2983 | } |
| 2984 | |
| 2985 | static void |
| 2986 | mv643xx_get_ethtool_stats(struct net_device *netdev, |
| 2987 | struct ethtool_stats *stats, uint64_t *data) |
| 2988 | { |
| 2989 | struct mv643xx_private *mp = netdev->priv; |
| 2990 | int i; |
| 2991 | |
| 2992 | eth_update_mib_counters(mp); |
| 2993 | |
| 2994 | for(i = 0; i < MV643XX_STATS_LEN; i++) { |
| 2995 | char *p = (char *)mp+mv643xx_gstrings_stats[i].stat_offset; |
| 2996 | data[i] = (mv643xx_gstrings_stats[i].sizeof_stat == |
| 2997 | sizeof(uint64_t)) ? *(uint64_t *)p : *(uint32_t *)p; |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | static void |
| 3002 | mv643xx_get_strings(struct net_device *netdev, uint32_t stringset, uint8_t *data) |
| 3003 | { |
| 3004 | int i; |
| 3005 | |
| 3006 | switch(stringset) { |
| 3007 | case ETH_SS_STATS: |
| 3008 | for (i=0; i < MV643XX_STATS_LEN; i++) { |
| 3009 | memcpy(data + i * ETH_GSTRING_LEN, |
| 3010 | mv643xx_gstrings_stats[i].stat_string, |
| 3011 | ETH_GSTRING_LEN); |
| 3012 | } |
| 3013 | break; |
| 3014 | } |
| 3015 | } |
| 3016 | |
| 3017 | static struct ethtool_ops mv643xx_ethtool_ops = { |
| 3018 | .get_settings = mv643xx_get_settings, |
| 3019 | .get_drvinfo = mv643xx_get_drvinfo, |
| 3020 | .get_link = ethtool_op_get_link, |
| 3021 | .get_sg = ethtool_op_get_sg, |
| 3022 | .set_sg = ethtool_op_set_sg, |
| 3023 | .get_strings = mv643xx_get_strings, |
| 3024 | .get_stats_count = mv643xx_get_stats_count, |
| 3025 | .get_ethtool_stats = mv643xx_get_ethtool_stats, |
| 3026 | }; |
| 3027 | |
| 3028 | /************* End ethtool support *************************/ |