ibm_emac_core.c 58.3 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * drivers/net/ibm_emac/ibm_emac_core.c
L
Linus Torvalds 已提交
3
 *
4
 * Driver for PowerPC 4xx on-chip ethernet controller.
L
Linus Torvalds 已提交
5
 *
6 7
 * Copyright (c) 2004, 2005 Zultys Technologies.
 * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
L
Linus Torvalds 已提交
8
 *
9 10 11
 * Based on original work by
 * 	Matt Porter <mporter@kernel.crashing.org>
 *	(c) 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
L
Linus Torvalds 已提交
12 13 14 15 16 17 18
 *      Armin Kuster <akuster@mvista.com>
 * 	Johnnie Peters <jpeters@mvista.com>
 *
 * This program is free software; you can redistribute  it and/or modify it
 * under  the terms of  the GNU General  Public License as published by the
 * Free Software Foundation;  either version 2 of the  License, or (at your
 * option) any later version.
19
 *
L
Linus Torvalds 已提交
20
 */
21 22

#include <linux/config.h>
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/interrupt.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/types.h>
32 33 34 35 36
#include <linux/pci.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/skbuff.h>
#include <linux/crc32.h>
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47
#include <linux/ethtool.h>
#include <linux/mii.h>
#include <linux/bitops.h>

#include <asm/processor.h>
#include <asm/io.h>
#include <asm/dma.h>
#include <asm/uaccess.h>
#include <asm/ocp.h>

#include "ibm_emac_core.h"
48
#include "ibm_emac_debug.h"
L
Linus Torvalds 已提交
49 50

/*
51 52 53 54 55 56 57 58 59 60 61 62 63 64
 * Lack of dma_unmap_???? calls is intentional.
 *
 * API-correct usage requires additional support state information to be 
 * maintained for every RX and TX buffer descriptor (BD). Unfortunately, due to
 * EMAC design (e.g. TX buffer passed from network stack can be split into
 * several BDs, dma_map_single/dma_map_page can be used to map particular BD),
 * maintaining such information will add additional overhead.
 * Current DMA API implementation for 4xx processors only ensures cache coherency
 * and dma_unmap_???? routines are empty and are likely to stay this way.
 * I decided to omit dma_unmap_??? calls because I don't want to add additional
 * complexity just for the sake of following some abstract API, when it doesn't
 * add any real benefit to the driver. I understand that this decision maybe 
 * controversial, but I really tried to make code API-correct and efficient 
 * at the same time and didn't come up with code I liked :(.                --ebs
L
Linus Torvalds 已提交
65 66
 */

67 68 69 70
#define DRV_NAME        "emac"
#define DRV_VERSION     "3.53"
#define DRV_DESC        "PPC 4xx OCP EMAC driver"

L
Linus Torvalds 已提交
71
MODULE_DESCRIPTION(DRV_DESC);
72 73
MODULE_AUTHOR
    ("Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>");
L
Linus Torvalds 已提交
74 75
MODULE_LICENSE("GPL");

76 77
/* minimum number of free TX descriptors required to wake up TX process */
#define EMAC_TX_WAKEUP_THRESH		(NUM_TX_BUFF / 4)
L
Linus Torvalds 已提交
78

79 80 81 82
/* If packet size is less than this number, we allocate small skb and copy packet 
 * contents into it instead of just sending original big skb up
 */
#define EMAC_RX_COPY_THRESH		CONFIG_IBM_EMAC_RX_COPY_THRESHOLD
L
Linus Torvalds 已提交
83

84 85 86 87 88
/* Since multiple EMACs share MDIO lines in various ways, we need
 * to avoid re-using the same PHY ID in cases where the arch didn't
 * setup precise phy_map entries
 */
static u32 busy_phy_map;
L
Linus Torvalds 已提交
89

90 91
#if defined(CONFIG_IBM_EMAC_PHY_RX_CLK_FIX) && \
    (defined(CONFIG_405EP) || defined(CONFIG_440EP) || defined(CONFIG_440GR))
92 93
/* 405EP has "EMAC to PHY Control Register" (CPC0_EPCTL) which can help us
 * with PHY RX clock problem.
94
 * 440EP/440GR has more sane SDR0_MFR register implementation than 440GX, which
95 96 97 98 99 100
 * also allows controlling each EMAC clock
 */
static inline void EMAC_RX_CLK_TX(int idx)
{
	unsigned long flags;
	local_irq_save(flags);
L
Linus Torvalds 已提交
101

102 103
#if defined(CONFIG_405EP)
	mtdcr(0xf3, mfdcr(0xf3) | (1 << idx));
104
#else /* CONFIG_440EP || CONFIG_440GR */
105 106
	SDR_WRITE(DCRN_SDR_MFR, SDR_READ(DCRN_SDR_MFR) | (0x08000000 >> idx));
#endif
L
Linus Torvalds 已提交
107

108 109
	local_irq_restore(flags);
}
L
Linus Torvalds 已提交
110

111 112 113 114
static inline void EMAC_RX_CLK_DEFAULT(int idx)
{
	unsigned long flags;
	local_irq_save(flags);
L
Linus Torvalds 已提交
115

116 117 118 119 120
#if defined(CONFIG_405EP)
	mtdcr(0xf3, mfdcr(0xf3) & ~(1 << idx));
#else /* CONFIG_440EP */
	SDR_WRITE(DCRN_SDR_MFR, SDR_READ(DCRN_SDR_MFR) & ~(0x08000000 >> idx));
#endif
L
Linus Torvalds 已提交
121

122 123 124 125 126 127
	local_irq_restore(flags);
}
#else
#define EMAC_RX_CLK_TX(idx)		((void)0)
#define EMAC_RX_CLK_DEFAULT(idx)	((void)0)
#endif
L
Linus Torvalds 已提交
128

129 130 131 132
#if defined(CONFIG_IBM_EMAC_PHY_RX_CLK_FIX) && defined(CONFIG_440GX)
/* We can switch Ethernet clock to the internal source through SDR0_MFR[ECS],
 * unfortunately this is less flexible than 440EP case, because it's a global 
 * setting for all EMACs, therefore we do this clock trick only during probe.
L
Linus Torvalds 已提交
133
 */
134 135 136 137 138 139 140 141
#define EMAC_CLK_INTERNAL		SDR_WRITE(DCRN_SDR_MFR, \
					    SDR_READ(DCRN_SDR_MFR) | 0x08000000)
#define EMAC_CLK_EXTERNAL		SDR_WRITE(DCRN_SDR_MFR, \
					    SDR_READ(DCRN_SDR_MFR) & ~0x08000000)
#else
#define EMAC_CLK_INTERNAL		((void)0)
#define EMAC_CLK_EXTERNAL		((void)0)
#endif
L
Linus Torvalds 已提交
142

143 144 145 146 147 148 149 150 151 152 153 154 155
/* I don't want to litter system log with timeout errors 
 * when we have brain-damaged PHY.
 */
static inline void emac_report_timeout_error(struct ocp_enet_private *dev,
					     const char *error)
{
#if defined(CONFIG_IBM_EMAC_PHY_RX_CLK_FIX)
	DBG("%d: %s" NL, dev->def->index, error);
#else
	if (net_ratelimit())
		printk(KERN_ERR "emac%d: %s\n", dev->def->index, error);
#endif
}
L
Linus Torvalds 已提交
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
/* PHY polling intervals */
#define PHY_POLL_LINK_ON	HZ
#define PHY_POLL_LINK_OFF	(HZ / 5)

/* Please, keep in sync with struct ibm_emac_stats/ibm_emac_error_stats */
static const char emac_stats_keys[EMAC_ETHTOOL_STATS_COUNT][ETH_GSTRING_LEN] = {
	"rx_packets", "rx_bytes", "tx_packets", "tx_bytes", "rx_packets_csum",
	"tx_packets_csum", "tx_undo", "rx_dropped_stack", "rx_dropped_oom",
	"rx_dropped_error", "rx_dropped_resize", "rx_dropped_mtu",
	"rx_stopped", "rx_bd_errors", "rx_bd_overrun", "rx_bd_bad_packet",
	"rx_bd_runt_packet", "rx_bd_short_event", "rx_bd_alignment_error",
	"rx_bd_bad_fcs", "rx_bd_packet_too_long", "rx_bd_out_of_range",
	"rx_bd_in_range", "rx_parity", "rx_fifo_overrun", "rx_overrun",
	"rx_bad_packet", "rx_runt_packet", "rx_short_event",
	"rx_alignment_error", "rx_bad_fcs", "rx_packet_too_long",
	"rx_out_of_range", "rx_in_range", "tx_dropped", "tx_bd_errors",
	"tx_bd_bad_fcs", "tx_bd_carrier_loss", "tx_bd_excessive_deferral",
	"tx_bd_excessive_collisions", "tx_bd_late_collision",
	"tx_bd_multple_collisions", "tx_bd_single_collision",
	"tx_bd_underrun", "tx_bd_sqe", "tx_parity", "tx_underrun", "tx_sqe",
	"tx_errors"
L
Linus Torvalds 已提交
178 179
};

180 181 182 183
static irqreturn_t emac_irq(int irq, void *dev_instance, struct pt_regs *regs);
static void emac_clean_tx_ring(struct ocp_enet_private *dev);

static inline int emac_phy_supports_gige(int phy_mode)
L
Linus Torvalds 已提交
184
{
185 186 187 188 189
	return  phy_mode == PHY_MODE_GMII ||
		phy_mode == PHY_MODE_RGMII ||
		phy_mode == PHY_MODE_TBI ||
		phy_mode == PHY_MODE_RTBI;
}
L
Linus Torvalds 已提交
190

191
static inline int emac_phy_gpcs(int phy_mode)
L
Linus Torvalds 已提交
192
{
193 194 195
	return  phy_mode == PHY_MODE_TBI ||
		phy_mode == PHY_MODE_RTBI;
}
L
Linus Torvalds 已提交
196

197 198 199 200 201
static inline void emac_tx_enable(struct ocp_enet_private *dev)
{
	struct emac_regs *p = dev->emacp;
	unsigned long flags;
	u32 r;
L
Linus Torvalds 已提交
202

203
	local_irq_save(flags);
L
Linus Torvalds 已提交
204

205
	DBG("%d: tx_enable" NL, dev->def->index);
L
Linus Torvalds 已提交
206

207 208 209 210 211
	r = in_be32(&p->mr0);
	if (!(r & EMAC_MR0_TXE))
		out_be32(&p->mr0, r | EMAC_MR0_TXE);
	local_irq_restore(flags);
}
L
Linus Torvalds 已提交
212

213 214 215 216 217
static void emac_tx_disable(struct ocp_enet_private *dev)
{
	struct emac_regs *p = dev->emacp;
	unsigned long flags;
	u32 r;
L
Linus Torvalds 已提交
218

219
	local_irq_save(flags);
L
Linus Torvalds 已提交
220

221 222 223 224 225 226 227 228 229 230 231 232 233
	DBG("%d: tx_disable" NL, dev->def->index);

	r = in_be32(&p->mr0);
	if (r & EMAC_MR0_TXE) {
		int n = 300;
		out_be32(&p->mr0, r & ~EMAC_MR0_TXE);
		while (!(in_be32(&p->mr0) & EMAC_MR0_TXI) && n)
			--n;
		if (unlikely(!n))
			emac_report_timeout_error(dev, "TX disable timeout");
	}
	local_irq_restore(flags);
}
L
Linus Torvalds 已提交
234

235 236 237 238 239
static void emac_rx_enable(struct ocp_enet_private *dev)
{
	struct emac_regs *p = dev->emacp;
	unsigned long flags;
	u32 r;
L
Linus Torvalds 已提交
240

241 242 243
	local_irq_save(flags);
	if (unlikely(dev->commac.rx_stopped))
		goto out;
L
Linus Torvalds 已提交
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
	DBG("%d: rx_enable" NL, dev->def->index);

	r = in_be32(&p->mr0);
	if (!(r & EMAC_MR0_RXE)) {
		if (unlikely(!(r & EMAC_MR0_RXI))) {
			/* Wait if previous async disable is still in progress */
			int n = 100;
			while (!(r = in_be32(&p->mr0) & EMAC_MR0_RXI) && n)
				--n;
			if (unlikely(!n))
				emac_report_timeout_error(dev,
							  "RX disable timeout");
		}
		out_be32(&p->mr0, r | EMAC_MR0_RXE);
	}
      out:
	local_irq_restore(flags);
L
Linus Torvalds 已提交
262 263
}

