cassini.c 138 KB

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