ksz884x.c 181 KB

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