r8169.c 74 KB

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