ucc_geth.c 116.1 KB
Newer Older
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 9 10
 *
 * Description:
 * QE UCC Gigabit Ethernet Driver
 */
11 12 13

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

14 15 16 17 18
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/errno.h>
#include <linux/slab.h>
#include <linux/stddef.h>
19
#include <linux/module.h>
20 21 22 23 24 25 26 27
#include <linux/interrupt.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/spinlock.h>
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#include <linux/mii.h>
28
#include <linux/phy.h>
29
#include <linux/phy_fixed.h>
30
#include <linux/workqueue.h>
31 32
#include <linux/of_address.h>
#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 39
#include <asm/irq.h>
#include <asm/io.h>
40 41 42 43
#include <soc/fsl/qe/immap_qe.h>
#include <soc/fsl/qe/qe.h>
#include <soc/fsl/qe/ucc.h>
#include <soc/fsl/qe/ucc_fast.h>
44
#include <asm/machdep.h>
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

#include "ucc_geth.h"

#undef DEBUG

#define ugeth_printk(level, format, arg...)  \
        printk(level format "\n", ## arg)

#define ugeth_dbg(format, arg...)            \
        ugeth_printk(KERN_DEBUG , format , ## arg)

#ifdef UGETH_VERBOSE_DEBUG
#define ugeth_vdbg ugeth_dbg
#else
#define ugeth_vdbg(fmt, args...) do { } while (0)
#endif				/* UGETH_VERBOSE_DEBUG */
61
#define UGETH_MSG_DEFAULT	(NETIF_MSG_IFUP << 1 ) - 1
62

63

64 65
static DEFINE_SPINLOCK(ugeth_lock);

66 67 68 69 70 71 72
static struct {
	u32 msg_enable;
} debug = { -1 };

module_param_named(debug, debug.msg_enable, int, 0);
MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 0xffff=all)");

73
static struct ucc_geth_info ugeth_primary_info = {
74 75 76 77
	.uf_info = {
		    .bd_mem_part = MEM_PART_SYSTEM,
		    .rtsm = UCC_FAST_SEND_IDLES_BETWEEN_FRAMES,
		    .max_rx_buf_length = 1536,
78
		    /* adjusted at startup if max-speed 1000 */
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
		    .urfs = UCC_GETH_URFS_INIT,
		    .urfet = UCC_GETH_URFET_INIT,
		    .urfset = UCC_GETH_URFSET_INIT,
		    .utfs = UCC_GETH_UTFS_INIT,
		    .utfet = UCC_GETH_UTFET_INIT,
		    .utftt = UCC_GETH_UTFTT_INIT,
		    .ufpt = 256,
		    .mode = UCC_FAST_PROTOCOL_MODE_ETHERNET,
		    .ttx_trx = UCC_FAST_GUMR_TRANSPARENT_TTX_TRX_NORMAL,
		    .tenc = UCC_FAST_TX_ENCODING_NRZ,
		    .renc = UCC_FAST_RX_ENCODING_NRZ,
		    .tcrc = UCC_FAST_16_BIT_CRC,
		    .synl = UCC_FAST_SYNC_LEN_NOT_USED,
		    },
	.numQueuesTx = 1,
	.numQueuesRx = 1,
	.extendedFilteringChainPointer = ((uint32_t) NULL),
	.typeorlen = 3072 /*1536 */ ,
	.nonBackToBackIfgPart1 = 0x40,
	.nonBackToBackIfgPart2 = 0x60,
	.miminumInterFrameGapEnforcement = 0x50,
	.backToBackInterFrameGap = 0x60,
	.mblinterval = 128,
	.nortsrbytetime = 5,
	.fracsiz = 1,
	.strictpriorityq = 0xff,
	.altBebTruncation = 0xa,
	.excessDefer = 1,
	.maxRetransmission = 0xf,
	.collisionWindow = 0x37,
	.receiveFlowControl = 1,
L
Li Yang 已提交
110
	.transmitFlowControl = 1,
111 112 113
	.maxGroupAddrInHash = 4,
	.maxIndAddrInHash = 4,
	.prel = 7,
114
	.maxFrameLength = 1518+16, /* Add extra bytes for VLANs etc. */
115
	.minFrameLength = 64,
116 117
	.maxD1Length = 1520+16, /* Add extra bytes for VLANs etc. */
	.maxD2Length = 1520+16, /* Add extra bytes for VLANs etc. */
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	.vlantype = 0x8100,
	.ecamptr = ((uint32_t) NULL),
	.eventRegMask = UCCE_OTHER,
	.pausePeriod = 0xf000,
	.interruptcoalescingmaxvalue = {1, 1, 1, 1, 1, 1, 1, 1},
	.bdRingLenTx = {
			TX_BD_RING_LEN,
			TX_BD_RING_LEN,
			TX_BD_RING_LEN,
			TX_BD_RING_LEN,
			TX_BD_RING_LEN,
			TX_BD_RING_LEN,
			TX_BD_RING_LEN,
			TX_BD_RING_LEN},

	.bdRingLenRx = {
			RX_BD_RING_LEN,
			RX_BD_RING_LEN,
			RX_BD_RING_LEN,
			RX_BD_RING_LEN,
			RX_BD_RING_LEN,
			RX_BD_RING_LEN,
			RX_BD_RING_LEN,
			RX_BD_RING_LEN},

	.numStationAddresses = UCC_GETH_NUM_OF_STATION_ADDRESSES_1,
	.largestexternallookupkeysize =
	    QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_NONE,
L
Li Yang 已提交
146 147 148
	.statisticsMode = UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE |
		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX |
		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX,
149 150 151 152 153
	.vlanOperationTagged = UCC_GETH_VLAN_OPERATION_TAGGED_NOP,
	.vlanOperationNonTagged = UCC_GETH_VLAN_OPERATION_NON_TAGGED_NOP,
	.rxQoSMode = UCC_GETH_QOS_MODE_DEFAULT,
	.aufc = UPSMR_AUTOMATIC_FLOW_CONTROL_MODE_NONE,
	.padAndCrc = MACCFG2_PAD_AND_CRC_MODE_PAD_AND_CRC,
154 155
	.numThreadsTx = UCC_GETH_NUM_OF_THREADS_1,
	.numThreadsRx = UCC_GETH_NUM_OF_THREADS_1,
156 157 158 159
	.riscTx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
	.riscRx = QE_RISC_ALLOCATION_RISC1_AND_RISC2,
};

160
static struct ucc_geth_info ugeth_info[8];
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

#ifdef DEBUG
static void mem_disp(u8 *addr, int size)
{
	u8 *i;
	int size16Aling = (size >> 4) << 4;
	int size4Aling = (size >> 2) << 2;
	int notAlign = 0;
	if (size % 16)
		notAlign = 1;

	for (i = addr; (u32) i < (u32) addr + size16Aling; i += 16)
		printk("0x%08x: %08x %08x %08x %08x\r\n",
		       (u32) i,
		       *((u32 *) (i)),
		       *((u32 *) (i + 4)),
		       *((u32 *) (i + 8)), *((u32 *) (i + 12)));
	if (notAlign == 1)
		printk("0x%08x: ", (u32) i);
	for (; (u32) i < (u32) addr + size4Aling; i += 4)
		printk("%08x ", *((u32 *) (i)));
	for (; (u32) i < (u32) addr + size; i++)
183
		printk("%02x", *((i)));
184 185 186 187 188 189 190 191 192
	if (notAlign == 1)
		printk("\r\n");
}
#endif /* DEBUG */

static struct list_head *dequeue(struct list_head *lh)
{
	unsigned long flags;

193
	spin_lock_irqsave(&ugeth_lock, flags);
194 195 196
	if (!list_empty(lh)) {
		struct list_head *node = lh->next;
		list_del(node);
197
		spin_unlock_irqrestore(&ugeth_lock, flags);
198 199
		return node;
	} else {
200
		spin_unlock_irqrestore(&ugeth_lock, flags);
201 202 203 204
		return NULL;
	}
}

205 206
static struct sk_buff *get_new_skb(struct ucc_geth_private *ugeth,
		u8 __iomem *bd)
207
{
E
Eric Dumazet 已提交
208
	struct sk_buff *skb;
209

E
Eric Dumazet 已提交
210 211 212
	skb = netdev_alloc_skb(ugeth->ndev,
			       ugeth->ug_info->uf_info.max_rx_buf_length +
			       UCC_GETH_RX_DATA_BUF_ALIGNMENT);
213
	if (!skb)
214 215 216 217 218 219 220 221 222 223
		return NULL;

	/* We need the data buffer to be aligned properly.  We will reserve
	 * as many bytes as needed to align the data properly
	 */
	skb_reserve(skb,
		    UCC_GETH_RX_DATA_BUF_ALIGNMENT -
		    (((unsigned)skb->data) & (UCC_GETH_RX_DATA_BUF_ALIGNMENT -
					      1)));

224
	out_be32(&((struct qe_bd __iomem *)bd)->buf,
225
		      dma_map_single(ugeth->dev,
226 227 228 229 230
				     skb->data,
				     ugeth->ug_info->uf_info.max_rx_buf_length +
				     UCC_GETH_RX_DATA_BUF_ALIGNMENT,
				     DMA_FROM_DEVICE));

231 232
	out_be32((u32 __iomem *)bd,
			(R_E | R_I | (in_be32((u32 __iomem*)bd) & R_W)));
233 234 235 236

	return skb;
}

237
static int rx_bd_buffer_set(struct ucc_geth_private *ugeth, u8 rxQ)
238
{
239
	u8 __iomem *bd;
240 241 242 243 244 245 246 247
	u32 bd_status;
	struct sk_buff *skb;
	int i;

	bd = ugeth->p_rx_bd_ring[rxQ];
	i = 0;

	do {
248
		bd_status = in_be32((u32 __iomem *)bd);
249 250 251 252 253 254 255 256 257
		skb = get_new_skb(ugeth, bd);

		if (!skb)	/* If can not allocate data buffer,
				abort. Cleanup will be elsewhere */
			return -ENOMEM;

		ugeth->rx_skbuff[rxQ][i] = skb;

		/* advance the BD pointer */
258
		bd += sizeof(struct qe_bd);
259 260 261 262 263 264
		i++;
	} while (!(bd_status & R_W));

	return 0;
}

265
static int fill_init_enet_entries(struct ucc_geth_private *ugeth,
266
				  u32 *p_start,
267 268 269
				  u8 num_entries,
				  u32 thread_size,
				  u32 thread_alignment,
270
				  unsigned int risc,
271 272 273 274 275 276 277 278
				  int skip_page_for_first_entry)
{
	u32 init_enet_offset;
	u8 i;
	int snum;

	for (i = 0; i < num_entries; i++) {
		if ((snum = qe_get_snum()) < 0) {
279
			if (netif_msg_ifup(ugeth))
280
				pr_err("Can not get SNUM\n");
281 282 283 284 285 286 287 288
			return snum;
		}
		if ((i == 0) && skip_page_for_first_entry)
		/* First entry of Rx does not have page */
			init_enet_offset = 0;
		else {
			init_enet_offset =
			    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 293 294 295 296 297 298 299 300 301 302 303
				qe_put_snum((u8) snum);
				return -ENOMEM;
			}
		}
		*(p_start++) =
		    ((u8) snum << ENET_INIT_PARAM_SNUM_SHIFT) | init_enet_offset
		    | risc;
	}

	return 0;
}

304
static int return_init_enet_entries(struct ucc_geth_private *ugeth,
305
				    u32 *p_start,
306
				    u8 num_entries,
307
				    unsigned int risc,
308 309 310 311 312 313 314
				    int skip_page_for_first_entry)
{
	u32 init_enet_offset;
	u8 i;
	int snum;

	for (i = 0; i < num_entries; i++) {
315 316
		u32 val = *p_start;

317 318
		/* Check that this entry was actually valid --
		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 323 324 325 326
			    ENET_INIT_PARAM_SNUM_SHIFT;
			qe_put_snum((u8) snum);
			if (!((i == 0) && skip_page_for_first_entry)) {
			/* First entry of Rx does not have page */
				init_enet_offset =
327
				    (val & ENET_INIT_PARAM_PTR_MASK);
328 329
				qe_muram_free(init_enet_offset);
			}
330
			*p_start++ = 0;
331 332 333 334 335 336 337
		}
	}

	return 0;
}

#ifdef DEBUG
338
static int dump_init_enet_entries(struct ucc_geth_private *ugeth,
339
				  u32 __iomem *p_start,
340 341
				  u8 num_entries,
				  u32 thread_size,
342
				  unsigned int risc,
343 344 345 346 347 348 349
				  int skip_page_for_first_entry)
{
	u32 init_enet_offset;
	u8 i;
	int snum;

	for (i = 0; i < num_entries; i++) {
350 351
		u32 val = in_be32(p_start);

352 353
		/* Check that this entry was actually valid --
		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 358 359 360 361 362 363
			    ENET_INIT_PARAM_SNUM_SHIFT;
			qe_put_snum((u8) snum);
			if (!((i == 0) && skip_page_for_first_entry)) {
			/* First entry of Rx does not have page */
				init_enet_offset =
				    (in_be32(p_start) &
				     ENET_INIT_PARAM_PTR_MASK);
364 365 366
				pr_info("Init enet entry %d:\n", i);
				pr_info("Base address: 0x%08x\n",
					(u32)qe_muram_addr(init_enet_offset));
367 368 369 370 371 372 373 374 375 376 377
				mem_disp(qe_muram_addr(init_enet_offset),
					 thread_size);
			}
			p_start++;
		}
	}

	return 0;
}
#endif

378
static void put_enet_addr_container(struct enet_addr_container *enet_addr_cont)
379 380 381 382
{
	kfree(enet_addr_cont);
}

383
static void set_mac_addr(__be16 __iomem *reg, u8 *mac)
384 385 386 387 388 389 390
{
	out_be16(&reg[0], ((u16)mac[5] << 8) | mac[4]);
	out_be16(&reg[1], ((u16)mac[3] << 8) | mac[2]);
	out_be16(&reg[2], ((u16)mac[1] << 8) | mac[0]);
}

static 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 395
	if (paddr_num >= NUM_OF_PADDRS) {
		pr_warn("%s: Invalid paddr_num: %u\n", __func__, paddr_num);
396 397 398 399
		return -EINVAL;
	}

	p_82xx_addr_filt =
400
	    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->p_rx_glbl_pram->
401 402 403 404 405 406 407 408 409 410 411
	    addressfiltering;

	/* Writing address ff.ff.ff.ff.ff.ff disables address
	recognition for this register */
	out_be16(&p_82xx_addr_filt->paddr[paddr_num].h, 0xffff);
	out_be16(&p_82xx_addr_filt->paddr[paddr_num].m, 0xffff);
	out_be16(&p_82xx_addr_filt->paddr[paddr_num].l, 0xffff);

	return 0;
}

412 413
static void hw_add_addr_in_hash(struct ucc_geth_private *ugeth,
                                u8 *p_enet_addr)
414
{
415
	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
416 417 418
	u32 cecr_subblock;

	p_82xx_addr_filt =
419
	    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->p_rx_glbl_pram->
420 421 422 423 424 425
	    addressfiltering;

	cecr_subblock =
	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);

	/* Ethernet frames are defined in Little Endian mode,
D
Daniel Mack 已提交
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
436 437
static void get_statistics(struct ucc_geth_private *ugeth,
			   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 444
	struct ucc_fast __iomem *uf_regs;
	struct ucc_geth __iomem *ug_regs;
445 446
	struct ucc_geth_tx_firmware_statistics_pram *p_tx_fw_statistics_pram;
	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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
	p_tx_fw_statistics_pram = ugeth->p_tx_fw_statistics_pram;
	p_rx_fw_statistics_pram = ugeth->p_rx_fw_statistics_pram;

	/* Tx firmware only if user handed pointer and driver actually
	gathers Tx firmware statistics */
	if (tx_firmware_statistics && p_tx_fw_statistics_pram) {
		tx_firmware_statistics->sicoltx =
		    in_be32(&p_tx_fw_statistics_pram->sicoltx);
		tx_firmware_statistics->mulcoltx =
		    in_be32(&p_tx_fw_statistics_pram->mulcoltx);
		tx_firmware_statistics->latecoltxfr =
		    in_be32(&p_tx_fw_statistics_pram->latecoltxfr);
		tx_firmware_statistics->frabortduecol =
		    in_be32(&p_tx_fw_statistics_pram->frabortduecol);
		tx_firmware_statistics->frlostinmactxer =
		    in_be32(&p_tx_fw_statistics_pram->frlostinmactxer);
		tx_firmware_statistics->carriersenseertx =
		    in_be32(&p_tx_fw_statistics_pram->carriersenseertx);
		tx_firmware_statistics->frtxok =
		    in_be32(&p_tx_fw_statistics_pram->frtxok);
		tx_firmware_statistics->txfrexcessivedefer =
		    in_be32(&p_tx_fw_statistics_pram->txfrexcessivedefer);
		tx_firmware_statistics->txpkts256 =
		    in_be32(&p_tx_fw_statistics_pram->txpkts256);
		tx_firmware_statistics->txpkts512 =
		    in_be32(&p_tx_fw_statistics_pram->txpkts512);
		tx_firmware_statistics->txpkts1024 =
		    in_be32(&p_tx_fw_statistics_pram->txpkts1024);
		tx_firmware_statistics->txpktsjumbo =
		    in_be32(&p_tx_fw_statistics_pram->txpktsjumbo);
	}

	/* Rx firmware only if user handed pointer and driver actually
	 * gathers Rx firmware statistics */
	if (rx_firmware_statistics && p_rx_fw_statistics_pram) {
		int i;
		rx_firmware_statistics->frrxfcser =
		    in_be32(&p_rx_fw_statistics_pram->frrxfcser);
		rx_firmware_statistics->fraligner =
		    in_be32(&p_rx_fw_statistics_pram->fraligner);
		rx_firmware_statistics->inrangelenrxer =
		    in_be32(&p_rx_fw_statistics_pram->inrangelenrxer);
		rx_firmware_statistics->outrangelenrxer =
		    in_be32(&p_rx_fw_statistics_pram->outrangelenrxer);
		rx_firmware_statistics->frtoolong =
		    in_be32(&p_rx_fw_statistics_pram->frtoolong);
		rx_firmware_statistics->runt =
		    in_be32(&p_rx_fw_statistics_pram->runt);
		rx_firmware_statistics->verylongevent =
		    in_be32(&p_rx_fw_statistics_pram->verylongevent);
		rx_firmware_statistics->symbolerror =
		    in_be32(&p_rx_fw_statistics_pram->symbolerror);
		rx_firmware_statistics->dropbsy =
		    in_be32(&p_rx_fw_statistics_pram->dropbsy);
		for (i = 0; i < 0x8; i++)
			rx_firmware_statistics->res0[i] =
			    p_rx_fw_statistics_pram->res0[i];
		rx_firmware_statistics->mismatchdrop =
		    in_be32(&p_rx_fw_statistics_pram->mismatchdrop);
		rx_firmware_statistics->underpkts =
		    in_be32(&p_rx_fw_statistics_pram->underpkts);
		rx_firmware_statistics->pkts256 =
		    in_be32(&p_rx_fw_statistics_pram->pkts256);
		rx_firmware_statistics->pkts512 =
		    in_be32(&p_rx_fw_statistics_pram->pkts512);
		rx_firmware_statistics->pkts1024 =
		    in_be32(&p_rx_fw_statistics_pram->pkts1024);
		rx_firmware_statistics->pktsjumbo =
		    in_be32(&p_rx_fw_statistics_pram->pktsjumbo);
		rx_firmware_statistics->frlossinmacer =
		    in_be32(&p_rx_fw_statistics_pram->frlossinmacer);
		rx_firmware_statistics->pausefr =
		    in_be32(&p_rx_fw_statistics_pram->pausefr);
		for (i = 0; i < 0x4; i++)
			rx_firmware_statistics->res1[i] =
			    p_rx_fw_statistics_pram->res1[i];
		rx_firmware_statistics->removevlan =
		    in_be32(&p_rx_fw_statistics_pram->removevlan);
		rx_firmware_statistics->replacevlan =
		    in_be32(&p_rx_fw_statistics_pram->replacevlan);
		rx_firmware_statistics->insertvlan =
		    in_be32(&p_rx_fw_statistics_pram->insertvlan);
	}

	/* Hardware only if user handed pointer and driver actually
	gathers hardware statistics */
T
Timur Tabi 已提交
536 537
	if (hardware_statistics &&
	    (in_be32(&uf_regs->upsmr) & UCC_GETH_UPSMR_HSE)) {
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
		hardware_statistics->tx64 = in_be32(&ug_regs->tx64);
		hardware_statistics->tx127 = in_be32(&ug_regs->tx127);
		hardware_statistics->tx255 = in_be32(&ug_regs->tx255);
		hardware_statistics->rx64 = in_be32(&ug_regs->rx64);
		hardware_statistics->rx127 = in_be32(&ug_regs->rx127);
		hardware_statistics->rx255 = in_be32(&ug_regs->rx255);
		hardware_statistics->txok = in_be32(&ug_regs->txok);
		hardware_statistics->txcf = in_be16(&ug_regs->txcf);
		hardware_statistics->tmca = in_be32(&ug_regs->tmca);
		hardware_statistics->tbca = in_be32(&ug_regs->tbca);
		hardware_statistics->rxfok = in_be32(&ug_regs->rxfok);
		hardware_statistics->rxbok = in_be32(&ug_regs->rxbok);
		hardware_statistics->rbyt = in_be32(&ug_regs->rbyt);
		hardware_statistics->rmca = in_be32(&ug_regs->rmca);
		hardware_statistics->rbca = in_be32(&ug_regs->rbca);
	}
}

