e100.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2006 Intel Corporation. */
  3. /*
  4. * e100.c: Intel(R) PRO/100 ethernet driver
  5. *
  6. * (Re)written 2003 by scott.feldman@intel.com. Based loosely on
  7. * original e100 driver, but better described as a munging of
  8. * e100, e1000, eepro100, tg3, 8139cp, and other drivers.
  9. *
  10. * References:
  11. * Intel 8255x 10/100 Mbps Ethernet Controller Family,
  12. * Open Source Software Developers Manual,
  13. * http://sourceforge.net/projects/e1000
  14. *
  15. *
  16. * Theory of Operation
  17. *
  18. * I. General
  19. *
  20. * The driver supports Intel(R) 10/100 Mbps PCI Fast Ethernet
  21. * controller family, which includes the 82557, 82558, 82559, 82550,
  22. * 82551, and 82562 devices. 82558 and greater controllers
  23. * integrate the Intel 82555 PHY. The controllers are used in
  24. * server and client network interface cards, as well as in
  25. * LAN-On-Motherboard (LOM), CardBus, MiniPCI, and ICHx
  26. * configurations. 8255x supports a 32-bit linear addressing
  27. * mode and operates at 33Mhz PCI clock rate.
  28. *
  29. * II. Driver Operation
  30. *
  31. * Memory-mapped mode is used exclusively to access the device's
  32. * shared-memory structure, the Control/Status Registers (CSR). All
  33. * setup, configuration, and control of the device, including queuing
  34. * of Tx, Rx, and configuration commands is through the CSR.
  35. * cmd_lock serializes accesses to the CSR command register. cb_lock
  36. * protects the shared Command Block List (CBL).
  37. *
  38. * 8255x is highly MII-compliant and all access to the PHY go
  39. * through the Management Data Interface (MDI). Consequently, the
  40. * driver leverages the mii.c library shared with other MII-compliant
  41. * devices.
  42. *
  43. * Big- and Little-Endian byte order as well as 32- and 64-bit
  44. * archs are supported. Weak-ordered memory and non-cache-coherent
  45. * archs are supported.
  46. *
  47. * III. Transmit
  48. *
  49. * A Tx skb is mapped and hangs off of a TCB. TCBs are linked
  50. * together in a fixed-size ring (CBL) thus forming the flexible mode
  51. * memory structure. A TCB marked with the suspend-bit indicates
  52. * the end of the ring. The last TCB processed suspends the
  53. * controller, and the controller can be restarted by issue a CU
  54. * resume command to continue from the suspend point, or a CU start
  55. * command to start at a given position in the ring.
  56. *
  57. * Non-Tx commands (config, multicast setup, etc) are linked
  58. * into the CBL ring along with Tx commands. The common structure
  59. * used for both Tx and non-Tx commands is the Command Block (CB).
  60. *
  61. * cb_to_use is the next CB to use for queuing a command; cb_to_clean
  62. * is the next CB to check for completion; cb_to_send is the first
  63. * CB to start on in case of a previous failure to resume. CB clean
  64. * up happens in interrupt context in response to a CU interrupt.
  65. * cbs_avail keeps track of number of free CB resources available.
  66. *
  67. * Hardware padding of short packets to minimum packet size is
  68. * enabled. 82557 pads with 7Eh, while the later controllers pad
  69. * with 00h.
  70. *
  71. * IV. Receive
  72. *
  73. * The Receive Frame Area (RFA) comprises a ring of Receive Frame
  74. * Descriptors (RFD) + data buffer, thus forming the simplified mode
  75. * memory structure. Rx skbs are allocated to contain both the RFD
  76. * and the data buffer, but the RFD is pulled off before the skb is
  77. * indicated. The data buffer is aligned such that encapsulated
  78. * protocol headers are u32-aligned. Since the RFD is part of the
  79. * mapped shared memory, and completion status is contained within
  80. * the RFD, the RFD must be dma_sync'ed to maintain a consistent
  81. * view from software and hardware.
  82. *
  83. * In order to keep updates to the RFD link field from colliding with
  84. * hardware writes to mark packets complete, we use the feature that
  85. * hardware will not write to a size 0 descriptor and mark the previous
  86. * packet as end-of-list (EL). After updating the link, we remove EL
  87. * and only then restore the size such that hardware may use the
  88. * previous-to-end RFD.
  89. *
  90. * Under typical operation, the receive unit (RU) is start once,
  91. * and the controller happily fills RFDs as frames arrive. If
  92. * replacement RFDs cannot be allocated, or the RU goes non-active,
  93. * the RU must be restarted. Frame arrival generates an interrupt,
  94. * and Rx indication and re-allocation happen in the same context,
  95. * therefore no locking is required. A software-generated interrupt
  96. * is generated from the watchdog to recover from a failed allocation
  97. * scenario where all Rx resources have been indicated and none re-
  98. * placed.
  99. *
  100. * V. Miscellaneous
  101. *
  102. * VLAN offloading of tagging, stripping and filtering is not
  103. * supported, but driver will accommodate the extra 4-byte VLAN tag
  104. * for processing by upper layers. Tx/Rx Checksum offloading is not
  105. * supported. Tx Scatter/Gather is not supported. Jumbo Frames is
  106. * not supported (hardware limitation).
  107. *
  108. * MagicPacket(tm) WoL support is enabled/disabled via ethtool.
  109. *
  110. * Thanks to JC (jchapman@katalix.com) for helping with
  111. * testing/troubleshooting the development driver.
  112. *
  113. * TODO:
  114. * o several entry points race with dev->close
  115. * o check for tx-no-resources/stop Q races with tx clean/wake Q
  116. *
  117. * FIXES:
  118. * 2005/12/02 - Michael O'Donnell <Michael.ODonnell at stratus dot com>
  119. * - Stratus87247: protect MDI control register manipulations
  120. * 2009/06/01 - Andreas Mohr <andi at lisas dot de>
  121. * - add clean lowlevel I/O emulation for cards with MII-lacking PHYs
  122. */
  123. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  124. #include <linux/hardirq.h>
  125. #include <linux/interrupt.h>
  126. #include <linux/module.h>
  127. #include <linux/moduleparam.h>
  128. #include <linux/kernel.h>
  129. #include <linux/types.h>
  130. #include <linux/sched.h>
  131. #include <linux/slab.h>
  132. #include <linux/delay.h>
  133. #include <linux/init.h>
  134. #include <linux/pci.h>
  135. #include <linux/dma-mapping.h>
  136. #include <linux/dmapool.h>
  137. #include <linux/netdevice.h>
  138. #include <linux/etherdevice.h>
  139. #include <linux/mii.h>
  140. #include <linux/if_vlan.h>
  141. #include <linux/skbuff.h>
  142. #include <linux/ethtool.h>
  143. #include <linux/string.h>
  144. #include <linux/firmware.h>
  145. #include <linux/rtnetlink.h>
  146. #include <linux/unaligned.h>
  147. #define DRV_NAME "e100"
  148. #define DRV_DESCRIPTION "Intel(R) PRO/100 Network Driver"
  149. #define DRV_COPYRIGHT "Copyright(c) 1999-2006 Intel Corporation"
  150. #define E100_WATCHDOG_PERIOD (2 * HZ)
  151. #define E100_NAPI_WEIGHT 16
  152. #define FIRMWARE_D101M "e100/d101m_ucode.bin"
  153. #define FIRMWARE_D101S "e100/d101s_ucode.bin"
  154. #define FIRMWARE_D102E "e100/d102e_ucode.bin"
  155. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  156. MODULE_LICENSE("GPL v2");
  157. MODULE_FIRMWARE(FIRMWARE_D101M);
  158. MODULE_FIRMWARE(FIRMWARE_D101S);
  159. MODULE_FIRMWARE(FIRMWARE_D102E);
  160. static int debug = 3;
  161. static int eeprom_bad_csum_allow = 0;
  162. static int use_io = 0;
  163. module_param(debug, int, 0);
  164. module_param(eeprom_bad_csum_allow, int, 0444);
  165. module_param(use_io, int, 0444);
  166. MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all)");
  167. MODULE_PARM_DESC(eeprom_bad_csum_allow, "Allow bad eeprom checksums");
  168. MODULE_PARM_DESC(use_io, "Force use of i/o access mode");
  169. #define INTEL_8255X_ETHERNET_DEVICE(device_id, ich) {\
  170. PCI_VENDOR_ID_INTEL, device_id, PCI_ANY_ID, PCI_ANY_ID, \
  171. PCI_CLASS_NETWORK_ETHERNET << 8, 0xFFFF00, ich }
  172. static const struct pci_device_id e100_id_table[] = {
  173. INTEL_8255X_ETHERNET_DEVICE(0x1029, 0),
  174. INTEL_8255X_ETHERNET_DEVICE(0x1030, 0),
  175. INTEL_8255X_ETHERNET_DEVICE(0x1031, 3),
  176. INTEL_8255X_ETHERNET_DEVICE(0x1032, 3),
  177. INTEL_8255X_ETHERNET_DEVICE(0x1033, 3),
  178. INTEL_8255X_ETHERNET_DEVICE(0x1034, 3),
  179. INTEL_8255X_ETHERNET_DEVICE(0x1038, 3),
  180. INTEL_8255X_ETHERNET_DEVICE(0x1039, 4),
  181. INTEL_8255X_ETHERNET_DEVICE(0x103A, 4),
  182. INTEL_8255X_ETHERNET_DEVICE(0x103B, 4),
  183. INTEL_8255X_ETHERNET_DEVICE(0x103C, 4),
  184. INTEL_8255X_ETHERNET_DEVICE(0x103D, 4),
  185. INTEL_8255X_ETHERNET_DEVICE(0x103E, 4),
  186. INTEL_8255X_ETHERNET_DEVICE(0x1050, 5),
  187. INTEL_8255X_ETHERNET_DEVICE(0x1051, 5),
  188. INTEL_8255X_ETHERNET_DEVICE(0x1052, 5),
  189. INTEL_8255X_ETHERNET_DEVICE(0x1053, 5),
  190. INTEL_8255X_ETHERNET_DEVICE(0x1054, 5),
  191. INTEL_8255X_ETHERNET_DEVICE(0x1055, 5),
  192. INTEL_8255X_ETHERNET_DEVICE(0x1056, 5),
  193. INTEL_8255X_ETHERNET_DEVICE(0x1057, 5),
  194. INTEL_8255X_ETHERNET_DEVICE(0x1059, 0),
  195. INTEL_8255X_ETHERNET_DEVICE(0x1064, 6),
  196. INTEL_8255X_ETHERNET_DEVICE(0x1065, 6),
  197. INTEL_8255X_ETHERNET_DEVICE(0x1066, 6),
  198. INTEL_8255X_ETHERNET_DEVICE(0x1067, 6),
  199. INTEL_8255X_ETHERNET_DEVICE(0x1068, 6),
  200. INTEL_8255X_ETHERNET_DEVICE(0x1069, 6),
  201. INTEL_8255X_ETHERNET_DEVICE(0x106A, 6),
  202. INTEL_8255X_ETHERNET_DEVICE(0x106B, 6),
  203. INTEL_8255X_ETHERNET_DEVICE(0x1091, 7),
  204. INTEL_8255X_ETHERNET_DEVICE(0x1092, 7),
  205. INTEL_8255X_ETHERNET_DEVICE(0x1093, 7),
  206. INTEL_8255X_ETHERNET_DEVICE(0x1094, 7),
  207. INTEL_8255X_ETHERNET_DEVICE(0x1095, 7),
  208. INTEL_8255X_ETHERNET_DEVICE(0x10fe, 7),
  209. INTEL_8255X_ETHERNET_DEVICE(0x1209, 0),
  210. INTEL_8255X_ETHERNET_DEVICE(0x1229, 0),
  211. INTEL_8255X_ETHERNET_DEVICE(0x2449, 2),
  212. INTEL_8255X_ETHERNET_DEVICE(0x2459, 2),
  213. INTEL_8255X_ETHERNET_DEVICE(0x245D, 2),
  214. INTEL_8255X_ETHERNET_DEVICE(0x27DC, 7),
  215. { 0, }
  216. };
  217. MODULE_DEVICE_TABLE(pci, e100_id_table);
  218. enum mac {
  219. mac_82557_D100_A = 0,
  220. mac_82557_D100_B = 1,
  221. mac_82557_D100_C = 2,
  222. mac_82558_D101_A4 = 4,
  223. mac_82558_D101_B0 = 5,
  224. mac_82559_D101M = 8,
  225. mac_82559_D101S = 9,
  226. mac_82550_D102 = 12,
  227. mac_82550_D102_C = 13,
  228. mac_82551_E = 14,
  229. mac_82551_F = 15,
  230. mac_82551_10 = 16,
  231. mac_unknown = 0xFF,
  232. };
  233. enum phy {
  234. phy_100a = 0x000003E0,
  235. phy_100c = 0x035002A8,
  236. phy_82555_tx = 0x015002A8,
  237. phy_nsc_tx = 0x5C002000,
  238. phy_82562_et = 0x033002A8,
  239. phy_82562_em = 0x032002A8,
  240. phy_82562_ek = 0x031002A8,
  241. phy_82562_eh = 0x017002A8,
  242. phy_82552_v = 0xd061004d,
  243. phy_unknown = 0xFFFFFFFF,
  244. };
  245. /* CSR (Control/Status Registers) */
  246. struct csr {
  247. struct {
  248. u8 status;
  249. u8 stat_ack;
  250. u8 cmd_lo;
  251. u8 cmd_hi;
  252. u32 gen_ptr;
  253. } scb;
  254. u32 port;
  255. u16 flash_ctrl;
  256. u8 eeprom_ctrl_lo;
  257. u8 eeprom_ctrl_hi;
  258. u32 mdi_ctrl;
  259. u32 rx_dma_count;
  260. };
  261. enum scb_status {
  262. rus_no_res = 0x08,
  263. rus_ready = 0x10,
  264. rus_mask = 0x3C,
  265. };
  266. enum ru_state {
  267. RU_SUSPENDED = 0,
  268. RU_RUNNING = 1,
  269. RU_UNINITIALIZED = -1,
  270. };
  271. enum scb_stat_ack {
  272. stat_ack_not_ours = 0x00,
  273. stat_ack_sw_gen = 0x04,
  274. stat_ack_rnr = 0x10,
  275. stat_ack_cu_idle = 0x20,
  276. stat_ack_frame_rx = 0x40,
  277. stat_ack_cu_cmd_done = 0x80,
  278. stat_ack_not_present = 0xFF,
  279. stat_ack_rx = (stat_ack_sw_gen | stat_ack_rnr | stat_ack_frame_rx),
  280. stat_ack_tx = (stat_ack_cu_idle | stat_ack_cu_cmd_done),
  281. };
  282. enum scb_cmd_hi {
  283. irq_mask_none = 0x00,
  284. irq_mask_all = 0x01,
  285. irq_sw_gen = 0x02,
  286. };
  287. enum scb_cmd_lo {
  288. cuc_nop = 0x00,
  289. ruc_start = 0x01,
  290. ruc_load_base = 0x06,
  291. cuc_start = 0x10,
  292. cuc_resume = 0x20,
  293. cuc_dump_addr = 0x40,
  294. cuc_dump_stats = 0x50,
  295. cuc_load_base = 0x60,
  296. cuc_dump_reset = 0x70,
  297. };
  298. enum cuc_dump {
  299. cuc_dump_complete = 0x0000A005,
  300. cuc_dump_reset_complete = 0x0000A007,
  301. };
  302. enum port {
  303. software_reset = 0x0000,
  304. selftest = 0x0001,
  305. selective_reset = 0x0002,
  306. };
  307. enum eeprom_ctrl_lo {
  308. eesk = 0x01,
  309. eecs = 0x02,
  310. eedi = 0x04,
  311. eedo = 0x08,
  312. };
  313. enum mdi_ctrl {
  314. mdi_write = 0x04000000,
  315. mdi_read = 0x08000000,
  316. mdi_ready = 0x10000000,
  317. };
  318. enum eeprom_op {
  319. op_write = 0x05,
  320. op_read = 0x06,
  321. op_ewds = 0x10,
  322. op_ewen = 0x13,
  323. };
  324. enum eeprom_offsets {
  325. eeprom_cnfg_mdix = 0x03,
  326. eeprom_phy_iface = 0x06,
  327. eeprom_id = 0x0A,
  328. eeprom_config_asf = 0x0D,
  329. eeprom_smbus_addr = 0x90,
  330. };
  331. enum eeprom_cnfg_mdix {
  332. eeprom_mdix_enabled = 0x0080,
  333. };
  334. enum eeprom_phy_iface {
  335. NoSuchPhy = 0,
  336. I82553AB,
  337. I82553C,
  338. I82503,
  339. DP83840,
  340. S80C240,
  341. S80C24,
  342. I82555,
  343. DP83840A = 10,
  344. };
  345. enum eeprom_id {
  346. eeprom_id_wol = 0x0020,
  347. };
  348. enum eeprom_config_asf {
  349. eeprom_asf = 0x8000,
  350. eeprom_gcl = 0x4000,
  351. };
  352. enum cb_status {
  353. cb_complete = 0x8000,
  354. cb_ok = 0x2000,
  355. };
  356. /*
  357. * cb_command - Command Block flags
  358. * @cb_tx_nc: 0: controller does CRC (normal), 1: CRC from skb memory
  359. */
  360. enum cb_command {
  361. cb_nop = 0x0000,
  362. cb_iaaddr = 0x0001,
  363. cb_config = 0x0002,
  364. cb_multi = 0x0003,
  365. cb_tx = 0x0004,
  366. cb_ucode = 0x0005,
  367. cb_dump = 0x0006,
  368. cb_tx_sf = 0x0008,
  369. cb_tx_nc = 0x0010,
  370. cb_cid = 0x1f00,
  371. cb_i = 0x2000,
  372. cb_s = 0x4000,
  373. cb_el = 0x8000,
  374. };
  375. struct rfd {
  376. __le16 status;
  377. __le16 command;
  378. __le32 link;
  379. __le32 rbd;
  380. __le16 actual_size;
  381. __le16 size;
  382. };
  383. struct rx {
  384. struct rx *next, *prev;
  385. struct sk_buff *skb;
  386. dma_addr_t dma_addr;
  387. };
  388. #if defined(__BIG_ENDIAN_BITFIELD)
  389. #define X(a,b) b,a
  390. #else
  391. #define X(a,b) a,b
  392. #endif
  393. struct config {
  394. /*0*/ u8 X(byte_count:6, pad0:2);
  395. /*1*/ u8 X(X(rx_fifo_limit:4, tx_fifo_limit:3), pad1:1);
  396. /*2*/ u8 adaptive_ifs;
  397. /*3*/ u8 X(X(X(X(mwi_enable:1, type_enable:1), read_align_enable:1),
  398. term_write_cache_line:1), pad3:4);
  399. /*4*/ u8 X(rx_dma_max_count:7, pad4:1);
  400. /*5*/ u8 X(tx_dma_max_count:7, dma_max_count_enable:1);
  401. /*6*/ u8 X(X(X(X(X(X(X(late_scb_update:1, direct_rx_dma:1),
  402. tno_intr:1), cna_intr:1), standard_tcb:1), standard_stat_counter:1),
  403. rx_save_overruns : 1), rx_save_bad_frames : 1);
  404. /*7*/ u8 X(X(X(X(X(rx_discard_short_frames:1, tx_underrun_retry:2),
  405. pad7:2), rx_extended_rfd:1), tx_two_frames_in_fifo:1),
  406. tx_dynamic_tbd:1);
  407. /*8*/ u8 X(X(mii_mode:1, pad8:6), csma_disabled:1);
  408. /*9*/ u8 X(X(X(X(X(rx_tcpudp_checksum:1, pad9:3), vlan_arp_tco:1),
  409. link_status_wake:1), arp_wake:1), mcmatch_wake:1);
  410. /*10*/ u8 X(X(X(pad10:3, no_source_addr_insertion:1), preamble_length:2),
  411. loopback:2);
  412. /*11*/ u8 X(linear_priority:3, pad11:5);
  413. /*12*/ u8 X(X(linear_priority_mode:1, pad12:3), ifs:4);
  414. /*13*/ u8 ip_addr_lo;
  415. /*14*/ u8 ip_addr_hi;
  416. /*15*/ u8 X(X(X(X(X(X(X(promiscuous_mode:1, broadcast_disabled:1),
  417. wait_after_win:1), pad15_1:1), ignore_ul_bit:1), crc_16_bit:1),
  418. pad15_2:1), crs_or_cdt:1);
  419. /*16*/ u8 fc_delay_lo;
  420. /*17*/ u8 fc_delay_hi;
  421. /*18*/ u8 X(X(X(X(X(rx_stripping:1, tx_padding:1), rx_crc_transfer:1),
  422. rx_long_ok:1), fc_priority_threshold:3), pad18:1);
  423. /*19*/ u8 X(X(X(X(X(X(X(addr_wake:1, magic_packet_disable:1),
  424. fc_disable:1), fc_restop:1), fc_restart:1), fc_reject:1),
  425. full_duplex_force:1), full_duplex_pin:1);
  426. /*20*/ u8 X(X(X(pad20_1:5, fc_priority_location:1), multi_ia:1), pad20_2:1);
  427. /*21*/ u8 X(X(pad21_1:3, multicast_all:1), pad21_2:4);
  428. /*22*/ u8 X(X(rx_d102_mode:1, rx_vlan_drop:1), pad22:6);
  429. u8 pad_d102[9];
  430. };
  431. #define E100_MAX_MULTICAST_ADDRS 64
  432. struct multi {
  433. __le16 count;
  434. u8 addr[E100_MAX_MULTICAST_ADDRS * ETH_ALEN + 2/*pad*/];
  435. };
  436. /* Important: keep total struct u32-aligned */
  437. #define UCODE_SIZE 134
  438. struct cb {
  439. __le16 status;
  440. __le16 command;
  441. __le32 link;
  442. union {
  443. u8 iaaddr[ETH_ALEN];
  444. __le32 ucode[UCODE_SIZE];
  445. struct config config;
  446. struct multi multi;
  447. struct {
  448. u32 tbd_array;
  449. u16 tcb_byte_count;
  450. u8 threshold;
  451. u8 tbd_count;
  452. struct {
  453. __le32 buf_addr;
  454. __le16 size;
  455. u16 eol;
  456. } tbd;
  457. } tcb;
  458. __le32 dump_buffer_addr;
  459. } u;
  460. struct cb *next, *prev;
  461. dma_addr_t dma_addr;
  462. struct sk_buff *skb;
  463. };
  464. enum loopback {
  465. lb_none = 0, lb_mac = 1, lb_phy = 3,
  466. };
  467. struct stats {
  468. __le32 tx_good_frames, tx_max_collisions, tx_late_collisions,
  469. tx_underruns, tx_lost_crs, tx_deferred, tx_single_collisions,
  470. tx_multiple_collisions, tx_total_collisions;
  471. __le32 rx_good_frames, rx_crc_errors, rx_alignment_errors,
  472. rx_resource_errors, rx_overrun_errors, rx_cdt_errors,
  473. rx_short_frame_errors;
  474. __le32 fc_xmt_pause, fc_rcv_pause, fc_rcv_unsupported;
  475. __le16 xmt_tco_frames, rcv_tco_frames;
  476. __le32 complete;
  477. };
  478. struct mem {
  479. struct {
  480. u32 signature;
  481. u32 result;
  482. } selftest;
  483. struct stats stats;
  484. u8 dump_buf[596];
  485. };
  486. struct param_range {
  487. u32 min;
  488. u32 max;
  489. u32 count;
  490. };
  491. struct params {
  492. struct param_range rfds;
  493. struct param_range cbs;
  494. };
  495. struct nic {
  496. /* Begin: frequently used values: keep adjacent for cache effect */
  497. u32 msg_enable ____cacheline_aligned;
  498. struct net_device *netdev;
  499. struct pci_dev *pdev;
  500. u16 (*mdio_ctrl)(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data);
  501. struct rx *rxs ____cacheline_aligned;
  502. struct rx *rx_to_use;
  503. struct rx *rx_to_clean;
  504. struct rfd blank_rfd;
  505. enum ru_state ru_running;
  506. spinlock_t cb_lock ____cacheline_aligned;
  507. spinlock_t cmd_lock;
  508. struct csr __iomem *csr;
  509. enum scb_cmd_lo cuc_cmd;
  510. unsigned int cbs_avail;
  511. struct napi_struct napi;
  512. struct cb *cbs;
  513. struct cb *cb_to_use;
  514. struct cb *cb_to_send;
  515. struct cb *cb_to_clean;
  516. __le16 tx_command;
  517. /* End: frequently used values: keep adjacent for cache effect */
  518. enum {
  519. ich = (1 << 0),
  520. promiscuous = (1 << 1),
  521. multicast_all = (1 << 2),
  522. wol_magic = (1 << 3),
  523. ich_10h_workaround = (1 << 4),
  524. } flags ____cacheline_aligned;
  525. enum mac mac;
  526. enum phy phy;
  527. struct params params;
  528. struct timer_list watchdog;
  529. struct mii_if_info mii;
  530. struct work_struct tx_timeout_task;
  531. enum loopback loopback;
  532. struct mem *mem;
  533. dma_addr_t dma_addr;
  534. struct dma_pool *cbs_pool;
  535. dma_addr_t cbs_dma_addr;
  536. u8 adaptive_ifs;
  537. u8 tx_threshold;
  538. u32 tx_frames;
  539. u32 tx_collisions;
  540. u32 tx_deferred;
  541. u32 tx_single_collisions;
  542. u32 tx_multiple_collisions;
  543. u32 tx_fc_pause;
  544. u32 tx_tco_frames;
  545. u32 rx_fc_pause;
  546. u32 rx_fc_unsupported;
  547. u32 rx_tco_frames;
  548. u32 rx_short_frame_errors;
  549. u32 rx_over_length_errors;
  550. u16 eeprom_wc;
  551. __le16 eeprom[256];
  552. spinlock_t mdio_lock;
  553. const struct firmware *fw;
  554. };
  555. static inline void e100_write_flush(struct nic *nic)
  556. {
  557. /* Flush previous PCI writes through intermediate bridges
  558. * by doing a benign read */
  559. (void)ioread8(&nic->csr->scb.status);
  560. }
  561. static void e100_enable_irq(struct nic *nic)
  562. {
  563. unsigned long flags;
  564. spin_lock_irqsave(&nic->cmd_lock, flags);
  565. iowrite8(irq_mask_none, &nic->csr->scb.cmd_hi);
  566. e100_write_flush(nic);
  567. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  568. }
  569. static void e100_disable_irq(struct nic *nic)
  570. {
  571. unsigned long flags;
  572. spin_lock_irqsave(&nic->cmd_lock, flags);
  573. iowrite8(irq_mask_all, &nic->csr->scb.cmd_hi);
  574. e100_write_flush(nic);
  575. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  576. }
  577. static void e100_hw_reset(struct nic *nic)
  578. {
  579. /* Put CU and RU into idle with a selective reset to get
  580. * device off of PCI bus */
  581. iowrite32(selective_reset, &nic->csr->port);
  582. e100_write_flush(nic); udelay(20);
  583. /* Now fully reset device */
  584. iowrite32(software_reset, &nic->csr->port);
  585. e100_write_flush(nic); udelay(20);
  586. /* Mask off our interrupt line - it's unmasked after reset */
  587. e100_disable_irq(nic);
  588. }
  589. static int e100_self_test(struct nic *nic)
  590. {
  591. u32 dma_addr = nic->dma_addr + offsetof(struct mem, selftest);
  592. /* Passing the self-test is a pretty good indication
  593. * that the device can DMA to/from host memory */
  594. nic->mem->selftest.signature = 0;
  595. nic->mem->selftest.result = 0xFFFFFFFF;
  596. iowrite32(selftest | dma_addr, &nic->csr->port);
  597. e100_write_flush(nic);
  598. /* Wait 10 msec for self-test to complete */
  599. msleep(10);
  600. /* Interrupts are enabled after self-test */
  601. e100_disable_irq(nic);
  602. /* Check results of self-test */
  603. if (nic->mem->selftest.result != 0) {
  604. netif_err(nic, hw, nic->netdev,
  605. "Self-test failed: result=0x%08X\n",
  606. nic->mem->selftest.result);
  607. return -ETIMEDOUT;
  608. }
  609. if (nic->mem->selftest.signature == 0) {
  610. netif_err(nic, hw, nic->netdev, "Self-test failed: timed out\n");
  611. return -ETIMEDOUT;
  612. }
  613. return 0;
  614. }
  615. static void e100_eeprom_write(struct nic *nic, u16 addr_len, u16 addr, __le16 data)
  616. {
  617. u32 cmd_addr_data[3];
  618. u8 ctrl;
  619. int i, j;
  620. /* Three cmds: write/erase enable, write data, write/erase disable */
  621. cmd_addr_data[0] = op_ewen << (addr_len - 2);
  622. cmd_addr_data[1] = (((op_write << addr_len) | addr) << 16) |
  623. le16_to_cpu(data);
  624. cmd_addr_data[2] = op_ewds << (addr_len - 2);
  625. /* Bit-bang cmds to write word to eeprom */
  626. for (j = 0; j < 3; j++) {
  627. /* Chip select */
  628. iowrite8(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
  629. e100_write_flush(nic); udelay(4);
  630. for (i = 31; i >= 0; i--) {
  631. ctrl = (cmd_addr_data[j] & (1 << i)) ?
  632. eecs | eedi : eecs;
  633. iowrite8(ctrl, &nic->csr->eeprom_ctrl_lo);
  634. e100_write_flush(nic); udelay(4);
  635. iowrite8(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
  636. e100_write_flush(nic); udelay(4);
  637. }
  638. /* Wait 10 msec for cmd to complete */
  639. msleep(10);
  640. /* Chip deselect */
  641. iowrite8(0, &nic->csr->eeprom_ctrl_lo);
  642. e100_write_flush(nic); udelay(4);
  643. }
  644. };
  645. /* General technique stolen from the eepro100 driver - very clever */
  646. static __le16 e100_eeprom_read(struct nic *nic, u16 *addr_len, u16 addr)
  647. {
  648. u32 cmd_addr_data;
  649. u16 data = 0;
  650. u8 ctrl;
  651. int i;
  652. cmd_addr_data = ((op_read << *addr_len) | addr) << 16;
  653. /* Chip select */
  654. iowrite8(eecs | eesk, &nic->csr->eeprom_ctrl_lo);
  655. e100_write_flush(nic); udelay(4);
  656. /* Bit-bang to read word from eeprom */
  657. for (i = 31; i >= 0; i--) {
  658. ctrl = (cmd_addr_data & (1 << i)) ? eecs | eedi : eecs;
  659. iowrite8(ctrl, &nic->csr->eeprom_ctrl_lo);
  660. e100_write_flush(nic); udelay(4);
  661. iowrite8(ctrl | eesk, &nic->csr->eeprom_ctrl_lo);
  662. e100_write_flush(nic); udelay(4);
  663. /* Eeprom drives a dummy zero to EEDO after receiving
  664. * complete address. Use this to adjust addr_len. */
  665. ctrl = ioread8(&nic->csr->eeprom_ctrl_lo);
  666. if (!(ctrl & eedo) && i > 16) {
  667. *addr_len -= (i - 16);
  668. i = 17;
  669. }
  670. data = (data << 1) | (ctrl & eedo ? 1 : 0);
  671. }
  672. /* Chip deselect */
  673. iowrite8(0, &nic->csr->eeprom_ctrl_lo);
  674. e100_write_flush(nic); udelay(4);
  675. return cpu_to_le16(data);
  676. };
  677. /* Load entire EEPROM image into driver cache and validate checksum */
  678. static int e100_eeprom_load(struct nic *nic)
  679. {
  680. u16 addr, addr_len = 8, checksum = 0;
  681. /* Try reading with an 8-bit addr len to discover actual addr len */
  682. e100_eeprom_read(nic, &addr_len, 0);
  683. nic->eeprom_wc = 1 << addr_len;
  684. for (addr = 0; addr < nic->eeprom_wc; addr++) {
  685. nic->eeprom[addr] = e100_eeprom_read(nic, &addr_len, addr);
  686. if (addr < nic->eeprom_wc - 1)
  687. checksum += le16_to_cpu(nic->eeprom[addr]);
  688. }
  689. /* The checksum, stored in the last word, is calculated such that
  690. * the sum of words should be 0xBABA */
  691. if (cpu_to_le16(0xBABA - checksum) != nic->eeprom[nic->eeprom_wc - 1]) {
  692. netif_err(nic, probe, nic->netdev, "EEPROM corrupted\n");
  693. if (!eeprom_bad_csum_allow)
  694. return -EAGAIN;
  695. }
  696. return 0;
  697. }
  698. /* Save (portion of) driver EEPROM cache to device and update checksum */
  699. static int e100_eeprom_save(struct nic *nic, u16 start, u16 count)
  700. {
  701. u16 addr, addr_len = 8, checksum = 0;
  702. /* Try reading with an 8-bit addr len to discover actual addr len */
  703. e100_eeprom_read(nic, &addr_len, 0);
  704. nic->eeprom_wc = 1 << addr_len;
  705. if (start + count >= nic->eeprom_wc)
  706. return -EINVAL;
  707. for (addr = start; addr < start + count; addr++)
  708. e100_eeprom_write(nic, addr_len, addr, nic->eeprom[addr]);
  709. /* The checksum, stored in the last word, is calculated such that
  710. * the sum of words should be 0xBABA */
  711. for (addr = 0; addr < nic->eeprom_wc - 1; addr++)
  712. checksum += le16_to_cpu(nic->eeprom[addr]);
  713. nic->eeprom[nic->eeprom_wc - 1] = cpu_to_le16(0xBABA - checksum);
  714. e100_eeprom_write(nic, addr_len, nic->eeprom_wc - 1,
  715. nic->eeprom[nic->eeprom_wc - 1]);
  716. return 0;
  717. }
  718. #define E100_WAIT_SCB_TIMEOUT 20000 /* we might have to wait 100ms!!! */
  719. #define E100_WAIT_SCB_FAST 20 /* delay like the old code */
  720. static int e100_exec_cmd(struct nic *nic, u8 cmd, dma_addr_t dma_addr)
  721. {
  722. unsigned long flags;
  723. unsigned int i;
  724. int err = 0;
  725. spin_lock_irqsave(&nic->cmd_lock, flags);
  726. /* Previous command is accepted when SCB clears */
  727. for (i = 0; i < E100_WAIT_SCB_TIMEOUT; i++) {
  728. if (likely(!ioread8(&nic->csr->scb.cmd_lo)))
  729. break;
  730. cpu_relax();
  731. if (unlikely(i > E100_WAIT_SCB_FAST))
  732. udelay(5);
  733. }
  734. if (unlikely(i == E100_WAIT_SCB_TIMEOUT)) {
  735. err = -EAGAIN;
  736. goto err_unlock;
  737. }
  738. if (unlikely(cmd != cuc_resume))
  739. iowrite32(dma_addr, &nic->csr->scb.gen_ptr);
  740. iowrite8(cmd, &nic->csr->scb.cmd_lo);
  741. err_unlock:
  742. spin_unlock_irqrestore(&nic->cmd_lock, flags);
  743. return err;
  744. }
  745. static int e100_exec_cb(struct nic *nic, struct sk_buff *skb,
  746. int (*cb_prepare)(struct nic *, struct cb *, struct sk_buff *))
  747. {
  748. struct cb *cb;
  749. unsigned long flags;
  750. int err;
  751. spin_lock_irqsave(&nic->cb_lock, flags);
  752. if (unlikely(!nic->cbs_avail)) {
  753. err = -ENOMEM;
  754. goto err_unlock;
  755. }
  756. cb = nic->cb_to_use;
  757. nic->cb_to_use = cb->next;
  758. nic->cbs_avail--;
  759. cb->skb = skb;
  760. err = cb_prepare(nic, cb, skb);
  761. if (err)
  762. goto err_unlock;
  763. if (unlikely(!nic->cbs_avail))
  764. err = -ENOSPC;
  765. /* Order is important otherwise we'll be in a race with h/w:
  766. * set S-bit in current first, then clear S-bit in previous. */
  767. cb->command |= cpu_to_le16(cb_s);
  768. dma_wmb();
  769. cb->prev->command &= cpu_to_le16(~cb_s);
  770. while (nic->cb_to_send != nic->cb_to_use) {
  771. if (unlikely(e100_exec_cmd(nic, nic->cuc_cmd,
  772. nic->cb_to_send->dma_addr))) {
  773. /* Ok, here's where things get sticky. It's
  774. * possible that we can't schedule the command
  775. * because the controller is too busy, so
  776. * let's just queue the command and try again
  777. * when another command is scheduled. */
  778. if (err == -ENOSPC) {
  779. //request a reset
  780. schedule_work(&nic->tx_timeout_task);
  781. }
  782. break;
  783. } else {
  784. nic->cuc_cmd = cuc_resume;
  785. nic->cb_to_send = nic->cb_to_send->next;
  786. }
  787. }
  788. err_unlock:
  789. spin_unlock_irqrestore(&nic->cb_lock, flags);
  790. return err;
  791. }
  792. static int mdio_read(struct net_device *netdev, int addr, int reg)
  793. {
  794. struct nic *nic = netdev_priv(netdev);
  795. return nic->mdio_ctrl(nic, addr, mdi_read, reg, 0);
  796. }
  797. static void mdio_write(struct net_device *netdev, int addr, int reg, int data)
  798. {
  799. struct nic *nic = netdev_priv(netdev);
  800. nic->mdio_ctrl(nic, addr, mdi_write, reg, data);
  801. }
  802. /* the standard mdio_ctrl() function for usual MII-compliant hardware */
  803. static u16 mdio_ctrl_hw(struct nic *nic, u32 addr, u32 dir, u32 reg, u16 data)
  804. {
  805. u32 data_out = 0;
  806. unsigned int i;
  807. unsigned long flags;
  808. /*
  809. * Stratus87247: we shouldn't be writing the MDI control
  810. * register until the Ready bit shows True. Also, since
  811. * manipulation of the MDI control registers is a multi-step
  812. * procedure it should be done under lock.
  813. */
  814. spin_lock_irqsave(&nic->mdio_lock, flags);
  815. for (i = 100; i; --i) {
  816. if (ioread32(&nic->csr->mdi_ctrl) & mdi_ready)
  817. break;
  818. udelay(20);
  819. }
  820. if (unlikely(!i)) {
  821. netdev_err(nic->netdev, "e100.mdio_ctrl won't go Ready\n");
  822. spin_unlock_irqrestore(&nic->mdio_lock, flags);
  823. return 0; /* No way to indicate timeout error */
  824. }
  825. iowrite32((reg << 16) | (addr << 21) | dir | data, &nic->csr->mdi_ctrl);
  826. for (i = 0; i < 100; i++) {
  827. udelay(20);
  828. if ((data_out = ioread32(&nic->csr->mdi_ctrl)) & mdi_ready)
  829. break;
  830. }
  831. spin_unlock_irqrestore(&nic->mdio_lock, flags);
  832. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  833. "%s:addr=%d, reg=%d, data_in=0x%04X, data_out=0x%04X\n",
  834. dir == mdi_read ? "READ" : "WRITE",
  835. addr, reg, data, data_out);
  836. return (u16)data_out;
  837. }
  838. /* slightly tweaked mdio_ctrl() function for phy_82552_v specifics */
  839. static u16 mdio_ctrl_phy_82552_v(struct nic *nic,
  840. u32 addr,
  841. u32 dir,
  842. u32 reg,
  843. u16 data)
  844. {
  845. if ((reg == MII_BMCR) && (dir == mdi_write)) {
  846. if (data & (BMCR_ANRESTART | BMCR_ANENABLE)) {
  847. u16 advert = mdio_read(nic->netdev, nic->mii.phy_id,
  848. MII_ADVERTISE);
  849. /*
  850. * Workaround Si issue where sometimes the part will not
  851. * autoneg to 100Mbps even when advertised.
  852. */
  853. if (advert & ADVERTISE_100FULL)
  854. data |= BMCR_SPEED100 | BMCR_FULLDPLX;
  855. else if (advert & ADVERTISE_100HALF)
  856. data |= BMCR_SPEED100;
  857. }
  858. }
  859. return mdio_ctrl_hw(nic, addr, dir, reg, data);
  860. }
  861. /* Fully software-emulated mdio_ctrl() function for cards without
  862. * MII-compliant PHYs.
  863. * For now, this is mainly geared towards 80c24 support; in case of further
  864. * requirements for other types (i82503, ...?) either extend this mechanism
  865. * or split it, whichever is cleaner.
  866. */
  867. static u16 mdio_ctrl_phy_mii_emulated(struct nic *nic,
  868. u32 addr,
  869. u32 dir,
  870. u32 reg,
  871. u16 data)
  872. {
  873. /* might need to allocate a netdev_priv'ed register array eventually
  874. * to be able to record state changes, but for now
  875. * some fully hardcoded register handling ought to be ok I guess. */
  876. if (dir == mdi_read) {
  877. switch (reg) {
  878. case MII_BMCR:
  879. /* Auto-negotiation, right? */
  880. return BMCR_ANENABLE |
  881. BMCR_FULLDPLX;
  882. case MII_BMSR:
  883. return BMSR_LSTATUS /* for mii_link_ok() */ |
  884. BMSR_ANEGCAPABLE |
  885. BMSR_10FULL;
  886. case MII_ADVERTISE:
  887. /* 80c24 is a "combo card" PHY, right? */
  888. return ADVERTISE_10HALF |
  889. ADVERTISE_10FULL;
  890. default:
  891. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  892. "%s:addr=%d, reg=%d, data=0x%04X: unimplemented emulation!\n",
  893. dir == mdi_read ? "READ" : "WRITE",
  894. addr, reg, data);
  895. return 0xFFFF;
  896. }
  897. } else {
  898. switch (reg) {
  899. default:
  900. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  901. "%s:addr=%d, reg=%d, data=0x%04X: unimplemented emulation!\n",
  902. dir == mdi_read ? "READ" : "WRITE",
  903. addr, reg, data);
  904. return 0xFFFF;
  905. }
  906. }
  907. }
  908. static inline int e100_phy_supports_mii(struct nic *nic)
  909. {
  910. /* for now, just check it by comparing whether we
  911. are using MII software emulation.
  912. */
  913. return (nic->mdio_ctrl != mdio_ctrl_phy_mii_emulated);
  914. }
  915. static void e100_get_defaults(struct nic *nic)
  916. {
  917. struct param_range rfds = { .min = 16, .max = 256, .count = 256 };
  918. struct param_range cbs = { .min = 64, .max = 256, .count = 128 };
  919. /* MAC type is encoded as rev ID; exception: ICH is treated as 82559 */
  920. nic->mac = (nic->flags & ich) ? mac_82559_D101M : nic->pdev->revision;
  921. if (nic->mac == mac_unknown)
  922. nic->mac = mac_82557_D100_A;
  923. nic->params.rfds = rfds;
  924. nic->params.cbs = cbs;
  925. /* Quadwords to DMA into FIFO before starting frame transmit */
  926. nic->tx_threshold = 0xE0;
  927. /* no interrupt for every tx completion, delay = 256us if not 557 */
  928. nic->tx_command = cpu_to_le16(cb_tx | cb_tx_sf |
  929. ((nic->mac >= mac_82558_D101_A4) ? cb_cid : cb_i));
  930. /* Template for a freshly allocated RFD */
  931. nic->blank_rfd.command = 0;
  932. nic->blank_rfd.rbd = cpu_to_le32(0xFFFFFFFF);
  933. nic->blank_rfd.size = cpu_to_le16(VLAN_ETH_FRAME_LEN + ETH_FCS_LEN);
  934. /* MII setup */
  935. nic->mii.phy_id_mask = 0x1F;
  936. nic->mii.reg_num_mask = 0x1F;
  937. nic->mii.dev = nic->netdev;
  938. nic->mii.mdio_read = mdio_read;
  939. nic->mii.mdio_write = mdio_write;
  940. }
  941. static int e100_configure(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  942. {
  943. struct config *config = &cb->u.config;
  944. u8 *c = (u8 *)config;
  945. struct net_device *netdev = nic->netdev;
  946. cb->command = cpu_to_le16(cb_config);
  947. memset(config, 0, sizeof(struct config));
  948. config->byte_count = 0x16; /* bytes in this struct */
  949. config->rx_fifo_limit = 0x8; /* bytes in FIFO before DMA */
  950. config->direct_rx_dma = 0x1; /* reserved */
  951. config->standard_tcb = 0x1; /* 1=standard, 0=extended */
  952. config->standard_stat_counter = 0x1; /* 1=standard, 0=extended */
  953. config->rx_discard_short_frames = 0x1; /* 1=discard, 0=pass */
  954. config->tx_underrun_retry = 0x3; /* # of underrun retries */
  955. if (e100_phy_supports_mii(nic))
  956. config->mii_mode = 1; /* 1=MII mode, 0=i82503 mode */
  957. config->pad10 = 0x6;
  958. config->no_source_addr_insertion = 0x1; /* 1=no, 0=yes */
  959. config->preamble_length = 0x2; /* 0=1, 1=3, 2=7, 3=15 bytes */
  960. config->ifs = 0x6; /* x16 = inter frame spacing */
  961. config->ip_addr_hi = 0xF2; /* ARP IP filter - not used */
  962. config->pad15_1 = 0x1;
  963. config->pad15_2 = 0x1;
  964. config->crs_or_cdt = 0x0; /* 0=CRS only, 1=CRS or CDT */
  965. config->fc_delay_hi = 0x40; /* time delay for fc frame */
  966. config->tx_padding = 0x1; /* 1=pad short frames */
  967. config->fc_priority_threshold = 0x7; /* 7=priority fc disabled */
  968. config->pad18 = 0x1;
  969. config->full_duplex_pin = 0x1; /* 1=examine FDX# pin */
  970. config->pad20_1 = 0x1F;
  971. config->fc_priority_location = 0x1; /* 1=byte#31, 0=byte#19 */
  972. config->pad21_1 = 0x5;
  973. config->adaptive_ifs = nic->adaptive_ifs;
  974. config->loopback = nic->loopback;
  975. if (nic->mii.force_media && nic->mii.full_duplex)
  976. config->full_duplex_force = 0x1; /* 1=force, 0=auto */
  977. if (nic->flags & promiscuous || nic->loopback) {
  978. config->rx_save_bad_frames = 0x1; /* 1=save, 0=discard */
  979. config->rx_discard_short_frames = 0x0; /* 1=discard, 0=save */
  980. config->promiscuous_mode = 0x1; /* 1=on, 0=off */
  981. }
  982. if (unlikely(netdev->features & NETIF_F_RXFCS))
  983. config->rx_crc_transfer = 0x1; /* 1=save, 0=discard */
  984. if (nic->flags & multicast_all)
  985. config->multicast_all = 0x1; /* 1=accept, 0=no */
  986. /* disable WoL when up */
  987. if (netif_running(nic->netdev) || !(nic->flags & wol_magic))
  988. config->magic_packet_disable = 0x1; /* 1=off, 0=on */
  989. if (nic->mac >= mac_82558_D101_A4) {
  990. config->fc_disable = 0x1; /* 1=Tx fc off, 0=Tx fc on */
  991. config->mwi_enable = 0x1; /* 1=enable, 0=disable */
  992. config->standard_tcb = 0x0; /* 1=standard, 0=extended */
  993. config->rx_long_ok = 0x1; /* 1=VLANs ok, 0=standard */
  994. if (nic->mac >= mac_82559_D101M) {
  995. config->tno_intr = 0x1; /* TCO stats enable */
  996. /* Enable TCO in extended config */
  997. if (nic->mac >= mac_82551_10) {
  998. config->byte_count = 0x20; /* extended bytes */
  999. config->rx_d102_mode = 0x1; /* GMRC for TCO */
  1000. }
  1001. } else {
  1002. config->standard_stat_counter = 0x0;
  1003. }
  1004. }
  1005. if (netdev->features & NETIF_F_RXALL) {
  1006. config->rx_save_overruns = 0x1; /* 1=save, 0=discard */
  1007. config->rx_save_bad_frames = 0x1; /* 1=save, 0=discard */
  1008. config->rx_discard_short_frames = 0x0; /* 1=discard, 0=save */
  1009. }
  1010. netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[00-07]=%8ph\n",
  1011. c + 0);
  1012. netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[08-15]=%8ph\n",
  1013. c + 8);
  1014. netif_printk(nic, hw, KERN_DEBUG, nic->netdev, "[16-23]=%8ph\n",
  1015. c + 16);
  1016. return 0;
  1017. }
  1018. /*************************************************************************
  1019. * CPUSaver parameters
  1020. *
  1021. * All CPUSaver parameters are 16-bit literals that are part of a
  1022. * "move immediate value" instruction. By changing the value of
  1023. * the literal in the instruction before the code is loaded, the
  1024. * driver can change the algorithm.
  1025. *
  1026. * INTDELAY - This loads the dead-man timer with its initial value.
  1027. * When this timer expires the interrupt is asserted, and the
  1028. * timer is reset each time a new packet is received. (see
  1029. * BUNDLEMAX below to set the limit on number of chained packets)
  1030. * The current default is 0x600 or 1536. Experiments show that
  1031. * the value should probably stay within the 0x200 - 0x1000.
  1032. *
  1033. * BUNDLEMAX -
  1034. * This sets the maximum number of frames that will be bundled. In
  1035. * some situations, such as the TCP windowing algorithm, it may be
  1036. * better to limit the growth of the bundle size than let it go as
  1037. * high as it can, because that could cause too much added latency.
  1038. * The default is six, because this is the number of packets in the
  1039. * default TCP window size. A value of 1 would make CPUSaver indicate
  1040. * an interrupt for every frame received. If you do not want to put
  1041. * a limit on the bundle size, set this value to xFFFF.
  1042. *
  1043. * BUNDLESMALL -
  1044. * This contains a bit-mask describing the minimum size frame that
  1045. * will be bundled. The default masks the lower 7 bits, which means
  1046. * that any frame less than 128 bytes in length will not be bundled,
  1047. * but will instead immediately generate an interrupt. This does
  1048. * not affect the current bundle in any way. Any frame that is 128
  1049. * bytes or large will be bundled normally. This feature is meant
  1050. * to provide immediate indication of ACK frames in a TCP environment.
  1051. * Customers were seeing poor performance when a machine with CPUSaver
  1052. * enabled was sending but not receiving. The delay introduced when
  1053. * the ACKs were received was enough to reduce total throughput, because
  1054. * the sender would sit idle until the ACK was finally seen.
  1055. *
  1056. * The current default is 0xFF80, which masks out the lower 7 bits.
  1057. * This means that any frame which is x7F (127) bytes or smaller
  1058. * will cause an immediate interrupt. Because this value must be a
  1059. * bit mask, there are only a few valid values that can be used. To
  1060. * turn this feature off, the driver can write the value xFFFF to the
  1061. * lower word of this instruction (in the same way that the other
  1062. * parameters are used). Likewise, a value of 0xF800 (2047) would
  1063. * cause an interrupt to be generated for every frame, because all
  1064. * standard Ethernet frames are <= 2047 bytes in length.
  1065. *************************************************************************/
  1066. /* if you wish to disable the ucode functionality, while maintaining the
  1067. * workarounds it provides, set the following defines to:
  1068. * BUNDLESMALL 0
  1069. * BUNDLEMAX 1
  1070. * INTDELAY 1
  1071. */
  1072. #define BUNDLESMALL 1
  1073. #define BUNDLEMAX (u16)6
  1074. #define INTDELAY (u16)1536 /* 0x600 */
  1075. /* Initialize firmware */
  1076. static const struct firmware *e100_request_firmware(struct nic *nic)
  1077. {
  1078. const char *fw_name;
  1079. const struct firmware *fw = nic->fw;
  1080. u8 timer, bundle, min_size;
  1081. int err = 0;
  1082. bool required = false;
  1083. /* do not load u-code for ICH devices */
  1084. if (nic->flags & ich)
  1085. return NULL;
  1086. /* Search for ucode match against h/w revision
  1087. *
  1088. * Based on comments in the source code for the FreeBSD fxp
  1089. * driver, the FIRMWARE_D102E ucode includes both CPUSaver and
  1090. *
  1091. * "fixes for bugs in the B-step hardware (specifically, bugs
  1092. * with Inline Receive)."
  1093. *
  1094. * So we must fail if it cannot be loaded.
  1095. *
  1096. * The other microcode files are only required for the optional
  1097. * CPUSaver feature. Nice to have, but no reason to fail.
  1098. */
  1099. if (nic->mac == mac_82559_D101M) {
  1100. fw_name = FIRMWARE_D101M;
  1101. } else if (nic->mac == mac_82559_D101S) {
  1102. fw_name = FIRMWARE_D101S;
  1103. } else if (nic->mac == mac_82551_F || nic->mac == mac_82551_10) {
  1104. fw_name = FIRMWARE_D102E;
  1105. required = true;
  1106. } else { /* No ucode on other devices */
  1107. return NULL;
  1108. }
  1109. /* If the firmware has not previously been loaded, request a pointer
  1110. * to it. If it was previously loaded, we are reinitializing the
  1111. * adapter, possibly in a resume from hibernate, in which case
  1112. * request_firmware() cannot be used.
  1113. */
  1114. if (!fw)
  1115. err = request_firmware(&fw, fw_name, &nic->pdev->dev);
  1116. if (err) {
  1117. if (required) {
  1118. netif_err(nic, probe, nic->netdev,
  1119. "Failed to load firmware \"%s\": %d\n",
  1120. fw_name, err);
  1121. return ERR_PTR(err);
  1122. } else {
  1123. netif_info(nic, probe, nic->netdev,
  1124. "CPUSaver disabled. Needs \"%s\": %d\n",
  1125. fw_name, err);
  1126. return NULL;
  1127. }
  1128. }
  1129. /* Firmware should be precisely UCODE_SIZE (words) plus three bytes
  1130. indicating the offsets for BUNDLESMALL, BUNDLEMAX, INTDELAY */
  1131. if (fw->size != UCODE_SIZE * 4 + 3) {
  1132. netif_err(nic, probe, nic->netdev,
  1133. "Firmware \"%s\" has wrong size %zu\n",
  1134. fw_name, fw->size);
  1135. release_firmware(fw);
  1136. return ERR_PTR(-EINVAL);
  1137. }
  1138. /* Read timer, bundle and min_size from end of firmware blob */
  1139. timer = fw->data[UCODE_SIZE * 4];
  1140. bundle = fw->data[UCODE_SIZE * 4 + 1];
  1141. min_size = fw->data[UCODE_SIZE * 4 + 2];
  1142. if (timer >= UCODE_SIZE || bundle >= UCODE_SIZE ||
  1143. min_size >= UCODE_SIZE) {
  1144. netif_err(nic, probe, nic->netdev,
  1145. "\"%s\" has bogus offset values (0x%x,0x%x,0x%x)\n",
  1146. fw_name, timer, bundle, min_size);
  1147. release_firmware(fw);
  1148. return ERR_PTR(-EINVAL);
  1149. }
  1150. /* OK, firmware is validated and ready to use. Save a pointer
  1151. * to it in the nic */
  1152. nic->fw = fw;
  1153. return fw;
  1154. }
  1155. static int e100_setup_ucode(struct nic *nic, struct cb *cb,
  1156. struct sk_buff *skb)
  1157. {
  1158. const struct firmware *fw = (void *)skb;
  1159. u8 timer, bundle, min_size;
  1160. /* It's not a real skb; we just abused the fact that e100_exec_cb
  1161. will pass it through to here... */
  1162. cb->skb = NULL;
  1163. /* firmware is stored as little endian already */
  1164. memcpy(cb->u.ucode, fw->data, UCODE_SIZE * 4);
  1165. /* Read timer, bundle and min_size from end of firmware blob */
  1166. timer = fw->data[UCODE_SIZE * 4];
  1167. bundle = fw->data[UCODE_SIZE * 4 + 1];
  1168. min_size = fw->data[UCODE_SIZE * 4 + 2];
  1169. /* Insert user-tunable settings in cb->u.ucode */
  1170. cb->u.ucode[timer] &= cpu_to_le32(0xFFFF0000);
  1171. cb->u.ucode[timer] |= cpu_to_le32(INTDELAY);
  1172. cb->u.ucode[bundle] &= cpu_to_le32(0xFFFF0000);
  1173. cb->u.ucode[bundle] |= cpu_to_le32(BUNDLEMAX);
  1174. cb->u.ucode[min_size] &= cpu_to_le32(0xFFFF0000);
  1175. cb->u.ucode[min_size] |= cpu_to_le32((BUNDLESMALL) ? 0xFFFF : 0xFF80);
  1176. cb->command = cpu_to_le16(cb_ucode | cb_el);
  1177. return 0;
  1178. }
  1179. static inline int e100_load_ucode_wait(struct nic *nic)
  1180. {
  1181. const struct firmware *fw;
  1182. int err = 0, counter = 50;
  1183. struct cb *cb = nic->cb_to_clean;
  1184. fw = e100_request_firmware(nic);
  1185. /* If it's NULL, then no ucode is required */
  1186. if (IS_ERR_OR_NULL(fw))
  1187. return PTR_ERR_OR_ZERO(fw);
  1188. if ((err = e100_exec_cb(nic, (void *)fw, e100_setup_ucode)))
  1189. netif_err(nic, probe, nic->netdev,
  1190. "ucode cmd failed with error %d\n", err);
  1191. /* must restart cuc */
  1192. nic->cuc_cmd = cuc_start;
  1193. /* wait for completion */
  1194. e100_write_flush(nic);
  1195. udelay(10);
  1196. /* wait for possibly (ouch) 500ms */
  1197. while (!(cb->status & cpu_to_le16(cb_complete))) {
  1198. msleep(10);
  1199. if (!--counter) break;
  1200. }
  1201. /* ack any interrupts, something could have been set */
  1202. iowrite8(~0, &nic->csr->scb.stat_ack);
  1203. /* if the command failed, or is not OK, notify and return */
  1204. if (!counter || !(cb->status & cpu_to_le16(cb_ok))) {
  1205. netif_err(nic, probe, nic->netdev, "ucode load failed\n");
  1206. err = -EPERM;
  1207. }
  1208. return err;
  1209. }
  1210. static int e100_setup_iaaddr(struct nic *nic, struct cb *cb,
  1211. struct sk_buff *skb)
  1212. {
  1213. cb->command = cpu_to_le16(cb_iaaddr);
  1214. memcpy(cb->u.iaaddr, nic->netdev->dev_addr, ETH_ALEN);
  1215. return 0;
  1216. }
  1217. static int e100_dump(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  1218. {
  1219. cb->command = cpu_to_le16(cb_dump);
  1220. cb->u.dump_buffer_addr = cpu_to_le32(nic->dma_addr +
  1221. offsetof(struct mem, dump_buf));
  1222. return 0;
  1223. }
  1224. static int e100_phy_check_without_mii(struct nic *nic)
  1225. {
  1226. u8 phy_type;
  1227. int without_mii;
  1228. phy_type = (le16_to_cpu(nic->eeprom[eeprom_phy_iface]) >> 8) & 0x0f;
  1229. switch (phy_type) {
  1230. case NoSuchPhy: /* Non-MII PHY; UNTESTED! */
  1231. case I82503: /* Non-MII PHY; UNTESTED! */
  1232. case S80C24: /* Non-MII PHY; tested and working */
  1233. /* paragraph from the FreeBSD driver, "FXP_PHY_80C24":
  1234. * The Seeq 80c24 AutoDUPLEX(tm) Ethernet Interface Adapter
  1235. * doesn't have a programming interface of any sort. The
  1236. * media is sensed automatically based on how the link partner
  1237. * is configured. This is, in essence, manual configuration.
  1238. */
  1239. netif_info(nic, probe, nic->netdev,
  1240. "found MII-less i82503 or 80c24 or other PHY\n");
  1241. nic->mdio_ctrl = mdio_ctrl_phy_mii_emulated;
  1242. nic->mii.phy_id = 0; /* is this ok for an MII-less PHY? */
  1243. /* these might be needed for certain MII-less cards...
  1244. * nic->flags |= ich;
  1245. * nic->flags |= ich_10h_workaround; */
  1246. without_mii = 1;
  1247. break;
  1248. default:
  1249. without_mii = 0;
  1250. break;
  1251. }
  1252. return without_mii;
  1253. }
  1254. #define NCONFIG_AUTO_SWITCH 0x0080
  1255. #define MII_NSC_CONG MII_RESV1
  1256. #define NSC_CONG_ENABLE 0x0100
  1257. #define NSC_CONG_TXREADY 0x0400
  1258. static int e100_phy_init(struct nic *nic)
  1259. {
  1260. struct net_device *netdev = nic->netdev;
  1261. u32 addr;
  1262. u16 bmcr, stat, id_lo, id_hi, cong;
  1263. /* Discover phy addr by searching addrs in order {1,0,2,..., 31} */
  1264. for (addr = 0; addr < 32; addr++) {
  1265. nic->mii.phy_id = (addr == 0) ? 1 : (addr == 1) ? 0 : addr;
  1266. bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
  1267. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  1268. stat = mdio_read(netdev, nic->mii.phy_id, MII_BMSR);
  1269. if (!((bmcr == 0xFFFF) || ((stat == 0) && (bmcr == 0))))
  1270. break;
  1271. }
  1272. if (addr == 32) {
  1273. /* uhoh, no PHY detected: check whether we seem to be some
  1274. * weird, rare variant which is *known* to not have any MII.
  1275. * But do this AFTER MII checking only, since this does
  1276. * lookup of EEPROM values which may easily be unreliable. */
  1277. if (e100_phy_check_without_mii(nic))
  1278. return 0; /* simply return and hope for the best */
  1279. else {
  1280. /* for unknown cases log a fatal error */
  1281. netif_err(nic, hw, nic->netdev,
  1282. "Failed to locate any known PHY, aborting\n");
  1283. return -EAGAIN;
  1284. }
  1285. } else
  1286. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  1287. "phy_addr = %d\n", nic->mii.phy_id);
  1288. /* Get phy ID */
  1289. id_lo = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID1);
  1290. id_hi = mdio_read(netdev, nic->mii.phy_id, MII_PHYSID2);
  1291. nic->phy = (u32)id_hi << 16 | (u32)id_lo;
  1292. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  1293. "phy ID = 0x%08X\n", nic->phy);
  1294. /* Select the phy and isolate the rest */
  1295. for (addr = 0; addr < 32; addr++) {
  1296. if (addr != nic->mii.phy_id) {
  1297. mdio_write(netdev, addr, MII_BMCR, BMCR_ISOLATE);
  1298. } else if (nic->phy != phy_82552_v) {
  1299. bmcr = mdio_read(netdev, addr, MII_BMCR);
  1300. mdio_write(netdev, addr, MII_BMCR,
  1301. bmcr & ~BMCR_ISOLATE);
  1302. }
  1303. }
  1304. /*
  1305. * Workaround for 82552:
  1306. * Clear the ISOLATE bit on selected phy_id last (mirrored on all
  1307. * other phy_id's) using bmcr value from addr discovery loop above.
  1308. */
  1309. if (nic->phy == phy_82552_v)
  1310. mdio_write(netdev, nic->mii.phy_id, MII_BMCR,
  1311. bmcr & ~BMCR_ISOLATE);
  1312. /* Handle National tx phys */
  1313. #define NCS_PHY_MODEL_MASK 0xFFF0FFFF
  1314. if ((nic->phy & NCS_PHY_MODEL_MASK) == phy_nsc_tx) {
  1315. /* Disable congestion control */
  1316. cong = mdio_read(netdev, nic->mii.phy_id, MII_NSC_CONG);
  1317. cong |= NSC_CONG_TXREADY;
  1318. cong &= ~NSC_CONG_ENABLE;
  1319. mdio_write(netdev, nic->mii.phy_id, MII_NSC_CONG, cong);
  1320. }
  1321. if (nic->phy == phy_82552_v) {
  1322. u16 advert = mdio_read(netdev, nic->mii.phy_id, MII_ADVERTISE);
  1323. /* assign special tweaked mdio_ctrl() function */
  1324. nic->mdio_ctrl = mdio_ctrl_phy_82552_v;
  1325. /* Workaround Si not advertising flow-control during autoneg */
  1326. advert |= ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM;
  1327. mdio_write(netdev, nic->mii.phy_id, MII_ADVERTISE, advert);
  1328. /* Reset for the above changes to take effect */
  1329. bmcr = mdio_read(netdev, nic->mii.phy_id, MII_BMCR);
  1330. bmcr |= BMCR_RESET;
  1331. mdio_write(netdev, nic->mii.phy_id, MII_BMCR, bmcr);
  1332. } else if ((nic->mac >= mac_82550_D102) || ((nic->flags & ich) &&
  1333. (mdio_read(netdev, nic->mii.phy_id, MII_TPISTATUS) & 0x8000) &&
  1334. (le16_to_cpu(nic->eeprom[eeprom_cnfg_mdix]) & eeprom_mdix_enabled))) {
  1335. /* enable/disable MDI/MDI-X auto-switching. */
  1336. mdio_write(netdev, nic->mii.phy_id, MII_NCONFIG,
  1337. nic->mii.force_media ? 0 : NCONFIG_AUTO_SWITCH);
  1338. }
  1339. return 0;
  1340. }
  1341. static int e100_hw_init(struct nic *nic)
  1342. {
  1343. int err = 0;
  1344. e100_hw_reset(nic);
  1345. netif_err(nic, hw, nic->netdev, "e100_hw_init\n");
  1346. if ((err = e100_self_test(nic)))
  1347. return err;
  1348. if ((err = e100_phy_init(nic)))
  1349. return err;
  1350. if ((err = e100_exec_cmd(nic, cuc_load_base, 0)))
  1351. return err;
  1352. if ((err = e100_exec_cmd(nic, ruc_load_base, 0)))
  1353. return err;
  1354. if ((err = e100_load_ucode_wait(nic)))
  1355. return err;
  1356. if ((err = e100_exec_cb(nic, NULL, e100_configure)))
  1357. return err;
  1358. if ((err = e100_exec_cb(nic, NULL, e100_setup_iaaddr)))
  1359. return err;
  1360. if ((err = e100_exec_cmd(nic, cuc_dump_addr,
  1361. nic->dma_addr + offsetof(struct mem, stats))))
  1362. return err;
  1363. if ((err = e100_exec_cmd(nic, cuc_dump_reset, 0)))
  1364. return err;
  1365. e100_disable_irq(nic);
  1366. return 0;
  1367. }
  1368. static int e100_multi(struct nic *nic, struct cb *cb, struct sk_buff *skb)
  1369. {
  1370. struct net_device *netdev = nic->netdev;
  1371. struct netdev_hw_addr *ha;
  1372. u16 i, count = min(netdev_mc_count(netdev), E100_MAX_MULTICAST_ADDRS);
  1373. cb->command = cpu_to_le16(cb_multi);
  1374. cb->u.multi.count = cpu_to_le16(count * ETH_ALEN);
  1375. i = 0;
  1376. netdev_for_each_mc_addr(ha, netdev) {
  1377. if (i == count)
  1378. break;
  1379. memcpy(&cb->u.multi.addr[i++ * ETH_ALEN], &ha->addr,
  1380. ETH_ALEN);
  1381. }
  1382. return 0;
  1383. }
  1384. static void e100_set_multicast_list(struct net_device *netdev)
  1385. {
  1386. struct nic *nic = netdev_priv(netdev);
  1387. netif_printk(nic, hw, KERN_DEBUG, nic->netdev,
  1388. "mc_count=%d, flags=0x%04X\n",
  1389. netdev_mc_count(netdev), netdev->flags);
  1390. if (netdev->flags & IFF_PROMISC)
  1391. nic->flags |= promiscuous;
  1392. else
  1393. nic->flags &= ~promiscuous;
  1394. if (netdev->flags & IFF_ALLMULTI ||
  1395. netdev_mc_count(netdev) > E100_MAX_MULTICAST_ADDRS)
  1396. nic->flags |= multicast_all;
  1397. else
  1398. nic->flags &= ~multicast_all;
  1399. e100_exec_cb(nic, NULL, e100_configure);
  1400. e100_exec_cb(nic, NULL, e100_multi);
  1401. }
  1402. static void e100_update_stats(struct nic *nic)
  1403. {
  1404. struct net_device *dev = nic->netdev;
  1405. struct net_device_stats *ns = &dev->stats;
  1406. struct stats *s = &nic->mem->stats;
  1407. __le32 *complete = (nic->mac < mac_82558_D101_A4) ? &s->fc_xmt_pause :
  1408. (nic->mac < mac_82559_D101M) ? (__le32 *)&s->xmt_tco_frames :
  1409. &s->complete;
  1410. /* Device's stats reporting may take several microseconds to
  1411. * complete, so we're always waiting for results of the
  1412. * previous command. */
  1413. if (*complete == cpu_to_le32(cuc_dump_reset_complete)) {
  1414. *complete = 0;
  1415. nic->tx_frames = le32_to_cpu(s->tx_good_frames);
  1416. nic->tx_collisions = le32_to_cpu(s->tx_total_collisions);
  1417. ns->tx_aborted_errors += le32_to_cpu(s->tx_max_collisions);
  1418. ns->tx_window_errors += le32_to_cpu(s->tx_late_collisions);
  1419. ns->tx_carrier_errors += le32_to_cpu(s->tx_lost_crs);
  1420. ns->tx_fifo_errors += le32_to_cpu(s->tx_underruns);
  1421. ns->collisions += nic->tx_collisions;
  1422. ns->tx_errors += le32_to_cpu(s->tx_max_collisions) +
  1423. le32_to_cpu(s->tx_lost_crs);
  1424. nic->rx_short_frame_errors +=
  1425. le32_to_cpu(s->rx_short_frame_errors);
  1426. ns->rx_length_errors = nic->rx_short_frame_errors +
  1427. nic->rx_over_length_errors;
  1428. ns->rx_crc_errors += le32_to_cpu(s->rx_crc_errors);
  1429. ns->rx_frame_errors += le32_to_cpu(s->rx_alignment_errors);
  1430. ns->rx_over_errors += le32_to_cpu(s->rx_overrun_errors);
  1431. ns->rx_fifo_errors += le32_to_cpu(s->rx_overrun_errors);
  1432. ns->rx_missed_errors += le32_to_cpu(s->rx_resource_errors);
  1433. ns->rx_errors += le32_to_cpu(s->rx_crc_errors) +
  1434. le32_to_cpu(s->rx_alignment_errors) +
  1435. le32_to_cpu(s->rx_short_frame_errors) +
  1436. le32_to_cpu(s->rx_cdt_errors);
  1437. nic->tx_deferred += le32_to_cpu(s->tx_deferred);
  1438. nic->tx_single_collisions +=
  1439. le32_to_cpu(s->tx_single_collisions);
  1440. nic->tx_multiple_collisions +=
  1441. le32_to_cpu(s->tx_multiple_collisions);
  1442. if (nic->mac >= mac_82558_D101_A4) {
  1443. nic->tx_fc_pause += le32_to_cpu(s->fc_xmt_pause);
  1444. nic->rx_fc_pause += le32_to_cpu(s->fc_rcv_pause);
  1445. nic->rx_fc_unsupported +=
  1446. le32_to_cpu(s->fc_rcv_unsupported);
  1447. if (nic->mac >= mac_82559_D101M) {
  1448. nic->tx_tco_frames +=
  1449. le16_to_cpu(s->xmt_tco_frames);
  1450. nic->rx_tco_frames +=
  1451. le16_to_cpu(s->rcv_tco_frames);
  1452. }
  1453. }
  1454. }
  1455. if (e100_exec_cmd(nic, cuc_dump_reset, 0))
  1456. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  1457. "exec cuc_dump_reset failed\n");
  1458. }
  1459. static void e100_adjust_adaptive_ifs(struct nic *nic, int speed, int duplex)
  1460. {
  1461. /* Adjust inter-frame-spacing (IFS) between two transmits if
  1462. * we're getting collisions on a half-duplex connection. */
  1463. if (duplex == DUPLEX_HALF) {
  1464. u32 prev = nic->adaptive_ifs;
  1465. u32 min_frames = (speed == SPEED_100) ? 1000 : 100;
  1466. if ((nic->tx_frames / 32 < nic->tx_collisions) &&
  1467. (nic->tx_frames > min_frames)) {
  1468. if (nic->adaptive_ifs < 60)
  1469. nic->adaptive_ifs += 5;
  1470. } else if (nic->tx_frames < min_frames) {
  1471. if (nic->adaptive_ifs >= 5)
  1472. nic->adaptive_ifs -= 5;
  1473. }
  1474. if (nic->adaptive_ifs != prev)
  1475. e100_exec_cb(nic, NULL, e100_configure);
  1476. }
  1477. }
  1478. static void e100_watchdog(struct timer_list *t)
  1479. {
  1480. struct nic *nic = from_timer(nic, t, watchdog);
  1481. struct ethtool_cmd cmd = { .cmd = ETHTOOL_GSET };
  1482. u32 speed;
  1483. netif_printk(nic, timer, KERN_DEBUG, nic->netdev,
  1484. "right now = %ld\n", jiffies);
  1485. /* mii library handles link maintenance tasks */
  1486. mii_ethtool_gset(&nic->mii, &cmd);
  1487. speed = ethtool_cmd_speed(&cmd);
  1488. if (mii_link_ok(&nic->mii) && !netif_carrier_ok(nic->netdev)) {
  1489. netdev_info(nic->netdev, "NIC Link is Up %u Mbps %s Duplex\n",
  1490. speed == SPEED_100 ? 100 : 10,
  1491. cmd.duplex == DUPLEX_FULL ? "Full" : "Half");
  1492. } else if (!mii_link_ok(&nic->mii) && netif_carrier_ok(nic->netdev)) {
  1493. netdev_info(nic->netdev, "NIC Link is Down\n");
  1494. }
  1495. mii_check_link(&nic->mii);
  1496. /* Software generated interrupt to recover from (rare) Rx
  1497. * allocation failure.
  1498. * Unfortunately have to use a spinlock to not re-enable interrupts
  1499. * accidentally, due to hardware that shares a register between the
  1500. * interrupt mask bit and the SW Interrupt generation bit */
  1501. spin_lock_irq(&nic->cmd_lock);
  1502. iowrite8(ioread8(&nic->csr->scb.cmd_hi) | irq_sw_gen,&nic->csr->scb.cmd_hi);
  1503. e100_write_flush(nic);
  1504. spin_unlock_irq(&nic->cmd_lock);
  1505. e100_update_stats(nic);
  1506. e100_adjust_adaptive_ifs(nic, speed, cmd.duplex);
  1507. if (nic->mac <= mac_82557_D100_C)
  1508. /* Issue a multicast command to workaround a 557 lock up */
  1509. e100_set_multicast_list(nic->netdev);
  1510. if (nic->flags & ich && speed == SPEED_10 && cmd.duplex == DUPLEX_HALF)
  1511. /* Need SW workaround for ICH[x] 10Mbps/half duplex Tx hang. */
  1512. nic->flags |= ich_10h_workaround;
  1513. else
  1514. nic->flags &= ~ich_10h_workaround;
  1515. mod_timer(&nic->watchdog,
  1516. round_jiffies(jiffies + E100_WATCHDOG_PERIOD));
  1517. }
  1518. static int e100_xmit_prepare(struct nic *nic, struct cb *cb,
  1519. struct sk_buff *skb)
  1520. {
  1521. dma_addr_t dma_addr;
  1522. cb->command = nic->tx_command;
  1523. dma_addr = dma_map_single(&nic->pdev->dev, skb->data, skb->len,
  1524. DMA_TO_DEVICE);
  1525. /* If we can't map the skb, have the upper layer try later */
  1526. if (dma_mapping_error(&nic->pdev->dev, dma_addr))
  1527. return -ENOMEM;
  1528. /*
  1529. * Use the last 4 bytes of the SKB payload packet as the CRC, used for
  1530. * testing, ie sending frames with bad CRC.
  1531. */
  1532. if (unlikely(skb->no_fcs))
  1533. cb->command |= cpu_to_le16(cb_tx_nc);
  1534. else
  1535. cb->command &= ~cpu_to_le16(cb_tx_nc);
  1536. /* interrupt every 16 packets regardless of delay */
  1537. if ((nic->cbs_avail & ~15) == nic->cbs_avail)
  1538. cb->command |= cpu_to_le16(cb_i);
  1539. cb->u.tcb.tbd_array = cb->dma_addr + offsetof(struct cb, u.tcb.tbd);
  1540. cb->u.tcb.tcb_byte_count = 0;
  1541. cb->u.tcb.threshold = nic->tx_threshold;
  1542. cb->u.tcb.tbd_count = 1;
  1543. cb->u.tcb.tbd.buf_addr = cpu_to_le32(dma_addr);
  1544. cb->u.tcb.tbd.size = cpu_to_le16(skb->len);
  1545. skb_tx_timestamp(skb);
  1546. return 0;
  1547. }
  1548. static netdev_tx_t e100_xmit_frame(struct sk_buff *skb,
  1549. struct net_device *netdev)
  1550. {
  1551. struct nic *nic = netdev_priv(netdev);
  1552. int err;
  1553. if (nic->flags & ich_10h_workaround) {
  1554. /* SW workaround for ICH[x] 10Mbps/half duplex Tx hang.
  1555. Issue a NOP command followed by a 1us delay before
  1556. issuing the Tx command. */
  1557. if (e100_exec_cmd(nic, cuc_nop, 0))
  1558. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  1559. "exec cuc_nop failed\n");
  1560. udelay(1);
  1561. }
  1562. err = e100_exec_cb(nic, skb, e100_xmit_prepare);
  1563. switch (err) {
  1564. case -ENOSPC:
  1565. /* We queued the skb, but now we're out of space. */
  1566. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  1567. "No space for CB\n");
  1568. netif_stop_queue(netdev);
  1569. break;
  1570. case -ENOMEM:
  1571. /* This is a hard error - log it. */
  1572. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  1573. "Out of Tx resources, returning skb\n");
  1574. netif_stop_queue(netdev);
  1575. return NETDEV_TX_BUSY;
  1576. }
  1577. return NETDEV_TX_OK;
  1578. }
  1579. static int e100_tx_clean(struct nic *nic)
  1580. {
  1581. struct net_device *dev = nic->netdev;
  1582. struct cb *cb;
  1583. int tx_cleaned = 0;
  1584. spin_lock(&nic->cb_lock);
  1585. /* Clean CBs marked complete */
  1586. for (cb = nic->cb_to_clean;
  1587. cb->status & cpu_to_le16(cb_complete);
  1588. cb = nic->cb_to_clean = cb->next) {
  1589. dma_rmb(); /* read skb after status */
  1590. netif_printk(nic, tx_done, KERN_DEBUG, nic->netdev,
  1591. "cb[%d]->status = 0x%04X\n",
  1592. (int)(((void*)cb - (void*)nic->cbs)/sizeof(struct cb)),
  1593. cb->status);
  1594. if (likely(cb->skb != NULL)) {
  1595. dev->stats.tx_packets++;
  1596. dev->stats.tx_bytes += cb->skb->len;
  1597. dma_unmap_single(&nic->pdev->dev,
  1598. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1599. le16_to_cpu(cb->u.tcb.tbd.size),
  1600. DMA_TO_DEVICE);
  1601. dev_kfree_skb_any(cb->skb);
  1602. cb->skb = NULL;
  1603. tx_cleaned = 1;
  1604. }
  1605. cb->status = 0;
  1606. nic->cbs_avail++;
  1607. }
  1608. spin_unlock(&nic->cb_lock);
  1609. /* Recover from running out of Tx resources in xmit_frame */
  1610. if (unlikely(tx_cleaned && netif_queue_stopped(nic->netdev)))
  1611. netif_wake_queue(nic->netdev);
  1612. return tx_cleaned;
  1613. }
  1614. static void e100_clean_cbs(struct nic *nic)
  1615. {
  1616. if (nic->cbs) {
  1617. while (nic->cbs_avail != nic->params.cbs.count) {
  1618. struct cb *cb = nic->cb_to_clean;
  1619. if (cb->skb) {
  1620. dma_unmap_single(&nic->pdev->dev,
  1621. le32_to_cpu(cb->u.tcb.tbd.buf_addr),
  1622. le16_to_cpu(cb->u.tcb.tbd.size),
  1623. DMA_TO_DEVICE);
  1624. dev_kfree_skb(cb->skb);
  1625. }
  1626. nic->cb_to_clean = nic->cb_to_clean->next;
  1627. nic->cbs_avail++;
  1628. }
  1629. dma_pool_free(nic->cbs_pool, nic->cbs, nic->cbs_dma_addr);
  1630. nic->cbs = NULL;
  1631. nic->cbs_avail = 0;
  1632. }
  1633. nic->cuc_cmd = cuc_start;
  1634. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean =
  1635. nic->cbs;
  1636. }
  1637. static int e100_alloc_cbs(struct nic *nic)
  1638. {
  1639. struct cb *cb;
  1640. unsigned int i, count = nic->params.cbs.count;
  1641. nic->cuc_cmd = cuc_start;
  1642. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = NULL;
  1643. nic->cbs_avail = 0;
  1644. nic->cbs = dma_pool_zalloc(nic->cbs_pool, GFP_KERNEL,
  1645. &nic->cbs_dma_addr);
  1646. if (!nic->cbs)
  1647. return -ENOMEM;
  1648. for (cb = nic->cbs, i = 0; i < count; cb++, i++) {
  1649. cb->next = (i + 1 < count) ? cb + 1 : nic->cbs;
  1650. cb->prev = (i == 0) ? nic->cbs + count - 1 : cb - 1;
  1651. cb->dma_addr = nic->cbs_dma_addr + i * sizeof(struct cb);
  1652. cb->link = cpu_to_le32(nic->cbs_dma_addr +
  1653. ((i+1) % count) * sizeof(struct cb));
  1654. }
  1655. nic->cb_to_use = nic->cb_to_send = nic->cb_to_clean = nic->cbs;
  1656. nic->cbs_avail = count;
  1657. return 0;
  1658. }
  1659. static inline void e100_start_receiver(struct nic *nic, struct rx *rx)
  1660. {
  1661. if (!nic->rxs) return;
  1662. if (RU_SUSPENDED != nic->ru_running) return;
  1663. /* handle init time starts */
  1664. if (!rx) rx = nic->rxs;
  1665. /* (Re)start RU if suspended or idle and RFA is non-NULL */
  1666. if (rx->skb) {
  1667. e100_exec_cmd(nic, ruc_start, rx->dma_addr);
  1668. nic->ru_running = RU_RUNNING;
  1669. }
  1670. }
  1671. #define RFD_BUF_LEN (sizeof(struct rfd) + VLAN_ETH_FRAME_LEN + ETH_FCS_LEN)
  1672. static int e100_rx_alloc_skb(struct nic *nic, struct rx *rx)
  1673. {
  1674. if (!(rx->skb = netdev_alloc_skb_ip_align(nic->netdev, RFD_BUF_LEN)))
  1675. return -ENOMEM;
  1676. /* Init, and map the RFD. */
  1677. skb_copy_to_linear_data(rx->skb, &nic->blank_rfd, sizeof(struct rfd));
  1678. rx->dma_addr = dma_map_single(&nic->pdev->dev, rx->skb->data,
  1679. RFD_BUF_LEN, DMA_BIDIRECTIONAL);
  1680. if (dma_mapping_error(&nic->pdev->dev, rx->dma_addr)) {
  1681. dev_kfree_skb_any(rx->skb);
  1682. rx->skb = NULL;
  1683. rx->dma_addr = 0;
  1684. return -ENOMEM;
  1685. }
  1686. /* Link the RFD to end of RFA by linking previous RFD to
  1687. * this one. We are safe to touch the previous RFD because
  1688. * it is protected by the before last buffer's el bit being set */
  1689. if (rx->prev->skb) {
  1690. struct rfd *prev_rfd = (struct rfd *)rx->prev->skb->data;
  1691. put_unaligned_le32(rx->dma_addr, &prev_rfd->link);
  1692. dma_sync_single_for_device(&nic->pdev->dev,
  1693. rx->prev->dma_addr,
  1694. sizeof(struct rfd),
  1695. DMA_BIDIRECTIONAL);
  1696. }
  1697. return 0;
  1698. }
  1699. static int e100_rx_indicate(struct nic *nic, struct rx *rx,
  1700. unsigned int *work_done, unsigned int work_to_do)
  1701. {
  1702. struct net_device *dev = nic->netdev;
  1703. struct sk_buff *skb = rx->skb;
  1704. struct rfd *rfd = (struct rfd *)skb->data;
  1705. u16 rfd_status, actual_size;
  1706. u16 fcs_pad = 0;
  1707. if (unlikely(work_done && *work_done >= work_to_do))
  1708. return -EAGAIN;
  1709. /* Need to sync before taking a peek at cb_complete bit */
  1710. dma_sync_single_for_cpu(&nic->pdev->dev, rx->dma_addr,
  1711. sizeof(struct rfd), DMA_BIDIRECTIONAL);
  1712. rfd_status = le16_to_cpu(rfd->status);
  1713. netif_printk(nic, rx_status, KERN_DEBUG, nic->netdev,
  1714. "status=0x%04X\n", rfd_status);
  1715. dma_rmb(); /* read size after status bit */
  1716. /* If data isn't ready, nothing to indicate */
  1717. if (unlikely(!(rfd_status & cb_complete))) {
  1718. /* If the next buffer has the el bit, but we think the receiver
  1719. * is still running, check to see if it really stopped while
  1720. * we had interrupts off.
  1721. * This allows for a fast restart without re-enabling
  1722. * interrupts */
  1723. if ((le16_to_cpu(rfd->command) & cb_el) &&
  1724. (RU_RUNNING == nic->ru_running))
  1725. if (ioread8(&nic->csr->scb.status) & rus_no_res)
  1726. nic->ru_running = RU_SUSPENDED;
  1727. dma_sync_single_for_device(&nic->pdev->dev, rx->dma_addr,
  1728. sizeof(struct rfd),
  1729. DMA_FROM_DEVICE);
  1730. return -ENODATA;
  1731. }
  1732. /* Get actual data size */
  1733. if (unlikely(dev->features & NETIF_F_RXFCS))
  1734. fcs_pad = 4;
  1735. actual_size = le16_to_cpu(rfd->actual_size) & 0x3FFF;
  1736. if (unlikely(actual_size > RFD_BUF_LEN - sizeof(struct rfd)))
  1737. actual_size = RFD_BUF_LEN - sizeof(struct rfd);
  1738. /* Get data */
  1739. dma_unmap_single(&nic->pdev->dev, rx->dma_addr, RFD_BUF_LEN,
  1740. DMA_BIDIRECTIONAL);
  1741. /* If this buffer has the el bit, but we think the receiver
  1742. * is still running, check to see if it really stopped while
  1743. * we had interrupts off.
  1744. * This allows for a fast restart without re-enabling interrupts.
  1745. * This can happen when the RU sees the size change but also sees
  1746. * the el bit set. */
  1747. if ((le16_to_cpu(rfd->command) & cb_el) &&
  1748. (RU_RUNNING == nic->ru_running)) {
  1749. if (ioread8(&nic->csr->scb.status) & rus_no_res)
  1750. nic->ru_running = RU_SUSPENDED;
  1751. }
  1752. /* Pull off the RFD and put the actual data (minus eth hdr) */
  1753. skb_reserve(skb, sizeof(struct rfd));
  1754. skb_put(skb, actual_size);
  1755. skb->protocol = eth_type_trans(skb, nic->netdev);
  1756. /* If we are receiving all frames, then don't bother
  1757. * checking for errors.
  1758. */
  1759. if (unlikely(dev->features & NETIF_F_RXALL)) {
  1760. if (actual_size > ETH_DATA_LEN + VLAN_ETH_HLEN + fcs_pad)
  1761. /* Received oversized frame, but keep it. */
  1762. nic->rx_over_length_errors++;
  1763. goto process_skb;
  1764. }
  1765. if (unlikely(!(rfd_status & cb_ok))) {
  1766. /* Don't indicate if hardware indicates errors */
  1767. dev_kfree_skb_any(skb);
  1768. } else if (actual_size > ETH_DATA_LEN + VLAN_ETH_HLEN + fcs_pad) {
  1769. /* Don't indicate oversized frames */
  1770. nic->rx_over_length_errors++;
  1771. dev_kfree_skb_any(skb);
  1772. } else {
  1773. process_skb:
  1774. dev->stats.rx_packets++;
  1775. dev->stats.rx_bytes += (actual_size - fcs_pad);
  1776. netif_receive_skb(skb);
  1777. if (work_done)
  1778. (*work_done)++;
  1779. }
  1780. rx->skb = NULL;
  1781. return 0;
  1782. }
  1783. static void e100_rx_clean(struct nic *nic, unsigned int *work_done,
  1784. unsigned int work_to_do)
  1785. {
  1786. struct rx *rx;
  1787. int restart_required = 0, err = 0;
  1788. struct rx *old_before_last_rx, *new_before_last_rx;
  1789. struct rfd *old_before_last_rfd, *new_before_last_rfd;
  1790. /* Indicate newly arrived packets */
  1791. for (rx = nic->rx_to_clean; rx->skb; rx = nic->rx_to_clean = rx->next) {
  1792. err = e100_rx_indicate(nic, rx, work_done, work_to_do);
  1793. /* Hit quota or no more to clean */
  1794. if (-EAGAIN == err || -ENODATA == err)
  1795. break;
  1796. }
  1797. /* On EAGAIN, hit quota so have more work to do, restart once
  1798. * cleanup is complete.
  1799. * Else, are we already rnr? then pay attention!!! this ensures that
  1800. * the state machine progression never allows a start with a
  1801. * partially cleaned list, avoiding a race between hardware
  1802. * and rx_to_clean when in NAPI mode */
  1803. if (-EAGAIN != err && RU_SUSPENDED == nic->ru_running)
  1804. restart_required = 1;
  1805. old_before_last_rx = nic->rx_to_use->prev->prev;
  1806. old_before_last_rfd = (struct rfd *)old_before_last_rx->skb->data;
  1807. /* Alloc new skbs to refill list */
  1808. for (rx = nic->rx_to_use; !rx->skb; rx = nic->rx_to_use = rx->next) {
  1809. if (unlikely(e100_rx_alloc_skb(nic, rx)))
  1810. break; /* Better luck next time (see watchdog) */
  1811. }
  1812. new_before_last_rx = nic->rx_to_use->prev->prev;
  1813. if (new_before_last_rx != old_before_last_rx) {
  1814. /* Set the el-bit on the buffer that is before the last buffer.
  1815. * This lets us update the next pointer on the last buffer
  1816. * without worrying about hardware touching it.
  1817. * We set the size to 0 to prevent hardware from touching this
  1818. * buffer.
  1819. * When the hardware hits the before last buffer with el-bit
  1820. * and size of 0, it will RNR interrupt, the RUS will go into
  1821. * the No Resources state. It will not complete nor write to
  1822. * this buffer. */
  1823. new_before_last_rfd =
  1824. (struct rfd *)new_before_last_rx->skb->data;
  1825. new_before_last_rfd->size = 0;
  1826. new_before_last_rfd->command |= cpu_to_le16(cb_el);
  1827. dma_sync_single_for_device(&nic->pdev->dev,
  1828. new_before_last_rx->dma_addr,
  1829. sizeof(struct rfd),
  1830. DMA_BIDIRECTIONAL);
  1831. /* Now that we have a new stopping point, we can clear the old
  1832. * stopping point. We must sync twice to get the proper
  1833. * ordering on the hardware side of things. */
  1834. old_before_last_rfd->command &= ~cpu_to_le16(cb_el);
  1835. dma_sync_single_for_device(&nic->pdev->dev,
  1836. old_before_last_rx->dma_addr,
  1837. sizeof(struct rfd),
  1838. DMA_BIDIRECTIONAL);
  1839. old_before_last_rfd->size = cpu_to_le16(VLAN_ETH_FRAME_LEN
  1840. + ETH_FCS_LEN);
  1841. dma_sync_single_for_device(&nic->pdev->dev,
  1842. old_before_last_rx->dma_addr,
  1843. sizeof(struct rfd),
  1844. DMA_BIDIRECTIONAL);
  1845. }
  1846. if (restart_required) {
  1847. // ack the rnr?
  1848. iowrite8(stat_ack_rnr, &nic->csr->scb.stat_ack);
  1849. e100_start_receiver(nic, nic->rx_to_clean);
  1850. if (work_done)
  1851. (*work_done)++;
  1852. }
  1853. }
  1854. static void e100_rx_clean_list(struct nic *nic)
  1855. {
  1856. struct rx *rx;
  1857. unsigned int i, count = nic->params.rfds.count;
  1858. nic->ru_running = RU_UNINITIALIZED;
  1859. if (nic->rxs) {
  1860. for (rx = nic->rxs, i = 0; i < count; rx++, i++) {
  1861. if (rx->skb) {
  1862. dma_unmap_single(&nic->pdev->dev,
  1863. rx->dma_addr, RFD_BUF_LEN,
  1864. DMA_BIDIRECTIONAL);
  1865. dev_kfree_skb(rx->skb);
  1866. }
  1867. }
  1868. kfree(nic->rxs);
  1869. nic->rxs = NULL;
  1870. }
  1871. nic->rx_to_use = nic->rx_to_clean = NULL;
  1872. }
  1873. static int e100_rx_alloc_list(struct nic *nic)
  1874. {
  1875. struct rx *rx;
  1876. unsigned int i, count = nic->params.rfds.count;
  1877. struct rfd *before_last;
  1878. nic->rx_to_use = nic->rx_to_clean = NULL;
  1879. nic->ru_running = RU_UNINITIALIZED;
  1880. if (!(nic->rxs = kcalloc(count, sizeof(struct rx), GFP_KERNEL)))
  1881. return -ENOMEM;
  1882. for (rx = nic->rxs, i = 0; i < count; rx++, i++) {
  1883. rx->next = (i + 1 < count) ? rx + 1 : nic->rxs;
  1884. rx->prev = (i == 0) ? nic->rxs + count - 1 : rx - 1;
  1885. if (e100_rx_alloc_skb(nic, rx)) {
  1886. e100_rx_clean_list(nic);
  1887. return -ENOMEM;
  1888. }
  1889. }
  1890. /* Set the el-bit on the buffer that is before the last buffer.
  1891. * This lets us update the next pointer on the last buffer without
  1892. * worrying about hardware touching it.
  1893. * We set the size to 0 to prevent hardware from touching this buffer.
  1894. * When the hardware hits the before last buffer with el-bit and size
  1895. * of 0, it will RNR interrupt, the RU will go into the No Resources
  1896. * state. It will not complete nor write to this buffer. */
  1897. rx = nic->rxs->prev->prev;
  1898. before_last = (struct rfd *)rx->skb->data;
  1899. before_last->command |= cpu_to_le16(cb_el);
  1900. before_last->size = 0;
  1901. dma_sync_single_for_device(&nic->pdev->dev, rx->dma_addr,
  1902. sizeof(struct rfd), DMA_BIDIRECTIONAL);
  1903. nic->rx_to_use = nic->rx_to_clean = nic->rxs;
  1904. nic->ru_running = RU_SUSPENDED;
  1905. return 0;
  1906. }
  1907. static irqreturn_t e100_intr(int irq, void *dev_id)
  1908. {
  1909. struct net_device *netdev = dev_id;
  1910. struct nic *nic = netdev_priv(netdev);
  1911. u8 stat_ack = ioread8(&nic->csr->scb.stat_ack);
  1912. netif_printk(nic, intr, KERN_DEBUG, nic->netdev,
  1913. "stat_ack = 0x%02X\n", stat_ack);
  1914. if (stat_ack == stat_ack_not_ours || /* Not our interrupt */
  1915. stat_ack == stat_ack_not_present) /* Hardware is ejected */
  1916. return IRQ_NONE;
  1917. /* Ack interrupt(s) */
  1918. iowrite8(stat_ack, &nic->csr->scb.stat_ack);
  1919. /* We hit Receive No Resource (RNR); restart RU after cleaning */
  1920. if (stat_ack & stat_ack_rnr)
  1921. nic->ru_running = RU_SUSPENDED;
  1922. if (likely(napi_schedule_prep(&nic->napi))) {
  1923. e100_disable_irq(nic);
  1924. __napi_schedule(&nic->napi);
  1925. }
  1926. return IRQ_HANDLED;
  1927. }
  1928. static int e100_poll(struct napi_struct *napi, int budget)
  1929. {
  1930. struct nic *nic = container_of(napi, struct nic, napi);
  1931. unsigned int work_done = 0;
  1932. e100_rx_clean(nic, &work_done, budget);
  1933. e100_tx_clean(nic);
  1934. /* If budget fully consumed, continue polling */
  1935. if (work_done == budget)
  1936. return budget;
  1937. /* only re-enable interrupt if stack agrees polling is really done */
  1938. if (likely(napi_complete_done(napi, work_done)))
  1939. e100_enable_irq(nic);
  1940. return work_done;
  1941. }
  1942. #ifdef CONFIG_NET_POLL_CONTROLLER
  1943. static void e100_netpoll(struct net_device *netdev)
  1944. {
  1945. struct nic *nic = netdev_priv(netdev);
  1946. e100_disable_irq(nic);
  1947. e100_intr(nic->pdev->irq, netdev);
  1948. e100_tx_clean(nic);
  1949. e100_enable_irq(nic);
  1950. }
  1951. #endif
  1952. static int e100_set_mac_address(struct net_device *netdev, void *p)
  1953. {
  1954. struct nic *nic = netdev_priv(netdev);
  1955. struct sockaddr *addr = p;
  1956. if (!is_valid_ether_addr(addr->sa_data))
  1957. return -EADDRNOTAVAIL;
  1958. eth_hw_addr_set(netdev, addr->sa_data);
  1959. e100_exec_cb(nic, NULL, e100_setup_iaaddr);
  1960. return 0;
  1961. }
  1962. static int e100_asf(struct nic *nic)
  1963. {
  1964. /* ASF can be enabled from eeprom */
  1965. return (nic->pdev->device >= 0x1050) && (nic->pdev->device <= 0x1057) &&
  1966. (le16_to_cpu(nic->eeprom[eeprom_config_asf]) & eeprom_asf) &&
  1967. !(le16_to_cpu(nic->eeprom[eeprom_config_asf]) & eeprom_gcl) &&
  1968. ((le16_to_cpu(nic->eeprom[eeprom_smbus_addr]) & 0xFF) != 0xFE);
  1969. }
  1970. static int e100_up(struct nic *nic)
  1971. {
  1972. int err;
  1973. if ((err = e100_rx_alloc_list(nic)))
  1974. return err;
  1975. if ((err = e100_alloc_cbs(nic)))
  1976. goto err_rx_clean_list;
  1977. if ((err = e100_hw_init(nic)))
  1978. goto err_clean_cbs;
  1979. e100_set_multicast_list(nic->netdev);
  1980. e100_start_receiver(nic, NULL);
  1981. mod_timer(&nic->watchdog, jiffies);
  1982. if ((err = request_irq(nic->pdev->irq, e100_intr, IRQF_SHARED,
  1983. nic->netdev->name, nic->netdev)))
  1984. goto err_no_irq;
  1985. netif_wake_queue(nic->netdev);
  1986. napi_enable(&nic->napi);
  1987. /* enable ints _after_ enabling poll, preventing a race between
  1988. * disable ints+schedule */
  1989. e100_enable_irq(nic);
  1990. return 0;
  1991. err_no_irq:
  1992. del_timer_sync(&nic->watchdog);
  1993. err_clean_cbs:
  1994. e100_clean_cbs(nic);
  1995. err_rx_clean_list:
  1996. e100_rx_clean_list(nic);
  1997. return err;
  1998. }
  1999. static void e100_down(struct nic *nic)
  2000. {
  2001. /* wait here for poll to complete */
  2002. napi_disable(&nic->napi);
  2003. netif_stop_queue(nic->netdev);
  2004. e100_hw_reset(nic);
  2005. free_irq(nic->pdev->irq, nic->netdev);
  2006. del_timer_sync(&nic->watchdog);
  2007. netif_carrier_off(nic->netdev);
  2008. e100_clean_cbs(nic);
  2009. e100_rx_clean_list(nic);
  2010. }
  2011. static void e100_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  2012. {
  2013. struct nic *nic = netdev_priv(netdev);
  2014. /* Reset outside of interrupt context, to avoid request_irq
  2015. * in interrupt context */
  2016. schedule_work(&nic->tx_timeout_task);
  2017. }
  2018. static void e100_tx_timeout_task(struct work_struct *work)
  2019. {
  2020. struct nic *nic = container_of(work, struct nic, tx_timeout_task);
  2021. struct net_device *netdev = nic->netdev;
  2022. netif_printk(nic, tx_err, KERN_DEBUG, nic->netdev,
  2023. "scb.status=0x%02X\n", ioread8(&nic->csr->scb.status));
  2024. rtnl_lock();
  2025. if (netif_running(netdev)) {
  2026. e100_down(netdev_priv(netdev));
  2027. e100_up(netdev_priv(netdev));
  2028. }
  2029. rtnl_unlock();
  2030. }
  2031. static int e100_loopback_test(struct nic *nic, enum loopback loopback_mode)
  2032. {
  2033. int err;
  2034. struct sk_buff *skb;
  2035. /* Use driver resources to perform internal MAC or PHY
  2036. * loopback test. A single packet is prepared and transmitted
  2037. * in loopback mode, and the test passes if the received
  2038. * packet compares byte-for-byte to the transmitted packet. */
  2039. if ((err = e100_rx_alloc_list(nic)))
  2040. return err;
  2041. if ((err = e100_alloc_cbs(nic)))
  2042. goto err_clean_rx;
  2043. /* ICH PHY loopback is broken so do MAC loopback instead */
  2044. if (nic->flags & ich && loopback_mode == lb_phy)
  2045. loopback_mode = lb_mac;
  2046. nic->loopback = loopback_mode;
  2047. if ((err = e100_hw_init(nic)))
  2048. goto err_loopback_none;
  2049. if (loopback_mode == lb_phy)
  2050. mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR,
  2051. BMCR_LOOPBACK);
  2052. e100_start_receiver(nic, NULL);
  2053. if (!(skb = netdev_alloc_skb(nic->netdev, ETH_DATA_LEN))) {
  2054. err = -ENOMEM;
  2055. goto err_loopback_none;
  2056. }
  2057. skb_put(skb, ETH_DATA_LEN);
  2058. memset(skb->data, 0xFF, ETH_DATA_LEN);
  2059. e100_xmit_frame(skb, nic->netdev);
  2060. msleep(10);
  2061. dma_sync_single_for_cpu(&nic->pdev->dev, nic->rx_to_clean->dma_addr,
  2062. RFD_BUF_LEN, DMA_BIDIRECTIONAL);
  2063. if (memcmp(nic->rx_to_clean->skb->data + sizeof(struct rfd),
  2064. skb->data, ETH_DATA_LEN))
  2065. err = -EAGAIN;
  2066. err_loopback_none:
  2067. mdio_write(nic->netdev, nic->mii.phy_id, MII_BMCR, 0);
  2068. nic->loopback = lb_none;
  2069. e100_clean_cbs(nic);
  2070. e100_hw_reset(nic);
  2071. err_clean_rx:
  2072. e100_rx_clean_list(nic);
  2073. return err;
  2074. }
  2075. #define MII_LED_CONTROL 0x1B
  2076. #define E100_82552_LED_OVERRIDE 0x19
  2077. #define E100_82552_LED_ON 0x000F /* LEDTX and LED_RX both on */
  2078. #define E100_82552_LED_OFF 0x000A /* LEDTX and LED_RX both off */
  2079. static int e100_get_link_ksettings(struct net_device *netdev,
  2080. struct ethtool_link_ksettings *cmd)
  2081. {
  2082. struct nic *nic = netdev_priv(netdev);
  2083. mii_ethtool_get_link_ksettings(&nic->mii, cmd);
  2084. return 0;
  2085. }
  2086. static int e100_set_link_ksettings(struct net_device *netdev,
  2087. const struct ethtool_link_ksettings *cmd)
  2088. {
  2089. struct nic *nic = netdev_priv(netdev);
  2090. int err;
  2091. mdio_write(netdev, nic->mii.phy_id, MII_BMCR, BMCR_RESET);
  2092. err = mii_ethtool_set_link_ksettings(&nic->mii, cmd);
  2093. e100_exec_cb(nic, NULL, e100_configure);
  2094. return err;
  2095. }
  2096. static void e100_get_drvinfo(struct net_device *netdev,
  2097. struct ethtool_drvinfo *info)
  2098. {
  2099. struct nic *nic = netdev_priv(netdev);
  2100. strscpy(info->driver, DRV_NAME, sizeof(info->driver));
  2101. strscpy(info->bus_info, pci_name(nic->pdev),
  2102. sizeof(info->bus_info));
  2103. }
  2104. #define E100_PHY_REGS 0x1D
  2105. static int e100_get_regs_len(struct net_device *netdev)
  2106. {
  2107. struct nic *nic = netdev_priv(netdev);
  2108. /* We know the number of registers, and the size of the dump buffer.
  2109. * Calculate the total size in bytes.
  2110. */
  2111. return (1 + E100_PHY_REGS) * sizeof(u32) + sizeof(nic->mem->dump_buf);
  2112. }
  2113. static void e100_get_regs(struct net_device *netdev,
  2114. struct ethtool_regs *regs, void *p)
  2115. {
  2116. struct nic *nic = netdev_priv(netdev);
  2117. u32 *buff = p;
  2118. int i;
  2119. regs->version = (1 << 24) | nic->pdev->revision;
  2120. buff[0] = ioread8(&nic->csr->scb.cmd_hi) << 24 |
  2121. ioread8(&nic->csr->scb.cmd_lo) << 16 |
  2122. ioread16(&nic->csr->scb.status);
  2123. for (i = 0; i < E100_PHY_REGS; i++)
  2124. /* Note that we read the registers in reverse order. This
  2125. * ordering is the ABI apparently used by ethtool and other
  2126. * applications.
  2127. */
  2128. buff[1 + i] = mdio_read(netdev, nic->mii.phy_id,
  2129. E100_PHY_REGS - 1 - i);
  2130. memset(nic->mem->dump_buf, 0, sizeof(nic->mem->dump_buf));
  2131. e100_exec_cb(nic, NULL, e100_dump);
  2132. msleep(10);
  2133. memcpy(&buff[1 + E100_PHY_REGS], nic->mem->dump_buf,
  2134. sizeof(nic->mem->dump_buf));
  2135. }
  2136. static void e100_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2137. {
  2138. struct nic *nic = netdev_priv(netdev);
  2139. wol->supported = (nic->mac >= mac_82558_D101_A4) ? WAKE_MAGIC : 0;
  2140. wol->wolopts = (nic->flags & wol_magic) ? WAKE_MAGIC : 0;
  2141. }
  2142. static int e100_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
  2143. {
  2144. struct nic *nic = netdev_priv(netdev);
  2145. if ((wol->wolopts && wol->wolopts != WAKE_MAGIC) ||
  2146. !device_can_wakeup(&nic->pdev->dev))
  2147. return -EOPNOTSUPP;
  2148. if (wol->wolopts)
  2149. nic->flags |= wol_magic;
  2150. else
  2151. nic->flags &= ~wol_magic;
  2152. device_set_wakeup_enable(&nic->pdev->dev, wol->wolopts);
  2153. e100_exec_cb(nic, NULL, e100_configure);
  2154. return 0;
  2155. }
  2156. static u32 e100_get_msglevel(struct net_device *netdev)
  2157. {
  2158. struct nic *nic = netdev_priv(netdev);
  2159. return nic->msg_enable;
  2160. }
  2161. static void e100_set_msglevel(struct net_device *netdev, u32 value)
  2162. {
  2163. struct nic *nic = netdev_priv(netdev);
  2164. nic->msg_enable = value;
  2165. }
  2166. static int e100_nway_reset(struct net_device *netdev)
  2167. {
  2168. struct nic *nic = netdev_priv(netdev);
  2169. return mii_nway_restart(&nic->mii);
  2170. }
  2171. static u32 e100_get_link(struct net_device *netdev)
  2172. {
  2173. struct nic *nic = netdev_priv(netdev);
  2174. return mii_link_ok(&nic->mii);
  2175. }
  2176. static int e100_get_eeprom_len(struct net_device *netdev)
  2177. {
  2178. struct nic *nic = netdev_priv(netdev);
  2179. return nic->eeprom_wc << 1;
  2180. }
  2181. #define E100_EEPROM_MAGIC 0x1234
  2182. static int e100_get_eeprom(struct net_device *netdev,
  2183. struct ethtool_eeprom *eeprom, u8 *bytes)
  2184. {
  2185. struct nic *nic = netdev_priv(netdev);
  2186. eeprom->magic = E100_EEPROM_MAGIC;
  2187. memcpy(bytes, &((u8 *)nic->eeprom)[eeprom->offset], eeprom->len);
  2188. return 0;
  2189. }
  2190. static int e100_set_eeprom(struct net_device *netdev,
  2191. struct ethtool_eeprom *eeprom, u8 *bytes)
  2192. {
  2193. struct nic *nic = netdev_priv(netdev);
  2194. if (eeprom->magic != E100_EEPROM_MAGIC)
  2195. return -EINVAL;
  2196. memcpy(&((u8 *)nic->eeprom)[eeprom->offset], bytes, eeprom->len);
  2197. return e100_eeprom_save(nic, eeprom->offset >> 1,
  2198. (eeprom->len >> 1) + 1);
  2199. }
  2200. static void e100_get_ringparam(struct net_device *netdev,
  2201. struct ethtool_ringparam *ring,
  2202. struct kernel_ethtool_ringparam *kernel_ring,
  2203. struct netlink_ext_ack *extack)
  2204. {
  2205. struct nic *nic = netdev_priv(netdev);
  2206. struct param_range *rfds = &nic->params.rfds;
  2207. struct param_range *cbs = &nic->params.cbs;
  2208. ring->rx_max_pending = rfds->max;
  2209. ring->tx_max_pending = cbs->max;
  2210. ring->rx_pending = rfds->count;
  2211. ring->tx_pending = cbs->count;
  2212. }
  2213. static int e100_set_ringparam(struct net_device *netdev,
  2214. struct ethtool_ringparam *ring,
  2215. struct kernel_ethtool_ringparam *kernel_ring,
  2216. struct netlink_ext_ack *extack)
  2217. {
  2218. struct nic *nic = netdev_priv(netdev);
  2219. struct param_range *rfds = &nic->params.rfds;
  2220. struct param_range *cbs = &nic->params.cbs;
  2221. if ((ring->rx_mini_pending) || (ring->rx_jumbo_pending))
  2222. return -EINVAL;
  2223. if (netif_running(netdev))
  2224. e100_down(nic);
  2225. rfds->count = max(ring->rx_pending, rfds->min);
  2226. rfds->count = min(rfds->count, rfds->max);
  2227. cbs->count = max(ring->tx_pending, cbs->min);
  2228. cbs->count = min(cbs->count, cbs->max);
  2229. netif_info(nic, drv, nic->netdev, "Ring Param settings: rx: %d, tx %d\n",
  2230. rfds->count, cbs->count);
  2231. if (netif_running(netdev))
  2232. e100_up(nic);
  2233. return 0;
  2234. }
  2235. static const char e100_gstrings_test[][ETH_GSTRING_LEN] = {
  2236. "Link test (on/offline)",
  2237. "Eeprom test (on/offline)",
  2238. "Self test (offline)",
  2239. "Mac loopback (offline)",
  2240. "Phy loopback (offline)",
  2241. };
  2242. #define E100_TEST_LEN ARRAY_SIZE(e100_gstrings_test)
  2243. static void e100_diag_test(struct net_device *netdev,
  2244. struct ethtool_test *test, u64 *data)
  2245. {
  2246. struct ethtool_cmd cmd;
  2247. struct nic *nic = netdev_priv(netdev);
  2248. int i;
  2249. memset(data, 0, E100_TEST_LEN * sizeof(u64));
  2250. data[0] = !mii_link_ok(&nic->mii);
  2251. data[1] = e100_eeprom_load(nic);
  2252. if (test->flags & ETH_TEST_FL_OFFLINE) {
  2253. /* save speed, duplex & autoneg settings */
  2254. mii_ethtool_gset(&nic->mii, &cmd);
  2255. if (netif_running(netdev))
  2256. e100_down(nic);
  2257. data[2] = e100_self_test(nic);
  2258. data[3] = e100_loopback_test(nic, lb_mac);
  2259. data[4] = e100_loopback_test(nic, lb_phy);
  2260. /* restore speed, duplex & autoneg settings */
  2261. mii_ethtool_sset(&nic->mii, &cmd);
  2262. if (netif_running(netdev))
  2263. e100_up(nic);
  2264. }
  2265. for (i = 0; i < E100_TEST_LEN; i++)
  2266. test->flags |= data[i] ? ETH_TEST_FL_FAILED : 0;
  2267. msleep_interruptible(4 * 1000);
  2268. }
  2269. static int e100_set_phys_id(struct net_device *netdev,
  2270. enum ethtool_phys_id_state state)
  2271. {
  2272. struct nic *nic = netdev_priv(netdev);
  2273. enum led_state {
  2274. led_on = 0x01,
  2275. led_off = 0x04,
  2276. led_on_559 = 0x05,
  2277. led_on_557 = 0x07,
  2278. };
  2279. u16 led_reg = (nic->phy == phy_82552_v) ? E100_82552_LED_OVERRIDE :
  2280. MII_LED_CONTROL;
  2281. u16 leds = 0;
  2282. switch (state) {
  2283. case ETHTOOL_ID_ACTIVE:
  2284. return 2;
  2285. case ETHTOOL_ID_ON:
  2286. leds = (nic->phy == phy_82552_v) ? E100_82552_LED_ON :
  2287. (nic->mac < mac_82559_D101M) ? led_on_557 : led_on_559;
  2288. break;
  2289. case ETHTOOL_ID_OFF:
  2290. leds = (nic->phy == phy_82552_v) ? E100_82552_LED_OFF : led_off;
  2291. break;
  2292. case ETHTOOL_ID_INACTIVE:
  2293. break;
  2294. }
  2295. mdio_write(netdev, nic->mii.phy_id, led_reg, leds);
  2296. return 0;
  2297. }
  2298. static const char e100_gstrings_stats[][ETH_GSTRING_LEN] = {
  2299. "rx_packets", "tx_packets", "rx_bytes", "tx_bytes", "rx_errors",
  2300. "tx_errors", "rx_dropped", "tx_dropped", "multicast", "collisions",
  2301. "rx_length_errors", "rx_over_errors", "rx_crc_errors",
  2302. "rx_frame_errors", "rx_fifo_errors", "rx_missed_errors",
  2303. "tx_aborted_errors", "tx_carrier_errors", "tx_fifo_errors",
  2304. "tx_heartbeat_errors", "tx_window_errors",
  2305. /* device-specific stats */
  2306. "tx_deferred", "tx_single_collisions", "tx_multi_collisions",
  2307. "tx_flow_control_pause", "rx_flow_control_pause",
  2308. "rx_flow_control_unsupported", "tx_tco_packets", "rx_tco_packets",
  2309. "rx_short_frame_errors", "rx_over_length_errors",
  2310. };
  2311. #define E100_NET_STATS_LEN 21
  2312. #define E100_STATS_LEN ARRAY_SIZE(e100_gstrings_stats)
  2313. static int e100_get_sset_count(struct net_device *netdev, int sset)
  2314. {
  2315. switch (sset) {
  2316. case ETH_SS_TEST:
  2317. return E100_TEST_LEN;
  2318. case ETH_SS_STATS:
  2319. return E100_STATS_LEN;
  2320. default:
  2321. return -EOPNOTSUPP;
  2322. }
  2323. }
  2324. static void e100_get_ethtool_stats(struct net_device *netdev,
  2325. struct ethtool_stats *stats, u64 *data)
  2326. {
  2327. struct nic *nic = netdev_priv(netdev);
  2328. int i;
  2329. for (i = 0; i < E100_NET_STATS_LEN; i++)
  2330. data[i] = ((unsigned long *)&netdev->stats)[i];
  2331. data[i++] = nic->tx_deferred;
  2332. data[i++] = nic->tx_single_collisions;
  2333. data[i++] = nic->tx_multiple_collisions;
  2334. data[i++] = nic->tx_fc_pause;
  2335. data[i++] = nic->rx_fc_pause;
  2336. data[i++] = nic->rx_fc_unsupported;
  2337. data[i++] = nic->tx_tco_frames;
  2338. data[i++] = nic->rx_tco_frames;
  2339. data[i++] = nic->rx_short_frame_errors;
  2340. data[i++] = nic->rx_over_length_errors;
  2341. }
  2342. static void e100_get_strings(struct net_device *netdev, u32 stringset, u8 *data)
  2343. {
  2344. switch (stringset) {
  2345. case ETH_SS_TEST:
  2346. memcpy(data, e100_gstrings_test, sizeof(e100_gstrings_test));
  2347. break;
  2348. case ETH_SS_STATS:
  2349. memcpy(data, e100_gstrings_stats, sizeof(e100_gstrings_stats));
  2350. break;
  2351. }
  2352. }
  2353. static const struct ethtool_ops e100_ethtool_ops = {
  2354. .get_drvinfo = e100_get_drvinfo,
  2355. .get_regs_len = e100_get_regs_len,
  2356. .get_regs = e100_get_regs,
  2357. .get_wol = e100_get_wol,
  2358. .set_wol = e100_set_wol,
  2359. .get_msglevel = e100_get_msglevel,
  2360. .set_msglevel = e100_set_msglevel,
  2361. .nway_reset = e100_nway_reset,
  2362. .get_link = e100_get_link,
  2363. .get_eeprom_len = e100_get_eeprom_len,
  2364. .get_eeprom = e100_get_eeprom,
  2365. .set_eeprom = e100_set_eeprom,
  2366. .get_ringparam = e100_get_ringparam,
  2367. .set_ringparam = e100_set_ringparam,
  2368. .self_test = e100_diag_test,
  2369. .get_strings = e100_get_strings,
  2370. .set_phys_id = e100_set_phys_id,
  2371. .get_ethtool_stats = e100_get_ethtool_stats,
  2372. .get_sset_count = e100_get_sset_count,
  2373. .get_ts_info = ethtool_op_get_ts_info,
  2374. .get_link_ksettings = e100_get_link_ksettings,
  2375. .set_link_ksettings = e100_set_link_ksettings,
  2376. };
  2377. static int e100_do_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
  2378. {
  2379. struct nic *nic = netdev_priv(netdev);
  2380. return generic_mii_ioctl(&nic->mii, if_mii(ifr), cmd, NULL);
  2381. }
  2382. static int e100_alloc(struct nic *nic)
  2383. {
  2384. nic->mem = dma_alloc_coherent(&nic->pdev->dev, sizeof(struct mem),
  2385. &nic->dma_addr, GFP_KERNEL);
  2386. return nic->mem ? 0 : -ENOMEM;
  2387. }
  2388. static void e100_free(struct nic *nic)
  2389. {
  2390. if (nic->mem) {
  2391. dma_free_coherent(&nic->pdev->dev, sizeof(struct mem),
  2392. nic->mem, nic->dma_addr);
  2393. nic->mem = NULL;
  2394. }
  2395. }
  2396. static int e100_open(struct net_device *netdev)
  2397. {
  2398. struct nic *nic = netdev_priv(netdev);
  2399. int err = 0;
  2400. netif_carrier_off(netdev);
  2401. if ((err = e100_up(nic)))
  2402. netif_err(nic, ifup, nic->netdev, "Cannot open interface, aborting\n");
  2403. return err;
  2404. }
  2405. static int e100_close(struct net_device *netdev)
  2406. {
  2407. e100_down(netdev_priv(netdev));
  2408. return 0;
  2409. }
  2410. static int e100_set_features(struct net_device *netdev,
  2411. netdev_features_t features)
  2412. {
  2413. struct nic *nic = netdev_priv(netdev);
  2414. netdev_features_t changed = features ^ netdev->features;
  2415. if (!(changed & (NETIF_F_RXFCS | NETIF_F_RXALL)))
  2416. return 0;
  2417. netdev->features = features;
  2418. e100_exec_cb(nic, NULL, e100_configure);
  2419. return 1;
  2420. }
  2421. static const struct net_device_ops e100_netdev_ops = {
  2422. .ndo_open = e100_open,
  2423. .ndo_stop = e100_close,
  2424. .ndo_start_xmit = e100_xmit_frame,
  2425. .ndo_validate_addr = eth_validate_addr,
  2426. .ndo_set_rx_mode = e100_set_multicast_list,
  2427. .ndo_set_mac_address = e100_set_mac_address,
  2428. .ndo_eth_ioctl = e100_do_ioctl,
  2429. .ndo_tx_timeout = e100_tx_timeout,
  2430. #ifdef CONFIG_NET_POLL_CONTROLLER
  2431. .ndo_poll_controller = e100_netpoll,
  2432. #endif
  2433. .ndo_set_features = e100_set_features,
  2434. };
  2435. static int e100_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2436. {
  2437. struct net_device *netdev;
  2438. struct nic *nic;
  2439. int err;
  2440. if (!(netdev = alloc_etherdev(sizeof(struct nic))))
  2441. return -ENOMEM;
  2442. netdev->hw_features |= NETIF_F_RXFCS;
  2443. netdev->priv_flags |= IFF_SUPP_NOFCS;
  2444. netdev->hw_features |= NETIF_F_RXALL;
  2445. netdev->netdev_ops = &e100_netdev_ops;
  2446. netdev->ethtool_ops = &e100_ethtool_ops;
  2447. netdev->watchdog_timeo = E100_WATCHDOG_PERIOD;
  2448. strscpy(netdev->name, pci_name(pdev), sizeof(netdev->name));
  2449. nic = netdev_priv(netdev);
  2450. netif_napi_add_weight(netdev, &nic->napi, e100_poll, E100_NAPI_WEIGHT);
  2451. nic->netdev = netdev;
  2452. nic->pdev = pdev;
  2453. nic->msg_enable = (1 << debug) - 1;
  2454. nic->mdio_ctrl = mdio_ctrl_hw;
  2455. pci_set_drvdata(pdev, netdev);
  2456. if ((err = pci_enable_device(pdev))) {
  2457. netif_err(nic, probe, nic->netdev, "Cannot enable PCI device, aborting\n");
  2458. goto err_out_free_dev;
  2459. }
  2460. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  2461. netif_err(nic, probe, nic->netdev, "Cannot find proper PCI device base address, aborting\n");
  2462. err = -ENODEV;
  2463. goto err_out_disable_pdev;
  2464. }
  2465. if ((err = pci_request_regions(pdev, DRV_NAME))) {
  2466. netif_err(nic, probe, nic->netdev, "Cannot obtain PCI resources, aborting\n");
  2467. goto err_out_disable_pdev;
  2468. }
  2469. if ((err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)))) {
  2470. netif_err(nic, probe, nic->netdev, "No usable DMA configuration, aborting\n");
  2471. goto err_out_free_res;
  2472. }
  2473. SET_NETDEV_DEV(netdev, &pdev->dev);
  2474. if (use_io)
  2475. netif_info(nic, probe, nic->netdev, "using i/o access mode\n");
  2476. nic->csr = pci_iomap(pdev, (use_io ? 1 : 0), sizeof(struct csr));
  2477. if (!nic->csr) {
  2478. netif_err(nic, probe, nic->netdev, "Cannot map device registers, aborting\n");
  2479. err = -ENOMEM;
  2480. goto err_out_free_res;
  2481. }
  2482. if (ent->driver_data)
  2483. nic->flags |= ich;
  2484. else
  2485. nic->flags &= ~ich;
  2486. e100_get_defaults(nic);
  2487. /* D100 MAC doesn't allow rx of vlan packets with normal MTU */
  2488. if (nic->mac < mac_82558_D101_A4)
  2489. netdev->features |= NETIF_F_VLAN_CHALLENGED;
  2490. /* locks must be initialized before calling hw_reset */
  2491. spin_lock_init(&nic->cb_lock);
  2492. spin_lock_init(&nic->cmd_lock);
  2493. spin_lock_init(&nic->mdio_lock);
  2494. /* Reset the device before pci_set_master() in case device is in some
  2495. * funky state and has an interrupt pending - hint: we don't have the
  2496. * interrupt handler registered yet. */
  2497. e100_hw_reset(nic);
  2498. pci_set_master(pdev);
  2499. timer_setup(&nic->watchdog, e100_watchdog, 0);
  2500. INIT_WORK(&nic->tx_timeout_task, e100_tx_timeout_task);
  2501. if ((err = e100_alloc(nic))) {
  2502. netif_err(nic, probe, nic->netdev, "Cannot alloc driver memory, aborting\n");
  2503. goto err_out_iounmap;
  2504. }
  2505. if ((err = e100_eeprom_load(nic)))
  2506. goto err_out_free;
  2507. e100_phy_init(nic);
  2508. eth_hw_addr_set(netdev, (u8 *)nic->eeprom);
  2509. if (!is_valid_ether_addr(netdev->dev_addr)) {
  2510. if (!eeprom_bad_csum_allow) {
  2511. netif_err(nic, probe, nic->netdev, "Invalid MAC address from EEPROM, aborting\n");
  2512. err = -EAGAIN;
  2513. goto err_out_free;
  2514. } else {
  2515. netif_err(nic, probe, nic->netdev, "Invalid MAC address from EEPROM, you MUST configure one.\n");
  2516. }
  2517. }
  2518. /* Wol magic packet can be enabled from eeprom */
  2519. if ((nic->mac >= mac_82558_D101_A4) &&
  2520. (le16_to_cpu(nic->eeprom[eeprom_id]) & eeprom_id_wol)) {
  2521. nic->flags |= wol_magic;
  2522. device_set_wakeup_enable(&pdev->dev, true);
  2523. }
  2524. /* ack any pending wake events, disable PME */
  2525. pci_pme_active(pdev, false);
  2526. strcpy(netdev->name, "eth%d");
  2527. if ((err = register_netdev(netdev))) {
  2528. netif_err(nic, probe, nic->netdev, "Cannot register net device, aborting\n");
  2529. goto err_out_free;
  2530. }
  2531. nic->cbs_pool = dma_pool_create(netdev->name,
  2532. &nic->pdev->dev,
  2533. nic->params.cbs.max * sizeof(struct cb),
  2534. sizeof(u32),
  2535. 0);
  2536. if (!nic->cbs_pool) {
  2537. netif_err(nic, probe, nic->netdev, "Cannot create DMA pool, aborting\n");
  2538. err = -ENOMEM;
  2539. goto err_out_pool;
  2540. }
  2541. netif_info(nic, probe, nic->netdev,
  2542. "addr 0x%llx, irq %d, MAC addr %pM\n",
  2543. (unsigned long long)pci_resource_start(pdev, use_io ? 1 : 0),
  2544. pdev->irq, netdev->dev_addr);
  2545. return 0;
  2546. err_out_pool:
  2547. unregister_netdev(netdev);
  2548. err_out_free:
  2549. e100_free(nic);
  2550. err_out_iounmap:
  2551. pci_iounmap(pdev, nic->csr);
  2552. err_out_free_res:
  2553. pci_release_regions(pdev);
  2554. err_out_disable_pdev:
  2555. pci_disable_device(pdev);
  2556. err_out_free_dev:
  2557. free_netdev(netdev);
  2558. return err;
  2559. }
  2560. static void e100_remove(struct pci_dev *pdev)
  2561. {
  2562. struct net_device *netdev = pci_get_drvdata(pdev);
  2563. if (netdev) {
  2564. struct nic *nic = netdev_priv(netdev);
  2565. unregister_netdev(netdev);
  2566. e100_free(nic);
  2567. pci_iounmap(pdev, nic->csr);
  2568. dma_pool_destroy(nic->cbs_pool);
  2569. free_netdev(netdev);
  2570. pci_release_regions(pdev);
  2571. pci_disable_device(pdev);
  2572. }
  2573. }
  2574. #define E100_82552_SMARTSPEED 0x14 /* SmartSpeed Ctrl register */
  2575. #define E100_82552_REV_ANEG 0x0200 /* Reverse auto-negotiation */
  2576. #define E100_82552_ANEG_NOW 0x0400 /* Auto-negotiate now */
  2577. static void __e100_shutdown(struct pci_dev *pdev, bool *enable_wake)
  2578. {
  2579. struct net_device *netdev = pci_get_drvdata(pdev);
  2580. struct nic *nic = netdev_priv(netdev);
  2581. netif_device_detach(netdev);
  2582. if (netif_running(netdev))
  2583. e100_down(nic);
  2584. if ((nic->flags & wol_magic) | e100_asf(nic)) {
  2585. /* enable reverse auto-negotiation */
  2586. if (nic->phy == phy_82552_v) {
  2587. u16 smartspeed = mdio_read(netdev, nic->mii.phy_id,
  2588. E100_82552_SMARTSPEED);
  2589. mdio_write(netdev, nic->mii.phy_id,
  2590. E100_82552_SMARTSPEED, smartspeed |
  2591. E100_82552_REV_ANEG | E100_82552_ANEG_NOW);
  2592. }
  2593. *enable_wake = true;
  2594. } else {
  2595. *enable_wake = false;
  2596. }
  2597. pci_disable_device(pdev);
  2598. }
  2599. static int __e100_power_off(struct pci_dev *pdev, bool wake)
  2600. {
  2601. if (wake)
  2602. return pci_prepare_to_sleep(pdev);
  2603. pci_wake_from_d3(pdev, false);
  2604. pci_set_power_state(pdev, PCI_D3hot);
  2605. return 0;
  2606. }
  2607. static int e100_suspend(struct device *dev_d)
  2608. {
  2609. bool wake;
  2610. __e100_shutdown(to_pci_dev(dev_d), &wake);
  2611. return 0;
  2612. }
  2613. static int e100_resume(struct device *dev_d)
  2614. {
  2615. struct net_device *netdev = dev_get_drvdata(dev_d);
  2616. struct nic *nic = netdev_priv(netdev);
  2617. int err;
  2618. err = pci_enable_device(to_pci_dev(dev_d));
  2619. if (err) {
  2620. netdev_err(netdev, "Resume cannot enable PCI device, aborting\n");
  2621. return err;
  2622. }
  2623. pci_set_master(to_pci_dev(dev_d));
  2624. /* disable reverse auto-negotiation */
  2625. if (nic->phy == phy_82552_v) {
  2626. u16 smartspeed = mdio_read(netdev, nic->mii.phy_id,
  2627. E100_82552_SMARTSPEED);
  2628. mdio_write(netdev, nic->mii.phy_id,
  2629. E100_82552_SMARTSPEED,
  2630. smartspeed & ~(E100_82552_REV_ANEG));
  2631. }
  2632. if (netif_running(netdev))
  2633. e100_up(nic);
  2634. netif_device_attach(netdev);
  2635. return 0;
  2636. }
  2637. static void e100_shutdown(struct pci_dev *pdev)
  2638. {
  2639. bool wake;
  2640. __e100_shutdown(pdev, &wake);
  2641. if (system_state == SYSTEM_POWER_OFF)
  2642. __e100_power_off(pdev, wake);
  2643. }
  2644. /* ------------------ PCI Error Recovery infrastructure -------------- */
  2645. /**
  2646. * e100_io_error_detected - called when PCI error is detected.
  2647. * @pdev: Pointer to PCI device
  2648. * @state: The current pci connection state
  2649. */
  2650. static pci_ers_result_t e100_io_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
  2651. {
  2652. struct net_device *netdev = pci_get_drvdata(pdev);
  2653. struct nic *nic = netdev_priv(netdev);
  2654. netif_device_detach(netdev);
  2655. if (state == pci_channel_io_perm_failure)
  2656. return PCI_ERS_RESULT_DISCONNECT;
  2657. if (netif_running(netdev))
  2658. e100_down(nic);
  2659. pci_disable_device(pdev);
  2660. /* Request a slot reset. */
  2661. return PCI_ERS_RESULT_NEED_RESET;
  2662. }
  2663. /**
  2664. * e100_io_slot_reset - called after the pci bus has been reset.
  2665. * @pdev: Pointer to PCI device
  2666. *
  2667. * Restart the card from scratch.
  2668. */
  2669. static pci_ers_result_t e100_io_slot_reset(struct pci_dev *pdev)
  2670. {
  2671. struct net_device *netdev = pci_get_drvdata(pdev);
  2672. struct nic *nic = netdev_priv(netdev);
  2673. if (pci_enable_device(pdev)) {
  2674. pr_err("Cannot re-enable PCI device after reset\n");
  2675. return PCI_ERS_RESULT_DISCONNECT;
  2676. }
  2677. pci_set_master(pdev);
  2678. /* Only one device per card can do a reset */
  2679. if (0 != PCI_FUNC(pdev->devfn))
  2680. return PCI_ERS_RESULT_RECOVERED;
  2681. e100_hw_reset(nic);
  2682. e100_phy_init(nic);
  2683. return PCI_ERS_RESULT_RECOVERED;
  2684. }
  2685. /**
  2686. * e100_io_resume - resume normal operations
  2687. * @pdev: Pointer to PCI device
  2688. *
  2689. * Resume normal operations after an error recovery
  2690. * sequence has been completed.
  2691. */
  2692. static void e100_io_resume(struct pci_dev *pdev)
  2693. {
  2694. struct net_device *netdev = pci_get_drvdata(pdev);
  2695. struct nic *nic = netdev_priv(netdev);
  2696. /* ack any pending wake events, disable PME */
  2697. pci_enable_wake(pdev, PCI_D0, 0);
  2698. netif_device_attach(netdev);
  2699. if (netif_running(netdev)) {
  2700. e100_open(netdev);
  2701. mod_timer(&nic->watchdog, jiffies);
  2702. }
  2703. }
  2704. static const struct pci_error_handlers e100_err_handler = {
  2705. .error_detected = e100_io_error_detected,
  2706. .slot_reset = e100_io_slot_reset,
  2707. .resume = e100_io_resume,
  2708. };
  2709. static DEFINE_SIMPLE_DEV_PM_OPS(e100_pm_ops, e100_suspend, e100_resume);
  2710. static struct pci_driver e100_driver = {
  2711. .name = DRV_NAME,
  2712. .id_table = e100_id_table,
  2713. .probe = e100_probe,
  2714. .remove = e100_remove,
  2715. /* Power Management hooks */
  2716. .driver.pm = pm_sleep_ptr(&e100_pm_ops),
  2717. .shutdown = e100_shutdown,
  2718. .err_handler = &e100_err_handler,
  2719. };
  2720. static int __init e100_init_module(void)
  2721. {
  2722. if (((1 << debug) - 1) & NETIF_MSG_DRV) {
  2723. pr_info("%s\n", DRV_DESCRIPTION);
  2724. pr_info("%s\n", DRV_COPYRIGHT);
  2725. }
  2726. return pci_register_driver(&e100_driver);
  2727. }
  2728. static void __exit e100_cleanup_module(void)
  2729. {
  2730. pci_unregister_driver(&e100_driver);
  2731. }
  2732. module_init(e100_init_module);
  2733. module_exit(e100_cleanup_module);