r8169.c 78 KB

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