pasemi_mac.c 34.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 * Copyright (C) 2006-2007 PA Semi, Inc
 *
 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/interrupt.h>
#include <linux/dmaengine.h>
#include <linux/delay.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <asm/dma-mapping.h>
#include <linux/in.h>
#include <linux/skbuff.h>

#include <linux/ip.h>
#include <linux/tcp.h>
#include <net/checksum.h>

36 37
#include <asm/irq.h>

38 39
#include "pasemi_mac.h"

40 41 42 43 44 45
/* We have our own align, since ppc64 in general has it at 0 because
 * of design flaws in some of the server bridge chips. However, for
 * PWRficient doing the unaligned copies is more expensive than doing
 * unaligned DMA, so make sure the data is aligned instead.
 */
#define LOCAL_SKB_ALIGN	2
46 47 48 49 50

/* TODO list
 *
 * - Multicast support
 * - Large MTU support
O
Olof Johansson 已提交
51 52
 * - SW LRO
 * - Multiqueue RX/TX
53 54 55 56
 */


/* Must be a power of two */
57 58
#define RX_RING_SIZE 4096
#define TX_RING_SIZE 4096
59

60 61 62 63 64 65 66 67 68 69
#define DEFAULT_MSG_ENABLE	  \
	(NETIF_MSG_DRV		| \
	 NETIF_MSG_PROBE	| \
	 NETIF_MSG_LINK		| \
	 NETIF_MSG_TIMER	| \
	 NETIF_MSG_IFDOWN	| \
	 NETIF_MSG_IFUP		| \
	 NETIF_MSG_RX_ERR	| \
	 NETIF_MSG_TX_ERR)

70 71 72 73
#define TX_RING(mac, num)	((mac)->tx->ring[(num) & (TX_RING_SIZE-1)])
#define TX_RING_INFO(mac, num)	((mac)->tx->ring_info[(num) & (TX_RING_SIZE-1)])
#define RX_RING(mac, num)	((mac)->rx->ring[(num) & (RX_RING_SIZE-1)])
#define RX_RING_INFO(mac, num)	((mac)->rx->ring_info[(num) & (RX_RING_SIZE-1)])
74 75
#define RX_BUFF(mac, num)	((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])

76 77 78 79
#define RING_USED(ring)		(((ring)->next_to_fill - (ring)->next_to_clean) \
				 & ((ring)->size - 1))
#define RING_AVAIL(ring)	((ring->size) - RING_USED(ring))

80 81
#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */

82 83 84 85 86 87 88
MODULE_LICENSE("GPL");
MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");

static int debug = -1;	/* -1 == use DEFAULT_MSG_ENABLE as value */
module_param(debug, int, 0);
MODULE_PARM_DESC(debug, "PA Semi MAC bitmapped debugging message enable value");
89 90 91

static struct pasdma_status *dma_status;

92 93 94
static void write_iob_reg(struct pasemi_mac *mac, unsigned int reg,
			  unsigned int val)
{
95
	out_le32(mac->iob_regs+reg, val);
96 97 98 99
}

static unsigned int read_mac_reg(struct pasemi_mac *mac, unsigned int reg)
{
100
	return in_le32(mac->regs+reg);
101 102 103 104 105
}

static void write_mac_reg(struct pasemi_mac *mac, unsigned int reg,
			  unsigned int val)
{
106
	out_le32(mac->regs+reg, val);
107 108 109 110
}

static unsigned int read_dma_reg(struct pasemi_mac *mac, unsigned int reg)
{
111
	return in_le32(mac->dma_regs+reg);
112 113 114 115 116
}

static void write_dma_reg(struct pasemi_mac *mac, unsigned int reg,
			  unsigned int val)
{
117
	out_le32(mac->dma_regs+reg, val);
118 119
}

120 121 122 123
static int pasemi_get_mac_addr(struct pasemi_mac *mac)
{
	struct pci_dev *pdev = mac->pdev;
	struct device_node *dn = pci_device_to_OF_node(pdev);
124
	int len;
125 126 127 128 129 130 131 132 133
	const u8 *maddr;
	u8 addr[6];

	if (!dn) {
		dev_dbg(&pdev->dev,
			  "No device node for mac, not configuring\n");
		return -ENOENT;
	}

134 135 136 137 138 139 140 141 142 143
	maddr = of_get_property(dn, "local-mac-address", &len);

	if (maddr && len == 6) {
		memcpy(mac->mac_addr, maddr, 6);
		return 0;
	}

	/* Some old versions of firmware mistakenly uses mac-address
	 * (and as a string) instead of a byte array in local-mac-address.
	 */
144 145

	if (maddr == NULL)
146
		maddr = of_get_property(dn, "mac-address", NULL);
147

148 149 150 151 152 153
	if (maddr == NULL) {
		dev_warn(&pdev->dev,
			 "no mac address in device tree, not configuring\n");
		return -ENOENT;
	}

154

155 156 157 158 159 160 161
	if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
		   &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
		dev_warn(&pdev->dev,
			 "can't parse mac address, not configuring\n");
		return -EINVAL;
	}

162 163
	memcpy(mac->mac_addr, addr, 6);

164 165 166
	return 0;
}

O
Olof Johansson 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
				    struct sk_buff *skb,
				    dma_addr_t *dmas)
{
	int f;
	int nfrags = skb_shinfo(skb)->nr_frags;

	pci_unmap_single(mac->dma_pdev, dmas[0], skb_headlen(skb),
			 PCI_DMA_TODEVICE);

	for (f = 0; f < nfrags; f++) {
		skb_frag_t *frag = &skb_shinfo(skb)->frags[f];

		pci_unmap_page(mac->dma_pdev, dmas[f+1], frag->size,
			       PCI_DMA_TODEVICE);
	}
	dev_kfree_skb_irq(skb);

	/* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
	 * aligned up to a power of 2
	 */
	return (nfrags + 3) & ~1;
}

