PageRenderTime 60ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/net/sis900.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 2494 lines | 1551 code | 383 blank | 560 comment | 281 complexity | dade9c09a2827a4295355a85a280003a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* sis900.c: A SiS 900/7016 PCI Fast Ethernet driver for Linux.
  2. Copyright 1999 Silicon Integrated System Corporation
  3. Revision: 1.08.10 Apr. 2 2006
  4. Modified from the driver which is originally written by Donald Becker.
  5. This software may be used and distributed according to the terms
  6. of the GNU General Public License (GPL), incorporated herein by reference.
  7. Drivers based on this skeleton fall under the GPL and must retain
  8. the authorship (implicit copyright) notice.
  9. References:
  10. SiS 7016 Fast Ethernet PCI Bus 10/100 Mbps LAN Controller with OnNow Support,
  11. preliminary Rev. 1.0 Jan. 14, 1998
  12. SiS 900 Fast Ethernet PCI Bus 10/100 Mbps LAN Single Chip with OnNow Support,
  13. preliminary Rev. 1.0 Nov. 10, 1998
  14. SiS 7014 Single Chip 100BASE-TX/10BASE-T Physical Layer Solution,
  15. preliminary Rev. 1.0 Jan. 18, 1998
  16. Rev 1.08.10 Apr. 2 2006 Daniele Venzano add vlan (jumbo packets) support
  17. Rev 1.08.09 Sep. 19 2005 Daniele Venzano add Wake on LAN support
  18. Rev 1.08.08 Jan. 22 2005 Daniele Venzano use netif_msg for debugging messages
  19. Rev 1.08.07 Nov. 2 2003 Daniele Venzano <venza@brownhat.org> add suspend/resume support
  20. Rev 1.08.06 Sep. 24 2002 Mufasa Yang bug fix for Tx timeout & add SiS963 support
  21. Rev 1.08.05 Jun. 6 2002 Mufasa Yang bug fix for read_eeprom & Tx descriptor over-boundary
  22. Rev 1.08.04 Apr. 25 2002 Mufasa Yang <mufasa@sis.com.tw> added SiS962 support
  23. Rev 1.08.03 Feb. 1 2002 Matt Domsch <Matt_Domsch@dell.com> update to use library crc32 function
  24. Rev 1.08.02 Nov. 30 2001 Hui-Fen Hsu workaround for EDB & bug fix for dhcp problem
  25. Rev 1.08.01 Aug. 25 2001 Hui-Fen Hsu update for 630ET & workaround for ICS1893 PHY
  26. Rev 1.08.00 Jun. 11 2001 Hui-Fen Hsu workaround for RTL8201 PHY and some bug fix
  27. Rev 1.07.11 Apr. 2 2001 Hui-Fen Hsu updates PCI drivers to use the new pci_set_dma_mask for kernel 2.4.3
  28. Rev 1.07.10 Mar. 1 2001 Hui-Fen Hsu <hfhsu@sis.com.tw> some bug fix & 635M/B support
  29. Rev 1.07.09 Feb. 9 2001 Dave Jones <davej@suse.de> PCI enable cleanup
  30. Rev 1.07.08 Jan. 8 2001 Lei-Chun Chang added RTL8201 PHY support
  31. Rev 1.07.07 Nov. 29 2000 Lei-Chun Chang added kernel-doc extractable documentation and 630 workaround fix
  32. Rev 1.07.06 Nov. 7 2000 Jeff Garzik <jgarzik@pobox.com> some bug fix and cleaning
  33. Rev 1.07.05 Nov. 6 2000 metapirat<metapirat@gmx.de> contribute media type select by ifconfig
  34. Rev 1.07.04 Sep. 6 2000 Lei-Chun Chang added ICS1893 PHY support
  35. Rev 1.07.03 Aug. 24 2000 Lei-Chun Chang (lcchang@sis.com.tw) modified 630E equalizer workaround rule
  36. Rev 1.07.01 Aug. 08 2000 Ollie Lho minor update for SiS 630E and SiS 630E A1
  37. Rev 1.07 Mar. 07 2000 Ollie Lho bug fix in Rx buffer ring
  38. Rev 1.06.04 Feb. 11 2000 Jeff Garzik <jgarzik@pobox.com> softnet and init for kernel 2.4
  39. Rev 1.06.03 Dec. 23 1999 Ollie Lho Third release
  40. Rev 1.06.02 Nov. 23 1999 Ollie Lho bug in mac probing fixed
  41. Rev 1.06.01 Nov. 16 1999 Ollie Lho CRC calculation provide by Joseph Zbiciak (im14u2c@primenet.com)
  42. Rev 1.06 Nov. 4 1999 Ollie Lho (ollie@sis.com.tw) Second release
  43. Rev 1.05.05 Oct. 29 1999 Ollie Lho (ollie@sis.com.tw) Single buffer Tx/Rx
  44. Chin-Shan Li (lcs@sis.com.tw) Added AMD Am79c901 HomePNA PHY support
  45. Rev 1.05 Aug. 7 1999 Jim Huang (cmhuang@sis.com.tw) Initial release
  46. */
  47. #include <linux/module.h>
  48. #include <linux/moduleparam.h>
  49. #include <linux/kernel.h>
  50. #include <linux/sched.h>
  51. #include <linux/string.h>
  52. #include <linux/timer.h>
  53. #include <linux/errno.h>
  54. #include <linux/ioport.h>
  55. #include <linux/slab.h>
  56. #include <linux/interrupt.h>
  57. #include <linux/pci.h>
  58. #include <linux/netdevice.h>
  59. #include <linux/init.h>
  60. #include <linux/mii.h>
  61. #include <linux/etherdevice.h>
  62. #include <linux/skbuff.h>
  63. #include <linux/delay.h>
  64. #include <linux/ethtool.h>
  65. #include <linux/crc32.h>
  66. #include <linux/bitops.h>
  67. #include <linux/dma-mapping.h>
  68. #include <asm/processor.h> /* Processor type for cache alignment. */
  69. #include <asm/io.h>
  70. #include <asm/irq.h>
  71. #include <asm/uaccess.h> /* User space memory access functions */
  72. #include "sis900.h"
  73. #define SIS900_MODULE_NAME "sis900"
  74. #define SIS900_DRV_VERSION "v1.08.10 Apr. 2 2006"
  75. static const char version[] __devinitconst =
  76. KERN_INFO "sis900.c: " SIS900_DRV_VERSION "\n";
  77. static int max_interrupt_work = 40;
  78. static int multicast_filter_limit = 128;
  79. static int sis900_debug = -1; /* Use SIS900_DEF_MSG as value */
  80. #define SIS900_DEF_MSG \
  81. (NETIF_MSG_DRV | \
  82. NETIF_MSG_LINK | \
  83. NETIF_MSG_RX_ERR | \
  84. NETIF_MSG_TX_ERR)
  85. /* Time in jiffies before concluding the transmitter is hung. */
  86. #define TX_TIMEOUT (4*HZ)
  87. enum {
  88. SIS_900 = 0,
  89. SIS_7016
  90. };
  91. static const char * card_names[] = {
  92. "SiS 900 PCI Fast Ethernet",
  93. "SiS 7016 PCI Fast Ethernet"
  94. };
  95. static DEFINE_PCI_DEVICE_TABLE(sis900_pci_tbl) = {
  96. {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_900,
  97. PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_900},
  98. {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7016,
  99. PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_7016},
  100. {0,}
  101. };
  102. MODULE_DEVICE_TABLE (pci, sis900_pci_tbl);
  103. static void sis900_read_mode(struct net_device *net_dev, int *speed, int *duplex);
  104. static const struct mii_chip_info {
  105. const char * name;
  106. u16 phy_id0;
  107. u16 phy_id1;
  108. u8 phy_types;
  109. #define HOME 0x0001
  110. #define LAN 0x0002
  111. #define MIX 0x0003
  112. #define UNKNOWN 0x0
  113. } mii_chip_table[] = {
  114. { "SiS 900 Internal MII PHY", 0x001d, 0x8000, LAN },
  115. { "SiS 7014 Physical Layer Solution", 0x0016, 0xf830, LAN },
  116. { "SiS 900 on Foxconn 661 7MI", 0x0143, 0xBC70, LAN },
  117. { "Altimata AC101LF PHY", 0x0022, 0x5520, LAN },
  118. { "ADM 7001 LAN PHY", 0x002e, 0xcc60, LAN },
  119. { "AMD 79C901 10BASE-T PHY", 0x0000, 0x6B70, LAN },
  120. { "AMD 79C901 HomePNA PHY", 0x0000, 0x6B90, HOME},
  121. { "ICS LAN PHY", 0x0015, 0xF440, LAN },
  122. { "ICS LAN PHY", 0x0143, 0xBC70, LAN },
  123. { "NS 83851 PHY", 0x2000, 0x5C20, MIX },
  124. { "NS 83847 PHY", 0x2000, 0x5C30, MIX },
  125. { "Realtek RTL8201 PHY", 0x0000, 0x8200, LAN },
  126. { "VIA 6103 PHY", 0x0101, 0x8f20, LAN },
  127. {NULL,},
  128. };
  129. struct mii_phy {
  130. struct mii_phy * next;
  131. int phy_addr;
  132. u16 phy_id0;
  133. u16 phy_id1;
  134. u16 status;
  135. u8 phy_types;
  136. };
  137. typedef struct _BufferDesc {
  138. u32 link;
  139. u32 cmdsts;
  140. u32 bufptr;
  141. } BufferDesc;
  142. struct sis900_private {
  143. struct pci_dev * pci_dev;
  144. spinlock_t lock;
  145. struct mii_phy * mii;
  146. struct mii_phy * first_mii; /* record the first mii structure */
  147. unsigned int cur_phy;
  148. struct mii_if_info mii_info;
  149. struct timer_list timer; /* Link status detection timer. */
  150. u8 autong_complete; /* 1: auto-negotiate complete */
  151. u32 msg_enable;
  152. unsigned int cur_rx, dirty_rx; /* producer/comsumer pointers for Tx/Rx ring */
  153. unsigned int cur_tx, dirty_tx;
  154. /* The saved address of a sent/receive-in-place packet buffer */
  155. struct sk_buff *tx_skbuff[NUM_TX_DESC];
  156. struct sk_buff *rx_skbuff[NUM_RX_DESC];
  157. BufferDesc *tx_ring;
  158. BufferDesc *rx_ring;
  159. dma_addr_t tx_ring_dma;
  160. dma_addr_t rx_ring_dma;
  161. unsigned int tx_full; /* The Tx queue is full. */
  162. u8 host_bridge_rev;
  163. u8 chipset_rev;
  164. };
  165. MODULE_AUTHOR("Jim Huang <cmhuang@sis.com.tw>, Ollie Lho <ollie@sis.com.tw>");
  166. MODULE_DESCRIPTION("SiS 900 PCI Fast Ethernet driver");
  167. MODULE_LICENSE("GPL");
  168. module_param(multicast_filter_limit, int, 0444);
  169. module_param(max_interrupt_work, int, 0444);
  170. module_param(sis900_debug, int, 0444);
  171. MODULE_PARM_DESC(multicast_filter_limit, "SiS 900/7016 maximum number of filtered multicast addresses");
  172. MODULE_PARM_DESC(max_interrupt_work, "SiS 900/7016 maximum events handled per interrupt");
  173. MODULE_PARM_DESC(sis900_debug, "SiS 900/7016 bitmapped debugging message level");
  174. #ifdef CONFIG_NET_POLL_CONTROLLER
  175. static void sis900_poll(struct net_device *dev);
  176. #endif
  177. static int sis900_open(struct net_device *net_dev);
  178. static int sis900_mii_probe (struct net_device * net_dev);
  179. static void sis900_init_rxfilter (struct net_device * net_dev);
  180. static u16 read_eeprom(long ioaddr, int location);
  181. static int mdio_read(struct net_device *net_dev, int phy_id, int location);
  182. static void mdio_write(struct net_device *net_dev, int phy_id, int location, int val);
  183. static void sis900_timer(unsigned long data);
  184. static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy);
  185. static void sis900_tx_timeout(struct net_device *net_dev);
  186. static void sis900_init_tx_ring(struct net_device *net_dev);
  187. static void sis900_init_rx_ring(struct net_device *net_dev);
  188. static netdev_tx_t sis900_start_xmit(struct sk_buff *skb,
  189. struct net_device *net_dev);
  190. static int sis900_rx(struct net_device *net_dev);
  191. static void sis900_finish_xmit (struct net_device *net_dev);
  192. static irqreturn_t sis900_interrupt(int irq, void *dev_instance);
  193. static int sis900_close(struct net_device *net_dev);
  194. static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd);
  195. static u16 sis900_mcast_bitnr(u8 *addr, u8 revision);
  196. static void set_rx_mode(struct net_device *net_dev);
  197. static void sis900_reset(struct net_device *net_dev);
  198. static void sis630_set_eq(struct net_device *net_dev, u8 revision);
  199. static int sis900_set_config(struct net_device *dev, struct ifmap *map);
  200. static u16 sis900_default_phy(struct net_device * net_dev);
  201. static void sis900_set_capability( struct net_device *net_dev ,struct mii_phy *phy);
  202. static u16 sis900_reset_phy(struct net_device *net_dev, int phy_addr);
  203. static void sis900_auto_negotiate(struct net_device *net_dev, int phy_addr);
  204. static void sis900_set_mode (long ioaddr, int speed, int duplex);
  205. static const struct ethtool_ops sis900_ethtool_ops;
  206. /**
  207. * sis900_get_mac_addr - Get MAC address for stand alone SiS900 model
  208. * @pci_dev: the sis900 pci device
  209. * @net_dev: the net device to get address for
  210. *
  211. * Older SiS900 and friends, use EEPROM to store MAC address.
  212. * MAC address is read from read_eeprom() into @net_dev->dev_addr and
  213. * @net_dev->perm_addr.
  214. */
  215. static int __devinit sis900_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
  216. {
  217. long ioaddr = pci_resource_start(pci_dev, 0);
  218. u16 signature;
  219. int i;
  220. /* check to see if we have sane EEPROM */
  221. signature = (u16) read_eeprom(ioaddr, EEPROMSignature);
  222. if (signature == 0xffff || signature == 0x0000) {
  223. printk (KERN_WARNING "%s: Error EERPOM read %x\n",
  224. pci_name(pci_dev), signature);
  225. return 0;
  226. }
  227. /* get MAC address from EEPROM */
  228. for (i = 0; i < 3; i++)
  229. ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
  230. /* Store MAC Address in perm_addr */
  231. memcpy(net_dev->perm_addr, net_dev->dev_addr, ETH_ALEN);
  232. return 1;
  233. }
  234. /**
  235. * sis630e_get_mac_addr - Get MAC address for SiS630E model
  236. * @pci_dev: the sis900 pci device
  237. * @net_dev: the net device to get address for
  238. *
  239. * SiS630E model, use APC CMOS RAM to store MAC address.
  240. * APC CMOS RAM is accessed through ISA bridge.
  241. * MAC address is read into @net_dev->dev_addr and
  242. * @net_dev->perm_addr.
  243. */
  244. static int __devinit sis630e_get_mac_addr(struct pci_dev * pci_dev,
  245. struct net_device *net_dev)
  246. {
  247. struct pci_dev *isa_bridge = NULL;
  248. u8 reg;
  249. int i;
  250. isa_bridge = pci_get_device(PCI_VENDOR_ID_SI, 0x0008, isa_bridge);
  251. if (!isa_bridge)
  252. isa_bridge = pci_get_device(PCI_VENDOR_ID_SI, 0x0018, isa_bridge);
  253. if (!isa_bridge) {
  254. printk(KERN_WARNING "%s: Can not find ISA bridge\n",
  255. pci_name(pci_dev));
  256. return 0;
  257. }
  258. pci_read_config_byte(isa_bridge, 0x48, &reg);
  259. pci_write_config_byte(isa_bridge, 0x48, reg | 0x40);
  260. for (i = 0; i < 6; i++) {
  261. outb(0x09 + i, 0x70);
  262. ((u8 *)(net_dev->dev_addr))[i] = inb(0x71);
  263. }
  264. /* Store MAC Address in perm_addr */
  265. memcpy(net_dev->perm_addr, net_dev->dev_addr, ETH_ALEN);
  266. pci_write_config_byte(isa_bridge, 0x48, reg & ~0x40);
  267. pci_dev_put(isa_bridge);
  268. return 1;
  269. }
  270. /**
  271. * sis635_get_mac_addr - Get MAC address for SIS635 model
  272. * @pci_dev: the sis900 pci device
  273. * @net_dev: the net device to get address for
  274. *
  275. * SiS635 model, set MAC Reload Bit to load Mac address from APC
  276. * to rfdr. rfdr is accessed through rfcr. MAC address is read into
  277. * @net_dev->dev_addr and @net_dev->perm_addr.
  278. */
  279. static int __devinit sis635_get_mac_addr(struct pci_dev * pci_dev,
  280. struct net_device *net_dev)
  281. {
  282. long ioaddr = net_dev->base_addr;
  283. u32 rfcrSave;
  284. u32 i;
  285. rfcrSave = inl(rfcr + ioaddr);
  286. outl(rfcrSave | RELOAD, ioaddr + cr);
  287. outl(0, ioaddr + cr);
  288. /* disable packet filtering before setting filter */
  289. outl(rfcrSave & ~RFEN, rfcr + ioaddr);
  290. /* load MAC addr to filter data register */
  291. for (i = 0 ; i < 3 ; i++) {
  292. outl((i << RFADDR_shift), ioaddr + rfcr);
  293. *( ((u16 *)net_dev->dev_addr) + i) = inw(ioaddr + rfdr);
  294. }
  295. /* Store MAC Address in perm_addr */
  296. memcpy(net_dev->perm_addr, net_dev->dev_addr, ETH_ALEN);
  297. /* enable packet filtering */
  298. outl(rfcrSave | RFEN, rfcr + ioaddr);
  299. return 1;
  300. }
  301. /**
  302. * sis96x_get_mac_addr - Get MAC address for SiS962 or SiS963 model
  303. * @pci_dev: the sis900 pci device
  304. * @net_dev: the net device to get address for
  305. *
  306. * SiS962 or SiS963 model, use EEPROM to store MAC address. And EEPROM
  307. * is shared by
  308. * LAN and 1394. When access EEPROM, send EEREQ signal to hardware first
  309. * and wait for EEGNT. If EEGNT is ON, EEPROM is permitted to be access
  310. * by LAN, otherwise is not. After MAC address is read from EEPROM, send
  311. * EEDONE signal to refuse EEPROM access by LAN.
  312. * The EEPROM map of SiS962 or SiS963 is different to SiS900.
  313. * The signature field in SiS962 or SiS963 spec is meaningless.
  314. * MAC address is read into @net_dev->dev_addr and @net_dev->perm_addr.
  315. */
  316. static int __devinit sis96x_get_mac_addr(struct pci_dev * pci_dev,
  317. struct net_device *net_dev)
  318. {
  319. long ioaddr = net_dev->base_addr;
  320. long ee_addr = ioaddr + mear;
  321. u32 waittime = 0;
  322. int i;
  323. outl(EEREQ, ee_addr);
  324. while(waittime < 2000) {
  325. if(inl(ee_addr) & EEGNT) {
  326. /* get MAC address from EEPROM */
  327. for (i = 0; i < 3; i++)
  328. ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
  329. /* Store MAC Address in perm_addr */
  330. memcpy(net_dev->perm_addr, net_dev->dev_addr, ETH_ALEN);
  331. outl(EEDONE, ee_addr);
  332. return 1;
  333. } else {
  334. udelay(1);
  335. waittime ++;
  336. }
  337. }
  338. outl(EEDONE, ee_addr);
  339. return 0;
  340. }
  341. static const struct net_device_ops sis900_netdev_ops = {
  342. .ndo_open = sis900_open,
  343. .ndo_stop = sis900_close,
  344. .ndo_start_xmit = sis900_start_xmit,
  345. .ndo_set_config = sis900_set_config,
  346. .ndo_set_multicast_list = set_rx_mode,
  347. .ndo_change_mtu = eth_change_mtu,
  348. .ndo_validate_addr = eth_validate_addr,
  349. .ndo_set_mac_address = eth_mac_addr,
  350. .ndo_do_ioctl = mii_ioctl,
  351. .ndo_tx_timeout = sis900_tx_timeout,
  352. #ifdef CONFIG_NET_POLL_CONTROLLER
  353. .ndo_poll_controller = sis900_poll,
  354. #endif
  355. };
  356. /**
  357. * sis900_probe - Probe for sis900 device
  358. * @pci_dev: the sis900 pci device
  359. * @pci_id: the pci device ID
  360. *
  361. * Check and probe sis900 net device for @pci_dev.
  362. * Get mac address according to the chip revision,
  363. * and assign SiS900-specific entries in the device structure.
  364. * ie: sis900_open(), sis900_start_xmit(), sis900_close(), etc.
  365. */
  366. static int __devinit sis900_probe(struct pci_dev *pci_dev,
  367. const struct pci_device_id *pci_id)
  368. {
  369. struct sis900_private *sis_priv;
  370. struct net_device *net_dev;
  371. struct pci_dev *dev;
  372. dma_addr_t ring_dma;
  373. void *ring_space;
  374. long ioaddr;
  375. int i, ret;
  376. const char *card_name = card_names[pci_id->driver_data];
  377. const char *dev_name = pci_name(pci_dev);
  378. /* when built into the kernel, we only print version if device is found */
  379. #ifndef MODULE
  380. static int printed_version;
  381. if (!printed_version++)
  382. printk(version);
  383. #endif
  384. /* setup various bits in PCI command register */
  385. ret = pci_enable_device(pci_dev);
  386. if(ret) return ret;
  387. i = pci_set_dma_mask(pci_dev, DMA_BIT_MASK(32));
  388. if(i){
  389. printk(KERN_ERR "sis900.c: architecture does not support "
  390. "32bit PCI busmaster DMA\n");
  391. return i;
  392. }
  393. pci_set_master(pci_dev);
  394. net_dev = alloc_etherdev(sizeof(struct sis900_private));
  395. if (!net_dev)
  396. return -ENOMEM;
  397. SET_NETDEV_DEV(net_dev, &pci_dev->dev);
  398. /* We do a request_region() to register /proc/ioports info. */
  399. ioaddr = pci_resource_start(pci_dev, 0);
  400. ret = pci_request_regions(pci_dev, "sis900");
  401. if (ret)
  402. goto err_out;
  403. sis_priv = netdev_priv(net_dev);
  404. net_dev->base_addr = ioaddr;
  405. net_dev->irq = pci_dev->irq;
  406. sis_priv->pci_dev = pci_dev;
  407. spin_lock_init(&sis_priv->lock);
  408. pci_set_drvdata(pci_dev, net_dev);
  409. ring_space = pci_alloc_consistent(pci_dev, TX_TOTAL_SIZE, &ring_dma);
  410. if (!ring_space) {
  411. ret = -ENOMEM;
  412. goto err_out_cleardev;
  413. }
  414. sis_priv->tx_ring = (BufferDesc *)ring_space;
  415. sis_priv->tx_ring_dma = ring_dma;
  416. ring_space = pci_alloc_consistent(pci_dev, RX_TOTAL_SIZE, &ring_dma);
  417. if (!ring_space) {
  418. ret = -ENOMEM;
  419. goto err_unmap_tx;
  420. }
  421. sis_priv->rx_ring = (BufferDesc *)ring_space;
  422. sis_priv->rx_ring_dma = ring_dma;
  423. /* The SiS900-specific entries in the device structure. */
  424. net_dev->netdev_ops = &sis900_netdev_ops;
  425. net_dev->watchdog_timeo = TX_TIMEOUT;
  426. net_dev->ethtool_ops = &sis900_ethtool_ops;
  427. if (sis900_debug > 0)
  428. sis_priv->msg_enable = sis900_debug;
  429. else
  430. sis_priv->msg_enable = SIS900_DEF_MSG;
  431. sis_priv->mii_info.dev = net_dev;
  432. sis_priv->mii_info.mdio_read = mdio_read;
  433. sis_priv->mii_info.mdio_write = mdio_write;
  434. sis_priv->mii_info.phy_id_mask = 0x1f;
  435. sis_priv->mii_info.reg_num_mask = 0x1f;
  436. /* Get Mac address according to the chip revision */
  437. sis_priv->chipset_rev = pci_dev->revision;
  438. if(netif_msg_probe(sis_priv))
  439. printk(KERN_DEBUG "%s: detected revision %2.2x, "
  440. "trying to get MAC address...\n",
  441. dev_name, sis_priv->chipset_rev);
  442. ret = 0;
  443. if (sis_priv->chipset_rev == SIS630E_900_REV)
  444. ret = sis630e_get_mac_addr(pci_dev, net_dev);
  445. else if ((sis_priv->chipset_rev > 0x81) && (sis_priv->chipset_rev <= 0x90) )
  446. ret = sis635_get_mac_addr(pci_dev, net_dev);
  447. else if (sis_priv->chipset_rev == SIS96x_900_REV)
  448. ret = sis96x_get_mac_addr(pci_dev, net_dev);
  449. else
  450. ret = sis900_get_mac_addr(pci_dev, net_dev);
  451. if (!ret || !is_valid_ether_addr(net_dev->dev_addr)) {
  452. random_ether_addr(net_dev->dev_addr);
  453. printk(KERN_WARNING "%s: Unreadable or invalid MAC address,"
  454. "using random generated one\n", dev_name);
  455. }
  456. /* 630ET : set the mii access mode as software-mode */
  457. if (sis_priv->chipset_rev == SIS630ET_900_REV)
  458. outl(ACCESSMODE | inl(ioaddr + cr), ioaddr + cr);
  459. /* probe for mii transceiver */
  460. if (sis900_mii_probe(net_dev) == 0) {
  461. printk(KERN_WARNING "%s: Error probing MII device.\n",
  462. dev_name);
  463. ret = -ENODEV;
  464. goto err_unmap_rx;
  465. }
  466. /* save our host bridge revision */
  467. dev = pci_get_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_630, NULL);
  468. if (dev) {
  469. sis_priv->host_bridge_rev = dev->revision;
  470. pci_dev_put(dev);
  471. }
  472. ret = register_netdev(net_dev);
  473. if (ret)
  474. goto err_unmap_rx;
  475. /* print some information about our NIC */
  476. printk(KERN_INFO "%s: %s at %#lx, IRQ %d, %pM\n",
  477. net_dev->name, card_name, ioaddr, net_dev->irq,
  478. net_dev->dev_addr);
  479. /* Detect Wake on Lan support */
  480. ret = (inl(net_dev->base_addr + CFGPMC) & PMESP) >> 27;
  481. if (netif_msg_probe(sis_priv) && (ret & PME_D3C) == 0)
  482. printk(KERN_INFO "%s: Wake on LAN only available from suspend to RAM.", net_dev->name);
  483. return 0;
  484. err_unmap_rx:
  485. pci_free_consistent(pci_dev, RX_TOTAL_SIZE, sis_priv->rx_ring,
  486. sis_priv->rx_ring_dma);
  487. err_unmap_tx:
  488. pci_free_consistent(pci_dev, TX_TOTAL_SIZE, sis_priv->tx_ring,
  489. sis_priv->tx_ring_dma);
  490. err_out_cleardev:
  491. pci_set_drvdata(pci_dev, NULL);
  492. pci_release_regions(pci_dev);
  493. err_out:
  494. free_netdev(net_dev);
  495. return ret;
  496. }
  497. /**
  498. * sis900_mii_probe - Probe MII PHY for sis900
  499. * @net_dev: the net device to probe for
  500. *
  501. * Search for total of 32 possible mii phy addresses.
  502. * Identify and set current phy if found one,
  503. * return error if it failed to found.
  504. */
  505. static int __devinit sis900_mii_probe(struct net_device * net_dev)
  506. {
  507. struct sis900_private *sis_priv = netdev_priv(net_dev);
  508. const char *dev_name = pci_name(sis_priv->pci_dev);
  509. u16 poll_bit = MII_STAT_LINK, status = 0;
  510. unsigned long timeout = jiffies + 5 * HZ;
  511. int phy_addr;
  512. sis_priv->mii = NULL;
  513. /* search for total of 32 possible mii phy addresses */
  514. for (phy_addr = 0; phy_addr < 32; phy_addr++) {
  515. struct mii_phy * mii_phy = NULL;
  516. u16 mii_status;
  517. int i;
  518. mii_phy = NULL;
  519. for(i = 0; i < 2; i++)
  520. mii_status = mdio_read(net_dev, phy_addr, MII_STATUS);
  521. if (mii_status == 0xffff || mii_status == 0x0000) {
  522. if (netif_msg_probe(sis_priv))
  523. printk(KERN_DEBUG "%s: MII at address %d"
  524. " not accessible\n",
  525. dev_name, phy_addr);
  526. continue;
  527. }
  528. if ((mii_phy = kmalloc(sizeof(struct mii_phy), GFP_KERNEL)) == NULL) {
  529. printk(KERN_WARNING "Cannot allocate mem for struct mii_phy\n");
  530. mii_phy = sis_priv->first_mii;
  531. while (mii_phy) {
  532. struct mii_phy *phy;
  533. phy = mii_phy;
  534. mii_phy = mii_phy->next;
  535. kfree(phy);
  536. }
  537. return 0;
  538. }
  539. mii_phy->phy_id0 = mdio_read(net_dev, phy_addr, MII_PHY_ID0);
  540. mii_phy->phy_id1 = mdio_read(net_dev, phy_addr, MII_PHY_ID1);
  541. mii_phy->phy_addr = phy_addr;
  542. mii_phy->status = mii_status;
  543. mii_phy->next = sis_priv->mii;
  544. sis_priv->mii = mii_phy;
  545. sis_priv->first_mii = mii_phy;
  546. for (i = 0; mii_chip_table[i].phy_id1; i++)
  547. if ((mii_phy->phy_id0 == mii_chip_table[i].phy_id0 ) &&
  548. ((mii_phy->phy_id1 & 0xFFF0) == mii_chip_table[i].phy_id1)){
  549. mii_phy->phy_types = mii_chip_table[i].phy_types;
  550. if (mii_chip_table[i].phy_types == MIX)
  551. mii_phy->phy_types =
  552. (mii_status & (MII_STAT_CAN_TX_FDX | MII_STAT_CAN_TX)) ? LAN : HOME;
  553. printk(KERN_INFO "%s: %s transceiver found "
  554. "at address %d.\n",
  555. dev_name,
  556. mii_chip_table[i].name,
  557. phy_addr);
  558. break;
  559. }
  560. if( !mii_chip_table[i].phy_id1 ) {
  561. printk(KERN_INFO "%s: Unknown PHY transceiver found at address %d.\n",
  562. dev_name, phy_addr);
  563. mii_phy->phy_types = UNKNOWN;
  564. }
  565. }
  566. if (sis_priv->mii == NULL) {
  567. printk(KERN_INFO "%s: No MII transceivers found!\n", dev_name);
  568. return 0;
  569. }
  570. /* select default PHY for mac */
  571. sis_priv->mii = NULL;
  572. sis900_default_phy( net_dev );
  573. /* Reset phy if default phy is internal sis900 */
  574. if ((sis_priv->mii->phy_id0 == 0x001D) &&
  575. ((sis_priv->mii->phy_id1&0xFFF0) == 0x8000))
  576. status = sis900_reset_phy(net_dev, sis_priv->cur_phy);
  577. /* workaround for ICS1893 PHY */
  578. if ((sis_priv->mii->phy_id0 == 0x0015) &&
  579. ((sis_priv->mii->phy_id1&0xFFF0) == 0xF440))
  580. mdio_write(net_dev, sis_priv->cur_phy, 0x0018, 0xD200);
  581. if(status & MII_STAT_LINK){
  582. while (poll_bit) {
  583. yield();
  584. poll_bit ^= (mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS) & poll_bit);
  585. if (time_after_eq(jiffies, timeout)) {
  586. printk(KERN_WARNING "%s: reset phy and link down now\n",
  587. dev_name);
  588. return -ETIME;
  589. }
  590. }
  591. }
  592. if (sis_priv->chipset_rev == SIS630E_900_REV) {
  593. /* SiS 630E has some bugs on default value of PHY registers */
  594. mdio_write(net_dev, sis_priv->cur_phy, MII_ANADV, 0x05e1);
  595. mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG1, 0x22);
  596. mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG2, 0xff00);
  597. mdio_write(net_dev, sis_priv->cur_phy, MII_MASK, 0xffc0);
  598. //mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, 0x1000);
  599. }
  600. if (sis_priv->mii->status & MII_STAT_LINK)
  601. netif_carrier_on(net_dev);
  602. else
  603. netif_carrier_off(net_dev);
  604. return 1;
  605. }
  606. /**
  607. * sis900_default_phy - Select default PHY for sis900 mac.
  608. * @net_dev: the net device to probe for
  609. *
  610. * Select first detected PHY with link as default.
  611. * If no one is link on, select PHY whose types is HOME as default.
  612. * If HOME doesn't exist, select LAN.
  613. */
  614. static u16 sis900_default_phy(struct net_device * net_dev)
  615. {
  616. struct sis900_private *sis_priv = netdev_priv(net_dev);
  617. struct mii_phy *phy = NULL, *phy_home = NULL,
  618. *default_phy = NULL, *phy_lan = NULL;
  619. u16 status;
  620. for (phy=sis_priv->first_mii; phy; phy=phy->next) {
  621. status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
  622. status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
  623. /* Link ON & Not select default PHY & not ghost PHY */
  624. if ((status & MII_STAT_LINK) && !default_phy &&
  625. (phy->phy_types != UNKNOWN))
  626. default_phy = phy;
  627. else {
  628. status = mdio_read(net_dev, phy->phy_addr, MII_CONTROL);
  629. mdio_write(net_dev, phy->phy_addr, MII_CONTROL,
  630. status | MII_CNTL_AUTO | MII_CNTL_ISOLATE);
  631. if (phy->phy_types == HOME)
  632. phy_home = phy;
  633. else if(phy->phy_types == LAN)
  634. phy_lan = phy;
  635. }
  636. }
  637. if (!default_phy && phy_home)
  638. default_phy = phy_home;
  639. else if (!default_phy && phy_lan)
  640. default_phy = phy_lan;
  641. else if (!default_phy)
  642. default_phy = sis_priv->first_mii;
  643. if (sis_priv->mii != default_phy) {
  644. sis_priv->mii = default_phy;
  645. sis_priv->cur_phy = default_phy->phy_addr;
  646. printk(KERN_INFO "%s: Using transceiver found at address %d as default\n",
  647. pci_name(sis_priv->pci_dev), sis_priv->cur_phy);
  648. }
  649. sis_priv->mii_info.phy_id = sis_priv->cur_phy;
  650. status = mdio_read(net_dev, sis_priv->cur_phy, MII_CONTROL);
  651. status &= (~MII_CNTL_ISOLATE);
  652. mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, status);
  653. status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
  654. status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
  655. return status;
  656. }
  657. /**
  658. * sis900_set_capability - set the media capability of network adapter.
  659. * @net_dev : the net device to probe for
  660. * @phy : default PHY
  661. *
  662. * Set the media capability of network adapter according to
  663. * mii status register. It's necessary before auto-negotiate.
  664. */
  665. static void sis900_set_capability(struct net_device *net_dev, struct mii_phy *phy)
  666. {
  667. u16 cap;
  668. u16 status;
  669. status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
  670. status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
  671. cap = MII_NWAY_CSMA_CD |
  672. ((phy->status & MII_STAT_CAN_TX_FDX)? MII_NWAY_TX_FDX:0) |
  673. ((phy->status & MII_STAT_CAN_TX) ? MII_NWAY_TX:0) |
  674. ((phy->status & MII_STAT_CAN_T_FDX) ? MII_NWAY_T_FDX:0)|
  675. ((phy->status & MII_STAT_CAN_T) ? MII_NWAY_T:0);
  676. mdio_write(net_dev, phy->phy_addr, MII_ANADV, cap);
  677. }
  678. /* Delay between EEPROM clock transitions. */
  679. #define eeprom_delay() inl(ee_addr)
  680. /**
  681. * read_eeprom - Read Serial EEPROM
  682. * @ioaddr: base i/o address
  683. * @location: the EEPROM location to read
  684. *
  685. * Read Serial EEPROM through EEPROM Access Register.
  686. * Note that location is in word (16 bits) unit
  687. */
  688. static u16 __devinit read_eeprom(long ioaddr, int location)
  689. {
  690. int i;
  691. u16 retval = 0;
  692. long ee_addr = ioaddr + mear;
  693. u32 read_cmd = location | EEread;
  694. outl(0, ee_addr);
  695. eeprom_delay();
  696. outl(EECS, ee_addr);
  697. eeprom_delay();
  698. /* Shift the read command (9) bits out. */
  699. for (i = 8; i >= 0; i--) {
  700. u32 dataval = (read_cmd & (1 << i)) ? EEDI | EECS : EECS;
  701. outl(dataval, ee_addr);
  702. eeprom_delay();
  703. outl(dataval | EECLK, ee_addr);
  704. eeprom_delay();
  705. }
  706. outl(EECS, ee_addr);
  707. eeprom_delay();
  708. /* read the 16-bits data in */
  709. for (i = 16; i > 0; i--) {
  710. outl(EECS, ee_addr);
  711. eeprom_delay();
  712. outl(EECS | EECLK, ee_addr);
  713. eeprom_delay();
  714. retval = (retval << 1) | ((inl(ee_addr) & EEDO) ? 1 : 0);
  715. eeprom_delay();
  716. }
  717. /* Terminate the EEPROM access. */
  718. outl(0, ee_addr);
  719. eeprom_delay();
  720. return retval;
  721. }
  722. /* Read and write the MII management registers using software-generated
  723. serial MDIO protocol. Note that the command bits and data bits are
  724. send out separately */
  725. #define mdio_delay() inl(mdio_addr)
  726. static void mdio_idle(long mdio_addr)
  727. {
  728. outl(MDIO | MDDIR, mdio_addr);
  729. mdio_delay();
  730. outl(MDIO | MDDIR | MDC, mdio_addr);
  731. }
  732. /* Syncronize the MII management interface by shifting 32 one bits out. */
  733. static void mdio_reset(long mdio_addr)
  734. {
  735. int i;
  736. for (i = 31; i >= 0; i--) {
  737. outl(MDDIR | MDIO, mdio_addr);
  738. mdio_delay();
  739. outl(MDDIR | MDIO | MDC, mdio_addr);
  740. mdio_delay();
  741. }
  742. }
  743. /**
  744. * mdio_read - read MII PHY register
  745. * @net_dev: the net device to read
  746. * @phy_id: the phy address to read
  747. * @location: the phy regiester id to read
  748. *
  749. * Read MII registers through MDIO and MDC
  750. * using MDIO management frame structure and protocol(defined by ISO/IEC).
  751. * Please see SiS7014 or ICS spec
  752. */
  753. static int mdio_read(struct net_device *net_dev, int phy_id, int location)
  754. {
  755. long mdio_addr = net_dev->base_addr + mear;
  756. int mii_cmd = MIIread|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
  757. u16 retval = 0;
  758. int i;
  759. mdio_reset(mdio_addr);
  760. mdio_idle(mdio_addr);
  761. for (i = 15; i >= 0; i--) {
  762. int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
  763. outl(dataval, mdio_addr);
  764. mdio_delay();
  765. outl(dataval | MDC, mdio_addr);
  766. mdio_delay();
  767. }
  768. /* Read the 16 data bits. */
  769. for (i = 16; i > 0; i--) {
  770. outl(0, mdio_addr);
  771. mdio_delay();
  772. retval = (retval << 1) | ((inl(mdio_addr) & MDIO) ? 1 : 0);
  773. outl(MDC, mdio_addr);
  774. mdio_delay();
  775. }
  776. outl(0x00, mdio_addr);
  777. return retval;
  778. }
  779. /**
  780. * mdio_write - write MII PHY register
  781. * @net_dev: the net device to write
  782. * @phy_id: the phy address to write
  783. * @location: the phy regiester id to write
  784. * @value: the register value to write with
  785. *
  786. * Write MII registers with @value through MDIO and MDC
  787. * using MDIO management frame structure and protocol(defined by ISO/IEC)
  788. * please see SiS7014 or ICS spec
  789. */
  790. static void mdio_write(struct net_device *net_dev, int phy_id, int location,
  791. int value)
  792. {
  793. long mdio_addr = net_dev->base_addr + mear;
  794. int mii_cmd = MIIwrite|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
  795. int i;
  796. mdio_reset(mdio_addr);
  797. mdio_idle(mdio_addr);
  798. /* Shift the command bits out. */
  799. for (i = 15; i >= 0; i--) {
  800. int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
  801. outb(dataval, mdio_addr);
  802. mdio_delay();
  803. outb(dataval | MDC, mdio_addr);
  804. mdio_delay();
  805. }
  806. mdio_delay();
  807. /* Shift the value bits out. */
  808. for (i = 15; i >= 0; i--) {
  809. int dataval = (value & (1 << i)) ? MDDIR | MDIO : MDDIR;
  810. outl(dataval, mdio_addr);
  811. mdio_delay();
  812. outl(dataval | MDC, mdio_addr);
  813. mdio_delay();
  814. }
  815. mdio_delay();
  816. /* Clear out extra bits. */
  817. for (i = 2; i > 0; i--) {
  818. outb(0, mdio_addr);
  819. mdio_delay();
  820. outb(MDC, mdio_addr);
  821. mdio_delay();
  822. }
  823. outl(0x00, mdio_addr);
  824. }
  825. /**
  826. * sis900_reset_phy - reset sis900 mii phy.
  827. * @net_dev: the net device to write
  828. * @phy_addr: default phy address
  829. *
  830. * Some specific phy can't work properly without reset.
  831. * This function will be called during initialization and
  832. * link status change from ON to DOWN.
  833. */
  834. static u16 sis900_reset_phy(struct net_device *net_dev, int phy_addr)
  835. {
  836. int i;
  837. u16 status;
  838. for (i = 0; i < 2; i++)
  839. status = mdio_read(net_dev, phy_addr, MII_STATUS);
  840. mdio_write( net_dev, phy_addr, MII_CONTROL, MII_CNTL_RESET );
  841. return status;
  842. }
  843. #ifdef CONFIG_NET_POLL_CONTROLLER
  844. /*
  845. * Polling 'interrupt' - used by things like netconsole to send skbs
  846. * without having to re-enable interrupts. It's not called while
  847. * the interrupt routine is executing.
  848. */
  849. static void sis900_poll(struct net_device *dev)
  850. {
  851. disable_irq(dev->irq);
  852. sis900_interrupt(dev->irq, dev);
  853. enable_irq(dev->irq);
  854. }
  855. #endif
  856. /**
  857. * sis900_open - open sis900 device
  858. * @net_dev: the net device to open
  859. *
  860. * Do some initialization and start net interface.
  861. * enable interrupts and set sis900 timer.
  862. */
  863. static int
  864. sis900_open(struct net_device *net_dev)
  865. {
  866. struct sis900_private *sis_priv = netdev_priv(net_dev);
  867. long ioaddr = net_dev->base_addr;
  868. int ret;
  869. /* Soft reset the chip. */
  870. sis900_reset(net_dev);
  871. /* Equalizer workaround Rule */
  872. sis630_set_eq(net_dev, sis_priv->chipset_rev);
  873. ret = request_irq(net_dev->irq, sis900_interrupt, IRQF_SHARED,
  874. net_dev->name, net_dev);
  875. if (ret)
  876. return ret;
  877. sis900_init_rxfilter(net_dev);
  878. sis900_init_tx_ring(net_dev);
  879. sis900_init_rx_ring(net_dev);
  880. set_rx_mode(net_dev);
  881. netif_start_queue(net_dev);
  882. /* Workaround for EDB */
  883. sis900_set_mode(ioaddr, HW_SPEED_10_MBPS, FDX_CAPABLE_HALF_SELECTED);
  884. /* Enable all known interrupts by setting the interrupt mask. */
  885. outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
  886. outl(RxENA | inl(ioaddr + cr), ioaddr + cr);
  887. outl(IE, ioaddr + ier);
  888. sis900_check_mode(net_dev, sis_priv->mii);
  889. /* Set the timer to switch to check for link beat and perhaps switch
  890. to an alternate media type. */
  891. init_timer(&sis_priv->timer);
  892. sis_priv->timer.expires = jiffies + HZ;
  893. sis_priv->timer.data = (unsigned long)net_dev;
  894. sis_priv->timer.function = sis900_timer;
  895. add_timer(&sis_priv->timer);
  896. return 0;
  897. }
  898. /**
  899. * sis900_init_rxfilter - Initialize the Rx filter
  900. * @net_dev: the net device to initialize for
  901. *
  902. * Set receive filter address to our MAC address
  903. * and enable packet filtering.
  904. */
  905. static void
  906. sis900_init_rxfilter (struct net_device * net_dev)
  907. {
  908. struct sis900_private *sis_priv = netdev_priv(net_dev);
  909. long ioaddr = net_dev->base_addr;
  910. u32 rfcrSave;
  911. u32 i;
  912. rfcrSave = inl(rfcr + ioaddr);
  913. /* disable packet filtering before setting filter */
  914. outl(rfcrSave & ~RFEN, rfcr + ioaddr);
  915. /* load MAC addr to filter data register */
  916. for (i = 0 ; i < 3 ; i++) {
  917. u32 w;
  918. w = (u32) *((u16 *)(net_dev->dev_addr)+i);
  919. outl((i << RFADDR_shift), ioaddr + rfcr);
  920. outl(w, ioaddr + rfdr);
  921. if (netif_msg_hw(sis_priv)) {
  922. printk(KERN_DEBUG "%s: Receive Filter Addrss[%d]=%x\n",
  923. net_dev->name, i, inl(ioaddr + rfdr));
  924. }
  925. }
  926. /* enable packet filtering */
  927. outl(rfcrSave | RFEN, rfcr + ioaddr);
  928. }
  929. /**
  930. * sis900_init_tx_ring - Initialize the Tx descriptor ring
  931. * @net_dev: the net device to initialize for
  932. *
  933. * Initialize the Tx descriptor ring,
  934. */
  935. static void
  936. sis900_init_tx_ring(struct net_device *net_dev)
  937. {
  938. struct sis900_private *sis_priv = netdev_priv(net_dev);
  939. long ioaddr = net_dev->base_addr;
  940. int i;
  941. sis_priv->tx_full = 0;
  942. sis_priv->dirty_tx = sis_priv->cur_tx = 0;
  943. for (i = 0; i < NUM_TX_DESC; i++) {
  944. sis_priv->tx_skbuff[i] = NULL;
  945. sis_priv->tx_ring[i].link = sis_priv->tx_ring_dma +
  946. ((i+1)%NUM_TX_DESC)*sizeof(BufferDesc);
  947. sis_priv->tx_ring[i].cmdsts = 0;
  948. sis_priv->tx_ring[i].bufptr = 0;
  949. }
  950. /* load Transmit Descriptor Register */
  951. outl(sis_priv->tx_ring_dma, ioaddr + txdp);
  952. if (netif_msg_hw(sis_priv))
  953. printk(KERN_DEBUG "%s: TX descriptor register loaded with: %8.8x\n",
  954. net_dev->name, inl(ioaddr + txdp));
  955. }
  956. /**
  957. * sis900_init_rx_ring - Initialize the Rx descriptor ring
  958. * @net_dev: the net device to initialize for
  959. *
  960. * Initialize the Rx descriptor ring,
  961. * and pre-allocate recevie buffers (socket buffer)
  962. */
  963. static void
  964. sis900_init_rx_ring(struct net_device *net_dev)
  965. {
  966. struct sis900_private *sis_priv = netdev_priv(net_dev);
  967. long ioaddr = net_dev->base_addr;
  968. int i;
  969. sis_priv->cur_rx = 0;
  970. sis_priv->dirty_rx = 0;
  971. /* init RX descriptor */
  972. for (i = 0; i < NUM_RX_DESC; i++) {
  973. sis_priv->rx_skbuff[i] = NULL;
  974. sis_priv->rx_ring[i].link = sis_priv->rx_ring_dma +
  975. ((i+1)%NUM_RX_DESC)*sizeof(BufferDesc);
  976. sis_priv->rx_ring[i].cmdsts = 0;
  977. sis_priv->rx_ring[i].bufptr = 0;
  978. }
  979. /* allocate sock buffers */
  980. for (i = 0; i < NUM_RX_DESC; i++) {
  981. struct sk_buff *skb;
  982. if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
  983. /* not enough memory for skbuff, this makes a "hole"
  984. on the buffer ring, it is not clear how the
  985. hardware will react to this kind of degenerated
  986. buffer */
  987. break;
  988. }
  989. sis_priv->rx_skbuff[i] = skb;
  990. sis_priv->rx_ring[i].cmdsts = RX_BUF_SIZE;
  991. sis_priv->rx_ring[i].bufptr = pci_map_single(sis_priv->pci_dev,
  992. skb->data, RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  993. }
  994. sis_priv->dirty_rx = (unsigned int) (i - NUM_RX_DESC);
  995. /* load Receive Descriptor Register */
  996. outl(sis_priv->rx_ring_dma, ioaddr + rxdp);
  997. if (netif_msg_hw(sis_priv))
  998. printk(KERN_DEBUG "%s: RX descriptor register loaded with: %8.8x\n",
  999. net_dev->name, inl(ioaddr + rxdp));
  1000. }
  1001. /**
  1002. * sis630_set_eq - set phy equalizer value for 630 LAN
  1003. * @net_dev: the net device to set equalizer value
  1004. * @revision: 630 LAN revision number
  1005. *
  1006. * 630E equalizer workaround rule(Cyrus Huang 08/15)
  1007. * PHY register 14h(Test)
  1008. * Bit 14: 0 -- Automatically detect (default)
  1009. * 1 -- Manually set Equalizer filter
  1010. * Bit 13: 0 -- (Default)
  1011. * 1 -- Speed up convergence of equalizer setting
  1012. * Bit 9 : 0 -- (Default)
  1013. * 1 -- Disable Baseline Wander
  1014. * Bit 3~7 -- Equalizer filter setting
  1015. * Link ON: Set Bit 9, 13 to 1, Bit 14 to 0
  1016. * Then calculate equalizer value
  1017. * Then set equalizer value, and set Bit 14 to 1, Bit 9 to 0
  1018. * Link Off:Set Bit 13 to 1, Bit 14 to 0
  1019. * Calculate Equalizer value:
  1020. * When Link is ON and Bit 14 is 0, SIS900PHY will auto-detect proper equalizer value.
  1021. * When the equalizer is stable, this value is not a fixed value. It will be within
  1022. * a small range(eg. 7~9). Then we get a minimum and a maximum value(eg. min=7, max=9)
  1023. * 0 <= max <= 4 --> set equalizer to max
  1024. * 5 <= max <= 14 --> set equalizer to max+1 or set equalizer to max+2 if max == min
  1025. * max >= 15 --> set equalizer to max+5 or set equalizer to max+6 if max == min
  1026. */
  1027. static void sis630_set_eq(struct net_device *net_dev, u8 revision)
  1028. {
  1029. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1030. u16 reg14h, eq_value=0, max_value=0, min_value=0;
  1031. int i, maxcount=10;
  1032. if ( !(revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
  1033. revision == SIS630A_900_REV || revision == SIS630ET_900_REV) )
  1034. return;
  1035. if (netif_carrier_ok(net_dev)) {
  1036. reg14h = mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
  1037. mdio_write(net_dev, sis_priv->cur_phy, MII_RESV,
  1038. (0x2200 | reg14h) & 0xBFFF);
  1039. for (i=0; i < maxcount; i++) {
  1040. eq_value = (0x00F8 & mdio_read(net_dev,
  1041. sis_priv->cur_phy, MII_RESV)) >> 3;
  1042. if (i == 0)
  1043. max_value=min_value=eq_value;
  1044. max_value = (eq_value > max_value) ?
  1045. eq_value : max_value;
  1046. min_value = (eq_value < min_value) ?
  1047. eq_value : min_value;
  1048. }
  1049. /* 630E rule to determine the equalizer value */
  1050. if (revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
  1051. revision == SIS630ET_900_REV) {
  1052. if (max_value < 5)
  1053. eq_value = max_value;
  1054. else if (max_value >= 5 && max_value < 15)
  1055. eq_value = (max_value == min_value) ?
  1056. max_value+2 : max_value+1;
  1057. else if (max_value >= 15)
  1058. eq_value=(max_value == min_value) ?
  1059. max_value+6 : max_value+5;
  1060. }
  1061. /* 630B0&B1 rule to determine the equalizer value */
  1062. if (revision == SIS630A_900_REV &&
  1063. (sis_priv->host_bridge_rev == SIS630B0 ||
  1064. sis_priv->host_bridge_rev == SIS630B1)) {
  1065. if (max_value == 0)
  1066. eq_value = 3;
  1067. else
  1068. eq_value = (max_value + min_value + 1)/2;
  1069. }
  1070. /* write equalizer value and setting */
  1071. reg14h = mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
  1072. reg14h = (reg14h & 0xFF07) | ((eq_value << 3) & 0x00F8);
  1073. reg14h = (reg14h | 0x6000) & 0xFDFF;
  1074. mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, reg14h);
  1075. } else {
  1076. reg14h = mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
  1077. if (revision == SIS630A_900_REV &&
  1078. (sis_priv->host_bridge_rev == SIS630B0 ||
  1079. sis_priv->host_bridge_rev == SIS630B1))
  1080. mdio_write(net_dev, sis_priv->cur_phy, MII_RESV,
  1081. (reg14h | 0x2200) & 0xBFFF);
  1082. else
  1083. mdio_write(net_dev, sis_priv->cur_phy, MII_RESV,
  1084. (reg14h | 0x2000) & 0xBFFF);
  1085. }
  1086. }
  1087. /**
  1088. * sis900_timer - sis900 timer routine
  1089. * @data: pointer to sis900 net device
  1090. *
  1091. * On each timer ticks we check two things,
  1092. * link status (ON/OFF) and link mode (10/100/Full/Half)
  1093. */
  1094. static void sis900_timer(unsigned long data)
  1095. {
  1096. struct net_device *net_dev = (struct net_device *)data;
  1097. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1098. struct mii_phy *mii_phy = sis_priv->mii;
  1099. static const int next_tick = 5*HZ;
  1100. u16 status;
  1101. if (!sis_priv->autong_complete){
  1102. int uninitialized_var(speed), duplex = 0;
  1103. sis900_read_mode(net_dev, &speed, &duplex);
  1104. if (duplex){
  1105. sis900_set_mode(net_dev->base_addr, speed, duplex);
  1106. sis630_set_eq(net_dev, sis_priv->chipset_rev);
  1107. netif_start_queue(net_dev);
  1108. }
  1109. sis_priv->timer.expires = jiffies + HZ;
  1110. add_timer(&sis_priv->timer);
  1111. return;
  1112. }
  1113. status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
  1114. status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
  1115. /* Link OFF -> ON */
  1116. if (!netif_carrier_ok(net_dev)) {
  1117. LookForLink:
  1118. /* Search for new PHY */
  1119. status = sis900_default_phy(net_dev);
  1120. mii_phy = sis_priv->mii;
  1121. if (status & MII_STAT_LINK){
  1122. sis900_check_mode(net_dev, mii_phy);
  1123. netif_carrier_on(net_dev);
  1124. }
  1125. } else {
  1126. /* Link ON -> OFF */
  1127. if (!(status & MII_STAT_LINK)){
  1128. netif_carrier_off(net_dev);
  1129. if(netif_msg_link(sis_priv))
  1130. printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
  1131. /* Change mode issue */
  1132. if ((mii_phy->phy_id0 == 0x001D) &&
  1133. ((mii_phy->phy_id1 & 0xFFF0) == 0x8000))
  1134. sis900_reset_phy(net_dev, sis_priv->cur_phy);
  1135. sis630_set_eq(net_dev, sis_priv->chipset_rev);
  1136. goto LookForLink;
  1137. }
  1138. }
  1139. sis_priv->timer.expires = jiffies + next_tick;
  1140. add_timer(&sis_priv->timer);
  1141. }
  1142. /**
  1143. * sis900_check_mode - check the media mode for sis900
  1144. * @net_dev: the net device to be checked
  1145. * @mii_phy: the mii phy
  1146. *
  1147. * Older driver gets the media mode from mii status output
  1148. * register. Now we set our media capability and auto-negotiate
  1149. * to get the upper bound of speed and duplex between two ends.
  1150. * If the types of mii phy is HOME, it doesn't need to auto-negotiate
  1151. * and autong_complete should be set to 1.
  1152. */
  1153. static void sis900_check_mode(struct net_device *net_dev, struct mii_phy *mii_phy)
  1154. {
  1155. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1156. long ioaddr = net_dev->base_addr;
  1157. int speed, duplex;
  1158. if (mii_phy->phy_types == LAN) {
  1159. outl(~EXD & inl(ioaddr + cfg), ioaddr + cfg);
  1160. sis900_set_capability(net_dev , mii_phy);
  1161. sis900_auto_negotiate(net_dev, sis_priv->cur_phy);
  1162. } else {
  1163. outl(EXD | inl(ioaddr + cfg), ioaddr + cfg);
  1164. speed = HW_SPEED_HOME;
  1165. duplex = FDX_CAPABLE_HALF_SELECTED;
  1166. sis900_set_mode(ioaddr, speed, duplex);
  1167. sis_priv->autong_complete = 1;
  1168. }
  1169. }
  1170. /**
  1171. * sis900_set_mode - Set the media mode of mac register.
  1172. * @ioaddr: the address of the device
  1173. * @speed : the transmit speed to be determined
  1174. * @duplex: the duplex mode to be determined
  1175. *
  1176. * Set the media mode of mac register txcfg/rxcfg according to
  1177. * speed and duplex of phy. Bit EDB_MASTER_EN indicates the EDB
  1178. * bus is used instead of PCI bus. When this bit is set 1, the
  1179. * Max DMA Burst Size for TX/RX DMA should be no larger than 16
  1180. * double words.
  1181. */
  1182. static void sis900_set_mode (long ioaddr, int speed, int duplex)
  1183. {
  1184. u32 tx_flags = 0, rx_flags = 0;
  1185. if (inl(ioaddr + cfg) & EDB_MASTER_EN) {
  1186. tx_flags = TxATP | (DMA_BURST_64 << TxMXDMA_shift) |
  1187. (TX_FILL_THRESH << TxFILLT_shift);
  1188. rx_flags = DMA_BURST_64 << RxMXDMA_shift;
  1189. } else {
  1190. tx_flags = TxATP | (DMA_BURST_512 << TxMXDMA_shift) |
  1191. (TX_FILL_THRESH << TxFILLT_shift);
  1192. rx_flags = DMA_BURST_512 << RxMXDMA_shift;
  1193. }
  1194. if (speed == HW_SPEED_HOME || speed == HW_SPEED_10_MBPS) {
  1195. rx_flags |= (RxDRNT_10 << RxDRNT_shift);
  1196. tx_flags |= (TxDRNT_10 << TxDRNT_shift);
  1197. } else {
  1198. rx_flags |= (RxDRNT_100 << RxDRNT_shift);
  1199. tx_flags |= (TxDRNT_100 << TxDRNT_shift);
  1200. }
  1201. if (duplex == FDX_CAPABLE_FULL_SELECTED) {
  1202. tx_flags |= (TxCSI | TxHBI);
  1203. rx_flags |= RxATX;
  1204. }
  1205. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  1206. /* Can accept Jumbo packet */
  1207. rx_flags |= RxAJAB;
  1208. #endif
  1209. outl (tx_flags, ioaddr + txcfg);
  1210. outl (rx_flags, ioaddr + rxcfg);
  1211. }
  1212. /**
  1213. * sis900_auto_negotiate - Set the Auto-Negotiation Enable/Reset bit.
  1214. * @net_dev: the net device to read mode for
  1215. * @phy_addr: mii phy address
  1216. *
  1217. * If the adapter is link-on, set the auto-negotiate enable/reset bit.
  1218. * autong_complete should be set to 0 when starting auto-negotiation.
  1219. * autong_complete should be set to 1 if we didn't start auto-negotiation.
  1220. * sis900_timer will wait for link on again if autong_complete = 0.
  1221. */
  1222. static void sis900_auto_negotiate(struct net_device *net_dev, int phy_addr)
  1223. {
  1224. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1225. int i = 0;
  1226. u32 status;
  1227. for (i = 0; i < 2; i++)
  1228. status = mdio_read(net_dev, phy_addr, MII_STATUS);
  1229. if (!(status & MII_STAT_LINK)){
  1230. if(netif_msg_link(sis_priv))
  1231. printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
  1232. sis_priv->autong_complete = 1;
  1233. netif_carrier_off(net_dev);
  1234. return;
  1235. }
  1236. /* (Re)start AutoNegotiate */
  1237. mdio_write(net_dev, phy_addr, MII_CONTROL,
  1238. MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
  1239. sis_priv->autong_complete = 0;
  1240. }
  1241. /**
  1242. * sis900_read_mode - read media mode for sis900 internal phy
  1243. * @net_dev: the net device to read mode for
  1244. * @speed : the transmit speed to be determined
  1245. * @duplex : the duplex mode to be determined
  1246. *
  1247. * The capability of remote end will be put in mii register autorec
  1248. * after auto-negotiation. Use AND operation to get the upper bound
  1249. * of speed and duplex between two ends.
  1250. */
  1251. static void sis900_read_mode(struct net_device *net_dev, int *speed, int *duplex)
  1252. {
  1253. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1254. struct mii_phy *phy = sis_priv->mii;
  1255. int phy_addr = sis_priv->cur_phy;
  1256. u32 status;
  1257. u16 autoadv, autorec;
  1258. int i;
  1259. for (i = 0; i < 2; i++)
  1260. status = mdio_read(net_dev, phy_addr, MII_STATUS);
  1261. if (!(status & MII_STAT_LINK))
  1262. return;
  1263. /* AutoNegotiate completed */
  1264. autoadv = mdio_read(net_dev, phy_addr, MII_ANADV);
  1265. autorec = mdio_read(net_dev, phy_addr, MII_ANLPAR);
  1266. status = autoadv & autorec;
  1267. *speed = HW_SPEED_10_MBPS;
  1268. *duplex = FDX_CAPABLE_HALF_SELECTED;
  1269. if (status & (MII_NWAY_TX | MII_NWAY_TX_FDX))
  1270. *speed = HW_SPEED_100_MBPS;
  1271. if (status & ( MII_NWAY_TX_FDX | MII_NWAY_T_FDX))
  1272. *duplex = FDX_CAPABLE_FULL_SELECTED;
  1273. sis_priv->autong_complete = 1;
  1274. /* Workaround for Realtek RTL8201 PHY issue */
  1275. if ((phy->phy_id0 == 0x0000) && ((phy->phy_id1 & 0xFFF0) == 0x8200)) {
  1276. if (mdio_read(net_dev, phy_addr, MII_CONTROL) & MII_CNTL_FDX)
  1277. *duplex = FDX_CAPABLE_FULL_SELECTED;
  1278. if (mdio_read(net_dev, phy_addr, 0x0019) & 0x01)
  1279. *speed = HW_SPEED_100_MBPS;
  1280. }
  1281. if(netif_msg_link(sis_priv))
  1282. printk(KERN_INFO "%s: Media Link On %s %s-duplex\n",
  1283. net_dev->name,
  1284. *speed == HW_SPEED_100_MBPS ?
  1285. "100mbps" : "10mbps",
  1286. *duplex == FDX_CAPABLE_FULL_SELECTED ?
  1287. "full" : "half");
  1288. }
  1289. /**
  1290. * sis900_tx_timeout - sis900 transmit timeout routine
  1291. * @net_dev: the net device to transmit
  1292. *
  1293. * print transmit timeout status
  1294. * disable interrupts and do some tasks
  1295. */
  1296. static void sis900_tx_timeout(struct net_device *net_dev)
  1297. {
  1298. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1299. long ioaddr = net_dev->base_addr;
  1300. unsigned long flags;
  1301. int i;
  1302. if(netif_msg_tx_err(sis_priv))
  1303. printk(KERN_INFO "%s: Transmit timeout, status %8.8x %8.8x\n",
  1304. net_dev->name, inl(ioaddr + cr), inl(ioaddr + isr));
  1305. /* Disable interrupts by clearing the interrupt mask. */
  1306. outl(0x0000, ioaddr + imr);
  1307. /* use spinlock to prevent interrupt handler accessing buffer ring */
  1308. spin_lock_irqsave(&sis_priv->lock, flags);
  1309. /* discard unsent packets */
  1310. sis_priv->dirty_tx = sis_priv->cur_tx = 0;
  1311. for (i = 0; i < NUM_TX_DESC; i++) {
  1312. struct sk_buff *skb = sis_priv->tx_skbuff[i];
  1313. if (skb) {
  1314. pci_unmap_single(sis_priv->pci_dev,
  1315. sis_priv->tx_ring[i].bufptr, skb->len,
  1316. PCI_DMA_TODEVICE);
  1317. dev_kfree_skb_irq(skb);
  1318. sis_priv->tx_skbuff[i] = NULL;
  1319. sis_priv->tx_ring[i].cmdsts = 0;
  1320. sis_priv->tx_ring[i].bufptr = 0;
  1321. net_dev->stats.tx_dropped++;
  1322. }
  1323. }
  1324. sis_priv->tx_full = 0;
  1325. netif_wake_queue(net_dev);
  1326. spin_unlock_irqrestore(&sis_priv->lock, flags);
  1327. net_dev->trans_start = jiffies; /* prevent tx timeout */
  1328. /* load Transmit Descriptor Register */
  1329. outl(sis_priv->tx_ring_dma, ioaddr + txdp);
  1330. /* Enable all known interrupts by setting the interrupt mask. */
  1331. outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
  1332. }
  1333. /**
  1334. * sis900_start_xmit - sis900 start transmit routine
  1335. * @skb: socket buffer pointer to put the data being transmitted
  1336. * @net_dev: the net device to transmit with
  1337. *
  1338. * Set the transmit buffer descriptor,
  1339. * and write TxENA to enable transmit state machine.
  1340. * tell upper layer if the buffer is full
  1341. */
  1342. static netdev_tx_t
  1343. sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
  1344. {
  1345. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1346. long ioaddr = net_dev->base_addr;
  1347. unsigned int entry;
  1348. unsigned long flags;
  1349. unsigned int index_cur_tx, index_dirty_tx;
  1350. unsigned int count_dirty_tx;
  1351. /* Don't transmit data before the complete of auto-negotiation */
  1352. if(!sis_priv->autong_complete){
  1353. netif_stop_queue(net_dev);
  1354. return NETDEV_TX_BUSY;
  1355. }
  1356. spin_lock_irqsave(&sis_priv->lock, flags);
  1357. /* Calculate the next Tx descriptor entry. */
  1358. entry = sis_priv->cur_tx % NUM_TX_DESC;
  1359. sis_priv->tx_skbuff[entry] = skb;
  1360. /* set the transmit buffer descriptor and enable Transmit State Machine */
  1361. sis_priv->tx_ring[entry].bufptr = pci_map_single(sis_priv->pci_dev,
  1362. skb->data, skb->len, PCI_DMA_TODEVICE);
  1363. sis_priv->tx_ring[entry].cmdsts = (OWN | skb->len);
  1364. outl(TxENA | inl(ioaddr + cr), ioaddr + cr);
  1365. sis_priv->cur_tx ++;
  1366. index_cur_tx = sis_priv->cur_tx;
  1367. index_dirty_tx = sis_priv->dirty_tx;
  1368. for (count_dirty_tx = 0; index_cur_tx != index_dirty_tx; index_dirty_tx++)
  1369. count_dirty_tx ++;
  1370. if (index_cur_tx == index_dirty_tx) {
  1371. /* dirty_tx is met in the cycle of cur_tx, buffer full */
  1372. sis_priv->tx_full = 1;
  1373. netif_stop_queue(net_dev);
  1374. } else if (count_dirty_tx < NUM_TX_DESC) {
  1375. /* Typical path, tell upper layer that more transmission is possible */
  1376. netif_start_queue(net_dev);
  1377. } else {
  1378. /* buffer full, tell upper layer no more transmission */
  1379. sis_priv->tx_full = 1;
  1380. netif_stop_queue(net_dev);
  1381. }
  1382. spin_unlock_irqrestore(&sis_priv->lock, flags);
  1383. if (netif_msg_tx_queued(sis_priv))
  1384. printk(KERN_DEBUG "%s: Queued Tx packet at %p size %d "
  1385. "to slot %d.\n",
  1386. net_dev->name, skb->data, (int)skb->len, entry);
  1387. return NETDEV_TX_OK;
  1388. }
  1389. /**
  1390. * sis900_interrupt - sis900 interrupt handler
  1391. * @irq: the irq number
  1392. * @dev_instance: the client data object
  1393. *
  1394. * The interrupt handler does all of the Rx thread work,
  1395. * and cleans up after the Tx thread
  1396. */
  1397. static irqreturn_t sis900_interrupt(int irq, void *dev_instance)
  1398. {
  1399. struct net_device *net_dev = dev_instance;
  1400. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1401. int boguscnt = max_interrupt_work;
  1402. long ioaddr = net_dev->base_addr;
  1403. u32 status;
  1404. unsigned int handled = 0;
  1405. spin_lock (&sis_priv->lock);
  1406. do {
  1407. status = inl(ioaddr + isr);
  1408. if ((status & (HIBERR|TxURN|TxERR|TxIDLE|RxORN|RxERR|RxOK)) == 0)
  1409. /* nothing intresting happened */
  1410. break;
  1411. handled = 1;
  1412. /* why dow't we break after Tx/Rx case ?? keyword: full-duplex */
  1413. if (status & (RxORN | RxERR | RxOK))
  1414. /* Rx interrupt */
  1415. sis900_rx(net_dev);
  1416. if (status & (TxURN | TxERR | TxIDLE))
  1417. /* Tx interrupt */
  1418. sis900_finish_xmit(net_dev);
  1419. /* something strange happened !!! */
  1420. if (status & HIBERR) {
  1421. if(netif_msg_intr(sis_priv))
  1422. printk(KERN_INFO "%s: Abnormal interrupt, "
  1423. "status %#8.8x.\n", net_dev->name, status);
  1424. break;
  1425. }
  1426. if (--boguscnt < 0) {
  1427. if(netif_msg_intr(sis_priv))
  1428. printk(KERN_INFO "%s: Too much work at interrupt, "
  1429. "interrupt status = %#8.8x.\n",
  1430. net_dev->name, status);
  1431. break;
  1432. }
  1433. } while (1);
  1434. if(netif_msg_intr(sis_priv))
  1435. printk(KERN_DEBUG "%s: exiting interrupt, "
  1436. "interrupt status = 0x%#8.8x.\n",
  1437. net_dev->name, inl(ioaddr + isr));
  1438. spin_unlock (&sis_priv->lock);
  1439. return IRQ_RETVAL(handled);
  1440. }
  1441. /**
  1442. * sis900_rx - sis900 receive routine
  1443. * @net_dev: the net device which receives data
  1444. *
  1445. * Process receive interrupt events,
  1446. * put buffer to higher layer and refill buffer pool
  1447. * Note: This function is called by interrupt handler,
  1448. * don't do "too much" work here
  1449. */
  1450. static int sis900_rx(struct net_device *net_dev)
  1451. {
  1452. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1453. long ioaddr = net_dev->base_addr;
  1454. unsigned int entry = sis_priv->cur_rx % NUM_RX_DESC;
  1455. u32 rx_status = sis_priv->rx_ring[entry].cmdsts;
  1456. int rx_work_limit;
  1457. if (netif_msg_rx_status(sis_priv))
  1458. printk(KERN_DEBUG "sis900_rx, cur_rx:%4.4d, dirty_rx:%4.4d "
  1459. "status:0x%8.8x\n",
  1460. sis_priv->cur_rx, sis_priv->dirty_rx, rx_status);
  1461. rx_work_limit = sis_priv->dirty_rx + NUM_RX_DESC - sis_priv->cur_rx;
  1462. while (rx_status & OWN) {
  1463. unsigned int rx_size;
  1464. unsigned int data_size;
  1465. if (--rx_work_limit < 0)
  1466. break;
  1467. data_size = rx_status & DSIZE;
  1468. rx_size = data_size - CRC_SIZE;
  1469. #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
  1470. /* ``TOOLONG'' flag means jumbo packet received. */
  1471. if ((rx_status & TOOLONG) && data_size <= MAX_FRAME_SIZE)
  1472. rx_status &= (~ ((unsigned int)TOOLONG));
  1473. #endif
  1474. if (rx_status & (ABORT|OVERRUN|TOOLONG|RUNT|RXISERR|CRCERR|FAERR)) {
  1475. /* corrupted packet received */
  1476. if (netif_msg_rx_err(sis_priv))
  1477. printk(KERN_DEBUG "%s: Corrupted packet "
  1478. "received, buffer status = 0x%8.8x/%d.\n",
  1479. net_dev->name, rx_status, data_size);
  1480. net_dev->stats.rx_errors++;
  1481. if (rx_status & OVERRUN)
  1482. net_dev->stats.rx_over_errors++;
  1483. if (rx_status & (TOOLONG|RUNT))
  1484. net_dev->stats.rx_length_errors++;
  1485. if (rx_status & (RXISERR | FAERR))
  1486. net_dev->stats.rx_frame_errors++;
  1487. if (rx_status & CRCERR)
  1488. net_dev->stats.rx_crc_errors++;
  1489. /* reset buffer descriptor state */
  1490. sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
  1491. } else {
  1492. struct sk_buff * skb;
  1493. struct sk_buff * rx_skb;
  1494. pci_unmap_single(sis_priv->pci_dev,
  1495. sis_priv->rx_ring[entry].bufptr, RX_BUF_SIZE,
  1496. PCI_DMA_FROMDEVICE);
  1497. /* refill the Rx buffer, what if there is not enough
  1498. * memory for new socket buffer ?? */
  1499. if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
  1500. /*
  1501. * Not enough memory to refill the buffer
  1502. * so we need to recycle the old one so
  1503. * as to avoid creating a memory hole
  1504. * in the rx ring
  1505. */
  1506. skb = sis_priv->rx_skbuff[entry];
  1507. net_dev->stats.rx_dropped++;
  1508. goto refill_rx_ring;
  1509. }
  1510. /* This situation should never happen, but due to
  1511. some unknown bugs, it is possible that
  1512. we are working on NULL sk_buff :-( */
  1513. if (sis_priv->rx_skbuff[entry] == NULL) {
  1514. if (netif_msg_rx_err(sis_priv))
  1515. printk(KERN_WARNING "%s: NULL pointer "
  1516. "encountered in Rx ring\n"
  1517. "cur_rx:%4.4d, dirty_rx:%4.4d\n",
  1518. net_dev->name, sis_priv->cur_rx,
  1519. sis_priv->dirty_rx);
  1520. dev_kfree_skb(skb);
  1521. break;
  1522. }
  1523. /* give the socket buffer to upper layers */
  1524. rx_skb = sis_priv->rx_skbuff[entry];
  1525. skb_put(rx_skb, rx_size);
  1526. rx_skb->protocol = eth_type_trans(rx_skb, net_dev);
  1527. netif_rx(rx_skb);
  1528. /* some network statistics */
  1529. if ((rx_status & BCAST) == MCAST)
  1530. net_dev->stats.multicast++;
  1531. net_dev->stats.rx_bytes += rx_size;
  1532. net_dev->stats.rx_packets++;
  1533. sis_priv->dirty_rx++;
  1534. refill_rx_ring:
  1535. sis_priv->rx_skbuff[entry] = skb;
  1536. sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
  1537. sis_priv->rx_ring[entry].bufptr =
  1538. pci_map_single(sis_priv->pci_dev, skb->data,
  1539. RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  1540. }
  1541. sis_priv->cur_rx++;
  1542. entry = sis_priv->cur_rx % NUM_RX_DESC;
  1543. rx_status = sis_priv->rx_ring[entry].cmdsts;
  1544. } // while
  1545. /* refill the Rx buffer, what if the rate of refilling is slower
  1546. * than consuming ?? */
  1547. for (; sis_priv->cur_rx != sis_priv->dirty_rx; sis_priv->dirty_rx++) {
  1548. struct sk_buff *skb;
  1549. entry = sis_priv->dirty_rx % NUM_RX_DESC;
  1550. if (sis_priv->rx_skbuff[entry] == NULL) {
  1551. if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
  1552. /* not enough memory for skbuff, this makes a
  1553. * "hole" on the buffer ring, it is not clear
  1554. * how the hardware will react to this kind
  1555. * of degenerated buffer */
  1556. if (netif_msg_rx_err(sis_priv))
  1557. printk(KERN_INFO "%s: Memory squeeze, "
  1558. "deferring packet.\n",
  1559. net_dev->name);
  1560. net_dev->stats.rx_dropped++;
  1561. break;
  1562. }
  1563. sis_priv->rx_skbuff[entry] = skb;
  1564. sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
  1565. sis_priv->rx_ring[entry].bufptr =
  1566. pci_map_single(sis_priv->pci_dev, skb->data,
  1567. RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  1568. }
  1569. }
  1570. /* re-enable the potentially idle receive state matchine */
  1571. outl(RxENA | inl(ioaddr + cr), ioaddr + cr );
  1572. return 0;
  1573. }
  1574. /**
  1575. * sis900_finish_xmit - finish up transmission of packets
  1576. * @net_dev: the net device to be transmitted on
  1577. *
  1578. * Check for error condition and free socket buffer etc
  1579. * schedule for more transmission as needed
  1580. * Note: This function is called by interrupt handler,
  1581. * don't do "too much" work here
  1582. */
  1583. static void sis900_finish_xmit (struct net_device *net_dev)
  1584. {
  1585. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1586. for (; sis_priv->dirty_tx != sis_priv->cur_tx; sis_priv->dirty_tx++) {
  1587. struct sk_buff *skb;
  1588. unsigned int entry;
  1589. u32 tx_status;
  1590. entry = sis_priv->dirty_tx % NUM_TX_DESC;
  1591. tx_status = sis_priv->tx_ring[entry].cmdsts;
  1592. if (tx_status & OWN) {
  1593. /* The packet is not transmitted yet (owned by hardware) !
  1594. * Note: the interrupt is generated only when Tx Machine
  1595. * is idle, so this is an almost impossible case */
  1596. break;
  1597. }
  1598. if (tx_status & (ABORT | UNDERRUN | OWCOLL)) {
  1599. /* packet unsuccessfully transmitted */
  1600. if (netif_msg_tx_err(sis_priv))
  1601. printk(KERN_DEBUG "%s: Transmit "
  1602. "error, Tx status %8.8x.\n",
  1603. net_dev->name, tx_status);
  1604. net_dev->stats.tx_errors++;
  1605. if (tx_status & UNDERRUN)
  1606. net_dev->stats.tx_fifo_errors++;
  1607. if (tx_status & ABORT)
  1608. net_dev->stats.tx_aborted_errors++;
  1609. if (tx_status & NOCARRIER)
  1610. net_dev->stats.tx_carrier_errors++;
  1611. if (tx_status & OWCOLL)
  1612. net_dev->stats.tx_window_errors++;
  1613. } else {
  1614. /* packet successfully transmitted */
  1615. net_dev->stats.collisions += (tx_status & COLCNT) >> 16;
  1616. net_dev->stats.tx_bytes += tx_status & DSIZE;
  1617. net_dev->stats.tx_packets++;
  1618. }
  1619. /* Free the original skb. */
  1620. skb = sis_priv->tx_skbuff[entry];
  1621. pci_unmap_single(sis_priv->pci_dev,
  1622. sis_priv->tx_ring[entry].bufptr, skb->len,
  1623. PCI_DMA_TODEVICE);
  1624. dev_kfree_skb_irq(skb);
  1625. sis_priv->tx_skbuff[entry] = NULL;
  1626. sis_priv->tx_ring[entry].bufptr = 0;
  1627. sis_priv->tx_ring[entry].cmdsts = 0;
  1628. }
  1629. if (sis_priv->tx_full && netif_queue_stopped(net_dev) &&
  1630. sis_priv->cur_tx - sis_priv->dirty_tx < NUM_TX_DESC - 4) {
  1631. /* The ring is no longer full, clear tx_full and schedule
  1632. * more transmission by netif_wake_queue(net_dev) */
  1633. sis_priv->tx_full = 0;
  1634. netif_wake_queue (net_dev);
  1635. }
  1636. }
  1637. /**
  1638. * sis900_close - close sis900 device
  1639. * @net_dev: the net device to be closed
  1640. *
  1641. * Disable interrupts, stop the Tx and Rx Status Machine
  1642. * free Tx and RX socket buffer
  1643. */
  1644. static int sis900_close(struct net_device *net_dev)
  1645. {
  1646. long ioaddr = net_dev->base_addr;
  1647. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1648. struct sk_buff *skb;
  1649. int i;
  1650. netif_stop_queue(net_dev);
  1651. /* Disable interrupts by clearing the interrupt mask. */
  1652. outl(0x0000, ioaddr + imr);
  1653. outl(0x0000, ioaddr + ier);
  1654. /* Stop the chip's Tx and Rx Status Machine */
  1655. outl(RxDIS | TxDIS | inl(ioaddr + cr), ioaddr + cr);
  1656. del_timer(&sis_priv->timer);
  1657. free_irq(net_dev->irq, net_dev);
  1658. /* Free Tx and RX skbuff */
  1659. for (i = 0; i < NUM_RX_DESC; i++) {
  1660. skb = sis_priv->rx_skbuff[i];
  1661. if (skb) {
  1662. pci_unmap_single(sis_priv->pci_dev,
  1663. sis_priv->rx_ring[i].bufptr,
  1664. RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  1665. dev_kfree_skb(skb);
  1666. sis_priv->rx_skbuff[i] = NULL;
  1667. }
  1668. }
  1669. for (i = 0; i < NUM_TX_DESC; i++) {
  1670. skb = sis_priv->tx_skbuff[i];
  1671. if (skb) {
  1672. pci_unmap_single(sis_priv->pci_dev,
  1673. sis_priv->tx_ring[i].bufptr, skb->len,
  1674. PCI_DMA_TODEVICE);
  1675. dev_kfree_skb(skb);
  1676. sis_priv->tx_skbuff[i] = NULL;
  1677. }
  1678. }
  1679. /* Green! Put the chip in low-power mode. */
  1680. return 0;
  1681. }
  1682. /**
  1683. * sis900_get_drvinfo - Return information about driver
  1684. * @net_dev: the net device to probe
  1685. * @info: container for info returned
  1686. *
  1687. * Process ethtool command such as "ehtool -i" to show information
  1688. */
  1689. static void sis900_get_drvinfo(struct net_device *net_dev,
  1690. struct ethtool_drvinfo *info)
  1691. {
  1692. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1693. strcpy (info->driver, SIS900_MODULE_NAME);
  1694. strcpy (info->version, SIS900_DRV_VERSION);
  1695. strcpy (info->bus_info, pci_name(sis_priv->pci_dev));
  1696. }
  1697. static u32 sis900_get_msglevel(struct net_device *net_dev)
  1698. {
  1699. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1700. return sis_priv->msg_enable;
  1701. }
  1702. static void sis900_set_msglevel(struct net_device *net_dev, u32 value)
  1703. {
  1704. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1705. sis_priv->msg_enable = value;
  1706. }
  1707. static u32 sis900_get_link(struct net_device *net_dev)
  1708. {
  1709. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1710. return mii_link_ok(&sis_priv->mii_info);
  1711. }
  1712. static int sis900_get_settings(struct net_device *net_dev,
  1713. struct ethtool_cmd *cmd)
  1714. {
  1715. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1716. spin_lock_irq(&sis_priv->lock);
  1717. mii_ethtool_gset(&sis_priv->mii_info, cmd);
  1718. spin_unlock_irq(&sis_priv->lock);
  1719. return 0;
  1720. }
  1721. static int sis900_set_settings(struct net_device *net_dev,
  1722. struct ethtool_cmd *cmd)
  1723. {
  1724. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1725. int rt;
  1726. spin_lock_irq(&sis_priv->lock);
  1727. rt = mii_ethtool_sset(&sis_priv->mii_info, cmd);
  1728. spin_unlock_irq(&sis_priv->lock);
  1729. return rt;
  1730. }
  1731. static int sis900_nway_reset(struct net_device *net_dev)
  1732. {
  1733. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1734. return mii_nway_restart(&sis_priv->mii_info);
  1735. }
  1736. /**
  1737. * sis900_set_wol - Set up Wake on Lan registers
  1738. * @net_dev: the net device to probe
  1739. * @wol: container for info passed to the driver
  1740. *
  1741. * Process ethtool command "wol" to setup wake on lan features.
  1742. * SiS900 supports sending WoL events if a correct packet is received,
  1743. * but there is no simple way to filter them to only a subset (broadcast,
  1744. * multicast, unicast or arp).
  1745. */
  1746. static int sis900_set_wol(struct net_device *net_dev, struct ethtool_wolinfo *wol)
  1747. {
  1748. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1749. long pmctrl_addr = net_dev->base_addr + pmctrl;
  1750. u32 cfgpmcsr = 0, pmctrl_bits = 0;
  1751. if (wol->wolopts == 0) {
  1752. pci_read_config_dword(sis_priv->pci_dev, CFGPMCSR, &cfgpmcsr);
  1753. cfgpmcsr &= ~PME_EN;
  1754. pci_write_config_dword(sis_priv->pci_dev, CFGPMCSR, cfgpmcsr);
  1755. outl(pmctrl_bits, pmctrl_addr);
  1756. if (netif_msg_wol(sis_priv))
  1757. printk(KERN_DEBUG "%s: Wake on LAN disabled\n", net_dev->name);
  1758. return 0;
  1759. }
  1760. if (wol->wolopts & (WAKE_MAGICSECURE | WAKE_UCAST | WAKE_MCAST
  1761. | WAKE_BCAST | WAKE_ARP))
  1762. return -EINVAL;
  1763. if (wol->wolopts & WAKE_MAGIC)
  1764. pmctrl_bits |= MAGICPKT;
  1765. if (wol->wolopts & WAKE_PHY)
  1766. pmctrl_bits |= LINKON;
  1767. outl(pmctrl_bits, pmctrl_addr);
  1768. pci_read_config_dword(sis_priv->pci_dev, CFGPMCSR, &cfgpmcsr);
  1769. cfgpmcsr |= PME_EN;
  1770. pci_write_config_dword(sis_priv->pci_dev, CFGPMCSR, cfgpmcsr);
  1771. if (netif_msg_wol(sis_priv))
  1772. printk(KERN_DEBUG "%s: Wake on LAN enabled\n", net_dev->name);
  1773. return 0;
  1774. }
  1775. static void sis900_get_wol(struct net_device *net_dev, struct ethtool_wolinfo *wol)
  1776. {
  1777. long pmctrl_addr = net_dev->base_addr + pmctrl;
  1778. u32 pmctrl_bits;
  1779. pmctrl_bits = inl(pmctrl_addr);
  1780. if (pmctrl_bits & MAGICPKT)
  1781. wol->wolopts |= WAKE_MAGIC;
  1782. if (pmctrl_bits & LINKON)
  1783. wol->wolopts |= WAKE_PHY;
  1784. wol->supported = (WAKE_PHY | WAKE_MAGIC);
  1785. }
  1786. static const struct ethtool_ops sis900_ethtool_ops = {
  1787. .get_drvinfo = sis900_get_drvinfo,
  1788. .get_msglevel = sis900_get_msglevel,
  1789. .set_msglevel = sis900_set_msglevel,
  1790. .get_link = sis900_get_link,
  1791. .get_settings = sis900_get_settings,
  1792. .set_settings = sis900_set_settings,
  1793. .nway_reset = sis900_nway_reset,
  1794. .get_wol = sis900_get_wol,
  1795. .set_wol = sis900_set_wol
  1796. };
  1797. /**
  1798. * mii_ioctl - process MII i/o control command
  1799. * @net_dev: the net device to command for
  1800. * @rq: parameter for command
  1801. * @cmd: the i/o command
  1802. *
  1803. * Process MII command like read/write MII register
  1804. */
  1805. static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
  1806. {
  1807. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1808. struct mii_ioctl_data *data = if_mii(rq);
  1809. switch(cmd) {
  1810. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  1811. data->phy_id = sis_priv->mii->phy_addr;
  1812. /* Fall Through */
  1813. case SIOCGMIIREG: /* Read MII PHY register. */
  1814. data->val_out = mdio_read(net_dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
  1815. return 0;
  1816. case SIOCSMIIREG: /* Write MII PHY register. */
  1817. mdio_write(net_dev, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
  1818. return 0;
  1819. default:
  1820. return -EOPNOTSUPP;
  1821. }
  1822. }
  1823. /**
  1824. * sis900_set_config - Set media type by net_device.set_config
  1825. * @dev: the net device for media type change
  1826. * @map: ifmap passed by ifconfig
  1827. *
  1828. * Set media type to 10baseT, 100baseT or 0(for auto) by ifconfig
  1829. * we support only port changes. All other runtime configuration
  1830. * changes will be ignored
  1831. */
  1832. static int sis900_set_config(struct net_device *dev, struct ifmap *map)
  1833. {
  1834. struct sis900_private *sis_priv = netdev_priv(dev);
  1835. struct mii_phy *mii_phy = sis_priv->mii;
  1836. u16 status;
  1837. if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
  1838. /* we switch on the ifmap->port field. I couldn't find anything
  1839. * like a definition or standard for the values of that field.
  1840. * I think the meaning of those values is device specific. But
  1841. * since I would like to change the media type via the ifconfig
  1842. * command I use the definition from linux/netdevice.h
  1843. * (which seems to be different from the ifport(pcmcia) definition) */
  1844. switch(map->port){
  1845. case IF_PORT_UNKNOWN: /* use auto here */
  1846. dev->if_port = map->port;
  1847. /* we are going to change the media type, so the Link
  1848. * will be temporary down and we need to reflect that
  1849. * here. When the Link comes up again, it will be
  1850. * sensed by the sis_timer procedure, which also does
  1851. * all the rest for us */
  1852. netif_carrier_off(dev);
  1853. /* read current state */
  1854. status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
  1855. /* enable auto negotiation and reset the negotioation
  1856. * (I don't really know what the auto negatiotiation
  1857. * reset really means, but it sounds for me right to
  1858. * do one here) */
  1859. mdio_write(dev, mii_phy->phy_addr,
  1860. MII_CONTROL, status | MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
  1861. break;
  1862. case IF_PORT_10BASET: /* 10BaseT */
  1863. dev->if_port = map->port;
  1864. /* we are going to change the media type, so the Link
  1865. * will be temporary down and we need to reflect that
  1866. * here. When the Link comes up again, it will be
  1867. * sensed by the sis_timer procedure, which also does
  1868. * all the rest for us */
  1869. netif_carrier_off(dev);
  1870. /* set Speed to 10Mbps */
  1871. /* read current state */
  1872. status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
  1873. /* disable auto negotiation and force 10MBit mode*/
  1874. mdio_write(dev, mii_phy->phy_addr,
  1875. MII_CONTROL, status & ~(MII_CNTL_SPEED |
  1876. MII_CNTL_AUTO));
  1877. break;
  1878. case IF_PORT_100BASET: /* 100BaseT */
  1879. case IF_PORT_100BASETX: /* 100BaseTx */
  1880. dev->if_port = map->port;
  1881. /* we are going to change the media type, so the Link
  1882. * will be temporary down and we need to reflect that
  1883. * here. When the Link comes up again, it will be
  1884. * sensed by the sis_timer procedure, which also does
  1885. * all the rest for us */
  1886. netif_carrier_off(dev);
  1887. /* set Speed to 100Mbps */
  1888. /* disable auto negotiation and enable 100MBit Mode */
  1889. status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
  1890. mdio_write(dev, mii_phy->phy_addr,
  1891. MII_CONTROL, (status & ~MII_CNTL_SPEED) |
  1892. MII_CNTL_SPEED);
  1893. break;
  1894. case IF_PORT_10BASE2: /* 10Base2 */
  1895. case IF_PORT_AUI: /* AUI */
  1896. case IF_PORT_100BASEFX: /* 100BaseFx */
  1897. /* These Modes are not supported (are they?)*/
  1898. return -EOPNOTSUPP;
  1899. break;
  1900. default:
  1901. return -EINVAL;
  1902. }
  1903. }
  1904. return 0;
  1905. }
  1906. /**
  1907. * sis900_mcast_bitnr - compute hashtable index
  1908. * @addr: multicast address
  1909. * @revision: revision id of chip
  1910. *
  1911. * SiS 900 uses the most sigificant 7 bits to index a 128 bits multicast
  1912. * hash table, which makes this function a little bit different from other drivers
  1913. * SiS 900 B0 & 635 M/B uses the most significat 8 bits to index 256 bits
  1914. * multicast hash table.
  1915. */
  1916. static inline u16 sis900_mcast_bitnr(u8 *addr, u8 revision)
  1917. {
  1918. u32 crc = ether_crc(6, addr);
  1919. /* leave 8 or 7 most siginifant bits */
  1920. if ((revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV))
  1921. return (int)(crc >> 24);
  1922. else
  1923. return (int)(crc >> 25);
  1924. }
  1925. /**
  1926. * set_rx_mode - Set SiS900 receive mode
  1927. * @net_dev: the net device to be set
  1928. *
  1929. * Set SiS900 receive mode for promiscuous, multicast, or broadcast mode.
  1930. * And set the appropriate multicast filter.
  1931. * Multicast hash table changes from 128 to 256 bits for 635M/B & 900B0.
  1932. */
  1933. static void set_rx_mode(struct net_device *net_dev)
  1934. {
  1935. long ioaddr = net_dev->base_addr;
  1936. struct sis900_private *sis_priv = netdev_priv(net_dev);
  1937. u16 mc_filter[16] = {0}; /* 256/128 bits multicast hash table */
  1938. int i, table_entries;
  1939. u32 rx_mode;
  1940. /* 635 Hash Table entries = 256(2^16) */
  1941. if((sis_priv->chipset_rev >= SIS635A_900_REV) ||
  1942. (sis_priv->chipset_rev == SIS900B_900_REV))
  1943. table_entries = 16;
  1944. else
  1945. table_entries = 8;
  1946. if (net_dev->flags & IFF_PROMISC) {
  1947. /* Accept any kinds of packets */
  1948. rx_mode = RFPromiscuous;
  1949. for (i = 0; i < table_entries; i++)
  1950. mc_filter[i] = 0xffff;
  1951. } else if ((netdev_mc_count(net_dev) > multicast_filter_limit) ||
  1952. (net_dev->flags & IFF_ALLMULTI)) {
  1953. /* too many multicast addresses or accept all multicast packet */
  1954. rx_mode = RFAAB | RFAAM;
  1955. for (i = 0; i < table_entries; i++)
  1956. mc_filter[i] = 0xffff;
  1957. } else {
  1958. /* Accept Broadcast packet, destination address matchs our
  1959. * MAC address, use Receive Filter to reject unwanted MCAST
  1960. * packets */
  1961. struct netdev_hw_addr *ha;
  1962. rx_mode = RFAAB;
  1963. netdev_for_each_mc_addr(ha, net_dev) {
  1964. unsigned int bit_nr;
  1965. bit_nr = sis900_mcast_bitnr(ha->addr,
  1966. sis_priv->chipset_rev);
  1967. mc_filter[bit_nr >> 4] |= (1 << (bit_nr & 0xf));
  1968. }
  1969. }
  1970. /* update Multicast Hash Table in Receive Filter */
  1971. for (i = 0; i < table_entries; i++) {
  1972. /* why plus 0x04 ??, That makes the correct value for hash table. */
  1973. outl((u32)(0x00000004+i) << RFADDR_shift, ioaddr + rfcr);
  1974. outl(mc_filter[i], ioaddr + rfdr);
  1975. }
  1976. outl(RFEN | rx_mode, ioaddr + rfcr);
  1977. /* sis900 is capable of looping back packets at MAC level for
  1978. * debugging purpose */
  1979. if (net_dev->flags & IFF_LOOPBACK) {
  1980. u32 cr_saved;
  1981. /* We must disable Tx/Rx before setting loopback mode */
  1982. cr_saved = inl(ioaddr + cr);
  1983. outl(cr_saved | TxDIS | RxDIS, ioaddr + cr);
  1984. /* enable loopback */
  1985. outl(inl(ioaddr + txcfg) | TxMLB, ioaddr + txcfg);
  1986. outl(inl(ioaddr + rxcfg) | RxATX, ioaddr + rxcfg);
  1987. /* restore cr */
  1988. outl(cr_saved, ioaddr + cr);
  1989. }
  1990. }
  1991. /**
  1992. * sis900_reset - Reset sis900 MAC
  1993. * @net_dev: the net device to reset
  1994. *
  1995. * reset sis900 MAC and wait until finished
  1996. * reset through command register
  1997. * change backoff algorithm for 900B0 & 635 M/B
  1998. */
  1999. static void sis900_reset(struct net_device *net_dev)
  2000. {
  2001. struct sis900_private *sis_priv = netdev_priv(net_dev);
  2002. long ioaddr = net_dev->base_addr;
  2003. int i = 0;
  2004. u32 status = TxRCMP | RxRCMP;
  2005. outl(0, ioaddr + ier);
  2006. outl(0, ioaddr + imr);
  2007. outl(0, ioaddr + rfcr);
  2008. outl(RxRESET | TxRESET | RESET | inl(ioaddr + cr), ioaddr + cr);
  2009. /* Check that the chip has finished the reset. */
  2010. while (status && (i++ < 1000)) {
  2011. status ^= (inl(isr + ioaddr) & status);
  2012. }
  2013. if( (sis_priv->chipset_rev >= SIS635A_900_REV) ||
  2014. (sis_priv->chipset_rev == SIS900B_900_REV) )
  2015. outl(PESEL | RND_CNT, ioaddr + cfg);
  2016. else
  2017. outl(PESEL, ioaddr + cfg);
  2018. }
  2019. /**
  2020. * sis900_remove - Remove sis900 device
  2021. * @pci_dev: the pci device to be removed
  2022. *
  2023. * remove and release SiS900 net device
  2024. */
  2025. static void __devexit sis900_remove(struct pci_dev *pci_dev)
  2026. {
  2027. struct net_device *net_dev = pci_get_drvdata(pci_dev);
  2028. struct sis900_private *sis_priv = netdev_priv(net_dev);
  2029. struct mii_phy *phy = NULL;
  2030. while (sis_priv->first_mii) {
  2031. phy = sis_priv->first_mii;
  2032. sis_priv->first_mii = phy->next;
  2033. kfree(phy);
  2034. }
  2035. pci_free_consistent(pci_dev, RX_TOTAL_SIZE, sis_priv->rx_ring,
  2036. sis_priv->rx_ring_dma);
  2037. pci_free_consistent(pci_dev, TX_TOTAL_SIZE, sis_priv->tx_ring,
  2038. sis_priv->tx_ring_dma);
  2039. unregister_netdev(net_dev);
  2040. free_netdev(net_dev);
  2041. pci_release_regions(pci_dev);
  2042. pci_set_drvdata(pci_dev, NULL);
  2043. }
  2044. #ifdef CONFIG_PM
  2045. static int sis900_suspend(struct pci_dev *pci_dev, pm_message_t state)
  2046. {
  2047. struct net_device *net_dev = pci_get_drvdata(pci_dev);
  2048. long ioaddr = net_dev->base_addr;
  2049. if(!netif_running(net_dev))
  2050. return 0;
  2051. netif_stop_queue(net_dev);
  2052. netif_device_detach(net_dev);
  2053. /* Stop the chip's Tx and Rx Status Machine */
  2054. outl(RxDIS | TxDIS | inl(ioaddr + cr), ioaddr + cr);
  2055. pci_set_power_state(pci_dev, PCI_D3hot);
  2056. pci_save_state(pci_dev);
  2057. return 0;
  2058. }
  2059. static int sis900_resume(struct pci_dev *pci_dev)
  2060. {
  2061. struct net_device *net_dev = pci_get_drvdata(pci_dev);
  2062. struct sis900_private *sis_priv = netdev_priv(net_dev);
  2063. long ioaddr = net_dev->base_addr;
  2064. if(!netif_running(net_dev))
  2065. return 0;
  2066. pci_restore_state(pci_dev);
  2067. pci_set_power_state(pci_dev, PCI_D0);
  2068. sis900_init_rxfilter(net_dev);
  2069. sis900_init_tx_ring(net_dev);
  2070. sis900_init_rx_ring(net_dev);
  2071. set_rx_mode(net_dev);
  2072. netif_device_attach(net_dev);
  2073. netif_start_queue(net_dev);
  2074. /* Workaround for EDB */
  2075. sis900_set_mode(ioaddr, HW_SPEED_10_MBPS, FDX_CAPABLE_HALF_SELECTED);
  2076. /* Enable all known interrupts by setting the interrupt mask. */
  2077. outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
  2078. outl(RxENA | inl(ioaddr + cr), ioaddr + cr);
  2079. outl(IE, ioaddr + ier);
  2080. sis900_check_mode(net_dev, sis_priv->mii);
  2081. return 0;
  2082. }
  2083. #endif /* CONFIG_PM */
  2084. static struct pci_driver sis900_pci_driver = {
  2085. .name = SIS900_MODULE_NAME,
  2086. .id_table = sis900_pci_tbl,
  2087. .probe = sis900_probe,
  2088. .remove = __devexit_p(sis900_remove),
  2089. #ifdef CONFIG_PM
  2090. .suspend = sis900_suspend,
  2091. .resume = sis900_resume,
  2092. #endif /* CONFIG_PM */
  2093. };
  2094. static int __init sis900_init_module(void)
  2095. {
  2096. /* when a module, this is printed whether or not devices are found in probe */
  2097. #ifdef MODULE
  2098. printk(version);
  2099. #endif
  2100. return pci_register_driver(&sis900_pci_driver);
  2101. }
  2102. static void __exit sis900_cleanup_module(void)
  2103. {
  2104. pci_unregister_driver(&sis900_pci_driver);
  2105. }
  2106. module_init(sis900_init_module);
  2107. module_exit(sis900_cleanup_module);