264
static void emac_rx_disable(struct ocp_enet_private *dev)
L
Linus Torvalds 已提交
265
{
266 267 268
	struct emac_regs *p = dev->emacp;
	unsigned long flags;
	u32 r;
L
Linus Torvalds 已提交
269

270
	local_irq_save(flags);
L
Linus Torvalds 已提交
271

272
	DBG("%d: rx_disable" NL, dev->def->index);
L
Linus Torvalds 已提交
273

274 275 276 277 278 279 280 281 282 283
	r = in_be32(&p->mr0);
	if (r & EMAC_MR0_RXE) {
		int n = 300;
		out_be32(&p->mr0, r & ~EMAC_MR0_RXE);
		while (!(in_be32(&p->mr0) & EMAC_MR0_RXI) && n)
			--n;
		if (unlikely(!n))
			emac_report_timeout_error(dev, "RX disable timeout");
	}
	local_irq_restore(flags);
L
Linus Torvalds 已提交
284 285
}

286
static inline void emac_rx_disable_async(struct ocp_enet_private *dev)
L
Linus Torvalds 已提交
287
{
288 289 290
	struct emac_regs *p = dev->emacp;
	unsigned long flags;
	u32 r;
L
Linus Torvalds 已提交
291

292 293 294 295 296 297 298 299
	local_irq_save(flags);

	DBG("%d: rx_disable_async" NL, dev->def->index);

	r = in_be32(&p->mr0);
	if (r & EMAC_MR0_RXE)
		out_be32(&p->mr0, r & ~EMAC_MR0_RXE);
	local_irq_restore(flags);
L
Linus Torvalds 已提交
300 301
}

302
static int emac_reset(struct ocp_enet_private *dev)
L
Linus Torvalds 已提交
303
{
304 305 306
	struct emac_regs *p = dev->emacp;
	unsigned long flags;
	int n = 20;
L
Linus Torvalds 已提交
307

308
	DBG("%d: reset" NL, dev->def->index);
L
Linus Torvalds 已提交
309

310
	local_irq_save(flags);
L
Linus Torvalds 已提交
311

312 313 314 315 316 317
	if (!dev->reset_failed) {
		/* 40x erratum suggests stopping RX channel before reset,
		 * we stop TX as well
		 */
		emac_rx_disable(dev);
		emac_tx_disable(dev);
L
Linus Torvalds 已提交
318 319
	}

320 321 322 323 324 325 326 327
	out_be32(&p->mr0, EMAC_MR0_SRST);
	while ((in_be32(&p->mr0) & EMAC_MR0_SRST) && n)
		--n;
	local_irq_restore(flags);

	if (n) {
		dev->reset_failed = 0;
		return 0;
L
Linus Torvalds 已提交
328
	} else {
329 330 331
		emac_report_timeout_error(dev, "reset timeout");
		dev->reset_failed = 1;
		return -ETIMEDOUT;
L
Linus Torvalds 已提交
332
	}
333
}
L
Linus Torvalds 已提交
334

335 336 337 338 339
static void emac_hash_mc(struct ocp_enet_private *dev)
{
	struct emac_regs *p = dev->emacp;
	u16 gaht[4] = { 0 };
	struct dev_mc_list *dmi;
L
Linus Torvalds 已提交
340

341
	DBG("%d: hash_mc %d" NL, dev->def->index, dev->ndev->mc_count);
L
Linus Torvalds 已提交
342

343 344 345 346 347 348
	for (dmi = dev->ndev->mc_list; dmi; dmi = dmi->next) {
		int bit;
		DBG2("%d: mc %02x:%02x:%02x:%02x:%02x:%02x" NL,
		     dev->def->index,
		     dmi->dmi_addr[0], dmi->dmi_addr[1], dmi->dmi_addr[2],
		     dmi->dmi_addr[3], dmi->dmi_addr[4], dmi->dmi_addr[5]);
L
Linus Torvalds 已提交
349

350 351 352 353 354 355 356
		bit = 63 - (ether_crc(ETH_ALEN, dmi->dmi_addr) >> 26);
		gaht[bit >> 4] |= 0x8000 >> (bit & 0x0f);
	}
	out_be32(&p->gaht1, gaht[0]);
	out_be32(&p->gaht2, gaht[1]);
	out_be32(&p->gaht3, gaht[2]);
	out_be32(&p->gaht4, gaht[3]);
L
Linus Torvalds 已提交
357 358
}

359
static inline u32 emac_iff2rmr(struct net_device *ndev)
L
Linus Torvalds 已提交
360
{
361 362 363 364 365 366 367 368 369
	u32 r = EMAC_RMR_SP | EMAC_RMR_SFCS | EMAC_RMR_IAE | EMAC_RMR_BAE |
	    EMAC_RMR_BASE;

	if (ndev->flags & IFF_PROMISC)
		r |= EMAC_RMR_PME;
	else if (ndev->flags & IFF_ALLMULTI || ndev->mc_count > 32)
		r |= EMAC_RMR_PMME;
	else if (ndev->mc_count > 0)
		r |= EMAC_RMR_MAE;
L
Linus Torvalds 已提交
370

371
	return r;
L
Linus Torvalds 已提交
372 373
}

374
static inline int emac_opb_mhz(void)
L
Linus Torvalds 已提交
375
{
376
	return (ocp_sys_info.opb_bus_freq + 500000) / 1000000;
L
Linus Torvalds 已提交
377 378
}

379 380
/* BHs disabled */
static int emac_configure(struct ocp_enet_private *dev)
L
Linus Torvalds 已提交
381
{
382 383 384 385
	struct emac_regs *p = dev->emacp;
	struct net_device *ndev = dev->ndev;
	int gige;
	u32 r;
L
Linus Torvalds 已提交
386

387
	DBG("%d: configure" NL, dev->def->index);
L
Linus Torvalds 已提交
388

389 390
	if (emac_reset(dev) < 0)
		return -ETIMEDOUT;
L
Linus Torvalds 已提交
391

392
	tah_reset(dev->tah_dev);
L
Linus Torvalds 已提交
393

394 395 396 397 398 399 400 401 402
	/* Mode register */
	r = EMAC_MR1_BASE(emac_opb_mhz()) | EMAC_MR1_VLE | EMAC_MR1_IST;
	if (dev->phy.duplex == DUPLEX_FULL)
		r |= EMAC_MR1_FDE;
	switch (dev->phy.speed) {
	case SPEED_1000:
		if (emac_phy_gpcs(dev->phy.mode)) {
			r |= EMAC_MR1_MF_1000GPCS |
			    EMAC_MR1_MF_IPPA(dev->phy.address);
L
Linus Torvalds 已提交
403

404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
			/* Put some arbitrary OUI, Manuf & Rev IDs so we can
			 * identify this GPCS PHY later.
			 */
			out_be32(&p->ipcr, 0xdeadbeef);
		} else
			r |= EMAC_MR1_MF_1000;
		r |= EMAC_MR1_RFS_16K;
		gige = 1;
		
		if (dev->ndev->mtu > ETH_DATA_LEN)
			r |= EMAC_MR1_JPSM;
		break;
	case SPEED_100:
		r |= EMAC_MR1_MF_100;
		/* Fall through */
	default:
		r |= EMAC_MR1_RFS_4K;
		gige = 0;
		break;
L
Linus Torvalds 已提交
423 424
	}

425 426 427 428 429
	if (dev->rgmii_dev)
		rgmii_set_speed(dev->rgmii_dev, dev->rgmii_input,
				dev->phy.speed);
	else
		zmii_set_speed(dev->zmii_dev, dev->zmii_input, dev->phy.speed);
L
Linus Torvalds 已提交
430

431 432 433 434 435 436 437 438 439
#if !defined(CONFIG_40x)
	/* on 40x erratum forces us to NOT use integrated flow control, 
	 * let's hope it works on 44x ;)
	 */
	if (dev->phy.duplex == DUPLEX_FULL) {
		if (dev->phy.pause)
			r |= EMAC_MR1_EIFC | EMAC_MR1_APP;
		else if (dev->phy.asym_pause)
			r |= EMAC_MR1_APP;
L
Linus Torvalds 已提交
440
	}
441 442 443 444 445 446 447 448 449 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
#endif
	out_be32(&p->mr1, r);

	/* Set individual MAC address */
	out_be32(&p->iahr, (ndev->dev_addr[0] << 8) | ndev->dev_addr[1]);
	out_be32(&p->ialr, (ndev->dev_addr[2] << 24) |
		 (ndev->dev_addr[3] << 16) | (ndev->dev_addr[4] << 8) |
		 ndev->dev_addr[5]);

	/* VLAN Tag Protocol ID */
	out_be32(&p->vtpid, 0x8100);

	/* Receive mode register */
	r = emac_iff2rmr(ndev);
	if (r & EMAC_RMR_MAE)
		emac_hash_mc(dev);
	out_be32(&p->rmr, r);

	/* FIFOs thresholds */
	r = EMAC_TMR1((EMAC_MAL_BURST_SIZE / EMAC_FIFO_ENTRY_SIZE) + 1,
		      EMAC_TX_FIFO_SIZE / 2 / EMAC_FIFO_ENTRY_SIZE);
	out_be32(&p->tmr1, r);
	out_be32(&p->trtr, EMAC_TRTR(EMAC_TX_FIFO_SIZE / 2));

	/* PAUSE frame is sent when RX FIFO reaches its high-water mark,
	   there should be still enough space in FIFO to allow the our link
	   partner time to process this frame and also time to send PAUSE 
	   frame itself.

	   Here is the worst case scenario for the RX FIFO "headroom"
	   (from "The Switch Book") (100Mbps, without preamble, inter-frame gap):

	   1) One maximum-length frame on TX                    1522 bytes
	   2) One PAUSE frame time                                64 bytes
	   3) PAUSE frame decode time allowance                   64 bytes
	   4) One maximum-length frame on RX                    1522 bytes
	   5) Round-trip propagation delay of the link (100Mb)    15 bytes
	   ----------       
	   3187 bytes

	   I chose to set high-water mark to RX_FIFO_SIZE / 4 (1024 bytes)
	   low-water mark  to RX_FIFO_SIZE / 8 (512 bytes)
	 */
	r = EMAC_RWMR(EMAC_RX_FIFO_SIZE(gige) / 8 / EMAC_FIFO_ENTRY_SIZE,
		      EMAC_RX_FIFO_SIZE(gige) / 4 / EMAC_FIFO_ENTRY_SIZE);
	out_be32(&p->rwmr, r);

	/* Set PAUSE timer to the maximum */
	out_be32(&p->ptr, 0xffff);

	/* IRQ sources */
	out_be32(&p->iser, EMAC_ISR_TXPE | EMAC_ISR_RXPE | /* EMAC_ISR_TXUE |
		 EMAC_ISR_RXOE | */ EMAC_ISR_OVR | EMAC_ISR_BP | EMAC_ISR_SE |
		 EMAC_ISR_ALE | EMAC_ISR_BFCS | EMAC_ISR_PTLE | EMAC_ISR_ORE |
		 EMAC_ISR_IRE | EMAC_ISR_TE);
		 
	/* We need to take GPCS PHY out of isolate mode after EMAC reset */
	if (emac_phy_gpcs(dev->phy.mode)) 
		mii_reset_phy(&dev->phy);
		 
	return 0;
}
L
Linus Torvalds 已提交
503

504 505 506 507
/* BHs disabled */
static void emac_reinitialize(struct ocp_enet_private *dev)
{
	DBG("%d: reinitialize" NL, dev->def->index);
L
Linus Torvalds 已提交
508

509 510 511 512 513
	if (!emac_configure(dev)) {
		emac_tx_enable(dev);
		emac_rx_enable(dev);
	}
}
L
Linus Torvalds 已提交
514

515 516 517 518 519
/* BHs disabled */
static void emac_full_tx_reset(struct net_device *ndev)
{
	struct ocp_enet_private *dev = ndev->priv;
	struct ocp_func_emac_data *emacdata = dev->def->additions;
L
Linus Torvalds 已提交
520

521
	DBG("%d: full_tx_reset" NL, dev->def->index);
L
Linus Torvalds 已提交
522

523 524 525 526 527 528
	emac_tx_disable(dev);
	mal_disable_tx_channel(dev->mal, emacdata->mal_tx_chan);
	emac_clean_tx_ring(dev);
	dev->tx_cnt = dev->tx_slot = dev->ack_slot = 0;

	emac_configure(dev);
L
Linus Torvalds 已提交
529

530 531 532
	mal_enable_tx_channel(dev->mal, emacdata->mal_tx_chan);
	emac_tx_enable(dev);
	emac_rx_enable(dev);
L
Linus Torvalds 已提交
533

534
	netif_wake_queue(ndev);
L
Linus Torvalds 已提交
535 536
}

