ksz884x.c 179 KB

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