191 192 193 194 195 196 197 198 199 200 201 202 203
static int pasemi_mac_setup_rx_resources(struct net_device *dev)
{
	struct pasemi_mac_rxring *ring;
	struct pasemi_mac *mac = netdev_priv(dev);
	int chan_id = mac->dma_rxch;

	ring = kzalloc(sizeof(*ring), GFP_KERNEL);

	if (!ring)
		goto out_ring;

	spin_lock_init(&ring->lock);

204
	ring->size = RX_RING_SIZE;
205
	ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
206 207
				  RX_RING_SIZE, GFP_KERNEL);

208 209
	if (!ring->ring_info)
		goto out_ring_info;
210 211

	/* Allocate descriptors */
212 213
	ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
					RX_RING_SIZE * sizeof(u64),
214 215
					&ring->dma, GFP_KERNEL);

216 217
	if (!ring->ring)
		goto out_ring_desc;
218

219
	memset(ring->ring, 0, RX_RING_SIZE * sizeof(u64));
220 221 222 223 224 225 226 227 228

	ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
					   RX_RING_SIZE * sizeof(u64),
					   &ring->buf_dma, GFP_KERNEL);
	if (!ring->buffers)
		goto out_buffers;

	memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));

229
	write_dma_reg(mac, PAS_DMA_RXCHAN_BASEL(chan_id), PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
230

231 232
	write_dma_reg(mac, PAS_DMA_RXCHAN_BASEU(chan_id),
			   PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
233
			   PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
234

235
	write_dma_reg(mac, PAS_DMA_RXCHAN_CFG(chan_id),
236
			   PAS_DMA_RXCHAN_CFG_HBU(2));
237

238 239
	write_dma_reg(mac, PAS_DMA_RXINT_BASEL(mac->dma_if),
			   PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
240

241 242 243
	write_dma_reg(mac, PAS_DMA_RXINT_BASEU(mac->dma_if),
			   PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
			   PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
244

245
	write_dma_reg(mac, PAS_DMA_RXINT_CFG(mac->dma_if),
246 247 248
			   PAS_DMA_RXINT_CFG_DHL(3) |
			   PAS_DMA_RXINT_CFG_L2 |
			   PAS_DMA_RXINT_CFG_LW);
249

250 251 252 253 254 255 256 257 258 259 260
	ring->next_to_fill = 0;
	ring->next_to_clean = 0;

	snprintf(ring->irq_name, sizeof(ring->irq_name),
		 "%s rx", dev->name);
	mac->rx = ring;

	return 0;

out_buffers:
	dma_free_coherent(&mac->dma_pdev->dev,
261 262 263 264 265
			  RX_RING_SIZE * sizeof(u64),
			  mac->rx->ring, mac->rx->dma);
out_ring_desc:
	kfree(ring->ring_info);
out_ring_info:
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	kfree(ring);
out_ring:
	return -ENOMEM;
}


static int pasemi_mac_setup_tx_resources(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
	u32 val;
	int chan_id = mac->dma_txch;
	struct pasemi_mac_txring *ring;

	ring = kzalloc(sizeof(*ring), GFP_KERNEL);
	if (!ring)
		goto out_ring;

	spin_lock_init(&ring->lock);

285
	ring->size = TX_RING_SIZE;
286
	ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
287
				  TX_RING_SIZE, GFP_KERNEL);
288 289
	if (!ring->ring_info)
		goto out_ring_info;
290 291

	/* Allocate descriptors */
292 293
	ring->ring = dma_alloc_coherent(&mac->dma_pdev->dev,
					TX_RING_SIZE * sizeof(u64),
294
					&ring->dma, GFP_KERNEL);
295 296
	if (!ring->ring)
		goto out_ring_desc;
297

298
	memset(ring->ring, 0, TX_RING_SIZE * sizeof(u64));
299

300 301
	write_dma_reg(mac, PAS_DMA_TXCHAN_BASEL(chan_id),
			   PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
302
	val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
303
	val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
304

305
	write_dma_reg(mac, PAS_DMA_TXCHAN_BASEU(chan_id), val);
306

307 308 309 310 311
	write_dma_reg(mac, PAS_DMA_TXCHAN_CFG(chan_id),
			   PAS_DMA_TXCHAN_CFG_TY_IFACE |
			   PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
			   PAS_DMA_TXCHAN_CFG_UP |
			   PAS_DMA_TXCHAN_CFG_WT(2));
312

313
	ring->next_to_fill = 0;
314 315 316 317 318 319 320 321
	ring->next_to_clean = 0;

	snprintf(ring->irq_name, sizeof(ring->irq_name),
		 "%s tx", dev->name);
	mac->tx = ring;

	return 0;

322 323 324
out_ring_desc:
	kfree(ring->ring_info);
out_ring_info:
325 326 327 328 329 330 331 332
	kfree(ring);
out_ring:
	return -ENOMEM;
}

static void pasemi_mac_free_tx_resources(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
O
Olof Johansson 已提交
333
	unsigned int i, j;
334
	struct pasemi_mac_buffer *info;
O
Olof Johansson 已提交
335 336
	dma_addr_t dmas[MAX_SKB_FRAGS+1];
	int freed;
337
	int start, limit;
338

339 340 341 342 343 344 345 346
	start = mac->tx->next_to_clean;
	limit = mac->tx->next_to_fill;

	/* Compensate for when fill has wrapped and clean has not */
	if (start > limit)
		limit += TX_RING_SIZE;

	for (i = start; i < limit; i += freed) {
347 348
		info = &TX_RING_INFO(mac, i+1);
		if (info->dma && info->skb) {
O
Olof Johansson 已提交
349 350 351 352 353
			for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
				dmas[j] = TX_RING_INFO(mac, i+1+j).dma;
			freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
		} else
			freed = 2;
354 355
	}

O
Olof Johansson 已提交
356 357 358
	for (i = 0; i < TX_RING_SIZE; i++)
		TX_RING(mac, i) = 0;

359
	dma_free_coherent(&mac->dma_pdev->dev,
360 361
			  TX_RING_SIZE * sizeof(u64),
			  mac->tx->ring, mac->tx->dma);
362

363
	kfree(mac->tx->ring_info);
364 365 366 367 368 369 370 371 372 373 374
	kfree(mac->tx);
	mac->tx = NULL;
}

static void pasemi_mac_free_rx_resources(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
	unsigned int i;
	struct pasemi_mac_buffer *info;

	for (i = 0; i < RX_RING_SIZE; i++) {
375 376 377 378 379 380 381
		info = &RX_RING_INFO(mac, i);
		if (info->skb && info->dma) {
			pci_unmap_single(mac->dma_pdev,
					 info->dma,
					 info->skb->len,
					 PCI_DMA_FROMDEVICE);
			dev_kfree_skb_any(info->skb);
382
		}
383 384
		info->dma = 0;
		info->skb = NULL;
385 386
	}

387 388 389
	for (i = 0; i < RX_RING_SIZE; i++)
		RX_RING(mac, i) = 0;

390
	dma_free_coherent(&mac->dma_pdev->dev,
391 392
			  RX_RING_SIZE * sizeof(u64),
			  mac->rx->ring, mac->rx->dma);
393 394 395 396

	dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
			  mac->rx->buffers, mac->rx->buf_dma);

397
	kfree(mac->rx->ring_info);
398 399 400 401
	kfree(mac->rx);
	mac->rx = NULL;
}

402
static void pasemi_mac_replenish_rx_ring(struct net_device *dev, int limit)
403 404 405
{
	struct pasemi_mac *mac = netdev_priv(dev);
	int start = mac->rx->next_to_fill;
406
	unsigned int fill, count;
407

408
	if (limit <= 0)
409 410
		return;

411
	fill = start;
412
	for (count = 0; count < limit; count++) {
413 414
		struct pasemi_mac_buffer *info = &RX_RING_INFO(mac, fill);
		u64 *buff = &RX_BUFF(mac, fill);
415 416 417
		struct sk_buff *skb;
		dma_addr_t dma;

418 419 420
		/* Entry in use? */
		WARN_ON(*buff);

421 422 423
		/* skb might still be in there for recycle on short receives */
		if (info->skb)
			skb = info->skb;
424
		else {
425
			skb = dev_alloc_skb(BUF_SIZE);
426 427
			skb_reserve(skb, LOCAL_SKB_ALIGN);
		}
428

429
		if (unlikely(!skb))
430 431
			break;

432 433
		dma = pci_map_single(mac->dma_pdev, skb->data,
				     BUF_SIZE - LOCAL_SKB_ALIGN,
434 435
				     PCI_DMA_FROMDEVICE);

436
		if (unlikely(dma_mapping_error(dma))) {
437 438 439 440 441 442 443
			dev_kfree_skb_irq(info->skb);
			break;
		}

		info->skb = skb;
		info->dma = dma;
		*buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
444
		fill++;
445 446 447 448
	}

	wmb();

449 450
	write_dma_reg(mac, PAS_DMA_RXCHAN_INCR(mac->dma_rxch), count);
	write_dma_reg(mac, PAS_DMA_RXINT_INCR(mac->dma_if), count);
451

452
	mac->rx->next_to_fill += count;
453 454
}

455 456
static void pasemi_mac_restart_rx_intr(struct pasemi_mac *mac)
{
O
Olof Johansson 已提交
457
	unsigned int reg, pcnt;
458 459 460 461
	/* Re-enable packet count interrupts: finally
	 * ack the packet count interrupt we got in rx_intr.
	 */

O
Olof Johansson 已提交
462
	pcnt = *mac->rx_status & PAS_STATUS_PCNT_M;
463

O
Olof Johansson 已提交
464
	reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
465

466
	write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
467 468 469 470
}

static void pasemi_mac_restart_tx_intr(struct pasemi_mac *mac)
{
O
Olof Johansson 已提交
471
	unsigned int reg, pcnt;
472 473

	/* Re-enable packet count interrupts */
O
Olof Johansson 已提交
474
	pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;
475

O
Olof Johansson 已提交
476
	reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
477

478
	write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
479 480 481
}


O
Olof Johansson 已提交
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
static inline void pasemi_mac_rx_error(struct pasemi_mac *mac, u64 macrx)
{
	unsigned int rcmdsta, ccmdsta;

	if (!netif_msg_rx_err(mac))
		return;

	rcmdsta = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
	ccmdsta = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));

	printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
		macrx, *mac->rx_status);

	printk(KERN_ERR "pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
		rcmdsta, ccmdsta);
}

