sis900.c 72 KB

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