11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2527a6266SJeff Kirsher /* 3527a6266SJeff Kirsher * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports 4527a6266SJeff Kirsher * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com> 5527a6266SJeff Kirsher * 6527a6266SJeff Kirsher * Based on the 64360 driver from: 7527a6266SJeff Kirsher * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il> 8527a6266SJeff Kirsher * Rabeeh Khoury <rabeeh@marvell.com> 9527a6266SJeff Kirsher * 10527a6266SJeff Kirsher * Copyright (C) 2003 PMC-Sierra, Inc., 11527a6266SJeff Kirsher * written by Manish Lachwani 12527a6266SJeff Kirsher * 13527a6266SJeff Kirsher * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org> 14527a6266SJeff Kirsher * 15527a6266SJeff Kirsher * Copyright (C) 2004-2006 MontaVista Software, Inc. 16527a6266SJeff Kirsher * Dale Farnsworth <dale@farnsworth.org> 17527a6266SJeff Kirsher * 18527a6266SJeff Kirsher * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com> 19527a6266SJeff Kirsher * <sjhill@realitydiluted.com> 20527a6266SJeff Kirsher * 21527a6266SJeff Kirsher * Copyright (C) 2007-2008 Marvell Semiconductor 22527a6266SJeff Kirsher * Lennert Buytenhek <buytenh@marvell.com> 23527a6266SJeff Kirsher * 243871c387SMichael Stapelberg * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de> 25527a6266SJeff Kirsher */ 26527a6266SJeff Kirsher 27527a6266SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 28527a6266SJeff Kirsher 29527a6266SJeff Kirsher #include <linux/init.h> 30527a6266SJeff Kirsher #include <linux/dma-mapping.h> 31527a6266SJeff Kirsher #include <linux/in.h> 32527a6266SJeff Kirsher #include <linux/ip.h> 333ae8f4e0SEzequiel Garcia #include <net/tso.h> 34527a6266SJeff Kirsher #include <linux/tcp.h> 35527a6266SJeff Kirsher #include <linux/udp.h> 36527a6266SJeff Kirsher #include <linux/etherdevice.h> 37527a6266SJeff Kirsher #include <linux/delay.h> 38527a6266SJeff Kirsher #include <linux/ethtool.h> 39527a6266SJeff Kirsher #include <linux/platform_device.h> 40527a6266SJeff Kirsher #include <linux/module.h> 41527a6266SJeff Kirsher #include <linux/kernel.h> 42527a6266SJeff Kirsher #include <linux/spinlock.h> 43527a6266SJeff Kirsher #include <linux/workqueue.h> 44527a6266SJeff Kirsher #include <linux/phy.h> 45527a6266SJeff Kirsher #include <linux/mv643xx_eth.h> 46527a6266SJeff Kirsher #include <linux/io.h> 473619eb85SSebastian Hesselbarth #include <linux/interrupt.h> 48527a6266SJeff Kirsher #include <linux/types.h> 49527a6266SJeff Kirsher #include <linux/slab.h> 50452503ebSAndrew Lunn #include <linux/clk.h> 5176723bcaSSebastian Hesselbarth #include <linux/of.h> 5276723bcaSSebastian Hesselbarth #include <linux/of_irq.h> 5376723bcaSSebastian Hesselbarth #include <linux/of_net.h> 54cc9d4598SSebastian Hesselbarth #include <linux/of_mdio.h> 55527a6266SJeff Kirsher 56527a6266SJeff Kirsher static char mv643xx_eth_driver_name[] = "mv643xx_eth"; 57527a6266SJeff Kirsher static char mv643xx_eth_driver_version[] = "1.4"; 58527a6266SJeff Kirsher 59527a6266SJeff Kirsher 60527a6266SJeff Kirsher /* 61527a6266SJeff Kirsher * Registers shared between all ports. 62527a6266SJeff Kirsher */ 63527a6266SJeff Kirsher #define PHY_ADDR 0x0000 64527a6266SJeff Kirsher #define WINDOW_BASE(w) (0x0200 + ((w) << 3)) 65527a6266SJeff Kirsher #define WINDOW_SIZE(w) (0x0204 + ((w) << 3)) 66527a6266SJeff Kirsher #define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2)) 67527a6266SJeff Kirsher #define WINDOW_BAR_ENABLE 0x0290 68527a6266SJeff Kirsher #define WINDOW_PROTECT(w) (0x0294 + ((w) << 4)) 69527a6266SJeff Kirsher 70527a6266SJeff Kirsher /* 71527a6266SJeff Kirsher * Main per-port registers. These live at offset 0x0400 for 72527a6266SJeff Kirsher * port #0, 0x0800 for port #1, and 0x0c00 for port #2. 73527a6266SJeff Kirsher */ 74527a6266SJeff Kirsher #define PORT_CONFIG 0x0000 75527a6266SJeff Kirsher #define UNICAST_PROMISCUOUS_MODE 0x00000001 76527a6266SJeff Kirsher #define PORT_CONFIG_EXT 0x0004 77527a6266SJeff Kirsher #define MAC_ADDR_LOW 0x0014 78527a6266SJeff Kirsher #define MAC_ADDR_HIGH 0x0018 79527a6266SJeff Kirsher #define SDMA_CONFIG 0x001c 80527a6266SJeff Kirsher #define TX_BURST_SIZE_16_64BIT 0x01000000 81527a6266SJeff Kirsher #define TX_BURST_SIZE_4_64BIT 0x00800000 82527a6266SJeff Kirsher #define BLM_TX_NO_SWAP 0x00000020 83527a6266SJeff Kirsher #define BLM_RX_NO_SWAP 0x00000010 84527a6266SJeff Kirsher #define RX_BURST_SIZE_16_64BIT 0x00000008 85527a6266SJeff Kirsher #define RX_BURST_SIZE_4_64BIT 0x00000004 86527a6266SJeff Kirsher #define PORT_SERIAL_CONTROL 0x003c 87527a6266SJeff Kirsher #define SET_MII_SPEED_TO_100 0x01000000 88527a6266SJeff Kirsher #define SET_GMII_SPEED_TO_1000 0x00800000 89527a6266SJeff Kirsher #define SET_FULL_DUPLEX_MODE 0x00200000 90527a6266SJeff Kirsher #define MAX_RX_PACKET_9700BYTE 0x000a0000 91527a6266SJeff Kirsher #define DISABLE_AUTO_NEG_SPEED_GMII 0x00002000 92527a6266SJeff Kirsher #define DO_NOT_FORCE_LINK_FAIL 0x00000400 93527a6266SJeff Kirsher #define SERIAL_PORT_CONTROL_RESERVED 0x00000200 94527a6266SJeff Kirsher #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL 0x00000008 95527a6266SJeff Kirsher #define DISABLE_AUTO_NEG_FOR_DUPLEX 0x00000004 96527a6266SJeff Kirsher #define FORCE_LINK_PASS 0x00000002 97527a6266SJeff Kirsher #define SERIAL_PORT_ENABLE 0x00000001 98527a6266SJeff Kirsher #define PORT_STATUS 0x0044 99527a6266SJeff Kirsher #define TX_FIFO_EMPTY 0x00000400 100527a6266SJeff Kirsher #define TX_IN_PROGRESS 0x00000080 101527a6266SJeff Kirsher #define PORT_SPEED_MASK 0x00000030 102527a6266SJeff Kirsher #define PORT_SPEED_1000 0x00000010 103527a6266SJeff Kirsher #define PORT_SPEED_100 0x00000020 104527a6266SJeff Kirsher #define PORT_SPEED_10 0x00000000 105527a6266SJeff Kirsher #define FLOW_CONTROL_ENABLED 0x00000008 106527a6266SJeff Kirsher #define FULL_DUPLEX 0x00000004 107527a6266SJeff Kirsher #define LINK_UP 0x00000002 108527a6266SJeff Kirsher #define TXQ_COMMAND 0x0048 109527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF 0x004c 110cb85215fSSebastian Hesselbarth #define PORT_SERIAL_CONTROL1 0x004c 111cb85215fSSebastian Hesselbarth #define CLK125_BYPASS_EN 0x00000010 112527a6266SJeff Kirsher #define TX_BW_RATE 0x0050 113527a6266SJeff Kirsher #define TX_BW_MTU 0x0058 114527a6266SJeff Kirsher #define TX_BW_BURST 0x005c 115527a6266SJeff Kirsher #define INT_CAUSE 0x0060 116527a6266SJeff Kirsher #define INT_TX_END 0x07f80000 117527a6266SJeff Kirsher #define INT_TX_END_0 0x00080000 118527a6266SJeff Kirsher #define INT_RX 0x000003fc 119527a6266SJeff Kirsher #define INT_RX_0 0x00000004 120527a6266SJeff Kirsher #define INT_EXT 0x00000002 121527a6266SJeff Kirsher #define INT_CAUSE_EXT 0x0064 122527a6266SJeff Kirsher #define INT_EXT_LINK_PHY 0x00110000 123527a6266SJeff Kirsher #define INT_EXT_TX 0x000000ff 124527a6266SJeff Kirsher #define INT_MASK 0x0068 125527a6266SJeff Kirsher #define INT_MASK_EXT 0x006c 126527a6266SJeff Kirsher #define TX_FIFO_URGENT_THRESHOLD 0x0074 127302476c9SPaulius Zaleckas #define RX_DISCARD_FRAME_CNT 0x0084 128302476c9SPaulius Zaleckas #define RX_OVERRUN_FRAME_CNT 0x0088 129527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF_MOVED 0x00dc 130527a6266SJeff Kirsher #define TX_BW_RATE_MOVED 0x00e0 131527a6266SJeff Kirsher #define TX_BW_MTU_MOVED 0x00e8 132527a6266SJeff Kirsher #define TX_BW_BURST_MOVED 0x00ec 133527a6266SJeff Kirsher #define RXQ_CURRENT_DESC_PTR(q) (0x020c + ((q) << 4)) 134527a6266SJeff Kirsher #define RXQ_COMMAND 0x0280 135527a6266SJeff Kirsher #define TXQ_CURRENT_DESC_PTR(q) (0x02c0 + ((q) << 2)) 136527a6266SJeff Kirsher #define TXQ_BW_TOKENS(q) (0x0300 + ((q) << 4)) 137527a6266SJeff Kirsher #define TXQ_BW_CONF(q) (0x0304 + ((q) << 4)) 138527a6266SJeff Kirsher #define TXQ_BW_WRR_CONF(q) (0x0308 + ((q) << 4)) 139527a6266SJeff Kirsher 140527a6266SJeff Kirsher /* 141527a6266SJeff Kirsher * Misc per-port registers. 142527a6266SJeff Kirsher */ 143527a6266SJeff Kirsher #define MIB_COUNTERS(p) (0x1000 + ((p) << 7)) 144527a6266SJeff Kirsher #define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10)) 145527a6266SJeff Kirsher #define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10)) 146527a6266SJeff Kirsher #define UNICAST_TABLE(p) (0x1600 + ((p) << 10)) 147527a6266SJeff Kirsher 148527a6266SJeff Kirsher 149527a6266SJeff Kirsher /* 150527a6266SJeff Kirsher * SDMA configuration register default value. 151527a6266SJeff Kirsher */ 152527a6266SJeff Kirsher #if defined(__BIG_ENDIAN) 153527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE \ 154527a6266SJeff Kirsher (RX_BURST_SIZE_4_64BIT | \ 155527a6266SJeff Kirsher TX_BURST_SIZE_4_64BIT) 156527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN) 157527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE \ 158527a6266SJeff Kirsher (RX_BURST_SIZE_4_64BIT | \ 159527a6266SJeff Kirsher BLM_RX_NO_SWAP | \ 160527a6266SJeff Kirsher BLM_TX_NO_SWAP | \ 161527a6266SJeff Kirsher TX_BURST_SIZE_4_64BIT) 162527a6266SJeff Kirsher #else 163527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined 164527a6266SJeff Kirsher #endif 165527a6266SJeff Kirsher 166527a6266SJeff Kirsher 167527a6266SJeff Kirsher /* 168527a6266SJeff Kirsher * Misc definitions. 169527a6266SJeff Kirsher */ 170527a6266SJeff Kirsher #define DEFAULT_RX_QUEUE_SIZE 128 1713ae8f4e0SEzequiel Garcia #define DEFAULT_TX_QUEUE_SIZE 512 172527a6266SJeff Kirsher #define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES) 173527a6266SJeff Kirsher 174ee9e4956SEzequiel Garcia /* Max number of allowed TCP segments for software TSO */ 175ee9e4956SEzequiel Garcia #define MV643XX_MAX_TSO_SEGS 100 176ee9e4956SEzequiel Garcia #define MV643XX_MAX_SKB_DESCS (MV643XX_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) 177ee9e4956SEzequiel Garcia 178b926260cSEzequiel Garcia #define IS_TSO_HEADER(txq, addr) \ 179b926260cSEzequiel Garcia ((addr >= txq->tso_hdrs_dma) && \ 180b926260cSEzequiel Garcia (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE)) 1819e911414SEzequiel Garcia 1829e911414SEzequiel Garcia #define DESC_DMA_MAP_SINGLE 0 1839e911414SEzequiel Garcia #define DESC_DMA_MAP_PAGE 1 1849e911414SEzequiel Garcia 185527a6266SJeff Kirsher /* 186527a6266SJeff Kirsher * RX/TX descriptors. 187527a6266SJeff Kirsher */ 188527a6266SJeff Kirsher #if defined(__BIG_ENDIAN) 189527a6266SJeff Kirsher struct rx_desc { 190527a6266SJeff Kirsher u16 byte_cnt; /* Descriptor buffer byte count */ 191527a6266SJeff Kirsher u16 buf_size; /* Buffer size */ 192527a6266SJeff Kirsher u32 cmd_sts; /* Descriptor command status */ 193527a6266SJeff Kirsher u32 next_desc_ptr; /* Next descriptor pointer */ 194527a6266SJeff Kirsher u32 buf_ptr; /* Descriptor buffer pointer */ 195527a6266SJeff Kirsher }; 196527a6266SJeff Kirsher 197527a6266SJeff Kirsher struct tx_desc { 198527a6266SJeff Kirsher u16 byte_cnt; /* buffer byte count */ 199527a6266SJeff Kirsher u16 l4i_chk; /* CPU provided TCP checksum */ 200527a6266SJeff Kirsher u32 cmd_sts; /* Command/status field */ 201527a6266SJeff Kirsher u32 next_desc_ptr; /* Pointer to next descriptor */ 202527a6266SJeff Kirsher u32 buf_ptr; /* pointer to buffer for this descriptor*/ 203527a6266SJeff Kirsher }; 204527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN) 205527a6266SJeff Kirsher struct rx_desc { 206527a6266SJeff Kirsher u32 cmd_sts; /* Descriptor command status */ 207527a6266SJeff Kirsher u16 buf_size; /* Buffer size */ 208527a6266SJeff Kirsher u16 byte_cnt; /* Descriptor buffer byte count */ 209527a6266SJeff Kirsher u32 buf_ptr; /* Descriptor buffer pointer */ 210527a6266SJeff Kirsher u32 next_desc_ptr; /* Next descriptor pointer */ 211527a6266SJeff Kirsher }; 212527a6266SJeff Kirsher 213527a6266SJeff Kirsher struct tx_desc { 214527a6266SJeff Kirsher u32 cmd_sts; /* Command/status field */ 215527a6266SJeff Kirsher u16 l4i_chk; /* CPU provided TCP checksum */ 216527a6266SJeff Kirsher u16 byte_cnt; /* buffer byte count */ 217527a6266SJeff Kirsher u32 buf_ptr; /* pointer to buffer for this descriptor*/ 218527a6266SJeff Kirsher u32 next_desc_ptr; /* Pointer to next descriptor */ 219527a6266SJeff Kirsher }; 220527a6266SJeff Kirsher #else 221527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined 222527a6266SJeff Kirsher #endif 223527a6266SJeff Kirsher 224527a6266SJeff Kirsher /* RX & TX descriptor command */ 225527a6266SJeff Kirsher #define BUFFER_OWNED_BY_DMA 0x80000000 226527a6266SJeff Kirsher 227527a6266SJeff Kirsher /* RX & TX descriptor status */ 228527a6266SJeff Kirsher #define ERROR_SUMMARY 0x00000001 229527a6266SJeff Kirsher 230527a6266SJeff Kirsher /* RX descriptor status */ 231527a6266SJeff Kirsher #define LAYER_4_CHECKSUM_OK 0x40000000 232527a6266SJeff Kirsher #define RX_ENABLE_INTERRUPT 0x20000000 233527a6266SJeff Kirsher #define RX_FIRST_DESC 0x08000000 234527a6266SJeff Kirsher #define RX_LAST_DESC 0x04000000 235527a6266SJeff Kirsher #define RX_IP_HDR_OK 0x02000000 236527a6266SJeff Kirsher #define RX_PKT_IS_IPV4 0x01000000 237527a6266SJeff Kirsher #define RX_PKT_IS_ETHERNETV2 0x00800000 238527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_MASK 0x00600000 239527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_TCP_IPV4 0x00000000 240527a6266SJeff Kirsher #define RX_PKT_IS_VLAN_TAGGED 0x00080000 241527a6266SJeff Kirsher 242527a6266SJeff Kirsher /* TX descriptor command */ 243527a6266SJeff Kirsher #define TX_ENABLE_INTERRUPT 0x00800000 244527a6266SJeff Kirsher #define GEN_CRC 0x00400000 245527a6266SJeff Kirsher #define TX_FIRST_DESC 0x00200000 246527a6266SJeff Kirsher #define TX_LAST_DESC 0x00100000 247527a6266SJeff Kirsher #define ZERO_PADDING 0x00080000 248527a6266SJeff Kirsher #define GEN_IP_V4_CHECKSUM 0x00040000 249527a6266SJeff Kirsher #define GEN_TCP_UDP_CHECKSUM 0x00020000 250527a6266SJeff Kirsher #define UDP_FRAME 0x00010000 251527a6266SJeff Kirsher #define MAC_HDR_EXTRA_4_BYTES 0x00008000 25284411f73SEzequiel Garcia #define GEN_TCP_UDP_CHK_FULL 0x00000400 253527a6266SJeff Kirsher #define MAC_HDR_EXTRA_8_BYTES 0x00000200 254527a6266SJeff Kirsher 255527a6266SJeff Kirsher #define TX_IHL_SHIFT 11 256527a6266SJeff Kirsher 257527a6266SJeff Kirsher 258527a6266SJeff Kirsher /* global *******************************************************************/ 259527a6266SJeff Kirsher struct mv643xx_eth_shared_private { 260527a6266SJeff Kirsher /* 261527a6266SJeff Kirsher * Ethernet controller base address. 262527a6266SJeff Kirsher */ 263527a6266SJeff Kirsher void __iomem *base; 264527a6266SJeff Kirsher 265527a6266SJeff Kirsher /* 266527a6266SJeff Kirsher * Per-port MBUS window access register value. 267527a6266SJeff Kirsher */ 268527a6266SJeff Kirsher u32 win_protect; 269527a6266SJeff Kirsher 270527a6266SJeff Kirsher /* 271527a6266SJeff Kirsher * Hardware-specific parameters. 272527a6266SJeff Kirsher */ 273527a6266SJeff Kirsher int extended_rx_coal_limit; 274527a6266SJeff Kirsher int tx_bw_control; 275527a6266SJeff Kirsher int tx_csum_limit; 27620922486SSebastian Hesselbarth struct clk *clk; 277527a6266SJeff Kirsher }; 278527a6266SJeff Kirsher 279527a6266SJeff Kirsher #define TX_BW_CONTROL_ABSENT 0 280527a6266SJeff Kirsher #define TX_BW_CONTROL_OLD_LAYOUT 1 281527a6266SJeff Kirsher #define TX_BW_CONTROL_NEW_LAYOUT 2 282527a6266SJeff Kirsher 283527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev); 284527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev); 285527a6266SJeff Kirsher 286527a6266SJeff Kirsher 287527a6266SJeff Kirsher /* per-port *****************************************************************/ 288527a6266SJeff Kirsher struct mib_counters { 289527a6266SJeff Kirsher u64 good_octets_received; 290527a6266SJeff Kirsher u32 bad_octets_received; 291527a6266SJeff Kirsher u32 internal_mac_transmit_err; 292527a6266SJeff Kirsher u32 good_frames_received; 293527a6266SJeff Kirsher u32 bad_frames_received; 294527a6266SJeff Kirsher u32 broadcast_frames_received; 295527a6266SJeff Kirsher u32 multicast_frames_received; 296527a6266SJeff Kirsher u32 frames_64_octets; 297527a6266SJeff Kirsher u32 frames_65_to_127_octets; 298527a6266SJeff Kirsher u32 frames_128_to_255_octets; 299527a6266SJeff Kirsher u32 frames_256_to_511_octets; 300527a6266SJeff Kirsher u32 frames_512_to_1023_octets; 301527a6266SJeff Kirsher u32 frames_1024_to_max_octets; 302527a6266SJeff Kirsher u64 good_octets_sent; 303527a6266SJeff Kirsher u32 good_frames_sent; 304527a6266SJeff Kirsher u32 excessive_collision; 305527a6266SJeff Kirsher u32 multicast_frames_sent; 306527a6266SJeff Kirsher u32 broadcast_frames_sent; 307527a6266SJeff Kirsher u32 unrec_mac_control_received; 308527a6266SJeff Kirsher u32 fc_sent; 309527a6266SJeff Kirsher u32 good_fc_received; 310527a6266SJeff Kirsher u32 bad_fc_received; 311527a6266SJeff Kirsher u32 undersize_received; 312527a6266SJeff Kirsher u32 fragments_received; 313527a6266SJeff Kirsher u32 oversize_received; 314527a6266SJeff Kirsher u32 jabber_received; 315527a6266SJeff Kirsher u32 mac_receive_error; 316527a6266SJeff Kirsher u32 bad_crc_event; 317527a6266SJeff Kirsher u32 collision; 318527a6266SJeff Kirsher u32 late_collision; 319302476c9SPaulius Zaleckas /* Non MIB hardware counters */ 320302476c9SPaulius Zaleckas u32 rx_discard; 321302476c9SPaulius Zaleckas u32 rx_overrun; 322527a6266SJeff Kirsher }; 323527a6266SJeff Kirsher 324527a6266SJeff Kirsher struct rx_queue { 325527a6266SJeff Kirsher int index; 326527a6266SJeff Kirsher 327527a6266SJeff Kirsher int rx_ring_size; 328527a6266SJeff Kirsher 329527a6266SJeff Kirsher int rx_desc_count; 330527a6266SJeff Kirsher int rx_curr_desc; 331527a6266SJeff Kirsher int rx_used_desc; 332527a6266SJeff Kirsher 333527a6266SJeff Kirsher struct rx_desc *rx_desc_area; 334527a6266SJeff Kirsher dma_addr_t rx_desc_dma; 335527a6266SJeff Kirsher int rx_desc_area_size; 336527a6266SJeff Kirsher struct sk_buff **rx_skb; 337527a6266SJeff Kirsher }; 338527a6266SJeff Kirsher 339527a6266SJeff Kirsher struct tx_queue { 340527a6266SJeff Kirsher int index; 341527a6266SJeff Kirsher 342527a6266SJeff Kirsher int tx_ring_size; 343527a6266SJeff Kirsher 344527a6266SJeff Kirsher int tx_desc_count; 345527a6266SJeff Kirsher int tx_curr_desc; 346527a6266SJeff Kirsher int tx_used_desc; 347527a6266SJeff Kirsher 348ee9e4956SEzequiel Garcia int tx_stop_threshold; 349ee9e4956SEzequiel Garcia int tx_wake_threshold; 350ee9e4956SEzequiel Garcia 3513ae8f4e0SEzequiel Garcia char *tso_hdrs; 3523ae8f4e0SEzequiel Garcia dma_addr_t tso_hdrs_dma; 3533ae8f4e0SEzequiel Garcia 354527a6266SJeff Kirsher struct tx_desc *tx_desc_area; 3559e911414SEzequiel Garcia char *tx_desc_mapping; /* array to track the type of the dma mapping */ 356527a6266SJeff Kirsher dma_addr_t tx_desc_dma; 357527a6266SJeff Kirsher int tx_desc_area_size; 358527a6266SJeff Kirsher 359527a6266SJeff Kirsher struct sk_buff_head tx_skb; 360527a6266SJeff Kirsher 361527a6266SJeff Kirsher unsigned long tx_packets; 362527a6266SJeff Kirsher unsigned long tx_bytes; 363527a6266SJeff Kirsher unsigned long tx_dropped; 364527a6266SJeff Kirsher }; 365527a6266SJeff Kirsher 366527a6266SJeff Kirsher struct mv643xx_eth_private { 367527a6266SJeff Kirsher struct mv643xx_eth_shared_private *shared; 368527a6266SJeff Kirsher void __iomem *base; 369527a6266SJeff Kirsher int port_num; 370527a6266SJeff Kirsher 371527a6266SJeff Kirsher struct net_device *dev; 372527a6266SJeff Kirsher 373527a6266SJeff Kirsher struct timer_list mib_counters_timer; 374527a6266SJeff Kirsher spinlock_t mib_counters_lock; 375527a6266SJeff Kirsher struct mib_counters mib_counters; 376527a6266SJeff Kirsher 377527a6266SJeff Kirsher struct work_struct tx_timeout_task; 378527a6266SJeff Kirsher 379527a6266SJeff Kirsher struct napi_struct napi; 380527a6266SJeff Kirsher u32 int_mask; 381527a6266SJeff Kirsher u8 oom; 382527a6266SJeff Kirsher u8 work_link; 383527a6266SJeff Kirsher u8 work_tx; 384527a6266SJeff Kirsher u8 work_tx_end; 385527a6266SJeff Kirsher u8 work_rx; 386527a6266SJeff Kirsher u8 work_rx_refill; 387527a6266SJeff Kirsher 388527a6266SJeff Kirsher int skb_size; 389527a6266SJeff Kirsher 390527a6266SJeff Kirsher /* 391527a6266SJeff Kirsher * RX state. 392527a6266SJeff Kirsher */ 393527a6266SJeff Kirsher int rx_ring_size; 394527a6266SJeff Kirsher unsigned long rx_desc_sram_addr; 395527a6266SJeff Kirsher int rx_desc_sram_size; 396527a6266SJeff Kirsher int rxq_count; 397527a6266SJeff Kirsher struct timer_list rx_oom; 398527a6266SJeff Kirsher struct rx_queue rxq[8]; 399527a6266SJeff Kirsher 400527a6266SJeff Kirsher /* 401527a6266SJeff Kirsher * TX state. 402527a6266SJeff Kirsher */ 403527a6266SJeff Kirsher int tx_ring_size; 404527a6266SJeff Kirsher unsigned long tx_desc_sram_addr; 405527a6266SJeff Kirsher int tx_desc_sram_size; 406527a6266SJeff Kirsher int txq_count; 407527a6266SJeff Kirsher struct tx_queue txq[8]; 408452503ebSAndrew Lunn 409452503ebSAndrew Lunn /* 410452503ebSAndrew Lunn * Hardware-specific parameters. 411452503ebSAndrew Lunn */ 412452503ebSAndrew Lunn struct clk *clk; 413452503ebSAndrew Lunn unsigned int t_clk; 414527a6266SJeff Kirsher }; 415527a6266SJeff Kirsher 416527a6266SJeff Kirsher 417527a6266SJeff Kirsher /* port register accessors **************************************************/ 418527a6266SJeff Kirsher static inline u32 rdl(struct mv643xx_eth_private *mp, int offset) 419527a6266SJeff Kirsher { 420527a6266SJeff Kirsher return readl(mp->shared->base + offset); 421527a6266SJeff Kirsher } 422527a6266SJeff Kirsher 423527a6266SJeff Kirsher static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset) 424527a6266SJeff Kirsher { 425527a6266SJeff Kirsher return readl(mp->base + offset); 426527a6266SJeff Kirsher } 427527a6266SJeff Kirsher 428527a6266SJeff Kirsher static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data) 429527a6266SJeff Kirsher { 430527a6266SJeff Kirsher writel(data, mp->shared->base + offset); 431527a6266SJeff Kirsher } 432527a6266SJeff Kirsher 433527a6266SJeff Kirsher static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data) 434527a6266SJeff Kirsher { 435527a6266SJeff Kirsher writel(data, mp->base + offset); 436527a6266SJeff Kirsher } 437527a6266SJeff Kirsher 438527a6266SJeff Kirsher 439527a6266SJeff Kirsher /* rxq/txq helper functions *************************************************/ 440527a6266SJeff Kirsher static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq) 441527a6266SJeff Kirsher { 442527a6266SJeff Kirsher return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]); 443527a6266SJeff Kirsher } 444527a6266SJeff Kirsher 445527a6266SJeff Kirsher static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq) 446527a6266SJeff Kirsher { 447527a6266SJeff Kirsher return container_of(txq, struct mv643xx_eth_private, txq[txq->index]); 448527a6266SJeff Kirsher } 449527a6266SJeff Kirsher 450527a6266SJeff Kirsher static void rxq_enable(struct rx_queue *rxq) 451527a6266SJeff Kirsher { 452527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 453527a6266SJeff Kirsher wrlp(mp, RXQ_COMMAND, 1 << rxq->index); 454527a6266SJeff Kirsher } 455527a6266SJeff Kirsher 456527a6266SJeff Kirsher static void rxq_disable(struct rx_queue *rxq) 457527a6266SJeff Kirsher { 458527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 459527a6266SJeff Kirsher u8 mask = 1 << rxq->index; 460527a6266SJeff Kirsher 461527a6266SJeff Kirsher wrlp(mp, RXQ_COMMAND, mask << 8); 462527a6266SJeff Kirsher while (rdlp(mp, RXQ_COMMAND) & mask) 463527a6266SJeff Kirsher udelay(10); 464527a6266SJeff Kirsher } 465527a6266SJeff Kirsher 466527a6266SJeff Kirsher static void txq_reset_hw_ptr(struct tx_queue *txq) 467527a6266SJeff Kirsher { 468527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 469527a6266SJeff Kirsher u32 addr; 470527a6266SJeff Kirsher 471527a6266SJeff Kirsher addr = (u32)txq->tx_desc_dma; 472527a6266SJeff Kirsher addr += txq->tx_curr_desc * sizeof(struct tx_desc); 473527a6266SJeff Kirsher wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr); 474527a6266SJeff Kirsher } 475527a6266SJeff Kirsher 476527a6266SJeff Kirsher static void txq_enable(struct tx_queue *txq) 477527a6266SJeff Kirsher { 478527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 479527a6266SJeff Kirsher wrlp(mp, TXQ_COMMAND, 1 << txq->index); 480527a6266SJeff Kirsher } 481527a6266SJeff Kirsher 482527a6266SJeff Kirsher static void txq_disable(struct tx_queue *txq) 483527a6266SJeff Kirsher { 484527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 485527a6266SJeff Kirsher u8 mask = 1 << txq->index; 486527a6266SJeff Kirsher 487527a6266SJeff Kirsher wrlp(mp, TXQ_COMMAND, mask << 8); 488527a6266SJeff Kirsher while (rdlp(mp, TXQ_COMMAND) & mask) 489527a6266SJeff Kirsher udelay(10); 490527a6266SJeff Kirsher } 491527a6266SJeff Kirsher 492527a6266SJeff Kirsher static void txq_maybe_wake(struct tx_queue *txq) 493527a6266SJeff Kirsher { 494527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 495527a6266SJeff Kirsher struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index); 496527a6266SJeff Kirsher 497527a6266SJeff Kirsher if (netif_tx_queue_stopped(nq)) { 498527a6266SJeff Kirsher __netif_tx_lock(nq, smp_processor_id()); 499ee9e4956SEzequiel Garcia if (txq->tx_desc_count <= txq->tx_wake_threshold) 500527a6266SJeff Kirsher netif_tx_wake_queue(nq); 501527a6266SJeff Kirsher __netif_tx_unlock(nq); 502527a6266SJeff Kirsher } 503527a6266SJeff Kirsher } 504527a6266SJeff Kirsher 505527a6266SJeff Kirsher static int rxq_process(struct rx_queue *rxq, int budget) 506527a6266SJeff Kirsher { 507527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 508527a6266SJeff Kirsher struct net_device_stats *stats = &mp->dev->stats; 509527a6266SJeff Kirsher int rx; 510527a6266SJeff Kirsher 511527a6266SJeff Kirsher rx = 0; 512527a6266SJeff Kirsher while (rx < budget && rxq->rx_desc_count) { 513527a6266SJeff Kirsher struct rx_desc *rx_desc; 514527a6266SJeff Kirsher unsigned int cmd_sts; 515527a6266SJeff Kirsher struct sk_buff *skb; 516527a6266SJeff Kirsher u16 byte_cnt; 517527a6266SJeff Kirsher 518527a6266SJeff Kirsher rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc]; 519527a6266SJeff Kirsher 520527a6266SJeff Kirsher cmd_sts = rx_desc->cmd_sts; 521527a6266SJeff Kirsher if (cmd_sts & BUFFER_OWNED_BY_DMA) 522527a6266SJeff Kirsher break; 523527a6266SJeff Kirsher rmb(); 524527a6266SJeff Kirsher 525527a6266SJeff Kirsher skb = rxq->rx_skb[rxq->rx_curr_desc]; 526527a6266SJeff Kirsher rxq->rx_skb[rxq->rx_curr_desc] = NULL; 527527a6266SJeff Kirsher 528527a6266SJeff Kirsher rxq->rx_curr_desc++; 529527a6266SJeff Kirsher if (rxq->rx_curr_desc == rxq->rx_ring_size) 530527a6266SJeff Kirsher rxq->rx_curr_desc = 0; 531527a6266SJeff Kirsher 532527a6266SJeff Kirsher dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr, 533527a6266SJeff Kirsher rx_desc->buf_size, DMA_FROM_DEVICE); 534527a6266SJeff Kirsher rxq->rx_desc_count--; 535527a6266SJeff Kirsher rx++; 536527a6266SJeff Kirsher 537527a6266SJeff Kirsher mp->work_rx_refill |= 1 << rxq->index; 538527a6266SJeff Kirsher 539527a6266SJeff Kirsher byte_cnt = rx_desc->byte_cnt; 540527a6266SJeff Kirsher 541527a6266SJeff Kirsher /* 542527a6266SJeff Kirsher * Update statistics. 543527a6266SJeff Kirsher * 544527a6266SJeff Kirsher * Note that the descriptor byte count includes 2 dummy 545527a6266SJeff Kirsher * bytes automatically inserted by the hardware at the 546527a6266SJeff Kirsher * start of the packet (which we don't count), and a 4 547527a6266SJeff Kirsher * byte CRC at the end of the packet (which we do count). 548527a6266SJeff Kirsher */ 549527a6266SJeff Kirsher stats->rx_packets++; 550527a6266SJeff Kirsher stats->rx_bytes += byte_cnt - 2; 551527a6266SJeff Kirsher 552527a6266SJeff Kirsher /* 553527a6266SJeff Kirsher * In case we received a packet without first / last bits 554527a6266SJeff Kirsher * on, or the error summary bit is set, the packet needs 555527a6266SJeff Kirsher * to be dropped. 556527a6266SJeff Kirsher */ 557527a6266SJeff Kirsher if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY)) 558527a6266SJeff Kirsher != (RX_FIRST_DESC | RX_LAST_DESC)) 559527a6266SJeff Kirsher goto err; 560527a6266SJeff Kirsher 561527a6266SJeff Kirsher /* 562527a6266SJeff Kirsher * The -4 is for the CRC in the trailer of the 563527a6266SJeff Kirsher * received packet 564527a6266SJeff Kirsher */ 565527a6266SJeff Kirsher skb_put(skb, byte_cnt - 2 - 4); 566527a6266SJeff Kirsher 567527a6266SJeff Kirsher if (cmd_sts & LAYER_4_CHECKSUM_OK) 568527a6266SJeff Kirsher skb->ip_summed = CHECKSUM_UNNECESSARY; 569527a6266SJeff Kirsher skb->protocol = eth_type_trans(skb, mp->dev); 570527a6266SJeff Kirsher 57109bf1c10SSebastian Hesselbarth napi_gro_receive(&mp->napi, skb); 572527a6266SJeff Kirsher 573527a6266SJeff Kirsher continue; 574527a6266SJeff Kirsher 575527a6266SJeff Kirsher err: 576527a6266SJeff Kirsher stats->rx_dropped++; 577527a6266SJeff Kirsher 578527a6266SJeff Kirsher if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) != 579527a6266SJeff Kirsher (RX_FIRST_DESC | RX_LAST_DESC)) { 580527a6266SJeff Kirsher if (net_ratelimit()) 581527a6266SJeff Kirsher netdev_err(mp->dev, 582527a6266SJeff Kirsher "received packet spanning multiple descriptors\n"); 583527a6266SJeff Kirsher } 584527a6266SJeff Kirsher 585527a6266SJeff Kirsher if (cmd_sts & ERROR_SUMMARY) 586527a6266SJeff Kirsher stats->rx_errors++; 587527a6266SJeff Kirsher 588527a6266SJeff Kirsher dev_kfree_skb(skb); 589527a6266SJeff Kirsher } 590527a6266SJeff Kirsher 591527a6266SJeff Kirsher if (rx < budget) 592527a6266SJeff Kirsher mp->work_rx &= ~(1 << rxq->index); 593527a6266SJeff Kirsher 594527a6266SJeff Kirsher return rx; 595527a6266SJeff Kirsher } 596527a6266SJeff Kirsher 597527a6266SJeff Kirsher static int rxq_refill(struct rx_queue *rxq, int budget) 598527a6266SJeff Kirsher { 599527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 600527a6266SJeff Kirsher int refilled; 601527a6266SJeff Kirsher 602527a6266SJeff Kirsher refilled = 0; 603527a6266SJeff Kirsher while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) { 604527a6266SJeff Kirsher struct sk_buff *skb; 605527a6266SJeff Kirsher int rx; 606527a6266SJeff Kirsher struct rx_desc *rx_desc; 607527a6266SJeff Kirsher int size; 608527a6266SJeff Kirsher 609c056b734SPradeep A Dalvi skb = netdev_alloc_skb(mp->dev, mp->skb_size); 610527a6266SJeff Kirsher 611527a6266SJeff Kirsher if (skb == NULL) { 612527a6266SJeff Kirsher mp->oom = 1; 613527a6266SJeff Kirsher goto oom; 614527a6266SJeff Kirsher } 615527a6266SJeff Kirsher 616527a6266SJeff Kirsher if (SKB_DMA_REALIGN) 617527a6266SJeff Kirsher skb_reserve(skb, SKB_DMA_REALIGN); 618527a6266SJeff Kirsher 619527a6266SJeff Kirsher refilled++; 620527a6266SJeff Kirsher rxq->rx_desc_count++; 621527a6266SJeff Kirsher 622527a6266SJeff Kirsher rx = rxq->rx_used_desc++; 623527a6266SJeff Kirsher if (rxq->rx_used_desc == rxq->rx_ring_size) 624527a6266SJeff Kirsher rxq->rx_used_desc = 0; 625527a6266SJeff Kirsher 626527a6266SJeff Kirsher rx_desc = rxq->rx_desc_area + rx; 627527a6266SJeff Kirsher 62818f1d054SIsaku Yamahata size = skb_end_pointer(skb) - skb->data; 629527a6266SJeff Kirsher rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent, 630527a6266SJeff Kirsher skb->data, size, 631527a6266SJeff Kirsher DMA_FROM_DEVICE); 632527a6266SJeff Kirsher rx_desc->buf_size = size; 633527a6266SJeff Kirsher rxq->rx_skb[rx] = skb; 634527a6266SJeff Kirsher wmb(); 635527a6266SJeff Kirsher rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT; 636527a6266SJeff Kirsher wmb(); 637527a6266SJeff Kirsher 638527a6266SJeff Kirsher /* 639527a6266SJeff Kirsher * The hardware automatically prepends 2 bytes of 640527a6266SJeff Kirsher * dummy data to each received packet, so that the 641527a6266SJeff Kirsher * IP header ends up 16-byte aligned. 642527a6266SJeff Kirsher */ 643527a6266SJeff Kirsher skb_reserve(skb, 2); 644527a6266SJeff Kirsher } 645527a6266SJeff Kirsher 646527a6266SJeff Kirsher if (refilled < budget) 647527a6266SJeff Kirsher mp->work_rx_refill &= ~(1 << rxq->index); 648527a6266SJeff Kirsher 649527a6266SJeff Kirsher oom: 650527a6266SJeff Kirsher return refilled; 651527a6266SJeff Kirsher } 652527a6266SJeff Kirsher 653527a6266SJeff Kirsher 654527a6266SJeff Kirsher /* tx ***********************************************************************/ 655527a6266SJeff Kirsher static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb) 656527a6266SJeff Kirsher { 657527a6266SJeff Kirsher int frag; 658527a6266SJeff Kirsher 659527a6266SJeff Kirsher for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) { 6609e903e08SEric Dumazet const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag]; 6619e903e08SEric Dumazet 662b54c9d5bSJonathan Lemon if (skb_frag_size(fragp) <= 8 && skb_frag_off(fragp) & 7) 663527a6266SJeff Kirsher return 1; 664527a6266SJeff Kirsher } 665527a6266SJeff Kirsher 666527a6266SJeff Kirsher return 0; 667527a6266SJeff Kirsher } 668527a6266SJeff Kirsher 6690a8fa933SEzequiel Garcia static inline __be16 sum16_as_be(__sum16 sum) 6700a8fa933SEzequiel Garcia { 6710a8fa933SEzequiel Garcia return (__force __be16)sum; 6720a8fa933SEzequiel Garcia } 6730a8fa933SEzequiel Garcia 6740a8fa933SEzequiel Garcia static int skb_tx_csum(struct mv643xx_eth_private *mp, struct sk_buff *skb, 6750a8fa933SEzequiel Garcia u16 *l4i_chk, u32 *command, int length) 6760a8fa933SEzequiel Garcia { 6770a8fa933SEzequiel Garcia int ret; 6780a8fa933SEzequiel Garcia u32 cmd = 0; 6790a8fa933SEzequiel Garcia 6800a8fa933SEzequiel Garcia if (skb->ip_summed == CHECKSUM_PARTIAL) { 6810a8fa933SEzequiel Garcia int hdr_len; 6820a8fa933SEzequiel Garcia int tag_bytes; 6830a8fa933SEzequiel Garcia 6840a8fa933SEzequiel Garcia BUG_ON(skb->protocol != htons(ETH_P_IP) && 6850a8fa933SEzequiel Garcia skb->protocol != htons(ETH_P_8021Q)); 6860a8fa933SEzequiel Garcia 6870a8fa933SEzequiel Garcia hdr_len = (void *)ip_hdr(skb) - (void *)skb->data; 6880a8fa933SEzequiel Garcia tag_bytes = hdr_len - ETH_HLEN; 6890a8fa933SEzequiel Garcia 6900a8fa933SEzequiel Garcia if (length - hdr_len > mp->shared->tx_csum_limit || 6910a8fa933SEzequiel Garcia unlikely(tag_bytes & ~12)) { 6920a8fa933SEzequiel Garcia ret = skb_checksum_help(skb); 6930a8fa933SEzequiel Garcia if (!ret) 6940a8fa933SEzequiel Garcia goto no_csum; 6950a8fa933SEzequiel Garcia return ret; 6960a8fa933SEzequiel Garcia } 6970a8fa933SEzequiel Garcia 6980a8fa933SEzequiel Garcia if (tag_bytes & 4) 6990a8fa933SEzequiel Garcia cmd |= MAC_HDR_EXTRA_4_BYTES; 7000a8fa933SEzequiel Garcia if (tag_bytes & 8) 7010a8fa933SEzequiel Garcia cmd |= MAC_HDR_EXTRA_8_BYTES; 7020a8fa933SEzequiel Garcia 70384411f73SEzequiel Garcia cmd |= GEN_TCP_UDP_CHECKSUM | GEN_TCP_UDP_CHK_FULL | 7040a8fa933SEzequiel Garcia GEN_IP_V4_CHECKSUM | 7050a8fa933SEzequiel Garcia ip_hdr(skb)->ihl << TX_IHL_SHIFT; 7060a8fa933SEzequiel Garcia 70784411f73SEzequiel Garcia /* TODO: Revisit this. With the usage of GEN_TCP_UDP_CHK_FULL 70884411f73SEzequiel Garcia * it seems we don't need to pass the initial checksum. */ 7090a8fa933SEzequiel Garcia switch (ip_hdr(skb)->protocol) { 7100a8fa933SEzequiel Garcia case IPPROTO_UDP: 7110a8fa933SEzequiel Garcia cmd |= UDP_FRAME; 71284411f73SEzequiel Garcia *l4i_chk = 0; 7130a8fa933SEzequiel Garcia break; 7140a8fa933SEzequiel Garcia case IPPROTO_TCP: 71584411f73SEzequiel Garcia *l4i_chk = 0; 7160a8fa933SEzequiel Garcia break; 7170a8fa933SEzequiel Garcia default: 7180a8fa933SEzequiel Garcia WARN(1, "protocol not supported"); 7190a8fa933SEzequiel Garcia } 7200a8fa933SEzequiel Garcia } else { 7210a8fa933SEzequiel Garcia no_csum: 7220a8fa933SEzequiel Garcia /* Errata BTS #50, IHL must be 5 if no HW checksum */ 7230a8fa933SEzequiel Garcia cmd |= 5 << TX_IHL_SHIFT; 7240a8fa933SEzequiel Garcia } 7250a8fa933SEzequiel Garcia *command = cmd; 7260a8fa933SEzequiel Garcia return 0; 7270a8fa933SEzequiel Garcia } 7280a8fa933SEzequiel Garcia 7293ae8f4e0SEzequiel Garcia static inline int 7303ae8f4e0SEzequiel Garcia txq_put_data_tso(struct net_device *dev, struct tx_queue *txq, 7313ae8f4e0SEzequiel Garcia struct sk_buff *skb, char *data, int length, 7323ae8f4e0SEzequiel Garcia bool last_tcp, bool is_last) 7333ae8f4e0SEzequiel Garcia { 7343ae8f4e0SEzequiel Garcia int tx_index; 7353ae8f4e0SEzequiel Garcia u32 cmd_sts; 7363ae8f4e0SEzequiel Garcia struct tx_desc *desc; 7373ae8f4e0SEzequiel Garcia 7383ae8f4e0SEzequiel Garcia tx_index = txq->tx_curr_desc++; 7393ae8f4e0SEzequiel Garcia if (txq->tx_curr_desc == txq->tx_ring_size) 7403ae8f4e0SEzequiel Garcia txq->tx_curr_desc = 0; 7413ae8f4e0SEzequiel Garcia desc = &txq->tx_desc_area[tx_index]; 7429e911414SEzequiel Garcia txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE; 7433ae8f4e0SEzequiel Garcia 7443ae8f4e0SEzequiel Garcia desc->l4i_chk = 0; 7453ae8f4e0SEzequiel Garcia desc->byte_cnt = length; 74691986fd3SPhilipp Kirchhofer 74791986fd3SPhilipp Kirchhofer if (length <= 8 && (uintptr_t)data & 0x7) { 74891986fd3SPhilipp Kirchhofer /* Copy unaligned small data fragment to TSO header data area */ 7493b89624aSNicolas Schichan memcpy(txq->tso_hdrs + tx_index * TSO_HEADER_SIZE, 75091986fd3SPhilipp Kirchhofer data, length); 75191986fd3SPhilipp Kirchhofer desc->buf_ptr = txq->tso_hdrs_dma 7523b89624aSNicolas Schichan + tx_index * TSO_HEADER_SIZE; 75391986fd3SPhilipp Kirchhofer } else { 75491986fd3SPhilipp Kirchhofer /* Alignment is okay, map buffer and hand off to hardware */ 75591986fd3SPhilipp Kirchhofer txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE; 7563ae8f4e0SEzequiel Garcia desc->buf_ptr = dma_map_single(dev->dev.parent, data, 7573ae8f4e0SEzequiel Garcia length, DMA_TO_DEVICE); 75891986fd3SPhilipp Kirchhofer if (unlikely(dma_mapping_error(dev->dev.parent, 75991986fd3SPhilipp Kirchhofer desc->buf_ptr))) { 7603ae8f4e0SEzequiel Garcia WARN(1, "dma_map_single failed!\n"); 7613ae8f4e0SEzequiel Garcia return -ENOMEM; 7623ae8f4e0SEzequiel Garcia } 76391986fd3SPhilipp Kirchhofer } 7643ae8f4e0SEzequiel Garcia 7653ae8f4e0SEzequiel Garcia cmd_sts = BUFFER_OWNED_BY_DMA; 7663ae8f4e0SEzequiel Garcia if (last_tcp) { 7673ae8f4e0SEzequiel Garcia /* last descriptor in the TCP packet */ 7683ae8f4e0SEzequiel Garcia cmd_sts |= ZERO_PADDING | TX_LAST_DESC; 7693ae8f4e0SEzequiel Garcia /* last descriptor in SKB */ 7703ae8f4e0SEzequiel Garcia if (is_last) 7713ae8f4e0SEzequiel Garcia cmd_sts |= TX_ENABLE_INTERRUPT; 7723ae8f4e0SEzequiel Garcia } 7733ae8f4e0SEzequiel Garcia desc->cmd_sts = cmd_sts; 7743ae8f4e0SEzequiel Garcia return 0; 7753ae8f4e0SEzequiel Garcia } 7763ae8f4e0SEzequiel Garcia 7773ae8f4e0SEzequiel Garcia static inline void 778968200f3SPhilipp Kirchhofer txq_put_hdr_tso(struct sk_buff *skb, struct tx_queue *txq, int length, 779968200f3SPhilipp Kirchhofer u32 *first_cmd_sts, bool first_desc) 7803ae8f4e0SEzequiel Garcia { 7813ae8f4e0SEzequiel Garcia struct mv643xx_eth_private *mp = txq_to_mp(txq); 7823ae8f4e0SEzequiel Garcia int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 7833ae8f4e0SEzequiel Garcia int tx_index; 7843ae8f4e0SEzequiel Garcia struct tx_desc *desc; 7853ae8f4e0SEzequiel Garcia int ret; 7863ae8f4e0SEzequiel Garcia u32 cmd_csum = 0; 7873ae8f4e0SEzequiel Garcia u16 l4i_chk = 0; 788968200f3SPhilipp Kirchhofer u32 cmd_sts; 7893ae8f4e0SEzequiel Garcia 7903ae8f4e0SEzequiel Garcia tx_index = txq->tx_curr_desc; 7913ae8f4e0SEzequiel Garcia desc = &txq->tx_desc_area[tx_index]; 7923ae8f4e0SEzequiel Garcia 7933ae8f4e0SEzequiel Garcia ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_csum, length); 7943ae8f4e0SEzequiel Garcia if (ret) 7953ae8f4e0SEzequiel Garcia WARN(1, "failed to prepare checksum!"); 7963ae8f4e0SEzequiel Garcia 7973ae8f4e0SEzequiel Garcia /* Should we set this? Can't use the value from skb_tx_csum() 7983ae8f4e0SEzequiel Garcia * as it's not the correct initial L4 checksum to use. */ 7993ae8f4e0SEzequiel Garcia desc->l4i_chk = 0; 8003ae8f4e0SEzequiel Garcia 8013ae8f4e0SEzequiel Garcia desc->byte_cnt = hdr_len; 8023ae8f4e0SEzequiel Garcia desc->buf_ptr = txq->tso_hdrs_dma + 8033ae8f4e0SEzequiel Garcia txq->tx_curr_desc * TSO_HEADER_SIZE; 804968200f3SPhilipp Kirchhofer cmd_sts = cmd_csum | BUFFER_OWNED_BY_DMA | TX_FIRST_DESC | 8053ae8f4e0SEzequiel Garcia GEN_CRC; 8063ae8f4e0SEzequiel Garcia 807968200f3SPhilipp Kirchhofer /* Defer updating the first command descriptor until all 808968200f3SPhilipp Kirchhofer * following descriptors have been written. 809968200f3SPhilipp Kirchhofer */ 810968200f3SPhilipp Kirchhofer if (first_desc) 811968200f3SPhilipp Kirchhofer *first_cmd_sts = cmd_sts; 812968200f3SPhilipp Kirchhofer else 813968200f3SPhilipp Kirchhofer desc->cmd_sts = cmd_sts; 814968200f3SPhilipp Kirchhofer 8153ae8f4e0SEzequiel Garcia txq->tx_curr_desc++; 8163ae8f4e0SEzequiel Garcia if (txq->tx_curr_desc == txq->tx_ring_size) 8173ae8f4e0SEzequiel Garcia txq->tx_curr_desc = 0; 8183ae8f4e0SEzequiel Garcia } 8193ae8f4e0SEzequiel Garcia 8203ae8f4e0SEzequiel Garcia static int txq_submit_tso(struct tx_queue *txq, struct sk_buff *skb, 8213ae8f4e0SEzequiel Garcia struct net_device *dev) 8223ae8f4e0SEzequiel Garcia { 8233ae8f4e0SEzequiel Garcia struct mv643xx_eth_private *mp = txq_to_mp(txq); 8243ae8f4e0SEzequiel Garcia int total_len, data_left, ret; 8253ae8f4e0SEzequiel Garcia int desc_count = 0; 8263ae8f4e0SEzequiel Garcia struct tso_t tso; 8273ae8f4e0SEzequiel Garcia int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 828968200f3SPhilipp Kirchhofer struct tx_desc *first_tx_desc; 829968200f3SPhilipp Kirchhofer u32 first_cmd_sts = 0; 8303ae8f4e0SEzequiel Garcia 8313ae8f4e0SEzequiel Garcia /* Count needed descriptors */ 8323ae8f4e0SEzequiel Garcia if ((txq->tx_desc_count + tso_count_descs(skb)) >= txq->tx_ring_size) { 8333ae8f4e0SEzequiel Garcia netdev_dbg(dev, "not enough descriptors for TSO!\n"); 8343ae8f4e0SEzequiel Garcia return -EBUSY; 8353ae8f4e0SEzequiel Garcia } 8363ae8f4e0SEzequiel Garcia 837968200f3SPhilipp Kirchhofer first_tx_desc = &txq->tx_desc_area[txq->tx_curr_desc]; 838968200f3SPhilipp Kirchhofer 8393ae8f4e0SEzequiel Garcia /* Initialize the TSO handler, and prepare the first payload */ 8403ae8f4e0SEzequiel Garcia tso_start(skb, &tso); 8413ae8f4e0SEzequiel Garcia 8423ae8f4e0SEzequiel Garcia total_len = skb->len - hdr_len; 8433ae8f4e0SEzequiel Garcia while (total_len > 0) { 844968200f3SPhilipp Kirchhofer bool first_desc = (desc_count == 0); 8453ae8f4e0SEzequiel Garcia char *hdr; 8463ae8f4e0SEzequiel Garcia 8473ae8f4e0SEzequiel Garcia data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); 8483ae8f4e0SEzequiel Garcia total_len -= data_left; 8493ae8f4e0SEzequiel Garcia desc_count++; 8503ae8f4e0SEzequiel Garcia 8513ae8f4e0SEzequiel Garcia /* prepare packet headers: MAC + IP + TCP */ 8523ae8f4e0SEzequiel Garcia hdr = txq->tso_hdrs + txq->tx_curr_desc * TSO_HEADER_SIZE; 8533ae8f4e0SEzequiel Garcia tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); 854968200f3SPhilipp Kirchhofer txq_put_hdr_tso(skb, txq, data_left, &first_cmd_sts, 855968200f3SPhilipp Kirchhofer first_desc); 8563ae8f4e0SEzequiel Garcia 8573ae8f4e0SEzequiel Garcia while (data_left > 0) { 8583ae8f4e0SEzequiel Garcia int size; 8593ae8f4e0SEzequiel Garcia desc_count++; 8603ae8f4e0SEzequiel Garcia 8613ae8f4e0SEzequiel Garcia size = min_t(int, tso.size, data_left); 8623ae8f4e0SEzequiel Garcia ret = txq_put_data_tso(dev, txq, skb, tso.data, size, 8633ae8f4e0SEzequiel Garcia size == data_left, 8643ae8f4e0SEzequiel Garcia total_len == 0); 8653ae8f4e0SEzequiel Garcia if (ret) 8663ae8f4e0SEzequiel Garcia goto err_release; 8673ae8f4e0SEzequiel Garcia data_left -= size; 8683ae8f4e0SEzequiel Garcia tso_build_data(skb, &tso, size); 8693ae8f4e0SEzequiel Garcia } 8703ae8f4e0SEzequiel Garcia } 8713ae8f4e0SEzequiel Garcia 8723ae8f4e0SEzequiel Garcia __skb_queue_tail(&txq->tx_skb, skb); 8733ae8f4e0SEzequiel Garcia skb_tx_timestamp(skb); 8743ae8f4e0SEzequiel Garcia 875968200f3SPhilipp Kirchhofer /* ensure all other descriptors are written before first cmd_sts */ 876968200f3SPhilipp Kirchhofer wmb(); 877968200f3SPhilipp Kirchhofer first_tx_desc->cmd_sts = first_cmd_sts; 878968200f3SPhilipp Kirchhofer 8793ae8f4e0SEzequiel Garcia /* clear TX_END status */ 8803ae8f4e0SEzequiel Garcia mp->work_tx_end &= ~(1 << txq->index); 8813ae8f4e0SEzequiel Garcia 8823ae8f4e0SEzequiel Garcia /* ensure all descriptors are written before poking hardware */ 8833ae8f4e0SEzequiel Garcia wmb(); 8843ae8f4e0SEzequiel Garcia txq_enable(txq); 8853ae8f4e0SEzequiel Garcia txq->tx_desc_count += desc_count; 8863ae8f4e0SEzequiel Garcia return 0; 8873ae8f4e0SEzequiel Garcia err_release: 8883ae8f4e0SEzequiel Garcia /* TODO: Release all used data descriptors; header descriptors must not 8893ae8f4e0SEzequiel Garcia * be DMA-unmapped. 8903ae8f4e0SEzequiel Garcia */ 8913ae8f4e0SEzequiel Garcia return ret; 8923ae8f4e0SEzequiel Garcia } 8933ae8f4e0SEzequiel Garcia 894527a6266SJeff Kirsher static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb) 895527a6266SJeff Kirsher { 896527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 897527a6266SJeff Kirsher int nr_frags = skb_shinfo(skb)->nr_frags; 898527a6266SJeff Kirsher int frag; 899527a6266SJeff Kirsher 900527a6266SJeff Kirsher for (frag = 0; frag < nr_frags; frag++) { 901527a6266SJeff Kirsher skb_frag_t *this_frag; 902527a6266SJeff Kirsher int tx_index; 903527a6266SJeff Kirsher struct tx_desc *desc; 904527a6266SJeff Kirsher 905527a6266SJeff Kirsher this_frag = &skb_shinfo(skb)->frags[frag]; 906527a6266SJeff Kirsher tx_index = txq->tx_curr_desc++; 907527a6266SJeff Kirsher if (txq->tx_curr_desc == txq->tx_ring_size) 908527a6266SJeff Kirsher txq->tx_curr_desc = 0; 909527a6266SJeff Kirsher desc = &txq->tx_desc_area[tx_index]; 9109e911414SEzequiel Garcia txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_PAGE; 911527a6266SJeff Kirsher 912527a6266SJeff Kirsher /* 913527a6266SJeff Kirsher * The last fragment will generate an interrupt 914527a6266SJeff Kirsher * which will free the skb on TX completion. 915527a6266SJeff Kirsher */ 916527a6266SJeff Kirsher if (frag == nr_frags - 1) { 917527a6266SJeff Kirsher desc->cmd_sts = BUFFER_OWNED_BY_DMA | 918527a6266SJeff Kirsher ZERO_PADDING | TX_LAST_DESC | 919527a6266SJeff Kirsher TX_ENABLE_INTERRUPT; 920527a6266SJeff Kirsher } else { 921527a6266SJeff Kirsher desc->cmd_sts = BUFFER_OWNED_BY_DMA; 922527a6266SJeff Kirsher } 923527a6266SJeff Kirsher 924527a6266SJeff Kirsher desc->l4i_chk = 0; 9259e903e08SEric Dumazet desc->byte_cnt = skb_frag_size(this_frag); 9269e911414SEzequiel Garcia desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent, 9279e911414SEzequiel Garcia this_frag, 0, desc->byte_cnt, 9289e911414SEzequiel Garcia DMA_TO_DEVICE); 929527a6266SJeff Kirsher } 930527a6266SJeff Kirsher } 931527a6266SJeff Kirsher 932ee9e4956SEzequiel Garcia static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb, 933ee9e4956SEzequiel Garcia struct net_device *dev) 934527a6266SJeff Kirsher { 935527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 936527a6266SJeff Kirsher int nr_frags = skb_shinfo(skb)->nr_frags; 937527a6266SJeff Kirsher int tx_index; 938527a6266SJeff Kirsher struct tx_desc *desc; 939527a6266SJeff Kirsher u32 cmd_sts; 940527a6266SJeff Kirsher u16 l4i_chk; 9410a8fa933SEzequiel Garcia int length, ret; 942527a6266SJeff Kirsher 9430a8fa933SEzequiel Garcia cmd_sts = 0; 944527a6266SJeff Kirsher l4i_chk = 0; 945527a6266SJeff Kirsher 946ee9e4956SEzequiel Garcia if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) { 947ee9e4956SEzequiel Garcia if (net_ratelimit()) 948ee9e4956SEzequiel Garcia netdev_err(dev, "tx queue full?!\n"); 949ee9e4956SEzequiel Garcia return -EBUSY; 950527a6266SJeff Kirsher } 951ee9e4956SEzequiel Garcia 952ee9e4956SEzequiel Garcia ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_sts, skb->len); 953ee9e4956SEzequiel Garcia if (ret) 954ee9e4956SEzequiel Garcia return ret; 9550a8fa933SEzequiel Garcia cmd_sts |= TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA; 956527a6266SJeff Kirsher 957527a6266SJeff Kirsher tx_index = txq->tx_curr_desc++; 958527a6266SJeff Kirsher if (txq->tx_curr_desc == txq->tx_ring_size) 959527a6266SJeff Kirsher txq->tx_curr_desc = 0; 960527a6266SJeff Kirsher desc = &txq->tx_desc_area[tx_index]; 9619e911414SEzequiel Garcia txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE; 962527a6266SJeff Kirsher 963527a6266SJeff Kirsher if (nr_frags) { 964527a6266SJeff Kirsher txq_submit_frag_skb(txq, skb); 965527a6266SJeff Kirsher length = skb_headlen(skb); 966527a6266SJeff Kirsher } else { 967527a6266SJeff Kirsher cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT; 968527a6266SJeff Kirsher length = skb->len; 969527a6266SJeff Kirsher } 970527a6266SJeff Kirsher 971527a6266SJeff Kirsher desc->l4i_chk = l4i_chk; 972527a6266SJeff Kirsher desc->byte_cnt = length; 973527a6266SJeff Kirsher desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data, 974527a6266SJeff Kirsher length, DMA_TO_DEVICE); 975527a6266SJeff Kirsher 976527a6266SJeff Kirsher __skb_queue_tail(&txq->tx_skb, skb); 977527a6266SJeff Kirsher 978527a6266SJeff Kirsher skb_tx_timestamp(skb); 979527a6266SJeff Kirsher 980527a6266SJeff Kirsher /* ensure all other descriptors are written before first cmd_sts */ 981527a6266SJeff Kirsher wmb(); 982527a6266SJeff Kirsher desc->cmd_sts = cmd_sts; 983527a6266SJeff Kirsher 984527a6266SJeff Kirsher /* clear TX_END status */ 985527a6266SJeff Kirsher mp->work_tx_end &= ~(1 << txq->index); 986527a6266SJeff Kirsher 987527a6266SJeff Kirsher /* ensure all descriptors are written before poking hardware */ 988527a6266SJeff Kirsher wmb(); 989527a6266SJeff Kirsher txq_enable(txq); 990527a6266SJeff Kirsher 991527a6266SJeff Kirsher txq->tx_desc_count += nr_frags + 1; 992527a6266SJeff Kirsher 993527a6266SJeff Kirsher return 0; 994527a6266SJeff Kirsher } 995527a6266SJeff Kirsher 996527a6266SJeff Kirsher static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev) 997527a6266SJeff Kirsher { 998527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 9993ae8f4e0SEzequiel Garcia int length, queue, ret; 1000527a6266SJeff Kirsher struct tx_queue *txq; 1001527a6266SJeff Kirsher struct netdev_queue *nq; 1002527a6266SJeff Kirsher 1003527a6266SJeff Kirsher queue = skb_get_queue_mapping(skb); 1004527a6266SJeff Kirsher txq = mp->txq + queue; 1005527a6266SJeff Kirsher nq = netdev_get_tx_queue(dev, queue); 1006527a6266SJeff Kirsher 1007527a6266SJeff Kirsher if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) { 1008527a6266SJeff Kirsher netdev_printk(KERN_DEBUG, dev, 1009527a6266SJeff Kirsher "failed to linearize skb with tiny unaligned fragment\n"); 1010527a6266SJeff Kirsher return NETDEV_TX_BUSY; 1011527a6266SJeff Kirsher } 1012527a6266SJeff Kirsher 1013527a6266SJeff Kirsher length = skb->len; 1014527a6266SJeff Kirsher 10153ae8f4e0SEzequiel Garcia if (skb_is_gso(skb)) 10163ae8f4e0SEzequiel Garcia ret = txq_submit_tso(txq, skb, dev); 10173ae8f4e0SEzequiel Garcia else 1018ee9e4956SEzequiel Garcia ret = txq_submit_skb(txq, skb, dev); 10193ae8f4e0SEzequiel Garcia if (!ret) { 1020527a6266SJeff Kirsher txq->tx_bytes += length; 1021527a6266SJeff Kirsher txq->tx_packets++; 1022527a6266SJeff Kirsher 1023ee9e4956SEzequiel Garcia if (txq->tx_desc_count >= txq->tx_stop_threshold) 1024527a6266SJeff Kirsher netif_tx_stop_queue(nq); 1025dd11680dSEzequiel Garcia } else { 1026dd11680dSEzequiel Garcia txq->tx_dropped++; 1027dd11680dSEzequiel Garcia dev_kfree_skb_any(skb); 1028527a6266SJeff Kirsher } 1029527a6266SJeff Kirsher 1030527a6266SJeff Kirsher return NETDEV_TX_OK; 1031527a6266SJeff Kirsher } 1032527a6266SJeff Kirsher 1033527a6266SJeff Kirsher 1034527a6266SJeff Kirsher /* tx napi ******************************************************************/ 1035527a6266SJeff Kirsher static void txq_kick(struct tx_queue *txq) 1036527a6266SJeff Kirsher { 1037527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 1038527a6266SJeff Kirsher struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index); 1039527a6266SJeff Kirsher u32 hw_desc_ptr; 1040527a6266SJeff Kirsher u32 expected_ptr; 1041527a6266SJeff Kirsher 1042527a6266SJeff Kirsher __netif_tx_lock(nq, smp_processor_id()); 1043527a6266SJeff Kirsher 1044527a6266SJeff Kirsher if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index)) 1045527a6266SJeff Kirsher goto out; 1046527a6266SJeff Kirsher 1047527a6266SJeff Kirsher hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index)); 1048527a6266SJeff Kirsher expected_ptr = (u32)txq->tx_desc_dma + 1049527a6266SJeff Kirsher txq->tx_curr_desc * sizeof(struct tx_desc); 1050527a6266SJeff Kirsher 1051527a6266SJeff Kirsher if (hw_desc_ptr != expected_ptr) 1052527a6266SJeff Kirsher txq_enable(txq); 1053527a6266SJeff Kirsher 1054527a6266SJeff Kirsher out: 1055527a6266SJeff Kirsher __netif_tx_unlock(nq); 1056527a6266SJeff Kirsher 1057527a6266SJeff Kirsher mp->work_tx_end &= ~(1 << txq->index); 1058527a6266SJeff Kirsher } 1059527a6266SJeff Kirsher 1060527a6266SJeff Kirsher static int txq_reclaim(struct tx_queue *txq, int budget, int force) 1061527a6266SJeff Kirsher { 1062527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 1063527a6266SJeff Kirsher struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index); 1064527a6266SJeff Kirsher int reclaimed; 1065527a6266SJeff Kirsher 10663aefe2b4SRussell King __netif_tx_lock_bh(nq); 1067527a6266SJeff Kirsher 1068527a6266SJeff Kirsher reclaimed = 0; 1069527a6266SJeff Kirsher while (reclaimed < budget && txq->tx_desc_count > 0) { 1070527a6266SJeff Kirsher int tx_index; 1071527a6266SJeff Kirsher struct tx_desc *desc; 1072527a6266SJeff Kirsher u32 cmd_sts; 10739e911414SEzequiel Garcia char desc_dma_map; 1074527a6266SJeff Kirsher 1075527a6266SJeff Kirsher tx_index = txq->tx_used_desc; 1076527a6266SJeff Kirsher desc = &txq->tx_desc_area[tx_index]; 10779e911414SEzequiel Garcia desc_dma_map = txq->tx_desc_mapping[tx_index]; 10789e911414SEzequiel Garcia 1079527a6266SJeff Kirsher cmd_sts = desc->cmd_sts; 1080527a6266SJeff Kirsher 1081527a6266SJeff Kirsher if (cmd_sts & BUFFER_OWNED_BY_DMA) { 1082527a6266SJeff Kirsher if (!force) 1083527a6266SJeff Kirsher break; 1084527a6266SJeff Kirsher desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA; 1085527a6266SJeff Kirsher } 1086527a6266SJeff Kirsher 1087527a6266SJeff Kirsher txq->tx_used_desc = tx_index + 1; 1088527a6266SJeff Kirsher if (txq->tx_used_desc == txq->tx_ring_size) 1089527a6266SJeff Kirsher txq->tx_used_desc = 0; 1090527a6266SJeff Kirsher 1091527a6266SJeff Kirsher reclaimed++; 1092527a6266SJeff Kirsher txq->tx_desc_count--; 1093527a6266SJeff Kirsher 10949e911414SEzequiel Garcia if (!IS_TSO_HEADER(txq, desc->buf_ptr)) { 10959e911414SEzequiel Garcia 10969e911414SEzequiel Garcia if (desc_dma_map == DESC_DMA_MAP_PAGE) 10979e911414SEzequiel Garcia dma_unmap_page(mp->dev->dev.parent, 10989e911414SEzequiel Garcia desc->buf_ptr, 10999e911414SEzequiel Garcia desc->byte_cnt, 11009e911414SEzequiel Garcia DMA_TO_DEVICE); 11019e911414SEzequiel Garcia else 11029e911414SEzequiel Garcia dma_unmap_single(mp->dev->dev.parent, 11039e911414SEzequiel Garcia desc->buf_ptr, 11049e911414SEzequiel Garcia desc->byte_cnt, 11059e911414SEzequiel Garcia DMA_TO_DEVICE); 11069e911414SEzequiel Garcia } 11072c2a9cbdSKarl Beldan 11082c2a9cbdSKarl Beldan if (cmd_sts & TX_ENABLE_INTERRUPT) { 11092c2a9cbdSKarl Beldan struct sk_buff *skb = __skb_dequeue(&txq->tx_skb); 11102c2a9cbdSKarl Beldan 11112c2a9cbdSKarl Beldan if (!WARN_ON(!skb)) 111243cee2d2SFlorian Fainelli dev_consume_skb_any(skb); 11132c2a9cbdSKarl Beldan } 1114527a6266SJeff Kirsher 1115527a6266SJeff Kirsher if (cmd_sts & ERROR_SUMMARY) { 1116527a6266SJeff Kirsher netdev_info(mp->dev, "tx error\n"); 1117527a6266SJeff Kirsher mp->dev->stats.tx_errors++; 1118527a6266SJeff Kirsher } 1119527a6266SJeff Kirsher 1120527a6266SJeff Kirsher } 1121527a6266SJeff Kirsher 11223aefe2b4SRussell King __netif_tx_unlock_bh(nq); 1123527a6266SJeff Kirsher 1124527a6266SJeff Kirsher if (reclaimed < budget) 1125527a6266SJeff Kirsher mp->work_tx &= ~(1 << txq->index); 1126527a6266SJeff Kirsher 1127527a6266SJeff Kirsher return reclaimed; 1128527a6266SJeff Kirsher } 1129527a6266SJeff Kirsher 1130527a6266SJeff Kirsher 1131527a6266SJeff Kirsher /* tx rate control **********************************************************/ 1132527a6266SJeff Kirsher /* 1133527a6266SJeff Kirsher * Set total maximum TX rate (shared by all TX queues for this port) 1134527a6266SJeff Kirsher * to 'rate' bits per second, with a maximum burst of 'burst' bytes. 1135527a6266SJeff Kirsher */ 1136527a6266SJeff Kirsher static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst) 1137527a6266SJeff Kirsher { 1138527a6266SJeff Kirsher int token_rate; 1139527a6266SJeff Kirsher int mtu; 1140527a6266SJeff Kirsher int bucket_size; 1141527a6266SJeff Kirsher 1142452503ebSAndrew Lunn token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000); 1143527a6266SJeff Kirsher if (token_rate > 1023) 1144527a6266SJeff Kirsher token_rate = 1023; 1145527a6266SJeff Kirsher 1146527a6266SJeff Kirsher mtu = (mp->dev->mtu + 255) >> 8; 1147527a6266SJeff Kirsher if (mtu > 63) 1148527a6266SJeff Kirsher mtu = 63; 1149527a6266SJeff Kirsher 1150527a6266SJeff Kirsher bucket_size = (burst + 255) >> 8; 1151527a6266SJeff Kirsher if (bucket_size > 65535) 1152527a6266SJeff Kirsher bucket_size = 65535; 1153527a6266SJeff Kirsher 1154527a6266SJeff Kirsher switch (mp->shared->tx_bw_control) { 1155527a6266SJeff Kirsher case TX_BW_CONTROL_OLD_LAYOUT: 1156527a6266SJeff Kirsher wrlp(mp, TX_BW_RATE, token_rate); 1157527a6266SJeff Kirsher wrlp(mp, TX_BW_MTU, mtu); 1158527a6266SJeff Kirsher wrlp(mp, TX_BW_BURST, bucket_size); 1159527a6266SJeff Kirsher break; 1160527a6266SJeff Kirsher case TX_BW_CONTROL_NEW_LAYOUT: 1161527a6266SJeff Kirsher wrlp(mp, TX_BW_RATE_MOVED, token_rate); 1162527a6266SJeff Kirsher wrlp(mp, TX_BW_MTU_MOVED, mtu); 1163527a6266SJeff Kirsher wrlp(mp, TX_BW_BURST_MOVED, bucket_size); 1164527a6266SJeff Kirsher break; 1165527a6266SJeff Kirsher } 1166527a6266SJeff Kirsher } 1167527a6266SJeff Kirsher 1168527a6266SJeff Kirsher static void txq_set_rate(struct tx_queue *txq, int rate, int burst) 1169527a6266SJeff Kirsher { 1170527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 1171527a6266SJeff Kirsher int token_rate; 1172527a6266SJeff Kirsher int bucket_size; 1173527a6266SJeff Kirsher 1174452503ebSAndrew Lunn token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000); 1175527a6266SJeff Kirsher if (token_rate > 1023) 1176527a6266SJeff Kirsher token_rate = 1023; 1177527a6266SJeff Kirsher 1178527a6266SJeff Kirsher bucket_size = (burst + 255) >> 8; 1179527a6266SJeff Kirsher if (bucket_size > 65535) 1180527a6266SJeff Kirsher bucket_size = 65535; 1181527a6266SJeff Kirsher 1182527a6266SJeff Kirsher wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14); 1183527a6266SJeff Kirsher wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate); 1184527a6266SJeff Kirsher } 1185527a6266SJeff Kirsher 1186527a6266SJeff Kirsher static void txq_set_fixed_prio_mode(struct tx_queue *txq) 1187527a6266SJeff Kirsher { 1188527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 1189527a6266SJeff Kirsher int off; 1190527a6266SJeff Kirsher u32 val; 1191527a6266SJeff Kirsher 1192527a6266SJeff Kirsher /* 1193527a6266SJeff Kirsher * Turn on fixed priority mode. 1194527a6266SJeff Kirsher */ 1195527a6266SJeff Kirsher off = 0; 1196527a6266SJeff Kirsher switch (mp->shared->tx_bw_control) { 1197527a6266SJeff Kirsher case TX_BW_CONTROL_OLD_LAYOUT: 1198527a6266SJeff Kirsher off = TXQ_FIX_PRIO_CONF; 1199527a6266SJeff Kirsher break; 1200527a6266SJeff Kirsher case TX_BW_CONTROL_NEW_LAYOUT: 1201527a6266SJeff Kirsher off = TXQ_FIX_PRIO_CONF_MOVED; 1202527a6266SJeff Kirsher break; 1203527a6266SJeff Kirsher } 1204527a6266SJeff Kirsher 1205527a6266SJeff Kirsher if (off) { 1206527a6266SJeff Kirsher val = rdlp(mp, off); 1207527a6266SJeff Kirsher val |= 1 << txq->index; 1208527a6266SJeff Kirsher wrlp(mp, off, val); 1209527a6266SJeff Kirsher } 1210527a6266SJeff Kirsher } 1211527a6266SJeff Kirsher 1212527a6266SJeff Kirsher 1213527a6266SJeff Kirsher /* mii management interface *************************************************/ 12140a9e413bSEzequiel Garcia static void mv643xx_eth_adjust_link(struct net_device *dev) 1215260055bbSPhil Sutter { 12160a9e413bSEzequiel Garcia struct mv643xx_eth_private *mp = netdev_priv(dev); 1217260055bbSPhil Sutter u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL); 1218260055bbSPhil Sutter u32 autoneg_disable = FORCE_LINK_PASS | 1219260055bbSPhil Sutter DISABLE_AUTO_NEG_SPEED_GMII | 1220260055bbSPhil Sutter DISABLE_AUTO_NEG_FOR_FLOW_CTRL | 1221260055bbSPhil Sutter DISABLE_AUTO_NEG_FOR_DUPLEX; 1222260055bbSPhil Sutter 12231e8a655dSPhilippe Reynes if (dev->phydev->autoneg == AUTONEG_ENABLE) { 1224260055bbSPhil Sutter /* enable auto negotiation */ 1225260055bbSPhil Sutter pscr &= ~autoneg_disable; 1226260055bbSPhil Sutter goto out_write; 1227260055bbSPhil Sutter } 1228260055bbSPhil Sutter 1229260055bbSPhil Sutter pscr |= autoneg_disable; 1230260055bbSPhil Sutter 12311e8a655dSPhilippe Reynes if (dev->phydev->speed == SPEED_1000) { 1232260055bbSPhil Sutter /* force gigabit, half duplex not supported */ 1233260055bbSPhil Sutter pscr |= SET_GMII_SPEED_TO_1000; 1234260055bbSPhil Sutter pscr |= SET_FULL_DUPLEX_MODE; 1235260055bbSPhil Sutter goto out_write; 1236260055bbSPhil Sutter } 1237260055bbSPhil Sutter 1238260055bbSPhil Sutter pscr &= ~SET_GMII_SPEED_TO_1000; 1239260055bbSPhil Sutter 12401e8a655dSPhilippe Reynes if (dev->phydev->speed == SPEED_100) 1241260055bbSPhil Sutter pscr |= SET_MII_SPEED_TO_100; 1242260055bbSPhil Sutter else 1243260055bbSPhil Sutter pscr &= ~SET_MII_SPEED_TO_100; 1244260055bbSPhil Sutter 12451e8a655dSPhilippe Reynes if (dev->phydev->duplex == DUPLEX_FULL) 1246260055bbSPhil Sutter pscr |= SET_FULL_DUPLEX_MODE; 1247260055bbSPhil Sutter else 1248260055bbSPhil Sutter pscr &= ~SET_FULL_DUPLEX_MODE; 1249260055bbSPhil Sutter 1250260055bbSPhil Sutter out_write: 1251260055bbSPhil Sutter wrlp(mp, PORT_SERIAL_CONTROL, pscr); 1252260055bbSPhil Sutter } 1253260055bbSPhil Sutter 1254527a6266SJeff Kirsher /* statistics ***************************************************************/ 1255527a6266SJeff Kirsher static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev) 1256527a6266SJeff Kirsher { 1257527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1258527a6266SJeff Kirsher struct net_device_stats *stats = &dev->stats; 1259527a6266SJeff Kirsher unsigned long tx_packets = 0; 1260527a6266SJeff Kirsher unsigned long tx_bytes = 0; 1261527a6266SJeff Kirsher unsigned long tx_dropped = 0; 1262527a6266SJeff Kirsher int i; 1263527a6266SJeff Kirsher 1264527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) { 1265527a6266SJeff Kirsher struct tx_queue *txq = mp->txq + i; 1266527a6266SJeff Kirsher 1267527a6266SJeff Kirsher tx_packets += txq->tx_packets; 1268527a6266SJeff Kirsher tx_bytes += txq->tx_bytes; 1269527a6266SJeff Kirsher tx_dropped += txq->tx_dropped; 1270527a6266SJeff Kirsher } 1271527a6266SJeff Kirsher 1272527a6266SJeff Kirsher stats->tx_packets = tx_packets; 1273527a6266SJeff Kirsher stats->tx_bytes = tx_bytes; 1274527a6266SJeff Kirsher stats->tx_dropped = tx_dropped; 1275527a6266SJeff Kirsher 1276527a6266SJeff Kirsher return stats; 1277527a6266SJeff Kirsher } 1278527a6266SJeff Kirsher 1279527a6266SJeff Kirsher static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset) 1280527a6266SJeff Kirsher { 1281527a6266SJeff Kirsher return rdl(mp, MIB_COUNTERS(mp->port_num) + offset); 1282527a6266SJeff Kirsher } 1283527a6266SJeff Kirsher 1284527a6266SJeff Kirsher static void mib_counters_clear(struct mv643xx_eth_private *mp) 1285527a6266SJeff Kirsher { 1286527a6266SJeff Kirsher int i; 1287527a6266SJeff Kirsher 1288527a6266SJeff Kirsher for (i = 0; i < 0x80; i += 4) 1289527a6266SJeff Kirsher mib_read(mp, i); 1290302476c9SPaulius Zaleckas 1291302476c9SPaulius Zaleckas /* Clear non MIB hw counters also */ 1292302476c9SPaulius Zaleckas rdlp(mp, RX_DISCARD_FRAME_CNT); 1293302476c9SPaulius Zaleckas rdlp(mp, RX_OVERRUN_FRAME_CNT); 1294527a6266SJeff Kirsher } 1295527a6266SJeff Kirsher 1296527a6266SJeff Kirsher static void mib_counters_update(struct mv643xx_eth_private *mp) 1297527a6266SJeff Kirsher { 1298527a6266SJeff Kirsher struct mib_counters *p = &mp->mib_counters; 1299527a6266SJeff Kirsher 1300527a6266SJeff Kirsher spin_lock_bh(&mp->mib_counters_lock); 1301527a6266SJeff Kirsher p->good_octets_received += mib_read(mp, 0x00); 1302527a6266SJeff Kirsher p->bad_octets_received += mib_read(mp, 0x08); 1303527a6266SJeff Kirsher p->internal_mac_transmit_err += mib_read(mp, 0x0c); 1304527a6266SJeff Kirsher p->good_frames_received += mib_read(mp, 0x10); 1305527a6266SJeff Kirsher p->bad_frames_received += mib_read(mp, 0x14); 1306527a6266SJeff Kirsher p->broadcast_frames_received += mib_read(mp, 0x18); 1307527a6266SJeff Kirsher p->multicast_frames_received += mib_read(mp, 0x1c); 1308527a6266SJeff Kirsher p->frames_64_octets += mib_read(mp, 0x20); 1309527a6266SJeff Kirsher p->frames_65_to_127_octets += mib_read(mp, 0x24); 1310527a6266SJeff Kirsher p->frames_128_to_255_octets += mib_read(mp, 0x28); 1311527a6266SJeff Kirsher p->frames_256_to_511_octets += mib_read(mp, 0x2c); 1312527a6266SJeff Kirsher p->frames_512_to_1023_octets += mib_read(mp, 0x30); 1313527a6266SJeff Kirsher p->frames_1024_to_max_octets += mib_read(mp, 0x34); 1314527a6266SJeff Kirsher p->good_octets_sent += mib_read(mp, 0x38); 1315527a6266SJeff Kirsher p->good_frames_sent += mib_read(mp, 0x40); 1316527a6266SJeff Kirsher p->excessive_collision += mib_read(mp, 0x44); 1317527a6266SJeff Kirsher p->multicast_frames_sent += mib_read(mp, 0x48); 1318527a6266SJeff Kirsher p->broadcast_frames_sent += mib_read(mp, 0x4c); 1319527a6266SJeff Kirsher p->unrec_mac_control_received += mib_read(mp, 0x50); 1320527a6266SJeff Kirsher p->fc_sent += mib_read(mp, 0x54); 1321527a6266SJeff Kirsher p->good_fc_received += mib_read(mp, 0x58); 1322527a6266SJeff Kirsher p->bad_fc_received += mib_read(mp, 0x5c); 1323527a6266SJeff Kirsher p->undersize_received += mib_read(mp, 0x60); 1324527a6266SJeff Kirsher p->fragments_received += mib_read(mp, 0x64); 1325527a6266SJeff Kirsher p->oversize_received += mib_read(mp, 0x68); 1326527a6266SJeff Kirsher p->jabber_received += mib_read(mp, 0x6c); 1327527a6266SJeff Kirsher p->mac_receive_error += mib_read(mp, 0x70); 1328527a6266SJeff Kirsher p->bad_crc_event += mib_read(mp, 0x74); 1329527a6266SJeff Kirsher p->collision += mib_read(mp, 0x78); 1330527a6266SJeff Kirsher p->late_collision += mib_read(mp, 0x7c); 1331302476c9SPaulius Zaleckas /* Non MIB hardware counters */ 1332302476c9SPaulius Zaleckas p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT); 1333302476c9SPaulius Zaleckas p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT); 1334527a6266SJeff Kirsher spin_unlock_bh(&mp->mib_counters_lock); 1335527a6266SJeff Kirsher } 1336527a6266SJeff Kirsher 1337e99e88a9SKees Cook static void mib_counters_timer_wrapper(struct timer_list *t) 1338527a6266SJeff Kirsher { 1339e99e88a9SKees Cook struct mv643xx_eth_private *mp = from_timer(mp, t, mib_counters_timer); 1340527a6266SJeff Kirsher mib_counters_update(mp); 1341041b4ddbSSebastian Hesselbarth mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ); 1342527a6266SJeff Kirsher } 1343527a6266SJeff Kirsher 1344527a6266SJeff Kirsher 1345527a6266SJeff Kirsher /* interrupt coalescing *****************************************************/ 1346527a6266SJeff Kirsher /* 1347527a6266SJeff Kirsher * Hardware coalescing parameters are set in units of 64 t_clk 1348527a6266SJeff Kirsher * cycles. I.e.: 1349527a6266SJeff Kirsher * 1350527a6266SJeff Kirsher * coal_delay_in_usec = 64000000 * register_value / t_clk_rate 1351527a6266SJeff Kirsher * 1352527a6266SJeff Kirsher * register_value = coal_delay_in_usec * t_clk_rate / 64000000 1353527a6266SJeff Kirsher * 1354527a6266SJeff Kirsher * In the ->set*() methods, we round the computed register value 1355527a6266SJeff Kirsher * to the nearest integer. 1356527a6266SJeff Kirsher */ 1357527a6266SJeff Kirsher static unsigned int get_rx_coal(struct mv643xx_eth_private *mp) 1358527a6266SJeff Kirsher { 1359527a6266SJeff Kirsher u32 val = rdlp(mp, SDMA_CONFIG); 1360527a6266SJeff Kirsher u64 temp; 1361527a6266SJeff Kirsher 1362527a6266SJeff Kirsher if (mp->shared->extended_rx_coal_limit) 1363527a6266SJeff Kirsher temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7); 1364527a6266SJeff Kirsher else 1365527a6266SJeff Kirsher temp = (val & 0x003fff00) >> 8; 1366527a6266SJeff Kirsher 1367527a6266SJeff Kirsher temp *= 64000000; 1368b9b84fc0SRussell King temp += mp->t_clk / 2; 1369452503ebSAndrew Lunn do_div(temp, mp->t_clk); 1370527a6266SJeff Kirsher 1371527a6266SJeff Kirsher return (unsigned int)temp; 1372527a6266SJeff Kirsher } 1373527a6266SJeff Kirsher 1374527a6266SJeff Kirsher static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec) 1375527a6266SJeff Kirsher { 1376527a6266SJeff Kirsher u64 temp; 1377527a6266SJeff Kirsher u32 val; 1378527a6266SJeff Kirsher 1379452503ebSAndrew Lunn temp = (u64)usec * mp->t_clk; 1380527a6266SJeff Kirsher temp += 31999999; 1381527a6266SJeff Kirsher do_div(temp, 64000000); 1382527a6266SJeff Kirsher 1383527a6266SJeff Kirsher val = rdlp(mp, SDMA_CONFIG); 1384527a6266SJeff Kirsher if (mp->shared->extended_rx_coal_limit) { 1385527a6266SJeff Kirsher if (temp > 0xffff) 1386527a6266SJeff Kirsher temp = 0xffff; 1387527a6266SJeff Kirsher val &= ~0x023fff80; 1388527a6266SJeff Kirsher val |= (temp & 0x8000) << 10; 1389527a6266SJeff Kirsher val |= (temp & 0x7fff) << 7; 1390527a6266SJeff Kirsher } else { 1391527a6266SJeff Kirsher if (temp > 0x3fff) 1392527a6266SJeff Kirsher temp = 0x3fff; 1393527a6266SJeff Kirsher val &= ~0x003fff00; 1394527a6266SJeff Kirsher val |= (temp & 0x3fff) << 8; 1395527a6266SJeff Kirsher } 1396527a6266SJeff Kirsher wrlp(mp, SDMA_CONFIG, val); 1397527a6266SJeff Kirsher } 1398527a6266SJeff Kirsher 1399527a6266SJeff Kirsher static unsigned int get_tx_coal(struct mv643xx_eth_private *mp) 1400527a6266SJeff Kirsher { 1401527a6266SJeff Kirsher u64 temp; 1402527a6266SJeff Kirsher 1403527a6266SJeff Kirsher temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4; 1404527a6266SJeff Kirsher temp *= 64000000; 1405b9b84fc0SRussell King temp += mp->t_clk / 2; 1406452503ebSAndrew Lunn do_div(temp, mp->t_clk); 1407527a6266SJeff Kirsher 1408527a6266SJeff Kirsher return (unsigned int)temp; 1409527a6266SJeff Kirsher } 1410527a6266SJeff Kirsher 1411527a6266SJeff Kirsher static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec) 1412527a6266SJeff Kirsher { 1413527a6266SJeff Kirsher u64 temp; 1414527a6266SJeff Kirsher 1415452503ebSAndrew Lunn temp = (u64)usec * mp->t_clk; 1416527a6266SJeff Kirsher temp += 31999999; 1417527a6266SJeff Kirsher do_div(temp, 64000000); 1418527a6266SJeff Kirsher 1419527a6266SJeff Kirsher if (temp > 0x3fff) 1420527a6266SJeff Kirsher temp = 0x3fff; 1421527a6266SJeff Kirsher 1422527a6266SJeff Kirsher wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4); 1423527a6266SJeff Kirsher } 1424527a6266SJeff Kirsher 1425527a6266SJeff Kirsher 1426527a6266SJeff Kirsher /* ethtool ******************************************************************/ 1427527a6266SJeff Kirsher struct mv643xx_eth_stats { 1428527a6266SJeff Kirsher char stat_string[ETH_GSTRING_LEN]; 1429527a6266SJeff Kirsher int sizeof_stat; 1430527a6266SJeff Kirsher int netdev_off; 1431527a6266SJeff Kirsher int mp_off; 1432527a6266SJeff Kirsher }; 1433527a6266SJeff Kirsher 1434527a6266SJeff Kirsher #define SSTAT(m) \ 1435527a6266SJeff Kirsher { #m, FIELD_SIZEOF(struct net_device_stats, m), \ 1436527a6266SJeff Kirsher offsetof(struct net_device, stats.m), -1 } 1437527a6266SJeff Kirsher 1438527a6266SJeff Kirsher #define MIBSTAT(m) \ 1439527a6266SJeff Kirsher { #m, FIELD_SIZEOF(struct mib_counters, m), \ 1440527a6266SJeff Kirsher -1, offsetof(struct mv643xx_eth_private, mib_counters.m) } 1441527a6266SJeff Kirsher 1442527a6266SJeff Kirsher static const struct mv643xx_eth_stats mv643xx_eth_stats[] = { 1443527a6266SJeff Kirsher SSTAT(rx_packets), 1444527a6266SJeff Kirsher SSTAT(tx_packets), 1445527a6266SJeff Kirsher SSTAT(rx_bytes), 1446527a6266SJeff Kirsher SSTAT(tx_bytes), 1447527a6266SJeff Kirsher SSTAT(rx_errors), 1448527a6266SJeff Kirsher SSTAT(tx_errors), 1449527a6266SJeff Kirsher SSTAT(rx_dropped), 1450527a6266SJeff Kirsher SSTAT(tx_dropped), 1451527a6266SJeff Kirsher MIBSTAT(good_octets_received), 1452527a6266SJeff Kirsher MIBSTAT(bad_octets_received), 1453527a6266SJeff Kirsher MIBSTAT(internal_mac_transmit_err), 1454527a6266SJeff Kirsher MIBSTAT(good_frames_received), 1455527a6266SJeff Kirsher MIBSTAT(bad_frames_received), 1456527a6266SJeff Kirsher MIBSTAT(broadcast_frames_received), 1457527a6266SJeff Kirsher MIBSTAT(multicast_frames_received), 1458527a6266SJeff Kirsher MIBSTAT(frames_64_octets), 1459527a6266SJeff Kirsher MIBSTAT(frames_65_to_127_octets), 1460527a6266SJeff Kirsher MIBSTAT(frames_128_to_255_octets), 1461527a6266SJeff Kirsher MIBSTAT(frames_256_to_511_octets), 1462527a6266SJeff Kirsher MIBSTAT(frames_512_to_1023_octets), 1463527a6266SJeff Kirsher MIBSTAT(frames_1024_to_max_octets), 1464527a6266SJeff Kirsher MIBSTAT(good_octets_sent), 1465527a6266SJeff Kirsher MIBSTAT(good_frames_sent), 1466527a6266SJeff Kirsher MIBSTAT(excessive_collision), 1467527a6266SJeff Kirsher MIBSTAT(multicast_frames_sent), 1468527a6266SJeff Kirsher MIBSTAT(broadcast_frames_sent), 1469527a6266SJeff Kirsher MIBSTAT(unrec_mac_control_received), 1470527a6266SJeff Kirsher MIBSTAT(fc_sent), 1471527a6266SJeff Kirsher MIBSTAT(good_fc_received), 1472527a6266SJeff Kirsher MIBSTAT(bad_fc_received), 1473527a6266SJeff Kirsher MIBSTAT(undersize_received), 1474527a6266SJeff Kirsher MIBSTAT(fragments_received), 1475527a6266SJeff Kirsher MIBSTAT(oversize_received), 1476527a6266SJeff Kirsher MIBSTAT(jabber_received), 1477527a6266SJeff Kirsher MIBSTAT(mac_receive_error), 1478527a6266SJeff Kirsher MIBSTAT(bad_crc_event), 1479527a6266SJeff Kirsher MIBSTAT(collision), 1480527a6266SJeff Kirsher MIBSTAT(late_collision), 1481302476c9SPaulius Zaleckas MIBSTAT(rx_discard), 1482302476c9SPaulius Zaleckas MIBSTAT(rx_overrun), 1483527a6266SJeff Kirsher }; 1484527a6266SJeff Kirsher 1485527a6266SJeff Kirsher static int 1486a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings_phy(struct mv643xx_eth_private *mp, 1487a54e1612SPhilippe Reynes struct ethtool_link_ksettings *cmd) 1488527a6266SJeff Kirsher { 14891e8a655dSPhilippe Reynes struct net_device *dev = mp->dev; 1490527a6266SJeff Kirsher 14915514174fSyuval.shaia@oracle.com phy_ethtool_ksettings_get(dev->phydev, cmd); 1492527a6266SJeff Kirsher 1493527a6266SJeff Kirsher /* 1494527a6266SJeff Kirsher * The MAC does not support 1000baseT_Half. 1495527a6266SJeff Kirsher */ 14963c1bcc86SAndrew Lunn linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 1497a54e1612SPhilippe Reynes cmd->link_modes.supported); 14983c1bcc86SAndrew Lunn linkmode_clear_bit(ETHTOOL_LINK_MODE_1000baseT_Half_BIT, 1499a54e1612SPhilippe Reynes cmd->link_modes.advertising); 1500527a6266SJeff Kirsher 15015514174fSyuval.shaia@oracle.com return 0; 1502527a6266SJeff Kirsher } 1503527a6266SJeff Kirsher 1504527a6266SJeff Kirsher static int 1505a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings_phyless(struct mv643xx_eth_private *mp, 1506a54e1612SPhilippe Reynes struct ethtool_link_ksettings *cmd) 1507527a6266SJeff Kirsher { 1508527a6266SJeff Kirsher u32 port_status; 1509a54e1612SPhilippe Reynes u32 supported, advertising; 1510527a6266SJeff Kirsher 1511527a6266SJeff Kirsher port_status = rdlp(mp, PORT_STATUS); 1512527a6266SJeff Kirsher 1513a54e1612SPhilippe Reynes supported = SUPPORTED_MII; 1514a54e1612SPhilippe Reynes advertising = ADVERTISED_MII; 1515527a6266SJeff Kirsher switch (port_status & PORT_SPEED_MASK) { 1516527a6266SJeff Kirsher case PORT_SPEED_10: 1517a54e1612SPhilippe Reynes cmd->base.speed = SPEED_10; 1518527a6266SJeff Kirsher break; 1519527a6266SJeff Kirsher case PORT_SPEED_100: 1520a54e1612SPhilippe Reynes cmd->base.speed = SPEED_100; 1521527a6266SJeff Kirsher break; 1522527a6266SJeff Kirsher case PORT_SPEED_1000: 1523a54e1612SPhilippe Reynes cmd->base.speed = SPEED_1000; 1524527a6266SJeff Kirsher break; 1525527a6266SJeff Kirsher default: 1526a54e1612SPhilippe Reynes cmd->base.speed = -1; 1527527a6266SJeff Kirsher break; 1528527a6266SJeff Kirsher } 1529a54e1612SPhilippe Reynes cmd->base.duplex = (port_status & FULL_DUPLEX) ? 1530a54e1612SPhilippe Reynes DUPLEX_FULL : DUPLEX_HALF; 1531a54e1612SPhilippe Reynes cmd->base.port = PORT_MII; 1532a54e1612SPhilippe Reynes cmd->base.phy_address = 0; 1533a54e1612SPhilippe Reynes cmd->base.autoneg = AUTONEG_DISABLE; 1534a54e1612SPhilippe Reynes 1535a54e1612SPhilippe Reynes ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported, 1536a54e1612SPhilippe Reynes supported); 1537a54e1612SPhilippe Reynes ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising, 1538a54e1612SPhilippe Reynes advertising); 1539527a6266SJeff Kirsher 1540527a6266SJeff Kirsher return 0; 1541527a6266SJeff Kirsher } 1542527a6266SJeff Kirsher 15433871c387SMichael Stapelberg static void 15443871c387SMichael Stapelberg mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 15453871c387SMichael Stapelberg { 15463871c387SMichael Stapelberg wol->supported = 0; 15473871c387SMichael Stapelberg wol->wolopts = 0; 15481e8a655dSPhilippe Reynes if (dev->phydev) 15491e8a655dSPhilippe Reynes phy_ethtool_get_wol(dev->phydev, wol); 15503871c387SMichael Stapelberg } 15513871c387SMichael Stapelberg 15523871c387SMichael Stapelberg static int 15533871c387SMichael Stapelberg mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 15543871c387SMichael Stapelberg { 15553871c387SMichael Stapelberg int err; 15563871c387SMichael Stapelberg 15571e8a655dSPhilippe Reynes if (!dev->phydev) 15583871c387SMichael Stapelberg return -EOPNOTSUPP; 15593871c387SMichael Stapelberg 15601e8a655dSPhilippe Reynes err = phy_ethtool_set_wol(dev->phydev, wol); 15613871c387SMichael Stapelberg /* Given that mv643xx_eth works without the marvell-specific PHY driver, 15623871c387SMichael Stapelberg * this debugging hint is useful to have. 15633871c387SMichael Stapelberg */ 15643871c387SMichael Stapelberg if (err == -EOPNOTSUPP) 15653871c387SMichael Stapelberg netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n"); 15663871c387SMichael Stapelberg return err; 15673871c387SMichael Stapelberg } 15683871c387SMichael Stapelberg 1569527a6266SJeff Kirsher static int 1570a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings(struct net_device *dev, 1571a54e1612SPhilippe Reynes struct ethtool_link_ksettings *cmd) 1572527a6266SJeff Kirsher { 1573527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1574527a6266SJeff Kirsher 15751e8a655dSPhilippe Reynes if (dev->phydev) 1576a54e1612SPhilippe Reynes return mv643xx_eth_get_link_ksettings_phy(mp, cmd); 1577527a6266SJeff Kirsher else 1578a54e1612SPhilippe Reynes return mv643xx_eth_get_link_ksettings_phyless(mp, cmd); 1579527a6266SJeff Kirsher } 1580527a6266SJeff Kirsher 1581527a6266SJeff Kirsher static int 1582a54e1612SPhilippe Reynes mv643xx_eth_set_link_ksettings(struct net_device *dev, 1583a54e1612SPhilippe Reynes const struct ethtool_link_ksettings *cmd) 1584527a6266SJeff Kirsher { 1585a54e1612SPhilippe Reynes struct ethtool_link_ksettings c = *cmd; 1586a54e1612SPhilippe Reynes u32 advertising; 1587260055bbSPhil Sutter int ret; 1588527a6266SJeff Kirsher 15891e8a655dSPhilippe Reynes if (!dev->phydev) 1590527a6266SJeff Kirsher return -EINVAL; 1591527a6266SJeff Kirsher 1592527a6266SJeff Kirsher /* 1593527a6266SJeff Kirsher * The MAC does not support 1000baseT_Half. 1594527a6266SJeff Kirsher */ 1595a54e1612SPhilippe Reynes ethtool_convert_link_mode_to_legacy_u32(&advertising, 1596a54e1612SPhilippe Reynes c.link_modes.advertising); 1597a54e1612SPhilippe Reynes advertising &= ~ADVERTISED_1000baseT_Half; 1598a54e1612SPhilippe Reynes ethtool_convert_legacy_u32_to_link_mode(c.link_modes.advertising, 1599a54e1612SPhilippe Reynes advertising); 1600527a6266SJeff Kirsher 1601a54e1612SPhilippe Reynes ret = phy_ethtool_ksettings_set(dev->phydev, &c); 1602260055bbSPhil Sutter if (!ret) 16030a9e413bSEzequiel Garcia mv643xx_eth_adjust_link(dev); 1604260055bbSPhil Sutter return ret; 1605527a6266SJeff Kirsher } 1606527a6266SJeff Kirsher 1607527a6266SJeff Kirsher static void mv643xx_eth_get_drvinfo(struct net_device *dev, 1608527a6266SJeff Kirsher struct ethtool_drvinfo *drvinfo) 1609527a6266SJeff Kirsher { 16106f39da2cSAxel Lin strlcpy(drvinfo->driver, mv643xx_eth_driver_name, 16116f39da2cSAxel Lin sizeof(drvinfo->driver)); 161268aad78cSRick Jones strlcpy(drvinfo->version, mv643xx_eth_driver_version, 16136f39da2cSAxel Lin sizeof(drvinfo->version)); 16146f39da2cSAxel Lin strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 16156f39da2cSAxel Lin strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 1616527a6266SJeff Kirsher } 1617527a6266SJeff Kirsher 1618527a6266SJeff Kirsher static int 1619527a6266SJeff Kirsher mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec) 1620527a6266SJeff Kirsher { 1621527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1622527a6266SJeff Kirsher 1623527a6266SJeff Kirsher ec->rx_coalesce_usecs = get_rx_coal(mp); 1624527a6266SJeff Kirsher ec->tx_coalesce_usecs = get_tx_coal(mp); 1625527a6266SJeff Kirsher 1626527a6266SJeff Kirsher return 0; 1627527a6266SJeff Kirsher } 1628527a6266SJeff Kirsher 1629527a6266SJeff Kirsher static int 1630527a6266SJeff Kirsher mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec) 1631527a6266SJeff Kirsher { 1632527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1633527a6266SJeff Kirsher 1634527a6266SJeff Kirsher set_rx_coal(mp, ec->rx_coalesce_usecs); 1635527a6266SJeff Kirsher set_tx_coal(mp, ec->tx_coalesce_usecs); 1636527a6266SJeff Kirsher 1637527a6266SJeff Kirsher return 0; 1638527a6266SJeff Kirsher } 1639527a6266SJeff Kirsher 1640527a6266SJeff Kirsher static void 1641527a6266SJeff Kirsher mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er) 1642527a6266SJeff Kirsher { 1643527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1644527a6266SJeff Kirsher 1645527a6266SJeff Kirsher er->rx_max_pending = 4096; 1646527a6266SJeff Kirsher er->tx_max_pending = 4096; 1647527a6266SJeff Kirsher 1648527a6266SJeff Kirsher er->rx_pending = mp->rx_ring_size; 1649527a6266SJeff Kirsher er->tx_pending = mp->tx_ring_size; 1650527a6266SJeff Kirsher } 1651527a6266SJeff Kirsher 1652527a6266SJeff Kirsher static int 1653527a6266SJeff Kirsher mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er) 1654527a6266SJeff Kirsher { 1655527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1656527a6266SJeff Kirsher 1657527a6266SJeff Kirsher if (er->rx_mini_pending || er->rx_jumbo_pending) 1658527a6266SJeff Kirsher return -EINVAL; 1659527a6266SJeff Kirsher 1660527a6266SJeff Kirsher mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096; 1661ee9e4956SEzequiel Garcia mp->tx_ring_size = clamp_t(unsigned int, er->tx_pending, 1662ee9e4956SEzequiel Garcia MV643XX_MAX_SKB_DESCS * 2, 4096); 1663ee9e4956SEzequiel Garcia if (mp->tx_ring_size != er->tx_pending) 1664ee9e4956SEzequiel Garcia netdev_warn(dev, "TX queue size set to %u (requested %u)\n", 1665ee9e4956SEzequiel Garcia mp->tx_ring_size, er->tx_pending); 1666527a6266SJeff Kirsher 1667527a6266SJeff Kirsher if (netif_running(dev)) { 1668527a6266SJeff Kirsher mv643xx_eth_stop(dev); 1669527a6266SJeff Kirsher if (mv643xx_eth_open(dev)) { 1670527a6266SJeff Kirsher netdev_err(dev, 1671527a6266SJeff Kirsher "fatal error on re-opening device after ring param change\n"); 1672527a6266SJeff Kirsher return -ENOMEM; 1673527a6266SJeff Kirsher } 1674527a6266SJeff Kirsher } 1675527a6266SJeff Kirsher 1676527a6266SJeff Kirsher return 0; 1677527a6266SJeff Kirsher } 1678527a6266SJeff Kirsher 1679527a6266SJeff Kirsher 1680527a6266SJeff Kirsher static int 1681c8f44affSMichał Mirosław mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features) 1682527a6266SJeff Kirsher { 1683527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 16843ad9b358SMichał Mirosław bool rx_csum = features & NETIF_F_RXCSUM; 1685527a6266SJeff Kirsher 1686527a6266SJeff Kirsher wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000); 1687527a6266SJeff Kirsher 1688527a6266SJeff Kirsher return 0; 1689527a6266SJeff Kirsher } 1690527a6266SJeff Kirsher 1691527a6266SJeff Kirsher static void mv643xx_eth_get_strings(struct net_device *dev, 1692527a6266SJeff Kirsher uint32_t stringset, uint8_t *data) 1693527a6266SJeff Kirsher { 1694527a6266SJeff Kirsher int i; 1695527a6266SJeff Kirsher 1696527a6266SJeff Kirsher if (stringset == ETH_SS_STATS) { 1697527a6266SJeff Kirsher for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) { 1698527a6266SJeff Kirsher memcpy(data + i * ETH_GSTRING_LEN, 1699527a6266SJeff Kirsher mv643xx_eth_stats[i].stat_string, 1700527a6266SJeff Kirsher ETH_GSTRING_LEN); 1701527a6266SJeff Kirsher } 1702527a6266SJeff Kirsher } 1703527a6266SJeff Kirsher } 1704527a6266SJeff Kirsher 1705527a6266SJeff Kirsher static void mv643xx_eth_get_ethtool_stats(struct net_device *dev, 1706527a6266SJeff Kirsher struct ethtool_stats *stats, 1707527a6266SJeff Kirsher uint64_t *data) 1708527a6266SJeff Kirsher { 1709527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1710527a6266SJeff Kirsher int i; 1711527a6266SJeff Kirsher 1712527a6266SJeff Kirsher mv643xx_eth_get_stats(dev); 1713527a6266SJeff Kirsher mib_counters_update(mp); 1714527a6266SJeff Kirsher 1715527a6266SJeff Kirsher for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) { 1716527a6266SJeff Kirsher const struct mv643xx_eth_stats *stat; 1717527a6266SJeff Kirsher void *p; 1718527a6266SJeff Kirsher 1719527a6266SJeff Kirsher stat = mv643xx_eth_stats + i; 1720527a6266SJeff Kirsher 1721527a6266SJeff Kirsher if (stat->netdev_off >= 0) 1722527a6266SJeff Kirsher p = ((void *)mp->dev) + stat->netdev_off; 1723527a6266SJeff Kirsher else 1724527a6266SJeff Kirsher p = ((void *)mp) + stat->mp_off; 1725527a6266SJeff Kirsher 1726527a6266SJeff Kirsher data[i] = (stat->sizeof_stat == 8) ? 1727527a6266SJeff Kirsher *(uint64_t *)p : *(uint32_t *)p; 1728527a6266SJeff Kirsher } 1729527a6266SJeff Kirsher } 1730527a6266SJeff Kirsher 1731527a6266SJeff Kirsher static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset) 1732527a6266SJeff Kirsher { 1733527a6266SJeff Kirsher if (sset == ETH_SS_STATS) 1734527a6266SJeff Kirsher return ARRAY_SIZE(mv643xx_eth_stats); 1735527a6266SJeff Kirsher 1736527a6266SJeff Kirsher return -EOPNOTSUPP; 1737527a6266SJeff Kirsher } 1738527a6266SJeff Kirsher 1739527a6266SJeff Kirsher static const struct ethtool_ops mv643xx_eth_ethtool_ops = { 1740527a6266SJeff Kirsher .get_drvinfo = mv643xx_eth_get_drvinfo, 1741fc5e353cSFlorian Fainelli .nway_reset = phy_ethtool_nway_reset, 1742527a6266SJeff Kirsher .get_link = ethtool_op_get_link, 1743527a6266SJeff Kirsher .get_coalesce = mv643xx_eth_get_coalesce, 1744527a6266SJeff Kirsher .set_coalesce = mv643xx_eth_set_coalesce, 1745527a6266SJeff Kirsher .get_ringparam = mv643xx_eth_get_ringparam, 1746527a6266SJeff Kirsher .set_ringparam = mv643xx_eth_set_ringparam, 1747527a6266SJeff Kirsher .get_strings = mv643xx_eth_get_strings, 1748527a6266SJeff Kirsher .get_ethtool_stats = mv643xx_eth_get_ethtool_stats, 1749527a6266SJeff Kirsher .get_sset_count = mv643xx_eth_get_sset_count, 1750ebad0a8dSRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 17513871c387SMichael Stapelberg .get_wol = mv643xx_eth_get_wol, 17523871c387SMichael Stapelberg .set_wol = mv643xx_eth_set_wol, 1753a54e1612SPhilippe Reynes .get_link_ksettings = mv643xx_eth_get_link_ksettings, 1754a54e1612SPhilippe Reynes .set_link_ksettings = mv643xx_eth_set_link_ksettings, 1755527a6266SJeff Kirsher }; 1756527a6266SJeff Kirsher 1757527a6266SJeff Kirsher 1758527a6266SJeff Kirsher /* address handling *********************************************************/ 1759527a6266SJeff Kirsher static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr) 1760527a6266SJeff Kirsher { 1761527a6266SJeff Kirsher unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH); 1762527a6266SJeff Kirsher unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW); 1763527a6266SJeff Kirsher 1764527a6266SJeff Kirsher addr[0] = (mac_h >> 24) & 0xff; 1765527a6266SJeff Kirsher addr[1] = (mac_h >> 16) & 0xff; 1766527a6266SJeff Kirsher addr[2] = (mac_h >> 8) & 0xff; 1767527a6266SJeff Kirsher addr[3] = mac_h & 0xff; 1768527a6266SJeff Kirsher addr[4] = (mac_l >> 8) & 0xff; 1769527a6266SJeff Kirsher addr[5] = mac_l & 0xff; 1770527a6266SJeff Kirsher } 1771527a6266SJeff Kirsher 1772527a6266SJeff Kirsher static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr) 1773527a6266SJeff Kirsher { 1774527a6266SJeff Kirsher wrlp(mp, MAC_ADDR_HIGH, 1775527a6266SJeff Kirsher (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]); 1776527a6266SJeff Kirsher wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]); 1777527a6266SJeff Kirsher } 1778527a6266SJeff Kirsher 1779527a6266SJeff Kirsher static u32 uc_addr_filter_mask(struct net_device *dev) 1780527a6266SJeff Kirsher { 1781527a6266SJeff Kirsher struct netdev_hw_addr *ha; 1782527a6266SJeff Kirsher u32 nibbles; 1783527a6266SJeff Kirsher 1784527a6266SJeff Kirsher if (dev->flags & IFF_PROMISC) 1785527a6266SJeff Kirsher return 0; 1786527a6266SJeff Kirsher 1787527a6266SJeff Kirsher nibbles = 1 << (dev->dev_addr[5] & 0x0f); 1788527a6266SJeff Kirsher netdev_for_each_uc_addr(ha, dev) { 1789527a6266SJeff Kirsher if (memcmp(dev->dev_addr, ha->addr, 5)) 1790527a6266SJeff Kirsher return 0; 1791527a6266SJeff Kirsher if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0) 1792527a6266SJeff Kirsher return 0; 1793527a6266SJeff Kirsher 1794527a6266SJeff Kirsher nibbles |= 1 << (ha->addr[5] & 0x0f); 1795527a6266SJeff Kirsher } 1796527a6266SJeff Kirsher 1797527a6266SJeff Kirsher return nibbles; 1798527a6266SJeff Kirsher } 1799527a6266SJeff Kirsher 1800527a6266SJeff Kirsher static void mv643xx_eth_program_unicast_filter(struct net_device *dev) 1801527a6266SJeff Kirsher { 1802527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1803527a6266SJeff Kirsher u32 port_config; 1804527a6266SJeff Kirsher u32 nibbles; 1805527a6266SJeff Kirsher int i; 1806527a6266SJeff Kirsher 1807527a6266SJeff Kirsher uc_addr_set(mp, dev->dev_addr); 1808527a6266SJeff Kirsher 1809527a6266SJeff Kirsher port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE; 1810527a6266SJeff Kirsher 1811527a6266SJeff Kirsher nibbles = uc_addr_filter_mask(dev); 1812527a6266SJeff Kirsher if (!nibbles) { 1813527a6266SJeff Kirsher port_config |= UNICAST_PROMISCUOUS_MODE; 1814527a6266SJeff Kirsher nibbles = 0xffff; 1815527a6266SJeff Kirsher } 1816527a6266SJeff Kirsher 1817527a6266SJeff Kirsher for (i = 0; i < 16; i += 4) { 1818527a6266SJeff Kirsher int off = UNICAST_TABLE(mp->port_num) + i; 1819527a6266SJeff Kirsher u32 v; 1820527a6266SJeff Kirsher 1821527a6266SJeff Kirsher v = 0; 1822527a6266SJeff Kirsher if (nibbles & 1) 1823527a6266SJeff Kirsher v |= 0x00000001; 1824527a6266SJeff Kirsher if (nibbles & 2) 1825527a6266SJeff Kirsher v |= 0x00000100; 1826527a6266SJeff Kirsher if (nibbles & 4) 1827527a6266SJeff Kirsher v |= 0x00010000; 1828527a6266SJeff Kirsher if (nibbles & 8) 1829527a6266SJeff Kirsher v |= 0x01000000; 1830527a6266SJeff Kirsher nibbles >>= 4; 1831527a6266SJeff Kirsher 1832527a6266SJeff Kirsher wrl(mp, off, v); 1833527a6266SJeff Kirsher } 1834527a6266SJeff Kirsher 1835527a6266SJeff Kirsher wrlp(mp, PORT_CONFIG, port_config); 1836527a6266SJeff Kirsher } 1837527a6266SJeff Kirsher 1838527a6266SJeff Kirsher static int addr_crc(unsigned char *addr) 1839527a6266SJeff Kirsher { 1840527a6266SJeff Kirsher int crc = 0; 1841527a6266SJeff Kirsher int i; 1842527a6266SJeff Kirsher 1843527a6266SJeff Kirsher for (i = 0; i < 6; i++) { 1844527a6266SJeff Kirsher int j; 1845527a6266SJeff Kirsher 1846527a6266SJeff Kirsher crc = (crc ^ addr[i]) << 8; 1847527a6266SJeff Kirsher for (j = 7; j >= 0; j--) { 1848527a6266SJeff Kirsher if (crc & (0x100 << j)) 1849527a6266SJeff Kirsher crc ^= 0x107 << j; 1850527a6266SJeff Kirsher } 1851527a6266SJeff Kirsher } 1852527a6266SJeff Kirsher 1853527a6266SJeff Kirsher return crc; 1854527a6266SJeff Kirsher } 1855527a6266SJeff Kirsher 1856527a6266SJeff Kirsher static void mv643xx_eth_program_multicast_filter(struct net_device *dev) 1857527a6266SJeff Kirsher { 1858527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1859527a6266SJeff Kirsher u32 *mc_spec; 1860527a6266SJeff Kirsher u32 *mc_other; 1861527a6266SJeff Kirsher struct netdev_hw_addr *ha; 1862527a6266SJeff Kirsher int i; 1863527a6266SJeff Kirsher 18648b711d6dSJoe Perches if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) 18658b711d6dSJoe Perches goto promiscuous; 1866527a6266SJeff Kirsher 18678b711d6dSJoe Perches /* Allocate both mc_spec and mc_other tables */ 18688b711d6dSJoe Perches mc_spec = kcalloc(128, sizeof(u32), GFP_ATOMIC); 18698b711d6dSJoe Perches if (!mc_spec) 18708b711d6dSJoe Perches goto promiscuous; 18718b711d6dSJoe Perches mc_other = &mc_spec[64]; 1872527a6266SJeff Kirsher 1873527a6266SJeff Kirsher netdev_for_each_mc_addr(ha, dev) { 1874527a6266SJeff Kirsher u8 *a = ha->addr; 1875527a6266SJeff Kirsher u32 *table; 18768b711d6dSJoe Perches u8 entry; 1877527a6266SJeff Kirsher 1878527a6266SJeff Kirsher if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) { 1879527a6266SJeff Kirsher table = mc_spec; 1880527a6266SJeff Kirsher entry = a[5]; 1881527a6266SJeff Kirsher } else { 1882527a6266SJeff Kirsher table = mc_other; 1883527a6266SJeff Kirsher entry = addr_crc(a); 1884527a6266SJeff Kirsher } 1885527a6266SJeff Kirsher 1886527a6266SJeff Kirsher table[entry >> 2] |= 1 << (8 * (entry & 3)); 1887527a6266SJeff Kirsher } 1888527a6266SJeff Kirsher 18898b711d6dSJoe Perches for (i = 0; i < 64; i++) { 18908b711d6dSJoe Perches wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32), 18918b711d6dSJoe Perches mc_spec[i]); 18928b711d6dSJoe Perches wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32), 18938b711d6dSJoe Perches mc_other[i]); 1894527a6266SJeff Kirsher } 1895527a6266SJeff Kirsher 1896527a6266SJeff Kirsher kfree(mc_spec); 18978b711d6dSJoe Perches return; 18988b711d6dSJoe Perches 18998b711d6dSJoe Perches promiscuous: 19008b711d6dSJoe Perches for (i = 0; i < 64; i++) { 19018b711d6dSJoe Perches wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i * sizeof(u32), 19028b711d6dSJoe Perches 0x01010101u); 19038b711d6dSJoe Perches wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i * sizeof(u32), 19048b711d6dSJoe Perches 0x01010101u); 19058b711d6dSJoe Perches } 1906527a6266SJeff Kirsher } 1907527a6266SJeff Kirsher 1908527a6266SJeff Kirsher static void mv643xx_eth_set_rx_mode(struct net_device *dev) 1909527a6266SJeff Kirsher { 1910527a6266SJeff Kirsher mv643xx_eth_program_unicast_filter(dev); 1911527a6266SJeff Kirsher mv643xx_eth_program_multicast_filter(dev); 1912527a6266SJeff Kirsher } 1913527a6266SJeff Kirsher 1914527a6266SJeff Kirsher static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr) 1915527a6266SJeff Kirsher { 1916527a6266SJeff Kirsher struct sockaddr *sa = addr; 1917527a6266SJeff Kirsher 1918527a6266SJeff Kirsher if (!is_valid_ether_addr(sa->sa_data)) 1919504f9b5aSDanny Kukawka return -EADDRNOTAVAIL; 1920527a6266SJeff Kirsher 1921527a6266SJeff Kirsher memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN); 1922527a6266SJeff Kirsher 1923527a6266SJeff Kirsher netif_addr_lock_bh(dev); 1924527a6266SJeff Kirsher mv643xx_eth_program_unicast_filter(dev); 1925527a6266SJeff Kirsher netif_addr_unlock_bh(dev); 1926527a6266SJeff Kirsher 1927527a6266SJeff Kirsher return 0; 1928527a6266SJeff Kirsher } 1929527a6266SJeff Kirsher 1930527a6266SJeff Kirsher 1931527a6266SJeff Kirsher /* rx/tx queue initialisation ***********************************************/ 1932527a6266SJeff Kirsher static int rxq_init(struct mv643xx_eth_private *mp, int index) 1933527a6266SJeff Kirsher { 1934527a6266SJeff Kirsher struct rx_queue *rxq = mp->rxq + index; 1935527a6266SJeff Kirsher struct rx_desc *rx_desc; 1936527a6266SJeff Kirsher int size; 1937527a6266SJeff Kirsher int i; 1938527a6266SJeff Kirsher 1939527a6266SJeff Kirsher rxq->index = index; 1940527a6266SJeff Kirsher 1941527a6266SJeff Kirsher rxq->rx_ring_size = mp->rx_ring_size; 1942527a6266SJeff Kirsher 1943527a6266SJeff Kirsher rxq->rx_desc_count = 0; 1944527a6266SJeff Kirsher rxq->rx_curr_desc = 0; 1945527a6266SJeff Kirsher rxq->rx_used_desc = 0; 1946527a6266SJeff Kirsher 1947527a6266SJeff Kirsher size = rxq->rx_ring_size * sizeof(struct rx_desc); 1948527a6266SJeff Kirsher 1949527a6266SJeff Kirsher if (index == 0 && size <= mp->rx_desc_sram_size) { 1950527a6266SJeff Kirsher rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr, 1951527a6266SJeff Kirsher mp->rx_desc_sram_size); 1952527a6266SJeff Kirsher rxq->rx_desc_dma = mp->rx_desc_sram_addr; 1953527a6266SJeff Kirsher } else { 1954527a6266SJeff Kirsher rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent, 1955527a6266SJeff Kirsher size, &rxq->rx_desc_dma, 1956527a6266SJeff Kirsher GFP_KERNEL); 1957527a6266SJeff Kirsher } 1958527a6266SJeff Kirsher 1959527a6266SJeff Kirsher if (rxq->rx_desc_area == NULL) { 1960527a6266SJeff Kirsher netdev_err(mp->dev, 1961527a6266SJeff Kirsher "can't allocate rx ring (%d bytes)\n", size); 1962527a6266SJeff Kirsher goto out; 1963527a6266SJeff Kirsher } 1964527a6266SJeff Kirsher memset(rxq->rx_desc_area, 0, size); 1965527a6266SJeff Kirsher 1966527a6266SJeff Kirsher rxq->rx_desc_area_size = size; 19679fa8e980SLubomir Rintel rxq->rx_skb = kcalloc(rxq->rx_ring_size, sizeof(*rxq->rx_skb), 1968527a6266SJeff Kirsher GFP_KERNEL); 1969b2adaca9SJoe Perches if (rxq->rx_skb == NULL) 1970527a6266SJeff Kirsher goto out_free; 1971527a6266SJeff Kirsher 197264699336SJoe Perches rx_desc = rxq->rx_desc_area; 1973527a6266SJeff Kirsher for (i = 0; i < rxq->rx_ring_size; i++) { 1974527a6266SJeff Kirsher int nexti; 1975527a6266SJeff Kirsher 1976527a6266SJeff Kirsher nexti = i + 1; 1977527a6266SJeff Kirsher if (nexti == rxq->rx_ring_size) 1978527a6266SJeff Kirsher nexti = 0; 1979527a6266SJeff Kirsher 1980527a6266SJeff Kirsher rx_desc[i].next_desc_ptr = rxq->rx_desc_dma + 1981527a6266SJeff Kirsher nexti * sizeof(struct rx_desc); 1982527a6266SJeff Kirsher } 1983527a6266SJeff Kirsher 1984527a6266SJeff Kirsher return 0; 1985527a6266SJeff Kirsher 1986527a6266SJeff Kirsher 1987527a6266SJeff Kirsher out_free: 1988527a6266SJeff Kirsher if (index == 0 && size <= mp->rx_desc_sram_size) 1989527a6266SJeff Kirsher iounmap(rxq->rx_desc_area); 1990527a6266SJeff Kirsher else 1991527a6266SJeff Kirsher dma_free_coherent(mp->dev->dev.parent, size, 1992527a6266SJeff Kirsher rxq->rx_desc_area, 1993527a6266SJeff Kirsher rxq->rx_desc_dma); 1994527a6266SJeff Kirsher 1995527a6266SJeff Kirsher out: 1996527a6266SJeff Kirsher return -ENOMEM; 1997527a6266SJeff Kirsher } 1998527a6266SJeff Kirsher 1999527a6266SJeff Kirsher static void rxq_deinit(struct rx_queue *rxq) 2000527a6266SJeff Kirsher { 2001527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 2002527a6266SJeff Kirsher int i; 2003527a6266SJeff Kirsher 2004527a6266SJeff Kirsher rxq_disable(rxq); 2005527a6266SJeff Kirsher 2006527a6266SJeff Kirsher for (i = 0; i < rxq->rx_ring_size; i++) { 2007527a6266SJeff Kirsher if (rxq->rx_skb[i]) { 200843cee2d2SFlorian Fainelli dev_consume_skb_any(rxq->rx_skb[i]); 2009527a6266SJeff Kirsher rxq->rx_desc_count--; 2010527a6266SJeff Kirsher } 2011527a6266SJeff Kirsher } 2012527a6266SJeff Kirsher 2013527a6266SJeff Kirsher if (rxq->rx_desc_count) { 2014527a6266SJeff Kirsher netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n", 2015527a6266SJeff Kirsher rxq->rx_desc_count); 2016527a6266SJeff Kirsher } 2017527a6266SJeff Kirsher 2018527a6266SJeff Kirsher if (rxq->index == 0 && 2019527a6266SJeff Kirsher rxq->rx_desc_area_size <= mp->rx_desc_sram_size) 2020527a6266SJeff Kirsher iounmap(rxq->rx_desc_area); 2021527a6266SJeff Kirsher else 2022527a6266SJeff Kirsher dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size, 2023527a6266SJeff Kirsher rxq->rx_desc_area, rxq->rx_desc_dma); 2024527a6266SJeff Kirsher 2025527a6266SJeff Kirsher kfree(rxq->rx_skb); 2026527a6266SJeff Kirsher } 2027527a6266SJeff Kirsher 2028527a6266SJeff Kirsher static int txq_init(struct mv643xx_eth_private *mp, int index) 2029527a6266SJeff Kirsher { 2030527a6266SJeff Kirsher struct tx_queue *txq = mp->txq + index; 2031527a6266SJeff Kirsher struct tx_desc *tx_desc; 2032527a6266SJeff Kirsher int size; 20339e911414SEzequiel Garcia int ret; 2034527a6266SJeff Kirsher int i; 2035527a6266SJeff Kirsher 2036527a6266SJeff Kirsher txq->index = index; 2037527a6266SJeff Kirsher 2038527a6266SJeff Kirsher txq->tx_ring_size = mp->tx_ring_size; 2039527a6266SJeff Kirsher 2040ee9e4956SEzequiel Garcia /* A queue must always have room for at least one skb. 2041ee9e4956SEzequiel Garcia * Therefore, stop the queue when the free entries reaches 2042ee9e4956SEzequiel Garcia * the maximum number of descriptors per skb. 2043ee9e4956SEzequiel Garcia */ 2044ee9e4956SEzequiel Garcia txq->tx_stop_threshold = txq->tx_ring_size - MV643XX_MAX_SKB_DESCS; 2045ee9e4956SEzequiel Garcia txq->tx_wake_threshold = txq->tx_stop_threshold / 2; 2046ee9e4956SEzequiel Garcia 2047527a6266SJeff Kirsher txq->tx_desc_count = 0; 2048527a6266SJeff Kirsher txq->tx_curr_desc = 0; 2049527a6266SJeff Kirsher txq->tx_used_desc = 0; 2050527a6266SJeff Kirsher 2051527a6266SJeff Kirsher size = txq->tx_ring_size * sizeof(struct tx_desc); 2052527a6266SJeff Kirsher 2053527a6266SJeff Kirsher if (index == 0 && size <= mp->tx_desc_sram_size) { 2054527a6266SJeff Kirsher txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr, 2055527a6266SJeff Kirsher mp->tx_desc_sram_size); 2056527a6266SJeff Kirsher txq->tx_desc_dma = mp->tx_desc_sram_addr; 2057527a6266SJeff Kirsher } else { 2058527a6266SJeff Kirsher txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent, 2059527a6266SJeff Kirsher size, &txq->tx_desc_dma, 2060527a6266SJeff Kirsher GFP_KERNEL); 2061527a6266SJeff Kirsher } 2062527a6266SJeff Kirsher 2063527a6266SJeff Kirsher if (txq->tx_desc_area == NULL) { 2064527a6266SJeff Kirsher netdev_err(mp->dev, 2065527a6266SJeff Kirsher "can't allocate tx ring (%d bytes)\n", size); 2066527a6266SJeff Kirsher return -ENOMEM; 2067527a6266SJeff Kirsher } 2068527a6266SJeff Kirsher memset(txq->tx_desc_area, 0, size); 2069527a6266SJeff Kirsher 2070527a6266SJeff Kirsher txq->tx_desc_area_size = size; 2071527a6266SJeff Kirsher 207264699336SJoe Perches tx_desc = txq->tx_desc_area; 2073527a6266SJeff Kirsher for (i = 0; i < txq->tx_ring_size; i++) { 2074527a6266SJeff Kirsher struct tx_desc *txd = tx_desc + i; 2075527a6266SJeff Kirsher int nexti; 2076527a6266SJeff Kirsher 2077527a6266SJeff Kirsher nexti = i + 1; 2078527a6266SJeff Kirsher if (nexti == txq->tx_ring_size) 2079527a6266SJeff Kirsher nexti = 0; 2080527a6266SJeff Kirsher 2081527a6266SJeff Kirsher txd->cmd_sts = 0; 2082527a6266SJeff Kirsher txd->next_desc_ptr = txq->tx_desc_dma + 2083527a6266SJeff Kirsher nexti * sizeof(struct tx_desc); 2084527a6266SJeff Kirsher } 2085527a6266SJeff Kirsher 20869e911414SEzequiel Garcia txq->tx_desc_mapping = kcalloc(txq->tx_ring_size, sizeof(char), 20879e911414SEzequiel Garcia GFP_KERNEL); 20889e911414SEzequiel Garcia if (!txq->tx_desc_mapping) { 20899e911414SEzequiel Garcia ret = -ENOMEM; 20909e911414SEzequiel Garcia goto err_free_desc_area; 20919e911414SEzequiel Garcia } 20929e911414SEzequiel Garcia 20933ae8f4e0SEzequiel Garcia /* Allocate DMA buffers for TSO MAC/IP/TCP headers */ 20943ae8f4e0SEzequiel Garcia txq->tso_hdrs = dma_alloc_coherent(mp->dev->dev.parent, 20953ae8f4e0SEzequiel Garcia txq->tx_ring_size * TSO_HEADER_SIZE, 20963ae8f4e0SEzequiel Garcia &txq->tso_hdrs_dma, GFP_KERNEL); 20973ae8f4e0SEzequiel Garcia if (txq->tso_hdrs == NULL) { 20989e911414SEzequiel Garcia ret = -ENOMEM; 20999e911414SEzequiel Garcia goto err_free_desc_mapping; 21003ae8f4e0SEzequiel Garcia } 2101527a6266SJeff Kirsher skb_queue_head_init(&txq->tx_skb); 2102527a6266SJeff Kirsher 2103527a6266SJeff Kirsher return 0; 21049e911414SEzequiel Garcia 21059e911414SEzequiel Garcia err_free_desc_mapping: 21069e911414SEzequiel Garcia kfree(txq->tx_desc_mapping); 21079e911414SEzequiel Garcia err_free_desc_area: 21089e911414SEzequiel Garcia if (index == 0 && size <= mp->tx_desc_sram_size) 21099e911414SEzequiel Garcia iounmap(txq->tx_desc_area); 21109e911414SEzequiel Garcia else 21119e911414SEzequiel Garcia dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size, 21129e911414SEzequiel Garcia txq->tx_desc_area, txq->tx_desc_dma); 21139e911414SEzequiel Garcia return ret; 2114527a6266SJeff Kirsher } 2115527a6266SJeff Kirsher 2116527a6266SJeff Kirsher static void txq_deinit(struct tx_queue *txq) 2117527a6266SJeff Kirsher { 2118527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 2119527a6266SJeff Kirsher 2120527a6266SJeff Kirsher txq_disable(txq); 2121527a6266SJeff Kirsher txq_reclaim(txq, txq->tx_ring_size, 1); 2122527a6266SJeff Kirsher 2123527a6266SJeff Kirsher BUG_ON(txq->tx_used_desc != txq->tx_curr_desc); 2124527a6266SJeff Kirsher 2125527a6266SJeff Kirsher if (txq->index == 0 && 2126527a6266SJeff Kirsher txq->tx_desc_area_size <= mp->tx_desc_sram_size) 2127527a6266SJeff Kirsher iounmap(txq->tx_desc_area); 2128527a6266SJeff Kirsher else 2129527a6266SJeff Kirsher dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size, 2130527a6266SJeff Kirsher txq->tx_desc_area, txq->tx_desc_dma); 21319e911414SEzequiel Garcia kfree(txq->tx_desc_mapping); 21329e911414SEzequiel Garcia 21333ae8f4e0SEzequiel Garcia if (txq->tso_hdrs) 21343ae8f4e0SEzequiel Garcia dma_free_coherent(mp->dev->dev.parent, 21353ae8f4e0SEzequiel Garcia txq->tx_ring_size * TSO_HEADER_SIZE, 21363ae8f4e0SEzequiel Garcia txq->tso_hdrs, txq->tso_hdrs_dma); 2137527a6266SJeff Kirsher } 2138527a6266SJeff Kirsher 2139527a6266SJeff Kirsher 2140527a6266SJeff Kirsher /* netdev ops and related ***************************************************/ 2141527a6266SJeff Kirsher static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp) 2142527a6266SJeff Kirsher { 2143527a6266SJeff Kirsher u32 int_cause; 2144527a6266SJeff Kirsher u32 int_cause_ext; 2145527a6266SJeff Kirsher 2146527a6266SJeff Kirsher int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask; 2147527a6266SJeff Kirsher if (int_cause == 0) 2148527a6266SJeff Kirsher return 0; 2149527a6266SJeff Kirsher 2150527a6266SJeff Kirsher int_cause_ext = 0; 2151527a6266SJeff Kirsher if (int_cause & INT_EXT) { 2152527a6266SJeff Kirsher int_cause &= ~INT_EXT; 2153527a6266SJeff Kirsher int_cause_ext = rdlp(mp, INT_CAUSE_EXT); 2154527a6266SJeff Kirsher } 2155527a6266SJeff Kirsher 2156527a6266SJeff Kirsher if (int_cause) { 2157527a6266SJeff Kirsher wrlp(mp, INT_CAUSE, ~int_cause); 2158527a6266SJeff Kirsher mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) & 2159527a6266SJeff Kirsher ~(rdlp(mp, TXQ_COMMAND) & 0xff); 2160527a6266SJeff Kirsher mp->work_rx |= (int_cause & INT_RX) >> 2; 2161527a6266SJeff Kirsher } 2162527a6266SJeff Kirsher 2163527a6266SJeff Kirsher int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX; 2164527a6266SJeff Kirsher if (int_cause_ext) { 2165527a6266SJeff Kirsher wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext); 2166527a6266SJeff Kirsher if (int_cause_ext & INT_EXT_LINK_PHY) 2167527a6266SJeff Kirsher mp->work_link = 1; 2168527a6266SJeff Kirsher mp->work_tx |= int_cause_ext & INT_EXT_TX; 2169527a6266SJeff Kirsher } 2170527a6266SJeff Kirsher 2171527a6266SJeff Kirsher return 1; 2172527a6266SJeff Kirsher } 2173527a6266SJeff Kirsher 2174527a6266SJeff Kirsher static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id) 2175527a6266SJeff Kirsher { 2176527a6266SJeff Kirsher struct net_device *dev = (struct net_device *)dev_id; 2177527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2178527a6266SJeff Kirsher 2179527a6266SJeff Kirsher if (unlikely(!mv643xx_eth_collect_events(mp))) 2180527a6266SJeff Kirsher return IRQ_NONE; 2181527a6266SJeff Kirsher 2182527a6266SJeff Kirsher wrlp(mp, INT_MASK, 0); 2183527a6266SJeff Kirsher napi_schedule(&mp->napi); 2184527a6266SJeff Kirsher 2185527a6266SJeff Kirsher return IRQ_HANDLED; 2186527a6266SJeff Kirsher } 2187527a6266SJeff Kirsher 2188527a6266SJeff Kirsher static void handle_link_event(struct mv643xx_eth_private *mp) 2189527a6266SJeff Kirsher { 2190527a6266SJeff Kirsher struct net_device *dev = mp->dev; 2191527a6266SJeff Kirsher u32 port_status; 2192527a6266SJeff Kirsher int speed; 2193527a6266SJeff Kirsher int duplex; 2194527a6266SJeff Kirsher int fc; 2195527a6266SJeff Kirsher 2196527a6266SJeff Kirsher port_status = rdlp(mp, PORT_STATUS); 2197527a6266SJeff Kirsher if (!(port_status & LINK_UP)) { 2198527a6266SJeff Kirsher if (netif_carrier_ok(dev)) { 2199527a6266SJeff Kirsher int i; 2200527a6266SJeff Kirsher 2201527a6266SJeff Kirsher netdev_info(dev, "link down\n"); 2202527a6266SJeff Kirsher 2203527a6266SJeff Kirsher netif_carrier_off(dev); 2204527a6266SJeff Kirsher 2205527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) { 2206527a6266SJeff Kirsher struct tx_queue *txq = mp->txq + i; 2207527a6266SJeff Kirsher 2208527a6266SJeff Kirsher txq_reclaim(txq, txq->tx_ring_size, 1); 2209527a6266SJeff Kirsher txq_reset_hw_ptr(txq); 2210527a6266SJeff Kirsher } 2211527a6266SJeff Kirsher } 2212527a6266SJeff Kirsher return; 2213527a6266SJeff Kirsher } 2214527a6266SJeff Kirsher 2215527a6266SJeff Kirsher switch (port_status & PORT_SPEED_MASK) { 2216527a6266SJeff Kirsher case PORT_SPEED_10: 2217527a6266SJeff Kirsher speed = 10; 2218527a6266SJeff Kirsher break; 2219527a6266SJeff Kirsher case PORT_SPEED_100: 2220527a6266SJeff Kirsher speed = 100; 2221527a6266SJeff Kirsher break; 2222527a6266SJeff Kirsher case PORT_SPEED_1000: 2223527a6266SJeff Kirsher speed = 1000; 2224527a6266SJeff Kirsher break; 2225527a6266SJeff Kirsher default: 2226527a6266SJeff Kirsher speed = -1; 2227527a6266SJeff Kirsher break; 2228527a6266SJeff Kirsher } 2229527a6266SJeff Kirsher duplex = (port_status & FULL_DUPLEX) ? 1 : 0; 2230527a6266SJeff Kirsher fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0; 2231527a6266SJeff Kirsher 2232527a6266SJeff Kirsher netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n", 2233527a6266SJeff Kirsher speed, duplex ? "full" : "half", fc ? "en" : "dis"); 2234527a6266SJeff Kirsher 2235527a6266SJeff Kirsher if (!netif_carrier_ok(dev)) 2236527a6266SJeff Kirsher netif_carrier_on(dev); 2237527a6266SJeff Kirsher } 2238527a6266SJeff Kirsher 2239527a6266SJeff Kirsher static int mv643xx_eth_poll(struct napi_struct *napi, int budget) 2240527a6266SJeff Kirsher { 2241527a6266SJeff Kirsher struct mv643xx_eth_private *mp; 2242527a6266SJeff Kirsher int work_done; 2243527a6266SJeff Kirsher 2244527a6266SJeff Kirsher mp = container_of(napi, struct mv643xx_eth_private, napi); 2245527a6266SJeff Kirsher 2246527a6266SJeff Kirsher if (unlikely(mp->oom)) { 2247527a6266SJeff Kirsher mp->oom = 0; 2248527a6266SJeff Kirsher del_timer(&mp->rx_oom); 2249527a6266SJeff Kirsher } 2250527a6266SJeff Kirsher 2251527a6266SJeff Kirsher work_done = 0; 2252527a6266SJeff Kirsher while (work_done < budget) { 2253527a6266SJeff Kirsher u8 queue_mask; 2254527a6266SJeff Kirsher int queue; 2255527a6266SJeff Kirsher int work_tbd; 2256527a6266SJeff Kirsher 2257527a6266SJeff Kirsher if (mp->work_link) { 2258527a6266SJeff Kirsher mp->work_link = 0; 2259527a6266SJeff Kirsher handle_link_event(mp); 2260527a6266SJeff Kirsher work_done++; 2261527a6266SJeff Kirsher continue; 2262527a6266SJeff Kirsher } 2263527a6266SJeff Kirsher 2264527a6266SJeff Kirsher queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx; 2265527a6266SJeff Kirsher if (likely(!mp->oom)) 2266527a6266SJeff Kirsher queue_mask |= mp->work_rx_refill; 2267527a6266SJeff Kirsher 2268527a6266SJeff Kirsher if (!queue_mask) { 2269527a6266SJeff Kirsher if (mv643xx_eth_collect_events(mp)) 2270527a6266SJeff Kirsher continue; 2271527a6266SJeff Kirsher break; 2272527a6266SJeff Kirsher } 2273527a6266SJeff Kirsher 2274527a6266SJeff Kirsher queue = fls(queue_mask) - 1; 2275527a6266SJeff Kirsher queue_mask = 1 << queue; 2276527a6266SJeff Kirsher 2277527a6266SJeff Kirsher work_tbd = budget - work_done; 2278527a6266SJeff Kirsher if (work_tbd > 16) 2279527a6266SJeff Kirsher work_tbd = 16; 2280527a6266SJeff Kirsher 2281527a6266SJeff Kirsher if (mp->work_tx_end & queue_mask) { 2282527a6266SJeff Kirsher txq_kick(mp->txq + queue); 2283527a6266SJeff Kirsher } else if (mp->work_tx & queue_mask) { 2284527a6266SJeff Kirsher work_done += txq_reclaim(mp->txq + queue, work_tbd, 0); 2285527a6266SJeff Kirsher txq_maybe_wake(mp->txq + queue); 2286527a6266SJeff Kirsher } else if (mp->work_rx & queue_mask) { 2287527a6266SJeff Kirsher work_done += rxq_process(mp->rxq + queue, work_tbd); 2288527a6266SJeff Kirsher } else if (!mp->oom && (mp->work_rx_refill & queue_mask)) { 2289527a6266SJeff Kirsher work_done += rxq_refill(mp->rxq + queue, work_tbd); 2290527a6266SJeff Kirsher } else { 2291527a6266SJeff Kirsher BUG(); 2292527a6266SJeff Kirsher } 2293527a6266SJeff Kirsher } 2294527a6266SJeff Kirsher 2295527a6266SJeff Kirsher if (work_done < budget) { 2296527a6266SJeff Kirsher if (mp->oom) 2297527a6266SJeff Kirsher mod_timer(&mp->rx_oom, jiffies + (HZ / 10)); 22986ad20165SEric Dumazet napi_complete_done(napi, work_done); 2299527a6266SJeff Kirsher wrlp(mp, INT_MASK, mp->int_mask); 2300527a6266SJeff Kirsher } 2301527a6266SJeff Kirsher 2302527a6266SJeff Kirsher return work_done; 2303527a6266SJeff Kirsher } 2304527a6266SJeff Kirsher 2305e99e88a9SKees Cook static inline void oom_timer_wrapper(struct timer_list *t) 2306527a6266SJeff Kirsher { 2307e99e88a9SKees Cook struct mv643xx_eth_private *mp = from_timer(mp, t, rx_oom); 2308527a6266SJeff Kirsher 2309527a6266SJeff Kirsher napi_schedule(&mp->napi); 2310527a6266SJeff Kirsher } 2311527a6266SJeff Kirsher 2312527a6266SJeff Kirsher static void port_start(struct mv643xx_eth_private *mp) 2313527a6266SJeff Kirsher { 23141e8a655dSPhilippe Reynes struct net_device *dev = mp->dev; 2315527a6266SJeff Kirsher u32 pscr; 2316527a6266SJeff Kirsher int i; 2317527a6266SJeff Kirsher 2318527a6266SJeff Kirsher /* 2319527a6266SJeff Kirsher * Perform PHY reset, if there is a PHY. 2320527a6266SJeff Kirsher */ 23211e8a655dSPhilippe Reynes if (dev->phydev) { 2322a54e1612SPhilippe Reynes struct ethtool_link_ksettings cmd; 2323527a6266SJeff Kirsher 2324a54e1612SPhilippe Reynes mv643xx_eth_get_link_ksettings(dev, &cmd); 23251e8a655dSPhilippe Reynes phy_init_hw(dev->phydev); 2326a54e1612SPhilippe Reynes mv643xx_eth_set_link_ksettings( 2327a54e1612SPhilippe Reynes dev, (const struct ethtool_link_ksettings *)&cmd); 23281e8a655dSPhilippe Reynes phy_start(dev->phydev); 2329527a6266SJeff Kirsher } 2330527a6266SJeff Kirsher 2331527a6266SJeff Kirsher /* 2332527a6266SJeff Kirsher * Configure basic link parameters. 2333527a6266SJeff Kirsher */ 2334527a6266SJeff Kirsher pscr = rdlp(mp, PORT_SERIAL_CONTROL); 2335527a6266SJeff Kirsher 2336527a6266SJeff Kirsher pscr |= SERIAL_PORT_ENABLE; 2337527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, pscr); 2338527a6266SJeff Kirsher 2339527a6266SJeff Kirsher pscr |= DO_NOT_FORCE_LINK_FAIL; 23401e8a655dSPhilippe Reynes if (!dev->phydev) 2341527a6266SJeff Kirsher pscr |= FORCE_LINK_PASS; 2342527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, pscr); 2343527a6266SJeff Kirsher 2344527a6266SJeff Kirsher /* 2345527a6266SJeff Kirsher * Configure TX path and queues. 2346527a6266SJeff Kirsher */ 2347527a6266SJeff Kirsher tx_set_rate(mp, 1000000000, 16777216); 2348527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) { 2349527a6266SJeff Kirsher struct tx_queue *txq = mp->txq + i; 2350527a6266SJeff Kirsher 2351527a6266SJeff Kirsher txq_reset_hw_ptr(txq); 2352527a6266SJeff Kirsher txq_set_rate(txq, 1000000000, 16777216); 2353527a6266SJeff Kirsher txq_set_fixed_prio_mode(txq); 2354527a6266SJeff Kirsher } 2355527a6266SJeff Kirsher 2356527a6266SJeff Kirsher /* 2357527a6266SJeff Kirsher * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast 2358527a6266SJeff Kirsher * frames to RX queue #0, and include the pseudo-header when 2359527a6266SJeff Kirsher * calculating receive checksums. 2360527a6266SJeff Kirsher */ 2361527a6266SJeff Kirsher mv643xx_eth_set_features(mp->dev, mp->dev->features); 2362527a6266SJeff Kirsher 2363527a6266SJeff Kirsher /* 2364527a6266SJeff Kirsher * Treat BPDUs as normal multicasts, and disable partition mode. 2365527a6266SJeff Kirsher */ 2366527a6266SJeff Kirsher wrlp(mp, PORT_CONFIG_EXT, 0x00000000); 2367527a6266SJeff Kirsher 2368527a6266SJeff Kirsher /* 2369527a6266SJeff Kirsher * Add configured unicast addresses to address filter table. 2370527a6266SJeff Kirsher */ 2371527a6266SJeff Kirsher mv643xx_eth_program_unicast_filter(mp->dev); 2372527a6266SJeff Kirsher 2373527a6266SJeff Kirsher /* 2374527a6266SJeff Kirsher * Enable the receive queues. 2375527a6266SJeff Kirsher */ 2376527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) { 2377527a6266SJeff Kirsher struct rx_queue *rxq = mp->rxq + i; 2378527a6266SJeff Kirsher u32 addr; 2379527a6266SJeff Kirsher 2380527a6266SJeff Kirsher addr = (u32)rxq->rx_desc_dma; 2381527a6266SJeff Kirsher addr += rxq->rx_curr_desc * sizeof(struct rx_desc); 2382527a6266SJeff Kirsher wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr); 2383527a6266SJeff Kirsher 2384527a6266SJeff Kirsher rxq_enable(rxq); 2385527a6266SJeff Kirsher } 2386527a6266SJeff Kirsher } 2387527a6266SJeff Kirsher 2388527a6266SJeff Kirsher static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp) 2389527a6266SJeff Kirsher { 2390527a6266SJeff Kirsher int skb_size; 2391527a6266SJeff Kirsher 2392527a6266SJeff Kirsher /* 2393527a6266SJeff Kirsher * Reserve 2+14 bytes for an ethernet header (the hardware 2394527a6266SJeff Kirsher * automatically prepends 2 bytes of dummy data to each 2395527a6266SJeff Kirsher * received packet), 16 bytes for up to four VLAN tags, and 2396527a6266SJeff Kirsher * 4 bytes for the trailing FCS -- 36 bytes total. 2397527a6266SJeff Kirsher */ 2398527a6266SJeff Kirsher skb_size = mp->dev->mtu + 36; 2399527a6266SJeff Kirsher 2400527a6266SJeff Kirsher /* 2401527a6266SJeff Kirsher * Make sure that the skb size is a multiple of 8 bytes, as 2402527a6266SJeff Kirsher * the lower three bits of the receive descriptor's buffer 2403527a6266SJeff Kirsher * size field are ignored by the hardware. 2404527a6266SJeff Kirsher */ 2405527a6266SJeff Kirsher mp->skb_size = (skb_size + 7) & ~7; 2406527a6266SJeff Kirsher 2407527a6266SJeff Kirsher /* 2408527a6266SJeff Kirsher * If NET_SKB_PAD is smaller than a cache line, 2409527a6266SJeff Kirsher * netdev_alloc_skb() will cause skb->data to be misaligned 2410527a6266SJeff Kirsher * to a cache line boundary. If this is the case, include 2411527a6266SJeff Kirsher * some extra space to allow re-aligning the data area. 2412527a6266SJeff Kirsher */ 2413527a6266SJeff Kirsher mp->skb_size += SKB_DMA_REALIGN; 2414527a6266SJeff Kirsher } 2415527a6266SJeff Kirsher 2416527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev) 2417527a6266SJeff Kirsher { 2418527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2419527a6266SJeff Kirsher int err; 2420527a6266SJeff Kirsher int i; 2421527a6266SJeff Kirsher 2422527a6266SJeff Kirsher wrlp(mp, INT_CAUSE, 0); 2423527a6266SJeff Kirsher wrlp(mp, INT_CAUSE_EXT, 0); 2424527a6266SJeff Kirsher rdlp(mp, INT_CAUSE_EXT); 2425527a6266SJeff Kirsher 2426527a6266SJeff Kirsher err = request_irq(dev->irq, mv643xx_eth_irq, 2427527a6266SJeff Kirsher IRQF_SHARED, dev->name, dev); 2428527a6266SJeff Kirsher if (err) { 2429527a6266SJeff Kirsher netdev_err(dev, "can't assign irq\n"); 2430527a6266SJeff Kirsher return -EAGAIN; 2431527a6266SJeff Kirsher } 2432527a6266SJeff Kirsher 2433527a6266SJeff Kirsher mv643xx_eth_recalc_skb_size(mp); 2434527a6266SJeff Kirsher 2435527a6266SJeff Kirsher napi_enable(&mp->napi); 2436527a6266SJeff Kirsher 2437527a6266SJeff Kirsher mp->int_mask = INT_EXT; 2438527a6266SJeff Kirsher 2439527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) { 2440527a6266SJeff Kirsher err = rxq_init(mp, i); 2441527a6266SJeff Kirsher if (err) { 2442527a6266SJeff Kirsher while (--i >= 0) 2443527a6266SJeff Kirsher rxq_deinit(mp->rxq + i); 2444527a6266SJeff Kirsher goto out; 2445527a6266SJeff Kirsher } 2446527a6266SJeff Kirsher 2447527a6266SJeff Kirsher rxq_refill(mp->rxq + i, INT_MAX); 2448527a6266SJeff Kirsher mp->int_mask |= INT_RX_0 << i; 2449527a6266SJeff Kirsher } 2450527a6266SJeff Kirsher 2451527a6266SJeff Kirsher if (mp->oom) { 2452527a6266SJeff Kirsher mp->rx_oom.expires = jiffies + (HZ / 10); 2453527a6266SJeff Kirsher add_timer(&mp->rx_oom); 2454527a6266SJeff Kirsher } 2455527a6266SJeff Kirsher 2456527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) { 2457527a6266SJeff Kirsher err = txq_init(mp, i); 2458527a6266SJeff Kirsher if (err) { 2459527a6266SJeff Kirsher while (--i >= 0) 2460527a6266SJeff Kirsher txq_deinit(mp->txq + i); 2461527a6266SJeff Kirsher goto out_free; 2462527a6266SJeff Kirsher } 2463527a6266SJeff Kirsher mp->int_mask |= INT_TX_END_0 << i; 2464527a6266SJeff Kirsher } 2465527a6266SJeff Kirsher 2466f564412cSSebastian Hesselbarth add_timer(&mp->mib_counters_timer); 2467527a6266SJeff Kirsher port_start(mp); 2468527a6266SJeff Kirsher 2469527a6266SJeff Kirsher wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX); 2470527a6266SJeff Kirsher wrlp(mp, INT_MASK, mp->int_mask); 2471527a6266SJeff Kirsher 2472527a6266SJeff Kirsher return 0; 2473527a6266SJeff Kirsher 2474527a6266SJeff Kirsher 2475527a6266SJeff Kirsher out_free: 2476527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) 2477527a6266SJeff Kirsher rxq_deinit(mp->rxq + i); 2478527a6266SJeff Kirsher out: 2479527a6266SJeff Kirsher free_irq(dev->irq, dev); 2480527a6266SJeff Kirsher 2481527a6266SJeff Kirsher return err; 2482527a6266SJeff Kirsher } 2483527a6266SJeff Kirsher 2484527a6266SJeff Kirsher static void port_reset(struct mv643xx_eth_private *mp) 2485527a6266SJeff Kirsher { 2486527a6266SJeff Kirsher unsigned int data; 2487527a6266SJeff Kirsher int i; 2488527a6266SJeff Kirsher 2489527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) 2490527a6266SJeff Kirsher rxq_disable(mp->rxq + i); 2491527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) 2492527a6266SJeff Kirsher txq_disable(mp->txq + i); 2493527a6266SJeff Kirsher 2494527a6266SJeff Kirsher while (1) { 2495527a6266SJeff Kirsher u32 ps = rdlp(mp, PORT_STATUS); 2496527a6266SJeff Kirsher 2497527a6266SJeff Kirsher if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY) 2498527a6266SJeff Kirsher break; 2499527a6266SJeff Kirsher udelay(10); 2500527a6266SJeff Kirsher } 2501527a6266SJeff Kirsher 2502527a6266SJeff Kirsher /* Reset the Enable bit in the Configuration Register */ 2503527a6266SJeff Kirsher data = rdlp(mp, PORT_SERIAL_CONTROL); 2504527a6266SJeff Kirsher data &= ~(SERIAL_PORT_ENABLE | 2505527a6266SJeff Kirsher DO_NOT_FORCE_LINK_FAIL | 2506527a6266SJeff Kirsher FORCE_LINK_PASS); 2507527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, data); 2508527a6266SJeff Kirsher } 2509527a6266SJeff Kirsher 2510527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev) 2511527a6266SJeff Kirsher { 2512527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2513527a6266SJeff Kirsher int i; 2514527a6266SJeff Kirsher 2515527a6266SJeff Kirsher wrlp(mp, INT_MASK_EXT, 0x00000000); 2516527a6266SJeff Kirsher wrlp(mp, INT_MASK, 0x00000000); 2517527a6266SJeff Kirsher rdlp(mp, INT_MASK); 2518527a6266SJeff Kirsher 2519527a6266SJeff Kirsher napi_disable(&mp->napi); 2520527a6266SJeff Kirsher 2521527a6266SJeff Kirsher del_timer_sync(&mp->rx_oom); 2522527a6266SJeff Kirsher 2523527a6266SJeff Kirsher netif_carrier_off(dev); 25241e8a655dSPhilippe Reynes if (dev->phydev) 25251e8a655dSPhilippe Reynes phy_stop(dev->phydev); 2526527a6266SJeff Kirsher free_irq(dev->irq, dev); 2527527a6266SJeff Kirsher 2528527a6266SJeff Kirsher port_reset(mp); 2529527a6266SJeff Kirsher mv643xx_eth_get_stats(dev); 2530527a6266SJeff Kirsher mib_counters_update(mp); 2531527a6266SJeff Kirsher del_timer_sync(&mp->mib_counters_timer); 2532527a6266SJeff Kirsher 2533527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) 2534527a6266SJeff Kirsher rxq_deinit(mp->rxq + i); 2535527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) 2536527a6266SJeff Kirsher txq_deinit(mp->txq + i); 2537527a6266SJeff Kirsher 2538527a6266SJeff Kirsher return 0; 2539527a6266SJeff Kirsher } 2540527a6266SJeff Kirsher 2541527a6266SJeff Kirsher static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 2542527a6266SJeff Kirsher { 2543260055bbSPhil Sutter int ret; 2544527a6266SJeff Kirsher 25451e8a655dSPhilippe Reynes if (!dev->phydev) 2546260055bbSPhil Sutter return -ENOTSUPP; 2547527a6266SJeff Kirsher 25481e8a655dSPhilippe Reynes ret = phy_mii_ioctl(dev->phydev, ifr, cmd); 2549260055bbSPhil Sutter if (!ret) 25500a9e413bSEzequiel Garcia mv643xx_eth_adjust_link(dev); 2551260055bbSPhil Sutter return ret; 2552527a6266SJeff Kirsher } 2553527a6266SJeff Kirsher 2554527a6266SJeff Kirsher static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu) 2555527a6266SJeff Kirsher { 2556527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2557527a6266SJeff Kirsher 2558527a6266SJeff Kirsher dev->mtu = new_mtu; 2559527a6266SJeff Kirsher mv643xx_eth_recalc_skb_size(mp); 2560527a6266SJeff Kirsher tx_set_rate(mp, 1000000000, 16777216); 2561527a6266SJeff Kirsher 2562527a6266SJeff Kirsher if (!netif_running(dev)) 2563527a6266SJeff Kirsher return 0; 2564527a6266SJeff Kirsher 2565527a6266SJeff Kirsher /* 2566527a6266SJeff Kirsher * Stop and then re-open the interface. This will allocate RX 2567527a6266SJeff Kirsher * skbs of the new MTU. 2568527a6266SJeff Kirsher * There is a possible danger that the open will not succeed, 2569527a6266SJeff Kirsher * due to memory being full. 2570527a6266SJeff Kirsher */ 2571527a6266SJeff Kirsher mv643xx_eth_stop(dev); 2572527a6266SJeff Kirsher if (mv643xx_eth_open(dev)) { 2573527a6266SJeff Kirsher netdev_err(dev, 2574527a6266SJeff Kirsher "fatal error on re-opening device after MTU change\n"); 2575527a6266SJeff Kirsher } 2576527a6266SJeff Kirsher 2577527a6266SJeff Kirsher return 0; 2578527a6266SJeff Kirsher } 2579527a6266SJeff Kirsher 2580527a6266SJeff Kirsher static void tx_timeout_task(struct work_struct *ugly) 2581527a6266SJeff Kirsher { 2582527a6266SJeff Kirsher struct mv643xx_eth_private *mp; 2583527a6266SJeff Kirsher 2584527a6266SJeff Kirsher mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task); 2585527a6266SJeff Kirsher if (netif_running(mp->dev)) { 2586527a6266SJeff Kirsher netif_tx_stop_all_queues(mp->dev); 2587527a6266SJeff Kirsher port_reset(mp); 2588527a6266SJeff Kirsher port_start(mp); 2589527a6266SJeff Kirsher netif_tx_wake_all_queues(mp->dev); 2590527a6266SJeff Kirsher } 2591527a6266SJeff Kirsher } 2592527a6266SJeff Kirsher 2593527a6266SJeff Kirsher static void mv643xx_eth_tx_timeout(struct net_device *dev) 2594527a6266SJeff Kirsher { 2595527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2596527a6266SJeff Kirsher 2597527a6266SJeff Kirsher netdev_info(dev, "tx timeout\n"); 2598527a6266SJeff Kirsher 2599527a6266SJeff Kirsher schedule_work(&mp->tx_timeout_task); 2600527a6266SJeff Kirsher } 2601527a6266SJeff Kirsher 2602527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 2603527a6266SJeff Kirsher static void mv643xx_eth_netpoll(struct net_device *dev) 2604527a6266SJeff Kirsher { 2605527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2606527a6266SJeff Kirsher 2607527a6266SJeff Kirsher wrlp(mp, INT_MASK, 0x00000000); 2608527a6266SJeff Kirsher rdlp(mp, INT_MASK); 2609527a6266SJeff Kirsher 2610527a6266SJeff Kirsher mv643xx_eth_irq(dev->irq, dev); 2611527a6266SJeff Kirsher 2612527a6266SJeff Kirsher wrlp(mp, INT_MASK, mp->int_mask); 2613527a6266SJeff Kirsher } 2614527a6266SJeff Kirsher #endif 2615527a6266SJeff Kirsher 2616527a6266SJeff Kirsher 2617527a6266SJeff Kirsher /* platform glue ************************************************************/ 2618527a6266SJeff Kirsher static void 2619527a6266SJeff Kirsher mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp, 262063a9332bSAndrew Lunn const struct mbus_dram_target_info *dram) 2621527a6266SJeff Kirsher { 2622527a6266SJeff Kirsher void __iomem *base = msp->base; 2623527a6266SJeff Kirsher u32 win_enable; 2624527a6266SJeff Kirsher u32 win_protect; 2625527a6266SJeff Kirsher int i; 2626527a6266SJeff Kirsher 2627527a6266SJeff Kirsher for (i = 0; i < 6; i++) { 2628527a6266SJeff Kirsher writel(0, base + WINDOW_BASE(i)); 2629527a6266SJeff Kirsher writel(0, base + WINDOW_SIZE(i)); 2630527a6266SJeff Kirsher if (i < 4) 2631527a6266SJeff Kirsher writel(0, base + WINDOW_REMAP_HIGH(i)); 2632527a6266SJeff Kirsher } 2633527a6266SJeff Kirsher 2634527a6266SJeff Kirsher win_enable = 0x3f; 2635527a6266SJeff Kirsher win_protect = 0; 2636527a6266SJeff Kirsher 2637527a6266SJeff Kirsher for (i = 0; i < dram->num_cs; i++) { 263863a9332bSAndrew Lunn const struct mbus_dram_window *cs = dram->cs + i; 2639527a6266SJeff Kirsher 2640527a6266SJeff Kirsher writel((cs->base & 0xffff0000) | 2641527a6266SJeff Kirsher (cs->mbus_attr << 8) | 2642527a6266SJeff Kirsher dram->mbus_dram_target_id, base + WINDOW_BASE(i)); 2643527a6266SJeff Kirsher writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i)); 2644527a6266SJeff Kirsher 2645527a6266SJeff Kirsher win_enable &= ~(1 << i); 2646527a6266SJeff Kirsher win_protect |= 3 << (2 * i); 2647527a6266SJeff Kirsher } 2648527a6266SJeff Kirsher 2649527a6266SJeff Kirsher writel(win_enable, base + WINDOW_BAR_ENABLE); 2650527a6266SJeff Kirsher msp->win_protect = win_protect; 2651527a6266SJeff Kirsher } 2652527a6266SJeff Kirsher 2653527a6266SJeff Kirsher static void infer_hw_params(struct mv643xx_eth_shared_private *msp) 2654527a6266SJeff Kirsher { 2655527a6266SJeff Kirsher /* 2656527a6266SJeff Kirsher * Check whether we have a 14-bit coal limit field in bits 2657527a6266SJeff Kirsher * [21:8], or a 16-bit coal limit in bits [25,21:7] of the 2658527a6266SJeff Kirsher * SDMA config register. 2659527a6266SJeff Kirsher */ 2660527a6266SJeff Kirsher writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG); 2661527a6266SJeff Kirsher if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000) 2662527a6266SJeff Kirsher msp->extended_rx_coal_limit = 1; 2663527a6266SJeff Kirsher else 2664527a6266SJeff Kirsher msp->extended_rx_coal_limit = 0; 2665527a6266SJeff Kirsher 2666527a6266SJeff Kirsher /* 2667527a6266SJeff Kirsher * Check whether the MAC supports TX rate control, and if 2668527a6266SJeff Kirsher * yes, whether its associated registers are in the old or 2669527a6266SJeff Kirsher * the new place. 2670527a6266SJeff Kirsher */ 2671527a6266SJeff Kirsher writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED); 2672527a6266SJeff Kirsher if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) { 2673527a6266SJeff Kirsher msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT; 2674527a6266SJeff Kirsher } else { 2675527a6266SJeff Kirsher writel(7, msp->base + 0x0400 + TX_BW_RATE); 2676527a6266SJeff Kirsher if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7) 2677527a6266SJeff Kirsher msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT; 2678527a6266SJeff Kirsher else 2679527a6266SJeff Kirsher msp->tx_bw_control = TX_BW_CONTROL_ABSENT; 2680527a6266SJeff Kirsher } 2681527a6266SJeff Kirsher } 2682527a6266SJeff Kirsher 268376723bcaSSebastian Hesselbarth #if defined(CONFIG_OF) 268476723bcaSSebastian Hesselbarth static const struct of_device_id mv643xx_eth_shared_ids[] = { 268576723bcaSSebastian Hesselbarth { .compatible = "marvell,orion-eth", }, 268676723bcaSSebastian Hesselbarth { .compatible = "marvell,kirkwood-eth", }, 268776723bcaSSebastian Hesselbarth { } 268876723bcaSSebastian Hesselbarth }; 268976723bcaSSebastian Hesselbarth MODULE_DEVICE_TABLE(of, mv643xx_eth_shared_ids); 269076723bcaSSebastian Hesselbarth #endif 269176723bcaSSebastian Hesselbarth 26923e3397e7SSudip Mukherjee #if defined(CONFIG_OF_IRQ) && !defined(CONFIG_MV64X60) 269376723bcaSSebastian Hesselbarth #define mv643xx_eth_property(_np, _name, _v) \ 269476723bcaSSebastian Hesselbarth do { \ 269576723bcaSSebastian Hesselbarth u32 tmp; \ 269676723bcaSSebastian Hesselbarth if (!of_property_read_u32(_np, "marvell," _name, &tmp)) \ 269776723bcaSSebastian Hesselbarth _v = tmp; \ 269876723bcaSSebastian Hesselbarth } while (0) 269976723bcaSSebastian Hesselbarth 270076723bcaSSebastian Hesselbarth static struct platform_device *port_platdev[3]; 270176723bcaSSebastian Hesselbarth 270276723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev, 270376723bcaSSebastian Hesselbarth struct device_node *pnp) 270476723bcaSSebastian Hesselbarth { 270576723bcaSSebastian Hesselbarth struct platform_device *ppdev; 270676723bcaSSebastian Hesselbarth struct mv643xx_eth_platform_data ppd; 270776723bcaSSebastian Hesselbarth struct resource res; 270876723bcaSSebastian Hesselbarth const char *mac_addr; 270976723bcaSSebastian Hesselbarth int ret; 2710785bf6f7SJonas Gorski int dev_num = 0; 271176723bcaSSebastian Hesselbarth 271276723bcaSSebastian Hesselbarth memset(&ppd, 0, sizeof(ppd)); 271376723bcaSSebastian Hesselbarth ppd.shared = pdev; 271476723bcaSSebastian Hesselbarth 271576723bcaSSebastian Hesselbarth memset(&res, 0, sizeof(res)); 2716cfbcb61fSSergei Shtylyov if (of_irq_to_resource(pnp, 0, &res) <= 0) { 271721c328dcSRob Herring dev_err(&pdev->dev, "missing interrupt on %pOFn\n", pnp); 271876723bcaSSebastian Hesselbarth return -EINVAL; 271976723bcaSSebastian Hesselbarth } 272076723bcaSSebastian Hesselbarth 272176723bcaSSebastian Hesselbarth if (of_property_read_u32(pnp, "reg", &ppd.port_number)) { 272221c328dcSRob Herring dev_err(&pdev->dev, "missing reg property on %pOFn\n", pnp); 272376723bcaSSebastian Hesselbarth return -EINVAL; 272476723bcaSSebastian Hesselbarth } 272576723bcaSSebastian Hesselbarth 272676723bcaSSebastian Hesselbarth if (ppd.port_number >= 3) { 272721c328dcSRob Herring dev_err(&pdev->dev, "invalid reg property on %pOFn\n", pnp); 272876723bcaSSebastian Hesselbarth return -EINVAL; 272976723bcaSSebastian Hesselbarth } 273076723bcaSSebastian Hesselbarth 2731785bf6f7SJonas Gorski while (dev_num < 3 && port_platdev[dev_num]) 2732785bf6f7SJonas Gorski dev_num++; 2733785bf6f7SJonas Gorski 2734785bf6f7SJonas Gorski if (dev_num == 3) { 2735785bf6f7SJonas Gorski dev_err(&pdev->dev, "too many ports registered\n"); 2736785bf6f7SJonas Gorski return -EINVAL; 2737785bf6f7SJonas Gorski } 2738785bf6f7SJonas Gorski 273976723bcaSSebastian Hesselbarth mac_addr = of_get_mac_address(pnp); 2740a51645f7SPetr Štetiar if (!IS_ERR(mac_addr)) 27412d2924afSPetr Štetiar ether_addr_copy(ppd.mac_addr, mac_addr); 274276723bcaSSebastian Hesselbarth 274376723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "tx-queue-size", ppd.tx_queue_size); 274476723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "tx-sram-addr", ppd.tx_sram_addr); 274576723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "tx-sram-size", ppd.tx_sram_size); 274676723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "rx-queue-size", ppd.rx_queue_size); 274776723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "rx-sram-addr", ppd.rx_sram_addr); 274876723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "rx-sram-size", ppd.rx_sram_size); 274976723bcaSSebastian Hesselbarth 275076723bcaSSebastian Hesselbarth ppd.phy_node = of_parse_phandle(pnp, "phy-handle", 0); 275176723bcaSSebastian Hesselbarth if (!ppd.phy_node) { 275276723bcaSSebastian Hesselbarth ppd.phy_addr = MV643XX_ETH_PHY_NONE; 275376723bcaSSebastian Hesselbarth of_property_read_u32(pnp, "speed", &ppd.speed); 275476723bcaSSebastian Hesselbarth of_property_read_u32(pnp, "duplex", &ppd.duplex); 275576723bcaSSebastian Hesselbarth } 275676723bcaSSebastian Hesselbarth 2757785bf6f7SJonas Gorski ppdev = platform_device_alloc(MV643XX_ETH_NAME, dev_num); 275876723bcaSSebastian Hesselbarth if (!ppdev) 275976723bcaSSebastian Hesselbarth return -ENOMEM; 276076723bcaSSebastian Hesselbarth ppdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); 2761b5d82db8SSebastian Hesselbarth ppdev->dev.of_node = pnp; 276276723bcaSSebastian Hesselbarth 276376723bcaSSebastian Hesselbarth ret = platform_device_add_resources(ppdev, &res, 1); 276476723bcaSSebastian Hesselbarth if (ret) 276576723bcaSSebastian Hesselbarth goto port_err; 276676723bcaSSebastian Hesselbarth 276776723bcaSSebastian Hesselbarth ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd)); 276876723bcaSSebastian Hesselbarth if (ret) 276976723bcaSSebastian Hesselbarth goto port_err; 277076723bcaSSebastian Hesselbarth 277176723bcaSSebastian Hesselbarth ret = platform_device_add(ppdev); 277276723bcaSSebastian Hesselbarth if (ret) 277376723bcaSSebastian Hesselbarth goto port_err; 277476723bcaSSebastian Hesselbarth 2775785bf6f7SJonas Gorski port_platdev[dev_num] = ppdev; 277676723bcaSSebastian Hesselbarth 277776723bcaSSebastian Hesselbarth return 0; 277876723bcaSSebastian Hesselbarth 277976723bcaSSebastian Hesselbarth port_err: 278076723bcaSSebastian Hesselbarth platform_device_put(ppdev); 278176723bcaSSebastian Hesselbarth return ret; 278276723bcaSSebastian Hesselbarth } 278376723bcaSSebastian Hesselbarth 278476723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_probe(struct platform_device *pdev) 278576723bcaSSebastian Hesselbarth { 278676723bcaSSebastian Hesselbarth struct mv643xx_eth_shared_platform_data *pd; 278776723bcaSSebastian Hesselbarth struct device_node *pnp, *np = pdev->dev.of_node; 278876723bcaSSebastian Hesselbarth int ret; 278976723bcaSSebastian Hesselbarth 279076723bcaSSebastian Hesselbarth /* bail out if not registered from DT */ 279176723bcaSSebastian Hesselbarth if (!np) 279276723bcaSSebastian Hesselbarth return 0; 279376723bcaSSebastian Hesselbarth 279476723bcaSSebastian Hesselbarth pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL); 279576723bcaSSebastian Hesselbarth if (!pd) 279676723bcaSSebastian Hesselbarth return -ENOMEM; 279776723bcaSSebastian Hesselbarth pdev->dev.platform_data = pd; 279876723bcaSSebastian Hesselbarth 279976723bcaSSebastian Hesselbarth mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit); 280076723bcaSSebastian Hesselbarth 280176723bcaSSebastian Hesselbarth for_each_available_child_of_node(np, pnp) { 280276723bcaSSebastian Hesselbarth ret = mv643xx_eth_shared_of_add_port(pdev, pnp); 280326b7974dSJulia Lawall if (ret) { 280426b7974dSJulia Lawall of_node_put(pnp); 280576723bcaSSebastian Hesselbarth return ret; 280676723bcaSSebastian Hesselbarth } 280726b7974dSJulia Lawall } 280876723bcaSSebastian Hesselbarth return 0; 280976723bcaSSebastian Hesselbarth } 281076723bcaSSebastian Hesselbarth 281176723bcaSSebastian Hesselbarth static void mv643xx_eth_shared_of_remove(void) 281276723bcaSSebastian Hesselbarth { 281376723bcaSSebastian Hesselbarth int n; 281476723bcaSSebastian Hesselbarth 281576723bcaSSebastian Hesselbarth for (n = 0; n < 3; n++) { 281676723bcaSSebastian Hesselbarth platform_device_del(port_platdev[n]); 281776723bcaSSebastian Hesselbarth port_platdev[n] = NULL; 281876723bcaSSebastian Hesselbarth } 281976723bcaSSebastian Hesselbarth } 282076723bcaSSebastian Hesselbarth #else 2821ff20877aSArnd Bergmann static inline int mv643xx_eth_shared_of_probe(struct platform_device *pdev) 282276723bcaSSebastian Hesselbarth { 2823ff20877aSArnd Bergmann return 0; 282476723bcaSSebastian Hesselbarth } 282576723bcaSSebastian Hesselbarth 2826ff20877aSArnd Bergmann static inline void mv643xx_eth_shared_of_remove(void) 2827ff20877aSArnd Bergmann { 2828ff20877aSArnd Bergmann } 282976723bcaSSebastian Hesselbarth #endif 283076723bcaSSebastian Hesselbarth 2831527a6266SJeff Kirsher static int mv643xx_eth_shared_probe(struct platform_device *pdev) 2832527a6266SJeff Kirsher { 2833527a6266SJeff Kirsher static int mv643xx_eth_version_printed; 283476723bcaSSebastian Hesselbarth struct mv643xx_eth_shared_platform_data *pd; 2835527a6266SJeff Kirsher struct mv643xx_eth_shared_private *msp; 283663a9332bSAndrew Lunn const struct mbus_dram_target_info *dram; 2837527a6266SJeff Kirsher struct resource *res; 283876723bcaSSebastian Hesselbarth int ret; 2839527a6266SJeff Kirsher 2840527a6266SJeff Kirsher if (!mv643xx_eth_version_printed++) 2841527a6266SJeff Kirsher pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n", 2842527a6266SJeff Kirsher mv643xx_eth_driver_version); 2843527a6266SJeff Kirsher 2844527a6266SJeff Kirsher res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2845527a6266SJeff Kirsher if (res == NULL) 2846727f957aSSebastian Hesselbarth return -EINVAL; 2847527a6266SJeff Kirsher 2848727f957aSSebastian Hesselbarth msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL); 2849527a6266SJeff Kirsher if (msp == NULL) 2850727f957aSSebastian Hesselbarth return -ENOMEM; 285176723bcaSSebastian Hesselbarth platform_set_drvdata(pdev, msp); 2852527a6266SJeff Kirsher 285365a6f969SSebastian Hesselbarth msp->base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 2854527a6266SJeff Kirsher if (msp->base == NULL) 2855727f957aSSebastian Hesselbarth return -ENOMEM; 2856527a6266SJeff Kirsher 285720922486SSebastian Hesselbarth msp->clk = devm_clk_get(&pdev->dev, NULL); 285820922486SSebastian Hesselbarth if (!IS_ERR(msp->clk)) 285920922486SSebastian Hesselbarth clk_prepare_enable(msp->clk); 286020922486SSebastian Hesselbarth 2861527a6266SJeff Kirsher /* 2862527a6266SJeff Kirsher * (Re-)program MBUS remapping windows if we are asked to. 2863527a6266SJeff Kirsher */ 286463a9332bSAndrew Lunn dram = mv_mbus_dram_info(); 286563a9332bSAndrew Lunn if (dram) 286663a9332bSAndrew Lunn mv643xx_eth_conf_mbus_windows(msp, dram); 2867527a6266SJeff Kirsher 286876723bcaSSebastian Hesselbarth ret = mv643xx_eth_shared_of_probe(pdev); 286976723bcaSSebastian Hesselbarth if (ret) 2870e928b5d6SAlexey Khoroshilov goto err_put_clk; 2871bbfa6d0aSJingoo Han pd = dev_get_platdata(&pdev->dev); 287276723bcaSSebastian Hesselbarth 2873527a6266SJeff Kirsher msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ? 2874527a6266SJeff Kirsher pd->tx_csum_limit : 9 * 1024; 2875527a6266SJeff Kirsher infer_hw_params(msp); 2876527a6266SJeff Kirsher 2877527a6266SJeff Kirsher return 0; 2878e928b5d6SAlexey Khoroshilov 2879e928b5d6SAlexey Khoroshilov err_put_clk: 2880e928b5d6SAlexey Khoroshilov if (!IS_ERR(msp->clk)) 2881e928b5d6SAlexey Khoroshilov clk_disable_unprepare(msp->clk); 2882e928b5d6SAlexey Khoroshilov return ret; 2883527a6266SJeff Kirsher } 2884527a6266SJeff Kirsher 2885527a6266SJeff Kirsher static int mv643xx_eth_shared_remove(struct platform_device *pdev) 2886527a6266SJeff Kirsher { 2887527a6266SJeff Kirsher struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev); 2888527a6266SJeff Kirsher 288976723bcaSSebastian Hesselbarth mv643xx_eth_shared_of_remove(); 289020922486SSebastian Hesselbarth if (!IS_ERR(msp->clk)) 289120922486SSebastian Hesselbarth clk_disable_unprepare(msp->clk); 2892527a6266SJeff Kirsher return 0; 2893527a6266SJeff Kirsher } 2894527a6266SJeff Kirsher 2895527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_shared_driver = { 2896527a6266SJeff Kirsher .probe = mv643xx_eth_shared_probe, 2897527a6266SJeff Kirsher .remove = mv643xx_eth_shared_remove, 2898527a6266SJeff Kirsher .driver = { 2899527a6266SJeff Kirsher .name = MV643XX_ETH_SHARED_NAME, 290076723bcaSSebastian Hesselbarth .of_match_table = of_match_ptr(mv643xx_eth_shared_ids), 2901527a6266SJeff Kirsher }, 2902527a6266SJeff Kirsher }; 2903527a6266SJeff Kirsher 2904527a6266SJeff Kirsher static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr) 2905527a6266SJeff Kirsher { 2906527a6266SJeff Kirsher int addr_shift = 5 * mp->port_num; 2907527a6266SJeff Kirsher u32 data; 2908527a6266SJeff Kirsher 2909527a6266SJeff Kirsher data = rdl(mp, PHY_ADDR); 2910527a6266SJeff Kirsher data &= ~(0x1f << addr_shift); 2911527a6266SJeff Kirsher data |= (phy_addr & 0x1f) << addr_shift; 2912527a6266SJeff Kirsher wrl(mp, PHY_ADDR, data); 2913527a6266SJeff Kirsher } 2914527a6266SJeff Kirsher 2915527a6266SJeff Kirsher static int phy_addr_get(struct mv643xx_eth_private *mp) 2916527a6266SJeff Kirsher { 2917527a6266SJeff Kirsher unsigned int data; 2918527a6266SJeff Kirsher 2919527a6266SJeff Kirsher data = rdl(mp, PHY_ADDR); 2920527a6266SJeff Kirsher 2921527a6266SJeff Kirsher return (data >> (5 * mp->port_num)) & 0x1f; 2922527a6266SJeff Kirsher } 2923527a6266SJeff Kirsher 2924527a6266SJeff Kirsher static void set_params(struct mv643xx_eth_private *mp, 2925527a6266SJeff Kirsher struct mv643xx_eth_platform_data *pd) 2926527a6266SJeff Kirsher { 2927527a6266SJeff Kirsher struct net_device *dev = mp->dev; 2928ee9e4956SEzequiel Garcia unsigned int tx_ring_size; 2929527a6266SJeff Kirsher 2930527a6266SJeff Kirsher if (is_valid_ether_addr(pd->mac_addr)) 2931d458cdf7SJoe Perches memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN); 2932527a6266SJeff Kirsher else 2933527a6266SJeff Kirsher uc_addr_get(mp, dev->dev_addr); 2934527a6266SJeff Kirsher 2935527a6266SJeff Kirsher mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE; 2936527a6266SJeff Kirsher if (pd->rx_queue_size) 2937527a6266SJeff Kirsher mp->rx_ring_size = pd->rx_queue_size; 2938527a6266SJeff Kirsher mp->rx_desc_sram_addr = pd->rx_sram_addr; 2939527a6266SJeff Kirsher mp->rx_desc_sram_size = pd->rx_sram_size; 2940527a6266SJeff Kirsher 2941527a6266SJeff Kirsher mp->rxq_count = pd->rx_queue_count ? : 1; 2942527a6266SJeff Kirsher 2943ee9e4956SEzequiel Garcia tx_ring_size = DEFAULT_TX_QUEUE_SIZE; 2944527a6266SJeff Kirsher if (pd->tx_queue_size) 2945ee9e4956SEzequiel Garcia tx_ring_size = pd->tx_queue_size; 2946ee9e4956SEzequiel Garcia 2947ee9e4956SEzequiel Garcia mp->tx_ring_size = clamp_t(unsigned int, tx_ring_size, 2948ee9e4956SEzequiel Garcia MV643XX_MAX_SKB_DESCS * 2, 4096); 2949ee9e4956SEzequiel Garcia if (mp->tx_ring_size != tx_ring_size) 2950ee9e4956SEzequiel Garcia netdev_warn(dev, "TX queue size set to %u (requested %u)\n", 2951ee9e4956SEzequiel Garcia mp->tx_ring_size, tx_ring_size); 2952ee9e4956SEzequiel Garcia 2953527a6266SJeff Kirsher mp->tx_desc_sram_addr = pd->tx_sram_addr; 2954527a6266SJeff Kirsher mp->tx_desc_sram_size = pd->tx_sram_size; 2955527a6266SJeff Kirsher 2956527a6266SJeff Kirsher mp->txq_count = pd->tx_queue_count ? : 1; 2957527a6266SJeff Kirsher } 2958527a6266SJeff Kirsher 2959fd33b244SJason Gunthorpe static int get_phy_mode(struct mv643xx_eth_private *mp) 2960fd33b244SJason Gunthorpe { 2961fd33b244SJason Gunthorpe struct device *dev = mp->dev->dev.parent; 2962*0c65b2b9SAndrew Lunn phy_interface_t iface; 2963*0c65b2b9SAndrew Lunn int err; 2964fd33b244SJason Gunthorpe 2965fd33b244SJason Gunthorpe if (dev->of_node) 2966*0c65b2b9SAndrew Lunn err = of_get_phy_mode(dev->of_node, &iface); 2967fd33b244SJason Gunthorpe 2968fd33b244SJason Gunthorpe /* Historical default if unspecified. We could also read/write 2969fd33b244SJason Gunthorpe * the interface state in the PSC1 2970fd33b244SJason Gunthorpe */ 2971*0c65b2b9SAndrew Lunn if (!dev->of_node || err) 2972fd33b244SJason Gunthorpe iface = PHY_INTERFACE_MODE_GMII; 2973fd33b244SJason Gunthorpe return iface; 2974fd33b244SJason Gunthorpe } 2975fd33b244SJason Gunthorpe 2976527a6266SJeff Kirsher static struct phy_device *phy_scan(struct mv643xx_eth_private *mp, 2977527a6266SJeff Kirsher int phy_addr) 2978527a6266SJeff Kirsher { 2979527a6266SJeff Kirsher struct phy_device *phydev; 2980527a6266SJeff Kirsher int start; 2981527a6266SJeff Kirsher int num; 2982527a6266SJeff Kirsher int i; 2983c3a07134SFlorian Fainelli char phy_id[MII_BUS_ID_SIZE + 3]; 2984527a6266SJeff Kirsher 2985527a6266SJeff Kirsher if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) { 2986527a6266SJeff Kirsher start = phy_addr_get(mp) & 0x1f; 2987527a6266SJeff Kirsher num = 32; 2988527a6266SJeff Kirsher } else { 2989527a6266SJeff Kirsher start = phy_addr & 0x1f; 2990527a6266SJeff Kirsher num = 1; 2991527a6266SJeff Kirsher } 2992527a6266SJeff Kirsher 2993c3a07134SFlorian Fainelli /* Attempt to connect to the PHY using orion-mdio */ 2994976c90b9SSimon Baatz phydev = ERR_PTR(-ENODEV); 2995527a6266SJeff Kirsher for (i = 0; i < num; i++) { 2996527a6266SJeff Kirsher int addr = (start + i) & 0x1f; 2997527a6266SJeff Kirsher 2998c3a07134SFlorian Fainelli snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT, 2999c3a07134SFlorian Fainelli "orion-mdio-mii", addr); 3000527a6266SJeff Kirsher 3001c3a07134SFlorian Fainelli phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link, 3002fd33b244SJason Gunthorpe get_phy_mode(mp)); 3003c3a07134SFlorian Fainelli if (!IS_ERR(phydev)) { 3004527a6266SJeff Kirsher phy_addr_set(mp, addr); 3005c3a07134SFlorian Fainelli break; 3006527a6266SJeff Kirsher } 3007527a6266SJeff Kirsher } 3008527a6266SJeff Kirsher 3009527a6266SJeff Kirsher return phydev; 3010527a6266SJeff Kirsher } 3011527a6266SJeff Kirsher 3012527a6266SJeff Kirsher static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex) 3013527a6266SJeff Kirsher { 30141e8a655dSPhilippe Reynes struct net_device *dev = mp->dev; 30151e8a655dSPhilippe Reynes struct phy_device *phy = dev->phydev; 3016527a6266SJeff Kirsher 3017527a6266SJeff Kirsher if (speed == 0) { 3018527a6266SJeff Kirsher phy->autoneg = AUTONEG_ENABLE; 3019527a6266SJeff Kirsher phy->speed = 0; 3020527a6266SJeff Kirsher phy->duplex = 0; 30213c1bcc86SAndrew Lunn linkmode_copy(phy->advertising, phy->supported); 30223c1bcc86SAndrew Lunn linkmode_set_bit(ETHTOOL_LINK_MODE_Autoneg_BIT, 30233c1bcc86SAndrew Lunn phy->advertising); 3024527a6266SJeff Kirsher } else { 3025527a6266SJeff Kirsher phy->autoneg = AUTONEG_DISABLE; 30263c1bcc86SAndrew Lunn linkmode_zero(phy->advertising); 3027527a6266SJeff Kirsher phy->speed = speed; 3028527a6266SJeff Kirsher phy->duplex = duplex; 3029527a6266SJeff Kirsher } 3030527a6266SJeff Kirsher phy_start_aneg(phy); 3031527a6266SJeff Kirsher } 3032527a6266SJeff Kirsher 3033527a6266SJeff Kirsher static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex) 3034527a6266SJeff Kirsher { 30351e8a655dSPhilippe Reynes struct net_device *dev = mp->dev; 3036527a6266SJeff Kirsher u32 pscr; 3037527a6266SJeff Kirsher 3038527a6266SJeff Kirsher pscr = rdlp(mp, PORT_SERIAL_CONTROL); 3039527a6266SJeff Kirsher if (pscr & SERIAL_PORT_ENABLE) { 3040527a6266SJeff Kirsher pscr &= ~SERIAL_PORT_ENABLE; 3041527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, pscr); 3042527a6266SJeff Kirsher } 3043527a6266SJeff Kirsher 3044527a6266SJeff Kirsher pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED; 30451e8a655dSPhilippe Reynes if (!dev->phydev) { 3046527a6266SJeff Kirsher pscr |= DISABLE_AUTO_NEG_SPEED_GMII; 3047527a6266SJeff Kirsher if (speed == SPEED_1000) 3048527a6266SJeff Kirsher pscr |= SET_GMII_SPEED_TO_1000; 3049527a6266SJeff Kirsher else if (speed == SPEED_100) 3050527a6266SJeff Kirsher pscr |= SET_MII_SPEED_TO_100; 3051527a6266SJeff Kirsher 3052527a6266SJeff Kirsher pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL; 3053527a6266SJeff Kirsher 3054527a6266SJeff Kirsher pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX; 3055527a6266SJeff Kirsher if (duplex == DUPLEX_FULL) 3056527a6266SJeff Kirsher pscr |= SET_FULL_DUPLEX_MODE; 3057527a6266SJeff Kirsher } 3058527a6266SJeff Kirsher 3059527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, pscr); 3060527a6266SJeff Kirsher } 3061527a6266SJeff Kirsher 3062527a6266SJeff Kirsher static const struct net_device_ops mv643xx_eth_netdev_ops = { 3063527a6266SJeff Kirsher .ndo_open = mv643xx_eth_open, 3064527a6266SJeff Kirsher .ndo_stop = mv643xx_eth_stop, 3065527a6266SJeff Kirsher .ndo_start_xmit = mv643xx_eth_xmit, 3066527a6266SJeff Kirsher .ndo_set_rx_mode = mv643xx_eth_set_rx_mode, 3067527a6266SJeff Kirsher .ndo_set_mac_address = mv643xx_eth_set_mac_address, 3068527a6266SJeff Kirsher .ndo_validate_addr = eth_validate_addr, 3069527a6266SJeff Kirsher .ndo_do_ioctl = mv643xx_eth_ioctl, 3070527a6266SJeff Kirsher .ndo_change_mtu = mv643xx_eth_change_mtu, 3071527a6266SJeff Kirsher .ndo_set_features = mv643xx_eth_set_features, 3072527a6266SJeff Kirsher .ndo_tx_timeout = mv643xx_eth_tx_timeout, 3073527a6266SJeff Kirsher .ndo_get_stats = mv643xx_eth_get_stats, 3074527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 3075527a6266SJeff Kirsher .ndo_poll_controller = mv643xx_eth_netpoll, 3076527a6266SJeff Kirsher #endif 3077527a6266SJeff Kirsher }; 3078527a6266SJeff Kirsher 3079527a6266SJeff Kirsher static int mv643xx_eth_probe(struct platform_device *pdev) 3080527a6266SJeff Kirsher { 3081527a6266SJeff Kirsher struct mv643xx_eth_platform_data *pd; 3082527a6266SJeff Kirsher struct mv643xx_eth_private *mp; 3083527a6266SJeff Kirsher struct net_device *dev; 30841e8a655dSPhilippe Reynes struct phy_device *phydev = NULL; 3085527a6266SJeff Kirsher struct resource *res; 3086527a6266SJeff Kirsher int err; 3087527a6266SJeff Kirsher 3088bbfa6d0aSJingoo Han pd = dev_get_platdata(&pdev->dev); 3089527a6266SJeff Kirsher if (pd == NULL) { 3090527a6266SJeff Kirsher dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n"); 3091527a6266SJeff Kirsher return -ENODEV; 3092527a6266SJeff Kirsher } 3093527a6266SJeff Kirsher 3094527a6266SJeff Kirsher if (pd->shared == NULL) { 3095527a6266SJeff Kirsher dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n"); 3096527a6266SJeff Kirsher return -ENODEV; 3097527a6266SJeff Kirsher } 3098527a6266SJeff Kirsher 3099527a6266SJeff Kirsher dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8); 3100527a6266SJeff Kirsher if (!dev) 3101527a6266SJeff Kirsher return -ENOMEM; 3102527a6266SJeff Kirsher 3103fd33b244SJason Gunthorpe SET_NETDEV_DEV(dev, &pdev->dev); 3104527a6266SJeff Kirsher mp = netdev_priv(dev); 3105527a6266SJeff Kirsher platform_set_drvdata(pdev, mp); 3106527a6266SJeff Kirsher 3107527a6266SJeff Kirsher mp->shared = platform_get_drvdata(pd->shared); 3108527a6266SJeff Kirsher mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10); 3109527a6266SJeff Kirsher mp->port_num = pd->port_number; 3110527a6266SJeff Kirsher 3111527a6266SJeff Kirsher mp->dev = dev; 3112527a6266SJeff Kirsher 3113cb85215fSSebastian Hesselbarth /* Kirkwood resets some registers on gated clocks. Especially 3114cb85215fSSebastian Hesselbarth * CLK125_BYPASS_EN must be cleared but is not available on 3115cb85215fSSebastian Hesselbarth * all other SoCs/System Controllers using this driver. 3116cb85215fSSebastian Hesselbarth */ 3117cb85215fSSebastian Hesselbarth if (of_device_is_compatible(pdev->dev.of_node, 3118cb85215fSSebastian Hesselbarth "marvell,kirkwood-eth-port")) 3119cb85215fSSebastian Hesselbarth wrlp(mp, PORT_SERIAL_CONTROL1, 3120cb85215fSSebastian Hesselbarth rdlp(mp, PORT_SERIAL_CONTROL1) & ~CLK125_BYPASS_EN); 3121cb85215fSSebastian Hesselbarth 3122452503ebSAndrew Lunn /* 31239a43a026SAndrew Lunn * Start with a default rate, and if there is a clock, allow 31249a43a026SAndrew Lunn * it to override the default. 3125452503ebSAndrew Lunn */ 31269a43a026SAndrew Lunn mp->t_clk = 133000000; 312720922486SSebastian Hesselbarth mp->clk = devm_clk_get(&pdev->dev, NULL); 3128452503ebSAndrew Lunn if (!IS_ERR(mp->clk)) { 3129452503ebSAndrew Lunn clk_prepare_enable(mp->clk); 3130452503ebSAndrew Lunn mp->t_clk = clk_get_rate(mp->clk); 313176723bcaSSebastian Hesselbarth } else if (!IS_ERR(mp->shared->clk)) { 313276723bcaSSebastian Hesselbarth mp->t_clk = clk_get_rate(mp->shared->clk); 3133452503ebSAndrew Lunn } 313420922486SSebastian Hesselbarth 3135527a6266SJeff Kirsher set_params(mp, pd); 3136527a6266SJeff Kirsher netif_set_real_num_tx_queues(dev, mp->txq_count); 3137527a6266SJeff Kirsher netif_set_real_num_rx_queues(dev, mp->rxq_count); 3138527a6266SJeff Kirsher 3139cc9d4598SSebastian Hesselbarth err = 0; 3140cc9d4598SSebastian Hesselbarth if (pd->phy_node) { 31411e8a655dSPhilippe Reynes phydev = of_phy_connect(mp->dev, pd->phy_node, 3142cc9d4598SSebastian Hesselbarth mv643xx_eth_adjust_link, 0, 3143fd33b244SJason Gunthorpe get_phy_mode(mp)); 31441e8a655dSPhilippe Reynes if (!phydev) 3145cc9d4598SSebastian Hesselbarth err = -ENODEV; 31466115c11fSDan Carpenter else 31471e8a655dSPhilippe Reynes phy_addr_set(mp, phydev->mdio.addr); 3148cc9d4598SSebastian Hesselbarth } else if (pd->phy_addr != MV643XX_ETH_PHY_NONE) { 31491e8a655dSPhilippe Reynes phydev = phy_scan(mp, pd->phy_addr); 3150527a6266SJeff Kirsher 31511e8a655dSPhilippe Reynes if (IS_ERR(phydev)) 31521e8a655dSPhilippe Reynes err = PTR_ERR(phydev); 3153cc9d4598SSebastian Hesselbarth else 3154cc9d4598SSebastian Hesselbarth phy_init(mp, pd->speed, pd->duplex); 3155cc9d4598SSebastian Hesselbarth } 3156cc9d4598SSebastian Hesselbarth if (err == -ENODEV) { 3157976c90b9SSimon Baatz err = -EPROBE_DEFER; 3158976c90b9SSimon Baatz goto out; 3159976c90b9SSimon Baatz } 3160cc9d4598SSebastian Hesselbarth if (err) 3161cc9d4598SSebastian Hesselbarth goto out; 3162527a6266SJeff Kirsher 31637ad24ea4SWilfried Klaebe dev->ethtool_ops = &mv643xx_eth_ethtool_ops; 3164527a6266SJeff Kirsher 3165527a6266SJeff Kirsher init_pscr(mp, pd->speed, pd->duplex); 3166527a6266SJeff Kirsher 3167527a6266SJeff Kirsher 3168527a6266SJeff Kirsher mib_counters_clear(mp); 3169527a6266SJeff Kirsher 3170e99e88a9SKees Cook timer_setup(&mp->mib_counters_timer, mib_counters_timer_wrapper, 0); 3171527a6266SJeff Kirsher mp->mib_counters_timer.expires = jiffies + 30 * HZ; 3172527a6266SJeff Kirsher 3173527a6266SJeff Kirsher spin_lock_init(&mp->mib_counters_lock); 3174527a6266SJeff Kirsher 3175527a6266SJeff Kirsher INIT_WORK(&mp->tx_timeout_task, tx_timeout_task); 3176527a6266SJeff Kirsher 3177a3659aa0SAndrew Lunn netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, NAPI_POLL_WEIGHT); 3178527a6266SJeff Kirsher 3179e99e88a9SKees Cook timer_setup(&mp->rx_oom, oom_timer_wrapper, 0); 3180527a6266SJeff Kirsher 3181527a6266SJeff Kirsher 3182527a6266SJeff Kirsher res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 3183527a6266SJeff Kirsher BUG_ON(!res); 3184527a6266SJeff Kirsher dev->irq = res->start; 3185527a6266SJeff Kirsher 3186527a6266SJeff Kirsher dev->netdev_ops = &mv643xx_eth_netdev_ops; 3187527a6266SJeff Kirsher 3188527a6266SJeff Kirsher dev->watchdog_timeo = 2 * HZ; 3189527a6266SJeff Kirsher dev->base_addr = 0; 3190527a6266SJeff Kirsher 31913ae8f4e0SEzequiel Garcia dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO; 31924d48d589SEzequiel Garcia dev->vlan_features = dev->features; 31934d48d589SEzequiel Garcia 31944d48d589SEzequiel Garcia dev->features |= NETIF_F_RXCSUM; 31954d48d589SEzequiel Garcia dev->hw_features = dev->features; 3196527a6266SJeff Kirsher 319701789349SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT; 3198ee9e4956SEzequiel Garcia dev->gso_max_segs = MV643XX_MAX_TSO_SEGS; 319901789349SJiri Pirko 3200d894be57SJarod Wilson /* MTU range: 64 - 9500 */ 3201d894be57SJarod Wilson dev->min_mtu = 64; 3202d894be57SJarod Wilson dev->max_mtu = 9500; 3203d894be57SJarod Wilson 3204527a6266SJeff Kirsher if (mp->shared->win_protect) 3205527a6266SJeff Kirsher wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect); 3206527a6266SJeff Kirsher 3207527a6266SJeff Kirsher netif_carrier_off(dev); 3208527a6266SJeff Kirsher 3209527a6266SJeff Kirsher wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE); 3210527a6266SJeff Kirsher 3211527a6266SJeff Kirsher set_rx_coal(mp, 250); 3212527a6266SJeff Kirsher set_tx_coal(mp, 0); 3213527a6266SJeff Kirsher 3214527a6266SJeff Kirsher err = register_netdev(dev); 3215527a6266SJeff Kirsher if (err) 3216527a6266SJeff Kirsher goto out; 3217527a6266SJeff Kirsher 3218527a6266SJeff Kirsher netdev_notice(dev, "port %d with MAC address %pM\n", 3219527a6266SJeff Kirsher mp->port_num, dev->dev_addr); 3220527a6266SJeff Kirsher 3221527a6266SJeff Kirsher if (mp->tx_desc_sram_size > 0) 3222527a6266SJeff Kirsher netdev_notice(dev, "configured with sram\n"); 3223527a6266SJeff Kirsher 3224527a6266SJeff Kirsher return 0; 3225527a6266SJeff Kirsher 3226527a6266SJeff Kirsher out: 322720922486SSebastian Hesselbarth if (!IS_ERR(mp->clk)) 3228baffab28SSimon Baatz clk_disable_unprepare(mp->clk); 3229527a6266SJeff Kirsher free_netdev(dev); 3230527a6266SJeff Kirsher 3231527a6266SJeff Kirsher return err; 3232527a6266SJeff Kirsher } 3233527a6266SJeff Kirsher 3234527a6266SJeff Kirsher static int mv643xx_eth_remove(struct platform_device *pdev) 3235527a6266SJeff Kirsher { 3236527a6266SJeff Kirsher struct mv643xx_eth_private *mp = platform_get_drvdata(pdev); 32371e8a655dSPhilippe Reynes struct net_device *dev = mp->dev; 3238527a6266SJeff Kirsher 3239527a6266SJeff Kirsher unregister_netdev(mp->dev); 32401e8a655dSPhilippe Reynes if (dev->phydev) 32411e8a655dSPhilippe Reynes phy_disconnect(dev->phydev); 3242527a6266SJeff Kirsher cancel_work_sync(&mp->tx_timeout_task); 3243452503ebSAndrew Lunn 324420922486SSebastian Hesselbarth if (!IS_ERR(mp->clk)) 3245452503ebSAndrew Lunn clk_disable_unprepare(mp->clk); 32469a43a026SAndrew Lunn 3247527a6266SJeff Kirsher free_netdev(mp->dev); 3248527a6266SJeff Kirsher 3249527a6266SJeff Kirsher return 0; 3250527a6266SJeff Kirsher } 3251527a6266SJeff Kirsher 3252527a6266SJeff Kirsher static void mv643xx_eth_shutdown(struct platform_device *pdev) 3253527a6266SJeff Kirsher { 3254527a6266SJeff Kirsher struct mv643xx_eth_private *mp = platform_get_drvdata(pdev); 3255527a6266SJeff Kirsher 3256527a6266SJeff Kirsher /* Mask all interrupts on ethernet port */ 3257527a6266SJeff Kirsher wrlp(mp, INT_MASK, 0); 3258527a6266SJeff Kirsher rdlp(mp, INT_MASK); 3259527a6266SJeff Kirsher 3260527a6266SJeff Kirsher if (netif_running(mp->dev)) 3261527a6266SJeff Kirsher port_reset(mp); 3262527a6266SJeff Kirsher } 3263527a6266SJeff Kirsher 3264527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_driver = { 3265527a6266SJeff Kirsher .probe = mv643xx_eth_probe, 3266527a6266SJeff Kirsher .remove = mv643xx_eth_remove, 3267527a6266SJeff Kirsher .shutdown = mv643xx_eth_shutdown, 3268527a6266SJeff Kirsher .driver = { 3269527a6266SJeff Kirsher .name = MV643XX_ETH_NAME, 3270527a6266SJeff Kirsher }, 3271527a6266SJeff Kirsher }; 3272527a6266SJeff Kirsher 32733b5dde70SThierry Reding static struct platform_driver * const drivers[] = { 32743b5dde70SThierry Reding &mv643xx_eth_shared_driver, 32753b5dde70SThierry Reding &mv643xx_eth_driver, 32763b5dde70SThierry Reding }; 32773b5dde70SThierry Reding 3278527a6266SJeff Kirsher static int __init mv643xx_eth_init_module(void) 3279527a6266SJeff Kirsher { 32803b5dde70SThierry Reding return platform_register_drivers(drivers, ARRAY_SIZE(drivers)); 3281527a6266SJeff Kirsher } 3282527a6266SJeff Kirsher module_init(mv643xx_eth_init_module); 3283527a6266SJeff Kirsher 3284527a6266SJeff Kirsher static void __exit mv643xx_eth_cleanup_module(void) 3285527a6266SJeff Kirsher { 32863b5dde70SThierry Reding platform_unregister_drivers(drivers, ARRAY_SIZE(drivers)); 3287527a6266SJeff Kirsher } 3288527a6266SJeff Kirsher module_exit(mv643xx_eth_cleanup_module); 3289527a6266SJeff Kirsher 3290527a6266SJeff Kirsher MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, " 3291527a6266SJeff Kirsher "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek"); 3292527a6266SJeff Kirsher MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX"); 3293527a6266SJeff Kirsher MODULE_LICENSE("GPL"); 3294527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME); 3295527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_NAME); 3296