cassini.c 138 KB

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