cassini.c 138 KB

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