pasemi_mac.c 36.1 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
#include <asm/irq.h>
O
Olof Johansson 已提交
37
#include <asm/firmware.h>
38
#include <asm/pasemi_dma.h>
39

40 41
#include "pasemi_mac.h"

42 43 44 45 46 47
/* 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
48 49 50 51 52

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


/* Must be a power of two */
O
Olof Johansson 已提交
59
#define RX_RING_SIZE 1024
60
#define TX_RING_SIZE 4096
61

62 63 64 65 66 67 68 69 70 71
#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)

72
#define TX_DESC(tx, num)	((tx)->chan.ring_virt[(num) & (TX_RING_SIZE-1)])
73
#define TX_DESC_INFO(tx, num)	((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
74
#define RX_DESC(rx, num)	((rx)->chan.ring_virt[(num) & (RX_RING_SIZE-1)])
75 76
#define RX_DESC_INFO(rx, num)	((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
#define RX_BUFF(rx, num)	((rx)->buffers[(num) & (RX_RING_SIZE-1)])
77

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

82 83
#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */

84 85 86 87 88 89 90
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");
91

O
Olof Johansson 已提交
92 93 94 95 96 97 98 99 100
static int translation_enabled(void)
{
#if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
	return 1;
#else
	return firmware_has_feature(FW_FEATURE_LPAR);
#endif
}

101
static void write_iob_reg(unsigned int reg, unsigned int val)
102
{
103
	pasemi_write_iob_reg(reg, val);
104 105
}

O
Olof Johansson 已提交
106
static unsigned int read_mac_reg(const struct pasemi_mac *mac, unsigned int reg)
107
{
108
	return pasemi_read_mac_reg(mac->dma_if, reg);
109 110
}

O
Olof Johansson 已提交
111
static void write_mac_reg(const struct pasemi_mac *mac, unsigned int reg,
112 113
			  unsigned int val)
{
114
	pasemi_write_mac_reg(mac->dma_if, reg, val);
115 116
}

117
static unsigned int read_dma_reg(unsigned int reg)
118
{
119
	return pasemi_read_dma_reg(reg);
120 121
}

122
static void write_dma_reg(unsigned int reg, unsigned int val)
123
{
124
	pasemi_write_dma_reg(reg, val);
125 126
}

O
Olof Johansson 已提交
127
static struct pasemi_mac_rxring *rx_ring(const struct pasemi_mac *mac)
128 129 130 131
{
	return mac->rx;
}

O
Olof Johansson 已提交
132
static struct pasemi_mac_txring *tx_ring(const struct pasemi_mac *mac)
133 134 135 136
{
	return mac->tx;
}

O
Olof Johansson 已提交
137 138 139 140 141 142 143 144 145 146
static inline void prefetch_skb(const struct sk_buff *skb)
{
	const void *d = skb;

	prefetch(d);
	prefetch(d+64);
	prefetch(d+128);
	prefetch(d+192);
}

147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
static int mac_to_intf(struct pasemi_mac *mac)
{
	struct pci_dev *pdev = mac->pdev;
	u32 tmp;
	int nintf, off, i, j;
	int devfn = pdev->devfn;

	tmp = read_dma_reg(PAS_DMA_CAP_IFI);
	nintf = (tmp & PAS_DMA_CAP_IFI_NIN_M) >> PAS_DMA_CAP_IFI_NIN_S;
	off = (tmp & PAS_DMA_CAP_IFI_IOFF_M) >> PAS_DMA_CAP_IFI_IOFF_S;

	/* IOFF contains the offset to the registers containing the
	 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
	 * of total interfaces. Each register contains 4 devfns.
	 * Just do a linear search until we find the devfn of the MAC
	 * we're trying to look up.
	 */

	for (i = 0; i < (nintf+3)/4; i++) {
		tmp = read_dma_reg(off+4*i);
		for (j = 0; j < 4; j++) {
			if (((tmp >> (8*j)) & 0xff) == devfn)
				return i*4 + j;
		}
	}
	return -1;
}

175 176 177 178
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);
179
	int len;
180 181 182 183 184 185 186 187 188
	const u8 *maddr;
	u8 addr[6];

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

189 190 191 192 193 194 195 196 197 198
	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.
	 */
199 200

	if (maddr == NULL)
201
		maddr = of_get_property(dn, "mac-address", NULL);
202

203 204 205 206 207 208
	if (maddr == NULL) {
		dev_warn(&pdev->dev,
			 "no mac address in device tree, not configuring\n");
		return -ENOENT;
	}

209

210 211 212 213 214 215 216
	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;
	}

217 218
	memcpy(mac->mac_addr, addr, 6);

219 220 221
	return 0;
}

O
Olof Johansson 已提交
222 223
static int pasemi_mac_unmap_tx_skb(struct pasemi_mac *mac,
				    struct sk_buff *skb,
O
Olof Johansson 已提交
224
				    const dma_addr_t *dmas)
O
Olof Johansson 已提交
225 226 227
{
	int f;
	int nfrags = skb_shinfo(skb)->nr_frags;
O
Olof Johansson 已提交
228
	struct pci_dev *pdev = mac->dma_pdev;
O
Olof Johansson 已提交
229

O
Olof Johansson 已提交
230
	pci_unmap_single(pdev, dmas[0], skb_headlen(skb), PCI_DMA_TODEVICE);
O
Olof Johansson 已提交
231 232 233 234

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

O
Olof Johansson 已提交
235
		pci_unmap_page(pdev, dmas[f+1], frag->size, PCI_DMA_TODEVICE);
O
Olof Johansson 已提交
236 237 238 239 240 241 242 243 244
	}
	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;
}