556
static void dump_bds(struct ucc_geth_private *ugeth)
557 558 559 560 561 562 563 564
{
	int i;
	int length;

	for (i = 0; i < ugeth->ug_info->numQueuesTx; i++) {
		if (ugeth->p_tx_bd_ring[i]) {
			length =
			    (ugeth->ug_info->bdRingLenTx[i] *
565
			     sizeof(struct qe_bd));
566
			pr_info("TX BDs[%d]\n", i);
567 568 569 570 571 572 573
			mem_disp(ugeth->p_tx_bd_ring[i], length);
		}
	}
	for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
		if (ugeth->p_rx_bd_ring[i]) {
			length =
			    (ugeth->ug_info->bdRingLenRx[i] *
574
			     sizeof(struct qe_bd));
575
			pr_info("RX BDs[%d]\n", i);
576 577 578 579 580
			mem_disp(ugeth->p_rx_bd_ring[i], length);
		}
	}
}

581
static void dump_regs(struct ucc_geth_private *ugeth)
582 583 584
{
	int i;

585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
	pr_info("UCC%d Geth registers:\n", ugeth->ug_info->uf_info.ucc_num + 1);
	pr_info("Base address: 0x%08x\n", (u32)ugeth->ug_regs);

	pr_info("maccfg1    : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->maccfg1,
		in_be32(&ugeth->ug_regs->maccfg1));
	pr_info("maccfg2    : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->maccfg2,
		in_be32(&ugeth->ug_regs->maccfg2));
	pr_info("ipgifg     : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->ipgifg,
		in_be32(&ugeth->ug_regs->ipgifg));
	pr_info("hafdup     : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->hafdup,
		in_be32(&ugeth->ug_regs->hafdup));
	pr_info("ifctl      : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->ifctl,
		in_be32(&ugeth->ug_regs->ifctl));
	pr_info("ifstat     : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->ifstat,
		in_be32(&ugeth->ug_regs->ifstat));
	pr_info("macstnaddr1: addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->macstnaddr1,
		in_be32(&ugeth->ug_regs->macstnaddr1));
	pr_info("macstnaddr2: addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->macstnaddr2,
		in_be32(&ugeth->ug_regs->macstnaddr2));
	pr_info("uempr      : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->uempr,
		in_be32(&ugeth->ug_regs->uempr));
	pr_info("utbipar    : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->utbipar,
		in_be32(&ugeth->ug_regs->utbipar));
	pr_info("uescr      : addr - 0x%08x, val - 0x%04x\n",
		(u32)&ugeth->ug_regs->uescr,
		in_be16(&ugeth->ug_regs->uescr));
	pr_info("tx64       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->tx64,
		in_be32(&ugeth->ug_regs->tx64));
	pr_info("tx127      : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->tx127,
		in_be32(&ugeth->ug_regs->tx127));
	pr_info("tx255      : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->tx255,
		in_be32(&ugeth->ug_regs->tx255));
	pr_info("rx64       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->rx64,
		in_be32(&ugeth->ug_regs->rx64));
	pr_info("rx127      : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->rx127,
		in_be32(&ugeth->ug_regs->rx127));
	pr_info("rx255      : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->rx255,
		in_be32(&ugeth->ug_regs->rx255));
	pr_info("txok       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->txok,
		in_be32(&ugeth->ug_regs->txok));
	pr_info("txcf       : addr - 0x%08x, val - 0x%04x\n",
		(u32)&ugeth->ug_regs->txcf,
		in_be16(&ugeth->ug_regs->txcf));
	pr_info("tmca       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->tmca,
		in_be32(&ugeth->ug_regs->tmca));
	pr_info("tbca       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->tbca,
		in_be32(&ugeth->ug_regs->tbca));
	pr_info("rxfok      : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->rxfok,
		in_be32(&ugeth->ug_regs->rxfok));
	pr_info("rxbok      : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->rxbok,
		in_be32(&ugeth->ug_regs->rxbok));
	pr_info("rbyt       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->rbyt,
		in_be32(&ugeth->ug_regs->rbyt));
	pr_info("rmca       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->rmca,
		in_be32(&ugeth->ug_regs->rmca));
	pr_info("rbca       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->rbca,
		in_be32(&ugeth->ug_regs->rbca));
	pr_info("scar       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->scar,
		in_be32(&ugeth->ug_regs->scar));
	pr_info("scam       : addr - 0x%08x, val - 0x%08x\n",
		(u32)&ugeth->ug_regs->scam,
		in_be32(&ugeth->ug_regs->scam));
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695

	if (ugeth->p_thread_data_tx) {
		int numThreadsTxNumerical;
		switch (ugeth->ug_info->numThreadsTx) {
		case UCC_GETH_NUM_OF_THREADS_1:
			numThreadsTxNumerical = 1;
			break;
		case UCC_GETH_NUM_OF_THREADS_2:
			numThreadsTxNumerical = 2;
			break;
		case UCC_GETH_NUM_OF_THREADS_4:
			numThreadsTxNumerical = 4;
			break;
		case UCC_GETH_NUM_OF_THREADS_6:
			numThreadsTxNumerical = 6;
			break;
		case UCC_GETH_NUM_OF_THREADS_8:
			numThreadsTxNumerical = 8;
			break;
		default:
			numThreadsTxNumerical = 0;
			break;
		}

696 697 698
		pr_info("Thread data TXs:\n");
		pr_info("Base address: 0x%08x\n",
			(u32)ugeth->p_thread_data_tx);
699
		for (i = 0; i < numThreadsTxNumerical; i++) {
700 701 702
			pr_info("Thread data TX[%d]:\n", i);
			pr_info("Base address: 0x%08x\n",
				(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 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729
		}
	}
	if (ugeth->p_thread_data_rx) {
		int numThreadsRxNumerical;
		switch (ugeth->ug_info->numThreadsRx) {
		case UCC_GETH_NUM_OF_THREADS_1:
			numThreadsRxNumerical = 1;
			break;
		case UCC_GETH_NUM_OF_THREADS_2:
			numThreadsRxNumerical = 2;
			break;
		case UCC_GETH_NUM_OF_THREADS_4:
			numThreadsRxNumerical = 4;
			break;
		case UCC_GETH_NUM_OF_THREADS_6:
			numThreadsRxNumerical = 6;
			break;
		case UCC_GETH_NUM_OF_THREADS_8:
			numThreadsRxNumerical = 8;
			break;
		default:
			numThreadsRxNumerical = 0;
			break;
		}

730 731 732
		pr_info("Thread data RX:\n");
		pr_info("Base address: 0x%08x\n",
			(u32)ugeth->p_thread_data_rx);
733
		for (i = 0; i < numThreadsRxNumerical; i++) {
734 735 736
			pr_info("Thread data RX[%d]:\n", i);
			pr_info("Base address: 0x%08x\n",
				(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 743 744
		pr_info("EXF global param:\n");
		pr_info("Base address: 0x%08x\n",
			(u32)ugeth->p_exf_glbl_param);
745 746 747 748
		mem_disp((u8 *) ugeth->p_exf_glbl_param,
			 sizeof(*ugeth->p_exf_glbl_param));
	}
	if (ugeth->p_tx_glbl_pram) {
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
		pr_info("TX global param:\n");
		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_tx_glbl_pram);
		pr_info("temoder      : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_tx_glbl_pram->temoder,
			in_be16(&ugeth->p_tx_glbl_pram->temoder));
	       pr_info("sqptr        : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->sqptr,
			in_be32(&ugeth->p_tx_glbl_pram->sqptr));
		pr_info("schedulerbasepointer: addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->schedulerbasepointer,
			in_be32(&ugeth->p_tx_glbl_pram->schedulerbasepointer));
		pr_info("txrmonbaseptr: addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->txrmonbaseptr,
			in_be32(&ugeth->p_tx_glbl_pram->txrmonbaseptr));
		pr_info("tstate       : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->tstate,
			in_be32(&ugeth->p_tx_glbl_pram->tstate));
		pr_info("iphoffset[0] : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_tx_glbl_pram->iphoffset[0],
			ugeth->p_tx_glbl_pram->iphoffset[0]);
		pr_info("iphoffset[1] : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_tx_glbl_pram->iphoffset[1],
			ugeth->p_tx_glbl_pram->iphoffset[1]);
		pr_info("iphoffset[2] : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_tx_glbl_pram->iphoffset[2],
			ugeth->p_tx_glbl_pram->iphoffset[2]);
		pr_info("iphoffset[3] : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_tx_glbl_pram->iphoffset[3],
			ugeth->p_tx_glbl_pram->iphoffset[3]);
		pr_info("iphoffset[4] : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_tx_glbl_pram->iphoffset[4],
			ugeth->p_tx_glbl_pram->iphoffset[4]);
		pr_info("iphoffset[5] : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_tx_glbl_pram->iphoffset[5],
			ugeth->p_tx_glbl_pram->iphoffset[5]);
		pr_info("iphoffset[6] : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_tx_glbl_pram->iphoffset[6],
			ugeth->p_tx_glbl_pram->iphoffset[6]);
		pr_info("iphoffset[7] : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_tx_glbl_pram->iphoffset[7],
			ugeth->p_tx_glbl_pram->iphoffset[7]);
		pr_info("vtagtable[0] : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->vtagtable[0],
			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[0]));
		pr_info("vtagtable[1] : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->vtagtable[1],
			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[1]));
		pr_info("vtagtable[2] : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->vtagtable[2],
			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[2]));
		pr_info("vtagtable[3] : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->vtagtable[3],
			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[3]));
		pr_info("vtagtable[4] : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->vtagtable[4],
			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[4]));
		pr_info("vtagtable[5] : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->vtagtable[5],
			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[5]));
		pr_info("vtagtable[6] : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->vtagtable[6],
			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[6]));
		pr_info("vtagtable[7] : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->vtagtable[7],
			in_be32(&ugeth->p_tx_glbl_pram->vtagtable[7]));
		pr_info("tqptr        : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_tx_glbl_pram->tqptr,
			in_be32(&ugeth->p_tx_glbl_pram->tqptr));
817 818
	}
	if (ugeth->p_rx_glbl_pram) {
819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
		pr_info("RX global param:\n");
		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_rx_glbl_pram);
		pr_info("remoder         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->remoder,
			in_be32(&ugeth->p_rx_glbl_pram->remoder));
		pr_info("rqptr           : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->rqptr,
			in_be32(&ugeth->p_rx_glbl_pram->rqptr));
		pr_info("typeorlen       : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_rx_glbl_pram->typeorlen,
			in_be16(&ugeth->p_rx_glbl_pram->typeorlen));
		pr_info("rxgstpack       : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_rx_glbl_pram->rxgstpack,
			ugeth->p_rx_glbl_pram->rxgstpack);
		pr_info("rxrmonbaseptr   : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->rxrmonbaseptr,
			in_be32(&ugeth->p_rx_glbl_pram->rxrmonbaseptr));
		pr_info("intcoalescingptr: addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->intcoalescingptr,
			in_be32(&ugeth->p_rx_glbl_pram->intcoalescingptr));
		pr_info("rstate          : addr - 0x%08x, val - 0x%02x\n",
			(u32)&ugeth->p_rx_glbl_pram->rstate,
			ugeth->p_rx_glbl_pram->rstate);
		pr_info("mrblr           : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_rx_glbl_pram->mrblr,
			in_be16(&ugeth->p_rx_glbl_pram->mrblr));
		pr_info("rbdqptr         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->rbdqptr,
			in_be32(&ugeth->p_rx_glbl_pram->rbdqptr));
		pr_info("mflr            : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_rx_glbl_pram->mflr,
			in_be16(&ugeth->p_rx_glbl_pram->mflr));
		pr_info("minflr          : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_rx_glbl_pram->minflr,
			in_be16(&ugeth->p_rx_glbl_pram->minflr));
		pr_info("maxd1           : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_rx_glbl_pram->maxd1,
			in_be16(&ugeth->p_rx_glbl_pram->maxd1));
		pr_info("maxd2           : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_rx_glbl_pram->maxd2,
			in_be16(&ugeth->p_rx_glbl_pram->maxd2));
		pr_info("ecamptr         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->ecamptr,
			in_be32(&ugeth->p_rx_glbl_pram->ecamptr));
		pr_info("l2qt            : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l2qt,
			in_be32(&ugeth->p_rx_glbl_pram->l2qt));
		pr_info("l3qt[0]         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l3qt[0],
			in_be32(&ugeth->p_rx_glbl_pram->l3qt[0]));
		pr_info("l3qt[1]         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l3qt[1],
			in_be32(&ugeth->p_rx_glbl_pram->l3qt[1]));
		pr_info("l3qt[2]         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l3qt[2],
			in_be32(&ugeth->p_rx_glbl_pram->l3qt[2]));
		pr_info("l3qt[3]         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l3qt[3],
			in_be32(&ugeth->p_rx_glbl_pram->l3qt[3]));
		pr_info("l3qt[4]         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l3qt[4],
			in_be32(&ugeth->p_rx_glbl_pram->l3qt[4]));
		pr_info("l3qt[5]         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l3qt[5],
			in_be32(&ugeth->p_rx_glbl_pram->l3qt[5]));
		pr_info("l3qt[6]         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l3qt[6],
			in_be32(&ugeth->p_rx_glbl_pram->l3qt[6]));
		pr_info("l3qt[7]         : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->l3qt[7],
			in_be32(&ugeth->p_rx_glbl_pram->l3qt[7]));
		pr_info("vlantype        : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_rx_glbl_pram->vlantype,
			in_be16(&ugeth->p_rx_glbl_pram->vlantype));
		pr_info("vlantci         : addr - 0x%08x, val - 0x%04x\n",
			(u32)&ugeth->p_rx_glbl_pram->vlantci,
			in_be16(&ugeth->p_rx_glbl_pram->vlantci));
896
		for (i = 0; i < 64; i++)
897 898 899 900 901 902 903
			pr_info("addressfiltering[%d]: addr - 0x%08x, val - 0x%02x\n",
				i,
				(u32)&ugeth->p_rx_glbl_pram->addressfiltering[i],
				ugeth->p_rx_glbl_pram->addressfiltering[i]);
		pr_info("exfGlobalParam  : addr - 0x%08x, val - 0x%08x\n",
			(u32)&ugeth->p_rx_glbl_pram->exfGlobalParam,
			in_be32(&ugeth->p_rx_glbl_pram->exfGlobalParam));
904 905
	}
	if (ugeth->p_send_q_mem_reg) {
906 907
		pr_info("Send Q memory registers:\n");
		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 910 911
			pr_info("SQQD[%d]:\n", i);
			pr_info("Base address: 0x%08x\n",
				(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 918
		pr_info("Scheduler:\n");
		pr_info("Base address: 0x%08x\n", (u32)ugeth->p_scheduler);
919 920 921 922
		mem_disp((u8 *) ugeth->p_scheduler,
			 sizeof(*ugeth->p_scheduler));
	}
	if (ugeth->p_tx_fw_statistics_pram) {
923 924 925
		pr_info("TX FW statistics pram:\n");
		pr_info("Base address: 0x%08x\n",
			(u32)ugeth->p_tx_fw_statistics_pram);
926 927 928 929
		mem_disp((u8 *) ugeth->p_tx_fw_statistics_pram,
			 sizeof(*ugeth->p_tx_fw_statistics_pram));
	}
	if (ugeth->p_rx_fw_statistics_pram) {
930 931 932
		pr_info("RX FW statistics pram:\n");
		pr_info("Base address: 0x%08x\n",
			(u32)ugeth->p_rx_fw_statistics_pram);
933 934 935 936
		mem_disp((u8 *) ugeth->p_rx_fw_statistics_pram,
			 sizeof(*ugeth->p_rx_fw_statistics_pram));
	}
	if (ugeth->p_rx_irq_coalescing_tbl) {
937 938 939
		pr_info("RX IRQ coalescing tables:\n");
		pr_info("Base address: 0x%08x\n",
			(u32)ugeth->p_rx_irq_coalescing_tbl);
940
		for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
			pr_info("RX IRQ coalescing table entry[%d]:\n", i);
			pr_info("Base address: 0x%08x\n",
				(u32)&ugeth->p_rx_irq_coalescing_tbl->
				coalescingentry[i]);
			pr_info("interruptcoalescingmaxvalue: addr - 0x%08x, val - 0x%08x\n",
				(u32)&ugeth->p_rx_irq_coalescing_tbl->
				coalescingentry[i].interruptcoalescingmaxvalue,
				in_be32(&ugeth->p_rx_irq_coalescing_tbl->
					coalescingentry[i].
					interruptcoalescingmaxvalue));
			pr_info("interruptcoalescingcounter : addr - 0x%08x, val - 0x%08x\n",
				(u32)&ugeth->p_rx_irq_coalescing_tbl->
				coalescingentry[i].interruptcoalescingcounter,
				in_be32(&ugeth->p_rx_irq_coalescing_tbl->
					coalescingentry[i].
					interruptcoalescingcounter));
957 958 959
		}
	}
	if (ugeth->p_rx_bd_qs_tbl) {
960 961
		pr_info("RX BD QS tables:\n");
		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 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983
			pr_info("RX BD QS table[%d]:\n", i);
			pr_info("Base address: 0x%08x\n",
				(u32)&ugeth->p_rx_bd_qs_tbl[i]);
			pr_info("bdbaseptr        : addr - 0x%08x, val - 0x%08x\n",
				(u32)&ugeth->p_rx_bd_qs_tbl[i].bdbaseptr,
				in_be32(&ugeth->p_rx_bd_qs_tbl[i].bdbaseptr));
			pr_info("bdptr            : addr - 0x%08x, val - 0x%08x\n",
				(u32)&ugeth->p_rx_bd_qs_tbl[i].bdptr,
				in_be32(&ugeth->p_rx_bd_qs_tbl[i].bdptr));
			pr_info("externalbdbaseptr: addr - 0x%08x, val - 0x%08x\n",
				(u32)&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
				in_be32(&ugeth->p_rx_bd_qs_tbl[i].
					externalbdbaseptr));
			pr_info("externalbdptr    : addr - 0x%08x, val - 0x%08x\n",
				(u32)&ugeth->p_rx_bd_qs_tbl[i].externalbdptr,
				in_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdptr));
			pr_info("ucode RX Prefetched BDs:\n");
			pr_info("Base address: 0x%08x\n",
				(u32)qe_muram_addr(in_be32
						   (&ugeth->p_rx_bd_qs_tbl[i].
						    bdbaseptr)));
984 985 986 987
			mem_disp((u8 *)
				 qe_muram_addr(in_be32
					       (&ugeth->p_rx_bd_qs_tbl[i].
						bdbaseptr)),
988
				 sizeof(struct ucc_geth_rx_prefetched_bds));
989 990 991 992
		}
	}
	if (ugeth->p_init_enet_param_shadow) {
		int size;
993 994 995
		pr_info("Init enet param shadow:\n");
		pr_info("Base address: 0x%08x\n",
			(u32) ugeth->p_init_enet_param_shadow);
996 997 998
		mem_disp((u8 *) ugeth->p_init_enet_param_shadow,
			 sizeof(*ugeth->p_init_enet_param_shadow));

999
		size = sizeof(struct ucc_geth_thread_rx_pram);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		if (ugeth->ug_info->rxExtendedFiltering) {
			size +=
			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING;
			if (ugeth->ug_info->largestexternallookupkeysize ==
			    QE_FLTR_TABLE_LOOKUP_KEY_SIZE_8_BYTES)
				size +=
			THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_8;
			if (ugeth->ug_info->largestexternallookupkeysize ==
			    QE_FLTR_TABLE_LOOKUP_KEY_SIZE_16_BYTES)
				size +=
			THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_16;
		}

		dump_init_enet_entries(ugeth,
				       &(ugeth->p_init_enet_param_shadow->
					 txthread[0]),
				       ENET_INIT_PARAM_MAX_ENTRIES_TX,
1017
				       sizeof(struct ucc_geth_thread_tx_pram),
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
				       ugeth->ug_info->riscTx, 0);
		dump_init_enet_entries(ugeth,
				       &(ugeth->p_init_enet_param_shadow->
					 rxthread[0]),
				       ENET_INIT_PARAM_MAX_ENTRIES_RX, size,
				       ugeth->ug_info->riscRx, 1);
	}
}
#endif /* DEBUG */

1028 1029 1030
static void init_default_reg_vals(u32 __iomem *upsmr_register,
				  u32 __iomem *maccfg1_register,
				  u32 __iomem *maccfg2_register)
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043
{
	out_be32(upsmr_register, UCC_GETH_UPSMR_INIT);
	out_be32(maccfg1_register, UCC_GETH_MACCFG1_INIT);
	out_be32(maccfg2_register, UCC_GETH_MACCFG2_INIT);
}

