et131x.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086
  1. /* Agere Systems Inc.
  2. * 10/100/1000 Base-T Ethernet Driver for the ET1301 and ET131x series MACs
  3. *
  4. * Copyright © 2005 Agere Systems Inc.
  5. * All rights reserved.
  6. * http://www.agere.com
  7. *
  8. * Copyright (c) 2011 Mark Einon <mark.einon@gmail.com>
  9. *
  10. *------------------------------------------------------------------------------
  11. *
  12. * SOFTWARE LICENSE
  13. *
  14. * This software is provided subject to the following terms and conditions,
  15. * which you should read carefully before using the software. Using this
  16. * software indicates your acceptance of these terms and conditions. If you do
  17. * not agree with these terms and conditions, do not use the software.
  18. *
  19. * Copyright © 2005 Agere Systems Inc.
  20. * All rights reserved.
  21. *
  22. * Redistribution and use in source or binary forms, with or without
  23. * modifications, are permitted provided that the following conditions are met:
  24. *
  25. * . Redistributions of source code must retain the above copyright notice, this
  26. * list of conditions and the following Disclaimer as comments in the code as
  27. * well as in the documentation and/or other materials provided with the
  28. * distribution.
  29. *
  30. * . Redistributions in binary form must reproduce the above copyright notice,
  31. * this list of conditions and the following Disclaimer in the documentation
  32. * and/or other materials provided with the distribution.
  33. *
  34. * . Neither the name of Agere Systems Inc. nor the names of the contributors
  35. * may be used to endorse or promote products derived from this software
  36. * without specific prior written permission.
  37. *
  38. * Disclaimer
  39. *
  40. * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  41. * INCLUDING, BUT NOT LIMITED TO, INFRINGEMENT AND THE IMPLIED WARRANTIES OF
  42. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. ANY
  43. * USE, MODIFICATION OR DISTRIBUTION OF THIS SOFTWARE IS SOLELY AT THE USERS OWN
  44. * RISK. IN NO EVENT SHALL AGERE SYSTEMS INC. OR CONTRIBUTORS BE LIABLE FOR ANY
  45. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  46. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  48. * ON ANY THEORY OF LIABILITY, INCLUDING, BUT NOT LIMITED TO, CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  50. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  51. * DAMAGE.
  52. */
  53. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  54. #include <linux/pci.h>
  55. #include <linux/module.h>
  56. #include <linux/types.h>
  57. #include <linux/kernel.h>
  58. #include <linux/sched.h>
  59. #include <linux/ptrace.h>
  60. #include <linux/slab.h>
  61. #include <linux/ctype.h>
  62. #include <linux/string.h>
  63. #include <linux/timer.h>
  64. #include <linux/interrupt.h>
  65. #include <linux/in.h>
  66. #include <linux/delay.h>
  67. #include <linux/bitops.h>
  68. #include <linux/io.h>
  69. #include <linux/netdevice.h>
  70. #include <linux/etherdevice.h>
  71. #include <linux/skbuff.h>
  72. #include <linux/if_arp.h>
  73. #include <linux/ioport.h>
  74. #include <linux/crc32.h>
  75. #include <linux/random.h>
  76. #include <linux/phy.h>
  77. #include "et131x.h"
  78. MODULE_AUTHOR("Victor Soriano <vjsoriano@agere.com>");
  79. MODULE_AUTHOR("Mark Einon <mark.einon@gmail.com>");
  80. MODULE_LICENSE("Dual BSD/GPL");
  81. MODULE_DESCRIPTION("10/100/1000 Base-T Ethernet Driver for the ET1310 by Agere Systems");
  82. /* EEPROM defines */
  83. #define MAX_NUM_REGISTER_POLLS 1000
  84. #define MAX_NUM_WRITE_RETRIES 2
  85. /* MAC defines */
  86. #define COUNTER_WRAP_16_BIT 0x10000
  87. #define COUNTER_WRAP_12_BIT 0x1000
  88. /* PCI defines */
  89. #define INTERNAL_MEM_SIZE 0x400 /* 1024 of internal memory */
  90. #define INTERNAL_MEM_RX_OFFSET 0x1FF /* 50% Tx, 50% Rx */
  91. /* ISR defines */
  92. /* For interrupts, normal running is:
  93. * rxdma_xfr_done, phy_interrupt, mac_stat_interrupt,
  94. * watchdog_interrupt & txdma_xfer_done
  95. *
  96. * In both cases, when flow control is enabled for either Tx or bi-direction,
  97. * we additional enable rx_fbr0_low and rx_fbr1_low, so we know when the
  98. * buffer rings are running low.
  99. */
  100. #define INT_MASK_DISABLE 0xffffffff
  101. /* NOTE: Masking out MAC_STAT Interrupt for now...
  102. * #define INT_MASK_ENABLE 0xfff6bf17
  103. * #define INT_MASK_ENABLE_NO_FLOW 0xfff6bfd7
  104. */
  105. #define INT_MASK_ENABLE 0xfffebf17
  106. #define INT_MASK_ENABLE_NO_FLOW 0xfffebfd7
  107. /* General defines */
  108. /* Packet and header sizes */
  109. #define NIC_MIN_PACKET_SIZE 60
  110. /* Multicast list size */
  111. #define NIC_MAX_MCAST_LIST 128
  112. /* Supported Filters */
  113. #define ET131X_PACKET_TYPE_DIRECTED 0x0001
  114. #define ET131X_PACKET_TYPE_MULTICAST 0x0002
  115. #define ET131X_PACKET_TYPE_BROADCAST 0x0004
  116. #define ET131X_PACKET_TYPE_PROMISCUOUS 0x0008
  117. #define ET131X_PACKET_TYPE_ALL_MULTICAST 0x0010
  118. /* Tx Timeout */
  119. #define ET131X_TX_TIMEOUT (1 * HZ)
  120. #define NIC_SEND_HANG_THRESHOLD 0
  121. /* MP_ADAPTER flags */
  122. #define FMP_ADAPTER_INTERRUPT_IN_USE 0x00000008
  123. /* MP_SHARED flags */
  124. #define FMP_ADAPTER_LOWER_POWER 0x00200000
  125. #define FMP_ADAPTER_NON_RECOVER_ERROR 0x00800000
  126. #define FMP_ADAPTER_HARDWARE_ERROR 0x04000000
  127. #define FMP_ADAPTER_FAIL_SEND_MASK 0x3ff00000
  128. /* Some offsets in PCI config space that are actually used. */
  129. #define ET1310_PCI_MAC_ADDRESS 0xA4
  130. #define ET1310_PCI_EEPROM_STATUS 0xB2
  131. #define ET1310_PCI_ACK_NACK 0xC0
  132. #define ET1310_PCI_REPLAY 0xC2
  133. #define ET1310_PCI_L0L1LATENCY 0xCF
  134. /* PCI Product IDs */
  135. #define ET131X_PCI_DEVICE_ID_GIG 0xED00 /* ET1310 1000 Base-T 8 */
  136. #define ET131X_PCI_DEVICE_ID_FAST 0xED01 /* ET1310 100 Base-T */
  137. /* Define order of magnitude converter */
  138. #define NANO_IN_A_MICRO 1000
  139. #define PARM_RX_NUM_BUFS_DEF 4
  140. #define PARM_RX_TIME_INT_DEF 10
  141. #define PARM_RX_MEM_END_DEF 0x2bc
  142. #define PARM_TX_TIME_INT_DEF 40
  143. #define PARM_TX_NUM_BUFS_DEF 4
  144. #define PARM_DMA_CACHE_DEF 0
  145. /* RX defines */
  146. #define FBR_CHUNKS 32
  147. #define MAX_DESC_PER_RING_RX 1024
  148. /* number of RFDs - default and min */
  149. #define RFD_LOW_WATER_MARK 40
  150. #define NIC_DEFAULT_NUM_RFD 1024
  151. #define NUM_FBRS 2
  152. #define MAX_PACKETS_HANDLED 256
  153. #define ET131X_MIN_MTU 64
  154. #define ET131X_MAX_MTU 9216
  155. #define ALCATEL_MULTICAST_PKT 0x01000000
  156. #define ALCATEL_BROADCAST_PKT 0x02000000
  157. /* typedefs for Free Buffer Descriptors */
  158. struct fbr_desc {
  159. u32 addr_lo;
  160. u32 addr_hi;
  161. u32 word2; /* Bits 10-31 reserved, 0-9 descriptor */
  162. };
  163. /* Packet Status Ring Descriptors
  164. *
  165. * Word 0:
  166. *
  167. * top 16 bits are from the Alcatel Status Word as enumerated in
  168. * PE-MCXMAC Data Sheet IPD DS54 0210-1 (also IPD-DS80 0205-2)
  169. *
  170. * 0: hp hash pass
  171. * 1: ipa IP checksum assist
  172. * 2: ipp IP checksum pass
  173. * 3: tcpa TCP checksum assist
  174. * 4: tcpp TCP checksum pass
  175. * 5: wol WOL Event
  176. * 6: rxmac_error RXMAC Error Indicator
  177. * 7: drop Drop packet
  178. * 8: ft Frame Truncated
  179. * 9: jp Jumbo Packet
  180. * 10: vp VLAN Packet
  181. * 11-15: unused
  182. * 16: asw_prev_pkt_dropped e.g. IFG too small on previous
  183. * 17: asw_RX_DV_event short receive event detected
  184. * 18: asw_false_carrier_event bad carrier since last good packet
  185. * 19: asw_code_err one or more nibbles signalled as errors
  186. * 20: asw_CRC_err CRC error
  187. * 21: asw_len_chk_err frame length field incorrect
  188. * 22: asw_too_long frame length > 1518 bytes
  189. * 23: asw_OK valid CRC + no code error
  190. * 24: asw_multicast has a multicast address
  191. * 25: asw_broadcast has a broadcast address
  192. * 26: asw_dribble_nibble spurious bits after EOP
  193. * 27: asw_control_frame is a control frame
  194. * 28: asw_pause_frame is a pause frame
  195. * 29: asw_unsupported_op unsupported OP code
  196. * 30: asw_VLAN_tag VLAN tag detected
  197. * 31: asw_long_evt Rx long event
  198. *
  199. * Word 1:
  200. * 0-15: length length in bytes
  201. * 16-25: bi Buffer Index
  202. * 26-27: ri Ring Index
  203. * 28-31: reserved
  204. */
  205. struct pkt_stat_desc {
  206. u32 word0;
  207. u32 word1;
  208. };
  209. /* Typedefs for the RX DMA status word */
  210. /* rx status word 0 holds part of the status bits of the Rx DMA engine
  211. * that get copied out to memory by the ET-1310. Word 0 is a 32 bit word
  212. * which contains the Free Buffer ring 0 and 1 available offset.
  213. *
  214. * bit 0-9 FBR1 offset
  215. * bit 10 Wrap flag for FBR1
  216. * bit 16-25 FBR0 offset
  217. * bit 26 Wrap flag for FBR0
  218. */
  219. /* RXSTAT_WORD1_t structure holds part of the status bits of the Rx DMA engine
  220. * that get copied out to memory by the ET-1310. Word 3 is a 32 bit word
  221. * which contains the Packet Status Ring available offset.
  222. *
  223. * bit 0-15 reserved
  224. * bit 16-27 PSRoffset
  225. * bit 28 PSRwrap
  226. * bit 29-31 unused
  227. */
  228. /* struct rx_status_block is a structure representing the status of the Rx
  229. * DMA engine it sits in free memory, and is pointed to by 0x101c / 0x1020
  230. */
  231. struct rx_status_block {
  232. u32 word0;
  233. u32 word1;
  234. };
  235. /* Structure for look-up table holding free buffer ring pointers, addresses
  236. * and state.
  237. */
  238. struct fbr_lookup {
  239. void *virt[MAX_DESC_PER_RING_RX];
  240. u32 bus_high[MAX_DESC_PER_RING_RX];
  241. u32 bus_low[MAX_DESC_PER_RING_RX];
  242. void *ring_virtaddr;
  243. dma_addr_t ring_physaddr;
  244. void *mem_virtaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
  245. dma_addr_t mem_physaddrs[MAX_DESC_PER_RING_RX / FBR_CHUNKS];
  246. u32 local_full;
  247. u32 num_entries;
  248. dma_addr_t buffsize;
  249. };
  250. /* struct rx_ring is the structure representing the adaptor's local
  251. * reference(s) to the rings
  252. */
  253. struct rx_ring {
  254. struct fbr_lookup *fbr[NUM_FBRS];
  255. void *ps_ring_virtaddr;
  256. dma_addr_t ps_ring_physaddr;
  257. u32 local_psr_full;
  258. u32 psr_entries;
  259. struct rx_status_block *rx_status_block;
  260. dma_addr_t rx_status_bus;
  261. struct list_head recv_list;
  262. u32 num_ready_recv;
  263. u32 num_rfd;
  264. bool unfinished_receives;
  265. };
  266. /* TX defines */
  267. /* word 2 of the control bits in the Tx Descriptor ring for the ET-1310
  268. *
  269. * 0-15: length of packet
  270. * 16-27: VLAN tag
  271. * 28: VLAN CFI
  272. * 29-31: VLAN priority
  273. *
  274. * word 3 of the control bits in the Tx Descriptor ring for the ET-1310
  275. *
  276. * 0: last packet in the sequence
  277. * 1: first packet in the sequence
  278. * 2: interrupt the processor when this pkt sent
  279. * 3: Control word - no packet data
  280. * 4: Issue half-duplex backpressure : XON/XOFF
  281. * 5: send pause frame
  282. * 6: Tx frame has error
  283. * 7: append CRC
  284. * 8: MAC override
  285. * 9: pad packet
  286. * 10: Packet is a Huge packet
  287. * 11: append VLAN tag
  288. * 12: IP checksum assist
  289. * 13: TCP checksum assist
  290. * 14: UDP checksum assist
  291. */
  292. #define TXDESC_FLAG_LASTPKT 0x0001
  293. #define TXDESC_FLAG_FIRSTPKT 0x0002
  294. #define TXDESC_FLAG_INTPROC 0x0004
  295. /* struct tx_desc represents each descriptor on the ring */
  296. struct tx_desc {
  297. u32 addr_hi;
  298. u32 addr_lo;
  299. u32 len_vlan; /* control words how to xmit the */
  300. u32 flags; /* data (detailed above) */
  301. };
  302. /* The status of the Tx DMA engine it sits in free memory, and is pointed to
  303. * by 0x101c / 0x1020. This is a DMA10 type
  304. */
  305. /* TCB (Transmit Control Block: Host Side) */
  306. struct tcb {
  307. struct tcb *next; /* Next entry in ring */
  308. u32 count; /* Used to spot stuck/lost packets */
  309. u32 stale; /* Used to spot stuck/lost packets */
  310. struct sk_buff *skb; /* Network skb we are tied to */
  311. u32 index; /* Ring indexes */
  312. u32 index_start;
  313. };
  314. /* Structure representing our local reference(s) to the ring */
  315. struct tx_ring {
  316. /* TCB (Transmit Control Block) memory and lists */
  317. struct tcb *tcb_ring;
  318. /* List of TCBs that are ready to be used */
  319. struct tcb *tcb_qhead;
  320. struct tcb *tcb_qtail;
  321. /* list of TCBs that are currently being sent. */
  322. struct tcb *send_head;
  323. struct tcb *send_tail;
  324. int used;
  325. /* The actual descriptor ring */
  326. struct tx_desc *tx_desc_ring;
  327. dma_addr_t tx_desc_ring_pa;
  328. /* send_idx indicates where we last wrote to in the descriptor ring. */
  329. u32 send_idx;
  330. /* The location of the write-back status block */
  331. u32 *tx_status;
  332. dma_addr_t tx_status_pa;
  333. /* Packets since the last IRQ: used for interrupt coalescing */
  334. int since_irq;
  335. };
  336. /* Do not change these values: if changed, then change also in respective
  337. * TXdma and Rxdma engines
  338. */
  339. #define NUM_DESC_PER_RING_TX 512 /* TX Do not change these values */
  340. #define NUM_TCB 64
  341. /* These values are all superseded by registry entries to facilitate tuning.
  342. * Once the desired performance has been achieved, the optimal registry values
  343. * should be re-populated to these #defines:
  344. */
  345. #define TX_ERROR_PERIOD 1000
  346. #define LO_MARK_PERCENT_FOR_PSR 15
  347. #define LO_MARK_PERCENT_FOR_RX 15
  348. /* RFD (Receive Frame Descriptor) */
  349. struct rfd {
  350. struct list_head list_node;
  351. struct sk_buff *skb;
  352. u32 len; /* total size of receive frame */
  353. u16 bufferindex;
  354. u8 ringindex;
  355. };
  356. /* Flow Control */
  357. #define FLOW_BOTH 0
  358. #define FLOW_TXONLY 1
  359. #define FLOW_RXONLY 2
  360. #define FLOW_NONE 3
  361. /* Struct to define some device statistics */
  362. struct ce_stats {
  363. u32 multicast_pkts_rcvd;
  364. u32 rcvd_pkts_dropped;
  365. u32 tx_underflows;
  366. u32 tx_collisions;
  367. u32 tx_excessive_collisions;
  368. u32 tx_first_collisions;
  369. u32 tx_late_collisions;
  370. u32 tx_max_pkt_errs;
  371. u32 tx_deferred;
  372. u32 rx_overflows;
  373. u32 rx_length_errs;
  374. u32 rx_align_errs;
  375. u32 rx_crc_errs;
  376. u32 rx_code_violations;
  377. u32 rx_other_errs;
  378. u32 interrupt_status;
  379. };
  380. /* The private adapter structure */
  381. struct et131x_adapter {
  382. struct net_device *netdev;
  383. struct pci_dev *pdev;
  384. struct mii_bus *mii_bus;
  385. struct napi_struct napi;
  386. /* Flags that indicate current state of the adapter */
  387. u32 flags;
  388. /* local link state, to determine if a state change has occurred */
  389. int link;
  390. /* Configuration */
  391. u8 rom_addr[ETH_ALEN];
  392. u8 addr[ETH_ALEN];
  393. bool has_eeprom;
  394. u8 eeprom_data[2];
  395. spinlock_t tcb_send_qlock; /* protects the tx_ring send tcb list */
  396. spinlock_t tcb_ready_qlock; /* protects the tx_ring ready tcb list */
  397. spinlock_t rcv_lock; /* protects the rx_ring receive list */
  398. /* Packet Filter and look ahead size */
  399. u32 packet_filter;
  400. /* multicast list */
  401. u32 multicast_addr_count;
  402. u8 multicast_list[NIC_MAX_MCAST_LIST][ETH_ALEN];
  403. /* Pointer to the device's PCI register space */
  404. struct address_map __iomem *regs;
  405. /* Registry parameters */
  406. u8 wanted_flow; /* Flow we want for 802.3x flow control */
  407. u32 registry_jumbo_packet; /* Max supported ethernet packet size */
  408. /* Derived from the registry: */
  409. u8 flow; /* flow control validated by the far-end */
  410. /* Minimize init-time */
  411. struct timer_list error_timer;
  412. /* variable putting the phy into coma mode when boot up with no cable
  413. * plugged in after 5 seconds
  414. */
  415. u8 boot_coma;
  416. /* Tx Memory Variables */
  417. struct tx_ring tx_ring;
  418. /* Rx Memory Variables */
  419. struct rx_ring rx_ring;
  420. struct ce_stats stats;
  421. };
  422. static int eeprom_wait_ready(struct pci_dev *pdev, u32 *status)
  423. {
  424. u32 reg;
  425. int i;
  426. /* 1. Check LBCIF Status Register for bits 6 & 3:2 all equal to 0 and
  427. * bits 7,1:0 both equal to 1, at least once after reset.
  428. * Subsequent operations need only to check that bits 1:0 are equal
  429. * to 1 prior to starting a single byte read/write
  430. */
  431. for (i = 0; i < MAX_NUM_REGISTER_POLLS; i++) {
  432. if (pci_read_config_dword(pdev, LBCIF_DWORD1_GROUP, &reg))
  433. return -EIO;
  434. /* I2C idle and Phy Queue Avail both true */
  435. if ((reg & 0x3000) == 0x3000) {
  436. if (status)
  437. *status = reg;
  438. return reg & 0xFF;
  439. }
  440. }
  441. return -ETIMEDOUT;
  442. }
  443. static int eeprom_write(struct et131x_adapter *adapter, u32 addr, u8 data)
  444. {
  445. struct pci_dev *pdev = adapter->pdev;
  446. int index = 0;
  447. int retries;
  448. int err = 0;
  449. int writeok = 0;
  450. u32 status;
  451. u32 val = 0;
  452. /* For an EEPROM, an I2C single byte write is defined as a START
  453. * condition followed by the device address, EEPROM address, one byte
  454. * of data and a STOP condition. The STOP condition will trigger the
  455. * EEPROM's internally timed write cycle to the nonvolatile memory.
  456. * All inputs are disabled during this write cycle and the EEPROM will
  457. * not respond to any access until the internal write is complete.
  458. */
  459. err = eeprom_wait_ready(pdev, NULL);
  460. if (err < 0)
  461. return err;
  462. /* 2. Write to the LBCIF Control Register: bit 7=1, bit 6=1, bit 3=0,
  463. * and bits 1:0 both =0. Bit 5 should be set according to the
  464. * type of EEPROM being accessed (1=two byte addressing, 0=one
  465. * byte addressing).
  466. */
  467. if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
  468. LBCIF_CONTROL_LBCIF_ENABLE |
  469. LBCIF_CONTROL_I2C_WRITE))
  470. return -EIO;
  471. /* Prepare EEPROM address for Step 3 */
  472. for (retries = 0; retries < MAX_NUM_WRITE_RETRIES; retries++) {
  473. if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
  474. break;
  475. /* Write the data to the LBCIF Data Register (the I2C write
  476. * will begin).
  477. */
  478. if (pci_write_config_byte(pdev, LBCIF_DATA_REGISTER, data))
  479. break;
  480. /* Monitor bit 1:0 of the LBCIF Status Register. When bits
  481. * 1:0 are both equal to 1, the I2C write has completed and the
  482. * internal write cycle of the EEPROM is about to start.
  483. * (bits 1:0 = 01 is a legal state while waiting from both
  484. * equal to 1, but bits 1:0 = 10 is invalid and implies that
  485. * something is broken).
  486. */
  487. err = eeprom_wait_ready(pdev, &status);
  488. if (err < 0)
  489. return 0;
  490. /* Check bit 3 of the LBCIF Status Register. If equal to 1,
  491. * an error has occurred.Don't break here if we are revision
  492. * 1, this is so we do a blind write for load bug.
  493. */
  494. if ((status & LBCIF_STATUS_GENERAL_ERROR) &&
  495. adapter->pdev->revision == 0)
  496. break;
  497. /* Check bit 2 of the LBCIF Status Register. If equal to 1 an
  498. * ACK error has occurred on the address phase of the write.
  499. * This could be due to an actual hardware failure or the
  500. * EEPROM may still be in its internal write cycle from a
  501. * previous write. This write operation was ignored and must be
  502. *repeated later.
  503. */
  504. if (status & LBCIF_STATUS_ACK_ERROR) {
  505. /* This could be due to an actual hardware failure
  506. * or the EEPROM may still be in its internal write
  507. * cycle from a previous write. This write operation
  508. * was ignored and must be repeated later.
  509. */
  510. udelay(10);
  511. continue;
  512. }
  513. writeok = 1;
  514. break;
  515. }
  516. udelay(10);
  517. while (1) {
  518. if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
  519. LBCIF_CONTROL_LBCIF_ENABLE))
  520. writeok = 0;
  521. /* Do read until internal ACK_ERROR goes away meaning write
  522. * completed
  523. */
  524. do {
  525. pci_write_config_dword(pdev,
  526. LBCIF_ADDRESS_REGISTER,
  527. addr);
  528. do {
  529. pci_read_config_dword(pdev,
  530. LBCIF_DATA_REGISTER,
  531. &val);
  532. } while ((val & 0x00010000) == 0);
  533. } while (val & 0x00040000);
  534. if ((val & 0xFF00) != 0xC000 || index == 10000)
  535. break;
  536. index++;
  537. }
  538. return writeok ? 0 : -EIO;
  539. }
  540. static int eeprom_read(struct et131x_adapter *adapter, u32 addr, u8 *pdata)
  541. {
  542. struct pci_dev *pdev = adapter->pdev;
  543. int err;
  544. u32 status;
  545. /* A single byte read is similar to the single byte write, with the
  546. * exception of the data flow:
  547. */
  548. err = eeprom_wait_ready(pdev, NULL);
  549. if (err < 0)
  550. return err;
  551. /* Write to the LBCIF Control Register: bit 7=1, bit 6=0, bit 3=0,
  552. * and bits 1:0 both =0. Bit 5 should be set according to the type
  553. * of EEPROM being accessed (1=two byte addressing, 0=one byte
  554. * addressing).
  555. */
  556. if (pci_write_config_byte(pdev, LBCIF_CONTROL_REGISTER,
  557. LBCIF_CONTROL_LBCIF_ENABLE))
  558. return -EIO;
  559. /* Write the address to the LBCIF Address Register (I2C read will
  560. * begin).
  561. */
  562. if (pci_write_config_dword(pdev, LBCIF_ADDRESS_REGISTER, addr))
  563. return -EIO;
  564. /* Monitor bit 0 of the LBCIF Status Register. When = 1, I2C read
  565. * is complete. (if bit 1 =1 and bit 0 stays = 0, a hardware failure
  566. * has occurred).
  567. */
  568. err = eeprom_wait_ready(pdev, &status);
  569. if (err < 0)
  570. return err;
  571. /* Regardless of error status, read data byte from LBCIF Data
  572. * Register.
  573. */
  574. *pdata = err;
  575. return (status & LBCIF_STATUS_ACK_ERROR) ? -EIO : 0;
  576. }
  577. static int et131x_init_eeprom(struct et131x_adapter *adapter)
  578. {
  579. struct pci_dev *pdev = adapter->pdev;
  580. u8 eestatus;
  581. pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus);
  582. /* THIS IS A WORKAROUND:
  583. * I need to call this function twice to get my card in a
  584. * LG M1 Express Dual running. I tried also a msleep before this
  585. * function, because I thought there could be some time conditions
  586. * but it didn't work. Call the whole function twice also work.
  587. */
  588. if (pci_read_config_byte(pdev, ET1310_PCI_EEPROM_STATUS, &eestatus)) {
  589. dev_err(&pdev->dev,
  590. "Could not read PCI config space for EEPROM Status\n");
  591. return -EIO;
  592. }
  593. /* Determine if the error(s) we care about are present. If they are
  594. * present we need to fail.
  595. */
  596. if (eestatus & 0x4C) {
  597. int write_failed = 0;
  598. if (pdev->revision == 0x01) {
  599. int i;
  600. static const u8 eedata[4] = { 0xFE, 0x13, 0x10, 0xFF };
  601. /* Re-write the first 4 bytes if we have an eeprom
  602. * present and the revision id is 1, this fixes the
  603. * corruption seen with 1310 B Silicon
  604. */
  605. for (i = 0; i < 3; i++)
  606. if (eeprom_write(adapter, i, eedata[i]) < 0)
  607. write_failed = 1;
  608. }
  609. if (pdev->revision != 0x01 || write_failed) {
  610. dev_err(&pdev->dev,
  611. "Fatal EEPROM Status Error - 0x%04x\n",
  612. eestatus);
  613. /* This error could mean that there was an error
  614. * reading the eeprom or that the eeprom doesn't exist.
  615. * We will treat each case the same and not try to
  616. * gather additional information that normally would
  617. * come from the eeprom, like MAC Address
  618. */
  619. adapter->has_eeprom = 0;
  620. return -EIO;
  621. }
  622. }
  623. adapter->has_eeprom = 1;
  624. /* Read the EEPROM for information regarding LED behavior. Refer to
  625. * et131x_xcvr_init() for its use.
  626. */
  627. eeprom_read(adapter, 0x70, &adapter->eeprom_data[0]);
  628. eeprom_read(adapter, 0x71, &adapter->eeprom_data[1]);
  629. if (adapter->eeprom_data[0] != 0xcd)
  630. /* Disable all optional features */
  631. adapter->eeprom_data[1] = 0x00;
  632. return 0;
  633. }
  634. static void et131x_rx_dma_enable(struct et131x_adapter *adapter)
  635. {
  636. /* Setup the receive dma configuration register for normal operation */
  637. u32 csr = ET_RXDMA_CSR_FBR1_ENABLE;
  638. struct rx_ring *rx_ring = &adapter->rx_ring;
  639. if (rx_ring->fbr[1]->buffsize == 4096)
  640. csr |= ET_RXDMA_CSR_FBR1_SIZE_LO;
  641. else if (rx_ring->fbr[1]->buffsize == 8192)
  642. csr |= ET_RXDMA_CSR_FBR1_SIZE_HI;
  643. else if (rx_ring->fbr[1]->buffsize == 16384)
  644. csr |= ET_RXDMA_CSR_FBR1_SIZE_LO | ET_RXDMA_CSR_FBR1_SIZE_HI;
  645. csr |= ET_RXDMA_CSR_FBR0_ENABLE;
  646. if (rx_ring->fbr[0]->buffsize == 256)
  647. csr |= ET_RXDMA_CSR_FBR0_SIZE_LO;
  648. else if (rx_ring->fbr[0]->buffsize == 512)
  649. csr |= ET_RXDMA_CSR_FBR0_SIZE_HI;
  650. else if (rx_ring->fbr[0]->buffsize == 1024)
  651. csr |= ET_RXDMA_CSR_FBR0_SIZE_LO | ET_RXDMA_CSR_FBR0_SIZE_HI;
  652. writel(csr, &adapter->regs->rxdma.csr);
  653. csr = readl(&adapter->regs->rxdma.csr);
  654. if (csr & ET_RXDMA_CSR_HALT_STATUS) {
  655. udelay(5);
  656. csr = readl(&adapter->regs->rxdma.csr);
  657. if (csr & ET_RXDMA_CSR_HALT_STATUS) {
  658. dev_err(&adapter->pdev->dev,
  659. "RX Dma failed to exit halt state. CSR 0x%08x\n",
  660. csr);
  661. }
  662. }
  663. }
  664. static void et131x_rx_dma_disable(struct et131x_adapter *adapter)
  665. {
  666. u32 csr;
  667. /* Setup the receive dma configuration register */
  668. writel(ET_RXDMA_CSR_HALT | ET_RXDMA_CSR_FBR1_ENABLE,
  669. &adapter->regs->rxdma.csr);
  670. csr = readl(&adapter->regs->rxdma.csr);
  671. if (!(csr & ET_RXDMA_CSR_HALT_STATUS)) {
  672. udelay(5);
  673. csr = readl(&adapter->regs->rxdma.csr);
  674. if (!(csr & ET_RXDMA_CSR_HALT_STATUS))
  675. dev_err(&adapter->pdev->dev,
  676. "RX Dma failed to enter halt state. CSR 0x%08x\n",
  677. csr);
  678. }
  679. }
  680. static void et131x_tx_dma_enable(struct et131x_adapter *adapter)
  681. {
  682. /* Setup the transmit dma configuration register for normal
  683. * operation
  684. */
  685. writel(ET_TXDMA_SNGL_EPKT | (PARM_DMA_CACHE_DEF << ET_TXDMA_CACHE_SHIFT),
  686. &adapter->regs->txdma.csr);
  687. }
  688. static inline void add_10bit(u32 *v, int n)
  689. {
  690. *v = INDEX10(*v + n) | (*v & ET_DMA10_WRAP);
  691. }
  692. static inline void add_12bit(u32 *v, int n)
  693. {
  694. *v = INDEX12(*v + n) | (*v & ET_DMA12_WRAP);
  695. }
  696. static void et1310_config_mac_regs1(struct et131x_adapter *adapter)
  697. {
  698. struct mac_regs __iomem *macregs = &adapter->regs->mac;
  699. u32 station1;
  700. u32 station2;
  701. u32 ipg;
  702. /* First we need to reset everything. Write to MAC configuration
  703. * register 1 to perform reset.
  704. */
  705. writel(ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET |
  706. ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
  707. ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC,
  708. &macregs->cfg1);
  709. /* Next lets configure the MAC Inter-packet gap register */
  710. ipg = 0x38005860; /* IPG1 0x38 IPG2 0x58 B2B 0x60 */
  711. ipg |= 0x50 << 8; /* ifg enforce 0x50 */
  712. writel(ipg, &macregs->ipg);
  713. /* Next lets configure the MAC Half Duplex register */
  714. /* BEB trunc 0xA, Ex Defer, Rexmit 0xF Coll 0x37 */
  715. writel(0x00A1F037, &macregs->hfdp);
  716. /* Next lets configure the MAC Interface Control register */
  717. writel(0, &macregs->if_ctrl);
  718. writel(ET_MAC_MIIMGMT_CLK_RST, &macregs->mii_mgmt_cfg);
  719. /* Next lets configure the MAC Station Address register. These
  720. * values are read from the EEPROM during initialization and stored
  721. * in the adapter structure. We write what is stored in the adapter
  722. * structure to the MAC Station Address registers high and low. This
  723. * station address is used for generating and checking pause control
  724. * packets.
  725. */
  726. station2 = (adapter->addr[1] << ET_MAC_STATION_ADDR2_OC2_SHIFT) |
  727. (adapter->addr[0] << ET_MAC_STATION_ADDR2_OC1_SHIFT);
  728. station1 = (adapter->addr[5] << ET_MAC_STATION_ADDR1_OC6_SHIFT) |
  729. (adapter->addr[4] << ET_MAC_STATION_ADDR1_OC5_SHIFT) |
  730. (adapter->addr[3] << ET_MAC_STATION_ADDR1_OC4_SHIFT) |
  731. adapter->addr[2];
  732. writel(station1, &macregs->station_addr_1);
  733. writel(station2, &macregs->station_addr_2);
  734. /* Max ethernet packet in bytes that will be passed by the mac without
  735. * being truncated. Allow the MAC to pass 4 more than our max packet
  736. * size. This is 4 for the Ethernet CRC.
  737. *
  738. * Packets larger than (registry_jumbo_packet) that do not contain a
  739. * VLAN ID will be dropped by the Rx function.
  740. */
  741. writel(adapter->registry_jumbo_packet + 4, &macregs->max_fm_len);
  742. /* clear out MAC config reset */
  743. writel(0, &macregs->cfg1);
  744. }
  745. static void et1310_config_mac_regs2(struct et131x_adapter *adapter)
  746. {
  747. int32_t delay = 0;
  748. struct mac_regs __iomem *mac = &adapter->regs->mac;
  749. struct phy_device *phydev = adapter->netdev->phydev;
  750. u32 cfg1;
  751. u32 cfg2;
  752. u32 ifctrl;
  753. u32 ctl;
  754. ctl = readl(&adapter->regs->txmac.ctl);
  755. cfg1 = readl(&mac->cfg1);
  756. cfg2 = readl(&mac->cfg2);
  757. ifctrl = readl(&mac->if_ctrl);
  758. /* Set up the if mode bits */
  759. cfg2 &= ~ET_MAC_CFG2_IFMODE_MASK;
  760. if (phydev->speed == SPEED_1000) {
  761. cfg2 |= ET_MAC_CFG2_IFMODE_1000;
  762. ifctrl &= ~ET_MAC_IFCTRL_PHYMODE;
  763. } else {
  764. cfg2 |= ET_MAC_CFG2_IFMODE_100;
  765. ifctrl |= ET_MAC_IFCTRL_PHYMODE;
  766. }
  767. cfg1 |= ET_MAC_CFG1_RX_ENABLE | ET_MAC_CFG1_TX_ENABLE |
  768. ET_MAC_CFG1_TX_FLOW;
  769. cfg1 &= ~(ET_MAC_CFG1_LOOPBACK | ET_MAC_CFG1_RX_FLOW);
  770. if (adapter->flow == FLOW_RXONLY || adapter->flow == FLOW_BOTH)
  771. cfg1 |= ET_MAC_CFG1_RX_FLOW;
  772. writel(cfg1, &mac->cfg1);
  773. /* Now we need to initialize the MAC Configuration 2 register */
  774. /* preamble 7, check length, huge frame off, pad crc, crc enable
  775. * full duplex off
  776. */
  777. cfg2 |= 0x7 << ET_MAC_CFG2_PREAMBLE_SHIFT;
  778. cfg2 |= ET_MAC_CFG2_IFMODE_LEN_CHECK;
  779. cfg2 |= ET_MAC_CFG2_IFMODE_PAD_CRC;
  780. cfg2 |= ET_MAC_CFG2_IFMODE_CRC_ENABLE;
  781. cfg2 &= ~ET_MAC_CFG2_IFMODE_HUGE_FRAME;
  782. cfg2 &= ~ET_MAC_CFG2_IFMODE_FULL_DPLX;
  783. if (phydev->duplex == DUPLEX_FULL)
  784. cfg2 |= ET_MAC_CFG2_IFMODE_FULL_DPLX;
  785. ifctrl &= ~ET_MAC_IFCTRL_GHDMODE;
  786. if (phydev->duplex == DUPLEX_HALF)
  787. ifctrl |= ET_MAC_IFCTRL_GHDMODE;
  788. writel(ifctrl, &mac->if_ctrl);
  789. writel(cfg2, &mac->cfg2);
  790. do {
  791. udelay(10);
  792. delay++;
  793. cfg1 = readl(&mac->cfg1);
  794. } while ((cfg1 & ET_MAC_CFG1_WAIT) != ET_MAC_CFG1_WAIT && delay < 100);
  795. if (delay == 100) {
  796. dev_warn(&adapter->pdev->dev,
  797. "Syncd bits did not respond correctly cfg1 word 0x%08x\n",
  798. cfg1);
  799. }
  800. ctl |= ET_TX_CTRL_TXMAC_ENABLE | ET_TX_CTRL_FC_DISABLE;
  801. writel(ctl, &adapter->regs->txmac.ctl);
  802. if (adapter->flags & FMP_ADAPTER_LOWER_POWER) {
  803. et131x_rx_dma_enable(adapter);
  804. et131x_tx_dma_enable(adapter);
  805. }
  806. }
  807. static int et1310_in_phy_coma(struct et131x_adapter *adapter)
  808. {
  809. u32 pmcsr = readl(&adapter->regs->global.pm_csr);
  810. return ET_PM_PHY_SW_COMA & pmcsr ? 1 : 0;
  811. }
  812. static void et1310_setup_device_for_multicast(struct et131x_adapter *adapter)
  813. {
  814. struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
  815. u32 hash1 = 0;
  816. u32 hash2 = 0;
  817. u32 hash3 = 0;
  818. u32 hash4 = 0;
  819. u32 pm_csr;
  820. /* If ET131X_PACKET_TYPE_MULTICAST is specified, then we provision
  821. * the multi-cast LIST. If it is NOT specified, (and "ALL" is not
  822. * specified) then we should pass NO multi-cast addresses to the
  823. * driver.
  824. */
  825. if (adapter->packet_filter & ET131X_PACKET_TYPE_MULTICAST) {
  826. int i;
  827. /* Loop through our multicast array and set up the device */
  828. for (i = 0; i < adapter->multicast_addr_count; i++) {
  829. u32 result;
  830. result = ether_crc(6, adapter->multicast_list[i]);
  831. result = (result & 0x3F800000) >> 23;
  832. if (result < 32) {
  833. hash1 |= (1 << result);
  834. } else if ((31 < result) && (result < 64)) {
  835. result -= 32;
  836. hash2 |= (1 << result);
  837. } else if ((63 < result) && (result < 96)) {
  838. result -= 64;
  839. hash3 |= (1 << result);
  840. } else {
  841. result -= 96;
  842. hash4 |= (1 << result);
  843. }
  844. }
  845. }
  846. /* Write out the new hash to the device */
  847. pm_csr = readl(&adapter->regs->global.pm_csr);
  848. if (!et1310_in_phy_coma(adapter)) {
  849. writel(hash1, &rxmac->multi_hash1);
  850. writel(hash2, &rxmac->multi_hash2);
  851. writel(hash3, &rxmac->multi_hash3);
  852. writel(hash4, &rxmac->multi_hash4);
  853. }
  854. }
  855. static void et1310_setup_device_for_unicast(struct et131x_adapter *adapter)
  856. {
  857. struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
  858. u32 uni_pf1;
  859. u32 uni_pf2;
  860. u32 uni_pf3;
  861. u32 pm_csr;
  862. /* Set up unicast packet filter reg 3 to be the first two octets of
  863. * the MAC address for both address
  864. *
  865. * Set up unicast packet filter reg 2 to be the octets 2 - 5 of the
  866. * MAC address for second address
  867. *
  868. * Set up unicast packet filter reg 3 to be the octets 2 - 5 of the
  869. * MAC address for first address
  870. */
  871. uni_pf3 = (adapter->addr[0] << ET_RX_UNI_PF_ADDR2_1_SHIFT) |
  872. (adapter->addr[1] << ET_RX_UNI_PF_ADDR2_2_SHIFT) |
  873. (adapter->addr[0] << ET_RX_UNI_PF_ADDR1_1_SHIFT) |
  874. adapter->addr[1];
  875. uni_pf2 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR2_3_SHIFT) |
  876. (adapter->addr[3] << ET_RX_UNI_PF_ADDR2_4_SHIFT) |
  877. (adapter->addr[4] << ET_RX_UNI_PF_ADDR2_5_SHIFT) |
  878. adapter->addr[5];
  879. uni_pf1 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR1_3_SHIFT) |
  880. (adapter->addr[3] << ET_RX_UNI_PF_ADDR1_4_SHIFT) |
  881. (adapter->addr[4] << ET_RX_UNI_PF_ADDR1_5_SHIFT) |
  882. adapter->addr[5];
  883. pm_csr = readl(&adapter->regs->global.pm_csr);
  884. if (!et1310_in_phy_coma(adapter)) {
  885. writel(uni_pf1, &rxmac->uni_pf_addr1);
  886. writel(uni_pf2, &rxmac->uni_pf_addr2);
  887. writel(uni_pf3, &rxmac->uni_pf_addr3);
  888. }
  889. }
  890. static void et1310_config_rxmac_regs(struct et131x_adapter *adapter)
  891. {
  892. struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
  893. struct phy_device *phydev = adapter->netdev->phydev;
  894. u32 sa_lo;
  895. u32 sa_hi = 0;
  896. u32 pf_ctrl = 0;
  897. u32 __iomem *wolw;
  898. /* Disable the MAC while it is being configured (also disable WOL) */
  899. writel(0x8, &rxmac->ctrl);
  900. /* Initialize WOL to disabled. */
  901. writel(0, &rxmac->crc0);
  902. writel(0, &rxmac->crc12);
  903. writel(0, &rxmac->crc34);
  904. /* We need to set the WOL mask0 - mask4 next. We initialize it to
  905. * its default Values of 0x00000000 because there are not WOL masks
  906. * as of this time.
  907. */
  908. for (wolw = &rxmac->mask0_word0; wolw <= &rxmac->mask4_word3; wolw++)
  909. writel(0, wolw);
  910. /* Lets setup the WOL Source Address */
  911. sa_lo = (adapter->addr[2] << ET_RX_WOL_LO_SA3_SHIFT) |
  912. (adapter->addr[3] << ET_RX_WOL_LO_SA4_SHIFT) |
  913. (adapter->addr[4] << ET_RX_WOL_LO_SA5_SHIFT) |
  914. adapter->addr[5];
  915. writel(sa_lo, &rxmac->sa_lo);
  916. sa_hi = (u32)(adapter->addr[0] << ET_RX_WOL_HI_SA1_SHIFT) |
  917. adapter->addr[1];
  918. writel(sa_hi, &rxmac->sa_hi);
  919. /* Disable all Packet Filtering */
  920. writel(0, &rxmac->pf_ctrl);
  921. /* Let's initialize the Unicast Packet filtering address */
  922. if (adapter->packet_filter & ET131X_PACKET_TYPE_DIRECTED) {
  923. et1310_setup_device_for_unicast(adapter);
  924. pf_ctrl |= ET_RX_PFCTRL_UNICST_FILTER_ENABLE;
  925. } else {
  926. writel(0, &rxmac->uni_pf_addr1);
  927. writel(0, &rxmac->uni_pf_addr2);
  928. writel(0, &rxmac->uni_pf_addr3);
  929. }
  930. /* Let's initialize the Multicast hash */
  931. if (!(adapter->packet_filter & ET131X_PACKET_TYPE_ALL_MULTICAST)) {
  932. pf_ctrl |= ET_RX_PFCTRL_MLTCST_FILTER_ENABLE;
  933. et1310_setup_device_for_multicast(adapter);
  934. }
  935. /* Runt packet filtering. Didn't work in version A silicon. */
  936. pf_ctrl |= (NIC_MIN_PACKET_SIZE + 4) << ET_RX_PFCTRL_MIN_PKT_SZ_SHIFT;
  937. pf_ctrl |= ET_RX_PFCTRL_FRAG_FILTER_ENABLE;
  938. if (adapter->registry_jumbo_packet > 8192)
  939. /* In order to transmit jumbo packets greater than 8k, the
  940. * FIFO between RxMAC and RxDMA needs to be reduced in size
  941. * to (16k - Jumbo packet size). In order to implement this,
  942. * we must use "cut through" mode in the RxMAC, which chops
  943. * packets down into segments which are (max_size * 16). In
  944. * this case we selected 256 bytes, since this is the size of
  945. * the PCI-Express TLP's that the 1310 uses.
  946. *
  947. * seg_en on, fc_en off, size 0x10
  948. */
  949. writel(0x41, &rxmac->mcif_ctrl_max_seg);
  950. else
  951. writel(0, &rxmac->mcif_ctrl_max_seg);
  952. writel(0, &rxmac->mcif_water_mark);
  953. writel(0, &rxmac->mif_ctrl);
  954. writel(0, &rxmac->space_avail);
  955. /* Initialize the the mif_ctrl register
  956. * bit 3: Receive code error. One or more nibbles were signaled as
  957. * errors during the reception of the packet. Clear this
  958. * bit in Gigabit, set it in 100Mbit. This was derived
  959. * experimentally at UNH.
  960. * bit 4: Receive CRC error. The packet's CRC did not match the
  961. * internally generated CRC.
  962. * bit 5: Receive length check error. Indicates that frame length
  963. * field value in the packet does not match the actual data
  964. * byte length and is not a type field.
  965. * bit 16: Receive frame truncated.
  966. * bit 17: Drop packet enable
  967. */
  968. if (phydev && phydev->speed == SPEED_100)
  969. writel(0x30038, &rxmac->mif_ctrl);
  970. else
  971. writel(0x30030, &rxmac->mif_ctrl);
  972. /* Finally we initialize RxMac to be enabled & WOL disabled. Packet
  973. * filter is always enabled since it is where the runt packets are
  974. * supposed to be dropped. For version A silicon, runt packet
  975. * dropping doesn't work, so it is disabled in the pf_ctrl register,
  976. * but we still leave the packet filter on.
  977. */
  978. writel(pf_ctrl, &rxmac->pf_ctrl);
  979. writel(ET_RX_CTRL_RXMAC_ENABLE | ET_RX_CTRL_WOL_DISABLE, &rxmac->ctrl);
  980. }
  981. static void et1310_config_txmac_regs(struct et131x_adapter *adapter)
  982. {
  983. struct txmac_regs __iomem *txmac = &adapter->regs->txmac;
  984. /* We need to update the Control Frame Parameters
  985. * cfpt - control frame pause timer set to 64 (0x40)
  986. * cfep - control frame extended pause timer set to 0x0
  987. */
  988. if (adapter->flow == FLOW_NONE)
  989. writel(0, &txmac->cf_param);
  990. else
  991. writel(0x40, &txmac->cf_param);
  992. }
  993. static void et1310_config_macstat_regs(struct et131x_adapter *adapter)
  994. {
  995. struct macstat_regs __iomem *macstat = &adapter->regs->macstat;
  996. u32 __iomem *reg;
  997. /* initialize all the macstat registers to zero on the device */
  998. for (reg = &macstat->txrx_0_64_byte_frames;
  999. reg <= &macstat->carry_reg2; reg++)
  1000. writel(0, reg);
  1001. /* Unmask any counters that we want to track the overflow of.
  1002. * Initially this will be all counters. It may become clear later
  1003. * that we do not need to track all counters.
  1004. */
  1005. writel(0xFFFFBE32, &macstat->carry_reg1_mask);
  1006. writel(0xFFFE7E8B, &macstat->carry_reg2_mask);
  1007. }
  1008. static int et131x_phy_mii_read(struct et131x_adapter *adapter, u8 addr,
  1009. u8 reg, u16 *value)
  1010. {
  1011. struct mac_regs __iomem *mac = &adapter->regs->mac;
  1012. int status = 0;
  1013. u32 delay = 0;
  1014. u32 mii_addr;
  1015. u32 mii_cmd;
  1016. u32 mii_indicator;
  1017. /* Save a local copy of the registers we are dealing with so we can
  1018. * set them back
  1019. */
  1020. mii_addr = readl(&mac->mii_mgmt_addr);
  1021. mii_cmd = readl(&mac->mii_mgmt_cmd);
  1022. /* Stop the current operation */
  1023. writel(0, &mac->mii_mgmt_cmd);
  1024. /* Set up the register we need to read from on the correct PHY */
  1025. writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
  1026. writel(0x1, &mac->mii_mgmt_cmd);
  1027. do {
  1028. udelay(50);
  1029. delay++;
  1030. mii_indicator = readl(&mac->mii_mgmt_indicator);
  1031. } while ((mii_indicator & ET_MAC_MGMT_WAIT) && delay < 50);
  1032. /* If we hit the max delay, we could not read the register */
  1033. if (delay == 50) {
  1034. dev_warn(&adapter->pdev->dev,
  1035. "reg 0x%08x could not be read\n", reg);
  1036. dev_warn(&adapter->pdev->dev, "status is 0x%08x\n",
  1037. mii_indicator);
  1038. status = -EIO;
  1039. goto out;
  1040. }
  1041. /* If we hit here we were able to read the register and we need to
  1042. * return the value to the caller
  1043. */
  1044. *value = readl(&mac->mii_mgmt_stat) & ET_MAC_MIIMGMT_STAT_PHYCRTL_MASK;
  1045. out:
  1046. /* Stop the read operation */
  1047. writel(0, &mac->mii_mgmt_cmd);
  1048. /* set the registers we touched back to the state at which we entered
  1049. * this function
  1050. */
  1051. writel(mii_addr, &mac->mii_mgmt_addr);
  1052. writel(mii_cmd, &mac->mii_mgmt_cmd);
  1053. return status;
  1054. }
  1055. static int et131x_mii_read(struct et131x_adapter *adapter, u8 reg, u16 *value)
  1056. {
  1057. struct phy_device *phydev = adapter->netdev->phydev;
  1058. if (!phydev)
  1059. return -EIO;
  1060. return et131x_phy_mii_read(adapter, phydev->mdio.addr, reg, value);
  1061. }
  1062. static int et131x_mii_write(struct et131x_adapter *adapter, u8 addr, u8 reg,
  1063. u16 value)
  1064. {
  1065. struct mac_regs __iomem *mac = &adapter->regs->mac;
  1066. int status = 0;
  1067. u32 delay = 0;
  1068. u32 mii_addr;
  1069. u32 mii_cmd;
  1070. u32 mii_indicator;
  1071. /* Save a local copy of the registers we are dealing with so we can
  1072. * set them back
  1073. */
  1074. mii_addr = readl(&mac->mii_mgmt_addr);
  1075. mii_cmd = readl(&mac->mii_mgmt_cmd);
  1076. /* Stop the current operation */
  1077. writel(0, &mac->mii_mgmt_cmd);
  1078. /* Set up the register we need to write to on the correct PHY */
  1079. writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
  1080. /* Add the value to write to the registers to the mac */
  1081. writel(value, &mac->mii_mgmt_ctrl);
  1082. do {
  1083. udelay(50);
  1084. delay++;
  1085. mii_indicator = readl(&mac->mii_mgmt_indicator);
  1086. } while ((mii_indicator & ET_MAC_MGMT_BUSY) && delay < 100);
  1087. /* If we hit the max delay, we could not write the register */
  1088. if (delay == 100) {
  1089. u16 tmp;
  1090. dev_warn(&adapter->pdev->dev,
  1091. "reg 0x%08x could not be written", reg);
  1092. dev_warn(&adapter->pdev->dev, "status is 0x%08x\n",
  1093. mii_indicator);
  1094. dev_warn(&adapter->pdev->dev, "command is 0x%08x\n",
  1095. readl(&mac->mii_mgmt_cmd));
  1096. et131x_mii_read(adapter, reg, &tmp);
  1097. status = -EIO;
  1098. }
  1099. /* Stop the write operation */
  1100. writel(0, &mac->mii_mgmt_cmd);
  1101. /* set the registers we touched back to the state at which we entered
  1102. * this function
  1103. */
  1104. writel(mii_addr, &mac->mii_mgmt_addr);
  1105. writel(mii_cmd, &mac->mii_mgmt_cmd);
  1106. return status;
  1107. }
  1108. static void et1310_phy_read_mii_bit(struct et131x_adapter *adapter,
  1109. u16 regnum,
  1110. u16 bitnum,
  1111. u8 *value)
  1112. {
  1113. u16 reg;
  1114. u16 mask = 1 << bitnum;
  1115. et131x_mii_read(adapter, regnum, &reg);
  1116. *value = (reg & mask) >> bitnum;
  1117. }
  1118. static void et1310_config_flow_control(struct et131x_adapter *adapter)
  1119. {
  1120. struct phy_device *phydev = adapter->netdev->phydev;
  1121. if (phydev->duplex == DUPLEX_HALF) {
  1122. adapter->flow = FLOW_NONE;
  1123. } else {
  1124. char remote_pause, remote_async_pause;
  1125. et1310_phy_read_mii_bit(adapter, 5, 10, &remote_pause);
  1126. et1310_phy_read_mii_bit(adapter, 5, 11, &remote_async_pause);
  1127. if (remote_pause && remote_async_pause) {
  1128. adapter->flow = adapter->wanted_flow;
  1129. } else if (remote_pause && !remote_async_pause) {
  1130. if (adapter->wanted_flow == FLOW_BOTH)
  1131. adapter->flow = FLOW_BOTH;
  1132. else
  1133. adapter->flow = FLOW_NONE;
  1134. } else if (!remote_pause && !remote_async_pause) {
  1135. adapter->flow = FLOW_NONE;
  1136. } else {
  1137. if (adapter->wanted_flow == FLOW_BOTH)
  1138. adapter->flow = FLOW_RXONLY;
  1139. else
  1140. adapter->flow = FLOW_NONE;
  1141. }
  1142. }
  1143. }
  1144. /* et1310_update_macstat_host_counters - Update local copy of the statistics */
  1145. static void et1310_update_macstat_host_counters(struct et131x_adapter *adapter)
  1146. {
  1147. struct ce_stats *stats = &adapter->stats;
  1148. struct macstat_regs __iomem *macstat =
  1149. &adapter->regs->macstat;
  1150. stats->tx_collisions += readl(&macstat->tx_total_collisions);
  1151. stats->tx_first_collisions += readl(&macstat->tx_single_collisions);
  1152. stats->tx_deferred += readl(&macstat->tx_deferred);
  1153. stats->tx_excessive_collisions +=
  1154. readl(&macstat->tx_multiple_collisions);
  1155. stats->tx_late_collisions += readl(&macstat->tx_late_collisions);
  1156. stats->tx_underflows += readl(&macstat->tx_undersize_frames);
  1157. stats->tx_max_pkt_errs += readl(&macstat->tx_oversize_frames);
  1158. stats->rx_align_errs += readl(&macstat->rx_align_errs);
  1159. stats->rx_crc_errs += readl(&macstat->rx_code_errs);
  1160. stats->rcvd_pkts_dropped += readl(&macstat->rx_drops);
  1161. stats->rx_overflows += readl(&macstat->rx_oversize_packets);
  1162. stats->rx_code_violations += readl(&macstat->rx_fcs_errs);
  1163. stats->rx_length_errs += readl(&macstat->rx_frame_len_errs);
  1164. stats->rx_other_errs += readl(&macstat->rx_fragment_packets);
  1165. }
  1166. /* et1310_handle_macstat_interrupt
  1167. *
  1168. * One of the MACSTAT counters has wrapped. Update the local copy of
  1169. * the statistics held in the adapter structure, checking the "wrap"
  1170. * bit for each counter.
  1171. */
  1172. static void et1310_handle_macstat_interrupt(struct et131x_adapter *adapter)
  1173. {
  1174. u32 carry_reg1;
  1175. u32 carry_reg2;
  1176. /* Read the interrupt bits from the register(s). These are Clear On
  1177. * Write.
  1178. */
  1179. carry_reg1 = readl(&adapter->regs->macstat.carry_reg1);
  1180. carry_reg2 = readl(&adapter->regs->macstat.carry_reg2);
  1181. writel(carry_reg1, &adapter->regs->macstat.carry_reg1);
  1182. writel(carry_reg2, &adapter->regs->macstat.carry_reg2);
  1183. /* We need to do update the host copy of all the MAC_STAT counters.
  1184. * For each counter, check it's overflow bit. If the overflow bit is
  1185. * set, then increment the host version of the count by one complete
  1186. * revolution of the counter. This routine is called when the counter
  1187. * block indicates that one of the counters has wrapped.
  1188. */
  1189. if (carry_reg1 & (1 << 14))
  1190. adapter->stats.rx_code_violations += COUNTER_WRAP_16_BIT;
  1191. if (carry_reg1 & (1 << 8))
  1192. adapter->stats.rx_align_errs += COUNTER_WRAP_12_BIT;
  1193. if (carry_reg1 & (1 << 7))
  1194. adapter->stats.rx_length_errs += COUNTER_WRAP_16_BIT;
  1195. if (carry_reg1 & (1 << 2))
  1196. adapter->stats.rx_other_errs += COUNTER_WRAP_16_BIT;
  1197. if (carry_reg1 & (1 << 6))
  1198. adapter->stats.rx_crc_errs += COUNTER_WRAP_16_BIT;
  1199. if (carry_reg1 & (1 << 3))
  1200. adapter->stats.rx_overflows += COUNTER_WRAP_16_BIT;
  1201. if (carry_reg1 & (1 << 0))
  1202. adapter->stats.rcvd_pkts_dropped += COUNTER_WRAP_16_BIT;
  1203. if (carry_reg2 & (1 << 16))
  1204. adapter->stats.tx_max_pkt_errs += COUNTER_WRAP_12_BIT;
  1205. if (carry_reg2 & (1 << 15))
  1206. adapter->stats.tx_underflows += COUNTER_WRAP_12_BIT;
  1207. if (carry_reg2 & (1 << 6))
  1208. adapter->stats.tx_first_collisions += COUNTER_WRAP_12_BIT;
  1209. if (carry_reg2 & (1 << 8))
  1210. adapter->stats.tx_deferred += COUNTER_WRAP_12_BIT;
  1211. if (carry_reg2 & (1 << 5))
  1212. adapter->stats.tx_excessive_collisions += COUNTER_WRAP_12_BIT;
  1213. if (carry_reg2 & (1 << 4))
  1214. adapter->stats.tx_late_collisions += COUNTER_WRAP_12_BIT;
  1215. if (carry_reg2 & (1 << 2))
  1216. adapter->stats.tx_collisions += COUNTER_WRAP_12_BIT;
  1217. }
  1218. static int et131x_mdio_read(struct mii_bus *bus, int phy_addr, int reg)
  1219. {
  1220. struct net_device *netdev = bus->priv;
  1221. struct et131x_adapter *adapter = netdev_priv(netdev);
  1222. u16 value;
  1223. int ret;
  1224. ret = et131x_phy_mii_read(adapter, phy_addr, reg, &value);
  1225. if (ret < 0)
  1226. return ret;
  1227. return value;
  1228. }
  1229. static int et131x_mdio_write(struct mii_bus *bus, int phy_addr,
  1230. int reg, u16 value)
  1231. {
  1232. struct net_device *netdev = bus->priv;
  1233. struct et131x_adapter *adapter = netdev_priv(netdev);
  1234. return et131x_mii_write(adapter, phy_addr, reg, value);
  1235. }
  1236. /* et1310_phy_power_switch - PHY power control
  1237. * @adapter: device to control
  1238. * @down: true for off/false for back on
  1239. *
  1240. * one hundred, ten, one thousand megs
  1241. * How would you like to have your LAN accessed
  1242. * Can't you see that this code processed
  1243. * Phy power, phy power..
  1244. */
  1245. static void et1310_phy_power_switch(struct et131x_adapter *adapter, bool down)
  1246. {
  1247. u16 data;
  1248. struct phy_device *phydev = adapter->netdev->phydev;
  1249. et131x_mii_read(adapter, MII_BMCR, &data);
  1250. data &= ~BMCR_PDOWN;
  1251. if (down)
  1252. data |= BMCR_PDOWN;
  1253. et131x_mii_write(adapter, phydev->mdio.addr, MII_BMCR, data);
  1254. }
  1255. /* et131x_xcvr_init - Init the phy if we are setting it into force mode */
  1256. static void et131x_xcvr_init(struct et131x_adapter *adapter)
  1257. {
  1258. u16 lcr2;
  1259. struct phy_device *phydev = adapter->netdev->phydev;
  1260. /* Set the LED behavior such that LED 1 indicates speed (off =
  1261. * 10Mbits, blink = 100Mbits, on = 1000Mbits) and LED 2 indicates
  1262. * link and activity (on for link, blink off for activity).
  1263. *
  1264. * NOTE: Some customizations have been added here for specific
  1265. * vendors; The LED behavior is now determined by vendor data in the
  1266. * EEPROM. However, the above description is the default.
  1267. */
  1268. if ((adapter->eeprom_data[1] & 0x4) == 0) {
  1269. et131x_mii_read(adapter, PHY_LED_2, &lcr2);
  1270. lcr2 &= (ET_LED2_LED_100TX | ET_LED2_LED_1000T);
  1271. lcr2 |= (LED_VAL_LINKON_ACTIVE << LED_LINK_SHIFT);
  1272. if ((adapter->eeprom_data[1] & 0x8) == 0)
  1273. lcr2 |= (LED_VAL_1000BT_100BTX << LED_TXRX_SHIFT);
  1274. else
  1275. lcr2 |= (LED_VAL_LINKON << LED_TXRX_SHIFT);
  1276. et131x_mii_write(adapter, phydev->mdio.addr, PHY_LED_2, lcr2);
  1277. }
  1278. }
  1279. /* et131x_configure_global_regs - configure JAGCore global regs */
  1280. static void et131x_configure_global_regs(struct et131x_adapter *adapter)
  1281. {
  1282. struct global_regs __iomem *regs = &adapter->regs->global;
  1283. writel(0, &regs->rxq_start_addr);
  1284. writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
  1285. if (adapter->registry_jumbo_packet < 2048) {
  1286. /* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
  1287. * block of RAM that the driver can split between Tx
  1288. * and Rx as it desires. Our default is to split it
  1289. * 50/50:
  1290. */
  1291. writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
  1292. writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
  1293. } else if (adapter->registry_jumbo_packet < 8192) {
  1294. /* For jumbo packets > 2k but < 8k, split 50-50. */
  1295. writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
  1296. writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
  1297. } else {
  1298. /* 9216 is the only packet size greater than 8k that
  1299. * is available. The Tx buffer has to be big enough
  1300. * for one whole packet on the Tx side. We'll make
  1301. * the Tx 9408, and give the rest to Rx
  1302. */
  1303. writel(0x01b3, &regs->rxq_end_addr);
  1304. writel(0x01b4, &regs->txq_start_addr);
  1305. }
  1306. /* Initialize the loopback register. Disable all loopbacks. */
  1307. writel(0, &regs->loopback);
  1308. writel(0, &regs->msi_config);
  1309. /* By default, disable the watchdog timer. It will be enabled when
  1310. * a packet is queued.
  1311. */
  1312. writel(0, &regs->watchdog_timer);
  1313. }
  1314. /* et131x_config_rx_dma_regs - Start of Rx_DMA init sequence */
  1315. static void et131x_config_rx_dma_regs(struct et131x_adapter *adapter)
  1316. {
  1317. struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
  1318. struct rx_ring *rx_local = &adapter->rx_ring;
  1319. struct fbr_desc *fbr_entry;
  1320. u32 entry;
  1321. u32 psr_num_des;
  1322. unsigned long flags;
  1323. u8 id;
  1324. et131x_rx_dma_disable(adapter);
  1325. /* Load the completion writeback physical address */
  1326. writel(upper_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_hi);
  1327. writel(lower_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_lo);
  1328. memset(rx_local->rx_status_block, 0, sizeof(struct rx_status_block));
  1329. /* Set the address and parameters of the packet status ring */
  1330. writel(upper_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_hi);
  1331. writel(lower_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_lo);
  1332. writel(rx_local->psr_entries - 1, &rx_dma->psr_num_des);
  1333. writel(0, &rx_dma->psr_full_offset);
  1334. psr_num_des = readl(&rx_dma->psr_num_des) & ET_RXDMA_PSR_NUM_DES_MASK;
  1335. writel((psr_num_des * LO_MARK_PERCENT_FOR_PSR) / 100,
  1336. &rx_dma->psr_min_des);
  1337. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1338. /* These local variables track the PSR in the adapter structure */
  1339. rx_local->local_psr_full = 0;
  1340. for (id = 0; id < NUM_FBRS; id++) {
  1341. u32 __iomem *num_des;
  1342. u32 __iomem *full_offset;
  1343. u32 __iomem *min_des;
  1344. u32 __iomem *base_hi;
  1345. u32 __iomem *base_lo;
  1346. struct fbr_lookup *fbr = rx_local->fbr[id];
  1347. if (id == 0) {
  1348. num_des = &rx_dma->fbr0_num_des;
  1349. full_offset = &rx_dma->fbr0_full_offset;
  1350. min_des = &rx_dma->fbr0_min_des;
  1351. base_hi = &rx_dma->fbr0_base_hi;
  1352. base_lo = &rx_dma->fbr0_base_lo;
  1353. } else {
  1354. num_des = &rx_dma->fbr1_num_des;
  1355. full_offset = &rx_dma->fbr1_full_offset;
  1356. min_des = &rx_dma->fbr1_min_des;
  1357. base_hi = &rx_dma->fbr1_base_hi;
  1358. base_lo = &rx_dma->fbr1_base_lo;
  1359. }
  1360. /* Now's the best time to initialize FBR contents */
  1361. fbr_entry = fbr->ring_virtaddr;
  1362. for (entry = 0; entry < fbr->num_entries; entry++) {
  1363. fbr_entry->addr_hi = fbr->bus_high[entry];
  1364. fbr_entry->addr_lo = fbr->bus_low[entry];
  1365. fbr_entry->word2 = entry;
  1366. fbr_entry++;
  1367. }
  1368. /* Set the address and parameters of Free buffer ring 1 and 0 */
  1369. writel(upper_32_bits(fbr->ring_physaddr), base_hi);
  1370. writel(lower_32_bits(fbr->ring_physaddr), base_lo);
  1371. writel(fbr->num_entries - 1, num_des);
  1372. writel(ET_DMA10_WRAP, full_offset);
  1373. /* This variable tracks the free buffer ring 1 full position,
  1374. * so it has to match the above.
  1375. */
  1376. fbr->local_full = ET_DMA10_WRAP;
  1377. writel(((fbr->num_entries * LO_MARK_PERCENT_FOR_RX) / 100) - 1,
  1378. min_des);
  1379. }
  1380. /* Program the number of packets we will receive before generating an
  1381. * interrupt.
  1382. * For version B silicon, this value gets updated once autoneg is
  1383. *complete.
  1384. */
  1385. writel(PARM_RX_NUM_BUFS_DEF, &rx_dma->num_pkt_done);
  1386. /* The "time_done" is not working correctly to coalesce interrupts
  1387. * after a given time period, but rather is giving us an interrupt
  1388. * regardless of whether we have received packets.
  1389. * This value gets updated once autoneg is complete.
  1390. */
  1391. writel(PARM_RX_TIME_INT_DEF, &rx_dma->max_pkt_time);
  1392. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1393. }
  1394. /* et131x_config_tx_dma_regs - Set up the tx dma section of the JAGCore.
  1395. *
  1396. * Configure the transmit engine with the ring buffers we have created
  1397. * and prepare it for use.
  1398. */
  1399. static void et131x_config_tx_dma_regs(struct et131x_adapter *adapter)
  1400. {
  1401. struct txdma_regs __iomem *txdma = &adapter->regs->txdma;
  1402. struct tx_ring *tx_ring = &adapter->tx_ring;
  1403. /* Load the hardware with the start of the transmit descriptor ring. */
  1404. writel(upper_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_hi);
  1405. writel(lower_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_lo);
  1406. /* Initialise the transmit DMA engine */
  1407. writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des);
  1408. /* Load the completion writeback physical address */
  1409. writel(upper_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_hi);
  1410. writel(lower_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_lo);
  1411. *tx_ring->tx_status = 0;
  1412. writel(0, &txdma->service_request);
  1413. tx_ring->send_idx = 0;
  1414. }
  1415. /* et131x_adapter_setup - Set the adapter up as per cassini+ documentation */
  1416. static void et131x_adapter_setup(struct et131x_adapter *adapter)
  1417. {
  1418. et131x_configure_global_regs(adapter);
  1419. et1310_config_mac_regs1(adapter);
  1420. /* Configure the MMC registers */
  1421. /* All we need to do is initialize the Memory Control Register */
  1422. writel(ET_MMC_ENABLE, &adapter->regs->mmc.mmc_ctrl);
  1423. et1310_config_rxmac_regs(adapter);
  1424. et1310_config_txmac_regs(adapter);
  1425. et131x_config_rx_dma_regs(adapter);
  1426. et131x_config_tx_dma_regs(adapter);
  1427. et1310_config_macstat_regs(adapter);
  1428. et1310_phy_power_switch(adapter, 0);
  1429. et131x_xcvr_init(adapter);
  1430. }
  1431. /* et131x_soft_reset - Issue soft reset to the hardware, complete for ET1310 */
  1432. static void et131x_soft_reset(struct et131x_adapter *adapter)
  1433. {
  1434. u32 reg;
  1435. /* Disable MAC Core */
  1436. reg = ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET |
  1437. ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
  1438. ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
  1439. writel(reg, &adapter->regs->mac.cfg1);
  1440. reg = ET_RESET_ALL;
  1441. writel(reg, &adapter->regs->global.sw_reset);
  1442. reg = ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
  1443. ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
  1444. writel(reg, &adapter->regs->mac.cfg1);
  1445. writel(0, &adapter->regs->mac.cfg1);
  1446. }
  1447. static void et131x_enable_interrupts(struct et131x_adapter *adapter)
  1448. {
  1449. u32 mask;
  1450. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
  1451. mask = INT_MASK_ENABLE;
  1452. else
  1453. mask = INT_MASK_ENABLE_NO_FLOW;
  1454. writel(mask, &adapter->regs->global.int_mask);
  1455. }
  1456. static void et131x_disable_interrupts(struct et131x_adapter *adapter)
  1457. {
  1458. writel(INT_MASK_DISABLE, &adapter->regs->global.int_mask);
  1459. }
  1460. static void et131x_tx_dma_disable(struct et131x_adapter *adapter)
  1461. {
  1462. /* Setup the transmit dma configuration register */
  1463. writel(ET_TXDMA_CSR_HALT | ET_TXDMA_SNGL_EPKT,
  1464. &adapter->regs->txdma.csr);
  1465. }
  1466. static void et131x_enable_txrx(struct net_device *netdev)
  1467. {
  1468. struct et131x_adapter *adapter = netdev_priv(netdev);
  1469. et131x_rx_dma_enable(adapter);
  1470. et131x_tx_dma_enable(adapter);
  1471. if (adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE)
  1472. et131x_enable_interrupts(adapter);
  1473. netif_start_queue(netdev);
  1474. }
  1475. static void et131x_disable_txrx(struct net_device *netdev)
  1476. {
  1477. struct et131x_adapter *adapter = netdev_priv(netdev);
  1478. netif_stop_queue(netdev);
  1479. et131x_rx_dma_disable(adapter);
  1480. et131x_tx_dma_disable(adapter);
  1481. et131x_disable_interrupts(adapter);
  1482. }
  1483. static void et131x_init_send(struct et131x_adapter *adapter)
  1484. {
  1485. int i;
  1486. struct tx_ring *tx_ring = &adapter->tx_ring;
  1487. struct tcb *tcb = tx_ring->tcb_ring;
  1488. tx_ring->tcb_qhead = tcb;
  1489. memset(tcb, 0, sizeof(struct tcb) * NUM_TCB);
  1490. for (i = 0; i < NUM_TCB; i++) {
  1491. tcb->next = tcb + 1;
  1492. tcb++;
  1493. }
  1494. tcb--;
  1495. tx_ring->tcb_qtail = tcb;
  1496. tcb->next = NULL;
  1497. /* Curr send queue should now be empty */
  1498. tx_ring->send_head = NULL;
  1499. tx_ring->send_tail = NULL;
  1500. }
  1501. /* et1310_enable_phy_coma
  1502. *
  1503. * driver receive an phy status change interrupt while in D0 and check that
  1504. * phy_status is down.
  1505. *
  1506. * -- gate off JAGCore;
  1507. * -- set gigE PHY in Coma mode
  1508. * -- wake on phy_interrupt; Perform software reset JAGCore,
  1509. * re-initialize jagcore and gigE PHY
  1510. */
  1511. static void et1310_enable_phy_coma(struct et131x_adapter *adapter)
  1512. {
  1513. u32 pmcsr = readl(&adapter->regs->global.pm_csr);
  1514. /* Stop sending packets. */
  1515. adapter->flags |= FMP_ADAPTER_LOWER_POWER;
  1516. /* Wait for outstanding Receive packets */
  1517. et131x_disable_txrx(adapter->netdev);
  1518. /* Gate off JAGCore 3 clock domains */
  1519. pmcsr &= ~ET_PMCSR_INIT;
  1520. writel(pmcsr, &adapter->regs->global.pm_csr);
  1521. /* Program gigE PHY in to Coma mode */
  1522. pmcsr |= ET_PM_PHY_SW_COMA;
  1523. writel(pmcsr, &adapter->regs->global.pm_csr);
  1524. }
  1525. static void et1310_disable_phy_coma(struct et131x_adapter *adapter)
  1526. {
  1527. u32 pmcsr;
  1528. pmcsr = readl(&adapter->regs->global.pm_csr);
  1529. /* Disable phy_sw_coma register and re-enable JAGCore clocks */
  1530. pmcsr |= ET_PMCSR_INIT;
  1531. pmcsr &= ~ET_PM_PHY_SW_COMA;
  1532. writel(pmcsr, &adapter->regs->global.pm_csr);
  1533. /* Restore the GbE PHY speed and duplex modes;
  1534. * Reset JAGCore; re-configure and initialize JAGCore and gigE PHY
  1535. */
  1536. /* Re-initialize the send structures */
  1537. et131x_init_send(adapter);
  1538. /* Bring the device back to the state it was during init prior to
  1539. * autonegotiation being complete. This way, when we get the auto-neg
  1540. * complete interrupt, we can complete init by calling ConfigMacREGS2.
  1541. */
  1542. et131x_soft_reset(adapter);
  1543. et131x_adapter_setup(adapter);
  1544. /* Allow Tx to restart */
  1545. adapter->flags &= ~FMP_ADAPTER_LOWER_POWER;
  1546. et131x_enable_txrx(adapter->netdev);
  1547. }
  1548. static inline u32 bump_free_buff_ring(u32 *free_buff_ring, u32 limit)
  1549. {
  1550. u32 tmp_free_buff_ring = *free_buff_ring;
  1551. tmp_free_buff_ring++;
  1552. /* This works for all cases where limit < 1024. The 1023 case
  1553. * works because 1023++ is 1024 which means the if condition is not
  1554. * taken but the carry of the bit into the wrap bit toggles the wrap
  1555. * value correctly
  1556. */
  1557. if ((tmp_free_buff_ring & ET_DMA10_MASK) > limit) {
  1558. tmp_free_buff_ring &= ~ET_DMA10_MASK;
  1559. tmp_free_buff_ring ^= ET_DMA10_WRAP;
  1560. }
  1561. /* For the 1023 case */
  1562. tmp_free_buff_ring &= (ET_DMA10_MASK | ET_DMA10_WRAP);
  1563. *free_buff_ring = tmp_free_buff_ring;
  1564. return tmp_free_buff_ring;
  1565. }
  1566. /* et131x_rx_dma_memory_alloc
  1567. *
  1568. * Allocates Free buffer ring 1 for sure, free buffer ring 0 if required,
  1569. * and the Packet Status Ring.
  1570. */
  1571. static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
  1572. {
  1573. u8 id;
  1574. u32 i, j;
  1575. u32 bufsize;
  1576. u32 psr_size;
  1577. u32 fbr_chunksize;
  1578. struct rx_ring *rx_ring = &adapter->rx_ring;
  1579. struct fbr_lookup *fbr;
  1580. /* Alloc memory for the lookup table */
  1581. rx_ring->fbr[0] = kzalloc(sizeof(*fbr), GFP_KERNEL);
  1582. if (rx_ring->fbr[0] == NULL)
  1583. return -ENOMEM;
  1584. rx_ring->fbr[1] = kzalloc(sizeof(*fbr), GFP_KERNEL);
  1585. if (rx_ring->fbr[1] == NULL)
  1586. return -ENOMEM;
  1587. /* The first thing we will do is configure the sizes of the buffer
  1588. * rings. These will change based on jumbo packet support. Larger
  1589. * jumbo packets increases the size of each entry in FBR0, and the
  1590. * number of entries in FBR0, while at the same time decreasing the
  1591. * number of entries in FBR1.
  1592. *
  1593. * FBR1 holds "large" frames, FBR0 holds "small" frames. If FBR1
  1594. * entries are huge in order to accommodate a "jumbo" frame, then it
  1595. * will have less entries. Conversely, FBR1 will now be relied upon
  1596. * to carry more "normal" frames, thus it's entry size also increases
  1597. * and the number of entries goes up too (since it now carries
  1598. * "small" + "regular" packets.
  1599. *
  1600. * In this scheme, we try to maintain 512 entries between the two
  1601. * rings. Also, FBR1 remains a constant size - when it's size doubles
  1602. * the number of entries halves. FBR0 increases in size, however.
  1603. */
  1604. if (adapter->registry_jumbo_packet < 2048) {
  1605. rx_ring->fbr[0]->buffsize = 256;
  1606. rx_ring->fbr[0]->num_entries = 512;
  1607. rx_ring->fbr[1]->buffsize = 2048;
  1608. rx_ring->fbr[1]->num_entries = 512;
  1609. } else if (adapter->registry_jumbo_packet < 4096) {
  1610. rx_ring->fbr[0]->buffsize = 512;
  1611. rx_ring->fbr[0]->num_entries = 1024;
  1612. rx_ring->fbr[1]->buffsize = 4096;
  1613. rx_ring->fbr[1]->num_entries = 512;
  1614. } else {
  1615. rx_ring->fbr[0]->buffsize = 1024;
  1616. rx_ring->fbr[0]->num_entries = 768;
  1617. rx_ring->fbr[1]->buffsize = 16384;
  1618. rx_ring->fbr[1]->num_entries = 128;
  1619. }
  1620. rx_ring->psr_entries = rx_ring->fbr[0]->num_entries +
  1621. rx_ring->fbr[1]->num_entries;
  1622. for (id = 0; id < NUM_FBRS; id++) {
  1623. fbr = rx_ring->fbr[id];
  1624. /* Allocate an area of memory for Free Buffer Ring */
  1625. bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
  1626. fbr->ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
  1627. bufsize,
  1628. &fbr->ring_physaddr,
  1629. GFP_KERNEL);
  1630. if (!fbr->ring_virtaddr) {
  1631. dev_err(&adapter->pdev->dev,
  1632. "Cannot alloc memory for Free Buffer Ring %d\n",
  1633. id);
  1634. return -ENOMEM;
  1635. }
  1636. }
  1637. for (id = 0; id < NUM_FBRS; id++) {
  1638. fbr = rx_ring->fbr[id];
  1639. fbr_chunksize = (FBR_CHUNKS * fbr->buffsize);
  1640. for (i = 0; i < fbr->num_entries / FBR_CHUNKS; i++) {
  1641. dma_addr_t fbr_physaddr;
  1642. fbr->mem_virtaddrs[i] = dma_alloc_coherent(
  1643. &adapter->pdev->dev, fbr_chunksize,
  1644. &fbr->mem_physaddrs[i],
  1645. GFP_KERNEL);
  1646. if (!fbr->mem_virtaddrs[i]) {
  1647. dev_err(&adapter->pdev->dev,
  1648. "Could not alloc memory\n");
  1649. return -ENOMEM;
  1650. }
  1651. /* See NOTE in "Save Physical Address" comment above */
  1652. fbr_physaddr = fbr->mem_physaddrs[i];
  1653. for (j = 0; j < FBR_CHUNKS; j++) {
  1654. u32 k = (i * FBR_CHUNKS) + j;
  1655. /* Save the Virtual address of this index for
  1656. * quick access later
  1657. */
  1658. fbr->virt[k] = (u8 *)fbr->mem_virtaddrs[i] +
  1659. (j * fbr->buffsize);
  1660. /* now store the physical address in the
  1661. * descriptor so the device can access it
  1662. */
  1663. fbr->bus_high[k] = upper_32_bits(fbr_physaddr);
  1664. fbr->bus_low[k] = lower_32_bits(fbr_physaddr);
  1665. fbr_physaddr += fbr->buffsize;
  1666. }
  1667. }
  1668. }
  1669. /* Allocate an area of memory for FIFO of Packet Status ring entries */
  1670. psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
  1671. rx_ring->ps_ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
  1672. psr_size,
  1673. &rx_ring->ps_ring_physaddr,
  1674. GFP_KERNEL);
  1675. if (!rx_ring->ps_ring_virtaddr) {
  1676. dev_err(&adapter->pdev->dev,
  1677. "Cannot alloc memory for Packet Status Ring\n");
  1678. return -ENOMEM;
  1679. }
  1680. /* Allocate an area of memory for writeback of status information */
  1681. rx_ring->rx_status_block = dma_alloc_coherent(&adapter->pdev->dev,
  1682. sizeof(struct rx_status_block),
  1683. &rx_ring->rx_status_bus,
  1684. GFP_KERNEL);
  1685. if (!rx_ring->rx_status_block) {
  1686. dev_err(&adapter->pdev->dev,
  1687. "Cannot alloc memory for Status Block\n");
  1688. return -ENOMEM;
  1689. }
  1690. rx_ring->num_rfd = NIC_DEFAULT_NUM_RFD;
  1691. /* The RFDs are going to be put on lists later on, so initialize the
  1692. * lists now.
  1693. */
  1694. INIT_LIST_HEAD(&rx_ring->recv_list);
  1695. return 0;
  1696. }
  1697. static void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
  1698. {
  1699. u8 id;
  1700. u32 ii;
  1701. u32 bufsize;
  1702. u32 psr_size;
  1703. struct rfd *rfd;
  1704. struct rx_ring *rx_ring = &adapter->rx_ring;
  1705. struct fbr_lookup *fbr;
  1706. /* Free RFDs and associated packet descriptors */
  1707. WARN_ON(rx_ring->num_ready_recv != rx_ring->num_rfd);
  1708. while (!list_empty(&rx_ring->recv_list)) {
  1709. rfd = list_entry(rx_ring->recv_list.next,
  1710. struct rfd, list_node);
  1711. list_del(&rfd->list_node);
  1712. rfd->skb = NULL;
  1713. kfree(rfd);
  1714. }
  1715. /* Free Free Buffer Rings */
  1716. for (id = 0; id < NUM_FBRS; id++) {
  1717. fbr = rx_ring->fbr[id];
  1718. if (!fbr || !fbr->ring_virtaddr)
  1719. continue;
  1720. /* First the packet memory */
  1721. for (ii = 0; ii < fbr->num_entries / FBR_CHUNKS; ii++) {
  1722. if (fbr->mem_virtaddrs[ii]) {
  1723. bufsize = fbr->buffsize * FBR_CHUNKS;
  1724. dma_free_coherent(&adapter->pdev->dev,
  1725. bufsize,
  1726. fbr->mem_virtaddrs[ii],
  1727. fbr->mem_physaddrs[ii]);
  1728. fbr->mem_virtaddrs[ii] = NULL;
  1729. }
  1730. }
  1731. bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
  1732. dma_free_coherent(&adapter->pdev->dev,
  1733. bufsize,
  1734. fbr->ring_virtaddr,
  1735. fbr->ring_physaddr);
  1736. fbr->ring_virtaddr = NULL;
  1737. }
  1738. /* Free Packet Status Ring */
  1739. if (rx_ring->ps_ring_virtaddr) {
  1740. psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
  1741. dma_free_coherent(&adapter->pdev->dev, psr_size,
  1742. rx_ring->ps_ring_virtaddr,
  1743. rx_ring->ps_ring_physaddr);
  1744. rx_ring->ps_ring_virtaddr = NULL;
  1745. }
  1746. /* Free area of memory for the writeback of status information */
  1747. if (rx_ring->rx_status_block) {
  1748. dma_free_coherent(&adapter->pdev->dev,
  1749. sizeof(struct rx_status_block),
  1750. rx_ring->rx_status_block,
  1751. rx_ring->rx_status_bus);
  1752. rx_ring->rx_status_block = NULL;
  1753. }
  1754. /* Free the FBR Lookup Table */
  1755. kfree(rx_ring->fbr[0]);
  1756. kfree(rx_ring->fbr[1]);
  1757. /* Reset Counters */
  1758. rx_ring->num_ready_recv = 0;
  1759. }
  1760. /* et131x_init_recv - Initialize receive data structures */
  1761. static int et131x_init_recv(struct et131x_adapter *adapter)
  1762. {
  1763. struct rfd *rfd;
  1764. u32 rfdct;
  1765. struct rx_ring *rx_ring = &adapter->rx_ring;
  1766. /* Setup each RFD */
  1767. for (rfdct = 0; rfdct < rx_ring->num_rfd; rfdct++) {
  1768. rfd = kzalloc(sizeof(*rfd), GFP_ATOMIC | GFP_DMA);
  1769. if (!rfd)
  1770. return -ENOMEM;
  1771. rfd->skb = NULL;
  1772. /* Add this RFD to the recv_list */
  1773. list_add_tail(&rfd->list_node, &rx_ring->recv_list);
  1774. /* Increment the available RFD's */
  1775. rx_ring->num_ready_recv++;
  1776. }
  1777. return 0;
  1778. }
  1779. /* et131x_set_rx_dma_timer - Set the heartbeat timer according to line rate */
  1780. static void et131x_set_rx_dma_timer(struct et131x_adapter *adapter)
  1781. {
  1782. struct phy_device *phydev = adapter->netdev->phydev;
  1783. /* For version B silicon, we do not use the RxDMA timer for 10 and 100
  1784. * Mbits/s line rates. We do not enable and RxDMA interrupt coalescing.
  1785. */
  1786. if ((phydev->speed == SPEED_100) || (phydev->speed == SPEED_10)) {
  1787. writel(0, &adapter->regs->rxdma.max_pkt_time);
  1788. writel(1, &adapter->regs->rxdma.num_pkt_done);
  1789. }
  1790. }
  1791. /* nic_return_rfd - Recycle a RFD and put it back onto the receive list */
  1792. static void nic_return_rfd(struct et131x_adapter *adapter, struct rfd *rfd)
  1793. {
  1794. struct rx_ring *rx_local = &adapter->rx_ring;
  1795. struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
  1796. u16 buff_index = rfd->bufferindex;
  1797. u8 ring_index = rfd->ringindex;
  1798. unsigned long flags;
  1799. struct fbr_lookup *fbr = rx_local->fbr[ring_index];
  1800. /* We don't use any of the OOB data besides status. Otherwise, we
  1801. * need to clean up OOB data
  1802. */
  1803. if (buff_index < fbr->num_entries) {
  1804. u32 free_buff_ring;
  1805. u32 __iomem *offset;
  1806. struct fbr_desc *next;
  1807. if (ring_index == 0)
  1808. offset = &rx_dma->fbr0_full_offset;
  1809. else
  1810. offset = &rx_dma->fbr1_full_offset;
  1811. next = (struct fbr_desc *)(fbr->ring_virtaddr) +
  1812. INDEX10(fbr->local_full);
  1813. /* Handle the Free Buffer Ring advancement here. Write
  1814. * the PA / Buffer Index for the returned buffer into
  1815. * the oldest (next to be freed)FBR entry
  1816. */
  1817. next->addr_hi = fbr->bus_high[buff_index];
  1818. next->addr_lo = fbr->bus_low[buff_index];
  1819. next->word2 = buff_index;
  1820. free_buff_ring = bump_free_buff_ring(&fbr->local_full,
  1821. fbr->num_entries - 1);
  1822. writel(free_buff_ring, offset);
  1823. } else {
  1824. dev_err(&adapter->pdev->dev,
  1825. "%s illegal Buffer Index returned\n", __func__);
  1826. }
  1827. /* The processing on this RFD is done, so put it back on the tail of
  1828. * our list
  1829. */
  1830. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1831. list_add_tail(&rfd->list_node, &rx_local->recv_list);
  1832. rx_local->num_ready_recv++;
  1833. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1834. WARN_ON(rx_local->num_ready_recv > rx_local->num_rfd);
  1835. }
  1836. /* nic_rx_pkts - Checks the hardware for available packets
  1837. *
  1838. * Checks the hardware for available packets, using completion ring
  1839. * If packets are available, it gets an RFD from the recv_list, attaches
  1840. * the packet to it, puts the RFD in the RecvPendList, and also returns
  1841. * the pointer to the RFD.
  1842. */
  1843. static struct rfd *nic_rx_pkts(struct et131x_adapter *adapter)
  1844. {
  1845. struct rx_ring *rx_local = &adapter->rx_ring;
  1846. struct rx_status_block *status;
  1847. struct pkt_stat_desc *psr;
  1848. struct rfd *rfd;
  1849. unsigned long flags;
  1850. struct list_head *element;
  1851. u8 ring_index;
  1852. u16 buff_index;
  1853. u32 len;
  1854. u32 word0;
  1855. u32 word1;
  1856. struct sk_buff *skb;
  1857. struct fbr_lookup *fbr;
  1858. /* RX Status block is written by the DMA engine prior to every
  1859. * interrupt. It contains the next to be used entry in the Packet
  1860. * Status Ring, and also the two Free Buffer rings.
  1861. */
  1862. status = rx_local->rx_status_block;
  1863. word1 = status->word1 >> 16;
  1864. /* Check the PSR and wrap bits do not match */
  1865. if ((word1 & 0x1FFF) == (rx_local->local_psr_full & 0x1FFF))
  1866. return NULL; /* Looks like this ring is not updated yet */
  1867. /* The packet status ring indicates that data is available. */
  1868. psr = (struct pkt_stat_desc *)(rx_local->ps_ring_virtaddr) +
  1869. (rx_local->local_psr_full & 0xFFF);
  1870. /* Grab any information that is required once the PSR is advanced,
  1871. * since we can no longer rely on the memory being accurate
  1872. */
  1873. len = psr->word1 & 0xFFFF;
  1874. ring_index = (psr->word1 >> 26) & 0x03;
  1875. fbr = rx_local->fbr[ring_index];
  1876. buff_index = (psr->word1 >> 16) & 0x3FF;
  1877. word0 = psr->word0;
  1878. /* Indicate that we have used this PSR entry. */
  1879. /* FIXME wrap 12 */
  1880. add_12bit(&rx_local->local_psr_full, 1);
  1881. if ((rx_local->local_psr_full & 0xFFF) > rx_local->psr_entries - 1) {
  1882. /* Clear psr full and toggle the wrap bit */
  1883. rx_local->local_psr_full &= ~0xFFF;
  1884. rx_local->local_psr_full ^= 0x1000;
  1885. }
  1886. writel(rx_local->local_psr_full, &adapter->regs->rxdma.psr_full_offset);
  1887. if (ring_index > 1 || buff_index > fbr->num_entries - 1) {
  1888. /* Illegal buffer or ring index cannot be used by S/W*/
  1889. dev_err(&adapter->pdev->dev,
  1890. "NICRxPkts PSR Entry %d indicates length of %d and/or bad bi(%d)\n",
  1891. rx_local->local_psr_full & 0xFFF, len, buff_index);
  1892. return NULL;
  1893. }
  1894. /* Get and fill the RFD. */
  1895. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1896. element = rx_local->recv_list.next;
  1897. rfd = list_entry(element, struct rfd, list_node);
  1898. if (!rfd) {
  1899. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1900. return NULL;
  1901. }
  1902. list_del(&rfd->list_node);
  1903. rx_local->num_ready_recv--;
  1904. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1905. rfd->bufferindex = buff_index;
  1906. rfd->ringindex = ring_index;
  1907. /* In V1 silicon, there is a bug which screws up filtering of runt
  1908. * packets. Therefore runt packet filtering is disabled in the MAC and
  1909. * the packets are dropped here. They are also counted here.
  1910. */
  1911. if (len < (NIC_MIN_PACKET_SIZE + 4)) {
  1912. adapter->stats.rx_other_errs++;
  1913. rfd->len = 0;
  1914. goto out;
  1915. }
  1916. if ((word0 & ALCATEL_MULTICAST_PKT) && !(word0 & ALCATEL_BROADCAST_PKT))
  1917. adapter->stats.multicast_pkts_rcvd++;
  1918. rfd->len = len;
  1919. skb = dev_alloc_skb(rfd->len + 2);
  1920. if (!skb)
  1921. return NULL;
  1922. adapter->netdev->stats.rx_bytes += rfd->len;
  1923. skb_put_data(skb, fbr->virt[buff_index], rfd->len);
  1924. skb->protocol = eth_type_trans(skb, adapter->netdev);
  1925. skb->ip_summed = CHECKSUM_NONE;
  1926. netif_receive_skb(skb);
  1927. out:
  1928. nic_return_rfd(adapter, rfd);
  1929. return rfd;
  1930. }
  1931. static int et131x_handle_recv_pkts(struct et131x_adapter *adapter, int budget)
  1932. {
  1933. struct rfd *rfd = NULL;
  1934. int count = 0;
  1935. int limit = budget;
  1936. bool done = true;
  1937. struct rx_ring *rx_ring = &adapter->rx_ring;
  1938. if (budget > MAX_PACKETS_HANDLED)
  1939. limit = MAX_PACKETS_HANDLED;
  1940. /* Process up to available RFD's */
  1941. while (count < limit) {
  1942. if (list_empty(&rx_ring->recv_list)) {
  1943. WARN_ON(rx_ring->num_ready_recv != 0);
  1944. done = false;
  1945. break;
  1946. }
  1947. rfd = nic_rx_pkts(adapter);
  1948. if (rfd == NULL)
  1949. break;
  1950. /* Do not receive any packets until a filter has been set.
  1951. * Do not receive any packets until we have link.
  1952. * If length is zero, return the RFD in order to advance the
  1953. * Free buffer ring.
  1954. */
  1955. if (!adapter->packet_filter ||
  1956. !netif_carrier_ok(adapter->netdev) ||
  1957. rfd->len == 0)
  1958. continue;
  1959. adapter->netdev->stats.rx_packets++;
  1960. if (rx_ring->num_ready_recv < RFD_LOW_WATER_MARK)
  1961. dev_warn(&adapter->pdev->dev, "RFD's are running out\n");
  1962. count++;
  1963. }
  1964. if (count == limit || !done) {
  1965. rx_ring->unfinished_receives = true;
  1966. writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
  1967. &adapter->regs->global.watchdog_timer);
  1968. } else {
  1969. /* Watchdog timer will disable itself if appropriate. */
  1970. rx_ring->unfinished_receives = false;
  1971. }
  1972. return count;
  1973. }
  1974. /* et131x_tx_dma_memory_alloc
  1975. *
  1976. * Allocates memory that will be visible both to the device and to the CPU.
  1977. * The OS will pass us packets, pointers to which we will insert in the Tx
  1978. * Descriptor queue. The device will read this queue to find the packets in
  1979. * memory. The device will update the "status" in memory each time it xmits a
  1980. * packet.
  1981. */
  1982. static int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
  1983. {
  1984. int desc_size = 0;
  1985. struct tx_ring *tx_ring = &adapter->tx_ring;
  1986. /* Allocate memory for the TCB's (Transmit Control Block) */
  1987. tx_ring->tcb_ring = kcalloc(NUM_TCB, sizeof(struct tcb),
  1988. GFP_ATOMIC | GFP_DMA);
  1989. if (!tx_ring->tcb_ring)
  1990. return -ENOMEM;
  1991. desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
  1992. tx_ring->tx_desc_ring = dma_alloc_coherent(&adapter->pdev->dev,
  1993. desc_size,
  1994. &tx_ring->tx_desc_ring_pa,
  1995. GFP_KERNEL);
  1996. if (!tx_ring->tx_desc_ring) {
  1997. dev_err(&adapter->pdev->dev,
  1998. "Cannot alloc memory for Tx Ring\n");
  1999. return -ENOMEM;
  2000. }
  2001. tx_ring->tx_status = dma_alloc_coherent(&adapter->pdev->dev,
  2002. sizeof(u32),
  2003. &tx_ring->tx_status_pa,
  2004. GFP_KERNEL);
  2005. if (!tx_ring->tx_status) {
  2006. dev_err(&adapter->pdev->dev,
  2007. "Cannot alloc memory for Tx status block\n");
  2008. return -ENOMEM;
  2009. }
  2010. return 0;
  2011. }
  2012. static void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
  2013. {
  2014. int desc_size = 0;
  2015. struct tx_ring *tx_ring = &adapter->tx_ring;
  2016. if (tx_ring->tx_desc_ring) {
  2017. /* Free memory relating to Tx rings here */
  2018. desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
  2019. dma_free_coherent(&adapter->pdev->dev,
  2020. desc_size,
  2021. tx_ring->tx_desc_ring,
  2022. tx_ring->tx_desc_ring_pa);
  2023. tx_ring->tx_desc_ring = NULL;
  2024. }
  2025. /* Free memory for the Tx status block */
  2026. if (tx_ring->tx_status) {
  2027. dma_free_coherent(&adapter->pdev->dev,
  2028. sizeof(u32),
  2029. tx_ring->tx_status,
  2030. tx_ring->tx_status_pa);
  2031. tx_ring->tx_status = NULL;
  2032. }
  2033. /* Free the memory for the tcb structures */
  2034. kfree(tx_ring->tcb_ring);
  2035. }
  2036. /* nic_send_packet - NIC specific send handler for version B silicon. */
  2037. static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
  2038. {
  2039. u32 i;
  2040. struct tx_desc desc[24];
  2041. u32 frag = 0;
  2042. u32 thiscopy, remainder;
  2043. struct sk_buff *skb = tcb->skb;
  2044. u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
  2045. struct skb_frag_struct *frags = &skb_shinfo(skb)->frags[0];
  2046. struct phy_device *phydev = adapter->netdev->phydev;
  2047. dma_addr_t dma_addr;
  2048. struct tx_ring *tx_ring = &adapter->tx_ring;
  2049. /* Part of the optimizations of this send routine restrict us to
  2050. * sending 24 fragments at a pass. In practice we should never see
  2051. * more than 5 fragments.
  2052. */
  2053. /* nr_frags should be no more than 18. */
  2054. BUILD_BUG_ON(MAX_SKB_FRAGS + 1 > 23);
  2055. memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
  2056. for (i = 0; i < nr_frags; i++) {
  2057. /* If there is something in this element, lets get a
  2058. * descriptor from the ring and get the necessary data
  2059. */
  2060. if (i == 0) {
  2061. /* If the fragments are smaller than a standard MTU,
  2062. * then map them to a single descriptor in the Tx
  2063. * Desc ring. However, if they're larger, as is
  2064. * possible with support for jumbo packets, then
  2065. * split them each across 2 descriptors.
  2066. *
  2067. * This will work until we determine why the hardware
  2068. * doesn't seem to like large fragments.
  2069. */
  2070. if (skb_headlen(skb) <= 1514) {
  2071. /* Low 16bits are length, high is vlan and
  2072. * unused currently so zero
  2073. */
  2074. desc[frag].len_vlan = skb_headlen(skb);
  2075. dma_addr = dma_map_single(&adapter->pdev->dev,
  2076. skb->data,
  2077. skb_headlen(skb),
  2078. DMA_TO_DEVICE);
  2079. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2080. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2081. frag++;
  2082. } else {
  2083. desc[frag].len_vlan = skb_headlen(skb) / 2;
  2084. dma_addr = dma_map_single(&adapter->pdev->dev,
  2085. skb->data,
  2086. skb_headlen(skb) / 2,
  2087. DMA_TO_DEVICE);
  2088. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2089. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2090. frag++;
  2091. desc[frag].len_vlan = skb_headlen(skb) / 2;
  2092. dma_addr = dma_map_single(&adapter->pdev->dev,
  2093. skb->data +
  2094. skb_headlen(skb) / 2,
  2095. skb_headlen(skb) / 2,
  2096. DMA_TO_DEVICE);
  2097. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2098. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2099. frag++;
  2100. }
  2101. } else {
  2102. desc[frag].len_vlan = frags[i - 1].size;
  2103. dma_addr = skb_frag_dma_map(&adapter->pdev->dev,
  2104. &frags[i - 1],
  2105. 0,
  2106. frags[i - 1].size,
  2107. DMA_TO_DEVICE);
  2108. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2109. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2110. frag++;
  2111. }
  2112. }
  2113. if (phydev && phydev->speed == SPEED_1000) {
  2114. if (++tx_ring->since_irq == PARM_TX_NUM_BUFS_DEF) {
  2115. /* Last element & Interrupt flag */
  2116. desc[frag - 1].flags =
  2117. TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
  2118. tx_ring->since_irq = 0;
  2119. } else { /* Last element */
  2120. desc[frag - 1].flags = TXDESC_FLAG_LASTPKT;
  2121. }
  2122. } else {
  2123. desc[frag - 1].flags =
  2124. TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
  2125. }
  2126. desc[0].flags |= TXDESC_FLAG_FIRSTPKT;
  2127. tcb->index_start = tx_ring->send_idx;
  2128. tcb->stale = 0;
  2129. thiscopy = NUM_DESC_PER_RING_TX - INDEX10(tx_ring->send_idx);
  2130. if (thiscopy >= frag) {
  2131. remainder = 0;
  2132. thiscopy = frag;
  2133. } else {
  2134. remainder = frag - thiscopy;
  2135. }
  2136. memcpy(tx_ring->tx_desc_ring + INDEX10(tx_ring->send_idx),
  2137. desc,
  2138. sizeof(struct tx_desc) * thiscopy);
  2139. add_10bit(&tx_ring->send_idx, thiscopy);
  2140. if (INDEX10(tx_ring->send_idx) == 0 ||
  2141. INDEX10(tx_ring->send_idx) == NUM_DESC_PER_RING_TX) {
  2142. tx_ring->send_idx &= ~ET_DMA10_MASK;
  2143. tx_ring->send_idx ^= ET_DMA10_WRAP;
  2144. }
  2145. if (remainder) {
  2146. memcpy(tx_ring->tx_desc_ring,
  2147. desc + thiscopy,
  2148. sizeof(struct tx_desc) * remainder);
  2149. add_10bit(&tx_ring->send_idx, remainder);
  2150. }
  2151. if (INDEX10(tx_ring->send_idx) == 0) {
  2152. if (tx_ring->send_idx)
  2153. tcb->index = NUM_DESC_PER_RING_TX - 1;
  2154. else
  2155. tcb->index = ET_DMA10_WRAP|(NUM_DESC_PER_RING_TX - 1);
  2156. } else {
  2157. tcb->index = tx_ring->send_idx - 1;
  2158. }
  2159. spin_lock(&adapter->tcb_send_qlock);
  2160. if (tx_ring->send_tail)
  2161. tx_ring->send_tail->next = tcb;
  2162. else
  2163. tx_ring->send_head = tcb;
  2164. tx_ring->send_tail = tcb;
  2165. WARN_ON(tcb->next != NULL);
  2166. tx_ring->used++;
  2167. spin_unlock(&adapter->tcb_send_qlock);
  2168. /* Write the new write pointer back to the device. */
  2169. writel(tx_ring->send_idx, &adapter->regs->txdma.service_request);
  2170. /* For Gig only, we use Tx Interrupt coalescing. Enable the software
  2171. * timer to wake us up if this packet isn't followed by N more.
  2172. */
  2173. if (phydev && phydev->speed == SPEED_1000) {
  2174. writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
  2175. &adapter->regs->global.watchdog_timer);
  2176. }
  2177. return 0;
  2178. }
  2179. static int send_packet(struct sk_buff *skb, struct et131x_adapter *adapter)
  2180. {
  2181. int status;
  2182. struct tcb *tcb;
  2183. unsigned long flags;
  2184. struct tx_ring *tx_ring = &adapter->tx_ring;
  2185. /* All packets must have at least a MAC address and a protocol type */
  2186. if (skb->len < ETH_HLEN)
  2187. return -EIO;
  2188. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2189. tcb = tx_ring->tcb_qhead;
  2190. if (tcb == NULL) {
  2191. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2192. return -ENOMEM;
  2193. }
  2194. tx_ring->tcb_qhead = tcb->next;
  2195. if (tx_ring->tcb_qhead == NULL)
  2196. tx_ring->tcb_qtail = NULL;
  2197. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2198. tcb->skb = skb;
  2199. tcb->next = NULL;
  2200. status = nic_send_packet(adapter, tcb);
  2201. if (status != 0) {
  2202. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2203. if (tx_ring->tcb_qtail)
  2204. tx_ring->tcb_qtail->next = tcb;
  2205. else
  2206. /* Apparently ready Q is empty. */
  2207. tx_ring->tcb_qhead = tcb;
  2208. tx_ring->tcb_qtail = tcb;
  2209. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2210. return status;
  2211. }
  2212. WARN_ON(tx_ring->used > NUM_TCB);
  2213. return 0;
  2214. }
  2215. /* free_send_packet - Recycle a struct tcb */
  2216. static inline void free_send_packet(struct et131x_adapter *adapter,
  2217. struct tcb *tcb)
  2218. {
  2219. unsigned long flags;
  2220. struct tx_desc *desc = NULL;
  2221. struct net_device_stats *stats = &adapter->netdev->stats;
  2222. struct tx_ring *tx_ring = &adapter->tx_ring;
  2223. u64 dma_addr;
  2224. if (tcb->skb) {
  2225. stats->tx_bytes += tcb->skb->len;
  2226. /* Iterate through the TX descriptors on the ring
  2227. * corresponding to this packet and umap the fragments
  2228. * they point to
  2229. */
  2230. do {
  2231. desc = tx_ring->tx_desc_ring +
  2232. INDEX10(tcb->index_start);
  2233. dma_addr = desc->addr_lo;
  2234. dma_addr |= (u64)desc->addr_hi << 32;
  2235. dma_unmap_single(&adapter->pdev->dev,
  2236. dma_addr,
  2237. desc->len_vlan, DMA_TO_DEVICE);
  2238. add_10bit(&tcb->index_start, 1);
  2239. if (INDEX10(tcb->index_start) >=
  2240. NUM_DESC_PER_RING_TX) {
  2241. tcb->index_start &= ~ET_DMA10_MASK;
  2242. tcb->index_start ^= ET_DMA10_WRAP;
  2243. }
  2244. } while (desc != tx_ring->tx_desc_ring + INDEX10(tcb->index));
  2245. dev_kfree_skb_any(tcb->skb);
  2246. }
  2247. memset(tcb, 0, sizeof(struct tcb));
  2248. /* Add the TCB to the Ready Q */
  2249. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2250. stats->tx_packets++;
  2251. if (tx_ring->tcb_qtail)
  2252. tx_ring->tcb_qtail->next = tcb;
  2253. else /* Apparently ready Q is empty. */
  2254. tx_ring->tcb_qhead = tcb;
  2255. tx_ring->tcb_qtail = tcb;
  2256. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2257. WARN_ON(tx_ring->used < 0);
  2258. }
  2259. /* et131x_free_busy_send_packets - Free and complete the stopped active sends */
  2260. static void et131x_free_busy_send_packets(struct et131x_adapter *adapter)
  2261. {
  2262. struct tcb *tcb;
  2263. unsigned long flags;
  2264. u32 freed = 0;
  2265. struct tx_ring *tx_ring = &adapter->tx_ring;
  2266. /* Any packets being sent? Check the first TCB on the send list */
  2267. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2268. tcb = tx_ring->send_head;
  2269. while (tcb != NULL && freed < NUM_TCB) {
  2270. struct tcb *next = tcb->next;
  2271. tx_ring->send_head = next;
  2272. if (next == NULL)
  2273. tx_ring->send_tail = NULL;
  2274. tx_ring->used--;
  2275. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2276. freed++;
  2277. free_send_packet(adapter, tcb);
  2278. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2279. tcb = tx_ring->send_head;
  2280. }
  2281. WARN_ON(freed == NUM_TCB);
  2282. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2283. tx_ring->used = 0;
  2284. }
  2285. /* et131x_handle_send_pkts
  2286. *
  2287. * Re-claim the send resources, complete sends and get more to send from
  2288. * the send wait queue.
  2289. */
  2290. static void et131x_handle_send_pkts(struct et131x_adapter *adapter)
  2291. {
  2292. unsigned long flags;
  2293. u32 serviced;
  2294. struct tcb *tcb;
  2295. u32 index;
  2296. struct tx_ring *tx_ring = &adapter->tx_ring;
  2297. serviced = readl(&adapter->regs->txdma.new_service_complete);
  2298. index = INDEX10(serviced);
  2299. /* Has the ring wrapped? Process any descriptors that do not have
  2300. * the same "wrap" indicator as the current completion indicator
  2301. */
  2302. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2303. tcb = tx_ring->send_head;
  2304. while (tcb &&
  2305. ((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
  2306. index < INDEX10(tcb->index)) {
  2307. tx_ring->used--;
  2308. tx_ring->send_head = tcb->next;
  2309. if (tcb->next == NULL)
  2310. tx_ring->send_tail = NULL;
  2311. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2312. free_send_packet(adapter, tcb);
  2313. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2314. /* Goto the next packet */
  2315. tcb = tx_ring->send_head;
  2316. }
  2317. while (tcb &&
  2318. !((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
  2319. index > (tcb->index & ET_DMA10_MASK)) {
  2320. tx_ring->used--;
  2321. tx_ring->send_head = tcb->next;
  2322. if (tcb->next == NULL)
  2323. tx_ring->send_tail = NULL;
  2324. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2325. free_send_packet(adapter, tcb);
  2326. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2327. /* Goto the next packet */
  2328. tcb = tx_ring->send_head;
  2329. }
  2330. /* Wake up the queue when we hit a low-water mark */
  2331. if (tx_ring->used <= NUM_TCB / 3)
  2332. netif_wake_queue(adapter->netdev);
  2333. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2334. }
  2335. static int et131x_get_regs_len(struct net_device *netdev)
  2336. {
  2337. #define ET131X_REGS_LEN 256
  2338. return ET131X_REGS_LEN * sizeof(u32);
  2339. }
  2340. static void et131x_get_regs(struct net_device *netdev,
  2341. struct ethtool_regs *regs, void *regs_data)
  2342. {
  2343. struct et131x_adapter *adapter = netdev_priv(netdev);
  2344. struct address_map __iomem *aregs = adapter->regs;
  2345. u32 *regs_buff = regs_data;
  2346. u32 num = 0;
  2347. u16 tmp;
  2348. memset(regs_data, 0, et131x_get_regs_len(netdev));
  2349. regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
  2350. adapter->pdev->device;
  2351. /* PHY regs */
  2352. et131x_mii_read(adapter, MII_BMCR, &tmp);
  2353. regs_buff[num++] = tmp;
  2354. et131x_mii_read(adapter, MII_BMSR, &tmp);
  2355. regs_buff[num++] = tmp;
  2356. et131x_mii_read(adapter, MII_PHYSID1, &tmp);
  2357. regs_buff[num++] = tmp;
  2358. et131x_mii_read(adapter, MII_PHYSID2, &tmp);
  2359. regs_buff[num++] = tmp;
  2360. et131x_mii_read(adapter, MII_ADVERTISE, &tmp);
  2361. regs_buff[num++] = tmp;
  2362. et131x_mii_read(adapter, MII_LPA, &tmp);
  2363. regs_buff[num++] = tmp;
  2364. et131x_mii_read(adapter, MII_EXPANSION, &tmp);
  2365. regs_buff[num++] = tmp;
  2366. /* Autoneg next page transmit reg */
  2367. et131x_mii_read(adapter, 0x07, &tmp);
  2368. regs_buff[num++] = tmp;
  2369. /* Link partner next page reg */
  2370. et131x_mii_read(adapter, 0x08, &tmp);
  2371. regs_buff[num++] = tmp;
  2372. et131x_mii_read(adapter, MII_CTRL1000, &tmp);
  2373. regs_buff[num++] = tmp;
  2374. et131x_mii_read(adapter, MII_STAT1000, &tmp);
  2375. regs_buff[num++] = tmp;
  2376. et131x_mii_read(adapter, 0x0b, &tmp);
  2377. regs_buff[num++] = tmp;
  2378. et131x_mii_read(adapter, 0x0c, &tmp);
  2379. regs_buff[num++] = tmp;
  2380. et131x_mii_read(adapter, MII_MMD_CTRL, &tmp);
  2381. regs_buff[num++] = tmp;
  2382. et131x_mii_read(adapter, MII_MMD_DATA, &tmp);
  2383. regs_buff[num++] = tmp;
  2384. et131x_mii_read(adapter, MII_ESTATUS, &tmp);
  2385. regs_buff[num++] = tmp;
  2386. et131x_mii_read(adapter, PHY_INDEX_REG, &tmp);
  2387. regs_buff[num++] = tmp;
  2388. et131x_mii_read(adapter, PHY_DATA_REG, &tmp);
  2389. regs_buff[num++] = tmp;
  2390. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG, &tmp);
  2391. regs_buff[num++] = tmp;
  2392. et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL, &tmp);
  2393. regs_buff[num++] = tmp;
  2394. et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL + 1, &tmp);
  2395. regs_buff[num++] = tmp;
  2396. et131x_mii_read(adapter, PHY_REGISTER_MGMT_CONTROL, &tmp);
  2397. regs_buff[num++] = tmp;
  2398. et131x_mii_read(adapter, PHY_CONFIG, &tmp);
  2399. regs_buff[num++] = tmp;
  2400. et131x_mii_read(adapter, PHY_PHY_CONTROL, &tmp);
  2401. regs_buff[num++] = tmp;
  2402. et131x_mii_read(adapter, PHY_INTERRUPT_MASK, &tmp);
  2403. regs_buff[num++] = tmp;
  2404. et131x_mii_read(adapter, PHY_INTERRUPT_STATUS, &tmp);
  2405. regs_buff[num++] = tmp;
  2406. et131x_mii_read(adapter, PHY_PHY_STATUS, &tmp);
  2407. regs_buff[num++] = tmp;
  2408. et131x_mii_read(adapter, PHY_LED_1, &tmp);
  2409. regs_buff[num++] = tmp;
  2410. et131x_mii_read(adapter, PHY_LED_2, &tmp);
  2411. regs_buff[num++] = tmp;
  2412. /* Global regs */
  2413. regs_buff[num++] = readl(&aregs->global.txq_start_addr);
  2414. regs_buff[num++] = readl(&aregs->global.txq_end_addr);
  2415. regs_buff[num++] = readl(&aregs->global.rxq_start_addr);
  2416. regs_buff[num++] = readl(&aregs->global.rxq_end_addr);
  2417. regs_buff[num++] = readl(&aregs->global.pm_csr);
  2418. regs_buff[num++] = adapter->stats.interrupt_status;
  2419. regs_buff[num++] = readl(&aregs->global.int_mask);
  2420. regs_buff[num++] = readl(&aregs->global.int_alias_clr_en);
  2421. regs_buff[num++] = readl(&aregs->global.int_status_alias);
  2422. regs_buff[num++] = readl(&aregs->global.sw_reset);
  2423. regs_buff[num++] = readl(&aregs->global.slv_timer);
  2424. regs_buff[num++] = readl(&aregs->global.msi_config);
  2425. regs_buff[num++] = readl(&aregs->global.loopback);
  2426. regs_buff[num++] = readl(&aregs->global.watchdog_timer);
  2427. /* TXDMA regs */
  2428. regs_buff[num++] = readl(&aregs->txdma.csr);
  2429. regs_buff[num++] = readl(&aregs->txdma.pr_base_hi);
  2430. regs_buff[num++] = readl(&aregs->txdma.pr_base_lo);
  2431. regs_buff[num++] = readl(&aregs->txdma.pr_num_des);
  2432. regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr);
  2433. regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr_ext);
  2434. regs_buff[num++] = readl(&aregs->txdma.txq_rd_addr);
  2435. regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_hi);
  2436. regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_lo);
  2437. regs_buff[num++] = readl(&aregs->txdma.service_request);
  2438. regs_buff[num++] = readl(&aregs->txdma.service_complete);
  2439. regs_buff[num++] = readl(&aregs->txdma.cache_rd_index);
  2440. regs_buff[num++] = readl(&aregs->txdma.cache_wr_index);
  2441. regs_buff[num++] = readl(&aregs->txdma.tx_dma_error);
  2442. regs_buff[num++] = readl(&aregs->txdma.desc_abort_cnt);
  2443. regs_buff[num++] = readl(&aregs->txdma.payload_abort_cnt);
  2444. regs_buff[num++] = readl(&aregs->txdma.writeback_abort_cnt);
  2445. regs_buff[num++] = readl(&aregs->txdma.desc_timeout_cnt);
  2446. regs_buff[num++] = readl(&aregs->txdma.payload_timeout_cnt);
  2447. regs_buff[num++] = readl(&aregs->txdma.writeback_timeout_cnt);
  2448. regs_buff[num++] = readl(&aregs->txdma.desc_error_cnt);
  2449. regs_buff[num++] = readl(&aregs->txdma.payload_error_cnt);
  2450. regs_buff[num++] = readl(&aregs->txdma.writeback_error_cnt);
  2451. regs_buff[num++] = readl(&aregs->txdma.dropped_tlp_cnt);
  2452. regs_buff[num++] = readl(&aregs->txdma.new_service_complete);
  2453. regs_buff[num++] = readl(&aregs->txdma.ethernet_packet_cnt);
  2454. /* RXDMA regs */
  2455. regs_buff[num++] = readl(&aregs->rxdma.csr);
  2456. regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_hi);
  2457. regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_lo);
  2458. regs_buff[num++] = readl(&aregs->rxdma.num_pkt_done);
  2459. regs_buff[num++] = readl(&aregs->rxdma.max_pkt_time);
  2460. regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr);
  2461. regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr_ext);
  2462. regs_buff[num++] = readl(&aregs->rxdma.rxq_wr_addr);
  2463. regs_buff[num++] = readl(&aregs->rxdma.psr_base_hi);
  2464. regs_buff[num++] = readl(&aregs->rxdma.psr_base_lo);
  2465. regs_buff[num++] = readl(&aregs->rxdma.psr_num_des);
  2466. regs_buff[num++] = readl(&aregs->rxdma.psr_avail_offset);
  2467. regs_buff[num++] = readl(&aregs->rxdma.psr_full_offset);
  2468. regs_buff[num++] = readl(&aregs->rxdma.psr_access_index);
  2469. regs_buff[num++] = readl(&aregs->rxdma.psr_min_des);
  2470. regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_lo);
  2471. regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_hi);
  2472. regs_buff[num++] = readl(&aregs->rxdma.fbr0_num_des);
  2473. regs_buff[num++] = readl(&aregs->rxdma.fbr0_avail_offset);
  2474. regs_buff[num++] = readl(&aregs->rxdma.fbr0_full_offset);
  2475. regs_buff[num++] = readl(&aregs->rxdma.fbr0_rd_index);
  2476. regs_buff[num++] = readl(&aregs->rxdma.fbr0_min_des);
  2477. regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_lo);
  2478. regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_hi);
  2479. regs_buff[num++] = readl(&aregs->rxdma.fbr1_num_des);
  2480. regs_buff[num++] = readl(&aregs->rxdma.fbr1_avail_offset);
  2481. regs_buff[num++] = readl(&aregs->rxdma.fbr1_full_offset);
  2482. regs_buff[num++] = readl(&aregs->rxdma.fbr1_rd_index);
  2483. regs_buff[num++] = readl(&aregs->rxdma.fbr1_min_des);
  2484. }
  2485. static void et131x_get_drvinfo(struct net_device *netdev,
  2486. struct ethtool_drvinfo *info)
  2487. {
  2488. struct et131x_adapter *adapter = netdev_priv(netdev);
  2489. strlcpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  2490. strlcpy(info->version, DRIVER_VERSION, sizeof(info->version));
  2491. strlcpy(info->bus_info, pci_name(adapter->pdev),
  2492. sizeof(info->bus_info));
  2493. }
  2494. static const struct ethtool_ops et131x_ethtool_ops = {
  2495. .get_drvinfo = et131x_get_drvinfo,
  2496. .get_regs_len = et131x_get_regs_len,
  2497. .get_regs = et131x_get_regs,
  2498. .get_link = ethtool_op_get_link,
  2499. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2500. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2501. };
  2502. /* et131x_hwaddr_init - set up the MAC Address */
  2503. static void et131x_hwaddr_init(struct et131x_adapter *adapter)
  2504. {
  2505. /* If have our default mac from init and no mac address from
  2506. * EEPROM then we need to generate the last octet and set it on the
  2507. * device
  2508. */
  2509. if (is_zero_ether_addr(adapter->rom_addr)) {
  2510. /* We need to randomly generate the last octet so we
  2511. * decrease our chances of setting the mac address to
  2512. * same as another one of our cards in the system
  2513. */
  2514. get_random_bytes(&adapter->addr[5], 1);
  2515. /* We have the default value in the register we are
  2516. * working with so we need to copy the current
  2517. * address into the permanent address
  2518. */
  2519. ether_addr_copy(adapter->rom_addr, adapter->addr);
  2520. } else {
  2521. /* We do not have an override address, so set the
  2522. * current address to the permanent address and add
  2523. * it to the device
  2524. */
  2525. ether_addr_copy(adapter->addr, adapter->rom_addr);
  2526. }
  2527. }
  2528. static int et131x_pci_init(struct et131x_adapter *adapter,
  2529. struct pci_dev *pdev)
  2530. {
  2531. u16 max_payload;
  2532. int i, rc;
  2533. rc = et131x_init_eeprom(adapter);
  2534. if (rc < 0)
  2535. goto out;
  2536. if (!pci_is_pcie(pdev)) {
  2537. dev_err(&pdev->dev, "Missing PCIe capabilities\n");
  2538. goto err_out;
  2539. }
  2540. /* Program the Ack/Nak latency and replay timers */
  2541. max_payload = pdev->pcie_mpss;
  2542. if (max_payload < 2) {
  2543. static const u16 acknak[2] = { 0x76, 0xD0 };
  2544. static const u16 replay[2] = { 0x1E0, 0x2ED };
  2545. if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
  2546. acknak[max_payload])) {
  2547. dev_err(&pdev->dev,
  2548. "Could not write PCI config space for ACK/NAK\n");
  2549. goto err_out;
  2550. }
  2551. if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
  2552. replay[max_payload])) {
  2553. dev_err(&pdev->dev,
  2554. "Could not write PCI config space for Replay Timer\n");
  2555. goto err_out;
  2556. }
  2557. }
  2558. /* l0s and l1 latency timers. We are using default values.
  2559. * Representing 001 for L0s and 010 for L1
  2560. */
  2561. if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
  2562. dev_err(&pdev->dev,
  2563. "Could not write PCI config space for Latency Timers\n");
  2564. goto err_out;
  2565. }
  2566. /* Change the max read size to 2k */
  2567. if (pcie_set_readrq(pdev, 2048)) {
  2568. dev_err(&pdev->dev,
  2569. "Couldn't change PCI config space for Max read size\n");
  2570. goto err_out;
  2571. }
  2572. /* Get MAC address from config space if an eeprom exists, otherwise
  2573. * the MAC address there will not be valid
  2574. */
  2575. if (!adapter->has_eeprom) {
  2576. et131x_hwaddr_init(adapter);
  2577. return 0;
  2578. }
  2579. for (i = 0; i < ETH_ALEN; i++) {
  2580. if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
  2581. adapter->rom_addr + i)) {
  2582. dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
  2583. goto err_out;
  2584. }
  2585. }
  2586. ether_addr_copy(adapter->addr, adapter->rom_addr);
  2587. out:
  2588. return rc;
  2589. err_out:
  2590. rc = -EIO;
  2591. goto out;
  2592. }
  2593. /* et131x_error_timer_handler
  2594. * @data: timer-specific variable; here a pointer to our adapter structure
  2595. *
  2596. * The routine called when the error timer expires, to track the number of
  2597. * recurring errors.
  2598. */
  2599. static void et131x_error_timer_handler(struct timer_list *t)
  2600. {
  2601. struct et131x_adapter *adapter = from_timer(adapter, t, error_timer);
  2602. struct phy_device *phydev = adapter->netdev->phydev;
  2603. if (et1310_in_phy_coma(adapter)) {
  2604. /* Bring the device immediately out of coma, to
  2605. * prevent it from sleeping indefinitely, this
  2606. * mechanism could be improved!
  2607. */
  2608. et1310_disable_phy_coma(adapter);
  2609. adapter->boot_coma = 20;
  2610. } else {
  2611. et1310_update_macstat_host_counters(adapter);
  2612. }
  2613. if (!phydev->link && adapter->boot_coma < 11)
  2614. adapter->boot_coma++;
  2615. if (adapter->boot_coma == 10) {
  2616. if (!phydev->link) {
  2617. if (!et1310_in_phy_coma(adapter)) {
  2618. /* NOTE - This was originally a 'sync with
  2619. * interrupt'. How to do that under Linux?
  2620. */
  2621. et131x_enable_interrupts(adapter);
  2622. et1310_enable_phy_coma(adapter);
  2623. }
  2624. }
  2625. }
  2626. /* This is a periodic timer, so reschedule */
  2627. mod_timer(&adapter->error_timer, jiffies +
  2628. msecs_to_jiffies(TX_ERROR_PERIOD));
  2629. }
  2630. static void et131x_adapter_memory_free(struct et131x_adapter *adapter)
  2631. {
  2632. et131x_tx_dma_memory_free(adapter);
  2633. et131x_rx_dma_memory_free(adapter);
  2634. }
  2635. static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
  2636. {
  2637. int status;
  2638. status = et131x_tx_dma_memory_alloc(adapter);
  2639. if (status) {
  2640. dev_err(&adapter->pdev->dev,
  2641. "et131x_tx_dma_memory_alloc FAILED\n");
  2642. et131x_tx_dma_memory_free(adapter);
  2643. return status;
  2644. }
  2645. status = et131x_rx_dma_memory_alloc(adapter);
  2646. if (status) {
  2647. dev_err(&adapter->pdev->dev,
  2648. "et131x_rx_dma_memory_alloc FAILED\n");
  2649. et131x_adapter_memory_free(adapter);
  2650. return status;
  2651. }
  2652. status = et131x_init_recv(adapter);
  2653. if (status) {
  2654. dev_err(&adapter->pdev->dev, "et131x_init_recv FAILED\n");
  2655. et131x_adapter_memory_free(adapter);
  2656. }
  2657. return status;
  2658. }
  2659. static void et131x_adjust_link(struct net_device *netdev)
  2660. {
  2661. struct et131x_adapter *adapter = netdev_priv(netdev);
  2662. struct phy_device *phydev = netdev->phydev;
  2663. if (!phydev)
  2664. return;
  2665. if (phydev->link == adapter->link)
  2666. return;
  2667. /* Check to see if we are in coma mode and if
  2668. * so, disable it because we will not be able
  2669. * to read PHY values until we are out.
  2670. */
  2671. if (et1310_in_phy_coma(adapter))
  2672. et1310_disable_phy_coma(adapter);
  2673. adapter->link = phydev->link;
  2674. phy_print_status(phydev);
  2675. if (phydev->link) {
  2676. adapter->boot_coma = 20;
  2677. if (phydev->speed == SPEED_10) {
  2678. u16 register18;
  2679. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
  2680. &register18);
  2681. et131x_mii_write(adapter, phydev->mdio.addr,
  2682. PHY_MPHY_CONTROL_REG,
  2683. register18 | 0x4);
  2684. et131x_mii_write(adapter, phydev->mdio.addr,
  2685. PHY_INDEX_REG, register18 | 0x8402);
  2686. et131x_mii_write(adapter, phydev->mdio.addr,
  2687. PHY_DATA_REG, register18 | 511);
  2688. et131x_mii_write(adapter, phydev->mdio.addr,
  2689. PHY_MPHY_CONTROL_REG, register18);
  2690. }
  2691. et1310_config_flow_control(adapter);
  2692. if (phydev->speed == SPEED_1000 &&
  2693. adapter->registry_jumbo_packet > 2048) {
  2694. u16 reg;
  2695. et131x_mii_read(adapter, PHY_CONFIG, &reg);
  2696. reg &= ~ET_PHY_CONFIG_TX_FIFO_DEPTH;
  2697. reg |= ET_PHY_CONFIG_FIFO_DEPTH_32;
  2698. et131x_mii_write(adapter, phydev->mdio.addr,
  2699. PHY_CONFIG, reg);
  2700. }
  2701. et131x_set_rx_dma_timer(adapter);
  2702. et1310_config_mac_regs2(adapter);
  2703. } else {
  2704. adapter->boot_coma = 0;
  2705. if (phydev->speed == SPEED_10) {
  2706. u16 register18;
  2707. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
  2708. &register18);
  2709. et131x_mii_write(adapter, phydev->mdio.addr,
  2710. PHY_MPHY_CONTROL_REG,
  2711. register18 | 0x4);
  2712. et131x_mii_write(adapter, phydev->mdio.addr,
  2713. PHY_INDEX_REG, register18 | 0x8402);
  2714. et131x_mii_write(adapter, phydev->mdio.addr,
  2715. PHY_DATA_REG, register18 | 511);
  2716. et131x_mii_write(adapter, phydev->mdio.addr,
  2717. PHY_MPHY_CONTROL_REG, register18);
  2718. }
  2719. et131x_free_busy_send_packets(adapter);
  2720. et131x_init_send(adapter);
  2721. /* Bring the device back to the state it was during
  2722. * init prior to autonegotiation being complete. This
  2723. * way, when we get the auto-neg complete interrupt,
  2724. * we can complete init by calling config_mac_regs2.
  2725. */
  2726. et131x_soft_reset(adapter);
  2727. et131x_adapter_setup(adapter);
  2728. et131x_disable_txrx(netdev);
  2729. et131x_enable_txrx(netdev);
  2730. }
  2731. }
  2732. static int et131x_mii_probe(struct net_device *netdev)
  2733. {
  2734. struct et131x_adapter *adapter = netdev_priv(netdev);
  2735. struct phy_device *phydev = NULL;
  2736. phydev = phy_find_first(adapter->mii_bus);
  2737. if (!phydev) {
  2738. dev_err(&adapter->pdev->dev, "no PHY found\n");
  2739. return -ENODEV;
  2740. }
  2741. phydev = phy_connect(netdev, phydev_name(phydev),
  2742. &et131x_adjust_link, PHY_INTERFACE_MODE_MII);
  2743. if (IS_ERR(phydev)) {
  2744. dev_err(&adapter->pdev->dev, "Could not attach to PHY\n");
  2745. return PTR_ERR(phydev);
  2746. }
  2747. phydev->supported &= (SUPPORTED_10baseT_Half |
  2748. SUPPORTED_10baseT_Full |
  2749. SUPPORTED_100baseT_Half |
  2750. SUPPORTED_100baseT_Full |
  2751. SUPPORTED_Autoneg |
  2752. SUPPORTED_MII |
  2753. SUPPORTED_TP);
  2754. if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
  2755. phydev->supported |= SUPPORTED_1000baseT_Half |
  2756. SUPPORTED_1000baseT_Full;
  2757. phydev->advertising = phydev->supported;
  2758. phydev->autoneg = AUTONEG_ENABLE;
  2759. phy_attached_info(phydev);
  2760. return 0;
  2761. }
  2762. static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
  2763. struct pci_dev *pdev)
  2764. {
  2765. static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
  2766. struct et131x_adapter *adapter;
  2767. adapter = netdev_priv(netdev);
  2768. adapter->pdev = pci_dev_get(pdev);
  2769. adapter->netdev = netdev;
  2770. spin_lock_init(&adapter->tcb_send_qlock);
  2771. spin_lock_init(&adapter->tcb_ready_qlock);
  2772. spin_lock_init(&adapter->rcv_lock);
  2773. adapter->registry_jumbo_packet = 1514; /* 1514-9216 */
  2774. ether_addr_copy(adapter->addr, default_mac);
  2775. return adapter;
  2776. }
  2777. static void et131x_pci_remove(struct pci_dev *pdev)
  2778. {
  2779. struct net_device *netdev = pci_get_drvdata(pdev);
  2780. struct et131x_adapter *adapter = netdev_priv(netdev);
  2781. unregister_netdev(netdev);
  2782. netif_napi_del(&adapter->napi);
  2783. phy_disconnect(netdev->phydev);
  2784. mdiobus_unregister(adapter->mii_bus);
  2785. mdiobus_free(adapter->mii_bus);
  2786. et131x_adapter_memory_free(adapter);
  2787. iounmap(adapter->regs);
  2788. pci_dev_put(pdev);
  2789. free_netdev(netdev);
  2790. pci_release_regions(pdev);
  2791. pci_disable_device(pdev);
  2792. }
  2793. static void et131x_up(struct net_device *netdev)
  2794. {
  2795. et131x_enable_txrx(netdev);
  2796. phy_start(netdev->phydev);
  2797. }
  2798. static void et131x_down(struct net_device *netdev)
  2799. {
  2800. /* Save the timestamp for the TX watchdog, prevent a timeout */
  2801. netif_trans_update(netdev);
  2802. phy_stop(netdev->phydev);
  2803. et131x_disable_txrx(netdev);
  2804. }
  2805. #ifdef CONFIG_PM_SLEEP
  2806. static int et131x_suspend(struct device *dev)
  2807. {
  2808. struct pci_dev *pdev = to_pci_dev(dev);
  2809. struct net_device *netdev = pci_get_drvdata(pdev);
  2810. if (netif_running(netdev)) {
  2811. netif_device_detach(netdev);
  2812. et131x_down(netdev);
  2813. pci_save_state(pdev);
  2814. }
  2815. return 0;
  2816. }
  2817. static int et131x_resume(struct device *dev)
  2818. {
  2819. struct pci_dev *pdev = to_pci_dev(dev);
  2820. struct net_device *netdev = pci_get_drvdata(pdev);
  2821. if (netif_running(netdev)) {
  2822. pci_restore_state(pdev);
  2823. et131x_up(netdev);
  2824. netif_device_attach(netdev);
  2825. }
  2826. return 0;
  2827. }
  2828. #endif
  2829. static SIMPLE_DEV_PM_OPS(et131x_pm_ops, et131x_suspend, et131x_resume);
  2830. static irqreturn_t et131x_isr(int irq, void *dev_id)
  2831. {
  2832. bool handled = true;
  2833. bool enable_interrupts = true;
  2834. struct net_device *netdev = dev_id;
  2835. struct et131x_adapter *adapter = netdev_priv(netdev);
  2836. struct address_map __iomem *iomem = adapter->regs;
  2837. struct rx_ring *rx_ring = &adapter->rx_ring;
  2838. struct tx_ring *tx_ring = &adapter->tx_ring;
  2839. u32 status;
  2840. if (!netif_device_present(netdev)) {
  2841. handled = false;
  2842. enable_interrupts = false;
  2843. goto out;
  2844. }
  2845. et131x_disable_interrupts(adapter);
  2846. status = readl(&adapter->regs->global.int_status);
  2847. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
  2848. status &= ~INT_MASK_ENABLE;
  2849. else
  2850. status &= ~INT_MASK_ENABLE_NO_FLOW;
  2851. /* Make sure this is our interrupt */
  2852. if (!status) {
  2853. handled = false;
  2854. et131x_enable_interrupts(adapter);
  2855. goto out;
  2856. }
  2857. /* This is our interrupt, so process accordingly */
  2858. if (status & ET_INTR_WATCHDOG) {
  2859. struct tcb *tcb = tx_ring->send_head;
  2860. if (tcb)
  2861. if (++tcb->stale > 1)
  2862. status |= ET_INTR_TXDMA_ISR;
  2863. if (rx_ring->unfinished_receives)
  2864. status |= ET_INTR_RXDMA_XFR_DONE;
  2865. else if (tcb == NULL)
  2866. writel(0, &adapter->regs->global.watchdog_timer);
  2867. status &= ~ET_INTR_WATCHDOG;
  2868. }
  2869. if (status & (ET_INTR_RXDMA_XFR_DONE | ET_INTR_TXDMA_ISR)) {
  2870. enable_interrupts = false;
  2871. napi_schedule(&adapter->napi);
  2872. }
  2873. status &= ~(ET_INTR_TXDMA_ISR | ET_INTR_RXDMA_XFR_DONE);
  2874. if (!status)
  2875. goto out;
  2876. if (status & ET_INTR_TXDMA_ERR) {
  2877. /* Following read also clears the register (COR) */
  2878. u32 txdma_err = readl(&iomem->txdma.tx_dma_error);
  2879. dev_warn(&adapter->pdev->dev,
  2880. "TXDMA_ERR interrupt, error = %d\n",
  2881. txdma_err);
  2882. }
  2883. if (status & (ET_INTR_RXDMA_FB_R0_LOW | ET_INTR_RXDMA_FB_R1_LOW)) {
  2884. /* This indicates the number of unused buffers in RXDMA free
  2885. * buffer ring 0 is <= the limit you programmed. Free buffer
  2886. * resources need to be returned. Free buffers are consumed as
  2887. * packets are passed from the network to the host. The host
  2888. * becomes aware of the packets from the contents of the packet
  2889. * status ring. This ring is queried when the packet done
  2890. * interrupt occurs. Packets are then passed to the OS. When
  2891. * the OS is done with the packets the resources can be
  2892. * returned to the ET1310 for re-use. This interrupt is one
  2893. * method of returning resources.
  2894. */
  2895. /* If the user has flow control on, then we will
  2896. * send a pause packet, otherwise just exit
  2897. */
  2898. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH) {
  2899. u32 pm_csr;
  2900. /* Tell the device to send a pause packet via the back
  2901. * pressure register (bp req and bp xon/xoff)
  2902. */
  2903. pm_csr = readl(&iomem->global.pm_csr);
  2904. if (!et1310_in_phy_coma(adapter))
  2905. writel(3, &iomem->txmac.bp_ctrl);
  2906. }
  2907. }
  2908. /* Handle Packet Status Ring Low Interrupt */
  2909. if (status & ET_INTR_RXDMA_STAT_LOW) {
  2910. /* Same idea as with the two Free Buffer Rings. Packets going
  2911. * from the network to the host each consume a free buffer
  2912. * resource and a packet status resource. These resources are
  2913. * passed to the OS. When the OS is done with the resources,
  2914. * they need to be returned to the ET1310. This is one method
  2915. * of returning the resources.
  2916. */
  2917. }
  2918. if (status & ET_INTR_RXDMA_ERR) {
  2919. /* The rxdma_error interrupt is sent when a time-out on a
  2920. * request issued by the JAGCore has occurred or a completion is
  2921. * returned with an un-successful status. In both cases the
  2922. * request is considered complete. The JAGCore will
  2923. * automatically re-try the request in question. Normally
  2924. * information on events like these are sent to the host using
  2925. * the "Advanced Error Reporting" capability. This interrupt is
  2926. * another way of getting similar information. The only thing
  2927. * required is to clear the interrupt by reading the ISR in the
  2928. * global resources. The JAGCore will do a re-try on the
  2929. * request. Normally you should never see this interrupt. If
  2930. * you start to see this interrupt occurring frequently then
  2931. * something bad has occurred. A reset might be the thing to do.
  2932. */
  2933. /* TRAP();*/
  2934. dev_warn(&adapter->pdev->dev, "RxDMA_ERR interrupt, error %x\n",
  2935. readl(&iomem->txmac.tx_test));
  2936. }
  2937. /* Handle the Wake on LAN Event */
  2938. if (status & ET_INTR_WOL) {
  2939. /* This is a secondary interrupt for wake on LAN. The driver
  2940. * should never see this, if it does, something serious is
  2941. * wrong.
  2942. */
  2943. dev_err(&adapter->pdev->dev, "WAKE_ON_LAN interrupt\n");
  2944. }
  2945. if (status & ET_INTR_TXMAC) {
  2946. u32 err = readl(&iomem->txmac.err);
  2947. /* When any of the errors occur and TXMAC generates an
  2948. * interrupt to report these errors, it usually means that
  2949. * TXMAC has detected an error in the data stream retrieved
  2950. * from the on-chip Tx Q. All of these errors are catastrophic
  2951. * and TXMAC won't be able to recover data when these errors
  2952. * occur. In a nutshell, the whole Tx path will have to be reset
  2953. * and re-configured afterwards.
  2954. */
  2955. dev_warn(&adapter->pdev->dev, "TXMAC interrupt, error 0x%08x\n",
  2956. err);
  2957. /* If we are debugging, we want to see this error, otherwise we
  2958. * just want the device to be reset and continue
  2959. */
  2960. }
  2961. if (status & ET_INTR_RXMAC) {
  2962. /* These interrupts are catastrophic to the device, what we need
  2963. * to do is disable the interrupts and set the flag to cause us
  2964. * to reset so we can solve this issue.
  2965. */
  2966. dev_warn(&adapter->pdev->dev,
  2967. "RXMAC interrupt, error 0x%08x. Requesting reset\n",
  2968. readl(&iomem->rxmac.err_reg));
  2969. dev_warn(&adapter->pdev->dev,
  2970. "Enable 0x%08x, Diag 0x%08x\n",
  2971. readl(&iomem->rxmac.ctrl),
  2972. readl(&iomem->rxmac.rxq_diag));
  2973. /* If we are debugging, we want to see this error, otherwise we
  2974. * just want the device to be reset and continue
  2975. */
  2976. }
  2977. if (status & ET_INTR_MAC_STAT) {
  2978. /* This means at least one of the un-masked counters in the
  2979. * MAC_STAT block has rolled over. Use this to maintain the top,
  2980. * software managed bits of the counter(s).
  2981. */
  2982. et1310_handle_macstat_interrupt(adapter);
  2983. }
  2984. if (status & ET_INTR_SLV_TIMEOUT) {
  2985. /* This means a timeout has occurred on a read or write request
  2986. * to one of the JAGCore registers. The Global Resources block
  2987. * has terminated the request and on a read request, returned a
  2988. * "fake" value. The most likely reasons are: Bad Address or the
  2989. * addressed module is in a power-down state and can't respond.
  2990. */
  2991. }
  2992. out:
  2993. if (enable_interrupts)
  2994. et131x_enable_interrupts(adapter);
  2995. return IRQ_RETVAL(handled);
  2996. }
  2997. static int et131x_poll(struct napi_struct *napi, int budget)
  2998. {
  2999. struct et131x_adapter *adapter =
  3000. container_of(napi, struct et131x_adapter, napi);
  3001. int work_done = et131x_handle_recv_pkts(adapter, budget);
  3002. et131x_handle_send_pkts(adapter);
  3003. if (work_done < budget) {
  3004. napi_complete_done(&adapter->napi, work_done);
  3005. et131x_enable_interrupts(adapter);
  3006. }
  3007. return work_done;
  3008. }
  3009. /* et131x_stats - Return the current device statistics */
  3010. static struct net_device_stats *et131x_stats(struct net_device *netdev)
  3011. {
  3012. struct et131x_adapter *adapter = netdev_priv(netdev);
  3013. struct net_device_stats *stats = &adapter->netdev->stats;
  3014. struct ce_stats *devstat = &adapter->stats;
  3015. stats->rx_errors = devstat->rx_length_errs +
  3016. devstat->rx_align_errs +
  3017. devstat->rx_crc_errs +
  3018. devstat->rx_code_violations +
  3019. devstat->rx_other_errs;
  3020. stats->tx_errors = devstat->tx_max_pkt_errs;
  3021. stats->multicast = devstat->multicast_pkts_rcvd;
  3022. stats->collisions = devstat->tx_collisions;
  3023. stats->rx_length_errors = devstat->rx_length_errs;
  3024. stats->rx_over_errors = devstat->rx_overflows;
  3025. stats->rx_crc_errors = devstat->rx_crc_errs;
  3026. stats->rx_dropped = devstat->rcvd_pkts_dropped;
  3027. /* NOTE: Not used, can't find analogous statistics */
  3028. /* stats->rx_frame_errors = devstat->; */
  3029. /* stats->rx_fifo_errors = devstat->; */
  3030. /* stats->rx_missed_errors = devstat->; */
  3031. /* stats->tx_aborted_errors = devstat->; */
  3032. /* stats->tx_carrier_errors = devstat->; */
  3033. /* stats->tx_fifo_errors = devstat->; */
  3034. /* stats->tx_heartbeat_errors = devstat->; */
  3035. /* stats->tx_window_errors = devstat->; */
  3036. return stats;
  3037. }
  3038. static int et131x_open(struct net_device *netdev)
  3039. {
  3040. struct et131x_adapter *adapter = netdev_priv(netdev);
  3041. struct pci_dev *pdev = adapter->pdev;
  3042. unsigned int irq = pdev->irq;
  3043. int result;
  3044. /* Start the timer to track NIC errors */
  3045. timer_setup(&adapter->error_timer, et131x_error_timer_handler, 0);
  3046. adapter->error_timer.expires = jiffies +
  3047. msecs_to_jiffies(TX_ERROR_PERIOD);
  3048. add_timer(&adapter->error_timer);
  3049. result = request_irq(irq, et131x_isr,
  3050. IRQF_SHARED, netdev->name, netdev);
  3051. if (result) {
  3052. dev_err(&pdev->dev, "could not register IRQ %d\n", irq);
  3053. return result;
  3054. }
  3055. adapter->flags |= FMP_ADAPTER_INTERRUPT_IN_USE;
  3056. napi_enable(&adapter->napi);
  3057. et131x_up(netdev);
  3058. return result;
  3059. }
  3060. static int et131x_close(struct net_device *netdev)
  3061. {
  3062. struct et131x_adapter *adapter = netdev_priv(netdev);
  3063. et131x_down(netdev);
  3064. napi_disable(&adapter->napi);
  3065. adapter->flags &= ~FMP_ADAPTER_INTERRUPT_IN_USE;
  3066. free_irq(adapter->pdev->irq, netdev);
  3067. /* Stop the error timer */
  3068. return del_timer_sync(&adapter->error_timer);
  3069. }
  3070. static int et131x_ioctl(struct net_device *netdev, struct ifreq *reqbuf,
  3071. int cmd)
  3072. {
  3073. if (!netdev->phydev)
  3074. return -EINVAL;
  3075. return phy_mii_ioctl(netdev->phydev, reqbuf, cmd);
  3076. }
  3077. /* et131x_set_packet_filter - Configures the Rx Packet filtering */
  3078. static int et131x_set_packet_filter(struct et131x_adapter *adapter)
  3079. {
  3080. int filter = adapter->packet_filter;
  3081. u32 ctrl;
  3082. u32 pf_ctrl;
  3083. ctrl = readl(&adapter->regs->rxmac.ctrl);
  3084. pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
  3085. /* Default to disabled packet filtering */
  3086. ctrl |= 0x04;
  3087. /* Set us to be in promiscuous mode so we receive everything, this
  3088. * is also true when we get a packet filter of 0
  3089. */
  3090. if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
  3091. pf_ctrl &= ~7; /* Clear filter bits */
  3092. else {
  3093. /* Set us up with Multicast packet filtering. Three cases are
  3094. * possible - (1) we have a multi-cast list, (2) we receive ALL
  3095. * multicast entries or (3) we receive none.
  3096. */
  3097. if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
  3098. pf_ctrl &= ~2; /* Multicast filter bit */
  3099. else {
  3100. et1310_setup_device_for_multicast(adapter);
  3101. pf_ctrl |= 2;
  3102. ctrl &= ~0x04;
  3103. }
  3104. /* Set us up with Unicast packet filtering */
  3105. if (filter & ET131X_PACKET_TYPE_DIRECTED) {
  3106. et1310_setup_device_for_unicast(adapter);
  3107. pf_ctrl |= 4;
  3108. ctrl &= ~0x04;
  3109. }
  3110. /* Set us up with Broadcast packet filtering */
  3111. if (filter & ET131X_PACKET_TYPE_BROADCAST) {
  3112. pf_ctrl |= 1; /* Broadcast filter bit */
  3113. ctrl &= ~0x04;
  3114. } else {
  3115. pf_ctrl &= ~1;
  3116. }
  3117. /* Setup the receive mac configuration registers - Packet
  3118. * Filter control + the enable / disable for packet filter
  3119. * in the control reg.
  3120. */
  3121. writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
  3122. writel(ctrl, &adapter->regs->rxmac.ctrl);
  3123. }
  3124. return 0;
  3125. }
  3126. static void et131x_multicast(struct net_device *netdev)
  3127. {
  3128. struct et131x_adapter *adapter = netdev_priv(netdev);
  3129. int packet_filter;
  3130. struct netdev_hw_addr *ha;
  3131. int i;
  3132. /* Before we modify the platform-independent filter flags, store them
  3133. * locally. This allows us to determine if anything's changed and if
  3134. * we even need to bother the hardware
  3135. */
  3136. packet_filter = adapter->packet_filter;
  3137. /* Clear the 'multicast' flag locally; because we only have a single
  3138. * flag to check multicast, and multiple multicast addresses can be
  3139. * set, this is the easiest way to determine if more than one
  3140. * multicast address is being set.
  3141. */
  3142. packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
  3143. /* Check the net_device flags and set the device independent flags
  3144. * accordingly
  3145. */
  3146. if (netdev->flags & IFF_PROMISC)
  3147. adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
  3148. else
  3149. adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
  3150. if ((netdev->flags & IFF_ALLMULTI) ||
  3151. (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST))
  3152. adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
  3153. if (netdev_mc_count(netdev) < 1) {
  3154. adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
  3155. adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
  3156. } else {
  3157. adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
  3158. }
  3159. /* Set values in the private adapter struct */
  3160. i = 0;
  3161. netdev_for_each_mc_addr(ha, netdev) {
  3162. if (i == NIC_MAX_MCAST_LIST)
  3163. break;
  3164. ether_addr_copy(adapter->multicast_list[i++], ha->addr);
  3165. }
  3166. adapter->multicast_addr_count = i;
  3167. /* Are the new flags different from the previous ones? If not, then no
  3168. * action is required
  3169. *
  3170. * NOTE - This block will always update the multicast_list with the
  3171. * hardware, even if the addresses aren't the same.
  3172. */
  3173. if (packet_filter != adapter->packet_filter)
  3174. et131x_set_packet_filter(adapter);
  3175. }
  3176. static netdev_tx_t et131x_tx(struct sk_buff *skb, struct net_device *netdev)
  3177. {
  3178. struct et131x_adapter *adapter = netdev_priv(netdev);
  3179. struct tx_ring *tx_ring = &adapter->tx_ring;
  3180. /* stop the queue if it's getting full */
  3181. if (tx_ring->used >= NUM_TCB - 1 && !netif_queue_stopped(netdev))
  3182. netif_stop_queue(netdev);
  3183. /* Save the timestamp for the TX timeout watchdog */
  3184. netif_trans_update(netdev);
  3185. /* TCB is not available */
  3186. if (tx_ring->used >= NUM_TCB)
  3187. goto drop_err;
  3188. if ((adapter->flags & FMP_ADAPTER_FAIL_SEND_MASK) ||
  3189. !netif_carrier_ok(netdev))
  3190. goto drop_err;
  3191. if (send_packet(skb, adapter))
  3192. goto drop_err;
  3193. return NETDEV_TX_OK;
  3194. drop_err:
  3195. dev_kfree_skb_any(skb);
  3196. adapter->netdev->stats.tx_dropped++;
  3197. return NETDEV_TX_OK;
  3198. }
  3199. /* et131x_tx_timeout - Timeout handler
  3200. *
  3201. * The handler called when a Tx request times out. The timeout period is
  3202. * specified by the 'tx_timeo" element in the net_device structure (see
  3203. * et131x_alloc_device() to see how this value is set).
  3204. */
  3205. static void et131x_tx_timeout(struct net_device *netdev)
  3206. {
  3207. struct et131x_adapter *adapter = netdev_priv(netdev);
  3208. struct tx_ring *tx_ring = &adapter->tx_ring;
  3209. struct tcb *tcb;
  3210. unsigned long flags;
  3211. /* If the device is closed, ignore the timeout */
  3212. if (!(adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE))
  3213. return;
  3214. /* Any nonrecoverable hardware error?
  3215. * Checks adapter->flags for any failure in phy reading
  3216. */
  3217. if (adapter->flags & FMP_ADAPTER_NON_RECOVER_ERROR)
  3218. return;
  3219. /* Hardware failure? */
  3220. if (adapter->flags & FMP_ADAPTER_HARDWARE_ERROR) {
  3221. dev_err(&adapter->pdev->dev, "hardware error - reset\n");
  3222. return;
  3223. }
  3224. /* Is send stuck? */
  3225. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  3226. tcb = tx_ring->send_head;
  3227. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  3228. if (tcb) {
  3229. tcb->count++;
  3230. if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
  3231. dev_warn(&adapter->pdev->dev,
  3232. "Send stuck - reset. tcb->WrIndex %x\n",
  3233. tcb->index);
  3234. adapter->netdev->stats.tx_errors++;
  3235. /* perform reset of tx/rx */
  3236. et131x_disable_txrx(netdev);
  3237. et131x_enable_txrx(netdev);
  3238. }
  3239. }
  3240. }
  3241. static int et131x_change_mtu(struct net_device *netdev, int new_mtu)
  3242. {
  3243. int result = 0;
  3244. struct et131x_adapter *adapter = netdev_priv(netdev);
  3245. et131x_disable_txrx(netdev);
  3246. netdev->mtu = new_mtu;
  3247. et131x_adapter_memory_free(adapter);
  3248. /* Set the config parameter for Jumbo Packet support */
  3249. adapter->registry_jumbo_packet = new_mtu + 14;
  3250. et131x_soft_reset(adapter);
  3251. result = et131x_adapter_memory_alloc(adapter);
  3252. if (result != 0) {
  3253. dev_warn(&adapter->pdev->dev,
  3254. "Change MTU failed; couldn't re-alloc DMA memory\n");
  3255. return result;
  3256. }
  3257. et131x_init_send(adapter);
  3258. et131x_hwaddr_init(adapter);
  3259. ether_addr_copy(netdev->dev_addr, adapter->addr);
  3260. /* Init the device with the new settings */
  3261. et131x_adapter_setup(adapter);
  3262. et131x_enable_txrx(netdev);
  3263. return result;
  3264. }
  3265. static const struct net_device_ops et131x_netdev_ops = {
  3266. .ndo_open = et131x_open,
  3267. .ndo_stop = et131x_close,
  3268. .ndo_start_xmit = et131x_tx,
  3269. .ndo_set_rx_mode = et131x_multicast,
  3270. .ndo_tx_timeout = et131x_tx_timeout,
  3271. .ndo_change_mtu = et131x_change_mtu,
  3272. .ndo_set_mac_address = eth_mac_addr,
  3273. .ndo_validate_addr = eth_validate_addr,
  3274. .ndo_get_stats = et131x_stats,
  3275. .ndo_do_ioctl = et131x_ioctl,
  3276. };
  3277. static int et131x_pci_setup(struct pci_dev *pdev,
  3278. const struct pci_device_id *ent)
  3279. {
  3280. struct net_device *netdev;
  3281. struct et131x_adapter *adapter;
  3282. int rc;
  3283. rc = pci_enable_device(pdev);
  3284. if (rc < 0) {
  3285. dev_err(&pdev->dev, "pci_enable_device() failed\n");
  3286. goto out;
  3287. }
  3288. /* Perform some basic PCI checks */
  3289. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  3290. dev_err(&pdev->dev, "Can't find PCI device's base address\n");
  3291. rc = -ENODEV;
  3292. goto err_disable;
  3293. }
  3294. rc = pci_request_regions(pdev, DRIVER_NAME);
  3295. if (rc < 0) {
  3296. dev_err(&pdev->dev, "Can't get PCI resources\n");
  3297. goto err_disable;
  3298. }
  3299. pci_set_master(pdev);
  3300. /* Check the DMA addressing support of this device */
  3301. if (dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64)) &&
  3302. dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32))) {
  3303. dev_err(&pdev->dev, "No usable DMA addressing method\n");
  3304. rc = -EIO;
  3305. goto err_release_res;
  3306. }
  3307. netdev = alloc_etherdev(sizeof(struct et131x_adapter));
  3308. if (!netdev) {
  3309. dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
  3310. rc = -ENOMEM;
  3311. goto err_release_res;
  3312. }
  3313. netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
  3314. netdev->netdev_ops = &et131x_netdev_ops;
  3315. netdev->min_mtu = ET131X_MIN_MTU;
  3316. netdev->max_mtu = ET131X_MAX_MTU;
  3317. SET_NETDEV_DEV(netdev, &pdev->dev);
  3318. netdev->ethtool_ops = &et131x_ethtool_ops;
  3319. adapter = et131x_adapter_init(netdev, pdev);
  3320. rc = et131x_pci_init(adapter, pdev);
  3321. if (rc < 0)
  3322. goto err_free_dev;
  3323. /* Map the bus-relative registers to system virtual memory */
  3324. adapter->regs = pci_ioremap_bar(pdev, 0);
  3325. if (!adapter->regs) {
  3326. dev_err(&pdev->dev, "Cannot map device registers\n");
  3327. rc = -ENOMEM;
  3328. goto err_free_dev;
  3329. }
  3330. /* If Phy COMA mode was enabled when we went down, disable it here. */
  3331. writel(ET_PMCSR_INIT, &adapter->regs->global.pm_csr);
  3332. et131x_soft_reset(adapter);
  3333. et131x_disable_interrupts(adapter);
  3334. rc = et131x_adapter_memory_alloc(adapter);
  3335. if (rc < 0) {
  3336. dev_err(&pdev->dev, "Could not alloc adapter memory (DMA)\n");
  3337. goto err_iounmap;
  3338. }
  3339. et131x_init_send(adapter);
  3340. netif_napi_add(netdev, &adapter->napi, et131x_poll, 64);
  3341. ether_addr_copy(netdev->dev_addr, adapter->addr);
  3342. rc = -ENOMEM;
  3343. adapter->mii_bus = mdiobus_alloc();
  3344. if (!adapter->mii_bus) {
  3345. dev_err(&pdev->dev, "Alloc of mii_bus struct failed\n");
  3346. goto err_mem_free;
  3347. }
  3348. adapter->mii_bus->name = "et131x_eth_mii";
  3349. snprintf(adapter->mii_bus->id, MII_BUS_ID_SIZE, "%x",
  3350. (adapter->pdev->bus->number << 8) | adapter->pdev->devfn);
  3351. adapter->mii_bus->priv = netdev;
  3352. adapter->mii_bus->read = et131x_mdio_read;
  3353. adapter->mii_bus->write = et131x_mdio_write;
  3354. rc = mdiobus_register(adapter->mii_bus);
  3355. if (rc < 0) {
  3356. dev_err(&pdev->dev, "failed to register MII bus\n");
  3357. goto err_mdio_free;
  3358. }
  3359. rc = et131x_mii_probe(netdev);
  3360. if (rc < 0) {
  3361. dev_err(&pdev->dev, "failed to probe MII bus\n");
  3362. goto err_mdio_unregister;
  3363. }
  3364. et131x_adapter_setup(adapter);
  3365. /* Init variable for counting how long we do not have link status */
  3366. adapter->boot_coma = 0;
  3367. et1310_disable_phy_coma(adapter);
  3368. /* We can enable interrupts now
  3369. *
  3370. * NOTE - Because registration of interrupt handler is done in the
  3371. * device's open(), defer enabling device interrupts to that
  3372. * point
  3373. */
  3374. rc = register_netdev(netdev);
  3375. if (rc < 0) {
  3376. dev_err(&pdev->dev, "register_netdev() failed\n");
  3377. goto err_phy_disconnect;
  3378. }
  3379. /* Register the net_device struct with the PCI subsystem. Save a copy
  3380. * of the PCI config space for this device now that the device has
  3381. * been initialized, just in case it needs to be quickly restored.
  3382. */
  3383. pci_set_drvdata(pdev, netdev);
  3384. out:
  3385. return rc;
  3386. err_phy_disconnect:
  3387. phy_disconnect(netdev->phydev);
  3388. err_mdio_unregister:
  3389. mdiobus_unregister(adapter->mii_bus);
  3390. err_mdio_free:
  3391. mdiobus_free(adapter->mii_bus);
  3392. err_mem_free:
  3393. et131x_adapter_memory_free(adapter);
  3394. err_iounmap:
  3395. iounmap(adapter->regs);
  3396. err_free_dev:
  3397. pci_dev_put(pdev);
  3398. free_netdev(netdev);
  3399. err_release_res:
  3400. pci_release_regions(pdev);
  3401. err_disable:
  3402. pci_disable_device(pdev);
  3403. goto out;
  3404. }
  3405. static const struct pci_device_id et131x_pci_table[] = {
  3406. { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_GIG), 0UL},
  3407. { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_FAST), 0UL},
  3408. { 0,}
  3409. };
  3410. MODULE_DEVICE_TABLE(pci, et131x_pci_table);
  3411. static struct pci_driver et131x_driver = {
  3412. .name = DRIVER_NAME,
  3413. .id_table = et131x_pci_table,
  3414. .probe = et131x_pci_setup,
  3415. .remove = et131x_pci_remove,
  3416. .driver.pm = &et131x_pm_ops,
  3417. };
  3418. module_pci_driver(et131x_driver);