sis900.c 72 KB

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