static int init_half_duplex_params(int alt_beb,
				   int back_pressure_no_backoff,
				   int no_backoff,
				   int excess_defer,
				   u8 alt_beb_truncation,
				   u8 max_retransmissions,
				   u8 collision_window,
1044
				   u32 __iomem *hafdup_register)
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
{
	u32 value = 0;

	if ((alt_beb_truncation > HALFDUP_ALT_BEB_TRUNCATION_MAX) ||
	    (max_retransmissions > HALFDUP_MAX_RETRANSMISSION_MAX) ||
	    (collision_window > HALFDUP_COLLISION_WINDOW_MAX))
		return -EINVAL;

	value = (u32) (alt_beb_truncation << HALFDUP_ALT_BEB_TRUNCATION_SHIFT);

	if (alt_beb)
		value |= HALFDUP_ALT_BEB;
	if (back_pressure_no_backoff)
		value |= HALFDUP_BACK_PRESSURE_NO_BACKOFF;
	if (no_backoff)
		value |= HALFDUP_NO_BACKOFF;
	if (excess_defer)
		value |= HALFDUP_EXCESSIVE_DEFER;

	value |= (max_retransmissions << HALFDUP_MAX_RETRANSMISSION_SHIFT);

	value |= collision_window;

	out_be32(hafdup_register, value);
	return 0;
}

static int init_inter_frame_gap_params(u8 non_btb_cs_ipg,
				       u8 non_btb_ipg,
				       u8 min_ifg,
				       u8 btb_ipg,
1076
				       u32 __iomem *ipgifg_register)
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
{
	u32 value = 0;

	/* Non-Back-to-back IPG part 1 should be <= Non-Back-to-back
	IPG part 2 */
	if (non_btb_cs_ipg > non_btb_ipg)
		return -EINVAL;

	if ((non_btb_cs_ipg > IPGIFG_NON_BACK_TO_BACK_IFG_PART1_MAX) ||
	    (non_btb_ipg > IPGIFG_NON_BACK_TO_BACK_IFG_PART2_MAX) ||
	    /*(min_ifg        > IPGIFG_MINIMUM_IFG_ENFORCEMENT_MAX) || */
	    (btb_ipg > IPGIFG_BACK_TO_BACK_IFG_MAX))
		return -EINVAL;

	value |=
	    ((non_btb_cs_ipg << IPGIFG_NON_BACK_TO_BACK_IFG_PART1_SHIFT) &
	     IPGIFG_NBTB_CS_IPG_MASK);
	value |=
	    ((non_btb_ipg << IPGIFG_NON_BACK_TO_BACK_IFG_PART2_SHIFT) &
	     IPGIFG_NBTB_IPG_MASK);
	value |=
	    ((min_ifg << IPGIFG_MINIMUM_IFG_ENFORCEMENT_SHIFT) &
	     IPGIFG_MIN_IFG_MASK);
	value |= (btb_ipg & IPGIFG_BTB_IPG_MASK);

	out_be32(ipgifg_register, value);
	return 0;
}

L
Li Yang 已提交
1106
int init_flow_control_params(u32 automatic_flow_control_mode,
1107 1108 1109 1110
				    int rx_flow_control_enable,
				    int tx_flow_control_enable,
				    u16 pause_period,
				    u16 extension_field,
1111 1112 1113
				    u32 __iomem *upsmr_register,
				    u32 __iomem *uempr_register,
				    u32 __iomem *maccfg1_register)
1114 1115 1116 1117 1118 1119 1120 1121 1122
{
	u32 value = 0;

	/* Set UEMPR register */
	value = (u32) pause_period << UEMPR_PAUSE_TIME_VALUE_SHIFT;
	value |= (u32) extension_field << UEMPR_EXTENDED_PAUSE_TIME_VALUE_SHIFT;
	out_be32(uempr_register, value);

	/* Set UPSMR register */
T
Timur Tabi 已提交
1123
	setbits32(upsmr_register, automatic_flow_control_mode);
1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

	value = in_be32(maccfg1_register);
	if (rx_flow_control_enable)
		value |= MACCFG1_FLOW_RX;
	if (tx_flow_control_enable)
		value |= MACCFG1_FLOW_TX;
	out_be32(maccfg1_register, value);

	return 0;
}

static int init_hw_statistics_gathering_mode(int enable_hardware_statistics,
					     int auto_zero_hardware_statistics,
1137 1138
					     u32 __iomem *upsmr_register,
					     u16 __iomem *uescr_register)
1139 1140
{
	u16 uescr_value = 0;
T
Timur Tabi 已提交
1141

1142
	/* Enable hardware statistics gathering if requested */
T
Timur Tabi 已提交
1143 1144
	if (enable_hardware_statistics)
		setbits32(upsmr_register, UCC_GETH_UPSMR_HSE);
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160

	/* Clear hardware statistics counters */
	uescr_value = in_be16(uescr_register);
	uescr_value |= UESCR_CLRCNT;
	/* Automatically zero hardware statistics counters on read,
	if requested */
	if (auto_zero_hardware_statistics)
		uescr_value |= UESCR_AUTOZ;
	out_be16(uescr_register, uescr_value);

	return 0;
}

static int init_firmware_statistics_gathering_mode(int
		enable_tx_firmware_statistics,
		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 1166
		u16 __iomem *temoder_register,
		u32 __iomem *remoder_register)
1167 1168 1169 1170 1171 1172 1173
{
	/* Note: this function does not check if */
	/* the parameters it receives are NULL   */

	if (enable_tx_firmware_statistics) {
		out_be32(tx_rmon_base_ptr,
			 tx_firmware_statistics_structure_address);
T
Timur Tabi 已提交
1174
		setbits16(temoder_register, TEMODER_TX_RMON_STATISTICS_ENABLE);
1175 1176 1177 1178 1179
	}

	if (enable_rx_firmware_statistics) {
		out_be32(rx_rmon_base_ptr,
			 rx_firmware_statistics_structure_address);
T
Timur Tabi 已提交
1180
		setbits32(remoder_register, REMODER_RX_RMON_STATISTICS_ENABLE);
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	}

	return 0;
}

static int init_mac_station_addr_regs(u8 address_byte_0,
				      u8 address_byte_1,
				      u8 address_byte_2,
				      u8 address_byte_3,
				      u8 address_byte_4,
				      u8 address_byte_5,
1192 1193
				      u32 __iomem *macstnaddr1_register,
				      u32 __iomem *macstnaddr2_register)
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
{
	u32 value = 0;

	/* Example: for a station address of 0x12345678ABCD, */
	/* 0x12 is byte 0, 0x34 is byte 1 and so on and 0xCD is byte 5 */

	/* MACSTNADDR1 Register: */

	/* 0                      7   8                      15  */
	/* station address byte 5     station address byte 4     */
	/* 16                     23  24                     31  */
	/* station address byte 3     station address byte 2     */
	value |= (u32) ((address_byte_2 << 0) & 0x000000FF);
	value |= (u32) ((address_byte_3 << 8) & 0x0000FF00);
	value |= (u32) ((address_byte_4 << 16) & 0x00FF0000);
	value |= (u32) ((address_byte_5 << 24) & 0xFF000000);

	out_be32(macstnaddr1_register, value);

	/* MACSTNADDR2 Register: */

	/* 0                      7   8                      15  */
	/* station address byte 1     station address byte 0     */
	/* 16                     23  24                     31  */
	/*         reserved                   reserved           */
	value = 0;
	value |= (u32) ((address_byte_0 << 16) & 0x00FF0000);
	value |= (u32) ((address_byte_1 << 24) & 0xFF000000);

	out_be32(macstnaddr2_register, value);

	return 0;
}

static int init_check_frame_length_mode(int length_check,
1229
					u32 __iomem *maccfg2_register)
1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
{
	u32 value = 0;

	value = in_be32(maccfg2_register);

	if (length_check)
		value |= MACCFG2_LC;
	else
		value &= ~MACCFG2_LC;

	out_be32(maccfg2_register, value);
	return 0;
}

static int init_preamble_length(u8 preamble_length,
1245
				u32 __iomem *maccfg2_register)
1246 1247 1248 1249
{
	if ((preamble_length < 3) || (preamble_length > 7))
		return -EINVAL;

T
Timur Tabi 已提交
1250 1251 1252
	clrsetbits_be32(maccfg2_register, MACCFG2_PREL_MASK,
			preamble_length << MACCFG2_PREL_SHIFT);

1253 1254 1255 1256 1257
	return 0;
}

static int init_rx_parameters(int reject_broadcast,
			      int receive_short_frames,
1258
			      int promiscuous, u32 __iomem *upsmr_register)
1259 1260 1261 1262 1263 1264
{
	u32 value = 0;

	value = in_be32(upsmr_register);

	if (reject_broadcast)
T
Timur Tabi 已提交
1265
		value |= UCC_GETH_UPSMR_BRO;
1266
	else
T
Timur Tabi 已提交
1267
		value &= ~UCC_GETH_UPSMR_BRO;
1268 1269

	if (receive_short_frames)
T
Timur Tabi 已提交
1270
		value |= UCC_GETH_UPSMR_RSH;
1271
	else
T
Timur Tabi 已提交
1272
		value &= ~UCC_GETH_UPSMR_RSH;
1273 1274

	if (promiscuous)
T
Timur Tabi 已提交
1275
		value |= UCC_GETH_UPSMR_PRO;
1276
	else
T
Timur Tabi 已提交
1277
		value &= ~UCC_GETH_UPSMR_PRO;
1278 1279 1280 1281 1282 1283 1284

	out_be32(upsmr_register, value);

	return 0;
}

static 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 1289
	if ((max_rx_buf_len == 0) ||
	    (max_rx_buf_len % UCC_GETH_MRBLR_ALIGNMENT))
1290 1291 1292 1293 1294 1295 1296
		return -EINVAL;

	out_be16(mrblr_register, max_rx_buf_len);
	return 0;
}

static int init_min_frame_len(u16 min_frame_length,
1297 1298
			      u16 __iomem *minflr_register,
			      u16 __iomem *mrblr_register)
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
{
	u16 mrblr_value = 0;

	mrblr_value = in_be16(mrblr_register);
	if (min_frame_length >= (mrblr_value - 4))
		return -EINVAL;

	out_be16(minflr_register, min_frame_length);
	return 0;
}

1310
static int adjust_enet_interface(struct ucc_geth_private *ugeth)
1311
{
1312
	struct ucc_geth_info *ug_info;
1313 1314
	struct ucc_geth __iomem *ug_regs;
	struct ucc_fast __iomem *uf_regs;
1315
	int ret_val;
1316
	u32 upsmr, maccfg2;
1317 1318
	u16 value;

1319
	ugeth_vdbg("%s: IN", __func__);
1320 1321 1322 1323 1324 1325 1326 1327

	ug_info = ugeth->ug_info;
	ug_regs = ugeth->ug_regs;
	uf_regs = ugeth->uccf->uf_regs;

	/*                    Set MACCFG2                    */
	maccfg2 = in_be32(&ug_regs->maccfg2);
	maccfg2 &= ~MACCFG2_INTERFACE_MODE_MASK;
1328 1329
	if ((ugeth->max_speed == SPEED_10) ||
	    (ugeth->max_speed == SPEED_100))
1330
		maccfg2 |= MACCFG2_INTERFACE_MODE_NIBBLE;
1331
	else if (ugeth->max_speed == SPEED_1000)
1332 1333 1334 1335 1336 1337
		maccfg2 |= MACCFG2_INTERFACE_MODE_BYTE;
	maccfg2 |= ug_info->padAndCrc;
	out_be32(&ug_regs->maccfg2, maccfg2);

	/*                    Set UPSMR                      */
	upsmr = in_be32(&uf_regs->upsmr);
T
Timur Tabi 已提交
1338 1339
	upsmr &= ~(UCC_GETH_UPSMR_RPM | UCC_GETH_UPSMR_R10M |
		   UCC_GETH_UPSMR_TBIM | UCC_GETH_UPSMR_RMM);
1340 1341 1342
	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_RMII) ||
	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII) ||
	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1343 1344
	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
1345
	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1346 1347
		if (ugeth->phy_interface != PHY_INTERFACE_MODE_RMII)
			upsmr |= UCC_GETH_UPSMR_RPM;
1348 1349
		switch (ugeth->max_speed) {
		case SPEED_10:
T
Timur Tabi 已提交
1350
			upsmr |= UCC_GETH_UPSMR_R10M;
1351
			fallthrough;
1352 1353
		case SPEED_100:
			if (ugeth->phy_interface != PHY_INTERFACE_MODE_RTBI)
T
Timur Tabi 已提交
1354
				upsmr |= UCC_GETH_UPSMR_RMM;
1355 1356 1357 1358
		}
	}
	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_TBI) ||
	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
T
Timur Tabi 已提交
1359
		upsmr |= UCC_GETH_UPSMR_TBIM;
1360
	}
1361
	if (ugeth->phy_interface == PHY_INTERFACE_MODE_SGMII)
1362 1363
		upsmr |= UCC_GETH_UPSMR_SGMM;

1364 1365 1366 1367 1368
	out_be32(&uf_regs->upsmr, upsmr);

	/* Disable autonegotiation in tbi mode, because by default it
	comes up in autonegotiation mode. */
	/* Note that this depends on proper setting in utbipar register. */
1369 1370
	if ((ugeth->phy_interface == PHY_INTERFACE_MODE_TBI) ||
	    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
1371 1372 1373 1374
		struct ucc_geth_info *ug_info = ugeth->ug_info;
		struct phy_device *tbiphy;

		if (!ug_info->tbi_node)
1375
			pr_warn("TBI mode requires that the device tree specify a tbi-handle\n");
1376 1377 1378

		tbiphy = of_phy_find_device(ug_info->tbi_node);
		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

A
Andrew Lunn 已提交
1385
		put_device(&tbiphy->mdio.dev);
1386 1387 1388 1389 1390 1391
	}

	init_check_frame_length_mode(ug_info->lengthCheckRx, &ug_regs->maccfg2);

	ret_val = init_preamble_length(ug_info->prel, &ug_regs->maccfg2);
	if (ret_val != 0) {
1392
		if (netif_msg_probe(ugeth))
1393
			pr_err("Preamble length must be between 3 and 7 inclusive\n");
1394 1395 1396 1397 1398 1399
		return ret_val;
	}

	return 0;
}

1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502
static int ugeth_graceful_stop_tx(struct ucc_geth_private *ugeth)
{
	struct ucc_fast_private *uccf;
	u32 cecr_subblock;
	u32 temp;
	int i = 10;

	uccf = ugeth->uccf;

	/* Mask GRACEFUL STOP TX interrupt bit and clear it */
	clrbits32(uccf->p_uccm, UCC_GETH_UCCE_GRA);
	out_be32(uccf->p_ucce, UCC_GETH_UCCE_GRA);  /* clear by writing 1 */

	/* Issue host command */
	cecr_subblock =
	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
	qe_issue_cmd(QE_GRACEFUL_STOP_TX, cecr_subblock,
		     QE_CR_PROTOCOL_ETHERNET, 0);

	/* Wait for command to complete */
	do {
		msleep(10);
		temp = in_be32(uccf->p_ucce);
	} while (!(temp & UCC_GETH_UCCE_GRA) && --i);

	uccf->stopped_tx = 1;

	return 0;
}

static int ugeth_graceful_stop_rx(struct ucc_geth_private *ugeth)
{
	struct ucc_fast_private *uccf;
	u32 cecr_subblock;
	u8 temp;
	int i = 10;

	uccf = ugeth->uccf;

	/* Clear acknowledge bit */
	temp = in_8(&ugeth->p_rx_glbl_pram->rxgstpack);
	temp &= ~GRACEFUL_STOP_ACKNOWLEDGE_RX;
	out_8(&ugeth->p_rx_glbl_pram->rxgstpack, temp);

	/* Keep issuing command and checking acknowledge bit until
	it is asserted, according to spec */
	do {
		/* Issue host command */
		cecr_subblock =
		    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.
						ucc_num);
		qe_issue_cmd(QE_GRACEFUL_STOP_RX, cecr_subblock,
			     QE_CR_PROTOCOL_ETHERNET, 0);
		msleep(10);
		temp = in_8(&ugeth->p_rx_glbl_pram->rxgstpack);
	} while (!(temp & GRACEFUL_STOP_ACKNOWLEDGE_RX) && --i);

	uccf->stopped_rx = 1;

	return 0;
}

static int ugeth_restart_tx(struct ucc_geth_private *ugeth)
{
	struct ucc_fast_private *uccf;
	u32 cecr_subblock;

	uccf = ugeth->uccf;

	cecr_subblock =
	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
	qe_issue_cmd(QE_RESTART_TX, cecr_subblock, QE_CR_PROTOCOL_ETHERNET, 0);
	uccf->stopped_tx = 0;

	return 0;
}

static int ugeth_restart_rx(struct ucc_geth_private *ugeth)
{
	struct ucc_fast_private *uccf;
	u32 cecr_subblock;

	uccf = ugeth->uccf;

	cecr_subblock =
	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
	qe_issue_cmd(QE_RESTART_RX, cecr_subblock, QE_CR_PROTOCOL_ETHERNET,
		     0);
	uccf->stopped_rx = 0;

	return 0;
}

static int ugeth_enable(struct ucc_geth_private *ugeth, enum comm_dir mode)
{
	struct ucc_fast_private *uccf;
	int enabled_tx, enabled_rx;

	uccf = ugeth->uccf;

	/* check if the UCC number is in range. */
	if (ugeth->ug_info->uf_info.ucc_num >= UCC_MAX_NUM) {
		if (netif_msg_probe(ugeth))
1503
			pr_err("ucc_num out of range\n");
1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531
		return -EINVAL;
	}

	enabled_tx = uccf->enabled_tx;
	enabled_rx = uccf->enabled_rx;

	/* Get Tx and Rx going again, in case this channel was actively
	disabled. */
	if ((mode & COMM_DIR_TX) && (!enabled_tx) && uccf->stopped_tx)
		ugeth_restart_tx(ugeth);
	if ((mode & COMM_DIR_RX) && (!enabled_rx) && uccf->stopped_rx)
		ugeth_restart_rx(ugeth);

	ucc_fast_enable(uccf, mode);	/* OK to do even if not disabled */

	return 0;

}

static int ugeth_disable(struct ucc_geth_private *ugeth, enum comm_dir mode)
{
	struct ucc_fast_private *uccf;

	uccf = ugeth->uccf;

	/* check if the UCC number is in range. */
	if (ugeth->ug_info->uf_info.ucc_num >= UCC_MAX_NUM) {
		if (netif_msg_probe(ugeth))
1532
			pr_err("ucc_num out of range\n");
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
		return -EINVAL;
	}

	/* Stop any transmissions */
	if ((mode & COMM_DIR_TX) && uccf->enabled_tx && !uccf->stopped_tx)
		ugeth_graceful_stop_tx(ugeth);

	/* Stop any receptions */
	if ((mode & COMM_DIR_RX) && uccf->enabled_rx && !uccf->stopped_rx)
		ugeth_graceful_stop_rx(ugeth);

	ucc_fast_disable(ugeth->uccf, mode); /* OK to do even if not enabled */

	return 0;
}

1549 1550
static void ugeth_quiesce(struct ucc_geth_private *ugeth)
{
1551 1552
	/* Prevent any further xmits */
	netif_tx_stop_all_queues(ugeth->ndev);
1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564

	/* Disable the interrupt to avoid NAPI rescheduling. */
	disable_irq(ugeth->ug_info->uf_info.irq);

	/* Stop NAPI, and possibly wait for its completion. */
	napi_disable(&ugeth->napi);
}

static void ugeth_activate(struct ucc_geth_private *ugeth)
{
	napi_enable(&ugeth->napi);
	enable_irq(ugeth->ug_info->uf_info.irq);
1565 1566 1567 1568

	/* allow to xmit again  */
	netif_tx_wake_all_queues(ugeth->ndev);
	__netdev_watchdog_up(ugeth->ndev);
1569 1570
}

1571 1572 1573 1574 1575 1576
/* Called every time the controller might need to be made
 * aware of new link state.  The PHY code conveys this
 * information through variables in the ugeth structure, and this
 * function converts those variables into the appropriate
 * register values, and can bring down the device if needed.
 */
1577

