cassini.c 141 KB

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