blob: bde833542b6f7588b72b093f475f7e4ff11fd1cc [file] [log] [blame]
Olof Johanssonf5cd7872007-01-31 21:43:54 -06001/*
2 * Copyright (C) 2006-2007 PA Semi, Inc
3 *
4 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/interrupt.h>
24#include <linux/dmaengine.h>
25#include <linux/delay.h>
26#include <linux/netdevice.h>
27#include <linux/etherdevice.h>
28#include <asm/dma-mapping.h>
29#include <linux/in.h>
30#include <linux/skbuff.h>
31
32#include <linux/ip.h>
33#include <linux/tcp.h>
34#include <net/checksum.h>
35
36#include "pasemi_mac.h"
37
38
39/* TODO list
40 *
41 * - Get rid of pci_{read,write}_config(), map registers with ioremap
42 * for performance
43 * - PHY support
44 * - Multicast support
45 * - Large MTU support
46 * - Other performance improvements
47 */
48
49
50/* Must be a power of two */
51#define RX_RING_SIZE 512
52#define TX_RING_SIZE 512
53
54#define TX_DESC(mac, num) ((mac)->tx->desc[(num) & (TX_RING_SIZE-1)])
55#define TX_DESC_INFO(mac, num) ((mac)->tx->desc_info[(num) & (TX_RING_SIZE-1)])
56#define RX_DESC(mac, num) ((mac)->rx->desc[(num) & (RX_RING_SIZE-1)])
57#define RX_DESC_INFO(mac, num) ((mac)->rx->desc_info[(num) & (RX_RING_SIZE-1)])
58#define RX_BUFF(mac, num) ((mac)->rx->buffers[(num) & (RX_RING_SIZE-1)])
59
60#define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
61
62/* XXXOJN these should come out of the device tree some day */
63#define PAS_DMA_CAP_BASE 0xe00d0040
64#define PAS_DMA_CAP_SIZE 0x100
65#define PAS_DMA_COM_BASE 0xe00d0100
66#define PAS_DMA_COM_SIZE 0x100
67
68static struct pasdma_status *dma_status;
69
70static int pasemi_get_mac_addr(struct pasemi_mac *mac)
71{
72 struct pci_dev *pdev = mac->pdev;
73 struct device_node *dn = pci_device_to_OF_node(pdev);
74 const u8 *maddr;
75 u8 addr[6];
76
77 if (!dn) {
78 dev_dbg(&pdev->dev,
79 "No device node for mac, not configuring\n");
80 return -ENOENT;
81 }
82
83 maddr = get_property(dn, "mac-address", NULL);
84 if (maddr == NULL) {
85 dev_warn(&pdev->dev,
86 "no mac address in device tree, not configuring\n");
87 return -ENOENT;
88 }
89
90 if (sscanf(maddr, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr[0],
91 &addr[1], &addr[2], &addr[3], &addr[4], &addr[5]) != 6) {
92 dev_warn(&pdev->dev,
93 "can't parse mac address, not configuring\n");
94 return -EINVAL;
95 }
96
97 memcpy(mac->mac_addr, addr, sizeof(addr));
98 return 0;
99}
100
101static int pasemi_mac_setup_rx_resources(struct net_device *dev)
102{
103 struct pasemi_mac_rxring *ring;
104 struct pasemi_mac *mac = netdev_priv(dev);
105 int chan_id = mac->dma_rxch;
106
107 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
108
109 if (!ring)
110 goto out_ring;
111
112 spin_lock_init(&ring->lock);
113
114 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
115 RX_RING_SIZE, GFP_KERNEL);
116
117 if (!ring->desc_info)
118 goto out_desc_info;
119
120 /* Allocate descriptors */
121 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
122 RX_RING_SIZE *
123 sizeof(struct pas_dma_xct_descr),
124 &ring->dma, GFP_KERNEL);
125
126 if (!ring->desc)
127 goto out_desc;
128
129 memset(ring->desc, 0, RX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
130
131 ring->buffers = dma_alloc_coherent(&mac->dma_pdev->dev,
132 RX_RING_SIZE * sizeof(u64),
133 &ring->buf_dma, GFP_KERNEL);
134 if (!ring->buffers)
135 goto out_buffers;
136
137 memset(ring->buffers, 0, RX_RING_SIZE * sizeof(u64));
138
139 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEL(chan_id),
140 PAS_DMA_RXCHAN_BASEL_BRBL(ring->dma));
141
142 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_BASEU(chan_id),
143 PAS_DMA_RXCHAN_BASEU_BRBH(ring->dma >> 32) |
144 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE >> 2));
145
146 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXCHAN_CFG(chan_id),
147 PAS_DMA_RXCHAN_CFG_HBU(1));
148
149 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEL(mac->dma_if),
150 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring->buffers)));
151
152 pci_write_config_dword(mac->dma_pdev, PAS_DMA_RXINT_BASEU(mac->dma_if),
153 PAS_DMA_RXINT_BASEU_BRBH(__pa(ring->buffers) >> 32) |
154 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE >> 3));
155
156 ring->next_to_fill = 0;
157 ring->next_to_clean = 0;
158
159 snprintf(ring->irq_name, sizeof(ring->irq_name),
160 "%s rx", dev->name);
161 mac->rx = ring;
162
163 return 0;
164
165out_buffers:
166 dma_free_coherent(&mac->dma_pdev->dev,
167 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
168 mac->rx->desc, mac->rx->dma);
169out_desc:
170 kfree(ring->desc_info);
171out_desc_info:
172 kfree(ring);
173out_ring:
174 return -ENOMEM;
175}
176
177
178static int pasemi_mac_setup_tx_resources(struct net_device *dev)
179{
180 struct pasemi_mac *mac = netdev_priv(dev);
181 u32 val;
182 int chan_id = mac->dma_txch;
183 struct pasemi_mac_txring *ring;
184
185 ring = kzalloc(sizeof(*ring), GFP_KERNEL);
186 if (!ring)
187 goto out_ring;
188
189 spin_lock_init(&ring->lock);
190
191 ring->desc_info = kzalloc(sizeof(struct pasemi_mac_buffer) *
192 TX_RING_SIZE, GFP_KERNEL);
193 if (!ring->desc_info)
194 goto out_desc_info;
195
196 /* Allocate descriptors */
197 ring->desc = dma_alloc_coherent(&mac->dma_pdev->dev,
198 TX_RING_SIZE *
199 sizeof(struct pas_dma_xct_descr),
200 &ring->dma, GFP_KERNEL);
201 if (!ring->desc)
202 goto out_desc;
203
204 memset(ring->desc, 0, TX_RING_SIZE * sizeof(struct pas_dma_xct_descr));
205
206 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEL(chan_id),
207 PAS_DMA_TXCHAN_BASEL_BRBL(ring->dma));
208 val = PAS_DMA_TXCHAN_BASEU_BRBH(ring->dma >> 32);
209 val |= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE >> 2);
210
211 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_BASEU(chan_id), val);
212
213 pci_write_config_dword(mac->dma_pdev, PAS_DMA_TXCHAN_CFG(chan_id),
214 PAS_DMA_TXCHAN_CFG_TY_IFACE |
215 PAS_DMA_TXCHAN_CFG_TATTR(mac->dma_if) |
216 PAS_DMA_TXCHAN_CFG_UP |
217 PAS_DMA_TXCHAN_CFG_WT(2));
218
219 ring->next_to_use = 0;
220 ring->next_to_clean = 0;
221
222 snprintf(ring->irq_name, sizeof(ring->irq_name),
223 "%s tx", dev->name);
224 mac->tx = ring;
225
226 return 0;
227
228out_desc:
229 kfree(ring->desc_info);
230out_desc_info:
231 kfree(ring);
232out_ring:
233 return -ENOMEM;
234}
235
236static void pasemi_mac_free_tx_resources(struct net_device *dev)
237{
238 struct pasemi_mac *mac = netdev_priv(dev);
239 unsigned int i;
240 struct pasemi_mac_buffer *info;
241 struct pas_dma_xct_descr *dp;
242
243 for (i = 0; i < TX_RING_SIZE; i++) {
244 info = &TX_DESC_INFO(mac, i);
245 dp = &TX_DESC(mac, i);
246 if (info->dma) {
247 if (info->skb) {
248 pci_unmap_single(mac->dma_pdev,
249 info->dma,
250 info->skb->len,
251 PCI_DMA_TODEVICE);
252 dev_kfree_skb_any(info->skb);
253 }
254 info->dma = 0;
255 info->skb = NULL;
256 dp->mactx = 0;
257 dp->ptr = 0;
258 }
259 }
260
261 dma_free_coherent(&mac->dma_pdev->dev,
262 TX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
263 mac->tx->desc, mac->tx->dma);
264
265 kfree(mac->tx->desc_info);
266 kfree(mac->tx);
267 mac->tx = NULL;
268}
269
270static void pasemi_mac_free_rx_resources(struct net_device *dev)
271{
272 struct pasemi_mac *mac = netdev_priv(dev);
273 unsigned int i;
274 struct pasemi_mac_buffer *info;
275 struct pas_dma_xct_descr *dp;
276
277 for (i = 0; i < RX_RING_SIZE; i++) {
278 info = &RX_DESC_INFO(mac, i);
279 dp = &RX_DESC(mac, i);
280 if (info->dma) {
281 if (info->skb) {
282 pci_unmap_single(mac->dma_pdev,
283 info->dma,
284 info->skb->len,
285 PCI_DMA_FROMDEVICE);
286 dev_kfree_skb_any(info->skb);
287 }
288 info->dma = 0;
289 info->skb = NULL;
290 dp->macrx = 0;
291 dp->ptr = 0;
292 }
293 }
294
295 dma_free_coherent(&mac->dma_pdev->dev,
296 RX_RING_SIZE * sizeof(struct pas_dma_xct_descr),
297 mac->rx->desc, mac->rx->dma);
298
299 dma_free_coherent(&mac->dma_pdev->dev, RX_RING_SIZE * sizeof(u64),
300 mac->rx->buffers, mac->rx->buf_dma);
301
302 kfree(mac->rx->desc_info);
303 kfree(mac->rx);
304 mac->rx = NULL;
305}
306
307static void pasemi_mac_replenish_rx_ring(struct net_device *dev)
308{
309 struct pasemi_mac *mac = netdev_priv(dev);
310 unsigned int i;
311 int start = mac->rx->next_to_fill;
312 unsigned int count;
313
314 count = (mac->rx->next_to_clean + RX_RING_SIZE -
315 mac->rx->next_to_fill) & (RX_RING_SIZE - 1);
316
317 /* Check to see if we're doing first-time setup */
318 if (unlikely(mac->rx->next_to_clean == 0 && mac->rx->next_to_fill == 0))
319 count = RX_RING_SIZE;
320
321 if (count <= 0)
322 return;
323
324 for (i = start; i < start + count; i++) {
325 struct pasemi_mac_buffer *info = &RX_DESC_INFO(mac, i);
326 u64 *buff = &RX_BUFF(mac, i);
327 struct sk_buff *skb;
328 dma_addr_t dma;
329
330 skb = dev_alloc_skb(BUF_SIZE);
331
332 if (!skb) {
333 count = i - start;
334 break;
335 }
336
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600337 dma = pci_map_single(mac->dma_pdev, skb->data, skb->len,
338 PCI_DMA_FROMDEVICE);
339
340 if (dma_mapping_error(dma)) {
341 dev_kfree_skb_irq(info->skb);
342 count = i - start;
343 break;
344 }
345
346 info->skb = skb;
347 info->dma = dma;
348 *buff = XCT_RXB_LEN(BUF_SIZE) | XCT_RXB_ADDR(dma);
349 }
350
351 wmb();
352
353 pci_write_config_dword(mac->dma_pdev,
354 PAS_DMA_RXCHAN_INCR(mac->dma_rxch),
355 count);
356 pci_write_config_dword(mac->dma_pdev,
357 PAS_DMA_RXINT_INCR(mac->dma_if),
358 count);
359
360 mac->rx->next_to_fill += count;
361}
362
363static int pasemi_mac_clean_rx(struct pasemi_mac *mac, int limit)
364{
365 unsigned int i;
366 int start, count;
367
368 spin_lock(&mac->rx->lock);
369
370 start = mac->rx->next_to_clean;
371 count = 0;
372
373 for (i = start; i < (start + RX_RING_SIZE) && count < limit; i++) {
374 struct pas_dma_xct_descr *dp;
375 struct pasemi_mac_buffer *info;
376 struct sk_buff *skb;
377 unsigned int j, len;
378 dma_addr_t dma;
379
380 rmb();
381
382 dp = &RX_DESC(mac, i);
383
384 if (!(dp->macrx & XCT_MACRX_O))
385 break;
386
387 count++;
388
389 info = NULL;
390
391 /* We have to scan for our skb since there's no way
392 * to back-map them from the descriptor, and if we
393 * have several receive channels then they might not
394 * show up in the same order as they were put on the
395 * interface ring.
396 */
397
398 dma = (dp->ptr & XCT_PTR_ADDR_M);
399 for (j = start; j < (start + RX_RING_SIZE); j++) {
400 info = &RX_DESC_INFO(mac, j);
401 if (info->dma == dma)
402 break;
403 }
404
405 BUG_ON(!info);
406 BUG_ON(info->dma != dma);
407
408 pci_unmap_single(mac->dma_pdev, info->dma, info->skb->len,
409 PCI_DMA_FROMDEVICE);
410
411 skb = info->skb;
412
413 len = (dp->macrx & XCT_MACRX_LLEN_M) >> XCT_MACRX_LLEN_S;
414
415 skb_put(skb, len);
416
417 skb->protocol = eth_type_trans(skb, mac->netdev);
418
419 if ((dp->macrx & XCT_MACRX_HTY_M) == XCT_MACRX_HTY_IPV4_OK) {
420 skb->ip_summed = CHECKSUM_COMPLETE;
421 skb->csum = (dp->macrx & XCT_MACRX_CSUM_M) >>
422 XCT_MACRX_CSUM_S;
423 } else
424 skb->ip_summed = CHECKSUM_NONE;
425
426 mac->stats.rx_bytes += len;
427 mac->stats.rx_packets++;
428
429 netif_receive_skb(skb);
430
431 info->dma = 0;
432 info->skb = NULL;
433 dp->ptr = 0;
434 dp->macrx = 0;
435 }
436
437 mac->rx->next_to_clean += count;
438 pasemi_mac_replenish_rx_ring(mac->netdev);
439
440 spin_unlock(&mac->rx->lock);
441
442 return count;
443}
444
445static int pasemi_mac_clean_tx(struct pasemi_mac *mac)
446{
447 int i;
448 struct pasemi_mac_buffer *info;
449 struct pas_dma_xct_descr *dp;
450 int start, count;
451 int flags;
452
453 spin_lock_irqsave(&mac->tx->lock, flags);
454
455 start = mac->tx->next_to_clean;
456 count = 0;
457
458 for (i = start; i < mac->tx->next_to_use; i++) {
459 dp = &TX_DESC(mac, i);
460 if (!dp || (dp->mactx & XCT_MACTX_O))
461 break;
462
463 count++;
464
465 info = &TX_DESC_INFO(mac, i);
466
467 pci_unmap_single(mac->dma_pdev, info->dma,
468 info->skb->len, PCI_DMA_TODEVICE);
469 dev_kfree_skb_irq(info->skb);
470
471 info->skb = NULL;
472 info->dma = 0;
473 dp->mactx = 0;
474 dp->ptr = 0;
475 }
476 mac->tx->next_to_clean += count;
477 spin_unlock_irqrestore(&mac->tx->lock, flags);
478
Olof Johansson0ce68c72007-04-28 15:36:40 -0500479 netif_wake_queue(mac->netdev);
480
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600481 return count;
482}
483
484
485static irqreturn_t pasemi_mac_rx_intr(int irq, void *data)
486{
487 struct net_device *dev = data;
488 struct pasemi_mac *mac = netdev_priv(dev);
489 unsigned int reg;
490
491 if (!(*mac->rx_status & PAS_STATUS_INT))
492 return IRQ_NONE;
493
494 netif_rx_schedule(dev);
495 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
496 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0));
497
498 reg = PAS_IOB_DMA_RXCH_RESET_PINTC | PAS_IOB_DMA_RXCH_RESET_SINTC |
499 PAS_IOB_DMA_RXCH_RESET_DINTC;
500 if (*mac->rx_status & PAS_STATUS_TIMER)
501 reg |= PAS_IOB_DMA_RXCH_RESET_TINTC;
502
503 pci_write_config_dword(mac->iob_pdev,
504 PAS_IOB_DMA_RXCH_RESET(mac->dma_rxch), reg);
505
506
507 return IRQ_HANDLED;
508}
509
510static irqreturn_t pasemi_mac_tx_intr(int irq, void *data)
511{
512 struct net_device *dev = data;
513 struct pasemi_mac *mac = netdev_priv(dev);
514 unsigned int reg;
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600515
516 if (!(*mac->tx_status & PAS_STATUS_INT))
517 return IRQ_NONE;
518
519 pasemi_mac_clean_tx(mac);
520
521 reg = PAS_IOB_DMA_TXCH_RESET_PINTC | PAS_IOB_DMA_TXCH_RESET_SINTC;
522 if (*mac->tx_status & PAS_STATUS_TIMER)
523 reg |= PAS_IOB_DMA_TXCH_RESET_TINTC;
524
525 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_TXCH_RESET(mac->dma_txch),
526 reg);
527
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600528 return IRQ_HANDLED;
529}
530
531static int pasemi_mac_open(struct net_device *dev)
532{
533 struct pasemi_mac *mac = netdev_priv(dev);
534 unsigned int flags;
535 int ret;
536
537 /* enable rx section */
538 pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_RXCMD,
539 PAS_DMA_COM_RXCMD_EN);
540
541 /* enable tx section */
542 pci_write_config_dword(mac->dma_pdev, PAS_DMA_COM_TXCMD,
543 PAS_DMA_COM_TXCMD_EN);
544
545 flags = PAS_MAC_CFG_TXP_FCE | PAS_MAC_CFG_TXP_FPC(3) |
546 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
547 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
548
549 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_TXP, flags);
550
551 flags = PAS_MAC_CFG_PCFG_S1 | PAS_MAC_CFG_PCFG_PE |
552 PAS_MAC_CFG_PCFG_PR | PAS_MAC_CFG_PCFG_CE;
553
554 flags |= PAS_MAC_CFG_PCFG_TSR_1G | PAS_MAC_CFG_PCFG_SPD_1G;
555
556 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_RXCH_CFG(mac->dma_rxch),
557 PAS_IOB_DMA_RXCH_CFG_CNTTH(30));
558
559 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
560 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(1000000));
561
562 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
563
564 ret = pasemi_mac_setup_rx_resources(dev);
565 if (ret)
566 goto out_rx_resources;
567
568 ret = pasemi_mac_setup_tx_resources(dev);
569 if (ret)
570 goto out_tx_resources;
571
572 pci_write_config_dword(mac->pdev, PAS_MAC_IPC_CHNL,
573 PAS_MAC_IPC_CHNL_DCHNO(mac->dma_rxch) |
574 PAS_MAC_IPC_CHNL_BCH(mac->dma_rxch));
575
576 /* enable rx if */
577 pci_write_config_dword(mac->dma_pdev,
578 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
579 PAS_DMA_RXINT_RCMDSTA_EN);
580
581 /* enable rx channel */
582 pci_write_config_dword(mac->dma_pdev,
583 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
584 PAS_DMA_RXCHAN_CCMDSTA_EN |
585 PAS_DMA_RXCHAN_CCMDSTA_DU);
586
587 /* enable tx channel */
588 pci_write_config_dword(mac->dma_pdev,
589 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
590 PAS_DMA_TXCHAN_TCMDSTA_EN);
591
592 pasemi_mac_replenish_rx_ring(dev);
593
594 netif_start_queue(dev);
595 netif_poll_enable(dev);
596
597 ret = request_irq(mac->dma_pdev->irq + mac->dma_txch,
598 &pasemi_mac_tx_intr, IRQF_DISABLED,
599 mac->tx->irq_name, dev);
600 if (ret) {
601 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
602 mac->dma_pdev->irq + mac->dma_txch, ret);
603 goto out_tx_int;
604 }
605
606 ret = request_irq(mac->dma_pdev->irq + 20 + mac->dma_rxch,
607 &pasemi_mac_rx_intr, IRQF_DISABLED,
608 mac->rx->irq_name, dev);
609 if (ret) {
610 dev_err(&mac->pdev->dev, "request_irq of irq %d failed: %d\n",
611 mac->dma_pdev->irq + 20 + mac->dma_rxch, ret);
612 goto out_rx_int;
613 }
614
615 return 0;
616
617out_rx_int:
618 free_irq(mac->dma_pdev->irq + mac->dma_txch, dev);
619out_tx_int:
620 netif_poll_disable(dev);
621 netif_stop_queue(dev);
622 pasemi_mac_free_tx_resources(dev);
623out_tx_resources:
624 pasemi_mac_free_rx_resources(dev);
625out_rx_resources:
626
627 return ret;
628}
629
630#define MAX_RETRIES 5000
631
632static int pasemi_mac_close(struct net_device *dev)
633{
634 struct pasemi_mac *mac = netdev_priv(dev);
635 unsigned int stat;
636 int retries;
637
638 netif_stop_queue(dev);
639
640 /* Clean out any pending buffers */
641 pasemi_mac_clean_tx(mac);
642 pasemi_mac_clean_rx(mac, RX_RING_SIZE);
643
644 /* Disable interface */
645 pci_write_config_dword(mac->dma_pdev,
646 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
647 PAS_DMA_TXCHAN_TCMDSTA_ST);
648 pci_write_config_dword(mac->dma_pdev,
649 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
650 PAS_DMA_RXINT_RCMDSTA_ST);
651 pci_write_config_dword(mac->dma_pdev,
652 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
653 PAS_DMA_RXCHAN_CCMDSTA_ST);
654
655 for (retries = 0; retries < MAX_RETRIES; retries++) {
656 pci_read_config_dword(mac->dma_pdev,
657 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch),
658 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500659 if (!(stat & PAS_DMA_TXCHAN_TCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600660 break;
661 cond_resched();
662 }
663
Olof Johansson0ce68c72007-04-28 15:36:40 -0500664 if (stat & PAS_DMA_TXCHAN_TCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600665 dev_err(&mac->dma_pdev->dev, "Failed to stop tx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600666
667 for (retries = 0; retries < MAX_RETRIES; retries++) {
668 pci_read_config_dword(mac->dma_pdev,
669 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch),
670 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500671 if (!(stat & PAS_DMA_RXCHAN_CCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600672 break;
673 cond_resched();
674 }
675
Olof Johansson0ce68c72007-04-28 15:36:40 -0500676 if (stat & PAS_DMA_RXCHAN_CCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600677 dev_err(&mac->dma_pdev->dev, "Failed to stop rx channel\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600678
679 for (retries = 0; retries < MAX_RETRIES; retries++) {
680 pci_read_config_dword(mac->dma_pdev,
681 PAS_DMA_RXINT_RCMDSTA(mac->dma_if),
682 &stat);
Olof Johansson0ce68c72007-04-28 15:36:40 -0500683 if (!(stat & PAS_DMA_RXINT_RCMDSTA_ACT))
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600684 break;
685 cond_resched();
686 }
687
Olof Johansson0ce68c72007-04-28 15:36:40 -0500688 if (stat & PAS_DMA_RXINT_RCMDSTA_ACT)
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600689 dev_err(&mac->dma_pdev->dev, "Failed to stop rx interface\n");
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600690
691 /* Then, disable the channel. This must be done separately from
692 * stopping, since you can't disable when active.
693 */
694
695 pci_write_config_dword(mac->dma_pdev,
696 PAS_DMA_TXCHAN_TCMDSTA(mac->dma_txch), 0);
697 pci_write_config_dword(mac->dma_pdev,
698 PAS_DMA_RXCHAN_CCMDSTA(mac->dma_rxch), 0);
699 pci_write_config_dword(mac->dma_pdev,
700 PAS_DMA_RXINT_RCMDSTA(mac->dma_if), 0);
701
702 free_irq(mac->dma_pdev->irq + mac->dma_txch, dev);
703 free_irq(mac->dma_pdev->irq + 20 + mac->dma_rxch, dev);
704
705 /* Free resources */
706 pasemi_mac_free_rx_resources(dev);
707 pasemi_mac_free_tx_resources(dev);
708
709 return 0;
710}
711
712static int pasemi_mac_start_tx(struct sk_buff *skb, struct net_device *dev)
713{
714 struct pasemi_mac *mac = netdev_priv(dev);
715 struct pasemi_mac_txring *txring;
716 struct pasemi_mac_buffer *info;
717 struct pas_dma_xct_descr *dp;
718 u64 dflags;
719 dma_addr_t map;
720 int flags;
721
722 dflags = XCT_MACTX_O | XCT_MACTX_ST | XCT_MACTX_SS | XCT_MACTX_CRC_PAD;
723
724 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700725 const unsigned char *nh = skb_network_header(skb);
726
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700727 switch (ip_hdr(skb)->protocol) {
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600728 case IPPROTO_TCP:
729 dflags |= XCT_MACTX_CSUM_TCP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300730 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700731 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600732 break;
733 case IPPROTO_UDP:
734 dflags |= XCT_MACTX_CSUM_UDP;
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -0300735 dflags |= XCT_MACTX_IPH(skb_network_header_len(skb) >> 2);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700736 dflags |= XCT_MACTX_IPO(nh - skb->data);
Olof Johanssonf5cd7872007-01-31 21:43:54 -0600737 break;
738 }
739 }
740
741 map = pci_map_single(mac->dma_pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
742
743 if (dma_mapping_error(map))
744 return NETDEV_TX_BUSY;
745
746 txring = mac->tx;
747
748 spin_lock_irqsave(&txring->lock, flags);
749
750 if (txring->next_to_clean - txring->next_to_use == TX_RING_SIZE) {
751 spin_unlock_irqrestore(&txring->lock, flags);
752 pasemi_mac_clean_tx(mac);
753 spin_lock_irqsave(&txring->lock, flags);
754
755 if (txring->next_to_clean - txring->next_to_use ==
756 TX_RING_SIZE) {
757 /* Still no room -- stop the queue and wait for tx
758 * intr when there's room.
759 */
760 netif_stop_queue(dev);
761 goto out_err;
762 }
763 }
764
765
766 dp = &TX_DESC(mac, txring->next_to_use);
767 info = &TX_DESC_INFO(mac, txring->next_to_use);
768
769 dp->mactx = dflags | XCT_MACTX_LLEN(skb->len);
770 dp->ptr = XCT_PTR_LEN(skb->len) | XCT_PTR_ADDR(map);
771 info->dma = map;
772 info->skb = skb;
773
774 txring->next_to_use++;
775 mac->stats.tx_packets++;
776 mac->stats.tx_bytes += skb->len;
777
778 spin_unlock_irqrestore(&txring->lock, flags);
779
780 pci_write_config_dword(mac->dma_pdev,
781 PAS_DMA_TXCHAN_INCR(mac->dma_txch), 1);
782
783 return NETDEV_TX_OK;
784
785out_err:
786 spin_unlock_irqrestore(&txring->lock, flags);
787 pci_unmap_single(mac->dma_pdev, map, skb->len, PCI_DMA_TODEVICE);
788 return NETDEV_TX_BUSY;
789}
790
791static struct net_device_stats *pasemi_mac_get_stats(struct net_device *dev)
792{
793 struct pasemi_mac *mac = netdev_priv(dev);
794
795 return &mac->stats;
796}
797
798static void pasemi_mac_set_rx_mode(struct net_device *dev)
799{
800 struct pasemi_mac *mac = netdev_priv(dev);
801 unsigned int flags;
802
803 pci_read_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, &flags);
804
805 /* Set promiscuous */
806 if (dev->flags & IFF_PROMISC)
807 flags |= PAS_MAC_CFG_PCFG_PR;
808 else
809 flags &= ~PAS_MAC_CFG_PCFG_PR;
810
811 pci_write_config_dword(mac->pdev, PAS_MAC_CFG_PCFG, flags);
812}
813
814
815static int pasemi_mac_poll(struct net_device *dev, int *budget)
816{
817 int pkts, limit = min(*budget, dev->quota);
818 struct pasemi_mac *mac = netdev_priv(dev);
819
820 pkts = pasemi_mac_clean_rx(mac, limit);
821
822 if (pkts < limit) {
823 /* all done, no more packets present */
824 netif_rx_complete(dev);
825
826 /* re-enable receive interrupts */
827 pci_write_config_dword(mac->iob_pdev, PAS_IOB_DMA_COM_TIMEOUTCFG,
828 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(1000000));
829 return 0;
830 } else {
831 /* used up our quantum, so reschedule */
832 dev->quota -= pkts;
833 *budget -= pkts;
834 return 1;
835 }
836}
837
838static int __devinit
839pasemi_mac_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
840{
841 static int index = 0;
842 struct net_device *dev;
843 struct pasemi_mac *mac;
844 int err;
845
846 err = pci_enable_device(pdev);
847 if (err)
848 return err;
849
850 dev = alloc_etherdev(sizeof(struct pasemi_mac));
851 if (dev == NULL) {
852 dev_err(&pdev->dev,
853 "pasemi_mac: Could not allocate ethernet device.\n");
854 err = -ENOMEM;
855 goto out_disable_device;
856 }
857
858 SET_MODULE_OWNER(dev);
859 pci_set_drvdata(pdev, dev);
860 SET_NETDEV_DEV(dev, &pdev->dev);
861
862 mac = netdev_priv(dev);
863
864 mac->pdev = pdev;
865 mac->netdev = dev;
866 mac->dma_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa007, NULL);
867
868 if (!mac->dma_pdev) {
869 dev_err(&pdev->dev, "Can't find DMA Controller\n");
870 err = -ENODEV;
871 goto out_free_netdev;
872 }
873
874 mac->iob_pdev = pci_get_device(PCI_VENDOR_ID_PASEMI, 0xa001, NULL);
875
876 if (!mac->iob_pdev) {
877 dev_err(&pdev->dev, "Can't find I/O Bridge\n");
878 err = -ENODEV;
879 goto out_put_dma_pdev;
880 }
881
882 /* These should come out of the device tree eventually */
883 mac->dma_txch = index;
884 mac->dma_rxch = index;
885
886 /* We probe GMAC before XAUI, but the DMA interfaces are
887 * in XAUI, GMAC order.
888 */
889 if (index < 4)
890 mac->dma_if = index + 2;
891 else
892 mac->dma_if = index - 4;
893 index++;
894
895 switch (pdev->device) {
896 case 0xa005:
897 mac->type = MAC_TYPE_GMAC;
898 break;
899 case 0xa006:
900 mac->type = MAC_TYPE_XAUI;
901 break;
902 default:
903 err = -ENODEV;
904 goto out;
905 }
906
907 /* get mac addr from device tree */
908 if (pasemi_get_mac_addr(mac) || !is_valid_ether_addr(mac->mac_addr)) {
909 err = -ENODEV;
910 goto out;
911 }
912 memcpy(dev->dev_addr, mac->mac_addr, sizeof(mac->mac_addr));
913
914 dev->open = pasemi_mac_open;
915 dev->stop = pasemi_mac_close;
916 dev->hard_start_xmit = pasemi_mac_start_tx;
917 dev->get_stats = pasemi_mac_get_stats;
918 dev->set_multicast_list = pasemi_mac_set_rx_mode;
919 dev->weight = 64;
920 dev->poll = pasemi_mac_poll;
921 dev->features = NETIF_F_HW_CSUM;
922
923 /* The dma status structure is located in the I/O bridge, and
924 * is cache coherent.
925 */
926 if (!dma_status)
927 /* XXXOJN This should come from the device tree */
928 dma_status = __ioremap(0xfd800000, 0x1000, 0);
929
930 mac->rx_status = &dma_status->rx_sta[mac->dma_rxch];
931 mac->tx_status = &dma_status->tx_sta[mac->dma_txch];
932
933 err = register_netdev(dev);
934
935 if (err) {
936 dev_err(&mac->pdev->dev, "register_netdev failed with error %d\n",
937 err);
938 goto out;
939 } else
940 printk(KERN_INFO "%s: PA Semi %s: intf %d, txch %d, rxch %d, "
941 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
942 dev->name, mac->type == MAC_TYPE_GMAC ? "GMAC" : "XAUI",
943 mac->dma_if, mac->dma_txch, mac->dma_rxch,
944 dev->dev_addr[0], dev->dev_addr[1], dev->dev_addr[2],
945 dev->dev_addr[3], dev->dev_addr[4], dev->dev_addr[5]);
946
947 return err;
948
949out:
950 pci_dev_put(mac->iob_pdev);
951out_put_dma_pdev:
952 pci_dev_put(mac->dma_pdev);
953out_free_netdev:
954 free_netdev(dev);
955out_disable_device:
956 pci_disable_device(pdev);
957 return err;
958
959}
960
961static void __devexit pasemi_mac_remove(struct pci_dev *pdev)
962{
963 struct net_device *netdev = pci_get_drvdata(pdev);
964 struct pasemi_mac *mac;
965
966 if (!netdev)
967 return;
968
969 mac = netdev_priv(netdev);
970
971 unregister_netdev(netdev);
972
973 pci_disable_device(pdev);
974 pci_dev_put(mac->dma_pdev);
975 pci_dev_put(mac->iob_pdev);
976
977 pci_set_drvdata(pdev, NULL);
978 free_netdev(netdev);
979}
980
981static struct pci_device_id pasemi_mac_pci_tbl[] = {
982 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa005) },
983 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI, 0xa006) },
984};
985
986MODULE_DEVICE_TABLE(pci, pasemi_mac_pci_tbl);
987
988static struct pci_driver pasemi_mac_driver = {
989 .name = "pasemi_mac",
990 .id_table = pasemi_mac_pci_tbl,
991 .probe = pasemi_mac_probe,
992 .remove = __devexit_p(pasemi_mac_remove),
993};
994
995static void __exit pasemi_mac_cleanup_module(void)
996{
997 pci_unregister_driver(&pasemi_mac_driver);
998 __iounmap(dma_status);
999 dma_status = NULL;
1000}
1001
1002int pasemi_mac_init_module(void)
1003{
1004 return pci_register_driver(&pasemi_mac_driver);
1005}
1006
1007MODULE_LICENSE("GPL");
1008MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
1009MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
1010
1011module_init(pasemi_mac_init_module);
1012module_exit(pasemi_mac_cleanup_module);