25 #include <linux/compiler.h>
29 #include <linux/ethtool.h>
31 #include <linux/if_vlan.h>
38 #include <linux/kernel.h>
39 #include <linux/mii.h>
40 #include <linux/module.h>
41 #include <linux/netdevice.h>
42 #include <linux/device.h>
43 #include <linux/pci.h>
45 #include <linux/tcp.h>
46 #include <linux/types.h>
48 #include <linux/wait.h>
50 #include <linux/bitops.h>
51 #include <asm/pci-bridge.h>
129 spider_net_write_phy(
struct net_device *netdev,
int mii_id,
135 writevalue = ((
u32)mii_id << 21) |
136 ((
u32)reg << 16) | ((
u32)val);
158 readvalue = ((
u32)mii_id << 21) | ((
u32)reg << 16);
202 phy->
def->ops->setup_aneg(phy, advertise);
276 spider_net_get_mac_address(
struct net_device *netdev)
284 netdev->
dev_addr[0] = (macu >> 24) & 0xff;
285 netdev->
dev_addr[1] = (macu >> 16) & 0xff;
286 netdev->
dev_addr[2] = (macu >> 8) & 0xff;
288 netdev->
dev_addr[4] = (macl >> 8) & 0xff;
291 if (!is_valid_ether_addr(&netdev->
dev_addr[0]))
324 descr->
hwdescr->next_descr_addr = 0;
326 }
while (descr != chain->
ring);
366 for (i=0; i < chain->
num_desc; i++, descr++, hwdescr++) {
370 descr->hwdescr = hwdescr;
371 descr->bus_addr =
buf;
372 descr->next = descr + 1;
373 descr->prev = descr - 1;
379 chain->
ring->prev = descr-1;
401 pci_unmap_single(card->
pdev, descr->
hwdescr->buf_addr,
404 dev_kfree_skb(descr->
skb);
408 }
while (descr != card->
rx_chain.head);
437 descr->
skb = netdev_alloc_skb(card->
netdev,
442 "Not enough memory to allocate rx buffer\n");
452 offset = ((
unsigned long)descr->
skb->data) &
457 buf = pci_map_single(card->
pdev, descr->
skb->data,
459 if (pci_dma_mapping_error(card->
pdev, buf)) {
463 dev_err(&card->
netdev->dev,
"Could not iommu-map rx buffer\n");
540 while (spider_net_get_descr_status(chain->
head->hwdescr) ==
542 if (spider_net_prepare_rx_descr(card, chain->
head))
547 spin_unlock_irqrestore(&chain->
lock, flags);
565 descr->
prev->hwdescr->next_descr_addr = descr->
bus_addr;
567 }
while (descr != start);
572 if (spider_net_prepare_rx_descr(card, chain->
head))
579 spider_net_refill_rx_chain(card);
580 spider_net_enable_rxdmac(card);
584 spider_net_free_rx_chain_contents(card);
602 char addr_for_crc[
ETH_ALEN] = { 0, };
605 for (i = 0; i <
ETH_ALEN * 8; i++) {
606 bit = (addr[i / 8] >> (i % 8)) & 1;
607 addr_for_crc[ETH_ALEN - 1 - i / 8] += bit << (7 - (i % 8));
629 spider_net_set_multi(
struct net_device *netdev)
639 spider_net_set_promisc(card);
653 hash = spider_net_get_multicast_hash(netdev, ha->
addr);
697 if (pci_dma_mapping_error(card->
pdev, buf)) {
699 dev_err(&card->
netdev->dev,
"could not iommu-map packet (%p, %i). "
700 "Dropping packet\n", skb->
data, skb->
len);
707 if (descr->
next == chain->
tail->prev) {
708 spin_unlock_irqrestore(&chain->
lock, flags);
723 spin_unlock_irqrestore(&chain->
lock, flags);
737 descr->
prev->hwdescr->next_descr_addr = descr->
bus_addr;
755 while (descr != card->
tx_chain.head) {
764 if (cnt < card->tx_chain.num_desc/4)
782 spin_unlock_irqrestore(&card->
tx_chain.lock, flags);
813 spin_unlock_irqrestore(&chain->
lock, flags);
819 status = spider_net_get_descr_status(hwdescr);
822 dev->
stats.tx_packets++;
823 dev->
stats.tx_bytes += descr->
skb->len;
828 spin_unlock_irqrestore(&chain->
lock, flags);
841 "with status x%02x\n", status);
842 dev->
stats.tx_errors++;
846 dev->
stats.tx_dropped++;
848 spin_unlock_irqrestore(&chain->
lock, flags);
858 spin_unlock_irqrestore(&chain->
lock, flags);
862 pci_unmap_single(card->
pdev, buf_addr, skb->
len,
892 if (spider_net_get_descr_status(descr->
hwdescr) ==
922 spider_net_release_tx_chain(card, 0);
924 if (spider_net_prepare_tx_descr(card, skb) != 0) {
925 netdev->
stats.tx_dropped++;
926 netif_stop_queue(netdev);
930 cnt = spider_net_set_low_watermark(card);
932 spider_net_kick_tx_dma(card);
948 if ((spider_net_release_tx_chain(card, 0) != 0) &&
950 spider_net_kick_tx_dma(card);
951 netif_wake_queue(card->
netdev);
995 #define SPIDER_MISALIGN 2
1000 skb_checksum_none_assert(skb);
1003 SPIDER_NET_DATA_STATUS_CKSUM_MASK) &&
1013 netdev->
stats.rx_packets++;
1014 netdev->
stats.rx_bytes += skb->
len;
1032 int off = start - chain->
ring;
1035 dev_info(dev,
"Total number of descrs=%d\n",
1037 dev_info(dev,
"Chain tail located at descr=%d, status=0x%x\n",
1047 off = descr - chain->
ring;
1050 if (descr == chain->
head)
1051 dev_info(dev,
"Chain head is at %d, head status=0x%x\n",
1055 dev_info(dev,
"HW curr desc (GDACTDPA) is at %d, status=0x%x\n",
1059 dev_info(dev,
"HW next desc (GDACNEXTDA) is at %d, status=0x%x\n",
1063 dev_info(dev,
"chain is cut at %d\n", off);
1065 if (cstat != status) {
1068 dev_info(dev,
"Have %d (from %d to %d) descrs "
1069 "with stat=0x%08x\n", cnt, from, to, cstat);
1076 descr = descr->
next;
1077 }
while (descr != start);
1079 dev_info(dev,
"Last %d descrs with stat=0x%08x "
1080 "for a total of %d descrs\n", cnt, cstat, tot);
1088 status = spider_net_get_descr_status(hwd);
1089 cnt = descr - chain->
ring;
1090 dev_info(dev,
"Descr %d stat=0x%08x skb=%p\n",
1091 cnt, status, descr->
skb);
1092 dev_info(dev,
"bus addr=%08x buf addr=%08x sz=%d\n",
1094 dev_info(dev,
"next=%08x result sz=%d valid sz=%d\n",
1097 dev_info(dev,
"dmac=%08x data stat=%08x data err=%08x\n",
1102 descr = descr->
next;
1103 }
while (descr != start);
1119 unsigned long flags;
1125 descr = chain->
head;
1126 status = spider_net_get_descr_status(descr->
hwdescr);
1133 descr = chain->
head;
1134 status = spider_net_get_descr_status(descr->
hwdescr);
1135 for (i=0; i<chain->
num_desc; i++) {
1137 descr = descr->
next;
1138 status = spider_net_get_descr_status(descr->
hwdescr);
1142 spin_unlock_irqrestore(&chain->
lock, flags);
1152 descr = chain->
tail;
1153 status = spider_net_get_descr_status(descr->
hwdescr);
1155 for (i=0; i<chain->
num_desc; i++) {
1158 descr = descr->
next;
1159 status = spider_net_get_descr_status(descr->
hwdescr);
1163 if ((i == chain->
num_desc) || (i == 0))
1189 status = spider_net_get_descr_status(hwdescr);
1202 pci_unmap_single(card->
pdev, hw_buf_addr,
1210 "dropping RX descriptor with state %d\n", status);
1211 dev->
stats.rx_dropped++;
1219 "RX descriptor with unknown state %d\n", status);
1228 "error in received descriptor found, "
1229 "data_status=x%08x, data_error=x%08x\n",
1235 dev_err(&card->
netdev->dev,
"bad status, cmd_status=x%08x\n",
1237 pr_err(
"buf_addr=x%08x\n", hw_buf_addr);
1251 spider_net_pass_skb_up(descr, card);
1258 show_rx_chain(card);
1280 int packets_done = 0;
1282 while (packets_done < budget) {
1283 if (!spider_net_decode_one_descr(card))
1289 if ((packets_done == 0) && (card->
num_rx_ints != 0)) {
1290 if (!spider_net_resync_tail_ptr(card))
1291 packets_done = budget;
1292 spider_net_resync_head_ptr(card);
1296 spider_net_refill_rx_chain(card);
1297 spider_net_enable_rxdmac(card);
1299 spider_net_cleanup_tx_ring(card);
1303 if (packets_done < budget) {
1305 spider_net_rx_irq_on(card);
1309 return packets_done;
1320 spider_net_change_mtu(
struct net_device *netdev,
int new_mtu)
1327 netdev->
mtu = new_mtu;
1340 spider_net_set_mac(
struct net_device *netdev,
void *
p)
1343 u32 macl, macu, regvalue;
1346 if (!is_valid_ether_addr(addr->
sa_data))
1351 regvalue &= ~((1 << 5) | (1 << 6));
1363 regvalue |= ((1 << 5) | (1 << 6));
1366 spider_net_set_promisc(card);
1369 if (spider_net_get_mac_address(netdev))
1386 spider_net_link_reset(
struct net_device *netdev)
1401 spider_net_setup_aneg(card);
1416 u32 error_reg1,
u32 error_reg2)
1423 for (i = 0; i < 32; i++)
1424 if (status_reg & (1<<i))
1510 for (i = 0; i < 32; i++)
1511 if (error_reg1 & (1<<i))
1527 spider_net_resync_head_ptr(card);
1528 spider_net_refill_rx_chain(card);
1529 spider_net_enable_rxdmac(card);
1531 napi_schedule(&card->
napi);
1547 spider_net_resync_head_ptr(card);
1548 spider_net_refill_rx_chain(card);
1549 spider_net_enable_rxdmac(card);
1551 napi_schedule(&card->
napi);
1561 spider_net_resync_head_ptr(card);
1562 spider_net_refill_rx_chain(card);
1563 spider_net_enable_rxdmac(card);
1565 napi_schedule(&card->
napi);
1587 for (i = 0; i < 32; i++)
1588 if (error_reg2 & (1<<i))
1621 dev_err(&card->
netdev->dev,
"Error interrupt, GHIINT0STS = 0x%08x, "
1622 "GHIINT1STS = 0x%08x, GHIINT2STS = 0x%08x\n",
1623 status_reg, error_reg1, error_reg2);
1642 spider_net_interrupt(
int irq,
void *
ptr)
1646 u32 status_reg, error_reg1, error_reg2;
1658 spider_net_rx_irq_off(card);
1659 napi_schedule(&card->
napi);
1663 napi_schedule(&card->
napi);
1666 spider_net_link_reset(netdev);
1669 spider_net_handle_error_irq(card, status_reg,
1670 error_reg1, error_reg2);
1678 #ifdef CONFIG_NET_POLL_CONTROLLER
1686 spider_net_poll_controller(
struct net_device *netdev)
1689 spider_net_interrupt(netdev->
irq, netdev);
1704 SPIDER_NET_INT0_MASK_VALUE);
1706 SPIDER_NET_INT1_MASK_VALUE);
1708 SPIDER_NET_INT2_MASK_VALUE);
1746 spider_net_disable_interrupts(card);
1803 while (regs[i][0]) {
1804 spider_net_write_reg(card, regs[i][0], regs[i][1]);
1809 for (i = 1; i <= 14; i++) {
1810 spider_net_write_reg(card,
1813 spider_net_write_reg(card,
1824 spider_net_enable_rxchtails(card);
1825 spider_net_enable_rxdmac(card);
1848 const void *firmware_ptr)
1851 const u32 *fw_ptr = firmware_ptr;
1859 spider_net_write_reg(card,
1863 sequencer * 8, *fw_ptr);
1908 const u8 *fw_prop =
NULL;
1917 "Incorrect size of spidernet firmware in " \
1918 "filesystem. Looking in host firmware...\n");
1921 err = spider_net_download_firmware(card, firmware->
data);
1931 dn = pci_device_to_OF_node(card->
pdev);
1942 "Incorrect size of spidernet firmware in host firmware\n");
1946 err = spider_net_download_firmware(card, fw_prop);
1953 "Couldn't find spidernet firmware in filesystem " \
1954 "or host firmware\n");
1973 result = spider_net_init_firmware(card);
1975 goto init_firmware_failed;
1980 spider_net_setup_aneg(card);
1981 if (card->
phy.def->phy_id)
1984 result = spider_net_init_chain(card, &card->
tx_chain);
1986 goto alloc_tx_failed;
1989 result = spider_net_init_chain(card, &card->
rx_chain);
1991 goto alloc_rx_failed;
1994 if (spider_net_alloc_rx_skbs(card))
1995 goto alloc_skbs_failed;
1997 spider_net_set_multi(netdev);
2004 goto register_int_failed;
2006 spider_net_enable_card(card);
2008 netif_start_queue(netdev);
2010 napi_enable(&card->
napi);
2012 spider_net_enable_interrupts(card);
2016 register_int_failed:
2017 spider_net_free_rx_chain_contents(card);
2019 spider_net_free_chain(card, &card->
rx_chain);
2021 spider_net_free_chain(card, &card->
tx_chain);
2024 init_firmware_failed:
2033 static void spider_net_link_phy(
unsigned long data)
2041 pr_debug(
"%s: link is down trying to bring it up\n",
2047 if (phy->
def->ops->enable_fiber)
2048 phy->
def->ops->enable_fiber(phy, 1);
2054 if (phy->
def->ops->enable_fiber)
2055 phy->
def->ops->enable_fiber(phy, 0);
2062 spider_net_setup_aneg(card);
2073 if (!(phy->
def->ops->poll_link(phy))) {
2080 phy->
def->ops->read_link(phy);
2086 if (phy->
speed == 1000)
2093 pr_info(
"%s: link up, %i Mbps, %s-duplex %sautoneg.\n",
2095 phy->
duplex == 1 ?
"Full" :
"Half",
2096 phy->
autoneg == 1 ?
"" :
"no ");
2124 if (
id != 0x0000 &&
id != 0xffff) {
2144 int i, sequencer = 0;
2154 sequencer * 8, 0x0);
2157 sequencer * 8, 0x0);
2180 napi_disable(&card->
napi);
2182 netif_stop_queue(netdev);
2186 spider_net_disable_interrupts(card);
2194 spider_net_disable_rxdmac(card);
2197 spider_net_release_tx_chain(card, 1);
2198 spider_net_free_rx_chain_contents(card);
2200 spider_net_free_chain(card, &card->
tx_chain);
2201 spider_net_free_chain(card, &card->
rx_chain);
2226 spider_net_workaround_rxramfull(card);
2227 spider_net_init_card(card);
2229 if (spider_net_setup_phy(card))
2233 spider_net_kick_tx_dma(card);
2247 spider_net_tx_timeout(
struct net_device *netdev)
2251 card = netdev_priv(netdev);
2263 .ndo_start_xmit = spider_net_xmit,
2264 .ndo_set_rx_mode = spider_net_set_multi,
2265 .ndo_set_mac_address = spider_net_set_mac,
2266 .ndo_change_mtu = spider_net_change_mtu,
2267 .ndo_do_ioctl = spider_net_do_ioctl,
2268 .ndo_tx_timeout = spider_net_tx_timeout,
2271 #ifdef CONFIG_NET_POLL_CONTROLLER
2273 .ndo_poll_controller = spider_net_poll_controller,
2284 spider_net_setup_netdev_ops(
struct net_device *netdev)
2311 pci_set_drvdata(card->
pdev, netdev);
2315 (
void (*)(
unsigned long)) spider_net_cleanup_tx_ring;
2317 netdev->
irq = card->
pdev->irq;
2321 card->
aneg_timer.function = spider_net_link_phy;
2327 spider_net_setup_netdev_ops(netdev);
2336 netdev->
irq = card->
pdev->irq;
2340 dn = pci_device_to_OF_node(card->
pdev);
2349 result = spider_net_set_mac(netdev, &addr);
2352 "Failed to set MAC address: %i\n", result);
2358 "Couldn't register net_device: %i\n", result);
2363 pr_info(
"Initialized device %s.\n", netdev->
name);
2376 spider_net_alloc_card(
void)
2384 netdev = alloc_etherdev(alloc_size);
2388 card = netdev_priv(netdev);
2395 card->
rx_chain.num_desc = rx_descriptors;
2397 card->
tx_chain.num_desc = tx_descriptors;
2432 unsigned long mmio_start, mmio_len;
2435 dev_err(&pdev->
dev,
"Couldn't enable PCI device\n");
2441 "Couldn't find proper PCI device base address.\n");
2442 goto out_disable_dev;
2447 "Couldn't obtain PCI resources, aborting.\n");
2448 goto out_disable_dev;
2453 card = spider_net_alloc_card();
2456 "Couldn't allocate net_device structure, aborting.\n");
2457 goto out_release_regions;
2465 card->
netdev->mem_start = mmio_start;
2466 card->
netdev->mem_end = mmio_start + mmio_len;
2471 "Couldn't obtain PCI resources, aborting.\n");
2472 goto out_release_regions;
2477 out_release_regions:
2481 pci_set_drvdata(pdev,
NULL);
2501 card = spider_net_setup_pci_dev(pdev);
2505 spider_net_workaround_rxramfull(card);
2506 spider_net_init_card(card);
2508 err = spider_net_setup_phy(card);
2512 err = spider_net_setup_netdev(card);
2519 spider_net_undo_pci_setup(card);
2535 spider_net_remove(
struct pci_dev *pdev)
2540 netdev = pci_get_drvdata(pdev);
2541 card = netdev_priv(netdev);
2554 spider_net_undo_pci_setup(card);
2558 static struct pci_driver spider_net_driver = {
2560 .id_table = spider_net_pci_tbl,
2561 .probe = spider_net_probe,
2570 static int __init spider_net_init(
void)
2576 pr_info(
"adjusting rx descriptors to %i.\n", rx_descriptors);
2580 pr_info(
"adjusting rx descriptors to %i.\n", rx_descriptors);
2584 pr_info(
"adjusting tx descriptors to %i.\n", tx_descriptors);
2588 pr_info(
"adjusting tx descriptors to %i.\n", tx_descriptors);
2591 return pci_register_driver(&spider_net_driver);
2599 static void __exit spider_net_cleanup(
void)