cassini.c 139 KB

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