sis900.c 70 KB

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