sis900.c 74 KB

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