et131x.c 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065
  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 <[email protected]>
  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 <[email protected]>");
  79. MODULE_AUTHOR("Mark Einon <[email protected]>");
  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 = false;
  620. return -EIO;
  621. }
  622. }
  623. adapter->has_eeprom = true;
  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. /* If ET131X_PACKET_TYPE_MULTICAST is specified, then we provision
  820. * the multi-cast LIST. If it is NOT specified, (and "ALL" is not
  821. * specified) then we should pass NO multi-cast addresses to the
  822. * driver.
  823. */
  824. if (adapter->packet_filter & ET131X_PACKET_TYPE_MULTICAST) {
  825. int i;
  826. /* Loop through our multicast array and set up the device */
  827. for (i = 0; i < adapter->multicast_addr_count; i++) {
  828. u32 result;
  829. result = ether_crc(6, adapter->multicast_list[i]);
  830. result = (result & 0x3F800000) >> 23;
  831. if (result < 32) {
  832. hash1 |= (1 << result);
  833. } else if ((31 < result) && (result < 64)) {
  834. result -= 32;
  835. hash2 |= (1 << result);
  836. } else if ((63 < result) && (result < 96)) {
  837. result -= 64;
  838. hash3 |= (1 << result);
  839. } else {
  840. result -= 96;
  841. hash4 |= (1 << result);
  842. }
  843. }
  844. }
  845. /* Write out the new hash to the device */
  846. if (!et1310_in_phy_coma(adapter)) {
  847. writel(hash1, &rxmac->multi_hash1);
  848. writel(hash2, &rxmac->multi_hash2);
  849. writel(hash3, &rxmac->multi_hash3);
  850. writel(hash4, &rxmac->multi_hash4);
  851. }
  852. }
  853. static void et1310_setup_device_for_unicast(struct et131x_adapter *adapter)
  854. {
  855. struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
  856. u32 uni_pf1;
  857. u32 uni_pf2;
  858. u32 uni_pf3;
  859. /* Set up unicast packet filter reg 3 to be the first two octets of
  860. * the MAC address for both address
  861. *
  862. * Set up unicast packet filter reg 2 to be the octets 2 - 5 of the
  863. * MAC address for second address
  864. *
  865. * Set up unicast packet filter reg 3 to be the octets 2 - 5 of the
  866. * MAC address for first address
  867. */
  868. uni_pf3 = (adapter->addr[0] << ET_RX_UNI_PF_ADDR2_1_SHIFT) |
  869. (adapter->addr[1] << ET_RX_UNI_PF_ADDR2_2_SHIFT) |
  870. (adapter->addr[0] << ET_RX_UNI_PF_ADDR1_1_SHIFT) |
  871. adapter->addr[1];
  872. uni_pf2 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR2_3_SHIFT) |
  873. (adapter->addr[3] << ET_RX_UNI_PF_ADDR2_4_SHIFT) |
  874. (adapter->addr[4] << ET_RX_UNI_PF_ADDR2_5_SHIFT) |
  875. adapter->addr[5];
  876. uni_pf1 = (adapter->addr[2] << ET_RX_UNI_PF_ADDR1_3_SHIFT) |
  877. (adapter->addr[3] << ET_RX_UNI_PF_ADDR1_4_SHIFT) |
  878. (adapter->addr[4] << ET_RX_UNI_PF_ADDR1_5_SHIFT) |
  879. adapter->addr[5];
  880. if (!et1310_in_phy_coma(adapter)) {
  881. writel(uni_pf1, &rxmac->uni_pf_addr1);
  882. writel(uni_pf2, &rxmac->uni_pf_addr2);
  883. writel(uni_pf3, &rxmac->uni_pf_addr3);
  884. }
  885. }
  886. static void et1310_config_rxmac_regs(struct et131x_adapter *adapter)
  887. {
  888. struct rxmac_regs __iomem *rxmac = &adapter->regs->rxmac;
  889. struct phy_device *phydev = adapter->netdev->phydev;
  890. u32 sa_lo;
  891. u32 sa_hi = 0;
  892. u32 pf_ctrl = 0;
  893. u32 __iomem *wolw;
  894. /* Disable the MAC while it is being configured (also disable WOL) */
  895. writel(0x8, &rxmac->ctrl);
  896. /* Initialize WOL to disabled. */
  897. writel(0, &rxmac->crc0);
  898. writel(0, &rxmac->crc12);
  899. writel(0, &rxmac->crc34);
  900. /* We need to set the WOL mask0 - mask4 next. We initialize it to
  901. * its default Values of 0x00000000 because there are not WOL masks
  902. * as of this time.
  903. */
  904. for (wolw = &rxmac->mask0_word0; wolw <= &rxmac->mask4_word3; wolw++)
  905. writel(0, wolw);
  906. /* Lets setup the WOL Source Address */
  907. sa_lo = (adapter->addr[2] << ET_RX_WOL_LO_SA3_SHIFT) |
  908. (adapter->addr[3] << ET_RX_WOL_LO_SA4_SHIFT) |
  909. (adapter->addr[4] << ET_RX_WOL_LO_SA5_SHIFT) |
  910. adapter->addr[5];
  911. writel(sa_lo, &rxmac->sa_lo);
  912. sa_hi = (u32)(adapter->addr[0] << ET_RX_WOL_HI_SA1_SHIFT) |
  913. adapter->addr[1];
  914. writel(sa_hi, &rxmac->sa_hi);
  915. /* Disable all Packet Filtering */
  916. writel(0, &rxmac->pf_ctrl);
  917. /* Let's initialize the Unicast Packet filtering address */
  918. if (adapter->packet_filter & ET131X_PACKET_TYPE_DIRECTED) {
  919. et1310_setup_device_for_unicast(adapter);
  920. pf_ctrl |= ET_RX_PFCTRL_UNICST_FILTER_ENABLE;
  921. } else {
  922. writel(0, &rxmac->uni_pf_addr1);
  923. writel(0, &rxmac->uni_pf_addr2);
  924. writel(0, &rxmac->uni_pf_addr3);
  925. }
  926. /* Let's initialize the Multicast hash */
  927. if (!(adapter->packet_filter & ET131X_PACKET_TYPE_ALL_MULTICAST)) {
  928. pf_ctrl |= ET_RX_PFCTRL_MLTCST_FILTER_ENABLE;
  929. et1310_setup_device_for_multicast(adapter);
  930. }
  931. /* Runt packet filtering. Didn't work in version A silicon. */
  932. pf_ctrl |= (NIC_MIN_PACKET_SIZE + 4) << ET_RX_PFCTRL_MIN_PKT_SZ_SHIFT;
  933. pf_ctrl |= ET_RX_PFCTRL_FRAG_FILTER_ENABLE;
  934. if (adapter->registry_jumbo_packet > 8192)
  935. /* In order to transmit jumbo packets greater than 8k, the
  936. * FIFO between RxMAC and RxDMA needs to be reduced in size
  937. * to (16k - Jumbo packet size). In order to implement this,
  938. * we must use "cut through" mode in the RxMAC, which chops
  939. * packets down into segments which are (max_size * 16). In
  940. * this case we selected 256 bytes, since this is the size of
  941. * the PCI-Express TLP's that the 1310 uses.
  942. *
  943. * seg_en on, fc_en off, size 0x10
  944. */
  945. writel(0x41, &rxmac->mcif_ctrl_max_seg);
  946. else
  947. writel(0, &rxmac->mcif_ctrl_max_seg);
  948. writel(0, &rxmac->mcif_water_mark);
  949. writel(0, &rxmac->mif_ctrl);
  950. writel(0, &rxmac->space_avail);
  951. /* Initialize the mif_ctrl register
  952. * bit 3: Receive code error. One or more nibbles were signaled as
  953. * errors during the reception of the packet. Clear this
  954. * bit in Gigabit, set it in 100Mbit. This was derived
  955. * experimentally at UNH.
  956. * bit 4: Receive CRC error. The packet's CRC did not match the
  957. * internally generated CRC.
  958. * bit 5: Receive length check error. Indicates that frame length
  959. * field value in the packet does not match the actual data
  960. * byte length and is not a type field.
  961. * bit 16: Receive frame truncated.
  962. * bit 17: Drop packet enable
  963. */
  964. if (phydev && phydev->speed == SPEED_100)
  965. writel(0x30038, &rxmac->mif_ctrl);
  966. else
  967. writel(0x30030, &rxmac->mif_ctrl);
  968. /* Finally we initialize RxMac to be enabled & WOL disabled. Packet
  969. * filter is always enabled since it is where the runt packets are
  970. * supposed to be dropped. For version A silicon, runt packet
  971. * dropping doesn't work, so it is disabled in the pf_ctrl register,
  972. * but we still leave the packet filter on.
  973. */
  974. writel(pf_ctrl, &rxmac->pf_ctrl);
  975. writel(ET_RX_CTRL_RXMAC_ENABLE | ET_RX_CTRL_WOL_DISABLE, &rxmac->ctrl);
  976. }
  977. static void et1310_config_txmac_regs(struct et131x_adapter *adapter)
  978. {
  979. struct txmac_regs __iomem *txmac = &adapter->regs->txmac;
  980. /* We need to update the Control Frame Parameters
  981. * cfpt - control frame pause timer set to 64 (0x40)
  982. * cfep - control frame extended pause timer set to 0x0
  983. */
  984. if (adapter->flow == FLOW_NONE)
  985. writel(0, &txmac->cf_param);
  986. else
  987. writel(0x40, &txmac->cf_param);
  988. }
  989. static void et1310_config_macstat_regs(struct et131x_adapter *adapter)
  990. {
  991. struct macstat_regs __iomem *macstat = &adapter->regs->macstat;
  992. u32 __iomem *reg;
  993. /* initialize all the macstat registers to zero on the device */
  994. for (reg = &macstat->txrx_0_64_byte_frames;
  995. reg <= &macstat->carry_reg2; reg++)
  996. writel(0, reg);
  997. /* Unmask any counters that we want to track the overflow of.
  998. * Initially this will be all counters. It may become clear later
  999. * that we do not need to track all counters.
  1000. */
  1001. writel(0xFFFFBE32, &macstat->carry_reg1_mask);
  1002. writel(0xFFFE7E8B, &macstat->carry_reg2_mask);
  1003. }
  1004. static int et131x_phy_mii_read(struct et131x_adapter *adapter, u8 addr,
  1005. u8 reg, u16 *value)
  1006. {
  1007. struct mac_regs __iomem *mac = &adapter->regs->mac;
  1008. int status = 0;
  1009. u32 delay = 0;
  1010. u32 mii_addr;
  1011. u32 mii_cmd;
  1012. u32 mii_indicator;
  1013. /* Save a local copy of the registers we are dealing with so we can
  1014. * set them back
  1015. */
  1016. mii_addr = readl(&mac->mii_mgmt_addr);
  1017. mii_cmd = readl(&mac->mii_mgmt_cmd);
  1018. /* Stop the current operation */
  1019. writel(0, &mac->mii_mgmt_cmd);
  1020. /* Set up the register we need to read from on the correct PHY */
  1021. writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
  1022. writel(0x1, &mac->mii_mgmt_cmd);
  1023. do {
  1024. udelay(50);
  1025. delay++;
  1026. mii_indicator = readl(&mac->mii_mgmt_indicator);
  1027. } while ((mii_indicator & ET_MAC_MGMT_WAIT) && delay < 50);
  1028. /* If we hit the max delay, we could not read the register */
  1029. if (delay == 50) {
  1030. dev_warn(&adapter->pdev->dev,
  1031. "reg 0x%08x could not be read\n", reg);
  1032. dev_warn(&adapter->pdev->dev, "status is 0x%08x\n",
  1033. mii_indicator);
  1034. status = -EIO;
  1035. goto out;
  1036. }
  1037. /* If we hit here we were able to read the register and we need to
  1038. * return the value to the caller
  1039. */
  1040. *value = readl(&mac->mii_mgmt_stat) & ET_MAC_MIIMGMT_STAT_PHYCRTL_MASK;
  1041. out:
  1042. /* Stop the read operation */
  1043. writel(0, &mac->mii_mgmt_cmd);
  1044. /* set the registers we touched back to the state at which we entered
  1045. * this function
  1046. */
  1047. writel(mii_addr, &mac->mii_mgmt_addr);
  1048. writel(mii_cmd, &mac->mii_mgmt_cmd);
  1049. return status;
  1050. }
  1051. static int et131x_mii_read(struct et131x_adapter *adapter, u8 reg, u16 *value)
  1052. {
  1053. struct phy_device *phydev = adapter->netdev->phydev;
  1054. if (!phydev)
  1055. return -EIO;
  1056. return et131x_phy_mii_read(adapter, phydev->mdio.addr, reg, value);
  1057. }
  1058. static int et131x_mii_write(struct et131x_adapter *adapter, u8 addr, u8 reg,
  1059. u16 value)
  1060. {
  1061. struct mac_regs __iomem *mac = &adapter->regs->mac;
  1062. int status = 0;
  1063. u32 delay = 0;
  1064. u32 mii_addr;
  1065. u32 mii_cmd;
  1066. u32 mii_indicator;
  1067. /* Save a local copy of the registers we are dealing with so we can
  1068. * set them back
  1069. */
  1070. mii_addr = readl(&mac->mii_mgmt_addr);
  1071. mii_cmd = readl(&mac->mii_mgmt_cmd);
  1072. /* Stop the current operation */
  1073. writel(0, &mac->mii_mgmt_cmd);
  1074. /* Set up the register we need to write to on the correct PHY */
  1075. writel(ET_MAC_MII_ADDR(addr, reg), &mac->mii_mgmt_addr);
  1076. /* Add the value to write to the registers to the mac */
  1077. writel(value, &mac->mii_mgmt_ctrl);
  1078. do {
  1079. udelay(50);
  1080. delay++;
  1081. mii_indicator = readl(&mac->mii_mgmt_indicator);
  1082. } while ((mii_indicator & ET_MAC_MGMT_BUSY) && delay < 100);
  1083. /* If we hit the max delay, we could not write the register */
  1084. if (delay == 100) {
  1085. u16 tmp;
  1086. dev_warn(&adapter->pdev->dev,
  1087. "reg 0x%08x could not be written", reg);
  1088. dev_warn(&adapter->pdev->dev, "status is 0x%08x\n",
  1089. mii_indicator);
  1090. dev_warn(&adapter->pdev->dev, "command is 0x%08x\n",
  1091. readl(&mac->mii_mgmt_cmd));
  1092. et131x_mii_read(adapter, reg, &tmp);
  1093. status = -EIO;
  1094. }
  1095. /* Stop the write operation */
  1096. writel(0, &mac->mii_mgmt_cmd);
  1097. /* set the registers we touched back to the state at which we entered
  1098. * this function
  1099. */
  1100. writel(mii_addr, &mac->mii_mgmt_addr);
  1101. writel(mii_cmd, &mac->mii_mgmt_cmd);
  1102. return status;
  1103. }
  1104. static void et1310_phy_read_mii_bit(struct et131x_adapter *adapter,
  1105. u16 regnum,
  1106. u16 bitnum,
  1107. u8 *value)
  1108. {
  1109. u16 reg;
  1110. u16 mask = 1 << bitnum;
  1111. et131x_mii_read(adapter, regnum, &reg);
  1112. *value = (reg & mask) >> bitnum;
  1113. }
  1114. static void et1310_config_flow_control(struct et131x_adapter *adapter)
  1115. {
  1116. struct phy_device *phydev = adapter->netdev->phydev;
  1117. if (phydev->duplex == DUPLEX_HALF) {
  1118. adapter->flow = FLOW_NONE;
  1119. } else {
  1120. char remote_pause, remote_async_pause;
  1121. et1310_phy_read_mii_bit(adapter, 5, 10, &remote_pause);
  1122. et1310_phy_read_mii_bit(adapter, 5, 11, &remote_async_pause);
  1123. if (remote_pause && remote_async_pause) {
  1124. adapter->flow = adapter->wanted_flow;
  1125. } else if (remote_pause && !remote_async_pause) {
  1126. if (adapter->wanted_flow == FLOW_BOTH)
  1127. adapter->flow = FLOW_BOTH;
  1128. else
  1129. adapter->flow = FLOW_NONE;
  1130. } else if (!remote_pause && !remote_async_pause) {
  1131. adapter->flow = FLOW_NONE;
  1132. } else {
  1133. if (adapter->wanted_flow == FLOW_BOTH)
  1134. adapter->flow = FLOW_RXONLY;
  1135. else
  1136. adapter->flow = FLOW_NONE;
  1137. }
  1138. }
  1139. }
  1140. /* et1310_update_macstat_host_counters - Update local copy of the statistics */
  1141. static void et1310_update_macstat_host_counters(struct et131x_adapter *adapter)
  1142. {
  1143. struct ce_stats *stats = &adapter->stats;
  1144. struct macstat_regs __iomem *macstat =
  1145. &adapter->regs->macstat;
  1146. stats->tx_collisions += readl(&macstat->tx_total_collisions);
  1147. stats->tx_first_collisions += readl(&macstat->tx_single_collisions);
  1148. stats->tx_deferred += readl(&macstat->tx_deferred);
  1149. stats->tx_excessive_collisions +=
  1150. readl(&macstat->tx_multiple_collisions);
  1151. stats->tx_late_collisions += readl(&macstat->tx_late_collisions);
  1152. stats->tx_underflows += readl(&macstat->tx_undersize_frames);
  1153. stats->tx_max_pkt_errs += readl(&macstat->tx_oversize_frames);
  1154. stats->rx_align_errs += readl(&macstat->rx_align_errs);
  1155. stats->rx_crc_errs += readl(&macstat->rx_code_errs);
  1156. stats->rcvd_pkts_dropped += readl(&macstat->rx_drops);
  1157. stats->rx_overflows += readl(&macstat->rx_oversize_packets);
  1158. stats->rx_code_violations += readl(&macstat->rx_fcs_errs);
  1159. stats->rx_length_errs += readl(&macstat->rx_frame_len_errs);
  1160. stats->rx_other_errs += readl(&macstat->rx_fragment_packets);
  1161. }
  1162. /* et1310_handle_macstat_interrupt
  1163. *
  1164. * One of the MACSTAT counters has wrapped. Update the local copy of
  1165. * the statistics held in the adapter structure, checking the "wrap"
  1166. * bit for each counter.
  1167. */
  1168. static void et1310_handle_macstat_interrupt(struct et131x_adapter *adapter)
  1169. {
  1170. u32 carry_reg1;
  1171. u32 carry_reg2;
  1172. /* Read the interrupt bits from the register(s). These are Clear On
  1173. * Write.
  1174. */
  1175. carry_reg1 = readl(&adapter->regs->macstat.carry_reg1);
  1176. carry_reg2 = readl(&adapter->regs->macstat.carry_reg2);
  1177. writel(carry_reg1, &adapter->regs->macstat.carry_reg1);
  1178. writel(carry_reg2, &adapter->regs->macstat.carry_reg2);
  1179. /* We need to do update the host copy of all the MAC_STAT counters.
  1180. * For each counter, check it's overflow bit. If the overflow bit is
  1181. * set, then increment the host version of the count by one complete
  1182. * revolution of the counter. This routine is called when the counter
  1183. * block indicates that one of the counters has wrapped.
  1184. */
  1185. if (carry_reg1 & (1 << 14))
  1186. adapter->stats.rx_code_violations += COUNTER_WRAP_16_BIT;
  1187. if (carry_reg1 & (1 << 8))
  1188. adapter->stats.rx_align_errs += COUNTER_WRAP_12_BIT;
  1189. if (carry_reg1 & (1 << 7))
  1190. adapter->stats.rx_length_errs += COUNTER_WRAP_16_BIT;
  1191. if (carry_reg1 & (1 << 2))
  1192. adapter->stats.rx_other_errs += COUNTER_WRAP_16_BIT;
  1193. if (carry_reg1 & (1 << 6))
  1194. adapter->stats.rx_crc_errs += COUNTER_WRAP_16_BIT;
  1195. if (carry_reg1 & (1 << 3))
  1196. adapter->stats.rx_overflows += COUNTER_WRAP_16_BIT;
  1197. if (carry_reg1 & (1 << 0))
  1198. adapter->stats.rcvd_pkts_dropped += COUNTER_WRAP_16_BIT;
  1199. if (carry_reg2 & (1 << 16))
  1200. adapter->stats.tx_max_pkt_errs += COUNTER_WRAP_12_BIT;
  1201. if (carry_reg2 & (1 << 15))
  1202. adapter->stats.tx_underflows += COUNTER_WRAP_12_BIT;
  1203. if (carry_reg2 & (1 << 6))
  1204. adapter->stats.tx_first_collisions += COUNTER_WRAP_12_BIT;
  1205. if (carry_reg2 & (1 << 8))
  1206. adapter->stats.tx_deferred += COUNTER_WRAP_12_BIT;
  1207. if (carry_reg2 & (1 << 5))
  1208. adapter->stats.tx_excessive_collisions += COUNTER_WRAP_12_BIT;
  1209. if (carry_reg2 & (1 << 4))
  1210. adapter->stats.tx_late_collisions += COUNTER_WRAP_12_BIT;
  1211. if (carry_reg2 & (1 << 2))
  1212. adapter->stats.tx_collisions += COUNTER_WRAP_12_BIT;
  1213. }
  1214. static int et131x_mdio_read(struct mii_bus *bus, int phy_addr, int reg)
  1215. {
  1216. struct net_device *netdev = bus->priv;
  1217. struct et131x_adapter *adapter = netdev_priv(netdev);
  1218. u16 value;
  1219. int ret;
  1220. ret = et131x_phy_mii_read(adapter, phy_addr, reg, &value);
  1221. if (ret < 0)
  1222. return ret;
  1223. return value;
  1224. }
  1225. static int et131x_mdio_write(struct mii_bus *bus, int phy_addr,
  1226. int reg, u16 value)
  1227. {
  1228. struct net_device *netdev = bus->priv;
  1229. struct et131x_adapter *adapter = netdev_priv(netdev);
  1230. return et131x_mii_write(adapter, phy_addr, reg, value);
  1231. }
  1232. /* et1310_phy_power_switch - PHY power control
  1233. * @adapter: device to control
  1234. * @down: true for off/false for back on
  1235. *
  1236. * one hundred, ten, one thousand megs
  1237. * How would you like to have your LAN accessed
  1238. * Can't you see that this code processed
  1239. * Phy power, phy power..
  1240. */
  1241. static void et1310_phy_power_switch(struct et131x_adapter *adapter, bool down)
  1242. {
  1243. u16 data;
  1244. struct phy_device *phydev = adapter->netdev->phydev;
  1245. et131x_mii_read(adapter, MII_BMCR, &data);
  1246. data &= ~BMCR_PDOWN;
  1247. if (down)
  1248. data |= BMCR_PDOWN;
  1249. et131x_mii_write(adapter, phydev->mdio.addr, MII_BMCR, data);
  1250. }
  1251. /* et131x_xcvr_init - Init the phy if we are setting it into force mode */
  1252. static void et131x_xcvr_init(struct et131x_adapter *adapter)
  1253. {
  1254. u16 lcr2;
  1255. struct phy_device *phydev = adapter->netdev->phydev;
  1256. /* Set the LED behavior such that LED 1 indicates speed (off =
  1257. * 10Mbits, blink = 100Mbits, on = 1000Mbits) and LED 2 indicates
  1258. * link and activity (on for link, blink off for activity).
  1259. *
  1260. * NOTE: Some customizations have been added here for specific
  1261. * vendors; The LED behavior is now determined by vendor data in the
  1262. * EEPROM. However, the above description is the default.
  1263. */
  1264. if ((adapter->eeprom_data[1] & 0x4) == 0) {
  1265. et131x_mii_read(adapter, PHY_LED_2, &lcr2);
  1266. lcr2 &= (ET_LED2_LED_100TX | ET_LED2_LED_1000T);
  1267. lcr2 |= (LED_VAL_LINKON_ACTIVE << LED_LINK_SHIFT);
  1268. if ((adapter->eeprom_data[1] & 0x8) == 0)
  1269. lcr2 |= (LED_VAL_1000BT_100BTX << LED_TXRX_SHIFT);
  1270. else
  1271. lcr2 |= (LED_VAL_LINKON << LED_TXRX_SHIFT);
  1272. et131x_mii_write(adapter, phydev->mdio.addr, PHY_LED_2, lcr2);
  1273. }
  1274. }
  1275. /* et131x_configure_global_regs - configure JAGCore global regs */
  1276. static void et131x_configure_global_regs(struct et131x_adapter *adapter)
  1277. {
  1278. struct global_regs __iomem *regs = &adapter->regs->global;
  1279. writel(0, &regs->rxq_start_addr);
  1280. writel(INTERNAL_MEM_SIZE - 1, &regs->txq_end_addr);
  1281. if (adapter->registry_jumbo_packet < 2048) {
  1282. /* Tx / RxDMA and Tx/Rx MAC interfaces have a 1k word
  1283. * block of RAM that the driver can split between Tx
  1284. * and Rx as it desires. Our default is to split it
  1285. * 50/50:
  1286. */
  1287. writel(PARM_RX_MEM_END_DEF, &regs->rxq_end_addr);
  1288. writel(PARM_RX_MEM_END_DEF + 1, &regs->txq_start_addr);
  1289. } else if (adapter->registry_jumbo_packet < 8192) {
  1290. /* For jumbo packets > 2k but < 8k, split 50-50. */
  1291. writel(INTERNAL_MEM_RX_OFFSET, &regs->rxq_end_addr);
  1292. writel(INTERNAL_MEM_RX_OFFSET + 1, &regs->txq_start_addr);
  1293. } else {
  1294. /* 9216 is the only packet size greater than 8k that
  1295. * is available. The Tx buffer has to be big enough
  1296. * for one whole packet on the Tx side. We'll make
  1297. * the Tx 9408, and give the rest to Rx
  1298. */
  1299. writel(0x01b3, &regs->rxq_end_addr);
  1300. writel(0x01b4, &regs->txq_start_addr);
  1301. }
  1302. /* Initialize the loopback register. Disable all loopbacks. */
  1303. writel(0, &regs->loopback);
  1304. writel(0, &regs->msi_config);
  1305. /* By default, disable the watchdog timer. It will be enabled when
  1306. * a packet is queued.
  1307. */
  1308. writel(0, &regs->watchdog_timer);
  1309. }
  1310. /* et131x_config_rx_dma_regs - Start of Rx_DMA init sequence */
  1311. static void et131x_config_rx_dma_regs(struct et131x_adapter *adapter)
  1312. {
  1313. struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
  1314. struct rx_ring *rx_local = &adapter->rx_ring;
  1315. struct fbr_desc *fbr_entry;
  1316. u32 entry;
  1317. u32 psr_num_des;
  1318. unsigned long flags;
  1319. u8 id;
  1320. et131x_rx_dma_disable(adapter);
  1321. /* Load the completion writeback physical address */
  1322. writel(upper_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_hi);
  1323. writel(lower_32_bits(rx_local->rx_status_bus), &rx_dma->dma_wb_base_lo);
  1324. memset(rx_local->rx_status_block, 0, sizeof(struct rx_status_block));
  1325. /* Set the address and parameters of the packet status ring */
  1326. writel(upper_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_hi);
  1327. writel(lower_32_bits(rx_local->ps_ring_physaddr), &rx_dma->psr_base_lo);
  1328. writel(rx_local->psr_entries - 1, &rx_dma->psr_num_des);
  1329. writel(0, &rx_dma->psr_full_offset);
  1330. psr_num_des = readl(&rx_dma->psr_num_des) & ET_RXDMA_PSR_NUM_DES_MASK;
  1331. writel((psr_num_des * LO_MARK_PERCENT_FOR_PSR) / 100,
  1332. &rx_dma->psr_min_des);
  1333. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1334. /* These local variables track the PSR in the adapter structure */
  1335. rx_local->local_psr_full = 0;
  1336. for (id = 0; id < NUM_FBRS; id++) {
  1337. u32 __iomem *num_des;
  1338. u32 __iomem *full_offset;
  1339. u32 __iomem *min_des;
  1340. u32 __iomem *base_hi;
  1341. u32 __iomem *base_lo;
  1342. struct fbr_lookup *fbr = rx_local->fbr[id];
  1343. if (id == 0) {
  1344. num_des = &rx_dma->fbr0_num_des;
  1345. full_offset = &rx_dma->fbr0_full_offset;
  1346. min_des = &rx_dma->fbr0_min_des;
  1347. base_hi = &rx_dma->fbr0_base_hi;
  1348. base_lo = &rx_dma->fbr0_base_lo;
  1349. } else {
  1350. num_des = &rx_dma->fbr1_num_des;
  1351. full_offset = &rx_dma->fbr1_full_offset;
  1352. min_des = &rx_dma->fbr1_min_des;
  1353. base_hi = &rx_dma->fbr1_base_hi;
  1354. base_lo = &rx_dma->fbr1_base_lo;
  1355. }
  1356. /* Now's the best time to initialize FBR contents */
  1357. fbr_entry = fbr->ring_virtaddr;
  1358. for (entry = 0; entry < fbr->num_entries; entry++) {
  1359. fbr_entry->addr_hi = fbr->bus_high[entry];
  1360. fbr_entry->addr_lo = fbr->bus_low[entry];
  1361. fbr_entry->word2 = entry;
  1362. fbr_entry++;
  1363. }
  1364. /* Set the address and parameters of Free buffer ring 1 and 0 */
  1365. writel(upper_32_bits(fbr->ring_physaddr), base_hi);
  1366. writel(lower_32_bits(fbr->ring_physaddr), base_lo);
  1367. writel(fbr->num_entries - 1, num_des);
  1368. writel(ET_DMA10_WRAP, full_offset);
  1369. /* This variable tracks the free buffer ring 1 full position,
  1370. * so it has to match the above.
  1371. */
  1372. fbr->local_full = ET_DMA10_WRAP;
  1373. writel(((fbr->num_entries * LO_MARK_PERCENT_FOR_RX) / 100) - 1,
  1374. min_des);
  1375. }
  1376. /* Program the number of packets we will receive before generating an
  1377. * interrupt.
  1378. * For version B silicon, this value gets updated once autoneg is
  1379. *complete.
  1380. */
  1381. writel(PARM_RX_NUM_BUFS_DEF, &rx_dma->num_pkt_done);
  1382. /* The "time_done" is not working correctly to coalesce interrupts
  1383. * after a given time period, but rather is giving us an interrupt
  1384. * regardless of whether we have received packets.
  1385. * This value gets updated once autoneg is complete.
  1386. */
  1387. writel(PARM_RX_TIME_INT_DEF, &rx_dma->max_pkt_time);
  1388. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1389. }
  1390. /* et131x_config_tx_dma_regs - Set up the tx dma section of the JAGCore.
  1391. *
  1392. * Configure the transmit engine with the ring buffers we have created
  1393. * and prepare it for use.
  1394. */
  1395. static void et131x_config_tx_dma_regs(struct et131x_adapter *adapter)
  1396. {
  1397. struct txdma_regs __iomem *txdma = &adapter->regs->txdma;
  1398. struct tx_ring *tx_ring = &adapter->tx_ring;
  1399. /* Load the hardware with the start of the transmit descriptor ring. */
  1400. writel(upper_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_hi);
  1401. writel(lower_32_bits(tx_ring->tx_desc_ring_pa), &txdma->pr_base_lo);
  1402. /* Initialise the transmit DMA engine */
  1403. writel(NUM_DESC_PER_RING_TX - 1, &txdma->pr_num_des);
  1404. /* Load the completion writeback physical address */
  1405. writel(upper_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_hi);
  1406. writel(lower_32_bits(tx_ring->tx_status_pa), &txdma->dma_wb_base_lo);
  1407. *tx_ring->tx_status = 0;
  1408. writel(0, &txdma->service_request);
  1409. tx_ring->send_idx = 0;
  1410. }
  1411. /* et131x_adapter_setup - Set the adapter up as per cassini+ documentation */
  1412. static void et131x_adapter_setup(struct et131x_adapter *adapter)
  1413. {
  1414. et131x_configure_global_regs(adapter);
  1415. et1310_config_mac_regs1(adapter);
  1416. /* Configure the MMC registers */
  1417. /* All we need to do is initialize the Memory Control Register */
  1418. writel(ET_MMC_ENABLE, &adapter->regs->mmc.mmc_ctrl);
  1419. et1310_config_rxmac_regs(adapter);
  1420. et1310_config_txmac_regs(adapter);
  1421. et131x_config_rx_dma_regs(adapter);
  1422. et131x_config_tx_dma_regs(adapter);
  1423. et1310_config_macstat_regs(adapter);
  1424. et1310_phy_power_switch(adapter, 0);
  1425. et131x_xcvr_init(adapter);
  1426. }
  1427. /* et131x_soft_reset - Issue soft reset to the hardware, complete for ET1310 */
  1428. static void et131x_soft_reset(struct et131x_adapter *adapter)
  1429. {
  1430. u32 reg;
  1431. /* Disable MAC Core */
  1432. reg = ET_MAC_CFG1_SOFT_RESET | ET_MAC_CFG1_SIM_RESET |
  1433. ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
  1434. ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
  1435. writel(reg, &adapter->regs->mac.cfg1);
  1436. reg = ET_RESET_ALL;
  1437. writel(reg, &adapter->regs->global.sw_reset);
  1438. reg = ET_MAC_CFG1_RESET_RXMC | ET_MAC_CFG1_RESET_TXMC |
  1439. ET_MAC_CFG1_RESET_RXFUNC | ET_MAC_CFG1_RESET_TXFUNC;
  1440. writel(reg, &adapter->regs->mac.cfg1);
  1441. writel(0, &adapter->regs->mac.cfg1);
  1442. }
  1443. static void et131x_enable_interrupts(struct et131x_adapter *adapter)
  1444. {
  1445. u32 mask;
  1446. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
  1447. mask = INT_MASK_ENABLE;
  1448. else
  1449. mask = INT_MASK_ENABLE_NO_FLOW;
  1450. writel(mask, &adapter->regs->global.int_mask);
  1451. }
  1452. static void et131x_disable_interrupts(struct et131x_adapter *adapter)
  1453. {
  1454. writel(INT_MASK_DISABLE, &adapter->regs->global.int_mask);
  1455. }
  1456. static void et131x_tx_dma_disable(struct et131x_adapter *adapter)
  1457. {
  1458. /* Setup the transmit dma configuration register */
  1459. writel(ET_TXDMA_CSR_HALT | ET_TXDMA_SNGL_EPKT,
  1460. &adapter->regs->txdma.csr);
  1461. }
  1462. static void et131x_enable_txrx(struct net_device *netdev)
  1463. {
  1464. struct et131x_adapter *adapter = netdev_priv(netdev);
  1465. et131x_rx_dma_enable(adapter);
  1466. et131x_tx_dma_enable(adapter);
  1467. if (adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE)
  1468. et131x_enable_interrupts(adapter);
  1469. netif_start_queue(netdev);
  1470. }
  1471. static void et131x_disable_txrx(struct net_device *netdev)
  1472. {
  1473. struct et131x_adapter *adapter = netdev_priv(netdev);
  1474. netif_stop_queue(netdev);
  1475. et131x_rx_dma_disable(adapter);
  1476. et131x_tx_dma_disable(adapter);
  1477. et131x_disable_interrupts(adapter);
  1478. }
  1479. static void et131x_init_send(struct et131x_adapter *adapter)
  1480. {
  1481. int i;
  1482. struct tx_ring *tx_ring = &adapter->tx_ring;
  1483. struct tcb *tcb = tx_ring->tcb_ring;
  1484. tx_ring->tcb_qhead = tcb;
  1485. memset(tcb, 0, sizeof(struct tcb) * NUM_TCB);
  1486. for (i = 0; i < NUM_TCB; i++) {
  1487. tcb->next = tcb + 1;
  1488. tcb++;
  1489. }
  1490. tcb--;
  1491. tx_ring->tcb_qtail = tcb;
  1492. tcb->next = NULL;
  1493. /* Curr send queue should now be empty */
  1494. tx_ring->send_head = NULL;
  1495. tx_ring->send_tail = NULL;
  1496. }
  1497. /* et1310_enable_phy_coma
  1498. *
  1499. * driver receive an phy status change interrupt while in D0 and check that
  1500. * phy_status is down.
  1501. *
  1502. * -- gate off JAGCore;
  1503. * -- set gigE PHY in Coma mode
  1504. * -- wake on phy_interrupt; Perform software reset JAGCore,
  1505. * re-initialize jagcore and gigE PHY
  1506. */
  1507. static void et1310_enable_phy_coma(struct et131x_adapter *adapter)
  1508. {
  1509. u32 pmcsr = readl(&adapter->regs->global.pm_csr);
  1510. /* Stop sending packets. */
  1511. adapter->flags |= FMP_ADAPTER_LOWER_POWER;
  1512. /* Wait for outstanding Receive packets */
  1513. et131x_disable_txrx(adapter->netdev);
  1514. /* Gate off JAGCore 3 clock domains */
  1515. pmcsr &= ~ET_PMCSR_INIT;
  1516. writel(pmcsr, &adapter->regs->global.pm_csr);
  1517. /* Program gigE PHY in to Coma mode */
  1518. pmcsr |= ET_PM_PHY_SW_COMA;
  1519. writel(pmcsr, &adapter->regs->global.pm_csr);
  1520. }
  1521. static void et1310_disable_phy_coma(struct et131x_adapter *adapter)
  1522. {
  1523. u32 pmcsr;
  1524. pmcsr = readl(&adapter->regs->global.pm_csr);
  1525. /* Disable phy_sw_coma register and re-enable JAGCore clocks */
  1526. pmcsr |= ET_PMCSR_INIT;
  1527. pmcsr &= ~ET_PM_PHY_SW_COMA;
  1528. writel(pmcsr, &adapter->regs->global.pm_csr);
  1529. /* Restore the GbE PHY speed and duplex modes;
  1530. * Reset JAGCore; re-configure and initialize JAGCore and gigE PHY
  1531. */
  1532. /* Re-initialize the send structures */
  1533. et131x_init_send(adapter);
  1534. /* Bring the device back to the state it was during init prior to
  1535. * autonegotiation being complete. This way, when we get the auto-neg
  1536. * complete interrupt, we can complete init by calling ConfigMacREGS2.
  1537. */
  1538. et131x_soft_reset(adapter);
  1539. et131x_adapter_setup(adapter);
  1540. /* Allow Tx to restart */
  1541. adapter->flags &= ~FMP_ADAPTER_LOWER_POWER;
  1542. et131x_enable_txrx(adapter->netdev);
  1543. }
  1544. static inline u32 bump_free_buff_ring(u32 *free_buff_ring, u32 limit)
  1545. {
  1546. u32 tmp_free_buff_ring = *free_buff_ring;
  1547. tmp_free_buff_ring++;
  1548. /* This works for all cases where limit < 1024. The 1023 case
  1549. * works because 1023++ is 1024 which means the if condition is not
  1550. * taken but the carry of the bit into the wrap bit toggles the wrap
  1551. * value correctly
  1552. */
  1553. if ((tmp_free_buff_ring & ET_DMA10_MASK) > limit) {
  1554. tmp_free_buff_ring &= ~ET_DMA10_MASK;
  1555. tmp_free_buff_ring ^= ET_DMA10_WRAP;
  1556. }
  1557. /* For the 1023 case */
  1558. tmp_free_buff_ring &= (ET_DMA10_MASK | ET_DMA10_WRAP);
  1559. *free_buff_ring = tmp_free_buff_ring;
  1560. return tmp_free_buff_ring;
  1561. }
  1562. /* et131x_rx_dma_memory_alloc
  1563. *
  1564. * Allocates Free buffer ring 1 for sure, free buffer ring 0 if required,
  1565. * and the Packet Status Ring.
  1566. */
  1567. static int et131x_rx_dma_memory_alloc(struct et131x_adapter *adapter)
  1568. {
  1569. u8 id;
  1570. u32 i, j;
  1571. u32 bufsize;
  1572. u32 psr_size;
  1573. u32 fbr_chunksize;
  1574. struct rx_ring *rx_ring = &adapter->rx_ring;
  1575. struct fbr_lookup *fbr;
  1576. /* Alloc memory for the lookup table */
  1577. rx_ring->fbr[0] = kzalloc(sizeof(*fbr), GFP_KERNEL);
  1578. if (rx_ring->fbr[0] == NULL)
  1579. return -ENOMEM;
  1580. rx_ring->fbr[1] = kzalloc(sizeof(*fbr), GFP_KERNEL);
  1581. if (rx_ring->fbr[1] == NULL)
  1582. return -ENOMEM;
  1583. /* The first thing we will do is configure the sizes of the buffer
  1584. * rings. These will change based on jumbo packet support. Larger
  1585. * jumbo packets increases the size of each entry in FBR0, and the
  1586. * number of entries in FBR0, while at the same time decreasing the
  1587. * number of entries in FBR1.
  1588. *
  1589. * FBR1 holds "large" frames, FBR0 holds "small" frames. If FBR1
  1590. * entries are huge in order to accommodate a "jumbo" frame, then it
  1591. * will have less entries. Conversely, FBR1 will now be relied upon
  1592. * to carry more "normal" frames, thus it's entry size also increases
  1593. * and the number of entries goes up too (since it now carries
  1594. * "small" + "regular" packets.
  1595. *
  1596. * In this scheme, we try to maintain 512 entries between the two
  1597. * rings. Also, FBR1 remains a constant size - when it's size doubles
  1598. * the number of entries halves. FBR0 increases in size, however.
  1599. */
  1600. if (adapter->registry_jumbo_packet < 2048) {
  1601. rx_ring->fbr[0]->buffsize = 256;
  1602. rx_ring->fbr[0]->num_entries = 512;
  1603. rx_ring->fbr[1]->buffsize = 2048;
  1604. rx_ring->fbr[1]->num_entries = 512;
  1605. } else if (adapter->registry_jumbo_packet < 4096) {
  1606. rx_ring->fbr[0]->buffsize = 512;
  1607. rx_ring->fbr[0]->num_entries = 1024;
  1608. rx_ring->fbr[1]->buffsize = 4096;
  1609. rx_ring->fbr[1]->num_entries = 512;
  1610. } else {
  1611. rx_ring->fbr[0]->buffsize = 1024;
  1612. rx_ring->fbr[0]->num_entries = 768;
  1613. rx_ring->fbr[1]->buffsize = 16384;
  1614. rx_ring->fbr[1]->num_entries = 128;
  1615. }
  1616. rx_ring->psr_entries = rx_ring->fbr[0]->num_entries +
  1617. rx_ring->fbr[1]->num_entries;
  1618. for (id = 0; id < NUM_FBRS; id++) {
  1619. fbr = rx_ring->fbr[id];
  1620. /* Allocate an area of memory for Free Buffer Ring */
  1621. bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
  1622. fbr->ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
  1623. bufsize,
  1624. &fbr->ring_physaddr,
  1625. GFP_KERNEL);
  1626. if (!fbr->ring_virtaddr) {
  1627. dev_err(&adapter->pdev->dev,
  1628. "Cannot alloc memory for Free Buffer Ring %d\n",
  1629. id);
  1630. return -ENOMEM;
  1631. }
  1632. }
  1633. for (id = 0; id < NUM_FBRS; id++) {
  1634. fbr = rx_ring->fbr[id];
  1635. fbr_chunksize = (FBR_CHUNKS * fbr->buffsize);
  1636. for (i = 0; i < fbr->num_entries / FBR_CHUNKS; i++) {
  1637. dma_addr_t fbr_physaddr;
  1638. fbr->mem_virtaddrs[i] = dma_alloc_coherent(
  1639. &adapter->pdev->dev, fbr_chunksize,
  1640. &fbr->mem_physaddrs[i],
  1641. GFP_KERNEL);
  1642. if (!fbr->mem_virtaddrs[i]) {
  1643. dev_err(&adapter->pdev->dev,
  1644. "Could not alloc memory\n");
  1645. return -ENOMEM;
  1646. }
  1647. /* See NOTE in "Save Physical Address" comment above */
  1648. fbr_physaddr = fbr->mem_physaddrs[i];
  1649. for (j = 0; j < FBR_CHUNKS; j++) {
  1650. u32 k = (i * FBR_CHUNKS) + j;
  1651. /* Save the Virtual address of this index for
  1652. * quick access later
  1653. */
  1654. fbr->virt[k] = (u8 *)fbr->mem_virtaddrs[i] +
  1655. (j * fbr->buffsize);
  1656. /* now store the physical address in the
  1657. * descriptor so the device can access it
  1658. */
  1659. fbr->bus_high[k] = upper_32_bits(fbr_physaddr);
  1660. fbr->bus_low[k] = lower_32_bits(fbr_physaddr);
  1661. fbr_physaddr += fbr->buffsize;
  1662. }
  1663. }
  1664. }
  1665. /* Allocate an area of memory for FIFO of Packet Status ring entries */
  1666. psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
  1667. rx_ring->ps_ring_virtaddr = dma_alloc_coherent(&adapter->pdev->dev,
  1668. psr_size,
  1669. &rx_ring->ps_ring_physaddr,
  1670. GFP_KERNEL);
  1671. if (!rx_ring->ps_ring_virtaddr) {
  1672. dev_err(&adapter->pdev->dev,
  1673. "Cannot alloc memory for Packet Status Ring\n");
  1674. return -ENOMEM;
  1675. }
  1676. /* Allocate an area of memory for writeback of status information */
  1677. rx_ring->rx_status_block = dma_alloc_coherent(&adapter->pdev->dev,
  1678. sizeof(struct rx_status_block),
  1679. &rx_ring->rx_status_bus,
  1680. GFP_KERNEL);
  1681. if (!rx_ring->rx_status_block) {
  1682. dev_err(&adapter->pdev->dev,
  1683. "Cannot alloc memory for Status Block\n");
  1684. return -ENOMEM;
  1685. }
  1686. rx_ring->num_rfd = NIC_DEFAULT_NUM_RFD;
  1687. /* The RFDs are going to be put on lists later on, so initialize the
  1688. * lists now.
  1689. */
  1690. INIT_LIST_HEAD(&rx_ring->recv_list);
  1691. return 0;
  1692. }
  1693. static void et131x_rx_dma_memory_free(struct et131x_adapter *adapter)
  1694. {
  1695. u8 id;
  1696. u32 ii;
  1697. u32 bufsize;
  1698. u32 psr_size;
  1699. struct rfd *rfd;
  1700. struct rx_ring *rx_ring = &adapter->rx_ring;
  1701. struct fbr_lookup *fbr;
  1702. /* Free RFDs and associated packet descriptors */
  1703. WARN_ON(rx_ring->num_ready_recv != rx_ring->num_rfd);
  1704. while (!list_empty(&rx_ring->recv_list)) {
  1705. rfd = list_entry(rx_ring->recv_list.next,
  1706. struct rfd, list_node);
  1707. list_del(&rfd->list_node);
  1708. rfd->skb = NULL;
  1709. kfree(rfd);
  1710. }
  1711. /* Free Free Buffer Rings */
  1712. for (id = 0; id < NUM_FBRS; id++) {
  1713. fbr = rx_ring->fbr[id];
  1714. if (!fbr || !fbr->ring_virtaddr)
  1715. continue;
  1716. /* First the packet memory */
  1717. for (ii = 0; ii < fbr->num_entries / FBR_CHUNKS; ii++) {
  1718. if (fbr->mem_virtaddrs[ii]) {
  1719. bufsize = fbr->buffsize * FBR_CHUNKS;
  1720. dma_free_coherent(&adapter->pdev->dev,
  1721. bufsize,
  1722. fbr->mem_virtaddrs[ii],
  1723. fbr->mem_physaddrs[ii]);
  1724. fbr->mem_virtaddrs[ii] = NULL;
  1725. }
  1726. }
  1727. bufsize = sizeof(struct fbr_desc) * fbr->num_entries;
  1728. dma_free_coherent(&adapter->pdev->dev,
  1729. bufsize,
  1730. fbr->ring_virtaddr,
  1731. fbr->ring_physaddr);
  1732. fbr->ring_virtaddr = NULL;
  1733. }
  1734. /* Free Packet Status Ring */
  1735. if (rx_ring->ps_ring_virtaddr) {
  1736. psr_size = sizeof(struct pkt_stat_desc) * rx_ring->psr_entries;
  1737. dma_free_coherent(&adapter->pdev->dev, psr_size,
  1738. rx_ring->ps_ring_virtaddr,
  1739. rx_ring->ps_ring_physaddr);
  1740. rx_ring->ps_ring_virtaddr = NULL;
  1741. }
  1742. /* Free area of memory for the writeback of status information */
  1743. if (rx_ring->rx_status_block) {
  1744. dma_free_coherent(&adapter->pdev->dev,
  1745. sizeof(struct rx_status_block),
  1746. rx_ring->rx_status_block,
  1747. rx_ring->rx_status_bus);
  1748. rx_ring->rx_status_block = NULL;
  1749. }
  1750. /* Free the FBR Lookup Table */
  1751. kfree(rx_ring->fbr[0]);
  1752. kfree(rx_ring->fbr[1]);
  1753. /* Reset Counters */
  1754. rx_ring->num_ready_recv = 0;
  1755. }
  1756. /* et131x_init_recv - Initialize receive data structures */
  1757. static int et131x_init_recv(struct et131x_adapter *adapter)
  1758. {
  1759. struct rfd *rfd;
  1760. u32 rfdct;
  1761. struct rx_ring *rx_ring = &adapter->rx_ring;
  1762. /* Setup each RFD */
  1763. for (rfdct = 0; rfdct < rx_ring->num_rfd; rfdct++) {
  1764. rfd = kzalloc(sizeof(*rfd), GFP_ATOMIC | GFP_DMA);
  1765. if (!rfd)
  1766. return -ENOMEM;
  1767. rfd->skb = NULL;
  1768. /* Add this RFD to the recv_list */
  1769. list_add_tail(&rfd->list_node, &rx_ring->recv_list);
  1770. /* Increment the available RFD's */
  1771. rx_ring->num_ready_recv++;
  1772. }
  1773. return 0;
  1774. }
  1775. /* et131x_set_rx_dma_timer - Set the heartbeat timer according to line rate */
  1776. static void et131x_set_rx_dma_timer(struct et131x_adapter *adapter)
  1777. {
  1778. struct phy_device *phydev = adapter->netdev->phydev;
  1779. /* For version B silicon, we do not use the RxDMA timer for 10 and 100
  1780. * Mbits/s line rates. We do not enable and RxDMA interrupt coalescing.
  1781. */
  1782. if ((phydev->speed == SPEED_100) || (phydev->speed == SPEED_10)) {
  1783. writel(0, &adapter->regs->rxdma.max_pkt_time);
  1784. writel(1, &adapter->regs->rxdma.num_pkt_done);
  1785. }
  1786. }
  1787. /* nic_return_rfd - Recycle a RFD and put it back onto the receive list */
  1788. static void nic_return_rfd(struct et131x_adapter *adapter, struct rfd *rfd)
  1789. {
  1790. struct rx_ring *rx_local = &adapter->rx_ring;
  1791. struct rxdma_regs __iomem *rx_dma = &adapter->regs->rxdma;
  1792. u16 buff_index = rfd->bufferindex;
  1793. u8 ring_index = rfd->ringindex;
  1794. unsigned long flags;
  1795. struct fbr_lookup *fbr = rx_local->fbr[ring_index];
  1796. /* We don't use any of the OOB data besides status. Otherwise, we
  1797. * need to clean up OOB data
  1798. */
  1799. if (buff_index < fbr->num_entries) {
  1800. u32 free_buff_ring;
  1801. u32 __iomem *offset;
  1802. struct fbr_desc *next;
  1803. if (ring_index == 0)
  1804. offset = &rx_dma->fbr0_full_offset;
  1805. else
  1806. offset = &rx_dma->fbr1_full_offset;
  1807. next = (struct fbr_desc *)(fbr->ring_virtaddr) +
  1808. INDEX10(fbr->local_full);
  1809. /* Handle the Free Buffer Ring advancement here. Write
  1810. * the PA / Buffer Index for the returned buffer into
  1811. * the oldest (next to be freed)FBR entry
  1812. */
  1813. next->addr_hi = fbr->bus_high[buff_index];
  1814. next->addr_lo = fbr->bus_low[buff_index];
  1815. next->word2 = buff_index;
  1816. free_buff_ring = bump_free_buff_ring(&fbr->local_full,
  1817. fbr->num_entries - 1);
  1818. writel(free_buff_ring, offset);
  1819. } else {
  1820. dev_err(&adapter->pdev->dev,
  1821. "%s illegal Buffer Index returned\n", __func__);
  1822. }
  1823. /* The processing on this RFD is done, so put it back on the tail of
  1824. * our list
  1825. */
  1826. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1827. list_add_tail(&rfd->list_node, &rx_local->recv_list);
  1828. rx_local->num_ready_recv++;
  1829. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1830. WARN_ON(rx_local->num_ready_recv > rx_local->num_rfd);
  1831. }
  1832. /* nic_rx_pkts - Checks the hardware for available packets
  1833. *
  1834. * Checks the hardware for available packets, using completion ring
  1835. * If packets are available, it gets an RFD from the recv_list, attaches
  1836. * the packet to it, puts the RFD in the RecvPendList, and also returns
  1837. * the pointer to the RFD.
  1838. */
  1839. static struct rfd *nic_rx_pkts(struct et131x_adapter *adapter)
  1840. {
  1841. struct rx_ring *rx_local = &adapter->rx_ring;
  1842. struct rx_status_block *status;
  1843. struct pkt_stat_desc *psr;
  1844. struct rfd *rfd;
  1845. unsigned long flags;
  1846. struct list_head *element;
  1847. u8 ring_index;
  1848. u16 buff_index;
  1849. u32 len;
  1850. u32 word0;
  1851. u32 word1;
  1852. struct sk_buff *skb;
  1853. struct fbr_lookup *fbr;
  1854. /* RX Status block is written by the DMA engine prior to every
  1855. * interrupt. It contains the next to be used entry in the Packet
  1856. * Status Ring, and also the two Free Buffer rings.
  1857. */
  1858. status = rx_local->rx_status_block;
  1859. word1 = status->word1 >> 16;
  1860. /* Check the PSR and wrap bits do not match */
  1861. if ((word1 & 0x1FFF) == (rx_local->local_psr_full & 0x1FFF))
  1862. return NULL; /* Looks like this ring is not updated yet */
  1863. /* The packet status ring indicates that data is available. */
  1864. psr = (struct pkt_stat_desc *)(rx_local->ps_ring_virtaddr) +
  1865. (rx_local->local_psr_full & 0xFFF);
  1866. /* Grab any information that is required once the PSR is advanced,
  1867. * since we can no longer rely on the memory being accurate
  1868. */
  1869. len = psr->word1 & 0xFFFF;
  1870. ring_index = (psr->word1 >> 26) & 0x03;
  1871. fbr = rx_local->fbr[ring_index];
  1872. buff_index = (psr->word1 >> 16) & 0x3FF;
  1873. word0 = psr->word0;
  1874. /* Indicate that we have used this PSR entry. */
  1875. /* FIXME wrap 12 */
  1876. add_12bit(&rx_local->local_psr_full, 1);
  1877. if ((rx_local->local_psr_full & 0xFFF) > rx_local->psr_entries - 1) {
  1878. /* Clear psr full and toggle the wrap bit */
  1879. rx_local->local_psr_full &= ~0xFFF;
  1880. rx_local->local_psr_full ^= 0x1000;
  1881. }
  1882. writel(rx_local->local_psr_full, &adapter->regs->rxdma.psr_full_offset);
  1883. if (ring_index > 1 || buff_index > fbr->num_entries - 1) {
  1884. /* Illegal buffer or ring index cannot be used by S/W*/
  1885. dev_err(&adapter->pdev->dev,
  1886. "NICRxPkts PSR Entry %d indicates length of %d and/or bad bi(%d)\n",
  1887. rx_local->local_psr_full & 0xFFF, len, buff_index);
  1888. return NULL;
  1889. }
  1890. /* Get and fill the RFD. */
  1891. spin_lock_irqsave(&adapter->rcv_lock, flags);
  1892. element = rx_local->recv_list.next;
  1893. rfd = list_entry(element, struct rfd, list_node);
  1894. if (!rfd) {
  1895. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1896. return NULL;
  1897. }
  1898. list_del(&rfd->list_node);
  1899. rx_local->num_ready_recv--;
  1900. spin_unlock_irqrestore(&adapter->rcv_lock, flags);
  1901. rfd->bufferindex = buff_index;
  1902. rfd->ringindex = ring_index;
  1903. /* In V1 silicon, there is a bug which screws up filtering of runt
  1904. * packets. Therefore runt packet filtering is disabled in the MAC and
  1905. * the packets are dropped here. They are also counted here.
  1906. */
  1907. if (len < (NIC_MIN_PACKET_SIZE + 4)) {
  1908. adapter->stats.rx_other_errs++;
  1909. rfd->len = 0;
  1910. goto out;
  1911. }
  1912. if ((word0 & ALCATEL_MULTICAST_PKT) && !(word0 & ALCATEL_BROADCAST_PKT))
  1913. adapter->stats.multicast_pkts_rcvd++;
  1914. rfd->len = len;
  1915. skb = dev_alloc_skb(rfd->len + 2);
  1916. if (!skb)
  1917. return NULL;
  1918. adapter->netdev->stats.rx_bytes += rfd->len;
  1919. skb_put_data(skb, fbr->virt[buff_index], rfd->len);
  1920. skb->protocol = eth_type_trans(skb, adapter->netdev);
  1921. skb->ip_summed = CHECKSUM_NONE;
  1922. netif_receive_skb(skb);
  1923. out:
  1924. nic_return_rfd(adapter, rfd);
  1925. return rfd;
  1926. }
  1927. static int et131x_handle_recv_pkts(struct et131x_adapter *adapter, int budget)
  1928. {
  1929. struct rfd *rfd = NULL;
  1930. int count = 0;
  1931. int limit = budget;
  1932. bool done = true;
  1933. struct rx_ring *rx_ring = &adapter->rx_ring;
  1934. if (budget > MAX_PACKETS_HANDLED)
  1935. limit = MAX_PACKETS_HANDLED;
  1936. /* Process up to available RFD's */
  1937. while (count < limit) {
  1938. if (list_empty(&rx_ring->recv_list)) {
  1939. WARN_ON(rx_ring->num_ready_recv != 0);
  1940. done = false;
  1941. break;
  1942. }
  1943. rfd = nic_rx_pkts(adapter);
  1944. if (rfd == NULL)
  1945. break;
  1946. /* Do not receive any packets until a filter has been set.
  1947. * Do not receive any packets until we have link.
  1948. * If length is zero, return the RFD in order to advance the
  1949. * Free buffer ring.
  1950. */
  1951. if (!adapter->packet_filter ||
  1952. !netif_carrier_ok(adapter->netdev) ||
  1953. rfd->len == 0)
  1954. continue;
  1955. adapter->netdev->stats.rx_packets++;
  1956. if (rx_ring->num_ready_recv < RFD_LOW_WATER_MARK)
  1957. dev_warn(&adapter->pdev->dev, "RFD's are running out\n");
  1958. count++;
  1959. }
  1960. if (count == limit || !done) {
  1961. rx_ring->unfinished_receives = true;
  1962. writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
  1963. &adapter->regs->global.watchdog_timer);
  1964. } else {
  1965. /* Watchdog timer will disable itself if appropriate. */
  1966. rx_ring->unfinished_receives = false;
  1967. }
  1968. return count;
  1969. }
  1970. /* et131x_tx_dma_memory_alloc
  1971. *
  1972. * Allocates memory that will be visible both to the device and to the CPU.
  1973. * The OS will pass us packets, pointers to which we will insert in the Tx
  1974. * Descriptor queue. The device will read this queue to find the packets in
  1975. * memory. The device will update the "status" in memory each time it xmits a
  1976. * packet.
  1977. */
  1978. static int et131x_tx_dma_memory_alloc(struct et131x_adapter *adapter)
  1979. {
  1980. int desc_size = 0;
  1981. struct tx_ring *tx_ring = &adapter->tx_ring;
  1982. /* Allocate memory for the TCB's (Transmit Control Block) */
  1983. tx_ring->tcb_ring = kcalloc(NUM_TCB, sizeof(struct tcb),
  1984. GFP_KERNEL | GFP_DMA);
  1985. if (!tx_ring->tcb_ring)
  1986. return -ENOMEM;
  1987. desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
  1988. tx_ring->tx_desc_ring = dma_alloc_coherent(&adapter->pdev->dev,
  1989. desc_size,
  1990. &tx_ring->tx_desc_ring_pa,
  1991. GFP_KERNEL);
  1992. if (!tx_ring->tx_desc_ring) {
  1993. dev_err(&adapter->pdev->dev,
  1994. "Cannot alloc memory for Tx Ring\n");
  1995. return -ENOMEM;
  1996. }
  1997. tx_ring->tx_status = dma_alloc_coherent(&adapter->pdev->dev,
  1998. sizeof(u32),
  1999. &tx_ring->tx_status_pa,
  2000. GFP_KERNEL);
  2001. if (!tx_ring->tx_status) {
  2002. dev_err(&adapter->pdev->dev,
  2003. "Cannot alloc memory for Tx status block\n");
  2004. return -ENOMEM;
  2005. }
  2006. return 0;
  2007. }
  2008. static void et131x_tx_dma_memory_free(struct et131x_adapter *adapter)
  2009. {
  2010. int desc_size = 0;
  2011. struct tx_ring *tx_ring = &adapter->tx_ring;
  2012. if (tx_ring->tx_desc_ring) {
  2013. /* Free memory relating to Tx rings here */
  2014. desc_size = (sizeof(struct tx_desc) * NUM_DESC_PER_RING_TX);
  2015. dma_free_coherent(&adapter->pdev->dev,
  2016. desc_size,
  2017. tx_ring->tx_desc_ring,
  2018. tx_ring->tx_desc_ring_pa);
  2019. tx_ring->tx_desc_ring = NULL;
  2020. }
  2021. /* Free memory for the Tx status block */
  2022. if (tx_ring->tx_status) {
  2023. dma_free_coherent(&adapter->pdev->dev,
  2024. sizeof(u32),
  2025. tx_ring->tx_status,
  2026. tx_ring->tx_status_pa);
  2027. tx_ring->tx_status = NULL;
  2028. }
  2029. /* Free the memory for the tcb structures */
  2030. kfree(tx_ring->tcb_ring);
  2031. }
  2032. #define MAX_TX_DESC_PER_PKT 24
  2033. /* nic_send_packet - NIC specific send handler for version B silicon. */
  2034. static int nic_send_packet(struct et131x_adapter *adapter, struct tcb *tcb)
  2035. {
  2036. u32 i;
  2037. struct tx_desc desc[MAX_TX_DESC_PER_PKT];
  2038. u32 frag = 0;
  2039. u32 thiscopy, remainder;
  2040. struct sk_buff *skb = tcb->skb;
  2041. u32 nr_frags = skb_shinfo(skb)->nr_frags + 1;
  2042. skb_frag_t *frags = &skb_shinfo(skb)->frags[0];
  2043. struct phy_device *phydev = adapter->netdev->phydev;
  2044. dma_addr_t dma_addr;
  2045. struct tx_ring *tx_ring = &adapter->tx_ring;
  2046. /* Part of the optimizations of this send routine restrict us to
  2047. * sending 24 fragments at a pass. In practice we should never see
  2048. * more than 5 fragments.
  2049. */
  2050. memset(desc, 0, sizeof(struct tx_desc) * (nr_frags + 1));
  2051. for (i = 0; i < nr_frags; i++) {
  2052. /* If there is something in this element, lets get a
  2053. * descriptor from the ring and get the necessary data
  2054. */
  2055. if (i == 0) {
  2056. /* If the fragments are smaller than a standard MTU,
  2057. * then map them to a single descriptor in the Tx
  2058. * Desc ring. However, if they're larger, as is
  2059. * possible with support for jumbo packets, then
  2060. * split them each across 2 descriptors.
  2061. *
  2062. * This will work until we determine why the hardware
  2063. * doesn't seem to like large fragments.
  2064. */
  2065. if (skb_headlen(skb) <= 1514) {
  2066. /* Low 16bits are length, high is vlan and
  2067. * unused currently so zero
  2068. */
  2069. desc[frag].len_vlan = skb_headlen(skb);
  2070. dma_addr = dma_map_single(&adapter->pdev->dev,
  2071. skb->data,
  2072. skb_headlen(skb),
  2073. DMA_TO_DEVICE);
  2074. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2075. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2076. frag++;
  2077. } else {
  2078. desc[frag].len_vlan = skb_headlen(skb) / 2;
  2079. dma_addr = dma_map_single(&adapter->pdev->dev,
  2080. skb->data,
  2081. skb_headlen(skb) / 2,
  2082. DMA_TO_DEVICE);
  2083. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2084. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2085. frag++;
  2086. desc[frag].len_vlan = skb_headlen(skb) / 2;
  2087. dma_addr = dma_map_single(&adapter->pdev->dev,
  2088. skb->data +
  2089. skb_headlen(skb) / 2,
  2090. skb_headlen(skb) / 2,
  2091. DMA_TO_DEVICE);
  2092. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2093. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2094. frag++;
  2095. }
  2096. } else {
  2097. desc[frag].len_vlan = skb_frag_size(&frags[i - 1]);
  2098. dma_addr = skb_frag_dma_map(&adapter->pdev->dev,
  2099. &frags[i - 1],
  2100. 0,
  2101. desc[frag].len_vlan,
  2102. DMA_TO_DEVICE);
  2103. desc[frag].addr_lo = lower_32_bits(dma_addr);
  2104. desc[frag].addr_hi = upper_32_bits(dma_addr);
  2105. frag++;
  2106. }
  2107. }
  2108. if (phydev && phydev->speed == SPEED_1000) {
  2109. if (++tx_ring->since_irq == PARM_TX_NUM_BUFS_DEF) {
  2110. /* Last element & Interrupt flag */
  2111. desc[frag - 1].flags =
  2112. TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
  2113. tx_ring->since_irq = 0;
  2114. } else { /* Last element */
  2115. desc[frag - 1].flags = TXDESC_FLAG_LASTPKT;
  2116. }
  2117. } else {
  2118. desc[frag - 1].flags =
  2119. TXDESC_FLAG_INTPROC | TXDESC_FLAG_LASTPKT;
  2120. }
  2121. desc[0].flags |= TXDESC_FLAG_FIRSTPKT;
  2122. tcb->index_start = tx_ring->send_idx;
  2123. tcb->stale = 0;
  2124. thiscopy = NUM_DESC_PER_RING_TX - INDEX10(tx_ring->send_idx);
  2125. if (thiscopy >= frag) {
  2126. remainder = 0;
  2127. thiscopy = frag;
  2128. } else {
  2129. remainder = frag - thiscopy;
  2130. }
  2131. memcpy(tx_ring->tx_desc_ring + INDEX10(tx_ring->send_idx),
  2132. desc,
  2133. sizeof(struct tx_desc) * thiscopy);
  2134. add_10bit(&tx_ring->send_idx, thiscopy);
  2135. if (INDEX10(tx_ring->send_idx) == 0 ||
  2136. INDEX10(tx_ring->send_idx) == NUM_DESC_PER_RING_TX) {
  2137. tx_ring->send_idx &= ~ET_DMA10_MASK;
  2138. tx_ring->send_idx ^= ET_DMA10_WRAP;
  2139. }
  2140. if (remainder) {
  2141. memcpy(tx_ring->tx_desc_ring,
  2142. desc + thiscopy,
  2143. sizeof(struct tx_desc) * remainder);
  2144. add_10bit(&tx_ring->send_idx, remainder);
  2145. }
  2146. if (INDEX10(tx_ring->send_idx) == 0) {
  2147. if (tx_ring->send_idx)
  2148. tcb->index = NUM_DESC_PER_RING_TX - 1;
  2149. else
  2150. tcb->index = ET_DMA10_WRAP|(NUM_DESC_PER_RING_TX - 1);
  2151. } else {
  2152. tcb->index = tx_ring->send_idx - 1;
  2153. }
  2154. spin_lock(&adapter->tcb_send_qlock);
  2155. if (tx_ring->send_tail)
  2156. tx_ring->send_tail->next = tcb;
  2157. else
  2158. tx_ring->send_head = tcb;
  2159. tx_ring->send_tail = tcb;
  2160. WARN_ON(tcb->next != NULL);
  2161. tx_ring->used++;
  2162. spin_unlock(&adapter->tcb_send_qlock);
  2163. /* Write the new write pointer back to the device. */
  2164. writel(tx_ring->send_idx, &adapter->regs->txdma.service_request);
  2165. /* For Gig only, we use Tx Interrupt coalescing. Enable the software
  2166. * timer to wake us up if this packet isn't followed by N more.
  2167. */
  2168. if (phydev && phydev->speed == SPEED_1000) {
  2169. writel(PARM_TX_TIME_INT_DEF * NANO_IN_A_MICRO,
  2170. &adapter->regs->global.watchdog_timer);
  2171. }
  2172. return 0;
  2173. }
  2174. static int send_packet(struct sk_buff *skb, struct et131x_adapter *adapter)
  2175. {
  2176. int status;
  2177. struct tcb *tcb;
  2178. unsigned long flags;
  2179. struct tx_ring *tx_ring = &adapter->tx_ring;
  2180. /* All packets must have at least a MAC address and a protocol type */
  2181. if (skb->len < ETH_HLEN)
  2182. return -EIO;
  2183. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2184. tcb = tx_ring->tcb_qhead;
  2185. if (tcb == NULL) {
  2186. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2187. return -ENOMEM;
  2188. }
  2189. tx_ring->tcb_qhead = tcb->next;
  2190. if (tx_ring->tcb_qhead == NULL)
  2191. tx_ring->tcb_qtail = NULL;
  2192. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2193. tcb->skb = skb;
  2194. tcb->next = NULL;
  2195. status = nic_send_packet(adapter, tcb);
  2196. if (status != 0) {
  2197. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2198. if (tx_ring->tcb_qtail)
  2199. tx_ring->tcb_qtail->next = tcb;
  2200. else
  2201. /* Apparently ready Q is empty. */
  2202. tx_ring->tcb_qhead = tcb;
  2203. tx_ring->tcb_qtail = tcb;
  2204. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2205. return status;
  2206. }
  2207. WARN_ON(tx_ring->used > NUM_TCB);
  2208. return 0;
  2209. }
  2210. /* free_send_packet - Recycle a struct tcb */
  2211. static inline void free_send_packet(struct et131x_adapter *adapter,
  2212. struct tcb *tcb)
  2213. {
  2214. unsigned long flags;
  2215. struct tx_desc *desc = NULL;
  2216. struct net_device_stats *stats = &adapter->netdev->stats;
  2217. struct tx_ring *tx_ring = &adapter->tx_ring;
  2218. u64 dma_addr;
  2219. if (tcb->skb) {
  2220. stats->tx_bytes += tcb->skb->len;
  2221. /* Iterate through the TX descriptors on the ring
  2222. * corresponding to this packet and umap the fragments
  2223. * they point to
  2224. */
  2225. do {
  2226. desc = tx_ring->tx_desc_ring +
  2227. INDEX10(tcb->index_start);
  2228. dma_addr = desc->addr_lo;
  2229. dma_addr |= (u64)desc->addr_hi << 32;
  2230. dma_unmap_single(&adapter->pdev->dev,
  2231. dma_addr,
  2232. desc->len_vlan, DMA_TO_DEVICE);
  2233. add_10bit(&tcb->index_start, 1);
  2234. if (INDEX10(tcb->index_start) >=
  2235. NUM_DESC_PER_RING_TX) {
  2236. tcb->index_start &= ~ET_DMA10_MASK;
  2237. tcb->index_start ^= ET_DMA10_WRAP;
  2238. }
  2239. } while (desc != tx_ring->tx_desc_ring + INDEX10(tcb->index));
  2240. dev_kfree_skb_any(tcb->skb);
  2241. }
  2242. memset(tcb, 0, sizeof(struct tcb));
  2243. /* Add the TCB to the Ready Q */
  2244. spin_lock_irqsave(&adapter->tcb_ready_qlock, flags);
  2245. stats->tx_packets++;
  2246. if (tx_ring->tcb_qtail)
  2247. tx_ring->tcb_qtail->next = tcb;
  2248. else /* Apparently ready Q is empty. */
  2249. tx_ring->tcb_qhead = tcb;
  2250. tx_ring->tcb_qtail = tcb;
  2251. spin_unlock_irqrestore(&adapter->tcb_ready_qlock, flags);
  2252. WARN_ON(tx_ring->used < 0);
  2253. }
  2254. /* et131x_free_busy_send_packets - Free and complete the stopped active sends */
  2255. static void et131x_free_busy_send_packets(struct et131x_adapter *adapter)
  2256. {
  2257. struct tcb *tcb;
  2258. unsigned long flags;
  2259. u32 freed = 0;
  2260. struct tx_ring *tx_ring = &adapter->tx_ring;
  2261. /* Any packets being sent? Check the first TCB on the send list */
  2262. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2263. tcb = tx_ring->send_head;
  2264. while (tcb != NULL && freed < NUM_TCB) {
  2265. struct tcb *next = tcb->next;
  2266. tx_ring->send_head = next;
  2267. if (next == NULL)
  2268. tx_ring->send_tail = NULL;
  2269. tx_ring->used--;
  2270. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2271. freed++;
  2272. free_send_packet(adapter, tcb);
  2273. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2274. tcb = tx_ring->send_head;
  2275. }
  2276. WARN_ON(freed == NUM_TCB);
  2277. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2278. tx_ring->used = 0;
  2279. }
  2280. /* et131x_handle_send_pkts
  2281. *
  2282. * Re-claim the send resources, complete sends and get more to send from
  2283. * the send wait queue.
  2284. */
  2285. static void et131x_handle_send_pkts(struct et131x_adapter *adapter)
  2286. {
  2287. unsigned long flags;
  2288. u32 serviced;
  2289. struct tcb *tcb;
  2290. u32 index;
  2291. struct tx_ring *tx_ring = &adapter->tx_ring;
  2292. serviced = readl(&adapter->regs->txdma.new_service_complete);
  2293. index = INDEX10(serviced);
  2294. /* Has the ring wrapped? Process any descriptors that do not have
  2295. * the same "wrap" indicator as the current completion indicator
  2296. */
  2297. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2298. tcb = tx_ring->send_head;
  2299. while (tcb &&
  2300. ((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
  2301. index < INDEX10(tcb->index)) {
  2302. tx_ring->used--;
  2303. tx_ring->send_head = tcb->next;
  2304. if (tcb->next == NULL)
  2305. tx_ring->send_tail = NULL;
  2306. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2307. free_send_packet(adapter, tcb);
  2308. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2309. /* Goto the next packet */
  2310. tcb = tx_ring->send_head;
  2311. }
  2312. while (tcb &&
  2313. !((serviced ^ tcb->index) & ET_DMA10_WRAP) &&
  2314. index > (tcb->index & ET_DMA10_MASK)) {
  2315. tx_ring->used--;
  2316. tx_ring->send_head = tcb->next;
  2317. if (tcb->next == NULL)
  2318. tx_ring->send_tail = NULL;
  2319. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2320. free_send_packet(adapter, tcb);
  2321. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  2322. /* Goto the next packet */
  2323. tcb = tx_ring->send_head;
  2324. }
  2325. /* Wake up the queue when we hit a low-water mark */
  2326. if (tx_ring->used <= NUM_TCB / 3)
  2327. netif_wake_queue(adapter->netdev);
  2328. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  2329. }
  2330. static int et131x_get_regs_len(struct net_device *netdev)
  2331. {
  2332. #define ET131X_REGS_LEN 256
  2333. return ET131X_REGS_LEN * sizeof(u32);
  2334. }
  2335. static void et131x_get_regs(struct net_device *netdev,
  2336. struct ethtool_regs *regs, void *regs_data)
  2337. {
  2338. struct et131x_adapter *adapter = netdev_priv(netdev);
  2339. struct address_map __iomem *aregs = adapter->regs;
  2340. u32 *regs_buff = regs_data;
  2341. u32 num = 0;
  2342. u16 tmp;
  2343. memset(regs_data, 0, et131x_get_regs_len(netdev));
  2344. regs->version = (1 << 24) | (adapter->pdev->revision << 16) |
  2345. adapter->pdev->device;
  2346. /* PHY regs */
  2347. et131x_mii_read(adapter, MII_BMCR, &tmp);
  2348. regs_buff[num++] = tmp;
  2349. et131x_mii_read(adapter, MII_BMSR, &tmp);
  2350. regs_buff[num++] = tmp;
  2351. et131x_mii_read(adapter, MII_PHYSID1, &tmp);
  2352. regs_buff[num++] = tmp;
  2353. et131x_mii_read(adapter, MII_PHYSID2, &tmp);
  2354. regs_buff[num++] = tmp;
  2355. et131x_mii_read(adapter, MII_ADVERTISE, &tmp);
  2356. regs_buff[num++] = tmp;
  2357. et131x_mii_read(adapter, MII_LPA, &tmp);
  2358. regs_buff[num++] = tmp;
  2359. et131x_mii_read(adapter, MII_EXPANSION, &tmp);
  2360. regs_buff[num++] = tmp;
  2361. /* Autoneg next page transmit reg */
  2362. et131x_mii_read(adapter, 0x07, &tmp);
  2363. regs_buff[num++] = tmp;
  2364. /* Link partner next page reg */
  2365. et131x_mii_read(adapter, 0x08, &tmp);
  2366. regs_buff[num++] = tmp;
  2367. et131x_mii_read(adapter, MII_CTRL1000, &tmp);
  2368. regs_buff[num++] = tmp;
  2369. et131x_mii_read(adapter, MII_STAT1000, &tmp);
  2370. regs_buff[num++] = tmp;
  2371. et131x_mii_read(adapter, 0x0b, &tmp);
  2372. regs_buff[num++] = tmp;
  2373. et131x_mii_read(adapter, 0x0c, &tmp);
  2374. regs_buff[num++] = tmp;
  2375. et131x_mii_read(adapter, MII_MMD_CTRL, &tmp);
  2376. regs_buff[num++] = tmp;
  2377. et131x_mii_read(adapter, MII_MMD_DATA, &tmp);
  2378. regs_buff[num++] = tmp;
  2379. et131x_mii_read(adapter, MII_ESTATUS, &tmp);
  2380. regs_buff[num++] = tmp;
  2381. et131x_mii_read(adapter, PHY_INDEX_REG, &tmp);
  2382. regs_buff[num++] = tmp;
  2383. et131x_mii_read(adapter, PHY_DATA_REG, &tmp);
  2384. regs_buff[num++] = tmp;
  2385. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG, &tmp);
  2386. regs_buff[num++] = tmp;
  2387. et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL, &tmp);
  2388. regs_buff[num++] = tmp;
  2389. et131x_mii_read(adapter, PHY_LOOPBACK_CONTROL + 1, &tmp);
  2390. regs_buff[num++] = tmp;
  2391. et131x_mii_read(adapter, PHY_REGISTER_MGMT_CONTROL, &tmp);
  2392. regs_buff[num++] = tmp;
  2393. et131x_mii_read(adapter, PHY_CONFIG, &tmp);
  2394. regs_buff[num++] = tmp;
  2395. et131x_mii_read(adapter, PHY_PHY_CONTROL, &tmp);
  2396. regs_buff[num++] = tmp;
  2397. et131x_mii_read(adapter, PHY_INTERRUPT_MASK, &tmp);
  2398. regs_buff[num++] = tmp;
  2399. et131x_mii_read(adapter, PHY_INTERRUPT_STATUS, &tmp);
  2400. regs_buff[num++] = tmp;
  2401. et131x_mii_read(adapter, PHY_PHY_STATUS, &tmp);
  2402. regs_buff[num++] = tmp;
  2403. et131x_mii_read(adapter, PHY_LED_1, &tmp);
  2404. regs_buff[num++] = tmp;
  2405. et131x_mii_read(adapter, PHY_LED_2, &tmp);
  2406. regs_buff[num++] = tmp;
  2407. /* Global regs */
  2408. regs_buff[num++] = readl(&aregs->global.txq_start_addr);
  2409. regs_buff[num++] = readl(&aregs->global.txq_end_addr);
  2410. regs_buff[num++] = readl(&aregs->global.rxq_start_addr);
  2411. regs_buff[num++] = readl(&aregs->global.rxq_end_addr);
  2412. regs_buff[num++] = readl(&aregs->global.pm_csr);
  2413. regs_buff[num++] = adapter->stats.interrupt_status;
  2414. regs_buff[num++] = readl(&aregs->global.int_mask);
  2415. regs_buff[num++] = readl(&aregs->global.int_alias_clr_en);
  2416. regs_buff[num++] = readl(&aregs->global.int_status_alias);
  2417. regs_buff[num++] = readl(&aregs->global.sw_reset);
  2418. regs_buff[num++] = readl(&aregs->global.slv_timer);
  2419. regs_buff[num++] = readl(&aregs->global.msi_config);
  2420. regs_buff[num++] = readl(&aregs->global.loopback);
  2421. regs_buff[num++] = readl(&aregs->global.watchdog_timer);
  2422. /* TXDMA regs */
  2423. regs_buff[num++] = readl(&aregs->txdma.csr);
  2424. regs_buff[num++] = readl(&aregs->txdma.pr_base_hi);
  2425. regs_buff[num++] = readl(&aregs->txdma.pr_base_lo);
  2426. regs_buff[num++] = readl(&aregs->txdma.pr_num_des);
  2427. regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr);
  2428. regs_buff[num++] = readl(&aregs->txdma.txq_wr_addr_ext);
  2429. regs_buff[num++] = readl(&aregs->txdma.txq_rd_addr);
  2430. regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_hi);
  2431. regs_buff[num++] = readl(&aregs->txdma.dma_wb_base_lo);
  2432. regs_buff[num++] = readl(&aregs->txdma.service_request);
  2433. regs_buff[num++] = readl(&aregs->txdma.service_complete);
  2434. regs_buff[num++] = readl(&aregs->txdma.cache_rd_index);
  2435. regs_buff[num++] = readl(&aregs->txdma.cache_wr_index);
  2436. regs_buff[num++] = readl(&aregs->txdma.tx_dma_error);
  2437. regs_buff[num++] = readl(&aregs->txdma.desc_abort_cnt);
  2438. regs_buff[num++] = readl(&aregs->txdma.payload_abort_cnt);
  2439. regs_buff[num++] = readl(&aregs->txdma.writeback_abort_cnt);
  2440. regs_buff[num++] = readl(&aregs->txdma.desc_timeout_cnt);
  2441. regs_buff[num++] = readl(&aregs->txdma.payload_timeout_cnt);
  2442. regs_buff[num++] = readl(&aregs->txdma.writeback_timeout_cnt);
  2443. regs_buff[num++] = readl(&aregs->txdma.desc_error_cnt);
  2444. regs_buff[num++] = readl(&aregs->txdma.payload_error_cnt);
  2445. regs_buff[num++] = readl(&aregs->txdma.writeback_error_cnt);
  2446. regs_buff[num++] = readl(&aregs->txdma.dropped_tlp_cnt);
  2447. regs_buff[num++] = readl(&aregs->txdma.new_service_complete);
  2448. regs_buff[num++] = readl(&aregs->txdma.ethernet_packet_cnt);
  2449. /* RXDMA regs */
  2450. regs_buff[num++] = readl(&aregs->rxdma.csr);
  2451. regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_hi);
  2452. regs_buff[num++] = readl(&aregs->rxdma.dma_wb_base_lo);
  2453. regs_buff[num++] = readl(&aregs->rxdma.num_pkt_done);
  2454. regs_buff[num++] = readl(&aregs->rxdma.max_pkt_time);
  2455. regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr);
  2456. regs_buff[num++] = readl(&aregs->rxdma.rxq_rd_addr_ext);
  2457. regs_buff[num++] = readl(&aregs->rxdma.rxq_wr_addr);
  2458. regs_buff[num++] = readl(&aregs->rxdma.psr_base_hi);
  2459. regs_buff[num++] = readl(&aregs->rxdma.psr_base_lo);
  2460. regs_buff[num++] = readl(&aregs->rxdma.psr_num_des);
  2461. regs_buff[num++] = readl(&aregs->rxdma.psr_avail_offset);
  2462. regs_buff[num++] = readl(&aregs->rxdma.psr_full_offset);
  2463. regs_buff[num++] = readl(&aregs->rxdma.psr_access_index);
  2464. regs_buff[num++] = readl(&aregs->rxdma.psr_min_des);
  2465. regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_lo);
  2466. regs_buff[num++] = readl(&aregs->rxdma.fbr0_base_hi);
  2467. regs_buff[num++] = readl(&aregs->rxdma.fbr0_num_des);
  2468. regs_buff[num++] = readl(&aregs->rxdma.fbr0_avail_offset);
  2469. regs_buff[num++] = readl(&aregs->rxdma.fbr0_full_offset);
  2470. regs_buff[num++] = readl(&aregs->rxdma.fbr0_rd_index);
  2471. regs_buff[num++] = readl(&aregs->rxdma.fbr0_min_des);
  2472. regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_lo);
  2473. regs_buff[num++] = readl(&aregs->rxdma.fbr1_base_hi);
  2474. regs_buff[num++] = readl(&aregs->rxdma.fbr1_num_des);
  2475. regs_buff[num++] = readl(&aregs->rxdma.fbr1_avail_offset);
  2476. regs_buff[num++] = readl(&aregs->rxdma.fbr1_full_offset);
  2477. regs_buff[num++] = readl(&aregs->rxdma.fbr1_rd_index);
  2478. regs_buff[num++] = readl(&aregs->rxdma.fbr1_min_des);
  2479. }
  2480. static void et131x_get_drvinfo(struct net_device *netdev,
  2481. struct ethtool_drvinfo *info)
  2482. {
  2483. struct et131x_adapter *adapter = netdev_priv(netdev);
  2484. strscpy(info->driver, DRIVER_NAME, sizeof(info->driver));
  2485. strscpy(info->bus_info, pci_name(adapter->pdev),
  2486. sizeof(info->bus_info));
  2487. }
  2488. static const struct ethtool_ops et131x_ethtool_ops = {
  2489. .get_drvinfo = et131x_get_drvinfo,
  2490. .get_regs_len = et131x_get_regs_len,
  2491. .get_regs = et131x_get_regs,
  2492. .get_link = ethtool_op_get_link,
  2493. .get_link_ksettings = phy_ethtool_get_link_ksettings,
  2494. .set_link_ksettings = phy_ethtool_set_link_ksettings,
  2495. };
  2496. /* et131x_hwaddr_init - set up the MAC Address */
  2497. static void et131x_hwaddr_init(struct et131x_adapter *adapter)
  2498. {
  2499. /* If have our default mac from init and no mac address from
  2500. * EEPROM then we need to generate the last octet and set it on the
  2501. * device
  2502. */
  2503. if (is_zero_ether_addr(adapter->rom_addr)) {
  2504. /* We need to randomly generate the last octet so we
  2505. * decrease our chances of setting the mac address to
  2506. * same as another one of our cards in the system
  2507. */
  2508. get_random_bytes(&adapter->addr[5], 1);
  2509. /* We have the default value in the register we are
  2510. * working with so we need to copy the current
  2511. * address into the permanent address
  2512. */
  2513. ether_addr_copy(adapter->rom_addr, adapter->addr);
  2514. } else {
  2515. /* We do not have an override address, so set the
  2516. * current address to the permanent address and add
  2517. * it to the device
  2518. */
  2519. ether_addr_copy(adapter->addr, adapter->rom_addr);
  2520. }
  2521. }
  2522. static int et131x_pci_init(struct et131x_adapter *adapter,
  2523. struct pci_dev *pdev)
  2524. {
  2525. u16 max_payload;
  2526. int i, rc;
  2527. rc = et131x_init_eeprom(adapter);
  2528. if (rc < 0)
  2529. goto out;
  2530. if (!pci_is_pcie(pdev)) {
  2531. dev_err(&pdev->dev, "Missing PCIe capabilities\n");
  2532. goto err_out;
  2533. }
  2534. /* Program the Ack/Nak latency and replay timers */
  2535. max_payload = pdev->pcie_mpss;
  2536. if (max_payload < 2) {
  2537. static const u16 acknak[2] = { 0x76, 0xD0 };
  2538. static const u16 replay[2] = { 0x1E0, 0x2ED };
  2539. if (pci_write_config_word(pdev, ET1310_PCI_ACK_NACK,
  2540. acknak[max_payload])) {
  2541. dev_err(&pdev->dev,
  2542. "Could not write PCI config space for ACK/NAK\n");
  2543. goto err_out;
  2544. }
  2545. if (pci_write_config_word(pdev, ET1310_PCI_REPLAY,
  2546. replay[max_payload])) {
  2547. dev_err(&pdev->dev,
  2548. "Could not write PCI config space for Replay Timer\n");
  2549. goto err_out;
  2550. }
  2551. }
  2552. /* l0s and l1 latency timers. We are using default values.
  2553. * Representing 001 for L0s and 010 for L1
  2554. */
  2555. if (pci_write_config_byte(pdev, ET1310_PCI_L0L1LATENCY, 0x11)) {
  2556. dev_err(&pdev->dev,
  2557. "Could not write PCI config space for Latency Timers\n");
  2558. goto err_out;
  2559. }
  2560. /* Change the max read size to 2k */
  2561. if (pcie_set_readrq(pdev, 2048)) {
  2562. dev_err(&pdev->dev,
  2563. "Couldn't change PCI config space for Max read size\n");
  2564. goto err_out;
  2565. }
  2566. /* Get MAC address from config space if an eeprom exists, otherwise
  2567. * the MAC address there will not be valid
  2568. */
  2569. if (!adapter->has_eeprom) {
  2570. et131x_hwaddr_init(adapter);
  2571. return 0;
  2572. }
  2573. for (i = 0; i < ETH_ALEN; i++) {
  2574. if (pci_read_config_byte(pdev, ET1310_PCI_MAC_ADDRESS + i,
  2575. adapter->rom_addr + i)) {
  2576. dev_err(&pdev->dev, "Could not read PCI config space for MAC address\n");
  2577. goto err_out;
  2578. }
  2579. }
  2580. ether_addr_copy(adapter->addr, adapter->rom_addr);
  2581. out:
  2582. return rc;
  2583. err_out:
  2584. rc = -EIO;
  2585. goto out;
  2586. }
  2587. /* et131x_error_timer_handler
  2588. * @data: timer-specific variable; here a pointer to our adapter structure
  2589. *
  2590. * The routine called when the error timer expires, to track the number of
  2591. * recurring errors.
  2592. */
  2593. static void et131x_error_timer_handler(struct timer_list *t)
  2594. {
  2595. struct et131x_adapter *adapter = from_timer(adapter, t, error_timer);
  2596. struct phy_device *phydev = adapter->netdev->phydev;
  2597. if (et1310_in_phy_coma(adapter)) {
  2598. /* Bring the device immediately out of coma, to
  2599. * prevent it from sleeping indefinitely, this
  2600. * mechanism could be improved!
  2601. */
  2602. et1310_disable_phy_coma(adapter);
  2603. adapter->boot_coma = 20;
  2604. } else {
  2605. et1310_update_macstat_host_counters(adapter);
  2606. }
  2607. if (!phydev->link && adapter->boot_coma < 11)
  2608. adapter->boot_coma++;
  2609. if (adapter->boot_coma == 10) {
  2610. if (!phydev->link) {
  2611. if (!et1310_in_phy_coma(adapter)) {
  2612. /* NOTE - This was originally a 'sync with
  2613. * interrupt'. How to do that under Linux?
  2614. */
  2615. et131x_enable_interrupts(adapter);
  2616. et1310_enable_phy_coma(adapter);
  2617. }
  2618. }
  2619. }
  2620. /* This is a periodic timer, so reschedule */
  2621. mod_timer(&adapter->error_timer, jiffies +
  2622. msecs_to_jiffies(TX_ERROR_PERIOD));
  2623. }
  2624. static void et131x_adapter_memory_free(struct et131x_adapter *adapter)
  2625. {
  2626. et131x_tx_dma_memory_free(adapter);
  2627. et131x_rx_dma_memory_free(adapter);
  2628. }
  2629. static int et131x_adapter_memory_alloc(struct et131x_adapter *adapter)
  2630. {
  2631. int status;
  2632. status = et131x_tx_dma_memory_alloc(adapter);
  2633. if (status) {
  2634. dev_err(&adapter->pdev->dev,
  2635. "et131x_tx_dma_memory_alloc FAILED\n");
  2636. et131x_tx_dma_memory_free(adapter);
  2637. return status;
  2638. }
  2639. status = et131x_rx_dma_memory_alloc(adapter);
  2640. if (status) {
  2641. dev_err(&adapter->pdev->dev,
  2642. "et131x_rx_dma_memory_alloc FAILED\n");
  2643. et131x_adapter_memory_free(adapter);
  2644. return status;
  2645. }
  2646. status = et131x_init_recv(adapter);
  2647. if (status) {
  2648. dev_err(&adapter->pdev->dev, "et131x_init_recv FAILED\n");
  2649. et131x_adapter_memory_free(adapter);
  2650. }
  2651. return status;
  2652. }
  2653. static void et131x_adjust_link(struct net_device *netdev)
  2654. {
  2655. struct et131x_adapter *adapter = netdev_priv(netdev);
  2656. struct phy_device *phydev = netdev->phydev;
  2657. if (!phydev)
  2658. return;
  2659. if (phydev->link == adapter->link)
  2660. return;
  2661. /* Check to see if we are in coma mode and if
  2662. * so, disable it because we will not be able
  2663. * to read PHY values until we are out.
  2664. */
  2665. if (et1310_in_phy_coma(adapter))
  2666. et1310_disable_phy_coma(adapter);
  2667. adapter->link = phydev->link;
  2668. phy_print_status(phydev);
  2669. if (phydev->link) {
  2670. adapter->boot_coma = 20;
  2671. if (phydev->speed == SPEED_10) {
  2672. u16 register18;
  2673. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
  2674. &register18);
  2675. et131x_mii_write(adapter, phydev->mdio.addr,
  2676. PHY_MPHY_CONTROL_REG,
  2677. register18 | 0x4);
  2678. et131x_mii_write(adapter, phydev->mdio.addr,
  2679. PHY_INDEX_REG, register18 | 0x8402);
  2680. et131x_mii_write(adapter, phydev->mdio.addr,
  2681. PHY_DATA_REG, register18 | 511);
  2682. et131x_mii_write(adapter, phydev->mdio.addr,
  2683. PHY_MPHY_CONTROL_REG, register18);
  2684. }
  2685. et1310_config_flow_control(adapter);
  2686. if (phydev->speed == SPEED_1000 &&
  2687. adapter->registry_jumbo_packet > 2048) {
  2688. u16 reg;
  2689. et131x_mii_read(adapter, PHY_CONFIG, &reg);
  2690. reg &= ~ET_PHY_CONFIG_TX_FIFO_DEPTH;
  2691. reg |= ET_PHY_CONFIG_FIFO_DEPTH_32;
  2692. et131x_mii_write(adapter, phydev->mdio.addr,
  2693. PHY_CONFIG, reg);
  2694. }
  2695. et131x_set_rx_dma_timer(adapter);
  2696. et1310_config_mac_regs2(adapter);
  2697. } else {
  2698. adapter->boot_coma = 0;
  2699. if (phydev->speed == SPEED_10) {
  2700. u16 register18;
  2701. et131x_mii_read(adapter, PHY_MPHY_CONTROL_REG,
  2702. &register18);
  2703. et131x_mii_write(adapter, phydev->mdio.addr,
  2704. PHY_MPHY_CONTROL_REG,
  2705. register18 | 0x4);
  2706. et131x_mii_write(adapter, phydev->mdio.addr,
  2707. PHY_INDEX_REG, register18 | 0x8402);
  2708. et131x_mii_write(adapter, phydev->mdio.addr,
  2709. PHY_DATA_REG, register18 | 511);
  2710. et131x_mii_write(adapter, phydev->mdio.addr,
  2711. PHY_MPHY_CONTROL_REG, register18);
  2712. }
  2713. et131x_free_busy_send_packets(adapter);
  2714. et131x_init_send(adapter);
  2715. /* Bring the device back to the state it was during
  2716. * init prior to autonegotiation being complete. This
  2717. * way, when we get the auto-neg complete interrupt,
  2718. * we can complete init by calling config_mac_regs2.
  2719. */
  2720. et131x_soft_reset(adapter);
  2721. et131x_adapter_setup(adapter);
  2722. et131x_disable_txrx(netdev);
  2723. et131x_enable_txrx(netdev);
  2724. }
  2725. }
  2726. static int et131x_mii_probe(struct net_device *netdev)
  2727. {
  2728. struct et131x_adapter *adapter = netdev_priv(netdev);
  2729. struct phy_device *phydev = NULL;
  2730. phydev = phy_find_first(adapter->mii_bus);
  2731. if (!phydev) {
  2732. dev_err(&adapter->pdev->dev, "no PHY found\n");
  2733. return -ENODEV;
  2734. }
  2735. phydev = phy_connect(netdev, phydev_name(phydev),
  2736. &et131x_adjust_link, PHY_INTERFACE_MODE_MII);
  2737. if (IS_ERR(phydev)) {
  2738. dev_err(&adapter->pdev->dev, "Could not attach to PHY\n");
  2739. return PTR_ERR(phydev);
  2740. }
  2741. phy_set_max_speed(phydev, SPEED_100);
  2742. if (adapter->pdev->device != ET131X_PCI_DEVICE_ID_FAST)
  2743. phy_set_max_speed(phydev, SPEED_1000);
  2744. phydev->autoneg = AUTONEG_ENABLE;
  2745. phy_attached_info(phydev);
  2746. return 0;
  2747. }
  2748. static struct et131x_adapter *et131x_adapter_init(struct net_device *netdev,
  2749. struct pci_dev *pdev)
  2750. {
  2751. static const u8 default_mac[] = { 0x00, 0x05, 0x3d, 0x00, 0x02, 0x00 };
  2752. struct et131x_adapter *adapter;
  2753. adapter = netdev_priv(netdev);
  2754. adapter->pdev = pci_dev_get(pdev);
  2755. adapter->netdev = netdev;
  2756. spin_lock_init(&adapter->tcb_send_qlock);
  2757. spin_lock_init(&adapter->tcb_ready_qlock);
  2758. spin_lock_init(&adapter->rcv_lock);
  2759. adapter->registry_jumbo_packet = 1514; /* 1514-9216 */
  2760. ether_addr_copy(adapter->addr, default_mac);
  2761. return adapter;
  2762. }
  2763. static void et131x_pci_remove(struct pci_dev *pdev)
  2764. {
  2765. struct net_device *netdev = pci_get_drvdata(pdev);
  2766. struct et131x_adapter *adapter = netdev_priv(netdev);
  2767. unregister_netdev(netdev);
  2768. netif_napi_del(&adapter->napi);
  2769. phy_disconnect(netdev->phydev);
  2770. mdiobus_unregister(adapter->mii_bus);
  2771. mdiobus_free(adapter->mii_bus);
  2772. et131x_adapter_memory_free(adapter);
  2773. iounmap(adapter->regs);
  2774. pci_dev_put(pdev);
  2775. free_netdev(netdev);
  2776. pci_release_regions(pdev);
  2777. pci_disable_device(pdev);
  2778. }
  2779. static void et131x_up(struct net_device *netdev)
  2780. {
  2781. et131x_enable_txrx(netdev);
  2782. phy_start(netdev->phydev);
  2783. }
  2784. static void et131x_down(struct net_device *netdev)
  2785. {
  2786. /* Save the timestamp for the TX watchdog, prevent a timeout */
  2787. netif_trans_update(netdev);
  2788. phy_stop(netdev->phydev);
  2789. et131x_disable_txrx(netdev);
  2790. }
  2791. #ifdef CONFIG_PM_SLEEP
  2792. static int et131x_suspend(struct device *dev)
  2793. {
  2794. struct pci_dev *pdev = to_pci_dev(dev);
  2795. struct net_device *netdev = pci_get_drvdata(pdev);
  2796. if (netif_running(netdev)) {
  2797. netif_device_detach(netdev);
  2798. et131x_down(netdev);
  2799. pci_save_state(pdev);
  2800. }
  2801. return 0;
  2802. }
  2803. static int et131x_resume(struct device *dev)
  2804. {
  2805. struct pci_dev *pdev = to_pci_dev(dev);
  2806. struct net_device *netdev = pci_get_drvdata(pdev);
  2807. if (netif_running(netdev)) {
  2808. pci_restore_state(pdev);
  2809. et131x_up(netdev);
  2810. netif_device_attach(netdev);
  2811. }
  2812. return 0;
  2813. }
  2814. #endif
  2815. static SIMPLE_DEV_PM_OPS(et131x_pm_ops, et131x_suspend, et131x_resume);
  2816. static irqreturn_t et131x_isr(int irq, void *dev_id)
  2817. {
  2818. bool handled = true;
  2819. bool enable_interrupts = true;
  2820. struct net_device *netdev = dev_id;
  2821. struct et131x_adapter *adapter = netdev_priv(netdev);
  2822. struct address_map __iomem *iomem = adapter->regs;
  2823. struct rx_ring *rx_ring = &adapter->rx_ring;
  2824. struct tx_ring *tx_ring = &adapter->tx_ring;
  2825. u32 status;
  2826. if (!netif_device_present(netdev)) {
  2827. handled = false;
  2828. enable_interrupts = false;
  2829. goto out;
  2830. }
  2831. et131x_disable_interrupts(adapter);
  2832. status = readl(&adapter->regs->global.int_status);
  2833. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH)
  2834. status &= ~INT_MASK_ENABLE;
  2835. else
  2836. status &= ~INT_MASK_ENABLE_NO_FLOW;
  2837. /* Make sure this is our interrupt */
  2838. if (!status) {
  2839. handled = false;
  2840. et131x_enable_interrupts(adapter);
  2841. goto out;
  2842. }
  2843. /* This is our interrupt, so process accordingly */
  2844. if (status & ET_INTR_WATCHDOG) {
  2845. struct tcb *tcb = tx_ring->send_head;
  2846. if (tcb)
  2847. if (++tcb->stale > 1)
  2848. status |= ET_INTR_TXDMA_ISR;
  2849. if (rx_ring->unfinished_receives)
  2850. status |= ET_INTR_RXDMA_XFR_DONE;
  2851. else if (tcb == NULL)
  2852. writel(0, &adapter->regs->global.watchdog_timer);
  2853. status &= ~ET_INTR_WATCHDOG;
  2854. }
  2855. if (status & (ET_INTR_RXDMA_XFR_DONE | ET_INTR_TXDMA_ISR)) {
  2856. enable_interrupts = false;
  2857. napi_schedule(&adapter->napi);
  2858. }
  2859. status &= ~(ET_INTR_TXDMA_ISR | ET_INTR_RXDMA_XFR_DONE);
  2860. if (!status)
  2861. goto out;
  2862. if (status & ET_INTR_TXDMA_ERR) {
  2863. /* Following read also clears the register (COR) */
  2864. u32 txdma_err = readl(&iomem->txdma.tx_dma_error);
  2865. dev_warn(&adapter->pdev->dev,
  2866. "TXDMA_ERR interrupt, error = %d\n",
  2867. txdma_err);
  2868. }
  2869. if (status & (ET_INTR_RXDMA_FB_R0_LOW | ET_INTR_RXDMA_FB_R1_LOW)) {
  2870. /* This indicates the number of unused buffers in RXDMA free
  2871. * buffer ring 0 is <= the limit you programmed. Free buffer
  2872. * resources need to be returned. Free buffers are consumed as
  2873. * packets are passed from the network to the host. The host
  2874. * becomes aware of the packets from the contents of the packet
  2875. * status ring. This ring is queried when the packet done
  2876. * interrupt occurs. Packets are then passed to the OS. When
  2877. * the OS is done with the packets the resources can be
  2878. * returned to the ET1310 for re-use. This interrupt is one
  2879. * method of returning resources.
  2880. */
  2881. /* If the user has flow control on, then we will
  2882. * send a pause packet, otherwise just exit
  2883. */
  2884. if (adapter->flow == FLOW_TXONLY || adapter->flow == FLOW_BOTH) {
  2885. /* Tell the device to send a pause packet via the back
  2886. * pressure register (bp req and bp xon/xoff)
  2887. */
  2888. if (!et1310_in_phy_coma(adapter))
  2889. writel(3, &iomem->txmac.bp_ctrl);
  2890. }
  2891. }
  2892. /* Handle Packet Status Ring Low Interrupt */
  2893. if (status & ET_INTR_RXDMA_STAT_LOW) {
  2894. /* Same idea as with the two Free Buffer Rings. Packets going
  2895. * from the network to the host each consume a free buffer
  2896. * resource and a packet status resource. These resources are
  2897. * passed to the OS. When the OS is done with the resources,
  2898. * they need to be returned to the ET1310. This is one method
  2899. * of returning the resources.
  2900. */
  2901. }
  2902. if (status & ET_INTR_RXDMA_ERR) {
  2903. /* The rxdma_error interrupt is sent when a time-out on a
  2904. * request issued by the JAGCore has occurred or a completion is
  2905. * returned with an un-successful status. In both cases the
  2906. * request is considered complete. The JAGCore will
  2907. * automatically re-try the request in question. Normally
  2908. * information on events like these are sent to the host using
  2909. * the "Advanced Error Reporting" capability. This interrupt is
  2910. * another way of getting similar information. The only thing
  2911. * required is to clear the interrupt by reading the ISR in the
  2912. * global resources. The JAGCore will do a re-try on the
  2913. * request. Normally you should never see this interrupt. If
  2914. * you start to see this interrupt occurring frequently then
  2915. * something bad has occurred. A reset might be the thing to do.
  2916. */
  2917. /* TRAP();*/
  2918. dev_warn(&adapter->pdev->dev, "RxDMA_ERR interrupt, error %x\n",
  2919. readl(&iomem->txmac.tx_test));
  2920. }
  2921. /* Handle the Wake on LAN Event */
  2922. if (status & ET_INTR_WOL) {
  2923. /* This is a secondary interrupt for wake on LAN. The driver
  2924. * should never see this, if it does, something serious is
  2925. * wrong.
  2926. */
  2927. dev_err(&adapter->pdev->dev, "WAKE_ON_LAN interrupt\n");
  2928. }
  2929. if (status & ET_INTR_TXMAC) {
  2930. u32 err = readl(&iomem->txmac.err);
  2931. /* When any of the errors occur and TXMAC generates an
  2932. * interrupt to report these errors, it usually means that
  2933. * TXMAC has detected an error in the data stream retrieved
  2934. * from the on-chip Tx Q. All of these errors are catastrophic
  2935. * and TXMAC won't be able to recover data when these errors
  2936. * occur. In a nutshell, the whole Tx path will have to be reset
  2937. * and re-configured afterwards.
  2938. */
  2939. dev_warn(&adapter->pdev->dev, "TXMAC interrupt, error 0x%08x\n",
  2940. err);
  2941. /* If we are debugging, we want to see this error, otherwise we
  2942. * just want the device to be reset and continue
  2943. */
  2944. }
  2945. if (status & ET_INTR_RXMAC) {
  2946. /* These interrupts are catastrophic to the device, what we need
  2947. * to do is disable the interrupts and set the flag to cause us
  2948. * to reset so we can solve this issue.
  2949. */
  2950. dev_warn(&adapter->pdev->dev,
  2951. "RXMAC interrupt, error 0x%08x. Requesting reset\n",
  2952. readl(&iomem->rxmac.err_reg));
  2953. dev_warn(&adapter->pdev->dev,
  2954. "Enable 0x%08x, Diag 0x%08x\n",
  2955. readl(&iomem->rxmac.ctrl),
  2956. readl(&iomem->rxmac.rxq_diag));
  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_MAC_STAT) {
  2962. /* This means at least one of the un-masked counters in the
  2963. * MAC_STAT block has rolled over. Use this to maintain the top,
  2964. * software managed bits of the counter(s).
  2965. */
  2966. et1310_handle_macstat_interrupt(adapter);
  2967. }
  2968. if (status & ET_INTR_SLV_TIMEOUT) {
  2969. /* This means a timeout has occurred on a read or write request
  2970. * to one of the JAGCore registers. The Global Resources block
  2971. * has terminated the request and on a read request, returned a
  2972. * "fake" value. The most likely reasons are: Bad Address or the
  2973. * addressed module is in a power-down state and can't respond.
  2974. */
  2975. }
  2976. out:
  2977. if (enable_interrupts)
  2978. et131x_enable_interrupts(adapter);
  2979. return IRQ_RETVAL(handled);
  2980. }
  2981. static int et131x_poll(struct napi_struct *napi, int budget)
  2982. {
  2983. struct et131x_adapter *adapter =
  2984. container_of(napi, struct et131x_adapter, napi);
  2985. int work_done = et131x_handle_recv_pkts(adapter, budget);
  2986. et131x_handle_send_pkts(adapter);
  2987. if (work_done < budget) {
  2988. napi_complete_done(&adapter->napi, work_done);
  2989. et131x_enable_interrupts(adapter);
  2990. }
  2991. return work_done;
  2992. }
  2993. /* et131x_stats - Return the current device statistics */
  2994. static struct net_device_stats *et131x_stats(struct net_device *netdev)
  2995. {
  2996. struct et131x_adapter *adapter = netdev_priv(netdev);
  2997. struct net_device_stats *stats = &adapter->netdev->stats;
  2998. struct ce_stats *devstat = &adapter->stats;
  2999. stats->rx_errors = devstat->rx_length_errs +
  3000. devstat->rx_align_errs +
  3001. devstat->rx_crc_errs +
  3002. devstat->rx_code_violations +
  3003. devstat->rx_other_errs;
  3004. stats->tx_errors = devstat->tx_max_pkt_errs;
  3005. stats->multicast = devstat->multicast_pkts_rcvd;
  3006. stats->collisions = devstat->tx_collisions;
  3007. stats->rx_length_errors = devstat->rx_length_errs;
  3008. stats->rx_over_errors = devstat->rx_overflows;
  3009. stats->rx_crc_errors = devstat->rx_crc_errs;
  3010. stats->rx_dropped = devstat->rcvd_pkts_dropped;
  3011. /* NOTE: Not used, can't find analogous statistics */
  3012. /* stats->rx_frame_errors = devstat->; */
  3013. /* stats->rx_fifo_errors = devstat->; */
  3014. /* stats->rx_missed_errors = devstat->; */
  3015. /* stats->tx_aborted_errors = devstat->; */
  3016. /* stats->tx_carrier_errors = devstat->; */
  3017. /* stats->tx_fifo_errors = devstat->; */
  3018. /* stats->tx_heartbeat_errors = devstat->; */
  3019. /* stats->tx_window_errors = devstat->; */
  3020. return stats;
  3021. }
  3022. static int et131x_open(struct net_device *netdev)
  3023. {
  3024. struct et131x_adapter *adapter = netdev_priv(netdev);
  3025. struct pci_dev *pdev = adapter->pdev;
  3026. unsigned int irq = pdev->irq;
  3027. int result;
  3028. /* Start the timer to track NIC errors */
  3029. timer_setup(&adapter->error_timer, et131x_error_timer_handler, 0);
  3030. adapter->error_timer.expires = jiffies +
  3031. msecs_to_jiffies(TX_ERROR_PERIOD);
  3032. add_timer(&adapter->error_timer);
  3033. result = request_irq(irq, et131x_isr,
  3034. IRQF_SHARED, netdev->name, netdev);
  3035. if (result) {
  3036. dev_err(&pdev->dev, "could not register IRQ %d\n", irq);
  3037. return result;
  3038. }
  3039. adapter->flags |= FMP_ADAPTER_INTERRUPT_IN_USE;
  3040. napi_enable(&adapter->napi);
  3041. et131x_up(netdev);
  3042. return result;
  3043. }
  3044. static int et131x_close(struct net_device *netdev)
  3045. {
  3046. struct et131x_adapter *adapter = netdev_priv(netdev);
  3047. et131x_down(netdev);
  3048. napi_disable(&adapter->napi);
  3049. adapter->flags &= ~FMP_ADAPTER_INTERRUPT_IN_USE;
  3050. free_irq(adapter->pdev->irq, netdev);
  3051. /* Stop the error timer */
  3052. return del_timer_sync(&adapter->error_timer);
  3053. }
  3054. /* et131x_set_packet_filter - Configures the Rx Packet filtering */
  3055. static int et131x_set_packet_filter(struct et131x_adapter *adapter)
  3056. {
  3057. int filter = adapter->packet_filter;
  3058. u32 ctrl;
  3059. u32 pf_ctrl;
  3060. ctrl = readl(&adapter->regs->rxmac.ctrl);
  3061. pf_ctrl = readl(&adapter->regs->rxmac.pf_ctrl);
  3062. /* Default to disabled packet filtering */
  3063. ctrl |= 0x04;
  3064. /* Set us to be in promiscuous mode so we receive everything, this
  3065. * is also true when we get a packet filter of 0
  3066. */
  3067. if ((filter & ET131X_PACKET_TYPE_PROMISCUOUS) || filter == 0)
  3068. pf_ctrl &= ~7; /* Clear filter bits */
  3069. else {
  3070. /* Set us up with Multicast packet filtering. Three cases are
  3071. * possible - (1) we have a multi-cast list, (2) we receive ALL
  3072. * multicast entries or (3) we receive none.
  3073. */
  3074. if (filter & ET131X_PACKET_TYPE_ALL_MULTICAST)
  3075. pf_ctrl &= ~2; /* Multicast filter bit */
  3076. else {
  3077. et1310_setup_device_for_multicast(adapter);
  3078. pf_ctrl |= 2;
  3079. ctrl &= ~0x04;
  3080. }
  3081. /* Set us up with Unicast packet filtering */
  3082. if (filter & ET131X_PACKET_TYPE_DIRECTED) {
  3083. et1310_setup_device_for_unicast(adapter);
  3084. pf_ctrl |= 4;
  3085. ctrl &= ~0x04;
  3086. }
  3087. /* Set us up with Broadcast packet filtering */
  3088. if (filter & ET131X_PACKET_TYPE_BROADCAST) {
  3089. pf_ctrl |= 1; /* Broadcast filter bit */
  3090. ctrl &= ~0x04;
  3091. } else {
  3092. pf_ctrl &= ~1;
  3093. }
  3094. /* Setup the receive mac configuration registers - Packet
  3095. * Filter control + the enable / disable for packet filter
  3096. * in the control reg.
  3097. */
  3098. writel(pf_ctrl, &adapter->regs->rxmac.pf_ctrl);
  3099. writel(ctrl, &adapter->regs->rxmac.ctrl);
  3100. }
  3101. return 0;
  3102. }
  3103. static void et131x_multicast(struct net_device *netdev)
  3104. {
  3105. struct et131x_adapter *adapter = netdev_priv(netdev);
  3106. int packet_filter;
  3107. struct netdev_hw_addr *ha;
  3108. int i;
  3109. /* Before we modify the platform-independent filter flags, store them
  3110. * locally. This allows us to determine if anything's changed and if
  3111. * we even need to bother the hardware
  3112. */
  3113. packet_filter = adapter->packet_filter;
  3114. /* Clear the 'multicast' flag locally; because we only have a single
  3115. * flag to check multicast, and multiple multicast addresses can be
  3116. * set, this is the easiest way to determine if more than one
  3117. * multicast address is being set.
  3118. */
  3119. packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
  3120. /* Check the net_device flags and set the device independent flags
  3121. * accordingly
  3122. */
  3123. if (netdev->flags & IFF_PROMISC)
  3124. adapter->packet_filter |= ET131X_PACKET_TYPE_PROMISCUOUS;
  3125. else
  3126. adapter->packet_filter &= ~ET131X_PACKET_TYPE_PROMISCUOUS;
  3127. if ((netdev->flags & IFF_ALLMULTI) ||
  3128. (netdev_mc_count(netdev) > NIC_MAX_MCAST_LIST))
  3129. adapter->packet_filter |= ET131X_PACKET_TYPE_ALL_MULTICAST;
  3130. if (netdev_mc_count(netdev) < 1) {
  3131. adapter->packet_filter &= ~ET131X_PACKET_TYPE_ALL_MULTICAST;
  3132. adapter->packet_filter &= ~ET131X_PACKET_TYPE_MULTICAST;
  3133. } else {
  3134. adapter->packet_filter |= ET131X_PACKET_TYPE_MULTICAST;
  3135. }
  3136. /* Set values in the private adapter struct */
  3137. i = 0;
  3138. netdev_for_each_mc_addr(ha, netdev) {
  3139. if (i == NIC_MAX_MCAST_LIST)
  3140. break;
  3141. ether_addr_copy(adapter->multicast_list[i++], ha->addr);
  3142. }
  3143. adapter->multicast_addr_count = i;
  3144. /* Are the new flags different from the previous ones? If not, then no
  3145. * action is required
  3146. *
  3147. * NOTE - This block will always update the multicast_list with the
  3148. * hardware, even if the addresses aren't the same.
  3149. */
  3150. if (packet_filter != adapter->packet_filter)
  3151. et131x_set_packet_filter(adapter);
  3152. }
  3153. static netdev_tx_t et131x_tx(struct sk_buff *skb, struct net_device *netdev)
  3154. {
  3155. struct et131x_adapter *adapter = netdev_priv(netdev);
  3156. struct tx_ring *tx_ring = &adapter->tx_ring;
  3157. /* This driver does not support TSO, it is very unlikely
  3158. * this condition is true.
  3159. */
  3160. if (unlikely(skb_shinfo(skb)->nr_frags > MAX_TX_DESC_PER_PKT - 2)) {
  3161. if (skb_linearize(skb))
  3162. goto drop_err;
  3163. }
  3164. /* stop the queue if it's getting full */
  3165. if (tx_ring->used >= NUM_TCB - 1 && !netif_queue_stopped(netdev))
  3166. netif_stop_queue(netdev);
  3167. /* Save the timestamp for the TX timeout watchdog */
  3168. netif_trans_update(netdev);
  3169. /* TCB is not available */
  3170. if (tx_ring->used >= NUM_TCB)
  3171. goto drop_err;
  3172. if ((adapter->flags & FMP_ADAPTER_FAIL_SEND_MASK) ||
  3173. !netif_carrier_ok(netdev))
  3174. goto drop_err;
  3175. if (send_packet(skb, adapter))
  3176. goto drop_err;
  3177. return NETDEV_TX_OK;
  3178. drop_err:
  3179. dev_kfree_skb_any(skb);
  3180. adapter->netdev->stats.tx_dropped++;
  3181. return NETDEV_TX_OK;
  3182. }
  3183. /* et131x_tx_timeout - Timeout handler
  3184. *
  3185. * The handler called when a Tx request times out. The timeout period is
  3186. * specified by the 'tx_timeo" element in the net_device structure (see
  3187. * et131x_alloc_device() to see how this value is set).
  3188. */
  3189. static void et131x_tx_timeout(struct net_device *netdev, unsigned int txqueue)
  3190. {
  3191. struct et131x_adapter *adapter = netdev_priv(netdev);
  3192. struct tx_ring *tx_ring = &adapter->tx_ring;
  3193. struct tcb *tcb;
  3194. unsigned long flags;
  3195. /* If the device is closed, ignore the timeout */
  3196. if (!(adapter->flags & FMP_ADAPTER_INTERRUPT_IN_USE))
  3197. return;
  3198. /* Any nonrecoverable hardware error?
  3199. * Checks adapter->flags for any failure in phy reading
  3200. */
  3201. if (adapter->flags & FMP_ADAPTER_NON_RECOVER_ERROR)
  3202. return;
  3203. /* Hardware failure? */
  3204. if (adapter->flags & FMP_ADAPTER_HARDWARE_ERROR) {
  3205. dev_err(&adapter->pdev->dev, "hardware error - reset\n");
  3206. return;
  3207. }
  3208. /* Is send stuck? */
  3209. spin_lock_irqsave(&adapter->tcb_send_qlock, flags);
  3210. tcb = tx_ring->send_head;
  3211. spin_unlock_irqrestore(&adapter->tcb_send_qlock, flags);
  3212. if (tcb) {
  3213. tcb->count++;
  3214. if (tcb->count > NIC_SEND_HANG_THRESHOLD) {
  3215. dev_warn(&adapter->pdev->dev,
  3216. "Send stuck - reset. tcb->WrIndex %x\n",
  3217. tcb->index);
  3218. adapter->netdev->stats.tx_errors++;
  3219. /* perform reset of tx/rx */
  3220. et131x_disable_txrx(netdev);
  3221. et131x_enable_txrx(netdev);
  3222. }
  3223. }
  3224. }
  3225. static int et131x_change_mtu(struct net_device *netdev, int new_mtu)
  3226. {
  3227. int result = 0;
  3228. struct et131x_adapter *adapter = netdev_priv(netdev);
  3229. et131x_disable_txrx(netdev);
  3230. netdev->mtu = new_mtu;
  3231. et131x_adapter_memory_free(adapter);
  3232. /* Set the config parameter for Jumbo Packet support */
  3233. adapter->registry_jumbo_packet = new_mtu + 14;
  3234. et131x_soft_reset(adapter);
  3235. result = et131x_adapter_memory_alloc(adapter);
  3236. if (result != 0) {
  3237. dev_warn(&adapter->pdev->dev,
  3238. "Change MTU failed; couldn't re-alloc DMA memory\n");
  3239. return result;
  3240. }
  3241. et131x_init_send(adapter);
  3242. et131x_hwaddr_init(adapter);
  3243. eth_hw_addr_set(netdev, adapter->addr);
  3244. /* Init the device with the new settings */
  3245. et131x_adapter_setup(adapter);
  3246. et131x_enable_txrx(netdev);
  3247. return result;
  3248. }
  3249. static const struct net_device_ops et131x_netdev_ops = {
  3250. .ndo_open = et131x_open,
  3251. .ndo_stop = et131x_close,
  3252. .ndo_start_xmit = et131x_tx,
  3253. .ndo_set_rx_mode = et131x_multicast,
  3254. .ndo_tx_timeout = et131x_tx_timeout,
  3255. .ndo_change_mtu = et131x_change_mtu,
  3256. .ndo_set_mac_address = eth_mac_addr,
  3257. .ndo_validate_addr = eth_validate_addr,
  3258. .ndo_get_stats = et131x_stats,
  3259. .ndo_eth_ioctl = phy_do_ioctl,
  3260. };
  3261. static int et131x_pci_setup(struct pci_dev *pdev,
  3262. const struct pci_device_id *ent)
  3263. {
  3264. struct net_device *netdev;
  3265. struct et131x_adapter *adapter;
  3266. int rc;
  3267. rc = pci_enable_device(pdev);
  3268. if (rc < 0) {
  3269. dev_err(&pdev->dev, "pci_enable_device() failed\n");
  3270. goto out;
  3271. }
  3272. /* Perform some basic PCI checks */
  3273. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  3274. dev_err(&pdev->dev, "Can't find PCI device's base address\n");
  3275. rc = -ENODEV;
  3276. goto err_disable;
  3277. }
  3278. rc = pci_request_regions(pdev, DRIVER_NAME);
  3279. if (rc < 0) {
  3280. dev_err(&pdev->dev, "Can't get PCI resources\n");
  3281. goto err_disable;
  3282. }
  3283. pci_set_master(pdev);
  3284. /* Check the DMA addressing support of this device */
  3285. rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  3286. if (rc) {
  3287. dev_err(&pdev->dev, "No usable DMA addressing method\n");
  3288. goto err_release_res;
  3289. }
  3290. netdev = alloc_etherdev(sizeof(struct et131x_adapter));
  3291. if (!netdev) {
  3292. dev_err(&pdev->dev, "Couldn't alloc netdev struct\n");
  3293. rc = -ENOMEM;
  3294. goto err_release_res;
  3295. }
  3296. netdev->watchdog_timeo = ET131X_TX_TIMEOUT;
  3297. netdev->netdev_ops = &et131x_netdev_ops;
  3298. netdev->min_mtu = ET131X_MIN_MTU;
  3299. netdev->max_mtu = ET131X_MAX_MTU;
  3300. SET_NETDEV_DEV(netdev, &pdev->dev);
  3301. netdev->ethtool_ops = &et131x_ethtool_ops;
  3302. adapter = et131x_adapter_init(netdev, pdev);
  3303. rc = et131x_pci_init(adapter, pdev);
  3304. if (rc < 0)
  3305. goto err_free_dev;
  3306. /* Map the bus-relative registers to system virtual memory */
  3307. adapter->regs = pci_ioremap_bar(pdev, 0);
  3308. if (!adapter->regs) {
  3309. dev_err(&pdev->dev, "Cannot map device registers\n");
  3310. rc = -ENOMEM;
  3311. goto err_free_dev;
  3312. }
  3313. /* If Phy COMA mode was enabled when we went down, disable it here. */
  3314. writel(ET_PMCSR_INIT, &adapter->regs->global.pm_csr);
  3315. et131x_soft_reset(adapter);
  3316. et131x_disable_interrupts(adapter);
  3317. rc = et131x_adapter_memory_alloc(adapter);
  3318. if (rc < 0) {
  3319. dev_err(&pdev->dev, "Could not alloc adapter memory (DMA)\n");
  3320. goto err_iounmap;
  3321. }
  3322. et131x_init_send(adapter);
  3323. netif_napi_add(netdev, &adapter->napi, et131x_poll);
  3324. eth_hw_addr_set(netdev, adapter->addr);
  3325. rc = -ENOMEM;
  3326. adapter->mii_bus = mdiobus_alloc();
  3327. if (!adapter->mii_bus) {
  3328. dev_err(&pdev->dev, "Alloc of mii_bus struct failed\n");
  3329. goto err_mem_free;
  3330. }
  3331. adapter->mii_bus->name = "et131x_eth_mii";
  3332. snprintf(adapter->mii_bus->id, MII_BUS_ID_SIZE, "%x",
  3333. (adapter->pdev->bus->number << 8) | adapter->pdev->devfn);
  3334. adapter->mii_bus->priv = netdev;
  3335. adapter->mii_bus->read = et131x_mdio_read;
  3336. adapter->mii_bus->write = et131x_mdio_write;
  3337. rc = mdiobus_register(adapter->mii_bus);
  3338. if (rc < 0) {
  3339. dev_err(&pdev->dev, "failed to register MII bus\n");
  3340. goto err_mdio_free;
  3341. }
  3342. rc = et131x_mii_probe(netdev);
  3343. if (rc < 0) {
  3344. dev_err(&pdev->dev, "failed to probe MII bus\n");
  3345. goto err_mdio_unregister;
  3346. }
  3347. et131x_adapter_setup(adapter);
  3348. /* Init variable for counting how long we do not have link status */
  3349. adapter->boot_coma = 0;
  3350. et1310_disable_phy_coma(adapter);
  3351. /* We can enable interrupts now
  3352. *
  3353. * NOTE - Because registration of interrupt handler is done in the
  3354. * device's open(), defer enabling device interrupts to that
  3355. * point
  3356. */
  3357. rc = register_netdev(netdev);
  3358. if (rc < 0) {
  3359. dev_err(&pdev->dev, "register_netdev() failed\n");
  3360. goto err_phy_disconnect;
  3361. }
  3362. /* Register the net_device struct with the PCI subsystem. Save a copy
  3363. * of the PCI config space for this device now that the device has
  3364. * been initialized, just in case it needs to be quickly restored.
  3365. */
  3366. pci_set_drvdata(pdev, netdev);
  3367. out:
  3368. return rc;
  3369. err_phy_disconnect:
  3370. phy_disconnect(netdev->phydev);
  3371. err_mdio_unregister:
  3372. mdiobus_unregister(adapter->mii_bus);
  3373. err_mdio_free:
  3374. mdiobus_free(adapter->mii_bus);
  3375. err_mem_free:
  3376. et131x_adapter_memory_free(adapter);
  3377. err_iounmap:
  3378. iounmap(adapter->regs);
  3379. err_free_dev:
  3380. pci_dev_put(pdev);
  3381. free_netdev(netdev);
  3382. err_release_res:
  3383. pci_release_regions(pdev);
  3384. err_disable:
  3385. pci_disable_device(pdev);
  3386. goto out;
  3387. }
  3388. static const struct pci_device_id et131x_pci_table[] = {
  3389. { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_GIG), 0UL},
  3390. { PCI_VDEVICE(ATT, ET131X_PCI_DEVICE_ID_FAST), 0UL},
  3391. { 0,}
  3392. };
  3393. MODULE_DEVICE_TABLE(pci, et131x_pci_table);
  3394. static struct pci_driver et131x_driver = {
  3395. .name = DRIVER_NAME,
  3396. .id_table = et131x_pci_table,
  3397. .probe = et131x_pci_setup,
  3398. .remove = et131x_pci_remove,
  3399. .driver.pm = &et131x_pm_ops,
  3400. };
  3401. module_pci_driver(et131x_driver);