cassini.c 137 KB

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