cassini.c 140 KB

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