cassini.c 138 KB

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