1578 1579
static void adjust_link(struct net_device *dev)
{
1580
	struct ucc_geth_private *ugeth = netdev_priv(dev);
1581 1582
	struct ucc_geth __iomem *ug_regs;
	struct ucc_fast __iomem *uf_regs;
1583 1584
	struct phy_device *phydev = ugeth->phydev;
	int new_state = 0;
1585 1586

	ug_regs = ugeth->ug_regs;
1587
	uf_regs = ugeth->uccf->uf_regs;
1588

1589 1590 1591
	if (phydev->link) {
		u32 tempval = in_be32(&ug_regs->maccfg2);
		u32 upsmr = in_be32(&uf_regs->upsmr);
1592 1593
		/* Now we make sure that we can be in full duplex mode.
		 * If not, we operate in half-duplex mode. */
1594 1595 1596
		if (phydev->duplex != ugeth->oldduplex) {
			new_state = 1;
			if (!(phydev->duplex))
1597
				tempval &= ~(MACCFG2_FDX);
1598
			else
1599
				tempval |= MACCFG2_FDX;
1600
			ugeth->oldduplex = phydev->duplex;
1601 1602
		}

1603 1604 1605 1606 1607 1608 1609
		if (phydev->speed != ugeth->oldspeed) {
			new_state = 1;
			switch (phydev->speed) {
			case SPEED_1000:
				tempval = ((tempval &
					    ~(MACCFG2_INTERFACE_MODE_MASK)) |
					    MACCFG2_INTERFACE_MODE_BYTE);
1610
				break;
1611 1612 1613 1614 1615 1616 1617 1618 1619
			case SPEED_100:
			case SPEED_10:
				tempval = ((tempval &
					    ~(MACCFG2_INTERFACE_MODE_MASK)) |
					    MACCFG2_INTERFACE_MODE_NIBBLE);
				/* if reduced mode, re-set UPSMR.R10M */
				if ((ugeth->phy_interface == PHY_INTERFACE_MODE_RMII) ||
				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII) ||
				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_ID) ||
1620 1621
				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_RXID) ||
				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID) ||
1622 1623
				    (ugeth->phy_interface == PHY_INTERFACE_MODE_RTBI)) {
					if (phydev->speed == SPEED_10)
T
Timur Tabi 已提交
1624
						upsmr |= UCC_GETH_UPSMR_R10M;
1625
					else
T
Timur Tabi 已提交
1626
						upsmr &= ~UCC_GETH_UPSMR_R10M;
1627
				}
1628 1629
				break;
			default:
1630
				if (netif_msg_link(ugeth))
1631
					pr_warn(
1632 1633
						"%s: Ack!  Speed (%d) is not 10/100/1000!",
						dev->name, phydev->speed);
1634 1635
				break;
			}
1636
			ugeth->oldspeed = phydev->speed;
1637 1638 1639
		}

		if (!ugeth->oldlink) {
1640
			new_state = 1;
1641 1642
			ugeth->oldlink = 1;
		}
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660

		if (new_state) {
			/*
			 * To change the MAC configuration we need to disable
			 * the controller. To do so, we have to either grab
			 * ugeth->lock, which is a bad idea since 'graceful
			 * stop' commands might take quite a while, or we can
			 * quiesce driver's activity.
			 */
			ugeth_quiesce(ugeth);
			ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);

			out_be32(&ug_regs->maccfg2, tempval);
			out_be32(&uf_regs->upsmr, upsmr);

			ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
			ugeth_activate(ugeth);
		}
1661 1662
	} else if (ugeth->oldlink) {
			new_state = 1;
1663 1664 1665 1666
			ugeth->oldlink = 0;
			ugeth->oldspeed = 0;
			ugeth->oldduplex = -1;
	}
1667 1668 1669

	if (new_state && netif_msg_link(ugeth))
		phy_print_status(phydev);
1670 1671
}

1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
/* Initialize TBI PHY interface for communicating with the
 * SERDES lynx PHY on the chip.  We communicate with this PHY
 * through the MDIO bus on each controller, treating it as a
 * "normal" PHY at the address found in the UTBIPA register.  We assume
 * that the UTBIPA register is valid.  Either the MDIO bus code will set
 * it to a value that doesn't conflict with other PHYs on the bus, or the
 * value doesn't matter, as there are no other PHYs on the bus.
 */
static void uec_configure_serdes(struct net_device *dev)
{
	struct ucc_geth_private *ugeth = netdev_priv(dev);
	struct ucc_geth_info *ug_info = ugeth->ug_info;
	struct phy_device *tbiphy;

	if (!ug_info->tbi_node) {
		dev_warn(&dev->dev, "SGMII mode requires that the device "
			"tree specify a tbi-handle\n");
		return;
	}

	tbiphy = of_phy_find_device(ug_info->tbi_node);
	if (!tbiphy) {
		dev_err(&dev->dev, "error: Could not get TBI device\n");
		return;
	}

	/*
	 * If the link is already up, we must already be ok, and don't need to
	 * configure and reset the TBI<->SerDes link.  Maybe U-Boot configured
	 * everything for us?  Resetting it takes the link down and requires
	 * several seconds for it to come back.
	 */
1704
	if (phy_read(tbiphy, ENET_TBI_MII_SR) & TBISR_LSTATUS) {
A
Andrew Lunn 已提交
1705
		put_device(&tbiphy->mdio.dev);
1706
		return;
1707
	}
1708 1709 1710 1711 1712 1713 1714

	/* Single clk mode, mii mode off(for serdes communication) */
	phy_write(tbiphy, ENET_TBI_MII_ANA, TBIANA_SETTINGS);

	phy_write(tbiphy, ENET_TBI_MII_TBICON, TBICON_CLK_SELECT);

	phy_write(tbiphy, ENET_TBI_MII_CR, TBICR_SETTINGS);
1715

1716
	put_device(&tbiphy->mdio.dev);
1717 1718
}

1719 1720 1721 1722 1723
/* Configure the PHY for dev.
 * returns 0 if success.  -1 if failure
 */
static int init_phy(struct net_device *dev)
{
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 1729 1730
	priv->oldlink = 0;
	priv->oldspeed = 0;
	priv->oldduplex = -1;
1731

1732 1733 1734
	phydev = of_phy_connect(dev, ug_info->phy_node, &adjust_link, 0,
				priv->phy_interface);
	if (!phydev) {
1735
		dev_err(&dev->dev, "Could not attach to PHY\n");
1736
		return -ENODEV;
1737 1738
	}

1739 1740 1741
	if (priv->phy_interface == PHY_INTERFACE_MODE_SGMII)
		uec_configure_serdes(dev);

1742
	phy_set_max_speed(phydev, priv->max_speed);
1743

1744
	priv->phydev = phydev;
1745 1746 1747 1748

	return 0;
}

1749
static void ugeth_dump_regs(struct ucc_geth_private *ugeth)
1750 1751 1752 1753 1754 1755 1756 1757
{
#ifdef DEBUG
	ucc_fast_dump_regs(ugeth->uccf);
	dump_regs(ugeth);
	dump_bds(ugeth);
#endif
}

1758
static int ugeth_82xx_filtering_clear_all_addr_in_hash(struct ucc_geth_private *
1759
						       ugeth,
1760
						       enum enet_addr_type
1761 1762
						       enet_addr_type)
{
1763
	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
1764 1765
	struct ucc_fast_private *uccf;
	enum comm_dir comm_dir;
1766 1767
	struct list_head *p_lh;
	u16 i, num;
1768 1769
	u32 __iomem *addr_h;
	u32 __iomem *addr_l;
1770 1771 1772 1773 1774
	u8 *p_counter;

	uccf = ugeth->uccf;

	p_82xx_addr_filt =
1775 1776
	    (struct ucc_geth_82xx_address_filtering_pram __iomem *)
	    ugeth->p_rx_glbl_pram->addressfiltering;
1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819

	if (enet_addr_type == ENET_ADDR_TYPE_GROUP) {
		addr_h = &(p_82xx_addr_filt->gaddr_h);
		addr_l = &(p_82xx_addr_filt->gaddr_l);
		p_lh = &ugeth->group_hash_q;
		p_counter = &(ugeth->numGroupAddrInHash);
	} else if (enet_addr_type == ENET_ADDR_TYPE_INDIVIDUAL) {
		addr_h = &(p_82xx_addr_filt->iaddr_h);
		addr_l = &(p_82xx_addr_filt->iaddr_l);
		p_lh = &ugeth->ind_hash_q;
		p_counter = &(ugeth->numIndAddrInHash);
	} else
		return -EINVAL;

	comm_dir = 0;
	if (uccf->enabled_tx)
		comm_dir |= COMM_DIR_TX;
	if (uccf->enabled_rx)
		comm_dir |= COMM_DIR_RX;
	if (comm_dir)
		ugeth_disable(ugeth, comm_dir);

	/* Clear the hash table. */
	out_be32(addr_h, 0x00000000);
	out_be32(addr_l, 0x00000000);

	if (!p_lh)
		return 0;

	num = *p_counter;

	/* Delete all remaining CQ elements */
	for (i = 0; i < num; i++)
		put_enet_addr_container(ENET_ADDR_CONT_ENTRY(dequeue(p_lh)));

	*p_counter = 0;

	if (comm_dir)
		ugeth_enable(ugeth, comm_dir);

	return 0;
}

1820
static int ugeth_82xx_filtering_clear_addr_in_paddr(struct ucc_geth_private *ugeth,
1821 1822 1823 1824 1825 1826
						    u8 paddr_num)
{
	ugeth->indAddrRegUsed[paddr_num] = 0; /* mark this paddr as not used */
	return hw_clear_addr_in_paddr(ugeth, paddr_num);/* clear in hardware */
}

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
static void ucc_geth_free_rx(struct ucc_geth_private *ugeth)
{
	struct ucc_geth_info *ug_info;
	struct ucc_fast_info *uf_info;
	u16 i, j;
	u8 __iomem *bd;


	ug_info = ugeth->ug_info;
	uf_info = &ug_info->uf_info;

	for (i = 0; i < ugeth->ug_info->numQueuesRx; i++) {
		if (ugeth->p_rx_bd_ring[i]) {
			/* Return existing data buffers in ring */
			bd = ugeth->p_rx_bd_ring[i];
			for (j = 0; j < ugeth->ug_info->bdRingLenRx[i]; j++) {
				if (ugeth->rx_skbuff[i][j]) {
					dma_unmap_single(ugeth->dev,
						in_be32(&((struct qe_bd __iomem *)bd)->buf),
						ugeth->ug_info->
						uf_info.max_rx_buf_length +
						UCC_GETH_RX_DATA_BUF_ALIGNMENT,
						DMA_FROM_DEVICE);
					dev_kfree_skb_any(
						ugeth->rx_skbuff[i][j]);
					ugeth->rx_skbuff[i][j] = NULL;
				}
				bd += sizeof(struct qe_bd);
			}

			kfree(ugeth->rx_skbuff[i]);

			if (ugeth->ug_info->uf_info.bd_mem_part ==
			    MEM_PART_SYSTEM)
				kfree((void *)ugeth->rx_bd_ring_offset[i]);
			else if (ugeth->ug_info->uf_info.bd_mem_part ==
				 MEM_PART_MURAM)
				qe_muram_free(ugeth->rx_bd_ring_offset[i]);
			ugeth->p_rx_bd_ring[i] = NULL;
		}
	}

}

static void ucc_geth_free_tx(struct ucc_geth_private *ugeth)
1872
{
1873 1874
	struct ucc_geth_info *ug_info;
	struct ucc_fast_info *uf_info;
1875
	u16 i, j;
1876
	u8 __iomem *bd;
1877

1878 1879
	netdev_reset_queue(ugeth->ndev);

1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915
	ug_info = ugeth->ug_info;
	uf_info = &ug_info->uf_info;

	for (i = 0; i < ugeth->ug_info->numQueuesTx; i++) {
		bd = ugeth->p_tx_bd_ring[i];
		if (!bd)
			continue;
		for (j = 0; j < ugeth->ug_info->bdRingLenTx[i]; j++) {
			if (ugeth->tx_skbuff[i][j]) {
				dma_unmap_single(ugeth->dev,
						 in_be32(&((struct qe_bd __iomem *)bd)->buf),
						 (in_be32((u32 __iomem *)bd) &
						  BD_LENGTH_MASK),
						 DMA_TO_DEVICE);
				dev_kfree_skb_any(ugeth->tx_skbuff[i][j]);
				ugeth->tx_skbuff[i][j] = NULL;
			}
		}

		kfree(ugeth->tx_skbuff[i]);

		if (ugeth->p_tx_bd_ring[i]) {
			if (ugeth->ug_info->uf_info.bd_mem_part ==
			    MEM_PART_SYSTEM)
				kfree((void *)ugeth->tx_bd_ring_offset[i]);
			else if (ugeth->ug_info->uf_info.bd_mem_part ==
				 MEM_PART_MURAM)
				qe_muram_free(ugeth->tx_bd_ring_offset[i]);
			ugeth->p_tx_bd_ring[i] = NULL;
		}
	}

}

static void ucc_geth_memclean(struct ucc_geth_private *ugeth)
{
1916 1917 1918
	if (!ugeth)
		return;

A
Anton Vorontsov 已提交
1919
	if (ugeth->uccf) {
1920
		ucc_fast_free(ugeth->uccf);
A
Anton Vorontsov 已提交
1921 1922
		ugeth->uccf = NULL;
	}
1923

1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956
	qe_muram_free_addr(ugeth->p_thread_data_tx);
	ugeth->p_thread_data_tx = NULL;

	qe_muram_free_addr(ugeth->p_thread_data_rx);
	ugeth->p_thread_data_rx = NULL;

	qe_muram_free_addr(ugeth->p_exf_glbl_param);
	ugeth->p_exf_glbl_param = NULL;

	qe_muram_free_addr(ugeth->p_rx_glbl_pram);
	ugeth->p_rx_glbl_pram = NULL;

	qe_muram_free_addr(ugeth->p_tx_glbl_pram);
	ugeth->p_tx_glbl_pram = NULL;

	qe_muram_free_addr(ugeth->p_send_q_mem_reg);
	ugeth->p_send_q_mem_reg = NULL;

	qe_muram_free_addr(ugeth->p_scheduler);
	ugeth->p_scheduler = NULL;

	qe_muram_free_addr(ugeth->p_tx_fw_statistics_pram);
	ugeth->p_tx_fw_statistics_pram = NULL;

	qe_muram_free_addr(ugeth->p_rx_fw_statistics_pram);
	ugeth->p_rx_fw_statistics_pram = NULL;

	qe_muram_free_addr(ugeth->p_rx_irq_coalescing_tbl);
	ugeth->p_rx_irq_coalescing_tbl = NULL;

	qe_muram_free_addr(ugeth->p_rx_bd_qs_tbl);
	ugeth->p_rx_bd_qs_tbl = NULL;

1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970
	if (ugeth->p_init_enet_param_shadow) {
		return_init_enet_entries(ugeth,
					 &(ugeth->p_init_enet_param_shadow->
					   rxthread[0]),
					 ENET_INIT_PARAM_MAX_ENTRIES_RX,
					 ugeth->ug_info->riscRx, 1);
		return_init_enet_entries(ugeth,
					 &(ugeth->p_init_enet_param_shadow->
					   txthread[0]),
					 ENET_INIT_PARAM_MAX_ENTRIES_TX,
					 ugeth->ug_info->riscTx, 0);
		kfree(ugeth->p_init_enet_param_shadow);
		ugeth->p_init_enet_param_shadow = NULL;
	}
1971 1972
	ucc_geth_free_tx(ugeth);
	ucc_geth_free_rx(ugeth);
1973 1974 1975 1976 1977 1978
	while (!list_empty(&ugeth->group_hash_q))
		put_enet_addr_container(ENET_ADDR_CONT_ENTRY
					(dequeue(&ugeth->group_hash_q)));
	while (!list_empty(&ugeth->ind_hash_q))
		put_enet_addr_container(ENET_ADDR_CONT_ENTRY
					(dequeue(&ugeth->ind_hash_q)));
1979 1980 1981 1982
	if (ugeth->ug_regs) {
		iounmap(ugeth->ug_regs);
		ugeth->ug_regs = NULL;
	}
1983 1984 1985 1986
}

static void ucc_geth_set_multi(struct net_device *dev)
{
1987
	struct ucc_geth_private *ugeth;
1988
	struct netdev_hw_addr *ha;
1989 1990
	struct ucc_fast __iomem *uf_regs;
	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
1991 1992 1993 1994 1995 1996

	ugeth = netdev_priv(dev);

	uf_regs = ugeth->uccf->uf_regs;

	if (dev->flags & IFF_PROMISC) {
T
Timur Tabi 已提交
1997
		setbits32(&uf_regs->upsmr, UCC_GETH_UPSMR_PRO);
1998
	} else {
T
Timur Tabi 已提交
1999
		clrbits32(&uf_regs->upsmr, UCC_GETH_UPSMR_PRO);
2000 2001

		p_82xx_addr_filt =
2002
		    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->
2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
		    p_rx_glbl_pram->addressfiltering;

		if (dev->flags & IFF_ALLMULTI) {
			/* Catch all multicast addresses, so set the
			 * filter to all 1's.
			 */
			out_be32(&p_82xx_addr_filt->gaddr_h, 0xffffffff);
			out_be32(&p_82xx_addr_filt->gaddr_l, 0xffffffff);
		} else {
			/* Clear filter and add the addresses in the list.
			 */
			out_be32(&p_82xx_addr_filt->gaddr_h, 0x0);
			out_be32(&p_82xx_addr_filt->gaddr_l, 0x0);

2017
			netdev_for_each_mc_addr(ha, dev) {
2018 2019 2020
				/* Ask CPM to run CRC and set bit in
				 * filter mask.
				 */
2021
				hw_add_addr_in_hash(ugeth, ha->addr);
2022 2023 2024 2025 2026
			}
		}
	}
}

2027
static void ucc_geth_stop(struct ucc_geth_private *ugeth)
2028
{
2029
	struct ucc_geth __iomem *ug_regs = ugeth->ug_regs;
2030
	struct phy_device *phydev = ugeth->phydev;
2031

2032
	ugeth_vdbg("%s: IN", __func__);
2033

J
Joakim Tjernlund 已提交
2034 2035 2036 2037 2038 2039 2040
	/*
	 * Tell the kernel the link is down.
	 * Must be done before disabling the controller
	 * or deadlock may happen.
	 */
	phy_stop(phydev);

2041 2042 2043 2044
	/* Disable the controller */
	ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);

	/* Mask all interrupts */
2045
	out_be32(ugeth->uccf->p_uccm, 0x00000000);
2046 2047 2048 2049 2050

	/* Clear all interrupts */
	out_be32(ugeth->uccf->p_ucce, 0xffffffff);

	/* Disable Rx and Tx */
T
Timur Tabi 已提交
2051
	clrbits32(&ug_regs->maccfg1, MACCFG1_ENABLE_RX | MACCFG1_ENABLE_TX);
2052 2053 2054 2055

	ucc_geth_memclean(ugeth);
}

