8139too.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622
  1. /*
  2. 8139too.c: A RealTek RTL-8139 Fast Ethernet driver for Linux.
  3. Maintained by Jeff Garzik <jgarzik@pobox.com>
  4. Copyright 2000-2002 Jeff Garzik
  5. Much code comes from Donald Becker's rtl8139.c driver,
  6. versions 1.13 and older. This driver was originally based
  7. on rtl8139.c version 1.07. Header of rtl8139.c version 1.13:
  8. -----<snip>-----
  9. Written 1997-2001 by Donald Becker.
  10. This software may be used and distributed according to the
  11. terms of the GNU General Public License (GPL), incorporated
  12. herein by reference. Drivers based on or derived from this
  13. code fall under the GPL and must retain the authorship,
  14. copyright and license notice. This file is not a complete
  15. program and may only be used when the entire operating
  16. system is licensed under the GPL.
  17. This driver is for boards based on the RTL8129 and RTL8139
  18. PCI ethernet chips.
  19. The author may be reached as becker@scyld.com, or C/O Scyld
  20. Computing Corporation 410 Severn Ave., Suite 210 Annapolis
  21. MD 21403
  22. Support and updates available at
  23. http://www.scyld.com/network/rtl8139.html
  24. Twister-tuning table provided by Kinston
  25. <shangh@realtek.com.tw>.
  26. -----<snip>-----
  27. This software may be used and distributed according to the terms
  28. of the GNU General Public License, incorporated herein by reference.
  29. Contributors:
  30. Donald Becker - he wrote the original driver, kudos to him!
  31. (but please don't e-mail him for support, this isn't his driver)
  32. Tigran Aivazian - bug fixes, skbuff free cleanup
  33. Martin Mares - suggestions for PCI cleanup
  34. David S. Miller - PCI DMA and softnet updates
  35. Ernst Gill - fixes ported from BSD driver
  36. Daniel Kobras - identified specific locations of
  37. posted MMIO write bugginess
  38. Gerard Sharp - bug fix, testing and feedback
  39. David Ford - Rx ring wrap fix
  40. Dan DeMaggio - swapped RTL8139 cards with me, and allowed me
  41. to find and fix a crucial bug on older chipsets.
  42. Donald Becker/Chris Butterworth/Marcus Westergren -
  43. Noticed various Rx packet size-related buglets.
  44. Santiago Garcia Mantinan - testing and feedback
  45. Jens David - 2.2.x kernel backports
  46. Martin Dennett - incredibly helpful insight on undocumented
  47. features of the 8139 chips
  48. Jean-Jacques Michel - bug fix
  49. Tobias Ringström - Rx interrupt status checking suggestion
  50. Andrew Morton - Clear blocked signals, avoid
  51. buffer overrun setting current->comm.
  52. Kalle Olavi Niemitalo - Wake-on-LAN ioctls
  53. Robert Kuebel - Save kernel thread from dying on any signal.
  54. Submitting bug reports:
  55. "rtl8139-diag -mmmaaavvveefN" output
  56. enable RTL8139_DEBUG below, and look at 'dmesg' or kernel log
  57. */
  58. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  59. #define DRV_NAME "8139too"
  60. #define DRV_VERSION "0.9.28"
  61. #include <linux/module.h>
  62. #include <linux/kernel.h>
  63. #include <linux/compiler.h>
  64. #include <linux/pci.h>
  65. #include <linux/init.h>
  66. #include <linux/netdevice.h>
  67. #include <linux/etherdevice.h>
  68. #include <linux/rtnetlink.h>
  69. #include <linux/delay.h>
  70. #include <linux/ethtool.h>
  71. #include <linux/mii.h>
  72. #include <linux/completion.h>
  73. #include <linux/crc32.h>
  74. #include <linux/io.h>
  75. #include <linux/uaccess.h>
  76. #include <linux/gfp.h>
  77. #include <asm/irq.h>
  78. #define RTL8139_DRIVER_NAME DRV_NAME " Fast Ethernet driver " DRV_VERSION
  79. /* Default Message level */
  80. #define RTL8139_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
  81. NETIF_MSG_PROBE | \
  82. NETIF_MSG_LINK)
  83. /* define to 1, 2 or 3 to enable copious debugging info */
  84. #define RTL8139_DEBUG 0
  85. /* define to 1 to disable lightweight runtime debugging checks */
  86. #undef RTL8139_NDEBUG
  87. #ifdef RTL8139_NDEBUG
  88. # define assert(expr) do {} while (0)
  89. #else
  90. # define assert(expr) \
  91. if (unlikely(!(expr))) { \
  92. pr_err("Assertion failed! %s,%s,%s,line=%d\n", \
  93. #expr, __FILE__, __func__, __LINE__); \
  94. }
  95. #endif
  96. /* A few user-configurable values. */
  97. /* media options */
  98. #define MAX_UNITS 8
  99. static int media[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  100. static int full_duplex[MAX_UNITS] = {-1, -1, -1, -1, -1, -1, -1, -1};
  101. /* Whether to use MMIO or PIO. Default to MMIO. */
  102. #ifdef CONFIG_8139TOO_PIO
  103. static int use_io = 1;
  104. #else
  105. static int use_io = 0;
  106. #endif
  107. /* Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
  108. The RTL chips use a 64 element hash table based on the Ethernet CRC. */
  109. static int multicast_filter_limit = 32;
  110. /* bitmapped message enable number */
  111. static int debug = -1;
  112. /*
  113. * Receive ring size
  114. * Warning: 64K ring has hardware issues and may lock up.
  115. */
  116. #if defined(CONFIG_SH_DREAMCAST)
  117. #define RX_BUF_IDX 0 /* 8K ring */
  118. #else
  119. #define RX_BUF_IDX 2 /* 32K ring */
  120. #endif
  121. #define RX_BUF_LEN (8192 << RX_BUF_IDX)
  122. #define RX_BUF_PAD 16
  123. #define RX_BUF_WRAP_PAD 2048 /* spare padding to handle lack of packet wrap */
  124. #if RX_BUF_LEN == 65536
  125. #define RX_BUF_TOT_LEN RX_BUF_LEN
  126. #else
  127. #define RX_BUF_TOT_LEN (RX_BUF_LEN + RX_BUF_PAD + RX_BUF_WRAP_PAD)
  128. #endif
  129. /* Number of Tx descriptor registers. */
  130. #define NUM_TX_DESC 4
  131. /* max supported ethernet frame size -- must be at least (dev->mtu+14+4).*/
  132. #define MAX_ETH_FRAME_SIZE 1536
  133. /* Size of the Tx bounce buffers -- must be at least (dev->mtu+14+4). */
  134. #define TX_BUF_SIZE MAX_ETH_FRAME_SIZE
  135. #define TX_BUF_TOT_LEN (TX_BUF_SIZE * NUM_TX_DESC)
  136. /* PCI Tuning Parameters
  137. Threshold is bytes transferred to chip before transmission starts. */
  138. #define TX_FIFO_THRESH 256 /* In bytes, rounded down to 32 byte units. */
  139. /* The following settings are log_2(bytes)-4: 0 == 16 bytes .. 6==1024, 7==end of packet. */
  140. #define RX_FIFO_THRESH 7 /* Rx buffer level before first PCI xfer. */
  141. #define RX_DMA_BURST 7 /* Maximum PCI burst, '6' is 1024 */
  142. #define TX_DMA_BURST 6 /* Maximum PCI burst, '6' is 1024 */
  143. #define TX_RETRY 8 /* 0-15. retries = 16 + (TX_RETRY * 16) */
  144. /* Operational parameters that usually are not changed. */
  145. /* Time in jiffies before concluding the transmitter is hung. */
  146. #define TX_TIMEOUT (6*HZ)
  147. enum {
  148. HAS_MII_XCVR = 0x010000,
  149. HAS_CHIP_XCVR = 0x020000,
  150. HAS_LNK_CHNG = 0x040000,
  151. };
  152. #define RTL_NUM_STATS 4 /* number of ETHTOOL_GSTATS u64's */
  153. #define RTL_REGS_VER 1 /* version of reg. data in ETHTOOL_GREGS */
  154. #define RTL_MIN_IO_SIZE 0x80
  155. #define RTL8139B_IO_SIZE 256
  156. #define RTL8129_CAPS HAS_MII_XCVR
  157. #define RTL8139_CAPS (HAS_CHIP_XCVR|HAS_LNK_CHNG)
  158. typedef enum {
  159. RTL8139 = 0,
  160. RTL8129,
  161. } board_t;
  162. /* indexed by board_t, above */
  163. static const struct {
  164. const char *name;
  165. u32 hw_flags;
  166. } board_info[] __devinitdata = {
  167. { "RealTek RTL8139", RTL8139_CAPS },
  168. { "RealTek RTL8129", RTL8129_CAPS },
  169. };
  170. static DEFINE_PCI_DEVICE_TABLE(rtl8139_pci_tbl) = {
  171. {0x10ec, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  172. {0x10ec, 0x8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  173. {0x1113, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  174. {0x1500, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  175. {0x4033, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  176. {0x1186, 0x1300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  177. {0x1186, 0x1340, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  178. {0x13d1, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  179. {0x1259, 0xa117, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  180. {0x1259, 0xa11e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  181. {0x14ea, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  182. {0x14ea, 0xab07, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  183. {0x11db, 0x1234, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  184. {0x1432, 0x9130, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  185. {0x02ac, 0x1012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  186. {0x018a, 0x0106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  187. {0x126c, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  188. {0x1743, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  189. {0x021b, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  190. #ifdef CONFIG_SH_SECUREEDGE5410
  191. /* Bogus 8139 silicon reports 8129 without external PROM :-( */
  192. {0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  193. #endif
  194. #ifdef CONFIG_8139TOO_8129
  195. {0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8129 },
  196. #endif
  197. /* some crazy cards report invalid vendor ids like
  198. * 0x0001 here. The other ids are valid and constant,
  199. * so we simply don't match on the main vendor id.
  200. */
  201. {PCI_ANY_ID, 0x8139, 0x10ec, 0x8139, 0, 0, RTL8139 },
  202. {PCI_ANY_ID, 0x8139, 0x1186, 0x1300, 0, 0, RTL8139 },
  203. {PCI_ANY_ID, 0x8139, 0x13d1, 0xab06, 0, 0, RTL8139 },
  204. {0,}
  205. };
  206. MODULE_DEVICE_TABLE (pci, rtl8139_pci_tbl);
  207. static struct {
  208. const char str[ETH_GSTRING_LEN];
  209. } ethtool_stats_keys[] = {
  210. { "early_rx" },
  211. { "tx_buf_mapped" },
  212. { "tx_timeouts" },
  213. { "rx_lost_in_ring" },
  214. };
  215. /* The rest of these values should never change. */
  216. /* Symbolic offsets to registers. */
  217. enum RTL8139_registers {
  218. MAC0 = 0, /* Ethernet hardware address. */
  219. MAR0 = 8, /* Multicast filter. */
  220. TxStatus0 = 0x10, /* Transmit status (Four 32bit registers). */
  221. TxAddr0 = 0x20, /* Tx descriptors (also four 32bit). */
  222. RxBuf = 0x30,
  223. ChipCmd = 0x37,
  224. RxBufPtr = 0x38,
  225. RxBufAddr = 0x3A,
  226. IntrMask = 0x3C,
  227. IntrStatus = 0x3E,
  228. TxConfig = 0x40,
  229. RxConfig = 0x44,
  230. Timer = 0x48, /* A general-purpose counter. */
  231. RxMissed = 0x4C, /* 24 bits valid, write clears. */
  232. Cfg9346 = 0x50,
  233. Config0 = 0x51,
  234. Config1 = 0x52,
  235. TimerInt = 0x54,
  236. MediaStatus = 0x58,
  237. Config3 = 0x59,
  238. Config4 = 0x5A, /* absent on RTL-8139A */
  239. HltClk = 0x5B,
  240. MultiIntr = 0x5C,
  241. TxSummary = 0x60,
  242. BasicModeCtrl = 0x62,
  243. BasicModeStatus = 0x64,
  244. NWayAdvert = 0x66,
  245. NWayLPAR = 0x68,
  246. NWayExpansion = 0x6A,
  247. /* Undocumented registers, but required for proper operation. */
  248. FIFOTMS = 0x70, /* FIFO Control and test. */
  249. CSCR = 0x74, /* Chip Status and Configuration Register. */
  250. PARA78 = 0x78,
  251. FlashReg = 0xD4, /* Communication with Flash ROM, four bytes. */
  252. PARA7c = 0x7c, /* Magic transceiver parameter register. */
  253. Config5 = 0xD8, /* absent on RTL-8139A */
  254. };
  255. enum ClearBitMasks {
  256. MultiIntrClear = 0xF000,
  257. ChipCmdClear = 0xE2,
  258. Config1Clear = (1<<7)|(1<<6)|(1<<3)|(1<<2)|(1<<1),
  259. };
  260. enum ChipCmdBits {
  261. CmdReset = 0x10,
  262. CmdRxEnb = 0x08,
  263. CmdTxEnb = 0x04,
  264. RxBufEmpty = 0x01,
  265. };
  266. /* Interrupt register bits, using my own meaningful names. */
  267. enum IntrStatusBits {
  268. PCIErr = 0x8000,
  269. PCSTimeout = 0x4000,
  270. RxFIFOOver = 0x40,
  271. RxUnderrun = 0x20,
  272. RxOverflow = 0x10,
  273. TxErr = 0x08,
  274. TxOK = 0x04,
  275. RxErr = 0x02,
  276. RxOK = 0x01,
  277. RxAckBits = RxFIFOOver | RxOverflow | RxOK,
  278. };
  279. enum TxStatusBits {
  280. TxHostOwns = 0x2000,
  281. TxUnderrun = 0x4000,
  282. TxStatOK = 0x8000,
  283. TxOutOfWindow = 0x20000000,
  284. TxAborted = 0x40000000,
  285. TxCarrierLost = 0x80000000,
  286. };
  287. enum RxStatusBits {
  288. RxMulticast = 0x8000,
  289. RxPhysical = 0x4000,
  290. RxBroadcast = 0x2000,
  291. RxBadSymbol = 0x0020,
  292. RxRunt = 0x0010,
  293. RxTooLong = 0x0008,
  294. RxCRCErr = 0x0004,
  295. RxBadAlign = 0x0002,
  296. RxStatusOK = 0x0001,
  297. };
  298. /* Bits in RxConfig. */
  299. enum rx_mode_bits {
  300. AcceptErr = 0x20,
  301. AcceptRunt = 0x10,
  302. AcceptBroadcast = 0x08,
  303. AcceptMulticast = 0x04,
  304. AcceptMyPhys = 0x02,
  305. AcceptAllPhys = 0x01,
  306. };
  307. /* Bits in TxConfig. */
  308. enum tx_config_bits {
  309. /* Interframe Gap Time. Only TxIFG96 doesn't violate IEEE 802.3 */
  310. TxIFGShift = 24,
  311. TxIFG84 = (0 << TxIFGShift), /* 8.4us / 840ns (10 / 100Mbps) */
  312. TxIFG88 = (1 << TxIFGShift), /* 8.8us / 880ns (10 / 100Mbps) */
  313. TxIFG92 = (2 << TxIFGShift), /* 9.2us / 920ns (10 / 100Mbps) */
  314. TxIFG96 = (3 << TxIFGShift), /* 9.6us / 960ns (10 / 100Mbps) */
  315. TxLoopBack = (1 << 18) | (1 << 17), /* enable loopback test mode */
  316. TxCRC = (1 << 16), /* DISABLE Tx pkt CRC append */
  317. TxClearAbt = (1 << 0), /* Clear abort (WO) */
  318. TxDMAShift = 8, /* DMA burst value (0-7) is shifted X many bits */
  319. TxRetryShift = 4, /* TXRR value (0-15) is shifted X many bits */
  320. TxVersionMask = 0x7C800000, /* mask out version bits 30-26, 23 */
  321. };
  322. /* Bits in Config1 */
  323. enum Config1Bits {
  324. Cfg1_PM_Enable = 0x01,
  325. Cfg1_VPD_Enable = 0x02,
  326. Cfg1_PIO = 0x04,
  327. Cfg1_MMIO = 0x08,
  328. LWAKE = 0x10, /* not on 8139, 8139A */
  329. Cfg1_Driver_Load = 0x20,
  330. Cfg1_LED0 = 0x40,
  331. Cfg1_LED1 = 0x80,
  332. SLEEP = (1 << 1), /* only on 8139, 8139A */
  333. PWRDN = (1 << 0), /* only on 8139, 8139A */
  334. };
  335. /* Bits in Config3 */
  336. enum Config3Bits {
  337. Cfg3_FBtBEn = (1 << 0), /* 1 = Fast Back to Back */
  338. Cfg3_FuncRegEn = (1 << 1), /* 1 = enable CardBus Function registers */
  339. Cfg3_CLKRUN_En = (1 << 2), /* 1 = enable CLKRUN */
  340. Cfg3_CardB_En = (1 << 3), /* 1 = enable CardBus registers */
  341. Cfg3_LinkUp = (1 << 4), /* 1 = wake up on link up */
  342. Cfg3_Magic = (1 << 5), /* 1 = wake up on Magic Packet (tm) */
  343. Cfg3_PARM_En = (1 << 6), /* 0 = software can set twister parameters */
  344. Cfg3_GNTSel = (1 << 7), /* 1 = delay 1 clock from PCI GNT signal */
  345. };
  346. /* Bits in Config4 */
  347. enum Config4Bits {
  348. LWPTN = (1 << 2), /* not on 8139, 8139A */
  349. };
  350. /* Bits in Config5 */
  351. enum Config5Bits {
  352. Cfg5_PME_STS = (1 << 0), /* 1 = PCI reset resets PME_Status */
  353. Cfg5_LANWake = (1 << 1), /* 1 = enable LANWake signal */
  354. Cfg5_LDPS = (1 << 2), /* 0 = save power when link is down */
  355. Cfg5_FIFOAddrPtr= (1 << 3), /* Realtek internal SRAM testing */
  356. Cfg5_UWF = (1 << 4), /* 1 = accept unicast wakeup frame */
  357. Cfg5_MWF = (1 << 5), /* 1 = accept multicast wakeup frame */
  358. Cfg5_BWF = (1 << 6), /* 1 = accept broadcast wakeup frame */
  359. };
  360. enum RxConfigBits {
  361. /* rx fifo threshold */
  362. RxCfgFIFOShift = 13,
  363. RxCfgFIFONone = (7 << RxCfgFIFOShift),
  364. /* Max DMA burst */
  365. RxCfgDMAShift = 8,
  366. RxCfgDMAUnlimited = (7 << RxCfgDMAShift),
  367. /* rx ring buffer length */
  368. RxCfgRcv8K = 0,
  369. RxCfgRcv16K = (1 << 11),
  370. RxCfgRcv32K = (1 << 12),
  371. RxCfgRcv64K = (1 << 11) | (1 << 12),
  372. /* Disable packet wrap at end of Rx buffer. (not possible with 64k) */
  373. RxNoWrap = (1 << 7),
  374. };
  375. /* Twister tuning parameters from RealTek.
  376. Completely undocumented, but required to tune bad links on some boards. */
  377. enum CSCRBits {
  378. CSCR_LinkOKBit = 0x0400,
  379. CSCR_LinkChangeBit = 0x0800,
  380. CSCR_LinkStatusBits = 0x0f000,
  381. CSCR_LinkDownOffCmd = 0x003c0,
  382. CSCR_LinkDownCmd = 0x0f3c0,
  383. };
  384. enum Cfg9346Bits {
  385. Cfg9346_Lock = 0x00,
  386. Cfg9346_Unlock = 0xC0,
  387. };
  388. typedef enum {
  389. CH_8139 = 0,
  390. CH_8139_K,
  391. CH_8139A,
  392. CH_8139A_G,
  393. CH_8139B,
  394. CH_8130,
  395. CH_8139C,
  396. CH_8100,
  397. CH_8100B_8139D,
  398. CH_8101,
  399. } chip_t;
  400. enum chip_flags {
  401. HasHltClk = (1 << 0),
  402. HasLWake = (1 << 1),
  403. };
  404. #define HW_REVID(b30, b29, b28, b27, b26, b23, b22) \
  405. (b30<<30 | b29<<29 | b28<<28 | b27<<27 | b26<<26 | b23<<23 | b22<<22)
  406. #define HW_REVID_MASK HW_REVID(1, 1, 1, 1, 1, 1, 1)
  407. /* directly indexed by chip_t, above */
  408. static const struct {
  409. const char *name;
  410. u32 version; /* from RTL8139C/RTL8139D docs */
  411. u32 flags;
  412. } rtl_chip_info[] = {
  413. { "RTL-8139",
  414. HW_REVID(1, 0, 0, 0, 0, 0, 0),
  415. HasHltClk,
  416. },
  417. { "RTL-8139 rev K",
  418. HW_REVID(1, 1, 0, 0, 0, 0, 0),
  419. HasHltClk,
  420. },
  421. { "RTL-8139A",
  422. HW_REVID(1, 1, 1, 0, 0, 0, 0),
  423. HasHltClk, /* XXX undocumented? */
  424. },
  425. { "RTL-8139A rev G",
  426. HW_REVID(1, 1, 1, 0, 0, 1, 0),
  427. HasHltClk, /* XXX undocumented? */
  428. },
  429. { "RTL-8139B",
  430. HW_REVID(1, 1, 1, 1, 0, 0, 0),
  431. HasLWake,
  432. },
  433. { "RTL-8130",
  434. HW_REVID(1, 1, 1, 1, 1, 0, 0),
  435. HasLWake,
  436. },
  437. { "RTL-8139C",
  438. HW_REVID(1, 1, 1, 0, 1, 0, 0),
  439. HasLWake,
  440. },
  441. { "RTL-8100",
  442. HW_REVID(1, 1, 1, 1, 0, 1, 0),
  443. HasLWake,
  444. },
  445. { "RTL-8100B/8139D",
  446. HW_REVID(1, 1, 1, 0, 1, 0, 1),
  447. HasHltClk /* XXX undocumented? */
  448. | HasLWake,
  449. },
  450. { "RTL-8101",
  451. HW_REVID(1, 1, 1, 0, 1, 1, 1),
  452. HasLWake,
  453. },
  454. };
  455. struct rtl_extra_stats {
  456. unsigned long early_rx;
  457. unsigned long tx_buf_mapped;
  458. unsigned long tx_timeouts;
  459. unsigned long rx_lost_in_ring;
  460. };
  461. struct rtl8139_private {
  462. void __iomem *mmio_addr;
  463. int drv_flags;
  464. struct pci_dev *pci_dev;
  465. u32 msg_enable;
  466. struct napi_struct napi;
  467. struct net_device *dev;
  468. unsigned char *rx_ring;
  469. unsigned int cur_rx; /* RX buf index of next pkt */
  470. dma_addr_t rx_ring_dma;
  471. unsigned int tx_flag;
  472. unsigned long cur_tx;
  473. unsigned long dirty_tx;
  474. unsigned char *tx_buf[NUM_TX_DESC]; /* Tx bounce buffers */
  475. unsigned char *tx_bufs; /* Tx bounce buffer region. */
  476. dma_addr_t tx_bufs_dma;
  477. signed char phys[4]; /* MII device addresses. */
  478. /* Twister tune state. */
  479. char twistie, twist_row, twist_col;
  480. unsigned int watchdog_fired : 1;
  481. unsigned int default_port : 4; /* Last dev->if_port value. */
  482. unsigned int have_thread : 1;
  483. spinlock_t lock;
  484. spinlock_t rx_lock;
  485. chip_t chipset;
  486. u32 rx_config;
  487. struct rtl_extra_stats xstats;
  488. struct delayed_work thread;
  489. struct mii_if_info mii;
  490. unsigned int regs_len;
  491. unsigned long fifo_copy_timeout;
  492. };
  493. MODULE_AUTHOR ("Jeff Garzik <jgarzik@pobox.com>");
  494. MODULE_DESCRIPTION ("RealTek RTL-8139 Fast Ethernet driver");
  495. MODULE_LICENSE("GPL");
  496. MODULE_VERSION(DRV_VERSION);
  497. module_param(use_io, int, 0);
  498. MODULE_PARM_DESC(use_io, "Force use of I/O access mode. 0=MMIO 1=PIO");
  499. module_param(multicast_filter_limit, int, 0);
  500. module_param_array(media, int, NULL, 0);
  501. module_param_array(full_duplex, int, NULL, 0);
  502. module_param(debug, int, 0);
  503. MODULE_PARM_DESC (debug, "8139too bitmapped message enable number");
  504. MODULE_PARM_DESC (multicast_filter_limit, "8139too maximum number of filtered multicast addresses");
  505. MODULE_PARM_DESC (media, "8139too: Bits 4+9: force full duplex, bit 5: 100Mbps");
  506. MODULE_PARM_DESC (full_duplex, "8139too: Force full duplex for board(s) (1)");
  507. static int read_eeprom (void __iomem *ioaddr, int location, int addr_len);
  508. static int rtl8139_open (struct net_device *dev);
  509. static int mdio_read (struct net_device *dev, int phy_id, int location);
  510. static void mdio_write (struct net_device *dev, int phy_id, int location,
  511. int val);
  512. static void rtl8139_start_thread(struct rtl8139_private *tp);
  513. static void rtl8139_tx_timeout (struct net_device *dev);
  514. static void rtl8139_init_ring (struct net_device *dev);
  515. static netdev_tx_t rtl8139_start_xmit (struct sk_buff *skb,
  516. struct net_device *dev);
  517. #ifdef CONFIG_NET_POLL_CONTROLLER
  518. static void rtl8139_poll_controller(struct net_device *dev);
  519. #endif
  520. static int rtl8139_set_mac_address(struct net_device *dev, void *p);
  521. static int rtl8139_poll(struct napi_struct *napi, int budget);
  522. static irqreturn_t rtl8139_interrupt (int irq, void *dev_instance);
  523. static int rtl8139_close (struct net_device *dev);
  524. static int netdev_ioctl (struct net_device *dev, struct ifreq *rq, int cmd);
  525. static struct net_device_stats *rtl8139_get_stats (struct net_device *dev);
  526. static void rtl8139_set_rx_mode (struct net_device *dev);
  527. static void __set_rx_mode (struct net_device *dev);
  528. static void rtl8139_hw_start (struct net_device *dev);
  529. static void rtl8139_thread (struct work_struct *work);
  530. static void rtl8139_tx_timeout_task(struct work_struct *work);
  531. static const struct ethtool_ops rtl8139_ethtool_ops;
  532. /* write MMIO register, with flush */
  533. /* Flush avoids rtl8139 bug w/ posted MMIO writes */
  534. #define RTL_W8_F(reg, val8) do { iowrite8 ((val8), ioaddr + (reg)); ioread8 (ioaddr + (reg)); } while (0)
  535. #define RTL_W16_F(reg, val16) do { iowrite16 ((val16), ioaddr + (reg)); ioread16 (ioaddr + (reg)); } while (0)
  536. #define RTL_W32_F(reg, val32) do { iowrite32 ((val32), ioaddr + (reg)); ioread32 (ioaddr + (reg)); } while (0)
  537. /* write MMIO register */
  538. #define RTL_W8(reg, val8) iowrite8 ((val8), ioaddr + (reg))
  539. #define RTL_W16(reg, val16) iowrite16 ((val16), ioaddr + (reg))
  540. #define RTL_W32(reg, val32) iowrite32 ((val32), ioaddr + (reg))
  541. /* read MMIO register */
  542. #define RTL_R8(reg) ioread8 (ioaddr + (reg))
  543. #define RTL_R16(reg) ioread16 (ioaddr + (reg))
  544. #define RTL_R32(reg) ioread32 (ioaddr + (reg))
  545. static const u16 rtl8139_intr_mask =
  546. PCIErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver |
  547. TxErr | TxOK | RxErr | RxOK;
  548. static const u16 rtl8139_norx_intr_mask =
  549. PCIErr | PCSTimeout | RxUnderrun |
  550. TxErr | TxOK | RxErr ;
  551. #if RX_BUF_IDX == 0
  552. static const unsigned int rtl8139_rx_config =
  553. RxCfgRcv8K | RxNoWrap |
  554. (RX_FIFO_THRESH << RxCfgFIFOShift) |
  555. (RX_DMA_BURST << RxCfgDMAShift);
  556. #elif RX_BUF_IDX == 1
  557. static const unsigned int rtl8139_rx_config =
  558. RxCfgRcv16K | RxNoWrap |
  559. (RX_FIFO_THRESH << RxCfgFIFOShift) |
  560. (RX_DMA_BURST << RxCfgDMAShift);
  561. #elif RX_BUF_IDX == 2
  562. static const unsigned int rtl8139_rx_config =
  563. RxCfgRcv32K | RxNoWrap |
  564. (RX_FIFO_THRESH << RxCfgFIFOShift) |
  565. (RX_DMA_BURST << RxCfgDMAShift);
  566. #elif RX_BUF_IDX == 3
  567. static const unsigned int rtl8139_rx_config =
  568. RxCfgRcv64K |
  569. (RX_FIFO_THRESH << RxCfgFIFOShift) |
  570. (RX_DMA_BURST << RxCfgDMAShift);
  571. #else
  572. #error "Invalid configuration for 8139_RXBUF_IDX"
  573. #endif
  574. static const unsigned int rtl8139_tx_config =
  575. TxIFG96 | (TX_DMA_BURST << TxDMAShift) | (TX_RETRY << TxRetryShift);
  576. static void __rtl8139_cleanup_dev (struct net_device *dev)
  577. {
  578. struct rtl8139_private *tp = netdev_priv(dev);
  579. struct pci_dev *pdev;
  580. assert (dev != NULL);
  581. assert (tp->pci_dev != NULL);
  582. pdev = tp->pci_dev;
  583. if (tp->mmio_addr)
  584. pci_iounmap (pdev, tp->mmio_addr);
  585. /* it's ok to call this even if we have no regions to free */
  586. pci_release_regions (pdev);
  587. free_netdev(dev);
  588. pci_set_drvdata (pdev, NULL);
  589. }
  590. static void rtl8139_chip_reset (void __iomem *ioaddr)
  591. {
  592. int i;
  593. /* Soft reset the chip. */
  594. RTL_W8 (ChipCmd, CmdReset);
  595. /* Check that the chip has finished the reset. */
  596. for (i = 1000; i > 0; i--) {
  597. barrier();
  598. if ((RTL_R8 (ChipCmd) & CmdReset) == 0)
  599. break;
  600. udelay (10);
  601. }
  602. }
  603. static __devinit struct net_device * rtl8139_init_board (struct pci_dev *pdev)
  604. {
  605. void __iomem *ioaddr;
  606. struct net_device *dev;
  607. struct rtl8139_private *tp;
  608. u8 tmp8;
  609. int rc, disable_dev_on_err = 0;
  610. unsigned int i;
  611. unsigned long pio_start, pio_end, pio_flags, pio_len;
  612. unsigned long mmio_start, mmio_end, mmio_flags, mmio_len;
  613. u32 version;
  614. assert (pdev != NULL);
  615. /* dev and priv zeroed in alloc_etherdev */
  616. dev = alloc_etherdev (sizeof (*tp));
  617. if (dev == NULL) {
  618. dev_err(&pdev->dev, "Unable to alloc new net device\n");
  619. return ERR_PTR(-ENOMEM);
  620. }
  621. SET_NETDEV_DEV(dev, &pdev->dev);
  622. tp = netdev_priv(dev);
  623. tp->pci_dev = pdev;
  624. /* enable device (incl. PCI PM wakeup and hotplug setup) */
  625. rc = pci_enable_device (pdev);
  626. if (rc)
  627. goto err_out;
  628. pio_start = pci_resource_start (pdev, 0);
  629. pio_end = pci_resource_end (pdev, 0);
  630. pio_flags = pci_resource_flags (pdev, 0);
  631. pio_len = pci_resource_len (pdev, 0);
  632. mmio_start = pci_resource_start (pdev, 1);
  633. mmio_end = pci_resource_end (pdev, 1);
  634. mmio_flags = pci_resource_flags (pdev, 1);
  635. mmio_len = pci_resource_len (pdev, 1);
  636. /* set this immediately, we need to know before
  637. * we talk to the chip directly */
  638. pr_debug("PIO region size == 0x%02lX\n", pio_len);
  639. pr_debug("MMIO region size == 0x%02lX\n", mmio_len);
  640. retry:
  641. if (use_io) {
  642. /* make sure PCI base addr 0 is PIO */
  643. if (!(pio_flags & IORESOURCE_IO)) {
  644. dev_err(&pdev->dev, "region #0 not a PIO resource, aborting\n");
  645. rc = -ENODEV;
  646. goto err_out;
  647. }
  648. /* check for weird/broken PCI region reporting */
  649. if (pio_len < RTL_MIN_IO_SIZE) {
  650. dev_err(&pdev->dev, "Invalid PCI I/O region size(s), aborting\n");
  651. rc = -ENODEV;
  652. goto err_out;
  653. }
  654. } else {
  655. /* make sure PCI base addr 1 is MMIO */
  656. if (!(mmio_flags & IORESOURCE_MEM)) {
  657. dev_err(&pdev->dev, "region #1 not an MMIO resource, aborting\n");
  658. rc = -ENODEV;
  659. goto err_out;
  660. }
  661. if (mmio_len < RTL_MIN_IO_SIZE) {
  662. dev_err(&pdev->dev, "Invalid PCI mem region size(s), aborting\n");
  663. rc = -ENODEV;
  664. goto err_out;
  665. }
  666. }
  667. rc = pci_request_regions (pdev, DRV_NAME);
  668. if (rc)
  669. goto err_out;
  670. disable_dev_on_err = 1;
  671. /* enable PCI bus-mastering */
  672. pci_set_master (pdev);
  673. if (use_io) {
  674. ioaddr = pci_iomap(pdev, 0, 0);
  675. if (!ioaddr) {
  676. dev_err(&pdev->dev, "cannot map PIO, aborting\n");
  677. rc = -EIO;
  678. goto err_out;
  679. }
  680. dev->base_addr = pio_start;
  681. tp->regs_len = pio_len;
  682. } else {
  683. /* ioremap MMIO region */
  684. ioaddr = pci_iomap(pdev, 1, 0);
  685. if (ioaddr == NULL) {
  686. dev_err(&pdev->dev, "cannot remap MMIO, trying PIO\n");
  687. pci_release_regions(pdev);
  688. use_io = 1;
  689. goto retry;
  690. }
  691. dev->base_addr = (long) ioaddr;
  692. tp->regs_len = mmio_len;
  693. }
  694. tp->mmio_addr = ioaddr;
  695. /* Bring old chips out of low-power mode. */
  696. RTL_W8 (HltClk, 'R');
  697. /* check for missing/broken hardware */
  698. if (RTL_R32 (TxConfig) == 0xFFFFFFFF) {
  699. dev_err(&pdev->dev, "Chip not responding, ignoring board\n");
  700. rc = -EIO;
  701. goto err_out;
  702. }
  703. /* identify chip attached to board */
  704. version = RTL_R32 (TxConfig) & HW_REVID_MASK;
  705. for (i = 0; i < ARRAY_SIZE (rtl_chip_info); i++)
  706. if (version == rtl_chip_info[i].version) {
  707. tp->chipset = i;
  708. goto match;
  709. }
  710. /* if unknown chip, assume array element #0, original RTL-8139 in this case */
  711. i = 0;
  712. dev_dbg(&pdev->dev, "unknown chip version, assuming RTL-8139\n");
  713. dev_dbg(&pdev->dev, "TxConfig = 0x%x\n", RTL_R32 (TxConfig));
  714. tp->chipset = 0;
  715. match:
  716. pr_debug("chipset id (%d) == index %d, '%s'\n",
  717. version, i, rtl_chip_info[i].name);
  718. if (tp->chipset >= CH_8139B) {
  719. u8 new_tmp8 = tmp8 = RTL_R8 (Config1);
  720. pr_debug("PCI PM wakeup\n");
  721. if ((rtl_chip_info[tp->chipset].flags & HasLWake) &&
  722. (tmp8 & LWAKE))
  723. new_tmp8 &= ~LWAKE;
  724. new_tmp8 |= Cfg1_PM_Enable;
  725. if (new_tmp8 != tmp8) {
  726. RTL_W8 (Cfg9346, Cfg9346_Unlock);
  727. RTL_W8 (Config1, tmp8);
  728. RTL_W8 (Cfg9346, Cfg9346_Lock);
  729. }
  730. if (rtl_chip_info[tp->chipset].flags & HasLWake) {
  731. tmp8 = RTL_R8 (Config4);
  732. if (tmp8 & LWPTN) {
  733. RTL_W8 (Cfg9346, Cfg9346_Unlock);
  734. RTL_W8 (Config4, tmp8 & ~LWPTN);
  735. RTL_W8 (Cfg9346, Cfg9346_Lock);
  736. }
  737. }
  738. } else {
  739. pr_debug("Old chip wakeup\n");
  740. tmp8 = RTL_R8 (Config1);
  741. tmp8 &= ~(SLEEP | PWRDN);
  742. RTL_W8 (Config1, tmp8);
  743. }
  744. rtl8139_chip_reset (ioaddr);
  745. return dev;
  746. err_out:
  747. __rtl8139_cleanup_dev (dev);
  748. if (disable_dev_on_err)
  749. pci_disable_device (pdev);
  750. return ERR_PTR(rc);
  751. }
  752. static const struct net_device_ops rtl8139_netdev_ops = {
  753. .ndo_open = rtl8139_open,
  754. .ndo_stop = rtl8139_close,
  755. .ndo_get_stats = rtl8139_get_stats,
  756. .ndo_change_mtu = eth_change_mtu,
  757. .ndo_validate_addr = eth_validate_addr,
  758. .ndo_set_mac_address = rtl8139_set_mac_address,
  759. .ndo_start_xmit = rtl8139_start_xmit,
  760. .ndo_set_multicast_list = rtl8139_set_rx_mode,
  761. .ndo_do_ioctl = netdev_ioctl,
  762. .ndo_tx_timeout = rtl8139_tx_timeout,
  763. #ifdef CONFIG_NET_POLL_CONTROLLER
  764. .ndo_poll_controller = rtl8139_poll_controller,
  765. #endif
  766. };
  767. static int __devinit rtl8139_init_one (struct pci_dev *pdev,
  768. const struct pci_device_id *ent)
  769. {
  770. struct net_device *dev = NULL;
  771. struct rtl8139_private *tp;
  772. int i, addr_len, option;
  773. void __iomem *ioaddr;
  774. static int board_idx = -1;
  775. assert (pdev != NULL);
  776. assert (ent != NULL);
  777. board_idx++;
  778. /* when we're built into the kernel, the driver version message
  779. * is only printed if at least one 8139 board has been found
  780. */
  781. #ifndef MODULE
  782. {
  783. static int printed_version;
  784. if (!printed_version++)
  785. pr_info(RTL8139_DRIVER_NAME "\n");
  786. }
  787. #endif
  788. if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
  789. pdev->device == PCI_DEVICE_ID_REALTEK_8139 && pdev->revision >= 0x20) {
  790. dev_info(&pdev->dev,
  791. "This (id %04x:%04x rev %02x) is an enhanced 8139C+ chip, use 8139cp\n",
  792. pdev->vendor, pdev->device, pdev->revision);
  793. return -ENODEV;
  794. }
  795. if (pdev->vendor == PCI_VENDOR_ID_REALTEK &&
  796. pdev->device == PCI_DEVICE_ID_REALTEK_8139 &&
  797. pdev->subsystem_vendor == PCI_VENDOR_ID_ATHEROS &&
  798. pdev->subsystem_device == PCI_DEVICE_ID_REALTEK_8139) {
  799. pr_info("OQO Model 2 detected. Forcing PIO\n");
  800. use_io = 1;
  801. }
  802. dev = rtl8139_init_board (pdev);
  803. if (IS_ERR(dev))
  804. return PTR_ERR(dev);
  805. assert (dev != NULL);
  806. tp = netdev_priv(dev);
  807. tp->dev = dev;
  808. ioaddr = tp->mmio_addr;
  809. assert (ioaddr != NULL);
  810. addr_len = read_eeprom (ioaddr, 0, 8) == 0x8129 ? 8 : 6;
  811. for (i = 0; i < 3; i++)
  812. ((__le16 *) (dev->dev_addr))[i] =
  813. cpu_to_le16(read_eeprom (ioaddr, i + 7, addr_len));
  814. memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
  815. /* The Rtl8139-specific entries in the device structure. */
  816. dev->netdev_ops = &rtl8139_netdev_ops;
  817. dev->ethtool_ops = &rtl8139_ethtool_ops;
  818. dev->watchdog_timeo = TX_TIMEOUT;
  819. netif_napi_add(dev, &tp->napi, rtl8139_poll, 64);
  820. /* note: the hardware is not capable of sg/csum/highdma, however
  821. * through the use of skb_copy_and_csum_dev we enable these
  822. * features
  823. */
  824. dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_HIGHDMA;
  825. dev->vlan_features = dev->features;
  826. dev->irq = pdev->irq;
  827. /* tp zeroed and aligned in alloc_etherdev */
  828. tp = netdev_priv(dev);
  829. /* note: tp->chipset set in rtl8139_init_board */
  830. tp->drv_flags = board_info[ent->driver_data].hw_flags;
  831. tp->mmio_addr = ioaddr;
  832. tp->msg_enable =
  833. (debug < 0 ? RTL8139_DEF_MSG_ENABLE : ((1 << debug) - 1));
  834. spin_lock_init (&tp->lock);
  835. spin_lock_init (&tp->rx_lock);
  836. INIT_DELAYED_WORK(&tp->thread, rtl8139_thread);
  837. tp->mii.dev = dev;
  838. tp->mii.mdio_read = mdio_read;
  839. tp->mii.mdio_write = mdio_write;
  840. tp->mii.phy_id_mask = 0x3f;
  841. tp->mii.reg_num_mask = 0x1f;
  842. /* dev is fully set up and ready to use now */
  843. pr_debug("about to register device named %s (%p)...\n",
  844. dev->name, dev);
  845. i = register_netdev (dev);
  846. if (i) goto err_out;
  847. pci_set_drvdata (pdev, dev);
  848. netdev_info(dev, "%s at 0x%lx, %pM, IRQ %d\n",
  849. board_info[ent->driver_data].name,
  850. dev->base_addr, dev->dev_addr, dev->irq);
  851. netdev_dbg(dev, "Identified 8139 chip type '%s'\n",
  852. rtl_chip_info[tp->chipset].name);
  853. /* Find the connected MII xcvrs.
  854. Doing this in open() would allow detecting external xcvrs later, but
  855. takes too much time. */
  856. #ifdef CONFIG_8139TOO_8129
  857. if (tp->drv_flags & HAS_MII_XCVR) {
  858. int phy, phy_idx = 0;
  859. for (phy = 0; phy < 32 && phy_idx < sizeof(tp->phys); phy++) {
  860. int mii_status = mdio_read(dev, phy, 1);
  861. if (mii_status != 0xffff && mii_status != 0x0000) {
  862. u16 advertising = mdio_read(dev, phy, 4);
  863. tp->phys[phy_idx++] = phy;
  864. netdev_info(dev, "MII transceiver %d status 0x%04x advertising %04x\n",
  865. phy, mii_status, advertising);
  866. }
  867. }
  868. if (phy_idx == 0) {
  869. netdev_info(dev, "No MII transceivers found! Assuming SYM transceiver\n");
  870. tp->phys[0] = 32;
  871. }
  872. } else
  873. #endif
  874. tp->phys[0] = 32;
  875. tp->mii.phy_id = tp->phys[0];
  876. /* The lower four bits are the media type. */
  877. option = (board_idx >= MAX_UNITS) ? 0 : media[board_idx];
  878. if (option > 0) {
  879. tp->mii.full_duplex = (option & 0x210) ? 1 : 0;
  880. tp->default_port = option & 0xFF;
  881. if (tp->default_port)
  882. tp->mii.force_media = 1;
  883. }
  884. if (board_idx < MAX_UNITS && full_duplex[board_idx] > 0)
  885. tp->mii.full_duplex = full_duplex[board_idx];
  886. if (tp->mii.full_duplex) {
  887. netdev_info(dev, "Media type forced to Full Duplex\n");
  888. /* Changing the MII-advertised media because might prevent
  889. re-connection. */
  890. tp->mii.force_media = 1;
  891. }
  892. if (tp->default_port) {
  893. netdev_info(dev, " Forcing %dMbps %s-duplex operation\n",
  894. (option & 0x20 ? 100 : 10),
  895. (option & 0x10 ? "full" : "half"));
  896. mdio_write(dev, tp->phys[0], 0,
  897. ((option & 0x20) ? 0x2000 : 0) | /* 100Mbps? */
  898. ((option & 0x10) ? 0x0100 : 0)); /* Full duplex? */
  899. }
  900. /* Put the chip into low-power mode. */
  901. if (rtl_chip_info[tp->chipset].flags & HasHltClk)
  902. RTL_W8 (HltClk, 'H'); /* 'R' would leave the clock running. */
  903. return 0;
  904. err_out:
  905. __rtl8139_cleanup_dev (dev);
  906. pci_disable_device (pdev);
  907. return i;
  908. }
  909. static void __devexit rtl8139_remove_one (struct pci_dev *pdev)
  910. {
  911. struct net_device *dev = pci_get_drvdata (pdev);
  912. struct rtl8139_private *tp = netdev_priv(dev);
  913. assert (dev != NULL);
  914. cancel_delayed_work_sync(&tp->thread);
  915. unregister_netdev (dev);
  916. __rtl8139_cleanup_dev (dev);
  917. pci_disable_device (pdev);
  918. }
  919. /* Serial EEPROM section. */
  920. /* EEPROM_Ctrl bits. */
  921. #define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
  922. #define EE_CS 0x08 /* EEPROM chip select. */
  923. #define EE_DATA_WRITE 0x02 /* EEPROM chip data in. */
  924. #define EE_WRITE_0 0x00
  925. #define EE_WRITE_1 0x02
  926. #define EE_DATA_READ 0x01 /* EEPROM chip data out. */
  927. #define EE_ENB (0x80 | EE_CS)
  928. /* Delay between EEPROM clock transitions.
  929. No extra delay is needed with 33Mhz PCI, but 66Mhz may change this.
  930. */
  931. #define eeprom_delay() (void)RTL_R32(Cfg9346)
  932. /* The EEPROM commands include the alway-set leading bit. */
  933. #define EE_WRITE_CMD (5)
  934. #define EE_READ_CMD (6)
  935. #define EE_ERASE_CMD (7)
  936. static int __devinit read_eeprom (void __iomem *ioaddr, int location, int addr_len)
  937. {
  938. int i;
  939. unsigned retval = 0;
  940. int read_cmd = location | (EE_READ_CMD << addr_len);
  941. RTL_W8 (Cfg9346, EE_ENB & ~EE_CS);
  942. RTL_W8 (Cfg9346, EE_ENB);
  943. eeprom_delay ();
  944. /* Shift the read command bits out. */
  945. for (i = 4 + addr_len; i >= 0; i--) {
  946. int dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
  947. RTL_W8 (Cfg9346, EE_ENB | dataval);
  948. eeprom_delay ();
  949. RTL_W8 (Cfg9346, EE_ENB | dataval | EE_SHIFT_CLK);
  950. eeprom_delay ();
  951. }
  952. RTL_W8 (Cfg9346, EE_ENB);
  953. eeprom_delay ();
  954. for (i = 16; i > 0; i--) {
  955. RTL_W8 (Cfg9346, EE_ENB | EE_SHIFT_CLK);
  956. eeprom_delay ();
  957. retval =
  958. (retval << 1) | ((RTL_R8 (Cfg9346) & EE_DATA_READ) ? 1 :
  959. 0);
  960. RTL_W8 (Cfg9346, EE_ENB);
  961. eeprom_delay ();
  962. }
  963. /* Terminate the EEPROM access. */
  964. RTL_W8 (Cfg9346, ~EE_CS);
  965. eeprom_delay ();
  966. return retval;
  967. }
  968. /* MII serial management: mostly bogus for now. */
  969. /* Read and write the MII management registers using software-generated
  970. serial MDIO protocol.
  971. The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
  972. met by back-to-back PCI I/O cycles, but we insert a delay to avoid
  973. "overclocking" issues. */
  974. #define MDIO_DIR 0x80
  975. #define MDIO_DATA_OUT 0x04
  976. #define MDIO_DATA_IN 0x02
  977. #define MDIO_CLK 0x01
  978. #define MDIO_WRITE0 (MDIO_DIR)
  979. #define MDIO_WRITE1 (MDIO_DIR | MDIO_DATA_OUT)
  980. #define mdio_delay() RTL_R8(Config4)
  981. static const char mii_2_8139_map[8] = {
  982. BasicModeCtrl,
  983. BasicModeStatus,
  984. 0,
  985. 0,
  986. NWayAdvert,
  987. NWayLPAR,
  988. NWayExpansion,
  989. 0
  990. };
  991. #ifdef CONFIG_8139TOO_8129
  992. /* Syncronize the MII management interface by shifting 32 one bits out. */
  993. static void mdio_sync (void __iomem *ioaddr)
  994. {
  995. int i;
  996. for (i = 32; i >= 0; i--) {
  997. RTL_W8 (Config4, MDIO_WRITE1);
  998. mdio_delay ();
  999. RTL_W8 (Config4, MDIO_WRITE1 | MDIO_CLK);
  1000. mdio_delay ();
  1001. }
  1002. }
  1003. #endif
  1004. static int mdio_read (struct net_device *dev, int phy_id, int location)
  1005. {
  1006. struct rtl8139_private *tp = netdev_priv(dev);
  1007. int retval = 0;
  1008. #ifdef CONFIG_8139TOO_8129
  1009. void __iomem *ioaddr = tp->mmio_addr;
  1010. int mii_cmd = (0xf6 << 10) | (phy_id << 5) | location;
  1011. int i;
  1012. #endif
  1013. if (phy_id > 31) { /* Really a 8139. Use internal registers. */
  1014. void __iomem *ioaddr = tp->mmio_addr;
  1015. return location < 8 && mii_2_8139_map[location] ?
  1016. RTL_R16 (mii_2_8139_map[location]) : 0;
  1017. }
  1018. #ifdef CONFIG_8139TOO_8129
  1019. mdio_sync (ioaddr);
  1020. /* Shift the read command bits out. */
  1021. for (i = 15; i >= 0; i--) {
  1022. int dataval = (mii_cmd & (1 << i)) ? MDIO_DATA_OUT : 0;
  1023. RTL_W8 (Config4, MDIO_DIR | dataval);
  1024. mdio_delay ();
  1025. RTL_W8 (Config4, MDIO_DIR | dataval | MDIO_CLK);
  1026. mdio_delay ();
  1027. }
  1028. /* Read the two transition, 16 data, and wire-idle bits. */
  1029. for (i = 19; i > 0; i--) {
  1030. RTL_W8 (Config4, 0);
  1031. mdio_delay ();
  1032. retval = (retval << 1) | ((RTL_R8 (Config4) & MDIO_DATA_IN) ? 1 : 0);
  1033. RTL_W8 (Config4, MDIO_CLK);
  1034. mdio_delay ();
  1035. }
  1036. #endif
  1037. return (retval >> 1) & 0xffff;
  1038. }
  1039. static void mdio_write (struct net_device *dev, int phy_id, int location,
  1040. int value)
  1041. {
  1042. struct rtl8139_private *tp = netdev_priv(dev);
  1043. #ifdef CONFIG_8139TOO_8129
  1044. void __iomem *ioaddr = tp->mmio_addr;
  1045. int mii_cmd = (0x5002 << 16) | (phy_id << 23) | (location << 18) | value;
  1046. int i;
  1047. #endif
  1048. if (phy_id > 31) { /* Really a 8139. Use internal registers. */
  1049. void __iomem *ioaddr = tp->mmio_addr;
  1050. if (location == 0) {
  1051. RTL_W8 (Cfg9346, Cfg9346_Unlock);
  1052. RTL_W16 (BasicModeCtrl, value);
  1053. RTL_W8 (Cfg9346, Cfg9346_Lock);
  1054. } else if (location < 8 && mii_2_8139_map[location])
  1055. RTL_W16 (mii_2_8139_map[location], value);
  1056. return;
  1057. }
  1058. #ifdef CONFIG_8139TOO_8129
  1059. mdio_sync (ioaddr);
  1060. /* Shift the command bits out. */
  1061. for (i = 31; i >= 0; i--) {
  1062. int dataval =
  1063. (mii_cmd & (1 << i)) ? MDIO_WRITE1 : MDIO_WRITE0;
  1064. RTL_W8 (Config4, dataval);
  1065. mdio_delay ();
  1066. RTL_W8 (Config4, dataval | MDIO_CLK);
  1067. mdio_delay ();
  1068. }
  1069. /* Clear out extra bits. */
  1070. for (i = 2; i > 0; i--) {
  1071. RTL_W8 (Config4, 0);
  1072. mdio_delay ();
  1073. RTL_W8 (Config4, MDIO_CLK);
  1074. mdio_delay ();
  1075. }
  1076. #endif
  1077. }
  1078. static int rtl8139_open (struct net_device *dev)
  1079. {
  1080. struct rtl8139_private *tp = netdev_priv(dev);
  1081. int retval;
  1082. void __iomem *ioaddr = tp->mmio_addr;
  1083. retval = request_irq (dev->irq, rtl8139_interrupt, IRQF_SHARED, dev->name, dev);
  1084. if (retval)
  1085. return retval;
  1086. tp->tx_bufs = dma_alloc_coherent(&tp->pci_dev->dev, TX_BUF_TOT_LEN,
  1087. &tp->tx_bufs_dma, GFP_KERNEL);
  1088. tp->rx_ring = dma_alloc_coherent(&tp->pci_dev->dev, RX_BUF_TOT_LEN,
  1089. &tp->rx_ring_dma, GFP_KERNEL);
  1090. if (tp->tx_bufs == NULL || tp->rx_ring == NULL) {
  1091. free_irq(dev->irq, dev);
  1092. if (tp->tx_bufs)
  1093. dma_free_coherent(&tp->pci_dev->dev, TX_BUF_TOT_LEN,
  1094. tp->tx_bufs, tp->tx_bufs_dma);
  1095. if (tp->rx_ring)
  1096. dma_free_coherent(&tp->pci_dev->dev, RX_BUF_TOT_LEN,
  1097. tp->rx_ring, tp->rx_ring_dma);
  1098. return -ENOMEM;
  1099. }
  1100. napi_enable(&tp->napi);
  1101. tp->mii.full_duplex = tp->mii.force_media;
  1102. tp->tx_flag = (TX_FIFO_THRESH << 11) & 0x003f0000;
  1103. rtl8139_init_ring (dev);
  1104. rtl8139_hw_start (dev);
  1105. netif_start_queue (dev);
  1106. netif_dbg(tp, ifup, dev,
  1107. "%s() ioaddr %#llx IRQ %d GP Pins %02x %s-duplex\n",
  1108. __func__,
  1109. (unsigned long long)pci_resource_start (tp->pci_dev, 1),
  1110. dev->irq, RTL_R8 (MediaStatus),
  1111. tp->mii.full_duplex ? "full" : "half");
  1112. rtl8139_start_thread(tp);
  1113. return 0;
  1114. }
  1115. static void rtl_check_media (struct net_device *dev, unsigned int init_media)
  1116. {
  1117. struct rtl8139_private *tp = netdev_priv(dev);
  1118. if (tp->phys[0] >= 0) {
  1119. mii_check_media(&tp->mii, netif_msg_link(tp), init_media);
  1120. }
  1121. }
  1122. /* Start the hardware at open or resume. */
  1123. static void rtl8139_hw_start (struct net_device *dev)
  1124. {
  1125. struct rtl8139_private *tp = netdev_priv(dev);
  1126. void __iomem *ioaddr = tp->mmio_addr;
  1127. u32 i;
  1128. u8 tmp;
  1129. /* Bring old chips out of low-power mode. */
  1130. if (rtl_chip_info[tp->chipset].flags & HasHltClk)
  1131. RTL_W8 (HltClk, 'R');
  1132. rtl8139_chip_reset (ioaddr);
  1133. /* unlock Config[01234] and BMCR register writes */
  1134. RTL_W8_F (Cfg9346, Cfg9346_Unlock);
  1135. /* Restore our idea of the MAC address. */
  1136. RTL_W32_F (MAC0 + 0, le32_to_cpu (*(__le32 *) (dev->dev_addr + 0)));
  1137. RTL_W32_F (MAC0 + 4, le16_to_cpu (*(__le16 *) (dev->dev_addr + 4)));
  1138. tp->cur_rx = 0;
  1139. /* init Rx ring buffer DMA address */
  1140. RTL_W32_F (RxBuf, tp->rx_ring_dma);
  1141. /* Must enable Tx/Rx before setting transfer thresholds! */
  1142. RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
  1143. tp->rx_config = rtl8139_rx_config | AcceptBroadcast | AcceptMyPhys;
  1144. RTL_W32 (RxConfig, tp->rx_config);
  1145. RTL_W32 (TxConfig, rtl8139_tx_config);
  1146. rtl_check_media (dev, 1);
  1147. if (tp->chipset >= CH_8139B) {
  1148. /* Disable magic packet scanning, which is enabled
  1149. * when PM is enabled in Config1. It can be reenabled
  1150. * via ETHTOOL_SWOL if desired. */
  1151. RTL_W8 (Config3, RTL_R8 (Config3) & ~Cfg3_Magic);
  1152. }
  1153. netdev_dbg(dev, "init buffer addresses\n");
  1154. /* Lock Config[01234] and BMCR register writes */
  1155. RTL_W8 (Cfg9346, Cfg9346_Lock);
  1156. /* init Tx buffer DMA addresses */
  1157. for (i = 0; i < NUM_TX_DESC; i++)
  1158. RTL_W32_F (TxAddr0 + (i * 4), tp->tx_bufs_dma + (tp->tx_buf[i] - tp->tx_bufs));
  1159. RTL_W32 (RxMissed, 0);
  1160. rtl8139_set_rx_mode (dev);
  1161. /* no early-rx interrupts */
  1162. RTL_W16 (MultiIntr, RTL_R16 (MultiIntr) & MultiIntrClear);
  1163. /* make sure RxTx has started */
  1164. tmp = RTL_R8 (ChipCmd);
  1165. if ((!(tmp & CmdRxEnb)) || (!(tmp & CmdTxEnb)))
  1166. RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
  1167. /* Enable all known interrupts by setting the interrupt mask. */
  1168. RTL_W16 (IntrMask, rtl8139_intr_mask);
  1169. }
  1170. /* Initialize the Rx and Tx rings, along with various 'dev' bits. */
  1171. static void rtl8139_init_ring (struct net_device *dev)
  1172. {
  1173. struct rtl8139_private *tp = netdev_priv(dev);
  1174. int i;
  1175. tp->cur_rx = 0;
  1176. tp->cur_tx = 0;
  1177. tp->dirty_tx = 0;
  1178. for (i = 0; i < NUM_TX_DESC; i++)
  1179. tp->tx_buf[i] = &tp->tx_bufs[i * TX_BUF_SIZE];
  1180. }
  1181. /* This must be global for CONFIG_8139TOO_TUNE_TWISTER case */
  1182. static int next_tick = 3 * HZ;
  1183. #ifndef CONFIG_8139TOO_TUNE_TWISTER
  1184. static inline void rtl8139_tune_twister (struct net_device *dev,
  1185. struct rtl8139_private *tp) {}
  1186. #else
  1187. enum TwisterParamVals {
  1188. PARA78_default = 0x78fa8388,
  1189. PARA7c_default = 0xcb38de43, /* param[0][3] */
  1190. PARA7c_xxx = 0xcb38de43,
  1191. };
  1192. static const unsigned long param[4][4] = {
  1193. {0xcb39de43, 0xcb39ce43, 0xfb38de03, 0xcb38de43},
  1194. {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
  1195. {0xcb39de43, 0xcb39ce43, 0xcb39ce83, 0xcb39ce83},
  1196. {0xbb39de43, 0xbb39ce43, 0xbb39ce83, 0xbb39ce83}
  1197. };
  1198. static void rtl8139_tune_twister (struct net_device *dev,
  1199. struct rtl8139_private *tp)
  1200. {
  1201. int linkcase;
  1202. void __iomem *ioaddr = tp->mmio_addr;
  1203. /* This is a complicated state machine to configure the "twister" for
  1204. impedance/echos based on the cable length.
  1205. All of this is magic and undocumented.
  1206. */
  1207. switch (tp->twistie) {
  1208. case 1:
  1209. if (RTL_R16 (CSCR) & CSCR_LinkOKBit) {
  1210. /* We have link beat, let us tune the twister. */
  1211. RTL_W16 (CSCR, CSCR_LinkDownOffCmd);
  1212. tp->twistie = 2; /* Change to state 2. */
  1213. next_tick = HZ / 10;
  1214. } else {
  1215. /* Just put in some reasonable defaults for when beat returns. */
  1216. RTL_W16 (CSCR, CSCR_LinkDownCmd);
  1217. RTL_W32 (FIFOTMS, 0x20); /* Turn on cable test mode. */
  1218. RTL_W32 (PARA78, PARA78_default);
  1219. RTL_W32 (PARA7c, PARA7c_default);
  1220. tp->twistie = 0; /* Bail from future actions. */
  1221. }
  1222. break;
  1223. case 2:
  1224. /* Read how long it took to hear the echo. */
  1225. linkcase = RTL_R16 (CSCR) & CSCR_LinkStatusBits;
  1226. if (linkcase == 0x7000)
  1227. tp->twist_row = 3;
  1228. else if (linkcase == 0x3000)
  1229. tp->twist_row = 2;
  1230. else if (linkcase == 0x1000)
  1231. tp->twist_row = 1;
  1232. else
  1233. tp->twist_row = 0;
  1234. tp->twist_col = 0;
  1235. tp->twistie = 3; /* Change to state 2. */
  1236. next_tick = HZ / 10;
  1237. break;
  1238. case 3:
  1239. /* Put out four tuning parameters, one per 100msec. */
  1240. if (tp->twist_col == 0)
  1241. RTL_W16 (FIFOTMS, 0);
  1242. RTL_W32 (PARA7c, param[(int) tp->twist_row]
  1243. [(int) tp->twist_col]);
  1244. next_tick = HZ / 10;
  1245. if (++tp->twist_col >= 4) {
  1246. /* For short cables we are done.
  1247. For long cables (row == 3) check for mistune. */
  1248. tp->twistie =
  1249. (tp->twist_row == 3) ? 4 : 0;
  1250. }
  1251. break;
  1252. case 4:
  1253. /* Special case for long cables: check for mistune. */
  1254. if ((RTL_R16 (CSCR) &
  1255. CSCR_LinkStatusBits) == 0x7000) {
  1256. tp->twistie = 0;
  1257. break;
  1258. } else {
  1259. RTL_W32 (PARA7c, 0xfb38de03);
  1260. tp->twistie = 5;
  1261. next_tick = HZ / 10;
  1262. }
  1263. break;
  1264. case 5:
  1265. /* Retune for shorter cable (column 2). */
  1266. RTL_W32 (FIFOTMS, 0x20);
  1267. RTL_W32 (PARA78, PARA78_default);
  1268. RTL_W32 (PARA7c, PARA7c_default);
  1269. RTL_W32 (FIFOTMS, 0x00);
  1270. tp->twist_row = 2;
  1271. tp->twist_col = 0;
  1272. tp->twistie = 3;
  1273. next_tick = HZ / 10;
  1274. break;
  1275. default:
  1276. /* do nothing */
  1277. break;
  1278. }
  1279. }
  1280. #endif /* CONFIG_8139TOO_TUNE_TWISTER */
  1281. static inline void rtl8139_thread_iter (struct net_device *dev,
  1282. struct rtl8139_private *tp,
  1283. void __iomem *ioaddr)
  1284. {
  1285. int mii_lpa;
  1286. mii_lpa = mdio_read (dev, tp->phys[0], MII_LPA);
  1287. if (!tp->mii.force_media && mii_lpa != 0xffff) {
  1288. int duplex = ((mii_lpa & LPA_100FULL) ||
  1289. (mii_lpa & 0x01C0) == 0x0040);
  1290. if (tp->mii.full_duplex != duplex) {
  1291. tp->mii.full_duplex = duplex;
  1292. if (mii_lpa) {
  1293. netdev_info(dev, "Setting %s-duplex based on MII #%d link partner ability of %04x\n",
  1294. tp->mii.full_duplex ? "full" : "half",
  1295. tp->phys[0], mii_lpa);
  1296. } else {
  1297. netdev_info(dev, "media is unconnected, link down, or incompatible connection\n");
  1298. }
  1299. #if 0
  1300. RTL_W8 (Cfg9346, Cfg9346_Unlock);
  1301. RTL_W8 (Config1, tp->mii.full_duplex ? 0x60 : 0x20);
  1302. RTL_W8 (Cfg9346, Cfg9346_Lock);
  1303. #endif
  1304. }
  1305. }
  1306. next_tick = HZ * 60;
  1307. rtl8139_tune_twister (dev, tp);
  1308. netdev_dbg(dev, "Media selection tick, Link partner %04x\n",
  1309. RTL_R16(NWayLPAR));
  1310. netdev_dbg(dev, "Other registers are IntMask %04x IntStatus %04x\n",
  1311. RTL_R16(IntrMask), RTL_R16(IntrStatus));
  1312. netdev_dbg(dev, "Chip config %02x %02x\n",
  1313. RTL_R8(Config0), RTL_R8(Config1));
  1314. }
  1315. static void rtl8139_thread (struct work_struct *work)
  1316. {
  1317. struct rtl8139_private *tp =
  1318. container_of(work, struct rtl8139_private, thread.work);
  1319. struct net_device *dev = tp->mii.dev;
  1320. unsigned long thr_delay = next_tick;
  1321. rtnl_lock();
  1322. if (!netif_running(dev))
  1323. goto out_unlock;
  1324. if (tp->watchdog_fired) {
  1325. tp->watchdog_fired = 0;
  1326. rtl8139_tx_timeout_task(work);
  1327. } else
  1328. rtl8139_thread_iter(dev, tp, tp->mmio_addr);
  1329. if (tp->have_thread)
  1330. schedule_delayed_work(&tp->thread, thr_delay);
  1331. out_unlock:
  1332. rtnl_unlock ();
  1333. }
  1334. static void rtl8139_start_thread(struct rtl8139_private *tp)
  1335. {
  1336. tp->twistie = 0;
  1337. if (tp->chipset == CH_8139_K)
  1338. tp->twistie = 1;
  1339. else if (tp->drv_flags & HAS_LNK_CHNG)
  1340. return;
  1341. tp->have_thread = 1;
  1342. tp->watchdog_fired = 0;
  1343. schedule_delayed_work(&tp->thread, next_tick);
  1344. }
  1345. static inline void rtl8139_tx_clear (struct rtl8139_private *tp)
  1346. {
  1347. tp->cur_tx = 0;
  1348. tp->dirty_tx = 0;
  1349. /* XXX account for unsent Tx packets in tp->stats.tx_dropped */
  1350. }
  1351. static void rtl8139_tx_timeout_task (struct work_struct *work)
  1352. {
  1353. struct rtl8139_private *tp =
  1354. container_of(work, struct rtl8139_private, thread.work);
  1355. struct net_device *dev = tp->mii.dev;
  1356. void __iomem *ioaddr = tp->mmio_addr;
  1357. int i;
  1358. u8 tmp8;
  1359. netdev_dbg(dev, "Transmit timeout, status %02x %04x %04x media %02x\n",
  1360. RTL_R8(ChipCmd), RTL_R16(IntrStatus),
  1361. RTL_R16(IntrMask), RTL_R8(MediaStatus));
  1362. /* Emit info to figure out what went wrong. */
  1363. netdev_dbg(dev, "Tx queue start entry %ld dirty entry %ld\n",
  1364. tp->cur_tx, tp->dirty_tx);
  1365. for (i = 0; i < NUM_TX_DESC; i++)
  1366. netdev_dbg(dev, "Tx descriptor %d is %08x%s\n",
  1367. i, RTL_R32(TxStatus0 + (i * 4)),
  1368. i == tp->dirty_tx % NUM_TX_DESC ?
  1369. " (queue head)" : "");
  1370. tp->xstats.tx_timeouts++;
  1371. /* disable Tx ASAP, if not already */
  1372. tmp8 = RTL_R8 (ChipCmd);
  1373. if (tmp8 & CmdTxEnb)
  1374. RTL_W8 (ChipCmd, CmdRxEnb);
  1375. spin_lock_bh(&tp->rx_lock);
  1376. /* Disable interrupts by clearing the interrupt mask. */
  1377. RTL_W16 (IntrMask, 0x0000);
  1378. /* Stop a shared interrupt from scavenging while we are. */
  1379. spin_lock_irq(&tp->lock);
  1380. rtl8139_tx_clear (tp);
  1381. spin_unlock_irq(&tp->lock);
  1382. /* ...and finally, reset everything */
  1383. if (netif_running(dev)) {
  1384. rtl8139_hw_start (dev);
  1385. netif_wake_queue (dev);
  1386. }
  1387. spin_unlock_bh(&tp->rx_lock);
  1388. }
  1389. static void rtl8139_tx_timeout (struct net_device *dev)
  1390. {
  1391. struct rtl8139_private *tp = netdev_priv(dev);
  1392. tp->watchdog_fired = 1;
  1393. if (!tp->have_thread) {
  1394. INIT_DELAYED_WORK(&tp->thread, rtl8139_thread);
  1395. schedule_delayed_work(&tp->thread, next_tick);
  1396. }
  1397. }
  1398. static netdev_tx_t rtl8139_start_xmit (struct sk_buff *skb,
  1399. struct net_device *dev)
  1400. {
  1401. struct rtl8139_private *tp = netdev_priv(dev);
  1402. void __iomem *ioaddr = tp->mmio_addr;
  1403. unsigned int entry;
  1404. unsigned int len = skb->len;
  1405. unsigned long flags;
  1406. /* Calculate the next Tx descriptor entry. */
  1407. entry = tp->cur_tx % NUM_TX_DESC;
  1408. /* Note: the chip doesn't have auto-pad! */
  1409. if (likely(len < TX_BUF_SIZE)) {
  1410. if (len < ETH_ZLEN)
  1411. memset(tp->tx_buf[entry], 0, ETH_ZLEN);
  1412. skb_copy_and_csum_dev(skb, tp->tx_buf[entry]);
  1413. dev_kfree_skb(skb);
  1414. } else {
  1415. dev_kfree_skb(skb);
  1416. dev->stats.tx_dropped++;
  1417. return NETDEV_TX_OK;
  1418. }
  1419. spin_lock_irqsave(&tp->lock, flags);
  1420. /*
  1421. * Writing to TxStatus triggers a DMA transfer of the data
  1422. * copied to tp->tx_buf[entry] above. Use a memory barrier
  1423. * to make sure that the device sees the updated data.
  1424. */
  1425. wmb();
  1426. RTL_W32_F (TxStatus0 + (entry * sizeof (u32)),
  1427. tp->tx_flag | max(len, (unsigned int)ETH_ZLEN));
  1428. tp->cur_tx++;
  1429. if ((tp->cur_tx - NUM_TX_DESC) == tp->dirty_tx)
  1430. netif_stop_queue (dev);
  1431. spin_unlock_irqrestore(&tp->lock, flags);
  1432. netif_dbg(tp, tx_queued, dev, "Queued Tx packet size %u to slot %d\n",
  1433. len, entry);
  1434. return NETDEV_TX_OK;
  1435. }
  1436. static void rtl8139_tx_interrupt (struct net_device *dev,
  1437. struct rtl8139_private *tp,
  1438. void __iomem *ioaddr)
  1439. {
  1440. unsigned long dirty_tx, tx_left;
  1441. assert (dev != NULL);
  1442. assert (ioaddr != NULL);
  1443. dirty_tx = tp->dirty_tx;
  1444. tx_left = tp->cur_tx - dirty_tx;
  1445. while (tx_left > 0) {
  1446. int entry = dirty_tx % NUM_TX_DESC;
  1447. int txstatus;
  1448. txstatus = RTL_R32 (TxStatus0 + (entry * sizeof (u32)));
  1449. if (!(txstatus & (TxStatOK | TxUnderrun | TxAborted)))
  1450. break; /* It still hasn't been Txed */
  1451. /* Note: TxCarrierLost is always asserted at 100mbps. */
  1452. if (txstatus & (TxOutOfWindow | TxAborted)) {
  1453. /* There was an major error, log it. */
  1454. netif_dbg(tp, tx_err, dev, "Transmit error, Tx status %08x\n",
  1455. txstatus);
  1456. dev->stats.tx_errors++;
  1457. if (txstatus & TxAborted) {
  1458. dev->stats.tx_aborted_errors++;
  1459. RTL_W32 (TxConfig, TxClearAbt);
  1460. RTL_W16 (IntrStatus, TxErr);
  1461. wmb();
  1462. }
  1463. if (txstatus & TxCarrierLost)
  1464. dev->stats.tx_carrier_errors++;
  1465. if (txstatus & TxOutOfWindow)
  1466. dev->stats.tx_window_errors++;
  1467. } else {
  1468. if (txstatus & TxUnderrun) {
  1469. /* Add 64 to the Tx FIFO threshold. */
  1470. if (tp->tx_flag < 0x00300000)
  1471. tp->tx_flag += 0x00020000;
  1472. dev->stats.tx_fifo_errors++;
  1473. }
  1474. dev->stats.collisions += (txstatus >> 24) & 15;
  1475. dev->stats.tx_bytes += txstatus & 0x7ff;
  1476. dev->stats.tx_packets++;
  1477. }
  1478. dirty_tx++;
  1479. tx_left--;
  1480. }
  1481. #ifndef RTL8139_NDEBUG
  1482. if (tp->cur_tx - dirty_tx > NUM_TX_DESC) {
  1483. netdev_err(dev, "Out-of-sync dirty pointer, %ld vs. %ld\n",
  1484. dirty_tx, tp->cur_tx);
  1485. dirty_tx += NUM_TX_DESC;
  1486. }
  1487. #endif /* RTL8139_NDEBUG */
  1488. /* only wake the queue if we did work, and the queue is stopped */
  1489. if (tp->dirty_tx != dirty_tx) {
  1490. tp->dirty_tx = dirty_tx;
  1491. mb();
  1492. netif_wake_queue (dev);
  1493. }
  1494. }
  1495. /* TODO: clean this up! Rx reset need not be this intensive */
  1496. static void rtl8139_rx_err (u32 rx_status, struct net_device *dev,
  1497. struct rtl8139_private *tp, void __iomem *ioaddr)
  1498. {
  1499. u8 tmp8;
  1500. #ifdef CONFIG_8139_OLD_RX_RESET
  1501. int tmp_work;
  1502. #endif
  1503. netif_dbg(tp, rx_err, dev, "Ethernet frame had errors, status %08x\n",
  1504. rx_status);
  1505. dev->stats.rx_errors++;
  1506. if (!(rx_status & RxStatusOK)) {
  1507. if (rx_status & RxTooLong) {
  1508. netdev_dbg(dev, "Oversized Ethernet frame, status %04x!\n",
  1509. rx_status);
  1510. /* A.C.: The chip hangs here. */
  1511. }
  1512. if (rx_status & (RxBadSymbol | RxBadAlign))
  1513. dev->stats.rx_frame_errors++;
  1514. if (rx_status & (RxRunt | RxTooLong))
  1515. dev->stats.rx_length_errors++;
  1516. if (rx_status & RxCRCErr)
  1517. dev->stats.rx_crc_errors++;
  1518. } else {
  1519. tp->xstats.rx_lost_in_ring++;
  1520. }
  1521. #ifndef CONFIG_8139_OLD_RX_RESET
  1522. tmp8 = RTL_R8 (ChipCmd);
  1523. RTL_W8 (ChipCmd, tmp8 & ~CmdRxEnb);
  1524. RTL_W8 (ChipCmd, tmp8);
  1525. RTL_W32 (RxConfig, tp->rx_config);
  1526. tp->cur_rx = 0;
  1527. #else
  1528. /* Reset the receiver, based on RealTek recommendation. (Bug?) */
  1529. /* disable receive */
  1530. RTL_W8_F (ChipCmd, CmdTxEnb);
  1531. tmp_work = 200;
  1532. while (--tmp_work > 0) {
  1533. udelay(1);
  1534. tmp8 = RTL_R8 (ChipCmd);
  1535. if (!(tmp8 & CmdRxEnb))
  1536. break;
  1537. }
  1538. if (tmp_work <= 0)
  1539. netdev_warn(dev, "rx stop wait too long\n");
  1540. /* restart receive */
  1541. tmp_work = 200;
  1542. while (--tmp_work > 0) {
  1543. RTL_W8_F (ChipCmd, CmdRxEnb | CmdTxEnb);
  1544. udelay(1);
  1545. tmp8 = RTL_R8 (ChipCmd);
  1546. if ((tmp8 & CmdRxEnb) && (tmp8 & CmdTxEnb))
  1547. break;
  1548. }
  1549. if (tmp_work <= 0)
  1550. netdev_warn(dev, "tx/rx enable wait too long\n");
  1551. /* and reinitialize all rx related registers */
  1552. RTL_W8_F (Cfg9346, Cfg9346_Unlock);
  1553. /* Must enable Tx/Rx before setting transfer thresholds! */
  1554. RTL_W8 (ChipCmd, CmdRxEnb | CmdTxEnb);
  1555. tp->rx_config = rtl8139_rx_config | AcceptBroadcast | AcceptMyPhys;
  1556. RTL_W32 (RxConfig, tp->rx_config);
  1557. tp->cur_rx = 0;
  1558. netdev_dbg(dev, "init buffer addresses\n");
  1559. /* Lock Config[01234] and BMCR register writes */
  1560. RTL_W8 (Cfg9346, Cfg9346_Lock);
  1561. /* init Rx ring buffer DMA address */
  1562. RTL_W32_F (RxBuf, tp->rx_ring_dma);
  1563. /* A.C.: Reset the multicast list. */
  1564. __set_rx_mode (dev);
  1565. #endif
  1566. }
  1567. #if RX_BUF_IDX == 3
  1568. static inline void wrap_copy(struct sk_buff *skb, const unsigned char *ring,
  1569. u32 offset, unsigned int size)
  1570. {
  1571. u32 left = RX_BUF_LEN - offset;
  1572. if (size > left) {
  1573. skb_copy_to_linear_data(skb, ring + offset, left);
  1574. skb_copy_to_linear_data_offset(skb, left, ring, size - left);
  1575. } else
  1576. skb_copy_to_linear_data(skb, ring + offset, size);
  1577. }
  1578. #endif
  1579. static void rtl8139_isr_ack(struct rtl8139_private *tp)
  1580. {
  1581. void __iomem *ioaddr = tp->mmio_addr;
  1582. u16 status;
  1583. status = RTL_R16 (IntrStatus) & RxAckBits;
  1584. /* Clear out errors and receive interrupts */
  1585. if (likely(status != 0)) {
  1586. if (unlikely(status & (RxFIFOOver | RxOverflow))) {
  1587. tp->dev->stats.rx_errors++;
  1588. if (status & RxFIFOOver)
  1589. tp->dev->stats.rx_fifo_errors++;
  1590. }
  1591. RTL_W16_F (IntrStatus, RxAckBits);
  1592. }
  1593. }
  1594. static int rtl8139_rx(struct net_device *dev, struct rtl8139_private *tp,
  1595. int budget)
  1596. {
  1597. void __iomem *ioaddr = tp->mmio_addr;
  1598. int received = 0;
  1599. unsigned char *rx_ring = tp->rx_ring;
  1600. unsigned int cur_rx = tp->cur_rx;
  1601. unsigned int rx_size = 0;
  1602. netdev_dbg(dev, "In %s(), current %04x BufAddr %04x, free to %04x, Cmd %02x\n",
  1603. __func__, (u16)cur_rx,
  1604. RTL_R16(RxBufAddr), RTL_R16(RxBufPtr), RTL_R8(ChipCmd));
  1605. while (netif_running(dev) && received < budget &&
  1606. (RTL_R8 (ChipCmd) & RxBufEmpty) == 0) {
  1607. u32 ring_offset = cur_rx % RX_BUF_LEN;
  1608. u32 rx_status;
  1609. unsigned int pkt_size;
  1610. struct sk_buff *skb;
  1611. rmb();
  1612. /* read size+status of next frame from DMA ring buffer */
  1613. rx_status = le32_to_cpu (*(__le32 *) (rx_ring + ring_offset));
  1614. rx_size = rx_status >> 16;
  1615. pkt_size = rx_size - 4;
  1616. netif_dbg(tp, rx_status, dev, "%s() status %04x, size %04x, cur %04x\n",
  1617. __func__, rx_status, rx_size, cur_rx);
  1618. #if RTL8139_DEBUG > 2
  1619. print_hex_dump(KERN_DEBUG, "Frame contents: ",
  1620. DUMP_PREFIX_OFFSET, 16, 1,
  1621. &rx_ring[ring_offset], 70, true);
  1622. #endif
  1623. /* Packet copy from FIFO still in progress.
  1624. * Theoretically, this should never happen
  1625. * since EarlyRx is disabled.
  1626. */
  1627. if (unlikely(rx_size == 0xfff0)) {
  1628. if (!tp->fifo_copy_timeout)
  1629. tp->fifo_copy_timeout = jiffies + 2;
  1630. else if (time_after(jiffies, tp->fifo_copy_timeout)) {
  1631. netdev_dbg(dev, "hung FIFO. Reset\n");
  1632. rx_size = 0;
  1633. goto no_early_rx;
  1634. }
  1635. netif_dbg(tp, intr, dev, "fifo copy in progress\n");
  1636. tp->xstats.early_rx++;
  1637. break;
  1638. }
  1639. no_early_rx:
  1640. tp->fifo_copy_timeout = 0;
  1641. /* If Rx err or invalid rx_size/rx_status received
  1642. * (which happens if we get lost in the ring),
  1643. * Rx process gets reset, so we abort any further
  1644. * Rx processing.
  1645. */
  1646. if (unlikely((rx_size > (MAX_ETH_FRAME_SIZE+4)) ||
  1647. (rx_size < 8) ||
  1648. (!(rx_status & RxStatusOK)))) {
  1649. rtl8139_rx_err (rx_status, dev, tp, ioaddr);
  1650. received = -1;
  1651. goto out;
  1652. }
  1653. /* Malloc up new buffer, compatible with net-2e. */
  1654. /* Omit the four octet CRC from the length. */
  1655. skb = netdev_alloc_skb_ip_align(dev, pkt_size);
  1656. if (likely(skb)) {
  1657. #if RX_BUF_IDX == 3
  1658. wrap_copy(skb, rx_ring, ring_offset+4, pkt_size);
  1659. #else
  1660. skb_copy_to_linear_data (skb, &rx_ring[ring_offset + 4], pkt_size);
  1661. #endif
  1662. skb_put (skb, pkt_size);
  1663. skb->protocol = eth_type_trans (skb, dev);
  1664. dev->stats.rx_bytes += pkt_size;
  1665. dev->stats.rx_packets++;
  1666. netif_receive_skb (skb);
  1667. } else {
  1668. if (net_ratelimit())
  1669. netdev_warn(dev, "Memory squeeze, dropping packet\n");
  1670. dev->stats.rx_dropped++;
  1671. }
  1672. received++;
  1673. cur_rx = (cur_rx + rx_size + 4 + 3) & ~3;
  1674. RTL_W16 (RxBufPtr, (u16) (cur_rx - 16));
  1675. rtl8139_isr_ack(tp);
  1676. }
  1677. if (unlikely(!received || rx_size == 0xfff0))
  1678. rtl8139_isr_ack(tp);
  1679. netdev_dbg(dev, "Done %s(), current %04x BufAddr %04x, free to %04x, Cmd %02x\n",
  1680. __func__, cur_rx,
  1681. RTL_R16(RxBufAddr), RTL_R16(RxBufPtr), RTL_R8(ChipCmd));
  1682. tp->cur_rx = cur_rx;
  1683. /*
  1684. * The receive buffer should be mostly empty.
  1685. * Tell NAPI to reenable the Rx irq.
  1686. */
  1687. if (tp->fifo_copy_timeout)
  1688. received = budget;
  1689. out:
  1690. return received;
  1691. }
  1692. static void rtl8139_weird_interrupt (struct net_device *dev,
  1693. struct rtl8139_private *tp,
  1694. void __iomem *ioaddr,
  1695. int status, int link_changed)
  1696. {
  1697. netdev_dbg(dev, "Abnormal interrupt, status %08x\n", status);
  1698. assert (dev != NULL);
  1699. assert (tp != NULL);
  1700. assert (ioaddr != NULL);
  1701. /* Update the error count. */
  1702. dev->stats.rx_missed_errors += RTL_R32 (RxMissed);
  1703. RTL_W32 (RxMissed, 0);
  1704. if ((status & RxUnderrun) && link_changed &&
  1705. (tp->drv_flags & HAS_LNK_CHNG)) {
  1706. rtl_check_media(dev, 0);
  1707. status &= ~RxUnderrun;
  1708. }
  1709. if (status & (RxUnderrun | RxErr))
  1710. dev->stats.rx_errors++;
  1711. if (status & PCSTimeout)
  1712. dev->stats.rx_length_errors++;
  1713. if (status & RxUnderrun)
  1714. dev->stats.rx_fifo_errors++;
  1715. if (status & PCIErr) {
  1716. u16 pci_cmd_status;
  1717. pci_read_config_word (tp->pci_dev, PCI_STATUS, &pci_cmd_status);
  1718. pci_write_config_word (tp->pci_dev, PCI_STATUS, pci_cmd_status);
  1719. netdev_err(dev, "PCI Bus error %04x\n", pci_cmd_status);
  1720. }
  1721. }
  1722. static int rtl8139_poll(struct napi_struct *napi, int budget)
  1723. {
  1724. struct rtl8139_private *tp = container_of(napi, struct rtl8139_private, napi);
  1725. struct net_device *dev = tp->dev;
  1726. void __iomem *ioaddr = tp->mmio_addr;
  1727. int work_done;
  1728. spin_lock(&tp->rx_lock);
  1729. work_done = 0;
  1730. if (likely(RTL_R16(IntrStatus) & RxAckBits))
  1731. work_done += rtl8139_rx(dev, tp, budget);
  1732. if (work_done < budget) {
  1733. unsigned long flags;
  1734. /*
  1735. * Order is important since data can get interrupted
  1736. * again when we think we are done.
  1737. */
  1738. spin_lock_irqsave(&tp->lock, flags);
  1739. __napi_complete(napi);
  1740. RTL_W16_F(IntrMask, rtl8139_intr_mask);
  1741. spin_unlock_irqrestore(&tp->lock, flags);
  1742. }
  1743. spin_unlock(&tp->rx_lock);
  1744. return work_done;
  1745. }
  1746. /* The interrupt handler does all of the Rx thread work and cleans up
  1747. after the Tx thread. */
  1748. static irqreturn_t rtl8139_interrupt (int irq, void *dev_instance)
  1749. {
  1750. struct net_device *dev = (struct net_device *) dev_instance;
  1751. struct rtl8139_private *tp = netdev_priv(dev);
  1752. void __iomem *ioaddr = tp->mmio_addr;
  1753. u16 status, ackstat;
  1754. int link_changed = 0; /* avoid bogus "uninit" warning */
  1755. int handled = 0;
  1756. spin_lock (&tp->lock);
  1757. status = RTL_R16 (IntrStatus);
  1758. /* shared irq? */
  1759. if (unlikely((status & rtl8139_intr_mask) == 0))
  1760. goto out;
  1761. handled = 1;
  1762. /* h/w no longer present (hotplug?) or major error, bail */
  1763. if (unlikely(status == 0xFFFF))
  1764. goto out;
  1765. /* close possible race's with dev_close */
  1766. if (unlikely(!netif_running(dev))) {
  1767. RTL_W16 (IntrMask, 0);
  1768. goto out;
  1769. }
  1770. /* Acknowledge all of the current interrupt sources ASAP, but
  1771. an first get an additional status bit from CSCR. */
  1772. if (unlikely(status & RxUnderrun))
  1773. link_changed = RTL_R16 (CSCR) & CSCR_LinkChangeBit;
  1774. ackstat = status & ~(RxAckBits | TxErr);
  1775. if (ackstat)
  1776. RTL_W16 (IntrStatus, ackstat);
  1777. /* Receive packets are processed by poll routine.
  1778. If not running start it now. */
  1779. if (status & RxAckBits){
  1780. if (napi_schedule_prep(&tp->napi)) {
  1781. RTL_W16_F (IntrMask, rtl8139_norx_intr_mask);
  1782. __napi_schedule(&tp->napi);
  1783. }
  1784. }
  1785. /* Check uncommon events with one test. */
  1786. if (unlikely(status & (PCIErr | PCSTimeout | RxUnderrun | RxErr)))
  1787. rtl8139_weird_interrupt (dev, tp, ioaddr,
  1788. status, link_changed);
  1789. if (status & (TxOK | TxErr)) {
  1790. rtl8139_tx_interrupt (dev, tp, ioaddr);
  1791. if (status & TxErr)
  1792. RTL_W16 (IntrStatus, TxErr);
  1793. }
  1794. out:
  1795. spin_unlock (&tp->lock);
  1796. netdev_dbg(dev, "exiting interrupt, intr_status=%#4.4x\n",
  1797. RTL_R16(IntrStatus));
  1798. return IRQ_RETVAL(handled);
  1799. }
  1800. #ifdef CONFIG_NET_POLL_CONTROLLER
  1801. /*
  1802. * Polling receive - used by netconsole and other diagnostic tools
  1803. * to allow network i/o with interrupts disabled.
  1804. */
  1805. static void rtl8139_poll_controller(struct net_device *dev)
  1806. {
  1807. disable_irq(dev->irq);
  1808. rtl8139_interrupt(dev->irq, dev);
  1809. enable_irq(dev->irq);
  1810. }
  1811. #endif
  1812. static int rtl8139_set_mac_address(struct net_device *dev, void *p)
  1813. {
  1814. struct rtl8139_private *tp = netdev_priv(dev);
  1815. void __iomem *ioaddr = tp->mmio_addr;
  1816. struct sockaddr *addr = p;
  1817. if (!is_valid_ether_addr(addr->sa_data))
  1818. return -EADDRNOTAVAIL;
  1819. memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
  1820. spin_lock_irq(&tp->lock);
  1821. RTL_W8_F(Cfg9346, Cfg9346_Unlock);
  1822. RTL_W32_F(MAC0 + 0, cpu_to_le32 (*(u32 *) (dev->dev_addr + 0)));
  1823. RTL_W32_F(MAC0 + 4, cpu_to_le32 (*(u32 *) (dev->dev_addr + 4)));
  1824. RTL_W8_F(Cfg9346, Cfg9346_Lock);
  1825. spin_unlock_irq(&tp->lock);
  1826. return 0;
  1827. }
  1828. static int rtl8139_close (struct net_device *dev)
  1829. {
  1830. struct rtl8139_private *tp = netdev_priv(dev);
  1831. void __iomem *ioaddr = tp->mmio_addr;
  1832. unsigned long flags;
  1833. netif_stop_queue(dev);
  1834. napi_disable(&tp->napi);
  1835. netif_dbg(tp, ifdown, dev, "Shutting down ethercard, status was 0x%04x\n",
  1836. RTL_R16(IntrStatus));
  1837. spin_lock_irqsave (&tp->lock, flags);
  1838. /* Stop the chip's Tx and Rx DMA processes. */
  1839. RTL_W8 (ChipCmd, 0);
  1840. /* Disable interrupts by clearing the interrupt mask. */
  1841. RTL_W16 (IntrMask, 0);
  1842. /* Update the error counts. */
  1843. dev->stats.rx_missed_errors += RTL_R32 (RxMissed);
  1844. RTL_W32 (RxMissed, 0);
  1845. spin_unlock_irqrestore (&tp->lock, flags);
  1846. free_irq (dev->irq, dev);
  1847. rtl8139_tx_clear (tp);
  1848. dma_free_coherent(&tp->pci_dev->dev, RX_BUF_TOT_LEN,
  1849. tp->rx_ring, tp->rx_ring_dma);
  1850. dma_free_coherent(&tp->pci_dev->dev, TX_BUF_TOT_LEN,
  1851. tp->tx_bufs, tp->tx_bufs_dma);
  1852. tp->rx_ring = NULL;
  1853. tp->tx_bufs = NULL;
  1854. /* Green! Put the chip in low-power mode. */
  1855. RTL_W8 (Cfg9346, Cfg9346_Unlock);
  1856. if (rtl_chip_info[tp->chipset].flags & HasHltClk)
  1857. RTL_W8 (HltClk, 'H'); /* 'R' would leave the clock running. */
  1858. return 0;
  1859. }
  1860. /* Get the ethtool Wake-on-LAN settings. Assumes that wol points to
  1861. kernel memory, *wol has been initialized as {ETHTOOL_GWOL}, and
  1862. other threads or interrupts aren't messing with the 8139. */
  1863. static void rtl8139_get_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1864. {
  1865. struct rtl8139_private *tp = netdev_priv(dev);
  1866. void __iomem *ioaddr = tp->mmio_addr;
  1867. spin_lock_irq(&tp->lock);
  1868. if (rtl_chip_info[tp->chipset].flags & HasLWake) {
  1869. u8 cfg3 = RTL_R8 (Config3);
  1870. u8 cfg5 = RTL_R8 (Config5);
  1871. wol->supported = WAKE_PHY | WAKE_MAGIC
  1872. | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST;
  1873. wol->wolopts = 0;
  1874. if (cfg3 & Cfg3_LinkUp)
  1875. wol->wolopts |= WAKE_PHY;
  1876. if (cfg3 & Cfg3_Magic)
  1877. wol->wolopts |= WAKE_MAGIC;
  1878. /* (KON)FIXME: See how netdev_set_wol() handles the
  1879. following constants. */
  1880. if (cfg5 & Cfg5_UWF)
  1881. wol->wolopts |= WAKE_UCAST;
  1882. if (cfg5 & Cfg5_MWF)
  1883. wol->wolopts |= WAKE_MCAST;
  1884. if (cfg5 & Cfg5_BWF)
  1885. wol->wolopts |= WAKE_BCAST;
  1886. }
  1887. spin_unlock_irq(&tp->lock);
  1888. }
  1889. /* Set the ethtool Wake-on-LAN settings. Return 0 or -errno. Assumes
  1890. that wol points to kernel memory and other threads or interrupts
  1891. aren't messing with the 8139. */
  1892. static int rtl8139_set_wol(struct net_device *dev, struct ethtool_wolinfo *wol)
  1893. {
  1894. struct rtl8139_private *tp = netdev_priv(dev);
  1895. void __iomem *ioaddr = tp->mmio_addr;
  1896. u32 support;
  1897. u8 cfg3, cfg5;
  1898. support = ((rtl_chip_info[tp->chipset].flags & HasLWake)
  1899. ? (WAKE_PHY | WAKE_MAGIC
  1900. | WAKE_UCAST | WAKE_MCAST | WAKE_BCAST)
  1901. : 0);
  1902. if (wol->wolopts & ~support)
  1903. return -EINVAL;
  1904. spin_lock_irq(&tp->lock);
  1905. cfg3 = RTL_R8 (Config3) & ~(Cfg3_LinkUp | Cfg3_Magic);
  1906. if (wol->wolopts & WAKE_PHY)
  1907. cfg3 |= Cfg3_LinkUp;
  1908. if (wol->wolopts & WAKE_MAGIC)
  1909. cfg3 |= Cfg3_Magic;
  1910. RTL_W8 (Cfg9346, Cfg9346_Unlock);
  1911. RTL_W8 (Config3, cfg3);
  1912. RTL_W8 (Cfg9346, Cfg9346_Lock);
  1913. cfg5 = RTL_R8 (Config5) & ~(Cfg5_UWF | Cfg5_MWF | Cfg5_BWF);
  1914. /* (KON)FIXME: These are untested. We may have to set the
  1915. CRC0, Wakeup0 and LSBCRC0 registers too, but I have no
  1916. documentation. */
  1917. if (wol->wolopts & WAKE_UCAST)
  1918. cfg5 |= Cfg5_UWF;
  1919. if (wol->wolopts & WAKE_MCAST)
  1920. cfg5 |= Cfg5_MWF;
  1921. if (wol->wolopts & WAKE_BCAST)
  1922. cfg5 |= Cfg5_BWF;
  1923. RTL_W8 (Config5, cfg5); /* need not unlock via Cfg9346 */
  1924. spin_unlock_irq(&tp->lock);
  1925. return 0;
  1926. }
  1927. static void rtl8139_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  1928. {
  1929. struct rtl8139_private *tp = netdev_priv(dev);
  1930. strcpy(info->driver, DRV_NAME);
  1931. strcpy(info->version, DRV_VERSION);
  1932. strcpy(info->bus_info, pci_name(tp->pci_dev));
  1933. info->regdump_len = tp->regs_len;
  1934. }
  1935. static int rtl8139_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1936. {
  1937. struct rtl8139_private *tp = netdev_priv(dev);
  1938. spin_lock_irq(&tp->lock);
  1939. mii_ethtool_gset(&tp->mii, cmd);
  1940. spin_unlock_irq(&tp->lock);
  1941. return 0;
  1942. }
  1943. static int rtl8139_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  1944. {
  1945. struct rtl8139_private *tp = netdev_priv(dev);
  1946. int rc;
  1947. spin_lock_irq(&tp->lock);
  1948. rc = mii_ethtool_sset(&tp->mii, cmd);
  1949. spin_unlock_irq(&tp->lock);
  1950. return rc;
  1951. }
  1952. static int rtl8139_nway_reset(struct net_device *dev)
  1953. {
  1954. struct rtl8139_private *tp = netdev_priv(dev);
  1955. return mii_nway_restart(&tp->mii);
  1956. }
  1957. static u32 rtl8139_get_link(struct net_device *dev)
  1958. {
  1959. struct rtl8139_private *tp = netdev_priv(dev);
  1960. return mii_link_ok(&tp->mii);
  1961. }
  1962. static u32 rtl8139_get_msglevel(struct net_device *dev)
  1963. {
  1964. struct rtl8139_private *tp = netdev_priv(dev);
  1965. return tp->msg_enable;
  1966. }
  1967. static void rtl8139_set_msglevel(struct net_device *dev, u32 datum)
  1968. {
  1969. struct rtl8139_private *tp = netdev_priv(dev);
  1970. tp->msg_enable = datum;
  1971. }
  1972. static int rtl8139_get_regs_len(struct net_device *dev)
  1973. {
  1974. struct rtl8139_private *tp;
  1975. /* TODO: we are too slack to do reg dumping for pio, for now */
  1976. if (use_io)
  1977. return 0;
  1978. tp = netdev_priv(dev);
  1979. return tp->regs_len;
  1980. }
  1981. static void rtl8139_get_regs(struct net_device *dev, struct ethtool_regs *regs, void *regbuf)
  1982. {
  1983. struct rtl8139_private *tp;
  1984. /* TODO: we are too slack to do reg dumping for pio, for now */
  1985. if (use_io)
  1986. return;
  1987. tp = netdev_priv(dev);
  1988. regs->version = RTL_REGS_VER;
  1989. spin_lock_irq(&tp->lock);
  1990. memcpy_fromio(regbuf, tp->mmio_addr, regs->len);
  1991. spin_unlock_irq(&tp->lock);
  1992. }
  1993. static int rtl8139_get_sset_count(struct net_device *dev, int sset)
  1994. {
  1995. switch (sset) {
  1996. case ETH_SS_STATS:
  1997. return RTL_NUM_STATS;
  1998. default:
  1999. return -EOPNOTSUPP;
  2000. }
  2001. }
  2002. static void rtl8139_get_ethtool_stats(struct net_device *dev, struct ethtool_stats *stats, u64 *data)
  2003. {
  2004. struct rtl8139_private *tp = netdev_priv(dev);
  2005. data[0] = tp->xstats.early_rx;
  2006. data[1] = tp->xstats.tx_buf_mapped;
  2007. data[2] = tp->xstats.tx_timeouts;
  2008. data[3] = tp->xstats.rx_lost_in_ring;
  2009. }
  2010. static void rtl8139_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  2011. {
  2012. memcpy(data, ethtool_stats_keys, sizeof(ethtool_stats_keys));
  2013. }
  2014. static const struct ethtool_ops rtl8139_ethtool_ops = {
  2015. .get_drvinfo = rtl8139_get_drvinfo,
  2016. .get_settings = rtl8139_get_settings,
  2017. .set_settings = rtl8139_set_settings,
  2018. .get_regs_len = rtl8139_get_regs_len,
  2019. .get_regs = rtl8139_get_regs,
  2020. .nway_reset = rtl8139_nway_reset,
  2021. .get_link = rtl8139_get_link,
  2022. .get_msglevel = rtl8139_get_msglevel,
  2023. .set_msglevel = rtl8139_set_msglevel,
  2024. .get_wol = rtl8139_get_wol,
  2025. .set_wol = rtl8139_set_wol,
  2026. .get_strings = rtl8139_get_strings,
  2027. .get_sset_count = rtl8139_get_sset_count,
  2028. .get_ethtool_stats = rtl8139_get_ethtool_stats,
  2029. };
  2030. static int netdev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
  2031. {
  2032. struct rtl8139_private *tp = netdev_priv(dev);
  2033. int rc;
  2034. if (!netif_running(dev))
  2035. return -EINVAL;
  2036. spin_lock_irq(&tp->lock);
  2037. rc = generic_mii_ioctl(&tp->mii, if_mii(rq), cmd, NULL);
  2038. spin_unlock_irq(&tp->lock);
  2039. return rc;
  2040. }
  2041. static struct net_device_stats *rtl8139_get_stats (struct net_device *dev)
  2042. {
  2043. struct rtl8139_private *tp = netdev_priv(dev);
  2044. void __iomem *ioaddr = tp->mmio_addr;
  2045. unsigned long flags;
  2046. if (netif_running(dev)) {
  2047. spin_lock_irqsave (&tp->lock, flags);
  2048. dev->stats.rx_missed_errors += RTL_R32 (RxMissed);
  2049. RTL_W32 (RxMissed, 0);
  2050. spin_unlock_irqrestore (&tp->lock, flags);
  2051. }
  2052. return &dev->stats;
  2053. }
  2054. /* Set or clear the multicast filter for this adaptor.
  2055. This routine is not state sensitive and need not be SMP locked. */
  2056. static void __set_rx_mode (struct net_device *dev)
  2057. {
  2058. struct rtl8139_private *tp = netdev_priv(dev);
  2059. void __iomem *ioaddr = tp->mmio_addr;
  2060. u32 mc_filter[2]; /* Multicast hash filter */
  2061. int rx_mode;
  2062. u32 tmp;
  2063. netdev_dbg(dev, "rtl8139_set_rx_mode(%04x) done -- Rx config %08x\n",
  2064. dev->flags, RTL_R32(RxConfig));
  2065. /* Note: do not reorder, GCC is clever about common statements. */
  2066. if (dev->flags & IFF_PROMISC) {
  2067. rx_mode =
  2068. AcceptBroadcast | AcceptMulticast | AcceptMyPhys |
  2069. AcceptAllPhys;
  2070. mc_filter[1] = mc_filter[0] = 0xffffffff;
  2071. } else if ((netdev_mc_count(dev) > multicast_filter_limit) ||
  2072. (dev->flags & IFF_ALLMULTI)) {
  2073. /* Too many to filter perfectly -- accept all multicasts. */
  2074. rx_mode = AcceptBroadcast | AcceptMulticast | AcceptMyPhys;
  2075. mc_filter[1] = mc_filter[0] = 0xffffffff;
  2076. } else {
  2077. struct netdev_hw_addr *ha;
  2078. rx_mode = AcceptBroadcast | AcceptMyPhys;
  2079. mc_filter[1] = mc_filter[0] = 0;
  2080. netdev_for_each_mc_addr(ha, dev) {
  2081. int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  2082. mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
  2083. rx_mode |= AcceptMulticast;
  2084. }
  2085. }
  2086. /* We can safely update without stopping the chip. */
  2087. tmp = rtl8139_rx_config | rx_mode;
  2088. if (tp->rx_config != tmp) {
  2089. RTL_W32_F (RxConfig, tmp);
  2090. tp->rx_config = tmp;
  2091. }
  2092. RTL_W32_F (MAR0 + 0, mc_filter[0]);
  2093. RTL_W32_F (MAR0 + 4, mc_filter[1]);
  2094. }
  2095. static void rtl8139_set_rx_mode (struct net_device *dev)
  2096. {
  2097. unsigned long flags;
  2098. struct rtl8139_private *tp = netdev_priv(dev);
  2099. spin_lock_irqsave (&tp->lock, flags);
  2100. __set_rx_mode(dev);
  2101. spin_unlock_irqrestore (&tp->lock, flags);
  2102. }
  2103. #ifdef CONFIG_PM
  2104. static int rtl8139_suspend (struct pci_dev *pdev, pm_message_t state)
  2105. {
  2106. struct net_device *dev = pci_get_drvdata (pdev);
  2107. struct rtl8139_private *tp = netdev_priv(dev);
  2108. void __iomem *ioaddr = tp->mmio_addr;
  2109. unsigned long flags;
  2110. pci_save_state (pdev);
  2111. if (!netif_running (dev))
  2112. return 0;
  2113. netif_device_detach (dev);
  2114. spin_lock_irqsave (&tp->lock, flags);
  2115. /* Disable interrupts, stop Tx and Rx. */
  2116. RTL_W16 (IntrMask, 0);
  2117. RTL_W8 (ChipCmd, 0);
  2118. /* Update the error counts. */
  2119. dev->stats.rx_missed_errors += RTL_R32 (RxMissed);
  2120. RTL_W32 (RxMissed, 0);
  2121. spin_unlock_irqrestore (&tp->lock, flags);
  2122. pci_set_power_state (pdev, PCI_D3hot);
  2123. return 0;
  2124. }
  2125. static int rtl8139_resume (struct pci_dev *pdev)
  2126. {
  2127. struct net_device *dev = pci_get_drvdata (pdev);
  2128. pci_restore_state (pdev);
  2129. if (!netif_running (dev))
  2130. return 0;
  2131. pci_set_power_state (pdev, PCI_D0);
  2132. rtl8139_init_ring (dev);
  2133. rtl8139_hw_start (dev);
  2134. netif_device_attach (dev);
  2135. return 0;
  2136. }
  2137. #endif /* CONFIG_PM */
  2138. static struct pci_driver rtl8139_pci_driver = {
  2139. .name = DRV_NAME,
  2140. .id_table = rtl8139_pci_tbl,
  2141. .probe = rtl8139_init_one,
  2142. .remove = __devexit_p(rtl8139_remove_one),
  2143. #ifdef CONFIG_PM
  2144. .suspend = rtl8139_suspend,
  2145. .resume = rtl8139_resume,
  2146. #endif /* CONFIG_PM */
  2147. };
  2148. static int __init rtl8139_init_module (void)
  2149. {
  2150. /* when we're a module, we always print a version message,
  2151. * even if no 8139 board is found.
  2152. */
  2153. #ifdef MODULE
  2154. pr_info(RTL8139_DRIVER_NAME "\n");
  2155. #endif
  2156. return pci_register_driver(&rtl8139_pci_driver);
  2157. }
  2158. static void __exit rtl8139_cleanup_module (void)
  2159. {
  2160. pci_unregister_driver (&rtl8139_pci_driver);
  2161. }
  2162. module_init(rtl8139_init_module);
  2163. module_exit(rtl8139_cleanup_module);