static inline void pasemi_mac_tx_error(struct pasemi_mac *mac, u64 mactx)
{
	unsigned int cmdsta;

	if (!netif_msg_tx_err(mac))
		return;

	cmdsta = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));

	printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
		"tx status 0x%016lx\n", mactx, *mac->tx_status);

	printk(KERN_ERR "pasemi_mac: tcmdsta 0x%08x\n", cmdsta);
}

514 515
static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
{
516 517 518 519 520 521 522
	unsigned int n;
	int count;
	struct pasemi_mac_buffer *info;
	struct sk_buff *skb;
	unsigned int i, len;
	u64 macrx;
	dma_addr_t dma;
523 524 525

	spin_lock(&mac->rx->lock);

526
	n = mac->rx->next_to_clean;
527

528
	for (count = limit; count; count--) {
529
		macrx = RX_RING(mac, n);
530

O
Olof Johansson 已提交
531 532 533 534
		if ((macrx & XCT_MACRX_E) ||
		    (*mac->rx_status & PAS_STATUS_ERROR))
			pasemi_mac_rx_error(mac, macrx);

535
		if (!(macrx & XCT_MACRX_O))
536 537 538 539 540 541 542 543 544 545 546
			break;

		info = NULL;

		/* We have to scan for our skb since there's no way
		 * to back-map them from the descriptor, and if we
		 * have several receive channels then they might not
		 * show up in the same order as they were put on the
		 * interface ring.
		 */

547 548 549 550 551
		dma = (RX_RING(mac, n+1) & XCT_PTR_ADDR_M);
		for (i = mac->rx->next_to_fill;
		     i < (mac->rx->next_to_fill + RX_RING_SIZE);
		     i++) {
			info = &RX_RING_INFO(mac, i);
552 553 554
			if (info->dma == dma)
				break;
		}
555

556
		skb = info->skb;
557

558 559
		prefetch(skb);
		prefetch(&skb->data_len);
560

561
		len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
562

563
		if (len < 256) {
564 565 566 567
			struct sk_buff *new_skb;

			new_skb = netdev_alloc_skb(mac->netdev,
						   len + LOCAL_SKB_ALIGN);
568
			if (new_skb) {
569
				skb_reserve(new_skb, LOCAL_SKB_ALIGN);
570
				memcpy(new_skb->data, skb->data, len);
571 572 573 574 575 576
				/* save the skb in buffer_info as good */
				skb = new_skb;
			}
			/* else just continue with the old one */
		} else
			info->skb = NULL;
577

578 579 580
		pci_unmap_single(mac->dma_pdev, dma, len, PCI_DMA_FROMDEVICE);

		info->dma = 0;
581

582 583
		skb_put(skb, len);

O
Olof Johansson 已提交
584
		if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
O
Olof Johansson 已提交
585
			skb->ip_summed = CHECKSUM_UNNECESSARY;
586
			skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
587 588 589 590
					   XCT_MACRX_CSUM_S;
		} else
			skb->ip_summed = CHECKSUM_NONE;

591 592
		mac->netdev->stats.rx_bytes += len;
		mac->netdev->stats.rx_packets++;
593

O
Olof Johansson 已提交
594
		skb->protocol = eth_type_trans(skb, mac->netdev);
595 596
		netif_receive_skb(skb);

597 598
		RX_RING(mac, n) = 0;
		RX_RING(mac, n+1) = 0;
599

600 601 602 603 604
		/* Need to zero it out since hardware doesn't, since the
		 * replenish loop uses it to tell when it's done.
		 */
		RX_BUFF(mac, i) = 0;

605
		n += 2;
606 607
	}