O
Olof Johansson 已提交
245
static int pasemi_mac_setup_rx_resources(const struct net_device *dev)
246 247 248
{
	struct pasemi_mac_rxring *ring;
	struct pasemi_mac *mac = netdev_priv(dev);
249
	int chno;
O
Olof Johansson 已提交
250
	unsigned int cfg;
251

252 253
	ring = pasemi_dma_alloc_chan(RXCHAN, sizeof(struct pasemi_mac_rxring),
				     offsetof(struct pasemi_mac_rxring, chan));
254

255 256 257 258 259
	if (!ring) {
		dev_err(&mac->pdev->dev, "Can't allocate RX channel\n");
		goto out_chan;
	}
	chno = ring->chan.chno;
260 261 262

	spin_lock_init(&ring->lock);

263
	ring->size = RX_RING_SIZE;
264
	ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
265 266
				  RX_RING_SIZE, GFP_KERNEL);

267 268
	if (!ring->ring_info)
		goto out_ring_info;
269 270

	/* Allocate descriptors */
271
	if (pasemi_dma_alloc_ring(&ring->chan, RX_RING_SIZE))
272
		goto out_ring_desc;
273 274 275 276 277

	ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
					   RX_RING_SIZE * sizeof(u64),
					   &ring->buf_dma, GFP_KERNEL);
	if (!ring->buffers)
278
		goto out_ring_desc;
279 280 281

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

282 283
	write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno),
		      PAS_DMA_RXCHAN_BASEL_BRBL(ring->chan.ring_dma));
284

285 286 287
	write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno),
		      PAS_DMA_RXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32) |
		      PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 3));
288

O
Olof Johansson 已提交
289
	cfg = PAS_DMA_RXCHAN_CFG_HBU(2);
O
Olof Johansson 已提交
290 291 292 293

	if (translation_enabled())
		cfg |= PAS_DMA_RXCHAN_CFG_CTR;

294
	write_dma_reg(PAS_DMA_RXCHAN_CFG(chno), cfg);
295

296 297
	write_dma_reg(PAS_DMA_RXINT_BASEL(mac->dma_if),
		      PAS_DMA_RXINT_BASEL_BRBL(ring->buf_dma));
298

299 300 301
	write_dma_reg(PAS_DMA_RXINT_BASEU(mac->dma_if),
		      PAS_DMA_RXINT_BASEU_BRBH(ring->buf_dma >> 32) |
		      PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
302

O
Olof Johansson 已提交
303
	cfg = PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2 |
O
Olof Johansson 已提交
304 305 306 307 308 309
	      PAS_DMA_RXINT_CFG_LW | PAS_DMA_RXINT_CFG_RBP |
	      PAS_DMA_RXINT_CFG_HEN;

	if (translation_enabled())
		cfg |= PAS_DMA_RXINT_CFG_ITRR | PAS_DMA_RXINT_CFG_ITR;

310
	write_dma_reg(PAS_DMA_RXINT_CFG(mac->dma_if), cfg);
311

312 313
	ring->next_to_fill = 0;
	ring->next_to_clean = 0;
314
	ring->mac = mac;
315 316 317 318
	mac->rx = ring;

	return 0;

319 320 321
out_ring_desc:
	kfree(ring->ring_info);
out_ring_info:
322 323
	pasemi_dma_free_chan(&ring->chan);
out_chan:
324 325 326
	return -ENOMEM;
}

327
static struct pasemi_mac_txring *
O
Olof Johansson 已提交
328
pasemi_mac_setup_tx_resources(const struct net_device *dev)
329 330 331 332
{
	struct pasemi_mac *mac = netdev_priv(dev);
	u32 val;
	struct pasemi_mac_txring *ring;
O
Olof Johansson 已提交
333
	unsigned int cfg;
334
	int chno;
335

336 337 338 339 340 341 342 343 344
	ring = pasemi_dma_alloc_chan(TXCHAN, sizeof(struct pasemi_mac_txring),
				     offsetof(struct pasemi_mac_txring, chan));

	if (!ring) {
		dev_err(&mac->pdev->dev, "Can't allocate TX channel\n");
		goto out_chan;
	}

	chno = ring->chan.chno;
345 346 347

	spin_lock_init(&ring->lock);

348
	ring->size = TX_RING_SIZE;
349
	ring->ring_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
350
				  TX_RING_SIZE, GFP_KERNEL);
351 352
	if (!ring->ring_info)
		goto out_ring_info;
353 354

	/* Allocate descriptors */
355
	if (pasemi_dma_alloc_ring(&ring->chan, TX_RING_SIZE))
356
		goto out_ring_desc;
357

358 359 360
	write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno),
		      PAS_DMA_TXCHAN_BASEL_BRBL(ring->chan.ring_dma));
	val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->chan.ring_dma >> 32);
361
	val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 3);
362

363
	write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno), val);
364

O
Olof Johansson 已提交
365 366 367 368 369 370 371 372
	cfg = 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);

	if (translation_enabled())
		cfg |= PAS_DMA_TXCHAN_CFG_TRD | PAS_DMA_TXCHAN_CFG_TRR;

373
	write_dma_reg(PAS_DMA_TXCHAN_CFG(chno), cfg);
374

375
	ring->next_to_fill = 0;
376
	ring->next_to_clean = 0;
377
	ring->mac = mac;
378

379
	return ring;
380

381 382 383
out_ring_desc:
	kfree(ring->ring_info);
out_ring_info:
384 385
	pasemi_dma_free_chan(&ring->chan);
out_chan:
386
	return NULL;
387 388
}

