cassini.c 139 KB

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