2 * Copyright (C) 2006-2007 PA Semi, Inc
4 * Driver for the PA Semi PWRficient onchip 1G/10G Ethernet MACs
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.
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.
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
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>
30 #include <linux/skbuff.h>
33 #include <linux/tcp.h>
34 #include <net/checksum.h>
36 #include "pasemi_mac.h"
41 * - Get rid of pci_{read,write}_config(), map registers with ioremap
46 * - Other performance improvements
50 /* Must be a power of two */
51 #define RX_RING_SIZE 512
52 #define TX_RING_SIZE 512
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)])
60 #define BUF_SIZE 1646 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
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
68 static struct pasdma_status
*dma_status
;
70 static int pasemi_get_mac_addr(struct pasemi_mac
*mac
)
72 struct pci_dev
*pdev
= mac
->pdev
;
73 struct device_node
*dn
= pci_device_to_OF_node(pdev
);
79 "No device node for mac, not configuring\n");
83 maddr
= get_property(dn
, "mac-address", NULL
);
86 "no mac address in device tree, not configuring\n");
90 if (sscanf(maddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr
[0],
91 &addr
[1], &addr
[2], &addr
[3], &addr
[4], &addr
[5]) != 6) {
93 "can't parse mac address, not configuring\n");
97 memcpy(mac
->mac_addr
, addr
, sizeof(addr
));
101 static int pasemi_mac_setup_rx_resources(struct net_device
*dev
)
103 struct pasemi_mac_rxring
*ring
;
104 struct pasemi_mac
*mac
= netdev_priv(dev
);
105 int chan_id
= mac
->dma_rxch
;
107 ring
= kzalloc(sizeof(*ring
), GFP_KERNEL
);
112 spin_lock_init(&ring
->lock
);
114 ring
->desc_info
= kzalloc(sizeof(struct pasemi_mac_buffer
) *
115 RX_RING_SIZE
, GFP_KERNEL
);
117 if (!ring
->desc_info
)
120 /* Allocate descriptors */
121 ring
->desc
= dma_alloc_coherent(&mac
->dma_pdev
->dev
,
123 sizeof(struct pas_dma_xct_descr
),
124 &ring
->dma
, GFP_KERNEL
);
129 memset(ring
->desc
, 0, RX_RING_SIZE
* sizeof(struct pas_dma_xct_descr
));
131 ring
->buffers
= dma_alloc_coherent(&mac
->dma_pdev
->dev
,
132 RX_RING_SIZE
* sizeof(u64
),
133 &ring
->buf_dma
, GFP_KERNEL
);
137 memset(ring
->buffers
, 0, RX_RING_SIZE
* sizeof(u64
));
139 pci_write_config_dword(mac
->dma_pdev
, PAS_DMA_RXCHAN_BASEL(chan_id
),
140 PAS_DMA_RXCHAN_BASEL_BRBL(ring
->dma
));
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));
146 pci_write_config_dword(mac
->dma_pdev
, PAS_DMA_RXCHAN_CFG(chan_id
),
147 PAS_DMA_RXCHAN_CFG_HBU(1));
149 pci_write_config_dword(mac
->dma_pdev
, PAS_DMA_RXINT_BASEL(mac
->dma_if
),
150 PAS_DMA_RXINT_BASEL_BRBL(__pa(ring
->buffers
)));
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));
156 ring
->next_to_fill
= 0;
157 ring
->next_to_clean
= 0;
159 snprintf(ring
->irq_name
, sizeof(ring
->irq_name
),
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
);
170 kfree(ring
->desc_info
);
178 static int pasemi_mac_setup_tx_resources(struct net_device
*dev
)
180 struct pasemi_mac
*mac
= netdev_priv(dev
);
182 int chan_id
= mac
->dma_txch
;
183 struct pasemi_mac_txring
*ring
;
185 ring
= kzalloc(sizeof(*ring
), GFP_KERNEL
);
189 spin_lock_init(&ring
->lock
);
191 ring
->desc_info
= kzalloc(sizeof(struct pasemi_mac_buffer
) *
192 TX_RING_SIZE
, GFP_KERNEL
);
193 if (!ring
->desc_info
)
196 /* Allocate descriptors */
197 ring
->desc
= dma_alloc_coherent(&mac
->dma_pdev
->dev
,
199 sizeof(struct pas_dma_xct_descr
),
200 &ring
->dma
, GFP_KERNEL
);
204 memset(ring
->desc
, 0, TX_RING_SIZE
* sizeof(struct pas_dma_xct_descr
));
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);
211 pci_write_config_dword(mac
->dma_pdev
, PAS_DMA_TXCHAN_BASEU(chan_id
), val
);
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));
219 ring
->next_to_use
= 0;
220 ring
->next_to_clean
= 0;
222 snprintf(ring
->irq_name
, sizeof(ring
->irq_name
),
229 kfree(ring
->desc_info
);
236 static void pasemi_mac_free_tx_resources(struct net_device
*dev
)
238 struct pasemi_mac
*mac
= netdev_priv(dev
);
240 struct pasemi_mac_buffer
*info
;
241 struct pas_dma_xct_descr
*dp
;
243 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
244 info
= &TX_DESC_INFO(mac
, i
);
245 dp
= &TX_DESC(mac
, i
);
248 pci_unmap_single(mac
->dma_pdev
,
252 dev_kfree_skb_any(info
->skb
);
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
);
265 kfree(mac
->tx
->desc_info
);
270 static void pasemi_mac_free_rx_resources(struct net_device
*dev
)
272 struct pasemi_mac
*mac
= netdev_priv(dev
);
274 struct pasemi_mac_buffer
*info
;
275 struct pas_dma_xct_descr
*dp
;
277 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
278 info
= &RX_DESC_INFO(mac
, i
);
279 dp
= &RX_DESC(mac
, i
);
282 pci_unmap_single(mac
->dma_pdev
,
286 dev_kfree_skb_any(info
->skb
);
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
);
299 dma_free_coherent(&mac
->dma_pdev
->dev
, RX_RING_SIZE
* sizeof(u64
),
300 mac
->rx
->buffers
, mac
->rx
->buf_dma
);
302 kfree(mac
->rx
->desc_info
);
307 static void pasemi_mac_replenish_rx_ring(struct net_device
*dev
)
309 struct pasemi_mac
*mac
= netdev_priv(dev
);
311 int start
= mac
->rx
->next_to_fill
;
314 count
= (mac
->rx
->next_to_clean
+ RX_RING_SIZE
-
315 mac
->rx
->next_to_fill
) & (RX_RING_SIZE
- 1);
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
;
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
);
330 skb
= dev_alloc_skb(BUF_SIZE
);
339 dma
= pci_map_single(mac
->dma_pdev
, skb
->data
, skb
->len
,
342 if (dma_mapping_error(dma
)) {
343 dev_kfree_skb_irq(info
->skb
);
350 *buff
= XCT_RXB_LEN(BUF_SIZE
) | XCT_RXB_ADDR(dma
);
355 pci_write_config_dword(mac
->dma_pdev
,
356 PAS_DMA_RXCHAN_INCR(mac
->dma_rxch
),
358 pci_write_config_dword(mac
->dma_pdev
,
359 PAS_DMA_RXINT_INCR(mac
->dma_if
),
362 mac
->rx
->next_to_fill
+= count
;
365 static int pasemi_mac_clean_rx(struct pasemi_mac
*mac
, int limit
)
370 spin_lock(&mac
->rx
->lock
);
372 start
= mac
->rx
->next_to_clean
;
375 for (i
= start
; i
< (start
+ RX_RING_SIZE
) && count
< limit
; i
++) {
376 struct pas_dma_xct_descr
*dp
;
377 struct pasemi_mac_buffer
*info
;
384 dp
= &RX_DESC(mac
, i
);
386 if (!(dp
->macrx
& XCT_MACRX_O
))
393 /* We have to scan for our skb since there's no way
394 * to back-map them from the descriptor, and if we
395 * have several receive channels then they might not
396 * show up in the same order as they were put on the
400 dma
= (dp
->ptr
& XCT_PTR_ADDR_M
);
401 for (j
= start
; j
< (start
+ RX_RING_SIZE
); j
++) {
402 info
= &RX_DESC_INFO(mac
, j
);
403 if (info
->dma
== dma
)
408 BUG_ON(info
->dma
!= dma
);
410 pci_unmap_single(mac
->dma_pdev
, info
->dma
, info
->skb
->len
,
415 len
= (dp
->macrx
& XCT_MACRX_LLEN_M
) >> XCT_MACRX_LLEN_S
;
419 skb
->protocol
= eth_type_trans(skb
, mac
->netdev
);
421 if ((dp
->macrx
& XCT_MACRX_HTY_M
) == XCT_MACRX_HTY_IPV4_OK
) {
422 skb
->ip_summed
= CHECKSUM_COMPLETE
;
423 skb
->csum
= (dp
->macrx
& XCT_MACRX_CSUM_M
) >>
426 skb
->ip_summed
= CHECKSUM_NONE
;
428 mac
->stats
.rx_bytes
+= len
;
429 mac
->stats
.rx_packets
++;
431 netif_receive_skb(skb
);
439 mac
->rx
->next_to_clean
+= count
;
440 pasemi_mac_replenish_rx_ring(mac
->netdev
);
442 spin_unlock(&mac
->rx
->lock
);
447 static int pasemi_mac_clean_tx(struct pasemi_mac
*mac
)
450 struct pasemi_mac_buffer
*info
;
451 struct pas_dma_xct_descr
*dp
;
455 spin_lock_irqsave(&mac
->tx
->lock
, flags
);
457 start
= mac
->tx
->next_to_clean
;
460 for (i
= start
; i
< mac
->tx
->next_to_use
; i
++) {
461 dp
= &TX_DESC(mac
, i
);
462 if (!dp
|| (dp
->mactx
& XCT_MACTX_O
))
467 info
= &TX_DESC_INFO(mac
, i
);
469 pci_unmap_single(mac
->dma_pdev
, info
->dma
,
470 info
->skb
->len
, PCI_DMA_TODEVICE
);
471 dev_kfree_skb_irq(info
->skb
);
478 mac
->tx
->next_to_clean
+= count
;
479 spin_unlock_irqrestore(&mac
->tx
->lock
, flags
);
485 static irqreturn_t
pasemi_mac_rx_intr(int irq
, void *data
)
487 struct net_device
*dev
= data
;
488 struct pasemi_mac
*mac
= netdev_priv(dev
);
491 if (!(*mac
->rx_status
& PAS_STATUS_INT
))
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));
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
;
503 pci_write_config_dword(mac
->iob_pdev
,
504 PAS_IOB_DMA_RXCH_RESET(mac
->dma_rxch
), reg
);
510 static irqreturn_t
pasemi_mac_tx_intr(int irq
, void *data
)
512 struct net_device
*dev
= data
;
513 struct pasemi_mac
*mac
= netdev_priv(dev
);
517 was_full
= mac
->tx
->next_to_clean
- mac
->tx
->next_to_use
== TX_RING_SIZE
;
519 if (!(*mac
->tx_status
& PAS_STATUS_INT
))
522 pasemi_mac_clean_tx(mac
);
524 reg
= PAS_IOB_DMA_TXCH_RESET_PINTC
| PAS_IOB_DMA_TXCH_RESET_SINTC
;
525 if (*mac
->tx_status
& PAS_STATUS_TIMER
)
526 reg
|= PAS_IOB_DMA_TXCH_RESET_TINTC
;
528 pci_write_config_dword(mac
->iob_pdev
, PAS_IOB_DMA_TXCH_RESET(mac
->dma_txch
),
532 netif_wake_queue(dev
);
537 static int pasemi_mac_open(struct net_device
*dev
)
539 struct pasemi_mac
*mac
= netdev_priv(dev
);
543 /* enable rx section */
544 pci_write_config_dword(mac
->dma_pdev
, PAS_DMA_COM_RXCMD
,
545 PAS_DMA_COM_RXCMD_EN
);
547 /* enable tx section */
548 pci_write_config_dword(mac
->dma_pdev
, PAS_DMA_COM_TXCMD
,
549 PAS_DMA_COM_TXCMD_EN
);
551 flags
= PAS_MAC_CFG_TXP_FCE
| PAS_MAC_CFG_TXP_FPC(3) |
552 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
553 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
555 pci_write_config_dword(mac
->pdev
, PAS_MAC_CFG_TXP
, flags
);
557 flags
= PAS_MAC_CFG_PCFG_S1
| PAS_MAC_CFG_PCFG_PE
|
558 PAS_MAC_CFG_PCFG_PR
| PAS_MAC_CFG_PCFG_CE
;
560 flags
|= PAS_MAC_CFG_PCFG_TSR_1G
| PAS_MAC_CFG_PCFG_SPD_1G
;
562 pci_write_config_dword(mac
->iob_pdev
, PAS_IOB_DMA_RXCH_CFG(mac
->dma_rxch
),
563 PAS_IOB_DMA_RXCH_CFG_CNTTH(30));
565 pci_write_config_dword(mac
->iob_pdev
, PAS_IOB_DMA_COM_TIMEOUTCFG
,
566 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(1000000));
568 pci_write_config_dword(mac
->pdev
, PAS_MAC_CFG_PCFG
, flags
);
570 ret
= pasemi_mac_setup_rx_resources(dev
);
572 goto out_rx_resources
;
574 ret
= pasemi_mac_setup_tx_resources(dev
);
576 goto out_tx_resources
;
578 pci_write_config_dword(mac
->pdev
, PAS_MAC_IPC_CHNL
,
579 PAS_MAC_IPC_CHNL_DCHNO(mac
->dma_rxch
) |
580 PAS_MAC_IPC_CHNL_BCH(mac
->dma_rxch
));
583 pci_write_config_dword(mac
->dma_pdev
,
584 PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
),
585 PAS_DMA_RXINT_RCMDSTA_EN
);
587 /* enable rx channel */
588 pci_write_config_dword(mac
->dma_pdev
,
589 PAS_DMA_RXCHAN_CCMDSTA(mac
->dma_rxch
),
590 PAS_DMA_RXCHAN_CCMDSTA_EN
|
591 PAS_DMA_RXCHAN_CCMDSTA_DU
);
593 /* enable tx channel */
594 pci_write_config_dword(mac
->dma_pdev
,
595 PAS_DMA_TXCHAN_TCMDSTA(mac
->dma_txch
),
596 PAS_DMA_TXCHAN_TCMDSTA_EN
);
598 pasemi_mac_replenish_rx_ring(dev
);
600 netif_start_queue(dev
);
601 netif_poll_enable(dev
);
603 ret
= request_irq(mac
->dma_pdev
->irq
+ mac
->dma_txch
,
604 &pasemi_mac_tx_intr
, IRQF_DISABLED
,
605 mac
->tx
->irq_name
, dev
);
607 dev_err(&mac
->pdev
->dev
, "request_irq of irq %d failed: %d\n",
608 mac
->dma_pdev
->irq
+ mac
->dma_txch
, ret
);
612 ret
= request_irq(mac
->dma_pdev
->irq
+ 20 + mac
->dma_rxch
,
613 &pasemi_mac_rx_intr
, IRQF_DISABLED
,
614 mac
->rx
->irq_name
, dev
);
616 dev_err(&mac
->pdev
->dev
, "request_irq of irq %d failed: %d\n",
617 mac
->dma_pdev
->irq
+ 20 + mac
->dma_rxch
, ret
);
624 free_irq(mac
->dma_pdev
->irq
+ mac
->dma_txch
, dev
);
626 netif_poll_disable(dev
);
627 netif_stop_queue(dev
);
628 pasemi_mac_free_tx_resources(dev
);
630 pasemi_mac_free_rx_resources(dev
);
636 #define MAX_RETRIES 5000
638 static int pasemi_mac_close(struct net_device
*dev
)
640 struct pasemi_mac
*mac
= netdev_priv(dev
);
644 netif_stop_queue(dev
);
646 /* Clean out any pending buffers */
647 pasemi_mac_clean_tx(mac
);
648 pasemi_mac_clean_rx(mac
, RX_RING_SIZE
);
650 /* Disable interface */
651 pci_write_config_dword(mac
->dma_pdev
,
652 PAS_DMA_TXCHAN_TCMDSTA(mac
->dma_txch
),
653 PAS_DMA_TXCHAN_TCMDSTA_ST
);
654 pci_write_config_dword(mac
->dma_pdev
,
655 PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
),
656 PAS_DMA_RXINT_RCMDSTA_ST
);
657 pci_write_config_dword(mac
->dma_pdev
,
658 PAS_DMA_RXCHAN_CCMDSTA(mac
->dma_rxch
),
659 PAS_DMA_RXCHAN_CCMDSTA_ST
);
661 for (retries
= 0; retries
< MAX_RETRIES
; retries
++) {
662 pci_read_config_dword(mac
->dma_pdev
,
663 PAS_DMA_TXCHAN_TCMDSTA(mac
->dma_txch
),
665 if (stat
& PAS_DMA_TXCHAN_TCMDSTA_ACT
)
670 if (!(stat
& PAS_DMA_TXCHAN_TCMDSTA_ACT
)) {
671 dev_err(&mac
->dma_pdev
->dev
, "Failed to stop tx channel\n");
674 for (retries
= 0; retries
< MAX_RETRIES
; retries
++) {
675 pci_read_config_dword(mac
->dma_pdev
,
676 PAS_DMA_RXCHAN_CCMDSTA(mac
->dma_rxch
),
678 if (stat
& PAS_DMA_RXCHAN_CCMDSTA_ACT
)
683 if (!(stat
& PAS_DMA_RXCHAN_CCMDSTA_ACT
)) {
684 dev_err(&mac
->dma_pdev
->dev
, "Failed to stop rx channel\n");
687 for (retries
= 0; retries
< MAX_RETRIES
; retries
++) {
688 pci_read_config_dword(mac
->dma_pdev
,
689 PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
),
691 if (stat
& PAS_DMA_RXINT_RCMDSTA_ACT
)
696 if (!(stat
& PAS_DMA_RXINT_RCMDSTA_ACT
)) {
697 dev_err(&mac
->dma_pdev
->dev
, "Failed to stop rx interface\n");
700 /* Then, disable the channel. This must be done separately from
701 * stopping, since you can't disable when active.
704 pci_write_config_dword(mac
->dma_pdev
,
705 PAS_DMA_TXCHAN_TCMDSTA(mac
->dma_txch
), 0);
706 pci_write_config_dword(mac
->dma_pdev
,
707 PAS_DMA_RXCHAN_CCMDSTA(mac
->dma_rxch
), 0);
708 pci_write_config_dword(mac
->dma_pdev
,
709 PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
), 0);
711 free_irq(mac
->dma_pdev
->irq
+ mac
->dma_txch
, dev
);
712 free_irq(mac
->dma_pdev
->irq
+ 20 + mac
->dma_rxch
, dev
);
715 pasemi_mac_free_rx_resources(dev
);
716 pasemi_mac_free_tx_resources(dev
);
721 static int pasemi_mac_start_tx(struct sk_buff
*skb
, struct net_device
*dev
)
723 struct pasemi_mac
*mac
= netdev_priv(dev
);
724 struct pasemi_mac_txring
*txring
;
725 struct pasemi_mac_buffer
*info
;
726 struct pas_dma_xct_descr
*dp
;
731 dflags
= XCT_MACTX_O
| XCT_MACTX_ST
| XCT_MACTX_SS
| XCT_MACTX_CRC_PAD
;
733 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
734 switch (skb
->nh
.iph
->protocol
) {
736 dflags
|= XCT_MACTX_CSUM_TCP
;
737 dflags
|= XCT_MACTX_IPH((skb
->h
.raw
- skb
->nh
.raw
) >> 2);
738 dflags
|= XCT_MACTX_IPO(skb
->nh
.raw
- skb
->data
);
741 dflags
|= XCT_MACTX_CSUM_UDP
;
742 dflags
|= XCT_MACTX_IPH((skb
->h
.raw
- skb
->nh
.raw
) >> 2);
743 dflags
|= XCT_MACTX_IPO(skb
->nh
.raw
- skb
->data
);
748 map
= pci_map_single(mac
->dma_pdev
, skb
->data
, skb
->len
, PCI_DMA_TODEVICE
);
750 if (dma_mapping_error(map
))
751 return NETDEV_TX_BUSY
;
755 spin_lock_irqsave(&txring
->lock
, flags
);
757 if (txring
->next_to_clean
- txring
->next_to_use
== TX_RING_SIZE
) {
758 spin_unlock_irqrestore(&txring
->lock
, flags
);
759 pasemi_mac_clean_tx(mac
);
760 spin_lock_irqsave(&txring
->lock
, flags
);
762 if (txring
->next_to_clean
- txring
->next_to_use
==
764 /* Still no room -- stop the queue and wait for tx
765 * intr when there's room.
767 netif_stop_queue(dev
);
773 dp
= &TX_DESC(mac
, txring
->next_to_use
);
774 info
= &TX_DESC_INFO(mac
, txring
->next_to_use
);
776 dp
->mactx
= dflags
| XCT_MACTX_LLEN(skb
->len
);
777 dp
->ptr
= XCT_PTR_LEN(skb
->len
) | XCT_PTR_ADDR(map
);
781 txring
->next_to_use
++;
782 mac
->stats
.tx_packets
++;
783 mac
->stats
.tx_bytes
+= skb
->len
;
785 spin_unlock_irqrestore(&txring
->lock
, flags
);
787 pci_write_config_dword(mac
->dma_pdev
,
788 PAS_DMA_TXCHAN_INCR(mac
->dma_txch
), 1);
793 spin_unlock_irqrestore(&txring
->lock
, flags
);
794 pci_unmap_single(mac
->dma_pdev
, map
, skb
->len
, PCI_DMA_TODEVICE
);
795 return NETDEV_TX_BUSY
;
798 static struct net_device_stats
*pasemi_mac_get_stats(struct net_device
*dev
)
800 struct pasemi_mac
*mac
= netdev_priv(dev
);
805 static void pasemi_mac_set_rx_mode(struct net_device
*dev
)
807 struct pasemi_mac
*mac
= netdev_priv(dev
);
810 pci_read_config_dword(mac
->pdev
, PAS_MAC_CFG_PCFG
, &flags
);
812 /* Set promiscuous */
813 if (dev
->flags
& IFF_PROMISC
)
814 flags
|= PAS_MAC_CFG_PCFG_PR
;
816 flags
&= ~PAS_MAC_CFG_PCFG_PR
;
818 pci_write_config_dword(mac
->pdev
, PAS_MAC_CFG_PCFG
, flags
);
822 static int pasemi_mac_poll(struct net_device
*dev
, int *budget
)
824 int pkts
, limit
= min(*budget
, dev
->quota
);
825 struct pasemi_mac
*mac
= netdev_priv(dev
);
827 pkts
= pasemi_mac_clean_rx(mac
, limit
);
830 /* all done, no more packets present */
831 netif_rx_complete(dev
);
833 /* re-enable receive interrupts */
834 pci_write_config_dword(mac
->iob_pdev
, PAS_IOB_DMA_COM_TIMEOUTCFG
,
835 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(1000000));
838 /* used up our quantum, so reschedule */
846 pasemi_mac_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
848 static int index
= 0;
849 struct net_device
*dev
;
850 struct pasemi_mac
*mac
;
853 err
= pci_enable_device(pdev
);
857 dev
= alloc_etherdev(sizeof(struct pasemi_mac
));
860 "pasemi_mac: Could not allocate ethernet device.\n");
862 goto out_disable_device
;
865 SET_MODULE_OWNER(dev
);
866 pci_set_drvdata(pdev
, dev
);
867 SET_NETDEV_DEV(dev
, &pdev
->dev
);
869 mac
= netdev_priv(dev
);
873 mac
->dma_pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa007, NULL
);
875 if (!mac
->dma_pdev
) {
876 dev_err(&pdev
->dev
, "Can't find DMA Controller\n");
878 goto out_free_netdev
;
881 mac
->iob_pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa001, NULL
);
883 if (!mac
->iob_pdev
) {
884 dev_err(&pdev
->dev
, "Can't find I/O Bridge\n");
886 goto out_put_dma_pdev
;
889 /* These should come out of the device tree eventually */
890 mac
->dma_txch
= index
;
891 mac
->dma_rxch
= index
;
893 /* We probe GMAC before XAUI, but the DMA interfaces are
894 * in XAUI, GMAC order.
897 mac
->dma_if
= index
+ 2;
899 mac
->dma_if
= index
- 4;
902 switch (pdev
->device
) {
904 mac
->type
= MAC_TYPE_GMAC
;
907 mac
->type
= MAC_TYPE_XAUI
;
914 /* get mac addr from device tree */
915 if (pasemi_get_mac_addr(mac
) || !is_valid_ether_addr(mac
->mac_addr
)) {
919 memcpy(dev
->dev_addr
, mac
->mac_addr
, sizeof(mac
->mac_addr
));
921 dev
->open
= pasemi_mac_open
;
922 dev
->stop
= pasemi_mac_close
;
923 dev
->hard_start_xmit
= pasemi_mac_start_tx
;
924 dev
->get_stats
= pasemi_mac_get_stats
;
925 dev
->set_multicast_list
= pasemi_mac_set_rx_mode
;
927 dev
->poll
= pasemi_mac_poll
;
928 dev
->features
= NETIF_F_HW_CSUM
;
930 /* The dma status structure is located in the I/O bridge, and
934 /* XXXOJN This should come from the device tree */
935 dma_status
= __ioremap(0xfd800000, 0x1000, 0);
937 mac
->rx_status
= &dma_status
->rx_sta
[mac
->dma_rxch
];
938 mac
->tx_status
= &dma_status
->tx_sta
[mac
->dma_txch
];
940 err
= register_netdev(dev
);
943 dev_err(&mac
->pdev
->dev
, "register_netdev failed with error %d\n",
947 printk(KERN_INFO
"%s: PA Semi %s: intf %d, txch %d, rxch %d, "
948 "hw addr %02x:%02x:%02x:%02x:%02x:%02x\n",
949 dev
->name
, mac
->type
== MAC_TYPE_GMAC
? "GMAC" : "XAUI",
950 mac
->dma_if
, mac
->dma_txch
, mac
->dma_rxch
,
951 dev
->dev_addr
[0], dev
->dev_addr
[1], dev
->dev_addr
[2],
952 dev
->dev_addr
[3], dev
->dev_addr
[4], dev
->dev_addr
[5]);
957 pci_dev_put(mac
->iob_pdev
);
959 pci_dev_put(mac
->dma_pdev
);
963 pci_disable_device(pdev
);
968 static void __devexit
pasemi_mac_remove(struct pci_dev
*pdev
)
970 struct net_device
*netdev
= pci_get_drvdata(pdev
);
971 struct pasemi_mac
*mac
;
976 mac
= netdev_priv(netdev
);
978 unregister_netdev(netdev
);
980 pci_disable_device(pdev
);
981 pci_dev_put(mac
->dma_pdev
);
982 pci_dev_put(mac
->iob_pdev
);
984 pci_set_drvdata(pdev
, NULL
);
988 static struct pci_device_id pasemi_mac_pci_tbl
[] = {
989 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI
, 0xa005) },
990 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI
, 0xa006) },
993 MODULE_DEVICE_TABLE(pci
, pasemi_mac_pci_tbl
);
995 static struct pci_driver pasemi_mac_driver
= {
996 .name
= "pasemi_mac",
997 .id_table
= pasemi_mac_pci_tbl
,
998 .probe
= pasemi_mac_probe
,
999 .remove
= __devexit_p(pasemi_mac_remove
),
1002 static void __exit
pasemi_mac_cleanup_module(void)
1004 pci_unregister_driver(&pasemi_mac_driver
);
1005 __iounmap(dma_status
);
1009 int pasemi_mac_init_module(void)
1011 return pci_register_driver(&pasemi_mac_driver
);
1014 MODULE_LICENSE("GPL");
1015 MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
1016 MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
1018 module_init(pasemi_mac_init_module
);
1019 module_exit(pasemi_mac_cleanup_module
);