389
static void pasemi_mac_free_tx_resources(struct pasemi_mac *mac)
390
{
391
	struct pasemi_mac_txring *txring = tx_ring(mac);
O
Olof Johansson 已提交
392
	unsigned int i, j;
393
	struct pasemi_mac_buffer *info;
O
Olof Johansson 已提交
394 395
	dma_addr_t dmas[MAX_SKB_FRAGS+1];
	int freed;
396
	int start, limit;
397

398 399
	start = txring->next_to_clean;
	limit = txring->next_to_fill;
400 401 402 403 404 405

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

	for (i = start; i < limit; i += freed) {
406
		info = &txring->ring_info[(i+1) & (TX_RING_SIZE-1)];
407
		if (info->dma && info->skb) {
O
Olof Johansson 已提交
408
			for (j = 0; j <= skb_shinfo(info->skb)->nr_frags; j++)
409 410
				dmas[j] = txring->ring_info[(i+1+j) &
						(TX_RING_SIZE-1)].dma;
O
Olof Johansson 已提交
411 412 413
			freed = pasemi_mac_unmap_tx_skb(mac, info->skb, dmas);
		} else
			freed = 2;
414 415
	}

416
	kfree(txring->ring_info);
417 418
	pasemi_dma_free_chan(&txring->chan);

419 420
}

421
static void pasemi_mac_free_rx_resources(struct pasemi_mac *mac)
422
{
423
	struct pasemi_mac_rxring *rx = rx_ring(mac);
424 425 426 427
	unsigned int i;
	struct pasemi_mac_buffer *info;

	for (i = 0; i < RX_RING_SIZE; i++) {
428
		info = &RX_DESC_INFO(rx, i);
429 430 431 432 433 434
		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);
435
		}
436 437
		info->dma = 0;
		info->skb = NULL;
438 439
	}

440
	for (i = 0; i < RX_RING_SIZE; i++)
441
		RX_DESC(rx, i) = 0;
442

443
	dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
444
			  rx_ring(mac)->buffers, rx_ring(mac)->buf_dma);
445

446
	kfree(rx_ring(mac)->ring_info);
447
	pasemi_dma_free_chan(&rx_ring(mac)->chan);
448 449 450
	mac->rx = NULL;
}

O
Olof Johansson 已提交
451 452
static void pasemi_mac_replenish_rx_ring(const struct net_device *dev,
					 const int limit)
453
{
O
Olof Johansson 已提交
454
	const struct pasemi_mac *mac = netdev_priv(dev);
455
	struct pasemi_mac_rxring *rx = rx_ring(mac);
456
	int fill, count;
457

458
	if (limit <= 0)
459 460
		return;

461
	fill = rx_ring(mac)->next_to_fill;
462
	for (count = 0; count < limit; count++) {
463 464
		struct pasemi_mac_buffer *info = &RX_DESC_INFO(rx, fill);
		u64 *buff = &RX_BUFF(rx, fill);
465 466 467
		struct sk_buff *skb;
		dma_addr_t dma;

468 469 470
		/* Entry in use? */
		WARN_ON(*buff);

471 472 473
		/* skb might still be in there for recycle on short receives */
		if (info->skb)
			skb = info->skb;
474
		else {
475
			skb = dev_alloc_skb(BUF_SIZE);
476 477
			skb_reserve(skb, LOCAL_SKB_ALIGN);
		}
478

479
		if (unlikely(!skb))
480 481
			break;

482 483
		dma = pci_map_single(mac->dma_pdev, skb->data,
				     BUF_SIZE - LOCAL_SKB_ALIGN,
484 485
				     PCI_DMA_FROMDEVICE);

486
		if (unlikely(dma_mapping_error(dma))) {
487 488 489 490 491 492 493
			dev_kfree_skb_irq(info->skb);
			break;
		}

		info->skb = skb;
		info->dma = dma;
		*buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
494
		fill++;
495 496 497 498
	}

	wmb();

499
	write_dma_reg(PAS_DMA_RXINT_INCR(mac->dma_if), count);
500

501
	rx_ring(mac)->next_to_fill = (rx_ring(mac)->next_to_fill + count) &
502
				(RX_RING_SIZE - 1);
503 504
}

O
Olof Johansson 已提交
505
static void pasemi_mac_restart_rx_intr(const struct pasemi_mac *mac)
506
{
O
Olof Johansson 已提交
507
	unsigned int reg, pcnt;
508 509 510 511
	/* Re-enable packet count interrupts: finally
	 * ack the packet count interrupt we got in rx_intr.
	 */

512
	pcnt = *rx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
513

O
Olof Johansson 已提交
514
	reg = PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_RXCH_RESET_PINTC;
515

516
	write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac->rx->chan.chno), reg);
517 518
}

O
Olof Johansson 已提交
519
static void pasemi_mac_restart_tx_intr(const struct pasemi_mac *mac)
520
{
O
Olof Johansson 已提交
521
	unsigned int reg, pcnt;
522 523

	/* Re-enable packet count interrupts */
524
	pcnt = *tx_ring(mac)->chan.status & PAS_STATUS_PCNT_M;
525

O
Olof Johansson 已提交
526
	reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
527

528
	write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac)->chan.chno), reg);
529 530 531
}


O
Olof Johansson 已提交
532 533
static inline void pasemi_mac_rx_error(const struct pasemi_mac *mac,
				       const u64 macrx)
O
Olof Johansson 已提交
534 535
{
	unsigned int rcmdsta, ccmdsta;
536
	struct pasemi_dmachan *chan = &rx_ring(mac)->chan;
O
Olof Johansson 已提交
537 538 539 540

	if (!netif_msg_rx_err(mac))
		return;

541 542
	rcmdsta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
	ccmdsta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan->chno));