608 609 610 611 612
	if (n > RX_RING_SIZE) {
		/* Errata 5971 workaround: L2 target of headers */
		write_iob_reg(mac, PAS_IOB_COM_PKTHDRCNT, 0);
		n &= (RX_RING_SIZE-1);
	}
613
	mac->rx->next_to_clean = n;
614
	pasemi_mac_replenish_rx_ring(mac->netdev, limit-count);
615 616 617 618 619 620

	spin_unlock(&mac->rx->lock);

	return count;
}

O
Olof Johansson 已提交
621 622 623
/* Can't make this too large or we blow the kernel stack limits */
#define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)

624 625
static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
{
O
Olof Johansson 已提交
626
	int i, j;
627 628
	unsigned int start, descr_count, buf_count, batch_limit;
	unsigned int ring_limit;
629
	unsigned int total_count;
630
	unsigned long flags;
O
Olof Johansson 已提交
631 632
	struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
	dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
633

634
	total_count = 0;
635
	batch_limit = TX_CLEAN_BATCHSIZE;
636
restart:
637 638 639
	spin_lock_irqsave(&mac->tx->lock, flags);

	start = mac->tx->next_to_clean;
640 641 642 643 644
	ring_limit = mac->tx->next_to_fill;

	/* Compensate for when fill has wrapped but clean has not */
	if (start > ring_limit)
		ring_limit += TX_RING_SIZE;
645

O
Olof Johansson 已提交
646 647
	buf_count = 0;
	descr_count = 0;
648

O
Olof Johansson 已提交
649
	for (i = start;
650
	     descr_count < batch_limit && i < ring_limit;
O
Olof Johansson 已提交
651
	     i += buf_count) {
652
		u64 mactx = TX_RING(mac, i);
653
		struct sk_buff *skb;
O
Olof Johansson 已提交
654

655
		if ((mactx  & XCT_MACTX_E) ||
O
Olof Johansson 已提交
656
		    (*mac->tx_status & PAS_STATUS_ERROR))
657
			pasemi_mac_tx_error(mac, mactx);
O
Olof Johansson 已提交
658

659
		if (unlikely(mactx & XCT_MACTX_O))
660
			/* Not yet transmitted */
661 662
			break;

663 664
		skb = TX_RING_INFO(mac, i+1).skb;
		skbs[descr_count] = skb;
O
Olof Johansson 已提交
665

666 667
		buf_count = 2 + skb_shinfo(skb)->nr_frags;
		for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
O
Olof Johansson 已提交
668 669
			dmas[descr_count][j] = TX_RING_INFO(mac, i+1+j).dma;

670 671 672
		TX_RING(mac, i) = 0;
		TX_RING(mac, i+1) = 0;

O
Olof Johansson 已提交
673 674 675 676 677 678
		/* Since we always fill with an even number of entries, make
		 * sure we skip any unused one at the end as well.
		 */
		if (buf_count & 1)
			buf_count++;
		descr_count++;
679
	}
680
	mac->tx->next_to_clean = i & (TX_RING_SIZE-1);
O
Olof Johansson 已提交
681

682
	spin_unlock_irqrestore(&mac->tx->lock, flags);
683 684
	netif_wake_queue(mac->netdev);

O
Olof Johansson 已提交
685 686
	for (i = 0; i < descr_count; i++)
		pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
687

O
Olof Johansson 已提交
688
	total_count += descr_count;
689 690

	/* If the batch was full, try to clean more */
691
	if (descr_count == batch_limit)
692 693 694
		goto restart;

	return total_count;
695 696 697 698 699 700 701 702 703
}