2056
static int ucc_struct_init(struct ucc_geth_private *ugeth)
2057
{
2058 2059
	struct ucc_geth_info *ug_info;
	struct ucc_fast_info *uf_info;
2060
	int i;
2061 2062 2063 2064 2065 2066

	ug_info = ugeth->ug_info;
	uf_info = &ug_info->uf_info;

	if (!((uf_info->bd_mem_part == MEM_PART_SYSTEM) ||
	      (uf_info->bd_mem_part == MEM_PART_MURAM))) {
2067
		if (netif_msg_probe(ugeth))
2068
			pr_err("Bad memory partition value\n");
2069 2070 2071 2072 2073 2074 2075 2076
		return -EINVAL;
	}

	/* Rx BD lengths */
	for (i = 0; i < ug_info->numQueuesRx; i++) {
		if ((ug_info->bdRingLenRx[i] < UCC_GETH_RX_BD_RING_SIZE_MIN) ||
		    (ug_info->bdRingLenRx[i] %
		     UCC_GETH_RX_BD_RING_SIZE_ALIGNMENT)) {
2077
			if (netif_msg_probe(ugeth))
2078
				pr_err("Rx BD ring length must be multiple of 4, no smaller than 8\n");
2079 2080 2081 2082 2083 2084 2085
			return -EINVAL;
		}
	}

	/* Tx BD lengths */
	for (i = 0; i < ug_info->numQueuesTx; i++) {
		if (ug_info->bdRingLenTx[i] < UCC_GETH_TX_BD_RING_SIZE_MIN) {
2086
			if (netif_msg_probe(ugeth))
2087
				pr_err("Tx BD ring length must be no smaller than 2\n");
2088 2089 2090 2091 2092 2093 2094
			return -EINVAL;
		}
	}

	/* mrblr */
	if ((uf_info->max_rx_buf_length == 0) ||
	    (uf_info->max_rx_buf_length % UCC_GETH_MRBLR_ALIGNMENT)) {
2095
		if (netif_msg_probe(ugeth))
2096
			pr_err("max_rx_buf_length must be non-zero multiple of 128\n");
2097 2098 2099 2100 2101
		return -EINVAL;
	}

	/* num Tx queues */
	if (ug_info->numQueuesTx > NUM_TX_QUEUES) {
2102
		if (netif_msg_probe(ugeth))
2103
			pr_err("number of tx queues too large\n");
2104 2105 2106 2107 2108
		return -EINVAL;
	}

	/* num Rx queues */
	if (ug_info->numQueuesRx > NUM_RX_QUEUES) {
2109
		if (netif_msg_probe(ugeth))
2110
			pr_err("number of rx queues too large\n");
2111 2112 2113 2114 2115 2116
		return -EINVAL;
	}

	/* l2qt */
	for (i = 0; i < UCC_GETH_VLAN_PRIORITY_MAX; i++) {
		if (ug_info->l2qt[i] >= ug_info->numQueuesRx) {
2117
			if (netif_msg_probe(ugeth))
2118
				pr_err("VLAN priority table entry must not be larger than number of Rx queues\n");
2119 2120 2121 2122 2123 2124 2125
			return -EINVAL;
		}
	}

	/* l3qt */
	for (i = 0; i < UCC_GETH_IP_PRIORITY_MAX; i++) {
		if (ug_info->l3qt[i] >= ug_info->numQueuesRx) {
2126
			if (netif_msg_probe(ugeth))
2127
				pr_err("IP priority table entry must not be larger than number of Rx queues\n");
2128 2129 2130 2131 2132
			return -EINVAL;
		}
	}

	if (ug_info->cam && !ug_info->ecamptr) {
2133
		if (netif_msg_probe(ugeth))
2134
			pr_err("If cam mode is chosen, must supply cam ptr\n");
2135 2136 2137 2138
		return -EINVAL;
	}

	if ((ug_info->numStationAddresses !=
2139 2140
	     UCC_GETH_NUM_OF_STATION_ADDRESSES_1) &&
	    ug_info->rxExtendedFiltering) {
2141
		if (netif_msg_probe(ugeth))
2142
			pr_err("Number of station addresses greater than 1 not allowed in extended parsing mode\n");
2143 2144 2145 2146 2147 2148
		return -EINVAL;
	}

	/* Generate uccm_mask for receive */
	uf_info->uccm_mask = ug_info->eventRegMask & UCCE_OTHER;/* Errors */
	for (i = 0; i < ug_info->numQueuesRx; i++)
T
Timur Tabi 已提交
2149
		uf_info->uccm_mask |= (UCC_GETH_UCCE_RXF0 << i);
2150 2151

	for (i = 0; i < ug_info->numQueuesTx; i++)
T
Timur Tabi 已提交
2152
		uf_info->uccm_mask |= (UCC_GETH_UCCE_TXB0 << i);
2153
	/* Initialize the general fast UCC block. */
2154
	if (ucc_fast_init(uf_info, &ugeth->uccf)) {
2155
		if (netif_msg_probe(ugeth))
2156
			pr_err("Failed to init uccf\n");
2157 2158
		return -ENOMEM;
	}
2159

2160 2161 2162 2163 2164 2165 2166 2167
	/* read the number of risc engines, update the riscTx and riscRx
	 * if there are 4 riscs in QE
	 */
	if (qe_get_num_of_risc() == 4) {
		ug_info->riscTx = QE_RISC_ALLOCATION_FOUR_RISCS;
		ug_info->riscRx = QE_RISC_ALLOCATION_FOUR_RISCS;
	}

2168 2169 2170
	ugeth->ug_regs = ioremap(uf_info->regs, sizeof(*ugeth->ug_regs));
	if (!ugeth->ug_regs) {
		if (netif_msg_probe(ugeth))
2171
			pr_err("Failed to ioremap regs\n");
2172 2173
		return -ENOMEM;
	}
2174 2175 2176 2177

	return 0;
}

2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221
static int ucc_geth_alloc_tx(struct ucc_geth_private *ugeth)
{
	struct ucc_geth_info *ug_info;
	struct ucc_fast_info *uf_info;
	int length;
	u16 i, j;
	u8 __iomem *bd;

	ug_info = ugeth->ug_info;
	uf_info = &ug_info->uf_info;

	/* Allocate Tx bds */
	for (j = 0; j < ug_info->numQueuesTx; j++) {
		/* Allocate in multiple of
		   UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT,
		   according to spec */
		length = ((ug_info->bdRingLenTx[j] * sizeof(struct qe_bd))
			  / UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT)
		    * UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
		if ((ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)) %
		    UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT)
			length += UCC_GETH_TX_BD_RING_SIZE_MEMORY_ALIGNMENT;
		if (uf_info->bd_mem_part == MEM_PART_SYSTEM) {
			u32 align = 4;
			if (UCC_GETH_TX_BD_RING_ALIGNMENT > 4)
				align = UCC_GETH_TX_BD_RING_ALIGNMENT;
			ugeth->tx_bd_ring_offset[j] =
				(u32) kmalloc((u32) (length + align), GFP_KERNEL);

			if (ugeth->tx_bd_ring_offset[j] != 0)
				ugeth->p_tx_bd_ring[j] =
					(u8 __iomem *)((ugeth->tx_bd_ring_offset[j] +
					align) & ~(align - 1));
		} else if (uf_info->bd_mem_part == MEM_PART_MURAM) {
			ugeth->tx_bd_ring_offset[j] =
			    qe_muram_alloc(length,
					   UCC_GETH_TX_BD_RING_ALIGNMENT);
			if (!IS_ERR_VALUE(ugeth->tx_bd_ring_offset[j]))
				ugeth->p_tx_bd_ring[j] =
				    (u8 __iomem *) qe_muram_addr(ugeth->
							 tx_bd_ring_offset[j]);
		}
		if (!ugeth->p_tx_bd_ring[j]) {
			if (netif_msg_ifup(ugeth))
2222
				pr_err("Can not allocate memory for Tx bd rings\n");
2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233
			return -ENOMEM;
		}
		/* Zero unused end of bd ring, according to spec */
		memset_io((void __iomem *)(ugeth->p_tx_bd_ring[j] +
		       ug_info->bdRingLenTx[j] * sizeof(struct qe_bd)), 0,
		       length - ug_info->bdRingLenTx[j] * sizeof(struct qe_bd));
	}

	/* Init Tx bds */
	for (j = 0; j < ug_info->numQueuesTx; j++) {
		/* Setup the skbuff rings */
2234 2235 2236
		ugeth->tx_skbuff[j] =
			kmalloc_array(ugeth->ug_info->bdRingLenTx[j],
				      sizeof(struct sk_buff *), GFP_KERNEL);
2237 2238 2239

		if (ugeth->tx_skbuff[j] == NULL) {
			if (netif_msg_ifup(ugeth))
2240
				pr_err("Could not allocate tx_skbuff\n");
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298
			return -ENOMEM;
		}

		for (i = 0; i < ugeth->ug_info->bdRingLenTx[j]; i++)
			ugeth->tx_skbuff[j][i] = NULL;

		ugeth->skb_curtx[j] = ugeth->skb_dirtytx[j] = 0;
		bd = ugeth->confBd[j] = ugeth->txBd[j] = ugeth->p_tx_bd_ring[j];
		for (i = 0; i < ug_info->bdRingLenTx[j]; i++) {
			/* clear bd buffer */
			out_be32(&((struct qe_bd __iomem *)bd)->buf, 0);
			/* set bd status and length */
			out_be32((u32 __iomem *)bd, 0);
			bd += sizeof(struct qe_bd);
		}
		bd -= sizeof(struct qe_bd);
		/* set bd status and length */
		out_be32((u32 __iomem *)bd, T_W); /* for last BD set Wrap bit */
	}

	return 0;
}

static int ucc_geth_alloc_rx(struct ucc_geth_private *ugeth)
{
	struct ucc_geth_info *ug_info;
	struct ucc_fast_info *uf_info;
	int length;
	u16 i, j;
	u8 __iomem *bd;

	ug_info = ugeth->ug_info;
	uf_info = &ug_info->uf_info;

	/* Allocate Rx bds */
	for (j = 0; j < ug_info->numQueuesRx; j++) {
		length = ug_info->bdRingLenRx[j] * sizeof(struct qe_bd);
		if (uf_info->bd_mem_part == MEM_PART_SYSTEM) {
			u32 align = 4;
			if (UCC_GETH_RX_BD_RING_ALIGNMENT > 4)
				align = UCC_GETH_RX_BD_RING_ALIGNMENT;
			ugeth->rx_bd_ring_offset[j] =
				(u32) kmalloc((u32) (length + align), GFP_KERNEL);
			if (ugeth->rx_bd_ring_offset[j] != 0)
				ugeth->p_rx_bd_ring[j] =
					(u8 __iomem *)((ugeth->rx_bd_ring_offset[j] +
					align) & ~(align - 1));
		} else if (uf_info->bd_mem_part == MEM_PART_MURAM) {
			ugeth->rx_bd_ring_offset[j] =
			    qe_muram_alloc(length,
					   UCC_GETH_RX_BD_RING_ALIGNMENT);
			if (!IS_ERR_VALUE(ugeth->rx_bd_ring_offset[j]))
				ugeth->p_rx_bd_ring[j] =
				    (u8 __iomem *) qe_muram_addr(ugeth->
							 rx_bd_ring_offset[j]);
		}
		if (!ugeth->p_rx_bd_ring[j]) {
			if (netif_msg_ifup(ugeth))
2299
				pr_err("Can not allocate memory for Rx bd rings\n");
2300 2301 2302 2303 2304 2305 2306
			return -ENOMEM;
		}
	}

	/* Init Rx bds */
	for (j = 0; j < ug_info->numQueuesRx; j++) {
		/* Setup the skbuff rings */
2307 2308 2309
		ugeth->rx_skbuff[j] =
			kmalloc_array(ugeth->ug_info->bdRingLenRx[j],
				      sizeof(struct sk_buff *), GFP_KERNEL);
2310 2311 2312

		if (ugeth->rx_skbuff[j] == NULL) {
			if (netif_msg_ifup(ugeth))
2313
				pr_err("Could not allocate rx_skbuff\n");
2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336
			return -ENOMEM;
		}

		for (i = 0; i < ugeth->ug_info->bdRingLenRx[j]; i++)
			ugeth->rx_skbuff[j][i] = NULL;

		ugeth->skb_currx[j] = 0;
		bd = ugeth->rxBd[j] = ugeth->p_rx_bd_ring[j];
		for (i = 0; i < ug_info->bdRingLenRx[j]; i++) {
			/* set bd status and length */
			out_be32((u32 __iomem *)bd, R_I);
			/* clear bd buffer */
			out_be32(&((struct qe_bd __iomem *)bd)->buf, 0);
			bd += sizeof(struct qe_bd);
		}
		bd -= sizeof(struct qe_bd);
		/* set bd status and length */
		out_be32((u32 __iomem *)bd, R_W); /* for last BD set Wrap bit */
	}

	return 0;
}

2337 2338
static int ucc_geth_startup(struct ucc_geth_private *ugeth)
{
2339 2340
	struct ucc_geth_82xx_address_filtering_pram __iomem *p_82xx_addr_filt;
	struct ucc_geth_init_pram __iomem *p_init_enet_pram;
2341 2342 2343
	struct ucc_fast_private *uccf;
	struct ucc_geth_info *ug_info;
	struct ucc_fast_info *uf_info;
2344 2345
	struct ucc_fast __iomem *uf_regs;
	struct ucc_geth __iomem *ug_regs;
2346 2347
	int ret_val = -EINVAL;
	u32 remoder = UCC_GETH_REMODER_INIT;
T
Timur Tabi 已提交
2348
	u32 init_enet_pram_offset, cecr_subblock, command;
2349
	u32 ifstat, i, j, size, l2qt, l3qt;
2350 2351
	u16 temoder = UCC_GETH_TEMODER_INIT;
	u8 function_code = 0;
2352
	u8 __iomem *endOfRing;
2353 2354
	u8 numThreadsRxNumerical, numThreadsTxNumerical;

2355
	ugeth_vdbg("%s: IN", __func__);
2356 2357 2358 2359 2360
	uccf = ugeth->uccf;
	ug_info = ugeth->ug_info;
	uf_info = &ug_info->uf_info;
	uf_regs = uccf->uf_regs;
	ug_regs = ugeth->ug_regs;
2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378

	switch (ug_info->numThreadsRx) {
	case UCC_GETH_NUM_OF_THREADS_1:
		numThreadsRxNumerical = 1;
		break;
	case UCC_GETH_NUM_OF_THREADS_2:
		numThreadsRxNumerical = 2;
		break;
	case UCC_GETH_NUM_OF_THREADS_4:
		numThreadsRxNumerical = 4;
		break;
	case UCC_GETH_NUM_OF_THREADS_6:
		numThreadsRxNumerical = 6;
		break;
	case UCC_GETH_NUM_OF_THREADS_8:
		numThreadsRxNumerical = 8;
		break;
	default:
2379
		if (netif_msg_ifup(ugeth))
2380
			pr_err("Bad number of Rx threads value\n");
2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400
		return -EINVAL;
	}

	switch (ug_info->numThreadsTx) {
	case UCC_GETH_NUM_OF_THREADS_1:
		numThreadsTxNumerical = 1;
		break;
	case UCC_GETH_NUM_OF_THREADS_2:
		numThreadsTxNumerical = 2;
		break;
	case UCC_GETH_NUM_OF_THREADS_4:
		numThreadsTxNumerical = 4;
		break;
	case UCC_GETH_NUM_OF_THREADS_6:
		numThreadsTxNumerical = 6;
		break;
	case UCC_GETH_NUM_OF_THREADS_8:
		numThreadsTxNumerical = 8;
		break;
	default:
2401
		if (netif_msg_ifup(ugeth))
2402
			pr_err("Bad number of Tx threads value\n");
2403 2404 2405 2406 2407 2408 2409 2410 2411 2412
		return -EINVAL;
	}

	/* Calculate rx_extended_features */
	ugeth->rx_non_dynamic_extended_features = ug_info->ipCheckSumCheck ||
	    ug_info->ipAddressAlignment ||
	    (ug_info->numStationAddresses !=
	     UCC_GETH_NUM_OF_STATION_ADDRESSES_1);

	ugeth->rx_extended_features = ugeth->rx_non_dynamic_extended_features ||
2413 2414 2415
		(ug_info->vlanOperationTagged != UCC_GETH_VLAN_OPERATION_TAGGED_NOP) ||
		(ug_info->vlanOperationNonTagged !=
		 UCC_GETH_VLAN_OPERATION_NON_TAGGED_NOP);
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431

	init_default_reg_vals(&uf_regs->upsmr,
			      &ug_regs->maccfg1, &ug_regs->maccfg2);

	/*                    Set UPSMR                      */
	/* For more details see the hardware spec.           */
	init_rx_parameters(ug_info->bro,
			   ug_info->rsh, ug_info->pro, &uf_regs->upsmr);

	/* We're going to ignore other registers for now, */
	/* except as needed to get up and running         */

	/*                    Set MACCFG1                    */
	/* For more details see the hardware spec.           */
	init_flow_control_params(ug_info->aufc,
				 ug_info->receiveFlowControl,
L
Li Yang 已提交
2432
				 ug_info->transmitFlowControl,
2433 2434 2435 2436 2437
				 ug_info->pausePeriod,
				 ug_info->extensionField,
				 &uf_regs->upsmr,
				 &ug_regs->uempr, &ug_regs->maccfg1);

T
Timur Tabi 已提交
2438
	setbits32(&ug_regs->maccfg1, MACCFG1_ENABLE_RX | MACCFG1_ENABLE_TX);
2439 2440 2441 2442 2443 2444 2445 2446 2447 2448

	/*                    Set IPGIFG                     */
	/* For more details see the hardware spec.           */
	ret_val = init_inter_frame_gap_params(ug_info->nonBackToBackIfgPart1,
					      ug_info->nonBackToBackIfgPart2,
					      ug_info->
					      miminumInterFrameGapEnforcement,
					      ug_info->backToBackInterFrameGap,
					      &ug_regs->ipgifg);
	if (ret_val != 0) {
2449
		if (netif_msg_ifup(ugeth))
2450
			pr_err("IPGIFG initialization parameter too large\n");
2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464
		return ret_val;
	}

	/*                    Set HAFDUP                     */
	/* For more details see the hardware spec.           */
	ret_val = init_half_duplex_params(ug_info->altBeb,
					  ug_info->backPressureNoBackoff,
					  ug_info->noBackoff,
					  ug_info->excessDefer,
					  ug_info->altBebTruncation,
					  ug_info->maxRetransmission,
					  ug_info->collisionWindow,
					  &ug_regs->hafdup);
	if (ret_val != 0) {
2465
		if (netif_msg_ifup(ugeth))
2466
			pr_err("Half Duplex initialization parameter too large\n");
2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484
		return ret_val;
	}

	/*                    Set IFSTAT                     */
	/* For more details see the hardware spec.           */
	/* Read only - resets upon read                      */
	ifstat = in_be32(&ug_regs->ifstat);

	/*                    Clear UEMPR                    */
	/* For more details see the hardware spec.           */
	out_be32(&ug_regs->uempr, 0);

	/*                    Set UESCR                      */
	/* For more details see the hardware spec.           */
	init_hw_statistics_gathering_mode((ug_info->statisticsMode &
				UCC_GETH_STATISTICS_GATHERING_MODE_HARDWARE),
				0, &uf_regs->upsmr, &ug_regs->uescr);

2485 2486 2487
	ret_val = ucc_geth_alloc_tx(ugeth);
	if (ret_val != 0)
		return ret_val;
2488

2489 2490 2491
	ret_val = ucc_geth_alloc_rx(ugeth);
	if (ret_val != 0)
		return ret_val;
2492 2493 2494 2495 2496 2497 2498

	/*
	 * Global PRAM
	 */
	/* Tx global PRAM */
	/* Allocate global tx parameter RAM page */
	ugeth->tx_glbl_pram_offset =
2499
	    qe_muram_alloc(sizeof(struct ucc_geth_tx_global_pram),
2500
			   UCC_GETH_TX_GLOBAL_PRAM_ALIGNMENT);
2501
	if (IS_ERR_VALUE(ugeth->tx_glbl_pram_offset)) {
2502
		if (netif_msg_ifup(ugeth))
2503
			pr_err("Can not allocate DPRAM memory for p_tx_glbl_pram\n");
2504 2505 2506
		return -ENOMEM;
	}
	ugeth->p_tx_glbl_pram =
2507
	    (struct ucc_geth_tx_global_pram __iomem *) qe_muram_addr(ugeth->
2508 2509 2510 2511 2512 2513 2514
							tx_glbl_pram_offset);
	/* Fill global PRAM */

	/* TQPTR */
	/* Size varies with number of Tx threads */
	ugeth->thread_dat_tx_offset =
	    qe_muram_alloc(numThreadsTxNumerical *
2515
			   sizeof(struct ucc_geth_thread_data_tx) +
2516 2517
			   32 * (numThreadsTxNumerical == 1),
			   UCC_GETH_THREAD_DATA_ALIGNMENT);
2518
	if (IS_ERR_VALUE(ugeth->thread_dat_tx_offset)) {
2519
		if (netif_msg_ifup(ugeth))
2520
			pr_err("Can not allocate DPRAM memory for p_thread_data_tx\n");
2521 2522 2523 2524
		return -ENOMEM;
	}

	ugeth->p_thread_data_tx =
2525
	    (struct ucc_geth_thread_data_tx __iomem *) qe_muram_addr(ugeth->
2526 2527 2528 2529 2530 2531 2532 2533 2534 2535
							thread_dat_tx_offset);
	out_be32(&ugeth->p_tx_glbl_pram->tqptr, ugeth->thread_dat_tx_offset);

	/* vtagtable */
	for (i = 0; i < UCC_GETH_TX_VTAG_TABLE_ENTRY_MAX; i++)
		out_be32(&ugeth->p_tx_glbl_pram->vtagtable[i],
			 ug_info->vtagtable[i]);

	/* iphoffset */
	for (i = 0; i < TX_IP_OFFSET_ENTRY_MAX; i++)
2536 2537
		out_8(&ugeth->p_tx_glbl_pram->iphoffset[i],
				ug_info->iphoffset[i]);
2538 2539 2540 2541 2542

	/* SQPTR */
	/* Size varies with number of Tx queues */
	ugeth->send_q_mem_reg_offset =
	    qe_muram_alloc(ug_info->numQueuesTx *
2543
			   sizeof(struct ucc_geth_send_queue_qd),
2544
			   UCC_GETH_SEND_QUEUE_QUEUE_DESCRIPTOR_ALIGNMENT);
2545
	if (IS_ERR_VALUE(ugeth->send_q_mem_reg_offset)) {
2546
		if (netif_msg_ifup(ugeth))
2547
			pr_err("Can not allocate DPRAM memory for p_send_q_mem_reg\n");
2548 2549 2550 2551
		return -ENOMEM;
	}

	ugeth->p_send_q_mem_reg =
2552
	    (struct ucc_geth_send_queue_mem_region __iomem *) qe_muram_addr(ugeth->
2553 2554 2555 2556 2557 2558 2559 2560
			send_q_mem_reg_offset);
	out_be32(&ugeth->p_tx_glbl_pram->sqptr, ugeth->send_q_mem_reg_offset);

	/* Setup the table */
	/* Assume BD rings are already established */
	for (i = 0; i < ug_info->numQueuesTx; i++) {
		endOfRing =
		    ugeth->p_tx_bd_ring[i] + (ug_info->bdRingLenTx[i] -
2561
					      1) * sizeof(struct qe_bd);
2562 2563 2564 2565 2566 2567 2568 2569 2570
		if (ugeth->ug_info->uf_info.bd_mem_part == MEM_PART_SYSTEM) {
			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].bd_ring_base,
				 (u32) virt_to_phys(ugeth->p_tx_bd_ring[i]));
			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].
				 last_bd_completed_address,
				 (u32) virt_to_phys(endOfRing));
		} else if (ugeth->ug_info->uf_info.bd_mem_part ==
			   MEM_PART_MURAM) {
			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].bd_ring_base,