O
Olof Johansson 已提交
543 544

	printk(KERN_ERR "pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
545
		macrx, *chan->status);
O
Olof Johansson 已提交
546 547 548 549 550

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

O
Olof Johansson 已提交
551 552
static inline void pasemi_mac_tx_error(const struct pasemi_mac *mac,
				       const u64 mactx)
O
Olof Johansson 已提交
553 554
{
	unsigned int cmdsta;
555
	struct pasemi_dmachan *chan = &tx_ring(mac)->chan;
O
Olof Johansson 已提交
556 557 558 559

	if (!netif_msg_tx_err(mac))
		return;

560
	cmdsta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan->chno));
O
Olof Johansson 已提交
561 562

	printk(KERN_ERR "pasemi_mac: tx error. mactx 0x%016lx, "\
563
		"tx status 0x%016lx\n", mactx, *chan->status);
O
Olof Johansson 已提交
564 565 566 567

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

O
Olof Johansson 已提交
568 569
static int pasemi_mac_clean_rx(struct pasemi_mac_rxring *rx,
			       const int limit)
570
{
O
Olof Johansson 已提交
571
	const struct pasemi_dmachan *chan = &rx->chan;
572
	struct pasemi_mac *mac = rx->mac;
O
Olof Johansson 已提交
573
	struct pci_dev *pdev = mac->dma_pdev;
574
	unsigned int n;
O
Olof Johansson 已提交
575
	int count, buf_index, tot_bytes, packets;
576 577
	struct pasemi_mac_buffer *info;
	struct sk_buff *skb;
578
	unsigned int len;
O
Olof Johansson 已提交
579
	u64 macrx, eval;
580
	dma_addr_t dma;
O
Olof Johansson 已提交
581 582 583

	tot_bytes = 0;
	packets = 0;
584

585
	spin_lock(&rx->lock);
586

587
	n = rx->next_to_clean;
588

589
	prefetch(&RX_DESC(rx, n));
590 591

	for (count = 0; count < limit; count++) {
592
		macrx = RX_DESC(rx, n);
O
Olof Johansson 已提交
593
		prefetch(&RX_DESC(rx, n+4));
594

O
Olof Johansson 已提交
595
		if ((macrx & XCT_MACRX_E) ||
596
		    (*chan->status & PAS_STATUS_ERROR))
O
Olof Johansson 已提交
597 598
			pasemi_mac_rx_error(mac, macrx);

599
		if (!(macrx & XCT_MACRX_O))
600 601 602 603
			break;

		info = NULL;

604
		BUG_ON(!(macrx & XCT_MACRX_RR_8BRES));
605

606
		eval = (RX_DESC(rx, n+1) & XCT_RXRES_8B_EVAL_M) >>
607 608 609
			XCT_RXRES_8B_EVAL_S;
		buf_index = eval-1;

610 611
		dma = (RX_DESC(rx, n+2) & XCT_PTR_ADDR_M);
		info = &RX_DESC_INFO(rx, buf_index);
612

613
		skb = info->skb;
614

O
Olof Johansson 已提交
615
		prefetch_skb(skb);
616

617
		len = (macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
618

O
Olof Johansson 已提交
619 620
		pci_unmap_single(pdev, dma, BUF_SIZE-LOCAL_SKB_ALIGN,
				 PCI_DMA_FROMDEVICE);
O
Olof Johansson 已提交
621 622 623 624 625

		if (macrx & XCT_MACRX_CRC) {
			/* CRC error flagged */
			mac->netdev->stats.rx_errors++;
			mac->netdev->stats.rx_crc_errors++;
626
			/* No need to free skb, it'll be reused */
O
Olof Johansson 已提交
627 628 629
			goto next;
		}

630
		if (len < 256) {
631 632 633 634
			struct sk_buff *new_skb;

			new_skb = netdev_alloc_skb(mac->netdev,
						   len + LOCAL_SKB_ALIGN);
635
			if (new_skb) {
636
				skb_reserve(new_skb, LOCAL_SKB_ALIGN);
637
				memcpy(new_skb->data, skb->data, len);
638 639 640 641 642 643
				/* save the skb in buffer_info as good */
				skb = new_skb;
			}
			/* else just continue with the old one */
		} else
			info->skb = NULL;
644

645
		info->dma = 0;
646

O
Olof Johansson 已提交
647
		if (likely((macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK)) {
O
Olof Johansson 已提交
648
			skb->ip_summed = CHECKSUM_UNNECESSARY;
649
			skb->csum = (macrx & XCT_MACRX_CSUM_M) >>
650 651 652 653
					   XCT_MACRX_CSUM_S;
		} else
			skb->ip_summed = CHECKSUM_NONE;

O
Olof Johansson 已提交
654 655 656 657 658
		packets++;
		tot_bytes += len;

		/* Don't include CRC */
		skb_put(skb, len-4);
659

O
Olof Johansson 已提交
660
		skb->protocol = eth_type_trans(skb, mac->netdev);
661 662
		netif_receive_skb(skb);

O
Olof Johansson 已提交
663
next:
664 665
		RX_DESC(rx, n) = 0;
		RX_DESC(rx, n+1) = 0;
666

667 668 669
		/* Need to zero it out since hardware doesn't, since the
		 * replenish loop uses it to tell when it's done.
		 */
670
		RX_BUFF(rx, buf_index) = 0;
671

672
		n += 4;
673 674
	}

675 676
	if (n > RX_RING_SIZE) {
		/* Errata 5971 workaround: L2 target of headers */
677
		write_iob_reg(PAS_IOB_COM_PKTHDRCNT, 0);
678 679
		n &= (RX_RING_SIZE-1);
	}
680

681
	rx_ring(mac)->next_to_clean = n;
682 683 684 685 686

	/* Increase is in number of 16-byte entries, and since each descriptor
	 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
	 * count*2.
	 */
687
	write_dma_reg(PAS_DMA_RXCHAN_INCR(mac->rx->chan.chno), count << 1);
688 689

	pasemi_mac_replenish_rx_ring(mac->netdev, count);
690

O
Olof Johansson 已提交
691 692 693
	mac->netdev->stats.rx_bytes += tot_bytes;
	mac->netdev->stats.rx_packets += packets;

694
	spin_unlock(&rx_ring(mac)->lock);
695 696 697 698

	return count;
}

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

702
static int pasemi_mac_clean_tx(struct pasemi_mac_txring *txring)
703
{
704
	struct pasemi_dmachan *chan = &txring->chan;
705
	struct pasemi_mac *mac = txring->mac;
O
Olof Johansson 已提交
706
	int i, j;
707 708
	unsigned int start, descr_count, buf_count, batch_limit;
	unsigned int ring_limit;
709
	unsigned int total_count;
710
	unsigned long flags;
O
Olof Johansson 已提交
711 712
	struct sk_buff *skbs[TX_CLEAN_BATCHSIZE];
	dma_addr_t dmas[TX_CLEAN_BATCHSIZE][MAX_SKB_FRAGS+1];
713

714
	total_count = 0;
715
	batch_limit = TX_CLEAN_BATCHSIZE;
716
restart:
717
	spin_lock_irqsave(&txring->lock, flags);
718

719 720
	start = txring->next_to_clean;
	ring_limit = txring->next_to_fill;
721 722 723 724

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

O
Olof Johansson 已提交
726 727
	buf_count = 0;
	descr_count = 0;
728

O
Olof Johansson 已提交
729
	for (i = start;
730
	     descr_count < batch_limit && i < ring_limit;
O
Olof Johansson 已提交
731
	     i += buf_count) {
732
		u64 mactx = TX_DESC(txring, i);
733
		struct sk_buff *skb;
O
Olof Johansson 已提交
734

735
		if ((mactx  & XCT_MACTX_E) ||
736
		    (*chan->status & PAS_STATUS_ERROR))
737
			pasemi_mac_tx_error(mac, mactx);
O
Olof Johansson 已提交
738

739
		if (unlikely(mactx & XCT_MACTX_O))
740
			/* Not yet transmitted */
741 742
			break;

743
		skb = TX_DESC_INFO(txring, i+1).skb;
744
		skbs[descr_count] = skb;
O
Olof Johansson 已提交
745

746 747
		buf_count = 2 + skb_shinfo(skb)->nr_frags;
		for (j = 0; j <= skb_shinfo(skb)->nr_frags; j++)
748
			dmas[descr_count][j] = TX_DESC_INFO(txring, i+1+j).dma;
O
Olof Johansson 已提交
749

750 751
		TX_DESC(txring, i) = 0;
		TX_DESC(txring, i+1) = 0;
752

O
Olof Johansson 已提交
753 754 755 756 757 758
		/* 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++;
759
	}
760
	txring->next_to_clean = i & (TX_RING_SIZE-1);
O
Olof Johansson 已提交
761

762
	spin_unlock_irqrestore(&txring->lock, flags);
763 764
	netif_wake_queue(mac->netdev);

O
Olof Johansson 已提交
765 766
	for (i = 0; i < descr_count; i++)
		pasemi_mac_unmap_tx_skb(mac, skbs[i], dmas[i]);
767

O
Olof Johansson 已提交
768
	total_count += descr_count;
769 770

	/* If the batch was full, try to clean more */
771
	if (descr_count == batch_limit)
772 773 774
		goto restart;

	return total_count;
775 776 777 778 779
}


static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
{
O
Olof Johansson 已提交
780
	const struct pasemi_mac_rxring *rxring = data;
781 782
	struct pasemi_mac *mac = rxring->mac;
	struct net_device *dev = mac->netdev;
O
Olof Johansson 已提交
783
	const struct pasemi_dmachan *chan = &rxring->chan;
784 785
	unsigned int reg;

786
	if (!(*chan->status & PAS_STATUS_CAUSE_M))
787 788
		return IRQ_NONE;

789 790 791 792 793
	/* Don't reset packet count so it won't fire again but clear
	 * all others.
	 */

	reg = 0;
794
	if (*chan->status & PAS_STATUS_SOFT)
795
		reg |= PAS_IOB_DMA_RXCH_RESET_SINTC;
796
	if (*chan->status & PAS_STATUS_ERROR)
797
		reg |= PAS_IOB_DMA_RXCH_RESET_DINTC;
798
	if (*chan->status & PAS_STATUS_TIMER)
799 800
		reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;

801
	netif_rx_schedule(dev, &mac->napi);
802

803
	write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan->chno), reg);
804 805 806 807 808 809

	return IRQ_HANDLED;
}

static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
{
810
	struct pasemi_mac_txring *txring = data;
O
Olof Johansson 已提交
811
	const struct pasemi_dmachan *chan = &txring->chan;
O
Olof Johansson 已提交
812
	unsigned int reg, pcnt;
813

814
	if (!(*chan->status & PAS_STATUS_CAUSE_M))
815 816
		return IRQ_NONE;

817
	pasemi_mac_clean_tx(txring);
818

819
	pcnt = *chan->status & PAS_STATUS_PCNT_M;
O
Olof Johansson 已提交
820 821

	reg = PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt) | PAS_IOB_DMA_TXCH_RESET_PINTC;
822

823
	if (*chan->status & PAS_STATUS_SOFT)
824
		reg |= PAS_IOB_DMA_TXCH_RESET_SINTC;
825
	if (*chan->status & PAS_STATUS_ERROR)
826
		reg |= PAS_IOB_DMA_TXCH_RESET_DINTC;
827

828
	write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan->chno), reg);
