niu.c 230 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259
  1. /* niu.c: Neptune ethernet driver.
  2. *
  3. * Copyright (C) 2007, 2008 David S. Miller (davem@davemloft.net)
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/pci.h>
  9. #include <linux/dma-mapping.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/delay.h>
  15. #include <linux/bitops.h>
  16. #include <linux/mii.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/if_vlan.h>
  19. #include <linux/ip.h>
  20. #include <linux/in.h>
  21. #include <linux/ipv6.h>
  22. #include <linux/log2.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/crc32.h>
  25. #include <linux/list.h>
  26. #include <linux/slab.h>
  27. #include <linux/io.h>
  28. #include <linux/of_device.h>
  29. #include "niu.h"
  30. #define DRV_MODULE_NAME "niu"
  31. #define DRV_MODULE_VERSION "1.1"
  32. #define DRV_MODULE_RELDATE "Apr 22, 2010"
  33. static char version[] __devinitdata =
  34. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  35. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  36. MODULE_DESCRIPTION("NIU ethernet driver");
  37. MODULE_LICENSE("GPL");
  38. MODULE_VERSION(DRV_MODULE_VERSION);
  39. #ifndef readq
  40. static u64 readq(void __iomem *reg)
  41. {
  42. return ((u64) readl(reg)) | (((u64) readl(reg + 4UL)) << 32);
  43. }
  44. static void writeq(u64 val, void __iomem *reg)
  45. {
  46. writel(val & 0xffffffff, reg);
  47. writel(val >> 32, reg + 0x4UL);
  48. }
  49. #endif
  50. static DEFINE_PCI_DEVICE_TABLE(niu_pci_tbl) = {
  51. {PCI_DEVICE(PCI_VENDOR_ID_SUN, 0xabcd)},
  52. {}
  53. };
  54. MODULE_DEVICE_TABLE(pci, niu_pci_tbl);
  55. #define NIU_TX_TIMEOUT (5 * HZ)
  56. #define nr64(reg) readq(np->regs + (reg))
  57. #define nw64(reg, val) writeq((val), np->regs + (reg))
  58. #define nr64_mac(reg) readq(np->mac_regs + (reg))
  59. #define nw64_mac(reg, val) writeq((val), np->mac_regs + (reg))
  60. #define nr64_ipp(reg) readq(np->regs + np->ipp_off + (reg))
  61. #define nw64_ipp(reg, val) writeq((val), np->regs + np->ipp_off + (reg))
  62. #define nr64_pcs(reg) readq(np->regs + np->pcs_off + (reg))
  63. #define nw64_pcs(reg, val) writeq((val), np->regs + np->pcs_off + (reg))
  64. #define nr64_xpcs(reg) readq(np->regs + np->xpcs_off + (reg))
  65. #define nw64_xpcs(reg, val) writeq((val), np->regs + np->xpcs_off + (reg))
  66. #define NIU_MSG_DEFAULT (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
  67. static int niu_debug;
  68. static int debug = -1;
  69. module_param(debug, int, 0);
  70. MODULE_PARM_DESC(debug, "NIU debug level");
  71. #define niu_lock_parent(np, flags) \
  72. spin_lock_irqsave(&np->parent->lock, flags)
  73. #define niu_unlock_parent(np, flags) \
  74. spin_unlock_irqrestore(&np->parent->lock, flags)
  75. static int serdes_init_10g_serdes(struct niu *np);
  76. static int __niu_wait_bits_clear_mac(struct niu *np, unsigned long reg,
  77. u64 bits, int limit, int delay)
  78. {
  79. while (--limit >= 0) {
  80. u64 val = nr64_mac(reg);
  81. if (!(val & bits))
  82. break;
  83. udelay(delay);
  84. }
  85. if (limit < 0)
  86. return -ENODEV;
  87. return 0;
  88. }
  89. static int __niu_set_and_wait_clear_mac(struct niu *np, unsigned long reg,
  90. u64 bits, int limit, int delay,
  91. const char *reg_name)
  92. {
  93. int err;
  94. nw64_mac(reg, bits);
  95. err = __niu_wait_bits_clear_mac(np, reg, bits, limit, delay);
  96. if (err)
  97. netdev_err(np->dev, "bits (%llx) of register %s would not clear, val[%llx]\n",
  98. (unsigned long long)bits, reg_name,
  99. (unsigned long long)nr64_mac(reg));
  100. return err;
  101. }
  102. #define niu_set_and_wait_clear_mac(NP, REG, BITS, LIMIT, DELAY, REG_NAME) \
  103. ({ BUILD_BUG_ON(LIMIT <= 0 || DELAY < 0); \
  104. __niu_set_and_wait_clear_mac(NP, REG, BITS, LIMIT, DELAY, REG_NAME); \
  105. })
  106. static int __niu_wait_bits_clear_ipp(struct niu *np, unsigned long reg,
  107. u64 bits, int limit, int delay)
  108. {
  109. while (--limit >= 0) {
  110. u64 val = nr64_ipp(reg);
  111. if (!(val & bits))
  112. break;
  113. udelay(delay);
  114. }
  115. if (limit < 0)
  116. return -ENODEV;
  117. return 0;
  118. }
  119. static int __niu_set_and_wait_clear_ipp(struct niu *np, unsigned long reg,
  120. u64 bits, int limit, int delay,
  121. const char *reg_name)
  122. {
  123. int err;
  124. u64 val;
  125. val = nr64_ipp(reg);
  126. val |= bits;
  127. nw64_ipp(reg, val);
  128. err = __niu_wait_bits_clear_ipp(np, reg, bits, limit, delay);
  129. if (err)
  130. netdev_err(np->dev, "bits (%llx) of register %s would not clear, val[%llx]\n",
  131. (unsigned long long)bits, reg_name,
  132. (unsigned long long)nr64_ipp(reg));
  133. return err;
  134. }
  135. #define niu_set_and_wait_clear_ipp(NP, REG, BITS, LIMIT, DELAY, REG_NAME) \
  136. ({ BUILD_BUG_ON(LIMIT <= 0 || DELAY < 0); \
  137. __niu_set_and_wait_clear_ipp(NP, REG, BITS, LIMIT, DELAY, REG_NAME); \
  138. })
  139. static int __niu_wait_bits_clear(struct niu *np, unsigned long reg,
  140. u64 bits, int limit, int delay)
  141. {
  142. while (--limit >= 0) {
  143. u64 val = nr64(reg);
  144. if (!(val & bits))
  145. break;
  146. udelay(delay);
  147. }
  148. if (limit < 0)
  149. return -ENODEV;
  150. return 0;
  151. }
  152. #define niu_wait_bits_clear(NP, REG, BITS, LIMIT, DELAY) \
  153. ({ BUILD_BUG_ON(LIMIT <= 0 || DELAY < 0); \
  154. __niu_wait_bits_clear(NP, REG, BITS, LIMIT, DELAY); \
  155. })
  156. static int __niu_set_and_wait_clear(struct niu *np, unsigned long reg,
  157. u64 bits, int limit, int delay,
  158. const char *reg_name)
  159. {
  160. int err;
  161. nw64(reg, bits);
  162. err = __niu_wait_bits_clear(np, reg, bits, limit, delay);
  163. if (err)
  164. netdev_err(np->dev, "bits (%llx) of register %s would not clear, val[%llx]\n",
  165. (unsigned long long)bits, reg_name,
  166. (unsigned long long)nr64(reg));
  167. return err;
  168. }
  169. #define niu_set_and_wait_clear(NP, REG, BITS, LIMIT, DELAY, REG_NAME) \
  170. ({ BUILD_BUG_ON(LIMIT <= 0 || DELAY < 0); \
  171. __niu_set_and_wait_clear(NP, REG, BITS, LIMIT, DELAY, REG_NAME); \
  172. })
  173. static void niu_ldg_rearm(struct niu *np, struct niu_ldg *lp, int on)
  174. {
  175. u64 val = (u64) lp->timer;
  176. if (on)
  177. val |= LDG_IMGMT_ARM;
  178. nw64(LDG_IMGMT(lp->ldg_num), val);
  179. }
  180. static int niu_ldn_irq_enable(struct niu *np, int ldn, int on)
  181. {
  182. unsigned long mask_reg, bits;
  183. u64 val;
  184. if (ldn < 0 || ldn > LDN_MAX)
  185. return -EINVAL;
  186. if (ldn < 64) {
  187. mask_reg = LD_IM0(ldn);
  188. bits = LD_IM0_MASK;
  189. } else {
  190. mask_reg = LD_IM1(ldn - 64);
  191. bits = LD_IM1_MASK;
  192. }
  193. val = nr64(mask_reg);
  194. if (on)
  195. val &= ~bits;
  196. else
  197. val |= bits;
  198. nw64(mask_reg, val);
  199. return 0;
  200. }
  201. static int niu_enable_ldn_in_ldg(struct niu *np, struct niu_ldg *lp, int on)
  202. {
  203. struct niu_parent *parent = np->parent;
  204. int i;
  205. for (i = 0; i <= LDN_MAX; i++) {
  206. int err;
  207. if (parent->ldg_map[i] != lp->ldg_num)
  208. continue;
  209. err = niu_ldn_irq_enable(np, i, on);
  210. if (err)
  211. return err;
  212. }
  213. return 0;
  214. }
  215. static int niu_enable_interrupts(struct niu *np, int on)
  216. {
  217. int i;
  218. for (i = 0; i < np->num_ldg; i++) {
  219. struct niu_ldg *lp = &np->ldg[i];
  220. int err;
  221. err = niu_enable_ldn_in_ldg(np, lp, on);
  222. if (err)
  223. return err;
  224. }
  225. for (i = 0; i < np->num_ldg; i++)
  226. niu_ldg_rearm(np, &np->ldg[i], on);
  227. return 0;
  228. }
  229. static u32 phy_encode(u32 type, int port)
  230. {
  231. return type << (port * 2);
  232. }
  233. static u32 phy_decode(u32 val, int port)
  234. {
  235. return (val >> (port * 2)) & PORT_TYPE_MASK;
  236. }
  237. static int mdio_wait(struct niu *np)
  238. {
  239. int limit = 1000;
  240. u64 val;
  241. while (--limit > 0) {
  242. val = nr64(MIF_FRAME_OUTPUT);
  243. if ((val >> MIF_FRAME_OUTPUT_TA_SHIFT) & 0x1)
  244. return val & MIF_FRAME_OUTPUT_DATA;
  245. udelay(10);
  246. }
  247. return -ENODEV;
  248. }
  249. static int mdio_read(struct niu *np, int port, int dev, int reg)
  250. {
  251. int err;
  252. nw64(MIF_FRAME_OUTPUT, MDIO_ADDR_OP(port, dev, reg));
  253. err = mdio_wait(np);
  254. if (err < 0)
  255. return err;
  256. nw64(MIF_FRAME_OUTPUT, MDIO_READ_OP(port, dev));
  257. return mdio_wait(np);
  258. }
  259. static int mdio_write(struct niu *np, int port, int dev, int reg, int data)
  260. {
  261. int err;
  262. nw64(MIF_FRAME_OUTPUT, MDIO_ADDR_OP(port, dev, reg));
  263. err = mdio_wait(np);
  264. if (err < 0)
  265. return err;
  266. nw64(MIF_FRAME_OUTPUT, MDIO_WRITE_OP(port, dev, data));
  267. err = mdio_wait(np);
  268. if (err < 0)
  269. return err;
  270. return 0;
  271. }
  272. static int mii_read(struct niu *np, int port, int reg)
  273. {
  274. nw64(MIF_FRAME_OUTPUT, MII_READ_OP(port, reg));
  275. return mdio_wait(np);
  276. }
  277. static int mii_write(struct niu *np, int port, int reg, int data)
  278. {
  279. int err;
  280. nw64(MIF_FRAME_OUTPUT, MII_WRITE_OP(port, reg, data));
  281. err = mdio_wait(np);
  282. if (err < 0)
  283. return err;
  284. return 0;
  285. }
  286. static int esr2_set_tx_cfg(struct niu *np, unsigned long channel, u32 val)
  287. {
  288. int err;
  289. err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  290. ESR2_TI_PLL_TX_CFG_L(channel),
  291. val & 0xffff);
  292. if (!err)
  293. err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  294. ESR2_TI_PLL_TX_CFG_H(channel),
  295. val >> 16);
  296. return err;
  297. }
  298. static int esr2_set_rx_cfg(struct niu *np, unsigned long channel, u32 val)
  299. {
  300. int err;
  301. err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  302. ESR2_TI_PLL_RX_CFG_L(channel),
  303. val & 0xffff);
  304. if (!err)
  305. err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  306. ESR2_TI_PLL_RX_CFG_H(channel),
  307. val >> 16);
  308. return err;
  309. }
  310. /* Mode is always 10G fiber. */
  311. static int serdes_init_niu_10g_fiber(struct niu *np)
  312. {
  313. struct niu_link_config *lp = &np->link_config;
  314. u32 tx_cfg, rx_cfg;
  315. unsigned long i;
  316. tx_cfg = (PLL_TX_CFG_ENTX | PLL_TX_CFG_SWING_1375MV);
  317. rx_cfg = (PLL_RX_CFG_ENRX | PLL_RX_CFG_TERM_0P8VDDT |
  318. PLL_RX_CFG_ALIGN_ENA | PLL_RX_CFG_LOS_LTHRESH |
  319. PLL_RX_CFG_EQ_LP_ADAPTIVE);
  320. if (lp->loopback_mode == LOOPBACK_PHY) {
  321. u16 test_cfg = PLL_TEST_CFG_LOOPBACK_CML_DIS;
  322. mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  323. ESR2_TI_PLL_TEST_CFG_L, test_cfg);
  324. tx_cfg |= PLL_TX_CFG_ENTEST;
  325. rx_cfg |= PLL_RX_CFG_ENTEST;
  326. }
  327. /* Initialize all 4 lanes of the SERDES. */
  328. for (i = 0; i < 4; i++) {
  329. int err = esr2_set_tx_cfg(np, i, tx_cfg);
  330. if (err)
  331. return err;
  332. }
  333. for (i = 0; i < 4; i++) {
  334. int err = esr2_set_rx_cfg(np, i, rx_cfg);
  335. if (err)
  336. return err;
  337. }
  338. return 0;
  339. }
  340. static int serdes_init_niu_1g_serdes(struct niu *np)
  341. {
  342. struct niu_link_config *lp = &np->link_config;
  343. u16 pll_cfg, pll_sts;
  344. int max_retry = 100;
  345. u64 uninitialized_var(sig), mask, val;
  346. u32 tx_cfg, rx_cfg;
  347. unsigned long i;
  348. int err;
  349. tx_cfg = (PLL_TX_CFG_ENTX | PLL_TX_CFG_SWING_1375MV |
  350. PLL_TX_CFG_RATE_HALF);
  351. rx_cfg = (PLL_RX_CFG_ENRX | PLL_RX_CFG_TERM_0P8VDDT |
  352. PLL_RX_CFG_ALIGN_ENA | PLL_RX_CFG_LOS_LTHRESH |
  353. PLL_RX_CFG_RATE_HALF);
  354. if (np->port == 0)
  355. rx_cfg |= PLL_RX_CFG_EQ_LP_ADAPTIVE;
  356. if (lp->loopback_mode == LOOPBACK_PHY) {
  357. u16 test_cfg = PLL_TEST_CFG_LOOPBACK_CML_DIS;
  358. mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  359. ESR2_TI_PLL_TEST_CFG_L, test_cfg);
  360. tx_cfg |= PLL_TX_CFG_ENTEST;
  361. rx_cfg |= PLL_RX_CFG_ENTEST;
  362. }
  363. /* Initialize PLL for 1G */
  364. pll_cfg = (PLL_CFG_ENPLL | PLL_CFG_MPY_8X);
  365. err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  366. ESR2_TI_PLL_CFG_L, pll_cfg);
  367. if (err) {
  368. netdev_err(np->dev, "NIU Port %d %s() mdio write to ESR2_TI_PLL_CFG_L failed\n",
  369. np->port, __func__);
  370. return err;
  371. }
  372. pll_sts = PLL_CFG_ENPLL;
  373. err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  374. ESR2_TI_PLL_STS_L, pll_sts);
  375. if (err) {
  376. netdev_err(np->dev, "NIU Port %d %s() mdio write to ESR2_TI_PLL_STS_L failed\n",
  377. np->port, __func__);
  378. return err;
  379. }
  380. udelay(200);
  381. /* Initialize all 4 lanes of the SERDES. */
  382. for (i = 0; i < 4; i++) {
  383. err = esr2_set_tx_cfg(np, i, tx_cfg);
  384. if (err)
  385. return err;
  386. }
  387. for (i = 0; i < 4; i++) {
  388. err = esr2_set_rx_cfg(np, i, rx_cfg);
  389. if (err)
  390. return err;
  391. }
  392. switch (np->port) {
  393. case 0:
  394. val = (ESR_INT_SRDY0_P0 | ESR_INT_DET0_P0);
  395. mask = val;
  396. break;
  397. case 1:
  398. val = (ESR_INT_SRDY0_P1 | ESR_INT_DET0_P1);
  399. mask = val;
  400. break;
  401. default:
  402. return -EINVAL;
  403. }
  404. while (max_retry--) {
  405. sig = nr64(ESR_INT_SIGNALS);
  406. if ((sig & mask) == val)
  407. break;
  408. mdelay(500);
  409. }
  410. if ((sig & mask) != val) {
  411. netdev_err(np->dev, "Port %u signal bits [%08x] are not [%08x]\n",
  412. np->port, (int)(sig & mask), (int)val);
  413. return -ENODEV;
  414. }
  415. return 0;
  416. }
  417. static int serdes_init_niu_10g_serdes(struct niu *np)
  418. {
  419. struct niu_link_config *lp = &np->link_config;
  420. u32 tx_cfg, rx_cfg, pll_cfg, pll_sts;
  421. int max_retry = 100;
  422. u64 uninitialized_var(sig), mask, val;
  423. unsigned long i;
  424. int err;
  425. tx_cfg = (PLL_TX_CFG_ENTX | PLL_TX_CFG_SWING_1375MV);
  426. rx_cfg = (PLL_RX_CFG_ENRX | PLL_RX_CFG_TERM_0P8VDDT |
  427. PLL_RX_CFG_ALIGN_ENA | PLL_RX_CFG_LOS_LTHRESH |
  428. PLL_RX_CFG_EQ_LP_ADAPTIVE);
  429. if (lp->loopback_mode == LOOPBACK_PHY) {
  430. u16 test_cfg = PLL_TEST_CFG_LOOPBACK_CML_DIS;
  431. mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  432. ESR2_TI_PLL_TEST_CFG_L, test_cfg);
  433. tx_cfg |= PLL_TX_CFG_ENTEST;
  434. rx_cfg |= PLL_RX_CFG_ENTEST;
  435. }
  436. /* Initialize PLL for 10G */
  437. pll_cfg = (PLL_CFG_ENPLL | PLL_CFG_MPY_10X);
  438. err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  439. ESR2_TI_PLL_CFG_L, pll_cfg & 0xffff);
  440. if (err) {
  441. netdev_err(np->dev, "NIU Port %d %s() mdio write to ESR2_TI_PLL_CFG_L failed\n",
  442. np->port, __func__);
  443. return err;
  444. }
  445. pll_sts = PLL_CFG_ENPLL;
  446. err = mdio_write(np, np->port, NIU_ESR2_DEV_ADDR,
  447. ESR2_TI_PLL_STS_L, pll_sts & 0xffff);
  448. if (err) {
  449. netdev_err(np->dev, "NIU Port %d %s() mdio write to ESR2_TI_PLL_STS_L failed\n",
  450. np->port, __func__);
  451. return err;
  452. }
  453. udelay(200);
  454. /* Initialize all 4 lanes of the SERDES. */
  455. for (i = 0; i < 4; i++) {
  456. err = esr2_set_tx_cfg(np, i, tx_cfg);
  457. if (err)
  458. return err;
  459. }
  460. for (i = 0; i < 4; i++) {
  461. err = esr2_set_rx_cfg(np, i, rx_cfg);
  462. if (err)
  463. return err;
  464. }
  465. /* check if serdes is ready */
  466. switch (np->port) {
  467. case 0:
  468. mask = ESR_INT_SIGNALS_P0_BITS;
  469. val = (ESR_INT_SRDY0_P0 |
  470. ESR_INT_DET0_P0 |
  471. ESR_INT_XSRDY_P0 |
  472. ESR_INT_XDP_P0_CH3 |
  473. ESR_INT_XDP_P0_CH2 |
  474. ESR_INT_XDP_P0_CH1 |
  475. ESR_INT_XDP_P0_CH0);
  476. break;
  477. case 1:
  478. mask = ESR_INT_SIGNALS_P1_BITS;
  479. val = (ESR_INT_SRDY0_P1 |
  480. ESR_INT_DET0_P1 |
  481. ESR_INT_XSRDY_P1 |
  482. ESR_INT_XDP_P1_CH3 |
  483. ESR_INT_XDP_P1_CH2 |
  484. ESR_INT_XDP_P1_CH1 |
  485. ESR_INT_XDP_P1_CH0);
  486. break;
  487. default:
  488. return -EINVAL;
  489. }
  490. while (max_retry--) {
  491. sig = nr64(ESR_INT_SIGNALS);
  492. if ((sig & mask) == val)
  493. break;
  494. mdelay(500);
  495. }
  496. if ((sig & mask) != val) {
  497. pr_info("NIU Port %u signal bits [%08x] are not [%08x] for 10G...trying 1G\n",
  498. np->port, (int)(sig & mask), (int)val);
  499. /* 10G failed, try initializing at 1G */
  500. err = serdes_init_niu_1g_serdes(np);
  501. if (!err) {
  502. np->flags &= ~NIU_FLAGS_10G;
  503. np->mac_xcvr = MAC_XCVR_PCS;
  504. } else {
  505. netdev_err(np->dev, "Port %u 10G/1G SERDES Link Failed\n",
  506. np->port);
  507. return -ENODEV;
  508. }
  509. }
  510. return 0;
  511. }
  512. static int esr_read_rxtx_ctrl(struct niu *np, unsigned long chan, u32 *val)
  513. {
  514. int err;
  515. err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR, ESR_RXTX_CTRL_L(chan));
  516. if (err >= 0) {
  517. *val = (err & 0xffff);
  518. err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
  519. ESR_RXTX_CTRL_H(chan));
  520. if (err >= 0)
  521. *val |= ((err & 0xffff) << 16);
  522. err = 0;
  523. }
  524. return err;
  525. }
  526. static int esr_read_glue0(struct niu *np, unsigned long chan, u32 *val)
  527. {
  528. int err;
  529. err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
  530. ESR_GLUE_CTRL0_L(chan));
  531. if (err >= 0) {
  532. *val = (err & 0xffff);
  533. err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
  534. ESR_GLUE_CTRL0_H(chan));
  535. if (err >= 0) {
  536. *val |= ((err & 0xffff) << 16);
  537. err = 0;
  538. }
  539. }
  540. return err;
  541. }
  542. static int esr_read_reset(struct niu *np, u32 *val)
  543. {
  544. int err;
  545. err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
  546. ESR_RXTX_RESET_CTRL_L);
  547. if (err >= 0) {
  548. *val = (err & 0xffff);
  549. err = mdio_read(np, np->port, NIU_ESR_DEV_ADDR,
  550. ESR_RXTX_RESET_CTRL_H);
  551. if (err >= 0) {
  552. *val |= ((err & 0xffff) << 16);
  553. err = 0;
  554. }
  555. }
  556. return err;
  557. }
  558. static int esr_write_rxtx_ctrl(struct niu *np, unsigned long chan, u32 val)
  559. {
  560. int err;
  561. err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
  562. ESR_RXTX_CTRL_L(chan), val & 0xffff);
  563. if (!err)
  564. err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
  565. ESR_RXTX_CTRL_H(chan), (val >> 16));
  566. return err;
  567. }
  568. static int esr_write_glue0(struct niu *np, unsigned long chan, u32 val)
  569. {
  570. int err;
  571. err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
  572. ESR_GLUE_CTRL0_L(chan), val & 0xffff);
  573. if (!err)
  574. err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
  575. ESR_GLUE_CTRL0_H(chan), (val >> 16));
  576. return err;
  577. }
  578. static int esr_reset(struct niu *np)
  579. {
  580. u32 uninitialized_var(reset);
  581. int err;
  582. err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
  583. ESR_RXTX_RESET_CTRL_L, 0x0000);
  584. if (err)
  585. return err;
  586. err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
  587. ESR_RXTX_RESET_CTRL_H, 0xffff);
  588. if (err)
  589. return err;
  590. udelay(200);
  591. err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
  592. ESR_RXTX_RESET_CTRL_L, 0xffff);
  593. if (err)
  594. return err;
  595. udelay(200);
  596. err = mdio_write(np, np->port, NIU_ESR_DEV_ADDR,
  597. ESR_RXTX_RESET_CTRL_H, 0x0000);
  598. if (err)
  599. return err;
  600. udelay(200);
  601. err = esr_read_reset(np, &reset);
  602. if (err)
  603. return err;
  604. if (reset != 0) {
  605. netdev_err(np->dev, "Port %u ESR_RESET did not clear [%08x]\n",
  606. np->port, reset);
  607. return -ENODEV;
  608. }
  609. return 0;
  610. }
  611. static int serdes_init_10g(struct niu *np)
  612. {
  613. struct niu_link_config *lp = &np->link_config;
  614. unsigned long ctrl_reg, test_cfg_reg, i;
  615. u64 ctrl_val, test_cfg_val, sig, mask, val;
  616. int err;
  617. switch (np->port) {
  618. case 0:
  619. ctrl_reg = ENET_SERDES_0_CTRL_CFG;
  620. test_cfg_reg = ENET_SERDES_0_TEST_CFG;
  621. break;
  622. case 1:
  623. ctrl_reg = ENET_SERDES_1_CTRL_CFG;
  624. test_cfg_reg = ENET_SERDES_1_TEST_CFG;
  625. break;
  626. default:
  627. return -EINVAL;
  628. }
  629. ctrl_val = (ENET_SERDES_CTRL_SDET_0 |
  630. ENET_SERDES_CTRL_SDET_1 |
  631. ENET_SERDES_CTRL_SDET_2 |
  632. ENET_SERDES_CTRL_SDET_3 |
  633. (0x5 << ENET_SERDES_CTRL_EMPH_0_SHIFT) |
  634. (0x5 << ENET_SERDES_CTRL_EMPH_1_SHIFT) |
  635. (0x5 << ENET_SERDES_CTRL_EMPH_2_SHIFT) |
  636. (0x5 << ENET_SERDES_CTRL_EMPH_3_SHIFT) |
  637. (0x1 << ENET_SERDES_CTRL_LADJ_0_SHIFT) |
  638. (0x1 << ENET_SERDES_CTRL_LADJ_1_SHIFT) |
  639. (0x1 << ENET_SERDES_CTRL_LADJ_2_SHIFT) |
  640. (0x1 << ENET_SERDES_CTRL_LADJ_3_SHIFT));
  641. test_cfg_val = 0;
  642. if (lp->loopback_mode == LOOPBACK_PHY) {
  643. test_cfg_val |= ((ENET_TEST_MD_PAD_LOOPBACK <<
  644. ENET_SERDES_TEST_MD_0_SHIFT) |
  645. (ENET_TEST_MD_PAD_LOOPBACK <<
  646. ENET_SERDES_TEST_MD_1_SHIFT) |
  647. (ENET_TEST_MD_PAD_LOOPBACK <<
  648. ENET_SERDES_TEST_MD_2_SHIFT) |
  649. (ENET_TEST_MD_PAD_LOOPBACK <<
  650. ENET_SERDES_TEST_MD_3_SHIFT));
  651. }
  652. nw64(ctrl_reg, ctrl_val);
  653. nw64(test_cfg_reg, test_cfg_val);
  654. /* Initialize all 4 lanes of the SERDES. */
  655. for (i = 0; i < 4; i++) {
  656. u32 rxtx_ctrl, glue0;
  657. err = esr_read_rxtx_ctrl(np, i, &rxtx_ctrl);
  658. if (err)
  659. return err;
  660. err = esr_read_glue0(np, i, &glue0);
  661. if (err)
  662. return err;
  663. rxtx_ctrl &= ~(ESR_RXTX_CTRL_VMUXLO);
  664. rxtx_ctrl |= (ESR_RXTX_CTRL_ENSTRETCH |
  665. (2 << ESR_RXTX_CTRL_VMUXLO_SHIFT));
  666. glue0 &= ~(ESR_GLUE_CTRL0_SRATE |
  667. ESR_GLUE_CTRL0_THCNT |
  668. ESR_GLUE_CTRL0_BLTIME);
  669. glue0 |= (ESR_GLUE_CTRL0_RXLOSENAB |
  670. (0xf << ESR_GLUE_CTRL0_SRATE_SHIFT) |
  671. (0xff << ESR_GLUE_CTRL0_THCNT_SHIFT) |
  672. (BLTIME_300_CYCLES <<
  673. ESR_GLUE_CTRL0_BLTIME_SHIFT));
  674. err = esr_write_rxtx_ctrl(np, i, rxtx_ctrl);
  675. if (err)
  676. return err;
  677. err = esr_write_glue0(np, i, glue0);
  678. if (err)
  679. return err;
  680. }
  681. err = esr_reset(np);
  682. if (err)
  683. return err;
  684. sig = nr64(ESR_INT_SIGNALS);
  685. switch (np->port) {
  686. case 0:
  687. mask = ESR_INT_SIGNALS_P0_BITS;
  688. val = (ESR_INT_SRDY0_P0 |
  689. ESR_INT_DET0_P0 |
  690. ESR_INT_XSRDY_P0 |
  691. ESR_INT_XDP_P0_CH3 |
  692. ESR_INT_XDP_P0_CH2 |
  693. ESR_INT_XDP_P0_CH1 |
  694. ESR_INT_XDP_P0_CH0);
  695. break;
  696. case 1:
  697. mask = ESR_INT_SIGNALS_P1_BITS;
  698. val = (ESR_INT_SRDY0_P1 |
  699. ESR_INT_DET0_P1 |
  700. ESR_INT_XSRDY_P1 |
  701. ESR_INT_XDP_P1_CH3 |
  702. ESR_INT_XDP_P1_CH2 |
  703. ESR_INT_XDP_P1_CH1 |
  704. ESR_INT_XDP_P1_CH0);
  705. break;
  706. default:
  707. return -EINVAL;
  708. }
  709. if ((sig & mask) != val) {
  710. if (np->flags & NIU_FLAGS_HOTPLUG_PHY) {
  711. np->flags &= ~NIU_FLAGS_HOTPLUG_PHY_PRESENT;
  712. return 0;
  713. }
  714. netdev_err(np->dev, "Port %u signal bits [%08x] are not [%08x]\n",
  715. np->port, (int)(sig & mask), (int)val);
  716. return -ENODEV;
  717. }
  718. if (np->flags & NIU_FLAGS_HOTPLUG_PHY)
  719. np->flags |= NIU_FLAGS_HOTPLUG_PHY_PRESENT;
  720. return 0;
  721. }
  722. static int serdes_init_1g(struct niu *np)
  723. {
  724. u64 val;
  725. val = nr64(ENET_SERDES_1_PLL_CFG);
  726. val &= ~ENET_SERDES_PLL_FBDIV2;
  727. switch (np->port) {
  728. case 0:
  729. val |= ENET_SERDES_PLL_HRATE0;
  730. break;
  731. case 1:
  732. val |= ENET_SERDES_PLL_HRATE1;
  733. break;
  734. case 2:
  735. val |= ENET_SERDES_PLL_HRATE2;
  736. break;
  737. case 3:
  738. val |= ENET_SERDES_PLL_HRATE3;
  739. break;
  740. default:
  741. return -EINVAL;
  742. }
  743. nw64(ENET_SERDES_1_PLL_CFG, val);
  744. return 0;
  745. }
  746. static int serdes_init_1g_serdes(struct niu *np)
  747. {
  748. struct niu_link_config *lp = &np->link_config;
  749. unsigned long ctrl_reg, test_cfg_reg, pll_cfg, i;
  750. u64 ctrl_val, test_cfg_val, sig, mask, val;
  751. int err;
  752. u64 reset_val, val_rd;
  753. val = ENET_SERDES_PLL_HRATE0 | ENET_SERDES_PLL_HRATE1 |
  754. ENET_SERDES_PLL_HRATE2 | ENET_SERDES_PLL_HRATE3 |
  755. ENET_SERDES_PLL_FBDIV0;
  756. switch (np->port) {
  757. case 0:
  758. reset_val = ENET_SERDES_RESET_0;
  759. ctrl_reg = ENET_SERDES_0_CTRL_CFG;
  760. test_cfg_reg = ENET_SERDES_0_TEST_CFG;
  761. pll_cfg = ENET_SERDES_0_PLL_CFG;
  762. break;
  763. case 1:
  764. reset_val = ENET_SERDES_RESET_1;
  765. ctrl_reg = ENET_SERDES_1_CTRL_CFG;
  766. test_cfg_reg = ENET_SERDES_1_TEST_CFG;
  767. pll_cfg = ENET_SERDES_1_PLL_CFG;
  768. break;
  769. default:
  770. return -EINVAL;
  771. }
  772. ctrl_val = (ENET_SERDES_CTRL_SDET_0 |
  773. ENET_SERDES_CTRL_SDET_1 |
  774. ENET_SERDES_CTRL_SDET_2 |
  775. ENET_SERDES_CTRL_SDET_3 |
  776. (0x5 << ENET_SERDES_CTRL_EMPH_0_SHIFT) |
  777. (0x5 << ENET_SERDES_CTRL_EMPH_1_SHIFT) |
  778. (0x5 << ENET_SERDES_CTRL_EMPH_2_SHIFT) |
  779. (0x5 << ENET_SERDES_CTRL_EMPH_3_SHIFT) |
  780. (0x1 << ENET_SERDES_CTRL_LADJ_0_SHIFT) |
  781. (0x1 << ENET_SERDES_CTRL_LADJ_1_SHIFT) |
  782. (0x1 << ENET_SERDES_CTRL_LADJ_2_SHIFT) |
  783. (0x1 << ENET_SERDES_CTRL_LADJ_3_SHIFT));
  784. test_cfg_val = 0;
  785. if (lp->loopback_mode == LOOPBACK_PHY) {
  786. test_cfg_val |= ((ENET_TEST_MD_PAD_LOOPBACK <<
  787. ENET_SERDES_TEST_MD_0_SHIFT) |
  788. (ENET_TEST_MD_PAD_LOOPBACK <<
  789. ENET_SERDES_TEST_MD_1_SHIFT) |
  790. (ENET_TEST_MD_PAD_LOOPBACK <<
  791. ENET_SERDES_TEST_MD_2_SHIFT) |
  792. (ENET_TEST_MD_PAD_LOOPBACK <<
  793. ENET_SERDES_TEST_MD_3_SHIFT));
  794. }
  795. nw64(ENET_SERDES_RESET, reset_val);
  796. mdelay(20);
  797. val_rd = nr64(ENET_SERDES_RESET);
  798. val_rd &= ~reset_val;
  799. nw64(pll_cfg, val);
  800. nw64(ctrl_reg, ctrl_val);
  801. nw64(test_cfg_reg, test_cfg_val);
  802. nw64(ENET_SERDES_RESET, val_rd);
  803. mdelay(2000);
  804. /* Initialize all 4 lanes of the SERDES. */
  805. for (i = 0; i < 4; i++) {
  806. u32 rxtx_ctrl, glue0;
  807. err = esr_read_rxtx_ctrl(np, i, &rxtx_ctrl);
  808. if (err)
  809. return err;
  810. err = esr_read_glue0(np, i, &glue0);
  811. if (err)
  812. return err;
  813. rxtx_ctrl &= ~(ESR_RXTX_CTRL_VMUXLO);
  814. rxtx_ctrl |= (ESR_RXTX_CTRL_ENSTRETCH |
  815. (2 << ESR_RXTX_CTRL_VMUXLO_SHIFT));
  816. glue0 &= ~(ESR_GLUE_CTRL0_SRATE |
  817. ESR_GLUE_CTRL0_THCNT |
  818. ESR_GLUE_CTRL0_BLTIME);
  819. glue0 |= (ESR_GLUE_CTRL0_RXLOSENAB |
  820. (0xf << ESR_GLUE_CTRL0_SRATE_SHIFT) |
  821. (0xff << ESR_GLUE_CTRL0_THCNT_SHIFT) |
  822. (BLTIME_300_CYCLES <<
  823. ESR_GLUE_CTRL0_BLTIME_SHIFT));
  824. err = esr_write_rxtx_ctrl(np, i, rxtx_ctrl);
  825. if (err)
  826. return err;
  827. err = esr_write_glue0(np, i, glue0);
  828. if (err)
  829. return err;
  830. }
  831. sig = nr64(ESR_INT_SIGNALS);
  832. switch (np->port) {
  833. case 0:
  834. val = (ESR_INT_SRDY0_P0 | ESR_INT_DET0_P0);
  835. mask = val;
  836. break;
  837. case 1:
  838. val = (ESR_INT_SRDY0_P1 | ESR_INT_DET0_P1);
  839. mask = val;
  840. break;
  841. default:
  842. return -EINVAL;
  843. }
  844. if ((sig & mask) != val) {
  845. netdev_err(np->dev, "Port %u signal bits [%08x] are not [%08x]\n",
  846. np->port, (int)(sig & mask), (int)val);
  847. return -ENODEV;
  848. }
  849. return 0;
  850. }
  851. static int link_status_1g_serdes(struct niu *np, int *link_up_p)
  852. {
  853. struct niu_link_config *lp = &np->link_config;
  854. int link_up;
  855. u64 val;
  856. u16 current_speed;
  857. unsigned long flags;
  858. u8 current_duplex;
  859. link_up = 0;
  860. current_speed = SPEED_INVALID;
  861. current_duplex = DUPLEX_INVALID;
  862. spin_lock_irqsave(&np->lock, flags);
  863. val = nr64_pcs(PCS_MII_STAT);
  864. if (val & PCS_MII_STAT_LINK_STATUS) {
  865. link_up = 1;
  866. current_speed = SPEED_1000;
  867. current_duplex = DUPLEX_FULL;
  868. }
  869. lp->active_speed = current_speed;
  870. lp->active_duplex = current_duplex;
  871. spin_unlock_irqrestore(&np->lock, flags);
  872. *link_up_p = link_up;
  873. return 0;
  874. }
  875. static int link_status_10g_serdes(struct niu *np, int *link_up_p)
  876. {
  877. unsigned long flags;
  878. struct niu_link_config *lp = &np->link_config;
  879. int link_up = 0;
  880. int link_ok = 1;
  881. u64 val, val2;
  882. u16 current_speed;
  883. u8 current_duplex;
  884. if (!(np->flags & NIU_FLAGS_10G))
  885. return link_status_1g_serdes(np, link_up_p);
  886. current_speed = SPEED_INVALID;
  887. current_duplex = DUPLEX_INVALID;
  888. spin_lock_irqsave(&np->lock, flags);
  889. val = nr64_xpcs(XPCS_STATUS(0));
  890. val2 = nr64_mac(XMAC_INTER2);
  891. if (val2 & 0x01000000)
  892. link_ok = 0;
  893. if ((val & 0x1000ULL) && link_ok) {
  894. link_up = 1;
  895. current_speed = SPEED_10000;
  896. current_duplex = DUPLEX_FULL;
  897. }
  898. lp->active_speed = current_speed;
  899. lp->active_duplex = current_duplex;
  900. spin_unlock_irqrestore(&np->lock, flags);
  901. *link_up_p = link_up;
  902. return 0;
  903. }
  904. static int link_status_mii(struct niu *np, int *link_up_p)
  905. {
  906. struct niu_link_config *lp = &np->link_config;
  907. int err;
  908. int bmsr, advert, ctrl1000, stat1000, lpa, bmcr, estatus;
  909. int supported, advertising, active_speed, active_duplex;
  910. err = mii_read(np, np->phy_addr, MII_BMCR);
  911. if (unlikely(err < 0))
  912. return err;
  913. bmcr = err;
  914. err = mii_read(np, np->phy_addr, MII_BMSR);
  915. if (unlikely(err < 0))
  916. return err;
  917. bmsr = err;
  918. err = mii_read(np, np->phy_addr, MII_ADVERTISE);
  919. if (unlikely(err < 0))
  920. return err;
  921. advert = err;
  922. err = mii_read(np, np->phy_addr, MII_LPA);
  923. if (unlikely(err < 0))
  924. return err;
  925. lpa = err;
  926. if (likely(bmsr & BMSR_ESTATEN)) {
  927. err = mii_read(np, np->phy_addr, MII_ESTATUS);
  928. if (unlikely(err < 0))
  929. return err;
  930. estatus = err;
  931. err = mii_read(np, np->phy_addr, MII_CTRL1000);
  932. if (unlikely(err < 0))
  933. return err;
  934. ctrl1000 = err;
  935. err = mii_read(np, np->phy_addr, MII_STAT1000);
  936. if (unlikely(err < 0))
  937. return err;
  938. stat1000 = err;
  939. } else
  940. estatus = ctrl1000 = stat1000 = 0;
  941. supported = 0;
  942. if (bmsr & BMSR_ANEGCAPABLE)
  943. supported |= SUPPORTED_Autoneg;
  944. if (bmsr & BMSR_10HALF)
  945. supported |= SUPPORTED_10baseT_Half;
  946. if (bmsr & BMSR_10FULL)
  947. supported |= SUPPORTED_10baseT_Full;
  948. if (bmsr & BMSR_100HALF)
  949. supported |= SUPPORTED_100baseT_Half;
  950. if (bmsr & BMSR_100FULL)
  951. supported |= SUPPORTED_100baseT_Full;
  952. if (estatus & ESTATUS_1000_THALF)
  953. supported |= SUPPORTED_1000baseT_Half;
  954. if (estatus & ESTATUS_1000_TFULL)
  955. supported |= SUPPORTED_1000baseT_Full;
  956. lp->supported = supported;
  957. advertising = 0;
  958. if (advert & ADVERTISE_10HALF)
  959. advertising |= ADVERTISED_10baseT_Half;
  960. if (advert & ADVERTISE_10FULL)
  961. advertising |= ADVERTISED_10baseT_Full;
  962. if (advert & ADVERTISE_100HALF)
  963. advertising |= ADVERTISED_100baseT_Half;
  964. if (advert & ADVERTISE_100FULL)
  965. advertising |= ADVERTISED_100baseT_Full;
  966. if (ctrl1000 & ADVERTISE_1000HALF)
  967. advertising |= ADVERTISED_1000baseT_Half;
  968. if (ctrl1000 & ADVERTISE_1000FULL)
  969. advertising |= ADVERTISED_1000baseT_Full;
  970. if (bmcr & BMCR_ANENABLE) {
  971. int neg, neg1000;
  972. lp->active_autoneg = 1;
  973. advertising |= ADVERTISED_Autoneg;
  974. neg = advert & lpa;
  975. neg1000 = (ctrl1000 << 2) & stat1000;
  976. if (neg1000 & (LPA_1000FULL | LPA_1000HALF))
  977. active_speed = SPEED_1000;
  978. else if (neg & LPA_100)
  979. active_speed = SPEED_100;
  980. else if (neg & (LPA_10HALF | LPA_10FULL))
  981. active_speed = SPEED_10;
  982. else
  983. active_speed = SPEED_INVALID;
  984. if ((neg1000 & LPA_1000FULL) || (neg & LPA_DUPLEX))
  985. active_duplex = DUPLEX_FULL;
  986. else if (active_speed != SPEED_INVALID)
  987. active_duplex = DUPLEX_HALF;
  988. else
  989. active_duplex = DUPLEX_INVALID;
  990. } else {
  991. lp->active_autoneg = 0;
  992. if ((bmcr & BMCR_SPEED1000) && !(bmcr & BMCR_SPEED100))
  993. active_speed = SPEED_1000;
  994. else if (bmcr & BMCR_SPEED100)
  995. active_speed = SPEED_100;
  996. else
  997. active_speed = SPEED_10;
  998. if (bmcr & BMCR_FULLDPLX)
  999. active_duplex = DUPLEX_FULL;
  1000. else
  1001. active_duplex = DUPLEX_HALF;
  1002. }
  1003. lp->active_advertising = advertising;
  1004. lp->active_speed = active_speed;
  1005. lp->active_duplex = active_duplex;
  1006. *link_up_p = !!(bmsr & BMSR_LSTATUS);
  1007. return 0;
  1008. }
  1009. static int link_status_1g_rgmii(struct niu *np, int *link_up_p)
  1010. {
  1011. struct niu_link_config *lp = &np->link_config;
  1012. u16 current_speed, bmsr;
  1013. unsigned long flags;
  1014. u8 current_duplex;
  1015. int err, link_up;
  1016. link_up = 0;
  1017. current_speed = SPEED_INVALID;
  1018. current_duplex = DUPLEX_INVALID;
  1019. spin_lock_irqsave(&np->lock, flags);
  1020. err = -EINVAL;
  1021. err = mii_read(np, np->phy_addr, MII_BMSR);
  1022. if (err < 0)
  1023. goto out;
  1024. bmsr = err;
  1025. if (bmsr & BMSR_LSTATUS) {
  1026. u16 adv, lpa;
  1027. err = mii_read(np, np->phy_addr, MII_ADVERTISE);
  1028. if (err < 0)
  1029. goto out;
  1030. adv = err;
  1031. err = mii_read(np, np->phy_addr, MII_LPA);
  1032. if (err < 0)
  1033. goto out;
  1034. lpa = err;
  1035. err = mii_read(np, np->phy_addr, MII_ESTATUS);
  1036. if (err < 0)
  1037. goto out;
  1038. link_up = 1;
  1039. current_speed = SPEED_1000;
  1040. current_duplex = DUPLEX_FULL;
  1041. }
  1042. lp->active_speed = current_speed;
  1043. lp->active_duplex = current_duplex;
  1044. err = 0;
  1045. out:
  1046. spin_unlock_irqrestore(&np->lock, flags);
  1047. *link_up_p = link_up;
  1048. return err;
  1049. }
  1050. static int link_status_1g(struct niu *np, int *link_up_p)
  1051. {
  1052. struct niu_link_config *lp = &np->link_config;
  1053. unsigned long flags;
  1054. int err;
  1055. spin_lock_irqsave(&np->lock, flags);
  1056. err = link_status_mii(np, link_up_p);
  1057. lp->supported |= SUPPORTED_TP;
  1058. lp->active_advertising |= ADVERTISED_TP;
  1059. spin_unlock_irqrestore(&np->lock, flags);
  1060. return err;
  1061. }
  1062. static int bcm8704_reset(struct niu *np)
  1063. {
  1064. int err, limit;
  1065. err = mdio_read(np, np->phy_addr,
  1066. BCM8704_PHYXS_DEV_ADDR, MII_BMCR);
  1067. if (err < 0 || err == 0xffff)
  1068. return err;
  1069. err |= BMCR_RESET;
  1070. err = mdio_write(np, np->phy_addr, BCM8704_PHYXS_DEV_ADDR,
  1071. MII_BMCR, err);
  1072. if (err)
  1073. return err;
  1074. limit = 1000;
  1075. while (--limit >= 0) {
  1076. err = mdio_read(np, np->phy_addr,
  1077. BCM8704_PHYXS_DEV_ADDR, MII_BMCR);
  1078. if (err < 0)
  1079. return err;
  1080. if (!(err & BMCR_RESET))
  1081. break;
  1082. }
  1083. if (limit < 0) {
  1084. netdev_err(np->dev, "Port %u PHY will not reset (bmcr=%04x)\n",
  1085. np->port, (err & 0xffff));
  1086. return -ENODEV;
  1087. }
  1088. return 0;
  1089. }
  1090. /* When written, certain PHY registers need to be read back twice
  1091. * in order for the bits to settle properly.
  1092. */
  1093. static int bcm8704_user_dev3_readback(struct niu *np, int reg)
  1094. {
  1095. int err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR, reg);
  1096. if (err < 0)
  1097. return err;
  1098. err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR, reg);
  1099. if (err < 0)
  1100. return err;
  1101. return 0;
  1102. }
  1103. static int bcm8706_init_user_dev3(struct niu *np)
  1104. {
  1105. int err;
  1106. err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
  1107. BCM8704_USER_OPT_DIGITAL_CTRL);
  1108. if (err < 0)
  1109. return err;
  1110. err &= ~USER_ODIG_CTRL_GPIOS;
  1111. err |= (0x3 << USER_ODIG_CTRL_GPIOS_SHIFT);
  1112. err |= USER_ODIG_CTRL_RESV2;
  1113. err = mdio_write(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
  1114. BCM8704_USER_OPT_DIGITAL_CTRL, err);
  1115. if (err)
  1116. return err;
  1117. mdelay(1000);
  1118. return 0;
  1119. }
  1120. static int bcm8704_init_user_dev3(struct niu *np)
  1121. {
  1122. int err;
  1123. err = mdio_write(np, np->phy_addr,
  1124. BCM8704_USER_DEV3_ADDR, BCM8704_USER_CONTROL,
  1125. (USER_CONTROL_OPTXRST_LVL |
  1126. USER_CONTROL_OPBIASFLT_LVL |
  1127. USER_CONTROL_OBTMPFLT_LVL |
  1128. USER_CONTROL_OPPRFLT_LVL |
  1129. USER_CONTROL_OPTXFLT_LVL |
  1130. USER_CONTROL_OPRXLOS_LVL |
  1131. USER_CONTROL_OPRXFLT_LVL |
  1132. USER_CONTROL_OPTXON_LVL |
  1133. (0x3f << USER_CONTROL_RES1_SHIFT)));
  1134. if (err)
  1135. return err;
  1136. err = mdio_write(np, np->phy_addr,
  1137. BCM8704_USER_DEV3_ADDR, BCM8704_USER_PMD_TX_CONTROL,
  1138. (USER_PMD_TX_CTL_XFP_CLKEN |
  1139. (1 << USER_PMD_TX_CTL_TX_DAC_TXD_SH) |
  1140. (2 << USER_PMD_TX_CTL_TX_DAC_TXCK_SH) |
  1141. USER_PMD_TX_CTL_TSCK_LPWREN));
  1142. if (err)
  1143. return err;
  1144. err = bcm8704_user_dev3_readback(np, BCM8704_USER_CONTROL);
  1145. if (err)
  1146. return err;
  1147. err = bcm8704_user_dev3_readback(np, BCM8704_USER_PMD_TX_CONTROL);
  1148. if (err)
  1149. return err;
  1150. err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
  1151. BCM8704_USER_OPT_DIGITAL_CTRL);
  1152. if (err < 0)
  1153. return err;
  1154. err &= ~USER_ODIG_CTRL_GPIOS;
  1155. err |= (0x3 << USER_ODIG_CTRL_GPIOS_SHIFT);
  1156. err = mdio_write(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
  1157. BCM8704_USER_OPT_DIGITAL_CTRL, err);
  1158. if (err)
  1159. return err;
  1160. mdelay(1000);
  1161. return 0;
  1162. }
  1163. static int mrvl88x2011_act_led(struct niu *np, int val)
  1164. {
  1165. int err;
  1166. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV2_ADDR,
  1167. MRVL88X2011_LED_8_TO_11_CTL);
  1168. if (err < 0)
  1169. return err;
  1170. err &= ~MRVL88X2011_LED(MRVL88X2011_LED_ACT,MRVL88X2011_LED_CTL_MASK);
  1171. err |= MRVL88X2011_LED(MRVL88X2011_LED_ACT,val);
  1172. return mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV2_ADDR,
  1173. MRVL88X2011_LED_8_TO_11_CTL, err);
  1174. }
  1175. static int mrvl88x2011_led_blink_rate(struct niu *np, int rate)
  1176. {
  1177. int err;
  1178. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV2_ADDR,
  1179. MRVL88X2011_LED_BLINK_CTL);
  1180. if (err >= 0) {
  1181. err &= ~MRVL88X2011_LED_BLKRATE_MASK;
  1182. err |= (rate << 4);
  1183. err = mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV2_ADDR,
  1184. MRVL88X2011_LED_BLINK_CTL, err);
  1185. }
  1186. return err;
  1187. }
  1188. static int xcvr_init_10g_mrvl88x2011(struct niu *np)
  1189. {
  1190. int err;
  1191. /* Set LED functions */
  1192. err = mrvl88x2011_led_blink_rate(np, MRVL88X2011_LED_BLKRATE_134MS);
  1193. if (err)
  1194. return err;
  1195. /* led activity */
  1196. err = mrvl88x2011_act_led(np, MRVL88X2011_LED_CTL_OFF);
  1197. if (err)
  1198. return err;
  1199. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV3_ADDR,
  1200. MRVL88X2011_GENERAL_CTL);
  1201. if (err < 0)
  1202. return err;
  1203. err |= MRVL88X2011_ENA_XFPREFCLK;
  1204. err = mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV3_ADDR,
  1205. MRVL88X2011_GENERAL_CTL, err);
  1206. if (err < 0)
  1207. return err;
  1208. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
  1209. MRVL88X2011_PMA_PMD_CTL_1);
  1210. if (err < 0)
  1211. return err;
  1212. if (np->link_config.loopback_mode == LOOPBACK_MAC)
  1213. err |= MRVL88X2011_LOOPBACK;
  1214. else
  1215. err &= ~MRVL88X2011_LOOPBACK;
  1216. err = mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
  1217. MRVL88X2011_PMA_PMD_CTL_1, err);
  1218. if (err < 0)
  1219. return err;
  1220. /* Enable PMD */
  1221. return mdio_write(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
  1222. MRVL88X2011_10G_PMD_TX_DIS, MRVL88X2011_ENA_PMDTX);
  1223. }
  1224. static int xcvr_diag_bcm870x(struct niu *np)
  1225. {
  1226. u16 analog_stat0, tx_alarm_status;
  1227. int err = 0;
  1228. #if 1
  1229. err = mdio_read(np, np->phy_addr, BCM8704_PMA_PMD_DEV_ADDR,
  1230. MII_STAT1000);
  1231. if (err < 0)
  1232. return err;
  1233. pr_info("Port %u PMA_PMD(MII_STAT1000) [%04x]\n", np->port, err);
  1234. err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR, 0x20);
  1235. if (err < 0)
  1236. return err;
  1237. pr_info("Port %u USER_DEV3(0x20) [%04x]\n", np->port, err);
  1238. err = mdio_read(np, np->phy_addr, BCM8704_PHYXS_DEV_ADDR,
  1239. MII_NWAYTEST);
  1240. if (err < 0)
  1241. return err;
  1242. pr_info("Port %u PHYXS(MII_NWAYTEST) [%04x]\n", np->port, err);
  1243. #endif
  1244. /* XXX dig this out it might not be so useful XXX */
  1245. err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
  1246. BCM8704_USER_ANALOG_STATUS0);
  1247. if (err < 0)
  1248. return err;
  1249. err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
  1250. BCM8704_USER_ANALOG_STATUS0);
  1251. if (err < 0)
  1252. return err;
  1253. analog_stat0 = err;
  1254. err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
  1255. BCM8704_USER_TX_ALARM_STATUS);
  1256. if (err < 0)
  1257. return err;
  1258. err = mdio_read(np, np->phy_addr, BCM8704_USER_DEV3_ADDR,
  1259. BCM8704_USER_TX_ALARM_STATUS);
  1260. if (err < 0)
  1261. return err;
  1262. tx_alarm_status = err;
  1263. if (analog_stat0 != 0x03fc) {
  1264. if ((analog_stat0 == 0x43bc) && (tx_alarm_status != 0)) {
  1265. pr_info("Port %u cable not connected or bad cable\n",
  1266. np->port);
  1267. } else if (analog_stat0 == 0x639c) {
  1268. pr_info("Port %u optical module is bad or missing\n",
  1269. np->port);
  1270. }
  1271. }
  1272. return 0;
  1273. }
  1274. static int xcvr_10g_set_lb_bcm870x(struct niu *np)
  1275. {
  1276. struct niu_link_config *lp = &np->link_config;
  1277. int err;
  1278. err = mdio_read(np, np->phy_addr, BCM8704_PCS_DEV_ADDR,
  1279. MII_BMCR);
  1280. if (err < 0)
  1281. return err;
  1282. err &= ~BMCR_LOOPBACK;
  1283. if (lp->loopback_mode == LOOPBACK_MAC)
  1284. err |= BMCR_LOOPBACK;
  1285. err = mdio_write(np, np->phy_addr, BCM8704_PCS_DEV_ADDR,
  1286. MII_BMCR, err);
  1287. if (err)
  1288. return err;
  1289. return 0;
  1290. }
  1291. static int xcvr_init_10g_bcm8706(struct niu *np)
  1292. {
  1293. int err = 0;
  1294. u64 val;
  1295. if ((np->flags & NIU_FLAGS_HOTPLUG_PHY) &&
  1296. (np->flags & NIU_FLAGS_HOTPLUG_PHY_PRESENT) == 0)
  1297. return err;
  1298. val = nr64_mac(XMAC_CONFIG);
  1299. val &= ~XMAC_CONFIG_LED_POLARITY;
  1300. val |= XMAC_CONFIG_FORCE_LED_ON;
  1301. nw64_mac(XMAC_CONFIG, val);
  1302. val = nr64(MIF_CONFIG);
  1303. val |= MIF_CONFIG_INDIRECT_MODE;
  1304. nw64(MIF_CONFIG, val);
  1305. err = bcm8704_reset(np);
  1306. if (err)
  1307. return err;
  1308. err = xcvr_10g_set_lb_bcm870x(np);
  1309. if (err)
  1310. return err;
  1311. err = bcm8706_init_user_dev3(np);
  1312. if (err)
  1313. return err;
  1314. err = xcvr_diag_bcm870x(np);
  1315. if (err)
  1316. return err;
  1317. return 0;
  1318. }
  1319. static int xcvr_init_10g_bcm8704(struct niu *np)
  1320. {
  1321. int err;
  1322. err = bcm8704_reset(np);
  1323. if (err)
  1324. return err;
  1325. err = bcm8704_init_user_dev3(np);
  1326. if (err)
  1327. return err;
  1328. err = xcvr_10g_set_lb_bcm870x(np);
  1329. if (err)
  1330. return err;
  1331. err = xcvr_diag_bcm870x(np);
  1332. if (err)
  1333. return err;
  1334. return 0;
  1335. }
  1336. static int xcvr_init_10g(struct niu *np)
  1337. {
  1338. int phy_id, err;
  1339. u64 val;
  1340. val = nr64_mac(XMAC_CONFIG);
  1341. val &= ~XMAC_CONFIG_LED_POLARITY;
  1342. val |= XMAC_CONFIG_FORCE_LED_ON;
  1343. nw64_mac(XMAC_CONFIG, val);
  1344. /* XXX shared resource, lock parent XXX */
  1345. val = nr64(MIF_CONFIG);
  1346. val |= MIF_CONFIG_INDIRECT_MODE;
  1347. nw64(MIF_CONFIG, val);
  1348. phy_id = phy_decode(np->parent->port_phy, np->port);
  1349. phy_id = np->parent->phy_probe_info.phy_id[phy_id][np->port];
  1350. /* handle different phy types */
  1351. switch (phy_id & NIU_PHY_ID_MASK) {
  1352. case NIU_PHY_ID_MRVL88X2011:
  1353. err = xcvr_init_10g_mrvl88x2011(np);
  1354. break;
  1355. default: /* bcom 8704 */
  1356. err = xcvr_init_10g_bcm8704(np);
  1357. break;
  1358. }
  1359. return err;
  1360. }
  1361. static int mii_reset(struct niu *np)
  1362. {
  1363. int limit, err;
  1364. err = mii_write(np, np->phy_addr, MII_BMCR, BMCR_RESET);
  1365. if (err)
  1366. return err;
  1367. limit = 1000;
  1368. while (--limit >= 0) {
  1369. udelay(500);
  1370. err = mii_read(np, np->phy_addr, MII_BMCR);
  1371. if (err < 0)
  1372. return err;
  1373. if (!(err & BMCR_RESET))
  1374. break;
  1375. }
  1376. if (limit < 0) {
  1377. netdev_err(np->dev, "Port %u MII would not reset, bmcr[%04x]\n",
  1378. np->port, err);
  1379. return -ENODEV;
  1380. }
  1381. return 0;
  1382. }
  1383. static int xcvr_init_1g_rgmii(struct niu *np)
  1384. {
  1385. int err;
  1386. u64 val;
  1387. u16 bmcr, bmsr, estat;
  1388. val = nr64(MIF_CONFIG);
  1389. val &= ~MIF_CONFIG_INDIRECT_MODE;
  1390. nw64(MIF_CONFIG, val);
  1391. err = mii_reset(np);
  1392. if (err)
  1393. return err;
  1394. err = mii_read(np, np->phy_addr, MII_BMSR);
  1395. if (err < 0)
  1396. return err;
  1397. bmsr = err;
  1398. estat = 0;
  1399. if (bmsr & BMSR_ESTATEN) {
  1400. err = mii_read(np, np->phy_addr, MII_ESTATUS);
  1401. if (err < 0)
  1402. return err;
  1403. estat = err;
  1404. }
  1405. bmcr = 0;
  1406. err = mii_write(np, np->phy_addr, MII_BMCR, bmcr);
  1407. if (err)
  1408. return err;
  1409. if (bmsr & BMSR_ESTATEN) {
  1410. u16 ctrl1000 = 0;
  1411. if (estat & ESTATUS_1000_TFULL)
  1412. ctrl1000 |= ADVERTISE_1000FULL;
  1413. err = mii_write(np, np->phy_addr, MII_CTRL1000, ctrl1000);
  1414. if (err)
  1415. return err;
  1416. }
  1417. bmcr = (BMCR_SPEED1000 | BMCR_FULLDPLX);
  1418. err = mii_write(np, np->phy_addr, MII_BMCR, bmcr);
  1419. if (err)
  1420. return err;
  1421. err = mii_read(np, np->phy_addr, MII_BMCR);
  1422. if (err < 0)
  1423. return err;
  1424. bmcr = mii_read(np, np->phy_addr, MII_BMCR);
  1425. err = mii_read(np, np->phy_addr, MII_BMSR);
  1426. if (err < 0)
  1427. return err;
  1428. return 0;
  1429. }
  1430. static int mii_init_common(struct niu *np)
  1431. {
  1432. struct niu_link_config *lp = &np->link_config;
  1433. u16 bmcr, bmsr, adv, estat;
  1434. int err;
  1435. err = mii_reset(np);
  1436. if (err)
  1437. return err;
  1438. err = mii_read(np, np->phy_addr, MII_BMSR);
  1439. if (err < 0)
  1440. return err;
  1441. bmsr = err;
  1442. estat = 0;
  1443. if (bmsr & BMSR_ESTATEN) {
  1444. err = mii_read(np, np->phy_addr, MII_ESTATUS);
  1445. if (err < 0)
  1446. return err;
  1447. estat = err;
  1448. }
  1449. bmcr = 0;
  1450. err = mii_write(np, np->phy_addr, MII_BMCR, bmcr);
  1451. if (err)
  1452. return err;
  1453. if (lp->loopback_mode == LOOPBACK_MAC) {
  1454. bmcr |= BMCR_LOOPBACK;
  1455. if (lp->active_speed == SPEED_1000)
  1456. bmcr |= BMCR_SPEED1000;
  1457. if (lp->active_duplex == DUPLEX_FULL)
  1458. bmcr |= BMCR_FULLDPLX;
  1459. }
  1460. if (lp->loopback_mode == LOOPBACK_PHY) {
  1461. u16 aux;
  1462. aux = (BCM5464R_AUX_CTL_EXT_LB |
  1463. BCM5464R_AUX_CTL_WRITE_1);
  1464. err = mii_write(np, np->phy_addr, BCM5464R_AUX_CTL, aux);
  1465. if (err)
  1466. return err;
  1467. }
  1468. if (lp->autoneg) {
  1469. u16 ctrl1000;
  1470. adv = ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP;
  1471. if ((bmsr & BMSR_10HALF) &&
  1472. (lp->advertising & ADVERTISED_10baseT_Half))
  1473. adv |= ADVERTISE_10HALF;
  1474. if ((bmsr & BMSR_10FULL) &&
  1475. (lp->advertising & ADVERTISED_10baseT_Full))
  1476. adv |= ADVERTISE_10FULL;
  1477. if ((bmsr & BMSR_100HALF) &&
  1478. (lp->advertising & ADVERTISED_100baseT_Half))
  1479. adv |= ADVERTISE_100HALF;
  1480. if ((bmsr & BMSR_100FULL) &&
  1481. (lp->advertising & ADVERTISED_100baseT_Full))
  1482. adv |= ADVERTISE_100FULL;
  1483. err = mii_write(np, np->phy_addr, MII_ADVERTISE, adv);
  1484. if (err)
  1485. return err;
  1486. if (likely(bmsr & BMSR_ESTATEN)) {
  1487. ctrl1000 = 0;
  1488. if ((estat & ESTATUS_1000_THALF) &&
  1489. (lp->advertising & ADVERTISED_1000baseT_Half))
  1490. ctrl1000 |= ADVERTISE_1000HALF;
  1491. if ((estat & ESTATUS_1000_TFULL) &&
  1492. (lp->advertising & ADVERTISED_1000baseT_Full))
  1493. ctrl1000 |= ADVERTISE_1000FULL;
  1494. err = mii_write(np, np->phy_addr,
  1495. MII_CTRL1000, ctrl1000);
  1496. if (err)
  1497. return err;
  1498. }
  1499. bmcr |= (BMCR_ANENABLE | BMCR_ANRESTART);
  1500. } else {
  1501. /* !lp->autoneg */
  1502. int fulldpx;
  1503. if (lp->duplex == DUPLEX_FULL) {
  1504. bmcr |= BMCR_FULLDPLX;
  1505. fulldpx = 1;
  1506. } else if (lp->duplex == DUPLEX_HALF)
  1507. fulldpx = 0;
  1508. else
  1509. return -EINVAL;
  1510. if (lp->speed == SPEED_1000) {
  1511. /* if X-full requested while not supported, or
  1512. X-half requested while not supported... */
  1513. if ((fulldpx && !(estat & ESTATUS_1000_TFULL)) ||
  1514. (!fulldpx && !(estat & ESTATUS_1000_THALF)))
  1515. return -EINVAL;
  1516. bmcr |= BMCR_SPEED1000;
  1517. } else if (lp->speed == SPEED_100) {
  1518. if ((fulldpx && !(bmsr & BMSR_100FULL)) ||
  1519. (!fulldpx && !(bmsr & BMSR_100HALF)))
  1520. return -EINVAL;
  1521. bmcr |= BMCR_SPEED100;
  1522. } else if (lp->speed == SPEED_10) {
  1523. if ((fulldpx && !(bmsr & BMSR_10FULL)) ||
  1524. (!fulldpx && !(bmsr & BMSR_10HALF)))
  1525. return -EINVAL;
  1526. } else
  1527. return -EINVAL;
  1528. }
  1529. err = mii_write(np, np->phy_addr, MII_BMCR, bmcr);
  1530. if (err)
  1531. return err;
  1532. #if 0
  1533. err = mii_read(np, np->phy_addr, MII_BMCR);
  1534. if (err < 0)
  1535. return err;
  1536. bmcr = err;
  1537. err = mii_read(np, np->phy_addr, MII_BMSR);
  1538. if (err < 0)
  1539. return err;
  1540. bmsr = err;
  1541. pr_info("Port %u after MII init bmcr[%04x] bmsr[%04x]\n",
  1542. np->port, bmcr, bmsr);
  1543. #endif
  1544. return 0;
  1545. }
  1546. static int xcvr_init_1g(struct niu *np)
  1547. {
  1548. u64 val;
  1549. /* XXX shared resource, lock parent XXX */
  1550. val = nr64(MIF_CONFIG);
  1551. val &= ~MIF_CONFIG_INDIRECT_MODE;
  1552. nw64(MIF_CONFIG, val);
  1553. return mii_init_common(np);
  1554. }
  1555. static int niu_xcvr_init(struct niu *np)
  1556. {
  1557. const struct niu_phy_ops *ops = np->phy_ops;
  1558. int err;
  1559. err = 0;
  1560. if (ops->xcvr_init)
  1561. err = ops->xcvr_init(np);
  1562. return err;
  1563. }
  1564. static int niu_serdes_init(struct niu *np)
  1565. {
  1566. const struct niu_phy_ops *ops = np->phy_ops;
  1567. int err;
  1568. err = 0;
  1569. if (ops->serdes_init)
  1570. err = ops->serdes_init(np);
  1571. return err;
  1572. }
  1573. static void niu_init_xif(struct niu *);
  1574. static void niu_handle_led(struct niu *, int status);
  1575. static int niu_link_status_common(struct niu *np, int link_up)
  1576. {
  1577. struct niu_link_config *lp = &np->link_config;
  1578. struct net_device *dev = np->dev;
  1579. unsigned long flags;
  1580. if (!netif_carrier_ok(dev) && link_up) {
  1581. netif_info(np, link, dev, "Link is up at %s, %s duplex\n",
  1582. lp->active_speed == SPEED_10000 ? "10Gb/sec" :
  1583. lp->active_speed == SPEED_1000 ? "1Gb/sec" :
  1584. lp->active_speed == SPEED_100 ? "100Mbit/sec" :
  1585. "10Mbit/sec",
  1586. lp->active_duplex == DUPLEX_FULL ? "full" : "half");
  1587. spin_lock_irqsave(&np->lock, flags);
  1588. niu_init_xif(np);
  1589. niu_handle_led(np, 1);
  1590. spin_unlock_irqrestore(&np->lock, flags);
  1591. netif_carrier_on(dev);
  1592. } else if (netif_carrier_ok(dev) && !link_up) {
  1593. netif_warn(np, link, dev, "Link is down\n");
  1594. spin_lock_irqsave(&np->lock, flags);
  1595. niu_handle_led(np, 0);
  1596. spin_unlock_irqrestore(&np->lock, flags);
  1597. netif_carrier_off(dev);
  1598. }
  1599. return 0;
  1600. }
  1601. static int link_status_10g_mrvl(struct niu *np, int *link_up_p)
  1602. {
  1603. int err, link_up, pma_status, pcs_status;
  1604. link_up = 0;
  1605. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
  1606. MRVL88X2011_10G_PMD_STATUS_2);
  1607. if (err < 0)
  1608. goto out;
  1609. /* Check PMA/PMD Register: 1.0001.2 == 1 */
  1610. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV1_ADDR,
  1611. MRVL88X2011_PMA_PMD_STATUS_1);
  1612. if (err < 0)
  1613. goto out;
  1614. pma_status = ((err & MRVL88X2011_LNK_STATUS_OK) ? 1 : 0);
  1615. /* Check PMC Register : 3.0001.2 == 1: read twice */
  1616. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV3_ADDR,
  1617. MRVL88X2011_PMA_PMD_STATUS_1);
  1618. if (err < 0)
  1619. goto out;
  1620. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV3_ADDR,
  1621. MRVL88X2011_PMA_PMD_STATUS_1);
  1622. if (err < 0)
  1623. goto out;
  1624. pcs_status = ((err & MRVL88X2011_LNK_STATUS_OK) ? 1 : 0);
  1625. /* Check XGXS Register : 4.0018.[0-3,12] */
  1626. err = mdio_read(np, np->phy_addr, MRVL88X2011_USER_DEV4_ADDR,
  1627. MRVL88X2011_10G_XGXS_LANE_STAT);
  1628. if (err < 0)
  1629. goto out;
  1630. if (err == (PHYXS_XGXS_LANE_STAT_ALINGED | PHYXS_XGXS_LANE_STAT_LANE3 |
  1631. PHYXS_XGXS_LANE_STAT_LANE2 | PHYXS_XGXS_LANE_STAT_LANE1 |
  1632. PHYXS_XGXS_LANE_STAT_LANE0 | PHYXS_XGXS_LANE_STAT_MAGIC |
  1633. 0x800))
  1634. link_up = (pma_status && pcs_status) ? 1 : 0;
  1635. np->link_config.active_speed = SPEED_10000;
  1636. np->link_config.active_duplex = DUPLEX_FULL;
  1637. err = 0;
  1638. out:
  1639. mrvl88x2011_act_led(np, (link_up ?
  1640. MRVL88X2011_LED_CTL_PCS_ACT :
  1641. MRVL88X2011_LED_CTL_OFF));
  1642. *link_up_p = link_up;
  1643. return err;
  1644. }
  1645. static int link_status_10g_bcm8706(struct niu *np, int *link_up_p)
  1646. {
  1647. int err, link_up;
  1648. link_up = 0;
  1649. err = mdio_read(np, np->phy_addr, BCM8704_PMA_PMD_DEV_ADDR,
  1650. BCM8704_PMD_RCV_SIGDET);
  1651. if (err < 0 || err == 0xffff)
  1652. goto out;
  1653. if (!(err & PMD_RCV_SIGDET_GLOBAL)) {
  1654. err = 0;
  1655. goto out;
  1656. }
  1657. err = mdio_read(np, np->phy_addr, BCM8704_PCS_DEV_ADDR,
  1658. BCM8704_PCS_10G_R_STATUS);
  1659. if (err < 0)
  1660. goto out;
  1661. if (!(err & PCS_10G_R_STATUS_BLK_LOCK)) {
  1662. err = 0;
  1663. goto out;
  1664. }
  1665. err = mdio_read(np, np->phy_addr, BCM8704_PHYXS_DEV_ADDR,
  1666. BCM8704_PHYXS_XGXS_LANE_STAT);
  1667. if (err < 0)
  1668. goto out;
  1669. if (err != (PHYXS_XGXS_LANE_STAT_ALINGED |
  1670. PHYXS_XGXS_LANE_STAT_MAGIC |
  1671. PHYXS_XGXS_LANE_STAT_PATTEST |
  1672. PHYXS_XGXS_LANE_STAT_LANE3 |
  1673. PHYXS_XGXS_LANE_STAT_LANE2 |
  1674. PHYXS_XGXS_LANE_STAT_LANE1 |
  1675. PHYXS_XGXS_LANE_STAT_LANE0)) {
  1676. err = 0;
  1677. np->link_config.active_speed = SPEED_INVALID;
  1678. np->link_config.active_duplex = DUPLEX_INVALID;
  1679. goto out;
  1680. }
  1681. link_up = 1;
  1682. np->link_config.active_speed = SPEED_10000;
  1683. np->link_config.active_duplex = DUPLEX_FULL;
  1684. err = 0;
  1685. out:
  1686. *link_up_p = link_up;
  1687. return err;
  1688. }
  1689. static int link_status_10g_bcom(struct niu *np, int *link_up_p)
  1690. {
  1691. int err, link_up;
  1692. link_up = 0;
  1693. err = mdio_read(np, np->phy_addr, BCM8704_PMA_PMD_DEV_ADDR,
  1694. BCM8704_PMD_RCV_SIGDET);
  1695. if (err < 0)
  1696. goto out;
  1697. if (!(err & PMD_RCV_SIGDET_GLOBAL)) {
  1698. err = 0;
  1699. goto out;
  1700. }
  1701. err = mdio_read(np, np->phy_addr, BCM8704_PCS_DEV_ADDR,
  1702. BCM8704_PCS_10G_R_STATUS);
  1703. if (err < 0)
  1704. goto out;
  1705. if (!(err & PCS_10G_R_STATUS_BLK_LOCK)) {
  1706. err = 0;
  1707. goto out;
  1708. }
  1709. err = mdio_read(np, np->phy_addr, BCM8704_PHYXS_DEV_ADDR,
  1710. BCM8704_PHYXS_XGXS_LANE_STAT);
  1711. if (err < 0)
  1712. goto out;
  1713. if (err != (PHYXS_XGXS_LANE_STAT_ALINGED |
  1714. PHYXS_XGXS_LANE_STAT_MAGIC |
  1715. PHYXS_XGXS_LANE_STAT_LANE3 |
  1716. PHYXS_XGXS_LANE_STAT_LANE2 |
  1717. PHYXS_XGXS_LANE_STAT_LANE1 |
  1718. PHYXS_XGXS_LANE_STAT_LANE0)) {
  1719. err = 0;
  1720. goto out;
  1721. }
  1722. link_up = 1;
  1723. np->link_config.active_speed = SPEED_10000;
  1724. np->link_config.active_duplex = DUPLEX_FULL;
  1725. err = 0;
  1726. out:
  1727. *link_up_p = link_up;
  1728. return err;
  1729. }
  1730. static int link_status_10g(struct niu *np, int *link_up_p)
  1731. {
  1732. unsigned long flags;
  1733. int err = -EINVAL;
  1734. spin_lock_irqsave(&np->lock, flags);
  1735. if (np->link_config.loopback_mode == LOOPBACK_DISABLED) {
  1736. int phy_id;
  1737. phy_id = phy_decode(np->parent->port_phy, np->port);
  1738. phy_id = np->parent->phy_probe_info.phy_id[phy_id][np->port];
  1739. /* handle different phy types */
  1740. switch (phy_id & NIU_PHY_ID_MASK) {
  1741. case NIU_PHY_ID_MRVL88X2011:
  1742. err = link_status_10g_mrvl(np, link_up_p);
  1743. break;
  1744. default: /* bcom 8704 */
  1745. err = link_status_10g_bcom(np, link_up_p);
  1746. break;
  1747. }
  1748. }
  1749. spin_unlock_irqrestore(&np->lock, flags);
  1750. return err;
  1751. }
  1752. static int niu_10g_phy_present(struct niu *np)
  1753. {
  1754. u64 sig, mask, val;
  1755. sig = nr64(ESR_INT_SIGNALS);
  1756. switch (np->port) {
  1757. case 0:
  1758. mask = ESR_INT_SIGNALS_P0_BITS;
  1759. val = (ESR_INT_SRDY0_P0 |
  1760. ESR_INT_DET0_P0 |
  1761. ESR_INT_XSRDY_P0 |
  1762. ESR_INT_XDP_P0_CH3 |
  1763. ESR_INT_XDP_P0_CH2 |
  1764. ESR_INT_XDP_P0_CH1 |
  1765. ESR_INT_XDP_P0_CH0);
  1766. break;
  1767. case 1:
  1768. mask = ESR_INT_SIGNALS_P1_BITS;
  1769. val = (ESR_INT_SRDY0_P1 |
  1770. ESR_INT_DET0_P1 |
  1771. ESR_INT_XSRDY_P1 |
  1772. ESR_INT_XDP_P1_CH3 |
  1773. ESR_INT_XDP_P1_CH2 |
  1774. ESR_INT_XDP_P1_CH1 |
  1775. ESR_INT_XDP_P1_CH0);
  1776. break;
  1777. default:
  1778. return 0;
  1779. }
  1780. if ((sig & mask) != val)
  1781. return 0;
  1782. return 1;
  1783. }
  1784. static int link_status_10g_hotplug(struct niu *np, int *link_up_p)
  1785. {
  1786. unsigned long flags;
  1787. int err = 0;
  1788. int phy_present;
  1789. int phy_present_prev;
  1790. spin_lock_irqsave(&np->lock, flags);
  1791. if (np->link_config.loopback_mode == LOOPBACK_DISABLED) {
  1792. phy_present_prev = (np->flags & NIU_FLAGS_HOTPLUG_PHY_PRESENT) ?
  1793. 1 : 0;
  1794. phy_present = niu_10g_phy_present(np);
  1795. if (phy_present != phy_present_prev) {
  1796. /* state change */
  1797. if (phy_present) {
  1798. /* A NEM was just plugged in */
  1799. np->flags |= NIU_FLAGS_HOTPLUG_PHY_PRESENT;
  1800. if (np->phy_ops->xcvr_init)
  1801. err = np->phy_ops->xcvr_init(np);
  1802. if (err) {
  1803. err = mdio_read(np, np->phy_addr,
  1804. BCM8704_PHYXS_DEV_ADDR, MII_BMCR);
  1805. if (err == 0xffff) {
  1806. /* No mdio, back-to-back XAUI */
  1807. goto out;
  1808. }
  1809. /* debounce */
  1810. np->flags &= ~NIU_FLAGS_HOTPLUG_PHY_PRESENT;
  1811. }
  1812. } else {
  1813. np->flags &= ~NIU_FLAGS_HOTPLUG_PHY_PRESENT;
  1814. *link_up_p = 0;
  1815. netif_warn(np, link, np->dev,
  1816. "Hotplug PHY Removed\n");
  1817. }
  1818. }
  1819. out:
  1820. if (np->flags & NIU_FLAGS_HOTPLUG_PHY_PRESENT) {
  1821. err = link_status_10g_bcm8706(np, link_up_p);
  1822. if (err == 0xffff) {
  1823. /* No mdio, back-to-back XAUI: it is C10NEM */
  1824. *link_up_p = 1;
  1825. np->link_config.active_speed = SPEED_10000;
  1826. np->link_config.active_duplex = DUPLEX_FULL;
  1827. }
  1828. }
  1829. }
  1830. spin_unlock_irqrestore(&np->lock, flags);
  1831. return 0;
  1832. }
  1833. static int niu_link_status(struct niu *np, int *link_up_p)
  1834. {
  1835. const struct niu_phy_ops *ops = np->phy_ops;
  1836. int err;
  1837. err = 0;
  1838. if (ops->link_status)
  1839. err = ops->link_status(np, link_up_p);
  1840. return err;
  1841. }
  1842. static void niu_timer(unsigned long __opaque)
  1843. {
  1844. struct niu *np = (struct niu *) __opaque;
  1845. unsigned long off;
  1846. int err, link_up;
  1847. err = niu_link_status(np, &link_up);
  1848. if (!err)
  1849. niu_link_status_common(np, link_up);
  1850. if (netif_carrier_ok(np->dev))
  1851. off = 5 * HZ;
  1852. else
  1853. off = 1 * HZ;
  1854. np->timer.expires = jiffies + off;
  1855. add_timer(&np->timer);
  1856. }
  1857. static const struct niu_phy_ops phy_ops_10g_serdes = {
  1858. .serdes_init = serdes_init_10g_serdes,
  1859. .link_status = link_status_10g_serdes,
  1860. };
  1861. static const struct niu_phy_ops phy_ops_10g_serdes_niu = {
  1862. .serdes_init = serdes_init_niu_10g_serdes,
  1863. .link_status = link_status_10g_serdes,
  1864. };
  1865. static const struct niu_phy_ops phy_ops_1g_serdes_niu = {
  1866. .serdes_init = serdes_init_niu_1g_serdes,
  1867. .link_status = link_status_1g_serdes,
  1868. };
  1869. static const struct niu_phy_ops phy_ops_1g_rgmii = {
  1870. .xcvr_init = xcvr_init_1g_rgmii,
  1871. .link_status = link_status_1g_rgmii,
  1872. };
  1873. static const struct niu_phy_ops phy_ops_10g_fiber_niu = {
  1874. .serdes_init = serdes_init_niu_10g_fiber,
  1875. .xcvr_init = xcvr_init_10g,
  1876. .link_status = link_status_10g,
  1877. };
  1878. static const struct niu_phy_ops phy_ops_10g_fiber = {
  1879. .serdes_init = serdes_init_10g,
  1880. .xcvr_init = xcvr_init_10g,
  1881. .link_status = link_status_10g,
  1882. };
  1883. static const struct niu_phy_ops phy_ops_10g_fiber_hotplug = {
  1884. .serdes_init = serdes_init_10g,
  1885. .xcvr_init = xcvr_init_10g_bcm8706,
  1886. .link_status = link_status_10g_hotplug,
  1887. };
  1888. static const struct niu_phy_ops phy_ops_niu_10g_hotplug = {
  1889. .serdes_init = serdes_init_niu_10g_fiber,
  1890. .xcvr_init = xcvr_init_10g_bcm8706,
  1891. .link_status = link_status_10g_hotplug,
  1892. };
  1893. static const struct niu_phy_ops phy_ops_10g_copper = {
  1894. .serdes_init = serdes_init_10g,
  1895. .link_status = link_status_10g, /* XXX */
  1896. };
  1897. static const struct niu_phy_ops phy_ops_1g_fiber = {
  1898. .serdes_init = serdes_init_1g,
  1899. .xcvr_init = xcvr_init_1g,
  1900. .link_status = link_status_1g,
  1901. };
  1902. static const struct niu_phy_ops phy_ops_1g_copper = {
  1903. .xcvr_init = xcvr_init_1g,
  1904. .link_status = link_status_1g,
  1905. };
  1906. struct niu_phy_template {
  1907. const struct niu_phy_ops *ops;
  1908. u32 phy_addr_base;
  1909. };
  1910. static const struct niu_phy_template phy_template_niu_10g_fiber = {
  1911. .ops = &phy_ops_10g_fiber_niu,
  1912. .phy_addr_base = 16,
  1913. };
  1914. static const struct niu_phy_template phy_template_niu_10g_serdes = {
  1915. .ops = &phy_ops_10g_serdes_niu,
  1916. .phy_addr_base = 0,
  1917. };
  1918. static const struct niu_phy_template phy_template_niu_1g_serdes = {
  1919. .ops = &phy_ops_1g_serdes_niu,
  1920. .phy_addr_base = 0,
  1921. };
  1922. static const struct niu_phy_template phy_template_10g_fiber = {
  1923. .ops = &phy_ops_10g_fiber,
  1924. .phy_addr_base = 8,
  1925. };
  1926. static const struct niu_phy_template phy_template_10g_fiber_hotplug = {
  1927. .ops = &phy_ops_10g_fiber_hotplug,
  1928. .phy_addr_base = 8,
  1929. };
  1930. static const struct niu_phy_template phy_template_niu_10g_hotplug = {
  1931. .ops = &phy_ops_niu_10g_hotplug,
  1932. .phy_addr_base = 8,
  1933. };
  1934. static const struct niu_phy_template phy_template_10g_copper = {
  1935. .ops = &phy_ops_10g_copper,
  1936. .phy_addr_base = 10,
  1937. };
  1938. static const struct niu_phy_template phy_template_1g_fiber = {
  1939. .ops = &phy_ops_1g_fiber,
  1940. .phy_addr_base = 0,
  1941. };
  1942. static const struct niu_phy_template phy_template_1g_copper = {
  1943. .ops = &phy_ops_1g_copper,
  1944. .phy_addr_base = 0,
  1945. };
  1946. static const struct niu_phy_template phy_template_1g_rgmii = {
  1947. .ops = &phy_ops_1g_rgmii,
  1948. .phy_addr_base = 0,
  1949. };
  1950. static const struct niu_phy_template phy_template_10g_serdes = {
  1951. .ops = &phy_ops_10g_serdes,
  1952. .phy_addr_base = 0,
  1953. };
  1954. static int niu_atca_port_num[4] = {
  1955. 0, 0, 11, 10
  1956. };
  1957. static int serdes_init_10g_serdes(struct niu *np)
  1958. {
  1959. struct niu_link_config *lp = &np->link_config;
  1960. unsigned long ctrl_reg, test_cfg_reg, pll_cfg, i;
  1961. u64 ctrl_val, test_cfg_val, sig, mask, val;
  1962. switch (np->port) {
  1963. case 0:
  1964. ctrl_reg = ENET_SERDES_0_CTRL_CFG;
  1965. test_cfg_reg = ENET_SERDES_0_TEST_CFG;
  1966. pll_cfg = ENET_SERDES_0_PLL_CFG;
  1967. break;
  1968. case 1:
  1969. ctrl_reg = ENET_SERDES_1_CTRL_CFG;
  1970. test_cfg_reg = ENET_SERDES_1_TEST_CFG;
  1971. pll_cfg = ENET_SERDES_1_PLL_CFG;
  1972. break;
  1973. default:
  1974. return -EINVAL;
  1975. }
  1976. ctrl_val = (ENET_SERDES_CTRL_SDET_0 |
  1977. ENET_SERDES_CTRL_SDET_1 |
  1978. ENET_SERDES_CTRL_SDET_2 |
  1979. ENET_SERDES_CTRL_SDET_3 |
  1980. (0x5 << ENET_SERDES_CTRL_EMPH_0_SHIFT) |
  1981. (0x5 << ENET_SERDES_CTRL_EMPH_1_SHIFT) |
  1982. (0x5 << ENET_SERDES_CTRL_EMPH_2_SHIFT) |
  1983. (0x5 << ENET_SERDES_CTRL_EMPH_3_SHIFT) |
  1984. (0x1 << ENET_SERDES_CTRL_LADJ_0_SHIFT) |
  1985. (0x1 << ENET_SERDES_CTRL_LADJ_1_SHIFT) |
  1986. (0x1 << ENET_SERDES_CTRL_LADJ_2_SHIFT) |
  1987. (0x1 << ENET_SERDES_CTRL_LADJ_3_SHIFT));
  1988. test_cfg_val = 0;
  1989. if (lp->loopback_mode == LOOPBACK_PHY) {
  1990. test_cfg_val |= ((ENET_TEST_MD_PAD_LOOPBACK <<
  1991. ENET_SERDES_TEST_MD_0_SHIFT) |
  1992. (ENET_TEST_MD_PAD_LOOPBACK <<
  1993. ENET_SERDES_TEST_MD_1_SHIFT) |
  1994. (ENET_TEST_MD_PAD_LOOPBACK <<
  1995. ENET_SERDES_TEST_MD_2_SHIFT) |
  1996. (ENET_TEST_MD_PAD_LOOPBACK <<
  1997. ENET_SERDES_TEST_MD_3_SHIFT));
  1998. }
  1999. esr_reset(np);
  2000. nw64(pll_cfg, ENET_SERDES_PLL_FBDIV2);
  2001. nw64(ctrl_reg, ctrl_val);
  2002. nw64(test_cfg_reg, test_cfg_val);
  2003. /* Initialize all 4 lanes of the SERDES. */
  2004. for (i = 0; i < 4; i++) {
  2005. u32 rxtx_ctrl, glue0;
  2006. int err;
  2007. err = esr_read_rxtx_ctrl(np, i, &rxtx_ctrl);
  2008. if (err)
  2009. return err;
  2010. err = esr_read_glue0(np, i, &glue0);
  2011. if (err)
  2012. return err;
  2013. rxtx_ctrl &= ~(ESR_RXTX_CTRL_VMUXLO);
  2014. rxtx_ctrl |= (ESR_RXTX_CTRL_ENSTRETCH |
  2015. (2 << ESR_RXTX_CTRL_VMUXLO_SHIFT));
  2016. glue0 &= ~(ESR_GLUE_CTRL0_SRATE |
  2017. ESR_GLUE_CTRL0_THCNT |
  2018. ESR_GLUE_CTRL0_BLTIME);
  2019. glue0 |= (ESR_GLUE_CTRL0_RXLOSENAB |
  2020. (0xf << ESR_GLUE_CTRL0_SRATE_SHIFT) |
  2021. (0xff << ESR_GLUE_CTRL0_THCNT_SHIFT) |
  2022. (BLTIME_300_CYCLES <<
  2023. ESR_GLUE_CTRL0_BLTIME_SHIFT));
  2024. err = esr_write_rxtx_ctrl(np, i, rxtx_ctrl);
  2025. if (err)
  2026. return err;
  2027. err = esr_write_glue0(np, i, glue0);
  2028. if (err)
  2029. return err;
  2030. }
  2031. sig = nr64(ESR_INT_SIGNALS);
  2032. switch (np->port) {
  2033. case 0:
  2034. mask = ESR_INT_SIGNALS_P0_BITS;
  2035. val = (ESR_INT_SRDY0_P0 |
  2036. ESR_INT_DET0_P0 |
  2037. ESR_INT_XSRDY_P0 |
  2038. ESR_INT_XDP_P0_CH3 |
  2039. ESR_INT_XDP_P0_CH2 |
  2040. ESR_INT_XDP_P0_CH1 |
  2041. ESR_INT_XDP_P0_CH0);
  2042. break;
  2043. case 1:
  2044. mask = ESR_INT_SIGNALS_P1_BITS;
  2045. val = (ESR_INT_SRDY0_P1 |
  2046. ESR_INT_DET0_P1 |
  2047. ESR_INT_XSRDY_P1 |
  2048. ESR_INT_XDP_P1_CH3 |
  2049. ESR_INT_XDP_P1_CH2 |
  2050. ESR_INT_XDP_P1_CH1 |
  2051. ESR_INT_XDP_P1_CH0);
  2052. break;
  2053. default:
  2054. return -EINVAL;
  2055. }
  2056. if ((sig & mask) != val) {
  2057. int err;
  2058. err = serdes_init_1g_serdes(np);
  2059. if (!err) {
  2060. np->flags &= ~NIU_FLAGS_10G;
  2061. np->mac_xcvr = MAC_XCVR_PCS;
  2062. } else {
  2063. netdev_err(np->dev, "Port %u 10G/1G SERDES Link Failed\n",
  2064. np->port);
  2065. return -ENODEV;
  2066. }
  2067. }
  2068. return 0;
  2069. }
  2070. static int niu_determine_phy_disposition(struct niu *np)
  2071. {
  2072. struct niu_parent *parent = np->parent;
  2073. u8 plat_type = parent->plat_type;
  2074. const struct niu_phy_template *tp;
  2075. u32 phy_addr_off = 0;
  2076. if (plat_type == PLAT_TYPE_NIU) {
  2077. switch (np->flags &
  2078. (NIU_FLAGS_10G |
  2079. NIU_FLAGS_FIBER |
  2080. NIU_FLAGS_XCVR_SERDES)) {
  2081. case NIU_FLAGS_10G | NIU_FLAGS_XCVR_SERDES:
  2082. /* 10G Serdes */
  2083. tp = &phy_template_niu_10g_serdes;
  2084. break;
  2085. case NIU_FLAGS_XCVR_SERDES:
  2086. /* 1G Serdes */
  2087. tp = &phy_template_niu_1g_serdes;
  2088. break;
  2089. case NIU_FLAGS_10G | NIU_FLAGS_FIBER:
  2090. /* 10G Fiber */
  2091. default:
  2092. if (np->flags & NIU_FLAGS_HOTPLUG_PHY) {
  2093. tp = &phy_template_niu_10g_hotplug;
  2094. if (np->port == 0)
  2095. phy_addr_off = 8;
  2096. if (np->port == 1)
  2097. phy_addr_off = 12;
  2098. } else {
  2099. tp = &phy_template_niu_10g_fiber;
  2100. phy_addr_off += np->port;
  2101. }
  2102. break;
  2103. }
  2104. } else {
  2105. switch (np->flags &
  2106. (NIU_FLAGS_10G |
  2107. NIU_FLAGS_FIBER |
  2108. NIU_FLAGS_XCVR_SERDES)) {
  2109. case 0:
  2110. /* 1G copper */
  2111. tp = &phy_template_1g_copper;
  2112. if (plat_type == PLAT_TYPE_VF_P0)
  2113. phy_addr_off = 10;
  2114. else if (plat_type == PLAT_TYPE_VF_P1)
  2115. phy_addr_off = 26;
  2116. phy_addr_off += (np->port ^ 0x3);
  2117. break;
  2118. case NIU_FLAGS_10G:
  2119. /* 10G copper */
  2120. tp = &phy_template_10g_copper;
  2121. break;
  2122. case NIU_FLAGS_FIBER:
  2123. /* 1G fiber */
  2124. tp = &phy_template_1g_fiber;
  2125. break;
  2126. case NIU_FLAGS_10G | NIU_FLAGS_FIBER:
  2127. /* 10G fiber */
  2128. tp = &phy_template_10g_fiber;
  2129. if (plat_type == PLAT_TYPE_VF_P0 ||
  2130. plat_type == PLAT_TYPE_VF_P1)
  2131. phy_addr_off = 8;
  2132. phy_addr_off += np->port;
  2133. if (np->flags & NIU_FLAGS_HOTPLUG_PHY) {
  2134. tp = &phy_template_10g_fiber_hotplug;
  2135. if (np->port == 0)
  2136. phy_addr_off = 8;
  2137. if (np->port == 1)
  2138. phy_addr_off = 12;
  2139. }
  2140. break;
  2141. case NIU_FLAGS_10G | NIU_FLAGS_XCVR_SERDES:
  2142. case NIU_FLAGS_XCVR_SERDES | NIU_FLAGS_FIBER:
  2143. case NIU_FLAGS_XCVR_SERDES:
  2144. switch(np->port) {
  2145. case 0:
  2146. case 1:
  2147. tp = &phy_template_10g_serdes;
  2148. break;
  2149. case 2:
  2150. case 3:
  2151. tp = &phy_template_1g_rgmii;
  2152. break;
  2153. default:
  2154. return -EINVAL;
  2155. break;
  2156. }
  2157. phy_addr_off = niu_atca_port_num[np->port];
  2158. break;
  2159. default:
  2160. return -EINVAL;
  2161. }
  2162. }
  2163. np->phy_ops = tp->ops;
  2164. np->phy_addr = tp->phy_addr_base + phy_addr_off;
  2165. return 0;
  2166. }
  2167. static int niu_init_link(struct niu *np)
  2168. {
  2169. struct niu_parent *parent = np->parent;
  2170. int err, ignore;
  2171. if (parent->plat_type == PLAT_TYPE_NIU) {
  2172. err = niu_xcvr_init(np);
  2173. if (err)
  2174. return err;
  2175. msleep(200);
  2176. }
  2177. err = niu_serdes_init(np);
  2178. if (err && !(np->flags & NIU_FLAGS_HOTPLUG_PHY))
  2179. return err;
  2180. msleep(200);
  2181. err = niu_xcvr_init(np);
  2182. if (!err || (np->flags & NIU_FLAGS_HOTPLUG_PHY))
  2183. niu_link_status(np, &ignore);
  2184. return 0;
  2185. }
  2186. static void niu_set_primary_mac(struct niu *np, unsigned char *addr)
  2187. {
  2188. u16 reg0 = addr[4] << 8 | addr[5];
  2189. u16 reg1 = addr[2] << 8 | addr[3];
  2190. u16 reg2 = addr[0] << 8 | addr[1];
  2191. if (np->flags & NIU_FLAGS_XMAC) {
  2192. nw64_mac(XMAC_ADDR0, reg0);
  2193. nw64_mac(XMAC_ADDR1, reg1);
  2194. nw64_mac(XMAC_ADDR2, reg2);
  2195. } else {
  2196. nw64_mac(BMAC_ADDR0, reg0);
  2197. nw64_mac(BMAC_ADDR1, reg1);
  2198. nw64_mac(BMAC_ADDR2, reg2);
  2199. }
  2200. }
  2201. static int niu_num_alt_addr(struct niu *np)
  2202. {
  2203. if (np->flags & NIU_FLAGS_XMAC)
  2204. return XMAC_NUM_ALT_ADDR;
  2205. else
  2206. return BMAC_NUM_ALT_ADDR;
  2207. }
  2208. static int niu_set_alt_mac(struct niu *np, int index, unsigned char *addr)
  2209. {
  2210. u16 reg0 = addr[4] << 8 | addr[5];
  2211. u16 reg1 = addr[2] << 8 | addr[3];
  2212. u16 reg2 = addr[0] << 8 | addr[1];
  2213. if (index >= niu_num_alt_addr(np))
  2214. return -EINVAL;
  2215. if (np->flags & NIU_FLAGS_XMAC) {
  2216. nw64_mac(XMAC_ALT_ADDR0(index), reg0);
  2217. nw64_mac(XMAC_ALT_ADDR1(index), reg1);
  2218. nw64_mac(XMAC_ALT_ADDR2(index), reg2);
  2219. } else {
  2220. nw64_mac(BMAC_ALT_ADDR0(index), reg0);
  2221. nw64_mac(BMAC_ALT_ADDR1(index), reg1);
  2222. nw64_mac(BMAC_ALT_ADDR2(index), reg2);
  2223. }
  2224. return 0;
  2225. }
  2226. static int niu_enable_alt_mac(struct niu *np, int index, int on)
  2227. {
  2228. unsigned long reg;
  2229. u64 val, mask;
  2230. if (index >= niu_num_alt_addr(np))
  2231. return -EINVAL;
  2232. if (np->flags & NIU_FLAGS_XMAC) {
  2233. reg = XMAC_ADDR_CMPEN;
  2234. mask = 1 << index;
  2235. } else {
  2236. reg = BMAC_ADDR_CMPEN;
  2237. mask = 1 << (index + 1);
  2238. }
  2239. val = nr64_mac(reg);
  2240. if (on)
  2241. val |= mask;
  2242. else
  2243. val &= ~mask;
  2244. nw64_mac(reg, val);
  2245. return 0;
  2246. }
  2247. static void __set_rdc_table_num_hw(struct niu *np, unsigned long reg,
  2248. int num, int mac_pref)
  2249. {
  2250. u64 val = nr64_mac(reg);
  2251. val &= ~(HOST_INFO_MACRDCTBLN | HOST_INFO_MPR);
  2252. val |= num;
  2253. if (mac_pref)
  2254. val |= HOST_INFO_MPR;
  2255. nw64_mac(reg, val);
  2256. }
  2257. static int __set_rdc_table_num(struct niu *np,
  2258. int xmac_index, int bmac_index,
  2259. int rdc_table_num, int mac_pref)
  2260. {
  2261. unsigned long reg;
  2262. if (rdc_table_num & ~HOST_INFO_MACRDCTBLN)
  2263. return -EINVAL;
  2264. if (np->flags & NIU_FLAGS_XMAC)
  2265. reg = XMAC_HOST_INFO(xmac_index);
  2266. else
  2267. reg = BMAC_HOST_INFO(bmac_index);
  2268. __set_rdc_table_num_hw(np, reg, rdc_table_num, mac_pref);
  2269. return 0;
  2270. }
  2271. static int niu_set_primary_mac_rdc_table(struct niu *np, int table_num,
  2272. int mac_pref)
  2273. {
  2274. return __set_rdc_table_num(np, 17, 0, table_num, mac_pref);
  2275. }
  2276. static int niu_set_multicast_mac_rdc_table(struct niu *np, int table_num,
  2277. int mac_pref)
  2278. {
  2279. return __set_rdc_table_num(np, 16, 8, table_num, mac_pref);
  2280. }
  2281. static int niu_set_alt_mac_rdc_table(struct niu *np, int idx,
  2282. int table_num, int mac_pref)
  2283. {
  2284. if (idx >= niu_num_alt_addr(np))
  2285. return -EINVAL;
  2286. return __set_rdc_table_num(np, idx, idx + 1, table_num, mac_pref);
  2287. }
  2288. static u64 vlan_entry_set_parity(u64 reg_val)
  2289. {
  2290. u64 port01_mask;
  2291. u64 port23_mask;
  2292. port01_mask = 0x00ff;
  2293. port23_mask = 0xff00;
  2294. if (hweight64(reg_val & port01_mask) & 1)
  2295. reg_val |= ENET_VLAN_TBL_PARITY0;
  2296. else
  2297. reg_val &= ~ENET_VLAN_TBL_PARITY0;
  2298. if (hweight64(reg_val & port23_mask) & 1)
  2299. reg_val |= ENET_VLAN_TBL_PARITY1;
  2300. else
  2301. reg_val &= ~ENET_VLAN_TBL_PARITY1;
  2302. return reg_val;
  2303. }
  2304. static void vlan_tbl_write(struct niu *np, unsigned long index,
  2305. int port, int vpr, int rdc_table)
  2306. {
  2307. u64 reg_val = nr64(ENET_VLAN_TBL(index));
  2308. reg_val &= ~((ENET_VLAN_TBL_VPR |
  2309. ENET_VLAN_TBL_VLANRDCTBLN) <<
  2310. ENET_VLAN_TBL_SHIFT(port));
  2311. if (vpr)
  2312. reg_val |= (ENET_VLAN_TBL_VPR <<
  2313. ENET_VLAN_TBL_SHIFT(port));
  2314. reg_val |= (rdc_table << ENET_VLAN_TBL_SHIFT(port));
  2315. reg_val = vlan_entry_set_parity(reg_val);
  2316. nw64(ENET_VLAN_TBL(index), reg_val);
  2317. }
  2318. static void vlan_tbl_clear(struct niu *np)
  2319. {
  2320. int i;
  2321. for (i = 0; i < ENET_VLAN_TBL_NUM_ENTRIES; i++)
  2322. nw64(ENET_VLAN_TBL(i), 0);
  2323. }
  2324. static int tcam_wait_bit(struct niu *np, u64 bit)
  2325. {
  2326. int limit = 1000;
  2327. while (--limit > 0) {
  2328. if (nr64(TCAM_CTL) & bit)
  2329. break;
  2330. udelay(1);
  2331. }
  2332. if (limit <= 0)
  2333. return -ENODEV;
  2334. return 0;
  2335. }
  2336. static int tcam_flush(struct niu *np, int index)
  2337. {
  2338. nw64(TCAM_KEY_0, 0x00);
  2339. nw64(TCAM_KEY_MASK_0, 0xff);
  2340. nw64(TCAM_CTL, (TCAM_CTL_RWC_TCAM_WRITE | index));
  2341. return tcam_wait_bit(np, TCAM_CTL_STAT);
  2342. }
  2343. #if 0
  2344. static int tcam_read(struct niu *np, int index,
  2345. u64 *key, u64 *mask)
  2346. {
  2347. int err;
  2348. nw64(TCAM_CTL, (TCAM_CTL_RWC_TCAM_READ | index));
  2349. err = tcam_wait_bit(np, TCAM_CTL_STAT);
  2350. if (!err) {
  2351. key[0] = nr64(TCAM_KEY_0);
  2352. key[1] = nr64(TCAM_KEY_1);
  2353. key[2] = nr64(TCAM_KEY_2);
  2354. key[3] = nr64(TCAM_KEY_3);
  2355. mask[0] = nr64(TCAM_KEY_MASK_0);
  2356. mask[1] = nr64(TCAM_KEY_MASK_1);
  2357. mask[2] = nr64(TCAM_KEY_MASK_2);
  2358. mask[3] = nr64(TCAM_KEY_MASK_3);
  2359. }
  2360. return err;
  2361. }
  2362. #endif
  2363. static int tcam_write(struct niu *np, int index,
  2364. u64 *key, u64 *mask)
  2365. {
  2366. nw64(TCAM_KEY_0, key[0]);
  2367. nw64(TCAM_KEY_1, key[1]);
  2368. nw64(TCAM_KEY_2, key[2]);
  2369. nw64(TCAM_KEY_3, key[3]);
  2370. nw64(TCAM_KEY_MASK_0, mask[0]);
  2371. nw64(TCAM_KEY_MASK_1, mask[1]);
  2372. nw64(TCAM_KEY_MASK_2, mask[2]);
  2373. nw64(TCAM_KEY_MASK_3, mask[3]);
  2374. nw64(TCAM_CTL, (TCAM_CTL_RWC_TCAM_WRITE | index));
  2375. return tcam_wait_bit(np, TCAM_CTL_STAT);
  2376. }
  2377. #if 0
  2378. static int tcam_assoc_read(struct niu *np, int index, u64 *data)
  2379. {
  2380. int err;
  2381. nw64(TCAM_CTL, (TCAM_CTL_RWC_RAM_READ | index));
  2382. err = tcam_wait_bit(np, TCAM_CTL_STAT);
  2383. if (!err)
  2384. *data = nr64(TCAM_KEY_1);
  2385. return err;
  2386. }
  2387. #endif
  2388. static int tcam_assoc_write(struct niu *np, int index, u64 assoc_data)
  2389. {
  2390. nw64(TCAM_KEY_1, assoc_data);
  2391. nw64(TCAM_CTL, (TCAM_CTL_RWC_RAM_WRITE | index));
  2392. return tcam_wait_bit(np, TCAM_CTL_STAT);
  2393. }
  2394. static void tcam_enable(struct niu *np, int on)
  2395. {
  2396. u64 val = nr64(FFLP_CFG_1);
  2397. if (on)
  2398. val &= ~FFLP_CFG_1_TCAM_DIS;
  2399. else
  2400. val |= FFLP_CFG_1_TCAM_DIS;
  2401. nw64(FFLP_CFG_1, val);
  2402. }
  2403. static void tcam_set_lat_and_ratio(struct niu *np, u64 latency, u64 ratio)
  2404. {
  2405. u64 val = nr64(FFLP_CFG_1);
  2406. val &= ~(FFLP_CFG_1_FFLPINITDONE |
  2407. FFLP_CFG_1_CAMLAT |
  2408. FFLP_CFG_1_CAMRATIO);
  2409. val |= (latency << FFLP_CFG_1_CAMLAT_SHIFT);
  2410. val |= (ratio << FFLP_CFG_1_CAMRATIO_SHIFT);
  2411. nw64(FFLP_CFG_1, val);
  2412. val = nr64(FFLP_CFG_1);
  2413. val |= FFLP_CFG_1_FFLPINITDONE;
  2414. nw64(FFLP_CFG_1, val);
  2415. }
  2416. static int tcam_user_eth_class_enable(struct niu *np, unsigned long class,
  2417. int on)
  2418. {
  2419. unsigned long reg;
  2420. u64 val;
  2421. if (class < CLASS_CODE_ETHERTYPE1 ||
  2422. class > CLASS_CODE_ETHERTYPE2)
  2423. return -EINVAL;
  2424. reg = L2_CLS(class - CLASS_CODE_ETHERTYPE1);
  2425. val = nr64(reg);
  2426. if (on)
  2427. val |= L2_CLS_VLD;
  2428. else
  2429. val &= ~L2_CLS_VLD;
  2430. nw64(reg, val);
  2431. return 0;
  2432. }
  2433. #if 0
  2434. static int tcam_user_eth_class_set(struct niu *np, unsigned long class,
  2435. u64 ether_type)
  2436. {
  2437. unsigned long reg;
  2438. u64 val;
  2439. if (class < CLASS_CODE_ETHERTYPE1 ||
  2440. class > CLASS_CODE_ETHERTYPE2 ||
  2441. (ether_type & ~(u64)0xffff) != 0)
  2442. return -EINVAL;
  2443. reg = L2_CLS(class - CLASS_CODE_ETHERTYPE1);
  2444. val = nr64(reg);
  2445. val &= ~L2_CLS_ETYPE;
  2446. val |= (ether_type << L2_CLS_ETYPE_SHIFT);
  2447. nw64(reg, val);
  2448. return 0;
  2449. }
  2450. #endif
  2451. static int tcam_user_ip_class_enable(struct niu *np, unsigned long class,
  2452. int on)
  2453. {
  2454. unsigned long reg;
  2455. u64 val;
  2456. if (class < CLASS_CODE_USER_PROG1 ||
  2457. class > CLASS_CODE_USER_PROG4)
  2458. return -EINVAL;
  2459. reg = L3_CLS(class - CLASS_CODE_USER_PROG1);
  2460. val = nr64(reg);
  2461. if (on)
  2462. val |= L3_CLS_VALID;
  2463. else
  2464. val &= ~L3_CLS_VALID;
  2465. nw64(reg, val);
  2466. return 0;
  2467. }
  2468. static int tcam_user_ip_class_set(struct niu *np, unsigned long class,
  2469. int ipv6, u64 protocol_id,
  2470. u64 tos_mask, u64 tos_val)
  2471. {
  2472. unsigned long reg;
  2473. u64 val;
  2474. if (class < CLASS_CODE_USER_PROG1 ||
  2475. class > CLASS_CODE_USER_PROG4 ||
  2476. (protocol_id & ~(u64)0xff) != 0 ||
  2477. (tos_mask & ~(u64)0xff) != 0 ||
  2478. (tos_val & ~(u64)0xff) != 0)
  2479. return -EINVAL;
  2480. reg = L3_CLS(class - CLASS_CODE_USER_PROG1);
  2481. val = nr64(reg);
  2482. val &= ~(L3_CLS_IPVER | L3_CLS_PID |
  2483. L3_CLS_TOSMASK | L3_CLS_TOS);
  2484. if (ipv6)
  2485. val |= L3_CLS_IPVER;
  2486. val |= (protocol_id << L3_CLS_PID_SHIFT);
  2487. val |= (tos_mask << L3_CLS_TOSMASK_SHIFT);
  2488. val |= (tos_val << L3_CLS_TOS_SHIFT);
  2489. nw64(reg, val);
  2490. return 0;
  2491. }
  2492. static int tcam_early_init(struct niu *np)
  2493. {
  2494. unsigned long i;
  2495. int err;
  2496. tcam_enable(np, 0);
  2497. tcam_set_lat_and_ratio(np,
  2498. DEFAULT_TCAM_LATENCY,
  2499. DEFAULT_TCAM_ACCESS_RATIO);
  2500. for (i = CLASS_CODE_ETHERTYPE1; i <= CLASS_CODE_ETHERTYPE2; i++) {
  2501. err = tcam_user_eth_class_enable(np, i, 0);
  2502. if (err)
  2503. return err;
  2504. }
  2505. for (i = CLASS_CODE_USER_PROG1; i <= CLASS_CODE_USER_PROG4; i++) {
  2506. err = tcam_user_ip_class_enable(np, i, 0);
  2507. if (err)
  2508. return err;
  2509. }
  2510. return 0;
  2511. }
  2512. static int tcam_flush_all(struct niu *np)
  2513. {
  2514. unsigned long i;
  2515. for (i = 0; i < np->parent->tcam_num_entries; i++) {
  2516. int err = tcam_flush(np, i);
  2517. if (err)
  2518. return err;
  2519. }
  2520. return 0;
  2521. }
  2522. static u64 hash_addr_regval(unsigned long index, unsigned long num_entries)
  2523. {
  2524. return (u64)index | (num_entries == 1 ? HASH_TBL_ADDR_AUTOINC : 0);
  2525. }
  2526. #if 0
  2527. static int hash_read(struct niu *np, unsigned long partition,
  2528. unsigned long index, unsigned long num_entries,
  2529. u64 *data)
  2530. {
  2531. u64 val = hash_addr_regval(index, num_entries);
  2532. unsigned long i;
  2533. if (partition >= FCRAM_NUM_PARTITIONS ||
  2534. index + num_entries > FCRAM_SIZE)
  2535. return -EINVAL;
  2536. nw64(HASH_TBL_ADDR(partition), val);
  2537. for (i = 0; i < num_entries; i++)
  2538. data[i] = nr64(HASH_TBL_DATA(partition));
  2539. return 0;
  2540. }
  2541. #endif
  2542. static int hash_write(struct niu *np, unsigned long partition,
  2543. unsigned long index, unsigned long num_entries,
  2544. u64 *data)
  2545. {
  2546. u64 val = hash_addr_regval(index, num_entries);
  2547. unsigned long i;
  2548. if (partition >= FCRAM_NUM_PARTITIONS ||
  2549. index + (num_entries * 8) > FCRAM_SIZE)
  2550. return -EINVAL;
  2551. nw64(HASH_TBL_ADDR(partition), val);
  2552. for (i = 0; i < num_entries; i++)
  2553. nw64(HASH_TBL_DATA(partition), data[i]);
  2554. return 0;
  2555. }
  2556. static void fflp_reset(struct niu *np)
  2557. {
  2558. u64 val;
  2559. nw64(FFLP_CFG_1, FFLP_CFG_1_PIO_FIO_RST);
  2560. udelay(10);
  2561. nw64(FFLP_CFG_1, 0);
  2562. val = FFLP_CFG_1_FCRAMOUTDR_NORMAL | FFLP_CFG_1_FFLPINITDONE;
  2563. nw64(FFLP_CFG_1, val);
  2564. }
  2565. static void fflp_set_timings(struct niu *np)
  2566. {
  2567. u64 val = nr64(FFLP_CFG_1);
  2568. val &= ~FFLP_CFG_1_FFLPINITDONE;
  2569. val |= (DEFAULT_FCRAMRATIO << FFLP_CFG_1_FCRAMRATIO_SHIFT);
  2570. nw64(FFLP_CFG_1, val);
  2571. val = nr64(FFLP_CFG_1);
  2572. val |= FFLP_CFG_1_FFLPINITDONE;
  2573. nw64(FFLP_CFG_1, val);
  2574. val = nr64(FCRAM_REF_TMR);
  2575. val &= ~(FCRAM_REF_TMR_MAX | FCRAM_REF_TMR_MIN);
  2576. val |= (DEFAULT_FCRAM_REFRESH_MAX << FCRAM_REF_TMR_MAX_SHIFT);
  2577. val |= (DEFAULT_FCRAM_REFRESH_MIN << FCRAM_REF_TMR_MIN_SHIFT);
  2578. nw64(FCRAM_REF_TMR, val);
  2579. }
  2580. static int fflp_set_partition(struct niu *np, u64 partition,
  2581. u64 mask, u64 base, int enable)
  2582. {
  2583. unsigned long reg;
  2584. u64 val;
  2585. if (partition >= FCRAM_NUM_PARTITIONS ||
  2586. (mask & ~(u64)0x1f) != 0 ||
  2587. (base & ~(u64)0x1f) != 0)
  2588. return -EINVAL;
  2589. reg = FLW_PRT_SEL(partition);
  2590. val = nr64(reg);
  2591. val &= ~(FLW_PRT_SEL_EXT | FLW_PRT_SEL_MASK | FLW_PRT_SEL_BASE);
  2592. val |= (mask << FLW_PRT_SEL_MASK_SHIFT);
  2593. val |= (base << FLW_PRT_SEL_BASE_SHIFT);
  2594. if (enable)
  2595. val |= FLW_PRT_SEL_EXT;
  2596. nw64(reg, val);
  2597. return 0;
  2598. }
  2599. static int fflp_disable_all_partitions(struct niu *np)
  2600. {
  2601. unsigned long i;
  2602. for (i = 0; i < FCRAM_NUM_PARTITIONS; i++) {
  2603. int err = fflp_set_partition(np, 0, 0, 0, 0);
  2604. if (err)
  2605. return err;
  2606. }
  2607. return 0;
  2608. }
  2609. static void fflp_llcsnap_enable(struct niu *np, int on)
  2610. {
  2611. u64 val = nr64(FFLP_CFG_1);
  2612. if (on)
  2613. val |= FFLP_CFG_1_LLCSNAP;
  2614. else
  2615. val &= ~FFLP_CFG_1_LLCSNAP;
  2616. nw64(FFLP_CFG_1, val);
  2617. }
  2618. static void fflp_errors_enable(struct niu *np, int on)
  2619. {
  2620. u64 val = nr64(FFLP_CFG_1);
  2621. if (on)
  2622. val &= ~FFLP_CFG_1_ERRORDIS;
  2623. else
  2624. val |= FFLP_CFG_1_ERRORDIS;
  2625. nw64(FFLP_CFG_1, val);
  2626. }
  2627. static int fflp_hash_clear(struct niu *np)
  2628. {
  2629. struct fcram_hash_ipv4 ent;
  2630. unsigned long i;
  2631. /* IPV4 hash entry with valid bit clear, rest is don't care. */
  2632. memset(&ent, 0, sizeof(ent));
  2633. ent.header = HASH_HEADER_EXT;
  2634. for (i = 0; i < FCRAM_SIZE; i += sizeof(ent)) {
  2635. int err = hash_write(np, 0, i, 1, (u64 *) &ent);
  2636. if (err)
  2637. return err;
  2638. }
  2639. return 0;
  2640. }
  2641. static int fflp_early_init(struct niu *np)
  2642. {
  2643. struct niu_parent *parent;
  2644. unsigned long flags;
  2645. int err;
  2646. niu_lock_parent(np, flags);
  2647. parent = np->parent;
  2648. err = 0;
  2649. if (!(parent->flags & PARENT_FLGS_CLS_HWINIT)) {
  2650. if (np->parent->plat_type != PLAT_TYPE_NIU) {
  2651. fflp_reset(np);
  2652. fflp_set_timings(np);
  2653. err = fflp_disable_all_partitions(np);
  2654. if (err) {
  2655. netif_printk(np, probe, KERN_DEBUG, np->dev,
  2656. "fflp_disable_all_partitions failed, err=%d\n",
  2657. err);
  2658. goto out;
  2659. }
  2660. }
  2661. err = tcam_early_init(np);
  2662. if (err) {
  2663. netif_printk(np, probe, KERN_DEBUG, np->dev,
  2664. "tcam_early_init failed, err=%d\n", err);
  2665. goto out;
  2666. }
  2667. fflp_llcsnap_enable(np, 1);
  2668. fflp_errors_enable(np, 0);
  2669. nw64(H1POLY, 0);
  2670. nw64(H2POLY, 0);
  2671. err = tcam_flush_all(np);
  2672. if (err) {
  2673. netif_printk(np, probe, KERN_DEBUG, np->dev,
  2674. "tcam_flush_all failed, err=%d\n", err);
  2675. goto out;
  2676. }
  2677. if (np->parent->plat_type != PLAT_TYPE_NIU) {
  2678. err = fflp_hash_clear(np);
  2679. if (err) {
  2680. netif_printk(np, probe, KERN_DEBUG, np->dev,
  2681. "fflp_hash_clear failed, err=%d\n",
  2682. err);
  2683. goto out;
  2684. }
  2685. }
  2686. vlan_tbl_clear(np);
  2687. parent->flags |= PARENT_FLGS_CLS_HWINIT;
  2688. }
  2689. out:
  2690. niu_unlock_parent(np, flags);
  2691. return err;
  2692. }
  2693. static int niu_set_flow_key(struct niu *np, unsigned long class_code, u64 key)
  2694. {
  2695. if (class_code < CLASS_CODE_USER_PROG1 ||
  2696. class_code > CLASS_CODE_SCTP_IPV6)
  2697. return -EINVAL;
  2698. nw64(FLOW_KEY(class_code - CLASS_CODE_USER_PROG1), key);
  2699. return 0;
  2700. }
  2701. static int niu_set_tcam_key(struct niu *np, unsigned long class_code, u64 key)
  2702. {
  2703. if (class_code < CLASS_CODE_USER_PROG1 ||
  2704. class_code > CLASS_CODE_SCTP_IPV6)
  2705. return -EINVAL;
  2706. nw64(TCAM_KEY(class_code - CLASS_CODE_USER_PROG1), key);
  2707. return 0;
  2708. }
  2709. /* Entries for the ports are interleaved in the TCAM */
  2710. static u16 tcam_get_index(struct niu *np, u16 idx)
  2711. {
  2712. /* One entry reserved for IP fragment rule */
  2713. if (idx >= (np->clas.tcam_sz - 1))
  2714. idx = 0;
  2715. return np->clas.tcam_top + ((idx+1) * np->parent->num_ports);
  2716. }
  2717. static u16 tcam_get_size(struct niu *np)
  2718. {
  2719. /* One entry reserved for IP fragment rule */
  2720. return np->clas.tcam_sz - 1;
  2721. }
  2722. static u16 tcam_get_valid_entry_cnt(struct niu *np)
  2723. {
  2724. /* One entry reserved for IP fragment rule */
  2725. return np->clas.tcam_valid_entries - 1;
  2726. }
  2727. static void niu_rx_skb_append(struct sk_buff *skb, struct page *page,
  2728. u32 offset, u32 size)
  2729. {
  2730. int i = skb_shinfo(skb)->nr_frags;
  2731. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  2732. frag->page = page;
  2733. frag->page_offset = offset;
  2734. frag->size = size;
  2735. skb->len += size;
  2736. skb->data_len += size;
  2737. skb->truesize += size;
  2738. skb_shinfo(skb)->nr_frags = i + 1;
  2739. }
  2740. static unsigned int niu_hash_rxaddr(struct rx_ring_info *rp, u64 a)
  2741. {
  2742. a >>= PAGE_SHIFT;
  2743. a ^= (a >> ilog2(MAX_RBR_RING_SIZE));
  2744. return a & (MAX_RBR_RING_SIZE - 1);
  2745. }
  2746. static struct page *niu_find_rxpage(struct rx_ring_info *rp, u64 addr,
  2747. struct page ***link)
  2748. {
  2749. unsigned int h = niu_hash_rxaddr(rp, addr);
  2750. struct page *p, **pp;
  2751. addr &= PAGE_MASK;
  2752. pp = &rp->rxhash[h];
  2753. for (; (p = *pp) != NULL; pp = (struct page **) &p->mapping) {
  2754. if (p->index == addr) {
  2755. *link = pp;
  2756. goto found;
  2757. }
  2758. }
  2759. BUG();
  2760. found:
  2761. return p;
  2762. }
  2763. static void niu_hash_page(struct rx_ring_info *rp, struct page *page, u64 base)
  2764. {
  2765. unsigned int h = niu_hash_rxaddr(rp, base);
  2766. page->index = base;
  2767. page->mapping = (struct address_space *) rp->rxhash[h];
  2768. rp->rxhash[h] = page;
  2769. }
  2770. static int niu_rbr_add_page(struct niu *np, struct rx_ring_info *rp,
  2771. gfp_t mask, int start_index)
  2772. {
  2773. struct page *page;
  2774. u64 addr;
  2775. int i;
  2776. page = alloc_page(mask);
  2777. if (!page)
  2778. return -ENOMEM;
  2779. addr = np->ops->map_page(np->device, page, 0,
  2780. PAGE_SIZE, DMA_FROM_DEVICE);
  2781. niu_hash_page(rp, page, addr);
  2782. if (rp->rbr_blocks_per_page > 1)
  2783. atomic_add(rp->rbr_blocks_per_page - 1,
  2784. &compound_head(page)->_count);
  2785. for (i = 0; i < rp->rbr_blocks_per_page; i++) {
  2786. __le32 *rbr = &rp->rbr[start_index + i];
  2787. *rbr = cpu_to_le32(addr >> RBR_DESCR_ADDR_SHIFT);
  2788. addr += rp->rbr_block_size;
  2789. }
  2790. return 0;
  2791. }
  2792. static void niu_rbr_refill(struct niu *np, struct rx_ring_info *rp, gfp_t mask)
  2793. {
  2794. int index = rp->rbr_index;
  2795. rp->rbr_pending++;
  2796. if ((rp->rbr_pending % rp->rbr_blocks_per_page) == 0) {
  2797. int err = niu_rbr_add_page(np, rp, mask, index);
  2798. if (unlikely(err)) {
  2799. rp->rbr_pending--;
  2800. return;
  2801. }
  2802. rp->rbr_index += rp->rbr_blocks_per_page;
  2803. BUG_ON(rp->rbr_index > rp->rbr_table_size);
  2804. if (rp->rbr_index == rp->rbr_table_size)
  2805. rp->rbr_index = 0;
  2806. if (rp->rbr_pending >= rp->rbr_kick_thresh) {
  2807. nw64(RBR_KICK(rp->rx_channel), rp->rbr_pending);
  2808. rp->rbr_pending = 0;
  2809. }
  2810. }
  2811. }
  2812. static int niu_rx_pkt_ignore(struct niu *np, struct rx_ring_info *rp)
  2813. {
  2814. unsigned int index = rp->rcr_index;
  2815. int num_rcr = 0;
  2816. rp->rx_dropped++;
  2817. while (1) {
  2818. struct page *page, **link;
  2819. u64 addr, val;
  2820. u32 rcr_size;
  2821. num_rcr++;
  2822. val = le64_to_cpup(&rp->rcr[index]);
  2823. addr = (val & RCR_ENTRY_PKT_BUF_ADDR) <<
  2824. RCR_ENTRY_PKT_BUF_ADDR_SHIFT;
  2825. page = niu_find_rxpage(rp, addr, &link);
  2826. rcr_size = rp->rbr_sizes[(val & RCR_ENTRY_PKTBUFSZ) >>
  2827. RCR_ENTRY_PKTBUFSZ_SHIFT];
  2828. if ((page->index + PAGE_SIZE) - rcr_size == addr) {
  2829. *link = (struct page *) page->mapping;
  2830. np->ops->unmap_page(np->device, page->index,
  2831. PAGE_SIZE, DMA_FROM_DEVICE);
  2832. page->index = 0;
  2833. page->mapping = NULL;
  2834. __free_page(page);
  2835. rp->rbr_refill_pending++;
  2836. }
  2837. index = NEXT_RCR(rp, index);
  2838. if (!(val & RCR_ENTRY_MULTI))
  2839. break;
  2840. }
  2841. rp->rcr_index = index;
  2842. return num_rcr;
  2843. }
  2844. static int niu_process_rx_pkt(struct napi_struct *napi, struct niu *np,
  2845. struct rx_ring_info *rp)
  2846. {
  2847. unsigned int index = rp->rcr_index;
  2848. struct rx_pkt_hdr1 *rh;
  2849. struct sk_buff *skb;
  2850. int len, num_rcr;
  2851. skb = netdev_alloc_skb(np->dev, RX_SKB_ALLOC_SIZE);
  2852. if (unlikely(!skb))
  2853. return niu_rx_pkt_ignore(np, rp);
  2854. num_rcr = 0;
  2855. while (1) {
  2856. struct page *page, **link;
  2857. u32 rcr_size, append_size;
  2858. u64 addr, val, off;
  2859. num_rcr++;
  2860. val = le64_to_cpup(&rp->rcr[index]);
  2861. len = (val & RCR_ENTRY_L2_LEN) >>
  2862. RCR_ENTRY_L2_LEN_SHIFT;
  2863. len -= ETH_FCS_LEN;
  2864. addr = (val & RCR_ENTRY_PKT_BUF_ADDR) <<
  2865. RCR_ENTRY_PKT_BUF_ADDR_SHIFT;
  2866. page = niu_find_rxpage(rp, addr, &link);
  2867. rcr_size = rp->rbr_sizes[(val & RCR_ENTRY_PKTBUFSZ) >>
  2868. RCR_ENTRY_PKTBUFSZ_SHIFT];
  2869. off = addr & ~PAGE_MASK;
  2870. append_size = rcr_size;
  2871. if (num_rcr == 1) {
  2872. int ptype;
  2873. ptype = (val >> RCR_ENTRY_PKT_TYPE_SHIFT);
  2874. if ((ptype == RCR_PKT_TYPE_TCP ||
  2875. ptype == RCR_PKT_TYPE_UDP) &&
  2876. !(val & (RCR_ENTRY_NOPORT |
  2877. RCR_ENTRY_ERROR)))
  2878. skb->ip_summed = CHECKSUM_UNNECESSARY;
  2879. else
  2880. skb_checksum_none_assert(skb);
  2881. } else if (!(val & RCR_ENTRY_MULTI))
  2882. append_size = len - skb->len;
  2883. niu_rx_skb_append(skb, page, off, append_size);
  2884. if ((page->index + rp->rbr_block_size) - rcr_size == addr) {
  2885. *link = (struct page *) page->mapping;
  2886. np->ops->unmap_page(np->device, page->index,
  2887. PAGE_SIZE, DMA_FROM_DEVICE);
  2888. page->index = 0;
  2889. page->mapping = NULL;
  2890. rp->rbr_refill_pending++;
  2891. } else
  2892. get_page(page);
  2893. index = NEXT_RCR(rp, index);
  2894. if (!(val & RCR_ENTRY_MULTI))
  2895. break;
  2896. }
  2897. rp->rcr_index = index;
  2898. len += sizeof(*rh);
  2899. len = min_t(int, len, sizeof(*rh) + VLAN_ETH_HLEN);
  2900. __pskb_pull_tail(skb, len);
  2901. rh = (struct rx_pkt_hdr1 *) skb->data;
  2902. if (np->dev->features & NETIF_F_RXHASH)
  2903. skb->rxhash = ((u32)rh->hashval2_0 << 24 |
  2904. (u32)rh->hashval2_1 << 16 |
  2905. (u32)rh->hashval1_1 << 8 |
  2906. (u32)rh->hashval1_2 << 0);
  2907. skb_pull(skb, sizeof(*rh));
  2908. rp->rx_packets++;
  2909. rp->rx_bytes += skb->len;
  2910. skb->protocol = eth_type_trans(skb, np->dev);
  2911. skb_record_rx_queue(skb, rp->rx_channel);
  2912. napi_gro_receive(napi, skb);
  2913. return num_rcr;
  2914. }
  2915. static int niu_rbr_fill(struct niu *np, struct rx_ring_info *rp, gfp_t mask)
  2916. {
  2917. int blocks_per_page = rp->rbr_blocks_per_page;
  2918. int err, index = rp->rbr_index;
  2919. err = 0;
  2920. while (index < (rp->rbr_table_size - blocks_per_page)) {
  2921. err = niu_rbr_add_page(np, rp, mask, index);
  2922. if (err)
  2923. break;
  2924. index += blocks_per_page;
  2925. }
  2926. rp->rbr_index = index;
  2927. return err;
  2928. }
  2929. static void niu_rbr_free(struct niu *np, struct rx_ring_info *rp)
  2930. {
  2931. int i;
  2932. for (i = 0; i < MAX_RBR_RING_SIZE; i++) {
  2933. struct page *page;
  2934. page = rp->rxhash[i];
  2935. while (page) {
  2936. struct page *next = (struct page *) page->mapping;
  2937. u64 base = page->index;
  2938. np->ops->unmap_page(np->device, base, PAGE_SIZE,
  2939. DMA_FROM_DEVICE);
  2940. page->index = 0;
  2941. page->mapping = NULL;
  2942. __free_page(page);
  2943. page = next;
  2944. }
  2945. }
  2946. for (i = 0; i < rp->rbr_table_size; i++)
  2947. rp->rbr[i] = cpu_to_le32(0);
  2948. rp->rbr_index = 0;
  2949. }
  2950. static int release_tx_packet(struct niu *np, struct tx_ring_info *rp, int idx)
  2951. {
  2952. struct tx_buff_info *tb = &rp->tx_buffs[idx];
  2953. struct sk_buff *skb = tb->skb;
  2954. struct tx_pkt_hdr *tp;
  2955. u64 tx_flags;
  2956. int i, len;
  2957. tp = (struct tx_pkt_hdr *) skb->data;
  2958. tx_flags = le64_to_cpup(&tp->flags);
  2959. rp->tx_packets++;
  2960. rp->tx_bytes += (((tx_flags & TXHDR_LEN) >> TXHDR_LEN_SHIFT) -
  2961. ((tx_flags & TXHDR_PAD) / 2));
  2962. len = skb_headlen(skb);
  2963. np->ops->unmap_single(np->device, tb->mapping,
  2964. len, DMA_TO_DEVICE);
  2965. if (le64_to_cpu(rp->descr[idx]) & TX_DESC_MARK)
  2966. rp->mark_pending--;
  2967. tb->skb = NULL;
  2968. do {
  2969. idx = NEXT_TX(rp, idx);
  2970. len -= MAX_TX_DESC_LEN;
  2971. } while (len > 0);
  2972. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  2973. tb = &rp->tx_buffs[idx];
  2974. BUG_ON(tb->skb != NULL);
  2975. np->ops->unmap_page(np->device, tb->mapping,
  2976. skb_shinfo(skb)->frags[i].size,
  2977. DMA_TO_DEVICE);
  2978. idx = NEXT_TX(rp, idx);
  2979. }
  2980. dev_kfree_skb(skb);
  2981. return idx;
  2982. }
  2983. #define NIU_TX_WAKEUP_THRESH(rp) ((rp)->pending / 4)
  2984. static void niu_tx_work(struct niu *np, struct tx_ring_info *rp)
  2985. {
  2986. struct netdev_queue *txq;
  2987. u16 pkt_cnt, tmp;
  2988. int cons, index;
  2989. u64 cs;
  2990. index = (rp - np->tx_rings);
  2991. txq = netdev_get_tx_queue(np->dev, index);
  2992. cs = rp->tx_cs;
  2993. if (unlikely(!(cs & (TX_CS_MK | TX_CS_MMK))))
  2994. goto out;
  2995. tmp = pkt_cnt = (cs & TX_CS_PKT_CNT) >> TX_CS_PKT_CNT_SHIFT;
  2996. pkt_cnt = (pkt_cnt - rp->last_pkt_cnt) &
  2997. (TX_CS_PKT_CNT >> TX_CS_PKT_CNT_SHIFT);
  2998. rp->last_pkt_cnt = tmp;
  2999. cons = rp->cons;
  3000. netif_printk(np, tx_done, KERN_DEBUG, np->dev,
  3001. "%s() pkt_cnt[%u] cons[%d]\n", __func__, pkt_cnt, cons);
  3002. while (pkt_cnt--)
  3003. cons = release_tx_packet(np, rp, cons);
  3004. rp->cons = cons;
  3005. smp_mb();
  3006. out:
  3007. if (unlikely(netif_tx_queue_stopped(txq) &&
  3008. (niu_tx_avail(rp) > NIU_TX_WAKEUP_THRESH(rp)))) {
  3009. __netif_tx_lock(txq, smp_processor_id());
  3010. if (netif_tx_queue_stopped(txq) &&
  3011. (niu_tx_avail(rp) > NIU_TX_WAKEUP_THRESH(rp)))
  3012. netif_tx_wake_queue(txq);
  3013. __netif_tx_unlock(txq);
  3014. }
  3015. }
  3016. static inline void niu_sync_rx_discard_stats(struct niu *np,
  3017. struct rx_ring_info *rp,
  3018. const int limit)
  3019. {
  3020. /* This elaborate scheme is needed for reading the RX discard
  3021. * counters, as they are only 16-bit and can overflow quickly,
  3022. * and because the overflow indication bit is not usable as
  3023. * the counter value does not wrap, but remains at max value
  3024. * 0xFFFF.
  3025. *
  3026. * In theory and in practice counters can be lost in between
  3027. * reading nr64() and clearing the counter nw64(). For this
  3028. * reason, the number of counter clearings nw64() is
  3029. * limited/reduced though the limit parameter.
  3030. */
  3031. int rx_channel = rp->rx_channel;
  3032. u32 misc, wred;
  3033. /* RXMISC (Receive Miscellaneous Discard Count), covers the
  3034. * following discard events: IPP (Input Port Process),
  3035. * FFLP/TCAM, Full RCR (Receive Completion Ring) RBR (Receive
  3036. * Block Ring) prefetch buffer is empty.
  3037. */
  3038. misc = nr64(RXMISC(rx_channel));
  3039. if (unlikely((misc & RXMISC_COUNT) > limit)) {
  3040. nw64(RXMISC(rx_channel), 0);
  3041. rp->rx_errors += misc & RXMISC_COUNT;
  3042. if (unlikely(misc & RXMISC_OFLOW))
  3043. dev_err(np->device, "rx-%d: Counter overflow RXMISC discard\n",
  3044. rx_channel);
  3045. netif_printk(np, rx_err, KERN_DEBUG, np->dev,
  3046. "rx-%d: MISC drop=%u over=%u\n",
  3047. rx_channel, misc, misc-limit);
  3048. }
  3049. /* WRED (Weighted Random Early Discard) by hardware */
  3050. wred = nr64(RED_DIS_CNT(rx_channel));
  3051. if (unlikely((wred & RED_DIS_CNT_COUNT) > limit)) {
  3052. nw64(RED_DIS_CNT(rx_channel), 0);
  3053. rp->rx_dropped += wred & RED_DIS_CNT_COUNT;
  3054. if (unlikely(wred & RED_DIS_CNT_OFLOW))
  3055. dev_err(np->device, "rx-%d: Counter overflow WRED discard\n", rx_channel);
  3056. netif_printk(np, rx_err, KERN_DEBUG, np->dev,
  3057. "rx-%d: WRED drop=%u over=%u\n",
  3058. rx_channel, wred, wred-limit);
  3059. }
  3060. }
  3061. static int niu_rx_work(struct napi_struct *napi, struct niu *np,
  3062. struct rx_ring_info *rp, int budget)
  3063. {
  3064. int qlen, rcr_done = 0, work_done = 0;
  3065. struct rxdma_mailbox *mbox = rp->mbox;
  3066. u64 stat;
  3067. #if 1
  3068. stat = nr64(RX_DMA_CTL_STAT(rp->rx_channel));
  3069. qlen = nr64(RCRSTAT_A(rp->rx_channel)) & RCRSTAT_A_QLEN;
  3070. #else
  3071. stat = le64_to_cpup(&mbox->rx_dma_ctl_stat);
  3072. qlen = (le64_to_cpup(&mbox->rcrstat_a) & RCRSTAT_A_QLEN);
  3073. #endif
  3074. mbox->rx_dma_ctl_stat = 0;
  3075. mbox->rcrstat_a = 0;
  3076. netif_printk(np, rx_status, KERN_DEBUG, np->dev,
  3077. "%s(chan[%d]), stat[%llx] qlen=%d\n",
  3078. __func__, rp->rx_channel, (unsigned long long)stat, qlen);
  3079. rcr_done = work_done = 0;
  3080. qlen = min(qlen, budget);
  3081. while (work_done < qlen) {
  3082. rcr_done += niu_process_rx_pkt(napi, np, rp);
  3083. work_done++;
  3084. }
  3085. if (rp->rbr_refill_pending >= rp->rbr_kick_thresh) {
  3086. unsigned int i;
  3087. for (i = 0; i < rp->rbr_refill_pending; i++)
  3088. niu_rbr_refill(np, rp, GFP_ATOMIC);
  3089. rp->rbr_refill_pending = 0;
  3090. }
  3091. stat = (RX_DMA_CTL_STAT_MEX |
  3092. ((u64)work_done << RX_DMA_CTL_STAT_PKTREAD_SHIFT) |
  3093. ((u64)rcr_done << RX_DMA_CTL_STAT_PTRREAD_SHIFT));
  3094. nw64(RX_DMA_CTL_STAT(rp->rx_channel), stat);
  3095. /* Only sync discards stats when qlen indicate potential for drops */
  3096. if (qlen > 10)
  3097. niu_sync_rx_discard_stats(np, rp, 0x7FFF);
  3098. return work_done;
  3099. }
  3100. static int niu_poll_core(struct niu *np, struct niu_ldg *lp, int budget)
  3101. {
  3102. u64 v0 = lp->v0;
  3103. u32 tx_vec = (v0 >> 32);
  3104. u32 rx_vec = (v0 & 0xffffffff);
  3105. int i, work_done = 0;
  3106. netif_printk(np, intr, KERN_DEBUG, np->dev,
  3107. "%s() v0[%016llx]\n", __func__, (unsigned long long)v0);
  3108. for (i = 0; i < np->num_tx_rings; i++) {
  3109. struct tx_ring_info *rp = &np->tx_rings[i];
  3110. if (tx_vec & (1 << rp->tx_channel))
  3111. niu_tx_work(np, rp);
  3112. nw64(LD_IM0(LDN_TXDMA(rp->tx_channel)), 0);
  3113. }
  3114. for (i = 0; i < np->num_rx_rings; i++) {
  3115. struct rx_ring_info *rp = &np->rx_rings[i];
  3116. if (rx_vec & (1 << rp->rx_channel)) {
  3117. int this_work_done;
  3118. this_work_done = niu_rx_work(&lp->napi, np, rp,
  3119. budget);
  3120. budget -= this_work_done;
  3121. work_done += this_work_done;
  3122. }
  3123. nw64(LD_IM0(LDN_RXDMA(rp->rx_channel)), 0);
  3124. }
  3125. return work_done;
  3126. }
  3127. static int niu_poll(struct napi_struct *napi, int budget)
  3128. {
  3129. struct niu_ldg *lp = container_of(napi, struct niu_ldg, napi);
  3130. struct niu *np = lp->np;
  3131. int work_done;
  3132. work_done = niu_poll_core(np, lp, budget);
  3133. if (work_done < budget) {
  3134. napi_complete(napi);
  3135. niu_ldg_rearm(np, lp, 1);
  3136. }
  3137. return work_done;
  3138. }
  3139. static void niu_log_rxchan_errors(struct niu *np, struct rx_ring_info *rp,
  3140. u64 stat)
  3141. {
  3142. netdev_err(np->dev, "RX channel %u errors ( ", rp->rx_channel);
  3143. if (stat & RX_DMA_CTL_STAT_RBR_TMOUT)
  3144. pr_cont("RBR_TMOUT ");
  3145. if (stat & RX_DMA_CTL_STAT_RSP_CNT_ERR)
  3146. pr_cont("RSP_CNT ");
  3147. if (stat & RX_DMA_CTL_STAT_BYTE_EN_BUS)
  3148. pr_cont("BYTE_EN_BUS ");
  3149. if (stat & RX_DMA_CTL_STAT_RSP_DAT_ERR)
  3150. pr_cont("RSP_DAT ");
  3151. if (stat & RX_DMA_CTL_STAT_RCR_ACK_ERR)
  3152. pr_cont("RCR_ACK ");
  3153. if (stat & RX_DMA_CTL_STAT_RCR_SHA_PAR)
  3154. pr_cont("RCR_SHA_PAR ");
  3155. if (stat & RX_DMA_CTL_STAT_RBR_PRE_PAR)
  3156. pr_cont("RBR_PRE_PAR ");
  3157. if (stat & RX_DMA_CTL_STAT_CONFIG_ERR)
  3158. pr_cont("CONFIG ");
  3159. if (stat & RX_DMA_CTL_STAT_RCRINCON)
  3160. pr_cont("RCRINCON ");
  3161. if (stat & RX_DMA_CTL_STAT_RCRFULL)
  3162. pr_cont("RCRFULL ");
  3163. if (stat & RX_DMA_CTL_STAT_RBRFULL)
  3164. pr_cont("RBRFULL ");
  3165. if (stat & RX_DMA_CTL_STAT_RBRLOGPAGE)
  3166. pr_cont("RBRLOGPAGE ");
  3167. if (stat & RX_DMA_CTL_STAT_CFIGLOGPAGE)
  3168. pr_cont("CFIGLOGPAGE ");
  3169. if (stat & RX_DMA_CTL_STAT_DC_FIFO_ERR)
  3170. pr_cont("DC_FIDO ");
  3171. pr_cont(")\n");
  3172. }
  3173. static int niu_rx_error(struct niu *np, struct rx_ring_info *rp)
  3174. {
  3175. u64 stat = nr64(RX_DMA_CTL_STAT(rp->rx_channel));
  3176. int err = 0;
  3177. if (stat & (RX_DMA_CTL_STAT_CHAN_FATAL |
  3178. RX_DMA_CTL_STAT_PORT_FATAL))
  3179. err = -EINVAL;
  3180. if (err) {
  3181. netdev_err(np->dev, "RX channel %u error, stat[%llx]\n",
  3182. rp->rx_channel,
  3183. (unsigned long long) stat);
  3184. niu_log_rxchan_errors(np, rp, stat);
  3185. }
  3186. nw64(RX_DMA_CTL_STAT(rp->rx_channel),
  3187. stat & RX_DMA_CTL_WRITE_CLEAR_ERRS);
  3188. return err;
  3189. }
  3190. static void niu_log_txchan_errors(struct niu *np, struct tx_ring_info *rp,
  3191. u64 cs)
  3192. {
  3193. netdev_err(np->dev, "TX channel %u errors ( ", rp->tx_channel);
  3194. if (cs & TX_CS_MBOX_ERR)
  3195. pr_cont("MBOX ");
  3196. if (cs & TX_CS_PKT_SIZE_ERR)
  3197. pr_cont("PKT_SIZE ");
  3198. if (cs & TX_CS_TX_RING_OFLOW)
  3199. pr_cont("TX_RING_OFLOW ");
  3200. if (cs & TX_CS_PREF_BUF_PAR_ERR)
  3201. pr_cont("PREF_BUF_PAR ");
  3202. if (cs & TX_CS_NACK_PREF)
  3203. pr_cont("NACK_PREF ");
  3204. if (cs & TX_CS_NACK_PKT_RD)
  3205. pr_cont("NACK_PKT_RD ");
  3206. if (cs & TX_CS_CONF_PART_ERR)
  3207. pr_cont("CONF_PART ");
  3208. if (cs & TX_CS_PKT_PRT_ERR)
  3209. pr_cont("PKT_PTR ");
  3210. pr_cont(")\n");
  3211. }
  3212. static int niu_tx_error(struct niu *np, struct tx_ring_info *rp)
  3213. {
  3214. u64 cs, logh, logl;
  3215. cs = nr64(TX_CS(rp->tx_channel));
  3216. logh = nr64(TX_RNG_ERR_LOGH(rp->tx_channel));
  3217. logl = nr64(TX_RNG_ERR_LOGL(rp->tx_channel));
  3218. netdev_err(np->dev, "TX channel %u error, cs[%llx] logh[%llx] logl[%llx]\n",
  3219. rp->tx_channel,
  3220. (unsigned long long)cs,
  3221. (unsigned long long)logh,
  3222. (unsigned long long)logl);
  3223. niu_log_txchan_errors(np, rp, cs);
  3224. return -ENODEV;
  3225. }
  3226. static int niu_mif_interrupt(struct niu *np)
  3227. {
  3228. u64 mif_status = nr64(MIF_STATUS);
  3229. int phy_mdint = 0;
  3230. if (np->flags & NIU_FLAGS_XMAC) {
  3231. u64 xrxmac_stat = nr64_mac(XRXMAC_STATUS);
  3232. if (xrxmac_stat & XRXMAC_STATUS_PHY_MDINT)
  3233. phy_mdint = 1;
  3234. }
  3235. netdev_err(np->dev, "MIF interrupt, stat[%llx] phy_mdint(%d)\n",
  3236. (unsigned long long)mif_status, phy_mdint);
  3237. return -ENODEV;
  3238. }
  3239. static void niu_xmac_interrupt(struct niu *np)
  3240. {
  3241. struct niu_xmac_stats *mp = &np->mac_stats.xmac;
  3242. u64 val;
  3243. val = nr64_mac(XTXMAC_STATUS);
  3244. if (val & XTXMAC_STATUS_FRAME_CNT_EXP)
  3245. mp->tx_frames += TXMAC_FRM_CNT_COUNT;
  3246. if (val & XTXMAC_STATUS_BYTE_CNT_EXP)
  3247. mp->tx_bytes += TXMAC_BYTE_CNT_COUNT;
  3248. if (val & XTXMAC_STATUS_TXFIFO_XFR_ERR)
  3249. mp->tx_fifo_errors++;
  3250. if (val & XTXMAC_STATUS_TXMAC_OFLOW)
  3251. mp->tx_overflow_errors++;
  3252. if (val & XTXMAC_STATUS_MAX_PSIZE_ERR)
  3253. mp->tx_max_pkt_size_errors++;
  3254. if (val & XTXMAC_STATUS_TXMAC_UFLOW)
  3255. mp->tx_underflow_errors++;
  3256. val = nr64_mac(XRXMAC_STATUS);
  3257. if (val & XRXMAC_STATUS_LCL_FLT_STATUS)
  3258. mp->rx_local_faults++;
  3259. if (val & XRXMAC_STATUS_RFLT_DET)
  3260. mp->rx_remote_faults++;
  3261. if (val & XRXMAC_STATUS_LFLT_CNT_EXP)
  3262. mp->rx_link_faults += LINK_FAULT_CNT_COUNT;
  3263. if (val & XRXMAC_STATUS_ALIGNERR_CNT_EXP)
  3264. mp->rx_align_errors += RXMAC_ALIGN_ERR_CNT_COUNT;
  3265. if (val & XRXMAC_STATUS_RXFRAG_CNT_EXP)
  3266. mp->rx_frags += RXMAC_FRAG_CNT_COUNT;
  3267. if (val & XRXMAC_STATUS_RXMULTF_CNT_EXP)
  3268. mp->rx_mcasts += RXMAC_MC_FRM_CNT_COUNT;
  3269. if (val & XRXMAC_STATUS_RXBCAST_CNT_EXP)
  3270. mp->rx_bcasts += RXMAC_BC_FRM_CNT_COUNT;
  3271. if (val & XRXMAC_STATUS_RXBCAST_CNT_EXP)
  3272. mp->rx_bcasts += RXMAC_BC_FRM_CNT_COUNT;
  3273. if (val & XRXMAC_STATUS_RXHIST1_CNT_EXP)
  3274. mp->rx_hist_cnt1 += RXMAC_HIST_CNT1_COUNT;
  3275. if (val & XRXMAC_STATUS_RXHIST2_CNT_EXP)
  3276. mp->rx_hist_cnt2 += RXMAC_HIST_CNT2_COUNT;
  3277. if (val & XRXMAC_STATUS_RXHIST3_CNT_EXP)
  3278. mp->rx_hist_cnt3 += RXMAC_HIST_CNT3_COUNT;
  3279. if (val & XRXMAC_STATUS_RXHIST4_CNT_EXP)
  3280. mp->rx_hist_cnt4 += RXMAC_HIST_CNT4_COUNT;
  3281. if (val & XRXMAC_STATUS_RXHIST5_CNT_EXP)
  3282. mp->rx_hist_cnt5 += RXMAC_HIST_CNT5_COUNT;
  3283. if (val & XRXMAC_STATUS_RXHIST6_CNT_EXP)
  3284. mp->rx_hist_cnt6 += RXMAC_HIST_CNT6_COUNT;
  3285. if (val & XRXMAC_STATUS_RXHIST7_CNT_EXP)
  3286. mp->rx_hist_cnt7 += RXMAC_HIST_CNT7_COUNT;
  3287. if (val & XRXMAC_STATUS_RXOCTET_CNT_EXP)
  3288. mp->rx_octets += RXMAC_BT_CNT_COUNT;
  3289. if (val & XRXMAC_STATUS_CVIOLERR_CNT_EXP)
  3290. mp->rx_code_violations += RXMAC_CD_VIO_CNT_COUNT;
  3291. if (val & XRXMAC_STATUS_LENERR_CNT_EXP)
  3292. mp->rx_len_errors += RXMAC_MPSZER_CNT_COUNT;
  3293. if (val & XRXMAC_STATUS_CRCERR_CNT_EXP)
  3294. mp->rx_crc_errors += RXMAC_CRC_ER_CNT_COUNT;
  3295. if (val & XRXMAC_STATUS_RXUFLOW)
  3296. mp->rx_underflows++;
  3297. if (val & XRXMAC_STATUS_RXOFLOW)
  3298. mp->rx_overflows++;
  3299. val = nr64_mac(XMAC_FC_STAT);
  3300. if (val & XMAC_FC_STAT_TX_MAC_NPAUSE)
  3301. mp->pause_off_state++;
  3302. if (val & XMAC_FC_STAT_TX_MAC_PAUSE)
  3303. mp->pause_on_state++;
  3304. if (val & XMAC_FC_STAT_RX_MAC_RPAUSE)
  3305. mp->pause_received++;
  3306. }
  3307. static void niu_bmac_interrupt(struct niu *np)
  3308. {
  3309. struct niu_bmac_stats *mp = &np->mac_stats.bmac;
  3310. u64 val;
  3311. val = nr64_mac(BTXMAC_STATUS);
  3312. if (val & BTXMAC_STATUS_UNDERRUN)
  3313. mp->tx_underflow_errors++;
  3314. if (val & BTXMAC_STATUS_MAX_PKT_ERR)
  3315. mp->tx_max_pkt_size_errors++;
  3316. if (val & BTXMAC_STATUS_BYTE_CNT_EXP)
  3317. mp->tx_bytes += BTXMAC_BYTE_CNT_COUNT;
  3318. if (val & BTXMAC_STATUS_FRAME_CNT_EXP)
  3319. mp->tx_frames += BTXMAC_FRM_CNT_COUNT;
  3320. val = nr64_mac(BRXMAC_STATUS);
  3321. if (val & BRXMAC_STATUS_OVERFLOW)
  3322. mp->rx_overflows++;
  3323. if (val & BRXMAC_STATUS_FRAME_CNT_EXP)
  3324. mp->rx_frames += BRXMAC_FRAME_CNT_COUNT;
  3325. if (val & BRXMAC_STATUS_ALIGN_ERR_EXP)
  3326. mp->rx_align_errors += BRXMAC_ALIGN_ERR_CNT_COUNT;
  3327. if (val & BRXMAC_STATUS_CRC_ERR_EXP)
  3328. mp->rx_crc_errors += BRXMAC_ALIGN_ERR_CNT_COUNT;
  3329. if (val & BRXMAC_STATUS_LEN_ERR_EXP)
  3330. mp->rx_len_errors += BRXMAC_CODE_VIOL_ERR_CNT_COUNT;
  3331. val = nr64_mac(BMAC_CTRL_STATUS);
  3332. if (val & BMAC_CTRL_STATUS_NOPAUSE)
  3333. mp->pause_off_state++;
  3334. if (val & BMAC_CTRL_STATUS_PAUSE)
  3335. mp->pause_on_state++;
  3336. if (val & BMAC_CTRL_STATUS_PAUSE_RECV)
  3337. mp->pause_received++;
  3338. }
  3339. static int niu_mac_interrupt(struct niu *np)
  3340. {
  3341. if (np->flags & NIU_FLAGS_XMAC)
  3342. niu_xmac_interrupt(np);
  3343. else
  3344. niu_bmac_interrupt(np);
  3345. return 0;
  3346. }
  3347. static void niu_log_device_error(struct niu *np, u64 stat)
  3348. {
  3349. netdev_err(np->dev, "Core device errors ( ");
  3350. if (stat & SYS_ERR_MASK_META2)
  3351. pr_cont("META2 ");
  3352. if (stat & SYS_ERR_MASK_META1)
  3353. pr_cont("META1 ");
  3354. if (stat & SYS_ERR_MASK_PEU)
  3355. pr_cont("PEU ");
  3356. if (stat & SYS_ERR_MASK_TXC)
  3357. pr_cont("TXC ");
  3358. if (stat & SYS_ERR_MASK_RDMC)
  3359. pr_cont("RDMC ");
  3360. if (stat & SYS_ERR_MASK_TDMC)
  3361. pr_cont("TDMC ");
  3362. if (stat & SYS_ERR_MASK_ZCP)
  3363. pr_cont("ZCP ");
  3364. if (stat & SYS_ERR_MASK_FFLP)
  3365. pr_cont("FFLP ");
  3366. if (stat & SYS_ERR_MASK_IPP)
  3367. pr_cont("IPP ");
  3368. if (stat & SYS_ERR_MASK_MAC)
  3369. pr_cont("MAC ");
  3370. if (stat & SYS_ERR_MASK_SMX)
  3371. pr_cont("SMX ");
  3372. pr_cont(")\n");
  3373. }
  3374. static int niu_device_error(struct niu *np)
  3375. {
  3376. u64 stat = nr64(SYS_ERR_STAT);
  3377. netdev_err(np->dev, "Core device error, stat[%llx]\n",
  3378. (unsigned long long)stat);
  3379. niu_log_device_error(np, stat);
  3380. return -ENODEV;
  3381. }
  3382. static int niu_slowpath_interrupt(struct niu *np, struct niu_ldg *lp,
  3383. u64 v0, u64 v1, u64 v2)
  3384. {
  3385. int i, err = 0;
  3386. lp->v0 = v0;
  3387. lp->v1 = v1;
  3388. lp->v2 = v2;
  3389. if (v1 & 0x00000000ffffffffULL) {
  3390. u32 rx_vec = (v1 & 0xffffffff);
  3391. for (i = 0; i < np->num_rx_rings; i++) {
  3392. struct rx_ring_info *rp = &np->rx_rings[i];
  3393. if (rx_vec & (1 << rp->rx_channel)) {
  3394. int r = niu_rx_error(np, rp);
  3395. if (r) {
  3396. err = r;
  3397. } else {
  3398. if (!v0)
  3399. nw64(RX_DMA_CTL_STAT(rp->rx_channel),
  3400. RX_DMA_CTL_STAT_MEX);
  3401. }
  3402. }
  3403. }
  3404. }
  3405. if (v1 & 0x7fffffff00000000ULL) {
  3406. u32 tx_vec = (v1 >> 32) & 0x7fffffff;
  3407. for (i = 0; i < np->num_tx_rings; i++) {
  3408. struct tx_ring_info *rp = &np->tx_rings[i];
  3409. if (tx_vec & (1 << rp->tx_channel)) {
  3410. int r = niu_tx_error(np, rp);
  3411. if (r)
  3412. err = r;
  3413. }
  3414. }
  3415. }
  3416. if ((v0 | v1) & 0x8000000000000000ULL) {
  3417. int r = niu_mif_interrupt(np);
  3418. if (r)
  3419. err = r;
  3420. }
  3421. if (v2) {
  3422. if (v2 & 0x01ef) {
  3423. int r = niu_mac_interrupt(np);
  3424. if (r)
  3425. err = r;
  3426. }
  3427. if (v2 & 0x0210) {
  3428. int r = niu_device_error(np);
  3429. if (r)
  3430. err = r;
  3431. }
  3432. }
  3433. if (err)
  3434. niu_enable_interrupts(np, 0);
  3435. return err;
  3436. }
  3437. static void niu_rxchan_intr(struct niu *np, struct rx_ring_info *rp,
  3438. int ldn)
  3439. {
  3440. struct rxdma_mailbox *mbox = rp->mbox;
  3441. u64 stat_write, stat = le64_to_cpup(&mbox->rx_dma_ctl_stat);
  3442. stat_write = (RX_DMA_CTL_STAT_RCRTHRES |
  3443. RX_DMA_CTL_STAT_RCRTO);
  3444. nw64(RX_DMA_CTL_STAT(rp->rx_channel), stat_write);
  3445. netif_printk(np, intr, KERN_DEBUG, np->dev,
  3446. "%s() stat[%llx]\n", __func__, (unsigned long long)stat);
  3447. }
  3448. static void niu_txchan_intr(struct niu *np, struct tx_ring_info *rp,
  3449. int ldn)
  3450. {
  3451. rp->tx_cs = nr64(TX_CS(rp->tx_channel));
  3452. netif_printk(np, intr, KERN_DEBUG, np->dev,
  3453. "%s() cs[%llx]\n", __func__, (unsigned long long)rp->tx_cs);
  3454. }
  3455. static void __niu_fastpath_interrupt(struct niu *np, int ldg, u64 v0)
  3456. {
  3457. struct niu_parent *parent = np->parent;
  3458. u32 rx_vec, tx_vec;
  3459. int i;
  3460. tx_vec = (v0 >> 32);
  3461. rx_vec = (v0 & 0xffffffff);
  3462. for (i = 0; i < np->num_rx_rings; i++) {
  3463. struct rx_ring_info *rp = &np->rx_rings[i];
  3464. int ldn = LDN_RXDMA(rp->rx_channel);
  3465. if (parent->ldg_map[ldn] != ldg)
  3466. continue;
  3467. nw64(LD_IM0(ldn), LD_IM0_MASK);
  3468. if (rx_vec & (1 << rp->rx_channel))
  3469. niu_rxchan_intr(np, rp, ldn);
  3470. }
  3471. for (i = 0; i < np->num_tx_rings; i++) {
  3472. struct tx_ring_info *rp = &np->tx_rings[i];
  3473. int ldn = LDN_TXDMA(rp->tx_channel);
  3474. if (parent->ldg_map[ldn] != ldg)
  3475. continue;
  3476. nw64(LD_IM0(ldn), LD_IM0_MASK);
  3477. if (tx_vec & (1 << rp->tx_channel))
  3478. niu_txchan_intr(np, rp, ldn);
  3479. }
  3480. }
  3481. static void niu_schedule_napi(struct niu *np, struct niu_ldg *lp,
  3482. u64 v0, u64 v1, u64 v2)
  3483. {
  3484. if (likely(napi_schedule_prep(&lp->napi))) {
  3485. lp->v0 = v0;
  3486. lp->v1 = v1;
  3487. lp->v2 = v2;
  3488. __niu_fastpath_interrupt(np, lp->ldg_num, v0);
  3489. __napi_schedule(&lp->napi);
  3490. }
  3491. }
  3492. static irqreturn_t niu_interrupt(int irq, void *dev_id)
  3493. {
  3494. struct niu_ldg *lp = dev_id;
  3495. struct niu *np = lp->np;
  3496. int ldg = lp->ldg_num;
  3497. unsigned long flags;
  3498. u64 v0, v1, v2;
  3499. if (netif_msg_intr(np))
  3500. printk(KERN_DEBUG KBUILD_MODNAME ": " "%s() ldg[%p](%d)",
  3501. __func__, lp, ldg);
  3502. spin_lock_irqsave(&np->lock, flags);
  3503. v0 = nr64(LDSV0(ldg));
  3504. v1 = nr64(LDSV1(ldg));
  3505. v2 = nr64(LDSV2(ldg));
  3506. if (netif_msg_intr(np))
  3507. pr_cont(" v0[%llx] v1[%llx] v2[%llx]\n",
  3508. (unsigned long long) v0,
  3509. (unsigned long long) v1,
  3510. (unsigned long long) v2);
  3511. if (unlikely(!v0 && !v1 && !v2)) {
  3512. spin_unlock_irqrestore(&np->lock, flags);
  3513. return IRQ_NONE;
  3514. }
  3515. if (unlikely((v0 & ((u64)1 << LDN_MIF)) || v1 || v2)) {
  3516. int err = niu_slowpath_interrupt(np, lp, v0, v1, v2);
  3517. if (err)
  3518. goto out;
  3519. }
  3520. if (likely(v0 & ~((u64)1 << LDN_MIF)))
  3521. niu_schedule_napi(np, lp, v0, v1, v2);
  3522. else
  3523. niu_ldg_rearm(np, lp, 1);
  3524. out:
  3525. spin_unlock_irqrestore(&np->lock, flags);
  3526. return IRQ_HANDLED;
  3527. }
  3528. static void niu_free_rx_ring_info(struct niu *np, struct rx_ring_info *rp)
  3529. {
  3530. if (rp->mbox) {
  3531. np->ops->free_coherent(np->device,
  3532. sizeof(struct rxdma_mailbox),
  3533. rp->mbox, rp->mbox_dma);
  3534. rp->mbox = NULL;
  3535. }
  3536. if (rp->rcr) {
  3537. np->ops->free_coherent(np->device,
  3538. MAX_RCR_RING_SIZE * sizeof(__le64),
  3539. rp->rcr, rp->rcr_dma);
  3540. rp->rcr = NULL;
  3541. rp->rcr_table_size = 0;
  3542. rp->rcr_index = 0;
  3543. }
  3544. if (rp->rbr) {
  3545. niu_rbr_free(np, rp);
  3546. np->ops->free_coherent(np->device,
  3547. MAX_RBR_RING_SIZE * sizeof(__le32),
  3548. rp->rbr, rp->rbr_dma);
  3549. rp->rbr = NULL;
  3550. rp->rbr_table_size = 0;
  3551. rp->rbr_index = 0;
  3552. }
  3553. kfree(rp->rxhash);
  3554. rp->rxhash = NULL;
  3555. }
  3556. static void niu_free_tx_ring_info(struct niu *np, struct tx_ring_info *rp)
  3557. {
  3558. if (rp->mbox) {
  3559. np->ops->free_coherent(np->device,
  3560. sizeof(struct txdma_mailbox),
  3561. rp->mbox, rp->mbox_dma);
  3562. rp->mbox = NULL;
  3563. }
  3564. if (rp->descr) {
  3565. int i;
  3566. for (i = 0; i < MAX_TX_RING_SIZE; i++) {
  3567. if (rp->tx_buffs[i].skb)
  3568. (void) release_tx_packet(np, rp, i);
  3569. }
  3570. np->ops->free_coherent(np->device,
  3571. MAX_TX_RING_SIZE * sizeof(__le64),
  3572. rp->descr, rp->descr_dma);
  3573. rp->descr = NULL;
  3574. rp->pending = 0;
  3575. rp->prod = 0;
  3576. rp->cons = 0;
  3577. rp->wrap_bit = 0;
  3578. }
  3579. }
  3580. static void niu_free_channels(struct niu *np)
  3581. {
  3582. int i;
  3583. if (np->rx_rings) {
  3584. for (i = 0; i < np->num_rx_rings; i++) {
  3585. struct rx_ring_info *rp = &np->rx_rings[i];
  3586. niu_free_rx_ring_info(np, rp);
  3587. }
  3588. kfree(np->rx_rings);
  3589. np->rx_rings = NULL;
  3590. np->num_rx_rings = 0;
  3591. }
  3592. if (np->tx_rings) {
  3593. for (i = 0; i < np->num_tx_rings; i++) {
  3594. struct tx_ring_info *rp = &np->tx_rings[i];
  3595. niu_free_tx_ring_info(np, rp);
  3596. }
  3597. kfree(np->tx_rings);
  3598. np->tx_rings = NULL;
  3599. np->num_tx_rings = 0;
  3600. }
  3601. }
  3602. static int niu_alloc_rx_ring_info(struct niu *np,
  3603. struct rx_ring_info *rp)
  3604. {
  3605. BUILD_BUG_ON(sizeof(struct rxdma_mailbox) != 64);
  3606. rp->rxhash = kzalloc(MAX_RBR_RING_SIZE * sizeof(struct page *),
  3607. GFP_KERNEL);
  3608. if (!rp->rxhash)
  3609. return -ENOMEM;
  3610. rp->mbox = np->ops->alloc_coherent(np->device,
  3611. sizeof(struct rxdma_mailbox),
  3612. &rp->mbox_dma, GFP_KERNEL);
  3613. if (!rp->mbox)
  3614. return -ENOMEM;
  3615. if ((unsigned long)rp->mbox & (64UL - 1)) {
  3616. netdev_err(np->dev, "Coherent alloc gives misaligned RXDMA mailbox %p\n",
  3617. rp->mbox);
  3618. return -EINVAL;
  3619. }
  3620. rp->rcr = np->ops->alloc_coherent(np->device,
  3621. MAX_RCR_RING_SIZE * sizeof(__le64),
  3622. &rp->rcr_dma, GFP_KERNEL);
  3623. if (!rp->rcr)
  3624. return -ENOMEM;
  3625. if ((unsigned long)rp->rcr & (64UL - 1)) {
  3626. netdev_err(np->dev, "Coherent alloc gives misaligned RXDMA RCR table %p\n",
  3627. rp->rcr);
  3628. return -EINVAL;
  3629. }
  3630. rp->rcr_table_size = MAX_RCR_RING_SIZE;
  3631. rp->rcr_index = 0;
  3632. rp->rbr = np->ops->alloc_coherent(np->device,
  3633. MAX_RBR_RING_SIZE * sizeof(__le32),
  3634. &rp->rbr_dma, GFP_KERNEL);
  3635. if (!rp->rbr)
  3636. return -ENOMEM;
  3637. if ((unsigned long)rp->rbr & (64UL - 1)) {
  3638. netdev_err(np->dev, "Coherent alloc gives misaligned RXDMA RBR table %p\n",
  3639. rp->rbr);
  3640. return -EINVAL;
  3641. }
  3642. rp->rbr_table_size = MAX_RBR_RING_SIZE;
  3643. rp->rbr_index = 0;
  3644. rp->rbr_pending = 0;
  3645. return 0;
  3646. }
  3647. static void niu_set_max_burst(struct niu *np, struct tx_ring_info *rp)
  3648. {
  3649. int mtu = np->dev->mtu;
  3650. /* These values are recommended by the HW designers for fair
  3651. * utilization of DRR amongst the rings.
  3652. */
  3653. rp->max_burst = mtu + 32;
  3654. if (rp->max_burst > 4096)
  3655. rp->max_burst = 4096;
  3656. }
  3657. static int niu_alloc_tx_ring_info(struct niu *np,
  3658. struct tx_ring_info *rp)
  3659. {
  3660. BUILD_BUG_ON(sizeof(struct txdma_mailbox) != 64);
  3661. rp->mbox = np->ops->alloc_coherent(np->device,
  3662. sizeof(struct txdma_mailbox),
  3663. &rp->mbox_dma, GFP_KERNEL);
  3664. if (!rp->mbox)
  3665. return -ENOMEM;
  3666. if ((unsigned long)rp->mbox & (64UL - 1)) {
  3667. netdev_err(np->dev, "Coherent alloc gives misaligned TXDMA mailbox %p\n",
  3668. rp->mbox);
  3669. return -EINVAL;
  3670. }
  3671. rp->descr = np->ops->alloc_coherent(np->device,
  3672. MAX_TX_RING_SIZE * sizeof(__le64),
  3673. &rp->descr_dma, GFP_KERNEL);
  3674. if (!rp->descr)
  3675. return -ENOMEM;
  3676. if ((unsigned long)rp->descr & (64UL - 1)) {
  3677. netdev_err(np->dev, "Coherent alloc gives misaligned TXDMA descr table %p\n",
  3678. rp->descr);
  3679. return -EINVAL;
  3680. }
  3681. rp->pending = MAX_TX_RING_SIZE;
  3682. rp->prod = 0;
  3683. rp->cons = 0;
  3684. rp->wrap_bit = 0;
  3685. /* XXX make these configurable... XXX */
  3686. rp->mark_freq = rp->pending / 4;
  3687. niu_set_max_burst(np, rp);
  3688. return 0;
  3689. }
  3690. static void niu_size_rbr(struct niu *np, struct rx_ring_info *rp)
  3691. {
  3692. u16 bss;
  3693. bss = min(PAGE_SHIFT, 15);
  3694. rp->rbr_block_size = 1 << bss;
  3695. rp->rbr_blocks_per_page = 1 << (PAGE_SHIFT-bss);
  3696. rp->rbr_sizes[0] = 256;
  3697. rp->rbr_sizes[1] = 1024;
  3698. if (np->dev->mtu > ETH_DATA_LEN) {
  3699. switch (PAGE_SIZE) {
  3700. case 4 * 1024:
  3701. rp->rbr_sizes[2] = 4096;
  3702. break;
  3703. default:
  3704. rp->rbr_sizes[2] = 8192;
  3705. break;
  3706. }
  3707. } else {
  3708. rp->rbr_sizes[2] = 2048;
  3709. }
  3710. rp->rbr_sizes[3] = rp->rbr_block_size;
  3711. }
  3712. static int niu_alloc_channels(struct niu *np)
  3713. {
  3714. struct niu_parent *parent = np->parent;
  3715. int first_rx_channel, first_tx_channel;
  3716. int num_rx_rings, num_tx_rings;
  3717. struct rx_ring_info *rx_rings;
  3718. struct tx_ring_info *tx_rings;
  3719. int i, port, err;
  3720. port = np->port;
  3721. first_rx_channel = first_tx_channel = 0;
  3722. for (i = 0; i < port; i++) {
  3723. first_rx_channel += parent->rxchan_per_port[i];
  3724. first_tx_channel += parent->txchan_per_port[i];
  3725. }
  3726. num_rx_rings = parent->rxchan_per_port[port];
  3727. num_tx_rings = parent->txchan_per_port[port];
  3728. rx_rings = kcalloc(num_rx_rings, sizeof(struct rx_ring_info),
  3729. GFP_KERNEL);
  3730. err = -ENOMEM;
  3731. if (!rx_rings)
  3732. goto out_err;
  3733. np->num_rx_rings = num_rx_rings;
  3734. smp_wmb();
  3735. np->rx_rings = rx_rings;
  3736. netif_set_real_num_rx_queues(np->dev, num_rx_rings);
  3737. for (i = 0; i < np->num_rx_rings; i++) {
  3738. struct rx_ring_info *rp = &np->rx_rings[i];
  3739. rp->np = np;
  3740. rp->rx_channel = first_rx_channel + i;
  3741. err = niu_alloc_rx_ring_info(np, rp);
  3742. if (err)
  3743. goto out_err;
  3744. niu_size_rbr(np, rp);
  3745. /* XXX better defaults, configurable, etc... XXX */
  3746. rp->nonsyn_window = 64;
  3747. rp->nonsyn_threshold = rp->rcr_table_size - 64;
  3748. rp->syn_window = 64;
  3749. rp->syn_threshold = rp->rcr_table_size - 64;
  3750. rp->rcr_pkt_threshold = 16;
  3751. rp->rcr_timeout = 8;
  3752. rp->rbr_kick_thresh = RBR_REFILL_MIN;
  3753. if (rp->rbr_kick_thresh < rp->rbr_blocks_per_page)
  3754. rp->rbr_kick_thresh = rp->rbr_blocks_per_page;
  3755. err = niu_rbr_fill(np, rp, GFP_KERNEL);
  3756. if (err)
  3757. return err;
  3758. }
  3759. tx_rings = kcalloc(num_tx_rings, sizeof(struct tx_ring_info),
  3760. GFP_KERNEL);
  3761. err = -ENOMEM;
  3762. if (!tx_rings)
  3763. goto out_err;
  3764. np->num_tx_rings = num_tx_rings;
  3765. smp_wmb();
  3766. np->tx_rings = tx_rings;
  3767. netif_set_real_num_tx_queues(np->dev, num_tx_rings);
  3768. for (i = 0; i < np->num_tx_rings; i++) {
  3769. struct tx_ring_info *rp = &np->tx_rings[i];
  3770. rp->np = np;
  3771. rp->tx_channel = first_tx_channel + i;
  3772. err = niu_alloc_tx_ring_info(np, rp);
  3773. if (err)
  3774. goto out_err;
  3775. }
  3776. return 0;
  3777. out_err:
  3778. niu_free_channels(np);
  3779. return err;
  3780. }
  3781. static int niu_tx_cs_sng_poll(struct niu *np, int channel)
  3782. {
  3783. int limit = 1000;
  3784. while (--limit > 0) {
  3785. u64 val = nr64(TX_CS(channel));
  3786. if (val & TX_CS_SNG_STATE)
  3787. return 0;
  3788. }
  3789. return -ENODEV;
  3790. }
  3791. static int niu_tx_channel_stop(struct niu *np, int channel)
  3792. {
  3793. u64 val = nr64(TX_CS(channel));
  3794. val |= TX_CS_STOP_N_GO;
  3795. nw64(TX_CS(channel), val);
  3796. return niu_tx_cs_sng_poll(np, channel);
  3797. }
  3798. static int niu_tx_cs_reset_poll(struct niu *np, int channel)
  3799. {
  3800. int limit = 1000;
  3801. while (--limit > 0) {
  3802. u64 val = nr64(TX_CS(channel));
  3803. if (!(val & TX_CS_RST))
  3804. return 0;
  3805. }
  3806. return -ENODEV;
  3807. }
  3808. static int niu_tx_channel_reset(struct niu *np, int channel)
  3809. {
  3810. u64 val = nr64(TX_CS(channel));
  3811. int err;
  3812. val |= TX_CS_RST;
  3813. nw64(TX_CS(channel), val);
  3814. err = niu_tx_cs_reset_poll(np, channel);
  3815. if (!err)
  3816. nw64(TX_RING_KICK(channel), 0);
  3817. return err;
  3818. }
  3819. static int niu_tx_channel_lpage_init(struct niu *np, int channel)
  3820. {
  3821. u64 val;
  3822. nw64(TX_LOG_MASK1(channel), 0);
  3823. nw64(TX_LOG_VAL1(channel), 0);
  3824. nw64(TX_LOG_MASK2(channel), 0);
  3825. nw64(TX_LOG_VAL2(channel), 0);
  3826. nw64(TX_LOG_PAGE_RELO1(channel), 0);
  3827. nw64(TX_LOG_PAGE_RELO2(channel), 0);
  3828. nw64(TX_LOG_PAGE_HDL(channel), 0);
  3829. val = (u64)np->port << TX_LOG_PAGE_VLD_FUNC_SHIFT;
  3830. val |= (TX_LOG_PAGE_VLD_PAGE0 | TX_LOG_PAGE_VLD_PAGE1);
  3831. nw64(TX_LOG_PAGE_VLD(channel), val);
  3832. /* XXX TXDMA 32bit mode? XXX */
  3833. return 0;
  3834. }
  3835. static void niu_txc_enable_port(struct niu *np, int on)
  3836. {
  3837. unsigned long flags;
  3838. u64 val, mask;
  3839. niu_lock_parent(np, flags);
  3840. val = nr64(TXC_CONTROL);
  3841. mask = (u64)1 << np->port;
  3842. if (on) {
  3843. val |= TXC_CONTROL_ENABLE | mask;
  3844. } else {
  3845. val &= ~mask;
  3846. if ((val & ~TXC_CONTROL_ENABLE) == 0)
  3847. val &= ~TXC_CONTROL_ENABLE;
  3848. }
  3849. nw64(TXC_CONTROL, val);
  3850. niu_unlock_parent(np, flags);
  3851. }
  3852. static void niu_txc_set_imask(struct niu *np, u64 imask)
  3853. {
  3854. unsigned long flags;
  3855. u64 val;
  3856. niu_lock_parent(np, flags);
  3857. val = nr64(TXC_INT_MASK);
  3858. val &= ~TXC_INT_MASK_VAL(np->port);
  3859. val |= (imask << TXC_INT_MASK_VAL_SHIFT(np->port));
  3860. niu_unlock_parent(np, flags);
  3861. }
  3862. static void niu_txc_port_dma_enable(struct niu *np, int on)
  3863. {
  3864. u64 val = 0;
  3865. if (on) {
  3866. int i;
  3867. for (i = 0; i < np->num_tx_rings; i++)
  3868. val |= (1 << np->tx_rings[i].tx_channel);
  3869. }
  3870. nw64(TXC_PORT_DMA(np->port), val);
  3871. }
  3872. static int niu_init_one_tx_channel(struct niu *np, struct tx_ring_info *rp)
  3873. {
  3874. int err, channel = rp->tx_channel;
  3875. u64 val, ring_len;
  3876. err = niu_tx_channel_stop(np, channel);
  3877. if (err)
  3878. return err;
  3879. err = niu_tx_channel_reset(np, channel);
  3880. if (err)
  3881. return err;
  3882. err = niu_tx_channel_lpage_init(np, channel);
  3883. if (err)
  3884. return err;
  3885. nw64(TXC_DMA_MAX(channel), rp->max_burst);
  3886. nw64(TX_ENT_MSK(channel), 0);
  3887. if (rp->descr_dma & ~(TX_RNG_CFIG_STADDR_BASE |
  3888. TX_RNG_CFIG_STADDR)) {
  3889. netdev_err(np->dev, "TX ring channel %d DMA addr (%llx) is not aligned\n",
  3890. channel, (unsigned long long)rp->descr_dma);
  3891. return -EINVAL;
  3892. }
  3893. /* The length field in TX_RNG_CFIG is measured in 64-byte
  3894. * blocks. rp->pending is the number of TX descriptors in
  3895. * our ring, 8 bytes each, thus we divide by 8 bytes more
  3896. * to get the proper value the chip wants.
  3897. */
  3898. ring_len = (rp->pending / 8);
  3899. val = ((ring_len << TX_RNG_CFIG_LEN_SHIFT) |
  3900. rp->descr_dma);
  3901. nw64(TX_RNG_CFIG(channel), val);
  3902. if (((rp->mbox_dma >> 32) & ~TXDMA_MBH_MBADDR) ||
  3903. ((u32)rp->mbox_dma & ~TXDMA_MBL_MBADDR)) {
  3904. netdev_err(np->dev, "TX ring channel %d MBOX addr (%llx) has invalid bits\n",
  3905. channel, (unsigned long long)rp->mbox_dma);
  3906. return -EINVAL;
  3907. }
  3908. nw64(TXDMA_MBH(channel), rp->mbox_dma >> 32);
  3909. nw64(TXDMA_MBL(channel), rp->mbox_dma & TXDMA_MBL_MBADDR);
  3910. nw64(TX_CS(channel), 0);
  3911. rp->last_pkt_cnt = 0;
  3912. return 0;
  3913. }
  3914. static void niu_init_rdc_groups(struct niu *np)
  3915. {
  3916. struct niu_rdc_tables *tp = &np->parent->rdc_group_cfg[np->port];
  3917. int i, first_table_num = tp->first_table_num;
  3918. for (i = 0; i < tp->num_tables; i++) {
  3919. struct rdc_table *tbl = &tp->tables[i];
  3920. int this_table = first_table_num + i;
  3921. int slot;
  3922. for (slot = 0; slot < NIU_RDC_TABLE_SLOTS; slot++)
  3923. nw64(RDC_TBL(this_table, slot),
  3924. tbl->rxdma_channel[slot]);
  3925. }
  3926. nw64(DEF_RDC(np->port), np->parent->rdc_default[np->port]);
  3927. }
  3928. static void niu_init_drr_weight(struct niu *np)
  3929. {
  3930. int type = phy_decode(np->parent->port_phy, np->port);
  3931. u64 val;
  3932. switch (type) {
  3933. case PORT_TYPE_10G:
  3934. val = PT_DRR_WEIGHT_DEFAULT_10G;
  3935. break;
  3936. case PORT_TYPE_1G:
  3937. default:
  3938. val = PT_DRR_WEIGHT_DEFAULT_1G;
  3939. break;
  3940. }
  3941. nw64(PT_DRR_WT(np->port), val);
  3942. }
  3943. static int niu_init_hostinfo(struct niu *np)
  3944. {
  3945. struct niu_parent *parent = np->parent;
  3946. struct niu_rdc_tables *tp = &parent->rdc_group_cfg[np->port];
  3947. int i, err, num_alt = niu_num_alt_addr(np);
  3948. int first_rdc_table = tp->first_table_num;
  3949. err = niu_set_primary_mac_rdc_table(np, first_rdc_table, 1);
  3950. if (err)
  3951. return err;
  3952. err = niu_set_multicast_mac_rdc_table(np, first_rdc_table, 1);
  3953. if (err)
  3954. return err;
  3955. for (i = 0; i < num_alt; i++) {
  3956. err = niu_set_alt_mac_rdc_table(np, i, first_rdc_table, 1);
  3957. if (err)
  3958. return err;
  3959. }
  3960. return 0;
  3961. }
  3962. static int niu_rx_channel_reset(struct niu *np, int channel)
  3963. {
  3964. return niu_set_and_wait_clear(np, RXDMA_CFIG1(channel),
  3965. RXDMA_CFIG1_RST, 1000, 10,
  3966. "RXDMA_CFIG1");
  3967. }
  3968. static int niu_rx_channel_lpage_init(struct niu *np, int channel)
  3969. {
  3970. u64 val;
  3971. nw64(RX_LOG_MASK1(channel), 0);
  3972. nw64(RX_LOG_VAL1(channel), 0);
  3973. nw64(RX_LOG_MASK2(channel), 0);
  3974. nw64(RX_LOG_VAL2(channel), 0);
  3975. nw64(RX_LOG_PAGE_RELO1(channel), 0);
  3976. nw64(RX_LOG_PAGE_RELO2(channel), 0);
  3977. nw64(RX_LOG_PAGE_HDL(channel), 0);
  3978. val = (u64)np->port << RX_LOG_PAGE_VLD_FUNC_SHIFT;
  3979. val |= (RX_LOG_PAGE_VLD_PAGE0 | RX_LOG_PAGE_VLD_PAGE1);
  3980. nw64(RX_LOG_PAGE_VLD(channel), val);
  3981. return 0;
  3982. }
  3983. static void niu_rx_channel_wred_init(struct niu *np, struct rx_ring_info *rp)
  3984. {
  3985. u64 val;
  3986. val = (((u64)rp->nonsyn_window << RDC_RED_PARA_WIN_SHIFT) |
  3987. ((u64)rp->nonsyn_threshold << RDC_RED_PARA_THRE_SHIFT) |
  3988. ((u64)rp->syn_window << RDC_RED_PARA_WIN_SYN_SHIFT) |
  3989. ((u64)rp->syn_threshold << RDC_RED_PARA_THRE_SYN_SHIFT));
  3990. nw64(RDC_RED_PARA(rp->rx_channel), val);
  3991. }
  3992. static int niu_compute_rbr_cfig_b(struct rx_ring_info *rp, u64 *ret)
  3993. {
  3994. u64 val = 0;
  3995. *ret = 0;
  3996. switch (rp->rbr_block_size) {
  3997. case 4 * 1024:
  3998. val |= (RBR_BLKSIZE_4K << RBR_CFIG_B_BLKSIZE_SHIFT);
  3999. break;
  4000. case 8 * 1024:
  4001. val |= (RBR_BLKSIZE_8K << RBR_CFIG_B_BLKSIZE_SHIFT);
  4002. break;
  4003. case 16 * 1024:
  4004. val |= (RBR_BLKSIZE_16K << RBR_CFIG_B_BLKSIZE_SHIFT);
  4005. break;
  4006. case 32 * 1024:
  4007. val |= (RBR_BLKSIZE_32K << RBR_CFIG_B_BLKSIZE_SHIFT);
  4008. break;
  4009. default:
  4010. return -EINVAL;
  4011. }
  4012. val |= RBR_CFIG_B_VLD2;
  4013. switch (rp->rbr_sizes[2]) {
  4014. case 2 * 1024:
  4015. val |= (RBR_BUFSZ2_2K << RBR_CFIG_B_BUFSZ2_SHIFT);
  4016. break;
  4017. case 4 * 1024:
  4018. val |= (RBR_BUFSZ2_4K << RBR_CFIG_B_BUFSZ2_SHIFT);
  4019. break;
  4020. case 8 * 1024:
  4021. val |= (RBR_BUFSZ2_8K << RBR_CFIG_B_BUFSZ2_SHIFT);
  4022. break;
  4023. case 16 * 1024:
  4024. val |= (RBR_BUFSZ2_16K << RBR_CFIG_B_BUFSZ2_SHIFT);
  4025. break;
  4026. default:
  4027. return -EINVAL;
  4028. }
  4029. val |= RBR_CFIG_B_VLD1;
  4030. switch (rp->rbr_sizes[1]) {
  4031. case 1 * 1024:
  4032. val |= (RBR_BUFSZ1_1K << RBR_CFIG_B_BUFSZ1_SHIFT);
  4033. break;
  4034. case 2 * 1024:
  4035. val |= (RBR_BUFSZ1_2K << RBR_CFIG_B_BUFSZ1_SHIFT);
  4036. break;
  4037. case 4 * 1024:
  4038. val |= (RBR_BUFSZ1_4K << RBR_CFIG_B_BUFSZ1_SHIFT);
  4039. break;
  4040. case 8 * 1024:
  4041. val |= (RBR_BUFSZ1_8K << RBR_CFIG_B_BUFSZ1_SHIFT);
  4042. break;
  4043. default:
  4044. return -EINVAL;
  4045. }
  4046. val |= RBR_CFIG_B_VLD0;
  4047. switch (rp->rbr_sizes[0]) {
  4048. case 256:
  4049. val |= (RBR_BUFSZ0_256 << RBR_CFIG_B_BUFSZ0_SHIFT);
  4050. break;
  4051. case 512:
  4052. val |= (RBR_BUFSZ0_512 << RBR_CFIG_B_BUFSZ0_SHIFT);
  4053. break;
  4054. case 1 * 1024:
  4055. val |= (RBR_BUFSZ0_1K << RBR_CFIG_B_BUFSZ0_SHIFT);
  4056. break;
  4057. case 2 * 1024:
  4058. val |= (RBR_BUFSZ0_2K << RBR_CFIG_B_BUFSZ0_SHIFT);
  4059. break;
  4060. default:
  4061. return -EINVAL;
  4062. }
  4063. *ret = val;
  4064. return 0;
  4065. }
  4066. static int niu_enable_rx_channel(struct niu *np, int channel, int on)
  4067. {
  4068. u64 val = nr64(RXDMA_CFIG1(channel));
  4069. int limit;
  4070. if (on)
  4071. val |= RXDMA_CFIG1_EN;
  4072. else
  4073. val &= ~RXDMA_CFIG1_EN;
  4074. nw64(RXDMA_CFIG1(channel), val);
  4075. limit = 1000;
  4076. while (--limit > 0) {
  4077. if (nr64(RXDMA_CFIG1(channel)) & RXDMA_CFIG1_QST)
  4078. break;
  4079. udelay(10);
  4080. }
  4081. if (limit <= 0)
  4082. return -ENODEV;
  4083. return 0;
  4084. }
  4085. static int niu_init_one_rx_channel(struct niu *np, struct rx_ring_info *rp)
  4086. {
  4087. int err, channel = rp->rx_channel;
  4088. u64 val;
  4089. err = niu_rx_channel_reset(np, channel);
  4090. if (err)
  4091. return err;
  4092. err = niu_rx_channel_lpage_init(np, channel);
  4093. if (err)
  4094. return err;
  4095. niu_rx_channel_wred_init(np, rp);
  4096. nw64(RX_DMA_ENT_MSK(channel), RX_DMA_ENT_MSK_RBR_EMPTY);
  4097. nw64(RX_DMA_CTL_STAT(channel),
  4098. (RX_DMA_CTL_STAT_MEX |
  4099. RX_DMA_CTL_STAT_RCRTHRES |
  4100. RX_DMA_CTL_STAT_RCRTO |
  4101. RX_DMA_CTL_STAT_RBR_EMPTY));
  4102. nw64(RXDMA_CFIG1(channel), rp->mbox_dma >> 32);
  4103. nw64(RXDMA_CFIG2(channel),
  4104. ((rp->mbox_dma & RXDMA_CFIG2_MBADDR_L) |
  4105. RXDMA_CFIG2_FULL_HDR));
  4106. nw64(RBR_CFIG_A(channel),
  4107. ((u64)rp->rbr_table_size << RBR_CFIG_A_LEN_SHIFT) |
  4108. (rp->rbr_dma & (RBR_CFIG_A_STADDR_BASE | RBR_CFIG_A_STADDR)));
  4109. err = niu_compute_rbr_cfig_b(rp, &val);
  4110. if (err)
  4111. return err;
  4112. nw64(RBR_CFIG_B(channel), val);
  4113. nw64(RCRCFIG_A(channel),
  4114. ((u64)rp->rcr_table_size << RCRCFIG_A_LEN_SHIFT) |
  4115. (rp->rcr_dma & (RCRCFIG_A_STADDR_BASE | RCRCFIG_A_STADDR)));
  4116. nw64(RCRCFIG_B(channel),
  4117. ((u64)rp->rcr_pkt_threshold << RCRCFIG_B_PTHRES_SHIFT) |
  4118. RCRCFIG_B_ENTOUT |
  4119. ((u64)rp->rcr_timeout << RCRCFIG_B_TIMEOUT_SHIFT));
  4120. err = niu_enable_rx_channel(np, channel, 1);
  4121. if (err)
  4122. return err;
  4123. nw64(RBR_KICK(channel), rp->rbr_index);
  4124. val = nr64(RX_DMA_CTL_STAT(channel));
  4125. val |= RX_DMA_CTL_STAT_RBR_EMPTY;
  4126. nw64(RX_DMA_CTL_STAT(channel), val);
  4127. return 0;
  4128. }
  4129. static int niu_init_rx_channels(struct niu *np)
  4130. {
  4131. unsigned long flags;
  4132. u64 seed = jiffies_64;
  4133. int err, i;
  4134. niu_lock_parent(np, flags);
  4135. nw64(RX_DMA_CK_DIV, np->parent->rxdma_clock_divider);
  4136. nw64(RED_RAN_INIT, RED_RAN_INIT_OPMODE | (seed & RED_RAN_INIT_VAL));
  4137. niu_unlock_parent(np, flags);
  4138. /* XXX RXDMA 32bit mode? XXX */
  4139. niu_init_rdc_groups(np);
  4140. niu_init_drr_weight(np);
  4141. err = niu_init_hostinfo(np);
  4142. if (err)
  4143. return err;
  4144. for (i = 0; i < np->num_rx_rings; i++) {
  4145. struct rx_ring_info *rp = &np->rx_rings[i];
  4146. err = niu_init_one_rx_channel(np, rp);
  4147. if (err)
  4148. return err;
  4149. }
  4150. return 0;
  4151. }
  4152. static int niu_set_ip_frag_rule(struct niu *np)
  4153. {
  4154. struct niu_parent *parent = np->parent;
  4155. struct niu_classifier *cp = &np->clas;
  4156. struct niu_tcam_entry *tp;
  4157. int index, err;
  4158. index = cp->tcam_top;
  4159. tp = &parent->tcam[index];
  4160. /* Note that the noport bit is the same in both ipv4 and
  4161. * ipv6 format TCAM entries.
  4162. */
  4163. memset(tp, 0, sizeof(*tp));
  4164. tp->key[1] = TCAM_V4KEY1_NOPORT;
  4165. tp->key_mask[1] = TCAM_V4KEY1_NOPORT;
  4166. tp->assoc_data = (TCAM_ASSOCDATA_TRES_USE_OFFSET |
  4167. ((u64)0 << TCAM_ASSOCDATA_OFFSET_SHIFT));
  4168. err = tcam_write(np, index, tp->key, tp->key_mask);
  4169. if (err)
  4170. return err;
  4171. err = tcam_assoc_write(np, index, tp->assoc_data);
  4172. if (err)
  4173. return err;
  4174. tp->valid = 1;
  4175. cp->tcam_valid_entries++;
  4176. return 0;
  4177. }
  4178. static int niu_init_classifier_hw(struct niu *np)
  4179. {
  4180. struct niu_parent *parent = np->parent;
  4181. struct niu_classifier *cp = &np->clas;
  4182. int i, err;
  4183. nw64(H1POLY, cp->h1_init);
  4184. nw64(H2POLY, cp->h2_init);
  4185. err = niu_init_hostinfo(np);
  4186. if (err)
  4187. return err;
  4188. for (i = 0; i < ENET_VLAN_TBL_NUM_ENTRIES; i++) {
  4189. struct niu_vlan_rdc *vp = &cp->vlan_mappings[i];
  4190. vlan_tbl_write(np, i, np->port,
  4191. vp->vlan_pref, vp->rdc_num);
  4192. }
  4193. for (i = 0; i < cp->num_alt_mac_mappings; i++) {
  4194. struct niu_altmac_rdc *ap = &cp->alt_mac_mappings[i];
  4195. err = niu_set_alt_mac_rdc_table(np, ap->alt_mac_num,
  4196. ap->rdc_num, ap->mac_pref);
  4197. if (err)
  4198. return err;
  4199. }
  4200. for (i = CLASS_CODE_USER_PROG1; i <= CLASS_CODE_SCTP_IPV6; i++) {
  4201. int index = i - CLASS_CODE_USER_PROG1;
  4202. err = niu_set_tcam_key(np, i, parent->tcam_key[index]);
  4203. if (err)
  4204. return err;
  4205. err = niu_set_flow_key(np, i, parent->flow_key[index]);
  4206. if (err)
  4207. return err;
  4208. }
  4209. err = niu_set_ip_frag_rule(np);
  4210. if (err)
  4211. return err;
  4212. tcam_enable(np, 1);
  4213. return 0;
  4214. }
  4215. static int niu_zcp_write(struct niu *np, int index, u64 *data)
  4216. {
  4217. nw64(ZCP_RAM_DATA0, data[0]);
  4218. nw64(ZCP_RAM_DATA1, data[1]);
  4219. nw64(ZCP_RAM_DATA2, data[2]);
  4220. nw64(ZCP_RAM_DATA3, data[3]);
  4221. nw64(ZCP_RAM_DATA4, data[4]);
  4222. nw64(ZCP_RAM_BE, ZCP_RAM_BE_VAL);
  4223. nw64(ZCP_RAM_ACC,
  4224. (ZCP_RAM_ACC_WRITE |
  4225. (0 << ZCP_RAM_ACC_ZFCID_SHIFT) |
  4226. (ZCP_RAM_SEL_CFIFO(np->port) << ZCP_RAM_ACC_RAM_SEL_SHIFT)));
  4227. return niu_wait_bits_clear(np, ZCP_RAM_ACC, ZCP_RAM_ACC_BUSY,
  4228. 1000, 100);
  4229. }
  4230. static int niu_zcp_read(struct niu *np, int index, u64 *data)
  4231. {
  4232. int err;
  4233. err = niu_wait_bits_clear(np, ZCP_RAM_ACC, ZCP_RAM_ACC_BUSY,
  4234. 1000, 100);
  4235. if (err) {
  4236. netdev_err(np->dev, "ZCP read busy won't clear, ZCP_RAM_ACC[%llx]\n",
  4237. (unsigned long long)nr64(ZCP_RAM_ACC));
  4238. return err;
  4239. }
  4240. nw64(ZCP_RAM_ACC,
  4241. (ZCP_RAM_ACC_READ |
  4242. (0 << ZCP_RAM_ACC_ZFCID_SHIFT) |
  4243. (ZCP_RAM_SEL_CFIFO(np->port) << ZCP_RAM_ACC_RAM_SEL_SHIFT)));
  4244. err = niu_wait_bits_clear(np, ZCP_RAM_ACC, ZCP_RAM_ACC_BUSY,
  4245. 1000, 100);
  4246. if (err) {
  4247. netdev_err(np->dev, "ZCP read busy2 won't clear, ZCP_RAM_ACC[%llx]\n",
  4248. (unsigned long long)nr64(ZCP_RAM_ACC));
  4249. return err;
  4250. }
  4251. data[0] = nr64(ZCP_RAM_DATA0);
  4252. data[1] = nr64(ZCP_RAM_DATA1);
  4253. data[2] = nr64(ZCP_RAM_DATA2);
  4254. data[3] = nr64(ZCP_RAM_DATA3);
  4255. data[4] = nr64(ZCP_RAM_DATA4);
  4256. return 0;
  4257. }
  4258. static void niu_zcp_cfifo_reset(struct niu *np)
  4259. {
  4260. u64 val = nr64(RESET_CFIFO);
  4261. val |= RESET_CFIFO_RST(np->port);
  4262. nw64(RESET_CFIFO, val);
  4263. udelay(10);
  4264. val &= ~RESET_CFIFO_RST(np->port);
  4265. nw64(RESET_CFIFO, val);
  4266. }
  4267. static int niu_init_zcp(struct niu *np)
  4268. {
  4269. u64 data[5], rbuf[5];
  4270. int i, max, err;
  4271. if (np->parent->plat_type != PLAT_TYPE_NIU) {
  4272. if (np->port == 0 || np->port == 1)
  4273. max = ATLAS_P0_P1_CFIFO_ENTRIES;
  4274. else
  4275. max = ATLAS_P2_P3_CFIFO_ENTRIES;
  4276. } else
  4277. max = NIU_CFIFO_ENTRIES;
  4278. data[0] = 0;
  4279. data[1] = 0;
  4280. data[2] = 0;
  4281. data[3] = 0;
  4282. data[4] = 0;
  4283. for (i = 0; i < max; i++) {
  4284. err = niu_zcp_write(np, i, data);
  4285. if (err)
  4286. return err;
  4287. err = niu_zcp_read(np, i, rbuf);
  4288. if (err)
  4289. return err;
  4290. }
  4291. niu_zcp_cfifo_reset(np);
  4292. nw64(CFIFO_ECC(np->port), 0);
  4293. nw64(ZCP_INT_STAT, ZCP_INT_STAT_ALL);
  4294. (void) nr64(ZCP_INT_STAT);
  4295. nw64(ZCP_INT_MASK, ZCP_INT_MASK_ALL);
  4296. return 0;
  4297. }
  4298. static void niu_ipp_write(struct niu *np, int index, u64 *data)
  4299. {
  4300. u64 val = nr64_ipp(IPP_CFIG);
  4301. nw64_ipp(IPP_CFIG, val | IPP_CFIG_DFIFO_PIO_W);
  4302. nw64_ipp(IPP_DFIFO_WR_PTR, index);
  4303. nw64_ipp(IPP_DFIFO_WR0, data[0]);
  4304. nw64_ipp(IPP_DFIFO_WR1, data[1]);
  4305. nw64_ipp(IPP_DFIFO_WR2, data[2]);
  4306. nw64_ipp(IPP_DFIFO_WR3, data[3]);
  4307. nw64_ipp(IPP_DFIFO_WR4, data[4]);
  4308. nw64_ipp(IPP_CFIG, val & ~IPP_CFIG_DFIFO_PIO_W);
  4309. }
  4310. static void niu_ipp_read(struct niu *np, int index, u64 *data)
  4311. {
  4312. nw64_ipp(IPP_DFIFO_RD_PTR, index);
  4313. data[0] = nr64_ipp(IPP_DFIFO_RD0);
  4314. data[1] = nr64_ipp(IPP_DFIFO_RD1);
  4315. data[2] = nr64_ipp(IPP_DFIFO_RD2);
  4316. data[3] = nr64_ipp(IPP_DFIFO_RD3);
  4317. data[4] = nr64_ipp(IPP_DFIFO_RD4);
  4318. }
  4319. static int niu_ipp_reset(struct niu *np)
  4320. {
  4321. return niu_set_and_wait_clear_ipp(np, IPP_CFIG, IPP_CFIG_SOFT_RST,
  4322. 1000, 100, "IPP_CFIG");
  4323. }
  4324. static int niu_init_ipp(struct niu *np)
  4325. {
  4326. u64 data[5], rbuf[5], val;
  4327. int i, max, err;
  4328. if (np->parent->plat_type != PLAT_TYPE_NIU) {
  4329. if (np->port == 0 || np->port == 1)
  4330. max = ATLAS_P0_P1_DFIFO_ENTRIES;
  4331. else
  4332. max = ATLAS_P2_P3_DFIFO_ENTRIES;
  4333. } else
  4334. max = NIU_DFIFO_ENTRIES;
  4335. data[0] = 0;
  4336. data[1] = 0;
  4337. data[2] = 0;
  4338. data[3] = 0;
  4339. data[4] = 0;
  4340. for (i = 0; i < max; i++) {
  4341. niu_ipp_write(np, i, data);
  4342. niu_ipp_read(np, i, rbuf);
  4343. }
  4344. (void) nr64_ipp(IPP_INT_STAT);
  4345. (void) nr64_ipp(IPP_INT_STAT);
  4346. err = niu_ipp_reset(np);
  4347. if (err)
  4348. return err;
  4349. (void) nr64_ipp(IPP_PKT_DIS);
  4350. (void) nr64_ipp(IPP_BAD_CS_CNT);
  4351. (void) nr64_ipp(IPP_ECC);
  4352. (void) nr64_ipp(IPP_INT_STAT);
  4353. nw64_ipp(IPP_MSK, ~IPP_MSK_ALL);
  4354. val = nr64_ipp(IPP_CFIG);
  4355. val &= ~IPP_CFIG_IP_MAX_PKT;
  4356. val |= (IPP_CFIG_IPP_ENABLE |
  4357. IPP_CFIG_DFIFO_ECC_EN |
  4358. IPP_CFIG_DROP_BAD_CRC |
  4359. IPP_CFIG_CKSUM_EN |
  4360. (0x1ffff << IPP_CFIG_IP_MAX_PKT_SHIFT));
  4361. nw64_ipp(IPP_CFIG, val);
  4362. return 0;
  4363. }
  4364. static void niu_handle_led(struct niu *np, int status)
  4365. {
  4366. u64 val;
  4367. val = nr64_mac(XMAC_CONFIG);
  4368. if ((np->flags & NIU_FLAGS_10G) != 0 &&
  4369. (np->flags & NIU_FLAGS_FIBER) != 0) {
  4370. if (status) {
  4371. val |= XMAC_CONFIG_LED_POLARITY;
  4372. val &= ~XMAC_CONFIG_FORCE_LED_ON;
  4373. } else {
  4374. val |= XMAC_CONFIG_FORCE_LED_ON;
  4375. val &= ~XMAC_CONFIG_LED_POLARITY;
  4376. }
  4377. }
  4378. nw64_mac(XMAC_CONFIG, val);
  4379. }
  4380. static void niu_init_xif_xmac(struct niu *np)
  4381. {
  4382. struct niu_link_config *lp = &np->link_config;
  4383. u64 val;
  4384. if (np->flags & NIU_FLAGS_XCVR_SERDES) {
  4385. val = nr64(MIF_CONFIG);
  4386. val |= MIF_CONFIG_ATCA_GE;
  4387. nw64(MIF_CONFIG, val);
  4388. }
  4389. val = nr64_mac(XMAC_CONFIG);
  4390. val &= ~XMAC_CONFIG_SEL_POR_CLK_SRC;
  4391. val |= XMAC_CONFIG_TX_OUTPUT_EN;
  4392. if (lp->loopback_mode == LOOPBACK_MAC) {
  4393. val &= ~XMAC_CONFIG_SEL_POR_CLK_SRC;
  4394. val |= XMAC_CONFIG_LOOPBACK;
  4395. } else {
  4396. val &= ~XMAC_CONFIG_LOOPBACK;
  4397. }
  4398. if (np->flags & NIU_FLAGS_10G) {
  4399. val &= ~XMAC_CONFIG_LFS_DISABLE;
  4400. } else {
  4401. val |= XMAC_CONFIG_LFS_DISABLE;
  4402. if (!(np->flags & NIU_FLAGS_FIBER) &&
  4403. !(np->flags & NIU_FLAGS_XCVR_SERDES))
  4404. val |= XMAC_CONFIG_1G_PCS_BYPASS;
  4405. else
  4406. val &= ~XMAC_CONFIG_1G_PCS_BYPASS;
  4407. }
  4408. val &= ~XMAC_CONFIG_10G_XPCS_BYPASS;
  4409. if (lp->active_speed == SPEED_100)
  4410. val |= XMAC_CONFIG_SEL_CLK_25MHZ;
  4411. else
  4412. val &= ~XMAC_CONFIG_SEL_CLK_25MHZ;
  4413. nw64_mac(XMAC_CONFIG, val);
  4414. val = nr64_mac(XMAC_CONFIG);
  4415. val &= ~XMAC_CONFIG_MODE_MASK;
  4416. if (np->flags & NIU_FLAGS_10G) {
  4417. val |= XMAC_CONFIG_MODE_XGMII;
  4418. } else {
  4419. if (lp->active_speed == SPEED_1000)
  4420. val |= XMAC_CONFIG_MODE_GMII;
  4421. else
  4422. val |= XMAC_CONFIG_MODE_MII;
  4423. }
  4424. nw64_mac(XMAC_CONFIG, val);
  4425. }
  4426. static void niu_init_xif_bmac(struct niu *np)
  4427. {
  4428. struct niu_link_config *lp = &np->link_config;
  4429. u64 val;
  4430. val = BMAC_XIF_CONFIG_TX_OUTPUT_EN;
  4431. if (lp->loopback_mode == LOOPBACK_MAC)
  4432. val |= BMAC_XIF_CONFIG_MII_LOOPBACK;
  4433. else
  4434. val &= ~BMAC_XIF_CONFIG_MII_LOOPBACK;
  4435. if (lp->active_speed == SPEED_1000)
  4436. val |= BMAC_XIF_CONFIG_GMII_MODE;
  4437. else
  4438. val &= ~BMAC_XIF_CONFIG_GMII_MODE;
  4439. val &= ~(BMAC_XIF_CONFIG_LINK_LED |
  4440. BMAC_XIF_CONFIG_LED_POLARITY);
  4441. if (!(np->flags & NIU_FLAGS_10G) &&
  4442. !(np->flags & NIU_FLAGS_FIBER) &&
  4443. lp->active_speed == SPEED_100)
  4444. val |= BMAC_XIF_CONFIG_25MHZ_CLOCK;
  4445. else
  4446. val &= ~BMAC_XIF_CONFIG_25MHZ_CLOCK;
  4447. nw64_mac(BMAC_XIF_CONFIG, val);
  4448. }
  4449. static void niu_init_xif(struct niu *np)
  4450. {
  4451. if (np->flags & NIU_FLAGS_XMAC)
  4452. niu_init_xif_xmac(np);
  4453. else
  4454. niu_init_xif_bmac(np);
  4455. }
  4456. static void niu_pcs_mii_reset(struct niu *np)
  4457. {
  4458. int limit = 1000;
  4459. u64 val = nr64_pcs(PCS_MII_CTL);
  4460. val |= PCS_MII_CTL_RST;
  4461. nw64_pcs(PCS_MII_CTL, val);
  4462. while ((--limit >= 0) && (val & PCS_MII_CTL_RST)) {
  4463. udelay(100);
  4464. val = nr64_pcs(PCS_MII_CTL);
  4465. }
  4466. }
  4467. static void niu_xpcs_reset(struct niu *np)
  4468. {
  4469. int limit = 1000;
  4470. u64 val = nr64_xpcs(XPCS_CONTROL1);
  4471. val |= XPCS_CONTROL1_RESET;
  4472. nw64_xpcs(XPCS_CONTROL1, val);
  4473. while ((--limit >= 0) && (val & XPCS_CONTROL1_RESET)) {
  4474. udelay(100);
  4475. val = nr64_xpcs(XPCS_CONTROL1);
  4476. }
  4477. }
  4478. static int niu_init_pcs(struct niu *np)
  4479. {
  4480. struct niu_link_config *lp = &np->link_config;
  4481. u64 val;
  4482. switch (np->flags & (NIU_FLAGS_10G |
  4483. NIU_FLAGS_FIBER |
  4484. NIU_FLAGS_XCVR_SERDES)) {
  4485. case NIU_FLAGS_FIBER:
  4486. /* 1G fiber */
  4487. nw64_pcs(PCS_CONF, PCS_CONF_MASK | PCS_CONF_ENABLE);
  4488. nw64_pcs(PCS_DPATH_MODE, 0);
  4489. niu_pcs_mii_reset(np);
  4490. break;
  4491. case NIU_FLAGS_10G:
  4492. case NIU_FLAGS_10G | NIU_FLAGS_FIBER:
  4493. case NIU_FLAGS_10G | NIU_FLAGS_XCVR_SERDES:
  4494. /* 10G SERDES */
  4495. if (!(np->flags & NIU_FLAGS_XMAC))
  4496. return -EINVAL;
  4497. /* 10G copper or fiber */
  4498. val = nr64_mac(XMAC_CONFIG);
  4499. val &= ~XMAC_CONFIG_10G_XPCS_BYPASS;
  4500. nw64_mac(XMAC_CONFIG, val);
  4501. niu_xpcs_reset(np);
  4502. val = nr64_xpcs(XPCS_CONTROL1);
  4503. if (lp->loopback_mode == LOOPBACK_PHY)
  4504. val |= XPCS_CONTROL1_LOOPBACK;
  4505. else
  4506. val &= ~XPCS_CONTROL1_LOOPBACK;
  4507. nw64_xpcs(XPCS_CONTROL1, val);
  4508. nw64_xpcs(XPCS_DESKEW_ERR_CNT, 0);
  4509. (void) nr64_xpcs(XPCS_SYMERR_CNT01);
  4510. (void) nr64_xpcs(XPCS_SYMERR_CNT23);
  4511. break;
  4512. case NIU_FLAGS_XCVR_SERDES:
  4513. /* 1G SERDES */
  4514. niu_pcs_mii_reset(np);
  4515. nw64_pcs(PCS_CONF, PCS_CONF_MASK | PCS_CONF_ENABLE);
  4516. nw64_pcs(PCS_DPATH_MODE, 0);
  4517. break;
  4518. case 0:
  4519. /* 1G copper */
  4520. case NIU_FLAGS_XCVR_SERDES | NIU_FLAGS_FIBER:
  4521. /* 1G RGMII FIBER */
  4522. nw64_pcs(PCS_DPATH_MODE, PCS_DPATH_MODE_MII);
  4523. niu_pcs_mii_reset(np);
  4524. break;
  4525. default:
  4526. return -EINVAL;
  4527. }
  4528. return 0;
  4529. }
  4530. static int niu_reset_tx_xmac(struct niu *np)
  4531. {
  4532. return niu_set_and_wait_clear_mac(np, XTXMAC_SW_RST,
  4533. (XTXMAC_SW_RST_REG_RS |
  4534. XTXMAC_SW_RST_SOFT_RST),
  4535. 1000, 100, "XTXMAC_SW_RST");
  4536. }
  4537. static int niu_reset_tx_bmac(struct niu *np)
  4538. {
  4539. int limit;
  4540. nw64_mac(BTXMAC_SW_RST, BTXMAC_SW_RST_RESET);
  4541. limit = 1000;
  4542. while (--limit >= 0) {
  4543. if (!(nr64_mac(BTXMAC_SW_RST) & BTXMAC_SW_RST_RESET))
  4544. break;
  4545. udelay(100);
  4546. }
  4547. if (limit < 0) {
  4548. dev_err(np->device, "Port %u TX BMAC would not reset, BTXMAC_SW_RST[%llx]\n",
  4549. np->port,
  4550. (unsigned long long) nr64_mac(BTXMAC_SW_RST));
  4551. return -ENODEV;
  4552. }
  4553. return 0;
  4554. }
  4555. static int niu_reset_tx_mac(struct niu *np)
  4556. {
  4557. if (np->flags & NIU_FLAGS_XMAC)
  4558. return niu_reset_tx_xmac(np);
  4559. else
  4560. return niu_reset_tx_bmac(np);
  4561. }
  4562. static void niu_init_tx_xmac(struct niu *np, u64 min, u64 max)
  4563. {
  4564. u64 val;
  4565. val = nr64_mac(XMAC_MIN);
  4566. val &= ~(XMAC_MIN_TX_MIN_PKT_SIZE |
  4567. XMAC_MIN_RX_MIN_PKT_SIZE);
  4568. val |= (min << XMAC_MIN_RX_MIN_PKT_SIZE_SHFT);
  4569. val |= (min << XMAC_MIN_TX_MIN_PKT_SIZE_SHFT);
  4570. nw64_mac(XMAC_MIN, val);
  4571. nw64_mac(XMAC_MAX, max);
  4572. nw64_mac(XTXMAC_STAT_MSK, ~(u64)0);
  4573. val = nr64_mac(XMAC_IPG);
  4574. if (np->flags & NIU_FLAGS_10G) {
  4575. val &= ~XMAC_IPG_IPG_XGMII;
  4576. val |= (IPG_12_15_XGMII << XMAC_IPG_IPG_XGMII_SHIFT);
  4577. } else {
  4578. val &= ~XMAC_IPG_IPG_MII_GMII;
  4579. val |= (IPG_12_MII_GMII << XMAC_IPG_IPG_MII_GMII_SHIFT);
  4580. }
  4581. nw64_mac(XMAC_IPG, val);
  4582. val = nr64_mac(XMAC_CONFIG);
  4583. val &= ~(XMAC_CONFIG_ALWAYS_NO_CRC |
  4584. XMAC_CONFIG_STRETCH_MODE |
  4585. XMAC_CONFIG_VAR_MIN_IPG_EN |
  4586. XMAC_CONFIG_TX_ENABLE);
  4587. nw64_mac(XMAC_CONFIG, val);
  4588. nw64_mac(TXMAC_FRM_CNT, 0);
  4589. nw64_mac(TXMAC_BYTE_CNT, 0);
  4590. }
  4591. static void niu_init_tx_bmac(struct niu *np, u64 min, u64 max)
  4592. {
  4593. u64 val;
  4594. nw64_mac(BMAC_MIN_FRAME, min);
  4595. nw64_mac(BMAC_MAX_FRAME, max);
  4596. nw64_mac(BTXMAC_STATUS_MASK, ~(u64)0);
  4597. nw64_mac(BMAC_CTRL_TYPE, 0x8808);
  4598. nw64_mac(BMAC_PREAMBLE_SIZE, 7);
  4599. val = nr64_mac(BTXMAC_CONFIG);
  4600. val &= ~(BTXMAC_CONFIG_FCS_DISABLE |
  4601. BTXMAC_CONFIG_ENABLE);
  4602. nw64_mac(BTXMAC_CONFIG, val);
  4603. }
  4604. static void niu_init_tx_mac(struct niu *np)
  4605. {
  4606. u64 min, max;
  4607. min = 64;
  4608. if (np->dev->mtu > ETH_DATA_LEN)
  4609. max = 9216;
  4610. else
  4611. max = 1522;
  4612. /* The XMAC_MIN register only accepts values for TX min which
  4613. * have the low 3 bits cleared.
  4614. */
  4615. BUG_ON(min & 0x7);
  4616. if (np->flags & NIU_FLAGS_XMAC)
  4617. niu_init_tx_xmac(np, min, max);
  4618. else
  4619. niu_init_tx_bmac(np, min, max);
  4620. }
  4621. static int niu_reset_rx_xmac(struct niu *np)
  4622. {
  4623. int limit;
  4624. nw64_mac(XRXMAC_SW_RST,
  4625. XRXMAC_SW_RST_REG_RS | XRXMAC_SW_RST_SOFT_RST);
  4626. limit = 1000;
  4627. while (--limit >= 0) {
  4628. if (!(nr64_mac(XRXMAC_SW_RST) & (XRXMAC_SW_RST_REG_RS |
  4629. XRXMAC_SW_RST_SOFT_RST)))
  4630. break;
  4631. udelay(100);
  4632. }
  4633. if (limit < 0) {
  4634. dev_err(np->device, "Port %u RX XMAC would not reset, XRXMAC_SW_RST[%llx]\n",
  4635. np->port,
  4636. (unsigned long long) nr64_mac(XRXMAC_SW_RST));
  4637. return -ENODEV;
  4638. }
  4639. return 0;
  4640. }
  4641. static int niu_reset_rx_bmac(struct niu *np)
  4642. {
  4643. int limit;
  4644. nw64_mac(BRXMAC_SW_RST, BRXMAC_SW_RST_RESET);
  4645. limit = 1000;
  4646. while (--limit >= 0) {
  4647. if (!(nr64_mac(BRXMAC_SW_RST) & BRXMAC_SW_RST_RESET))
  4648. break;
  4649. udelay(100);
  4650. }
  4651. if (limit < 0) {
  4652. dev_err(np->device, "Port %u RX BMAC would not reset, BRXMAC_SW_RST[%llx]\n",
  4653. np->port,
  4654. (unsigned long long) nr64_mac(BRXMAC_SW_RST));
  4655. return -ENODEV;
  4656. }
  4657. return 0;
  4658. }
  4659. static int niu_reset_rx_mac(struct niu *np)
  4660. {
  4661. if (np->flags & NIU_FLAGS_XMAC)
  4662. return niu_reset_rx_xmac(np);
  4663. else
  4664. return niu_reset_rx_bmac(np);
  4665. }
  4666. static void niu_init_rx_xmac(struct niu *np)
  4667. {
  4668. struct niu_parent *parent = np->parent;
  4669. struct niu_rdc_tables *tp = &parent->rdc_group_cfg[np->port];
  4670. int first_rdc_table = tp->first_table_num;
  4671. unsigned long i;
  4672. u64 val;
  4673. nw64_mac(XMAC_ADD_FILT0, 0);
  4674. nw64_mac(XMAC_ADD_FILT1, 0);
  4675. nw64_mac(XMAC_ADD_FILT2, 0);
  4676. nw64_mac(XMAC_ADD_FILT12_MASK, 0);
  4677. nw64_mac(XMAC_ADD_FILT00_MASK, 0);
  4678. for (i = 0; i < MAC_NUM_HASH; i++)
  4679. nw64_mac(XMAC_HASH_TBL(i), 0);
  4680. nw64_mac(XRXMAC_STAT_MSK, ~(u64)0);
  4681. niu_set_primary_mac_rdc_table(np, first_rdc_table, 1);
  4682. niu_set_multicast_mac_rdc_table(np, first_rdc_table, 1);
  4683. val = nr64_mac(XMAC_CONFIG);
  4684. val &= ~(XMAC_CONFIG_RX_MAC_ENABLE |
  4685. XMAC_CONFIG_PROMISCUOUS |
  4686. XMAC_CONFIG_PROMISC_GROUP |
  4687. XMAC_CONFIG_ERR_CHK_DIS |
  4688. XMAC_CONFIG_RX_CRC_CHK_DIS |
  4689. XMAC_CONFIG_RESERVED_MULTICAST |
  4690. XMAC_CONFIG_RX_CODEV_CHK_DIS |
  4691. XMAC_CONFIG_ADDR_FILTER_EN |
  4692. XMAC_CONFIG_RCV_PAUSE_ENABLE |
  4693. XMAC_CONFIG_STRIP_CRC |
  4694. XMAC_CONFIG_PASS_FLOW_CTRL |
  4695. XMAC_CONFIG_MAC2IPP_PKT_CNT_EN);
  4696. val |= (XMAC_CONFIG_HASH_FILTER_EN);
  4697. nw64_mac(XMAC_CONFIG, val);
  4698. nw64_mac(RXMAC_BT_CNT, 0);
  4699. nw64_mac(RXMAC_BC_FRM_CNT, 0);
  4700. nw64_mac(RXMAC_MC_FRM_CNT, 0);
  4701. nw64_mac(RXMAC_FRAG_CNT, 0);
  4702. nw64_mac(RXMAC_HIST_CNT1, 0);
  4703. nw64_mac(RXMAC_HIST_CNT2, 0);
  4704. nw64_mac(RXMAC_HIST_CNT3, 0);
  4705. nw64_mac(RXMAC_HIST_CNT4, 0);
  4706. nw64_mac(RXMAC_HIST_CNT5, 0);
  4707. nw64_mac(RXMAC_HIST_CNT6, 0);
  4708. nw64_mac(RXMAC_HIST_CNT7, 0);
  4709. nw64_mac(RXMAC_MPSZER_CNT, 0);
  4710. nw64_mac(RXMAC_CRC_ER_CNT, 0);
  4711. nw64_mac(RXMAC_CD_VIO_CNT, 0);
  4712. nw64_mac(LINK_FAULT_CNT, 0);
  4713. }
  4714. static void niu_init_rx_bmac(struct niu *np)
  4715. {
  4716. struct niu_parent *parent = np->parent;
  4717. struct niu_rdc_tables *tp = &parent->rdc_group_cfg[np->port];
  4718. int first_rdc_table = tp->first_table_num;
  4719. unsigned long i;
  4720. u64 val;
  4721. nw64_mac(BMAC_ADD_FILT0, 0);
  4722. nw64_mac(BMAC_ADD_FILT1, 0);
  4723. nw64_mac(BMAC_ADD_FILT2, 0);
  4724. nw64_mac(BMAC_ADD_FILT12_MASK, 0);
  4725. nw64_mac(BMAC_ADD_FILT00_MASK, 0);
  4726. for (i = 0; i < MAC_NUM_HASH; i++)
  4727. nw64_mac(BMAC_HASH_TBL(i), 0);
  4728. niu_set_primary_mac_rdc_table(np, first_rdc_table, 1);
  4729. niu_set_multicast_mac_rdc_table(np, first_rdc_table, 1);
  4730. nw64_mac(BRXMAC_STATUS_MASK, ~(u64)0);
  4731. val = nr64_mac(BRXMAC_CONFIG);
  4732. val &= ~(BRXMAC_CONFIG_ENABLE |
  4733. BRXMAC_CONFIG_STRIP_PAD |
  4734. BRXMAC_CONFIG_STRIP_FCS |
  4735. BRXMAC_CONFIG_PROMISC |
  4736. BRXMAC_CONFIG_PROMISC_GRP |
  4737. BRXMAC_CONFIG_ADDR_FILT_EN |
  4738. BRXMAC_CONFIG_DISCARD_DIS);
  4739. val |= (BRXMAC_CONFIG_HASH_FILT_EN);
  4740. nw64_mac(BRXMAC_CONFIG, val);
  4741. val = nr64_mac(BMAC_ADDR_CMPEN);
  4742. val |= BMAC_ADDR_CMPEN_EN0;
  4743. nw64_mac(BMAC_ADDR_CMPEN, val);
  4744. }
  4745. static void niu_init_rx_mac(struct niu *np)
  4746. {
  4747. niu_set_primary_mac(np, np->dev->dev_addr);
  4748. if (np->flags & NIU_FLAGS_XMAC)
  4749. niu_init_rx_xmac(np);
  4750. else
  4751. niu_init_rx_bmac(np);
  4752. }
  4753. static void niu_enable_tx_xmac(struct niu *np, int on)
  4754. {
  4755. u64 val = nr64_mac(XMAC_CONFIG);
  4756. if (on)
  4757. val |= XMAC_CONFIG_TX_ENABLE;
  4758. else
  4759. val &= ~XMAC_CONFIG_TX_ENABLE;
  4760. nw64_mac(XMAC_CONFIG, val);
  4761. }
  4762. static void niu_enable_tx_bmac(struct niu *np, int on)
  4763. {
  4764. u64 val = nr64_mac(BTXMAC_CONFIG);
  4765. if (on)
  4766. val |= BTXMAC_CONFIG_ENABLE;
  4767. else
  4768. val &= ~BTXMAC_CONFIG_ENABLE;
  4769. nw64_mac(BTXMAC_CONFIG, val);
  4770. }
  4771. static void niu_enable_tx_mac(struct niu *np, int on)
  4772. {
  4773. if (np->flags & NIU_FLAGS_XMAC)
  4774. niu_enable_tx_xmac(np, on);
  4775. else
  4776. niu_enable_tx_bmac(np, on);
  4777. }
  4778. static void niu_enable_rx_xmac(struct niu *np, int on)
  4779. {
  4780. u64 val = nr64_mac(XMAC_CONFIG);
  4781. val &= ~(XMAC_CONFIG_HASH_FILTER_EN |
  4782. XMAC_CONFIG_PROMISCUOUS);
  4783. if (np->flags & NIU_FLAGS_MCAST)
  4784. val |= XMAC_CONFIG_HASH_FILTER_EN;
  4785. if (np->flags & NIU_FLAGS_PROMISC)
  4786. val |= XMAC_CONFIG_PROMISCUOUS;
  4787. if (on)
  4788. val |= XMAC_CONFIG_RX_MAC_ENABLE;
  4789. else
  4790. val &= ~XMAC_CONFIG_RX_MAC_ENABLE;
  4791. nw64_mac(XMAC_CONFIG, val);
  4792. }
  4793. static void niu_enable_rx_bmac(struct niu *np, int on)
  4794. {
  4795. u64 val = nr64_mac(BRXMAC_CONFIG);
  4796. val &= ~(BRXMAC_CONFIG_HASH_FILT_EN |
  4797. BRXMAC_CONFIG_PROMISC);
  4798. if (np->flags & NIU_FLAGS_MCAST)
  4799. val |= BRXMAC_CONFIG_HASH_FILT_EN;
  4800. if (np->flags & NIU_FLAGS_PROMISC)
  4801. val |= BRXMAC_CONFIG_PROMISC;
  4802. if (on)
  4803. val |= BRXMAC_CONFIG_ENABLE;
  4804. else
  4805. val &= ~BRXMAC_CONFIG_ENABLE;
  4806. nw64_mac(BRXMAC_CONFIG, val);
  4807. }
  4808. static void niu_enable_rx_mac(struct niu *np, int on)
  4809. {
  4810. if (np->flags & NIU_FLAGS_XMAC)
  4811. niu_enable_rx_xmac(np, on);
  4812. else
  4813. niu_enable_rx_bmac(np, on);
  4814. }
  4815. static int niu_init_mac(struct niu *np)
  4816. {
  4817. int err;
  4818. niu_init_xif(np);
  4819. err = niu_init_pcs(np);
  4820. if (err)
  4821. return err;
  4822. err = niu_reset_tx_mac(np);
  4823. if (err)
  4824. return err;
  4825. niu_init_tx_mac(np);
  4826. err = niu_reset_rx_mac(np);
  4827. if (err)
  4828. return err;
  4829. niu_init_rx_mac(np);
  4830. /* This looks hookey but the RX MAC reset we just did will
  4831. * undo some of the state we setup in niu_init_tx_mac() so we
  4832. * have to call it again. In particular, the RX MAC reset will
  4833. * set the XMAC_MAX register back to it's default value.
  4834. */
  4835. niu_init_tx_mac(np);
  4836. niu_enable_tx_mac(np, 1);
  4837. niu_enable_rx_mac(np, 1);
  4838. return 0;
  4839. }
  4840. static void niu_stop_one_tx_channel(struct niu *np, struct tx_ring_info *rp)
  4841. {
  4842. (void) niu_tx_channel_stop(np, rp->tx_channel);
  4843. }
  4844. static void niu_stop_tx_channels(struct niu *np)
  4845. {
  4846. int i;
  4847. for (i = 0; i < np->num_tx_rings; i++) {
  4848. struct tx_ring_info *rp = &np->tx_rings[i];
  4849. niu_stop_one_tx_channel(np, rp);
  4850. }
  4851. }
  4852. static void niu_reset_one_tx_channel(struct niu *np, struct tx_ring_info *rp)
  4853. {
  4854. (void) niu_tx_channel_reset(np, rp->tx_channel);
  4855. }
  4856. static void niu_reset_tx_channels(struct niu *np)
  4857. {
  4858. int i;
  4859. for (i = 0; i < np->num_tx_rings; i++) {
  4860. struct tx_ring_info *rp = &np->tx_rings[i];
  4861. niu_reset_one_tx_channel(np, rp);
  4862. }
  4863. }
  4864. static void niu_stop_one_rx_channel(struct niu *np, struct rx_ring_info *rp)
  4865. {
  4866. (void) niu_enable_rx_channel(np, rp->rx_channel, 0);
  4867. }
  4868. static void niu_stop_rx_channels(struct niu *np)
  4869. {
  4870. int i;
  4871. for (i = 0; i < np->num_rx_rings; i++) {
  4872. struct rx_ring_info *rp = &np->rx_rings[i];
  4873. niu_stop_one_rx_channel(np, rp);
  4874. }
  4875. }
  4876. static void niu_reset_one_rx_channel(struct niu *np, struct rx_ring_info *rp)
  4877. {
  4878. int channel = rp->rx_channel;
  4879. (void) niu_rx_channel_reset(np, channel);
  4880. nw64(RX_DMA_ENT_MSK(channel), RX_DMA_ENT_MSK_ALL);
  4881. nw64(RX_DMA_CTL_STAT(channel), 0);
  4882. (void) niu_enable_rx_channel(np, channel, 0);
  4883. }
  4884. static void niu_reset_rx_channels(struct niu *np)
  4885. {
  4886. int i;
  4887. for (i = 0; i < np->num_rx_rings; i++) {
  4888. struct rx_ring_info *rp = &np->rx_rings[i];
  4889. niu_reset_one_rx_channel(np, rp);
  4890. }
  4891. }
  4892. static void niu_disable_ipp(struct niu *np)
  4893. {
  4894. u64 rd, wr, val;
  4895. int limit;
  4896. rd = nr64_ipp(IPP_DFIFO_RD_PTR);
  4897. wr = nr64_ipp(IPP_DFIFO_WR_PTR);
  4898. limit = 100;
  4899. while (--limit >= 0 && (rd != wr)) {
  4900. rd = nr64_ipp(IPP_DFIFO_RD_PTR);
  4901. wr = nr64_ipp(IPP_DFIFO_WR_PTR);
  4902. }
  4903. if (limit < 0 &&
  4904. (rd != 0 && wr != 1)) {
  4905. netdev_err(np->dev, "IPP would not quiesce, rd_ptr[%llx] wr_ptr[%llx]\n",
  4906. (unsigned long long)nr64_ipp(IPP_DFIFO_RD_PTR),
  4907. (unsigned long long)nr64_ipp(IPP_DFIFO_WR_PTR));
  4908. }
  4909. val = nr64_ipp(IPP_CFIG);
  4910. val &= ~(IPP_CFIG_IPP_ENABLE |
  4911. IPP_CFIG_DFIFO_ECC_EN |
  4912. IPP_CFIG_DROP_BAD_CRC |
  4913. IPP_CFIG_CKSUM_EN);
  4914. nw64_ipp(IPP_CFIG, val);
  4915. (void) niu_ipp_reset(np);
  4916. }
  4917. static int niu_init_hw(struct niu *np)
  4918. {
  4919. int i, err;
  4920. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize TXC\n");
  4921. niu_txc_enable_port(np, 1);
  4922. niu_txc_port_dma_enable(np, 1);
  4923. niu_txc_set_imask(np, 0);
  4924. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize TX channels\n");
  4925. for (i = 0; i < np->num_tx_rings; i++) {
  4926. struct tx_ring_info *rp = &np->tx_rings[i];
  4927. err = niu_init_one_tx_channel(np, rp);
  4928. if (err)
  4929. return err;
  4930. }
  4931. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize RX channels\n");
  4932. err = niu_init_rx_channels(np);
  4933. if (err)
  4934. goto out_uninit_tx_channels;
  4935. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize classifier\n");
  4936. err = niu_init_classifier_hw(np);
  4937. if (err)
  4938. goto out_uninit_rx_channels;
  4939. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize ZCP\n");
  4940. err = niu_init_zcp(np);
  4941. if (err)
  4942. goto out_uninit_rx_channels;
  4943. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize IPP\n");
  4944. err = niu_init_ipp(np);
  4945. if (err)
  4946. goto out_uninit_rx_channels;
  4947. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Initialize MAC\n");
  4948. err = niu_init_mac(np);
  4949. if (err)
  4950. goto out_uninit_ipp;
  4951. return 0;
  4952. out_uninit_ipp:
  4953. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Uninit IPP\n");
  4954. niu_disable_ipp(np);
  4955. out_uninit_rx_channels:
  4956. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Uninit RX channels\n");
  4957. niu_stop_rx_channels(np);
  4958. niu_reset_rx_channels(np);
  4959. out_uninit_tx_channels:
  4960. netif_printk(np, ifup, KERN_DEBUG, np->dev, "Uninit TX channels\n");
  4961. niu_stop_tx_channels(np);
  4962. niu_reset_tx_channels(np);
  4963. return err;
  4964. }
  4965. static void niu_stop_hw(struct niu *np)
  4966. {
  4967. netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Disable interrupts\n");
  4968. niu_enable_interrupts(np, 0);
  4969. netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Disable RX MAC\n");
  4970. niu_enable_rx_mac(np, 0);
  4971. netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Disable IPP\n");
  4972. niu_disable_ipp(np);
  4973. netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Stop TX channels\n");
  4974. niu_stop_tx_channels(np);
  4975. netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Stop RX channels\n");
  4976. niu_stop_rx_channels(np);
  4977. netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Reset TX channels\n");
  4978. niu_reset_tx_channels(np);
  4979. netif_printk(np, ifdown, KERN_DEBUG, np->dev, "Reset RX channels\n");
  4980. niu_reset_rx_channels(np);
  4981. }
  4982. static void niu_set_irq_name(struct niu *np)
  4983. {
  4984. int port = np->port;
  4985. int i, j = 1;
  4986. sprintf(np->irq_name[0], "%s:MAC", np->dev->name);
  4987. if (port == 0) {
  4988. sprintf(np->irq_name[1], "%s:MIF", np->dev->name);
  4989. sprintf(np->irq_name[2], "%s:SYSERR", np->dev->name);
  4990. j = 3;
  4991. }
  4992. for (i = 0; i < np->num_ldg - j; i++) {
  4993. if (i < np->num_rx_rings)
  4994. sprintf(np->irq_name[i+j], "%s-rx-%d",
  4995. np->dev->name, i);
  4996. else if (i < np->num_tx_rings + np->num_rx_rings)
  4997. sprintf(np->irq_name[i+j], "%s-tx-%d", np->dev->name,
  4998. i - np->num_rx_rings);
  4999. }
  5000. }
  5001. static int niu_request_irq(struct niu *np)
  5002. {
  5003. int i, j, err;
  5004. niu_set_irq_name(np);
  5005. err = 0;
  5006. for (i = 0; i < np->num_ldg; i++) {
  5007. struct niu_ldg *lp = &np->ldg[i];
  5008. err = request_irq(lp->irq, niu_interrupt, IRQF_SHARED,
  5009. np->irq_name[i], lp);
  5010. if (err)
  5011. goto out_free_irqs;
  5012. }
  5013. return 0;
  5014. out_free_irqs:
  5015. for (j = 0; j < i; j++) {
  5016. struct niu_ldg *lp = &np->ldg[j];
  5017. free_irq(lp->irq, lp);
  5018. }
  5019. return err;
  5020. }
  5021. static void niu_free_irq(struct niu *np)
  5022. {
  5023. int i;
  5024. for (i = 0; i < np->num_ldg; i++) {
  5025. struct niu_ldg *lp = &np->ldg[i];
  5026. free_irq(lp->irq, lp);
  5027. }
  5028. }
  5029. static void niu_enable_napi(struct niu *np)
  5030. {
  5031. int i;
  5032. for (i = 0; i < np->num_ldg; i++)
  5033. napi_enable(&np->ldg[i].napi);
  5034. }
  5035. static void niu_disable_napi(struct niu *np)
  5036. {
  5037. int i;
  5038. for (i = 0; i < np->num_ldg; i++)
  5039. napi_disable(&np->ldg[i].napi);
  5040. }
  5041. static int niu_open(struct net_device *dev)
  5042. {
  5043. struct niu *np = netdev_priv(dev);
  5044. int err;
  5045. netif_carrier_off(dev);
  5046. err = niu_alloc_channels(np);
  5047. if (err)
  5048. goto out_err;
  5049. err = niu_enable_interrupts(np, 0);
  5050. if (err)
  5051. goto out_free_channels;
  5052. err = niu_request_irq(np);
  5053. if (err)
  5054. goto out_free_channels;
  5055. niu_enable_napi(np);
  5056. spin_lock_irq(&np->lock);
  5057. err = niu_init_hw(np);
  5058. if (!err) {
  5059. init_timer(&np->timer);
  5060. np->timer.expires = jiffies + HZ;
  5061. np->timer.data = (unsigned long) np;
  5062. np->timer.function = niu_timer;
  5063. err = niu_enable_interrupts(np, 1);
  5064. if (err)
  5065. niu_stop_hw(np);
  5066. }
  5067. spin_unlock_irq(&np->lock);
  5068. if (err) {
  5069. niu_disable_napi(np);
  5070. goto out_free_irq;
  5071. }
  5072. netif_tx_start_all_queues(dev);
  5073. if (np->link_config.loopback_mode != LOOPBACK_DISABLED)
  5074. netif_carrier_on(dev);
  5075. add_timer(&np->timer);
  5076. return 0;
  5077. out_free_irq:
  5078. niu_free_irq(np);
  5079. out_free_channels:
  5080. niu_free_channels(np);
  5081. out_err:
  5082. return err;
  5083. }
  5084. static void niu_full_shutdown(struct niu *np, struct net_device *dev)
  5085. {
  5086. cancel_work_sync(&np->reset_task);
  5087. niu_disable_napi(np);
  5088. netif_tx_stop_all_queues(dev);
  5089. del_timer_sync(&np->timer);
  5090. spin_lock_irq(&np->lock);
  5091. niu_stop_hw(np);
  5092. spin_unlock_irq(&np->lock);
  5093. }
  5094. static int niu_close(struct net_device *dev)
  5095. {
  5096. struct niu *np = netdev_priv(dev);
  5097. niu_full_shutdown(np, dev);
  5098. niu_free_irq(np);
  5099. niu_free_channels(np);
  5100. niu_handle_led(np, 0);
  5101. return 0;
  5102. }
  5103. static void niu_sync_xmac_stats(struct niu *np)
  5104. {
  5105. struct niu_xmac_stats *mp = &np->mac_stats.xmac;
  5106. mp->tx_frames += nr64_mac(TXMAC_FRM_CNT);
  5107. mp->tx_bytes += nr64_mac(TXMAC_BYTE_CNT);
  5108. mp->rx_link_faults += nr64_mac(LINK_FAULT_CNT);
  5109. mp->rx_align_errors += nr64_mac(RXMAC_ALIGN_ERR_CNT);
  5110. mp->rx_frags += nr64_mac(RXMAC_FRAG_CNT);
  5111. mp->rx_mcasts += nr64_mac(RXMAC_MC_FRM_CNT);
  5112. mp->rx_bcasts += nr64_mac(RXMAC_BC_FRM_CNT);
  5113. mp->rx_hist_cnt1 += nr64_mac(RXMAC_HIST_CNT1);
  5114. mp->rx_hist_cnt2 += nr64_mac(RXMAC_HIST_CNT2);
  5115. mp->rx_hist_cnt3 += nr64_mac(RXMAC_HIST_CNT3);
  5116. mp->rx_hist_cnt4 += nr64_mac(RXMAC_HIST_CNT4);
  5117. mp->rx_hist_cnt5 += nr64_mac(RXMAC_HIST_CNT5);
  5118. mp->rx_hist_cnt6 += nr64_mac(RXMAC_HIST_CNT6);
  5119. mp->rx_hist_cnt7 += nr64_mac(RXMAC_HIST_CNT7);
  5120. mp->rx_octets += nr64_mac(RXMAC_BT_CNT);
  5121. mp->rx_code_violations += nr64_mac(RXMAC_CD_VIO_CNT);
  5122. mp->rx_len_errors += nr64_mac(RXMAC_MPSZER_CNT);
  5123. mp->rx_crc_errors += nr64_mac(RXMAC_CRC_ER_CNT);
  5124. }
  5125. static void niu_sync_bmac_stats(struct niu *np)
  5126. {
  5127. struct niu_bmac_stats *mp = &np->mac_stats.bmac;
  5128. mp->tx_bytes += nr64_mac(BTXMAC_BYTE_CNT);
  5129. mp->tx_frames += nr64_mac(BTXMAC_FRM_CNT);
  5130. mp->rx_frames += nr64_mac(BRXMAC_FRAME_CNT);
  5131. mp->rx_align_errors += nr64_mac(BRXMAC_ALIGN_ERR_CNT);
  5132. mp->rx_crc_errors += nr64_mac(BRXMAC_ALIGN_ERR_CNT);
  5133. mp->rx_len_errors += nr64_mac(BRXMAC_CODE_VIOL_ERR_CNT);
  5134. }
  5135. static void niu_sync_mac_stats(struct niu *np)
  5136. {
  5137. if (np->flags & NIU_FLAGS_XMAC)
  5138. niu_sync_xmac_stats(np);
  5139. else
  5140. niu_sync_bmac_stats(np);
  5141. }
  5142. static void niu_get_rx_stats(struct niu *np)
  5143. {
  5144. unsigned long pkts, dropped, errors, bytes;
  5145. struct rx_ring_info *rx_rings;
  5146. int i;
  5147. pkts = dropped = errors = bytes = 0;
  5148. rx_rings = ACCESS_ONCE(np->rx_rings);
  5149. if (!rx_rings)
  5150. goto no_rings;
  5151. for (i = 0; i < np->num_rx_rings; i++) {
  5152. struct rx_ring_info *rp = &rx_rings[i];
  5153. niu_sync_rx_discard_stats(np, rp, 0);
  5154. pkts += rp->rx_packets;
  5155. bytes += rp->rx_bytes;
  5156. dropped += rp->rx_dropped;
  5157. errors += rp->rx_errors;
  5158. }
  5159. no_rings:
  5160. np->dev->stats.rx_packets = pkts;
  5161. np->dev->stats.rx_bytes = bytes;
  5162. np->dev->stats.rx_dropped = dropped;
  5163. np->dev->stats.rx_errors = errors;
  5164. }
  5165. static void niu_get_tx_stats(struct niu *np)
  5166. {
  5167. unsigned long pkts, errors, bytes;
  5168. struct tx_ring_info *tx_rings;
  5169. int i;
  5170. pkts = errors = bytes = 0;
  5171. tx_rings = ACCESS_ONCE(np->tx_rings);
  5172. if (!tx_rings)
  5173. goto no_rings;
  5174. for (i = 0; i < np->num_tx_rings; i++) {
  5175. struct tx_ring_info *rp = &tx_rings[i];
  5176. pkts += rp->tx_packets;
  5177. bytes += rp->tx_bytes;
  5178. errors += rp->tx_errors;
  5179. }
  5180. no_rings:
  5181. np->dev->stats.tx_packets = pkts;
  5182. np->dev->stats.tx_bytes = bytes;
  5183. np->dev->stats.tx_errors = errors;
  5184. }
  5185. static struct net_device_stats *niu_get_stats(struct net_device *dev)
  5186. {
  5187. struct niu *np = netdev_priv(dev);
  5188. if (netif_running(dev)) {
  5189. niu_get_rx_stats(np);
  5190. niu_get_tx_stats(np);
  5191. }
  5192. return &dev->stats;
  5193. }
  5194. static void niu_load_hash_xmac(struct niu *np, u16 *hash)
  5195. {
  5196. int i;
  5197. for (i = 0; i < 16; i++)
  5198. nw64_mac(XMAC_HASH_TBL(i), hash[i]);
  5199. }
  5200. static void niu_load_hash_bmac(struct niu *np, u16 *hash)
  5201. {
  5202. int i;
  5203. for (i = 0; i < 16; i++)
  5204. nw64_mac(BMAC_HASH_TBL(i), hash[i]);
  5205. }
  5206. static void niu_load_hash(struct niu *np, u16 *hash)
  5207. {
  5208. if (np->flags & NIU_FLAGS_XMAC)
  5209. niu_load_hash_xmac(np, hash);
  5210. else
  5211. niu_load_hash_bmac(np, hash);
  5212. }
  5213. static void niu_set_rx_mode(struct net_device *dev)
  5214. {
  5215. struct niu *np = netdev_priv(dev);
  5216. int i, alt_cnt, err;
  5217. struct netdev_hw_addr *ha;
  5218. unsigned long flags;
  5219. u16 hash[16] = { 0, };
  5220. spin_lock_irqsave(&np->lock, flags);
  5221. niu_enable_rx_mac(np, 0);
  5222. np->flags &= ~(NIU_FLAGS_MCAST | NIU_FLAGS_PROMISC);
  5223. if (dev->flags & IFF_PROMISC)
  5224. np->flags |= NIU_FLAGS_PROMISC;
  5225. if ((dev->flags & IFF_ALLMULTI) || (!netdev_mc_empty(dev)))
  5226. np->flags |= NIU_FLAGS_MCAST;
  5227. alt_cnt = netdev_uc_count(dev);
  5228. if (alt_cnt > niu_num_alt_addr(np)) {
  5229. alt_cnt = 0;
  5230. np->flags |= NIU_FLAGS_PROMISC;
  5231. }
  5232. if (alt_cnt) {
  5233. int index = 0;
  5234. netdev_for_each_uc_addr(ha, dev) {
  5235. err = niu_set_alt_mac(np, index, ha->addr);
  5236. if (err)
  5237. netdev_warn(dev, "Error %d adding alt mac %d\n",
  5238. err, index);
  5239. err = niu_enable_alt_mac(np, index, 1);
  5240. if (err)
  5241. netdev_warn(dev, "Error %d enabling alt mac %d\n",
  5242. err, index);
  5243. index++;
  5244. }
  5245. } else {
  5246. int alt_start;
  5247. if (np->flags & NIU_FLAGS_XMAC)
  5248. alt_start = 0;
  5249. else
  5250. alt_start = 1;
  5251. for (i = alt_start; i < niu_num_alt_addr(np); i++) {
  5252. err = niu_enable_alt_mac(np, i, 0);
  5253. if (err)
  5254. netdev_warn(dev, "Error %d disabling alt mac %d\n",
  5255. err, i);
  5256. }
  5257. }
  5258. if (dev->flags & IFF_ALLMULTI) {
  5259. for (i = 0; i < 16; i++)
  5260. hash[i] = 0xffff;
  5261. } else if (!netdev_mc_empty(dev)) {
  5262. netdev_for_each_mc_addr(ha, dev) {
  5263. u32 crc = ether_crc_le(ETH_ALEN, ha->addr);
  5264. crc >>= 24;
  5265. hash[crc >> 4] |= (1 << (15 - (crc & 0xf)));
  5266. }
  5267. }
  5268. if (np->flags & NIU_FLAGS_MCAST)
  5269. niu_load_hash(np, hash);
  5270. niu_enable_rx_mac(np, 1);
  5271. spin_unlock_irqrestore(&np->lock, flags);
  5272. }
  5273. static int niu_set_mac_addr(struct net_device *dev, void *p)
  5274. {
  5275. struct niu *np = netdev_priv(dev);
  5276. struct sockaddr *addr = p;
  5277. unsigned long flags;
  5278. if (!is_valid_ether_addr(addr->sa_data))
  5279. return -EINVAL;
  5280. memcpy(dev->dev_addr, addr->sa_data, ETH_ALEN);
  5281. if (!netif_running(dev))
  5282. return 0;
  5283. spin_lock_irqsave(&np->lock, flags);
  5284. niu_enable_rx_mac(np, 0);
  5285. niu_set_primary_mac(np, dev->dev_addr);
  5286. niu_enable_rx_mac(np, 1);
  5287. spin_unlock_irqrestore(&np->lock, flags);
  5288. return 0;
  5289. }
  5290. static int niu_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  5291. {
  5292. return -EOPNOTSUPP;
  5293. }
  5294. static void niu_netif_stop(struct niu *np)
  5295. {
  5296. np->dev->trans_start = jiffies; /* prevent tx timeout */
  5297. niu_disable_napi(np);
  5298. netif_tx_disable(np->dev);
  5299. }
  5300. static void niu_netif_start(struct niu *np)
  5301. {
  5302. /* NOTE: unconditional netif_wake_queue is only appropriate
  5303. * so long as all callers are assured to have free tx slots
  5304. * (such as after niu_init_hw).
  5305. */
  5306. netif_tx_wake_all_queues(np->dev);
  5307. niu_enable_napi(np);
  5308. niu_enable_interrupts(np, 1);
  5309. }
  5310. static void niu_reset_buffers(struct niu *np)
  5311. {
  5312. int i, j, k, err;
  5313. if (np->rx_rings) {
  5314. for (i = 0; i < np->num_rx_rings; i++) {
  5315. struct rx_ring_info *rp = &np->rx_rings[i];
  5316. for (j = 0, k = 0; j < MAX_RBR_RING_SIZE; j++) {
  5317. struct page *page;
  5318. page = rp->rxhash[j];
  5319. while (page) {
  5320. struct page *next =
  5321. (struct page *) page->mapping;
  5322. u64 base = page->index;
  5323. base = base >> RBR_DESCR_ADDR_SHIFT;
  5324. rp->rbr[k++] = cpu_to_le32(base);
  5325. page = next;
  5326. }
  5327. }
  5328. for (; k < MAX_RBR_RING_SIZE; k++) {
  5329. err = niu_rbr_add_page(np, rp, GFP_ATOMIC, k);
  5330. if (unlikely(err))
  5331. break;
  5332. }
  5333. rp->rbr_index = rp->rbr_table_size - 1;
  5334. rp->rcr_index = 0;
  5335. rp->rbr_pending = 0;
  5336. rp->rbr_refill_pending = 0;
  5337. }
  5338. }
  5339. if (np->tx_rings) {
  5340. for (i = 0; i < np->num_tx_rings; i++) {
  5341. struct tx_ring_info *rp = &np->tx_rings[i];
  5342. for (j = 0; j < MAX_TX_RING_SIZE; j++) {
  5343. if (rp->tx_buffs[j].skb)
  5344. (void) release_tx_packet(np, rp, j);
  5345. }
  5346. rp->pending = MAX_TX_RING_SIZE;
  5347. rp->prod = 0;
  5348. rp->cons = 0;
  5349. rp->wrap_bit = 0;
  5350. }
  5351. }
  5352. }
  5353. static void niu_reset_task(struct work_struct *work)
  5354. {
  5355. struct niu *np = container_of(work, struct niu, reset_task);
  5356. unsigned long flags;
  5357. int err;
  5358. spin_lock_irqsave(&np->lock, flags);
  5359. if (!netif_running(np->dev)) {
  5360. spin_unlock_irqrestore(&np->lock, flags);
  5361. return;
  5362. }
  5363. spin_unlock_irqrestore(&np->lock, flags);
  5364. del_timer_sync(&np->timer);
  5365. niu_netif_stop(np);
  5366. spin_lock_irqsave(&np->lock, flags);
  5367. niu_stop_hw(np);
  5368. spin_unlock_irqrestore(&np->lock, flags);
  5369. niu_reset_buffers(np);
  5370. spin_lock_irqsave(&np->lock, flags);
  5371. err = niu_init_hw(np);
  5372. if (!err) {
  5373. np->timer.expires = jiffies + HZ;
  5374. add_timer(&np->timer);
  5375. niu_netif_start(np);
  5376. }
  5377. spin_unlock_irqrestore(&np->lock, flags);
  5378. }
  5379. static void niu_tx_timeout(struct net_device *dev)
  5380. {
  5381. struct niu *np = netdev_priv(dev);
  5382. dev_err(np->device, "%s: Transmit timed out, resetting\n",
  5383. dev->name);
  5384. schedule_work(&np->reset_task);
  5385. }
  5386. static void niu_set_txd(struct tx_ring_info *rp, int index,
  5387. u64 mapping, u64 len, u64 mark,
  5388. u64 n_frags)
  5389. {
  5390. __le64 *desc = &rp->descr[index];
  5391. *desc = cpu_to_le64(mark |
  5392. (n_frags << TX_DESC_NUM_PTR_SHIFT) |
  5393. (len << TX_DESC_TR_LEN_SHIFT) |
  5394. (mapping & TX_DESC_SAD));
  5395. }
  5396. static u64 niu_compute_tx_flags(struct sk_buff *skb, struct ethhdr *ehdr,
  5397. u64 pad_bytes, u64 len)
  5398. {
  5399. u16 eth_proto, eth_proto_inner;
  5400. u64 csum_bits, l3off, ihl, ret;
  5401. u8 ip_proto;
  5402. int ipv6;
  5403. eth_proto = be16_to_cpu(ehdr->h_proto);
  5404. eth_proto_inner = eth_proto;
  5405. if (eth_proto == ETH_P_8021Q) {
  5406. struct vlan_ethhdr *vp = (struct vlan_ethhdr *) ehdr;
  5407. __be16 val = vp->h_vlan_encapsulated_proto;
  5408. eth_proto_inner = be16_to_cpu(val);
  5409. }
  5410. ipv6 = ihl = 0;
  5411. switch (skb->protocol) {
  5412. case cpu_to_be16(ETH_P_IP):
  5413. ip_proto = ip_hdr(skb)->protocol;
  5414. ihl = ip_hdr(skb)->ihl;
  5415. break;
  5416. case cpu_to_be16(ETH_P_IPV6):
  5417. ip_proto = ipv6_hdr(skb)->nexthdr;
  5418. ihl = (40 >> 2);
  5419. ipv6 = 1;
  5420. break;
  5421. default:
  5422. ip_proto = ihl = 0;
  5423. break;
  5424. }
  5425. csum_bits = TXHDR_CSUM_NONE;
  5426. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  5427. u64 start, stuff;
  5428. csum_bits = (ip_proto == IPPROTO_TCP ?
  5429. TXHDR_CSUM_TCP :
  5430. (ip_proto == IPPROTO_UDP ?
  5431. TXHDR_CSUM_UDP : TXHDR_CSUM_SCTP));
  5432. start = skb_checksum_start_offset(skb) -
  5433. (pad_bytes + sizeof(struct tx_pkt_hdr));
  5434. stuff = start + skb->csum_offset;
  5435. csum_bits |= (start / 2) << TXHDR_L4START_SHIFT;
  5436. csum_bits |= (stuff / 2) << TXHDR_L4STUFF_SHIFT;
  5437. }
  5438. l3off = skb_network_offset(skb) -
  5439. (pad_bytes + sizeof(struct tx_pkt_hdr));
  5440. ret = (((pad_bytes / 2) << TXHDR_PAD_SHIFT) |
  5441. (len << TXHDR_LEN_SHIFT) |
  5442. ((l3off / 2) << TXHDR_L3START_SHIFT) |
  5443. (ihl << TXHDR_IHL_SHIFT) |
  5444. ((eth_proto_inner < 1536) ? TXHDR_LLC : 0) |
  5445. ((eth_proto == ETH_P_8021Q) ? TXHDR_VLAN : 0) |
  5446. (ipv6 ? TXHDR_IP_VER : 0) |
  5447. csum_bits);
  5448. return ret;
  5449. }
  5450. static netdev_tx_t niu_start_xmit(struct sk_buff *skb,
  5451. struct net_device *dev)
  5452. {
  5453. struct niu *np = netdev_priv(dev);
  5454. unsigned long align, headroom;
  5455. struct netdev_queue *txq;
  5456. struct tx_ring_info *rp;
  5457. struct tx_pkt_hdr *tp;
  5458. unsigned int len, nfg;
  5459. struct ethhdr *ehdr;
  5460. int prod, i, tlen;
  5461. u64 mapping, mrk;
  5462. i = skb_get_queue_mapping(skb);
  5463. rp = &np->tx_rings[i];
  5464. txq = netdev_get_tx_queue(dev, i);
  5465. if (niu_tx_avail(rp) <= (skb_shinfo(skb)->nr_frags + 1)) {
  5466. netif_tx_stop_queue(txq);
  5467. dev_err(np->device, "%s: BUG! Tx ring full when queue awake!\n", dev->name);
  5468. rp->tx_errors++;
  5469. return NETDEV_TX_BUSY;
  5470. }
  5471. if (skb->len < ETH_ZLEN) {
  5472. unsigned int pad_bytes = ETH_ZLEN - skb->len;
  5473. if (skb_pad(skb, pad_bytes))
  5474. goto out;
  5475. skb_put(skb, pad_bytes);
  5476. }
  5477. len = sizeof(struct tx_pkt_hdr) + 15;
  5478. if (skb_headroom(skb) < len) {
  5479. struct sk_buff *skb_new;
  5480. skb_new = skb_realloc_headroom(skb, len);
  5481. if (!skb_new) {
  5482. rp->tx_errors++;
  5483. goto out_drop;
  5484. }
  5485. kfree_skb(skb);
  5486. skb = skb_new;
  5487. } else
  5488. skb_orphan(skb);
  5489. align = ((unsigned long) skb->data & (16 - 1));
  5490. headroom = align + sizeof(struct tx_pkt_hdr);
  5491. ehdr = (struct ethhdr *) skb->data;
  5492. tp = (struct tx_pkt_hdr *) skb_push(skb, headroom);
  5493. len = skb->len - sizeof(struct tx_pkt_hdr);
  5494. tp->flags = cpu_to_le64(niu_compute_tx_flags(skb, ehdr, align, len));
  5495. tp->resv = 0;
  5496. len = skb_headlen(skb);
  5497. mapping = np->ops->map_single(np->device, skb->data,
  5498. len, DMA_TO_DEVICE);
  5499. prod = rp->prod;
  5500. rp->tx_buffs[prod].skb = skb;
  5501. rp->tx_buffs[prod].mapping = mapping;
  5502. mrk = TX_DESC_SOP;
  5503. if (++rp->mark_counter == rp->mark_freq) {
  5504. rp->mark_counter = 0;
  5505. mrk |= TX_DESC_MARK;
  5506. rp->mark_pending++;
  5507. }
  5508. tlen = len;
  5509. nfg = skb_shinfo(skb)->nr_frags;
  5510. while (tlen > 0) {
  5511. tlen -= MAX_TX_DESC_LEN;
  5512. nfg++;
  5513. }
  5514. while (len > 0) {
  5515. unsigned int this_len = len;
  5516. if (this_len > MAX_TX_DESC_LEN)
  5517. this_len = MAX_TX_DESC_LEN;
  5518. niu_set_txd(rp, prod, mapping, this_len, mrk, nfg);
  5519. mrk = nfg = 0;
  5520. prod = NEXT_TX(rp, prod);
  5521. mapping += this_len;
  5522. len -= this_len;
  5523. }
  5524. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  5525. skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  5526. len = frag->size;
  5527. mapping = np->ops->map_page(np->device, frag->page,
  5528. frag->page_offset, len,
  5529. DMA_TO_DEVICE);
  5530. rp->tx_buffs[prod].skb = NULL;
  5531. rp->tx_buffs[prod].mapping = mapping;
  5532. niu_set_txd(rp, prod, mapping, len, 0, 0);
  5533. prod = NEXT_TX(rp, prod);
  5534. }
  5535. if (prod < rp->prod)
  5536. rp->wrap_bit ^= TX_RING_KICK_WRAP;
  5537. rp->prod = prod;
  5538. nw64(TX_RING_KICK(rp->tx_channel), rp->wrap_bit | (prod << 3));
  5539. if (unlikely(niu_tx_avail(rp) <= (MAX_SKB_FRAGS + 1))) {
  5540. netif_tx_stop_queue(txq);
  5541. if (niu_tx_avail(rp) > NIU_TX_WAKEUP_THRESH(rp))
  5542. netif_tx_wake_queue(txq);
  5543. }
  5544. out:
  5545. return NETDEV_TX_OK;
  5546. out_drop:
  5547. rp->tx_errors++;
  5548. kfree_skb(skb);
  5549. goto out;
  5550. }
  5551. static int niu_change_mtu(struct net_device *dev, int new_mtu)
  5552. {
  5553. struct niu *np = netdev_priv(dev);
  5554. int err, orig_jumbo, new_jumbo;
  5555. if (new_mtu < 68 || new_mtu > NIU_MAX_MTU)
  5556. return -EINVAL;
  5557. orig_jumbo = (dev->mtu > ETH_DATA_LEN);
  5558. new_jumbo = (new_mtu > ETH_DATA_LEN);
  5559. dev->mtu = new_mtu;
  5560. if (!netif_running(dev) ||
  5561. (orig_jumbo == new_jumbo))
  5562. return 0;
  5563. niu_full_shutdown(np, dev);
  5564. niu_free_channels(np);
  5565. niu_enable_napi(np);
  5566. err = niu_alloc_channels(np);
  5567. if (err)
  5568. return err;
  5569. spin_lock_irq(&np->lock);
  5570. err = niu_init_hw(np);
  5571. if (!err) {
  5572. init_timer(&np->timer);
  5573. np->timer.expires = jiffies + HZ;
  5574. np->timer.data = (unsigned long) np;
  5575. np->timer.function = niu_timer;
  5576. err = niu_enable_interrupts(np, 1);
  5577. if (err)
  5578. niu_stop_hw(np);
  5579. }
  5580. spin_unlock_irq(&np->lock);
  5581. if (!err) {
  5582. netif_tx_start_all_queues(dev);
  5583. if (np->link_config.loopback_mode != LOOPBACK_DISABLED)
  5584. netif_carrier_on(dev);
  5585. add_timer(&np->timer);
  5586. }
  5587. return err;
  5588. }
  5589. static void niu_get_drvinfo(struct net_device *dev,
  5590. struct ethtool_drvinfo *info)
  5591. {
  5592. struct niu *np = netdev_priv(dev);
  5593. struct niu_vpd *vpd = &np->vpd;
  5594. strcpy(info->driver, DRV_MODULE_NAME);
  5595. strcpy(info->version, DRV_MODULE_VERSION);
  5596. sprintf(info->fw_version, "%d.%d",
  5597. vpd->fcode_major, vpd->fcode_minor);
  5598. if (np->parent->plat_type != PLAT_TYPE_NIU)
  5599. strcpy(info->bus_info, pci_name(np->pdev));
  5600. }
  5601. static int niu_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  5602. {
  5603. struct niu *np = netdev_priv(dev);
  5604. struct niu_link_config *lp;
  5605. lp = &np->link_config;
  5606. memset(cmd, 0, sizeof(*cmd));
  5607. cmd->phy_address = np->phy_addr;
  5608. cmd->supported = lp->supported;
  5609. cmd->advertising = lp->active_advertising;
  5610. cmd->autoneg = lp->active_autoneg;
  5611. ethtool_cmd_speed_set(cmd, lp->active_speed);
  5612. cmd->duplex = lp->active_duplex;
  5613. cmd->port = (np->flags & NIU_FLAGS_FIBER) ? PORT_FIBRE : PORT_TP;
  5614. cmd->transceiver = (np->flags & NIU_FLAGS_XCVR_SERDES) ?
  5615. XCVR_EXTERNAL : XCVR_INTERNAL;
  5616. return 0;
  5617. }
  5618. static int niu_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
  5619. {
  5620. struct niu *np = netdev_priv(dev);
  5621. struct niu_link_config *lp = &np->link_config;
  5622. lp->advertising = cmd->advertising;
  5623. lp->speed = ethtool_cmd_speed(cmd);
  5624. lp->duplex = cmd->duplex;
  5625. lp->autoneg = cmd->autoneg;
  5626. return niu_init_link(np);
  5627. }
  5628. static u32 niu_get_msglevel(struct net_device *dev)
  5629. {
  5630. struct niu *np = netdev_priv(dev);
  5631. return np->msg_enable;
  5632. }
  5633. static void niu_set_msglevel(struct net_device *dev, u32 value)
  5634. {
  5635. struct niu *np = netdev_priv(dev);
  5636. np->msg_enable = value;
  5637. }
  5638. static int niu_nway_reset(struct net_device *dev)
  5639. {
  5640. struct niu *np = netdev_priv(dev);
  5641. if (np->link_config.autoneg)
  5642. return niu_init_link(np);
  5643. return 0;
  5644. }
  5645. static int niu_get_eeprom_len(struct net_device *dev)
  5646. {
  5647. struct niu *np = netdev_priv(dev);
  5648. return np->eeprom_len;
  5649. }
  5650. static int niu_get_eeprom(struct net_device *dev,
  5651. struct ethtool_eeprom *eeprom, u8 *data)
  5652. {
  5653. struct niu *np = netdev_priv(dev);
  5654. u32 offset, len, val;
  5655. offset = eeprom->offset;
  5656. len = eeprom->len;
  5657. if (offset + len < offset)
  5658. return -EINVAL;
  5659. if (offset >= np->eeprom_len)
  5660. return -EINVAL;
  5661. if (offset + len > np->eeprom_len)
  5662. len = eeprom->len = np->eeprom_len - offset;
  5663. if (offset & 3) {
  5664. u32 b_offset, b_count;
  5665. b_offset = offset & 3;
  5666. b_count = 4 - b_offset;
  5667. if (b_count > len)
  5668. b_count = len;
  5669. val = nr64(ESPC_NCR((offset - b_offset) / 4));
  5670. memcpy(data, ((char *)&val) + b_offset, b_count);
  5671. data += b_count;
  5672. len -= b_count;
  5673. offset += b_count;
  5674. }
  5675. while (len >= 4) {
  5676. val = nr64(ESPC_NCR(offset / 4));
  5677. memcpy(data, &val, 4);
  5678. data += 4;
  5679. len -= 4;
  5680. offset += 4;
  5681. }
  5682. if (len) {
  5683. val = nr64(ESPC_NCR(offset / 4));
  5684. memcpy(data, &val, len);
  5685. }
  5686. return 0;
  5687. }
  5688. static void niu_ethflow_to_l3proto(int flow_type, u8 *pid)
  5689. {
  5690. switch (flow_type) {
  5691. case TCP_V4_FLOW:
  5692. case TCP_V6_FLOW:
  5693. *pid = IPPROTO_TCP;
  5694. break;
  5695. case UDP_V4_FLOW:
  5696. case UDP_V6_FLOW:
  5697. *pid = IPPROTO_UDP;
  5698. break;
  5699. case SCTP_V4_FLOW:
  5700. case SCTP_V6_FLOW:
  5701. *pid = IPPROTO_SCTP;
  5702. break;
  5703. case AH_V4_FLOW:
  5704. case AH_V6_FLOW:
  5705. *pid = IPPROTO_AH;
  5706. break;
  5707. case ESP_V4_FLOW:
  5708. case ESP_V6_FLOW:
  5709. *pid = IPPROTO_ESP;
  5710. break;
  5711. default:
  5712. *pid = 0;
  5713. break;
  5714. }
  5715. }
  5716. static int niu_class_to_ethflow(u64 class, int *flow_type)
  5717. {
  5718. switch (class) {
  5719. case CLASS_CODE_TCP_IPV4:
  5720. *flow_type = TCP_V4_FLOW;
  5721. break;
  5722. case CLASS_CODE_UDP_IPV4:
  5723. *flow_type = UDP_V4_FLOW;
  5724. break;
  5725. case CLASS_CODE_AH_ESP_IPV4:
  5726. *flow_type = AH_V4_FLOW;
  5727. break;
  5728. case CLASS_CODE_SCTP_IPV4:
  5729. *flow_type = SCTP_V4_FLOW;
  5730. break;
  5731. case CLASS_CODE_TCP_IPV6:
  5732. *flow_type = TCP_V6_FLOW;
  5733. break;
  5734. case CLASS_CODE_UDP_IPV6:
  5735. *flow_type = UDP_V6_FLOW;
  5736. break;
  5737. case CLASS_CODE_AH_ESP_IPV6:
  5738. *flow_type = AH_V6_FLOW;
  5739. break;
  5740. case CLASS_CODE_SCTP_IPV6:
  5741. *flow_type = SCTP_V6_FLOW;
  5742. break;
  5743. case CLASS_CODE_USER_PROG1:
  5744. case CLASS_CODE_USER_PROG2:
  5745. case CLASS_CODE_USER_PROG3:
  5746. case CLASS_CODE_USER_PROG4:
  5747. *flow_type = IP_USER_FLOW;
  5748. break;
  5749. default:
  5750. return 0;
  5751. }
  5752. return 1;
  5753. }
  5754. static int niu_ethflow_to_class(int flow_type, u64 *class)
  5755. {
  5756. switch (flow_type) {
  5757. case TCP_V4_FLOW:
  5758. *class = CLASS_CODE_TCP_IPV4;
  5759. break;
  5760. case UDP_V4_FLOW:
  5761. *class = CLASS_CODE_UDP_IPV4;
  5762. break;
  5763. case AH_ESP_V4_FLOW:
  5764. case AH_V4_FLOW:
  5765. case ESP_V4_FLOW:
  5766. *class = CLASS_CODE_AH_ESP_IPV4;
  5767. break;
  5768. case SCTP_V4_FLOW:
  5769. *class = CLASS_CODE_SCTP_IPV4;
  5770. break;
  5771. case TCP_V6_FLOW:
  5772. *class = CLASS_CODE_TCP_IPV6;
  5773. break;
  5774. case UDP_V6_FLOW:
  5775. *class = CLASS_CODE_UDP_IPV6;
  5776. break;
  5777. case AH_ESP_V6_FLOW:
  5778. case AH_V6_FLOW:
  5779. case ESP_V6_FLOW:
  5780. *class = CLASS_CODE_AH_ESP_IPV6;
  5781. break;
  5782. case SCTP_V6_FLOW:
  5783. *class = CLASS_CODE_SCTP_IPV6;
  5784. break;
  5785. default:
  5786. return 0;
  5787. }
  5788. return 1;
  5789. }
  5790. static u64 niu_flowkey_to_ethflow(u64 flow_key)
  5791. {
  5792. u64 ethflow = 0;
  5793. if (flow_key & FLOW_KEY_L2DA)
  5794. ethflow |= RXH_L2DA;
  5795. if (flow_key & FLOW_KEY_VLAN)
  5796. ethflow |= RXH_VLAN;
  5797. if (flow_key & FLOW_KEY_IPSA)
  5798. ethflow |= RXH_IP_SRC;
  5799. if (flow_key & FLOW_KEY_IPDA)
  5800. ethflow |= RXH_IP_DST;
  5801. if (flow_key & FLOW_KEY_PROTO)
  5802. ethflow |= RXH_L3_PROTO;
  5803. if (flow_key & (FLOW_KEY_L4_BYTE12 << FLOW_KEY_L4_0_SHIFT))
  5804. ethflow |= RXH_L4_B_0_1;
  5805. if (flow_key & (FLOW_KEY_L4_BYTE12 << FLOW_KEY_L4_1_SHIFT))
  5806. ethflow |= RXH_L4_B_2_3;
  5807. return ethflow;
  5808. }
  5809. static int niu_ethflow_to_flowkey(u64 ethflow, u64 *flow_key)
  5810. {
  5811. u64 key = 0;
  5812. if (ethflow & RXH_L2DA)
  5813. key |= FLOW_KEY_L2DA;
  5814. if (ethflow & RXH_VLAN)
  5815. key |= FLOW_KEY_VLAN;
  5816. if (ethflow & RXH_IP_SRC)
  5817. key |= FLOW_KEY_IPSA;
  5818. if (ethflow & RXH_IP_DST)
  5819. key |= FLOW_KEY_IPDA;
  5820. if (ethflow & RXH_L3_PROTO)
  5821. key |= FLOW_KEY_PROTO;
  5822. if (ethflow & RXH_L4_B_0_1)
  5823. key |= (FLOW_KEY_L4_BYTE12 << FLOW_KEY_L4_0_SHIFT);
  5824. if (ethflow & RXH_L4_B_2_3)
  5825. key |= (FLOW_KEY_L4_BYTE12 << FLOW_KEY_L4_1_SHIFT);
  5826. *flow_key = key;
  5827. return 1;
  5828. }
  5829. static int niu_get_hash_opts(struct niu *np, struct ethtool_rxnfc *nfc)
  5830. {
  5831. u64 class;
  5832. nfc->data = 0;
  5833. if (!niu_ethflow_to_class(nfc->flow_type, &class))
  5834. return -EINVAL;
  5835. if (np->parent->tcam_key[class - CLASS_CODE_USER_PROG1] &
  5836. TCAM_KEY_DISC)
  5837. nfc->data = RXH_DISCARD;
  5838. else
  5839. nfc->data = niu_flowkey_to_ethflow(np->parent->flow_key[class -
  5840. CLASS_CODE_USER_PROG1]);
  5841. return 0;
  5842. }
  5843. static void niu_get_ip4fs_from_tcam_key(struct niu_tcam_entry *tp,
  5844. struct ethtool_rx_flow_spec *fsp)
  5845. {
  5846. u32 tmp;
  5847. u16 prt;
  5848. tmp = (tp->key[3] & TCAM_V4KEY3_SADDR) >> TCAM_V4KEY3_SADDR_SHIFT;
  5849. fsp->h_u.tcp_ip4_spec.ip4src = cpu_to_be32(tmp);
  5850. tmp = (tp->key[3] & TCAM_V4KEY3_DADDR) >> TCAM_V4KEY3_DADDR_SHIFT;
  5851. fsp->h_u.tcp_ip4_spec.ip4dst = cpu_to_be32(tmp);
  5852. tmp = (tp->key_mask[3] & TCAM_V4KEY3_SADDR) >> TCAM_V4KEY3_SADDR_SHIFT;
  5853. fsp->m_u.tcp_ip4_spec.ip4src = cpu_to_be32(tmp);
  5854. tmp = (tp->key_mask[3] & TCAM_V4KEY3_DADDR) >> TCAM_V4KEY3_DADDR_SHIFT;
  5855. fsp->m_u.tcp_ip4_spec.ip4dst = cpu_to_be32(tmp);
  5856. fsp->h_u.tcp_ip4_spec.tos = (tp->key[2] & TCAM_V4KEY2_TOS) >>
  5857. TCAM_V4KEY2_TOS_SHIFT;
  5858. fsp->m_u.tcp_ip4_spec.tos = (tp->key_mask[2] & TCAM_V4KEY2_TOS) >>
  5859. TCAM_V4KEY2_TOS_SHIFT;
  5860. switch (fsp->flow_type) {
  5861. case TCP_V4_FLOW:
  5862. case UDP_V4_FLOW:
  5863. case SCTP_V4_FLOW:
  5864. prt = ((tp->key[2] & TCAM_V4KEY2_PORT_SPI) >>
  5865. TCAM_V4KEY2_PORT_SPI_SHIFT) >> 16;
  5866. fsp->h_u.tcp_ip4_spec.psrc = cpu_to_be16(prt);
  5867. prt = ((tp->key[2] & TCAM_V4KEY2_PORT_SPI) >>
  5868. TCAM_V4KEY2_PORT_SPI_SHIFT) & 0xffff;
  5869. fsp->h_u.tcp_ip4_spec.pdst = cpu_to_be16(prt);
  5870. prt = ((tp->key_mask[2] & TCAM_V4KEY2_PORT_SPI) >>
  5871. TCAM_V4KEY2_PORT_SPI_SHIFT) >> 16;
  5872. fsp->m_u.tcp_ip4_spec.psrc = cpu_to_be16(prt);
  5873. prt = ((tp->key_mask[2] & TCAM_V4KEY2_PORT_SPI) >>
  5874. TCAM_V4KEY2_PORT_SPI_SHIFT) & 0xffff;
  5875. fsp->m_u.tcp_ip4_spec.pdst = cpu_to_be16(prt);
  5876. break;
  5877. case AH_V4_FLOW:
  5878. case ESP_V4_FLOW:
  5879. tmp = (tp->key[2] & TCAM_V4KEY2_PORT_SPI) >>
  5880. TCAM_V4KEY2_PORT_SPI_SHIFT;
  5881. fsp->h_u.ah_ip4_spec.spi = cpu_to_be32(tmp);
  5882. tmp = (tp->key_mask[2] & TCAM_V4KEY2_PORT_SPI) >>
  5883. TCAM_V4KEY2_PORT_SPI_SHIFT;
  5884. fsp->m_u.ah_ip4_spec.spi = cpu_to_be32(tmp);
  5885. break;
  5886. case IP_USER_FLOW:
  5887. tmp = (tp->key[2] & TCAM_V4KEY2_PORT_SPI) >>
  5888. TCAM_V4KEY2_PORT_SPI_SHIFT;
  5889. fsp->h_u.usr_ip4_spec.l4_4_bytes = cpu_to_be32(tmp);
  5890. tmp = (tp->key_mask[2] & TCAM_V4KEY2_PORT_SPI) >>
  5891. TCAM_V4KEY2_PORT_SPI_SHIFT;
  5892. fsp->m_u.usr_ip4_spec.l4_4_bytes = cpu_to_be32(tmp);
  5893. fsp->h_u.usr_ip4_spec.proto =
  5894. (tp->key[2] & TCAM_V4KEY2_PROTO) >>
  5895. TCAM_V4KEY2_PROTO_SHIFT;
  5896. fsp->m_u.usr_ip4_spec.proto =
  5897. (tp->key_mask[2] & TCAM_V4KEY2_PROTO) >>
  5898. TCAM_V4KEY2_PROTO_SHIFT;
  5899. fsp->h_u.usr_ip4_spec.ip_ver = ETH_RX_NFC_IP4;
  5900. break;
  5901. default:
  5902. break;
  5903. }
  5904. }
  5905. static int niu_get_ethtool_tcam_entry(struct niu *np,
  5906. struct ethtool_rxnfc *nfc)
  5907. {
  5908. struct niu_parent *parent = np->parent;
  5909. struct niu_tcam_entry *tp;
  5910. struct ethtool_rx_flow_spec *fsp = &nfc->fs;
  5911. u16 idx;
  5912. u64 class;
  5913. int ret = 0;
  5914. idx = tcam_get_index(np, (u16)nfc->fs.location);
  5915. tp = &parent->tcam[idx];
  5916. if (!tp->valid) {
  5917. netdev_info(np->dev, "niu%d: entry [%d] invalid for idx[%d]\n",
  5918. parent->index, (u16)nfc->fs.location, idx);
  5919. return -EINVAL;
  5920. }
  5921. /* fill the flow spec entry */
  5922. class = (tp->key[0] & TCAM_V4KEY0_CLASS_CODE) >>
  5923. TCAM_V4KEY0_CLASS_CODE_SHIFT;
  5924. ret = niu_class_to_ethflow(class, &fsp->flow_type);
  5925. if (ret < 0) {
  5926. netdev_info(np->dev, "niu%d: niu_class_to_ethflow failed\n",
  5927. parent->index);
  5928. ret = -EINVAL;
  5929. goto out;
  5930. }
  5931. if (fsp->flow_type == AH_V4_FLOW || fsp->flow_type == AH_V6_FLOW) {
  5932. u32 proto = (tp->key[2] & TCAM_V4KEY2_PROTO) >>
  5933. TCAM_V4KEY2_PROTO_SHIFT;
  5934. if (proto == IPPROTO_ESP) {
  5935. if (fsp->flow_type == AH_V4_FLOW)
  5936. fsp->flow_type = ESP_V4_FLOW;
  5937. else
  5938. fsp->flow_type = ESP_V6_FLOW;
  5939. }
  5940. }
  5941. switch (fsp->flow_type) {
  5942. case TCP_V4_FLOW:
  5943. case UDP_V4_FLOW:
  5944. case SCTP_V4_FLOW:
  5945. case AH_V4_FLOW:
  5946. case ESP_V4_FLOW:
  5947. niu_get_ip4fs_from_tcam_key(tp, fsp);
  5948. break;
  5949. case TCP_V6_FLOW:
  5950. case UDP_V6_FLOW:
  5951. case SCTP_V6_FLOW:
  5952. case AH_V6_FLOW:
  5953. case ESP_V6_FLOW:
  5954. /* Not yet implemented */
  5955. ret = -EINVAL;
  5956. break;
  5957. case IP_USER_FLOW:
  5958. niu_get_ip4fs_from_tcam_key(tp, fsp);
  5959. break;
  5960. default:
  5961. ret = -EINVAL;
  5962. break;
  5963. }
  5964. if (ret < 0)
  5965. goto out;
  5966. if (tp->assoc_data & TCAM_ASSOCDATA_DISC)
  5967. fsp->ring_cookie = RX_CLS_FLOW_DISC;
  5968. else
  5969. fsp->ring_cookie = (tp->assoc_data & TCAM_ASSOCDATA_OFFSET) >>
  5970. TCAM_ASSOCDATA_OFFSET_SHIFT;
  5971. /* put the tcam size here */
  5972. nfc->data = tcam_get_size(np);
  5973. out:
  5974. return ret;
  5975. }
  5976. static int niu_get_ethtool_tcam_all(struct niu *np,
  5977. struct ethtool_rxnfc *nfc,
  5978. u32 *rule_locs)
  5979. {
  5980. struct niu_parent *parent = np->parent;
  5981. struct niu_tcam_entry *tp;
  5982. int i, idx, cnt;
  5983. unsigned long flags;
  5984. int ret = 0;
  5985. /* put the tcam size here */
  5986. nfc->data = tcam_get_size(np);
  5987. niu_lock_parent(np, flags);
  5988. for (cnt = 0, i = 0; i < nfc->data; i++) {
  5989. idx = tcam_get_index(np, i);
  5990. tp = &parent->tcam[idx];
  5991. if (!tp->valid)
  5992. continue;
  5993. if (cnt == nfc->rule_cnt) {
  5994. ret = -EMSGSIZE;
  5995. break;
  5996. }
  5997. rule_locs[cnt] = i;
  5998. cnt++;
  5999. }
  6000. niu_unlock_parent(np, flags);
  6001. return ret;
  6002. }
  6003. static int niu_get_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd,
  6004. void *rule_locs)
  6005. {
  6006. struct niu *np = netdev_priv(dev);
  6007. int ret = 0;
  6008. switch (cmd->cmd) {
  6009. case ETHTOOL_GRXFH:
  6010. ret = niu_get_hash_opts(np, cmd);
  6011. break;
  6012. case ETHTOOL_GRXRINGS:
  6013. cmd->data = np->num_rx_rings;
  6014. break;
  6015. case ETHTOOL_GRXCLSRLCNT:
  6016. cmd->rule_cnt = tcam_get_valid_entry_cnt(np);
  6017. break;
  6018. case ETHTOOL_GRXCLSRULE:
  6019. ret = niu_get_ethtool_tcam_entry(np, cmd);
  6020. break;
  6021. case ETHTOOL_GRXCLSRLALL:
  6022. ret = niu_get_ethtool_tcam_all(np, cmd, (u32 *)rule_locs);
  6023. break;
  6024. default:
  6025. ret = -EINVAL;
  6026. break;
  6027. }
  6028. return ret;
  6029. }
  6030. static int niu_set_hash_opts(struct niu *np, struct ethtool_rxnfc *nfc)
  6031. {
  6032. u64 class;
  6033. u64 flow_key = 0;
  6034. unsigned long flags;
  6035. if (!niu_ethflow_to_class(nfc->flow_type, &class))
  6036. return -EINVAL;
  6037. if (class < CLASS_CODE_USER_PROG1 ||
  6038. class > CLASS_CODE_SCTP_IPV6)
  6039. return -EINVAL;
  6040. if (nfc->data & RXH_DISCARD) {
  6041. niu_lock_parent(np, flags);
  6042. flow_key = np->parent->tcam_key[class -
  6043. CLASS_CODE_USER_PROG1];
  6044. flow_key |= TCAM_KEY_DISC;
  6045. nw64(TCAM_KEY(class - CLASS_CODE_USER_PROG1), flow_key);
  6046. np->parent->tcam_key[class - CLASS_CODE_USER_PROG1] = flow_key;
  6047. niu_unlock_parent(np, flags);
  6048. return 0;
  6049. } else {
  6050. /* Discard was set before, but is not set now */
  6051. if (np->parent->tcam_key[class - CLASS_CODE_USER_PROG1] &
  6052. TCAM_KEY_DISC) {
  6053. niu_lock_parent(np, flags);
  6054. flow_key = np->parent->tcam_key[class -
  6055. CLASS_CODE_USER_PROG1];
  6056. flow_key &= ~TCAM_KEY_DISC;
  6057. nw64(TCAM_KEY(class - CLASS_CODE_USER_PROG1),
  6058. flow_key);
  6059. np->parent->tcam_key[class - CLASS_CODE_USER_PROG1] =
  6060. flow_key;
  6061. niu_unlock_parent(np, flags);
  6062. }
  6063. }
  6064. if (!niu_ethflow_to_flowkey(nfc->data, &flow_key))
  6065. return -EINVAL;
  6066. niu_lock_parent(np, flags);
  6067. nw64(FLOW_KEY(class - CLASS_CODE_USER_PROG1), flow_key);
  6068. np->parent->flow_key[class - CLASS_CODE_USER_PROG1] = flow_key;
  6069. niu_unlock_parent(np, flags);
  6070. return 0;
  6071. }
  6072. static void niu_get_tcamkey_from_ip4fs(struct ethtool_rx_flow_spec *fsp,
  6073. struct niu_tcam_entry *tp,
  6074. int l2_rdc_tab, u64 class)
  6075. {
  6076. u8 pid = 0;
  6077. u32 sip, dip, sipm, dipm, spi, spim;
  6078. u16 sport, dport, spm, dpm;
  6079. sip = be32_to_cpu(fsp->h_u.tcp_ip4_spec.ip4src);
  6080. sipm = be32_to_cpu(fsp->m_u.tcp_ip4_spec.ip4src);
  6081. dip = be32_to_cpu(fsp->h_u.tcp_ip4_spec.ip4dst);
  6082. dipm = be32_to_cpu(fsp->m_u.tcp_ip4_spec.ip4dst);
  6083. tp->key[0] = class << TCAM_V4KEY0_CLASS_CODE_SHIFT;
  6084. tp->key_mask[0] = TCAM_V4KEY0_CLASS_CODE;
  6085. tp->key[1] = (u64)l2_rdc_tab << TCAM_V4KEY1_L2RDCNUM_SHIFT;
  6086. tp->key_mask[1] = TCAM_V4KEY1_L2RDCNUM;
  6087. tp->key[3] = (u64)sip << TCAM_V4KEY3_SADDR_SHIFT;
  6088. tp->key[3] |= dip;
  6089. tp->key_mask[3] = (u64)sipm << TCAM_V4KEY3_SADDR_SHIFT;
  6090. tp->key_mask[3] |= dipm;
  6091. tp->key[2] |= ((u64)fsp->h_u.tcp_ip4_spec.tos <<
  6092. TCAM_V4KEY2_TOS_SHIFT);
  6093. tp->key_mask[2] |= ((u64)fsp->m_u.tcp_ip4_spec.tos <<
  6094. TCAM_V4KEY2_TOS_SHIFT);
  6095. switch (fsp->flow_type) {
  6096. case TCP_V4_FLOW:
  6097. case UDP_V4_FLOW:
  6098. case SCTP_V4_FLOW:
  6099. sport = be16_to_cpu(fsp->h_u.tcp_ip4_spec.psrc);
  6100. spm = be16_to_cpu(fsp->m_u.tcp_ip4_spec.psrc);
  6101. dport = be16_to_cpu(fsp->h_u.tcp_ip4_spec.pdst);
  6102. dpm = be16_to_cpu(fsp->m_u.tcp_ip4_spec.pdst);
  6103. tp->key[2] |= (((u64)sport << 16) | dport);
  6104. tp->key_mask[2] |= (((u64)spm << 16) | dpm);
  6105. niu_ethflow_to_l3proto(fsp->flow_type, &pid);
  6106. break;
  6107. case AH_V4_FLOW:
  6108. case ESP_V4_FLOW:
  6109. spi = be32_to_cpu(fsp->h_u.ah_ip4_spec.spi);
  6110. spim = be32_to_cpu(fsp->m_u.ah_ip4_spec.spi);
  6111. tp->key[2] |= spi;
  6112. tp->key_mask[2] |= spim;
  6113. niu_ethflow_to_l3proto(fsp->flow_type, &pid);
  6114. break;
  6115. case IP_USER_FLOW:
  6116. spi = be32_to_cpu(fsp->h_u.usr_ip4_spec.l4_4_bytes);
  6117. spim = be32_to_cpu(fsp->m_u.usr_ip4_spec.l4_4_bytes);
  6118. tp->key[2] |= spi;
  6119. tp->key_mask[2] |= spim;
  6120. pid = fsp->h_u.usr_ip4_spec.proto;
  6121. break;
  6122. default:
  6123. break;
  6124. }
  6125. tp->key[2] |= ((u64)pid << TCAM_V4KEY2_PROTO_SHIFT);
  6126. if (pid) {
  6127. tp->key_mask[2] |= TCAM_V4KEY2_PROTO;
  6128. }
  6129. }
  6130. static int niu_add_ethtool_tcam_entry(struct niu *np,
  6131. struct ethtool_rxnfc *nfc)
  6132. {
  6133. struct niu_parent *parent = np->parent;
  6134. struct niu_tcam_entry *tp;
  6135. struct ethtool_rx_flow_spec *fsp = &nfc->fs;
  6136. struct niu_rdc_tables *rdc_table = &parent->rdc_group_cfg[np->port];
  6137. int l2_rdc_table = rdc_table->first_table_num;
  6138. u16 idx;
  6139. u64 class;
  6140. unsigned long flags;
  6141. int err, ret;
  6142. ret = 0;
  6143. idx = nfc->fs.location;
  6144. if (idx >= tcam_get_size(np))
  6145. return -EINVAL;
  6146. if (fsp->flow_type == IP_USER_FLOW) {
  6147. int i;
  6148. int add_usr_cls = 0;
  6149. struct ethtool_usrip4_spec *uspec = &fsp->h_u.usr_ip4_spec;
  6150. struct ethtool_usrip4_spec *umask = &fsp->m_u.usr_ip4_spec;
  6151. if (uspec->ip_ver != ETH_RX_NFC_IP4)
  6152. return -EINVAL;
  6153. niu_lock_parent(np, flags);
  6154. for (i = 0; i < NIU_L3_PROG_CLS; i++) {
  6155. if (parent->l3_cls[i]) {
  6156. if (uspec->proto == parent->l3_cls_pid[i]) {
  6157. class = parent->l3_cls[i];
  6158. parent->l3_cls_refcnt[i]++;
  6159. add_usr_cls = 1;
  6160. break;
  6161. }
  6162. } else {
  6163. /* Program new user IP class */
  6164. switch (i) {
  6165. case 0:
  6166. class = CLASS_CODE_USER_PROG1;
  6167. break;
  6168. case 1:
  6169. class = CLASS_CODE_USER_PROG2;
  6170. break;
  6171. case 2:
  6172. class = CLASS_CODE_USER_PROG3;
  6173. break;
  6174. case 3:
  6175. class = CLASS_CODE_USER_PROG4;
  6176. break;
  6177. default:
  6178. break;
  6179. }
  6180. ret = tcam_user_ip_class_set(np, class, 0,
  6181. uspec->proto,
  6182. uspec->tos,
  6183. umask->tos);
  6184. if (ret)
  6185. goto out;
  6186. ret = tcam_user_ip_class_enable(np, class, 1);
  6187. if (ret)
  6188. goto out;
  6189. parent->l3_cls[i] = class;
  6190. parent->l3_cls_pid[i] = uspec->proto;
  6191. parent->l3_cls_refcnt[i]++;
  6192. add_usr_cls = 1;
  6193. break;
  6194. }
  6195. }
  6196. if (!add_usr_cls) {
  6197. netdev_info(np->dev, "niu%d: %s(): Could not find/insert class for pid %d\n",
  6198. parent->index, __func__, uspec->proto);
  6199. ret = -EINVAL;
  6200. goto out;
  6201. }
  6202. niu_unlock_parent(np, flags);
  6203. } else {
  6204. if (!niu_ethflow_to_class(fsp->flow_type, &class)) {
  6205. return -EINVAL;
  6206. }
  6207. }
  6208. niu_lock_parent(np, flags);
  6209. idx = tcam_get_index(np, idx);
  6210. tp = &parent->tcam[idx];
  6211. memset(tp, 0, sizeof(*tp));
  6212. /* fill in the tcam key and mask */
  6213. switch (fsp->flow_type) {
  6214. case TCP_V4_FLOW:
  6215. case UDP_V4_FLOW:
  6216. case SCTP_V4_FLOW:
  6217. case AH_V4_FLOW:
  6218. case ESP_V4_FLOW:
  6219. niu_get_tcamkey_from_ip4fs(fsp, tp, l2_rdc_table, class);
  6220. break;
  6221. case TCP_V6_FLOW:
  6222. case UDP_V6_FLOW:
  6223. case SCTP_V6_FLOW:
  6224. case AH_V6_FLOW:
  6225. case ESP_V6_FLOW:
  6226. /* Not yet implemented */
  6227. netdev_info(np->dev, "niu%d: In %s(): flow %d for IPv6 not implemented\n",
  6228. parent->index, __func__, fsp->flow_type);
  6229. ret = -EINVAL;
  6230. goto out;
  6231. case IP_USER_FLOW:
  6232. niu_get_tcamkey_from_ip4fs(fsp, tp, l2_rdc_table, class);
  6233. break;
  6234. default:
  6235. netdev_info(np->dev, "niu%d: In %s(): Unknown flow type %d\n",
  6236. parent->index, __func__, fsp->flow_type);
  6237. ret = -EINVAL;
  6238. goto out;
  6239. }
  6240. /* fill in the assoc data */
  6241. if (fsp->ring_cookie == RX_CLS_FLOW_DISC) {
  6242. tp->assoc_data = TCAM_ASSOCDATA_DISC;
  6243. } else {
  6244. if (fsp->ring_cookie >= np->num_rx_rings) {
  6245. netdev_info(np->dev, "niu%d: In %s(): Invalid RX ring %lld\n",
  6246. parent->index, __func__,
  6247. (long long)fsp->ring_cookie);
  6248. ret = -EINVAL;
  6249. goto out;
  6250. }
  6251. tp->assoc_data = (TCAM_ASSOCDATA_TRES_USE_OFFSET |
  6252. (fsp->ring_cookie <<
  6253. TCAM_ASSOCDATA_OFFSET_SHIFT));
  6254. }
  6255. err = tcam_write(np, idx, tp->key, tp->key_mask);
  6256. if (err) {
  6257. ret = -EINVAL;
  6258. goto out;
  6259. }
  6260. err = tcam_assoc_write(np, idx, tp->assoc_data);
  6261. if (err) {
  6262. ret = -EINVAL;
  6263. goto out;
  6264. }
  6265. /* validate the entry */
  6266. tp->valid = 1;
  6267. np->clas.tcam_valid_entries++;
  6268. out:
  6269. niu_unlock_parent(np, flags);
  6270. return ret;
  6271. }
  6272. static int niu_del_ethtool_tcam_entry(struct niu *np, u32 loc)
  6273. {
  6274. struct niu_parent *parent = np->parent;
  6275. struct niu_tcam_entry *tp;
  6276. u16 idx;
  6277. unsigned long flags;
  6278. u64 class;
  6279. int ret = 0;
  6280. if (loc >= tcam_get_size(np))
  6281. return -EINVAL;
  6282. niu_lock_parent(np, flags);
  6283. idx = tcam_get_index(np, loc);
  6284. tp = &parent->tcam[idx];
  6285. /* if the entry is of a user defined class, then update*/
  6286. class = (tp->key[0] & TCAM_V4KEY0_CLASS_CODE) >>
  6287. TCAM_V4KEY0_CLASS_CODE_SHIFT;
  6288. if (class >= CLASS_CODE_USER_PROG1 && class <= CLASS_CODE_USER_PROG4) {
  6289. int i;
  6290. for (i = 0; i < NIU_L3_PROG_CLS; i++) {
  6291. if (parent->l3_cls[i] == class) {
  6292. parent->l3_cls_refcnt[i]--;
  6293. if (!parent->l3_cls_refcnt[i]) {
  6294. /* disable class */
  6295. ret = tcam_user_ip_class_enable(np,
  6296. class,
  6297. 0);
  6298. if (ret)
  6299. goto out;
  6300. parent->l3_cls[i] = 0;
  6301. parent->l3_cls_pid[i] = 0;
  6302. }
  6303. break;
  6304. }
  6305. }
  6306. if (i == NIU_L3_PROG_CLS) {
  6307. netdev_info(np->dev, "niu%d: In %s(): Usr class 0x%llx not found\n",
  6308. parent->index, __func__,
  6309. (unsigned long long)class);
  6310. ret = -EINVAL;
  6311. goto out;
  6312. }
  6313. }
  6314. ret = tcam_flush(np, idx);
  6315. if (ret)
  6316. goto out;
  6317. /* invalidate the entry */
  6318. tp->valid = 0;
  6319. np->clas.tcam_valid_entries--;
  6320. out:
  6321. niu_unlock_parent(np, flags);
  6322. return ret;
  6323. }
  6324. static int niu_set_nfc(struct net_device *dev, struct ethtool_rxnfc *cmd)
  6325. {
  6326. struct niu *np = netdev_priv(dev);
  6327. int ret = 0;
  6328. switch (cmd->cmd) {
  6329. case ETHTOOL_SRXFH:
  6330. ret = niu_set_hash_opts(np, cmd);
  6331. break;
  6332. case ETHTOOL_SRXCLSRLINS:
  6333. ret = niu_add_ethtool_tcam_entry(np, cmd);
  6334. break;
  6335. case ETHTOOL_SRXCLSRLDEL:
  6336. ret = niu_del_ethtool_tcam_entry(np, cmd->fs.location);
  6337. break;
  6338. default:
  6339. ret = -EINVAL;
  6340. break;
  6341. }
  6342. return ret;
  6343. }
  6344. static const struct {
  6345. const char string[ETH_GSTRING_LEN];
  6346. } niu_xmac_stat_keys[] = {
  6347. { "tx_frames" },
  6348. { "tx_bytes" },
  6349. { "tx_fifo_errors" },
  6350. { "tx_overflow_errors" },
  6351. { "tx_max_pkt_size_errors" },
  6352. { "tx_underflow_errors" },
  6353. { "rx_local_faults" },
  6354. { "rx_remote_faults" },
  6355. { "rx_link_faults" },
  6356. { "rx_align_errors" },
  6357. { "rx_frags" },
  6358. { "rx_mcasts" },
  6359. { "rx_bcasts" },
  6360. { "rx_hist_cnt1" },
  6361. { "rx_hist_cnt2" },
  6362. { "rx_hist_cnt3" },
  6363. { "rx_hist_cnt4" },
  6364. { "rx_hist_cnt5" },
  6365. { "rx_hist_cnt6" },
  6366. { "rx_hist_cnt7" },
  6367. { "rx_octets" },
  6368. { "rx_code_violations" },
  6369. { "rx_len_errors" },
  6370. { "rx_crc_errors" },
  6371. { "rx_underflows" },
  6372. { "rx_overflows" },
  6373. { "pause_off_state" },
  6374. { "pause_on_state" },
  6375. { "pause_received" },
  6376. };
  6377. #define NUM_XMAC_STAT_KEYS ARRAY_SIZE(niu_xmac_stat_keys)
  6378. static const struct {
  6379. const char string[ETH_GSTRING_LEN];
  6380. } niu_bmac_stat_keys[] = {
  6381. { "tx_underflow_errors" },
  6382. { "tx_max_pkt_size_errors" },
  6383. { "tx_bytes" },
  6384. { "tx_frames" },
  6385. { "rx_overflows" },
  6386. { "rx_frames" },
  6387. { "rx_align_errors" },
  6388. { "rx_crc_errors" },
  6389. { "rx_len_errors" },
  6390. { "pause_off_state" },
  6391. { "pause_on_state" },
  6392. { "pause_received" },
  6393. };
  6394. #define NUM_BMAC_STAT_KEYS ARRAY_SIZE(niu_bmac_stat_keys)
  6395. static const struct {
  6396. const char string[ETH_GSTRING_LEN];
  6397. } niu_rxchan_stat_keys[] = {
  6398. { "rx_channel" },
  6399. { "rx_packets" },
  6400. { "rx_bytes" },
  6401. { "rx_dropped" },
  6402. { "rx_errors" },
  6403. };
  6404. #define NUM_RXCHAN_STAT_KEYS ARRAY_SIZE(niu_rxchan_stat_keys)
  6405. static const struct {
  6406. const char string[ETH_GSTRING_LEN];
  6407. } niu_txchan_stat_keys[] = {
  6408. { "tx_channel" },
  6409. { "tx_packets" },
  6410. { "tx_bytes" },
  6411. { "tx_errors" },
  6412. };
  6413. #define NUM_TXCHAN_STAT_KEYS ARRAY_SIZE(niu_txchan_stat_keys)
  6414. static void niu_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  6415. {
  6416. struct niu *np = netdev_priv(dev);
  6417. int i;
  6418. if (stringset != ETH_SS_STATS)
  6419. return;
  6420. if (np->flags & NIU_FLAGS_XMAC) {
  6421. memcpy(data, niu_xmac_stat_keys,
  6422. sizeof(niu_xmac_stat_keys));
  6423. data += sizeof(niu_xmac_stat_keys);
  6424. } else {
  6425. memcpy(data, niu_bmac_stat_keys,
  6426. sizeof(niu_bmac_stat_keys));
  6427. data += sizeof(niu_bmac_stat_keys);
  6428. }
  6429. for (i = 0; i < np->num_rx_rings; i++) {
  6430. memcpy(data, niu_rxchan_stat_keys,
  6431. sizeof(niu_rxchan_stat_keys));
  6432. data += sizeof(niu_rxchan_stat_keys);
  6433. }
  6434. for (i = 0; i < np->num_tx_rings; i++) {
  6435. memcpy(data, niu_txchan_stat_keys,
  6436. sizeof(niu_txchan_stat_keys));
  6437. data += sizeof(niu_txchan_stat_keys);
  6438. }
  6439. }
  6440. static int niu_get_sset_count(struct net_device *dev, int stringset)
  6441. {
  6442. struct niu *np = netdev_priv(dev);
  6443. if (stringset != ETH_SS_STATS)
  6444. return -EINVAL;
  6445. return (np->flags & NIU_FLAGS_XMAC ?
  6446. NUM_XMAC_STAT_KEYS :
  6447. NUM_BMAC_STAT_KEYS) +
  6448. (np->num_rx_rings * NUM_RXCHAN_STAT_KEYS) +
  6449. (np->num_tx_rings * NUM_TXCHAN_STAT_KEYS);
  6450. }
  6451. static void niu_get_ethtool_stats(struct net_device *dev,
  6452. struct ethtool_stats *stats, u64 *data)
  6453. {
  6454. struct niu *np = netdev_priv(dev);
  6455. int i;
  6456. niu_sync_mac_stats(np);
  6457. if (np->flags & NIU_FLAGS_XMAC) {
  6458. memcpy(data, &np->mac_stats.xmac,
  6459. sizeof(struct niu_xmac_stats));
  6460. data += (sizeof(struct niu_xmac_stats) / sizeof(u64));
  6461. } else {
  6462. memcpy(data, &np->mac_stats.bmac,
  6463. sizeof(struct niu_bmac_stats));
  6464. data += (sizeof(struct niu_bmac_stats) / sizeof(u64));
  6465. }
  6466. for (i = 0; i < np->num_rx_rings; i++) {
  6467. struct rx_ring_info *rp = &np->rx_rings[i];
  6468. niu_sync_rx_discard_stats(np, rp, 0);
  6469. data[0] = rp->rx_channel;
  6470. data[1] = rp->rx_packets;
  6471. data[2] = rp->rx_bytes;
  6472. data[3] = rp->rx_dropped;
  6473. data[4] = rp->rx_errors;
  6474. data += 5;
  6475. }
  6476. for (i = 0; i < np->num_tx_rings; i++) {
  6477. struct tx_ring_info *rp = &np->tx_rings[i];
  6478. data[0] = rp->tx_channel;
  6479. data[1] = rp->tx_packets;
  6480. data[2] = rp->tx_bytes;
  6481. data[3] = rp->tx_errors;
  6482. data += 4;
  6483. }
  6484. }
  6485. static u64 niu_led_state_save(struct niu *np)
  6486. {
  6487. if (np->flags & NIU_FLAGS_XMAC)
  6488. return nr64_mac(XMAC_CONFIG);
  6489. else
  6490. return nr64_mac(BMAC_XIF_CONFIG);
  6491. }
  6492. static void niu_led_state_restore(struct niu *np, u64 val)
  6493. {
  6494. if (np->flags & NIU_FLAGS_XMAC)
  6495. nw64_mac(XMAC_CONFIG, val);
  6496. else
  6497. nw64_mac(BMAC_XIF_CONFIG, val);
  6498. }
  6499. static void niu_force_led(struct niu *np, int on)
  6500. {
  6501. u64 val, reg, bit;
  6502. if (np->flags & NIU_FLAGS_XMAC) {
  6503. reg = XMAC_CONFIG;
  6504. bit = XMAC_CONFIG_FORCE_LED_ON;
  6505. } else {
  6506. reg = BMAC_XIF_CONFIG;
  6507. bit = BMAC_XIF_CONFIG_LINK_LED;
  6508. }
  6509. val = nr64_mac(reg);
  6510. if (on)
  6511. val |= bit;
  6512. else
  6513. val &= ~bit;
  6514. nw64_mac(reg, val);
  6515. }
  6516. static int niu_set_phys_id(struct net_device *dev,
  6517. enum ethtool_phys_id_state state)
  6518. {
  6519. struct niu *np = netdev_priv(dev);
  6520. if (!netif_running(dev))
  6521. return -EAGAIN;
  6522. switch (state) {
  6523. case ETHTOOL_ID_ACTIVE:
  6524. np->orig_led_state = niu_led_state_save(np);
  6525. return 1; /* cycle on/off once per second */
  6526. case ETHTOOL_ID_ON:
  6527. niu_force_led(np, 1);
  6528. break;
  6529. case ETHTOOL_ID_OFF:
  6530. niu_force_led(np, 0);
  6531. break;
  6532. case ETHTOOL_ID_INACTIVE:
  6533. niu_led_state_restore(np, np->orig_led_state);
  6534. }
  6535. return 0;
  6536. }
  6537. static const struct ethtool_ops niu_ethtool_ops = {
  6538. .get_drvinfo = niu_get_drvinfo,
  6539. .get_link = ethtool_op_get_link,
  6540. .get_msglevel = niu_get_msglevel,
  6541. .set_msglevel = niu_set_msglevel,
  6542. .nway_reset = niu_nway_reset,
  6543. .get_eeprom_len = niu_get_eeprom_len,
  6544. .get_eeprom = niu_get_eeprom,
  6545. .get_settings = niu_get_settings,
  6546. .set_settings = niu_set_settings,
  6547. .get_strings = niu_get_strings,
  6548. .get_sset_count = niu_get_sset_count,
  6549. .get_ethtool_stats = niu_get_ethtool_stats,
  6550. .set_phys_id = niu_set_phys_id,
  6551. .get_rxnfc = niu_get_nfc,
  6552. .set_rxnfc = niu_set_nfc,
  6553. };
  6554. static int niu_ldg_assign_ldn(struct niu *np, struct niu_parent *parent,
  6555. int ldg, int ldn)
  6556. {
  6557. if (ldg < NIU_LDG_MIN || ldg > NIU_LDG_MAX)
  6558. return -EINVAL;
  6559. if (ldn < 0 || ldn > LDN_MAX)
  6560. return -EINVAL;
  6561. parent->ldg_map[ldn] = ldg;
  6562. if (np->parent->plat_type == PLAT_TYPE_NIU) {
  6563. /* On N2 NIU, the ldn-->ldg assignments are setup and fixed by
  6564. * the firmware, and we're not supposed to change them.
  6565. * Validate the mapping, because if it's wrong we probably
  6566. * won't get any interrupts and that's painful to debug.
  6567. */
  6568. if (nr64(LDG_NUM(ldn)) != ldg) {
  6569. dev_err(np->device, "Port %u, mis-matched LDG assignment for ldn %d, should be %d is %llu\n",
  6570. np->port, ldn, ldg,
  6571. (unsigned long long) nr64(LDG_NUM(ldn)));
  6572. return -EINVAL;
  6573. }
  6574. } else
  6575. nw64(LDG_NUM(ldn), ldg);
  6576. return 0;
  6577. }
  6578. static int niu_set_ldg_timer_res(struct niu *np, int res)
  6579. {
  6580. if (res < 0 || res > LDG_TIMER_RES_VAL)
  6581. return -EINVAL;
  6582. nw64(LDG_TIMER_RES, res);
  6583. return 0;
  6584. }
  6585. static int niu_set_ldg_sid(struct niu *np, int ldg, int func, int vector)
  6586. {
  6587. if ((ldg < NIU_LDG_MIN || ldg > NIU_LDG_MAX) ||
  6588. (func < 0 || func > 3) ||
  6589. (vector < 0 || vector > 0x1f))
  6590. return -EINVAL;
  6591. nw64(SID(ldg), (func << SID_FUNC_SHIFT) | vector);
  6592. return 0;
  6593. }
  6594. static int __devinit niu_pci_eeprom_read(struct niu *np, u32 addr)
  6595. {
  6596. u64 frame, frame_base = (ESPC_PIO_STAT_READ_START |
  6597. (addr << ESPC_PIO_STAT_ADDR_SHIFT));
  6598. int limit;
  6599. if (addr > (ESPC_PIO_STAT_ADDR >> ESPC_PIO_STAT_ADDR_SHIFT))
  6600. return -EINVAL;
  6601. frame = frame_base;
  6602. nw64(ESPC_PIO_STAT, frame);
  6603. limit = 64;
  6604. do {
  6605. udelay(5);
  6606. frame = nr64(ESPC_PIO_STAT);
  6607. if (frame & ESPC_PIO_STAT_READ_END)
  6608. break;
  6609. } while (limit--);
  6610. if (!(frame & ESPC_PIO_STAT_READ_END)) {
  6611. dev_err(np->device, "EEPROM read timeout frame[%llx]\n",
  6612. (unsigned long long) frame);
  6613. return -ENODEV;
  6614. }
  6615. frame = frame_base;
  6616. nw64(ESPC_PIO_STAT, frame);
  6617. limit = 64;
  6618. do {
  6619. udelay(5);
  6620. frame = nr64(ESPC_PIO_STAT);
  6621. if (frame & ESPC_PIO_STAT_READ_END)
  6622. break;
  6623. } while (limit--);
  6624. if (!(frame & ESPC_PIO_STAT_READ_END)) {
  6625. dev_err(np->device, "EEPROM read timeout frame[%llx]\n",
  6626. (unsigned long long) frame);
  6627. return -ENODEV;
  6628. }
  6629. frame = nr64(ESPC_PIO_STAT);
  6630. return (frame & ESPC_PIO_STAT_DATA) >> ESPC_PIO_STAT_DATA_SHIFT;
  6631. }
  6632. static int __devinit niu_pci_eeprom_read16(struct niu *np, u32 off)
  6633. {
  6634. int err = niu_pci_eeprom_read(np, off);
  6635. u16 val;
  6636. if (err < 0)
  6637. return err;
  6638. val = (err << 8);
  6639. err = niu_pci_eeprom_read(np, off + 1);
  6640. if (err < 0)
  6641. return err;
  6642. val |= (err & 0xff);
  6643. return val;
  6644. }
  6645. static int __devinit niu_pci_eeprom_read16_swp(struct niu *np, u32 off)
  6646. {
  6647. int err = niu_pci_eeprom_read(np, off);
  6648. u16 val;
  6649. if (err < 0)
  6650. return err;
  6651. val = (err & 0xff);
  6652. err = niu_pci_eeprom_read(np, off + 1);
  6653. if (err < 0)
  6654. return err;
  6655. val |= (err & 0xff) << 8;
  6656. return val;
  6657. }
  6658. static int __devinit niu_pci_vpd_get_propname(struct niu *np,
  6659. u32 off,
  6660. char *namebuf,
  6661. int namebuf_len)
  6662. {
  6663. int i;
  6664. for (i = 0; i < namebuf_len; i++) {
  6665. int err = niu_pci_eeprom_read(np, off + i);
  6666. if (err < 0)
  6667. return err;
  6668. *namebuf++ = err;
  6669. if (!err)
  6670. break;
  6671. }
  6672. if (i >= namebuf_len)
  6673. return -EINVAL;
  6674. return i + 1;
  6675. }
  6676. static void __devinit niu_vpd_parse_version(struct niu *np)
  6677. {
  6678. struct niu_vpd *vpd = &np->vpd;
  6679. int len = strlen(vpd->version) + 1;
  6680. const char *s = vpd->version;
  6681. int i;
  6682. for (i = 0; i < len - 5; i++) {
  6683. if (!strncmp(s + i, "FCode ", 6))
  6684. break;
  6685. }
  6686. if (i >= len - 5)
  6687. return;
  6688. s += i + 5;
  6689. sscanf(s, "%d.%d", &vpd->fcode_major, &vpd->fcode_minor);
  6690. netif_printk(np, probe, KERN_DEBUG, np->dev,
  6691. "VPD_SCAN: FCODE major(%d) minor(%d)\n",
  6692. vpd->fcode_major, vpd->fcode_minor);
  6693. if (vpd->fcode_major > NIU_VPD_MIN_MAJOR ||
  6694. (vpd->fcode_major == NIU_VPD_MIN_MAJOR &&
  6695. vpd->fcode_minor >= NIU_VPD_MIN_MINOR))
  6696. np->flags |= NIU_FLAGS_VPD_VALID;
  6697. }
  6698. /* ESPC_PIO_EN_ENABLE must be set */
  6699. static int __devinit niu_pci_vpd_scan_props(struct niu *np,
  6700. u32 start, u32 end)
  6701. {
  6702. unsigned int found_mask = 0;
  6703. #define FOUND_MASK_MODEL 0x00000001
  6704. #define FOUND_MASK_BMODEL 0x00000002
  6705. #define FOUND_MASK_VERS 0x00000004
  6706. #define FOUND_MASK_MAC 0x00000008
  6707. #define FOUND_MASK_NMAC 0x00000010
  6708. #define FOUND_MASK_PHY 0x00000020
  6709. #define FOUND_MASK_ALL 0x0000003f
  6710. netif_printk(np, probe, KERN_DEBUG, np->dev,
  6711. "VPD_SCAN: start[%x] end[%x]\n", start, end);
  6712. while (start < end) {
  6713. int len, err, prop_len;
  6714. char namebuf[64];
  6715. u8 *prop_buf;
  6716. int max_len;
  6717. if (found_mask == FOUND_MASK_ALL) {
  6718. niu_vpd_parse_version(np);
  6719. return 1;
  6720. }
  6721. err = niu_pci_eeprom_read(np, start + 2);
  6722. if (err < 0)
  6723. return err;
  6724. len = err;
  6725. start += 3;
  6726. prop_len = niu_pci_eeprom_read(np, start + 4);
  6727. err = niu_pci_vpd_get_propname(np, start + 5, namebuf, 64);
  6728. if (err < 0)
  6729. return err;
  6730. prop_buf = NULL;
  6731. max_len = 0;
  6732. if (!strcmp(namebuf, "model")) {
  6733. prop_buf = np->vpd.model;
  6734. max_len = NIU_VPD_MODEL_MAX;
  6735. found_mask |= FOUND_MASK_MODEL;
  6736. } else if (!strcmp(namebuf, "board-model")) {
  6737. prop_buf = np->vpd.board_model;
  6738. max_len = NIU_VPD_BD_MODEL_MAX;
  6739. found_mask |= FOUND_MASK_BMODEL;
  6740. } else if (!strcmp(namebuf, "version")) {
  6741. prop_buf = np->vpd.version;
  6742. max_len = NIU_VPD_VERSION_MAX;
  6743. found_mask |= FOUND_MASK_VERS;
  6744. } else if (!strcmp(namebuf, "local-mac-address")) {
  6745. prop_buf = np->vpd.local_mac;
  6746. max_len = ETH_ALEN;
  6747. found_mask |= FOUND_MASK_MAC;
  6748. } else if (!strcmp(namebuf, "num-mac-addresses")) {
  6749. prop_buf = &np->vpd.mac_num;
  6750. max_len = 1;
  6751. found_mask |= FOUND_MASK_NMAC;
  6752. } else if (!strcmp(namebuf, "phy-type")) {
  6753. prop_buf = np->vpd.phy_type;
  6754. max_len = NIU_VPD_PHY_TYPE_MAX;
  6755. found_mask |= FOUND_MASK_PHY;
  6756. }
  6757. if (max_len && prop_len > max_len) {
  6758. dev_err(np->device, "Property '%s' length (%d) is too long\n", namebuf, prop_len);
  6759. return -EINVAL;
  6760. }
  6761. if (prop_buf) {
  6762. u32 off = start + 5 + err;
  6763. int i;
  6764. netif_printk(np, probe, KERN_DEBUG, np->dev,
  6765. "VPD_SCAN: Reading in property [%s] len[%d]\n",
  6766. namebuf, prop_len);
  6767. for (i = 0; i < prop_len; i++)
  6768. *prop_buf++ = niu_pci_eeprom_read(np, off + i);
  6769. }
  6770. start += len;
  6771. }
  6772. return 0;
  6773. }
  6774. /* ESPC_PIO_EN_ENABLE must be set */
  6775. static void __devinit niu_pci_vpd_fetch(struct niu *np, u32 start)
  6776. {
  6777. u32 offset;
  6778. int err;
  6779. err = niu_pci_eeprom_read16_swp(np, start + 1);
  6780. if (err < 0)
  6781. return;
  6782. offset = err + 3;
  6783. while (start + offset < ESPC_EEPROM_SIZE) {
  6784. u32 here = start + offset;
  6785. u32 end;
  6786. err = niu_pci_eeprom_read(np, here);
  6787. if (err != 0x90)
  6788. return;
  6789. err = niu_pci_eeprom_read16_swp(np, here + 1);
  6790. if (err < 0)
  6791. return;
  6792. here = start + offset + 3;
  6793. end = start + offset + err;
  6794. offset += err;
  6795. err = niu_pci_vpd_scan_props(np, here, end);
  6796. if (err < 0 || err == 1)
  6797. return;
  6798. }
  6799. }
  6800. /* ESPC_PIO_EN_ENABLE must be set */
  6801. static u32 __devinit niu_pci_vpd_offset(struct niu *np)
  6802. {
  6803. u32 start = 0, end = ESPC_EEPROM_SIZE, ret;
  6804. int err;
  6805. while (start < end) {
  6806. ret = start;
  6807. /* ROM header signature? */
  6808. err = niu_pci_eeprom_read16(np, start + 0);
  6809. if (err != 0x55aa)
  6810. return 0;
  6811. /* Apply offset to PCI data structure. */
  6812. err = niu_pci_eeprom_read16(np, start + 23);
  6813. if (err < 0)
  6814. return 0;
  6815. start += err;
  6816. /* Check for "PCIR" signature. */
  6817. err = niu_pci_eeprom_read16(np, start + 0);
  6818. if (err != 0x5043)
  6819. return 0;
  6820. err = niu_pci_eeprom_read16(np, start + 2);
  6821. if (err != 0x4952)
  6822. return 0;
  6823. /* Check for OBP image type. */
  6824. err = niu_pci_eeprom_read(np, start + 20);
  6825. if (err < 0)
  6826. return 0;
  6827. if (err != 0x01) {
  6828. err = niu_pci_eeprom_read(np, ret + 2);
  6829. if (err < 0)
  6830. return 0;
  6831. start = ret + (err * 512);
  6832. continue;
  6833. }
  6834. err = niu_pci_eeprom_read16_swp(np, start + 8);
  6835. if (err < 0)
  6836. return err;
  6837. ret += err;
  6838. err = niu_pci_eeprom_read(np, ret + 0);
  6839. if (err != 0x82)
  6840. return 0;
  6841. return ret;
  6842. }
  6843. return 0;
  6844. }
  6845. static int __devinit niu_phy_type_prop_decode(struct niu *np,
  6846. const char *phy_prop)
  6847. {
  6848. if (!strcmp(phy_prop, "mif")) {
  6849. /* 1G copper, MII */
  6850. np->flags &= ~(NIU_FLAGS_FIBER |
  6851. NIU_FLAGS_10G);
  6852. np->mac_xcvr = MAC_XCVR_MII;
  6853. } else if (!strcmp(phy_prop, "xgf")) {
  6854. /* 10G fiber, XPCS */
  6855. np->flags |= (NIU_FLAGS_10G |
  6856. NIU_FLAGS_FIBER);
  6857. np->mac_xcvr = MAC_XCVR_XPCS;
  6858. } else if (!strcmp(phy_prop, "pcs")) {
  6859. /* 1G fiber, PCS */
  6860. np->flags &= ~NIU_FLAGS_10G;
  6861. np->flags |= NIU_FLAGS_FIBER;
  6862. np->mac_xcvr = MAC_XCVR_PCS;
  6863. } else if (!strcmp(phy_prop, "xgc")) {
  6864. /* 10G copper, XPCS */
  6865. np->flags |= NIU_FLAGS_10G;
  6866. np->flags &= ~NIU_FLAGS_FIBER;
  6867. np->mac_xcvr = MAC_XCVR_XPCS;
  6868. } else if (!strcmp(phy_prop, "xgsd") || !strcmp(phy_prop, "gsd")) {
  6869. /* 10G Serdes or 1G Serdes, default to 10G */
  6870. np->flags |= NIU_FLAGS_10G;
  6871. np->flags &= ~NIU_FLAGS_FIBER;
  6872. np->flags |= NIU_FLAGS_XCVR_SERDES;
  6873. np->mac_xcvr = MAC_XCVR_XPCS;
  6874. } else {
  6875. return -EINVAL;
  6876. }
  6877. return 0;
  6878. }
  6879. static int niu_pci_vpd_get_nports(struct niu *np)
  6880. {
  6881. int ports = 0;
  6882. if ((!strcmp(np->vpd.model, NIU_QGC_LP_MDL_STR)) ||
  6883. (!strcmp(np->vpd.model, NIU_QGC_PEM_MDL_STR)) ||
  6884. (!strcmp(np->vpd.model, NIU_MARAMBA_MDL_STR)) ||
  6885. (!strcmp(np->vpd.model, NIU_KIMI_MDL_STR)) ||
  6886. (!strcmp(np->vpd.model, NIU_ALONSO_MDL_STR))) {
  6887. ports = 4;
  6888. } else if ((!strcmp(np->vpd.model, NIU_2XGF_LP_MDL_STR)) ||
  6889. (!strcmp(np->vpd.model, NIU_2XGF_PEM_MDL_STR)) ||
  6890. (!strcmp(np->vpd.model, NIU_FOXXY_MDL_STR)) ||
  6891. (!strcmp(np->vpd.model, NIU_2XGF_MRVL_MDL_STR))) {
  6892. ports = 2;
  6893. }
  6894. return ports;
  6895. }
  6896. static void __devinit niu_pci_vpd_validate(struct niu *np)
  6897. {
  6898. struct net_device *dev = np->dev;
  6899. struct niu_vpd *vpd = &np->vpd;
  6900. u8 val8;
  6901. if (!is_valid_ether_addr(&vpd->local_mac[0])) {
  6902. dev_err(np->device, "VPD MAC invalid, falling back to SPROM\n");
  6903. np->flags &= ~NIU_FLAGS_VPD_VALID;
  6904. return;
  6905. }
  6906. if (!strcmp(np->vpd.model, NIU_ALONSO_MDL_STR) ||
  6907. !strcmp(np->vpd.model, NIU_KIMI_MDL_STR)) {
  6908. np->flags |= NIU_FLAGS_10G;
  6909. np->flags &= ~NIU_FLAGS_FIBER;
  6910. np->flags |= NIU_FLAGS_XCVR_SERDES;
  6911. np->mac_xcvr = MAC_XCVR_PCS;
  6912. if (np->port > 1) {
  6913. np->flags |= NIU_FLAGS_FIBER;
  6914. np->flags &= ~NIU_FLAGS_10G;
  6915. }
  6916. if (np->flags & NIU_FLAGS_10G)
  6917. np->mac_xcvr = MAC_XCVR_XPCS;
  6918. } else if (!strcmp(np->vpd.model, NIU_FOXXY_MDL_STR)) {
  6919. np->flags |= (NIU_FLAGS_10G | NIU_FLAGS_FIBER |
  6920. NIU_FLAGS_HOTPLUG_PHY);
  6921. } else if (niu_phy_type_prop_decode(np, np->vpd.phy_type)) {
  6922. dev_err(np->device, "Illegal phy string [%s]\n",
  6923. np->vpd.phy_type);
  6924. dev_err(np->device, "Falling back to SPROM\n");
  6925. np->flags &= ~NIU_FLAGS_VPD_VALID;
  6926. return;
  6927. }
  6928. memcpy(dev->perm_addr, vpd->local_mac, ETH_ALEN);
  6929. val8 = dev->perm_addr[5];
  6930. dev->perm_addr[5] += np->port;
  6931. if (dev->perm_addr[5] < val8)
  6932. dev->perm_addr[4]++;
  6933. memcpy(dev->dev_addr, dev->perm_addr, dev->addr_len);
  6934. }
  6935. static int __devinit niu_pci_probe_sprom(struct niu *np)
  6936. {
  6937. struct net_device *dev = np->dev;
  6938. int len, i;
  6939. u64 val, sum;
  6940. u8 val8;
  6941. val = (nr64(ESPC_VER_IMGSZ) & ESPC_VER_IMGSZ_IMGSZ);
  6942. val >>= ESPC_VER_IMGSZ_IMGSZ_SHIFT;
  6943. len = val / 4;
  6944. np->eeprom_len = len;
  6945. netif_printk(np, probe, KERN_DEBUG, np->dev,
  6946. "SPROM: Image size %llu\n", (unsigned long long)val);
  6947. sum = 0;
  6948. for (i = 0; i < len; i++) {
  6949. val = nr64(ESPC_NCR(i));
  6950. sum += (val >> 0) & 0xff;
  6951. sum += (val >> 8) & 0xff;
  6952. sum += (val >> 16) & 0xff;
  6953. sum += (val >> 24) & 0xff;
  6954. }
  6955. netif_printk(np, probe, KERN_DEBUG, np->dev,
  6956. "SPROM: Checksum %x\n", (int)(sum & 0xff));
  6957. if ((sum & 0xff) != 0xab) {
  6958. dev_err(np->device, "Bad SPROM checksum (%x, should be 0xab)\n", (int)(sum & 0xff));
  6959. return -EINVAL;
  6960. }
  6961. val = nr64(ESPC_PHY_TYPE);
  6962. switch (np->port) {
  6963. case 0:
  6964. val8 = (val & ESPC_PHY_TYPE_PORT0) >>
  6965. ESPC_PHY_TYPE_PORT0_SHIFT;
  6966. break;
  6967. case 1:
  6968. val8 = (val & ESPC_PHY_TYPE_PORT1) >>
  6969. ESPC_PHY_TYPE_PORT1_SHIFT;
  6970. break;
  6971. case 2:
  6972. val8 = (val & ESPC_PHY_TYPE_PORT2) >>
  6973. ESPC_PHY_TYPE_PORT2_SHIFT;
  6974. break;
  6975. case 3:
  6976. val8 = (val & ESPC_PHY_TYPE_PORT3) >>
  6977. ESPC_PHY_TYPE_PORT3_SHIFT;
  6978. break;
  6979. default:
  6980. dev_err(np->device, "Bogus port number %u\n",
  6981. np->port);
  6982. return -EINVAL;
  6983. }
  6984. netif_printk(np, probe, KERN_DEBUG, np->dev,
  6985. "SPROM: PHY type %x\n", val8);
  6986. switch (val8) {
  6987. case ESPC_PHY_TYPE_1G_COPPER:
  6988. /* 1G copper, MII */
  6989. np->flags &= ~(NIU_FLAGS_FIBER |
  6990. NIU_FLAGS_10G);
  6991. np->mac_xcvr = MAC_XCVR_MII;
  6992. break;
  6993. case ESPC_PHY_TYPE_1G_FIBER:
  6994. /* 1G fiber, PCS */
  6995. np->flags &= ~NIU_FLAGS_10G;
  6996. np->flags |= NIU_FLAGS_FIBER;
  6997. np->mac_xcvr = MAC_XCVR_PCS;
  6998. break;
  6999. case ESPC_PHY_TYPE_10G_COPPER:
  7000. /* 10G copper, XPCS */
  7001. np->flags |= NIU_FLAGS_10G;
  7002. np->flags &= ~NIU_FLAGS_FIBER;
  7003. np->mac_xcvr = MAC_XCVR_XPCS;
  7004. break;
  7005. case ESPC_PHY_TYPE_10G_FIBER:
  7006. /* 10G fiber, XPCS */
  7007. np->flags |= (NIU_FLAGS_10G |
  7008. NIU_FLAGS_FIBER);
  7009. np->mac_xcvr = MAC_XCVR_XPCS;
  7010. break;
  7011. default:
  7012. dev_err(np->device, "Bogus SPROM phy type %u\n", val8);
  7013. return -EINVAL;
  7014. }
  7015. val = nr64(ESPC_MAC_ADDR0);
  7016. netif_printk(np, probe, KERN_DEBUG, np->dev,
  7017. "SPROM: MAC_ADDR0[%08llx]\n", (unsigned long long)val);
  7018. dev->perm_addr[0] = (val >> 0) & 0xff;
  7019. dev->perm_addr[1] = (val >> 8) & 0xff;
  7020. dev->perm_addr[2] = (val >> 16) & 0xff;
  7021. dev->perm_addr[3] = (val >> 24) & 0xff;
  7022. val = nr64(ESPC_MAC_ADDR1);
  7023. netif_printk(np, probe, KERN_DEBUG, np->dev,
  7024. "SPROM: MAC_ADDR1[%08llx]\n", (unsigned long long)val);
  7025. dev->perm_addr[4] = (val >> 0) & 0xff;
  7026. dev->perm_addr[5] = (val >> 8) & 0xff;
  7027. if (!is_valid_ether_addr(&dev->perm_addr[0])) {
  7028. dev_err(np->device, "SPROM MAC address invalid [ %pM ]\n",
  7029. dev->perm_addr);
  7030. return -EINVAL;
  7031. }
  7032. val8 = dev->perm_addr[5];
  7033. dev->perm_addr[5] += np->port;
  7034. if (dev->perm_addr[5] < val8)
  7035. dev->perm_addr[4]++;
  7036. memcpy(dev->dev_addr, dev->perm_addr, dev->addr_len);
  7037. val = nr64(ESPC_MOD_STR_LEN);
  7038. netif_printk(np, probe, KERN_DEBUG, np->dev,
  7039. "SPROM: MOD_STR_LEN[%llu]\n", (unsigned long long)val);
  7040. if (val >= 8 * 4)
  7041. return -EINVAL;
  7042. for (i = 0; i < val; i += 4) {
  7043. u64 tmp = nr64(ESPC_NCR(5 + (i / 4)));
  7044. np->vpd.model[i + 3] = (tmp >> 0) & 0xff;
  7045. np->vpd.model[i + 2] = (tmp >> 8) & 0xff;
  7046. np->vpd.model[i + 1] = (tmp >> 16) & 0xff;
  7047. np->vpd.model[i + 0] = (tmp >> 24) & 0xff;
  7048. }
  7049. np->vpd.model[val] = '\0';
  7050. val = nr64(ESPC_BD_MOD_STR_LEN);
  7051. netif_printk(np, probe, KERN_DEBUG, np->dev,
  7052. "SPROM: BD_MOD_STR_LEN[%llu]\n", (unsigned long long)val);
  7053. if (val >= 4 * 4)
  7054. return -EINVAL;
  7055. for (i = 0; i < val; i += 4) {
  7056. u64 tmp = nr64(ESPC_NCR(14 + (i / 4)));
  7057. np->vpd.board_model[i + 3] = (tmp >> 0) & 0xff;
  7058. np->vpd.board_model[i + 2] = (tmp >> 8) & 0xff;
  7059. np->vpd.board_model[i + 1] = (tmp >> 16) & 0xff;
  7060. np->vpd.board_model[i + 0] = (tmp >> 24) & 0xff;
  7061. }
  7062. np->vpd.board_model[val] = '\0';
  7063. np->vpd.mac_num =
  7064. nr64(ESPC_NUM_PORTS_MACS) & ESPC_NUM_PORTS_MACS_VAL;
  7065. netif_printk(np, probe, KERN_DEBUG, np->dev,
  7066. "SPROM: NUM_PORTS_MACS[%d]\n", np->vpd.mac_num);
  7067. return 0;
  7068. }
  7069. static int __devinit niu_get_and_validate_port(struct niu *np)
  7070. {
  7071. struct niu_parent *parent = np->parent;
  7072. if (np->port <= 1)
  7073. np->flags |= NIU_FLAGS_XMAC;
  7074. if (!parent->num_ports) {
  7075. if (parent->plat_type == PLAT_TYPE_NIU) {
  7076. parent->num_ports = 2;
  7077. } else {
  7078. parent->num_ports = niu_pci_vpd_get_nports(np);
  7079. if (!parent->num_ports) {
  7080. /* Fall back to SPROM as last resort.
  7081. * This will fail on most cards.
  7082. */
  7083. parent->num_ports = nr64(ESPC_NUM_PORTS_MACS) &
  7084. ESPC_NUM_PORTS_MACS_VAL;
  7085. /* All of the current probing methods fail on
  7086. * Maramba on-board parts.
  7087. */
  7088. if (!parent->num_ports)
  7089. parent->num_ports = 4;
  7090. }
  7091. }
  7092. }
  7093. if (np->port >= parent->num_ports)
  7094. return -ENODEV;
  7095. return 0;
  7096. }
  7097. static int __devinit phy_record(struct niu_parent *parent,
  7098. struct phy_probe_info *p,
  7099. int dev_id_1, int dev_id_2, u8 phy_port,
  7100. int type)
  7101. {
  7102. u32 id = (dev_id_1 << 16) | dev_id_2;
  7103. u8 idx;
  7104. if (dev_id_1 < 0 || dev_id_2 < 0)
  7105. return 0;
  7106. if (type == PHY_TYPE_PMA_PMD || type == PHY_TYPE_PCS) {
  7107. if (((id & NIU_PHY_ID_MASK) != NIU_PHY_ID_BCM8704) &&
  7108. ((id & NIU_PHY_ID_MASK) != NIU_PHY_ID_MRVL88X2011) &&
  7109. ((id & NIU_PHY_ID_MASK) != NIU_PHY_ID_BCM8706))
  7110. return 0;
  7111. } else {
  7112. if ((id & NIU_PHY_ID_MASK) != NIU_PHY_ID_BCM5464R)
  7113. return 0;
  7114. }
  7115. pr_info("niu%d: Found PHY %08x type %s at phy_port %u\n",
  7116. parent->index, id,
  7117. type == PHY_TYPE_PMA_PMD ? "PMA/PMD" :
  7118. type == PHY_TYPE_PCS ? "PCS" : "MII",
  7119. phy_port);
  7120. if (p->cur[type] >= NIU_MAX_PORTS) {
  7121. pr_err("Too many PHY ports\n");
  7122. return -EINVAL;
  7123. }
  7124. idx = p->cur[type];
  7125. p->phy_id[type][idx] = id;
  7126. p->phy_port[type][idx] = phy_port;
  7127. p->cur[type] = idx + 1;
  7128. return 0;
  7129. }
  7130. static int __devinit port_has_10g(struct phy_probe_info *p, int port)
  7131. {
  7132. int i;
  7133. for (i = 0; i < p->cur[PHY_TYPE_PMA_PMD]; i++) {
  7134. if (p->phy_port[PHY_TYPE_PMA_PMD][i] == port)
  7135. return 1;
  7136. }
  7137. for (i = 0; i < p->cur[PHY_TYPE_PCS]; i++) {
  7138. if (p->phy_port[PHY_TYPE_PCS][i] == port)
  7139. return 1;
  7140. }
  7141. return 0;
  7142. }
  7143. static int __devinit count_10g_ports(struct phy_probe_info *p, int *lowest)
  7144. {
  7145. int port, cnt;
  7146. cnt = 0;
  7147. *lowest = 32;
  7148. for (port = 8; port < 32; port++) {
  7149. if (port_has_10g(p, port)) {
  7150. if (!cnt)
  7151. *lowest = port;
  7152. cnt++;
  7153. }
  7154. }
  7155. return cnt;
  7156. }
  7157. static int __devinit count_1g_ports(struct phy_probe_info *p, int *lowest)
  7158. {
  7159. *lowest = 32;
  7160. if (p->cur[PHY_TYPE_MII])
  7161. *lowest = p->phy_port[PHY_TYPE_MII][0];
  7162. return p->cur[PHY_TYPE_MII];
  7163. }
  7164. static void __devinit niu_n2_divide_channels(struct niu_parent *parent)
  7165. {
  7166. int num_ports = parent->num_ports;
  7167. int i;
  7168. for (i = 0; i < num_ports; i++) {
  7169. parent->rxchan_per_port[i] = (16 / num_ports);
  7170. parent->txchan_per_port[i] = (16 / num_ports);
  7171. pr_info("niu%d: Port %u [%u RX chans] [%u TX chans]\n",
  7172. parent->index, i,
  7173. parent->rxchan_per_port[i],
  7174. parent->txchan_per_port[i]);
  7175. }
  7176. }
  7177. static void __devinit niu_divide_channels(struct niu_parent *parent,
  7178. int num_10g, int num_1g)
  7179. {
  7180. int num_ports = parent->num_ports;
  7181. int rx_chans_per_10g, rx_chans_per_1g;
  7182. int tx_chans_per_10g, tx_chans_per_1g;
  7183. int i, tot_rx, tot_tx;
  7184. if (!num_10g || !num_1g) {
  7185. rx_chans_per_10g = rx_chans_per_1g =
  7186. (NIU_NUM_RXCHAN / num_ports);
  7187. tx_chans_per_10g = tx_chans_per_1g =
  7188. (NIU_NUM_TXCHAN / num_ports);
  7189. } else {
  7190. rx_chans_per_1g = NIU_NUM_RXCHAN / 8;
  7191. rx_chans_per_10g = (NIU_NUM_RXCHAN -
  7192. (rx_chans_per_1g * num_1g)) /
  7193. num_10g;
  7194. tx_chans_per_1g = NIU_NUM_TXCHAN / 6;
  7195. tx_chans_per_10g = (NIU_NUM_TXCHAN -
  7196. (tx_chans_per_1g * num_1g)) /
  7197. num_10g;
  7198. }
  7199. tot_rx = tot_tx = 0;
  7200. for (i = 0; i < num_ports; i++) {
  7201. int type = phy_decode(parent->port_phy, i);
  7202. if (type == PORT_TYPE_10G) {
  7203. parent->rxchan_per_port[i] = rx_chans_per_10g;
  7204. parent->txchan_per_port[i] = tx_chans_per_10g;
  7205. } else {
  7206. parent->rxchan_per_port[i] = rx_chans_per_1g;
  7207. parent->txchan_per_port[i] = tx_chans_per_1g;
  7208. }
  7209. pr_info("niu%d: Port %u [%u RX chans] [%u TX chans]\n",
  7210. parent->index, i,
  7211. parent->rxchan_per_port[i],
  7212. parent->txchan_per_port[i]);
  7213. tot_rx += parent->rxchan_per_port[i];
  7214. tot_tx += parent->txchan_per_port[i];
  7215. }
  7216. if (tot_rx > NIU_NUM_RXCHAN) {
  7217. pr_err("niu%d: Too many RX channels (%d), resetting to one per port\n",
  7218. parent->index, tot_rx);
  7219. for (i = 0; i < num_ports; i++)
  7220. parent->rxchan_per_port[i] = 1;
  7221. }
  7222. if (tot_tx > NIU_NUM_TXCHAN) {
  7223. pr_err("niu%d: Too many TX channels (%d), resetting to one per port\n",
  7224. parent->index, tot_tx);
  7225. for (i = 0; i < num_ports; i++)
  7226. parent->txchan_per_port[i] = 1;
  7227. }
  7228. if (tot_rx < NIU_NUM_RXCHAN || tot_tx < NIU_NUM_TXCHAN) {
  7229. pr_warning("niu%d: Driver bug, wasted channels, RX[%d] TX[%d]\n",
  7230. parent->index, tot_rx, tot_tx);
  7231. }
  7232. }
  7233. static void __devinit niu_divide_rdc_groups(struct niu_parent *parent,
  7234. int num_10g, int num_1g)
  7235. {
  7236. int i, num_ports = parent->num_ports;
  7237. int rdc_group, rdc_groups_per_port;
  7238. int rdc_channel_base;
  7239. rdc_group = 0;
  7240. rdc_groups_per_port = NIU_NUM_RDC_TABLES / num_ports;
  7241. rdc_channel_base = 0;
  7242. for (i = 0; i < num_ports; i++) {
  7243. struct niu_rdc_tables *tp = &parent->rdc_group_cfg[i];
  7244. int grp, num_channels = parent->rxchan_per_port[i];
  7245. int this_channel_offset;
  7246. tp->first_table_num = rdc_group;
  7247. tp->num_tables = rdc_groups_per_port;
  7248. this_channel_offset = 0;
  7249. for (grp = 0; grp < tp->num_tables; grp++) {
  7250. struct rdc_table *rt = &tp->tables[grp];
  7251. int slot;
  7252. pr_info("niu%d: Port %d RDC tbl(%d) [ ",
  7253. parent->index, i, tp->first_table_num + grp);
  7254. for (slot = 0; slot < NIU_RDC_TABLE_SLOTS; slot++) {
  7255. rt->rxdma_channel[slot] =
  7256. rdc_channel_base + this_channel_offset;
  7257. pr_cont("%d ", rt->rxdma_channel[slot]);
  7258. if (++this_channel_offset == num_channels)
  7259. this_channel_offset = 0;
  7260. }
  7261. pr_cont("]\n");
  7262. }
  7263. parent->rdc_default[i] = rdc_channel_base;
  7264. rdc_channel_base += num_channels;
  7265. rdc_group += rdc_groups_per_port;
  7266. }
  7267. }
  7268. static int __devinit fill_phy_probe_info(struct niu *np,
  7269. struct niu_parent *parent,
  7270. struct phy_probe_info *info)
  7271. {
  7272. unsigned long flags;
  7273. int port, err;
  7274. memset(info, 0, sizeof(*info));
  7275. /* Port 0 to 7 are reserved for onboard Serdes, probe the rest. */
  7276. niu_lock_parent(np, flags);
  7277. err = 0;
  7278. for (port = 8; port < 32; port++) {
  7279. int dev_id_1, dev_id_2;
  7280. dev_id_1 = mdio_read(np, port,
  7281. NIU_PMA_PMD_DEV_ADDR, MII_PHYSID1);
  7282. dev_id_2 = mdio_read(np, port,
  7283. NIU_PMA_PMD_DEV_ADDR, MII_PHYSID2);
  7284. err = phy_record(parent, info, dev_id_1, dev_id_2, port,
  7285. PHY_TYPE_PMA_PMD);
  7286. if (err)
  7287. break;
  7288. dev_id_1 = mdio_read(np, port,
  7289. NIU_PCS_DEV_ADDR, MII_PHYSID1);
  7290. dev_id_2 = mdio_read(np, port,
  7291. NIU_PCS_DEV_ADDR, MII_PHYSID2);
  7292. err = phy_record(parent, info, dev_id_1, dev_id_2, port,
  7293. PHY_TYPE_PCS);
  7294. if (err)
  7295. break;
  7296. dev_id_1 = mii_read(np, port, MII_PHYSID1);
  7297. dev_id_2 = mii_read(np, port, MII_PHYSID2);
  7298. err = phy_record(parent, info, dev_id_1, dev_id_2, port,
  7299. PHY_TYPE_MII);
  7300. if (err)
  7301. break;
  7302. }
  7303. niu_unlock_parent(np, flags);
  7304. return err;
  7305. }
  7306. static int __devinit walk_phys(struct niu *np, struct niu_parent *parent)
  7307. {
  7308. struct phy_probe_info *info = &parent->phy_probe_info;
  7309. int lowest_10g, lowest_1g;
  7310. int num_10g, num_1g;
  7311. u32 val;
  7312. int err;
  7313. num_10g = num_1g = 0;
  7314. if (!strcmp(np->vpd.model, NIU_ALONSO_MDL_STR) ||
  7315. !strcmp(np->vpd.model, NIU_KIMI_MDL_STR)) {
  7316. num_10g = 0;
  7317. num_1g = 2;
  7318. parent->plat_type = PLAT_TYPE_ATCA_CP3220;
  7319. parent->num_ports = 4;
  7320. val = (phy_encode(PORT_TYPE_1G, 0) |
  7321. phy_encode(PORT_TYPE_1G, 1) |
  7322. phy_encode(PORT_TYPE_1G, 2) |
  7323. phy_encode(PORT_TYPE_1G, 3));
  7324. } else if (!strcmp(np->vpd.model, NIU_FOXXY_MDL_STR)) {
  7325. num_10g = 2;
  7326. num_1g = 0;
  7327. parent->num_ports = 2;
  7328. val = (phy_encode(PORT_TYPE_10G, 0) |
  7329. phy_encode(PORT_TYPE_10G, 1));
  7330. } else if ((np->flags & NIU_FLAGS_XCVR_SERDES) &&
  7331. (parent->plat_type == PLAT_TYPE_NIU)) {
  7332. /* this is the Monza case */
  7333. if (np->flags & NIU_FLAGS_10G) {
  7334. val = (phy_encode(PORT_TYPE_10G, 0) |
  7335. phy_encode(PORT_TYPE_10G, 1));
  7336. } else {
  7337. val = (phy_encode(PORT_TYPE_1G, 0) |
  7338. phy_encode(PORT_TYPE_1G, 1));
  7339. }
  7340. } else {
  7341. err = fill_phy_probe_info(np, parent, info);
  7342. if (err)
  7343. return err;
  7344. num_10g = count_10g_ports(info, &lowest_10g);
  7345. num_1g = count_1g_ports(info, &lowest_1g);
  7346. switch ((num_10g << 4) | num_1g) {
  7347. case 0x24:
  7348. if (lowest_1g == 10)
  7349. parent->plat_type = PLAT_TYPE_VF_P0;
  7350. else if (lowest_1g == 26)
  7351. parent->plat_type = PLAT_TYPE_VF_P1;
  7352. else
  7353. goto unknown_vg_1g_port;
  7354. /* fallthru */
  7355. case 0x22:
  7356. val = (phy_encode(PORT_TYPE_10G, 0) |
  7357. phy_encode(PORT_TYPE_10G, 1) |
  7358. phy_encode(PORT_TYPE_1G, 2) |
  7359. phy_encode(PORT_TYPE_1G, 3));
  7360. break;
  7361. case 0x20:
  7362. val = (phy_encode(PORT_TYPE_10G, 0) |
  7363. phy_encode(PORT_TYPE_10G, 1));
  7364. break;
  7365. case 0x10:
  7366. val = phy_encode(PORT_TYPE_10G, np->port);
  7367. break;
  7368. case 0x14:
  7369. if (lowest_1g == 10)
  7370. parent->plat_type = PLAT_TYPE_VF_P0;
  7371. else if (lowest_1g == 26)
  7372. parent->plat_type = PLAT_TYPE_VF_P1;
  7373. else
  7374. goto unknown_vg_1g_port;
  7375. /* fallthru */
  7376. case 0x13:
  7377. if ((lowest_10g & 0x7) == 0)
  7378. val = (phy_encode(PORT_TYPE_10G, 0) |
  7379. phy_encode(PORT_TYPE_1G, 1) |
  7380. phy_encode(PORT_TYPE_1G, 2) |
  7381. phy_encode(PORT_TYPE_1G, 3));
  7382. else
  7383. val = (phy_encode(PORT_TYPE_1G, 0) |
  7384. phy_encode(PORT_TYPE_10G, 1) |
  7385. phy_encode(PORT_TYPE_1G, 2) |
  7386. phy_encode(PORT_TYPE_1G, 3));
  7387. break;
  7388. case 0x04:
  7389. if (lowest_1g == 10)
  7390. parent->plat_type = PLAT_TYPE_VF_P0;
  7391. else if (lowest_1g == 26)
  7392. parent->plat_type = PLAT_TYPE_VF_P1;
  7393. else
  7394. goto unknown_vg_1g_port;
  7395. val = (phy_encode(PORT_TYPE_1G, 0) |
  7396. phy_encode(PORT_TYPE_1G, 1) |
  7397. phy_encode(PORT_TYPE_1G, 2) |
  7398. phy_encode(PORT_TYPE_1G, 3));
  7399. break;
  7400. default:
  7401. pr_err("Unsupported port config 10G[%d] 1G[%d]\n",
  7402. num_10g, num_1g);
  7403. return -EINVAL;
  7404. }
  7405. }
  7406. parent->port_phy = val;
  7407. if (parent->plat_type == PLAT_TYPE_NIU)
  7408. niu_n2_divide_channels(parent);
  7409. else
  7410. niu_divide_channels(parent, num_10g, num_1g);
  7411. niu_divide_rdc_groups(parent, num_10g, num_1g);
  7412. return 0;
  7413. unknown_vg_1g_port:
  7414. pr_err("Cannot identify platform type, 1gport=%d\n", lowest_1g);
  7415. return -EINVAL;
  7416. }
  7417. static int __devinit niu_probe_ports(struct niu *np)
  7418. {
  7419. struct niu_parent *parent = np->parent;
  7420. int err, i;
  7421. if (parent->port_phy == PORT_PHY_UNKNOWN) {
  7422. err = walk_phys(np, parent);
  7423. if (err)
  7424. return err;
  7425. niu_set_ldg_timer_res(np, 2);
  7426. for (i = 0; i <= LDN_MAX; i++)
  7427. niu_ldn_irq_enable(np, i, 0);
  7428. }
  7429. if (parent->port_phy == PORT_PHY_INVALID)
  7430. return -EINVAL;
  7431. return 0;
  7432. }
  7433. static int __devinit niu_classifier_swstate_init(struct niu *np)
  7434. {
  7435. struct niu_classifier *cp = &np->clas;
  7436. cp->tcam_top = (u16) np->port;
  7437. cp->tcam_sz = np->parent->tcam_num_entries / np->parent->num_ports;
  7438. cp->h1_init = 0xffffffff;
  7439. cp->h2_init = 0xffff;
  7440. return fflp_early_init(np);
  7441. }
  7442. static void __devinit niu_link_config_init(struct niu *np)
  7443. {
  7444. struct niu_link_config *lp = &np->link_config;
  7445. lp->advertising = (ADVERTISED_10baseT_Half |
  7446. ADVERTISED_10baseT_Full |
  7447. ADVERTISED_100baseT_Half |
  7448. ADVERTISED_100baseT_Full |
  7449. ADVERTISED_1000baseT_Half |
  7450. ADVERTISED_1000baseT_Full |
  7451. ADVERTISED_10000baseT_Full |
  7452. ADVERTISED_Autoneg);
  7453. lp->speed = lp->active_speed = SPEED_INVALID;
  7454. lp->duplex = DUPLEX_FULL;
  7455. lp->active_duplex = DUPLEX_INVALID;
  7456. lp->autoneg = 1;
  7457. #if 0
  7458. lp->loopback_mode = LOOPBACK_MAC;
  7459. lp->active_speed = SPEED_10000;
  7460. lp->active_duplex = DUPLEX_FULL;
  7461. #else
  7462. lp->loopback_mode = LOOPBACK_DISABLED;
  7463. #endif
  7464. }
  7465. static int __devinit niu_init_mac_ipp_pcs_base(struct niu *np)
  7466. {
  7467. switch (np->port) {
  7468. case 0:
  7469. np->mac_regs = np->regs + XMAC_PORT0_OFF;
  7470. np->ipp_off = 0x00000;
  7471. np->pcs_off = 0x04000;
  7472. np->xpcs_off = 0x02000;
  7473. break;
  7474. case 1:
  7475. np->mac_regs = np->regs + XMAC_PORT1_OFF;
  7476. np->ipp_off = 0x08000;
  7477. np->pcs_off = 0x0a000;
  7478. np->xpcs_off = 0x08000;
  7479. break;
  7480. case 2:
  7481. np->mac_regs = np->regs + BMAC_PORT2_OFF;
  7482. np->ipp_off = 0x04000;
  7483. np->pcs_off = 0x0e000;
  7484. np->xpcs_off = ~0UL;
  7485. break;
  7486. case 3:
  7487. np->mac_regs = np->regs + BMAC_PORT3_OFF;
  7488. np->ipp_off = 0x0c000;
  7489. np->pcs_off = 0x12000;
  7490. np->xpcs_off = ~0UL;
  7491. break;
  7492. default:
  7493. dev_err(np->device, "Port %u is invalid, cannot compute MAC block offset\n", np->port);
  7494. return -EINVAL;
  7495. }
  7496. return 0;
  7497. }
  7498. static void __devinit niu_try_msix(struct niu *np, u8 *ldg_num_map)
  7499. {
  7500. struct msix_entry msi_vec[NIU_NUM_LDG];
  7501. struct niu_parent *parent = np->parent;
  7502. struct pci_dev *pdev = np->pdev;
  7503. int i, num_irqs, err;
  7504. u8 first_ldg;
  7505. first_ldg = (NIU_NUM_LDG / parent->num_ports) * np->port;
  7506. for (i = 0; i < (NIU_NUM_LDG / parent->num_ports); i++)
  7507. ldg_num_map[i] = first_ldg + i;
  7508. num_irqs = (parent->rxchan_per_port[np->port] +
  7509. parent->txchan_per_port[np->port] +
  7510. (np->port == 0 ? 3 : 1));
  7511. BUG_ON(num_irqs > (NIU_NUM_LDG / parent->num_ports));
  7512. retry:
  7513. for (i = 0; i < num_irqs; i++) {
  7514. msi_vec[i].vector = 0;
  7515. msi_vec[i].entry = i;
  7516. }
  7517. err = pci_enable_msix(pdev, msi_vec, num_irqs);
  7518. if (err < 0) {
  7519. np->flags &= ~NIU_FLAGS_MSIX;
  7520. return;
  7521. }
  7522. if (err > 0) {
  7523. num_irqs = err;
  7524. goto retry;
  7525. }
  7526. np->flags |= NIU_FLAGS_MSIX;
  7527. for (i = 0; i < num_irqs; i++)
  7528. np->ldg[i].irq = msi_vec[i].vector;
  7529. np->num_ldg = num_irqs;
  7530. }
  7531. static int __devinit niu_n2_irq_init(struct niu *np, u8 *ldg_num_map)
  7532. {
  7533. #ifdef CONFIG_SPARC64
  7534. struct platform_device *op = np->op;
  7535. const u32 *int_prop;
  7536. int i;
  7537. int_prop = of_get_property(op->dev.of_node, "interrupts", NULL);
  7538. if (!int_prop)
  7539. return -ENODEV;
  7540. for (i = 0; i < op->archdata.num_irqs; i++) {
  7541. ldg_num_map[i] = int_prop[i];
  7542. np->ldg[i].irq = op->archdata.irqs[i];
  7543. }
  7544. np->num_ldg = op->archdata.num_irqs;
  7545. return 0;
  7546. #else
  7547. return -EINVAL;
  7548. #endif
  7549. }
  7550. static int __devinit niu_ldg_init(struct niu *np)
  7551. {
  7552. struct niu_parent *parent = np->parent;
  7553. u8 ldg_num_map[NIU_NUM_LDG];
  7554. int first_chan, num_chan;
  7555. int i, err, ldg_rotor;
  7556. u8 port;
  7557. np->num_ldg = 1;
  7558. np->ldg[0].irq = np->dev->irq;
  7559. if (parent->plat_type == PLAT_TYPE_NIU) {
  7560. err = niu_n2_irq_init(np, ldg_num_map);
  7561. if (err)
  7562. return err;
  7563. } else
  7564. niu_try_msix(np, ldg_num_map);
  7565. port = np->port;
  7566. for (i = 0; i < np->num_ldg; i++) {
  7567. struct niu_ldg *lp = &np->ldg[i];
  7568. netif_napi_add(np->dev, &lp->napi, niu_poll, 64);
  7569. lp->np = np;
  7570. lp->ldg_num = ldg_num_map[i];
  7571. lp->timer = 2; /* XXX */
  7572. /* On N2 NIU the firmware has setup the SID mappings so they go
  7573. * to the correct values that will route the LDG to the proper
  7574. * interrupt in the NCU interrupt table.
  7575. */
  7576. if (np->parent->plat_type != PLAT_TYPE_NIU) {
  7577. err = niu_set_ldg_sid(np, lp->ldg_num, port, i);
  7578. if (err)
  7579. return err;
  7580. }
  7581. }
  7582. /* We adopt the LDG assignment ordering used by the N2 NIU
  7583. * 'interrupt' properties because that simplifies a lot of
  7584. * things. This ordering is:
  7585. *
  7586. * MAC
  7587. * MIF (if port zero)
  7588. * SYSERR (if port zero)
  7589. * RX channels
  7590. * TX channels
  7591. */
  7592. ldg_rotor = 0;
  7593. err = niu_ldg_assign_ldn(np, parent, ldg_num_map[ldg_rotor],
  7594. LDN_MAC(port));
  7595. if (err)
  7596. return err;
  7597. ldg_rotor++;
  7598. if (ldg_rotor == np->num_ldg)
  7599. ldg_rotor = 0;
  7600. if (port == 0) {
  7601. err = niu_ldg_assign_ldn(np, parent,
  7602. ldg_num_map[ldg_rotor],
  7603. LDN_MIF);
  7604. if (err)
  7605. return err;
  7606. ldg_rotor++;
  7607. if (ldg_rotor == np->num_ldg)
  7608. ldg_rotor = 0;
  7609. err = niu_ldg_assign_ldn(np, parent,
  7610. ldg_num_map[ldg_rotor],
  7611. LDN_DEVICE_ERROR);
  7612. if (err)
  7613. return err;
  7614. ldg_rotor++;
  7615. if (ldg_rotor == np->num_ldg)
  7616. ldg_rotor = 0;
  7617. }
  7618. first_chan = 0;
  7619. for (i = 0; i < port; i++)
  7620. first_chan += parent->rxchan_per_port[i];
  7621. num_chan = parent->rxchan_per_port[port];
  7622. for (i = first_chan; i < (first_chan + num_chan); i++) {
  7623. err = niu_ldg_assign_ldn(np, parent,
  7624. ldg_num_map[ldg_rotor],
  7625. LDN_RXDMA(i));
  7626. if (err)
  7627. return err;
  7628. ldg_rotor++;
  7629. if (ldg_rotor == np->num_ldg)
  7630. ldg_rotor = 0;
  7631. }
  7632. first_chan = 0;
  7633. for (i = 0; i < port; i++)
  7634. first_chan += parent->txchan_per_port[i];
  7635. num_chan = parent->txchan_per_port[port];
  7636. for (i = first_chan; i < (first_chan + num_chan); i++) {
  7637. err = niu_ldg_assign_ldn(np, parent,
  7638. ldg_num_map[ldg_rotor],
  7639. LDN_TXDMA(i));
  7640. if (err)
  7641. return err;
  7642. ldg_rotor++;
  7643. if (ldg_rotor == np->num_ldg)
  7644. ldg_rotor = 0;
  7645. }
  7646. return 0;
  7647. }
  7648. static void __devexit niu_ldg_free(struct niu *np)
  7649. {
  7650. if (np->flags & NIU_FLAGS_MSIX)
  7651. pci_disable_msix(np->pdev);
  7652. }
  7653. static int __devinit niu_get_of_props(struct niu *np)
  7654. {
  7655. #ifdef CONFIG_SPARC64
  7656. struct net_device *dev = np->dev;
  7657. struct device_node *dp;
  7658. const char *phy_type;
  7659. const u8 *mac_addr;
  7660. const char *model;
  7661. int prop_len;
  7662. if (np->parent->plat_type == PLAT_TYPE_NIU)
  7663. dp = np->op->dev.of_node;
  7664. else
  7665. dp = pci_device_to_OF_node(np->pdev);
  7666. phy_type = of_get_property(dp, "phy-type", &prop_len);
  7667. if (!phy_type) {
  7668. netdev_err(dev, "%s: OF node lacks phy-type property\n",
  7669. dp->full_name);
  7670. return -EINVAL;
  7671. }
  7672. if (!strcmp(phy_type, "none"))
  7673. return -ENODEV;
  7674. strcpy(np->vpd.phy_type, phy_type);
  7675. if (niu_phy_type_prop_decode(np, np->vpd.phy_type)) {
  7676. netdev_err(dev, "%s: Illegal phy string [%s]\n",
  7677. dp->full_name, np->vpd.phy_type);
  7678. return -EINVAL;
  7679. }
  7680. mac_addr = of_get_property(dp, "local-mac-address", &prop_len);
  7681. if (!mac_addr) {
  7682. netdev_err(dev, "%s: OF node lacks local-mac-address property\n",
  7683. dp->full_name);
  7684. return -EINVAL;
  7685. }
  7686. if (prop_len != dev->addr_len) {
  7687. netdev_err(dev, "%s: OF MAC address prop len (%d) is wrong\n",
  7688. dp->full_name, prop_len);
  7689. }
  7690. memcpy(dev->perm_addr, mac_addr, dev->addr_len);
  7691. if (!is_valid_ether_addr(&dev->perm_addr[0])) {
  7692. netdev_err(dev, "%s: OF MAC address is invalid\n",
  7693. dp->full_name);
  7694. netdev_err(dev, "%s: [ %pM ]\n", dp->full_name, dev->perm_addr);
  7695. return -EINVAL;
  7696. }
  7697. memcpy(dev->dev_addr, dev->perm_addr, dev->addr_len);
  7698. model = of_get_property(dp, "model", &prop_len);
  7699. if (model)
  7700. strcpy(np->vpd.model, model);
  7701. if (of_find_property(dp, "hot-swappable-phy", &prop_len)) {
  7702. np->flags |= (NIU_FLAGS_10G | NIU_FLAGS_FIBER |
  7703. NIU_FLAGS_HOTPLUG_PHY);
  7704. }
  7705. return 0;
  7706. #else
  7707. return -EINVAL;
  7708. #endif
  7709. }
  7710. static int __devinit niu_get_invariants(struct niu *np)
  7711. {
  7712. int err, have_props;
  7713. u32 offset;
  7714. err = niu_get_of_props(np);
  7715. if (err == -ENODEV)
  7716. return err;
  7717. have_props = !err;
  7718. err = niu_init_mac_ipp_pcs_base(np);
  7719. if (err)
  7720. return err;
  7721. if (have_props) {
  7722. err = niu_get_and_validate_port(np);
  7723. if (err)
  7724. return err;
  7725. } else {
  7726. if (np->parent->plat_type == PLAT_TYPE_NIU)
  7727. return -EINVAL;
  7728. nw64(ESPC_PIO_EN, ESPC_PIO_EN_ENABLE);
  7729. offset = niu_pci_vpd_offset(np);
  7730. netif_printk(np, probe, KERN_DEBUG, np->dev,
  7731. "%s() VPD offset [%08x]\n", __func__, offset);
  7732. if (offset)
  7733. niu_pci_vpd_fetch(np, offset);
  7734. nw64(ESPC_PIO_EN, 0);
  7735. if (np->flags & NIU_FLAGS_VPD_VALID) {
  7736. niu_pci_vpd_validate(np);
  7737. err = niu_get_and_validate_port(np);
  7738. if (err)
  7739. return err;
  7740. }
  7741. if (!(np->flags & NIU_FLAGS_VPD_VALID)) {
  7742. err = niu_get_and_validate_port(np);
  7743. if (err)
  7744. return err;
  7745. err = niu_pci_probe_sprom(np);
  7746. if (err)
  7747. return err;
  7748. }
  7749. }
  7750. err = niu_probe_ports(np);
  7751. if (err)
  7752. return err;
  7753. niu_ldg_init(np);
  7754. niu_classifier_swstate_init(np);
  7755. niu_link_config_init(np);
  7756. err = niu_determine_phy_disposition(np);
  7757. if (!err)
  7758. err = niu_init_link(np);
  7759. return err;
  7760. }
  7761. static LIST_HEAD(niu_parent_list);
  7762. static DEFINE_MUTEX(niu_parent_lock);
  7763. static int niu_parent_index;
  7764. static ssize_t show_port_phy(struct device *dev,
  7765. struct device_attribute *attr, char *buf)
  7766. {
  7767. struct platform_device *plat_dev = to_platform_device(dev);
  7768. struct niu_parent *p = plat_dev->dev.platform_data;
  7769. u32 port_phy = p->port_phy;
  7770. char *orig_buf = buf;
  7771. int i;
  7772. if (port_phy == PORT_PHY_UNKNOWN ||
  7773. port_phy == PORT_PHY_INVALID)
  7774. return 0;
  7775. for (i = 0; i < p->num_ports; i++) {
  7776. const char *type_str;
  7777. int type;
  7778. type = phy_decode(port_phy, i);
  7779. if (type == PORT_TYPE_10G)
  7780. type_str = "10G";
  7781. else
  7782. type_str = "1G";
  7783. buf += sprintf(buf,
  7784. (i == 0) ? "%s" : " %s",
  7785. type_str);
  7786. }
  7787. buf += sprintf(buf, "\n");
  7788. return buf - orig_buf;
  7789. }
  7790. static ssize_t show_plat_type(struct device *dev,
  7791. struct device_attribute *attr, char *buf)
  7792. {
  7793. struct platform_device *plat_dev = to_platform_device(dev);
  7794. struct niu_parent *p = plat_dev->dev.platform_data;
  7795. const char *type_str;
  7796. switch (p->plat_type) {
  7797. case PLAT_TYPE_ATLAS:
  7798. type_str = "atlas";
  7799. break;
  7800. case PLAT_TYPE_NIU:
  7801. type_str = "niu";
  7802. break;
  7803. case PLAT_TYPE_VF_P0:
  7804. type_str = "vf_p0";
  7805. break;
  7806. case PLAT_TYPE_VF_P1:
  7807. type_str = "vf_p1";
  7808. break;
  7809. default:
  7810. type_str = "unknown";
  7811. break;
  7812. }
  7813. return sprintf(buf, "%s\n", type_str);
  7814. }
  7815. static ssize_t __show_chan_per_port(struct device *dev,
  7816. struct device_attribute *attr, char *buf,
  7817. int rx)
  7818. {
  7819. struct platform_device *plat_dev = to_platform_device(dev);
  7820. struct niu_parent *p = plat_dev->dev.platform_data;
  7821. char *orig_buf = buf;
  7822. u8 *arr;
  7823. int i;
  7824. arr = (rx ? p->rxchan_per_port : p->txchan_per_port);
  7825. for (i = 0; i < p->num_ports; i++) {
  7826. buf += sprintf(buf,
  7827. (i == 0) ? "%d" : " %d",
  7828. arr[i]);
  7829. }
  7830. buf += sprintf(buf, "\n");
  7831. return buf - orig_buf;
  7832. }
  7833. static ssize_t show_rxchan_per_port(struct device *dev,
  7834. struct device_attribute *attr, char *buf)
  7835. {
  7836. return __show_chan_per_port(dev, attr, buf, 1);
  7837. }
  7838. static ssize_t show_txchan_per_port(struct device *dev,
  7839. struct device_attribute *attr, char *buf)
  7840. {
  7841. return __show_chan_per_port(dev, attr, buf, 1);
  7842. }
  7843. static ssize_t show_num_ports(struct device *dev,
  7844. struct device_attribute *attr, char *buf)
  7845. {
  7846. struct platform_device *plat_dev = to_platform_device(dev);
  7847. struct niu_parent *p = plat_dev->dev.platform_data;
  7848. return sprintf(buf, "%d\n", p->num_ports);
  7849. }
  7850. static struct device_attribute niu_parent_attributes[] = {
  7851. __ATTR(port_phy, S_IRUGO, show_port_phy, NULL),
  7852. __ATTR(plat_type, S_IRUGO, show_plat_type, NULL),
  7853. __ATTR(rxchan_per_port, S_IRUGO, show_rxchan_per_port, NULL),
  7854. __ATTR(txchan_per_port, S_IRUGO, show_txchan_per_port, NULL),
  7855. __ATTR(num_ports, S_IRUGO, show_num_ports, NULL),
  7856. {}
  7857. };
  7858. static struct niu_parent * __devinit niu_new_parent(struct niu *np,
  7859. union niu_parent_id *id,
  7860. u8 ptype)
  7861. {
  7862. struct platform_device *plat_dev;
  7863. struct niu_parent *p;
  7864. int i;
  7865. plat_dev = platform_device_register_simple("niu-board", niu_parent_index,
  7866. NULL, 0);
  7867. if (IS_ERR(plat_dev))
  7868. return NULL;
  7869. for (i = 0; attr_name(niu_parent_attributes[i]); i++) {
  7870. int err = device_create_file(&plat_dev->dev,
  7871. &niu_parent_attributes[i]);
  7872. if (err)
  7873. goto fail_unregister;
  7874. }
  7875. p = kzalloc(sizeof(*p), GFP_KERNEL);
  7876. if (!p)
  7877. goto fail_unregister;
  7878. p->index = niu_parent_index++;
  7879. plat_dev->dev.platform_data = p;
  7880. p->plat_dev = plat_dev;
  7881. memcpy(&p->id, id, sizeof(*id));
  7882. p->plat_type = ptype;
  7883. INIT_LIST_HEAD(&p->list);
  7884. atomic_set(&p->refcnt, 0);
  7885. list_add(&p->list, &niu_parent_list);
  7886. spin_lock_init(&p->lock);
  7887. p->rxdma_clock_divider = 7500;
  7888. p->tcam_num_entries = NIU_PCI_TCAM_ENTRIES;
  7889. if (p->plat_type == PLAT_TYPE_NIU)
  7890. p->tcam_num_entries = NIU_NONPCI_TCAM_ENTRIES;
  7891. for (i = CLASS_CODE_USER_PROG1; i <= CLASS_CODE_SCTP_IPV6; i++) {
  7892. int index = i - CLASS_CODE_USER_PROG1;
  7893. p->tcam_key[index] = TCAM_KEY_TSEL;
  7894. p->flow_key[index] = (FLOW_KEY_IPSA |
  7895. FLOW_KEY_IPDA |
  7896. FLOW_KEY_PROTO |
  7897. (FLOW_KEY_L4_BYTE12 <<
  7898. FLOW_KEY_L4_0_SHIFT) |
  7899. (FLOW_KEY_L4_BYTE12 <<
  7900. FLOW_KEY_L4_1_SHIFT));
  7901. }
  7902. for (i = 0; i < LDN_MAX + 1; i++)
  7903. p->ldg_map[i] = LDG_INVALID;
  7904. return p;
  7905. fail_unregister:
  7906. platform_device_unregister(plat_dev);
  7907. return NULL;
  7908. }
  7909. static struct niu_parent * __devinit niu_get_parent(struct niu *np,
  7910. union niu_parent_id *id,
  7911. u8 ptype)
  7912. {
  7913. struct niu_parent *p, *tmp;
  7914. int port = np->port;
  7915. mutex_lock(&niu_parent_lock);
  7916. p = NULL;
  7917. list_for_each_entry(tmp, &niu_parent_list, list) {
  7918. if (!memcmp(id, &tmp->id, sizeof(*id))) {
  7919. p = tmp;
  7920. break;
  7921. }
  7922. }
  7923. if (!p)
  7924. p = niu_new_parent(np, id, ptype);
  7925. if (p) {
  7926. char port_name[6];
  7927. int err;
  7928. sprintf(port_name, "port%d", port);
  7929. err = sysfs_create_link(&p->plat_dev->dev.kobj,
  7930. &np->device->kobj,
  7931. port_name);
  7932. if (!err) {
  7933. p->ports[port] = np;
  7934. atomic_inc(&p->refcnt);
  7935. }
  7936. }
  7937. mutex_unlock(&niu_parent_lock);
  7938. return p;
  7939. }
  7940. static void niu_put_parent(struct niu *np)
  7941. {
  7942. struct niu_parent *p = np->parent;
  7943. u8 port = np->port;
  7944. char port_name[6];
  7945. BUG_ON(!p || p->ports[port] != np);
  7946. netif_printk(np, probe, KERN_DEBUG, np->dev,
  7947. "%s() port[%u]\n", __func__, port);
  7948. sprintf(port_name, "port%d", port);
  7949. mutex_lock(&niu_parent_lock);
  7950. sysfs_remove_link(&p->plat_dev->dev.kobj, port_name);
  7951. p->ports[port] = NULL;
  7952. np->parent = NULL;
  7953. if (atomic_dec_and_test(&p->refcnt)) {
  7954. list_del(&p->list);
  7955. platform_device_unregister(p->plat_dev);
  7956. }
  7957. mutex_unlock(&niu_parent_lock);
  7958. }
  7959. static void *niu_pci_alloc_coherent(struct device *dev, size_t size,
  7960. u64 *handle, gfp_t flag)
  7961. {
  7962. dma_addr_t dh;
  7963. void *ret;
  7964. ret = dma_alloc_coherent(dev, size, &dh, flag);
  7965. if (ret)
  7966. *handle = dh;
  7967. return ret;
  7968. }
  7969. static void niu_pci_free_coherent(struct device *dev, size_t size,
  7970. void *cpu_addr, u64 handle)
  7971. {
  7972. dma_free_coherent(dev, size, cpu_addr, handle);
  7973. }
  7974. static u64 niu_pci_map_page(struct device *dev, struct page *page,
  7975. unsigned long offset, size_t size,
  7976. enum dma_data_direction direction)
  7977. {
  7978. return dma_map_page(dev, page, offset, size, direction);
  7979. }
  7980. static void niu_pci_unmap_page(struct device *dev, u64 dma_address,
  7981. size_t size, enum dma_data_direction direction)
  7982. {
  7983. dma_unmap_page(dev, dma_address, size, direction);
  7984. }
  7985. static u64 niu_pci_map_single(struct device *dev, void *cpu_addr,
  7986. size_t size,
  7987. enum dma_data_direction direction)
  7988. {
  7989. return dma_map_single(dev, cpu_addr, size, direction);
  7990. }
  7991. static void niu_pci_unmap_single(struct device *dev, u64 dma_address,
  7992. size_t size,
  7993. enum dma_data_direction direction)
  7994. {
  7995. dma_unmap_single(dev, dma_address, size, direction);
  7996. }
  7997. static const struct niu_ops niu_pci_ops = {
  7998. .alloc_coherent = niu_pci_alloc_coherent,
  7999. .free_coherent = niu_pci_free_coherent,
  8000. .map_page = niu_pci_map_page,
  8001. .unmap_page = niu_pci_unmap_page,
  8002. .map_single = niu_pci_map_single,
  8003. .unmap_single = niu_pci_unmap_single,
  8004. };
  8005. static void __devinit niu_driver_version(void)
  8006. {
  8007. static int niu_version_printed;
  8008. if (niu_version_printed++ == 0)
  8009. pr_info("%s", version);
  8010. }
  8011. static struct net_device * __devinit niu_alloc_and_init(
  8012. struct device *gen_dev, struct pci_dev *pdev,
  8013. struct platform_device *op, const struct niu_ops *ops,
  8014. u8 port)
  8015. {
  8016. struct net_device *dev;
  8017. struct niu *np;
  8018. dev = alloc_etherdev_mq(sizeof(struct niu), NIU_NUM_TXCHAN);
  8019. if (!dev) {
  8020. dev_err(gen_dev, "Etherdev alloc failed, aborting\n");
  8021. return NULL;
  8022. }
  8023. SET_NETDEV_DEV(dev, gen_dev);
  8024. np = netdev_priv(dev);
  8025. np->dev = dev;
  8026. np->pdev = pdev;
  8027. np->op = op;
  8028. np->device = gen_dev;
  8029. np->ops = ops;
  8030. np->msg_enable = niu_debug;
  8031. spin_lock_init(&np->lock);
  8032. INIT_WORK(&np->reset_task, niu_reset_task);
  8033. np->port = port;
  8034. return dev;
  8035. }
  8036. static const struct net_device_ops niu_netdev_ops = {
  8037. .ndo_open = niu_open,
  8038. .ndo_stop = niu_close,
  8039. .ndo_start_xmit = niu_start_xmit,
  8040. .ndo_get_stats = niu_get_stats,
  8041. .ndo_set_multicast_list = niu_set_rx_mode,
  8042. .ndo_validate_addr = eth_validate_addr,
  8043. .ndo_set_mac_address = niu_set_mac_addr,
  8044. .ndo_do_ioctl = niu_ioctl,
  8045. .ndo_tx_timeout = niu_tx_timeout,
  8046. .ndo_change_mtu = niu_change_mtu,
  8047. };
  8048. static void __devinit niu_assign_netdev_ops(struct net_device *dev)
  8049. {
  8050. dev->netdev_ops = &niu_netdev_ops;
  8051. dev->ethtool_ops = &niu_ethtool_ops;
  8052. dev->watchdog_timeo = NIU_TX_TIMEOUT;
  8053. }
  8054. static void __devinit niu_device_announce(struct niu *np)
  8055. {
  8056. struct net_device *dev = np->dev;
  8057. pr_info("%s: NIU Ethernet %pM\n", dev->name, dev->dev_addr);
  8058. if (np->parent->plat_type == PLAT_TYPE_ATCA_CP3220) {
  8059. pr_info("%s: Port type[%s] mode[%s:%s] XCVR[%s] phy[%s]\n",
  8060. dev->name,
  8061. (np->flags & NIU_FLAGS_XMAC ? "XMAC" : "BMAC"),
  8062. (np->flags & NIU_FLAGS_10G ? "10G" : "1G"),
  8063. (np->flags & NIU_FLAGS_FIBER ? "RGMII FIBER" : "SERDES"),
  8064. (np->mac_xcvr == MAC_XCVR_MII ? "MII" :
  8065. (np->mac_xcvr == MAC_XCVR_PCS ? "PCS" : "XPCS")),
  8066. np->vpd.phy_type);
  8067. } else {
  8068. pr_info("%s: Port type[%s] mode[%s:%s] XCVR[%s] phy[%s]\n",
  8069. dev->name,
  8070. (np->flags & NIU_FLAGS_XMAC ? "XMAC" : "BMAC"),
  8071. (np->flags & NIU_FLAGS_10G ? "10G" : "1G"),
  8072. (np->flags & NIU_FLAGS_FIBER ? "FIBER" :
  8073. (np->flags & NIU_FLAGS_XCVR_SERDES ? "SERDES" :
  8074. "COPPER")),
  8075. (np->mac_xcvr == MAC_XCVR_MII ? "MII" :
  8076. (np->mac_xcvr == MAC_XCVR_PCS ? "PCS" : "XPCS")),
  8077. np->vpd.phy_type);
  8078. }
  8079. }
  8080. static void __devinit niu_set_basic_features(struct net_device *dev)
  8081. {
  8082. dev->hw_features = NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXHASH;
  8083. dev->features |= dev->hw_features | NETIF_F_RXCSUM;
  8084. }
  8085. static int __devinit niu_pci_init_one(struct pci_dev *pdev,
  8086. const struct pci_device_id *ent)
  8087. {
  8088. union niu_parent_id parent_id;
  8089. struct net_device *dev;
  8090. struct niu *np;
  8091. int err, pos;
  8092. u64 dma_mask;
  8093. u16 val16;
  8094. niu_driver_version();
  8095. err = pci_enable_device(pdev);
  8096. if (err) {
  8097. dev_err(&pdev->dev, "Cannot enable PCI device, aborting\n");
  8098. return err;
  8099. }
  8100. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
  8101. !(pci_resource_flags(pdev, 2) & IORESOURCE_MEM)) {
  8102. dev_err(&pdev->dev, "Cannot find proper PCI device base addresses, aborting\n");
  8103. err = -ENODEV;
  8104. goto err_out_disable_pdev;
  8105. }
  8106. err = pci_request_regions(pdev, DRV_MODULE_NAME);
  8107. if (err) {
  8108. dev_err(&pdev->dev, "Cannot obtain PCI resources, aborting\n");
  8109. goto err_out_disable_pdev;
  8110. }
  8111. pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
  8112. if (pos <= 0) {
  8113. dev_err(&pdev->dev, "Cannot find PCI Express capability, aborting\n");
  8114. goto err_out_free_res;
  8115. }
  8116. dev = niu_alloc_and_init(&pdev->dev, pdev, NULL,
  8117. &niu_pci_ops, PCI_FUNC(pdev->devfn));
  8118. if (!dev) {
  8119. err = -ENOMEM;
  8120. goto err_out_free_res;
  8121. }
  8122. np = netdev_priv(dev);
  8123. memset(&parent_id, 0, sizeof(parent_id));
  8124. parent_id.pci.domain = pci_domain_nr(pdev->bus);
  8125. parent_id.pci.bus = pdev->bus->number;
  8126. parent_id.pci.device = PCI_SLOT(pdev->devfn);
  8127. np->parent = niu_get_parent(np, &parent_id,
  8128. PLAT_TYPE_ATLAS);
  8129. if (!np->parent) {
  8130. err = -ENOMEM;
  8131. goto err_out_free_dev;
  8132. }
  8133. pci_read_config_word(pdev, pos + PCI_EXP_DEVCTL, &val16);
  8134. val16 &= ~PCI_EXP_DEVCTL_NOSNOOP_EN;
  8135. val16 |= (PCI_EXP_DEVCTL_CERE |
  8136. PCI_EXP_DEVCTL_NFERE |
  8137. PCI_EXP_DEVCTL_FERE |
  8138. PCI_EXP_DEVCTL_URRE |
  8139. PCI_EXP_DEVCTL_RELAX_EN);
  8140. pci_write_config_word(pdev, pos + PCI_EXP_DEVCTL, val16);
  8141. dma_mask = DMA_BIT_MASK(44);
  8142. err = pci_set_dma_mask(pdev, dma_mask);
  8143. if (!err) {
  8144. dev->features |= NETIF_F_HIGHDMA;
  8145. err = pci_set_consistent_dma_mask(pdev, dma_mask);
  8146. if (err) {
  8147. dev_err(&pdev->dev, "Unable to obtain 44 bit DMA for consistent allocations, aborting\n");
  8148. goto err_out_release_parent;
  8149. }
  8150. }
  8151. if (err || dma_mask == DMA_BIT_MASK(32)) {
  8152. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  8153. if (err) {
  8154. dev_err(&pdev->dev, "No usable DMA configuration, aborting\n");
  8155. goto err_out_release_parent;
  8156. }
  8157. }
  8158. niu_set_basic_features(dev);
  8159. np->regs = pci_ioremap_bar(pdev, 0);
  8160. if (!np->regs) {
  8161. dev_err(&pdev->dev, "Cannot map device registers, aborting\n");
  8162. err = -ENOMEM;
  8163. goto err_out_release_parent;
  8164. }
  8165. pci_set_master(pdev);
  8166. pci_save_state(pdev);
  8167. dev->irq = pdev->irq;
  8168. niu_assign_netdev_ops(dev);
  8169. err = niu_get_invariants(np);
  8170. if (err) {
  8171. if (err != -ENODEV)
  8172. dev_err(&pdev->dev, "Problem fetching invariants of chip, aborting\n");
  8173. goto err_out_iounmap;
  8174. }
  8175. err = register_netdev(dev);
  8176. if (err) {
  8177. dev_err(&pdev->dev, "Cannot register net device, aborting\n");
  8178. goto err_out_iounmap;
  8179. }
  8180. pci_set_drvdata(pdev, dev);
  8181. niu_device_announce(np);
  8182. return 0;
  8183. err_out_iounmap:
  8184. if (np->regs) {
  8185. iounmap(np->regs);
  8186. np->regs = NULL;
  8187. }
  8188. err_out_release_parent:
  8189. niu_put_parent(np);
  8190. err_out_free_dev:
  8191. free_netdev(dev);
  8192. err_out_free_res:
  8193. pci_release_regions(pdev);
  8194. err_out_disable_pdev:
  8195. pci_disable_device(pdev);
  8196. pci_set_drvdata(pdev, NULL);
  8197. return err;
  8198. }
  8199. static void __devexit niu_pci_remove_one(struct pci_dev *pdev)
  8200. {
  8201. struct net_device *dev = pci_get_drvdata(pdev);
  8202. if (dev) {
  8203. struct niu *np = netdev_priv(dev);
  8204. unregister_netdev(dev);
  8205. if (np->regs) {
  8206. iounmap(np->regs);
  8207. np->regs = NULL;
  8208. }
  8209. niu_ldg_free(np);
  8210. niu_put_parent(np);
  8211. free_netdev(dev);
  8212. pci_release_regions(pdev);
  8213. pci_disable_device(pdev);
  8214. pci_set_drvdata(pdev, NULL);
  8215. }
  8216. }
  8217. static int niu_suspend(struct pci_dev *pdev, pm_message_t state)
  8218. {
  8219. struct net_device *dev = pci_get_drvdata(pdev);
  8220. struct niu *np = netdev_priv(dev);
  8221. unsigned long flags;
  8222. if (!netif_running(dev))
  8223. return 0;
  8224. flush_work_sync(&np->reset_task);
  8225. niu_netif_stop(np);
  8226. del_timer_sync(&np->timer);
  8227. spin_lock_irqsave(&np->lock, flags);
  8228. niu_enable_interrupts(np, 0);
  8229. spin_unlock_irqrestore(&np->lock, flags);
  8230. netif_device_detach(dev);
  8231. spin_lock_irqsave(&np->lock, flags);
  8232. niu_stop_hw(np);
  8233. spin_unlock_irqrestore(&np->lock, flags);
  8234. pci_save_state(pdev);
  8235. return 0;
  8236. }
  8237. static int niu_resume(struct pci_dev *pdev)
  8238. {
  8239. struct net_device *dev = pci_get_drvdata(pdev);
  8240. struct niu *np = netdev_priv(dev);
  8241. unsigned long flags;
  8242. int err;
  8243. if (!netif_running(dev))
  8244. return 0;
  8245. pci_restore_state(pdev);
  8246. netif_device_attach(dev);
  8247. spin_lock_irqsave(&np->lock, flags);
  8248. err = niu_init_hw(np);
  8249. if (!err) {
  8250. np->timer.expires = jiffies + HZ;
  8251. add_timer(&np->timer);
  8252. niu_netif_start(np);
  8253. }
  8254. spin_unlock_irqrestore(&np->lock, flags);
  8255. return err;
  8256. }
  8257. static struct pci_driver niu_pci_driver = {
  8258. .name = DRV_MODULE_NAME,
  8259. .id_table = niu_pci_tbl,
  8260. .probe = niu_pci_init_one,
  8261. .remove = __devexit_p(niu_pci_remove_one),
  8262. .suspend = niu_suspend,
  8263. .resume = niu_resume,
  8264. };
  8265. #ifdef CONFIG_SPARC64
  8266. static void *niu_phys_alloc_coherent(struct device *dev, size_t size,
  8267. u64 *dma_addr, gfp_t flag)
  8268. {
  8269. unsigned long order = get_order(size);
  8270. unsigned long page = __get_free_pages(flag, order);
  8271. if (page == 0UL)
  8272. return NULL;
  8273. memset((char *)page, 0, PAGE_SIZE << order);
  8274. *dma_addr = __pa(page);
  8275. return (void *) page;
  8276. }
  8277. static void niu_phys_free_coherent(struct device *dev, size_t size,
  8278. void *cpu_addr, u64 handle)
  8279. {
  8280. unsigned long order = get_order(size);
  8281. free_pages((unsigned long) cpu_addr, order);
  8282. }
  8283. static u64 niu_phys_map_page(struct device *dev, struct page *page,
  8284. unsigned long offset, size_t size,
  8285. enum dma_data_direction direction)
  8286. {
  8287. return page_to_phys(page) + offset;
  8288. }
  8289. static void niu_phys_unmap_page(struct device *dev, u64 dma_address,
  8290. size_t size, enum dma_data_direction direction)
  8291. {
  8292. /* Nothing to do. */
  8293. }
  8294. static u64 niu_phys_map_single(struct device *dev, void *cpu_addr,
  8295. size_t size,
  8296. enum dma_data_direction direction)
  8297. {
  8298. return __pa(cpu_addr);
  8299. }
  8300. static void niu_phys_unmap_single(struct device *dev, u64 dma_address,
  8301. size_t size,
  8302. enum dma_data_direction direction)
  8303. {
  8304. /* Nothing to do. */
  8305. }
  8306. static const struct niu_ops niu_phys_ops = {
  8307. .alloc_coherent = niu_phys_alloc_coherent,
  8308. .free_coherent = niu_phys_free_coherent,
  8309. .map_page = niu_phys_map_page,
  8310. .unmap_page = niu_phys_unmap_page,
  8311. .map_single = niu_phys_map_single,
  8312. .unmap_single = niu_phys_unmap_single,
  8313. };
  8314. static int __devinit niu_of_probe(struct platform_device *op)
  8315. {
  8316. union niu_parent_id parent_id;
  8317. struct net_device *dev;
  8318. struct niu *np;
  8319. const u32 *reg;
  8320. int err;
  8321. niu_driver_version();
  8322. reg = of_get_property(op->dev.of_node, "reg", NULL);
  8323. if (!reg) {
  8324. dev_err(&op->dev, "%s: No 'reg' property, aborting\n",
  8325. op->dev.of_node->full_name);
  8326. return -ENODEV;
  8327. }
  8328. dev = niu_alloc_and_init(&op->dev, NULL, op,
  8329. &niu_phys_ops, reg[0] & 0x1);
  8330. if (!dev) {
  8331. err = -ENOMEM;
  8332. goto err_out;
  8333. }
  8334. np = netdev_priv(dev);
  8335. memset(&parent_id, 0, sizeof(parent_id));
  8336. parent_id.of = of_get_parent(op->dev.of_node);
  8337. np->parent = niu_get_parent(np, &parent_id,
  8338. PLAT_TYPE_NIU);
  8339. if (!np->parent) {
  8340. err = -ENOMEM;
  8341. goto err_out_free_dev;
  8342. }
  8343. niu_set_basic_features(dev);
  8344. np->regs = of_ioremap(&op->resource[1], 0,
  8345. resource_size(&op->resource[1]),
  8346. "niu regs");
  8347. if (!np->regs) {
  8348. dev_err(&op->dev, "Cannot map device registers, aborting\n");
  8349. err = -ENOMEM;
  8350. goto err_out_release_parent;
  8351. }
  8352. np->vir_regs_1 = of_ioremap(&op->resource[2], 0,
  8353. resource_size(&op->resource[2]),
  8354. "niu vregs-1");
  8355. if (!np->vir_regs_1) {
  8356. dev_err(&op->dev, "Cannot map device vir registers 1, aborting\n");
  8357. err = -ENOMEM;
  8358. goto err_out_iounmap;
  8359. }
  8360. np->vir_regs_2 = of_ioremap(&op->resource[3], 0,
  8361. resource_size(&op->resource[3]),
  8362. "niu vregs-2");
  8363. if (!np->vir_regs_2) {
  8364. dev_err(&op->dev, "Cannot map device vir registers 2, aborting\n");
  8365. err = -ENOMEM;
  8366. goto err_out_iounmap;
  8367. }
  8368. niu_assign_netdev_ops(dev);
  8369. err = niu_get_invariants(np);
  8370. if (err) {
  8371. if (err != -ENODEV)
  8372. dev_err(&op->dev, "Problem fetching invariants of chip, aborting\n");
  8373. goto err_out_iounmap;
  8374. }
  8375. err = register_netdev(dev);
  8376. if (err) {
  8377. dev_err(&op->dev, "Cannot register net device, aborting\n");
  8378. goto err_out_iounmap;
  8379. }
  8380. dev_set_drvdata(&op->dev, dev);
  8381. niu_device_announce(np);
  8382. return 0;
  8383. err_out_iounmap:
  8384. if (np->vir_regs_1) {
  8385. of_iounmap(&op->resource[2], np->vir_regs_1,
  8386. resource_size(&op->resource[2]));
  8387. np->vir_regs_1 = NULL;
  8388. }
  8389. if (np->vir_regs_2) {
  8390. of_iounmap(&op->resource[3], np->vir_regs_2,
  8391. resource_size(&op->resource[3]));
  8392. np->vir_regs_2 = NULL;
  8393. }
  8394. if (np->regs) {
  8395. of_iounmap(&op->resource[1], np->regs,
  8396. resource_size(&op->resource[1]));
  8397. np->regs = NULL;
  8398. }
  8399. err_out_release_parent:
  8400. niu_put_parent(np);
  8401. err_out_free_dev:
  8402. free_netdev(dev);
  8403. err_out:
  8404. return err;
  8405. }
  8406. static int __devexit niu_of_remove(struct platform_device *op)
  8407. {
  8408. struct net_device *dev = dev_get_drvdata(&op->dev);
  8409. if (dev) {
  8410. struct niu *np = netdev_priv(dev);
  8411. unregister_netdev(dev);
  8412. if (np->vir_regs_1) {
  8413. of_iounmap(&op->resource[2], np->vir_regs_1,
  8414. resource_size(&op->resource[2]));
  8415. np->vir_regs_1 = NULL;
  8416. }
  8417. if (np->vir_regs_2) {
  8418. of_iounmap(&op->resource[3], np->vir_regs_2,
  8419. resource_size(&op->resource[3]));
  8420. np->vir_regs_2 = NULL;
  8421. }
  8422. if (np->regs) {
  8423. of_iounmap(&op->resource[1], np->regs,
  8424. resource_size(&op->resource[1]));
  8425. np->regs = NULL;
  8426. }
  8427. niu_ldg_free(np);
  8428. niu_put_parent(np);
  8429. free_netdev(dev);
  8430. dev_set_drvdata(&op->dev, NULL);
  8431. }
  8432. return 0;
  8433. }
  8434. static const struct of_device_id niu_match[] = {
  8435. {
  8436. .name = "network",
  8437. .compatible = "SUNW,niusl",
  8438. },
  8439. {},
  8440. };
  8441. MODULE_DEVICE_TABLE(of, niu_match);
  8442. static struct platform_driver niu_of_driver = {
  8443. .driver = {
  8444. .name = "niu",
  8445. .owner = THIS_MODULE,
  8446. .of_match_table = niu_match,
  8447. },
  8448. .probe = niu_of_probe,
  8449. .remove = __devexit_p(niu_of_remove),
  8450. };
  8451. #endif /* CONFIG_SPARC64 */
  8452. static int __init niu_init(void)
  8453. {
  8454. int err = 0;
  8455. BUILD_BUG_ON(PAGE_SIZE < 4 * 1024);
  8456. niu_debug = netif_msg_init(debug, NIU_MSG_DEFAULT);
  8457. #ifdef CONFIG_SPARC64
  8458. err = platform_driver_register(&niu_of_driver);
  8459. #endif
  8460. if (!err) {
  8461. err = pci_register_driver(&niu_pci_driver);
  8462. #ifdef CONFIG_SPARC64
  8463. if (err)
  8464. platform_driver_unregister(&niu_of_driver);
  8465. #endif
  8466. }
  8467. return err;
  8468. }
  8469. static void __exit niu_exit(void)
  8470. {
  8471. pci_unregister_driver(&niu_pci_driver);
  8472. #ifdef CONFIG_SPARC64
  8473. platform_driver_unregister(&niu_of_driver);
  8474. #endif
  8475. }
  8476. module_init(niu_init);
  8477. module_exit(niu_exit);