r8169.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021
  1. /*
  2. =========================================================================
  3. r8169.c: A RealTek RTL-8169 Gigabit Ethernet driver for Linux kernel 2.4.x.
  4. --------------------------------------------------------------------
  5. History:
  6. Feb 4 2002 - created initially by ShuChen <shuchen@realtek.com.tw>.
  7. May 20 2002 - Add link status force-mode and TBI mode support.
  8. 2004 - Massive updates. See kernel SCM system for details.
  9. =========================================================================
  10. 1. [DEPRECATED: use ethtool instead] The media can be forced in 5 modes.
  11. Command: 'insmod r8169 media = SET_MEDIA'
  12. Ex: 'insmod r8169 media = 0x04' will force PHY to operate in 100Mpbs Half-duplex.
  13. SET_MEDIA can be:
  14. _10_Half = 0x01
  15. _10_Full = 0x02
  16. _100_Half = 0x04
  17. _100_Full = 0x08
  18. _1000_Full = 0x10
  19. 2. Support TBI mode.
  20. =========================================================================
  21. VERSION 1.1 <2002/10/4>
  22. The bit4:0 of MII register 4 is called "selector field", and have to be
  23. 00001b to indicate support of IEEE std 802.3 during NWay process of
  24. exchanging Link Code Word (FLP).
  25. VERSION 1.2 <2002/11/30>
  26. - Large style cleanup
  27. - Use ether_crc in stock kernel (linux/crc32.h)
  28. - Copy mc_filter setup code from 8139cp
  29. (includes an optimization, and avoids set_bit use)
  30. VERSION 1.6LK <2004/04/14>
  31. - Merge of Realtek's version 1.6
  32. - Conversion to DMA API
  33. - Suspend/resume
  34. - Endianness
  35. - Misc Rx/Tx bugs
  36. VERSION 2.2LK <2005/01/25>
  37. - RX csum, TX csum/SG, TSO
  38. - VLAN
  39. - baby (< 7200) Jumbo frames support
  40. - Merge of Realtek's version 2.2 (new phy)
  41. */
  42. #include <linux/module.h>
  43. #include <linux/moduleparam.h>
  44. #include <linux/pci.h>
  45. #include <linux/netdevice.h>
  46. #include <linux/etherdevice.h>
  47. #include <linux/delay.h>
  48. #include <linux/ethtool.h>
  49. #include <linux/mii.h>
  50. #include <linux/if_vlan.h>
  51. #include <linux/crc32.h>
  52. #include <linux/in.h>
  53. #include <linux/ip.h>
  54. #include <linux/tcp.h>
  55. #include <linux/init.h>
  56. #include <linux/dma-mapping.h>
  57. #include <asm/system.h>
  58. #include <asm/io.h>
  59. #include <asm/irq.h>
  60. #ifdef CONFIG_R8169_NAPI
  61. #define NAPI_SUFFIX "-NAPI"
  62. #else
  63. #define NAPI_SUFFIX ""
  64. #endif
  65. #define RTL8169_VERSION "2.2LK" NAPI_SUFFIX
  66. #define MODULENAME "r8169"
  67. #define PFX MODULENAME ": "
  68. #ifdef RTL8169_DEBUG
  69. #define assert(expr) \
  70. if (!(expr)) { \
  71. printk( "Assertion failed! %s,%s,%s,line=%d\n", \
  72. #expr,__FILE__,__FUNCTION__,__LINE__); \
  73. }
  74. #define dprintk(fmt, args...) do { printk(PFX fmt, ## args); } while (0)
  75. #else
  76. #define assert(expr) do {} while (0)
  77. #define dprintk(fmt, args...) do {} while (0)
  78. #endif /* RTL8169_DEBUG */
  79. #define R8169_MSG_DEFAULT \
  80. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_IFUP | NETIF_MSG_IFDOWN)
  81. #define TX_BUFFS_AVAIL(tp) \
  82. (tp->dirty_tx + NUM_TX_DESC - tp->cur_tx - 1)
  83. #ifdef CONFIG_R8169_NAPI
  84. #define rtl8169_rx_skb netif_receive_skb
  85. #define rtl8169_rx_hwaccel_skb vlan_hwaccel_receive_skb
  86. #define rtl8169_rx_quota(count, quota) min(count, quota)
  87. #else
  88. #define rtl8169_rx_skb netif_rx
  89. #define rtl8169_rx_hwaccel_skb vlan_hwaccel_rx
  90. #define rtl8169_rx_quota(count, quota) count
  91. #endif
  92. /* media options */
  93. #define MAX_UNITS 8
  94. static int media[MAX_UNITS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
  95. static int num_media = 0;
  96. /* Maximum events (Rx packets, etc.) to handle at each interrupt. */
  97. static const int max_interrupt_work = 20;
  98. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  99. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  100. static const int multicast_filter_limit = 32;
  101. /* MAC address length */
  102. #define MAC_ADDR_LEN 6
  103. #define RX_FIFO_THRESH 7 /* 7 means NO threshold, Rx buffer level before first PCI xfer. */
  104. #define RX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  105. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  106. #define EarlyTxThld 0x3F /* 0x3F means NO early transmit */
  107. #define RxPacketMaxSize 0x3FE8 /* 16K - 1 - ETH_HLEN - VLAN - CRC... */
  108. #define SafeMtu 0x1c20 /* ... actually life sucks beyond ~7k */
  109. #define InterFrameGap 0x03 /* 3 means InterFrameGap = the shortest one */
  110. #define R8169_REGS_SIZE 256
  111. #define R8169_NAPI_WEIGHT 64
  112. #define NUM_TX_DESC 64 /* Number of Tx descriptor registers */
  113. #define NUM_RX_DESC 256 /* Number of Rx descriptor registers */
  114. #define RX_BUF_SIZE 1536 /* Rx Buffer size */
  115. #define R8169_TX_RING_BYTES (NUM_TX_DESC * sizeof(struct TxDesc))
  116. #define R8169_RX_RING_BYTES (NUM_RX_DESC * sizeof(struct RxDesc))
  117. #define RTL8169_TX_TIMEOUT (6*HZ)
  118. #define RTL8169_PHY_TIMEOUT (10*HZ)
  119. /* write/read MMIO register */
  120. #define RTL_W8(reg, val8) writeb ((val8), ioaddr + (reg))
  121. #define RTL_W16(reg, val16) writew ((val16), ioaddr + (reg))
  122. #define RTL_W32(reg, val32) writel ((val32), ioaddr + (reg))
  123. #define RTL_R8(reg) readb (ioaddr + (reg))
  124. #define RTL_R16(reg) readw (ioaddr + (reg))
  125. #define RTL_R32(reg) ((unsigned long) readl (ioaddr + (reg)))
  126. enum mac_version {
  127. RTL_GIGA_MAC_VER_01 = 0x00,
  128. RTL_GIGA_MAC_VER_02 = 0x01,
  129. RTL_GIGA_MAC_VER_03 = 0x02,
  130. RTL_GIGA_MAC_VER_04 = 0x03,
  131. RTL_GIGA_MAC_VER_05 = 0x04,
  132. RTL_GIGA_MAC_VER_11 = 0x0b, // 8168Bb
  133. RTL_GIGA_MAC_VER_12 = 0x0c, // 8168Be 8168Bf
  134. RTL_GIGA_MAC_VER_13 = 0x0d,
  135. RTL_GIGA_MAC_VER_14 = 0x0e,
  136. RTL_GIGA_MAC_VER_15 = 0x0f
  137. };
  138. enum phy_version {
  139. RTL_GIGA_PHY_VER_C = 0x03, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  140. RTL_GIGA_PHY_VER_D = 0x04, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  141. RTL_GIGA_PHY_VER_E = 0x05, /* PHY Reg 0x03 bit0-3 == 0x0000 */
  142. RTL_GIGA_PHY_VER_F = 0x06, /* PHY Reg 0x03 bit0-3 == 0x0001 */
  143. RTL_GIGA_PHY_VER_G = 0x07, /* PHY Reg 0x03 bit0-3 == 0x0002 */
  144. RTL_GIGA_PHY_VER_H = 0x08, /* PHY Reg 0x03 bit0-3 == 0x0003 */
  145. };
  146. #define _R(NAME,MAC,MASK) \
  147. { .name = NAME, .mac_version = MAC, .RxConfigMask = MASK }
  148. static const struct {
  149. const char *name;
  150. u8 mac_version;
  151. u32 RxConfigMask; /* Clears the bits supported by this chip */
  152. } rtl_chip_info[] = {
  153. _R("RTL8169", RTL_GIGA_MAC_VER_01, 0xff7e1880),
  154. _R("RTL8169s/8110s", RTL_GIGA_MAC_VER_02, 0xff7e1880),
  155. _R("RTL8169s/8110s", RTL_GIGA_MAC_VER_03, 0xff7e1880),
  156. _R("RTL8169sb/8110sb", RTL_GIGA_MAC_VER_04, 0xff7e1880),
  157. _R("RTL8169sc/8110sc", RTL_GIGA_MAC_VER_05, 0xff7e1880),
  158. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_11, 0xff7e1880), // PCI-E
  159. _R("RTL8168b/8111b", RTL_GIGA_MAC_VER_12, 0xff7e1880), // PCI-E
  160. _R("RTL8101e", RTL_GIGA_MAC_VER_13, 0xff7e1880), // PCI-E 8139
  161. _R("RTL8100e", RTL_GIGA_MAC_VER_14, 0xff7e1880), // PCI-E 8139
  162. _R("RTL8100e", RTL_GIGA_MAC_VER_15, 0xff7e1880) // PCI-E 8139
  163. };
  164. #undef _R
  165. enum cfg_version {
  166. RTL_CFG_0 = 0x00,
  167. RTL_CFG_1,
  168. RTL_CFG_2
  169. };
  170. static void rtl_hw_start_8169(struct net_device *);
  171. static void rtl_hw_start_8168(struct net_device *);
  172. static void rtl_hw_start_8101(struct net_device *);
  173. static const struct {
  174. void (*hw_start)(struct net_device *);
  175. unsigned int region;
  176. unsigned int align;
  177. } rtl_cfg_info[] = {
  178. [RTL_CFG_0] = { rtl_hw_start_8169, 1, NET_IP_ALIGN },
  179. [RTL_CFG_1] = { rtl_hw_start_8168, 2, 8 },
  180. [RTL_CFG_2] = { rtl_hw_start_8101, 2, 8 }
  181. };
  182. static struct pci_device_id rtl8169_pci_tbl[] = {
  183. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8129), 0, 0, RTL_CFG_0 },
  184. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8136), 0, 0, RTL_CFG_2 },
  185. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8167), 0, 0, RTL_CFG_0 },
  186. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8168), 0, 0, RTL_CFG_1 },
  187. { PCI_DEVICE(PCI_VENDOR_ID_REALTEK, 0x8169), 0, 0, RTL_CFG_0 },
  188. { PCI_DEVICE(PCI_VENDOR_ID_DLINK, 0x4300), 0, 0, RTL_CFG_0 },
  189. { PCI_DEVICE(0x1259, 0xc107), 0, 0, RTL_CFG_0 },
  190. { PCI_DEVICE(0x16ec, 0x0116), 0, 0, RTL_CFG_0 },
  191. { PCI_VENDOR_ID_LINKSYS, 0x1032,
  192. PCI_ANY_ID, 0x0024, 0, 0, RTL_CFG_0 },
  193. {0,},
  194. };
  195. MODULE_DEVICE_TABLE(pci, rtl8169_pci_tbl);
  196. static int rx_copybreak = 200;
  197. static int use_dac;
  198. static struct {
  199. u32 msg_enable;
  200. } debug = { -1 };
  201. enum RTL8169_registers {
  202. MAC0 = 0, /* Ethernet hardware address. */
  203. MAR0 = 8, /* Multicast filter. */
  204. CounterAddrLow = 0x10,
  205. CounterAddrHigh = 0x14,
  206. TxDescStartAddrLow = 0x20,
  207. TxDescStartAddrHigh = 0x24,
  208. TxHDescStartAddrLow = 0x28,
  209. TxHDescStartAddrHigh = 0x2c,
  210. FLASH = 0x30,
  211. ERSR = 0x36,
  212. ChipCmd = 0x37,
  213. TxPoll = 0x38,
  214. IntrMask = 0x3C,
  215. IntrStatus = 0x3E,
  216. TxConfig = 0x40,
  217. RxConfig = 0x44,
  218. RxMissed = 0x4C,
  219. Cfg9346 = 0x50,
  220. Config0 = 0x51,
  221. Config1 = 0x52,
  222. Config2 = 0x53,
  223. Config3 = 0x54,
  224. Config4 = 0x55,
  225. Config5 = 0x56,
  226. MultiIntr = 0x5C,
  227. PHYAR = 0x60,
  228. TBICSR = 0x64,
  229. TBI_ANAR = 0x68,
  230. TBI_LPAR = 0x6A,
  231. PHYstatus = 0x6C,
  232. RxMaxSize = 0xDA,
  233. CPlusCmd = 0xE0,
  234. IntrMitigate = 0xE2,
  235. RxDescAddrLow = 0xE4,
  236. RxDescAddrHigh = 0xE8,
  237. EarlyTxThres = 0xEC,
  238. FuncEvent = 0xF0,
  239. FuncEventMask = 0xF4,
  240. FuncPresetState = 0xF8,
  241. FuncForceEvent = 0xFC,
  242. };
  243. enum RTL8169_register_content {
  244. /* InterruptStatusBits */
  245. SYSErr = 0x8000,
  246. PCSTimeout = 0x4000,
  247. SWInt = 0x0100,
  248. TxDescUnavail = 0x80,
  249. RxFIFOOver = 0x40,
  250. LinkChg = 0x20,
  251. RxOverflow = 0x10,
  252. TxErr = 0x08,
  253. TxOK = 0x04,
  254. RxErr = 0x02,
  255. RxOK = 0x01,
  256. /* RxStatusDesc */
  257. RxFOVF = (1 << 23),
  258. RxRWT = (1 << 22),
  259. RxRES = (1 << 21),
  260. RxRUNT = (1 << 20),
  261. RxCRC = (1 << 19),
  262. /* ChipCmdBits */
  263. CmdReset = 0x10,
  264. CmdRxEnb = 0x08,
  265. CmdTxEnb = 0x04,
  266. RxBufEmpty = 0x01,
  267. /* Cfg9346Bits */
  268. Cfg9346_Lock = 0x00,
  269. Cfg9346_Unlock = 0xC0,
  270. /* rx_mode_bits */
  271. AcceptErr = 0x20,
  272. AcceptRunt = 0x10,
  273. AcceptBroadcast = 0x08,
  274. AcceptMulticast = 0x04,
  275. AcceptMyPhys = 0x02,
  276. AcceptAllPhys = 0x01,
  277. /* RxConfigBits */
  278. RxCfgFIFOShift = 13,
  279. RxCfgDMAShift = 8,
  280. /* TxConfigBits */
  281. TxInterFrameGapShift = 24,
  282. TxDMAShift = 8, /* DMA burst value (0-7) is shift this many bits */
  283. /* Config1 register p.24 */
  284. PMEnable = (1 << 0), /* Power Management Enable */
  285. /* Config3 register p.25 */
  286. MagicPacket = (1 << 5), /* Wake up when receives a Magic Packet */
  287. LinkUp = (1 << 4), /* Wake up when the cable connection is re-established */
  288. /* Config5 register p.27 */
  289. BWF = (1 << 6), /* Accept Broadcast wakeup frame */
  290. MWF = (1 << 5), /* Accept Multicast wakeup frame */
  291. UWF = (1 << 4), /* Accept Unicast wakeup frame */
  292. LanWake = (1 << 1), /* LanWake enable/disable */
  293. PMEStatus = (1 << 0), /* PME status can be reset by PCI RST# */
  294. /* TBICSR p.28 */
  295. TBIReset = 0x80000000,
  296. TBILoopback = 0x40000000,
  297. TBINwEnable = 0x20000000,
  298. TBINwRestart = 0x10000000,
  299. TBILinkOk = 0x02000000,
  300. TBINwComplete = 0x01000000,
  301. /* CPlusCmd p.31 */
  302. RxVlan = (1 << 6),
  303. RxChkSum = (1 << 5),
  304. PCIDAC = (1 << 4),
  305. PCIMulRW = (1 << 3),
  306. /* rtl8169_PHYstatus */
  307. TBI_Enable = 0x80,
  308. TxFlowCtrl = 0x40,
  309. RxFlowCtrl = 0x20,
  310. _1000bpsF = 0x10,
  311. _100bps = 0x08,
  312. _10bps = 0x04,
  313. LinkStatus = 0x02,
  314. FullDup = 0x01,
  315. /* _MediaType */
  316. _10_Half = 0x01,
  317. _10_Full = 0x02,
  318. _100_Half = 0x04,
  319. _100_Full = 0x08,
  320. _1000_Full = 0x10,
  321. /* _TBICSRBit */
  322. TBILinkOK = 0x02000000,
  323. /* DumpCounterCommand */
  324. CounterDump = 0x8,
  325. };
  326. enum _DescStatusBit {
  327. DescOwn = (1 << 31), /* Descriptor is owned by NIC */
  328. RingEnd = (1 << 30), /* End of descriptor ring */
  329. FirstFrag = (1 << 29), /* First segment of a packet */
  330. LastFrag = (1 << 28), /* Final segment of a packet */
  331. /* Tx private */
  332. LargeSend = (1 << 27), /* TCP Large Send Offload (TSO) */
  333. MSSShift = 16, /* MSS value position */
  334. MSSMask = 0xfff, /* MSS value + LargeSend bit: 12 bits */
  335. IPCS = (1 << 18), /* Calculate IP checksum */
  336. UDPCS = (1 << 17), /* Calculate UDP/IP checksum */
  337. TCPCS = (1 << 16), /* Calculate TCP/IP checksum */
  338. TxVlanTag = (1 << 17), /* Add VLAN tag */
  339. /* Rx private */
  340. PID1 = (1 << 18), /* Protocol ID bit 1/2 */
  341. PID0 = (1 << 17), /* Protocol ID bit 2/2 */
  342. #define RxProtoUDP (PID1)
  343. #define RxProtoTCP (PID0)
  344. #define RxProtoIP (PID1 | PID0)
  345. #define RxProtoMask RxProtoIP
  346. IPFail = (1 << 16), /* IP checksum failed */
  347. UDPFail = (1 << 15), /* UDP/IP checksum failed */
  348. TCPFail = (1 << 14), /* TCP/IP checksum failed */
  349. RxVlanTag = (1 << 16), /* VLAN tag available */
  350. };
  351. #define RsvdMask 0x3fffc000
  352. struct TxDesc {
  353. u32 opts1;
  354. u32 opts2;
  355. u64 addr;
  356. };
  357. struct RxDesc {
  358. u32 opts1;
  359. u32 opts2;
  360. u64 addr;
  361. };
  362. struct ring_info {
  363. struct sk_buff *skb;
  364. u32 len;
  365. u8 __pad[sizeof(void *) - sizeof(u32)];
  366. };
  367. struct rtl8169_private {
  368. void __iomem *mmio_addr; /* memory map physical address */
  369. struct pci_dev *pci_dev; /* Index of PCI device */
  370. struct net_device *dev;
  371. struct net_device_stats stats; /* statistics of net device */
  372. spinlock_t lock; /* spin lock flag */
  373. u32 msg_enable;
  374. int chipset;
  375. int mac_version;
  376. int phy_version;
  377. u32 cur_rx; /* Index into the Rx descriptor buffer of next Rx pkt. */
  378. u32 cur_tx; /* Index into the Tx descriptor buffer of next Rx pkt. */
  379. u32 dirty_rx;
  380. u32 dirty_tx;
  381. struct TxDesc *TxDescArray; /* 256-aligned Tx descriptor ring */
  382. struct RxDesc *RxDescArray; /* 256-aligned Rx descriptor ring */
  383. dma_addr_t TxPhyAddr;
  384. dma_addr_t RxPhyAddr;
  385. struct sk_buff *Rx_skbuff[NUM_RX_DESC]; /* Rx data buffers */
  386. struct ring_info tx_skb[NUM_TX_DESC]; /* Tx data buffers */
  387. unsigned align;
  388. unsigned rx_buf_sz;
  389. struct timer_list timer;
  390. u16 cp_cmd;
  391. u16 intr_mask;
  392. int phy_auto_nego_reg;
  393. int phy_1000_ctrl_reg;
  394. #ifdef CONFIG_R8169_VLAN
  395. struct vlan_group *vlgrp;
  396. #endif
  397. int (*set_speed)(struct net_device *, u8 autoneg, u16 speed, u8 duplex);
  398. void (*get_settings)(struct net_device *, struct ethtool_cmd *);
  399. void (*phy_reset_enable)(void __iomem *);
  400. void (*hw_start)(struct net_device *);
  401. unsigned int (*phy_reset_pending)(void __iomem *);
  402. unsigned int (*link_ok)(void __iomem *);
  403. struct delayed_work task;
  404. unsigned wol_enabled : 1;
  405. };
  406. MODULE_AUTHOR("Realtek and the Linux r8169 crew <netdev@vger.kernel.org>");
  407. MODULE_DESCRIPTION("RealTek RTL-8169 Gigabit Ethernet driver");
  408. module_param_array(media, int, &num_media, 0);
  409. MODULE_PARM_DESC(media, "force phy operation. Deprecated by ethtool (8).");
  410. module_param(rx_copybreak, int, 0);
  411. MODULE_PARM_DESC(rx_copybreak, "Copy breakpoint for copy-only-tiny-frames");
  412. module_param(use_dac, int, 0);
  413. MODULE_PARM_DESC(use_dac, "Enable PCI DAC. Unsafe on 32 bit PCI slot.");
  414. module_param_named(debug, debug.msg_enable, int, 0);
  415. MODULE_PARM_DESC(debug, "Debug verbosity level (0=none, ..., 16=all)");
  416. MODULE_LICENSE("GPL");
  417. MODULE_VERSION(RTL8169_VERSION);
  418. static int rtl8169_open(struct net_device *dev);
  419. static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev);
  420. static irqreturn_t rtl8169_interrupt(int irq, void *dev_instance);
  421. static int rtl8169_init_ring(struct net_device *dev);
  422. static void rtl_hw_start(struct net_device *dev);
  423. static int rtl8169_close(struct net_device *dev);
  424. static void rtl_set_rx_mode(struct net_device *dev);
  425. static void rtl8169_tx_timeout(struct net_device *dev);
  426. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev);
  427. static int rtl8169_rx_interrupt(struct net_device *, struct rtl8169_private *,
  428. void __iomem *);
  429. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu);
  430. static void rtl8169_down(struct net_device *dev);
  431. static void rtl8169_rx_clear(struct rtl8169_private *tp);
  432. #ifdef CONFIG_R8169_NAPI
  433. static int rtl8169_poll(struct net_device *dev, int *budget);
  434. #endif
  435. static const u16 rtl8169_intr_mask =
  436. SYSErr | LinkChg | RxOverflow | RxFIFOOver | TxErr | TxOK | RxErr | RxOK;
  437. static const u16 rtl8169_napi_event =
  438. RxOK | RxOverflow | RxFIFOOver | TxOK | TxErr;
  439. static const unsigned int rtl8169_rx_config =
  440. (RX_FIFO_THRESH << RxCfgFIFOShift) | (RX_DMA_BURST << RxCfgDMAShift);
  441. static void mdio_write(void __iomem *ioaddr, int RegAddr, int value)
  442. {
  443. int i;
  444. RTL_W32(PHYAR, 0x80000000 | (RegAddr & 0xFF) << 16 | value);
  445. for (i = 20; i > 0; i--) {
  446. /* Check if the RTL8169 has completed writing to the specified MII register */
  447. if (!(RTL_R32(PHYAR) & 0x80000000))
  448. break;
  449. udelay(25);
  450. }
  451. }
  452. static int mdio_read(void __iomem *ioaddr, int RegAddr)
  453. {
  454. int i, value = -1;
  455. RTL_W32(PHYAR, 0x0 | (RegAddr & 0xFF) << 16);
  456. for (i = 20; i > 0; i--) {
  457. /* Check if the RTL8169 has completed retrieving data from the specified MII register */
  458. if (RTL_R32(PHYAR) & 0x80000000) {
  459. value = (int) (RTL_R32(PHYAR) & 0xFFFF);
  460. break;
  461. }
  462. udelay(25);
  463. }
  464. return value;
  465. }
  466. static void rtl8169_irq_mask_and_ack(void __iomem *ioaddr)
  467. {
  468. RTL_W16(IntrMask, 0x0000);
  469. RTL_W16(IntrStatus, 0xffff);
  470. }
  471. static void rtl8169_asic_down(void __iomem *ioaddr)
  472. {
  473. RTL_W8(ChipCmd, 0x00);
  474. rtl8169_irq_mask_and_ack(ioaddr);
  475. RTL_R16(CPlusCmd);
  476. }
  477. static unsigned int rtl8169_tbi_reset_pending(void __iomem *ioaddr)
  478. {
  479. return RTL_R32(TBICSR) & TBIReset;
  480. }
  481. static unsigned int rtl8169_xmii_reset_pending(void __iomem *ioaddr)
  482. {
  483. return mdio_read(ioaddr, MII_BMCR) & BMCR_RESET;
  484. }
  485. static unsigned int rtl8169_tbi_link_ok(void __iomem *ioaddr)
  486. {
  487. return RTL_R32(TBICSR) & TBILinkOk;
  488. }
  489. static unsigned int rtl8169_xmii_link_ok(void __iomem *ioaddr)
  490. {
  491. return RTL_R8(PHYstatus) & LinkStatus;
  492. }
  493. static void rtl8169_tbi_reset_enable(void __iomem *ioaddr)
  494. {
  495. RTL_W32(TBICSR, RTL_R32(TBICSR) | TBIReset);
  496. }
  497. static void rtl8169_xmii_reset_enable(void __iomem *ioaddr)
  498. {
  499. unsigned int val;
  500. val = mdio_read(ioaddr, MII_BMCR) | BMCR_RESET;
  501. mdio_write(ioaddr, MII_BMCR, val & 0xffff);
  502. }
  503. static void rtl8169_check_link_status(struct net_device *dev,
  504. struct rtl8169_private *tp, void __iomem *ioaddr)
  505. {
  506. unsigned long flags;
  507. spin_lock_irqsave(&tp->lock, flags);
  508. if (tp->link_ok(ioaddr)) {
  509. netif_carrier_on(dev);
  510. if (netif_msg_ifup(tp))
  511. printk(KERN_INFO PFX "%s: link up\n", dev->name);
  512. } else {
  513. if (netif_msg_ifdown(tp))
  514. printk(KERN_INFO PFX "%s: link down\n", dev->name);
  515. netif_carrier_off(dev);
  516. }
  517. spin_unlock_irqrestore(&tp->lock, flags);
  518. }
  519. static void rtl8169_link_option(int idx, u8 *autoneg, u16 *speed, u8 *duplex)
  520. {
  521. struct {
  522. u16 speed;
  523. u8 duplex;
  524. u8 autoneg;
  525. u8 media;
  526. } link_settings[] = {
  527. { SPEED_10, DUPLEX_HALF, AUTONEG_DISABLE, _10_Half },
  528. { SPEED_10, DUPLEX_FULL, AUTONEG_DISABLE, _10_Full },
  529. { SPEED_100, DUPLEX_HALF, AUTONEG_DISABLE, _100_Half },
  530. { SPEED_100, DUPLEX_FULL, AUTONEG_DISABLE, _100_Full },
  531. { SPEED_1000, DUPLEX_FULL, AUTONEG_DISABLE, _1000_Full },
  532. /* Make TBI happy */
  533. { SPEED_1000, DUPLEX_FULL, AUTONEG_ENABLE, 0xff }
  534. }, *p;
  535. unsigned char option;
  536. option = ((idx < MAX_UNITS) && (idx >= 0)) ? media[idx] : 0xff;
  537. if ((option != 0xff) && !idx && netif_msg_drv(&debug))
  538. printk(KERN_WARNING PFX "media option is deprecated.\n");
  539. for (p = link_settings; p->media != 0xff; p++) {
  540. if (p->media == option)
  541. break;
  542. }
  543. *autoneg = p->autoneg;
  544. *speed = p->speed;
  545. *duplex = p->duplex;
  546. }
  547. static void rtl8169_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  548. {
  549. struct rtl8169_private *tp = netdev_priv(dev);
  550. void __iomem *ioaddr = tp->mmio_addr;
  551. u8 options;
  552. wol->wolopts = 0;
  553. #define WAKE_ANY (WAKE_PHY | WAKE_MAGIC | WAKE_UCAST | WAKE_BCAST | WAKE_MCAST)
  554. wol->supported = WAKE_ANY;
  555. spin_lock_irq(&tp->lock);
  556. options = RTL_R8(Config1);
  557. if (!(options & PMEnable))
  558. goto out_unlock;
  559. options = RTL_R8(Config3);
  560. if (options & LinkUp)
  561. wol->wolopts |= WAKE_PHY;
  562. if (options & MagicPacket)
  563. wol->wolopts |= WAKE_MAGIC;
  564. options = RTL_R8(Config5);
  565. if (options & UWF)
  566. wol->wolopts |= WAKE_UCAST;
  567. if (options & BWF)
  568. wol->wolopts |= WAKE_BCAST;
  569. if (options & MWF)
  570. wol->wolopts |= WAKE_MCAST;
  571. out_unlock:
  572. spin_unlock_irq(&tp->lock);
  573. }
  574. static int rtl8169_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  575. {
  576. struct rtl8169_private *tp = netdev_priv(dev);
  577. void __iomem *ioaddr = tp->mmio_addr;
  578. int i;
  579. static struct {
  580. u32 opt;
  581. u16 reg;
  582. u8 mask;
  583. } cfg[] = {
  584. { WAKE_ANY, Config1, PMEnable },
  585. { WAKE_PHY, Config3, LinkUp },
  586. { WAKE_MAGIC, Config3, MagicPacket },
  587. { WAKE_UCAST, Config5, UWF },
  588. { WAKE_BCAST, Config5, BWF },
  589. { WAKE_MCAST, Config5, MWF },
  590. { WAKE_ANY, Config5, LanWake }
  591. };
  592. spin_lock_irq(&tp->lock);
  593. RTL_W8(Cfg9346, Cfg9346_Unlock);
  594. for (i = 0; i < ARRAY_SIZE(cfg); i++) {
  595. u8 options = RTL_R8(cfg[i].reg) & ~cfg[i].mask;
  596. if (wol->wolopts & cfg[i].opt)
  597. options |= cfg[i].mask;
  598. RTL_W8(cfg[i].reg, options);
  599. }
  600. RTL_W8(Cfg9346, Cfg9346_Lock);
  601. tp->wol_enabled = (wol->wolopts) ? 1 : 0;
  602. spin_unlock_irq(&tp->lock);
  603. return 0;
  604. }
  605. static void rtl8169_get_drvinfo(struct net_device *dev,
  606. struct ethtool_drvinfo *info)
  607. {
  608. struct rtl8169_private *tp = netdev_priv(dev);
  609. strcpy(info->driver, MODULENAME);
  610. strcpy(info->version, RTL8169_VERSION);
  611. strcpy(info->bus_info, pci_name(tp->pci_dev));
  612. }
  613. static int rtl8169_get_regs_len(struct net_device *dev)
  614. {
  615. return R8169_REGS_SIZE;
  616. }
  617. static int rtl8169_set_speed_tbi(struct net_device *dev,
  618. u8 autoneg, u16 speed, u8 duplex)
  619. {
  620. struct rtl8169_private *tp = netdev_priv(dev);
  621. void __iomem *ioaddr = tp->mmio_addr;
  622. int ret = 0;
  623. u32 reg;
  624. reg = RTL_R32(TBICSR);
  625. if ((autoneg == AUTONEG_DISABLE) && (speed == SPEED_1000) &&
  626. (duplex == DUPLEX_FULL)) {
  627. RTL_W32(TBICSR, reg & ~(TBINwEnable | TBINwRestart));
  628. } else if (autoneg == AUTONEG_ENABLE)
  629. RTL_W32(TBICSR, reg | TBINwEnable | TBINwRestart);
  630. else {
  631. if (netif_msg_link(tp)) {
  632. printk(KERN_WARNING "%s: "
  633. "incorrect speed setting refused in TBI mode\n",
  634. dev->name);
  635. }
  636. ret = -EOPNOTSUPP;
  637. }
  638. return ret;
  639. }
  640. static int rtl8169_set_speed_xmii(struct net_device *dev,
  641. u8 autoneg, u16 speed, u8 duplex)
  642. {
  643. struct rtl8169_private *tp = netdev_priv(dev);
  644. void __iomem *ioaddr = tp->mmio_addr;
  645. int auto_nego, giga_ctrl;
  646. auto_nego = mdio_read(ioaddr, MII_ADVERTISE);
  647. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_10FULL |
  648. ADVERTISE_100HALF | ADVERTISE_100FULL);
  649. giga_ctrl = mdio_read(ioaddr, MII_CTRL1000);
  650. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  651. if (autoneg == AUTONEG_ENABLE) {
  652. auto_nego |= (ADVERTISE_10HALF | ADVERTISE_10FULL |
  653. ADVERTISE_100HALF | ADVERTISE_100FULL);
  654. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  655. } else {
  656. if (speed == SPEED_10)
  657. auto_nego |= ADVERTISE_10HALF | ADVERTISE_10FULL;
  658. else if (speed == SPEED_100)
  659. auto_nego |= ADVERTISE_100HALF | ADVERTISE_100FULL;
  660. else if (speed == SPEED_1000)
  661. giga_ctrl |= ADVERTISE_1000FULL | ADVERTISE_1000HALF;
  662. if (duplex == DUPLEX_HALF)
  663. auto_nego &= ~(ADVERTISE_10FULL | ADVERTISE_100FULL);
  664. if (duplex == DUPLEX_FULL)
  665. auto_nego &= ~(ADVERTISE_10HALF | ADVERTISE_100HALF);
  666. /* This tweak comes straight from Realtek's driver. */
  667. if ((speed == SPEED_100) && (duplex == DUPLEX_HALF) &&
  668. (tp->mac_version == RTL_GIGA_MAC_VER_13)) {
  669. auto_nego = ADVERTISE_100HALF | ADVERTISE_CSMA;
  670. }
  671. }
  672. /* The 8100e/8101e do Fast Ethernet only. */
  673. if ((tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  674. (tp->mac_version == RTL_GIGA_MAC_VER_14) ||
  675. (tp->mac_version == RTL_GIGA_MAC_VER_15)) {
  676. if ((giga_ctrl & (ADVERTISE_1000FULL | ADVERTISE_1000HALF)) &&
  677. netif_msg_link(tp)) {
  678. printk(KERN_INFO "%s: PHY does not support 1000Mbps.\n",
  679. dev->name);
  680. }
  681. giga_ctrl &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
  682. }
  683. auto_nego |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  684. tp->phy_auto_nego_reg = auto_nego;
  685. tp->phy_1000_ctrl_reg = giga_ctrl;
  686. mdio_write(ioaddr, MII_ADVERTISE, auto_nego);
  687. mdio_write(ioaddr, MII_CTRL1000, giga_ctrl);
  688. mdio_write(ioaddr, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART);
  689. return 0;
  690. }
  691. static int rtl8169_set_speed(struct net_device *dev,
  692. u8 autoneg, u16 speed, u8 duplex)
  693. {
  694. struct rtl8169_private *tp = netdev_priv(dev);
  695. int ret;
  696. ret = tp->set_speed(dev, autoneg, speed, duplex);
  697. if (netif_running(dev) && (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  698. mod_timer(&tp->timer, jiffies + RTL8169_PHY_TIMEOUT);
  699. return ret;
  700. }
  701. static int rtl8169_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  702. {
  703. struct rtl8169_private *tp = netdev_priv(dev);
  704. unsigned long flags;
  705. int ret;
  706. spin_lock_irqsave(&tp->lock, flags);
  707. ret = rtl8169_set_speed(dev, cmd->autoneg, cmd->speed, cmd->duplex);
  708. spin_unlock_irqrestore(&tp->lock, flags);
  709. return ret;
  710. }
  711. static u32 rtl8169_get_rx_csum(struct net_device *dev)
  712. {
  713. struct rtl8169_private *tp = netdev_priv(dev);
  714. return tp->cp_cmd & RxChkSum;
  715. }
  716. static int rtl8169_set_rx_csum(struct net_device *dev, u32 data)
  717. {
  718. struct rtl8169_private *tp = netdev_priv(dev);
  719. void __iomem *ioaddr = tp->mmio_addr;
  720. unsigned long flags;
  721. spin_lock_irqsave(&tp->lock, flags);
  722. if (data)
  723. tp->cp_cmd |= RxChkSum;
  724. else
  725. tp->cp_cmd &= ~RxChkSum;
  726. RTL_W16(CPlusCmd, tp->cp_cmd);
  727. RTL_R16(CPlusCmd);
  728. spin_unlock_irqrestore(&tp->lock, flags);
  729. return 0;
  730. }
  731. #ifdef CONFIG_R8169_VLAN
  732. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  733. struct sk_buff *skb)
  734. {
  735. return (tp->vlgrp && vlan_tx_tag_present(skb)) ?
  736. TxVlanTag | swab16(vlan_tx_tag_get(skb)) : 0x00;
  737. }
  738. static void rtl8169_vlan_rx_register(struct net_device *dev,
  739. struct vlan_group *grp)
  740. {
  741. struct rtl8169_private *tp = netdev_priv(dev);
  742. void __iomem *ioaddr = tp->mmio_addr;
  743. unsigned long flags;
  744. spin_lock_irqsave(&tp->lock, flags);
  745. tp->vlgrp = grp;
  746. if (tp->vlgrp)
  747. tp->cp_cmd |= RxVlan;
  748. else
  749. tp->cp_cmd &= ~RxVlan;
  750. RTL_W16(CPlusCmd, tp->cp_cmd);
  751. RTL_R16(CPlusCmd);
  752. spin_unlock_irqrestore(&tp->lock, flags);
  753. }
  754. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  755. struct sk_buff *skb)
  756. {
  757. u32 opts2 = le32_to_cpu(desc->opts2);
  758. int ret;
  759. if (tp->vlgrp && (opts2 & RxVlanTag)) {
  760. rtl8169_rx_hwaccel_skb(skb, tp->vlgrp,
  761. swab16(opts2 & 0xffff));
  762. ret = 0;
  763. } else
  764. ret = -1;
  765. desc->opts2 = 0;
  766. return ret;
  767. }
  768. #else /* !CONFIG_R8169_VLAN */
  769. static inline u32 rtl8169_tx_vlan_tag(struct rtl8169_private *tp,
  770. struct sk_buff *skb)
  771. {
  772. return 0;
  773. }
  774. static int rtl8169_rx_vlan_skb(struct rtl8169_private *tp, struct RxDesc *desc,
  775. struct sk_buff *skb)
  776. {
  777. return -1;
  778. }
  779. #endif
  780. static void rtl8169_gset_tbi(struct net_device *dev, struct ethtool_cmd *cmd)
  781. {
  782. struct rtl8169_private *tp = netdev_priv(dev);
  783. void __iomem *ioaddr = tp->mmio_addr;
  784. u32 status;
  785. cmd->supported =
  786. SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_FIBRE;
  787. cmd->port = PORT_FIBRE;
  788. cmd->transceiver = XCVR_INTERNAL;
  789. status = RTL_R32(TBICSR);
  790. cmd->advertising = (status & TBINwEnable) ? ADVERTISED_Autoneg : 0;
  791. cmd->autoneg = !!(status & TBINwEnable);
  792. cmd->speed = SPEED_1000;
  793. cmd->duplex = DUPLEX_FULL; /* Always set */
  794. }
  795. static void rtl8169_gset_xmii(struct net_device *dev, struct ethtool_cmd *cmd)
  796. {
  797. struct rtl8169_private *tp = netdev_priv(dev);
  798. void __iomem *ioaddr = tp->mmio_addr;
  799. u8 status;
  800. cmd->supported = SUPPORTED_10baseT_Half |
  801. SUPPORTED_10baseT_Full |
  802. SUPPORTED_100baseT_Half |
  803. SUPPORTED_100baseT_Full |
  804. SUPPORTED_1000baseT_Full |
  805. SUPPORTED_Autoneg |
  806. SUPPORTED_TP;
  807. cmd->autoneg = 1;
  808. cmd->advertising = ADVERTISED_TP | ADVERTISED_Autoneg;
  809. if (tp->phy_auto_nego_reg & ADVERTISE_10HALF)
  810. cmd->advertising |= ADVERTISED_10baseT_Half;
  811. if (tp->phy_auto_nego_reg & ADVERTISE_10FULL)
  812. cmd->advertising |= ADVERTISED_10baseT_Full;
  813. if (tp->phy_auto_nego_reg & ADVERTISE_100HALF)
  814. cmd->advertising |= ADVERTISED_100baseT_Half;
  815. if (tp->phy_auto_nego_reg & ADVERTISE_100FULL)
  816. cmd->advertising |= ADVERTISED_100baseT_Full;
  817. if (tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL)
  818. cmd->advertising |= ADVERTISED_1000baseT_Full;
  819. status = RTL_R8(PHYstatus);
  820. if (status & _1000bpsF)
  821. cmd->speed = SPEED_1000;
  822. else if (status & _100bps)
  823. cmd->speed = SPEED_100;
  824. else if (status & _10bps)
  825. cmd->speed = SPEED_10;
  826. if (status & TxFlowCtrl)
  827. cmd->advertising |= ADVERTISED_Asym_Pause;
  828. if (status & RxFlowCtrl)
  829. cmd->advertising |= ADVERTISED_Pause;
  830. cmd->duplex = ((status & _1000bpsF) || (status & FullDup)) ?
  831. DUPLEX_FULL : DUPLEX_HALF;
  832. }
  833. static int rtl8169_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  834. {
  835. struct rtl8169_private *tp = netdev_priv(dev);
  836. unsigned long flags;
  837. spin_lock_irqsave(&tp->lock, flags);
  838. tp->get_settings(dev, cmd);
  839. spin_unlock_irqrestore(&tp->lock, flags);
  840. return 0;
  841. }
  842. static void rtl8169_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  843. void *p)
  844. {
  845. struct rtl8169_private *tp = netdev_priv(dev);
  846. unsigned long flags;
  847. if (regs->len > R8169_REGS_SIZE)
  848. regs->len = R8169_REGS_SIZE;
  849. spin_lock_irqsave(&tp->lock, flags);
  850. memcpy_fromio(p, tp->mmio_addr, regs->len);
  851. spin_unlock_irqrestore(&tp->lock, flags);
  852. }
  853. static u32 rtl8169_get_msglevel(struct net_device *dev)
  854. {
  855. struct rtl8169_private *tp = netdev_priv(dev);
  856. return tp->msg_enable;
  857. }
  858. static void rtl8169_set_msglevel(struct net_device *dev, u32 value)
  859. {
  860. struct rtl8169_private *tp = netdev_priv(dev);
  861. tp->msg_enable = value;
  862. }
  863. static const char rtl8169_gstrings[][ETH_GSTRING_LEN] = {
  864. "tx_packets",
  865. "rx_packets",
  866. "tx_errors",
  867. "rx_errors",
  868. "rx_missed",
  869. "align_errors",
  870. "tx_single_collisions",
  871. "tx_multi_collisions",
  872. "unicast",
  873. "broadcast",
  874. "multicast",
  875. "tx_aborted",
  876. "tx_underrun",
  877. };
  878. struct rtl8169_counters {
  879. u64 tx_packets;
  880. u64 rx_packets;
  881. u64 tx_errors;
  882. u32 rx_errors;
  883. u16 rx_missed;
  884. u16 align_errors;
  885. u32 tx_one_collision;
  886. u32 tx_multi_collision;
  887. u64 rx_unicast;
  888. u64 rx_broadcast;
  889. u32 rx_multicast;
  890. u16 tx_aborted;
  891. u16 tx_underun;
  892. };
  893. static int rtl8169_get_stats_count(struct net_device *dev)
  894. {
  895. return ARRAY_SIZE(rtl8169_gstrings);
  896. }
  897. static void rtl8169_get_ethtool_stats(struct net_device *dev,
  898. struct ethtool_stats *stats, u64 *data)
  899. {
  900. struct rtl8169_private *tp = netdev_priv(dev);
  901. void __iomem *ioaddr = tp->mmio_addr;
  902. struct rtl8169_counters *counters;
  903. dma_addr_t paddr;
  904. u32 cmd;
  905. ASSERT_RTNL();
  906. counters = pci_alloc_consistent(tp->pci_dev, sizeof(*counters), &paddr);
  907. if (!counters)
  908. return;
  909. RTL_W32(CounterAddrHigh, (u64)paddr >> 32);
  910. cmd = (u64)paddr & DMA_32BIT_MASK;
  911. RTL_W32(CounterAddrLow, cmd);
  912. RTL_W32(CounterAddrLow, cmd | CounterDump);
  913. while (RTL_R32(CounterAddrLow) & CounterDump) {
  914. if (msleep_interruptible(1))
  915. break;
  916. }
  917. RTL_W32(CounterAddrLow, 0);
  918. RTL_W32(CounterAddrHigh, 0);
  919. data[0] = le64_to_cpu(counters->tx_packets);
  920. data[1] = le64_to_cpu(counters->rx_packets);
  921. data[2] = le64_to_cpu(counters->tx_errors);
  922. data[3] = le32_to_cpu(counters->rx_errors);
  923. data[4] = le16_to_cpu(counters->rx_missed);
  924. data[5] = le16_to_cpu(counters->align_errors);
  925. data[6] = le32_to_cpu(counters->tx_one_collision);
  926. data[7] = le32_to_cpu(counters->tx_multi_collision);
  927. data[8] = le64_to_cpu(counters->rx_unicast);
  928. data[9] = le64_to_cpu(counters->rx_broadcast);
  929. data[10] = le32_to_cpu(counters->rx_multicast);
  930. data[11] = le16_to_cpu(counters->tx_aborted);
  931. data[12] = le16_to_cpu(counters->tx_underun);
  932. pci_free_consistent(tp->pci_dev, sizeof(*counters), counters, paddr);
  933. }
  934. static void rtl8169_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  935. {
  936. switch(stringset) {
  937. case ETH_SS_STATS:
  938. memcpy(data, *rtl8169_gstrings, sizeof(rtl8169_gstrings));
  939. break;
  940. }
  941. }
  942. static const struct ethtool_ops rtl8169_ethtool_ops = {
  943. .get_drvinfo = rtl8169_get_drvinfo,
  944. .get_regs_len = rtl8169_get_regs_len,
  945. .get_link = ethtool_op_get_link,
  946. .get_settings = rtl8169_get_settings,
  947. .set_settings = rtl8169_set_settings,
  948. .get_msglevel = rtl8169_get_msglevel,
  949. .set_msglevel = rtl8169_set_msglevel,
  950. .get_rx_csum = rtl8169_get_rx_csum,
  951. .set_rx_csum = rtl8169_set_rx_csum,
  952. .get_tx_csum = ethtool_op_get_tx_csum,
  953. .set_tx_csum = ethtool_op_set_tx_csum,
  954. .get_sg = ethtool_op_get_sg,
  955. .set_sg = ethtool_op_set_sg,
  956. .get_tso = ethtool_op_get_tso,
  957. .set_tso = ethtool_op_set_tso,
  958. .get_regs = rtl8169_get_regs,
  959. .get_wol = rtl8169_get_wol,
  960. .set_wol = rtl8169_set_wol,
  961. .get_strings = rtl8169_get_strings,
  962. .get_stats_count = rtl8169_get_stats_count,
  963. .get_ethtool_stats = rtl8169_get_ethtool_stats,
  964. .get_perm_addr = ethtool_op_get_perm_addr,
  965. };
  966. static void rtl8169_write_gmii_reg_bit(void __iomem *ioaddr, int reg, int bitnum,
  967. int bitval)
  968. {
  969. int val;
  970. val = mdio_read(ioaddr, reg);
  971. val = (bitval == 1) ?
  972. val | (bitval << bitnum) : val & ~(0x0001 << bitnum);
  973. mdio_write(ioaddr, reg, val & 0xffff);
  974. }
  975. static void rtl8169_get_mac_version(struct rtl8169_private *tp, void __iomem *ioaddr)
  976. {
  977. const struct {
  978. u32 mask;
  979. int mac_version;
  980. } mac_info[] = {
  981. { 0x38800000, RTL_GIGA_MAC_VER_15 },
  982. { 0x38000000, RTL_GIGA_MAC_VER_12 },
  983. { 0x34000000, RTL_GIGA_MAC_VER_13 },
  984. { 0x30800000, RTL_GIGA_MAC_VER_14 },
  985. { 0x30000000, RTL_GIGA_MAC_VER_11 },
  986. { 0x18000000, RTL_GIGA_MAC_VER_05 },
  987. { 0x10000000, RTL_GIGA_MAC_VER_04 },
  988. { 0x04000000, RTL_GIGA_MAC_VER_03 },
  989. { 0x00800000, RTL_GIGA_MAC_VER_02 },
  990. { 0x00000000, RTL_GIGA_MAC_VER_01 } /* Catch-all */
  991. }, *p = mac_info;
  992. u32 reg;
  993. reg = RTL_R32(TxConfig) & 0x7c800000;
  994. while ((reg & p->mask) != p->mask)
  995. p++;
  996. tp->mac_version = p->mac_version;
  997. }
  998. static void rtl8169_print_mac_version(struct rtl8169_private *tp)
  999. {
  1000. dprintk("mac_version = 0x%02x\n", tp->mac_version);
  1001. }
  1002. static void rtl8169_get_phy_version(struct rtl8169_private *tp, void __iomem *ioaddr)
  1003. {
  1004. const struct {
  1005. u16 mask;
  1006. u16 set;
  1007. int phy_version;
  1008. } phy_info[] = {
  1009. { 0x000f, 0x0002, RTL_GIGA_PHY_VER_G },
  1010. { 0x000f, 0x0001, RTL_GIGA_PHY_VER_F },
  1011. { 0x000f, 0x0000, RTL_GIGA_PHY_VER_E },
  1012. { 0x0000, 0x0000, RTL_GIGA_PHY_VER_D } /* Catch-all */
  1013. }, *p = phy_info;
  1014. u16 reg;
  1015. reg = mdio_read(ioaddr, MII_PHYSID2) & 0xffff;
  1016. while ((reg & p->mask) != p->set)
  1017. p++;
  1018. tp->phy_version = p->phy_version;
  1019. }
  1020. static void rtl8169_print_phy_version(struct rtl8169_private *tp)
  1021. {
  1022. struct {
  1023. int version;
  1024. char *msg;
  1025. u32 reg;
  1026. } phy_print[] = {
  1027. { RTL_GIGA_PHY_VER_G, "RTL_GIGA_PHY_VER_G", 0x0002 },
  1028. { RTL_GIGA_PHY_VER_F, "RTL_GIGA_PHY_VER_F", 0x0001 },
  1029. { RTL_GIGA_PHY_VER_E, "RTL_GIGA_PHY_VER_E", 0x0000 },
  1030. { RTL_GIGA_PHY_VER_D, "RTL_GIGA_PHY_VER_D", 0x0000 },
  1031. { 0, NULL, 0x0000 }
  1032. }, *p;
  1033. for (p = phy_print; p->msg; p++) {
  1034. if (tp->phy_version == p->version) {
  1035. dprintk("phy_version == %s (%04x)\n", p->msg, p->reg);
  1036. return;
  1037. }
  1038. }
  1039. dprintk("phy_version == Unknown\n");
  1040. }
  1041. static void rtl8169_hw_phy_config(struct net_device *dev)
  1042. {
  1043. struct rtl8169_private *tp = netdev_priv(dev);
  1044. void __iomem *ioaddr = tp->mmio_addr;
  1045. struct {
  1046. u16 regs[5]; /* Beware of bit-sign propagation */
  1047. } phy_magic[5] = { {
  1048. { 0x0000, //w 4 15 12 0
  1049. 0x00a1, //w 3 15 0 00a1
  1050. 0x0008, //w 2 15 0 0008
  1051. 0x1020, //w 1 15 0 1020
  1052. 0x1000 } },{ //w 0 15 0 1000
  1053. { 0x7000, //w 4 15 12 7
  1054. 0xff41, //w 3 15 0 ff41
  1055. 0xde60, //w 2 15 0 de60
  1056. 0x0140, //w 1 15 0 0140
  1057. 0x0077 } },{ //w 0 15 0 0077
  1058. { 0xa000, //w 4 15 12 a
  1059. 0xdf01, //w 3 15 0 df01
  1060. 0xdf20, //w 2 15 0 df20
  1061. 0xff95, //w 1 15 0 ff95
  1062. 0xfa00 } },{ //w 0 15 0 fa00
  1063. { 0xb000, //w 4 15 12 b
  1064. 0xff41, //w 3 15 0 ff41
  1065. 0xde20, //w 2 15 0 de20
  1066. 0x0140, //w 1 15 0 0140
  1067. 0x00bb } },{ //w 0 15 0 00bb
  1068. { 0xf000, //w 4 15 12 f
  1069. 0xdf01, //w 3 15 0 df01
  1070. 0xdf20, //w 2 15 0 df20
  1071. 0xff95, //w 1 15 0 ff95
  1072. 0xbf00 } //w 0 15 0 bf00
  1073. }
  1074. }, *p = phy_magic;
  1075. int i;
  1076. rtl8169_print_mac_version(tp);
  1077. rtl8169_print_phy_version(tp);
  1078. if (tp->mac_version <= RTL_GIGA_MAC_VER_01)
  1079. return;
  1080. if (tp->phy_version >= RTL_GIGA_PHY_VER_H)
  1081. return;
  1082. dprintk("MAC version != 0 && PHY version == 0 or 1\n");
  1083. dprintk("Do final_reg2.cfg\n");
  1084. /* Shazam ! */
  1085. if (tp->mac_version == RTL_GIGA_MAC_VER_04) {
  1086. mdio_write(ioaddr, 31, 0x0002);
  1087. mdio_write(ioaddr, 1, 0x90d0);
  1088. mdio_write(ioaddr, 31, 0x0000);
  1089. return;
  1090. }
  1091. /* phy config for RTL8169s mac_version C chip */
  1092. mdio_write(ioaddr, 31, 0x0001); //w 31 2 0 1
  1093. mdio_write(ioaddr, 21, 0x1000); //w 21 15 0 1000
  1094. mdio_write(ioaddr, 24, 0x65c7); //w 24 15 0 65c7
  1095. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  1096. for (i = 0; i < ARRAY_SIZE(phy_magic); i++, p++) {
  1097. int val, pos = 4;
  1098. val = (mdio_read(ioaddr, pos) & 0x0fff) | (p->regs[0] & 0xffff);
  1099. mdio_write(ioaddr, pos, val);
  1100. while (--pos >= 0)
  1101. mdio_write(ioaddr, pos, p->regs[4 - pos] & 0xffff);
  1102. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 1); //w 4 11 11 1
  1103. rtl8169_write_gmii_reg_bit(ioaddr, 4, 11, 0); //w 4 11 11 0
  1104. }
  1105. mdio_write(ioaddr, 31, 0x0000); //w 31 2 0 0
  1106. }
  1107. static void rtl8169_phy_timer(unsigned long __opaque)
  1108. {
  1109. struct net_device *dev = (struct net_device *)__opaque;
  1110. struct rtl8169_private *tp = netdev_priv(dev);
  1111. struct timer_list *timer = &tp->timer;
  1112. void __iomem *ioaddr = tp->mmio_addr;
  1113. unsigned long timeout = RTL8169_PHY_TIMEOUT;
  1114. assert(tp->mac_version > RTL_GIGA_MAC_VER_01);
  1115. assert(tp->phy_version < RTL_GIGA_PHY_VER_H);
  1116. if (!(tp->phy_1000_ctrl_reg & ADVERTISE_1000FULL))
  1117. return;
  1118. spin_lock_irq(&tp->lock);
  1119. if (tp->phy_reset_pending(ioaddr)) {
  1120. /*
  1121. * A busy loop could burn quite a few cycles on nowadays CPU.
  1122. * Let's delay the execution of the timer for a few ticks.
  1123. */
  1124. timeout = HZ/10;
  1125. goto out_mod_timer;
  1126. }
  1127. if (tp->link_ok(ioaddr))
  1128. goto out_unlock;
  1129. if (netif_msg_link(tp))
  1130. printk(KERN_WARNING "%s: PHY reset until link up\n", dev->name);
  1131. tp->phy_reset_enable(ioaddr);
  1132. out_mod_timer:
  1133. mod_timer(timer, jiffies + timeout);
  1134. out_unlock:
  1135. spin_unlock_irq(&tp->lock);
  1136. }
  1137. static inline void rtl8169_delete_timer(struct net_device *dev)
  1138. {
  1139. struct rtl8169_private *tp = netdev_priv(dev);
  1140. struct timer_list *timer = &tp->timer;
  1141. if ((tp->mac_version <= RTL_GIGA_MAC_VER_01) ||
  1142. (tp->phy_version >= RTL_GIGA_PHY_VER_H))
  1143. return;
  1144. del_timer_sync(timer);
  1145. }
  1146. static inline void rtl8169_request_timer(struct net_device *dev)
  1147. {
  1148. struct rtl8169_private *tp = netdev_priv(dev);
  1149. struct timer_list *timer = &tp->timer;
  1150. if ((tp->mac_version <= RTL_GIGA_MAC_VER_01) ||
  1151. (tp->phy_version >= RTL_GIGA_PHY_VER_H))
  1152. return;
  1153. mod_timer(timer, jiffies + RTL8169_PHY_TIMEOUT);
  1154. }
  1155. #ifdef CONFIG_NET_POLL_CONTROLLER
  1156. /*
  1157. * Polling 'interrupt' - used by things like netconsole to send skbs
  1158. * without having to re-enable interrupts. It's not called while
  1159. * the interrupt routine is executing.
  1160. */
  1161. static void rtl8169_netpoll(struct net_device *dev)
  1162. {
  1163. struct rtl8169_private *tp = netdev_priv(dev);
  1164. struct pci_dev *pdev = tp->pci_dev;
  1165. disable_irq(pdev->irq);
  1166. rtl8169_interrupt(pdev->irq, dev);
  1167. enable_irq(pdev->irq);
  1168. }
  1169. #endif
  1170. static void rtl8169_release_board(struct pci_dev *pdev, struct net_device *dev,
  1171. void __iomem *ioaddr)
  1172. {
  1173. iounmap(ioaddr);
  1174. pci_release_regions(pdev);
  1175. pci_disable_device(pdev);
  1176. free_netdev(dev);
  1177. }
  1178. static void rtl8169_phy_reset(struct net_device *dev,
  1179. struct rtl8169_private *tp)
  1180. {
  1181. void __iomem *ioaddr = tp->mmio_addr;
  1182. int i;
  1183. tp->phy_reset_enable(ioaddr);
  1184. for (i = 0; i < 100; i++) {
  1185. if (!tp->phy_reset_pending(ioaddr))
  1186. return;
  1187. msleep(1);
  1188. }
  1189. if (netif_msg_link(tp))
  1190. printk(KERN_ERR "%s: PHY reset failed.\n", dev->name);
  1191. }
  1192. static void rtl8169_init_phy(struct net_device *dev, struct rtl8169_private *tp)
  1193. {
  1194. void __iomem *ioaddr = tp->mmio_addr;
  1195. static int board_idx = -1;
  1196. u8 autoneg, duplex;
  1197. u16 speed;
  1198. board_idx++;
  1199. rtl8169_hw_phy_config(dev);
  1200. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1201. RTL_W8(0x82, 0x01);
  1202. if (tp->mac_version < RTL_GIGA_MAC_VER_03) {
  1203. dprintk("Set PCI Latency=0x40\n");
  1204. pci_write_config_byte(tp->pci_dev, PCI_LATENCY_TIMER, 0x40);
  1205. }
  1206. if (tp->mac_version == RTL_GIGA_MAC_VER_02) {
  1207. dprintk("Set MAC Reg C+CR Offset 0x82h = 0x01h\n");
  1208. RTL_W8(0x82, 0x01);
  1209. dprintk("Set PHY Reg 0x0bh = 0x00h\n");
  1210. mdio_write(ioaddr, 0x0b, 0x0000); //w 0x0b 15 0 0
  1211. }
  1212. rtl8169_link_option(board_idx, &autoneg, &speed, &duplex);
  1213. rtl8169_phy_reset(dev, tp);
  1214. rtl8169_set_speed(dev, autoneg, speed, duplex);
  1215. if ((RTL_R8(PHYstatus) & TBI_Enable) && netif_msg_link(tp))
  1216. printk(KERN_INFO PFX "%s: TBI auto-negotiating\n", dev->name);
  1217. }
  1218. static int rtl8169_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  1219. {
  1220. struct rtl8169_private *tp = netdev_priv(dev);
  1221. struct mii_ioctl_data *data = if_mii(ifr);
  1222. if (!netif_running(dev))
  1223. return -ENODEV;
  1224. switch (cmd) {
  1225. case SIOCGMIIPHY:
  1226. data->phy_id = 32; /* Internal PHY */
  1227. return 0;
  1228. case SIOCGMIIREG:
  1229. data->val_out = mdio_read(tp->mmio_addr, data->reg_num & 0x1f);
  1230. return 0;
  1231. case SIOCSMIIREG:
  1232. if (!capable(CAP_NET_ADMIN))
  1233. return -EPERM;
  1234. mdio_write(tp->mmio_addr, data->reg_num & 0x1f, data->val_in);
  1235. return 0;
  1236. }
  1237. return -EOPNOTSUPP;
  1238. }
  1239. static int __devinit
  1240. rtl8169_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1241. {
  1242. const unsigned int region = rtl_cfg_info[ent->driver_data].region;
  1243. struct rtl8169_private *tp;
  1244. struct net_device *dev;
  1245. void __iomem *ioaddr;
  1246. unsigned int pm_cap;
  1247. int i, rc;
  1248. if (netif_msg_drv(&debug)) {
  1249. printk(KERN_INFO "%s Gigabit Ethernet driver %s loaded\n",
  1250. MODULENAME, RTL8169_VERSION);
  1251. }
  1252. dev = alloc_etherdev(sizeof (*tp));
  1253. if (!dev) {
  1254. if (netif_msg_drv(&debug))
  1255. dev_err(&pdev->dev, "unable to alloc new ethernet\n");
  1256. rc = -ENOMEM;
  1257. goto out;
  1258. }
  1259. SET_MODULE_OWNER(dev);
  1260. SET_NETDEV_DEV(dev, &pdev->dev);
  1261. tp = netdev_priv(dev);
  1262. tp->dev = dev;
  1263. tp->msg_enable = netif_msg_init(debug.msg_enable, R8169_MSG_DEFAULT);
  1264. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  1265. rc = pci_enable_device(pdev);
  1266. if (rc < 0) {
  1267. if (netif_msg_probe(tp))
  1268. dev_err(&pdev->dev, "enable failure\n");
  1269. goto err_out_free_dev_1;
  1270. }
  1271. rc = pci_set_mwi(pdev);
  1272. if (rc < 0)
  1273. goto err_out_disable_2;
  1274. /* save power state before pci_enable_device overwrites it */
  1275. pm_cap = pci_find_capability(pdev, PCI_CAP_ID_PM);
  1276. if (pm_cap) {
  1277. u16 pwr_command, acpi_idle_state;
  1278. pci_read_config_word(pdev, pm_cap + PCI_PM_CTRL, &pwr_command);
  1279. acpi_idle_state = pwr_command & PCI_PM_CTRL_STATE_MASK;
  1280. } else {
  1281. if (netif_msg_probe(tp)) {
  1282. dev_err(&pdev->dev,
  1283. "PowerManagement capability not found.\n");
  1284. }
  1285. }
  1286. /* make sure PCI base addr 1 is MMIO */
  1287. if (!(pci_resource_flags(pdev, region) & IORESOURCE_MEM)) {
  1288. if (netif_msg_probe(tp)) {
  1289. dev_err(&pdev->dev,
  1290. "region #%d not an MMIO resource, aborting\n",
  1291. region);
  1292. }
  1293. rc = -ENODEV;
  1294. goto err_out_mwi_3;
  1295. }
  1296. /* check for weird/broken PCI region reporting */
  1297. if (pci_resource_len(pdev, region) < R8169_REGS_SIZE) {
  1298. if (netif_msg_probe(tp)) {
  1299. dev_err(&pdev->dev,
  1300. "Invalid PCI region size(s), aborting\n");
  1301. }
  1302. rc = -ENODEV;
  1303. goto err_out_mwi_3;
  1304. }
  1305. rc = pci_request_regions(pdev, MODULENAME);
  1306. if (rc < 0) {
  1307. if (netif_msg_probe(tp))
  1308. dev_err(&pdev->dev, "could not request regions.\n");
  1309. goto err_out_mwi_3;
  1310. }
  1311. tp->cp_cmd = PCIMulRW | RxChkSum;
  1312. if ((sizeof(dma_addr_t) > 4) &&
  1313. !pci_set_dma_mask(pdev, DMA_64BIT_MASK) && use_dac) {
  1314. tp->cp_cmd |= PCIDAC;
  1315. dev->features |= NETIF_F_HIGHDMA;
  1316. } else {
  1317. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1318. if (rc < 0) {
  1319. if (netif_msg_probe(tp)) {
  1320. dev_err(&pdev->dev,
  1321. "DMA configuration failed.\n");
  1322. }
  1323. goto err_out_free_res_4;
  1324. }
  1325. }
  1326. pci_set_master(pdev);
  1327. /* ioremap MMIO region */
  1328. ioaddr = ioremap(pci_resource_start(pdev, region), R8169_REGS_SIZE);
  1329. if (!ioaddr) {
  1330. if (netif_msg_probe(tp))
  1331. dev_err(&pdev->dev, "cannot remap MMIO, aborting\n");
  1332. rc = -EIO;
  1333. goto err_out_free_res_4;
  1334. }
  1335. /* Unneeded ? Don't mess with Mrs. Murphy. */
  1336. rtl8169_irq_mask_and_ack(ioaddr);
  1337. /* Soft reset the chip. */
  1338. RTL_W8(ChipCmd, CmdReset);
  1339. /* Check that the chip has finished the reset. */
  1340. for (i = 100; i > 0; i--) {
  1341. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  1342. break;
  1343. msleep_interruptible(1);
  1344. }
  1345. /* Identify chip attached to board */
  1346. rtl8169_get_mac_version(tp, ioaddr);
  1347. rtl8169_get_phy_version(tp, ioaddr);
  1348. rtl8169_print_mac_version(tp);
  1349. rtl8169_print_phy_version(tp);
  1350. for (i = ARRAY_SIZE(rtl_chip_info) - 1; i >= 0; i--) {
  1351. if (tp->mac_version == rtl_chip_info[i].mac_version)
  1352. break;
  1353. }
  1354. if (i < 0) {
  1355. /* Unknown chip: assume array element #0, original RTL-8169 */
  1356. if (netif_msg_probe(tp)) {
  1357. dev_printk(KERN_DEBUG, &pdev->dev,
  1358. "unknown chip version, assuming %s\n",
  1359. rtl_chip_info[0].name);
  1360. }
  1361. i++;
  1362. }
  1363. tp->chipset = i;
  1364. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1365. RTL_W8(Config1, RTL_R8(Config1) | PMEnable);
  1366. RTL_W8(Config5, RTL_R8(Config5) & PMEStatus);
  1367. RTL_W8(Cfg9346, Cfg9346_Lock);
  1368. if (RTL_R8(PHYstatus) & TBI_Enable) {
  1369. tp->set_speed = rtl8169_set_speed_tbi;
  1370. tp->get_settings = rtl8169_gset_tbi;
  1371. tp->phy_reset_enable = rtl8169_tbi_reset_enable;
  1372. tp->phy_reset_pending = rtl8169_tbi_reset_pending;
  1373. tp->link_ok = rtl8169_tbi_link_ok;
  1374. tp->phy_1000_ctrl_reg = ADVERTISE_1000FULL; /* Implied by TBI */
  1375. } else {
  1376. tp->set_speed = rtl8169_set_speed_xmii;
  1377. tp->get_settings = rtl8169_gset_xmii;
  1378. tp->phy_reset_enable = rtl8169_xmii_reset_enable;
  1379. tp->phy_reset_pending = rtl8169_xmii_reset_pending;
  1380. tp->link_ok = rtl8169_xmii_link_ok;
  1381. dev->do_ioctl = rtl8169_ioctl;
  1382. }
  1383. /* Get MAC address. FIXME: read EEPROM */
  1384. for (i = 0; i < MAC_ADDR_LEN; i++)
  1385. dev->dev_addr[i] = RTL_R8(MAC0 + i);
  1386. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  1387. dev->open = rtl8169_open;
  1388. dev->hard_start_xmit = rtl8169_start_xmit;
  1389. dev->get_stats = rtl8169_get_stats;
  1390. SET_ETHTOOL_OPS(dev, &rtl8169_ethtool_ops);
  1391. dev->stop = rtl8169_close;
  1392. dev->tx_timeout = rtl8169_tx_timeout;
  1393. dev->set_multicast_list = rtl_set_rx_mode;
  1394. dev->watchdog_timeo = RTL8169_TX_TIMEOUT;
  1395. dev->irq = pdev->irq;
  1396. dev->base_addr = (unsigned long) ioaddr;
  1397. dev->change_mtu = rtl8169_change_mtu;
  1398. #ifdef CONFIG_R8169_NAPI
  1399. dev->poll = rtl8169_poll;
  1400. dev->weight = R8169_NAPI_WEIGHT;
  1401. #endif
  1402. #ifdef CONFIG_R8169_VLAN
  1403. dev->features |= NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX;
  1404. dev->vlan_rx_register = rtl8169_vlan_rx_register;
  1405. #endif
  1406. #ifdef CONFIG_NET_POLL_CONTROLLER
  1407. dev->poll_controller = rtl8169_netpoll;
  1408. #endif
  1409. tp->intr_mask = 0xffff;
  1410. tp->pci_dev = pdev;
  1411. tp->mmio_addr = ioaddr;
  1412. tp->align = rtl_cfg_info[ent->driver_data].align;
  1413. init_timer(&tp->timer);
  1414. tp->timer.data = (unsigned long) dev;
  1415. tp->timer.function = rtl8169_phy_timer;
  1416. tp->hw_start = rtl_cfg_info[ent->driver_data].hw_start;
  1417. spin_lock_init(&tp->lock);
  1418. rc = register_netdev(dev);
  1419. if (rc < 0)
  1420. goto err_out_unmap_5;
  1421. pci_set_drvdata(pdev, dev);
  1422. if (netif_msg_probe(tp)) {
  1423. printk(KERN_INFO "%s: %s at 0x%lx, "
  1424. "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x, "
  1425. "IRQ %d\n",
  1426. dev->name,
  1427. rtl_chip_info[tp->chipset].name,
  1428. dev->base_addr,
  1429. dev->dev_addr[0], dev->dev_addr[1],
  1430. dev->dev_addr[2], dev->dev_addr[3],
  1431. dev->dev_addr[4], dev->dev_addr[5], dev->irq);
  1432. }
  1433. rtl8169_init_phy(dev, tp);
  1434. out:
  1435. return rc;
  1436. err_out_unmap_5:
  1437. iounmap(ioaddr);
  1438. err_out_free_res_4:
  1439. pci_release_regions(pdev);
  1440. err_out_mwi_3:
  1441. pci_clear_mwi(pdev);
  1442. err_out_disable_2:
  1443. pci_disable_device(pdev);
  1444. err_out_free_dev_1:
  1445. free_netdev(dev);
  1446. goto out;
  1447. }
  1448. static void __devexit
  1449. rtl8169_remove_one(struct pci_dev *pdev)
  1450. {
  1451. struct net_device *dev = pci_get_drvdata(pdev);
  1452. struct rtl8169_private *tp = netdev_priv(dev);
  1453. assert(dev != NULL);
  1454. assert(tp != NULL);
  1455. flush_scheduled_work();
  1456. unregister_netdev(dev);
  1457. rtl8169_release_board(pdev, dev, tp->mmio_addr);
  1458. pci_set_drvdata(pdev, NULL);
  1459. }
  1460. static void rtl8169_set_rxbufsize(struct rtl8169_private *tp,
  1461. struct net_device *dev)
  1462. {
  1463. unsigned int mtu = dev->mtu;
  1464. tp->rx_buf_sz = (mtu > RX_BUF_SIZE) ? mtu + ETH_HLEN + 8 : RX_BUF_SIZE;
  1465. }
  1466. static int rtl8169_open(struct net_device *dev)
  1467. {
  1468. struct rtl8169_private *tp = netdev_priv(dev);
  1469. struct pci_dev *pdev = tp->pci_dev;
  1470. int retval = -ENOMEM;
  1471. rtl8169_set_rxbufsize(tp, dev);
  1472. /*
  1473. * Rx and Tx desscriptors needs 256 bytes alignment.
  1474. * pci_alloc_consistent provides more.
  1475. */
  1476. tp->TxDescArray = pci_alloc_consistent(pdev, R8169_TX_RING_BYTES,
  1477. &tp->TxPhyAddr);
  1478. if (!tp->TxDescArray)
  1479. goto out;
  1480. tp->RxDescArray = pci_alloc_consistent(pdev, R8169_RX_RING_BYTES,
  1481. &tp->RxPhyAddr);
  1482. if (!tp->RxDescArray)
  1483. goto err_free_tx_0;
  1484. retval = rtl8169_init_ring(dev);
  1485. if (retval < 0)
  1486. goto err_free_rx_1;
  1487. INIT_DELAYED_WORK(&tp->task, NULL);
  1488. smp_mb();
  1489. retval = request_irq(dev->irq, rtl8169_interrupt, IRQF_SHARED,
  1490. dev->name, dev);
  1491. if (retval < 0)
  1492. goto err_release_ring_2;
  1493. rtl_hw_start(dev);
  1494. rtl8169_request_timer(dev);
  1495. rtl8169_check_link_status(dev, tp, tp->mmio_addr);
  1496. out:
  1497. return retval;
  1498. err_release_ring_2:
  1499. rtl8169_rx_clear(tp);
  1500. err_free_rx_1:
  1501. pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
  1502. tp->RxPhyAddr);
  1503. err_free_tx_0:
  1504. pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
  1505. tp->TxPhyAddr);
  1506. goto out;
  1507. }
  1508. static void rtl8169_hw_reset(void __iomem *ioaddr)
  1509. {
  1510. /* Disable interrupts */
  1511. rtl8169_irq_mask_and_ack(ioaddr);
  1512. /* Reset the chipset */
  1513. RTL_W8(ChipCmd, CmdReset);
  1514. /* PCI commit */
  1515. RTL_R8(ChipCmd);
  1516. }
  1517. static void rtl_set_rx_tx_config_registers(struct rtl8169_private *tp)
  1518. {
  1519. void __iomem *ioaddr = tp->mmio_addr;
  1520. u32 cfg = rtl8169_rx_config;
  1521. cfg |= (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  1522. RTL_W32(RxConfig, cfg);
  1523. /* Set DMA burst size and Interframe Gap Time */
  1524. RTL_W32(TxConfig, (TX_DMA_BURST << TxDMAShift) |
  1525. (InterFrameGap << TxInterFrameGapShift));
  1526. }
  1527. static void rtl_hw_start(struct net_device *dev)
  1528. {
  1529. struct rtl8169_private *tp = netdev_priv(dev);
  1530. void __iomem *ioaddr = tp->mmio_addr;
  1531. u32 i;
  1532. /* Soft reset the chip. */
  1533. RTL_W8(ChipCmd, CmdReset);
  1534. /* Check that the chip has finished the reset. */
  1535. for (i = 100; i > 0; i--) {
  1536. if ((RTL_R8(ChipCmd) & CmdReset) == 0)
  1537. break;
  1538. msleep_interruptible(1);
  1539. }
  1540. tp->hw_start(dev);
  1541. /* Enable all known interrupts by setting the interrupt mask. */
  1542. RTL_W16(IntrMask, rtl8169_intr_mask);
  1543. netif_start_queue(dev);
  1544. }
  1545. static void rtl_set_rx_tx_desc_registers(struct rtl8169_private *tp,
  1546. void __iomem *ioaddr)
  1547. {
  1548. /*
  1549. * Magic spell: some iop3xx ARM board needs the TxDescAddrHigh
  1550. * register to be written before TxDescAddrLow to work.
  1551. * Switching from MMIO to I/O access fixes the issue as well.
  1552. */
  1553. RTL_W32(TxDescStartAddrHigh, ((u64) tp->TxPhyAddr) >> 32);
  1554. RTL_W32(TxDescStartAddrLow, ((u64) tp->TxPhyAddr) & DMA_32BIT_MASK);
  1555. RTL_W32(RxDescAddrHigh, ((u64) tp->RxPhyAddr) >> 32);
  1556. RTL_W32(RxDescAddrLow, ((u64) tp->RxPhyAddr) & DMA_32BIT_MASK);
  1557. }
  1558. static u16 rtl_rw_cpluscmd(void __iomem *ioaddr)
  1559. {
  1560. u16 cmd;
  1561. cmd = RTL_R16(CPlusCmd);
  1562. RTL_W16(CPlusCmd, cmd);
  1563. return cmd;
  1564. }
  1565. static void rtl_set_rx_max_size(void __iomem *ioaddr)
  1566. {
  1567. /* Low hurts. Let's disable the filtering. */
  1568. RTL_W16(RxMaxSize, 16383);
  1569. }
  1570. static void rtl_hw_start_8169(struct net_device *dev)
  1571. {
  1572. struct rtl8169_private *tp = netdev_priv(dev);
  1573. void __iomem *ioaddr = tp->mmio_addr;
  1574. struct pci_dev *pdev = tp->pci_dev;
  1575. u16 cmd;
  1576. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  1577. RTL_W16(CPlusCmd, RTL_R16(CPlusCmd) | PCIMulRW);
  1578. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  1579. }
  1580. if (tp->mac_version == RTL_GIGA_MAC_VER_13) {
  1581. pci_write_config_word(pdev, 0x68, 0x00);
  1582. pci_write_config_word(pdev, 0x69, 0x08);
  1583. }
  1584. /* Undocumented stuff. */
  1585. if (tp->mac_version == RTL_GIGA_MAC_VER_05) {
  1586. /* Realtek's r1000_n.c driver uses '&& 0x01' here. Well... */
  1587. if ((RTL_R8(Config2) & 0x07) & 0x01)
  1588. RTL_W32(0x7c, 0x0007ffff);
  1589. RTL_W32(0x7c, 0x0007ff00);
  1590. pci_read_config_word(pdev, PCI_COMMAND, &cmd);
  1591. cmd = cmd & 0xef;
  1592. pci_write_config_word(pdev, PCI_COMMAND, cmd);
  1593. }
  1594. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1595. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  1596. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  1597. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  1598. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  1599. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1600. RTL_W8(EarlyTxThres, EarlyTxThld);
  1601. rtl_set_rx_max_size(ioaddr);
  1602. if ((tp->mac_version == RTL_GIGA_MAC_VER_01) ||
  1603. (tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  1604. (tp->mac_version == RTL_GIGA_MAC_VER_03) ||
  1605. (tp->mac_version == RTL_GIGA_MAC_VER_04))
  1606. rtl_set_rx_tx_config_registers(tp);
  1607. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  1608. if ((tp->mac_version == RTL_GIGA_MAC_VER_02) ||
  1609. (tp->mac_version == RTL_GIGA_MAC_VER_03)) {
  1610. dprintk(KERN_INFO PFX "Set MAC Reg C+CR Offset 0xE0. "
  1611. "Bit-3 and bit-14 MUST be 1\n");
  1612. tp->cp_cmd |= (1 << 14);
  1613. }
  1614. RTL_W16(CPlusCmd, tp->cp_cmd);
  1615. /*
  1616. * Undocumented corner. Supposedly:
  1617. * (TxTimer << 12) | (TxPackets << 8) | (RxTimer << 4) | RxPackets
  1618. */
  1619. RTL_W16(IntrMitigate, 0x0000);
  1620. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  1621. if ((tp->mac_version != RTL_GIGA_MAC_VER_01) &&
  1622. (tp->mac_version != RTL_GIGA_MAC_VER_02) &&
  1623. (tp->mac_version != RTL_GIGA_MAC_VER_03) &&
  1624. (tp->mac_version != RTL_GIGA_MAC_VER_04)) {
  1625. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1626. rtl_set_rx_tx_config_registers(tp);
  1627. }
  1628. RTL_W8(Cfg9346, Cfg9346_Lock);
  1629. /* Initially a 10 us delay. Turned it into a PCI commit. - FR */
  1630. RTL_R8(IntrMask);
  1631. RTL_W32(RxMissed, 0);
  1632. rtl_set_rx_mode(dev);
  1633. /* no early-rx interrupts */
  1634. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  1635. }
  1636. static void rtl_hw_start_8168(struct net_device *dev)
  1637. {
  1638. struct rtl8169_private *tp = netdev_priv(dev);
  1639. void __iomem *ioaddr = tp->mmio_addr;
  1640. RTL_W8(Cfg9346, Cfg9346_Unlock);
  1641. RTL_W8(EarlyTxThres, EarlyTxThld);
  1642. rtl_set_rx_max_size(ioaddr);
  1643. tp->cp_cmd |= rtl_rw_cpluscmd(ioaddr) | PCIMulRW;
  1644. RTL_W16(CPlusCmd, tp->cp_cmd);
  1645. RTL_W16(IntrMitigate, 0x0000);
  1646. rtl_set_rx_tx_desc_registers(tp, ioaddr);
  1647. RTL_W8(ChipCmd, CmdTxEnb | CmdRxEnb);
  1648. rtl_set_rx_tx_config_registers(tp);
  1649. RTL_W8(Cfg9346, Cfg9346_Lock);
  1650. RTL_R8(IntrMask);
  1651. RTL_W32(RxMissed, 0);
  1652. rtl_set_rx_mode(dev);
  1653. RTL_W16(MultiIntr, RTL_R16(MultiIntr) & 0xF000);
  1654. }
  1655. static void rtl_hw_start_8101(struct net_device *dev)
  1656. {
  1657. rtl_hw_start_8169(dev);
  1658. }
  1659. static int rtl8169_change_mtu(struct net_device *dev, int new_mtu)
  1660. {
  1661. struct rtl8169_private *tp = netdev_priv(dev);
  1662. int ret = 0;
  1663. if (new_mtu < ETH_ZLEN || new_mtu > SafeMtu)
  1664. return -EINVAL;
  1665. dev->mtu = new_mtu;
  1666. if (!netif_running(dev))
  1667. goto out;
  1668. rtl8169_down(dev);
  1669. rtl8169_set_rxbufsize(tp, dev);
  1670. ret = rtl8169_init_ring(dev);
  1671. if (ret < 0)
  1672. goto out;
  1673. netif_poll_enable(dev);
  1674. rtl_hw_start(dev);
  1675. rtl8169_request_timer(dev);
  1676. out:
  1677. return ret;
  1678. }
  1679. static inline void rtl8169_make_unusable_by_asic(struct RxDesc *desc)
  1680. {
  1681. desc->addr = 0x0badbadbadbadbadull;
  1682. desc->opts1 &= ~cpu_to_le32(DescOwn | RsvdMask);
  1683. }
  1684. static void rtl8169_free_rx_skb(struct rtl8169_private *tp,
  1685. struct sk_buff **sk_buff, struct RxDesc *desc)
  1686. {
  1687. struct pci_dev *pdev = tp->pci_dev;
  1688. pci_unmap_single(pdev, le64_to_cpu(desc->addr), tp->rx_buf_sz,
  1689. PCI_DMA_FROMDEVICE);
  1690. dev_kfree_skb(*sk_buff);
  1691. *sk_buff = NULL;
  1692. rtl8169_make_unusable_by_asic(desc);
  1693. }
  1694. static inline void rtl8169_mark_to_asic(struct RxDesc *desc, u32 rx_buf_sz)
  1695. {
  1696. u32 eor = le32_to_cpu(desc->opts1) & RingEnd;
  1697. desc->opts1 = cpu_to_le32(DescOwn | eor | rx_buf_sz);
  1698. }
  1699. static inline void rtl8169_map_to_asic(struct RxDesc *desc, dma_addr_t mapping,
  1700. u32 rx_buf_sz)
  1701. {
  1702. desc->addr = cpu_to_le64(mapping);
  1703. wmb();
  1704. rtl8169_mark_to_asic(desc, rx_buf_sz);
  1705. }
  1706. static struct sk_buff *rtl8169_alloc_rx_skb(struct pci_dev *pdev,
  1707. struct net_device *dev,
  1708. struct RxDesc *desc, int rx_buf_sz,
  1709. unsigned int align)
  1710. {
  1711. struct sk_buff *skb;
  1712. dma_addr_t mapping;
  1713. skb = netdev_alloc_skb(dev, rx_buf_sz + align);
  1714. if (!skb)
  1715. goto err_out;
  1716. skb_reserve(skb, (align - 1) & (unsigned long)skb->data);
  1717. mapping = pci_map_single(pdev, skb->data, rx_buf_sz,
  1718. PCI_DMA_FROMDEVICE);
  1719. rtl8169_map_to_asic(desc, mapping, rx_buf_sz);
  1720. out:
  1721. return skb;
  1722. err_out:
  1723. rtl8169_make_unusable_by_asic(desc);
  1724. goto out;
  1725. }
  1726. static void rtl8169_rx_clear(struct rtl8169_private *tp)
  1727. {
  1728. int i;
  1729. for (i = 0; i < NUM_RX_DESC; i++) {
  1730. if (tp->Rx_skbuff[i]) {
  1731. rtl8169_free_rx_skb(tp, tp->Rx_skbuff + i,
  1732. tp->RxDescArray + i);
  1733. }
  1734. }
  1735. }
  1736. static u32 rtl8169_rx_fill(struct rtl8169_private *tp, struct net_device *dev,
  1737. u32 start, u32 end)
  1738. {
  1739. u32 cur;
  1740. for (cur = start; end - cur != 0; cur++) {
  1741. struct sk_buff *skb;
  1742. unsigned int i = cur % NUM_RX_DESC;
  1743. WARN_ON((s32)(end - cur) < 0);
  1744. if (tp->Rx_skbuff[i])
  1745. continue;
  1746. skb = rtl8169_alloc_rx_skb(tp->pci_dev, dev,
  1747. tp->RxDescArray + i,
  1748. tp->rx_buf_sz, tp->align);
  1749. if (!skb)
  1750. break;
  1751. tp->Rx_skbuff[i] = skb;
  1752. }
  1753. return cur - start;
  1754. }
  1755. static inline void rtl8169_mark_as_last_descriptor(struct RxDesc *desc)
  1756. {
  1757. desc->opts1 |= cpu_to_le32(RingEnd);
  1758. }
  1759. static void rtl8169_init_ring_indexes(struct rtl8169_private *tp)
  1760. {
  1761. tp->dirty_tx = tp->dirty_rx = tp->cur_tx = tp->cur_rx = 0;
  1762. }
  1763. static int rtl8169_init_ring(struct net_device *dev)
  1764. {
  1765. struct rtl8169_private *tp = netdev_priv(dev);
  1766. rtl8169_init_ring_indexes(tp);
  1767. memset(tp->tx_skb, 0x0, NUM_TX_DESC * sizeof(struct ring_info));
  1768. memset(tp->Rx_skbuff, 0x0, NUM_RX_DESC * sizeof(struct sk_buff *));
  1769. if (rtl8169_rx_fill(tp, dev, 0, NUM_RX_DESC) != NUM_RX_DESC)
  1770. goto err_out;
  1771. rtl8169_mark_as_last_descriptor(tp->RxDescArray + NUM_RX_DESC - 1);
  1772. return 0;
  1773. err_out:
  1774. rtl8169_rx_clear(tp);
  1775. return -ENOMEM;
  1776. }
  1777. static void rtl8169_unmap_tx_skb(struct pci_dev *pdev, struct ring_info *tx_skb,
  1778. struct TxDesc *desc)
  1779. {
  1780. unsigned int len = tx_skb->len;
  1781. pci_unmap_single(pdev, le64_to_cpu(desc->addr), len, PCI_DMA_TODEVICE);
  1782. desc->opts1 = 0x00;
  1783. desc->opts2 = 0x00;
  1784. desc->addr = 0x00;
  1785. tx_skb->len = 0;
  1786. }
  1787. static void rtl8169_tx_clear(struct rtl8169_private *tp)
  1788. {
  1789. unsigned int i;
  1790. for (i = tp->dirty_tx; i < tp->dirty_tx + NUM_TX_DESC; i++) {
  1791. unsigned int entry = i % NUM_TX_DESC;
  1792. struct ring_info *tx_skb = tp->tx_skb + entry;
  1793. unsigned int len = tx_skb->len;
  1794. if (len) {
  1795. struct sk_buff *skb = tx_skb->skb;
  1796. rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb,
  1797. tp->TxDescArray + entry);
  1798. if (skb) {
  1799. dev_kfree_skb(skb);
  1800. tx_skb->skb = NULL;
  1801. }
  1802. tp->stats.tx_dropped++;
  1803. }
  1804. }
  1805. tp->cur_tx = tp->dirty_tx = 0;
  1806. }
  1807. static void rtl8169_schedule_work(struct net_device *dev, work_func_t task)
  1808. {
  1809. struct rtl8169_private *tp = netdev_priv(dev);
  1810. PREPARE_DELAYED_WORK(&tp->task, task);
  1811. schedule_delayed_work(&tp->task, 4);
  1812. }
  1813. static void rtl8169_wait_for_quiescence(struct net_device *dev)
  1814. {
  1815. struct rtl8169_private *tp = netdev_priv(dev);
  1816. void __iomem *ioaddr = tp->mmio_addr;
  1817. synchronize_irq(dev->irq);
  1818. /* Wait for any pending NAPI task to complete */
  1819. netif_poll_disable(dev);
  1820. rtl8169_irq_mask_and_ack(ioaddr);
  1821. netif_poll_enable(dev);
  1822. }
  1823. static void rtl8169_reinit_task(struct work_struct *work)
  1824. {
  1825. struct rtl8169_private *tp =
  1826. container_of(work, struct rtl8169_private, task.work);
  1827. struct net_device *dev = tp->dev;
  1828. int ret;
  1829. rtnl_lock();
  1830. if (!netif_running(dev))
  1831. goto out_unlock;
  1832. rtl8169_wait_for_quiescence(dev);
  1833. rtl8169_close(dev);
  1834. ret = rtl8169_open(dev);
  1835. if (unlikely(ret < 0)) {
  1836. if (net_ratelimit()) {
  1837. struct rtl8169_private *tp = netdev_priv(dev);
  1838. if (netif_msg_drv(tp)) {
  1839. printk(PFX KERN_ERR
  1840. "%s: reinit failure (status = %d)."
  1841. " Rescheduling.\n", dev->name, ret);
  1842. }
  1843. }
  1844. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  1845. }
  1846. out_unlock:
  1847. rtnl_unlock();
  1848. }
  1849. static void rtl8169_reset_task(struct work_struct *work)
  1850. {
  1851. struct rtl8169_private *tp =
  1852. container_of(work, struct rtl8169_private, task.work);
  1853. struct net_device *dev = tp->dev;
  1854. rtnl_lock();
  1855. if (!netif_running(dev))
  1856. goto out_unlock;
  1857. rtl8169_wait_for_quiescence(dev);
  1858. rtl8169_rx_interrupt(dev, tp, tp->mmio_addr);
  1859. rtl8169_tx_clear(tp);
  1860. if (tp->dirty_rx == tp->cur_rx) {
  1861. rtl8169_init_ring_indexes(tp);
  1862. rtl_hw_start(dev);
  1863. netif_wake_queue(dev);
  1864. } else {
  1865. if (net_ratelimit()) {
  1866. struct rtl8169_private *tp = netdev_priv(dev);
  1867. if (netif_msg_intr(tp)) {
  1868. printk(PFX KERN_EMERG
  1869. "%s: Rx buffers shortage\n", dev->name);
  1870. }
  1871. }
  1872. rtl8169_schedule_work(dev, rtl8169_reset_task);
  1873. }
  1874. out_unlock:
  1875. rtnl_unlock();
  1876. }
  1877. static void rtl8169_tx_timeout(struct net_device *dev)
  1878. {
  1879. struct rtl8169_private *tp = netdev_priv(dev);
  1880. rtl8169_hw_reset(tp->mmio_addr);
  1881. /* Let's wait a bit while any (async) irq lands on */
  1882. rtl8169_schedule_work(dev, rtl8169_reset_task);
  1883. }
  1884. static int rtl8169_xmit_frags(struct rtl8169_private *tp, struct sk_buff *skb,
  1885. u32 opts1)
  1886. {
  1887. struct skb_shared_info *info = skb_shinfo(skb);
  1888. unsigned int cur_frag, entry;
  1889. struct TxDesc *txd;
  1890. entry = tp->cur_tx;
  1891. for (cur_frag = 0; cur_frag < info->nr_frags; cur_frag++) {
  1892. skb_frag_t *frag = info->frags + cur_frag;
  1893. dma_addr_t mapping;
  1894. u32 status, len;
  1895. void *addr;
  1896. entry = (entry + 1) % NUM_TX_DESC;
  1897. txd = tp->TxDescArray + entry;
  1898. len = frag->size;
  1899. addr = ((void *) page_address(frag->page)) + frag->page_offset;
  1900. mapping = pci_map_single(tp->pci_dev, addr, len, PCI_DMA_TODEVICE);
  1901. /* anti gcc 2.95.3 bugware (sic) */
  1902. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  1903. txd->opts1 = cpu_to_le32(status);
  1904. txd->addr = cpu_to_le64(mapping);
  1905. tp->tx_skb[entry].len = len;
  1906. }
  1907. if (cur_frag) {
  1908. tp->tx_skb[entry].skb = skb;
  1909. txd->opts1 |= cpu_to_le32(LastFrag);
  1910. }
  1911. return cur_frag;
  1912. }
  1913. static inline u32 rtl8169_tso_csum(struct sk_buff *skb, struct net_device *dev)
  1914. {
  1915. if (dev->features & NETIF_F_TSO) {
  1916. u32 mss = skb_shinfo(skb)->gso_size;
  1917. if (mss)
  1918. return LargeSend | ((mss & MSSMask) << MSSShift);
  1919. }
  1920. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  1921. const struct iphdr *ip = ip_hdr(skb);
  1922. if (ip->protocol == IPPROTO_TCP)
  1923. return IPCS | TCPCS;
  1924. else if (ip->protocol == IPPROTO_UDP)
  1925. return IPCS | UDPCS;
  1926. WARN_ON(1); /* we need a WARN() */
  1927. }
  1928. return 0;
  1929. }
  1930. static int rtl8169_start_xmit(struct sk_buff *skb, struct net_device *dev)
  1931. {
  1932. struct rtl8169_private *tp = netdev_priv(dev);
  1933. unsigned int frags, entry = tp->cur_tx % NUM_TX_DESC;
  1934. struct TxDesc *txd = tp->TxDescArray + entry;
  1935. void __iomem *ioaddr = tp->mmio_addr;
  1936. dma_addr_t mapping;
  1937. u32 status, len;
  1938. u32 opts1;
  1939. int ret = NETDEV_TX_OK;
  1940. if (unlikely(TX_BUFFS_AVAIL(tp) < skb_shinfo(skb)->nr_frags)) {
  1941. if (netif_msg_drv(tp)) {
  1942. printk(KERN_ERR
  1943. "%s: BUG! Tx Ring full when queue awake!\n",
  1944. dev->name);
  1945. }
  1946. goto err_stop;
  1947. }
  1948. if (unlikely(le32_to_cpu(txd->opts1) & DescOwn))
  1949. goto err_stop;
  1950. opts1 = DescOwn | rtl8169_tso_csum(skb, dev);
  1951. frags = rtl8169_xmit_frags(tp, skb, opts1);
  1952. if (frags) {
  1953. len = skb_headlen(skb);
  1954. opts1 |= FirstFrag;
  1955. } else {
  1956. len = skb->len;
  1957. if (unlikely(len < ETH_ZLEN)) {
  1958. if (skb_padto(skb, ETH_ZLEN))
  1959. goto err_update_stats;
  1960. len = ETH_ZLEN;
  1961. }
  1962. opts1 |= FirstFrag | LastFrag;
  1963. tp->tx_skb[entry].skb = skb;
  1964. }
  1965. mapping = pci_map_single(tp->pci_dev, skb->data, len, PCI_DMA_TODEVICE);
  1966. tp->tx_skb[entry].len = len;
  1967. txd->addr = cpu_to_le64(mapping);
  1968. txd->opts2 = cpu_to_le32(rtl8169_tx_vlan_tag(tp, skb));
  1969. wmb();
  1970. /* anti gcc 2.95.3 bugware (sic) */
  1971. status = opts1 | len | (RingEnd * !((entry + 1) % NUM_TX_DESC));
  1972. txd->opts1 = cpu_to_le32(status);
  1973. dev->trans_start = jiffies;
  1974. tp->cur_tx += frags + 1;
  1975. smp_wmb();
  1976. RTL_W8(TxPoll, 0x40); /* set polling bit */
  1977. if (TX_BUFFS_AVAIL(tp) < MAX_SKB_FRAGS) {
  1978. netif_stop_queue(dev);
  1979. smp_rmb();
  1980. if (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)
  1981. netif_wake_queue(dev);
  1982. }
  1983. out:
  1984. return ret;
  1985. err_stop:
  1986. netif_stop_queue(dev);
  1987. ret = NETDEV_TX_BUSY;
  1988. err_update_stats:
  1989. tp->stats.tx_dropped++;
  1990. goto out;
  1991. }
  1992. static void rtl8169_pcierr_interrupt(struct net_device *dev)
  1993. {
  1994. struct rtl8169_private *tp = netdev_priv(dev);
  1995. struct pci_dev *pdev = tp->pci_dev;
  1996. void __iomem *ioaddr = tp->mmio_addr;
  1997. u16 pci_status, pci_cmd;
  1998. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  1999. pci_read_config_word(pdev, PCI_STATUS, &pci_status);
  2000. if (netif_msg_intr(tp)) {
  2001. printk(KERN_ERR
  2002. "%s: PCI error (cmd = 0x%04x, status = 0x%04x).\n",
  2003. dev->name, pci_cmd, pci_status);
  2004. }
  2005. /*
  2006. * The recovery sequence below admits a very elaborated explanation:
  2007. * - it seems to work;
  2008. * - I did not see what else could be done;
  2009. * - it makes iop3xx happy.
  2010. *
  2011. * Feel free to adjust to your needs.
  2012. */
  2013. if (pdev->broken_parity_status)
  2014. pci_cmd &= ~PCI_COMMAND_PARITY;
  2015. else
  2016. pci_cmd |= PCI_COMMAND_SERR | PCI_COMMAND_PARITY;
  2017. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  2018. pci_write_config_word(pdev, PCI_STATUS,
  2019. pci_status & (PCI_STATUS_DETECTED_PARITY |
  2020. PCI_STATUS_SIG_SYSTEM_ERROR | PCI_STATUS_REC_MASTER_ABORT |
  2021. PCI_STATUS_REC_TARGET_ABORT | PCI_STATUS_SIG_TARGET_ABORT));
  2022. /* The infamous DAC f*ckup only happens at boot time */
  2023. if ((tp->cp_cmd & PCIDAC) && !tp->dirty_rx && !tp->cur_rx) {
  2024. if (netif_msg_intr(tp))
  2025. printk(KERN_INFO "%s: disabling PCI DAC.\n", dev->name);
  2026. tp->cp_cmd &= ~PCIDAC;
  2027. RTL_W16(CPlusCmd, tp->cp_cmd);
  2028. dev->features &= ~NETIF_F_HIGHDMA;
  2029. }
  2030. rtl8169_hw_reset(ioaddr);
  2031. rtl8169_schedule_work(dev, rtl8169_reinit_task);
  2032. }
  2033. static void
  2034. rtl8169_tx_interrupt(struct net_device *dev, struct rtl8169_private *tp,
  2035. void __iomem *ioaddr)
  2036. {
  2037. unsigned int dirty_tx, tx_left;
  2038. assert(dev != NULL);
  2039. assert(tp != NULL);
  2040. assert(ioaddr != NULL);
  2041. dirty_tx = tp->dirty_tx;
  2042. smp_rmb();
  2043. tx_left = tp->cur_tx - dirty_tx;
  2044. while (tx_left > 0) {
  2045. unsigned int entry = dirty_tx % NUM_TX_DESC;
  2046. struct ring_info *tx_skb = tp->tx_skb + entry;
  2047. u32 len = tx_skb->len;
  2048. u32 status;
  2049. rmb();
  2050. status = le32_to_cpu(tp->TxDescArray[entry].opts1);
  2051. if (status & DescOwn)
  2052. break;
  2053. tp->stats.tx_bytes += len;
  2054. tp->stats.tx_packets++;
  2055. rtl8169_unmap_tx_skb(tp->pci_dev, tx_skb, tp->TxDescArray + entry);
  2056. if (status & LastFrag) {
  2057. dev_kfree_skb_irq(tx_skb->skb);
  2058. tx_skb->skb = NULL;
  2059. }
  2060. dirty_tx++;
  2061. tx_left--;
  2062. }
  2063. if (tp->dirty_tx != dirty_tx) {
  2064. tp->dirty_tx = dirty_tx;
  2065. smp_wmb();
  2066. if (netif_queue_stopped(dev) &&
  2067. (TX_BUFFS_AVAIL(tp) >= MAX_SKB_FRAGS)) {
  2068. netif_wake_queue(dev);
  2069. }
  2070. }
  2071. }
  2072. static inline int rtl8169_fragmented_frame(u32 status)
  2073. {
  2074. return (status & (FirstFrag | LastFrag)) != (FirstFrag | LastFrag);
  2075. }
  2076. static inline void rtl8169_rx_csum(struct sk_buff *skb, struct RxDesc *desc)
  2077. {
  2078. u32 opts1 = le32_to_cpu(desc->opts1);
  2079. u32 status = opts1 & RxProtoMask;
  2080. if (((status == RxProtoTCP) && !(opts1 & TCPFail)) ||
  2081. ((status == RxProtoUDP) && !(opts1 & UDPFail)) ||
  2082. ((status == RxProtoIP) && !(opts1 & IPFail)))
  2083. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2084. else
  2085. skb->ip_summed = CHECKSUM_NONE;
  2086. }
  2087. static inline bool rtl8169_try_rx_copy(struct sk_buff **sk_buff, int pkt_size,
  2088. struct pci_dev *pdev, dma_addr_t addr,
  2089. unsigned int align)
  2090. {
  2091. struct sk_buff *skb;
  2092. bool done = false;
  2093. if (pkt_size >= rx_copybreak)
  2094. goto out;
  2095. skb = dev_alloc_skb(pkt_size + align);
  2096. if (!skb)
  2097. goto out;
  2098. pci_dma_sync_single_for_cpu(pdev, addr, pkt_size, PCI_DMA_FROMDEVICE);
  2099. skb_reserve(skb, (align - 1) & (unsigned long)skb->data);
  2100. skb_copy_from_linear_data(*sk_buff, skb->data, pkt_size);
  2101. *sk_buff = skb;
  2102. done = true;
  2103. out:
  2104. return done;
  2105. }
  2106. static int
  2107. rtl8169_rx_interrupt(struct net_device *dev, struct rtl8169_private *tp,
  2108. void __iomem *ioaddr)
  2109. {
  2110. unsigned int cur_rx, rx_left;
  2111. unsigned int delta, count;
  2112. assert(dev != NULL);
  2113. assert(tp != NULL);
  2114. assert(ioaddr != NULL);
  2115. cur_rx = tp->cur_rx;
  2116. rx_left = NUM_RX_DESC + tp->dirty_rx - cur_rx;
  2117. rx_left = rtl8169_rx_quota(rx_left, (u32) dev->quota);
  2118. for (; rx_left > 0; rx_left--, cur_rx++) {
  2119. unsigned int entry = cur_rx % NUM_RX_DESC;
  2120. struct RxDesc *desc = tp->RxDescArray + entry;
  2121. u32 status;
  2122. rmb();
  2123. status = le32_to_cpu(desc->opts1);
  2124. if (status & DescOwn)
  2125. break;
  2126. if (unlikely(status & RxRES)) {
  2127. if (netif_msg_rx_err(tp)) {
  2128. printk(KERN_INFO
  2129. "%s: Rx ERROR. status = %08x\n",
  2130. dev->name, status);
  2131. }
  2132. tp->stats.rx_errors++;
  2133. if (status & (RxRWT | RxRUNT))
  2134. tp->stats.rx_length_errors++;
  2135. if (status & RxCRC)
  2136. tp->stats.rx_crc_errors++;
  2137. if (status & RxFOVF) {
  2138. rtl8169_schedule_work(dev, rtl8169_reset_task);
  2139. tp->stats.rx_fifo_errors++;
  2140. }
  2141. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2142. } else {
  2143. struct sk_buff *skb = tp->Rx_skbuff[entry];
  2144. dma_addr_t addr = le64_to_cpu(desc->addr);
  2145. int pkt_size = (status & 0x00001FFF) - 4;
  2146. struct pci_dev *pdev = tp->pci_dev;
  2147. /*
  2148. * The driver does not support incoming fragmented
  2149. * frames. They are seen as a symptom of over-mtu
  2150. * sized frames.
  2151. */
  2152. if (unlikely(rtl8169_fragmented_frame(status))) {
  2153. tp->stats.rx_dropped++;
  2154. tp->stats.rx_length_errors++;
  2155. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2156. continue;
  2157. }
  2158. rtl8169_rx_csum(skb, desc);
  2159. if (rtl8169_try_rx_copy(&skb, pkt_size, pdev, addr,
  2160. tp->align)) {
  2161. pci_dma_sync_single_for_device(pdev, addr,
  2162. pkt_size, PCI_DMA_FROMDEVICE);
  2163. rtl8169_mark_to_asic(desc, tp->rx_buf_sz);
  2164. } else {
  2165. pci_unmap_single(pdev, addr, pkt_size,
  2166. PCI_DMA_FROMDEVICE);
  2167. tp->Rx_skbuff[entry] = NULL;
  2168. }
  2169. skb_put(skb, pkt_size);
  2170. skb->protocol = eth_type_trans(skb, dev);
  2171. if (rtl8169_rx_vlan_skb(tp, desc, skb) < 0)
  2172. rtl8169_rx_skb(skb);
  2173. dev->last_rx = jiffies;
  2174. tp->stats.rx_bytes += pkt_size;
  2175. tp->stats.rx_packets++;
  2176. }
  2177. }
  2178. count = cur_rx - tp->cur_rx;
  2179. tp->cur_rx = cur_rx;
  2180. delta = rtl8169_rx_fill(tp, dev, tp->dirty_rx, tp->cur_rx);
  2181. if (!delta && count && netif_msg_intr(tp))
  2182. printk(KERN_INFO "%s: no Rx buffer allocated\n", dev->name);
  2183. tp->dirty_rx += delta;
  2184. /*
  2185. * FIXME: until there is periodic timer to try and refill the ring,
  2186. * a temporary shortage may definitely kill the Rx process.
  2187. * - disable the asic to try and avoid an overflow and kick it again
  2188. * after refill ?
  2189. * - how do others driver handle this condition (Uh oh...).
  2190. */
  2191. if ((tp->dirty_rx + NUM_RX_DESC == tp->cur_rx) && netif_msg_intr(tp))
  2192. printk(KERN_EMERG "%s: Rx buffers exhausted\n", dev->name);
  2193. return count;
  2194. }
  2195. /* The interrupt handler does all of the Rx thread work and cleans up after the Tx thread. */
  2196. static irqreturn_t
  2197. rtl8169_interrupt(int irq, void *dev_instance)
  2198. {
  2199. struct net_device *dev = (struct net_device *) dev_instance;
  2200. struct rtl8169_private *tp = netdev_priv(dev);
  2201. int boguscnt = max_interrupt_work;
  2202. void __iomem *ioaddr = tp->mmio_addr;
  2203. int status;
  2204. int handled = 0;
  2205. do {
  2206. status = RTL_R16(IntrStatus);
  2207. /* hotplug/major error/no more work/shared irq */
  2208. if ((status == 0xFFFF) || !status)
  2209. break;
  2210. handled = 1;
  2211. if (unlikely(!netif_running(dev))) {
  2212. rtl8169_asic_down(ioaddr);
  2213. goto out;
  2214. }
  2215. status &= tp->intr_mask;
  2216. RTL_W16(IntrStatus,
  2217. (status & RxFIFOOver) ? (status | RxOverflow) : status);
  2218. if (!(status & rtl8169_intr_mask))
  2219. break;
  2220. if (unlikely(status & SYSErr)) {
  2221. rtl8169_pcierr_interrupt(dev);
  2222. break;
  2223. }
  2224. if (status & LinkChg)
  2225. rtl8169_check_link_status(dev, tp, ioaddr);
  2226. #ifdef CONFIG_R8169_NAPI
  2227. RTL_W16(IntrMask, rtl8169_intr_mask & ~rtl8169_napi_event);
  2228. tp->intr_mask = ~rtl8169_napi_event;
  2229. if (likely(netif_rx_schedule_prep(dev)))
  2230. __netif_rx_schedule(dev);
  2231. else if (netif_msg_intr(tp)) {
  2232. printk(KERN_INFO "%s: interrupt %04x taken in poll\n",
  2233. dev->name, status);
  2234. }
  2235. break;
  2236. #else
  2237. /* Rx interrupt */
  2238. if (status & (RxOK | RxOverflow | RxFIFOOver)) {
  2239. rtl8169_rx_interrupt(dev, tp, ioaddr);
  2240. }
  2241. /* Tx interrupt */
  2242. if (status & (TxOK | TxErr))
  2243. rtl8169_tx_interrupt(dev, tp, ioaddr);
  2244. #endif
  2245. boguscnt--;
  2246. } while (boguscnt > 0);
  2247. if (boguscnt <= 0) {
  2248. if (netif_msg_intr(tp) && net_ratelimit() ) {
  2249. printk(KERN_WARNING
  2250. "%s: Too much work at interrupt!\n", dev->name);
  2251. }
  2252. /* Clear all interrupt sources. */
  2253. RTL_W16(IntrStatus, 0xffff);
  2254. }
  2255. out:
  2256. return IRQ_RETVAL(handled);
  2257. }
  2258. #ifdef CONFIG_R8169_NAPI
  2259. static int rtl8169_poll(struct net_device *dev, int *budget)
  2260. {
  2261. unsigned int work_done, work_to_do = min(*budget, dev->quota);
  2262. struct rtl8169_private *tp = netdev_priv(dev);
  2263. void __iomem *ioaddr = tp->mmio_addr;
  2264. work_done = rtl8169_rx_interrupt(dev, tp, ioaddr);
  2265. rtl8169_tx_interrupt(dev, tp, ioaddr);
  2266. *budget -= work_done;
  2267. dev->quota -= work_done;
  2268. if (work_done < work_to_do) {
  2269. netif_rx_complete(dev);
  2270. tp->intr_mask = 0xffff;
  2271. /*
  2272. * 20040426: the barrier is not strictly required but the
  2273. * behavior of the irq handler could be less predictable
  2274. * without it. Btw, the lack of flush for the posted pci
  2275. * write is safe - FR
  2276. */
  2277. smp_wmb();
  2278. RTL_W16(IntrMask, rtl8169_intr_mask);
  2279. }
  2280. return (work_done >= work_to_do);
  2281. }
  2282. #endif
  2283. static void rtl8169_down(struct net_device *dev)
  2284. {
  2285. struct rtl8169_private *tp = netdev_priv(dev);
  2286. void __iomem *ioaddr = tp->mmio_addr;
  2287. unsigned int poll_locked = 0;
  2288. unsigned int intrmask;
  2289. rtl8169_delete_timer(dev);
  2290. netif_stop_queue(dev);
  2291. core_down:
  2292. spin_lock_irq(&tp->lock);
  2293. rtl8169_asic_down(ioaddr);
  2294. /* Update the error counts. */
  2295. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  2296. RTL_W32(RxMissed, 0);
  2297. spin_unlock_irq(&tp->lock);
  2298. synchronize_irq(dev->irq);
  2299. if (!poll_locked) {
  2300. netif_poll_disable(dev);
  2301. poll_locked++;
  2302. }
  2303. /* Give a racing hard_start_xmit a few cycles to complete. */
  2304. synchronize_sched(); /* FIXME: should this be synchronize_irq()? */
  2305. /*
  2306. * And now for the 50k$ question: are IRQ disabled or not ?
  2307. *
  2308. * Two paths lead here:
  2309. * 1) dev->close
  2310. * -> netif_running() is available to sync the current code and the
  2311. * IRQ handler. See rtl8169_interrupt for details.
  2312. * 2) dev->change_mtu
  2313. * -> rtl8169_poll can not be issued again and re-enable the
  2314. * interruptions. Let's simply issue the IRQ down sequence again.
  2315. *
  2316. * No loop if hotpluged or major error (0xffff).
  2317. */
  2318. intrmask = RTL_R16(IntrMask);
  2319. if (intrmask && (intrmask != 0xffff))
  2320. goto core_down;
  2321. rtl8169_tx_clear(tp);
  2322. rtl8169_rx_clear(tp);
  2323. }
  2324. static int rtl8169_close(struct net_device *dev)
  2325. {
  2326. struct rtl8169_private *tp = netdev_priv(dev);
  2327. struct pci_dev *pdev = tp->pci_dev;
  2328. rtl8169_down(dev);
  2329. free_irq(dev->irq, dev);
  2330. netif_poll_enable(dev);
  2331. pci_free_consistent(pdev, R8169_RX_RING_BYTES, tp->RxDescArray,
  2332. tp->RxPhyAddr);
  2333. pci_free_consistent(pdev, R8169_TX_RING_BYTES, tp->TxDescArray,
  2334. tp->TxPhyAddr);
  2335. tp->TxDescArray = NULL;
  2336. tp->RxDescArray = NULL;
  2337. return 0;
  2338. }
  2339. static void rtl_set_rx_mode(struct net_device *dev)
  2340. {
  2341. struct rtl8169_private *tp = netdev_priv(dev);
  2342. void __iomem *ioaddr = tp->mmio_addr;
  2343. unsigned long flags;
  2344. u32 mc_filter[2]; /* Multicast hash filter */
  2345. int i, rx_mode;
  2346. u32 tmp = 0;
  2347. if (dev->flags & IFF_PROMISC) {
  2348. /* Unconditionally log net taps. */
  2349. if (netif_msg_link(tp)) {
  2350. printk(KERN_NOTICE "%s: Promiscuous mode enabled.\n",
  2351. dev->name);
  2352. }
  2353. rx_mode =
  2354. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  2355. AcceptAllPhys;
  2356. mc_filter[1] = mc_filter[0] = 0xffffffff;
  2357. } else if ((dev->mc_count > multicast_filter_limit)
  2358. || (dev->flags & IFF_ALLMULTI)) {
  2359. /* Too many to filter perfectly -- accept all multicasts. */
  2360. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  2361. mc_filter[1] = mc_filter[0] = 0xffffffff;
  2362. } else {
  2363. struct dev_mc_list *mclist;
  2364. rx_mode = AcceptBroadcast | AcceptMyPhys;
  2365. mc_filter[1] = mc_filter[0] = 0;
  2366. for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
  2367. i++, mclist = mclist->next) {
  2368. int bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
  2369. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  2370. rx_mode |= AcceptMulticast;
  2371. }
  2372. }
  2373. spin_lock_irqsave(&tp->lock, flags);
  2374. tmp = rtl8169_rx_config | rx_mode |
  2375. (RTL_R32(RxConfig) & rtl_chip_info[tp->chipset].RxConfigMask);
  2376. if ((tp->mac_version == RTL_GIGA_MAC_VER_11) ||
  2377. (tp->mac_version == RTL_GIGA_MAC_VER_12) ||
  2378. (tp->mac_version == RTL_GIGA_MAC_VER_13) ||
  2379. (tp->mac_version == RTL_GIGA_MAC_VER_14) ||
  2380. (tp->mac_version == RTL_GIGA_MAC_VER_15)) {
  2381. mc_filter[0] = 0xffffffff;
  2382. mc_filter[1] = 0xffffffff;
  2383. }
  2384. RTL_W32(RxConfig, tmp);
  2385. RTL_W32(MAR0 + 0, mc_filter[0]);
  2386. RTL_W32(MAR0 + 4, mc_filter[1]);
  2387. spin_unlock_irqrestore(&tp->lock, flags);
  2388. }
  2389. /**
  2390. * rtl8169_get_stats - Get rtl8169 read/write statistics
  2391. * @dev: The Ethernet Device to get statistics for
  2392. *
  2393. * Get TX/RX statistics for rtl8169
  2394. */
  2395. static struct net_device_stats *rtl8169_get_stats(struct net_device *dev)
  2396. {
  2397. struct rtl8169_private *tp = netdev_priv(dev);
  2398. void __iomem *ioaddr = tp->mmio_addr;
  2399. unsigned long flags;
  2400. if (netif_running(dev)) {
  2401. spin_lock_irqsave(&tp->lock, flags);
  2402. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  2403. RTL_W32(RxMissed, 0);
  2404. spin_unlock_irqrestore(&tp->lock, flags);
  2405. }
  2406. return &tp->stats;
  2407. }
  2408. #ifdef CONFIG_PM
  2409. static int rtl8169_suspend(struct pci_dev *pdev, pm_message_t state)
  2410. {
  2411. struct net_device *dev = pci_get_drvdata(pdev);
  2412. struct rtl8169_private *tp = netdev_priv(dev);
  2413. void __iomem *ioaddr = tp->mmio_addr;
  2414. if (!netif_running(dev))
  2415. goto out_pci_suspend;
  2416. netif_device_detach(dev);
  2417. netif_stop_queue(dev);
  2418. spin_lock_irq(&tp->lock);
  2419. rtl8169_asic_down(ioaddr);
  2420. tp->stats.rx_missed_errors += RTL_R32(RxMissed);
  2421. RTL_W32(RxMissed, 0);
  2422. spin_unlock_irq(&tp->lock);
  2423. out_pci_suspend:
  2424. pci_save_state(pdev);
  2425. pci_enable_wake(pdev, pci_choose_state(pdev, state), tp->wol_enabled);
  2426. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  2427. return 0;
  2428. }
  2429. static int rtl8169_resume(struct pci_dev *pdev)
  2430. {
  2431. struct net_device *dev = pci_get_drvdata(pdev);
  2432. pci_set_power_state(pdev, PCI_D0);
  2433. pci_restore_state(pdev);
  2434. pci_enable_wake(pdev, PCI_D0, 0);
  2435. if (!netif_running(dev))
  2436. goto out;
  2437. netif_device_attach(dev);
  2438. rtl8169_schedule_work(dev, rtl8169_reset_task);
  2439. out:
  2440. return 0;
  2441. }
  2442. #endif /* CONFIG_PM */
  2443. static struct pci_driver rtl8169_pci_driver = {
  2444. .name = MODULENAME,
  2445. .id_table = rtl8169_pci_tbl,
  2446. .probe = rtl8169_init_one,
  2447. .remove = __devexit_p(rtl8169_remove_one),
  2448. #ifdef CONFIG_PM
  2449. .suspend = rtl8169_suspend,
  2450. .resume = rtl8169_resume,
  2451. #endif
  2452. };
  2453. static int __init
  2454. rtl8169_init_module(void)
  2455. {
  2456. return pci_register_driver(&rtl8169_pci_driver);
  2457. }
  2458. static void __exit
  2459. rtl8169_cleanup_module(void)
  2460. {
  2461. pci_unregister_driver(&rtl8169_pci_driver);
  2462. }
  2463. module_init(rtl8169_init_module);
  2464. module_exit(rtl8169_cleanup_module);