devices/8139too-2.6.13-ethercat.c
changeset 573 cdee4ea90ce9
parent 535 4c8edc04fe24
child 639 aa23c48dca2d
equal deleted inserted replaced
572:aef7ea866a41 573:cdee4ea90ce9
   151 #include <linux/crc32.h>
   151 #include <linux/crc32.h>
   152 #include <asm/io.h>
   152 #include <asm/io.h>
   153 #include <asm/uaccess.h>
   153 #include <asm/uaccess.h>
   154 #include <asm/irq.h>
   154 #include <asm/irq.h>
   155 
   155 
   156 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
   157 
       
   158 #include "../globals.h"
   156 #include "../globals.h"
   159 #include "ecdev.h"
   157 #include "ecdev.h"
   160 
   158 
   161 #define RTL8139_DRIVER_NAME DRV_NAME \
   159 #define RTL8139_DRIVER_NAME DRV_NAME \
   162                             " EtherCAT-capable Fast Ethernet driver " \
   160                             " EtherCAT-capable Fast Ethernet driver " \
   163                             DRV_VERSION ", master " EC_MASTER_VERSION
   161                             DRV_VERSION ", master " EC_MASTER_VERSION
   164 
       
   165 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
   166 
   162 
   167 #define PFX DRV_NAME ": "
   163 #define PFX DRV_NAME ": "
   168 
   164 
   169 /* Default Message level */
   165 /* Default Message level */
   170 #define RTL8139_DEF_MSG_ENABLE   (NETIF_MSG_DRV   | \
   166 #define RTL8139_DEF_MSG_ENABLE   (NETIF_MSG_DRV   | \
   212    The RTL chips use a 64 element hash table based on the Ethernet CRC.  */
   208    The RTL chips use a 64 element hash table based on the Ethernet CRC.  */
   213 static int multicast_filter_limit = 32;
   209 static int multicast_filter_limit = 32;
   214 
   210 
   215 /* bitmapped message enable number */
   211 /* bitmapped message enable number */
   216 static int debug = -1;
   212 static int debug = -1;
   217 
       
   218 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
   219 
       
   220 static int ec_device_index = -1;
       
   221 static int ec_device_master_index = 0;
       
   222 static ec_device_t *rtl_ec_dev;
       
   223 struct net_device *rtl_ec_net_dev = NULL;
       
   224 
       
   225 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
   226 
   213 
   227 /*
   214 /*
   228  * Receive ring size 
   215  * Receive ring size 
   229  * Warning: 64K ring has hardware issues and may lock up.
   216  * Warning: 64K ring has hardware issues and may lock up.
   230  */
   217  */
   336 	{PCI_ANY_ID, 0x8139, 0x13d1, 0xab06, 0, 0, RTL8139 },
   323 	{PCI_ANY_ID, 0x8139, 0x13d1, 0xab06, 0, 0, RTL8139 },
   337 
   324 
   338 	{0,}
   325 	{0,}
   339 };
   326 };
   340 
   327 
   341 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
   342 
       
   343 /* prevent driver from being loaded automatically */
   328 /* prevent driver from being loaded automatically */
   344 //MODULE_DEVICE_TABLE (pci, rtl8139_pci_tbl);
   329 //MODULE_DEVICE_TABLE (pci, rtl8139_pci_tbl);
   345 
       
   346 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
   347 
   330 
   348 static struct {
   331 static struct {
   349 	const char str[ETH_GSTRING_LEN];
   332 	const char str[ETH_GSTRING_LEN];
   350 } ethtool_stats_keys[] = {
   333 } ethtool_stats_keys[] = {
   351 	{ "early_rx" },
   334 	{ "early_rx" },
   662 	struct rtl_extra_stats xstats;
   645 	struct rtl_extra_stats xstats;
   663 	int time_to_die;
   646 	int time_to_die;
   664 	struct mii_if_info mii;
   647 	struct mii_if_info mii;
   665 	unsigned int regs_len;
   648 	unsigned int regs_len;
   666 	unsigned long fifo_copy_timeout;
   649 	unsigned long fifo_copy_timeout;
       
   650     
       
   651 	ec_device_t *ecdev;
   667 };
   652 };
   668 
       
   669 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
   670 
   653 
   671 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
   654 MODULE_AUTHOR("Florian Pose <fp@igh-essen.com>");
   672 MODULE_DESCRIPTION("RealTek RTL-8139 EtherCAT driver");
   655 MODULE_DESCRIPTION("RealTek RTL-8139 EtherCAT driver");
   673 MODULE_LICENSE("GPL");
   656 MODULE_LICENSE("GPL");
   674 MODULE_VERSION(EC_MASTER_VERSION);
   657 MODULE_VERSION(EC_MASTER_VERSION);
   675 
       
   676 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
   677 
   658 
   678 module_param(multicast_filter_limit, int, 0);
   659 module_param(multicast_filter_limit, int, 0);
   679 module_param_array(media, int, NULL, 0);
   660 module_param_array(media, int, NULL, 0);
   680 module_param_array(full_duplex, int, NULL, 0);
   661 module_param_array(full_duplex, int, NULL, 0);
   681 module_param(debug, int, 0);
   662 module_param(debug, int, 0);
   682 MODULE_PARM_DESC (debug, "8139too bitmapped message enable number");
   663 MODULE_PARM_DESC (debug, "8139too bitmapped message enable number");
   683 MODULE_PARM_DESC (multicast_filter_limit, "8139too maximum number of filtered multicast addresses");
   664 MODULE_PARM_DESC (multicast_filter_limit, "8139too maximum number of filtered multicast addresses");
   684 MODULE_PARM_DESC (media, "8139too: Bits 4+9: force full duplex, bit 5: 100Mbps");
   665 MODULE_PARM_DESC (media, "8139too: Bits 4+9: force full duplex, bit 5: 100Mbps");
   685 MODULE_PARM_DESC (full_duplex, "8139too: Force full duplex for board(s) (1)");
   666 MODULE_PARM_DESC (full_duplex, "8139too: Force full duplex for board(s) (1)");
   686 
   667 
   687 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
   688 
       
   689 module_param(ec_device_index, int, -1);
       
   690 module_param(ec_device_master_index, int, 0);
       
   691 MODULE_PARM_DESC(ec_device_index,
       
   692                  "Index of the device reserved for EtherCAT.");
       
   693 MODULE_PARM_DESC(ec_device_master_index,
       
   694                  "Index of the EtherCAT master to register the device.");
       
   695 
       
   696 void ec_poll(struct net_device *);
   668 void ec_poll(struct net_device *);
   697 
       
   698 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
   699 
   669 
   700 static int read_eeprom (void __iomem *ioaddr, int location, int addr_len);
   670 static int read_eeprom (void __iomem *ioaddr, int location, int addr_len);
   701 static int rtl8139_open (struct net_device *dev);
   671 static int rtl8139_open (struct net_device *dev);
   702 static int mdio_read (struct net_device *dev, int phy_id, int location);
   672 static int mdio_read (struct net_device *dev, int phy_id, int location);
   703 static void mdio_write (struct net_device *dev, int phy_id, int location,
   673 static void mdio_write (struct net_device *dev, int phy_id, int location,
  1044 		return i;
  1014 		return i;
  1045 
  1015 
  1046 	assert (dev != NULL);
  1016 	assert (dev != NULL);
  1047 	tp = netdev_priv(dev);
  1017 	tp = netdev_priv(dev);
  1048 
  1018 
  1049 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1050 
       
  1051 	if (board_idx == ec_device_index) {
       
  1052 		rtl_ec_net_dev = dev;
       
  1053 		strcpy(dev->name, "ec0");
       
  1054 	}
       
  1055 
       
  1056 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1057 
       
  1058 	ioaddr = tp->mmio_addr;
  1019 	ioaddr = tp->mmio_addr;
  1059 	assert (ioaddr != NULL);
  1020 	assert (ioaddr != NULL);
  1060 
  1021 
  1061 	addr_len = read_eeprom (ioaddr, 0, 8) == 0x8129 ? 8 : 6;
  1022 	addr_len = read_eeprom (ioaddr, 0, 8) == 0x8129 ? 8 : 6;
  1062 	for (i = 0; i < 3; i++)
  1023 	for (i = 0; i < 3; i++)
  1104 	tp->mii.mdio_write = mdio_write;
  1065 	tp->mii.mdio_write = mdio_write;
  1105 	tp->mii.phy_id_mask = 0x3f;
  1066 	tp->mii.phy_id_mask = 0x3f;
  1106 	tp->mii.reg_num_mask = 0x1f;
  1067 	tp->mii.reg_num_mask = 0x1f;
  1107 
  1068 
  1108 	/* dev is fully set up and ready to use now */
  1069 	/* dev is fully set up and ready to use now */
  1109 
  1070     
  1110 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1071 	// offer device to EtherCAT master module
  1111 
  1072 	if (ecdev_offer(dev, &tp->ecdev, "8139too", board_idx,
  1112 	if (dev != rtl_ec_net_dev) {
  1073 				ec_poll, THIS_MODULE)) {
       
  1074 		printk(KERN_ERR PFX "Failed to offer device.\n");
       
  1075 		goto err_out;
       
  1076 	}
       
  1077 
       
  1078 	if (!tp->ecdev) {
  1113 		DPRINTK("about to register device named %s (%p)...\n", dev->name, dev);
  1079 		DPRINTK("about to register device named %s (%p)...\n", dev->name, dev);
  1114 		i = register_netdev (dev);
  1080 		i = register_netdev (dev);
  1115 		if (i) goto err_out;
  1081 		if (i) goto err_out;
  1116 	}
  1082 	}
  1117 
       
  1118 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1119 
  1083 
  1120 	pci_set_drvdata (pdev, dev);
  1084 	pci_set_drvdata (pdev, dev);
  1121 
  1085 
  1122 	printk (KERN_INFO "%s: %s at 0x%lx, "
  1086 	printk (KERN_INFO "%s: %s at 0x%lx, "
  1123 		"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
  1087 		"%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
  1187 
  1151 
  1188 	/* Put the chip into low-power mode. */
  1152 	/* Put the chip into low-power mode. */
  1189 	if (rtl_chip_info[tp->chipset].flags & HasHltClk)
  1153 	if (rtl_chip_info[tp->chipset].flags & HasHltClk)
  1190 		RTL_W8 (HltClk, 'H');	/* 'R' would leave the clock running. */
  1154 		RTL_W8 (HltClk, 'H');	/* 'R' would leave the clock running. */
  1191 
  1155 
       
  1156 	if (tp->ecdev && ecdev_open(tp->ecdev)) {
       
  1157 		ecdev_withdraw(tp->ecdev);
       
  1158 		goto err_out;
       
  1159 	}
       
  1160 
  1192 	return 0;
  1161 	return 0;
  1193 
  1162 
  1194 err_out:
  1163 err_out:
  1195 	__rtl8139_cleanup_dev (dev);
  1164 	__rtl8139_cleanup_dev (dev);
  1196 	pci_disable_device (pdev);
  1165 	pci_disable_device (pdev);
  1199 
  1168 
  1200 
  1169 
  1201 static void __devexit rtl8139_remove_one (struct pci_dev *pdev)
  1170 static void __devexit rtl8139_remove_one (struct pci_dev *pdev)
  1202 {
  1171 {
  1203 	struct net_device *dev = pci_get_drvdata (pdev);
  1172 	struct net_device *dev = pci_get_drvdata (pdev);
       
  1173 	struct rtl8139_private *tp = netdev_priv(dev);
  1204 
  1174 
  1205 	assert (dev != NULL);
  1175 	assert (dev != NULL);
  1206 
  1176 
  1207 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1177 	if (tp->ecdev) {
  1208 
  1178 		ecdev_close(tp->ecdev);
  1209 	if (dev != rtl_ec_net_dev) {
  1179 		ecdev_withdraw(tp->ecdev);
       
  1180 	}
       
  1181 	else {
  1210 		unregister_netdev (dev);
  1182 		unregister_netdev (dev);
  1211 	}
  1183 	}
  1212 
       
  1213 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1214 
  1184 
  1215 	__rtl8139_cleanup_dev (dev);
  1185 	__rtl8139_cleanup_dev (dev);
  1216 	pci_disable_device (pdev);
  1186 	pci_disable_device (pdev);
  1217 }
  1187 }
  1218 
  1188 
  1410 {
  1380 {
  1411 	struct rtl8139_private *tp = netdev_priv(dev);
  1381 	struct rtl8139_private *tp = netdev_priv(dev);
  1412 	int retval;
  1382 	int retval;
  1413 	void __iomem *ioaddr = tp->mmio_addr;
  1383 	void __iomem *ioaddr = tp->mmio_addr;
  1414 
  1384 
  1415 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1385 	if (!tp->ecdev) {
  1416 
       
  1417 	if (dev != rtl_ec_net_dev) {
       
  1418 		retval = request_irq(dev->irq, rtl8139_interrupt,
  1386 		retval = request_irq(dev->irq, rtl8139_interrupt,
  1419 			SA_SHIRQ, dev->name, dev);
  1387 			SA_SHIRQ, dev->name, dev);
  1420 		if (retval)
  1388 		if (retval)
  1421 			return retval;
  1389 			return retval;
  1422 	}
  1390 	}
  1423 
       
  1424 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1425 
  1391 
  1426 	tp->tx_bufs = pci_alloc_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
  1392 	tp->tx_bufs = pci_alloc_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
  1427 					   &tp->tx_bufs_dma);
  1393 					   &tp->tx_bufs_dma);
  1428 	tp->rx_ring = pci_alloc_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
  1394 	tp->rx_ring = pci_alloc_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
  1429 					   &tp->rx_ring_dma);
  1395 					   &tp->rx_ring_dma);
  1430 	if (tp->tx_bufs == NULL || tp->rx_ring == NULL) {
  1396 	if (tp->tx_bufs == NULL || tp->rx_ring == NULL) {
  1431 		/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1397 		if (!tp->ecdev) free_irq(dev->irq, dev);
  1432 
       
  1433 		if (dev != rtl_ec_net_dev) {
       
  1434 			free_irq(dev->irq, dev);
       
  1435 		}
       
  1436 
       
  1437 		/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1438 
       
  1439 		if (tp->tx_bufs)
  1398 		if (tp->tx_bufs)
  1440 			pci_free_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
  1399 			pci_free_consistent(tp->pci_dev, TX_BUF_TOT_LEN,
  1441 					    tp->tx_bufs, tp->tx_bufs_dma);
  1400 					    tp->tx_bufs, tp->tx_bufs_dma);
  1442 		if (tp->rx_ring)
  1401 		if (tp->rx_ring)
  1443 			pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
  1402 			pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
  1451 	tp->tx_flag = (TX_FIFO_THRESH << 11) & 0x003f0000;
  1410 	tp->tx_flag = (TX_FIFO_THRESH << 11) & 0x003f0000;
  1452 
  1411 
  1453 	rtl8139_init_ring (dev);
  1412 	rtl8139_init_ring (dev);
  1454 	rtl8139_hw_start (dev);
  1413 	rtl8139_hw_start (dev);
  1455 
  1414 
  1456 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1415 	if (!tp->ecdev) {
  1457 
       
  1458 	if (dev != rtl_ec_net_dev) {
       
  1459 		netif_start_queue (dev);
  1416 		netif_start_queue (dev);
  1460 
  1417 
  1461 		if (netif_msg_ifup(tp))
  1418 		if (netif_msg_ifup(tp))
  1462 			printk(KERN_DEBUG "%s: rtl8139_open() ioaddr %#lx IRQ %d"
  1419 			printk(KERN_DEBUG "%s: rtl8139_open() ioaddr %#lx IRQ %d"
  1463 			       " GP Pins %2.2x %s-duplex.\n",
  1420 			       " GP Pins %2.2x %s-duplex.\n",
  1466 			       tp->mii.full_duplex ? "full" : "half");
  1423 			       tp->mii.full_duplex ? "full" : "half");
  1467 
  1424 
  1468 		rtl8139_start_thread(dev);
  1425 		rtl8139_start_thread(dev);
  1469 	}
  1426 	}
  1470 
  1427 
  1471 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1472 
       
  1473 	return 0;
  1428 	return 0;
  1474 }
  1429 }
  1475 
  1430 
  1476 
  1431 
  1477 static void rtl_check_media (struct net_device *dev, unsigned int init_media)
  1432 static void rtl_check_media (struct net_device *dev, unsigned int init_media)
  1478 {
  1433 {
  1479 	struct rtl8139_private *tp = netdev_priv(dev);
  1434 	struct rtl8139_private *tp = netdev_priv(dev);
  1480 
  1435 
  1481 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1436 	if (tp->ecdev) {
  1482 
  1437 		void __iomem *ioaddr = tp->mmio_addr;
  1483 	if (dev != rtl_ec_net_dev) {
  1438 		uint16_t state = RTL_R16(BasicModeStatus) & BMSR_LSTATUS;
       
  1439 		ecdev_link_state(tp->ecdev, state ? 1 : 0);
       
  1440 	}
       
  1441 	else {
  1484 		if (tp->phys[0] >= 0) {
  1442 		if (tp->phys[0] >= 0) {
  1485 			mii_check_media(&tp->mii, netif_msg_link(tp), init_media);
  1443 			mii_check_media(&tp->mii, netif_msg_link(tp), init_media);
  1486 		}
  1444 		}
  1487 	} else {
  1445 	}
  1488 		void __iomem *ioaddr = tp->mmio_addr;
       
  1489 		uint16_t state = RTL_R16(BasicModeStatus) & BMSR_LSTATUS;
       
  1490 		ecdev_link_state(rtl_ec_dev, state ? 1 : 0);
       
  1491 	}
       
  1492 
       
  1493 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1494 }
  1446 }
  1495 
  1447 
  1496 /* Start the hardware at open or resume. */
  1448 /* Start the hardware at open or resume. */
  1497 static void rtl8139_hw_start (struct net_device *dev)
  1449 static void rtl8139_hw_start (struct net_device *dev)
  1498 {
  1450 {
  1553 	/* make sure RxTx has started */
  1505 	/* make sure RxTx has started */
  1554 	tmp = RTL_R8 (ChipCmd);
  1506 	tmp = RTL_R8 (ChipCmd);
  1555 	if ((!(tmp & CmdRxEnb)) || (!(tmp & CmdTxEnb)))
  1507 	if ((!(tmp & CmdRxEnb)) || (!(tmp & CmdTxEnb)))
  1556 		RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
  1508 		RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
  1557 
  1509 
  1558 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1510 	if (!tp->ecdev)
  1559 
       
  1560 	if (dev != rtl_ec_net_dev) {
       
  1561 		/* Enable all known interrupts by setting the interrupt mask. */
  1511 		/* Enable all known interrupts by setting the interrupt mask. */
  1562 		RTL_W16 (IntrMask, rtl8139_intr_mask);
  1512 		RTL_W16 (IntrMask, rtl8139_intr_mask);
  1563 	}
       
  1564 
       
  1565 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1566 }
  1513 }
  1567 
  1514 
  1568 
  1515 
  1569 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1516 /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1570 static void rtl8139_init_ring (struct net_device *dev)
  1517 static void rtl8139_init_ring (struct net_device *dev)
  1819 	/* disable Tx ASAP, if not already */
  1766 	/* disable Tx ASAP, if not already */
  1820 	tmp8 = RTL_R8 (ChipCmd);
  1767 	tmp8 = RTL_R8 (ChipCmd);
  1821 	if (tmp8 & CmdTxEnb)
  1768 	if (tmp8 & CmdTxEnb)
  1822 		RTL_W8 (ChipCmd, CmdRxEnb);
  1769 		RTL_W8 (ChipCmd, CmdRxEnb);
  1823 
  1770 
  1824 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1771     if (tp->ecdev) {
  1825 
  1772         rtl8139_tx_clear (tp);
  1826 	if (dev != rtl_ec_net_dev) {
  1773         rtl8139_hw_start (dev);
  1827 		spin_lock(&tp->rx_lock);
  1774     }
  1828 		/* Disable interrupts by clearing the interrupt mask. */
  1775     else {
  1829 		RTL_W16 (IntrMask, 0x0000);
  1776         spin_lock(&tp->rx_lock);
  1830 
  1777         /* Disable interrupts by clearing the interrupt mask. */
  1831 		/* Stop a shared interrupt from scavenging while we are. */
  1778         RTL_W16 (IntrMask, 0x0000);
  1832 		spin_lock_irqsave (&tp->lock, flags);
  1779 
  1833 		rtl8139_tx_clear (tp);
  1780         /* Stop a shared interrupt from scavenging while we are. */
  1834 		spin_unlock_irqrestore (&tp->lock, flags);
  1781         spin_lock_irqsave (&tp->lock, flags);
  1835 
  1782         rtl8139_tx_clear (tp);
  1836 		/* ...and finally, reset everything */
  1783         spin_unlock_irqrestore (&tp->lock, flags);
  1837 		if (netif_running(dev)) {
  1784 
  1838 			rtl8139_hw_start (dev);
  1785         /* ...and finally, reset everything */
  1839 			netif_wake_queue (dev);
  1786         if (netif_running(dev)) {
  1840 		}
  1787             rtl8139_hw_start (dev);
  1841 		spin_unlock(&tp->rx_lock);
  1788             netif_wake_queue (dev);
  1842 	} else {
  1789         }
  1843 		rtl8139_tx_clear (tp);
  1790         spin_unlock(&tp->rx_lock);
  1844 		rtl8139_hw_start (dev);
  1791     }
  1845 	}
       
  1846 
       
  1847 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1848 }
  1792 }
  1849 
  1793 
  1850 
  1794 
  1851 static int rtl8139_start_xmit (struct sk_buff *skb, struct net_device *dev)
  1795 static int rtl8139_start_xmit (struct sk_buff *skb, struct net_device *dev)
  1852 {
  1796 {
  1855 	unsigned int entry;
  1799 	unsigned int entry;
  1856 	unsigned int len = skb->len;
  1800 	unsigned int len = skb->len;
  1857 
  1801 
  1858 	/* Calculate the next Tx descriptor entry. */
  1802 	/* Calculate the next Tx descriptor entry. */
  1859 	entry = tp->cur_tx % NUM_TX_DESC;
  1803 	entry = tp->cur_tx % NUM_TX_DESC;
  1860 
       
  1861 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1862 
  1804 
  1863 	/* Note: the chip doesn't have auto-pad! */
  1805 	/* Note: the chip doesn't have auto-pad! */
  1864 	if (likely(len < TX_BUF_SIZE)) {
  1806 	if (likely(len < TX_BUF_SIZE)) {
  1865 		if (len < ETH_ZLEN)
  1807 		if (len < ETH_ZLEN)
  1866 			memset(tp->tx_buf[entry], 0, ETH_ZLEN);
  1808 			memset(tp->tx_buf[entry], 0, ETH_ZLEN);
  1867 		skb_copy_and_csum_dev(skb, tp->tx_buf[entry]);
  1809 		skb_copy_and_csum_dev(skb, tp->tx_buf[entry]);
  1868 		if (dev != rtl_ec_net_dev) {
  1810 		if (!tp->ecdev) dev_kfree_skb(skb);
  1869 			dev_kfree_skb(skb);
       
  1870 		}
       
  1871 	} else {
  1811 	} else {
  1872 		if (dev != rtl_ec_net_dev) {
  1812 		if (!tp->ecdev) dev_kfree_skb(skb);
  1873 			dev_kfree_skb(skb);
       
  1874 		}
       
  1875 		tp->stats.tx_dropped++;
  1813 		tp->stats.tx_dropped++;
  1876 		return 0;
  1814 		return 0;
  1877 	}
  1815 	}
  1878 
  1816 
  1879 	if (dev != rtl_ec_net_dev) {
  1817 	if (!tp->ecdev) spin_lock_irq(&tp->lock);
  1880 		spin_lock_irq(&tp->lock);
       
  1881 	}
       
  1882 
       
  1883 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1884 
       
  1885 	RTL_W32_F (TxStatus0 + (entry * sizeof (u32)),
  1818 	RTL_W32_F (TxStatus0 + (entry * sizeof (u32)),
  1886 		   tp->tx_flag | max(len, (unsigned int)ETH_ZLEN));
  1819 		   tp->tx_flag | max(len, (unsigned int)ETH_ZLEN));
  1887 
  1820 
  1888 	dev->trans_start = jiffies;
  1821 	dev->trans_start = jiffies;
  1889 
  1822 
  1890 	tp->cur_tx++;
  1823 	tp->cur_tx++;
  1891 	wmb();
  1824 	wmb();
  1892 
  1825 
  1893 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  1826 	if (!tp->ecdev) {
  1894 
       
  1895 	if (dev != rtl_ec_net_dev) {
       
  1896 		if ((tp->cur_tx - NUM_TX_DESC) == tp->dirty_tx)
  1827 		if ((tp->cur_tx - NUM_TX_DESC) == tp->dirty_tx)
  1897 			netif_stop_queue (dev);
  1828 			netif_stop_queue (dev);
  1898 		spin_unlock_irq(&tp->lock);
  1829 		spin_unlock_irq(&tp->lock);
  1899 
  1830 
  1900 		if (netif_msg_tx_queued(tp))
  1831 		if (netif_msg_tx_queued(tp))
  1901 			printk (KERN_DEBUG "%s: Queued Tx packet size %u to slot %d.\n",
  1832 			printk (KERN_DEBUG "%s: Queued Tx packet size %u to slot %d.\n",
  1902 				dev->name, len, entry);
  1833 				dev->name, len, entry);
  1903 	}
  1834 	}
  1904 
       
  1905 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1906 
  1835 
  1907 	return 0;
  1836 	return 0;
  1908 }
  1837 }
  1909 
  1838 
  1910 
  1839 
  1959 
  1888 
  1960 		dirty_tx++;
  1889 		dirty_tx++;
  1961 		tx_left--;
  1890 		tx_left--;
  1962 	}
  1891 	}
  1963 
  1892 
  1964 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  1965 
       
  1966 #ifndef RTL8139_NDEBUG
  1893 #ifndef RTL8139_NDEBUG
  1967 	if (dev != rtl_ec_net_dev && tp->cur_tx - dirty_tx > NUM_TX_DESC) {
  1894 	if (!tp->ecdev && tp->cur_tx - dirty_tx > NUM_TX_DESC) {
  1968 		printk (KERN_ERR "%s: Out-of-sync dirty pointer, %ld vs. %ld.\n",
  1895 		printk (KERN_ERR "%s: Out-of-sync dirty pointer, %ld vs. %ld.\n",
  1969 		        dev->name, dirty_tx, tp->cur_tx);
  1896 		        dev->name, dirty_tx, tp->cur_tx);
  1970 		dirty_tx += NUM_TX_DESC;
  1897 		dirty_tx += NUM_TX_DESC;
  1971 	}
  1898 	}
  1972 #endif /* RTL8139_NDEBUG */
  1899 #endif /* RTL8139_NDEBUG */
  1973 
  1900 
  1974 	/* only wake the queue if we did work, and the queue is stopped */
  1901 	/* only wake the queue if we did work, and the queue is stopped */
  1975 	if (tp->dirty_tx != dirty_tx) {
  1902 	if (tp->dirty_tx != dirty_tx) {
  1976 		tp->dirty_tx = dirty_tx;
  1903 		tp->dirty_tx = dirty_tx;
  1977 		mb();
  1904 		mb();
  1978 
  1905 		if (!tp->ecdev) netif_wake_queue (dev);
  1979 		if (dev != rtl_ec_net_dev) {
  1906 	}
  1980 			netif_wake_queue (dev);
       
  1981 		}
       
  1982 	}
       
  1983 
       
  1984 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  1985 }
  1907 }
  1986 
  1908 
  1987 
  1909 
  1988 /* TODO: clean this up!  Rx reset need not be this intensive */
  1910 /* TODO: clean this up!  Rx reset need not be this intensive */
  1989 static void rtl8139_rx_err (u32 rx_status, struct net_device *dev,
  1911 static void rtl8139_rx_err (u32 rx_status, struct net_device *dev,
  2112 	DPRINTK ("%s: In rtl8139_rx(), current %4.4x BufAddr %4.4x,"
  2034 	DPRINTK ("%s: In rtl8139_rx(), current %4.4x BufAddr %4.4x,"
  2113 		 " free to %4.4x, Cmd %2.2x.\n", dev->name, (u16)cur_rx,
  2035 		 " free to %4.4x, Cmd %2.2x.\n", dev->name, (u16)cur_rx,
  2114 		 RTL_R16 (RxBufAddr),
  2036 		 RTL_R16 (RxBufAddr),
  2115 		 RTL_R16 (RxBufPtr), RTL_R8 (ChipCmd));
  2037 		 RTL_R16 (RxBufPtr), RTL_R8 (ChipCmd));
  2116 
  2038 
  2117 
  2039 	while ((tp->ecdev || netif_running(dev))
  2118 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2119 
       
  2120 	while ((dev == rtl_ec_net_dev || netif_running(dev))
       
  2121 	       && received < budget
  2040 	       && received < budget
  2122 	       && (RTL_R8 (ChipCmd) & RxBufEmpty) == 0) {
  2041 	       && (RTL_R8 (ChipCmd) & RxBufEmpty) == 0) {
  2123 
       
  2124 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2125 
       
  2126 		u32 ring_offset = cur_rx % RX_BUF_LEN;
  2042 		u32 ring_offset = cur_rx % RX_BUF_LEN;
  2127 		u32 rx_status;
  2043 		u32 rx_status;
  2128 		unsigned int pkt_size;
  2044 		unsigned int pkt_size;
  2129 		struct sk_buff *skb;
  2045 		struct sk_buff *skb;
  2130 
  2046 
  2133 		/* read size+status of next frame from DMA ring buffer */
  2049 		/* read size+status of next frame from DMA ring buffer */
  2134 		rx_status = le32_to_cpu (*(u32 *) (rx_ring + ring_offset));
  2050 		rx_status = le32_to_cpu (*(u32 *) (rx_ring + ring_offset));
  2135 		rx_size = rx_status >> 16;
  2051 		rx_size = rx_status >> 16;
  2136 		pkt_size = rx_size - 4;
  2052 		pkt_size = rx_size - 4;
  2137 
  2053 
  2138 		/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2054 		if (!tp->ecdev) {
  2139 
       
  2140 		if (dev != rtl_ec_net_dev) {
       
  2141 			if (netif_msg_rx_status(tp))
  2055 			if (netif_msg_rx_status(tp))
  2142 				printk(KERN_DEBUG "%s:  rtl8139_rx() status %4.4x, size %4.4x,"
  2056 				printk(KERN_DEBUG "%s:  rtl8139_rx() status %4.4x, size %4.4x,"
  2143 				       " cur %4.4x.\n", dev->name, rx_status,
  2057 				       " cur %4.4x.\n", dev->name, rx_status,
  2144 				       rx_size, cur_rx);
  2058 				       rx_size, cur_rx);
  2145 		}
  2059 		}
  2146 
       
  2147 		/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2148 
       
  2149 #if RTL8139_DEBUG > 2
  2060 #if RTL8139_DEBUG > 2
  2150 		{
  2061 		{
  2151 			int i;
  2062 			int i;
  2152 			DPRINTK ("%s: Frame contents ", dev->name);
  2063 			DPRINTK ("%s: Frame contents ", dev->name);
  2153 			for (i = 0; i < 70; i++)
  2064 			for (i = 0; i < 70; i++)
  2191 			rtl8139_rx_err (rx_status, dev, tp, ioaddr);
  2102 			rtl8139_rx_err (rx_status, dev, tp, ioaddr);
  2192 			received = -1;
  2103 			received = -1;
  2193 			goto out;
  2104 			goto out;
  2194 		}
  2105 		}
  2195 
  2106 
  2196 		/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2107 		if (tp->ecdev) {
  2197 
  2108 			ecdev_receive(tp->ecdev,
  2198 		if (dev != rtl_ec_net_dev) {
  2109 					&rx_ring[ring_offset + 4], pkt_size);
       
  2110 			dev->last_rx = jiffies;
       
  2111 			tp->stats.rx_bytes += pkt_size;
       
  2112 			tp->stats.rx_packets++;
       
  2113 		}
       
  2114 		else {
  2199 			/* Malloc up new buffer, compatible with net-2e. */
  2115 			/* Malloc up new buffer, compatible with net-2e. */
  2200 			/* Omit the four octet CRC from the length. */
  2116 			/* Omit the four octet CRC from the length. */
  2201 
  2117 
  2202 			skb = dev_alloc_skb (pkt_size + 2);
  2118 			skb = dev_alloc_skb (pkt_size + 2);
  2203 			if (likely(skb)) {
  2119 			if (likely(skb)) {
  2218 
  2134 
  2219 				netif_receive_skb (skb);
  2135 				netif_receive_skb (skb);
  2220 			} else {
  2136 			} else {
  2221 				if (net_ratelimit())
  2137 				if (net_ratelimit())
  2222 					printk(KERN_WARNING
  2138 					printk(KERN_WARNING
  2223 					       "%s: Memory squeeze, dropping packet.\n",
  2139 							"%s: Memory squeeze, dropping packet.\n",
  2224 					       dev->name);
  2140 							dev->name);
  2225 				tp->stats.rx_dropped++;
  2141 				tp->stats.rx_dropped++;
  2226 			}
  2142 			}
  2227 		} else {
       
  2228 			ecdev_receive(rtl_ec_dev,
       
  2229 			              &rx_ring[ring_offset + 4], pkt_size);
       
  2230 			dev->last_rx = jiffies;
       
  2231 			tp->stats.rx_bytes += pkt_size;
       
  2232 			tp->stats.rx_packets++;
       
  2233 		}
  2143 		}
  2234 
       
  2235 		/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2236 
       
  2237 		received++;
  2144 		received++;
  2238 
  2145 
  2239 		cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
  2146 		cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
  2240 		RTL_W16 (RxBufPtr, (u16) (cur_rx - 16));
  2147 		RTL_W16 (RxBufPtr, (u16) (cur_rx - 16));
  2241 
  2148 
  2337 	spin_unlock(&tp->rx_lock);
  2244 	spin_unlock(&tp->rx_lock);
  2338 
  2245 
  2339 	return !done;
  2246 	return !done;
  2340 }
  2247 }
  2341 
  2248 
  2342 /* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2343 
       
  2344 void ec_poll(struct net_device *dev)
  2249 void ec_poll(struct net_device *dev)
  2345 {
  2250 {
  2346     rtl8139_interrupt(0, dev, NULL);
  2251     rtl8139_interrupt(0, dev, NULL);
  2347 }
  2252 }
  2348 
       
  2349 /* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2350 
  2253 
  2351 /* The interrupt handler does all of the Rx thread work and cleans up
  2254 /* The interrupt handler does all of the Rx thread work and cleans up
  2352    after the Tx thread. */
  2255    after the Tx thread. */
  2353 irqreturn_t rtl8139_interrupt (int irq, void *dev_instance,
  2256 static irqreturn_t rtl8139_interrupt (int irq, void *dev_instance,
  2354                                struct pt_regs *regs)
  2257 			       struct pt_regs *regs)
  2355 {
  2258 {
  2356 	struct net_device *dev = (struct net_device *) dev_instance;
  2259 	struct net_device *dev = (struct net_device *) dev_instance;
  2357 	struct rtl8139_private *tp = netdev_priv(dev);
  2260 	struct rtl8139_private *tp = netdev_priv(dev);
  2358 	void __iomem *ioaddr = tp->mmio_addr;
  2261 	void __iomem *ioaddr = tp->mmio_addr;
  2359 	u16 status, ackstat;
  2262 	u16 status, ackstat;
  2360 	int link_changed = 0; /* avoid bogus "uninit" warning */
  2263 	int link_changed = 0; /* avoid bogus "uninit" warning */
  2361 	int handled = 0;
  2264 	int handled = 0;
  2362 
  2265 
  2363 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2266 	if (tp->ecdev) {
  2364 
  2267 		status = RTL_R16 (IntrStatus);
  2365 	if (dev != rtl_ec_net_dev) {
  2268 	}
       
  2269 	else {
  2366 		spin_lock (&tp->lock);
  2270 		spin_lock (&tp->lock);
  2367 		status = RTL_R16 (IntrStatus);
  2271 		status = RTL_R16 (IntrStatus);
  2368 
  2272 
  2369 		/* shared irq? */
  2273 		/* shared irq? */
  2370 		if (unlikely((status & rtl8139_intr_mask) == 0))
  2274 		if (unlikely((status & rtl8139_intr_mask) == 0))
  2371 			goto out;
  2275 			goto out;
  2372 	} else {
  2276 	}
  2373 		status = RTL_R16 (IntrStatus);
       
  2374 	}
       
  2375 
       
  2376 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2377 
  2277 
  2378 	handled = 1;
  2278 	handled = 1;
  2379 
  2279 
  2380 	/* h/w no longer present (hotplug?) or major error, bail */
  2280 	/* h/w no longer present (hotplug?) or major error, bail */
  2381 	if (unlikely(status == 0xFFFF)) 
  2281 	if (unlikely(status == 0xFFFF)) 
  2382 		goto out;
  2282 		goto out;
  2383 
  2283 
  2384 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2284 	if (!tp->ecdev) {
  2385 
       
  2386 	if (dev != rtl_ec_net_dev) {
       
  2387 		/* close possible race's with dev_close */
  2285 		/* close possible race's with dev_close */
  2388 		if (unlikely(!netif_running(dev))) {
  2286 		if (unlikely(!netif_running(dev))) {
  2389 			RTL_W16 (IntrMask, 0);
  2287 			RTL_W16 (IntrMask, 0);
  2390 			goto out;
  2288 			goto out;
  2391 		}
  2289 		}
  2392 	}
  2290 	}
  2393 
  2291 
  2394 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2395 
       
  2396 	/* Acknowledge all of the current interrupt sources ASAP, but
  2292 	/* Acknowledge all of the current interrupt sources ASAP, but
  2397 	   an first get an additional status bit from CSCR. */
  2293 	   an first get an additional status bit from CSCR. */
  2398 	if (unlikely(status & RxUnderrun))
  2294 	if (unlikely(status & RxUnderrun))
  2399 		link_changed = RTL_R16 (CSCR) & CSCR_LinkChangeBit;
  2295 		link_changed = RTL_R16 (CSCR) & CSCR_LinkChangeBit;
  2400 
  2296 
  2402 	if (ackstat)
  2298 	if (ackstat)
  2403 		RTL_W16 (IntrStatus, ackstat);
  2299 		RTL_W16 (IntrStatus, ackstat);
  2404 
  2300 
  2405 	/* Receive packets are processed by poll routine.
  2301 	/* Receive packets are processed by poll routine.
  2406 	   If not running start it now. */
  2302 	   If not running start it now. */
  2407 
       
  2408 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2409 
       
  2410 	if (status & RxAckBits){
  2303 	if (status & RxAckBits){
  2411 		if (dev != rtl_ec_net_dev) {
  2304 		if (tp->ecdev) {
       
  2305 			/* EtherCAT device: Just receive all frames */
       
  2306 			rtl8139_rx(dev, tp, 100); // FIXME
       
  2307 		}
       
  2308 		else {
  2412 			/* Mark for polling */
  2309 			/* Mark for polling */
  2413 			if (netif_rx_schedule_prep(dev)) {
  2310 			if (netif_rx_schedule_prep(dev)) {
  2414 				RTL_W16_F (IntrMask, rtl8139_norx_intr_mask);
  2311 				RTL_W16_F (IntrMask, rtl8139_norx_intr_mask);
  2415 				__netif_rx_schedule (dev);
  2312 				__netif_rx_schedule (dev);
  2416 			}
  2313 			}
  2417 		} else {
       
  2418 			/* EtherCAT device: Just receive all frames */
       
  2419 			rtl8139_rx(dev, tp, 100); // FIXME
       
  2420 		}
  2314 		}
  2421 	}
  2315 	}
  2422 
       
  2423 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2424 
  2316 
  2425 	/* Check uncommon events with one test. */
  2317 	/* Check uncommon events with one test. */
  2426 	if (unlikely(status & (PCIErr | PCSTimeout | RxUnderrun | RxErr)))
  2318 	if (unlikely(status & (PCIErr | PCSTimeout | RxUnderrun | RxErr)))
  2427 		rtl8139_weird_interrupt (dev, tp, ioaddr,
  2319 		rtl8139_weird_interrupt (dev, tp, ioaddr,
  2428 					 status, link_changed);
  2320 					 status, link_changed);
  2431 		rtl8139_tx_interrupt (dev, tp, ioaddr);
  2323 		rtl8139_tx_interrupt (dev, tp, ioaddr);
  2432 		if (status & TxErr)
  2324 		if (status & TxErr)
  2433 			RTL_W16 (IntrStatus, TxErr);
  2325 			RTL_W16 (IntrStatus, TxErr);
  2434 	}
  2326 	}
  2435  out:
  2327  out:
  2436 
  2328 	if (!tp->ecdev) spin_unlock (&tp->lock);
  2437 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2438 
       
  2439 	if (dev != rtl_ec_net_dev) {
       
  2440 		spin_unlock (&tp->lock);
       
  2441 	}
       
  2442 
       
  2443 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2444 
  2329 
  2445 	DPRINTK ("%s: exiting interrupt, intr_status=%#4.4x.\n",
  2330 	DPRINTK ("%s: exiting interrupt, intr_status=%#4.4x.\n",
  2446 		 dev->name, RTL_R16 (IntrStatus));
  2331 		 dev->name, RTL_R16 (IntrStatus));
  2447 	return IRQ_RETVAL(handled);
  2332 	return IRQ_RETVAL(handled);
  2448 }
  2333 }
  2465 	struct rtl8139_private *tp = netdev_priv(dev);
  2350 	struct rtl8139_private *tp = netdev_priv(dev);
  2466 	void __iomem *ioaddr = tp->mmio_addr;
  2351 	void __iomem *ioaddr = tp->mmio_addr;
  2467 	int ret = 0;
  2352 	int ret = 0;
  2468 	unsigned long flags;
  2353 	unsigned long flags;
  2469 
  2354 
  2470 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2355 	if (tp->ecdev) {
  2471 
  2356 		/* Stop the chip's Tx and Rx DMA processes. */
  2472 	if (dev != rtl_ec_net_dev) {
  2357 		RTL_W8 (ChipCmd, 0);
       
  2358 
       
  2359 		/* Disable interrupts by clearing the interrupt mask. */
       
  2360 		RTL_W16 (IntrMask, 0);
       
  2361 
       
  2362 		/* Update the error counts. */
       
  2363 		tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
       
  2364 		RTL_W32 (RxMissed, 0);
       
  2365 	} else {
  2473 		netif_stop_queue (dev);
  2366 		netif_stop_queue (dev);
  2474 
  2367 
  2475 		if (tp->thr_pid >= 0) {
  2368 		if (tp->thr_pid >= 0) {
  2476 			tp->time_to_die = 1;
  2369 			tp->time_to_die = 1;
  2477 			wmb();
  2370 			wmb();
  2501 
  2394 
  2502 		spin_unlock_irqrestore (&tp->lock, flags);
  2395 		spin_unlock_irqrestore (&tp->lock, flags);
  2503 
  2396 
  2504 		synchronize_irq (dev->irq);	/* racy, but that's ok here */
  2397 		synchronize_irq (dev->irq);	/* racy, but that's ok here */
  2505 		free_irq (dev->irq, dev);
  2398 		free_irq (dev->irq, dev);
  2506 	} else {
  2399 	}
  2507 		/* Stop the chip's Tx and Rx DMA processes. */
       
  2508 		RTL_W8 (ChipCmd, 0);
       
  2509 
       
  2510 		/* Disable interrupts by clearing the interrupt mask. */
       
  2511 		RTL_W16 (IntrMask, 0);
       
  2512 
       
  2513 		/* Update the error counts. */
       
  2514 		tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
       
  2515 		RTL_W32 (RxMissed, 0);
       
  2516 	}
       
  2517 
       
  2518 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2519 
  2400 
  2520 	rtl8139_tx_clear (tp);
  2401 	rtl8139_tx_clear (tp);
  2521 
  2402 
  2522 	pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
  2403 	pci_free_consistent(tp->pci_dev, RX_BUF_TOT_LEN,
  2523 			    tp->rx_ring, tp->rx_ring_dma);
  2404 			    tp->rx_ring, tp->rx_ring_dma);
  2728 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2609 static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2729 {
  2610 {
  2730 	struct rtl8139_private *np = netdev_priv(dev);
  2611 	struct rtl8139_private *np = netdev_priv(dev);
  2731 	int rc;
  2612 	int rc;
  2732 
  2613 
  2733 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2614 	if (np->ecdev || !netif_running(dev))
  2734 
       
  2735 	if (dev == rtl_ec_net_dev || !netif_running(dev))
       
  2736 		return -EINVAL;
  2615 		return -EINVAL;
  2737 
       
  2738 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2739 
  2616 
  2740 	spin_lock_irq(&np->lock);
  2617 	spin_lock_irq(&np->lock);
  2741 	rc = generic_mii_ioctl(&np->mii, if_mii(rq), cmd, NULL);
  2618 	rc = generic_mii_ioctl(&np->mii, if_mii(rq), cmd, NULL);
  2742 	spin_unlock_irq(&np->lock);
  2619 	spin_unlock_irq(&np->lock);
  2743 
  2620 
  2749 {
  2626 {
  2750 	struct rtl8139_private *tp = netdev_priv(dev);
  2627 	struct rtl8139_private *tp = netdev_priv(dev);
  2751 	void __iomem *ioaddr = tp->mmio_addr;
  2628 	void __iomem *ioaddr = tp->mmio_addr;
  2752 	unsigned long flags;
  2629 	unsigned long flags;
  2753 
  2630 
  2754 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2631 	if (tp->ecdev || netif_running(dev)) {
  2755 
       
  2756 	if (dev == rtl_ec_net_dev || netif_running(dev)) {
       
  2757 		spin_lock_irqsave (&tp->lock, flags);
  2632 		spin_lock_irqsave (&tp->lock, flags);
  2758 		tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
  2633 		tp->stats.rx_missed_errors += RTL_R32 (RxMissed);
  2759 		RTL_W32 (RxMissed, 0);
  2634 		RTL_W32 (RxMissed, 0);
  2760 		spin_unlock_irqrestore (&tp->lock, flags);
  2635 		spin_unlock_irqrestore (&tp->lock, flags);
  2761 	}
  2636 	}
  2762 
       
  2763 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2764 
  2637 
  2765 	return &tp->stats;
  2638 	return &tp->stats;
  2766 }
  2639 }
  2767 
  2640 
  2768 /* Set or clear the multicast filter for this adaptor.
  2641 /* Set or clear the multicast filter for this adaptor.
  2835 	void __iomem *ioaddr = tp->mmio_addr;
  2708 	void __iomem *ioaddr = tp->mmio_addr;
  2836 	unsigned long flags;
  2709 	unsigned long flags;
  2837 
  2710 
  2838 	pci_save_state (pdev);
  2711 	pci_save_state (pdev);
  2839 
  2712 
  2840 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2713 	if (tp->ecdev || !netif_running (dev))
  2841 
       
  2842 	if (dev == rtl_ec_net_dev || !netif_running (dev))
       
  2843 		return 0;
  2714 		return 0;
  2844 
       
  2845 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2846 
  2715 
  2847 	netif_device_detach (dev);
  2716 	netif_device_detach (dev);
  2848 
  2717 
  2849 	spin_lock_irqsave (&tp->lock, flags);
  2718 	spin_lock_irqsave (&tp->lock, flags);
  2850 
  2719 
  2865 
  2734 
  2866 
  2735 
  2867 static int rtl8139_resume (struct pci_dev *pdev)
  2736 static int rtl8139_resume (struct pci_dev *pdev)
  2868 {
  2737 {
  2869 	struct net_device *dev = pci_get_drvdata (pdev);
  2738 	struct net_device *dev = pci_get_drvdata (pdev);
       
  2739 	struct rtl8139_private *tp = netdev_priv(dev);
  2870 
  2740 
  2871 	pci_restore_state (pdev);
  2741 	pci_restore_state (pdev);
  2872 
  2742 	if (tp->ecdev || !netif_running (dev))
  2873 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
       
  2874 
       
  2875 	if (dev == rtl_ec_net_dev || !netif_running (dev))
       
  2876 		return 0;
  2743 		return 0;
  2877 
       
  2878 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2879 
       
  2880 	pci_set_power_state (pdev, PCI_D0);
  2744 	pci_set_power_state (pdev, PCI_D0);
  2881 	rtl8139_init_ring (dev);
  2745 	rtl8139_init_ring (dev);
  2882 	rtl8139_hw_start (dev);
  2746 	rtl8139_hw_start (dev);
  2883 	netif_device_attach (dev);
  2747 	netif_device_attach (dev);
  2884 	return 0;
  2748 	return 0;
  2899 };
  2763 };
  2900 
  2764 
  2901 
  2765 
  2902 static int __init rtl8139_init_module (void)
  2766 static int __init rtl8139_init_module (void)
  2903 {
  2767 {
  2904 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2768 	/* when we're a module, we always print a version message,
  2905 
  2769 	 * even if no 8139 board is found.
  2906 	printk(KERN_INFO RTL8139_DRIVER_NAME "\n");
  2770 	 */
  2907 	printk(KERN_INFO "ec_device_index is %i\n", ec_device_index);
  2771 #ifdef MODULE
  2908 
  2772 	printk (KERN_INFO RTL8139_DRIVER_NAME "\n");
  2909 	if (pci_module_init(&rtl8139_pci_driver) < 0) {
  2773 #endif
  2910 		printk(KERN_ERR "Failed to init PCI module.\n");
  2774 
  2911 		goto out_return;
  2775 	return pci_module_init (&rtl8139_pci_driver);
  2912 	}
       
  2913 
       
  2914 	if (rtl_ec_net_dev) {
       
  2915 		printk(KERN_INFO "Registering EtherCAT device...\n");
       
  2916 		if (!(rtl_ec_dev = ecdev_register(ec_device_master_index,
       
  2917 			rtl_ec_net_dev, ec_poll, THIS_MODULE))) {
       
  2918 			printk(KERN_ERR "Failed to register EtherCAT device!\n");
       
  2919 			goto out_pci;
       
  2920 		}
       
  2921 
       
  2922 		printk(KERN_INFO "Opening EtherCAT device...\n");
       
  2923 		if (ecdev_open(rtl_ec_dev)) {
       
  2924 			printk(KERN_ERR "Failed to open EtherCAT device!\n");
       
  2925 			goto out_unregister;
       
  2926 		}
       
  2927 
       
  2928 		printk(KERN_INFO "EtherCAT device ready.\n");
       
  2929 	} else {
       
  2930 		printk(KERN_WARNING "No EtherCAT device registered!\n");
       
  2931 	}
       
  2932 
       
  2933 	return 0;
       
  2934 
       
  2935     out_unregister:
       
  2936 	printk(KERN_INFO "Unregistering EtherCAT device...\n");
       
  2937 	ecdev_unregister(ec_device_master_index, rtl_ec_dev);
       
  2938 	rtl_ec_dev = NULL;
       
  2939     out_pci:
       
  2940 	pci_unregister_driver(&rtl8139_pci_driver);
       
  2941     out_return:
       
  2942 	return -1;
       
  2943 
       
  2944 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2945 }
  2776 }
  2946 
  2777 
  2947 
  2778 
  2948 static void __exit rtl8139_cleanup_module (void)
  2779 static void __exit rtl8139_cleanup_module (void)
  2949 {
  2780 {
  2950 	/* EtherCAT >>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
  2781 	pci_unregister_driver (&rtl8139_pci_driver);
  2951 
       
  2952 	printk(KERN_INFO "Cleaning up RTL8139-EtherCAT module...\n");
       
  2953 
       
  2954 	if (rtl_ec_net_dev) {
       
  2955 		printk(KERN_INFO "Closing EtherCAT device...\n");
       
  2956 		ecdev_close(rtl_ec_dev);
       
  2957 		printk(KERN_INFO "Unregistering EtherCAT device...\n");
       
  2958 		ecdev_unregister(ec_device_master_index, rtl_ec_dev);
       
  2959 		rtl_ec_dev = NULL;
       
  2960 	}
       
  2961 
       
  2962 	pci_unregister_driver(&rtl8139_pci_driver);
       
  2963 
       
  2964 	printk(KERN_INFO "RTL8139-EtherCAT module cleaned up.\n");
       
  2965 
       
  2966 	/* EtherCAT <<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
       
  2967 }
  2782 }
  2968 
  2783 
  2969 
  2784 
  2970 module_init(rtl8139_init_module);
  2785 module_init(rtl8139_init_module);
  2971 module_exit(rtl8139_cleanup_module);
  2786 module_exit(rtl8139_cleanup_module);