static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
{
	struct net_device *dev = data;
	struct pasemi_mac *mac = netdev_priv(dev);
	unsigned int reg;

704
	if (!(*mac->rx_status & PAS_STATUS_CAUSE_M))
705 706
		return IRQ_NONE;

707 708 709 710 711 712 713 714 715
	/* Don't reset packet count so it won't fire again but clear
	 * all others.
	 */

	reg = 0;
	if (*mac->rx_status & PAS_STATUS_SOFT)
		reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
	if (*mac->rx_status & PAS_STATUS_ERROR)
		reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
716 717 718
	if (*mac->rx_status & PAS_STATUS_TIMER)
		reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;

719
	netif_rx_schedule(dev, &mac->napi);
720

721
	write_iob_reg(mac, PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
722 723 724 725 726 727 728 729

	return IRQ_HANDLED;
}

static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
{
	struct net_device *dev = data;
	struct pasemi_mac *mac = netdev_priv(dev);
O
Olof Johansson 已提交
730
	unsigned int reg, pcnt;
731

732
	if (!(*mac->tx_status & PAS_STATUS_CAUSE_M))
733 734 735 736
		return IRQ_NONE;

	pasemi_mac_clean_tx(mac);

O
Olof Johansson 已提交
737 738 739
	pcnt = *mac->tx_status & PAS_STATUS_PCNT_M;

	reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
740 741 742 743 744

	if (*mac->tx_status & PAS_STATUS_SOFT)
		reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
	if (*mac->tx_status & PAS_STATUS_ERROR)
		reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
745

746
	write_iob_reg(mac, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch), reg);
747 748 749 750

	return IRQ_HANDLED;
}

O
Olof Johansson 已提交
751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
static void pasemi_adjust_link(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
	int msg;
	unsigned int flags;
	unsigned int new_flags;

	if (!mac->phydev->link) {
		/* If no link, MAC speed settings don't matter. Just report
		 * link down and return.
		 */
		if (mac->link && netif_msg_link(mac))
			printk(KERN_INFO "%s: Link is down.\n", dev->name);

		netif_carrier_off(dev);
		mac->link = 0;

		return;
	} else
		netif_carrier_on(dev);

772
	flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
O
Olof Johansson 已提交
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
	new_flags = flags & ~(PAS_MAC_CFG_PCFG_HD | PAS_MAC_CFG_PCFG_SPD_M |
			      PAS_MAC_CFG_PCFG_TSR_M);

	if (!mac->phydev->duplex)
		new_flags |= PAS_MAC_CFG_PCFG_HD;

	switch (mac->phydev->speed) {
	case 1000:
		new_flags |= PAS_MAC_CFG_PCFG_SPD_1G |
			     PAS_MAC_CFG_PCFG_TSR_1G;
		break;
	case 100:
		new_flags |= PAS_MAC_CFG_PCFG_SPD_100M |
			     PAS_MAC_CFG_PCFG_TSR_100M;
		break;
	case 10:
		new_flags |= PAS_MAC_CFG_PCFG_SPD_10M |
			     PAS_MAC_CFG_PCFG_TSR_10M;
		break;
	default:
		printk("Unsupported speed %d\n", mac->phydev->speed);
	}

	/* Print on link or speed/duplex change */
	msg = mac->link != mac->phydev->link || flags != new_flags;

	mac->duplex = mac->phydev->duplex;
	mac->speed = mac->phydev->speed;
	mac->link = mac->phydev->link;

	if (new_flags != flags)
804
		write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
O
Olof Johansson 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822

	if (msg && netif_msg_link(mac))
		printk(KERN_INFO "%s: Link is up at %d Mbps, %s duplex.\n",
		       dev->name, mac->speed, mac->duplex ? "full" : "half");
}

static int pasemi_mac_phy_init(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
	struct device_node *dn, *phy_dn;
	struct phy_device *phydev;
	unsigned int phy_id;
	const phandle *ph;
	const unsigned int *prop;
	struct resource r;
	int ret;

	dn = pci_device_to_OF_node(mac->pdev);
823
	ph = of_get_property(dn, "phy-handle", NULL);
O
Olof Johansson 已提交
824 825 826 827
	if (!ph)
		return -ENODEV;
	phy_dn = of_find_node_by_phandle(*ph);

828
	prop = of_get_property(phy_dn, "reg", NULL);
O
Olof Johansson 已提交
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
	ret = of_address_to_resource(phy_dn->parent, 0, &r);
	if (ret)
		goto err;

	phy_id = *prop;
	snprintf(mac->phy_id, BUS_ID_SIZE, PHY_ID_FMT, (int)r.start, phy_id);

	of_node_put(phy_dn);

	mac->link = 0;
	mac->speed = 0;
	mac->duplex = -1;

	phydev = phy_connect(dev, mac->phy_id, &pasemi_adjust_link, 0, PHY_INTERFACE_MODE_SGMII);

	if (IS_ERR(phydev)) {
		printk(KERN_ERR "%s: Could not attach to phy\n", dev->name);
		return PTR_ERR(phydev);
	}

	mac->phydev = phydev;

	return 0;

err:
	of_node_put(phy_dn);
	return -ENODEV;
}


