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. #if defined(CONFIG_CASSINI_NAPI) && defined(HAVE_NETDEV_POLL)
  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 struct pci_device_id cas_pci_tbl[] __devinitdata = {
  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 *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. cp->dev->last_rx = jiffies;
  2095. next:
  2096. npackets++;
  2097. /* should it be released? */
  2098. if (words[0] & RX_COMP1_RELEASE_HDR) {
  2099. i = CAS_VAL(RX_COMP2_HDR_INDEX, words[1]);
  2100. dring = CAS_VAL(RX_INDEX_RING, i);
  2101. i = CAS_VAL(RX_INDEX_NUM, i);
  2102. cas_post_page(cp, dring, i);
  2103. }
  2104. if (words[0] & RX_COMP1_RELEASE_DATA) {
  2105. i = CAS_VAL(RX_COMP1_DATA_INDEX, words[0]);
  2106. dring = CAS_VAL(RX_INDEX_RING, i);
  2107. i = CAS_VAL(RX_INDEX_NUM, i);
  2108. cas_post_page(cp, dring, i);
  2109. }
  2110. if (words[0] & RX_COMP1_RELEASE_NEXT) {
  2111. i = CAS_VAL(RX_COMP2_NEXT_INDEX, words[1]);
  2112. dring = CAS_VAL(RX_INDEX_RING, i);
  2113. i = CAS_VAL(RX_INDEX_NUM, i);
  2114. cas_post_page(cp, dring, i);
  2115. }
  2116. /* skip to the next entry */
  2117. entry = RX_COMP_ENTRY(ring, entry + 1 +
  2118. CAS_VAL(RX_COMP1_SKIP, words[0]));
  2119. #ifdef USE_NAPI
  2120. if (budget && (npackets >= budget))
  2121. break;
  2122. #endif
  2123. }
  2124. cp->rx_new[ring] = entry;
  2125. if (drops)
  2126. printk(KERN_INFO "%s: Memory squeeze, deferring packet.\n",
  2127. cp->dev->name);
  2128. return npackets;
  2129. }
  2130. /* put completion entries back on the ring */
  2131. static void cas_post_rxcs_ringN(struct net_device *dev,
  2132. struct cas *cp, int ring)
  2133. {
  2134. struct cas_rx_comp *rxc = cp->init_rxcs[ring];
  2135. int last, entry;
  2136. last = cp->rx_cur[ring];
  2137. entry = cp->rx_new[ring];
  2138. if (netif_msg_intr(cp))
  2139. printk(KERN_DEBUG "%s: rxc[%d] interrupt, done: %d/%d\n",
  2140. dev->name, ring, readl(cp->regs + REG_RX_COMP_HEAD),
  2141. entry);
  2142. /* zero and re-mark descriptors */
  2143. while (last != entry) {
  2144. cas_rxc_init(rxc + last);
  2145. last = RX_COMP_ENTRY(ring, last + 1);
  2146. }
  2147. cp->rx_cur[ring] = last;
  2148. if (ring == 0)
  2149. writel(last, cp->regs + REG_RX_COMP_TAIL);
  2150. else if (cp->cas_flags & CAS_FLAG_REG_PLUS)
  2151. writel(last, cp->regs + REG_PLUS_RX_COMPN_TAIL(ring));
  2152. }
  2153. /* cassini can use all four PCI interrupts for the completion ring.
  2154. * rings 3 and 4 are identical
  2155. */
  2156. #if defined(USE_PCI_INTC) || defined(USE_PCI_INTD)
  2157. static inline void cas_handle_irqN(struct net_device *dev,
  2158. struct cas *cp, const u32 status,
  2159. const int ring)
  2160. {
  2161. if (status & (INTR_RX_COMP_FULL_ALT | INTR_RX_COMP_AF_ALT))
  2162. cas_post_rxcs_ringN(dev, cp, ring);
  2163. }
  2164. static irqreturn_t cas_interruptN(int irq, void *dev_id)
  2165. {
  2166. struct net_device *dev = dev_id;
  2167. struct cas *cp = netdev_priv(dev);
  2168. unsigned long flags;
  2169. int ring;
  2170. u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(ring));
  2171. /* check for shared irq */
  2172. if (status == 0)
  2173. return IRQ_NONE;
  2174. ring = (irq == cp->pci_irq_INTC) ? 2 : 3;
  2175. spin_lock_irqsave(&cp->lock, flags);
  2176. if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
  2177. #ifdef USE_NAPI
  2178. cas_mask_intr(cp);
  2179. netif_rx_schedule(dev, &cp->napi);
  2180. #else
  2181. cas_rx_ringN(cp, ring, 0);
  2182. #endif
  2183. status &= ~INTR_RX_DONE_ALT;
  2184. }
  2185. if (status)
  2186. cas_handle_irqN(dev, cp, status, ring);
  2187. spin_unlock_irqrestore(&cp->lock, flags);
  2188. return IRQ_HANDLED;
  2189. }
  2190. #endif
  2191. #ifdef USE_PCI_INTB
  2192. /* everything but rx packets */
  2193. static inline void cas_handle_irq1(struct cas *cp, const u32 status)
  2194. {
  2195. if (status & INTR_RX_BUF_UNAVAIL_1) {
  2196. /* Frame arrived, no free RX buffers available.
  2197. * NOTE: we can get this on a link transition. */
  2198. cas_post_rxds_ringN(cp, 1, 0);
  2199. spin_lock(&cp->stat_lock[1]);
  2200. cp->net_stats[1].rx_dropped++;
  2201. spin_unlock(&cp->stat_lock[1]);
  2202. }
  2203. if (status & INTR_RX_BUF_AE_1)
  2204. cas_post_rxds_ringN(cp, 1, RX_DESC_RINGN_SIZE(1) -
  2205. RX_AE_FREEN_VAL(1));
  2206. if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
  2207. cas_post_rxcs_ringN(cp, 1);
  2208. }
  2209. /* ring 2 handles a few more events than 3 and 4 */
  2210. static irqreturn_t cas_interrupt1(int irq, void *dev_id)
  2211. {
  2212. struct net_device *dev = dev_id;
  2213. struct cas *cp = netdev_priv(dev);
  2214. unsigned long flags;
  2215. u32 status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
  2216. /* check for shared interrupt */
  2217. if (status == 0)
  2218. return IRQ_NONE;
  2219. spin_lock_irqsave(&cp->lock, flags);
  2220. if (status & INTR_RX_DONE_ALT) { /* handle rx separately */
  2221. #ifdef USE_NAPI
  2222. cas_mask_intr(cp);
  2223. netif_rx_schedule(dev, &cp->napi);
  2224. #else
  2225. cas_rx_ringN(cp, 1, 0);
  2226. #endif
  2227. status &= ~INTR_RX_DONE_ALT;
  2228. }
  2229. if (status)
  2230. cas_handle_irq1(cp, status);
  2231. spin_unlock_irqrestore(&cp->lock, flags);
  2232. return IRQ_HANDLED;
  2233. }
  2234. #endif
  2235. static inline void cas_handle_irq(struct net_device *dev,
  2236. struct cas *cp, const u32 status)
  2237. {
  2238. /* housekeeping interrupts */
  2239. if (status & INTR_ERROR_MASK)
  2240. cas_abnormal_irq(dev, cp, status);
  2241. if (status & INTR_RX_BUF_UNAVAIL) {
  2242. /* Frame arrived, no free RX buffers available.
  2243. * NOTE: we can get this on a link transition.
  2244. */
  2245. cas_post_rxds_ringN(cp, 0, 0);
  2246. spin_lock(&cp->stat_lock[0]);
  2247. cp->net_stats[0].rx_dropped++;
  2248. spin_unlock(&cp->stat_lock[0]);
  2249. } else if (status & INTR_RX_BUF_AE) {
  2250. cas_post_rxds_ringN(cp, 0, RX_DESC_RINGN_SIZE(0) -
  2251. RX_AE_FREEN_VAL(0));
  2252. }
  2253. if (status & (INTR_RX_COMP_AF | INTR_RX_COMP_FULL))
  2254. cas_post_rxcs_ringN(dev, cp, 0);
  2255. }
  2256. static irqreturn_t cas_interrupt(int irq, void *dev_id)
  2257. {
  2258. struct net_device *dev = dev_id;
  2259. struct cas *cp = netdev_priv(dev);
  2260. unsigned long flags;
  2261. u32 status = readl(cp->regs + REG_INTR_STATUS);
  2262. if (status == 0)
  2263. return IRQ_NONE;
  2264. spin_lock_irqsave(&cp->lock, flags);
  2265. if (status & (INTR_TX_ALL | INTR_TX_INTME)) {
  2266. cas_tx(dev, cp, status);
  2267. status &= ~(INTR_TX_ALL | INTR_TX_INTME);
  2268. }
  2269. if (status & INTR_RX_DONE) {
  2270. #ifdef USE_NAPI
  2271. cas_mask_intr(cp);
  2272. netif_rx_schedule(dev, &cp->napi);
  2273. #else
  2274. cas_rx_ringN(cp, 0, 0);
  2275. #endif
  2276. status &= ~INTR_RX_DONE;
  2277. }
  2278. if (status)
  2279. cas_handle_irq(dev, cp, status);
  2280. spin_unlock_irqrestore(&cp->lock, flags);
  2281. return IRQ_HANDLED;
  2282. }
  2283. #ifdef USE_NAPI
  2284. static int cas_poll(struct napi_struct *napi, int budget)
  2285. {
  2286. struct cas *cp = container_of(napi, struct cas, napi);
  2287. struct net_device *dev = cp->dev;
  2288. int i, enable_intr, credits;
  2289. u32 status = readl(cp->regs + REG_INTR_STATUS);
  2290. unsigned long flags;
  2291. spin_lock_irqsave(&cp->lock, flags);
  2292. cas_tx(dev, cp, status);
  2293. spin_unlock_irqrestore(&cp->lock, flags);
  2294. /* NAPI rx packets. we spread the credits across all of the
  2295. * rxc rings
  2296. *
  2297. * to make sure we're fair with the work we loop through each
  2298. * ring N_RX_COMP_RING times with a request of
  2299. * budget / N_RX_COMP_RINGS
  2300. */
  2301. enable_intr = 1;
  2302. credits = 0;
  2303. for (i = 0; i < N_RX_COMP_RINGS; i++) {
  2304. int j;
  2305. for (j = 0; j < N_RX_COMP_RINGS; j++) {
  2306. credits += cas_rx_ringN(cp, j, budget / N_RX_COMP_RINGS);
  2307. if (credits >= budget) {
  2308. enable_intr = 0;
  2309. goto rx_comp;
  2310. }
  2311. }
  2312. }
  2313. rx_comp:
  2314. /* final rx completion */
  2315. spin_lock_irqsave(&cp->lock, flags);
  2316. if (status)
  2317. cas_handle_irq(dev, cp, status);
  2318. #ifdef USE_PCI_INTB
  2319. if (N_RX_COMP_RINGS > 1) {
  2320. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(1));
  2321. if (status)
  2322. cas_handle_irq1(dev, cp, status);
  2323. }
  2324. #endif
  2325. #ifdef USE_PCI_INTC
  2326. if (N_RX_COMP_RINGS > 2) {
  2327. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(2));
  2328. if (status)
  2329. cas_handle_irqN(dev, cp, status, 2);
  2330. }
  2331. #endif
  2332. #ifdef USE_PCI_INTD
  2333. if (N_RX_COMP_RINGS > 3) {
  2334. status = readl(cp->regs + REG_PLUS_INTRN_STATUS(3));
  2335. if (status)
  2336. cas_handle_irqN(dev, cp, status, 3);
  2337. }
  2338. #endif
  2339. spin_unlock_irqrestore(&cp->lock, flags);
  2340. if (enable_intr) {
  2341. netif_rx_complete(dev, napi);
  2342. cas_unmask_intr(cp);
  2343. }
  2344. return credits;
  2345. }
  2346. #endif
  2347. #ifdef CONFIG_NET_POLL_CONTROLLER
  2348. static void cas_netpoll(struct net_device *dev)
  2349. {
  2350. struct cas *cp = netdev_priv(dev);
  2351. cas_disable_irq(cp, 0);
  2352. cas_interrupt(cp->pdev->irq, dev);
  2353. cas_enable_irq(cp, 0);
  2354. #ifdef USE_PCI_INTB
  2355. if (N_RX_COMP_RINGS > 1) {
  2356. /* cas_interrupt1(); */
  2357. }
  2358. #endif
  2359. #ifdef USE_PCI_INTC
  2360. if (N_RX_COMP_RINGS > 2) {
  2361. /* cas_interruptN(); */
  2362. }
  2363. #endif
  2364. #ifdef USE_PCI_INTD
  2365. if (N_RX_COMP_RINGS > 3) {
  2366. /* cas_interruptN(); */
  2367. }
  2368. #endif
  2369. }
  2370. #endif
  2371. static void cas_tx_timeout(struct net_device *dev)
  2372. {
  2373. struct cas *cp = netdev_priv(dev);
  2374. printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
  2375. if (!cp->hw_running) {
  2376. printk("%s: hrm.. hw not running!\n", dev->name);
  2377. return;
  2378. }
  2379. printk(KERN_ERR "%s: MIF_STATE[%08x]\n",
  2380. dev->name, readl(cp->regs + REG_MIF_STATE_MACHINE));
  2381. printk(KERN_ERR "%s: MAC_STATE[%08x]\n",
  2382. dev->name, readl(cp->regs + REG_MAC_STATE_MACHINE));
  2383. printk(KERN_ERR "%s: TX_STATE[%08x:%08x:%08x] "
  2384. "FIFO[%08x:%08x:%08x] SM1[%08x] SM2[%08x]\n",
  2385. dev->name,
  2386. readl(cp->regs + REG_TX_CFG),
  2387. readl(cp->regs + REG_MAC_TX_STATUS),
  2388. readl(cp->regs + REG_MAC_TX_CFG),
  2389. readl(cp->regs + REG_TX_FIFO_PKT_CNT),
  2390. readl(cp->regs + REG_TX_FIFO_WRITE_PTR),
  2391. readl(cp->regs + REG_TX_FIFO_READ_PTR),
  2392. readl(cp->regs + REG_TX_SM_1),
  2393. readl(cp->regs + REG_TX_SM_2));
  2394. printk(KERN_ERR "%s: RX_STATE[%08x:%08x:%08x]\n",
  2395. dev->name,
  2396. readl(cp->regs + REG_RX_CFG),
  2397. readl(cp->regs + REG_MAC_RX_STATUS),
  2398. readl(cp->regs + REG_MAC_RX_CFG));
  2399. printk(KERN_ERR "%s: HP_STATE[%08x:%08x:%08x:%08x]\n",
  2400. dev->name,
  2401. readl(cp->regs + REG_HP_STATE_MACHINE),
  2402. readl(cp->regs + REG_HP_STATUS0),
  2403. readl(cp->regs + REG_HP_STATUS1),
  2404. readl(cp->regs + REG_HP_STATUS2));
  2405. #if 1
  2406. atomic_inc(&cp->reset_task_pending);
  2407. atomic_inc(&cp->reset_task_pending_all);
  2408. schedule_work(&cp->reset_task);
  2409. #else
  2410. atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
  2411. schedule_work(&cp->reset_task);
  2412. #endif
  2413. }
  2414. static inline int cas_intme(int ring, int entry)
  2415. {
  2416. /* Algorithm: IRQ every 1/2 of descriptors. */
  2417. if (!(entry & ((TX_DESC_RINGN_SIZE(ring) >> 1) - 1)))
  2418. return 1;
  2419. return 0;
  2420. }
  2421. static void cas_write_txd(struct cas *cp, int ring, int entry,
  2422. dma_addr_t mapping, int len, u64 ctrl, int last)
  2423. {
  2424. struct cas_tx_desc *txd = cp->init_txds[ring] + entry;
  2425. ctrl |= CAS_BASE(TX_DESC_BUFLEN, len);
  2426. if (cas_intme(ring, entry))
  2427. ctrl |= TX_DESC_INTME;
  2428. if (last)
  2429. ctrl |= TX_DESC_EOF;
  2430. txd->control = cpu_to_le64(ctrl);
  2431. txd->buffer = cpu_to_le64(mapping);
  2432. }
  2433. static inline void *tx_tiny_buf(struct cas *cp, const int ring,
  2434. const int entry)
  2435. {
  2436. return cp->tx_tiny_bufs[ring] + TX_TINY_BUF_LEN*entry;
  2437. }
  2438. static inline dma_addr_t tx_tiny_map(struct cas *cp, const int ring,
  2439. const int entry, const int tentry)
  2440. {
  2441. cp->tx_tiny_use[ring][tentry].nbufs++;
  2442. cp->tx_tiny_use[ring][entry].used = 1;
  2443. return cp->tx_tiny_dvma[ring] + TX_TINY_BUF_LEN*entry;
  2444. }
  2445. static inline int cas_xmit_tx_ringN(struct cas *cp, int ring,
  2446. struct sk_buff *skb)
  2447. {
  2448. struct net_device *dev = cp->dev;
  2449. int entry, nr_frags, frag, tabort, tentry;
  2450. dma_addr_t mapping;
  2451. unsigned long flags;
  2452. u64 ctrl;
  2453. u32 len;
  2454. spin_lock_irqsave(&cp->tx_lock[ring], flags);
  2455. /* This is a hard error, log it. */
  2456. if (TX_BUFFS_AVAIL(cp, ring) <=
  2457. CAS_TABORT(cp)*(skb_shinfo(skb)->nr_frags + 1)) {
  2458. netif_stop_queue(dev);
  2459. spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
  2460. printk(KERN_ERR PFX "%s: BUG! Tx Ring full when "
  2461. "queue awake!\n", dev->name);
  2462. return 1;
  2463. }
  2464. ctrl = 0;
  2465. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  2466. const u64 csum_start_off = skb_transport_offset(skb);
  2467. const u64 csum_stuff_off = csum_start_off + skb->csum_offset;
  2468. ctrl = TX_DESC_CSUM_EN |
  2469. CAS_BASE(TX_DESC_CSUM_START, csum_start_off) |
  2470. CAS_BASE(TX_DESC_CSUM_STUFF, csum_stuff_off);
  2471. }
  2472. entry = cp->tx_new[ring];
  2473. cp->tx_skbs[ring][entry] = skb;
  2474. nr_frags = skb_shinfo(skb)->nr_frags;
  2475. len = skb_headlen(skb);
  2476. mapping = pci_map_page(cp->pdev, virt_to_page(skb->data),
  2477. offset_in_page(skb->data), len,
  2478. PCI_DMA_TODEVICE);
  2479. tentry = entry;
  2480. tabort = cas_calc_tabort(cp, (unsigned long) skb->data, len);
  2481. if (unlikely(tabort)) {
  2482. /* NOTE: len is always > tabort */
  2483. cas_write_txd(cp, ring, entry, mapping, len - tabort,
  2484. ctrl | TX_DESC_SOF, 0);
  2485. entry = TX_DESC_NEXT(ring, entry);
  2486. skb_copy_from_linear_data_offset(skb, len - tabort,
  2487. tx_tiny_buf(cp, ring, entry), tabort);
  2488. mapping = tx_tiny_map(cp, ring, entry, tentry);
  2489. cas_write_txd(cp, ring, entry, mapping, tabort, ctrl,
  2490. (nr_frags == 0));
  2491. } else {
  2492. cas_write_txd(cp, ring, entry, mapping, len, ctrl |
  2493. TX_DESC_SOF, (nr_frags == 0));
  2494. }
  2495. entry = TX_DESC_NEXT(ring, entry);
  2496. for (frag = 0; frag < nr_frags; frag++) {
  2497. skb_frag_t *fragp = &skb_shinfo(skb)->frags[frag];
  2498. len = fragp->size;
  2499. mapping = pci_map_page(cp->pdev, fragp->page,
  2500. fragp->page_offset, len,
  2501. PCI_DMA_TODEVICE);
  2502. tabort = cas_calc_tabort(cp, fragp->page_offset, len);
  2503. if (unlikely(tabort)) {
  2504. void *addr;
  2505. /* NOTE: len is always > tabort */
  2506. cas_write_txd(cp, ring, entry, mapping, len - tabort,
  2507. ctrl, 0);
  2508. entry = TX_DESC_NEXT(ring, entry);
  2509. addr = cas_page_map(fragp->page);
  2510. memcpy(tx_tiny_buf(cp, ring, entry),
  2511. addr + fragp->page_offset + len - tabort,
  2512. tabort);
  2513. cas_page_unmap(addr);
  2514. mapping = tx_tiny_map(cp, ring, entry, tentry);
  2515. len = tabort;
  2516. }
  2517. cas_write_txd(cp, ring, entry, mapping, len, ctrl,
  2518. (frag + 1 == nr_frags));
  2519. entry = TX_DESC_NEXT(ring, entry);
  2520. }
  2521. cp->tx_new[ring] = entry;
  2522. if (TX_BUFFS_AVAIL(cp, ring) <= CAS_TABORT(cp)*(MAX_SKB_FRAGS + 1))
  2523. netif_stop_queue(dev);
  2524. if (netif_msg_tx_queued(cp))
  2525. printk(KERN_DEBUG "%s: tx[%d] queued, slot %d, skblen %d, "
  2526. "avail %d\n",
  2527. dev->name, ring, entry, skb->len,
  2528. TX_BUFFS_AVAIL(cp, ring));
  2529. writel(entry, cp->regs + REG_TX_KICKN(ring));
  2530. spin_unlock_irqrestore(&cp->tx_lock[ring], flags);
  2531. return 0;
  2532. }
  2533. static int cas_start_xmit(struct sk_buff *skb, struct net_device *dev)
  2534. {
  2535. struct cas *cp = netdev_priv(dev);
  2536. /* this is only used as a load-balancing hint, so it doesn't
  2537. * need to be SMP safe
  2538. */
  2539. static int ring;
  2540. if (skb_padto(skb, cp->min_frame_size))
  2541. return 0;
  2542. /* XXX: we need some higher-level QoS hooks to steer packets to
  2543. * individual queues.
  2544. */
  2545. if (cas_xmit_tx_ringN(cp, ring++ & N_TX_RINGS_MASK, skb))
  2546. return 1;
  2547. dev->trans_start = jiffies;
  2548. return 0;
  2549. }
  2550. static void cas_init_tx_dma(struct cas *cp)
  2551. {
  2552. u64 desc_dma = cp->block_dvma;
  2553. unsigned long off;
  2554. u32 val;
  2555. int i;
  2556. /* set up tx completion writeback registers. must be 8-byte aligned */
  2557. #ifdef USE_TX_COMPWB
  2558. off = offsetof(struct cas_init_block, tx_compwb);
  2559. writel((desc_dma + off) >> 32, cp->regs + REG_TX_COMPWB_DB_HI);
  2560. writel((desc_dma + off) & 0xffffffff, cp->regs + REG_TX_COMPWB_DB_LOW);
  2561. #endif
  2562. /* enable completion writebacks, enable paced mode,
  2563. * disable read pipe, and disable pre-interrupt compwbs
  2564. */
  2565. val = TX_CFG_COMPWB_Q1 | TX_CFG_COMPWB_Q2 |
  2566. TX_CFG_COMPWB_Q3 | TX_CFG_COMPWB_Q4 |
  2567. TX_CFG_DMA_RDPIPE_DIS | TX_CFG_PACED_MODE |
  2568. TX_CFG_INTR_COMPWB_DIS;
  2569. /* write out tx ring info and tx desc bases */
  2570. for (i = 0; i < MAX_TX_RINGS; i++) {
  2571. off = (unsigned long) cp->init_txds[i] -
  2572. (unsigned long) cp->init_block;
  2573. val |= CAS_TX_RINGN_BASE(i);
  2574. writel((desc_dma + off) >> 32, cp->regs + REG_TX_DBN_HI(i));
  2575. writel((desc_dma + off) & 0xffffffff, cp->regs +
  2576. REG_TX_DBN_LOW(i));
  2577. /* don't zero out the kick register here as the system
  2578. * will wedge
  2579. */
  2580. }
  2581. writel(val, cp->regs + REG_TX_CFG);
  2582. /* program max burst sizes. these numbers should be different
  2583. * if doing QoS.
  2584. */
  2585. #ifdef USE_QOS
  2586. writel(0x800, cp->regs + REG_TX_MAXBURST_0);
  2587. writel(0x1600, cp->regs + REG_TX_MAXBURST_1);
  2588. writel(0x2400, cp->regs + REG_TX_MAXBURST_2);
  2589. writel(0x4800, cp->regs + REG_TX_MAXBURST_3);
  2590. #else
  2591. writel(0x800, cp->regs + REG_TX_MAXBURST_0);
  2592. writel(0x800, cp->regs + REG_TX_MAXBURST_1);
  2593. writel(0x800, cp->regs + REG_TX_MAXBURST_2);
  2594. writel(0x800, cp->regs + REG_TX_MAXBURST_3);
  2595. #endif
  2596. }
  2597. /* Must be invoked under cp->lock. */
  2598. static inline void cas_init_dma(struct cas *cp)
  2599. {
  2600. cas_init_tx_dma(cp);
  2601. cas_init_rx_dma(cp);
  2602. }
  2603. /* Must be invoked under cp->lock. */
  2604. static u32 cas_setup_multicast(struct cas *cp)
  2605. {
  2606. u32 rxcfg = 0;
  2607. int i;
  2608. if (cp->dev->flags & IFF_PROMISC) {
  2609. rxcfg |= MAC_RX_CFG_PROMISC_EN;
  2610. } else if (cp->dev->flags & IFF_ALLMULTI) {
  2611. for (i=0; i < 16; i++)
  2612. writel(0xFFFF, cp->regs + REG_MAC_HASH_TABLEN(i));
  2613. rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
  2614. } else {
  2615. u16 hash_table[16];
  2616. u32 crc;
  2617. struct dev_mc_list *dmi = cp->dev->mc_list;
  2618. int i;
  2619. /* use the alternate mac address registers for the
  2620. * first 15 multicast addresses
  2621. */
  2622. for (i = 1; i <= CAS_MC_EXACT_MATCH_SIZE; i++) {
  2623. if (!dmi) {
  2624. writel(0x0, cp->regs + REG_MAC_ADDRN(i*3 + 0));
  2625. writel(0x0, cp->regs + REG_MAC_ADDRN(i*3 + 1));
  2626. writel(0x0, cp->regs + REG_MAC_ADDRN(i*3 + 2));
  2627. continue;
  2628. }
  2629. writel((dmi->dmi_addr[4] << 8) | dmi->dmi_addr[5],
  2630. cp->regs + REG_MAC_ADDRN(i*3 + 0));
  2631. writel((dmi->dmi_addr[2] << 8) | dmi->dmi_addr[3],
  2632. cp->regs + REG_MAC_ADDRN(i*3 + 1));
  2633. writel((dmi->dmi_addr[0] << 8) | dmi->dmi_addr[1],
  2634. cp->regs + REG_MAC_ADDRN(i*3 + 2));
  2635. dmi = dmi->next;
  2636. }
  2637. /* use hw hash table for the next series of
  2638. * multicast addresses
  2639. */
  2640. memset(hash_table, 0, sizeof(hash_table));
  2641. while (dmi) {
  2642. crc = ether_crc_le(ETH_ALEN, dmi->dmi_addr);
  2643. crc >>= 24;
  2644. hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
  2645. dmi = dmi->next;
  2646. }
  2647. for (i=0; i < 16; i++)
  2648. writel(hash_table[i], cp->regs +
  2649. REG_MAC_HASH_TABLEN(i));
  2650. rxcfg |= MAC_RX_CFG_HASH_FILTER_EN;
  2651. }
  2652. return rxcfg;
  2653. }
  2654. /* must be invoked under cp->stat_lock[N_TX_RINGS] */
  2655. static void cas_clear_mac_err(struct cas *cp)
  2656. {
  2657. writel(0, cp->regs + REG_MAC_COLL_NORMAL);
  2658. writel(0, cp->regs + REG_MAC_COLL_FIRST);
  2659. writel(0, cp->regs + REG_MAC_COLL_EXCESS);
  2660. writel(0, cp->regs + REG_MAC_COLL_LATE);
  2661. writel(0, cp->regs + REG_MAC_TIMER_DEFER);
  2662. writel(0, cp->regs + REG_MAC_ATTEMPTS_PEAK);
  2663. writel(0, cp->regs + REG_MAC_RECV_FRAME);
  2664. writel(0, cp->regs + REG_MAC_LEN_ERR);
  2665. writel(0, cp->regs + REG_MAC_ALIGN_ERR);
  2666. writel(0, cp->regs + REG_MAC_FCS_ERR);
  2667. writel(0, cp->regs + REG_MAC_RX_CODE_ERR);
  2668. }
  2669. static void cas_mac_reset(struct cas *cp)
  2670. {
  2671. int i;
  2672. /* do both TX and RX reset */
  2673. writel(0x1, cp->regs + REG_MAC_TX_RESET);
  2674. writel(0x1, cp->regs + REG_MAC_RX_RESET);
  2675. /* wait for TX */
  2676. i = STOP_TRIES;
  2677. while (i-- > 0) {
  2678. if (readl(cp->regs + REG_MAC_TX_RESET) == 0)
  2679. break;
  2680. udelay(10);
  2681. }
  2682. /* wait for RX */
  2683. i = STOP_TRIES;
  2684. while (i-- > 0) {
  2685. if (readl(cp->regs + REG_MAC_RX_RESET) == 0)
  2686. break;
  2687. udelay(10);
  2688. }
  2689. if (readl(cp->regs + REG_MAC_TX_RESET) |
  2690. readl(cp->regs + REG_MAC_RX_RESET))
  2691. printk(KERN_ERR "%s: mac tx[%d]/rx[%d] reset failed [%08x]\n",
  2692. cp->dev->name, readl(cp->regs + REG_MAC_TX_RESET),
  2693. readl(cp->regs + REG_MAC_RX_RESET),
  2694. readl(cp->regs + REG_MAC_STATE_MACHINE));
  2695. }
  2696. /* Must be invoked under cp->lock. */
  2697. static void cas_init_mac(struct cas *cp)
  2698. {
  2699. unsigned char *e = &cp->dev->dev_addr[0];
  2700. int i;
  2701. #ifdef CONFIG_CASSINI_MULTICAST_REG_WRITE
  2702. u32 rxcfg;
  2703. #endif
  2704. cas_mac_reset(cp);
  2705. /* setup core arbitration weight register */
  2706. writel(CAWR_RR_DIS, cp->regs + REG_CAWR);
  2707. /* XXX Use pci_dma_burst_advice() */
  2708. #if !defined(CONFIG_SPARC64) && !defined(CONFIG_ALPHA)
  2709. /* set the infinite burst register for chips that don't have
  2710. * pci issues.
  2711. */
  2712. if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) == 0)
  2713. writel(INF_BURST_EN, cp->regs + REG_INF_BURST);
  2714. #endif
  2715. writel(0x1BF0, cp->regs + REG_MAC_SEND_PAUSE);
  2716. writel(0x00, cp->regs + REG_MAC_IPG0);
  2717. writel(0x08, cp->regs + REG_MAC_IPG1);
  2718. writel(0x04, cp->regs + REG_MAC_IPG2);
  2719. /* change later for 802.3z */
  2720. writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
  2721. /* min frame + FCS */
  2722. writel(ETH_ZLEN + 4, cp->regs + REG_MAC_FRAMESIZE_MIN);
  2723. /* Ethernet payload + header + FCS + optional VLAN tag. NOTE: we
  2724. * specify the maximum frame size to prevent RX tag errors on
  2725. * oversized frames.
  2726. */
  2727. writel(CAS_BASE(MAC_FRAMESIZE_MAX_BURST, 0x2000) |
  2728. CAS_BASE(MAC_FRAMESIZE_MAX_FRAME,
  2729. (CAS_MAX_MTU + ETH_HLEN + 4 + 4)),
  2730. cp->regs + REG_MAC_FRAMESIZE_MAX);
  2731. /* NOTE: crc_size is used as a surrogate for half-duplex.
  2732. * workaround saturn half-duplex issue by increasing preamble
  2733. * size to 65 bytes.
  2734. */
  2735. if ((cp->cas_flags & CAS_FLAG_SATURN) && cp->crc_size)
  2736. writel(0x41, cp->regs + REG_MAC_PA_SIZE);
  2737. else
  2738. writel(0x07, cp->regs + REG_MAC_PA_SIZE);
  2739. writel(0x04, cp->regs + REG_MAC_JAM_SIZE);
  2740. writel(0x10, cp->regs + REG_MAC_ATTEMPT_LIMIT);
  2741. writel(0x8808, cp->regs + REG_MAC_CTRL_TYPE);
  2742. writel((e[5] | (e[4] << 8)) & 0x3ff, cp->regs + REG_MAC_RANDOM_SEED);
  2743. writel(0, cp->regs + REG_MAC_ADDR_FILTER0);
  2744. writel(0, cp->regs + REG_MAC_ADDR_FILTER1);
  2745. writel(0, cp->regs + REG_MAC_ADDR_FILTER2);
  2746. writel(0, cp->regs + REG_MAC_ADDR_FILTER2_1_MASK);
  2747. writel(0, cp->regs + REG_MAC_ADDR_FILTER0_MASK);
  2748. /* setup mac address in perfect filter array */
  2749. for (i = 0; i < 45; i++)
  2750. writel(0x0, cp->regs + REG_MAC_ADDRN(i));
  2751. writel((e[4] << 8) | e[5], cp->regs + REG_MAC_ADDRN(0));
  2752. writel((e[2] << 8) | e[3], cp->regs + REG_MAC_ADDRN(1));
  2753. writel((e[0] << 8) | e[1], cp->regs + REG_MAC_ADDRN(2));
  2754. writel(0x0001, cp->regs + REG_MAC_ADDRN(42));
  2755. writel(0xc200, cp->regs + REG_MAC_ADDRN(43));
  2756. writel(0x0180, cp->regs + REG_MAC_ADDRN(44));
  2757. #ifndef CONFIG_CASSINI_MULTICAST_REG_WRITE
  2758. cp->mac_rx_cfg = cas_setup_multicast(cp);
  2759. #else
  2760. /* WTZ: Do what Adrian did in cas_set_multicast. Doing
  2761. * a writel does not seem to be necessary because Cassini
  2762. * seems to preserve the configuration when we do the reset.
  2763. * If the chip is in trouble, though, it is not clear if we
  2764. * can really count on this behavior. cas_set_multicast uses
  2765. * spin_lock_irqsave, but we are called only in cas_init_hw and
  2766. * cas_init_hw is protected by cas_lock_all, which calls
  2767. * spin_lock_irq (so it doesn't need to save the flags, and
  2768. * we should be OK for the writel, as that is the only
  2769. * difference).
  2770. */
  2771. cp->mac_rx_cfg = rxcfg = cas_setup_multicast(cp);
  2772. writel(rxcfg, cp->regs + REG_MAC_RX_CFG);
  2773. #endif
  2774. spin_lock(&cp->stat_lock[N_TX_RINGS]);
  2775. cas_clear_mac_err(cp);
  2776. spin_unlock(&cp->stat_lock[N_TX_RINGS]);
  2777. /* Setup MAC interrupts. We want to get all of the interesting
  2778. * counter expiration events, but we do not want to hear about
  2779. * normal rx/tx as the DMA engine tells us that.
  2780. */
  2781. writel(MAC_TX_FRAME_XMIT, cp->regs + REG_MAC_TX_MASK);
  2782. writel(MAC_RX_FRAME_RECV, cp->regs + REG_MAC_RX_MASK);
  2783. /* Don't enable even the PAUSE interrupts for now, we
  2784. * make no use of those events other than to record them.
  2785. */
  2786. writel(0xffffffff, cp->regs + REG_MAC_CTRL_MASK);
  2787. }
  2788. /* Must be invoked under cp->lock. */
  2789. static void cas_init_pause_thresholds(struct cas *cp)
  2790. {
  2791. /* Calculate pause thresholds. Setting the OFF threshold to the
  2792. * full RX fifo size effectively disables PAUSE generation
  2793. */
  2794. if (cp->rx_fifo_size <= (2 * 1024)) {
  2795. cp->rx_pause_off = cp->rx_pause_on = cp->rx_fifo_size;
  2796. } else {
  2797. int max_frame = (cp->dev->mtu + ETH_HLEN + 4 + 4 + 64) & ~63;
  2798. if (max_frame * 3 > cp->rx_fifo_size) {
  2799. cp->rx_pause_off = 7104;
  2800. cp->rx_pause_on = 960;
  2801. } else {
  2802. int off = (cp->rx_fifo_size - (max_frame * 2));
  2803. int on = off - max_frame;
  2804. cp->rx_pause_off = off;
  2805. cp->rx_pause_on = on;
  2806. }
  2807. }
  2808. }
  2809. static int cas_vpd_match(const void __iomem *p, const char *str)
  2810. {
  2811. int len = strlen(str) + 1;
  2812. int i;
  2813. for (i = 0; i < len; i++) {
  2814. if (readb(p + i) != str[i])
  2815. return 0;
  2816. }
  2817. return 1;
  2818. }
  2819. /* get the mac address by reading the vpd information in the rom.
  2820. * also get the phy type and determine if there's an entropy generator.
  2821. * NOTE: this is a bit convoluted for the following reasons:
  2822. * 1) vpd info has order-dependent mac addresses for multinic cards
  2823. * 2) the only way to determine the nic order is to use the slot
  2824. * number.
  2825. * 3) fiber cards don't have bridges, so their slot numbers don't
  2826. * mean anything.
  2827. * 4) we don't actually know we have a fiber card until after
  2828. * the mac addresses are parsed.
  2829. */
  2830. static int cas_get_vpd_info(struct cas *cp, unsigned char *dev_addr,
  2831. const int offset)
  2832. {
  2833. void __iomem *p = cp->regs + REG_EXPANSION_ROM_RUN_START;
  2834. void __iomem *base, *kstart;
  2835. int i, len;
  2836. int found = 0;
  2837. #define VPD_FOUND_MAC 0x01
  2838. #define VPD_FOUND_PHY 0x02
  2839. int phy_type = CAS_PHY_MII_MDIO0; /* default phy type */
  2840. int mac_off = 0;
  2841. /* give us access to the PROM */
  2842. writel(BIM_LOCAL_DEV_PROM | BIM_LOCAL_DEV_PAD,
  2843. cp->regs + REG_BIM_LOCAL_DEV_EN);
  2844. /* check for an expansion rom */
  2845. if (readb(p) != 0x55 || readb(p + 1) != 0xaa)
  2846. goto use_random_mac_addr;
  2847. /* search for beginning of vpd */
  2848. base = NULL;
  2849. for (i = 2; i < EXPANSION_ROM_SIZE; i++) {
  2850. /* check for PCIR */
  2851. if ((readb(p + i + 0) == 0x50) &&
  2852. (readb(p + i + 1) == 0x43) &&
  2853. (readb(p + i + 2) == 0x49) &&
  2854. (readb(p + i + 3) == 0x52)) {
  2855. base = p + (readb(p + i + 8) |
  2856. (readb(p + i + 9) << 8));
  2857. break;
  2858. }
  2859. }
  2860. if (!base || (readb(base) != 0x82))
  2861. goto use_random_mac_addr;
  2862. i = (readb(base + 1) | (readb(base + 2) << 8)) + 3;
  2863. while (i < EXPANSION_ROM_SIZE) {
  2864. if (readb(base + i) != 0x90) /* no vpd found */
  2865. goto use_random_mac_addr;
  2866. /* found a vpd field */
  2867. len = readb(base + i + 1) | (readb(base + i + 2) << 8);
  2868. /* extract keywords */
  2869. kstart = base + i + 3;
  2870. p = kstart;
  2871. while ((p - kstart) < len) {
  2872. int klen = readb(p + 2);
  2873. int j;
  2874. char type;
  2875. p += 3;
  2876. /* look for the following things:
  2877. * -- correct length == 29
  2878. * 3 (type) + 2 (size) +
  2879. * 18 (strlen("local-mac-address") + 1) +
  2880. * 6 (mac addr)
  2881. * -- VPD Instance 'I'
  2882. * -- VPD Type Bytes 'B'
  2883. * -- VPD data length == 6
  2884. * -- property string == local-mac-address
  2885. *
  2886. * -- correct length == 24
  2887. * 3 (type) + 2 (size) +
  2888. * 12 (strlen("entropy-dev") + 1) +
  2889. * 7 (strlen("vms110") + 1)
  2890. * -- VPD Instance 'I'
  2891. * -- VPD Type String 'B'
  2892. * -- VPD data length == 7
  2893. * -- property string == entropy-dev
  2894. *
  2895. * -- correct length == 18
  2896. * 3 (type) + 2 (size) +
  2897. * 9 (strlen("phy-type") + 1) +
  2898. * 4 (strlen("pcs") + 1)
  2899. * -- VPD Instance 'I'
  2900. * -- VPD Type String 'S'
  2901. * -- VPD data length == 4
  2902. * -- property string == phy-type
  2903. *
  2904. * -- correct length == 23
  2905. * 3 (type) + 2 (size) +
  2906. * 14 (strlen("phy-interface") + 1) +
  2907. * 4 (strlen("pcs") + 1)
  2908. * -- VPD Instance 'I'
  2909. * -- VPD Type String 'S'
  2910. * -- VPD data length == 4
  2911. * -- property string == phy-interface
  2912. */
  2913. if (readb(p) != 'I')
  2914. goto next;
  2915. /* finally, check string and length */
  2916. type = readb(p + 3);
  2917. if (type == 'B') {
  2918. if ((klen == 29) && readb(p + 4) == 6 &&
  2919. cas_vpd_match(p + 5,
  2920. "local-mac-address")) {
  2921. if (mac_off++ > offset)
  2922. goto next;
  2923. /* set mac address */
  2924. for (j = 0; j < 6; j++)
  2925. dev_addr[j] =
  2926. readb(p + 23 + j);
  2927. goto found_mac;
  2928. }
  2929. }
  2930. if (type != 'S')
  2931. goto next;
  2932. #ifdef USE_ENTROPY_DEV
  2933. if ((klen == 24) &&
  2934. cas_vpd_match(p + 5, "entropy-dev") &&
  2935. cas_vpd_match(p + 17, "vms110")) {
  2936. cp->cas_flags |= CAS_FLAG_ENTROPY_DEV;
  2937. goto next;
  2938. }
  2939. #endif
  2940. if (found & VPD_FOUND_PHY)
  2941. goto next;
  2942. if ((klen == 18) && readb(p + 4) == 4 &&
  2943. cas_vpd_match(p + 5, "phy-type")) {
  2944. if (cas_vpd_match(p + 14, "pcs")) {
  2945. phy_type = CAS_PHY_SERDES;
  2946. goto found_phy;
  2947. }
  2948. }
  2949. if ((klen == 23) && readb(p + 4) == 4 &&
  2950. cas_vpd_match(p + 5, "phy-interface")) {
  2951. if (cas_vpd_match(p + 19, "pcs")) {
  2952. phy_type = CAS_PHY_SERDES;
  2953. goto found_phy;
  2954. }
  2955. }
  2956. found_mac:
  2957. found |= VPD_FOUND_MAC;
  2958. goto next;
  2959. found_phy:
  2960. found |= VPD_FOUND_PHY;
  2961. next:
  2962. p += klen;
  2963. }
  2964. i += len + 3;
  2965. }
  2966. use_random_mac_addr:
  2967. if (found & VPD_FOUND_MAC)
  2968. goto done;
  2969. /* Sun MAC prefix then 3 random bytes. */
  2970. printk(PFX "MAC address not found in ROM VPD\n");
  2971. dev_addr[0] = 0x08;
  2972. dev_addr[1] = 0x00;
  2973. dev_addr[2] = 0x20;
  2974. get_random_bytes(dev_addr + 3, 3);
  2975. done:
  2976. writel(0, cp->regs + REG_BIM_LOCAL_DEV_EN);
  2977. return phy_type;
  2978. }
  2979. /* check pci invariants */
  2980. static void cas_check_pci_invariants(struct cas *cp)
  2981. {
  2982. struct pci_dev *pdev = cp->pdev;
  2983. cp->cas_flags = 0;
  2984. if ((pdev->vendor == PCI_VENDOR_ID_SUN) &&
  2985. (pdev->device == PCI_DEVICE_ID_SUN_CASSINI)) {
  2986. if (pdev->revision >= CAS_ID_REVPLUS)
  2987. cp->cas_flags |= CAS_FLAG_REG_PLUS;
  2988. if (pdev->revision < CAS_ID_REVPLUS02u)
  2989. cp->cas_flags |= CAS_FLAG_TARGET_ABORT;
  2990. /* Original Cassini supports HW CSUM, but it's not
  2991. * enabled by default as it can trigger TX hangs.
  2992. */
  2993. if (pdev->revision < CAS_ID_REV2)
  2994. cp->cas_flags |= CAS_FLAG_NO_HW_CSUM;
  2995. } else {
  2996. /* Only sun has original cassini chips. */
  2997. cp->cas_flags |= CAS_FLAG_REG_PLUS;
  2998. /* We use a flag because the same phy might be externally
  2999. * connected.
  3000. */
  3001. if ((pdev->vendor == PCI_VENDOR_ID_NS) &&
  3002. (pdev->device == PCI_DEVICE_ID_NS_SATURN))
  3003. cp->cas_flags |= CAS_FLAG_SATURN;
  3004. }
  3005. }
  3006. static int cas_check_invariants(struct cas *cp)
  3007. {
  3008. struct pci_dev *pdev = cp->pdev;
  3009. u32 cfg;
  3010. int i;
  3011. /* get page size for rx buffers. */
  3012. cp->page_order = 0;
  3013. #ifdef USE_PAGE_ORDER
  3014. if (PAGE_SHIFT < CAS_JUMBO_PAGE_SHIFT) {
  3015. /* see if we can allocate larger pages */
  3016. struct page *page = alloc_pages(GFP_ATOMIC,
  3017. CAS_JUMBO_PAGE_SHIFT -
  3018. PAGE_SHIFT);
  3019. if (page) {
  3020. __free_pages(page, CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT);
  3021. cp->page_order = CAS_JUMBO_PAGE_SHIFT - PAGE_SHIFT;
  3022. } else {
  3023. printk(PFX "MTU limited to %d bytes\n", CAS_MAX_MTU);
  3024. }
  3025. }
  3026. #endif
  3027. cp->page_size = (PAGE_SIZE << cp->page_order);
  3028. /* Fetch the FIFO configurations. */
  3029. cp->tx_fifo_size = readl(cp->regs + REG_TX_FIFO_SIZE) * 64;
  3030. cp->rx_fifo_size = RX_FIFO_SIZE;
  3031. /* finish phy determination. MDIO1 takes precedence over MDIO0 if
  3032. * they're both connected.
  3033. */
  3034. cp->phy_type = cas_get_vpd_info(cp, cp->dev->dev_addr,
  3035. PCI_SLOT(pdev->devfn));
  3036. if (cp->phy_type & CAS_PHY_SERDES) {
  3037. cp->cas_flags |= CAS_FLAG_1000MB_CAP;
  3038. return 0; /* no more checking needed */
  3039. }
  3040. /* MII */
  3041. cfg = readl(cp->regs + REG_MIF_CFG);
  3042. if (cfg & MIF_CFG_MDIO_1) {
  3043. cp->phy_type = CAS_PHY_MII_MDIO1;
  3044. } else if (cfg & MIF_CFG_MDIO_0) {
  3045. cp->phy_type = CAS_PHY_MII_MDIO0;
  3046. }
  3047. cas_mif_poll(cp, 0);
  3048. writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
  3049. for (i = 0; i < 32; i++) {
  3050. u32 phy_id;
  3051. int j;
  3052. for (j = 0; j < 3; j++) {
  3053. cp->phy_addr = i;
  3054. phy_id = cas_phy_read(cp, MII_PHYSID1) << 16;
  3055. phy_id |= cas_phy_read(cp, MII_PHYSID2);
  3056. if (phy_id && (phy_id != 0xFFFFFFFF)) {
  3057. cp->phy_id = phy_id;
  3058. goto done;
  3059. }
  3060. }
  3061. }
  3062. printk(KERN_ERR PFX "MII phy did not respond [%08x]\n",
  3063. readl(cp->regs + REG_MIF_STATE_MACHINE));
  3064. return -1;
  3065. done:
  3066. /* see if we can do gigabit */
  3067. cfg = cas_phy_read(cp, MII_BMSR);
  3068. if ((cfg & CAS_BMSR_1000_EXTEND) &&
  3069. cas_phy_read(cp, CAS_MII_1000_EXTEND))
  3070. cp->cas_flags |= CAS_FLAG_1000MB_CAP;
  3071. return 0;
  3072. }
  3073. /* Must be invoked under cp->lock. */
  3074. static inline void cas_start_dma(struct cas *cp)
  3075. {
  3076. int i;
  3077. u32 val;
  3078. int txfailed = 0;
  3079. /* enable dma */
  3080. val = readl(cp->regs + REG_TX_CFG) | TX_CFG_DMA_EN;
  3081. writel(val, cp->regs + REG_TX_CFG);
  3082. val = readl(cp->regs + REG_RX_CFG) | RX_CFG_DMA_EN;
  3083. writel(val, cp->regs + REG_RX_CFG);
  3084. /* enable the mac */
  3085. val = readl(cp->regs + REG_MAC_TX_CFG) | MAC_TX_CFG_EN;
  3086. writel(val, cp->regs + REG_MAC_TX_CFG);
  3087. val = readl(cp->regs + REG_MAC_RX_CFG) | MAC_RX_CFG_EN;
  3088. writel(val, cp->regs + REG_MAC_RX_CFG);
  3089. i = STOP_TRIES;
  3090. while (i-- > 0) {
  3091. val = readl(cp->regs + REG_MAC_TX_CFG);
  3092. if ((val & MAC_TX_CFG_EN))
  3093. break;
  3094. udelay(10);
  3095. }
  3096. if (i < 0) txfailed = 1;
  3097. i = STOP_TRIES;
  3098. while (i-- > 0) {
  3099. val = readl(cp->regs + REG_MAC_RX_CFG);
  3100. if ((val & MAC_RX_CFG_EN)) {
  3101. if (txfailed) {
  3102. printk(KERN_ERR
  3103. "%s: enabling mac failed [tx:%08x:%08x].\n",
  3104. cp->dev->name,
  3105. readl(cp->regs + REG_MIF_STATE_MACHINE),
  3106. readl(cp->regs + REG_MAC_STATE_MACHINE));
  3107. }
  3108. goto enable_rx_done;
  3109. }
  3110. udelay(10);
  3111. }
  3112. printk(KERN_ERR "%s: enabling mac failed [%s:%08x:%08x].\n",
  3113. cp->dev->name,
  3114. (txfailed? "tx,rx":"rx"),
  3115. readl(cp->regs + REG_MIF_STATE_MACHINE),
  3116. readl(cp->regs + REG_MAC_STATE_MACHINE));
  3117. enable_rx_done:
  3118. cas_unmask_intr(cp); /* enable interrupts */
  3119. writel(RX_DESC_RINGN_SIZE(0) - 4, cp->regs + REG_RX_KICK);
  3120. writel(0, cp->regs + REG_RX_COMP_TAIL);
  3121. if (cp->cas_flags & CAS_FLAG_REG_PLUS) {
  3122. if (N_RX_DESC_RINGS > 1)
  3123. writel(RX_DESC_RINGN_SIZE(1) - 4,
  3124. cp->regs + REG_PLUS_RX_KICK1);
  3125. for (i = 1; i < N_RX_COMP_RINGS; i++)
  3126. writel(0, cp->regs + REG_PLUS_RX_COMPN_TAIL(i));
  3127. }
  3128. }
  3129. /* Must be invoked under cp->lock. */
  3130. static void cas_read_pcs_link_mode(struct cas *cp, int *fd, int *spd,
  3131. int *pause)
  3132. {
  3133. u32 val = readl(cp->regs + REG_PCS_MII_LPA);
  3134. *fd = (val & PCS_MII_LPA_FD) ? 1 : 0;
  3135. *pause = (val & PCS_MII_LPA_SYM_PAUSE) ? 0x01 : 0x00;
  3136. if (val & PCS_MII_LPA_ASYM_PAUSE)
  3137. *pause |= 0x10;
  3138. *spd = 1000;
  3139. }
  3140. /* Must be invoked under cp->lock. */
  3141. static void cas_read_mii_link_mode(struct cas *cp, int *fd, int *spd,
  3142. int *pause)
  3143. {
  3144. u32 val;
  3145. *fd = 0;
  3146. *spd = 10;
  3147. *pause = 0;
  3148. /* use GMII registers */
  3149. val = cas_phy_read(cp, MII_LPA);
  3150. if (val & CAS_LPA_PAUSE)
  3151. *pause = 0x01;
  3152. if (val & CAS_LPA_ASYM_PAUSE)
  3153. *pause |= 0x10;
  3154. if (val & LPA_DUPLEX)
  3155. *fd = 1;
  3156. if (val & LPA_100)
  3157. *spd = 100;
  3158. if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
  3159. val = cas_phy_read(cp, CAS_MII_1000_STATUS);
  3160. if (val & (CAS_LPA_1000FULL | CAS_LPA_1000HALF))
  3161. *spd = 1000;
  3162. if (val & CAS_LPA_1000FULL)
  3163. *fd = 1;
  3164. }
  3165. }
  3166. /* A link-up condition has occurred, initialize and enable the
  3167. * rest of the chip.
  3168. *
  3169. * Must be invoked under cp->lock.
  3170. */
  3171. static void cas_set_link_modes(struct cas *cp)
  3172. {
  3173. u32 val;
  3174. int full_duplex, speed, pause;
  3175. full_duplex = 0;
  3176. speed = 10;
  3177. pause = 0;
  3178. if (CAS_PHY_MII(cp->phy_type)) {
  3179. cas_mif_poll(cp, 0);
  3180. val = cas_phy_read(cp, MII_BMCR);
  3181. if (val & BMCR_ANENABLE) {
  3182. cas_read_mii_link_mode(cp, &full_duplex, &speed,
  3183. &pause);
  3184. } else {
  3185. if (val & BMCR_FULLDPLX)
  3186. full_duplex = 1;
  3187. if (val & BMCR_SPEED100)
  3188. speed = 100;
  3189. else if (val & CAS_BMCR_SPEED1000)
  3190. speed = (cp->cas_flags & CAS_FLAG_1000MB_CAP) ?
  3191. 1000 : 100;
  3192. }
  3193. cas_mif_poll(cp, 1);
  3194. } else {
  3195. val = readl(cp->regs + REG_PCS_MII_CTRL);
  3196. cas_read_pcs_link_mode(cp, &full_duplex, &speed, &pause);
  3197. if ((val & PCS_MII_AUTONEG_EN) == 0) {
  3198. if (val & PCS_MII_CTRL_DUPLEX)
  3199. full_duplex = 1;
  3200. }
  3201. }
  3202. if (netif_msg_link(cp))
  3203. printk(KERN_INFO "%s: Link up at %d Mbps, %s-duplex.\n",
  3204. cp->dev->name, speed, (full_duplex ? "full" : "half"));
  3205. val = MAC_XIF_TX_MII_OUTPUT_EN | MAC_XIF_LINK_LED;
  3206. if (CAS_PHY_MII(cp->phy_type)) {
  3207. val |= MAC_XIF_MII_BUFFER_OUTPUT_EN;
  3208. if (!full_duplex)
  3209. val |= MAC_XIF_DISABLE_ECHO;
  3210. }
  3211. if (full_duplex)
  3212. val |= MAC_XIF_FDPLX_LED;
  3213. if (speed == 1000)
  3214. val |= MAC_XIF_GMII_MODE;
  3215. writel(val, cp->regs + REG_MAC_XIF_CFG);
  3216. /* deal with carrier and collision detect. */
  3217. val = MAC_TX_CFG_IPG_EN;
  3218. if (full_duplex) {
  3219. val |= MAC_TX_CFG_IGNORE_CARRIER;
  3220. val |= MAC_TX_CFG_IGNORE_COLL;
  3221. } else {
  3222. #ifndef USE_CSMA_CD_PROTO
  3223. val |= MAC_TX_CFG_NEVER_GIVE_UP_EN;
  3224. val |= MAC_TX_CFG_NEVER_GIVE_UP_LIM;
  3225. #endif
  3226. }
  3227. /* val now set up for REG_MAC_TX_CFG */
  3228. /* If gigabit and half-duplex, enable carrier extension
  3229. * mode. increase slot time to 512 bytes as well.
  3230. * else, disable it and make sure slot time is 64 bytes.
  3231. * also activate checksum bug workaround
  3232. */
  3233. if ((speed == 1000) && !full_duplex) {
  3234. writel(val | MAC_TX_CFG_CARRIER_EXTEND,
  3235. cp->regs + REG_MAC_TX_CFG);
  3236. val = readl(cp->regs + REG_MAC_RX_CFG);
  3237. val &= ~MAC_RX_CFG_STRIP_FCS; /* checksum workaround */
  3238. writel(val | MAC_RX_CFG_CARRIER_EXTEND,
  3239. cp->regs + REG_MAC_RX_CFG);
  3240. writel(0x200, cp->regs + REG_MAC_SLOT_TIME);
  3241. cp->crc_size = 4;
  3242. /* minimum size gigabit frame at half duplex */
  3243. cp->min_frame_size = CAS_1000MB_MIN_FRAME;
  3244. } else {
  3245. writel(val, cp->regs + REG_MAC_TX_CFG);
  3246. /* checksum bug workaround. don't strip FCS when in
  3247. * half-duplex mode
  3248. */
  3249. val = readl(cp->regs + REG_MAC_RX_CFG);
  3250. if (full_duplex) {
  3251. val |= MAC_RX_CFG_STRIP_FCS;
  3252. cp->crc_size = 0;
  3253. cp->min_frame_size = CAS_MIN_MTU;
  3254. } else {
  3255. val &= ~MAC_RX_CFG_STRIP_FCS;
  3256. cp->crc_size = 4;
  3257. cp->min_frame_size = CAS_MIN_FRAME;
  3258. }
  3259. writel(val & ~MAC_RX_CFG_CARRIER_EXTEND,
  3260. cp->regs + REG_MAC_RX_CFG);
  3261. writel(0x40, cp->regs + REG_MAC_SLOT_TIME);
  3262. }
  3263. if (netif_msg_link(cp)) {
  3264. if (pause & 0x01) {
  3265. printk(KERN_INFO "%s: Pause is enabled "
  3266. "(rxfifo: %d off: %d on: %d)\n",
  3267. cp->dev->name,
  3268. cp->rx_fifo_size,
  3269. cp->rx_pause_off,
  3270. cp->rx_pause_on);
  3271. } else if (pause & 0x10) {
  3272. printk(KERN_INFO "%s: TX pause enabled\n",
  3273. cp->dev->name);
  3274. } else {
  3275. printk(KERN_INFO "%s: Pause is disabled\n",
  3276. cp->dev->name);
  3277. }
  3278. }
  3279. val = readl(cp->regs + REG_MAC_CTRL_CFG);
  3280. val &= ~(MAC_CTRL_CFG_SEND_PAUSE_EN | MAC_CTRL_CFG_RECV_PAUSE_EN);
  3281. if (pause) { /* symmetric or asymmetric pause */
  3282. val |= MAC_CTRL_CFG_SEND_PAUSE_EN;
  3283. if (pause & 0x01) { /* symmetric pause */
  3284. val |= MAC_CTRL_CFG_RECV_PAUSE_EN;
  3285. }
  3286. }
  3287. writel(val, cp->regs + REG_MAC_CTRL_CFG);
  3288. cas_start_dma(cp);
  3289. }
  3290. /* Must be invoked under cp->lock. */
  3291. static void cas_init_hw(struct cas *cp, int restart_link)
  3292. {
  3293. if (restart_link)
  3294. cas_phy_init(cp);
  3295. cas_init_pause_thresholds(cp);
  3296. cas_init_mac(cp);
  3297. cas_init_dma(cp);
  3298. if (restart_link) {
  3299. /* Default aneg parameters */
  3300. cp->timer_ticks = 0;
  3301. cas_begin_auto_negotiation(cp, NULL);
  3302. } else if (cp->lstate == link_up) {
  3303. cas_set_link_modes(cp);
  3304. netif_carrier_on(cp->dev);
  3305. }
  3306. }
  3307. /* Must be invoked under cp->lock. on earlier cassini boards,
  3308. * SOFT_0 is tied to PCI reset. we use this to force a pci reset,
  3309. * let it settle out, and then restore pci state.
  3310. */
  3311. static void cas_hard_reset(struct cas *cp)
  3312. {
  3313. writel(BIM_LOCAL_DEV_SOFT_0, cp->regs + REG_BIM_LOCAL_DEV_EN);
  3314. udelay(20);
  3315. pci_restore_state(cp->pdev);
  3316. }
  3317. static void cas_global_reset(struct cas *cp, int blkflag)
  3318. {
  3319. int limit;
  3320. /* issue a global reset. don't use RSTOUT. */
  3321. if (blkflag && !CAS_PHY_MII(cp->phy_type)) {
  3322. /* For PCS, when the blkflag is set, we should set the
  3323. * SW_REST_BLOCK_PCS_SLINK bit to prevent the results of
  3324. * the last autonegotiation from being cleared. We'll
  3325. * need some special handling if the chip is set into a
  3326. * loopback mode.
  3327. */
  3328. writel((SW_RESET_TX | SW_RESET_RX | SW_RESET_BLOCK_PCS_SLINK),
  3329. cp->regs + REG_SW_RESET);
  3330. } else {
  3331. writel(SW_RESET_TX | SW_RESET_RX, cp->regs + REG_SW_RESET);
  3332. }
  3333. /* need to wait at least 3ms before polling register */
  3334. mdelay(3);
  3335. limit = STOP_TRIES;
  3336. while (limit-- > 0) {
  3337. u32 val = readl(cp->regs + REG_SW_RESET);
  3338. if ((val & (SW_RESET_TX | SW_RESET_RX)) == 0)
  3339. goto done;
  3340. udelay(10);
  3341. }
  3342. printk(KERN_ERR "%s: sw reset failed.\n", cp->dev->name);
  3343. done:
  3344. /* enable various BIM interrupts */
  3345. writel(BIM_CFG_DPAR_INTR_ENABLE | BIM_CFG_RMA_INTR_ENABLE |
  3346. BIM_CFG_RTA_INTR_ENABLE, cp->regs + REG_BIM_CFG);
  3347. /* clear out pci error status mask for handled errors.
  3348. * we don't deal with DMA counter overflows as they happen
  3349. * all the time.
  3350. */
  3351. writel(0xFFFFFFFFU & ~(PCI_ERR_BADACK | PCI_ERR_DTRTO |
  3352. PCI_ERR_OTHER | PCI_ERR_BIM_DMA_WRITE |
  3353. PCI_ERR_BIM_DMA_READ), cp->regs +
  3354. REG_PCI_ERR_STATUS_MASK);
  3355. /* set up for MII by default to address mac rx reset timeout
  3356. * issue
  3357. */
  3358. writel(PCS_DATAPATH_MODE_MII, cp->regs + REG_PCS_DATAPATH_MODE);
  3359. }
  3360. static void cas_reset(struct cas *cp, int blkflag)
  3361. {
  3362. u32 val;
  3363. cas_mask_intr(cp);
  3364. cas_global_reset(cp, blkflag);
  3365. cas_mac_reset(cp);
  3366. cas_entropy_reset(cp);
  3367. /* disable dma engines. */
  3368. val = readl(cp->regs + REG_TX_CFG);
  3369. val &= ~TX_CFG_DMA_EN;
  3370. writel(val, cp->regs + REG_TX_CFG);
  3371. val = readl(cp->regs + REG_RX_CFG);
  3372. val &= ~RX_CFG_DMA_EN;
  3373. writel(val, cp->regs + REG_RX_CFG);
  3374. /* program header parser */
  3375. if ((cp->cas_flags & CAS_FLAG_TARGET_ABORT) ||
  3376. (CAS_HP_ALT_FIRMWARE == cas_prog_null)) {
  3377. cas_load_firmware(cp, CAS_HP_FIRMWARE);
  3378. } else {
  3379. cas_load_firmware(cp, CAS_HP_ALT_FIRMWARE);
  3380. }
  3381. /* clear out error registers */
  3382. spin_lock(&cp->stat_lock[N_TX_RINGS]);
  3383. cas_clear_mac_err(cp);
  3384. spin_unlock(&cp->stat_lock[N_TX_RINGS]);
  3385. }
  3386. /* Shut down the chip, must be called with pm_mutex held. */
  3387. static void cas_shutdown(struct cas *cp)
  3388. {
  3389. unsigned long flags;
  3390. /* Make us not-running to avoid timers respawning */
  3391. cp->hw_running = 0;
  3392. del_timer_sync(&cp->link_timer);
  3393. /* Stop the reset task */
  3394. #if 0
  3395. while (atomic_read(&cp->reset_task_pending_mtu) ||
  3396. atomic_read(&cp->reset_task_pending_spare) ||
  3397. atomic_read(&cp->reset_task_pending_all))
  3398. schedule();
  3399. #else
  3400. while (atomic_read(&cp->reset_task_pending))
  3401. schedule();
  3402. #endif
  3403. /* Actually stop the chip */
  3404. cas_lock_all_save(cp, flags);
  3405. cas_reset(cp, 0);
  3406. if (cp->cas_flags & CAS_FLAG_SATURN)
  3407. cas_phy_powerdown(cp);
  3408. cas_unlock_all_restore(cp, flags);
  3409. }
  3410. static int cas_change_mtu(struct net_device *dev, int new_mtu)
  3411. {
  3412. struct cas *cp = netdev_priv(dev);
  3413. if (new_mtu < CAS_MIN_MTU || new_mtu > CAS_MAX_MTU)
  3414. return -EINVAL;
  3415. dev->mtu = new_mtu;
  3416. if (!netif_running(dev) || !netif_device_present(dev))
  3417. return 0;
  3418. /* let the reset task handle it */
  3419. #if 1
  3420. atomic_inc(&cp->reset_task_pending);
  3421. if ((cp->phy_type & CAS_PHY_SERDES)) {
  3422. atomic_inc(&cp->reset_task_pending_all);
  3423. } else {
  3424. atomic_inc(&cp->reset_task_pending_mtu);
  3425. }
  3426. schedule_work(&cp->reset_task);
  3427. #else
  3428. atomic_set(&cp->reset_task_pending, (cp->phy_type & CAS_PHY_SERDES) ?
  3429. CAS_RESET_ALL : CAS_RESET_MTU);
  3430. printk(KERN_ERR "reset called in cas_change_mtu\n");
  3431. schedule_work(&cp->reset_task);
  3432. #endif
  3433. flush_scheduled_work();
  3434. return 0;
  3435. }
  3436. static void cas_clean_txd(struct cas *cp, int ring)
  3437. {
  3438. struct cas_tx_desc *txd = cp->init_txds[ring];
  3439. struct sk_buff *skb, **skbs = cp->tx_skbs[ring];
  3440. u64 daddr, dlen;
  3441. int i, size;
  3442. size = TX_DESC_RINGN_SIZE(ring);
  3443. for (i = 0; i < size; i++) {
  3444. int frag;
  3445. if (skbs[i] == NULL)
  3446. continue;
  3447. skb = skbs[i];
  3448. skbs[i] = NULL;
  3449. for (frag = 0; frag <= skb_shinfo(skb)->nr_frags; frag++) {
  3450. int ent = i & (size - 1);
  3451. /* first buffer is never a tiny buffer and so
  3452. * needs to be unmapped.
  3453. */
  3454. daddr = le64_to_cpu(txd[ent].buffer);
  3455. dlen = CAS_VAL(TX_DESC_BUFLEN,
  3456. le64_to_cpu(txd[ent].control));
  3457. pci_unmap_page(cp->pdev, daddr, dlen,
  3458. PCI_DMA_TODEVICE);
  3459. if (frag != skb_shinfo(skb)->nr_frags) {
  3460. i++;
  3461. /* next buffer might by a tiny buffer.
  3462. * skip past it.
  3463. */
  3464. ent = i & (size - 1);
  3465. if (cp->tx_tiny_use[ring][ent].used)
  3466. i++;
  3467. }
  3468. }
  3469. dev_kfree_skb_any(skb);
  3470. }
  3471. /* zero out tiny buf usage */
  3472. memset(cp->tx_tiny_use[ring], 0, size*sizeof(*cp->tx_tiny_use[ring]));
  3473. }
  3474. /* freed on close */
  3475. static inline void cas_free_rx_desc(struct cas *cp, int ring)
  3476. {
  3477. cas_page_t **page = cp->rx_pages[ring];
  3478. int i, size;
  3479. size = RX_DESC_RINGN_SIZE(ring);
  3480. for (i = 0; i < size; i++) {
  3481. if (page[i]) {
  3482. cas_page_free(cp, page[i]);
  3483. page[i] = NULL;
  3484. }
  3485. }
  3486. }
  3487. static void cas_free_rxds(struct cas *cp)
  3488. {
  3489. int i;
  3490. for (i = 0; i < N_RX_DESC_RINGS; i++)
  3491. cas_free_rx_desc(cp, i);
  3492. }
  3493. /* Must be invoked under cp->lock. */
  3494. static void cas_clean_rings(struct cas *cp)
  3495. {
  3496. int i;
  3497. /* need to clean all tx rings */
  3498. memset(cp->tx_old, 0, sizeof(*cp->tx_old)*N_TX_RINGS);
  3499. memset(cp->tx_new, 0, sizeof(*cp->tx_new)*N_TX_RINGS);
  3500. for (i = 0; i < N_TX_RINGS; i++)
  3501. cas_clean_txd(cp, i);
  3502. /* zero out init block */
  3503. memset(cp->init_block, 0, sizeof(struct cas_init_block));
  3504. cas_clean_rxds(cp);
  3505. cas_clean_rxcs(cp);
  3506. }
  3507. /* allocated on open */
  3508. static inline int cas_alloc_rx_desc(struct cas *cp, int ring)
  3509. {
  3510. cas_page_t **page = cp->rx_pages[ring];
  3511. int size, i = 0;
  3512. size = RX_DESC_RINGN_SIZE(ring);
  3513. for (i = 0; i < size; i++) {
  3514. if ((page[i] = cas_page_alloc(cp, GFP_KERNEL)) == NULL)
  3515. return -1;
  3516. }
  3517. return 0;
  3518. }
  3519. static int cas_alloc_rxds(struct cas *cp)
  3520. {
  3521. int i;
  3522. for (i = 0; i < N_RX_DESC_RINGS; i++) {
  3523. if (cas_alloc_rx_desc(cp, i) < 0) {
  3524. cas_free_rxds(cp);
  3525. return -1;
  3526. }
  3527. }
  3528. return 0;
  3529. }
  3530. static void cas_reset_task(struct work_struct *work)
  3531. {
  3532. struct cas *cp = container_of(work, struct cas, reset_task);
  3533. #if 0
  3534. int pending = atomic_read(&cp->reset_task_pending);
  3535. #else
  3536. int pending_all = atomic_read(&cp->reset_task_pending_all);
  3537. int pending_spare = atomic_read(&cp->reset_task_pending_spare);
  3538. int pending_mtu = atomic_read(&cp->reset_task_pending_mtu);
  3539. if (pending_all == 0 && pending_spare == 0 && pending_mtu == 0) {
  3540. /* We can have more tasks scheduled than actually
  3541. * needed.
  3542. */
  3543. atomic_dec(&cp->reset_task_pending);
  3544. return;
  3545. }
  3546. #endif
  3547. /* The link went down, we reset the ring, but keep
  3548. * DMA stopped. Use this function for reset
  3549. * on error as well.
  3550. */
  3551. if (cp->hw_running) {
  3552. unsigned long flags;
  3553. /* Make sure we don't get interrupts or tx packets */
  3554. netif_device_detach(cp->dev);
  3555. cas_lock_all_save(cp, flags);
  3556. if (cp->opened) {
  3557. /* We call cas_spare_recover when we call cas_open.
  3558. * but we do not initialize the lists cas_spare_recover
  3559. * uses until cas_open is called.
  3560. */
  3561. cas_spare_recover(cp, GFP_ATOMIC);
  3562. }
  3563. #if 1
  3564. /* test => only pending_spare set */
  3565. if (!pending_all && !pending_mtu)
  3566. goto done;
  3567. #else
  3568. if (pending == CAS_RESET_SPARE)
  3569. goto done;
  3570. #endif
  3571. /* when pending == CAS_RESET_ALL, the following
  3572. * call to cas_init_hw will restart auto negotiation.
  3573. * Setting the second argument of cas_reset to
  3574. * !(pending == CAS_RESET_ALL) will set this argument
  3575. * to 1 (avoiding reinitializing the PHY for the normal
  3576. * PCS case) when auto negotiation is not restarted.
  3577. */
  3578. #if 1
  3579. cas_reset(cp, !(pending_all > 0));
  3580. if (cp->opened)
  3581. cas_clean_rings(cp);
  3582. cas_init_hw(cp, (pending_all > 0));
  3583. #else
  3584. cas_reset(cp, !(pending == CAS_RESET_ALL));
  3585. if (cp->opened)
  3586. cas_clean_rings(cp);
  3587. cas_init_hw(cp, pending == CAS_RESET_ALL);
  3588. #endif
  3589. done:
  3590. cas_unlock_all_restore(cp, flags);
  3591. netif_device_attach(cp->dev);
  3592. }
  3593. #if 1
  3594. atomic_sub(pending_all, &cp->reset_task_pending_all);
  3595. atomic_sub(pending_spare, &cp->reset_task_pending_spare);
  3596. atomic_sub(pending_mtu, &cp->reset_task_pending_mtu);
  3597. atomic_dec(&cp->reset_task_pending);
  3598. #else
  3599. atomic_set(&cp->reset_task_pending, 0);
  3600. #endif
  3601. }
  3602. static void cas_link_timer(unsigned long data)
  3603. {
  3604. struct cas *cp = (struct cas *) data;
  3605. int mask, pending = 0, reset = 0;
  3606. unsigned long flags;
  3607. if (link_transition_timeout != 0 &&
  3608. cp->link_transition_jiffies_valid &&
  3609. ((jiffies - cp->link_transition_jiffies) >
  3610. (link_transition_timeout))) {
  3611. /* One-second counter so link-down workaround doesn't
  3612. * cause resets to occur so fast as to fool the switch
  3613. * into thinking the link is down.
  3614. */
  3615. cp->link_transition_jiffies_valid = 0;
  3616. }
  3617. if (!cp->hw_running)
  3618. return;
  3619. spin_lock_irqsave(&cp->lock, flags);
  3620. cas_lock_tx(cp);
  3621. cas_entropy_gather(cp);
  3622. /* If the link task is still pending, we just
  3623. * reschedule the link timer
  3624. */
  3625. #if 1
  3626. if (atomic_read(&cp->reset_task_pending_all) ||
  3627. atomic_read(&cp->reset_task_pending_spare) ||
  3628. atomic_read(&cp->reset_task_pending_mtu))
  3629. goto done;
  3630. #else
  3631. if (atomic_read(&cp->reset_task_pending))
  3632. goto done;
  3633. #endif
  3634. /* check for rx cleaning */
  3635. if ((mask = (cp->cas_flags & CAS_FLAG_RXD_POST_MASK))) {
  3636. int i, rmask;
  3637. for (i = 0; i < MAX_RX_DESC_RINGS; i++) {
  3638. rmask = CAS_FLAG_RXD_POST(i);
  3639. if ((mask & rmask) == 0)
  3640. continue;
  3641. /* post_rxds will do a mod_timer */
  3642. if (cas_post_rxds_ringN(cp, i, cp->rx_last[i]) < 0) {
  3643. pending = 1;
  3644. continue;
  3645. }
  3646. cp->cas_flags &= ~rmask;
  3647. }
  3648. }
  3649. if (CAS_PHY_MII(cp->phy_type)) {
  3650. u16 bmsr;
  3651. cas_mif_poll(cp, 0);
  3652. bmsr = cas_phy_read(cp, MII_BMSR);
  3653. /* WTZ: Solaris driver reads this twice, but that
  3654. * may be due to the PCS case and the use of a
  3655. * common implementation. Read it twice here to be
  3656. * safe.
  3657. */
  3658. bmsr = cas_phy_read(cp, MII_BMSR);
  3659. cas_mif_poll(cp, 1);
  3660. readl(cp->regs + REG_MIF_STATUS); /* avoid dups */
  3661. reset = cas_mii_link_check(cp, bmsr);
  3662. } else {
  3663. reset = cas_pcs_link_check(cp);
  3664. }
  3665. if (reset)
  3666. goto done;
  3667. /* check for tx state machine confusion */
  3668. if ((readl(cp->regs + REG_MAC_TX_STATUS) & MAC_TX_FRAME_XMIT) == 0) {
  3669. u32 val = readl(cp->regs + REG_MAC_STATE_MACHINE);
  3670. u32 wptr, rptr;
  3671. int tlm = CAS_VAL(MAC_SM_TLM, val);
  3672. if (((tlm == 0x5) || (tlm == 0x3)) &&
  3673. (CAS_VAL(MAC_SM_ENCAP_SM, val) == 0)) {
  3674. if (netif_msg_tx_err(cp))
  3675. printk(KERN_DEBUG "%s: tx err: "
  3676. "MAC_STATE[%08x]\n",
  3677. cp->dev->name, val);
  3678. reset = 1;
  3679. goto done;
  3680. }
  3681. val = readl(cp->regs + REG_TX_FIFO_PKT_CNT);
  3682. wptr = readl(cp->regs + REG_TX_FIFO_WRITE_PTR);
  3683. rptr = readl(cp->regs + REG_TX_FIFO_READ_PTR);
  3684. if ((val == 0) && (wptr != rptr)) {
  3685. if (netif_msg_tx_err(cp))
  3686. printk(KERN_DEBUG "%s: tx err: "
  3687. "TX_FIFO[%08x:%08x:%08x]\n",
  3688. cp->dev->name, val, wptr, rptr);
  3689. reset = 1;
  3690. }
  3691. if (reset)
  3692. cas_hard_reset(cp);
  3693. }
  3694. done:
  3695. if (reset) {
  3696. #if 1
  3697. atomic_inc(&cp->reset_task_pending);
  3698. atomic_inc(&cp->reset_task_pending_all);
  3699. schedule_work(&cp->reset_task);
  3700. #else
  3701. atomic_set(&cp->reset_task_pending, CAS_RESET_ALL);
  3702. printk(KERN_ERR "reset called in cas_link_timer\n");
  3703. schedule_work(&cp->reset_task);
  3704. #endif
  3705. }
  3706. if (!pending)
  3707. mod_timer(&cp->link_timer, jiffies + CAS_LINK_TIMEOUT);
  3708. cas_unlock_tx(cp);
  3709. spin_unlock_irqrestore(&cp->lock, flags);
  3710. }
  3711. /* tiny buffers are used to avoid target abort issues with
  3712. * older cassini's
  3713. */
  3714. static void cas_tx_tiny_free(struct cas *cp)
  3715. {
  3716. struct pci_dev *pdev = cp->pdev;
  3717. int i;
  3718. for (i = 0; i < N_TX_RINGS; i++) {
  3719. if (!cp->tx_tiny_bufs[i])
  3720. continue;
  3721. pci_free_consistent(pdev, TX_TINY_BUF_BLOCK,
  3722. cp->tx_tiny_bufs[i],
  3723. cp->tx_tiny_dvma[i]);
  3724. cp->tx_tiny_bufs[i] = NULL;
  3725. }
  3726. }
  3727. static int cas_tx_tiny_alloc(struct cas *cp)
  3728. {
  3729. struct pci_dev *pdev = cp->pdev;
  3730. int i;
  3731. for (i = 0; i < N_TX_RINGS; i++) {
  3732. cp->tx_tiny_bufs[i] =
  3733. pci_alloc_consistent(pdev, TX_TINY_BUF_BLOCK,
  3734. &cp->tx_tiny_dvma[i]);
  3735. if (!cp->tx_tiny_bufs[i]) {
  3736. cas_tx_tiny_free(cp);
  3737. return -1;
  3738. }
  3739. }
  3740. return 0;
  3741. }
  3742. static int cas_open(struct net_device *dev)
  3743. {
  3744. struct cas *cp = netdev_priv(dev);
  3745. int hw_was_up, err;
  3746. unsigned long flags;
  3747. mutex_lock(&cp->pm_mutex);
  3748. hw_was_up = cp->hw_running;
  3749. /* The power-management mutex protects the hw_running
  3750. * etc. state so it is safe to do this bit without cp->lock
  3751. */
  3752. if (!cp->hw_running) {
  3753. /* Reset the chip */
  3754. cas_lock_all_save(cp, flags);
  3755. /* We set the second arg to cas_reset to zero
  3756. * because cas_init_hw below will have its second
  3757. * argument set to non-zero, which will force
  3758. * autonegotiation to start.
  3759. */
  3760. cas_reset(cp, 0);
  3761. cp->hw_running = 1;
  3762. cas_unlock_all_restore(cp, flags);
  3763. }
  3764. if (cas_tx_tiny_alloc(cp) < 0)
  3765. return -ENOMEM;
  3766. /* alloc rx descriptors */
  3767. err = -ENOMEM;
  3768. if (cas_alloc_rxds(cp) < 0)
  3769. goto err_tx_tiny;
  3770. /* allocate spares */
  3771. cas_spare_init(cp);
  3772. cas_spare_recover(cp, GFP_KERNEL);
  3773. /* We can now request the interrupt as we know it's masked
  3774. * on the controller. cassini+ has up to 4 interrupts
  3775. * that can be used, but you need to do explicit pci interrupt
  3776. * mapping to expose them
  3777. */
  3778. if (request_irq(cp->pdev->irq, cas_interrupt,
  3779. IRQF_SHARED, dev->name, (void *) dev)) {
  3780. printk(KERN_ERR "%s: failed to request irq !\n",
  3781. cp->dev->name);
  3782. err = -EAGAIN;
  3783. goto err_spare;
  3784. }
  3785. #ifdef USE_NAPI
  3786. napi_enable(&cp->napi);
  3787. #endif
  3788. /* init hw */
  3789. cas_lock_all_save(cp, flags);
  3790. cas_clean_rings(cp);
  3791. cas_init_hw(cp, !hw_was_up);
  3792. cp->opened = 1;
  3793. cas_unlock_all_restore(cp, flags);
  3794. netif_start_queue(dev);
  3795. mutex_unlock(&cp->pm_mutex);
  3796. return 0;
  3797. err_spare:
  3798. cas_spare_free(cp);
  3799. cas_free_rxds(cp);
  3800. err_tx_tiny:
  3801. cas_tx_tiny_free(cp);
  3802. mutex_unlock(&cp->pm_mutex);
  3803. return err;
  3804. }
  3805. static int cas_close(struct net_device *dev)
  3806. {
  3807. unsigned long flags;
  3808. struct cas *cp = netdev_priv(dev);
  3809. #ifdef USE_NAPI
  3810. napi_disable(&cp->napi);
  3811. #endif
  3812. /* Make sure we don't get distracted by suspend/resume */
  3813. mutex_lock(&cp->pm_mutex);
  3814. netif_stop_queue(dev);
  3815. /* Stop traffic, mark us closed */
  3816. cas_lock_all_save(cp, flags);
  3817. cp->opened = 0;
  3818. cas_reset(cp, 0);
  3819. cas_phy_init(cp);
  3820. cas_begin_auto_negotiation(cp, NULL);
  3821. cas_clean_rings(cp);
  3822. cas_unlock_all_restore(cp, flags);
  3823. free_irq(cp->pdev->irq, (void *) dev);
  3824. cas_spare_free(cp);
  3825. cas_free_rxds(cp);
  3826. cas_tx_tiny_free(cp);
  3827. mutex_unlock(&cp->pm_mutex);
  3828. return 0;
  3829. }
  3830. static struct {
  3831. const char name[ETH_GSTRING_LEN];
  3832. } ethtool_cassini_statnames[] = {
  3833. {"collisions"},
  3834. {"rx_bytes"},
  3835. {"rx_crc_errors"},
  3836. {"rx_dropped"},
  3837. {"rx_errors"},
  3838. {"rx_fifo_errors"},
  3839. {"rx_frame_errors"},
  3840. {"rx_length_errors"},
  3841. {"rx_over_errors"},
  3842. {"rx_packets"},
  3843. {"tx_aborted_errors"},
  3844. {"tx_bytes"},
  3845. {"tx_dropped"},
  3846. {"tx_errors"},
  3847. {"tx_fifo_errors"},
  3848. {"tx_packets"}
  3849. };
  3850. #define CAS_NUM_STAT_KEYS ARRAY_SIZE(ethtool_cassini_statnames)
  3851. static struct {
  3852. const int offsets; /* neg. values for 2nd arg to cas_read_phy */
  3853. } ethtool_register_table[] = {
  3854. {-MII_BMSR},
  3855. {-MII_BMCR},
  3856. {REG_CAWR},
  3857. {REG_INF_BURST},
  3858. {REG_BIM_CFG},
  3859. {REG_RX_CFG},
  3860. {REG_HP_CFG},
  3861. {REG_MAC_TX_CFG},
  3862. {REG_MAC_RX_CFG},
  3863. {REG_MAC_CTRL_CFG},
  3864. {REG_MAC_XIF_CFG},
  3865. {REG_MIF_CFG},
  3866. {REG_PCS_CFG},
  3867. {REG_SATURN_PCFG},
  3868. {REG_PCS_MII_STATUS},
  3869. {REG_PCS_STATE_MACHINE},
  3870. {REG_MAC_COLL_EXCESS},
  3871. {REG_MAC_COLL_LATE}
  3872. };
  3873. #define CAS_REG_LEN ARRAY_SIZE(ethtool_register_table)
  3874. #define CAS_MAX_REGS (sizeof (u32)*CAS_REG_LEN)
  3875. static void cas_read_regs(struct cas *cp, u8 *ptr, int len)
  3876. {
  3877. u8 *p;
  3878. int i;
  3879. unsigned long flags;
  3880. spin_lock_irqsave(&cp->lock, flags);
  3881. for (i = 0, p = ptr; i < len ; i ++, p += sizeof(u32)) {
  3882. u16 hval;
  3883. u32 val;
  3884. if (ethtool_register_table[i].offsets < 0) {
  3885. hval = cas_phy_read(cp,
  3886. -ethtool_register_table[i].offsets);
  3887. val = hval;
  3888. } else {
  3889. val= readl(cp->regs+ethtool_register_table[i].offsets);
  3890. }
  3891. memcpy(p, (u8 *)&val, sizeof(u32));
  3892. }
  3893. spin_unlock_irqrestore(&cp->lock, flags);
  3894. }
  3895. static struct net_device_stats *cas_get_stats(struct net_device *dev)
  3896. {
  3897. struct cas *cp = netdev_priv(dev);
  3898. struct net_device_stats *stats = cp->net_stats;
  3899. unsigned long flags;
  3900. int i;
  3901. unsigned long tmp;
  3902. /* we collate all of the stats into net_stats[N_TX_RING] */
  3903. if (!cp->hw_running)
  3904. return stats + N_TX_RINGS;
  3905. /* collect outstanding stats */
  3906. /* WTZ: the Cassini spec gives these as 16 bit counters but
  3907. * stored in 32-bit words. Added a mask of 0xffff to be safe,
  3908. * in case the chip somehow puts any garbage in the other bits.
  3909. * Also, counter usage didn't seem to mach what Adrian did
  3910. * in the parts of the code that set these quantities. Made
  3911. * that consistent.
  3912. */
  3913. spin_lock_irqsave(&cp->stat_lock[N_TX_RINGS], flags);
  3914. stats[N_TX_RINGS].rx_crc_errors +=
  3915. readl(cp->regs + REG_MAC_FCS_ERR) & 0xffff;
  3916. stats[N_TX_RINGS].rx_frame_errors +=
  3917. readl(cp->regs + REG_MAC_ALIGN_ERR) &0xffff;
  3918. stats[N_TX_RINGS].rx_length_errors +=
  3919. readl(cp->regs + REG_MAC_LEN_ERR) & 0xffff;
  3920. #if 1
  3921. tmp = (readl(cp->regs + REG_MAC_COLL_EXCESS) & 0xffff) +
  3922. (readl(cp->regs + REG_MAC_COLL_LATE) & 0xffff);
  3923. stats[N_TX_RINGS].tx_aborted_errors += tmp;
  3924. stats[N_TX_RINGS].collisions +=
  3925. tmp + (readl(cp->regs + REG_MAC_COLL_NORMAL) & 0xffff);
  3926. #else
  3927. stats[N_TX_RINGS].tx_aborted_errors +=
  3928. readl(cp->regs + REG_MAC_COLL_EXCESS);
  3929. stats[N_TX_RINGS].collisions += readl(cp->regs + REG_MAC_COLL_EXCESS) +
  3930. readl(cp->regs + REG_MAC_COLL_LATE);
  3931. #endif
  3932. cas_clear_mac_err(cp);
  3933. /* saved bits that are unique to ring 0 */
  3934. spin_lock(&cp->stat_lock[0]);
  3935. stats[N_TX_RINGS].collisions += stats[0].collisions;
  3936. stats[N_TX_RINGS].rx_over_errors += stats[0].rx_over_errors;
  3937. stats[N_TX_RINGS].rx_frame_errors += stats[0].rx_frame_errors;
  3938. stats[N_TX_RINGS].rx_fifo_errors += stats[0].rx_fifo_errors;
  3939. stats[N_TX_RINGS].tx_aborted_errors += stats[0].tx_aborted_errors;
  3940. stats[N_TX_RINGS].tx_fifo_errors += stats[0].tx_fifo_errors;
  3941. spin_unlock(&cp->stat_lock[0]);
  3942. for (i = 0; i < N_TX_RINGS; i++) {
  3943. spin_lock(&cp->stat_lock[i]);
  3944. stats[N_TX_RINGS].rx_length_errors +=
  3945. stats[i].rx_length_errors;
  3946. stats[N_TX_RINGS].rx_crc_errors += stats[i].rx_crc_errors;
  3947. stats[N_TX_RINGS].rx_packets += stats[i].rx_packets;
  3948. stats[N_TX_RINGS].tx_packets += stats[i].tx_packets;
  3949. stats[N_TX_RINGS].rx_bytes += stats[i].rx_bytes;
  3950. stats[N_TX_RINGS].tx_bytes += stats[i].tx_bytes;
  3951. stats[N_TX_RINGS].rx_errors += stats[i].rx_errors;
  3952. stats[N_TX_RINGS].tx_errors += stats[i].tx_errors;
  3953. stats[N_TX_RINGS].rx_dropped += stats[i].rx_dropped;
  3954. stats[N_TX_RINGS].tx_dropped += stats[i].tx_dropped;
  3955. memset(stats + i, 0, sizeof(struct net_device_stats));
  3956. spin_unlock(&cp->stat_lock[i]);
  3957. }
  3958. spin_unlock_irqrestore(&cp->stat_lock[N_TX_RINGS], flags);
  3959. return stats + N_TX_RINGS;
  3960. }
  3961. static void cas_set_multicast(struct net_device *dev)
  3962. {
  3963. struct cas *cp = netdev_priv(dev);
  3964. u32 rxcfg, rxcfg_new;
  3965. unsigned long flags;
  3966. int limit = STOP_TRIES;
  3967. if (!cp->hw_running)
  3968. return;
  3969. spin_lock_irqsave(&cp->lock, flags);
  3970. rxcfg = readl(cp->regs + REG_MAC_RX_CFG);
  3971. /* disable RX MAC and wait for completion */
  3972. writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  3973. while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_EN) {
  3974. if (!limit--)
  3975. break;
  3976. udelay(10);
  3977. }
  3978. /* disable hash filter and wait for completion */
  3979. limit = STOP_TRIES;
  3980. rxcfg &= ~(MAC_RX_CFG_PROMISC_EN | MAC_RX_CFG_HASH_FILTER_EN);
  3981. writel(rxcfg & ~MAC_RX_CFG_EN, cp->regs + REG_MAC_RX_CFG);
  3982. while (readl(cp->regs + REG_MAC_RX_CFG) & MAC_RX_CFG_HASH_FILTER_EN) {
  3983. if (!limit--)
  3984. break;
  3985. udelay(10);
  3986. }
  3987. /* program hash filters */
  3988. cp->mac_rx_cfg = rxcfg_new = cas_setup_multicast(cp);
  3989. rxcfg |= rxcfg_new;
  3990. writel(rxcfg, cp->regs + REG_MAC_RX_CFG);
  3991. spin_unlock_irqrestore(&cp->lock, flags);
  3992. }
  3993. static void cas_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  3994. {
  3995. struct cas *cp = netdev_priv(dev);
  3996. strncpy(info->driver, DRV_MODULE_NAME, ETHTOOL_BUSINFO_LEN);
  3997. strncpy(info->version, DRV_MODULE_VERSION, ETHTOOL_BUSINFO_LEN);
  3998. info->fw_version[0] = '\0';
  3999. strncpy(info->bus_info, pci_name(cp->pdev), ETHTOOL_BUSINFO_LEN);
  4000. info->regdump_len = cp->casreg_len < CAS_MAX_REGS ?
  4001. cp->casreg_len : CAS_MAX_REGS;
  4002. info->n_stats = CAS_NUM_STAT_KEYS;
  4003. }
  4004. static int cas_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  4005. {
  4006. struct cas *cp = netdev_priv(dev);
  4007. u16 bmcr;
  4008. int full_duplex, speed, pause;
  4009. unsigned long flags;
  4010. enum link_state linkstate = link_up;
  4011. cmd->advertising = 0;
  4012. cmd->supported = SUPPORTED_Autoneg;
  4013. if (cp->cas_flags & CAS_FLAG_1000MB_CAP) {
  4014. cmd->supported |= SUPPORTED_1000baseT_Full;
  4015. cmd->advertising |= ADVERTISED_1000baseT_Full;
  4016. }
  4017. /* Record PHY settings if HW is on. */
  4018. spin_lock_irqsave(&cp->lock, flags);
  4019. bmcr = 0;
  4020. linkstate = cp->lstate;
  4021. if (CAS_PHY_MII(cp->phy_type)) {
  4022. cmd->port = PORT_MII;
  4023. cmd->transceiver = (cp->cas_flags & CAS_FLAG_SATURN) ?
  4024. XCVR_INTERNAL : XCVR_EXTERNAL;
  4025. cmd->phy_address = cp->phy_addr;
  4026. cmd->advertising |= ADVERTISED_TP | ADVERTISED_MII |
  4027. ADVERTISED_10baseT_Half |
  4028. ADVERTISED_10baseT_Full |
  4029. ADVERTISED_100baseT_Half |
  4030. ADVERTISED_100baseT_Full;
  4031. cmd->supported |=
  4032. (SUPPORTED_10baseT_Half |
  4033. SUPPORTED_10baseT_Full |
  4034. SUPPORTED_100baseT_Half |
  4035. SUPPORTED_100baseT_Full |
  4036. SUPPORTED_TP | SUPPORTED_MII);
  4037. if (cp->hw_running) {
  4038. cas_mif_poll(cp, 0);
  4039. bmcr = cas_phy_read(cp, MII_BMCR);
  4040. cas_read_mii_link_mode(cp, &full_duplex,
  4041. &speed, &pause);
  4042. cas_mif_poll(cp, 1);
  4043. }
  4044. } else {
  4045. cmd->port = PORT_FIBRE;
  4046. cmd->transceiver = XCVR_INTERNAL;
  4047. cmd->phy_address = 0;
  4048. cmd->supported |= SUPPORTED_FIBRE;
  4049. cmd->advertising |= ADVERTISED_FIBRE;
  4050. if (cp->hw_running) {
  4051. /* pcs uses the same bits as mii */
  4052. bmcr = readl(cp->regs + REG_PCS_MII_CTRL);
  4053. cas_read_pcs_link_mode(cp, &full_duplex,
  4054. &speed, &pause);
  4055. }
  4056. }
  4057. spin_unlock_irqrestore(&cp->lock, flags);
  4058. if (bmcr & BMCR_ANENABLE) {
  4059. cmd->advertising |= ADVERTISED_Autoneg;
  4060. cmd->autoneg = AUTONEG_ENABLE;
  4061. cmd->speed = ((speed == 10) ?
  4062. SPEED_10 :
  4063. ((speed == 1000) ?
  4064. SPEED_1000 : SPEED_100));
  4065. cmd->duplex = full_duplex ? DUPLEX_FULL : DUPLEX_HALF;
  4066. } else {
  4067. cmd->autoneg = AUTONEG_DISABLE;
  4068. cmd->speed =
  4069. (bmcr & CAS_BMCR_SPEED1000) ?
  4070. SPEED_1000 :
  4071. ((bmcr & BMCR_SPEED100) ? SPEED_100:
  4072. SPEED_10);
  4073. cmd->duplex =
  4074. (bmcr & BMCR_FULLDPLX) ?
  4075. DUPLEX_FULL : DUPLEX_HALF;
  4076. }
  4077. if (linkstate != link_up) {
  4078. /* Force these to "unknown" if the link is not up and
  4079. * autonogotiation in enabled. We can set the link
  4080. * speed to 0, but not cmd->duplex,
  4081. * because its legal values are 0 and 1. Ethtool will
  4082. * print the value reported in parentheses after the
  4083. * word "Unknown" for unrecognized values.
  4084. *
  4085. * If in forced mode, we report the speed and duplex
  4086. * settings that we configured.
  4087. */
  4088. if (cp->link_cntl & BMCR_ANENABLE) {
  4089. cmd->speed = 0;
  4090. cmd->duplex = 0xff;
  4091. } else {
  4092. cmd->speed = SPEED_10;
  4093. if (cp->link_cntl & BMCR_SPEED100) {
  4094. cmd->speed = SPEED_100;
  4095. } else if (cp->link_cntl & CAS_BMCR_SPEED1000) {
  4096. cmd->speed = SPEED_1000;
  4097. }
  4098. cmd->duplex = (cp->link_cntl & BMCR_FULLDPLX)?
  4099. DUPLEX_FULL : DUPLEX_HALF;
  4100. }
  4101. }
  4102. return 0;
  4103. }
  4104. static int cas_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  4105. {
  4106. struct cas *cp = netdev_priv(dev);
  4107. unsigned long flags;
  4108. /* Verify the settings we care about. */
  4109. if (cmd->autoneg != AUTONEG_ENABLE &&
  4110. cmd->autoneg != AUTONEG_DISABLE)
  4111. return -EINVAL;
  4112. if (cmd->autoneg == AUTONEG_DISABLE &&
  4113. ((cmd->speed != SPEED_1000 &&
  4114. cmd->speed != SPEED_100 &&
  4115. cmd->speed != SPEED_10) ||
  4116. (cmd->duplex != DUPLEX_HALF &&
  4117. cmd->duplex != DUPLEX_FULL)))
  4118. return -EINVAL;
  4119. /* Apply settings and restart link process. */
  4120. spin_lock_irqsave(&cp->lock, flags);
  4121. cas_begin_auto_negotiation(cp, cmd);
  4122. spin_unlock_irqrestore(&cp->lock, flags);
  4123. return 0;
  4124. }
  4125. static int cas_nway_reset(struct net_device *dev)
  4126. {
  4127. struct cas *cp = netdev_priv(dev);
  4128. unsigned long flags;
  4129. if ((cp->link_cntl & BMCR_ANENABLE) == 0)
  4130. return -EINVAL;
  4131. /* Restart link process. */
  4132. spin_lock_irqsave(&cp->lock, flags);
  4133. cas_begin_auto_negotiation(cp, NULL);
  4134. spin_unlock_irqrestore(&cp->lock, flags);
  4135. return 0;
  4136. }
  4137. static u32 cas_get_link(struct net_device *dev)
  4138. {
  4139. struct cas *cp = netdev_priv(dev);
  4140. return cp->lstate == link_up;
  4141. }
  4142. static u32 cas_get_msglevel(struct net_device *dev)
  4143. {
  4144. struct cas *cp = netdev_priv(dev);
  4145. return cp->msg_enable;
  4146. }
  4147. static void cas_set_msglevel(struct net_device *dev, u32 value)
  4148. {
  4149. struct cas *cp = netdev_priv(dev);
  4150. cp->msg_enable = value;
  4151. }
  4152. static int cas_get_regs_len(struct net_device *dev)
  4153. {
  4154. struct cas *cp = netdev_priv(dev);
  4155. return cp->casreg_len < CAS_MAX_REGS ? cp->casreg_len: CAS_MAX_REGS;
  4156. }
  4157. static void cas_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  4158. void *p)
  4159. {
  4160. struct cas *cp = netdev_priv(dev);
  4161. regs->version = 0;
  4162. /* cas_read_regs handles locks (cp->lock). */
  4163. cas_read_regs(cp, p, regs->len / sizeof(u32));
  4164. }
  4165. static int cas_get_sset_count(struct net_device *dev, int sset)
  4166. {
  4167. switch (sset) {
  4168. case ETH_SS_STATS:
  4169. return CAS_NUM_STAT_KEYS;
  4170. default:
  4171. return -EOPNOTSUPP;
  4172. }
  4173. }
  4174. static void cas_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  4175. {
  4176. memcpy(data, &ethtool_cassini_statnames,
  4177. CAS_NUM_STAT_KEYS * ETH_GSTRING_LEN);
  4178. }
  4179. static void cas_get_ethtool_stats(struct net_device *dev,
  4180. struct ethtool_stats *estats, u64 *data)
  4181. {
  4182. struct cas *cp = netdev_priv(dev);
  4183. struct net_device_stats *stats = cas_get_stats(cp->dev);
  4184. int i = 0;
  4185. data[i++] = stats->collisions;
  4186. data[i++] = stats->rx_bytes;
  4187. data[i++] = stats->rx_crc_errors;
  4188. data[i++] = stats->rx_dropped;
  4189. data[i++] = stats->rx_errors;
  4190. data[i++] = stats->rx_fifo_errors;
  4191. data[i++] = stats->rx_frame_errors;
  4192. data[i++] = stats->rx_length_errors;
  4193. data[i++] = stats->rx_over_errors;
  4194. data[i++] = stats->rx_packets;
  4195. data[i++] = stats->tx_aborted_errors;
  4196. data[i++] = stats->tx_bytes;
  4197. data[i++] = stats->tx_dropped;
  4198. data[i++] = stats->tx_errors;
  4199. data[i++] = stats->tx_fifo_errors;
  4200. data[i++] = stats->tx_packets;
  4201. BUG_ON(i != CAS_NUM_STAT_KEYS);
  4202. }
  4203. static const struct ethtool_ops cas_ethtool_ops = {
  4204. .get_drvinfo = cas_get_drvinfo,
  4205. .get_settings = cas_get_settings,
  4206. .set_settings = cas_set_settings,
  4207. .nway_reset = cas_nway_reset,
  4208. .get_link = cas_get_link,
  4209. .get_msglevel = cas_get_msglevel,
  4210. .set_msglevel = cas_set_msglevel,
  4211. .get_regs_len = cas_get_regs_len,
  4212. .get_regs = cas_get_regs,
  4213. .get_sset_count = cas_get_sset_count,
  4214. .get_strings = cas_get_strings,
  4215. .get_ethtool_stats = cas_get_ethtool_stats,
  4216. };
  4217. static int cas_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  4218. {
  4219. struct cas *cp = netdev_priv(dev);
  4220. struct mii_ioctl_data *data = if_mii(ifr);
  4221. unsigned long flags;
  4222. int rc = -EOPNOTSUPP;
  4223. /* Hold the PM mutex while doing ioctl's or we may collide
  4224. * with open/close and power management and oops.
  4225. */
  4226. mutex_lock(&cp->pm_mutex);
  4227. switch (cmd) {
  4228. case SIOCGMIIPHY: /* Get address of MII PHY in use. */
  4229. data->phy_id = cp->phy_addr;
  4230. /* Fallthrough... */
  4231. case SIOCGMIIREG: /* Read MII PHY register. */
  4232. spin_lock_irqsave(&cp->lock, flags);
  4233. cas_mif_poll(cp, 0);
  4234. data->val_out = cas_phy_read(cp, data->reg_num & 0x1f);
  4235. cas_mif_poll(cp, 1);
  4236. spin_unlock_irqrestore(&cp->lock, flags);
  4237. rc = 0;
  4238. break;
  4239. case SIOCSMIIREG: /* Write MII PHY register. */
  4240. if (!capable(CAP_NET_ADMIN)) {
  4241. rc = -EPERM;
  4242. break;
  4243. }
  4244. spin_lock_irqsave(&cp->lock, flags);
  4245. cas_mif_poll(cp, 0);
  4246. rc = cas_phy_write(cp, data->reg_num & 0x1f, data->val_in);
  4247. cas_mif_poll(cp, 1);
  4248. spin_unlock_irqrestore(&cp->lock, flags);
  4249. break;
  4250. default:
  4251. break;
  4252. };
  4253. mutex_unlock(&cp->pm_mutex);
  4254. return rc;
  4255. }
  4256. /* When this chip sits underneath an Intel 31154 bridge, it is the
  4257. * only subordinate device and we can tweak the bridge settings to
  4258. * reflect that fact.
  4259. */
  4260. static void __devinit cas_program_bridge(struct pci_dev *cas_pdev)
  4261. {
  4262. struct pci_dev *pdev = cas_pdev->bus->self;
  4263. u32 val;
  4264. if (!pdev)
  4265. return;
  4266. if (pdev->vendor != 0x8086 || pdev->device != 0x537c)
  4267. return;
  4268. /* Clear bit 10 (Bus Parking Control) in the Secondary
  4269. * Arbiter Control/Status Register which lives at offset
  4270. * 0x41. Using a 32-bit word read/modify/write at 0x40
  4271. * is much simpler so that's how we do this.
  4272. */
  4273. pci_read_config_dword(pdev, 0x40, &val);
  4274. val &= ~0x00040000;
  4275. pci_write_config_dword(pdev, 0x40, val);
  4276. /* Max out the Multi-Transaction Timer settings since
  4277. * Cassini is the only device present.
  4278. *
  4279. * The register is 16-bit and lives at 0x50. When the
  4280. * settings are enabled, it extends the GRANT# signal
  4281. * for a requestor after a transaction is complete. This
  4282. * allows the next request to run without first needing
  4283. * to negotiate the GRANT# signal back.
  4284. *
  4285. * Bits 12:10 define the grant duration:
  4286. *
  4287. * 1 -- 16 clocks
  4288. * 2 -- 32 clocks
  4289. * 3 -- 64 clocks
  4290. * 4 -- 128 clocks
  4291. * 5 -- 256 clocks
  4292. *
  4293. * All other values are illegal.
  4294. *
  4295. * Bits 09:00 define which REQ/GNT signal pairs get the
  4296. * GRANT# signal treatment. We set them all.
  4297. */
  4298. pci_write_config_word(pdev, 0x50, (5 << 10) | 0x3ff);
  4299. /* The Read Prefecth Policy register is 16-bit and sits at
  4300. * offset 0x52. It enables a "smart" pre-fetch policy. We
  4301. * enable it and max out all of the settings since only one
  4302. * device is sitting underneath and thus bandwidth sharing is
  4303. * not an issue.
  4304. *
  4305. * The register has several 3 bit fields, which indicates a
  4306. * multiplier applied to the base amount of prefetching the
  4307. * chip would do. These fields are at:
  4308. *
  4309. * 15:13 --- ReRead Primary Bus
  4310. * 12:10 --- FirstRead Primary Bus
  4311. * 09:07 --- ReRead Secondary Bus
  4312. * 06:04 --- FirstRead Secondary Bus
  4313. *
  4314. * Bits 03:00 control which REQ/GNT pairs the prefetch settings
  4315. * get enabled on. Bit 3 is a grouped enabler which controls
  4316. * all of the REQ/GNT pairs from [8:3]. Bits 2 to 0 control
  4317. * the individual REQ/GNT pairs [2:0].
  4318. */
  4319. pci_write_config_word(pdev, 0x52,
  4320. (0x7 << 13) |
  4321. (0x7 << 10) |
  4322. (0x7 << 7) |
  4323. (0x7 << 4) |
  4324. (0xf << 0));
  4325. /* Force cacheline size to 0x8 */
  4326. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, 0x08);
  4327. /* Force latency timer to maximum setting so Cassini can
  4328. * sit on the bus as long as it likes.
  4329. */
  4330. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0xff);
  4331. }
  4332. static int __devinit cas_init_one(struct pci_dev *pdev,
  4333. const struct pci_device_id *ent)
  4334. {
  4335. static int cas_version_printed = 0;
  4336. unsigned long casreg_len;
  4337. struct net_device *dev;
  4338. struct cas *cp;
  4339. int i, err, pci_using_dac;
  4340. u16 pci_cmd;
  4341. u8 orig_cacheline_size = 0, cas_cacheline_size = 0;
  4342. DECLARE_MAC_BUF(mac);
  4343. if (cas_version_printed++ == 0)
  4344. printk(KERN_INFO "%s", version);
  4345. err = pci_enable_device(pdev);
  4346. if (err) {
  4347. dev_err(&pdev->dev, "Cannot enable PCI device, aborting.\n");
  4348. return err;
  4349. }
  4350. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  4351. dev_err(&pdev->dev, "Cannot find proper PCI device "
  4352. "base address, aborting.\n");
  4353. err = -ENODEV;
  4354. goto err_out_disable_pdev;
  4355. }
  4356. dev = alloc_etherdev(sizeof(*cp));
  4357. if (!dev) {
  4358. dev_err(&pdev->dev, "Etherdev alloc failed, aborting.\n");
  4359. err = -ENOMEM;
  4360. goto err_out_disable_pdev;
  4361. }
  4362. SET_NETDEV_DEV(dev, &pdev->dev);
  4363. err = pci_request_regions(pdev, dev->name);
  4364. if (err) {
  4365. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting.\n");
  4366. goto err_out_free_netdev;
  4367. }
  4368. pci_set_master(pdev);
  4369. /* we must always turn on parity response or else parity
  4370. * doesn't get generated properly. disable SERR/PERR as well.
  4371. * in addition, we want to turn MWI on.
  4372. */
  4373. pci_read_config_word(pdev, PCI_COMMAND, &pci_cmd);
  4374. pci_cmd &= ~PCI_COMMAND_SERR;
  4375. pci_cmd |= PCI_COMMAND_PARITY;
  4376. pci_write_config_word(pdev, PCI_COMMAND, pci_cmd);
  4377. if (pci_try_set_mwi(pdev))
  4378. printk(KERN_WARNING PFX "Could not enable MWI for %s\n",
  4379. pci_name(pdev));
  4380. cas_program_bridge(pdev);
  4381. /*
  4382. * On some architectures, the default cache line size set
  4383. * by pci_try_set_mwi reduces perforamnce. We have to increase
  4384. * it for this case. To start, we'll print some configuration
  4385. * data.
  4386. */
  4387. #if 1
  4388. pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  4389. &orig_cacheline_size);
  4390. if (orig_cacheline_size < CAS_PREF_CACHELINE_SIZE) {
  4391. cas_cacheline_size =
  4392. (CAS_PREF_CACHELINE_SIZE < SMP_CACHE_BYTES) ?
  4393. CAS_PREF_CACHELINE_SIZE : SMP_CACHE_BYTES;
  4394. if (pci_write_config_byte(pdev,
  4395. PCI_CACHE_LINE_SIZE,
  4396. cas_cacheline_size)) {
  4397. dev_err(&pdev->dev, "Could not set PCI cache "
  4398. "line size\n");
  4399. goto err_write_cacheline;
  4400. }
  4401. }
  4402. #endif
  4403. /* Configure DMA attributes. */
  4404. if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  4405. pci_using_dac = 1;
  4406. err = pci_set_consistent_dma_mask(pdev,
  4407. DMA_64BIT_MASK);
  4408. if (err < 0) {
  4409. dev_err(&pdev->dev, "Unable to obtain 64-bit DMA "
  4410. "for consistent allocations\n");
  4411. goto err_out_free_res;
  4412. }
  4413. } else {
  4414. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  4415. if (err) {
  4416. dev_err(&pdev->dev, "No usable DMA configuration, "
  4417. "aborting.\n");
  4418. goto err_out_free_res;
  4419. }
  4420. pci_using_dac = 0;
  4421. }
  4422. casreg_len = pci_resource_len(pdev, 0);
  4423. cp = netdev_priv(dev);
  4424. cp->pdev = pdev;
  4425. #if 1
  4426. /* A value of 0 indicates we never explicitly set it */
  4427. cp->orig_cacheline_size = cas_cacheline_size ? orig_cacheline_size: 0;
  4428. #endif
  4429. cp->dev = dev;
  4430. cp->msg_enable = (cassini_debug < 0) ? CAS_DEF_MSG_ENABLE :
  4431. cassini_debug;
  4432. cp->link_transition = LINK_TRANSITION_UNKNOWN;
  4433. cp->link_transition_jiffies_valid = 0;
  4434. spin_lock_init(&cp->lock);
  4435. spin_lock_init(&cp->rx_inuse_lock);
  4436. spin_lock_init(&cp->rx_spare_lock);
  4437. for (i = 0; i < N_TX_RINGS; i++) {
  4438. spin_lock_init(&cp->stat_lock[i]);
  4439. spin_lock_init(&cp->tx_lock[i]);
  4440. }
  4441. spin_lock_init(&cp->stat_lock[N_TX_RINGS]);
  4442. mutex_init(&cp->pm_mutex);
  4443. init_timer(&cp->link_timer);
  4444. cp->link_timer.function = cas_link_timer;
  4445. cp->link_timer.data = (unsigned long) cp;
  4446. #if 1
  4447. /* Just in case the implementation of atomic operations
  4448. * change so that an explicit initialization is necessary.
  4449. */
  4450. atomic_set(&cp->reset_task_pending, 0);
  4451. atomic_set(&cp->reset_task_pending_all, 0);
  4452. atomic_set(&cp->reset_task_pending_spare, 0);
  4453. atomic_set(&cp->reset_task_pending_mtu, 0);
  4454. #endif
  4455. INIT_WORK(&cp->reset_task, cas_reset_task);
  4456. /* Default link parameters */
  4457. if (link_mode >= 0 && link_mode <= 6)
  4458. cp->link_cntl = link_modes[link_mode];
  4459. else
  4460. cp->link_cntl = BMCR_ANENABLE;
  4461. cp->lstate = link_down;
  4462. cp->link_transition = LINK_TRANSITION_LINK_DOWN;
  4463. netif_carrier_off(cp->dev);
  4464. cp->timer_ticks = 0;
  4465. /* give us access to cassini registers */
  4466. cp->regs = pci_iomap(pdev, 0, casreg_len);
  4467. if (!cp->regs) {
  4468. dev_err(&pdev->dev, "Cannot map device registers, aborting.\n");
  4469. goto err_out_free_res;
  4470. }
  4471. cp->casreg_len = casreg_len;
  4472. pci_save_state(pdev);
  4473. cas_check_pci_invariants(cp);
  4474. cas_hard_reset(cp);
  4475. cas_reset(cp, 0);
  4476. if (cas_check_invariants(cp))
  4477. goto err_out_iounmap;
  4478. if (cp->cas_flags & CAS_FLAG_SATURN)
  4479. if (cas_saturn_firmware_init(cp))
  4480. goto err_out_iounmap;
  4481. cp->init_block = (struct cas_init_block *)
  4482. pci_alloc_consistent(pdev, sizeof(struct cas_init_block),
  4483. &cp->block_dvma);
  4484. if (!cp->init_block) {
  4485. dev_err(&pdev->dev, "Cannot allocate init block, aborting.\n");
  4486. goto err_out_iounmap;
  4487. }
  4488. for (i = 0; i < N_TX_RINGS; i++)
  4489. cp->init_txds[i] = cp->init_block->txds[i];
  4490. for (i = 0; i < N_RX_DESC_RINGS; i++)
  4491. cp->init_rxds[i] = cp->init_block->rxds[i];
  4492. for (i = 0; i < N_RX_COMP_RINGS; i++)
  4493. cp->init_rxcs[i] = cp->init_block->rxcs[i];
  4494. for (i = 0; i < N_RX_FLOWS; i++)
  4495. skb_queue_head_init(&cp->rx_flows[i]);
  4496. dev->open = cas_open;
  4497. dev->stop = cas_close;
  4498. dev->hard_start_xmit = cas_start_xmit;
  4499. dev->get_stats = cas_get_stats;
  4500. dev->set_multicast_list = cas_set_multicast;
  4501. dev->do_ioctl = cas_ioctl;
  4502. dev->ethtool_ops = &cas_ethtool_ops;
  4503. dev->tx_timeout = cas_tx_timeout;
  4504. dev->watchdog_timeo = CAS_TX_TIMEOUT;
  4505. dev->change_mtu = cas_change_mtu;
  4506. #ifdef USE_NAPI
  4507. netif_napi_add(dev, &cp->napi, cas_poll, 64);
  4508. #endif
  4509. #ifdef CONFIG_NET_POLL_CONTROLLER
  4510. dev->poll_controller = cas_netpoll;
  4511. #endif
  4512. dev->irq = pdev->irq;
  4513. dev->dma = 0;
  4514. /* Cassini features. */
  4515. if ((cp->cas_flags & CAS_FLAG_NO_HW_CSUM) == 0)
  4516. dev->features |= NETIF_F_HW_CSUM | NETIF_F_SG;
  4517. if (pci_using_dac)
  4518. dev->features |= NETIF_F_HIGHDMA;
  4519. if (register_netdev(dev)) {
  4520. dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
  4521. goto err_out_free_consistent;
  4522. }
  4523. i = readl(cp->regs + REG_BIM_CFG);
  4524. printk(KERN_INFO "%s: Sun Cassini%s (%sbit/%sMHz PCI/%s) "
  4525. "Ethernet[%d] %s\n", dev->name,
  4526. (cp->cas_flags & CAS_FLAG_REG_PLUS) ? "+" : "",
  4527. (i & BIM_CFG_32BIT) ? "32" : "64",
  4528. (i & BIM_CFG_66MHZ) ? "66" : "33",
  4529. (cp->phy_type == CAS_PHY_SERDES) ? "Fi" : "Cu", pdev->irq,
  4530. print_mac(mac, dev->dev_addr));
  4531. pci_set_drvdata(pdev, dev);
  4532. cp->hw_running = 1;
  4533. cas_entropy_reset(cp);
  4534. cas_phy_init(cp);
  4535. cas_begin_auto_negotiation(cp, NULL);
  4536. return 0;
  4537. err_out_free_consistent:
  4538. pci_free_consistent(pdev, sizeof(struct cas_init_block),
  4539. cp->init_block, cp->block_dvma);
  4540. err_out_iounmap:
  4541. mutex_lock(&cp->pm_mutex);
  4542. if (cp->hw_running)
  4543. cas_shutdown(cp);
  4544. mutex_unlock(&cp->pm_mutex);
  4545. pci_iounmap(pdev, cp->regs);
  4546. err_out_free_res:
  4547. pci_release_regions(pdev);
  4548. err_write_cacheline:
  4549. /* Try to restore it in case the error occured after we
  4550. * set it.
  4551. */
  4552. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE, orig_cacheline_size);
  4553. err_out_free_netdev:
  4554. free_netdev(dev);
  4555. err_out_disable_pdev:
  4556. pci_disable_device(pdev);
  4557. pci_set_drvdata(pdev, NULL);
  4558. return -ENODEV;
  4559. }
  4560. static void __devexit cas_remove_one(struct pci_dev *pdev)
  4561. {
  4562. struct net_device *dev = pci_get_drvdata(pdev);
  4563. struct cas *cp;
  4564. if (!dev)
  4565. return;
  4566. cp = netdev_priv(dev);
  4567. unregister_netdev(dev);
  4568. if (cp->fw_data)
  4569. vfree(cp->fw_data);
  4570. mutex_lock(&cp->pm_mutex);
  4571. flush_scheduled_work();
  4572. if (cp->hw_running)
  4573. cas_shutdown(cp);
  4574. mutex_unlock(&cp->pm_mutex);
  4575. #if 1
  4576. if (cp->orig_cacheline_size) {
  4577. /* Restore the cache line size if we had modified
  4578. * it.
  4579. */
  4580. pci_write_config_byte(pdev, PCI_CACHE_LINE_SIZE,
  4581. cp->orig_cacheline_size);
  4582. }
  4583. #endif
  4584. pci_free_consistent(pdev, sizeof(struct cas_init_block),
  4585. cp->init_block, cp->block_dvma);
  4586. pci_iounmap(pdev, cp->regs);
  4587. free_netdev(dev);
  4588. pci_release_regions(pdev);
  4589. pci_disable_device(pdev);
  4590. pci_set_drvdata(pdev, NULL);
  4591. }
  4592. #ifdef CONFIG_PM
  4593. static int cas_suspend(struct pci_dev *pdev, pm_message_t state)
  4594. {
  4595. struct net_device *dev = pci_get_drvdata(pdev);
  4596. struct cas *cp = netdev_priv(dev);
  4597. unsigned long flags;
  4598. mutex_lock(&cp->pm_mutex);
  4599. /* If the driver is opened, we stop the DMA */
  4600. if (cp->opened) {
  4601. netif_device_detach(dev);
  4602. cas_lock_all_save(cp, flags);
  4603. /* We can set the second arg of cas_reset to 0
  4604. * because on resume, we'll call cas_init_hw with
  4605. * its second arg set so that autonegotiation is
  4606. * restarted.
  4607. */
  4608. cas_reset(cp, 0);
  4609. cas_clean_rings(cp);
  4610. cas_unlock_all_restore(cp, flags);
  4611. }
  4612. if (cp->hw_running)
  4613. cas_shutdown(cp);
  4614. mutex_unlock(&cp->pm_mutex);
  4615. return 0;
  4616. }
  4617. static int cas_resume(struct pci_dev *pdev)
  4618. {
  4619. struct net_device *dev = pci_get_drvdata(pdev);
  4620. struct cas *cp = netdev_priv(dev);
  4621. printk(KERN_INFO "%s: resuming\n", dev->name);
  4622. mutex_lock(&cp->pm_mutex);
  4623. cas_hard_reset(cp);
  4624. if (cp->opened) {
  4625. unsigned long flags;
  4626. cas_lock_all_save(cp, flags);
  4627. cas_reset(cp, 0);
  4628. cp->hw_running = 1;
  4629. cas_clean_rings(cp);
  4630. cas_init_hw(cp, 1);
  4631. cas_unlock_all_restore(cp, flags);
  4632. netif_device_attach(dev);
  4633. }
  4634. mutex_unlock(&cp->pm_mutex);
  4635. return 0;
  4636. }
  4637. #endif /* CONFIG_PM */
  4638. static struct pci_driver cas_driver = {
  4639. .name = DRV_MODULE_NAME,
  4640. .id_table = cas_pci_tbl,
  4641. .probe = cas_init_one,
  4642. .remove = __devexit_p(cas_remove_one),
  4643. #ifdef CONFIG_PM
  4644. .suspend = cas_suspend,
  4645. .resume = cas_resume
  4646. #endif
  4647. };
  4648. static int __init cas_init(void)
  4649. {
  4650. if (linkdown_timeout > 0)
  4651. link_transition_timeout = linkdown_timeout * HZ;
  4652. else
  4653. link_transition_timeout = 0;
  4654. return pci_register_driver(&cas_driver);
  4655. }
  4656. static void __exit cas_cleanup(void)
  4657. {
  4658. pci_unregister_driver(&cas_driver);
  4659. }
  4660. module_init(cas_init);
  4661. module_exit(cas_cleanup);