537
static int __emac_mdio_read(struct ocp_enet_private *dev, u8 id, u8 reg)
L
Linus Torvalds 已提交
538
{
539 540 541
	struct emac_regs *p = dev->emacp;
	u32 r;
	int n;
L
Linus Torvalds 已提交
542

543
	DBG2("%d: mdio_read(%02x,%02x)" NL, dev->def->index, id, reg);
L
Linus Torvalds 已提交
544

545 546
	/* Enable proper MDIO port */
	zmii_enable_mdio(dev->zmii_dev, dev->zmii_input);
L
Linus Torvalds 已提交
547

548 549
	/* Wait for management interface to become idle */
	n = 10;
550
	while (!emac_phy_done(in_be32(&p->stacr))) {
551 552 553
		udelay(1);
		if (!--n)
			goto to;
L
Linus Torvalds 已提交
554 555
	}

556 557 558 559
	/* Issue read command */
	out_be32(&p->stacr,
		 EMAC_STACR_BASE(emac_opb_mhz()) | EMAC_STACR_STAC_READ |
		 (reg & EMAC_STACR_PRA_MASK)
560 561
		 | ((id & EMAC_STACR_PCDA_MASK) << EMAC_STACR_PCDA_SHIFT)
		 | EMAC_STACR_START);
562 563 564

	/* Wait for read to complete */
	n = 100;
565
	while (!emac_phy_done(r = in_be32(&p->stacr))) {
L
Linus Torvalds 已提交
566
		udelay(1);
567 568 569
		if (!--n)
			goto to;
	}
L
Linus Torvalds 已提交
570

571 572 573 574
	if (unlikely(r & EMAC_STACR_PHYE)) {
		DBG("%d: mdio_read(%02x, %02x) failed" NL, dev->def->index,
		    id, reg);
		return -EREMOTEIO;
L
Linus Torvalds 已提交
575 576
	}

577 578 579 580 581 582 583 584 585 586 587 588 589
	r = ((r >> EMAC_STACR_PHYD_SHIFT) & EMAC_STACR_PHYD_MASK);
	DBG2("%d: mdio_read -> %04x" NL, dev->def->index, r);
	return r;
      to:
	DBG("%d: MII management interface timeout (read)" NL, dev->def->index);
	return -ETIMEDOUT;
}

static void __emac_mdio_write(struct ocp_enet_private *dev, u8 id, u8 reg,
			      u16 val)
{
	struct emac_regs *p = dev->emacp;
	int n;
L
Linus Torvalds 已提交
590

591 592
	DBG2("%d: mdio_write(%02x,%02x,%04x)" NL, dev->def->index, id, reg,
	     val);
L
Linus Torvalds 已提交
593

594 595
	/* Enable proper MDIO port */
	zmii_enable_mdio(dev->zmii_dev, dev->zmii_input);
L
Linus Torvalds 已提交
596

597 598
	/* Wait for management interface to be idle */
	n = 10;
599
	while (!emac_phy_done(in_be32(&p->stacr))) {
L
Linus Torvalds 已提交
600
		udelay(1);
601 602 603
		if (!--n)
			goto to;
	}
L
Linus Torvalds 已提交
604

605 606 607 608 609
	/* Issue write command */
	out_be32(&p->stacr,
		 EMAC_STACR_BASE(emac_opb_mhz()) | EMAC_STACR_STAC_WRITE |
		 (reg & EMAC_STACR_PRA_MASK) |
		 ((id & EMAC_STACR_PCDA_MASK) << EMAC_STACR_PCDA_SHIFT) |
610
		 (val << EMAC_STACR_PHYD_SHIFT) | EMAC_STACR_START);
L
Linus Torvalds 已提交
611

612 613
	/* Wait for write to complete */
	n = 100;
614
	while (!emac_phy_done(in_be32(&p->stacr))) {
615 616 617
		udelay(1);
		if (!--n)
			goto to;
L
Linus Torvalds 已提交
618
	}
619 620 621
	return;
      to:
	DBG("%d: MII management interface timeout (write)" NL, dev->def->index);
L
Linus Torvalds 已提交
622 623
}

624
static int emac_mdio_read(struct net_device *ndev, int id, int reg)
L
Linus Torvalds 已提交
625
{
626 627 628 629 630 631 632 633 634
	struct ocp_enet_private *dev = ndev->priv;
	int res;

	local_bh_disable();
	res = __emac_mdio_read(dev->mdio_dev ? dev->mdio_dev : dev, (u8) id,
			       (u8) reg);
	local_bh_enable();
	return res;
}
L
Linus Torvalds 已提交
635

636 637 638
static void emac_mdio_write(struct net_device *ndev, int id, int reg, int val)
{
	struct ocp_enet_private *dev = ndev->priv;
L
Linus Torvalds 已提交
639

640 641 642 643 644
	local_bh_disable();
	__emac_mdio_write(dev->mdio_dev ? dev->mdio_dev : dev, (u8) id,
			  (u8) reg, (u16) val);
	local_bh_enable();
}
L
Linus Torvalds 已提交
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 672 673 674 675
/* BHs disabled */
static void emac_set_multicast_list(struct net_device *ndev)
{
	struct ocp_enet_private *dev = ndev->priv;
	struct emac_regs *p = dev->emacp;
	u32 rmr = emac_iff2rmr(ndev);

	DBG("%d: multicast %08x" NL, dev->def->index, rmr);
	BUG_ON(!netif_running(dev->ndev));

	/* I decided to relax register access rules here to avoid
	 * full EMAC reset.
	 *
	 * There is a real problem with EMAC4 core if we use MWSW_001 bit 
	 * in MR1 register and do a full EMAC reset.
	 * One TX BD status update is delayed and, after EMAC reset, it 
	 * never happens, resulting in TX hung (it'll be recovered by TX 
	 * timeout handler eventually, but this is just gross).
	 * So we either have to do full TX reset or try to cheat here :)
	 *
	 * The only required change is to RX mode register, so I *think* all
	 * we need is just to stop RX channel. This seems to work on all
	 * tested SoCs.                                                --ebs
	 */
	emac_rx_disable(dev);
	if (rmr & EMAC_RMR_MAE)
		emac_hash_mc(dev);
	out_be32(&p->rmr, rmr);
	emac_rx_enable(dev);
}
L
Linus Torvalds 已提交
676

