Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Mar 24-27, 2025, special US time zones
Register
Loading...
v3.15
 
   1/*
   2 * Copyright (C) 2006-2009 Freescale Semicondutor, Inc. All rights reserved.
   3 *
   4 * Author: Shlomi Gridish <gridish@freescale.com>
   5 *	   Li Yang <leoli@freescale.com>
   6 *
   7 * Description:
   8 * QE UCC Gigabit Ethernet Driver
   9 *
  10 * This program is free software; you can redistribute  it and/or modify it
  11 * under  the terms of  the GNU General  Public License as published by the
  12 * Free Software Foundation;  either version 2 of the  License, or (at your
  13 * option) any later version.
  14 */
  15
  16#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17
  18#include <linux/kernel.h>
  19#include <linux/init.h>
  20#include <linux/errno.h>
  21#include <linux/slab.h>
  22#include <linux/stddef.h>
  23#include <linux/module.h>
  24#include <linux/interrupt.h>
  25#include <linux/netdevice.h>
  26#include <linux/etherdevice.h>
  27#include <linux/skbuff.h>
  28#include <linux/spinlock.h>
  29#include <linux/mm.h>
  30#include <linux/dma-mapping.h>
  31#include <linux/mii.h>
  32#include <linux/phy.h>
 
  33#include <linux/workqueue.h>
  34#include <linux/of_address.h>
  35#include <linux/of_irq.h>
  36#include <linux/of_mdio.h>
  37#include <linux/of_net.h>
  38#include <linux/of_platform.h>
  39
  40#include <asm/uaccess.h>
  41#include <asm/irq.h>
  42#include <asm/io.h>
  43#include <asm/immap_qe.h>
  44#include <asm/qe.h>
  45#include <asm/ucc.h>
  46#include <asm/ucc_fast.h>
  47#include <asm/machdep.h>
  48
  49#include "ucc_geth.h"
  50
  51#undef DEBUG
  52
  53#define ugeth_printk(level, format, arg...)  \
  54        printk(level format "\n", ## arg)
  55
  56#define ugeth_dbg(format, arg...)            \
  57        ugeth_printk(KERN_DEBUG , format , ## arg)
  58
  59#ifdef UGETH_VERBOSE_DEBUG
  60#define ugeth_vdbg ugeth_dbg
  61#else
  62#define ugeth_vdbg(fmt, args...) do { } while (0)
  63#endif				/* UGETH_VERBOSE_DEBUG */
  64#define UGETH_MSG_DEFAULT	(NETIF_MSG_IFUP << 1 ) - 1
  65
  66
  67static DEFINE_SPINLOCK(ugeth_lock);
  68
  69static struct {
  70	u32 msg_enable;
  71} debug = { -1 };
  72
  73module_param_named(debug, debug.msg_enable, int, 0);
  74MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 0xffff=all)");
  75
  76static struct ucc_geth_info ugeth_primary_info = {
  77	.uf_info = {
  78		    .bd_mem_part = MEM_PART_SYSTEM,
  79		    .rtsm = UCC_FAST_SEND_IDLES_BETWEEN_FRAMES,
  80		    .max_rx_buf_length = 1536,
  81		    /* adjusted at startup if max-speed 1000 */
  82		    .urfs = UCC_GETH_URFS_INIT,
  83		    .urfet = UCC_GETH_URFET_INIT,
  84		    .urfset = UCC_GETH_URFSET_INIT,
  85		    .utfs = UCC_GETH_UTFS_INIT,
  86		    .utfet = UCC_GETH_UTFET_INIT,
  87		    .utftt = UCC_GETH_UTFTT_INIT,
  88		    .ufpt = 256,
  89		    .mode = UCC_FAST_PROTOCOL_MODE_ETHERNET,
  90		    .ttx_trx = UCC_FAST_GUMR_TRANSPARENT_TTX_TRX_NORMAL,
  91		    .tenc = UCC_FAST_TX_ENCODING_NRZ,
  92		    .renc = UCC_FAST_RX_ENCODING_NRZ,
  93		    .tcrc = UCC_FAST_16_BIT_CRC,
  94		    .synl = UCC_FAST_SYNC_LEN_NOT_USED,
  95		    },
  96	.numQueuesTx = 1,
  97	.numQueuesRx = 1,
  98	.extendedFilteringChainPointer = ((uint32_t) NULL),
  99	.typeorlen = 3072 /*1536 */ ,
 100	.nonBackToBackIfgPart1 = 0x40,
 101	.nonBackToBackIfgPart2 = 0x60,
 102	.miminumInterFrameGapEnforcement = 0x50,
 103	.backToBackInterFrameGap = 0x60,
 104	.mblinterval = 128,
 105	.nortsrbytetime = 5,
 106	.fracsiz = 1,
 107	.strictpriorityq = 0xff,
 108	.altBebTruncation = 0xa,
 109	.excessDefer = 1,
 110	.maxRetransmission = 0xf,
 111	.collisionWindow = 0x37,
 112	.receiveFlowControl = 1,
 113	.transmitFlowControl = 1,
 114	.maxGroupAddrInHash = 4,
 115	.maxIndAddrInHash = 4,
 116	.prel = 7,
 117	.maxFrameLength = 1518+16, /* Add extra bytes for VLANs etc. */
 118	.minFrameLength = 64,
 119	.maxD1Length = 1520+16, /* Add extra bytes for VLANs etc. */
 120	.maxD2Length = 1520+16, /* Add extra bytes for VLANs etc. */
 121	.vlantype = 0x8100,
 122	.ecamptr = ((uint32_t) NULL),
 123	.eventRegMask = UCCE_OTHER,
 124	.pausePeriod = 0xf000,
 125	.interruptcoalescingmaxvalue = {1, 1, 1, 1, 1, 1, 1, 1},
 126	.bdRingLenTx = {
 127			TX_BD_RING_LEN,
 128			TX_BD_RING_LEN,
 129			TX_BD_RING_LEN,
 130			TX_BD_RING_LEN,
 131			TX_BD_RING_LEN,
 132			TX_BD_RING_LEN,
 133			TX_BD_RING_LEN,
 134			TX_BD_RING_LEN},
 135
 136	.bdRingLenRx = {
 137			RX_BD_RING_LEN,
 138			RX_BD_RING_LEN,
 139			RX_BD_RING_LEN,
 140			RX_BD_RING_LEN,
 141			RX_BD_RING_LEN,
 142			RX_BD_RING_LEN,
 143			RX_BD_RING_LEN,
 144			RX_BD_RING_LEN},
 145
 146	.numStationAddresses = UCC_GETH_NUM_OF_STATION_ADDRESSES_1,
 147	.largestexternallookupkeysize =
 148	    QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_NONE,
 149	.statisticsMode = UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE |
 150		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX |
 151		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX,
 152	.vlanOperationTagged = UCC_GETH_VLAN_OPERATION_TAGGED_NOP,
 153	.vlanOperationNonTagged = UCC_GETH_VLAN_OPERATION_NON_TAGGED_NOP,
 154	.rxQoSMode = UCC_GETH_QOS_MODE_DEFAULT,
 155	.aufc = UPSMR_AUTOMATIC_FLOW_CONTROL_MODE_NONE,
 156	.padAndCrc = MACCFG2_PAD_AND_CRC_MODE_PAD_AND_CRC,
 157	.numThreadsTx = UCC_GETH_NUM_OF_THREADS_1,
 158	.numThreadsRx = UCC_GETH_NUM_OF_THREADS_1,
 159	.riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
 160	.riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
 161};
 162
 163static struct ucc_geth_info ugeth_info[8];
 164
 165#ifdef DEBUG
 166static void mem_disp(u8 *addr, int size)
 167{
 168	u8 *i;
 169	int size16Aling = (size >> 4) << 4;
 170	int size4Aling = (size >> 2) << 2;
 171	int notAlign = 0;
 172	if (size % 16)
 173		notAlign = 1;
 174
 175	for (i = addr; (u32) i < (u32) addr + size16Aling; i += 16)
 176		printk("0x%08x: %08x %08x %08x %08x\r\n",
 177		       (u32) i,
 178		       *((u32 *) (i)),
 179		       *((u32 *) (i + 4)),
 180		       *((u32 *) (i + 8)), *((u32 *) (i + 12)));
 181	if (notAlign == 1)
 182		printk("0x%08x: ", (u32) i);
 183	for (; (u32) i < (u32) addr + size4Aling; i += 4)
 184		printk("%08x ", *((u32 *) (i)));
 185	for (; (u32) i < (u32) addr + size; i++)
 186		printk("%02x", *((i)));
 187	if (notAlign == 1)
 188		printk("\r\n");
 189}
 190#endif /* DEBUG */
 191
 192static struct list_head *dequeue(struct list_head *lh)
 193{
 194	unsigned long flags;
 195
 196	spin_lock_irqsave(&ugeth_lock, flags);
 197	if (!list_empty(lh)) {
 198		struct list_head *node = lh->next;
 199		list_del(node);
 200		spin_unlock_irqrestore(&ugeth_lock, flags);
 201		return node;
 202	} else {
 203		spin_unlock_irqrestore(&ugeth_lock, flags);
 204		return NULL;
 205	}
 206}
 207
 208static struct sk_buff *get_new_skb(struct ucc_geth_private *ugeth,
 209		u8 __iomem *bd)
 210{
 211	struct sk_buff *skb;
 212
 213	skb = netdev_alloc_skb(ugeth->ndev,
 214			       ugeth->ug_info->uf_info.max_rx_buf_length +
 215			       UCC_GETH_RX_DATA_BUF_ALIGNMENT);
 216	if (!skb)
 217		return NULL;
 218
 219	/* We need the data buffer to be aligned properly.  We will reserve
 220	 * as many bytes as needed to align the data properly
 221	 */
 222	skb_reserve(skb,
 223		    UCC_GETH_RX_DATA_BUF_ALIGNMENT -
 224		    (((unsigned)skb->data) & (UCC_GETH_RX_DATA_BUF_ALIGNMENT -
 225					      1)));
 226
 227	out_be32(&((struct qe_bd __iomem *)bd)->buf,
 228		      dma_map_single(ugeth->dev,
 229				     skb->data,
 230				     ugeth->ug_info->uf_info.max_rx_buf_length +
 231				     UCC_GETH_RX_DATA_BUF_ALIGNMENT,
 232				     DMA_FROM_DEVICE));
 233
 234	out_be32((u32 __iomem *)bd,
 235			(R_E | R_I | (in_be32((u32 __iomem*)bd) & R_W)));
 236
 237	return skb;
 238}
 239
 240static int rx_bd_buffer_set(struct ucc_geth_private *ugeth, u8 rxQ)
 241{
 242	u8 __iomem *bd;
 243	u32 bd_status;
 244	struct sk_buff *skb;
 245	int i;
 246
 247	bd = ugeth->p_rx_bd_ring[rxQ];
 248	i = 0;
 249
 250	do {
 251		bd_status = in_be32((u32 __iomem *)bd);
 252		skb = get_new_skb(ugeth, bd);
 253
 254		if (!skb)	/* If can not allocate data buffer,
 255				abort. Cleanup will be elsewhere */
 256			return -ENOMEM;
 257
 258		ugeth->rx_skbuff[rxQ][i] = skb;
 259
 260		/* advance the BD pointer */
 261		bd += sizeof(struct qe_bd);
 262		i++;
 263	} while (!(bd_status & R_W));
 264
 265	return 0;
 266}
 267
 268static int fill_init_enet_entries(struct ucc_geth_private *ugeth,
 269				  u32 *p_start,
 270				  u8 num_entries,
 271				  u32 thread_size,
 272				  u32 thread_alignment,
 273				  unsigned int risc,
 274				  int skip_page_for_first_entry)
 275{
 276	u32 init_enet_offset;
 277	u8 i;
 278	int snum;
 279
 280	for (i = 0; i < num_entries; i++) {
 281		if ((snum = qe_get_snum()) < 0) {
 282			if (netif_msg_ifup(ugeth))
 283				pr_err("Can not get SNUM\n");
 284			return snum;
 285		}
 286		if ((i == 0) && skip_page_for_first_entry)
 287		/* First entry of Rx does not have page */
 288			init_enet_offset = 0;
 289		else {
 290			init_enet_offset =
 291			    qe_muram_alloc(thread_size, thread_alignment);
 292			if (IS_ERR_VALUE(init_enet_offset)) {
 293				if (netif_msg_ifup(ugeth))
 294					pr_err("Can not allocate DPRAM memory\n");
 295				qe_put_snum((u8) snum);
 296				return -ENOMEM;
 297			}
 298		}
 299		*(p_start++) =
 300		    ((u8) snum << ENET_INIT_PARAM_SNUM_SHIFT) | init_enet_offset
 301		    | risc;
 302	}
 303
 304	return 0;
 305}
 306
 307static int return_init_enet_entries(struct ucc_geth_private *ugeth,
 308				    u32 *p_start,
 309				    u8 num_entries,
 310				    unsigned int risc,
 311				    int skip_page_for_first_entry)
 312{
 313	u32 init_enet_offset;
 314	u8 i;
 315	int snum;
 316
 317	for (i = 0; i < num_entries; i++) {
 318		u32 val = *p_start;
 319
 320		/* Check that this entry was actually valid --
 321		needed in case failed in allocations */
 322		if ((val & ENET_INIT_PARAM_RISC_MASK) == risc) {
 323			snum =
 324			    (u32) (val & ENET_INIT_PARAM_SNUM_MASK) >>
 325			    ENET_INIT_PARAM_SNUM_SHIFT;
 326			qe_put_snum((u8) snum);
 327			if (!((i == 0) && skip_page_for_first_entry)) {
 328			/* First entry of Rx does not have page */
 329				init_enet_offset =
 330				    (val & ENET_INIT_PARAM_PTR_MASK);
 331				qe_muram_free(init_enet_offset);
 332			}
 333			*p_start++ = 0;
 334		}
 335	}
 336
 337	return 0;
 338}
 339
 340#ifdef DEBUG
 341static int dump_init_enet_entries(struct ucc_geth_private *ugeth,
 342				  u32 __iomem *p_start,
 343				  u8 num_entries,
 344				  u32 thread_size,
 345				  unsigned int risc,
 346				  int skip_page_for_first_entry)
 347{
 348	u32 init_enet_offset;
 349	u8 i;
 350	int snum;
 351
 352	for (i = 0; i < num_entries; i++) {
 353		u32 val = in_be32(p_start);
 354
 355		/* Check that this entry was actually valid --
 356		needed in case failed in allocations */
 357		if ((val & ENET_INIT_PARAM_RISC_MASK) == risc) {
 358			snum =
 359			    (u32) (val & ENET_INIT_PARAM_SNUM_MASK) >>
 360			    ENET_INIT_PARAM_SNUM_SHIFT;
 361			qe_put_snum((u8) snum);
 362			if (!((i == 0) && skip_page_for_first_entry)) {
 363			/* First entry of Rx does not have page */
 364				init_enet_offset =
 365				    (in_be32(p_start) &
 366				     ENET_INIT_PARAM_PTR_MASK);
 367				pr_info("Init enet entry %d:\n", i);
 368				pr_info("Base address: 0x%08x\n",
 369					(u32)qe_muram_addr(init_enet_offset));
 370				mem_disp(qe_muram_addr(init_enet_offset),
 371					 thread_size);
 372			}
 373			p_start++;
 374		}
 375	}
 376
 377	return 0;
 378}
 379#endif
 380
 381static void put_enet_addr_container(struct enet_addr_container *enet_addr_cont)
 382{
 383	kfree(enet_addr_cont);
 384}
 385
 386static void set_mac_addr(__be16 __iomem *reg, u8 *mac)
 387{
 388	out_be16(&reg[0], ((u16)mac[5] << 8) | mac[4]);
 389	out_be16(&reg[1], ((u16)mac[3] << 8) | mac[2]);
 390	out_be16(&reg[2], ((u16)mac[1] << 8) | mac[0]);
 391}
 392
 393static int hw_clear_addr_in_paddr(struct ucc_geth_private *ugeth, u8 paddr_num)
 394{
 395	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
 396
 397	if (paddr_num >= NUM_OF_PADDRS) {
 398		pr_warn("%s: Invalid paddr_num: %u\n", __func__, paddr_num);
 399		return -EINVAL;
 400	}
 401
 402	p_82xx_addr_filt =
 403	    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->p_rx_glbl_pram->
 404	    addressfiltering;
 405
 406	/* Writing address ff.ff.ff.ff.ff.ff disables address
 407	recognition for this register */
 408	out_be16(&p_82xx_addr_filt->paddr[paddr_num].h, 0xffff);
 409	out_be16(&p_82xx_addr_filt->paddr[paddr_num].m, 0xffff);
 410	out_be16(&p_82xx_addr_filt->paddr[paddr_num].l, 0xffff);
 411
 412	return 0;
 413}
 414
 415static void hw_add_addr_in_hash(struct ucc_geth_private *ugeth,
 416                                u8 *p_enet_addr)
 417{
 418	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
 419	u32 cecr_subblock;
 420
 421	p_82xx_addr_filt =
 422	    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->p_rx_glbl_pram->
 423	    addressfiltering;
 424
 425	cecr_subblock =
 426	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
 427
 428	/* Ethernet frames are defined in Little Endian mode,
 429	therefore to insert */
 430	/* the address to the hash (Big Endian mode), we reverse the bytes.*/
 431
 432	set_mac_addr(&p_82xx_addr_filt->taddr.h, p_enet_addr);
 433
 434	qe_issue_cmd(QE_SET_GROUP_ADDRESS, cecr_subblock,
 435		     QE_CR_PROTOCOL_ETHERNET, 0);
 436}
 437
 438#ifdef DEBUG
 439static void get_statistics(struct ucc_geth_private *ugeth,
 440			   struct ucc_geth_tx_firmware_statistics *
 441			   tx_firmware_statistics,
 442			   struct ucc_geth_rx_firmware_statistics *
 443			   rx_firmware_statistics,
 444			   struct ucc_geth_hardware_statistics *hardware_statistics)
 445{
 446	struct ucc_fast __iomem *uf_regs;
 447	struct ucc_geth __iomem *ug_regs;
 448	struct ucc_geth_tx_firmware_statistics_pram *p_tx_fw_statistics_pram;
 449	struct ucc_geth_rx_firmware_statistics_pram *p_rx_fw_statistics_pram;
 450
 451	ug_regs = ugeth->ug_regs;
 452	uf_regs = (struct ucc_fast __iomem *) ug_regs;
 453	p_tx_fw_statistics_pram = ugeth->p_tx_fw_statistics_pram;
 454	p_rx_fw_statistics_pram = ugeth->p_rx_fw_statistics_pram;
 455
 456	/* Tx firmware only if user handed pointer and driver actually
 457	gathers Tx firmware statistics */
 458	if (tx_firmware_statistics && p_tx_fw_statistics_pram) {
 459		tx_firmware_statistics->sicoltx =
 460		    in_be32(&p_tx_fw_statistics_pram->sicoltx);
 461		tx_firmware_statistics->mulcoltx =
 462		    in_be32(&p_tx_fw_statistics_pram->mulcoltx);
 463		tx_firmware_statistics->latecoltxfr =
 464		    in_be32(&p_tx_fw_statistics_pram->latecoltxfr);
 465		tx_firmware_statistics->frabortduecol =
 466		    in_be32(&p_tx_fw_statistics_pram->frabortduecol);
 467		tx_firmware_statistics->frlostinmactxer =
 468		    in_be32(&p_tx_fw_statistics_pram->frlostinmactxer);
 469		tx_firmware_statistics->carriersenseertx =
 470		    in_be32(&p_tx_fw_statistics_pram->carriersenseertx);
 471		tx_firmware_statistics->frtxok =
 472		    in_be32(&p_tx_fw_statistics_pram->frtxok);
 473		tx_firmware_statistics->txfrexcessivedefer =
 474		    in_be32(&p_tx_fw_statistics_pram->txfrexcessivedefer);
 475		tx_firmware_statistics->txpkts256 =
 476		    in_be32(&p_tx_fw_statistics_pram->txpkts256);
 477		tx_firmware_statistics->txpkts512 =
 478		    in_be32(&p_tx_fw_statistics_pram->txpkts512);
 479		tx_firmware_statistics->txpkts1024 =
 480		    in_be32(&p_tx_fw_statistics_pram->txpkts1024);
 481		tx_firmware_statistics->txpktsjumbo =
 482		    in_be32(&p_tx_fw_statistics_pram->txpktsjumbo);
 483	}
 484
 485	/* Rx firmware only if user handed pointer and driver actually
 486	 * gathers Rx firmware statistics */
 487	if (rx_firmware_statistics && p_rx_fw_statistics_pram) {
 488		int i;
 489		rx_firmware_statistics->frrxfcser =
 490		    in_be32(&p_rx_fw_statistics_pram->frrxfcser);
 491		rx_firmware_statistics->fraligner =
 492		    in_be32(&p_rx_fw_statistics_pram->fraligner);
 493		rx_firmware_statistics->inrangelenrxer =
 494		    in_be32(&p_rx_fw_statistics_pram->inrangelenrxer);
 495		rx_firmware_statistics->outrangelenrxer =
 496		    in_be32(&p_rx_fw_statistics_pram->outrangelenrxer);
 497		rx_firmware_statistics->frtoolong =
 498		    in_be32(&p_rx_fw_statistics_pram->frtoolong);
 499		rx_firmware_statistics->runt =
 500		    in_be32(&p_rx_fw_statistics_pram->runt);
 501		rx_firmware_statistics->verylongevent =
 502		    in_be32(&p_rx_fw_statistics_pram->verylongevent);
 503		rx_firmware_statistics->symbolerror =
 504		    in_be32(&p_rx_fw_statistics_pram->symbolerror);
 505		rx_firmware_statistics->dropbsy =
 506		    in_be32(&p_rx_fw_statistics_pram->dropbsy);
 507		for (i = 0; i < 0x8; i++)
 508			rx_firmware_statistics->res0[i] =
 509			    p_rx_fw_statistics_pram->res0[i];
 510		rx_firmware_statistics->mismatchdrop =
 511		    in_be32(&p_rx_fw_statistics_pram->mismatchdrop);
 512		rx_firmware_statistics->underpkts =
 513		    in_be32(&p_rx_fw_statistics_pram->underpkts);
 514		rx_firmware_statistics->pkts256 =
 515		    in_be32(&p_rx_fw_statistics_pram->pkts256);
 516		rx_firmware_statistics->pkts512 =
 517		    in_be32(&p_rx_fw_statistics_pram->pkts512);
 518		rx_firmware_statistics->pkts1024 =
 519		    in_be32(&p_rx_fw_statistics_pram->pkts1024);
 520		rx_firmware_statistics->pktsjumbo =
 521		    in_be32(&p_rx_fw_statistics_pram->pktsjumbo);
 522		rx_firmware_statistics->frlossinmacer =
 523		    in_be32(&p_rx_fw_statistics_pram->frlossinmacer);
 524		rx_firmware_statistics->pausefr =
 525		    in_be32(&p_rx_fw_statistics_pram->pausefr);
 526		for (i = 0; i < 0x4; i++)
 527			rx_firmware_statistics->res1[i] =
 528			    p_rx_fw_statistics_pram->res1[i];
 529		rx_firmware_statistics->removevlan =
 530		    in_be32(&p_rx_fw_statistics_pram->removevlan);
 531		rx_firmware_statistics->replacevlan =
 532		    in_be32(&p_rx_fw_statistics_pram->replacevlan);
 533		rx_firmware_statistics->insertvlan =
 534		    in_be32(&p_rx_fw_statistics_pram->insertvlan);
 535	}
 536
 537	/* Hardware only if user handed pointer and driver actually
 538	gathers hardware statistics */
 539	if (hardware_statistics &&
 540	    (in_be32(&uf_regs->upsmr) & UCC_GETH_UPSMR_HSE)) {
 541		hardware_statistics->tx64 = in_be32(&ug_regs->tx64);
 542		hardware_statistics->tx127 = in_be32(&ug_regs->tx127);
 543		hardware_statistics->tx255 = in_be32(&ug_regs->tx255);
 544		hardware_statistics->rx64 = in_be32(&ug_regs->rx64);
 545		hardware_statistics->rx127 = in_be32(&ug_regs->rx127);
 546		hardware_statistics->rx255 = in_be32(&ug_regs->rx255);
 547		hardware_statistics->txok = in_be32(&ug_regs->txok);
 548		hardware_statistics->txcf = in_be16(&ug_regs->txcf);
 549		hardware_statistics->tmca = in_be32(&ug_regs->tmca);
 550		hardware_statistics->tbca = in_be32(&ug_regs->tbca);
 551		hardware_statistics->rxfok = in_be32(&ug_regs->rxfok);
 552		hardware_statistics->rxbok = in_be32(&ug_regs->rxbok);
 553		hardware_statistics->rbyt = in_be32(&ug_regs->rbyt);
 554		hardware_statistics->rmca = in_be32(&ug_regs->rmca);
 555		hardware_statistics->rbca = in_be32(&ug_regs->rbca);
 556	}
 557}
 558
 559static void dump_bds(struct ucc_geth_private *ugeth)
 560{
 561	int i;
 562	int length;
 563
 564	for (i = 0; i < ugeth->ug_info->numQueuesTx; i++) {
 565		if (ugeth->p_tx_bd_ring[i]) {
 566			length =
 567			    (ugeth->ug_info->bdRingLenTx[i] *
 568			     sizeof(struct qe_bd));
 569			pr_info("TX BDs[%d]\n", i);
 570			mem_disp(ugeth->p_tx_bd_ring[i], length);
 571		}
 572	}
 573	for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
 574		if (ugeth->p_rx_bd_ring[i]) {
 575			length =
 576			    (ugeth->ug_info->bdRingLenRx[i] *
 577			     sizeof(struct qe_bd));
 578			pr_info("RX BDs[%d]\n", i);
 579			mem_disp(ugeth->p_rx_bd_ring[i], length);
 580		}
 581	}
 582}
 583
 584static void dump_regs(struct ucc_geth_private *ugeth)
 585{
 586	int i;
 587
 588	pr_info("UCC%d Geth registers:\n", ugeth->ug_info->uf_info.ucc_num + 1);
 589	pr_info("Base address: 0x%08x\n", (u32)ugeth->ug_regs);
 590
 591	pr_info("maccfg1    : addr - 0x%08x, val - 0x%08x\n",
 592		(u32)&ugeth->ug_regs->maccfg1,
 593		in_be32(&ugeth->ug_regs->maccfg1));
 594	pr_info("maccfg2    : addr - 0x%08x, val - 0x%08x\n",
 595		(u32)&ugeth->ug_regs->maccfg2,
 596		in_be32(&ugeth->ug_regs->maccfg2));
 597	pr_info("ipgifg     : addr - 0x%08x, val - 0x%08x\n",
 598		(u32)&ugeth->ug_regs->ipgifg,
 599		in_be32(&ugeth->ug_regs->ipgifg));
 600	pr_info("hafdup     : addr - 0x%08x, val - 0x%08x\n",
 601		(u32)&ugeth->ug_regs->hafdup,
 602		in_be32(&ugeth->ug_regs->hafdup));
 603	pr_info("ifctl      : addr - 0x%08x, val - 0x%08x\n",
 604		(u32)&ugeth->ug_regs->ifctl,
 605		in_be32(&ugeth->ug_regs->ifctl));
 606	pr_info("ifstat     : addr - 0x%08x, val - 0x%08x\n",
 607		(u32)&ugeth->ug_regs->ifstat,
 608		in_be32(&ugeth->ug_regs->ifstat));
 609	pr_info("macstnaddr1: addr - 0x%08x, val - 0x%08x\n",
 610		(u32)&ugeth->ug_regs->macstnaddr1,
 611		in_be32(&ugeth->ug_regs->macstnaddr1));
 612	pr_info("macstnaddr2: addr - 0x%08x, val - 0x%08x\n",
 613		(u32)&ugeth->ug_regs->macstnaddr2,
 614		in_be32(&ugeth->ug_regs->macstnaddr2));
 615	pr_info("uempr      : addr - 0x%08x, val - 0x%08x\n",
 616		(u32)&ugeth->ug_regs->uempr,
 617		in_be32(&ugeth->ug_regs->uempr));
 618	pr_info("utbipar    : addr - 0x%08x, val - 0x%08x\n",
 619		(u32)&ugeth->ug_regs->utbipar,
 620		in_be32(&ugeth->ug_regs->utbipar));
 621	pr_info("uescr      : addr - 0x%08x, val - 0x%04x\n",
 622		(u32)&ugeth->ug_regs->uescr,
 623		in_be16(&ugeth->ug_regs->uescr));
 624	pr_info("tx64       : addr - 0x%08x, val - 0x%08x\n",
 625		(u32)&ugeth->ug_regs->tx64,
 626		in_be32(&ugeth->ug_regs->tx64));
 627	pr_info("tx127      : addr - 0x%08x, val - 0x%08x\n",
 628		(u32)&ugeth->ug_regs->tx127,
 629		in_be32(&ugeth->ug_regs->tx127));
 630	pr_info("tx255      : addr - 0x%08x, val - 0x%08x\n",
 631		(u32)&ugeth->ug_regs->tx255,
 632		in_be32(&ugeth->ug_regs->tx255));
 633	pr_info("rx64       : addr - 0x%08x, val - 0x%08x\n",
 634		(u32)&ugeth->ug_regs->rx64,
 635		in_be32(&ugeth->ug_regs->rx64));
 636	pr_info("rx127      : addr - 0x%08x, val - 0x%08x\n",
 637		(u32)&ugeth->ug_regs->rx127,
 638		in_be32(&ugeth->ug_regs->rx127));
 639	pr_info("rx255      : addr - 0x%08x, val - 0x%08x\n",
 640		(u32)&ugeth->ug_regs->rx255,
 641		in_be32(&ugeth->ug_regs->rx255));
 642	pr_info("txok       : addr - 0x%08x, val - 0x%08x\n",
 643		(u32)&ugeth->ug_regs->txok,
 644		in_be32(&ugeth->ug_regs->txok));
 645	pr_info("txcf       : addr - 0x%08x, val - 0x%04x\n",
 646		(u32)&ugeth->ug_regs->txcf,
 647		in_be16(&ugeth->ug_regs->txcf));
 648	pr_info("tmca       : addr - 0x%08x, val - 0x%08x\n",
 649		(u32)&ugeth->ug_regs->tmca,
 650		in_be32(&ugeth->ug_regs->tmca));
 651	pr_info("tbca       : addr - 0x%08x, val - 0x%08x\n",
 652		(u32)&ugeth->ug_regs->tbca,
 653		in_be32(&ugeth->ug_regs->tbca));
 654	pr_info("rxfok      : addr - 0x%08x, val - 0x%08x\n",
 655		(u32)&ugeth->ug_regs->rxfok,
 656		in_be32(&ugeth->ug_regs->rxfok));
 657	pr_info("rxbok      : addr - 0x%08x, val - 0x%08x\n",
 658		(u32)&ugeth->ug_regs->rxbok,
 659		in_be32(&ugeth->ug_regs->rxbok));
 660	pr_info("rbyt       : addr - 0x%08x, val - 0x%08x\n",
 661		(u32)&ugeth->ug_regs->rbyt,
 662		in_be32(&ugeth->ug_regs->rbyt));
 663	pr_info("rmca       : addr - 0x%08x, val - 0x%08x\n",
 664		(u32)&ugeth->ug_regs->rmca,
 665		in_be32(&ugeth->ug_regs->rmca));
 666	pr_info("rbca       : addr - 0x%08x, val - 0x%08x\n",
 667		(u32)&ugeth->ug_regs->rbca,
 668		in_be32(&ugeth->ug_regs->rbca));
 669	pr_info("scar       : addr - 0x%08x, val - 0x%08x\n",
 670		(u32)&ugeth->ug_regs->scar,
 671		in_be32(&ugeth->ug_regs->scar));
 672	pr_info("scam       : addr - 0x%08x, val - 0x%08x\n",
 673		(u32)&ugeth->ug_regs->scam,
 674		in_be32(&ugeth->ug_regs->scam));
 675
 676	if (ugeth->p_thread_data_tx) {
 677		int numThreadsTxNumerical;
 678		switch (ugeth->ug_info->numThreadsTx) {
 679		case UCC_GETH_NUM_OF_THREADS_1:
 680			numThreadsTxNumerical = 1;
 681			break;
 682		case UCC_GETH_NUM_OF_THREADS_2:
 683			numThreadsTxNumerical = 2;
 684			break;
 685		case UCC_GETH_NUM_OF_THREADS_4:
 686			numThreadsTxNumerical = 4;
 687			break;
 688		case UCC_GETH_NUM_OF_THREADS_6:
 689			numThreadsTxNumerical = 6;
 690			break;
 691		case UCC_GETH_NUM_OF_THREADS_8:
 692			numThreadsTxNumerical = 8;
 693			break;
 694		default:
 695			numThreadsTxNumerical = 0;
 696			break;
 697		}
 698
 699		pr_info("Thread data TXs:\n");
 700		pr_info("Base address: 0x%08x\n",
 701			(u32)ugeth->p_thread_data_tx);
 702		for (i = 0; i < numThreadsTxNumerical; i++) {
 703			pr_info("Thread data TX[%d]:\n", i);
 704			pr_info("Base address: 0x%08x\n",
 705				(u32)&ugeth->p_thread_data_tx[i]);
 706			mem_disp((u8 *) & ugeth->p_thread_data_tx[i],
 707				 sizeof(struct ucc_geth_thread_data_tx));
 708		}
 709	}
 710	if (ugeth->p_thread_data_rx) {
 711		int numThreadsRxNumerical;
 712		switch (ugeth->ug_info->numThreadsRx) {
 713		case UCC_GETH_NUM_OF_THREADS_1:
 714			numThreadsRxNumerical = 1;
 715			break;
 716		case UCC_GETH_NUM_OF_THREADS_2:
 717			numThreadsRxNumerical = 2;
 718			break;
 719		case UCC_GETH_NUM_OF_THREADS_4:
 720			numThreadsRxNumerical = 4;
 721			break;
 722		case UCC_GETH_NUM_OF_THREADS_6:
 723			numThreadsRxNumerical = 6;
 724			break;
 725		case UCC_GETH_NUM_OF_THREADS_8:
 726			numThreadsRxNumerical = 8;
 727			break;
 728		default:
 729			numThreadsRxNumerical = 0;
 730			break;
 731		}
 732
 733		pr_info("Thread data RX:\n");
 734		pr_info("Base address: 0x%08x\n",
 735			(u32)ugeth->p_thread_data_rx);
 736		for (i = 0; i < numThreadsRxNumerical; i++) {
 737			pr_info("Thread data RX[%d]:\n", i);
 738			pr_info("Base address: 0x%08x\n",
 739				(u32)&ugeth->p_thread_data_rx[i]);
 740			mem_disp((u8 *) & ugeth->p_thread_data_rx[i],
 741				 sizeof(struct ucc_geth_thread_data_rx));
 742		}
 743	}
 744	if (ugeth->p_exf_glbl_param) {
 745		pr_info("EXF global param:\n");
 746		pr_info("Base address: 0x%08x\n",
 747			(u32)ugeth->p_exf_glbl_param);
 748		mem_disp((u8 *) ugeth->p_exf_glbl_param,
 749			 sizeof(*ugeth->p_exf_glbl_param));
 750	}
 751	if (ugeth->p_tx_glbl_pram) {
 752		pr_info("TX global param:\n");
 753		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_tx_glbl_pram);
 754		pr_info("temoder      : addr - 0x%08x, val - 0x%04x\n",
 755			(u32)&ugeth->p_tx_glbl_pram->temoder,
 756			in_be16(&ugeth->p_tx_glbl_pram->temoder));
 757	       pr_info("sqptr        : addr - 0x%08x, val - 0x%08x\n",
 758			(u32)&ugeth->p_tx_glbl_pram->sqptr,
 759			in_be32(&ugeth->p_tx_glbl_pram->sqptr));
 760		pr_info("schedulerbasepointer: addr - 0x%08x, val - 0x%08x\n",
 761			(u32)&ugeth->p_tx_glbl_pram->schedulerbasepointer,
 762			in_be32(&ugeth->p_tx_glbl_pram->schedulerbasepointer));
 763		pr_info("txrmonbaseptr: addr - 0x%08x, val - 0x%08x\n",
 764			(u32)&ugeth->p_tx_glbl_pram->txrmonbaseptr,
 765			in_be32(&ugeth->p_tx_glbl_pram->txrmonbaseptr));
 766		pr_info("tstate       : addr - 0x%08x, val - 0x%08x\n",
 767			(u32)&ugeth->p_tx_glbl_pram->tstate,
 768			in_be32(&ugeth->p_tx_glbl_pram->tstate));
 769		pr_info("iphoffset[0] : addr - 0x%08x, val - 0x%02x\n",
 770			(u32)&ugeth->p_tx_glbl_pram->iphoffset[0],
 771			ugeth->p_tx_glbl_pram->iphoffset[0]);
 772		pr_info("iphoffset[1] : addr - 0x%08x, val - 0x%02x\n",
 773			(u32)&ugeth->p_tx_glbl_pram->iphoffset[1],
 774			ugeth->p_tx_glbl_pram->iphoffset[1]);
 775		pr_info("iphoffset[2] : addr - 0x%08x, val - 0x%02x\n",
 776			(u32)&ugeth->p_tx_glbl_pram->iphoffset[2],
 777			ugeth->p_tx_glbl_pram->iphoffset[2]);
 778		pr_info("iphoffset[3] : addr - 0x%08x, val - 0x%02x\n",
 779			(u32)&ugeth->p_tx_glbl_pram->iphoffset[3],
 780			ugeth->p_tx_glbl_pram->iphoffset[3]);
 781		pr_info("iphoffset[4] : addr - 0x%08x, val - 0x%02x\n",
 782			(u32)&ugeth->p_tx_glbl_pram->iphoffset[4],
 783			ugeth->p_tx_glbl_pram->iphoffset[4]);
 784		pr_info("iphoffset[5] : addr - 0x%08x, val - 0x%02x\n",
 785			(u32)&ugeth->p_tx_glbl_pram->iphoffset[5],
 786			ugeth->p_tx_glbl_pram->iphoffset[5]);
 787		pr_info("iphoffset[6] : addr - 0x%08x, val - 0x%02x\n",
 788			(u32)&ugeth->p_tx_glbl_pram->iphoffset[6],
 789			ugeth->p_tx_glbl_pram->iphoffset[6]);
 790		pr_info("iphoffset[7] : addr - 0x%08x, val - 0x%02x\n",
 791			(u32)&ugeth->p_tx_glbl_pram->iphoffset[7],
 792			ugeth->p_tx_glbl_pram->iphoffset[7]);
 793		pr_info("vtagtable[0] : addr - 0x%08x, val - 0x%08x\n",
 794			(u32)&ugeth->p_tx_glbl_pram->vtagtable[0],
 795			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[0]));
 796		pr_info("vtagtable[1] : addr - 0x%08x, val - 0x%08x\n",
 797			(u32)&ugeth->p_tx_glbl_pram->vtagtable[1],
 798			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[1]));
 799		pr_info("vtagtable[2] : addr - 0x%08x, val - 0x%08x\n",
 800			(u32)&ugeth->p_tx_glbl_pram->vtagtable[2],
 801			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[2]));
 802		pr_info("vtagtable[3] : addr - 0x%08x, val - 0x%08x\n",
 803			(u32)&ugeth->p_tx_glbl_pram->vtagtable[3],
 804			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[3]));
 805		pr_info("vtagtable[4] : addr - 0x%08x, val - 0x%08x\n",
 806			(u32)&ugeth->p_tx_glbl_pram->vtagtable[4],
 807			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[4]));
 808		pr_info("vtagtable[5] : addr - 0x%08x, val - 0x%08x\n",
 809			(u32)&ugeth->p_tx_glbl_pram->vtagtable[5],
 810			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[5]));
 811		pr_info("vtagtable[6] : addr - 0x%08x, val - 0x%08x\n",
 812			(u32)&ugeth->p_tx_glbl_pram->vtagtable[6],
 813			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[6]));
 814		pr_info("vtagtable[7] : addr - 0x%08x, val - 0x%08x\n",
 815			(u32)&ugeth->p_tx_glbl_pram->vtagtable[7],
 816			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[7]));
 817		pr_info("tqptr        : addr - 0x%08x, val - 0x%08x\n",
 818			(u32)&ugeth->p_tx_glbl_pram->tqptr,
 819			in_be32(&ugeth->p_tx_glbl_pram->tqptr));
 820	}
 821	if (ugeth->p_rx_glbl_pram) {
 822		pr_info("RX global param:\n");
 823		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_rx_glbl_pram);
 824		pr_info("remoder         : addr - 0x%08x, val - 0x%08x\n",
 825			(u32)&ugeth->p_rx_glbl_pram->remoder,
 826			in_be32(&ugeth->p_rx_glbl_pram->remoder));
 827		pr_info("rqptr           : addr - 0x%08x, val - 0x%08x\n",
 828			(u32)&ugeth->p_rx_glbl_pram->rqptr,
 829			in_be32(&ugeth->p_rx_glbl_pram->rqptr));
 830		pr_info("typeorlen       : addr - 0x%08x, val - 0x%04x\n",
 831			(u32)&ugeth->p_rx_glbl_pram->typeorlen,
 832			in_be16(&ugeth->p_rx_glbl_pram->typeorlen));
 833		pr_info("rxgstpack       : addr - 0x%08x, val - 0x%02x\n",
 834			(u32)&ugeth->p_rx_glbl_pram->rxgstpack,
 835			ugeth->p_rx_glbl_pram->rxgstpack);
 836		pr_info("rxrmonbaseptr   : addr - 0x%08x, val - 0x%08x\n",
 837			(u32)&ugeth->p_rx_glbl_pram->rxrmonbaseptr,
 838			in_be32(&ugeth->p_rx_glbl_pram->rxrmonbaseptr));
 839		pr_info("intcoalescingptr: addr - 0x%08x, val - 0x%08x\n",
 840			(u32)&ugeth->p_rx_glbl_pram->intcoalescingptr,
 841			in_be32(&ugeth->p_rx_glbl_pram->intcoalescingptr));
 842		pr_info("rstate          : addr - 0x%08x, val - 0x%02x\n",
 843			(u32)&ugeth->p_rx_glbl_pram->rstate,
 844			ugeth->p_rx_glbl_pram->rstate);
 845		pr_info("mrblr           : addr - 0x%08x, val - 0x%04x\n",
 846			(u32)&ugeth->p_rx_glbl_pram->mrblr,
 847			in_be16(&ugeth->p_rx_glbl_pram->mrblr));
 848		pr_info("rbdqptr         : addr - 0x%08x, val - 0x%08x\n",
 849			(u32)&ugeth->p_rx_glbl_pram->rbdqptr,
 850			in_be32(&ugeth->p_rx_glbl_pram->rbdqptr));
 851		pr_info("mflr            : addr - 0x%08x, val - 0x%04x\n",
 852			(u32)&ugeth->p_rx_glbl_pram->mflr,
 853			in_be16(&ugeth->p_rx_glbl_pram->mflr));
 854		pr_info("minflr          : addr - 0x%08x, val - 0x%04x\n",
 855			(u32)&ugeth->p_rx_glbl_pram->minflr,
 856			in_be16(&ugeth->p_rx_glbl_pram->minflr));
 857		pr_info("maxd1           : addr - 0x%08x, val - 0x%04x\n",
 858			(u32)&ugeth->p_rx_glbl_pram->maxd1,
 859			in_be16(&ugeth->p_rx_glbl_pram->maxd1));
 860		pr_info("maxd2           : addr - 0x%08x, val - 0x%04x\n",
 861			(u32)&ugeth->p_rx_glbl_pram->maxd2,
 862			in_be16(&ugeth->p_rx_glbl_pram->maxd2));
 863		pr_info("ecamptr         : addr - 0x%08x, val - 0x%08x\n",
 864			(u32)&ugeth->p_rx_glbl_pram->ecamptr,
 865			in_be32(&ugeth->p_rx_glbl_pram->ecamptr));
 866		pr_info("l2qt            : addr - 0x%08x, val - 0x%08x\n",
 867			(u32)&ugeth->p_rx_glbl_pram->l2qt,
 868			in_be32(&ugeth->p_rx_glbl_pram->l2qt));
 869		pr_info("l3qt[0]         : addr - 0x%08x, val - 0x%08x\n",
 870			(u32)&ugeth->p_rx_glbl_pram->l3qt[0],
 871			in_be32(&ugeth->p_rx_glbl_pram->l3qt[0]));
 872		pr_info("l3qt[1]         : addr - 0x%08x, val - 0x%08x\n",
 873			(u32)&ugeth->p_rx_glbl_pram->l3qt[1],
 874			in_be32(&ugeth->p_rx_glbl_pram->l3qt[1]));
 875		pr_info("l3qt[2]         : addr - 0x%08x, val - 0x%08x\n",
 876			(u32)&ugeth->p_rx_glbl_pram->l3qt[2],
 877			in_be32(&ugeth->p_rx_glbl_pram->l3qt[2]));
 878		pr_info("l3qt[3]         : addr - 0x%08x, val - 0x%08x\n",
 879			(u32)&ugeth->p_rx_glbl_pram->l3qt[3],
 880			in_be32(&ugeth->p_rx_glbl_pram->l3qt[3]));
 881		pr_info("l3qt[4]         : addr - 0x%08x, val - 0x%08x\n",
 882			(u32)&ugeth->p_rx_glbl_pram->l3qt[4],
 883			in_be32(&ugeth->p_rx_glbl_pram->l3qt[4]));
 884		pr_info("l3qt[5]         : addr - 0x%08x, val - 0x%08x\n",
 885			(u32)&ugeth->p_rx_glbl_pram->l3qt[5],
 886			in_be32(&ugeth->p_rx_glbl_pram->l3qt[5]));
 887		pr_info("l3qt[6]         : addr - 0x%08x, val - 0x%08x\n",
 888			(u32)&ugeth->p_rx_glbl_pram->l3qt[6],
 889			in_be32(&ugeth->p_rx_glbl_pram->l3qt[6]));
 890		pr_info("l3qt[7]         : addr - 0x%08x, val - 0x%08x\n",
 891			(u32)&ugeth->p_rx_glbl_pram->l3qt[7],
 892			in_be32(&ugeth->p_rx_glbl_pram->l3qt[7]));
 893		pr_info("vlantype        : addr - 0x%08x, val - 0x%04x\n",
 894			(u32)&ugeth->p_rx_glbl_pram->vlantype,
 895			in_be16(&ugeth->p_rx_glbl_pram->vlantype));
 896		pr_info("vlantci         : addr - 0x%08x, val - 0x%04x\n",
 897			(u32)&ugeth->p_rx_glbl_pram->vlantci,
 898			in_be16(&ugeth->p_rx_glbl_pram->vlantci));
 899		for (i = 0; i < 64; i++)
 900			pr_info("addressfiltering[%d]: addr - 0x%08x, val - 0x%02x\n",
 901				i,
 902				(u32)&ugeth->p_rx_glbl_pram->addressfiltering[i],
 903				ugeth->p_rx_glbl_pram->addressfiltering[i]);
 904		pr_info("exfGlobalParam  : addr - 0x%08x, val - 0x%08x\n",
 905			(u32)&ugeth->p_rx_glbl_pram->exfGlobalParam,
 906			in_be32(&ugeth->p_rx_glbl_pram->exfGlobalParam));
 907	}
 908	if (ugeth->p_send_q_mem_reg) {
 909		pr_info("Send Q memory registers:\n");
 910		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_send_q_mem_reg);
 911		for (i = 0; i < ugeth->ug_info->numQueuesTx; i++) {
 912			pr_info("SQQD[%d]:\n", i);
 913			pr_info("Base address: 0x%08x\n",
 914				(u32)&ugeth->p_send_q_mem_reg->sqqd[i]);
 915			mem_disp((u8 *) & ugeth->p_send_q_mem_reg->sqqd[i],
 916				 sizeof(struct ucc_geth_send_queue_qd));
 917		}
 918	}
 919	if (ugeth->p_scheduler) {
 920		pr_info("Scheduler:\n");
 921		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_scheduler);
 922		mem_disp((u8 *) ugeth->p_scheduler,
 923			 sizeof(*ugeth->p_scheduler));
 924	}
 925	if (ugeth->p_tx_fw_statistics_pram) {
 926		pr_info("TX FW statistics pram:\n");
 927		pr_info("Base address: 0x%08x\n",
 928			(u32)ugeth->p_tx_fw_statistics_pram);
 929		mem_disp((u8 *) ugeth->p_tx_fw_statistics_pram,
 930			 sizeof(*ugeth->p_tx_fw_statistics_pram));
 931	}
 932	if (ugeth->p_rx_fw_statistics_pram) {
 933		pr_info("RX FW statistics pram:\n");
 934		pr_info("Base address: 0x%08x\n",
 935			(u32)ugeth->p_rx_fw_statistics_pram);
 936		mem_disp((u8 *) ugeth->p_rx_fw_statistics_pram,
 937			 sizeof(*ugeth->p_rx_fw_statistics_pram));
 938	}
 939	if (ugeth->p_rx_irq_coalescing_tbl) {
 940		pr_info("RX IRQ coalescing tables:\n");
 941		pr_info("Base address: 0x%08x\n",
 942			(u32)ugeth->p_rx_irq_coalescing_tbl);
 943		for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
 944			pr_info("RX IRQ coalescing table entry[%d]:\n", i);
 945			pr_info("Base address: 0x%08x\n",
 946				(u32)&ugeth->p_rx_irq_coalescing_tbl->
 947				coalescingentry[i]);
 948			pr_info("interruptcoalescingmaxvalue: addr - 0x%08x, val - 0x%08x\n",
 949				(u32)&ugeth->p_rx_irq_coalescing_tbl->
 950				coalescingentry[i].interruptcoalescingmaxvalue,
 951				in_be32(&ugeth->p_rx_irq_coalescing_tbl->
 952					coalescingentry[i].
 953					interruptcoalescingmaxvalue));
 954			pr_info("interruptcoalescingcounter : addr - 0x%08x, val - 0x%08x\n",
 955				(u32)&ugeth->p_rx_irq_coalescing_tbl->
 956				coalescingentry[i].interruptcoalescingcounter,
 957				in_be32(&ugeth->p_rx_irq_coalescing_tbl->
 958					coalescingentry[i].
 959					interruptcoalescingcounter));
 960		}
 961	}
 962	if (ugeth->p_rx_bd_qs_tbl) {
 963		pr_info("RX BD QS tables:\n");
 964		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_rx_bd_qs_tbl);
 965		for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
 966			pr_info("RX BD QS table[%d]:\n", i);
 967			pr_info("Base address: 0x%08x\n",
 968				(u32)&ugeth->p_rx_bd_qs_tbl[i]);
 969			pr_info("bdbaseptr        : addr - 0x%08x, val - 0x%08x\n",
 970				(u32)&ugeth->p_rx_bd_qs_tbl[i].bdbaseptr,
 971				in_be32(&ugeth->p_rx_bd_qs_tbl[i].bdbaseptr));
 972			pr_info("bdptr            : addr - 0x%08x, val - 0x%08x\n",
 973				(u32)&ugeth->p_rx_bd_qs_tbl[i].bdptr,
 974				in_be32(&ugeth->p_rx_bd_qs_tbl[i].bdptr));
 975			pr_info("externalbdbaseptr: addr - 0x%08x, val - 0x%08x\n",
 976				(u32)&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
 977				in_be32(&ugeth->p_rx_bd_qs_tbl[i].
 978					externalbdbaseptr));
 979			pr_info("externalbdptr    : addr - 0x%08x, val - 0x%08x\n",
 980				(u32)&ugeth->p_rx_bd_qs_tbl[i].externalbdptr,
 981				in_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdptr));
 982			pr_info("ucode RX Prefetched BDs:\n");
 983			pr_info("Base address: 0x%08x\n",
 984				(u32)qe_muram_addr(in_be32
 985						   (&ugeth->p_rx_bd_qs_tbl[i].
 986						    bdbaseptr)));
 987			mem_disp((u8 *)
 988				 qe_muram_addr(in_be32
 989					       (&ugeth->p_rx_bd_qs_tbl[i].
 990						bdbaseptr)),
 991				 sizeof(struct ucc_geth_rx_prefetched_bds));
 992		}
 993	}
 994	if (ugeth->p_init_enet_param_shadow) {
 995		int size;
 996		pr_info("Init enet param shadow:\n");
 997		pr_info("Base address: 0x%08x\n",
 998			(u32) ugeth->p_init_enet_param_shadow);
 999		mem_disp((u8 *) ugeth->p_init_enet_param_shadow,
1000			 sizeof(*ugeth->p_init_enet_param_shadow));
1001
1002		size = sizeof(struct ucc_geth_thread_rx_pram);
1003		if (ugeth->ug_info->rxExtendedFiltering) {
1004			size +=
1005			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING;
1006			if (ugeth->ug_info->largestexternallookupkeysize ==
1007			    QE_FLTR_TABLE_LOOKUP_KEY_SIZE_8_BYTES)
1008				size +=
1009			THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_8;
1010			if (ugeth->ug_info->largestexternallookupkeysize ==
1011			    QE_FLTR_TABLE_LOOKUP_KEY_SIZE_16_BYTES)
1012				size +=
1013			THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_16;
1014		}
1015
1016		dump_init_enet_entries(ugeth,
1017				       &(ugeth->p_init_enet_param_shadow->
1018					 txthread[0]),
1019				       ENET_INIT_PARAM_MAX_ENTRIES_TX,
1020				       sizeof(struct ucc_geth_thread_tx_pram),
1021				       ugeth->ug_info->riscTx, 0);
1022		dump_init_enet_entries(ugeth,
1023				       &(ugeth->p_init_enet_param_shadow->
1024					 rxthread[0]),
1025				       ENET_INIT_PARAM_MAX_ENTRIES_RX, size,
1026				       ugeth->ug_info->riscRx, 1);
1027	}
1028}
1029#endif /* DEBUG */
1030
1031static void init_default_reg_vals(u32 __iomem *upsmr_register,
1032				  u32 __iomem *maccfg1_register,
1033				  u32 __iomem *maccfg2_register)
1034{
1035	out_be32(upsmr_register, UCC_GETH_UPSMR_INIT);
1036	out_be32(maccfg1_register, UCC_GETH_MACCFG1_INIT);
1037	out_be32(maccfg2_register, UCC_GETH_MACCFG2_INIT);
1038}
1039
1040static int init_half_duplex_params(int alt_beb,
1041				   int back_pressure_no_backoff,
1042				   int no_backoff,
1043				   int excess_defer,
1044				   u8 alt_beb_truncation,
1045				   u8 max_retransmissions,
1046				   u8 collision_window,
1047				   u32 __iomem *hafdup_register)
1048{
1049	u32 value = 0;
1050
1051	if ((alt_beb_truncation > HALFDUP_ALT_BEB_TRUNCATION_MAX) ||
1052	    (max_retransmissions > HALFDUP_MAX_RETRANSMISSION_MAX) ||
1053	    (collision_window > HALFDUP_COLLISION_WINDOW_MAX))
1054		return -EINVAL;
1055
1056	value = (u32) (alt_beb_truncation << HALFDUP_ALT_BEB_TRUNCATION_SHIFT);
1057
1058	if (alt_beb)
1059		value |= HALFDUP_ALT_BEB;
1060	if (back_pressure_no_backoff)
1061		value |= HALFDUP_BACK_PRESSURE_NO_BACKOFF;
1062	if (no_backoff)
1063		value |= HALFDUP_NO_BACKOFF;
1064	if (excess_defer)
1065		value |= HALFDUP_EXCESSIVE_DEFER;
1066
1067	value |= (max_retransmissions << HALFDUP_MAX_RETRANSMISSION_SHIFT);
1068
1069	value |= collision_window;
1070
1071	out_be32(hafdup_register, value);
1072	return 0;
1073}
1074
1075static int init_inter_frame_gap_params(u8 non_btb_cs_ipg,
1076				       u8 non_btb_ipg,
1077				       u8 min_ifg,
1078				       u8 btb_ipg,
1079				       u32 __iomem *ipgifg_register)
1080{
1081	u32 value = 0;
1082
1083	/* Non-Back-to-back IPG part 1 should be <= Non-Back-to-back
1084	IPG part 2 */
1085	if (non_btb_cs_ipg > non_btb_ipg)
1086		return -EINVAL;
1087
1088	if ((non_btb_cs_ipg > IPGIFG_NON_BACK_TO_BACK_IFG_PART1_MAX) ||
1089	    (non_btb_ipg > IPGIFG_NON_BACK_TO_BACK_IFG_PART2_MAX) ||
1090	    /*(min_ifg        > IPGIFG_MINIMUM_IFG_ENFORCEMENT_MAX) || */
1091	    (btb_ipg > IPGIFG_BACK_TO_BACK_IFG_MAX))
1092		return -EINVAL;
1093
1094	value |=
1095	    ((non_btb_cs_ipg << IPGIFG_NON_BACK_TO_BACK_IFG_PART1_SHIFT) &
1096	     IPGIFG_NBTB_CS_IPG_MASK);
1097	value |=
1098	    ((non_btb_ipg << IPGIFG_NON_BACK_TO_BACK_IFG_PART2_SHIFT) &
1099	     IPGIFG_NBTB_IPG_MASK);
1100	value |=
1101	    ((min_ifg << IPGIFG_MINIMUM_IFG_ENFORCEMENT_SHIFT) &
1102	     IPGIFG_MIN_IFG_MASK);
1103	value |= (btb_ipg & IPGIFG_BTB_IPG_MASK);
1104
1105	out_be32(ipgifg_register, value);
1106	return 0;
1107}
1108
1109int init_flow_control_params(u32 automatic_flow_control_mode,
1110				    int rx_flow_control_enable,
1111				    int tx_flow_control_enable,
1112				    u16 pause_period,
1113				    u16 extension_field,
1114				    u32 __iomem *upsmr_register,
1115				    u32 __iomem *uempr_register,
1116				    u32 __iomem *maccfg1_register)
1117{
1118	u32 value = 0;
1119
1120	/* Set UEMPR register */
1121	value = (u32) pause_period << UEMPR_PAUSE_TIME_VALUE_SHIFT;
1122	value |= (u32) extension_field << UEMPR_EXTENDED_PAUSE_TIME_VALUE_SHIFT;
1123	out_be32(uempr_register, value);
1124
1125	/* Set UPSMR register */
1126	setbits32(upsmr_register, automatic_flow_control_mode);
1127
1128	value = in_be32(maccfg1_register);
1129	if (rx_flow_control_enable)
1130		value |= MACCFG1_FLOW_RX;
1131	if (tx_flow_control_enable)
1132		value |= MACCFG1_FLOW_TX;
1133	out_be32(maccfg1_register, value);
1134
1135	return 0;
1136}
1137
1138static int init_hw_statistics_gathering_mode(int enable_hardware_statistics,
1139					     int auto_zero_hardware_statistics,
1140					     u32 __iomem *upsmr_register,
1141					     u16 __iomem *uescr_register)
1142{
1143	u16 uescr_value = 0;
1144
1145	/* Enable hardware statistics gathering if requested */
1146	if (enable_hardware_statistics)
1147		setbits32(upsmr_register, UCC_GETH_UPSMR_HSE);
1148
1149	/* Clear hardware statistics counters */
1150	uescr_value = in_be16(uescr_register);
1151	uescr_value |= UESCR_CLRCNT;
1152	/* Automatically zero hardware statistics counters on read,
1153	if requested */
1154	if (auto_zero_hardware_statistics)
1155		uescr_value |= UESCR_AUTOZ;
1156	out_be16(uescr_register, uescr_value);
1157
1158	return 0;
1159}
1160
1161static int init_firmware_statistics_gathering_mode(int
1162		enable_tx_firmware_statistics,
1163		int enable_rx_firmware_statistics,
1164		u32 __iomem *tx_rmon_base_ptr,
1165		u32 tx_firmware_statistics_structure_address,
1166		u32 __iomem *rx_rmon_base_ptr,
1167		u32 rx_firmware_statistics_structure_address,
1168		u16 __iomem *temoder_register,
1169		u32 __iomem *remoder_register)
1170{
1171	/* Note: this function does not check if */
1172	/* the parameters it receives are NULL   */
1173
1174	if (enable_tx_firmware_statistics) {
1175		out_be32(tx_rmon_base_ptr,
1176			 tx_firmware_statistics_structure_address);
1177		setbits16(temoder_register, TEMODER_TX_RMON_STATISTICS_ENABLE);
1178	}
1179
1180	if (enable_rx_firmware_statistics) {
1181		out_be32(rx_rmon_base_ptr,
1182			 rx_firmware_statistics_structure_address);
1183		setbits32(remoder_register, REMODER_RX_RMON_STATISTICS_ENABLE);
1184	}
1185
1186	return 0;
1187}
1188
1189static int init_mac_station_addr_regs(u8 address_byte_0,
1190				      u8 address_byte_1,
1191				      u8 address_byte_2,
1192				      u8 address_byte_3,
1193				      u8 address_byte_4,
1194				      u8 address_byte_5,
1195				      u32 __iomem *macstnaddr1_register,
1196				      u32 __iomem *macstnaddr2_register)
1197{
1198	u32 value = 0;
1199
1200	/* Example: for a station address of 0x12345678ABCD, */
1201	/* 0x12 is byte 0, 0x34 is byte 1 and so on and 0xCD is byte 5 */
1202
1203	/* MACSTNADDR1 Register: */
1204
1205	/* 0                      7   8                      15  */
1206	/* station address byte 5     station address byte 4     */
1207	/* 16                     23  24                     31  */
1208	/* station address byte 3     station address byte 2     */
1209	value |= (u32) ((address_byte_2 << 0) & 0x000000FF);
1210	value |= (u32) ((address_byte_3 << 8) & 0x0000FF00);
1211	value |= (u32) ((address_byte_4 << 16) & 0x00FF0000);
1212	value |= (u32) ((address_byte_5 << 24) & 0xFF000000);
1213
1214	out_be32(macstnaddr1_register, value);
1215
1216	/* MACSTNADDR2 Register: */
1217
1218	/* 0                      7   8                      15  */
1219	/* station address byte 1     station address byte 0     */
1220	/* 16                     23  24                     31  */
1221	/*         reserved                   reserved           */
1222	value = 0;
1223	value |= (u32) ((address_byte_0 << 16) & 0x00FF0000);
1224	value |= (u32) ((address_byte_1 << 24) & 0xFF000000);
1225
1226	out_be32(macstnaddr2_register, value);
1227
1228	return 0;
1229}
1230
1231static int init_check_frame_length_mode(int length_check,
1232					u32 __iomem *maccfg2_register)
1233{
1234	u32 value = 0;
1235
1236	value = in_be32(maccfg2_register);
1237
1238	if (length_check)
1239		value |= MACCFG2_LC;
1240	else
1241		value &= ~MACCFG2_LC;
1242
1243	out_be32(maccfg2_register, value);
1244	return 0;
1245}
1246
1247static int init_preamble_length(u8 preamble_length,
1248				u32 __iomem *maccfg2_register)
1249{
1250	if ((preamble_length < 3) || (preamble_length > 7))
1251		return -EINVAL;
1252
1253	clrsetbits_be32(maccfg2_register, MACCFG2_PREL_MASK,
1254			preamble_length << MACCFG2_PREL_SHIFT);
1255
1256	return 0;
1257}
1258
1259static int init_rx_parameters(int reject_broadcast,
1260			      int receive_short_frames,
1261			      int promiscuous, u32 __iomem *upsmr_register)
1262{
1263	u32 value = 0;
1264
1265	value = in_be32(upsmr_register);
1266
1267	if (reject_broadcast)
1268		value |= UCC_GETH_UPSMR_BRO;
1269	else
1270		value &= ~UCC_GETH_UPSMR_BRO;
1271
1272	if (receive_short_frames)
1273		value |= UCC_GETH_UPSMR_RSH;
1274	else
1275		value &= ~UCC_GETH_UPSMR_RSH;
1276
1277	if (promiscuous)
1278		value |= UCC_GETH_UPSMR_PRO;
1279	else
1280		value &= ~UCC_GETH_UPSMR_PRO;
1281
1282	out_be32(upsmr_register, value);
1283
1284	return 0;
1285}
1286
1287static int init_max_rx_buff_len(u16 max_rx_buf_len,
1288				u16 __iomem *mrblr_register)
1289{
1290	/* max_rx_buf_len value must be a multiple of 128 */
1291	if ((max_rx_buf_len == 0) ||
1292	    (max_rx_buf_len % UCC_GETH_MRBLR_ALIGNMENT))
1293		return -EINVAL;
1294
1295	out_be16(mrblr_register, max_rx_buf_len);
1296	return 0;
1297}
1298
1299static int init_min_frame_len(u16 min_frame_length,
1300			      u16 __iomem *minflr_register,
1301			      u16 __iomem *mrblr_register)
1302{
1303	u16 mrblr_value = 0;
1304
1305	mrblr_value = in_be16(mrblr_register);
1306	if (min_frame_length >= (mrblr_value - 4))
1307		return -EINVAL;
1308
1309	out_be16(minflr_register, min_frame_length);
1310	return 0;
1311}
1312
1313static int adjust_enet_interface(struct ucc_geth_private *ugeth)
1314{
1315	struct ucc_geth_info *ug_info;
1316	struct ucc_geth __iomem *ug_regs;
1317	struct ucc_fast __iomem *uf_regs;
1318	int ret_val;
1319	u32 upsmr, maccfg2;
1320	u16 value;
1321
1322	ugeth_vdbg("%s: IN", __func__);
1323
1324	ug_info = ugeth->ug_info;
1325	ug_regs = ugeth->ug_regs;
1326	uf_regs = ugeth->uccf->uf_regs;
1327
1328	/*                    Set MACCFG2                    */
1329	maccfg2 = in_be32(&ug_regs->maccfg2);
1330	maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
1331	if ((ugeth->max_speed == SPEED_10) ||
1332	    (ugeth->max_speed == SPEED_100))
1333		maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
1334	else if (ugeth->max_speed == SPEED_1000)
1335		maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
1336	maccfg2 |= ug_info->padAndCrc;
1337	out_be32(&ug_regs->maccfg2, maccfg2);
1338
1339	/*                    Set UPSMR                      */
1340	upsmr = in_be32(&uf_regs->upsmr);
1341	upsmr &= ~(UCC_GETH_UPSMR_RPM | UCC_GETH_UPSMR_R10M |
1342		   UCC_GETH_UPSMR_TBIM | UCC_GETH_UPSMR_RMM);
1343	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_RMII) ||
1344	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII) ||
1345	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1346	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1347	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
1348	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1349		if (ugeth->phy_interface != PHY_INTERFACE_MODE_RMII)
1350			upsmr |= UCC_GETH_UPSMR_RPM;
1351		switch (ugeth->max_speed) {
1352		case SPEED_10:
1353			upsmr |= UCC_GETH_UPSMR_R10M;
1354			/* FALLTHROUGH */
1355		case SPEED_100:
1356			if (ugeth->phy_interface != PHY_INTERFACE_MODE_RTBI)
1357				upsmr |= UCC_GETH_UPSMR_RMM;
1358		}
1359	}
1360	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_TBI) ||
1361	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1362		upsmr |= UCC_GETH_UPSMR_TBIM;
1363	}
1364	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_SGMII))
1365		upsmr |= UCC_GETH_UPSMR_SGMM;
1366
1367	out_be32(&uf_regs->upsmr, upsmr);
1368
1369	/* Disable autonegotiation in tbi mode, because by default it
1370	comes up in autonegotiation mode. */
1371	/* Note that this depends on proper setting in utbipar register. */
1372	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_TBI) ||
1373	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1374		struct ucc_geth_info *ug_info = ugeth->ug_info;
1375		struct phy_device *tbiphy;
1376
1377		if (!ug_info->tbi_node)
1378			pr_warn("TBI mode requires that the device tree specify a tbi-handle\n");
1379
1380		tbiphy = of_phy_find_device(ug_info->tbi_node);
1381		if (!tbiphy)
1382			pr_warn("Could not get TBI device\n");
1383
1384		value = phy_read(tbiphy, ENET_TBI_MII_CR);
1385		value &= ~0x1000;	/* Turn off autonegotiation */
1386		phy_write(tbiphy, ENET_TBI_MII_CR, value);
 
 
1387	}
1388
1389	init_check_frame_length_mode(ug_info->lengthCheckRx, &ug_regs->maccfg2);
1390
1391	ret_val = init_preamble_length(ug_info->prel, &ug_regs->maccfg2);
1392	if (ret_val != 0) {
1393		if (netif_msg_probe(ugeth))
1394			pr_err("Preamble length must be between 3 and 7 inclusive\n");
1395		return ret_val;
1396	}
1397
1398	return 0;
1399}
1400
1401static int ugeth_graceful_stop_tx(struct ucc_geth_private *ugeth)
1402{
1403	struct ucc_fast_private *uccf;
1404	u32 cecr_subblock;
1405	u32 temp;
1406	int i = 10;
1407
1408	uccf = ugeth->uccf;
1409
1410	/* Mask GRACEFUL STOP TX interrupt bit and clear it */
1411	clrbits32(uccf->p_uccm, UCC_GETH_UCCE_GRA);
1412	out_be32(uccf->p_ucce, UCC_GETH_UCCE_GRA);  /* clear by writing 1 */
1413
1414	/* Issue host command */
1415	cecr_subblock =
1416	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
1417	qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
1418		     QE_CR_PROTOCOL_ETHERNET, 0);
1419
1420	/* Wait for command to complete */
1421	do {
1422		msleep(10);
1423		temp = in_be32(uccf->p_ucce);
1424	} while (!(temp & UCC_GETH_UCCE_GRA) && --i);
1425
1426	uccf->stopped_tx = 1;
1427
1428	return 0;
1429}
1430
1431static int ugeth_graceful_stop_rx(struct ucc_geth_private *ugeth)
1432{
1433	struct ucc_fast_private *uccf;
1434	u32 cecr_subblock;
1435	u8 temp;
1436	int i = 10;
1437
1438	uccf = ugeth->uccf;
1439
1440	/* Clear acknowledge bit */
1441	temp = in_8(&ugeth->p_rx_glbl_pram->rxgstpack);
1442	temp &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
1443	out_8(&ugeth->p_rx_glbl_pram->rxgstpack, temp);
1444
1445	/* Keep issuing command and checking acknowledge bit until
1446	it is asserted, according to spec */
1447	do {
1448		/* Issue host command */
1449		cecr_subblock =
1450		    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.
1451						ucc_num);
1452		qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
1453			     QE_CR_PROTOCOL_ETHERNET, 0);
1454		msleep(10);
1455		temp = in_8(&ugeth->p_rx_glbl_pram->rxgstpack);
1456	} while (!(temp & GRACEFUL_STOP_ACKNOWLEDGE_RX) && --i);
1457
1458	uccf->stopped_rx = 1;
1459
1460	return 0;
1461}
1462
1463static int ugeth_restart_tx(struct ucc_geth_private *ugeth)
1464{
1465	struct ucc_fast_private *uccf;
1466	u32 cecr_subblock;
1467
1468	uccf = ugeth->uccf;
1469
1470	cecr_subblock =
1471	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
1472	qe_issue_cmd(QE_RESTART_TX, cecr_subblock, QE_CR_PROTOCOL_ETHERNET, 0);
1473	uccf->stopped_tx = 0;
1474
1475	return 0;
1476}
1477
1478static int ugeth_restart_rx(struct ucc_geth_private *ugeth)
1479{
1480	struct ucc_fast_private *uccf;
1481	u32 cecr_subblock;
1482
1483	uccf = ugeth->uccf;
1484
1485	cecr_subblock =
1486	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
1487	qe_issue_cmd(QE_RESTART_RX, cecr_subblock, QE_CR_PROTOCOL_ETHERNET,
1488		     0);
1489	uccf->stopped_rx = 0;
1490
1491	return 0;
1492}
1493
1494static int ugeth_enable(struct ucc_geth_private *ugeth, enum comm_dir mode)
1495{
1496	struct ucc_fast_private *uccf;
1497	int enabled_tx, enabled_rx;
1498
1499	uccf = ugeth->uccf;
1500
1501	/* check if the UCC number is in range. */
1502	if (ugeth->ug_info->uf_info.ucc_num >= UCC_MAX_NUM) {
1503		if (netif_msg_probe(ugeth))
1504			pr_err("ucc_num out of range\n");
1505		return -EINVAL;
1506	}
1507
1508	enabled_tx = uccf->enabled_tx;
1509	enabled_rx = uccf->enabled_rx;
1510
1511	/* Get Tx and Rx going again, in case this channel was actively
1512	disabled. */
1513	if ((mode & COMM_DIR_TX) && (!enabled_tx) && uccf->stopped_tx)
1514		ugeth_restart_tx(ugeth);
1515	if ((mode & COMM_DIR_RX) && (!enabled_rx) && uccf->stopped_rx)
1516		ugeth_restart_rx(ugeth);
1517
1518	ucc_fast_enable(uccf, mode);	/* OK to do even if not disabled */
1519
1520	return 0;
1521
1522}
1523
1524static int ugeth_disable(struct ucc_geth_private *ugeth, enum comm_dir mode)
1525{
1526	struct ucc_fast_private *uccf;
1527
1528	uccf = ugeth->uccf;
1529
1530	/* check if the UCC number is in range. */
1531	if (ugeth->ug_info->uf_info.ucc_num >= UCC_MAX_NUM) {
1532		if (netif_msg_probe(ugeth))
1533			pr_err("ucc_num out of range\n");
1534		return -EINVAL;
1535	}
1536
1537	/* Stop any transmissions */
1538	if ((mode & COMM_DIR_TX) && uccf->enabled_tx && !uccf->stopped_tx)
1539		ugeth_graceful_stop_tx(ugeth);
1540
1541	/* Stop any receptions */
1542	if ((mode & COMM_DIR_RX) && uccf->enabled_rx && !uccf->stopped_rx)
1543		ugeth_graceful_stop_rx(ugeth);
1544
1545	ucc_fast_disable(ugeth->uccf, mode); /* OK to do even if not enabled */
1546
1547	return 0;
1548}
1549
1550static void ugeth_quiesce(struct ucc_geth_private *ugeth)
1551{
1552	/* Prevent any further xmits, plus detach the device. */
1553	netif_device_detach(ugeth->ndev);
1554
1555	/* Wait for any current xmits to finish. */
1556	netif_tx_disable(ugeth->ndev);
1557
1558	/* Disable the interrupt to avoid NAPI rescheduling. */
1559	disable_irq(ugeth->ug_info->uf_info.irq);
1560
1561	/* Stop NAPI, and possibly wait for its completion. */
1562	napi_disable(&ugeth->napi);
1563}
1564
1565static void ugeth_activate(struct ucc_geth_private *ugeth)
1566{
1567	napi_enable(&ugeth->napi);
1568	enable_irq(ugeth->ug_info->uf_info.irq);
1569	netif_device_attach(ugeth->ndev);
1570}
1571
1572/* Called every time the controller might need to be made
1573 * aware of new link state.  The PHY code conveys this
1574 * information through variables in the ugeth structure, and this
1575 * function converts those variables into the appropriate
1576 * register values, and can bring down the device if needed.
1577 */
1578
1579static void adjust_link(struct net_device *dev)
1580{
1581	struct ucc_geth_private *ugeth = netdev_priv(dev);
1582	struct ucc_geth __iomem *ug_regs;
1583	struct ucc_fast __iomem *uf_regs;
1584	struct phy_device *phydev = ugeth->phydev;
1585	int new_state = 0;
1586
1587	ug_regs = ugeth->ug_regs;
1588	uf_regs = ugeth->uccf->uf_regs;
1589
1590	if (phydev->link) {
1591		u32 tempval = in_be32(&ug_regs->maccfg2);
1592		u32 upsmr = in_be32(&uf_regs->upsmr);
1593		/* Now we make sure that we can be in full duplex mode.
1594		 * If not, we operate in half-duplex mode. */
1595		if (phydev->duplex != ugeth->oldduplex) {
1596			new_state = 1;
1597			if (!(phydev->duplex))
1598				tempval &= ~(MACCFG2_FDX);
1599			else
1600				tempval |= MACCFG2_FDX;
1601			ugeth->oldduplex = phydev->duplex;
1602		}
1603
1604		if (phydev->speed != ugeth->oldspeed) {
1605			new_state = 1;
1606			switch (phydev->speed) {
1607			case SPEED_1000:
1608				tempval = ((tempval &
1609					    ~(MACCFG2_INTERFACE_MODE_MASK)) |
1610					    MACCFG2_INTERFACE_MODE_BYTE);
1611				break;
1612			case SPEED_100:
1613			case SPEED_10:
1614				tempval = ((tempval &
1615					    ~(MACCFG2_INTERFACE_MODE_MASK)) |
1616					    MACCFG2_INTERFACE_MODE_NIBBLE);
1617				/* if reduced mode, re-set UPSMR.R10M */
1618				if ((ugeth->phy_interface == PHY_INTERFACE_MODE_RMII) ||
1619				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII) ||
1620				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1621				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1622				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
1623				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1624					if (phydev->speed == SPEED_10)
1625						upsmr |= UCC_GETH_UPSMR_R10M;
1626					else
1627						upsmr &= ~UCC_GETH_UPSMR_R10M;
1628				}
1629				break;
1630			default:
1631				if (netif_msg_link(ugeth))
1632					pr_warn(
1633						"%s: Ack!  Speed (%d) is not 10/100/1000!",
1634						dev->name, phydev->speed);
1635				break;
1636			}
1637			ugeth->oldspeed = phydev->speed;
1638		}
1639
1640		if (!ugeth->oldlink) {
1641			new_state = 1;
1642			ugeth->oldlink = 1;
1643		}
1644
1645		if (new_state) {
1646			/*
1647			 * To change the MAC configuration we need to disable
1648			 * the controller. To do so, we have to either grab
1649			 * ugeth->lock, which is a bad idea since 'graceful
1650			 * stop' commands might take quite a while, or we can
1651			 * quiesce driver's activity.
1652			 */
1653			ugeth_quiesce(ugeth);
1654			ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);
1655
1656			out_be32(&ug_regs->maccfg2, tempval);
1657			out_be32(&uf_regs->upsmr, upsmr);
1658
1659			ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
1660			ugeth_activate(ugeth);
1661		}
1662	} else if (ugeth->oldlink) {
1663			new_state = 1;
1664			ugeth->oldlink = 0;
1665			ugeth->oldspeed = 0;
1666			ugeth->oldduplex = -1;
1667	}
1668
1669	if (new_state && netif_msg_link(ugeth))
1670		phy_print_status(phydev);
1671}
1672
1673/* Initialize TBI PHY interface for communicating with the
1674 * SERDES lynx PHY on the chip.  We communicate with this PHY
1675 * through the MDIO bus on each controller, treating it as a
1676 * "normal" PHY at the address found in the UTBIPA register.  We assume
1677 * that the UTBIPA register is valid.  Either the MDIO bus code will set
1678 * it to a value that doesn't conflict with other PHYs on the bus, or the
1679 * value doesn't matter, as there are no other PHYs on the bus.
1680 */
1681static void uec_configure_serdes(struct net_device *dev)
1682{
1683	struct ucc_geth_private *ugeth = netdev_priv(dev);
1684	struct ucc_geth_info *ug_info = ugeth->ug_info;
1685	struct phy_device *tbiphy;
1686
1687	if (!ug_info->tbi_node) {
1688		dev_warn(&dev->dev, "SGMII mode requires that the device "
1689			"tree specify a tbi-handle\n");
1690		return;
1691	}
1692
1693	tbiphy = of_phy_find_device(ug_info->tbi_node);
1694	if (!tbiphy) {
1695		dev_err(&dev->dev, "error: Could not get TBI device\n");
1696		return;
1697	}
1698
1699	/*
1700	 * If the link is already up, we must already be ok, and don't need to
1701	 * configure and reset the TBI<->SerDes link.  Maybe U-Boot configured
1702	 * everything for us?  Resetting it takes the link down and requires
1703	 * several seconds for it to come back.
1704	 */
1705	if (phy_read(tbiphy, ENET_TBI_MII_SR) & TBISR_LSTATUS)
 
1706		return;
 
1707
1708	/* Single clk mode, mii mode off(for serdes communication) */
1709	phy_write(tbiphy, ENET_TBI_MII_ANA, TBIANA_SETTINGS);
1710
1711	phy_write(tbiphy, ENET_TBI_MII_TBICON, TBICON_CLK_SELECT);
1712
1713	phy_write(tbiphy, ENET_TBI_MII_CR, TBICR_SETTINGS);
 
 
1714}
1715
1716/* Configure the PHY for dev.
1717 * returns 0 if success.  -1 if failure
1718 */
1719static int init_phy(struct net_device *dev)
1720{
1721	struct ucc_geth_private *priv = netdev_priv(dev);
1722	struct ucc_geth_info *ug_info = priv->ug_info;
1723	struct phy_device *phydev;
1724
1725	priv->oldlink = 0;
1726	priv->oldspeed = 0;
1727	priv->oldduplex = -1;
1728
1729	phydev = of_phy_connect(dev, ug_info->phy_node, &adjust_link, 0,
1730				priv->phy_interface);
1731	if (!phydev)
1732		phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1733						   priv->phy_interface);
1734	if (!phydev) {
1735		dev_err(&dev->dev, "Could not attach to PHY\n");
1736		return -ENODEV;
1737	}
1738
1739	if (priv->phy_interface == PHY_INTERFACE_MODE_SGMII)
1740		uec_configure_serdes(dev);
1741
1742	phydev->supported &= (SUPPORTED_MII |
1743			      SUPPORTED_Autoneg |
1744			      ADVERTISED_10baseT_Half |
1745			      ADVERTISED_10baseT_Full |
1746			      ADVERTISED_100baseT_Half |
1747			      ADVERTISED_100baseT_Full);
1748
1749	if (priv->max_speed == SPEED_1000)
1750		phydev->supported |= ADVERTISED_1000baseT_Full;
1751
1752	phydev->advertising = phydev->supported;
1753
1754	priv->phydev = phydev;
1755
1756	return 0;
1757}
1758
1759static void ugeth_dump_regs(struct ucc_geth_private *ugeth)
1760{
1761#ifdef DEBUG
1762	ucc_fast_dump_regs(ugeth->uccf);
1763	dump_regs(ugeth);
1764	dump_bds(ugeth);
1765#endif
1766}
1767
1768static int ugeth_82xx_filtering_clear_all_addr_in_hash(struct ucc_geth_private *
1769						       ugeth,
1770						       enum enet_addr_type
1771						       enet_addr_type)
1772{
1773	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
1774	struct ucc_fast_private *uccf;
1775	enum comm_dir comm_dir;
1776	struct list_head *p_lh;
1777	u16 i, num;
1778	u32 __iomem *addr_h;
1779	u32 __iomem *addr_l;
1780	u8 *p_counter;
1781
1782	uccf = ugeth->uccf;
1783
1784	p_82xx_addr_filt =
1785	    (struct ucc_geth_82xx_address_filtering_pram __iomem *)
1786	    ugeth->p_rx_glbl_pram->addressfiltering;
1787
1788	if (enet_addr_type == ENET_ADDR_TYPE_GROUP) {
1789		addr_h = &(p_82xx_addr_filt->gaddr_h);
1790		addr_l = &(p_82xx_addr_filt->gaddr_l);
1791		p_lh = &ugeth->group_hash_q;
1792		p_counter = &(ugeth->numGroupAddrInHash);
1793	} else if (enet_addr_type == ENET_ADDR_TYPE_INDIVIDUAL) {
1794		addr_h = &(p_82xx_addr_filt->iaddr_h);
1795		addr_l = &(p_82xx_addr_filt->iaddr_l);
1796		p_lh = &ugeth->ind_hash_q;
1797		p_counter = &(ugeth->numIndAddrInHash);
1798	} else
1799		return -EINVAL;
1800
1801	comm_dir = 0;
1802	if (uccf->enabled_tx)
1803		comm_dir |= COMM_DIR_TX;
1804	if (uccf->enabled_rx)
1805		comm_dir |= COMM_DIR_RX;
1806	if (comm_dir)
1807		ugeth_disable(ugeth, comm_dir);
1808
1809	/* Clear the hash table. */
1810	out_be32(addr_h, 0x00000000);
1811	out_be32(addr_l, 0x00000000);
1812
1813	if (!p_lh)
1814		return 0;
1815
1816	num = *p_counter;
1817
1818	/* Delete all remaining CQ elements */
1819	for (i = 0; i < num; i++)
1820		put_enet_addr_container(ENET_ADDR_CONT_ENTRY(dequeue(p_lh)));
1821
1822	*p_counter = 0;
1823
1824	if (comm_dir)
1825		ugeth_enable(ugeth, comm_dir);
1826
1827	return 0;
1828}
1829
1830static int ugeth_82xx_filtering_clear_addr_in_paddr(struct ucc_geth_private *ugeth,
1831						    u8 paddr_num)
1832{
1833	ugeth->indAddrRegUsed[paddr_num] = 0; /* mark this paddr as not used */
1834	return hw_clear_addr_in_paddr(ugeth, paddr_num);/* clear in hardware */
1835}
1836
1837static void ucc_geth_free_rx(struct ucc_geth_private *ugeth)
1838{
1839	struct ucc_geth_info *ug_info;
1840	struct ucc_fast_info *uf_info;
1841	u16 i, j;
1842	u8 __iomem *bd;
1843
1844
1845	ug_info = ugeth->ug_info;
1846	uf_info = &ug_info->uf_info;
1847
1848	for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
1849		if (ugeth->p_rx_bd_ring[i]) {
1850			/* Return existing data buffers in ring */
1851			bd = ugeth->p_rx_bd_ring[i];
1852			for (j = 0; j < ugeth->ug_info->bdRingLenRx[i]; j++) {
1853				if (ugeth->rx_skbuff[i][j]) {
1854					dma_unmap_single(ugeth->dev,
1855						in_be32(&((struct qe_bd __iomem *)bd)->buf),
1856						ugeth->ug_info->
1857						uf_info.max_rx_buf_length +
1858						UCC_GETH_RX_DATA_BUF_ALIGNMENT,
1859						DMA_FROM_DEVICE);
1860					dev_kfree_skb_any(
1861						ugeth->rx_skbuff[i][j]);
1862					ugeth->rx_skbuff[i][j] = NULL;
1863				}
1864				bd += sizeof(struct qe_bd);
1865			}
1866
1867			kfree(ugeth->rx_skbuff[i]);
1868
1869			if (ugeth->ug_info->uf_info.bd_mem_part ==
1870			    MEM_PART_SYSTEM)
1871				kfree((void *)ugeth->rx_bd_ring_offset[i]);
1872			else if (ugeth->ug_info->uf_info.bd_mem_part ==
1873				 MEM_PART_MURAM)
1874				qe_muram_free(ugeth->rx_bd_ring_offset[i]);
1875			ugeth->p_rx_bd_ring[i] = NULL;
1876		}
1877	}
1878
1879}
1880
1881static void ucc_geth_free_tx(struct ucc_geth_private *ugeth)
1882{
1883	struct ucc_geth_info *ug_info;
1884	struct ucc_fast_info *uf_info;
1885	u16 i, j;
1886	u8 __iomem *bd;
1887
 
 
1888	ug_info = ugeth->ug_info;
1889	uf_info = &ug_info->uf_info;
1890
1891	for (i = 0; i < ugeth->ug_info->numQueuesTx; i++) {
1892		bd = ugeth->p_tx_bd_ring[i];
1893		if (!bd)
1894			continue;
1895		for (j = 0; j < ugeth->ug_info->bdRingLenTx[i]; j++) {
1896			if (ugeth->tx_skbuff[i][j]) {
1897				dma_unmap_single(ugeth->dev,
1898						 in_be32(&((struct qe_bd __iomem *)bd)->buf),
1899						 (in_be32((u32 __iomem *)bd) &
1900						  BD_LENGTH_MASK),
1901						 DMA_TO_DEVICE);
1902				dev_kfree_skb_any(ugeth->tx_skbuff[i][j]);
1903				ugeth->tx_skbuff[i][j] = NULL;
1904			}
1905		}
1906
1907		kfree(ugeth->tx_skbuff[i]);
1908
1909		if (ugeth->p_tx_bd_ring[i]) {
1910			if (ugeth->ug_info->uf_info.bd_mem_part ==
1911			    MEM_PART_SYSTEM)
1912				kfree((void *)ugeth->tx_bd_ring_offset[i]);
1913			else if (ugeth->ug_info->uf_info.bd_mem_part ==
1914				 MEM_PART_MURAM)
1915				qe_muram_free(ugeth->tx_bd_ring_offset[i]);
1916			ugeth->p_tx_bd_ring[i] = NULL;
1917		}
1918	}
1919
1920}
1921
1922static void ucc_geth_memclean(struct ucc_geth_private *ugeth)
1923{
1924	if (!ugeth)
1925		return;
1926
1927	if (ugeth->uccf) {
1928		ucc_fast_free(ugeth->uccf);
1929		ugeth->uccf = NULL;
1930	}
1931
1932	if (ugeth->p_thread_data_tx) {
1933		qe_muram_free(ugeth->thread_dat_tx_offset);
1934		ugeth->p_thread_data_tx = NULL;
1935	}
1936	if (ugeth->p_thread_data_rx) {
1937		qe_muram_free(ugeth->thread_dat_rx_offset);
1938		ugeth->p_thread_data_rx = NULL;
1939	}
1940	if (ugeth->p_exf_glbl_param) {
1941		qe_muram_free(ugeth->exf_glbl_param_offset);
1942		ugeth->p_exf_glbl_param = NULL;
1943	}
1944	if (ugeth->p_rx_glbl_pram) {
1945		qe_muram_free(ugeth->rx_glbl_pram_offset);
1946		ugeth->p_rx_glbl_pram = NULL;
1947	}
1948	if (ugeth->p_tx_glbl_pram) {
1949		qe_muram_free(ugeth->tx_glbl_pram_offset);
1950		ugeth->p_tx_glbl_pram = NULL;
1951	}
1952	if (ugeth->p_send_q_mem_reg) {
1953		qe_muram_free(ugeth->send_q_mem_reg_offset);
1954		ugeth->p_send_q_mem_reg = NULL;
1955	}
1956	if (ugeth->p_scheduler) {
1957		qe_muram_free(ugeth->scheduler_offset);
1958		ugeth->p_scheduler = NULL;
1959	}
1960	if (ugeth->p_tx_fw_statistics_pram) {
1961		qe_muram_free(ugeth->tx_fw_statistics_pram_offset);
1962		ugeth->p_tx_fw_statistics_pram = NULL;
1963	}
1964	if (ugeth->p_rx_fw_statistics_pram) {
1965		qe_muram_free(ugeth->rx_fw_statistics_pram_offset);
1966		ugeth->p_rx_fw_statistics_pram = NULL;
1967	}
1968	if (ugeth->p_rx_irq_coalescing_tbl) {
1969		qe_muram_free(ugeth->rx_irq_coalescing_tbl_offset);
1970		ugeth->p_rx_irq_coalescing_tbl = NULL;
1971	}
1972	if (ugeth->p_rx_bd_qs_tbl) {
1973		qe_muram_free(ugeth->rx_bd_qs_tbl_offset);
1974		ugeth->p_rx_bd_qs_tbl = NULL;
1975	}
1976	if (ugeth->p_init_enet_param_shadow) {
1977		return_init_enet_entries(ugeth,
1978					 &(ugeth->p_init_enet_param_shadow->
1979					   rxthread[0]),
1980					 ENET_INIT_PARAM_MAX_ENTRIES_RX,
1981					 ugeth->ug_info->riscRx, 1);
1982		return_init_enet_entries(ugeth,
1983					 &(ugeth->p_init_enet_param_shadow->
1984					   txthread[0]),
1985					 ENET_INIT_PARAM_MAX_ENTRIES_TX,
1986					 ugeth->ug_info->riscTx, 0);
1987		kfree(ugeth->p_init_enet_param_shadow);
1988		ugeth->p_init_enet_param_shadow = NULL;
1989	}
1990	ucc_geth_free_tx(ugeth);
1991	ucc_geth_free_rx(ugeth);
1992	while (!list_empty(&ugeth->group_hash_q))
1993		put_enet_addr_container(ENET_ADDR_CONT_ENTRY
1994					(dequeue(&ugeth->group_hash_q)));
1995	while (!list_empty(&ugeth->ind_hash_q))
1996		put_enet_addr_container(ENET_ADDR_CONT_ENTRY
1997					(dequeue(&ugeth->ind_hash_q)));
1998	if (ugeth->ug_regs) {
1999		iounmap(ugeth->ug_regs);
2000		ugeth->ug_regs = NULL;
2001	}
2002}
2003
2004static void ucc_geth_set_multi(struct net_device *dev)
2005{
2006	struct ucc_geth_private *ugeth;
2007	struct netdev_hw_addr *ha;
2008	struct ucc_fast __iomem *uf_regs;
2009	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
2010
2011	ugeth = netdev_priv(dev);
2012
2013	uf_regs = ugeth->uccf->uf_regs;
2014
2015	if (dev->flags & IFF_PROMISC) {
2016		setbits32(&uf_regs->upsmr, UCC_GETH_UPSMR_PRO);
2017	} else {
2018		clrbits32(&uf_regs->upsmr, UCC_GETH_UPSMR_PRO);
2019
2020		p_82xx_addr_filt =
2021		    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->
2022		    p_rx_glbl_pram->addressfiltering;
2023
2024		if (dev->flags & IFF_ALLMULTI) {
2025			/* Catch all multicast addresses, so set the
2026			 * filter to all 1's.
2027			 */
2028			out_be32(&p_82xx_addr_filt->gaddr_h, 0xffffffff);
2029			out_be32(&p_82xx_addr_filt->gaddr_l, 0xffffffff);
2030		} else {
2031			/* Clear filter and add the addresses in the list.
2032			 */
2033			out_be32(&p_82xx_addr_filt->gaddr_h, 0x0);
2034			out_be32(&p_82xx_addr_filt->gaddr_l, 0x0);
2035
2036			netdev_for_each_mc_addr(ha, dev) {
2037				/* Ask CPM to run CRC and set bit in
2038				 * filter mask.
2039				 */
2040				hw_add_addr_in_hash(ugeth, ha->addr);
2041			}
2042		}
2043	}
2044}
2045
2046static void ucc_geth_stop(struct ucc_geth_private *ugeth)
2047{
2048	struct ucc_geth __iomem *ug_regs = ugeth->ug_regs;
2049	struct phy_device *phydev = ugeth->phydev;
2050
2051	ugeth_vdbg("%s: IN", __func__);
2052
2053	/*
2054	 * Tell the kernel the link is down.
2055	 * Must be done before disabling the controller
2056	 * or deadlock may happen.
2057	 */
2058	phy_stop(phydev);
2059
2060	/* Disable the controller */
2061	ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);
2062
2063	/* Mask all interrupts */
2064	out_be32(ugeth->uccf->p_uccm, 0x00000000);
2065
2066	/* Clear all interrupts */
2067	out_be32(ugeth->uccf->p_ucce, 0xffffffff);
2068
2069	/* Disable Rx and Tx */
2070	clrbits32(&ug_regs->maccfg1, MACCFG1_ENABLE_RX | MACCFG1_ENABLE_TX);
2071
2072	ucc_geth_memclean(ugeth);
2073}
2074
2075static int ucc_struct_init(struct ucc_geth_private *ugeth)
2076{
2077	struct ucc_geth_info *ug_info;
2078	struct ucc_fast_info *uf_info;
2079	int i;
2080
2081	ug_info = ugeth->ug_info;
2082	uf_info = &ug_info->uf_info;
2083
2084	if (!((uf_info->bd_mem_part == MEM_PART_SYSTEM) ||
2085	      (uf_info->bd_mem_part == MEM_PART_MURAM))) {
2086		if (netif_msg_probe(ugeth))
2087			pr_err("Bad memory partition value\n");
2088		return -EINVAL;
2089	}
2090
2091	/* Rx BD lengths */
2092	for (i = 0; i < ug_info->numQueuesRx; i++) {
2093		if ((ug_info->bdRingLenRx[i] < UCC_GETH_RX_BD_RING_SIZE_MIN) ||
2094		    (ug_info->bdRingLenRx[i] %
2095		     UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT)) {
2096			if (netif_msg_probe(ugeth))
2097				pr_err("Rx BD ring length must be multiple of 4, no smaller than 8\n");
2098			return -EINVAL;
2099		}
2100	}
2101
2102	/* Tx BD lengths */
2103	for (i = 0; i < ug_info->numQueuesTx; i++) {
2104		if (ug_info->bdRingLenTx[i] < UCC_GETH_TX_BD_RING_SIZE_MIN) {
2105			if (netif_msg_probe(ugeth))
2106				pr_err("Tx BD ring length must be no smaller than 2\n");
2107			return -EINVAL;
2108		}
2109	}
2110
2111	/* mrblr */
2112	if ((uf_info->max_rx_buf_length == 0) ||
2113	    (uf_info->max_rx_buf_length % UCC_GETH_MRBLR_ALIGNMENT)) {
2114		if (netif_msg_probe(ugeth))
2115			pr_err("max_rx_buf_length must be non-zero multiple of 128\n");
2116		return -EINVAL;
2117	}
2118
2119	/* num Tx queues */
2120	if (ug_info->numQueuesTx > NUM_TX_QUEUES) {
2121		if (netif_msg_probe(ugeth))
2122			pr_err("number of tx queues too large\n");
2123		return -EINVAL;
2124	}
2125
2126	/* num Rx queues */
2127	if (ug_info->numQueuesRx > NUM_RX_QUEUES) {
2128		if (netif_msg_probe(ugeth))
2129			pr_err("number of rx queues too large\n");
2130		return -EINVAL;
2131	}
2132
2133	/* l2qt */
2134	for (i = 0; i < UCC_GETH_VLAN_PRIORITY_MAX; i++) {
2135		if (ug_info->l2qt[i] >= ug_info->numQueuesRx) {
2136			if (netif_msg_probe(ugeth))
2137				pr_err("VLAN priority table entry must not be larger than number of Rx queues\n");
2138			return -EINVAL;
2139		}
2140	}
2141
2142	/* l3qt */
2143	for (i = 0; i < UCC_GETH_IP_PRIORITY_MAX; i++) {
2144		if (ug_info->l3qt[i] >= ug_info->numQueuesRx) {
2145			if (netif_msg_probe(ugeth))
2146				pr_err("IP priority table entry must not be larger than number of Rx queues\n");
2147			return -EINVAL;
2148		}
2149	}
2150
2151	if (ug_info->cam && !ug_info->ecamptr) {
2152		if (netif_msg_probe(ugeth))
2153			pr_err("If cam mode is chosen, must supply cam ptr\n");
2154		return -EINVAL;
2155	}
2156
2157	if ((ug_info->numStationAddresses !=
2158	     UCC_GETH_NUM_OF_STATION_ADDRESSES_1) &&
2159	    ug_info->rxExtendedFiltering) {
2160		if (netif_msg_probe(ugeth))
2161			pr_err("Number of station addresses greater than 1 not allowed in extended parsing mode\n");
2162		return -EINVAL;
2163	}
2164
2165	/* Generate uccm_mask for receive */
2166	uf_info->uccm_mask = ug_info->eventRegMask & UCCE_OTHER;/* Errors */
2167	for (i = 0; i < ug_info->numQueuesRx; i++)
2168		uf_info->uccm_mask |= (UCC_GETH_UCCE_RXF0 << i);
2169
2170	for (i = 0; i < ug_info->numQueuesTx; i++)
2171		uf_info->uccm_mask |= (UCC_GETH_UCCE_TXB0 << i);
2172	/* Initialize the general fast UCC block. */
2173	if (ucc_fast_init(uf_info, &ugeth->uccf)) {
2174		if (netif_msg_probe(ugeth))
2175			pr_err("Failed to init uccf\n");
2176		return -ENOMEM;
2177	}
2178
2179	/* read the number of risc engines, update the riscTx and riscRx
2180	 * if there are 4 riscs in QE
2181	 */
2182	if (qe_get_num_of_risc() == 4) {
2183		ug_info->riscTx = QE_RISC_ALLOCATION_FOUR_RISCS;
2184		ug_info->riscRx = QE_RISC_ALLOCATION_FOUR_RISCS;
2185	}
2186
2187	ugeth->ug_regs = ioremap(uf_info->regs, sizeof(*ugeth->ug_regs));
2188	if (!ugeth->ug_regs) {
2189		if (netif_msg_probe(ugeth))
2190			pr_err("Failed to ioremap regs\n");
2191		return -ENOMEM;
2192	}
2193
2194	return 0;
2195}
2196
2197static int ucc_geth_alloc_tx(struct ucc_geth_private *ugeth)
2198{
2199	struct ucc_geth_info *ug_info;
2200	struct ucc_fast_info *uf_info;
2201	int length;
2202	u16 i, j;
2203	u8 __iomem *bd;
2204
2205	ug_info = ugeth->ug_info;
2206	uf_info = &ug_info->uf_info;
2207
2208	/* Allocate Tx bds */
2209	for (j = 0; j < ug_info->numQueuesTx; j++) {
2210		/* Allocate in multiple of
2211		   UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT,
2212		   according to spec */
2213		length = ((ug_info->bdRingLenTx[j] * sizeof(struct qe_bd))
2214			  / UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT)
2215		    * UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
2216		if ((ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)) %
2217		    UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT)
2218			length += UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
2219		if (uf_info->bd_mem_part == MEM_PART_SYSTEM) {
2220			u32 align = 4;
2221			if (UCC_GETH_TX_BD_RING_ALIGNMENT > 4)
2222				align = UCC_GETH_TX_BD_RING_ALIGNMENT;
2223			ugeth->tx_bd_ring_offset[j] =
2224				(u32) kmalloc((u32) (length + align), GFP_KERNEL);
2225
2226			if (ugeth->tx_bd_ring_offset[j] != 0)
2227				ugeth->p_tx_bd_ring[j] =
2228					(u8 __iomem *)((ugeth->tx_bd_ring_offset[j] +
2229					align) & ~(align - 1));
2230		} else if (uf_info->bd_mem_part == MEM_PART_MURAM) {
2231			ugeth->tx_bd_ring_offset[j] =
2232			    qe_muram_alloc(length,
2233					   UCC_GETH_TX_BD_RING_ALIGNMENT);
2234			if (!IS_ERR_VALUE(ugeth->tx_bd_ring_offset[j]))
2235				ugeth->p_tx_bd_ring[j] =
2236				    (u8 __iomem *) qe_muram_addr(ugeth->
2237							 tx_bd_ring_offset[j]);
2238		}
2239		if (!ugeth->p_tx_bd_ring[j]) {
2240			if (netif_msg_ifup(ugeth))
2241				pr_err("Can not allocate memory for Tx bd rings\n");
2242			return -ENOMEM;
2243		}
2244		/* Zero unused end of bd ring, according to spec */
2245		memset_io((void __iomem *)(ugeth->p_tx_bd_ring[j] +
2246		       ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)), 0,
2247		       length - ug_info->bdRingLenTx[j] * sizeof(struct qe_bd));
2248	}
2249
2250	/* Init Tx bds */
2251	for (j = 0; j < ug_info->numQueuesTx; j++) {
2252		/* Setup the skbuff rings */
2253		ugeth->tx_skbuff[j] = kmalloc(sizeof(struct sk_buff *) *
2254					      ugeth->ug_info->bdRingLenTx[j],
2255					      GFP_KERNEL);
2256
2257		if (ugeth->tx_skbuff[j] == NULL) {
2258			if (netif_msg_ifup(ugeth))
2259				pr_err("Could not allocate tx_skbuff\n");
2260			return -ENOMEM;
2261		}
2262
2263		for (i = 0; i < ugeth->ug_info->bdRingLenTx[j]; i++)
2264			ugeth->tx_skbuff[j][i] = NULL;
2265
2266		ugeth->skb_curtx[j] = ugeth->skb_dirtytx[j] = 0;
2267		bd = ugeth->confBd[j] = ugeth->txBd[j] = ugeth->p_tx_bd_ring[j];
2268		for (i = 0; i < ug_info->bdRingLenTx[j]; i++) {
2269			/* clear bd buffer */
2270			out_be32(&((struct qe_bd __iomem *)bd)->buf, 0);
2271			/* set bd status and length */
2272			out_be32((u32 __iomem *)bd, 0);
2273			bd += sizeof(struct qe_bd);
2274		}
2275		bd -= sizeof(struct qe_bd);
2276		/* set bd status and length */
2277		out_be32((u32 __iomem *)bd, T_W); /* for last BD set Wrap bit */
2278	}
2279
2280	return 0;
2281}
2282
2283static int ucc_geth_alloc_rx(struct ucc_geth_private *ugeth)
2284{
2285	struct ucc_geth_info *ug_info;
2286	struct ucc_fast_info *uf_info;
2287	int length;
2288	u16 i, j;
2289	u8 __iomem *bd;
2290
2291	ug_info = ugeth->ug_info;
2292	uf_info = &ug_info->uf_info;
2293
2294	/* Allocate Rx bds */
2295	for (j = 0; j < ug_info->numQueuesRx; j++) {
2296		length = ug_info->bdRingLenRx[j] * sizeof(struct qe_bd);
2297		if (uf_info->bd_mem_part == MEM_PART_SYSTEM) {
2298			u32 align = 4;
2299			if (UCC_GETH_RX_BD_RING_ALIGNMENT > 4)
2300				align = UCC_GETH_RX_BD_RING_ALIGNMENT;
2301			ugeth->rx_bd_ring_offset[j] =
2302				(u32) kmalloc((u32) (length + align), GFP_KERNEL);
2303			if (ugeth->rx_bd_ring_offset[j] != 0)
2304				ugeth->p_rx_bd_ring[j] =
2305					(u8 __iomem *)((ugeth->rx_bd_ring_offset[j] +
2306					align) & ~(align - 1));
2307		} else if (uf_info->bd_mem_part == MEM_PART_MURAM) {
2308			ugeth->rx_bd_ring_offset[j] =
2309			    qe_muram_alloc(length,
2310					   UCC_GETH_RX_BD_RING_ALIGNMENT);
2311			if (!IS_ERR_VALUE(ugeth->rx_bd_ring_offset[j]))
2312				ugeth->p_rx_bd_ring[j] =
2313				    (u8 __iomem *) qe_muram_addr(ugeth->
2314							 rx_bd_ring_offset[j]);
2315		}
2316		if (!ugeth->p_rx_bd_ring[j]) {
2317			if (netif_msg_ifup(ugeth))
2318				pr_err("Can not allocate memory for Rx bd rings\n");
2319			return -ENOMEM;
2320		}
2321	}
2322
2323	/* Init Rx bds */
2324	for (j = 0; j < ug_info->numQueuesRx; j++) {
2325		/* Setup the skbuff rings */
2326		ugeth->rx_skbuff[j] = kmalloc(sizeof(struct sk_buff *) *
2327					      ugeth->ug_info->bdRingLenRx[j],
2328					      GFP_KERNEL);
2329
2330		if (ugeth->rx_skbuff[j] == NULL) {
2331			if (netif_msg_ifup(ugeth))
2332				pr_err("Could not allocate rx_skbuff\n");
2333			return -ENOMEM;
2334		}
2335
2336		for (i = 0; i < ugeth->ug_info->bdRingLenRx[j]; i++)
2337			ugeth->rx_skbuff[j][i] = NULL;
2338
2339		ugeth->skb_currx[j] = 0;
2340		bd = ugeth->rxBd[j] = ugeth->p_rx_bd_ring[j];
2341		for (i = 0; i < ug_info->bdRingLenRx[j]; i++) {
2342			/* set bd status and length */
2343			out_be32((u32 __iomem *)bd, R_I);
2344			/* clear bd buffer */
2345			out_be32(&((struct qe_bd __iomem *)bd)->buf, 0);
2346			bd += sizeof(struct qe_bd);
2347		}
2348		bd -= sizeof(struct qe_bd);
2349		/* set bd status and length */
2350		out_be32((u32 __iomem *)bd, R_W); /* for last BD set Wrap bit */
2351	}
2352
2353	return 0;
2354}
2355
2356static int ucc_geth_startup(struct ucc_geth_private *ugeth)
2357{
2358	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
2359	struct ucc_geth_init_pram __iomem *p_init_enet_pram;
2360	struct ucc_fast_private *uccf;
2361	struct ucc_geth_info *ug_info;
2362	struct ucc_fast_info *uf_info;
2363	struct ucc_fast __iomem *uf_regs;
2364	struct ucc_geth __iomem *ug_regs;
2365	int ret_val = -EINVAL;
2366	u32 remoder = UCC_GETH_REMODER_INIT;
2367	u32 init_enet_pram_offset, cecr_subblock, command;
2368	u32 ifstat, i, j, size, l2qt, l3qt;
2369	u16 temoder = UCC_GETH_TEMODER_INIT;
2370	u16 test;
2371	u8 function_code = 0;
2372	u8 __iomem *endOfRing;
2373	u8 numThreadsRxNumerical, numThreadsTxNumerical;
2374
2375	ugeth_vdbg("%s: IN", __func__);
2376	uccf = ugeth->uccf;
2377	ug_info = ugeth->ug_info;
2378	uf_info = &ug_info->uf_info;
2379	uf_regs = uccf->uf_regs;
2380	ug_regs = ugeth->ug_regs;
2381
2382	switch (ug_info->numThreadsRx) {
2383	case UCC_GETH_NUM_OF_THREADS_1:
2384		numThreadsRxNumerical = 1;
2385		break;
2386	case UCC_GETH_NUM_OF_THREADS_2:
2387		numThreadsRxNumerical = 2;
2388		break;
2389	case UCC_GETH_NUM_OF_THREADS_4:
2390		numThreadsRxNumerical = 4;
2391		break;
2392	case UCC_GETH_NUM_OF_THREADS_6:
2393		numThreadsRxNumerical = 6;
2394		break;
2395	case UCC_GETH_NUM_OF_THREADS_8:
2396		numThreadsRxNumerical = 8;
2397		break;
2398	default:
2399		if (netif_msg_ifup(ugeth))
2400			pr_err("Bad number of Rx threads value\n");
2401		return -EINVAL;
2402		break;
2403	}
2404
2405	switch (ug_info->numThreadsTx) {
2406	case UCC_GETH_NUM_OF_THREADS_1:
2407		numThreadsTxNumerical = 1;
2408		break;
2409	case UCC_GETH_NUM_OF_THREADS_2:
2410		numThreadsTxNumerical = 2;
2411		break;
2412	case UCC_GETH_NUM_OF_THREADS_4:
2413		numThreadsTxNumerical = 4;
2414		break;
2415	case UCC_GETH_NUM_OF_THREADS_6:
2416		numThreadsTxNumerical = 6;
2417		break;
2418	case UCC_GETH_NUM_OF_THREADS_8:
2419		numThreadsTxNumerical = 8;
2420		break;
2421	default:
2422		if (netif_msg_ifup(ugeth))
2423			pr_err("Bad number of Tx threads value\n");
2424		return -EINVAL;
2425		break;
2426	}
2427
2428	/* Calculate rx_extended_features */
2429	ugeth->rx_non_dynamic_extended_features = ug_info->ipCheckSumCheck ||
2430	    ug_info->ipAddressAlignment ||
2431	    (ug_info->numStationAddresses !=
2432	     UCC_GETH_NUM_OF_STATION_ADDRESSES_1);
2433
2434	ugeth->rx_extended_features = ugeth->rx_non_dynamic_extended_features ||
2435		(ug_info->vlanOperationTagged != UCC_GETH_VLAN_OPERATION_TAGGED_NOP) ||
2436		(ug_info->vlanOperationNonTagged !=
2437		 UCC_GETH_VLAN_OPERATION_NON_TAGGED_NOP);
2438
2439	init_default_reg_vals(&uf_regs->upsmr,
2440			      &ug_regs->maccfg1, &ug_regs->maccfg2);
2441
2442	/*                    Set UPSMR                      */
2443	/* For more details see the hardware spec.           */
2444	init_rx_parameters(ug_info->bro,
2445			   ug_info->rsh, ug_info->pro, &uf_regs->upsmr);
2446
2447	/* We're going to ignore other registers for now, */
2448	/* except as needed to get up and running         */
2449
2450	/*                    Set MACCFG1                    */
2451	/* For more details see the hardware spec.           */
2452	init_flow_control_params(ug_info->aufc,
2453				 ug_info->receiveFlowControl,
2454				 ug_info->transmitFlowControl,
2455				 ug_info->pausePeriod,
2456				 ug_info->extensionField,
2457				 &uf_regs->upsmr,
2458				 &ug_regs->uempr, &ug_regs->maccfg1);
2459
2460	setbits32(&ug_regs->maccfg1, MACCFG1_ENABLE_RX | MACCFG1_ENABLE_TX);
2461
2462	/*                    Set IPGIFG                     */
2463	/* For more details see the hardware spec.           */
2464	ret_val = init_inter_frame_gap_params(ug_info->nonBackToBackIfgPart1,
2465					      ug_info->nonBackToBackIfgPart2,
2466					      ug_info->
2467					      miminumInterFrameGapEnforcement,
2468					      ug_info->backToBackInterFrameGap,
2469					      &ug_regs->ipgifg);
2470	if (ret_val != 0) {
2471		if (netif_msg_ifup(ugeth))
2472			pr_err("IPGIFG initialization parameter too large\n");
2473		return ret_val;
2474	}
2475
2476	/*                    Set HAFDUP                     */
2477	/* For more details see the hardware spec.           */
2478	ret_val = init_half_duplex_params(ug_info->altBeb,
2479					  ug_info->backPressureNoBackoff,
2480					  ug_info->noBackoff,
2481					  ug_info->excessDefer,
2482					  ug_info->altBebTruncation,
2483					  ug_info->maxRetransmission,
2484					  ug_info->collisionWindow,
2485					  &ug_regs->hafdup);
2486	if (ret_val != 0) {
2487		if (netif_msg_ifup(ugeth))
2488			pr_err("Half Duplex initialization parameter too large\n");
2489		return ret_val;
2490	}
2491
2492	/*                    Set IFSTAT                     */
2493	/* For more details see the hardware spec.           */
2494	/* Read only - resets upon read                      */
2495	ifstat = in_be32(&ug_regs->ifstat);
2496
2497	/*                    Clear UEMPR                    */
2498	/* For more details see the hardware spec.           */
2499	out_be32(&ug_regs->uempr, 0);
2500
2501	/*                    Set UESCR                      */
2502	/* For more details see the hardware spec.           */
2503	init_hw_statistics_gathering_mode((ug_info->statisticsMode &
2504				UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE),
2505				0, &uf_regs->upsmr, &ug_regs->uescr);
2506
2507	ret_val = ucc_geth_alloc_tx(ugeth);
2508	if (ret_val != 0)
2509		return ret_val;
2510
2511	ret_val = ucc_geth_alloc_rx(ugeth);
2512	if (ret_val != 0)
2513		return ret_val;
2514
2515	/*
2516	 * Global PRAM
2517	 */
2518	/* Tx global PRAM */
2519	/* Allocate global tx parameter RAM page */
2520	ugeth->tx_glbl_pram_offset =
2521	    qe_muram_alloc(sizeof(struct ucc_geth_tx_global_pram),
2522			   UCC_GETH_TX_GLOBAL_PRAM_ALIGNMENT);
2523	if (IS_ERR_VALUE(ugeth->tx_glbl_pram_offset)) {
2524		if (netif_msg_ifup(ugeth))
2525			pr_err("Can not allocate DPRAM memory for p_tx_glbl_pram\n");
2526		return -ENOMEM;
2527	}
2528	ugeth->p_tx_glbl_pram =
2529	    (struct ucc_geth_tx_global_pram __iomem *) qe_muram_addr(ugeth->
2530							tx_glbl_pram_offset);
2531	/* Zero out p_tx_glbl_pram */
2532	memset_io((void __iomem *)ugeth->p_tx_glbl_pram, 0, sizeof(struct ucc_geth_tx_global_pram));
2533
2534	/* Fill global PRAM */
2535
2536	/* TQPTR */
2537	/* Size varies with number of Tx threads */
2538	ugeth->thread_dat_tx_offset =
2539	    qe_muram_alloc(numThreadsTxNumerical *
2540			   sizeof(struct ucc_geth_thread_data_tx) +
2541			   32 * (numThreadsTxNumerical == 1),
2542			   UCC_GETH_THREAD_DATA_ALIGNMENT);
2543	if (IS_ERR_VALUE(ugeth->thread_dat_tx_offset)) {
2544		if (netif_msg_ifup(ugeth))
2545			pr_err("Can not allocate DPRAM memory for p_thread_data_tx\n");
2546		return -ENOMEM;
2547	}
2548
2549	ugeth->p_thread_data_tx =
2550	    (struct ucc_geth_thread_data_tx __iomem *) qe_muram_addr(ugeth->
2551							thread_dat_tx_offset);
2552	out_be32(&ugeth->p_tx_glbl_pram->tqptr, ugeth->thread_dat_tx_offset);
2553
2554	/* vtagtable */
2555	for (i = 0; i < UCC_GETH_TX_VTAG_TABLE_ENTRY_MAX; i++)
2556		out_be32(&ugeth->p_tx_glbl_pram->vtagtable[i],
2557			 ug_info->vtagtable[i]);
2558
2559	/* iphoffset */
2560	for (i = 0; i < TX_IP_OFFSET_ENTRY_MAX; i++)
2561		out_8(&ugeth->p_tx_glbl_pram->iphoffset[i],
2562				ug_info->iphoffset[i]);
2563
2564	/* SQPTR */
2565	/* Size varies with number of Tx queues */
2566	ugeth->send_q_mem_reg_offset =
2567	    qe_muram_alloc(ug_info->numQueuesTx *
2568			   sizeof(struct ucc_geth_send_queue_qd),
2569			   UCC_GETH_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
2570	if (IS_ERR_VALUE(ugeth->send_q_mem_reg_offset)) {
2571		if (netif_msg_ifup(ugeth))
2572			pr_err("Can not allocate DPRAM memory for p_send_q_mem_reg\n");
2573		return -ENOMEM;
2574	}
2575
2576	ugeth->p_send_q_mem_reg =
2577	    (struct ucc_geth_send_queue_mem_region __iomem *) qe_muram_addr(ugeth->
2578			send_q_mem_reg_offset);
2579	out_be32(&ugeth->p_tx_glbl_pram->sqptr, ugeth->send_q_mem_reg_offset);
2580
2581	/* Setup the table */
2582	/* Assume BD rings are already established */
2583	for (i = 0; i < ug_info->numQueuesTx; i++) {
2584		endOfRing =
2585		    ugeth->p_tx_bd_ring[i] + (ug_info->bdRingLenTx[i] -
2586					      1) * sizeof(struct qe_bd);
2587		if (ugeth->ug_info->uf_info.bd_mem_part == MEM_PART_SYSTEM) {
2588			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].bd_ring_base,
2589				 (u32) virt_to_phys(ugeth->p_tx_bd_ring[i]));
2590			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].
2591				 last_bd_completed_address,
2592				 (u32) virt_to_phys(endOfRing));
2593		} else if (ugeth->ug_info->uf_info.bd_mem_part ==
2594			   MEM_PART_MURAM) {
2595			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].bd_ring_base,
2596				 (u32) immrbar_virt_to_phys(ugeth->
2597							    p_tx_bd_ring[i]));
2598			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].
2599				 last_bd_completed_address,
2600				 (u32) immrbar_virt_to_phys(endOfRing));
2601		}
2602	}
2603
2604	/* schedulerbasepointer */
2605
2606	if (ug_info->numQueuesTx > 1) {
2607	/* scheduler exists only if more than 1 tx queue */
2608		ugeth->scheduler_offset =
2609		    qe_muram_alloc(sizeof(struct ucc_geth_scheduler),
2610				   UCC_GETH_SCHEDULER_ALIGNMENT);
2611		if (IS_ERR_VALUE(ugeth->scheduler_offset)) {
2612			if (netif_msg_ifup(ugeth))
2613				pr_err("Can not allocate DPRAM memory for p_scheduler\n");
2614			return -ENOMEM;
2615		}
2616
2617		ugeth->p_scheduler =
2618		    (struct ucc_geth_scheduler __iomem *) qe_muram_addr(ugeth->
2619							   scheduler_offset);
2620		out_be32(&ugeth->p_tx_glbl_pram->schedulerbasepointer,
2621			 ugeth->scheduler_offset);
2622		/* Zero out p_scheduler */
2623		memset_io((void __iomem *)ugeth->p_scheduler, 0, sizeof(struct ucc_geth_scheduler));
2624
2625		/* Set values in scheduler */
2626		out_be32(&ugeth->p_scheduler->mblinterval,
2627			 ug_info->mblinterval);
2628		out_be16(&ugeth->p_scheduler->nortsrbytetime,
2629			 ug_info->nortsrbytetime);
2630		out_8(&ugeth->p_scheduler->fracsiz, ug_info->fracsiz);
2631		out_8(&ugeth->p_scheduler->strictpriorityq,
2632				ug_info->strictpriorityq);
2633		out_8(&ugeth->p_scheduler->txasap, ug_info->txasap);
2634		out_8(&ugeth->p_scheduler->extrabw, ug_info->extrabw);
2635		for (i = 0; i < NUM_TX_QUEUES; i++)
2636			out_8(&ugeth->p_scheduler->weightfactor[i],
2637			    ug_info->weightfactor[i]);
2638
2639		/* Set pointers to cpucount registers in scheduler */
2640		ugeth->p_cpucount[0] = &(ugeth->p_scheduler->cpucount0);
2641		ugeth->p_cpucount[1] = &(ugeth->p_scheduler->cpucount1);
2642		ugeth->p_cpucount[2] = &(ugeth->p_scheduler->cpucount2);
2643		ugeth->p_cpucount[3] = &(ugeth->p_scheduler->cpucount3);
2644		ugeth->p_cpucount[4] = &(ugeth->p_scheduler->cpucount4);
2645		ugeth->p_cpucount[5] = &(ugeth->p_scheduler->cpucount5);
2646		ugeth->p_cpucount[6] = &(ugeth->p_scheduler->cpucount6);
2647		ugeth->p_cpucount[7] = &(ugeth->p_scheduler->cpucount7);
2648	}
2649
2650	/* schedulerbasepointer */
2651	/* TxRMON_PTR (statistics) */
2652	if (ug_info->
2653	    statisticsMode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
2654		ugeth->tx_fw_statistics_pram_offset =
2655		    qe_muram_alloc(sizeof
2656				   (struct ucc_geth_tx_firmware_statistics_pram),
2657				   UCC_GETH_TX_STATISTICS_ALIGNMENT);
2658		if (IS_ERR_VALUE(ugeth->tx_fw_statistics_pram_offset)) {
2659			if (netif_msg_ifup(ugeth))
2660				pr_err("Can not allocate DPRAM memory for p_tx_fw_statistics_pram\n");
2661			return -ENOMEM;
2662		}
2663		ugeth->p_tx_fw_statistics_pram =
2664		    (struct ucc_geth_tx_firmware_statistics_pram __iomem *)
2665		    qe_muram_addr(ugeth->tx_fw_statistics_pram_offset);
2666		/* Zero out p_tx_fw_statistics_pram */
2667		memset_io((void __iomem *)ugeth->p_tx_fw_statistics_pram,
2668		       0, sizeof(struct ucc_geth_tx_firmware_statistics_pram));
2669	}
2670
2671	/* temoder */
2672	/* Already has speed set */
2673
2674	if (ug_info->numQueuesTx > 1)
2675		temoder |= TEMODER_SCHEDULER_ENABLE;
2676	if (ug_info->ipCheckSumGenerate)
2677		temoder |= TEMODER_IP_CHECKSUM_GENERATE;
2678	temoder |= ((ug_info->numQueuesTx - 1) << TEMODER_NUM_OF_QUEUES_SHIFT);
2679	out_be16(&ugeth->p_tx_glbl_pram->temoder, temoder);
2680
2681	test = in_be16(&ugeth->p_tx_glbl_pram->temoder);
2682
2683	/* Function code register value to be used later */
2684	function_code = UCC_BMR_BO_BE | UCC_BMR_GBL;
2685	/* Required for QE */
2686
2687	/* function code register */
2688	out_be32(&ugeth->p_tx_glbl_pram->tstate, ((u32) function_code) << 24);
2689
2690	/* Rx global PRAM */
2691	/* Allocate global rx parameter RAM page */
2692	ugeth->rx_glbl_pram_offset =
2693	    qe_muram_alloc(sizeof(struct ucc_geth_rx_global_pram),
2694			   UCC_GETH_RX_GLOBAL_PRAM_ALIGNMENT);
2695	if (IS_ERR_VALUE(ugeth->rx_glbl_pram_offset)) {
2696		if (netif_msg_ifup(ugeth))
2697			pr_err("Can not allocate DPRAM memory for p_rx_glbl_pram\n");
2698		return -ENOMEM;
2699	}
2700	ugeth->p_rx_glbl_pram =
2701	    (struct ucc_geth_rx_global_pram __iomem *) qe_muram_addr(ugeth->
2702							rx_glbl_pram_offset);
2703	/* Zero out p_rx_glbl_pram */
2704	memset_io((void __iomem *)ugeth->p_rx_glbl_pram, 0, sizeof(struct ucc_geth_rx_global_pram));
2705
2706	/* Fill global PRAM */
2707
2708	/* RQPTR */
2709	/* Size varies with number of Rx threads */
2710	ugeth->thread_dat_rx_offset =
2711	    qe_muram_alloc(numThreadsRxNumerical *
2712			   sizeof(struct ucc_geth_thread_data_rx),
2713			   UCC_GETH_THREAD_DATA_ALIGNMENT);
2714	if (IS_ERR_VALUE(ugeth->thread_dat_rx_offset)) {
2715		if (netif_msg_ifup(ugeth))
2716			pr_err("Can not allocate DPRAM memory for p_thread_data_rx\n");
2717		return -ENOMEM;
2718	}
2719
2720	ugeth->p_thread_data_rx =
2721	    (struct ucc_geth_thread_data_rx __iomem *) qe_muram_addr(ugeth->
2722							thread_dat_rx_offset);
2723	out_be32(&ugeth->p_rx_glbl_pram->rqptr, ugeth->thread_dat_rx_offset);
2724
2725	/* typeorlen */
2726	out_be16(&ugeth->p_rx_glbl_pram->typeorlen, ug_info->typeorlen);
2727
2728	/* rxrmonbaseptr (statistics) */
2729	if (ug_info->
2730	    statisticsMode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
2731		ugeth->rx_fw_statistics_pram_offset =
2732		    qe_muram_alloc(sizeof
2733				   (struct ucc_geth_rx_firmware_statistics_pram),
2734				   UCC_GETH_RX_STATISTICS_ALIGNMENT);
2735		if (IS_ERR_VALUE(ugeth->rx_fw_statistics_pram_offset)) {
2736			if (netif_msg_ifup(ugeth))
2737				pr_err("Can not allocate DPRAM memory for p_rx_fw_statistics_pram\n");
2738			return -ENOMEM;
2739		}
2740		ugeth->p_rx_fw_statistics_pram =
2741		    (struct ucc_geth_rx_firmware_statistics_pram __iomem *)
2742		    qe_muram_addr(ugeth->rx_fw_statistics_pram_offset);
2743		/* Zero out p_rx_fw_statistics_pram */
2744		memset_io((void __iomem *)ugeth->p_rx_fw_statistics_pram, 0,
2745		       sizeof(struct ucc_geth_rx_firmware_statistics_pram));
2746	}
2747
2748	/* intCoalescingPtr */
2749
2750	/* Size varies with number of Rx queues */
2751	ugeth->rx_irq_coalescing_tbl_offset =
2752	    qe_muram_alloc(ug_info->numQueuesRx *
2753			   sizeof(struct ucc_geth_rx_interrupt_coalescing_entry)
2754			   + 4, UCC_GETH_RX_INTERRUPT_COALESCING_ALIGNMENT);
2755	if (IS_ERR_VALUE(ugeth->rx_irq_coalescing_tbl_offset)) {
2756		if (netif_msg_ifup(ugeth))
2757			pr_err("Can not allocate DPRAM memory for p_rx_irq_coalescing_tbl\n");
2758		return -ENOMEM;
2759	}
2760
2761	ugeth->p_rx_irq_coalescing_tbl =
2762	    (struct ucc_geth_rx_interrupt_coalescing_table __iomem *)
2763	    qe_muram_addr(ugeth->rx_irq_coalescing_tbl_offset);
2764	out_be32(&ugeth->p_rx_glbl_pram->intcoalescingptr,
2765		 ugeth->rx_irq_coalescing_tbl_offset);
2766
2767	/* Fill interrupt coalescing table */
2768	for (i = 0; i < ug_info->numQueuesRx; i++) {
2769		out_be32(&ugeth->p_rx_irq_coalescing_tbl->coalescingentry[i].
2770			 interruptcoalescingmaxvalue,
2771			 ug_info->interruptcoalescingmaxvalue[i]);
2772		out_be32(&ugeth->p_rx_irq_coalescing_tbl->coalescingentry[i].
2773			 interruptcoalescingcounter,
2774			 ug_info->interruptcoalescingmaxvalue[i]);
2775	}
2776
2777	/* MRBLR */
2778	init_max_rx_buff_len(uf_info->max_rx_buf_length,
2779			     &ugeth->p_rx_glbl_pram->mrblr);
2780	/* MFLR */
2781	out_be16(&ugeth->p_rx_glbl_pram->mflr, ug_info->maxFrameLength);
2782	/* MINFLR */
2783	init_min_frame_len(ug_info->minFrameLength,
2784			   &ugeth->p_rx_glbl_pram->minflr,
2785			   &ugeth->p_rx_glbl_pram->mrblr);
2786	/* MAXD1 */
2787	out_be16(&ugeth->p_rx_glbl_pram->maxd1, ug_info->maxD1Length);
2788	/* MAXD2 */
2789	out_be16(&ugeth->p_rx_glbl_pram->maxd2, ug_info->maxD2Length);
2790
2791	/* l2qt */
2792	l2qt = 0;
2793	for (i = 0; i < UCC_GETH_VLAN_PRIORITY_MAX; i++)
2794		l2qt |= (ug_info->l2qt[i] << (28 - 4 * i));
2795	out_be32(&ugeth->p_rx_glbl_pram->l2qt, l2qt);
2796
2797	/* l3qt */
2798	for (j = 0; j < UCC_GETH_IP_PRIORITY_MAX; j += 8) {
2799		l3qt = 0;
2800		for (i = 0; i < 8; i++)
2801			l3qt |= (ug_info->l3qt[j + i] << (28 - 4 * i));
2802		out_be32(&ugeth->p_rx_glbl_pram->l3qt[j/8], l3qt);
2803	}
2804
2805	/* vlantype */
2806	out_be16(&ugeth->p_rx_glbl_pram->vlantype, ug_info->vlantype);
2807
2808	/* vlantci */
2809	out_be16(&ugeth->p_rx_glbl_pram->vlantci, ug_info->vlantci);
2810
2811	/* ecamptr */
2812	out_be32(&ugeth->p_rx_glbl_pram->ecamptr, ug_info->ecamptr);
2813
2814	/* RBDQPTR */
2815	/* Size varies with number of Rx queues */
2816	ugeth->rx_bd_qs_tbl_offset =
2817	    qe_muram_alloc(ug_info->numQueuesRx *
2818			   (sizeof(struct ucc_geth_rx_bd_queues_entry) +
2819			    sizeof(struct ucc_geth_rx_prefetched_bds)),
2820			   UCC_GETH_RX_BD_QUEUES_ALIGNMENT);
2821	if (IS_ERR_VALUE(ugeth->rx_bd_qs_tbl_offset)) {
2822		if (netif_msg_ifup(ugeth))
2823			pr_err("Can not allocate DPRAM memory for p_rx_bd_qs_tbl\n");
2824		return -ENOMEM;
2825	}
2826
2827	ugeth->p_rx_bd_qs_tbl =
2828	    (struct ucc_geth_rx_bd_queues_entry __iomem *) qe_muram_addr(ugeth->
2829				    rx_bd_qs_tbl_offset);
2830	out_be32(&ugeth->p_rx_glbl_pram->rbdqptr, ugeth->rx_bd_qs_tbl_offset);
2831	/* Zero out p_rx_bd_qs_tbl */
2832	memset_io((void __iomem *)ugeth->p_rx_bd_qs_tbl,
2833	       0,
2834	       ug_info->numQueuesRx * (sizeof(struct ucc_geth_rx_bd_queues_entry) +
2835				       sizeof(struct ucc_geth_rx_prefetched_bds)));
2836
2837	/* Setup the table */
2838	/* Assume BD rings are already established */
2839	for (i = 0; i < ug_info->numQueuesRx; i++) {
2840		if (ugeth->ug_info->uf_info.bd_mem_part == MEM_PART_SYSTEM) {
2841			out_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
2842				 (u32) virt_to_phys(ugeth->p_rx_bd_ring[i]));
2843		} else if (ugeth->ug_info->uf_info.bd_mem_part ==
2844			   MEM_PART_MURAM) {
2845			out_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
2846				 (u32) immrbar_virt_to_phys(ugeth->
2847							    p_rx_bd_ring[i]));
2848		}
2849		/* rest of fields handled by QE */
2850	}
2851
2852	/* remoder */
2853	/* Already has speed set */
2854
2855	if (ugeth->rx_extended_features)
2856		remoder |= REMODER_RX_EXTENDED_FEATURES;
2857	if (ug_info->rxExtendedFiltering)
2858		remoder |= REMODER_RX_EXTENDED_FILTERING;
2859	if (ug_info->dynamicMaxFrameLength)
2860		remoder |= REMODER_DYNAMIC_MAX_FRAME_LENGTH;
2861	if (ug_info->dynamicMinFrameLength)
2862		remoder |= REMODER_DYNAMIC_MIN_FRAME_LENGTH;
2863	remoder |=
2864	    ug_info->vlanOperationTagged << REMODER_VLAN_OPERATION_TAGGED_SHIFT;
2865	remoder |=
2866	    ug_info->
2867	    vlanOperationNonTagged << REMODER_VLAN_OPERATION_NON_TAGGED_SHIFT;
2868	remoder |= ug_info->rxQoSMode << REMODER_RX_QOS_MODE_SHIFT;
2869	remoder |= ((ug_info->numQueuesRx - 1) << REMODER_NUM_OF_QUEUES_SHIFT);
2870	if (ug_info->ipCheckSumCheck)
2871		remoder |= REMODER_IP_CHECKSUM_CHECK;
2872	if (ug_info->ipAddressAlignment)
2873		remoder |= REMODER_IP_ADDRESS_ALIGNMENT;
2874	out_be32(&ugeth->p_rx_glbl_pram->remoder, remoder);
2875
2876	/* Note that this function must be called */
2877	/* ONLY AFTER p_tx_fw_statistics_pram */
2878	/* andp_UccGethRxFirmwareStatisticsPram are allocated ! */
2879	init_firmware_statistics_gathering_mode((ug_info->
2880		statisticsMode &
2881		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX),
2882		(ug_info->statisticsMode &
2883		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX),
2884		&ugeth->p_tx_glbl_pram->txrmonbaseptr,
2885		ugeth->tx_fw_statistics_pram_offset,
2886		&ugeth->p_rx_glbl_pram->rxrmonbaseptr,
2887		ugeth->rx_fw_statistics_pram_offset,
2888		&ugeth->p_tx_glbl_pram->temoder,
2889		&ugeth->p_rx_glbl_pram->remoder);
2890
2891	/* function code register */
2892	out_8(&ugeth->p_rx_glbl_pram->rstate, function_code);
2893
2894	/* initialize extended filtering */
2895	if (ug_info->rxExtendedFiltering) {
2896		if (!ug_info->extendedFilteringChainPointer) {
2897			if (netif_msg_ifup(ugeth))
2898				pr_err("Null Extended Filtering Chain Pointer\n");
2899			return -EINVAL;
2900		}
2901
2902		/* Allocate memory for extended filtering Mode Global
2903		Parameters */
2904		ugeth->exf_glbl_param_offset =
2905		    qe_muram_alloc(sizeof(struct ucc_geth_exf_global_pram),
2906		UCC_GETH_RX_EXTENDED_FILTERING_GLOBAL_PARAMETERS_ALIGNMENT);
2907		if (IS_ERR_VALUE(ugeth->exf_glbl_param_offset)) {
2908			if (netif_msg_ifup(ugeth))
2909				pr_err("Can not allocate DPRAM memory for p_exf_glbl_param\n");
2910			return -ENOMEM;
2911		}
2912
2913		ugeth->p_exf_glbl_param =
2914		    (struct ucc_geth_exf_global_pram __iomem *) qe_muram_addr(ugeth->
2915				 exf_glbl_param_offset);
2916		out_be32(&ugeth->p_rx_glbl_pram->exfGlobalParam,
2917			 ugeth->exf_glbl_param_offset);
2918		out_be32(&ugeth->p_exf_glbl_param->l2pcdptr,
2919			 (u32) ug_info->extendedFilteringChainPointer);
2920
2921	} else {		/* initialize 82xx style address filtering */
2922
2923		/* Init individual address recognition registers to disabled */
2924
2925		for (j = 0; j < NUM_OF_PADDRS; j++)
2926			ugeth_82xx_filtering_clear_addr_in_paddr(ugeth, (u8) j);
2927
2928		p_82xx_addr_filt =
2929		    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->
2930		    p_rx_glbl_pram->addressfiltering;
2931
2932		ugeth_82xx_filtering_clear_all_addr_in_hash(ugeth,
2933			ENET_ADDR_TYPE_GROUP);
2934		ugeth_82xx_filtering_clear_all_addr_in_hash(ugeth,
2935			ENET_ADDR_TYPE_INDIVIDUAL);
2936	}
2937
2938	/*
2939	 * Initialize UCC at QE level
2940	 */
2941
2942	command = QE_INIT_TX_RX;
2943
2944	/* Allocate shadow InitEnet command parameter structure.
2945	 * This is needed because after the InitEnet command is executed,
2946	 * the structure in DPRAM is released, because DPRAM is a premium
2947	 * resource.
2948	 * This shadow structure keeps a copy of what was done so that the
2949	 * allocated resources can be released when the channel is freed.
2950	 */
2951	if (!(ugeth->p_init_enet_param_shadow =
2952	      kmalloc(sizeof(struct ucc_geth_init_pram), GFP_KERNEL))) {
2953		if (netif_msg_ifup(ugeth))
2954			pr_err("Can not allocate memory for p_UccInitEnetParamShadows\n");
2955		return -ENOMEM;
2956	}
2957	/* Zero out *p_init_enet_param_shadow */
2958	memset((char *)ugeth->p_init_enet_param_shadow,
2959	       0, sizeof(struct ucc_geth_init_pram));
2960
2961	/* Fill shadow InitEnet command parameter structure */
2962
2963	ugeth->p_init_enet_param_shadow->resinit1 =
2964	    ENET_INIT_PARAM_MAGIC_RES_INIT1;
2965	ugeth->p_init_enet_param_shadow->resinit2 =
2966	    ENET_INIT_PARAM_MAGIC_RES_INIT2;
2967	ugeth->p_init_enet_param_shadow->resinit3 =
2968	    ENET_INIT_PARAM_MAGIC_RES_INIT3;
2969	ugeth->p_init_enet_param_shadow->resinit4 =
2970	    ENET_INIT_PARAM_MAGIC_RES_INIT4;
2971	ugeth->p_init_enet_param_shadow->resinit5 =
2972	    ENET_INIT_PARAM_MAGIC_RES_INIT5;
2973	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
2974	    ((u32) ug_info->numThreadsRx) << ENET_INIT_PARAM_RGF_SHIFT;
2975	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
2976	    ((u32) ug_info->numThreadsTx) << ENET_INIT_PARAM_TGF_SHIFT;
2977
2978	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
2979	    ugeth->rx_glbl_pram_offset | ug_info->riscRx;
2980	if ((ug_info->largestexternallookupkeysize !=
2981	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_NONE) &&
2982	    (ug_info->largestexternallookupkeysize !=
2983	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_8_BYTES) &&
2984	    (ug_info->largestexternallookupkeysize !=
2985	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_16_BYTES)) {
2986		if (netif_msg_ifup(ugeth))
2987			pr_err("Invalid largest External Lookup Key Size\n");
2988		return -EINVAL;
2989	}
2990	ugeth->p_init_enet_param_shadow->largestexternallookupkeysize =
2991	    ug_info->largestexternallookupkeysize;
2992	size = sizeof(struct ucc_geth_thread_rx_pram);
2993	if (ug_info->rxExtendedFiltering) {
2994		size += THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING;
2995		if (ug_info->largestexternallookupkeysize ==
2996		    QE_FLTR_TABLE_LOOKUP_KEY_SIZE_8_BYTES)
2997			size +=
2998			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_8;
2999		if (ug_info->largestexternallookupkeysize ==
3000		    QE_FLTR_TABLE_LOOKUP_KEY_SIZE_16_BYTES)
3001			size +=
3002			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_16;
3003	}
3004
3005	if ((ret_val = fill_init_enet_entries(ugeth, &(ugeth->
3006		p_init_enet_param_shadow->rxthread[0]),
3007		(u8) (numThreadsRxNumerical + 1)
3008		/* Rx needs one extra for terminator */
3009		, size, UCC_GETH_THREAD_RX_PRAM_ALIGNMENT,
3010		ug_info->riscRx, 1)) != 0) {
3011		if (netif_msg_ifup(ugeth))
3012			pr_err("Can not fill p_init_enet_param_shadow\n");
3013		return ret_val;
3014	}
3015
3016	ugeth->p_init_enet_param_shadow->txglobal =
3017	    ugeth->tx_glbl_pram_offset | ug_info->riscTx;
3018	if ((ret_val =
3019	     fill_init_enet_entries(ugeth,
3020				    &(ugeth->p_init_enet_param_shadow->
3021				      txthread[0]), numThreadsTxNumerical,
3022				    sizeof(struct ucc_geth_thread_tx_pram),
3023				    UCC_GETH_THREAD_TX_PRAM_ALIGNMENT,
3024				    ug_info->riscTx, 0)) != 0) {
3025		if (netif_msg_ifup(ugeth))
3026			pr_err("Can not fill p_init_enet_param_shadow\n");
3027		return ret_val;
3028	}
3029
3030	/* Load Rx bds with buffers */
3031	for (i = 0; i < ug_info->numQueuesRx; i++) {
3032		if ((ret_val = rx_bd_buffer_set(ugeth, (u8) i)) != 0) {
3033			if (netif_msg_ifup(ugeth))
3034				pr_err("Can not fill Rx bds with buffers\n");
3035			return ret_val;
3036		}
3037	}
3038
3039	/* Allocate InitEnet command parameter structure */
3040	init_enet_pram_offset = qe_muram_alloc(sizeof(struct ucc_geth_init_pram), 4);
3041	if (IS_ERR_VALUE(init_enet_pram_offset)) {
3042		if (netif_msg_ifup(ugeth))
3043			pr_err("Can not allocate DPRAM memory for p_init_enet_pram\n");
3044		return -ENOMEM;
3045	}
3046	p_init_enet_pram =
3047	    (struct ucc_geth_init_pram __iomem *) qe_muram_addr(init_enet_pram_offset);
3048
3049	/* Copy shadow InitEnet command parameter structure into PRAM */
3050	out_8(&p_init_enet_pram->resinit1,
3051			ugeth->p_init_enet_param_shadow->resinit1);
3052	out_8(&p_init_enet_pram->resinit2,
3053			ugeth->p_init_enet_param_shadow->resinit2);
3054	out_8(&p_init_enet_pram->resinit3,
3055			ugeth->p_init_enet_param_shadow->resinit3);
3056	out_8(&p_init_enet_pram->resinit4,
3057			ugeth->p_init_enet_param_shadow->resinit4);
3058	out_be16(&p_init_enet_pram->resinit5,
3059		 ugeth->p_init_enet_param_shadow->resinit5);
3060	out_8(&p_init_enet_pram->largestexternallookupkeysize,
3061	    ugeth->p_init_enet_param_shadow->largestexternallookupkeysize);
3062	out_be32(&p_init_enet_pram->rgftgfrxglobal,
3063		 ugeth->p_init_enet_param_shadow->rgftgfrxglobal);
3064	for (i = 0; i < ENET_INIT_PARAM_MAX_ENTRIES_RX; i++)
3065		out_be32(&p_init_enet_pram->rxthread[i],
3066			 ugeth->p_init_enet_param_shadow->rxthread[i]);
3067	out_be32(&p_init_enet_pram->txglobal,
3068		 ugeth->p_init_enet_param_shadow->txglobal);
3069	for (i = 0; i < ENET_INIT_PARAM_MAX_ENTRIES_TX; i++)
3070		out_be32(&p_init_enet_pram->txthread[i],
3071			 ugeth->p_init_enet_param_shadow->txthread[i]);
3072
3073	/* Issue QE command */
3074	cecr_subblock =
3075	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
3076	qe_issue_cmd(command, cecr_subblock, QE_CR_PROTOCOL_ETHERNET,
3077		     init_enet_pram_offset);
3078
3079	/* Free InitEnet command parameter */
3080	qe_muram_free(init_enet_pram_offset);
3081
3082	return 0;
3083}
3084
3085/* This is called by the kernel when a frame is ready for transmission. */
3086/* It is pointed to by the dev->hard_start_xmit function pointer */
3087static int ucc_geth_start_xmit(struct sk_buff *skb, struct net_device *dev)
 
3088{
3089	struct ucc_geth_private *ugeth = netdev_priv(dev);
3090#ifdef CONFIG_UGETH_TX_ON_DEMAND
3091	struct ucc_fast_private *uccf;
3092#endif
3093	u8 __iomem *bd;			/* BD pointer */
3094	u32 bd_status;
3095	u8 txQ = 0;
3096	unsigned long flags;
3097
3098	ugeth_vdbg("%s: IN", __func__);
3099
 
3100	spin_lock_irqsave(&ugeth->lock, flags);
3101
3102	dev->stats.tx_bytes += skb->len;
3103
3104	/* Start from the next BD that should be filled */
3105	bd = ugeth->txBd[txQ];
3106	bd_status = in_be32((u32 __iomem *)bd);
3107	/* Save the skb pointer so we can free it later */
3108	ugeth->tx_skbuff[txQ][ugeth->skb_curtx[txQ]] = skb;
3109
3110	/* Update the current skb pointer (wrapping if this was the last) */
3111	ugeth->skb_curtx[txQ] =
3112	    (ugeth->skb_curtx[txQ] +
3113	     1) & TX_RING_MOD_MASK(ugeth->ug_info->bdRingLenTx[txQ]);
3114
3115	/* set up the buffer descriptor */
3116	out_be32(&((struct qe_bd __iomem *)bd)->buf,
3117		      dma_map_single(ugeth->dev, skb->data,
3118			      skb->len, DMA_TO_DEVICE));
3119
3120	/* printk(KERN_DEBUG"skb->data is 0x%x\n",skb->data); */
3121
3122	bd_status = (bd_status & T_W) | T_R | T_I | T_L | skb->len;
3123
3124	/* set bd status and length */
3125	out_be32((u32 __iomem *)bd, bd_status);
3126
3127	/* Move to next BD in the ring */
3128	if (!(bd_status & T_W))
3129		bd += sizeof(struct qe_bd);
3130	else
3131		bd = ugeth->p_tx_bd_ring[txQ];
3132
3133	/* If the next BD still needs to be cleaned up, then the bds
3134	   are full.  We need to tell the kernel to stop sending us stuff. */
3135	if (bd == ugeth->confBd[txQ]) {
3136		if (!netif_queue_stopped(dev))
3137			netif_stop_queue(dev);
3138	}
3139
3140	ugeth->txBd[txQ] = bd;
3141
3142	skb_tx_timestamp(skb);
3143
3144	if (ugeth->p_scheduler) {
3145		ugeth->cpucount[txQ]++;
3146		/* Indicate to QE that there are more Tx bds ready for
3147		transmission */
3148		/* This is done by writing a running counter of the bd
3149		count to the scheduler PRAM. */
3150		out_be16(ugeth->p_cpucount[txQ], ugeth->cpucount[txQ]);
3151	}
3152
3153#ifdef CONFIG_UGETH_TX_ON_DEMAND
3154	uccf = ugeth->uccf;
3155	out_be16(uccf->p_utodr, UCC_FAST_TOD);
3156#endif
3157	spin_unlock_irqrestore(&ugeth->lock, flags);
3158
3159	return NETDEV_TX_OK;
3160}
3161
3162static int ucc_geth_rx(struct ucc_geth_private *ugeth, u8 rxQ, int rx_work_limit)
3163{
3164	struct sk_buff *skb;
3165	u8 __iomem *bd;
3166	u16 length, howmany = 0;
3167	u32 bd_status;
3168	u8 *bdBuffer;
3169	struct net_device *dev;
3170
3171	ugeth_vdbg("%s: IN", __func__);
3172
3173	dev = ugeth->ndev;
3174
3175	/* collect received buffers */
3176	bd = ugeth->rxBd[rxQ];
3177
3178	bd_status = in_be32((u32 __iomem *)bd);
3179
3180	/* while there are received buffers and BD is full (~R_E) */
3181	while (!((bd_status & (R_E)) || (--rx_work_limit < 0))) {
3182		bdBuffer = (u8 *) in_be32(&((struct qe_bd __iomem *)bd)->buf);
3183		length = (u16) ((bd_status & BD_LENGTH_MASK) - 4);
3184		skb = ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]];
3185
3186		/* determine whether buffer is first, last, first and last
3187		(single buffer frame) or middle (not first and not last) */
3188		if (!skb ||
3189		    (!(bd_status & (R_F | R_L))) ||
3190		    (bd_status & R_ERRORS_FATAL)) {
3191			if (netif_msg_rx_err(ugeth))
3192				pr_err("%d: ERROR!!! skb - 0x%08x\n",
3193				       __LINE__, (u32)skb);
3194			dev_kfree_skb(skb);
3195
3196			ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]] = NULL;
3197			dev->stats.rx_dropped++;
3198		} else {
3199			dev->stats.rx_packets++;
3200			howmany++;
3201
3202			/* Prep the skb for the packet */
3203			skb_put(skb, length);
3204
3205			/* Tell the skb what kind of packet this is */
3206			skb->protocol = eth_type_trans(skb, ugeth->ndev);
3207
3208			dev->stats.rx_bytes += length;
3209			/* Send the packet up the stack */
3210			netif_receive_skb(skb);
3211		}
3212
3213		skb = get_new_skb(ugeth, bd);
3214		if (!skb) {
3215			if (netif_msg_rx_err(ugeth))
3216				pr_warn("No Rx Data Buffer\n");
3217			dev->stats.rx_dropped++;
3218			break;
3219		}
3220
3221		ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]] = skb;
3222
3223		/* update to point at the next skb */
3224		ugeth->skb_currx[rxQ] =
3225		    (ugeth->skb_currx[rxQ] +
3226		     1) & RX_RING_MOD_MASK(ugeth->ug_info->bdRingLenRx[rxQ]);
3227
3228		if (bd_status & R_W)
3229			bd = ugeth->p_rx_bd_ring[rxQ];
3230		else
3231			bd += sizeof(struct qe_bd);
3232
3233		bd_status = in_be32((u32 __iomem *)bd);
3234	}
3235
3236	ugeth->rxBd[rxQ] = bd;
3237	return howmany;
3238}
3239
3240static int ucc_geth_tx(struct net_device *dev, u8 txQ)
3241{
3242	/* Start from the next BD that should be filled */
3243	struct ucc_geth_private *ugeth = netdev_priv(dev);
 
 
3244	u8 __iomem *bd;		/* BD pointer */
3245	u32 bd_status;
3246
3247	bd = ugeth->confBd[txQ];
3248	bd_status = in_be32((u32 __iomem *)bd);
3249
3250	/* Normal processing. */
3251	while ((bd_status & T_R) == 0) {
3252		struct sk_buff *skb;
3253
3254		/* BD contains already transmitted buffer.   */
3255		/* Handle the transmitted buffer and release */
3256		/* the BD to be used with the current frame  */
3257
3258		skb = ugeth->tx_skbuff[txQ][ugeth->skb_dirtytx[txQ]];
3259		if (!skb)
3260			break;
3261
 
3262		dev->stats.tx_packets++;
3263
3264		dev_consume_skb_any(skb);
3265
3266		ugeth->tx_skbuff[txQ][ugeth->skb_dirtytx[txQ]] = NULL;
3267		ugeth->skb_dirtytx[txQ] =
3268		    (ugeth->skb_dirtytx[txQ] +
3269		     1) & TX_RING_MOD_MASK(ugeth->ug_info->bdRingLenTx[txQ]);
3270
3271		/* We freed a buffer, so now we can restart transmission */
3272		if (netif_queue_stopped(dev))
3273			netif_wake_queue(dev);
3274
3275		/* Advance the confirmation BD pointer */
3276		if (!(bd_status & T_W))
3277			bd += sizeof(struct qe_bd);
3278		else
3279			bd = ugeth->p_tx_bd_ring[txQ];
3280		bd_status = in_be32((u32 __iomem *)bd);
3281	}
3282	ugeth->confBd[txQ] = bd;
 
3283	return 0;
3284}
3285
3286static int ucc_geth_poll(struct napi_struct *napi, int budget)
3287{
3288	struct ucc_geth_private *ugeth = container_of(napi, struct ucc_geth_private, napi);
3289	struct ucc_geth_info *ug_info;
3290	int howmany, i;
3291
3292	ug_info = ugeth->ug_info;
3293
3294	/* Tx event processing */
3295	spin_lock(&ugeth->lock);
3296	for (i = 0; i < ug_info->numQueuesTx; i++)
3297		ucc_geth_tx(ugeth->ndev, i);
3298	spin_unlock(&ugeth->lock);
3299
3300	howmany = 0;
3301	for (i = 0; i < ug_info->numQueuesRx; i++)
3302		howmany += ucc_geth_rx(ugeth, i, budget - howmany);
3303
3304	if (howmany < budget) {
3305		napi_complete(napi);
3306		setbits32(ugeth->uccf->p_uccm, UCCE_RX_EVENTS | UCCE_TX_EVENTS);
3307	}
3308
3309	return howmany;
3310}
3311
3312static irqreturn_t ucc_geth_irq_handler(int irq, void *info)
3313{
3314	struct net_device *dev = info;
3315	struct ucc_geth_private *ugeth = netdev_priv(dev);
3316	struct ucc_fast_private *uccf;
3317	struct ucc_geth_info *ug_info;
3318	register u32 ucce;
3319	register u32 uccm;
3320
3321	ugeth_vdbg("%s: IN", __func__);
3322
3323	uccf = ugeth->uccf;
3324	ug_info = ugeth->ug_info;
3325
3326	/* read and clear events */
3327	ucce = (u32) in_be32(uccf->p_ucce);
3328	uccm = (u32) in_be32(uccf->p_uccm);
3329	ucce &= uccm;
3330	out_be32(uccf->p_ucce, ucce);
3331
3332	/* check for receive events that require processing */
3333	if (ucce & (UCCE_RX_EVENTS | UCCE_TX_EVENTS)) {
3334		if (napi_schedule_prep(&ugeth->napi)) {
3335			uccm &= ~(UCCE_RX_EVENTS | UCCE_TX_EVENTS);
3336			out_be32(uccf->p_uccm, uccm);
3337			__napi_schedule(&ugeth->napi);
3338		}
3339	}
3340
3341	/* Errors and other events */
3342	if (ucce & UCCE_OTHER) {
3343		if (ucce & UCC_GETH_UCCE_BSY)
3344			dev->stats.rx_errors++;
3345		if (ucce & UCC_GETH_UCCE_TXE)
3346			dev->stats.tx_errors++;
3347	}
3348
3349	return IRQ_HANDLED;
3350}
3351
3352#ifdef CONFIG_NET_POLL_CONTROLLER
3353/*
3354 * Polling 'interrupt' - used by things like netconsole to send skbs
3355 * without having to re-enable interrupts. It's not called while
3356 * the interrupt routine is executing.
3357 */
3358static void ucc_netpoll(struct net_device *dev)
3359{
3360	struct ucc_geth_private *ugeth = netdev_priv(dev);
3361	int irq = ugeth->ug_info->uf_info.irq;
3362
3363	disable_irq(irq);
3364	ucc_geth_irq_handler(irq, dev);
3365	enable_irq(irq);
3366}
3367#endif /* CONFIG_NET_POLL_CONTROLLER */
3368
3369static int ucc_geth_set_mac_addr(struct net_device *dev, void *p)
3370{
3371	struct ucc_geth_private *ugeth = netdev_priv(dev);
3372	struct sockaddr *addr = p;
3373
3374	if (!is_valid_ether_addr(addr->sa_data))
3375		return -EADDRNOTAVAIL;
3376
3377	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3378
3379	/*
3380	 * If device is not running, we will set mac addr register
3381	 * when opening the device.
3382	 */
3383	if (!netif_running(dev))
3384		return 0;
3385
3386	spin_lock_irq(&ugeth->lock);
3387	init_mac_station_addr_regs(dev->dev_addr[0],
3388				   dev->dev_addr[1],
3389				   dev->dev_addr[2],
3390				   dev->dev_addr[3],
3391				   dev->dev_addr[4],
3392				   dev->dev_addr[5],
3393				   &ugeth->ug_regs->macstnaddr1,
3394				   &ugeth->ug_regs->macstnaddr2);
3395	spin_unlock_irq(&ugeth->lock);
3396
3397	return 0;
3398}
3399
3400static int ucc_geth_init_mac(struct ucc_geth_private *ugeth)
3401{
3402	struct net_device *dev = ugeth->ndev;
3403	int err;
3404
3405	err = ucc_struct_init(ugeth);
3406	if (err) {
3407		netif_err(ugeth, ifup, dev, "Cannot configure internal struct, aborting\n");
3408		goto err;
3409	}
3410
3411	err = ucc_geth_startup(ugeth);
3412	if (err) {
3413		netif_err(ugeth, ifup, dev, "Cannot configure net device, aborting\n");
3414		goto err;
3415	}
3416
3417	err = adjust_enet_interface(ugeth);
3418	if (err) {
3419		netif_err(ugeth, ifup, dev, "Cannot configure net device, aborting\n");
3420		goto err;
3421	}
3422
3423	/*       Set MACSTNADDR1, MACSTNADDR2                */
3424	/* For more details see the hardware spec.           */
3425	init_mac_station_addr_regs(dev->dev_addr[0],
3426				   dev->dev_addr[1],
3427				   dev->dev_addr[2],
3428				   dev->dev_addr[3],
3429				   dev->dev_addr[4],
3430				   dev->dev_addr[5],
3431				   &ugeth->ug_regs->macstnaddr1,
3432				   &ugeth->ug_regs->macstnaddr2);
3433
3434	err = ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
3435	if (err) {
3436		netif_err(ugeth, ifup, dev, "Cannot enable net device, aborting\n");
3437		goto err;
3438	}
3439
3440	return 0;
3441err:
3442	ucc_geth_stop(ugeth);
3443	return err;
3444}
3445
3446/* Called when something needs to use the ethernet device */
3447/* Returns 0 for success. */
3448static int ucc_geth_open(struct net_device *dev)
3449{
3450	struct ucc_geth_private *ugeth = netdev_priv(dev);
3451	int err;
3452
3453	ugeth_vdbg("%s: IN", __func__);
3454
3455	/* Test station address */
3456	if (dev->dev_addr[0] & ENET_GROUP_ADDR) {
3457		netif_err(ugeth, ifup, dev,
3458			  "Multicast address used for station address - is this what you wanted?\n");
3459		return -EINVAL;
3460	}
3461
3462	err = init_phy(dev);
3463	if (err) {
3464		netif_err(ugeth, ifup, dev, "Cannot initialize PHY, aborting\n");
3465		return err;
3466	}
3467
3468	err = ucc_geth_init_mac(ugeth);
3469	if (err) {
3470		netif_err(ugeth, ifup, dev, "Cannot initialize MAC, aborting\n");
3471		goto err;
3472	}
3473
3474	err = request_irq(ugeth->ug_info->uf_info.irq, ucc_geth_irq_handler,
3475			  0, "UCC Geth", dev);
3476	if (err) {
3477		netif_err(ugeth, ifup, dev, "Cannot get IRQ for net device, aborting\n");
3478		goto err;
3479	}
3480
3481	phy_start(ugeth->phydev);
3482	napi_enable(&ugeth->napi);
 
3483	netif_start_queue(dev);
3484
3485	device_set_wakeup_capable(&dev->dev,
3486			qe_alive_during_sleep() || ugeth->phydev->irq);
3487	device_set_wakeup_enable(&dev->dev, ugeth->wol_en);
3488
3489	return err;
3490
3491err:
3492	ucc_geth_stop(ugeth);
3493	return err;
3494}
3495
3496/* Stops the kernel queue, and halts the controller */
3497static int ucc_geth_close(struct net_device *dev)
3498{
3499	struct ucc_geth_private *ugeth = netdev_priv(dev);
3500
3501	ugeth_vdbg("%s: IN", __func__);
3502
3503	napi_disable(&ugeth->napi);
3504
3505	cancel_work_sync(&ugeth->timeout_work);
3506	ucc_geth_stop(ugeth);
3507	phy_disconnect(ugeth->phydev);
3508	ugeth->phydev = NULL;
3509
3510	free_irq(ugeth->ug_info->uf_info.irq, ugeth->ndev);
3511
3512	netif_stop_queue(dev);
 
3513
3514	return 0;
3515}
3516
3517/* Reopen device. This will reset the MAC and PHY. */
3518static void ucc_geth_timeout_work(struct work_struct *work)
3519{
3520	struct ucc_geth_private *ugeth;
3521	struct net_device *dev;
3522
3523	ugeth = container_of(work, struct ucc_geth_private, timeout_work);
3524	dev = ugeth->ndev;
3525
3526	ugeth_vdbg("%s: IN", __func__);
3527
3528	dev->stats.tx_errors++;
3529
3530	ugeth_dump_regs(ugeth);
3531
3532	if (dev->flags & IFF_UP) {
3533		/*
3534		 * Must reset MAC *and* PHY. This is done by reopening
3535		 * the device.
3536		 */
3537		netif_tx_stop_all_queues(dev);
3538		ucc_geth_stop(ugeth);
3539		ucc_geth_init_mac(ugeth);
3540		/* Must start PHY here */
3541		phy_start(ugeth->phydev);
3542		netif_tx_start_all_queues(dev);
3543	}
3544
3545	netif_tx_schedule_all(dev);
3546}
3547
3548/*
3549 * ucc_geth_timeout gets called when a packet has not been
3550 * transmitted after a set amount of time.
3551 */
3552static void ucc_geth_timeout(struct net_device *dev)
3553{
3554	struct ucc_geth_private *ugeth = netdev_priv(dev);
3555
3556	schedule_work(&ugeth->timeout_work);
3557}
3558
3559
3560#ifdef CONFIG_PM
3561
3562static int ucc_geth_suspend(struct platform_device *ofdev, pm_message_t state)
3563{
3564	struct net_device *ndev = platform_get_drvdata(ofdev);
3565	struct ucc_geth_private *ugeth = netdev_priv(ndev);
3566
3567	if (!netif_running(ndev))
3568		return 0;
3569
3570	netif_device_detach(ndev);
3571	napi_disable(&ugeth->napi);
3572
3573	/*
3574	 * Disable the controller, otherwise we'll wakeup on any network
3575	 * activity.
3576	 */
3577	ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);
3578
3579	if (ugeth->wol_en & WAKE_MAGIC) {
3580		setbits32(ugeth->uccf->p_uccm, UCC_GETH_UCCE_MPD);
3581		setbits32(&ugeth->ug_regs->maccfg2, MACCFG2_MPE);
3582		ucc_fast_enable(ugeth->uccf, COMM_DIR_RX_AND_TX);
3583	} else if (!(ugeth->wol_en & WAKE_PHY)) {
3584		phy_stop(ugeth->phydev);
3585	}
3586
3587	return 0;
3588}
3589
3590static int ucc_geth_resume(struct platform_device *ofdev)
3591{
3592	struct net_device *ndev = platform_get_drvdata(ofdev);
3593	struct ucc_geth_private *ugeth = netdev_priv(ndev);
3594	int err;
3595
3596	if (!netif_running(ndev))
3597		return 0;
3598
3599	if (qe_alive_during_sleep()) {
3600		if (ugeth->wol_en & WAKE_MAGIC) {
3601			ucc_fast_disable(ugeth->uccf, COMM_DIR_RX_AND_TX);
3602			clrbits32(&ugeth->ug_regs->maccfg2, MACCFG2_MPE);
3603			clrbits32(ugeth->uccf->p_uccm, UCC_GETH_UCCE_MPD);
3604		}
3605		ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
3606	} else {
3607		/*
3608		 * Full reinitialization is required if QE shuts down
3609		 * during sleep.
3610		 */
3611		ucc_geth_memclean(ugeth);
3612
3613		err = ucc_geth_init_mac(ugeth);
3614		if (err) {
3615			netdev_err(ndev, "Cannot initialize MAC, aborting\n");
3616			return err;
3617		}
3618	}
3619
3620	ugeth->oldlink = 0;
3621	ugeth->oldspeed = 0;
3622	ugeth->oldduplex = -1;
3623
3624	phy_stop(ugeth->phydev);
3625	phy_start(ugeth->phydev);
3626
3627	napi_enable(&ugeth->napi);
3628	netif_device_attach(ndev);
3629
3630	return 0;
3631}
3632
3633#else
3634#define ucc_geth_suspend NULL
3635#define ucc_geth_resume NULL
3636#endif
3637
3638static phy_interface_t to_phy_interface(const char *phy_connection_type)
3639{
3640	if (strcasecmp(phy_connection_type, "mii") == 0)
3641		return PHY_INTERFACE_MODE_MII;
3642	if (strcasecmp(phy_connection_type, "gmii") == 0)
3643		return PHY_INTERFACE_MODE_GMII;
3644	if (strcasecmp(phy_connection_type, "tbi") == 0)
3645		return PHY_INTERFACE_MODE_TBI;
3646	if (strcasecmp(phy_connection_type, "rmii") == 0)
3647		return PHY_INTERFACE_MODE_RMII;
3648	if (strcasecmp(phy_connection_type, "rgmii") == 0)
3649		return PHY_INTERFACE_MODE_RGMII;
3650	if (strcasecmp(phy_connection_type, "rgmii-id") == 0)
3651		return PHY_INTERFACE_MODE_RGMII_ID;
3652	if (strcasecmp(phy_connection_type, "rgmii-txid") == 0)
3653		return PHY_INTERFACE_MODE_RGMII_TXID;
3654	if (strcasecmp(phy_connection_type, "rgmii-rxid") == 0)
3655		return PHY_INTERFACE_MODE_RGMII_RXID;
3656	if (strcasecmp(phy_connection_type, "rtbi") == 0)
3657		return PHY_INTERFACE_MODE_RTBI;
3658	if (strcasecmp(phy_connection_type, "sgmii") == 0)
3659		return PHY_INTERFACE_MODE_SGMII;
3660
3661	return PHY_INTERFACE_MODE_MII;
3662}
3663
3664static int ucc_geth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3665{
3666	struct ucc_geth_private *ugeth = netdev_priv(dev);
3667
3668	if (!netif_running(dev))
3669		return -EINVAL;
3670
3671	if (!ugeth->phydev)
3672		return -ENODEV;
3673
3674	return phy_mii_ioctl(ugeth->phydev, rq, cmd);
3675}
3676
3677static const struct net_device_ops ucc_geth_netdev_ops = {
3678	.ndo_open		= ucc_geth_open,
3679	.ndo_stop		= ucc_geth_close,
3680	.ndo_start_xmit		= ucc_geth_start_xmit,
3681	.ndo_validate_addr	= eth_validate_addr,
 
3682	.ndo_set_mac_address	= ucc_geth_set_mac_addr,
3683	.ndo_change_mtu		= eth_change_mtu,
3684	.ndo_set_rx_mode	= ucc_geth_set_multi,
3685	.ndo_tx_timeout		= ucc_geth_timeout,
3686	.ndo_do_ioctl		= ucc_geth_ioctl,
3687#ifdef CONFIG_NET_POLL_CONTROLLER
3688	.ndo_poll_controller	= ucc_netpoll,
3689#endif
3690};
3691
3692static int ucc_geth_probe(struct platform_device* ofdev)
3693{
3694	struct device *device = &ofdev->dev;
3695	struct device_node *np = ofdev->dev.of_node;
3696	struct net_device *dev = NULL;
3697	struct ucc_geth_private *ugeth = NULL;
3698	struct ucc_geth_info *ug_info;
3699	struct resource res;
3700	int err, ucc_num, max_speed = 0;
3701	const unsigned int *prop;
3702	const char *sprop;
3703	const void *mac_addr;
3704	phy_interface_t phy_interface;
3705	static const int enet_to_speed[] = {
3706		SPEED_10, SPEED_10, SPEED_10,
3707		SPEED_100, SPEED_100, SPEED_100,
3708		SPEED_1000, SPEED_1000, SPEED_1000, SPEED_1000,
3709	};
3710	static const phy_interface_t enet_to_phy_interface[] = {
3711		PHY_INTERFACE_MODE_MII, PHY_INTERFACE_MODE_RMII,
3712		PHY_INTERFACE_MODE_RGMII, PHY_INTERFACE_MODE_MII,
3713		PHY_INTERFACE_MODE_RMII, PHY_INTERFACE_MODE_RGMII,
3714		PHY_INTERFACE_MODE_GMII, PHY_INTERFACE_MODE_RGMII,
3715		PHY_INTERFACE_MODE_TBI, PHY_INTERFACE_MODE_RTBI,
3716		PHY_INTERFACE_MODE_SGMII,
3717	};
3718
3719	ugeth_vdbg("%s: IN", __func__);
3720
3721	prop = of_get_property(np, "cell-index", NULL);
3722	if (!prop) {
3723		prop = of_get_property(np, "device-id", NULL);
3724		if (!prop)
3725			return -ENODEV;
3726	}
3727
3728	ucc_num = *prop - 1;
3729	if ((ucc_num < 0) || (ucc_num > 7))
3730		return -ENODEV;
3731
3732	ug_info = &ugeth_info[ucc_num];
3733	if (ug_info == NULL) {
3734		if (netif_msg_probe(&debug))
3735			pr_err("[%d] Missing additional data!\n", ucc_num);
3736		return -ENODEV;
3737	}
3738
3739	ug_info->uf_info.ucc_num = ucc_num;
3740
3741	sprop = of_get_property(np, "rx-clock-name", NULL);
3742	if (sprop) {
3743		ug_info->uf_info.rx_clock = qe_clock_source(sprop);
3744		if ((ug_info->uf_info.rx_clock < QE_CLK_NONE) ||
3745		    (ug_info->uf_info.rx_clock > QE_CLK24)) {
3746			pr_err("invalid rx-clock-name property\n");
3747			return -EINVAL;
3748		}
3749	} else {
3750		prop = of_get_property(np, "rx-clock", NULL);
3751		if (!prop) {
3752			/* If both rx-clock-name and rx-clock are missing,
3753			   we want to tell people to use rx-clock-name. */
3754			pr_err("missing rx-clock-name property\n");
3755			return -EINVAL;
3756		}
3757		if ((*prop < QE_CLK_NONE) || (*prop > QE_CLK24)) {
3758			pr_err("invalid rx-clock propperty\n");
3759			return -EINVAL;
3760		}
3761		ug_info->uf_info.rx_clock = *prop;
3762	}
3763
3764	sprop = of_get_property(np, "tx-clock-name", NULL);
3765	if (sprop) {
3766		ug_info->uf_info.tx_clock = qe_clock_source(sprop);
3767		if ((ug_info->uf_info.tx_clock < QE_CLK_NONE) ||
3768		    (ug_info->uf_info.tx_clock > QE_CLK24)) {
3769			pr_err("invalid tx-clock-name property\n");
3770			return -EINVAL;
3771		}
3772	} else {
3773		prop = of_get_property(np, "tx-clock", NULL);
3774		if (!prop) {
3775			pr_err("missing tx-clock-name property\n");
3776			return -EINVAL;
3777		}
3778		if ((*prop < QE_CLK_NONE) || (*prop > QE_CLK24)) {
3779			pr_err("invalid tx-clock property\n");
3780			return -EINVAL;
3781		}
3782		ug_info->uf_info.tx_clock = *prop;
3783	}
3784
3785	err = of_address_to_resource(np, 0, &res);
3786	if (err)
3787		return -EINVAL;
3788
3789	ug_info->uf_info.regs = res.start;
3790	ug_info->uf_info.irq = irq_of_parse_and_map(np, 0);
3791
3792	ug_info->phy_node = of_parse_phandle(np, "phy-handle", 0);
 
 
 
 
 
 
 
 
 
 
3793
3794	/* Find the TBI PHY node.  If it's not there, we don't support SGMII */
3795	ug_info->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
3796
3797	/* get the phy interface type, or default to MII */
3798	prop = of_get_property(np, "phy-connection-type", NULL);
3799	if (!prop) {
3800		/* handle interface property present in old trees */
3801		prop = of_get_property(ug_info->phy_node, "interface", NULL);
3802		if (prop != NULL) {
3803			phy_interface = enet_to_phy_interface[*prop];
3804			max_speed = enet_to_speed[*prop];
3805		} else
3806			phy_interface = PHY_INTERFACE_MODE_MII;
3807	} else {
3808		phy_interface = to_phy_interface((const char *)prop);
3809	}
3810
3811	/* get speed, or derive from PHY interface */
3812	if (max_speed == 0)
3813		switch (phy_interface) {
3814		case PHY_INTERFACE_MODE_GMII:
3815		case PHY_INTERFACE_MODE_RGMII:
3816		case PHY_INTERFACE_MODE_RGMII_ID:
3817		case PHY_INTERFACE_MODE_RGMII_RXID:
3818		case PHY_INTERFACE_MODE_RGMII_TXID:
3819		case PHY_INTERFACE_MODE_TBI:
3820		case PHY_INTERFACE_MODE_RTBI:
3821		case PHY_INTERFACE_MODE_SGMII:
3822			max_speed = SPEED_1000;
3823			break;
3824		default:
3825			max_speed = SPEED_100;
3826			break;
3827		}
3828
3829	if (max_speed == SPEED_1000) {
3830		unsigned int snums = qe_get_num_of_snums();
3831
3832		/* configure muram FIFOs for gigabit operation */
3833		ug_info->uf_info.urfs = UCC_GETH_URFS_GIGA_INIT;
3834		ug_info->uf_info.urfet = UCC_GETH_URFET_GIGA_INIT;
3835		ug_info->uf_info.urfset = UCC_GETH_URFSET_GIGA_INIT;
3836		ug_info->uf_info.utfs = UCC_GETH_UTFS_GIGA_INIT;
3837		ug_info->uf_info.utfet = UCC_GETH_UTFET_GIGA_INIT;
3838		ug_info->uf_info.utftt = UCC_GETH_UTFTT_GIGA_INIT;
3839		ug_info->numThreadsTx = UCC_GETH_NUM_OF_THREADS_4;
3840
3841		/* If QE's snum number is 46/76 which means we need to support
3842		 * 4 UECs at 1000Base-T simultaneously, we need to allocate
3843		 * more Threads to Rx.
3844		 */
3845		if ((snums == 76) || (snums == 46))
3846			ug_info->numThreadsRx = UCC_GETH_NUM_OF_THREADS_6;
3847		else
3848			ug_info->numThreadsRx = UCC_GETH_NUM_OF_THREADS_4;
3849	}
3850
3851	if (netif_msg_probe(&debug))
3852		pr_info("UCC%1d at 0x%8x (irq = %d)\n",
3853			ug_info->uf_info.ucc_num + 1, ug_info->uf_info.regs,
 
3854			ug_info->uf_info.irq);
3855
3856	/* Create an ethernet device instance */
3857	dev = alloc_etherdev(sizeof(*ugeth));
3858
3859	if (dev == NULL)
3860		return -ENOMEM;
 
 
3861
3862	ugeth = netdev_priv(dev);
3863	spin_lock_init(&ugeth->lock);
3864
3865	/* Create CQs for hash tables */
3866	INIT_LIST_HEAD(&ugeth->group_hash_q);
3867	INIT_LIST_HEAD(&ugeth->ind_hash_q);
3868
3869	dev_set_drvdata(device, dev);
3870
3871	/* Set the dev->base_addr to the gfar reg region */
3872	dev->base_addr = (unsigned long)(ug_info->uf_info.regs);
3873
3874	SET_NETDEV_DEV(dev, device);
3875
3876	/* Fill in the dev structure */
3877	uec_set_ethtool_ops(dev);
3878	dev->netdev_ops = &ucc_geth_netdev_ops;
3879	dev->watchdog_timeo = TX_TIMEOUT;
3880	INIT_WORK(&ugeth->timeout_work, ucc_geth_timeout_work);
3881	netif_napi_add(dev, &ugeth->napi, ucc_geth_poll, 64);
3882	dev->mtu = 1500;
3883
3884	ugeth->msg_enable = netif_msg_init(debug.msg_enable, UGETH_MSG_DEFAULT);
3885	ugeth->phy_interface = phy_interface;
3886	ugeth->max_speed = max_speed;
3887
 
 
 
3888	err = register_netdev(dev);
3889	if (err) {
3890		if (netif_msg_probe(ugeth))
3891			pr_err("%s: Cannot register net device, aborting\n",
3892			       dev->name);
3893		free_netdev(dev);
3894		return err;
3895	}
3896
3897	mac_addr = of_get_mac_address(np);
3898	if (mac_addr)
3899		memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
3900
3901	ugeth->ug_info = ug_info;
3902	ugeth->dev = device;
3903	ugeth->ndev = dev;
3904	ugeth->node = np;
3905
3906	return 0;
 
 
 
 
 
 
 
 
 
 
3907}
3908
3909static int ucc_geth_remove(struct platform_device* ofdev)
3910{
3911	struct net_device *dev = platform_get_drvdata(ofdev);
3912	struct ucc_geth_private *ugeth = netdev_priv(dev);
 
3913
3914	unregister_netdev(dev);
3915	free_netdev(dev);
3916	ucc_geth_memclean(ugeth);
 
 
 
 
3917
3918	return 0;
3919}
3920
3921static struct of_device_id ucc_geth_match[] = {
3922	{
3923		.type = "network",
3924		.compatible = "ucc_geth",
3925	},
3926	{},
3927};
3928
3929MODULE_DEVICE_TABLE(of, ucc_geth_match);
3930
3931static struct platform_driver ucc_geth_driver = {
3932	.driver = {
3933		.name = DRV_NAME,
3934		.owner = THIS_MODULE,
3935		.of_match_table = ucc_geth_match,
3936	},
3937	.probe		= ucc_geth_probe,
3938	.remove		= ucc_geth_remove,
3939	.suspend	= ucc_geth_suspend,
3940	.resume		= ucc_geth_resume,
3941};
3942
3943static int __init ucc_geth_init(void)
3944{
3945	int i, ret;
3946
3947	if (netif_msg_drv(&debug))
3948		pr_info(DRV_DESC "\n");
3949	for (i = 0; i < 8; i++)
3950		memcpy(&(ugeth_info[i]), &ugeth_primary_info,
3951		       sizeof(ugeth_primary_info));
3952
3953	ret = platform_driver_register(&ucc_geth_driver);
3954
3955	return ret;
3956}
3957
3958static void __exit ucc_geth_exit(void)
3959{
3960	platform_driver_unregister(&ucc_geth_driver);
3961}
3962
3963module_init(ucc_geth_init);
3964module_exit(ucc_geth_exit);
3965
3966MODULE_AUTHOR("Freescale Semiconductor, Inc");
3967MODULE_DESCRIPTION(DRV_DESC);
3968MODULE_VERSION(DRV_VERSION);
3969MODULE_LICENSE("GPL");
v5.4
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 * Copyright (C) 2006-2009 Freescale Semicondutor, Inc. All rights reserved.
   4 *
   5 * Author: Shlomi Gridish <gridish@freescale.com>
   6 *	   Li Yang <leoli@freescale.com>
   7 *
   8 * Description:
   9 * QE UCC Gigabit Ethernet Driver
 
 
 
 
 
  10 */
  11
  12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  13
  14#include <linux/kernel.h>
  15#include <linux/init.h>
  16#include <linux/errno.h>
  17#include <linux/slab.h>
  18#include <linux/stddef.h>
  19#include <linux/module.h>
  20#include <linux/interrupt.h>
  21#include <linux/netdevice.h>
  22#include <linux/etherdevice.h>
  23#include <linux/skbuff.h>
  24#include <linux/spinlock.h>
  25#include <linux/mm.h>
  26#include <linux/dma-mapping.h>
  27#include <linux/mii.h>
  28#include <linux/phy.h>
  29#include <linux/phy_fixed.h>
  30#include <linux/workqueue.h>
  31#include <linux/of_address.h>
  32#include <linux/of_irq.h>
  33#include <linux/of_mdio.h>
  34#include <linux/of_net.h>
  35#include <linux/of_platform.h>
  36
  37#include <linux/uaccess.h>
  38#include <asm/irq.h>
  39#include <asm/io.h>
  40#include <soc/fsl/qe/immap_qe.h>
  41#include <soc/fsl/qe/qe.h>
  42#include <soc/fsl/qe/ucc.h>
  43#include <soc/fsl/qe/ucc_fast.h>
  44#include <asm/machdep.h>
  45
  46#include "ucc_geth.h"
  47
  48#undef DEBUG
  49
  50#define ugeth_printk(level, format, arg...)  \
  51        printk(level format "\n", ## arg)
  52
  53#define ugeth_dbg(format, arg...)            \
  54        ugeth_printk(KERN_DEBUG , format , ## arg)
  55
  56#ifdef UGETH_VERBOSE_DEBUG
  57#define ugeth_vdbg ugeth_dbg
  58#else
  59#define ugeth_vdbg(fmt, args...) do { } while (0)
  60#endif				/* UGETH_VERBOSE_DEBUG */
  61#define UGETH_MSG_DEFAULT	(NETIF_MSG_IFUP << 1 ) - 1
  62
  63
  64static DEFINE_SPINLOCK(ugeth_lock);
  65
  66static struct {
  67	u32 msg_enable;
  68} debug = { -1 };
  69
  70module_param_named(debug, debug.msg_enable, int, 0);
  71MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 0xffff=all)");
  72
  73static struct ucc_geth_info ugeth_primary_info = {
  74	.uf_info = {
  75		    .bd_mem_part = MEM_PART_SYSTEM,
  76		    .rtsm = UCC_FAST_SEND_IDLES_BETWEEN_FRAMES,
  77		    .max_rx_buf_length = 1536,
  78		    /* adjusted at startup if max-speed 1000 */
  79		    .urfs = UCC_GETH_URFS_INIT,
  80		    .urfet = UCC_GETH_URFET_INIT,
  81		    .urfset = UCC_GETH_URFSET_INIT,
  82		    .utfs = UCC_GETH_UTFS_INIT,
  83		    .utfet = UCC_GETH_UTFET_INIT,
  84		    .utftt = UCC_GETH_UTFTT_INIT,
  85		    .ufpt = 256,
  86		    .mode = UCC_FAST_PROTOCOL_MODE_ETHERNET,
  87		    .ttx_trx = UCC_FAST_GUMR_TRANSPARENT_TTX_TRX_NORMAL,
  88		    .tenc = UCC_FAST_TX_ENCODING_NRZ,
  89		    .renc = UCC_FAST_RX_ENCODING_NRZ,
  90		    .tcrc = UCC_FAST_16_BIT_CRC,
  91		    .synl = UCC_FAST_SYNC_LEN_NOT_USED,
  92		    },
  93	.numQueuesTx = 1,
  94	.numQueuesRx = 1,
  95	.extendedFilteringChainPointer = ((uint32_t) NULL),
  96	.typeorlen = 3072 /*1536 */ ,
  97	.nonBackToBackIfgPart1 = 0x40,
  98	.nonBackToBackIfgPart2 = 0x60,
  99	.miminumInterFrameGapEnforcement = 0x50,
 100	.backToBackInterFrameGap = 0x60,
 101	.mblinterval = 128,
 102	.nortsrbytetime = 5,
 103	.fracsiz = 1,
 104	.strictpriorityq = 0xff,
 105	.altBebTruncation = 0xa,
 106	.excessDefer = 1,
 107	.maxRetransmission = 0xf,
 108	.collisionWindow = 0x37,
 109	.receiveFlowControl = 1,
 110	.transmitFlowControl = 1,
 111	.maxGroupAddrInHash = 4,
 112	.maxIndAddrInHash = 4,
 113	.prel = 7,
 114	.maxFrameLength = 1518+16, /* Add extra bytes for VLANs etc. */
 115	.minFrameLength = 64,
 116	.maxD1Length = 1520+16, /* Add extra bytes for VLANs etc. */
 117	.maxD2Length = 1520+16, /* Add extra bytes for VLANs etc. */
 118	.vlantype = 0x8100,
 119	.ecamptr = ((uint32_t) NULL),
 120	.eventRegMask = UCCE_OTHER,
 121	.pausePeriod = 0xf000,
 122	.interruptcoalescingmaxvalue = {1, 1, 1, 1, 1, 1, 1, 1},
 123	.bdRingLenTx = {
 124			TX_BD_RING_LEN,
 125			TX_BD_RING_LEN,
 126			TX_BD_RING_LEN,
 127			TX_BD_RING_LEN,
 128			TX_BD_RING_LEN,
 129			TX_BD_RING_LEN,
 130			TX_BD_RING_LEN,
 131			TX_BD_RING_LEN},
 132
 133	.bdRingLenRx = {
 134			RX_BD_RING_LEN,
 135			RX_BD_RING_LEN,
 136			RX_BD_RING_LEN,
 137			RX_BD_RING_LEN,
 138			RX_BD_RING_LEN,
 139			RX_BD_RING_LEN,
 140			RX_BD_RING_LEN,
 141			RX_BD_RING_LEN},
 142
 143	.numStationAddresses = UCC_GETH_NUM_OF_STATION_ADDRESSES_1,
 144	.largestexternallookupkeysize =
 145	    QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_NONE,
 146	.statisticsMode = UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE |
 147		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX |
 148		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX,
 149	.vlanOperationTagged = UCC_GETH_VLAN_OPERATION_TAGGED_NOP,
 150	.vlanOperationNonTagged = UCC_GETH_VLAN_OPERATION_NON_TAGGED_NOP,
 151	.rxQoSMode = UCC_GETH_QOS_MODE_DEFAULT,
 152	.aufc = UPSMR_AUTOMATIC_FLOW_CONTROL_MODE_NONE,
 153	.padAndCrc = MACCFG2_PAD_AND_CRC_MODE_PAD_AND_CRC,
 154	.numThreadsTx = UCC_GETH_NUM_OF_THREADS_1,
 155	.numThreadsRx = UCC_GETH_NUM_OF_THREADS_1,
 156	.riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
 157	.riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
 158};
 159
 160static struct ucc_geth_info ugeth_info[8];
 161
 162#ifdef DEBUG
 163static void mem_disp(u8 *addr, int size)
 164{
 165	u8 *i;
 166	int size16Aling = (size >> 4) << 4;
 167	int size4Aling = (size >> 2) << 2;
 168	int notAlign = 0;
 169	if (size % 16)
 170		notAlign = 1;
 171
 172	for (i = addr; (u32) i < (u32) addr + size16Aling; i += 16)
 173		printk("0x%08x: %08x %08x %08x %08x\r\n",
 174		       (u32) i,
 175		       *((u32 *) (i)),
 176		       *((u32 *) (i + 4)),
 177		       *((u32 *) (i + 8)), *((u32 *) (i + 12)));
 178	if (notAlign == 1)
 179		printk("0x%08x: ", (u32) i);
 180	for (; (u32) i < (u32) addr + size4Aling; i += 4)
 181		printk("%08x ", *((u32 *) (i)));
 182	for (; (u32) i < (u32) addr + size; i++)
 183		printk("%02x", *((i)));
 184	if (notAlign == 1)
 185		printk("\r\n");
 186}
 187#endif /* DEBUG */
 188
 189static struct list_head *dequeue(struct list_head *lh)
 190{
 191	unsigned long flags;
 192
 193	spin_lock_irqsave(&ugeth_lock, flags);
 194	if (!list_empty(lh)) {
 195		struct list_head *node = lh->next;
 196		list_del(node);
 197		spin_unlock_irqrestore(&ugeth_lock, flags);
 198		return node;
 199	} else {
 200		spin_unlock_irqrestore(&ugeth_lock, flags);
 201		return NULL;
 202	}
 203}
 204
 205static struct sk_buff *get_new_skb(struct ucc_geth_private *ugeth,
 206		u8 __iomem *bd)
 207{
 208	struct sk_buff *skb;
 209
 210	skb = netdev_alloc_skb(ugeth->ndev,
 211			       ugeth->ug_info->uf_info.max_rx_buf_length +
 212			       UCC_GETH_RX_DATA_BUF_ALIGNMENT);
 213	if (!skb)
 214		return NULL;
 215
 216	/* We need the data buffer to be aligned properly.  We will reserve
 217	 * as many bytes as needed to align the data properly
 218	 */
 219	skb_reserve(skb,
 220		    UCC_GETH_RX_DATA_BUF_ALIGNMENT -
 221		    (((unsigned)skb->data) & (UCC_GETH_RX_DATA_BUF_ALIGNMENT -
 222					      1)));
 223
 224	out_be32(&((struct qe_bd __iomem *)bd)->buf,
 225		      dma_map_single(ugeth->dev,
 226				     skb->data,
 227				     ugeth->ug_info->uf_info.max_rx_buf_length +
 228				     UCC_GETH_RX_DATA_BUF_ALIGNMENT,
 229				     DMA_FROM_DEVICE));
 230
 231	out_be32((u32 __iomem *)bd,
 232			(R_E | R_I | (in_be32((u32 __iomem*)bd) & R_W)));
 233
 234	return skb;
 235}
 236
 237static int rx_bd_buffer_set(struct ucc_geth_private *ugeth, u8 rxQ)
 238{
 239	u8 __iomem *bd;
 240	u32 bd_status;
 241	struct sk_buff *skb;
 242	int i;
 243
 244	bd = ugeth->p_rx_bd_ring[rxQ];
 245	i = 0;
 246
 247	do {
 248		bd_status = in_be32((u32 __iomem *)bd);
 249		skb = get_new_skb(ugeth, bd);
 250
 251		if (!skb)	/* If can not allocate data buffer,
 252				abort. Cleanup will be elsewhere */
 253			return -ENOMEM;
 254
 255		ugeth->rx_skbuff[rxQ][i] = skb;
 256
 257		/* advance the BD pointer */
 258		bd += sizeof(struct qe_bd);
 259		i++;
 260	} while (!(bd_status & R_W));
 261
 262	return 0;
 263}
 264
 265static int fill_init_enet_entries(struct ucc_geth_private *ugeth,
 266				  u32 *p_start,
 267				  u8 num_entries,
 268				  u32 thread_size,
 269				  u32 thread_alignment,
 270				  unsigned int risc,
 271				  int skip_page_for_first_entry)
 272{
 273	u32 init_enet_offset;
 274	u8 i;
 275	int snum;
 276
 277	for (i = 0; i < num_entries; i++) {
 278		if ((snum = qe_get_snum()) < 0) {
 279			if (netif_msg_ifup(ugeth))
 280				pr_err("Can not get SNUM\n");
 281			return snum;
 282		}
 283		if ((i == 0) && skip_page_for_first_entry)
 284		/* First entry of Rx does not have page */
 285			init_enet_offset = 0;
 286		else {
 287			init_enet_offset =
 288			    qe_muram_alloc(thread_size, thread_alignment);
 289			if (IS_ERR_VALUE(init_enet_offset)) {
 290				if (netif_msg_ifup(ugeth))
 291					pr_err("Can not allocate DPRAM memory\n");
 292				qe_put_snum((u8) snum);
 293				return -ENOMEM;
 294			}
 295		}
 296		*(p_start++) =
 297		    ((u8) snum << ENET_INIT_PARAM_SNUM_SHIFT) | init_enet_offset
 298		    | risc;
 299	}
 300
 301	return 0;
 302}
 303
 304static int return_init_enet_entries(struct ucc_geth_private *ugeth,
 305				    u32 *p_start,
 306				    u8 num_entries,
 307				    unsigned int risc,
 308				    int skip_page_for_first_entry)
 309{
 310	u32 init_enet_offset;
 311	u8 i;
 312	int snum;
 313
 314	for (i = 0; i < num_entries; i++) {
 315		u32 val = *p_start;
 316
 317		/* Check that this entry was actually valid --
 318		needed in case failed in allocations */
 319		if ((val & ENET_INIT_PARAM_RISC_MASK) == risc) {
 320			snum =
 321			    (u32) (val & ENET_INIT_PARAM_SNUM_MASK) >>
 322			    ENET_INIT_PARAM_SNUM_SHIFT;
 323			qe_put_snum((u8) snum);
 324			if (!((i == 0) && skip_page_for_first_entry)) {
 325			/* First entry of Rx does not have page */
 326				init_enet_offset =
 327				    (val & ENET_INIT_PARAM_PTR_MASK);
 328				qe_muram_free(init_enet_offset);
 329			}
 330			*p_start++ = 0;
 331		}
 332	}
 333
 334	return 0;
 335}
 336
 337#ifdef DEBUG
 338static int dump_init_enet_entries(struct ucc_geth_private *ugeth,
 339				  u32 __iomem *p_start,
 340				  u8 num_entries,
 341				  u32 thread_size,
 342				  unsigned int risc,
 343				  int skip_page_for_first_entry)
 344{
 345	u32 init_enet_offset;
 346	u8 i;
 347	int snum;
 348
 349	for (i = 0; i < num_entries; i++) {
 350		u32 val = in_be32(p_start);
 351
 352		/* Check that this entry was actually valid --
 353		needed in case failed in allocations */
 354		if ((val & ENET_INIT_PARAM_RISC_MASK) == risc) {
 355			snum =
 356			    (u32) (val & ENET_INIT_PARAM_SNUM_MASK) >>
 357			    ENET_INIT_PARAM_SNUM_SHIFT;
 358			qe_put_snum((u8) snum);
 359			if (!((i == 0) && skip_page_for_first_entry)) {
 360			/* First entry of Rx does not have page */
 361				init_enet_offset =
 362				    (in_be32(p_start) &
 363				     ENET_INIT_PARAM_PTR_MASK);
 364				pr_info("Init enet entry %d:\n", i);
 365				pr_info("Base address: 0x%08x\n",
 366					(u32)qe_muram_addr(init_enet_offset));
 367				mem_disp(qe_muram_addr(init_enet_offset),
 368					 thread_size);
 369			}
 370			p_start++;
 371		}
 372	}
 373
 374	return 0;
 375}
 376#endif
 377
 378static void put_enet_addr_container(struct enet_addr_container *enet_addr_cont)
 379{
 380	kfree(enet_addr_cont);
 381}
 382
 383static void set_mac_addr(__be16 __iomem *reg, u8 *mac)
 384{
 385	out_be16(&reg[0], ((u16)mac[5] << 8) | mac[4]);
 386	out_be16(&reg[1], ((u16)mac[3] << 8) | mac[2]);
 387	out_be16(&reg[2], ((u16)mac[1] << 8) | mac[0]);
 388}
 389
 390static int hw_clear_addr_in_paddr(struct ucc_geth_private *ugeth, u8 paddr_num)
 391{
 392	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
 393
 394	if (paddr_num >= NUM_OF_PADDRS) {
 395		pr_warn("%s: Invalid paddr_num: %u\n", __func__, paddr_num);
 396		return -EINVAL;
 397	}
 398
 399	p_82xx_addr_filt =
 400	    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->p_rx_glbl_pram->
 401	    addressfiltering;
 402
 403	/* Writing address ff.ff.ff.ff.ff.ff disables address
 404	recognition for this register */
 405	out_be16(&p_82xx_addr_filt->paddr[paddr_num].h, 0xffff);
 406	out_be16(&p_82xx_addr_filt->paddr[paddr_num].m, 0xffff);
 407	out_be16(&p_82xx_addr_filt->paddr[paddr_num].l, 0xffff);
 408
 409	return 0;
 410}
 411
 412static void hw_add_addr_in_hash(struct ucc_geth_private *ugeth,
 413                                u8 *p_enet_addr)
 414{
 415	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
 416	u32 cecr_subblock;
 417
 418	p_82xx_addr_filt =
 419	    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->p_rx_glbl_pram->
 420	    addressfiltering;
 421
 422	cecr_subblock =
 423	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
 424
 425	/* Ethernet frames are defined in Little Endian mode,
 426	therefore to insert */
 427	/* the address to the hash (Big Endian mode), we reverse the bytes.*/
 428
 429	set_mac_addr(&p_82xx_addr_filt->taddr.h, p_enet_addr);
 430
 431	qe_issue_cmd(QE_SET_GROUP_ADDRESS, cecr_subblock,
 432		     QE_CR_PROTOCOL_ETHERNET, 0);
 433}
 434
 435#ifdef DEBUG
 436static void get_statistics(struct ucc_geth_private *ugeth,
 437			   struct ucc_geth_tx_firmware_statistics *
 438			   tx_firmware_statistics,
 439			   struct ucc_geth_rx_firmware_statistics *
 440			   rx_firmware_statistics,
 441			   struct ucc_geth_hardware_statistics *hardware_statistics)
 442{
 443	struct ucc_fast __iomem *uf_regs;
 444	struct ucc_geth __iomem *ug_regs;
 445	struct ucc_geth_tx_firmware_statistics_pram *p_tx_fw_statistics_pram;
 446	struct ucc_geth_rx_firmware_statistics_pram *p_rx_fw_statistics_pram;
 447
 448	ug_regs = ugeth->ug_regs;
 449	uf_regs = (struct ucc_fast __iomem *) ug_regs;
 450	p_tx_fw_statistics_pram = ugeth->p_tx_fw_statistics_pram;
 451	p_rx_fw_statistics_pram = ugeth->p_rx_fw_statistics_pram;
 452
 453	/* Tx firmware only if user handed pointer and driver actually
 454	gathers Tx firmware statistics */
 455	if (tx_firmware_statistics && p_tx_fw_statistics_pram) {
 456		tx_firmware_statistics->sicoltx =
 457		    in_be32(&p_tx_fw_statistics_pram->sicoltx);
 458		tx_firmware_statistics->mulcoltx =
 459		    in_be32(&p_tx_fw_statistics_pram->mulcoltx);
 460		tx_firmware_statistics->latecoltxfr =
 461		    in_be32(&p_tx_fw_statistics_pram->latecoltxfr);
 462		tx_firmware_statistics->frabortduecol =
 463		    in_be32(&p_tx_fw_statistics_pram->frabortduecol);
 464		tx_firmware_statistics->frlostinmactxer =
 465		    in_be32(&p_tx_fw_statistics_pram->frlostinmactxer);
 466		tx_firmware_statistics->carriersenseertx =
 467		    in_be32(&p_tx_fw_statistics_pram->carriersenseertx);
 468		tx_firmware_statistics->frtxok =
 469		    in_be32(&p_tx_fw_statistics_pram->frtxok);
 470		tx_firmware_statistics->txfrexcessivedefer =
 471		    in_be32(&p_tx_fw_statistics_pram->txfrexcessivedefer);
 472		tx_firmware_statistics->txpkts256 =
 473		    in_be32(&p_tx_fw_statistics_pram->txpkts256);
 474		tx_firmware_statistics->txpkts512 =
 475		    in_be32(&p_tx_fw_statistics_pram->txpkts512);
 476		tx_firmware_statistics->txpkts1024 =
 477		    in_be32(&p_tx_fw_statistics_pram->txpkts1024);
 478		tx_firmware_statistics->txpktsjumbo =
 479		    in_be32(&p_tx_fw_statistics_pram->txpktsjumbo);
 480	}
 481
 482	/* Rx firmware only if user handed pointer and driver actually
 483	 * gathers Rx firmware statistics */
 484	if (rx_firmware_statistics && p_rx_fw_statistics_pram) {
 485		int i;
 486		rx_firmware_statistics->frrxfcser =
 487		    in_be32(&p_rx_fw_statistics_pram->frrxfcser);
 488		rx_firmware_statistics->fraligner =
 489		    in_be32(&p_rx_fw_statistics_pram->fraligner);
 490		rx_firmware_statistics->inrangelenrxer =
 491		    in_be32(&p_rx_fw_statistics_pram->inrangelenrxer);
 492		rx_firmware_statistics->outrangelenrxer =
 493		    in_be32(&p_rx_fw_statistics_pram->outrangelenrxer);
 494		rx_firmware_statistics->frtoolong =
 495		    in_be32(&p_rx_fw_statistics_pram->frtoolong);
 496		rx_firmware_statistics->runt =
 497		    in_be32(&p_rx_fw_statistics_pram->runt);
 498		rx_firmware_statistics->verylongevent =
 499		    in_be32(&p_rx_fw_statistics_pram->verylongevent);
 500		rx_firmware_statistics->symbolerror =
 501		    in_be32(&p_rx_fw_statistics_pram->symbolerror);
 502		rx_firmware_statistics->dropbsy =
 503		    in_be32(&p_rx_fw_statistics_pram->dropbsy);
 504		for (i = 0; i < 0x8; i++)
 505			rx_firmware_statistics->res0[i] =
 506			    p_rx_fw_statistics_pram->res0[i];
 507		rx_firmware_statistics->mismatchdrop =
 508		    in_be32(&p_rx_fw_statistics_pram->mismatchdrop);
 509		rx_firmware_statistics->underpkts =
 510		    in_be32(&p_rx_fw_statistics_pram->underpkts);
 511		rx_firmware_statistics->pkts256 =
 512		    in_be32(&p_rx_fw_statistics_pram->pkts256);
 513		rx_firmware_statistics->pkts512 =
 514		    in_be32(&p_rx_fw_statistics_pram->pkts512);
 515		rx_firmware_statistics->pkts1024 =
 516		    in_be32(&p_rx_fw_statistics_pram->pkts1024);
 517		rx_firmware_statistics->pktsjumbo =
 518		    in_be32(&p_rx_fw_statistics_pram->pktsjumbo);
 519		rx_firmware_statistics->frlossinmacer =
 520		    in_be32(&p_rx_fw_statistics_pram->frlossinmacer);
 521		rx_firmware_statistics->pausefr =
 522		    in_be32(&p_rx_fw_statistics_pram->pausefr);
 523		for (i = 0; i < 0x4; i++)
 524			rx_firmware_statistics->res1[i] =
 525			    p_rx_fw_statistics_pram->res1[i];
 526		rx_firmware_statistics->removevlan =
 527		    in_be32(&p_rx_fw_statistics_pram->removevlan);
 528		rx_firmware_statistics->replacevlan =
 529		    in_be32(&p_rx_fw_statistics_pram->replacevlan);
 530		rx_firmware_statistics->insertvlan =
 531		    in_be32(&p_rx_fw_statistics_pram->insertvlan);
 532	}
 533
 534	/* Hardware only if user handed pointer and driver actually
 535	gathers hardware statistics */
 536	if (hardware_statistics &&
 537	    (in_be32(&uf_regs->upsmr) & UCC_GETH_UPSMR_HSE)) {
 538		hardware_statistics->tx64 = in_be32(&ug_regs->tx64);
 539		hardware_statistics->tx127 = in_be32(&ug_regs->tx127);
 540		hardware_statistics->tx255 = in_be32(&ug_regs->tx255);
 541		hardware_statistics->rx64 = in_be32(&ug_regs->rx64);
 542		hardware_statistics->rx127 = in_be32(&ug_regs->rx127);
 543		hardware_statistics->rx255 = in_be32(&ug_regs->rx255);
 544		hardware_statistics->txok = in_be32(&ug_regs->txok);
 545		hardware_statistics->txcf = in_be16(&ug_regs->txcf);
 546		hardware_statistics->tmca = in_be32(&ug_regs->tmca);
 547		hardware_statistics->tbca = in_be32(&ug_regs->tbca);
 548		hardware_statistics->rxfok = in_be32(&ug_regs->rxfok);
 549		hardware_statistics->rxbok = in_be32(&ug_regs->rxbok);
 550		hardware_statistics->rbyt = in_be32(&ug_regs->rbyt);
 551		hardware_statistics->rmca = in_be32(&ug_regs->rmca);
 552		hardware_statistics->rbca = in_be32(&ug_regs->rbca);
 553	}
 554}
 555
 556static void dump_bds(struct ucc_geth_private *ugeth)
 557{
 558	int i;
 559	int length;
 560
 561	for (i = 0; i < ugeth->ug_info->numQueuesTx; i++) {
 562		if (ugeth->p_tx_bd_ring[i]) {
 563			length =
 564			    (ugeth->ug_info->bdRingLenTx[i] *
 565			     sizeof(struct qe_bd));
 566			pr_info("TX BDs[%d]\n", i);
 567			mem_disp(ugeth->p_tx_bd_ring[i], length);
 568		}
 569	}
 570	for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
 571		if (ugeth->p_rx_bd_ring[i]) {
 572			length =
 573			    (ugeth->ug_info->bdRingLenRx[i] *
 574			     sizeof(struct qe_bd));
 575			pr_info("RX BDs[%d]\n", i);
 576			mem_disp(ugeth->p_rx_bd_ring[i], length);
 577		}
 578	}
 579}
 580
 581static void dump_regs(struct ucc_geth_private *ugeth)
 582{
 583	int i;
 584
 585	pr_info("UCC%d Geth registers:\n", ugeth->ug_info->uf_info.ucc_num + 1);
 586	pr_info("Base address: 0x%08x\n", (u32)ugeth->ug_regs);
 587
 588	pr_info("maccfg1    : addr - 0x%08x, val - 0x%08x\n",
 589		(u32)&ugeth->ug_regs->maccfg1,
 590		in_be32(&ugeth->ug_regs->maccfg1));
 591	pr_info("maccfg2    : addr - 0x%08x, val - 0x%08x\n",
 592		(u32)&ugeth->ug_regs->maccfg2,
 593		in_be32(&ugeth->ug_regs->maccfg2));
 594	pr_info("ipgifg     : addr - 0x%08x, val - 0x%08x\n",
 595		(u32)&ugeth->ug_regs->ipgifg,
 596		in_be32(&ugeth->ug_regs->ipgifg));
 597	pr_info("hafdup     : addr - 0x%08x, val - 0x%08x\n",
 598		(u32)&ugeth->ug_regs->hafdup,
 599		in_be32(&ugeth->ug_regs->hafdup));
 600	pr_info("ifctl      : addr - 0x%08x, val - 0x%08x\n",
 601		(u32)&ugeth->ug_regs->ifctl,
 602		in_be32(&ugeth->ug_regs->ifctl));
 603	pr_info("ifstat     : addr - 0x%08x, val - 0x%08x\n",
 604		(u32)&ugeth->ug_regs->ifstat,
 605		in_be32(&ugeth->ug_regs->ifstat));
 606	pr_info("macstnaddr1: addr - 0x%08x, val - 0x%08x\n",
 607		(u32)&ugeth->ug_regs->macstnaddr1,
 608		in_be32(&ugeth->ug_regs->macstnaddr1));
 609	pr_info("macstnaddr2: addr - 0x%08x, val - 0x%08x\n",
 610		(u32)&ugeth->ug_regs->macstnaddr2,
 611		in_be32(&ugeth->ug_regs->macstnaddr2));
 612	pr_info("uempr      : addr - 0x%08x, val - 0x%08x\n",
 613		(u32)&ugeth->ug_regs->uempr,
 614		in_be32(&ugeth->ug_regs->uempr));
 615	pr_info("utbipar    : addr - 0x%08x, val - 0x%08x\n",
 616		(u32)&ugeth->ug_regs->utbipar,
 617		in_be32(&ugeth->ug_regs->utbipar));
 618	pr_info("uescr      : addr - 0x%08x, val - 0x%04x\n",
 619		(u32)&ugeth->ug_regs->uescr,
 620		in_be16(&ugeth->ug_regs->uescr));
 621	pr_info("tx64       : addr - 0x%08x, val - 0x%08x\n",
 622		(u32)&ugeth->ug_regs->tx64,
 623		in_be32(&ugeth->ug_regs->tx64));
 624	pr_info("tx127      : addr - 0x%08x, val - 0x%08x\n",
 625		(u32)&ugeth->ug_regs->tx127,
 626		in_be32(&ugeth->ug_regs->tx127));
 627	pr_info("tx255      : addr - 0x%08x, val - 0x%08x\n",
 628		(u32)&ugeth->ug_regs->tx255,
 629		in_be32(&ugeth->ug_regs->tx255));
 630	pr_info("rx64       : addr - 0x%08x, val - 0x%08x\n",
 631		(u32)&ugeth->ug_regs->rx64,
 632		in_be32(&ugeth->ug_regs->rx64));
 633	pr_info("rx127      : addr - 0x%08x, val - 0x%08x\n",
 634		(u32)&ugeth->ug_regs->rx127,
 635		in_be32(&ugeth->ug_regs->rx127));
 636	pr_info("rx255      : addr - 0x%08x, val - 0x%08x\n",
 637		(u32)&ugeth->ug_regs->rx255,
 638		in_be32(&ugeth->ug_regs->rx255));
 639	pr_info("txok       : addr - 0x%08x, val - 0x%08x\n",
 640		(u32)&ugeth->ug_regs->txok,
 641		in_be32(&ugeth->ug_regs->txok));
 642	pr_info("txcf       : addr - 0x%08x, val - 0x%04x\n",
 643		(u32)&ugeth->ug_regs->txcf,
 644		in_be16(&ugeth->ug_regs->txcf));
 645	pr_info("tmca       : addr - 0x%08x, val - 0x%08x\n",
 646		(u32)&ugeth->ug_regs->tmca,
 647		in_be32(&ugeth->ug_regs->tmca));
 648	pr_info("tbca       : addr - 0x%08x, val - 0x%08x\n",
 649		(u32)&ugeth->ug_regs->tbca,
 650		in_be32(&ugeth->ug_regs->tbca));
 651	pr_info("rxfok      : addr - 0x%08x, val - 0x%08x\n",
 652		(u32)&ugeth->ug_regs->rxfok,
 653		in_be32(&ugeth->ug_regs->rxfok));
 654	pr_info("rxbok      : addr - 0x%08x, val - 0x%08x\n",
 655		(u32)&ugeth->ug_regs->rxbok,
 656		in_be32(&ugeth->ug_regs->rxbok));
 657	pr_info("rbyt       : addr - 0x%08x, val - 0x%08x\n",
 658		(u32)&ugeth->ug_regs->rbyt,
 659		in_be32(&ugeth->ug_regs->rbyt));
 660	pr_info("rmca       : addr - 0x%08x, val - 0x%08x\n",
 661		(u32)&ugeth->ug_regs->rmca,
 662		in_be32(&ugeth->ug_regs->rmca));
 663	pr_info("rbca       : addr - 0x%08x, val - 0x%08x\n",
 664		(u32)&ugeth->ug_regs->rbca,
 665		in_be32(&ugeth->ug_regs->rbca));
 666	pr_info("scar       : addr - 0x%08x, val - 0x%08x\n",
 667		(u32)&ugeth->ug_regs->scar,
 668		in_be32(&ugeth->ug_regs->scar));
 669	pr_info("scam       : addr - 0x%08x, val - 0x%08x\n",
 670		(u32)&ugeth->ug_regs->scam,
 671		in_be32(&ugeth->ug_regs->scam));
 672
 673	if (ugeth->p_thread_data_tx) {
 674		int numThreadsTxNumerical;
 675		switch (ugeth->ug_info->numThreadsTx) {
 676		case UCC_GETH_NUM_OF_THREADS_1:
 677			numThreadsTxNumerical = 1;
 678			break;
 679		case UCC_GETH_NUM_OF_THREADS_2:
 680			numThreadsTxNumerical = 2;
 681			break;
 682		case UCC_GETH_NUM_OF_THREADS_4:
 683			numThreadsTxNumerical = 4;
 684			break;
 685		case UCC_GETH_NUM_OF_THREADS_6:
 686			numThreadsTxNumerical = 6;
 687			break;
 688		case UCC_GETH_NUM_OF_THREADS_8:
 689			numThreadsTxNumerical = 8;
 690			break;
 691		default:
 692			numThreadsTxNumerical = 0;
 693			break;
 694		}
 695
 696		pr_info("Thread data TXs:\n");
 697		pr_info("Base address: 0x%08x\n",
 698			(u32)ugeth->p_thread_data_tx);
 699		for (i = 0; i < numThreadsTxNumerical; i++) {
 700			pr_info("Thread data TX[%d]:\n", i);
 701			pr_info("Base address: 0x%08x\n",
 702				(u32)&ugeth->p_thread_data_tx[i]);
 703			mem_disp((u8 *) & ugeth->p_thread_data_tx[i],
 704				 sizeof(struct ucc_geth_thread_data_tx));
 705		}
 706	}
 707	if (ugeth->p_thread_data_rx) {
 708		int numThreadsRxNumerical;
 709		switch (ugeth->ug_info->numThreadsRx) {
 710		case UCC_GETH_NUM_OF_THREADS_1:
 711			numThreadsRxNumerical = 1;
 712			break;
 713		case UCC_GETH_NUM_OF_THREADS_2:
 714			numThreadsRxNumerical = 2;
 715			break;
 716		case UCC_GETH_NUM_OF_THREADS_4:
 717			numThreadsRxNumerical = 4;
 718			break;
 719		case UCC_GETH_NUM_OF_THREADS_6:
 720			numThreadsRxNumerical = 6;
 721			break;
 722		case UCC_GETH_NUM_OF_THREADS_8:
 723			numThreadsRxNumerical = 8;
 724			break;
 725		default:
 726			numThreadsRxNumerical = 0;
 727			break;
 728		}
 729
 730		pr_info("Thread data RX:\n");
 731		pr_info("Base address: 0x%08x\n",
 732			(u32)ugeth->p_thread_data_rx);
 733		for (i = 0; i < numThreadsRxNumerical; i++) {
 734			pr_info("Thread data RX[%d]:\n", i);
 735			pr_info("Base address: 0x%08x\n",
 736				(u32)&ugeth->p_thread_data_rx[i]);
 737			mem_disp((u8 *) & ugeth->p_thread_data_rx[i],
 738				 sizeof(struct ucc_geth_thread_data_rx));
 739		}
 740	}
 741	if (ugeth->p_exf_glbl_param) {
 742		pr_info("EXF global param:\n");
 743		pr_info("Base address: 0x%08x\n",
 744			(u32)ugeth->p_exf_glbl_param);
 745		mem_disp((u8 *) ugeth->p_exf_glbl_param,
 746			 sizeof(*ugeth->p_exf_glbl_param));
 747	}
 748	if (ugeth->p_tx_glbl_pram) {
 749		pr_info("TX global param:\n");
 750		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_tx_glbl_pram);
 751		pr_info("temoder      : addr - 0x%08x, val - 0x%04x\n",
 752			(u32)&ugeth->p_tx_glbl_pram->temoder,
 753			in_be16(&ugeth->p_tx_glbl_pram->temoder));
 754	       pr_info("sqptr        : addr - 0x%08x, val - 0x%08x\n",
 755			(u32)&ugeth->p_tx_glbl_pram->sqptr,
 756			in_be32(&ugeth->p_tx_glbl_pram->sqptr));
 757		pr_info("schedulerbasepointer: addr - 0x%08x, val - 0x%08x\n",
 758			(u32)&ugeth->p_tx_glbl_pram->schedulerbasepointer,
 759			in_be32(&ugeth->p_tx_glbl_pram->schedulerbasepointer));
 760		pr_info("txrmonbaseptr: addr - 0x%08x, val - 0x%08x\n",
 761			(u32)&ugeth->p_tx_glbl_pram->txrmonbaseptr,
 762			in_be32(&ugeth->p_tx_glbl_pram->txrmonbaseptr));
 763		pr_info("tstate       : addr - 0x%08x, val - 0x%08x\n",
 764			(u32)&ugeth->p_tx_glbl_pram->tstate,
 765			in_be32(&ugeth->p_tx_glbl_pram->tstate));
 766		pr_info("iphoffset[0] : addr - 0x%08x, val - 0x%02x\n",
 767			(u32)&ugeth->p_tx_glbl_pram->iphoffset[0],
 768			ugeth->p_tx_glbl_pram->iphoffset[0]);
 769		pr_info("iphoffset[1] : addr - 0x%08x, val - 0x%02x\n",
 770			(u32)&ugeth->p_tx_glbl_pram->iphoffset[1],
 771			ugeth->p_tx_glbl_pram->iphoffset[1]);
 772		pr_info("iphoffset[2] : addr - 0x%08x, val - 0x%02x\n",
 773			(u32)&ugeth->p_tx_glbl_pram->iphoffset[2],
 774			ugeth->p_tx_glbl_pram->iphoffset[2]);
 775		pr_info("iphoffset[3] : addr - 0x%08x, val - 0x%02x\n",
 776			(u32)&ugeth->p_tx_glbl_pram->iphoffset[3],
 777			ugeth->p_tx_glbl_pram->iphoffset[3]);
 778		pr_info("iphoffset[4] : addr - 0x%08x, val - 0x%02x\n",
 779			(u32)&ugeth->p_tx_glbl_pram->iphoffset[4],
 780			ugeth->p_tx_glbl_pram->iphoffset[4]);
 781		pr_info("iphoffset[5] : addr - 0x%08x, val - 0x%02x\n",
 782			(u32)&ugeth->p_tx_glbl_pram->iphoffset[5],
 783			ugeth->p_tx_glbl_pram->iphoffset[5]);
 784		pr_info("iphoffset[6] : addr - 0x%08x, val - 0x%02x\n",
 785			(u32)&ugeth->p_tx_glbl_pram->iphoffset[6],
 786			ugeth->p_tx_glbl_pram->iphoffset[6]);
 787		pr_info("iphoffset[7] : addr - 0x%08x, val - 0x%02x\n",
 788			(u32)&ugeth->p_tx_glbl_pram->iphoffset[7],
 789			ugeth->p_tx_glbl_pram->iphoffset[7]);
 790		pr_info("vtagtable[0] : addr - 0x%08x, val - 0x%08x\n",
 791			(u32)&ugeth->p_tx_glbl_pram->vtagtable[0],
 792			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[0]));
 793		pr_info("vtagtable[1] : addr - 0x%08x, val - 0x%08x\n",
 794			(u32)&ugeth->p_tx_glbl_pram->vtagtable[1],
 795			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[1]));
 796		pr_info("vtagtable[2] : addr - 0x%08x, val - 0x%08x\n",
 797			(u32)&ugeth->p_tx_glbl_pram->vtagtable[2],
 798			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[2]));
 799		pr_info("vtagtable[3] : addr - 0x%08x, val - 0x%08x\n",
 800			(u32)&ugeth->p_tx_glbl_pram->vtagtable[3],
 801			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[3]));
 802		pr_info("vtagtable[4] : addr - 0x%08x, val - 0x%08x\n",
 803			(u32)&ugeth->p_tx_glbl_pram->vtagtable[4],
 804			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[4]));
 805		pr_info("vtagtable[5] : addr - 0x%08x, val - 0x%08x\n",
 806			(u32)&ugeth->p_tx_glbl_pram->vtagtable[5],
 807			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[5]));
 808		pr_info("vtagtable[6] : addr - 0x%08x, val - 0x%08x\n",
 809			(u32)&ugeth->p_tx_glbl_pram->vtagtable[6],
 810			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[6]));
 811		pr_info("vtagtable[7] : addr - 0x%08x, val - 0x%08x\n",
 812			(u32)&ugeth->p_tx_glbl_pram->vtagtable[7],
 813			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[7]));
 814		pr_info("tqptr        : addr - 0x%08x, val - 0x%08x\n",
 815			(u32)&ugeth->p_tx_glbl_pram->tqptr,
 816			in_be32(&ugeth->p_tx_glbl_pram->tqptr));
 817	}
 818	if (ugeth->p_rx_glbl_pram) {
 819		pr_info("RX global param:\n");
 820		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_rx_glbl_pram);
 821		pr_info("remoder         : addr - 0x%08x, val - 0x%08x\n",
 822			(u32)&ugeth->p_rx_glbl_pram->remoder,
 823			in_be32(&ugeth->p_rx_glbl_pram->remoder));
 824		pr_info("rqptr           : addr - 0x%08x, val - 0x%08x\n",
 825			(u32)&ugeth->p_rx_glbl_pram->rqptr,
 826			in_be32(&ugeth->p_rx_glbl_pram->rqptr));
 827		pr_info("typeorlen       : addr - 0x%08x, val - 0x%04x\n",
 828			(u32)&ugeth->p_rx_glbl_pram->typeorlen,
 829			in_be16(&ugeth->p_rx_glbl_pram->typeorlen));
 830		pr_info("rxgstpack       : addr - 0x%08x, val - 0x%02x\n",
 831			(u32)&ugeth->p_rx_glbl_pram->rxgstpack,
 832			ugeth->p_rx_glbl_pram->rxgstpack);
 833		pr_info("rxrmonbaseptr   : addr - 0x%08x, val - 0x%08x\n",
 834			(u32)&ugeth->p_rx_glbl_pram->rxrmonbaseptr,
 835			in_be32(&ugeth->p_rx_glbl_pram->rxrmonbaseptr));
 836		pr_info("intcoalescingptr: addr - 0x%08x, val - 0x%08x\n",
 837			(u32)&ugeth->p_rx_glbl_pram->intcoalescingptr,
 838			in_be32(&ugeth->p_rx_glbl_pram->intcoalescingptr));
 839		pr_info("rstate          : addr - 0x%08x, val - 0x%02x\n",
 840			(u32)&ugeth->p_rx_glbl_pram->rstate,
 841			ugeth->p_rx_glbl_pram->rstate);
 842		pr_info("mrblr           : addr - 0x%08x, val - 0x%04x\n",
 843			(u32)&ugeth->p_rx_glbl_pram->mrblr,
 844			in_be16(&ugeth->p_rx_glbl_pram->mrblr));
 845		pr_info("rbdqptr         : addr - 0x%08x, val - 0x%08x\n",
 846			(u32)&ugeth->p_rx_glbl_pram->rbdqptr,
 847			in_be32(&ugeth->p_rx_glbl_pram->rbdqptr));
 848		pr_info("mflr            : addr - 0x%08x, val - 0x%04x\n",
 849			(u32)&ugeth->p_rx_glbl_pram->mflr,
 850			in_be16(&ugeth->p_rx_glbl_pram->mflr));
 851		pr_info("minflr          : addr - 0x%08x, val - 0x%04x\n",
 852			(u32)&ugeth->p_rx_glbl_pram->minflr,
 853			in_be16(&ugeth->p_rx_glbl_pram->minflr));
 854		pr_info("maxd1           : addr - 0x%08x, val - 0x%04x\n",
 855			(u32)&ugeth->p_rx_glbl_pram->maxd1,
 856			in_be16(&ugeth->p_rx_glbl_pram->maxd1));
 857		pr_info("maxd2           : addr - 0x%08x, val - 0x%04x\n",
 858			(u32)&ugeth->p_rx_glbl_pram->maxd2,
 859			in_be16(&ugeth->p_rx_glbl_pram->maxd2));
 860		pr_info("ecamptr         : addr - 0x%08x, val - 0x%08x\n",
 861			(u32)&ugeth->p_rx_glbl_pram->ecamptr,
 862			in_be32(&ugeth->p_rx_glbl_pram->ecamptr));
 863		pr_info("l2qt            : addr - 0x%08x, val - 0x%08x\n",
 864			(u32)&ugeth->p_rx_glbl_pram->l2qt,
 865			in_be32(&ugeth->p_rx_glbl_pram->l2qt));
 866		pr_info("l3qt[0]         : addr - 0x%08x, val - 0x%08x\n",
 867			(u32)&ugeth->p_rx_glbl_pram->l3qt[0],
 868			in_be32(&ugeth->p_rx_glbl_pram->l3qt[0]));
 869		pr_info("l3qt[1]         : addr - 0x%08x, val - 0x%08x\n",
 870			(u32)&ugeth->p_rx_glbl_pram->l3qt[1],
 871			in_be32(&ugeth->p_rx_glbl_pram->l3qt[1]));
 872		pr_info("l3qt[2]         : addr - 0x%08x, val - 0x%08x\n",
 873			(u32)&ugeth->p_rx_glbl_pram->l3qt[2],
 874			in_be32(&ugeth->p_rx_glbl_pram->l3qt[2]));
 875		pr_info("l3qt[3]         : addr - 0x%08x, val - 0x%08x\n",
 876			(u32)&ugeth->p_rx_glbl_pram->l3qt[3],
 877			in_be32(&ugeth->p_rx_glbl_pram->l3qt[3]));
 878		pr_info("l3qt[4]         : addr - 0x%08x, val - 0x%08x\n",
 879			(u32)&ugeth->p_rx_glbl_pram->l3qt[4],
 880			in_be32(&ugeth->p_rx_glbl_pram->l3qt[4]));
 881		pr_info("l3qt[5]         : addr - 0x%08x, val - 0x%08x\n",
 882			(u32)&ugeth->p_rx_glbl_pram->l3qt[5],
 883			in_be32(&ugeth->p_rx_glbl_pram->l3qt[5]));
 884		pr_info("l3qt[6]         : addr - 0x%08x, val - 0x%08x\n",
 885			(u32)&ugeth->p_rx_glbl_pram->l3qt[6],
 886			in_be32(&ugeth->p_rx_glbl_pram->l3qt[6]));
 887		pr_info("l3qt[7]         : addr - 0x%08x, val - 0x%08x\n",
 888			(u32)&ugeth->p_rx_glbl_pram->l3qt[7],
 889			in_be32(&ugeth->p_rx_glbl_pram->l3qt[7]));
 890		pr_info("vlantype        : addr - 0x%08x, val - 0x%04x\n",
 891			(u32)&ugeth->p_rx_glbl_pram->vlantype,
 892			in_be16(&ugeth->p_rx_glbl_pram->vlantype));
 893		pr_info("vlantci         : addr - 0x%08x, val - 0x%04x\n",
 894			(u32)&ugeth->p_rx_glbl_pram->vlantci,
 895			in_be16(&ugeth->p_rx_glbl_pram->vlantci));
 896		for (i = 0; i < 64; i++)
 897			pr_info("addressfiltering[%d]: addr - 0x%08x, val - 0x%02x\n",
 898				i,
 899				(u32)&ugeth->p_rx_glbl_pram->addressfiltering[i],
 900				ugeth->p_rx_glbl_pram->addressfiltering[i]);
 901		pr_info("exfGlobalParam  : addr - 0x%08x, val - 0x%08x\n",
 902			(u32)&ugeth->p_rx_glbl_pram->exfGlobalParam,
 903			in_be32(&ugeth->p_rx_glbl_pram->exfGlobalParam));
 904	}
 905	if (ugeth->p_send_q_mem_reg) {
 906		pr_info("Send Q memory registers:\n");
 907		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_send_q_mem_reg);
 908		for (i = 0; i < ugeth->ug_info->numQueuesTx; i++) {
 909			pr_info("SQQD[%d]:\n", i);
 910			pr_info("Base address: 0x%08x\n",
 911				(u32)&ugeth->p_send_q_mem_reg->sqqd[i]);
 912			mem_disp((u8 *) & ugeth->p_send_q_mem_reg->sqqd[i],
 913				 sizeof(struct ucc_geth_send_queue_qd));
 914		}
 915	}
 916	if (ugeth->p_scheduler) {
 917		pr_info("Scheduler:\n");
 918		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_scheduler);
 919		mem_disp((u8 *) ugeth->p_scheduler,
 920			 sizeof(*ugeth->p_scheduler));
 921	}
 922	if (ugeth->p_tx_fw_statistics_pram) {
 923		pr_info("TX FW statistics pram:\n");
 924		pr_info("Base address: 0x%08x\n",
 925			(u32)ugeth->p_tx_fw_statistics_pram);
 926		mem_disp((u8 *) ugeth->p_tx_fw_statistics_pram,
 927			 sizeof(*ugeth->p_tx_fw_statistics_pram));
 928	}
 929	if (ugeth->p_rx_fw_statistics_pram) {
 930		pr_info("RX FW statistics pram:\n");
 931		pr_info("Base address: 0x%08x\n",
 932			(u32)ugeth->p_rx_fw_statistics_pram);
 933		mem_disp((u8 *) ugeth->p_rx_fw_statistics_pram,
 934			 sizeof(*ugeth->p_rx_fw_statistics_pram));
 935	}
 936	if (ugeth->p_rx_irq_coalescing_tbl) {
 937		pr_info("RX IRQ coalescing tables:\n");
 938		pr_info("Base address: 0x%08x\n",
 939			(u32)ugeth->p_rx_irq_coalescing_tbl);
 940		for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
 941			pr_info("RX IRQ coalescing table entry[%d]:\n", i);
 942			pr_info("Base address: 0x%08x\n",
 943				(u32)&ugeth->p_rx_irq_coalescing_tbl->
 944				coalescingentry[i]);
 945			pr_info("interruptcoalescingmaxvalue: addr - 0x%08x, val - 0x%08x\n",
 946				(u32)&ugeth->p_rx_irq_coalescing_tbl->
 947				coalescingentry[i].interruptcoalescingmaxvalue,
 948				in_be32(&ugeth->p_rx_irq_coalescing_tbl->
 949					coalescingentry[i].
 950					interruptcoalescingmaxvalue));
 951			pr_info("interruptcoalescingcounter : addr - 0x%08x, val - 0x%08x\n",
 952				(u32)&ugeth->p_rx_irq_coalescing_tbl->
 953				coalescingentry[i].interruptcoalescingcounter,
 954				in_be32(&ugeth->p_rx_irq_coalescing_tbl->
 955					coalescingentry[i].
 956					interruptcoalescingcounter));
 957		}
 958	}
 959	if (ugeth->p_rx_bd_qs_tbl) {
 960		pr_info("RX BD QS tables:\n");
 961		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_rx_bd_qs_tbl);
 962		for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
 963			pr_info("RX BD QS table[%d]:\n", i);
 964			pr_info("Base address: 0x%08x\n",
 965				(u32)&ugeth->p_rx_bd_qs_tbl[i]);
 966			pr_info("bdbaseptr        : addr - 0x%08x, val - 0x%08x\n",
 967				(u32)&ugeth->p_rx_bd_qs_tbl[i].bdbaseptr,
 968				in_be32(&ugeth->p_rx_bd_qs_tbl[i].bdbaseptr));
 969			pr_info("bdptr            : addr - 0x%08x, val - 0x%08x\n",
 970				(u32)&ugeth->p_rx_bd_qs_tbl[i].bdptr,
 971				in_be32(&ugeth->p_rx_bd_qs_tbl[i].bdptr));
 972			pr_info("externalbdbaseptr: addr - 0x%08x, val - 0x%08x\n",
 973				(u32)&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
 974				in_be32(&ugeth->p_rx_bd_qs_tbl[i].
 975					externalbdbaseptr));
 976			pr_info("externalbdptr    : addr - 0x%08x, val - 0x%08x\n",
 977				(u32)&ugeth->p_rx_bd_qs_tbl[i].externalbdptr,
 978				in_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdptr));
 979			pr_info("ucode RX Prefetched BDs:\n");
 980			pr_info("Base address: 0x%08x\n",
 981				(u32)qe_muram_addr(in_be32
 982						   (&ugeth->p_rx_bd_qs_tbl[i].
 983						    bdbaseptr)));
 984			mem_disp((u8 *)
 985				 qe_muram_addr(in_be32
 986					       (&ugeth->p_rx_bd_qs_tbl[i].
 987						bdbaseptr)),
 988				 sizeof(struct ucc_geth_rx_prefetched_bds));
 989		}
 990	}
 991	if (ugeth->p_init_enet_param_shadow) {
 992		int size;
 993		pr_info("Init enet param shadow:\n");
 994		pr_info("Base address: 0x%08x\n",
 995			(u32) ugeth->p_init_enet_param_shadow);
 996		mem_disp((u8 *) ugeth->p_init_enet_param_shadow,
 997			 sizeof(*ugeth->p_init_enet_param_shadow));
 998
 999		size = sizeof(struct ucc_geth_thread_rx_pram);
1000		if (ugeth->ug_info->rxExtendedFiltering) {
1001			size +=
1002			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING;
1003			if (ugeth->ug_info->largestexternallookupkeysize ==
1004			    QE_FLTR_TABLE_LOOKUP_KEY_SIZE_8_BYTES)
1005				size +=
1006			THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_8;
1007			if (ugeth->ug_info->largestexternallookupkeysize ==
1008			    QE_FLTR_TABLE_LOOKUP_KEY_SIZE_16_BYTES)
1009				size +=
1010			THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_16;
1011		}
1012
1013		dump_init_enet_entries(ugeth,
1014				       &(ugeth->p_init_enet_param_shadow->
1015					 txthread[0]),
1016				       ENET_INIT_PARAM_MAX_ENTRIES_TX,
1017				       sizeof(struct ucc_geth_thread_tx_pram),
1018				       ugeth->ug_info->riscTx, 0);
1019		dump_init_enet_entries(ugeth,
1020				       &(ugeth->p_init_enet_param_shadow->
1021					 rxthread[0]),
1022				       ENET_INIT_PARAM_MAX_ENTRIES_RX, size,
1023				       ugeth->ug_info->riscRx, 1);
1024	}
1025}
1026#endif /* DEBUG */
1027
1028static void init_default_reg_vals(u32 __iomem *upsmr_register,
1029				  u32 __iomem *maccfg1_register,
1030				  u32 __iomem *maccfg2_register)
1031{
1032	out_be32(upsmr_register, UCC_GETH_UPSMR_INIT);
1033	out_be32(maccfg1_register, UCC_GETH_MACCFG1_INIT);
1034	out_be32(maccfg2_register, UCC_GETH_MACCFG2_INIT);
1035}
1036
1037static int init_half_duplex_params(int alt_beb,
1038				   int back_pressure_no_backoff,
1039				   int no_backoff,
1040				   int excess_defer,
1041				   u8 alt_beb_truncation,
1042				   u8 max_retransmissions,
1043				   u8 collision_window,
1044				   u32 __iomem *hafdup_register)
1045{
1046	u32 value = 0;
1047
1048	if ((alt_beb_truncation > HALFDUP_ALT_BEB_TRUNCATION_MAX) ||
1049	    (max_retransmissions > HALFDUP_MAX_RETRANSMISSION_MAX) ||
1050	    (collision_window > HALFDUP_COLLISION_WINDOW_MAX))
1051		return -EINVAL;
1052
1053	value = (u32) (alt_beb_truncation << HALFDUP_ALT_BEB_TRUNCATION_SHIFT);
1054
1055	if (alt_beb)
1056		value |= HALFDUP_ALT_BEB;
1057	if (back_pressure_no_backoff)
1058		value |= HALFDUP_BACK_PRESSURE_NO_BACKOFF;
1059	if (no_backoff)
1060		value |= HALFDUP_NO_BACKOFF;
1061	if (excess_defer)
1062		value |= HALFDUP_EXCESSIVE_DEFER;
1063
1064	value |= (max_retransmissions << HALFDUP_MAX_RETRANSMISSION_SHIFT);
1065
1066	value |= collision_window;
1067
1068	out_be32(hafdup_register, value);
1069	return 0;
1070}
1071
1072static int init_inter_frame_gap_params(u8 non_btb_cs_ipg,
1073				       u8 non_btb_ipg,
1074				       u8 min_ifg,
1075				       u8 btb_ipg,
1076				       u32 __iomem *ipgifg_register)
1077{
1078	u32 value = 0;
1079
1080	/* Non-Back-to-back IPG part 1 should be <= Non-Back-to-back
1081	IPG part 2 */
1082	if (non_btb_cs_ipg > non_btb_ipg)
1083		return -EINVAL;
1084
1085	if ((non_btb_cs_ipg > IPGIFG_NON_BACK_TO_BACK_IFG_PART1_MAX) ||
1086	    (non_btb_ipg > IPGIFG_NON_BACK_TO_BACK_IFG_PART2_MAX) ||
1087	    /*(min_ifg        > IPGIFG_MINIMUM_IFG_ENFORCEMENT_MAX) || */
1088	    (btb_ipg > IPGIFG_BACK_TO_BACK_IFG_MAX))
1089		return -EINVAL;
1090
1091	value |=
1092	    ((non_btb_cs_ipg << IPGIFG_NON_BACK_TO_BACK_IFG_PART1_SHIFT) &
1093	     IPGIFG_NBTB_CS_IPG_MASK);
1094	value |=
1095	    ((non_btb_ipg << IPGIFG_NON_BACK_TO_BACK_IFG_PART2_SHIFT) &
1096	     IPGIFG_NBTB_IPG_MASK);
1097	value |=
1098	    ((min_ifg << IPGIFG_MINIMUM_IFG_ENFORCEMENT_SHIFT) &
1099	     IPGIFG_MIN_IFG_MASK);
1100	value |= (btb_ipg & IPGIFG_BTB_IPG_MASK);
1101
1102	out_be32(ipgifg_register, value);
1103	return 0;
1104}
1105
1106int init_flow_control_params(u32 automatic_flow_control_mode,
1107				    int rx_flow_control_enable,
1108				    int tx_flow_control_enable,
1109				    u16 pause_period,
1110				    u16 extension_field,
1111				    u32 __iomem *upsmr_register,
1112				    u32 __iomem *uempr_register,
1113				    u32 __iomem *maccfg1_register)
1114{
1115	u32 value = 0;
1116
1117	/* Set UEMPR register */
1118	value = (u32) pause_period << UEMPR_PAUSE_TIME_VALUE_SHIFT;
1119	value |= (u32) extension_field << UEMPR_EXTENDED_PAUSE_TIME_VALUE_SHIFT;
1120	out_be32(uempr_register, value);
1121
1122	/* Set UPSMR register */
1123	setbits32(upsmr_register, automatic_flow_control_mode);
1124
1125	value = in_be32(maccfg1_register);
1126	if (rx_flow_control_enable)
1127		value |= MACCFG1_FLOW_RX;
1128	if (tx_flow_control_enable)
1129		value |= MACCFG1_FLOW_TX;
1130	out_be32(maccfg1_register, value);
1131
1132	return 0;
1133}
1134
1135static int init_hw_statistics_gathering_mode(int enable_hardware_statistics,
1136					     int auto_zero_hardware_statistics,
1137					     u32 __iomem *upsmr_register,
1138					     u16 __iomem *uescr_register)
1139{
1140	u16 uescr_value = 0;
1141
1142	/* Enable hardware statistics gathering if requested */
1143	if (enable_hardware_statistics)
1144		setbits32(upsmr_register, UCC_GETH_UPSMR_HSE);
1145
1146	/* Clear hardware statistics counters */
1147	uescr_value = in_be16(uescr_register);
1148	uescr_value |= UESCR_CLRCNT;
1149	/* Automatically zero hardware statistics counters on read,
1150	if requested */
1151	if (auto_zero_hardware_statistics)
1152		uescr_value |= UESCR_AUTOZ;
1153	out_be16(uescr_register, uescr_value);
1154
1155	return 0;
1156}
1157
1158static int init_firmware_statistics_gathering_mode(int
1159		enable_tx_firmware_statistics,
1160		int enable_rx_firmware_statistics,
1161		u32 __iomem *tx_rmon_base_ptr,
1162		u32 tx_firmware_statistics_structure_address,
1163		u32 __iomem *rx_rmon_base_ptr,
1164		u32 rx_firmware_statistics_structure_address,
1165		u16 __iomem *temoder_register,
1166		u32 __iomem *remoder_register)
1167{
1168	/* Note: this function does not check if */
1169	/* the parameters it receives are NULL   */
1170
1171	if (enable_tx_firmware_statistics) {
1172		out_be32(tx_rmon_base_ptr,
1173			 tx_firmware_statistics_structure_address);
1174		setbits16(temoder_register, TEMODER_TX_RMON_STATISTICS_ENABLE);
1175	}
1176
1177	if (enable_rx_firmware_statistics) {
1178		out_be32(rx_rmon_base_ptr,
1179			 rx_firmware_statistics_structure_address);
1180		setbits32(remoder_register, REMODER_RX_RMON_STATISTICS_ENABLE);
1181	}
1182
1183	return 0;
1184}
1185
1186static int init_mac_station_addr_regs(u8 address_byte_0,
1187				      u8 address_byte_1,
1188				      u8 address_byte_2,
1189				      u8 address_byte_3,
1190				      u8 address_byte_4,
1191				      u8 address_byte_5,
1192				      u32 __iomem *macstnaddr1_register,
1193				      u32 __iomem *macstnaddr2_register)
1194{
1195	u32 value = 0;
1196
1197	/* Example: for a station address of 0x12345678ABCD, */
1198	/* 0x12 is byte 0, 0x34 is byte 1 and so on and 0xCD is byte 5 */
1199
1200	/* MACSTNADDR1 Register: */
1201
1202	/* 0                      7   8                      15  */
1203	/* station address byte 5     station address byte 4     */
1204	/* 16                     23  24                     31  */
1205	/* station address byte 3     station address byte 2     */
1206	value |= (u32) ((address_byte_2 << 0) & 0x000000FF);
1207	value |= (u32) ((address_byte_3 << 8) & 0x0000FF00);
1208	value |= (u32) ((address_byte_4 << 16) & 0x00FF0000);
1209	value |= (u32) ((address_byte_5 << 24) & 0xFF000000);
1210
1211	out_be32(macstnaddr1_register, value);
1212
1213	/* MACSTNADDR2 Register: */
1214
1215	/* 0                      7   8                      15  */
1216	/* station address byte 1     station address byte 0     */
1217	/* 16                     23  24                     31  */
1218	/*         reserved                   reserved           */
1219	value = 0;
1220	value |= (u32) ((address_byte_0 << 16) & 0x00FF0000);
1221	value |= (u32) ((address_byte_1 << 24) & 0xFF000000);
1222
1223	out_be32(macstnaddr2_register, value);
1224
1225	return 0;
1226}
1227
1228static int init_check_frame_length_mode(int length_check,
1229					u32 __iomem *maccfg2_register)
1230{
1231	u32 value = 0;
1232
1233	value = in_be32(maccfg2_register);
1234
1235	if (length_check)
1236		value |= MACCFG2_LC;
1237	else
1238		value &= ~MACCFG2_LC;
1239
1240	out_be32(maccfg2_register, value);
1241	return 0;
1242}
1243
1244static int init_preamble_length(u8 preamble_length,
1245				u32 __iomem *maccfg2_register)
1246{
1247	if ((preamble_length < 3) || (preamble_length > 7))
1248		return -EINVAL;
1249
1250	clrsetbits_be32(maccfg2_register, MACCFG2_PREL_MASK,
1251			preamble_length << MACCFG2_PREL_SHIFT);
1252
1253	return 0;
1254}
1255
1256static int init_rx_parameters(int reject_broadcast,
1257			      int receive_short_frames,
1258			      int promiscuous, u32 __iomem *upsmr_register)
1259{
1260	u32 value = 0;
1261
1262	value = in_be32(upsmr_register);
1263
1264	if (reject_broadcast)
1265		value |= UCC_GETH_UPSMR_BRO;
1266	else
1267		value &= ~UCC_GETH_UPSMR_BRO;
1268
1269	if (receive_short_frames)
1270		value |= UCC_GETH_UPSMR_RSH;
1271	else
1272		value &= ~UCC_GETH_UPSMR_RSH;
1273
1274	if (promiscuous)
1275		value |= UCC_GETH_UPSMR_PRO;
1276	else
1277		value &= ~UCC_GETH_UPSMR_PRO;
1278
1279	out_be32(upsmr_register, value);
1280
1281	return 0;
1282}
1283
1284static int init_max_rx_buff_len(u16 max_rx_buf_len,
1285				u16 __iomem *mrblr_register)
1286{
1287	/* max_rx_buf_len value must be a multiple of 128 */
1288	if ((max_rx_buf_len == 0) ||
1289	    (max_rx_buf_len % UCC_GETH_MRBLR_ALIGNMENT))
1290		return -EINVAL;
1291
1292	out_be16(mrblr_register, max_rx_buf_len);
1293	return 0;
1294}
1295
1296static int init_min_frame_len(u16 min_frame_length,
1297			      u16 __iomem *minflr_register,
1298			      u16 __iomem *mrblr_register)
1299{
1300	u16 mrblr_value = 0;
1301
1302	mrblr_value = in_be16(mrblr_register);
1303	if (min_frame_length >= (mrblr_value - 4))
1304		return -EINVAL;
1305
1306	out_be16(minflr_register, min_frame_length);
1307	return 0;
1308}
1309
1310static int adjust_enet_interface(struct ucc_geth_private *ugeth)
1311{
1312	struct ucc_geth_info *ug_info;
1313	struct ucc_geth __iomem *ug_regs;
1314	struct ucc_fast __iomem *uf_regs;
1315	int ret_val;
1316	u32 upsmr, maccfg2;
1317	u16 value;
1318
1319	ugeth_vdbg("%s: IN", __func__);
1320
1321	ug_info = ugeth->ug_info;
1322	ug_regs = ugeth->ug_regs;
1323	uf_regs = ugeth->uccf->uf_regs;
1324
1325	/*                    Set MACCFG2                    */
1326	maccfg2 = in_be32(&ug_regs->maccfg2);
1327	maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
1328	if ((ugeth->max_speed == SPEED_10) ||
1329	    (ugeth->max_speed == SPEED_100))
1330		maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
1331	else if (ugeth->max_speed == SPEED_1000)
1332		maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
1333	maccfg2 |= ug_info->padAndCrc;
1334	out_be32(&ug_regs->maccfg2, maccfg2);
1335
1336	/*                    Set UPSMR                      */
1337	upsmr = in_be32(&uf_regs->upsmr);
1338	upsmr &= ~(UCC_GETH_UPSMR_RPM | UCC_GETH_UPSMR_R10M |
1339		   UCC_GETH_UPSMR_TBIM | UCC_GETH_UPSMR_RMM);
1340	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_RMII) ||
1341	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII) ||
1342	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1343	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1344	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
1345	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1346		if (ugeth->phy_interface != PHY_INTERFACE_MODE_RMII)
1347			upsmr |= UCC_GETH_UPSMR_RPM;
1348		switch (ugeth->max_speed) {
1349		case SPEED_10:
1350			upsmr |= UCC_GETH_UPSMR_R10M;
1351			/* FALLTHROUGH */
1352		case SPEED_100:
1353			if (ugeth->phy_interface != PHY_INTERFACE_MODE_RTBI)
1354				upsmr |= UCC_GETH_UPSMR_RMM;
1355		}
1356	}
1357	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_TBI) ||
1358	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1359		upsmr |= UCC_GETH_UPSMR_TBIM;
1360	}
1361	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_SGMII))
1362		upsmr |= UCC_GETH_UPSMR_SGMM;
1363
1364	out_be32(&uf_regs->upsmr, upsmr);
1365
1366	/* Disable autonegotiation in tbi mode, because by default it
1367	comes up in autonegotiation mode. */
1368	/* Note that this depends on proper setting in utbipar register. */
1369	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_TBI) ||
1370	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1371		struct ucc_geth_info *ug_info = ugeth->ug_info;
1372		struct phy_device *tbiphy;
1373
1374		if (!ug_info->tbi_node)
1375			pr_warn("TBI mode requires that the device tree specify a tbi-handle\n");
1376
1377		tbiphy = of_phy_find_device(ug_info->tbi_node);
1378		if (!tbiphy)
1379			pr_warn("Could not get TBI device\n");
1380
1381		value = phy_read(tbiphy, ENET_TBI_MII_CR);
1382		value &= ~0x1000;	/* Turn off autonegotiation */
1383		phy_write(tbiphy, ENET_TBI_MII_CR, value);
1384
1385		put_device(&tbiphy->mdio.dev);
1386	}
1387
1388	init_check_frame_length_mode(ug_info->lengthCheckRx, &ug_regs->maccfg2);
1389
1390	ret_val = init_preamble_length(ug_info->prel, &ug_regs->maccfg2);
1391	if (ret_val != 0) {
1392		if (netif_msg_probe(ugeth))
1393			pr_err("Preamble length must be between 3 and 7 inclusive\n");
1394		return ret_val;
1395	}
1396
1397	return 0;
1398}
1399
1400static int ugeth_graceful_stop_tx(struct ucc_geth_private *ugeth)
1401{
1402	struct ucc_fast_private *uccf;
1403	u32 cecr_subblock;
1404	u32 temp;
1405	int i = 10;
1406
1407	uccf = ugeth->uccf;
1408
1409	/* Mask GRACEFUL STOP TX interrupt bit and clear it */
1410	clrbits32(uccf->p_uccm, UCC_GETH_UCCE_GRA);
1411	out_be32(uccf->p_ucce, UCC_GETH_UCCE_GRA);  /* clear by writing 1 */
1412
1413	/* Issue host command */
1414	cecr_subblock =
1415	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
1416	qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
1417		     QE_CR_PROTOCOL_ETHERNET, 0);
1418
1419	/* Wait for command to complete */
1420	do {
1421		msleep(10);
1422		temp = in_be32(uccf->p_ucce);
1423	} while (!(temp & UCC_GETH_UCCE_GRA) && --i);
1424
1425	uccf->stopped_tx = 1;
1426
1427	return 0;
1428}
1429
1430static int ugeth_graceful_stop_rx(struct ucc_geth_private *ugeth)
1431{
1432	struct ucc_fast_private *uccf;
1433	u32 cecr_subblock;
1434	u8 temp;
1435	int i = 10;
1436
1437	uccf = ugeth->uccf;
1438
1439	/* Clear acknowledge bit */
1440	temp = in_8(&ugeth->p_rx_glbl_pram->rxgstpack);
1441	temp &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
1442	out_8(&ugeth->p_rx_glbl_pram->rxgstpack, temp);
1443
1444	/* Keep issuing command and checking acknowledge bit until
1445	it is asserted, according to spec */
1446	do {
1447		/* Issue host command */
1448		cecr_subblock =
1449		    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.
1450						ucc_num);
1451		qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
1452			     QE_CR_PROTOCOL_ETHERNET, 0);
1453		msleep(10);
1454		temp = in_8(&ugeth->p_rx_glbl_pram->rxgstpack);
1455	} while (!(temp & GRACEFUL_STOP_ACKNOWLEDGE_RX) && --i);
1456
1457	uccf->stopped_rx = 1;
1458
1459	return 0;
1460}
1461
1462static int ugeth_restart_tx(struct ucc_geth_private *ugeth)
1463{
1464	struct ucc_fast_private *uccf;
1465	u32 cecr_subblock;
1466
1467	uccf = ugeth->uccf;
1468
1469	cecr_subblock =
1470	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
1471	qe_issue_cmd(QE_RESTART_TX, cecr_subblock, QE_CR_PROTOCOL_ETHERNET, 0);
1472	uccf->stopped_tx = 0;
1473
1474	return 0;
1475}
1476
1477static int ugeth_restart_rx(struct ucc_geth_private *ugeth)
1478{
1479	struct ucc_fast_private *uccf;
1480	u32 cecr_subblock;
1481
1482	uccf = ugeth->uccf;
1483
1484	cecr_subblock =
1485	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
1486	qe_issue_cmd(QE_RESTART_RX, cecr_subblock, QE_CR_PROTOCOL_ETHERNET,
1487		     0);
1488	uccf->stopped_rx = 0;
1489
1490	return 0;
1491}
1492
1493static int ugeth_enable(struct ucc_geth_private *ugeth, enum comm_dir mode)
1494{
1495	struct ucc_fast_private *uccf;
1496	int enabled_tx, enabled_rx;
1497
1498	uccf = ugeth->uccf;
1499
1500	/* check if the UCC number is in range. */
1501	if (ugeth->ug_info->uf_info.ucc_num >= UCC_MAX_NUM) {
1502		if (netif_msg_probe(ugeth))
1503			pr_err("ucc_num out of range\n");
1504		return -EINVAL;
1505	}
1506
1507	enabled_tx = uccf->enabled_tx;
1508	enabled_rx = uccf->enabled_rx;
1509
1510	/* Get Tx and Rx going again, in case this channel was actively
1511	disabled. */
1512	if ((mode & COMM_DIR_TX) && (!enabled_tx) && uccf->stopped_tx)
1513		ugeth_restart_tx(ugeth);
1514	if ((mode & COMM_DIR_RX) && (!enabled_rx) && uccf->stopped_rx)
1515		ugeth_restart_rx(ugeth);
1516
1517	ucc_fast_enable(uccf, mode);	/* OK to do even if not disabled */
1518
1519	return 0;
1520
1521}
1522
1523static int ugeth_disable(struct ucc_geth_private *ugeth, enum comm_dir mode)
1524{
1525	struct ucc_fast_private *uccf;
1526
1527	uccf = ugeth->uccf;
1528
1529	/* check if the UCC number is in range. */
1530	if (ugeth->ug_info->uf_info.ucc_num >= UCC_MAX_NUM) {
1531		if (netif_msg_probe(ugeth))
1532			pr_err("ucc_num out of range\n");
1533		return -EINVAL;
1534	}
1535
1536	/* Stop any transmissions */
1537	if ((mode & COMM_DIR_TX) && uccf->enabled_tx && !uccf->stopped_tx)
1538		ugeth_graceful_stop_tx(ugeth);
1539
1540	/* Stop any receptions */
1541	if ((mode & COMM_DIR_RX) && uccf->enabled_rx && !uccf->stopped_rx)
1542		ugeth_graceful_stop_rx(ugeth);
1543
1544	ucc_fast_disable(ugeth->uccf, mode); /* OK to do even if not enabled */
1545
1546	return 0;
1547}
1548
1549static void ugeth_quiesce(struct ucc_geth_private *ugeth)
1550{
1551	/* Prevent any further xmits, plus detach the device. */
1552	netif_device_detach(ugeth->ndev);
1553
1554	/* Wait for any current xmits to finish. */
1555	netif_tx_disable(ugeth->ndev);
1556
1557	/* Disable the interrupt to avoid NAPI rescheduling. */
1558	disable_irq(ugeth->ug_info->uf_info.irq);
1559
1560	/* Stop NAPI, and possibly wait for its completion. */
1561	napi_disable(&ugeth->napi);
1562}
1563
1564static void ugeth_activate(struct ucc_geth_private *ugeth)
1565{
1566	napi_enable(&ugeth->napi);
1567	enable_irq(ugeth->ug_info->uf_info.irq);
1568	netif_device_attach(ugeth->ndev);
1569}
1570
1571/* Called every time the controller might need to be made
1572 * aware of new link state.  The PHY code conveys this
1573 * information through variables in the ugeth structure, and this
1574 * function converts those variables into the appropriate
1575 * register values, and can bring down the device if needed.
1576 */
1577
1578static void adjust_link(struct net_device *dev)
1579{
1580	struct ucc_geth_private *ugeth = netdev_priv(dev);
1581	struct ucc_geth __iomem *ug_regs;
1582	struct ucc_fast __iomem *uf_regs;
1583	struct phy_device *phydev = ugeth->phydev;
1584	int new_state = 0;
1585
1586	ug_regs = ugeth->ug_regs;
1587	uf_regs = ugeth->uccf->uf_regs;
1588
1589	if (phydev->link) {
1590		u32 tempval = in_be32(&ug_regs->maccfg2);
1591		u32 upsmr = in_be32(&uf_regs->upsmr);
1592		/* Now we make sure that we can be in full duplex mode.
1593		 * If not, we operate in half-duplex mode. */
1594		if (phydev->duplex != ugeth->oldduplex) {
1595			new_state = 1;
1596			if (!(phydev->duplex))
1597				tempval &= ~(MACCFG2_FDX);
1598			else
1599				tempval |= MACCFG2_FDX;
1600			ugeth->oldduplex = phydev->duplex;
1601		}
1602
1603		if (phydev->speed != ugeth->oldspeed) {
1604			new_state = 1;
1605			switch (phydev->speed) {
1606			case SPEED_1000:
1607				tempval = ((tempval &
1608					    ~(MACCFG2_INTERFACE_MODE_MASK)) |
1609					    MACCFG2_INTERFACE_MODE_BYTE);
1610				break;
1611			case SPEED_100:
1612			case SPEED_10:
1613				tempval = ((tempval &
1614					    ~(MACCFG2_INTERFACE_MODE_MASK)) |
1615					    MACCFG2_INTERFACE_MODE_NIBBLE);
1616				/* if reduced mode, re-set UPSMR.R10M */
1617				if ((ugeth->phy_interface == PHY_INTERFACE_MODE_RMII) ||
1618				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII) ||
1619				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1620				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
1621				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
1622				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1623					if (phydev->speed == SPEED_10)
1624						upsmr |= UCC_GETH_UPSMR_R10M;
1625					else
1626						upsmr &= ~UCC_GETH_UPSMR_R10M;
1627				}
1628				break;
1629			default:
1630				if (netif_msg_link(ugeth))
1631					pr_warn(
1632						"%s: Ack!  Speed (%d) is not 10/100/1000!",
1633						dev->name, phydev->speed);
1634				break;
1635			}
1636			ugeth->oldspeed = phydev->speed;
1637		}
1638
1639		if (!ugeth->oldlink) {
1640			new_state = 1;
1641			ugeth->oldlink = 1;
1642		}
1643
1644		if (new_state) {
1645			/*
1646			 * To change the MAC configuration we need to disable
1647			 * the controller. To do so, we have to either grab
1648			 * ugeth->lock, which is a bad idea since 'graceful
1649			 * stop' commands might take quite a while, or we can
1650			 * quiesce driver's activity.
1651			 */
1652			ugeth_quiesce(ugeth);
1653			ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);
1654
1655			out_be32(&ug_regs->maccfg2, tempval);
1656			out_be32(&uf_regs->upsmr, upsmr);
1657
1658			ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
1659			ugeth_activate(ugeth);
1660		}
1661	} else if (ugeth->oldlink) {
1662			new_state = 1;
1663			ugeth->oldlink = 0;
1664			ugeth->oldspeed = 0;
1665			ugeth->oldduplex = -1;
1666	}
1667
1668	if (new_state && netif_msg_link(ugeth))
1669		phy_print_status(phydev);
1670}
1671
1672/* Initialize TBI PHY interface for communicating with the
1673 * SERDES lynx PHY on the chip.  We communicate with this PHY
1674 * through the MDIO bus on each controller, treating it as a
1675 * "normal" PHY at the address found in the UTBIPA register.  We assume
1676 * that the UTBIPA register is valid.  Either the MDIO bus code will set
1677 * it to a value that doesn't conflict with other PHYs on the bus, or the
1678 * value doesn't matter, as there are no other PHYs on the bus.
1679 */
1680static void uec_configure_serdes(struct net_device *dev)
1681{
1682	struct ucc_geth_private *ugeth = netdev_priv(dev);
1683	struct ucc_geth_info *ug_info = ugeth->ug_info;
1684	struct phy_device *tbiphy;
1685
1686	if (!ug_info->tbi_node) {
1687		dev_warn(&dev->dev, "SGMII mode requires that the device "
1688			"tree specify a tbi-handle\n");
1689		return;
1690	}
1691
1692	tbiphy = of_phy_find_device(ug_info->tbi_node);
1693	if (!tbiphy) {
1694		dev_err(&dev->dev, "error: Could not get TBI device\n");
1695		return;
1696	}
1697
1698	/*
1699	 * If the link is already up, we must already be ok, and don't need to
1700	 * configure and reset the TBI<->SerDes link.  Maybe U-Boot configured
1701	 * everything for us?  Resetting it takes the link down and requires
1702	 * several seconds for it to come back.
1703	 */
1704	if (phy_read(tbiphy, ENET_TBI_MII_SR) & TBISR_LSTATUS) {
1705		put_device(&tbiphy->mdio.dev);
1706		return;
1707	}
1708
1709	/* Single clk mode, mii mode off(for serdes communication) */
1710	phy_write(tbiphy, ENET_TBI_MII_ANA, TBIANA_SETTINGS);
1711
1712	phy_write(tbiphy, ENET_TBI_MII_TBICON, TBICON_CLK_SELECT);
1713
1714	phy_write(tbiphy, ENET_TBI_MII_CR, TBICR_SETTINGS);
1715
1716	put_device(&tbiphy->mdio.dev);
1717}
1718
1719/* Configure the PHY for dev.
1720 * returns 0 if success.  -1 if failure
1721 */
1722static int init_phy(struct net_device *dev)
1723{
1724	struct ucc_geth_private *priv = netdev_priv(dev);
1725	struct ucc_geth_info *ug_info = priv->ug_info;
1726	struct phy_device *phydev;
1727
1728	priv->oldlink = 0;
1729	priv->oldspeed = 0;
1730	priv->oldduplex = -1;
1731
1732	phydev = of_phy_connect(dev, ug_info->phy_node, &adjust_link, 0,
1733				priv->phy_interface);
 
 
 
1734	if (!phydev) {
1735		dev_err(&dev->dev, "Could not attach to PHY\n");
1736		return -ENODEV;
1737	}
1738
1739	if (priv->phy_interface == PHY_INTERFACE_MODE_SGMII)
1740		uec_configure_serdes(dev);
1741
1742	phy_set_max_speed(phydev, priv->max_speed);
 
 
 
 
 
 
 
 
 
 
1743
1744	priv->phydev = phydev;
1745
1746	return 0;
1747}
1748
1749static void ugeth_dump_regs(struct ucc_geth_private *ugeth)
1750{
1751#ifdef DEBUG
1752	ucc_fast_dump_regs(ugeth->uccf);
1753	dump_regs(ugeth);
1754	dump_bds(ugeth);
1755#endif
1756}
1757
1758static int ugeth_82xx_filtering_clear_all_addr_in_hash(struct ucc_geth_private *
1759						       ugeth,
1760						       enum enet_addr_type
1761						       enet_addr_type)
1762{
1763	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
1764	struct ucc_fast_private *uccf;
1765	enum comm_dir comm_dir;
1766	struct list_head *p_lh;
1767	u16 i, num;
1768	u32 __iomem *addr_h;
1769	u32 __iomem *addr_l;
1770	u8 *p_counter;
1771
1772	uccf = ugeth->uccf;
1773
1774	p_82xx_addr_filt =
1775	    (struct ucc_geth_82xx_address_filtering_pram __iomem *)
1776	    ugeth->p_rx_glbl_pram->addressfiltering;
1777
1778	if (enet_addr_type == ENET_ADDR_TYPE_GROUP) {
1779		addr_h = &(p_82xx_addr_filt->gaddr_h);
1780		addr_l = &(p_82xx_addr_filt->gaddr_l);
1781		p_lh = &ugeth->group_hash_q;
1782		p_counter = &(ugeth->numGroupAddrInHash);
1783	} else if (enet_addr_type == ENET_ADDR_TYPE_INDIVIDUAL) {
1784		addr_h = &(p_82xx_addr_filt->iaddr_h);
1785		addr_l = &(p_82xx_addr_filt->iaddr_l);
1786		p_lh = &ugeth->ind_hash_q;
1787		p_counter = &(ugeth->numIndAddrInHash);
1788	} else
1789		return -EINVAL;
1790
1791	comm_dir = 0;
1792	if (uccf->enabled_tx)
1793		comm_dir |= COMM_DIR_TX;
1794	if (uccf->enabled_rx)
1795		comm_dir |= COMM_DIR_RX;
1796	if (comm_dir)
1797		ugeth_disable(ugeth, comm_dir);
1798
1799	/* Clear the hash table. */
1800	out_be32(addr_h, 0x00000000);
1801	out_be32(addr_l, 0x00000000);
1802
1803	if (!p_lh)
1804		return 0;
1805
1806	num = *p_counter;
1807
1808	/* Delete all remaining CQ elements */
1809	for (i = 0; i < num; i++)
1810		put_enet_addr_container(ENET_ADDR_CONT_ENTRY(dequeue(p_lh)));
1811
1812	*p_counter = 0;
1813
1814	if (comm_dir)
1815		ugeth_enable(ugeth, comm_dir);
1816
1817	return 0;
1818}
1819
1820static int ugeth_82xx_filtering_clear_addr_in_paddr(struct ucc_geth_private *ugeth,
1821						    u8 paddr_num)
1822{
1823	ugeth->indAddrRegUsed[paddr_num] = 0; /* mark this paddr as not used */
1824	return hw_clear_addr_in_paddr(ugeth, paddr_num);/* clear in hardware */
1825}
1826
1827static void ucc_geth_free_rx(struct ucc_geth_private *ugeth)
1828{
1829	struct ucc_geth_info *ug_info;
1830	struct ucc_fast_info *uf_info;
1831	u16 i, j;
1832	u8 __iomem *bd;
1833
1834
1835	ug_info = ugeth->ug_info;
1836	uf_info = &ug_info->uf_info;
1837
1838	for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
1839		if (ugeth->p_rx_bd_ring[i]) {
1840			/* Return existing data buffers in ring */
1841			bd = ugeth->p_rx_bd_ring[i];
1842			for (j = 0; j < ugeth->ug_info->bdRingLenRx[i]; j++) {
1843				if (ugeth->rx_skbuff[i][j]) {
1844					dma_unmap_single(ugeth->dev,
1845						in_be32(&((struct qe_bd __iomem *)bd)->buf),
1846						ugeth->ug_info->
1847						uf_info.max_rx_buf_length +
1848						UCC_GETH_RX_DATA_BUF_ALIGNMENT,
1849						DMA_FROM_DEVICE);
1850					dev_kfree_skb_any(
1851						ugeth->rx_skbuff[i][j]);
1852					ugeth->rx_skbuff[i][j] = NULL;
1853				}
1854				bd += sizeof(struct qe_bd);
1855			}
1856
1857			kfree(ugeth->rx_skbuff[i]);
1858
1859			if (ugeth->ug_info->uf_info.bd_mem_part ==
1860			    MEM_PART_SYSTEM)
1861				kfree((void *)ugeth->rx_bd_ring_offset[i]);
1862			else if (ugeth->ug_info->uf_info.bd_mem_part ==
1863				 MEM_PART_MURAM)
1864				qe_muram_free(ugeth->rx_bd_ring_offset[i]);
1865			ugeth->p_rx_bd_ring[i] = NULL;
1866		}
1867	}
1868
1869}
1870
1871static void ucc_geth_free_tx(struct ucc_geth_private *ugeth)
1872{
1873	struct ucc_geth_info *ug_info;
1874	struct ucc_fast_info *uf_info;
1875	u16 i, j;
1876	u8 __iomem *bd;
1877
1878	netdev_reset_queue(ugeth->ndev);
1879
1880	ug_info = ugeth->ug_info;
1881	uf_info = &ug_info->uf_info;
1882
1883	for (i = 0; i < ugeth->ug_info->numQueuesTx; i++) {
1884		bd = ugeth->p_tx_bd_ring[i];
1885		if (!bd)
1886			continue;
1887		for (j = 0; j < ugeth->ug_info->bdRingLenTx[i]; j++) {
1888			if (ugeth->tx_skbuff[i][j]) {
1889				dma_unmap_single(ugeth->dev,
1890						 in_be32(&((struct qe_bd __iomem *)bd)->buf),
1891						 (in_be32((u32 __iomem *)bd) &
1892						  BD_LENGTH_MASK),
1893						 DMA_TO_DEVICE);
1894				dev_kfree_skb_any(ugeth->tx_skbuff[i][j]);
1895				ugeth->tx_skbuff[i][j] = NULL;
1896			}
1897		}
1898
1899		kfree(ugeth->tx_skbuff[i]);
1900
1901		if (ugeth->p_tx_bd_ring[i]) {
1902			if (ugeth->ug_info->uf_info.bd_mem_part ==
1903			    MEM_PART_SYSTEM)
1904				kfree((void *)ugeth->tx_bd_ring_offset[i]);
1905			else if (ugeth->ug_info->uf_info.bd_mem_part ==
1906				 MEM_PART_MURAM)
1907				qe_muram_free(ugeth->tx_bd_ring_offset[i]);
1908			ugeth->p_tx_bd_ring[i] = NULL;
1909		}
1910	}
1911
1912}
1913
1914static void ucc_geth_memclean(struct ucc_geth_private *ugeth)
1915{
1916	if (!ugeth)
1917		return;
1918
1919	if (ugeth->uccf) {
1920		ucc_fast_free(ugeth->uccf);
1921		ugeth->uccf = NULL;
1922	}
1923
1924	if (ugeth->p_thread_data_tx) {
1925		qe_muram_free(ugeth->thread_dat_tx_offset);
1926		ugeth->p_thread_data_tx = NULL;
1927	}
1928	if (ugeth->p_thread_data_rx) {
1929		qe_muram_free(ugeth->thread_dat_rx_offset);
1930		ugeth->p_thread_data_rx = NULL;
1931	}
1932	if (ugeth->p_exf_glbl_param) {
1933		qe_muram_free(ugeth->exf_glbl_param_offset);
1934		ugeth->p_exf_glbl_param = NULL;
1935	}
1936	if (ugeth->p_rx_glbl_pram) {
1937		qe_muram_free(ugeth->rx_glbl_pram_offset);
1938		ugeth->p_rx_glbl_pram = NULL;
1939	}
1940	if (ugeth->p_tx_glbl_pram) {
1941		qe_muram_free(ugeth->tx_glbl_pram_offset);
1942		ugeth->p_tx_glbl_pram = NULL;
1943	}
1944	if (ugeth->p_send_q_mem_reg) {
1945		qe_muram_free(ugeth->send_q_mem_reg_offset);
1946		ugeth->p_send_q_mem_reg = NULL;
1947	}
1948	if (ugeth->p_scheduler) {
1949		qe_muram_free(ugeth->scheduler_offset);
1950		ugeth->p_scheduler = NULL;
1951	}
1952	if (ugeth->p_tx_fw_statistics_pram) {
1953		qe_muram_free(ugeth->tx_fw_statistics_pram_offset);
1954		ugeth->p_tx_fw_statistics_pram = NULL;
1955	}
1956	if (ugeth->p_rx_fw_statistics_pram) {
1957		qe_muram_free(ugeth->rx_fw_statistics_pram_offset);
1958		ugeth->p_rx_fw_statistics_pram = NULL;
1959	}
1960	if (ugeth->p_rx_irq_coalescing_tbl) {
1961		qe_muram_free(ugeth->rx_irq_coalescing_tbl_offset);
1962		ugeth->p_rx_irq_coalescing_tbl = NULL;
1963	}
1964	if (ugeth->p_rx_bd_qs_tbl) {
1965		qe_muram_free(ugeth->rx_bd_qs_tbl_offset);
1966		ugeth->p_rx_bd_qs_tbl = NULL;
1967	}
1968	if (ugeth->p_init_enet_param_shadow) {
1969		return_init_enet_entries(ugeth,
1970					 &(ugeth->p_init_enet_param_shadow->
1971					   rxthread[0]),
1972					 ENET_INIT_PARAM_MAX_ENTRIES_RX,
1973					 ugeth->ug_info->riscRx, 1);
1974		return_init_enet_entries(ugeth,
1975					 &(ugeth->p_init_enet_param_shadow->
1976					   txthread[0]),
1977					 ENET_INIT_PARAM_MAX_ENTRIES_TX,
1978					 ugeth->ug_info->riscTx, 0);
1979		kfree(ugeth->p_init_enet_param_shadow);
1980		ugeth->p_init_enet_param_shadow = NULL;
1981	}
1982	ucc_geth_free_tx(ugeth);
1983	ucc_geth_free_rx(ugeth);
1984	while (!list_empty(&ugeth->group_hash_q))
1985		put_enet_addr_container(ENET_ADDR_CONT_ENTRY
1986					(dequeue(&ugeth->group_hash_q)));
1987	while (!list_empty(&ugeth->ind_hash_q))
1988		put_enet_addr_container(ENET_ADDR_CONT_ENTRY
1989					(dequeue(&ugeth->ind_hash_q)));
1990	if (ugeth->ug_regs) {
1991		iounmap(ugeth->ug_regs);
1992		ugeth->ug_regs = NULL;
1993	}
1994}
1995
1996static void ucc_geth_set_multi(struct net_device *dev)
1997{
1998	struct ucc_geth_private *ugeth;
1999	struct netdev_hw_addr *ha;
2000	struct ucc_fast __iomem *uf_regs;
2001	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
2002
2003	ugeth = netdev_priv(dev);
2004
2005	uf_regs = ugeth->uccf->uf_regs;
2006
2007	if (dev->flags & IFF_PROMISC) {
2008		setbits32(&uf_regs->upsmr, UCC_GETH_UPSMR_PRO);
2009	} else {
2010		clrbits32(&uf_regs->upsmr, UCC_GETH_UPSMR_PRO);
2011
2012		p_82xx_addr_filt =
2013		    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->
2014		    p_rx_glbl_pram->addressfiltering;
2015
2016		if (dev->flags & IFF_ALLMULTI) {
2017			/* Catch all multicast addresses, so set the
2018			 * filter to all 1's.
2019			 */
2020			out_be32(&p_82xx_addr_filt->gaddr_h, 0xffffffff);
2021			out_be32(&p_82xx_addr_filt->gaddr_l, 0xffffffff);
2022		} else {
2023			/* Clear filter and add the addresses in the list.
2024			 */
2025			out_be32(&p_82xx_addr_filt->gaddr_h, 0x0);
2026			out_be32(&p_82xx_addr_filt->gaddr_l, 0x0);
2027
2028			netdev_for_each_mc_addr(ha, dev) {
2029				/* Ask CPM to run CRC and set bit in
2030				 * filter mask.
2031				 */
2032				hw_add_addr_in_hash(ugeth, ha->addr);
2033			}
2034		}
2035	}
2036}
2037
2038static void ucc_geth_stop(struct ucc_geth_private *ugeth)
2039{
2040	struct ucc_geth __iomem *ug_regs = ugeth->ug_regs;
2041	struct phy_device *phydev = ugeth->phydev;
2042
2043	ugeth_vdbg("%s: IN", __func__);
2044
2045	/*
2046	 * Tell the kernel the link is down.
2047	 * Must be done before disabling the controller
2048	 * or deadlock may happen.
2049	 */
2050	phy_stop(phydev);
2051
2052	/* Disable the controller */
2053	ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);
2054
2055	/* Mask all interrupts */
2056	out_be32(ugeth->uccf->p_uccm, 0x00000000);
2057
2058	/* Clear all interrupts */
2059	out_be32(ugeth->uccf->p_ucce, 0xffffffff);
2060
2061	/* Disable Rx and Tx */
2062	clrbits32(&ug_regs->maccfg1, MACCFG1_ENABLE_RX | MACCFG1_ENABLE_TX);
2063
2064	ucc_geth_memclean(ugeth);
2065}
2066
2067static int ucc_struct_init(struct ucc_geth_private *ugeth)
2068{
2069	struct ucc_geth_info *ug_info;
2070	struct ucc_fast_info *uf_info;
2071	int i;
2072
2073	ug_info = ugeth->ug_info;
2074	uf_info = &ug_info->uf_info;
2075
2076	if (!((uf_info->bd_mem_part == MEM_PART_SYSTEM) ||
2077	      (uf_info->bd_mem_part == MEM_PART_MURAM))) {
2078		if (netif_msg_probe(ugeth))
2079			pr_err("Bad memory partition value\n");
2080		return -EINVAL;
2081	}
2082
2083	/* Rx BD lengths */
2084	for (i = 0; i < ug_info->numQueuesRx; i++) {
2085		if ((ug_info->bdRingLenRx[i] < UCC_GETH_RX_BD_RING_SIZE_MIN) ||
2086		    (ug_info->bdRingLenRx[i] %
2087		     UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT)) {
2088			if (netif_msg_probe(ugeth))
2089				pr_err("Rx BD ring length must be multiple of 4, no smaller than 8\n");
2090			return -EINVAL;
2091		}
2092	}
2093
2094	/* Tx BD lengths */
2095	for (i = 0; i < ug_info->numQueuesTx; i++) {
2096		if (ug_info->bdRingLenTx[i] < UCC_GETH_TX_BD_RING_SIZE_MIN) {
2097			if (netif_msg_probe(ugeth))
2098				pr_err("Tx BD ring length must be no smaller than 2\n");
2099			return -EINVAL;
2100		}
2101	}
2102
2103	/* mrblr */
2104	if ((uf_info->max_rx_buf_length == 0) ||
2105	    (uf_info->max_rx_buf_length % UCC_GETH_MRBLR_ALIGNMENT)) {
2106		if (netif_msg_probe(ugeth))
2107			pr_err("max_rx_buf_length must be non-zero multiple of 128\n");
2108		return -EINVAL;
2109	}
2110
2111	/* num Tx queues */
2112	if (ug_info->numQueuesTx > NUM_TX_QUEUES) {
2113		if (netif_msg_probe(ugeth))
2114			pr_err("number of tx queues too large\n");
2115		return -EINVAL;
2116	}
2117
2118	/* num Rx queues */
2119	if (ug_info->numQueuesRx > NUM_RX_QUEUES) {
2120		if (netif_msg_probe(ugeth))
2121			pr_err("number of rx queues too large\n");
2122		return -EINVAL;
2123	}
2124
2125	/* l2qt */
2126	for (i = 0; i < UCC_GETH_VLAN_PRIORITY_MAX; i++) {
2127		if (ug_info->l2qt[i] >= ug_info->numQueuesRx) {
2128			if (netif_msg_probe(ugeth))
2129				pr_err("VLAN priority table entry must not be larger than number of Rx queues\n");
2130			return -EINVAL;
2131		}
2132	}
2133
2134	/* l3qt */
2135	for (i = 0; i < UCC_GETH_IP_PRIORITY_MAX; i++) {
2136		if (ug_info->l3qt[i] >= ug_info->numQueuesRx) {
2137			if (netif_msg_probe(ugeth))
2138				pr_err("IP priority table entry must not be larger than number of Rx queues\n");
2139			return -EINVAL;
2140		}
2141	}
2142
2143	if (ug_info->cam && !ug_info->ecamptr) {
2144		if (netif_msg_probe(ugeth))
2145			pr_err("If cam mode is chosen, must supply cam ptr\n");
2146		return -EINVAL;
2147	}
2148
2149	if ((ug_info->numStationAddresses !=
2150	     UCC_GETH_NUM_OF_STATION_ADDRESSES_1) &&
2151	    ug_info->rxExtendedFiltering) {
2152		if (netif_msg_probe(ugeth))
2153			pr_err("Number of station addresses greater than 1 not allowed in extended parsing mode\n");
2154		return -EINVAL;
2155	}
2156
2157	/* Generate uccm_mask for receive */
2158	uf_info->uccm_mask = ug_info->eventRegMask & UCCE_OTHER;/* Errors */
2159	for (i = 0; i < ug_info->numQueuesRx; i++)
2160		uf_info->uccm_mask |= (UCC_GETH_UCCE_RXF0 << i);
2161
2162	for (i = 0; i < ug_info->numQueuesTx; i++)
2163		uf_info->uccm_mask |= (UCC_GETH_UCCE_TXB0 << i);
2164	/* Initialize the general fast UCC block. */
2165	if (ucc_fast_init(uf_info, &ugeth->uccf)) {
2166		if (netif_msg_probe(ugeth))
2167			pr_err("Failed to init uccf\n");
2168		return -ENOMEM;
2169	}
2170
2171	/* read the number of risc engines, update the riscTx and riscRx
2172	 * if there are 4 riscs in QE
2173	 */
2174	if (qe_get_num_of_risc() == 4) {
2175		ug_info->riscTx = QE_RISC_ALLOCATION_FOUR_RISCS;
2176		ug_info->riscRx = QE_RISC_ALLOCATION_FOUR_RISCS;
2177	}
2178
2179	ugeth->ug_regs = ioremap(uf_info->regs, sizeof(*ugeth->ug_regs));
2180	if (!ugeth->ug_regs) {
2181		if (netif_msg_probe(ugeth))
2182			pr_err("Failed to ioremap regs\n");
2183		return -ENOMEM;
2184	}
2185
2186	return 0;
2187}
2188
2189static int ucc_geth_alloc_tx(struct ucc_geth_private *ugeth)
2190{
2191	struct ucc_geth_info *ug_info;
2192	struct ucc_fast_info *uf_info;
2193	int length;
2194	u16 i, j;
2195	u8 __iomem *bd;
2196
2197	ug_info = ugeth->ug_info;
2198	uf_info = &ug_info->uf_info;
2199
2200	/* Allocate Tx bds */
2201	for (j = 0; j < ug_info->numQueuesTx; j++) {
2202		/* Allocate in multiple of
2203		   UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT,
2204		   according to spec */
2205		length = ((ug_info->bdRingLenTx[j] * sizeof(struct qe_bd))
2206			  / UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT)
2207		    * UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
2208		if ((ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)) %
2209		    UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT)
2210			length += UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
2211		if (uf_info->bd_mem_part == MEM_PART_SYSTEM) {
2212			u32 align = 4;
2213			if (UCC_GETH_TX_BD_RING_ALIGNMENT > 4)
2214				align = UCC_GETH_TX_BD_RING_ALIGNMENT;
2215			ugeth->tx_bd_ring_offset[j] =
2216				(u32) kmalloc((u32) (length + align), GFP_KERNEL);
2217
2218			if (ugeth->tx_bd_ring_offset[j] != 0)
2219				ugeth->p_tx_bd_ring[j] =
2220					(u8 __iomem *)((ugeth->tx_bd_ring_offset[j] +
2221					align) & ~(align - 1));
2222		} else if (uf_info->bd_mem_part == MEM_PART_MURAM) {
2223			ugeth->tx_bd_ring_offset[j] =
2224			    qe_muram_alloc(length,
2225					   UCC_GETH_TX_BD_RING_ALIGNMENT);
2226			if (!IS_ERR_VALUE(ugeth->tx_bd_ring_offset[j]))
2227				ugeth->p_tx_bd_ring[j] =
2228				    (u8 __iomem *) qe_muram_addr(ugeth->
2229							 tx_bd_ring_offset[j]);
2230		}
2231		if (!ugeth->p_tx_bd_ring[j]) {
2232			if (netif_msg_ifup(ugeth))
2233				pr_err("Can not allocate memory for Tx bd rings\n");
2234			return -ENOMEM;
2235		}
2236		/* Zero unused end of bd ring, according to spec */
2237		memset_io((void __iomem *)(ugeth->p_tx_bd_ring[j] +
2238		       ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)), 0,
2239		       length - ug_info->bdRingLenTx[j] * sizeof(struct qe_bd));
2240	}
2241
2242	/* Init Tx bds */
2243	for (j = 0; j < ug_info->numQueuesTx; j++) {
2244		/* Setup the skbuff rings */
2245		ugeth->tx_skbuff[j] =
2246			kmalloc_array(ugeth->ug_info->bdRingLenTx[j],
2247				      sizeof(struct sk_buff *), GFP_KERNEL);
2248
2249		if (ugeth->tx_skbuff[j] == NULL) {
2250			if (netif_msg_ifup(ugeth))
2251				pr_err("Could not allocate tx_skbuff\n");
2252			return -ENOMEM;
2253		}
2254
2255		for (i = 0; i < ugeth->ug_info->bdRingLenTx[j]; i++)
2256			ugeth->tx_skbuff[j][i] = NULL;
2257
2258		ugeth->skb_curtx[j] = ugeth->skb_dirtytx[j] = 0;
2259		bd = ugeth->confBd[j] = ugeth->txBd[j] = ugeth->p_tx_bd_ring[j];
2260		for (i = 0; i < ug_info->bdRingLenTx[j]; i++) {
2261			/* clear bd buffer */
2262			out_be32(&((struct qe_bd __iomem *)bd)->buf, 0);
2263			/* set bd status and length */
2264			out_be32((u32 __iomem *)bd, 0);
2265			bd += sizeof(struct qe_bd);
2266		}
2267		bd -= sizeof(struct qe_bd);
2268		/* set bd status and length */
2269		out_be32((u32 __iomem *)bd, T_W); /* for last BD set Wrap bit */
2270	}
2271
2272	return 0;
2273}
2274
2275static int ucc_geth_alloc_rx(struct ucc_geth_private *ugeth)
2276{
2277	struct ucc_geth_info *ug_info;
2278	struct ucc_fast_info *uf_info;
2279	int length;
2280	u16 i, j;
2281	u8 __iomem *bd;
2282
2283	ug_info = ugeth->ug_info;
2284	uf_info = &ug_info->uf_info;
2285
2286	/* Allocate Rx bds */
2287	for (j = 0; j < ug_info->numQueuesRx; j++) {
2288		length = ug_info->bdRingLenRx[j] * sizeof(struct qe_bd);
2289		if (uf_info->bd_mem_part == MEM_PART_SYSTEM) {
2290			u32 align = 4;
2291			if (UCC_GETH_RX_BD_RING_ALIGNMENT > 4)
2292				align = UCC_GETH_RX_BD_RING_ALIGNMENT;
2293			ugeth->rx_bd_ring_offset[j] =
2294				(u32) kmalloc((u32) (length + align), GFP_KERNEL);
2295			if (ugeth->rx_bd_ring_offset[j] != 0)
2296				ugeth->p_rx_bd_ring[j] =
2297					(u8 __iomem *)((ugeth->rx_bd_ring_offset[j] +
2298					align) & ~(align - 1));
2299		} else if (uf_info->bd_mem_part == MEM_PART_MURAM) {
2300			ugeth->rx_bd_ring_offset[j] =
2301			    qe_muram_alloc(length,
2302					   UCC_GETH_RX_BD_RING_ALIGNMENT);
2303			if (!IS_ERR_VALUE(ugeth->rx_bd_ring_offset[j]))
2304				ugeth->p_rx_bd_ring[j] =
2305				    (u8 __iomem *) qe_muram_addr(ugeth->
2306							 rx_bd_ring_offset[j]);
2307		}
2308		if (!ugeth->p_rx_bd_ring[j]) {
2309			if (netif_msg_ifup(ugeth))
2310				pr_err("Can not allocate memory for Rx bd rings\n");
2311			return -ENOMEM;
2312		}
2313	}
2314
2315	/* Init Rx bds */
2316	for (j = 0; j < ug_info->numQueuesRx; j++) {
2317		/* Setup the skbuff rings */
2318		ugeth->rx_skbuff[j] =
2319			kmalloc_array(ugeth->ug_info->bdRingLenRx[j],
2320				      sizeof(struct sk_buff *), GFP_KERNEL);
2321
2322		if (ugeth->rx_skbuff[j] == NULL) {
2323			if (netif_msg_ifup(ugeth))
2324				pr_err("Could not allocate rx_skbuff\n");
2325			return -ENOMEM;
2326		}
2327
2328		for (i = 0; i < ugeth->ug_info->bdRingLenRx[j]; i++)
2329			ugeth->rx_skbuff[j][i] = NULL;
2330
2331		ugeth->skb_currx[j] = 0;
2332		bd = ugeth->rxBd[j] = ugeth->p_rx_bd_ring[j];
2333		for (i = 0; i < ug_info->bdRingLenRx[j]; i++) {
2334			/* set bd status and length */
2335			out_be32((u32 __iomem *)bd, R_I);
2336			/* clear bd buffer */
2337			out_be32(&((struct qe_bd __iomem *)bd)->buf, 0);
2338			bd += sizeof(struct qe_bd);
2339		}
2340		bd -= sizeof(struct qe_bd);
2341		/* set bd status and length */
2342		out_be32((u32 __iomem *)bd, R_W); /* for last BD set Wrap bit */
2343	}
2344
2345	return 0;
2346}
2347
2348static int ucc_geth_startup(struct ucc_geth_private *ugeth)
2349{
2350	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
2351	struct ucc_geth_init_pram __iomem *p_init_enet_pram;
2352	struct ucc_fast_private *uccf;
2353	struct ucc_geth_info *ug_info;
2354	struct ucc_fast_info *uf_info;
2355	struct ucc_fast __iomem *uf_regs;
2356	struct ucc_geth __iomem *ug_regs;
2357	int ret_val = -EINVAL;
2358	u32 remoder = UCC_GETH_REMODER_INIT;
2359	u32 init_enet_pram_offset, cecr_subblock, command;
2360	u32 ifstat, i, j, size, l2qt, l3qt;
2361	u16 temoder = UCC_GETH_TEMODER_INIT;
2362	u16 test;
2363	u8 function_code = 0;
2364	u8 __iomem *endOfRing;
2365	u8 numThreadsRxNumerical, numThreadsTxNumerical;
2366
2367	ugeth_vdbg("%s: IN", __func__);
2368	uccf = ugeth->uccf;
2369	ug_info = ugeth->ug_info;
2370	uf_info = &ug_info->uf_info;
2371	uf_regs = uccf->uf_regs;
2372	ug_regs = ugeth->ug_regs;
2373
2374	switch (ug_info->numThreadsRx) {
2375	case UCC_GETH_NUM_OF_THREADS_1:
2376		numThreadsRxNumerical = 1;
2377		break;
2378	case UCC_GETH_NUM_OF_THREADS_2:
2379		numThreadsRxNumerical = 2;
2380		break;
2381	case UCC_GETH_NUM_OF_THREADS_4:
2382		numThreadsRxNumerical = 4;
2383		break;
2384	case UCC_GETH_NUM_OF_THREADS_6:
2385		numThreadsRxNumerical = 6;
2386		break;
2387	case UCC_GETH_NUM_OF_THREADS_8:
2388		numThreadsRxNumerical = 8;
2389		break;
2390	default:
2391		if (netif_msg_ifup(ugeth))
2392			pr_err("Bad number of Rx threads value\n");
2393		return -EINVAL;
 
2394	}
2395
2396	switch (ug_info->numThreadsTx) {
2397	case UCC_GETH_NUM_OF_THREADS_1:
2398		numThreadsTxNumerical = 1;
2399		break;
2400	case UCC_GETH_NUM_OF_THREADS_2:
2401		numThreadsTxNumerical = 2;
2402		break;
2403	case UCC_GETH_NUM_OF_THREADS_4:
2404		numThreadsTxNumerical = 4;
2405		break;
2406	case UCC_GETH_NUM_OF_THREADS_6:
2407		numThreadsTxNumerical = 6;
2408		break;
2409	case UCC_GETH_NUM_OF_THREADS_8:
2410		numThreadsTxNumerical = 8;
2411		break;
2412	default:
2413		if (netif_msg_ifup(ugeth))
2414			pr_err("Bad number of Tx threads value\n");
2415		return -EINVAL;
 
2416	}
2417
2418	/* Calculate rx_extended_features */
2419	ugeth->rx_non_dynamic_extended_features = ug_info->ipCheckSumCheck ||
2420	    ug_info->ipAddressAlignment ||
2421	    (ug_info->numStationAddresses !=
2422	     UCC_GETH_NUM_OF_STATION_ADDRESSES_1);
2423
2424	ugeth->rx_extended_features = ugeth->rx_non_dynamic_extended_features ||
2425		(ug_info->vlanOperationTagged != UCC_GETH_VLAN_OPERATION_TAGGED_NOP) ||
2426		(ug_info->vlanOperationNonTagged !=
2427		 UCC_GETH_VLAN_OPERATION_NON_TAGGED_NOP);
2428
2429	init_default_reg_vals(&uf_regs->upsmr,
2430			      &ug_regs->maccfg1, &ug_regs->maccfg2);
2431
2432	/*                    Set UPSMR                      */
2433	/* For more details see the hardware spec.           */
2434	init_rx_parameters(ug_info->bro,
2435			   ug_info->rsh, ug_info->pro, &uf_regs->upsmr);
2436
2437	/* We're going to ignore other registers for now, */
2438	/* except as needed to get up and running         */
2439
2440	/*                    Set MACCFG1                    */
2441	/* For more details see the hardware spec.           */
2442	init_flow_control_params(ug_info->aufc,
2443				 ug_info->receiveFlowControl,
2444				 ug_info->transmitFlowControl,
2445				 ug_info->pausePeriod,
2446				 ug_info->extensionField,
2447				 &uf_regs->upsmr,
2448				 &ug_regs->uempr, &ug_regs->maccfg1);
2449
2450	setbits32(&ug_regs->maccfg1, MACCFG1_ENABLE_RX | MACCFG1_ENABLE_TX);
2451
2452	/*                    Set IPGIFG                     */
2453	/* For more details see the hardware spec.           */
2454	ret_val = init_inter_frame_gap_params(ug_info->nonBackToBackIfgPart1,
2455					      ug_info->nonBackToBackIfgPart2,
2456					      ug_info->
2457					      miminumInterFrameGapEnforcement,
2458					      ug_info->backToBackInterFrameGap,
2459					      &ug_regs->ipgifg);
2460	if (ret_val != 0) {
2461		if (netif_msg_ifup(ugeth))
2462			pr_err("IPGIFG initialization parameter too large\n");
2463		return ret_val;
2464	}
2465
2466	/*                    Set HAFDUP                     */
2467	/* For more details see the hardware spec.           */
2468	ret_val = init_half_duplex_params(ug_info->altBeb,
2469					  ug_info->backPressureNoBackoff,
2470					  ug_info->noBackoff,
2471					  ug_info->excessDefer,
2472					  ug_info->altBebTruncation,
2473					  ug_info->maxRetransmission,
2474					  ug_info->collisionWindow,
2475					  &ug_regs->hafdup);
2476	if (ret_val != 0) {
2477		if (netif_msg_ifup(ugeth))
2478			pr_err("Half Duplex initialization parameter too large\n");
2479		return ret_val;
2480	}
2481
2482	/*                    Set IFSTAT                     */
2483	/* For more details see the hardware spec.           */
2484	/* Read only - resets upon read                      */
2485	ifstat = in_be32(&ug_regs->ifstat);
2486
2487	/*                    Clear UEMPR                    */
2488	/* For more details see the hardware spec.           */
2489	out_be32(&ug_regs->uempr, 0);
2490
2491	/*                    Set UESCR                      */
2492	/* For more details see the hardware spec.           */
2493	init_hw_statistics_gathering_mode((ug_info->statisticsMode &
2494				UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE),
2495				0, &uf_regs->upsmr, &ug_regs->uescr);
2496
2497	ret_val = ucc_geth_alloc_tx(ugeth);
2498	if (ret_val != 0)
2499		return ret_val;
2500
2501	ret_val = ucc_geth_alloc_rx(ugeth);
2502	if (ret_val != 0)
2503		return ret_val;
2504
2505	/*
2506	 * Global PRAM
2507	 */
2508	/* Tx global PRAM */
2509	/* Allocate global tx parameter RAM page */
2510	ugeth->tx_glbl_pram_offset =
2511	    qe_muram_alloc(sizeof(struct ucc_geth_tx_global_pram),
2512			   UCC_GETH_TX_GLOBAL_PRAM_ALIGNMENT);
2513	if (IS_ERR_VALUE(ugeth->tx_glbl_pram_offset)) {
2514		if (netif_msg_ifup(ugeth))
2515			pr_err("Can not allocate DPRAM memory for p_tx_glbl_pram\n");
2516		return -ENOMEM;
2517	}
2518	ugeth->p_tx_glbl_pram =
2519	    (struct ucc_geth_tx_global_pram __iomem *) qe_muram_addr(ugeth->
2520							tx_glbl_pram_offset);
2521	/* Zero out p_tx_glbl_pram */
2522	memset_io((void __iomem *)ugeth->p_tx_glbl_pram, 0, sizeof(struct ucc_geth_tx_global_pram));
2523
2524	/* Fill global PRAM */
2525
2526	/* TQPTR */
2527	/* Size varies with number of Tx threads */
2528	ugeth->thread_dat_tx_offset =
2529	    qe_muram_alloc(numThreadsTxNumerical *
2530			   sizeof(struct ucc_geth_thread_data_tx) +
2531			   32 * (numThreadsTxNumerical == 1),
2532			   UCC_GETH_THREAD_DATA_ALIGNMENT);
2533	if (IS_ERR_VALUE(ugeth->thread_dat_tx_offset)) {
2534		if (netif_msg_ifup(ugeth))
2535			pr_err("Can not allocate DPRAM memory for p_thread_data_tx\n");
2536		return -ENOMEM;
2537	}
2538
2539	ugeth->p_thread_data_tx =
2540	    (struct ucc_geth_thread_data_tx __iomem *) qe_muram_addr(ugeth->
2541							thread_dat_tx_offset);
2542	out_be32(&ugeth->p_tx_glbl_pram->tqptr, ugeth->thread_dat_tx_offset);
2543
2544	/* vtagtable */
2545	for (i = 0; i < UCC_GETH_TX_VTAG_TABLE_ENTRY_MAX; i++)
2546		out_be32(&ugeth->p_tx_glbl_pram->vtagtable[i],
2547			 ug_info->vtagtable[i]);
2548
2549	/* iphoffset */
2550	for (i = 0; i < TX_IP_OFFSET_ENTRY_MAX; i++)
2551		out_8(&ugeth->p_tx_glbl_pram->iphoffset[i],
2552				ug_info->iphoffset[i]);
2553
2554	/* SQPTR */
2555	/* Size varies with number of Tx queues */
2556	ugeth->send_q_mem_reg_offset =
2557	    qe_muram_alloc(ug_info->numQueuesTx *
2558			   sizeof(struct ucc_geth_send_queue_qd),
2559			   UCC_GETH_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
2560	if (IS_ERR_VALUE(ugeth->send_q_mem_reg_offset)) {
2561		if (netif_msg_ifup(ugeth))
2562			pr_err("Can not allocate DPRAM memory for p_send_q_mem_reg\n");
2563		return -ENOMEM;
2564	}
2565
2566	ugeth->p_send_q_mem_reg =
2567	    (struct ucc_geth_send_queue_mem_region __iomem *) qe_muram_addr(ugeth->
2568			send_q_mem_reg_offset);
2569	out_be32(&ugeth->p_tx_glbl_pram->sqptr, ugeth->send_q_mem_reg_offset);
2570
2571	/* Setup the table */
2572	/* Assume BD rings are already established */
2573	for (i = 0; i < ug_info->numQueuesTx; i++) {
2574		endOfRing =
2575		    ugeth->p_tx_bd_ring[i] + (ug_info->bdRingLenTx[i] -
2576					      1) * sizeof(struct qe_bd);
2577		if (ugeth->ug_info->uf_info.bd_mem_part == MEM_PART_SYSTEM) {
2578			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].bd_ring_base,
2579				 (u32) virt_to_phys(ugeth->p_tx_bd_ring[i]));
2580			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].
2581				 last_bd_completed_address,
2582				 (u32) virt_to_phys(endOfRing));
2583		} else if (ugeth->ug_info->uf_info.bd_mem_part ==
2584			   MEM_PART_MURAM) {
2585			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].bd_ring_base,
2586				 (u32)qe_muram_dma(ugeth->p_tx_bd_ring[i]));
 
2587			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].
2588				 last_bd_completed_address,
2589				 (u32)qe_muram_dma(endOfRing));
2590		}
2591	}
2592
2593	/* schedulerbasepointer */
2594
2595	if (ug_info->numQueuesTx > 1) {
2596	/* scheduler exists only if more than 1 tx queue */
2597		ugeth->scheduler_offset =
2598		    qe_muram_alloc(sizeof(struct ucc_geth_scheduler),
2599				   UCC_GETH_SCHEDULER_ALIGNMENT);
2600		if (IS_ERR_VALUE(ugeth->scheduler_offset)) {
2601			if (netif_msg_ifup(ugeth))
2602				pr_err("Can not allocate DPRAM memory for p_scheduler\n");
2603			return -ENOMEM;
2604		}
2605
2606		ugeth->p_scheduler =
2607		    (struct ucc_geth_scheduler __iomem *) qe_muram_addr(ugeth->
2608							   scheduler_offset);
2609		out_be32(&ugeth->p_tx_glbl_pram->schedulerbasepointer,
2610			 ugeth->scheduler_offset);
2611		/* Zero out p_scheduler */
2612		memset_io((void __iomem *)ugeth->p_scheduler, 0, sizeof(struct ucc_geth_scheduler));
2613
2614		/* Set values in scheduler */
2615		out_be32(&ugeth->p_scheduler->mblinterval,
2616			 ug_info->mblinterval);
2617		out_be16(&ugeth->p_scheduler->nortsrbytetime,
2618			 ug_info->nortsrbytetime);
2619		out_8(&ugeth->p_scheduler->fracsiz, ug_info->fracsiz);
2620		out_8(&ugeth->p_scheduler->strictpriorityq,
2621				ug_info->strictpriorityq);
2622		out_8(&ugeth->p_scheduler->txasap, ug_info->txasap);
2623		out_8(&ugeth->p_scheduler->extrabw, ug_info->extrabw);
2624		for (i = 0; i < NUM_TX_QUEUES; i++)
2625			out_8(&ugeth->p_scheduler->weightfactor[i],
2626			    ug_info->weightfactor[i]);
2627
2628		/* Set pointers to cpucount registers in scheduler */
2629		ugeth->p_cpucount[0] = &(ugeth->p_scheduler->cpucount0);
2630		ugeth->p_cpucount[1] = &(ugeth->p_scheduler->cpucount1);
2631		ugeth->p_cpucount[2] = &(ugeth->p_scheduler->cpucount2);
2632		ugeth->p_cpucount[3] = &(ugeth->p_scheduler->cpucount3);
2633		ugeth->p_cpucount[4] = &(ugeth->p_scheduler->cpucount4);
2634		ugeth->p_cpucount[5] = &(ugeth->p_scheduler->cpucount5);
2635		ugeth->p_cpucount[6] = &(ugeth->p_scheduler->cpucount6);
2636		ugeth->p_cpucount[7] = &(ugeth->p_scheduler->cpucount7);
2637	}
2638
2639	/* schedulerbasepointer */
2640	/* TxRMON_PTR (statistics) */
2641	if (ug_info->
2642	    statisticsMode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
2643		ugeth->tx_fw_statistics_pram_offset =
2644		    qe_muram_alloc(sizeof
2645				   (struct ucc_geth_tx_firmware_statistics_pram),
2646				   UCC_GETH_TX_STATISTICS_ALIGNMENT);
2647		if (IS_ERR_VALUE(ugeth->tx_fw_statistics_pram_offset)) {
2648			if (netif_msg_ifup(ugeth))
2649				pr_err("Can not allocate DPRAM memory for p_tx_fw_statistics_pram\n");
2650			return -ENOMEM;
2651		}
2652		ugeth->p_tx_fw_statistics_pram =
2653		    (struct ucc_geth_tx_firmware_statistics_pram __iomem *)
2654		    qe_muram_addr(ugeth->tx_fw_statistics_pram_offset);
2655		/* Zero out p_tx_fw_statistics_pram */
2656		memset_io((void __iomem *)ugeth->p_tx_fw_statistics_pram,
2657		       0, sizeof(struct ucc_geth_tx_firmware_statistics_pram));
2658	}
2659
2660	/* temoder */
2661	/* Already has speed set */
2662
2663	if (ug_info->numQueuesTx > 1)
2664		temoder |= TEMODER_SCHEDULER_ENABLE;
2665	if (ug_info->ipCheckSumGenerate)
2666		temoder |= TEMODER_IP_CHECKSUM_GENERATE;
2667	temoder |= ((ug_info->numQueuesTx - 1) << TEMODER_NUM_OF_QUEUES_SHIFT);
2668	out_be16(&ugeth->p_tx_glbl_pram->temoder, temoder);
2669
2670	test = in_be16(&ugeth->p_tx_glbl_pram->temoder);
2671
2672	/* Function code register value to be used later */
2673	function_code = UCC_BMR_BO_BE | UCC_BMR_GBL;
2674	/* Required for QE */
2675
2676	/* function code register */
2677	out_be32(&ugeth->p_tx_glbl_pram->tstate, ((u32) function_code) << 24);
2678
2679	/* Rx global PRAM */
2680	/* Allocate global rx parameter RAM page */
2681	ugeth->rx_glbl_pram_offset =
2682	    qe_muram_alloc(sizeof(struct ucc_geth_rx_global_pram),
2683			   UCC_GETH_RX_GLOBAL_PRAM_ALIGNMENT);
2684	if (IS_ERR_VALUE(ugeth->rx_glbl_pram_offset)) {
2685		if (netif_msg_ifup(ugeth))
2686			pr_err("Can not allocate DPRAM memory for p_rx_glbl_pram\n");
2687		return -ENOMEM;
2688	}
2689	ugeth->p_rx_glbl_pram =
2690	    (struct ucc_geth_rx_global_pram __iomem *) qe_muram_addr(ugeth->
2691							rx_glbl_pram_offset);
2692	/* Zero out p_rx_glbl_pram */
2693	memset_io((void __iomem *)ugeth->p_rx_glbl_pram, 0, sizeof(struct ucc_geth_rx_global_pram));
2694
2695	/* Fill global PRAM */
2696
2697	/* RQPTR */
2698	/* Size varies with number of Rx threads */
2699	ugeth->thread_dat_rx_offset =
2700	    qe_muram_alloc(numThreadsRxNumerical *
2701			   sizeof(struct ucc_geth_thread_data_rx),
2702			   UCC_GETH_THREAD_DATA_ALIGNMENT);
2703	if (IS_ERR_VALUE(ugeth->thread_dat_rx_offset)) {
2704		if (netif_msg_ifup(ugeth))
2705			pr_err("Can not allocate DPRAM memory for p_thread_data_rx\n");
2706		return -ENOMEM;
2707	}
2708
2709	ugeth->p_thread_data_rx =
2710	    (struct ucc_geth_thread_data_rx __iomem *) qe_muram_addr(ugeth->
2711							thread_dat_rx_offset);
2712	out_be32(&ugeth->p_rx_glbl_pram->rqptr, ugeth->thread_dat_rx_offset);
2713
2714	/* typeorlen */
2715	out_be16(&ugeth->p_rx_glbl_pram->typeorlen, ug_info->typeorlen);
2716
2717	/* rxrmonbaseptr (statistics) */
2718	if (ug_info->
2719	    statisticsMode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
2720		ugeth->rx_fw_statistics_pram_offset =
2721		    qe_muram_alloc(sizeof
2722				   (struct ucc_geth_rx_firmware_statistics_pram),
2723				   UCC_GETH_RX_STATISTICS_ALIGNMENT);
2724		if (IS_ERR_VALUE(ugeth->rx_fw_statistics_pram_offset)) {
2725			if (netif_msg_ifup(ugeth))
2726				pr_err("Can not allocate DPRAM memory for p_rx_fw_statistics_pram\n");
2727			return -ENOMEM;
2728		}
2729		ugeth->p_rx_fw_statistics_pram =
2730		    (struct ucc_geth_rx_firmware_statistics_pram __iomem *)
2731		    qe_muram_addr(ugeth->rx_fw_statistics_pram_offset);
2732		/* Zero out p_rx_fw_statistics_pram */
2733		memset_io((void __iomem *)ugeth->p_rx_fw_statistics_pram, 0,
2734		       sizeof(struct ucc_geth_rx_firmware_statistics_pram));
2735	}
2736
2737	/* intCoalescingPtr */
2738
2739	/* Size varies with number of Rx queues */
2740	ugeth->rx_irq_coalescing_tbl_offset =
2741	    qe_muram_alloc(ug_info->numQueuesRx *
2742			   sizeof(struct ucc_geth_rx_interrupt_coalescing_entry)
2743			   + 4, UCC_GETH_RX_INTERRUPT_COALESCING_ALIGNMENT);
2744	if (IS_ERR_VALUE(ugeth->rx_irq_coalescing_tbl_offset)) {
2745		if (netif_msg_ifup(ugeth))
2746			pr_err("Can not allocate DPRAM memory for p_rx_irq_coalescing_tbl\n");
2747		return -ENOMEM;
2748	}
2749
2750	ugeth->p_rx_irq_coalescing_tbl =
2751	    (struct ucc_geth_rx_interrupt_coalescing_table __iomem *)
2752	    qe_muram_addr(ugeth->rx_irq_coalescing_tbl_offset);
2753	out_be32(&ugeth->p_rx_glbl_pram->intcoalescingptr,
2754		 ugeth->rx_irq_coalescing_tbl_offset);
2755
2756	/* Fill interrupt coalescing table */
2757	for (i = 0; i < ug_info->numQueuesRx; i++) {
2758		out_be32(&ugeth->p_rx_irq_coalescing_tbl->coalescingentry[i].
2759			 interruptcoalescingmaxvalue,
2760			 ug_info->interruptcoalescingmaxvalue[i]);
2761		out_be32(&ugeth->p_rx_irq_coalescing_tbl->coalescingentry[i].
2762			 interruptcoalescingcounter,
2763			 ug_info->interruptcoalescingmaxvalue[i]);
2764	}
2765
2766	/* MRBLR */
2767	init_max_rx_buff_len(uf_info->max_rx_buf_length,
2768			     &ugeth->p_rx_glbl_pram->mrblr);
2769	/* MFLR */
2770	out_be16(&ugeth->p_rx_glbl_pram->mflr, ug_info->maxFrameLength);
2771	/* MINFLR */
2772	init_min_frame_len(ug_info->minFrameLength,
2773			   &ugeth->p_rx_glbl_pram->minflr,
2774			   &ugeth->p_rx_glbl_pram->mrblr);
2775	/* MAXD1 */
2776	out_be16(&ugeth->p_rx_glbl_pram->maxd1, ug_info->maxD1Length);
2777	/* MAXD2 */
2778	out_be16(&ugeth->p_rx_glbl_pram->maxd2, ug_info->maxD2Length);
2779
2780	/* l2qt */
2781	l2qt = 0;
2782	for (i = 0; i < UCC_GETH_VLAN_PRIORITY_MAX; i++)
2783		l2qt |= (ug_info->l2qt[i] << (28 - 4 * i));
2784	out_be32(&ugeth->p_rx_glbl_pram->l2qt, l2qt);
2785
2786	/* l3qt */
2787	for (j = 0; j < UCC_GETH_IP_PRIORITY_MAX; j += 8) {
2788		l3qt = 0;
2789		for (i = 0; i < 8; i++)
2790			l3qt |= (ug_info->l3qt[j + i] << (28 - 4 * i));
2791		out_be32(&ugeth->p_rx_glbl_pram->l3qt[j/8], l3qt);
2792	}
2793
2794	/* vlantype */
2795	out_be16(&ugeth->p_rx_glbl_pram->vlantype, ug_info->vlantype);
2796
2797	/* vlantci */
2798	out_be16(&ugeth->p_rx_glbl_pram->vlantci, ug_info->vlantci);
2799
2800	/* ecamptr */
2801	out_be32(&ugeth->p_rx_glbl_pram->ecamptr, ug_info->ecamptr);
2802
2803	/* RBDQPTR */
2804	/* Size varies with number of Rx queues */
2805	ugeth->rx_bd_qs_tbl_offset =
2806	    qe_muram_alloc(ug_info->numQueuesRx *
2807			   (sizeof(struct ucc_geth_rx_bd_queues_entry) +
2808			    sizeof(struct ucc_geth_rx_prefetched_bds)),
2809			   UCC_GETH_RX_BD_QUEUES_ALIGNMENT);
2810	if (IS_ERR_VALUE(ugeth->rx_bd_qs_tbl_offset)) {
2811		if (netif_msg_ifup(ugeth))
2812			pr_err("Can not allocate DPRAM memory for p_rx_bd_qs_tbl\n");
2813		return -ENOMEM;
2814	}
2815
2816	ugeth->p_rx_bd_qs_tbl =
2817	    (struct ucc_geth_rx_bd_queues_entry __iomem *) qe_muram_addr(ugeth->
2818				    rx_bd_qs_tbl_offset);
2819	out_be32(&ugeth->p_rx_glbl_pram->rbdqptr, ugeth->rx_bd_qs_tbl_offset);
2820	/* Zero out p_rx_bd_qs_tbl */
2821	memset_io((void __iomem *)ugeth->p_rx_bd_qs_tbl,
2822	       0,
2823	       ug_info->numQueuesRx * (sizeof(struct ucc_geth_rx_bd_queues_entry) +
2824				       sizeof(struct ucc_geth_rx_prefetched_bds)));
2825
2826	/* Setup the table */
2827	/* Assume BD rings are already established */
2828	for (i = 0; i < ug_info->numQueuesRx; i++) {
2829		if (ugeth->ug_info->uf_info.bd_mem_part == MEM_PART_SYSTEM) {
2830			out_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
2831				 (u32) virt_to_phys(ugeth->p_rx_bd_ring[i]));
2832		} else if (ugeth->ug_info->uf_info.bd_mem_part ==
2833			   MEM_PART_MURAM) {
2834			out_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
2835				 (u32)qe_muram_dma(ugeth->p_rx_bd_ring[i]));
 
2836		}
2837		/* rest of fields handled by QE */
2838	}
2839
2840	/* remoder */
2841	/* Already has speed set */
2842
2843	if (ugeth->rx_extended_features)
2844		remoder |= REMODER_RX_EXTENDED_FEATURES;
2845	if (ug_info->rxExtendedFiltering)
2846		remoder |= REMODER_RX_EXTENDED_FILTERING;
2847	if (ug_info->dynamicMaxFrameLength)
2848		remoder |= REMODER_DYNAMIC_MAX_FRAME_LENGTH;
2849	if (ug_info->dynamicMinFrameLength)
2850		remoder |= REMODER_DYNAMIC_MIN_FRAME_LENGTH;
2851	remoder |=
2852	    ug_info->vlanOperationTagged << REMODER_VLAN_OPERATION_TAGGED_SHIFT;
2853	remoder |=
2854	    ug_info->
2855	    vlanOperationNonTagged << REMODER_VLAN_OPERATION_NON_TAGGED_SHIFT;
2856	remoder |= ug_info->rxQoSMode << REMODER_RX_QOS_MODE_SHIFT;
2857	remoder |= ((ug_info->numQueuesRx - 1) << REMODER_NUM_OF_QUEUES_SHIFT);
2858	if (ug_info->ipCheckSumCheck)
2859		remoder |= REMODER_IP_CHECKSUM_CHECK;
2860	if (ug_info->ipAddressAlignment)
2861		remoder |= REMODER_IP_ADDRESS_ALIGNMENT;
2862	out_be32(&ugeth->p_rx_glbl_pram->remoder, remoder);
2863
2864	/* Note that this function must be called */
2865	/* ONLY AFTER p_tx_fw_statistics_pram */
2866	/* andp_UccGethRxFirmwareStatisticsPram are allocated ! */
2867	init_firmware_statistics_gathering_mode((ug_info->
2868		statisticsMode &
2869		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX),
2870		(ug_info->statisticsMode &
2871		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX),
2872		&ugeth->p_tx_glbl_pram->txrmonbaseptr,
2873		ugeth->tx_fw_statistics_pram_offset,
2874		&ugeth->p_rx_glbl_pram->rxrmonbaseptr,
2875		ugeth->rx_fw_statistics_pram_offset,
2876		&ugeth->p_tx_glbl_pram->temoder,
2877		&ugeth->p_rx_glbl_pram->remoder);
2878
2879	/* function code register */
2880	out_8(&ugeth->p_rx_glbl_pram->rstate, function_code);
2881
2882	/* initialize extended filtering */
2883	if (ug_info->rxExtendedFiltering) {
2884		if (!ug_info->extendedFilteringChainPointer) {
2885			if (netif_msg_ifup(ugeth))
2886				pr_err("Null Extended Filtering Chain Pointer\n");
2887			return -EINVAL;
2888		}
2889
2890		/* Allocate memory for extended filtering Mode Global
2891		Parameters */
2892		ugeth->exf_glbl_param_offset =
2893		    qe_muram_alloc(sizeof(struct ucc_geth_exf_global_pram),
2894		UCC_GETH_RX_EXTENDED_FILTERING_GLOBAL_PARAMETERS_ALIGNMENT);
2895		if (IS_ERR_VALUE(ugeth->exf_glbl_param_offset)) {
2896			if (netif_msg_ifup(ugeth))
2897				pr_err("Can not allocate DPRAM memory for p_exf_glbl_param\n");
2898			return -ENOMEM;
2899		}
2900
2901		ugeth->p_exf_glbl_param =
2902		    (struct ucc_geth_exf_global_pram __iomem *) qe_muram_addr(ugeth->
2903				 exf_glbl_param_offset);
2904		out_be32(&ugeth->p_rx_glbl_pram->exfGlobalParam,
2905			 ugeth->exf_glbl_param_offset);
2906		out_be32(&ugeth->p_exf_glbl_param->l2pcdptr,
2907			 (u32) ug_info->extendedFilteringChainPointer);
2908
2909	} else {		/* initialize 82xx style address filtering */
2910
2911		/* Init individual address recognition registers to disabled */
2912
2913		for (j = 0; j < NUM_OF_PADDRS; j++)
2914			ugeth_82xx_filtering_clear_addr_in_paddr(ugeth, (u8) j);
2915
2916		p_82xx_addr_filt =
2917		    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->
2918		    p_rx_glbl_pram->addressfiltering;
2919
2920		ugeth_82xx_filtering_clear_all_addr_in_hash(ugeth,
2921			ENET_ADDR_TYPE_GROUP);
2922		ugeth_82xx_filtering_clear_all_addr_in_hash(ugeth,
2923			ENET_ADDR_TYPE_INDIVIDUAL);
2924	}
2925
2926	/*
2927	 * Initialize UCC at QE level
2928	 */
2929
2930	command = QE_INIT_TX_RX;
2931
2932	/* Allocate shadow InitEnet command parameter structure.
2933	 * This is needed because after the InitEnet command is executed,
2934	 * the structure in DPRAM is released, because DPRAM is a premium
2935	 * resource.
2936	 * This shadow structure keeps a copy of what was done so that the
2937	 * allocated resources can be released when the channel is freed.
2938	 */
2939	if (!(ugeth->p_init_enet_param_shadow =
2940	      kmalloc(sizeof(struct ucc_geth_init_pram), GFP_KERNEL))) {
2941		if (netif_msg_ifup(ugeth))
2942			pr_err("Can not allocate memory for p_UccInitEnetParamShadows\n");
2943		return -ENOMEM;
2944	}
2945	/* Zero out *p_init_enet_param_shadow */
2946	memset((char *)ugeth->p_init_enet_param_shadow,
2947	       0, sizeof(struct ucc_geth_init_pram));
2948
2949	/* Fill shadow InitEnet command parameter structure */
2950
2951	ugeth->p_init_enet_param_shadow->resinit1 =
2952	    ENET_INIT_PARAM_MAGIC_RES_INIT1;
2953	ugeth->p_init_enet_param_shadow->resinit2 =
2954	    ENET_INIT_PARAM_MAGIC_RES_INIT2;
2955	ugeth->p_init_enet_param_shadow->resinit3 =
2956	    ENET_INIT_PARAM_MAGIC_RES_INIT3;
2957	ugeth->p_init_enet_param_shadow->resinit4 =
2958	    ENET_INIT_PARAM_MAGIC_RES_INIT4;
2959	ugeth->p_init_enet_param_shadow->resinit5 =
2960	    ENET_INIT_PARAM_MAGIC_RES_INIT5;
2961	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
2962	    ((u32) ug_info->numThreadsRx) << ENET_INIT_PARAM_RGF_SHIFT;
2963	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
2964	    ((u32) ug_info->numThreadsTx) << ENET_INIT_PARAM_TGF_SHIFT;
2965
2966	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
2967	    ugeth->rx_glbl_pram_offset | ug_info->riscRx;
2968	if ((ug_info->largestexternallookupkeysize !=
2969	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_NONE) &&
2970	    (ug_info->largestexternallookupkeysize !=
2971	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_8_BYTES) &&
2972	    (ug_info->largestexternallookupkeysize !=
2973	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_16_BYTES)) {
2974		if (netif_msg_ifup(ugeth))
2975			pr_err("Invalid largest External Lookup Key Size\n");
2976		return -EINVAL;
2977	}
2978	ugeth->p_init_enet_param_shadow->largestexternallookupkeysize =
2979	    ug_info->largestexternallookupkeysize;
2980	size = sizeof(struct ucc_geth_thread_rx_pram);
2981	if (ug_info->rxExtendedFiltering) {
2982		size += THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING;
2983		if (ug_info->largestexternallookupkeysize ==
2984		    QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_8_BYTES)
2985			size +=
2986			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_8;
2987		if (ug_info->largestexternallookupkeysize ==
2988		    QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_16_BYTES)
2989			size +=
2990			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_16;
2991	}
2992
2993	if ((ret_val = fill_init_enet_entries(ugeth, &(ugeth->
2994		p_init_enet_param_shadow->rxthread[0]),
2995		(u8) (numThreadsRxNumerical + 1)
2996		/* Rx needs one extra for terminator */
2997		, size, UCC_GETH_THREAD_RX_PRAM_ALIGNMENT,
2998		ug_info->riscRx, 1)) != 0) {
2999		if (netif_msg_ifup(ugeth))
3000			pr_err("Can not fill p_init_enet_param_shadow\n");
3001		return ret_val;
3002	}
3003
3004	ugeth->p_init_enet_param_shadow->txglobal =
3005	    ugeth->tx_glbl_pram_offset | ug_info->riscTx;
3006	if ((ret_val =
3007	     fill_init_enet_entries(ugeth,
3008				    &(ugeth->p_init_enet_param_shadow->
3009				      txthread[0]), numThreadsTxNumerical,
3010				    sizeof(struct ucc_geth_thread_tx_pram),
3011				    UCC_GETH_THREAD_TX_PRAM_ALIGNMENT,
3012				    ug_info->riscTx, 0)) != 0) {
3013		if (netif_msg_ifup(ugeth))
3014			pr_err("Can not fill p_init_enet_param_shadow\n");
3015		return ret_val;
3016	}
3017
3018	/* Load Rx bds with buffers */
3019	for (i = 0; i < ug_info->numQueuesRx; i++) {
3020		if ((ret_val = rx_bd_buffer_set(ugeth, (u8) i)) != 0) {
3021			if (netif_msg_ifup(ugeth))
3022				pr_err("Can not fill Rx bds with buffers\n");
3023			return ret_val;
3024		}
3025	}
3026
3027	/* Allocate InitEnet command parameter structure */
3028	init_enet_pram_offset = qe_muram_alloc(sizeof(struct ucc_geth_init_pram), 4);
3029	if (IS_ERR_VALUE(init_enet_pram_offset)) {
3030		if (netif_msg_ifup(ugeth))
3031			pr_err("Can not allocate DPRAM memory for p_init_enet_pram\n");
3032		return -ENOMEM;
3033	}
3034	p_init_enet_pram =
3035	    (struct ucc_geth_init_pram __iomem *) qe_muram_addr(init_enet_pram_offset);
3036
3037	/* Copy shadow InitEnet command parameter structure into PRAM */
3038	out_8(&p_init_enet_pram->resinit1,
3039			ugeth->p_init_enet_param_shadow->resinit1);
3040	out_8(&p_init_enet_pram->resinit2,
3041			ugeth->p_init_enet_param_shadow->resinit2);
3042	out_8(&p_init_enet_pram->resinit3,
3043			ugeth->p_init_enet_param_shadow->resinit3);
3044	out_8(&p_init_enet_pram->resinit4,
3045			ugeth->p_init_enet_param_shadow->resinit4);
3046	out_be16(&p_init_enet_pram->resinit5,
3047		 ugeth->p_init_enet_param_shadow->resinit5);
3048	out_8(&p_init_enet_pram->largestexternallookupkeysize,
3049	    ugeth->p_init_enet_param_shadow->largestexternallookupkeysize);
3050	out_be32(&p_init_enet_pram->rgftgfrxglobal,
3051		 ugeth->p_init_enet_param_shadow->rgftgfrxglobal);
3052	for (i = 0; i < ENET_INIT_PARAM_MAX_ENTRIES_RX; i++)
3053		out_be32(&p_init_enet_pram->rxthread[i],
3054			 ugeth->p_init_enet_param_shadow->rxthread[i]);
3055	out_be32(&p_init_enet_pram->txglobal,
3056		 ugeth->p_init_enet_param_shadow->txglobal);
3057	for (i = 0; i < ENET_INIT_PARAM_MAX_ENTRIES_TX; i++)
3058		out_be32(&p_init_enet_pram->txthread[i],
3059			 ugeth->p_init_enet_param_shadow->txthread[i]);
3060
3061	/* Issue QE command */
3062	cecr_subblock =
3063	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
3064	qe_issue_cmd(command, cecr_subblock, QE_CR_PROTOCOL_ETHERNET,
3065		     init_enet_pram_offset);
3066
3067	/* Free InitEnet command parameter */
3068	qe_muram_free(init_enet_pram_offset);
3069
3070	return 0;
3071}
3072
3073/* This is called by the kernel when a frame is ready for transmission. */
3074/* It is pointed to by the dev->hard_start_xmit function pointer */
3075static netdev_tx_t
3076ucc_geth_start_xmit(struct sk_buff *skb, struct net_device *dev)
3077{
3078	struct ucc_geth_private *ugeth = netdev_priv(dev);
3079#ifdef CONFIG_UGETH_TX_ON_DEMAND
3080	struct ucc_fast_private *uccf;
3081#endif
3082	u8 __iomem *bd;			/* BD pointer */
3083	u32 bd_status;
3084	u8 txQ = 0;
3085	unsigned long flags;
3086
3087	ugeth_vdbg("%s: IN", __func__);
3088
3089	netdev_sent_queue(dev, skb->len);
3090	spin_lock_irqsave(&ugeth->lock, flags);
3091
3092	dev->stats.tx_bytes += skb->len;
3093
3094	/* Start from the next BD that should be filled */
3095	bd = ugeth->txBd[txQ];
3096	bd_status = in_be32((u32 __iomem *)bd);
3097	/* Save the skb pointer so we can free it later */
3098	ugeth->tx_skbuff[txQ][ugeth->skb_curtx[txQ]] = skb;
3099
3100	/* Update the current skb pointer (wrapping if this was the last) */
3101	ugeth->skb_curtx[txQ] =
3102	    (ugeth->skb_curtx[txQ] +
3103	     1) & TX_RING_MOD_MASK(ugeth->ug_info->bdRingLenTx[txQ]);
3104
3105	/* set up the buffer descriptor */
3106	out_be32(&((struct qe_bd __iomem *)bd)->buf,
3107		      dma_map_single(ugeth->dev, skb->data,
3108			      skb->len, DMA_TO_DEVICE));
3109
3110	/* printk(KERN_DEBUG"skb->data is 0x%x\n",skb->data); */
3111
3112	bd_status = (bd_status & T_W) | T_R | T_I | T_L | skb->len;
3113
3114	/* set bd status and length */
3115	out_be32((u32 __iomem *)bd, bd_status);
3116
3117	/* Move to next BD in the ring */
3118	if (!(bd_status & T_W))
3119		bd += sizeof(struct qe_bd);
3120	else
3121		bd = ugeth->p_tx_bd_ring[txQ];
3122
3123	/* If the next BD still needs to be cleaned up, then the bds
3124	   are full.  We need to tell the kernel to stop sending us stuff. */
3125	if (bd == ugeth->confBd[txQ]) {
3126		if (!netif_queue_stopped(dev))
3127			netif_stop_queue(dev);
3128	}
3129
3130	ugeth->txBd[txQ] = bd;
3131
3132	skb_tx_timestamp(skb);
3133
3134	if (ugeth->p_scheduler) {
3135		ugeth->cpucount[txQ]++;
3136		/* Indicate to QE that there are more Tx bds ready for
3137		transmission */
3138		/* This is done by writing a running counter of the bd
3139		count to the scheduler PRAM. */
3140		out_be16(ugeth->p_cpucount[txQ], ugeth->cpucount[txQ]);
3141	}
3142
3143#ifdef CONFIG_UGETH_TX_ON_DEMAND
3144	uccf = ugeth->uccf;
3145	out_be16(uccf->p_utodr, UCC_FAST_TOD);
3146#endif
3147	spin_unlock_irqrestore(&ugeth->lock, flags);
3148
3149	return NETDEV_TX_OK;
3150}
3151
3152static int ucc_geth_rx(struct ucc_geth_private *ugeth, u8 rxQ, int rx_work_limit)
3153{
3154	struct sk_buff *skb;
3155	u8 __iomem *bd;
3156	u16 length, howmany = 0;
3157	u32 bd_status;
3158	u8 *bdBuffer;
3159	struct net_device *dev;
3160
3161	ugeth_vdbg("%s: IN", __func__);
3162
3163	dev = ugeth->ndev;
3164
3165	/* collect received buffers */
3166	bd = ugeth->rxBd[rxQ];
3167
3168	bd_status = in_be32((u32 __iomem *)bd);
3169
3170	/* while there are received buffers and BD is full (~R_E) */
3171	while (!((bd_status & (R_E)) || (--rx_work_limit < 0))) {
3172		bdBuffer = (u8 *) in_be32(&((struct qe_bd __iomem *)bd)->buf);
3173		length = (u16) ((bd_status & BD_LENGTH_MASK) - 4);
3174		skb = ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]];
3175
3176		/* determine whether buffer is first, last, first and last
3177		(single buffer frame) or middle (not first and not last) */
3178		if (!skb ||
3179		    (!(bd_status & (R_F | R_L))) ||
3180		    (bd_status & R_ERRORS_FATAL)) {
3181			if (netif_msg_rx_err(ugeth))
3182				pr_err("%d: ERROR!!! skb - 0x%08x\n",
3183				       __LINE__, (u32)skb);
3184			dev_kfree_skb(skb);
3185
3186			ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]] = NULL;
3187			dev->stats.rx_dropped++;
3188		} else {
3189			dev->stats.rx_packets++;
3190			howmany++;
3191
3192			/* Prep the skb for the packet */
3193			skb_put(skb, length);
3194
3195			/* Tell the skb what kind of packet this is */
3196			skb->protocol = eth_type_trans(skb, ugeth->ndev);
3197
3198			dev->stats.rx_bytes += length;
3199			/* Send the packet up the stack */
3200			netif_receive_skb(skb);
3201		}
3202
3203		skb = get_new_skb(ugeth, bd);
3204		if (!skb) {
3205			if (netif_msg_rx_err(ugeth))
3206				pr_warn("No Rx Data Buffer\n");
3207			dev->stats.rx_dropped++;
3208			break;
3209		}
3210
3211		ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]] = skb;
3212
3213		/* update to point at the next skb */
3214		ugeth->skb_currx[rxQ] =
3215		    (ugeth->skb_currx[rxQ] +
3216		     1) & RX_RING_MOD_MASK(ugeth->ug_info->bdRingLenRx[rxQ]);
3217
3218		if (bd_status & R_W)
3219			bd = ugeth->p_rx_bd_ring[rxQ];
3220		else
3221			bd += sizeof(struct qe_bd);
3222
3223		bd_status = in_be32((u32 __iomem *)bd);
3224	}
3225
3226	ugeth->rxBd[rxQ] = bd;
3227	return howmany;
3228}
3229
3230static int ucc_geth_tx(struct net_device *dev, u8 txQ)
3231{
3232	/* Start from the next BD that should be filled */
3233	struct ucc_geth_private *ugeth = netdev_priv(dev);
3234	unsigned int bytes_sent = 0;
3235	int howmany = 0;
3236	u8 __iomem *bd;		/* BD pointer */
3237	u32 bd_status;
3238
3239	bd = ugeth->confBd[txQ];
3240	bd_status = in_be32((u32 __iomem *)bd);
3241
3242	/* Normal processing. */
3243	while ((bd_status & T_R) == 0) {
3244		struct sk_buff *skb;
3245
3246		/* BD contains already transmitted buffer.   */
3247		/* Handle the transmitted buffer and release */
3248		/* the BD to be used with the current frame  */
3249
3250		skb = ugeth->tx_skbuff[txQ][ugeth->skb_dirtytx[txQ]];
3251		if (!skb)
3252			break;
3253		howmany++;
3254		bytes_sent += skb->len;
3255		dev->stats.tx_packets++;
3256
3257		dev_consume_skb_any(skb);
3258
3259		ugeth->tx_skbuff[txQ][ugeth->skb_dirtytx[txQ]] = NULL;
3260		ugeth->skb_dirtytx[txQ] =
3261		    (ugeth->skb_dirtytx[txQ] +
3262		     1) & TX_RING_MOD_MASK(ugeth->ug_info->bdRingLenTx[txQ]);
3263
3264		/* We freed a buffer, so now we can restart transmission */
3265		if (netif_queue_stopped(dev))
3266			netif_wake_queue(dev);
3267
3268		/* Advance the confirmation BD pointer */
3269		if (!(bd_status & T_W))
3270			bd += sizeof(struct qe_bd);
3271		else
3272			bd = ugeth->p_tx_bd_ring[txQ];
3273		bd_status = in_be32((u32 __iomem *)bd);
3274	}
3275	ugeth->confBd[txQ] = bd;
3276	netdev_completed_queue(dev, howmany, bytes_sent);
3277	return 0;
3278}
3279
3280static int ucc_geth_poll(struct napi_struct *napi, int budget)
3281{
3282	struct ucc_geth_private *ugeth = container_of(napi, struct ucc_geth_private, napi);
3283	struct ucc_geth_info *ug_info;
3284	int howmany, i;
3285
3286	ug_info = ugeth->ug_info;
3287
3288	/* Tx event processing */
3289	spin_lock(&ugeth->lock);
3290	for (i = 0; i < ug_info->numQueuesTx; i++)
3291		ucc_geth_tx(ugeth->ndev, i);
3292	spin_unlock(&ugeth->lock);
3293
3294	howmany = 0;
3295	for (i = 0; i < ug_info->numQueuesRx; i++)
3296		howmany += ucc_geth_rx(ugeth, i, budget - howmany);
3297
3298	if (howmany < budget) {
3299		napi_complete_done(napi, howmany);
3300		setbits32(ugeth->uccf->p_uccm, UCCE_RX_EVENTS | UCCE_TX_EVENTS);
3301	}
3302
3303	return howmany;
3304}
3305
3306static irqreturn_t ucc_geth_irq_handler(int irq, void *info)
3307{
3308	struct net_device *dev = info;
3309	struct ucc_geth_private *ugeth = netdev_priv(dev);
3310	struct ucc_fast_private *uccf;
3311	struct ucc_geth_info *ug_info;
3312	register u32 ucce;
3313	register u32 uccm;
3314
3315	ugeth_vdbg("%s: IN", __func__);
3316
3317	uccf = ugeth->uccf;
3318	ug_info = ugeth->ug_info;
3319
3320	/* read and clear events */
3321	ucce = (u32) in_be32(uccf->p_ucce);
3322	uccm = (u32) in_be32(uccf->p_uccm);
3323	ucce &= uccm;
3324	out_be32(uccf->p_ucce, ucce);
3325
3326	/* check for receive events that require processing */
3327	if (ucce & (UCCE_RX_EVENTS | UCCE_TX_EVENTS)) {
3328		if (napi_schedule_prep(&ugeth->napi)) {
3329			uccm &= ~(UCCE_RX_EVENTS | UCCE_TX_EVENTS);
3330			out_be32(uccf->p_uccm, uccm);
3331			__napi_schedule(&ugeth->napi);
3332		}
3333	}
3334
3335	/* Errors and other events */
3336	if (ucce & UCCE_OTHER) {
3337		if (ucce & UCC_GETH_UCCE_BSY)
3338			dev->stats.rx_errors++;
3339		if (ucce & UCC_GETH_UCCE_TXE)
3340			dev->stats.tx_errors++;
3341	}
3342
3343	return IRQ_HANDLED;
3344}
3345
3346#ifdef CONFIG_NET_POLL_CONTROLLER
3347/*
3348 * Polling 'interrupt' - used by things like netconsole to send skbs
3349 * without having to re-enable interrupts. It's not called while
3350 * the interrupt routine is executing.
3351 */
3352static void ucc_netpoll(struct net_device *dev)
3353{
3354	struct ucc_geth_private *ugeth = netdev_priv(dev);
3355	int irq = ugeth->ug_info->uf_info.irq;
3356
3357	disable_irq(irq);
3358	ucc_geth_irq_handler(irq, dev);
3359	enable_irq(irq);
3360}
3361#endif /* CONFIG_NET_POLL_CONTROLLER */
3362
3363static int ucc_geth_set_mac_addr(struct net_device *dev, void *p)
3364{
3365	struct ucc_geth_private *ugeth = netdev_priv(dev);
3366	struct sockaddr *addr = p;
3367
3368	if (!is_valid_ether_addr(addr->sa_data))
3369		return -EADDRNOTAVAIL;
3370
3371	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
3372
3373	/*
3374	 * If device is not running, we will set mac addr register
3375	 * when opening the device.
3376	 */
3377	if (!netif_running(dev))
3378		return 0;
3379
3380	spin_lock_irq(&ugeth->lock);
3381	init_mac_station_addr_regs(dev->dev_addr[0],
3382				   dev->dev_addr[1],
3383				   dev->dev_addr[2],
3384				   dev->dev_addr[3],
3385				   dev->dev_addr[4],
3386				   dev->dev_addr[5],
3387				   &ugeth->ug_regs->macstnaddr1,
3388				   &ugeth->ug_regs->macstnaddr2);
3389	spin_unlock_irq(&ugeth->lock);
3390
3391	return 0;
3392}
3393
3394static int ucc_geth_init_mac(struct ucc_geth_private *ugeth)
3395{
3396	struct net_device *dev = ugeth->ndev;
3397	int err;
3398
3399	err = ucc_struct_init(ugeth);
3400	if (err) {
3401		netif_err(ugeth, ifup, dev, "Cannot configure internal struct, aborting\n");
3402		goto err;
3403	}
3404
3405	err = ucc_geth_startup(ugeth);
3406	if (err) {
3407		netif_err(ugeth, ifup, dev, "Cannot configure net device, aborting\n");
3408		goto err;
3409	}
3410
3411	err = adjust_enet_interface(ugeth);
3412	if (err) {
3413		netif_err(ugeth, ifup, dev, "Cannot configure net device, aborting\n");
3414		goto err;
3415	}
3416
3417	/*       Set MACSTNADDR1, MACSTNADDR2                */
3418	/* For more details see the hardware spec.           */
3419	init_mac_station_addr_regs(dev->dev_addr[0],
3420				   dev->dev_addr[1],
3421				   dev->dev_addr[2],
3422				   dev->dev_addr[3],
3423				   dev->dev_addr[4],
3424				   dev->dev_addr[5],
3425				   &ugeth->ug_regs->macstnaddr1,
3426				   &ugeth->ug_regs->macstnaddr2);
3427
3428	err = ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
3429	if (err) {
3430		netif_err(ugeth, ifup, dev, "Cannot enable net device, aborting\n");
3431		goto err;
3432	}
3433
3434	return 0;
3435err:
3436	ucc_geth_stop(ugeth);
3437	return err;
3438}
3439
3440/* Called when something needs to use the ethernet device */
3441/* Returns 0 for success. */
3442static int ucc_geth_open(struct net_device *dev)
3443{
3444	struct ucc_geth_private *ugeth = netdev_priv(dev);
3445	int err;
3446
3447	ugeth_vdbg("%s: IN", __func__);
3448
3449	/* Test station address */
3450	if (dev->dev_addr[0] & ENET_GROUP_ADDR) {
3451		netif_err(ugeth, ifup, dev,
3452			  "Multicast address used for station address - is this what you wanted?\n");
3453		return -EINVAL;
3454	}
3455
3456	err = init_phy(dev);
3457	if (err) {
3458		netif_err(ugeth, ifup, dev, "Cannot initialize PHY, aborting\n");
3459		return err;
3460	}
3461
3462	err = ucc_geth_init_mac(ugeth);
3463	if (err) {
3464		netif_err(ugeth, ifup, dev, "Cannot initialize MAC, aborting\n");
3465		goto err;
3466	}
3467
3468	err = request_irq(ugeth->ug_info->uf_info.irq, ucc_geth_irq_handler,
3469			  0, "UCC Geth", dev);
3470	if (err) {
3471		netif_err(ugeth, ifup, dev, "Cannot get IRQ for net device, aborting\n");
3472		goto err;
3473	}
3474
3475	phy_start(ugeth->phydev);
3476	napi_enable(&ugeth->napi);
3477	netdev_reset_queue(dev);
3478	netif_start_queue(dev);
3479
3480	device_set_wakeup_capable(&dev->dev,
3481			qe_alive_during_sleep() || ugeth->phydev->irq);
3482	device_set_wakeup_enable(&dev->dev, ugeth->wol_en);
3483
3484	return err;
3485
3486err:
3487	ucc_geth_stop(ugeth);
3488	return err;
3489}
3490
3491/* Stops the kernel queue, and halts the controller */
3492static int ucc_geth_close(struct net_device *dev)
3493{
3494	struct ucc_geth_private *ugeth = netdev_priv(dev);
3495
3496	ugeth_vdbg("%s: IN", __func__);
3497
3498	napi_disable(&ugeth->napi);
3499
3500	cancel_work_sync(&ugeth->timeout_work);
3501	ucc_geth_stop(ugeth);
3502	phy_disconnect(ugeth->phydev);
3503	ugeth->phydev = NULL;
3504
3505	free_irq(ugeth->ug_info->uf_info.irq, ugeth->ndev);
3506
3507	netif_stop_queue(dev);
3508	netdev_reset_queue(dev);
3509
3510	return 0;
3511}
3512
3513/* Reopen device. This will reset the MAC and PHY. */
3514static void ucc_geth_timeout_work(struct work_struct *work)
3515{
3516	struct ucc_geth_private *ugeth;
3517	struct net_device *dev;
3518
3519	ugeth = container_of(work, struct ucc_geth_private, timeout_work);
3520	dev = ugeth->ndev;
3521
3522	ugeth_vdbg("%s: IN", __func__);
3523
3524	dev->stats.tx_errors++;
3525
3526	ugeth_dump_regs(ugeth);
3527
3528	if (dev->flags & IFF_UP) {
3529		/*
3530		 * Must reset MAC *and* PHY. This is done by reopening
3531		 * the device.
3532		 */
3533		netif_tx_stop_all_queues(dev);
3534		ucc_geth_stop(ugeth);
3535		ucc_geth_init_mac(ugeth);
3536		/* Must start PHY here */
3537		phy_start(ugeth->phydev);
3538		netif_tx_start_all_queues(dev);
3539	}
3540
3541	netif_tx_schedule_all(dev);
3542}
3543
3544/*
3545 * ucc_geth_timeout gets called when a packet has not been
3546 * transmitted after a set amount of time.
3547 */
3548static void ucc_geth_timeout(struct net_device *dev)
3549{
3550	struct ucc_geth_private *ugeth = netdev_priv(dev);
3551
3552	schedule_work(&ugeth->timeout_work);
3553}
3554
3555
3556#ifdef CONFIG_PM
3557
3558static int ucc_geth_suspend(struct platform_device *ofdev, pm_message_t state)
3559{
3560	struct net_device *ndev = platform_get_drvdata(ofdev);
3561	struct ucc_geth_private *ugeth = netdev_priv(ndev);
3562
3563	if (!netif_running(ndev))
3564		return 0;
3565
3566	netif_device_detach(ndev);
3567	napi_disable(&ugeth->napi);
3568
3569	/*
3570	 * Disable the controller, otherwise we'll wakeup on any network
3571	 * activity.
3572	 */
3573	ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);
3574
3575	if (ugeth->wol_en & WAKE_MAGIC) {
3576		setbits32(ugeth->uccf->p_uccm, UCC_GETH_UCCE_MPD);
3577		setbits32(&ugeth->ug_regs->maccfg2, MACCFG2_MPE);
3578		ucc_fast_enable(ugeth->uccf, COMM_DIR_RX_AND_TX);
3579	} else if (!(ugeth->wol_en & WAKE_PHY)) {
3580		phy_stop(ugeth->phydev);
3581	}
3582
3583	return 0;
3584}
3585
3586static int ucc_geth_resume(struct platform_device *ofdev)
3587{
3588	struct net_device *ndev = platform_get_drvdata(ofdev);
3589	struct ucc_geth_private *ugeth = netdev_priv(ndev);
3590	int err;
3591
3592	if (!netif_running(ndev))
3593		return 0;
3594
3595	if (qe_alive_during_sleep()) {
3596		if (ugeth->wol_en & WAKE_MAGIC) {
3597			ucc_fast_disable(ugeth->uccf, COMM_DIR_RX_AND_TX);
3598			clrbits32(&ugeth->ug_regs->maccfg2, MACCFG2_MPE);
3599			clrbits32(ugeth->uccf->p_uccm, UCC_GETH_UCCE_MPD);
3600		}
3601		ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
3602	} else {
3603		/*
3604		 * Full reinitialization is required if QE shuts down
3605		 * during sleep.
3606		 */
3607		ucc_geth_memclean(ugeth);
3608
3609		err = ucc_geth_init_mac(ugeth);
3610		if (err) {
3611			netdev_err(ndev, "Cannot initialize MAC, aborting\n");
3612			return err;
3613		}
3614	}
3615
3616	ugeth->oldlink = 0;
3617	ugeth->oldspeed = 0;
3618	ugeth->oldduplex = -1;
3619
3620	phy_stop(ugeth->phydev);
3621	phy_start(ugeth->phydev);
3622
3623	napi_enable(&ugeth->napi);
3624	netif_device_attach(ndev);
3625
3626	return 0;
3627}
3628
3629#else
3630#define ucc_geth_suspend NULL
3631#define ucc_geth_resume NULL
3632#endif
3633
3634static phy_interface_t to_phy_interface(const char *phy_connection_type)
3635{
3636	if (strcasecmp(phy_connection_type, "mii") == 0)
3637		return PHY_INTERFACE_MODE_MII;
3638	if (strcasecmp(phy_connection_type, "gmii") == 0)
3639		return PHY_INTERFACE_MODE_GMII;
3640	if (strcasecmp(phy_connection_type, "tbi") == 0)
3641		return PHY_INTERFACE_MODE_TBI;
3642	if (strcasecmp(phy_connection_type, "rmii") == 0)
3643		return PHY_INTERFACE_MODE_RMII;
3644	if (strcasecmp(phy_connection_type, "rgmii") == 0)
3645		return PHY_INTERFACE_MODE_RGMII;
3646	if (strcasecmp(phy_connection_type, "rgmii-id") == 0)
3647		return PHY_INTERFACE_MODE_RGMII_ID;
3648	if (strcasecmp(phy_connection_type, "rgmii-txid") == 0)
3649		return PHY_INTERFACE_MODE_RGMII_TXID;
3650	if (strcasecmp(phy_connection_type, "rgmii-rxid") == 0)
3651		return PHY_INTERFACE_MODE_RGMII_RXID;
3652	if (strcasecmp(phy_connection_type, "rtbi") == 0)
3653		return PHY_INTERFACE_MODE_RTBI;
3654	if (strcasecmp(phy_connection_type, "sgmii") == 0)
3655		return PHY_INTERFACE_MODE_SGMII;
3656
3657	return PHY_INTERFACE_MODE_MII;
3658}
3659
3660static int ucc_geth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
3661{
3662	struct ucc_geth_private *ugeth = netdev_priv(dev);
3663
3664	if (!netif_running(dev))
3665		return -EINVAL;
3666
3667	if (!ugeth->phydev)
3668		return -ENODEV;
3669
3670	return phy_mii_ioctl(ugeth->phydev, rq, cmd);
3671}
3672
3673static const struct net_device_ops ucc_geth_netdev_ops = {
3674	.ndo_open		= ucc_geth_open,
3675	.ndo_stop		= ucc_geth_close,
3676	.ndo_start_xmit		= ucc_geth_start_xmit,
3677	.ndo_validate_addr	= eth_validate_addr,
3678	.ndo_change_carrier     = fixed_phy_change_carrier,
3679	.ndo_set_mac_address	= ucc_geth_set_mac_addr,
 
3680	.ndo_set_rx_mode	= ucc_geth_set_multi,
3681	.ndo_tx_timeout		= ucc_geth_timeout,
3682	.ndo_do_ioctl		= ucc_geth_ioctl,
3683#ifdef CONFIG_NET_POLL_CONTROLLER
3684	.ndo_poll_controller	= ucc_netpoll,
3685#endif
3686};
3687
3688static int ucc_geth_probe(struct platform_device* ofdev)
3689{
3690	struct device *device = &ofdev->dev;
3691	struct device_node *np = ofdev->dev.of_node;
3692	struct net_device *dev = NULL;
3693	struct ucc_geth_private *ugeth = NULL;
3694	struct ucc_geth_info *ug_info;
3695	struct resource res;
3696	int err, ucc_num, max_speed = 0;
3697	const unsigned int *prop;
3698	const char *sprop;
3699	const void *mac_addr;
3700	phy_interface_t phy_interface;
3701	static const int enet_to_speed[] = {
3702		SPEED_10, SPEED_10, SPEED_10,
3703		SPEED_100, SPEED_100, SPEED_100,
3704		SPEED_1000, SPEED_1000, SPEED_1000, SPEED_1000,
3705	};
3706	static const phy_interface_t enet_to_phy_interface[] = {
3707		PHY_INTERFACE_MODE_MII, PHY_INTERFACE_MODE_RMII,
3708		PHY_INTERFACE_MODE_RGMII, PHY_INTERFACE_MODE_MII,
3709		PHY_INTERFACE_MODE_RMII, PHY_INTERFACE_MODE_RGMII,
3710		PHY_INTERFACE_MODE_GMII, PHY_INTERFACE_MODE_RGMII,
3711		PHY_INTERFACE_MODE_TBI, PHY_INTERFACE_MODE_RTBI,
3712		PHY_INTERFACE_MODE_SGMII,
3713	};
3714
3715	ugeth_vdbg("%s: IN", __func__);
3716
3717	prop = of_get_property(np, "cell-index", NULL);
3718	if (!prop) {
3719		prop = of_get_property(np, "device-id", NULL);
3720		if (!prop)
3721			return -ENODEV;
3722	}
3723
3724	ucc_num = *prop - 1;
3725	if ((ucc_num < 0) || (ucc_num > 7))
3726		return -ENODEV;
3727
3728	ug_info = &ugeth_info[ucc_num];
3729	if (ug_info == NULL) {
3730		if (netif_msg_probe(&debug))
3731			pr_err("[%d] Missing additional data!\n", ucc_num);
3732		return -ENODEV;
3733	}
3734
3735	ug_info->uf_info.ucc_num = ucc_num;
3736
3737	sprop = of_get_property(np, "rx-clock-name", NULL);
3738	if (sprop) {
3739		ug_info->uf_info.rx_clock = qe_clock_source(sprop);
3740		if ((ug_info->uf_info.rx_clock < QE_CLK_NONE) ||
3741		    (ug_info->uf_info.rx_clock > QE_CLK24)) {
3742			pr_err("invalid rx-clock-name property\n");
3743			return -EINVAL;
3744		}
3745	} else {
3746		prop = of_get_property(np, "rx-clock", NULL);
3747		if (!prop) {
3748			/* If both rx-clock-name and rx-clock are missing,
3749			   we want to tell people to use rx-clock-name. */
3750			pr_err("missing rx-clock-name property\n");
3751			return -EINVAL;
3752		}
3753		if ((*prop < QE_CLK_NONE) || (*prop > QE_CLK24)) {
3754			pr_err("invalid rx-clock property\n");
3755			return -EINVAL;
3756		}
3757		ug_info->uf_info.rx_clock = *prop;
3758	}
3759
3760	sprop = of_get_property(np, "tx-clock-name", NULL);
3761	if (sprop) {
3762		ug_info->uf_info.tx_clock = qe_clock_source(sprop);
3763		if ((ug_info->uf_info.tx_clock < QE_CLK_NONE) ||
3764		    (ug_info->uf_info.tx_clock > QE_CLK24)) {
3765			pr_err("invalid tx-clock-name property\n");
3766			return -EINVAL;
3767		}
3768	} else {
3769		prop = of_get_property(np, "tx-clock", NULL);
3770		if (!prop) {
3771			pr_err("missing tx-clock-name property\n");
3772			return -EINVAL;
3773		}
3774		if ((*prop < QE_CLK_NONE) || (*prop > QE_CLK24)) {
3775			pr_err("invalid tx-clock property\n");
3776			return -EINVAL;
3777		}
3778		ug_info->uf_info.tx_clock = *prop;
3779	}
3780
3781	err = of_address_to_resource(np, 0, &res);
3782	if (err)
3783		return -EINVAL;
3784
3785	ug_info->uf_info.regs = res.start;
3786	ug_info->uf_info.irq = irq_of_parse_and_map(np, 0);
3787
3788	ug_info->phy_node = of_parse_phandle(np, "phy-handle", 0);
3789	if (!ug_info->phy_node && of_phy_is_fixed_link(np)) {
3790		/*
3791		 * In the case of a fixed PHY, the DT node associated
3792		 * to the PHY is the Ethernet MAC DT node.
3793		 */
3794		err = of_phy_register_fixed_link(np);
3795		if (err)
3796			return err;
3797		ug_info->phy_node = of_node_get(np);
3798	}
3799
3800	/* Find the TBI PHY node.  If it's not there, we don't support SGMII */
3801	ug_info->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
3802
3803	/* get the phy interface type, or default to MII */
3804	prop = of_get_property(np, "phy-connection-type", NULL);
3805	if (!prop) {
3806		/* handle interface property present in old trees */
3807		prop = of_get_property(ug_info->phy_node, "interface", NULL);
3808		if (prop != NULL) {
3809			phy_interface = enet_to_phy_interface[*prop];
3810			max_speed = enet_to_speed[*prop];
3811		} else
3812			phy_interface = PHY_INTERFACE_MODE_MII;
3813	} else {
3814		phy_interface = to_phy_interface((const char *)prop);
3815	}
3816
3817	/* get speed, or derive from PHY interface */
3818	if (max_speed == 0)
3819		switch (phy_interface) {
3820		case PHY_INTERFACE_MODE_GMII:
3821		case PHY_INTERFACE_MODE_RGMII:
3822		case PHY_INTERFACE_MODE_RGMII_ID:
3823		case PHY_INTERFACE_MODE_RGMII_RXID:
3824		case PHY_INTERFACE_MODE_RGMII_TXID:
3825		case PHY_INTERFACE_MODE_TBI:
3826		case PHY_INTERFACE_MODE_RTBI:
3827		case PHY_INTERFACE_MODE_SGMII:
3828			max_speed = SPEED_1000;
3829			break;
3830		default:
3831			max_speed = SPEED_100;
3832			break;
3833		}
3834
3835	if (max_speed == SPEED_1000) {
3836		unsigned int snums = qe_get_num_of_snums();
3837
3838		/* configure muram FIFOs for gigabit operation */
3839		ug_info->uf_info.urfs = UCC_GETH_URFS_GIGA_INIT;
3840		ug_info->uf_info.urfet = UCC_GETH_URFET_GIGA_INIT;
3841		ug_info->uf_info.urfset = UCC_GETH_URFSET_GIGA_INIT;
3842		ug_info->uf_info.utfs = UCC_GETH_UTFS_GIGA_INIT;
3843		ug_info->uf_info.utfet = UCC_GETH_UTFET_GIGA_INIT;
3844		ug_info->uf_info.utftt = UCC_GETH_UTFTT_GIGA_INIT;
3845		ug_info->numThreadsTx = UCC_GETH_NUM_OF_THREADS_4;
3846
3847		/* If QE's snum number is 46/76 which means we need to support
3848		 * 4 UECs at 1000Base-T simultaneously, we need to allocate
3849		 * more Threads to Rx.
3850		 */
3851		if ((snums == 76) || (snums == 46))
3852			ug_info->numThreadsRx = UCC_GETH_NUM_OF_THREADS_6;
3853		else
3854			ug_info->numThreadsRx = UCC_GETH_NUM_OF_THREADS_4;
3855	}
3856
3857	if (netif_msg_probe(&debug))
3858		pr_info("UCC%1d at 0x%8llx (irq = %d)\n",
3859			ug_info->uf_info.ucc_num + 1,
3860			(u64)ug_info->uf_info.regs,
3861			ug_info->uf_info.irq);
3862
3863	/* Create an ethernet device instance */
3864	dev = alloc_etherdev(sizeof(*ugeth));
3865
3866	if (dev == NULL) {
3867		err = -ENOMEM;
3868		goto err_deregister_fixed_link;
3869	}
3870
3871	ugeth = netdev_priv(dev);
3872	spin_lock_init(&ugeth->lock);
3873
3874	/* Create CQs for hash tables */
3875	INIT_LIST_HEAD(&ugeth->group_hash_q);
3876	INIT_LIST_HEAD(&ugeth->ind_hash_q);
3877
3878	dev_set_drvdata(device, dev);
3879
3880	/* Set the dev->base_addr to the gfar reg region */
3881	dev->base_addr = (unsigned long)(ug_info->uf_info.regs);
3882
3883	SET_NETDEV_DEV(dev, device);
3884
3885	/* Fill in the dev structure */
3886	uec_set_ethtool_ops(dev);
3887	dev->netdev_ops = &ucc_geth_netdev_ops;
3888	dev->watchdog_timeo = TX_TIMEOUT;
3889	INIT_WORK(&ugeth->timeout_work, ucc_geth_timeout_work);
3890	netif_napi_add(dev, &ugeth->napi, ucc_geth_poll, 64);
3891	dev->mtu = 1500;
3892
3893	ugeth->msg_enable = netif_msg_init(debug.msg_enable, UGETH_MSG_DEFAULT);
3894	ugeth->phy_interface = phy_interface;
3895	ugeth->max_speed = max_speed;
3896
3897	/* Carrier starts down, phylib will bring it up */
3898	netif_carrier_off(dev);
3899
3900	err = register_netdev(dev);
3901	if (err) {
3902		if (netif_msg_probe(ugeth))
3903			pr_err("%s: Cannot register net device, aborting\n",
3904			       dev->name);
3905		goto err_free_netdev;
 
3906	}
3907
3908	mac_addr = of_get_mac_address(np);
3909	if (!IS_ERR(mac_addr))
3910		ether_addr_copy(dev->dev_addr, mac_addr);
3911
3912	ugeth->ug_info = ug_info;
3913	ugeth->dev = device;
3914	ugeth->ndev = dev;
3915	ugeth->node = np;
3916
3917	return 0;
3918
3919err_free_netdev:
3920	free_netdev(dev);
3921err_deregister_fixed_link:
3922	if (of_phy_is_fixed_link(np))
3923		of_phy_deregister_fixed_link(np);
3924	of_node_put(ug_info->tbi_node);
3925	of_node_put(ug_info->phy_node);
3926
3927	return err;
3928}
3929
3930static int ucc_geth_remove(struct platform_device* ofdev)
3931{
3932	struct net_device *dev = platform_get_drvdata(ofdev);
3933	struct ucc_geth_private *ugeth = netdev_priv(dev);
3934	struct device_node *np = ofdev->dev.of_node;
3935
3936	unregister_netdev(dev);
3937	free_netdev(dev);
3938	ucc_geth_memclean(ugeth);
3939	if (of_phy_is_fixed_link(np))
3940		of_phy_deregister_fixed_link(np);
3941	of_node_put(ugeth->ug_info->tbi_node);
3942	of_node_put(ugeth->ug_info->phy_node);
3943
3944	return 0;
3945}
3946
3947static const struct of_device_id ucc_geth_match[] = {
3948	{
3949		.type = "network",
3950		.compatible = "ucc_geth",
3951	},
3952	{},
3953};
3954
3955MODULE_DEVICE_TABLE(of, ucc_geth_match);
3956
3957static struct platform_driver ucc_geth_driver = {
3958	.driver = {
3959		.name = DRV_NAME,
 
3960		.of_match_table = ucc_geth_match,
3961	},
3962	.probe		= ucc_geth_probe,
3963	.remove		= ucc_geth_remove,
3964	.suspend	= ucc_geth_suspend,
3965	.resume		= ucc_geth_resume,
3966};
3967
3968static int __init ucc_geth_init(void)
3969{
3970	int i, ret;
3971
3972	if (netif_msg_drv(&debug))
3973		pr_info(DRV_DESC "\n");
3974	for (i = 0; i < 8; i++)
3975		memcpy(&(ugeth_info[i]), &ugeth_primary_info,
3976		       sizeof(ugeth_primary_info));
3977
3978	ret = platform_driver_register(&ucc_geth_driver);
3979
3980	return ret;
3981}
3982
3983static void __exit ucc_geth_exit(void)
3984{
3985	platform_driver_unregister(&ucc_geth_driver);
3986}
3987
3988module_init(ucc_geth_init);
3989module_exit(ucc_geth_exit);
3990
3991MODULE_AUTHOR("Freescale Semiconductor, Inc");
3992MODULE_DESCRIPTION(DRV_DESC);
3993MODULE_VERSION(DRV_VERSION);
3994MODULE_LICENSE("GPL");