829 830 831 832

	return IRQ_HANDLED;
}

O
Olof Johansson 已提交
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
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);

854
	flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
O
Olof Johansson 已提交
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
	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)
886
		write_mac_reg(mac, PAS_MAC_CFG_PCFG, new_flags);
O
Olof Johansson 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904

	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);
905
	ph = of_get_property(dn, "phy-handle", NULL);
O
Olof Johansson 已提交
906 907 908 909
	if (!ph)
		return -ENODEV;
	phy_dn = of_find_node_by_phandle(*ph);

910
	prop = of_get_property(phy_dn, "reg", NULL);
O
Olof Johansson 已提交
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
	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;
}


941 942 943 944 945 946 947
static int pasemi_mac_open(struct net_device *dev)
{
	struct pasemi_mac *mac = netdev_priv(dev);
	unsigned int flags;
	int ret;

	/* enable rx section */
948
	write_dma_reg(PAS_DMA_COM_RXCMD, PAS_DMA_COM_RXCMD_EN);
949 950

	/* enable tx section */
951
	write_dma_reg(PAS_DMA_COM_TXCMD, PAS_DMA_COM_TXCMD_EN);
952 953 954 955 956

	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);

957
	write_mac_reg(mac, PAS_MAC_CFG_TXP, flags);