677 678 679 680 681 682 683 684 685 686 687 688 689 690 691
/* BHs disabled */
static int emac_resize_rx_ring(struct ocp_enet_private *dev, int new_mtu)
{
	struct ocp_func_emac_data *emacdata = dev->def->additions;
	int rx_sync_size = emac_rx_sync_size(new_mtu);
	int rx_skb_size = emac_rx_skb_size(new_mtu);
	int i, ret = 0;

	emac_rx_disable(dev);
	mal_disable_rx_channel(dev->mal, emacdata->mal_rx_chan);

	if (dev->rx_sg_skb) {
		++dev->estats.rx_dropped_resize;
		dev_kfree_skb(dev->rx_sg_skb);
		dev->rx_sg_skb = NULL;
L
Linus Torvalds 已提交
692 693
	}

694 695 696 697 698 699 700
	/* Make a first pass over RX ring and mark BDs ready, dropping 
	 * non-processed packets on the way. We need this as a separate pass
	 * to simplify error recovery in the case of allocation failure later.
	 */
	for (i = 0; i < NUM_RX_BUFF; ++i) {
		if (dev->rx_desc[i].ctrl & MAL_RX_CTRL_FIRST)
			++dev->estats.rx_dropped_resize;
L
Linus Torvalds 已提交
701

702 703 704 705
		dev->rx_desc[i].data_len = 0;
		dev->rx_desc[i].ctrl = MAL_RX_CTRL_EMPTY |
		    (i == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
	}
L
Linus Torvalds 已提交
706

707 708 709
	/* Reallocate RX ring only if bigger skb buffers are required */
	if (rx_skb_size <= dev->rx_skb_size)
		goto skip;
L
Linus Torvalds 已提交
710

711 712 713 714 715 716
	/* Second pass, allocate new skbs */
	for (i = 0; i < NUM_RX_BUFF; ++i) {
		struct sk_buff *skb = alloc_skb(rx_skb_size, GFP_ATOMIC);
		if (!skb) {
			ret = -ENOMEM;
			goto oom;
L
Linus Torvalds 已提交
717 718
		}

719 720
		BUG_ON(!dev->rx_skb[i]);
		dev_kfree_skb(dev->rx_skb[i]);
L
Linus Torvalds 已提交
721

722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
		skb_reserve(skb, EMAC_RX_SKB_HEADROOM + 2);
		dev->rx_desc[i].data_ptr =
		    dma_map_single(dev->ldev, skb->data - 2, rx_sync_size,
				   DMA_FROM_DEVICE) + 2;
		dev->rx_skb[i] = skb;
	}
      skip:
	/* Check if we need to change "Jumbo" bit in MR1 */
	if ((new_mtu > ETH_DATA_LEN) ^ (dev->ndev->mtu > ETH_DATA_LEN)) {
		/* This is to prevent starting RX channel in emac_rx_enable() */
		dev->commac.rx_stopped = 1;

		dev->ndev->mtu = new_mtu;
		emac_full_tx_reset(dev->ndev);
	}
L
Linus Torvalds 已提交
737

738 739 740 741 742 743
	mal_set_rcbs(dev->mal, emacdata->mal_rx_chan, emac_rx_size(new_mtu));
      oom:
	/* Restart RX */
	dev->commac.rx_stopped = dev->rx_slot = 0;
	mal_enable_rx_channel(dev->mal, emacdata->mal_rx_chan);
	emac_rx_enable(dev);
L
Linus Torvalds 已提交
744

745
	return ret;
L
Linus Torvalds 已提交
746 747
}

748 749
/* Process ctx, rtnl_lock semaphore */
static int emac_change_mtu(struct net_device *ndev, int new_mtu)
L
Linus Torvalds 已提交
750
{
751 752
	struct ocp_enet_private *dev = ndev->priv;
	int ret = 0;
L
Linus Torvalds 已提交
753

754 755
	if (new_mtu < EMAC_MIN_MTU || new_mtu > EMAC_MAX_MTU)
		return -EINVAL;
L
Linus Torvalds 已提交
756

757
	DBG("%d: change_mtu(%d)" NL, dev->def->index, new_mtu);
L
Linus Torvalds 已提交
758

759 760 761 762 763 764
	local_bh_disable();
	if (netif_running(ndev)) {
		/* Check if we really need to reinitalize RX ring */
		if (emac_rx_skb_size(ndev->mtu) != emac_rx_skb_size(new_mtu))
			ret = emac_resize_rx_ring(dev, new_mtu);
	}
L
Linus Torvalds 已提交
765

766 767 768 769 770 771
	if (!ret) {
		ndev->mtu = new_mtu;
		dev->rx_skb_size = emac_rx_skb_size(new_mtu);
		dev->rx_sync_size = emac_rx_sync_size(new_mtu);
	}	
	local_bh_enable();
L
Linus Torvalds 已提交
772

773 774
	return ret;
}
L
Linus Torvalds 已提交
775

776 777 778 779 780 781 782 783 784
static void emac_clean_tx_ring(struct ocp_enet_private *dev)
{
	int i;
	for (i = 0; i < NUM_TX_BUFF; ++i) {
		if (dev->tx_skb[i]) {
			dev_kfree_skb(dev->tx_skb[i]);
			dev->tx_skb[i] = NULL;
			if (dev->tx_desc[i].ctrl & MAL_TX_CTRL_READY)
				++dev->estats.tx_dropped;
L
Linus Torvalds 已提交
785
		}
786 787 788
		dev->tx_desc[i].ctrl = 0;
		dev->tx_desc[i].data_ptr = 0;
	}
L
Linus Torvalds 已提交
789 790
}

791
static void emac_clean_rx_ring(struct ocp_enet_private *dev)
L
Linus Torvalds 已提交
792
{
793 794 795 796 797 798 799 800
	int i;
	for (i = 0; i < NUM_RX_BUFF; ++i)
		if (dev->rx_skb[i]) {
			dev->rx_desc[i].ctrl = 0;
			dev_kfree_skb(dev->rx_skb[i]);
			dev->rx_skb[i] = NULL;
			dev->rx_desc[i].data_ptr = 0;
		}
L
Linus Torvalds 已提交
801

802 803 804 805
	if (dev->rx_sg_skb) {
		dev_kfree_skb(dev->rx_sg_skb);
		dev->rx_sg_skb = NULL;
	}
L
Linus Torvalds 已提交
806 807
}

808 809
static inline int emac_alloc_rx_skb(struct ocp_enet_private *dev, int slot,
				    int flags)
L
Linus Torvalds 已提交
810
{
811 812 813
	struct sk_buff *skb = alloc_skb(dev->rx_skb_size, flags);
	if (unlikely(!skb))
		return -ENOMEM;
L
Linus Torvalds 已提交
814

815 816
	dev->rx_skb[slot] = skb;
	dev->rx_desc[slot].data_len = 0;
L
Linus Torvalds 已提交
817

818 819 820 821 822 823 824
	skb_reserve(skb, EMAC_RX_SKB_HEADROOM + 2);
	dev->rx_desc[slot].data_ptr = 
	    dma_map_single(dev->ldev, skb->data - 2, dev->rx_sync_size, 
			   DMA_FROM_DEVICE) + 2;
	barrier();
	dev->rx_desc[slot].ctrl = MAL_RX_CTRL_EMPTY |
	    (slot == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
L
Linus Torvalds 已提交
825

826
	return 0;
L
Linus Torvalds 已提交
827 828
}

829
static void emac_print_link_status(struct ocp_enet_private *dev)
L
Linus Torvalds 已提交
830
{
831 832 833 834 835 836 837 838 839
	if (netif_carrier_ok(dev->ndev))
		printk(KERN_INFO "%s: link is up, %d %s%s\n",
		       dev->ndev->name, dev->phy.speed,
		       dev->phy.duplex == DUPLEX_FULL ? "FDX" : "HDX",
		       dev->phy.pause ? ", pause enabled" :
		       dev->phy.asym_pause ? ", assymetric pause enabled" : "");
	else
		printk(KERN_INFO "%s: link is down\n", dev->ndev->name);
}
L
Linus Torvalds 已提交
840

841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
/* Process ctx, rtnl_lock semaphore */
static int emac_open(struct net_device *ndev)
{
	struct ocp_enet_private *dev = ndev->priv;
	struct ocp_func_emac_data *emacdata = dev->def->additions;
	int err, i;

	DBG("%d: open" NL, dev->def->index);

	/* Setup error IRQ handler */
	err = request_irq(dev->def->irq, emac_irq, 0, "EMAC", dev);
	if (err) {
		printk(KERN_ERR "%s: failed to request IRQ %d\n",
		       ndev->name, dev->def->irq);
		return err;
L
Linus Torvalds 已提交
856 857
	}

858 859 860 861 862 863 864
	/* Allocate RX ring */
	for (i = 0; i < NUM_RX_BUFF; ++i)
		if (emac_alloc_rx_skb(dev, i, GFP_KERNEL)) {
			printk(KERN_ERR "%s: failed to allocate RX ring\n",
			       ndev->name);
			goto oom;
		}
L
Linus Torvalds 已提交
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 896
	local_bh_disable();
	dev->tx_cnt = dev->tx_slot = dev->ack_slot = dev->rx_slot =
	    dev->commac.rx_stopped = 0;
	dev->rx_sg_skb = NULL;

	if (dev->phy.address >= 0) {
		int link_poll_interval;
		if (dev->phy.def->ops->poll_link(&dev->phy)) {
			dev->phy.def->ops->read_link(&dev->phy);
			EMAC_RX_CLK_DEFAULT(dev->def->index);
			netif_carrier_on(dev->ndev);
			link_poll_interval = PHY_POLL_LINK_ON;
		} else {
			EMAC_RX_CLK_TX(dev->def->index);
			netif_carrier_off(dev->ndev);
			link_poll_interval = PHY_POLL_LINK_OFF;
		}
		mod_timer(&dev->link_timer, jiffies + link_poll_interval);
		emac_print_link_status(dev);
	} else
		netif_carrier_on(dev->ndev);

	emac_configure(dev);
	mal_poll_add(dev->mal, &dev->commac);
	mal_enable_tx_channel(dev->mal, emacdata->mal_tx_chan);
	mal_set_rcbs(dev->mal, emacdata->mal_rx_chan, emac_rx_size(ndev->mtu));
	mal_enable_rx_channel(dev->mal, emacdata->mal_rx_chan);
	emac_tx_enable(dev);
	emac_rx_enable(dev);
	netif_start_queue(ndev);
	local_bh_enable();
L
Linus Torvalds 已提交
897

898 899 900 901 902
	return 0;
      oom:
	emac_clean_rx_ring(dev);
	free_irq(dev->def->irq, dev);
	return -ENOMEM;
L
Linus Torvalds 已提交
903 904
}

905 906
/* BHs disabled */
static int emac_link_differs(struct ocp_enet_private *dev)
L
Linus Torvalds 已提交
907
{
908
	u32 r = in_be32(&dev->emacp->mr1);
L
Linus Torvalds 已提交
909

910 911
	int duplex = r & EMAC_MR1_FDE ? DUPLEX_FULL : DUPLEX_HALF;
	int speed, pause, asym_pause;
L
Linus Torvalds 已提交
912

913 914 915 916 917 918
	if (r & (EMAC_MR1_MF_1000 | EMAC_MR1_MF_1000GPCS))
		speed = SPEED_1000;
	else if (r & EMAC_MR1_MF_100)
		speed = SPEED_100;
	else
		speed = SPEED_10;
L
Linus Torvalds 已提交
919

920 921 922 923 924 925 926 927 928 929 930
	switch (r & (EMAC_MR1_EIFC | EMAC_MR1_APP)) {
	case (EMAC_MR1_EIFC | EMAC_MR1_APP):
		pause = 1;
		asym_pause = 0;
		break;
	case EMAC_MR1_APP:
		pause = 0;
		asym_pause = 1;
		break;
	default:
		pause = asym_pause = 0;
L
Linus Torvalds 已提交
931
	}
932 933
	return speed != dev->phy.speed || duplex != dev->phy.duplex ||
	    pause != dev->phy.pause || asym_pause != dev->phy.asym_pause;
L
Linus Torvalds 已提交
934 935
}

936 937
/* BHs disabled */
static void emac_link_timer(unsigned long data)
L
Linus Torvalds 已提交
938
{
939 940
	struct ocp_enet_private *dev = (struct ocp_enet_private *)data;
	int link_poll_interval;
L
Linus Torvalds 已提交
941

942
	DBG2("%d: link timer" NL, dev->def->index);
L
Linus Torvalds 已提交
943

944 945 946
	if (dev->phy.def->ops->poll_link(&dev->phy)) {
		if (!netif_carrier_ok(dev->ndev)) {
			EMAC_RX_CLK_DEFAULT(dev->def->index);
L
Linus Torvalds 已提交
947

948 949
			/* Get new link parameters */
			dev->phy.def->ops->read_link(&dev->phy);
L
Linus Torvalds 已提交
950

951 952
			if (dev->tah_dev || emac_link_differs(dev))
				emac_full_tx_reset(dev->ndev);
L
Linus Torvalds 已提交
953

954 955 956 957 958 959 960 961 962 963 964 965
			netif_carrier_on(dev->ndev);
			emac_print_link_status(dev);
		}
		link_poll_interval = PHY_POLL_LINK_ON;
	} else {
		if (netif_carrier_ok(dev->ndev)) {
			EMAC_RX_CLK_TX(dev->def->index);
#if defined(CONFIG_IBM_EMAC_PHY_RX_CLK_FIX)
			emac_reinitialize(dev);
#endif
			netif_carrier_off(dev->ndev);
			emac_print_link_status(dev);
L
Linus Torvalds 已提交
966 967
		}

968 969 970 971 972 973 974
		/* Retry reset if the previous attempt failed.
		 * This is needed mostly for CONFIG_IBM_EMAC_PHY_RX_CLK_FIX
		 * case, but I left it here because it shouldn't trigger for
		 * sane PHYs anyway.
		 */
		if (unlikely(dev->reset_failed))
			emac_reinitialize(dev);
L
Linus Torvalds 已提交
975

976
		link_poll_interval = PHY_POLL_LINK_OFF;
L
Linus Torvalds 已提交
977
	}
978 979
	mod_timer(&dev->link_timer, jiffies + link_poll_interval);
}
L
Linus Torvalds 已提交
980

981 982 983 984 985 986 987
/* BHs disabled */
static void emac_force_link_update(struct ocp_enet_private *dev)
{
	netif_carrier_off(dev->ndev);
	if (timer_pending(&dev->link_timer))
		mod_timer(&dev->link_timer, jiffies + PHY_POLL_LINK_OFF);
}
L
Linus Torvalds 已提交
988

989 990 991 992 993
/* Process ctx, rtnl_lock semaphore */
static int emac_close(struct net_device *ndev)
{
	struct ocp_enet_private *dev = ndev->priv;
	struct ocp_func_emac_data *emacdata = dev->def->additions;
L
Linus Torvalds 已提交
994

995
	DBG("%d: close" NL, dev->def->index);
L
Linus Torvalds 已提交
996

997
	local_bh_disable();
L
Linus Torvalds 已提交
998

999 1000
	if (dev->phy.address >= 0)
		del_timer_sync(&dev->link_timer);
L
Linus Torvalds 已提交
1001

1002 1003 1004 1005 1006 1007 1008
	netif_stop_queue(ndev);
	emac_rx_disable(dev);
	emac_tx_disable(dev);
	mal_disable_rx_channel(dev->mal, emacdata->mal_rx_chan);
	mal_disable_tx_channel(dev->mal, emacdata->mal_tx_chan);
	mal_poll_del(dev->mal, &dev->commac);
	local_bh_enable();
L
Linus Torvalds 已提交
1009

1010 1011 1012
	emac_clean_tx_ring(dev);
	emac_clean_rx_ring(dev);
	free_irq(dev->def->irq, dev);
L
Linus Torvalds 已提交
1013 1014 1015 1016

	return 0;
}

1017 1018
static inline u16 emac_tx_csum(struct ocp_enet_private *dev,
			       struct sk_buff *skb)
L
Linus Torvalds 已提交
1019
{
1020 1021 1022 1023
#if defined(CONFIG_IBM_EMAC_TAH)
	if (skb->ip_summed == CHECKSUM_HW) {
		++dev->stats.tx_packets_csum;
		return EMAC_TX_CTRL_TAH_CSUM;
L
Linus Torvalds 已提交
1024
	}
1025 1026 1027
#endif
	return 0;
}
L
Linus Torvalds 已提交
1028

1029 1030 1031 1032
static inline int emac_xmit_finish(struct ocp_enet_private *dev, int len)
{
	struct emac_regs *p = dev->emacp;
	struct net_device *ndev = dev->ndev;
L
Linus Torvalds 已提交
1033

1034 1035
	/* Send the packet out */
	out_be32(&p->tmr0, EMAC_TMR0_XMIT);
L
Linus Torvalds 已提交
1036

1037 1038 1039 1040
	if (unlikely(++dev->tx_cnt == NUM_TX_BUFF)) {
		netif_stop_queue(ndev);
		DBG2("%d: stopped TX queue" NL, dev->def->index);
	}
L
Linus Torvalds 已提交
1041

1042 1043 1044
	ndev->trans_start = jiffies;
	++dev->stats.tx_packets;
	dev->stats.tx_bytes += len;
L
Linus Torvalds 已提交
1045 1046 1047 1048

	return 0;
}

1049 1050
/* BHs disabled */
static int emac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
L
Linus Torvalds 已提交
1051
{
1052 1053 1054
	struct ocp_enet_private *dev = ndev->priv;
	unsigned int len = skb->len;
	int slot;
L
Linus Torvalds 已提交
1055

1056 1057
	u16 ctrl = EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP | MAL_TX_CTRL_READY |
	    MAL_TX_CTRL_LAST | emac_tx_csum(dev, skb);
L
Linus Torvalds 已提交
1058

1059 1060 1061 1062 1063
	slot = dev->tx_slot++;
	if (dev->tx_slot == NUM_TX_BUFF) {
		dev->tx_slot = 0;
		ctrl |= MAL_TX_CTRL_WRAP;
	}
L
Linus Torvalds 已提交
1064

1065
	DBG2("%d: xmit(%u) %d" NL, dev->def->index, len, slot);
L
Linus Torvalds 已提交
1066

1067 1068 1069 1070 1071 1072
	dev->tx_skb[slot] = skb;
	dev->tx_desc[slot].data_ptr = dma_map_single(dev->ldev, skb->data, len,
						     DMA_TO_DEVICE);
	dev->tx_desc[slot].data_len = (u16) len;
	barrier();
	dev->tx_desc[slot].ctrl = ctrl;
L
Linus Torvalds 已提交
1073

1074
	return emac_xmit_finish(dev, len);
L
Linus Torvalds 已提交
1075 1076
}

1077 1078 1079
#if defined(CONFIG_IBM_EMAC_TAH)
static inline int emac_xmit_split(struct ocp_enet_private *dev, int slot,
				  u32 pd, int len, int last, u16 base_ctrl)
L
Linus Torvalds 已提交
1080
{
1081 1082 1083 1084
	while (1) {
		u16 ctrl = base_ctrl;
		int chunk = min(len, MAL_MAX_TX_SIZE);
		len -= chunk;
L
Linus Torvalds 已提交
1085

1086
		slot = (slot + 1) % NUM_TX_BUFF;
L
Linus Torvalds 已提交
1087

1088 1089 1090 1091
		if (last && !len)
			ctrl |= MAL_TX_CTRL_LAST;
		if (slot == NUM_TX_BUFF - 1)
			ctrl |= MAL_TX_CTRL_WRAP;
L
Linus Torvalds 已提交
1092

1093 1094 1095 1096 1097
		dev->tx_skb[slot] = NULL;
		dev->tx_desc[slot].data_ptr = pd;
		dev->tx_desc[slot].data_len = (u16) chunk;
		dev->tx_desc[slot].ctrl = ctrl;
		++dev->tx_cnt;
L
Linus Torvalds 已提交
1098

1099 1100
		if (!len)
			break;
L
Linus Torvalds 已提交
1101

1102
		pd += chunk;
L
Linus Torvalds 已提交
1103
	}
1104
	return slot;
L
Linus Torvalds 已提交
1105 1106
}

1107 1108
/* BHs disabled (SG version for TAH equipped EMACs) */
static int emac_start_xmit_sg(struct sk_buff *skb, struct net_device *ndev)
L
Linus Torvalds 已提交
1109
{
1110 1111 1112 1113 1114 1115
	struct ocp_enet_private *dev = ndev->priv;
	int nr_frags = skb_shinfo(skb)->nr_frags;
	int len = skb->len, chunk;
	int slot, i;
	u16 ctrl;
	u32 pd;
L
Linus Torvalds 已提交
1116

1117 1118 1119
	/* This is common "fast" path */
	if (likely(!nr_frags && len <= MAL_MAX_TX_SIZE))
		return emac_start_xmit(skb, ndev);
L
Linus Torvalds 已提交
1120

1121
	len -= skb->data_len;
L
Linus Torvalds 已提交
1122

1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	/* Note, this is only an *estimation*, we can still run out of empty
	 * slots because of the additional fragmentation into
	 * MAL_MAX_TX_SIZE-sized chunks
	 */
	if (unlikely(dev->tx_cnt + nr_frags + mal_tx_chunks(len) > NUM_TX_BUFF))
		goto stop_queue;

	ctrl = EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP | MAL_TX_CTRL_READY |
	    emac_tx_csum(dev, skb);
	slot = dev->tx_slot;

	/* skb data */
	dev->tx_skb[slot] = NULL;
	chunk = min(len, MAL_MAX_TX_SIZE);
	dev->tx_desc[slot].data_ptr = pd =
	    dma_map_single(dev->ldev, skb->data, len, DMA_TO_DEVICE);
	dev->tx_desc[slot].data_len = (u16) chunk;
	len -= chunk;
	if (unlikely(len))
		slot = emac_xmit_split(dev, slot, pd + chunk, len, !nr_frags,
				       ctrl);
	/* skb fragments */
	for (i = 0; i < nr_frags; ++i) {
		struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
		len = frag->size;
L
Linus Torvalds 已提交
1148

1149 1150
		if (unlikely(dev->tx_cnt + mal_tx_chunks(len) >= NUM_TX_BUFF))
			goto undo_frame;
L
Linus Torvalds 已提交
1151

1152 1153
		pd = dma_map_page(dev->ldev, frag->page, frag->page_offset, len,
				  DMA_TO_DEVICE);
L
Linus Torvalds 已提交
1154

1155 1156
		slot = emac_xmit_split(dev, slot, pd, len, i == nr_frags - 1,
				       ctrl);
L
Linus Torvalds 已提交
1157 1158
	}

1159 1160
	DBG2("%d: xmit_sg(%u) %d - %d" NL, dev->def->index, skb->len,
	     dev->tx_slot, slot);
L
Linus Torvalds 已提交
1161

1162 1163
	/* Attach skb to the last slot so we don't release it too early */
	dev->tx_skb[slot] = skb;
L
Linus Torvalds 已提交
1164

1165 1166 1167 1168 1169 1170
	/* Send the packet out */
	if (dev->tx_slot == NUM_TX_BUFF - 1)
		ctrl |= MAL_TX_CTRL_WRAP;
	barrier();
	dev->tx_desc[dev->tx_slot].ctrl = ctrl;
	dev->tx_slot = (slot + 1) % NUM_TX_BUFF;
L
Linus Torvalds 已提交
1171

1172
	return emac_xmit_finish(dev, skb->len);
L
Linus Torvalds 已提交
1173

1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 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
      undo_frame:
	/* Well, too bad. Our previous estimation was overly optimistic. 
	 * Undo everything.
	 */
	while (slot != dev->tx_slot) {
		dev->tx_desc[slot].ctrl = 0;
		--dev->tx_cnt;
		if (--slot < 0)
			slot = NUM_TX_BUFF - 1;
	}
	++dev->estats.tx_undo;

      stop_queue:
	netif_stop_queue(ndev);
	DBG2("%d: stopped TX queue" NL, dev->def->index);
	return 1;
}
#else
# define emac_start_xmit_sg	emac_start_xmit
#endif	/* !defined(CONFIG_IBM_EMAC_TAH) */

/* BHs disabled */
static void emac_parse_tx_error(struct ocp_enet_private *dev, u16 ctrl)
{
	struct ibm_emac_error_stats *st = &dev->estats;
	DBG("%d: BD TX error %04x" NL, dev->def->index, ctrl);

	++st->tx_bd_errors;
	if (ctrl & EMAC_TX_ST_BFCS)
		++st->tx_bd_bad_fcs;
	if (ctrl & EMAC_TX_ST_LCS)
		++st->tx_bd_carrier_loss;
	if (ctrl & EMAC_TX_ST_ED)
		++st->tx_bd_excessive_deferral;
	if (ctrl & EMAC_TX_ST_EC)
		++st->tx_bd_excessive_collisions;
	if (ctrl & EMAC_TX_ST_LC)
		++st->tx_bd_late_collision;
	if (ctrl & EMAC_TX_ST_MC)
		++st->tx_bd_multple_collisions;
	if (ctrl & EMAC_TX_ST_SC)
		++st->tx_bd_single_collision;
	if (ctrl & EMAC_TX_ST_UR)
		++st->tx_bd_underrun;
	if (ctrl & EMAC_TX_ST_SQE)
		++st->tx_bd_sqe;
L
Linus Torvalds 已提交
1220 1221
}

1222
static void emac_poll_tx(void *param)
L
Linus Torvalds 已提交
1223
{
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	struct ocp_enet_private *dev = param;
	DBG2("%d: poll_tx, %d %d" NL, dev->def->index, dev->tx_cnt,
	     dev->ack_slot);

	if (dev->tx_cnt) {
		u16 ctrl;
		int slot = dev->ack_slot, n = 0;
	      again:
		ctrl = dev->tx_desc[slot].ctrl;
		if (!(ctrl & MAL_TX_CTRL_READY)) {
			struct sk_buff *skb = dev->tx_skb[slot];
			++n;

			if (skb) {
				dev_kfree_skb(skb);
				dev->tx_skb[slot] = NULL;
			}
			slot = (slot + 1) % NUM_TX_BUFF;
L
Linus Torvalds 已提交
1242

1243 1244
			if (unlikely(EMAC_IS_BAD_TX(ctrl)))
				emac_parse_tx_error(dev, ctrl);
L
Linus Torvalds 已提交
1245

1246 1247
			if (--dev->tx_cnt)
				goto again;
L
Linus Torvalds 已提交
1248
		}
1249 1250 1251 1252 1253
		if (n) {
			dev->ack_slot = slot;
			if (netif_queue_stopped(dev->ndev) &&
			    dev->tx_cnt < EMAC_TX_WAKEUP_THRESH)
				netif_wake_queue(dev->ndev);
L
Linus Torvalds 已提交
1254

1255 1256 1257
			DBG2("%d: tx %d pkts" NL, dev->def->index, n);
		}
	}
L
Linus Torvalds 已提交
1258 1259
}

1260 1261
static inline void emac_recycle_rx_skb(struct ocp_enet_private *dev, int slot,
				       int len)
L
Linus Torvalds 已提交
1262
{
1263 1264
	struct sk_buff *skb = dev->rx_skb[slot];
	DBG2("%d: recycle %d %d" NL, dev->def->index, slot, len);
L
Linus Torvalds 已提交
1265

1266 1267 1268
	if (len) 
		dma_map_single(dev->ldev, skb->data - 2, 
			       EMAC_DMA_ALIGN(len + 2), DMA_FROM_DEVICE);
L
Linus Torvalds 已提交
1269

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
	dev->rx_desc[slot].data_len = 0;
	barrier();
	dev->rx_desc[slot].ctrl = MAL_RX_CTRL_EMPTY |
	    (slot == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
}

static void emac_parse_rx_error(struct ocp_enet_private *dev, u16 ctrl)
{
	struct ibm_emac_error_stats *st = &dev->estats;
	DBG("%d: BD RX error %04x" NL, dev->def->index, ctrl);

	++st->rx_bd_errors;
	if (ctrl & EMAC_RX_ST_OE)
		++st->rx_bd_overrun;
	if (ctrl & EMAC_RX_ST_BP)
		++st->rx_bd_bad_packet;
	if (ctrl & EMAC_RX_ST_RP)
		++st->rx_bd_runt_packet;
	if (ctrl & EMAC_RX_ST_SE)
		++st->rx_bd_short_event;
	if (ctrl & EMAC_RX_ST_AE)
		++st->rx_bd_alignment_error;
	if (ctrl & EMAC_RX_ST_BFCS)
		++st->rx_bd_bad_fcs;
	if (ctrl & EMAC_RX_ST_PTL)
		++st->rx_bd_packet_too_long;
	if (ctrl & EMAC_RX_ST_ORE)
		++st->rx_bd_out_of_range;
	if (ctrl & EMAC_RX_ST_IRE)
		++st->rx_bd_in_range;
}

static inline void emac_rx_csum(struct ocp_enet_private *dev,
				struct sk_buff *skb, u16 ctrl)
{
#if defined(CONFIG_IBM_EMAC_TAH)
	if (!ctrl && dev->tah_dev) {
		skb->ip_summed = CHECKSUM_UNNECESSARY;
		++dev->stats.rx_packets_csum;
	}
#endif
}
L
Linus Torvalds 已提交
1312

1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328
static inline int emac_rx_sg_append(struct ocp_enet_private *dev, int slot)
{
	if (likely(dev->rx_sg_skb != NULL)) {
		int len = dev->rx_desc[slot].data_len;
		int tot_len = dev->rx_sg_skb->len + len;

		if (unlikely(tot_len + 2 > dev->rx_skb_size)) {
			++dev->estats.rx_dropped_mtu;
			dev_kfree_skb(dev->rx_sg_skb);
			dev->rx_sg_skb = NULL;
		} else {
			cacheable_memcpy(dev->rx_sg_skb->tail,
					 dev->rx_skb[slot]->data, len);
			skb_put(dev->rx_sg_skb, len);
			emac_recycle_rx_skb(dev, slot, len);
			return 0;
L
Linus Torvalds 已提交
1329 1330
		}
	}
1331 1332 1333
	emac_recycle_rx_skb(dev, slot, 0);
	return -1;
}
L
Linus Torvalds 已提交
1334

1335 1336 1337 1338 1339 1340 1341
/* BHs disabled */
static int emac_poll_rx(void *param, int budget)
{
	struct ocp_enet_private *dev = param;
	int slot = dev->rx_slot, received = 0;

	DBG2("%d: poll_rx(%d)" NL, dev->def->index, budget);
L
Linus Torvalds 已提交
1342

1343 1344 1345 1346 1347 1348 1349 1350
      again:
	while (budget > 0) {
		int len;
		struct sk_buff *skb;
		u16 ctrl = dev->rx_desc[slot].ctrl;

		if (ctrl & MAL_RX_CTRL_EMPTY)
			break;
L
Linus Torvalds 已提交
1351

1352 1353 1354
		skb = dev->rx_skb[slot];
		barrier();
		len = dev->rx_desc[slot].data_len;
L
Linus Torvalds 已提交
1355

1356 1357
		if (unlikely(!MAL_IS_SINGLE_RX(ctrl)))
			goto sg;
L
Linus Torvalds 已提交
1358

1359 1360 1361 1362 1363 1364 1365 1366
		ctrl &= EMAC_BAD_RX_MASK;
		if (unlikely(ctrl && ctrl != EMAC_RX_TAH_BAD_CSUM)) {
			emac_parse_rx_error(dev, ctrl);
			++dev->estats.rx_dropped_error;
			emac_recycle_rx_skb(dev, slot, 0);
			len = 0;
			goto next;
		}
L
Linus Torvalds 已提交
1367

1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 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
		if (len && len < EMAC_RX_COPY_THRESH) {
			struct sk_buff *copy_skb =
			    alloc_skb(len + EMAC_RX_SKB_HEADROOM + 2, GFP_ATOMIC);
			if (unlikely(!copy_skb))
				goto oom;

			skb_reserve(copy_skb, EMAC_RX_SKB_HEADROOM + 2);
			cacheable_memcpy(copy_skb->data - 2, skb->data - 2,
					 len + 2);
			emac_recycle_rx_skb(dev, slot, len);
			skb = copy_skb;
		} else if (unlikely(emac_alloc_rx_skb(dev, slot, GFP_ATOMIC)))
			goto oom;

		skb_put(skb, len);
	      push_packet:
		skb->dev = dev->ndev;
		skb->protocol = eth_type_trans(skb, dev->ndev);
		emac_rx_csum(dev, skb, ctrl);

		if (unlikely(netif_receive_skb(skb) == NET_RX_DROP))
			++dev->estats.rx_dropped_stack;
	      next:
		++dev->stats.rx_packets;
	      skip:
		dev->stats.rx_bytes += len;
		slot = (slot + 1) % NUM_RX_BUFF;
		--budget;
		++received;
		continue;
	      sg:
		if (ctrl & MAL_RX_CTRL_FIRST) {
			BUG_ON(dev->rx_sg_skb);
			if (unlikely(emac_alloc_rx_skb(dev, slot, GFP_ATOMIC))) {
				DBG("%d: rx OOM %d" NL, dev->def->index, slot);
				++dev->estats.rx_dropped_oom;
				emac_recycle_rx_skb(dev, slot, 0);
			} else {
				dev->rx_sg_skb = skb;
				skb_put(skb, len);
			}
		} else if (!emac_rx_sg_append(dev, slot) &&
			   (ctrl & MAL_RX_CTRL_LAST)) {

			skb = dev->rx_sg_skb;
			dev->rx_sg_skb = NULL;

			ctrl &= EMAC_BAD_RX_MASK;
			if (unlikely(ctrl && ctrl != EMAC_RX_TAH_BAD_CSUM)) {
				emac_parse_rx_error(dev, ctrl);
				++dev->estats.rx_dropped_error;
				dev_kfree_skb(skb);
				len = 0;
			} else
				goto push_packet;
		}
		goto skip;
	      oom:
		DBG("%d: rx OOM %d" NL, dev->def->index, slot);
		/* Drop the packet and recycle skb */
		++dev->estats.rx_dropped_oom;
		emac_recycle_rx_skb(dev, slot, 0);
		goto next;
	}
L
Linus Torvalds 已提交
1432

1433 1434 1435 1436
	if (received) {
		DBG2("%d: rx %d BDs" NL, dev->def->index, received);
		dev->rx_slot = slot;
	}
L
Linus Torvalds 已提交
1437

1438 1439
	if (unlikely(budget && dev->commac.rx_stopped)) {
		struct ocp_func_emac_data *emacdata = dev->def->additions;
L
Linus Torvalds 已提交
1440

1441 1442 1443 1444 1445 1446
		barrier();
		if (!(dev->rx_desc[slot].ctrl & MAL_RX_CTRL_EMPTY)) {
			DBG2("%d: rx restart" NL, dev->def->index);
			received = 0;
			goto again;
		}
L
Linus Torvalds 已提交
1447

1448 1449 1450 1451 1452 1453 1454
		if (dev->rx_sg_skb) {
			DBG2("%d: dropping partial rx packet" NL,
			     dev->def->index);
			++dev->estats.rx_dropped_error;
			dev_kfree_skb(dev->rx_sg_skb);
			dev->rx_sg_skb = NULL;
		}
L
Linus Torvalds 已提交
1455

1456 1457 1458 1459 1460 1461
		dev->commac.rx_stopped = 0;
		mal_enable_rx_channel(dev->mal, emacdata->mal_rx_chan);
		emac_rx_enable(dev);
		dev->rx_slot = 0;
	}
	return received;
L
Linus Torvalds 已提交
1462 1463
}

1464 1465
/* BHs disabled */
static int emac_peek_rx(void *param)
L
Linus Torvalds 已提交
1466
{
1467 1468 1469
	struct ocp_enet_private *dev = param;
	return !(dev->rx_desc[dev->rx_slot].ctrl & MAL_RX_CTRL_EMPTY);
}
L
Linus Torvalds 已提交
1470

1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
/* BHs disabled */
static int emac_peek_rx_sg(void *param)
{
	struct ocp_enet_private *dev = param;
	int slot = dev->rx_slot;
	while (1) {
		u16 ctrl = dev->rx_desc[slot].ctrl;
		if (ctrl & MAL_RX_CTRL_EMPTY)
			return 0;
		else if (ctrl & MAL_RX_CTRL_LAST)
			return 1;
L
Linus Torvalds 已提交
1482

1483
		slot = (slot + 1) % NUM_RX_BUFF;
L
Linus Torvalds 已提交
1484

1485 1486 1487 1488
		/* I'm just being paranoid here :) */
		if (unlikely(slot == dev->rx_slot))
			return 0;
	}
L
Linus Torvalds 已提交
1489 1490
}

1491 1492
/* Hard IRQ */
static void emac_rxde(void *param)
L
Linus Torvalds 已提交
1493
{
1494 1495 1496 1497
	struct ocp_enet_private *dev = param;
	++dev->estats.rx_stopped;
	emac_rx_disable_async(dev);
}
L
Linus Torvalds 已提交
1498

1499 1500 1501 1502 1503 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 1532 1533 1534 1535 1536 1537 1538 1539 1540
/* Hard IRQ */
static irqreturn_t emac_irq(int irq, void *dev_instance, struct pt_regs *regs)
{
	struct ocp_enet_private *dev = dev_instance;
	struct emac_regs *p = dev->emacp;
	struct ibm_emac_error_stats *st = &dev->estats;

	u32 isr = in_be32(&p->isr);
	out_be32(&p->isr, isr);

	DBG("%d: isr = %08x" NL, dev->def->index, isr);

	if (isr & EMAC_ISR_TXPE)
		++st->tx_parity;
	if (isr & EMAC_ISR_RXPE)
		++st->rx_parity;
	if (isr & EMAC_ISR_TXUE)
		++st->tx_underrun;
	if (isr & EMAC_ISR_RXOE)
		++st->rx_fifo_overrun;
	if (isr & EMAC_ISR_OVR)
		++st->rx_overrun;
	if (isr & EMAC_ISR_BP)
		++st->rx_bad_packet;
	if (isr & EMAC_ISR_RP)
		++st->rx_runt_packet;
	if (isr & EMAC_ISR_SE)
		++st->rx_short_event;
	if (isr & EMAC_ISR_ALE)
		++st->rx_alignment_error;
	if (isr & EMAC_ISR_BFCS)
		++st->rx_bad_fcs;
	if (isr & EMAC_ISR_PTLE)
		++st->rx_packet_too_long;
	if (isr & EMAC_ISR_ORE)
		++st->rx_out_of_range;
	if (isr & EMAC_ISR_IRE)
		++st->rx_in_range;
	if (isr & EMAC_ISR_SQE)
		++st->tx_sqe;
	if (isr & EMAC_ISR_TE)
		++st->tx_errors;
L
Linus Torvalds 已提交
1541

1542 1543
	return IRQ_HANDLED;
}
L
Linus Torvalds 已提交
1544

1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594
static struct net_device_stats *emac_stats(struct net_device *ndev)
{
	struct ocp_enet_private *dev = ndev->priv;
	struct ibm_emac_stats *st = &dev->stats;
	struct ibm_emac_error_stats *est = &dev->estats;
	struct net_device_stats *nst = &dev->nstats;

	DBG2("%d: stats" NL, dev->def->index);

	/* Compute "legacy" statistics */
	local_irq_disable();
	nst->rx_packets = (unsigned long)st->rx_packets;
	nst->rx_bytes = (unsigned long)st->rx_bytes;
	nst->tx_packets = (unsigned long)st->tx_packets;
	nst->tx_bytes = (unsigned long)st->tx_bytes;
	nst->rx_dropped = (unsigned long)(est->rx_dropped_oom +
					  est->rx_dropped_error +
					  est->rx_dropped_resize +
					  est->rx_dropped_mtu);
	nst->tx_dropped = (unsigned long)est->tx_dropped;

	nst->rx_errors = (unsigned long)est->rx_bd_errors;
	nst->rx_fifo_errors = (unsigned long)(est->rx_bd_overrun +
					      est->rx_fifo_overrun +
					      est->rx_overrun);
	nst->rx_frame_errors = (unsigned long)(est->rx_bd_alignment_error +
					       est->rx_alignment_error);
	nst->rx_crc_errors = (unsigned long)(est->rx_bd_bad_fcs +
					     est->rx_bad_fcs);
	nst->rx_length_errors = (unsigned long)(est->rx_bd_runt_packet +
						est->rx_bd_short_event +
						est->rx_bd_packet_too_long +
						est->rx_bd_out_of_range +
						est->rx_bd_in_range +
						est->rx_runt_packet +
						est->rx_short_event +
						est->rx_packet_too_long +
						est->rx_out_of_range +
						est->rx_in_range);

	nst->tx_errors = (unsigned long)(est->tx_bd_errors + est->tx_errors);
	nst->tx_fifo_errors = (unsigned long)(est->tx_bd_underrun +
					      est->tx_underrun);
	nst->tx_carrier_errors = (unsigned long)est->tx_bd_carrier_loss;
	nst->collisions = (unsigned long)(est->tx_bd_excessive_deferral +
					  est->tx_bd_excessive_collisions +
					  est->tx_bd_late_collision +
					  est->tx_bd_multple_collisions);
	local_irq_enable();
	return nst;
L
Linus Torvalds 已提交
1595 1596
}

1597
static void emac_remove(struct ocp_device *ocpdev)
L
Linus Torvalds 已提交
1598
{
1599
	struct ocp_enet_private *dev = ocp_get_drvdata(ocpdev);
L
Linus Torvalds 已提交
1600

1601
	DBG("%d: remove" NL, dev->def->index);
L
Linus Torvalds 已提交
1602

1603 1604
	ocp_set_drvdata(ocpdev, 0);
	unregister_netdev(dev->ndev);
L
Linus Torvalds 已提交
1605

1606 1607 1608
	tah_fini(dev->tah_dev);
	rgmii_fini(dev->rgmii_dev, dev->rgmii_input);
	zmii_fini(dev->zmii_dev, dev->zmii_input);
L
Linus Torvalds 已提交
1609

1610 1611 1612 1613 1614
	emac_dbg_register(dev->def->index, 0);

	mal_unregister_commac(dev->mal, &dev->commac);
	iounmap((void *)dev->emacp);
	kfree(dev->ndev);
L
Linus Torvalds 已提交
1615 1616
}

1617 1618 1619 1620 1621 1622
static struct mal_commac_ops emac_commac_ops = {
	.poll_tx = &emac_poll_tx,
	.poll_rx = &emac_poll_rx,
	.peek_rx = &emac_peek_rx,
	.rxde = &emac_rxde,
};
L
Linus Torvalds 已提交
1623

1624 1625 1626 1627 1628 1629
static struct mal_commac_ops emac_commac_sg_ops = {
	.poll_tx = &emac_poll_tx,
	.poll_rx = &emac_poll_rx,
	.peek_rx = &emac_peek_rx_sg,
	.rxde = &emac_rxde,
};
L
Linus Torvalds 已提交
1630

1631 1632 1633
/* Ethtool support */
static int emac_ethtool_get_settings(struct net_device *ndev,
				     struct ethtool_cmd *cmd)
L
Linus Torvalds 已提交
1634
{
1635
	struct ocp_enet_private *dev = ndev->priv;
L
Linus Torvalds 已提交
1636

1637
	cmd->supported = dev->phy.features;
L
Linus Torvalds 已提交
1638
	cmd->port = PORT_MII;
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649
	cmd->phy_address = dev->phy.address;
	cmd->transceiver =
	    dev->phy.address >= 0 ? XCVR_EXTERNAL : XCVR_INTERNAL;

	local_bh_disable();
	cmd->advertising = dev->phy.advertising;
	cmd->autoneg = dev->phy.autoneg;
	cmd->speed = dev->phy.speed;
	cmd->duplex = dev->phy.duplex;
	local_bh_enable();

L
Linus Torvalds 已提交
1650 1651 1652
	return 0;
}

1653 1654
static int emac_ethtool_set_settings(struct net_device *ndev,
				     struct ethtool_cmd *cmd)
L
Linus Torvalds 已提交
1655
{
1656 1657
	struct ocp_enet_private *dev = ndev->priv;
	u32 f = dev->phy.features;
L
Linus Torvalds 已提交
1658

1659 1660
	DBG("%d: set_settings(%d, %d, %d, 0x%08x)" NL, dev->def->index,
	    cmd->autoneg, cmd->speed, cmd->duplex, cmd->advertising);
L
Linus Torvalds 已提交
1661

1662 1663 1664
	/* Basic sanity checks */
	if (dev->phy.address < 0)
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1665 1666 1667 1668 1669 1670
	if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
		return -EINVAL;
	if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
		return -EINVAL;
	if (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL)
		return -EINVAL;
1671 1672

	if (cmd->autoneg == AUTONEG_DISABLE) {
L
Linus Torvalds 已提交
1673 1674
		switch (cmd->speed) {
		case SPEED_10:
1675 1676
			if (cmd->duplex == DUPLEX_HALF
			    && !(f & SUPPORTED_10baseT_Half))
L
Linus Torvalds 已提交
1677
				return -EINVAL;
1678 1679
			if (cmd->duplex == DUPLEX_FULL
			    && !(f & SUPPORTED_10baseT_Full))
L
Linus Torvalds 已提交
1680 1681 1682
				return -EINVAL;
			break;
		case SPEED_100:
1683 1684
			if (cmd->duplex == DUPLEX_HALF
			    && !(f & SUPPORTED_100baseT_Half))
L
Linus Torvalds 已提交
1685
				return -EINVAL;
1686 1687
			if (cmd->duplex == DUPLEX_FULL
			    && !(f & SUPPORTED_100baseT_Full))
L
Linus Torvalds 已提交
1688 1689 1690
				return -EINVAL;
			break;
		case SPEED_1000:
1691 1692
			if (cmd->duplex == DUPLEX_HALF
			    && !(f & SUPPORTED_1000baseT_Half))
L
Linus Torvalds 已提交
1693
				return -EINVAL;
1694 1695
			if (cmd->duplex == DUPLEX_FULL
			    && !(f & SUPPORTED_1000baseT_Full))
L
Linus Torvalds 已提交
1696 1697 1698 1699
				return -EINVAL;
			break;
		default:
			return -EINVAL;
1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719
		}

		local_bh_disable();
		dev->phy.def->ops->setup_forced(&dev->phy, cmd->speed,
						cmd->duplex);

	} else {
		if (!(f & SUPPORTED_Autoneg))
			return -EINVAL;

		local_bh_disable();
		dev->phy.def->ops->setup_aneg(&dev->phy,
					      (cmd->advertising & f) |
					      (dev->phy.advertising &
					       (ADVERTISED_Pause |
						ADVERTISED_Asym_Pause)));
	}
	emac_force_link_update(dev);
	local_bh_enable();

L
Linus Torvalds 已提交
1720 1721 1722
	return 0;
}

1723 1724
static void emac_ethtool_get_ringparam(struct net_device *ndev,
				       struct ethtool_ringparam *rp)
L
Linus Torvalds 已提交
1725
{
1726 1727
	rp->rx_max_pending = rp->rx_pending = NUM_RX_BUFF;
	rp->tx_max_pending = rp->tx_pending = NUM_TX_BUFF;
L
Linus Torvalds 已提交
1728 1729
}

1730 1731
static void emac_ethtool_get_pauseparam(struct net_device *ndev,
					struct ethtool_pauseparam *pp)
L
Linus Torvalds 已提交
1732
{
1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747
	struct ocp_enet_private *dev = ndev->priv;

	local_bh_disable();
	if ((dev->phy.features & SUPPORTED_Autoneg) &&
	    (dev->phy.advertising & (ADVERTISED_Pause | ADVERTISED_Asym_Pause)))
		pp->autoneg = 1;

	if (dev->phy.duplex == DUPLEX_FULL) {
		if (dev->phy.pause)
			pp->rx_pause = pp->tx_pause = 1;
		else if (dev->phy.asym_pause)
			pp->tx_pause = 1;
	}
	local_bh_enable();
}
L
Linus Torvalds 已提交
1748

1749 1750 1751 1752
static u32 emac_ethtool_get_rx_csum(struct net_device *ndev)
{
	struct ocp_enet_private *dev = ndev->priv;
	return dev->tah_dev != 0;
L
Linus Torvalds 已提交
1753 1754
}

1755
static int emac_get_regs_len(struct ocp_enet_private *dev)
L
Linus Torvalds 已提交
1756
{
1757
	return sizeof(struct emac_ethtool_regs_subhdr) + EMAC_ETHTOOL_REGS_SIZE;
L
Linus Torvalds 已提交
1758 1759
}

1760 1761 1762 1763 1764 1765 1766 1767 1768
static int emac_ethtool_get_regs_len(struct net_device *ndev)
{
	struct ocp_enet_private *dev = ndev->priv;
	return sizeof(struct emac_ethtool_regs_hdr) +
	    emac_get_regs_len(dev) + mal_get_regs_len(dev->mal) +
	    zmii_get_regs_len(dev->zmii_dev) +
	    rgmii_get_regs_len(dev->rgmii_dev) +
	    tah_get_regs_len(dev->tah_dev);
}
L
Linus Torvalds 已提交
1769

1770
static void *emac_dump_regs(struct ocp_enet_private *dev, void *buf)
L
Linus Torvalds 已提交
1771
{
1772
	struct emac_ethtool_regs_subhdr *hdr = buf;
L
Linus Torvalds 已提交
1773

1774 1775 1776 1777 1778
	hdr->version = EMAC_ETHTOOL_REGS_VER;
	hdr->index = dev->def->index;
	memcpy_fromio(hdr + 1, dev->emacp, EMAC_ETHTOOL_REGS_SIZE);
	return ((void *)(hdr + 1) + EMAC_ETHTOOL_REGS_SIZE);
}
L
Linus Torvalds 已提交
1779

1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798
static void emac_ethtool_get_regs(struct net_device *ndev,
				  struct ethtool_regs *regs, void *buf)
{
	struct ocp_enet_private *dev = ndev->priv;
	struct emac_ethtool_regs_hdr *hdr = buf;

	hdr->components = 0;
	buf = hdr + 1;

	local_irq_disable();
	buf = mal_dump_regs(dev->mal, buf);
	buf = emac_dump_regs(dev, buf);
	if (dev->zmii_dev) {
		hdr->components |= EMAC_ETHTOOL_REGS_ZMII;
		buf = zmii_dump_regs(dev->zmii_dev, buf);
	}
	if (dev->rgmii_dev) {
		hdr->components |= EMAC_ETHTOOL_REGS_RGMII;
		buf = rgmii_dump_regs(dev->rgmii_dev, buf);
L
Linus Torvalds 已提交
1799
	}
1800 1801 1802 1803 1804
	if (dev->tah_dev) {
		hdr->components |= EMAC_ETHTOOL_REGS_TAH;
		buf = tah_dump_regs(dev->tah_dev, buf);
	}
	local_irq_enable();
L
Linus Torvalds 已提交
1805 1806
}

1807
static int emac_ethtool_nway_reset(struct net_device *ndev)
L
Linus Torvalds 已提交
1808
{
1809 1810
	struct ocp_enet_private *dev = ndev->priv;
	int res = 0;
L
Linus Torvalds 已提交
1811

1812
	DBG("%d: nway_reset" NL, dev->def->index);
L
Linus Torvalds 已提交
1813

1814 1815
	if (dev->phy.address < 0)
		return -EOPNOTSUPP;
L
Linus Torvalds 已提交
1816

1817 1818 1819 1820 1821
	local_bh_disable();
	if (!dev->phy.autoneg) {
		res = -EINVAL;
		goto out;
	}
L
Linus Torvalds 已提交
1822

1823 1824
	dev->phy.def->ops->setup_aneg(&dev->phy, dev->phy.advertising);
	emac_force_link_update(dev);
L
Linus Torvalds 已提交
1825

1826 1827 1828 1829
      out:
	local_bh_enable();
	return res;
}
L
Linus Torvalds 已提交
1830

1831 1832 1833
static int emac_ethtool_get_stats_count(struct net_device *ndev)
{
	return EMAC_ETHTOOL_STATS_COUNT;
L
Linus Torvalds 已提交
1834 1835
}

1836 1837
static void emac_ethtool_get_strings(struct net_device *ndev, u32 stringset,
				     u8 * buf)
L
Linus Torvalds 已提交
1838
{
1839 1840 1841
	if (stringset == ETH_SS_STATS)
		memcpy(buf, &emac_stats_keys, sizeof(emac_stats_keys));
}
L
Linus Torvalds 已提交
1842

1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853
static void emac_ethtool_get_ethtool_stats(struct net_device *ndev,
					   struct ethtool_stats *estats,
					   u64 * tmp_stats)
{
	struct ocp_enet_private *dev = ndev->priv;
	local_irq_disable();
	memcpy(tmp_stats, &dev->stats, sizeof(dev->stats));
	tmp_stats += sizeof(dev->stats) / sizeof(u64);
	memcpy(tmp_stats, &dev->estats, sizeof(dev->estats));
	local_irq_enable();
}
L
Linus Torvalds 已提交
1854

1855 1856 1857 1858
static void emac_ethtool_get_drvinfo(struct net_device *ndev,
				     struct ethtool_drvinfo *info)
{
	struct ocp_enet_private *dev = ndev->priv;
L
Linus Torvalds 已提交
1859

1860 1861 1862 1863 1864 1865 1866
	strcpy(info->driver, "ibm_emac");
	strcpy(info->version, DRV_VERSION);
	info->fw_version[0] = '\0';
	sprintf(info->bus_info, "PPC 4xx EMAC %d", dev->def->index);
	info->n_stats = emac_ethtool_get_stats_count(ndev);
	info->regdump_len = emac_ethtool_get_regs_len(ndev);
}
L
Linus Torvalds 已提交
1867

1868 1869 1870 1871
static struct ethtool_ops emac_ethtool_ops = {
	.get_settings = emac_ethtool_get_settings,
	.set_settings = emac_ethtool_set_settings,
	.get_drvinfo = emac_ethtool_get_drvinfo,
L
Linus Torvalds 已提交
1872

1873 1874
	.get_regs_len = emac_ethtool_get_regs_len,
	.get_regs = emac_ethtool_get_regs,
L
Linus Torvalds 已提交
1875

1876
	.nway_reset = emac_ethtool_nway_reset,
L
Linus Torvalds 已提交
1877

1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
	.get_ringparam = emac_ethtool_get_ringparam,
	.get_pauseparam = emac_ethtool_get_pauseparam,

	.get_rx_csum = emac_ethtool_get_rx_csum,

	.get_strings = emac_ethtool_get_strings,
	.get_stats_count = emac_ethtool_get_stats_count,
	.get_ethtool_stats = emac_ethtool_get_ethtool_stats,

	.get_link = ethtool_op_get_link,
	.get_tx_csum = ethtool_op_get_tx_csum,
	.get_sg = ethtool_op_get_sg,
L
Linus Torvalds 已提交
1890 1891
};

1892
static int emac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
L
Linus Torvalds 已提交
1893
{
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920
	struct ocp_enet_private *dev = ndev->priv;
	uint16_t *data = (uint16_t *) & rq->ifr_ifru;

	DBG("%d: ioctl %08x" NL, dev->def->index, cmd);

	if (dev->phy.address < 0)
		return -EOPNOTSUPP;

	switch (cmd) {
	case SIOCGMIIPHY:
	case SIOCDEVPRIVATE:
		data[0] = dev->phy.address;
		/* Fall through */
	case SIOCGMIIREG:
	case SIOCDEVPRIVATE + 1:
		data[3] = emac_mdio_read(ndev, dev->phy.address, data[1]);
		return 0;

	case SIOCSMIIREG:
	case SIOCDEVPRIVATE + 2:
		if (!capable(CAP_NET_ADMIN))
			return -EPERM;
		emac_mdio_write(ndev, dev->phy.address, data[1], data[2]);
		return 0;
	default:
		return -EOPNOTSUPP;
	}
L
Linus Torvalds 已提交
1921 1922
}

1923
static int __init emac_probe(struct ocp_device *ocpdev)
L
Linus Torvalds 已提交
1924
{
1925
	struct ocp_func_emac_data *emacdata = ocpdev->def->additions;
L
Linus Torvalds 已提交
1926
	struct net_device *ndev;
1927 1928 1929 1930 1931
	struct ocp_device *maldev;
	struct ocp_enet_private *dev;
	int err, i;

	DBG("%d: probe" NL, ocpdev->def->index);
L
Linus Torvalds 已提交
1932 1933 1934 1935 1936 1937 1938 1939 1940

	if (!emacdata) {
		printk(KERN_ERR "emac%d: Missing additional data!\n",
		       ocpdev->def->index);
		return -ENODEV;
	}

	/* Allocate our net_device structure */
	ndev = alloc_etherdev(sizeof(struct ocp_enet_private));
1941 1942
	if (!ndev) {
		printk(KERN_ERR "emac%d: could not allocate ethernet device!\n",
L
Linus Torvalds 已提交
1943 1944 1945
		       ocpdev->def->index);
		return -ENOMEM;
	}
1946 1947 1948 1949 1950
	dev = ndev->priv;
	dev->ndev = ndev;
	dev->ldev = &ocpdev->dev;
	dev->def = ocpdev->def;
	SET_MODULE_OWNER(ndev);
L
Linus Torvalds 已提交
1951

1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966
	/* Find MAL device we are connected to */
	maldev =
	    ocp_find_device(OCP_VENDOR_IBM, OCP_FUNC_MAL, emacdata->mal_idx);
	if (!maldev) {
		printk(KERN_ERR "emac%d: unknown mal%d device!\n",
		       dev->def->index, emacdata->mal_idx);
		err = -ENODEV;
		goto out;
	}
	dev->mal = ocp_get_drvdata(maldev);
	if (!dev->mal) {
		printk(KERN_ERR "emac%d: mal%d hasn't been initialized yet!\n",
		       dev->def->index, emacdata->mal_idx);
		err = -ENODEV;
		goto out;
L
Linus Torvalds 已提交
1967 1968
	}

1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016
	/* Register with MAL */
	dev->commac.ops = &emac_commac_ops;
	dev->commac.dev = dev;
	dev->commac.tx_chan_mask = MAL_CHAN_MASK(emacdata->mal_tx_chan);
	dev->commac.rx_chan_mask = MAL_CHAN_MASK(emacdata->mal_rx_chan);
	err = mal_register_commac(dev->mal, &dev->commac);
	if (err) {
		printk(KERN_ERR "emac%d: failed to register with mal%d!\n",
		       dev->def->index, emacdata->mal_idx);
		goto out;
	}
	dev->rx_skb_size = emac_rx_skb_size(ndev->mtu);
	dev->rx_sync_size = emac_rx_sync_size(ndev->mtu);

	/* Get pointers to BD rings */
	dev->tx_desc =
	    dev->mal->bd_virt + mal_tx_bd_offset(dev->mal,
						 emacdata->mal_tx_chan);
	dev->rx_desc =
	    dev->mal->bd_virt + mal_rx_bd_offset(dev->mal,
						 emacdata->mal_rx_chan);

	DBG("%d: tx_desc %p" NL, ocpdev->def->index, dev->tx_desc);
	DBG("%d: rx_desc %p" NL, ocpdev->def->index, dev->rx_desc);

	/* Clean rings */
	memset(dev->tx_desc, 0, NUM_TX_BUFF * sizeof(struct mal_descriptor));
	memset(dev->rx_desc, 0, NUM_RX_BUFF * sizeof(struct mal_descriptor));

	/* If we depend on another EMAC for MDIO, check whether it was probed already */
	if (emacdata->mdio_idx >= 0 && emacdata->mdio_idx != ocpdev->def->index) {
		struct ocp_device *mdiodev =
		    ocp_find_device(OCP_VENDOR_IBM, OCP_FUNC_EMAC,
				    emacdata->mdio_idx);
		if (!mdiodev) {
			printk(KERN_ERR "emac%d: unknown emac%d device!\n",
			       dev->def->index, emacdata->mdio_idx);
			err = -ENODEV;
			goto out2;
		}
		dev->mdio_dev = ocp_get_drvdata(mdiodev);
		if (!dev->mdio_dev) {
			printk(KERN_ERR
			       "emac%d: emac%d hasn't been initialized yet!\n",
			       dev->def->index, emacdata->mdio_idx);
			err = -ENODEV;
			goto out2;
		}
L
Linus Torvalds 已提交
2017 2018
	}

2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039
	/* Attach to ZMII, if needed */
	if ((err = zmii_attach(dev)) != 0)
		goto out2;

	/* Attach to RGMII, if needed */
	if ((err = rgmii_attach(dev)) != 0)
		goto out3;

	/* Attach to TAH, if needed */
	if ((err = tah_attach(dev)) != 0)
		goto out4;

	/* Map EMAC regs */
	dev->emacp =
	    (struct emac_regs *)ioremap(dev->def->paddr,
					sizeof(struct emac_regs));
	if (!dev->emacp) {
		printk(KERN_ERR "emac%d: could not ioremap device registers!\n",
		       dev->def->index);
		err = -ENOMEM;
		goto out5;
L
Linus Torvalds 已提交
2040 2041
	}

2042 2043 2044
	/* Fill in MAC address */
	for (i = 0; i < 6; ++i)
		ndev->dev_addr[i] = emacdata->mac_addr[i];
L
Linus Torvalds 已提交
2045

2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079
	/* Set some link defaults before we can find out real parameters */
	dev->phy.speed = SPEED_100;
	dev->phy.duplex = DUPLEX_FULL;
	dev->phy.autoneg = AUTONEG_DISABLE;
	dev->phy.pause = dev->phy.asym_pause = 0;
	init_timer(&dev->link_timer);
	dev->link_timer.function = emac_link_timer;
	dev->link_timer.data = (unsigned long)dev;

	/* Find PHY if any */
	dev->phy.dev = ndev;
	dev->phy.mode = emacdata->phy_mode;
	if (emacdata->phy_map != 0xffffffff) {
		u32 phy_map = emacdata->phy_map | busy_phy_map;
		u32 adv;

		DBG("%d: PHY maps %08x %08x" NL, dev->def->index,
		    emacdata->phy_map, busy_phy_map);

		EMAC_RX_CLK_TX(dev->def->index);

		dev->phy.mdio_read = emac_mdio_read;
		dev->phy.mdio_write = emac_mdio_write;

		/* Configure EMAC with defaults so we can at least use MDIO
		 * This is needed mostly for 440GX
		 */
		if (emac_phy_gpcs(dev->phy.mode)) {
			/* XXX
			 * Make GPCS PHY address equal to EMAC index.
			 * We probably should take into account busy_phy_map
			 * and/or phy_map here.
			 */
			dev->phy.address = dev->def->index;
L
Linus Torvalds 已提交
2080
		}
2081 2082 2083 2084 2085 2086 2087
		
		emac_configure(dev);

		for (i = 0; i < 0x20; phy_map >>= 1, ++i)
			if (!(phy_map & 1)) {
				int r;
				busy_phy_map |= 1 << i;
L
Linus Torvalds 已提交
2088

2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
				/* Quick check if there is a PHY at the address */
				r = emac_mdio_read(dev->ndev, i, MII_BMCR);
				if (r == 0xffff || r < 0)
					continue;
				if (!mii_phy_probe(&dev->phy, i))
					break;
			}
		if (i == 0x20) {
			printk(KERN_WARNING "emac%d: can't find PHY!\n",
			       dev->def->index);
			goto out6;
L
Linus Torvalds 已提交
2100 2101
		}

2102 2103 2104
		/* Init PHY */
		if (dev->phy.def->ops->init)
			dev->phy.def->ops->init(&dev->phy);
2105
		
2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
		/* Disable any PHY features not supported by the platform */
		dev->phy.def->features &= ~emacdata->phy_feat_exc;

		/* Setup initial link parameters */
		if (dev->phy.features & SUPPORTED_Autoneg) {
			adv = dev->phy.features;
#if !defined(CONFIG_40x)
			adv |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
#endif
			/* Restart autonegotiation */
			dev->phy.def->ops->setup_aneg(&dev->phy, adv);
2117
		} else {
2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136
			u32 f = dev->phy.def->features;
			int speed = SPEED_10, fd = DUPLEX_HALF;

			/* Select highest supported speed/duplex */
			if (f & SUPPORTED_1000baseT_Full) {
				speed = SPEED_1000;
				fd = DUPLEX_FULL;
			} else if (f & SUPPORTED_1000baseT_Half)
				speed = SPEED_1000;
			else if (f & SUPPORTED_100baseT_Full) {
				speed = SPEED_100;
				fd = DUPLEX_FULL;
			} else if (f & SUPPORTED_100baseT_Half)
				speed = SPEED_100;
			else if (f & SUPPORTED_10baseT_Full)
				fd = DUPLEX_FULL;

			/* Force link parameters */
			dev->phy.def->ops->setup_forced(&dev->phy, speed, fd);
2137
		}
2138 2139
	} else {
		emac_reset(dev);
L
Linus Torvalds 已提交
2140

2141 2142 2143 2144 2145 2146 2147
		/* PHY-less configuration.
		 * XXX I probably should move these settings to emacdata
		 */
		dev->phy.address = -1;
		dev->phy.features = SUPPORTED_100baseT_Full | SUPPORTED_MII;
		dev->phy.pause = 1;
	}
L
Linus Torvalds 已提交
2148 2149 2150

	/* Fill in the driver function table */
	ndev->open = &emac_open;
2151 2152 2153 2154 2155 2156 2157
	if (dev->tah_dev) {
		ndev->hard_start_xmit = &emac_start_xmit_sg;
		ndev->features |= NETIF_F_IP_CSUM | NETIF_F_SG;
	} else
		ndev->hard_start_xmit = &emac_start_xmit;
	ndev->tx_timeout = &emac_full_tx_reset;
	ndev->watchdog_timeo = 5 * HZ;
L
Linus Torvalds 已提交
2158 2159 2160 2161
	ndev->stop = &emac_close;
	ndev->get_stats = &emac_stats;
	ndev->set_multicast_list = &emac_set_multicast_list;
	ndev->do_ioctl = &emac_ioctl;
2162 2163 2164 2165
	if (emac_phy_supports_gige(emacdata->phy_mode)) {
		ndev->change_mtu = &emac_change_mtu;
		dev->commac.ops = &emac_commac_sg_ops;
	}
L
Linus Torvalds 已提交
2166 2167
	SET_ETHTOOL_OPS(ndev, &emac_ethtool_ops);

2168 2169 2170 2171 2172 2173 2174 2175 2176
	netif_carrier_off(ndev);
	netif_stop_queue(ndev);

	err = register_netdev(ndev);
	if (err) {
		printk(KERN_ERR "emac%d: failed to register net device (%d)!\n",
		       dev->def->index, err);
		goto out6;
	}
L
Linus Torvalds 已提交
2177

2178
	ocp_set_drvdata(ocpdev, dev);
L
Linus Torvalds 已提交
2179

2180 2181
	printk("%s: emac%d, MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
	       ndev->name, dev->def->index,
L
Linus Torvalds 已提交
2182 2183 2184
	       ndev->dev_addr[0], ndev->dev_addr[1], ndev->dev_addr[2],
	       ndev->dev_addr[3], ndev->dev_addr[4], ndev->dev_addr[5]);

2185 2186 2187
	if (dev->phy.address >= 0)
		printk("%s: found %s PHY (0x%02x)\n", ndev->name,
		       dev->phy.def->name, dev->phy.address);
L
Linus Torvalds 已提交
2188

2189
	emac_dbg_register(dev->def->index, dev);
L
Linus Torvalds 已提交
2190 2191

	return 0;
2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204
      out6:
	iounmap((void *)dev->emacp);
      out5:
	tah_fini(dev->tah_dev);
      out4:
	rgmii_fini(dev->rgmii_dev, dev->rgmii_input);
      out3:
	zmii_fini(dev->zmii_dev, dev->zmii_input);
      out2:
	mal_unregister_commac(dev->mal, &dev->commac);
      out:
	kfree(ndev);
	return err;
L
Linus Torvalds 已提交
2205 2206 2207
}

static struct ocp_device_id emac_ids[] = {
2208 2209
	{ .vendor = OCP_VENDOR_IBM, .function = OCP_FUNC_EMAC },
	{ .vendor = OCP_VENDOR_INVALID}
L
Linus Torvalds 已提交
2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220
};

static struct ocp_driver emac_driver = {
	.name = "emac",
	.id_table = emac_ids,
	.probe = emac_probe,
	.remove = emac_remove,
};

static int __init emac_init(void)
{
2221 2222 2223
	printk(KERN_INFO DRV_DESC ", version " DRV_VERSION "\n");

	DBG(": init" NL);
L
Linus Torvalds 已提交
2224

2225 2226 2227 2228 2229 2230 2231 2232 2233
	if (mal_init())
		return -ENODEV;

	EMAC_CLK_INTERNAL;
	if (ocp_register_driver(&emac_driver)) {
		EMAC_CLK_EXTERNAL;
		ocp_unregister_driver(&emac_driver);
		mal_exit();
		return -ENODEV;
L
Linus Torvalds 已提交
2234
	}
2235
	EMAC_CLK_EXTERNAL;
L
Linus Torvalds 已提交
2236

2237 2238
	emac_init_debug();
	return 0;
L
Linus Torvalds 已提交
2239 2240 2241 2242
}

static void __exit emac_exit(void)
{
2243
	DBG(": exit" NL);
L
Linus Torvalds 已提交
2244
	ocp_unregister_driver(&emac_driver);
2245 2246
	mal_exit();
	emac_fini_debug();
L
Linus Torvalds 已提交
2247 2248 2249 2250
}

module_init(emac_init);
module_exit(emac_exit);