cassini.c 140 KB

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