958

959
	/* 0xffffff is max value, about 16ms */
960 961
	write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG,
		      PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0xffffff));
962 963 964 965 966

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

967
	mac->tx = pasemi_mac_setup_tx_resources(dev);
968 969 970

	if (!mac->tx)
		goto out_tx_ring;
971

972 973 974 975 976 977
	write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac->rx->chan.chno),
		      PAS_IOB_DMA_RXCH_CFG_CNTTH(0));

	write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac->tx->chan.chno),
		      PAS_IOB_DMA_TXCH_CFG_CNTTH(128));

978
	write_mac_reg(mac, PAS_MAC_IPC_CHNL,
979 980
		      PAS_MAC_IPC_CHNL_DCHNO(mac->rx->chan.chno) |
		      PAS_MAC_IPC_CHNL_BCH(mac->rx->chan.chno));
981 982

	/* enable rx if */
983 984 985 986 987 988
	write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
		      PAS_DMA_RXINT_RCMDSTA_EN |
		      PAS_DMA_RXINT_RCMDSTA_DROPS_M |
		      PAS_DMA_RXINT_RCMDSTA_BP |
		      PAS_DMA_RXINT_RCMDSTA_OO |
		      PAS_DMA_RXINT_RCMDSTA_BT);
989 990

	/* enable rx channel */
991 992 993 994
	pasemi_dma_start_chan(&rx_ring(mac)->chan, PAS_DMA_RXCHAN_CCMDSTA_DU |
						   PAS_DMA_RXCHAN_CCMDSTA_OD |
						   PAS_DMA_RXCHAN_CCMDSTA_FD |
						   PAS_DMA_RXCHAN_CCMDSTA_DT);
995 996

	/* enable tx channel */
997 998 999 1000
	pasemi_dma_start_chan(&tx_ring(mac)->chan, PAS_DMA_TXCHAN_TCMDSTA_SZ |
						   PAS_DMA_TXCHAN_TCMDSTA_DB |
						   PAS_DMA_TXCHAN_TCMDSTA_DE |
						   PAS_DMA_TXCHAN_TCMDSTA_DA);
1001

1002
	pasemi_mac_replenish_rx_ring(dev, RX_RING_SIZE);
1003

1004 1005
	write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac)->chan.chno),
		      RX_RING_SIZE>>1);
1006

1007 1008 1009 1010
	/* Clear out any residual packet count state from firmware */
	pasemi_mac_restart_rx_intr(mac);
	pasemi_mac_restart_tx_intr(mac);

1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
	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 已提交
1022 1023 1024 1025 1026 1027 1028
	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);

1029
	netif_start_queue(dev);
1030
	napi_enable(&mac->napi);
1031

1032 1033
	snprintf(mac->tx_irq_name, sizeof(mac->tx_irq_name), "%s tx",
		 dev->name);
1034

1035
	ret = request_irq(mac->tx->chan.irq, &pasemi_mac_tx_intr, IRQF_DISABLED,
1036
			  mac->tx_irq_name, mac->tx);
1037 1038
	if (ret) {
		dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
1039
			mac->tx->chan.irq, ret);
1040 1041 1042
		goto out_tx_int;
	}

1043 1044 1045
	snprintf(mac->rx_irq_name, sizeof(mac->rx_irq_name), "%s rx",
		 dev->name);

1046 1047
	ret = request_irq(mac->rx->chan.irq, &pasemi_mac_rx_intr, IRQF_DISABLED,
			  mac->rx_irq_name, mac->rx);
1048 1049
	if (ret) {
		dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
1050
			mac->rx->chan.irq, ret);
1051 1052 1053
		goto out_rx_int;
	}

O
Olof Johansson 已提交
1054 1055 1056
	if (mac->phydev)
		phy_start(mac->phydev);

1057 1058 1059
	return 0;