859 860 861
static int pasemi_mac_open(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
862
	int base_irq;
863 864 865 866
	unsigned int flags;
	int ret;

	/* enable rx section */
867
	write_dma_reg(mac, PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
868 869

	/* enable tx section */
870
	write_dma_reg(mac, PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
871 872 873 874 875

	flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
		PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
		PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);

876
	write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
877

878 879
	write_iob_reg(mac, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
			   PAS_IOB_DMA_RXCH_CFG_CNTTH(0));
880

881
	write_iob_reg(mac, PAS_IOB_DMA_TXCH_CFG(mac->dma_txch),
882
			   PAS_IOB_DMA_TXCH_CFG_CNTTH(128));
883

884 885 886 887
	/* Clear out any residual packet count state from firmware */
	pasemi_mac_restart_rx_intr(mac);
	pasemi_mac_restart_tx_intr(mac);

888
	/* 0xffffff is max value, about 16ms */
889 890
	write_iob_reg(mac, PAS_IOB_DMA_COM_TIMEOUTCFG,
			   PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
891 892 893 894 895 896 897 898 899

	ret = pasemi_mac_setup_rx_resources(dev);
	if (ret)
		goto out_rx_resources;

	ret = pasemi_mac_setup_tx_resources(dev);
	if (ret)
		goto out_tx_resources;

900 901 902
	write_mac_reg(mac, PAS_MAC_IPC_CHNL,
			   PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
			   PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
903 904

	/* enable rx if */
905 906
	write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
			   PAS_DMA_RXINT_RCMDSTA_EN);
907 908

	/* enable rx channel */
909 910 911
	write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
			   PAS_DMA_RXCHAN_CCMDSTA_EN |
			   PAS_DMA_RXCHAN_CCMDSTA_DU);
912 913

	/* enable tx channel */
914 915
	write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
			   PAS_DMA_TXCHAN_TCMDSTA_EN);
916

917
	pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
918

919 920 921 922 923 924 925 926 927 928 929
	flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
		PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;

	if (mac->type == MAC_TYPE_GMAC)
		flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
	else
		flags |= PAS_MAC_CFG_PCFG_TSR_10G | PAS_MAC_CFG_PCFG_SPD_10G;

	/* Enable interface in MAC */
	write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);

O
Olof Johansson 已提交
930 931 932 933 934 935 936
	ret = pasemi_mac_phy_init(dev);
	/* Some configs don't have PHYs (XAUI etc), so don't complain about
	 * failed init due to -ENODEV.
	 */
	if (ret && ret != -ENODEV)
		dev_warn(&mac->pdev->dev, "phy init failed: %d\n", ret);

937
	netif_start_queue(dev);
938
	napi_enable(&mac->napi);
939

940 941 942 943 944 945 946 947 948 949 950 951
	/* Interrupts are a bit different for our DMA controller: While
	 * it's got one a regular PCI device header, the interrupt there
	 * is really the base of the range it's using. Each tx and rx
	 * channel has it's own interrupt source.
	 */

	base_irq = virq_to_hw(mac->dma_pdev->irq);

	mac->tx_irq = irq_create_mapping(NULL, base_irq + mac->dma_txch);
	mac->rx_irq = irq_create_mapping(NULL, base_irq + 20 + mac->dma_txch);

	ret = request_irq(mac->tx_irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
952 953 954
			  mac->tx->irq_name, dev);
	if (ret) {
		dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
955
			base_irq + mac->dma_txch, ret);
956 957 958
		goto out_tx_int;
	}

959
	ret = request_irq(mac->rx_irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
960 961 962
			  mac->rx->irq_name, dev);
	if (ret) {
		dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
963
			base_irq + 20 + mac->dma_rxch, ret);
964 965 966
		goto out_rx_int;
	}

O
Olof Johansson 已提交
967 968 969
	if (mac->phydev)
		phy_start(mac->phydev);

970 971 972
	return 0;

out_rx_int:
973
	free_irq(mac->tx_irq, dev);
974
out_tx_int:
975
	napi_disable(&mac->napi);
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992
	netif_stop_queue(dev);
	pasemi_mac_free_tx_resources(dev);
out_tx_resources:
	pasemi_mac_free_rx_resources(dev);
out_rx_resources:

	return ret;
}

#define MAX_RETRIES 5000

static int pasemi_mac_close(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
	unsigned int stat;
	int retries;

O
Olof Johansson 已提交
993 994 995 996 997
	if (mac->phydev) {
		phy_stop(mac->phydev);
		phy_disconnect(mac->phydev);
	}

998
	netif_stop_queue(dev);
999
	napi_disable(&mac->napi);
1000 1001 1002 1003 1004 1005

	/* Clean out any pending buffers */
	pasemi_mac_clean_tx(mac);
	pasemi_mac_clean_rx(mac, RX_RING_SIZE);

	/* Disable interface */
1006 1007 1008
	write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), PAS_DMA_TXCHAN_TCMDSTA_ST);
	write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), PAS_DMA_RXINT_RCMDSTA_ST);
	write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), PAS_DMA_RXCHAN_CCMDSTA_ST);
1009 1010

	for (retries = 0; retries < MAX_RETRIES; retries++) {
1011
		stat = read_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch));
1012
		if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
1013 1014 1015 1016
			break;
		cond_resched();
	}

1017
	if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
1018 1019 1020
		dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");

	for (retries = 0; retries < MAX_RETRIES; retries++) {
1021
		stat = read_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch));
1022
		if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
1023 1024 1025 1026
			break;
		cond_resched();
	}

1027
	if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
1028 1029 1030
		dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");

	for (retries = 0; retries < MAX_RETRIES; retries++) {
1031
		stat = read_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1032
		if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
1033 1034 1035 1036
			break;
		cond_resched();
	}

1037
	if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
1038 1039 1040 1041 1042 1043
		dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");

	/* Then, disable the channel. This must be done separately from
	 * stopping, since you can't disable when active.
	 */

