cassini.c 142 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370
  1. /* cassini.c: Sun Microsystems Cassini(+) ethernet driver.
  2. *
  3. * Copyright (C) 2004 Sun Microsystems Inc.
  4. * Copyright (C) 2003 Adrian Sun (asun@darksunrising.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation; either version 2 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. * 02111-1307, USA.
  20. *
  21. * This driver uses the sungem driver (c) David Miller
  22. * (davem@redhat.com) as its basis.
  23. *
  24. * The cassini chip has a number of features that distinguish it from
  25. * the gem chip:
  26. * 4 transmit descriptor rings that are used for either QoS (VLAN) or
  27. * load balancing (non-VLAN mode)
  28. * batching of multiple packets
  29. * multiple CPU dispatching
  30. * page-based RX descriptor engine with separate completion rings
  31. * Gigabit support (GMII and PCS interface)
  32. * MIF link up/down detection works
  33. *
  34. * RX is handled by page sized buffers that are attached as fragments to
  35. * the skb. here's what's done:
  36. * -- driver allocates pages at a time and keeps reference counts
  37. * on them.
  38. * -- the upper protocol layers assume that the header is in the skb
  39. * itself. as a result, cassini will copy a small amount (64 bytes)
  40. * to make them happy.
  41. * -- driver appends the rest of the data pages as frags to skbuffs
  42. * and increments the reference count
  43. * -- on page reclamation, the driver swaps the page with a spare page.
  44. * if that page is still in use, it frees its reference to that page,
  45. * and allocates a new page for use. otherwise, it just recycles the
  46. * the page.
  47. *
  48. * NOTE: cassini can parse the header. however, it's not worth it
  49. * as long as the network stack requires a header copy.
  50. *
  51. * TX has 4 queues. currently these queues are used in a round-robin
  52. * fashion for load balancing. They can also be used for QoS. for that
  53. * to work, however, QoS information needs to be exposed down to the driver
  54. * level so that subqueues get targetted to particular transmit rings.
  55. * alternatively, the queues can be configured via use of the all-purpose
  56. * ioctl.
  57. *
  58. * RX DATA: the rx completion ring has all the info, but the rx desc
  59. * ring has all of the data. RX can conceivably come in under multiple
  60. * interrupts, but the INT# assignment needs to be set up properly by
  61. * the BIOS and conveyed to the driver. PCI BIOSes don't know how to do
  62. * that. also, the two descriptor rings are designed to distinguish between
  63. * encrypted and non-encrypted packets, but we use them for buffering
  64. * instead.
  65. *
  66. * by default, the selective clear mask is set up to process rx packets.
  67. */
  68. #include <linux/module.h>
  69. #include <linux/kernel.h>
  70. #include <linux/types.h>
  71. #include <linux/compiler.h>
  72. #include <linux/slab.h>
  73. #include <linux/delay.h>
  74. #include <linux/init.h>
  75. #include <linux/vmalloc.h>
  76. #include <linux/ioport.h>
  77. #include <linux/pci.h>
  78. #include <linux/mm.h>
  79. #include <linux/highmem.h>
  80. #include <linux/list.h>
  81. #include <linux/dma-mapping.h>
  82. #include <linux/netdevice.h>
  83. #include <linux/etherdevice.h>
  84. #include <linux/skbuff.h>
  85. #include <linux/ethtool.h>
  86. #include <linux/crc32.h>
  87. #include <linux/random.h>
  88. #include <linux/mii.h>
  89. #include <linux/ip.h>
  90. #include <linux/tcp.h>
  91. #include <linux/mutex.h>
  92. #include <linux/firmware.h>
  93. #include <net/checksum.h>
  94. #include <asm/atomic.h>
  95. #include <asm/system.h>
  96. #include <asm/io.h>
  97. #include <asm/byteorder.h>
  98. #include <asm/uaccess.h>
  99. #define cas_page_map(x) kmap_atomic((x), KM_SKB_DATA_SOFTIRQ)
  100. #define cas_page_unmap(x) kunmap_atomic((x), KM_SKB_DATA_SOFTIRQ)
  101. #define CAS_NCPUS num_online_cpus()
  102. #ifdef CONFIG_CASSINI_NAPI
  103. #define USE_NAPI
  104. #define cas_skb_release(x) netif_receive_skb(x)
  105. #else
  106. #define cas_skb_release(x) netif_rx(x)
  107. #endif
  108. /* select which firmware to use */
  109. #define USE_HP_WORKAROUND
  110. #define HP_WORKAROUND_DEFAULT /* select which firmware to use as default */
  111. #define CAS_HP_ALT_FIRMWARE cas_prog_null /* alternate firmware */
  112. #include "cassini.h"
  113. #define USE_TX_COMPWB /* use completion writeback registers */
  114. #define USE_CSMA_CD_PROTO /* standard CSMA/CD */
  115. #define USE_RX_BLANK /* hw interrupt mitigation */
  116. #undef USE_ENTROPY_DEV /* don't test for entropy device */
  117. /* NOTE: these aren't useable unless PCI interrupts can be assigned.
  118. * also, we need to make cp->lock finer-grained.
  119. */
  120. #undef USE_PCI_INTB
  121. #undef USE_PCI_INTC
  122. #undef USE_PCI_INTD
  123. #undef USE_QOS
  124. #undef USE_VPD_DEBUG /* debug vpd information if defined */
  125. /* rx processing options */
  126. #define USE_PAGE_ORDER /* specify to allocate large rx pages */
  127. #define RX_DONT_BATCH 0 /* if 1, don't batch flows */
  128. #define RX_COPY_ALWAYS 0 /* if 0, use frags */
  129. #define RX_COPY_MIN 64 /* copy a little to make upper layers happy */
  130. #undef RX_COUNT_BUFFERS /* define to calculate RX buffer stats */
  131. #define DRV_MODULE_NAME "cassini"
  132. #define PFX DRV_MODULE_NAME ": "
  133. #define DRV_MODULE_VERSION "1.6"
  134. #define DRV_MODULE_RELDATE "21 May 2008"
  135. #define CAS_DEF_MSG_ENABLE \
  136. (NETIF_MSG_DRV | \
  137. NETIF_MSG_PROBE | \
  138. NETIF_MSG_LINK | \
  139. NETIF_MSG_TIMER | \
  140. NETIF_MSG_IFDOWN | \
  141. NETIF_MSG_IFUP | \
  142. NETIF_MSG_RX_ERR | \
  143. NETIF_MSG_TX_ERR)
  144. /* length of time before we decide the hardware is borked,
  145. * and dev->tx_timeout() should be called to fix the problem
  146. */
  147. #define CAS_TX_TIMEOUT (HZ)
  148. #define CAS_LINK_TIMEOUT (22*HZ/10)
  149. #define CAS_LINK_FAST_TIMEOUT (1)
  150. /* timeout values for state changing. these specify the number
  151. * of 10us delays to be used before giving up.
  152. */
  153. #define STOP_TRIES_PHY 1000
  154. #define STOP_TRIES 5000
  155. /* specify a minimum frame size to deal with some fifo issues
  156. * max mtu == 2 * page size - ethernet header - 64 - swivel =
  157. * 2 * page_size - 0x50
  158. */
  159. #define CAS_MIN_FRAME 97
  160. #define CAS_1000MB_MIN_FRAME 255
  161. #define CAS_MIN_MTU 60
  162. #define CAS_MAX_MTU min(((cp->page_size << 1) - 0x50), 9000)
  163. #if 1
  164. /*
  165. * Eliminate these and use separate atomic counters for each, to
  166. * avoid a race condition.
  167. */
  168. #else
  169. #define CAS_RESET_MTU 1
  170. #define CAS_RESET_ALL 2
  171. #define CAS_RESET_SPARE 3
  172. #endif
  173. static char version[] __devinitdata =
  174. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  175. static int cassini_debug = -1; /* -1 == use CAS_DEF_MSG_ENABLE as value */
  176. static int link_mode;
  177. MODULE_AUTHOR("Adrian Sun (asun@darksunrising.com)");
  178. MODULE_DESCRIPTION("Sun Cassini(+) ethernet driver");
  179. MODULE_LICENSE("GPL");
  180. MODULE_FIRMWARE("sun/cassini.bin");
  181. module_param(cassini_debug, int, 0);
  182. MODULE_PARM_DESC(cassini_debug, "Cassini bitmapped debugging message enable value");
  183. module_param(link_mode, int, 0);
  184. MODULE_PARM_DESC(link_mode, "default link mode");
  185. /*
  186. * Work around for a PCS bug in which the link goes down due to the chip
  187. * being confused and never showing a link status of "up."
  188. */
  189. #define DEFAULT_LINKDOWN_TIMEOUT 5
  190. /*
  191. * Value in seconds, for user input.
  192. */
  193. static int linkdown_timeout = DEFAULT_LINKDOWN_TIMEOUT;
  194. module_param(linkdown_timeout, int, 0);
  195. MODULE_PARM_DESC(linkdown_timeout,
  196. "min reset interval in sec. for PCS linkdown issue; disabled if not positive");
  197. /*
  198. * value in 'ticks' (units used by jiffies). Set when we init the
  199. * module because 'HZ' in actually a function call on some flavors of
  200. * Linux. This will default to DEFAULT_LINKDOWN_TIMEOUT * HZ.
  201. */
  202. static int link_transition_timeout;
  203. static u16 link_modes[] __devinitdata = {
  204. BMCR_ANENABLE, /* 0 : autoneg */
  205. 0, /* 1 : 10bt half duplex */
  206. BMCR_SPEED100, /* 2 : 100bt half duplex */
  207. BMCR_FULLDPLX, /* 3 : 10bt full duplex */
  208. BMCR_SPEED100|BMCR_FULLDPLX, /* 4 : 100bt full duplex */
  209. CAS_BMCR_SPEED1000|BMCR_FULLDPLX /* 5 : 1000bt full duplex */
  210. };
  211. static DEFINE_PCI_DEVICE_TABLE(cas_pci_tbl) = {
  212. { PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_CASSINI,
  213. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  214. { PCI_VENDOR_ID_NS, PCI_DEVICE_ID_NS_SATURN,
  215. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0UL },
  216. { 0, }
  217. };
  218. MODULE_DEVICE_TABLE(pci, cas_pci_tbl);
  219. static void cas_set_link_modes(struct cas *cp);
  220. static inline void cas_lock_tx(struct cas *cp)
  221. {
  222. int i;
  223. for (i = 0; i < N_TX_RINGS; i++)
  224. spin_lock(&cp->tx_lock[i]);
  225. }
  226. static inline void cas_lock_all(struct cas *cp)
  227. {
  228. spin_lock_irq(&cp->lock);
  229. cas_lock_tx(cp);
  230. }
  231. /* WTZ: QA was finding deadlock problems with the previous
  232. * versions after long test runs with multiple cards per machine.
  233. * See if replacing cas_lock_all with safer versions helps. The
  234. * symptoms QA is reporting match those we'd expect if interrupts
  235. * aren't being properly restored, and we fixed a previous deadlock
  236. * with similar symptoms by using save/restore versions in other
  237. * places.
  238. */
  239. #define cas_lock_all_save(cp, flags) \
  240. do { \
  241. struct cas *xxxcp = (cp); \
  242. spin_lock_irqsave(&xxxcp->lock, flags); \
  243. cas_lock_tx(xxxcp); \
  244. } while (0)
  245. static inline void cas_unlock_tx(struct cas *cp)
  246. {
  247. int i;
  248. for (i = N_TX_RINGS; i > 0; i--)
  249. spin_unlock(&cp->tx_lock[i - 1]);
  250. }
  251. static inline void cas_unlock_all(struct cas *cp)
  252. {
  253. cas_unlock_tx(cp);
  254. spin_unlock_irq(&cp->lock);
  255. }
  256. #define cas_unlock_all_restore(cp, flags) \
  257. do { \
  258. struct cas *xxxcp = (cp); \
  259. cas_unlock_tx(xxxcp); \
  260. spin_unlock_irqrestore(&xxxcp->lock, flags); \
  261. } while (0)
  262. static void cas_disable_irq(struct cas *cp, const int ring)
  263. {
  264. /* Make sure we won't get any more interrupts */
  265. if (ring == 0) {
  266. writel(0xFFFFFFFF, cp->regs + REG_INTR_MASK);
  267. return;
  268. }
  269. /* disable completion interrupts and selectively mask */
  270. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  271. switch (ring) {
  272. #if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
  273. #ifdef USE_PCI_INTB
  274. case 1:
  275. #endif
  276. #ifdef USE_PCI_INTC
  277. case 2:
  278. #endif
  279. #ifdef USE_PCI_INTD
  280. case 3:
  281. #endif
  282. writel(INTRN_MASK_CLEAR_ALL | INTRN_MASK_RX_EN,
  283. cp->regs + REG_PLUS_INTRN_MASK(ring));
  284. break;
  285. #endif
  286. default:
  287. writel(INTRN_MASK_CLEAR_ALL, cp->regs +
  288. REG_PLUS_INTRN_MASK(ring));
  289. break;
  290. }
  291. }
  292. }
  293. static inline void cas_mask_intr(struct cas *cp)
  294. {
  295. int i;
  296. for (i = 0; i < N_RX_COMP_RINGS; i++)
  297. cas_disable_irq(cp, i);
  298. }
  299. static void cas_enable_irq(struct cas *cp, const int ring)
  300. {
  301. if (ring == 0) { /* all but TX_DONE */
  302. writel(INTR_TX_DONE, cp->regs + REG_INTR_MASK);
  303. return;
  304. }
  305. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  306. switch (ring) {
  307. #if defined (USE_PCI_INTB) || defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
  308. #ifdef USE_PCI_INTB
  309. case 1:
  310. #endif
  311. #ifdef USE_PCI_INTC
  312. case 2:
  313. #endif
  314. #ifdef USE_PCI_INTD
  315. case 3:
  316. #endif
  317. writel(INTRN_MASK_RX_EN, cp->regs +
  318. REG_PLUS_INTRN_MASK(ring));
  319. break;
  320. #endif
  321. default:
  322. break;
  323. }
  324. }
  325. }
  326. static inline void cas_unmask_intr(struct cas *cp)
  327. {
  328. int i;
  329. for (i = 0; i < N_RX_COMP_RINGS; i++)
  330. cas_enable_irq(cp, i);
  331. }
  332. static inline void cas_entropy_gather(struct cas *cp)
  333. {
  334. #ifdef USE_ENTROPY_DEV
  335. if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
  336. return;
  337. batch_entropy_store(readl(cp->regs + REG_ENTROPY_IV),
  338. readl(cp->regs + REG_ENTROPY_IV),
  339. sizeof(uint64_t)*8);
  340. #endif
  341. }
  342. static inline void cas_entropy_reset(struct cas *cp)
  343. {
  344. #ifdef USE_ENTROPY_DEV
  345. if ((cp->cas_flags & CAS_FLAG_ENTROPY_DEV) == 0)
  346. return;
  347. writel(BIM_LOCAL_DEV_PAD | BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_EXT,
  348. cp->regs + REG_BIM_LOCAL_DEV_EN);
  349. writeb(ENTROPY_RESET_STC_MODE, cp->regs + REG_ENTROPY_RESET);
  350. writeb(0x55, cp->regs + REG_ENTROPY_RAND_REG);
  351. /* if we read back 0x0, we don't have an entropy device */
  352. if (readb(cp->regs + REG_ENTROPY_RAND_REG) == 0)
  353. cp->cas_flags &= ~CAS_FLAG_ENTROPY_DEV;
  354. #endif
  355. }
  356. /* access to the phy. the following assumes that we've initialized the MIF to
  357. * be in frame rather than bit-bang mode
  358. */
  359. static u16 cas_phy_read(struct cas *cp, int reg)
  360. {
  361. u32 cmd;
  362. int limit = STOP_TRIES_PHY;
  363. cmd = MIF_FRAME_ST | MIF_FRAME_OP_READ;
  364. cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
  365. cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
  366. cmd |= MIF_FRAME_TURN_AROUND_MSB;
  367. writel(cmd, cp->regs + REG_MIF_FRAME);
  368. /* poll for completion */
  369. while (limit-- > 0) {
  370. udelay(10);
  371. cmd = readl(cp->regs + REG_MIF_FRAME);
  372. if (cmd & MIF_FRAME_TURN_AROUND_LSB)
  373. return (cmd & MIF_FRAME_DATA_MASK);
  374. }
  375. return 0xFFFF; /* -1 */
  376. }
  377. static int cas_phy_write(struct cas *cp, int reg, u16 val)
  378. {
  379. int limit = STOP_TRIES_PHY;
  380. u32 cmd;
  381. cmd = MIF_FRAME_ST | MIF_FRAME_OP_WRITE;
  382. cmd |= CAS_BASE(MIF_FRAME_PHY_ADDR, cp->phy_addr);
  383. cmd |= CAS_BASE(MIF_FRAME_REG_ADDR, reg);
  384. cmd |= MIF_FRAME_TURN_AROUND_MSB;
  385. cmd |= val & MIF_FRAME_DATA_MASK;
  386. writel(cmd, cp->regs + REG_MIF_FRAME);
  387. /* poll for completion */
  388. while (limit-- > 0) {
  389. udelay(10);
  390. cmd = readl(cp->regs + REG_MIF_FRAME);
  391. if (cmd & MIF_FRAME_TURN_AROUND_LSB)
  392. return 0;
  393. }
  394. return -1;
  395. }
  396. static void cas_phy_powerup(struct cas *cp)
  397. {
  398. u16 ctl = cas_phy_read(cp, MII_BMCR);
  399. if ((ctl & BMCR_PDOWN) == 0)
  400. return;
  401. ctl &= ~BMCR_PDOWN;
  402. cas_phy_write(cp, MII_BMCR, ctl);
  403. }
  404. static void cas_phy_powerdown(struct cas *cp)
  405. {
  406. u16 ctl = cas_phy_read(cp, MII_BMCR);
  407. if (ctl & BMCR_PDOWN)
  408. return;
  409. ctl |= BMCR_PDOWN;
  410. cas_phy_write(cp, MII_BMCR, ctl);
  411. }
  412. /* cp->lock held. note: the last put_page will free the buffer */
  413. static int cas_page_free(struct cas *cp, cas_page_t *page)
  414. {
  415. pci_unmap_page(cp->pdev, page->dma_addr, cp->page_size,
  416. PCI_DMA_FROMDEVICE);
  417. __free_pages(page->buffer, cp->page_order);
  418. kfree(page);
  419. return 0;
  420. }
  421. #ifdef RX_COUNT_BUFFERS
  422. #define RX_USED_ADD(x, y) ((x)->used += (y))
  423. #define RX_USED_SET(x, y) ((x)->used = (y))
  424. #else
  425. #define RX_USED_ADD(x, y)
  426. #define RX_USED_SET(x, y)
  427. #endif
  428. /* local page allocation routines for the receive buffers. jumbo pages
  429. * require at least 8K contiguous and 8K aligned buffers.
  430. */
  431. static cas_page_t *cas_page_alloc(struct cas *cp, const gfp_t flags)
  432. {
  433. cas_page_t *page;
  434. page = kmalloc(sizeof(cas_page_t), flags);
  435. if (!page)
  436. return NULL;
  437. INIT_LIST_HEAD(&page->list);
  438. RX_USED_SET(page, 0);
  439. page->buffer = alloc_pages(flags, cp->page_order);
  440. if (!page->buffer)
  441. goto page_err;
  442. page->dma_addr = pci_map_page(cp->pdev, page->buffer, 0,
  443. cp->page_size, PCI_DMA_FROMDEVICE);
  444. return page;
  445. page_err:
  446. kfree(page);
  447. return NULL;
  448. }
  449. /* initialize spare pool of rx buffers, but allocate during the open */
  450. static void cas_spare_init(struct cas *cp)
  451. {
  452. spin_lock(&cp->rx_inuse_lock);
  453. INIT_LIST_HEAD(&cp->rx_inuse_list);
  454. spin_unlock(&cp->rx_inuse_lock);
  455. spin_lock(&cp->rx_spare_lock);
  456. INIT_LIST_HEAD(&cp->rx_spare_list);
  457. cp->rx_spares_needed = RX_SPARE_COUNT;
  458. spin_unlock(&cp->rx_spare_lock);
  459. }
  460. /* used on close. free all the spare buffers. */
  461. static void cas_spare_free(struct cas *cp)
  462. {
  463. struct list_head list, *elem, *tmp;
  464. /* free spare buffers */
  465. INIT_LIST_HEAD(&list);
  466. spin_lock(&cp->rx_spare_lock);
  467. list_splice_init(&cp->rx_spare_list, &list);
  468. spin_unlock(&cp->rx_spare_lock);
  469. list_for_each_safe(elem, tmp, &list) {
  470. cas_page_free(cp, list_entry(elem, cas_page_t, list));
  471. }
  472. INIT_LIST_HEAD(&list);
  473. #if 1
  474. /*
  475. * Looks like Adrian had protected this with a different
  476. * lock than used everywhere else to manipulate this list.
  477. */
  478. spin_lock(&cp->rx_inuse_lock);
  479. list_splice_init(&cp->rx_inuse_list, &list);
  480. spin_unlock(&cp->rx_inuse_lock);
  481. #else
  482. spin_lock(&cp->rx_spare_lock);
  483. list_splice_init(&cp->rx_inuse_list, &list);
  484. spin_unlock(&cp->rx_spare_lock);
  485. #endif
  486. list_for_each_safe(elem, tmp, &list) {
  487. cas_page_free(cp, list_entry(elem, cas_page_t, list));
  488. }
  489. }
  490. /* replenish spares if needed */
  491. static void cas_spare_recover(struct cas *cp, const gfp_t flags)
  492. {
  493. struct list_head list, *elem, *tmp;
  494. int needed, i;
  495. /* check inuse list. if we don't need any more free buffers,
  496. * just free it
  497. */
  498. /* make a local copy of the list */
  499. INIT_LIST_HEAD(&list);
  500. spin_lock(&cp->rx_inuse_lock);
  501. list_splice_init(&cp->rx_inuse_list, &list);
  502. spin_unlock(&cp->rx_inuse_lock);
  503. list_for_each_safe(elem, tmp, &list) {
  504. cas_page_t *page = list_entry(elem, cas_page_t, list);
  505. /*
  506. * With the lockless pagecache, cassini buffering scheme gets
  507. * slightly less accurate: we might find that a page has an
  508. * elevated reference count here, due to a speculative ref,
  509. * and skip it as in-use. Ideally we would be able to reclaim
  510. * it. However this would be such a rare case, it doesn't
  511. * matter too much as we should pick it up the next time round.
  512. *
  513. * Importantly, if we find that the page has a refcount of 1
  514. * here (our refcount), then we know it is definitely not inuse
  515. * so we can reuse it.
  516. */
  517. if (page_count(page->buffer) > 1)
  518. continue;
  519. list_del(elem);
  520. spin_lock(&cp->rx_spare_lock);
  521. if (cp->rx_spares_needed > 0) {
  522. list_add(elem, &cp->rx_spare_list);
  523. cp->rx_spares_needed--;
  524. spin_unlock(&cp->rx_spare_lock);
  525. } else {
  526. spin_unlock(&cp->rx_spare_lock);
  527. cas_page_free(cp, page);
  528. }
  529. }
  530. /* put any inuse buffers back on the list */
  531. if (!list_empty(&list)) {
  532. spin_lock(&cp->rx_inuse_lock);
  533. list_splice(&list, &cp->rx_inuse_list);
  534. spin_unlock(&cp->rx_inuse_lock);
  535. }
  536. spin_lock(&cp->rx_spare_lock);
  537. needed = cp->rx_spares_needed;
  538. spin_unlock(&cp->rx_spare_lock);
  539. if (!needed)
  540. return;
  541. /* we still need spares, so try to allocate some */
  542. INIT_LIST_HEAD(&list);
  543. i = 0;
  544. while (i < needed) {
  545. cas_page_t *spare = cas_page_alloc(cp, flags);
  546. if (!spare)
  547. break;
  548. list_add(&spare->list, &list);
  549. i++;
  550. }
  551. spin_lock(&cp->rx_spare_lock);
  552. list_splice(&list, &cp->rx_spare_list);
  553. cp->rx_spares_needed -= i;
  554. spin_unlock(&cp->rx_spare_lock);
  555. }
  556. /* pull a page from the list. */
  557. static cas_page_t *cas_page_dequeue(struct cas *cp)
  558. {
  559. struct list_head *entry;
  560. int recover;
  561. spin_lock(&cp->rx_spare_lock);
  562. if (list_empty(&cp->rx_spare_list)) {
  563. /* try to do a quick recovery */
  564. spin_unlock(&cp->rx_spare_lock);
  565. cas_spare_recover(cp, GFP_ATOMIC);
  566. spin_lock(&cp->rx_spare_lock);
  567. if (list_empty(&cp->rx_spare_list)) {
  568. if (netif_msg_rx_err(cp))
  569. printk(KERN_ERR "%s: no spare buffers "
  570. "available.\n", cp->dev->name);
  571. spin_unlock(&cp->rx_spare_lock);
  572. return NULL;
  573. }
  574. }
  575. entry = cp->rx_spare_list.next;
  576. list_del(entry);
  577. recover = ++cp->rx_spares_needed;
  578. spin_unlock(&cp->rx_spare_lock);
  579. /* trigger the timer to do the recovery */
  580. if ((recover & (RX_SPARE_RECOVER_VAL - 1)) == 0) {
  581. #if 1
  582. atomic_inc(&cp->reset_task_pending);
  583. atomic_inc(&cp->reset_task_pending_spare);
  584. schedule_work(&cp->reset_task);
  585. #else
  586. atomic_set(&cp->reset_task_pending, CAS_RESET_SPARE);
  587. schedule_work(&cp->reset_task);
  588. #endif
  589. }
  590. return list_entry(entry, cas_page_t, list);
  591. }
  592. static void cas_mif_poll(struct cas *cp, const int enable)
  593. {
  594. u32 cfg;
  595. cfg = readl(cp->regs + REG_MIF_CFG);
  596. cfg &= (MIF_CFG_MDIO_0 | MIF_CFG_MDIO_1);
  597. if (cp->phy_type & CAS_PHY_MII_MDIO1)
  598. cfg |= MIF_CFG_PHY_SELECT;
  599. /* poll and interrupt on link status change. */
  600. if (enable) {
  601. cfg |= MIF_CFG_POLL_EN;
  602. cfg |= CAS_BASE(MIF_CFG_POLL_REG, MII_BMSR);
  603. cfg |= CAS_BASE(MIF_CFG_POLL_PHY, cp->phy_addr);
  604. }
  605. writel((enable) ? ~(BMSR_LSTATUS | BMSR_ANEGCOMPLETE) : 0xFFFF,
  606. cp->regs + REG_MIF_MASK);
  607. writel(cfg, cp->regs + REG_MIF_CFG);
  608. }
  609. /* Must be invoked under cp->lock */
  610. static void cas_begin_auto_negotiation(struct cas *cp, struct ethtool_cmd *ep)
  611. {
  612. u16 ctl;
  613. #if 1
  614. int lcntl;
  615. int changed = 0;
  616. int oldstate = cp->lstate;
  617. int link_was_not_down = !(oldstate == link_down);
  618. #endif
  619. /* Setup link parameters */
  620. if (!ep)
  621. goto start_aneg;
  622. lcntl = cp->link_cntl;
  623. if (ep->autoneg == AUTONEG_ENABLE)
  624. cp->link_cntl = BMCR_ANENABLE;
  625. else {
  626. cp->link_cntl = 0;
  627. if (ep->speed == SPEED_100)
  628. cp->link_cntl |= BMCR_SPEED100;
  629. else if (ep->speed == SPEED_1000)
  630. cp->link_cntl |= CAS_BMCR_SPEED1000;
  631. if (ep->duplex == DUPLEX_FULL)
  632. cp->link_cntl |= BMCR_FULLDPLX;
  633. }
  634. #if 1
  635. changed = (lcntl != cp->link_cntl);
  636. #endif
  637. start_aneg:
  638. if (cp->lstate == link_up) {
  639. printk(KERN_INFO "%s: PCS link down.\n",
  640. cp->dev->name);
  641. } else {
  642. if (changed) {
  643. printk(KERN_INFO "%s: link configuration changed\n",
  644. cp->dev->name);
  645. }
  646. }
  647. cp->lstate = link_down;
  648. cp->link_transition = LINK_TRANSITION_LINK_DOWN;
  649. if (!cp->hw_running)
  650. return;
  651. #if 1
  652. /*
  653. * WTZ: If the old state was link_up, we turn off the carrier
  654. * to replicate everything we do elsewhere on a link-down
  655. * event when we were already in a link-up state..
  656. */
  657. if (oldstate == link_up)
  658. netif_carrier_off(cp->dev);
  659. if (changed && link_was_not_down) {
  660. /*
  661. * WTZ: This branch will simply schedule a full reset after
  662. * we explicitly changed link modes in an ioctl. See if this
  663. * fixes the link-problems we were having for forced mode.
  664. */
  665. atomic_inc(&cp->reset_task_pending);
  666. atomic_inc(&cp->reset_task_pending_all);
  667. schedule_work(&cp->reset_task);
  668. cp->timer_ticks = 0;
  669. mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
  670. return;
  671. }
  672. #endif
  673. if (cp->phy_type & CAS_PHY_SERDES) {
  674. u32 val = readl(cp->regs + REG_PCS_MII_CTRL);
  675. if (cp->link_cntl & BMCR_ANENABLE) {
  676. val |= (PCS_MII_RESTART_AUTONEG | PCS_MII_AUTONEG_EN);
  677. cp->lstate = link_aneg;
  678. } else {
  679. if (cp->link_cntl & BMCR_FULLDPLX)
  680. val |= PCS_MII_CTRL_DUPLEX;
  681. val &= ~PCS_MII_AUTONEG_EN;
  682. cp->lstate = link_force_ok;
  683. }
  684. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  685. writel(val, cp->regs + REG_PCS_MII_CTRL);
  686. } else {
  687. cas_mif_poll(cp, 0);
  688. ctl = cas_phy_read(cp, MII_BMCR);
  689. ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 |
  690. CAS_BMCR_SPEED1000 | BMCR_ANENABLE);
  691. ctl |= cp->link_cntl;
  692. if (ctl & BMCR_ANENABLE) {
  693. ctl |= BMCR_ANRESTART;
  694. cp->lstate = link_aneg;
  695. } else {
  696. cp->lstate = link_force_ok;
  697. }
  698. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  699. cas_phy_write(cp, MII_BMCR, ctl);
  700. cas_mif_poll(cp, 1);
  701. }
  702. cp->timer_ticks = 0;
  703. mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
  704. }
  705. /* Must be invoked under cp->lock. */
  706. static int cas_reset_mii_phy(struct cas *cp)
  707. {
  708. int limit = STOP_TRIES_PHY;
  709. u16 val;
  710. cas_phy_write(cp, MII_BMCR, BMCR_RESET);
  711. udelay(100);
  712. while (--limit) {
  713. val = cas_phy_read(cp, MII_BMCR);
  714. if ((val & BMCR_RESET) == 0)
  715. break;
  716. udelay(10);
  717. }
  718. return (limit <= 0);
  719. }
  720. static int cas_saturn_firmware_init(struct cas *cp)
  721. {
  722. const struct firmware *fw;
  723. const char fw_name[] = "sun/cassini.bin";
  724. int err;
  725. if (PHY_NS_DP83065 != cp->phy_id)
  726. return 0;
  727. err = request_firmware(&fw, fw_name, &cp->pdev->dev);
  728. if (err) {
  729. printk(KERN_ERR "cassini: Failed to load firmware \"%s\"\n",
  730. fw_name);
  731. return err;
  732. }
  733. if (fw->size < 2) {
  734. printk(KERN_ERR "cassini: bogus length %zu in \"%s\"\n",
  735. fw->size, fw_name);
  736. err = -EINVAL;
  737. goto out;
  738. }
  739. cp->fw_load_addr= fw->data[1] << 8 | fw->data[0];
  740. cp->fw_size = fw->size - 2;
  741. cp->fw_data = vmalloc(cp->fw_size);
  742. if (!cp->fw_data) {
  743. err = -ENOMEM;
  744. printk(KERN_ERR "cassini: \"%s\" Failed %d\n", fw_name, err);
  745. goto out;
  746. }
  747. memcpy(cp->fw_data, &fw->data[2], cp->fw_size);
  748. out:
  749. release_firmware(fw);
  750. return err;
  751. }
  752. static void cas_saturn_firmware_load(struct cas *cp)
  753. {
  754. int i;
  755. cas_phy_powerdown(cp);
  756. /* expanded memory access mode */
  757. cas_phy_write(cp, DP83065_MII_MEM, 0x0);
  758. /* pointer configuration for new firmware */
  759. cas_phy_write(cp, DP83065_MII_REGE, 0x8ff9);
  760. cas_phy_write(cp, DP83065_MII_REGD, 0xbd);
  761. cas_phy_write(cp, DP83065_MII_REGE, 0x8ffa);
  762. cas_phy_write(cp, DP83065_MII_REGD, 0x82);
  763. cas_phy_write(cp, DP83065_MII_REGE, 0x8ffb);
  764. cas_phy_write(cp, DP83065_MII_REGD, 0x0);
  765. cas_phy_write(cp, DP83065_MII_REGE, 0x8ffc);
  766. cas_phy_write(cp, DP83065_MII_REGD, 0x39);
  767. /* download new firmware */
  768. cas_phy_write(cp, DP83065_MII_MEM, 0x1);
  769. cas_phy_write(cp, DP83065_MII_REGE, cp->fw_load_addr);
  770. for (i = 0; i < cp->fw_size; i++)
  771. cas_phy_write(cp, DP83065_MII_REGD, cp->fw_data[i]);
  772. /* enable firmware */
  773. cas_phy_write(cp, DP83065_MII_REGE, 0x8ff8);
  774. cas_phy_write(cp, DP83065_MII_REGD, 0x1);
  775. }
  776. /* phy initialization */
  777. static void cas_phy_init(struct cas *cp)
  778. {
  779. u16 val;
  780. /* if we're in MII/GMII mode, set up phy */
  781. if (CAS_PHY_MII(cp->phy_type)) {
  782. writel(PCS_DATAPATH_MODE_MII,
  783. cp->regs + REG_PCS_DATAPATH_MODE);
  784. cas_mif_poll(cp, 0);
  785. cas_reset_mii_phy(cp); /* take out of isolate mode */
  786. if (PHY_LUCENT_B0 == cp->phy_id) {
  787. /* workaround link up/down issue with lucent */
  788. cas_phy_write(cp, LUCENT_MII_REG, 0x8000);
  789. cas_phy_write(cp, MII_BMCR, 0x00f1);
  790. cas_phy_write(cp, LUCENT_MII_REG, 0x0);
  791. } else if (PHY_BROADCOM_B0 == (cp->phy_id & 0xFFFFFFFC)) {
  792. /* workarounds for broadcom phy */
  793. cas_phy_write(cp, BROADCOM_MII_REG8, 0x0C20);
  794. cas_phy_write(cp, BROADCOM_MII_REG7, 0x0012);
  795. cas_phy_write(cp, BROADCOM_MII_REG5, 0x1804);
  796. cas_phy_write(cp, BROADCOM_MII_REG7, 0x0013);
  797. cas_phy_write(cp, BROADCOM_MII_REG5, 0x1204);
  798. cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
  799. cas_phy_write(cp, BROADCOM_MII_REG5, 0x0132);
  800. cas_phy_write(cp, BROADCOM_MII_REG7, 0x8006);
  801. cas_phy_write(cp, BROADCOM_MII_REG5, 0x0232);
  802. cas_phy_write(cp, BROADCOM_MII_REG7, 0x201F);
  803. cas_phy_write(cp, BROADCOM_MII_REG5, 0x0A20);
  804. } else if (PHY_BROADCOM_5411 == cp->phy_id) {
  805. val = cas_phy_read(cp, BROADCOM_MII_REG4);
  806. val = cas_phy_read(cp, BROADCOM_MII_REG4);
  807. if (val & 0x0080) {
  808. /* link workaround */
  809. cas_phy_write(cp, BROADCOM_MII_REG4,
  810. val & ~0x0080);
  811. }
  812. } else if (cp->cas_flags & CAS_FLAG_SATURN) {
  813. writel((cp->phy_type & CAS_PHY_MII_MDIO0) ?
  814. SATURN_PCFG_FSI : 0x0,
  815. cp->regs + REG_SATURN_PCFG);
  816. /* load firmware to address 10Mbps auto-negotiation
  817. * issue. NOTE: this will need to be changed if the
  818. * default firmware gets fixed.
  819. */
  820. if (PHY_NS_DP83065 == cp->phy_id) {
  821. cas_saturn_firmware_load(cp);
  822. }
  823. cas_phy_powerup(cp);
  824. }
  825. /* advertise capabilities */
  826. val = cas_phy_read(cp, MII_BMCR);
  827. val &= ~BMCR_ANENABLE;
  828. cas_phy_write(cp, MII_BMCR, val);
  829. udelay(10);
  830. cas_phy_write(cp, MII_ADVERTISE,
  831. cas_phy_read(cp, MII_ADVERTISE) |
  832. (ADVERTISE_10HALF | ADVERTISE_10FULL |
  833. ADVERTISE_100HALF | ADVERTISE_100FULL |
  834. CAS_ADVERTISE_PAUSE |
  835. CAS_ADVERTISE_ASYM_PAUSE));
  836. if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
  837. /* make sure that we don't advertise half
  838. * duplex to avoid a chip issue
  839. */
  840. val = cas_phy_read(cp, CAS_MII_1000_CTRL);
  841. val &= ~CAS_ADVERTISE_1000HALF;
  842. val |= CAS_ADVERTISE_1000FULL;
  843. cas_phy_write(cp, CAS_MII_1000_CTRL, val);
  844. }
  845. } else {
  846. /* reset pcs for serdes */
  847. u32 val;
  848. int limit;
  849. writel(PCS_DATAPATH_MODE_SERDES,
  850. cp->regs + REG_PCS_DATAPATH_MODE);
  851. /* enable serdes pins on saturn */
  852. if (cp->cas_flags & CAS_FLAG_SATURN)
  853. writel(0, cp->regs + REG_SATURN_PCFG);
  854. /* Reset PCS unit. */
  855. val = readl(cp->regs + REG_PCS_MII_CTRL);
  856. val |= PCS_MII_RESET;
  857. writel(val, cp->regs + REG_PCS_MII_CTRL);
  858. limit = STOP_TRIES;
  859. while (--limit > 0) {
  860. udelay(10);
  861. if ((readl(cp->regs + REG_PCS_MII_CTRL) &
  862. PCS_MII_RESET) == 0)
  863. break;
  864. }
  865. if (limit <= 0)
  866. printk(KERN_WARNING "%s: PCS reset bit would not "
  867. "clear [%08x].\n", cp->dev->name,
  868. readl(cp->regs + REG_PCS_STATE_MACHINE));
  869. /* Make sure PCS is disabled while changing advertisement
  870. * configuration.
  871. */
  872. writel(0x0, cp->regs + REG_PCS_CFG);
  873. /* Advertise all capabilities except half-duplex. */
  874. val = readl(cp->regs + REG_PCS_MII_ADVERT);
  875. val &= ~PCS_MII_ADVERT_HD;
  876. val |= (PCS_MII_ADVERT_FD | PCS_MII_ADVERT_SYM_PAUSE |
  877. PCS_MII_ADVERT_ASYM_PAUSE);
  878. writel(val, cp->regs + REG_PCS_MII_ADVERT);
  879. /* enable PCS */
  880. writel(PCS_CFG_EN, cp->regs + REG_PCS_CFG);
  881. /* pcs workaround: enable sync detect */
  882. writel(PCS_SERDES_CTRL_SYNCD_EN,
  883. cp->regs + REG_PCS_SERDES_CTRL);
  884. }
  885. }
  886. static int cas_pcs_link_check(struct cas *cp)
  887. {
  888. u32 stat, state_machine;
  889. int retval = 0;
  890. /* The link status bit latches on zero, so you must
  891. * read it twice in such a case to see a transition
  892. * to the link being up.
  893. */
  894. stat = readl(cp->regs + REG_PCS_MII_STATUS);
  895. if ((stat & PCS_MII_STATUS_LINK_STATUS) == 0)
  896. stat = readl(cp->regs + REG_PCS_MII_STATUS);
  897. /* The remote-fault indication is only valid
  898. * when autoneg has completed.
  899. */
  900. if ((stat & (PCS_MII_STATUS_AUTONEG_COMP |
  901. PCS_MII_STATUS_REMOTE_FAULT)) ==
  902. (PCS_MII_STATUS_AUTONEG_COMP | PCS_MII_STATUS_REMOTE_FAULT)) {
  903. if (netif_msg_link(cp))
  904. printk(KERN_INFO "%s: PCS RemoteFault\n",
  905. cp->dev->name);
  906. }
  907. /* work around link detection issue by querying the PCS state
  908. * machine directly.
  909. */
  910. state_machine = readl(cp->regs + REG_PCS_STATE_MACHINE);
  911. if ((state_machine & PCS_SM_LINK_STATE_MASK) != SM_LINK_STATE_UP) {
  912. stat &= ~PCS_MII_STATUS_LINK_STATUS;
  913. } else if (state_machine & PCS_SM_WORD_SYNC_STATE_MASK) {
  914. stat |= PCS_MII_STATUS_LINK_STATUS;
  915. }
  916. if (stat & PCS_MII_STATUS_LINK_STATUS) {
  917. if (cp->lstate != link_up) {
  918. if (cp->opened) {
  919. cp->lstate = link_up;
  920. cp->link_transition = LINK_TRANSITION_LINK_UP;
  921. cas_set_link_modes(cp);
  922. netif_carrier_on(cp->dev);
  923. }
  924. }
  925. } else if (cp->lstate == link_up) {
  926. cp->lstate = link_down;
  927. if (link_transition_timeout != 0 &&
  928. cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
  929. !cp->link_transition_jiffies_valid) {
  930. /*
  931. * force a reset, as a workaround for the
  932. * link-failure problem. May want to move this to a
  933. * point a bit earlier in the sequence. If we had
  934. * generated a reset a short time ago, we'll wait for
  935. * the link timer to check the status until a
  936. * timer expires (link_transistion_jiffies_valid is
  937. * true when the timer is running.) Instead of using
  938. * a system timer, we just do a check whenever the
  939. * link timer is running - this clears the flag after
  940. * a suitable delay.
  941. */
  942. retval = 1;
  943. cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
  944. cp->link_transition_jiffies = jiffies;
  945. cp->link_transition_jiffies_valid = 1;
  946. } else {
  947. cp->link_transition = LINK_TRANSITION_ON_FAILURE;
  948. }
  949. netif_carrier_off(cp->dev);
  950. if (cp->opened && netif_msg_link(cp)) {
  951. printk(KERN_INFO "%s: PCS link down.\n",
  952. cp->dev->name);
  953. }
  954. /* Cassini only: if you force a mode, there can be
  955. * sync problems on link down. to fix that, the following
  956. * things need to be checked:
  957. * 1) read serialink state register
  958. * 2) read pcs status register to verify link down.
  959. * 3) if link down and serial link == 0x03, then you need
  960. * to global reset the chip.
  961. */
  962. if ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0) {
  963. /* should check to see if we're in a forced mode */
  964. stat = readl(cp->regs + REG_PCS_SERDES_STATE);
  965. if (stat == 0x03)
  966. return 1;
  967. }
  968. } else if (cp->lstate == link_down) {
  969. if (link_transition_timeout != 0 &&
  970. cp->link_transition != LINK_TRANSITION_REQUESTED_RESET &&
  971. !cp->link_transition_jiffies_valid) {
  972. /* force a reset, as a workaround for the
  973. * link-failure problem. May want to move
  974. * this to a point a bit earlier in the
  975. * sequence.
  976. */
  977. retval = 1;
  978. cp->link_transition = LINK_TRANSITION_REQUESTED_RESET;
  979. cp->link_transition_jiffies = jiffies;
  980. cp->link_transition_jiffies_valid = 1;
  981. } else {
  982. cp->link_transition = LINK_TRANSITION_STILL_FAILED;
  983. }
  984. }
  985. return retval;
  986. }
  987. static int cas_pcs_interrupt(struct net_device *dev,
  988. struct cas *cp, u32 status)
  989. {
  990. u32 stat = readl(cp->regs + REG_PCS_INTR_STATUS);
  991. if ((stat & PCS_INTR_STATUS_LINK_CHANGE) == 0)
  992. return 0;
  993. return cas_pcs_link_check(cp);
  994. }
  995. static int cas_txmac_interrupt(struct net_device *dev,
  996. struct cas *cp, u32 status)
  997. {
  998. u32 txmac_stat = readl(cp->regs + REG_MAC_TX_STATUS);
  999. if (!txmac_stat)
  1000. return 0;
  1001. if (netif_msg_intr(cp))
  1002. printk(KERN_DEBUG "%s: txmac interrupt, txmac_stat: 0x%x\n",
  1003. cp->dev->name, txmac_stat);
  1004. /* Defer timer expiration is quite normal,
  1005. * don't even log the event.
  1006. */
  1007. if ((txmac_stat & MAC_TX_DEFER_TIMER) &&
  1008. !(txmac_stat & ~MAC_TX_DEFER_TIMER))
  1009. return 0;
  1010. spin_lock(&cp->stat_lock[0]);
  1011. if (txmac_stat & MAC_TX_UNDERRUN) {
  1012. printk(KERN_ERR "%s: TX MAC xmit underrun.\n",
  1013. dev->name);
  1014. cp->net_stats[0].tx_fifo_errors++;
  1015. }
  1016. if (txmac_stat & MAC_TX_MAX_PACKET_ERR) {
  1017. printk(KERN_ERR "%s: TX MAC max packet size error.\n",
  1018. dev->name);
  1019. cp->net_stats[0].tx_errors++;
  1020. }
  1021. /* The rest are all cases of one of the 16-bit TX
  1022. * counters expiring.
  1023. */
  1024. if (txmac_stat & MAC_TX_COLL_NORMAL)
  1025. cp->net_stats[0].collisions += 0x10000;
  1026. if (txmac_stat & MAC_TX_COLL_EXCESS) {
  1027. cp->net_stats[0].tx_aborted_errors += 0x10000;
  1028. cp->net_stats[0].collisions += 0x10000;
  1029. }
  1030. if (txmac_stat & MAC_TX_COLL_LATE) {
  1031. cp->net_stats[0].tx_aborted_errors += 0x10000;
  1032. cp->net_stats[0].collisions += 0x10000;
  1033. }
  1034. spin_unlock(&cp->stat_lock[0]);
  1035. /* We do not keep track of MAC_TX_COLL_FIRST and
  1036. * MAC_TX_PEAK_ATTEMPTS events.
  1037. */
  1038. return 0;
  1039. }
  1040. static void cas_load_firmware(struct cas *cp, cas_hp_inst_t *firmware)
  1041. {
  1042. cas_hp_inst_t *inst;
  1043. u32 val;
  1044. int i;
  1045. i = 0;
  1046. while ((inst = firmware) && inst->note) {
  1047. writel(i, cp->regs + REG_HP_INSTR_RAM_ADDR);
  1048. val = CAS_BASE(HP_INSTR_RAM_HI_VAL, inst->val);
  1049. val |= CAS_BASE(HP_INSTR_RAM_HI_MASK, inst->mask);
  1050. writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_HI);
  1051. val = CAS_BASE(HP_INSTR_RAM_MID_OUTARG, inst->outarg >> 10);
  1052. val |= CAS_BASE(HP_INSTR_RAM_MID_OUTOP, inst->outop);
  1053. val |= CAS_BASE(HP_INSTR_RAM_MID_FNEXT, inst->fnext);
  1054. val |= CAS_BASE(HP_INSTR_RAM_MID_FOFF, inst->foff);
  1055. val |= CAS_BASE(HP_INSTR_RAM_MID_SNEXT, inst->snext);
  1056. val |= CAS_BASE(HP_INSTR_RAM_MID_SOFF, inst->soff);
  1057. val |= CAS_BASE(HP_INSTR_RAM_MID_OP, inst->op);
  1058. writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_MID);
  1059. val = CAS_BASE(HP_INSTR_RAM_LOW_OUTMASK, inst->outmask);
  1060. val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTSHIFT, inst->outshift);
  1061. val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTEN, inst->outenab);
  1062. val |= CAS_BASE(HP_INSTR_RAM_LOW_OUTARG, inst->outarg);
  1063. writel(val, cp->regs + REG_HP_INSTR_RAM_DATA_LOW);
  1064. ++firmware;
  1065. ++i;
  1066. }
  1067. }
  1068. static void cas_init_rx_dma(struct cas *cp)
  1069. {
  1070. u64 desc_dma = cp->block_dvma;
  1071. u32 val;
  1072. int i, size;
  1073. /* rx free descriptors */
  1074. val = CAS_BASE(RX_CFG_SWIVEL, RX_SWIVEL_OFF_VAL);
  1075. val |= CAS_BASE(RX_CFG_DESC_RING, RX_DESC_RINGN_INDEX(0));
  1076. val |= CAS_BASE(RX_CFG_COMP_RING, RX_COMP_RINGN_INDEX(0));
  1077. if ((N_RX_DESC_RINGS > 1) &&
  1078. (cp->cas_flags & CAS_FLAG_REG_PLUS)) /* do desc 2 */
  1079. val |= CAS_BASE(RX_CFG_DESC_RING1, RX_DESC_RINGN_INDEX(1));
  1080. writel(val, cp->regs + REG_RX_CFG);
  1081. val = (unsigned long) cp->init_rxds[0] -
  1082. (unsigned long) cp->init_block;
  1083. writel((desc_dma + val) >> 32, cp->regs + REG_RX_DB_HI);
  1084. writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_DB_LOW);
  1085. writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
  1086. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  1087. /* rx desc 2 is for IPSEC packets. however,
  1088. * we don't it that for that purpose.
  1089. */
  1090. val = (unsigned long) cp->init_rxds[1] -
  1091. (unsigned long) cp->init_block;
  1092. writel((desc_dma + val) >> 32, cp->regs + REG_PLUS_RX_DB1_HI);
  1093. writel((desc_dma + val) & 0xffffffff, cp->regs +
  1094. REG_PLUS_RX_DB1_LOW);
  1095. writel(RX_DESC_RINGN_SIZE(1) - 4, cp->regs +
  1096. REG_PLUS_RX_KICK1);
  1097. }
  1098. /* rx completion registers */
  1099. val = (unsigned long) cp->init_rxcs[0] -
  1100. (unsigned long) cp->init_block;
  1101. writel((desc_dma + val) >> 32, cp->regs + REG_RX_CB_HI);
  1102. writel((desc_dma + val) & 0xffffffff, cp->regs + REG_RX_CB_LOW);
  1103. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  1104. /* rx comp 2-4 */
  1105. for (i = 1; i < MAX_RX_COMP_RINGS; i++) {
  1106. val = (unsigned long) cp->init_rxcs[i] -
  1107. (unsigned long) cp->init_block;
  1108. writel((desc_dma + val) >> 32, cp->regs +
  1109. REG_PLUS_RX_CBN_HI(i));
  1110. writel((desc_dma + val) & 0xffffffff, cp->regs +
  1111. REG_PLUS_RX_CBN_LOW(i));
  1112. }
  1113. }
  1114. /* read selective clear regs to prevent spurious interrupts
  1115. * on reset because complete == kick.
  1116. * selective clear set up to prevent interrupts on resets
  1117. */
  1118. readl(cp->regs + REG_INTR_STATUS_ALIAS);
  1119. writel(INTR_RX_DONE | INTR_RX_BUF_UNAVAIL, cp->regs + REG_ALIAS_CLEAR);
  1120. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  1121. for (i = 1; i < N_RX_COMP_RINGS; i++)
  1122. readl(cp->regs + REG_PLUS_INTRN_STATUS_ALIAS(i));
  1123. /* 2 is different from 3 and 4 */
  1124. if (N_RX_COMP_RINGS > 1)
  1125. writel(INTR_RX_DONE_ALT | INTR_RX_BUF_UNAVAIL_1,
  1126. cp->regs + REG_PLUS_ALIASN_CLEAR(1));
  1127. for (i = 2; i < N_RX_COMP_RINGS; i++)
  1128. writel(INTR_RX_DONE_ALT,
  1129. cp->regs + REG_PLUS_ALIASN_CLEAR(i));
  1130. }
  1131. /* set up pause thresholds */
  1132. val = CAS_BASE(RX_PAUSE_THRESH_OFF,
  1133. cp->rx_pause_off / RX_PAUSE_THRESH_QUANTUM);
  1134. val |= CAS_BASE(RX_PAUSE_THRESH_ON,
  1135. cp->rx_pause_on / RX_PAUSE_THRESH_QUANTUM);
  1136. writel(val, cp->regs + REG_RX_PAUSE_THRESH);
  1137. /* zero out dma reassembly buffers */
  1138. for (i = 0; i < 64; i++) {
  1139. writel(i, cp->regs + REG_RX_TABLE_ADDR);
  1140. writel(0x0, cp->regs + REG_RX_TABLE_DATA_LOW);
  1141. writel(0x0, cp->regs + REG_RX_TABLE_DATA_MID);
  1142. writel(0x0, cp->regs + REG_RX_TABLE_DATA_HI);
  1143. }
  1144. /* make sure address register is 0 for normal operation */
  1145. writel(0x0, cp->regs + REG_RX_CTRL_FIFO_ADDR);
  1146. writel(0x0, cp->regs + REG_RX_IPP_FIFO_ADDR);
  1147. /* interrupt mitigation */
  1148. #ifdef USE_RX_BLANK
  1149. val = CAS_BASE(RX_BLANK_INTR_TIME, RX_BLANK_INTR_TIME_VAL);
  1150. val |= CAS_BASE(RX_BLANK_INTR_PKT, RX_BLANK_INTR_PKT_VAL);
  1151. writel(val, cp->regs + REG_RX_BLANK);
  1152. #else
  1153. writel(0x0, cp->regs + REG_RX_BLANK);
  1154. #endif
  1155. /* interrupt generation as a function of low water marks for
  1156. * free desc and completion entries. these are used to trigger
  1157. * housekeeping for rx descs. we don't use the free interrupt
  1158. * as it's not very useful
  1159. */
  1160. /* val = CAS_BASE(RX_AE_THRESH_FREE, RX_AE_FREEN_VAL(0)); */
  1161. val = CAS_BASE(RX_AE_THRESH_COMP, RX_AE_COMP_VAL);
  1162. writel(val, cp->regs + REG_RX_AE_THRESH);
  1163. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  1164. val = CAS_BASE(RX_AE1_THRESH_FREE, RX_AE_FREEN_VAL(1));
  1165. writel(val, cp->regs + REG_PLUS_RX_AE1_THRESH);
  1166. }
  1167. /* Random early detect registers. useful for congestion avoidance.
  1168. * this should be tunable.
  1169. */
  1170. writel(0x0, cp->regs + REG_RX_RED);
  1171. /* receive page sizes. default == 2K (0x800) */
  1172. val = 0;
  1173. if (cp->page_size == 0x1000)
  1174. val = 0x1;
  1175. else if (cp->page_size == 0x2000)
  1176. val = 0x2;
  1177. else if (cp->page_size == 0x4000)
  1178. val = 0x3;
  1179. /* round mtu + offset. constrain to page size. */
  1180. size = cp->dev->mtu + 64;
  1181. if (size > cp->page_size)
  1182. size = cp->page_size;
  1183. if (size <= 0x400)
  1184. i = 0x0;
  1185. else if (size <= 0x800)
  1186. i = 0x1;
  1187. else if (size <= 0x1000)
  1188. i = 0x2;
  1189. else
  1190. i = 0x3;
  1191. cp->mtu_stride = 1 << (i + 10);
  1192. val = CAS_BASE(RX_PAGE_SIZE, val);
  1193. val |= CAS_BASE(RX_PAGE_SIZE_MTU_STRIDE, i);
  1194. val |= CAS_BASE(RX_PAGE_SIZE_MTU_COUNT, cp->page_size >> (i + 10));
  1195. val |= CAS_BASE(RX_PAGE_SIZE_MTU_OFF, 0x1);
  1196. writel(val, cp->regs + REG_RX_PAGE_SIZE);
  1197. /* enable the header parser if desired */
  1198. if (CAS_HP_FIRMWARE == cas_prog_null)
  1199. return;
  1200. val = CAS_BASE(HP_CFG_NUM_CPU, CAS_NCPUS > 63 ? 0 : CAS_NCPUS);
  1201. val |= HP_CFG_PARSE_EN | HP_CFG_SYN_INC_MASK;
  1202. val |= CAS_BASE(HP_CFG_TCP_THRESH, HP_TCP_THRESH_VAL);
  1203. writel(val, cp->regs + REG_HP_CFG);
  1204. }
  1205. static inline void cas_rxc_init(struct cas_rx_comp *rxc)
  1206. {
  1207. memset(rxc, 0, sizeof(*rxc));
  1208. rxc->word4 = cpu_to_le64(RX_COMP4_ZERO);
  1209. }
  1210. /* NOTE: we use the ENC RX DESC ring for spares. the rx_page[0,1]
  1211. * flipping is protected by the fact that the chip will not
  1212. * hand back the same page index while it's being processed.
  1213. */
  1214. static inline cas_page_t *cas_page_spare(struct cas *cp, const int index)
  1215. {
  1216. cas_page_t *page = cp->rx_pages[1][index];
  1217. cas_page_t *new;
  1218. if (page_count(page->buffer) == 1)
  1219. return page;
  1220. new = cas_page_dequeue(cp);
  1221. if (new) {
  1222. spin_lock(&cp->rx_inuse_lock);
  1223. list_add(&page->list, &cp->rx_inuse_list);
  1224. spin_unlock(&cp->rx_inuse_lock);
  1225. }
  1226. return new;
  1227. }
  1228. /* this needs to be changed if we actually use the ENC RX DESC ring */
  1229. static cas_page_t *cas_page_swap(struct cas *cp, const int ring,
  1230. const int index)
  1231. {
  1232. cas_page_t **page0 = cp->rx_pages[0];
  1233. cas_page_t **page1 = cp->rx_pages[1];
  1234. /* swap if buffer is in use */
  1235. if (page_count(page0[index]->buffer) > 1) {
  1236. cas_page_t *new = cas_page_spare(cp, index);
  1237. if (new) {
  1238. page1[index] = page0[index];
  1239. page0[index] = new;
  1240. }
  1241. }
  1242. RX_USED_SET(page0[index], 0);
  1243. return page0[index];
  1244. }
  1245. static void cas_clean_rxds(struct cas *cp)
  1246. {
  1247. /* only clean ring 0 as ring 1 is used for spare buffers */
  1248. struct cas_rx_desc *rxd = cp->init_rxds[0];
  1249. int i, size;
  1250. /* release all rx flows */
  1251. for (i = 0; i < N_RX_FLOWS; i++) {
  1252. struct sk_buff *skb;
  1253. while ((skb = __skb_dequeue(&cp->rx_flows[i]))) {
  1254. cas_skb_release(skb);
  1255. }
  1256. }
  1257. /* initialize descriptors */
  1258. size = RX_DESC_RINGN_SIZE(0);
  1259. for (i = 0; i < size; i++) {
  1260. cas_page_t *page = cas_page_swap(cp, 0, i);
  1261. rxd[i].buffer = cpu_to_le64(page->dma_addr);
  1262. rxd[i].index = cpu_to_le64(CAS_BASE(RX_INDEX_NUM, i) |
  1263. CAS_BASE(RX_INDEX_RING, 0));
  1264. }
  1265. cp->rx_old[0] = RX_DESC_RINGN_SIZE(0) - 4;
  1266. cp->rx_last[0] = 0;
  1267. cp->cas_flags &= ~CAS_FLAG_RXD_POST(0);
  1268. }
  1269. static void cas_clean_rxcs(struct cas *cp)
  1270. {
  1271. int i, j;
  1272. /* take ownership of rx comp descriptors */
  1273. memset(cp->rx_cur, 0, sizeof(*cp->rx_cur)*N_RX_COMP_RINGS);
  1274. memset(cp->rx_new, 0, sizeof(*cp->rx_new)*N_RX_COMP_RINGS);
  1275. for (i = 0; i < N_RX_COMP_RINGS; i++) {
  1276. struct cas_rx_comp *rxc = cp->init_rxcs[i];
  1277. for (j = 0; j < RX_COMP_RINGN_SIZE(i); j++) {
  1278. cas_rxc_init(rxc + j);
  1279. }
  1280. }
  1281. }
  1282. #if 0
  1283. /* When we get a RX fifo overflow, the RX unit is probably hung
  1284. * so we do the following.
  1285. *
  1286. * If any part of the reset goes wrong, we return 1 and that causes the
  1287. * whole chip to be reset.
  1288. */
  1289. static int cas_rxmac_reset(struct cas *cp)
  1290. {
  1291. struct net_device *dev = cp->dev;
  1292. int limit;
  1293. u32 val;
  1294. /* First, reset MAC RX. */
  1295. writel(cp->mac_rx_cfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  1296. for (limit = 0; limit < STOP_TRIES; limit++) {
  1297. if (!(readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN))
  1298. break;
  1299. udelay(10);
  1300. }
  1301. if (limit == STOP_TRIES) {
  1302. printk(KERN_ERR "%s: RX MAC will not disable, resetting whole "
  1303. "chip.\n", dev->name);
  1304. return 1;
  1305. }
  1306. /* Second, disable RX DMA. */
  1307. writel(0, cp->regs + REG_RX_CFG);
  1308. for (limit = 0; limit < STOP_TRIES; limit++) {
  1309. if (!(readl(cp->regs + REG_RX_CFG) & RX_CFG_DMA_EN))
  1310. break;
  1311. udelay(10);
  1312. }
  1313. if (limit == STOP_TRIES) {
  1314. printk(KERN_ERR "%s: RX DMA will not disable, resetting whole "
  1315. "chip.\n", dev->name);
  1316. return 1;
  1317. }
  1318. mdelay(5);
  1319. /* Execute RX reset command. */
  1320. writel(SW_RESET_RX, cp->regs + REG_SW_RESET);
  1321. for (limit = 0; limit < STOP_TRIES; limit++) {
  1322. if (!(readl(cp->regs + REG_SW_RESET) & SW_RESET_RX))
  1323. break;
  1324. udelay(10);
  1325. }
  1326. if (limit == STOP_TRIES) {
  1327. printk(KERN_ERR "%s: RX reset command will not execute, "
  1328. "resetting whole chip.\n", dev->name);
  1329. return 1;
  1330. }
  1331. /* reset driver rx state */
  1332. cas_clean_rxds(cp);
  1333. cas_clean_rxcs(cp);
  1334. /* Now, reprogram the rest of RX unit. */
  1335. cas_init_rx_dma(cp);
  1336. /* re-enable */
  1337. val = readl(cp->regs + REG_RX_CFG);
  1338. writel(val | RX_CFG_DMA_EN, cp->regs + REG_RX_CFG);
  1339. writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
  1340. val = readl(cp->regs + REG_MAC_RX_CFG);
  1341. writel(val | MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  1342. return 0;
  1343. }
  1344. #endif
  1345. static int cas_rxmac_interrupt(struct net_device *dev, struct cas *cp,
  1346. u32 status)
  1347. {
  1348. u32 stat = readl(cp->regs + REG_MAC_RX_STATUS);
  1349. if (!stat)
  1350. return 0;
  1351. if (netif_msg_intr(cp))
  1352. printk(KERN_DEBUG "%s: rxmac interrupt, stat: 0x%x\n",
  1353. cp->dev->name, stat);
  1354. /* these are all rollovers */
  1355. spin_lock(&cp->stat_lock[0]);
  1356. if (stat & MAC_RX_ALIGN_ERR)
  1357. cp->net_stats[0].rx_frame_errors += 0x10000;
  1358. if (stat & MAC_RX_CRC_ERR)
  1359. cp->net_stats[0].rx_crc_errors += 0x10000;
  1360. if (stat & MAC_RX_LEN_ERR)
  1361. cp->net_stats[0].rx_length_errors += 0x10000;
  1362. if (stat & MAC_RX_OVERFLOW) {
  1363. cp->net_stats[0].rx_over_errors++;
  1364. cp->net_stats[0].rx_fifo_errors++;
  1365. }
  1366. /* We do not track MAC_RX_FRAME_COUNT and MAC_RX_VIOL_ERR
  1367. * events.
  1368. */
  1369. spin_unlock(&cp->stat_lock[0]);
  1370. return 0;
  1371. }
  1372. static int cas_mac_interrupt(struct net_device *dev, struct cas *cp,
  1373. u32 status)
  1374. {
  1375. u32 stat = readl(cp->regs + REG_MAC_CTRL_STATUS);
  1376. if (!stat)
  1377. return 0;
  1378. if (netif_msg_intr(cp))
  1379. printk(KERN_DEBUG "%s: mac interrupt, stat: 0x%x\n",
  1380. cp->dev->name, stat);
  1381. /* This interrupt is just for pause frame and pause
  1382. * tracking. It is useful for diagnostics and debug
  1383. * but probably by default we will mask these events.
  1384. */
  1385. if (stat & MAC_CTRL_PAUSE_STATE)
  1386. cp->pause_entered++;
  1387. if (stat & MAC_CTRL_PAUSE_RECEIVED)
  1388. cp->pause_last_time_recvd = (stat >> 16);
  1389. return 0;
  1390. }
  1391. /* Must be invoked under cp->lock. */
  1392. static inline int cas_mdio_link_not_up(struct cas *cp)
  1393. {
  1394. u16 val;
  1395. switch (cp->lstate) {
  1396. case link_force_ret:
  1397. if (netif_msg_link(cp))
  1398. printk(KERN_INFO "%s: Autoneg failed again, keeping"
  1399. " forced mode\n", cp->dev->name);
  1400. cas_phy_write(cp, MII_BMCR, cp->link_fcntl);
  1401. cp->timer_ticks = 5;
  1402. cp->lstate = link_force_ok;
  1403. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  1404. break;
  1405. case link_aneg:
  1406. val = cas_phy_read(cp, MII_BMCR);
  1407. /* Try forced modes. we try things in the following order:
  1408. * 1000 full -> 100 full/half -> 10 half
  1409. */
  1410. val &= ~(BMCR_ANRESTART | BMCR_ANENABLE);
  1411. val |= BMCR_FULLDPLX;
  1412. val |= (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
  1413. CAS_BMCR_SPEED1000 : BMCR_SPEED100;
  1414. cas_phy_write(cp, MII_BMCR, val);
  1415. cp->timer_ticks = 5;
  1416. cp->lstate = link_force_try;
  1417. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  1418. break;
  1419. case link_force_try:
  1420. /* Downgrade from 1000 to 100 to 10 Mbps if necessary. */
  1421. val = cas_phy_read(cp, MII_BMCR);
  1422. cp->timer_ticks = 5;
  1423. if (val & CAS_BMCR_SPEED1000) { /* gigabit */
  1424. val &= ~CAS_BMCR_SPEED1000;
  1425. val |= (BMCR_SPEED100 | BMCR_FULLDPLX);
  1426. cas_phy_write(cp, MII_BMCR, val);
  1427. break;
  1428. }
  1429. if (val & BMCR_SPEED100) {
  1430. if (val & BMCR_FULLDPLX) /* fd failed */
  1431. val &= ~BMCR_FULLDPLX;
  1432. else { /* 100Mbps failed */
  1433. val &= ~BMCR_SPEED100;
  1434. }
  1435. cas_phy_write(cp, MII_BMCR, val);
  1436. break;
  1437. }
  1438. default:
  1439. break;
  1440. }
  1441. return 0;
  1442. }
  1443. /* must be invoked with cp->lock held */
  1444. static int cas_mii_link_check(struct cas *cp, const u16 bmsr)
  1445. {
  1446. int restart;
  1447. if (bmsr & BMSR_LSTATUS) {
  1448. /* Ok, here we got a link. If we had it due to a forced
  1449. * fallback, and we were configured for autoneg, we
  1450. * retry a short autoneg pass. If you know your hub is
  1451. * broken, use ethtool ;)
  1452. */
  1453. if ((cp->lstate == link_force_try) &&
  1454. (cp->link_cntl & BMCR_ANENABLE)) {
  1455. cp->lstate = link_force_ret;
  1456. cp->link_transition = LINK_TRANSITION_LINK_CONFIG;
  1457. cas_mif_poll(cp, 0);
  1458. cp->link_fcntl = cas_phy_read(cp, MII_BMCR);
  1459. cp->timer_ticks = 5;
  1460. if (cp->opened && netif_msg_link(cp))
  1461. printk(KERN_INFO "%s: Got link after fallback, retrying"
  1462. " autoneg once...\n", cp->dev->name);
  1463. cas_phy_write(cp, MII_BMCR,
  1464. cp->link_fcntl | BMCR_ANENABLE |
  1465. BMCR_ANRESTART);
  1466. cas_mif_poll(cp, 1);
  1467. } else if (cp->lstate != link_up) {
  1468. cp->lstate = link_up;
  1469. cp->link_transition = LINK_TRANSITION_LINK_UP;
  1470. if (cp->opened) {
  1471. cas_set_link_modes(cp);
  1472. netif_carrier_on(cp->dev);
  1473. }
  1474. }
  1475. return 0;
  1476. }
  1477. /* link not up. if the link was previously up, we restart the
  1478. * whole process
  1479. */
  1480. restart = 0;
  1481. if (cp->lstate == link_up) {
  1482. cp->lstate = link_down;
  1483. cp->link_transition = LINK_TRANSITION_LINK_DOWN;
  1484. netif_carrier_off(cp->dev);
  1485. if (cp->opened && netif_msg_link(cp))
  1486. printk(KERN_INFO "%s: Link down\n",
  1487. cp->dev->name);
  1488. restart = 1;
  1489. } else if (++cp->timer_ticks > 10)
  1490. cas_mdio_link_not_up(cp);
  1491. return restart;
  1492. }
  1493. static int cas_mif_interrupt(struct net_device *dev, struct cas *cp,
  1494. u32 status)
  1495. {
  1496. u32 stat = readl(cp->regs + REG_MIF_STATUS);
  1497. u16 bmsr;
  1498. /* check for a link change */
  1499. if (CAS_VAL(MIF_STATUS_POLL_STATUS, stat) == 0)
  1500. return 0;
  1501. bmsr = CAS_VAL(MIF_STATUS_POLL_DATA, stat);
  1502. return cas_mii_link_check(cp, bmsr);
  1503. }
  1504. static int cas_pci_interrupt(struct net_device *dev, struct cas *cp,
  1505. u32 status)
  1506. {
  1507. u32 stat = readl(cp->regs + REG_PCI_ERR_STATUS);
  1508. if (!stat)
  1509. return 0;
  1510. printk(KERN_ERR "%s: PCI error [%04x:%04x] ", dev->name, stat,
  1511. readl(cp->regs + REG_BIM_DIAG));
  1512. /* cassini+ has this reserved */
  1513. if ((stat & PCI_ERR_BADACK) &&
  1514. ((cp->cas_flags & CAS_FLAG_REG_PLUS) == 0))
  1515. printk("<No ACK64# during ABS64 cycle> ");
  1516. if (stat & PCI_ERR_DTRTO)
  1517. printk("<Delayed transaction timeout> ");
  1518. if (stat & PCI_ERR_OTHER)
  1519. printk("<other> ");
  1520. if (stat & PCI_ERR_BIM_DMA_WRITE)
  1521. printk("<BIM DMA 0 write req> ");
  1522. if (stat & PCI_ERR_BIM_DMA_READ)
  1523. printk("<BIM DMA 0 read req> ");
  1524. printk("\n");
  1525. if (stat & PCI_ERR_OTHER) {
  1526. u16 cfg;
  1527. /* Interrogate PCI config space for the
  1528. * true cause.
  1529. */
  1530. pci_read_config_word(cp->pdev, PCI_STATUS, &cfg);
  1531. printk(KERN_ERR "%s: Read PCI cfg space status [%04x]\n",
  1532. dev->name, cfg);
  1533. if (cfg & PCI_STATUS_PARITY)
  1534. printk(KERN_ERR "%s: PCI parity error detected.\n",
  1535. dev->name);
  1536. if (cfg & PCI_STATUS_SIG_TARGET_ABORT)
  1537. printk(KERN_ERR "%s: PCI target abort.\n",
  1538. dev->name);
  1539. if (cfg & PCI_STATUS_REC_TARGET_ABORT)
  1540. printk(KERN_ERR "%s: PCI master acks target abort.\n",
  1541. dev->name);
  1542. if (cfg & PCI_STATUS_REC_MASTER_ABORT)
  1543. printk(KERN_ERR "%s: PCI master abort.\n", dev->name);
  1544. if (cfg & PCI_STATUS_SIG_SYSTEM_ERROR)
  1545. printk(KERN_ERR "%s: PCI system error SERR#.\n",
  1546. dev->name);
  1547. if (cfg & PCI_STATUS_DETECTED_PARITY)
  1548. printk(KERN_ERR "%s: PCI parity error.\n",
  1549. dev->name);
  1550. /* Write the error bits back to clear them. */
  1551. cfg &= (PCI_STATUS_PARITY |
  1552. PCI_STATUS_SIG_TARGET_ABORT |
  1553. PCI_STATUS_REC_TARGET_ABORT |
  1554. PCI_STATUS_REC_MASTER_ABORT |
  1555. PCI_STATUS_SIG_SYSTEM_ERROR |
  1556. PCI_STATUS_DETECTED_PARITY);
  1557. pci_write_config_word(cp->pdev, PCI_STATUS, cfg);
  1558. }
  1559. /* For all PCI errors, we should reset the chip. */
  1560. return 1;
  1561. }
  1562. /* All non-normal interrupt conditions get serviced here.
  1563. * Returns non-zero if we should just exit the interrupt
  1564. * handler right now (ie. if we reset the card which invalidates
  1565. * all of the other original irq status bits).
  1566. */
  1567. static int cas_abnormal_irq(struct net_device *dev, struct cas *cp,
  1568. u32 status)
  1569. {
  1570. if (status & INTR_RX_TAG_ERROR) {
  1571. /* corrupt RX tag framing */
  1572. if (netif_msg_rx_err(cp))
  1573. printk(KERN_DEBUG "%s: corrupt rx tag framing\n",
  1574. cp->dev->name);
  1575. spin_lock(&cp->stat_lock[0]);
  1576. cp->net_stats[0].rx_errors++;
  1577. spin_unlock(&cp->stat_lock[0]);
  1578. goto do_reset;
  1579. }
  1580. if (status & INTR_RX_LEN_MISMATCH) {
  1581. /* length mismatch. */
  1582. if (netif_msg_rx_err(cp))
  1583. printk(KERN_DEBUG "%s: length mismatch for rx frame\n",
  1584. cp->dev->name);
  1585. spin_lock(&cp->stat_lock[0]);
  1586. cp->net_stats[0].rx_errors++;
  1587. spin_unlock(&cp->stat_lock[0]);
  1588. goto do_reset;
  1589. }
  1590. if (status & INTR_PCS_STATUS) {
  1591. if (cas_pcs_interrupt(dev, cp, status))
  1592. goto do_reset;
  1593. }
  1594. if (status & INTR_TX_MAC_STATUS) {
  1595. if (cas_txmac_interrupt(dev, cp, status))
  1596. goto do_reset;
  1597. }
  1598. if (status & INTR_RX_MAC_STATUS) {
  1599. if (cas_rxmac_interrupt(dev, cp, status))
  1600. goto do_reset;
  1601. }
  1602. if (status & INTR_MAC_CTRL_STATUS) {
  1603. if (cas_mac_interrupt(dev, cp, status))
  1604. goto do_reset;
  1605. }
  1606. if (status & INTR_MIF_STATUS) {
  1607. if (cas_mif_interrupt(dev, cp, status))
  1608. goto do_reset;
  1609. }
  1610. if (status & INTR_PCI_ERROR_STATUS) {
  1611. if (cas_pci_interrupt(dev, cp, status))
  1612. goto do_reset;
  1613. }
  1614. return 0;
  1615. do_reset:
  1616. #if 1
  1617. atomic_inc(&cp->reset_task_pending);
  1618. atomic_inc(&cp->reset_task_pending_all);
  1619. printk(KERN_ERR "%s:reset called in cas_abnormal_irq [0x%x]\n",
  1620. dev->name, status);
  1621. schedule_work(&cp->reset_task);
  1622. #else
  1623. atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
  1624. printk(KERN_ERR "reset called in cas_abnormal_irq\n");
  1625. schedule_work(&cp->reset_task);
  1626. #endif
  1627. return 1;
  1628. }
  1629. /* NOTE: CAS_TABORT returns 1 or 2 so that it can be used when
  1630. * determining whether to do a netif_stop/wakeup
  1631. */
  1632. #define CAS_TABORT(x) (((x)->cas_flags & CAS_FLAG_TARGET_ABORT) ? 2 : 1)
  1633. #define CAS_ROUND_PAGE(x) (((x) + PAGE_SIZE - 1) & PAGE_MASK)
  1634. static inline int cas_calc_tabort(struct cas *cp, const unsigned long addr,
  1635. const int len)
  1636. {
  1637. unsigned long off = addr + len;
  1638. if (CAS_TABORT(cp) == 1)
  1639. return 0;
  1640. if ((CAS_ROUND_PAGE(off) - off) > TX_TARGET_ABORT_LEN)
  1641. return 0;
  1642. return TX_TARGET_ABORT_LEN;
  1643. }
  1644. static inline void cas_tx_ringN(struct cas *cp, int ring, int limit)
  1645. {
  1646. struct cas_tx_desc *txds;
  1647. struct sk_buff **skbs;
  1648. struct net_device *dev = cp->dev;
  1649. int entry, count;
  1650. spin_lock(&cp->tx_lock[ring]);
  1651. txds = cp->init_txds[ring];
  1652. skbs = cp->tx_skbs[ring];
  1653. entry = cp->tx_old[ring];
  1654. count = TX_BUFF_COUNT(ring, entry, limit);
  1655. while (entry != limit) {
  1656. struct sk_buff *skb = skbs[entry];
  1657. dma_addr_t daddr;
  1658. u32 dlen;
  1659. int frag;
  1660. if (!skb) {
  1661. /* this should never occur */
  1662. entry = TX_DESC_NEXT(ring, entry);
  1663. continue;
  1664. }
  1665. /* however, we might get only a partial skb release. */
  1666. count -= skb_shinfo(skb)->nr_frags +
  1667. + cp->tx_tiny_use[ring][entry].nbufs + 1;
  1668. if (count < 0)
  1669. break;
  1670. if (netif_msg_tx_done(cp))
  1671. printk(KERN_DEBUG "%s: tx[%d] done, slot %d\n",
  1672. cp->dev->name, ring, entry);
  1673. skbs[entry] = NULL;
  1674. cp->tx_tiny_use[ring][entry].nbufs = 0;
  1675. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  1676. struct cas_tx_desc *txd = txds + entry;
  1677. daddr = le64_to_cpu(txd->buffer);
  1678. dlen = CAS_VAL(TX_DESC_BUFLEN,
  1679. le64_to_cpu(txd->control));
  1680. pci_unmap_page(cp->pdev, daddr, dlen,
  1681. PCI_DMA_TODEVICE);
  1682. entry = TX_DESC_NEXT(ring, entry);
  1683. /* tiny buffer may follow */
  1684. if (cp->tx_tiny_use[ring][entry].used) {
  1685. cp->tx_tiny_use[ring][entry].used = 0;
  1686. entry = TX_DESC_NEXT(ring, entry);
  1687. }
  1688. }
  1689. spin_lock(&cp->stat_lock[ring]);
  1690. cp->net_stats[ring].tx_packets++;
  1691. cp->net_stats[ring].tx_bytes += skb->len;
  1692. spin_unlock(&cp->stat_lock[ring]);
  1693. dev_kfree_skb_irq(skb);
  1694. }
  1695. cp->tx_old[ring] = entry;
  1696. /* this is wrong for multiple tx rings. the net device needs
  1697. * multiple queues for this to do the right thing. we wait
  1698. * for 2*packets to be available when using tiny buffers
  1699. */
  1700. if (netif_queue_stopped(dev) &&
  1701. (TX_BUFFS_AVAIL(cp, ring) > CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1)))
  1702. netif_wake_queue(dev);
  1703. spin_unlock(&cp->tx_lock[ring]);
  1704. }
  1705. static void cas_tx(struct net_device *dev, struct cas *cp,
  1706. u32 status)
  1707. {
  1708. int limit, ring;
  1709. #ifdef USE_TX_COMPWB
  1710. u64 compwb = le64_to_cpu(cp->init_block->tx_compwb);
  1711. #endif
  1712. if (netif_msg_intr(cp))
  1713. printk(KERN_DEBUG "%s: tx interrupt, status: 0x%x, %llx\n",
  1714. cp->dev->name, status, (unsigned long long)compwb);
  1715. /* process all the rings */
  1716. for (ring = 0; ring < N_TX_RINGS; ring++) {
  1717. #ifdef USE_TX_COMPWB
  1718. /* use the completion writeback registers */
  1719. limit = (CAS_VAL(TX_COMPWB_MSB, compwb) << 8) |
  1720. CAS_VAL(TX_COMPWB_LSB, compwb);
  1721. compwb = TX_COMPWB_NEXT(compwb);
  1722. #else
  1723. limit = readl(cp->regs + REG_TX_COMPN(ring));
  1724. #endif
  1725. if (cp->tx_old[ring] != limit)
  1726. cas_tx_ringN(cp, ring, limit);
  1727. }
  1728. }
  1729. static int cas_rx_process_pkt(struct cas *cp, struct cas_rx_comp *rxc,
  1730. int entry, const u64 *words,
  1731. struct sk_buff **skbref)
  1732. {
  1733. int dlen, hlen, len, i, alloclen;
  1734. int off, swivel = RX_SWIVEL_OFF_VAL;
  1735. struct cas_page *page;
  1736. struct sk_buff *skb;
  1737. void *addr, *crcaddr;
  1738. __sum16 csum;
  1739. char *p;
  1740. hlen = CAS_VAL(RX_COMP2_HDR_SIZE, words[1]);
  1741. dlen = CAS_VAL(RX_COMP1_DATA_SIZE, words[0]);
  1742. len = hlen + dlen;
  1743. if (RX_COPY_ALWAYS || (words[2] & RX_COMP3_SMALL_PKT))
  1744. alloclen = len;
  1745. else
  1746. alloclen = max(hlen, RX_COPY_MIN);
  1747. skb = dev_alloc_skb(alloclen + swivel + cp->crc_size);
  1748. if (skb == NULL)
  1749. return -1;
  1750. *skbref = skb;
  1751. skb_reserve(skb, swivel);
  1752. p = skb->data;
  1753. addr = crcaddr = NULL;
  1754. if (hlen) { /* always copy header pages */
  1755. i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
  1756. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1757. off = CAS_VAL(RX_COMP2_HDR_OFF, words[1]) * 0x100 +
  1758. swivel;
  1759. i = hlen;
  1760. if (!dlen) /* attach FCS */
  1761. i += cp->crc_size;
  1762. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
  1763. PCI_DMA_FROMDEVICE);
  1764. addr = cas_page_map(page->buffer);
  1765. memcpy(p, addr + off, i);
  1766. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
  1767. PCI_DMA_FROMDEVICE);
  1768. cas_page_unmap(addr);
  1769. RX_USED_ADD(page, 0x100);
  1770. p += hlen;
  1771. swivel = 0;
  1772. }
  1773. if (alloclen < (hlen + dlen)) {
  1774. skb_frag_t *frag = skb_shinfo(skb)->frags;
  1775. /* normal or jumbo packets. we use frags */
  1776. i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
  1777. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1778. off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
  1779. hlen = min(cp->page_size - off, dlen);
  1780. if (hlen < 0) {
  1781. if (netif_msg_rx_err(cp)) {
  1782. printk(KERN_DEBUG "%s: rx page overflow: "
  1783. "%d\n", cp->dev->name, hlen);
  1784. }
  1785. dev_kfree_skb_irq(skb);
  1786. return -1;
  1787. }
  1788. i = hlen;
  1789. if (i == dlen) /* attach FCS */
  1790. i += cp->crc_size;
  1791. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
  1792. PCI_DMA_FROMDEVICE);
  1793. /* make sure we always copy a header */
  1794. swivel = 0;
  1795. if (p == (char *) skb->data) { /* not split */
  1796. addr = cas_page_map(page->buffer);
  1797. memcpy(p, addr + off, RX_COPY_MIN);
  1798. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
  1799. PCI_DMA_FROMDEVICE);
  1800. cas_page_unmap(addr);
  1801. off += RX_COPY_MIN;
  1802. swivel = RX_COPY_MIN;
  1803. RX_USED_ADD(page, cp->mtu_stride);
  1804. } else {
  1805. RX_USED_ADD(page, hlen);
  1806. }
  1807. skb_put(skb, alloclen);
  1808. skb_shinfo(skb)->nr_frags++;
  1809. skb->data_len += hlen - swivel;
  1810. skb->truesize += hlen - swivel;
  1811. skb->len += hlen - swivel;
  1812. get_page(page->buffer);
  1813. frag->page = page->buffer;
  1814. frag->page_offset = off;
  1815. frag->size = hlen - swivel;
  1816. /* any more data? */
  1817. if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
  1818. hlen = dlen;
  1819. off = 0;
  1820. i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
  1821. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1822. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr,
  1823. hlen + cp->crc_size,
  1824. PCI_DMA_FROMDEVICE);
  1825. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr,
  1826. hlen + cp->crc_size,
  1827. PCI_DMA_FROMDEVICE);
  1828. skb_shinfo(skb)->nr_frags++;
  1829. skb->data_len += hlen;
  1830. skb->len += hlen;
  1831. frag++;
  1832. get_page(page->buffer);
  1833. frag->page = page->buffer;
  1834. frag->page_offset = 0;
  1835. frag->size = hlen;
  1836. RX_USED_ADD(page, hlen + cp->crc_size);
  1837. }
  1838. if (cp->crc_size) {
  1839. addr = cas_page_map(page->buffer);
  1840. crcaddr = addr + off + hlen;
  1841. }
  1842. } else {
  1843. /* copying packet */
  1844. if (!dlen)
  1845. goto end_copy_pkt;
  1846. i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
  1847. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1848. off = CAS_VAL(RX_COMP1_DATA_OFF, words[0]) + swivel;
  1849. hlen = min(cp->page_size - off, dlen);
  1850. if (hlen < 0) {
  1851. if (netif_msg_rx_err(cp)) {
  1852. printk(KERN_DEBUG "%s: rx page overflow: "
  1853. "%d\n", cp->dev->name, hlen);
  1854. }
  1855. dev_kfree_skb_irq(skb);
  1856. return -1;
  1857. }
  1858. i = hlen;
  1859. if (i == dlen) /* attach FCS */
  1860. i += cp->crc_size;
  1861. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr + off, i,
  1862. PCI_DMA_FROMDEVICE);
  1863. addr = cas_page_map(page->buffer);
  1864. memcpy(p, addr + off, i);
  1865. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr + off, i,
  1866. PCI_DMA_FROMDEVICE);
  1867. cas_page_unmap(addr);
  1868. if (p == (char *) skb->data) /* not split */
  1869. RX_USED_ADD(page, cp->mtu_stride);
  1870. else
  1871. RX_USED_ADD(page, i);
  1872. /* any more data? */
  1873. if ((words[0] & RX_COMP1_SPLIT_PKT) && ((dlen -= hlen) > 0)) {
  1874. p += hlen;
  1875. i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
  1876. page = cp->rx_pages[CAS_VAL(RX_INDEX_RING, i)][CAS_VAL(RX_INDEX_NUM, i)];
  1877. pci_dma_sync_single_for_cpu(cp->pdev, page->dma_addr,
  1878. dlen + cp->crc_size,
  1879. PCI_DMA_FROMDEVICE);
  1880. addr = cas_page_map(page->buffer);
  1881. memcpy(p, addr, dlen + cp->crc_size);
  1882. pci_dma_sync_single_for_device(cp->pdev, page->dma_addr,
  1883. dlen + cp->crc_size,
  1884. PCI_DMA_FROMDEVICE);
  1885. cas_page_unmap(addr);
  1886. RX_USED_ADD(page, dlen + cp->crc_size);
  1887. }
  1888. end_copy_pkt:
  1889. if (cp->crc_size) {
  1890. addr = NULL;
  1891. crcaddr = skb->data + alloclen;
  1892. }
  1893. skb_put(skb, alloclen);
  1894. }
  1895. csum = (__force __sum16)htons(CAS_VAL(RX_COMP4_TCP_CSUM, words[3]));
  1896. if (cp->crc_size) {
  1897. /* checksum includes FCS. strip it out. */
  1898. csum = csum_fold(csum_partial(crcaddr, cp->crc_size,
  1899. csum_unfold(csum)));
  1900. if (addr)
  1901. cas_page_unmap(addr);
  1902. }
  1903. skb->protocol = eth_type_trans(skb, cp->dev);
  1904. if (skb->protocol == htons(ETH_P_IP)) {
  1905. skb->csum = csum_unfold(~csum);
  1906. skb->ip_summed = CHECKSUM_COMPLETE;
  1907. } else
  1908. skb->ip_summed = CHECKSUM_NONE;
  1909. return len;
  1910. }
  1911. /* we can handle up to 64 rx flows at a time. we do the same thing
  1912. * as nonreassm except that we batch up the buffers.
  1913. * NOTE: we currently just treat each flow as a bunch of packets that
  1914. * we pass up. a better way would be to coalesce the packets
  1915. * into a jumbo packet. to do that, we need to do the following:
  1916. * 1) the first packet will have a clean split between header and
  1917. * data. save both.
  1918. * 2) each time the next flow packet comes in, extend the
  1919. * data length and merge the checksums.
  1920. * 3) on flow release, fix up the header.
  1921. * 4) make sure the higher layer doesn't care.
  1922. * because packets get coalesced, we shouldn't run into fragment count
  1923. * issues.
  1924. */
  1925. static inline void cas_rx_flow_pkt(struct cas *cp, const u64 *words,
  1926. struct sk_buff *skb)
  1927. {
  1928. int flowid = CAS_VAL(RX_COMP3_FLOWID, words[2]) & (N_RX_FLOWS - 1);
  1929. struct sk_buff_head *flow = &cp->rx_flows[flowid];
  1930. /* this is protected at a higher layer, so no need to
  1931. * do any additional locking here. stick the buffer
  1932. * at the end.
  1933. */
  1934. __skb_queue_tail(flow, skb);
  1935. if (words[0] & RX_COMP1_RELEASE_FLOW) {
  1936. while ((skb = __skb_dequeue(flow))) {
  1937. cas_skb_release(skb);
  1938. }
  1939. }
  1940. }
  1941. /* put rx descriptor back on ring. if a buffer is in use by a higher
  1942. * layer, this will need to put in a replacement.
  1943. */
  1944. static void cas_post_page(struct cas *cp, const int ring, const int index)
  1945. {
  1946. cas_page_t *new;
  1947. int entry;
  1948. entry = cp->rx_old[ring];
  1949. new = cas_page_swap(cp, ring, index);
  1950. cp->init_rxds[ring][entry].buffer = cpu_to_le64(new->dma_addr);
  1951. cp->init_rxds[ring][entry].index =
  1952. cpu_to_le64(CAS_BASE(RX_INDEX_NUM, index) |
  1953. CAS_BASE(RX_INDEX_RING, ring));
  1954. entry = RX_DESC_ENTRY(ring, entry + 1);
  1955. cp->rx_old[ring] = entry;
  1956. if (entry % 4)
  1957. return;
  1958. if (ring == 0)
  1959. writel(entry, cp->regs + REG_RX_KICK);
  1960. else if ((N_RX_DESC_RINGS > 1) &&
  1961. (cp->cas_flags & CAS_FLAG_REG_PLUS))
  1962. writel(entry, cp->regs + REG_PLUS_RX_KICK1);
  1963. }
  1964. /* only when things are bad */
  1965. static int cas_post_rxds_ringN(struct cas *cp, int ring, int num)
  1966. {
  1967. unsigned int entry, last, count, released;
  1968. int cluster;
  1969. cas_page_t **page = cp->rx_pages[ring];
  1970. entry = cp->rx_old[ring];
  1971. if (netif_msg_intr(cp))
  1972. printk(KERN_DEBUG "%s: rxd[%d] interrupt, done: %d\n",
  1973. cp->dev->name, ring, entry);
  1974. cluster = -1;
  1975. count = entry & 0x3;
  1976. last = RX_DESC_ENTRY(ring, num ? entry + num - 4: entry - 4);
  1977. released = 0;
  1978. while (entry != last) {
  1979. /* make a new buffer if it's still in use */
  1980. if (page_count(page[entry]->buffer) > 1) {
  1981. cas_page_t *new = cas_page_dequeue(cp);
  1982. if (!new) {
  1983. /* let the timer know that we need to
  1984. * do this again
  1985. */
  1986. cp->cas_flags |= CAS_FLAG_RXD_POST(ring);
  1987. if (!timer_pending(&cp->link_timer))
  1988. mod_timer(&cp->link_timer, jiffies +
  1989. CAS_LINK_FAST_TIMEOUT);
  1990. cp->rx_old[ring] = entry;
  1991. cp->rx_last[ring] = num ? num - released : 0;
  1992. return -ENOMEM;
  1993. }
  1994. spin_lock(&cp->rx_inuse_lock);
  1995. list_add(&page[entry]->list, &cp->rx_inuse_list);
  1996. spin_unlock(&cp->rx_inuse_lock);
  1997. cp->init_rxds[ring][entry].buffer =
  1998. cpu_to_le64(new->dma_addr);
  1999. page[entry] = new;
  2000. }
  2001. if (++count == 4) {
  2002. cluster = entry;
  2003. count = 0;
  2004. }
  2005. released++;
  2006. entry = RX_DESC_ENTRY(ring, entry + 1);
  2007. }
  2008. cp->rx_old[ring] = entry;
  2009. if (cluster < 0)
  2010. return 0;
  2011. if (ring == 0)
  2012. writel(cluster, cp->regs + REG_RX_KICK);
  2013. else if ((N_RX_DESC_RINGS > 1) &&
  2014. (cp->cas_flags & CAS_FLAG_REG_PLUS))
  2015. writel(cluster, cp->regs + REG_PLUS_RX_KICK1);
  2016. return 0;
  2017. }
  2018. /* process a completion ring. packets are set up in three basic ways:
  2019. * small packets: should be copied header + data in single buffer.
  2020. * large packets: header and data in a single buffer.
  2021. * split packets: header in a separate buffer from data.
  2022. * data may be in multiple pages. data may be > 256
  2023. * bytes but in a single page.
  2024. *
  2025. * NOTE: RX page posting is done in this routine as well. while there's
  2026. * the capability of using multiple RX completion rings, it isn't
  2027. * really worthwhile due to the fact that the page posting will
  2028. * force serialization on the single descriptor ring.
  2029. */
  2030. static int cas_rx_ringN(struct cas *cp, int ring, int budget)
  2031. {
  2032. struct cas_rx_comp *rxcs = cp->init_rxcs[ring];
  2033. int entry, drops;
  2034. int npackets = 0;
  2035. if (netif_msg_intr(cp))
  2036. printk(KERN_DEBUG "%s: rx[%d] interrupt, done: %d/%d\n",
  2037. cp->dev->name, ring,
  2038. readl(cp->regs + REG_RX_COMP_HEAD),
  2039. cp->rx_new[ring]);
  2040. entry = cp->rx_new[ring];
  2041. drops = 0;
  2042. while (1) {
  2043. struct cas_rx_comp *rxc = rxcs + entry;
  2044. struct sk_buff *uninitialized_var(skb);
  2045. int type, len;
  2046. u64 words[4];
  2047. int i, dring;
  2048. words[0] = le64_to_cpu(rxc->word1);
  2049. words[1] = le64_to_cpu(rxc->word2);
  2050. words[2] = le64_to_cpu(rxc->word3);
  2051. words[3] = le64_to_cpu(rxc->word4);
  2052. /* don't touch if still owned by hw */
  2053. type = CAS_VAL(RX_COMP1_TYPE, words[0]);
  2054. if (type == 0)
  2055. break;
  2056. /* hw hasn't cleared the zero bit yet */
  2057. if (words[3] & RX_COMP4_ZERO) {
  2058. break;
  2059. }
  2060. /* get info on the packet */
  2061. if (words[3] & (RX_COMP4_LEN_MISMATCH | RX_COMP4_BAD)) {
  2062. spin_lock(&cp->stat_lock[ring]);
  2063. cp->net_stats[ring].rx_errors++;
  2064. if (words[3] & RX_COMP4_LEN_MISMATCH)
  2065. cp->net_stats[ring].rx_length_errors++;
  2066. if (words[3] & RX_COMP4_BAD)
  2067. cp->net_stats[ring].rx_crc_errors++;
  2068. spin_unlock(&cp->stat_lock[ring]);
  2069. /* We'll just return it to Cassini. */
  2070. drop_it:
  2071. spin_lock(&cp->stat_lock[ring]);
  2072. ++cp->net_stats[ring].rx_dropped;
  2073. spin_unlock(&cp->stat_lock[ring]);
  2074. goto next;
  2075. }
  2076. len = cas_rx_process_pkt(cp, rxc, entry, words, &skb);
  2077. if (len < 0) {
  2078. ++drops;
  2079. goto drop_it;
  2080. }
  2081. /* see if it's a flow re-assembly or not. the driver
  2082. * itself handles release back up.
  2083. */
  2084. if (RX_DONT_BATCH || (type == 0x2)) {
  2085. /* non-reassm: these always get released */
  2086. cas_skb_release(skb);
  2087. } else {
  2088. cas_rx_flow_pkt(cp, words, skb);
  2089. }
  2090. spin_lock(&cp->stat_lock[ring]);
  2091. cp->net_stats[ring].rx_packets++;
  2092. cp->net_stats[ring].rx_bytes += len;
  2093. spin_unlock(&cp->stat_lock[ring]);
  2094. next:
  2095. npackets++;
  2096. /* should it be released? */
  2097. if (words[0] & RX_COMP1_RELEASE_HDR) {
  2098. i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
  2099. dring = CAS_VAL(RX_INDEX_RING, i);
  2100. i = CAS_VAL(RX_INDEX_NUM, i);
  2101. cas_post_page(cp, dring, i);
  2102. }
  2103. if (words[0] & RX_COMP1_RELEASE_DATA) {
  2104. i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
  2105. dring = CAS_VAL(RX_INDEX_RING, i);
  2106. i = CAS_VAL(RX_INDEX_NUM, i);
  2107. cas_post_page(cp, dring, i);
  2108. }
  2109. if (words[0] & RX_COMP1_RELEASE_NEXT) {
  2110. i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
  2111. dring = CAS_VAL(RX_INDEX_RING, i);
  2112. i = CAS_VAL(RX_INDEX_NUM, i);
  2113. cas_post_page(cp, dring, i);
  2114. }
  2115. /* skip to the next entry */
  2116. entry = RX_COMP_ENTRY(ring, entry + 1 +
  2117. CAS_VAL(RX_COMP1_SKIP, words[0]));
  2118. #ifdef USE_NAPI
  2119. if (budget && (npackets >= budget))
  2120. break;
  2121. #endif
  2122. }
  2123. cp->rx_new[ring] = entry;
  2124. if (drops)
  2125. printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
  2126. cp->dev->name);
  2127. return npackets;
  2128. }
  2129. /* put completion entries back on the ring */
  2130. static void cas_post_rxcs_ringN(struct net_device *dev,
  2131. struct cas *cp, int ring)
  2132. {
  2133. struct cas_rx_comp *rxc = cp->init_rxcs[ring];
  2134. int last, entry;
  2135. last = cp->rx_cur[ring];
  2136. entry = cp->rx_new[ring];
  2137. if (netif_msg_intr(cp))
  2138. printk(KERN_DEBUG "%s: rxc[%d] interrupt, done: %d/%d\n",
  2139. dev->name, ring, readl(cp->regs + REG_RX_COMP_HEAD),
  2140. entry);
  2141. /* zero and re-mark descriptors */
  2142. while (last != entry) {
  2143. cas_rxc_init(rxc + last);
  2144. last = RX_COMP_ENTRY(ring, last + 1);
  2145. }
  2146. cp->rx_cur[ring] = last;
  2147. if (ring == 0)
  2148. writel(last, cp->regs + REG_RX_COMP_TAIL);
  2149. else if (cp->cas_flags & CAS_FLAG_REG_PLUS)
  2150. writel(last, cp->regs + REG_PLUS_RX_COMPN_TAIL(ring));
  2151. }
  2152. /* cassini can use all four PCI interrupts for the completion ring.
  2153. * rings 3 and 4 are identical
  2154. */
  2155. #if defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
  2156. static inline void cas_handle_irqN(struct net_device *dev,
  2157. struct cas *cp, const u32 status,
  2158. const int ring)
  2159. {
  2160. if (status & (INTR_RX_COMP_FULL_ALT | INTR_RX_COMP_AF_ALT))
  2161. cas_post_rxcs_ringN(dev, cp, ring);
  2162. }
  2163. static irqreturn_t cas_interruptN(int irq, void *dev_id)
  2164. {
  2165. struct net_device *dev = dev_id;
  2166. struct cas *cp = netdev_priv(dev);
  2167. unsigned long flags;
  2168. int ring;
  2169. u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(ring));
  2170. /* check for shared irq */
  2171. if (status == 0)
  2172. return IRQ_NONE;
  2173. ring = (irq == cp->pci_irq_INTC) ? 2 : 3;
  2174. spin_lock_irqsave(&cp->lock, flags);
  2175. if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
  2176. #ifdef USE_NAPI
  2177. cas_mask_intr(cp);
  2178. napi_schedule(&cp->napi);
  2179. #else
  2180. cas_rx_ringN(cp, ring, 0);
  2181. #endif
  2182. status &= ~INTR_RX_DONE_ALT;
  2183. }
  2184. if (status)
  2185. cas_handle_irqN(dev, cp, status, ring);
  2186. spin_unlock_irqrestore(&cp->lock, flags);
  2187. return IRQ_HANDLED;
  2188. }
  2189. #endif
  2190. #ifdef USE_PCI_INTB
  2191. /* everything but rx packets */
  2192. static inline void cas_handle_irq1(struct cas *cp, const u32 status)
  2193. {
  2194. if (status & INTR_RX_BUF_UNAVAIL_1) {
  2195. /* Frame arrived, no free RX buffers available.
  2196. * NOTE: we can get this on a link transition. */
  2197. cas_post_rxds_ringN(cp, 1, 0);
  2198. spin_lock(&cp->stat_lock[1]);
  2199. cp->net_stats[1].rx_dropped++;
  2200. spin_unlock(&cp->stat_lock[1]);
  2201. }
  2202. if (status & INTR_RX_BUF_AE_1)
  2203. cas_post_rxds_ringN(cp, 1, RX_DESC_RINGN_SIZE(1) -
  2204. RX_AE_FREEN_VAL(1));
  2205. if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
  2206. cas_post_rxcs_ringN(cp, 1);
  2207. }
  2208. /* ring 2 handles a few more events than 3 and 4 */
  2209. static irqreturn_t cas_interrupt1(int irq, void *dev_id)
  2210. {
  2211. struct net_device *dev = dev_id;
  2212. struct cas *cp = netdev_priv(dev);
  2213. unsigned long flags;
  2214. u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
  2215. /* check for shared interrupt */
  2216. if (status == 0)
  2217. return IRQ_NONE;
  2218. spin_lock_irqsave(&cp->lock, flags);
  2219. if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
  2220. #ifdef USE_NAPI
  2221. cas_mask_intr(cp);
  2222. napi_schedule(&cp->napi);
  2223. #else
  2224. cas_rx_ringN(cp, 1, 0);
  2225. #endif
  2226. status &= ~INTR_RX_DONE_ALT;
  2227. }
  2228. if (status)
  2229. cas_handle_irq1(cp, status);
  2230. spin_unlock_irqrestore(&cp->lock, flags);
  2231. return IRQ_HANDLED;
  2232. }
  2233. #endif
  2234. static inline void cas_handle_irq(struct net_device *dev,
  2235. struct cas *cp, const u32 status)
  2236. {
  2237. /* housekeeping interrupts */
  2238. if (status & INTR_ERROR_MASK)
  2239. cas_abnormal_irq(dev, cp, status);
  2240. if (status & INTR_RX_BUF_UNAVAIL) {
  2241. /* Frame arrived, no free RX buffers available.
  2242. * NOTE: we can get this on a link transition.
  2243. */
  2244. cas_post_rxds_ringN(cp, 0, 0);
  2245. spin_lock(&cp->stat_lock[0]);
  2246. cp->net_stats[0].rx_dropped++;
  2247. spin_unlock(&cp->stat_lock[0]);
  2248. } else if (status & INTR_RX_BUF_AE) {
  2249. cas_post_rxds_ringN(cp, 0, RX_DESC_RINGN_SIZE(0) -
  2250. RX_AE_FREEN_VAL(0));
  2251. }
  2252. if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
  2253. cas_post_rxcs_ringN(dev, cp, 0);
  2254. }
  2255. static irqreturn_t cas_interrupt(int irq, void *dev_id)
  2256. {
  2257. struct net_device *dev = dev_id;
  2258. struct cas *cp = netdev_priv(dev);
  2259. unsigned long flags;
  2260. u32 status = readl(cp->regs + REG_INTR_STATUS);
  2261. if (status == 0)
  2262. return IRQ_NONE;
  2263. spin_lock_irqsave(&cp->lock, flags);
  2264. if (status & (INTR_TX_ALL | INTR_TX_INTME)) {
  2265. cas_tx(dev, cp, status);
  2266. status &= ~(INTR_TX_ALL | INTR_TX_INTME);
  2267. }
  2268. if (status & INTR_RX_DONE) {
  2269. #ifdef USE_NAPI
  2270. cas_mask_intr(cp);
  2271. napi_schedule(&cp->napi);
  2272. #else
  2273. cas_rx_ringN(cp, 0, 0);
  2274. #endif
  2275. status &= ~INTR_RX_DONE;
  2276. }
  2277. if (status)
  2278. cas_handle_irq(dev, cp, status);
  2279. spin_unlock_irqrestore(&cp->lock, flags);
  2280. return IRQ_HANDLED;
  2281. }
  2282. #ifdef USE_NAPI
  2283. static int cas_poll(struct napi_struct *napi, int budget)
  2284. {
  2285. struct cas *cp = container_of(napi, struct cas, napi);
  2286. struct net_device *dev = cp->dev;
  2287. int i, enable_intr, credits;
  2288. u32 status = readl(cp->regs + REG_INTR_STATUS);
  2289. unsigned long flags;
  2290. spin_lock_irqsave(&cp->lock, flags);
  2291. cas_tx(dev, cp, status);
  2292. spin_unlock_irqrestore(&cp->lock, flags);
  2293. /* NAPI rx packets. we spread the credits across all of the
  2294. * rxc rings
  2295. *
  2296. * to make sure we're fair with the work we loop through each
  2297. * ring N_RX_COMP_RING times with a request of
  2298. * budget / N_RX_COMP_RINGS
  2299. */
  2300. enable_intr = 1;
  2301. credits = 0;
  2302. for (i = 0; i < N_RX_COMP_RINGS; i++) {
  2303. int j;
  2304. for (j = 0; j < N_RX_COMP_RINGS; j++) {
  2305. credits += cas_rx_ringN(cp, j, budget / N_RX_COMP_RINGS);
  2306. if (credits >= budget) {
  2307. enable_intr = 0;
  2308. goto rx_comp;
  2309. }
  2310. }
  2311. }
  2312. rx_comp:
  2313. /* final rx completion */
  2314. spin_lock_irqsave(&cp->lock, flags);
  2315. if (status)
  2316. cas_handle_irq(dev, cp, status);
  2317. #ifdef USE_PCI_INTB
  2318. if (N_RX_COMP_RINGS > 1) {
  2319. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
  2320. if (status)
  2321. cas_handle_irq1(dev, cp, status);
  2322. }
  2323. #endif
  2324. #ifdef USE_PCI_INTC
  2325. if (N_RX_COMP_RINGS > 2) {
  2326. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(2));
  2327. if (status)
  2328. cas_handle_irqN(dev, cp, status, 2);
  2329. }
  2330. #endif
  2331. #ifdef USE_PCI_INTD
  2332. if (N_RX_COMP_RINGS > 3) {
  2333. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(3));
  2334. if (status)
  2335. cas_handle_irqN(dev, cp, status, 3);
  2336. }
  2337. #endif
  2338. spin_unlock_irqrestore(&cp->lock, flags);
  2339. if (enable_intr) {
  2340. napi_complete(napi);
  2341. cas_unmask_intr(cp);
  2342. }
  2343. return credits;
  2344. }
  2345. #endif
  2346. #ifdef CONFIG_NET_POLL_CONTROLLER
  2347. static void cas_netpoll(struct net_device *dev)
  2348. {
  2349. struct cas *cp = netdev_priv(dev);
  2350. cas_disable_irq(cp, 0);
  2351. cas_interrupt(cp->pdev->irq, dev);
  2352. cas_enable_irq(cp, 0);
  2353. #ifdef USE_PCI_INTB
  2354. if (N_RX_COMP_RINGS > 1) {
  2355. /* cas_interrupt1(); */
  2356. }
  2357. #endif
  2358. #ifdef USE_PCI_INTC
  2359. if (N_RX_COMP_RINGS > 2) {
  2360. /* cas_interruptN(); */
  2361. }
  2362. #endif
  2363. #ifdef USE_PCI_INTD
  2364. if (N_RX_COMP_RINGS > 3) {
  2365. /* cas_interruptN(); */
  2366. }
  2367. #endif
  2368. }
  2369. #endif
  2370. static void cas_tx_timeout(struct net_device *dev)
  2371. {
  2372. struct cas *cp = netdev_priv(dev);
  2373. printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
  2374. if (!cp->hw_running) {
  2375. printk("%s: hrm.. hw not running!\n", dev->name);
  2376. return;
  2377. }
  2378. printk(KERN_ERR "%s: MIF_STATE[%08x]\n",
  2379. dev->name, readl(cp->regs + REG_MIF_STATE_MACHINE));
  2380. printk(KERN_ERR "%s: MAC_STATE[%08x]\n",
  2381. dev->name, readl(cp->regs + REG_MAC_STATE_MACHINE));
  2382. printk(KERN_ERR "%s: TX_STATE[%08x:%08x:%08x] "
  2383. "FIFO[%08x:%08x:%08x] SM1[%08x] SM2[%08x]\n",
  2384. dev->name,
  2385. readl(cp->regs + REG_TX_CFG),
  2386. readl(cp->regs + REG_MAC_TX_STATUS),
  2387. readl(cp->regs + REG_MAC_TX_CFG),
  2388. readl(cp->regs + REG_TX_FIFO_PKT_CNT),
  2389. readl(cp->regs + REG_TX_FIFO_WRITE_PTR),
  2390. readl(cp->regs + REG_TX_FIFO_READ_PTR),
  2391. readl(cp->regs + REG_TX_SM_1),
  2392. readl(cp->regs + REG_TX_SM_2));
  2393. printk(KERN_ERR "%s: RX_STATE[%08x:%08x:%08x]\n",
  2394. dev->name,
  2395. readl(cp->regs + REG_RX_CFG),
  2396. readl(cp->regs + REG_MAC_RX_STATUS),
  2397. readl(cp->regs + REG_MAC_RX_CFG));
  2398. printk(KERN_ERR "%s: HP_STATE[%08x:%08x:%08x:%08x]\n",
  2399. dev->name,
  2400. readl(cp->regs + REG_HP_STATE_MACHINE),
  2401. readl(cp->regs + REG_HP_STATUS0),
  2402. readl(cp->regs + REG_HP_STATUS1),
  2403. readl(cp->regs + REG_HP_STATUS2));
  2404. #if 1
  2405. atomic_inc(&cp->reset_task_pending);
  2406. atomic_inc(&cp->reset_task_pending_all);
  2407. schedule_work(&cp->reset_task);
  2408. #else
  2409. atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
  2410. schedule_work(&cp->reset_task);
  2411. #endif
  2412. }
  2413. static inline int cas_intme(int ring, int entry)
  2414. {
  2415. /* Algorithm: IRQ every 1/2 of descriptors. */
  2416. if (!(entry & ((TX_DESC_RINGN_SIZE(ring) >> 1) - 1)))
  2417. return 1;
  2418. return 0;
  2419. }
  2420. static void cas_write_txd(struct cas *cp, int ring, int entry,
  2421. dma_addr_t mapping, int len, u64 ctrl, int last)
  2422. {
  2423. struct cas_tx_desc *txd = cp->init_txds[ring] + entry;
  2424. ctrl |= CAS_BASE(TX_DESC_BUFLEN, len);
  2425. if (cas_intme(ring, entry))
  2426. ctrl |= TX_DESC_INTME;
  2427. if (last)
  2428. ctrl |= TX_DESC_EOF;
  2429. txd->control = cpu_to_le64(ctrl);
  2430. txd->buffer = cpu_to_le64(mapping);
  2431. }
  2432. static inline void *tx_tiny_buf(struct cas *cp, const int ring,
  2433. const int entry)
  2434. {
  2435. return cp->tx_tiny_bufs[ring] + TX_TINY_BUF_LEN*entry;
  2436. }
  2437. static inline dma_addr_t tx_tiny_map(struct cas *cp, const int ring,
  2438. const int entry, const int tentry)
  2439. {
  2440. cp->tx_tiny_use[ring][tentry].nbufs++;
  2441. cp->tx_tiny_use[ring][entry].used = 1;
  2442. return cp->tx_tiny_dvma[ring] + TX_TINY_BUF_LEN*entry;
  2443. }
  2444. static inline int cas_xmit_tx_ringN(struct cas *cp, int ring,
  2445. struct sk_buff *skb)
  2446. {
  2447. struct net_device *dev = cp->dev;
  2448. int entry, nr_frags, frag, tabort, tentry;
  2449. dma_addr_t mapping;
  2450. unsigned long flags;
  2451. u64 ctrl;
  2452. u32 len;
  2453. spin_lock_irqsave(&cp->tx_lock[ring], flags);
  2454. /* This is a hard error, log it. */
  2455. if (TX_BUFFS_AVAIL(cp, ring) <=
  2456. CAS_TABORT(cp)*(skb_shinfo(skb)->nr_frags + 1)) {
  2457. netif_stop_queue(dev);
  2458. spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
  2459. printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
  2460. "queue awake!\n", dev->name);
  2461. return 1;
  2462. }
  2463. ctrl = 0;
  2464. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2465. const u64 csum_start_off = skb_transport_offset(skb);
  2466. const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
  2467. ctrl = TX_DESC_CSUM_EN |
  2468. CAS_BASE(TX_DESC_CSUM_START, csum_start_off) |
  2469. CAS_BASE(TX_DESC_CSUM_STUFF, csum_stuff_off);
  2470. }
  2471. entry = cp->tx_new[ring];
  2472. cp->tx_skbs[ring][entry] = skb;
  2473. nr_frags = skb_shinfo(skb)->nr_frags;
  2474. len = skb_headlen(skb);
  2475. mapping = pci_map_page(cp->pdev, virt_to_page(skb->data),
  2476. offset_in_page(skb->data), len,
  2477. PCI_DMA_TODEVICE);
  2478. tentry = entry;
  2479. tabort = cas_calc_tabort(cp, (unsigned long) skb->data, len);
  2480. if (unlikely(tabort)) {
  2481. /* NOTE: len is always > tabort */
  2482. cas_write_txd(cp, ring, entry, mapping, len - tabort,
  2483. ctrl | TX_DESC_SOF, 0);
  2484. entry = TX_DESC_NEXT(ring, entry);
  2485. skb_copy_from_linear_data_offset(skb, len - tabort,
  2486. tx_tiny_buf(cp, ring, entry), tabort);
  2487. mapping = tx_tiny_map(cp, ring, entry, tentry);
  2488. cas_write_txd(cp, ring, entry, mapping, tabort, ctrl,
  2489. (nr_frags == 0));
  2490. } else {
  2491. cas_write_txd(cp, ring, entry, mapping, len, ctrl |
  2492. TX_DESC_SOF, (nr_frags == 0));
  2493. }
  2494. entry = TX_DESC_NEXT(ring, entry);
  2495. for (frag = 0; frag < nr_frags; frag++) {
  2496. skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
  2497. len = fragp->size;
  2498. mapping = pci_map_page(cp->pdev, fragp->page,
  2499. fragp->page_offset, len,
  2500. PCI_DMA_TODEVICE);
  2501. tabort = cas_calc_tabort(cp, fragp->page_offset, len);
  2502. if (unlikely(tabort)) {
  2503. void *addr;
  2504. /* NOTE: len is always > tabort */
  2505. cas_write_txd(cp, ring, entry, mapping, len - tabort,
  2506. ctrl, 0);
  2507. entry = TX_DESC_NEXT(ring, entry);
  2508. addr = cas_page_map(fragp->page);
  2509. memcpy(tx_tiny_buf(cp, ring, entry),
  2510. addr + fragp->page_offset + len - tabort,
  2511. tabort);
  2512. cas_page_unmap(addr);
  2513. mapping = tx_tiny_map(cp, ring, entry, tentry);
  2514. len = tabort;
  2515. }
  2516. cas_write_txd(cp, ring, entry, mapping, len, ctrl,
  2517. (frag + 1 == nr_frags));
  2518. entry = TX_DESC_NEXT(ring, entry);
  2519. }
  2520. cp->tx_new[ring] = entry;
  2521. if (TX_BUFFS_AVAIL(cp, ring) <= CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1))
  2522. netif_stop_queue(dev);
  2523. if (netif_msg_tx_queued(cp))
  2524. printk(KERN_DEBUG "%s: tx[%d] queued, slot %d, skblen %d, "
  2525. "avail %d\n",
  2526. dev->name, ring, entry, skb->len,
  2527. TX_BUFFS_AVAIL(cp, ring));
  2528. writel(entry, cp->regs + REG_TX_KICKN(ring));
  2529. spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
  2530. return 0;
  2531. }
  2532. static netdev_tx_t cas_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2533. {
  2534. struct cas *cp = netdev_priv(dev);
  2535. /* this is only used as a load-balancing hint, so it doesn't
  2536. * need to be SMP safe
  2537. */
  2538. static int ring;
  2539. if (skb_padto(skb, cp->min_frame_size))
  2540. return NETDEV_TX_OK;
  2541. /* XXX: we need some higher-level QoS hooks to steer packets to
  2542. * individual queues.
  2543. */
  2544. if (cas_xmit_tx_ringN(cp, ring++ & N_TX_RINGS_MASK, skb))
  2545. return NETDEV_TX_BUSY;
  2546. dev->trans_start = jiffies;
  2547. return NETDEV_TX_OK;
  2548. }
  2549. static void cas_init_tx_dma(struct cas *cp)
  2550. {
  2551. u64 desc_dma = cp->block_dvma;
  2552. unsigned long off;
  2553. u32 val;
  2554. int i;
  2555. /* set up tx completion writeback registers. must be 8-byte aligned */
  2556. #ifdef USE_TX_COMPWB
  2557. off = offsetof(struct cas_init_block, tx_compwb);
  2558. writel((desc_dma + off) >> 32, cp->regs + REG_TX_COMPWB_DB_HI);
  2559. writel((desc_dma + off) & 0xffffffff, cp->regs + REG_TX_COMPWB_DB_LOW);
  2560. #endif
  2561. /* enable completion writebacks, enable paced mode,
  2562. * disable read pipe, and disable pre-interrupt compwbs
  2563. */
  2564. val = TX_CFG_COMPWB_Q1 | TX_CFG_COMPWB_Q2 |
  2565. TX_CFG_COMPWB_Q3 | TX_CFG_COMPWB_Q4 |
  2566. TX_CFG_DMA_RDPIPE_DIS | TX_CFG_PACED_MODE |
  2567. TX_CFG_INTR_COMPWB_DIS;
  2568. /* write out tx ring info and tx desc bases */
  2569. for (i = 0; i < MAX_TX_RINGS; i++) {
  2570. off = (unsigned long) cp->init_txds[i] -
  2571. (unsigned long) cp->init_block;
  2572. val |= CAS_TX_RINGN_BASE(i);
  2573. writel((desc_dma + off) >> 32, cp->regs + REG_TX_DBN_HI(i));
  2574. writel((desc_dma + off) & 0xffffffff, cp->regs +
  2575. REG_TX_DBN_LOW(i));
  2576. /* don't zero out the kick register here as the system
  2577. * will wedge
  2578. */
  2579. }
  2580. writel(val, cp->regs + REG_TX_CFG);
  2581. /* program max burst sizes. these numbers should be different
  2582. * if doing QoS.
  2583. */
  2584. #ifdef USE_QOS
  2585. writel(0x800, cp->regs + REG_TX_MAXBURST_0);
  2586. writel(0x1600, cp->regs + REG_TX_MAXBURST_1);
  2587. writel(0x2400, cp->regs + REG_TX_MAXBURST_2);
  2588. writel(0x4800, cp->regs + REG_TX_MAXBURST_3);
  2589. #else
  2590. writel(0x800, cp->regs + REG_TX_MAXBURST_0);
  2591. writel(0x800, cp->regs + REG_TX_MAXBURST_1);
  2592. writel(0x800, cp->regs + REG_TX_MAXBURST_2);
  2593. writel(0x800, cp->regs + REG_TX_MAXBURST_3);
  2594. #endif
  2595. }
  2596. /* Must be invoked under cp->lock. */
  2597. static inline void cas_init_dma(struct cas *cp)
  2598. {
  2599. cas_init_tx_dma(cp);
  2600. cas_init_rx_dma(cp);
  2601. }
  2602. static void cas_process_mc_list(struct cas *cp)
  2603. {
  2604. u16 hash_table[16];
  2605. u32 crc;
  2606. struct dev_mc_list *dmi;
  2607. int i = 1;
  2608. memset(hash_table, 0, sizeof(hash_table));
  2609. netdev_for_each_mc_addr(dmi, cp->dev) {
  2610. if (i <= CAS_MC_EXACT_MATCH_SIZE) {
  2611. /* use the alternate mac address registers for the
  2612. * first 15 multicast addresses
  2613. */
  2614. writel((dmi->dmi_addr[4] << 8) | dmi->dmi_addr[5],
  2615. cp->regs + REG_MAC_ADDRN(i*3 + 0));
  2616. writel((dmi->dmi_addr[2] << 8) | dmi->dmi_addr[3],
  2617. cp->regs + REG_MAC_ADDRN(i*3 + 1));
  2618. writel((dmi->dmi_addr[0] << 8) | dmi->dmi_addr[1],
  2619. cp->regs + REG_MAC_ADDRN(i*3 + 2));
  2620. i++;
  2621. }
  2622. else {
  2623. /* use hw hash table for the next series of
  2624. * multicast addresses
  2625. */
  2626. crc = ether_crc_le(ETH_ALEN, dmi->dmi_addr);
  2627. crc >>= 24;
  2628. hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
  2629. }
  2630. }
  2631. for (i = 0; i < 16; i++)
  2632. writel(hash_table[i], cp->regs + REG_MAC_HASH_TABLEN(i));
  2633. }
  2634. /* Must be invoked under cp->lock. */
  2635. static u32 cas_setup_multicast(struct cas *cp)
  2636. {
  2637. u32 rxcfg = 0;
  2638. int i;
  2639. if (cp->dev->flags & IFF_PROMISC) {
  2640. rxcfg |= MAC_RX_CFG_PROMISC_EN;
  2641. } else if (cp->dev->flags & IFF_ALLMULTI) {
  2642. for (i=0; i < 16; i++)
  2643. writel(0xFFFF, cp->regs + REG_MAC_HASH_TABLEN(i));
  2644. rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
  2645. } else {
  2646. cas_process_mc_list(cp);
  2647. rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
  2648. }
  2649. return rxcfg;
  2650. }
  2651. /* must be invoked under cp->stat_lock[N_TX_RINGS] */
  2652. static void cas_clear_mac_err(struct cas *cp)
  2653. {
  2654. writel(0, cp->regs + REG_MAC_COLL_NORMAL);
  2655. writel(0, cp->regs + REG_MAC_COLL_FIRST);
  2656. writel(0, cp->regs + REG_MAC_COLL_EXCESS);
  2657. writel(0, cp->regs + REG_MAC_COLL_LATE);
  2658. writel(0, cp->regs + REG_MAC_TIMER_DEFER);
  2659. writel(0, cp->regs + REG_MAC_ATTEMPTS_PEAK);
  2660. writel(0, cp->regs + REG_MAC_RECV_FRAME);
  2661. writel(0, cp->regs + REG_MAC_LEN_ERR);
  2662. writel(0, cp->regs + REG_MAC_ALIGN_ERR);
  2663. writel(0, cp->regs + REG_MAC_FCS_ERR);
  2664. writel(0, cp->regs + REG_MAC_RX_CODE_ERR);
  2665. }
  2666. static void cas_mac_reset(struct cas *cp)
  2667. {
  2668. int i;
  2669. /* do both TX and RX reset */
  2670. writel(0x1, cp->regs + REG_MAC_TX_RESET);
  2671. writel(0x1, cp->regs + REG_MAC_RX_RESET);
  2672. /* wait for TX */
  2673. i = STOP_TRIES;
  2674. while (i-- > 0) {
  2675. if (readl(cp->regs + REG_MAC_TX_RESET) == 0)
  2676. break;
  2677. udelay(10);
  2678. }
  2679. /* wait for RX */
  2680. i = STOP_TRIES;
  2681. while (i-- > 0) {
  2682. if (readl(cp->regs + REG_MAC_RX_RESET) == 0)
  2683. break;
  2684. udelay(10);
  2685. }
  2686. if (readl(cp->regs + REG_MAC_TX_RESET) |
  2687. readl(cp->regs + REG_MAC_RX_RESET))
  2688. printk(KERN_ERR "%s: mac tx[%d]/rx[%d] reset failed [%08x]\n",
  2689. cp->dev->name, readl(cp->regs + REG_MAC_TX_RESET),
  2690. readl(cp->regs + REG_MAC_RX_RESET),
  2691. readl(cp->regs + REG_MAC_STATE_MACHINE));
  2692. }
  2693. /* Must be invoked under cp->lock. */
  2694. static void cas_init_mac(struct cas *cp)
  2695. {
  2696. unsigned char *e = &cp->dev->dev_addr[0];
  2697. int i;
  2698. #ifdef CONFIG_CASSINI_MULTICAST_REG_WRITE
  2699. u32 rxcfg;
  2700. #endif
  2701. cas_mac_reset(cp);
  2702. /* setup core arbitration weight register */
  2703. writel(CAWR_RR_DIS, cp->regs + REG_CAWR);
  2704. /* XXX Use pci_dma_burst_advice() */
  2705. #if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
  2706. /* set the infinite burst register for chips that don't have
  2707. * pci issues.
  2708. */
  2709. if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) == 0)
  2710. writel(INF_BURST_EN, cp->regs + REG_INF_BURST);
  2711. #endif
  2712. writel(0x1BF0, cp->regs + REG_MAC_SEND_PAUSE);
  2713. writel(0x00, cp->regs + REG_MAC_IPG0);
  2714. writel(0x08, cp->regs + REG_MAC_IPG1);
  2715. writel(0x04, cp->regs + REG_MAC_IPG2);
  2716. /* change later for 802.3z */
  2717. writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
  2718. /* min frame + FCS */
  2719. writel(ETH_ZLEN + 4, cp->regs + REG_MAC_FRAMESIZE_MIN);
  2720. /* Ethernet payload + header + FCS + optional VLAN tag. NOTE: we
  2721. * specify the maximum frame size to prevent RX tag errors on
  2722. * oversized frames.
  2723. */
  2724. writel(CAS_BASE(MAC_FRAMESIZE_MAX_BURST, 0x2000) |
  2725. CAS_BASE(MAC_FRAMESIZE_MAX_FRAME,
  2726. (CAS_MAX_MTU + ETH_HLEN + 4 + 4)),
  2727. cp->regs + REG_MAC_FRAMESIZE_MAX);
  2728. /* NOTE: crc_size is used as a surrogate for half-duplex.
  2729. * workaround saturn half-duplex issue by increasing preamble
  2730. * size to 65 bytes.
  2731. */
  2732. if ((cp->cas_flags & CAS_FLAG_SATURN) && cp->crc_size)
  2733. writel(0x41, cp->regs + REG_MAC_PA_SIZE);
  2734. else
  2735. writel(0x07, cp->regs + REG_MAC_PA_SIZE);
  2736. writel(0x04, cp->regs + REG_MAC_JAM_SIZE);
  2737. writel(0x10, cp->regs + REG_MAC_ATTEMPT_LIMIT);
  2738. writel(0x8808, cp->regs + REG_MAC_CTRL_TYPE);
  2739. writel((e[5] | (e[4] << 8)) & 0x3ff, cp->regs + REG_MAC_RANDOM_SEED);
  2740. writel(0, cp->regs + REG_MAC_ADDR_FILTER0);
  2741. writel(0, cp->regs + REG_MAC_ADDR_FILTER1);
  2742. writel(0, cp->regs + REG_MAC_ADDR_FILTER2);
  2743. writel(0, cp->regs + REG_MAC_ADDR_FILTER2_1_MASK);
  2744. writel(0, cp->regs + REG_MAC_ADDR_FILTER0_MASK);
  2745. /* setup mac address in perfect filter array */
  2746. for (i = 0; i < 45; i++)
  2747. writel(0x0, cp->regs + REG_MAC_ADDRN(i));
  2748. writel((e[4] << 8) | e[5], cp->regs + REG_MAC_ADDRN(0));
  2749. writel((e[2] << 8) | e[3], cp->regs + REG_MAC_ADDRN(1));
  2750. writel((e[0] << 8) | e[1], cp->regs + REG_MAC_ADDRN(2));
  2751. writel(0x0001, cp->regs + REG_MAC_ADDRN(42));
  2752. writel(0xc200, cp->regs + REG_MAC_ADDRN(43));
  2753. writel(0x0180, cp->regs + REG_MAC_ADDRN(44));
  2754. #ifndef CONFIG_CASSINI_MULTICAST_REG_WRITE
  2755. cp->mac_rx_cfg = cas_setup_multicast(cp);
  2756. #else
  2757. /* WTZ: Do what Adrian did in cas_set_multicast. Doing
  2758. * a writel does not seem to be necessary because Cassini
  2759. * seems to preserve the configuration when we do the reset.
  2760. * If the chip is in trouble, though, it is not clear if we
  2761. * can really count on this behavior. cas_set_multicast uses
  2762. * spin_lock_irqsave, but we are called only in cas_init_hw and
  2763. * cas_init_hw is protected by cas_lock_all, which calls
  2764. * spin_lock_irq (so it doesn't need to save the flags, and
  2765. * we should be OK for the writel, as that is the only
  2766. * difference).
  2767. */
  2768. cp->mac_rx_cfg = rxcfg = cas_setup_multicast(cp);
  2769. writel(rxcfg, cp->regs + REG_MAC_RX_CFG);
  2770. #endif
  2771. spin_lock(&cp->stat_lock[N_TX_RINGS]);
  2772. cas_clear_mac_err(cp);
  2773. spin_unlock(&cp->stat_lock[N_TX_RINGS]);
  2774. /* Setup MAC interrupts. We want to get all of the interesting
  2775. * counter expiration events, but we do not want to hear about
  2776. * normal rx/tx as the DMA engine tells us that.
  2777. */
  2778. writel(MAC_TX_FRAME_XMIT, cp->regs + REG_MAC_TX_MASK);
  2779. writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
  2780. /* Don't enable even the PAUSE interrupts for now, we
  2781. * make no use of those events other than to record them.
  2782. */
  2783. writel(0xffffffff, cp->regs + REG_MAC_CTRL_MASK);
  2784. }
  2785. /* Must be invoked under cp->lock. */
  2786. static void cas_init_pause_thresholds(struct cas *cp)
  2787. {
  2788. /* Calculate pause thresholds. Setting the OFF threshold to the
  2789. * full RX fifo size effectively disables PAUSE generation
  2790. */
  2791. if (cp->rx_fifo_size <= (2 * 1024)) {
  2792. cp->rx_pause_off = cp->rx_pause_on = cp->rx_fifo_size;
  2793. } else {
  2794. int max_frame = (cp->dev->mtu + ETH_HLEN + 4 + 4 + 64) & ~63;
  2795. if (max_frame * 3 > cp->rx_fifo_size) {
  2796. cp->rx_pause_off = 7104;
  2797. cp->rx_pause_on = 960;
  2798. } else {
  2799. int off = (cp->rx_fifo_size - (max_frame * 2));
  2800. int on = off - max_frame;
  2801. cp->rx_pause_off = off;
  2802. cp->rx_pause_on = on;
  2803. }
  2804. }
  2805. }
  2806. static int cas_vpd_match(const void __iomem *p, const char *str)
  2807. {
  2808. int len = strlen(str) + 1;
  2809. int i;
  2810. for (i = 0; i < len; i++) {
  2811. if (readb(p + i) != str[i])
  2812. return 0;
  2813. }
  2814. return 1;
  2815. }
  2816. /* get the mac address by reading the vpd information in the rom.
  2817. * also get the phy type and determine if there's an entropy generator.
  2818. * NOTE: this is a bit convoluted for the following reasons:
  2819. * 1) vpd info has order-dependent mac addresses for multinic cards
  2820. * 2) the only way to determine the nic order is to use the slot
  2821. * number.
  2822. * 3) fiber cards don't have bridges, so their slot numbers don't
  2823. * mean anything.
  2824. * 4) we don't actually know we have a fiber card until after
  2825. * the mac addresses are parsed.
  2826. */
  2827. static int cas_get_vpd_info(struct cas *cp, unsigned char *dev_addr,
  2828. const int offset)
  2829. {
  2830. void __iomem *p = cp->regs + REG_EXPANSION_ROM_RUN_START;
  2831. void __iomem *base, *kstart;
  2832. int i, len;
  2833. int found = 0;
  2834. #define VPD_FOUND_MAC 0x01
  2835. #define VPD_FOUND_PHY 0x02
  2836. int phy_type = CAS_PHY_MII_MDIO0; /* default phy type */
  2837. int mac_off = 0;
  2838. /* give us access to the PROM */
  2839. writel(BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_PAD,
  2840. cp->regs + REG_BIM_LOCAL_DEV_EN);
  2841. /* check for an expansion rom */
  2842. if (readb(p) != 0x55 || readb(p + 1) != 0xaa)
  2843. goto use_random_mac_addr;
  2844. /* search for beginning of vpd */
  2845. base = NULL;
  2846. for (i = 2; i < EXPANSION_ROM_SIZE; i++) {
  2847. /* check for PCIR */
  2848. if ((readb(p + i + 0) == 0x50) &&
  2849. (readb(p + i + 1) == 0x43) &&
  2850. (readb(p + i + 2) == 0x49) &&
  2851. (readb(p + i + 3) == 0x52)) {
  2852. base = p + (readb(p + i + 8) |
  2853. (readb(p + i + 9) << 8));
  2854. break;
  2855. }
  2856. }
  2857. if (!base || (readb(base) != 0x82))
  2858. goto use_random_mac_addr;
  2859. i = (readb(base + 1) | (readb(base + 2) << 8)) + 3;
  2860. while (i < EXPANSION_ROM_SIZE) {
  2861. if (readb(base + i) != 0x90) /* no vpd found */
  2862. goto use_random_mac_addr;
  2863. /* found a vpd field */
  2864. len = readb(base + i + 1) | (readb(base + i + 2) << 8);
  2865. /* extract keywords */
  2866. kstart = base + i + 3;
  2867. p = kstart;
  2868. while ((p - kstart) < len) {
  2869. int klen = readb(p + 2);
  2870. int j;
  2871. char type;
  2872. p += 3;
  2873. /* look for the following things:
  2874. * -- correct length == 29
  2875. * 3 (type) + 2 (size) +
  2876. * 18 (strlen("local-mac-address") + 1) +
  2877. * 6 (mac addr)
  2878. * -- VPD Instance 'I'
  2879. * -- VPD Type Bytes 'B'
  2880. * -- VPD data length == 6
  2881. * -- property string == local-mac-address
  2882. *
  2883. * -- correct length == 24
  2884. * 3 (type) + 2 (size) +
  2885. * 12 (strlen("entropy-dev") + 1) +
  2886. * 7 (strlen("vms110") + 1)
  2887. * -- VPD Instance 'I'
  2888. * -- VPD Type String 'B'
  2889. * -- VPD data length == 7
  2890. * -- property string == entropy-dev
  2891. *
  2892. * -- correct length == 18
  2893. * 3 (type) + 2 (size) +
  2894. * 9 (strlen("phy-type") + 1) +
  2895. * 4 (strlen("pcs") + 1)
  2896. * -- VPD Instance 'I'
  2897. * -- VPD Type String 'S'
  2898. * -- VPD data length == 4
  2899. * -- property string == phy-type
  2900. *
  2901. * -- correct length == 23
  2902. * 3 (type) + 2 (size) +
  2903. * 14 (strlen("phy-interface") + 1) +
  2904. * 4 (strlen("pcs") + 1)
  2905. * -- VPD Instance 'I'
  2906. * -- VPD Type String 'S'
  2907. * -- VPD data length == 4
  2908. * -- property string == phy-interface
  2909. */
  2910. if (readb(p) != 'I')
  2911. goto next;
  2912. /* finally, check string and length */
  2913. type = readb(p + 3);
  2914. if (type == 'B') {
  2915. if ((klen == 29) && readb(p + 4) == 6 &&
  2916. cas_vpd_match(p + 5,
  2917. "local-mac-address")) {
  2918. if (mac_off++ > offset)
  2919. goto next;
  2920. /* set mac address */
  2921. for (j = 0; j < 6; j++)
  2922. dev_addr[j] =
  2923. readb(p + 23 + j);
  2924. goto found_mac;
  2925. }
  2926. }
  2927. if (type != 'S')
  2928. goto next;
  2929. #ifdef USE_ENTROPY_DEV
  2930. if ((klen == 24) &&
  2931. cas_vpd_match(p + 5, "entropy-dev") &&
  2932. cas_vpd_match(p + 17, "vms110")) {
  2933. cp->cas_flags |= CAS_FLAG_ENTROPY_DEV;
  2934. goto next;
  2935. }
  2936. #endif
  2937. if (found & VPD_FOUND_PHY)
  2938. goto next;
  2939. if ((klen == 18) && readb(p + 4) == 4 &&
  2940. cas_vpd_match(p + 5, "phy-type")) {
  2941. if (cas_vpd_match(p + 14, "pcs")) {
  2942. phy_type = CAS_PHY_SERDES;
  2943. goto found_phy;
  2944. }
  2945. }
  2946. if ((klen == 23) && readb(p + 4) == 4 &&
  2947. cas_vpd_match(p + 5, "phy-interface")) {
  2948. if (cas_vpd_match(p + 19, "pcs")) {
  2949. phy_type = CAS_PHY_SERDES;
  2950. goto found_phy;
  2951. }
  2952. }
  2953. found_mac:
  2954. found |= VPD_FOUND_MAC;
  2955. goto next;
  2956. found_phy:
  2957. found |= VPD_FOUND_PHY;
  2958. next:
  2959. p += klen;
  2960. }
  2961. i += len + 3;
  2962. }
  2963. use_random_mac_addr:
  2964. if (found & VPD_FOUND_MAC)
  2965. goto done;
  2966. /* Sun MAC prefix then 3 random bytes. */
  2967. printk(PFX "MAC address not found in ROM VPD\n");
  2968. dev_addr[0] = 0x08;
  2969. dev_addr[1] = 0x00;
  2970. dev_addr[2] = 0x20;
  2971. get_random_bytes(dev_addr + 3, 3);
  2972. done:
  2973. writel(0, cp->regs + REG_BIM_LOCAL_DEV_EN);
  2974. return phy_type;
  2975. }
  2976. /* check pci invariants */
  2977. static void cas_check_pci_invariants(struct cas *cp)
  2978. {
  2979. struct pci_dev *pdev = cp->pdev;
  2980. cp->cas_flags = 0;
  2981. if ((pdev->vendor == PCI_VENDOR_ID_SUN) &&
  2982. (pdev->device == PCI_DEVICE_ID_SUN_CASSINI)) {
  2983. if (pdev->revision >= CAS_ID_REVPLUS)
  2984. cp->cas_flags |= CAS_FLAG_REG_PLUS;
  2985. if (pdev->revision < CAS_ID_REVPLUS02u)
  2986. cp->cas_flags |= CAS_FLAG_TARGET_ABORT;
  2987. /* Original Cassini supports HW CSUM, but it's not
  2988. * enabled by default as it can trigger TX hangs.
  2989. */
  2990. if (pdev->revision < CAS_ID_REV2)
  2991. cp->cas_flags |= CAS_FLAG_NO_HW_CSUM;
  2992. } else {
  2993. /* Only sun has original cassini chips. */
  2994. cp->cas_flags |= CAS_FLAG_REG_PLUS;
  2995. /* We use a flag because the same phy might be externally
  2996. * connected.
  2997. */
  2998. if ((pdev->vendor == PCI_VENDOR_ID_NS) &&
  2999. (pdev->device == PCI_DEVICE_ID_NS_SATURN))
  3000. cp->cas_flags |= CAS_FLAG_SATURN;
  3001. }
  3002. }
  3003. static int cas_check_invariants(struct cas *cp)
  3004. {
  3005. struct pci_dev *pdev = cp->pdev;
  3006. u32 cfg;
  3007. int i;
  3008. /* get page size for rx buffers. */
  3009. cp->page_order = 0;
  3010. #ifdef USE_PAGE_ORDER
  3011. if (PAGE_SHIFT < CAS_JUMBO_PAGE_SHIFT) {
  3012. /* see if we can allocate larger pages */
  3013. struct page *page = alloc_pages(GFP_ATOMIC,
  3014. CAS_JUMBO_PAGE_SHIFT -
  3015. PAGE_SHIFT);
  3016. if (page) {
  3017. __free_pages(page, CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT);
  3018. cp->page_order = CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT;
  3019. } else {
  3020. printk(PFX "MTU limited to %d bytes\n", CAS_MAX_MTU);
  3021. }
  3022. }
  3023. #endif
  3024. cp->page_size = (PAGE_SIZE << cp->page_order);
  3025. /* Fetch the FIFO configurations. */
  3026. cp->tx_fifo_size = readl(cp->regs + REG_TX_FIFO_SIZE) * 64;
  3027. cp->rx_fifo_size = RX_FIFO_SIZE;
  3028. /* finish phy determination. MDIO1 takes precedence over MDIO0 if
  3029. * they're both connected.
  3030. */
  3031. cp->phy_type = cas_get_vpd_info(cp, cp->dev->dev_addr,
  3032. PCI_SLOT(pdev->devfn));
  3033. if (cp->phy_type & CAS_PHY_SERDES) {
  3034. cp->cas_flags |= CAS_FLAG_1000MB_CAP;
  3035. return 0; /* no more checking needed */
  3036. }
  3037. /* MII */
  3038. cfg = readl(cp->regs + REG_MIF_CFG);
  3039. if (cfg & MIF_CFG_MDIO_1) {
  3040. cp->phy_type = CAS_PHY_MII_MDIO1;
  3041. } else if (cfg & MIF_CFG_MDIO_0) {
  3042. cp->phy_type = CAS_PHY_MII_MDIO0;
  3043. }
  3044. cas_mif_poll(cp, 0);
  3045. writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
  3046. for (i = 0; i < 32; i++) {
  3047. u32 phy_id;
  3048. int j;
  3049. for (j = 0; j < 3; j++) {
  3050. cp->phy_addr = i;
  3051. phy_id = cas_phy_read(cp, MII_PHYSID1) << 16;
  3052. phy_id |= cas_phy_read(cp, MII_PHYSID2);
  3053. if (phy_id && (phy_id != 0xFFFFFFFF)) {
  3054. cp->phy_id = phy_id;
  3055. goto done;
  3056. }
  3057. }
  3058. }
  3059. printk(KERN_ERR PFX "MII phy did not respond [%08x]\n",
  3060. readl(cp->regs + REG_MIF_STATE_MACHINE));
  3061. return -1;
  3062. done:
  3063. /* see if we can do gigabit */
  3064. cfg = cas_phy_read(cp, MII_BMSR);
  3065. if ((cfg & CAS_BMSR_1000_EXTEND) &&
  3066. cas_phy_read(cp, CAS_MII_1000_EXTEND))
  3067. cp->cas_flags |= CAS_FLAG_1000MB_CAP;
  3068. return 0;
  3069. }
  3070. /* Must be invoked under cp->lock. */
  3071. static inline void cas_start_dma(struct cas *cp)
  3072. {
  3073. int i;
  3074. u32 val;
  3075. int txfailed = 0;
  3076. /* enable dma */
  3077. val = readl(cp->regs + REG_TX_CFG) | TX_CFG_DMA_EN;
  3078. writel(val, cp->regs + REG_TX_CFG);
  3079. val = readl(cp->regs + REG_RX_CFG) | RX_CFG_DMA_EN;
  3080. writel(val, cp->regs + REG_RX_CFG);
  3081. /* enable the mac */
  3082. val = readl(cp->regs + REG_MAC_TX_CFG) | MAC_TX_CFG_EN;
  3083. writel(val, cp->regs + REG_MAC_TX_CFG);
  3084. val = readl(cp->regs + REG_MAC_RX_CFG) | MAC_RX_CFG_EN;
  3085. writel(val, cp->regs + REG_MAC_RX_CFG);
  3086. i = STOP_TRIES;
  3087. while (i-- > 0) {
  3088. val = readl(cp->regs + REG_MAC_TX_CFG);
  3089. if ((val & MAC_TX_CFG_EN))
  3090. break;
  3091. udelay(10);
  3092. }
  3093. if (i < 0) txfailed = 1;
  3094. i = STOP_TRIES;
  3095. while (i-- > 0) {
  3096. val = readl(cp->regs + REG_MAC_RX_CFG);
  3097. if ((val & MAC_RX_CFG_EN)) {
  3098. if (txfailed) {
  3099. printk(KERN_ERR
  3100. "%s: enabling mac failed [tx:%08x:%08x].\n",
  3101. cp->dev->name,
  3102. readl(cp->regs + REG_MIF_STATE_MACHINE),
  3103. readl(cp->regs + REG_MAC_STATE_MACHINE));
  3104. }
  3105. goto enable_rx_done;
  3106. }
  3107. udelay(10);
  3108. }
  3109. printk(KERN_ERR "%s: enabling mac failed [%s:%08x:%08x].\n",
  3110. cp->dev->name,
  3111. (txfailed? "tx,rx":"rx"),
  3112. readl(cp->regs + REG_MIF_STATE_MACHINE),
  3113. readl(cp->regs + REG_MAC_STATE_MACHINE));
  3114. enable_rx_done:
  3115. cas_unmask_intr(cp); /* enable interrupts */
  3116. writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
  3117. writel(0, cp->regs + REG_RX_COMP_TAIL);
  3118. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  3119. if (N_RX_DESC_RINGS > 1)
  3120. writel(RX_DESC_RINGN_SIZE(1) - 4,
  3121. cp->regs + REG_PLUS_RX_KICK1);
  3122. for (i = 1; i < N_RX_COMP_RINGS; i++)
  3123. writel(0, cp->regs + REG_PLUS_RX_COMPN_TAIL(i));
  3124. }
  3125. }
  3126. /* Must be invoked under cp->lock. */
  3127. static void cas_read_pcs_link_mode(struct cas *cp, int *fd, int *spd,
  3128. int *pause)
  3129. {
  3130. u32 val = readl(cp->regs + REG_PCS_MII_LPA);
  3131. *fd = (val & PCS_MII_LPA_FD) ? 1 : 0;
  3132. *pause = (val & PCS_MII_LPA_SYM_PAUSE) ? 0x01 : 0x00;
  3133. if (val & PCS_MII_LPA_ASYM_PAUSE)
  3134. *pause |= 0x10;
  3135. *spd = 1000;
  3136. }
  3137. /* Must be invoked under cp->lock. */
  3138. static void cas_read_mii_link_mode(struct cas *cp, int *fd, int *spd,
  3139. int *pause)
  3140. {
  3141. u32 val;
  3142. *fd = 0;
  3143. *spd = 10;
  3144. *pause = 0;
  3145. /* use GMII registers */
  3146. val = cas_phy_read(cp, MII_LPA);
  3147. if (val & CAS_LPA_PAUSE)
  3148. *pause = 0x01;
  3149. if (val & CAS_LPA_ASYM_PAUSE)
  3150. *pause |= 0x10;
  3151. if (val & LPA_DUPLEX)
  3152. *fd = 1;
  3153. if (val & LPA_100)
  3154. *spd = 100;
  3155. if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
  3156. val = cas_phy_read(cp, CAS_MII_1000_STATUS);
  3157. if (val & (CAS_LPA_1000FULL | CAS_LPA_1000HALF))
  3158. *spd = 1000;
  3159. if (val & CAS_LPA_1000FULL)
  3160. *fd = 1;
  3161. }
  3162. }
  3163. /* A link-up condition has occurred, initialize and enable the
  3164. * rest of the chip.
  3165. *
  3166. * Must be invoked under cp->lock.
  3167. */
  3168. static void cas_set_link_modes(struct cas *cp)
  3169. {
  3170. u32 val;
  3171. int full_duplex, speed, pause;
  3172. full_duplex = 0;
  3173. speed = 10;
  3174. pause = 0;
  3175. if (CAS_PHY_MII(cp->phy_type)) {
  3176. cas_mif_poll(cp, 0);
  3177. val = cas_phy_read(cp, MII_BMCR);
  3178. if (val & BMCR_ANENABLE) {
  3179. cas_read_mii_link_mode(cp, &full_duplex, &speed,
  3180. &pause);
  3181. } else {
  3182. if (val & BMCR_FULLDPLX)
  3183. full_duplex = 1;
  3184. if (val & BMCR_SPEED100)
  3185. speed = 100;
  3186. else if (val & CAS_BMCR_SPEED1000)
  3187. speed = (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
  3188. 1000 : 100;
  3189. }
  3190. cas_mif_poll(cp, 1);
  3191. } else {
  3192. val = readl(cp->regs + REG_PCS_MII_CTRL);
  3193. cas_read_pcs_link_mode(cp, &full_duplex, &speed, &pause);
  3194. if ((val & PCS_MII_AUTONEG_EN) == 0) {
  3195. if (val & PCS_MII_CTRL_DUPLEX)
  3196. full_duplex = 1;
  3197. }
  3198. }
  3199. if (netif_msg_link(cp))
  3200. printk(KERN_INFO "%s: Link up at %d Mbps, %s-duplex.\n",
  3201. cp->dev->name, speed, (full_duplex ? "full" : "half"));
  3202. val = MAC_XIF_TX_MII_OUTPUT_EN | MAC_XIF_LINK_LED;
  3203. if (CAS_PHY_MII(cp->phy_type)) {
  3204. val |= MAC_XIF_MII_BUFFER_OUTPUT_EN;
  3205. if (!full_duplex)
  3206. val |= MAC_XIF_DISABLE_ECHO;
  3207. }
  3208. if (full_duplex)
  3209. val |= MAC_XIF_FDPLX_LED;
  3210. if (speed == 1000)
  3211. val |= MAC_XIF_GMII_MODE;
  3212. writel(val, cp->regs + REG_MAC_XIF_CFG);
  3213. /* deal with carrier and collision detect. */
  3214. val = MAC_TX_CFG_IPG_EN;
  3215. if (full_duplex) {
  3216. val |= MAC_TX_CFG_IGNORE_CARRIER;
  3217. val |= MAC_TX_CFG_IGNORE_COLL;
  3218. } else {
  3219. #ifndef USE_CSMA_CD_PROTO
  3220. val |= MAC_TX_CFG_NEVER_GIVE_UP_EN;
  3221. val |= MAC_TX_CFG_NEVER_GIVE_UP_LIM;
  3222. #endif
  3223. }
  3224. /* val now set up for REG_MAC_TX_CFG */
  3225. /* If gigabit and half-duplex, enable carrier extension
  3226. * mode. increase slot time to 512 bytes as well.
  3227. * else, disable it and make sure slot time is 64 bytes.
  3228. * also activate checksum bug workaround
  3229. */
  3230. if ((speed == 1000) && !full_duplex) {
  3231. writel(val | MAC_TX_CFG_CARRIER_EXTEND,
  3232. cp->regs + REG_MAC_TX_CFG);
  3233. val = readl(cp->regs + REG_MAC_RX_CFG);
  3234. val &= ~MAC_RX_CFG_STRIP_FCS; /* checksum workaround */
  3235. writel(val | MAC_RX_CFG_CARRIER_EXTEND,
  3236. cp->regs + REG_MAC_RX_CFG);
  3237. writel(0x200, cp->regs + REG_MAC_SLOT_TIME);
  3238. cp->crc_size = 4;
  3239. /* minimum size gigabit frame at half duplex */
  3240. cp->min_frame_size = CAS_1000MB_MIN_FRAME;
  3241. } else {
  3242. writel(val, cp->regs + REG_MAC_TX_CFG);
  3243. /* checksum bug workaround. don't strip FCS when in
  3244. * half-duplex mode
  3245. */
  3246. val = readl(cp->regs + REG_MAC_RX_CFG);
  3247. if (full_duplex) {
  3248. val |= MAC_RX_CFG_STRIP_FCS;
  3249. cp->crc_size = 0;
  3250. cp->min_frame_size = CAS_MIN_MTU;
  3251. } else {
  3252. val &= ~MAC_RX_CFG_STRIP_FCS;
  3253. cp->crc_size = 4;
  3254. cp->min_frame_size = CAS_MIN_FRAME;
  3255. }
  3256. writel(val & ~MAC_RX_CFG_CARRIER_EXTEND,
  3257. cp->regs + REG_MAC_RX_CFG);
  3258. writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
  3259. }
  3260. if (netif_msg_link(cp)) {
  3261. if (pause & 0x01) {
  3262. printk(KERN_INFO "%s: Pause is enabled "
  3263. "(rxfifo: %d off: %d on: %d)\n",
  3264. cp->dev->name,
  3265. cp->rx_fifo_size,
  3266. cp->rx_pause_off,
  3267. cp->rx_pause_on);
  3268. } else if (pause & 0x10) {
  3269. printk(KERN_INFO "%s: TX pause enabled\n",
  3270. cp->dev->name);
  3271. } else {
  3272. printk(KERN_INFO "%s: Pause is disabled\n",
  3273. cp->dev->name);
  3274. }
  3275. }
  3276. val = readl(cp->regs + REG_MAC_CTRL_CFG);
  3277. val &= ~(MAC_CTRL_CFG_SEND_PAUSE_EN | MAC_CTRL_CFG_RECV_PAUSE_EN);
  3278. if (pause) { /* symmetric or asymmetric pause */
  3279. val |= MAC_CTRL_CFG_SEND_PAUSE_EN;
  3280. if (pause & 0x01) { /* symmetric pause */
  3281. val |= MAC_CTRL_CFG_RECV_PAUSE_EN;
  3282. }
  3283. }
  3284. writel(val, cp->regs + REG_MAC_CTRL_CFG);
  3285. cas_start_dma(cp);
  3286. }
  3287. /* Must be invoked under cp->lock. */
  3288. static void cas_init_hw(struct cas *cp, int restart_link)
  3289. {
  3290. if (restart_link)
  3291. cas_phy_init(cp);
  3292. cas_init_pause_thresholds(cp);
  3293. cas_init_mac(cp);
  3294. cas_init_dma(cp);
  3295. if (restart_link) {
  3296. /* Default aneg parameters */
  3297. cp->timer_ticks = 0;
  3298. cas_begin_auto_negotiation(cp, NULL);
  3299. } else if (cp->lstate == link_up) {
  3300. cas_set_link_modes(cp);
  3301. netif_carrier_on(cp->dev);
  3302. }
  3303. }
  3304. /* Must be invoked under cp->lock. on earlier cassini boards,
  3305. * SOFT_0 is tied to PCI reset. we use this to force a pci reset,
  3306. * let it settle out, and then restore pci state.
  3307. */
  3308. static void cas_hard_reset(struct cas *cp)
  3309. {
  3310. writel(BIM_LOCAL_DEV_SOFT_0, cp->regs + REG_BIM_LOCAL_DEV_EN);
  3311. udelay(20);
  3312. pci_restore_state(cp->pdev);
  3313. }
  3314. static void cas_global_reset(struct cas *cp, int blkflag)
  3315. {
  3316. int limit;
  3317. /* issue a global reset. don't use RSTOUT. */
  3318. if (blkflag && !CAS_PHY_MII(cp->phy_type)) {
  3319. /* For PCS, when the blkflag is set, we should set the
  3320. * SW_REST_BLOCK_PCS_SLINK bit to prevent the results of
  3321. * the last autonegotiation from being cleared. We'll
  3322. * need some special handling if the chip is set into a
  3323. * loopback mode.
  3324. */
  3325. writel((SW_RESET_TX | SW_RESET_RX | SW_RESET_BLOCK_PCS_SLINK),
  3326. cp->regs + REG_SW_RESET);
  3327. } else {
  3328. writel(SW_RESET_TX | SW_RESET_RX, cp->regs + REG_SW_RESET);
  3329. }
  3330. /* need to wait at least 3ms before polling register */
  3331. mdelay(3);
  3332. limit = STOP_TRIES;
  3333. while (limit-- > 0) {
  3334. u32 val = readl(cp->regs + REG_SW_RESET);
  3335. if ((val & (SW_RESET_TX | SW_RESET_RX)) == 0)
  3336. goto done;
  3337. udelay(10);
  3338. }
  3339. printk(KERN_ERR "%s: sw reset failed.\n", cp->dev->name);
  3340. done:
  3341. /* enable various BIM interrupts */
  3342. writel(BIM_CFG_DPAR_INTR_ENABLE | BIM_CFG_RMA_INTR_ENABLE |
  3343. BIM_CFG_RTA_INTR_ENABLE, cp->regs + REG_BIM_CFG);
  3344. /* clear out pci error status mask for handled errors.
  3345. * we don't deal with DMA counter overflows as they happen
  3346. * all the time.
  3347. */
  3348. writel(0xFFFFFFFFU & ~(PCI_ERR_BADACK | PCI_ERR_DTRTO |
  3349. PCI_ERR_OTHER | PCI_ERR_BIM_DMA_WRITE |
  3350. PCI_ERR_BIM_DMA_READ), cp->regs +
  3351. REG_PCI_ERR_STATUS_MASK);
  3352. /* set up for MII by default to address mac rx reset timeout
  3353. * issue
  3354. */
  3355. writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
  3356. }
  3357. static void cas_reset(struct cas *cp, int blkflag)
  3358. {
  3359. u32 val;
  3360. cas_mask_intr(cp);
  3361. cas_global_reset(cp, blkflag);
  3362. cas_mac_reset(cp);
  3363. cas_entropy_reset(cp);
  3364. /* disable dma engines. */
  3365. val = readl(cp->regs + REG_TX_CFG);
  3366. val &= ~TX_CFG_DMA_EN;
  3367. writel(val, cp->regs + REG_TX_CFG);
  3368. val = readl(cp->regs + REG_RX_CFG);
  3369. val &= ~RX_CFG_DMA_EN;
  3370. writel(val, cp->regs + REG_RX_CFG);
  3371. /* program header parser */
  3372. if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) ||
  3373. (CAS_HP_ALT_FIRMWARE == cas_prog_null)) {
  3374. cas_load_firmware(cp, CAS_HP_FIRMWARE);
  3375. } else {
  3376. cas_load_firmware(cp, CAS_HP_ALT_FIRMWARE);
  3377. }
  3378. /* clear out error registers */
  3379. spin_lock(&cp->stat_lock[N_TX_RINGS]);
  3380. cas_clear_mac_err(cp);
  3381. spin_unlock(&cp->stat_lock[N_TX_RINGS]);
  3382. }
  3383. /* Shut down the chip, must be called with pm_mutex held. */
  3384. static void cas_shutdown(struct cas *cp)
  3385. {
  3386. unsigned long flags;
  3387. /* Make us not-running to avoid timers respawning */
  3388. cp->hw_running = 0;
  3389. del_timer_sync(&cp->link_timer);
  3390. /* Stop the reset task */
  3391. #if 0
  3392. while (atomic_read(&cp->reset_task_pending_mtu) ||
  3393. atomic_read(&cp->reset_task_pending_spare) ||
  3394. atomic_read(&cp->reset_task_pending_all))
  3395. schedule();
  3396. #else
  3397. while (atomic_read(&cp->reset_task_pending))
  3398. schedule();
  3399. #endif
  3400. /* Actually stop the chip */
  3401. cas_lock_all_save(cp, flags);
  3402. cas_reset(cp, 0);
  3403. if (cp->cas_flags & CAS_FLAG_SATURN)
  3404. cas_phy_powerdown(cp);
  3405. cas_unlock_all_restore(cp, flags);
  3406. }
  3407. static int cas_change_mtu(struct net_device *dev, int new_mtu)
  3408. {
  3409. struct cas *cp = netdev_priv(dev);
  3410. if (new_mtu < CAS_MIN_MTU || new_mtu > CAS_MAX_MTU)
  3411. return -EINVAL;
  3412. dev->mtu = new_mtu;
  3413. if (!netif_running(dev) || !netif_device_present(dev))
  3414. return 0;
  3415. /* let the reset task handle it */
  3416. #if 1
  3417. atomic_inc(&cp->reset_task_pending);
  3418. if ((cp->phy_type & CAS_PHY_SERDES)) {
  3419. atomic_inc(&cp->reset_task_pending_all);
  3420. } else {
  3421. atomic_inc(&cp->reset_task_pending_mtu);
  3422. }
  3423. schedule_work(&cp->reset_task);
  3424. #else
  3425. atomic_set(&cp->reset_task_pending, (cp->phy_type & CAS_PHY_SERDES) ?
  3426. CAS_RESET_ALL : CAS_RESET_MTU);
  3427. printk(KERN_ERR "reset called in cas_change_mtu\n");
  3428. schedule_work(&cp->reset_task);
  3429. #endif
  3430. flush_scheduled_work();
  3431. return 0;
  3432. }
  3433. static void cas_clean_txd(struct cas *cp, int ring)
  3434. {
  3435. struct cas_tx_desc *txd = cp->init_txds[ring];
  3436. struct sk_buff *skb, **skbs = cp->tx_skbs[ring];
  3437. u64 daddr, dlen;
  3438. int i, size;
  3439. size = TX_DESC_RINGN_SIZE(ring);
  3440. for (i = 0; i < size; i++) {
  3441. int frag;
  3442. if (skbs[i] == NULL)
  3443. continue;
  3444. skb = skbs[i];
  3445. skbs[i] = NULL;
  3446. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  3447. int ent = i & (size - 1);
  3448. /* first buffer is never a tiny buffer and so
  3449. * needs to be unmapped.
  3450. */
  3451. daddr = le64_to_cpu(txd[ent].buffer);
  3452. dlen = CAS_VAL(TX_DESC_BUFLEN,
  3453. le64_to_cpu(txd[ent].control));
  3454. pci_unmap_page(cp->pdev, daddr, dlen,
  3455. PCI_DMA_TODEVICE);
  3456. if (frag != skb_shinfo(skb)->nr_frags) {
  3457. i++;
  3458. /* next buffer might by a tiny buffer.
  3459. * skip past it.
  3460. */
  3461. ent = i & (size - 1);
  3462. if (cp->tx_tiny_use[ring][ent].used)
  3463. i++;
  3464. }
  3465. }
  3466. dev_kfree_skb_any(skb);
  3467. }
  3468. /* zero out tiny buf usage */
  3469. memset(cp->tx_tiny_use[ring], 0, size*sizeof(*cp->tx_tiny_use[ring]));
  3470. }
  3471. /* freed on close */
  3472. static inline void cas_free_rx_desc(struct cas *cp, int ring)
  3473. {
  3474. cas_page_t **page = cp->rx_pages[ring];
  3475. int i, size;
  3476. size = RX_DESC_RINGN_SIZE(ring);
  3477. for (i = 0; i < size; i++) {
  3478. if (page[i]) {
  3479. cas_page_free(cp, page[i]);
  3480. page[i] = NULL;
  3481. }
  3482. }
  3483. }
  3484. static void cas_free_rxds(struct cas *cp)
  3485. {
  3486. int i;
  3487. for (i = 0; i < N_RX_DESC_RINGS; i++)
  3488. cas_free_rx_desc(cp, i);
  3489. }
  3490. /* Must be invoked under cp->lock. */
  3491. static void cas_clean_rings(struct cas *cp)
  3492. {
  3493. int i;
  3494. /* need to clean all tx rings */
  3495. memset(cp->tx_old, 0, sizeof(*cp->tx_old)*N_TX_RINGS);
  3496. memset(cp->tx_new, 0, sizeof(*cp->tx_new)*N_TX_RINGS);
  3497. for (i = 0; i < N_TX_RINGS; i++)
  3498. cas_clean_txd(cp, i);
  3499. /* zero out init block */
  3500. memset(cp->init_block, 0, sizeof(struct cas_init_block));
  3501. cas_clean_rxds(cp);
  3502. cas_clean_rxcs(cp);
  3503. }
  3504. /* allocated on open */
  3505. static inline int cas_alloc_rx_desc(struct cas *cp, int ring)
  3506. {
  3507. cas_page_t **page = cp->rx_pages[ring];
  3508. int size, i = 0;
  3509. size = RX_DESC_RINGN_SIZE(ring);
  3510. for (i = 0; i < size; i++) {
  3511. if ((page[i] = cas_page_alloc(cp, GFP_KERNEL)) == NULL)
  3512. return -1;
  3513. }
  3514. return 0;
  3515. }
  3516. static int cas_alloc_rxds(struct cas *cp)
  3517. {
  3518. int i;
  3519. for (i = 0; i < N_RX_DESC_RINGS; i++) {
  3520. if (cas_alloc_rx_desc(cp, i) < 0) {
  3521. cas_free_rxds(cp);
  3522. return -1;
  3523. }
  3524. }
  3525. return 0;
  3526. }
  3527. static void cas_reset_task(struct work_struct *work)
  3528. {
  3529. struct cas *cp = container_of(work, struct cas, reset_task);
  3530. #if 0
  3531. int pending = atomic_read(&cp->reset_task_pending);
  3532. #else
  3533. int pending_all = atomic_read(&cp->reset_task_pending_all);
  3534. int pending_spare = atomic_read(&cp->reset_task_pending_spare);
  3535. int pending_mtu = atomic_read(&cp->reset_task_pending_mtu);
  3536. if (pending_all == 0 && pending_spare == 0 && pending_mtu == 0) {
  3537. /* We can have more tasks scheduled than actually
  3538. * needed.
  3539. */
  3540. atomic_dec(&cp->reset_task_pending);
  3541. return;
  3542. }
  3543. #endif
  3544. /* The link went down, we reset the ring, but keep
  3545. * DMA stopped. Use this function for reset
  3546. * on error as well.
  3547. */
  3548. if (cp->hw_running) {
  3549. unsigned long flags;
  3550. /* Make sure we don't get interrupts or tx packets */
  3551. netif_device_detach(cp->dev);
  3552. cas_lock_all_save(cp, flags);
  3553. if (cp->opened) {
  3554. /* We call cas_spare_recover when we call cas_open.
  3555. * but we do not initialize the lists cas_spare_recover
  3556. * uses until cas_open is called.
  3557. */
  3558. cas_spare_recover(cp, GFP_ATOMIC);
  3559. }
  3560. #if 1
  3561. /* test => only pending_spare set */
  3562. if (!pending_all && !pending_mtu)
  3563. goto done;
  3564. #else
  3565. if (pending == CAS_RESET_SPARE)
  3566. goto done;
  3567. #endif
  3568. /* when pending == CAS_RESET_ALL, the following
  3569. * call to cas_init_hw will restart auto negotiation.
  3570. * Setting the second argument of cas_reset to
  3571. * !(pending == CAS_RESET_ALL) will set this argument
  3572. * to 1 (avoiding reinitializing the PHY for the normal
  3573. * PCS case) when auto negotiation is not restarted.
  3574. */
  3575. #if 1
  3576. cas_reset(cp, !(pending_all > 0));
  3577. if (cp->opened)
  3578. cas_clean_rings(cp);
  3579. cas_init_hw(cp, (pending_all > 0));
  3580. #else
  3581. cas_reset(cp, !(pending == CAS_RESET_ALL));
  3582. if (cp->opened)
  3583. cas_clean_rings(cp);
  3584. cas_init_hw(cp, pending == CAS_RESET_ALL);
  3585. #endif
  3586. done:
  3587. cas_unlock_all_restore(cp, flags);
  3588. netif_device_attach(cp->dev);
  3589. }
  3590. #if 1
  3591. atomic_sub(pending_all, &cp->reset_task_pending_all);
  3592. atomic_sub(pending_spare, &cp->reset_task_pending_spare);
  3593. atomic_sub(pending_mtu, &cp->reset_task_pending_mtu);
  3594. atomic_dec(&cp->reset_task_pending);
  3595. #else
  3596. atomic_set(&cp->reset_task_pending, 0);
  3597. #endif
  3598. }
  3599. static void cas_link_timer(unsigned long data)
  3600. {
  3601. struct cas *cp = (struct cas *) data;
  3602. int mask, pending = 0, reset = 0;
  3603. unsigned long flags;
  3604. if (link_transition_timeout != 0 &&
  3605. cp->link_transition_jiffies_valid &&
  3606. ((jiffies - cp->link_transition_jiffies) >
  3607. (link_transition_timeout))) {
  3608. /* One-second counter so link-down workaround doesn't
  3609. * cause resets to occur so fast as to fool the switch
  3610. * into thinking the link is down.
  3611. */
  3612. cp->link_transition_jiffies_valid = 0;
  3613. }
  3614. if (!cp->hw_running)
  3615. return;
  3616. spin_lock_irqsave(&cp->lock, flags);
  3617. cas_lock_tx(cp);
  3618. cas_entropy_gather(cp);
  3619. /* If the link task is still pending, we just
  3620. * reschedule the link timer
  3621. */
  3622. #if 1
  3623. if (atomic_read(&cp->reset_task_pending_all) ||
  3624. atomic_read(&cp->reset_task_pending_spare) ||
  3625. atomic_read(&cp->reset_task_pending_mtu))
  3626. goto done;
  3627. #else
  3628. if (atomic_read(&cp->reset_task_pending))
  3629. goto done;
  3630. #endif
  3631. /* check for rx cleaning */
  3632. if ((mask = (cp->cas_flags & CAS_FLAG_RXD_POST_MASK))) {
  3633. int i, rmask;
  3634. for (i = 0; i < MAX_RX_DESC_RINGS; i++) {
  3635. rmask = CAS_FLAG_RXD_POST(i);
  3636. if ((mask & rmask) == 0)
  3637. continue;
  3638. /* post_rxds will do a mod_timer */
  3639. if (cas_post_rxds_ringN(cp, i, cp->rx_last[i]) < 0) {
  3640. pending = 1;
  3641. continue;
  3642. }
  3643. cp->cas_flags &= ~rmask;
  3644. }
  3645. }
  3646. if (CAS_PHY_MII(cp->phy_type)) {
  3647. u16 bmsr;
  3648. cas_mif_poll(cp, 0);
  3649. bmsr = cas_phy_read(cp, MII_BMSR);
  3650. /* WTZ: Solaris driver reads this twice, but that
  3651. * may be due to the PCS case and the use of a
  3652. * common implementation. Read it twice here to be
  3653. * safe.
  3654. */
  3655. bmsr = cas_phy_read(cp, MII_BMSR);
  3656. cas_mif_poll(cp, 1);
  3657. readl(cp->regs + REG_MIF_STATUS); /* avoid dups */
  3658. reset = cas_mii_link_check(cp, bmsr);
  3659. } else {
  3660. reset = cas_pcs_link_check(cp);
  3661. }
  3662. if (reset)
  3663. goto done;
  3664. /* check for tx state machine confusion */
  3665. if ((readl(cp->regs + REG_MAC_TX_STATUS) & MAC_TX_FRAME_XMIT) == 0) {
  3666. u32 val = readl(cp->regs + REG_MAC_STATE_MACHINE);
  3667. u32 wptr, rptr;
  3668. int tlm = CAS_VAL(MAC_SM_TLM, val);
  3669. if (((tlm == 0x5) || (tlm == 0x3)) &&
  3670. (CAS_VAL(MAC_SM_ENCAP_SM, val) == 0)) {
  3671. if (netif_msg_tx_err(cp))
  3672. printk(KERN_DEBUG "%s: tx err: "
  3673. "MAC_STATE[%08x]\n",
  3674. cp->dev->name, val);
  3675. reset = 1;
  3676. goto done;
  3677. }
  3678. val = readl(cp->regs + REG_TX_FIFO_PKT_CNT);
  3679. wptr = readl(cp->regs + REG_TX_FIFO_WRITE_PTR);
  3680. rptr = readl(cp->regs + REG_TX_FIFO_READ_PTR);
  3681. if ((val == 0) && (wptr != rptr)) {
  3682. if (netif_msg_tx_err(cp))
  3683. printk(KERN_DEBUG "%s: tx err: "
  3684. "TX_FIFO[%08x:%08x:%08x]\n",
  3685. cp->dev->name, val, wptr, rptr);
  3686. reset = 1;
  3687. }
  3688. if (reset)
  3689. cas_hard_reset(cp);
  3690. }
  3691. done:
  3692. if (reset) {
  3693. #if 1
  3694. atomic_inc(&cp->reset_task_pending);
  3695. atomic_inc(&cp->reset_task_pending_all);
  3696. schedule_work(&cp->reset_task);
  3697. #else
  3698. atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
  3699. printk(KERN_ERR "reset called in cas_link_timer\n");
  3700. schedule_work(&cp->reset_task);
  3701. #endif
  3702. }
  3703. if (!pending)
  3704. mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
  3705. cas_unlock_tx(cp);
  3706. spin_unlock_irqrestore(&cp->lock, flags);
  3707. }
  3708. /* tiny buffers are used to avoid target abort issues with
  3709. * older cassini's
  3710. */
  3711. static void cas_tx_tiny_free(struct cas *cp)
  3712. {
  3713. struct pci_dev *pdev = cp->pdev;
  3714. int i;
  3715. for (i = 0; i < N_TX_RINGS; i++) {
  3716. if (!cp->tx_tiny_bufs[i])
  3717. continue;
  3718. pci_free_consistent(pdev, TX_TINY_BUF_BLOCK,
  3719. cp->tx_tiny_bufs[i],
  3720. cp->tx_tiny_dvma[i]);
  3721. cp->tx_tiny_bufs[i] = NULL;
  3722. }
  3723. }
  3724. static int cas_tx_tiny_alloc(struct cas *cp)
  3725. {
  3726. struct pci_dev *pdev = cp->pdev;
  3727. int i;
  3728. for (i = 0; i < N_TX_RINGS; i++) {
  3729. cp->tx_tiny_bufs[i] =
  3730. pci_alloc_consistent(pdev, TX_TINY_BUF_BLOCK,
  3731. &cp->tx_tiny_dvma[i]);
  3732. if (!cp->tx_tiny_bufs[i]) {
  3733. cas_tx_tiny_free(cp);
  3734. return -1;
  3735. }
  3736. }
  3737. return 0;
  3738. }
  3739. static int cas_open(struct net_device *dev)
  3740. {
  3741. struct cas *cp = netdev_priv(dev);
  3742. int hw_was_up, err;
  3743. unsigned long flags;
  3744. mutex_lock(&cp->pm_mutex);
  3745. hw_was_up = cp->hw_running;
  3746. /* The power-management mutex protects the hw_running
  3747. * etc. state so it is safe to do this bit without cp->lock
  3748. */
  3749. if (!cp->hw_running) {
  3750. /* Reset the chip */
  3751. cas_lock_all_save(cp, flags);
  3752. /* We set the second arg to cas_reset to zero
  3753. * because cas_init_hw below will have its second
  3754. * argument set to non-zero, which will force
  3755. * autonegotiation to start.
  3756. */
  3757. cas_reset(cp, 0);
  3758. cp->hw_running = 1;
  3759. cas_unlock_all_restore(cp, flags);
  3760. }
  3761. err = -ENOMEM;
  3762. if (cas_tx_tiny_alloc(cp) < 0)
  3763. goto err_unlock;
  3764. /* alloc rx descriptors */
  3765. if (cas_alloc_rxds(cp) < 0)
  3766. goto err_tx_tiny;
  3767. /* allocate spares */
  3768. cas_spare_init(cp);
  3769. cas_spare_recover(cp, GFP_KERNEL);
  3770. /* We can now request the interrupt as we know it's masked
  3771. * on the controller. cassini+ has up to 4 interrupts
  3772. * that can be used, but you need to do explicit pci interrupt
  3773. * mapping to expose them
  3774. */
  3775. if (request_irq(cp->pdev->irq, cas_interrupt,
  3776. IRQF_SHARED, dev->name, (void *) dev)) {
  3777. printk(KERN_ERR "%s: failed to request irq !\n",
  3778. cp->dev->name);
  3779. err = -EAGAIN;
  3780. goto err_spare;
  3781. }
  3782. #ifdef USE_NAPI
  3783. napi_enable(&cp->napi);
  3784. #endif
  3785. /* init hw */
  3786. cas_lock_all_save(cp, flags);
  3787. cas_clean_rings(cp);
  3788. cas_init_hw(cp, !hw_was_up);
  3789. cp->opened = 1;
  3790. cas_unlock_all_restore(cp, flags);
  3791. netif_start_queue(dev);
  3792. mutex_unlock(&cp->pm_mutex);
  3793. return 0;
  3794. err_spare:
  3795. cas_spare_free(cp);
  3796. cas_free_rxds(cp);
  3797. err_tx_tiny:
  3798. cas_tx_tiny_free(cp);
  3799. err_unlock:
  3800. mutex_unlock(&cp->pm_mutex);
  3801. return err;
  3802. }
  3803. static int cas_close(struct net_device *dev)
  3804. {
  3805. unsigned long flags;
  3806. struct cas *cp = netdev_priv(dev);
  3807. #ifdef USE_NAPI
  3808. napi_disable(&cp->napi);
  3809. #endif
  3810. /* Make sure we don't get distracted by suspend/resume */
  3811. mutex_lock(&cp->pm_mutex);
  3812. netif_stop_queue(dev);
  3813. /* Stop traffic, mark us closed */
  3814. cas_lock_all_save(cp, flags);
  3815. cp->opened = 0;
  3816. cas_reset(cp, 0);
  3817. cas_phy_init(cp);
  3818. cas_begin_auto_negotiation(cp, NULL);
  3819. cas_clean_rings(cp);
  3820. cas_unlock_all_restore(cp, flags);
  3821. free_irq(cp->pdev->irq, (void *) dev);
  3822. cas_spare_free(cp);
  3823. cas_free_rxds(cp);
  3824. cas_tx_tiny_free(cp);
  3825. mutex_unlock(&cp->pm_mutex);
  3826. return 0;
  3827. }
  3828. static struct {
  3829. const char name[ETH_GSTRING_LEN];
  3830. } ethtool_cassini_statnames[] = {
  3831. {"collisions"},
  3832. {"rx_bytes"},
  3833. {"rx_crc_errors"},
  3834. {"rx_dropped"},
  3835. {"rx_errors"},
  3836. {"rx_fifo_errors"},
  3837. {"rx_frame_errors"},
  3838. {"rx_length_errors"},
  3839. {"rx_over_errors"},
  3840. {"rx_packets"},
  3841. {"tx_aborted_errors"},
  3842. {"tx_bytes"},
  3843. {"tx_dropped"},
  3844. {"tx_errors"},
  3845. {"tx_fifo_errors"},
  3846. {"tx_packets"}
  3847. };
  3848. #define CAS_NUM_STAT_KEYS ARRAY_SIZE(ethtool_cassini_statnames)
  3849. static struct {
  3850. const int offsets; /* neg. values for 2nd arg to cas_read_phy */
  3851. } ethtool_register_table[] = {
  3852. {-MII_BMSR},
  3853. {-MII_BMCR},
  3854. {REG_CAWR},
  3855. {REG_INF_BURST},
  3856. {REG_BIM_CFG},
  3857. {REG_RX_CFG},
  3858. {REG_HP_CFG},
  3859. {REG_MAC_TX_CFG},
  3860. {REG_MAC_RX_CFG},
  3861. {REG_MAC_CTRL_CFG},
  3862. {REG_MAC_XIF_CFG},
  3863. {REG_MIF_CFG},
  3864. {REG_PCS_CFG},
  3865. {REG_SATURN_PCFG},
  3866. {REG_PCS_MII_STATUS},
  3867. {REG_PCS_STATE_MACHINE},
  3868. {REG_MAC_COLL_EXCESS},
  3869. {REG_MAC_COLL_LATE}
  3870. };
  3871. #define CAS_REG_LEN ARRAY_SIZE(ethtool_register_table)
  3872. #define CAS_MAX_REGS (sizeof (u32)*CAS_REG_LEN)
  3873. static void cas_read_regs(struct cas *cp, u8 *ptr, int len)
  3874. {
  3875. u8 *p;
  3876. int i;
  3877. unsigned long flags;
  3878. spin_lock_irqsave(&cp->lock, flags);
  3879. for (i = 0, p = ptr; i < len ; i ++, p += sizeof(u32)) {
  3880. u16 hval;
  3881. u32 val;
  3882. if (ethtool_register_table[i].offsets < 0) {
  3883. hval = cas_phy_read(cp,
  3884. -ethtool_register_table[i].offsets);
  3885. val = hval;
  3886. } else {
  3887. val= readl(cp->regs+ethtool_register_table[i].offsets);
  3888. }
  3889. memcpy(p, (u8 *)&val, sizeof(u32));
  3890. }
  3891. spin_unlock_irqrestore(&cp->lock, flags);
  3892. }
  3893. static struct net_device_stats *cas_get_stats(struct net_device *dev)
  3894. {
  3895. struct cas *cp = netdev_priv(dev);
  3896. struct net_device_stats *stats = cp->net_stats;
  3897. unsigned long flags;
  3898. int i;
  3899. unsigned long tmp;
  3900. /* we collate all of the stats into net_stats[N_TX_RING] */
  3901. if (!cp->hw_running)
  3902. return stats + N_TX_RINGS;
  3903. /* collect outstanding stats */
  3904. /* WTZ: the Cassini spec gives these as 16 bit counters but
  3905. * stored in 32-bit words. Added a mask of 0xffff to be safe,
  3906. * in case the chip somehow puts any garbage in the other bits.
  3907. * Also, counter usage didn't seem to mach what Adrian did
  3908. * in the parts of the code that set these quantities. Made
  3909. * that consistent.
  3910. */
  3911. spin_lock_irqsave(&cp->stat_lock[N_TX_RINGS], flags);
  3912. stats[N_TX_RINGS].rx_crc_errors +=
  3913. readl(cp->regs + REG_MAC_FCS_ERR) & 0xffff;
  3914. stats[N_TX_RINGS].rx_frame_errors +=
  3915. readl(cp->regs + REG_MAC_ALIGN_ERR) &0xffff;
  3916. stats[N_TX_RINGS].rx_length_errors +=
  3917. readl(cp->regs + REG_MAC_LEN_ERR) & 0xffff;
  3918. #if 1
  3919. tmp = (readl(cp->regs + REG_MAC_COLL_EXCESS) & 0xffff) +
  3920. (readl(cp->regs + REG_MAC_COLL_LATE) & 0xffff);
  3921. stats[N_TX_RINGS].tx_aborted_errors += tmp;
  3922. stats[N_TX_RINGS].collisions +=
  3923. tmp + (readl(cp->regs + REG_MAC_COLL_NORMAL) & 0xffff);
  3924. #else
  3925. stats[N_TX_RINGS].tx_aborted_errors +=
  3926. readl(cp->regs + REG_MAC_COLL_EXCESS);
  3927. stats[N_TX_RINGS].collisions += readl(cp->regs + REG_MAC_COLL_EXCESS) +
  3928. readl(cp->regs + REG_MAC_COLL_LATE);
  3929. #endif
  3930. cas_clear_mac_err(cp);
  3931. /* saved bits that are unique to ring 0 */
  3932. spin_lock(&cp->stat_lock[0]);
  3933. stats[N_TX_RINGS].collisions += stats[0].collisions;
  3934. stats[N_TX_RINGS].rx_over_errors += stats[0].rx_over_errors;
  3935. stats[N_TX_RINGS].rx_frame_errors += stats[0].rx_frame_errors;
  3936. stats[N_TX_RINGS].rx_fifo_errors += stats[0].rx_fifo_errors;
  3937. stats[N_TX_RINGS].tx_aborted_errors += stats[0].tx_aborted_errors;
  3938. stats[N_TX_RINGS].tx_fifo_errors += stats[0].tx_fifo_errors;
  3939. spin_unlock(&cp->stat_lock[0]);
  3940. for (i = 0; i < N_TX_RINGS; i++) {
  3941. spin_lock(&cp->stat_lock[i]);
  3942. stats[N_TX_RINGS].rx_length_errors +=
  3943. stats[i].rx_length_errors;
  3944. stats[N_TX_RINGS].rx_crc_errors += stats[i].rx_crc_errors;
  3945. stats[N_TX_RINGS].rx_packets += stats[i].rx_packets;
  3946. stats[N_TX_RINGS].tx_packets += stats[i].tx_packets;
  3947. stats[N_TX_RINGS].rx_bytes += stats[i].rx_bytes;
  3948. stats[N_TX_RINGS].tx_bytes += stats[i].tx_bytes;
  3949. stats[N_TX_RINGS].rx_errors += stats[i].rx_errors;
  3950. stats[N_TX_RINGS].tx_errors += stats[i].tx_errors;
  3951. stats[N_TX_RINGS].rx_dropped += stats[i].rx_dropped;
  3952. stats[N_TX_RINGS].tx_dropped += stats[i].tx_dropped;
  3953. memset(stats + i, 0, sizeof(struct net_device_stats));
  3954. spin_unlock(&cp->stat_lock[i]);
  3955. }
  3956. spin_unlock_irqrestore(&cp->stat_lock[N_TX_RINGS], flags);
  3957. return stats + N_TX_RINGS;
  3958. }
  3959. static void cas_set_multicast(struct net_device *dev)
  3960. {
  3961. struct cas *cp = netdev_priv(dev);
  3962. u32 rxcfg, rxcfg_new;
  3963. unsigned long flags;
  3964. int limit = STOP_TRIES;
  3965. if (!cp->hw_running)
  3966. return;
  3967. spin_lock_irqsave(&cp->lock, flags);
  3968. rxcfg = readl(cp->regs + REG_MAC_RX_CFG);
  3969. /* disable RX MAC and wait for completion */
  3970. writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  3971. while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN) {
  3972. if (!limit--)
  3973. break;
  3974. udelay(10);
  3975. }
  3976. /* disable hash filter and wait for completion */
  3977. limit = STOP_TRIES;
  3978. rxcfg &= ~(MAC_RX_CFG_PROMISC_EN | MAC_RX_CFG_HASH_FILTER_EN);
  3979. writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  3980. while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_HASH_FILTER_EN) {
  3981. if (!limit--)
  3982. break;
  3983. udelay(10);
  3984. }
  3985. /* program hash filters */
  3986. cp->mac_rx_cfg = rxcfg_new = cas_setup_multicast(cp);
  3987. rxcfg |= rxcfg_new;
  3988. writel(rxcfg, cp->regs + REG_MAC_RX_CFG);
  3989. spin_unlock_irqrestore(&cp->lock, flags);
  3990. }
  3991. static void cas_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  3992. {
  3993. struct cas *cp = netdev_priv(dev);
  3994. strncpy(info->driver, DRV_MODULE_NAME, ETHTOOL_BUSINFO_LEN);
  3995. strncpy(info->version, DRV_MODULE_VERSION, ETHTOOL_BUSINFO_LEN);
  3996. info->fw_version[0] = '\0';
  3997. strncpy(info->bus_info, pci_name(cp->pdev), ETHTOOL_BUSINFO_LEN);
  3998. info->regdump_len = cp->casreg_len < CAS_MAX_REGS ?
  3999. cp->casreg_len : CAS_MAX_REGS;
  4000. info->n_stats = CAS_NUM_STAT_KEYS;
  4001. }
  4002. static int cas_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  4003. {
  4004. struct cas *cp = netdev_priv(dev);
  4005. u16 bmcr;
  4006. int full_duplex, speed, pause;
  4007. unsigned long flags;
  4008. enum link_state linkstate = link_up;
  4009. cmd->advertising = 0;
  4010. cmd->supported = SUPPORTED_Autoneg;
  4011. if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
  4012. cmd->supported |= SUPPORTED_1000baseT_Full;
  4013. cmd->advertising |= ADVERTISED_1000baseT_Full;
  4014. }
  4015. /* Record PHY settings if HW is on. */
  4016. spin_lock_irqsave(&cp->lock, flags);
  4017. bmcr = 0;
  4018. linkstate = cp->lstate;
  4019. if (CAS_PHY_MII(cp->phy_type)) {
  4020. cmd->port = PORT_MII;
  4021. cmd->transceiver = (cp->cas_flags & CAS_FLAG_SATURN) ?
  4022. XCVR_INTERNAL : XCVR_EXTERNAL;
  4023. cmd->phy_address = cp->phy_addr;
  4024. cmd->advertising |= ADVERTISED_TP | ADVERTISED_MII |
  4025. ADVERTISED_10baseT_Half |
  4026. ADVERTISED_10baseT_Full |
  4027. ADVERTISED_100baseT_Half |
  4028. ADVERTISED_100baseT_Full;
  4029. cmd->supported |=
  4030. (SUPPORTED_10baseT_Half |
  4031. SUPPORTED_10baseT_Full |
  4032. SUPPORTED_100baseT_Half |
  4033. SUPPORTED_100baseT_Full |
  4034. SUPPORTED_TP | SUPPORTED_MII);
  4035. if (cp->hw_running) {
  4036. cas_mif_poll(cp, 0);
  4037. bmcr = cas_phy_read(cp, MII_BMCR);
  4038. cas_read_mii_link_mode(cp, &full_duplex,
  4039. &speed, &pause);
  4040. cas_mif_poll(cp, 1);
  4041. }
  4042. } else {
  4043. cmd->port = PORT_FIBRE;
  4044. cmd->transceiver = XCVR_INTERNAL;
  4045. cmd->phy_address = 0;
  4046. cmd->supported |= SUPPORTED_FIBRE;
  4047. cmd->advertising |= ADVERTISED_FIBRE;
  4048. if (cp->hw_running) {
  4049. /* pcs uses the same bits as mii */
  4050. bmcr = readl(cp->regs + REG_PCS_MII_CTRL);
  4051. cas_read_pcs_link_mode(cp, &full_duplex,
  4052. &speed, &pause);
  4053. }
  4054. }
  4055. spin_unlock_irqrestore(&cp->lock, flags);
  4056. if (bmcr & BMCR_ANENABLE) {
  4057. cmd->advertising |= ADVERTISED_Autoneg;
  4058. cmd->autoneg = AUTONEG_ENABLE;
  4059. cmd->speed = ((speed == 10) ?
  4060. SPEED_10 :
  4061. ((speed == 1000) ?
  4062. SPEED_1000 : SPEED_100));
  4063. cmd->duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  4064. } else {
  4065. cmd->autoneg = AUTONEG_DISABLE;
  4066. cmd->speed =
  4067. (bmcr & CAS_BMCR_SPEED1000) ?
  4068. SPEED_1000 :
  4069. ((bmcr & BMCR_SPEED100) ? SPEED_100:
  4070. SPEED_10);
  4071. cmd->duplex =
  4072. (bmcr & BMCR_FULLDPLX) ?
  4073. DUPLEX_FULL : DUPLEX_HALF;
  4074. }
  4075. if (linkstate != link_up) {
  4076. /* Force these to "unknown" if the link is not up and
  4077. * autonogotiation in enabled. We can set the link
  4078. * speed to 0, but not cmd->duplex,
  4079. * because its legal values are 0 and 1. Ethtool will
  4080. * print the value reported in parentheses after the
  4081. * word "Unknown" for unrecognized values.
  4082. *
  4083. * If in forced mode, we report the speed and duplex
  4084. * settings that we configured.
  4085. */
  4086. if (cp->link_cntl & BMCR_ANENABLE) {
  4087. cmd->speed = 0;
  4088. cmd->duplex = 0xff;
  4089. } else {
  4090. cmd->speed = SPEED_10;
  4091. if (cp->link_cntl & BMCR_SPEED100) {
  4092. cmd->speed = SPEED_100;
  4093. } else if (cp->link_cntl & CAS_BMCR_SPEED1000) {
  4094. cmd->speed = SPEED_1000;
  4095. }
  4096. cmd->duplex = (cp->link_cntl & BMCR_FULLDPLX)?
  4097. DUPLEX_FULL : DUPLEX_HALF;
  4098. }
  4099. }
  4100. return 0;
  4101. }
  4102. static int cas_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  4103. {
  4104. struct cas *cp = netdev_priv(dev);
  4105. unsigned long flags;
  4106. /* Verify the settings we care about. */
  4107. if (cmd->autoneg != AUTONEG_ENABLE &&
  4108. cmd->autoneg != AUTONEG_DISABLE)
  4109. return -EINVAL;
  4110. if (cmd->autoneg == AUTONEG_DISABLE &&
  4111. ((cmd->speed != SPEED_1000 &&
  4112. cmd->speed != SPEED_100 &&
  4113. cmd->speed != SPEED_10) ||
  4114. (cmd->duplex != DUPLEX_HALF &&
  4115. cmd->duplex != DUPLEX_FULL)))
  4116. return -EINVAL;
  4117. /* Apply settings and restart link process. */
  4118. spin_lock_irqsave(&cp->lock, flags);
  4119. cas_begin_auto_negotiation(cp, cmd);
  4120. spin_unlock_irqrestore(&cp->lock, flags);
  4121. return 0;
  4122. }
  4123. static int cas_nway_reset(struct net_device *dev)
  4124. {
  4125. struct cas *cp = netdev_priv(dev);
  4126. unsigned long flags;
  4127. if ((cp->link_cntl & BMCR_ANENABLE) == 0)
  4128. return -EINVAL;
  4129. /* Restart link process. */
  4130. spin_lock_irqsave(&cp->lock, flags);
  4131. cas_begin_auto_negotiation(cp, NULL);
  4132. spin_unlock_irqrestore(&cp->lock, flags);
  4133. return 0;
  4134. }
  4135. static u32 cas_get_link(struct net_device *dev)
  4136. {
  4137. struct cas *cp = netdev_priv(dev);
  4138. return cp->lstate == link_up;
  4139. }
  4140. static u32 cas_get_msglevel(struct net_device *dev)
  4141. {
  4142. struct cas *cp = netdev_priv(dev);
  4143. return cp->msg_enable;
  4144. }
  4145. static void cas_set_msglevel(struct net_device *dev, u32 value)
  4146. {
  4147. struct cas *cp = netdev_priv(dev);
  4148. cp->msg_enable = value;
  4149. }
  4150. static int cas_get_regs_len(struct net_device *dev)
  4151. {
  4152. struct cas *cp = netdev_priv(dev);
  4153. return cp->casreg_len < CAS_MAX_REGS ? cp->casreg_len: CAS_MAX_REGS;
  4154. }
  4155. static void cas_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  4156. void *p)
  4157. {
  4158. struct cas *cp = netdev_priv(dev);
  4159. regs->version = 0;
  4160. /* cas_read_regs handles locks (cp->lock). */
  4161. cas_read_regs(cp, p, regs->len / sizeof(u32));
  4162. }
  4163. static int cas_get_sset_count(struct net_device *dev, int sset)
  4164. {
  4165. switch (sset) {
  4166. case ETH_SS_STATS:
  4167. return CAS_NUM_STAT_KEYS;
  4168. default:
  4169. return -EOPNOTSUPP;
  4170. }
  4171. }
  4172. static void cas_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  4173. {
  4174. memcpy(data, &ethtool_cassini_statnames,
  4175. CAS_NUM_STAT_KEYS * ETH_GSTRING_LEN);
  4176. }
  4177. static void cas_get_ethtool_stats(struct net_device *dev,
  4178. struct ethtool_stats *estats, u64 *data)
  4179. {
  4180. struct cas *cp = netdev_priv(dev);
  4181. struct net_device_stats *stats = cas_get_stats(cp->dev);
  4182. int i = 0;
  4183. data[i++] = stats->collisions;
  4184. data[i++] = stats->rx_bytes;
  4185. data[i++] = stats->rx_crc_errors;
  4186. data[i++] = stats->rx_dropped;
  4187. data[i++] = stats->rx_errors;
  4188. data[i++] = stats->rx_fifo_errors;
  4189. data[i++] = stats->rx_frame_errors;
  4190. data[i++] = stats->rx_length_errors;
  4191. data[i++] = stats->rx_over_errors;
  4192. data[i++] = stats->rx_packets;
  4193. data[i++] = stats->tx_aborted_errors;
  4194. data[i++] = stats->tx_bytes;
  4195. data[i++] = stats->tx_dropped;
  4196. data[i++] = stats->tx_errors;
  4197. data[i++] = stats->tx_fifo_errors;
  4198. data[i++] = stats->tx_packets;
  4199. BUG_ON(i != CAS_NUM_STAT_KEYS);
  4200. }
  4201. static const struct ethtool_ops cas_ethtool_ops = {
  4202. .get_drvinfo = cas_get_drvinfo,
  4203. .get_settings = cas_get_settings,
  4204. .set_settings = cas_set_settings,
  4205. .nway_reset = cas_nway_reset,
  4206. .get_link = cas_get_link,
  4207. .get_msglevel = cas_get_msglevel,
  4208. .set_msglevel = cas_set_msglevel,
  4209. .get_regs_len = cas_get_regs_len,
  4210. .get_regs = cas_get_regs,
  4211. .get_sset_count = cas_get_sset_count,
  4212. .get_strings = cas_get_strings,
  4213. .get_ethtool_stats = cas_get_ethtool_stats,
  4214. };
  4215. static int cas_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  4216. {
  4217. struct cas *cp = netdev_priv(dev);
  4218. struct mii_ioctl_data *data = if_mii(ifr);
  4219. unsigned long flags;
  4220. int rc = -EOPNOTSUPP;
  4221. /* Hold the PM mutex while doing ioctl's or we may collide
  4222. * with open/close and power management and oops.
  4223. */
  4224. mutex_lock(&cp->pm_mutex);
  4225. switch (cmd) {
  4226. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  4227. data->phy_id = cp->phy_addr;
  4228. /* Fallthrough... */
  4229. case SIOCGMIIREG: /* Read MII PHY register. */
  4230. spin_lock_irqsave(&cp->lock, flags);
  4231. cas_mif_poll(cp, 0);
  4232. data->val_out = cas_phy_read(cp, data->reg_num & 0x1f);
  4233. cas_mif_poll(cp, 1);
  4234. spin_unlock_irqrestore(&cp->lock, flags);
  4235. rc = 0;
  4236. break;
  4237. case SIOCSMIIREG: /* Write MII PHY register. */
  4238. spin_lock_irqsave(&cp->lock, flags);
  4239. cas_mif_poll(cp, 0);
  4240. rc = cas_phy_write(cp, data->reg_num & 0x1f, data->val_in);
  4241. cas_mif_poll(cp, 1);
  4242. spin_unlock_irqrestore(&cp->lock, flags);
  4243. break;
  4244. default:
  4245. break;
  4246. };
  4247. mutex_unlock(&cp->pm_mutex);
  4248. return rc;
  4249. }
  4250. /* When this chip sits underneath an Intel 31154 bridge, it is the
  4251. * only subordinate device and we can tweak the bridge settings to
  4252. * reflect that fact.
  4253. */
  4254. static void __devinit cas_program_bridge(struct pci_dev *cas_pdev)
  4255. {
  4256. struct pci_dev *pdev = cas_pdev->bus->self;
  4257. u32 val;
  4258. if (!pdev)
  4259. return;
  4260. if (pdev->vendor != 0x8086 || pdev->device != 0x537c)
  4261. return;
  4262. /* Clear bit 10 (Bus Parking Control) in the Secondary
  4263. * Arbiter Control/Status Register which lives at offset
  4264. * 0x41. Using a 32-bit word read/modify/write at 0x40
  4265. * is much simpler so that's how we do this.
  4266. */
  4267. pci_read_config_dword(pdev, 0x40, &val);
  4268. val &= ~0x00040000;
  4269. pci_write_config_dword(pdev, 0x40, val);
  4270. /* Max out the Multi-Transaction Timer settings since
  4271. * Cassini is the only device present.
  4272. *
  4273. * The register is 16-bit and lives at 0x50. When the
  4274. * settings are enabled, it extends the GRANT# signal
  4275. * for a requestor after a transaction is complete. This
  4276. * allows the next request to run without first needing
  4277. * to negotiate the GRANT# signal back.
  4278. *
  4279. * Bits 12:10 define the grant duration:
  4280. *
  4281. * 1 -- 16 clocks
  4282. * 2 -- 32 clocks
  4283. * 3 -- 64 clocks
  4284. * 4 -- 128 clocks
  4285. * 5 -- 256 clocks
  4286. *
  4287. * All other values are illegal.
  4288. *
  4289. * Bits 09:00 define which REQ/GNT signal pairs get the
  4290. * GRANT# signal treatment. We set them all.
  4291. */
  4292. pci_write_config_word(pdev, 0x50, (5 << 10) | 0x3ff);
  4293. /* The Read Prefecth Policy register is 16-bit and sits at
  4294. * offset 0x52. It enables a "smart" pre-fetch policy. We
  4295. * enable it and max out all of the settings since only one
  4296. * device is sitting underneath and thus bandwidth sharing is
  4297. * not an issue.
  4298. *
  4299. * The register has several 3 bit fields, which indicates a
  4300. * multiplier applied to the base amount of prefetching the
  4301. * chip would do. These fields are at:
  4302. *
  4303. * 15:13 --- ReRead Primary Bus
  4304. * 12:10 --- FirstRead Primary Bus
  4305. * 09:07 --- ReRead Secondary Bus
  4306. * 06:04 --- FirstRead Secondary Bus
  4307. *
  4308. * Bits 03:00 control which REQ/GNT pairs the prefetch settings
  4309. * get enabled on. Bit 3 is a grouped enabler which controls
  4310. * all of the REQ/GNT pairs from [8:3]. Bits 2 to 0 control
  4311. * the individual REQ/GNT pairs [2:0].
  4312. */
  4313. pci_write_config_word(pdev, 0x52,
  4314. (0x7 << 13) |
  4315. (0x7 << 10) |
  4316. (0x7 << 7) |
  4317. (0x7 << 4) |
  4318. (0xf << 0));
  4319. /* Force cacheline size to 0x8 */
  4320. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  4321. /* Force latency timer to maximum setting so Cassini can
  4322. * sit on the bus as long as it likes.
  4323. */
  4324. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xff);
  4325. }
  4326. static const struct net_device_ops cas_netdev_ops = {
  4327. .ndo_open = cas_open,
  4328. .ndo_stop = cas_close,
  4329. .ndo_start_xmit = cas_start_xmit,
  4330. .ndo_get_stats = cas_get_stats,
  4331. .ndo_set_multicast_list = cas_set_multicast,
  4332. .ndo_do_ioctl = cas_ioctl,
  4333. .ndo_tx_timeout = cas_tx_timeout,
  4334. .ndo_change_mtu = cas_change_mtu,
  4335. .ndo_set_mac_address = eth_mac_addr,
  4336. .ndo_validate_addr = eth_validate_addr,
  4337. #ifdef CONFIG_NET_POLL_CONTROLLER
  4338. .ndo_poll_controller = cas_netpoll,
  4339. #endif
  4340. };
  4341. static int __devinit cas_init_one(struct pci_dev *pdev,
  4342. const struct pci_device_id *ent)
  4343. {
  4344. static int cas_version_printed = 0;
  4345. unsigned long casreg_len;
  4346. struct net_device *dev;
  4347. struct cas *cp;
  4348. int i, err, pci_using_dac;
  4349. u16 pci_cmd;
  4350. u8 orig_cacheline_size = 0, cas_cacheline_size = 0;
  4351. if (cas_version_printed++ == 0)
  4352. printk(KERN_INFO "%s", version);
  4353. err = pci_enable_device(pdev);
  4354. if (err) {
  4355. dev_err(&pdev->dev, "Cannot enable PCI device, aborting.\n");
  4356. return err;
  4357. }
  4358. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  4359. dev_err(&pdev->dev, "Cannot find proper PCI device "
  4360. "base address, aborting.\n");
  4361. err = -ENODEV;
  4362. goto err_out_disable_pdev;
  4363. }
  4364. dev = alloc_etherdev(sizeof(*cp));
  4365. if (!dev) {
  4366. dev_err(&pdev->dev, "Etherdev alloc failed, aborting.\n");
  4367. err = -ENOMEM;
  4368. goto err_out_disable_pdev;
  4369. }
  4370. SET_NETDEV_DEV(dev, &pdev->dev);
  4371. err = pci_request_regions(pdev, dev->name);
  4372. if (err) {
  4373. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting.\n");
  4374. goto err_out_free_netdev;
  4375. }
  4376. pci_set_master(pdev);
  4377. /* we must always turn on parity response or else parity
  4378. * doesn't get generated properly. disable SERR/PERR as well.
  4379. * in addition, we want to turn MWI on.
  4380. */
  4381. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  4382. pci_cmd &= ~PCI_COMMAND_SERR;
  4383. pci_cmd |= PCI_COMMAND_PARITY;
  4384. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  4385. if (pci_try_set_mwi(pdev))
  4386. printk(KERN_WARNING PFX "Could not enable MWI for %s\n",
  4387. pci_name(pdev));
  4388. cas_program_bridge(pdev);
  4389. /*
  4390. * On some architectures, the default cache line size set
  4391. * by pci_try_set_mwi reduces perforamnce. We have to increase
  4392. * it for this case. To start, we'll print some configuration
  4393. * data.
  4394. */
  4395. #if 1
  4396. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  4397. &orig_cacheline_size);
  4398. if (orig_cacheline_size < CAS_PREF_CACHELINE_SIZE) {
  4399. cas_cacheline_size =
  4400. (CAS_PREF_CACHELINE_SIZE < SMP_CACHE_BYTES) ?
  4401. CAS_PREF_CACHELINE_SIZE : SMP_CACHE_BYTES;
  4402. if (pci_write_config_byte(pdev,
  4403. PCI_CACHE_LINE_SIZE,
  4404. cas_cacheline_size)) {
  4405. dev_err(&pdev->dev, "Could not set PCI cache "
  4406. "line size\n");
  4407. goto err_write_cacheline;
  4408. }
  4409. }
  4410. #endif
  4411. /* Configure DMA attributes. */
  4412. if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
  4413. pci_using_dac = 1;
  4414. err = pci_set_consistent_dma_mask(pdev,
  4415. DMA_BIT_MASK(64));
  4416. if (err < 0) {
  4417. dev_err(&pdev->dev, "Unable to obtain 64-bit DMA "
  4418. "for consistent allocations\n");
  4419. goto err_out_free_res;
  4420. }
  4421. } else {
  4422. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4423. if (err) {
  4424. dev_err(&pdev->dev, "No usable DMA configuration, "
  4425. "aborting.\n");
  4426. goto err_out_free_res;
  4427. }
  4428. pci_using_dac = 0;
  4429. }
  4430. casreg_len = pci_resource_len(pdev, 0);
  4431. cp = netdev_priv(dev);
  4432. cp->pdev = pdev;
  4433. #if 1
  4434. /* A value of 0 indicates we never explicitly set it */
  4435. cp->orig_cacheline_size = cas_cacheline_size ? orig_cacheline_size: 0;
  4436. #endif
  4437. cp->dev = dev;
  4438. cp->msg_enable = (cassini_debug < 0) ? CAS_DEF_MSG_ENABLE :
  4439. cassini_debug;
  4440. cp->link_transition = LINK_TRANSITION_UNKNOWN;
  4441. cp->link_transition_jiffies_valid = 0;
  4442. spin_lock_init(&cp->lock);
  4443. spin_lock_init(&cp->rx_inuse_lock);
  4444. spin_lock_init(&cp->rx_spare_lock);
  4445. for (i = 0; i < N_TX_RINGS; i++) {
  4446. spin_lock_init(&cp->stat_lock[i]);
  4447. spin_lock_init(&cp->tx_lock[i]);
  4448. }
  4449. spin_lock_init(&cp->stat_lock[N_TX_RINGS]);
  4450. mutex_init(&cp->pm_mutex);
  4451. init_timer(&cp->link_timer);
  4452. cp->link_timer.function = cas_link_timer;
  4453. cp->link_timer.data = (unsigned long) cp;
  4454. #if 1
  4455. /* Just in case the implementation of atomic operations
  4456. * change so that an explicit initialization is necessary.
  4457. */
  4458. atomic_set(&cp->reset_task_pending, 0);
  4459. atomic_set(&cp->reset_task_pending_all, 0);
  4460. atomic_set(&cp->reset_task_pending_spare, 0);
  4461. atomic_set(&cp->reset_task_pending_mtu, 0);
  4462. #endif
  4463. INIT_WORK(&cp->reset_task, cas_reset_task);
  4464. /* Default link parameters */
  4465. if (link_mode >= 0 && link_mode <= 6)
  4466. cp->link_cntl = link_modes[link_mode];
  4467. else
  4468. cp->link_cntl = BMCR_ANENABLE;
  4469. cp->lstate = link_down;
  4470. cp->link_transition = LINK_TRANSITION_LINK_DOWN;
  4471. netif_carrier_off(cp->dev);
  4472. cp->timer_ticks = 0;
  4473. /* give us access to cassini registers */
  4474. cp->regs = pci_iomap(pdev, 0, casreg_len);
  4475. if (!cp->regs) {
  4476. dev_err(&pdev->dev, "Cannot map device registers, aborting.\n");
  4477. goto err_out_free_res;
  4478. }
  4479. cp->casreg_len = casreg_len;
  4480. pci_save_state(pdev);
  4481. cas_check_pci_invariants(cp);
  4482. cas_hard_reset(cp);
  4483. cas_reset(cp, 0);
  4484. if (cas_check_invariants(cp))
  4485. goto err_out_iounmap;
  4486. if (cp->cas_flags & CAS_FLAG_SATURN)
  4487. if (cas_saturn_firmware_init(cp))
  4488. goto err_out_iounmap;
  4489. cp->init_block = (struct cas_init_block *)
  4490. pci_alloc_consistent(pdev, sizeof(struct cas_init_block),
  4491. &cp->block_dvma);
  4492. if (!cp->init_block) {
  4493. dev_err(&pdev->dev, "Cannot allocate init block, aborting.\n");
  4494. goto err_out_iounmap;
  4495. }
  4496. for (i = 0; i < N_TX_RINGS; i++)
  4497. cp->init_txds[i] = cp->init_block->txds[i];
  4498. for (i = 0; i < N_RX_DESC_RINGS; i++)
  4499. cp->init_rxds[i] = cp->init_block->rxds[i];
  4500. for (i = 0; i < N_RX_COMP_RINGS; i++)
  4501. cp->init_rxcs[i] = cp->init_block->rxcs[i];
  4502. for (i = 0; i < N_RX_FLOWS; i++)
  4503. skb_queue_head_init(&cp->rx_flows[i]);
  4504. dev->netdev_ops = &cas_netdev_ops;
  4505. dev->ethtool_ops = &cas_ethtool_ops;
  4506. dev->watchdog_timeo = CAS_TX_TIMEOUT;
  4507. #ifdef USE_NAPI
  4508. netif_napi_add(dev, &cp->napi, cas_poll, 64);
  4509. #endif
  4510. dev->irq = pdev->irq;
  4511. dev->dma = 0;
  4512. /* Cassini features. */
  4513. if ((cp->cas_flags & CAS_FLAG_NO_HW_CSUM) == 0)
  4514. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  4515. if (pci_using_dac)
  4516. dev->features |= NETIF_F_HIGHDMA;
  4517. if (register_netdev(dev)) {
  4518. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  4519. goto err_out_free_consistent;
  4520. }
  4521. i = readl(cp->regs + REG_BIM_CFG);
  4522. printk(KERN_INFO "%s: Sun Cassini%s (%sbit/%sMHz PCI/%s) "
  4523. "Ethernet[%d] %pM\n", dev->name,
  4524. (cp->cas_flags & CAS_FLAG_REG_PLUS) ? "+" : "",
  4525. (i & BIM_CFG_32BIT) ? "32" : "64",
  4526. (i & BIM_CFG_66MHZ) ? "66" : "33",
  4527. (cp->phy_type == CAS_PHY_SERDES) ? "Fi" : "Cu", pdev->irq,
  4528. dev->dev_addr);
  4529. pci_set_drvdata(pdev, dev);
  4530. cp->hw_running = 1;
  4531. cas_entropy_reset(cp);
  4532. cas_phy_init(cp);
  4533. cas_begin_auto_negotiation(cp, NULL);
  4534. return 0;
  4535. err_out_free_consistent:
  4536. pci_free_consistent(pdev, sizeof(struct cas_init_block),
  4537. cp->init_block, cp->block_dvma);
  4538. err_out_iounmap:
  4539. mutex_lock(&cp->pm_mutex);
  4540. if (cp->hw_running)
  4541. cas_shutdown(cp);
  4542. mutex_unlock(&cp->pm_mutex);
  4543. pci_iounmap(pdev, cp->regs);
  4544. err_out_free_res:
  4545. pci_release_regions(pdev);
  4546. err_write_cacheline:
  4547. /* Try to restore it in case the error occured after we
  4548. * set it.
  4549. */
  4550. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, orig_cacheline_size);
  4551. err_out_free_netdev:
  4552. free_netdev(dev);
  4553. err_out_disable_pdev:
  4554. pci_disable_device(pdev);
  4555. pci_set_drvdata(pdev, NULL);
  4556. return -ENODEV;
  4557. }
  4558. static void __devexit cas_remove_one(struct pci_dev *pdev)
  4559. {
  4560. struct net_device *dev = pci_get_drvdata(pdev);
  4561. struct cas *cp;
  4562. if (!dev)
  4563. return;
  4564. cp = netdev_priv(dev);
  4565. unregister_netdev(dev);
  4566. if (cp->fw_data)
  4567. vfree(cp->fw_data);
  4568. mutex_lock(&cp->pm_mutex);
  4569. flush_scheduled_work();
  4570. if (cp->hw_running)
  4571. cas_shutdown(cp);
  4572. mutex_unlock(&cp->pm_mutex);
  4573. #if 1
  4574. if (cp->orig_cacheline_size) {
  4575. /* Restore the cache line size if we had modified
  4576. * it.
  4577. */
  4578. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  4579. cp->orig_cacheline_size);
  4580. }
  4581. #endif
  4582. pci_free_consistent(pdev, sizeof(struct cas_init_block),
  4583. cp->init_block, cp->block_dvma);
  4584. pci_iounmap(pdev, cp->regs);
  4585. free_netdev(dev);
  4586. pci_release_regions(pdev);
  4587. pci_disable_device(pdev);
  4588. pci_set_drvdata(pdev, NULL);
  4589. }
  4590. #ifdef CONFIG_PM
  4591. static int cas_suspend(struct pci_dev *pdev, pm_message_t state)
  4592. {
  4593. struct net_device *dev = pci_get_drvdata(pdev);
  4594. struct cas *cp = netdev_priv(dev);
  4595. unsigned long flags;
  4596. mutex_lock(&cp->pm_mutex);
  4597. /* If the driver is opened, we stop the DMA */
  4598. if (cp->opened) {
  4599. netif_device_detach(dev);
  4600. cas_lock_all_save(cp, flags);
  4601. /* We can set the second arg of cas_reset to 0
  4602. * because on resume, we'll call cas_init_hw with
  4603. * its second arg set so that autonegotiation is
  4604. * restarted.
  4605. */
  4606. cas_reset(cp, 0);
  4607. cas_clean_rings(cp);
  4608. cas_unlock_all_restore(cp, flags);
  4609. }
  4610. if (cp->hw_running)
  4611. cas_shutdown(cp);
  4612. mutex_unlock(&cp->pm_mutex);
  4613. return 0;
  4614. }
  4615. static int cas_resume(struct pci_dev *pdev)
  4616. {
  4617. struct net_device *dev = pci_get_drvdata(pdev);
  4618. struct cas *cp = netdev_priv(dev);
  4619. printk(KERN_INFO "%s: resuming\n", dev->name);
  4620. mutex_lock(&cp->pm_mutex);
  4621. cas_hard_reset(cp);
  4622. if (cp->opened) {
  4623. unsigned long flags;
  4624. cas_lock_all_save(cp, flags);
  4625. cas_reset(cp, 0);
  4626. cp->hw_running = 1;
  4627. cas_clean_rings(cp);
  4628. cas_init_hw(cp, 1);
  4629. cas_unlock_all_restore(cp, flags);
  4630. netif_device_attach(dev);
  4631. }
  4632. mutex_unlock(&cp->pm_mutex);
  4633. return 0;
  4634. }
  4635. #endif /* CONFIG_PM */
  4636. static struct pci_driver cas_driver = {
  4637. .name = DRV_MODULE_NAME,
  4638. .id_table = cas_pci_tbl,
  4639. .probe = cas_init_one,
  4640. .remove = __devexit_p(cas_remove_one),
  4641. #ifdef CONFIG_PM
  4642. .suspend = cas_suspend,
  4643. .resume = cas_resume
  4644. #endif
  4645. };
  4646. static int __init cas_init(void)
  4647. {
  4648. if (linkdown_timeout > 0)
  4649. link_transition_timeout = linkdown_timeout * HZ;
  4650. else
  4651. link_transition_timeout = 0;
  4652. return pci_register_driver(&cas_driver);
  4653. }
  4654. static void __exit cas_cleanup(void)
  4655. {
  4656. pci_unregister_driver(&cas_driver);
  4657. }
  4658. module_init(cas_init);
  4659. module_exit(cas_cleanup);