1527a6266SJeff Kirsher /* 2527a6266SJeff Kirsher * Driver for Marvell Discovery (MV643XX) and Marvell Orion ethernet ports 3527a6266SJeff Kirsher * Copyright (C) 2002 Matthew Dharm <mdharm@momenco.com> 4527a6266SJeff Kirsher * 5527a6266SJeff Kirsher * Based on the 64360 driver from: 6527a6266SJeff Kirsher * Copyright (C) 2002 Rabeeh Khoury <rabeeh@galileo.co.il> 7527a6266SJeff Kirsher * Rabeeh Khoury <rabeeh@marvell.com> 8527a6266SJeff Kirsher * 9527a6266SJeff Kirsher * Copyright (C) 2003 PMC-Sierra, Inc., 10527a6266SJeff Kirsher * written by Manish Lachwani 11527a6266SJeff Kirsher * 12527a6266SJeff Kirsher * Copyright (C) 2003 Ralf Baechle <ralf@linux-mips.org> 13527a6266SJeff Kirsher * 14527a6266SJeff Kirsher * Copyright (C) 2004-2006 MontaVista Software, Inc. 15527a6266SJeff Kirsher * Dale Farnsworth <dale@farnsworth.org> 16527a6266SJeff Kirsher * 17527a6266SJeff Kirsher * Copyright (C) 2004 Steven J. Hill <sjhill1@rockwellcollins.com> 18527a6266SJeff Kirsher * <sjhill@realitydiluted.com> 19527a6266SJeff Kirsher * 20527a6266SJeff Kirsher * Copyright (C) 2007-2008 Marvell Semiconductor 21527a6266SJeff Kirsher * Lennert Buytenhek <buytenh@marvell.com> 22527a6266SJeff Kirsher * 233871c387SMichael Stapelberg * Copyright (C) 2013 Michael Stapelberg <michael@stapelberg.de> 243871c387SMichael Stapelberg * 25527a6266SJeff Kirsher * This program is free software; you can redistribute it and/or 26527a6266SJeff Kirsher * modify it under the terms of the GNU General Public License 27527a6266SJeff Kirsher * as published by the Free Software Foundation; either version 2 28527a6266SJeff Kirsher * of the License, or (at your option) any later version. 29527a6266SJeff Kirsher * 30527a6266SJeff Kirsher * This program is distributed in the hope that it will be useful, 31527a6266SJeff Kirsher * but WITHOUT ANY WARRANTY; without even the implied warranty of 32527a6266SJeff Kirsher * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 33527a6266SJeff Kirsher * GNU General Public License for more details. 34527a6266SJeff Kirsher * 35527a6266SJeff Kirsher * You should have received a copy of the GNU General Public License 360ab75ae8SJeff Kirsher * along with this program; if not, see <http://www.gnu.org/licenses/>. 37527a6266SJeff Kirsher */ 38527a6266SJeff Kirsher 39527a6266SJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 40527a6266SJeff Kirsher 41527a6266SJeff Kirsher #include <linux/init.h> 42527a6266SJeff Kirsher #include <linux/dma-mapping.h> 43527a6266SJeff Kirsher #include <linux/in.h> 44527a6266SJeff Kirsher #include <linux/ip.h> 453ae8f4e0SEzequiel Garcia #include <net/tso.h> 46527a6266SJeff Kirsher #include <linux/tcp.h> 47527a6266SJeff Kirsher #include <linux/udp.h> 48527a6266SJeff Kirsher #include <linux/etherdevice.h> 49527a6266SJeff Kirsher #include <linux/delay.h> 50527a6266SJeff Kirsher #include <linux/ethtool.h> 51527a6266SJeff Kirsher #include <linux/platform_device.h> 52527a6266SJeff Kirsher #include <linux/module.h> 53527a6266SJeff Kirsher #include <linux/kernel.h> 54527a6266SJeff Kirsher #include <linux/spinlock.h> 55527a6266SJeff Kirsher #include <linux/workqueue.h> 56527a6266SJeff Kirsher #include <linux/phy.h> 57527a6266SJeff Kirsher #include <linux/mv643xx_eth.h> 58527a6266SJeff Kirsher #include <linux/io.h> 593619eb85SSebastian Hesselbarth #include <linux/interrupt.h> 60527a6266SJeff Kirsher #include <linux/types.h> 61527a6266SJeff Kirsher #include <linux/slab.h> 62452503ebSAndrew Lunn #include <linux/clk.h> 6376723bcaSSebastian Hesselbarth #include <linux/of.h> 6476723bcaSSebastian Hesselbarth #include <linux/of_irq.h> 6576723bcaSSebastian Hesselbarth #include <linux/of_net.h> 66cc9d4598SSebastian Hesselbarth #include <linux/of_mdio.h> 67527a6266SJeff Kirsher 68527a6266SJeff Kirsher static char mv643xx_eth_driver_name[] = "mv643xx_eth"; 69527a6266SJeff Kirsher static char mv643xx_eth_driver_version[] = "1.4"; 70527a6266SJeff Kirsher 71527a6266SJeff Kirsher 72527a6266SJeff Kirsher /* 73527a6266SJeff Kirsher * Registers shared between all ports. 74527a6266SJeff Kirsher */ 75527a6266SJeff Kirsher #define PHY_ADDR 0x0000 76527a6266SJeff Kirsher #define WINDOW_BASE(w) (0x0200 + ((w) << 3)) 77527a6266SJeff Kirsher #define WINDOW_SIZE(w) (0x0204 + ((w) << 3)) 78527a6266SJeff Kirsher #define WINDOW_REMAP_HIGH(w) (0x0280 + ((w) << 2)) 79527a6266SJeff Kirsher #define WINDOW_BAR_ENABLE 0x0290 80527a6266SJeff Kirsher #define WINDOW_PROTECT(w) (0x0294 + ((w) << 4)) 81527a6266SJeff Kirsher 82527a6266SJeff Kirsher /* 83527a6266SJeff Kirsher * Main per-port registers. These live at offset 0x0400 for 84527a6266SJeff Kirsher * port #0, 0x0800 for port #1, and 0x0c00 for port #2. 85527a6266SJeff Kirsher */ 86527a6266SJeff Kirsher #define PORT_CONFIG 0x0000 87527a6266SJeff Kirsher #define UNICAST_PROMISCUOUS_MODE 0x00000001 88527a6266SJeff Kirsher #define PORT_CONFIG_EXT 0x0004 89527a6266SJeff Kirsher #define MAC_ADDR_LOW 0x0014 90527a6266SJeff Kirsher #define MAC_ADDR_HIGH 0x0018 91527a6266SJeff Kirsher #define SDMA_CONFIG 0x001c 92527a6266SJeff Kirsher #define TX_BURST_SIZE_16_64BIT 0x01000000 93527a6266SJeff Kirsher #define TX_BURST_SIZE_4_64BIT 0x00800000 94527a6266SJeff Kirsher #define BLM_TX_NO_SWAP 0x00000020 95527a6266SJeff Kirsher #define BLM_RX_NO_SWAP 0x00000010 96527a6266SJeff Kirsher #define RX_BURST_SIZE_16_64BIT 0x00000008 97527a6266SJeff Kirsher #define RX_BURST_SIZE_4_64BIT 0x00000004 98527a6266SJeff Kirsher #define PORT_SERIAL_CONTROL 0x003c 99527a6266SJeff Kirsher #define SET_MII_SPEED_TO_100 0x01000000 100527a6266SJeff Kirsher #define SET_GMII_SPEED_TO_1000 0x00800000 101527a6266SJeff Kirsher #define SET_FULL_DUPLEX_MODE 0x00200000 102527a6266SJeff Kirsher #define MAX_RX_PACKET_9700BYTE 0x000a0000 103527a6266SJeff Kirsher #define DISABLE_AUTO_NEG_SPEED_GMII 0x00002000 104527a6266SJeff Kirsher #define DO_NOT_FORCE_LINK_FAIL 0x00000400 105527a6266SJeff Kirsher #define SERIAL_PORT_CONTROL_RESERVED 0x00000200 106527a6266SJeff Kirsher #define DISABLE_AUTO_NEG_FOR_FLOW_CTRL 0x00000008 107527a6266SJeff Kirsher #define DISABLE_AUTO_NEG_FOR_DUPLEX 0x00000004 108527a6266SJeff Kirsher #define FORCE_LINK_PASS 0x00000002 109527a6266SJeff Kirsher #define SERIAL_PORT_ENABLE 0x00000001 110527a6266SJeff Kirsher #define PORT_STATUS 0x0044 111527a6266SJeff Kirsher #define TX_FIFO_EMPTY 0x00000400 112527a6266SJeff Kirsher #define TX_IN_PROGRESS 0x00000080 113527a6266SJeff Kirsher #define PORT_SPEED_MASK 0x00000030 114527a6266SJeff Kirsher #define PORT_SPEED_1000 0x00000010 115527a6266SJeff Kirsher #define PORT_SPEED_100 0x00000020 116527a6266SJeff Kirsher #define PORT_SPEED_10 0x00000000 117527a6266SJeff Kirsher #define FLOW_CONTROL_ENABLED 0x00000008 118527a6266SJeff Kirsher #define FULL_DUPLEX 0x00000004 119527a6266SJeff Kirsher #define LINK_UP 0x00000002 120527a6266SJeff Kirsher #define TXQ_COMMAND 0x0048 121527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF 0x004c 122cb85215fSSebastian Hesselbarth #define PORT_SERIAL_CONTROL1 0x004c 123cb85215fSSebastian Hesselbarth #define CLK125_BYPASS_EN 0x00000010 124527a6266SJeff Kirsher #define TX_BW_RATE 0x0050 125527a6266SJeff Kirsher #define TX_BW_MTU 0x0058 126527a6266SJeff Kirsher #define TX_BW_BURST 0x005c 127527a6266SJeff Kirsher #define INT_CAUSE 0x0060 128527a6266SJeff Kirsher #define INT_TX_END 0x07f80000 129527a6266SJeff Kirsher #define INT_TX_END_0 0x00080000 130527a6266SJeff Kirsher #define INT_RX 0x000003fc 131527a6266SJeff Kirsher #define INT_RX_0 0x00000004 132527a6266SJeff Kirsher #define INT_EXT 0x00000002 133527a6266SJeff Kirsher #define INT_CAUSE_EXT 0x0064 134527a6266SJeff Kirsher #define INT_EXT_LINK_PHY 0x00110000 135527a6266SJeff Kirsher #define INT_EXT_TX 0x000000ff 136527a6266SJeff Kirsher #define INT_MASK 0x0068 137527a6266SJeff Kirsher #define INT_MASK_EXT 0x006c 138527a6266SJeff Kirsher #define TX_FIFO_URGENT_THRESHOLD 0x0074 139302476c9SPaulius Zaleckas #define RX_DISCARD_FRAME_CNT 0x0084 140302476c9SPaulius Zaleckas #define RX_OVERRUN_FRAME_CNT 0x0088 141527a6266SJeff Kirsher #define TXQ_FIX_PRIO_CONF_MOVED 0x00dc 142527a6266SJeff Kirsher #define TX_BW_RATE_MOVED 0x00e0 143527a6266SJeff Kirsher #define TX_BW_MTU_MOVED 0x00e8 144527a6266SJeff Kirsher #define TX_BW_BURST_MOVED 0x00ec 145527a6266SJeff Kirsher #define RXQ_CURRENT_DESC_PTR(q) (0x020c + ((q) << 4)) 146527a6266SJeff Kirsher #define RXQ_COMMAND 0x0280 147527a6266SJeff Kirsher #define TXQ_CURRENT_DESC_PTR(q) (0x02c0 + ((q) << 2)) 148527a6266SJeff Kirsher #define TXQ_BW_TOKENS(q) (0x0300 + ((q) << 4)) 149527a6266SJeff Kirsher #define TXQ_BW_CONF(q) (0x0304 + ((q) << 4)) 150527a6266SJeff Kirsher #define TXQ_BW_WRR_CONF(q) (0x0308 + ((q) << 4)) 151527a6266SJeff Kirsher 152527a6266SJeff Kirsher /* 153527a6266SJeff Kirsher * Misc per-port registers. 154527a6266SJeff Kirsher */ 155527a6266SJeff Kirsher #define MIB_COUNTERS(p) (0x1000 + ((p) << 7)) 156527a6266SJeff Kirsher #define SPECIAL_MCAST_TABLE(p) (0x1400 + ((p) << 10)) 157527a6266SJeff Kirsher #define OTHER_MCAST_TABLE(p) (0x1500 + ((p) << 10)) 158527a6266SJeff Kirsher #define UNICAST_TABLE(p) (0x1600 + ((p) << 10)) 159527a6266SJeff Kirsher 160527a6266SJeff Kirsher 161527a6266SJeff Kirsher /* 162527a6266SJeff Kirsher * SDMA configuration register default value. 163527a6266SJeff Kirsher */ 164527a6266SJeff Kirsher #if defined(__BIG_ENDIAN) 165527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE \ 166527a6266SJeff Kirsher (RX_BURST_SIZE_4_64BIT | \ 167527a6266SJeff Kirsher TX_BURST_SIZE_4_64BIT) 168527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN) 169527a6266SJeff Kirsher #define PORT_SDMA_CONFIG_DEFAULT_VALUE \ 170527a6266SJeff Kirsher (RX_BURST_SIZE_4_64BIT | \ 171527a6266SJeff Kirsher BLM_RX_NO_SWAP | \ 172527a6266SJeff Kirsher BLM_TX_NO_SWAP | \ 173527a6266SJeff Kirsher TX_BURST_SIZE_4_64BIT) 174527a6266SJeff Kirsher #else 175527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined 176527a6266SJeff Kirsher #endif 177527a6266SJeff Kirsher 178527a6266SJeff Kirsher 179527a6266SJeff Kirsher /* 180527a6266SJeff Kirsher * Misc definitions. 181527a6266SJeff Kirsher */ 182527a6266SJeff Kirsher #define DEFAULT_RX_QUEUE_SIZE 128 1833ae8f4e0SEzequiel Garcia #define DEFAULT_TX_QUEUE_SIZE 512 184527a6266SJeff Kirsher #define SKB_DMA_REALIGN ((PAGE_SIZE - NET_SKB_PAD) % SMP_CACHE_BYTES) 185527a6266SJeff Kirsher 1863ae8f4e0SEzequiel Garcia #define TSO_HEADER_SIZE 128 187527a6266SJeff Kirsher 188ee9e4956SEzequiel Garcia /* Max number of allowed TCP segments for software TSO */ 189ee9e4956SEzequiel Garcia #define MV643XX_MAX_TSO_SEGS 100 190ee9e4956SEzequiel Garcia #define MV643XX_MAX_SKB_DESCS (MV643XX_MAX_TSO_SEGS * 2 + MAX_SKB_FRAGS) 191ee9e4956SEzequiel Garcia 192b926260cSEzequiel Garcia #define IS_TSO_HEADER(txq, addr) \ 193b926260cSEzequiel Garcia ((addr >= txq->tso_hdrs_dma) && \ 194b926260cSEzequiel Garcia (addr < txq->tso_hdrs_dma + txq->tx_ring_size * TSO_HEADER_SIZE)) 1959e911414SEzequiel Garcia 1969e911414SEzequiel Garcia #define DESC_DMA_MAP_SINGLE 0 1979e911414SEzequiel Garcia #define DESC_DMA_MAP_PAGE 1 1989e911414SEzequiel Garcia 199527a6266SJeff Kirsher /* 200527a6266SJeff Kirsher * RX/TX descriptors. 201527a6266SJeff Kirsher */ 202527a6266SJeff Kirsher #if defined(__BIG_ENDIAN) 203527a6266SJeff Kirsher struct rx_desc { 204527a6266SJeff Kirsher u16 byte_cnt; /* Descriptor buffer byte count */ 205527a6266SJeff Kirsher u16 buf_size; /* Buffer size */ 206527a6266SJeff Kirsher u32 cmd_sts; /* Descriptor command status */ 207527a6266SJeff Kirsher u32 next_desc_ptr; /* Next descriptor pointer */ 208527a6266SJeff Kirsher u32 buf_ptr; /* Descriptor buffer pointer */ 209527a6266SJeff Kirsher }; 210527a6266SJeff Kirsher 211527a6266SJeff Kirsher struct tx_desc { 212527a6266SJeff Kirsher u16 byte_cnt; /* buffer byte count */ 213527a6266SJeff Kirsher u16 l4i_chk; /* CPU provided TCP checksum */ 214527a6266SJeff Kirsher u32 cmd_sts; /* Command/status field */ 215527a6266SJeff Kirsher u32 next_desc_ptr; /* Pointer to next descriptor */ 216527a6266SJeff Kirsher u32 buf_ptr; /* pointer to buffer for this descriptor*/ 217527a6266SJeff Kirsher }; 218527a6266SJeff Kirsher #elif defined(__LITTLE_ENDIAN) 219527a6266SJeff Kirsher struct rx_desc { 220527a6266SJeff Kirsher u32 cmd_sts; /* Descriptor command status */ 221527a6266SJeff Kirsher u16 buf_size; /* Buffer size */ 222527a6266SJeff Kirsher u16 byte_cnt; /* Descriptor buffer byte count */ 223527a6266SJeff Kirsher u32 buf_ptr; /* Descriptor buffer pointer */ 224527a6266SJeff Kirsher u32 next_desc_ptr; /* Next descriptor pointer */ 225527a6266SJeff Kirsher }; 226527a6266SJeff Kirsher 227527a6266SJeff Kirsher struct tx_desc { 228527a6266SJeff Kirsher u32 cmd_sts; /* Command/status field */ 229527a6266SJeff Kirsher u16 l4i_chk; /* CPU provided TCP checksum */ 230527a6266SJeff Kirsher u16 byte_cnt; /* buffer byte count */ 231527a6266SJeff Kirsher u32 buf_ptr; /* pointer to buffer for this descriptor*/ 232527a6266SJeff Kirsher u32 next_desc_ptr; /* Pointer to next descriptor */ 233527a6266SJeff Kirsher }; 234527a6266SJeff Kirsher #else 235527a6266SJeff Kirsher #error One of __BIG_ENDIAN or __LITTLE_ENDIAN must be defined 236527a6266SJeff Kirsher #endif 237527a6266SJeff Kirsher 238527a6266SJeff Kirsher /* RX & TX descriptor command */ 239527a6266SJeff Kirsher #define BUFFER_OWNED_BY_DMA 0x80000000 240527a6266SJeff Kirsher 241527a6266SJeff Kirsher /* RX & TX descriptor status */ 242527a6266SJeff Kirsher #define ERROR_SUMMARY 0x00000001 243527a6266SJeff Kirsher 244527a6266SJeff Kirsher /* RX descriptor status */ 245527a6266SJeff Kirsher #define LAYER_4_CHECKSUM_OK 0x40000000 246527a6266SJeff Kirsher #define RX_ENABLE_INTERRUPT 0x20000000 247527a6266SJeff Kirsher #define RX_FIRST_DESC 0x08000000 248527a6266SJeff Kirsher #define RX_LAST_DESC 0x04000000 249527a6266SJeff Kirsher #define RX_IP_HDR_OK 0x02000000 250527a6266SJeff Kirsher #define RX_PKT_IS_IPV4 0x01000000 251527a6266SJeff Kirsher #define RX_PKT_IS_ETHERNETV2 0x00800000 252527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_MASK 0x00600000 253527a6266SJeff Kirsher #define RX_PKT_LAYER4_TYPE_TCP_IPV4 0x00000000 254527a6266SJeff Kirsher #define RX_PKT_IS_VLAN_TAGGED 0x00080000 255527a6266SJeff Kirsher 256527a6266SJeff Kirsher /* TX descriptor command */ 257527a6266SJeff Kirsher #define TX_ENABLE_INTERRUPT 0x00800000 258527a6266SJeff Kirsher #define GEN_CRC 0x00400000 259527a6266SJeff Kirsher #define TX_FIRST_DESC 0x00200000 260527a6266SJeff Kirsher #define TX_LAST_DESC 0x00100000 261527a6266SJeff Kirsher #define ZERO_PADDING 0x00080000 262527a6266SJeff Kirsher #define GEN_IP_V4_CHECKSUM 0x00040000 263527a6266SJeff Kirsher #define GEN_TCP_UDP_CHECKSUM 0x00020000 264527a6266SJeff Kirsher #define UDP_FRAME 0x00010000 265527a6266SJeff Kirsher #define MAC_HDR_EXTRA_4_BYTES 0x00008000 26684411f73SEzequiel Garcia #define GEN_TCP_UDP_CHK_FULL 0x00000400 267527a6266SJeff Kirsher #define MAC_HDR_EXTRA_8_BYTES 0x00000200 268527a6266SJeff Kirsher 269527a6266SJeff Kirsher #define TX_IHL_SHIFT 11 270527a6266SJeff Kirsher 271527a6266SJeff Kirsher 272527a6266SJeff Kirsher /* global *******************************************************************/ 273527a6266SJeff Kirsher struct mv643xx_eth_shared_private { 274527a6266SJeff Kirsher /* 275527a6266SJeff Kirsher * Ethernet controller base address. 276527a6266SJeff Kirsher */ 277527a6266SJeff Kirsher void __iomem *base; 278527a6266SJeff Kirsher 279527a6266SJeff Kirsher /* 280527a6266SJeff Kirsher * Per-port MBUS window access register value. 281527a6266SJeff Kirsher */ 282527a6266SJeff Kirsher u32 win_protect; 283527a6266SJeff Kirsher 284527a6266SJeff Kirsher /* 285527a6266SJeff Kirsher * Hardware-specific parameters. 286527a6266SJeff Kirsher */ 287527a6266SJeff Kirsher int extended_rx_coal_limit; 288527a6266SJeff Kirsher int tx_bw_control; 289527a6266SJeff Kirsher int tx_csum_limit; 29020922486SSebastian Hesselbarth struct clk *clk; 291527a6266SJeff Kirsher }; 292527a6266SJeff Kirsher 293527a6266SJeff Kirsher #define TX_BW_CONTROL_ABSENT 0 294527a6266SJeff Kirsher #define TX_BW_CONTROL_OLD_LAYOUT 1 295527a6266SJeff Kirsher #define TX_BW_CONTROL_NEW_LAYOUT 2 296527a6266SJeff Kirsher 297527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev); 298527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev); 299527a6266SJeff Kirsher 300527a6266SJeff Kirsher 301527a6266SJeff Kirsher /* per-port *****************************************************************/ 302527a6266SJeff Kirsher struct mib_counters { 303527a6266SJeff Kirsher u64 good_octets_received; 304527a6266SJeff Kirsher u32 bad_octets_received; 305527a6266SJeff Kirsher u32 internal_mac_transmit_err; 306527a6266SJeff Kirsher u32 good_frames_received; 307527a6266SJeff Kirsher u32 bad_frames_received; 308527a6266SJeff Kirsher u32 broadcast_frames_received; 309527a6266SJeff Kirsher u32 multicast_frames_received; 310527a6266SJeff Kirsher u32 frames_64_octets; 311527a6266SJeff Kirsher u32 frames_65_to_127_octets; 312527a6266SJeff Kirsher u32 frames_128_to_255_octets; 313527a6266SJeff Kirsher u32 frames_256_to_511_octets; 314527a6266SJeff Kirsher u32 frames_512_to_1023_octets; 315527a6266SJeff Kirsher u32 frames_1024_to_max_octets; 316527a6266SJeff Kirsher u64 good_octets_sent; 317527a6266SJeff Kirsher u32 good_frames_sent; 318527a6266SJeff Kirsher u32 excessive_collision; 319527a6266SJeff Kirsher u32 multicast_frames_sent; 320527a6266SJeff Kirsher u32 broadcast_frames_sent; 321527a6266SJeff Kirsher u32 unrec_mac_control_received; 322527a6266SJeff Kirsher u32 fc_sent; 323527a6266SJeff Kirsher u32 good_fc_received; 324527a6266SJeff Kirsher u32 bad_fc_received; 325527a6266SJeff Kirsher u32 undersize_received; 326527a6266SJeff Kirsher u32 fragments_received; 327527a6266SJeff Kirsher u32 oversize_received; 328527a6266SJeff Kirsher u32 jabber_received; 329527a6266SJeff Kirsher u32 mac_receive_error; 330527a6266SJeff Kirsher u32 bad_crc_event; 331527a6266SJeff Kirsher u32 collision; 332527a6266SJeff Kirsher u32 late_collision; 333302476c9SPaulius Zaleckas /* Non MIB hardware counters */ 334302476c9SPaulius Zaleckas u32 rx_discard; 335302476c9SPaulius Zaleckas u32 rx_overrun; 336527a6266SJeff Kirsher }; 337527a6266SJeff Kirsher 338527a6266SJeff Kirsher struct rx_queue { 339527a6266SJeff Kirsher int index; 340527a6266SJeff Kirsher 341527a6266SJeff Kirsher int rx_ring_size; 342527a6266SJeff Kirsher 343527a6266SJeff Kirsher int rx_desc_count; 344527a6266SJeff Kirsher int rx_curr_desc; 345527a6266SJeff Kirsher int rx_used_desc; 346527a6266SJeff Kirsher 347527a6266SJeff Kirsher struct rx_desc *rx_desc_area; 348527a6266SJeff Kirsher dma_addr_t rx_desc_dma; 349527a6266SJeff Kirsher int rx_desc_area_size; 350527a6266SJeff Kirsher struct sk_buff **rx_skb; 351527a6266SJeff Kirsher }; 352527a6266SJeff Kirsher 353527a6266SJeff Kirsher struct tx_queue { 354527a6266SJeff Kirsher int index; 355527a6266SJeff Kirsher 356527a6266SJeff Kirsher int tx_ring_size; 357527a6266SJeff Kirsher 358527a6266SJeff Kirsher int tx_desc_count; 359527a6266SJeff Kirsher int tx_curr_desc; 360527a6266SJeff Kirsher int tx_used_desc; 361527a6266SJeff Kirsher 362ee9e4956SEzequiel Garcia int tx_stop_threshold; 363ee9e4956SEzequiel Garcia int tx_wake_threshold; 364ee9e4956SEzequiel Garcia 3653ae8f4e0SEzequiel Garcia char *tso_hdrs; 3663ae8f4e0SEzequiel Garcia dma_addr_t tso_hdrs_dma; 3673ae8f4e0SEzequiel Garcia 368527a6266SJeff Kirsher struct tx_desc *tx_desc_area; 3699e911414SEzequiel Garcia char *tx_desc_mapping; /* array to track the type of the dma mapping */ 370527a6266SJeff Kirsher dma_addr_t tx_desc_dma; 371527a6266SJeff Kirsher int tx_desc_area_size; 372527a6266SJeff Kirsher 373527a6266SJeff Kirsher struct sk_buff_head tx_skb; 374527a6266SJeff Kirsher 375527a6266SJeff Kirsher unsigned long tx_packets; 376527a6266SJeff Kirsher unsigned long tx_bytes; 377527a6266SJeff Kirsher unsigned long tx_dropped; 378527a6266SJeff Kirsher }; 379527a6266SJeff Kirsher 380527a6266SJeff Kirsher struct mv643xx_eth_private { 381527a6266SJeff Kirsher struct mv643xx_eth_shared_private *shared; 382527a6266SJeff Kirsher void __iomem *base; 383527a6266SJeff Kirsher int port_num; 384527a6266SJeff Kirsher 385527a6266SJeff Kirsher struct net_device *dev; 386527a6266SJeff Kirsher 387527a6266SJeff Kirsher struct phy_device *phy; 388527a6266SJeff Kirsher 389527a6266SJeff Kirsher struct timer_list mib_counters_timer; 390527a6266SJeff Kirsher spinlock_t mib_counters_lock; 391527a6266SJeff Kirsher struct mib_counters mib_counters; 392527a6266SJeff Kirsher 393527a6266SJeff Kirsher struct work_struct tx_timeout_task; 394527a6266SJeff Kirsher 395527a6266SJeff Kirsher struct napi_struct napi; 396527a6266SJeff Kirsher u32 int_mask; 397527a6266SJeff Kirsher u8 oom; 398527a6266SJeff Kirsher u8 work_link; 399527a6266SJeff Kirsher u8 work_tx; 400527a6266SJeff Kirsher u8 work_tx_end; 401527a6266SJeff Kirsher u8 work_rx; 402527a6266SJeff Kirsher u8 work_rx_refill; 403527a6266SJeff Kirsher 404527a6266SJeff Kirsher int skb_size; 405527a6266SJeff Kirsher 406527a6266SJeff Kirsher /* 407527a6266SJeff Kirsher * RX state. 408527a6266SJeff Kirsher */ 409527a6266SJeff Kirsher int rx_ring_size; 410527a6266SJeff Kirsher unsigned long rx_desc_sram_addr; 411527a6266SJeff Kirsher int rx_desc_sram_size; 412527a6266SJeff Kirsher int rxq_count; 413527a6266SJeff Kirsher struct timer_list rx_oom; 414527a6266SJeff Kirsher struct rx_queue rxq[8]; 415527a6266SJeff Kirsher 416527a6266SJeff Kirsher /* 417527a6266SJeff Kirsher * TX state. 418527a6266SJeff Kirsher */ 419527a6266SJeff Kirsher int tx_ring_size; 420527a6266SJeff Kirsher unsigned long tx_desc_sram_addr; 421527a6266SJeff Kirsher int tx_desc_sram_size; 422527a6266SJeff Kirsher int txq_count; 423527a6266SJeff Kirsher struct tx_queue txq[8]; 424452503ebSAndrew Lunn 425452503ebSAndrew Lunn /* 426452503ebSAndrew Lunn * Hardware-specific parameters. 427452503ebSAndrew Lunn */ 428452503ebSAndrew Lunn struct clk *clk; 429452503ebSAndrew Lunn unsigned int t_clk; 430527a6266SJeff Kirsher }; 431527a6266SJeff Kirsher 432527a6266SJeff Kirsher 433527a6266SJeff Kirsher /* port register accessors **************************************************/ 434527a6266SJeff Kirsher static inline u32 rdl(struct mv643xx_eth_private *mp, int offset) 435527a6266SJeff Kirsher { 436527a6266SJeff Kirsher return readl(mp->shared->base + offset); 437527a6266SJeff Kirsher } 438527a6266SJeff Kirsher 439527a6266SJeff Kirsher static inline u32 rdlp(struct mv643xx_eth_private *mp, int offset) 440527a6266SJeff Kirsher { 441527a6266SJeff Kirsher return readl(mp->base + offset); 442527a6266SJeff Kirsher } 443527a6266SJeff Kirsher 444527a6266SJeff Kirsher static inline void wrl(struct mv643xx_eth_private *mp, int offset, u32 data) 445527a6266SJeff Kirsher { 446527a6266SJeff Kirsher writel(data, mp->shared->base + offset); 447527a6266SJeff Kirsher } 448527a6266SJeff Kirsher 449527a6266SJeff Kirsher static inline void wrlp(struct mv643xx_eth_private *mp, int offset, u32 data) 450527a6266SJeff Kirsher { 451527a6266SJeff Kirsher writel(data, mp->base + offset); 452527a6266SJeff Kirsher } 453527a6266SJeff Kirsher 454527a6266SJeff Kirsher 455527a6266SJeff Kirsher /* rxq/txq helper functions *************************************************/ 456527a6266SJeff Kirsher static struct mv643xx_eth_private *rxq_to_mp(struct rx_queue *rxq) 457527a6266SJeff Kirsher { 458527a6266SJeff Kirsher return container_of(rxq, struct mv643xx_eth_private, rxq[rxq->index]); 459527a6266SJeff Kirsher } 460527a6266SJeff Kirsher 461527a6266SJeff Kirsher static struct mv643xx_eth_private *txq_to_mp(struct tx_queue *txq) 462527a6266SJeff Kirsher { 463527a6266SJeff Kirsher return container_of(txq, struct mv643xx_eth_private, txq[txq->index]); 464527a6266SJeff Kirsher } 465527a6266SJeff Kirsher 466527a6266SJeff Kirsher static void rxq_enable(struct rx_queue *rxq) 467527a6266SJeff Kirsher { 468527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 469527a6266SJeff Kirsher wrlp(mp, RXQ_COMMAND, 1 << rxq->index); 470527a6266SJeff Kirsher } 471527a6266SJeff Kirsher 472527a6266SJeff Kirsher static void rxq_disable(struct rx_queue *rxq) 473527a6266SJeff Kirsher { 474527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 475527a6266SJeff Kirsher u8 mask = 1 << rxq->index; 476527a6266SJeff Kirsher 477527a6266SJeff Kirsher wrlp(mp, RXQ_COMMAND, mask << 8); 478527a6266SJeff Kirsher while (rdlp(mp, RXQ_COMMAND) & mask) 479527a6266SJeff Kirsher udelay(10); 480527a6266SJeff Kirsher } 481527a6266SJeff Kirsher 482527a6266SJeff Kirsher static void txq_reset_hw_ptr(struct tx_queue *txq) 483527a6266SJeff Kirsher { 484527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 485527a6266SJeff Kirsher u32 addr; 486527a6266SJeff Kirsher 487527a6266SJeff Kirsher addr = (u32)txq->tx_desc_dma; 488527a6266SJeff Kirsher addr += txq->tx_curr_desc * sizeof(struct tx_desc); 489527a6266SJeff Kirsher wrlp(mp, TXQ_CURRENT_DESC_PTR(txq->index), addr); 490527a6266SJeff Kirsher } 491527a6266SJeff Kirsher 492527a6266SJeff Kirsher static void txq_enable(struct tx_queue *txq) 493527a6266SJeff Kirsher { 494527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 495527a6266SJeff Kirsher wrlp(mp, TXQ_COMMAND, 1 << txq->index); 496527a6266SJeff Kirsher } 497527a6266SJeff Kirsher 498527a6266SJeff Kirsher static void txq_disable(struct tx_queue *txq) 499527a6266SJeff Kirsher { 500527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 501527a6266SJeff Kirsher u8 mask = 1 << txq->index; 502527a6266SJeff Kirsher 503527a6266SJeff Kirsher wrlp(mp, TXQ_COMMAND, mask << 8); 504527a6266SJeff Kirsher while (rdlp(mp, TXQ_COMMAND) & mask) 505527a6266SJeff Kirsher udelay(10); 506527a6266SJeff Kirsher } 507527a6266SJeff Kirsher 508527a6266SJeff Kirsher static void txq_maybe_wake(struct tx_queue *txq) 509527a6266SJeff Kirsher { 510527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 511527a6266SJeff Kirsher struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index); 512527a6266SJeff Kirsher 513527a6266SJeff Kirsher if (netif_tx_queue_stopped(nq)) { 514527a6266SJeff Kirsher __netif_tx_lock(nq, smp_processor_id()); 515ee9e4956SEzequiel Garcia if (txq->tx_desc_count <= txq->tx_wake_threshold) 516527a6266SJeff Kirsher netif_tx_wake_queue(nq); 517527a6266SJeff Kirsher __netif_tx_unlock(nq); 518527a6266SJeff Kirsher } 519527a6266SJeff Kirsher } 520527a6266SJeff Kirsher 521527a6266SJeff Kirsher static int rxq_process(struct rx_queue *rxq, int budget) 522527a6266SJeff Kirsher { 523527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 524527a6266SJeff Kirsher struct net_device_stats *stats = &mp->dev->stats; 525527a6266SJeff Kirsher int rx; 526527a6266SJeff Kirsher 527527a6266SJeff Kirsher rx = 0; 528527a6266SJeff Kirsher while (rx < budget && rxq->rx_desc_count) { 529527a6266SJeff Kirsher struct rx_desc *rx_desc; 530527a6266SJeff Kirsher unsigned int cmd_sts; 531527a6266SJeff Kirsher struct sk_buff *skb; 532527a6266SJeff Kirsher u16 byte_cnt; 533527a6266SJeff Kirsher 534527a6266SJeff Kirsher rx_desc = &rxq->rx_desc_area[rxq->rx_curr_desc]; 535527a6266SJeff Kirsher 536527a6266SJeff Kirsher cmd_sts = rx_desc->cmd_sts; 537527a6266SJeff Kirsher if (cmd_sts & BUFFER_OWNED_BY_DMA) 538527a6266SJeff Kirsher break; 539527a6266SJeff Kirsher rmb(); 540527a6266SJeff Kirsher 541527a6266SJeff Kirsher skb = rxq->rx_skb[rxq->rx_curr_desc]; 542527a6266SJeff Kirsher rxq->rx_skb[rxq->rx_curr_desc] = NULL; 543527a6266SJeff Kirsher 544527a6266SJeff Kirsher rxq->rx_curr_desc++; 545527a6266SJeff Kirsher if (rxq->rx_curr_desc == rxq->rx_ring_size) 546527a6266SJeff Kirsher rxq->rx_curr_desc = 0; 547527a6266SJeff Kirsher 548527a6266SJeff Kirsher dma_unmap_single(mp->dev->dev.parent, rx_desc->buf_ptr, 549527a6266SJeff Kirsher rx_desc->buf_size, DMA_FROM_DEVICE); 550527a6266SJeff Kirsher rxq->rx_desc_count--; 551527a6266SJeff Kirsher rx++; 552527a6266SJeff Kirsher 553527a6266SJeff Kirsher mp->work_rx_refill |= 1 << rxq->index; 554527a6266SJeff Kirsher 555527a6266SJeff Kirsher byte_cnt = rx_desc->byte_cnt; 556527a6266SJeff Kirsher 557527a6266SJeff Kirsher /* 558527a6266SJeff Kirsher * Update statistics. 559527a6266SJeff Kirsher * 560527a6266SJeff Kirsher * Note that the descriptor byte count includes 2 dummy 561527a6266SJeff Kirsher * bytes automatically inserted by the hardware at the 562527a6266SJeff Kirsher * start of the packet (which we don't count), and a 4 563527a6266SJeff Kirsher * byte CRC at the end of the packet (which we do count). 564527a6266SJeff Kirsher */ 565527a6266SJeff Kirsher stats->rx_packets++; 566527a6266SJeff Kirsher stats->rx_bytes += byte_cnt - 2; 567527a6266SJeff Kirsher 568527a6266SJeff Kirsher /* 569527a6266SJeff Kirsher * In case we received a packet without first / last bits 570527a6266SJeff Kirsher * on, or the error summary bit is set, the packet needs 571527a6266SJeff Kirsher * to be dropped. 572527a6266SJeff Kirsher */ 573527a6266SJeff Kirsher if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC | ERROR_SUMMARY)) 574527a6266SJeff Kirsher != (RX_FIRST_DESC | RX_LAST_DESC)) 575527a6266SJeff Kirsher goto err; 576527a6266SJeff Kirsher 577527a6266SJeff Kirsher /* 578527a6266SJeff Kirsher * The -4 is for the CRC in the trailer of the 579527a6266SJeff Kirsher * received packet 580527a6266SJeff Kirsher */ 581527a6266SJeff Kirsher skb_put(skb, byte_cnt - 2 - 4); 582527a6266SJeff Kirsher 583527a6266SJeff Kirsher if (cmd_sts & LAYER_4_CHECKSUM_OK) 584527a6266SJeff Kirsher skb->ip_summed = CHECKSUM_UNNECESSARY; 585527a6266SJeff Kirsher skb->protocol = eth_type_trans(skb, mp->dev); 586527a6266SJeff Kirsher 58709bf1c10SSebastian Hesselbarth napi_gro_receive(&mp->napi, skb); 588527a6266SJeff Kirsher 589527a6266SJeff Kirsher continue; 590527a6266SJeff Kirsher 591527a6266SJeff Kirsher err: 592527a6266SJeff Kirsher stats->rx_dropped++; 593527a6266SJeff Kirsher 594527a6266SJeff Kirsher if ((cmd_sts & (RX_FIRST_DESC | RX_LAST_DESC)) != 595527a6266SJeff Kirsher (RX_FIRST_DESC | RX_LAST_DESC)) { 596527a6266SJeff Kirsher if (net_ratelimit()) 597527a6266SJeff Kirsher netdev_err(mp->dev, 598527a6266SJeff Kirsher "received packet spanning multiple descriptors\n"); 599527a6266SJeff Kirsher } 600527a6266SJeff Kirsher 601527a6266SJeff Kirsher if (cmd_sts & ERROR_SUMMARY) 602527a6266SJeff Kirsher stats->rx_errors++; 603527a6266SJeff Kirsher 604527a6266SJeff Kirsher dev_kfree_skb(skb); 605527a6266SJeff Kirsher } 606527a6266SJeff Kirsher 607527a6266SJeff Kirsher if (rx < budget) 608527a6266SJeff Kirsher mp->work_rx &= ~(1 << rxq->index); 609527a6266SJeff Kirsher 610527a6266SJeff Kirsher return rx; 611527a6266SJeff Kirsher } 612527a6266SJeff Kirsher 613527a6266SJeff Kirsher static int rxq_refill(struct rx_queue *rxq, int budget) 614527a6266SJeff Kirsher { 615527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 616527a6266SJeff Kirsher int refilled; 617527a6266SJeff Kirsher 618527a6266SJeff Kirsher refilled = 0; 619527a6266SJeff Kirsher while (refilled < budget && rxq->rx_desc_count < rxq->rx_ring_size) { 620527a6266SJeff Kirsher struct sk_buff *skb; 621527a6266SJeff Kirsher int rx; 622527a6266SJeff Kirsher struct rx_desc *rx_desc; 623527a6266SJeff Kirsher int size; 624527a6266SJeff Kirsher 625c056b734SPradeep A Dalvi skb = netdev_alloc_skb(mp->dev, mp->skb_size); 626527a6266SJeff Kirsher 627527a6266SJeff Kirsher if (skb == NULL) { 628527a6266SJeff Kirsher mp->oom = 1; 629527a6266SJeff Kirsher goto oom; 630527a6266SJeff Kirsher } 631527a6266SJeff Kirsher 632527a6266SJeff Kirsher if (SKB_DMA_REALIGN) 633527a6266SJeff Kirsher skb_reserve(skb, SKB_DMA_REALIGN); 634527a6266SJeff Kirsher 635527a6266SJeff Kirsher refilled++; 636527a6266SJeff Kirsher rxq->rx_desc_count++; 637527a6266SJeff Kirsher 638527a6266SJeff Kirsher rx = rxq->rx_used_desc++; 639527a6266SJeff Kirsher if (rxq->rx_used_desc == rxq->rx_ring_size) 640527a6266SJeff Kirsher rxq->rx_used_desc = 0; 641527a6266SJeff Kirsher 642527a6266SJeff Kirsher rx_desc = rxq->rx_desc_area + rx; 643527a6266SJeff Kirsher 64418f1d054SIsaku Yamahata size = skb_end_pointer(skb) - skb->data; 645527a6266SJeff Kirsher rx_desc->buf_ptr = dma_map_single(mp->dev->dev.parent, 646527a6266SJeff Kirsher skb->data, size, 647527a6266SJeff Kirsher DMA_FROM_DEVICE); 648527a6266SJeff Kirsher rx_desc->buf_size = size; 649527a6266SJeff Kirsher rxq->rx_skb[rx] = skb; 650527a6266SJeff Kirsher wmb(); 651527a6266SJeff Kirsher rx_desc->cmd_sts = BUFFER_OWNED_BY_DMA | RX_ENABLE_INTERRUPT; 652527a6266SJeff Kirsher wmb(); 653527a6266SJeff Kirsher 654527a6266SJeff Kirsher /* 655527a6266SJeff Kirsher * The hardware automatically prepends 2 bytes of 656527a6266SJeff Kirsher * dummy data to each received packet, so that the 657527a6266SJeff Kirsher * IP header ends up 16-byte aligned. 658527a6266SJeff Kirsher */ 659527a6266SJeff Kirsher skb_reserve(skb, 2); 660527a6266SJeff Kirsher } 661527a6266SJeff Kirsher 662527a6266SJeff Kirsher if (refilled < budget) 663527a6266SJeff Kirsher mp->work_rx_refill &= ~(1 << rxq->index); 664527a6266SJeff Kirsher 665527a6266SJeff Kirsher oom: 666527a6266SJeff Kirsher return refilled; 667527a6266SJeff Kirsher } 668527a6266SJeff Kirsher 669527a6266SJeff Kirsher 670527a6266SJeff Kirsher /* tx ***********************************************************************/ 671527a6266SJeff Kirsher static inline unsigned int has_tiny_unaligned_frags(struct sk_buff *skb) 672527a6266SJeff Kirsher { 673527a6266SJeff Kirsher int frag; 674527a6266SJeff Kirsher 675527a6266SJeff Kirsher for (frag = 0; frag < skb_shinfo(skb)->nr_frags; frag++) { 6769e903e08SEric Dumazet const skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag]; 6779e903e08SEric Dumazet 6789e903e08SEric Dumazet if (skb_frag_size(fragp) <= 8 && fragp->page_offset & 7) 679527a6266SJeff Kirsher return 1; 680527a6266SJeff Kirsher } 681527a6266SJeff Kirsher 682527a6266SJeff Kirsher return 0; 683527a6266SJeff Kirsher } 684527a6266SJeff Kirsher 6850a8fa933SEzequiel Garcia static inline __be16 sum16_as_be(__sum16 sum) 6860a8fa933SEzequiel Garcia { 6870a8fa933SEzequiel Garcia return (__force __be16)sum; 6880a8fa933SEzequiel Garcia } 6890a8fa933SEzequiel Garcia 6900a8fa933SEzequiel Garcia static int skb_tx_csum(struct mv643xx_eth_private *mp, struct sk_buff *skb, 6910a8fa933SEzequiel Garcia u16 *l4i_chk, u32 *command, int length) 6920a8fa933SEzequiel Garcia { 6930a8fa933SEzequiel Garcia int ret; 6940a8fa933SEzequiel Garcia u32 cmd = 0; 6950a8fa933SEzequiel Garcia 6960a8fa933SEzequiel Garcia if (skb->ip_summed == CHECKSUM_PARTIAL) { 6970a8fa933SEzequiel Garcia int hdr_len; 6980a8fa933SEzequiel Garcia int tag_bytes; 6990a8fa933SEzequiel Garcia 7000a8fa933SEzequiel Garcia BUG_ON(skb->protocol != htons(ETH_P_IP) && 7010a8fa933SEzequiel Garcia skb->protocol != htons(ETH_P_8021Q)); 7020a8fa933SEzequiel Garcia 7030a8fa933SEzequiel Garcia hdr_len = (void *)ip_hdr(skb) - (void *)skb->data; 7040a8fa933SEzequiel Garcia tag_bytes = hdr_len - ETH_HLEN; 7050a8fa933SEzequiel Garcia 7060a8fa933SEzequiel Garcia if (length - hdr_len > mp->shared->tx_csum_limit || 7070a8fa933SEzequiel Garcia unlikely(tag_bytes & ~12)) { 7080a8fa933SEzequiel Garcia ret = skb_checksum_help(skb); 7090a8fa933SEzequiel Garcia if (!ret) 7100a8fa933SEzequiel Garcia goto no_csum; 7110a8fa933SEzequiel Garcia return ret; 7120a8fa933SEzequiel Garcia } 7130a8fa933SEzequiel Garcia 7140a8fa933SEzequiel Garcia if (tag_bytes & 4) 7150a8fa933SEzequiel Garcia cmd |= MAC_HDR_EXTRA_4_BYTES; 7160a8fa933SEzequiel Garcia if (tag_bytes & 8) 7170a8fa933SEzequiel Garcia cmd |= MAC_HDR_EXTRA_8_BYTES; 7180a8fa933SEzequiel Garcia 71984411f73SEzequiel Garcia cmd |= GEN_TCP_UDP_CHECKSUM | GEN_TCP_UDP_CHK_FULL | 7200a8fa933SEzequiel Garcia GEN_IP_V4_CHECKSUM | 7210a8fa933SEzequiel Garcia ip_hdr(skb)->ihl << TX_IHL_SHIFT; 7220a8fa933SEzequiel Garcia 72384411f73SEzequiel Garcia /* TODO: Revisit this. With the usage of GEN_TCP_UDP_CHK_FULL 72484411f73SEzequiel Garcia * it seems we don't need to pass the initial checksum. */ 7250a8fa933SEzequiel Garcia switch (ip_hdr(skb)->protocol) { 7260a8fa933SEzequiel Garcia case IPPROTO_UDP: 7270a8fa933SEzequiel Garcia cmd |= UDP_FRAME; 72884411f73SEzequiel Garcia *l4i_chk = 0; 7290a8fa933SEzequiel Garcia break; 7300a8fa933SEzequiel Garcia case IPPROTO_TCP: 73184411f73SEzequiel Garcia *l4i_chk = 0; 7320a8fa933SEzequiel Garcia break; 7330a8fa933SEzequiel Garcia default: 7340a8fa933SEzequiel Garcia WARN(1, "protocol not supported"); 7350a8fa933SEzequiel Garcia } 7360a8fa933SEzequiel Garcia } else { 7370a8fa933SEzequiel Garcia no_csum: 7380a8fa933SEzequiel Garcia /* Errata BTS #50, IHL must be 5 if no HW checksum */ 7390a8fa933SEzequiel Garcia cmd |= 5 << TX_IHL_SHIFT; 7400a8fa933SEzequiel Garcia } 7410a8fa933SEzequiel Garcia *command = cmd; 7420a8fa933SEzequiel Garcia return 0; 7430a8fa933SEzequiel Garcia } 7440a8fa933SEzequiel Garcia 7453ae8f4e0SEzequiel Garcia static inline int 7463ae8f4e0SEzequiel Garcia txq_put_data_tso(struct net_device *dev, struct tx_queue *txq, 7473ae8f4e0SEzequiel Garcia struct sk_buff *skb, char *data, int length, 7483ae8f4e0SEzequiel Garcia bool last_tcp, bool is_last) 7493ae8f4e0SEzequiel Garcia { 7503ae8f4e0SEzequiel Garcia int tx_index; 7513ae8f4e0SEzequiel Garcia u32 cmd_sts; 7523ae8f4e0SEzequiel Garcia struct tx_desc *desc; 7533ae8f4e0SEzequiel Garcia 7543ae8f4e0SEzequiel Garcia tx_index = txq->tx_curr_desc++; 7553ae8f4e0SEzequiel Garcia if (txq->tx_curr_desc == txq->tx_ring_size) 7563ae8f4e0SEzequiel Garcia txq->tx_curr_desc = 0; 7573ae8f4e0SEzequiel Garcia desc = &txq->tx_desc_area[tx_index]; 7589e911414SEzequiel Garcia txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE; 7593ae8f4e0SEzequiel Garcia 7603ae8f4e0SEzequiel Garcia desc->l4i_chk = 0; 7613ae8f4e0SEzequiel Garcia desc->byte_cnt = length; 76291986fd3SPhilipp Kirchhofer 76391986fd3SPhilipp Kirchhofer if (length <= 8 && (uintptr_t)data & 0x7) { 76491986fd3SPhilipp Kirchhofer /* Copy unaligned small data fragment to TSO header data area */ 76591986fd3SPhilipp Kirchhofer memcpy(txq->tso_hdrs + txq->tx_curr_desc * TSO_HEADER_SIZE, 76691986fd3SPhilipp Kirchhofer data, length); 76791986fd3SPhilipp Kirchhofer desc->buf_ptr = txq->tso_hdrs_dma 76891986fd3SPhilipp Kirchhofer + txq->tx_curr_desc * TSO_HEADER_SIZE; 76991986fd3SPhilipp Kirchhofer } else { 77091986fd3SPhilipp Kirchhofer /* Alignment is okay, map buffer and hand off to hardware */ 77191986fd3SPhilipp Kirchhofer txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE; 7723ae8f4e0SEzequiel Garcia desc->buf_ptr = dma_map_single(dev->dev.parent, data, 7733ae8f4e0SEzequiel Garcia length, DMA_TO_DEVICE); 77491986fd3SPhilipp Kirchhofer if (unlikely(dma_mapping_error(dev->dev.parent, 77591986fd3SPhilipp Kirchhofer desc->buf_ptr))) { 7763ae8f4e0SEzequiel Garcia WARN(1, "dma_map_single failed!\n"); 7773ae8f4e0SEzequiel Garcia return -ENOMEM; 7783ae8f4e0SEzequiel Garcia } 77991986fd3SPhilipp Kirchhofer } 7803ae8f4e0SEzequiel Garcia 7813ae8f4e0SEzequiel Garcia cmd_sts = BUFFER_OWNED_BY_DMA; 7823ae8f4e0SEzequiel Garcia if (last_tcp) { 7833ae8f4e0SEzequiel Garcia /* last descriptor in the TCP packet */ 7843ae8f4e0SEzequiel Garcia cmd_sts |= ZERO_PADDING | TX_LAST_DESC; 7853ae8f4e0SEzequiel Garcia /* last descriptor in SKB */ 7863ae8f4e0SEzequiel Garcia if (is_last) 7873ae8f4e0SEzequiel Garcia cmd_sts |= TX_ENABLE_INTERRUPT; 7883ae8f4e0SEzequiel Garcia } 7893ae8f4e0SEzequiel Garcia desc->cmd_sts = cmd_sts; 7903ae8f4e0SEzequiel Garcia return 0; 7913ae8f4e0SEzequiel Garcia } 7923ae8f4e0SEzequiel Garcia 7933ae8f4e0SEzequiel Garcia static inline void 794968200f3SPhilipp Kirchhofer txq_put_hdr_tso(struct sk_buff *skb, struct tx_queue *txq, int length, 795968200f3SPhilipp Kirchhofer u32 *first_cmd_sts, bool first_desc) 7963ae8f4e0SEzequiel Garcia { 7973ae8f4e0SEzequiel Garcia struct mv643xx_eth_private *mp = txq_to_mp(txq); 7983ae8f4e0SEzequiel Garcia int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 7993ae8f4e0SEzequiel Garcia int tx_index; 8003ae8f4e0SEzequiel Garcia struct tx_desc *desc; 8013ae8f4e0SEzequiel Garcia int ret; 8023ae8f4e0SEzequiel Garcia u32 cmd_csum = 0; 8033ae8f4e0SEzequiel Garcia u16 l4i_chk = 0; 804968200f3SPhilipp Kirchhofer u32 cmd_sts; 8053ae8f4e0SEzequiel Garcia 8063ae8f4e0SEzequiel Garcia tx_index = txq->tx_curr_desc; 8073ae8f4e0SEzequiel Garcia desc = &txq->tx_desc_area[tx_index]; 8083ae8f4e0SEzequiel Garcia 8093ae8f4e0SEzequiel Garcia ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_csum, length); 8103ae8f4e0SEzequiel Garcia if (ret) 8113ae8f4e0SEzequiel Garcia WARN(1, "failed to prepare checksum!"); 8123ae8f4e0SEzequiel Garcia 8133ae8f4e0SEzequiel Garcia /* Should we set this? Can't use the value from skb_tx_csum() 8143ae8f4e0SEzequiel Garcia * as it's not the correct initial L4 checksum to use. */ 8153ae8f4e0SEzequiel Garcia desc->l4i_chk = 0; 8163ae8f4e0SEzequiel Garcia 8173ae8f4e0SEzequiel Garcia desc->byte_cnt = hdr_len; 8183ae8f4e0SEzequiel Garcia desc->buf_ptr = txq->tso_hdrs_dma + 8193ae8f4e0SEzequiel Garcia txq->tx_curr_desc * TSO_HEADER_SIZE; 820968200f3SPhilipp Kirchhofer cmd_sts = cmd_csum | BUFFER_OWNED_BY_DMA | TX_FIRST_DESC | 8213ae8f4e0SEzequiel Garcia GEN_CRC; 8223ae8f4e0SEzequiel Garcia 823968200f3SPhilipp Kirchhofer /* Defer updating the first command descriptor until all 824968200f3SPhilipp Kirchhofer * following descriptors have been written. 825968200f3SPhilipp Kirchhofer */ 826968200f3SPhilipp Kirchhofer if (first_desc) 827968200f3SPhilipp Kirchhofer *first_cmd_sts = cmd_sts; 828968200f3SPhilipp Kirchhofer else 829968200f3SPhilipp Kirchhofer desc->cmd_sts = cmd_sts; 830968200f3SPhilipp Kirchhofer 8313ae8f4e0SEzequiel Garcia txq->tx_curr_desc++; 8323ae8f4e0SEzequiel Garcia if (txq->tx_curr_desc == txq->tx_ring_size) 8333ae8f4e0SEzequiel Garcia txq->tx_curr_desc = 0; 8343ae8f4e0SEzequiel Garcia } 8353ae8f4e0SEzequiel Garcia 8363ae8f4e0SEzequiel Garcia static int txq_submit_tso(struct tx_queue *txq, struct sk_buff *skb, 8373ae8f4e0SEzequiel Garcia struct net_device *dev) 8383ae8f4e0SEzequiel Garcia { 8393ae8f4e0SEzequiel Garcia struct mv643xx_eth_private *mp = txq_to_mp(txq); 8403ae8f4e0SEzequiel Garcia int total_len, data_left, ret; 8413ae8f4e0SEzequiel Garcia int desc_count = 0; 8423ae8f4e0SEzequiel Garcia struct tso_t tso; 8433ae8f4e0SEzequiel Garcia int hdr_len = skb_transport_offset(skb) + tcp_hdrlen(skb); 844968200f3SPhilipp Kirchhofer struct tx_desc *first_tx_desc; 845968200f3SPhilipp Kirchhofer u32 first_cmd_sts = 0; 8463ae8f4e0SEzequiel Garcia 8473ae8f4e0SEzequiel Garcia /* Count needed descriptors */ 8483ae8f4e0SEzequiel Garcia if ((txq->tx_desc_count + tso_count_descs(skb)) >= txq->tx_ring_size) { 8493ae8f4e0SEzequiel Garcia netdev_dbg(dev, "not enough descriptors for TSO!\n"); 8503ae8f4e0SEzequiel Garcia return -EBUSY; 8513ae8f4e0SEzequiel Garcia } 8523ae8f4e0SEzequiel Garcia 853968200f3SPhilipp Kirchhofer first_tx_desc = &txq->tx_desc_area[txq->tx_curr_desc]; 854968200f3SPhilipp Kirchhofer 8553ae8f4e0SEzequiel Garcia /* Initialize the TSO handler, and prepare the first payload */ 8563ae8f4e0SEzequiel Garcia tso_start(skb, &tso); 8573ae8f4e0SEzequiel Garcia 8583ae8f4e0SEzequiel Garcia total_len = skb->len - hdr_len; 8593ae8f4e0SEzequiel Garcia while (total_len > 0) { 860968200f3SPhilipp Kirchhofer bool first_desc = (desc_count == 0); 8613ae8f4e0SEzequiel Garcia char *hdr; 8623ae8f4e0SEzequiel Garcia 8633ae8f4e0SEzequiel Garcia data_left = min_t(int, skb_shinfo(skb)->gso_size, total_len); 8643ae8f4e0SEzequiel Garcia total_len -= data_left; 8653ae8f4e0SEzequiel Garcia desc_count++; 8663ae8f4e0SEzequiel Garcia 8673ae8f4e0SEzequiel Garcia /* prepare packet headers: MAC + IP + TCP */ 8683ae8f4e0SEzequiel Garcia hdr = txq->tso_hdrs + txq->tx_curr_desc * TSO_HEADER_SIZE; 8693ae8f4e0SEzequiel Garcia tso_build_hdr(skb, hdr, &tso, data_left, total_len == 0); 870968200f3SPhilipp Kirchhofer txq_put_hdr_tso(skb, txq, data_left, &first_cmd_sts, 871968200f3SPhilipp Kirchhofer first_desc); 8723ae8f4e0SEzequiel Garcia 8733ae8f4e0SEzequiel Garcia while (data_left > 0) { 8743ae8f4e0SEzequiel Garcia int size; 8753ae8f4e0SEzequiel Garcia desc_count++; 8763ae8f4e0SEzequiel Garcia 8773ae8f4e0SEzequiel Garcia size = min_t(int, tso.size, data_left); 8783ae8f4e0SEzequiel Garcia ret = txq_put_data_tso(dev, txq, skb, tso.data, size, 8793ae8f4e0SEzequiel Garcia size == data_left, 8803ae8f4e0SEzequiel Garcia total_len == 0); 8813ae8f4e0SEzequiel Garcia if (ret) 8823ae8f4e0SEzequiel Garcia goto err_release; 8833ae8f4e0SEzequiel Garcia data_left -= size; 8843ae8f4e0SEzequiel Garcia tso_build_data(skb, &tso, size); 8853ae8f4e0SEzequiel Garcia } 8863ae8f4e0SEzequiel Garcia } 8873ae8f4e0SEzequiel Garcia 8883ae8f4e0SEzequiel Garcia __skb_queue_tail(&txq->tx_skb, skb); 8893ae8f4e0SEzequiel Garcia skb_tx_timestamp(skb); 8903ae8f4e0SEzequiel Garcia 891968200f3SPhilipp Kirchhofer /* ensure all other descriptors are written before first cmd_sts */ 892968200f3SPhilipp Kirchhofer wmb(); 893968200f3SPhilipp Kirchhofer first_tx_desc->cmd_sts = first_cmd_sts; 894968200f3SPhilipp Kirchhofer 8953ae8f4e0SEzequiel Garcia /* clear TX_END status */ 8963ae8f4e0SEzequiel Garcia mp->work_tx_end &= ~(1 << txq->index); 8973ae8f4e0SEzequiel Garcia 8983ae8f4e0SEzequiel Garcia /* ensure all descriptors are written before poking hardware */ 8993ae8f4e0SEzequiel Garcia wmb(); 9003ae8f4e0SEzequiel Garcia txq_enable(txq); 9013ae8f4e0SEzequiel Garcia txq->tx_desc_count += desc_count; 9023ae8f4e0SEzequiel Garcia return 0; 9033ae8f4e0SEzequiel Garcia err_release: 9043ae8f4e0SEzequiel Garcia /* TODO: Release all used data descriptors; header descriptors must not 9053ae8f4e0SEzequiel Garcia * be DMA-unmapped. 9063ae8f4e0SEzequiel Garcia */ 9073ae8f4e0SEzequiel Garcia return ret; 9083ae8f4e0SEzequiel Garcia } 9093ae8f4e0SEzequiel Garcia 910527a6266SJeff Kirsher static void txq_submit_frag_skb(struct tx_queue *txq, struct sk_buff *skb) 911527a6266SJeff Kirsher { 912527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 913527a6266SJeff Kirsher int nr_frags = skb_shinfo(skb)->nr_frags; 914527a6266SJeff Kirsher int frag; 915527a6266SJeff Kirsher 916527a6266SJeff Kirsher for (frag = 0; frag < nr_frags; frag++) { 917527a6266SJeff Kirsher skb_frag_t *this_frag; 918527a6266SJeff Kirsher int tx_index; 919527a6266SJeff Kirsher struct tx_desc *desc; 920527a6266SJeff Kirsher 921527a6266SJeff Kirsher this_frag = &skb_shinfo(skb)->frags[frag]; 922527a6266SJeff Kirsher tx_index = txq->tx_curr_desc++; 923527a6266SJeff Kirsher if (txq->tx_curr_desc == txq->tx_ring_size) 924527a6266SJeff Kirsher txq->tx_curr_desc = 0; 925527a6266SJeff Kirsher desc = &txq->tx_desc_area[tx_index]; 9269e911414SEzequiel Garcia txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_PAGE; 927527a6266SJeff Kirsher 928527a6266SJeff Kirsher /* 929527a6266SJeff Kirsher * The last fragment will generate an interrupt 930527a6266SJeff Kirsher * which will free the skb on TX completion. 931527a6266SJeff Kirsher */ 932527a6266SJeff Kirsher if (frag == nr_frags - 1) { 933527a6266SJeff Kirsher desc->cmd_sts = BUFFER_OWNED_BY_DMA | 934527a6266SJeff Kirsher ZERO_PADDING | TX_LAST_DESC | 935527a6266SJeff Kirsher TX_ENABLE_INTERRUPT; 936527a6266SJeff Kirsher } else { 937527a6266SJeff Kirsher desc->cmd_sts = BUFFER_OWNED_BY_DMA; 938527a6266SJeff Kirsher } 939527a6266SJeff Kirsher 940527a6266SJeff Kirsher desc->l4i_chk = 0; 9419e903e08SEric Dumazet desc->byte_cnt = skb_frag_size(this_frag); 9429e911414SEzequiel Garcia desc->buf_ptr = skb_frag_dma_map(mp->dev->dev.parent, 9439e911414SEzequiel Garcia this_frag, 0, desc->byte_cnt, 9449e911414SEzequiel Garcia DMA_TO_DEVICE); 945527a6266SJeff Kirsher } 946527a6266SJeff Kirsher } 947527a6266SJeff Kirsher 948ee9e4956SEzequiel Garcia static int txq_submit_skb(struct tx_queue *txq, struct sk_buff *skb, 949ee9e4956SEzequiel Garcia struct net_device *dev) 950527a6266SJeff Kirsher { 951527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 952527a6266SJeff Kirsher int nr_frags = skb_shinfo(skb)->nr_frags; 953527a6266SJeff Kirsher int tx_index; 954527a6266SJeff Kirsher struct tx_desc *desc; 955527a6266SJeff Kirsher u32 cmd_sts; 956527a6266SJeff Kirsher u16 l4i_chk; 9570a8fa933SEzequiel Garcia int length, ret; 958527a6266SJeff Kirsher 9590a8fa933SEzequiel Garcia cmd_sts = 0; 960527a6266SJeff Kirsher l4i_chk = 0; 961527a6266SJeff Kirsher 962ee9e4956SEzequiel Garcia if (txq->tx_ring_size - txq->tx_desc_count < MAX_SKB_FRAGS + 1) { 963ee9e4956SEzequiel Garcia if (net_ratelimit()) 964ee9e4956SEzequiel Garcia netdev_err(dev, "tx queue full?!\n"); 965ee9e4956SEzequiel Garcia return -EBUSY; 966527a6266SJeff Kirsher } 967ee9e4956SEzequiel Garcia 968ee9e4956SEzequiel Garcia ret = skb_tx_csum(mp, skb, &l4i_chk, &cmd_sts, skb->len); 969ee9e4956SEzequiel Garcia if (ret) 970ee9e4956SEzequiel Garcia return ret; 9710a8fa933SEzequiel Garcia cmd_sts |= TX_FIRST_DESC | GEN_CRC | BUFFER_OWNED_BY_DMA; 972527a6266SJeff Kirsher 973527a6266SJeff Kirsher tx_index = txq->tx_curr_desc++; 974527a6266SJeff Kirsher if (txq->tx_curr_desc == txq->tx_ring_size) 975527a6266SJeff Kirsher txq->tx_curr_desc = 0; 976527a6266SJeff Kirsher desc = &txq->tx_desc_area[tx_index]; 9779e911414SEzequiel Garcia txq->tx_desc_mapping[tx_index] = DESC_DMA_MAP_SINGLE; 978527a6266SJeff Kirsher 979527a6266SJeff Kirsher if (nr_frags) { 980527a6266SJeff Kirsher txq_submit_frag_skb(txq, skb); 981527a6266SJeff Kirsher length = skb_headlen(skb); 982527a6266SJeff Kirsher } else { 983527a6266SJeff Kirsher cmd_sts |= ZERO_PADDING | TX_LAST_DESC | TX_ENABLE_INTERRUPT; 984527a6266SJeff Kirsher length = skb->len; 985527a6266SJeff Kirsher } 986527a6266SJeff Kirsher 987527a6266SJeff Kirsher desc->l4i_chk = l4i_chk; 988527a6266SJeff Kirsher desc->byte_cnt = length; 989527a6266SJeff Kirsher desc->buf_ptr = dma_map_single(mp->dev->dev.parent, skb->data, 990527a6266SJeff Kirsher length, DMA_TO_DEVICE); 991527a6266SJeff Kirsher 992527a6266SJeff Kirsher __skb_queue_tail(&txq->tx_skb, skb); 993527a6266SJeff Kirsher 994527a6266SJeff Kirsher skb_tx_timestamp(skb); 995527a6266SJeff Kirsher 996527a6266SJeff Kirsher /* ensure all other descriptors are written before first cmd_sts */ 997527a6266SJeff Kirsher wmb(); 998527a6266SJeff Kirsher desc->cmd_sts = cmd_sts; 999527a6266SJeff Kirsher 1000527a6266SJeff Kirsher /* clear TX_END status */ 1001527a6266SJeff Kirsher mp->work_tx_end &= ~(1 << txq->index); 1002527a6266SJeff Kirsher 1003527a6266SJeff Kirsher /* ensure all descriptors are written before poking hardware */ 1004527a6266SJeff Kirsher wmb(); 1005527a6266SJeff Kirsher txq_enable(txq); 1006527a6266SJeff Kirsher 1007527a6266SJeff Kirsher txq->tx_desc_count += nr_frags + 1; 1008527a6266SJeff Kirsher 1009527a6266SJeff Kirsher return 0; 1010527a6266SJeff Kirsher } 1011527a6266SJeff Kirsher 1012527a6266SJeff Kirsher static netdev_tx_t mv643xx_eth_xmit(struct sk_buff *skb, struct net_device *dev) 1013527a6266SJeff Kirsher { 1014527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 10153ae8f4e0SEzequiel Garcia int length, queue, ret; 1016527a6266SJeff Kirsher struct tx_queue *txq; 1017527a6266SJeff Kirsher struct netdev_queue *nq; 1018527a6266SJeff Kirsher 1019527a6266SJeff Kirsher queue = skb_get_queue_mapping(skb); 1020527a6266SJeff Kirsher txq = mp->txq + queue; 1021527a6266SJeff Kirsher nq = netdev_get_tx_queue(dev, queue); 1022527a6266SJeff Kirsher 1023527a6266SJeff Kirsher if (has_tiny_unaligned_frags(skb) && __skb_linearize(skb)) { 1024527a6266SJeff Kirsher netdev_printk(KERN_DEBUG, dev, 1025527a6266SJeff Kirsher "failed to linearize skb with tiny unaligned fragment\n"); 1026527a6266SJeff Kirsher return NETDEV_TX_BUSY; 1027527a6266SJeff Kirsher } 1028527a6266SJeff Kirsher 1029527a6266SJeff Kirsher length = skb->len; 1030527a6266SJeff Kirsher 10313ae8f4e0SEzequiel Garcia if (skb_is_gso(skb)) 10323ae8f4e0SEzequiel Garcia ret = txq_submit_tso(txq, skb, dev); 10333ae8f4e0SEzequiel Garcia else 1034ee9e4956SEzequiel Garcia ret = txq_submit_skb(txq, skb, dev); 10353ae8f4e0SEzequiel Garcia if (!ret) { 1036527a6266SJeff Kirsher txq->tx_bytes += length; 1037527a6266SJeff Kirsher txq->tx_packets++; 1038527a6266SJeff Kirsher 1039ee9e4956SEzequiel Garcia if (txq->tx_desc_count >= txq->tx_stop_threshold) 1040527a6266SJeff Kirsher netif_tx_stop_queue(nq); 1041dd11680dSEzequiel Garcia } else { 1042dd11680dSEzequiel Garcia txq->tx_dropped++; 1043dd11680dSEzequiel Garcia dev_kfree_skb_any(skb); 1044527a6266SJeff Kirsher } 1045527a6266SJeff Kirsher 1046527a6266SJeff Kirsher return NETDEV_TX_OK; 1047527a6266SJeff Kirsher } 1048527a6266SJeff Kirsher 1049527a6266SJeff Kirsher 1050527a6266SJeff Kirsher /* tx napi ******************************************************************/ 1051527a6266SJeff Kirsher static void txq_kick(struct tx_queue *txq) 1052527a6266SJeff Kirsher { 1053527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 1054527a6266SJeff Kirsher struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index); 1055527a6266SJeff Kirsher u32 hw_desc_ptr; 1056527a6266SJeff Kirsher u32 expected_ptr; 1057527a6266SJeff Kirsher 1058527a6266SJeff Kirsher __netif_tx_lock(nq, smp_processor_id()); 1059527a6266SJeff Kirsher 1060527a6266SJeff Kirsher if (rdlp(mp, TXQ_COMMAND) & (1 << txq->index)) 1061527a6266SJeff Kirsher goto out; 1062527a6266SJeff Kirsher 1063527a6266SJeff Kirsher hw_desc_ptr = rdlp(mp, TXQ_CURRENT_DESC_PTR(txq->index)); 1064527a6266SJeff Kirsher expected_ptr = (u32)txq->tx_desc_dma + 1065527a6266SJeff Kirsher txq->tx_curr_desc * sizeof(struct tx_desc); 1066527a6266SJeff Kirsher 1067527a6266SJeff Kirsher if (hw_desc_ptr != expected_ptr) 1068527a6266SJeff Kirsher txq_enable(txq); 1069527a6266SJeff Kirsher 1070527a6266SJeff Kirsher out: 1071527a6266SJeff Kirsher __netif_tx_unlock(nq); 1072527a6266SJeff Kirsher 1073527a6266SJeff Kirsher mp->work_tx_end &= ~(1 << txq->index); 1074527a6266SJeff Kirsher } 1075527a6266SJeff Kirsher 1076527a6266SJeff Kirsher static int txq_reclaim(struct tx_queue *txq, int budget, int force) 1077527a6266SJeff Kirsher { 1078527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 1079527a6266SJeff Kirsher struct netdev_queue *nq = netdev_get_tx_queue(mp->dev, txq->index); 1080527a6266SJeff Kirsher int reclaimed; 1081527a6266SJeff Kirsher 10823aefe2b4SRussell King __netif_tx_lock_bh(nq); 1083527a6266SJeff Kirsher 1084527a6266SJeff Kirsher reclaimed = 0; 1085527a6266SJeff Kirsher while (reclaimed < budget && txq->tx_desc_count > 0) { 1086527a6266SJeff Kirsher int tx_index; 1087527a6266SJeff Kirsher struct tx_desc *desc; 1088527a6266SJeff Kirsher u32 cmd_sts; 10899e911414SEzequiel Garcia char desc_dma_map; 1090527a6266SJeff Kirsher 1091527a6266SJeff Kirsher tx_index = txq->tx_used_desc; 1092527a6266SJeff Kirsher desc = &txq->tx_desc_area[tx_index]; 10939e911414SEzequiel Garcia desc_dma_map = txq->tx_desc_mapping[tx_index]; 10949e911414SEzequiel Garcia 1095527a6266SJeff Kirsher cmd_sts = desc->cmd_sts; 1096527a6266SJeff Kirsher 1097527a6266SJeff Kirsher if (cmd_sts & BUFFER_OWNED_BY_DMA) { 1098527a6266SJeff Kirsher if (!force) 1099527a6266SJeff Kirsher break; 1100527a6266SJeff Kirsher desc->cmd_sts = cmd_sts & ~BUFFER_OWNED_BY_DMA; 1101527a6266SJeff Kirsher } 1102527a6266SJeff Kirsher 1103527a6266SJeff Kirsher txq->tx_used_desc = tx_index + 1; 1104527a6266SJeff Kirsher if (txq->tx_used_desc == txq->tx_ring_size) 1105527a6266SJeff Kirsher txq->tx_used_desc = 0; 1106527a6266SJeff Kirsher 1107527a6266SJeff Kirsher reclaimed++; 1108527a6266SJeff Kirsher txq->tx_desc_count--; 1109527a6266SJeff Kirsher 11109e911414SEzequiel Garcia if (!IS_TSO_HEADER(txq, desc->buf_ptr)) { 11119e911414SEzequiel Garcia 11129e911414SEzequiel Garcia if (desc_dma_map == DESC_DMA_MAP_PAGE) 11139e911414SEzequiel Garcia dma_unmap_page(mp->dev->dev.parent, 11149e911414SEzequiel Garcia desc->buf_ptr, 11159e911414SEzequiel Garcia desc->byte_cnt, 11169e911414SEzequiel Garcia DMA_TO_DEVICE); 11179e911414SEzequiel Garcia else 11189e911414SEzequiel Garcia dma_unmap_single(mp->dev->dev.parent, 11199e911414SEzequiel Garcia desc->buf_ptr, 11209e911414SEzequiel Garcia desc->byte_cnt, 11219e911414SEzequiel Garcia DMA_TO_DEVICE); 11229e911414SEzequiel Garcia } 11232c2a9cbdSKarl Beldan 11242c2a9cbdSKarl Beldan if (cmd_sts & TX_ENABLE_INTERRUPT) { 11252c2a9cbdSKarl Beldan struct sk_buff *skb = __skb_dequeue(&txq->tx_skb); 11262c2a9cbdSKarl Beldan 11272c2a9cbdSKarl Beldan if (!WARN_ON(!skb)) 11282c2a9cbdSKarl Beldan dev_kfree_skb(skb); 11292c2a9cbdSKarl Beldan } 1130527a6266SJeff Kirsher 1131527a6266SJeff Kirsher if (cmd_sts & ERROR_SUMMARY) { 1132527a6266SJeff Kirsher netdev_info(mp->dev, "tx error\n"); 1133527a6266SJeff Kirsher mp->dev->stats.tx_errors++; 1134527a6266SJeff Kirsher } 1135527a6266SJeff Kirsher 1136527a6266SJeff Kirsher } 1137527a6266SJeff Kirsher 11383aefe2b4SRussell King __netif_tx_unlock_bh(nq); 1139527a6266SJeff Kirsher 1140527a6266SJeff Kirsher if (reclaimed < budget) 1141527a6266SJeff Kirsher mp->work_tx &= ~(1 << txq->index); 1142527a6266SJeff Kirsher 1143527a6266SJeff Kirsher return reclaimed; 1144527a6266SJeff Kirsher } 1145527a6266SJeff Kirsher 1146527a6266SJeff Kirsher 1147527a6266SJeff Kirsher /* tx rate control **********************************************************/ 1148527a6266SJeff Kirsher /* 1149527a6266SJeff Kirsher * Set total maximum TX rate (shared by all TX queues for this port) 1150527a6266SJeff Kirsher * to 'rate' bits per second, with a maximum burst of 'burst' bytes. 1151527a6266SJeff Kirsher */ 1152527a6266SJeff Kirsher static void tx_set_rate(struct mv643xx_eth_private *mp, int rate, int burst) 1153527a6266SJeff Kirsher { 1154527a6266SJeff Kirsher int token_rate; 1155527a6266SJeff Kirsher int mtu; 1156527a6266SJeff Kirsher int bucket_size; 1157527a6266SJeff Kirsher 1158452503ebSAndrew Lunn token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000); 1159527a6266SJeff Kirsher if (token_rate > 1023) 1160527a6266SJeff Kirsher token_rate = 1023; 1161527a6266SJeff Kirsher 1162527a6266SJeff Kirsher mtu = (mp->dev->mtu + 255) >> 8; 1163527a6266SJeff Kirsher if (mtu > 63) 1164527a6266SJeff Kirsher mtu = 63; 1165527a6266SJeff Kirsher 1166527a6266SJeff Kirsher bucket_size = (burst + 255) >> 8; 1167527a6266SJeff Kirsher if (bucket_size > 65535) 1168527a6266SJeff Kirsher bucket_size = 65535; 1169527a6266SJeff Kirsher 1170527a6266SJeff Kirsher switch (mp->shared->tx_bw_control) { 1171527a6266SJeff Kirsher case TX_BW_CONTROL_OLD_LAYOUT: 1172527a6266SJeff Kirsher wrlp(mp, TX_BW_RATE, token_rate); 1173527a6266SJeff Kirsher wrlp(mp, TX_BW_MTU, mtu); 1174527a6266SJeff Kirsher wrlp(mp, TX_BW_BURST, bucket_size); 1175527a6266SJeff Kirsher break; 1176527a6266SJeff Kirsher case TX_BW_CONTROL_NEW_LAYOUT: 1177527a6266SJeff Kirsher wrlp(mp, TX_BW_RATE_MOVED, token_rate); 1178527a6266SJeff Kirsher wrlp(mp, TX_BW_MTU_MOVED, mtu); 1179527a6266SJeff Kirsher wrlp(mp, TX_BW_BURST_MOVED, bucket_size); 1180527a6266SJeff Kirsher break; 1181527a6266SJeff Kirsher } 1182527a6266SJeff Kirsher } 1183527a6266SJeff Kirsher 1184527a6266SJeff Kirsher static void txq_set_rate(struct tx_queue *txq, int rate, int burst) 1185527a6266SJeff Kirsher { 1186527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 1187527a6266SJeff Kirsher int token_rate; 1188527a6266SJeff Kirsher int bucket_size; 1189527a6266SJeff Kirsher 1190452503ebSAndrew Lunn token_rate = ((rate / 1000) * 64) / (mp->t_clk / 1000); 1191527a6266SJeff Kirsher if (token_rate > 1023) 1192527a6266SJeff Kirsher token_rate = 1023; 1193527a6266SJeff Kirsher 1194527a6266SJeff Kirsher bucket_size = (burst + 255) >> 8; 1195527a6266SJeff Kirsher if (bucket_size > 65535) 1196527a6266SJeff Kirsher bucket_size = 65535; 1197527a6266SJeff Kirsher 1198527a6266SJeff Kirsher wrlp(mp, TXQ_BW_TOKENS(txq->index), token_rate << 14); 1199527a6266SJeff Kirsher wrlp(mp, TXQ_BW_CONF(txq->index), (bucket_size << 10) | token_rate); 1200527a6266SJeff Kirsher } 1201527a6266SJeff Kirsher 1202527a6266SJeff Kirsher static void txq_set_fixed_prio_mode(struct tx_queue *txq) 1203527a6266SJeff Kirsher { 1204527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 1205527a6266SJeff Kirsher int off; 1206527a6266SJeff Kirsher u32 val; 1207527a6266SJeff Kirsher 1208527a6266SJeff Kirsher /* 1209527a6266SJeff Kirsher * Turn on fixed priority mode. 1210527a6266SJeff Kirsher */ 1211527a6266SJeff Kirsher off = 0; 1212527a6266SJeff Kirsher switch (mp->shared->tx_bw_control) { 1213527a6266SJeff Kirsher case TX_BW_CONTROL_OLD_LAYOUT: 1214527a6266SJeff Kirsher off = TXQ_FIX_PRIO_CONF; 1215527a6266SJeff Kirsher break; 1216527a6266SJeff Kirsher case TX_BW_CONTROL_NEW_LAYOUT: 1217527a6266SJeff Kirsher off = TXQ_FIX_PRIO_CONF_MOVED; 1218527a6266SJeff Kirsher break; 1219527a6266SJeff Kirsher } 1220527a6266SJeff Kirsher 1221527a6266SJeff Kirsher if (off) { 1222527a6266SJeff Kirsher val = rdlp(mp, off); 1223527a6266SJeff Kirsher val |= 1 << txq->index; 1224527a6266SJeff Kirsher wrlp(mp, off, val); 1225527a6266SJeff Kirsher } 1226527a6266SJeff Kirsher } 1227527a6266SJeff Kirsher 1228527a6266SJeff Kirsher 1229527a6266SJeff Kirsher /* mii management interface *************************************************/ 12300a9e413bSEzequiel Garcia static void mv643xx_eth_adjust_link(struct net_device *dev) 1231260055bbSPhil Sutter { 12320a9e413bSEzequiel Garcia struct mv643xx_eth_private *mp = netdev_priv(dev); 1233260055bbSPhil Sutter u32 pscr = rdlp(mp, PORT_SERIAL_CONTROL); 1234260055bbSPhil Sutter u32 autoneg_disable = FORCE_LINK_PASS | 1235260055bbSPhil Sutter DISABLE_AUTO_NEG_SPEED_GMII | 1236260055bbSPhil Sutter DISABLE_AUTO_NEG_FOR_FLOW_CTRL | 1237260055bbSPhil Sutter DISABLE_AUTO_NEG_FOR_DUPLEX; 1238260055bbSPhil Sutter 1239260055bbSPhil Sutter if (mp->phy->autoneg == AUTONEG_ENABLE) { 1240260055bbSPhil Sutter /* enable auto negotiation */ 1241260055bbSPhil Sutter pscr &= ~autoneg_disable; 1242260055bbSPhil Sutter goto out_write; 1243260055bbSPhil Sutter } 1244260055bbSPhil Sutter 1245260055bbSPhil Sutter pscr |= autoneg_disable; 1246260055bbSPhil Sutter 1247260055bbSPhil Sutter if (mp->phy->speed == SPEED_1000) { 1248260055bbSPhil Sutter /* force gigabit, half duplex not supported */ 1249260055bbSPhil Sutter pscr |= SET_GMII_SPEED_TO_1000; 1250260055bbSPhil Sutter pscr |= SET_FULL_DUPLEX_MODE; 1251260055bbSPhil Sutter goto out_write; 1252260055bbSPhil Sutter } 1253260055bbSPhil Sutter 1254260055bbSPhil Sutter pscr &= ~SET_GMII_SPEED_TO_1000; 1255260055bbSPhil Sutter 1256260055bbSPhil Sutter if (mp->phy->speed == SPEED_100) 1257260055bbSPhil Sutter pscr |= SET_MII_SPEED_TO_100; 1258260055bbSPhil Sutter else 1259260055bbSPhil Sutter pscr &= ~SET_MII_SPEED_TO_100; 1260260055bbSPhil Sutter 1261260055bbSPhil Sutter if (mp->phy->duplex == DUPLEX_FULL) 1262260055bbSPhil Sutter pscr |= SET_FULL_DUPLEX_MODE; 1263260055bbSPhil Sutter else 1264260055bbSPhil Sutter pscr &= ~SET_FULL_DUPLEX_MODE; 1265260055bbSPhil Sutter 1266260055bbSPhil Sutter out_write: 1267260055bbSPhil Sutter wrlp(mp, PORT_SERIAL_CONTROL, pscr); 1268260055bbSPhil Sutter } 1269260055bbSPhil Sutter 1270527a6266SJeff Kirsher /* statistics ***************************************************************/ 1271527a6266SJeff Kirsher static struct net_device_stats *mv643xx_eth_get_stats(struct net_device *dev) 1272527a6266SJeff Kirsher { 1273527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1274527a6266SJeff Kirsher struct net_device_stats *stats = &dev->stats; 1275527a6266SJeff Kirsher unsigned long tx_packets = 0; 1276527a6266SJeff Kirsher unsigned long tx_bytes = 0; 1277527a6266SJeff Kirsher unsigned long tx_dropped = 0; 1278527a6266SJeff Kirsher int i; 1279527a6266SJeff Kirsher 1280527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) { 1281527a6266SJeff Kirsher struct tx_queue *txq = mp->txq + i; 1282527a6266SJeff Kirsher 1283527a6266SJeff Kirsher tx_packets += txq->tx_packets; 1284527a6266SJeff Kirsher tx_bytes += txq->tx_bytes; 1285527a6266SJeff Kirsher tx_dropped += txq->tx_dropped; 1286527a6266SJeff Kirsher } 1287527a6266SJeff Kirsher 1288527a6266SJeff Kirsher stats->tx_packets = tx_packets; 1289527a6266SJeff Kirsher stats->tx_bytes = tx_bytes; 1290527a6266SJeff Kirsher stats->tx_dropped = tx_dropped; 1291527a6266SJeff Kirsher 1292527a6266SJeff Kirsher return stats; 1293527a6266SJeff Kirsher } 1294527a6266SJeff Kirsher 1295527a6266SJeff Kirsher static inline u32 mib_read(struct mv643xx_eth_private *mp, int offset) 1296527a6266SJeff Kirsher { 1297527a6266SJeff Kirsher return rdl(mp, MIB_COUNTERS(mp->port_num) + offset); 1298527a6266SJeff Kirsher } 1299527a6266SJeff Kirsher 1300527a6266SJeff Kirsher static void mib_counters_clear(struct mv643xx_eth_private *mp) 1301527a6266SJeff Kirsher { 1302527a6266SJeff Kirsher int i; 1303527a6266SJeff Kirsher 1304527a6266SJeff Kirsher for (i = 0; i < 0x80; i += 4) 1305527a6266SJeff Kirsher mib_read(mp, i); 1306302476c9SPaulius Zaleckas 1307302476c9SPaulius Zaleckas /* Clear non MIB hw counters also */ 1308302476c9SPaulius Zaleckas rdlp(mp, RX_DISCARD_FRAME_CNT); 1309302476c9SPaulius Zaleckas rdlp(mp, RX_OVERRUN_FRAME_CNT); 1310527a6266SJeff Kirsher } 1311527a6266SJeff Kirsher 1312527a6266SJeff Kirsher static void mib_counters_update(struct mv643xx_eth_private *mp) 1313527a6266SJeff Kirsher { 1314527a6266SJeff Kirsher struct mib_counters *p = &mp->mib_counters; 1315527a6266SJeff Kirsher 1316527a6266SJeff Kirsher spin_lock_bh(&mp->mib_counters_lock); 1317527a6266SJeff Kirsher p->good_octets_received += mib_read(mp, 0x00); 1318527a6266SJeff Kirsher p->bad_octets_received += mib_read(mp, 0x08); 1319527a6266SJeff Kirsher p->internal_mac_transmit_err += mib_read(mp, 0x0c); 1320527a6266SJeff Kirsher p->good_frames_received += mib_read(mp, 0x10); 1321527a6266SJeff Kirsher p->bad_frames_received += mib_read(mp, 0x14); 1322527a6266SJeff Kirsher p->broadcast_frames_received += mib_read(mp, 0x18); 1323527a6266SJeff Kirsher p->multicast_frames_received += mib_read(mp, 0x1c); 1324527a6266SJeff Kirsher p->frames_64_octets += mib_read(mp, 0x20); 1325527a6266SJeff Kirsher p->frames_65_to_127_octets += mib_read(mp, 0x24); 1326527a6266SJeff Kirsher p->frames_128_to_255_octets += mib_read(mp, 0x28); 1327527a6266SJeff Kirsher p->frames_256_to_511_octets += mib_read(mp, 0x2c); 1328527a6266SJeff Kirsher p->frames_512_to_1023_octets += mib_read(mp, 0x30); 1329527a6266SJeff Kirsher p->frames_1024_to_max_octets += mib_read(mp, 0x34); 1330527a6266SJeff Kirsher p->good_octets_sent += mib_read(mp, 0x38); 1331527a6266SJeff Kirsher p->good_frames_sent += mib_read(mp, 0x40); 1332527a6266SJeff Kirsher p->excessive_collision += mib_read(mp, 0x44); 1333527a6266SJeff Kirsher p->multicast_frames_sent += mib_read(mp, 0x48); 1334527a6266SJeff Kirsher p->broadcast_frames_sent += mib_read(mp, 0x4c); 1335527a6266SJeff Kirsher p->unrec_mac_control_received += mib_read(mp, 0x50); 1336527a6266SJeff Kirsher p->fc_sent += mib_read(mp, 0x54); 1337527a6266SJeff Kirsher p->good_fc_received += mib_read(mp, 0x58); 1338527a6266SJeff Kirsher p->bad_fc_received += mib_read(mp, 0x5c); 1339527a6266SJeff Kirsher p->undersize_received += mib_read(mp, 0x60); 1340527a6266SJeff Kirsher p->fragments_received += mib_read(mp, 0x64); 1341527a6266SJeff Kirsher p->oversize_received += mib_read(mp, 0x68); 1342527a6266SJeff Kirsher p->jabber_received += mib_read(mp, 0x6c); 1343527a6266SJeff Kirsher p->mac_receive_error += mib_read(mp, 0x70); 1344527a6266SJeff Kirsher p->bad_crc_event += mib_read(mp, 0x74); 1345527a6266SJeff Kirsher p->collision += mib_read(mp, 0x78); 1346527a6266SJeff Kirsher p->late_collision += mib_read(mp, 0x7c); 1347302476c9SPaulius Zaleckas /* Non MIB hardware counters */ 1348302476c9SPaulius Zaleckas p->rx_discard += rdlp(mp, RX_DISCARD_FRAME_CNT); 1349302476c9SPaulius Zaleckas p->rx_overrun += rdlp(mp, RX_OVERRUN_FRAME_CNT); 1350527a6266SJeff Kirsher spin_unlock_bh(&mp->mib_counters_lock); 1351527a6266SJeff Kirsher } 1352527a6266SJeff Kirsher 1353527a6266SJeff Kirsher static void mib_counters_timer_wrapper(unsigned long _mp) 1354527a6266SJeff Kirsher { 1355527a6266SJeff Kirsher struct mv643xx_eth_private *mp = (void *)_mp; 1356527a6266SJeff Kirsher mib_counters_update(mp); 1357041b4ddbSSebastian Hesselbarth mod_timer(&mp->mib_counters_timer, jiffies + 30 * HZ); 1358527a6266SJeff Kirsher } 1359527a6266SJeff Kirsher 1360527a6266SJeff Kirsher 1361527a6266SJeff Kirsher /* interrupt coalescing *****************************************************/ 1362527a6266SJeff Kirsher /* 1363527a6266SJeff Kirsher * Hardware coalescing parameters are set in units of 64 t_clk 1364527a6266SJeff Kirsher * cycles. I.e.: 1365527a6266SJeff Kirsher * 1366527a6266SJeff Kirsher * coal_delay_in_usec = 64000000 * register_value / t_clk_rate 1367527a6266SJeff Kirsher * 1368527a6266SJeff Kirsher * register_value = coal_delay_in_usec * t_clk_rate / 64000000 1369527a6266SJeff Kirsher * 1370527a6266SJeff Kirsher * In the ->set*() methods, we round the computed register value 1371527a6266SJeff Kirsher * to the nearest integer. 1372527a6266SJeff Kirsher */ 1373527a6266SJeff Kirsher static unsigned int get_rx_coal(struct mv643xx_eth_private *mp) 1374527a6266SJeff Kirsher { 1375527a6266SJeff Kirsher u32 val = rdlp(mp, SDMA_CONFIG); 1376527a6266SJeff Kirsher u64 temp; 1377527a6266SJeff Kirsher 1378527a6266SJeff Kirsher if (mp->shared->extended_rx_coal_limit) 1379527a6266SJeff Kirsher temp = ((val & 0x02000000) >> 10) | ((val & 0x003fff80) >> 7); 1380527a6266SJeff Kirsher else 1381527a6266SJeff Kirsher temp = (val & 0x003fff00) >> 8; 1382527a6266SJeff Kirsher 1383527a6266SJeff Kirsher temp *= 64000000; 1384452503ebSAndrew Lunn do_div(temp, mp->t_clk); 1385527a6266SJeff Kirsher 1386527a6266SJeff Kirsher return (unsigned int)temp; 1387527a6266SJeff Kirsher } 1388527a6266SJeff Kirsher 1389527a6266SJeff Kirsher static void set_rx_coal(struct mv643xx_eth_private *mp, unsigned int usec) 1390527a6266SJeff Kirsher { 1391527a6266SJeff Kirsher u64 temp; 1392527a6266SJeff Kirsher u32 val; 1393527a6266SJeff Kirsher 1394452503ebSAndrew Lunn temp = (u64)usec * mp->t_clk; 1395527a6266SJeff Kirsher temp += 31999999; 1396527a6266SJeff Kirsher do_div(temp, 64000000); 1397527a6266SJeff Kirsher 1398527a6266SJeff Kirsher val = rdlp(mp, SDMA_CONFIG); 1399527a6266SJeff Kirsher if (mp->shared->extended_rx_coal_limit) { 1400527a6266SJeff Kirsher if (temp > 0xffff) 1401527a6266SJeff Kirsher temp = 0xffff; 1402527a6266SJeff Kirsher val &= ~0x023fff80; 1403527a6266SJeff Kirsher val |= (temp & 0x8000) << 10; 1404527a6266SJeff Kirsher val |= (temp & 0x7fff) << 7; 1405527a6266SJeff Kirsher } else { 1406527a6266SJeff Kirsher if (temp > 0x3fff) 1407527a6266SJeff Kirsher temp = 0x3fff; 1408527a6266SJeff Kirsher val &= ~0x003fff00; 1409527a6266SJeff Kirsher val |= (temp & 0x3fff) << 8; 1410527a6266SJeff Kirsher } 1411527a6266SJeff Kirsher wrlp(mp, SDMA_CONFIG, val); 1412527a6266SJeff Kirsher } 1413527a6266SJeff Kirsher 1414527a6266SJeff Kirsher static unsigned int get_tx_coal(struct mv643xx_eth_private *mp) 1415527a6266SJeff Kirsher { 1416527a6266SJeff Kirsher u64 temp; 1417527a6266SJeff Kirsher 1418527a6266SJeff Kirsher temp = (rdlp(mp, TX_FIFO_URGENT_THRESHOLD) & 0x3fff0) >> 4; 1419527a6266SJeff Kirsher temp *= 64000000; 1420452503ebSAndrew Lunn do_div(temp, mp->t_clk); 1421527a6266SJeff Kirsher 1422527a6266SJeff Kirsher return (unsigned int)temp; 1423527a6266SJeff Kirsher } 1424527a6266SJeff Kirsher 1425527a6266SJeff Kirsher static void set_tx_coal(struct mv643xx_eth_private *mp, unsigned int usec) 1426527a6266SJeff Kirsher { 1427527a6266SJeff Kirsher u64 temp; 1428527a6266SJeff Kirsher 1429452503ebSAndrew Lunn temp = (u64)usec * mp->t_clk; 1430527a6266SJeff Kirsher temp += 31999999; 1431527a6266SJeff Kirsher do_div(temp, 64000000); 1432527a6266SJeff Kirsher 1433527a6266SJeff Kirsher if (temp > 0x3fff) 1434527a6266SJeff Kirsher temp = 0x3fff; 1435527a6266SJeff Kirsher 1436527a6266SJeff Kirsher wrlp(mp, TX_FIFO_URGENT_THRESHOLD, temp << 4); 1437527a6266SJeff Kirsher } 1438527a6266SJeff Kirsher 1439527a6266SJeff Kirsher 1440527a6266SJeff Kirsher /* ethtool ******************************************************************/ 1441527a6266SJeff Kirsher struct mv643xx_eth_stats { 1442527a6266SJeff Kirsher char stat_string[ETH_GSTRING_LEN]; 1443527a6266SJeff Kirsher int sizeof_stat; 1444527a6266SJeff Kirsher int netdev_off; 1445527a6266SJeff Kirsher int mp_off; 1446527a6266SJeff Kirsher }; 1447527a6266SJeff Kirsher 1448527a6266SJeff Kirsher #define SSTAT(m) \ 1449527a6266SJeff Kirsher { #m, FIELD_SIZEOF(struct net_device_stats, m), \ 1450527a6266SJeff Kirsher offsetof(struct net_device, stats.m), -1 } 1451527a6266SJeff Kirsher 1452527a6266SJeff Kirsher #define MIBSTAT(m) \ 1453527a6266SJeff Kirsher { #m, FIELD_SIZEOF(struct mib_counters, m), \ 1454527a6266SJeff Kirsher -1, offsetof(struct mv643xx_eth_private, mib_counters.m) } 1455527a6266SJeff Kirsher 1456527a6266SJeff Kirsher static const struct mv643xx_eth_stats mv643xx_eth_stats[] = { 1457527a6266SJeff Kirsher SSTAT(rx_packets), 1458527a6266SJeff Kirsher SSTAT(tx_packets), 1459527a6266SJeff Kirsher SSTAT(rx_bytes), 1460527a6266SJeff Kirsher SSTAT(tx_bytes), 1461527a6266SJeff Kirsher SSTAT(rx_errors), 1462527a6266SJeff Kirsher SSTAT(tx_errors), 1463527a6266SJeff Kirsher SSTAT(rx_dropped), 1464527a6266SJeff Kirsher SSTAT(tx_dropped), 1465527a6266SJeff Kirsher MIBSTAT(good_octets_received), 1466527a6266SJeff Kirsher MIBSTAT(bad_octets_received), 1467527a6266SJeff Kirsher MIBSTAT(internal_mac_transmit_err), 1468527a6266SJeff Kirsher MIBSTAT(good_frames_received), 1469527a6266SJeff Kirsher MIBSTAT(bad_frames_received), 1470527a6266SJeff Kirsher MIBSTAT(broadcast_frames_received), 1471527a6266SJeff Kirsher MIBSTAT(multicast_frames_received), 1472527a6266SJeff Kirsher MIBSTAT(frames_64_octets), 1473527a6266SJeff Kirsher MIBSTAT(frames_65_to_127_octets), 1474527a6266SJeff Kirsher MIBSTAT(frames_128_to_255_octets), 1475527a6266SJeff Kirsher MIBSTAT(frames_256_to_511_octets), 1476527a6266SJeff Kirsher MIBSTAT(frames_512_to_1023_octets), 1477527a6266SJeff Kirsher MIBSTAT(frames_1024_to_max_octets), 1478527a6266SJeff Kirsher MIBSTAT(good_octets_sent), 1479527a6266SJeff Kirsher MIBSTAT(good_frames_sent), 1480527a6266SJeff Kirsher MIBSTAT(excessive_collision), 1481527a6266SJeff Kirsher MIBSTAT(multicast_frames_sent), 1482527a6266SJeff Kirsher MIBSTAT(broadcast_frames_sent), 1483527a6266SJeff Kirsher MIBSTAT(unrec_mac_control_received), 1484527a6266SJeff Kirsher MIBSTAT(fc_sent), 1485527a6266SJeff Kirsher MIBSTAT(good_fc_received), 1486527a6266SJeff Kirsher MIBSTAT(bad_fc_received), 1487527a6266SJeff Kirsher MIBSTAT(undersize_received), 1488527a6266SJeff Kirsher MIBSTAT(fragments_received), 1489527a6266SJeff Kirsher MIBSTAT(oversize_received), 1490527a6266SJeff Kirsher MIBSTAT(jabber_received), 1491527a6266SJeff Kirsher MIBSTAT(mac_receive_error), 1492527a6266SJeff Kirsher MIBSTAT(bad_crc_event), 1493527a6266SJeff Kirsher MIBSTAT(collision), 1494527a6266SJeff Kirsher MIBSTAT(late_collision), 1495302476c9SPaulius Zaleckas MIBSTAT(rx_discard), 1496302476c9SPaulius Zaleckas MIBSTAT(rx_overrun), 1497527a6266SJeff Kirsher }; 1498527a6266SJeff Kirsher 1499527a6266SJeff Kirsher static int 1500527a6266SJeff Kirsher mv643xx_eth_get_settings_phy(struct mv643xx_eth_private *mp, 1501527a6266SJeff Kirsher struct ethtool_cmd *cmd) 1502527a6266SJeff Kirsher { 1503527a6266SJeff Kirsher int err; 1504527a6266SJeff Kirsher 1505527a6266SJeff Kirsher err = phy_read_status(mp->phy); 1506527a6266SJeff Kirsher if (err == 0) 1507527a6266SJeff Kirsher err = phy_ethtool_gset(mp->phy, cmd); 1508527a6266SJeff Kirsher 1509527a6266SJeff Kirsher /* 1510527a6266SJeff Kirsher * The MAC does not support 1000baseT_Half. 1511527a6266SJeff Kirsher */ 1512527a6266SJeff Kirsher cmd->supported &= ~SUPPORTED_1000baseT_Half; 1513527a6266SJeff Kirsher cmd->advertising &= ~ADVERTISED_1000baseT_Half; 1514527a6266SJeff Kirsher 1515527a6266SJeff Kirsher return err; 1516527a6266SJeff Kirsher } 1517527a6266SJeff Kirsher 1518527a6266SJeff Kirsher static int 1519527a6266SJeff Kirsher mv643xx_eth_get_settings_phyless(struct mv643xx_eth_private *mp, 1520527a6266SJeff Kirsher struct ethtool_cmd *cmd) 1521527a6266SJeff Kirsher { 1522527a6266SJeff Kirsher u32 port_status; 1523527a6266SJeff Kirsher 1524527a6266SJeff Kirsher port_status = rdlp(mp, PORT_STATUS); 1525527a6266SJeff Kirsher 1526527a6266SJeff Kirsher cmd->supported = SUPPORTED_MII; 1527527a6266SJeff Kirsher cmd->advertising = ADVERTISED_MII; 1528527a6266SJeff Kirsher switch (port_status & PORT_SPEED_MASK) { 1529527a6266SJeff Kirsher case PORT_SPEED_10: 1530527a6266SJeff Kirsher ethtool_cmd_speed_set(cmd, SPEED_10); 1531527a6266SJeff Kirsher break; 1532527a6266SJeff Kirsher case PORT_SPEED_100: 1533527a6266SJeff Kirsher ethtool_cmd_speed_set(cmd, SPEED_100); 1534527a6266SJeff Kirsher break; 1535527a6266SJeff Kirsher case PORT_SPEED_1000: 1536527a6266SJeff Kirsher ethtool_cmd_speed_set(cmd, SPEED_1000); 1537527a6266SJeff Kirsher break; 1538527a6266SJeff Kirsher default: 1539527a6266SJeff Kirsher cmd->speed = -1; 1540527a6266SJeff Kirsher break; 1541527a6266SJeff Kirsher } 1542527a6266SJeff Kirsher cmd->duplex = (port_status & FULL_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF; 1543527a6266SJeff Kirsher cmd->port = PORT_MII; 1544527a6266SJeff Kirsher cmd->phy_address = 0; 1545527a6266SJeff Kirsher cmd->transceiver = XCVR_INTERNAL; 1546527a6266SJeff Kirsher cmd->autoneg = AUTONEG_DISABLE; 1547527a6266SJeff Kirsher cmd->maxtxpkt = 1; 1548527a6266SJeff Kirsher cmd->maxrxpkt = 1; 1549527a6266SJeff Kirsher 1550527a6266SJeff Kirsher return 0; 1551527a6266SJeff Kirsher } 1552527a6266SJeff Kirsher 15533871c387SMichael Stapelberg static void 15543871c387SMichael Stapelberg mv643xx_eth_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 15553871c387SMichael Stapelberg { 15563871c387SMichael Stapelberg struct mv643xx_eth_private *mp = netdev_priv(dev); 15573871c387SMichael Stapelberg wol->supported = 0; 15583871c387SMichael Stapelberg wol->wolopts = 0; 15593871c387SMichael Stapelberg if (mp->phy) 15603871c387SMichael Stapelberg phy_ethtool_get_wol(mp->phy, wol); 15613871c387SMichael Stapelberg } 15623871c387SMichael Stapelberg 15633871c387SMichael Stapelberg static int 15643871c387SMichael Stapelberg mv643xx_eth_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol) 15653871c387SMichael Stapelberg { 15663871c387SMichael Stapelberg struct mv643xx_eth_private *mp = netdev_priv(dev); 15673871c387SMichael Stapelberg int err; 15683871c387SMichael Stapelberg 15693871c387SMichael Stapelberg if (mp->phy == NULL) 15703871c387SMichael Stapelberg return -EOPNOTSUPP; 15713871c387SMichael Stapelberg 15723871c387SMichael Stapelberg err = phy_ethtool_set_wol(mp->phy, wol); 15733871c387SMichael Stapelberg /* Given that mv643xx_eth works without the marvell-specific PHY driver, 15743871c387SMichael Stapelberg * this debugging hint is useful to have. 15753871c387SMichael Stapelberg */ 15763871c387SMichael Stapelberg if (err == -EOPNOTSUPP) 15773871c387SMichael Stapelberg netdev_info(dev, "The PHY does not support set_wol, was CONFIG_MARVELL_PHY enabled?\n"); 15783871c387SMichael Stapelberg return err; 15793871c387SMichael Stapelberg } 15803871c387SMichael Stapelberg 1581527a6266SJeff Kirsher static int 1582527a6266SJeff Kirsher mv643xx_eth_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) 1583527a6266SJeff Kirsher { 1584527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1585527a6266SJeff Kirsher 1586527a6266SJeff Kirsher if (mp->phy != NULL) 1587527a6266SJeff Kirsher return mv643xx_eth_get_settings_phy(mp, cmd); 1588527a6266SJeff Kirsher else 1589527a6266SJeff Kirsher return mv643xx_eth_get_settings_phyless(mp, cmd); 1590527a6266SJeff Kirsher } 1591527a6266SJeff Kirsher 1592527a6266SJeff Kirsher static int 1593527a6266SJeff Kirsher mv643xx_eth_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) 1594527a6266SJeff Kirsher { 1595527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1596260055bbSPhil Sutter int ret; 1597527a6266SJeff Kirsher 1598527a6266SJeff Kirsher if (mp->phy == NULL) 1599527a6266SJeff Kirsher return -EINVAL; 1600527a6266SJeff Kirsher 1601527a6266SJeff Kirsher /* 1602527a6266SJeff Kirsher * The MAC does not support 1000baseT_Half. 1603527a6266SJeff Kirsher */ 1604527a6266SJeff Kirsher cmd->advertising &= ~ADVERTISED_1000baseT_Half; 1605527a6266SJeff Kirsher 1606260055bbSPhil Sutter ret = phy_ethtool_sset(mp->phy, cmd); 1607260055bbSPhil Sutter if (!ret) 16080a9e413bSEzequiel Garcia mv643xx_eth_adjust_link(dev); 1609260055bbSPhil Sutter return ret; 1610527a6266SJeff Kirsher } 1611527a6266SJeff Kirsher 1612527a6266SJeff Kirsher static void mv643xx_eth_get_drvinfo(struct net_device *dev, 1613527a6266SJeff Kirsher struct ethtool_drvinfo *drvinfo) 1614527a6266SJeff Kirsher { 16156f39da2cSAxel Lin strlcpy(drvinfo->driver, mv643xx_eth_driver_name, 16166f39da2cSAxel Lin sizeof(drvinfo->driver)); 161768aad78cSRick Jones strlcpy(drvinfo->version, mv643xx_eth_driver_version, 16186f39da2cSAxel Lin sizeof(drvinfo->version)); 16196f39da2cSAxel Lin strlcpy(drvinfo->fw_version, "N/A", sizeof(drvinfo->fw_version)); 16206f39da2cSAxel Lin strlcpy(drvinfo->bus_info, "platform", sizeof(drvinfo->bus_info)); 1621527a6266SJeff Kirsher drvinfo->n_stats = ARRAY_SIZE(mv643xx_eth_stats); 1622527a6266SJeff Kirsher } 1623527a6266SJeff Kirsher 1624527a6266SJeff Kirsher static int mv643xx_eth_nway_reset(struct net_device *dev) 1625527a6266SJeff Kirsher { 1626527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1627527a6266SJeff Kirsher 1628527a6266SJeff Kirsher if (mp->phy == NULL) 1629527a6266SJeff Kirsher return -EINVAL; 1630527a6266SJeff Kirsher 1631527a6266SJeff Kirsher return genphy_restart_aneg(mp->phy); 1632527a6266SJeff Kirsher } 1633527a6266SJeff Kirsher 1634527a6266SJeff Kirsher static int 1635527a6266SJeff Kirsher mv643xx_eth_get_coalesce(struct net_device *dev, struct ethtool_coalesce *ec) 1636527a6266SJeff Kirsher { 1637527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1638527a6266SJeff Kirsher 1639527a6266SJeff Kirsher ec->rx_coalesce_usecs = get_rx_coal(mp); 1640527a6266SJeff Kirsher ec->tx_coalesce_usecs = get_tx_coal(mp); 1641527a6266SJeff Kirsher 1642527a6266SJeff Kirsher return 0; 1643527a6266SJeff Kirsher } 1644527a6266SJeff Kirsher 1645527a6266SJeff Kirsher static int 1646527a6266SJeff Kirsher mv643xx_eth_set_coalesce(struct net_device *dev, struct ethtool_coalesce *ec) 1647527a6266SJeff Kirsher { 1648527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1649527a6266SJeff Kirsher 1650527a6266SJeff Kirsher set_rx_coal(mp, ec->rx_coalesce_usecs); 1651527a6266SJeff Kirsher set_tx_coal(mp, ec->tx_coalesce_usecs); 1652527a6266SJeff Kirsher 1653527a6266SJeff Kirsher return 0; 1654527a6266SJeff Kirsher } 1655527a6266SJeff Kirsher 1656527a6266SJeff Kirsher static void 1657527a6266SJeff Kirsher mv643xx_eth_get_ringparam(struct net_device *dev, struct ethtool_ringparam *er) 1658527a6266SJeff Kirsher { 1659527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1660527a6266SJeff Kirsher 1661527a6266SJeff Kirsher er->rx_max_pending = 4096; 1662527a6266SJeff Kirsher er->tx_max_pending = 4096; 1663527a6266SJeff Kirsher 1664527a6266SJeff Kirsher er->rx_pending = mp->rx_ring_size; 1665527a6266SJeff Kirsher er->tx_pending = mp->tx_ring_size; 1666527a6266SJeff Kirsher } 1667527a6266SJeff Kirsher 1668527a6266SJeff Kirsher static int 1669527a6266SJeff Kirsher mv643xx_eth_set_ringparam(struct net_device *dev, struct ethtool_ringparam *er) 1670527a6266SJeff Kirsher { 1671527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1672527a6266SJeff Kirsher 1673527a6266SJeff Kirsher if (er->rx_mini_pending || er->rx_jumbo_pending) 1674527a6266SJeff Kirsher return -EINVAL; 1675527a6266SJeff Kirsher 1676527a6266SJeff Kirsher mp->rx_ring_size = er->rx_pending < 4096 ? er->rx_pending : 4096; 1677ee9e4956SEzequiel Garcia mp->tx_ring_size = clamp_t(unsigned int, er->tx_pending, 1678ee9e4956SEzequiel Garcia MV643XX_MAX_SKB_DESCS * 2, 4096); 1679ee9e4956SEzequiel Garcia if (mp->tx_ring_size != er->tx_pending) 1680ee9e4956SEzequiel Garcia netdev_warn(dev, "TX queue size set to %u (requested %u)\n", 1681ee9e4956SEzequiel Garcia mp->tx_ring_size, er->tx_pending); 1682527a6266SJeff Kirsher 1683527a6266SJeff Kirsher if (netif_running(dev)) { 1684527a6266SJeff Kirsher mv643xx_eth_stop(dev); 1685527a6266SJeff Kirsher if (mv643xx_eth_open(dev)) { 1686527a6266SJeff Kirsher netdev_err(dev, 1687527a6266SJeff Kirsher "fatal error on re-opening device after ring param change\n"); 1688527a6266SJeff Kirsher return -ENOMEM; 1689527a6266SJeff Kirsher } 1690527a6266SJeff Kirsher } 1691527a6266SJeff Kirsher 1692527a6266SJeff Kirsher return 0; 1693527a6266SJeff Kirsher } 1694527a6266SJeff Kirsher 1695527a6266SJeff Kirsher 1696527a6266SJeff Kirsher static int 1697c8f44affSMichał Mirosław mv643xx_eth_set_features(struct net_device *dev, netdev_features_t features) 1698527a6266SJeff Kirsher { 1699527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 17003ad9b358SMichał Mirosław bool rx_csum = features & NETIF_F_RXCSUM; 1701527a6266SJeff Kirsher 1702527a6266SJeff Kirsher wrlp(mp, PORT_CONFIG, rx_csum ? 0x02000000 : 0x00000000); 1703527a6266SJeff Kirsher 1704527a6266SJeff Kirsher return 0; 1705527a6266SJeff Kirsher } 1706527a6266SJeff Kirsher 1707527a6266SJeff Kirsher static void mv643xx_eth_get_strings(struct net_device *dev, 1708527a6266SJeff Kirsher uint32_t stringset, uint8_t *data) 1709527a6266SJeff Kirsher { 1710527a6266SJeff Kirsher int i; 1711527a6266SJeff Kirsher 1712527a6266SJeff Kirsher if (stringset == ETH_SS_STATS) { 1713527a6266SJeff Kirsher for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) { 1714527a6266SJeff Kirsher memcpy(data + i * ETH_GSTRING_LEN, 1715527a6266SJeff Kirsher mv643xx_eth_stats[i].stat_string, 1716527a6266SJeff Kirsher ETH_GSTRING_LEN); 1717527a6266SJeff Kirsher } 1718527a6266SJeff Kirsher } 1719527a6266SJeff Kirsher } 1720527a6266SJeff Kirsher 1721527a6266SJeff Kirsher static void mv643xx_eth_get_ethtool_stats(struct net_device *dev, 1722527a6266SJeff Kirsher struct ethtool_stats *stats, 1723527a6266SJeff Kirsher uint64_t *data) 1724527a6266SJeff Kirsher { 1725527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1726527a6266SJeff Kirsher int i; 1727527a6266SJeff Kirsher 1728527a6266SJeff Kirsher mv643xx_eth_get_stats(dev); 1729527a6266SJeff Kirsher mib_counters_update(mp); 1730527a6266SJeff Kirsher 1731527a6266SJeff Kirsher for (i = 0; i < ARRAY_SIZE(mv643xx_eth_stats); i++) { 1732527a6266SJeff Kirsher const struct mv643xx_eth_stats *stat; 1733527a6266SJeff Kirsher void *p; 1734527a6266SJeff Kirsher 1735527a6266SJeff Kirsher stat = mv643xx_eth_stats + i; 1736527a6266SJeff Kirsher 1737527a6266SJeff Kirsher if (stat->netdev_off >= 0) 1738527a6266SJeff Kirsher p = ((void *)mp->dev) + stat->netdev_off; 1739527a6266SJeff Kirsher else 1740527a6266SJeff Kirsher p = ((void *)mp) + stat->mp_off; 1741527a6266SJeff Kirsher 1742527a6266SJeff Kirsher data[i] = (stat->sizeof_stat == 8) ? 1743527a6266SJeff Kirsher *(uint64_t *)p : *(uint32_t *)p; 1744527a6266SJeff Kirsher } 1745527a6266SJeff Kirsher } 1746527a6266SJeff Kirsher 1747527a6266SJeff Kirsher static int mv643xx_eth_get_sset_count(struct net_device *dev, int sset) 1748527a6266SJeff Kirsher { 1749527a6266SJeff Kirsher if (sset == ETH_SS_STATS) 1750527a6266SJeff Kirsher return ARRAY_SIZE(mv643xx_eth_stats); 1751527a6266SJeff Kirsher 1752527a6266SJeff Kirsher return -EOPNOTSUPP; 1753527a6266SJeff Kirsher } 1754527a6266SJeff Kirsher 1755527a6266SJeff Kirsher static const struct ethtool_ops mv643xx_eth_ethtool_ops = { 1756527a6266SJeff Kirsher .get_settings = mv643xx_eth_get_settings, 1757527a6266SJeff Kirsher .set_settings = mv643xx_eth_set_settings, 1758527a6266SJeff Kirsher .get_drvinfo = mv643xx_eth_get_drvinfo, 1759527a6266SJeff Kirsher .nway_reset = mv643xx_eth_nway_reset, 1760527a6266SJeff Kirsher .get_link = ethtool_op_get_link, 1761527a6266SJeff Kirsher .get_coalesce = mv643xx_eth_get_coalesce, 1762527a6266SJeff Kirsher .set_coalesce = mv643xx_eth_set_coalesce, 1763527a6266SJeff Kirsher .get_ringparam = mv643xx_eth_get_ringparam, 1764527a6266SJeff Kirsher .set_ringparam = mv643xx_eth_set_ringparam, 1765527a6266SJeff Kirsher .get_strings = mv643xx_eth_get_strings, 1766527a6266SJeff Kirsher .get_ethtool_stats = mv643xx_eth_get_ethtool_stats, 1767527a6266SJeff Kirsher .get_sset_count = mv643xx_eth_get_sset_count, 1768ebad0a8dSRichard Cochran .get_ts_info = ethtool_op_get_ts_info, 17693871c387SMichael Stapelberg .get_wol = mv643xx_eth_get_wol, 17703871c387SMichael Stapelberg .set_wol = mv643xx_eth_set_wol, 1771527a6266SJeff Kirsher }; 1772527a6266SJeff Kirsher 1773527a6266SJeff Kirsher 1774527a6266SJeff Kirsher /* address handling *********************************************************/ 1775527a6266SJeff Kirsher static void uc_addr_get(struct mv643xx_eth_private *mp, unsigned char *addr) 1776527a6266SJeff Kirsher { 1777527a6266SJeff Kirsher unsigned int mac_h = rdlp(mp, MAC_ADDR_HIGH); 1778527a6266SJeff Kirsher unsigned int mac_l = rdlp(mp, MAC_ADDR_LOW); 1779527a6266SJeff Kirsher 1780527a6266SJeff Kirsher addr[0] = (mac_h >> 24) & 0xff; 1781527a6266SJeff Kirsher addr[1] = (mac_h >> 16) & 0xff; 1782527a6266SJeff Kirsher addr[2] = (mac_h >> 8) & 0xff; 1783527a6266SJeff Kirsher addr[3] = mac_h & 0xff; 1784527a6266SJeff Kirsher addr[4] = (mac_l >> 8) & 0xff; 1785527a6266SJeff Kirsher addr[5] = mac_l & 0xff; 1786527a6266SJeff Kirsher } 1787527a6266SJeff Kirsher 1788527a6266SJeff Kirsher static void uc_addr_set(struct mv643xx_eth_private *mp, unsigned char *addr) 1789527a6266SJeff Kirsher { 1790527a6266SJeff Kirsher wrlp(mp, MAC_ADDR_HIGH, 1791527a6266SJeff Kirsher (addr[0] << 24) | (addr[1] << 16) | (addr[2] << 8) | addr[3]); 1792527a6266SJeff Kirsher wrlp(mp, MAC_ADDR_LOW, (addr[4] << 8) | addr[5]); 1793527a6266SJeff Kirsher } 1794527a6266SJeff Kirsher 1795527a6266SJeff Kirsher static u32 uc_addr_filter_mask(struct net_device *dev) 1796527a6266SJeff Kirsher { 1797527a6266SJeff Kirsher struct netdev_hw_addr *ha; 1798527a6266SJeff Kirsher u32 nibbles; 1799527a6266SJeff Kirsher 1800527a6266SJeff Kirsher if (dev->flags & IFF_PROMISC) 1801527a6266SJeff Kirsher return 0; 1802527a6266SJeff Kirsher 1803527a6266SJeff Kirsher nibbles = 1 << (dev->dev_addr[5] & 0x0f); 1804527a6266SJeff Kirsher netdev_for_each_uc_addr(ha, dev) { 1805527a6266SJeff Kirsher if (memcmp(dev->dev_addr, ha->addr, 5)) 1806527a6266SJeff Kirsher return 0; 1807527a6266SJeff Kirsher if ((dev->dev_addr[5] ^ ha->addr[5]) & 0xf0) 1808527a6266SJeff Kirsher return 0; 1809527a6266SJeff Kirsher 1810527a6266SJeff Kirsher nibbles |= 1 << (ha->addr[5] & 0x0f); 1811527a6266SJeff Kirsher } 1812527a6266SJeff Kirsher 1813527a6266SJeff Kirsher return nibbles; 1814527a6266SJeff Kirsher } 1815527a6266SJeff Kirsher 1816527a6266SJeff Kirsher static void mv643xx_eth_program_unicast_filter(struct net_device *dev) 1817527a6266SJeff Kirsher { 1818527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1819527a6266SJeff Kirsher u32 port_config; 1820527a6266SJeff Kirsher u32 nibbles; 1821527a6266SJeff Kirsher int i; 1822527a6266SJeff Kirsher 1823527a6266SJeff Kirsher uc_addr_set(mp, dev->dev_addr); 1824527a6266SJeff Kirsher 1825527a6266SJeff Kirsher port_config = rdlp(mp, PORT_CONFIG) & ~UNICAST_PROMISCUOUS_MODE; 1826527a6266SJeff Kirsher 1827527a6266SJeff Kirsher nibbles = uc_addr_filter_mask(dev); 1828527a6266SJeff Kirsher if (!nibbles) { 1829527a6266SJeff Kirsher port_config |= UNICAST_PROMISCUOUS_MODE; 1830527a6266SJeff Kirsher nibbles = 0xffff; 1831527a6266SJeff Kirsher } 1832527a6266SJeff Kirsher 1833527a6266SJeff Kirsher for (i = 0; i < 16; i += 4) { 1834527a6266SJeff Kirsher int off = UNICAST_TABLE(mp->port_num) + i; 1835527a6266SJeff Kirsher u32 v; 1836527a6266SJeff Kirsher 1837527a6266SJeff Kirsher v = 0; 1838527a6266SJeff Kirsher if (nibbles & 1) 1839527a6266SJeff Kirsher v |= 0x00000001; 1840527a6266SJeff Kirsher if (nibbles & 2) 1841527a6266SJeff Kirsher v |= 0x00000100; 1842527a6266SJeff Kirsher if (nibbles & 4) 1843527a6266SJeff Kirsher v |= 0x00010000; 1844527a6266SJeff Kirsher if (nibbles & 8) 1845527a6266SJeff Kirsher v |= 0x01000000; 1846527a6266SJeff Kirsher nibbles >>= 4; 1847527a6266SJeff Kirsher 1848527a6266SJeff Kirsher wrl(mp, off, v); 1849527a6266SJeff Kirsher } 1850527a6266SJeff Kirsher 1851527a6266SJeff Kirsher wrlp(mp, PORT_CONFIG, port_config); 1852527a6266SJeff Kirsher } 1853527a6266SJeff Kirsher 1854527a6266SJeff Kirsher static int addr_crc(unsigned char *addr) 1855527a6266SJeff Kirsher { 1856527a6266SJeff Kirsher int crc = 0; 1857527a6266SJeff Kirsher int i; 1858527a6266SJeff Kirsher 1859527a6266SJeff Kirsher for (i = 0; i < 6; i++) { 1860527a6266SJeff Kirsher int j; 1861527a6266SJeff Kirsher 1862527a6266SJeff Kirsher crc = (crc ^ addr[i]) << 8; 1863527a6266SJeff Kirsher for (j = 7; j >= 0; j--) { 1864527a6266SJeff Kirsher if (crc & (0x100 << j)) 1865527a6266SJeff Kirsher crc ^= 0x107 << j; 1866527a6266SJeff Kirsher } 1867527a6266SJeff Kirsher } 1868527a6266SJeff Kirsher 1869527a6266SJeff Kirsher return crc; 1870527a6266SJeff Kirsher } 1871527a6266SJeff Kirsher 1872527a6266SJeff Kirsher static void mv643xx_eth_program_multicast_filter(struct net_device *dev) 1873527a6266SJeff Kirsher { 1874527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 1875527a6266SJeff Kirsher u32 *mc_spec; 1876527a6266SJeff Kirsher u32 *mc_other; 1877527a6266SJeff Kirsher struct netdev_hw_addr *ha; 1878527a6266SJeff Kirsher int i; 1879527a6266SJeff Kirsher 1880527a6266SJeff Kirsher if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) { 1881527a6266SJeff Kirsher int port_num; 1882527a6266SJeff Kirsher u32 accept; 1883527a6266SJeff Kirsher 1884527a6266SJeff Kirsher oom: 1885527a6266SJeff Kirsher port_num = mp->port_num; 1886527a6266SJeff Kirsher accept = 0x01010101; 1887527a6266SJeff Kirsher for (i = 0; i < 0x100; i += 4) { 1888527a6266SJeff Kirsher wrl(mp, SPECIAL_MCAST_TABLE(port_num) + i, accept); 1889527a6266SJeff Kirsher wrl(mp, OTHER_MCAST_TABLE(port_num) + i, accept); 1890527a6266SJeff Kirsher } 1891527a6266SJeff Kirsher return; 1892527a6266SJeff Kirsher } 1893527a6266SJeff Kirsher 1894b66a6085SRasmus Villemoes mc_spec = kzalloc(0x200, GFP_ATOMIC); 1895527a6266SJeff Kirsher if (mc_spec == NULL) 1896527a6266SJeff Kirsher goto oom; 1897527a6266SJeff Kirsher mc_other = mc_spec + (0x100 >> 2); 1898527a6266SJeff Kirsher 1899527a6266SJeff Kirsher netdev_for_each_mc_addr(ha, dev) { 1900527a6266SJeff Kirsher u8 *a = ha->addr; 1901527a6266SJeff Kirsher u32 *table; 1902527a6266SJeff Kirsher int entry; 1903527a6266SJeff Kirsher 1904527a6266SJeff Kirsher if (memcmp(a, "\x01\x00\x5e\x00\x00", 5) == 0) { 1905527a6266SJeff Kirsher table = mc_spec; 1906527a6266SJeff Kirsher entry = a[5]; 1907527a6266SJeff Kirsher } else { 1908527a6266SJeff Kirsher table = mc_other; 1909527a6266SJeff Kirsher entry = addr_crc(a); 1910527a6266SJeff Kirsher } 1911527a6266SJeff Kirsher 1912527a6266SJeff Kirsher table[entry >> 2] |= 1 << (8 * (entry & 3)); 1913527a6266SJeff Kirsher } 1914527a6266SJeff Kirsher 1915527a6266SJeff Kirsher for (i = 0; i < 0x100; i += 4) { 1916527a6266SJeff Kirsher wrl(mp, SPECIAL_MCAST_TABLE(mp->port_num) + i, mc_spec[i >> 2]); 1917527a6266SJeff Kirsher wrl(mp, OTHER_MCAST_TABLE(mp->port_num) + i, mc_other[i >> 2]); 1918527a6266SJeff Kirsher } 1919527a6266SJeff Kirsher 1920527a6266SJeff Kirsher kfree(mc_spec); 1921527a6266SJeff Kirsher } 1922527a6266SJeff Kirsher 1923527a6266SJeff Kirsher static void mv643xx_eth_set_rx_mode(struct net_device *dev) 1924527a6266SJeff Kirsher { 1925527a6266SJeff Kirsher mv643xx_eth_program_unicast_filter(dev); 1926527a6266SJeff Kirsher mv643xx_eth_program_multicast_filter(dev); 1927527a6266SJeff Kirsher } 1928527a6266SJeff Kirsher 1929527a6266SJeff Kirsher static int mv643xx_eth_set_mac_address(struct net_device *dev, void *addr) 1930527a6266SJeff Kirsher { 1931527a6266SJeff Kirsher struct sockaddr *sa = addr; 1932527a6266SJeff Kirsher 1933527a6266SJeff Kirsher if (!is_valid_ether_addr(sa->sa_data)) 1934504f9b5aSDanny Kukawka return -EADDRNOTAVAIL; 1935527a6266SJeff Kirsher 1936527a6266SJeff Kirsher memcpy(dev->dev_addr, sa->sa_data, ETH_ALEN); 1937527a6266SJeff Kirsher 1938527a6266SJeff Kirsher netif_addr_lock_bh(dev); 1939527a6266SJeff Kirsher mv643xx_eth_program_unicast_filter(dev); 1940527a6266SJeff Kirsher netif_addr_unlock_bh(dev); 1941527a6266SJeff Kirsher 1942527a6266SJeff Kirsher return 0; 1943527a6266SJeff Kirsher } 1944527a6266SJeff Kirsher 1945527a6266SJeff Kirsher 1946527a6266SJeff Kirsher /* rx/tx queue initialisation ***********************************************/ 1947527a6266SJeff Kirsher static int rxq_init(struct mv643xx_eth_private *mp, int index) 1948527a6266SJeff Kirsher { 1949527a6266SJeff Kirsher struct rx_queue *rxq = mp->rxq + index; 1950527a6266SJeff Kirsher struct rx_desc *rx_desc; 1951527a6266SJeff Kirsher int size; 1952527a6266SJeff Kirsher int i; 1953527a6266SJeff Kirsher 1954527a6266SJeff Kirsher rxq->index = index; 1955527a6266SJeff Kirsher 1956527a6266SJeff Kirsher rxq->rx_ring_size = mp->rx_ring_size; 1957527a6266SJeff Kirsher 1958527a6266SJeff Kirsher rxq->rx_desc_count = 0; 1959527a6266SJeff Kirsher rxq->rx_curr_desc = 0; 1960527a6266SJeff Kirsher rxq->rx_used_desc = 0; 1961527a6266SJeff Kirsher 1962527a6266SJeff Kirsher size = rxq->rx_ring_size * sizeof(struct rx_desc); 1963527a6266SJeff Kirsher 1964527a6266SJeff Kirsher if (index == 0 && size <= mp->rx_desc_sram_size) { 1965527a6266SJeff Kirsher rxq->rx_desc_area = ioremap(mp->rx_desc_sram_addr, 1966527a6266SJeff Kirsher mp->rx_desc_sram_size); 1967527a6266SJeff Kirsher rxq->rx_desc_dma = mp->rx_desc_sram_addr; 1968527a6266SJeff Kirsher } else { 1969527a6266SJeff Kirsher rxq->rx_desc_area = dma_alloc_coherent(mp->dev->dev.parent, 1970527a6266SJeff Kirsher size, &rxq->rx_desc_dma, 1971527a6266SJeff Kirsher GFP_KERNEL); 1972527a6266SJeff Kirsher } 1973527a6266SJeff Kirsher 1974527a6266SJeff Kirsher if (rxq->rx_desc_area == NULL) { 1975527a6266SJeff Kirsher netdev_err(mp->dev, 1976527a6266SJeff Kirsher "can't allocate rx ring (%d bytes)\n", size); 1977527a6266SJeff Kirsher goto out; 1978527a6266SJeff Kirsher } 1979527a6266SJeff Kirsher memset(rxq->rx_desc_area, 0, size); 1980527a6266SJeff Kirsher 1981527a6266SJeff Kirsher rxq->rx_desc_area_size = size; 19829fa8e980SLubomir Rintel rxq->rx_skb = kcalloc(rxq->rx_ring_size, sizeof(*rxq->rx_skb), 1983527a6266SJeff Kirsher GFP_KERNEL); 1984b2adaca9SJoe Perches if (rxq->rx_skb == NULL) 1985527a6266SJeff Kirsher goto out_free; 1986527a6266SJeff Kirsher 198764699336SJoe Perches rx_desc = rxq->rx_desc_area; 1988527a6266SJeff Kirsher for (i = 0; i < rxq->rx_ring_size; i++) { 1989527a6266SJeff Kirsher int nexti; 1990527a6266SJeff Kirsher 1991527a6266SJeff Kirsher nexti = i + 1; 1992527a6266SJeff Kirsher if (nexti == rxq->rx_ring_size) 1993527a6266SJeff Kirsher nexti = 0; 1994527a6266SJeff Kirsher 1995527a6266SJeff Kirsher rx_desc[i].next_desc_ptr = rxq->rx_desc_dma + 1996527a6266SJeff Kirsher nexti * sizeof(struct rx_desc); 1997527a6266SJeff Kirsher } 1998527a6266SJeff Kirsher 1999527a6266SJeff Kirsher return 0; 2000527a6266SJeff Kirsher 2001527a6266SJeff Kirsher 2002527a6266SJeff Kirsher out_free: 2003527a6266SJeff Kirsher if (index == 0 && size <= mp->rx_desc_sram_size) 2004527a6266SJeff Kirsher iounmap(rxq->rx_desc_area); 2005527a6266SJeff Kirsher else 2006527a6266SJeff Kirsher dma_free_coherent(mp->dev->dev.parent, size, 2007527a6266SJeff Kirsher rxq->rx_desc_area, 2008527a6266SJeff Kirsher rxq->rx_desc_dma); 2009527a6266SJeff Kirsher 2010527a6266SJeff Kirsher out: 2011527a6266SJeff Kirsher return -ENOMEM; 2012527a6266SJeff Kirsher } 2013527a6266SJeff Kirsher 2014527a6266SJeff Kirsher static void rxq_deinit(struct rx_queue *rxq) 2015527a6266SJeff Kirsher { 2016527a6266SJeff Kirsher struct mv643xx_eth_private *mp = rxq_to_mp(rxq); 2017527a6266SJeff Kirsher int i; 2018527a6266SJeff Kirsher 2019527a6266SJeff Kirsher rxq_disable(rxq); 2020527a6266SJeff Kirsher 2021527a6266SJeff Kirsher for (i = 0; i < rxq->rx_ring_size; i++) { 2022527a6266SJeff Kirsher if (rxq->rx_skb[i]) { 2023527a6266SJeff Kirsher dev_kfree_skb(rxq->rx_skb[i]); 2024527a6266SJeff Kirsher rxq->rx_desc_count--; 2025527a6266SJeff Kirsher } 2026527a6266SJeff Kirsher } 2027527a6266SJeff Kirsher 2028527a6266SJeff Kirsher if (rxq->rx_desc_count) { 2029527a6266SJeff Kirsher netdev_err(mp->dev, "error freeing rx ring -- %d skbs stuck\n", 2030527a6266SJeff Kirsher rxq->rx_desc_count); 2031527a6266SJeff Kirsher } 2032527a6266SJeff Kirsher 2033527a6266SJeff Kirsher if (rxq->index == 0 && 2034527a6266SJeff Kirsher rxq->rx_desc_area_size <= mp->rx_desc_sram_size) 2035527a6266SJeff Kirsher iounmap(rxq->rx_desc_area); 2036527a6266SJeff Kirsher else 2037527a6266SJeff Kirsher dma_free_coherent(mp->dev->dev.parent, rxq->rx_desc_area_size, 2038527a6266SJeff Kirsher rxq->rx_desc_area, rxq->rx_desc_dma); 2039527a6266SJeff Kirsher 2040527a6266SJeff Kirsher kfree(rxq->rx_skb); 2041527a6266SJeff Kirsher } 2042527a6266SJeff Kirsher 2043527a6266SJeff Kirsher static int txq_init(struct mv643xx_eth_private *mp, int index) 2044527a6266SJeff Kirsher { 2045527a6266SJeff Kirsher struct tx_queue *txq = mp->txq + index; 2046527a6266SJeff Kirsher struct tx_desc *tx_desc; 2047527a6266SJeff Kirsher int size; 20489e911414SEzequiel Garcia int ret; 2049527a6266SJeff Kirsher int i; 2050527a6266SJeff Kirsher 2051527a6266SJeff Kirsher txq->index = index; 2052527a6266SJeff Kirsher 2053527a6266SJeff Kirsher txq->tx_ring_size = mp->tx_ring_size; 2054527a6266SJeff Kirsher 2055ee9e4956SEzequiel Garcia /* A queue must always have room for at least one skb. 2056ee9e4956SEzequiel Garcia * Therefore, stop the queue when the free entries reaches 2057ee9e4956SEzequiel Garcia * the maximum number of descriptors per skb. 2058ee9e4956SEzequiel Garcia */ 2059ee9e4956SEzequiel Garcia txq->tx_stop_threshold = txq->tx_ring_size - MV643XX_MAX_SKB_DESCS; 2060ee9e4956SEzequiel Garcia txq->tx_wake_threshold = txq->tx_stop_threshold / 2; 2061ee9e4956SEzequiel Garcia 2062527a6266SJeff Kirsher txq->tx_desc_count = 0; 2063527a6266SJeff Kirsher txq->tx_curr_desc = 0; 2064527a6266SJeff Kirsher txq->tx_used_desc = 0; 2065527a6266SJeff Kirsher 2066527a6266SJeff Kirsher size = txq->tx_ring_size * sizeof(struct tx_desc); 2067527a6266SJeff Kirsher 2068527a6266SJeff Kirsher if (index == 0 && size <= mp->tx_desc_sram_size) { 2069527a6266SJeff Kirsher txq->tx_desc_area = ioremap(mp->tx_desc_sram_addr, 2070527a6266SJeff Kirsher mp->tx_desc_sram_size); 2071527a6266SJeff Kirsher txq->tx_desc_dma = mp->tx_desc_sram_addr; 2072527a6266SJeff Kirsher } else { 2073527a6266SJeff Kirsher txq->tx_desc_area = dma_alloc_coherent(mp->dev->dev.parent, 2074527a6266SJeff Kirsher size, &txq->tx_desc_dma, 2075527a6266SJeff Kirsher GFP_KERNEL); 2076527a6266SJeff Kirsher } 2077527a6266SJeff Kirsher 2078527a6266SJeff Kirsher if (txq->tx_desc_area == NULL) { 2079527a6266SJeff Kirsher netdev_err(mp->dev, 2080527a6266SJeff Kirsher "can't allocate tx ring (%d bytes)\n", size); 2081527a6266SJeff Kirsher return -ENOMEM; 2082527a6266SJeff Kirsher } 2083527a6266SJeff Kirsher memset(txq->tx_desc_area, 0, size); 2084527a6266SJeff Kirsher 2085527a6266SJeff Kirsher txq->tx_desc_area_size = size; 2086527a6266SJeff Kirsher 208764699336SJoe Perches tx_desc = txq->tx_desc_area; 2088527a6266SJeff Kirsher for (i = 0; i < txq->tx_ring_size; i++) { 2089527a6266SJeff Kirsher struct tx_desc *txd = tx_desc + i; 2090527a6266SJeff Kirsher int nexti; 2091527a6266SJeff Kirsher 2092527a6266SJeff Kirsher nexti = i + 1; 2093527a6266SJeff Kirsher if (nexti == txq->tx_ring_size) 2094527a6266SJeff Kirsher nexti = 0; 2095527a6266SJeff Kirsher 2096527a6266SJeff Kirsher txd->cmd_sts = 0; 2097527a6266SJeff Kirsher txd->next_desc_ptr = txq->tx_desc_dma + 2098527a6266SJeff Kirsher nexti * sizeof(struct tx_desc); 2099527a6266SJeff Kirsher } 2100527a6266SJeff Kirsher 21019e911414SEzequiel Garcia txq->tx_desc_mapping = kcalloc(txq->tx_ring_size, sizeof(char), 21029e911414SEzequiel Garcia GFP_KERNEL); 21039e911414SEzequiel Garcia if (!txq->tx_desc_mapping) { 21049e911414SEzequiel Garcia ret = -ENOMEM; 21059e911414SEzequiel Garcia goto err_free_desc_area; 21069e911414SEzequiel Garcia } 21079e911414SEzequiel Garcia 21083ae8f4e0SEzequiel Garcia /* Allocate DMA buffers for TSO MAC/IP/TCP headers */ 21093ae8f4e0SEzequiel Garcia txq->tso_hdrs = dma_alloc_coherent(mp->dev->dev.parent, 21103ae8f4e0SEzequiel Garcia txq->tx_ring_size * TSO_HEADER_SIZE, 21113ae8f4e0SEzequiel Garcia &txq->tso_hdrs_dma, GFP_KERNEL); 21123ae8f4e0SEzequiel Garcia if (txq->tso_hdrs == NULL) { 21139e911414SEzequiel Garcia ret = -ENOMEM; 21149e911414SEzequiel Garcia goto err_free_desc_mapping; 21153ae8f4e0SEzequiel Garcia } 2116527a6266SJeff Kirsher skb_queue_head_init(&txq->tx_skb); 2117527a6266SJeff Kirsher 2118527a6266SJeff Kirsher return 0; 21199e911414SEzequiel Garcia 21209e911414SEzequiel Garcia err_free_desc_mapping: 21219e911414SEzequiel Garcia kfree(txq->tx_desc_mapping); 21229e911414SEzequiel Garcia err_free_desc_area: 21239e911414SEzequiel Garcia if (index == 0 && size <= mp->tx_desc_sram_size) 21249e911414SEzequiel Garcia iounmap(txq->tx_desc_area); 21259e911414SEzequiel Garcia else 21269e911414SEzequiel Garcia dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size, 21279e911414SEzequiel Garcia txq->tx_desc_area, txq->tx_desc_dma); 21289e911414SEzequiel Garcia return ret; 2129527a6266SJeff Kirsher } 2130527a6266SJeff Kirsher 2131527a6266SJeff Kirsher static void txq_deinit(struct tx_queue *txq) 2132527a6266SJeff Kirsher { 2133527a6266SJeff Kirsher struct mv643xx_eth_private *mp = txq_to_mp(txq); 2134527a6266SJeff Kirsher 2135527a6266SJeff Kirsher txq_disable(txq); 2136527a6266SJeff Kirsher txq_reclaim(txq, txq->tx_ring_size, 1); 2137527a6266SJeff Kirsher 2138527a6266SJeff Kirsher BUG_ON(txq->tx_used_desc != txq->tx_curr_desc); 2139527a6266SJeff Kirsher 2140527a6266SJeff Kirsher if (txq->index == 0 && 2141527a6266SJeff Kirsher txq->tx_desc_area_size <= mp->tx_desc_sram_size) 2142527a6266SJeff Kirsher iounmap(txq->tx_desc_area); 2143527a6266SJeff Kirsher else 2144527a6266SJeff Kirsher dma_free_coherent(mp->dev->dev.parent, txq->tx_desc_area_size, 2145527a6266SJeff Kirsher txq->tx_desc_area, txq->tx_desc_dma); 21469e911414SEzequiel Garcia kfree(txq->tx_desc_mapping); 21479e911414SEzequiel Garcia 21483ae8f4e0SEzequiel Garcia if (txq->tso_hdrs) 21493ae8f4e0SEzequiel Garcia dma_free_coherent(mp->dev->dev.parent, 21503ae8f4e0SEzequiel Garcia txq->tx_ring_size * TSO_HEADER_SIZE, 21513ae8f4e0SEzequiel Garcia txq->tso_hdrs, txq->tso_hdrs_dma); 2152527a6266SJeff Kirsher } 2153527a6266SJeff Kirsher 2154527a6266SJeff Kirsher 2155527a6266SJeff Kirsher /* netdev ops and related ***************************************************/ 2156527a6266SJeff Kirsher static int mv643xx_eth_collect_events(struct mv643xx_eth_private *mp) 2157527a6266SJeff Kirsher { 2158527a6266SJeff Kirsher u32 int_cause; 2159527a6266SJeff Kirsher u32 int_cause_ext; 2160527a6266SJeff Kirsher 2161527a6266SJeff Kirsher int_cause = rdlp(mp, INT_CAUSE) & mp->int_mask; 2162527a6266SJeff Kirsher if (int_cause == 0) 2163527a6266SJeff Kirsher return 0; 2164527a6266SJeff Kirsher 2165527a6266SJeff Kirsher int_cause_ext = 0; 2166527a6266SJeff Kirsher if (int_cause & INT_EXT) { 2167527a6266SJeff Kirsher int_cause &= ~INT_EXT; 2168527a6266SJeff Kirsher int_cause_ext = rdlp(mp, INT_CAUSE_EXT); 2169527a6266SJeff Kirsher } 2170527a6266SJeff Kirsher 2171527a6266SJeff Kirsher if (int_cause) { 2172527a6266SJeff Kirsher wrlp(mp, INT_CAUSE, ~int_cause); 2173527a6266SJeff Kirsher mp->work_tx_end |= ((int_cause & INT_TX_END) >> 19) & 2174527a6266SJeff Kirsher ~(rdlp(mp, TXQ_COMMAND) & 0xff); 2175527a6266SJeff Kirsher mp->work_rx |= (int_cause & INT_RX) >> 2; 2176527a6266SJeff Kirsher } 2177527a6266SJeff Kirsher 2178527a6266SJeff Kirsher int_cause_ext &= INT_EXT_LINK_PHY | INT_EXT_TX; 2179527a6266SJeff Kirsher if (int_cause_ext) { 2180527a6266SJeff Kirsher wrlp(mp, INT_CAUSE_EXT, ~int_cause_ext); 2181527a6266SJeff Kirsher if (int_cause_ext & INT_EXT_LINK_PHY) 2182527a6266SJeff Kirsher mp->work_link = 1; 2183527a6266SJeff Kirsher mp->work_tx |= int_cause_ext & INT_EXT_TX; 2184527a6266SJeff Kirsher } 2185527a6266SJeff Kirsher 2186527a6266SJeff Kirsher return 1; 2187527a6266SJeff Kirsher } 2188527a6266SJeff Kirsher 2189527a6266SJeff Kirsher static irqreturn_t mv643xx_eth_irq(int irq, void *dev_id) 2190527a6266SJeff Kirsher { 2191527a6266SJeff Kirsher struct net_device *dev = (struct net_device *)dev_id; 2192527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2193527a6266SJeff Kirsher 2194527a6266SJeff Kirsher if (unlikely(!mv643xx_eth_collect_events(mp))) 2195527a6266SJeff Kirsher return IRQ_NONE; 2196527a6266SJeff Kirsher 2197527a6266SJeff Kirsher wrlp(mp, INT_MASK, 0); 2198527a6266SJeff Kirsher napi_schedule(&mp->napi); 2199527a6266SJeff Kirsher 2200527a6266SJeff Kirsher return IRQ_HANDLED; 2201527a6266SJeff Kirsher } 2202527a6266SJeff Kirsher 2203527a6266SJeff Kirsher static void handle_link_event(struct mv643xx_eth_private *mp) 2204527a6266SJeff Kirsher { 2205527a6266SJeff Kirsher struct net_device *dev = mp->dev; 2206527a6266SJeff Kirsher u32 port_status; 2207527a6266SJeff Kirsher int speed; 2208527a6266SJeff Kirsher int duplex; 2209527a6266SJeff Kirsher int fc; 2210527a6266SJeff Kirsher 2211527a6266SJeff Kirsher port_status = rdlp(mp, PORT_STATUS); 2212527a6266SJeff Kirsher if (!(port_status & LINK_UP)) { 2213527a6266SJeff Kirsher if (netif_carrier_ok(dev)) { 2214527a6266SJeff Kirsher int i; 2215527a6266SJeff Kirsher 2216527a6266SJeff Kirsher netdev_info(dev, "link down\n"); 2217527a6266SJeff Kirsher 2218527a6266SJeff Kirsher netif_carrier_off(dev); 2219527a6266SJeff Kirsher 2220527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) { 2221527a6266SJeff Kirsher struct tx_queue *txq = mp->txq + i; 2222527a6266SJeff Kirsher 2223527a6266SJeff Kirsher txq_reclaim(txq, txq->tx_ring_size, 1); 2224527a6266SJeff Kirsher txq_reset_hw_ptr(txq); 2225527a6266SJeff Kirsher } 2226527a6266SJeff Kirsher } 2227527a6266SJeff Kirsher return; 2228527a6266SJeff Kirsher } 2229527a6266SJeff Kirsher 2230527a6266SJeff Kirsher switch (port_status & PORT_SPEED_MASK) { 2231527a6266SJeff Kirsher case PORT_SPEED_10: 2232527a6266SJeff Kirsher speed = 10; 2233527a6266SJeff Kirsher break; 2234527a6266SJeff Kirsher case PORT_SPEED_100: 2235527a6266SJeff Kirsher speed = 100; 2236527a6266SJeff Kirsher break; 2237527a6266SJeff Kirsher case PORT_SPEED_1000: 2238527a6266SJeff Kirsher speed = 1000; 2239527a6266SJeff Kirsher break; 2240527a6266SJeff Kirsher default: 2241527a6266SJeff Kirsher speed = -1; 2242527a6266SJeff Kirsher break; 2243527a6266SJeff Kirsher } 2244527a6266SJeff Kirsher duplex = (port_status & FULL_DUPLEX) ? 1 : 0; 2245527a6266SJeff Kirsher fc = (port_status & FLOW_CONTROL_ENABLED) ? 1 : 0; 2246527a6266SJeff Kirsher 2247527a6266SJeff Kirsher netdev_info(dev, "link up, %d Mb/s, %s duplex, flow control %sabled\n", 2248527a6266SJeff Kirsher speed, duplex ? "full" : "half", fc ? "en" : "dis"); 2249527a6266SJeff Kirsher 2250527a6266SJeff Kirsher if (!netif_carrier_ok(dev)) 2251527a6266SJeff Kirsher netif_carrier_on(dev); 2252527a6266SJeff Kirsher } 2253527a6266SJeff Kirsher 2254527a6266SJeff Kirsher static int mv643xx_eth_poll(struct napi_struct *napi, int budget) 2255527a6266SJeff Kirsher { 2256527a6266SJeff Kirsher struct mv643xx_eth_private *mp; 2257527a6266SJeff Kirsher int work_done; 2258527a6266SJeff Kirsher 2259527a6266SJeff Kirsher mp = container_of(napi, struct mv643xx_eth_private, napi); 2260527a6266SJeff Kirsher 2261527a6266SJeff Kirsher if (unlikely(mp->oom)) { 2262527a6266SJeff Kirsher mp->oom = 0; 2263527a6266SJeff Kirsher del_timer(&mp->rx_oom); 2264527a6266SJeff Kirsher } 2265527a6266SJeff Kirsher 2266527a6266SJeff Kirsher work_done = 0; 2267527a6266SJeff Kirsher while (work_done < budget) { 2268527a6266SJeff Kirsher u8 queue_mask; 2269527a6266SJeff Kirsher int queue; 2270527a6266SJeff Kirsher int work_tbd; 2271527a6266SJeff Kirsher 2272527a6266SJeff Kirsher if (mp->work_link) { 2273527a6266SJeff Kirsher mp->work_link = 0; 2274527a6266SJeff Kirsher handle_link_event(mp); 2275527a6266SJeff Kirsher work_done++; 2276527a6266SJeff Kirsher continue; 2277527a6266SJeff Kirsher } 2278527a6266SJeff Kirsher 2279527a6266SJeff Kirsher queue_mask = mp->work_tx | mp->work_tx_end | mp->work_rx; 2280527a6266SJeff Kirsher if (likely(!mp->oom)) 2281527a6266SJeff Kirsher queue_mask |= mp->work_rx_refill; 2282527a6266SJeff Kirsher 2283527a6266SJeff Kirsher if (!queue_mask) { 2284527a6266SJeff Kirsher if (mv643xx_eth_collect_events(mp)) 2285527a6266SJeff Kirsher continue; 2286527a6266SJeff Kirsher break; 2287527a6266SJeff Kirsher } 2288527a6266SJeff Kirsher 2289527a6266SJeff Kirsher queue = fls(queue_mask) - 1; 2290527a6266SJeff Kirsher queue_mask = 1 << queue; 2291527a6266SJeff Kirsher 2292527a6266SJeff Kirsher work_tbd = budget - work_done; 2293527a6266SJeff Kirsher if (work_tbd > 16) 2294527a6266SJeff Kirsher work_tbd = 16; 2295527a6266SJeff Kirsher 2296527a6266SJeff Kirsher if (mp->work_tx_end & queue_mask) { 2297527a6266SJeff Kirsher txq_kick(mp->txq + queue); 2298527a6266SJeff Kirsher } else if (mp->work_tx & queue_mask) { 2299527a6266SJeff Kirsher work_done += txq_reclaim(mp->txq + queue, work_tbd, 0); 2300527a6266SJeff Kirsher txq_maybe_wake(mp->txq + queue); 2301527a6266SJeff Kirsher } else if (mp->work_rx & queue_mask) { 2302527a6266SJeff Kirsher work_done += rxq_process(mp->rxq + queue, work_tbd); 2303527a6266SJeff Kirsher } else if (!mp->oom && (mp->work_rx_refill & queue_mask)) { 2304527a6266SJeff Kirsher work_done += rxq_refill(mp->rxq + queue, work_tbd); 2305527a6266SJeff Kirsher } else { 2306527a6266SJeff Kirsher BUG(); 2307527a6266SJeff Kirsher } 2308527a6266SJeff Kirsher } 2309527a6266SJeff Kirsher 2310527a6266SJeff Kirsher if (work_done < budget) { 2311527a6266SJeff Kirsher if (mp->oom) 2312527a6266SJeff Kirsher mod_timer(&mp->rx_oom, jiffies + (HZ / 10)); 2313527a6266SJeff Kirsher napi_complete(napi); 2314527a6266SJeff Kirsher wrlp(mp, INT_MASK, mp->int_mask); 2315527a6266SJeff Kirsher } 2316527a6266SJeff Kirsher 2317527a6266SJeff Kirsher return work_done; 2318527a6266SJeff Kirsher } 2319527a6266SJeff Kirsher 2320527a6266SJeff Kirsher static inline void oom_timer_wrapper(unsigned long data) 2321527a6266SJeff Kirsher { 2322527a6266SJeff Kirsher struct mv643xx_eth_private *mp = (void *)data; 2323527a6266SJeff Kirsher 2324527a6266SJeff Kirsher napi_schedule(&mp->napi); 2325527a6266SJeff Kirsher } 2326527a6266SJeff Kirsher 2327527a6266SJeff Kirsher static void port_start(struct mv643xx_eth_private *mp) 2328527a6266SJeff Kirsher { 2329527a6266SJeff Kirsher u32 pscr; 2330527a6266SJeff Kirsher int i; 2331527a6266SJeff Kirsher 2332527a6266SJeff Kirsher /* 2333527a6266SJeff Kirsher * Perform PHY reset, if there is a PHY. 2334527a6266SJeff Kirsher */ 2335527a6266SJeff Kirsher if (mp->phy != NULL) { 2336527a6266SJeff Kirsher struct ethtool_cmd cmd; 2337527a6266SJeff Kirsher 2338527a6266SJeff Kirsher mv643xx_eth_get_settings(mp->dev, &cmd); 23397cd14636SFlorian Fainelli phy_init_hw(mp->phy); 2340527a6266SJeff Kirsher mv643xx_eth_set_settings(mp->dev, &cmd); 234158911151SSebastian Hesselbarth phy_start(mp->phy); 2342527a6266SJeff Kirsher } 2343527a6266SJeff Kirsher 2344527a6266SJeff Kirsher /* 2345527a6266SJeff Kirsher * Configure basic link parameters. 2346527a6266SJeff Kirsher */ 2347527a6266SJeff Kirsher pscr = rdlp(mp, PORT_SERIAL_CONTROL); 2348527a6266SJeff Kirsher 2349527a6266SJeff Kirsher pscr |= SERIAL_PORT_ENABLE; 2350527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, pscr); 2351527a6266SJeff Kirsher 2352527a6266SJeff Kirsher pscr |= DO_NOT_FORCE_LINK_FAIL; 2353527a6266SJeff Kirsher if (mp->phy == NULL) 2354527a6266SJeff Kirsher pscr |= FORCE_LINK_PASS; 2355527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, pscr); 2356527a6266SJeff Kirsher 2357527a6266SJeff Kirsher /* 2358527a6266SJeff Kirsher * Configure TX path and queues. 2359527a6266SJeff Kirsher */ 2360527a6266SJeff Kirsher tx_set_rate(mp, 1000000000, 16777216); 2361527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) { 2362527a6266SJeff Kirsher struct tx_queue *txq = mp->txq + i; 2363527a6266SJeff Kirsher 2364527a6266SJeff Kirsher txq_reset_hw_ptr(txq); 2365527a6266SJeff Kirsher txq_set_rate(txq, 1000000000, 16777216); 2366527a6266SJeff Kirsher txq_set_fixed_prio_mode(txq); 2367527a6266SJeff Kirsher } 2368527a6266SJeff Kirsher 2369527a6266SJeff Kirsher /* 2370527a6266SJeff Kirsher * Receive all unmatched unicast, TCP, UDP, BPDU and broadcast 2371527a6266SJeff Kirsher * frames to RX queue #0, and include the pseudo-header when 2372527a6266SJeff Kirsher * calculating receive checksums. 2373527a6266SJeff Kirsher */ 2374527a6266SJeff Kirsher mv643xx_eth_set_features(mp->dev, mp->dev->features); 2375527a6266SJeff Kirsher 2376527a6266SJeff Kirsher /* 2377527a6266SJeff Kirsher * Treat BPDUs as normal multicasts, and disable partition mode. 2378527a6266SJeff Kirsher */ 2379527a6266SJeff Kirsher wrlp(mp, PORT_CONFIG_EXT, 0x00000000); 2380527a6266SJeff Kirsher 2381527a6266SJeff Kirsher /* 2382527a6266SJeff Kirsher * Add configured unicast addresses to address filter table. 2383527a6266SJeff Kirsher */ 2384527a6266SJeff Kirsher mv643xx_eth_program_unicast_filter(mp->dev); 2385527a6266SJeff Kirsher 2386527a6266SJeff Kirsher /* 2387527a6266SJeff Kirsher * Enable the receive queues. 2388527a6266SJeff Kirsher */ 2389527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) { 2390527a6266SJeff Kirsher struct rx_queue *rxq = mp->rxq + i; 2391527a6266SJeff Kirsher u32 addr; 2392527a6266SJeff Kirsher 2393527a6266SJeff Kirsher addr = (u32)rxq->rx_desc_dma; 2394527a6266SJeff Kirsher addr += rxq->rx_curr_desc * sizeof(struct rx_desc); 2395527a6266SJeff Kirsher wrlp(mp, RXQ_CURRENT_DESC_PTR(i), addr); 2396527a6266SJeff Kirsher 2397527a6266SJeff Kirsher rxq_enable(rxq); 2398527a6266SJeff Kirsher } 2399527a6266SJeff Kirsher } 2400527a6266SJeff Kirsher 2401527a6266SJeff Kirsher static void mv643xx_eth_recalc_skb_size(struct mv643xx_eth_private *mp) 2402527a6266SJeff Kirsher { 2403527a6266SJeff Kirsher int skb_size; 2404527a6266SJeff Kirsher 2405527a6266SJeff Kirsher /* 2406527a6266SJeff Kirsher * Reserve 2+14 bytes for an ethernet header (the hardware 2407527a6266SJeff Kirsher * automatically prepends 2 bytes of dummy data to each 2408527a6266SJeff Kirsher * received packet), 16 bytes for up to four VLAN tags, and 2409527a6266SJeff Kirsher * 4 bytes for the trailing FCS -- 36 bytes total. 2410527a6266SJeff Kirsher */ 2411527a6266SJeff Kirsher skb_size = mp->dev->mtu + 36; 2412527a6266SJeff Kirsher 2413527a6266SJeff Kirsher /* 2414527a6266SJeff Kirsher * Make sure that the skb size is a multiple of 8 bytes, as 2415527a6266SJeff Kirsher * the lower three bits of the receive descriptor's buffer 2416527a6266SJeff Kirsher * size field are ignored by the hardware. 2417527a6266SJeff Kirsher */ 2418527a6266SJeff Kirsher mp->skb_size = (skb_size + 7) & ~7; 2419527a6266SJeff Kirsher 2420527a6266SJeff Kirsher /* 2421527a6266SJeff Kirsher * If NET_SKB_PAD is smaller than a cache line, 2422527a6266SJeff Kirsher * netdev_alloc_skb() will cause skb->data to be misaligned 2423527a6266SJeff Kirsher * to a cache line boundary. If this is the case, include 2424527a6266SJeff Kirsher * some extra space to allow re-aligning the data area. 2425527a6266SJeff Kirsher */ 2426527a6266SJeff Kirsher mp->skb_size += SKB_DMA_REALIGN; 2427527a6266SJeff Kirsher } 2428527a6266SJeff Kirsher 2429527a6266SJeff Kirsher static int mv643xx_eth_open(struct net_device *dev) 2430527a6266SJeff Kirsher { 2431527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2432527a6266SJeff Kirsher int err; 2433527a6266SJeff Kirsher int i; 2434527a6266SJeff Kirsher 2435527a6266SJeff Kirsher wrlp(mp, INT_CAUSE, 0); 2436527a6266SJeff Kirsher wrlp(mp, INT_CAUSE_EXT, 0); 2437527a6266SJeff Kirsher rdlp(mp, INT_CAUSE_EXT); 2438527a6266SJeff Kirsher 2439527a6266SJeff Kirsher err = request_irq(dev->irq, mv643xx_eth_irq, 2440527a6266SJeff Kirsher IRQF_SHARED, dev->name, dev); 2441527a6266SJeff Kirsher if (err) { 2442527a6266SJeff Kirsher netdev_err(dev, "can't assign irq\n"); 2443527a6266SJeff Kirsher return -EAGAIN; 2444527a6266SJeff Kirsher } 2445527a6266SJeff Kirsher 2446527a6266SJeff Kirsher mv643xx_eth_recalc_skb_size(mp); 2447527a6266SJeff Kirsher 2448527a6266SJeff Kirsher napi_enable(&mp->napi); 2449527a6266SJeff Kirsher 2450527a6266SJeff Kirsher mp->int_mask = INT_EXT; 2451527a6266SJeff Kirsher 2452527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) { 2453527a6266SJeff Kirsher err = rxq_init(mp, i); 2454527a6266SJeff Kirsher if (err) { 2455527a6266SJeff Kirsher while (--i >= 0) 2456527a6266SJeff Kirsher rxq_deinit(mp->rxq + i); 2457527a6266SJeff Kirsher goto out; 2458527a6266SJeff Kirsher } 2459527a6266SJeff Kirsher 2460527a6266SJeff Kirsher rxq_refill(mp->rxq + i, INT_MAX); 2461527a6266SJeff Kirsher mp->int_mask |= INT_RX_0 << i; 2462527a6266SJeff Kirsher } 2463527a6266SJeff Kirsher 2464527a6266SJeff Kirsher if (mp->oom) { 2465527a6266SJeff Kirsher mp->rx_oom.expires = jiffies + (HZ / 10); 2466527a6266SJeff Kirsher add_timer(&mp->rx_oom); 2467527a6266SJeff Kirsher } 2468527a6266SJeff Kirsher 2469527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) { 2470527a6266SJeff Kirsher err = txq_init(mp, i); 2471527a6266SJeff Kirsher if (err) { 2472527a6266SJeff Kirsher while (--i >= 0) 2473527a6266SJeff Kirsher txq_deinit(mp->txq + i); 2474527a6266SJeff Kirsher goto out_free; 2475527a6266SJeff Kirsher } 2476527a6266SJeff Kirsher mp->int_mask |= INT_TX_END_0 << i; 2477527a6266SJeff Kirsher } 2478527a6266SJeff Kirsher 2479f564412cSSebastian Hesselbarth add_timer(&mp->mib_counters_timer); 2480527a6266SJeff Kirsher port_start(mp); 2481527a6266SJeff Kirsher 2482527a6266SJeff Kirsher wrlp(mp, INT_MASK_EXT, INT_EXT_LINK_PHY | INT_EXT_TX); 2483527a6266SJeff Kirsher wrlp(mp, INT_MASK, mp->int_mask); 2484527a6266SJeff Kirsher 2485527a6266SJeff Kirsher return 0; 2486527a6266SJeff Kirsher 2487527a6266SJeff Kirsher 2488527a6266SJeff Kirsher out_free: 2489527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) 2490527a6266SJeff Kirsher rxq_deinit(mp->rxq + i); 2491527a6266SJeff Kirsher out: 2492527a6266SJeff Kirsher free_irq(dev->irq, dev); 2493527a6266SJeff Kirsher 2494527a6266SJeff Kirsher return err; 2495527a6266SJeff Kirsher } 2496527a6266SJeff Kirsher 2497527a6266SJeff Kirsher static void port_reset(struct mv643xx_eth_private *mp) 2498527a6266SJeff Kirsher { 2499527a6266SJeff Kirsher unsigned int data; 2500527a6266SJeff Kirsher int i; 2501527a6266SJeff Kirsher 2502527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) 2503527a6266SJeff Kirsher rxq_disable(mp->rxq + i); 2504527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) 2505527a6266SJeff Kirsher txq_disable(mp->txq + i); 2506527a6266SJeff Kirsher 2507527a6266SJeff Kirsher while (1) { 2508527a6266SJeff Kirsher u32 ps = rdlp(mp, PORT_STATUS); 2509527a6266SJeff Kirsher 2510527a6266SJeff Kirsher if ((ps & (TX_IN_PROGRESS | TX_FIFO_EMPTY)) == TX_FIFO_EMPTY) 2511527a6266SJeff Kirsher break; 2512527a6266SJeff Kirsher udelay(10); 2513527a6266SJeff Kirsher } 2514527a6266SJeff Kirsher 2515527a6266SJeff Kirsher /* Reset the Enable bit in the Configuration Register */ 2516527a6266SJeff Kirsher data = rdlp(mp, PORT_SERIAL_CONTROL); 2517527a6266SJeff Kirsher data &= ~(SERIAL_PORT_ENABLE | 2518527a6266SJeff Kirsher DO_NOT_FORCE_LINK_FAIL | 2519527a6266SJeff Kirsher FORCE_LINK_PASS); 2520527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, data); 2521527a6266SJeff Kirsher } 2522527a6266SJeff Kirsher 2523527a6266SJeff Kirsher static int mv643xx_eth_stop(struct net_device *dev) 2524527a6266SJeff Kirsher { 2525527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2526527a6266SJeff Kirsher int i; 2527527a6266SJeff Kirsher 2528527a6266SJeff Kirsher wrlp(mp, INT_MASK_EXT, 0x00000000); 2529527a6266SJeff Kirsher wrlp(mp, INT_MASK, 0x00000000); 2530527a6266SJeff Kirsher rdlp(mp, INT_MASK); 2531527a6266SJeff Kirsher 2532527a6266SJeff Kirsher napi_disable(&mp->napi); 2533527a6266SJeff Kirsher 2534527a6266SJeff Kirsher del_timer_sync(&mp->rx_oom); 2535527a6266SJeff Kirsher 2536527a6266SJeff Kirsher netif_carrier_off(dev); 253758911151SSebastian Hesselbarth if (mp->phy) 253858911151SSebastian Hesselbarth phy_stop(mp->phy); 2539527a6266SJeff Kirsher free_irq(dev->irq, dev); 2540527a6266SJeff Kirsher 2541527a6266SJeff Kirsher port_reset(mp); 2542527a6266SJeff Kirsher mv643xx_eth_get_stats(dev); 2543527a6266SJeff Kirsher mib_counters_update(mp); 2544527a6266SJeff Kirsher del_timer_sync(&mp->mib_counters_timer); 2545527a6266SJeff Kirsher 2546527a6266SJeff Kirsher for (i = 0; i < mp->rxq_count; i++) 2547527a6266SJeff Kirsher rxq_deinit(mp->rxq + i); 2548527a6266SJeff Kirsher for (i = 0; i < mp->txq_count; i++) 2549527a6266SJeff Kirsher txq_deinit(mp->txq + i); 2550527a6266SJeff Kirsher 2551527a6266SJeff Kirsher return 0; 2552527a6266SJeff Kirsher } 2553527a6266SJeff Kirsher 2554527a6266SJeff Kirsher static int mv643xx_eth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 2555527a6266SJeff Kirsher { 2556527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2557260055bbSPhil Sutter int ret; 2558527a6266SJeff Kirsher 2559260055bbSPhil Sutter if (mp->phy == NULL) 2560260055bbSPhil Sutter return -ENOTSUPP; 2561527a6266SJeff Kirsher 2562260055bbSPhil Sutter ret = phy_mii_ioctl(mp->phy, ifr, cmd); 2563260055bbSPhil Sutter if (!ret) 25640a9e413bSEzequiel Garcia mv643xx_eth_adjust_link(dev); 2565260055bbSPhil Sutter return ret; 2566527a6266SJeff Kirsher } 2567527a6266SJeff Kirsher 2568527a6266SJeff Kirsher static int mv643xx_eth_change_mtu(struct net_device *dev, int new_mtu) 2569527a6266SJeff Kirsher { 2570527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2571527a6266SJeff Kirsher 2572527a6266SJeff Kirsher if (new_mtu < 64 || new_mtu > 9500) 2573527a6266SJeff Kirsher return -EINVAL; 2574527a6266SJeff Kirsher 2575527a6266SJeff Kirsher dev->mtu = new_mtu; 2576527a6266SJeff Kirsher mv643xx_eth_recalc_skb_size(mp); 2577527a6266SJeff Kirsher tx_set_rate(mp, 1000000000, 16777216); 2578527a6266SJeff Kirsher 2579527a6266SJeff Kirsher if (!netif_running(dev)) 2580527a6266SJeff Kirsher return 0; 2581527a6266SJeff Kirsher 2582527a6266SJeff Kirsher /* 2583527a6266SJeff Kirsher * Stop and then re-open the interface. This will allocate RX 2584527a6266SJeff Kirsher * skbs of the new MTU. 2585527a6266SJeff Kirsher * There is a possible danger that the open will not succeed, 2586527a6266SJeff Kirsher * due to memory being full. 2587527a6266SJeff Kirsher */ 2588527a6266SJeff Kirsher mv643xx_eth_stop(dev); 2589527a6266SJeff Kirsher if (mv643xx_eth_open(dev)) { 2590527a6266SJeff Kirsher netdev_err(dev, 2591527a6266SJeff Kirsher "fatal error on re-opening device after MTU change\n"); 2592527a6266SJeff Kirsher } 2593527a6266SJeff Kirsher 2594527a6266SJeff Kirsher return 0; 2595527a6266SJeff Kirsher } 2596527a6266SJeff Kirsher 2597527a6266SJeff Kirsher static void tx_timeout_task(struct work_struct *ugly) 2598527a6266SJeff Kirsher { 2599527a6266SJeff Kirsher struct mv643xx_eth_private *mp; 2600527a6266SJeff Kirsher 2601527a6266SJeff Kirsher mp = container_of(ugly, struct mv643xx_eth_private, tx_timeout_task); 2602527a6266SJeff Kirsher if (netif_running(mp->dev)) { 2603527a6266SJeff Kirsher netif_tx_stop_all_queues(mp->dev); 2604527a6266SJeff Kirsher port_reset(mp); 2605527a6266SJeff Kirsher port_start(mp); 2606527a6266SJeff Kirsher netif_tx_wake_all_queues(mp->dev); 2607527a6266SJeff Kirsher } 2608527a6266SJeff Kirsher } 2609527a6266SJeff Kirsher 2610527a6266SJeff Kirsher static void mv643xx_eth_tx_timeout(struct net_device *dev) 2611527a6266SJeff Kirsher { 2612527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2613527a6266SJeff Kirsher 2614527a6266SJeff Kirsher netdev_info(dev, "tx timeout\n"); 2615527a6266SJeff Kirsher 2616527a6266SJeff Kirsher schedule_work(&mp->tx_timeout_task); 2617527a6266SJeff Kirsher } 2618527a6266SJeff Kirsher 2619527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 2620527a6266SJeff Kirsher static void mv643xx_eth_netpoll(struct net_device *dev) 2621527a6266SJeff Kirsher { 2622527a6266SJeff Kirsher struct mv643xx_eth_private *mp = netdev_priv(dev); 2623527a6266SJeff Kirsher 2624527a6266SJeff Kirsher wrlp(mp, INT_MASK, 0x00000000); 2625527a6266SJeff Kirsher rdlp(mp, INT_MASK); 2626527a6266SJeff Kirsher 2627527a6266SJeff Kirsher mv643xx_eth_irq(dev->irq, dev); 2628527a6266SJeff Kirsher 2629527a6266SJeff Kirsher wrlp(mp, INT_MASK, mp->int_mask); 2630527a6266SJeff Kirsher } 2631527a6266SJeff Kirsher #endif 2632527a6266SJeff Kirsher 2633527a6266SJeff Kirsher 2634527a6266SJeff Kirsher /* platform glue ************************************************************/ 2635527a6266SJeff Kirsher static void 2636527a6266SJeff Kirsher mv643xx_eth_conf_mbus_windows(struct mv643xx_eth_shared_private *msp, 263763a9332bSAndrew Lunn const struct mbus_dram_target_info *dram) 2638527a6266SJeff Kirsher { 2639527a6266SJeff Kirsher void __iomem *base = msp->base; 2640527a6266SJeff Kirsher u32 win_enable; 2641527a6266SJeff Kirsher u32 win_protect; 2642527a6266SJeff Kirsher int i; 2643527a6266SJeff Kirsher 2644527a6266SJeff Kirsher for (i = 0; i < 6; i++) { 2645527a6266SJeff Kirsher writel(0, base + WINDOW_BASE(i)); 2646527a6266SJeff Kirsher writel(0, base + WINDOW_SIZE(i)); 2647527a6266SJeff Kirsher if (i < 4) 2648527a6266SJeff Kirsher writel(0, base + WINDOW_REMAP_HIGH(i)); 2649527a6266SJeff Kirsher } 2650527a6266SJeff Kirsher 2651527a6266SJeff Kirsher win_enable = 0x3f; 2652527a6266SJeff Kirsher win_protect = 0; 2653527a6266SJeff Kirsher 2654527a6266SJeff Kirsher for (i = 0; i < dram->num_cs; i++) { 265563a9332bSAndrew Lunn const struct mbus_dram_window *cs = dram->cs + i; 2656527a6266SJeff Kirsher 2657527a6266SJeff Kirsher writel((cs->base & 0xffff0000) | 2658527a6266SJeff Kirsher (cs->mbus_attr << 8) | 2659527a6266SJeff Kirsher dram->mbus_dram_target_id, base + WINDOW_BASE(i)); 2660527a6266SJeff Kirsher writel((cs->size - 1) & 0xffff0000, base + WINDOW_SIZE(i)); 2661527a6266SJeff Kirsher 2662527a6266SJeff Kirsher win_enable &= ~(1 << i); 2663527a6266SJeff Kirsher win_protect |= 3 << (2 * i); 2664527a6266SJeff Kirsher } 2665527a6266SJeff Kirsher 2666527a6266SJeff Kirsher writel(win_enable, base + WINDOW_BAR_ENABLE); 2667527a6266SJeff Kirsher msp->win_protect = win_protect; 2668527a6266SJeff Kirsher } 2669527a6266SJeff Kirsher 2670527a6266SJeff Kirsher static void infer_hw_params(struct mv643xx_eth_shared_private *msp) 2671527a6266SJeff Kirsher { 2672527a6266SJeff Kirsher /* 2673527a6266SJeff Kirsher * Check whether we have a 14-bit coal limit field in bits 2674527a6266SJeff Kirsher * [21:8], or a 16-bit coal limit in bits [25,21:7] of the 2675527a6266SJeff Kirsher * SDMA config register. 2676527a6266SJeff Kirsher */ 2677527a6266SJeff Kirsher writel(0x02000000, msp->base + 0x0400 + SDMA_CONFIG); 2678527a6266SJeff Kirsher if (readl(msp->base + 0x0400 + SDMA_CONFIG) & 0x02000000) 2679527a6266SJeff Kirsher msp->extended_rx_coal_limit = 1; 2680527a6266SJeff Kirsher else 2681527a6266SJeff Kirsher msp->extended_rx_coal_limit = 0; 2682527a6266SJeff Kirsher 2683527a6266SJeff Kirsher /* 2684527a6266SJeff Kirsher * Check whether the MAC supports TX rate control, and if 2685527a6266SJeff Kirsher * yes, whether its associated registers are in the old or 2686527a6266SJeff Kirsher * the new place. 2687527a6266SJeff Kirsher */ 2688527a6266SJeff Kirsher writel(1, msp->base + 0x0400 + TX_BW_MTU_MOVED); 2689527a6266SJeff Kirsher if (readl(msp->base + 0x0400 + TX_BW_MTU_MOVED) & 1) { 2690527a6266SJeff Kirsher msp->tx_bw_control = TX_BW_CONTROL_NEW_LAYOUT; 2691527a6266SJeff Kirsher } else { 2692527a6266SJeff Kirsher writel(7, msp->base + 0x0400 + TX_BW_RATE); 2693527a6266SJeff Kirsher if (readl(msp->base + 0x0400 + TX_BW_RATE) & 7) 2694527a6266SJeff Kirsher msp->tx_bw_control = TX_BW_CONTROL_OLD_LAYOUT; 2695527a6266SJeff Kirsher else 2696527a6266SJeff Kirsher msp->tx_bw_control = TX_BW_CONTROL_ABSENT; 2697527a6266SJeff Kirsher } 2698527a6266SJeff Kirsher } 2699527a6266SJeff Kirsher 270076723bcaSSebastian Hesselbarth #if defined(CONFIG_OF) 270176723bcaSSebastian Hesselbarth static const struct of_device_id mv643xx_eth_shared_ids[] = { 270276723bcaSSebastian Hesselbarth { .compatible = "marvell,orion-eth", }, 270376723bcaSSebastian Hesselbarth { .compatible = "marvell,kirkwood-eth", }, 270476723bcaSSebastian Hesselbarth { } 270576723bcaSSebastian Hesselbarth }; 270676723bcaSSebastian Hesselbarth MODULE_DEVICE_TABLE(of, mv643xx_eth_shared_ids); 270776723bcaSSebastian Hesselbarth #endif 270876723bcaSSebastian Hesselbarth 270976723bcaSSebastian Hesselbarth #if defined(CONFIG_OF) && !defined(CONFIG_MV64X60) 271076723bcaSSebastian Hesselbarth #define mv643xx_eth_property(_np, _name, _v) \ 271176723bcaSSebastian Hesselbarth do { \ 271276723bcaSSebastian Hesselbarth u32 tmp; \ 271376723bcaSSebastian Hesselbarth if (!of_property_read_u32(_np, "marvell," _name, &tmp)) \ 271476723bcaSSebastian Hesselbarth _v = tmp; \ 271576723bcaSSebastian Hesselbarth } while (0) 271676723bcaSSebastian Hesselbarth 271776723bcaSSebastian Hesselbarth static struct platform_device *port_platdev[3]; 271876723bcaSSebastian Hesselbarth 271976723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_add_port(struct platform_device *pdev, 272076723bcaSSebastian Hesselbarth struct device_node *pnp) 272176723bcaSSebastian Hesselbarth { 272276723bcaSSebastian Hesselbarth struct platform_device *ppdev; 272376723bcaSSebastian Hesselbarth struct mv643xx_eth_platform_data ppd; 272476723bcaSSebastian Hesselbarth struct resource res; 272576723bcaSSebastian Hesselbarth const char *mac_addr; 272676723bcaSSebastian Hesselbarth int ret; 2727785bf6f7SJonas Gorski int dev_num = 0; 272876723bcaSSebastian Hesselbarth 272976723bcaSSebastian Hesselbarth memset(&ppd, 0, sizeof(ppd)); 273076723bcaSSebastian Hesselbarth ppd.shared = pdev; 273176723bcaSSebastian Hesselbarth 273276723bcaSSebastian Hesselbarth memset(&res, 0, sizeof(res)); 273376723bcaSSebastian Hesselbarth if (!of_irq_to_resource(pnp, 0, &res)) { 273476723bcaSSebastian Hesselbarth dev_err(&pdev->dev, "missing interrupt on %s\n", pnp->name); 273576723bcaSSebastian Hesselbarth return -EINVAL; 273676723bcaSSebastian Hesselbarth } 273776723bcaSSebastian Hesselbarth 273876723bcaSSebastian Hesselbarth if (of_property_read_u32(pnp, "reg", &ppd.port_number)) { 273976723bcaSSebastian Hesselbarth dev_err(&pdev->dev, "missing reg property on %s\n", pnp->name); 274076723bcaSSebastian Hesselbarth return -EINVAL; 274176723bcaSSebastian Hesselbarth } 274276723bcaSSebastian Hesselbarth 274376723bcaSSebastian Hesselbarth if (ppd.port_number >= 3) { 274476723bcaSSebastian Hesselbarth dev_err(&pdev->dev, "invalid reg property on %s\n", pnp->name); 274576723bcaSSebastian Hesselbarth return -EINVAL; 274676723bcaSSebastian Hesselbarth } 274776723bcaSSebastian Hesselbarth 2748785bf6f7SJonas Gorski while (dev_num < 3 && port_platdev[dev_num]) 2749785bf6f7SJonas Gorski dev_num++; 2750785bf6f7SJonas Gorski 2751785bf6f7SJonas Gorski if (dev_num == 3) { 2752785bf6f7SJonas Gorski dev_err(&pdev->dev, "too many ports registered\n"); 2753785bf6f7SJonas Gorski return -EINVAL; 2754785bf6f7SJonas Gorski } 2755785bf6f7SJonas Gorski 275676723bcaSSebastian Hesselbarth mac_addr = of_get_mac_address(pnp); 275776723bcaSSebastian Hesselbarth if (mac_addr) 2758d458cdf7SJoe Perches memcpy(ppd.mac_addr, mac_addr, ETH_ALEN); 275976723bcaSSebastian Hesselbarth 276076723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "tx-queue-size", ppd.tx_queue_size); 276176723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "tx-sram-addr", ppd.tx_sram_addr); 276276723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "tx-sram-size", ppd.tx_sram_size); 276376723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "rx-queue-size", ppd.rx_queue_size); 276476723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "rx-sram-addr", ppd.rx_sram_addr); 276576723bcaSSebastian Hesselbarth mv643xx_eth_property(pnp, "rx-sram-size", ppd.rx_sram_size); 276676723bcaSSebastian Hesselbarth 276776723bcaSSebastian Hesselbarth ppd.phy_node = of_parse_phandle(pnp, "phy-handle", 0); 276876723bcaSSebastian Hesselbarth if (!ppd.phy_node) { 276976723bcaSSebastian Hesselbarth ppd.phy_addr = MV643XX_ETH_PHY_NONE; 277076723bcaSSebastian Hesselbarth of_property_read_u32(pnp, "speed", &ppd.speed); 277176723bcaSSebastian Hesselbarth of_property_read_u32(pnp, "duplex", &ppd.duplex); 277276723bcaSSebastian Hesselbarth } 277376723bcaSSebastian Hesselbarth 2774785bf6f7SJonas Gorski ppdev = platform_device_alloc(MV643XX_ETH_NAME, dev_num); 277576723bcaSSebastian Hesselbarth if (!ppdev) 277676723bcaSSebastian Hesselbarth return -ENOMEM; 277776723bcaSSebastian Hesselbarth ppdev->dev.coherent_dma_mask = DMA_BIT_MASK(32); 2778b5d82db8SSebastian Hesselbarth ppdev->dev.of_node = pnp; 277976723bcaSSebastian Hesselbarth 278076723bcaSSebastian Hesselbarth ret = platform_device_add_resources(ppdev, &res, 1); 278176723bcaSSebastian Hesselbarth if (ret) 278276723bcaSSebastian Hesselbarth goto port_err; 278376723bcaSSebastian Hesselbarth 278476723bcaSSebastian Hesselbarth ret = platform_device_add_data(ppdev, &ppd, sizeof(ppd)); 278576723bcaSSebastian Hesselbarth if (ret) 278676723bcaSSebastian Hesselbarth goto port_err; 278776723bcaSSebastian Hesselbarth 278876723bcaSSebastian Hesselbarth ret = platform_device_add(ppdev); 278976723bcaSSebastian Hesselbarth if (ret) 279076723bcaSSebastian Hesselbarth goto port_err; 279176723bcaSSebastian Hesselbarth 2792785bf6f7SJonas Gorski port_platdev[dev_num] = ppdev; 279376723bcaSSebastian Hesselbarth 279476723bcaSSebastian Hesselbarth return 0; 279576723bcaSSebastian Hesselbarth 279676723bcaSSebastian Hesselbarth port_err: 279776723bcaSSebastian Hesselbarth platform_device_put(ppdev); 279876723bcaSSebastian Hesselbarth return ret; 279976723bcaSSebastian Hesselbarth } 280076723bcaSSebastian Hesselbarth 280176723bcaSSebastian Hesselbarth static int mv643xx_eth_shared_of_probe(struct platform_device *pdev) 280276723bcaSSebastian Hesselbarth { 280376723bcaSSebastian Hesselbarth struct mv643xx_eth_shared_platform_data *pd; 280476723bcaSSebastian Hesselbarth struct device_node *pnp, *np = pdev->dev.of_node; 280576723bcaSSebastian Hesselbarth int ret; 280676723bcaSSebastian Hesselbarth 280776723bcaSSebastian Hesselbarth /* bail out if not registered from DT */ 280876723bcaSSebastian Hesselbarth if (!np) 280976723bcaSSebastian Hesselbarth return 0; 281076723bcaSSebastian Hesselbarth 281176723bcaSSebastian Hesselbarth pd = devm_kzalloc(&pdev->dev, sizeof(*pd), GFP_KERNEL); 281276723bcaSSebastian Hesselbarth if (!pd) 281376723bcaSSebastian Hesselbarth return -ENOMEM; 281476723bcaSSebastian Hesselbarth pdev->dev.platform_data = pd; 281576723bcaSSebastian Hesselbarth 281676723bcaSSebastian Hesselbarth mv643xx_eth_property(np, "tx-checksum-limit", pd->tx_csum_limit); 281776723bcaSSebastian Hesselbarth 281876723bcaSSebastian Hesselbarth for_each_available_child_of_node(np, pnp) { 281976723bcaSSebastian Hesselbarth ret = mv643xx_eth_shared_of_add_port(pdev, pnp); 2820*26b7974dSJulia Lawall if (ret) { 2821*26b7974dSJulia Lawall of_node_put(pnp); 282276723bcaSSebastian Hesselbarth return ret; 282376723bcaSSebastian Hesselbarth } 2824*26b7974dSJulia Lawall } 282576723bcaSSebastian Hesselbarth return 0; 282676723bcaSSebastian Hesselbarth } 282776723bcaSSebastian Hesselbarth 282876723bcaSSebastian Hesselbarth static void mv643xx_eth_shared_of_remove(void) 282976723bcaSSebastian Hesselbarth { 283076723bcaSSebastian Hesselbarth int n; 283176723bcaSSebastian Hesselbarth 283276723bcaSSebastian Hesselbarth for (n = 0; n < 3; n++) { 283376723bcaSSebastian Hesselbarth platform_device_del(port_platdev[n]); 283476723bcaSSebastian Hesselbarth port_platdev[n] = NULL; 283576723bcaSSebastian Hesselbarth } 283676723bcaSSebastian Hesselbarth } 283776723bcaSSebastian Hesselbarth #else 2838ff20877aSArnd Bergmann static inline int mv643xx_eth_shared_of_probe(struct platform_device *pdev) 283976723bcaSSebastian Hesselbarth { 2840ff20877aSArnd Bergmann return 0; 284176723bcaSSebastian Hesselbarth } 284276723bcaSSebastian Hesselbarth 2843ff20877aSArnd Bergmann static inline void mv643xx_eth_shared_of_remove(void) 2844ff20877aSArnd Bergmann { 2845ff20877aSArnd Bergmann } 284676723bcaSSebastian Hesselbarth #endif 284776723bcaSSebastian Hesselbarth 2848527a6266SJeff Kirsher static int mv643xx_eth_shared_probe(struct platform_device *pdev) 2849527a6266SJeff Kirsher { 2850527a6266SJeff Kirsher static int mv643xx_eth_version_printed; 285176723bcaSSebastian Hesselbarth struct mv643xx_eth_shared_platform_data *pd; 2852527a6266SJeff Kirsher struct mv643xx_eth_shared_private *msp; 285363a9332bSAndrew Lunn const struct mbus_dram_target_info *dram; 2854527a6266SJeff Kirsher struct resource *res; 285576723bcaSSebastian Hesselbarth int ret; 2856527a6266SJeff Kirsher 2857527a6266SJeff Kirsher if (!mv643xx_eth_version_printed++) 2858527a6266SJeff Kirsher pr_notice("MV-643xx 10/100/1000 ethernet driver version %s\n", 2859527a6266SJeff Kirsher mv643xx_eth_driver_version); 2860527a6266SJeff Kirsher 2861527a6266SJeff Kirsher res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2862527a6266SJeff Kirsher if (res == NULL) 2863727f957aSSebastian Hesselbarth return -EINVAL; 2864527a6266SJeff Kirsher 2865727f957aSSebastian Hesselbarth msp = devm_kzalloc(&pdev->dev, sizeof(*msp), GFP_KERNEL); 2866527a6266SJeff Kirsher if (msp == NULL) 2867727f957aSSebastian Hesselbarth return -ENOMEM; 286876723bcaSSebastian Hesselbarth platform_set_drvdata(pdev, msp); 2869527a6266SJeff Kirsher 287065a6f969SSebastian Hesselbarth msp->base = devm_ioremap(&pdev->dev, res->start, resource_size(res)); 2871527a6266SJeff Kirsher if (msp->base == NULL) 2872727f957aSSebastian Hesselbarth return -ENOMEM; 2873527a6266SJeff Kirsher 287420922486SSebastian Hesselbarth msp->clk = devm_clk_get(&pdev->dev, NULL); 287520922486SSebastian Hesselbarth if (!IS_ERR(msp->clk)) 287620922486SSebastian Hesselbarth clk_prepare_enable(msp->clk); 287720922486SSebastian Hesselbarth 2878527a6266SJeff Kirsher /* 2879527a6266SJeff Kirsher * (Re-)program MBUS remapping windows if we are asked to. 2880527a6266SJeff Kirsher */ 288163a9332bSAndrew Lunn dram = mv_mbus_dram_info(); 288263a9332bSAndrew Lunn if (dram) 288363a9332bSAndrew Lunn mv643xx_eth_conf_mbus_windows(msp, dram); 2884527a6266SJeff Kirsher 288576723bcaSSebastian Hesselbarth ret = mv643xx_eth_shared_of_probe(pdev); 288676723bcaSSebastian Hesselbarth if (ret) 288776723bcaSSebastian Hesselbarth return ret; 2888bbfa6d0aSJingoo Han pd = dev_get_platdata(&pdev->dev); 288976723bcaSSebastian Hesselbarth 2890527a6266SJeff Kirsher msp->tx_csum_limit = (pd != NULL && pd->tx_csum_limit) ? 2891527a6266SJeff Kirsher pd->tx_csum_limit : 9 * 1024; 2892527a6266SJeff Kirsher infer_hw_params(msp); 2893527a6266SJeff Kirsher 2894527a6266SJeff Kirsher return 0; 2895527a6266SJeff Kirsher } 2896527a6266SJeff Kirsher 2897527a6266SJeff Kirsher static int mv643xx_eth_shared_remove(struct platform_device *pdev) 2898527a6266SJeff Kirsher { 2899527a6266SJeff Kirsher struct mv643xx_eth_shared_private *msp = platform_get_drvdata(pdev); 2900527a6266SJeff Kirsher 290176723bcaSSebastian Hesselbarth mv643xx_eth_shared_of_remove(); 290220922486SSebastian Hesselbarth if (!IS_ERR(msp->clk)) 290320922486SSebastian Hesselbarth clk_disable_unprepare(msp->clk); 2904527a6266SJeff Kirsher return 0; 2905527a6266SJeff Kirsher } 2906527a6266SJeff Kirsher 2907527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_shared_driver = { 2908527a6266SJeff Kirsher .probe = mv643xx_eth_shared_probe, 2909527a6266SJeff Kirsher .remove = mv643xx_eth_shared_remove, 2910527a6266SJeff Kirsher .driver = { 2911527a6266SJeff Kirsher .name = MV643XX_ETH_SHARED_NAME, 291276723bcaSSebastian Hesselbarth .of_match_table = of_match_ptr(mv643xx_eth_shared_ids), 2913527a6266SJeff Kirsher }, 2914527a6266SJeff Kirsher }; 2915527a6266SJeff Kirsher 2916527a6266SJeff Kirsher static void phy_addr_set(struct mv643xx_eth_private *mp, int phy_addr) 2917527a6266SJeff Kirsher { 2918527a6266SJeff Kirsher int addr_shift = 5 * mp->port_num; 2919527a6266SJeff Kirsher u32 data; 2920527a6266SJeff Kirsher 2921527a6266SJeff Kirsher data = rdl(mp, PHY_ADDR); 2922527a6266SJeff Kirsher data &= ~(0x1f << addr_shift); 2923527a6266SJeff Kirsher data |= (phy_addr & 0x1f) << addr_shift; 2924527a6266SJeff Kirsher wrl(mp, PHY_ADDR, data); 2925527a6266SJeff Kirsher } 2926527a6266SJeff Kirsher 2927527a6266SJeff Kirsher static int phy_addr_get(struct mv643xx_eth_private *mp) 2928527a6266SJeff Kirsher { 2929527a6266SJeff Kirsher unsigned int data; 2930527a6266SJeff Kirsher 2931527a6266SJeff Kirsher data = rdl(mp, PHY_ADDR); 2932527a6266SJeff Kirsher 2933527a6266SJeff Kirsher return (data >> (5 * mp->port_num)) & 0x1f; 2934527a6266SJeff Kirsher } 2935527a6266SJeff Kirsher 2936527a6266SJeff Kirsher static void set_params(struct mv643xx_eth_private *mp, 2937527a6266SJeff Kirsher struct mv643xx_eth_platform_data *pd) 2938527a6266SJeff Kirsher { 2939527a6266SJeff Kirsher struct net_device *dev = mp->dev; 2940ee9e4956SEzequiel Garcia unsigned int tx_ring_size; 2941527a6266SJeff Kirsher 2942527a6266SJeff Kirsher if (is_valid_ether_addr(pd->mac_addr)) 2943d458cdf7SJoe Perches memcpy(dev->dev_addr, pd->mac_addr, ETH_ALEN); 2944527a6266SJeff Kirsher else 2945527a6266SJeff Kirsher uc_addr_get(mp, dev->dev_addr); 2946527a6266SJeff Kirsher 2947527a6266SJeff Kirsher mp->rx_ring_size = DEFAULT_RX_QUEUE_SIZE; 2948527a6266SJeff Kirsher if (pd->rx_queue_size) 2949527a6266SJeff Kirsher mp->rx_ring_size = pd->rx_queue_size; 2950527a6266SJeff Kirsher mp->rx_desc_sram_addr = pd->rx_sram_addr; 2951527a6266SJeff Kirsher mp->rx_desc_sram_size = pd->rx_sram_size; 2952527a6266SJeff Kirsher 2953527a6266SJeff Kirsher mp->rxq_count = pd->rx_queue_count ? : 1; 2954527a6266SJeff Kirsher 2955ee9e4956SEzequiel Garcia tx_ring_size = DEFAULT_TX_QUEUE_SIZE; 2956527a6266SJeff Kirsher if (pd->tx_queue_size) 2957ee9e4956SEzequiel Garcia tx_ring_size = pd->tx_queue_size; 2958ee9e4956SEzequiel Garcia 2959ee9e4956SEzequiel Garcia mp->tx_ring_size = clamp_t(unsigned int, tx_ring_size, 2960ee9e4956SEzequiel Garcia MV643XX_MAX_SKB_DESCS * 2, 4096); 2961ee9e4956SEzequiel Garcia if (mp->tx_ring_size != tx_ring_size) 2962ee9e4956SEzequiel Garcia netdev_warn(dev, "TX queue size set to %u (requested %u)\n", 2963ee9e4956SEzequiel Garcia mp->tx_ring_size, tx_ring_size); 2964ee9e4956SEzequiel Garcia 2965527a6266SJeff Kirsher mp->tx_desc_sram_addr = pd->tx_sram_addr; 2966527a6266SJeff Kirsher mp->tx_desc_sram_size = pd->tx_sram_size; 2967527a6266SJeff Kirsher 2968527a6266SJeff Kirsher mp->txq_count = pd->tx_queue_count ? : 1; 2969527a6266SJeff Kirsher } 2970527a6266SJeff Kirsher 2971527a6266SJeff Kirsher static struct phy_device *phy_scan(struct mv643xx_eth_private *mp, 2972527a6266SJeff Kirsher int phy_addr) 2973527a6266SJeff Kirsher { 2974527a6266SJeff Kirsher struct phy_device *phydev; 2975527a6266SJeff Kirsher int start; 2976527a6266SJeff Kirsher int num; 2977527a6266SJeff Kirsher int i; 2978c3a07134SFlorian Fainelli char phy_id[MII_BUS_ID_SIZE + 3]; 2979527a6266SJeff Kirsher 2980527a6266SJeff Kirsher if (phy_addr == MV643XX_ETH_PHY_ADDR_DEFAULT) { 2981527a6266SJeff Kirsher start = phy_addr_get(mp) & 0x1f; 2982527a6266SJeff Kirsher num = 32; 2983527a6266SJeff Kirsher } else { 2984527a6266SJeff Kirsher start = phy_addr & 0x1f; 2985527a6266SJeff Kirsher num = 1; 2986527a6266SJeff Kirsher } 2987527a6266SJeff Kirsher 2988c3a07134SFlorian Fainelli /* Attempt to connect to the PHY using orion-mdio */ 2989976c90b9SSimon Baatz phydev = ERR_PTR(-ENODEV); 2990527a6266SJeff Kirsher for (i = 0; i < num; i++) { 2991527a6266SJeff Kirsher int addr = (start + i) & 0x1f; 2992527a6266SJeff Kirsher 2993c3a07134SFlorian Fainelli snprintf(phy_id, sizeof(phy_id), PHY_ID_FMT, 2994c3a07134SFlorian Fainelli "orion-mdio-mii", addr); 2995527a6266SJeff Kirsher 2996c3a07134SFlorian Fainelli phydev = phy_connect(mp->dev, phy_id, mv643xx_eth_adjust_link, 2997c3a07134SFlorian Fainelli PHY_INTERFACE_MODE_GMII); 2998c3a07134SFlorian Fainelli if (!IS_ERR(phydev)) { 2999527a6266SJeff Kirsher phy_addr_set(mp, addr); 3000c3a07134SFlorian Fainelli break; 3001527a6266SJeff Kirsher } 3002527a6266SJeff Kirsher } 3003527a6266SJeff Kirsher 3004527a6266SJeff Kirsher return phydev; 3005527a6266SJeff Kirsher } 3006527a6266SJeff Kirsher 3007527a6266SJeff Kirsher static void phy_init(struct mv643xx_eth_private *mp, int speed, int duplex) 3008527a6266SJeff Kirsher { 3009527a6266SJeff Kirsher struct phy_device *phy = mp->phy; 3010527a6266SJeff Kirsher 3011527a6266SJeff Kirsher if (speed == 0) { 3012527a6266SJeff Kirsher phy->autoneg = AUTONEG_ENABLE; 3013527a6266SJeff Kirsher phy->speed = 0; 3014527a6266SJeff Kirsher phy->duplex = 0; 3015527a6266SJeff Kirsher phy->advertising = phy->supported | ADVERTISED_Autoneg; 3016527a6266SJeff Kirsher } else { 3017527a6266SJeff Kirsher phy->autoneg = AUTONEG_DISABLE; 3018527a6266SJeff Kirsher phy->advertising = 0; 3019527a6266SJeff Kirsher phy->speed = speed; 3020527a6266SJeff Kirsher phy->duplex = duplex; 3021527a6266SJeff Kirsher } 3022527a6266SJeff Kirsher phy_start_aneg(phy); 3023527a6266SJeff Kirsher } 3024527a6266SJeff Kirsher 3025527a6266SJeff Kirsher static void init_pscr(struct mv643xx_eth_private *mp, int speed, int duplex) 3026527a6266SJeff Kirsher { 3027527a6266SJeff Kirsher u32 pscr; 3028527a6266SJeff Kirsher 3029527a6266SJeff Kirsher pscr = rdlp(mp, PORT_SERIAL_CONTROL); 3030527a6266SJeff Kirsher if (pscr & SERIAL_PORT_ENABLE) { 3031527a6266SJeff Kirsher pscr &= ~SERIAL_PORT_ENABLE; 3032527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, pscr); 3033527a6266SJeff Kirsher } 3034527a6266SJeff Kirsher 3035527a6266SJeff Kirsher pscr = MAX_RX_PACKET_9700BYTE | SERIAL_PORT_CONTROL_RESERVED; 3036527a6266SJeff Kirsher if (mp->phy == NULL) { 3037527a6266SJeff Kirsher pscr |= DISABLE_AUTO_NEG_SPEED_GMII; 3038527a6266SJeff Kirsher if (speed == SPEED_1000) 3039527a6266SJeff Kirsher pscr |= SET_GMII_SPEED_TO_1000; 3040527a6266SJeff Kirsher else if (speed == SPEED_100) 3041527a6266SJeff Kirsher pscr |= SET_MII_SPEED_TO_100; 3042527a6266SJeff Kirsher 3043527a6266SJeff Kirsher pscr |= DISABLE_AUTO_NEG_FOR_FLOW_CTRL; 3044527a6266SJeff Kirsher 3045527a6266SJeff Kirsher pscr |= DISABLE_AUTO_NEG_FOR_DUPLEX; 3046527a6266SJeff Kirsher if (duplex == DUPLEX_FULL) 3047527a6266SJeff Kirsher pscr |= SET_FULL_DUPLEX_MODE; 3048527a6266SJeff Kirsher } 3049527a6266SJeff Kirsher 3050527a6266SJeff Kirsher wrlp(mp, PORT_SERIAL_CONTROL, pscr); 3051527a6266SJeff Kirsher } 3052527a6266SJeff Kirsher 3053527a6266SJeff Kirsher static const struct net_device_ops mv643xx_eth_netdev_ops = { 3054527a6266SJeff Kirsher .ndo_open = mv643xx_eth_open, 3055527a6266SJeff Kirsher .ndo_stop = mv643xx_eth_stop, 3056527a6266SJeff Kirsher .ndo_start_xmit = mv643xx_eth_xmit, 3057527a6266SJeff Kirsher .ndo_set_rx_mode = mv643xx_eth_set_rx_mode, 3058527a6266SJeff Kirsher .ndo_set_mac_address = mv643xx_eth_set_mac_address, 3059527a6266SJeff Kirsher .ndo_validate_addr = eth_validate_addr, 3060527a6266SJeff Kirsher .ndo_do_ioctl = mv643xx_eth_ioctl, 3061527a6266SJeff Kirsher .ndo_change_mtu = mv643xx_eth_change_mtu, 3062527a6266SJeff Kirsher .ndo_set_features = mv643xx_eth_set_features, 3063527a6266SJeff Kirsher .ndo_tx_timeout = mv643xx_eth_tx_timeout, 3064527a6266SJeff Kirsher .ndo_get_stats = mv643xx_eth_get_stats, 3065527a6266SJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER 3066527a6266SJeff Kirsher .ndo_poll_controller = mv643xx_eth_netpoll, 3067527a6266SJeff Kirsher #endif 3068527a6266SJeff Kirsher }; 3069527a6266SJeff Kirsher 3070527a6266SJeff Kirsher static int mv643xx_eth_probe(struct platform_device *pdev) 3071527a6266SJeff Kirsher { 3072527a6266SJeff Kirsher struct mv643xx_eth_platform_data *pd; 3073527a6266SJeff Kirsher struct mv643xx_eth_private *mp; 3074527a6266SJeff Kirsher struct net_device *dev; 3075527a6266SJeff Kirsher struct resource *res; 3076527a6266SJeff Kirsher int err; 3077527a6266SJeff Kirsher 3078bbfa6d0aSJingoo Han pd = dev_get_platdata(&pdev->dev); 3079527a6266SJeff Kirsher if (pd == NULL) { 3080527a6266SJeff Kirsher dev_err(&pdev->dev, "no mv643xx_eth_platform_data\n"); 3081527a6266SJeff Kirsher return -ENODEV; 3082527a6266SJeff Kirsher } 3083527a6266SJeff Kirsher 3084527a6266SJeff Kirsher if (pd->shared == NULL) { 3085527a6266SJeff Kirsher dev_err(&pdev->dev, "no mv643xx_eth_platform_data->shared\n"); 3086527a6266SJeff Kirsher return -ENODEV; 3087527a6266SJeff Kirsher } 3088527a6266SJeff Kirsher 3089527a6266SJeff Kirsher dev = alloc_etherdev_mq(sizeof(struct mv643xx_eth_private), 8); 3090527a6266SJeff Kirsher if (!dev) 3091527a6266SJeff Kirsher return -ENOMEM; 3092527a6266SJeff Kirsher 3093527a6266SJeff Kirsher mp = netdev_priv(dev); 3094527a6266SJeff Kirsher platform_set_drvdata(pdev, mp); 3095527a6266SJeff Kirsher 3096527a6266SJeff Kirsher mp->shared = platform_get_drvdata(pd->shared); 3097527a6266SJeff Kirsher mp->base = mp->shared->base + 0x0400 + (pd->port_number << 10); 3098527a6266SJeff Kirsher mp->port_num = pd->port_number; 3099527a6266SJeff Kirsher 3100527a6266SJeff Kirsher mp->dev = dev; 3101527a6266SJeff Kirsher 3102cb85215fSSebastian Hesselbarth /* Kirkwood resets some registers on gated clocks. Especially 3103cb85215fSSebastian Hesselbarth * CLK125_BYPASS_EN must be cleared but is not available on 3104cb85215fSSebastian Hesselbarth * all other SoCs/System Controllers using this driver. 3105cb85215fSSebastian Hesselbarth */ 3106cb85215fSSebastian Hesselbarth if (of_device_is_compatible(pdev->dev.of_node, 3107cb85215fSSebastian Hesselbarth "marvell,kirkwood-eth-port")) 3108cb85215fSSebastian Hesselbarth wrlp(mp, PORT_SERIAL_CONTROL1, 3109cb85215fSSebastian Hesselbarth rdlp(mp, PORT_SERIAL_CONTROL1) & ~CLK125_BYPASS_EN); 3110cb85215fSSebastian Hesselbarth 3111452503ebSAndrew Lunn /* 31129a43a026SAndrew Lunn * Start with a default rate, and if there is a clock, allow 31139a43a026SAndrew Lunn * it to override the default. 3114452503ebSAndrew Lunn */ 31159a43a026SAndrew Lunn mp->t_clk = 133000000; 311620922486SSebastian Hesselbarth mp->clk = devm_clk_get(&pdev->dev, NULL); 3117452503ebSAndrew Lunn if (!IS_ERR(mp->clk)) { 3118452503ebSAndrew Lunn clk_prepare_enable(mp->clk); 3119452503ebSAndrew Lunn mp->t_clk = clk_get_rate(mp->clk); 312076723bcaSSebastian Hesselbarth } else if (!IS_ERR(mp->shared->clk)) { 312176723bcaSSebastian Hesselbarth mp->t_clk = clk_get_rate(mp->shared->clk); 3122452503ebSAndrew Lunn } 312320922486SSebastian Hesselbarth 3124527a6266SJeff Kirsher set_params(mp, pd); 3125527a6266SJeff Kirsher netif_set_real_num_tx_queues(dev, mp->txq_count); 3126527a6266SJeff Kirsher netif_set_real_num_rx_queues(dev, mp->rxq_count); 3127527a6266SJeff Kirsher 3128cc9d4598SSebastian Hesselbarth err = 0; 3129cc9d4598SSebastian Hesselbarth if (pd->phy_node) { 3130cc9d4598SSebastian Hesselbarth mp->phy = of_phy_connect(mp->dev, pd->phy_node, 3131cc9d4598SSebastian Hesselbarth mv643xx_eth_adjust_link, 0, 3132cc9d4598SSebastian Hesselbarth PHY_INTERFACE_MODE_GMII); 3133cc9d4598SSebastian Hesselbarth if (!mp->phy) 3134cc9d4598SSebastian Hesselbarth err = -ENODEV; 31356115c11fSDan Carpenter else 31361cce16d3SJason Gunthorpe phy_addr_set(mp, mp->phy->addr); 3137cc9d4598SSebastian Hesselbarth } else if (pd->phy_addr != MV643XX_ETH_PHY_NONE) { 3138527a6266SJeff Kirsher mp->phy = phy_scan(mp, pd->phy_addr); 3139527a6266SJeff Kirsher 3140cc9d4598SSebastian Hesselbarth if (IS_ERR(mp->phy)) 3141976c90b9SSimon Baatz err = PTR_ERR(mp->phy); 3142cc9d4598SSebastian Hesselbarth else 3143cc9d4598SSebastian Hesselbarth phy_init(mp, pd->speed, pd->duplex); 3144cc9d4598SSebastian Hesselbarth } 3145cc9d4598SSebastian Hesselbarth if (err == -ENODEV) { 3146976c90b9SSimon Baatz err = -EPROBE_DEFER; 3147976c90b9SSimon Baatz goto out; 3148976c90b9SSimon Baatz } 3149cc9d4598SSebastian Hesselbarth if (err) 3150cc9d4598SSebastian Hesselbarth goto out; 3151527a6266SJeff Kirsher 31527ad24ea4SWilfried Klaebe dev->ethtool_ops = &mv643xx_eth_ethtool_ops; 3153527a6266SJeff Kirsher 3154527a6266SJeff Kirsher init_pscr(mp, pd->speed, pd->duplex); 3155527a6266SJeff Kirsher 3156527a6266SJeff Kirsher 3157527a6266SJeff Kirsher mib_counters_clear(mp); 3158527a6266SJeff Kirsher 315912d5e6fdSVaishali Thakkar setup_timer(&mp->mib_counters_timer, mib_counters_timer_wrapper, 316012d5e6fdSVaishali Thakkar (unsigned long)mp); 3161527a6266SJeff Kirsher mp->mib_counters_timer.expires = jiffies + 30 * HZ; 3162527a6266SJeff Kirsher 3163527a6266SJeff Kirsher spin_lock_init(&mp->mib_counters_lock); 3164527a6266SJeff Kirsher 3165527a6266SJeff Kirsher INIT_WORK(&mp->tx_timeout_task, tx_timeout_task); 3166527a6266SJeff Kirsher 3167a3659aa0SAndrew Lunn netif_napi_add(dev, &mp->napi, mv643xx_eth_poll, NAPI_POLL_WEIGHT); 3168527a6266SJeff Kirsher 316912d5e6fdSVaishali Thakkar setup_timer(&mp->rx_oom, oom_timer_wrapper, (unsigned long)mp); 3170527a6266SJeff Kirsher 3171527a6266SJeff Kirsher 3172527a6266SJeff Kirsher res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); 3173527a6266SJeff Kirsher BUG_ON(!res); 3174527a6266SJeff Kirsher dev->irq = res->start; 3175527a6266SJeff Kirsher 3176527a6266SJeff Kirsher dev->netdev_ops = &mv643xx_eth_netdev_ops; 3177527a6266SJeff Kirsher 3178527a6266SJeff Kirsher dev->watchdog_timeo = 2 * HZ; 3179527a6266SJeff Kirsher dev->base_addr = 0; 3180527a6266SJeff Kirsher 31813ae8f4e0SEzequiel Garcia dev->features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO; 31824d48d589SEzequiel Garcia dev->vlan_features = dev->features; 31834d48d589SEzequiel Garcia 31844d48d589SEzequiel Garcia dev->features |= NETIF_F_RXCSUM; 31854d48d589SEzequiel Garcia dev->hw_features = dev->features; 3186527a6266SJeff Kirsher 318701789349SJiri Pirko dev->priv_flags |= IFF_UNICAST_FLT; 3188ee9e4956SEzequiel Garcia dev->gso_max_segs = MV643XX_MAX_TSO_SEGS; 318901789349SJiri Pirko 3190527a6266SJeff Kirsher SET_NETDEV_DEV(dev, &pdev->dev); 3191527a6266SJeff Kirsher 3192527a6266SJeff Kirsher if (mp->shared->win_protect) 3193527a6266SJeff Kirsher wrl(mp, WINDOW_PROTECT(mp->port_num), mp->shared->win_protect); 3194527a6266SJeff Kirsher 3195527a6266SJeff Kirsher netif_carrier_off(dev); 3196527a6266SJeff Kirsher 3197527a6266SJeff Kirsher wrlp(mp, SDMA_CONFIG, PORT_SDMA_CONFIG_DEFAULT_VALUE); 3198527a6266SJeff Kirsher 3199527a6266SJeff Kirsher set_rx_coal(mp, 250); 3200527a6266SJeff Kirsher set_tx_coal(mp, 0); 3201527a6266SJeff Kirsher 3202527a6266SJeff Kirsher err = register_netdev(dev); 3203527a6266SJeff Kirsher if (err) 3204527a6266SJeff Kirsher goto out; 3205527a6266SJeff Kirsher 3206527a6266SJeff Kirsher netdev_notice(dev, "port %d with MAC address %pM\n", 3207527a6266SJeff Kirsher mp->port_num, dev->dev_addr); 3208527a6266SJeff Kirsher 3209527a6266SJeff Kirsher if (mp->tx_desc_sram_size > 0) 3210527a6266SJeff Kirsher netdev_notice(dev, "configured with sram\n"); 3211527a6266SJeff Kirsher 3212527a6266SJeff Kirsher return 0; 3213527a6266SJeff Kirsher 3214527a6266SJeff Kirsher out: 321520922486SSebastian Hesselbarth if (!IS_ERR(mp->clk)) 3216baffab28SSimon Baatz clk_disable_unprepare(mp->clk); 3217527a6266SJeff Kirsher free_netdev(dev); 3218527a6266SJeff Kirsher 3219527a6266SJeff Kirsher return err; 3220527a6266SJeff Kirsher } 3221527a6266SJeff Kirsher 3222527a6266SJeff Kirsher static int mv643xx_eth_remove(struct platform_device *pdev) 3223527a6266SJeff Kirsher { 3224527a6266SJeff Kirsher struct mv643xx_eth_private *mp = platform_get_drvdata(pdev); 3225527a6266SJeff Kirsher 3226527a6266SJeff Kirsher unregister_netdev(mp->dev); 3227527a6266SJeff Kirsher if (mp->phy != NULL) 3228cec753f5SSebastian Hesselbarth phy_disconnect(mp->phy); 3229527a6266SJeff Kirsher cancel_work_sync(&mp->tx_timeout_task); 3230452503ebSAndrew Lunn 323120922486SSebastian Hesselbarth if (!IS_ERR(mp->clk)) 3232452503ebSAndrew Lunn clk_disable_unprepare(mp->clk); 32339a43a026SAndrew Lunn 3234527a6266SJeff Kirsher free_netdev(mp->dev); 3235527a6266SJeff Kirsher 3236527a6266SJeff Kirsher return 0; 3237527a6266SJeff Kirsher } 3238527a6266SJeff Kirsher 3239527a6266SJeff Kirsher static void mv643xx_eth_shutdown(struct platform_device *pdev) 3240527a6266SJeff Kirsher { 3241527a6266SJeff Kirsher struct mv643xx_eth_private *mp = platform_get_drvdata(pdev); 3242527a6266SJeff Kirsher 3243527a6266SJeff Kirsher /* Mask all interrupts on ethernet port */ 3244527a6266SJeff Kirsher wrlp(mp, INT_MASK, 0); 3245527a6266SJeff Kirsher rdlp(mp, INT_MASK); 3246527a6266SJeff Kirsher 3247527a6266SJeff Kirsher if (netif_running(mp->dev)) 3248527a6266SJeff Kirsher port_reset(mp); 3249527a6266SJeff Kirsher } 3250527a6266SJeff Kirsher 3251527a6266SJeff Kirsher static struct platform_driver mv643xx_eth_driver = { 3252527a6266SJeff Kirsher .probe = mv643xx_eth_probe, 3253527a6266SJeff Kirsher .remove = mv643xx_eth_remove, 3254527a6266SJeff Kirsher .shutdown = mv643xx_eth_shutdown, 3255527a6266SJeff Kirsher .driver = { 3256527a6266SJeff Kirsher .name = MV643XX_ETH_NAME, 3257527a6266SJeff Kirsher }, 3258527a6266SJeff Kirsher }; 3259527a6266SJeff Kirsher 3260527a6266SJeff Kirsher static int __init mv643xx_eth_init_module(void) 3261527a6266SJeff Kirsher { 3262527a6266SJeff Kirsher int rc; 3263527a6266SJeff Kirsher 3264527a6266SJeff Kirsher rc = platform_driver_register(&mv643xx_eth_shared_driver); 3265527a6266SJeff Kirsher if (!rc) { 3266527a6266SJeff Kirsher rc = platform_driver_register(&mv643xx_eth_driver); 3267527a6266SJeff Kirsher if (rc) 3268527a6266SJeff Kirsher platform_driver_unregister(&mv643xx_eth_shared_driver); 3269527a6266SJeff Kirsher } 3270527a6266SJeff Kirsher 3271527a6266SJeff Kirsher return rc; 3272527a6266SJeff Kirsher } 3273527a6266SJeff Kirsher module_init(mv643xx_eth_init_module); 3274527a6266SJeff Kirsher 3275527a6266SJeff Kirsher static void __exit mv643xx_eth_cleanup_module(void) 3276527a6266SJeff Kirsher { 3277527a6266SJeff Kirsher platform_driver_unregister(&mv643xx_eth_driver); 3278527a6266SJeff Kirsher platform_driver_unregister(&mv643xx_eth_shared_driver); 3279527a6266SJeff Kirsher } 3280527a6266SJeff Kirsher module_exit(mv643xx_eth_cleanup_module); 3281527a6266SJeff Kirsher 3282527a6266SJeff Kirsher MODULE_AUTHOR("Rabeeh Khoury, Assaf Hoffman, Matthew Dharm, " 3283527a6266SJeff Kirsher "Manish Lachwani, Dale Farnsworth and Lennert Buytenhek"); 3284527a6266SJeff Kirsher MODULE_DESCRIPTION("Ethernet driver for Marvell MV643XX"); 3285527a6266SJeff Kirsher MODULE_LICENSE("GPL"); 3286527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_SHARED_NAME); 3287527a6266SJeff Kirsher MODULE_ALIAS("platform:" MV643XX_ETH_NAME); 3288