e100.c 91 KB

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