1044 1045 1046
	write_dma_reg(mac, PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
	write_dma_reg(mac, PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
	write_dma_reg(mac, PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
1047

1048 1049
	free_irq(mac->tx_irq, dev);
	free_irq(mac->rx_irq, dev);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

	/* Free resources */
	pasemi_mac_free_rx_resources(dev);
	pasemi_mac_free_tx_resources(dev);

	return 0;
}

static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
	struct pasemi_mac_txring *txring;
O
Olof Johansson 已提交
1062 1063 1064
	u64 dflags, mactx;
	dma_addr_t map[MAX_SKB_FRAGS+1];
	unsigned int map_size[MAX_SKB_FRAGS+1];
1065
	unsigned long flags;
O
Olof Johansson 已提交
1066
	int i, nfrags;
1067 1068 1069 1070

	dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;

	if (skb->ip_summed == CHECKSUM_PARTIAL) {
1071 1072
		const unsigned char *nh = skb_network_header(skb);

1073
		switch (ip_hdr(skb)->protocol) {
1074 1075
		case IPPROTO_TCP:
			dflags |= XCT_MACTX_CSUM_TCP;
1076
			dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
1077
			dflags |= XCT_MACTX_IPO(nh - skb->data);
1078 1079 1080
			break;
		case IPPROTO_UDP:
			dflags |= XCT_MACTX_CSUM_UDP;
1081
			dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
1082
			dflags |= XCT_MACTX_IPO(nh - skb->data);
1083 1084 1085 1086
			break;
		}
	}

O
Olof Johansson 已提交
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
	nfrags = skb_shinfo(skb)->nr_frags;

	map[0] = pci_map_single(mac->dma_pdev, skb->data, skb_headlen(skb),
				PCI_DMA_TODEVICE);
	map_size[0] = skb_headlen(skb);
	if (dma_mapping_error(map[0]))
		goto out_err_nolock;

	for (i = 0; i < nfrags; i++) {
		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
1097

O
Olof Johansson 已提交
1098 1099 1100 1101 1102 1103 1104 1105 1106
		map[i+1] = pci_map_page(mac->dma_pdev, frag->page,
					frag->page_offset, frag->size,
					PCI_DMA_TODEVICE);
		map_size[i+1] = frag->size;
		if (dma_mapping_error(map[i+1])) {
			nfrags = i;
			goto out_err_nolock;
		}
	}
1107

O
Olof Johansson 已提交
1108 1109
	mactx = dflags | XCT_MACTX_LLEN(skb->len);

1110 1111 1112 1113
	txring = mac->tx;

	spin_lock_irqsave(&txring->lock, flags);

1114 1115 1116 1117 1118 1119 1120 1121
	/* Avoid stepping on the same cache line that the DMA controller
	 * is currently about to send, so leave at least 8 words available.
	 * Total free space needed is mactx + fragments + 8
	 */
	if (RING_AVAIL(txring) < nfrags + 10) {
		/* no room -- stop the queue and wait for tx intr */
		netif_stop_queue(dev);
		goto out_err;
1122 1123
	}

1124
	TX_RING(mac, txring->next_to_fill) = mactx;
O
Olof Johansson 已提交
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	txring->next_to_fill++;
	TX_RING_INFO(mac, txring->next_to_fill).skb = skb;
	for (i = 0; i <= nfrags; i++) {
		TX_RING(mac, txring->next_to_fill+i) =
		XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
		TX_RING_INFO(mac, txring->next_to_fill+i).dma = map[i];
	}

	/* We have to add an even number of 8-byte entries to the ring
	 * even if the last one is unused. That means always an odd number
	 * of pointers + one mactx descriptor.
	 */
	if (nfrags & 1)
		nfrags++;
1139

1140 1141
	txring->next_to_fill = (txring->next_to_fill + nfrags + 1) &
				(TX_RING_SIZE-1);
1142

1143 1144
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
1145 1146 1147

	spin_unlock_irqrestore(&txring->lock, flags);

O
Olof Johansson 已提交
1148
	write_dma_reg(mac, PAS_DMA_TXCHAN_INCR(mac->dma_txch), (nfrags+2) >> 1);
1149 1150 1151 1152 1153

	return NETDEV_TX_OK;

out_err:
	spin_unlock_irqrestore(&txring->lock, flags);
O
Olof Johansson 已提交
1154 1155 1156 1157 1158
out_err_nolock:
	while (nfrags--)
		pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
				 PCI_DMA_TODEVICE);

1159 1160 1161 1162 1163 1164 1165 1166
	return NETDEV_TX_BUSY;
}

static void pasemi_mac_set_rx_mode(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
	unsigned int flags;

1167
	flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
1168 1169 1170 1171 1172 1173 1174

	/* Set promiscuous */
	if (dev->flags & IFF_PROMISC)
		flags |= PAS_MAC_CFG_PCFG_PR;
	else
		flags &= ~PAS_MAC_CFG_PCFG_PR;

1175
	write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1176 1177 1178
}