2571
				 (u32)qe_muram_dma(ugeth->p_tx_bd_ring[i]));
2572 2573
			out_be32(&ugeth->p_send_q_mem_reg->sqqd[i].
				 last_bd_completed_address,
2574
				 (u32)qe_muram_dma(endOfRing));
2575 2576 2577 2578 2579 2580 2581 2582
		}
	}

	/* schedulerbasepointer */

	if (ug_info->numQueuesTx > 1) {
	/* scheduler exists only if more than 1 tx queue */
		ugeth->scheduler_offset =
2583
		    qe_muram_alloc(sizeof(struct ucc_geth_scheduler),
2584
				   UCC_GETH_SCHEDULER_ALIGNMENT);
2585
		if (IS_ERR_VALUE(ugeth->scheduler_offset)) {
2586
			if (netif_msg_ifup(ugeth))
2587
				pr_err("Can not allocate DPRAM memory for p_scheduler\n");
2588 2589 2590 2591
			return -ENOMEM;
		}

		ugeth->p_scheduler =
2592
		    (struct ucc_geth_scheduler __iomem *) qe_muram_addr(ugeth->
2593 2594 2595 2596 2597 2598 2599 2600 2601
							   scheduler_offset);
		out_be32(&ugeth->p_tx_glbl_pram->schedulerbasepointer,
			 ugeth->scheduler_offset);

		/* Set values in scheduler */
		out_be32(&ugeth->p_scheduler->mblinterval,
			 ug_info->mblinterval);
		out_be16(&ugeth->p_scheduler->nortsrbytetime,
			 ug_info->nortsrbytetime);
2602 2603 2604 2605 2606
		out_8(&ugeth->p_scheduler->fracsiz, ug_info->fracsiz);
		out_8(&ugeth->p_scheduler->strictpriorityq,
				ug_info->strictpriorityq);
		out_8(&ugeth->p_scheduler->txasap, ug_info->txasap);
		out_8(&ugeth->p_scheduler->extrabw, ug_info->extrabw);
2607
		for (i = 0; i < NUM_TX_QUEUES; i++)
2608 2609
			out_8(&ugeth->p_scheduler->weightfactor[i],
			    ug_info->weightfactor[i]);
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627

		/* Set pointers to cpucount registers in scheduler */
		ugeth->p_cpucount[0] = &(ugeth->p_scheduler->cpucount0);
		ugeth->p_cpucount[1] = &(ugeth->p_scheduler->cpucount1);
		ugeth->p_cpucount[2] = &(ugeth->p_scheduler->cpucount2);
		ugeth->p_cpucount[3] = &(ugeth->p_scheduler->cpucount3);
		ugeth->p_cpucount[4] = &(ugeth->p_scheduler->cpucount4);
		ugeth->p_cpucount[5] = &(ugeth->p_scheduler->cpucount5);
		ugeth->p_cpucount[6] = &(ugeth->p_scheduler->cpucount6);
		ugeth->p_cpucount[7] = &(ugeth->p_scheduler->cpucount7);
	}

	/* schedulerbasepointer */
	/* TxRMON_PTR (statistics) */
	if (ug_info->
	    statisticsMode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX) {
		ugeth->tx_fw_statistics_pram_offset =
		    qe_muram_alloc(sizeof
2628
				   (struct ucc_geth_tx_firmware_statistics_pram),
2629
				   UCC_GETH_TX_STATISTICS_ALIGNMENT);
2630
		if (IS_ERR_VALUE(ugeth->tx_fw_statistics_pram_offset)) {
2631
			if (netif_msg_ifup(ugeth))
2632
				pr_err("Can not allocate DPRAM memory for p_tx_fw_statistics_pram\n");
2633 2634 2635
			return -ENOMEM;
		}
		ugeth->p_tx_fw_statistics_pram =
2636
		    (struct ucc_geth_tx_firmware_statistics_pram __iomem *)
2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650
		    qe_muram_addr(ugeth->tx_fw_statistics_pram_offset);
	}

	/* temoder */
	/* Already has speed set */

	if (ug_info->numQueuesTx > 1)
		temoder |= TEMODER_SCHEDULER_ENABLE;
	if (ug_info->ipCheckSumGenerate)
		temoder |= TEMODER_IP_CHECKSUM_GENERATE;
	temoder |= ((ug_info->numQueuesTx - 1) << TEMODER_NUM_OF_QUEUES_SHIFT);
	out_be16(&ugeth->p_tx_glbl_pram->temoder, temoder);

	/* Function code register value to be used later */
2651
	function_code = UCC_BMR_BO_BE | UCC_BMR_GBL;
2652 2653 2654 2655 2656 2657 2658 2659
	/* Required for QE */

	/* function code register */
	out_be32(&ugeth->p_tx_glbl_pram->tstate, ((u32) function_code) << 24);

	/* Rx global PRAM */
	/* Allocate global rx parameter RAM page */
	ugeth->rx_glbl_pram_offset =
2660
	    qe_muram_alloc(sizeof(struct ucc_geth_rx_global_pram),
2661
			   UCC_GETH_RX_GLOBAL_PRAM_ALIGNMENT);
2662
	if (IS_ERR_VALUE(ugeth->rx_glbl_pram_offset)) {
2663
		if (netif_msg_ifup(ugeth))
2664
			pr_err("Can not allocate DPRAM memory for p_rx_glbl_pram\n");
2665 2666 2667
		return -ENOMEM;
	}
	ugeth->p_rx_glbl_pram =
2668
	    (struct ucc_geth_rx_global_pram __iomem *) qe_muram_addr(ugeth->
2669 2670 2671 2672 2673 2674 2675
							rx_glbl_pram_offset);
	/* Fill global PRAM */

	/* RQPTR */
	/* Size varies with number of Rx threads */
	ugeth->thread_dat_rx_offset =
	    qe_muram_alloc(numThreadsRxNumerical *
2676
			   sizeof(struct ucc_geth_thread_data_rx),
2677
			   UCC_GETH_THREAD_DATA_ALIGNMENT);
2678
	if (IS_ERR_VALUE(ugeth->thread_dat_rx_offset)) {
2679
		if (netif_msg_ifup(ugeth))
2680
			pr_err("Can not allocate DPRAM memory for p_thread_data_rx\n");
2681 2682 2683 2684
		return -ENOMEM;
	}

	ugeth->p_thread_data_rx =
2685
	    (struct ucc_geth_thread_data_rx __iomem *) qe_muram_addr(ugeth->
2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696
							thread_dat_rx_offset);
	out_be32(&ugeth->p_rx_glbl_pram->rqptr, ugeth->thread_dat_rx_offset);

	/* typeorlen */
	out_be16(&ugeth->p_rx_glbl_pram->typeorlen, ug_info->typeorlen);

	/* rxrmonbaseptr (statistics) */
	if (ug_info->
	    statisticsMode & UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX) {
		ugeth->rx_fw_statistics_pram_offset =
		    qe_muram_alloc(sizeof
2697
				   (struct ucc_geth_rx_firmware_statistics_pram),
2698
				   UCC_GETH_RX_STATISTICS_ALIGNMENT);
2699
		if (IS_ERR_VALUE(ugeth->rx_fw_statistics_pram_offset)) {
2700
			if (netif_msg_ifup(ugeth))
2701
				pr_err("Can not allocate DPRAM memory for p_rx_fw_statistics_pram\n");
2702 2703 2704
			return -ENOMEM;
		}
		ugeth->p_rx_fw_statistics_pram =
2705
		    (struct ucc_geth_rx_firmware_statistics_pram __iomem *)
2706 2707 2708 2709 2710 2711 2712 2713
		    qe_muram_addr(ugeth->rx_fw_statistics_pram_offset);
	}

	/* intCoalescingPtr */

	/* Size varies with number of Rx queues */
	ugeth->rx_irq_coalescing_tbl_offset =
	    qe_muram_alloc(ug_info->numQueuesRx *
2714 2715
			   sizeof(struct ucc_geth_rx_interrupt_coalescing_entry)
			   + 4, UCC_GETH_RX_INTERRUPT_COALESCING_ALIGNMENT);
2716
	if (IS_ERR_VALUE(ugeth->rx_irq_coalescing_tbl_offset)) {
2717
		if (netif_msg_ifup(ugeth))
2718
			pr_err("Can not allocate DPRAM memory for p_rx_irq_coalescing_tbl\n");
2719 2720 2721 2722
		return -ENOMEM;
	}

	ugeth->p_rx_irq_coalescing_tbl =
2723
	    (struct ucc_geth_rx_interrupt_coalescing_table __iomem *)
2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762
	    qe_muram_addr(ugeth->rx_irq_coalescing_tbl_offset);
	out_be32(&ugeth->p_rx_glbl_pram->intcoalescingptr,
		 ugeth->rx_irq_coalescing_tbl_offset);

	/* Fill interrupt coalescing table */
	for (i = 0; i < ug_info->numQueuesRx; i++) {
		out_be32(&ugeth->p_rx_irq_coalescing_tbl->coalescingentry[i].
			 interruptcoalescingmaxvalue,
			 ug_info->interruptcoalescingmaxvalue[i]);
		out_be32(&ugeth->p_rx_irq_coalescing_tbl->coalescingentry[i].
			 interruptcoalescingcounter,
			 ug_info->interruptcoalescingmaxvalue[i]);
	}

	/* MRBLR */
	init_max_rx_buff_len(uf_info->max_rx_buf_length,
			     &ugeth->p_rx_glbl_pram->mrblr);
	/* MFLR */
	out_be16(&ugeth->p_rx_glbl_pram->mflr, ug_info->maxFrameLength);
	/* MINFLR */
	init_min_frame_len(ug_info->minFrameLength,
			   &ugeth->p_rx_glbl_pram->minflr,
			   &ugeth->p_rx_glbl_pram->mrblr);
	/* MAXD1 */
	out_be16(&ugeth->p_rx_glbl_pram->maxd1, ug_info->maxD1Length);
	/* MAXD2 */
	out_be16(&ugeth->p_rx_glbl_pram->maxd2, ug_info->maxD2Length);

	/* l2qt */
	l2qt = 0;
	for (i = 0; i < UCC_GETH_VLAN_PRIORITY_MAX; i++)
		l2qt |= (ug_info->l2qt[i] << (28 - 4 * i));
	out_be32(&ugeth->p_rx_glbl_pram->l2qt, l2qt);

	/* l3qt */
	for (j = 0; j < UCC_GETH_IP_PRIORITY_MAX; j += 8) {
		l3qt = 0;
		for (i = 0; i < 8; i++)
			l3qt |= (ug_info->l3qt[j + i] << (28 - 4 * i));
2763
		out_be32(&ugeth->p_rx_glbl_pram->l3qt[j/8], l3qt);
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778
	}

	/* vlantype */
	out_be16(&ugeth->p_rx_glbl_pram->vlantype, ug_info->vlantype);

	/* vlantci */
	out_be16(&ugeth->p_rx_glbl_pram->vlantci, ug_info->vlantci);

	/* ecamptr */
	out_be32(&ugeth->p_rx_glbl_pram->ecamptr, ug_info->ecamptr);

	/* RBDQPTR */
	/* Size varies with number of Rx queues */
	ugeth->rx_bd_qs_tbl_offset =
	    qe_muram_alloc(ug_info->numQueuesRx *
2779 2780
			   (sizeof(struct ucc_geth_rx_bd_queues_entry) +
			    sizeof(struct ucc_geth_rx_prefetched_bds)),
2781
			   UCC_GETH_RX_BD_QUEUES_ALIGNMENT);
2782
	if (IS_ERR_VALUE(ugeth->rx_bd_qs_tbl_offset)) {
2783
		if (netif_msg_ifup(ugeth))
2784
			pr_err("Can not allocate DPRAM memory for p_rx_bd_qs_tbl\n");
2785 2786 2787 2788
		return -ENOMEM;
	}

	ugeth->p_rx_bd_qs_tbl =
2789
	    (struct ucc_geth_rx_bd_queues_entry __iomem *) qe_muram_addr(ugeth->
2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801
				    rx_bd_qs_tbl_offset);
	out_be32(&ugeth->p_rx_glbl_pram->rbdqptr, ugeth->rx_bd_qs_tbl_offset);

	/* Setup the table */
	/* Assume BD rings are already established */
	for (i = 0; i < ug_info->numQueuesRx; i++) {
		if (ugeth->ug_info->uf_info.bd_mem_part == MEM_PART_SYSTEM) {
			out_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
				 (u32) virt_to_phys(ugeth->p_rx_bd_ring[i]));
		} else if (ugeth->ug_info->uf_info.bd_mem_part ==
			   MEM_PART_MURAM) {
			out_be32(&ugeth->p_rx_bd_qs_tbl[i].externalbdbaseptr,
2802
				 (u32)qe_muram_dma(ugeth->p_rx_bd_ring[i]));
2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
		}
		/* rest of fields handled by QE */
	}

	/* remoder */
	/* Already has speed set */

	if (ugeth->rx_extended_features)
		remoder |= REMODER_RX_EXTENDED_FEATURES;
	if (ug_info->rxExtendedFiltering)
		remoder |= REMODER_RX_EXTENDED_FILTERING;
	if (ug_info->dynamicMaxFrameLength)
		remoder |= REMODER_DYNAMIC_MAX_FRAME_LENGTH;
	if (ug_info->dynamicMinFrameLength)
		remoder |= REMODER_DYNAMIC_MIN_FRAME_LENGTH;
	remoder |=
	    ug_info->vlanOperationTagged << REMODER_VLAN_OPERATION_TAGGED_SHIFT;
	remoder |=
	    ug_info->
	    vlanOperationNonTagged << REMODER_VLAN_OPERATION_NON_TAGGED_SHIFT;
	remoder |= ug_info->rxQoSMode << REMODER_RX_QOS_MODE_SHIFT;
	remoder |= ((ug_info->numQueuesRx - 1) << REMODER_NUM_OF_QUEUES_SHIFT);
	if (ug_info->ipCheckSumCheck)
		remoder |= REMODER_IP_CHECKSUM_CHECK;
	if (ug_info->ipAddressAlignment)
		remoder |= REMODER_IP_ADDRESS_ALIGNMENT;
	out_be32(&ugeth->p_rx_glbl_pram->remoder, remoder);

	/* Note that this function must be called */
	/* ONLY AFTER p_tx_fw_statistics_pram */
	/* andp_UccGethRxFirmwareStatisticsPram are allocated ! */
	init_firmware_statistics_gathering_mode((ug_info->
		statisticsMode &
		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_TX),
		(ug_info->statisticsMode &
		UCC_GETH_STATISTICS_GATHERING_MODE_FIRMWARE_RX),
		&ugeth->p_tx_glbl_pram->txrmonbaseptr,
		ugeth->tx_fw_statistics_pram_offset,
		&ugeth->p_rx_glbl_pram->rxrmonbaseptr,
		ugeth->rx_fw_statistics_pram_offset,
		&ugeth->p_tx_glbl_pram->temoder,
		&ugeth->p_rx_glbl_pram->remoder);

	/* function code register */
2847
	out_8(&ugeth->p_rx_glbl_pram->rstate, function_code);
2848 2849 2850 2851

	/* initialize extended filtering */
	if (ug_info->rxExtendedFiltering) {
		if (!ug_info->extendedFilteringChainPointer) {
2852
			if (netif_msg_ifup(ugeth))
2853
				pr_err("Null Extended Filtering Chain Pointer\n");
2854 2855 2856 2857 2858 2859
			return -EINVAL;
		}

		/* Allocate memory for extended filtering Mode Global
		Parameters */
		ugeth->exf_glbl_param_offset =
2860
		    qe_muram_alloc(sizeof(struct ucc_geth_exf_global_pram),
2861
		UCC_GETH_RX_EXTENDED_FILTERING_GLOBAL_PARAMETERS_ALIGNMENT);
2862
		if (IS_ERR_VALUE(ugeth->exf_glbl_param_offset)) {
2863
			if (netif_msg_ifup(ugeth))
2864
				pr_err("Can not allocate DPRAM memory for p_exf_glbl_param\n");
2865 2866 2867 2868
			return -ENOMEM;
		}

		ugeth->p_exf_glbl_param =
2869
		    (struct ucc_geth_exf_global_pram __iomem *) qe_muram_addr(ugeth->
2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883
				 exf_glbl_param_offset);
		out_be32(&ugeth->p_rx_glbl_pram->exfGlobalParam,
			 ugeth->exf_glbl_param_offset);
		out_be32(&ugeth->p_exf_glbl_param->l2pcdptr,
			 (u32) ug_info->extendedFilteringChainPointer);

	} else {		/* initialize 82xx style address filtering */

		/* Init individual address recognition registers to disabled */

		for (j = 0; j < NUM_OF_PADDRS; j++)
			ugeth_82xx_filtering_clear_addr_in_paddr(ugeth, (u8) j);

		p_82xx_addr_filt =
2884
		    (struct ucc_geth_82xx_address_filtering_pram __iomem *) ugeth->
2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906
		    p_rx_glbl_pram->addressfiltering;

		ugeth_82xx_filtering_clear_all_addr_in_hash(ugeth,
			ENET_ADDR_TYPE_GROUP);
		ugeth_82xx_filtering_clear_all_addr_in_hash(ugeth,
			ENET_ADDR_TYPE_INDIVIDUAL);
	}

	/*
	 * Initialize UCC at QE level
	 */

	command = QE_INIT_TX_RX;

	/* Allocate shadow InitEnet command parameter structure.
	 * This is needed because after the InitEnet command is executed,
	 * the structure in DPRAM is released, because DPRAM is a premium
	 * resource.
	 * This shadow structure keeps a copy of what was done so that the
	 * allocated resources can be released when the channel is freed.
	 */
	if (!(ugeth->p_init_enet_param_shadow =
2907
	      kzalloc(sizeof(struct ucc_geth_init_pram), GFP_KERNEL))) {
2908
		if (netif_msg_ifup(ugeth))
2909
			pr_err("Can not allocate memory for p_UccInitEnetParamShadows\n");
2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932
		return -ENOMEM;
	}

	/* Fill shadow InitEnet command parameter structure */

	ugeth->p_init_enet_param_shadow->resinit1 =
	    ENET_INIT_PARAM_MAGIC_RES_INIT1;
	ugeth->p_init_enet_param_shadow->resinit2 =
	    ENET_INIT_PARAM_MAGIC_RES_INIT2;
	ugeth->p_init_enet_param_shadow->resinit3 =
	    ENET_INIT_PARAM_MAGIC_RES_INIT3;
	ugeth->p_init_enet_param_shadow->resinit4 =
	    ENET_INIT_PARAM_MAGIC_RES_INIT4;
	ugeth->p_init_enet_param_shadow->resinit5 =
	    ENET_INIT_PARAM_MAGIC_RES_INIT5;
	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
	    ((u32) ug_info->numThreadsRx) << ENET_INIT_PARAM_RGF_SHIFT;
	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
	    ((u32) ug_info->numThreadsTx) << ENET_INIT_PARAM_TGF_SHIFT;

	ugeth->p_init_enet_param_shadow->rgftgfrxglobal |=
	    ugeth->rx_glbl_pram_offset | ug_info->riscRx;
	if ((ug_info->largestexternallookupkeysize !=
2933 2934 2935 2936 2937
	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_NONE) &&
	    (ug_info->largestexternallookupkeysize !=
	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_8_BYTES) &&
	    (ug_info->largestexternallookupkeysize !=
	     QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_16_BYTES)) {
2938
		if (netif_msg_ifup(ugeth))
2939
			pr_err("Invalid largest External Lookup Key Size\n");
2940 2941 2942 2943
		return -EINVAL;
	}
	ugeth->p_init_enet_param_shadow->largestexternallookupkeysize =
	    ug_info->largestexternallookupkeysize;
2944
	size = sizeof(struct ucc_geth_thread_rx_pram);
2945 2946 2947
	if (ug_info->rxExtendedFiltering) {
		size += THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING;
		if (ug_info->largestexternallookupkeysize ==
2948
		    QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_8_BYTES)
2949 2950 2951
			size +=
			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_8;
		if (ug_info->largestexternallookupkeysize ==
2952
		    QE_FLTR_LARGEST_EXTERNAL_TABLE_LOOKUP_KEY_SIZE_16_BYTES)
2953 2954 2955 2956 2957 2958 2959 2960 2961 2962
			size +=
			    THREAD_RX_PRAM_ADDITIONAL_FOR_EXTENDED_FILTERING_16;
	}

	if ((ret_val = fill_init_enet_entries(ugeth, &(ugeth->
		p_init_enet_param_shadow->rxthread[0]),
		(u8) (numThreadsRxNumerical + 1)
		/* Rx needs one extra for terminator */
		, size, UCC_GETH_THREAD_RX_PRAM_ALIGNMENT,
		ug_info->riscRx, 1)) != 0) {
2963
		if (netif_msg_ifup(ugeth))
2964
			pr_err("Can not fill p_init_enet_param_shadow\n");
2965 2966 2967 2968 2969 2970 2971 2972 2973
		return ret_val;
	}

	ugeth->p_init_enet_param_shadow->txglobal =
	    ugeth->tx_glbl_pram_offset | ug_info->riscTx;
	if ((ret_val =
	     fill_init_enet_entries(ugeth,
				    &(ugeth->p_init_enet_param_shadow->
				      txthread[0]), numThreadsTxNumerical,
2974
				    sizeof(struct ucc_geth_thread_tx_pram),
2975 2976
				    UCC_GETH_THREAD_TX_PRAM_ALIGNMENT,
				    ug_info->riscTx, 0)) != 0) {
2977
		if (netif_msg_ifup(ugeth))
2978
			pr_err("Can not fill p_init_enet_param_shadow\n");
2979 2980 2981 2982 2983 2984
		return ret_val;
	}

	/* Load Rx bds with buffers */
	for (i = 0; i < ug_info->numQueuesRx; i++) {
		if ((ret_val = rx_bd_buffer_set(ugeth, (u8) i)) != 0) {
2985
			if (netif_msg_ifup(ugeth))
2986
				pr_err("Can not fill Rx bds with buffers\n");
2987 2988 2989 2990 2991
			return ret_val;
		}
	}

	/* Allocate InitEnet command parameter structure */
