ksz884x.c 180 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289
  1. /**
  2. * drivers/net/ksx884x.c - Micrel KSZ8841/2 PCI Ethernet driver
  3. *
  4. * Copyright (c) 2009-2010 Micrel, Inc.
  5. * Tristram Ha <Tristram.Ha@micrel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/init.h>
  18. #include <linux/interrupt.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/ioport.h>
  22. #include <linux/pci.h>
  23. #include <linux/proc_fs.h>
  24. #include <linux/mii.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/ethtool.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/in.h>
  29. #include <linux/ip.h>
  30. #include <linux/if_vlan.h>
  31. #include <linux/crc32.h>
  32. #include <linux/sched.h>
  33. #include <linux/slab.h>
  34. /* DMA Registers */
  35. #define KS_DMA_TX_CTRL 0x0000
  36. #define DMA_TX_ENABLE 0x00000001
  37. #define DMA_TX_CRC_ENABLE 0x00000002
  38. #define DMA_TX_PAD_ENABLE 0x00000004
  39. #define DMA_TX_LOOPBACK 0x00000100
  40. #define DMA_TX_FLOW_ENABLE 0x00000200
  41. #define DMA_TX_CSUM_IP 0x00010000
  42. #define DMA_TX_CSUM_TCP 0x00020000
  43. #define DMA_TX_CSUM_UDP 0x00040000
  44. #define DMA_TX_BURST_SIZE 0x3F000000
  45. #define KS_DMA_RX_CTRL 0x0004
  46. #define DMA_RX_ENABLE 0x00000001
  47. #define KS884X_DMA_RX_MULTICAST 0x00000002
  48. #define DMA_RX_PROMISCUOUS 0x00000004
  49. #define DMA_RX_ERROR 0x00000008
  50. #define DMA_RX_UNICAST 0x00000010
  51. #define DMA_RX_ALL_MULTICAST 0x00000020
  52. #define DMA_RX_BROADCAST 0x00000040
  53. #define DMA_RX_FLOW_ENABLE 0x00000200
  54. #define DMA_RX_CSUM_IP 0x00010000
  55. #define DMA_RX_CSUM_TCP 0x00020000
  56. #define DMA_RX_CSUM_UDP 0x00040000
  57. #define DMA_RX_BURST_SIZE 0x3F000000
  58. #define DMA_BURST_SHIFT 24
  59. #define DMA_BURST_DEFAULT 8
  60. #define KS_DMA_TX_START 0x0008
  61. #define KS_DMA_RX_START 0x000C
  62. #define DMA_START 0x00000001
  63. #define KS_DMA_TX_ADDR 0x0010
  64. #define KS_DMA_RX_ADDR 0x0014
  65. #define DMA_ADDR_LIST_MASK 0xFFFFFFFC
  66. #define DMA_ADDR_LIST_SHIFT 2
  67. /* MTR0 */
  68. #define KS884X_MULTICAST_0_OFFSET 0x0020
  69. #define KS884X_MULTICAST_1_OFFSET 0x0021
  70. #define KS884X_MULTICAST_2_OFFSET 0x0022
  71. #define KS884x_MULTICAST_3_OFFSET 0x0023
  72. /* MTR1 */
  73. #define KS884X_MULTICAST_4_OFFSET 0x0024
  74. #define KS884X_MULTICAST_5_OFFSET 0x0025
  75. #define KS884X_MULTICAST_6_OFFSET 0x0026
  76. #define KS884X_MULTICAST_7_OFFSET 0x0027
  77. /* Interrupt Registers */
  78. /* INTEN */
  79. #define KS884X_INTERRUPTS_ENABLE 0x0028
  80. /* INTST */
  81. #define KS884X_INTERRUPTS_STATUS 0x002C
  82. #define KS884X_INT_RX_STOPPED 0x02000000
  83. #define KS884X_INT_TX_STOPPED 0x04000000
  84. #define KS884X_INT_RX_OVERRUN 0x08000000
  85. #define KS884X_INT_TX_EMPTY 0x10000000
  86. #define KS884X_INT_RX 0x20000000
  87. #define KS884X_INT_TX 0x40000000
  88. #define KS884X_INT_PHY 0x80000000
  89. #define KS884X_INT_RX_MASK \
  90. (KS884X_INT_RX | KS884X_INT_RX_OVERRUN)
  91. #define KS884X_INT_TX_MASK \
  92. (KS884X_INT_TX | KS884X_INT_TX_EMPTY)
  93. #define KS884X_INT_MASK (KS884X_INT_RX | KS884X_INT_TX | KS884X_INT_PHY)
  94. /* MAC Additional Station Address */
  95. /* MAAL0 */
  96. #define KS_ADD_ADDR_0_LO 0x0080
  97. /* MAAH0 */
  98. #define KS_ADD_ADDR_0_HI 0x0084
  99. /* MAAL1 */
  100. #define KS_ADD_ADDR_1_LO 0x0088
  101. /* MAAH1 */
  102. #define KS_ADD_ADDR_1_HI 0x008C
  103. /* MAAL2 */
  104. #define KS_ADD_ADDR_2_LO 0x0090
  105. /* MAAH2 */
  106. #define KS_ADD_ADDR_2_HI 0x0094
  107. /* MAAL3 */
  108. #define KS_ADD_ADDR_3_LO 0x0098
  109. /* MAAH3 */
  110. #define KS_ADD_ADDR_3_HI 0x009C
  111. /* MAAL4 */
  112. #define KS_ADD_ADDR_4_LO 0x00A0
  113. /* MAAH4 */
  114. #define KS_ADD_ADDR_4_HI 0x00A4
  115. /* MAAL5 */
  116. #define KS_ADD_ADDR_5_LO 0x00A8
  117. /* MAAH5 */
  118. #define KS_ADD_ADDR_5_HI 0x00AC
  119. /* MAAL6 */
  120. #define KS_ADD_ADDR_6_LO 0x00B0
  121. /* MAAH6 */
  122. #define KS_ADD_ADDR_6_HI 0x00B4
  123. /* MAAL7 */
  124. #define KS_ADD_ADDR_7_LO 0x00B8
  125. /* MAAH7 */
  126. #define KS_ADD_ADDR_7_HI 0x00BC
  127. /* MAAL8 */
  128. #define KS_ADD_ADDR_8_LO 0x00C0
  129. /* MAAH8 */
  130. #define KS_ADD_ADDR_8_HI 0x00C4
  131. /* MAAL9 */
  132. #define KS_ADD_ADDR_9_LO 0x00C8
  133. /* MAAH9 */
  134. #define KS_ADD_ADDR_9_HI 0x00CC
  135. /* MAAL10 */
  136. #define KS_ADD_ADDR_A_LO 0x00D0
  137. /* MAAH10 */
  138. #define KS_ADD_ADDR_A_HI 0x00D4
  139. /* MAAL11 */
  140. #define KS_ADD_ADDR_B_LO 0x00D8
  141. /* MAAH11 */
  142. #define KS_ADD_ADDR_B_HI 0x00DC
  143. /* MAAL12 */
  144. #define KS_ADD_ADDR_C_LO 0x00E0
  145. /* MAAH12 */
  146. #define KS_ADD_ADDR_C_HI 0x00E4
  147. /* MAAL13 */
  148. #define KS_ADD_ADDR_D_LO 0x00E8
  149. /* MAAH13 */
  150. #define KS_ADD_ADDR_D_HI 0x00EC
  151. /* MAAL14 */
  152. #define KS_ADD_ADDR_E_LO 0x00F0
  153. /* MAAH14 */
  154. #define KS_ADD_ADDR_E_HI 0x00F4
  155. /* MAAL15 */
  156. #define KS_ADD_ADDR_F_LO 0x00F8
  157. /* MAAH15 */
  158. #define KS_ADD_ADDR_F_HI 0x00FC
  159. #define ADD_ADDR_HI_MASK 0x0000FFFF
  160. #define ADD_ADDR_ENABLE 0x80000000
  161. #define ADD_ADDR_INCR 8
  162. /* Miscellaneous Registers */
  163. /* MARL */
  164. #define KS884X_ADDR_0_OFFSET 0x0200
  165. #define KS884X_ADDR_1_OFFSET 0x0201
  166. /* MARM */
  167. #define KS884X_ADDR_2_OFFSET 0x0202
  168. #define KS884X_ADDR_3_OFFSET 0x0203
  169. /* MARH */
  170. #define KS884X_ADDR_4_OFFSET 0x0204
  171. #define KS884X_ADDR_5_OFFSET 0x0205
  172. /* OBCR */
  173. #define KS884X_BUS_CTRL_OFFSET 0x0210
  174. #define BUS_SPEED_125_MHZ 0x0000
  175. #define BUS_SPEED_62_5_MHZ 0x0001
  176. #define BUS_SPEED_41_66_MHZ 0x0002
  177. #define BUS_SPEED_25_MHZ 0x0003
  178. /* EEPCR */
  179. #define KS884X_EEPROM_CTRL_OFFSET 0x0212
  180. #define EEPROM_CHIP_SELECT 0x0001
  181. #define EEPROM_SERIAL_CLOCK 0x0002
  182. #define EEPROM_DATA_OUT 0x0004
  183. #define EEPROM_DATA_IN 0x0008
  184. #define EEPROM_ACCESS_ENABLE 0x0010
  185. /* MBIR */
  186. #define KS884X_MEM_INFO_OFFSET 0x0214
  187. #define RX_MEM_TEST_FAILED 0x0008
  188. #define RX_MEM_TEST_FINISHED 0x0010
  189. #define TX_MEM_TEST_FAILED 0x0800
  190. #define TX_MEM_TEST_FINISHED 0x1000
  191. /* GCR */
  192. #define KS884X_GLOBAL_CTRL_OFFSET 0x0216
  193. #define GLOBAL_SOFTWARE_RESET 0x0001
  194. #define KS8841_POWER_MANAGE_OFFSET 0x0218
  195. /* WFCR */
  196. #define KS8841_WOL_CTRL_OFFSET 0x021A
  197. #define KS8841_WOL_MAGIC_ENABLE 0x0080
  198. #define KS8841_WOL_FRAME3_ENABLE 0x0008
  199. #define KS8841_WOL_FRAME2_ENABLE 0x0004
  200. #define KS8841_WOL_FRAME1_ENABLE 0x0002
  201. #define KS8841_WOL_FRAME0_ENABLE 0x0001
  202. /* WF0 */
  203. #define KS8841_WOL_FRAME_CRC_OFFSET 0x0220
  204. #define KS8841_WOL_FRAME_BYTE0_OFFSET 0x0224
  205. #define KS8841_WOL_FRAME_BYTE2_OFFSET 0x0228
  206. /* IACR */
  207. #define KS884X_IACR_P 0x04A0
  208. #define KS884X_IACR_OFFSET KS884X_IACR_P
  209. /* IADR1 */
  210. #define KS884X_IADR1_P 0x04A2
  211. #define KS884X_IADR2_P 0x04A4
  212. #define KS884X_IADR3_P 0x04A6
  213. #define KS884X_IADR4_P 0x04A8
  214. #define KS884X_IADR5_P 0x04AA
  215. #define KS884X_ACC_CTRL_SEL_OFFSET KS884X_IACR_P
  216. #define KS884X_ACC_CTRL_INDEX_OFFSET (KS884X_ACC_CTRL_SEL_OFFSET + 1)
  217. #define KS884X_ACC_DATA_0_OFFSET KS884X_IADR4_P
  218. #define KS884X_ACC_DATA_1_OFFSET (KS884X_ACC_DATA_0_OFFSET + 1)
  219. #define KS884X_ACC_DATA_2_OFFSET KS884X_IADR5_P
  220. #define KS884X_ACC_DATA_3_OFFSET (KS884X_ACC_DATA_2_OFFSET + 1)
  221. #define KS884X_ACC_DATA_4_OFFSET KS884X_IADR2_P
  222. #define KS884X_ACC_DATA_5_OFFSET (KS884X_ACC_DATA_4_OFFSET + 1)
  223. #define KS884X_ACC_DATA_6_OFFSET KS884X_IADR3_P
  224. #define KS884X_ACC_DATA_7_OFFSET (KS884X_ACC_DATA_6_OFFSET + 1)
  225. #define KS884X_ACC_DATA_8_OFFSET KS884X_IADR1_P
  226. /* P1MBCR */
  227. #define KS884X_P1MBCR_P 0x04D0
  228. #define KS884X_P1MBSR_P 0x04D2
  229. #define KS884X_PHY1ILR_P 0x04D4
  230. #define KS884X_PHY1IHR_P 0x04D6
  231. #define KS884X_P1ANAR_P 0x04D8
  232. #define KS884X_P1ANLPR_P 0x04DA
  233. /* P2MBCR */
  234. #define KS884X_P2MBCR_P 0x04E0
  235. #define KS884X_P2MBSR_P 0x04E2
  236. #define KS884X_PHY2ILR_P 0x04E4
  237. #define KS884X_PHY2IHR_P 0x04E6
  238. #define KS884X_P2ANAR_P 0x04E8
  239. #define KS884X_P2ANLPR_P 0x04EA
  240. #define KS884X_PHY_1_CTRL_OFFSET KS884X_P1MBCR_P
  241. #define PHY_CTRL_INTERVAL (KS884X_P2MBCR_P - KS884X_P1MBCR_P)
  242. #define KS884X_PHY_CTRL_OFFSET 0x00
  243. /* Mode Control Register */
  244. #define PHY_REG_CTRL 0
  245. #define PHY_RESET 0x8000
  246. #define PHY_LOOPBACK 0x4000
  247. #define PHY_SPEED_100MBIT 0x2000
  248. #define PHY_AUTO_NEG_ENABLE 0x1000
  249. #define PHY_POWER_DOWN 0x0800
  250. #define PHY_MII_DISABLE 0x0400
  251. #define PHY_AUTO_NEG_RESTART 0x0200
  252. #define PHY_FULL_DUPLEX 0x0100
  253. #define PHY_COLLISION_TEST 0x0080
  254. #define PHY_HP_MDIX 0x0020
  255. #define PHY_FORCE_MDIX 0x0010
  256. #define PHY_AUTO_MDIX_DISABLE 0x0008
  257. #define PHY_REMOTE_FAULT_DISABLE 0x0004
  258. #define PHY_TRANSMIT_DISABLE 0x0002
  259. #define PHY_LED_DISABLE 0x0001
  260. #define KS884X_PHY_STATUS_OFFSET 0x02
  261. /* Mode Status Register */
  262. #define PHY_REG_STATUS 1
  263. #define PHY_100BT4_CAPABLE 0x8000
  264. #define PHY_100BTX_FD_CAPABLE 0x4000
  265. #define PHY_100BTX_CAPABLE 0x2000
  266. #define PHY_10BT_FD_CAPABLE 0x1000
  267. #define PHY_10BT_CAPABLE 0x0800
  268. #define PHY_MII_SUPPRESS_CAPABLE 0x0040
  269. #define PHY_AUTO_NEG_ACKNOWLEDGE 0x0020
  270. #define PHY_REMOTE_FAULT 0x0010
  271. #define PHY_AUTO_NEG_CAPABLE 0x0008
  272. #define PHY_LINK_STATUS 0x0004
  273. #define PHY_JABBER_DETECT 0x0002
  274. #define PHY_EXTENDED_CAPABILITY 0x0001
  275. #define KS884X_PHY_ID_1_OFFSET 0x04
  276. #define KS884X_PHY_ID_2_OFFSET 0x06
  277. /* PHY Identifier Registers */
  278. #define PHY_REG_ID_1 2
  279. #define PHY_REG_ID_2 3
  280. #define KS884X_PHY_AUTO_NEG_OFFSET 0x08
  281. /* Auto-Negotiation Advertisement Register */
  282. #define PHY_REG_AUTO_NEGOTIATION 4
  283. #define PHY_AUTO_NEG_NEXT_PAGE 0x8000
  284. #define PHY_AUTO_NEG_REMOTE_FAULT 0x2000
  285. /* Not supported. */
  286. #define PHY_AUTO_NEG_ASYM_PAUSE 0x0800
  287. #define PHY_AUTO_NEG_SYM_PAUSE 0x0400
  288. #define PHY_AUTO_NEG_100BT4 0x0200
  289. #define PHY_AUTO_NEG_100BTX_FD 0x0100
  290. #define PHY_AUTO_NEG_100BTX 0x0080
  291. #define PHY_AUTO_NEG_10BT_FD 0x0040
  292. #define PHY_AUTO_NEG_10BT 0x0020
  293. #define PHY_AUTO_NEG_SELECTOR 0x001F
  294. #define PHY_AUTO_NEG_802_3 0x0001
  295. #define PHY_AUTO_NEG_PAUSE (PHY_AUTO_NEG_SYM_PAUSE | PHY_AUTO_NEG_ASYM_PAUSE)
  296. #define KS884X_PHY_REMOTE_CAP_OFFSET 0x0A
  297. /* Auto-Negotiation Link Partner Ability Register */
  298. #define PHY_REG_REMOTE_CAPABILITY 5
  299. #define PHY_REMOTE_NEXT_PAGE 0x8000
  300. #define PHY_REMOTE_ACKNOWLEDGE 0x4000
  301. #define PHY_REMOTE_REMOTE_FAULT 0x2000
  302. #define PHY_REMOTE_SYM_PAUSE 0x0400
  303. #define PHY_REMOTE_100BTX_FD 0x0100
  304. #define PHY_REMOTE_100BTX 0x0080
  305. #define PHY_REMOTE_10BT_FD 0x0040
  306. #define PHY_REMOTE_10BT 0x0020
  307. /* P1VCT */
  308. #define KS884X_P1VCT_P 0x04F0
  309. #define KS884X_P1PHYCTRL_P 0x04F2
  310. /* P2VCT */
  311. #define KS884X_P2VCT_P 0x04F4
  312. #define KS884X_P2PHYCTRL_P 0x04F6
  313. #define KS884X_PHY_SPECIAL_OFFSET KS884X_P1VCT_P
  314. #define PHY_SPECIAL_INTERVAL (KS884X_P2VCT_P - KS884X_P1VCT_P)
  315. #define KS884X_PHY_LINK_MD_OFFSET 0x00
  316. #define PHY_START_CABLE_DIAG 0x8000
  317. #define PHY_CABLE_DIAG_RESULT 0x6000
  318. #define PHY_CABLE_STAT_NORMAL 0x0000
  319. #define PHY_CABLE_STAT_OPEN 0x2000
  320. #define PHY_CABLE_STAT_SHORT 0x4000
  321. #define PHY_CABLE_STAT_FAILED 0x6000
  322. #define PHY_CABLE_10M_SHORT 0x1000
  323. #define PHY_CABLE_FAULT_COUNTER 0x01FF
  324. #define KS884X_PHY_PHY_CTRL_OFFSET 0x02
  325. #define PHY_STAT_REVERSED_POLARITY 0x0020
  326. #define PHY_STAT_MDIX 0x0010
  327. #define PHY_FORCE_LINK 0x0008
  328. #define PHY_POWER_SAVING_DISABLE 0x0004
  329. #define PHY_REMOTE_LOOPBACK 0x0002
  330. /* SIDER */
  331. #define KS884X_SIDER_P 0x0400
  332. #define KS884X_CHIP_ID_OFFSET KS884X_SIDER_P
  333. #define KS884X_FAMILY_ID_OFFSET (KS884X_CHIP_ID_OFFSET + 1)
  334. #define REG_FAMILY_ID 0x88
  335. #define REG_CHIP_ID_41 0x8810
  336. #define REG_CHIP_ID_42 0x8800
  337. #define KS884X_CHIP_ID_MASK_41 0xFF10
  338. #define KS884X_CHIP_ID_MASK 0xFFF0
  339. #define KS884X_CHIP_ID_SHIFT 4
  340. #define KS884X_REVISION_MASK 0x000E
  341. #define KS884X_REVISION_SHIFT 1
  342. #define KS8842_START 0x0001
  343. #define CHIP_IP_41_M 0x8810
  344. #define CHIP_IP_42_M 0x8800
  345. #define CHIP_IP_61_M 0x8890
  346. #define CHIP_IP_62_M 0x8880
  347. #define CHIP_IP_41_P 0x8850
  348. #define CHIP_IP_42_P 0x8840
  349. #define CHIP_IP_61_P 0x88D0
  350. #define CHIP_IP_62_P 0x88C0
  351. /* SGCR1 */
  352. #define KS8842_SGCR1_P 0x0402
  353. #define KS8842_SWITCH_CTRL_1_OFFSET KS8842_SGCR1_P
  354. #define SWITCH_PASS_ALL 0x8000
  355. #define SWITCH_TX_FLOW_CTRL 0x2000
  356. #define SWITCH_RX_FLOW_CTRL 0x1000
  357. #define SWITCH_CHECK_LENGTH 0x0800
  358. #define SWITCH_AGING_ENABLE 0x0400
  359. #define SWITCH_FAST_AGING 0x0200
  360. #define SWITCH_AGGR_BACKOFF 0x0100
  361. #define SWITCH_PASS_PAUSE 0x0008
  362. #define SWITCH_LINK_AUTO_AGING 0x0001
  363. /* SGCR2 */
  364. #define KS8842_SGCR2_P 0x0404
  365. #define KS8842_SWITCH_CTRL_2_OFFSET KS8842_SGCR2_P
  366. #define SWITCH_VLAN_ENABLE 0x8000
  367. #define SWITCH_IGMP_SNOOP 0x4000
  368. #define IPV6_MLD_SNOOP_ENABLE 0x2000
  369. #define IPV6_MLD_SNOOP_OPTION 0x1000
  370. #define PRIORITY_SCHEME_SELECT 0x0800
  371. #define SWITCH_MIRROR_RX_TX 0x0100
  372. #define UNICAST_VLAN_BOUNDARY 0x0080
  373. #define MULTICAST_STORM_DISABLE 0x0040
  374. #define SWITCH_BACK_PRESSURE 0x0020
  375. #define FAIR_FLOW_CTRL 0x0010
  376. #define NO_EXC_COLLISION_DROP 0x0008
  377. #define SWITCH_HUGE_PACKET 0x0004
  378. #define SWITCH_LEGAL_PACKET 0x0002
  379. #define SWITCH_BUF_RESERVE 0x0001
  380. /* SGCR3 */
  381. #define KS8842_SGCR3_P 0x0406
  382. #define KS8842_SWITCH_CTRL_3_OFFSET KS8842_SGCR3_P
  383. #define BROADCAST_STORM_RATE_LO 0xFF00
  384. #define SWITCH_REPEATER 0x0080
  385. #define SWITCH_HALF_DUPLEX 0x0040
  386. #define SWITCH_FLOW_CTRL 0x0020
  387. #define SWITCH_10_MBIT 0x0010
  388. #define SWITCH_REPLACE_NULL_VID 0x0008
  389. #define BROADCAST_STORM_RATE_HI 0x0007
  390. #define BROADCAST_STORM_RATE 0x07FF
  391. /* SGCR4 */
  392. #define KS8842_SGCR4_P 0x0408
  393. /* SGCR5 */
  394. #define KS8842_SGCR5_P 0x040A
  395. #define KS8842_SWITCH_CTRL_5_OFFSET KS8842_SGCR5_P
  396. #define LED_MODE 0x8200
  397. #define LED_SPEED_DUPLEX_ACT 0x0000
  398. #define LED_SPEED_DUPLEX_LINK_ACT 0x8000
  399. #define LED_DUPLEX_10_100 0x0200
  400. /* SGCR6 */
  401. #define KS8842_SGCR6_P 0x0410
  402. #define KS8842_SWITCH_CTRL_6_OFFSET KS8842_SGCR6_P
  403. #define KS8842_PRIORITY_MASK 3
  404. #define KS8842_PRIORITY_SHIFT 2
  405. /* SGCR7 */
  406. #define KS8842_SGCR7_P 0x0412
  407. #define KS8842_SWITCH_CTRL_7_OFFSET KS8842_SGCR7_P
  408. #define SWITCH_UNK_DEF_PORT_ENABLE 0x0008
  409. #define SWITCH_UNK_DEF_PORT_3 0x0004
  410. #define SWITCH_UNK_DEF_PORT_2 0x0002
  411. #define SWITCH_UNK_DEF_PORT_1 0x0001
  412. /* MACAR1 */
  413. #define KS8842_MACAR1_P 0x0470
  414. #define KS8842_MACAR2_P 0x0472
  415. #define KS8842_MACAR3_P 0x0474
  416. #define KS8842_MAC_ADDR_1_OFFSET KS8842_MACAR1_P
  417. #define KS8842_MAC_ADDR_0_OFFSET (KS8842_MAC_ADDR_1_OFFSET + 1)
  418. #define KS8842_MAC_ADDR_3_OFFSET KS8842_MACAR2_P
  419. #define KS8842_MAC_ADDR_2_OFFSET (KS8842_MAC_ADDR_3_OFFSET + 1)
  420. #define KS8842_MAC_ADDR_5_OFFSET KS8842_MACAR3_P
  421. #define KS8842_MAC_ADDR_4_OFFSET (KS8842_MAC_ADDR_5_OFFSET + 1)
  422. /* TOSR1 */
  423. #define KS8842_TOSR1_P 0x0480
  424. #define KS8842_TOSR2_P 0x0482
  425. #define KS8842_TOSR3_P 0x0484
  426. #define KS8842_TOSR4_P 0x0486
  427. #define KS8842_TOSR5_P 0x0488
  428. #define KS8842_TOSR6_P 0x048A
  429. #define KS8842_TOSR7_P 0x0490
  430. #define KS8842_TOSR8_P 0x0492
  431. #define KS8842_TOS_1_OFFSET KS8842_TOSR1_P
  432. #define KS8842_TOS_2_OFFSET KS8842_TOSR2_P
  433. #define KS8842_TOS_3_OFFSET KS8842_TOSR3_P
  434. #define KS8842_TOS_4_OFFSET KS8842_TOSR4_P
  435. #define KS8842_TOS_5_OFFSET KS8842_TOSR5_P
  436. #define KS8842_TOS_6_OFFSET KS8842_TOSR6_P
  437. #define KS8842_TOS_7_OFFSET KS8842_TOSR7_P
  438. #define KS8842_TOS_8_OFFSET KS8842_TOSR8_P
  439. /* P1CR1 */
  440. #define KS8842_P1CR1_P 0x0500
  441. #define KS8842_P1CR2_P 0x0502
  442. #define KS8842_P1VIDR_P 0x0504
  443. #define KS8842_P1CR3_P 0x0506
  444. #define KS8842_P1IRCR_P 0x0508
  445. #define KS8842_P1ERCR_P 0x050A
  446. #define KS884X_P1SCSLMD_P 0x0510
  447. #define KS884X_P1CR4_P 0x0512
  448. #define KS884X_P1SR_P 0x0514
  449. /* P2CR1 */
  450. #define KS8842_P2CR1_P 0x0520
  451. #define KS8842_P2CR2_P 0x0522
  452. #define KS8842_P2VIDR_P 0x0524
  453. #define KS8842_P2CR3_P 0x0526
  454. #define KS8842_P2IRCR_P 0x0528
  455. #define KS8842_P2ERCR_P 0x052A
  456. #define KS884X_P2SCSLMD_P 0x0530
  457. #define KS884X_P2CR4_P 0x0532
  458. #define KS884X_P2SR_P 0x0534
  459. /* P3CR1 */
  460. #define KS8842_P3CR1_P 0x0540
  461. #define KS8842_P3CR2_P 0x0542
  462. #define KS8842_P3VIDR_P 0x0544
  463. #define KS8842_P3CR3_P 0x0546
  464. #define KS8842_P3IRCR_P 0x0548
  465. #define KS8842_P3ERCR_P 0x054A
  466. #define KS8842_PORT_1_CTRL_1 KS8842_P1CR1_P
  467. #define KS8842_PORT_2_CTRL_1 KS8842_P2CR1_P
  468. #define KS8842_PORT_3_CTRL_1 KS8842_P3CR1_P
  469. #define PORT_CTRL_ADDR(port, addr) \
  470. (addr = KS8842_PORT_1_CTRL_1 + (port) * \
  471. (KS8842_PORT_2_CTRL_1 - KS8842_PORT_1_CTRL_1))
  472. #define KS8842_PORT_CTRL_1_OFFSET 0x00
  473. #define PORT_BROADCAST_STORM 0x0080
  474. #define PORT_DIFFSERV_ENABLE 0x0040
  475. #define PORT_802_1P_ENABLE 0x0020
  476. #define PORT_BASED_PRIORITY_MASK 0x0018
  477. #define PORT_BASED_PRIORITY_BASE 0x0003
  478. #define PORT_BASED_PRIORITY_SHIFT 3
  479. #define PORT_BASED_PRIORITY_0 0x0000
  480. #define PORT_BASED_PRIORITY_1 0x0008
  481. #define PORT_BASED_PRIORITY_2 0x0010
  482. #define PORT_BASED_PRIORITY_3 0x0018
  483. #define PORT_INSERT_TAG 0x0004
  484. #define PORT_REMOVE_TAG 0x0002
  485. #define PORT_PRIO_QUEUE_ENABLE 0x0001
  486. #define KS8842_PORT_CTRL_2_OFFSET 0x02
  487. #define PORT_INGRESS_VLAN_FILTER 0x4000
  488. #define PORT_DISCARD_NON_VID 0x2000
  489. #define PORT_FORCE_FLOW_CTRL 0x1000
  490. #define PORT_BACK_PRESSURE 0x0800
  491. #define PORT_TX_ENABLE 0x0400
  492. #define PORT_RX_ENABLE 0x0200
  493. #define PORT_LEARN_DISABLE 0x0100
  494. #define PORT_MIRROR_SNIFFER 0x0080
  495. #define PORT_MIRROR_RX 0x0040
  496. #define PORT_MIRROR_TX 0x0020
  497. #define PORT_USER_PRIORITY_CEILING 0x0008
  498. #define PORT_VLAN_MEMBERSHIP 0x0007
  499. #define KS8842_PORT_CTRL_VID_OFFSET 0x04
  500. #define PORT_DEFAULT_VID 0x0001
  501. #define KS8842_PORT_CTRL_3_OFFSET 0x06
  502. #define PORT_INGRESS_LIMIT_MODE 0x000C
  503. #define PORT_INGRESS_ALL 0x0000
  504. #define PORT_INGRESS_UNICAST 0x0004
  505. #define PORT_INGRESS_MULTICAST 0x0008
  506. #define PORT_INGRESS_BROADCAST 0x000C
  507. #define PORT_COUNT_IFG 0x0002
  508. #define PORT_COUNT_PREAMBLE 0x0001
  509. #define KS8842_PORT_IN_RATE_OFFSET 0x08
  510. #define KS8842_PORT_OUT_RATE_OFFSET 0x0A
  511. #define PORT_PRIORITY_RATE 0x0F
  512. #define PORT_PRIORITY_RATE_SHIFT 4
  513. #define KS884X_PORT_LINK_MD 0x10
  514. #define PORT_CABLE_10M_SHORT 0x8000
  515. #define PORT_CABLE_DIAG_RESULT 0x6000
  516. #define PORT_CABLE_STAT_NORMAL 0x0000
  517. #define PORT_CABLE_STAT_OPEN 0x2000
  518. #define PORT_CABLE_STAT_SHORT 0x4000
  519. #define PORT_CABLE_STAT_FAILED 0x6000
  520. #define PORT_START_CABLE_DIAG 0x1000
  521. #define PORT_FORCE_LINK 0x0800
  522. #define PORT_POWER_SAVING_DISABLE 0x0400
  523. #define PORT_PHY_REMOTE_LOOPBACK 0x0200
  524. #define PORT_CABLE_FAULT_COUNTER 0x01FF
  525. #define KS884X_PORT_CTRL_4_OFFSET 0x12
  526. #define PORT_LED_OFF 0x8000
  527. #define PORT_TX_DISABLE 0x4000
  528. #define PORT_AUTO_NEG_RESTART 0x2000
  529. #define PORT_REMOTE_FAULT_DISABLE 0x1000
  530. #define PORT_POWER_DOWN 0x0800
  531. #define PORT_AUTO_MDIX_DISABLE 0x0400
  532. #define PORT_FORCE_MDIX 0x0200
  533. #define PORT_LOOPBACK 0x0100
  534. #define PORT_AUTO_NEG_ENABLE 0x0080
  535. #define PORT_FORCE_100_MBIT 0x0040
  536. #define PORT_FORCE_FULL_DUPLEX 0x0020
  537. #define PORT_AUTO_NEG_SYM_PAUSE 0x0010
  538. #define PORT_AUTO_NEG_100BTX_FD 0x0008
  539. #define PORT_AUTO_NEG_100BTX 0x0004
  540. #define PORT_AUTO_NEG_10BT_FD 0x0002
  541. #define PORT_AUTO_NEG_10BT 0x0001
  542. #define KS884X_PORT_STATUS_OFFSET 0x14
  543. #define PORT_HP_MDIX 0x8000
  544. #define PORT_REVERSED_POLARITY 0x2000
  545. #define PORT_RX_FLOW_CTRL 0x0800
  546. #define PORT_TX_FLOW_CTRL 0x1000
  547. #define PORT_STATUS_SPEED_100MBIT 0x0400
  548. #define PORT_STATUS_FULL_DUPLEX 0x0200
  549. #define PORT_REMOTE_FAULT 0x0100
  550. #define PORT_MDIX_STATUS 0x0080
  551. #define PORT_AUTO_NEG_COMPLETE 0x0040
  552. #define PORT_STATUS_LINK_GOOD 0x0020
  553. #define PORT_REMOTE_SYM_PAUSE 0x0010
  554. #define PORT_REMOTE_100BTX_FD 0x0008
  555. #define PORT_REMOTE_100BTX 0x0004
  556. #define PORT_REMOTE_10BT_FD 0x0002
  557. #define PORT_REMOTE_10BT 0x0001
  558. /*
  559. #define STATIC_MAC_TABLE_ADDR 00-0000FFFF-FFFFFFFF
  560. #define STATIC_MAC_TABLE_FWD_PORTS 00-00070000-00000000
  561. #define STATIC_MAC_TABLE_VALID 00-00080000-00000000
  562. #define STATIC_MAC_TABLE_OVERRIDE 00-00100000-00000000
  563. #define STATIC_MAC_TABLE_USE_FID 00-00200000-00000000
  564. #define STATIC_MAC_TABLE_FID 00-03C00000-00000000
  565. */
  566. #define STATIC_MAC_TABLE_ADDR 0x0000FFFF
  567. #define STATIC_MAC_TABLE_FWD_PORTS 0x00070000
  568. #define STATIC_MAC_TABLE_VALID 0x00080000
  569. #define STATIC_MAC_TABLE_OVERRIDE 0x00100000
  570. #define STATIC_MAC_TABLE_USE_FID 0x00200000
  571. #define STATIC_MAC_TABLE_FID 0x03C00000
  572. #define STATIC_MAC_FWD_PORTS_SHIFT 16
  573. #define STATIC_MAC_FID_SHIFT 22
  574. /*
  575. #define VLAN_TABLE_VID 00-00000000-00000FFF
  576. #define VLAN_TABLE_FID 00-00000000-0000F000
  577. #define VLAN_TABLE_MEMBERSHIP 00-00000000-00070000
  578. #define VLAN_TABLE_VALID 00-00000000-00080000
  579. */
  580. #define VLAN_TABLE_VID 0x00000FFF
  581. #define VLAN_TABLE_FID 0x0000F000
  582. #define VLAN_TABLE_MEMBERSHIP 0x00070000
  583. #define VLAN_TABLE_VALID 0x00080000
  584. #define VLAN_TABLE_FID_SHIFT 12
  585. #define VLAN_TABLE_MEMBERSHIP_SHIFT 16
  586. /*
  587. #define DYNAMIC_MAC_TABLE_ADDR 00-0000FFFF-FFFFFFFF
  588. #define DYNAMIC_MAC_TABLE_FID 00-000F0000-00000000
  589. #define DYNAMIC_MAC_TABLE_SRC_PORT 00-00300000-00000000
  590. #define DYNAMIC_MAC_TABLE_TIMESTAMP 00-00C00000-00000000
  591. #define DYNAMIC_MAC_TABLE_ENTRIES 03-FF000000-00000000
  592. #define DYNAMIC_MAC_TABLE_MAC_EMPTY 04-00000000-00000000
  593. #define DYNAMIC_MAC_TABLE_RESERVED 78-00000000-00000000
  594. #define DYNAMIC_MAC_TABLE_NOT_READY 80-00000000-00000000
  595. */
  596. #define DYNAMIC_MAC_TABLE_ADDR 0x0000FFFF
  597. #define DYNAMIC_MAC_TABLE_FID 0x000F0000
  598. #define DYNAMIC_MAC_TABLE_SRC_PORT 0x00300000
  599. #define DYNAMIC_MAC_TABLE_TIMESTAMP 0x00C00000
  600. #define DYNAMIC_MAC_TABLE_ENTRIES 0xFF000000
  601. #define DYNAMIC_MAC_TABLE_ENTRIES_H 0x03
  602. #define DYNAMIC_MAC_TABLE_MAC_EMPTY 0x04
  603. #define DYNAMIC_MAC_TABLE_RESERVED 0x78
  604. #define DYNAMIC_MAC_TABLE_NOT_READY 0x80
  605. #define DYNAMIC_MAC_FID_SHIFT 16
  606. #define DYNAMIC_MAC_SRC_PORT_SHIFT 20
  607. #define DYNAMIC_MAC_TIMESTAMP_SHIFT 22
  608. #define DYNAMIC_MAC_ENTRIES_SHIFT 24
  609. #define DYNAMIC_MAC_ENTRIES_H_SHIFT 8
  610. /*
  611. #define MIB_COUNTER_VALUE 00-00000000-3FFFFFFF
  612. #define MIB_COUNTER_VALID 00-00000000-40000000
  613. #define MIB_COUNTER_OVERFLOW 00-00000000-80000000
  614. */
  615. #define MIB_COUNTER_VALUE 0x3FFFFFFF
  616. #define MIB_COUNTER_VALID 0x40000000
  617. #define MIB_COUNTER_OVERFLOW 0x80000000
  618. #define MIB_PACKET_DROPPED 0x0000FFFF
  619. #define KS_MIB_PACKET_DROPPED_TX_0 0x100
  620. #define KS_MIB_PACKET_DROPPED_TX_1 0x101
  621. #define KS_MIB_PACKET_DROPPED_TX 0x102
  622. #define KS_MIB_PACKET_DROPPED_RX_0 0x103
  623. #define KS_MIB_PACKET_DROPPED_RX_1 0x104
  624. #define KS_MIB_PACKET_DROPPED_RX 0x105
  625. /* Change default LED mode. */
  626. #define SET_DEFAULT_LED LED_SPEED_DUPLEX_ACT
  627. #define MAC_ADDR_LEN 6
  628. #define MAC_ADDR_ORDER(i) (MAC_ADDR_LEN - 1 - (i))
  629. #define MAX_ETHERNET_BODY_SIZE 1500
  630. #define ETHERNET_HEADER_SIZE 14
  631. #define MAX_ETHERNET_PACKET_SIZE \
  632. (MAX_ETHERNET_BODY_SIZE + ETHERNET_HEADER_SIZE)
  633. #define REGULAR_RX_BUF_SIZE (MAX_ETHERNET_PACKET_SIZE + 4)
  634. #define MAX_RX_BUF_SIZE (1912 + 4)
  635. #define ADDITIONAL_ENTRIES 16
  636. #define MAX_MULTICAST_LIST 32
  637. #define HW_MULTICAST_SIZE 8
  638. #define HW_TO_DEV_PORT(port) (port - 1)
  639. enum {
  640. media_connected,
  641. media_disconnected
  642. };
  643. enum {
  644. OID_COUNTER_UNKOWN,
  645. OID_COUNTER_FIRST,
  646. /* total transmit errors */
  647. OID_COUNTER_XMIT_ERROR,
  648. /* total receive errors */
  649. OID_COUNTER_RCV_ERROR,
  650. OID_COUNTER_LAST
  651. };
  652. /*
  653. * Hardware descriptor definitions
  654. */
  655. #define DESC_ALIGNMENT 16
  656. #define BUFFER_ALIGNMENT 8
  657. #define NUM_OF_RX_DESC 64
  658. #define NUM_OF_TX_DESC 64
  659. #define KS_DESC_RX_FRAME_LEN 0x000007FF
  660. #define KS_DESC_RX_FRAME_TYPE 0x00008000
  661. #define KS_DESC_RX_ERROR_CRC 0x00010000
  662. #define KS_DESC_RX_ERROR_RUNT 0x00020000
  663. #define KS_DESC_RX_ERROR_TOO_LONG 0x00040000
  664. #define KS_DESC_RX_ERROR_PHY 0x00080000
  665. #define KS884X_DESC_RX_PORT_MASK 0x00300000
  666. #define KS_DESC_RX_MULTICAST 0x01000000
  667. #define KS_DESC_RX_ERROR 0x02000000
  668. #define KS_DESC_RX_ERROR_CSUM_UDP 0x04000000
  669. #define KS_DESC_RX_ERROR_CSUM_TCP 0x08000000
  670. #define KS_DESC_RX_ERROR_CSUM_IP 0x10000000
  671. #define KS_DESC_RX_LAST 0x20000000
  672. #define KS_DESC_RX_FIRST 0x40000000
  673. #define KS_DESC_RX_ERROR_COND \
  674. (KS_DESC_RX_ERROR_CRC | \
  675. KS_DESC_RX_ERROR_RUNT | \
  676. KS_DESC_RX_ERROR_PHY | \
  677. KS_DESC_RX_ERROR_TOO_LONG)
  678. #define KS_DESC_HW_OWNED 0x80000000
  679. #define KS_DESC_BUF_SIZE 0x000007FF
  680. #define KS884X_DESC_TX_PORT_MASK 0x00300000
  681. #define KS_DESC_END_OF_RING 0x02000000
  682. #define KS_DESC_TX_CSUM_GEN_UDP 0x04000000
  683. #define KS_DESC_TX_CSUM_GEN_TCP 0x08000000
  684. #define KS_DESC_TX_CSUM_GEN_IP 0x10000000
  685. #define KS_DESC_TX_LAST 0x20000000
  686. #define KS_DESC_TX_FIRST 0x40000000
  687. #define KS_DESC_TX_INTERRUPT 0x80000000
  688. #define KS_DESC_PORT_SHIFT 20
  689. #define KS_DESC_RX_MASK (KS_DESC_BUF_SIZE)
  690. #define KS_DESC_TX_MASK \
  691. (KS_DESC_TX_INTERRUPT | \
  692. KS_DESC_TX_FIRST | \
  693. KS_DESC_TX_LAST | \
  694. KS_DESC_TX_CSUM_GEN_IP | \
  695. KS_DESC_TX_CSUM_GEN_TCP | \
  696. KS_DESC_TX_CSUM_GEN_UDP | \
  697. KS_DESC_BUF_SIZE)
  698. struct ksz_desc_rx_stat {
  699. #ifdef __BIG_ENDIAN_BITFIELD
  700. u32 hw_owned:1;
  701. u32 first_desc:1;
  702. u32 last_desc:1;
  703. u32 csum_err_ip:1;
  704. u32 csum_err_tcp:1;
  705. u32 csum_err_udp:1;
  706. u32 error:1;
  707. u32 multicast:1;
  708. u32 src_port:4;
  709. u32 err_phy:1;
  710. u32 err_too_long:1;
  711. u32 err_runt:1;
  712. u32 err_crc:1;
  713. u32 frame_type:1;
  714. u32 reserved1:4;
  715. u32 frame_len:11;
  716. #else
  717. u32 frame_len:11;
  718. u32 reserved1:4;
  719. u32 frame_type:1;
  720. u32 err_crc:1;
  721. u32 err_runt:1;
  722. u32 err_too_long:1;
  723. u32 err_phy:1;
  724. u32 src_port:4;
  725. u32 multicast:1;
  726. u32 error:1;
  727. u32 csum_err_udp:1;
  728. u32 csum_err_tcp:1;
  729. u32 csum_err_ip:1;
  730. u32 last_desc:1;
  731. u32 first_desc:1;
  732. u32 hw_owned:1;
  733. #endif
  734. };
  735. struct ksz_desc_tx_stat {
  736. #ifdef __BIG_ENDIAN_BITFIELD
  737. u32 hw_owned:1;
  738. u32 reserved1:31;
  739. #else
  740. u32 reserved1:31;
  741. u32 hw_owned:1;
  742. #endif
  743. };
  744. struct ksz_desc_rx_buf {
  745. #ifdef __BIG_ENDIAN_BITFIELD
  746. u32 reserved4:6;
  747. u32 end_of_ring:1;
  748. u32 reserved3:14;
  749. u32 buf_size:11;
  750. #else
  751. u32 buf_size:11;
  752. u32 reserved3:14;
  753. u32 end_of_ring:1;
  754. u32 reserved4:6;
  755. #endif
  756. };
  757. struct ksz_desc_tx_buf {
  758. #ifdef __BIG_ENDIAN_BITFIELD
  759. u32 intr:1;
  760. u32 first_seg:1;
  761. u32 last_seg:1;
  762. u32 csum_gen_ip:1;
  763. u32 csum_gen_tcp:1;
  764. u32 csum_gen_udp:1;
  765. u32 end_of_ring:1;
  766. u32 reserved4:1;
  767. u32 dest_port:4;
  768. u32 reserved3:9;
  769. u32 buf_size:11;
  770. #else
  771. u32 buf_size:11;
  772. u32 reserved3:9;
  773. u32 dest_port:4;
  774. u32 reserved4:1;
  775. u32 end_of_ring:1;
  776. u32 csum_gen_udp:1;
  777. u32 csum_gen_tcp:1;
  778. u32 csum_gen_ip:1;
  779. u32 last_seg:1;
  780. u32 first_seg:1;
  781. u32 intr:1;
  782. #endif
  783. };
  784. union desc_stat {
  785. struct ksz_desc_rx_stat rx;
  786. struct ksz_desc_tx_stat tx;
  787. u32 data;
  788. };
  789. union desc_buf {
  790. struct ksz_desc_rx_buf rx;
  791. struct ksz_desc_tx_buf tx;
  792. u32 data;
  793. };
  794. /**
  795. * struct ksz_hw_desc - Hardware descriptor data structure
  796. * @ctrl: Descriptor control value.
  797. * @buf: Descriptor buffer value.
  798. * @addr: Physical address of memory buffer.
  799. * @next: Pointer to next hardware descriptor.
  800. */
  801. struct ksz_hw_desc {
  802. union desc_stat ctrl;
  803. union desc_buf buf;
  804. u32 addr;
  805. u32 next;
  806. };
  807. /**
  808. * struct ksz_sw_desc - Software descriptor data structure
  809. * @ctrl: Descriptor control value.
  810. * @buf: Descriptor buffer value.
  811. * @buf_size: Current buffers size value in hardware descriptor.
  812. */
  813. struct ksz_sw_desc {
  814. union desc_stat ctrl;
  815. union desc_buf buf;
  816. u32 buf_size;
  817. };
  818. /**
  819. * struct ksz_dma_buf - OS dependent DMA buffer data structure
  820. * @skb: Associated socket buffer.
  821. * @dma: Associated physical DMA address.
  822. * len: Actual len used.
  823. */
  824. struct ksz_dma_buf {
  825. struct sk_buff *skb;
  826. dma_addr_t dma;
  827. int len;
  828. };
  829. /**
  830. * struct ksz_desc - Descriptor structure
  831. * @phw: Hardware descriptor pointer to uncached physical memory.
  832. * @sw: Cached memory to hold hardware descriptor values for
  833. * manipulation.
  834. * @dma_buf: Operating system dependent data structure to hold physical
  835. * memory buffer allocation information.
  836. */
  837. struct ksz_desc {
  838. struct ksz_hw_desc *phw;
  839. struct ksz_sw_desc sw;
  840. struct ksz_dma_buf dma_buf;
  841. };
  842. #define DMA_BUFFER(desc) ((struct ksz_dma_buf *)(&(desc)->dma_buf))
  843. /**
  844. * struct ksz_desc_info - Descriptor information data structure
  845. * @ring: First descriptor in the ring.
  846. * @cur: Current descriptor being manipulated.
  847. * @ring_virt: First hardware descriptor in the ring.
  848. * @ring_phys: The physical address of the first descriptor of the ring.
  849. * @size: Size of hardware descriptor.
  850. * @alloc: Number of descriptors allocated.
  851. * @avail: Number of descriptors available for use.
  852. * @last: Index for last descriptor released to hardware.
  853. * @next: Index for next descriptor available for use.
  854. * @mask: Mask for index wrapping.
  855. */
  856. struct ksz_desc_info {
  857. struct ksz_desc *ring;
  858. struct ksz_desc *cur;
  859. struct ksz_hw_desc *ring_virt;
  860. u32 ring_phys;
  861. int size;
  862. int alloc;
  863. int avail;
  864. int last;
  865. int next;
  866. int mask;
  867. };
  868. /*
  869. * KSZ8842 switch definitions
  870. */
  871. enum {
  872. TABLE_STATIC_MAC = 0,
  873. TABLE_VLAN,
  874. TABLE_DYNAMIC_MAC,
  875. TABLE_MIB
  876. };
  877. #define LEARNED_MAC_TABLE_ENTRIES 1024
  878. #define STATIC_MAC_TABLE_ENTRIES 8
  879. /**
  880. * struct ksz_mac_table - Static MAC table data structure
  881. * @mac_addr: MAC address to filter.
  882. * @vid: VID value.
  883. * @fid: FID value.
  884. * @ports: Port membership.
  885. * @override: Override setting.
  886. * @use_fid: FID use setting.
  887. * @valid: Valid setting indicating the entry is being used.
  888. */
  889. struct ksz_mac_table {
  890. u8 mac_addr[MAC_ADDR_LEN];
  891. u16 vid;
  892. u8 fid;
  893. u8 ports;
  894. u8 override:1;
  895. u8 use_fid:1;
  896. u8 valid:1;
  897. };
  898. #define VLAN_TABLE_ENTRIES 16
  899. /**
  900. * struct ksz_vlan_table - VLAN table data structure
  901. * @vid: VID value.
  902. * @fid: FID value.
  903. * @member: Port membership.
  904. */
  905. struct ksz_vlan_table {
  906. u16 vid;
  907. u8 fid;
  908. u8 member;
  909. };
  910. #define DIFFSERV_ENTRIES 64
  911. #define PRIO_802_1P_ENTRIES 8
  912. #define PRIO_QUEUES 4
  913. #define SWITCH_PORT_NUM 2
  914. #define TOTAL_PORT_NUM (SWITCH_PORT_NUM + 1)
  915. #define HOST_MASK (1 << SWITCH_PORT_NUM)
  916. #define PORT_MASK 7
  917. #define MAIN_PORT 0
  918. #define OTHER_PORT 1
  919. #define HOST_PORT SWITCH_PORT_NUM
  920. #define PORT_COUNTER_NUM 0x20
  921. #define TOTAL_PORT_COUNTER_NUM (PORT_COUNTER_NUM + 2)
  922. #define MIB_COUNTER_RX_LO_PRIORITY 0x00
  923. #define MIB_COUNTER_RX_HI_PRIORITY 0x01
  924. #define MIB_COUNTER_RX_UNDERSIZE 0x02
  925. #define MIB_COUNTER_RX_FRAGMENT 0x03
  926. #define MIB_COUNTER_RX_OVERSIZE 0x04
  927. #define MIB_COUNTER_RX_JABBER 0x05
  928. #define MIB_COUNTER_RX_SYMBOL_ERR 0x06
  929. #define MIB_COUNTER_RX_CRC_ERR 0x07
  930. #define MIB_COUNTER_RX_ALIGNMENT_ERR 0x08
  931. #define MIB_COUNTER_RX_CTRL_8808 0x09
  932. #define MIB_COUNTER_RX_PAUSE 0x0A
  933. #define MIB_COUNTER_RX_BROADCAST 0x0B
  934. #define MIB_COUNTER_RX_MULTICAST 0x0C
  935. #define MIB_COUNTER_RX_UNICAST 0x0D
  936. #define MIB_COUNTER_RX_OCTET_64 0x0E
  937. #define MIB_COUNTER_RX_OCTET_65_127 0x0F
  938. #define MIB_COUNTER_RX_OCTET_128_255 0x10
  939. #define MIB_COUNTER_RX_OCTET_256_511 0x11
  940. #define MIB_COUNTER_RX_OCTET_512_1023 0x12
  941. #define MIB_COUNTER_RX_OCTET_1024_1522 0x13
  942. #define MIB_COUNTER_TX_LO_PRIORITY 0x14
  943. #define MIB_COUNTER_TX_HI_PRIORITY 0x15
  944. #define MIB_COUNTER_TX_LATE_COLLISION 0x16
  945. #define MIB_COUNTER_TX_PAUSE 0x17
  946. #define MIB_COUNTER_TX_BROADCAST 0x18
  947. #define MIB_COUNTER_TX_MULTICAST 0x19
  948. #define MIB_COUNTER_TX_UNICAST 0x1A
  949. #define MIB_COUNTER_TX_DEFERRED 0x1B
  950. #define MIB_COUNTER_TX_TOTAL_COLLISION 0x1C
  951. #define MIB_COUNTER_TX_EXCESS_COLLISION 0x1D
  952. #define MIB_COUNTER_TX_SINGLE_COLLISION 0x1E
  953. #define MIB_COUNTER_TX_MULTI_COLLISION 0x1F
  954. #define MIB_COUNTER_RX_DROPPED_PACKET 0x20
  955. #define MIB_COUNTER_TX_DROPPED_PACKET 0x21
  956. /**
  957. * struct ksz_port_mib - Port MIB data structure
  958. * @cnt_ptr: Current pointer to MIB counter index.
  959. * @link_down: Indication the link has just gone down.
  960. * @state: Connection status of the port.
  961. * @mib_start: The starting counter index. Some ports do not start at 0.
  962. * @counter: 64-bit MIB counter value.
  963. * @dropped: Temporary buffer to remember last read packet dropped values.
  964. *
  965. * MIB counters needs to be read periodically so that counters do not get
  966. * overflowed and give incorrect values. A right balance is needed to
  967. * satisfy this condition and not waste too much CPU time.
  968. *
  969. * It is pointless to read MIB counters when the port is disconnected. The
  970. * @state provides the connection status so that MIB counters are read only
  971. * when the port is connected. The @link_down indicates the port is just
  972. * disconnected so that all MIB counters are read one last time to update the
  973. * information.
  974. */
  975. struct ksz_port_mib {
  976. u8 cnt_ptr;
  977. u8 link_down;
  978. u8 state;
  979. u8 mib_start;
  980. u64 counter[TOTAL_PORT_COUNTER_NUM];
  981. u32 dropped[2];
  982. };
  983. /**
  984. * struct ksz_port_cfg - Port configuration data structure
  985. * @vid: VID value.
  986. * @member: Port membership.
  987. * @port_prio: Port priority.
  988. * @rx_rate: Receive priority rate.
  989. * @tx_rate: Transmit priority rate.
  990. * @stp_state: Current Spanning Tree Protocol state.
  991. */
  992. struct ksz_port_cfg {
  993. u16 vid;
  994. u8 member;
  995. u8 port_prio;
  996. u32 rx_rate[PRIO_QUEUES];
  997. u32 tx_rate[PRIO_QUEUES];
  998. int stp_state;
  999. };
  1000. /**
  1001. * struct ksz_switch - KSZ8842 switch data structure
  1002. * @mac_table: MAC table entries information.
  1003. * @vlan_table: VLAN table entries information.
  1004. * @port_cfg: Port configuration information.
  1005. * @diffserv: DiffServ priority settings. Possible values from 6-bit of ToS
  1006. * (bit7 ~ bit2) field.
  1007. * @p_802_1p: 802.1P priority settings. Possible values from 3-bit of 802.1p
  1008. * Tag priority field.
  1009. * @br_addr: Bridge address. Used for STP.
  1010. * @other_addr: Other MAC address. Used for multiple network device mode.
  1011. * @broad_per: Broadcast storm percentage.
  1012. * @member: Current port membership. Used for STP.
  1013. */
  1014. struct ksz_switch {
  1015. struct ksz_mac_table mac_table[STATIC_MAC_TABLE_ENTRIES];
  1016. struct ksz_vlan_table vlan_table[VLAN_TABLE_ENTRIES];
  1017. struct ksz_port_cfg port_cfg[TOTAL_PORT_NUM];
  1018. u8 diffserv[DIFFSERV_ENTRIES];
  1019. u8 p_802_1p[PRIO_802_1P_ENTRIES];
  1020. u8 br_addr[MAC_ADDR_LEN];
  1021. u8 other_addr[MAC_ADDR_LEN];
  1022. u8 broad_per;
  1023. u8 member;
  1024. };
  1025. #define TX_RATE_UNIT 10000
  1026. /**
  1027. * struct ksz_port_info - Port information data structure
  1028. * @state: Connection status of the port.
  1029. * @tx_rate: Transmit rate divided by 10000 to get Mbit.
  1030. * @duplex: Duplex mode.
  1031. * @advertised: Advertised auto-negotiation setting. Used to determine link.
  1032. * @partner: Auto-negotiation partner setting. Used to determine link.
  1033. * @port_id: Port index to access actual hardware register.
  1034. * @pdev: Pointer to OS dependent network device.
  1035. */
  1036. struct ksz_port_info {
  1037. uint state;
  1038. uint tx_rate;
  1039. u8 duplex;
  1040. u8 advertised;
  1041. u8 partner;
  1042. u8 port_id;
  1043. void *pdev;
  1044. };
  1045. #define MAX_TX_HELD_SIZE 52000
  1046. /* Hardware features and bug fixes. */
  1047. #define LINK_INT_WORKING (1 << 0)
  1048. #define SMALL_PACKET_TX_BUG (1 << 1)
  1049. #define HALF_DUPLEX_SIGNAL_BUG (1 << 2)
  1050. #define RX_HUGE_FRAME (1 << 4)
  1051. #define STP_SUPPORT (1 << 8)
  1052. /* Software overrides. */
  1053. #define PAUSE_FLOW_CTRL (1 << 0)
  1054. #define FAST_AGING (1 << 1)
  1055. /**
  1056. * struct ksz_hw - KSZ884X hardware data structure
  1057. * @io: Virtual address assigned.
  1058. * @ksz_switch: Pointer to KSZ8842 switch.
  1059. * @port_info: Port information.
  1060. * @port_mib: Port MIB information.
  1061. * @dev_count: Number of network devices this hardware supports.
  1062. * @dst_ports: Destination ports in switch for transmission.
  1063. * @id: Hardware ID. Used for display only.
  1064. * @mib_cnt: Number of MIB counters this hardware has.
  1065. * @mib_port_cnt: Number of ports with MIB counters.
  1066. * @tx_cfg: Cached transmit control settings.
  1067. * @rx_cfg: Cached receive control settings.
  1068. * @intr_mask: Current interrupt mask.
  1069. * @intr_set: Current interrup set.
  1070. * @intr_blocked: Interrupt blocked.
  1071. * @rx_desc_info: Receive descriptor information.
  1072. * @tx_desc_info: Transmit descriptor information.
  1073. * @tx_int_cnt: Transmit interrupt count. Used for TX optimization.
  1074. * @tx_int_mask: Transmit interrupt mask. Used for TX optimization.
  1075. * @tx_size: Transmit data size. Used for TX optimization.
  1076. * The maximum is defined by MAX_TX_HELD_SIZE.
  1077. * @perm_addr: Permanent MAC address.
  1078. * @override_addr: Overrided MAC address.
  1079. * @address: Additional MAC address entries.
  1080. * @addr_list_size: Additional MAC address list size.
  1081. * @mac_override: Indication of MAC address overrided.
  1082. * @promiscuous: Counter to keep track of promiscuous mode set.
  1083. * @all_multi: Counter to keep track of all multicast mode set.
  1084. * @multi_list: Multicast address entries.
  1085. * @multi_bits: Cached multicast hash table settings.
  1086. * @multi_list_size: Multicast address list size.
  1087. * @enabled: Indication of hardware enabled.
  1088. * @rx_stop: Indication of receive process stop.
  1089. * @features: Hardware features to enable.
  1090. * @overrides: Hardware features to override.
  1091. * @parent: Pointer to parent, network device private structure.
  1092. */
  1093. struct ksz_hw {
  1094. void __iomem *io;
  1095. struct ksz_switch *ksz_switch;
  1096. struct ksz_port_info port_info[SWITCH_PORT_NUM];
  1097. struct ksz_port_mib port_mib[TOTAL_PORT_NUM];
  1098. int dev_count;
  1099. int dst_ports;
  1100. int id;
  1101. int mib_cnt;
  1102. int mib_port_cnt;
  1103. u32 tx_cfg;
  1104. u32 rx_cfg;
  1105. u32 intr_mask;
  1106. u32 intr_set;
  1107. uint intr_blocked;
  1108. struct ksz_desc_info rx_desc_info;
  1109. struct ksz_desc_info tx_desc_info;
  1110. int tx_int_cnt;
  1111. int tx_int_mask;
  1112. int tx_size;
  1113. u8 perm_addr[MAC_ADDR_LEN];
  1114. u8 override_addr[MAC_ADDR_LEN];
  1115. u8 address[ADDITIONAL_ENTRIES][MAC_ADDR_LEN];
  1116. u8 addr_list_size;
  1117. u8 mac_override;
  1118. u8 promiscuous;
  1119. u8 all_multi;
  1120. u8 multi_list[MAX_MULTICAST_LIST][MAC_ADDR_LEN];
  1121. u8 multi_bits[HW_MULTICAST_SIZE];
  1122. u8 multi_list_size;
  1123. u8 enabled;
  1124. u8 rx_stop;
  1125. u8 reserved2[1];
  1126. uint features;
  1127. uint overrides;
  1128. void *parent;
  1129. };
  1130. enum {
  1131. PHY_NO_FLOW_CTRL,
  1132. PHY_FLOW_CTRL,
  1133. PHY_TX_ONLY,
  1134. PHY_RX_ONLY
  1135. };
  1136. /**
  1137. * struct ksz_port - Virtual port data structure
  1138. * @duplex: Duplex mode setting. 1 for half duplex, 2 for full
  1139. * duplex, and 0 for auto, which normally results in full
  1140. * duplex.
  1141. * @speed: Speed setting. 10 for 10 Mbit, 100 for 100 Mbit, and
  1142. * 0 for auto, which normally results in 100 Mbit.
  1143. * @force_link: Force link setting. 0 for auto-negotiation, and 1 for
  1144. * force.
  1145. * @flow_ctrl: Flow control setting. PHY_NO_FLOW_CTRL for no flow
  1146. * control, and PHY_FLOW_CTRL for flow control.
  1147. * PHY_TX_ONLY and PHY_RX_ONLY are not supported for 100
  1148. * Mbit PHY.
  1149. * @first_port: Index of first port this port supports.
  1150. * @mib_port_cnt: Number of ports with MIB counters.
  1151. * @port_cnt: Number of ports this port supports.
  1152. * @counter: Port statistics counter.
  1153. * @hw: Pointer to hardware structure.
  1154. * @linked: Pointer to port information linked to this port.
  1155. */
  1156. struct ksz_port {
  1157. u8 duplex;
  1158. u8 speed;
  1159. u8 force_link;
  1160. u8 flow_ctrl;
  1161. int first_port;
  1162. int mib_port_cnt;
  1163. int port_cnt;
  1164. u64 counter[OID_COUNTER_LAST];
  1165. struct ksz_hw *hw;
  1166. struct ksz_port_info *linked;
  1167. };
  1168. /**
  1169. * struct ksz_timer_info - Timer information data structure
  1170. * @timer: Kernel timer.
  1171. * @cnt: Running timer counter.
  1172. * @max: Number of times to run timer; -1 for infinity.
  1173. * @period: Timer period in jiffies.
  1174. */
  1175. struct ksz_timer_info {
  1176. struct timer_list timer;
  1177. int cnt;
  1178. int max;
  1179. int period;
  1180. };
  1181. /**
  1182. * struct ksz_shared_mem - OS dependent shared memory data structure
  1183. * @dma_addr: Physical DMA address allocated.
  1184. * @alloc_size: Allocation size.
  1185. * @phys: Actual physical address used.
  1186. * @alloc_virt: Virtual address allocated.
  1187. * @virt: Actual virtual address used.
  1188. */
  1189. struct ksz_shared_mem {
  1190. dma_addr_t dma_addr;
  1191. uint alloc_size;
  1192. uint phys;
  1193. u8 *alloc_virt;
  1194. u8 *virt;
  1195. };
  1196. /**
  1197. * struct ksz_counter_info - OS dependent counter information data structure
  1198. * @counter: Wait queue to wakeup after counters are read.
  1199. * @time: Next time in jiffies to read counter.
  1200. * @read: Indication of counters read in full or not.
  1201. */
  1202. struct ksz_counter_info {
  1203. wait_queue_head_t counter;
  1204. unsigned long time;
  1205. int read;
  1206. };
  1207. /**
  1208. * struct dev_info - Network device information data structure
  1209. * @dev: Pointer to network device.
  1210. * @pdev: Pointer to PCI device.
  1211. * @hw: Hardware structure.
  1212. * @desc_pool: Physical memory used for descriptor pool.
  1213. * @hwlock: Spinlock to prevent hardware from accessing.
  1214. * @lock: Mutex lock to prevent device from accessing.
  1215. * @dev_rcv: Receive process function used.
  1216. * @last_skb: Socket buffer allocated for descriptor rx fragments.
  1217. * @skb_index: Buffer index for receiving fragments.
  1218. * @skb_len: Buffer length for receiving fragments.
  1219. * @mib_read: Workqueue to read MIB counters.
  1220. * @mib_timer_info: Timer to read MIB counters.
  1221. * @counter: Used for MIB reading.
  1222. * @mtu: Current MTU used. The default is REGULAR_RX_BUF_SIZE;
  1223. * the maximum is MAX_RX_BUF_SIZE.
  1224. * @opened: Counter to keep track of device open.
  1225. * @rx_tasklet: Receive processing tasklet.
  1226. * @tx_tasklet: Transmit processing tasklet.
  1227. * @wol_enable: Wake-on-LAN enable set by ethtool.
  1228. * @wol_support: Wake-on-LAN support used by ethtool.
  1229. * @pme_wait: Used for KSZ8841 power management.
  1230. */
  1231. struct dev_info {
  1232. struct net_device *dev;
  1233. struct pci_dev *pdev;
  1234. struct ksz_hw hw;
  1235. struct ksz_shared_mem desc_pool;
  1236. spinlock_t hwlock;
  1237. struct mutex lock;
  1238. int (*dev_rcv)(struct dev_info *);
  1239. struct sk_buff *last_skb;
  1240. int skb_index;
  1241. int skb_len;
  1242. struct work_struct mib_read;
  1243. struct ksz_timer_info mib_timer_info;
  1244. struct ksz_counter_info counter[TOTAL_PORT_NUM];
  1245. int mtu;
  1246. int opened;
  1247. struct tasklet_struct rx_tasklet;
  1248. struct tasklet_struct tx_tasklet;
  1249. int wol_enable;
  1250. int wol_support;
  1251. unsigned long pme_wait;
  1252. };
  1253. /**
  1254. * struct dev_priv - Network device private data structure
  1255. * @adapter: Adapter device information.
  1256. * @port: Port information.
  1257. * @monitor_time_info: Timer to monitor ports.
  1258. * @proc_sem: Semaphore for proc accessing.
  1259. * @id: Device ID.
  1260. * @mii_if: MII interface information.
  1261. * @advertising: Temporary variable to store advertised settings.
  1262. * @msg_enable: The message flags controlling driver output.
  1263. * @media_state: The connection status of the device.
  1264. * @multicast: The all multicast state of the device.
  1265. * @promiscuous: The promiscuous state of the device.
  1266. */
  1267. struct dev_priv {
  1268. struct dev_info *adapter;
  1269. struct ksz_port port;
  1270. struct ksz_timer_info monitor_timer_info;
  1271. struct semaphore proc_sem;
  1272. int id;
  1273. struct mii_if_info mii_if;
  1274. u32 advertising;
  1275. u32 msg_enable;
  1276. int media_state;
  1277. int multicast;
  1278. int promiscuous;
  1279. };
  1280. #define DRV_NAME "KSZ884X PCI"
  1281. #define DEVICE_NAME "KSZ884x PCI"
  1282. #define DRV_VERSION "1.0.0"
  1283. #define DRV_RELDATE "Feb 8, 2010"
  1284. static char version[] __devinitdata =
  1285. "Micrel " DEVICE_NAME " " DRV_VERSION " (" DRV_RELDATE ")";
  1286. static u8 DEFAULT_MAC_ADDRESS[] = { 0x00, 0x10, 0xA1, 0x88, 0x42, 0x01 };
  1287. /*
  1288. * Interrupt processing primary routines
  1289. */
  1290. static inline void hw_ack_intr(struct ksz_hw *hw, uint interrupt)
  1291. {
  1292. writel(interrupt, hw->io + KS884X_INTERRUPTS_STATUS);
  1293. }
  1294. static inline void hw_dis_intr(struct ksz_hw *hw)
  1295. {
  1296. hw->intr_blocked = hw->intr_mask;
  1297. writel(0, hw->io + KS884X_INTERRUPTS_ENABLE);
  1298. hw->intr_set = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
  1299. }
  1300. static inline void hw_set_intr(struct ksz_hw *hw, uint interrupt)
  1301. {
  1302. hw->intr_set = interrupt;
  1303. writel(interrupt, hw->io + KS884X_INTERRUPTS_ENABLE);
  1304. }
  1305. static inline void hw_ena_intr(struct ksz_hw *hw)
  1306. {
  1307. hw->intr_blocked = 0;
  1308. hw_set_intr(hw, hw->intr_mask);
  1309. }
  1310. static inline void hw_dis_intr_bit(struct ksz_hw *hw, uint bit)
  1311. {
  1312. hw->intr_mask &= ~(bit);
  1313. }
  1314. static inline void hw_turn_off_intr(struct ksz_hw *hw, uint interrupt)
  1315. {
  1316. u32 read_intr;
  1317. read_intr = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
  1318. hw->intr_set = read_intr & ~interrupt;
  1319. writel(hw->intr_set, hw->io + KS884X_INTERRUPTS_ENABLE);
  1320. hw_dis_intr_bit(hw, interrupt);
  1321. }
  1322. /**
  1323. * hw_turn_on_intr - turn on specified interrupts
  1324. * @hw: The hardware instance.
  1325. * @bit: The interrupt bits to be on.
  1326. *
  1327. * This routine turns on the specified interrupts in the interrupt mask so that
  1328. * those interrupts will be enabled.
  1329. */
  1330. static void hw_turn_on_intr(struct ksz_hw *hw, u32 bit)
  1331. {
  1332. hw->intr_mask |= bit;
  1333. if (!hw->intr_blocked)
  1334. hw_set_intr(hw, hw->intr_mask);
  1335. }
  1336. static inline void hw_ena_intr_bit(struct ksz_hw *hw, uint interrupt)
  1337. {
  1338. u32 read_intr;
  1339. read_intr = readl(hw->io + KS884X_INTERRUPTS_ENABLE);
  1340. hw->intr_set = read_intr | interrupt;
  1341. writel(hw->intr_set, hw->io + KS884X_INTERRUPTS_ENABLE);
  1342. }
  1343. static inline void hw_read_intr(struct ksz_hw *hw, uint *status)
  1344. {
  1345. *status = readl(hw->io + KS884X_INTERRUPTS_STATUS);
  1346. *status = *status & hw->intr_set;
  1347. }
  1348. static inline void hw_restore_intr(struct ksz_hw *hw, uint interrupt)
  1349. {
  1350. if (interrupt)
  1351. hw_ena_intr(hw);
  1352. }
  1353. /**
  1354. * hw_block_intr - block hardware interrupts
  1355. *
  1356. * This function blocks all interrupts of the hardware and returns the current
  1357. * interrupt enable mask so that interrupts can be restored later.
  1358. *
  1359. * Return the current interrupt enable mask.
  1360. */
  1361. static uint hw_block_intr(struct ksz_hw *hw)
  1362. {
  1363. uint interrupt = 0;
  1364. if (!hw->intr_blocked) {
  1365. hw_dis_intr(hw);
  1366. interrupt = hw->intr_blocked;
  1367. }
  1368. return interrupt;
  1369. }
  1370. /*
  1371. * Hardware descriptor routines
  1372. */
  1373. static inline void reset_desc(struct ksz_desc *desc, union desc_stat status)
  1374. {
  1375. status.rx.hw_owned = 0;
  1376. desc->phw->ctrl.data = cpu_to_le32(status.data);
  1377. }
  1378. static inline void release_desc(struct ksz_desc *desc)
  1379. {
  1380. desc->sw.ctrl.tx.hw_owned = 1;
  1381. if (desc->sw.buf_size != desc->sw.buf.data) {
  1382. desc->sw.buf_size = desc->sw.buf.data;
  1383. desc->phw->buf.data = cpu_to_le32(desc->sw.buf.data);
  1384. }
  1385. desc->phw->ctrl.data = cpu_to_le32(desc->sw.ctrl.data);
  1386. }
  1387. static void get_rx_pkt(struct ksz_desc_info *info, struct ksz_desc **desc)
  1388. {
  1389. *desc = &info->ring[info->last];
  1390. info->last++;
  1391. info->last &= info->mask;
  1392. info->avail--;
  1393. (*desc)->sw.buf.data &= ~KS_DESC_RX_MASK;
  1394. }
  1395. static inline void set_rx_buf(struct ksz_desc *desc, u32 addr)
  1396. {
  1397. desc->phw->addr = cpu_to_le32(addr);
  1398. }
  1399. static inline void set_rx_len(struct ksz_desc *desc, u32 len)
  1400. {
  1401. desc->sw.buf.rx.buf_size = len;
  1402. }
  1403. static inline void get_tx_pkt(struct ksz_desc_info *info,
  1404. struct ksz_desc **desc)
  1405. {
  1406. *desc = &info->ring[info->next];
  1407. info->next++;
  1408. info->next &= info->mask;
  1409. info->avail--;
  1410. (*desc)->sw.buf.data &= ~KS_DESC_TX_MASK;
  1411. }
  1412. static inline void set_tx_buf(struct ksz_desc *desc, u32 addr)
  1413. {
  1414. desc->phw->addr = cpu_to_le32(addr);
  1415. }
  1416. static inline void set_tx_len(struct ksz_desc *desc, u32 len)
  1417. {
  1418. desc->sw.buf.tx.buf_size = len;
  1419. }
  1420. /* Switch functions */
  1421. #define TABLE_READ 0x10
  1422. #define TABLE_SEL_SHIFT 2
  1423. #define HW_DELAY(hw, reg) \
  1424. do { \
  1425. u16 dummy; \
  1426. dummy = readw(hw->io + reg); \
  1427. } while (0)
  1428. /**
  1429. * sw_r_table - read 4 bytes of data from switch table
  1430. * @hw: The hardware instance.
  1431. * @table: The table selector.
  1432. * @addr: The address of the table entry.
  1433. * @data: Buffer to store the read data.
  1434. *
  1435. * This routine reads 4 bytes of data from the table of the switch.
  1436. * Hardware interrupts are disabled to minimize corruption of read data.
  1437. */
  1438. static void sw_r_table(struct ksz_hw *hw, int table, u16 addr, u32 *data)
  1439. {
  1440. u16 ctrl_addr;
  1441. uint interrupt;
  1442. ctrl_addr = (((table << TABLE_SEL_SHIFT) | TABLE_READ) << 8) | addr;
  1443. interrupt = hw_block_intr(hw);
  1444. writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
  1445. HW_DELAY(hw, KS884X_IACR_OFFSET);
  1446. *data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
  1447. hw_restore_intr(hw, interrupt);
  1448. }
  1449. /**
  1450. * sw_w_table_64 - write 8 bytes of data to the switch table
  1451. * @hw: The hardware instance.
  1452. * @table: The table selector.
  1453. * @addr: The address of the table entry.
  1454. * @data_hi: The high part of data to be written (bit63 ~ bit32).
  1455. * @data_lo: The low part of data to be written (bit31 ~ bit0).
  1456. *
  1457. * This routine writes 8 bytes of data to the table of the switch.
  1458. * Hardware interrupts are disabled to minimize corruption of written data.
  1459. */
  1460. static void sw_w_table_64(struct ksz_hw *hw, int table, u16 addr, u32 data_hi,
  1461. u32 data_lo)
  1462. {
  1463. u16 ctrl_addr;
  1464. uint interrupt;
  1465. ctrl_addr = ((table << TABLE_SEL_SHIFT) << 8) | addr;
  1466. interrupt = hw_block_intr(hw);
  1467. writel(data_hi, hw->io + KS884X_ACC_DATA_4_OFFSET);
  1468. writel(data_lo, hw->io + KS884X_ACC_DATA_0_OFFSET);
  1469. writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
  1470. HW_DELAY(hw, KS884X_IACR_OFFSET);
  1471. hw_restore_intr(hw, interrupt);
  1472. }
  1473. /**
  1474. * sw_w_sta_mac_table - write to the static MAC table
  1475. * @hw: The hardware instance.
  1476. * @addr: The address of the table entry.
  1477. * @mac_addr: The MAC address.
  1478. * @ports: The port members.
  1479. * @override: The flag to override the port receive/transmit settings.
  1480. * @valid: The flag to indicate entry is valid.
  1481. * @use_fid: The flag to indicate the FID is valid.
  1482. * @fid: The FID value.
  1483. *
  1484. * This routine writes an entry of the static MAC table of the switch. It
  1485. * calls sw_w_table_64() to write the data.
  1486. */
  1487. static void sw_w_sta_mac_table(struct ksz_hw *hw, u16 addr, u8 *mac_addr,
  1488. u8 ports, int override, int valid, int use_fid, u8 fid)
  1489. {
  1490. u32 data_hi;
  1491. u32 data_lo;
  1492. data_lo = ((u32) mac_addr[2] << 24) |
  1493. ((u32) mac_addr[3] << 16) |
  1494. ((u32) mac_addr[4] << 8) | mac_addr[5];
  1495. data_hi = ((u32) mac_addr[0] << 8) | mac_addr[1];
  1496. data_hi |= (u32) ports << STATIC_MAC_FWD_PORTS_SHIFT;
  1497. if (override)
  1498. data_hi |= STATIC_MAC_TABLE_OVERRIDE;
  1499. if (use_fid) {
  1500. data_hi |= STATIC_MAC_TABLE_USE_FID;
  1501. data_hi |= (u32) fid << STATIC_MAC_FID_SHIFT;
  1502. }
  1503. if (valid)
  1504. data_hi |= STATIC_MAC_TABLE_VALID;
  1505. sw_w_table_64(hw, TABLE_STATIC_MAC, addr, data_hi, data_lo);
  1506. }
  1507. /**
  1508. * sw_r_vlan_table - read from the VLAN table
  1509. * @hw: The hardware instance.
  1510. * @addr: The address of the table entry.
  1511. * @vid: Buffer to store the VID.
  1512. * @fid: Buffer to store the VID.
  1513. * @member: Buffer to store the port membership.
  1514. *
  1515. * This function reads an entry of the VLAN table of the switch. It calls
  1516. * sw_r_table() to get the data.
  1517. *
  1518. * Return 0 if the entry is valid; otherwise -1.
  1519. */
  1520. static int sw_r_vlan_table(struct ksz_hw *hw, u16 addr, u16 *vid, u8 *fid,
  1521. u8 *member)
  1522. {
  1523. u32 data;
  1524. sw_r_table(hw, TABLE_VLAN, addr, &data);
  1525. if (data & VLAN_TABLE_VALID) {
  1526. *vid = (u16)(data & VLAN_TABLE_VID);
  1527. *fid = (u8)((data & VLAN_TABLE_FID) >> VLAN_TABLE_FID_SHIFT);
  1528. *member = (u8)((data & VLAN_TABLE_MEMBERSHIP) >>
  1529. VLAN_TABLE_MEMBERSHIP_SHIFT);
  1530. return 0;
  1531. }
  1532. return -1;
  1533. }
  1534. /**
  1535. * port_r_mib_cnt - read MIB counter
  1536. * @hw: The hardware instance.
  1537. * @port: The port index.
  1538. * @addr: The address of the counter.
  1539. * @cnt: Buffer to store the counter.
  1540. *
  1541. * This routine reads a MIB counter of the port.
  1542. * Hardware interrupts are disabled to minimize corruption of read data.
  1543. */
  1544. static void port_r_mib_cnt(struct ksz_hw *hw, int port, u16 addr, u64 *cnt)
  1545. {
  1546. u32 data;
  1547. u16 ctrl_addr;
  1548. uint interrupt;
  1549. int timeout;
  1550. ctrl_addr = addr + PORT_COUNTER_NUM * port;
  1551. interrupt = hw_block_intr(hw);
  1552. ctrl_addr |= (((TABLE_MIB << TABLE_SEL_SHIFT) | TABLE_READ) << 8);
  1553. writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
  1554. HW_DELAY(hw, KS884X_IACR_OFFSET);
  1555. for (timeout = 100; timeout > 0; timeout--) {
  1556. data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
  1557. if (data & MIB_COUNTER_VALID) {
  1558. if (data & MIB_COUNTER_OVERFLOW)
  1559. *cnt += MIB_COUNTER_VALUE + 1;
  1560. *cnt += data & MIB_COUNTER_VALUE;
  1561. break;
  1562. }
  1563. }
  1564. hw_restore_intr(hw, interrupt);
  1565. }
  1566. /**
  1567. * port_r_mib_pkt - read dropped packet counts
  1568. * @hw: The hardware instance.
  1569. * @port: The port index.
  1570. * @cnt: Buffer to store the receive and transmit dropped packet counts.
  1571. *
  1572. * This routine reads the dropped packet counts of the port.
  1573. * Hardware interrupts are disabled to minimize corruption of read data.
  1574. */
  1575. static void port_r_mib_pkt(struct ksz_hw *hw, int port, u32 *last, u64 *cnt)
  1576. {
  1577. u32 cur;
  1578. u32 data;
  1579. u16 ctrl_addr;
  1580. uint interrupt;
  1581. int index;
  1582. index = KS_MIB_PACKET_DROPPED_RX_0 + port;
  1583. do {
  1584. interrupt = hw_block_intr(hw);
  1585. ctrl_addr = (u16) index;
  1586. ctrl_addr |= (((TABLE_MIB << TABLE_SEL_SHIFT) | TABLE_READ)
  1587. << 8);
  1588. writew(ctrl_addr, hw->io + KS884X_IACR_OFFSET);
  1589. HW_DELAY(hw, KS884X_IACR_OFFSET);
  1590. data = readl(hw->io + KS884X_ACC_DATA_0_OFFSET);
  1591. hw_restore_intr(hw, interrupt);
  1592. data &= MIB_PACKET_DROPPED;
  1593. cur = *last;
  1594. if (data != cur) {
  1595. *last = data;
  1596. if (data < cur)
  1597. data += MIB_PACKET_DROPPED + 1;
  1598. data -= cur;
  1599. *cnt += data;
  1600. }
  1601. ++last;
  1602. ++cnt;
  1603. index -= KS_MIB_PACKET_DROPPED_TX -
  1604. KS_MIB_PACKET_DROPPED_TX_0 + 1;
  1605. } while (index >= KS_MIB_PACKET_DROPPED_TX_0 + port);
  1606. }
  1607. /**
  1608. * port_r_cnt - read MIB counters periodically
  1609. * @hw: The hardware instance.
  1610. * @port: The port index.
  1611. *
  1612. * This routine is used to read the counters of the port periodically to avoid
  1613. * counter overflow. The hardware should be acquired first before calling this
  1614. * routine.
  1615. *
  1616. * Return non-zero when not all counters not read.
  1617. */
  1618. static int port_r_cnt(struct ksz_hw *hw, int port)
  1619. {
  1620. struct ksz_port_mib *mib = &hw->port_mib[port];
  1621. if (mib->mib_start < PORT_COUNTER_NUM)
  1622. while (mib->cnt_ptr < PORT_COUNTER_NUM) {
  1623. port_r_mib_cnt(hw, port, mib->cnt_ptr,
  1624. &mib->counter[mib->cnt_ptr]);
  1625. ++mib->cnt_ptr;
  1626. }
  1627. if (hw->mib_cnt > PORT_COUNTER_NUM)
  1628. port_r_mib_pkt(hw, port, mib->dropped,
  1629. &mib->counter[PORT_COUNTER_NUM]);
  1630. mib->cnt_ptr = 0;
  1631. return 0;
  1632. }
  1633. /**
  1634. * port_init_cnt - initialize MIB counter values
  1635. * @hw: The hardware instance.
  1636. * @port: The port index.
  1637. *
  1638. * This routine is used to initialize all counters to zero if the hardware
  1639. * cannot do it after reset.
  1640. */
  1641. static void port_init_cnt(struct ksz_hw *hw, int port)
  1642. {
  1643. struct ksz_port_mib *mib = &hw->port_mib[port];
  1644. mib->cnt_ptr = 0;
  1645. if (mib->mib_start < PORT_COUNTER_NUM)
  1646. do {
  1647. port_r_mib_cnt(hw, port, mib->cnt_ptr,
  1648. &mib->counter[mib->cnt_ptr]);
  1649. ++mib->cnt_ptr;
  1650. } while (mib->cnt_ptr < PORT_COUNTER_NUM);
  1651. if (hw->mib_cnt > PORT_COUNTER_NUM)
  1652. port_r_mib_pkt(hw, port, mib->dropped,
  1653. &mib->counter[PORT_COUNTER_NUM]);
  1654. memset((void *) mib->counter, 0, sizeof(u64) * TOTAL_PORT_COUNTER_NUM);
  1655. mib->cnt_ptr = 0;
  1656. }
  1657. /*
  1658. * Port functions
  1659. */
  1660. /**
  1661. * port_chk - check port register bits
  1662. * @hw: The hardware instance.
  1663. * @port: The port index.
  1664. * @offset: The offset of the port register.
  1665. * @bits: The data bits to check.
  1666. *
  1667. * This function checks whether the specified bits of the port register are set
  1668. * or not.
  1669. *
  1670. * Return 0 if the bits are not set.
  1671. */
  1672. static int port_chk(struct ksz_hw *hw, int port, int offset, u16 bits)
  1673. {
  1674. u32 addr;
  1675. u16 data;
  1676. PORT_CTRL_ADDR(port, addr);
  1677. addr += offset;
  1678. data = readw(hw->io + addr);
  1679. return (data & bits) == bits;
  1680. }
  1681. /**
  1682. * port_cfg - set port register bits
  1683. * @hw: The hardware instance.
  1684. * @port: The port index.
  1685. * @offset: The offset of the port register.
  1686. * @bits: The data bits to set.
  1687. * @set: The flag indicating whether the bits are to be set or not.
  1688. *
  1689. * This routine sets or resets the specified bits of the port register.
  1690. */
  1691. static void port_cfg(struct ksz_hw *hw, int port, int offset, u16 bits,
  1692. int set)
  1693. {
  1694. u32 addr;
  1695. u16 data;
  1696. PORT_CTRL_ADDR(port, addr);
  1697. addr += offset;
  1698. data = readw(hw->io + addr);
  1699. if (set)
  1700. data |= bits;
  1701. else
  1702. data &= ~bits;
  1703. writew(data, hw->io + addr);
  1704. }
  1705. /**
  1706. * port_chk_shift - check port bit
  1707. * @hw: The hardware instance.
  1708. * @port: The port index.
  1709. * @offset: The offset of the register.
  1710. * @shift: Number of bits to shift.
  1711. *
  1712. * This function checks whether the specified port is set in the register or
  1713. * not.
  1714. *
  1715. * Return 0 if the port is not set.
  1716. */
  1717. static int port_chk_shift(struct ksz_hw *hw, int port, u32 addr, int shift)
  1718. {
  1719. u16 data;
  1720. u16 bit = 1 << port;
  1721. data = readw(hw->io + addr);
  1722. data >>= shift;
  1723. return (data & bit) == bit;
  1724. }
  1725. /**
  1726. * port_cfg_shift - set port bit
  1727. * @hw: The hardware instance.
  1728. * @port: The port index.
  1729. * @offset: The offset of the register.
  1730. * @shift: Number of bits to shift.
  1731. * @set: The flag indicating whether the port is to be set or not.
  1732. *
  1733. * This routine sets or resets the specified port in the register.
  1734. */
  1735. static void port_cfg_shift(struct ksz_hw *hw, int port, u32 addr, int shift,
  1736. int set)
  1737. {
  1738. u16 data;
  1739. u16 bits = 1 << port;
  1740. data = readw(hw->io + addr);
  1741. bits <<= shift;
  1742. if (set)
  1743. data |= bits;
  1744. else
  1745. data &= ~bits;
  1746. writew(data, hw->io + addr);
  1747. }
  1748. /**
  1749. * port_r8 - read byte from port register
  1750. * @hw: The hardware instance.
  1751. * @port: The port index.
  1752. * @offset: The offset of the port register.
  1753. * @data: Buffer to store the data.
  1754. *
  1755. * This routine reads a byte from the port register.
  1756. */
  1757. static void port_r8(struct ksz_hw *hw, int port, int offset, u8 *data)
  1758. {
  1759. u32 addr;
  1760. PORT_CTRL_ADDR(port, addr);
  1761. addr += offset;
  1762. *data = readb(hw->io + addr);
  1763. }
  1764. /**
  1765. * port_r16 - read word from port register.
  1766. * @hw: The hardware instance.
  1767. * @port: The port index.
  1768. * @offset: The offset of the port register.
  1769. * @data: Buffer to store the data.
  1770. *
  1771. * This routine reads a word from the port register.
  1772. */
  1773. static void port_r16(struct ksz_hw *hw, int port, int offset, u16 *data)
  1774. {
  1775. u32 addr;
  1776. PORT_CTRL_ADDR(port, addr);
  1777. addr += offset;
  1778. *data = readw(hw->io + addr);
  1779. }
  1780. /**
  1781. * port_w16 - write word to port register.
  1782. * @hw: The hardware instance.
  1783. * @port: The port index.
  1784. * @offset: The offset of the port register.
  1785. * @data: Data to write.
  1786. *
  1787. * This routine writes a word to the port register.
  1788. */
  1789. static void port_w16(struct ksz_hw *hw, int port, int offset, u16 data)
  1790. {
  1791. u32 addr;
  1792. PORT_CTRL_ADDR(port, addr);
  1793. addr += offset;
  1794. writew(data, hw->io + addr);
  1795. }
  1796. /**
  1797. * sw_chk - check switch register bits
  1798. * @hw: The hardware instance.
  1799. * @addr: The address of the switch register.
  1800. * @bits: The data bits to check.
  1801. *
  1802. * This function checks whether the specified bits of the switch register are
  1803. * set or not.
  1804. *
  1805. * Return 0 if the bits are not set.
  1806. */
  1807. static int sw_chk(struct ksz_hw *hw, u32 addr, u16 bits)
  1808. {
  1809. u16 data;
  1810. data = readw(hw->io + addr);
  1811. return (data & bits) == bits;
  1812. }
  1813. /**
  1814. * sw_cfg - set switch register bits
  1815. * @hw: The hardware instance.
  1816. * @addr: The address of the switch register.
  1817. * @bits: The data bits to set.
  1818. * @set: The flag indicating whether the bits are to be set or not.
  1819. *
  1820. * This function sets or resets the specified bits of the switch register.
  1821. */
  1822. static void sw_cfg(struct ksz_hw *hw, u32 addr, u16 bits, int set)
  1823. {
  1824. u16 data;
  1825. data = readw(hw->io + addr);
  1826. if (set)
  1827. data |= bits;
  1828. else
  1829. data &= ~bits;
  1830. writew(data, hw->io + addr);
  1831. }
  1832. /* Bandwidth */
  1833. static inline void port_cfg_broad_storm(struct ksz_hw *hw, int p, int set)
  1834. {
  1835. port_cfg(hw, p,
  1836. KS8842_PORT_CTRL_1_OFFSET, PORT_BROADCAST_STORM, set);
  1837. }
  1838. static inline int port_chk_broad_storm(struct ksz_hw *hw, int p)
  1839. {
  1840. return port_chk(hw, p,
  1841. KS8842_PORT_CTRL_1_OFFSET, PORT_BROADCAST_STORM);
  1842. }
  1843. /* Driver set switch broadcast storm protection at 10% rate. */
  1844. #define BROADCAST_STORM_PROTECTION_RATE 10
  1845. /* 148,800 frames * 67 ms / 100 */
  1846. #define BROADCAST_STORM_VALUE 9969
  1847. /**
  1848. * sw_cfg_broad_storm - configure broadcast storm threshold
  1849. * @hw: The hardware instance.
  1850. * @percent: Broadcast storm threshold in percent of transmit rate.
  1851. *
  1852. * This routine configures the broadcast storm threshold of the switch.
  1853. */
  1854. static void sw_cfg_broad_storm(struct ksz_hw *hw, u8 percent)
  1855. {
  1856. u16 data;
  1857. u32 value = ((u32) BROADCAST_STORM_VALUE * (u32) percent / 100);
  1858. if (value > BROADCAST_STORM_RATE)
  1859. value = BROADCAST_STORM_RATE;
  1860. data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
  1861. data &= ~(BROADCAST_STORM_RATE_LO | BROADCAST_STORM_RATE_HI);
  1862. data |= ((value & 0x00FF) << 8) | ((value & 0xFF00) >> 8);
  1863. writew(data, hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
  1864. }
  1865. /**
  1866. * sw_get_board_storm - get broadcast storm threshold
  1867. * @hw: The hardware instance.
  1868. * @percent: Buffer to store the broadcast storm threshold percentage.
  1869. *
  1870. * This routine retrieves the broadcast storm threshold of the switch.
  1871. */
  1872. static void sw_get_broad_storm(struct ksz_hw *hw, u8 *percent)
  1873. {
  1874. int num;
  1875. u16 data;
  1876. data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
  1877. num = (data & BROADCAST_STORM_RATE_HI);
  1878. num <<= 8;
  1879. num |= (data & BROADCAST_STORM_RATE_LO) >> 8;
  1880. num = (num * 100 + BROADCAST_STORM_VALUE / 2) / BROADCAST_STORM_VALUE;
  1881. *percent = (u8) num;
  1882. }
  1883. /**
  1884. * sw_dis_broad_storm - disable broadstorm
  1885. * @hw: The hardware instance.
  1886. * @port: The port index.
  1887. *
  1888. * This routine disables the broadcast storm limit function of the switch.
  1889. */
  1890. static void sw_dis_broad_storm(struct ksz_hw *hw, int port)
  1891. {
  1892. port_cfg_broad_storm(hw, port, 0);
  1893. }
  1894. /**
  1895. * sw_ena_broad_storm - enable broadcast storm
  1896. * @hw: The hardware instance.
  1897. * @port: The port index.
  1898. *
  1899. * This routine enables the broadcast storm limit function of the switch.
  1900. */
  1901. static void sw_ena_broad_storm(struct ksz_hw *hw, int port)
  1902. {
  1903. sw_cfg_broad_storm(hw, hw->ksz_switch->broad_per);
  1904. port_cfg_broad_storm(hw, port, 1);
  1905. }
  1906. /**
  1907. * sw_init_broad_storm - initialize broadcast storm
  1908. * @hw: The hardware instance.
  1909. *
  1910. * This routine initializes the broadcast storm limit function of the switch.
  1911. */
  1912. static void sw_init_broad_storm(struct ksz_hw *hw)
  1913. {
  1914. int port;
  1915. hw->ksz_switch->broad_per = 1;
  1916. sw_cfg_broad_storm(hw, hw->ksz_switch->broad_per);
  1917. for (port = 0; port < TOTAL_PORT_NUM; port++)
  1918. sw_dis_broad_storm(hw, port);
  1919. sw_cfg(hw, KS8842_SWITCH_CTRL_2_OFFSET, MULTICAST_STORM_DISABLE, 1);
  1920. }
  1921. /**
  1922. * hw_cfg_broad_storm - configure broadcast storm
  1923. * @hw: The hardware instance.
  1924. * @percent: Broadcast storm threshold in percent of transmit rate.
  1925. *
  1926. * This routine configures the broadcast storm threshold of the switch.
  1927. * It is called by user functions. The hardware should be acquired first.
  1928. */
  1929. static void hw_cfg_broad_storm(struct ksz_hw *hw, u8 percent)
  1930. {
  1931. if (percent > 100)
  1932. percent = 100;
  1933. sw_cfg_broad_storm(hw, percent);
  1934. sw_get_broad_storm(hw, &percent);
  1935. hw->ksz_switch->broad_per = percent;
  1936. }
  1937. /**
  1938. * sw_dis_prio_rate - disable switch priority rate
  1939. * @hw: The hardware instance.
  1940. * @port: The port index.
  1941. *
  1942. * This routine disables the priority rate function of the switch.
  1943. */
  1944. static void sw_dis_prio_rate(struct ksz_hw *hw, int port)
  1945. {
  1946. u32 addr;
  1947. PORT_CTRL_ADDR(port, addr);
  1948. addr += KS8842_PORT_IN_RATE_OFFSET;
  1949. writel(0, hw->io + addr);
  1950. }
  1951. /**
  1952. * sw_init_prio_rate - initialize switch prioirty rate
  1953. * @hw: The hardware instance.
  1954. *
  1955. * This routine initializes the priority rate function of the switch.
  1956. */
  1957. static void sw_init_prio_rate(struct ksz_hw *hw)
  1958. {
  1959. int port;
  1960. int prio;
  1961. struct ksz_switch *sw = hw->ksz_switch;
  1962. for (port = 0; port < TOTAL_PORT_NUM; port++) {
  1963. for (prio = 0; prio < PRIO_QUEUES; prio++) {
  1964. sw->port_cfg[port].rx_rate[prio] =
  1965. sw->port_cfg[port].tx_rate[prio] = 0;
  1966. }
  1967. sw_dis_prio_rate(hw, port);
  1968. }
  1969. }
  1970. /* Communication */
  1971. static inline void port_cfg_back_pressure(struct ksz_hw *hw, int p, int set)
  1972. {
  1973. port_cfg(hw, p,
  1974. KS8842_PORT_CTRL_2_OFFSET, PORT_BACK_PRESSURE, set);
  1975. }
  1976. static inline void port_cfg_force_flow_ctrl(struct ksz_hw *hw, int p, int set)
  1977. {
  1978. port_cfg(hw, p,
  1979. KS8842_PORT_CTRL_2_OFFSET, PORT_FORCE_FLOW_CTRL, set);
  1980. }
  1981. static inline int port_chk_back_pressure(struct ksz_hw *hw, int p)
  1982. {
  1983. return port_chk(hw, p,
  1984. KS8842_PORT_CTRL_2_OFFSET, PORT_BACK_PRESSURE);
  1985. }
  1986. static inline int port_chk_force_flow_ctrl(struct ksz_hw *hw, int p)
  1987. {
  1988. return port_chk(hw, p,
  1989. KS8842_PORT_CTRL_2_OFFSET, PORT_FORCE_FLOW_CTRL);
  1990. }
  1991. /* Spanning Tree */
  1992. static inline void port_cfg_dis_learn(struct ksz_hw *hw, int p, int set)
  1993. {
  1994. port_cfg(hw, p,
  1995. KS8842_PORT_CTRL_2_OFFSET, PORT_LEARN_DISABLE, set);
  1996. }
  1997. static inline void port_cfg_rx(struct ksz_hw *hw, int p, int set)
  1998. {
  1999. port_cfg(hw, p,
  2000. KS8842_PORT_CTRL_2_OFFSET, PORT_RX_ENABLE, set);
  2001. }
  2002. static inline void port_cfg_tx(struct ksz_hw *hw, int p, int set)
  2003. {
  2004. port_cfg(hw, p,
  2005. KS8842_PORT_CTRL_2_OFFSET, PORT_TX_ENABLE, set);
  2006. }
  2007. static inline void sw_cfg_fast_aging(struct ksz_hw *hw, int set)
  2008. {
  2009. sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET, SWITCH_FAST_AGING, set);
  2010. }
  2011. static inline void sw_flush_dyn_mac_table(struct ksz_hw *hw)
  2012. {
  2013. if (!(hw->overrides & FAST_AGING)) {
  2014. sw_cfg_fast_aging(hw, 1);
  2015. mdelay(1);
  2016. sw_cfg_fast_aging(hw, 0);
  2017. }
  2018. }
  2019. /* VLAN */
  2020. static inline void port_cfg_ins_tag(struct ksz_hw *hw, int p, int insert)
  2021. {
  2022. port_cfg(hw, p,
  2023. KS8842_PORT_CTRL_1_OFFSET, PORT_INSERT_TAG, insert);
  2024. }
  2025. static inline void port_cfg_rmv_tag(struct ksz_hw *hw, int p, int remove)
  2026. {
  2027. port_cfg(hw, p,
  2028. KS8842_PORT_CTRL_1_OFFSET, PORT_REMOVE_TAG, remove);
  2029. }
  2030. static inline int port_chk_ins_tag(struct ksz_hw *hw, int p)
  2031. {
  2032. return port_chk(hw, p,
  2033. KS8842_PORT_CTRL_1_OFFSET, PORT_INSERT_TAG);
  2034. }
  2035. static inline int port_chk_rmv_tag(struct ksz_hw *hw, int p)
  2036. {
  2037. return port_chk(hw, p,
  2038. KS8842_PORT_CTRL_1_OFFSET, PORT_REMOVE_TAG);
  2039. }
  2040. static inline void port_cfg_dis_non_vid(struct ksz_hw *hw, int p, int set)
  2041. {
  2042. port_cfg(hw, p,
  2043. KS8842_PORT_CTRL_2_OFFSET, PORT_DISCARD_NON_VID, set);
  2044. }
  2045. static inline void port_cfg_in_filter(struct ksz_hw *hw, int p, int set)
  2046. {
  2047. port_cfg(hw, p,
  2048. KS8842_PORT_CTRL_2_OFFSET, PORT_INGRESS_VLAN_FILTER, set);
  2049. }
  2050. static inline int port_chk_dis_non_vid(struct ksz_hw *hw, int p)
  2051. {
  2052. return port_chk(hw, p,
  2053. KS8842_PORT_CTRL_2_OFFSET, PORT_DISCARD_NON_VID);
  2054. }
  2055. static inline int port_chk_in_filter(struct ksz_hw *hw, int p)
  2056. {
  2057. return port_chk(hw, p,
  2058. KS8842_PORT_CTRL_2_OFFSET, PORT_INGRESS_VLAN_FILTER);
  2059. }
  2060. /* Mirroring */
  2061. static inline void port_cfg_mirror_sniffer(struct ksz_hw *hw, int p, int set)
  2062. {
  2063. port_cfg(hw, p,
  2064. KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_SNIFFER, set);
  2065. }
  2066. static inline void port_cfg_mirror_rx(struct ksz_hw *hw, int p, int set)
  2067. {
  2068. port_cfg(hw, p,
  2069. KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_RX, set);
  2070. }
  2071. static inline void port_cfg_mirror_tx(struct ksz_hw *hw, int p, int set)
  2072. {
  2073. port_cfg(hw, p,
  2074. KS8842_PORT_CTRL_2_OFFSET, PORT_MIRROR_TX, set);
  2075. }
  2076. static inline void sw_cfg_mirror_rx_tx(struct ksz_hw *hw, int set)
  2077. {
  2078. sw_cfg(hw, KS8842_SWITCH_CTRL_2_OFFSET, SWITCH_MIRROR_RX_TX, set);
  2079. }
  2080. static void sw_init_mirror(struct ksz_hw *hw)
  2081. {
  2082. int port;
  2083. for (port = 0; port < TOTAL_PORT_NUM; port++) {
  2084. port_cfg_mirror_sniffer(hw, port, 0);
  2085. port_cfg_mirror_rx(hw, port, 0);
  2086. port_cfg_mirror_tx(hw, port, 0);
  2087. }
  2088. sw_cfg_mirror_rx_tx(hw, 0);
  2089. }
  2090. static inline void sw_cfg_unk_def_deliver(struct ksz_hw *hw, int set)
  2091. {
  2092. sw_cfg(hw, KS8842_SWITCH_CTRL_7_OFFSET,
  2093. SWITCH_UNK_DEF_PORT_ENABLE, set);
  2094. }
  2095. static inline int sw_cfg_chk_unk_def_deliver(struct ksz_hw *hw)
  2096. {
  2097. return sw_chk(hw, KS8842_SWITCH_CTRL_7_OFFSET,
  2098. SWITCH_UNK_DEF_PORT_ENABLE);
  2099. }
  2100. static inline void sw_cfg_unk_def_port(struct ksz_hw *hw, int port, int set)
  2101. {
  2102. port_cfg_shift(hw, port, KS8842_SWITCH_CTRL_7_OFFSET, 0, set);
  2103. }
  2104. static inline int sw_chk_unk_def_port(struct ksz_hw *hw, int port)
  2105. {
  2106. return port_chk_shift(hw, port, KS8842_SWITCH_CTRL_7_OFFSET, 0);
  2107. }
  2108. /* Priority */
  2109. static inline void port_cfg_diffserv(struct ksz_hw *hw, int p, int set)
  2110. {
  2111. port_cfg(hw, p,
  2112. KS8842_PORT_CTRL_1_OFFSET, PORT_DIFFSERV_ENABLE, set);
  2113. }
  2114. static inline void port_cfg_802_1p(struct ksz_hw *hw, int p, int set)
  2115. {
  2116. port_cfg(hw, p,
  2117. KS8842_PORT_CTRL_1_OFFSET, PORT_802_1P_ENABLE, set);
  2118. }
  2119. static inline void port_cfg_replace_vid(struct ksz_hw *hw, int p, int set)
  2120. {
  2121. port_cfg(hw, p,
  2122. KS8842_PORT_CTRL_2_OFFSET, PORT_USER_PRIORITY_CEILING, set);
  2123. }
  2124. static inline void port_cfg_prio(struct ksz_hw *hw, int p, int set)
  2125. {
  2126. port_cfg(hw, p,
  2127. KS8842_PORT_CTRL_1_OFFSET, PORT_PRIO_QUEUE_ENABLE, set);
  2128. }
  2129. static inline int port_chk_diffserv(struct ksz_hw *hw, int p)
  2130. {
  2131. return port_chk(hw, p,
  2132. KS8842_PORT_CTRL_1_OFFSET, PORT_DIFFSERV_ENABLE);
  2133. }
  2134. static inline int port_chk_802_1p(struct ksz_hw *hw, int p)
  2135. {
  2136. return port_chk(hw, p,
  2137. KS8842_PORT_CTRL_1_OFFSET, PORT_802_1P_ENABLE);
  2138. }
  2139. static inline int port_chk_replace_vid(struct ksz_hw *hw, int p)
  2140. {
  2141. return port_chk(hw, p,
  2142. KS8842_PORT_CTRL_2_OFFSET, PORT_USER_PRIORITY_CEILING);
  2143. }
  2144. static inline int port_chk_prio(struct ksz_hw *hw, int p)
  2145. {
  2146. return port_chk(hw, p,
  2147. KS8842_PORT_CTRL_1_OFFSET, PORT_PRIO_QUEUE_ENABLE);
  2148. }
  2149. /**
  2150. * sw_dis_diffserv - disable switch DiffServ priority
  2151. * @hw: The hardware instance.
  2152. * @port: The port index.
  2153. *
  2154. * This routine disables the DiffServ priority function of the switch.
  2155. */
  2156. static void sw_dis_diffserv(struct ksz_hw *hw, int port)
  2157. {
  2158. port_cfg_diffserv(hw, port, 0);
  2159. }
  2160. /**
  2161. * sw_dis_802_1p - disable switch 802.1p priority
  2162. * @hw: The hardware instance.
  2163. * @port: The port index.
  2164. *
  2165. * This routine disables the 802.1p priority function of the switch.
  2166. */
  2167. static void sw_dis_802_1p(struct ksz_hw *hw, int port)
  2168. {
  2169. port_cfg_802_1p(hw, port, 0);
  2170. }
  2171. /**
  2172. * sw_cfg_replace_null_vid -
  2173. * @hw: The hardware instance.
  2174. * @set: The flag to disable or enable.
  2175. *
  2176. */
  2177. static void sw_cfg_replace_null_vid(struct ksz_hw *hw, int set)
  2178. {
  2179. sw_cfg(hw, KS8842_SWITCH_CTRL_3_OFFSET, SWITCH_REPLACE_NULL_VID, set);
  2180. }
  2181. /**
  2182. * sw_cfg_replace_vid - enable switch 802.10 priority re-mapping
  2183. * @hw: The hardware instance.
  2184. * @port: The port index.
  2185. * @set: The flag to disable or enable.
  2186. *
  2187. * This routine enables the 802.1p priority re-mapping function of the switch.
  2188. * That allows 802.1p priority field to be replaced with the port's default
  2189. * tag's priority value if the ingress packet's 802.1p priority has a higher
  2190. * priority than port's default tag's priority.
  2191. */
  2192. static void sw_cfg_replace_vid(struct ksz_hw *hw, int port, int set)
  2193. {
  2194. port_cfg_replace_vid(hw, port, set);
  2195. }
  2196. /**
  2197. * sw_cfg_port_based - configure switch port based priority
  2198. * @hw: The hardware instance.
  2199. * @port: The port index.
  2200. * @prio: The priority to set.
  2201. *
  2202. * This routine configures the port based priority of the switch.
  2203. */
  2204. static void sw_cfg_port_based(struct ksz_hw *hw, int port, u8 prio)
  2205. {
  2206. u16 data;
  2207. if (prio > PORT_BASED_PRIORITY_BASE)
  2208. prio = PORT_BASED_PRIORITY_BASE;
  2209. hw->ksz_switch->port_cfg[port].port_prio = prio;
  2210. port_r16(hw, port, KS8842_PORT_CTRL_1_OFFSET, &data);
  2211. data &= ~PORT_BASED_PRIORITY_MASK;
  2212. data |= prio << PORT_BASED_PRIORITY_SHIFT;
  2213. port_w16(hw, port, KS8842_PORT_CTRL_1_OFFSET, data);
  2214. }
  2215. /**
  2216. * sw_dis_multi_queue - disable transmit multiple queues
  2217. * @hw: The hardware instance.
  2218. * @port: The port index.
  2219. *
  2220. * This routine disables the transmit multiple queues selection of the switch
  2221. * port. Only single transmit queue on the port.
  2222. */
  2223. static void sw_dis_multi_queue(struct ksz_hw *hw, int port)
  2224. {
  2225. port_cfg_prio(hw, port, 0);
  2226. }
  2227. /**
  2228. * sw_init_prio - initialize switch priority
  2229. * @hw: The hardware instance.
  2230. *
  2231. * This routine initializes the switch QoS priority functions.
  2232. */
  2233. static void sw_init_prio(struct ksz_hw *hw)
  2234. {
  2235. int port;
  2236. int tos;
  2237. struct ksz_switch *sw = hw->ksz_switch;
  2238. /*
  2239. * Init all the 802.1p tag priority value to be assigned to different
  2240. * priority queue.
  2241. */
  2242. sw->p_802_1p[0] = 0;
  2243. sw->p_802_1p[1] = 0;
  2244. sw->p_802_1p[2] = 1;
  2245. sw->p_802_1p[3] = 1;
  2246. sw->p_802_1p[4] = 2;
  2247. sw->p_802_1p[5] = 2;
  2248. sw->p_802_1p[6] = 3;
  2249. sw->p_802_1p[7] = 3;
  2250. /*
  2251. * Init all the DiffServ priority value to be assigned to priority
  2252. * queue 0.
  2253. */
  2254. for (tos = 0; tos < DIFFSERV_ENTRIES; tos++)
  2255. sw->diffserv[tos] = 0;
  2256. /* All QoS functions disabled. */
  2257. for (port = 0; port < TOTAL_PORT_NUM; port++) {
  2258. sw_dis_multi_queue(hw, port);
  2259. sw_dis_diffserv(hw, port);
  2260. sw_dis_802_1p(hw, port);
  2261. sw_cfg_replace_vid(hw, port, 0);
  2262. sw->port_cfg[port].port_prio = 0;
  2263. sw_cfg_port_based(hw, port, sw->port_cfg[port].port_prio);
  2264. }
  2265. sw_cfg_replace_null_vid(hw, 0);
  2266. }
  2267. /**
  2268. * port_get_def_vid - get port default VID.
  2269. * @hw: The hardware instance.
  2270. * @port: The port index.
  2271. * @vid: Buffer to store the VID.
  2272. *
  2273. * This routine retrieves the default VID of the port.
  2274. */
  2275. static void port_get_def_vid(struct ksz_hw *hw, int port, u16 *vid)
  2276. {
  2277. u32 addr;
  2278. PORT_CTRL_ADDR(port, addr);
  2279. addr += KS8842_PORT_CTRL_VID_OFFSET;
  2280. *vid = readw(hw->io + addr);
  2281. }
  2282. /**
  2283. * sw_init_vlan - initialize switch VLAN
  2284. * @hw: The hardware instance.
  2285. *
  2286. * This routine initializes the VLAN function of the switch.
  2287. */
  2288. static void sw_init_vlan(struct ksz_hw *hw)
  2289. {
  2290. int port;
  2291. int entry;
  2292. struct ksz_switch *sw = hw->ksz_switch;
  2293. /* Read 16 VLAN entries from device's VLAN table. */
  2294. for (entry = 0; entry < VLAN_TABLE_ENTRIES; entry++) {
  2295. sw_r_vlan_table(hw, entry,
  2296. &sw->vlan_table[entry].vid,
  2297. &sw->vlan_table[entry].fid,
  2298. &sw->vlan_table[entry].member);
  2299. }
  2300. for (port = 0; port < TOTAL_PORT_NUM; port++) {
  2301. port_get_def_vid(hw, port, &sw->port_cfg[port].vid);
  2302. sw->port_cfg[port].member = PORT_MASK;
  2303. }
  2304. }
  2305. /**
  2306. * sw_cfg_port_base_vlan - configure port-based VLAN membership
  2307. * @hw: The hardware instance.
  2308. * @port: The port index.
  2309. * @member: The port-based VLAN membership.
  2310. *
  2311. * This routine configures the port-based VLAN membership of the port.
  2312. */
  2313. static void sw_cfg_port_base_vlan(struct ksz_hw *hw, int port, u8 member)
  2314. {
  2315. u32 addr;
  2316. u8 data;
  2317. PORT_CTRL_ADDR(port, addr);
  2318. addr += KS8842_PORT_CTRL_2_OFFSET;
  2319. data = readb(hw->io + addr);
  2320. data &= ~PORT_VLAN_MEMBERSHIP;
  2321. data |= (member & PORT_MASK);
  2322. writeb(data, hw->io + addr);
  2323. hw->ksz_switch->port_cfg[port].member = member;
  2324. }
  2325. /**
  2326. * sw_get_addr - get the switch MAC address.
  2327. * @hw: The hardware instance.
  2328. * @mac_addr: Buffer to store the MAC address.
  2329. *
  2330. * This function retrieves the MAC address of the switch.
  2331. */
  2332. static inline void sw_get_addr(struct ksz_hw *hw, u8 *mac_addr)
  2333. {
  2334. int i;
  2335. for (i = 0; i < 6; i += 2) {
  2336. mac_addr[i] = readb(hw->io + KS8842_MAC_ADDR_0_OFFSET + i);
  2337. mac_addr[1 + i] = readb(hw->io + KS8842_MAC_ADDR_1_OFFSET + i);
  2338. }
  2339. }
  2340. /**
  2341. * sw_set_addr - configure switch MAC address
  2342. * @hw: The hardware instance.
  2343. * @mac_addr: The MAC address.
  2344. *
  2345. * This function configures the MAC address of the switch.
  2346. */
  2347. static void sw_set_addr(struct ksz_hw *hw, u8 *mac_addr)
  2348. {
  2349. int i;
  2350. for (i = 0; i < 6; i += 2) {
  2351. writeb(mac_addr[i], hw->io + KS8842_MAC_ADDR_0_OFFSET + i);
  2352. writeb(mac_addr[1 + i], hw->io + KS8842_MAC_ADDR_1_OFFSET + i);
  2353. }
  2354. }
  2355. /**
  2356. * sw_set_global_ctrl - set switch global control
  2357. * @hw: The hardware instance.
  2358. *
  2359. * This routine sets the global control of the switch function.
  2360. */
  2361. static void sw_set_global_ctrl(struct ksz_hw *hw)
  2362. {
  2363. u16 data;
  2364. /* Enable switch MII flow control. */
  2365. data = readw(hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
  2366. data |= SWITCH_FLOW_CTRL;
  2367. writew(data, hw->io + KS8842_SWITCH_CTRL_3_OFFSET);
  2368. data = readw(hw->io + KS8842_SWITCH_CTRL_1_OFFSET);
  2369. /* Enable aggressive back off algorithm in half duplex mode. */
  2370. data |= SWITCH_AGGR_BACKOFF;
  2371. /* Enable automatic fast aging when link changed detected. */
  2372. data |= SWITCH_AGING_ENABLE;
  2373. data |= SWITCH_LINK_AUTO_AGING;
  2374. if (hw->overrides & FAST_AGING)
  2375. data |= SWITCH_FAST_AGING;
  2376. else
  2377. data &= ~SWITCH_FAST_AGING;
  2378. writew(data, hw->io + KS8842_SWITCH_CTRL_1_OFFSET);
  2379. data = readw(hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
  2380. /* Enable no excessive collision drop. */
  2381. data |= NO_EXC_COLLISION_DROP;
  2382. writew(data, hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
  2383. }
  2384. enum {
  2385. STP_STATE_DISABLED = 0,
  2386. STP_STATE_LISTENING,
  2387. STP_STATE_LEARNING,
  2388. STP_STATE_FORWARDING,
  2389. STP_STATE_BLOCKED,
  2390. STP_STATE_SIMPLE
  2391. };
  2392. /**
  2393. * port_set_stp_state - configure port spanning tree state
  2394. * @hw: The hardware instance.
  2395. * @port: The port index.
  2396. * @state: The spanning tree state.
  2397. *
  2398. * This routine configures the spanning tree state of the port.
  2399. */
  2400. static void port_set_stp_state(struct ksz_hw *hw, int port, int state)
  2401. {
  2402. u16 data;
  2403. port_r16(hw, port, KS8842_PORT_CTRL_2_OFFSET, &data);
  2404. switch (state) {
  2405. case STP_STATE_DISABLED:
  2406. data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE);
  2407. data |= PORT_LEARN_DISABLE;
  2408. break;
  2409. case STP_STATE_LISTENING:
  2410. /*
  2411. * No need to turn on transmit because of port direct mode.
  2412. * Turning on receive is required if static MAC table is not setup.
  2413. */
  2414. data &= ~PORT_TX_ENABLE;
  2415. data |= PORT_RX_ENABLE;
  2416. data |= PORT_LEARN_DISABLE;
  2417. break;
  2418. case STP_STATE_LEARNING:
  2419. data &= ~PORT_TX_ENABLE;
  2420. data |= PORT_RX_ENABLE;
  2421. data &= ~PORT_LEARN_DISABLE;
  2422. break;
  2423. case STP_STATE_FORWARDING:
  2424. data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
  2425. data &= ~PORT_LEARN_DISABLE;
  2426. break;
  2427. case STP_STATE_BLOCKED:
  2428. /*
  2429. * Need to setup static MAC table with override to keep receiving BPDU
  2430. * messages. See sw_init_stp routine.
  2431. */
  2432. data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE);
  2433. data |= PORT_LEARN_DISABLE;
  2434. break;
  2435. case STP_STATE_SIMPLE:
  2436. data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
  2437. data |= PORT_LEARN_DISABLE;
  2438. break;
  2439. }
  2440. port_w16(hw, port, KS8842_PORT_CTRL_2_OFFSET, data);
  2441. hw->ksz_switch->port_cfg[port].stp_state = state;
  2442. }
  2443. #define STP_ENTRY 0
  2444. #define BROADCAST_ENTRY 1
  2445. #define BRIDGE_ADDR_ENTRY 2
  2446. #define IPV6_ADDR_ENTRY 3
  2447. /**
  2448. * sw_clr_sta_mac_table - clear static MAC table
  2449. * @hw: The hardware instance.
  2450. *
  2451. * This routine clears the static MAC table.
  2452. */
  2453. static void sw_clr_sta_mac_table(struct ksz_hw *hw)
  2454. {
  2455. struct ksz_mac_table *entry;
  2456. int i;
  2457. for (i = 0; i < STATIC_MAC_TABLE_ENTRIES; i++) {
  2458. entry = &hw->ksz_switch->mac_table[i];
  2459. sw_w_sta_mac_table(hw, i,
  2460. entry->mac_addr, entry->ports,
  2461. entry->override, 0,
  2462. entry->use_fid, entry->fid);
  2463. }
  2464. }
  2465. /**
  2466. * sw_init_stp - initialize switch spanning tree support
  2467. * @hw: The hardware instance.
  2468. *
  2469. * This routine initializes the spanning tree support of the switch.
  2470. */
  2471. static void sw_init_stp(struct ksz_hw *hw)
  2472. {
  2473. struct ksz_mac_table *entry;
  2474. entry = &hw->ksz_switch->mac_table[STP_ENTRY];
  2475. entry->mac_addr[0] = 0x01;
  2476. entry->mac_addr[1] = 0x80;
  2477. entry->mac_addr[2] = 0xC2;
  2478. entry->mac_addr[3] = 0x00;
  2479. entry->mac_addr[4] = 0x00;
  2480. entry->mac_addr[5] = 0x00;
  2481. entry->ports = HOST_MASK;
  2482. entry->override = 1;
  2483. entry->valid = 1;
  2484. sw_w_sta_mac_table(hw, STP_ENTRY,
  2485. entry->mac_addr, entry->ports,
  2486. entry->override, entry->valid,
  2487. entry->use_fid, entry->fid);
  2488. }
  2489. /**
  2490. * sw_block_addr - block certain packets from the host port
  2491. * @hw: The hardware instance.
  2492. *
  2493. * This routine blocks certain packets from reaching to the host port.
  2494. */
  2495. static void sw_block_addr(struct ksz_hw *hw)
  2496. {
  2497. struct ksz_mac_table *entry;
  2498. int i;
  2499. for (i = BROADCAST_ENTRY; i <= IPV6_ADDR_ENTRY; i++) {
  2500. entry = &hw->ksz_switch->mac_table[i];
  2501. entry->valid = 0;
  2502. sw_w_sta_mac_table(hw, i,
  2503. entry->mac_addr, entry->ports,
  2504. entry->override, entry->valid,
  2505. entry->use_fid, entry->fid);
  2506. }
  2507. }
  2508. #define PHY_LINK_SUPPORT \
  2509. (PHY_AUTO_NEG_ASYM_PAUSE | \
  2510. PHY_AUTO_NEG_SYM_PAUSE | \
  2511. PHY_AUTO_NEG_100BT4 | \
  2512. PHY_AUTO_NEG_100BTX_FD | \
  2513. PHY_AUTO_NEG_100BTX | \
  2514. PHY_AUTO_NEG_10BT_FD | \
  2515. PHY_AUTO_NEG_10BT)
  2516. static inline void hw_r_phy_ctrl(struct ksz_hw *hw, int phy, u16 *data)
  2517. {
  2518. *data = readw(hw->io + phy + KS884X_PHY_CTRL_OFFSET);
  2519. }
  2520. static inline void hw_w_phy_ctrl(struct ksz_hw *hw, int phy, u16 data)
  2521. {
  2522. writew(data, hw->io + phy + KS884X_PHY_CTRL_OFFSET);
  2523. }
  2524. static inline void hw_r_phy_link_stat(struct ksz_hw *hw, int phy, u16 *data)
  2525. {
  2526. *data = readw(hw->io + phy + KS884X_PHY_STATUS_OFFSET);
  2527. }
  2528. static inline void hw_r_phy_auto_neg(struct ksz_hw *hw, int phy, u16 *data)
  2529. {
  2530. *data = readw(hw->io + phy + KS884X_PHY_AUTO_NEG_OFFSET);
  2531. }
  2532. static inline void hw_w_phy_auto_neg(struct ksz_hw *hw, int phy, u16 data)
  2533. {
  2534. writew(data, hw->io + phy + KS884X_PHY_AUTO_NEG_OFFSET);
  2535. }
  2536. static inline void hw_r_phy_rem_cap(struct ksz_hw *hw, int phy, u16 *data)
  2537. {
  2538. *data = readw(hw->io + phy + KS884X_PHY_REMOTE_CAP_OFFSET);
  2539. }
  2540. static inline void hw_r_phy_crossover(struct ksz_hw *hw, int phy, u16 *data)
  2541. {
  2542. *data = readw(hw->io + phy + KS884X_PHY_CTRL_OFFSET);
  2543. }
  2544. static inline void hw_w_phy_crossover(struct ksz_hw *hw, int phy, u16 data)
  2545. {
  2546. writew(data, hw->io + phy + KS884X_PHY_CTRL_OFFSET);
  2547. }
  2548. static inline void hw_r_phy_polarity(struct ksz_hw *hw, int phy, u16 *data)
  2549. {
  2550. *data = readw(hw->io + phy + KS884X_PHY_PHY_CTRL_OFFSET);
  2551. }
  2552. static inline void hw_w_phy_polarity(struct ksz_hw *hw, int phy, u16 data)
  2553. {
  2554. writew(data, hw->io + phy + KS884X_PHY_PHY_CTRL_OFFSET);
  2555. }
  2556. static inline void hw_r_phy_link_md(struct ksz_hw *hw, int phy, u16 *data)
  2557. {
  2558. *data = readw(hw->io + phy + KS884X_PHY_LINK_MD_OFFSET);
  2559. }
  2560. static inline void hw_w_phy_link_md(struct ksz_hw *hw, int phy, u16 data)
  2561. {
  2562. writew(data, hw->io + phy + KS884X_PHY_LINK_MD_OFFSET);
  2563. }
  2564. /**
  2565. * hw_r_phy - read data from PHY register
  2566. * @hw: The hardware instance.
  2567. * @port: Port to read.
  2568. * @reg: PHY register to read.
  2569. * @val: Buffer to store the read data.
  2570. *
  2571. * This routine reads data from the PHY register.
  2572. */
  2573. static void hw_r_phy(struct ksz_hw *hw, int port, u16 reg, u16 *val)
  2574. {
  2575. int phy;
  2576. phy = KS884X_PHY_1_CTRL_OFFSET + port * PHY_CTRL_INTERVAL + reg;
  2577. *val = readw(hw->io + phy);
  2578. }
  2579. /**
  2580. * port_w_phy - write data to PHY register
  2581. * @hw: The hardware instance.
  2582. * @port: Port to write.
  2583. * @reg: PHY register to write.
  2584. * @val: Word data to write.
  2585. *
  2586. * This routine writes data to the PHY register.
  2587. */
  2588. static void hw_w_phy(struct ksz_hw *hw, int port, u16 reg, u16 val)
  2589. {
  2590. int phy;
  2591. phy = KS884X_PHY_1_CTRL_OFFSET + port * PHY_CTRL_INTERVAL + reg;
  2592. writew(val, hw->io + phy);
  2593. }
  2594. /*
  2595. * EEPROM access functions
  2596. */
  2597. #define AT93C_CODE 0
  2598. #define AT93C_WR_OFF 0x00
  2599. #define AT93C_WR_ALL 0x10
  2600. #define AT93C_ER_ALL 0x20
  2601. #define AT93C_WR_ON 0x30
  2602. #define AT93C_WRITE 1
  2603. #define AT93C_READ 2
  2604. #define AT93C_ERASE 3
  2605. #define EEPROM_DELAY 4
  2606. static inline void drop_gpio(struct ksz_hw *hw, u8 gpio)
  2607. {
  2608. u16 data;
  2609. data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
  2610. data &= ~gpio;
  2611. writew(data, hw->io + KS884X_EEPROM_CTRL_OFFSET);
  2612. }
  2613. static inline void raise_gpio(struct ksz_hw *hw, u8 gpio)
  2614. {
  2615. u16 data;
  2616. data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
  2617. data |= gpio;
  2618. writew(data, hw->io + KS884X_EEPROM_CTRL_OFFSET);
  2619. }
  2620. static inline u8 state_gpio(struct ksz_hw *hw, u8 gpio)
  2621. {
  2622. u16 data;
  2623. data = readw(hw->io + KS884X_EEPROM_CTRL_OFFSET);
  2624. return (u8)(data & gpio);
  2625. }
  2626. static void eeprom_clk(struct ksz_hw *hw)
  2627. {
  2628. raise_gpio(hw, EEPROM_SERIAL_CLOCK);
  2629. udelay(EEPROM_DELAY);
  2630. drop_gpio(hw, EEPROM_SERIAL_CLOCK);
  2631. udelay(EEPROM_DELAY);
  2632. }
  2633. static u16 spi_r(struct ksz_hw *hw)
  2634. {
  2635. int i;
  2636. u16 temp = 0;
  2637. for (i = 15; i >= 0; i--) {
  2638. raise_gpio(hw, EEPROM_SERIAL_CLOCK);
  2639. udelay(EEPROM_DELAY);
  2640. temp |= (state_gpio(hw, EEPROM_DATA_IN)) ? 1 << i : 0;
  2641. drop_gpio(hw, EEPROM_SERIAL_CLOCK);
  2642. udelay(EEPROM_DELAY);
  2643. }
  2644. return temp;
  2645. }
  2646. static void spi_w(struct ksz_hw *hw, u16 data)
  2647. {
  2648. int i;
  2649. for (i = 15; i >= 0; i--) {
  2650. (data & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
  2651. drop_gpio(hw, EEPROM_DATA_OUT);
  2652. eeprom_clk(hw);
  2653. }
  2654. }
  2655. static void spi_reg(struct ksz_hw *hw, u8 data, u8 reg)
  2656. {
  2657. int i;
  2658. /* Initial start bit */
  2659. raise_gpio(hw, EEPROM_DATA_OUT);
  2660. eeprom_clk(hw);
  2661. /* AT93C operation */
  2662. for (i = 1; i >= 0; i--) {
  2663. (data & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
  2664. drop_gpio(hw, EEPROM_DATA_OUT);
  2665. eeprom_clk(hw);
  2666. }
  2667. /* Address location */
  2668. for (i = 5; i >= 0; i--) {
  2669. (reg & (0x01 << i)) ? raise_gpio(hw, EEPROM_DATA_OUT) :
  2670. drop_gpio(hw, EEPROM_DATA_OUT);
  2671. eeprom_clk(hw);
  2672. }
  2673. }
  2674. #define EEPROM_DATA_RESERVED 0
  2675. #define EEPROM_DATA_MAC_ADDR_0 1
  2676. #define EEPROM_DATA_MAC_ADDR_1 2
  2677. #define EEPROM_DATA_MAC_ADDR_2 3
  2678. #define EEPROM_DATA_SUBSYS_ID 4
  2679. #define EEPROM_DATA_SUBSYS_VEN_ID 5
  2680. #define EEPROM_DATA_PM_CAP 6
  2681. /* User defined EEPROM data */
  2682. #define EEPROM_DATA_OTHER_MAC_ADDR 9
  2683. /**
  2684. * eeprom_read - read from AT93C46 EEPROM
  2685. * @hw: The hardware instance.
  2686. * @reg: The register offset.
  2687. *
  2688. * This function reads a word from the AT93C46 EEPROM.
  2689. *
  2690. * Return the data value.
  2691. */
  2692. static u16 eeprom_read(struct ksz_hw *hw, u8 reg)
  2693. {
  2694. u16 data;
  2695. raise_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
  2696. spi_reg(hw, AT93C_READ, reg);
  2697. data = spi_r(hw);
  2698. drop_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
  2699. return data;
  2700. }
  2701. /**
  2702. * eeprom_write - write to AT93C46 EEPROM
  2703. * @hw: The hardware instance.
  2704. * @reg: The register offset.
  2705. * @data: The data value.
  2706. *
  2707. * This procedure writes a word to the AT93C46 EEPROM.
  2708. */
  2709. static void eeprom_write(struct ksz_hw *hw, u8 reg, u16 data)
  2710. {
  2711. int timeout;
  2712. raise_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
  2713. /* Enable write. */
  2714. spi_reg(hw, AT93C_CODE, AT93C_WR_ON);
  2715. drop_gpio(hw, EEPROM_CHIP_SELECT);
  2716. udelay(1);
  2717. /* Erase the register. */
  2718. raise_gpio(hw, EEPROM_CHIP_SELECT);
  2719. spi_reg(hw, AT93C_ERASE, reg);
  2720. drop_gpio(hw, EEPROM_CHIP_SELECT);
  2721. udelay(1);
  2722. /* Check operation complete. */
  2723. raise_gpio(hw, EEPROM_CHIP_SELECT);
  2724. timeout = 8;
  2725. mdelay(2);
  2726. do {
  2727. mdelay(1);
  2728. } while (!state_gpio(hw, EEPROM_DATA_IN) && --timeout);
  2729. drop_gpio(hw, EEPROM_CHIP_SELECT);
  2730. udelay(1);
  2731. /* Write the register. */
  2732. raise_gpio(hw, EEPROM_CHIP_SELECT);
  2733. spi_reg(hw, AT93C_WRITE, reg);
  2734. spi_w(hw, data);
  2735. drop_gpio(hw, EEPROM_CHIP_SELECT);
  2736. udelay(1);
  2737. /* Check operation complete. */
  2738. raise_gpio(hw, EEPROM_CHIP_SELECT);
  2739. timeout = 8;
  2740. mdelay(2);
  2741. do {
  2742. mdelay(1);
  2743. } while (!state_gpio(hw, EEPROM_DATA_IN) && --timeout);
  2744. drop_gpio(hw, EEPROM_CHIP_SELECT);
  2745. udelay(1);
  2746. /* Disable write. */
  2747. raise_gpio(hw, EEPROM_CHIP_SELECT);
  2748. spi_reg(hw, AT93C_CODE, AT93C_WR_OFF);
  2749. drop_gpio(hw, EEPROM_ACCESS_ENABLE | EEPROM_CHIP_SELECT);
  2750. }
  2751. /*
  2752. * Link detection routines
  2753. */
  2754. static u16 advertised_flow_ctrl(struct ksz_port *port, u16 ctrl)
  2755. {
  2756. ctrl &= ~PORT_AUTO_NEG_SYM_PAUSE;
  2757. switch (port->flow_ctrl) {
  2758. case PHY_FLOW_CTRL:
  2759. ctrl |= PORT_AUTO_NEG_SYM_PAUSE;
  2760. break;
  2761. /* Not supported. */
  2762. case PHY_TX_ONLY:
  2763. case PHY_RX_ONLY:
  2764. default:
  2765. break;
  2766. }
  2767. return ctrl;
  2768. }
  2769. static void set_flow_ctrl(struct ksz_hw *hw, int rx, int tx)
  2770. {
  2771. u32 rx_cfg;
  2772. u32 tx_cfg;
  2773. rx_cfg = hw->rx_cfg;
  2774. tx_cfg = hw->tx_cfg;
  2775. if (rx)
  2776. hw->rx_cfg |= DMA_RX_FLOW_ENABLE;
  2777. else
  2778. hw->rx_cfg &= ~DMA_RX_FLOW_ENABLE;
  2779. if (tx)
  2780. hw->tx_cfg |= DMA_TX_FLOW_ENABLE;
  2781. else
  2782. hw->tx_cfg &= ~DMA_TX_FLOW_ENABLE;
  2783. if (hw->enabled) {
  2784. if (rx_cfg != hw->rx_cfg)
  2785. writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
  2786. if (tx_cfg != hw->tx_cfg)
  2787. writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
  2788. }
  2789. }
  2790. static void determine_flow_ctrl(struct ksz_hw *hw, struct ksz_port *port,
  2791. u16 local, u16 remote)
  2792. {
  2793. int rx;
  2794. int tx;
  2795. if (hw->overrides & PAUSE_FLOW_CTRL)
  2796. return;
  2797. rx = tx = 0;
  2798. if (port->force_link)
  2799. rx = tx = 1;
  2800. if (remote & PHY_AUTO_NEG_SYM_PAUSE) {
  2801. if (local & PHY_AUTO_NEG_SYM_PAUSE) {
  2802. rx = tx = 1;
  2803. } else if ((remote & PHY_AUTO_NEG_ASYM_PAUSE) &&
  2804. (local & PHY_AUTO_NEG_PAUSE) ==
  2805. PHY_AUTO_NEG_ASYM_PAUSE) {
  2806. tx = 1;
  2807. }
  2808. } else if (remote & PHY_AUTO_NEG_ASYM_PAUSE) {
  2809. if ((local & PHY_AUTO_NEG_PAUSE) == PHY_AUTO_NEG_PAUSE)
  2810. rx = 1;
  2811. }
  2812. if (!hw->ksz_switch)
  2813. set_flow_ctrl(hw, rx, tx);
  2814. }
  2815. static inline void port_cfg_change(struct ksz_hw *hw, struct ksz_port *port,
  2816. struct ksz_port_info *info, u16 link_status)
  2817. {
  2818. if ((hw->features & HALF_DUPLEX_SIGNAL_BUG) &&
  2819. !(hw->overrides & PAUSE_FLOW_CTRL)) {
  2820. u32 cfg = hw->tx_cfg;
  2821. /* Disable flow control in the half duplex mode. */
  2822. if (1 == info->duplex)
  2823. hw->tx_cfg &= ~DMA_TX_FLOW_ENABLE;
  2824. if (hw->enabled && cfg != hw->tx_cfg)
  2825. writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
  2826. }
  2827. }
  2828. /**
  2829. * port_get_link_speed - get current link status
  2830. * @port: The port instance.
  2831. *
  2832. * This routine reads PHY registers to determine the current link status of the
  2833. * switch ports.
  2834. */
  2835. static void port_get_link_speed(struct ksz_port *port)
  2836. {
  2837. uint interrupt;
  2838. struct ksz_port_info *info;
  2839. struct ksz_port_info *linked = NULL;
  2840. struct ksz_hw *hw = port->hw;
  2841. u16 data;
  2842. u16 status;
  2843. u8 local;
  2844. u8 remote;
  2845. int i;
  2846. int p;
  2847. int change = 0;
  2848. interrupt = hw_block_intr(hw);
  2849. for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
  2850. info = &hw->port_info[p];
  2851. port_r16(hw, p, KS884X_PORT_CTRL_4_OFFSET, &data);
  2852. port_r16(hw, p, KS884X_PORT_STATUS_OFFSET, &status);
  2853. /*
  2854. * Link status is changing all the time even when there is no
  2855. * cable connection!
  2856. */
  2857. remote = status & (PORT_AUTO_NEG_COMPLETE |
  2858. PORT_STATUS_LINK_GOOD);
  2859. local = (u8) data;
  2860. /* No change to status. */
  2861. if (local == info->advertised && remote == info->partner)
  2862. continue;
  2863. info->advertised = local;
  2864. info->partner = remote;
  2865. if (status & PORT_STATUS_LINK_GOOD) {
  2866. /* Remember the first linked port. */
  2867. if (!linked)
  2868. linked = info;
  2869. info->tx_rate = 10 * TX_RATE_UNIT;
  2870. if (status & PORT_STATUS_SPEED_100MBIT)
  2871. info->tx_rate = 100 * TX_RATE_UNIT;
  2872. info->duplex = 1;
  2873. if (status & PORT_STATUS_FULL_DUPLEX)
  2874. info->duplex = 2;
  2875. if (media_connected != info->state) {
  2876. hw_r_phy(hw, p, KS884X_PHY_AUTO_NEG_OFFSET,
  2877. &data);
  2878. hw_r_phy(hw, p, KS884X_PHY_REMOTE_CAP_OFFSET,
  2879. &status);
  2880. determine_flow_ctrl(hw, port, data, status);
  2881. if (hw->ksz_switch) {
  2882. port_cfg_back_pressure(hw, p,
  2883. (1 == info->duplex));
  2884. }
  2885. change |= 1 << i;
  2886. port_cfg_change(hw, port, info, status);
  2887. }
  2888. info->state = media_connected;
  2889. } else {
  2890. if (media_disconnected != info->state) {
  2891. change |= 1 << i;
  2892. /* Indicate the link just goes down. */
  2893. hw->port_mib[p].link_down = 1;
  2894. }
  2895. info->state = media_disconnected;
  2896. }
  2897. hw->port_mib[p].state = (u8) info->state;
  2898. }
  2899. if (linked && media_disconnected == port->linked->state)
  2900. port->linked = linked;
  2901. hw_restore_intr(hw, interrupt);
  2902. }
  2903. #define PHY_RESET_TIMEOUT 10
  2904. /**
  2905. * port_set_link_speed - set port speed
  2906. * @port: The port instance.
  2907. *
  2908. * This routine sets the link speed of the switch ports.
  2909. */
  2910. static void port_set_link_speed(struct ksz_port *port)
  2911. {
  2912. struct ksz_port_info *info;
  2913. struct ksz_hw *hw = port->hw;
  2914. u16 data;
  2915. u16 cfg;
  2916. u8 status;
  2917. int i;
  2918. int p;
  2919. for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
  2920. info = &hw->port_info[p];
  2921. port_r16(hw, p, KS884X_PORT_CTRL_4_OFFSET, &data);
  2922. port_r8(hw, p, KS884X_PORT_STATUS_OFFSET, &status);
  2923. cfg = 0;
  2924. if (status & PORT_STATUS_LINK_GOOD)
  2925. cfg = data;
  2926. data |= PORT_AUTO_NEG_ENABLE;
  2927. data = advertised_flow_ctrl(port, data);
  2928. data |= PORT_AUTO_NEG_100BTX_FD | PORT_AUTO_NEG_100BTX |
  2929. PORT_AUTO_NEG_10BT_FD | PORT_AUTO_NEG_10BT;
  2930. /* Check if manual configuration is specified by the user. */
  2931. if (port->speed || port->duplex) {
  2932. if (10 == port->speed)
  2933. data &= ~(PORT_AUTO_NEG_100BTX_FD |
  2934. PORT_AUTO_NEG_100BTX);
  2935. else if (100 == port->speed)
  2936. data &= ~(PORT_AUTO_NEG_10BT_FD |
  2937. PORT_AUTO_NEG_10BT);
  2938. if (1 == port->duplex)
  2939. data &= ~(PORT_AUTO_NEG_100BTX_FD |
  2940. PORT_AUTO_NEG_10BT_FD);
  2941. else if (2 == port->duplex)
  2942. data &= ~(PORT_AUTO_NEG_100BTX |
  2943. PORT_AUTO_NEG_10BT);
  2944. }
  2945. if (data != cfg) {
  2946. data |= PORT_AUTO_NEG_RESTART;
  2947. port_w16(hw, p, KS884X_PORT_CTRL_4_OFFSET, data);
  2948. }
  2949. }
  2950. }
  2951. /**
  2952. * port_force_link_speed - force port speed
  2953. * @port: The port instance.
  2954. *
  2955. * This routine forces the link speed of the switch ports.
  2956. */
  2957. static void port_force_link_speed(struct ksz_port *port)
  2958. {
  2959. struct ksz_hw *hw = port->hw;
  2960. u16 data;
  2961. int i;
  2962. int phy;
  2963. int p;
  2964. for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
  2965. phy = KS884X_PHY_1_CTRL_OFFSET + p * PHY_CTRL_INTERVAL;
  2966. hw_r_phy_ctrl(hw, phy, &data);
  2967. data &= ~PHY_AUTO_NEG_ENABLE;
  2968. if (10 == port->speed)
  2969. data &= ~PHY_SPEED_100MBIT;
  2970. else if (100 == port->speed)
  2971. data |= PHY_SPEED_100MBIT;
  2972. if (1 == port->duplex)
  2973. data &= ~PHY_FULL_DUPLEX;
  2974. else if (2 == port->duplex)
  2975. data |= PHY_FULL_DUPLEX;
  2976. hw_w_phy_ctrl(hw, phy, data);
  2977. }
  2978. }
  2979. static void port_set_power_saving(struct ksz_port *port, int enable)
  2980. {
  2981. struct ksz_hw *hw = port->hw;
  2982. int i;
  2983. int p;
  2984. for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++)
  2985. port_cfg(hw, p,
  2986. KS884X_PORT_CTRL_4_OFFSET, PORT_POWER_DOWN, enable);
  2987. }
  2988. /*
  2989. * KSZ8841 power management functions
  2990. */
  2991. /**
  2992. * hw_chk_wol_pme_status - check PMEN pin
  2993. * @hw: The hardware instance.
  2994. *
  2995. * This function is used to check PMEN pin is asserted.
  2996. *
  2997. * Return 1 if PMEN pin is asserted; otherwise, 0.
  2998. */
  2999. static int hw_chk_wol_pme_status(struct ksz_hw *hw)
  3000. {
  3001. struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
  3002. struct pci_dev *pdev = hw_priv->pdev;
  3003. u16 data;
  3004. if (!pdev->pm_cap)
  3005. return 0;
  3006. pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
  3007. return (data & PCI_PM_CTRL_PME_STATUS) == PCI_PM_CTRL_PME_STATUS;
  3008. }
  3009. /**
  3010. * hw_clr_wol_pme_status - clear PMEN pin
  3011. * @hw: The hardware instance.
  3012. *
  3013. * This routine is used to clear PME_Status to deassert PMEN pin.
  3014. */
  3015. static void hw_clr_wol_pme_status(struct ksz_hw *hw)
  3016. {
  3017. struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
  3018. struct pci_dev *pdev = hw_priv->pdev;
  3019. u16 data;
  3020. if (!pdev->pm_cap)
  3021. return;
  3022. /* Clear PME_Status to deassert PMEN pin. */
  3023. pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
  3024. data |= PCI_PM_CTRL_PME_STATUS;
  3025. pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, data);
  3026. }
  3027. /**
  3028. * hw_cfg_wol_pme - enable or disable Wake-on-LAN
  3029. * @hw: The hardware instance.
  3030. * @set: The flag indicating whether to enable or disable.
  3031. *
  3032. * This routine is used to enable or disable Wake-on-LAN.
  3033. */
  3034. static void hw_cfg_wol_pme(struct ksz_hw *hw, int set)
  3035. {
  3036. struct dev_info *hw_priv = container_of(hw, struct dev_info, hw);
  3037. struct pci_dev *pdev = hw_priv->pdev;
  3038. u16 data;
  3039. if (!pdev->pm_cap)
  3040. return;
  3041. pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &data);
  3042. data &= ~PCI_PM_CTRL_STATE_MASK;
  3043. if (set)
  3044. data |= PCI_PM_CTRL_PME_ENABLE | PCI_D3hot;
  3045. else
  3046. data &= ~PCI_PM_CTRL_PME_ENABLE;
  3047. pci_write_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, data);
  3048. }
  3049. /**
  3050. * hw_cfg_wol - configure Wake-on-LAN features
  3051. * @hw: The hardware instance.
  3052. * @frame: The pattern frame bit.
  3053. * @set: The flag indicating whether to enable or disable.
  3054. *
  3055. * This routine is used to enable or disable certain Wake-on-LAN features.
  3056. */
  3057. static void hw_cfg_wol(struct ksz_hw *hw, u16 frame, int set)
  3058. {
  3059. u16 data;
  3060. data = readw(hw->io + KS8841_WOL_CTRL_OFFSET);
  3061. if (set)
  3062. data |= frame;
  3063. else
  3064. data &= ~frame;
  3065. writew(data, hw->io + KS8841_WOL_CTRL_OFFSET);
  3066. }
  3067. /**
  3068. * hw_set_wol_frame - program Wake-on-LAN pattern
  3069. * @hw: The hardware instance.
  3070. * @i: The frame index.
  3071. * @mask_size: The size of the mask.
  3072. * @mask: Mask to ignore certain bytes in the pattern.
  3073. * @frame_size: The size of the frame.
  3074. * @pattern: The frame data.
  3075. *
  3076. * This routine is used to program Wake-on-LAN pattern.
  3077. */
  3078. static void hw_set_wol_frame(struct ksz_hw *hw, int i, uint mask_size,
  3079. const u8 *mask, uint frame_size, const u8 *pattern)
  3080. {
  3081. int bits;
  3082. int from;
  3083. int len;
  3084. int to;
  3085. u32 crc;
  3086. u8 data[64];
  3087. u8 val = 0;
  3088. if (frame_size > mask_size * 8)
  3089. frame_size = mask_size * 8;
  3090. if (frame_size > 64)
  3091. frame_size = 64;
  3092. i *= 0x10;
  3093. writel(0, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i);
  3094. writel(0, hw->io + KS8841_WOL_FRAME_BYTE2_OFFSET + i);
  3095. bits = len = from = to = 0;
  3096. do {
  3097. if (bits) {
  3098. if ((val & 1))
  3099. data[to++] = pattern[from];
  3100. val >>= 1;
  3101. ++from;
  3102. --bits;
  3103. } else {
  3104. val = mask[len];
  3105. writeb(val, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i
  3106. + len);
  3107. ++len;
  3108. if (val)
  3109. bits = 8;
  3110. else
  3111. from += 8;
  3112. }
  3113. } while (from < (int) frame_size);
  3114. if (val) {
  3115. bits = mask[len - 1];
  3116. val <<= (from % 8);
  3117. bits &= ~val;
  3118. writeb(bits, hw->io + KS8841_WOL_FRAME_BYTE0_OFFSET + i + len -
  3119. 1);
  3120. }
  3121. crc = ether_crc(to, data);
  3122. writel(crc, hw->io + KS8841_WOL_FRAME_CRC_OFFSET + i);
  3123. }
  3124. /**
  3125. * hw_add_wol_arp - add ARP pattern
  3126. * @hw: The hardware instance.
  3127. * @ip_addr: The IPv4 address assigned to the device.
  3128. *
  3129. * This routine is used to add ARP pattern for waking up the host.
  3130. */
  3131. static void hw_add_wol_arp(struct ksz_hw *hw, const u8 *ip_addr)
  3132. {
  3133. static const u8 mask[6] = { 0x3F, 0xF0, 0x3F, 0x00, 0xC0, 0x03 };
  3134. u8 pattern[42] = {
  3135. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  3136. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3137. 0x08, 0x06,
  3138. 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01,
  3139. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3140. 0x00, 0x00, 0x00, 0x00,
  3141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3142. 0x00, 0x00, 0x00, 0x00 };
  3143. memcpy(&pattern[38], ip_addr, 4);
  3144. hw_set_wol_frame(hw, 3, 6, mask, 42, pattern);
  3145. }
  3146. /**
  3147. * hw_add_wol_bcast - add broadcast pattern
  3148. * @hw: The hardware instance.
  3149. *
  3150. * This routine is used to add broadcast pattern for waking up the host.
  3151. */
  3152. static void hw_add_wol_bcast(struct ksz_hw *hw)
  3153. {
  3154. static const u8 mask[] = { 0x3F };
  3155. static const u8 pattern[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  3156. hw_set_wol_frame(hw, 2, 1, mask, MAC_ADDR_LEN, pattern);
  3157. }
  3158. /**
  3159. * hw_add_wol_mcast - add multicast pattern
  3160. * @hw: The hardware instance.
  3161. *
  3162. * This routine is used to add multicast pattern for waking up the host.
  3163. *
  3164. * It is assumed the multicast packet is the ICMPv6 neighbor solicitation used
  3165. * by IPv6 ping command. Note that multicast packets are filtred through the
  3166. * multicast hash table, so not all multicast packets can wake up the host.
  3167. */
  3168. static void hw_add_wol_mcast(struct ksz_hw *hw)
  3169. {
  3170. static const u8 mask[] = { 0x3F };
  3171. u8 pattern[] = { 0x33, 0x33, 0xFF, 0x00, 0x00, 0x00 };
  3172. memcpy(&pattern[3], &hw->override_addr[3], 3);
  3173. hw_set_wol_frame(hw, 1, 1, mask, 6, pattern);
  3174. }
  3175. /**
  3176. * hw_add_wol_ucast - add unicast pattern
  3177. * @hw: The hardware instance.
  3178. *
  3179. * This routine is used to add unicast pattern to wakeup the host.
  3180. *
  3181. * It is assumed the unicast packet is directed to the device, as the hardware
  3182. * can only receive them in normal case.
  3183. */
  3184. static void hw_add_wol_ucast(struct ksz_hw *hw)
  3185. {
  3186. static const u8 mask[] = { 0x3F };
  3187. hw_set_wol_frame(hw, 0, 1, mask, MAC_ADDR_LEN, hw->override_addr);
  3188. }
  3189. /**
  3190. * hw_enable_wol - enable Wake-on-LAN
  3191. * @hw: The hardware instance.
  3192. * @wol_enable: The Wake-on-LAN settings.
  3193. * @net_addr: The IPv4 address assigned to the device.
  3194. *
  3195. * This routine is used to enable Wake-on-LAN depending on driver settings.
  3196. */
  3197. static void hw_enable_wol(struct ksz_hw *hw, u32 wol_enable, const u8 *net_addr)
  3198. {
  3199. hw_cfg_wol(hw, KS8841_WOL_MAGIC_ENABLE, (wol_enable & WAKE_MAGIC));
  3200. hw_cfg_wol(hw, KS8841_WOL_FRAME0_ENABLE, (wol_enable & WAKE_UCAST));
  3201. hw_add_wol_ucast(hw);
  3202. hw_cfg_wol(hw, KS8841_WOL_FRAME1_ENABLE, (wol_enable & WAKE_MCAST));
  3203. hw_add_wol_mcast(hw);
  3204. hw_cfg_wol(hw, KS8841_WOL_FRAME2_ENABLE, (wol_enable & WAKE_BCAST));
  3205. hw_cfg_wol(hw, KS8841_WOL_FRAME3_ENABLE, (wol_enable & WAKE_ARP));
  3206. hw_add_wol_arp(hw, net_addr);
  3207. }
  3208. /**
  3209. * hw_init - check driver is correct for the hardware
  3210. * @hw: The hardware instance.
  3211. *
  3212. * This function checks the hardware is correct for this driver and sets the
  3213. * hardware up for proper initialization.
  3214. *
  3215. * Return number of ports or 0 if not right.
  3216. */
  3217. static int hw_init(struct ksz_hw *hw)
  3218. {
  3219. int rc = 0;
  3220. u16 data;
  3221. u16 revision;
  3222. /* Set bus speed to 125MHz. */
  3223. writew(BUS_SPEED_125_MHZ, hw->io + KS884X_BUS_CTRL_OFFSET);
  3224. /* Check KSZ884x chip ID. */
  3225. data = readw(hw->io + KS884X_CHIP_ID_OFFSET);
  3226. revision = (data & KS884X_REVISION_MASK) >> KS884X_REVISION_SHIFT;
  3227. data &= KS884X_CHIP_ID_MASK_41;
  3228. if (REG_CHIP_ID_41 == data)
  3229. rc = 1;
  3230. else if (REG_CHIP_ID_42 == data)
  3231. rc = 2;
  3232. else
  3233. return 0;
  3234. /* Setup hardware features or bug workarounds. */
  3235. if (revision <= 1) {
  3236. hw->features |= SMALL_PACKET_TX_BUG;
  3237. if (1 == rc)
  3238. hw->features |= HALF_DUPLEX_SIGNAL_BUG;
  3239. }
  3240. return rc;
  3241. }
  3242. /**
  3243. * hw_reset - reset the hardware
  3244. * @hw: The hardware instance.
  3245. *
  3246. * This routine resets the hardware.
  3247. */
  3248. static void hw_reset(struct ksz_hw *hw)
  3249. {
  3250. writew(GLOBAL_SOFTWARE_RESET, hw->io + KS884X_GLOBAL_CTRL_OFFSET);
  3251. /* Wait for device to reset. */
  3252. mdelay(10);
  3253. /* Write 0 to clear device reset. */
  3254. writew(0, hw->io + KS884X_GLOBAL_CTRL_OFFSET);
  3255. }
  3256. /**
  3257. * hw_setup - setup the hardware
  3258. * @hw: The hardware instance.
  3259. *
  3260. * This routine setup the hardware for proper operation.
  3261. */
  3262. static void hw_setup(struct ksz_hw *hw)
  3263. {
  3264. #if SET_DEFAULT_LED
  3265. u16 data;
  3266. /* Change default LED mode. */
  3267. data = readw(hw->io + KS8842_SWITCH_CTRL_5_OFFSET);
  3268. data &= ~LED_MODE;
  3269. data |= SET_DEFAULT_LED;
  3270. writew(data, hw->io + KS8842_SWITCH_CTRL_5_OFFSET);
  3271. #endif
  3272. /* Setup transmit control. */
  3273. hw->tx_cfg = (DMA_TX_PAD_ENABLE | DMA_TX_CRC_ENABLE |
  3274. (DMA_BURST_DEFAULT << DMA_BURST_SHIFT) | DMA_TX_ENABLE);
  3275. /* Setup receive control. */
  3276. hw->rx_cfg = (DMA_RX_BROADCAST | DMA_RX_UNICAST |
  3277. (DMA_BURST_DEFAULT << DMA_BURST_SHIFT) | DMA_RX_ENABLE);
  3278. hw->rx_cfg |= KS884X_DMA_RX_MULTICAST;
  3279. /* Hardware cannot handle UDP packet in IP fragments. */
  3280. hw->rx_cfg |= (DMA_RX_CSUM_TCP | DMA_RX_CSUM_IP);
  3281. if (hw->all_multi)
  3282. hw->rx_cfg |= DMA_RX_ALL_MULTICAST;
  3283. if (hw->promiscuous)
  3284. hw->rx_cfg |= DMA_RX_PROMISCUOUS;
  3285. }
  3286. /**
  3287. * hw_setup_intr - setup interrupt mask
  3288. * @hw: The hardware instance.
  3289. *
  3290. * This routine setup the interrupt mask for proper operation.
  3291. */
  3292. static void hw_setup_intr(struct ksz_hw *hw)
  3293. {
  3294. hw->intr_mask = KS884X_INT_MASK | KS884X_INT_RX_OVERRUN;
  3295. }
  3296. static void ksz_check_desc_num(struct ksz_desc_info *info)
  3297. {
  3298. #define MIN_DESC_SHIFT 2
  3299. int alloc = info->alloc;
  3300. int shift;
  3301. shift = 0;
  3302. while (!(alloc & 1)) {
  3303. shift++;
  3304. alloc >>= 1;
  3305. }
  3306. if (alloc != 1 || shift < MIN_DESC_SHIFT) {
  3307. pr_alert("Hardware descriptor numbers not right!\n");
  3308. while (alloc) {
  3309. shift++;
  3310. alloc >>= 1;
  3311. }
  3312. if (shift < MIN_DESC_SHIFT)
  3313. shift = MIN_DESC_SHIFT;
  3314. alloc = 1 << shift;
  3315. info->alloc = alloc;
  3316. }
  3317. info->mask = info->alloc - 1;
  3318. }
  3319. static void hw_init_desc(struct ksz_desc_info *desc_info, int transmit)
  3320. {
  3321. int i;
  3322. u32 phys = desc_info->ring_phys;
  3323. struct ksz_hw_desc *desc = desc_info->ring_virt;
  3324. struct ksz_desc *cur = desc_info->ring;
  3325. struct ksz_desc *previous = NULL;
  3326. for (i = 0; i < desc_info->alloc; i++) {
  3327. cur->phw = desc++;
  3328. phys += desc_info->size;
  3329. previous = cur++;
  3330. previous->phw->next = cpu_to_le32(phys);
  3331. }
  3332. previous->phw->next = cpu_to_le32(desc_info->ring_phys);
  3333. previous->sw.buf.rx.end_of_ring = 1;
  3334. previous->phw->buf.data = cpu_to_le32(previous->sw.buf.data);
  3335. desc_info->avail = desc_info->alloc;
  3336. desc_info->last = desc_info->next = 0;
  3337. desc_info->cur = desc_info->ring;
  3338. }
  3339. /**
  3340. * hw_set_desc_base - set descriptor base addresses
  3341. * @hw: The hardware instance.
  3342. * @tx_addr: The transmit descriptor base.
  3343. * @rx_addr: The receive descriptor base.
  3344. *
  3345. * This routine programs the descriptor base addresses after reset.
  3346. */
  3347. static void hw_set_desc_base(struct ksz_hw *hw, u32 tx_addr, u32 rx_addr)
  3348. {
  3349. /* Set base address of Tx/Rx descriptors. */
  3350. writel(tx_addr, hw->io + KS_DMA_TX_ADDR);
  3351. writel(rx_addr, hw->io + KS_DMA_RX_ADDR);
  3352. }
  3353. static void hw_reset_pkts(struct ksz_desc_info *info)
  3354. {
  3355. info->cur = info->ring;
  3356. info->avail = info->alloc;
  3357. info->last = info->next = 0;
  3358. }
  3359. static inline void hw_resume_rx(struct ksz_hw *hw)
  3360. {
  3361. writel(DMA_START, hw->io + KS_DMA_RX_START);
  3362. }
  3363. /**
  3364. * hw_start_rx - start receiving
  3365. * @hw: The hardware instance.
  3366. *
  3367. * This routine starts the receive function of the hardware.
  3368. */
  3369. static void hw_start_rx(struct ksz_hw *hw)
  3370. {
  3371. writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
  3372. /* Notify when the receive stops. */
  3373. hw->intr_mask |= KS884X_INT_RX_STOPPED;
  3374. writel(DMA_START, hw->io + KS_DMA_RX_START);
  3375. hw_ack_intr(hw, KS884X_INT_RX_STOPPED);
  3376. hw->rx_stop++;
  3377. /* Variable overflows. */
  3378. if (0 == hw->rx_stop)
  3379. hw->rx_stop = 2;
  3380. }
  3381. /*
  3382. * hw_stop_rx - stop receiving
  3383. * @hw: The hardware instance.
  3384. *
  3385. * This routine stops the receive function of the hardware.
  3386. */
  3387. static void hw_stop_rx(struct ksz_hw *hw)
  3388. {
  3389. hw->rx_stop = 0;
  3390. hw_turn_off_intr(hw, KS884X_INT_RX_STOPPED);
  3391. writel((hw->rx_cfg & ~DMA_RX_ENABLE), hw->io + KS_DMA_RX_CTRL);
  3392. }
  3393. /**
  3394. * hw_start_tx - start transmitting
  3395. * @hw: The hardware instance.
  3396. *
  3397. * This routine starts the transmit function of the hardware.
  3398. */
  3399. static void hw_start_tx(struct ksz_hw *hw)
  3400. {
  3401. writel(hw->tx_cfg, hw->io + KS_DMA_TX_CTRL);
  3402. }
  3403. /**
  3404. * hw_stop_tx - stop transmitting
  3405. * @hw: The hardware instance.
  3406. *
  3407. * This routine stops the transmit function of the hardware.
  3408. */
  3409. static void hw_stop_tx(struct ksz_hw *hw)
  3410. {
  3411. writel((hw->tx_cfg & ~DMA_TX_ENABLE), hw->io + KS_DMA_TX_CTRL);
  3412. }
  3413. /**
  3414. * hw_disable - disable hardware
  3415. * @hw: The hardware instance.
  3416. *
  3417. * This routine disables the hardware.
  3418. */
  3419. static void hw_disable(struct ksz_hw *hw)
  3420. {
  3421. hw_stop_rx(hw);
  3422. hw_stop_tx(hw);
  3423. hw->enabled = 0;
  3424. }
  3425. /**
  3426. * hw_enable - enable hardware
  3427. * @hw: The hardware instance.
  3428. *
  3429. * This routine enables the hardware.
  3430. */
  3431. static void hw_enable(struct ksz_hw *hw)
  3432. {
  3433. hw_start_tx(hw);
  3434. hw_start_rx(hw);
  3435. hw->enabled = 1;
  3436. }
  3437. /**
  3438. * hw_alloc_pkt - allocate enough descriptors for transmission
  3439. * @hw: The hardware instance.
  3440. * @length: The length of the packet.
  3441. * @physical: Number of descriptors required.
  3442. *
  3443. * This function allocates descriptors for transmission.
  3444. *
  3445. * Return 0 if not successful; 1 for buffer copy; or number of descriptors.
  3446. */
  3447. static int hw_alloc_pkt(struct ksz_hw *hw, int length, int physical)
  3448. {
  3449. /* Always leave one descriptor free. */
  3450. if (hw->tx_desc_info.avail <= 1)
  3451. return 0;
  3452. /* Allocate a descriptor for transmission and mark it current. */
  3453. get_tx_pkt(&hw->tx_desc_info, &hw->tx_desc_info.cur);
  3454. hw->tx_desc_info.cur->sw.buf.tx.first_seg = 1;
  3455. /* Keep track of number of transmit descriptors used so far. */
  3456. ++hw->tx_int_cnt;
  3457. hw->tx_size += length;
  3458. /* Cannot hold on too much data. */
  3459. if (hw->tx_size >= MAX_TX_HELD_SIZE)
  3460. hw->tx_int_cnt = hw->tx_int_mask + 1;
  3461. if (physical > hw->tx_desc_info.avail)
  3462. return 1;
  3463. return hw->tx_desc_info.avail;
  3464. }
  3465. /**
  3466. * hw_send_pkt - mark packet for transmission
  3467. * @hw: The hardware instance.
  3468. *
  3469. * This routine marks the packet for transmission in PCI version.
  3470. */
  3471. static void hw_send_pkt(struct ksz_hw *hw)
  3472. {
  3473. struct ksz_desc *cur = hw->tx_desc_info.cur;
  3474. cur->sw.buf.tx.last_seg = 1;
  3475. /* Interrupt only after specified number of descriptors used. */
  3476. if (hw->tx_int_cnt > hw->tx_int_mask) {
  3477. cur->sw.buf.tx.intr = 1;
  3478. hw->tx_int_cnt = 0;
  3479. hw->tx_size = 0;
  3480. }
  3481. /* KSZ8842 supports port directed transmission. */
  3482. cur->sw.buf.tx.dest_port = hw->dst_ports;
  3483. release_desc(cur);
  3484. writel(0, hw->io + KS_DMA_TX_START);
  3485. }
  3486. static int empty_addr(u8 *addr)
  3487. {
  3488. u32 *addr1 = (u32 *) addr;
  3489. u16 *addr2 = (u16 *) &addr[4];
  3490. return 0 == *addr1 && 0 == *addr2;
  3491. }
  3492. /**
  3493. * hw_set_addr - set MAC address
  3494. * @hw: The hardware instance.
  3495. *
  3496. * This routine programs the MAC address of the hardware when the address is
  3497. * overrided.
  3498. */
  3499. static void hw_set_addr(struct ksz_hw *hw)
  3500. {
  3501. int i;
  3502. for (i = 0; i < MAC_ADDR_LEN; i++)
  3503. writeb(hw->override_addr[MAC_ADDR_ORDER(i)],
  3504. hw->io + KS884X_ADDR_0_OFFSET + i);
  3505. sw_set_addr(hw, hw->override_addr);
  3506. }
  3507. /**
  3508. * hw_read_addr - read MAC address
  3509. * @hw: The hardware instance.
  3510. *
  3511. * This routine retrieves the MAC address of the hardware.
  3512. */
  3513. static void hw_read_addr(struct ksz_hw *hw)
  3514. {
  3515. int i;
  3516. for (i = 0; i < MAC_ADDR_LEN; i++)
  3517. hw->perm_addr[MAC_ADDR_ORDER(i)] = readb(hw->io +
  3518. KS884X_ADDR_0_OFFSET + i);
  3519. if (!hw->mac_override) {
  3520. memcpy(hw->override_addr, hw->perm_addr, MAC_ADDR_LEN);
  3521. if (empty_addr(hw->override_addr)) {
  3522. memcpy(hw->perm_addr, DEFAULT_MAC_ADDRESS,
  3523. MAC_ADDR_LEN);
  3524. memcpy(hw->override_addr, DEFAULT_MAC_ADDRESS,
  3525. MAC_ADDR_LEN);
  3526. hw->override_addr[5] += hw->id;
  3527. hw_set_addr(hw);
  3528. }
  3529. }
  3530. }
  3531. static void hw_ena_add_addr(struct ksz_hw *hw, int index, u8 *mac_addr)
  3532. {
  3533. int i;
  3534. u32 mac_addr_lo;
  3535. u32 mac_addr_hi;
  3536. mac_addr_hi = 0;
  3537. for (i = 0; i < 2; i++) {
  3538. mac_addr_hi <<= 8;
  3539. mac_addr_hi |= mac_addr[i];
  3540. }
  3541. mac_addr_hi |= ADD_ADDR_ENABLE;
  3542. mac_addr_lo = 0;
  3543. for (i = 2; i < 6; i++) {
  3544. mac_addr_lo <<= 8;
  3545. mac_addr_lo |= mac_addr[i];
  3546. }
  3547. index *= ADD_ADDR_INCR;
  3548. writel(mac_addr_lo, hw->io + index + KS_ADD_ADDR_0_LO);
  3549. writel(mac_addr_hi, hw->io + index + KS_ADD_ADDR_0_HI);
  3550. }
  3551. static void hw_set_add_addr(struct ksz_hw *hw)
  3552. {
  3553. int i;
  3554. for (i = 0; i < ADDITIONAL_ENTRIES; i++) {
  3555. if (empty_addr(hw->address[i]))
  3556. writel(0, hw->io + ADD_ADDR_INCR * i +
  3557. KS_ADD_ADDR_0_HI);
  3558. else
  3559. hw_ena_add_addr(hw, i, hw->address[i]);
  3560. }
  3561. }
  3562. static int hw_add_addr(struct ksz_hw *hw, u8 *mac_addr)
  3563. {
  3564. int i;
  3565. int j = ADDITIONAL_ENTRIES;
  3566. if (!memcmp(hw->override_addr, mac_addr, MAC_ADDR_LEN))
  3567. return 0;
  3568. for (i = 0; i < hw->addr_list_size; i++) {
  3569. if (!memcmp(hw->address[i], mac_addr, MAC_ADDR_LEN))
  3570. return 0;
  3571. if (ADDITIONAL_ENTRIES == j && empty_addr(hw->address[i]))
  3572. j = i;
  3573. }
  3574. if (j < ADDITIONAL_ENTRIES) {
  3575. memcpy(hw->address[j], mac_addr, MAC_ADDR_LEN);
  3576. hw_ena_add_addr(hw, j, hw->address[j]);
  3577. return 0;
  3578. }
  3579. return -1;
  3580. }
  3581. static int hw_del_addr(struct ksz_hw *hw, u8 *mac_addr)
  3582. {
  3583. int i;
  3584. for (i = 0; i < hw->addr_list_size; i++) {
  3585. if (!memcmp(hw->address[i], mac_addr, MAC_ADDR_LEN)) {
  3586. memset(hw->address[i], 0, MAC_ADDR_LEN);
  3587. writel(0, hw->io + ADD_ADDR_INCR * i +
  3588. KS_ADD_ADDR_0_HI);
  3589. return 0;
  3590. }
  3591. }
  3592. return -1;
  3593. }
  3594. /**
  3595. * hw_clr_multicast - clear multicast addresses
  3596. * @hw: The hardware instance.
  3597. *
  3598. * This routine removes all multicast addresses set in the hardware.
  3599. */
  3600. static void hw_clr_multicast(struct ksz_hw *hw)
  3601. {
  3602. int i;
  3603. for (i = 0; i < HW_MULTICAST_SIZE; i++) {
  3604. hw->multi_bits[i] = 0;
  3605. writeb(0, hw->io + KS884X_MULTICAST_0_OFFSET + i);
  3606. }
  3607. }
  3608. /**
  3609. * hw_set_grp_addr - set multicast addresses
  3610. * @hw: The hardware instance.
  3611. *
  3612. * This routine programs multicast addresses for the hardware to accept those
  3613. * addresses.
  3614. */
  3615. static void hw_set_grp_addr(struct ksz_hw *hw)
  3616. {
  3617. int i;
  3618. int index;
  3619. int position;
  3620. int value;
  3621. memset(hw->multi_bits, 0, sizeof(u8) * HW_MULTICAST_SIZE);
  3622. for (i = 0; i < hw->multi_list_size; i++) {
  3623. position = (ether_crc(6, hw->multi_list[i]) >> 26) & 0x3f;
  3624. index = position >> 3;
  3625. value = 1 << (position & 7);
  3626. hw->multi_bits[index] |= (u8) value;
  3627. }
  3628. for (i = 0; i < HW_MULTICAST_SIZE; i++)
  3629. writeb(hw->multi_bits[i], hw->io + KS884X_MULTICAST_0_OFFSET +
  3630. i);
  3631. }
  3632. /**
  3633. * hw_set_multicast - enable or disable all multicast receiving
  3634. * @hw: The hardware instance.
  3635. * @multicast: To turn on or off the all multicast feature.
  3636. *
  3637. * This routine enables/disables the hardware to accept all multicast packets.
  3638. */
  3639. static void hw_set_multicast(struct ksz_hw *hw, u8 multicast)
  3640. {
  3641. /* Stop receiving for reconfiguration. */
  3642. hw_stop_rx(hw);
  3643. if (multicast)
  3644. hw->rx_cfg |= DMA_RX_ALL_MULTICAST;
  3645. else
  3646. hw->rx_cfg &= ~DMA_RX_ALL_MULTICAST;
  3647. if (hw->enabled)
  3648. hw_start_rx(hw);
  3649. }
  3650. /**
  3651. * hw_set_promiscuous - enable or disable promiscuous receiving
  3652. * @hw: The hardware instance.
  3653. * @prom: To turn on or off the promiscuous feature.
  3654. *
  3655. * This routine enables/disables the hardware to accept all packets.
  3656. */
  3657. static void hw_set_promiscuous(struct ksz_hw *hw, u8 prom)
  3658. {
  3659. /* Stop receiving for reconfiguration. */
  3660. hw_stop_rx(hw);
  3661. if (prom)
  3662. hw->rx_cfg |= DMA_RX_PROMISCUOUS;
  3663. else
  3664. hw->rx_cfg &= ~DMA_RX_PROMISCUOUS;
  3665. if (hw->enabled)
  3666. hw_start_rx(hw);
  3667. }
  3668. /**
  3669. * sw_enable - enable the switch
  3670. * @hw: The hardware instance.
  3671. * @enable: The flag to enable or disable the switch
  3672. *
  3673. * This routine is used to enable/disable the switch in KSZ8842.
  3674. */
  3675. static void sw_enable(struct ksz_hw *hw, int enable)
  3676. {
  3677. int port;
  3678. for (port = 0; port < SWITCH_PORT_NUM; port++) {
  3679. if (hw->dev_count > 1) {
  3680. /* Set port-base vlan membership with host port. */
  3681. sw_cfg_port_base_vlan(hw, port,
  3682. HOST_MASK | (1 << port));
  3683. port_set_stp_state(hw, port, STP_STATE_DISABLED);
  3684. } else {
  3685. sw_cfg_port_base_vlan(hw, port, PORT_MASK);
  3686. port_set_stp_state(hw, port, STP_STATE_FORWARDING);
  3687. }
  3688. }
  3689. if (hw->dev_count > 1)
  3690. port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_SIMPLE);
  3691. else
  3692. port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_FORWARDING);
  3693. if (enable)
  3694. enable = KS8842_START;
  3695. writew(enable, hw->io + KS884X_CHIP_ID_OFFSET);
  3696. }
  3697. /**
  3698. * sw_setup - setup the switch
  3699. * @hw: The hardware instance.
  3700. *
  3701. * This routine setup the hardware switch engine for default operation.
  3702. */
  3703. static void sw_setup(struct ksz_hw *hw)
  3704. {
  3705. int port;
  3706. sw_set_global_ctrl(hw);
  3707. /* Enable switch broadcast storm protection at 10% percent rate. */
  3708. sw_init_broad_storm(hw);
  3709. hw_cfg_broad_storm(hw, BROADCAST_STORM_PROTECTION_RATE);
  3710. for (port = 0; port < SWITCH_PORT_NUM; port++)
  3711. sw_ena_broad_storm(hw, port);
  3712. sw_init_prio(hw);
  3713. sw_init_mirror(hw);
  3714. sw_init_prio_rate(hw);
  3715. sw_init_vlan(hw);
  3716. if (hw->features & STP_SUPPORT)
  3717. sw_init_stp(hw);
  3718. if (!sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
  3719. SWITCH_TX_FLOW_CTRL | SWITCH_RX_FLOW_CTRL))
  3720. hw->overrides |= PAUSE_FLOW_CTRL;
  3721. sw_enable(hw, 1);
  3722. }
  3723. /**
  3724. * ksz_start_timer - start kernel timer
  3725. * @info: Kernel timer information.
  3726. * @time: The time tick.
  3727. *
  3728. * This routine starts the kernel timer after the specified time tick.
  3729. */
  3730. static void ksz_start_timer(struct ksz_timer_info *info, int time)
  3731. {
  3732. info->cnt = 0;
  3733. info->timer.expires = jiffies + time;
  3734. add_timer(&info->timer);
  3735. /* infinity */
  3736. info->max = -1;
  3737. }
  3738. /**
  3739. * ksz_stop_timer - stop kernel timer
  3740. * @info: Kernel timer information.
  3741. *
  3742. * This routine stops the kernel timer.
  3743. */
  3744. static void ksz_stop_timer(struct ksz_timer_info *info)
  3745. {
  3746. if (info->max) {
  3747. info->max = 0;
  3748. del_timer_sync(&info->timer);
  3749. }
  3750. }
  3751. static void ksz_init_timer(struct ksz_timer_info *info, int period,
  3752. void (*function)(unsigned long), void *data)
  3753. {
  3754. info->max = 0;
  3755. info->period = period;
  3756. init_timer(&info->timer);
  3757. info->timer.function = function;
  3758. info->timer.data = (unsigned long) data;
  3759. }
  3760. static void ksz_update_timer(struct ksz_timer_info *info)
  3761. {
  3762. ++info->cnt;
  3763. if (info->max > 0) {
  3764. if (info->cnt < info->max) {
  3765. info->timer.expires = jiffies + info->period;
  3766. add_timer(&info->timer);
  3767. } else
  3768. info->max = 0;
  3769. } else if (info->max < 0) {
  3770. info->timer.expires = jiffies + info->period;
  3771. add_timer(&info->timer);
  3772. }
  3773. }
  3774. /**
  3775. * ksz_alloc_soft_desc - allocate software descriptors
  3776. * @desc_info: Descriptor information structure.
  3777. * @transmit: Indication that descriptors are for transmit.
  3778. *
  3779. * This local function allocates software descriptors for manipulation in
  3780. * memory.
  3781. *
  3782. * Return 0 if successful.
  3783. */
  3784. static int ksz_alloc_soft_desc(struct ksz_desc_info *desc_info, int transmit)
  3785. {
  3786. desc_info->ring = kmalloc(sizeof(struct ksz_desc) * desc_info->alloc,
  3787. GFP_KERNEL);
  3788. if (!desc_info->ring)
  3789. return 1;
  3790. memset((void *) desc_info->ring, 0,
  3791. sizeof(struct ksz_desc) * desc_info->alloc);
  3792. hw_init_desc(desc_info, transmit);
  3793. return 0;
  3794. }
  3795. /**
  3796. * ksz_alloc_desc - allocate hardware descriptors
  3797. * @adapter: Adapter information structure.
  3798. *
  3799. * This local function allocates hardware descriptors for receiving and
  3800. * transmitting.
  3801. *
  3802. * Return 0 if successful.
  3803. */
  3804. static int ksz_alloc_desc(struct dev_info *adapter)
  3805. {
  3806. struct ksz_hw *hw = &adapter->hw;
  3807. int offset;
  3808. /* Allocate memory for RX & TX descriptors. */
  3809. adapter->desc_pool.alloc_size =
  3810. hw->rx_desc_info.size * hw->rx_desc_info.alloc +
  3811. hw->tx_desc_info.size * hw->tx_desc_info.alloc +
  3812. DESC_ALIGNMENT;
  3813. adapter->desc_pool.alloc_virt =
  3814. pci_alloc_consistent(
  3815. adapter->pdev, adapter->desc_pool.alloc_size,
  3816. &adapter->desc_pool.dma_addr);
  3817. if (adapter->desc_pool.alloc_virt == NULL) {
  3818. adapter->desc_pool.alloc_size = 0;
  3819. return 1;
  3820. }
  3821. memset(adapter->desc_pool.alloc_virt, 0, adapter->desc_pool.alloc_size);
  3822. /* Align to the next cache line boundary. */
  3823. offset = (((ulong) adapter->desc_pool.alloc_virt % DESC_ALIGNMENT) ?
  3824. (DESC_ALIGNMENT -
  3825. ((ulong) adapter->desc_pool.alloc_virt % DESC_ALIGNMENT)) : 0);
  3826. adapter->desc_pool.virt = adapter->desc_pool.alloc_virt + offset;
  3827. adapter->desc_pool.phys = adapter->desc_pool.dma_addr + offset;
  3828. /* Allocate receive/transmit descriptors. */
  3829. hw->rx_desc_info.ring_virt = (struct ksz_hw_desc *)
  3830. adapter->desc_pool.virt;
  3831. hw->rx_desc_info.ring_phys = adapter->desc_pool.phys;
  3832. offset = hw->rx_desc_info.alloc * hw->rx_desc_info.size;
  3833. hw->tx_desc_info.ring_virt = (struct ksz_hw_desc *)
  3834. (adapter->desc_pool.virt + offset);
  3835. hw->tx_desc_info.ring_phys = adapter->desc_pool.phys + offset;
  3836. if (ksz_alloc_soft_desc(&hw->rx_desc_info, 0))
  3837. return 1;
  3838. if (ksz_alloc_soft_desc(&hw->tx_desc_info, 1))
  3839. return 1;
  3840. return 0;
  3841. }
  3842. /**
  3843. * free_dma_buf - release DMA buffer resources
  3844. * @adapter: Adapter information structure.
  3845. *
  3846. * This routine is just a helper function to release the DMA buffer resources.
  3847. */
  3848. static void free_dma_buf(struct dev_info *adapter, struct ksz_dma_buf *dma_buf,
  3849. int direction)
  3850. {
  3851. pci_unmap_single(adapter->pdev, dma_buf->dma, dma_buf->len, direction);
  3852. dev_kfree_skb(dma_buf->skb);
  3853. dma_buf->skb = NULL;
  3854. dma_buf->dma = 0;
  3855. }
  3856. /**
  3857. * ksz_init_rx_buffers - initialize receive descriptors
  3858. * @adapter: Adapter information structure.
  3859. *
  3860. * This routine initializes DMA buffers for receiving.
  3861. */
  3862. static void ksz_init_rx_buffers(struct dev_info *adapter)
  3863. {
  3864. int i;
  3865. struct ksz_desc *desc;
  3866. struct ksz_dma_buf *dma_buf;
  3867. struct ksz_hw *hw = &adapter->hw;
  3868. struct ksz_desc_info *info = &hw->rx_desc_info;
  3869. for (i = 0; i < hw->rx_desc_info.alloc; i++) {
  3870. get_rx_pkt(info, &desc);
  3871. dma_buf = DMA_BUFFER(desc);
  3872. if (dma_buf->skb && dma_buf->len != adapter->mtu)
  3873. free_dma_buf(adapter, dma_buf, PCI_DMA_FROMDEVICE);
  3874. dma_buf->len = adapter->mtu;
  3875. if (!dma_buf->skb)
  3876. dma_buf->skb = alloc_skb(dma_buf->len, GFP_ATOMIC);
  3877. if (dma_buf->skb && !dma_buf->dma) {
  3878. dma_buf->skb->dev = adapter->dev;
  3879. dma_buf->dma = pci_map_single(
  3880. adapter->pdev,
  3881. skb_tail_pointer(dma_buf->skb),
  3882. dma_buf->len,
  3883. PCI_DMA_FROMDEVICE);
  3884. }
  3885. /* Set descriptor. */
  3886. set_rx_buf(desc, dma_buf->dma);
  3887. set_rx_len(desc, dma_buf->len);
  3888. release_desc(desc);
  3889. }
  3890. }
  3891. /**
  3892. * ksz_alloc_mem - allocate memory for hardware descriptors
  3893. * @adapter: Adapter information structure.
  3894. *
  3895. * This function allocates memory for use by hardware descriptors for receiving
  3896. * and transmitting.
  3897. *
  3898. * Return 0 if successful.
  3899. */
  3900. static int ksz_alloc_mem(struct dev_info *adapter)
  3901. {
  3902. struct ksz_hw *hw = &adapter->hw;
  3903. /* Determine the number of receive and transmit descriptors. */
  3904. hw->rx_desc_info.alloc = NUM_OF_RX_DESC;
  3905. hw->tx_desc_info.alloc = NUM_OF_TX_DESC;
  3906. /* Determine how many descriptors to skip transmit interrupt. */
  3907. hw->tx_int_cnt = 0;
  3908. hw->tx_int_mask = NUM_OF_TX_DESC / 4;
  3909. if (hw->tx_int_mask > 8)
  3910. hw->tx_int_mask = 8;
  3911. while (hw->tx_int_mask) {
  3912. hw->tx_int_cnt++;
  3913. hw->tx_int_mask >>= 1;
  3914. }
  3915. if (hw->tx_int_cnt) {
  3916. hw->tx_int_mask = (1 << (hw->tx_int_cnt - 1)) - 1;
  3917. hw->tx_int_cnt = 0;
  3918. }
  3919. /* Determine the descriptor size. */
  3920. hw->rx_desc_info.size =
  3921. (((sizeof(struct ksz_hw_desc) + DESC_ALIGNMENT - 1) /
  3922. DESC_ALIGNMENT) * DESC_ALIGNMENT);
  3923. hw->tx_desc_info.size =
  3924. (((sizeof(struct ksz_hw_desc) + DESC_ALIGNMENT - 1) /
  3925. DESC_ALIGNMENT) * DESC_ALIGNMENT);
  3926. if (hw->rx_desc_info.size != sizeof(struct ksz_hw_desc))
  3927. pr_alert("Hardware descriptor size not right!\n");
  3928. ksz_check_desc_num(&hw->rx_desc_info);
  3929. ksz_check_desc_num(&hw->tx_desc_info);
  3930. /* Allocate descriptors. */
  3931. if (ksz_alloc_desc(adapter))
  3932. return 1;
  3933. return 0;
  3934. }
  3935. /**
  3936. * ksz_free_desc - free software and hardware descriptors
  3937. * @adapter: Adapter information structure.
  3938. *
  3939. * This local routine frees the software and hardware descriptors allocated by
  3940. * ksz_alloc_desc().
  3941. */
  3942. static void ksz_free_desc(struct dev_info *adapter)
  3943. {
  3944. struct ksz_hw *hw = &adapter->hw;
  3945. /* Reset descriptor. */
  3946. hw->rx_desc_info.ring_virt = NULL;
  3947. hw->tx_desc_info.ring_virt = NULL;
  3948. hw->rx_desc_info.ring_phys = 0;
  3949. hw->tx_desc_info.ring_phys = 0;
  3950. /* Free memory. */
  3951. if (adapter->desc_pool.alloc_virt)
  3952. pci_free_consistent(
  3953. adapter->pdev,
  3954. adapter->desc_pool.alloc_size,
  3955. adapter->desc_pool.alloc_virt,
  3956. adapter->desc_pool.dma_addr);
  3957. /* Reset resource pool. */
  3958. adapter->desc_pool.alloc_size = 0;
  3959. adapter->desc_pool.alloc_virt = NULL;
  3960. kfree(hw->rx_desc_info.ring);
  3961. hw->rx_desc_info.ring = NULL;
  3962. kfree(hw->tx_desc_info.ring);
  3963. hw->tx_desc_info.ring = NULL;
  3964. }
  3965. /**
  3966. * ksz_free_buffers - free buffers used in the descriptors
  3967. * @adapter: Adapter information structure.
  3968. * @desc_info: Descriptor information structure.
  3969. *
  3970. * This local routine frees buffers used in the DMA buffers.
  3971. */
  3972. static void ksz_free_buffers(struct dev_info *adapter,
  3973. struct ksz_desc_info *desc_info, int direction)
  3974. {
  3975. int i;
  3976. struct ksz_dma_buf *dma_buf;
  3977. struct ksz_desc *desc = desc_info->ring;
  3978. for (i = 0; i < desc_info->alloc; i++) {
  3979. dma_buf = DMA_BUFFER(desc);
  3980. if (dma_buf->skb)
  3981. free_dma_buf(adapter, dma_buf, direction);
  3982. desc++;
  3983. }
  3984. }
  3985. /**
  3986. * ksz_free_mem - free all resources used by descriptors
  3987. * @adapter: Adapter information structure.
  3988. *
  3989. * This local routine frees all the resources allocated by ksz_alloc_mem().
  3990. */
  3991. static void ksz_free_mem(struct dev_info *adapter)
  3992. {
  3993. /* Free transmit buffers. */
  3994. ksz_free_buffers(adapter, &adapter->hw.tx_desc_info,
  3995. PCI_DMA_TODEVICE);
  3996. /* Free receive buffers. */
  3997. ksz_free_buffers(adapter, &adapter->hw.rx_desc_info,
  3998. PCI_DMA_FROMDEVICE);
  3999. /* Free descriptors. */
  4000. ksz_free_desc(adapter);
  4001. }
  4002. static void get_mib_counters(struct ksz_hw *hw, int first, int cnt,
  4003. u64 *counter)
  4004. {
  4005. int i;
  4006. int mib;
  4007. int port;
  4008. struct ksz_port_mib *port_mib;
  4009. memset(counter, 0, sizeof(u64) * TOTAL_PORT_COUNTER_NUM);
  4010. for (i = 0, port = first; i < cnt; i++, port++) {
  4011. port_mib = &hw->port_mib[port];
  4012. for (mib = port_mib->mib_start; mib < hw->mib_cnt; mib++)
  4013. counter[mib] += port_mib->counter[mib];
  4014. }
  4015. }
  4016. /**
  4017. * send_packet - send packet
  4018. * @skb: Socket buffer.
  4019. * @dev: Network device.
  4020. *
  4021. * This routine is used to send a packet out to the network.
  4022. */
  4023. static void send_packet(struct sk_buff *skb, struct net_device *dev)
  4024. {
  4025. struct ksz_desc *desc;
  4026. struct ksz_desc *first;
  4027. struct dev_priv *priv = netdev_priv(dev);
  4028. struct dev_info *hw_priv = priv->adapter;
  4029. struct ksz_hw *hw = &hw_priv->hw;
  4030. struct ksz_desc_info *info = &hw->tx_desc_info;
  4031. struct ksz_dma_buf *dma_buf;
  4032. int len;
  4033. int last_frag = skb_shinfo(skb)->nr_frags;
  4034. /*
  4035. * KSZ8842 with multiple device interfaces needs to be told which port
  4036. * to send.
  4037. */
  4038. if (hw->dev_count > 1)
  4039. hw->dst_ports = 1 << priv->port.first_port;
  4040. /* Hardware will pad the length to 60. */
  4041. len = skb->len;
  4042. /* Remember the very first descriptor. */
  4043. first = info->cur;
  4044. desc = first;
  4045. dma_buf = DMA_BUFFER(desc);
  4046. if (last_frag) {
  4047. int frag;
  4048. skb_frag_t *this_frag;
  4049. dma_buf->len = skb_headlen(skb);
  4050. dma_buf->dma = pci_map_single(
  4051. hw_priv->pdev, skb->data, dma_buf->len,
  4052. PCI_DMA_TODEVICE);
  4053. set_tx_buf(desc, dma_buf->dma);
  4054. set_tx_len(desc, dma_buf->len);
  4055. frag = 0;
  4056. do {
  4057. this_frag = &skb_shinfo(skb)->frags[frag];
  4058. /* Get a new descriptor. */
  4059. get_tx_pkt(info, &desc);
  4060. /* Keep track of descriptors used so far. */
  4061. ++hw->tx_int_cnt;
  4062. dma_buf = DMA_BUFFER(desc);
  4063. dma_buf->len = this_frag->size;
  4064. dma_buf->dma = pci_map_single(
  4065. hw_priv->pdev,
  4066. page_address(this_frag->page) +
  4067. this_frag->page_offset,
  4068. dma_buf->len,
  4069. PCI_DMA_TODEVICE);
  4070. set_tx_buf(desc, dma_buf->dma);
  4071. set_tx_len(desc, dma_buf->len);
  4072. frag++;
  4073. if (frag == last_frag)
  4074. break;
  4075. /* Do not release the last descriptor here. */
  4076. release_desc(desc);
  4077. } while (1);
  4078. /* current points to the last descriptor. */
  4079. info->cur = desc;
  4080. /* Release the first descriptor. */
  4081. release_desc(first);
  4082. } else {
  4083. dma_buf->len = len;
  4084. dma_buf->dma = pci_map_single(
  4085. hw_priv->pdev, skb->data, dma_buf->len,
  4086. PCI_DMA_TODEVICE);
  4087. set_tx_buf(desc, dma_buf->dma);
  4088. set_tx_len(desc, dma_buf->len);
  4089. }
  4090. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  4091. (desc)->sw.buf.tx.csum_gen_tcp = 1;
  4092. (desc)->sw.buf.tx.csum_gen_udp = 1;
  4093. }
  4094. /*
  4095. * The last descriptor holds the packet so that it can be returned to
  4096. * network subsystem after all descriptors are transmitted.
  4097. */
  4098. dma_buf->skb = skb;
  4099. hw_send_pkt(hw);
  4100. /* Update transmit statistics. */
  4101. dev->stats.tx_packets++;
  4102. dev->stats.tx_bytes += len;
  4103. }
  4104. /**
  4105. * transmit_cleanup - clean up transmit descriptors
  4106. * @dev: Network device.
  4107. *
  4108. * This routine is called to clean up the transmitted buffers.
  4109. */
  4110. static void transmit_cleanup(struct dev_info *hw_priv, int normal)
  4111. {
  4112. int last;
  4113. union desc_stat status;
  4114. struct ksz_hw *hw = &hw_priv->hw;
  4115. struct ksz_desc_info *info = &hw->tx_desc_info;
  4116. struct ksz_desc *desc;
  4117. struct ksz_dma_buf *dma_buf;
  4118. struct net_device *dev = NULL;
  4119. spin_lock(&hw_priv->hwlock);
  4120. last = info->last;
  4121. while (info->avail < info->alloc) {
  4122. /* Get next descriptor which is not hardware owned. */
  4123. desc = &info->ring[last];
  4124. status.data = le32_to_cpu(desc->phw->ctrl.data);
  4125. if (status.tx.hw_owned) {
  4126. if (normal)
  4127. break;
  4128. else
  4129. reset_desc(desc, status);
  4130. }
  4131. dma_buf = DMA_BUFFER(desc);
  4132. pci_unmap_single(
  4133. hw_priv->pdev, dma_buf->dma, dma_buf->len,
  4134. PCI_DMA_TODEVICE);
  4135. /* This descriptor contains the last buffer in the packet. */
  4136. if (dma_buf->skb) {
  4137. dev = dma_buf->skb->dev;
  4138. /* Release the packet back to network subsystem. */
  4139. dev_kfree_skb_irq(dma_buf->skb);
  4140. dma_buf->skb = NULL;
  4141. }
  4142. /* Free the transmitted descriptor. */
  4143. last++;
  4144. last &= info->mask;
  4145. info->avail++;
  4146. }
  4147. info->last = last;
  4148. spin_unlock(&hw_priv->hwlock);
  4149. /* Notify the network subsystem that the packet has been sent. */
  4150. if (dev)
  4151. dev->trans_start = jiffies;
  4152. }
  4153. /**
  4154. * transmit_done - transmit done processing
  4155. * @dev: Network device.
  4156. *
  4157. * This routine is called when the transmit interrupt is triggered, indicating
  4158. * either a packet is sent successfully or there are transmit errors.
  4159. */
  4160. static void tx_done(struct dev_info *hw_priv)
  4161. {
  4162. struct ksz_hw *hw = &hw_priv->hw;
  4163. int port;
  4164. transmit_cleanup(hw_priv, 1);
  4165. for (port = 0; port < hw->dev_count; port++) {
  4166. struct net_device *dev = hw->port_info[port].pdev;
  4167. if (netif_running(dev) && netif_queue_stopped(dev))
  4168. netif_wake_queue(dev);
  4169. }
  4170. }
  4171. static inline void copy_old_skb(struct sk_buff *old, struct sk_buff *skb)
  4172. {
  4173. skb->dev = old->dev;
  4174. skb->protocol = old->protocol;
  4175. skb->ip_summed = old->ip_summed;
  4176. skb->csum = old->csum;
  4177. skb_set_network_header(skb, ETH_HLEN);
  4178. dev_kfree_skb(old);
  4179. }
  4180. /**
  4181. * netdev_tx - send out packet
  4182. * @skb: Socket buffer.
  4183. * @dev: Network device.
  4184. *
  4185. * This function is used by the upper network layer to send out a packet.
  4186. *
  4187. * Return 0 if successful; otherwise an error code indicating failure.
  4188. */
  4189. static netdev_tx_t netdev_tx(struct sk_buff *skb, struct net_device *dev)
  4190. {
  4191. struct dev_priv *priv = netdev_priv(dev);
  4192. struct dev_info *hw_priv = priv->adapter;
  4193. struct ksz_hw *hw = &hw_priv->hw;
  4194. int left;
  4195. int num = 1;
  4196. int rc = 0;
  4197. if (hw->features & SMALL_PACKET_TX_BUG) {
  4198. struct sk_buff *org_skb = skb;
  4199. if (skb->len <= 48) {
  4200. if (skb_end_pointer(skb) - skb->data >= 50) {
  4201. memset(&skb->data[skb->len], 0, 50 - skb->len);
  4202. skb->len = 50;
  4203. } else {
  4204. skb = dev_alloc_skb(50);
  4205. if (!skb)
  4206. return NETDEV_TX_BUSY;
  4207. memcpy(skb->data, org_skb->data, org_skb->len);
  4208. memset(&skb->data[org_skb->len], 0,
  4209. 50 - org_skb->len);
  4210. skb->len = 50;
  4211. copy_old_skb(org_skb, skb);
  4212. }
  4213. }
  4214. }
  4215. spin_lock_irq(&hw_priv->hwlock);
  4216. num = skb_shinfo(skb)->nr_frags + 1;
  4217. left = hw_alloc_pkt(hw, skb->len, num);
  4218. if (left) {
  4219. if (left < num ||
  4220. ((CHECKSUM_PARTIAL == skb->ip_summed) &&
  4221. (ETH_P_IPV6 == htons(skb->protocol)))) {
  4222. struct sk_buff *org_skb = skb;
  4223. skb = dev_alloc_skb(org_skb->len);
  4224. if (!skb) {
  4225. rc = NETDEV_TX_BUSY;
  4226. goto unlock;
  4227. }
  4228. skb_copy_and_csum_dev(org_skb, skb->data);
  4229. org_skb->ip_summed = CHECKSUM_NONE;
  4230. skb->len = org_skb->len;
  4231. copy_old_skb(org_skb, skb);
  4232. }
  4233. send_packet(skb, dev);
  4234. if (left <= num)
  4235. netif_stop_queue(dev);
  4236. } else {
  4237. /* Stop the transmit queue until packet is allocated. */
  4238. netif_stop_queue(dev);
  4239. rc = NETDEV_TX_BUSY;
  4240. }
  4241. unlock:
  4242. spin_unlock_irq(&hw_priv->hwlock);
  4243. return rc;
  4244. }
  4245. /**
  4246. * netdev_tx_timeout - transmit timeout processing
  4247. * @dev: Network device.
  4248. *
  4249. * This routine is called when the transmit timer expires. That indicates the
  4250. * hardware is not running correctly because transmit interrupts are not
  4251. * triggered to free up resources so that the transmit routine can continue
  4252. * sending out packets. The hardware is reset to correct the problem.
  4253. */
  4254. static void netdev_tx_timeout(struct net_device *dev)
  4255. {
  4256. static unsigned long last_reset;
  4257. struct dev_priv *priv = netdev_priv(dev);
  4258. struct dev_info *hw_priv = priv->adapter;
  4259. struct ksz_hw *hw = &hw_priv->hw;
  4260. int port;
  4261. if (hw->dev_count > 1) {
  4262. /*
  4263. * Only reset the hardware if time between calls is long
  4264. * enough.
  4265. */
  4266. if (jiffies - last_reset <= dev->watchdog_timeo)
  4267. hw_priv = NULL;
  4268. }
  4269. last_reset = jiffies;
  4270. if (hw_priv) {
  4271. hw_dis_intr(hw);
  4272. hw_disable(hw);
  4273. transmit_cleanup(hw_priv, 0);
  4274. hw_reset_pkts(&hw->rx_desc_info);
  4275. hw_reset_pkts(&hw->tx_desc_info);
  4276. ksz_init_rx_buffers(hw_priv);
  4277. hw_reset(hw);
  4278. hw_set_desc_base(hw,
  4279. hw->tx_desc_info.ring_phys,
  4280. hw->rx_desc_info.ring_phys);
  4281. hw_set_addr(hw);
  4282. if (hw->all_multi)
  4283. hw_set_multicast(hw, hw->all_multi);
  4284. else if (hw->multi_list_size)
  4285. hw_set_grp_addr(hw);
  4286. if (hw->dev_count > 1) {
  4287. hw_set_add_addr(hw);
  4288. for (port = 0; port < SWITCH_PORT_NUM; port++) {
  4289. struct net_device *port_dev;
  4290. port_set_stp_state(hw, port,
  4291. STP_STATE_DISABLED);
  4292. port_dev = hw->port_info[port].pdev;
  4293. if (netif_running(port_dev))
  4294. port_set_stp_state(hw, port,
  4295. STP_STATE_SIMPLE);
  4296. }
  4297. }
  4298. hw_enable(hw);
  4299. hw_ena_intr(hw);
  4300. }
  4301. dev->trans_start = jiffies;
  4302. netif_wake_queue(dev);
  4303. }
  4304. static inline void csum_verified(struct sk_buff *skb)
  4305. {
  4306. unsigned short protocol;
  4307. struct iphdr *iph;
  4308. protocol = skb->protocol;
  4309. skb_reset_network_header(skb);
  4310. iph = (struct iphdr *) skb_network_header(skb);
  4311. if (protocol == htons(ETH_P_8021Q)) {
  4312. protocol = iph->tot_len;
  4313. skb_set_network_header(skb, VLAN_HLEN);
  4314. iph = (struct iphdr *) skb_network_header(skb);
  4315. }
  4316. if (protocol == htons(ETH_P_IP)) {
  4317. if (iph->protocol == IPPROTO_TCP)
  4318. skb->ip_summed = CHECKSUM_UNNECESSARY;
  4319. }
  4320. }
  4321. static inline int rx_proc(struct net_device *dev, struct ksz_hw* hw,
  4322. struct ksz_desc *desc, union desc_stat status)
  4323. {
  4324. int packet_len;
  4325. struct dev_priv *priv = netdev_priv(dev);
  4326. struct dev_info *hw_priv = priv->adapter;
  4327. struct ksz_dma_buf *dma_buf;
  4328. struct sk_buff *skb;
  4329. int rx_status;
  4330. /* Received length includes 4-byte CRC. */
  4331. packet_len = status.rx.frame_len - 4;
  4332. dma_buf = DMA_BUFFER(desc);
  4333. pci_dma_sync_single_for_cpu(
  4334. hw_priv->pdev, dma_buf->dma, packet_len + 4,
  4335. PCI_DMA_FROMDEVICE);
  4336. do {
  4337. /* skb->data != skb->head */
  4338. skb = dev_alloc_skb(packet_len + 2);
  4339. if (!skb) {
  4340. dev->stats.rx_dropped++;
  4341. return -ENOMEM;
  4342. }
  4343. /*
  4344. * Align socket buffer in 4-byte boundary for better
  4345. * performance.
  4346. */
  4347. skb_reserve(skb, 2);
  4348. memcpy(skb_put(skb, packet_len),
  4349. dma_buf->skb->data, packet_len);
  4350. } while (0);
  4351. skb->protocol = eth_type_trans(skb, dev);
  4352. if (hw->rx_cfg & (DMA_RX_CSUM_UDP | DMA_RX_CSUM_TCP))
  4353. csum_verified(skb);
  4354. /* Update receive statistics. */
  4355. dev->stats.rx_packets++;
  4356. dev->stats.rx_bytes += packet_len;
  4357. /* Notify upper layer for received packet. */
  4358. rx_status = netif_rx(skb);
  4359. return 0;
  4360. }
  4361. static int dev_rcv_packets(struct dev_info *hw_priv)
  4362. {
  4363. int next;
  4364. union desc_stat status;
  4365. struct ksz_hw *hw = &hw_priv->hw;
  4366. struct net_device *dev = hw->port_info[0].pdev;
  4367. struct ksz_desc_info *info = &hw->rx_desc_info;
  4368. int left = info->alloc;
  4369. struct ksz_desc *desc;
  4370. int received = 0;
  4371. next = info->next;
  4372. while (left--) {
  4373. /* Get next descriptor which is not hardware owned. */
  4374. desc = &info->ring[next];
  4375. status.data = le32_to_cpu(desc->phw->ctrl.data);
  4376. if (status.rx.hw_owned)
  4377. break;
  4378. /* Status valid only when last descriptor bit is set. */
  4379. if (status.rx.last_desc && status.rx.first_desc) {
  4380. if (rx_proc(dev, hw, desc, status))
  4381. goto release_packet;
  4382. received++;
  4383. }
  4384. release_packet:
  4385. release_desc(desc);
  4386. next++;
  4387. next &= info->mask;
  4388. }
  4389. info->next = next;
  4390. return received;
  4391. }
  4392. static int port_rcv_packets(struct dev_info *hw_priv)
  4393. {
  4394. int next;
  4395. union desc_stat status;
  4396. struct ksz_hw *hw = &hw_priv->hw;
  4397. struct net_device *dev = hw->port_info[0].pdev;
  4398. struct ksz_desc_info *info = &hw->rx_desc_info;
  4399. int left = info->alloc;
  4400. struct ksz_desc *desc;
  4401. int received = 0;
  4402. next = info->next;
  4403. while (left--) {
  4404. /* Get next descriptor which is not hardware owned. */
  4405. desc = &info->ring[next];
  4406. status.data = le32_to_cpu(desc->phw->ctrl.data);
  4407. if (status.rx.hw_owned)
  4408. break;
  4409. if (hw->dev_count > 1) {
  4410. /* Get received port number. */
  4411. int p = HW_TO_DEV_PORT(status.rx.src_port);
  4412. dev = hw->port_info[p].pdev;
  4413. if (!netif_running(dev))
  4414. goto release_packet;
  4415. }
  4416. /* Status valid only when last descriptor bit is set. */
  4417. if (status.rx.last_desc && status.rx.first_desc) {
  4418. if (rx_proc(dev, hw, desc, status))
  4419. goto release_packet;
  4420. received++;
  4421. }
  4422. release_packet:
  4423. release_desc(desc);
  4424. next++;
  4425. next &= info->mask;
  4426. }
  4427. info->next = next;
  4428. return received;
  4429. }
  4430. static int dev_rcv_special(struct dev_info *hw_priv)
  4431. {
  4432. int next;
  4433. union desc_stat status;
  4434. struct ksz_hw *hw = &hw_priv->hw;
  4435. struct net_device *dev = hw->port_info[0].pdev;
  4436. struct ksz_desc_info *info = &hw->rx_desc_info;
  4437. int left = info->alloc;
  4438. struct ksz_desc *desc;
  4439. int received = 0;
  4440. next = info->next;
  4441. while (left--) {
  4442. /* Get next descriptor which is not hardware owned. */
  4443. desc = &info->ring[next];
  4444. status.data = le32_to_cpu(desc->phw->ctrl.data);
  4445. if (status.rx.hw_owned)
  4446. break;
  4447. if (hw->dev_count > 1) {
  4448. /* Get received port number. */
  4449. int p = HW_TO_DEV_PORT(status.rx.src_port);
  4450. dev = hw->port_info[p].pdev;
  4451. if (!netif_running(dev))
  4452. goto release_packet;
  4453. }
  4454. /* Status valid only when last descriptor bit is set. */
  4455. if (status.rx.last_desc && status.rx.first_desc) {
  4456. /*
  4457. * Receive without error. With receive errors
  4458. * disabled, packets with receive errors will be
  4459. * dropped, so no need to check the error bit.
  4460. */
  4461. if (!status.rx.error || (status.data &
  4462. KS_DESC_RX_ERROR_COND) ==
  4463. KS_DESC_RX_ERROR_TOO_LONG) {
  4464. if (rx_proc(dev, hw, desc, status))
  4465. goto release_packet;
  4466. received++;
  4467. } else {
  4468. struct dev_priv *priv = netdev_priv(dev);
  4469. /* Update receive error statistics. */
  4470. priv->port.counter[OID_COUNTER_RCV_ERROR]++;
  4471. }
  4472. }
  4473. release_packet:
  4474. release_desc(desc);
  4475. next++;
  4476. next &= info->mask;
  4477. }
  4478. info->next = next;
  4479. return received;
  4480. }
  4481. static void rx_proc_task(unsigned long data)
  4482. {
  4483. struct dev_info *hw_priv = (struct dev_info *) data;
  4484. struct ksz_hw *hw = &hw_priv->hw;
  4485. if (!hw->enabled)
  4486. return;
  4487. if (unlikely(!hw_priv->dev_rcv(hw_priv))) {
  4488. /* In case receive process is suspended because of overrun. */
  4489. hw_resume_rx(hw);
  4490. /* tasklets are interruptible. */
  4491. spin_lock_irq(&hw_priv->hwlock);
  4492. hw_turn_on_intr(hw, KS884X_INT_RX_MASK);
  4493. spin_unlock_irq(&hw_priv->hwlock);
  4494. } else {
  4495. hw_ack_intr(hw, KS884X_INT_RX);
  4496. tasklet_schedule(&hw_priv->rx_tasklet);
  4497. }
  4498. }
  4499. static void tx_proc_task(unsigned long data)
  4500. {
  4501. struct dev_info *hw_priv = (struct dev_info *) data;
  4502. struct ksz_hw *hw = &hw_priv->hw;
  4503. hw_ack_intr(hw, KS884X_INT_TX_MASK);
  4504. tx_done(hw_priv);
  4505. /* tasklets are interruptible. */
  4506. spin_lock_irq(&hw_priv->hwlock);
  4507. hw_turn_on_intr(hw, KS884X_INT_TX);
  4508. spin_unlock_irq(&hw_priv->hwlock);
  4509. }
  4510. static inline void handle_rx_stop(struct ksz_hw *hw)
  4511. {
  4512. /* Receive just has been stopped. */
  4513. if (0 == hw->rx_stop)
  4514. hw->intr_mask &= ~KS884X_INT_RX_STOPPED;
  4515. else if (hw->rx_stop > 1) {
  4516. if (hw->enabled && (hw->rx_cfg & DMA_RX_ENABLE)) {
  4517. hw_start_rx(hw);
  4518. } else {
  4519. hw->intr_mask &= ~KS884X_INT_RX_STOPPED;
  4520. hw->rx_stop = 0;
  4521. }
  4522. } else
  4523. /* Receive just has been started. */
  4524. hw->rx_stop++;
  4525. }
  4526. /**
  4527. * netdev_intr - interrupt handling
  4528. * @irq: Interrupt number.
  4529. * @dev_id: Network device.
  4530. *
  4531. * This function is called by upper network layer to signal interrupt.
  4532. *
  4533. * Return IRQ_HANDLED if interrupt is handled.
  4534. */
  4535. static irqreturn_t netdev_intr(int irq, void *dev_id)
  4536. {
  4537. uint int_enable = 0;
  4538. struct net_device *dev = (struct net_device *) dev_id;
  4539. struct dev_priv *priv = netdev_priv(dev);
  4540. struct dev_info *hw_priv = priv->adapter;
  4541. struct ksz_hw *hw = &hw_priv->hw;
  4542. hw_read_intr(hw, &int_enable);
  4543. /* Not our interrupt! */
  4544. if (!int_enable)
  4545. return IRQ_NONE;
  4546. do {
  4547. hw_ack_intr(hw, int_enable);
  4548. int_enable &= hw->intr_mask;
  4549. if (unlikely(int_enable & KS884X_INT_TX_MASK)) {
  4550. hw_dis_intr_bit(hw, KS884X_INT_TX_MASK);
  4551. tasklet_schedule(&hw_priv->tx_tasklet);
  4552. }
  4553. if (likely(int_enable & KS884X_INT_RX)) {
  4554. hw_dis_intr_bit(hw, KS884X_INT_RX);
  4555. tasklet_schedule(&hw_priv->rx_tasklet);
  4556. }
  4557. if (unlikely(int_enable & KS884X_INT_RX_OVERRUN)) {
  4558. dev->stats.rx_fifo_errors++;
  4559. hw_resume_rx(hw);
  4560. }
  4561. if (unlikely(int_enable & KS884X_INT_PHY)) {
  4562. struct ksz_port *port = &priv->port;
  4563. hw->features |= LINK_INT_WORKING;
  4564. port_get_link_speed(port);
  4565. }
  4566. if (unlikely(int_enable & KS884X_INT_RX_STOPPED)) {
  4567. handle_rx_stop(hw);
  4568. break;
  4569. }
  4570. if (unlikely(int_enable & KS884X_INT_TX_STOPPED)) {
  4571. u32 data;
  4572. hw->intr_mask &= ~KS884X_INT_TX_STOPPED;
  4573. pr_info("Tx stopped\n");
  4574. data = readl(hw->io + KS_DMA_TX_CTRL);
  4575. if (!(data & DMA_TX_ENABLE))
  4576. pr_info("Tx disabled\n");
  4577. break;
  4578. }
  4579. } while (0);
  4580. hw_ena_intr(hw);
  4581. return IRQ_HANDLED;
  4582. }
  4583. /*
  4584. * Linux network device functions
  4585. */
  4586. static unsigned long next_jiffies;
  4587. #ifdef CONFIG_NET_POLL_CONTROLLER
  4588. static void netdev_netpoll(struct net_device *dev)
  4589. {
  4590. struct dev_priv *priv = netdev_priv(dev);
  4591. struct dev_info *hw_priv = priv->adapter;
  4592. hw_dis_intr(&hw_priv->hw);
  4593. netdev_intr(dev->irq, dev);
  4594. }
  4595. #endif
  4596. static void bridge_change(struct ksz_hw *hw)
  4597. {
  4598. int port;
  4599. u8 member;
  4600. struct ksz_switch *sw = hw->ksz_switch;
  4601. /* No ports in forwarding state. */
  4602. if (!sw->member) {
  4603. port_set_stp_state(hw, SWITCH_PORT_NUM, STP_STATE_SIMPLE);
  4604. sw_block_addr(hw);
  4605. }
  4606. for (port = 0; port < SWITCH_PORT_NUM; port++) {
  4607. if (STP_STATE_FORWARDING == sw->port_cfg[port].stp_state)
  4608. member = HOST_MASK | sw->member;
  4609. else
  4610. member = HOST_MASK | (1 << port);
  4611. if (member != sw->port_cfg[port].member)
  4612. sw_cfg_port_base_vlan(hw, port, member);
  4613. }
  4614. }
  4615. /**
  4616. * netdev_close - close network device
  4617. * @dev: Network device.
  4618. *
  4619. * This function process the close operation of network device. This is caused
  4620. * by the user command "ifconfig ethX down."
  4621. *
  4622. * Return 0 if successful; otherwise an error code indicating failure.
  4623. */
  4624. static int netdev_close(struct net_device *dev)
  4625. {
  4626. struct dev_priv *priv = netdev_priv(dev);
  4627. struct dev_info *hw_priv = priv->adapter;
  4628. struct ksz_port *port = &priv->port;
  4629. struct ksz_hw *hw = &hw_priv->hw;
  4630. int pi;
  4631. netif_stop_queue(dev);
  4632. ksz_stop_timer(&priv->monitor_timer_info);
  4633. /* Need to shut the port manually in multiple device interfaces mode. */
  4634. if (hw->dev_count > 1) {
  4635. port_set_stp_state(hw, port->first_port, STP_STATE_DISABLED);
  4636. /* Port is closed. Need to change bridge setting. */
  4637. if (hw->features & STP_SUPPORT) {
  4638. pi = 1 << port->first_port;
  4639. if (hw->ksz_switch->member & pi) {
  4640. hw->ksz_switch->member &= ~pi;
  4641. bridge_change(hw);
  4642. }
  4643. }
  4644. }
  4645. if (port->first_port > 0)
  4646. hw_del_addr(hw, dev->dev_addr);
  4647. if (!hw_priv->wol_enable)
  4648. port_set_power_saving(port, true);
  4649. if (priv->multicast)
  4650. --hw->all_multi;
  4651. if (priv->promiscuous)
  4652. --hw->promiscuous;
  4653. hw_priv->opened--;
  4654. if (!(hw_priv->opened)) {
  4655. ksz_stop_timer(&hw_priv->mib_timer_info);
  4656. flush_work(&hw_priv->mib_read);
  4657. hw_dis_intr(hw);
  4658. hw_disable(hw);
  4659. hw_clr_multicast(hw);
  4660. /* Delay for receive task to stop scheduling itself. */
  4661. msleep(2000 / HZ);
  4662. tasklet_disable(&hw_priv->rx_tasklet);
  4663. tasklet_disable(&hw_priv->tx_tasklet);
  4664. free_irq(dev->irq, hw_priv->dev);
  4665. transmit_cleanup(hw_priv, 0);
  4666. hw_reset_pkts(&hw->rx_desc_info);
  4667. hw_reset_pkts(&hw->tx_desc_info);
  4668. /* Clean out static MAC table when the switch is shutdown. */
  4669. if (hw->features & STP_SUPPORT)
  4670. sw_clr_sta_mac_table(hw);
  4671. }
  4672. return 0;
  4673. }
  4674. static void hw_cfg_huge_frame(struct dev_info *hw_priv, struct ksz_hw *hw)
  4675. {
  4676. if (hw->ksz_switch) {
  4677. u32 data;
  4678. data = readw(hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
  4679. if (hw->features & RX_HUGE_FRAME)
  4680. data |= SWITCH_HUGE_PACKET;
  4681. else
  4682. data &= ~SWITCH_HUGE_PACKET;
  4683. writew(data, hw->io + KS8842_SWITCH_CTRL_2_OFFSET);
  4684. }
  4685. if (hw->features & RX_HUGE_FRAME) {
  4686. hw->rx_cfg |= DMA_RX_ERROR;
  4687. hw_priv->dev_rcv = dev_rcv_special;
  4688. } else {
  4689. hw->rx_cfg &= ~DMA_RX_ERROR;
  4690. if (hw->dev_count > 1)
  4691. hw_priv->dev_rcv = port_rcv_packets;
  4692. else
  4693. hw_priv->dev_rcv = dev_rcv_packets;
  4694. }
  4695. }
  4696. static int prepare_hardware(struct net_device *dev)
  4697. {
  4698. struct dev_priv *priv = netdev_priv(dev);
  4699. struct dev_info *hw_priv = priv->adapter;
  4700. struct ksz_hw *hw = &hw_priv->hw;
  4701. int rc = 0;
  4702. /* Remember the network device that requests interrupts. */
  4703. hw_priv->dev = dev;
  4704. rc = request_irq(dev->irq, netdev_intr, IRQF_SHARED, dev->name, dev);
  4705. if (rc)
  4706. return rc;
  4707. tasklet_enable(&hw_priv->rx_tasklet);
  4708. tasklet_enable(&hw_priv->tx_tasklet);
  4709. hw->promiscuous = 0;
  4710. hw->all_multi = 0;
  4711. hw->multi_list_size = 0;
  4712. hw_reset(hw);
  4713. hw_set_desc_base(hw,
  4714. hw->tx_desc_info.ring_phys, hw->rx_desc_info.ring_phys);
  4715. hw_set_addr(hw);
  4716. hw_cfg_huge_frame(hw_priv, hw);
  4717. ksz_init_rx_buffers(hw_priv);
  4718. return 0;
  4719. }
  4720. static void set_media_state(struct net_device *dev, int media_state)
  4721. {
  4722. struct dev_priv *priv = netdev_priv(dev);
  4723. if (media_state == priv->media_state)
  4724. netif_carrier_on(dev);
  4725. else
  4726. netif_carrier_off(dev);
  4727. netif_info(priv, link, dev, "link %s\n",
  4728. media_state == priv->media_state ? "on" : "off");
  4729. }
  4730. /**
  4731. * netdev_open - open network device
  4732. * @dev: Network device.
  4733. *
  4734. * This function process the open operation of network device. This is caused
  4735. * by the user command "ifconfig ethX up."
  4736. *
  4737. * Return 0 if successful; otherwise an error code indicating failure.
  4738. */
  4739. static int netdev_open(struct net_device *dev)
  4740. {
  4741. struct dev_priv *priv = netdev_priv(dev);
  4742. struct dev_info *hw_priv = priv->adapter;
  4743. struct ksz_hw *hw = &hw_priv->hw;
  4744. struct ksz_port *port = &priv->port;
  4745. int i;
  4746. int p;
  4747. int rc = 0;
  4748. priv->multicast = 0;
  4749. priv->promiscuous = 0;
  4750. /* Reset device statistics. */
  4751. memset(&dev->stats, 0, sizeof(struct net_device_stats));
  4752. memset((void *) port->counter, 0,
  4753. (sizeof(u64) * OID_COUNTER_LAST));
  4754. if (!(hw_priv->opened)) {
  4755. rc = prepare_hardware(dev);
  4756. if (rc)
  4757. return rc;
  4758. for (i = 0; i < hw->mib_port_cnt; i++) {
  4759. if (next_jiffies < jiffies)
  4760. next_jiffies = jiffies + HZ * 2;
  4761. else
  4762. next_jiffies += HZ * 1;
  4763. hw_priv->counter[i].time = next_jiffies;
  4764. hw->port_mib[i].state = media_disconnected;
  4765. port_init_cnt(hw, i);
  4766. }
  4767. if (hw->ksz_switch)
  4768. hw->port_mib[HOST_PORT].state = media_connected;
  4769. else {
  4770. hw_add_wol_bcast(hw);
  4771. hw_cfg_wol_pme(hw, 0);
  4772. hw_clr_wol_pme_status(&hw_priv->hw);
  4773. }
  4774. }
  4775. port_set_power_saving(port, false);
  4776. for (i = 0, p = port->first_port; i < port->port_cnt; i++, p++) {
  4777. /*
  4778. * Initialize to invalid value so that link detection
  4779. * is done.
  4780. */
  4781. hw->port_info[p].partner = 0xFF;
  4782. hw->port_info[p].state = media_disconnected;
  4783. }
  4784. /* Need to open the port in multiple device interfaces mode. */
  4785. if (hw->dev_count > 1) {
  4786. port_set_stp_state(hw, port->first_port, STP_STATE_SIMPLE);
  4787. if (port->first_port > 0)
  4788. hw_add_addr(hw, dev->dev_addr);
  4789. }
  4790. port_get_link_speed(port);
  4791. if (port->force_link)
  4792. port_force_link_speed(port);
  4793. else
  4794. port_set_link_speed(port);
  4795. if (!(hw_priv->opened)) {
  4796. hw_setup_intr(hw);
  4797. hw_enable(hw);
  4798. hw_ena_intr(hw);
  4799. if (hw->mib_port_cnt)
  4800. ksz_start_timer(&hw_priv->mib_timer_info,
  4801. hw_priv->mib_timer_info.period);
  4802. }
  4803. hw_priv->opened++;
  4804. ksz_start_timer(&priv->monitor_timer_info,
  4805. priv->monitor_timer_info.period);
  4806. priv->media_state = port->linked->state;
  4807. set_media_state(dev, media_connected);
  4808. netif_start_queue(dev);
  4809. return 0;
  4810. }
  4811. /* RX errors = rx_errors */
  4812. /* RX dropped = rx_dropped */
  4813. /* RX overruns = rx_fifo_errors */
  4814. /* RX frame = rx_crc_errors + rx_frame_errors + rx_length_errors */
  4815. /* TX errors = tx_errors */
  4816. /* TX dropped = tx_dropped */
  4817. /* TX overruns = tx_fifo_errors */
  4818. /* TX carrier = tx_aborted_errors + tx_carrier_errors + tx_window_errors */
  4819. /* collisions = collisions */
  4820. /**
  4821. * netdev_query_statistics - query network device statistics
  4822. * @dev: Network device.
  4823. *
  4824. * This function returns the statistics of the network device. The device
  4825. * needs not be opened.
  4826. *
  4827. * Return network device statistics.
  4828. */
  4829. static struct net_device_stats *netdev_query_statistics(struct net_device *dev)
  4830. {
  4831. struct dev_priv *priv = netdev_priv(dev);
  4832. struct ksz_port *port = &priv->port;
  4833. struct ksz_hw *hw = &priv->adapter->hw;
  4834. struct ksz_port_mib *mib;
  4835. int i;
  4836. int p;
  4837. dev->stats.rx_errors = port->counter[OID_COUNTER_RCV_ERROR];
  4838. dev->stats.tx_errors = port->counter[OID_COUNTER_XMIT_ERROR];
  4839. /* Reset to zero to add count later. */
  4840. dev->stats.multicast = 0;
  4841. dev->stats.collisions = 0;
  4842. dev->stats.rx_length_errors = 0;
  4843. dev->stats.rx_crc_errors = 0;
  4844. dev->stats.rx_frame_errors = 0;
  4845. dev->stats.tx_window_errors = 0;
  4846. for (i = 0, p = port->first_port; i < port->mib_port_cnt; i++, p++) {
  4847. mib = &hw->port_mib[p];
  4848. dev->stats.multicast += (unsigned long)
  4849. mib->counter[MIB_COUNTER_RX_MULTICAST];
  4850. dev->stats.collisions += (unsigned long)
  4851. mib->counter[MIB_COUNTER_TX_TOTAL_COLLISION];
  4852. dev->stats.rx_length_errors += (unsigned long)(
  4853. mib->counter[MIB_COUNTER_RX_UNDERSIZE] +
  4854. mib->counter[MIB_COUNTER_RX_FRAGMENT] +
  4855. mib->counter[MIB_COUNTER_RX_OVERSIZE] +
  4856. mib->counter[MIB_COUNTER_RX_JABBER]);
  4857. dev->stats.rx_crc_errors += (unsigned long)
  4858. mib->counter[MIB_COUNTER_RX_CRC_ERR];
  4859. dev->stats.rx_frame_errors += (unsigned long)(
  4860. mib->counter[MIB_COUNTER_RX_ALIGNMENT_ERR] +
  4861. mib->counter[MIB_COUNTER_RX_SYMBOL_ERR]);
  4862. dev->stats.tx_window_errors += (unsigned long)
  4863. mib->counter[MIB_COUNTER_TX_LATE_COLLISION];
  4864. }
  4865. return &dev->stats;
  4866. }
  4867. /**
  4868. * netdev_set_mac_address - set network device MAC address
  4869. * @dev: Network device.
  4870. * @addr: Buffer of MAC address.
  4871. *
  4872. * This function is used to set the MAC address of the network device.
  4873. *
  4874. * Return 0 to indicate success.
  4875. */
  4876. static int netdev_set_mac_address(struct net_device *dev, void *addr)
  4877. {
  4878. struct dev_priv *priv = netdev_priv(dev);
  4879. struct dev_info *hw_priv = priv->adapter;
  4880. struct ksz_hw *hw = &hw_priv->hw;
  4881. struct sockaddr *mac = addr;
  4882. uint interrupt;
  4883. if (priv->port.first_port > 0)
  4884. hw_del_addr(hw, dev->dev_addr);
  4885. else {
  4886. hw->mac_override = 1;
  4887. memcpy(hw->override_addr, mac->sa_data, MAC_ADDR_LEN);
  4888. }
  4889. memcpy(dev->dev_addr, mac->sa_data, MAX_ADDR_LEN);
  4890. interrupt = hw_block_intr(hw);
  4891. if (priv->port.first_port > 0)
  4892. hw_add_addr(hw, dev->dev_addr);
  4893. else
  4894. hw_set_addr(hw);
  4895. hw_restore_intr(hw, interrupt);
  4896. return 0;
  4897. }
  4898. static void dev_set_promiscuous(struct net_device *dev, struct dev_priv *priv,
  4899. struct ksz_hw *hw, int promiscuous)
  4900. {
  4901. if (promiscuous != priv->promiscuous) {
  4902. u8 prev_state = hw->promiscuous;
  4903. if (promiscuous)
  4904. ++hw->promiscuous;
  4905. else
  4906. --hw->promiscuous;
  4907. priv->promiscuous = promiscuous;
  4908. /* Turn on/off promiscuous mode. */
  4909. if (hw->promiscuous <= 1 && prev_state <= 1)
  4910. hw_set_promiscuous(hw, hw->promiscuous);
  4911. /*
  4912. * Port is not in promiscuous mode, meaning it is released
  4913. * from the bridge.
  4914. */
  4915. if ((hw->features & STP_SUPPORT) && !promiscuous &&
  4916. (dev->priv_flags & IFF_BRIDGE_PORT)) {
  4917. struct ksz_switch *sw = hw->ksz_switch;
  4918. int port = priv->port.first_port;
  4919. port_set_stp_state(hw, port, STP_STATE_DISABLED);
  4920. port = 1 << port;
  4921. if (sw->member & port) {
  4922. sw->member &= ~port;
  4923. bridge_change(hw);
  4924. }
  4925. }
  4926. }
  4927. }
  4928. static void dev_set_multicast(struct dev_priv *priv, struct ksz_hw *hw,
  4929. int multicast)
  4930. {
  4931. if (multicast != priv->multicast) {
  4932. u8 all_multi = hw->all_multi;
  4933. if (multicast)
  4934. ++hw->all_multi;
  4935. else
  4936. --hw->all_multi;
  4937. priv->multicast = multicast;
  4938. /* Turn on/off all multicast mode. */
  4939. if (hw->all_multi <= 1 && all_multi <= 1)
  4940. hw_set_multicast(hw, hw->all_multi);
  4941. }
  4942. }
  4943. /**
  4944. * netdev_set_rx_mode
  4945. * @dev: Network device.
  4946. *
  4947. * This routine is used to set multicast addresses or put the network device
  4948. * into promiscuous mode.
  4949. */
  4950. static void netdev_set_rx_mode(struct net_device *dev)
  4951. {
  4952. struct dev_priv *priv = netdev_priv(dev);
  4953. struct dev_info *hw_priv = priv->adapter;
  4954. struct ksz_hw *hw = &hw_priv->hw;
  4955. struct netdev_hw_addr *ha;
  4956. int multicast = (dev->flags & IFF_ALLMULTI);
  4957. dev_set_promiscuous(dev, priv, hw, (dev->flags & IFF_PROMISC));
  4958. if (hw_priv->hw.dev_count > 1)
  4959. multicast |= (dev->flags & IFF_MULTICAST);
  4960. dev_set_multicast(priv, hw, multicast);
  4961. /* Cannot use different hashes in multiple device interfaces mode. */
  4962. if (hw_priv->hw.dev_count > 1)
  4963. return;
  4964. if ((dev->flags & IFF_MULTICAST) && !netdev_mc_empty(dev)) {
  4965. int i = 0;
  4966. /* List too big to support so turn on all multicast mode. */
  4967. if (netdev_mc_count(dev) > MAX_MULTICAST_LIST) {
  4968. if (MAX_MULTICAST_LIST != hw->multi_list_size) {
  4969. hw->multi_list_size = MAX_MULTICAST_LIST;
  4970. ++hw->all_multi;
  4971. hw_set_multicast(hw, hw->all_multi);
  4972. }
  4973. return;
  4974. }
  4975. netdev_for_each_mc_addr(ha, dev) {
  4976. if (i >= MAX_MULTICAST_LIST)
  4977. break;
  4978. memcpy(hw->multi_list[i++], ha->addr, MAC_ADDR_LEN);
  4979. }
  4980. hw->multi_list_size = (u8) i;
  4981. hw_set_grp_addr(hw);
  4982. } else {
  4983. if (MAX_MULTICAST_LIST == hw->multi_list_size) {
  4984. --hw->all_multi;
  4985. hw_set_multicast(hw, hw->all_multi);
  4986. }
  4987. hw->multi_list_size = 0;
  4988. hw_clr_multicast(hw);
  4989. }
  4990. }
  4991. static int netdev_change_mtu(struct net_device *dev, int new_mtu)
  4992. {
  4993. struct dev_priv *priv = netdev_priv(dev);
  4994. struct dev_info *hw_priv = priv->adapter;
  4995. struct ksz_hw *hw = &hw_priv->hw;
  4996. int hw_mtu;
  4997. if (netif_running(dev))
  4998. return -EBUSY;
  4999. /* Cannot use different MTU in multiple device interfaces mode. */
  5000. if (hw->dev_count > 1)
  5001. if (dev != hw_priv->dev)
  5002. return 0;
  5003. if (new_mtu < 60)
  5004. return -EINVAL;
  5005. if (dev->mtu != new_mtu) {
  5006. hw_mtu = new_mtu + ETHERNET_HEADER_SIZE + 4;
  5007. if (hw_mtu > MAX_RX_BUF_SIZE)
  5008. return -EINVAL;
  5009. if (hw_mtu > REGULAR_RX_BUF_SIZE) {
  5010. hw->features |= RX_HUGE_FRAME;
  5011. hw_mtu = MAX_RX_BUF_SIZE;
  5012. } else {
  5013. hw->features &= ~RX_HUGE_FRAME;
  5014. hw_mtu = REGULAR_RX_BUF_SIZE;
  5015. }
  5016. hw_mtu = (hw_mtu + 3) & ~3;
  5017. hw_priv->mtu = hw_mtu;
  5018. dev->mtu = new_mtu;
  5019. }
  5020. return 0;
  5021. }
  5022. /**
  5023. * netdev_ioctl - I/O control processing
  5024. * @dev: Network device.
  5025. * @ifr: Interface request structure.
  5026. * @cmd: I/O control code.
  5027. *
  5028. * This function is used to process I/O control calls.
  5029. *
  5030. * Return 0 to indicate success.
  5031. */
  5032. static int netdev_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  5033. {
  5034. struct dev_priv *priv = netdev_priv(dev);
  5035. struct dev_info *hw_priv = priv->adapter;
  5036. struct ksz_hw *hw = &hw_priv->hw;
  5037. struct ksz_port *port = &priv->port;
  5038. int rc;
  5039. int result = 0;
  5040. struct mii_ioctl_data *data = if_mii(ifr);
  5041. if (down_interruptible(&priv->proc_sem))
  5042. return -ERESTARTSYS;
  5043. /* assume success */
  5044. rc = 0;
  5045. switch (cmd) {
  5046. /* Get address of MII PHY in use. */
  5047. case SIOCGMIIPHY:
  5048. data->phy_id = priv->id;
  5049. /* Fallthrough... */
  5050. /* Read MII PHY register. */
  5051. case SIOCGMIIREG:
  5052. if (data->phy_id != priv->id || data->reg_num >= 6)
  5053. result = -EIO;
  5054. else
  5055. hw_r_phy(hw, port->linked->port_id, data->reg_num,
  5056. &data->val_out);
  5057. break;
  5058. /* Write MII PHY register. */
  5059. case SIOCSMIIREG:
  5060. if (!capable(CAP_NET_ADMIN))
  5061. result = -EPERM;
  5062. else if (data->phy_id != priv->id || data->reg_num >= 6)
  5063. result = -EIO;
  5064. else
  5065. hw_w_phy(hw, port->linked->port_id, data->reg_num,
  5066. data->val_in);
  5067. break;
  5068. default:
  5069. result = -EOPNOTSUPP;
  5070. }
  5071. up(&priv->proc_sem);
  5072. return result;
  5073. }
  5074. /*
  5075. * MII support
  5076. */
  5077. /**
  5078. * mdio_read - read PHY register
  5079. * @dev: Network device.
  5080. * @phy_id: The PHY id.
  5081. * @reg_num: The register number.
  5082. *
  5083. * This function returns the PHY register value.
  5084. *
  5085. * Return the register value.
  5086. */
  5087. static int mdio_read(struct net_device *dev, int phy_id, int reg_num)
  5088. {
  5089. struct dev_priv *priv = netdev_priv(dev);
  5090. struct ksz_port *port = &priv->port;
  5091. struct ksz_hw *hw = port->hw;
  5092. u16 val_out;
  5093. hw_r_phy(hw, port->linked->port_id, reg_num << 1, &val_out);
  5094. return val_out;
  5095. }
  5096. /**
  5097. * mdio_write - set PHY register
  5098. * @dev: Network device.
  5099. * @phy_id: The PHY id.
  5100. * @reg_num: The register number.
  5101. * @val: The register value.
  5102. *
  5103. * This procedure sets the PHY register value.
  5104. */
  5105. static void mdio_write(struct net_device *dev, int phy_id, int reg_num, int val)
  5106. {
  5107. struct dev_priv *priv = netdev_priv(dev);
  5108. struct ksz_port *port = &priv->port;
  5109. struct ksz_hw *hw = port->hw;
  5110. int i;
  5111. int pi;
  5112. for (i = 0, pi = port->first_port; i < port->port_cnt; i++, pi++)
  5113. hw_w_phy(hw, pi, reg_num << 1, val);
  5114. }
  5115. /*
  5116. * ethtool support
  5117. */
  5118. #define EEPROM_SIZE 0x40
  5119. static u16 eeprom_data[EEPROM_SIZE] = { 0 };
  5120. #define ADVERTISED_ALL \
  5121. (ADVERTISED_10baseT_Half | \
  5122. ADVERTISED_10baseT_Full | \
  5123. ADVERTISED_100baseT_Half | \
  5124. ADVERTISED_100baseT_Full)
  5125. /* These functions use the MII functions in mii.c. */
  5126. /**
  5127. * netdev_get_settings - get network device settings
  5128. * @dev: Network device.
  5129. * @cmd: Ethtool command.
  5130. *
  5131. * This function queries the PHY and returns its state in the ethtool command.
  5132. *
  5133. * Return 0 if successful; otherwise an error code.
  5134. */
  5135. static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  5136. {
  5137. struct dev_priv *priv = netdev_priv(dev);
  5138. struct dev_info *hw_priv = priv->adapter;
  5139. mutex_lock(&hw_priv->lock);
  5140. mii_ethtool_gset(&priv->mii_if, cmd);
  5141. cmd->advertising |= SUPPORTED_TP;
  5142. mutex_unlock(&hw_priv->lock);
  5143. /* Save advertised settings for workaround in next function. */
  5144. priv->advertising = cmd->advertising;
  5145. return 0;
  5146. }
  5147. /**
  5148. * netdev_set_settings - set network device settings
  5149. * @dev: Network device.
  5150. * @cmd: Ethtool command.
  5151. *
  5152. * This function sets the PHY according to the ethtool command.
  5153. *
  5154. * Return 0 if successful; otherwise an error code.
  5155. */
  5156. static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  5157. {
  5158. struct dev_priv *priv = netdev_priv(dev);
  5159. struct dev_info *hw_priv = priv->adapter;
  5160. struct ksz_port *port = &priv->port;
  5161. u32 speed = ethtool_cmd_speed(cmd);
  5162. int rc;
  5163. /*
  5164. * ethtool utility does not change advertised setting if auto
  5165. * negotiation is not specified explicitly.
  5166. */
  5167. if (cmd->autoneg && priv->advertising == cmd->advertising) {
  5168. cmd->advertising |= ADVERTISED_ALL;
  5169. if (10 == speed)
  5170. cmd->advertising &=
  5171. ~(ADVERTISED_100baseT_Full |
  5172. ADVERTISED_100baseT_Half);
  5173. else if (100 == speed)
  5174. cmd->advertising &=
  5175. ~(ADVERTISED_10baseT_Full |
  5176. ADVERTISED_10baseT_Half);
  5177. if (0 == cmd->duplex)
  5178. cmd->advertising &=
  5179. ~(ADVERTISED_100baseT_Full |
  5180. ADVERTISED_10baseT_Full);
  5181. else if (1 == cmd->duplex)
  5182. cmd->advertising &=
  5183. ~(ADVERTISED_100baseT_Half |
  5184. ADVERTISED_10baseT_Half);
  5185. }
  5186. mutex_lock(&hw_priv->lock);
  5187. if (cmd->autoneg &&
  5188. (cmd->advertising & ADVERTISED_ALL) ==
  5189. ADVERTISED_ALL) {
  5190. port->duplex = 0;
  5191. port->speed = 0;
  5192. port->force_link = 0;
  5193. } else {
  5194. port->duplex = cmd->duplex + 1;
  5195. if (1000 != speed)
  5196. port->speed = speed;
  5197. if (cmd->autoneg)
  5198. port->force_link = 0;
  5199. else
  5200. port->force_link = 1;
  5201. }
  5202. rc = mii_ethtool_sset(&priv->mii_if, cmd);
  5203. mutex_unlock(&hw_priv->lock);
  5204. return rc;
  5205. }
  5206. /**
  5207. * netdev_nway_reset - restart auto-negotiation
  5208. * @dev: Network device.
  5209. *
  5210. * This function restarts the PHY for auto-negotiation.
  5211. *
  5212. * Return 0 if successful; otherwise an error code.
  5213. */
  5214. static int netdev_nway_reset(struct net_device *dev)
  5215. {
  5216. struct dev_priv *priv = netdev_priv(dev);
  5217. struct dev_info *hw_priv = priv->adapter;
  5218. int rc;
  5219. mutex_lock(&hw_priv->lock);
  5220. rc = mii_nway_restart(&priv->mii_if);
  5221. mutex_unlock(&hw_priv->lock);
  5222. return rc;
  5223. }
  5224. /**
  5225. * netdev_get_link - get network device link status
  5226. * @dev: Network device.
  5227. *
  5228. * This function gets the link status from the PHY.
  5229. *
  5230. * Return true if PHY is linked and false otherwise.
  5231. */
  5232. static u32 netdev_get_link(struct net_device *dev)
  5233. {
  5234. struct dev_priv *priv = netdev_priv(dev);
  5235. int rc;
  5236. rc = mii_link_ok(&priv->mii_if);
  5237. return rc;
  5238. }
  5239. /**
  5240. * netdev_get_drvinfo - get network driver information
  5241. * @dev: Network device.
  5242. * @info: Ethtool driver info data structure.
  5243. *
  5244. * This procedure returns the driver information.
  5245. */
  5246. static void netdev_get_drvinfo(struct net_device *dev,
  5247. struct ethtool_drvinfo *info)
  5248. {
  5249. struct dev_priv *priv = netdev_priv(dev);
  5250. struct dev_info *hw_priv = priv->adapter;
  5251. strcpy(info->driver, DRV_NAME);
  5252. strcpy(info->version, DRV_VERSION);
  5253. strcpy(info->bus_info, pci_name(hw_priv->pdev));
  5254. }
  5255. /**
  5256. * netdev_get_regs_len - get length of register dump
  5257. * @dev: Network device.
  5258. *
  5259. * This function returns the length of the register dump.
  5260. *
  5261. * Return length of the register dump.
  5262. */
  5263. static struct hw_regs {
  5264. int start;
  5265. int end;
  5266. } hw_regs_range[] = {
  5267. { KS_DMA_TX_CTRL, KS884X_INTERRUPTS_STATUS },
  5268. { KS_ADD_ADDR_0_LO, KS_ADD_ADDR_F_HI },
  5269. { KS884X_ADDR_0_OFFSET, KS8841_WOL_FRAME_BYTE2_OFFSET },
  5270. { KS884X_SIDER_P, KS8842_SGCR7_P },
  5271. { KS8842_MACAR1_P, KS8842_TOSR8_P },
  5272. { KS884X_P1MBCR_P, KS8842_P3ERCR_P },
  5273. { 0, 0 }
  5274. };
  5275. static int netdev_get_regs_len(struct net_device *dev)
  5276. {
  5277. struct hw_regs *range = hw_regs_range;
  5278. int regs_len = 0x10 * sizeof(u32);
  5279. while (range->end > range->start) {
  5280. regs_len += (range->end - range->start + 3) / 4 * 4;
  5281. range++;
  5282. }
  5283. return regs_len;
  5284. }
  5285. /**
  5286. * netdev_get_regs - get register dump
  5287. * @dev: Network device.
  5288. * @regs: Ethtool registers data structure.
  5289. * @ptr: Buffer to store the register values.
  5290. *
  5291. * This procedure dumps the register values in the provided buffer.
  5292. */
  5293. static void netdev_get_regs(struct net_device *dev, struct ethtool_regs *regs,
  5294. void *ptr)
  5295. {
  5296. struct dev_priv *priv = netdev_priv(dev);
  5297. struct dev_info *hw_priv = priv->adapter;
  5298. struct ksz_hw *hw = &hw_priv->hw;
  5299. int *buf = (int *) ptr;
  5300. struct hw_regs *range = hw_regs_range;
  5301. int len;
  5302. mutex_lock(&hw_priv->lock);
  5303. regs->version = 0;
  5304. for (len = 0; len < 0x40; len += 4) {
  5305. pci_read_config_dword(hw_priv->pdev, len, buf);
  5306. buf++;
  5307. }
  5308. while (range->end > range->start) {
  5309. for (len = range->start; len < range->end; len += 4) {
  5310. *buf = readl(hw->io + len);
  5311. buf++;
  5312. }
  5313. range++;
  5314. }
  5315. mutex_unlock(&hw_priv->lock);
  5316. }
  5317. #define WOL_SUPPORT \
  5318. (WAKE_PHY | WAKE_MAGIC | \
  5319. WAKE_UCAST | WAKE_MCAST | \
  5320. WAKE_BCAST | WAKE_ARP)
  5321. /**
  5322. * netdev_get_wol - get Wake-on-LAN support
  5323. * @dev: Network device.
  5324. * @wol: Ethtool Wake-on-LAN data structure.
  5325. *
  5326. * This procedure returns Wake-on-LAN support.
  5327. */
  5328. static void netdev_get_wol(struct net_device *dev,
  5329. struct ethtool_wolinfo *wol)
  5330. {
  5331. struct dev_priv *priv = netdev_priv(dev);
  5332. struct dev_info *hw_priv = priv->adapter;
  5333. wol->supported = hw_priv->wol_support;
  5334. wol->wolopts = hw_priv->wol_enable;
  5335. memset(&wol->sopass, 0, sizeof(wol->sopass));
  5336. }
  5337. /**
  5338. * netdev_set_wol - set Wake-on-LAN support
  5339. * @dev: Network device.
  5340. * @wol: Ethtool Wake-on-LAN data structure.
  5341. *
  5342. * This function sets Wake-on-LAN support.
  5343. *
  5344. * Return 0 if successful; otherwise an error code.
  5345. */
  5346. static int netdev_set_wol(struct net_device *dev,
  5347. struct ethtool_wolinfo *wol)
  5348. {
  5349. struct dev_priv *priv = netdev_priv(dev);
  5350. struct dev_info *hw_priv = priv->adapter;
  5351. /* Need to find a way to retrieve the device IP address. */
  5352. static const u8 net_addr[] = { 192, 168, 1, 1 };
  5353. if (wol->wolopts & ~hw_priv->wol_support)
  5354. return -EINVAL;
  5355. hw_priv->wol_enable = wol->wolopts;
  5356. /* Link wakeup cannot really be disabled. */
  5357. if (wol->wolopts)
  5358. hw_priv->wol_enable |= WAKE_PHY;
  5359. hw_enable_wol(&hw_priv->hw, hw_priv->wol_enable, net_addr);
  5360. return 0;
  5361. }
  5362. /**
  5363. * netdev_get_msglevel - get debug message level
  5364. * @dev: Network device.
  5365. *
  5366. * This function returns current debug message level.
  5367. *
  5368. * Return current debug message flags.
  5369. */
  5370. static u32 netdev_get_msglevel(struct net_device *dev)
  5371. {
  5372. struct dev_priv *priv = netdev_priv(dev);
  5373. return priv->msg_enable;
  5374. }
  5375. /**
  5376. * netdev_set_msglevel - set debug message level
  5377. * @dev: Network device.
  5378. * @value: Debug message flags.
  5379. *
  5380. * This procedure sets debug message level.
  5381. */
  5382. static void netdev_set_msglevel(struct net_device *dev, u32 value)
  5383. {
  5384. struct dev_priv *priv = netdev_priv(dev);
  5385. priv->msg_enable = value;
  5386. }
  5387. /**
  5388. * netdev_get_eeprom_len - get EEPROM length
  5389. * @dev: Network device.
  5390. *
  5391. * This function returns the length of the EEPROM.
  5392. *
  5393. * Return length of the EEPROM.
  5394. */
  5395. static int netdev_get_eeprom_len(struct net_device *dev)
  5396. {
  5397. return EEPROM_SIZE * 2;
  5398. }
  5399. /**
  5400. * netdev_get_eeprom - get EEPROM data
  5401. * @dev: Network device.
  5402. * @eeprom: Ethtool EEPROM data structure.
  5403. * @data: Buffer to store the EEPROM data.
  5404. *
  5405. * This function dumps the EEPROM data in the provided buffer.
  5406. *
  5407. * Return 0 if successful; otherwise an error code.
  5408. */
  5409. #define EEPROM_MAGIC 0x10A18842
  5410. static int netdev_get_eeprom(struct net_device *dev,
  5411. struct ethtool_eeprom *eeprom, u8 *data)
  5412. {
  5413. struct dev_priv *priv = netdev_priv(dev);
  5414. struct dev_info *hw_priv = priv->adapter;
  5415. u8 *eeprom_byte = (u8 *) eeprom_data;
  5416. int i;
  5417. int len;
  5418. len = (eeprom->offset + eeprom->len + 1) / 2;
  5419. for (i = eeprom->offset / 2; i < len; i++)
  5420. eeprom_data[i] = eeprom_read(&hw_priv->hw, i);
  5421. eeprom->magic = EEPROM_MAGIC;
  5422. memcpy(data, &eeprom_byte[eeprom->offset], eeprom->len);
  5423. return 0;
  5424. }
  5425. /**
  5426. * netdev_set_eeprom - write EEPROM data
  5427. * @dev: Network device.
  5428. * @eeprom: Ethtool EEPROM data structure.
  5429. * @data: Data buffer.
  5430. *
  5431. * This function modifies the EEPROM data one byte at a time.
  5432. *
  5433. * Return 0 if successful; otherwise an error code.
  5434. */
  5435. static int netdev_set_eeprom(struct net_device *dev,
  5436. struct ethtool_eeprom *eeprom, u8 *data)
  5437. {
  5438. struct dev_priv *priv = netdev_priv(dev);
  5439. struct dev_info *hw_priv = priv->adapter;
  5440. u16 eeprom_word[EEPROM_SIZE];
  5441. u8 *eeprom_byte = (u8 *) eeprom_word;
  5442. int i;
  5443. int len;
  5444. if (eeprom->magic != EEPROM_MAGIC)
  5445. return -EINVAL;
  5446. len = (eeprom->offset + eeprom->len + 1) / 2;
  5447. for (i = eeprom->offset / 2; i < len; i++)
  5448. eeprom_data[i] = eeprom_read(&hw_priv->hw, i);
  5449. memcpy(eeprom_word, eeprom_data, EEPROM_SIZE * 2);
  5450. memcpy(&eeprom_byte[eeprom->offset], data, eeprom->len);
  5451. for (i = 0; i < EEPROM_SIZE; i++)
  5452. if (eeprom_word[i] != eeprom_data[i]) {
  5453. eeprom_data[i] = eeprom_word[i];
  5454. eeprom_write(&hw_priv->hw, i, eeprom_data[i]);
  5455. }
  5456. return 0;
  5457. }
  5458. /**
  5459. * netdev_get_pauseparam - get flow control parameters
  5460. * @dev: Network device.
  5461. * @pause: Ethtool PAUSE settings data structure.
  5462. *
  5463. * This procedure returns the PAUSE control flow settings.
  5464. */
  5465. static void netdev_get_pauseparam(struct net_device *dev,
  5466. struct ethtool_pauseparam *pause)
  5467. {
  5468. struct dev_priv *priv = netdev_priv(dev);
  5469. struct dev_info *hw_priv = priv->adapter;
  5470. struct ksz_hw *hw = &hw_priv->hw;
  5471. pause->autoneg = (hw->overrides & PAUSE_FLOW_CTRL) ? 0 : 1;
  5472. if (!hw->ksz_switch) {
  5473. pause->rx_pause =
  5474. (hw->rx_cfg & DMA_RX_FLOW_ENABLE) ? 1 : 0;
  5475. pause->tx_pause =
  5476. (hw->tx_cfg & DMA_TX_FLOW_ENABLE) ? 1 : 0;
  5477. } else {
  5478. pause->rx_pause =
  5479. (sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
  5480. SWITCH_RX_FLOW_CTRL)) ? 1 : 0;
  5481. pause->tx_pause =
  5482. (sw_chk(hw, KS8842_SWITCH_CTRL_1_OFFSET,
  5483. SWITCH_TX_FLOW_CTRL)) ? 1 : 0;
  5484. }
  5485. }
  5486. /**
  5487. * netdev_set_pauseparam - set flow control parameters
  5488. * @dev: Network device.
  5489. * @pause: Ethtool PAUSE settings data structure.
  5490. *
  5491. * This function sets the PAUSE control flow settings.
  5492. * Not implemented yet.
  5493. *
  5494. * Return 0 if successful; otherwise an error code.
  5495. */
  5496. static int netdev_set_pauseparam(struct net_device *dev,
  5497. struct ethtool_pauseparam *pause)
  5498. {
  5499. struct dev_priv *priv = netdev_priv(dev);
  5500. struct dev_info *hw_priv = priv->adapter;
  5501. struct ksz_hw *hw = &hw_priv->hw;
  5502. struct ksz_port *port = &priv->port;
  5503. mutex_lock(&hw_priv->lock);
  5504. if (pause->autoneg) {
  5505. if (!pause->rx_pause && !pause->tx_pause)
  5506. port->flow_ctrl = PHY_NO_FLOW_CTRL;
  5507. else
  5508. port->flow_ctrl = PHY_FLOW_CTRL;
  5509. hw->overrides &= ~PAUSE_FLOW_CTRL;
  5510. port->force_link = 0;
  5511. if (hw->ksz_switch) {
  5512. sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
  5513. SWITCH_RX_FLOW_CTRL, 1);
  5514. sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
  5515. SWITCH_TX_FLOW_CTRL, 1);
  5516. }
  5517. port_set_link_speed(port);
  5518. } else {
  5519. hw->overrides |= PAUSE_FLOW_CTRL;
  5520. if (hw->ksz_switch) {
  5521. sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
  5522. SWITCH_RX_FLOW_CTRL, pause->rx_pause);
  5523. sw_cfg(hw, KS8842_SWITCH_CTRL_1_OFFSET,
  5524. SWITCH_TX_FLOW_CTRL, pause->tx_pause);
  5525. } else
  5526. set_flow_ctrl(hw, pause->rx_pause, pause->tx_pause);
  5527. }
  5528. mutex_unlock(&hw_priv->lock);
  5529. return 0;
  5530. }
  5531. /**
  5532. * netdev_get_ringparam - get tx/rx ring parameters
  5533. * @dev: Network device.
  5534. * @pause: Ethtool RING settings data structure.
  5535. *
  5536. * This procedure returns the TX/RX ring settings.
  5537. */
  5538. static void netdev_get_ringparam(struct net_device *dev,
  5539. struct ethtool_ringparam *ring)
  5540. {
  5541. struct dev_priv *priv = netdev_priv(dev);
  5542. struct dev_info *hw_priv = priv->adapter;
  5543. struct ksz_hw *hw = &hw_priv->hw;
  5544. ring->tx_max_pending = (1 << 9);
  5545. ring->tx_pending = hw->tx_desc_info.alloc;
  5546. ring->rx_max_pending = (1 << 9);
  5547. ring->rx_pending = hw->rx_desc_info.alloc;
  5548. }
  5549. #define STATS_LEN (TOTAL_PORT_COUNTER_NUM)
  5550. static struct {
  5551. char string[ETH_GSTRING_LEN];
  5552. } ethtool_stats_keys[STATS_LEN] = {
  5553. { "rx_lo_priority_octets" },
  5554. { "rx_hi_priority_octets" },
  5555. { "rx_undersize_packets" },
  5556. { "rx_fragments" },
  5557. { "rx_oversize_packets" },
  5558. { "rx_jabbers" },
  5559. { "rx_symbol_errors" },
  5560. { "rx_crc_errors" },
  5561. { "rx_align_errors" },
  5562. { "rx_mac_ctrl_packets" },
  5563. { "rx_pause_packets" },
  5564. { "rx_bcast_packets" },
  5565. { "rx_mcast_packets" },
  5566. { "rx_ucast_packets" },
  5567. { "rx_64_or_less_octet_packets" },
  5568. { "rx_65_to_127_octet_packets" },
  5569. { "rx_128_to_255_octet_packets" },
  5570. { "rx_256_to_511_octet_packets" },
  5571. { "rx_512_to_1023_octet_packets" },
  5572. { "rx_1024_to_1522_octet_packets" },
  5573. { "tx_lo_priority_octets" },
  5574. { "tx_hi_priority_octets" },
  5575. { "tx_late_collisions" },
  5576. { "tx_pause_packets" },
  5577. { "tx_bcast_packets" },
  5578. { "tx_mcast_packets" },
  5579. { "tx_ucast_packets" },
  5580. { "tx_deferred" },
  5581. { "tx_total_collisions" },
  5582. { "tx_excessive_collisions" },
  5583. { "tx_single_collisions" },
  5584. { "tx_mult_collisions" },
  5585. { "rx_discards" },
  5586. { "tx_discards" },
  5587. };
  5588. /**
  5589. * netdev_get_strings - get statistics identity strings
  5590. * @dev: Network device.
  5591. * @stringset: String set identifier.
  5592. * @buf: Buffer to store the strings.
  5593. *
  5594. * This procedure returns the strings used to identify the statistics.
  5595. */
  5596. static void netdev_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  5597. {
  5598. struct dev_priv *priv = netdev_priv(dev);
  5599. struct dev_info *hw_priv = priv->adapter;
  5600. struct ksz_hw *hw = &hw_priv->hw;
  5601. if (ETH_SS_STATS == stringset)
  5602. memcpy(buf, &ethtool_stats_keys,
  5603. ETH_GSTRING_LEN * hw->mib_cnt);
  5604. }
  5605. /**
  5606. * netdev_get_sset_count - get statistics size
  5607. * @dev: Network device.
  5608. * @sset: The statistics set number.
  5609. *
  5610. * This function returns the size of the statistics to be reported.
  5611. *
  5612. * Return size of the statistics to be reported.
  5613. */
  5614. static int netdev_get_sset_count(struct net_device *dev, int sset)
  5615. {
  5616. struct dev_priv *priv = netdev_priv(dev);
  5617. struct dev_info *hw_priv = priv->adapter;
  5618. struct ksz_hw *hw = &hw_priv->hw;
  5619. switch (sset) {
  5620. case ETH_SS_STATS:
  5621. return hw->mib_cnt;
  5622. default:
  5623. return -EOPNOTSUPP;
  5624. }
  5625. }
  5626. /**
  5627. * netdev_get_ethtool_stats - get network device statistics
  5628. * @dev: Network device.
  5629. * @stats: Ethtool statistics data structure.
  5630. * @data: Buffer to store the statistics.
  5631. *
  5632. * This procedure returns the statistics.
  5633. */
  5634. static void netdev_get_ethtool_stats(struct net_device *dev,
  5635. struct ethtool_stats *stats, u64 *data)
  5636. {
  5637. struct dev_priv *priv = netdev_priv(dev);
  5638. struct dev_info *hw_priv = priv->adapter;
  5639. struct ksz_hw *hw = &hw_priv->hw;
  5640. struct ksz_port *port = &priv->port;
  5641. int n_stats = stats->n_stats;
  5642. int i;
  5643. int n;
  5644. int p;
  5645. int rc;
  5646. u64 counter[TOTAL_PORT_COUNTER_NUM];
  5647. mutex_lock(&hw_priv->lock);
  5648. n = SWITCH_PORT_NUM;
  5649. for (i = 0, p = port->first_port; i < port->mib_port_cnt; i++, p++) {
  5650. if (media_connected == hw->port_mib[p].state) {
  5651. hw_priv->counter[p].read = 1;
  5652. /* Remember first port that requests read. */
  5653. if (n == SWITCH_PORT_NUM)
  5654. n = p;
  5655. }
  5656. }
  5657. mutex_unlock(&hw_priv->lock);
  5658. if (n < SWITCH_PORT_NUM)
  5659. schedule_work(&hw_priv->mib_read);
  5660. if (1 == port->mib_port_cnt && n < SWITCH_PORT_NUM) {
  5661. p = n;
  5662. rc = wait_event_interruptible_timeout(
  5663. hw_priv->counter[p].counter,
  5664. 2 == hw_priv->counter[p].read,
  5665. HZ * 1);
  5666. } else
  5667. for (i = 0, p = n; i < port->mib_port_cnt - n; i++, p++) {
  5668. if (0 == i) {
  5669. rc = wait_event_interruptible_timeout(
  5670. hw_priv->counter[p].counter,
  5671. 2 == hw_priv->counter[p].read,
  5672. HZ * 2);
  5673. } else if (hw->port_mib[p].cnt_ptr) {
  5674. rc = wait_event_interruptible_timeout(
  5675. hw_priv->counter[p].counter,
  5676. 2 == hw_priv->counter[p].read,
  5677. HZ * 1);
  5678. }
  5679. }
  5680. get_mib_counters(hw, port->first_port, port->mib_port_cnt, counter);
  5681. n = hw->mib_cnt;
  5682. if (n > n_stats)
  5683. n = n_stats;
  5684. n_stats -= n;
  5685. for (i = 0; i < n; i++)
  5686. *data++ = counter[i];
  5687. }
  5688. /**
  5689. * netdev_set_features - set receive checksum support
  5690. * @dev: Network device.
  5691. * @features: New device features (offloads).
  5692. *
  5693. * This function sets receive checksum support setting.
  5694. *
  5695. * Return 0 if successful; otherwise an error code.
  5696. */
  5697. static int netdev_set_features(struct net_device *dev, u32 features)
  5698. {
  5699. struct dev_priv *priv = netdev_priv(dev);
  5700. struct dev_info *hw_priv = priv->adapter;
  5701. struct ksz_hw *hw = &hw_priv->hw;
  5702. mutex_lock(&hw_priv->lock);
  5703. /* see note in hw_setup() */
  5704. if (features & NETIF_F_RXCSUM)
  5705. hw->rx_cfg |= DMA_RX_CSUM_TCP | DMA_RX_CSUM_IP;
  5706. else
  5707. hw->rx_cfg &= ~(DMA_RX_CSUM_TCP | DMA_RX_CSUM_IP);
  5708. if (hw->enabled)
  5709. writel(hw->rx_cfg, hw->io + KS_DMA_RX_CTRL);
  5710. mutex_unlock(&hw_priv->lock);
  5711. return 0;
  5712. }
  5713. static struct ethtool_ops netdev_ethtool_ops = {
  5714. .get_settings = netdev_get_settings,
  5715. .set_settings = netdev_set_settings,
  5716. .nway_reset = netdev_nway_reset,
  5717. .get_link = netdev_get_link,
  5718. .get_drvinfo = netdev_get_drvinfo,
  5719. .get_regs_len = netdev_get_regs_len,
  5720. .get_regs = netdev_get_regs,
  5721. .get_wol = netdev_get_wol,
  5722. .set_wol = netdev_set_wol,
  5723. .get_msglevel = netdev_get_msglevel,
  5724. .set_msglevel = netdev_set_msglevel,
  5725. .get_eeprom_len = netdev_get_eeprom_len,
  5726. .get_eeprom = netdev_get_eeprom,
  5727. .set_eeprom = netdev_set_eeprom,
  5728. .get_pauseparam = netdev_get_pauseparam,
  5729. .set_pauseparam = netdev_set_pauseparam,
  5730. .get_ringparam = netdev_get_ringparam,
  5731. .get_strings = netdev_get_strings,
  5732. .get_sset_count = netdev_get_sset_count,
  5733. .get_ethtool_stats = netdev_get_ethtool_stats,
  5734. };
  5735. /*
  5736. * Hardware monitoring
  5737. */
  5738. static void update_link(struct net_device *dev, struct dev_priv *priv,
  5739. struct ksz_port *port)
  5740. {
  5741. if (priv->media_state != port->linked->state) {
  5742. priv->media_state = port->linked->state;
  5743. if (netif_running(dev))
  5744. set_media_state(dev, media_connected);
  5745. }
  5746. }
  5747. static void mib_read_work(struct work_struct *work)
  5748. {
  5749. struct dev_info *hw_priv =
  5750. container_of(work, struct dev_info, mib_read);
  5751. struct ksz_hw *hw = &hw_priv->hw;
  5752. struct ksz_port_mib *mib;
  5753. int i;
  5754. next_jiffies = jiffies;
  5755. for (i = 0; i < hw->mib_port_cnt; i++) {
  5756. mib = &hw->port_mib[i];
  5757. /* Reading MIB counters or requested to read. */
  5758. if (mib->cnt_ptr || 1 == hw_priv->counter[i].read) {
  5759. /* Need to process receive interrupt. */
  5760. if (port_r_cnt(hw, i))
  5761. break;
  5762. hw_priv->counter[i].read = 0;
  5763. /* Finish reading counters. */
  5764. if (0 == mib->cnt_ptr) {
  5765. hw_priv->counter[i].read = 2;
  5766. wake_up_interruptible(
  5767. &hw_priv->counter[i].counter);
  5768. }
  5769. } else if (jiffies >= hw_priv->counter[i].time) {
  5770. /* Only read MIB counters when the port is connected. */
  5771. if (media_connected == mib->state)
  5772. hw_priv->counter[i].read = 1;
  5773. next_jiffies += HZ * 1 * hw->mib_port_cnt;
  5774. hw_priv->counter[i].time = next_jiffies;
  5775. /* Port is just disconnected. */
  5776. } else if (mib->link_down) {
  5777. mib->link_down = 0;
  5778. /* Read counters one last time after link is lost. */
  5779. hw_priv->counter[i].read = 1;
  5780. }
  5781. }
  5782. }
  5783. static void mib_monitor(unsigned long ptr)
  5784. {
  5785. struct dev_info *hw_priv = (struct dev_info *) ptr;
  5786. mib_read_work(&hw_priv->mib_read);
  5787. /* This is used to verify Wake-on-LAN is working. */
  5788. if (hw_priv->pme_wait) {
  5789. if (hw_priv->pme_wait <= jiffies) {
  5790. hw_clr_wol_pme_status(&hw_priv->hw);
  5791. hw_priv->pme_wait = 0;
  5792. }
  5793. } else if (hw_chk_wol_pme_status(&hw_priv->hw)) {
  5794. /* PME is asserted. Wait 2 seconds to clear it. */
  5795. hw_priv->pme_wait = jiffies + HZ * 2;
  5796. }
  5797. ksz_update_timer(&hw_priv->mib_timer_info);
  5798. }
  5799. /**
  5800. * dev_monitor - periodic monitoring
  5801. * @ptr: Network device pointer.
  5802. *
  5803. * This routine is run in a kernel timer to monitor the network device.
  5804. */
  5805. static void dev_monitor(unsigned long ptr)
  5806. {
  5807. struct net_device *dev = (struct net_device *) ptr;
  5808. struct dev_priv *priv = netdev_priv(dev);
  5809. struct dev_info *hw_priv = priv->adapter;
  5810. struct ksz_hw *hw = &hw_priv->hw;
  5811. struct ksz_port *port = &priv->port;
  5812. if (!(hw->features & LINK_INT_WORKING))
  5813. port_get_link_speed(port);
  5814. update_link(dev, priv, port);
  5815. ksz_update_timer(&priv->monitor_timer_info);
  5816. }
  5817. /*
  5818. * Linux network device interface functions
  5819. */
  5820. /* Driver exported variables */
  5821. static int msg_enable;
  5822. static char *macaddr = ":";
  5823. static char *mac1addr = ":";
  5824. /*
  5825. * This enables multiple network device mode for KSZ8842, which contains a
  5826. * switch with two physical ports. Some users like to take control of the
  5827. * ports for running Spanning Tree Protocol. The driver will create an
  5828. * additional eth? device for the other port.
  5829. *
  5830. * Some limitations are the network devices cannot have different MTU and
  5831. * multicast hash tables.
  5832. */
  5833. static int multi_dev;
  5834. /*
  5835. * As most users select multiple network device mode to use Spanning Tree
  5836. * Protocol, this enables a feature in which most unicast and multicast packets
  5837. * are forwarded inside the switch and not passed to the host. Only packets
  5838. * that need the host's attention are passed to it. This prevents the host
  5839. * wasting CPU time to examine each and every incoming packets and do the
  5840. * forwarding itself.
  5841. *
  5842. * As the hack requires the private bridge header, the driver cannot compile
  5843. * with just the kernel headers.
  5844. *
  5845. * Enabling STP support also turns on multiple network device mode.
  5846. */
  5847. static int stp;
  5848. /*
  5849. * This enables fast aging in the KSZ8842 switch. Not sure what situation
  5850. * needs that. However, fast aging is used to flush the dynamic MAC table when
  5851. * STP suport is enabled.
  5852. */
  5853. static int fast_aging;
  5854. /**
  5855. * netdev_init - initialize network device.
  5856. * @dev: Network device.
  5857. *
  5858. * This function initializes the network device.
  5859. *
  5860. * Return 0 if successful; otherwise an error code indicating failure.
  5861. */
  5862. static int __init netdev_init(struct net_device *dev)
  5863. {
  5864. struct dev_priv *priv = netdev_priv(dev);
  5865. /* 500 ms timeout */
  5866. ksz_init_timer(&priv->monitor_timer_info, 500 * HZ / 1000,
  5867. dev_monitor, dev);
  5868. /* 500 ms timeout */
  5869. dev->watchdog_timeo = HZ / 2;
  5870. dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG | NETIF_F_RXCSUM;
  5871. /*
  5872. * Hardware does not really support IPv6 checksum generation, but
  5873. * driver actually runs faster with this on.
  5874. */
  5875. dev->hw_features |= NETIF_F_IPV6_CSUM;
  5876. dev->features |= dev->hw_features;
  5877. sema_init(&priv->proc_sem, 1);
  5878. priv->mii_if.phy_id_mask = 0x1;
  5879. priv->mii_if.reg_num_mask = 0x7;
  5880. priv->mii_if.dev = dev;
  5881. priv->mii_if.mdio_read = mdio_read;
  5882. priv->mii_if.mdio_write = mdio_write;
  5883. priv->mii_if.phy_id = priv->port.first_port + 1;
  5884. priv->msg_enable = netif_msg_init(msg_enable,
  5885. (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK));
  5886. return 0;
  5887. }
  5888. static const struct net_device_ops netdev_ops = {
  5889. .ndo_init = netdev_init,
  5890. .ndo_open = netdev_open,
  5891. .ndo_stop = netdev_close,
  5892. .ndo_get_stats = netdev_query_statistics,
  5893. .ndo_start_xmit = netdev_tx,
  5894. .ndo_tx_timeout = netdev_tx_timeout,
  5895. .ndo_change_mtu = netdev_change_mtu,
  5896. .ndo_set_features = netdev_set_features,
  5897. .ndo_set_mac_address = netdev_set_mac_address,
  5898. .ndo_validate_addr = eth_validate_addr,
  5899. .ndo_do_ioctl = netdev_ioctl,
  5900. .ndo_set_rx_mode = netdev_set_rx_mode,
  5901. #ifdef CONFIG_NET_POLL_CONTROLLER
  5902. .ndo_poll_controller = netdev_netpoll,
  5903. #endif
  5904. };
  5905. static void netdev_free(struct net_device *dev)
  5906. {
  5907. if (dev->watchdog_timeo)
  5908. unregister_netdev(dev);
  5909. free_netdev(dev);
  5910. }
  5911. struct platform_info {
  5912. struct dev_info dev_info;
  5913. struct net_device *netdev[SWITCH_PORT_NUM];
  5914. };
  5915. static int net_device_present;
  5916. static void get_mac_addr(struct dev_info *hw_priv, u8 *macaddr, int port)
  5917. {
  5918. int i;
  5919. int j;
  5920. int got_num;
  5921. int num;
  5922. i = j = num = got_num = 0;
  5923. while (j < MAC_ADDR_LEN) {
  5924. if (macaddr[i]) {
  5925. int digit;
  5926. got_num = 1;
  5927. digit = hex_to_bin(macaddr[i]);
  5928. if (digit >= 0)
  5929. num = num * 16 + digit;
  5930. else if (':' == macaddr[i])
  5931. got_num = 2;
  5932. else
  5933. break;
  5934. } else if (got_num)
  5935. got_num = 2;
  5936. else
  5937. break;
  5938. if (2 == got_num) {
  5939. if (MAIN_PORT == port) {
  5940. hw_priv->hw.override_addr[j++] = (u8) num;
  5941. hw_priv->hw.override_addr[5] +=
  5942. hw_priv->hw.id;
  5943. } else {
  5944. hw_priv->hw.ksz_switch->other_addr[j++] =
  5945. (u8) num;
  5946. hw_priv->hw.ksz_switch->other_addr[5] +=
  5947. hw_priv->hw.id;
  5948. }
  5949. num = got_num = 0;
  5950. }
  5951. i++;
  5952. }
  5953. if (MAC_ADDR_LEN == j) {
  5954. if (MAIN_PORT == port)
  5955. hw_priv->hw.mac_override = 1;
  5956. }
  5957. }
  5958. #define KS884X_DMA_MASK (~0x0UL)
  5959. static void read_other_addr(struct ksz_hw *hw)
  5960. {
  5961. int i;
  5962. u16 data[3];
  5963. struct ksz_switch *sw = hw->ksz_switch;
  5964. for (i = 0; i < 3; i++)
  5965. data[i] = eeprom_read(hw, i + EEPROM_DATA_OTHER_MAC_ADDR);
  5966. if ((data[0] || data[1] || data[2]) && data[0] != 0xffff) {
  5967. sw->other_addr[5] = (u8) data[0];
  5968. sw->other_addr[4] = (u8)(data[0] >> 8);
  5969. sw->other_addr[3] = (u8) data[1];
  5970. sw->other_addr[2] = (u8)(data[1] >> 8);
  5971. sw->other_addr[1] = (u8) data[2];
  5972. sw->other_addr[0] = (u8)(data[2] >> 8);
  5973. }
  5974. }
  5975. #ifndef PCI_VENDOR_ID_MICREL_KS
  5976. #define PCI_VENDOR_ID_MICREL_KS 0x16c6
  5977. #endif
  5978. static int __devinit pcidev_init(struct pci_dev *pdev,
  5979. const struct pci_device_id *id)
  5980. {
  5981. struct net_device *dev;
  5982. struct dev_priv *priv;
  5983. struct dev_info *hw_priv;
  5984. struct ksz_hw *hw;
  5985. struct platform_info *info;
  5986. struct ksz_port *port;
  5987. unsigned long reg_base;
  5988. unsigned long reg_len;
  5989. int cnt;
  5990. int i;
  5991. int mib_port_count;
  5992. int pi;
  5993. int port_count;
  5994. int result;
  5995. char banner[sizeof(version)];
  5996. struct ksz_switch *sw = NULL;
  5997. result = pci_enable_device(pdev);
  5998. if (result)
  5999. return result;
  6000. result = -ENODEV;
  6001. if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) ||
  6002. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
  6003. return result;
  6004. reg_base = pci_resource_start(pdev, 0);
  6005. reg_len = pci_resource_len(pdev, 0);
  6006. if ((pci_resource_flags(pdev, 0) & IORESOURCE_IO) != 0)
  6007. return result;
  6008. if (!request_mem_region(reg_base, reg_len, DRV_NAME))
  6009. return result;
  6010. pci_set_master(pdev);
  6011. result = -ENOMEM;
  6012. info = kzalloc(sizeof(struct platform_info), GFP_KERNEL);
  6013. if (!info)
  6014. goto pcidev_init_dev_err;
  6015. hw_priv = &info->dev_info;
  6016. hw_priv->pdev = pdev;
  6017. hw = &hw_priv->hw;
  6018. hw->io = ioremap(reg_base, reg_len);
  6019. if (!hw->io)
  6020. goto pcidev_init_io_err;
  6021. cnt = hw_init(hw);
  6022. if (!cnt) {
  6023. if (msg_enable & NETIF_MSG_PROBE)
  6024. pr_alert("chip not detected\n");
  6025. result = -ENODEV;
  6026. goto pcidev_init_alloc_err;
  6027. }
  6028. snprintf(banner, sizeof(banner), "%s", version);
  6029. banner[13] = cnt + '0'; /* Replace x in "Micrel KSZ884x" */
  6030. dev_info(&hw_priv->pdev->dev, "%s\n", banner);
  6031. dev_dbg(&hw_priv->pdev->dev, "Mem = %p; IRQ = %d\n", hw->io, pdev->irq);
  6032. /* Assume device is KSZ8841. */
  6033. hw->dev_count = 1;
  6034. port_count = 1;
  6035. mib_port_count = 1;
  6036. hw->addr_list_size = 0;
  6037. hw->mib_cnt = PORT_COUNTER_NUM;
  6038. hw->mib_port_cnt = 1;
  6039. /* KSZ8842 has a switch with multiple ports. */
  6040. if (2 == cnt) {
  6041. if (fast_aging)
  6042. hw->overrides |= FAST_AGING;
  6043. hw->mib_cnt = TOTAL_PORT_COUNTER_NUM;
  6044. /* Multiple network device interfaces are required. */
  6045. if (multi_dev) {
  6046. hw->dev_count = SWITCH_PORT_NUM;
  6047. hw->addr_list_size = SWITCH_PORT_NUM - 1;
  6048. }
  6049. /* Single network device has multiple ports. */
  6050. if (1 == hw->dev_count) {
  6051. port_count = SWITCH_PORT_NUM;
  6052. mib_port_count = SWITCH_PORT_NUM;
  6053. }
  6054. hw->mib_port_cnt = TOTAL_PORT_NUM;
  6055. hw->ksz_switch = kzalloc(sizeof(struct ksz_switch), GFP_KERNEL);
  6056. if (!hw->ksz_switch)
  6057. goto pcidev_init_alloc_err;
  6058. sw = hw->ksz_switch;
  6059. }
  6060. for (i = 0; i < hw->mib_port_cnt; i++)
  6061. hw->port_mib[i].mib_start = 0;
  6062. hw->parent = hw_priv;
  6063. /* Default MTU is 1500. */
  6064. hw_priv->mtu = (REGULAR_RX_BUF_SIZE + 3) & ~3;
  6065. if (ksz_alloc_mem(hw_priv))
  6066. goto pcidev_init_mem_err;
  6067. hw_priv->hw.id = net_device_present;
  6068. spin_lock_init(&hw_priv->hwlock);
  6069. mutex_init(&hw_priv->lock);
  6070. /* tasklet is enabled. */
  6071. tasklet_init(&hw_priv->rx_tasklet, rx_proc_task,
  6072. (unsigned long) hw_priv);
  6073. tasklet_init(&hw_priv->tx_tasklet, tx_proc_task,
  6074. (unsigned long) hw_priv);
  6075. /* tasklet_enable will decrement the atomic counter. */
  6076. tasklet_disable(&hw_priv->rx_tasklet);
  6077. tasklet_disable(&hw_priv->tx_tasklet);
  6078. for (i = 0; i < TOTAL_PORT_NUM; i++)
  6079. init_waitqueue_head(&hw_priv->counter[i].counter);
  6080. if (macaddr[0] != ':')
  6081. get_mac_addr(hw_priv, macaddr, MAIN_PORT);
  6082. /* Read MAC address and initialize override address if not overrided. */
  6083. hw_read_addr(hw);
  6084. /* Multiple device interfaces mode requires a second MAC address. */
  6085. if (hw->dev_count > 1) {
  6086. memcpy(sw->other_addr, hw->override_addr, MAC_ADDR_LEN);
  6087. read_other_addr(hw);
  6088. if (mac1addr[0] != ':')
  6089. get_mac_addr(hw_priv, mac1addr, OTHER_PORT);
  6090. }
  6091. hw_setup(hw);
  6092. if (hw->ksz_switch)
  6093. sw_setup(hw);
  6094. else {
  6095. hw_priv->wol_support = WOL_SUPPORT;
  6096. hw_priv->wol_enable = 0;
  6097. }
  6098. INIT_WORK(&hw_priv->mib_read, mib_read_work);
  6099. /* 500 ms timeout */
  6100. ksz_init_timer(&hw_priv->mib_timer_info, 500 * HZ / 1000,
  6101. mib_monitor, hw_priv);
  6102. for (i = 0; i < hw->dev_count; i++) {
  6103. dev = alloc_etherdev(sizeof(struct dev_priv));
  6104. if (!dev)
  6105. goto pcidev_init_reg_err;
  6106. info->netdev[i] = dev;
  6107. priv = netdev_priv(dev);
  6108. priv->adapter = hw_priv;
  6109. priv->id = net_device_present++;
  6110. port = &priv->port;
  6111. port->port_cnt = port_count;
  6112. port->mib_port_cnt = mib_port_count;
  6113. port->first_port = i;
  6114. port->flow_ctrl = PHY_FLOW_CTRL;
  6115. port->hw = hw;
  6116. port->linked = &hw->port_info[port->first_port];
  6117. for (cnt = 0, pi = i; cnt < port_count; cnt++, pi++) {
  6118. hw->port_info[pi].port_id = pi;
  6119. hw->port_info[pi].pdev = dev;
  6120. hw->port_info[pi].state = media_disconnected;
  6121. }
  6122. dev->mem_start = (unsigned long) hw->io;
  6123. dev->mem_end = dev->mem_start + reg_len - 1;
  6124. dev->irq = pdev->irq;
  6125. if (MAIN_PORT == i)
  6126. memcpy(dev->dev_addr, hw_priv->hw.override_addr,
  6127. MAC_ADDR_LEN);
  6128. else {
  6129. memcpy(dev->dev_addr, sw->other_addr,
  6130. MAC_ADDR_LEN);
  6131. if (!memcmp(sw->other_addr, hw->override_addr,
  6132. MAC_ADDR_LEN))
  6133. dev->dev_addr[5] += port->first_port;
  6134. }
  6135. dev->netdev_ops = &netdev_ops;
  6136. SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
  6137. if (register_netdev(dev))
  6138. goto pcidev_init_reg_err;
  6139. port_set_power_saving(port, true);
  6140. }
  6141. pci_dev_get(hw_priv->pdev);
  6142. pci_set_drvdata(pdev, info);
  6143. return 0;
  6144. pcidev_init_reg_err:
  6145. for (i = 0; i < hw->dev_count; i++) {
  6146. if (info->netdev[i]) {
  6147. netdev_free(info->netdev[i]);
  6148. info->netdev[i] = NULL;
  6149. }
  6150. }
  6151. pcidev_init_mem_err:
  6152. ksz_free_mem(hw_priv);
  6153. kfree(hw->ksz_switch);
  6154. pcidev_init_alloc_err:
  6155. iounmap(hw->io);
  6156. pcidev_init_io_err:
  6157. kfree(info);
  6158. pcidev_init_dev_err:
  6159. release_mem_region(reg_base, reg_len);
  6160. return result;
  6161. }
  6162. static void pcidev_exit(struct pci_dev *pdev)
  6163. {
  6164. int i;
  6165. struct platform_info *info = pci_get_drvdata(pdev);
  6166. struct dev_info *hw_priv = &info->dev_info;
  6167. pci_set_drvdata(pdev, NULL);
  6168. release_mem_region(pci_resource_start(pdev, 0),
  6169. pci_resource_len(pdev, 0));
  6170. for (i = 0; i < hw_priv->hw.dev_count; i++) {
  6171. if (info->netdev[i])
  6172. netdev_free(info->netdev[i]);
  6173. }
  6174. if (hw_priv->hw.io)
  6175. iounmap(hw_priv->hw.io);
  6176. ksz_free_mem(hw_priv);
  6177. kfree(hw_priv->hw.ksz_switch);
  6178. pci_dev_put(hw_priv->pdev);
  6179. kfree(info);
  6180. }
  6181. #ifdef CONFIG_PM
  6182. static int pcidev_resume(struct pci_dev *pdev)
  6183. {
  6184. int i;
  6185. struct platform_info *info = pci_get_drvdata(pdev);
  6186. struct dev_info *hw_priv = &info->dev_info;
  6187. struct ksz_hw *hw = &hw_priv->hw;
  6188. pci_set_power_state(pdev, PCI_D0);
  6189. pci_restore_state(pdev);
  6190. pci_enable_wake(pdev, PCI_D0, 0);
  6191. if (hw_priv->wol_enable)
  6192. hw_cfg_wol_pme(hw, 0);
  6193. for (i = 0; i < hw->dev_count; i++) {
  6194. if (info->netdev[i]) {
  6195. struct net_device *dev = info->netdev[i];
  6196. if (netif_running(dev)) {
  6197. netdev_open(dev);
  6198. netif_device_attach(dev);
  6199. }
  6200. }
  6201. }
  6202. return 0;
  6203. }
  6204. static int pcidev_suspend(struct pci_dev *pdev, pm_message_t state)
  6205. {
  6206. int i;
  6207. struct platform_info *info = pci_get_drvdata(pdev);
  6208. struct dev_info *hw_priv = &info->dev_info;
  6209. struct ksz_hw *hw = &hw_priv->hw;
  6210. /* Need to find a way to retrieve the device IP address. */
  6211. static const u8 net_addr[] = { 192, 168, 1, 1 };
  6212. for (i = 0; i < hw->dev_count; i++) {
  6213. if (info->netdev[i]) {
  6214. struct net_device *dev = info->netdev[i];
  6215. if (netif_running(dev)) {
  6216. netif_device_detach(dev);
  6217. netdev_close(dev);
  6218. }
  6219. }
  6220. }
  6221. if (hw_priv->wol_enable) {
  6222. hw_enable_wol(hw, hw_priv->wol_enable, net_addr);
  6223. hw_cfg_wol_pme(hw, 1);
  6224. }
  6225. pci_save_state(pdev);
  6226. pci_enable_wake(pdev, pci_choose_state(pdev, state), 1);
  6227. pci_set_power_state(pdev, pci_choose_state(pdev, state));
  6228. return 0;
  6229. }
  6230. #endif
  6231. static char pcidev_name[] = "ksz884xp";
  6232. static struct pci_device_id pcidev_table[] = {
  6233. { PCI_VENDOR_ID_MICREL_KS, 0x8841,
  6234. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  6235. { PCI_VENDOR_ID_MICREL_KS, 0x8842,
  6236. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  6237. { 0 }
  6238. };
  6239. MODULE_DEVICE_TABLE(pci, pcidev_table);
  6240. static struct pci_driver pci_device_driver = {
  6241. #ifdef CONFIG_PM
  6242. .suspend = pcidev_suspend,
  6243. .resume = pcidev_resume,
  6244. #endif
  6245. .name = pcidev_name,
  6246. .id_table = pcidev_table,
  6247. .probe = pcidev_init,
  6248. .remove = pcidev_exit
  6249. };
  6250. static int __init ksz884x_init_module(void)
  6251. {
  6252. return pci_register_driver(&pci_device_driver);
  6253. }
  6254. static void __exit ksz884x_cleanup_module(void)
  6255. {
  6256. pci_unregister_driver(&pci_device_driver);
  6257. }
  6258. module_init(ksz884x_init_module);
  6259. module_exit(ksz884x_cleanup_module);
  6260. MODULE_DESCRIPTION("KSZ8841/2 PCI network driver");
  6261. MODULE_AUTHOR("Tristram Ha <Tristram.Ha@micrel.com>");
  6262. MODULE_LICENSE("GPL");
  6263. module_param_named(message, msg_enable, int, 0);
  6264. MODULE_PARM_DESC(message, "Message verbosity level (0=none, 31=all)");
  6265. module_param(macaddr, charp, 0);
  6266. module_param(mac1addr, charp, 0);
  6267. module_param(fast_aging, int, 0);
  6268. module_param(multi_dev, int, 0);
  6269. module_param(stp, int, 0);
  6270. MODULE_PARM_DESC(macaddr, "MAC address");
  6271. MODULE_PARM_DESC(mac1addr, "Second MAC address");
  6272. MODULE_PARM_DESC(fast_aging, "Fast aging");
  6273. MODULE_PARM_DESC(multi_dev, "Multiple device interfaces");
  6274. MODULE_PARM_DESC(stp, "STP support");