cassini.c 142 KB

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