2992
	init_enet_pram_offset = qe_muram_alloc(sizeof(struct ucc_geth_init_pram), 4);
2993
	if (IS_ERR_VALUE(init_enet_pram_offset)) {
2994
		if (netif_msg_ifup(ugeth))
2995
			pr_err("Can not allocate DPRAM memory for p_init_enet_pram\n");
2996 2997 2998
		return -ENOMEM;
	}
	p_init_enet_pram =
2999
	    (struct ucc_geth_init_pram __iomem *) qe_muram_addr(init_enet_pram_offset);
3000 3001

	/* Copy shadow InitEnet command parameter structure into PRAM */
3002 3003 3004 3005 3006 3007 3008 3009
	out_8(&p_init_enet_pram->resinit1,
			ugeth->p_init_enet_param_shadow->resinit1);
	out_8(&p_init_enet_pram->resinit2,
			ugeth->p_init_enet_param_shadow->resinit2);
	out_8(&p_init_enet_pram->resinit3,
			ugeth->p_init_enet_param_shadow->resinit3);
	out_8(&p_init_enet_pram->resinit4,
			ugeth->p_init_enet_param_shadow->resinit4);
3010 3011
	out_be16(&p_init_enet_pram->resinit5,
		 ugeth->p_init_enet_param_shadow->resinit5);
3012 3013
	out_8(&p_init_enet_pram->largestexternallookupkeysize,
	    ugeth->p_init_enet_param_shadow->largestexternallookupkeysize);
3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027
	out_be32(&p_init_enet_pram->rgftgfrxglobal,
		 ugeth->p_init_enet_param_shadow->rgftgfrxglobal);
	for (i = 0; i < ENET_INIT_PARAM_MAX_ENTRIES_RX; i++)
		out_be32(&p_init_enet_pram->rxthread[i],
			 ugeth->p_init_enet_param_shadow->rxthread[i]);
	out_be32(&p_init_enet_pram->txglobal,
		 ugeth->p_init_enet_param_shadow->txglobal);
	for (i = 0; i < ENET_INIT_PARAM_MAX_ENTRIES_TX; i++)
		out_be32(&p_init_enet_pram->txthread[i],
			 ugeth->p_init_enet_param_shadow->txthread[i]);

	/* Issue QE command */
	cecr_subblock =
	    ucc_fast_get_qe_cr_subblock(ugeth->ug_info->uf_info.ucc_num);
3028
	qe_issue_cmd(command, cecr_subblock, QE_CR_PROTOCOL_ETHERNET,
3029 3030 3031 3032 3033 3034 3035 3036 3037 3038
		     init_enet_pram_offset);

	/* Free InitEnet command parameter */
	qe_muram_free(init_enet_pram_offset);

	return 0;
}

/* This is called by the kernel when a frame is ready for transmission. */
/* It is pointed to by the dev->hard_start_xmit function pointer */
3039 3040
static netdev_tx_t
ucc_geth_start_xmit(struct sk_buff *skb, struct net_device *dev)
3041
{
3042
	struct ucc_geth_private *ugeth = netdev_priv(dev);
3043 3044 3045
#ifdef CONFIG_UGETH_TX_ON_DEMAND
	struct ucc_fast_private *uccf;
#endif
3046
	u8 __iomem *bd;			/* BD pointer */
3047 3048
	u32 bd_status;
	u8 txQ = 0;
3049
	unsigned long flags;
3050

3051
	ugeth_vdbg("%s: IN", __func__);
3052

J
Joakim Tjernlund 已提交
3053
	netdev_sent_queue(dev, skb->len);
3054
	spin_lock_irqsave(&ugeth->lock, flags);
3055

3056
	dev->stats.tx_bytes += skb->len;
3057 3058 3059

	/* Start from the next BD that should be filled */
	bd = ugeth->txBd[txQ];
3060
	bd_status = in_be32((u32 __iomem *)bd);
3061 3062 3063 3064 3065 3066 3067 3068 3069
	/* Save the skb pointer so we can free it later */
	ugeth->tx_skbuff[txQ][ugeth->skb_curtx[txQ]] = skb;

	/* Update the current skb pointer (wrapping if this was the last) */
	ugeth->skb_curtx[txQ] =
	    (ugeth->skb_curtx[txQ] +
	     1) & TX_RING_MOD_MASK(ugeth->ug_info->bdRingLenTx[txQ]);

	/* set up the buffer descriptor */
3070
	out_be32(&((struct qe_bd __iomem *)bd)->buf,
3071
		      dma_map_single(ugeth->dev, skb->data,
3072
			      skb->len, DMA_TO_DEVICE));
3073

3074
	/* printk(KERN_DEBUG"skb->data is 0x%x\n",skb->data); */
3075 3076 3077

	bd_status = (bd_status & T_W) | T_R | T_I | T_L | skb->len;

3078
	/* set bd status and length */
3079
	out_be32((u32 __iomem *)bd, bd_status);
3080 3081 3082

	/* Move to next BD in the ring */
	if (!(bd_status & T_W))
L
Li Yang 已提交
3083
		bd += sizeof(struct qe_bd);
3084
	else
L
Li Yang 已提交
3085
		bd = ugeth->p_tx_bd_ring[txQ];
3086 3087 3088 3089 3090 3091 3092 3093

	/* If the next BD still needs to be cleaned up, then the bds
	   are full.  We need to tell the kernel to stop sending us stuff. */
	if (bd == ugeth->confBd[txQ]) {
		if (!netif_queue_stopped(dev))
			netif_stop_queue(dev);
	}

L
Li Yang 已提交
3094 3095
	ugeth->txBd[txQ] = bd;

3096 3097
	skb_tx_timestamp(skb);

3098 3099 3100 3101 3102 3103 3104 3105 3106
	if (ugeth->p_scheduler) {
		ugeth->cpucount[txQ]++;
		/* Indicate to QE that there are more Tx bds ready for
		transmission */
		/* This is done by writing a running counter of the bd
		count to the scheduler PRAM. */
		out_be16(ugeth->p_cpucount[txQ], ugeth->cpucount[txQ]);
	}

3107 3108 3109 3110
#ifdef CONFIG_UGETH_TX_ON_DEMAND
	uccf = ugeth->uccf;
	out_be16(uccf->p_utodr, UCC_FAST_TOD);
#endif
3111
	spin_unlock_irqrestore(&ugeth->lock, flags);
3112

3113
	return NETDEV_TX_OK;
3114 3115
}

3116
static int ucc_geth_rx(struct ucc_geth_private *ugeth, u8 rxQ, int rx_work_limit)
3117 3118
{
	struct sk_buff *skb;
3119
	u8 __iomem *bd;
3120 3121 3122
	u16 length, howmany = 0;
	u32 bd_status;
	u8 *bdBuffer;
A
Andrew Morton 已提交
3123
	struct net_device *dev;
3124

3125
	ugeth_vdbg("%s: IN", __func__);
3126

3127
	dev = ugeth->ndev;
3128

3129 3130 3131
	/* collect received buffers */
	bd = ugeth->rxBd[rxQ];

3132
	bd_status = in_be32((u32 __iomem *)bd);
3133 3134 3135

	/* while there are received buffers and BD is full (~R_E) */
	while (!((bd_status & (R_E)) || (--rx_work_limit < 0))) {
3136
		bdBuffer = (u8 *) in_be32(&((struct qe_bd __iomem *)bd)->buf);
3137 3138 3139 3140 3141 3142 3143 3144
		length = (u16) ((bd_status & BD_LENGTH_MASK) - 4);
		skb = ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]];

		/* determine whether buffer is first, last, first and last
		(single buffer frame) or middle (not first and not last) */
		if (!skb ||
		    (!(bd_status & (R_F | R_L))) ||
		    (bd_status & R_ERRORS_FATAL)) {
3145
			if (netif_msg_rx_err(ugeth))
3146 3147
				pr_err("%d: ERROR!!! skb - 0x%08x\n",
				       __LINE__, (u32)skb);
3148
			dev_kfree_skb(skb);
3149 3150

			ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]] = NULL;
3151
			dev->stats.rx_dropped++;
3152
		} else {
3153
			dev->stats.rx_packets++;
3154 3155 3156 3157 3158 3159
			howmany++;

			/* Prep the skb for the packet */
			skb_put(skb, length);

			/* Tell the skb what kind of packet this is */
3160
			skb->protocol = eth_type_trans(skb, ugeth->ndev);
3161

3162
			dev->stats.rx_bytes += length;
3163 3164 3165 3166 3167 3168
			/* Send the packet up the stack */
			netif_receive_skb(skb);
		}

		skb = get_new_skb(ugeth, bd);
		if (!skb) {
3169
			if (netif_msg_rx_err(ugeth))
3170
				pr_warn("No Rx Data Buffer\n");
3171
			dev->stats.rx_dropped++;
3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184
			break;
		}

		ugeth->rx_skbuff[rxQ][ugeth->skb_currx[rxQ]] = skb;

		/* update to point at the next skb */
		ugeth->skb_currx[rxQ] =
		    (ugeth->skb_currx[rxQ] +
		     1) & RX_RING_MOD_MASK(ugeth->ug_info->bdRingLenRx[rxQ]);

		if (bd_status & R_W)
			bd = ugeth->p_rx_bd_ring[rxQ];
		else
3185
			bd += sizeof(struct qe_bd);
3186

3187
		bd_status = in_be32((u32 __iomem *)bd);
3188 3189 3190 3191 3192 3193 3194 3195 3196
	}

	ugeth->rxBd[rxQ] = bd;
	return howmany;
}

static int ucc_geth_tx(struct net_device *dev, u8 txQ)
{
	/* Start from the next BD that should be filled */
3197
	struct ucc_geth_private *ugeth = netdev_priv(dev);
J
Joakim Tjernlund 已提交
3198 3199
	unsigned int bytes_sent = 0;
	int howmany = 0;
3200
	u8 __iomem *bd;		/* BD pointer */
3201 3202 3203
	u32 bd_status;

	bd = ugeth->confBd[txQ];
3204
	bd_status = in_be32((u32 __iomem *)bd);
3205 3206 3207

	/* Normal processing. */
	while ((bd_status & T_R) == 0) {
3208 3209
		struct sk_buff *skb;

3210 3211 3212 3213
		/* BD contains already transmitted buffer.   */
		/* Handle the transmitted buffer and release */
		/* the BD to be used with the current frame  */

3214 3215
		skb = ugeth->tx_skbuff[txQ][ugeth->skb_dirtytx[txQ]];
		if (!skb)
3216
			break;
J
Joakim Tjernlund 已提交
3217 3218
		howmany++;
		bytes_sent += skb->len;
3219
		dev->stats.tx_packets++;
3220

3221
		dev_consume_skb_any(skb);
3222

3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233
		ugeth->tx_skbuff[txQ][ugeth->skb_dirtytx[txQ]] = NULL;
		ugeth->skb_dirtytx[txQ] =
		    (ugeth->skb_dirtytx[txQ] +
		     1) & TX_RING_MOD_MASK(ugeth->ug_info->bdRingLenTx[txQ]);

		/* We freed a buffer, so now we can restart transmission */
		if (netif_queue_stopped(dev))
			netif_wake_queue(dev);

		/* Advance the confirmation BD pointer */
		if (!(bd_status & T_W))
L
Li Yang 已提交
3234
			bd += sizeof(struct qe_bd);
3235
		else
L
Li Yang 已提交
3236
			bd = ugeth->p_tx_bd_ring[txQ];
3237
		bd_status = in_be32((u32 __iomem *)bd);
3238
	}
L
Li Yang 已提交
3239
	ugeth->confBd[txQ] = bd;
J
Joakim Tjernlund 已提交
3240
	netdev_completed_queue(dev, howmany, bytes_sent);
3241 3242 3243
	return 0;
}

3244
static int ucc_geth_poll(struct napi_struct *napi, int budget)
3245
{
3246
	struct ucc_geth_private *ugeth = container_of(napi, struct ucc_geth_private, napi);
M
Michael Reiss 已提交
3247
	struct ucc_geth_info *ug_info;
3248
	int howmany, i;
3249

M
Michael Reiss 已提交
3250 3251
	ug_info = ugeth->ug_info;

3252 3253 3254 3255 3256 3257
	/* Tx event processing */
	spin_lock(&ugeth->lock);
	for (i = 0; i < ug_info->numQueuesTx; i++)
		ucc_geth_tx(ugeth->ndev, i);
	spin_unlock(&ugeth->lock);

3258 3259 3260 3261
	howmany = 0;
	for (i = 0; i < ug_info->numQueuesRx; i++)
		howmany += ucc_geth_rx(ugeth, i, budget - howmany);

3262
	if (howmany < budget) {
3263
		napi_complete_done(napi, howmany);
3264
		setbits32(ugeth->uccf->p_uccm, UCCE_RX_EVENTS | UCCE_TX_EVENTS);
M
Michael Reiss 已提交
3265
	}
3266

3267
	return howmany;
3268 3269
}

3270
static irqreturn_t ucc_geth_irq_handler(int irq, void *info)
3271
{
3272
	struct net_device *dev = info;
3273 3274 3275
	struct ucc_geth_private *ugeth = netdev_priv(dev);
	struct ucc_fast_private *uccf;
	struct ucc_geth_info *ug_info;
M
Michael Reiss 已提交
3276 3277
	register u32 ucce;
	register u32 uccm;
3278

3279
	ugeth_vdbg("%s: IN", __func__);
3280 3281 3282 3283

	uccf = ugeth->uccf;
	ug_info = ugeth->ug_info;

M
Michael Reiss 已提交
3284 3285 3286 3287 3288
	/* read and clear events */
	ucce = (u32) in_be32(uccf->p_ucce);
	uccm = (u32) in_be32(uccf->p_uccm);
	ucce &= uccm;
	out_be32(uccf->p_ucce, ucce);
3289

M
Michael Reiss 已提交
3290
	/* check for receive events that require processing */
3291
	if (ucce & (UCCE_RX_EVENTS | UCCE_TX_EVENTS)) {
3292
		if (napi_schedule_prep(&ugeth->napi)) {
3293
			uccm &= ~(UCCE_RX_EVENTS | UCCE_TX_EVENTS);
M
Michael Reiss 已提交
3294
			out_be32(uccf->p_uccm, uccm);
3295
			__napi_schedule(&ugeth->napi);
M
Michael Reiss 已提交
3296 3297
		}
	}
3298

M
Michael Reiss 已提交
3299 3300
	/* Errors and other events */
	if (ucce & UCCE_OTHER) {
T
Timur Tabi 已提交
3301
		if (ucce & UCC_GETH_UCCE_BSY)
3302
			dev->stats.rx_errors++;
T
Timur Tabi 已提交
3303
		if (ucce & UCC_GETH_UCCE_TXE)
3304
			dev->stats.tx_errors++;
3305 3306 3307 3308 3309
	}

	return IRQ_HANDLED;
}

3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326
#ifdef CONFIG_NET_POLL_CONTROLLER
/*
 * Polling 'interrupt' - used by things like netconsole to send skbs
 * without having to re-enable interrupts. It's not called while
 * the interrupt routine is executing.
 */
static void ucc_netpoll(struct net_device *dev)
{
	struct ucc_geth_private *ugeth = netdev_priv(dev);
	int irq = ugeth->ug_info->uf_info.irq;

	disable_irq(irq);
	ucc_geth_irq_handler(irq, dev);
	enable_irq(irq);
}
#endif /* CONFIG_NET_POLL_CONTROLLER */

3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357
static int ucc_geth_set_mac_addr(struct net_device *dev, void *p)
{
	struct ucc_geth_private *ugeth = netdev_priv(dev);
	struct sockaddr *addr = p;

	if (!is_valid_ether_addr(addr->sa_data))
		return -EADDRNOTAVAIL;

	memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);

	/*
	 * If device is not running, we will set mac addr register
	 * when opening the device.
	 */
	if (!netif_running(dev))
		return 0;

	spin_lock_irq(&ugeth->lock);
	init_mac_station_addr_regs(dev->dev_addr[0],
				   dev->dev_addr[1],
				   dev->dev_addr[2],
				   dev->dev_addr[3],
				   dev->dev_addr[4],
				   dev->dev_addr[5],
				   &ugeth->ug_regs->macstnaddr1,
				   &ugeth->ug_regs->macstnaddr2);
	spin_unlock_irq(&ugeth->lock);

	return 0;
}

3358
static int ucc_geth_init_mac(struct ucc_geth_private *ugeth)
3359
{
3360
	struct net_device *dev = ugeth->ndev;
3361 3362
	int err;

3363 3364
	err = ucc_struct_init(ugeth);
	if (err) {
3365
		netif_err(ugeth, ifup, dev, "Cannot configure internal struct, aborting\n");
3366
		goto err;
3367 3368
	}

3369 3370
	err = ucc_geth_startup(ugeth);
	if (err) {
3371
		netif_err(ugeth, ifup, dev, "Cannot configure net device, aborting\n");
3372
		goto err;
3373 3374 3375 3376
	}

	err = adjust_enet_interface(ugeth);
	if (err) {
3377
		netif_err(ugeth, ifup, dev, "Cannot configure net device, aborting\n");
3378
		goto err;
3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391
	}

	/*       Set MACSTNADDR1, MACSTNADDR2                */
	/* For more details see the hardware spec.           */
	init_mac_station_addr_regs(dev->dev_addr[0],
				   dev->dev_addr[1],
				   dev->dev_addr[2],
				   dev->dev_addr[3],
				   dev->dev_addr[4],
				   dev->dev_addr[5],
				   &ugeth->ug_regs->macstnaddr1,
				   &ugeth->ug_regs->macstnaddr2);

3392
	err = ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
3393
	if (err) {
3394
		netif_err(ugeth, ifup, dev, "Cannot enable net device, aborting\n");
3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414
		goto err;
	}

	return 0;
err:
	ucc_geth_stop(ugeth);
	return err;
}

/* Called when something needs to use the ethernet device */
/* Returns 0 for success. */
static int ucc_geth_open(struct net_device *dev)
{
	struct ucc_geth_private *ugeth = netdev_priv(dev);
	int err;

	ugeth_vdbg("%s: IN", __func__);

	/* Test station address */
	if (dev->dev_addr[0] & ENET_GROUP_ADDR) {
3415 3416
		netif_err(ugeth, ifup, dev,
			  "Multicast address used for station address - is this what you wanted?\n");
3417 3418 3419 3420 3421
		return -EINVAL;
	}

	err = init_phy(dev);
	if (err) {
3422
		netif_err(ugeth, ifup, dev, "Cannot initialize PHY, aborting\n");
3423 3424 3425 3426 3427
		return err;
	}

	err = ucc_geth_init_mac(ugeth);
	if (err) {
3428
		netif_err(ugeth, ifup, dev, "Cannot initialize MAC, aborting\n");
3429
		goto err;
3430 3431
	}

3432 3433
	err = request_irq(ugeth->ug_info->uf_info.irq, ucc_geth_irq_handler,
			  0, "UCC Geth", dev);
3434
	if (err) {
3435
		netif_err(ugeth, ifup, dev, "Cannot get IRQ for net device, aborting\n");
3436
		goto err;
3437 3438
	}

3439 3440
	phy_start(ugeth->phydev);
	napi_enable(&ugeth->napi);
J
Joakim Tjernlund 已提交
3441
	netdev_reset_queue(dev);
3442 3443
	netif_start_queue(dev);

3444 3445 3446 3447
	device_set_wakeup_capable(&dev->dev,
			qe_alive_during_sleep() || ugeth->phydev->irq);
	device_set_wakeup_enable(&dev->dev, ugeth->wol_en);

3448
	return err;
3449

3450
err:
3451
	ucc_geth_stop(ugeth);
3452
	return err;
3453 3454 3455 3456 3457
}

