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>
35 #include <linux/inet_lro.h>
38 #include <asm/firmware.h>
39 #include <asm/pasemi_dma.h>
41 #include "pasemi_mac.h"
43 /* We have our own align, since ppc64 in general has it at 0 because
44 * of design flaws in some of the server bridge chips. However, for
45 * PWRficient doing the unaligned copies is more expensive than doing
46 * unaligned DMA, so make sure the data is aligned instead.
48 #define LOCAL_SKB_ALIGN 2
59 /* Must be a power of two */
60 #define RX_RING_SIZE 2048
61 #define TX_RING_SIZE 4096
63 #define LRO_MAX_AGGR 64
66 #define PE_MAX_MTU 1500
67 #define PE_DEF_MTU ETH_DATA_LEN
69 #define DEFAULT_MSG_ENABLE \
79 #define TX_DESC(tx, num) ((tx)->chan.ring_virt[(num) & (TX_RING_SIZE-1)])
80 #define TX_DESC_INFO(tx, num) ((tx)->ring_info[(num) & (TX_RING_SIZE-1)])
81 #define RX_DESC(rx, num) ((rx)->chan.ring_virt[(num) & (RX_RING_SIZE-1)])
82 #define RX_DESC_INFO(rx, num) ((rx)->ring_info[(num) & (RX_RING_SIZE-1)])
83 #define RX_BUFF(rx, num) ((rx)->buffers[(num) & (RX_RING_SIZE-1)])
85 #define RING_USED(ring) (((ring)->next_to_fill - (ring)->next_to_clean) \
87 #define RING_AVAIL(ring) ((ring->size) - RING_USED(ring))
89 MODULE_LICENSE("GPL");
90 MODULE_AUTHOR ("Olof Johansson <olof@lixom.net>");
91 MODULE_DESCRIPTION("PA Semi PWRficient Ethernet driver");
93 static int debug
= -1; /* -1 == use DEFAULT_MSG_ENABLE as value */
94 module_param(debug
, int, 0);
95 MODULE_PARM_DESC(debug
, "PA Semi MAC bitmapped debugging message enable value");
97 static int translation_enabled(void)
99 #if defined(CONFIG_PPC_PASEMI_IOMMU_DMA_FORCE)
102 return firmware_has_feature(FW_FEATURE_LPAR
);
106 static void write_iob_reg(unsigned int reg
, unsigned int val
)
108 pasemi_write_iob_reg(reg
, val
);
111 static unsigned int read_mac_reg(const struct pasemi_mac
*mac
, unsigned int reg
)
113 return pasemi_read_mac_reg(mac
->dma_if
, reg
);
116 static void write_mac_reg(const struct pasemi_mac
*mac
, unsigned int reg
,
119 pasemi_write_mac_reg(mac
->dma_if
, reg
, val
);
122 static unsigned int read_dma_reg(unsigned int reg
)
124 return pasemi_read_dma_reg(reg
);
127 static void write_dma_reg(unsigned int reg
, unsigned int val
)
129 pasemi_write_dma_reg(reg
, val
);
132 static struct pasemi_mac_rxring
*rx_ring(const struct pasemi_mac
*mac
)
137 static struct pasemi_mac_txring
*tx_ring(const struct pasemi_mac
*mac
)
142 static inline void prefetch_skb(const struct sk_buff
*skb
)
152 static int mac_to_intf(struct pasemi_mac
*mac
)
154 struct pci_dev
*pdev
= mac
->pdev
;
156 int nintf
, off
, i
, j
;
157 int devfn
= pdev
->devfn
;
159 tmp
= read_dma_reg(PAS_DMA_CAP_IFI
);
160 nintf
= (tmp
& PAS_DMA_CAP_IFI_NIN_M
) >> PAS_DMA_CAP_IFI_NIN_S
;
161 off
= (tmp
& PAS_DMA_CAP_IFI_IOFF_M
) >> PAS_DMA_CAP_IFI_IOFF_S
;
163 /* IOFF contains the offset to the registers containing the
164 * DMA interface-to-MAC-pci-id mappings, and NIN contains number
165 * of total interfaces. Each register contains 4 devfns.
166 * Just do a linear search until we find the devfn of the MAC
167 * we're trying to look up.
170 for (i
= 0; i
< (nintf
+3)/4; i
++) {
171 tmp
= read_dma_reg(off
+4*i
);
172 for (j
= 0; j
< 4; j
++) {
173 if (((tmp
>> (8*j
)) & 0xff) == devfn
)
180 static void pasemi_mac_intf_disable(struct pasemi_mac
*mac
)
184 flags
= read_mac_reg(mac
, PAS_MAC_CFG_PCFG
);
185 flags
&= ~PAS_MAC_CFG_PCFG_PE
;
186 write_mac_reg(mac
, PAS_MAC_CFG_PCFG
, flags
);
189 static void pasemi_mac_intf_enable(struct pasemi_mac
*mac
)
193 flags
= read_mac_reg(mac
, PAS_MAC_CFG_PCFG
);
194 flags
|= PAS_MAC_CFG_PCFG_PE
;
195 write_mac_reg(mac
, PAS_MAC_CFG_PCFG
, flags
);
198 static int pasemi_get_mac_addr(struct pasemi_mac
*mac
)
200 struct pci_dev
*pdev
= mac
->pdev
;
201 struct device_node
*dn
= pci_device_to_OF_node(pdev
);
208 "No device node for mac, not configuring\n");
212 maddr
= of_get_property(dn
, "local-mac-address", &len
);
214 if (maddr
&& len
== 6) {
215 memcpy(mac
->mac_addr
, maddr
, 6);
219 /* Some old versions of firmware mistakenly uses mac-address
220 * (and as a string) instead of a byte array in local-mac-address.
224 maddr
= of_get_property(dn
, "mac-address", NULL
);
228 "no mac address in device tree, not configuring\n");
232 if (sscanf(maddr
, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &addr
[0],
233 &addr
[1], &addr
[2], &addr
[3], &addr
[4], &addr
[5]) != 6) {
235 "can't parse mac address, not configuring\n");
239 memcpy(mac
->mac_addr
, addr
, 6);
244 static int pasemi_mac_set_mac_addr(struct net_device
*dev
, void *p
)
246 struct pasemi_mac
*mac
= netdev_priv(dev
);
247 struct sockaddr
*addr
= p
;
248 unsigned int adr0
, adr1
;
250 if (!is_valid_ether_addr(addr
->sa_data
))
253 memcpy(dev
->dev_addr
, addr
->sa_data
, dev
->addr_len
);
255 adr0
= dev
->dev_addr
[2] << 24 |
256 dev
->dev_addr
[3] << 16 |
257 dev
->dev_addr
[4] << 8 |
259 adr1
= read_mac_reg(mac
, PAS_MAC_CFG_ADR1
);
261 adr1
|= dev
->dev_addr
[0] << 8 | dev
->dev_addr
[1];
263 pasemi_mac_intf_disable(mac
);
264 write_mac_reg(mac
, PAS_MAC_CFG_ADR0
, adr0
);
265 write_mac_reg(mac
, PAS_MAC_CFG_ADR1
, adr1
);
266 pasemi_mac_intf_enable(mac
);
271 static int get_skb_hdr(struct sk_buff
*skb
, void **iphdr
,
272 void **tcph
, u64
*hdr_flags
, void *data
)
274 u64 macrx
= (u64
) data
;
278 /* IPv4 header checksum failed */
279 if ((macrx
& XCT_MACRX_HTY_M
) != XCT_MACRX_HTY_IPV4_OK
)
283 skb_reset_network_header(skb
);
285 if (iph
->protocol
!= IPPROTO_TCP
)
288 ip_len
= ip_hdrlen(skb
);
289 skb_set_transport_header(skb
, ip_len
);
290 *tcph
= tcp_hdr(skb
);
292 /* check if ip header and tcp header are complete */
293 if (iph
->tot_len
< ip_len
+ tcp_hdrlen(skb
))
296 *hdr_flags
= LRO_IPV4
| LRO_TCP
;
302 static int pasemi_mac_unmap_tx_skb(struct pasemi_mac
*mac
,
305 const dma_addr_t
*dmas
)
308 struct pci_dev
*pdev
= mac
->dma_pdev
;
310 pci_unmap_single(pdev
, dmas
[0], skb_headlen(skb
), PCI_DMA_TODEVICE
);
312 for (f
= 0; f
< nfrags
; f
++) {
313 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[f
];
315 pci_unmap_page(pdev
, dmas
[f
+1], frag
->size
, PCI_DMA_TODEVICE
);
317 dev_kfree_skb_irq(skb
);
319 /* Freed descriptor slot + main SKB ptr + nfrags additional ptrs,
320 * aligned up to a power of 2
322 return (nfrags
+ 3) & ~1;
325 static int pasemi_mac_setup_rx_resources(const struct net_device
*dev
)
327 struct pasemi_mac_rxring
*ring
;
328 struct pasemi_mac
*mac
= netdev_priv(dev
);
332 ring
= pasemi_dma_alloc_chan(RXCHAN
, sizeof(struct pasemi_mac_rxring
),
333 offsetof(struct pasemi_mac_rxring
, chan
));
336 dev_err(&mac
->pdev
->dev
, "Can't allocate RX channel\n");
339 chno
= ring
->chan
.chno
;
341 spin_lock_init(&ring
->lock
);
343 ring
->size
= RX_RING_SIZE
;
344 ring
->ring_info
= kzalloc(sizeof(struct pasemi_mac_buffer
) *
345 RX_RING_SIZE
, GFP_KERNEL
);
347 if (!ring
->ring_info
)
350 /* Allocate descriptors */
351 if (pasemi_dma_alloc_ring(&ring
->chan
, RX_RING_SIZE
))
354 ring
->buffers
= dma_alloc_coherent(&mac
->dma_pdev
->dev
,
355 RX_RING_SIZE
* sizeof(u64
),
356 &ring
->buf_dma
, GFP_KERNEL
);
360 memset(ring
->buffers
, 0, RX_RING_SIZE
* sizeof(u64
));
362 write_dma_reg(PAS_DMA_RXCHAN_BASEL(chno
),
363 PAS_DMA_RXCHAN_BASEL_BRBL(ring
->chan
.ring_dma
));
365 write_dma_reg(PAS_DMA_RXCHAN_BASEU(chno
),
366 PAS_DMA_RXCHAN_BASEU_BRBH(ring
->chan
.ring_dma
>> 32) |
367 PAS_DMA_RXCHAN_BASEU_SIZ(RX_RING_SIZE
>> 3));
369 cfg
= PAS_DMA_RXCHAN_CFG_HBU(2);
371 if (translation_enabled())
372 cfg
|= PAS_DMA_RXCHAN_CFG_CTR
;
374 write_dma_reg(PAS_DMA_RXCHAN_CFG(chno
), cfg
);
376 write_dma_reg(PAS_DMA_RXINT_BASEL(mac
->dma_if
),
377 PAS_DMA_RXINT_BASEL_BRBL(ring
->buf_dma
));
379 write_dma_reg(PAS_DMA_RXINT_BASEU(mac
->dma_if
),
380 PAS_DMA_RXINT_BASEU_BRBH(ring
->buf_dma
>> 32) |
381 PAS_DMA_RXINT_BASEU_SIZ(RX_RING_SIZE
>> 3));
383 cfg
= PAS_DMA_RXINT_CFG_DHL(2) | PAS_DMA_RXINT_CFG_L2
|
384 PAS_DMA_RXINT_CFG_LW
| PAS_DMA_RXINT_CFG_RBP
|
385 PAS_DMA_RXINT_CFG_HEN
;
387 if (translation_enabled())
388 cfg
|= PAS_DMA_RXINT_CFG_ITRR
| PAS_DMA_RXINT_CFG_ITR
;
390 write_dma_reg(PAS_DMA_RXINT_CFG(mac
->dma_if
), cfg
);
392 ring
->next_to_fill
= 0;
393 ring
->next_to_clean
= 0;
400 kfree(ring
->ring_info
);
402 pasemi_dma_free_chan(&ring
->chan
);
407 static struct pasemi_mac_txring
*
408 pasemi_mac_setup_tx_resources(const struct net_device
*dev
)
410 struct pasemi_mac
*mac
= netdev_priv(dev
);
412 struct pasemi_mac_txring
*ring
;
416 ring
= pasemi_dma_alloc_chan(TXCHAN
, sizeof(struct pasemi_mac_txring
),
417 offsetof(struct pasemi_mac_txring
, chan
));
420 dev_err(&mac
->pdev
->dev
, "Can't allocate TX channel\n");
424 chno
= ring
->chan
.chno
;
426 spin_lock_init(&ring
->lock
);
428 ring
->size
= TX_RING_SIZE
;
429 ring
->ring_info
= kzalloc(sizeof(struct pasemi_mac_buffer
) *
430 TX_RING_SIZE
, GFP_KERNEL
);
431 if (!ring
->ring_info
)
434 /* Allocate descriptors */
435 if (pasemi_dma_alloc_ring(&ring
->chan
, TX_RING_SIZE
))
438 write_dma_reg(PAS_DMA_TXCHAN_BASEL(chno
),
439 PAS_DMA_TXCHAN_BASEL_BRBL(ring
->chan
.ring_dma
));
440 val
= PAS_DMA_TXCHAN_BASEU_BRBH(ring
->chan
.ring_dma
>> 32);
441 val
|= PAS_DMA_TXCHAN_BASEU_SIZ(TX_RING_SIZE
>> 3);
443 write_dma_reg(PAS_DMA_TXCHAN_BASEU(chno
), val
);
445 cfg
= PAS_DMA_TXCHAN_CFG_TY_IFACE
|
446 PAS_DMA_TXCHAN_CFG_TATTR(mac
->dma_if
) |
447 PAS_DMA_TXCHAN_CFG_UP
|
448 PAS_DMA_TXCHAN_CFG_WT(2);
450 if (translation_enabled())
451 cfg
|= PAS_DMA_TXCHAN_CFG_TRD
| PAS_DMA_TXCHAN_CFG_TRR
;
453 write_dma_reg(PAS_DMA_TXCHAN_CFG(chno
), cfg
);
455 ring
->next_to_fill
= 0;
456 ring
->next_to_clean
= 0;
462 kfree(ring
->ring_info
);
464 pasemi_dma_free_chan(&ring
->chan
);
469 static void pasemi_mac_free_tx_resources(struct pasemi_mac
*mac
)
471 struct pasemi_mac_txring
*txring
= tx_ring(mac
);
473 struct pasemi_mac_buffer
*info
;
474 dma_addr_t dmas
[MAX_SKB_FRAGS
+1];
478 start
= txring
->next_to_clean
;
479 limit
= txring
->next_to_fill
;
481 /* Compensate for when fill has wrapped and clean has not */
483 limit
+= TX_RING_SIZE
;
485 for (i
= start
; i
< limit
; i
+= freed
) {
486 info
= &txring
->ring_info
[(i
+1) & (TX_RING_SIZE
-1)];
487 if (info
->dma
&& info
->skb
) {
488 nfrags
= skb_shinfo(info
->skb
)->nr_frags
;
489 for (j
= 0; j
<= nfrags
; j
++)
490 dmas
[j
] = txring
->ring_info
[(i
+1+j
) &
491 (TX_RING_SIZE
-1)].dma
;
492 freed
= pasemi_mac_unmap_tx_skb(mac
, nfrags
,
498 kfree(txring
->ring_info
);
499 pasemi_dma_free_chan(&txring
->chan
);
503 static void pasemi_mac_free_rx_buffers(struct pasemi_mac
*mac
)
505 struct pasemi_mac_rxring
*rx
= rx_ring(mac
);
507 struct pasemi_mac_buffer
*info
;
509 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
510 info
= &RX_DESC_INFO(rx
, i
);
511 if (info
->skb
&& info
->dma
) {
512 pci_unmap_single(mac
->dma_pdev
,
516 dev_kfree_skb_any(info
->skb
);
522 for (i
= 0; i
< RX_RING_SIZE
; i
++)
526 static void pasemi_mac_free_rx_resources(struct pasemi_mac
*mac
)
528 pasemi_mac_free_rx_buffers(mac
);
530 dma_free_coherent(&mac
->dma_pdev
->dev
, RX_RING_SIZE
* sizeof(u64
),
531 rx_ring(mac
)->buffers
, rx_ring(mac
)->buf_dma
);
533 kfree(rx_ring(mac
)->ring_info
);
534 pasemi_dma_free_chan(&rx_ring(mac
)->chan
);
538 static void pasemi_mac_replenish_rx_ring(const struct net_device
*dev
,
541 const struct pasemi_mac
*mac
= netdev_priv(dev
);
542 struct pasemi_mac_rxring
*rx
= rx_ring(mac
);
548 fill
= rx_ring(mac
)->next_to_fill
;
549 for (count
= 0; count
< limit
; count
++) {
550 struct pasemi_mac_buffer
*info
= &RX_DESC_INFO(rx
, fill
);
551 u64
*buff
= &RX_BUFF(rx
, fill
);
558 skb
= dev_alloc_skb(mac
->bufsz
);
559 skb_reserve(skb
, LOCAL_SKB_ALIGN
);
564 dma
= pci_map_single(mac
->dma_pdev
, skb
->data
,
565 mac
->bufsz
- LOCAL_SKB_ALIGN
,
568 if (unlikely(dma_mapping_error(dma
))) {
569 dev_kfree_skb_irq(info
->skb
);
575 *buff
= XCT_RXB_LEN(mac
->bufsz
) | XCT_RXB_ADDR(dma
);
581 write_dma_reg(PAS_DMA_RXINT_INCR(mac
->dma_if
), count
);
583 rx_ring(mac
)->next_to_fill
= (rx_ring(mac
)->next_to_fill
+ count
) &
587 static void pasemi_mac_restart_rx_intr(const struct pasemi_mac
*mac
)
589 struct pasemi_mac_rxring
*rx
= rx_ring(mac
);
590 unsigned int reg
, pcnt
;
591 /* Re-enable packet count interrupts: finally
592 * ack the packet count interrupt we got in rx_intr.
595 pcnt
= *rx
->chan
.status
& PAS_STATUS_PCNT_M
;
597 reg
= PAS_IOB_DMA_RXCH_RESET_PCNT(pcnt
) | PAS_IOB_DMA_RXCH_RESET_PINTC
;
599 if (*rx
->chan
.status
& PAS_STATUS_TIMER
)
600 reg
|= PAS_IOB_DMA_RXCH_RESET_TINTC
;
602 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(mac
->rx
->chan
.chno
), reg
);
605 static void pasemi_mac_restart_tx_intr(const struct pasemi_mac
*mac
)
607 unsigned int reg
, pcnt
;
609 /* Re-enable packet count interrupts */
610 pcnt
= *tx_ring(mac
)->chan
.status
& PAS_STATUS_PCNT_M
;
612 reg
= PAS_IOB_DMA_TXCH_RESET_PCNT(pcnt
) | PAS_IOB_DMA_TXCH_RESET_PINTC
;
614 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(tx_ring(mac
)->chan
.chno
), reg
);
618 static inline void pasemi_mac_rx_error(const struct pasemi_mac
*mac
,
621 unsigned int rcmdsta
, ccmdsta
;
622 struct pasemi_dmachan
*chan
= &rx_ring(mac
)->chan
;
624 if (!netif_msg_rx_err(mac
))
627 rcmdsta
= read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
));
628 ccmdsta
= read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(chan
->chno
));
630 printk(KERN_ERR
"pasemi_mac: rx error. macrx %016lx, rx status %lx\n",
631 macrx
, *chan
->status
);
633 printk(KERN_ERR
"pasemi_mac: rcmdsta %08x ccmdsta %08x\n",
637 static inline void pasemi_mac_tx_error(const struct pasemi_mac
*mac
,
641 struct pasemi_dmachan
*chan
= &tx_ring(mac
)->chan
;
643 if (!netif_msg_tx_err(mac
))
646 cmdsta
= read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(chan
->chno
));
648 printk(KERN_ERR
"pasemi_mac: tx error. mactx 0x%016lx, "\
649 "tx status 0x%016lx\n", mactx
, *chan
->status
);
651 printk(KERN_ERR
"pasemi_mac: tcmdsta 0x%08x\n", cmdsta
);
654 static int pasemi_mac_clean_rx(struct pasemi_mac_rxring
*rx
,
657 const struct pasemi_dmachan
*chan
= &rx
->chan
;
658 struct pasemi_mac
*mac
= rx
->mac
;
659 struct pci_dev
*pdev
= mac
->dma_pdev
;
661 int count
, buf_index
, tot_bytes
, packets
;
662 struct pasemi_mac_buffer
*info
;
671 spin_lock(&rx
->lock
);
673 n
= rx
->next_to_clean
;
675 prefetch(&RX_DESC(rx
, n
));
677 for (count
= 0; count
< limit
; count
++) {
678 macrx
= RX_DESC(rx
, n
);
679 prefetch(&RX_DESC(rx
, n
+4));
681 if ((macrx
& XCT_MACRX_E
) ||
682 (*chan
->status
& PAS_STATUS_ERROR
))
683 pasemi_mac_rx_error(mac
, macrx
);
685 if (!(macrx
& XCT_MACRX_O
))
690 BUG_ON(!(macrx
& XCT_MACRX_RR_8BRES
));
692 eval
= (RX_DESC(rx
, n
+1) & XCT_RXRES_8B_EVAL_M
) >>
696 dma
= (RX_DESC(rx
, n
+2) & XCT_PTR_ADDR_M
);
697 info
= &RX_DESC_INFO(rx
, buf_index
);
703 len
= (macrx
& XCT_MACRX_LLEN_M
) >> XCT_MACRX_LLEN_S
;
705 pci_unmap_single(pdev
, dma
, mac
->bufsz
- LOCAL_SKB_ALIGN
,
708 if (macrx
& XCT_MACRX_CRC
) {
709 /* CRC error flagged */
710 mac
->netdev
->stats
.rx_errors
++;
711 mac
->netdev
->stats
.rx_crc_errors
++;
712 /* No need to free skb, it'll be reused */
719 if (likely((macrx
& XCT_MACRX_HTY_M
) == XCT_MACRX_HTY_IPV4_OK
)) {
720 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
721 skb
->csum
= (macrx
& XCT_MACRX_CSUM_M
) >>
724 skb
->ip_summed
= CHECKSUM_NONE
;
729 /* Don't include CRC */
732 skb
->protocol
= eth_type_trans(skb
, mac
->netdev
);
733 lro_receive_skb(&mac
->lro_mgr
, skb
, (void *)macrx
);
737 RX_DESC(rx
, n
+1) = 0;
739 /* Need to zero it out since hardware doesn't, since the
740 * replenish loop uses it to tell when it's done.
742 RX_BUFF(rx
, buf_index
) = 0;
747 if (n
> RX_RING_SIZE
) {
748 /* Errata 5971 workaround: L2 target of headers */
749 write_iob_reg(PAS_IOB_COM_PKTHDRCNT
, 0);
750 n
&= (RX_RING_SIZE
-1);
753 rx_ring(mac
)->next_to_clean
= n
;
755 lro_flush_all(&mac
->lro_mgr
);
757 /* Increase is in number of 16-byte entries, and since each descriptor
758 * with an 8BRES takes up 3x8 bytes (padded to 4x8), increase with
761 write_dma_reg(PAS_DMA_RXCHAN_INCR(mac
->rx
->chan
.chno
), count
<< 1);
763 pasemi_mac_replenish_rx_ring(mac
->netdev
, count
);
765 mac
->netdev
->stats
.rx_bytes
+= tot_bytes
;
766 mac
->netdev
->stats
.rx_packets
+= packets
;
768 spin_unlock(&rx_ring(mac
)->lock
);
773 /* Can't make this too large or we blow the kernel stack limits */
774 #define TX_CLEAN_BATCHSIZE (128/MAX_SKB_FRAGS)
776 static int pasemi_mac_clean_tx(struct pasemi_mac_txring
*txring
)
778 struct pasemi_dmachan
*chan
= &txring
->chan
;
779 struct pasemi_mac
*mac
= txring
->mac
;
781 unsigned int start
, descr_count
, buf_count
, batch_limit
;
782 unsigned int ring_limit
;
783 unsigned int total_count
;
785 struct sk_buff
*skbs
[TX_CLEAN_BATCHSIZE
];
786 dma_addr_t dmas
[TX_CLEAN_BATCHSIZE
][MAX_SKB_FRAGS
+1];
787 int nf
[TX_CLEAN_BATCHSIZE
];
791 batch_limit
= TX_CLEAN_BATCHSIZE
;
793 spin_lock_irqsave(&txring
->lock
, flags
);
795 start
= txring
->next_to_clean
;
796 ring_limit
= txring
->next_to_fill
;
798 prefetch(&TX_DESC_INFO(txring
, start
+1).skb
);
800 /* Compensate for when fill has wrapped but clean has not */
801 if (start
> ring_limit
)
802 ring_limit
+= TX_RING_SIZE
;
808 descr_count
< batch_limit
&& i
< ring_limit
;
810 u64 mactx
= TX_DESC(txring
, i
);
813 skb
= TX_DESC_INFO(txring
, i
+1).skb
;
814 nr_frags
= TX_DESC_INFO(txring
, i
).dma
;
816 if ((mactx
& XCT_MACTX_E
) ||
817 (*chan
->status
& PAS_STATUS_ERROR
))
818 pasemi_mac_tx_error(mac
, mactx
);
820 if (unlikely(mactx
& XCT_MACTX_O
))
821 /* Not yet transmitted */
824 buf_count
= 2 + nr_frags
;
825 /* Since we always fill with an even number of entries, make
826 * sure we skip any unused one at the end as well.
831 for (j
= 0; j
<= nr_frags
; j
++)
832 dmas
[descr_count
][j
] = TX_DESC_INFO(txring
, i
+1+j
).dma
;
834 skbs
[descr_count
] = skb
;
835 nf
[descr_count
] = nr_frags
;
837 TX_DESC(txring
, i
) = 0;
838 TX_DESC(txring
, i
+1) = 0;
842 txring
->next_to_clean
= i
& (TX_RING_SIZE
-1);
844 spin_unlock_irqrestore(&txring
->lock
, flags
);
845 netif_wake_queue(mac
->netdev
);
847 for (i
= 0; i
< descr_count
; i
++)
848 pasemi_mac_unmap_tx_skb(mac
, nf
[i
], skbs
[i
], dmas
[i
]);
850 total_count
+= descr_count
;
852 /* If the batch was full, try to clean more */
853 if (descr_count
== batch_limit
)
860 static irqreturn_t
pasemi_mac_rx_intr(int irq
, void *data
)
862 const struct pasemi_mac_rxring
*rxring
= data
;
863 struct pasemi_mac
*mac
= rxring
->mac
;
864 struct net_device
*dev
= mac
->netdev
;
865 const struct pasemi_dmachan
*chan
= &rxring
->chan
;
868 if (!(*chan
->status
& PAS_STATUS_CAUSE_M
))
871 /* Don't reset packet count so it won't fire again but clear
876 if (*chan
->status
& PAS_STATUS_SOFT
)
877 reg
|= PAS_IOB_DMA_RXCH_RESET_SINTC
;
878 if (*chan
->status
& PAS_STATUS_ERROR
)
879 reg
|= PAS_IOB_DMA_RXCH_RESET_DINTC
;
881 netif_rx_schedule(dev
, &mac
->napi
);
883 write_iob_reg(PAS_IOB_DMA_RXCH_RESET(chan
->chno
), reg
);
888 #define TX_CLEAN_INTERVAL HZ
890 static void pasemi_mac_tx_timer(unsigned long data
)
892 struct pasemi_mac_txring
*txring
= (struct pasemi_mac_txring
*)data
;
893 struct pasemi_mac
*mac
= txring
->mac
;
895 pasemi_mac_clean_tx(txring
);
897 mod_timer(&txring
->clean_timer
, jiffies
+ TX_CLEAN_INTERVAL
);
899 pasemi_mac_restart_tx_intr(mac
);
902 static irqreturn_t
pasemi_mac_tx_intr(int irq
, void *data
)
904 struct pasemi_mac_txring
*txring
= data
;
905 const struct pasemi_dmachan
*chan
= &txring
->chan
;
906 struct pasemi_mac
*mac
= txring
->mac
;
909 if (!(*chan
->status
& PAS_STATUS_CAUSE_M
))
914 if (*chan
->status
& PAS_STATUS_SOFT
)
915 reg
|= PAS_IOB_DMA_TXCH_RESET_SINTC
;
916 if (*chan
->status
& PAS_STATUS_ERROR
)
917 reg
|= PAS_IOB_DMA_TXCH_RESET_DINTC
;
919 mod_timer(&txring
->clean_timer
, jiffies
+ (TX_CLEAN_INTERVAL
)*2);
921 netif_rx_schedule(mac
->netdev
, &mac
->napi
);
924 write_iob_reg(PAS_IOB_DMA_TXCH_RESET(chan
->chno
), reg
);
929 static void pasemi_adjust_link(struct net_device
*dev
)
931 struct pasemi_mac
*mac
= netdev_priv(dev
);
934 unsigned int new_flags
;
936 if (!mac
->phydev
->link
) {
937 /* If no link, MAC speed settings don't matter. Just report
938 * link down and return.
940 if (mac
->link
&& netif_msg_link(mac
))
941 printk(KERN_INFO
"%s: Link is down.\n", dev
->name
);
943 netif_carrier_off(dev
);
944 pasemi_mac_intf_disable(mac
);
949 pasemi_mac_intf_enable(mac
);
950 netif_carrier_on(dev
);
953 flags
= read_mac_reg(mac
, PAS_MAC_CFG_PCFG
);
954 new_flags
= flags
& ~(PAS_MAC_CFG_PCFG_HD
| PAS_MAC_CFG_PCFG_SPD_M
|
955 PAS_MAC_CFG_PCFG_TSR_M
);
957 if (!mac
->phydev
->duplex
)
958 new_flags
|= PAS_MAC_CFG_PCFG_HD
;
960 switch (mac
->phydev
->speed
) {
962 new_flags
|= PAS_MAC_CFG_PCFG_SPD_1G
|
963 PAS_MAC_CFG_PCFG_TSR_1G
;
966 new_flags
|= PAS_MAC_CFG_PCFG_SPD_100M
|
967 PAS_MAC_CFG_PCFG_TSR_100M
;
970 new_flags
|= PAS_MAC_CFG_PCFG_SPD_10M
|
971 PAS_MAC_CFG_PCFG_TSR_10M
;
974 printk("Unsupported speed %d\n", mac
->phydev
->speed
);
977 /* Print on link or speed/duplex change */
978 msg
= mac
->link
!= mac
->phydev
->link
|| flags
!= new_flags
;
980 mac
->duplex
= mac
->phydev
->duplex
;
981 mac
->speed
= mac
->phydev
->speed
;
982 mac
->link
= mac
->phydev
->link
;
984 if (new_flags
!= flags
)
985 write_mac_reg(mac
, PAS_MAC_CFG_PCFG
, new_flags
);
987 if (msg
&& netif_msg_link(mac
))
988 printk(KERN_INFO
"%s: Link is up at %d Mbps, %s duplex.\n",
989 dev
->name
, mac
->speed
, mac
->duplex
? "full" : "half");
992 static int pasemi_mac_phy_init(struct net_device
*dev
)
994 struct pasemi_mac
*mac
= netdev_priv(dev
);
995 struct device_node
*dn
, *phy_dn
;
996 struct phy_device
*phydev
;
999 const unsigned int *prop
;
1003 dn
= pci_device_to_OF_node(mac
->pdev
);
1004 ph
= of_get_property(dn
, "phy-handle", NULL
);
1007 phy_dn
= of_find_node_by_phandle(*ph
);
1009 prop
= of_get_property(phy_dn
, "reg", NULL
);
1010 ret
= of_address_to_resource(phy_dn
->parent
, 0, &r
);
1015 snprintf(mac
->phy_id
, BUS_ID_SIZE
, PHY_ID_FMT
, (int)r
.start
, phy_id
);
1017 of_node_put(phy_dn
);
1023 phydev
= phy_connect(dev
, mac
->phy_id
, &pasemi_adjust_link
, 0, PHY_INTERFACE_MODE_SGMII
);
1025 if (IS_ERR(phydev
)) {
1026 printk(KERN_ERR
"%s: Could not attach to phy\n", dev
->name
);
1027 return PTR_ERR(phydev
);
1030 mac
->phydev
= phydev
;
1035 of_node_put(phy_dn
);
1040 static int pasemi_mac_open(struct net_device
*dev
)
1042 struct pasemi_mac
*mac
= netdev_priv(dev
);
1046 /* enable rx section */
1047 write_dma_reg(PAS_DMA_COM_RXCMD
, PAS_DMA_COM_RXCMD_EN
);
1049 /* enable tx section */
1050 write_dma_reg(PAS_DMA_COM_TXCMD
, PAS_DMA_COM_TXCMD_EN
);
1052 flags
= PAS_MAC_CFG_TXP_FCE
| PAS_MAC_CFG_TXP_FPC(3) |
1053 PAS_MAC_CFG_TXP_SL(3) | PAS_MAC_CFG_TXP_COB(0xf) |
1054 PAS_MAC_CFG_TXP_TIFT(8) | PAS_MAC_CFG_TXP_TIFG(12);
1056 write_mac_reg(mac
, PAS_MAC_CFG_TXP
, flags
);
1058 ret
= pasemi_mac_setup_rx_resources(dev
);
1060 goto out_rx_resources
;
1062 mac
->tx
= pasemi_mac_setup_tx_resources(dev
);
1067 /* 0x3ff with 33MHz clock is about 31us */
1068 write_iob_reg(PAS_IOB_DMA_COM_TIMEOUTCFG
,
1069 PAS_IOB_DMA_COM_TIMEOUTCFG_TCNT(0x3ff));
1071 write_iob_reg(PAS_IOB_DMA_RXCH_CFG(mac
->rx
->chan
.chno
),
1072 PAS_IOB_DMA_RXCH_CFG_CNTTH(256));
1074 write_iob_reg(PAS_IOB_DMA_TXCH_CFG(mac
->tx
->chan
.chno
),
1075 PAS_IOB_DMA_TXCH_CFG_CNTTH(32));
1077 write_mac_reg(mac
, PAS_MAC_IPC_CHNL
,
1078 PAS_MAC_IPC_CHNL_DCHNO(mac
->rx
->chan
.chno
) |
1079 PAS_MAC_IPC_CHNL_BCH(mac
->rx
->chan
.chno
));
1082 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
),
1083 PAS_DMA_RXINT_RCMDSTA_EN
|
1084 PAS_DMA_RXINT_RCMDSTA_DROPS_M
|
1085 PAS_DMA_RXINT_RCMDSTA_BP
|
1086 PAS_DMA_RXINT_RCMDSTA_OO
|
1087 PAS_DMA_RXINT_RCMDSTA_BT
);
1089 /* enable rx channel */
1090 pasemi_dma_start_chan(&rx_ring(mac
)->chan
, PAS_DMA_RXCHAN_CCMDSTA_DU
|
1091 PAS_DMA_RXCHAN_CCMDSTA_OD
|
1092 PAS_DMA_RXCHAN_CCMDSTA_FD
|
1093 PAS_DMA_RXCHAN_CCMDSTA_DT
);
1095 /* enable tx channel */
1096 pasemi_dma_start_chan(&tx_ring(mac
)->chan
, PAS_DMA_TXCHAN_TCMDSTA_SZ
|
1097 PAS_DMA_TXCHAN_TCMDSTA_DB
|
1098 PAS_DMA_TXCHAN_TCMDSTA_DE
|
1099 PAS_DMA_TXCHAN_TCMDSTA_DA
);
1101 pasemi_mac_replenish_rx_ring(dev
, RX_RING_SIZE
);
1103 write_dma_reg(PAS_DMA_RXCHAN_INCR(rx_ring(mac
)->chan
.chno
),
1106 /* Clear out any residual packet count state from firmware */
1107 pasemi_mac_restart_rx_intr(mac
);
1108 pasemi_mac_restart_tx_intr(mac
);
1110 flags
= PAS_MAC_CFG_PCFG_S1
| PAS_MAC_CFG_PCFG_PR
| PAS_MAC_CFG_PCFG_CE
;
1112 if (mac
->type
== MAC_TYPE_GMAC
)
1113 flags
|= PAS_MAC_CFG_PCFG_TSR_1G
| PAS_MAC_CFG_PCFG_SPD_1G
;
1115 flags
|= PAS_MAC_CFG_PCFG_TSR_10G
| PAS_MAC_CFG_PCFG_SPD_10G
;
1117 /* Enable interface in MAC */
1118 write_mac_reg(mac
, PAS_MAC_CFG_PCFG
, flags
);
1120 ret
= pasemi_mac_phy_init(dev
);
1122 /* Since we won't get link notification, just enable RX */
1123 pasemi_mac_intf_enable(mac
);
1124 if (mac
->type
== MAC_TYPE_GMAC
) {
1125 /* Warn for missing PHY on SGMII (1Gig) ports */
1126 dev_warn(&mac
->pdev
->dev
,
1127 "PHY init failed: %d.\n", ret
);
1128 dev_warn(&mac
->pdev
->dev
,
1129 "Defaulting to 1Gbit full duplex\n");
1133 netif_start_queue(dev
);
1134 napi_enable(&mac
->napi
);
1136 snprintf(mac
->tx_irq_name
, sizeof(mac
->tx_irq_name
), "%s tx",
1139 ret
= request_irq(mac
->tx
->chan
.irq
, &pasemi_mac_tx_intr
, IRQF_DISABLED
,
1140 mac
->tx_irq_name
, mac
->tx
);
1142 dev_err(&mac
->pdev
->dev
, "request_irq of irq %d failed: %d\n",
1143 mac
->tx
->chan
.irq
, ret
);
1147 snprintf(mac
->rx_irq_name
, sizeof(mac
->rx_irq_name
), "%s rx",
1150 ret
= request_irq(mac
->rx
->chan
.irq
, &pasemi_mac_rx_intr
, IRQF_DISABLED
,
1151 mac
->rx_irq_name
, mac
->rx
);
1153 dev_err(&mac
->pdev
->dev
, "request_irq of irq %d failed: %d\n",
1154 mac
->rx
->chan
.irq
, ret
);
1159 phy_start(mac
->phydev
);
1161 init_timer(&mac
->tx
->clean_timer
);
1162 mac
->tx
->clean_timer
.function
= pasemi_mac_tx_timer
;
1163 mac
->tx
->clean_timer
.data
= (unsigned long)mac
->tx
;
1164 mac
->tx
->clean_timer
.expires
= jiffies
+HZ
;
1165 add_timer(&mac
->tx
->clean_timer
);
1170 free_irq(mac
->tx
->chan
.irq
, mac
->tx
);
1172 napi_disable(&mac
->napi
);
1173 netif_stop_queue(dev
);
1176 pasemi_mac_free_tx_resources(mac
);
1177 pasemi_mac_free_rx_resources(mac
);
1183 #define MAX_RETRIES 5000
1185 static void pasemi_mac_pause_txchan(struct pasemi_mac
*mac
)
1187 unsigned int sta
, retries
;
1188 int txch
= tx_ring(mac
)->chan
.chno
;
1190 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch
),
1191 PAS_DMA_TXCHAN_TCMDSTA_ST
);
1193 for (retries
= 0; retries
< MAX_RETRIES
; retries
++) {
1194 sta
= read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch
));
1195 if (!(sta
& PAS_DMA_TXCHAN_TCMDSTA_ACT
))
1200 if (sta
& PAS_DMA_TXCHAN_TCMDSTA_ACT
)
1201 dev_err(&mac
->dma_pdev
->dev
,
1202 "Failed to stop tx channel, tcmdsta %08x\n", sta
);
1204 write_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch
), 0);
1207 static void pasemi_mac_pause_rxchan(struct pasemi_mac
*mac
)
1209 unsigned int sta
, retries
;
1210 int rxch
= rx_ring(mac
)->chan
.chno
;
1212 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch
),
1213 PAS_DMA_RXCHAN_CCMDSTA_ST
);
1214 for (retries
= 0; retries
< MAX_RETRIES
; retries
++) {
1215 sta
= read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch
));
1216 if (!(sta
& PAS_DMA_RXCHAN_CCMDSTA_ACT
))
1221 if (sta
& PAS_DMA_RXCHAN_CCMDSTA_ACT
)
1222 dev_err(&mac
->dma_pdev
->dev
,
1223 "Failed to stop rx channel, ccmdsta 08%x\n", sta
);
1224 write_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch
), 0);
1227 static void pasemi_mac_pause_rxint(struct pasemi_mac
*mac
)
1229 unsigned int sta
, retries
;
1231 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
),
1232 PAS_DMA_RXINT_RCMDSTA_ST
);
1233 for (retries
= 0; retries
< MAX_RETRIES
; retries
++) {
1234 sta
= read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
));
1235 if (!(sta
& PAS_DMA_RXINT_RCMDSTA_ACT
))
1240 if (sta
& PAS_DMA_RXINT_RCMDSTA_ACT
)
1241 dev_err(&mac
->dma_pdev
->dev
,
1242 "Failed to stop rx interface, rcmdsta %08x\n", sta
);
1243 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
), 0);
1246 static int pasemi_mac_close(struct net_device
*dev
)
1248 struct pasemi_mac
*mac
= netdev_priv(dev
);
1252 rxch
= rx_ring(mac
)->chan
.chno
;
1253 txch
= tx_ring(mac
)->chan
.chno
;
1256 phy_stop(mac
->phydev
);
1257 phy_disconnect(mac
->phydev
);
1260 del_timer_sync(&mac
->tx
->clean_timer
);
1262 netif_stop_queue(dev
);
1263 napi_disable(&mac
->napi
);
1265 sta
= read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
));
1266 if (sta
& (PAS_DMA_RXINT_RCMDSTA_BP
|
1267 PAS_DMA_RXINT_RCMDSTA_OO
|
1268 PAS_DMA_RXINT_RCMDSTA_BT
))
1269 printk(KERN_DEBUG
"pasemi_mac: rcmdsta error: 0x%08x\n", sta
);
1271 sta
= read_dma_reg(PAS_DMA_RXCHAN_CCMDSTA(rxch
));
1272 if (sta
& (PAS_DMA_RXCHAN_CCMDSTA_DU
|
1273 PAS_DMA_RXCHAN_CCMDSTA_OD
|
1274 PAS_DMA_RXCHAN_CCMDSTA_FD
|
1275 PAS_DMA_RXCHAN_CCMDSTA_DT
))
1276 printk(KERN_DEBUG
"pasemi_mac: ccmdsta error: 0x%08x\n", sta
);
1278 sta
= read_dma_reg(PAS_DMA_TXCHAN_TCMDSTA(txch
));
1279 if (sta
& (PAS_DMA_TXCHAN_TCMDSTA_SZ
| PAS_DMA_TXCHAN_TCMDSTA_DB
|
1280 PAS_DMA_TXCHAN_TCMDSTA_DE
| PAS_DMA_TXCHAN_TCMDSTA_DA
))
1281 printk(KERN_DEBUG
"pasemi_mac: tcmdsta error: 0x%08x\n", sta
);
1283 /* Clean out any pending buffers */
1284 pasemi_mac_clean_tx(tx_ring(mac
));
1285 pasemi_mac_clean_rx(rx_ring(mac
), RX_RING_SIZE
);
1287 pasemi_mac_pause_txchan(mac
);
1288 pasemi_mac_pause_rxint(mac
);
1289 pasemi_mac_pause_rxchan(mac
);
1291 free_irq(mac
->tx
->chan
.irq
, mac
->tx
);
1292 free_irq(mac
->rx
->chan
.irq
, mac
->rx
);
1294 /* Free resources */
1295 pasemi_mac_free_rx_resources(mac
);
1296 pasemi_mac_free_tx_resources(mac
);
1301 static int pasemi_mac_start_tx(struct sk_buff
*skb
, struct net_device
*dev
)
1303 struct pasemi_mac
*mac
= netdev_priv(dev
);
1304 struct pasemi_mac_txring
*txring
;
1306 dma_addr_t map
[MAX_SKB_FRAGS
+1];
1307 unsigned int map_size
[MAX_SKB_FRAGS
+1];
1308 unsigned long flags
;
1312 dflags
= XCT_MACTX_O
| XCT_MACTX_ST
| XCT_MACTX_CRC_PAD
;
1314 if (skb
->ip_summed
== CHECKSUM_PARTIAL
) {
1315 const unsigned char *nh
= skb_network_header(skb
);
1317 switch (ip_hdr(skb
)->protocol
) {
1319 dflags
|= XCT_MACTX_CSUM_TCP
;
1320 dflags
|= XCT_MACTX_IPH(skb_network_header_len(skb
) >> 2);
1321 dflags
|= XCT_MACTX_IPO(nh
- skb
->data
);
1324 dflags
|= XCT_MACTX_CSUM_UDP
;
1325 dflags
|= XCT_MACTX_IPH(skb_network_header_len(skb
) >> 2);
1326 dflags
|= XCT_MACTX_IPO(nh
- skb
->data
);
1331 nfrags
= skb_shinfo(skb
)->nr_frags
;
1333 map
[0] = pci_map_single(mac
->dma_pdev
, skb
->data
, skb_headlen(skb
),
1335 map_size
[0] = skb_headlen(skb
);
1336 if (dma_mapping_error(map
[0]))
1337 goto out_err_nolock
;
1339 for (i
= 0; i
< nfrags
; i
++) {
1340 skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[i
];
1342 map
[i
+1] = pci_map_page(mac
->dma_pdev
, frag
->page
,
1343 frag
->page_offset
, frag
->size
,
1345 map_size
[i
+1] = frag
->size
;
1346 if (dma_mapping_error(map
[i
+1])) {
1348 goto out_err_nolock
;
1352 mactx
= dflags
| XCT_MACTX_LLEN(skb
->len
);
1354 txring
= tx_ring(mac
);
1356 spin_lock_irqsave(&txring
->lock
, flags
);
1358 fill
= txring
->next_to_fill
;
1360 /* Avoid stepping on the same cache line that the DMA controller
1361 * is currently about to send, so leave at least 8 words available.
1362 * Total free space needed is mactx + fragments + 8
1364 if (RING_AVAIL(txring
) < nfrags
+ 10) {
1365 /* no room -- stop the queue and wait for tx intr */
1366 netif_stop_queue(dev
);
1370 TX_DESC(txring
, fill
) = mactx
;
1371 TX_DESC_INFO(txring
, fill
).dma
= nfrags
;
1373 TX_DESC_INFO(txring
, fill
).skb
= skb
;
1374 for (i
= 0; i
<= nfrags
; i
++) {
1375 TX_DESC(txring
, fill
+i
) =
1376 XCT_PTR_LEN(map_size
[i
]) | XCT_PTR_ADDR(map
[i
]);
1377 TX_DESC_INFO(txring
, fill
+i
).dma
= map
[i
];
1380 /* We have to add an even number of 8-byte entries to the ring
1381 * even if the last one is unused. That means always an odd number
1382 * of pointers + one mactx descriptor.
1387 txring
->next_to_fill
= (fill
+ nfrags
+ 1) & (TX_RING_SIZE
-1);
1389 dev
->stats
.tx_packets
++;
1390 dev
->stats
.tx_bytes
+= skb
->len
;
1392 spin_unlock_irqrestore(&txring
->lock
, flags
);
1394 write_dma_reg(PAS_DMA_TXCHAN_INCR(txring
->chan
.chno
), (nfrags
+2) >> 1);
1396 return NETDEV_TX_OK
;
1399 spin_unlock_irqrestore(&txring
->lock
, flags
);
1402 pci_unmap_single(mac
->dma_pdev
, map
[nfrags
], map_size
[nfrags
],
1405 return NETDEV_TX_BUSY
;
1408 static void pasemi_mac_set_rx_mode(struct net_device
*dev
)
1410 const struct pasemi_mac
*mac
= netdev_priv(dev
);
1413 flags
= read_mac_reg(mac
, PAS_MAC_CFG_PCFG
);
1415 /* Set promiscuous */
1416 if (dev
->flags
& IFF_PROMISC
)
1417 flags
|= PAS_MAC_CFG_PCFG_PR
;
1419 flags
&= ~PAS_MAC_CFG_PCFG_PR
;
1421 write_mac_reg(mac
, PAS_MAC_CFG_PCFG
, flags
);
1425 static int pasemi_mac_poll(struct napi_struct
*napi
, int budget
)
1427 struct pasemi_mac
*mac
= container_of(napi
, struct pasemi_mac
, napi
);
1428 struct net_device
*dev
= mac
->netdev
;
1431 pasemi_mac_clean_tx(tx_ring(mac
));
1432 pkts
= pasemi_mac_clean_rx(rx_ring(mac
), budget
);
1433 if (pkts
< budget
) {
1434 /* all done, no more packets present */
1435 netif_rx_complete(dev
, napi
);
1437 pasemi_mac_restart_rx_intr(mac
);
1438 pasemi_mac_restart_tx_intr(mac
);
1443 static int pasemi_mac_change_mtu(struct net_device
*dev
, int new_mtu
)
1445 struct pasemi_mac
*mac
= netdev_priv(dev
);
1447 unsigned int rcmdsta
;
1450 if (new_mtu
< PE_MIN_MTU
|| new_mtu
> PE_MAX_MTU
)
1453 running
= netif_running(dev
);
1456 /* Need to stop the interface, clean out all already
1457 * received buffers, free all unused buffers on the RX
1458 * interface ring, then finally re-fill the rx ring with
1459 * the new-size buffers and restart.
1462 napi_disable(&mac
->napi
);
1463 netif_tx_disable(dev
);
1464 pasemi_mac_intf_disable(mac
);
1466 rcmdsta
= read_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
));
1467 pasemi_mac_pause_rxint(mac
);
1468 pasemi_mac_clean_rx(rx_ring(mac
), RX_RING_SIZE
);
1469 pasemi_mac_free_rx_buffers(mac
);
1472 /* Change maxf, i.e. what size frames are accepted.
1473 * Need room for ethernet header and CRC word
1475 reg
= read_mac_reg(mac
, PAS_MAC_CFG_MACCFG
);
1476 reg
&= ~PAS_MAC_CFG_MACCFG_MAXF_M
;
1477 reg
|= PAS_MAC_CFG_MACCFG_MAXF(new_mtu
+ ETH_HLEN
+ 4);
1478 write_mac_reg(mac
, PAS_MAC_CFG_MACCFG
, reg
);
1481 /* MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1482 mac
->bufsz
= new_mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ LOCAL_SKB_ALIGN
+ 128;
1485 write_dma_reg(PAS_DMA_RXINT_RCMDSTA(mac
->dma_if
),
1486 rcmdsta
| PAS_DMA_RXINT_RCMDSTA_EN
);
1488 rx_ring(mac
)->next_to_fill
= 0;
1489 pasemi_mac_replenish_rx_ring(dev
, RX_RING_SIZE
-1);
1491 napi_enable(&mac
->napi
);
1492 netif_start_queue(dev
);
1493 pasemi_mac_intf_enable(mac
);
1499 static int __devinit
1500 pasemi_mac_probe(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
1502 struct net_device
*dev
;
1503 struct pasemi_mac
*mac
;
1505 DECLARE_MAC_BUF(mac_buf
);
1507 err
= pci_enable_device(pdev
);
1511 dev
= alloc_etherdev(sizeof(struct pasemi_mac
));
1514 "pasemi_mac: Could not allocate ethernet device.\n");
1516 goto out_disable_device
;
1519 pci_set_drvdata(pdev
, dev
);
1520 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1522 mac
= netdev_priv(dev
);
1527 netif_napi_add(dev
, &mac
->napi
, pasemi_mac_poll
, 64);
1529 dev
->features
= NETIF_F_IP_CSUM
| NETIF_F_LLTX
| NETIF_F_SG
|
1532 mac
->lro_mgr
.max_aggr
= LRO_MAX_AGGR
;
1533 mac
->lro_mgr
.max_desc
= MAX_LRO_DESCRIPTORS
;
1534 mac
->lro_mgr
.lro_arr
= mac
->lro_desc
;
1535 mac
->lro_mgr
.get_skb_header
= get_skb_hdr
;
1536 mac
->lro_mgr
.features
= LRO_F_NAPI
| LRO_F_EXTRACT_VLAN_ID
;
1537 mac
->lro_mgr
.dev
= mac
->netdev
;
1538 mac
->lro_mgr
.ip_summed
= CHECKSUM_UNNECESSARY
;
1539 mac
->lro_mgr
.ip_summed_aggr
= CHECKSUM_UNNECESSARY
;
1542 mac
->dma_pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa007, NULL
);
1543 if (!mac
->dma_pdev
) {
1544 dev_err(&mac
->pdev
->dev
, "Can't find DMA Controller\n");
1549 mac
->iob_pdev
= pci_get_device(PCI_VENDOR_ID_PASEMI
, 0xa001, NULL
);
1550 if (!mac
->iob_pdev
) {
1551 dev_err(&mac
->pdev
->dev
, "Can't find I/O Bridge\n");
1556 /* get mac addr from device tree */
1557 if (pasemi_get_mac_addr(mac
) || !is_valid_ether_addr(mac
->mac_addr
)) {
1561 memcpy(dev
->dev_addr
, mac
->mac_addr
, sizeof(mac
->mac_addr
));
1563 mac
->dma_if
= mac_to_intf(mac
);
1564 if (mac
->dma_if
< 0) {
1565 dev_err(&mac
->pdev
->dev
, "Can't map DMA interface\n");
1570 switch (pdev
->device
) {
1572 mac
->type
= MAC_TYPE_GMAC
;
1575 mac
->type
= MAC_TYPE_XAUI
;
1582 dev
->open
= pasemi_mac_open
;
1583 dev
->stop
= pasemi_mac_close
;
1584 dev
->hard_start_xmit
= pasemi_mac_start_tx
;
1585 dev
->set_multicast_list
= pasemi_mac_set_rx_mode
;
1586 dev
->set_mac_address
= pasemi_mac_set_mac_addr
;
1587 dev
->mtu
= PE_DEF_MTU
;
1588 /* 1500 MTU + ETH_HLEN + VLAN_HLEN + 2 64B cachelines */
1589 mac
->bufsz
= dev
->mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ LOCAL_SKB_ALIGN
+ 128;
1591 dev
->change_mtu
= pasemi_mac_change_mtu
;
1596 mac
->msg_enable
= netif_msg_init(debug
, DEFAULT_MSG_ENABLE
);
1598 /* Enable most messages by default */
1599 mac
->msg_enable
= (NETIF_MSG_IFUP
<< 1 ) - 1;
1601 err
= register_netdev(dev
);
1604 dev_err(&mac
->pdev
->dev
, "register_netdev failed with error %d\n",
1607 } else if netif_msg_probe(mac
)
1608 printk(KERN_INFO
"%s: PA Semi %s: intf %d, hw addr %s\n",
1609 dev
->name
, mac
->type
== MAC_TYPE_GMAC
? "GMAC" : "XAUI",
1610 mac
->dma_if
, print_mac(mac_buf
, dev
->dev_addr
));
1616 pci_dev_put(mac
->iob_pdev
);
1618 pci_dev_put(mac
->dma_pdev
);
1622 pci_disable_device(pdev
);
1627 static void __devexit
pasemi_mac_remove(struct pci_dev
*pdev
)
1629 struct net_device
*netdev
= pci_get_drvdata(pdev
);
1630 struct pasemi_mac
*mac
;
1635 mac
= netdev_priv(netdev
);
1637 unregister_netdev(netdev
);
1639 pci_disable_device(pdev
);
1640 pci_dev_put(mac
->dma_pdev
);
1641 pci_dev_put(mac
->iob_pdev
);
1643 pasemi_dma_free_chan(&mac
->tx
->chan
);
1644 pasemi_dma_free_chan(&mac
->rx
->chan
);
1646 pci_set_drvdata(pdev
, NULL
);
1647 free_netdev(netdev
);
1650 static struct pci_device_id pasemi_mac_pci_tbl
[] = {
1651 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI
, 0xa005) },
1652 { PCI_DEVICE(PCI_VENDOR_ID_PASEMI
, 0xa006) },
1656 MODULE_DEVICE_TABLE(pci
, pasemi_mac_pci_tbl
);
1658 static struct pci_driver pasemi_mac_driver
= {
1659 .name
= "pasemi_mac",
1660 .id_table
= pasemi_mac_pci_tbl
,
1661 .probe
= pasemi_mac_probe
,
1662 .remove
= __devexit_p(pasemi_mac_remove
),
1665 static void __exit
pasemi_mac_cleanup_module(void)
1667 pci_unregister_driver(&pasemi_mac_driver
);
1670 int pasemi_mac_init_module(void)
1674 err
= pasemi_dma_init();
1678 return pci_register_driver(&pasemi_mac_driver
);
1681 module_init(pasemi_mac_init_module
);
1682 module_exit(pasemi_mac_cleanup_module
);