out_rx_int:
1060
	free_irq(mac->tx->chan.irq, mac->tx);
1061
out_tx_int:
1062
	napi_disable(&mac->napi);
1063
	netif_stop_queue(dev);
1064 1065 1066 1067
out_tx_ring:
	if (mac->tx)
		pasemi_mac_free_tx_resources(mac);
	pasemi_mac_free_rx_resources(mac);
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
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);
1078
	unsigned int sta;
1079
	int retries;
1080 1081 1082 1083
	int rxch, txch;

	rxch = rx_ring(mac)->chan.chno;
	txch = tx_ring(mac)->chan.chno;
1084

O
Olof Johansson 已提交
1085 1086 1087 1088 1089
	if (mac->phydev) {
		phy_stop(mac->phydev);
		phy_disconnect(mac->phydev);
	}

1090
	netif_stop_queue(dev);
1091
	napi_disable(&mac->napi);
1092

1093
	sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1094 1095 1096 1097 1098
	if (sta & (PAS_DMA_RXINT_RCMDSTA_BP |
		      PAS_DMA_RXINT_RCMDSTA_OO |
		      PAS_DMA_RXINT_RCMDSTA_BT))
		printk(KERN_DEBUG "pasemi_mac: rcmdsta error: 0x%08x\n", sta);

1099
	sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
1100 1101 1102 1103 1104 1105
	if (sta & (PAS_DMA_RXCHAN_CCMDSTA_DU |
		     PAS_DMA_RXCHAN_CCMDSTA_OD |
		     PAS_DMA_RXCHAN_CCMDSTA_FD |
		     PAS_DMA_RXCHAN_CCMDSTA_DT))
		printk(KERN_DEBUG "pasemi_mac: ccmdsta error: 0x%08x\n", sta);

1106
	sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch));
1107 1108
	if (sta & (PAS_DMA_TXCHAN_TCMDSTA_SZ | PAS_DMA_TXCHAN_TCMDSTA_DB |
		      PAS_DMA_TXCHAN_TCMDSTA_DE | PAS_DMA_TXCHAN_TCMDSTA_DA))
1109 1110
		printk(KERN_DEBUG "pasemi_mac: tcmdsta error: 0x%08x\n", sta);

1111
	/* Clean out any pending buffers */
1112 1113
	pasemi_mac_clean_tx(tx_ring(mac));
	pasemi_mac_clean_rx(rx_ring(mac), RX_RING_SIZE);
1114 1115

	/* Disable interface */
1116
	write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch),
1117
		      PAS_DMA_TXCHAN_TCMDSTA_ST);
1118
	write_dma_reg( PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
1119
		      PAS_DMA_RXINT_RCMDSTA_ST);
1120
	write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch),
1121
		      PAS_DMA_RXCHAN_CCMDSTA_ST);
1122 1123

	for (retries = 0; retries < MAX_RETRIES; retries++) {
1124
		sta = read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(rxch));
1125
		if (!(sta & PAS_DMA_TXCHAN_TCMDSTA_ACT))
1126 1127 1128 1129
			break;
		cond_resched();
	}

1130
	if (sta & PAS_DMA_TXCHAN_TCMDSTA_ACT)
1131
		dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
1132 1133

	for (retries = 0; retries < MAX_RETRIES; retries++) {
1134
		sta = read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch));
1135
		if (!(sta & PAS_DMA_RXCHAN_CCMDSTA_ACT))
1136 1137 1138 1139
			break;
		cond_resched();
	}

1140
	if (sta & PAS_DMA_RXCHAN_CCMDSTA_ACT)
1141 1142 1143
		dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");

	for (retries = 0; retries < MAX_RETRIES; retries++) {
1144
		sta = read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if));
1145
		if (!(sta & PAS_DMA_RXINT_RCMDSTA_ACT))
1146 1147 1148 1149
			break;
		cond_resched();
	}

1150
	if (sta & PAS_DMA_RXINT_RCMDSTA_ACT)
1151 1152 1153 1154 1155 1156
		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.
	 */

1157 1158 1159
	write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch), 0);
	write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch), 0);
	write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
1160

1161 1162
	free_irq(mac->tx->chan.irq, mac->tx);
	free_irq(mac->rx->chan.irq, mac->rx);
1163 1164

	/* Free resources */
1165 1166
	pasemi_mac_free_rx_resources(mac);
	pasemi_mac_free_tx_resources(mac);
1167 1168 1169 1170 1171 1172 1173 1174

	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 已提交
1175 1176 1177
	u64 dflags, mactx;
	dma_addr_t map[MAX_SKB_FRAGS+1];
	unsigned int map_size[MAX_SKB_FRAGS+1];
1178
	unsigned long flags;
O
Olof Johansson 已提交
1179
	int i, nfrags;
O
Olof Johansson 已提交
1180
	int fill;
1181

1182
	dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_CRC_PAD;
1183 1184

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

1187
		switch (ip_hdr(skb)->protocol) {
1188 1189
		case IPPROTO_TCP:
			dflags |= XCT_MACTX_CSUM_TCP;
1190
			dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
1191
			dflags |= XCT_MACTX_IPO(nh - skb->data);
1192 1193 1194
			break;
		case IPPROTO_UDP:
			dflags |= XCT_MACTX_CSUM_UDP;
1195
			dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
1196
			dflags |= XCT_MACTX_IPO(nh - skb->data);
1197 1198 1199 1200
			break;
		}
	}

O
Olof Johansson 已提交
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
	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];
1211

O
Olof Johansson 已提交
1212 1213 1214 1215 1216 1217 1218 1219 1220
		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;
		}
	}
