ksz884x.c 180 KB

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