/* Stops the kernel queue, and halts the controller */
static int ucc_geth_close(struct net_device *dev)
{
3458
	struct ucc_geth_private *ugeth = netdev_priv(dev);
3459

3460
	ugeth_vdbg("%s: IN", __func__);
3461

3462 3463
	napi_disable(&ugeth->napi);

3464
	cancel_work_sync(&ugeth->timeout_work);
3465
	ucc_geth_stop(ugeth);
3466 3467
	phy_disconnect(ugeth->phydev);
	ugeth->phydev = NULL;
3468

3469
	free_irq(ugeth->ug_info->uf_info.irq, ugeth->ndev);
3470

3471
	netif_stop_queue(dev);
J
Joakim Tjernlund 已提交
3472
	netdev_reset_queue(dev);
3473 3474 3475 3476

	return 0;
}

3477 3478 3479 3480 3481 3482 3483
/* Reopen device. This will reset the MAC and PHY. */
static void ucc_geth_timeout_work(struct work_struct *work)
{
	struct ucc_geth_private *ugeth;
	struct net_device *dev;

	ugeth = container_of(work, struct ucc_geth_private, timeout_work);
3484
	dev = ugeth->ndev;
3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496

	ugeth_vdbg("%s: IN", __func__);

	dev->stats.tx_errors++;

	ugeth_dump_regs(ugeth);

	if (dev->flags & IFF_UP) {
		/*
		 * Must reset MAC *and* PHY. This is done by reopening
		 * the device.
		 */
3497 3498 3499 3500 3501 3502
		netif_tx_stop_all_queues(dev);
		ucc_geth_stop(ugeth);
		ucc_geth_init_mac(ugeth);
		/* Must start PHY here */
		phy_start(ugeth->phydev);
		netif_tx_start_all_queues(dev);
3503 3504 3505 3506 3507 3508 3509 3510 3511
	}

	netif_tx_schedule_all(dev);
}

/*
 * ucc_geth_timeout gets called when a packet has not been
 * transmitted after a set amount of time.
 */
3512
static void ucc_geth_timeout(struct net_device *dev, unsigned int txqueue)
3513 3514 3515 3516 3517 3518
{
	struct ucc_geth_private *ugeth = netdev_priv(dev);

	schedule_work(&ugeth->timeout_work);
}

3519 3520 3521

#ifdef CONFIG_PM

3522
static int ucc_geth_suspend(struct platform_device *ofdev, pm_message_t state)
3523
{
3524
	struct net_device *ndev = platform_get_drvdata(ofdev);
3525 3526 3527 3528 3529
	struct ucc_geth_private *ugeth = netdev_priv(ndev);

	if (!netif_running(ndev))
		return 0;

3530
	netif_device_detach(ndev);
3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549
	napi_disable(&ugeth->napi);

	/*
	 * Disable the controller, otherwise we'll wakeup on any network
	 * activity.
	 */
	ugeth_disable(ugeth, COMM_DIR_RX_AND_TX);

	if (ugeth->wol_en & WAKE_MAGIC) {
		setbits32(ugeth->uccf->p_uccm, UCC_GETH_UCCE_MPD);
		setbits32(&ugeth->ug_regs->maccfg2, MACCFG2_MPE);
		ucc_fast_enable(ugeth->uccf, COMM_DIR_RX_AND_TX);
	} else if (!(ugeth->wol_en & WAKE_PHY)) {
		phy_stop(ugeth->phydev);
	}

	return 0;
}

3550
static int ucc_geth_resume(struct platform_device *ofdev)
3551
{
3552
	struct net_device *ndev = platform_get_drvdata(ofdev);
3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574
	struct ucc_geth_private *ugeth = netdev_priv(ndev);
	int err;

	if (!netif_running(ndev))
		return 0;

	if (qe_alive_during_sleep()) {
		if (ugeth->wol_en & WAKE_MAGIC) {
			ucc_fast_disable(ugeth->uccf, COMM_DIR_RX_AND_TX);
			clrbits32(&ugeth->ug_regs->maccfg2, MACCFG2_MPE);
			clrbits32(ugeth->uccf->p_uccm, UCC_GETH_UCCE_MPD);
		}
		ugeth_enable(ugeth, COMM_DIR_RX_AND_TX);
	} else {
		/*
		 * Full reinitialization is required if QE shuts down
		 * during sleep.
		 */
		ucc_geth_memclean(ugeth);

		err = ucc_geth_init_mac(ugeth);
		if (err) {
3575
			netdev_err(ndev, "Cannot initialize MAC, aborting\n");
3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587
			return err;
		}
	}

	ugeth->oldlink = 0;
	ugeth->oldspeed = 0;
	ugeth->oldduplex = -1;

	phy_stop(ugeth->phydev);
	phy_start(ugeth->phydev);

	napi_enable(&ugeth->napi);
3588
	netif_device_attach(ndev);
3589 3590 3591 3592 3593 3594 3595 3596 3597

	return 0;
}

#else
#define ucc_geth_suspend NULL
#define ucc_geth_resume NULL
#endif

3598
static phy_interface_t to_phy_interface(const char *phy_connection_type)
3599
{
3600
	if (strcasecmp(phy_connection_type, "mii") == 0)
3601
		return PHY_INTERFACE_MODE_MII;
3602
	if (strcasecmp(phy_connection_type, "gmii") == 0)
3603
		return PHY_INTERFACE_MODE_GMII;
3604
	if (strcasecmp(phy_connection_type, "tbi") == 0)
3605
		return PHY_INTERFACE_MODE_TBI;
3606
	if (strcasecmp(phy_connection_type, "rmii") == 0)
3607
		return PHY_INTERFACE_MODE_RMII;
3608
	if (strcasecmp(phy_connection_type, "rgmii") == 0)
3609
		return PHY_INTERFACE_MODE_RGMII;
3610
	if (strcasecmp(phy_connection_type, "rgmii-id") == 0)
3611
		return PHY_INTERFACE_MODE_RGMII_ID;
3612 3613 3614 3615
	if (strcasecmp(phy_connection_type, "rgmii-txid") == 0)
		return PHY_INTERFACE_MODE_RGMII_TXID;
	if (strcasecmp(phy_connection_type, "rgmii-rxid") == 0)
		return PHY_INTERFACE_MODE_RGMII_RXID;
3616
	if (strcasecmp(phy_connection_type, "rtbi") == 0)
3617
		return PHY_INTERFACE_MODE_RTBI;
3618 3619
	if (strcasecmp(phy_connection_type, "sgmii") == 0)
		return PHY_INTERFACE_MODE_SGMII;
3620 3621 3622 3623

	return PHY_INTERFACE_MODE_MII;
}

S
Sergey Matyukevich 已提交
3624 3625 3626 3627 3628 3629 3630 3631 3632 3633
static int ucc_geth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
{
	struct ucc_geth_private *ugeth = netdev_priv(dev);

	if (!netif_running(dev))
		return -EINVAL;

	if (!ugeth->phydev)
		return -ENODEV;

3634
	return phy_mii_ioctl(ugeth->phydev, rq, cmd);
S
Sergey Matyukevich 已提交
3635 3636
}

3637 3638 3639 3640 3641
static const struct net_device_ops ucc_geth_netdev_ops = {
	.ndo_open		= ucc_geth_open,
	.ndo_stop		= ucc_geth_close,
	.ndo_start_xmit		= ucc_geth_start_xmit,
	.ndo_validate_addr	= eth_validate_addr,
3642
	.ndo_change_carrier     = fixed_phy_change_carrier,
3643
	.ndo_set_mac_address	= ucc_geth_set_mac_addr,
3644
	.ndo_set_rx_mode	= ucc_geth_set_multi,
3645
	.ndo_tx_timeout		= ucc_geth_timeout,
S
Sergey Matyukevich 已提交
3646
	.ndo_do_ioctl		= ucc_geth_ioctl,
3647 3648 3649 3650 3651
#ifdef CONFIG_NET_POLL_CONTROLLER
	.ndo_poll_controller	= ucc_netpoll,
#endif
};

3652
static int ucc_geth_probe(struct platform_device* ofdev)
3653
{
3654
	struct device *device = &ofdev->dev;
3655
	struct device_node *np = ofdev->dev.of_node;
3656 3657 3658
	struct net_device *dev = NULL;
	struct ucc_geth_private *ugeth = NULL;
	struct ucc_geth_info *ug_info;
3659
	struct resource res;
3660
	int err, ucc_num, max_speed = 0;
3661
	const unsigned int *prop;
3662
	const char *sprop;
3663
	const void *mac_addr;
3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675
	phy_interface_t phy_interface;
	static const int enet_to_speed[] = {
		SPEED_10, SPEED_10, SPEED_10,
		SPEED_100, SPEED_100, SPEED_100,
		SPEED_1000, SPEED_1000, SPEED_1000, SPEED_1000,
	};
	static const phy_interface_t enet_to_phy_interface[] = {
		PHY_INTERFACE_MODE_MII, PHY_INTERFACE_MODE_RMII,
		PHY_INTERFACE_MODE_RGMII, PHY_INTERFACE_MODE_MII,
		PHY_INTERFACE_MODE_RMII, PHY_INTERFACE_MODE_RGMII,
		PHY_INTERFACE_MODE_GMII, PHY_INTERFACE_MODE_RGMII,
		PHY_INTERFACE_MODE_TBI, PHY_INTERFACE_MODE_RTBI,
3676
		PHY_INTERFACE_MODE_SGMII,
3677
	};
3678

3679
	ugeth_vdbg("%s: IN", __func__);
3680

3681 3682 3683 3684 3685 3686 3687
	prop = of_get_property(np, "cell-index", NULL);
	if (!prop) {
		prop = of_get_property(np, "device-id", NULL);
		if (!prop)
			return -ENODEV;
	}

3688 3689 3690 3691 3692
	ucc_num = *prop - 1;
	if ((ucc_num < 0) || (ucc_num > 7))
		return -ENODEV;

	ug_info = &ugeth_info[ucc_num];
3693 3694
	if (ug_info == NULL) {
		if (netif_msg_probe(&debug))
3695
			pr_err("[%d] Missing additional data!\n", ucc_num);
3696 3697 3698
		return -ENODEV;
	}

3699
	ug_info->uf_info.ucc_num = ucc_num;
3700

3701 3702 3703 3704 3705
	sprop = of_get_property(np, "rx-clock-name", NULL);
	if (sprop) {
		ug_info->uf_info.rx_clock = qe_clock_source(sprop);
		if ((ug_info->uf_info.rx_clock < QE_CLK_NONE) ||
		    (ug_info->uf_info.rx_clock > QE_CLK24)) {
3706
			pr_err("invalid rx-clock-name property\n");
3707 3708 3709 3710 3711 3712 3713
			return -EINVAL;
		}
	} else {
		prop = of_get_property(np, "rx-clock", NULL);
		if (!prop) {
			/* If both rx-clock-name and rx-clock are missing,
			   we want to tell people to use rx-clock-name. */
3714
			pr_err("missing rx-clock-name property\n");
3715 3716 3717
			return -EINVAL;
		}
		if ((*prop < QE_CLK_NONE) || (*prop > QE_CLK24)) {
3718
			pr_err("invalid rx-clock property\n");
3719 3720 3721 3722 3723 3724 3725 3726 3727 3728
			return -EINVAL;
		}
		ug_info->uf_info.rx_clock = *prop;
	}

	sprop = of_get_property(np, "tx-clock-name", NULL);
	if (sprop) {
		ug_info->uf_info.tx_clock = qe_clock_source(sprop);
		if ((ug_info->uf_info.tx_clock < QE_CLK_NONE) ||
		    (ug_info->uf_info.tx_clock > QE_CLK24)) {
3729
			pr_err("invalid tx-clock-name property\n");
3730 3731 3732
			return -EINVAL;
		}
	} else {
3733
		prop = of_get_property(np, "tx-clock", NULL);
3734
		if (!prop) {
3735
			pr_err("missing tx-clock-name property\n");
3736 3737 3738
			return -EINVAL;
		}
		if ((*prop < QE_CLK_NONE) || (*prop > QE_CLK24)) {
3739
			pr_err("invalid tx-clock property\n");
3740 3741 3742 3743 3744
			return -EINVAL;
		}
		ug_info->uf_info.tx_clock = *prop;
	}

3745 3746 3747 3748 3749 3750
	err = of_address_to_resource(np, 0, &res);
	if (err)
		return -EINVAL;

	ug_info->uf_info.regs = res.start;
	ug_info->uf_info.irq = irq_of_parse_and_map(np, 0);
3751 3752

	ug_info->phy_node = of_parse_phandle(np, "phy-handle", 0);
3753 3754 3755
	if (!ug_info->phy_node && of_phy_is_fixed_link(np)) {
		/*
		 * In the case of a fixed PHY, the DT node associated
3756 3757
		 * to the PHY is the Ethernet MAC DT node.
		 */
3758 3759 3760
		err = of_phy_register_fixed_link(np);
		if (err)
			return err;
3761
		ug_info->phy_node = of_node_get(np);
3762
	}
3763

3764 3765 3766
	/* Find the TBI PHY node.  If it's not there, we don't support SGMII */
	ug_info->tbi_node = of_parse_phandle(np, "tbi-handle", 0);

3767
	/* get the phy interface type, or default to MII */
3768
	prop = of_get_property(np, "phy-connection-type", NULL);
3769 3770
	if (!prop) {
		/* handle interface property present in old trees */
3771
		prop = of_get_property(ug_info->phy_node, "interface", NULL);
3772
		if (prop != NULL) {
3773
			phy_interface = enet_to_phy_interface[*prop];
3774 3775
			max_speed = enet_to_speed[*prop];
		} else
3776 3777 3778 3779 3780
			phy_interface = PHY_INTERFACE_MODE_MII;
	} else {
		phy_interface = to_phy_interface((const char *)prop);
	}

3781 3782
	/* get speed, or derive from PHY interface */
	if (max_speed == 0)
3783 3784 3785 3786
		switch (phy_interface) {
		case PHY_INTERFACE_MODE_GMII:
		case PHY_INTERFACE_MODE_RGMII:
		case PHY_INTERFACE_MODE_RGMII_ID:
3787 3788
		case PHY_INTERFACE_MODE_RGMII_RXID:
		case PHY_INTERFACE_MODE_RGMII_TXID:
3789 3790
		case PHY_INTERFACE_MODE_TBI:
		case PHY_INTERFACE_MODE_RTBI:
3791
		case PHY_INTERFACE_MODE_SGMII:
3792 3793 3794 3795 3796 3797 3798 3799
			max_speed = SPEED_1000;
			break;
		default:
			max_speed = SPEED_100;
			break;
		}

	if (max_speed == SPEED_1000) {
3800 3801
		unsigned int snums = qe_get_num_of_snums();

3802
		/* configure muram FIFOs for gigabit operation */
3803 3804 3805 3806 3807 3808
		ug_info->uf_info.urfs = UCC_GETH_URFS_GIGA_INIT;
		ug_info->uf_info.urfet = UCC_GETH_URFET_GIGA_INIT;
		ug_info->uf_info.urfset = UCC_GETH_URFSET_GIGA_INIT;
		ug_info->uf_info.utfs = UCC_GETH_UTFS_GIGA_INIT;
		ug_info->uf_info.utfet = UCC_GETH_UTFET_GIGA_INIT;
		ug_info->uf_info.utftt = UCC_GETH_UTFTT_GIGA_INIT;
3809
		ug_info->numThreadsTx = UCC_GETH_NUM_OF_THREADS_4;
3810

3811
		/* If QE's snum number is 46/76 which means we need to support
3812 3813 3814
		 * 4 UECs at 1000Base-T simultaneously, we need to allocate
		 * more Threads to Rx.
		 */
3815
		if ((snums == 76) || (snums == 46))
3816 3817 3818
			ug_info->numThreadsRx = UCC_GETH_NUM_OF_THREADS_6;
		else
			ug_info->numThreadsRx = UCC_GETH_NUM_OF_THREADS_4;
3819 3820
	}

3821
	if (netif_msg_probe(&debug))
3822 3823 3824
		pr_info("UCC%1d at 0x%8llx (irq = %d)\n",
			ug_info->uf_info.ucc_num + 1,
			(u64)ug_info->uf_info.regs,
3825
			ug_info->uf_info.irq);
3826 3827 3828 3829

	/* Create an ethernet device instance */
	dev = alloc_etherdev(sizeof(*ugeth));

3830
	if (dev == NULL) {
3831 3832
		err = -ENOMEM;
		goto err_deregister_fixed_link;
3833
	}
3834 3835 3836 3837

	ugeth = netdev_priv(dev);
	spin_lock_init(&ugeth->lock);

A
Anton Vorontsov 已提交
3838 3839 3840 3841
	/* Create CQs for hash tables */
	INIT_LIST_HEAD(&ugeth->group_hash_q);
	INIT_LIST_HEAD(&ugeth->ind_hash_q);

3842 3843 3844 3845 3846 3847 3848 3849
	dev_set_drvdata(device, dev);

	/* Set the dev->base_addr to the gfar reg region */
	dev->base_addr = (unsigned long)(ug_info->uf_info.regs);

	SET_NETDEV_DEV(dev, device);

	/* Fill in the dev structure */
L
Li Yang 已提交
3850
	uec_set_ethtool_ops(dev);
3851
	dev->netdev_ops = &ucc_geth_netdev_ops;
3852
	dev->watchdog_timeo = TX_TIMEOUT;
3853
	INIT_WORK(&ugeth->timeout_work, ucc_geth_timeout_work);
3854
	netif_napi_add(dev, &ugeth->napi, ucc_geth_poll, 64);
3855
	dev->mtu = 1500;
3856
	dev->max_mtu = 1518;
3857

3858
	ugeth->msg_enable = netif_msg_init(debug.msg_enable, UGETH_MSG_DEFAULT);
3859 3860 3861
	ugeth->phy_interface = phy_interface;
	ugeth->max_speed = max_speed;

3862 3863 3864
	/* Carrier starts down, phylib will bring it up */
	netif_carrier_off(dev);

3865 3866
	err = register_netdev(dev);
	if (err) {
3867
		if (netif_msg_probe(ugeth))
3868 3869
			pr_err("%s: Cannot register net device, aborting\n",
			       dev->name);
3870
		goto err_free_netdev;
3871 3872
	}

T
Timur Tabi 已提交
3873
	mac_addr = of_get_mac_address(np);
3874
	if (!IS_ERR(mac_addr))
3875
		ether_addr_copy(dev->dev_addr, mac_addr);
3876

3877
	ugeth->ug_info = ug_info;
3878 3879
	ugeth->dev = device;
	ugeth->ndev = dev;
3880
	ugeth->node = np;
3881

3882
	return 0;
3883 3884 3885 3886 3887 3888 3889 3890 3891 3892

err_free_netdev:
	free_netdev(dev);
err_deregister_fixed_link:
	if (of_phy_is_fixed_link(np))
		of_phy_deregister_fixed_link(np);
	of_node_put(ug_info->tbi_node);
	of_node_put(ug_info->phy_node);

	return err;
3893 3894
}

3895
static int ucc_geth_remove(struct platform_device* ofdev)
3896
{
3897
	struct net_device *dev = platform_get_drvdata(ofdev);
3898
	struct ucc_geth_private *ugeth = netdev_priv(dev);
3899
	struct device_node *np = ofdev->dev.of_node;
3900

A
Anton Vorontsov 已提交
3901 3902
	unregister_netdev(dev);
	ucc_geth_memclean(ugeth);
3903 3904
	if (of_phy_is_fixed_link(np))
		of_phy_deregister_fixed_link(np);
3905 3906
	of_node_put(ugeth->ug_info->tbi_node);
	of_node_put(ugeth->ug_info->phy_node);
3907
	free_netdev(dev);
3908 3909 3910 3911

	return 0;
}

3912
static const struct of_device_id ucc_geth_match[] = {
3913 3914 3915 3916 3917 3918 3919 3920 3921
	{
		.type = "network",
		.compatible = "ucc_geth",
	},
	{},
};

MODULE_DEVICE_TABLE(of, ucc_geth_match);

3922
static struct platform_driver ucc_geth_driver = {
3923 3924 3925 3926
	.driver = {
		.name = DRV_NAME,
		.of_match_table = ucc_geth_match,
	},
3927 3928
	.probe		= ucc_geth_probe,
	.remove		= ucc_geth_remove,
3929 3930
	.suspend	= ucc_geth_suspend,
	.resume		= ucc_geth_resume,
3931 3932 3933 3934
};

static int __init ucc_geth_init(void)
{
3935 3936
	int i, ret;

3937
	if (netif_msg_drv(&debug))
3938
		pr_info(DRV_DESC "\n");
3939 3940 3941 3942
	for (i = 0; i < 8; i++)
		memcpy(&(ugeth_info[i]), &ugeth_primary_info,
		       sizeof(ugeth_primary_info));

3943
	ret = platform_driver_register(&ucc_geth_driver);
3944 3945

	return ret;
3946 3947 3948 3949
}

static void __exit ucc_geth_exit(void)
{
3950
	platform_driver_unregister(&ucc_geth_driver);
3951 3952 3953 3954 3955 3956 3957 3958
}

module_init(ucc_geth_init);
module_exit(ucc_geth_exit);

MODULE_AUTHOR("Freescale Semiconductor, Inc");
MODULE_DESCRIPTION(DRV_DESC);
MODULE_LICENSE("GPL");