1221

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

1224
	txring = tx_ring(mac);
1225 1226 1227

	spin_lock_irqsave(&txring->lock, flags);

O
Olof Johansson 已提交
1228 1229
	fill = txring->next_to_fill;

1230 1231 1232 1233 1234 1235 1236 1237
	/* 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;
1238 1239
	}

O
Olof Johansson 已提交
1240 1241 1242
	TX_DESC(txring, fill) = mactx;
	fill++;
	TX_DESC_INFO(txring, fill).skb = skb;
O
Olof Johansson 已提交
1243
	for (i = 0; i <= nfrags; i++) {
O
Olof Johansson 已提交
1244
		TX_DESC(txring, fill+i) =
1245
			XCT_PTR_LEN(map_size[i]) | XCT_PTR_ADDR(map[i]);
O
Olof Johansson 已提交
1246
		TX_DESC_INFO(txring, fill+i).dma = map[i];
O
Olof Johansson 已提交
1247 1248 1249 1250 1251 1252 1253 1254
	}

	/* 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++;
1255

O
Olof Johansson 已提交
1256
	txring->next_to_fill = (fill + nfrags + 1) & (TX_RING_SIZE-1);
1257

1258 1259
	dev->stats.tx_packets++;
	dev->stats.tx_bytes += skb->len;
1260 1261 1262

	spin_unlock_irqrestore(&txring->lock, flags);

1263
	write_dma_reg(PAS_DMA_TXCHAN_INCR(txring->chan.chno), (nfrags+2) >> 1);
1264 1265 1266 1267 1268

	return NETDEV_TX_OK;

out_err:
	spin_unlock_irqrestore(&txring->lock, flags);
O
Olof Johansson 已提交
1269 1270 1271 1272 1273
out_err_nolock:
	while (nfrags--)
		pci_unmap_single(mac->dma_pdev, map[nfrags], map_size[nfrags],
				 PCI_DMA_TODEVICE);

1274 1275 1276 1277 1278
	return NETDEV_TX_BUSY;
}

static void pasemi_mac_set_rx_mode(struct net_device *dev)
{
O
Olof Johansson 已提交
1279
	const struct pasemi_mac *mac = netdev_priv(dev);
1280 1281
	unsigned int flags;

1282
	flags = read_mac_reg(mac, PAS_MAC_CFG_PCFG);
1283 1284 1285 1286 1287 1288 1289

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

1290
	write_mac_reg(mac, PAS_MAC_CFG_PCFG, flags);
1291 1292 1293
}


1294
static int pasemi_mac_poll(struct napi_struct *napi, int budget)
1295
{
1296 1297 1298
	struct pasemi_mac *mac = container_of(napi, struct pasemi_mac, napi);
	struct net_device *dev = mac->netdev;
	int pkts;
1299

1300 1301
	pasemi_mac_clean_tx(tx_ring(mac));
	pkts = pasemi_mac_clean_rx(rx_ring(mac), budget);
1302
	if (pkts < budget) {
1303
		/* all done, no more packets present */
1304
		netif_rx_complete(dev, napi);
1305

1306
		pasemi_mac_restart_rx_intr(mac);
1307
	}
1308
	return pkts;
1309 1310 1311 1312 1313 1314 1315 1316
}

static int __devinit
pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	struct net_device *dev;
	struct pasemi_mac *mac;
	int err;
1317
	DECLARE_MAC_BUF(mac_buf);
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338

	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;

1339 1340
	netif_napi_add(dev, &mac->napi, pasemi_mac_poll, 64);

O
Olof Johansson 已提交
1341 1342
	dev->features = NETIF_F_IP_CSUM | NETIF_F_LLTX | NETIF_F_SG |
			NETIF_F_HIGHDMA;
1343

1344 1345 1346 1347 1348 1349
	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");
		err = -ENODEV;
		goto out;
	}
1350

1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
	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");
		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));

	mac->dma_if = mac_to_intf(mac);
	if (mac->dma_if < 0) {
		dev_err(&mac->pdev->dev, "Can't map DMA interface\n");
		err = -ENODEV;
		goto out;
	}
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388

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

	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;

1389 1390
	if (err)
		goto out;
1391

1392 1393
	mac->msg_enable = netif_msg_init(debug, DEFAULT_MSG_ENABLE);

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

1397 1398 1399 1400 1401 1402
	err = register_netdev(dev);

	if (err) {
		dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
			err);
		goto out;
O
Olof Johansson 已提交
1403
	} else if netif_msg_probe(mac)
1404
		printk(KERN_INFO "%s: PA Semi %s: intf %d, hw addr %s\n",
1405
		       dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
1406
		       mac->dma_if, print_mac(mac_buf, dev->dev_addr));
1407 1408 1409 1410

	return err;

out:
1411 1412 1413 1414 1415
	if (mac->iob_pdev)
		pci_dev_put(mac->iob_pdev);
	if (mac->dma_pdev)
		pci_dev_put(mac->dma_pdev);

1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
	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);

1439 1440
	pasemi_dma_free_chan(&mac->tx->chan);
	pasemi_dma_free_chan(&mac->rx->chan);
1441

1442 1443 1444 1445 1446 1447 1448
	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) },
1449
	{ },
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467
};

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);
}

int pasemi_mac_init_module(void)
{
1468 1469 1470 1471 1472 1473
	int err;

	err = pasemi_dma_init();
	if (err)
		return err;

1474 1475 1476 1477 1478
	return pci_register_driver(&pasemi_mac_driver);
}

module_init(pasemi_mac_init_module);
module_exit(pasemi_mac_cleanup_module);