1179
static int pasemi_mac_poll(struct napi_struct *napi, int budget)
1180
{
1181 1182 1183
	struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
	struct net_device *dev = mac->netdev;
	int pkts;
1184

O
Olof Johansson 已提交
1185
	pasemi_mac_clean_tx(mac);
1186 1187
	pkts = pasemi_mac_clean_rx(mac, budget);
	if (pkts < budget) {
1188
		/* all done, no more packets present */
1189
		netif_rx_complete(dev, napi);
1190

1191
		pasemi_mac_restart_rx_intr(mac);
1192
	}
1193
	return pkts;
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 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
static void __iomem * __devinit map_onedev(struct pci_dev *p, int index)
{
	struct device_node *dn;
	void __iomem *ret;

	dn = pci_device_to_OF_node(p);
	if (!dn)
		goto fallback;

	ret = of_iomap(dn, index);
	if (!ret)
		goto fallback;

	return ret;
fallback:
	/* This is hardcoded and ugly, but we have some firmware versions
	 * that don't provide the register space in the device tree. Luckily
	 * they are at well-known locations so we can just do the math here.
	 */
	return ioremap(0xe0000000 + (p->devfn << 12), 0x2000);
}

static int __devinit pasemi_mac_map_regs(struct pasemi_mac *mac)
{
	struct resource res;
	struct device_node *dn;
	int err;

	mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
	if (!mac->dma_pdev) {
		dev_err(&mac->pdev->dev, "Can't find DMA Controller\n");
		return -ENODEV;
	}

	mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
	if (!mac->iob_pdev) {
		dev_err(&mac->pdev->dev, "Can't find I/O Bridge\n");
		return -ENODEV;
	}

	mac->regs = map_onedev(mac->pdev, 0);
	mac->dma_regs = map_onedev(mac->dma_pdev, 0);
	mac->iob_regs = map_onedev(mac->iob_pdev, 0);

	if (!mac->regs || !mac->dma_regs || !mac->iob_regs) {
		dev_err(&mac->pdev->dev, "Can't map registers\n");
		return -ENODEV;
	}

	/* The dma status structure is located in the I/O bridge, and
	 * is cache coherent.
	 */
	if (!dma_status) {
		dn = pci_device_to_OF_node(mac->iob_pdev);
		if (dn)
			err = of_address_to_resource(dn, 1, &res);
		if (!dn || err) {
			/* Fallback for old firmware */
			res.start = 0xfd800000;
			res.end = res.start + 0x1000;
		}
		dma_status = __ioremap(res.start, res.end-res.start, 0);
	}

	return 0;
}

1263 1264 1265 1266 1267 1268 1269
static int __devinit
pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	static int index = 0;
	struct net_device *dev;
	struct pasemi_mac *mac;
	int err;
1270
	DECLARE_MAC_BUF(mac_buf);
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291

	err = pci_enable_device(pdev);
	if (err)
		return err;

	dev = alloc_etherdev(sizeof(struct pasemi_mac));
	if (dev == NULL) {
		dev_err(&pdev->dev,
			"pasemi_mac: Could not allocate ethernet device.\n");
		err = -ENOMEM;
		goto out_disable_device;
	}

	pci_set_drvdata(pdev, dev);
	SET_NETDEV_DEV(dev, &pdev->dev);

	mac = netdev_priv(dev);

	mac->pdev = pdev;
	mac->netdev = dev;

1292 1293
	netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);

O
Olof Johansson 已提交
1294
	dev->features = NETIF_F_HW_CSUM | NETIF_F_LLTX | NETIF_F_SG;
1295

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	/* These should come out of the device tree eventually */
	mac->dma_txch = index;
	mac->dma_rxch = index;

	/* We probe GMAC before XAUI, but the DMA interfaces are
	 * in XAUI, GMAC order.
	 */
	if (index < 4)
		mac->dma_if = index + 2;
	else
		mac->dma_if = index - 4;
	index++;

	switch (pdev->device) {
	case 0xa005:
		mac->type = MAC_TYPE_GMAC;
		break;
	case 0xa006:
		mac->type = MAC_TYPE_XAUI;
		break;
	default:
		err = -ENODEV;
		goto out;
	}

	/* get mac addr from device tree */
	if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
		err = -ENODEV;
		goto out;
	}
	memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));

	dev->open = pasemi_mac_open;
	dev->stop = pasemi_mac_close;
	dev->hard_start_xmit = pasemi_mac_start_tx;
	dev->set_multicast_list = pasemi_mac_set_rx_mode;

1333 1334 1335
	err = pasemi_mac_map_regs(mac);
	if (err)
		goto out;
1336 1337 1338 1339

	mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
	mac->tx_status = &dma_status->tx_sta[mac->dma_txch];

1340 1341
	mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);

O
Olof Johansson 已提交
1342 1343 1344
	/* Enable most messages by default */
	mac->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;

1345 1346 1347 1348 1349 1350
	err = register_netdev(dev);

	if (err) {
		dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
			err);
		goto out;
O
Olof Johansson 已提交
1351
	} else if netif_msg_probe(mac)
1352
		printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
1353
		       "hw addr %s\n",
1354 1355
		       dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
		       mac->dma_if, mac->dma_txch, mac->dma_rxch,
1356
		       print_mac(mac_buf, dev->dev_addr));
1357 1358 1359 1360

	return err;

out:
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
	if (mac->iob_pdev)
		pci_dev_put(mac->iob_pdev);
	if (mac->dma_pdev)
		pci_dev_put(mac->dma_pdev);
	if (mac->dma_regs)
		iounmap(mac->dma_regs);
	if (mac->iob_regs)
		iounmap(mac->iob_regs);
	if (mac->regs)
		iounmap(mac->regs);

1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	free_netdev(dev);
out_disable_device:
	pci_disable_device(pdev);
	return err;

}

static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
{
	struct net_device *netdev = pci_get_drvdata(pdev);
	struct pasemi_mac *mac;

	if (!netdev)
		return;

	mac = netdev_priv(netdev);

	unregister_netdev(netdev);

	pci_disable_device(pdev);
	pci_dev_put(mac->dma_pdev);
	pci_dev_put(mac->iob_pdev);

1395 1396 1397 1398
	iounmap(mac->regs);
	iounmap(mac->dma_regs);
	iounmap(mac->iob_regs);

1399 1400 1401 1402 1403 1404 1405
	pci_set_drvdata(pdev, NULL);
	free_netdev(netdev);
}

static struct pci_device_id pasemi_mac_pci_tbl[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
	{ PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
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
};

MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);

static struct pci_driver pasemi_mac_driver = {
	.name		= "pasemi_mac",
	.id_table	= pasemi_mac_pci_tbl,
	.probe		= pasemi_mac_probe,
	.remove		= __devexit_p(pasemi_mac_remove),
};

static void __exit pasemi_mac_cleanup_module(void)
{
	pci_unregister_driver(&pasemi_mac_driver);
	__iounmap(dma_status);
	dma_status = NULL;
}

int pasemi_mac_init_module(void)
{
	return pci_register_driver(&pasemi_mac_driver);
}

module_init(pasemi_mac_init_module);
module_exit(pasemi_mac_cleanup_module);