sis900.c 72 KB

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