ksz884x.c 181 KB

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