ksz884x.c 181 KB

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