4965-mac.c 184 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *
  4. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  5. *
  6. * Portions of this file are derived from the ipw3945 project, as well
  7. * as portions of the ieee80211 subsystem header files.
  8. *
  9. * Contact Information:
  10. * Intel Linux Wireless <ilw@linux.intel.com>
  11. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  12. *
  13. *****************************************************************************/
  14. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/pci.h>
  19. #include <linux/slab.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/firmware.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/if_arp.h>
  28. #include <linux/units.h>
  29. #include <net/mac80211.h>
  30. #include <asm/div64.h>
  31. #define DRV_NAME "iwl4965"
  32. #include "common.h"
  33. #include "4965.h"
  34. /******************************************************************************
  35. *
  36. * module boiler plate
  37. *
  38. ******************************************************************************/
  39. /*
  40. * module name, copyright, version, etc.
  41. */
  42. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi 4965 driver for Linux"
  43. #ifdef CONFIG_IWLEGACY_DEBUG
  44. #define VD "d"
  45. #else
  46. #define VD
  47. #endif
  48. #define DRV_VERSION IWLWIFI_VERSION VD
  49. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  50. MODULE_VERSION(DRV_VERSION);
  51. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  52. MODULE_LICENSE("GPL");
  53. MODULE_ALIAS("iwl4965");
  54. void
  55. il4965_check_abort_status(struct il_priv *il, u8 frame_count, u32 status)
  56. {
  57. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  58. IL_ERR("Tx flush command to flush out all frames\n");
  59. if (!test_bit(S_EXIT_PENDING, &il->status))
  60. queue_work(il->workqueue, &il->tx_flush);
  61. }
  62. }
  63. /*
  64. * EEPROM
  65. */
  66. struct il_mod_params il4965_mod_params = {
  67. .restart_fw = 1,
  68. /* the rest are 0 by default */
  69. };
  70. void
  71. il4965_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
  72. {
  73. unsigned long flags;
  74. int i;
  75. spin_lock_irqsave(&rxq->lock, flags);
  76. INIT_LIST_HEAD(&rxq->rx_free);
  77. INIT_LIST_HEAD(&rxq->rx_used);
  78. /* Fill the rx_used queue with _all_ of the Rx buffers */
  79. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  80. /* In the reset function, these buffers may have been allocated
  81. * to an SKB, so we need to unmap and free potential storage */
  82. if (rxq->pool[i].page != NULL) {
  83. dma_unmap_page(&il->pci_dev->dev,
  84. rxq->pool[i].page_dma,
  85. PAGE_SIZE << il->hw_params.rx_page_order,
  86. DMA_FROM_DEVICE);
  87. __il_free_pages(il, rxq->pool[i].page);
  88. rxq->pool[i].page = NULL;
  89. }
  90. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  91. }
  92. for (i = 0; i < RX_QUEUE_SIZE; i++)
  93. rxq->queue[i] = NULL;
  94. /* Set us so that we have processed and used all buffers, but have
  95. * not restocked the Rx queue with fresh buffers */
  96. rxq->read = rxq->write = 0;
  97. rxq->write_actual = 0;
  98. rxq->free_count = 0;
  99. spin_unlock_irqrestore(&rxq->lock, flags);
  100. }
  101. int
  102. il4965_rx_init(struct il_priv *il, struct il_rx_queue *rxq)
  103. {
  104. u32 rb_size;
  105. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  106. u32 rb_timeout = 0;
  107. if (il->cfg->mod_params->amsdu_size_8K)
  108. rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  109. else
  110. rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  111. /* Stop Rx DMA */
  112. il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  113. /* Reset driver's Rx queue write idx */
  114. il_wr(il, FH49_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  115. /* Tell device where to find RBD circular buffer in DRAM */
  116. il_wr(il, FH49_RSCSR_CHNL0_RBDCB_BASE_REG, (u32) (rxq->bd_dma >> 8));
  117. /* Tell device where in DRAM to update its Rx status */
  118. il_wr(il, FH49_RSCSR_CHNL0_STTS_WPTR_REG, rxq->rb_stts_dma >> 4);
  119. /* Enable Rx DMA
  120. * Direct rx interrupts to hosts
  121. * Rx buffer size 4 or 8k
  122. * RB timeout 0x10
  123. * 256 RBDs
  124. */
  125. il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG,
  126. FH49_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  127. FH49_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  128. FH49_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  129. rb_size |
  130. (rb_timeout << FH49_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS) |
  131. (rfdnlog << FH49_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  132. /* Set interrupt coalescing timer to default (2048 usecs) */
  133. il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_TIMEOUT_DEF);
  134. return 0;
  135. }
  136. static void
  137. il4965_set_pwr_vmain(struct il_priv *il)
  138. {
  139. /*
  140. * (for documentation purposes)
  141. * to set power to V_AUX, do:
  142. if (pci_pme_capable(il->pci_dev, PCI_D3cold))
  143. il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
  144. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  145. ~APMG_PS_CTRL_MSK_PWR_SRC);
  146. */
  147. il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
  148. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  149. ~APMG_PS_CTRL_MSK_PWR_SRC);
  150. }
  151. int
  152. il4965_hw_nic_init(struct il_priv *il)
  153. {
  154. unsigned long flags;
  155. struct il_rx_queue *rxq = &il->rxq;
  156. int ret;
  157. spin_lock_irqsave(&il->lock, flags);
  158. il_apm_init(il);
  159. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  160. il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_CALIB_TIMEOUT_DEF);
  161. spin_unlock_irqrestore(&il->lock, flags);
  162. il4965_set_pwr_vmain(il);
  163. il4965_nic_config(il);
  164. /* Allocate the RX queue, or reset if it is already allocated */
  165. if (!rxq->bd) {
  166. ret = il_rx_queue_alloc(il);
  167. if (ret) {
  168. IL_ERR("Unable to initialize Rx queue\n");
  169. return -ENOMEM;
  170. }
  171. } else
  172. il4965_rx_queue_reset(il, rxq);
  173. il4965_rx_replenish(il);
  174. il4965_rx_init(il, rxq);
  175. spin_lock_irqsave(&il->lock, flags);
  176. rxq->need_update = 1;
  177. il_rx_queue_update_write_ptr(il, rxq);
  178. spin_unlock_irqrestore(&il->lock, flags);
  179. /* Allocate or reset and init all Tx and Command queues */
  180. if (!il->txq) {
  181. ret = il4965_txq_ctx_alloc(il);
  182. if (ret)
  183. return ret;
  184. } else
  185. il4965_txq_ctx_reset(il);
  186. set_bit(S_INIT, &il->status);
  187. return 0;
  188. }
  189. /*
  190. * il4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  191. */
  192. static inline __le32
  193. il4965_dma_addr2rbd_ptr(struct il_priv *il, dma_addr_t dma_addr)
  194. {
  195. return cpu_to_le32((u32) (dma_addr >> 8));
  196. }
  197. /*
  198. * il4965_rx_queue_restock - refill RX queue from pre-allocated pool
  199. *
  200. * If there are slots in the RX queue that need to be restocked,
  201. * and we have free pre-allocated buffers, fill the ranks as much
  202. * as we can, pulling from rx_free.
  203. *
  204. * This moves the 'write' idx forward to catch up with 'processed', and
  205. * also updates the memory address in the firmware to reference the new
  206. * target buffer.
  207. */
  208. void
  209. il4965_rx_queue_restock(struct il_priv *il)
  210. {
  211. struct il_rx_queue *rxq = &il->rxq;
  212. struct list_head *element;
  213. struct il_rx_buf *rxb;
  214. unsigned long flags;
  215. spin_lock_irqsave(&rxq->lock, flags);
  216. while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
  217. /* The overwritten rxb must be a used one */
  218. rxb = rxq->queue[rxq->write];
  219. BUG_ON(rxb && rxb->page);
  220. /* Get next free Rx buffer, remove from free list */
  221. element = rxq->rx_free.next;
  222. rxb = list_entry(element, struct il_rx_buf, list);
  223. list_del(element);
  224. /* Point to Rx buffer via next RBD in circular buffer */
  225. rxq->bd[rxq->write] =
  226. il4965_dma_addr2rbd_ptr(il, rxb->page_dma);
  227. rxq->queue[rxq->write] = rxb;
  228. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  229. rxq->free_count--;
  230. }
  231. spin_unlock_irqrestore(&rxq->lock, flags);
  232. /* If the pre-allocated buffer pool is dropping low, schedule to
  233. * refill it */
  234. if (rxq->free_count <= RX_LOW_WATERMARK)
  235. queue_work(il->workqueue, &il->rx_replenish);
  236. /* If we've added more space for the firmware to place data, tell it.
  237. * Increment device's write pointer in multiples of 8. */
  238. if (rxq->write_actual != (rxq->write & ~0x7)) {
  239. spin_lock_irqsave(&rxq->lock, flags);
  240. rxq->need_update = 1;
  241. spin_unlock_irqrestore(&rxq->lock, flags);
  242. il_rx_queue_update_write_ptr(il, rxq);
  243. }
  244. }
  245. /*
  246. * il4965_rx_replenish - Move all used packet from rx_used to rx_free
  247. *
  248. * When moving to rx_free an SKB is allocated for the slot.
  249. *
  250. * Also restock the Rx queue via il_rx_queue_restock.
  251. * This is called as a scheduled work item (except for during initialization)
  252. */
  253. static void
  254. il4965_rx_allocate(struct il_priv *il, gfp_t priority)
  255. {
  256. struct il_rx_queue *rxq = &il->rxq;
  257. struct list_head *element;
  258. struct il_rx_buf *rxb;
  259. struct page *page;
  260. dma_addr_t page_dma;
  261. unsigned long flags;
  262. gfp_t gfp_mask = priority;
  263. while (1) {
  264. spin_lock_irqsave(&rxq->lock, flags);
  265. if (list_empty(&rxq->rx_used)) {
  266. spin_unlock_irqrestore(&rxq->lock, flags);
  267. return;
  268. }
  269. spin_unlock_irqrestore(&rxq->lock, flags);
  270. if (rxq->free_count > RX_LOW_WATERMARK)
  271. gfp_mask |= __GFP_NOWARN;
  272. if (il->hw_params.rx_page_order > 0)
  273. gfp_mask |= __GFP_COMP;
  274. /* Alloc a new receive buffer */
  275. page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
  276. if (!page) {
  277. if (net_ratelimit())
  278. D_INFO("alloc_pages failed, " "order: %d\n",
  279. il->hw_params.rx_page_order);
  280. if (rxq->free_count <= RX_LOW_WATERMARK &&
  281. net_ratelimit())
  282. IL_ERR("Failed to alloc_pages with %s. "
  283. "Only %u free buffers remaining.\n",
  284. priority ==
  285. GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  286. rxq->free_count);
  287. /* We don't reschedule replenish work here -- we will
  288. * call the restock method and if it still needs
  289. * more buffers it will schedule replenish */
  290. return;
  291. }
  292. /* Get physical address of the RB */
  293. page_dma = dma_map_page(&il->pci_dev->dev, page, 0,
  294. PAGE_SIZE << il->hw_params.rx_page_order,
  295. DMA_FROM_DEVICE);
  296. if (unlikely(dma_mapping_error(&il->pci_dev->dev, page_dma))) {
  297. __free_pages(page, il->hw_params.rx_page_order);
  298. break;
  299. }
  300. spin_lock_irqsave(&rxq->lock, flags);
  301. if (list_empty(&rxq->rx_used)) {
  302. spin_unlock_irqrestore(&rxq->lock, flags);
  303. dma_unmap_page(&il->pci_dev->dev, page_dma,
  304. PAGE_SIZE << il->hw_params.rx_page_order,
  305. DMA_FROM_DEVICE);
  306. __free_pages(page, il->hw_params.rx_page_order);
  307. return;
  308. }
  309. element = rxq->rx_used.next;
  310. rxb = list_entry(element, struct il_rx_buf, list);
  311. list_del(element);
  312. BUG_ON(rxb->page);
  313. rxb->page = page;
  314. rxb->page_dma = page_dma;
  315. list_add_tail(&rxb->list, &rxq->rx_free);
  316. rxq->free_count++;
  317. il->alloc_rxb_page++;
  318. spin_unlock_irqrestore(&rxq->lock, flags);
  319. }
  320. }
  321. void
  322. il4965_rx_replenish(struct il_priv *il)
  323. {
  324. unsigned long flags;
  325. il4965_rx_allocate(il, GFP_KERNEL);
  326. spin_lock_irqsave(&il->lock, flags);
  327. il4965_rx_queue_restock(il);
  328. spin_unlock_irqrestore(&il->lock, flags);
  329. }
  330. void
  331. il4965_rx_replenish_now(struct il_priv *il)
  332. {
  333. il4965_rx_allocate(il, GFP_ATOMIC);
  334. il4965_rx_queue_restock(il);
  335. }
  336. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  337. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  338. * This free routine walks the list of POOL entries and if SKB is set to
  339. * non NULL it is unmapped and freed
  340. */
  341. void
  342. il4965_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
  343. {
  344. int i;
  345. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  346. if (rxq->pool[i].page != NULL) {
  347. dma_unmap_page(&il->pci_dev->dev,
  348. rxq->pool[i].page_dma,
  349. PAGE_SIZE << il->hw_params.rx_page_order,
  350. DMA_FROM_DEVICE);
  351. __il_free_pages(il, rxq->pool[i].page);
  352. rxq->pool[i].page = NULL;
  353. }
  354. }
  355. dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  356. rxq->bd_dma);
  357. dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
  358. rxq->rb_stts, rxq->rb_stts_dma);
  359. rxq->bd = NULL;
  360. rxq->rb_stts = NULL;
  361. }
  362. int
  363. il4965_rxq_stop(struct il_priv *il)
  364. {
  365. int ret;
  366. _il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  367. ret = _il_poll_bit(il, FH49_MEM_RSSR_RX_STATUS_REG,
  368. FH49_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
  369. FH49_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
  370. 1000);
  371. if (ret < 0)
  372. IL_ERR("Can't stop Rx DMA.\n");
  373. return 0;
  374. }
  375. int
  376. il4965_hwrate_to_mac80211_idx(u32 rate_n_flags, enum nl80211_band band)
  377. {
  378. int idx = 0;
  379. int band_offset = 0;
  380. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  381. if (rate_n_flags & RATE_MCS_HT_MSK) {
  382. idx = (rate_n_flags & 0xff);
  383. return idx;
  384. /* Legacy rate format, search for match in table */
  385. } else {
  386. if (band == NL80211_BAND_5GHZ)
  387. band_offset = IL_FIRST_OFDM_RATE;
  388. for (idx = band_offset; idx < RATE_COUNT_LEGACY; idx++)
  389. if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
  390. return idx - band_offset;
  391. }
  392. return -1;
  393. }
  394. static int
  395. il4965_calc_rssi(struct il_priv *il, struct il_rx_phy_res *rx_resp)
  396. {
  397. /* data from PHY/DSP regarding signal strength, etc.,
  398. * contents are always there, not configurable by host. */
  399. struct il4965_rx_non_cfg_phy *ncphy =
  400. (struct il4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
  401. u32 agc =
  402. (le16_to_cpu(ncphy->agc_info) & IL49_AGC_DB_MASK) >>
  403. IL49_AGC_DB_POS;
  404. u32 valid_antennae =
  405. (le16_to_cpu(rx_resp->phy_flags) & IL49_RX_PHY_FLAGS_ANTENNAE_MASK)
  406. >> IL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
  407. u8 max_rssi = 0;
  408. u32 i;
  409. /* Find max rssi among 3 possible receivers.
  410. * These values are measured by the digital signal processor (DSP).
  411. * They should stay fairly constant even as the signal strength varies,
  412. * if the radio's automatic gain control (AGC) is working right.
  413. * AGC value (see below) will provide the "interesting" info. */
  414. for (i = 0; i < 3; i++)
  415. if (valid_antennae & (1 << i))
  416. max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
  417. D_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  418. ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
  419. max_rssi, agc);
  420. /* dBm = max_rssi dB - agc dB - constant.
  421. * Higher AGC (higher radio gain) means lower signal. */
  422. return max_rssi - agc - IL4965_RSSI_OFFSET;
  423. }
  424. static u32
  425. il4965_translate_rx_status(struct il_priv *il, u32 decrypt_in)
  426. {
  427. u32 decrypt_out = 0;
  428. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  429. RX_RES_STATUS_STATION_FOUND)
  430. decrypt_out |=
  431. (RX_RES_STATUS_STATION_FOUND |
  432. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  433. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  434. /* packet was not encrypted */
  435. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  436. RX_RES_STATUS_SEC_TYPE_NONE)
  437. return decrypt_out;
  438. /* packet was encrypted with unknown alg */
  439. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  440. RX_RES_STATUS_SEC_TYPE_ERR)
  441. return decrypt_out;
  442. /* decryption was not done in HW */
  443. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  444. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  445. return decrypt_out;
  446. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  447. case RX_RES_STATUS_SEC_TYPE_CCMP:
  448. /* alg is CCM: check MIC only */
  449. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  450. /* Bad MIC */
  451. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  452. else
  453. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  454. break;
  455. case RX_RES_STATUS_SEC_TYPE_TKIP:
  456. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  457. /* Bad TTAK */
  458. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  459. break;
  460. }
  461. fallthrough; /* if TTAK OK */
  462. default:
  463. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  464. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  465. else
  466. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  467. break;
  468. }
  469. D_RX("decrypt_in:0x%x decrypt_out = 0x%x\n", decrypt_in, decrypt_out);
  470. return decrypt_out;
  471. }
  472. #define SMALL_PACKET_SIZE 256
  473. static void
  474. il4965_pass_packet_to_mac80211(struct il_priv *il, struct ieee80211_hdr *hdr,
  475. u32 len, u32 ampdu_status, struct il_rx_buf *rxb,
  476. struct ieee80211_rx_status *stats)
  477. {
  478. struct sk_buff *skb;
  479. __le16 fc = hdr->frame_control;
  480. /* We only process data packets if the interface is open */
  481. if (unlikely(!il->is_open)) {
  482. D_DROP("Dropping packet while interface is not open.\n");
  483. return;
  484. }
  485. if (unlikely(test_bit(IL_STOP_REASON_PASSIVE, &il->stop_reason))) {
  486. il_wake_queues_by_reason(il, IL_STOP_REASON_PASSIVE);
  487. D_INFO("Woke queues - frame received on passive channel\n");
  488. }
  489. /* In case of HW accelerated crypto and bad decryption, drop */
  490. if (!il->cfg->mod_params->sw_crypto &&
  491. il_set_decrypted_flag(il, hdr, ampdu_status, stats))
  492. return;
  493. skb = dev_alloc_skb(SMALL_PACKET_SIZE);
  494. if (!skb) {
  495. IL_ERR("dev_alloc_skb failed\n");
  496. return;
  497. }
  498. if (len <= SMALL_PACKET_SIZE) {
  499. skb_put_data(skb, hdr, len);
  500. } else {
  501. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb),
  502. len, PAGE_SIZE << il->hw_params.rx_page_order);
  503. il->alloc_rxb_page--;
  504. rxb->page = NULL;
  505. }
  506. il_update_stats(il, false, fc, len);
  507. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  508. ieee80211_rx(il->hw, skb);
  509. }
  510. /* Called for N_RX (legacy ABG frames), or
  511. * N_RX_MPDU (HT high-throughput N frames). */
  512. static void
  513. il4965_hdl_rx(struct il_priv *il, struct il_rx_buf *rxb)
  514. {
  515. struct ieee80211_hdr *header;
  516. struct ieee80211_rx_status rx_status = {};
  517. struct il_rx_pkt *pkt = rxb_addr(rxb);
  518. struct il_rx_phy_res *phy_res;
  519. __le32 rx_pkt_status;
  520. struct il_rx_mpdu_res_start *amsdu;
  521. u32 len;
  522. u32 ampdu_status;
  523. u32 rate_n_flags;
  524. /**
  525. * N_RX and N_RX_MPDU are handled differently.
  526. * N_RX: physical layer info is in this buffer
  527. * N_RX_MPDU: physical layer info was sent in separate
  528. * command and cached in il->last_phy_res
  529. *
  530. * Here we set up local variables depending on which command is
  531. * received.
  532. */
  533. if (pkt->hdr.cmd == N_RX) {
  534. phy_res = (struct il_rx_phy_res *)pkt->u.raw;
  535. header =
  536. (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res) +
  537. phy_res->cfg_phy_cnt);
  538. len = le16_to_cpu(phy_res->byte_count);
  539. rx_pkt_status =
  540. *(__le32 *) (pkt->u.raw + sizeof(*phy_res) +
  541. phy_res->cfg_phy_cnt + len);
  542. ampdu_status = le32_to_cpu(rx_pkt_status);
  543. } else {
  544. if (!il->_4965.last_phy_res_valid) {
  545. IL_ERR("MPDU frame without cached PHY data\n");
  546. return;
  547. }
  548. phy_res = &il->_4965.last_phy_res;
  549. amsdu = (struct il_rx_mpdu_res_start *)pkt->u.raw;
  550. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  551. len = le16_to_cpu(amsdu->byte_count);
  552. rx_pkt_status = *(__le32 *) (pkt->u.raw + sizeof(*amsdu) + len);
  553. ampdu_status =
  554. il4965_translate_rx_status(il, le32_to_cpu(rx_pkt_status));
  555. }
  556. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  557. D_DROP("dsp size out of range [0,20]: %d\n",
  558. phy_res->cfg_phy_cnt);
  559. return;
  560. }
  561. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  562. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  563. D_RX("Bad CRC or FIFO: 0x%08X.\n", le32_to_cpu(rx_pkt_status));
  564. return;
  565. }
  566. /* This will be used in several places later */
  567. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  568. /* rx_status carries information about the packet to mac80211 */
  569. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  570. rx_status.band =
  571. (phy_res->
  572. phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ? NL80211_BAND_2GHZ :
  573. NL80211_BAND_5GHZ;
  574. rx_status.freq =
  575. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
  576. rx_status.band);
  577. rx_status.rate_idx =
  578. il4965_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  579. rx_status.flag = 0;
  580. /* TSF isn't reliable. In order to allow smooth user experience,
  581. * this W/A doesn't propagate it to the mac80211 */
  582. /*rx_status.flag |= RX_FLAG_MACTIME_START; */
  583. il->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  584. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  585. rx_status.signal = il4965_calc_rssi(il, phy_res);
  586. D_STATS("Rssi %d, TSF %llu\n", rx_status.signal,
  587. (unsigned long long)rx_status.mactime);
  588. /*
  589. * "antenna number"
  590. *
  591. * It seems that the antenna field in the phy flags value
  592. * is actually a bit field. This is undefined by radiotap,
  593. * it wants an actual antenna number but I always get "7"
  594. * for most legacy frames I receive indicating that the
  595. * same frame was received on all three RX chains.
  596. *
  597. * I think this field should be removed in favor of a
  598. * new 802.11n radiotap field "RX chains" that is defined
  599. * as a bitmask.
  600. */
  601. rx_status.antenna =
  602. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK) >>
  603. RX_RES_PHY_FLAGS_ANTENNA_POS;
  604. /* set the preamble flag if appropriate */
  605. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  606. rx_status.enc_flags |= RX_ENC_FLAG_SHORTPRE;
  607. /* Set up the HT phy flags */
  608. if (rate_n_flags & RATE_MCS_HT_MSK)
  609. rx_status.encoding = RX_ENC_HT;
  610. if (rate_n_flags & RATE_MCS_HT40_MSK)
  611. rx_status.bw = RATE_INFO_BW_40;
  612. else
  613. rx_status.bw = RATE_INFO_BW_20;
  614. if (rate_n_flags & RATE_MCS_SGI_MSK)
  615. rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
  616. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_AGG_MSK) {
  617. /* We know which subframes of an A-MPDU belong
  618. * together since we get a single PHY response
  619. * from the firmware for all of them.
  620. */
  621. rx_status.flag |= RX_FLAG_AMPDU_DETAILS;
  622. rx_status.ampdu_reference = il->_4965.ampdu_ref;
  623. }
  624. il4965_pass_packet_to_mac80211(il, header, len, ampdu_status, rxb,
  625. &rx_status);
  626. }
  627. /* Cache phy data (Rx signal strength, etc) for HT frame (N_RX_PHY).
  628. * This will be used later in il_hdl_rx() for N_RX_MPDU. */
  629. static void
  630. il4965_hdl_rx_phy(struct il_priv *il, struct il_rx_buf *rxb)
  631. {
  632. struct il_rx_pkt *pkt = rxb_addr(rxb);
  633. il->_4965.last_phy_res_valid = true;
  634. il->_4965.ampdu_ref++;
  635. memcpy(&il->_4965.last_phy_res, pkt->u.raw,
  636. sizeof(struct il_rx_phy_res));
  637. }
  638. static int
  639. il4965_get_channels_for_scan(struct il_priv *il, struct ieee80211_vif *vif,
  640. enum nl80211_band band, u8 is_active,
  641. u8 n_probes, struct il_scan_channel *scan_ch)
  642. {
  643. struct ieee80211_channel *chan;
  644. const struct ieee80211_supported_band *sband;
  645. const struct il_channel_info *ch_info;
  646. u16 passive_dwell = 0;
  647. u16 active_dwell = 0;
  648. int added, i;
  649. u16 channel;
  650. sband = il_get_hw_mode(il, band);
  651. if (!sband)
  652. return 0;
  653. active_dwell = il_get_active_dwell_time(il, band, n_probes);
  654. passive_dwell = il_get_passive_dwell_time(il, band, vif);
  655. if (passive_dwell <= active_dwell)
  656. passive_dwell = active_dwell + 1;
  657. for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
  658. chan = il->scan_request->channels[i];
  659. if (chan->band != band)
  660. continue;
  661. channel = chan->hw_value;
  662. scan_ch->channel = cpu_to_le16(channel);
  663. ch_info = il_get_channel_info(il, band, channel);
  664. if (!il_is_channel_valid(ch_info)) {
  665. D_SCAN("Channel %d is INVALID for this band.\n",
  666. channel);
  667. continue;
  668. }
  669. if (!is_active || il_is_channel_passive(ch_info) ||
  670. (chan->flags & IEEE80211_CHAN_NO_IR))
  671. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  672. else
  673. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  674. if (n_probes)
  675. scan_ch->type |= IL_SCAN_PROBE_MASK(n_probes);
  676. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  677. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  678. /* Set txpower levels to defaults */
  679. scan_ch->dsp_atten = 110;
  680. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  681. * power level:
  682. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  683. */
  684. if (band == NL80211_BAND_5GHZ)
  685. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  686. else
  687. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  688. D_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n", channel,
  689. le32_to_cpu(scan_ch->type),
  690. (scan_ch->
  691. type & SCAN_CHANNEL_TYPE_ACTIVE) ? "ACTIVE" : "PASSIVE",
  692. (scan_ch->
  693. type & SCAN_CHANNEL_TYPE_ACTIVE) ? active_dwell :
  694. passive_dwell);
  695. scan_ch++;
  696. added++;
  697. }
  698. D_SCAN("total channels to scan %d\n", added);
  699. return added;
  700. }
  701. static void
  702. il4965_toggle_tx_ant(struct il_priv *il, u8 *ant, u8 valid)
  703. {
  704. int i;
  705. u8 ind = *ant;
  706. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  707. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  708. if (valid & BIT(ind)) {
  709. *ant = ind;
  710. return;
  711. }
  712. }
  713. }
  714. int
  715. il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
  716. {
  717. struct il_host_cmd cmd = {
  718. .id = C_SCAN,
  719. .len = sizeof(struct il_scan_cmd),
  720. .flags = CMD_SIZE_HUGE,
  721. };
  722. struct il_scan_cmd *scan;
  723. u32 rate_flags = 0;
  724. u16 cmd_len;
  725. u16 rx_chain = 0;
  726. enum nl80211_band band;
  727. u8 n_probes = 0;
  728. u8 rx_ant = il->hw_params.valid_rx_ant;
  729. u8 rate;
  730. bool is_active = false;
  731. int chan_mod;
  732. u8 active_chains;
  733. u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
  734. int ret;
  735. lockdep_assert_held(&il->mutex);
  736. if (!il->scan_cmd) {
  737. il->scan_cmd =
  738. kmalloc(sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE,
  739. GFP_KERNEL);
  740. if (!il->scan_cmd) {
  741. D_SCAN("fail to allocate memory for scan\n");
  742. return -ENOMEM;
  743. }
  744. }
  745. scan = il->scan_cmd;
  746. memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
  747. scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
  748. scan->quiet_time = IL_ACTIVE_QUIET_TIME;
  749. if (il_is_any_associated(il)) {
  750. u16 interval;
  751. u32 extra;
  752. u32 suspend_time = 100;
  753. u32 scan_suspend_time = 100;
  754. D_INFO("Scanning while associated...\n");
  755. interval = vif->bss_conf.beacon_int;
  756. scan->suspend_time = 0;
  757. scan->max_out_time = cpu_to_le32(200 * 1024);
  758. if (!interval)
  759. interval = suspend_time;
  760. extra = (suspend_time / interval) << 22;
  761. scan_suspend_time =
  762. (extra | ((suspend_time % interval) * 1024));
  763. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  764. D_SCAN("suspend_time 0x%X beacon interval %d\n",
  765. scan_suspend_time, interval);
  766. }
  767. if (il->scan_request->n_ssids) {
  768. int i, p = 0;
  769. D_SCAN("Kicking off active scan\n");
  770. for (i = 0; i < il->scan_request->n_ssids; i++) {
  771. /* always does wildcard anyway */
  772. if (!il->scan_request->ssids[i].ssid_len)
  773. continue;
  774. scan->direct_scan[p].id = WLAN_EID_SSID;
  775. scan->direct_scan[p].len =
  776. il->scan_request->ssids[i].ssid_len;
  777. memcpy(scan->direct_scan[p].ssid,
  778. il->scan_request->ssids[i].ssid,
  779. il->scan_request->ssids[i].ssid_len);
  780. n_probes++;
  781. p++;
  782. }
  783. is_active = true;
  784. } else
  785. D_SCAN("Start passive scan.\n");
  786. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  787. scan->tx_cmd.sta_id = il->hw_params.bcast_id;
  788. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  789. switch (il->scan_band) {
  790. case NL80211_BAND_2GHZ:
  791. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  792. chan_mod =
  793. le32_to_cpu(il->active.flags & RXON_FLG_CHANNEL_MODE_MSK) >>
  794. RXON_FLG_CHANNEL_MODE_POS;
  795. if (chan_mod == CHANNEL_MODE_PURE_40) {
  796. rate = RATE_6M_PLCP;
  797. } else {
  798. rate = RATE_1M_PLCP;
  799. rate_flags = RATE_MCS_CCK_MSK;
  800. }
  801. break;
  802. case NL80211_BAND_5GHZ:
  803. rate = RATE_6M_PLCP;
  804. break;
  805. default:
  806. IL_WARN("Invalid scan band\n");
  807. return -EIO;
  808. }
  809. /*
  810. * If active scanning is requested but a certain channel is
  811. * marked passive, we can do active scanning if we detect
  812. * transmissions.
  813. *
  814. * There is an issue with some firmware versions that triggers
  815. * a sysassert on a "good CRC threshold" of zero (== disabled),
  816. * on a radar channel even though this means that we should NOT
  817. * send probes.
  818. *
  819. * The "good CRC threshold" is the number of frames that we
  820. * need to receive during our dwell time on a channel before
  821. * sending out probes -- setting this to a huge value will
  822. * mean we never reach it, but at the same time work around
  823. * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
  824. * here instead of IL_GOOD_CRC_TH_DISABLED.
  825. */
  826. scan->good_CRC_th =
  827. is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
  828. band = il->scan_band;
  829. if (il->cfg->scan_rx_antennas[band])
  830. rx_ant = il->cfg->scan_rx_antennas[band];
  831. il4965_toggle_tx_ant(il, &il->scan_tx_ant[band], scan_tx_antennas);
  832. rate_flags |= BIT(il->scan_tx_ant[band]) << RATE_MCS_ANT_POS;
  833. scan->tx_cmd.rate_n_flags = cpu_to_le32(rate | rate_flags);
  834. /* In power save mode use one chain, otherwise use all chains */
  835. if (test_bit(S_POWER_PMI, &il->status)) {
  836. /* rx_ant has been set to all valid chains previously */
  837. active_chains =
  838. rx_ant & ((u8) (il->chain_noise_data.active_chains));
  839. if (!active_chains)
  840. active_chains = rx_ant;
  841. D_SCAN("chain_noise_data.active_chains: %u\n",
  842. il->chain_noise_data.active_chains);
  843. rx_ant = il4965_first_antenna(active_chains);
  844. }
  845. /* MIMO is not used here, but value is required */
  846. rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  847. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  848. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  849. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  850. scan->rx_chain = cpu_to_le16(rx_chain);
  851. cmd_len =
  852. il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
  853. vif->addr, il->scan_request->ie,
  854. il->scan_request->ie_len,
  855. IL_MAX_SCAN_SIZE - sizeof(*scan));
  856. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  857. scan->filter_flags |=
  858. (RXON_FILTER_ACCEPT_GRP_MSK | RXON_FILTER_BCON_AWARE_MSK);
  859. scan->channel_count =
  860. il4965_get_channels_for_scan(il, vif, band, is_active, n_probes,
  861. (void *)&scan->data[cmd_len]);
  862. if (scan->channel_count == 0) {
  863. D_SCAN("channel count %d\n", scan->channel_count);
  864. return -EIO;
  865. }
  866. cmd.len +=
  867. le16_to_cpu(scan->tx_cmd.len) +
  868. scan->channel_count * sizeof(struct il_scan_channel);
  869. cmd.data = scan;
  870. scan->len = cpu_to_le16(cmd.len);
  871. set_bit(S_SCAN_HW, &il->status);
  872. ret = il_send_cmd_sync(il, &cmd);
  873. if (ret)
  874. clear_bit(S_SCAN_HW, &il->status);
  875. return ret;
  876. }
  877. int
  878. il4965_manage_ibss_station(struct il_priv *il, struct ieee80211_vif *vif,
  879. bool add)
  880. {
  881. struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
  882. if (add)
  883. return il4965_add_bssid_station(il, vif->bss_conf.bssid,
  884. &vif_priv->ibss_bssid_sta_id);
  885. return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
  886. vif->bss_conf.bssid);
  887. }
  888. void
  889. il4965_free_tfds_in_queue(struct il_priv *il, int sta_id, int tid, int freed)
  890. {
  891. lockdep_assert_held(&il->sta_lock);
  892. if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  893. il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  894. else {
  895. D_TX("free more than tfds_in_queue (%u:%d)\n",
  896. il->stations[sta_id].tid[tid].tfds_in_queue, freed);
  897. il->stations[sta_id].tid[tid].tfds_in_queue = 0;
  898. }
  899. }
  900. #define IL_TX_QUEUE_MSK 0xfffff
  901. static bool
  902. il4965_is_single_rx_stream(struct il_priv *il)
  903. {
  904. return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
  905. il->current_ht_config.single_chain_sufficient;
  906. }
  907. #define IL_NUM_RX_CHAINS_MULTIPLE 3
  908. #define IL_NUM_RX_CHAINS_SINGLE 2
  909. #define IL_NUM_IDLE_CHAINS_DUAL 2
  910. #define IL_NUM_IDLE_CHAINS_SINGLE 1
  911. /*
  912. * Determine how many receiver/antenna chains to use.
  913. *
  914. * More provides better reception via diversity. Fewer saves power
  915. * at the expense of throughput, but only when not in powersave to
  916. * start with.
  917. *
  918. * MIMO (dual stream) requires at least 2, but works better with 3.
  919. * This does not determine *which* chains to use, just how many.
  920. */
  921. static int
  922. il4965_get_active_rx_chain_count(struct il_priv *il)
  923. {
  924. /* # of Rx chains to use when expecting MIMO. */
  925. if (il4965_is_single_rx_stream(il))
  926. return IL_NUM_RX_CHAINS_SINGLE;
  927. else
  928. return IL_NUM_RX_CHAINS_MULTIPLE;
  929. }
  930. /*
  931. * When we are in power saving mode, unless device support spatial
  932. * multiplexing power save, use the active count for rx chain count.
  933. */
  934. static int
  935. il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
  936. {
  937. /* # Rx chains when idling, depending on SMPS mode */
  938. switch (il->current_ht_config.smps) {
  939. case IEEE80211_SMPS_STATIC:
  940. case IEEE80211_SMPS_DYNAMIC:
  941. return IL_NUM_IDLE_CHAINS_SINGLE;
  942. case IEEE80211_SMPS_OFF:
  943. return active_cnt;
  944. default:
  945. WARN(1, "invalid SMPS mode %d", il->current_ht_config.smps);
  946. return active_cnt;
  947. }
  948. }
  949. /* up to 4 chains */
  950. static u8
  951. il4965_count_chain_bitmap(u32 chain_bitmap)
  952. {
  953. u8 res;
  954. res = (chain_bitmap & BIT(0)) >> 0;
  955. res += (chain_bitmap & BIT(1)) >> 1;
  956. res += (chain_bitmap & BIT(2)) >> 2;
  957. res += (chain_bitmap & BIT(3)) >> 3;
  958. return res;
  959. }
  960. /*
  961. * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  962. *
  963. * Selects how many and which Rx receivers/antennas/chains to use.
  964. * This should not be used for scan command ... it puts data in wrong place.
  965. */
  966. void
  967. il4965_set_rxon_chain(struct il_priv *il)
  968. {
  969. bool is_single = il4965_is_single_rx_stream(il);
  970. bool is_cam = !test_bit(S_POWER_PMI, &il->status);
  971. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  972. u32 active_chains;
  973. u16 rx_chain;
  974. /* Tell uCode which antennas are actually connected.
  975. * Before first association, we assume all antennas are connected.
  976. * Just after first association, il4965_chain_noise_calibration()
  977. * checks which antennas actually *are* connected. */
  978. if (il->chain_noise_data.active_chains)
  979. active_chains = il->chain_noise_data.active_chains;
  980. else
  981. active_chains = il->hw_params.valid_rx_ant;
  982. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  983. /* How many receivers should we use? */
  984. active_rx_cnt = il4965_get_active_rx_chain_count(il);
  985. idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
  986. /* correct rx chain count according hw settings
  987. * and chain noise calibration
  988. */
  989. valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
  990. if (valid_rx_cnt < active_rx_cnt)
  991. active_rx_cnt = valid_rx_cnt;
  992. if (valid_rx_cnt < idle_rx_cnt)
  993. idle_rx_cnt = valid_rx_cnt;
  994. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  995. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  996. il->staging.rx_chain = cpu_to_le16(rx_chain);
  997. if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
  998. il->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  999. else
  1000. il->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1001. D_ASSOC("rx_chain=0x%X active=%d idle=%d\n", il->staging.rx_chain,
  1002. active_rx_cnt, idle_rx_cnt);
  1003. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  1004. active_rx_cnt < idle_rx_cnt);
  1005. }
  1006. static const char *
  1007. il4965_get_fh_string(int cmd)
  1008. {
  1009. switch (cmd) {
  1010. IL_CMD(FH49_RSCSR_CHNL0_STTS_WPTR_REG);
  1011. IL_CMD(FH49_RSCSR_CHNL0_RBDCB_BASE_REG);
  1012. IL_CMD(FH49_RSCSR_CHNL0_WPTR);
  1013. IL_CMD(FH49_MEM_RCSR_CHNL0_CONFIG_REG);
  1014. IL_CMD(FH49_MEM_RSSR_SHARED_CTRL_REG);
  1015. IL_CMD(FH49_MEM_RSSR_RX_STATUS_REG);
  1016. IL_CMD(FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1017. IL_CMD(FH49_TSSR_TX_STATUS_REG);
  1018. IL_CMD(FH49_TSSR_TX_ERROR_REG);
  1019. default:
  1020. return "UNKNOWN";
  1021. }
  1022. }
  1023. int
  1024. il4965_dump_fh(struct il_priv *il, char **buf, bool display)
  1025. {
  1026. int i;
  1027. #ifdef CONFIG_IWLEGACY_DEBUG
  1028. int pos = 0;
  1029. size_t bufsz = 0;
  1030. #endif
  1031. static const u32 fh_tbl[] = {
  1032. FH49_RSCSR_CHNL0_STTS_WPTR_REG,
  1033. FH49_RSCSR_CHNL0_RBDCB_BASE_REG,
  1034. FH49_RSCSR_CHNL0_WPTR,
  1035. FH49_MEM_RCSR_CHNL0_CONFIG_REG,
  1036. FH49_MEM_RSSR_SHARED_CTRL_REG,
  1037. FH49_MEM_RSSR_RX_STATUS_REG,
  1038. FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1039. FH49_TSSR_TX_STATUS_REG,
  1040. FH49_TSSR_TX_ERROR_REG
  1041. };
  1042. #ifdef CONFIG_IWLEGACY_DEBUG
  1043. if (display) {
  1044. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1045. *buf = kmalloc(bufsz, GFP_KERNEL);
  1046. if (!*buf)
  1047. return -ENOMEM;
  1048. pos +=
  1049. scnprintf(*buf + pos, bufsz - pos, "FH register values:\n");
  1050. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1051. pos +=
  1052. scnprintf(*buf + pos, bufsz - pos,
  1053. " %34s: 0X%08x\n",
  1054. il4965_get_fh_string(fh_tbl[i]),
  1055. il_rd(il, fh_tbl[i]));
  1056. }
  1057. return pos;
  1058. }
  1059. #endif
  1060. IL_ERR("FH register values:\n");
  1061. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1062. IL_ERR(" %34s: 0X%08x\n", il4965_get_fh_string(fh_tbl[i]),
  1063. il_rd(il, fh_tbl[i]));
  1064. }
  1065. return 0;
  1066. }
  1067. static void
  1068. il4965_hdl_missed_beacon(struct il_priv *il, struct il_rx_buf *rxb)
  1069. {
  1070. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1071. struct il_missed_beacon_notif *missed_beacon;
  1072. missed_beacon = &pkt->u.missed_beacon;
  1073. if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
  1074. il->missed_beacon_threshold) {
  1075. D_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  1076. le32_to_cpu(missed_beacon->consecutive_missed_beacons),
  1077. le32_to_cpu(missed_beacon->total_missed_becons),
  1078. le32_to_cpu(missed_beacon->num_recvd_beacons),
  1079. le32_to_cpu(missed_beacon->num_expected_beacons));
  1080. if (!test_bit(S_SCANNING, &il->status))
  1081. il4965_init_sensitivity(il);
  1082. }
  1083. }
  1084. /* Calculate noise level, based on measurements during network silence just
  1085. * before arriving beacon. This measurement can be done only if we know
  1086. * exactly when to expect beacons, therefore only when we're associated. */
  1087. static void
  1088. il4965_rx_calc_noise(struct il_priv *il)
  1089. {
  1090. struct stats_rx_non_phy *rx_info;
  1091. int num_active_rx = 0;
  1092. int total_silence = 0;
  1093. int bcn_silence_a, bcn_silence_b, bcn_silence_c;
  1094. int last_rx_noise;
  1095. rx_info = &(il->_4965.stats.rx.general);
  1096. bcn_silence_a =
  1097. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  1098. bcn_silence_b =
  1099. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  1100. bcn_silence_c =
  1101. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  1102. if (bcn_silence_a) {
  1103. total_silence += bcn_silence_a;
  1104. num_active_rx++;
  1105. }
  1106. if (bcn_silence_b) {
  1107. total_silence += bcn_silence_b;
  1108. num_active_rx++;
  1109. }
  1110. if (bcn_silence_c) {
  1111. total_silence += bcn_silence_c;
  1112. num_active_rx++;
  1113. }
  1114. /* Average among active antennas */
  1115. if (num_active_rx)
  1116. last_rx_noise = (total_silence / num_active_rx) - 107;
  1117. else
  1118. last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
  1119. D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n", bcn_silence_a,
  1120. bcn_silence_b, bcn_silence_c, last_rx_noise);
  1121. }
  1122. #ifdef CONFIG_IWLEGACY_DEBUGFS
  1123. /*
  1124. * based on the assumption of all stats counter are in DWORD
  1125. * FIXME: This function is for debugging, do not deal with
  1126. * the case of counters roll-over.
  1127. */
  1128. static void
  1129. il4965_accumulative_stats(struct il_priv *il, __le32 * stats)
  1130. {
  1131. int i, size;
  1132. __le32 *prev_stats;
  1133. u32 *accum_stats;
  1134. u32 *delta, *max_delta;
  1135. struct stats_general_common *general, *accum_general;
  1136. prev_stats = (__le32 *) &il->_4965.stats;
  1137. accum_stats = (u32 *) &il->_4965.accum_stats;
  1138. size = sizeof(struct il_notif_stats);
  1139. general = &il->_4965.stats.general.common;
  1140. accum_general = &il->_4965.accum_stats.general.common;
  1141. delta = (u32 *) &il->_4965.delta_stats;
  1142. max_delta = (u32 *) &il->_4965.max_delta;
  1143. for (i = sizeof(__le32); i < size;
  1144. i +=
  1145. sizeof(__le32), stats++, prev_stats++, delta++, max_delta++,
  1146. accum_stats++) {
  1147. if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
  1148. *delta =
  1149. (le32_to_cpu(*stats) - le32_to_cpu(*prev_stats));
  1150. *accum_stats += *delta;
  1151. if (*delta > *max_delta)
  1152. *max_delta = *delta;
  1153. }
  1154. }
  1155. /* reset accumulative stats for "no-counter" type stats */
  1156. accum_general->temperature = general->temperature;
  1157. accum_general->ttl_timestamp = general->ttl_timestamp;
  1158. }
  1159. #endif
  1160. static void
  1161. il4965_hdl_stats(struct il_priv *il, struct il_rx_buf *rxb)
  1162. {
  1163. const int recalib_seconds = 60;
  1164. bool change;
  1165. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1166. D_RX("Statistics notification received (%d vs %d).\n",
  1167. (int)sizeof(struct il_notif_stats),
  1168. le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK);
  1169. change =
  1170. ((il->_4965.stats.general.common.temperature !=
  1171. pkt->u.stats.general.common.temperature) ||
  1172. ((il->_4965.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK) !=
  1173. (pkt->u.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK)));
  1174. #ifdef CONFIG_IWLEGACY_DEBUGFS
  1175. il4965_accumulative_stats(il, (__le32 *) &pkt->u.stats);
  1176. #endif
  1177. /* TODO: reading some of stats is unneeded */
  1178. memcpy(&il->_4965.stats, &pkt->u.stats, sizeof(il->_4965.stats));
  1179. set_bit(S_STATS, &il->status);
  1180. /*
  1181. * Reschedule the stats timer to occur in recalib_seconds to ensure
  1182. * we get a thermal update even if the uCode doesn't give us one
  1183. */
  1184. mod_timer(&il->stats_periodic,
  1185. jiffies + msecs_to_jiffies(recalib_seconds * 1000));
  1186. if (unlikely(!test_bit(S_SCANNING, &il->status)) &&
  1187. (pkt->hdr.cmd == N_STATS)) {
  1188. il4965_rx_calc_noise(il);
  1189. queue_work(il->workqueue, &il->run_time_calib_work);
  1190. }
  1191. if (change)
  1192. il4965_temperature_calib(il);
  1193. }
  1194. static void
  1195. il4965_hdl_c_stats(struct il_priv *il, struct il_rx_buf *rxb)
  1196. {
  1197. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1198. if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATS_CLEAR_MSK) {
  1199. #ifdef CONFIG_IWLEGACY_DEBUGFS
  1200. memset(&il->_4965.accum_stats, 0,
  1201. sizeof(struct il_notif_stats));
  1202. memset(&il->_4965.delta_stats, 0,
  1203. sizeof(struct il_notif_stats));
  1204. memset(&il->_4965.max_delta, 0, sizeof(struct il_notif_stats));
  1205. #endif
  1206. D_RX("Statistics have been cleared\n");
  1207. }
  1208. il4965_hdl_stats(il, rxb);
  1209. }
  1210. /*
  1211. * mac80211 queues, ACs, hardware queues, FIFOs.
  1212. *
  1213. * Cf. https://wireless.wiki.kernel.org/en/developers/Documentation/mac80211/queues
  1214. *
  1215. * Mac80211 uses the following numbers, which we get as from it
  1216. * by way of skb_get_queue_mapping(skb):
  1217. *
  1218. * VO 0
  1219. * VI 1
  1220. * BE 2
  1221. * BK 3
  1222. *
  1223. *
  1224. * Regular (not A-MPDU) frames are put into hardware queues corresponding
  1225. * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
  1226. * own queue per aggregation session (RA/TID combination), such queues are
  1227. * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
  1228. * order to map frames to the right queue, we also need an AC->hw queue
  1229. * mapping. This is implemented here.
  1230. *
  1231. * Due to the way hw queues are set up (by the hw specific modules like
  1232. * 4965.c), the AC->hw queue mapping is the identity
  1233. * mapping.
  1234. */
  1235. static const u8 tid_to_ac[] = {
  1236. IEEE80211_AC_BE,
  1237. IEEE80211_AC_BK,
  1238. IEEE80211_AC_BK,
  1239. IEEE80211_AC_BE,
  1240. IEEE80211_AC_VI,
  1241. IEEE80211_AC_VI,
  1242. IEEE80211_AC_VO,
  1243. IEEE80211_AC_VO
  1244. };
  1245. static inline int
  1246. il4965_get_ac_from_tid(u16 tid)
  1247. {
  1248. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  1249. return tid_to_ac[tid];
  1250. /* no support for TIDs 8-15 yet */
  1251. return -EINVAL;
  1252. }
  1253. static inline int
  1254. il4965_get_fifo_from_tid(u16 tid)
  1255. {
  1256. static const u8 ac_to_fifo[] = {
  1257. IL_TX_FIFO_VO,
  1258. IL_TX_FIFO_VI,
  1259. IL_TX_FIFO_BE,
  1260. IL_TX_FIFO_BK,
  1261. };
  1262. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  1263. return ac_to_fifo[tid_to_ac[tid]];
  1264. /* no support for TIDs 8-15 yet */
  1265. return -EINVAL;
  1266. }
  1267. /*
  1268. * handle build C_TX command notification.
  1269. */
  1270. static void
  1271. il4965_tx_cmd_build_basic(struct il_priv *il, struct sk_buff *skb,
  1272. struct il_tx_cmd *tx_cmd,
  1273. struct ieee80211_tx_info *info,
  1274. struct ieee80211_hdr *hdr, u8 std_id)
  1275. {
  1276. __le16 fc = hdr->frame_control;
  1277. __le32 tx_flags = tx_cmd->tx_flags;
  1278. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1279. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  1280. tx_flags |= TX_CMD_FLG_ACK_MSK;
  1281. if (ieee80211_is_mgmt(fc))
  1282. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1283. if (ieee80211_is_probe_resp(fc) &&
  1284. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  1285. tx_flags |= TX_CMD_FLG_TSF_MSK;
  1286. } else {
  1287. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  1288. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1289. }
  1290. if (ieee80211_is_back_req(fc))
  1291. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  1292. tx_cmd->sta_id = std_id;
  1293. if (ieee80211_has_morefrags(fc))
  1294. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  1295. if (ieee80211_is_data_qos(fc)) {
  1296. u8 *qc = ieee80211_get_qos_ctl(hdr);
  1297. tx_cmd->tid_tspec = qc[0] & 0xf;
  1298. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  1299. } else {
  1300. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1301. }
  1302. il_tx_cmd_protection(il, info, fc, &tx_flags);
  1303. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  1304. if (ieee80211_is_mgmt(fc)) {
  1305. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  1306. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  1307. else
  1308. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  1309. } else {
  1310. tx_cmd->timeout.pm_frame_timeout = 0;
  1311. }
  1312. tx_cmd->driver_txop = 0;
  1313. tx_cmd->tx_flags = tx_flags;
  1314. tx_cmd->next_frame_len = 0;
  1315. }
  1316. static void
  1317. il4965_tx_cmd_build_rate(struct il_priv *il,
  1318. struct il_tx_cmd *tx_cmd,
  1319. struct ieee80211_tx_info *info,
  1320. struct ieee80211_sta *sta,
  1321. __le16 fc)
  1322. {
  1323. const u8 rts_retry_limit = 60;
  1324. u32 rate_flags;
  1325. int rate_idx;
  1326. u8 data_retry_limit;
  1327. u8 rate_plcp;
  1328. /* Set retry limit on DATA packets and Probe Responses */
  1329. if (ieee80211_is_probe_resp(fc))
  1330. data_retry_limit = 3;
  1331. else
  1332. data_retry_limit = IL4965_DEFAULT_TX_RETRY;
  1333. tx_cmd->data_retry_limit = data_retry_limit;
  1334. /* Set retry limit on RTS packets */
  1335. tx_cmd->rts_retry_limit = min(data_retry_limit, rts_retry_limit);
  1336. /* DATA packets will use the uCode station table for rate/antenna
  1337. * selection */
  1338. if (ieee80211_is_data(fc)) {
  1339. tx_cmd->initial_rate_idx = 0;
  1340. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  1341. return;
  1342. }
  1343. /**
  1344. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  1345. * not really a TX rate. Thus, we use the lowest supported rate for
  1346. * this band. Also use the lowest supported rate if the stored rate
  1347. * idx is invalid.
  1348. */
  1349. rate_idx = info->control.rates[0].idx;
  1350. if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) || rate_idx < 0
  1351. || rate_idx > RATE_COUNT_LEGACY)
  1352. rate_idx = rate_lowest_index(&il->bands[info->band], sta);
  1353. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  1354. if (info->band == NL80211_BAND_5GHZ) {
  1355. rate_idx += IL_FIRST_OFDM_RATE;
  1356. if (rate_idx > IL_LAST_OFDM_RATE)
  1357. rate_idx = IL_LAST_OFDM_RATE;
  1358. }
  1359. /* Get PLCP rate for tx_cmd->rate_n_flags */
  1360. rate_plcp = il_rates[rate_idx].plcp;
  1361. /* Zero out flags for this packet */
  1362. rate_flags = 0;
  1363. /* Set CCK flag as needed */
  1364. if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
  1365. rate_flags |= RATE_MCS_CCK_MSK;
  1366. /* Set up antennas */
  1367. il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
  1368. rate_flags |= BIT(il->mgmt_tx_ant) << RATE_MCS_ANT_POS;
  1369. /* Set the rate in the TX cmd */
  1370. tx_cmd->rate_n_flags = cpu_to_le32(rate_plcp | rate_flags);
  1371. }
  1372. static void
  1373. il4965_tx_cmd_build_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
  1374. struct il_tx_cmd *tx_cmd, struct sk_buff *skb_frag,
  1375. int sta_id)
  1376. {
  1377. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  1378. switch (keyconf->cipher) {
  1379. case WLAN_CIPHER_SUITE_CCMP:
  1380. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  1381. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  1382. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  1383. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  1384. D_TX("tx_cmd with AES hwcrypto\n");
  1385. break;
  1386. case WLAN_CIPHER_SUITE_TKIP:
  1387. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  1388. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  1389. D_TX("tx_cmd with tkip hwcrypto\n");
  1390. break;
  1391. case WLAN_CIPHER_SUITE_WEP104:
  1392. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  1393. fallthrough;
  1394. case WLAN_CIPHER_SUITE_WEP40:
  1395. tx_cmd->sec_ctl |=
  1396. (TX_CMD_SEC_WEP | (keyconf->keyidx & TX_CMD_SEC_MSK) <<
  1397. TX_CMD_SEC_SHIFT);
  1398. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  1399. D_TX("Configuring packet for WEP encryption " "with key %d\n",
  1400. keyconf->keyidx);
  1401. break;
  1402. default:
  1403. IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
  1404. break;
  1405. }
  1406. }
  1407. /*
  1408. * start C_TX command process
  1409. */
  1410. int
  1411. il4965_tx_skb(struct il_priv *il,
  1412. struct ieee80211_sta *sta,
  1413. struct sk_buff *skb)
  1414. {
  1415. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1416. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1417. struct il_station_priv *sta_priv = NULL;
  1418. struct il_tx_queue *txq;
  1419. struct il_queue *q;
  1420. struct il_device_cmd *out_cmd;
  1421. struct il_cmd_meta *out_meta;
  1422. struct il_tx_cmd *tx_cmd;
  1423. int txq_id;
  1424. dma_addr_t phys_addr;
  1425. dma_addr_t txcmd_phys;
  1426. dma_addr_t scratch_phys;
  1427. u16 len, firstlen, secondlen;
  1428. u16 seq_number = 0;
  1429. __le16 fc;
  1430. u8 hdr_len;
  1431. u8 sta_id;
  1432. u8 wait_write_ptr = 0;
  1433. u8 tid = 0;
  1434. u8 *qc = NULL;
  1435. unsigned long flags;
  1436. bool is_agg = false;
  1437. spin_lock_irqsave(&il->lock, flags);
  1438. if (il_is_rfkill(il)) {
  1439. D_DROP("Dropping - RF KILL\n");
  1440. goto drop_unlock;
  1441. }
  1442. fc = hdr->frame_control;
  1443. #ifdef CONFIG_IWLEGACY_DEBUG
  1444. if (ieee80211_is_auth(fc))
  1445. D_TX("Sending AUTH frame\n");
  1446. else if (ieee80211_is_assoc_req(fc))
  1447. D_TX("Sending ASSOC frame\n");
  1448. else if (ieee80211_is_reassoc_req(fc))
  1449. D_TX("Sending REASSOC frame\n");
  1450. #endif
  1451. hdr_len = ieee80211_hdrlen(fc);
  1452. /* For management frames use broadcast id to do not break aggregation */
  1453. if (!ieee80211_is_data(fc))
  1454. sta_id = il->hw_params.bcast_id;
  1455. else {
  1456. /* Find idx into station table for destination station */
  1457. sta_id = il_sta_id_or_broadcast(il, sta);
  1458. if (sta_id == IL_INVALID_STATION) {
  1459. D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
  1460. goto drop_unlock;
  1461. }
  1462. }
  1463. D_TX("station Id %d\n", sta_id);
  1464. if (sta)
  1465. sta_priv = (void *)sta->drv_priv;
  1466. if (sta_priv && sta_priv->asleep &&
  1467. (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)) {
  1468. /*
  1469. * This sends an asynchronous command to the device,
  1470. * but we can rely on it being processed before the
  1471. * next frame is processed -- and the next frame to
  1472. * this station is the one that will consume this
  1473. * counter.
  1474. * For now set the counter to just 1 since we do not
  1475. * support uAPSD yet.
  1476. */
  1477. il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
  1478. }
  1479. /* FIXME: remove me ? */
  1480. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
  1481. /* Access category (AC) is also the queue number */
  1482. txq_id = skb_get_queue_mapping(skb);
  1483. /* irqs already disabled/saved above when locking il->lock */
  1484. spin_lock(&il->sta_lock);
  1485. if (ieee80211_is_data_qos(fc)) {
  1486. qc = ieee80211_get_qos_ctl(hdr);
  1487. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  1488. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
  1489. spin_unlock(&il->sta_lock);
  1490. goto drop_unlock;
  1491. }
  1492. seq_number = il->stations[sta_id].tid[tid].seq_number;
  1493. seq_number &= IEEE80211_SCTL_SEQ;
  1494. hdr->seq_ctrl =
  1495. hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG);
  1496. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  1497. seq_number += 0x10;
  1498. /* aggregation is on for this <sta,tid> */
  1499. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  1500. il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
  1501. txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
  1502. is_agg = true;
  1503. }
  1504. }
  1505. txq = &il->txq[txq_id];
  1506. q = &txq->q;
  1507. if (unlikely(il_queue_space(q) < q->high_mark)) {
  1508. spin_unlock(&il->sta_lock);
  1509. goto drop_unlock;
  1510. }
  1511. if (ieee80211_is_data_qos(fc)) {
  1512. il->stations[sta_id].tid[tid].tfds_in_queue++;
  1513. if (!ieee80211_has_morefrags(fc))
  1514. il->stations[sta_id].tid[tid].seq_number = seq_number;
  1515. }
  1516. spin_unlock(&il->sta_lock);
  1517. txq->skbs[q->write_ptr] = skb;
  1518. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1519. out_cmd = txq->cmd[q->write_ptr];
  1520. out_meta = &txq->meta[q->write_ptr];
  1521. tx_cmd = container_of(&out_cmd->cmd.tx, struct il_tx_cmd, __hdr);
  1522. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  1523. memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
  1524. /*
  1525. * Set up the Tx-command (not MAC!) header.
  1526. * Store the chosen Tx queue and TFD idx within the sequence field;
  1527. * after Tx, uCode's Tx response will return this value so driver can
  1528. * locate the frame within the tx queue and do post-tx processing.
  1529. */
  1530. out_cmd->hdr.cmd = C_TX;
  1531. out_cmd->hdr.sequence =
  1532. cpu_to_le16((u16)
  1533. (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
  1534. /* Copy MAC header from skb into command buffer */
  1535. memcpy(tx_cmd->hdr, hdr, hdr_len);
  1536. /* Total # bytes to be transmitted */
  1537. tx_cmd->len = cpu_to_le16((u16) skb->len);
  1538. if (info->control.hw_key)
  1539. il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
  1540. /* TODO need this for burst mode later on */
  1541. il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
  1542. il4965_tx_cmd_build_rate(il, tx_cmd, info, sta, fc);
  1543. /*
  1544. * Use the first empty entry in this queue's command buffer array
  1545. * to contain the Tx command and MAC header concatenated together
  1546. * (payload data will be in another buffer).
  1547. * Size of this varies, due to varying MAC header length.
  1548. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1549. * of the MAC header (device reads on dword boundaries).
  1550. * We'll tell device about this padding later.
  1551. */
  1552. len = sizeof(struct il_tx_cmd) + sizeof(struct il_cmd_header) + hdr_len;
  1553. firstlen = (len + 3) & ~3;
  1554. /* Tell NIC about any 2-byte padding after MAC header */
  1555. if (firstlen != len)
  1556. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1557. /* Physical address of this Tx command's header (not MAC header!),
  1558. * within command buffer array. */
  1559. txcmd_phys = dma_map_single(&il->pci_dev->dev, &out_cmd->hdr, firstlen,
  1560. DMA_BIDIRECTIONAL);
  1561. if (unlikely(dma_mapping_error(&il->pci_dev->dev, txcmd_phys)))
  1562. goto drop_unlock;
  1563. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1564. * if any (802.11 null frames have no payload). */
  1565. secondlen = skb->len - hdr_len;
  1566. if (secondlen > 0) {
  1567. phys_addr = dma_map_single(&il->pci_dev->dev, skb->data + hdr_len,
  1568. secondlen, DMA_TO_DEVICE);
  1569. if (unlikely(dma_mapping_error(&il->pci_dev->dev, phys_addr)))
  1570. goto drop_unlock;
  1571. }
  1572. /* Add buffer containing Tx command and MAC(!) header to TFD's
  1573. * first entry */
  1574. il->ops->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen, 1, 0);
  1575. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  1576. dma_unmap_len_set(out_meta, len, firstlen);
  1577. if (secondlen)
  1578. il->ops->txq_attach_buf_to_tfd(il, txq, phys_addr, secondlen,
  1579. 0, 0);
  1580. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  1581. txq->need_update = 1;
  1582. } else {
  1583. wait_write_ptr = 1;
  1584. txq->need_update = 0;
  1585. }
  1586. scratch_phys =
  1587. txcmd_phys + sizeof(struct il_cmd_header) +
  1588. offsetof(struct il_tx_cmd, scratch);
  1589. /* take back ownership of DMA buffer to enable update */
  1590. dma_sync_single_for_cpu(&il->pci_dev->dev, txcmd_phys, firstlen,
  1591. DMA_BIDIRECTIONAL);
  1592. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1593. tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
  1594. il_update_stats(il, true, fc, skb->len);
  1595. D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
  1596. D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  1597. il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd, sizeof(*tx_cmd));
  1598. il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr, hdr_len);
  1599. /* Set up entry for this TFD in Tx byte-count array */
  1600. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  1601. il->ops->txq_update_byte_cnt_tbl(il, txq, le16_to_cpu(tx_cmd->len));
  1602. dma_sync_single_for_device(&il->pci_dev->dev, txcmd_phys, firstlen,
  1603. DMA_BIDIRECTIONAL);
  1604. /* Tell device the write idx *just past* this latest filled TFD */
  1605. q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
  1606. il_txq_update_write_ptr(il, txq);
  1607. spin_unlock_irqrestore(&il->lock, flags);
  1608. /*
  1609. * At this point the frame is "transmitted" successfully
  1610. * and we will get a TX status notification eventually,
  1611. * regardless of the value of ret. "ret" only indicates
  1612. * whether or not we should update the write pointer.
  1613. */
  1614. /*
  1615. * Avoid atomic ops if it isn't an associated client.
  1616. * Also, if this is a packet for aggregation, don't
  1617. * increase the counter because the ucode will stop
  1618. * aggregation queues when their respective station
  1619. * goes to sleep.
  1620. */
  1621. if (sta_priv && sta_priv->client && !is_agg)
  1622. atomic_inc(&sta_priv->pending_frames);
  1623. if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
  1624. if (wait_write_ptr) {
  1625. spin_lock_irqsave(&il->lock, flags);
  1626. txq->need_update = 1;
  1627. il_txq_update_write_ptr(il, txq);
  1628. spin_unlock_irqrestore(&il->lock, flags);
  1629. } else {
  1630. il_stop_queue(il, txq);
  1631. }
  1632. }
  1633. return 0;
  1634. drop_unlock:
  1635. spin_unlock_irqrestore(&il->lock, flags);
  1636. return -1;
  1637. }
  1638. static inline int
  1639. il4965_alloc_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr, size_t size)
  1640. {
  1641. ptr->addr = dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma,
  1642. GFP_KERNEL);
  1643. if (!ptr->addr)
  1644. return -ENOMEM;
  1645. ptr->size = size;
  1646. return 0;
  1647. }
  1648. static inline void
  1649. il4965_free_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr)
  1650. {
  1651. if (unlikely(!ptr->addr))
  1652. return;
  1653. dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  1654. memset(ptr, 0, sizeof(*ptr));
  1655. }
  1656. /*
  1657. * il4965_hw_txq_ctx_free - Free TXQ Context
  1658. *
  1659. * Destroy all TX DMA queues and structures
  1660. */
  1661. void
  1662. il4965_hw_txq_ctx_free(struct il_priv *il)
  1663. {
  1664. int txq_id;
  1665. /* Tx queues */
  1666. if (il->txq) {
  1667. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  1668. if (txq_id == il->cmd_queue)
  1669. il_cmd_queue_free(il);
  1670. else
  1671. il_tx_queue_free(il, txq_id);
  1672. }
  1673. il4965_free_dma_ptr(il, &il->kw);
  1674. il4965_free_dma_ptr(il, &il->scd_bc_tbls);
  1675. /* free tx queue structure */
  1676. il_free_txq_mem(il);
  1677. }
  1678. /*
  1679. * il4965_txq_ctx_alloc - allocate TX queue context
  1680. * Allocate all Tx DMA structures and initialize them
  1681. */
  1682. int
  1683. il4965_txq_ctx_alloc(struct il_priv *il)
  1684. {
  1685. int ret, txq_id;
  1686. unsigned long flags;
  1687. /* Free all tx/cmd queues and keep-warm buffer */
  1688. il4965_hw_txq_ctx_free(il);
  1689. ret =
  1690. il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
  1691. il->hw_params.scd_bc_tbls_size);
  1692. if (ret) {
  1693. IL_ERR("Scheduler BC Table allocation failed\n");
  1694. goto error_bc_tbls;
  1695. }
  1696. /* Alloc keep-warm buffer */
  1697. ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
  1698. if (ret) {
  1699. IL_ERR("Keep Warm allocation failed\n");
  1700. goto error_kw;
  1701. }
  1702. /* allocate tx queue structure */
  1703. ret = il_alloc_txq_mem(il);
  1704. if (ret)
  1705. goto error;
  1706. spin_lock_irqsave(&il->lock, flags);
  1707. /* Turn off all Tx DMA fifos */
  1708. il4965_txq_set_sched(il, 0);
  1709. /* Tell NIC where to find the "keep warm" buffer */
  1710. il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
  1711. spin_unlock_irqrestore(&il->lock, flags);
  1712. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  1713. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
  1714. ret = il_tx_queue_init(il, txq_id);
  1715. if (ret) {
  1716. IL_ERR("Tx %d queue init failed\n", txq_id);
  1717. goto error;
  1718. }
  1719. }
  1720. return ret;
  1721. error:
  1722. il4965_hw_txq_ctx_free(il);
  1723. il4965_free_dma_ptr(il, &il->kw);
  1724. error_kw:
  1725. il4965_free_dma_ptr(il, &il->scd_bc_tbls);
  1726. error_bc_tbls:
  1727. return ret;
  1728. }
  1729. void
  1730. il4965_txq_ctx_reset(struct il_priv *il)
  1731. {
  1732. int txq_id;
  1733. unsigned long flags;
  1734. spin_lock_irqsave(&il->lock, flags);
  1735. /* Turn off all Tx DMA fifos */
  1736. il4965_txq_set_sched(il, 0);
  1737. /* Tell NIC where to find the "keep warm" buffer */
  1738. il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
  1739. spin_unlock_irqrestore(&il->lock, flags);
  1740. /* Alloc and init all Tx queues, including the command queue (#4) */
  1741. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  1742. il_tx_queue_reset(il, txq_id);
  1743. }
  1744. static void
  1745. il4965_txq_ctx_unmap(struct il_priv *il)
  1746. {
  1747. int txq_id;
  1748. if (!il->txq)
  1749. return;
  1750. /* Unmap DMA from host system and free skb's */
  1751. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  1752. if (txq_id == il->cmd_queue)
  1753. il_cmd_queue_unmap(il);
  1754. else
  1755. il_tx_queue_unmap(il, txq_id);
  1756. }
  1757. /*
  1758. * il4965_txq_ctx_stop - Stop all Tx DMA channels
  1759. */
  1760. void
  1761. il4965_txq_ctx_stop(struct il_priv *il)
  1762. {
  1763. int ch, ret;
  1764. _il_wr_prph(il, IL49_SCD_TXFACT, 0);
  1765. /* Stop each Tx DMA channel, and wait for it to be idle */
  1766. for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
  1767. _il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  1768. ret =
  1769. _il_poll_bit(il, FH49_TSSR_TX_STATUS_REG,
  1770. FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  1771. FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  1772. 1000);
  1773. if (ret < 0)
  1774. IL_ERR("Timeout stopping DMA channel %d [0x%08x]",
  1775. ch, _il_rd(il, FH49_TSSR_TX_STATUS_REG));
  1776. }
  1777. }
  1778. /*
  1779. * Find first available (lowest unused) Tx Queue, mark it "active".
  1780. * Called only when finding queue for aggregation.
  1781. * Should never return anything < 7, because they should already
  1782. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  1783. */
  1784. static int
  1785. il4965_txq_ctx_activate_free(struct il_priv *il)
  1786. {
  1787. int txq_id;
  1788. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  1789. if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
  1790. return txq_id;
  1791. return -1;
  1792. }
  1793. /*
  1794. * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  1795. */
  1796. static void
  1797. il4965_tx_queue_stop_scheduler(struct il_priv *il, u16 txq_id)
  1798. {
  1799. /* Simply stop the queue, but don't change any configuration;
  1800. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  1801. il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
  1802. (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  1803. (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  1804. }
  1805. /*
  1806. * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  1807. */
  1808. static int
  1809. il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid, u16 txq_id)
  1810. {
  1811. u32 tbl_dw_addr;
  1812. u32 tbl_dw;
  1813. u16 scd_q2ratid;
  1814. scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  1815. tbl_dw_addr =
  1816. il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  1817. tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
  1818. if (txq_id & 0x1)
  1819. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  1820. else
  1821. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  1822. il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
  1823. return 0;
  1824. }
  1825. /*
  1826. * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
  1827. *
  1828. * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
  1829. * i.e. it must be one of the higher queues used for aggregation
  1830. */
  1831. static int
  1832. il4965_txq_agg_enable(struct il_priv *il, int txq_id, int tx_fifo, int sta_id,
  1833. int tid, u16 ssn_idx)
  1834. {
  1835. unsigned long flags;
  1836. u16 ra_tid;
  1837. int ret;
  1838. if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
  1839. (IL49_FIRST_AMPDU_QUEUE +
  1840. il->cfg->num_of_ampdu_queues <= txq_id)) {
  1841. IL_WARN("queue number out of range: %d, must be %d to %d\n",
  1842. txq_id, IL49_FIRST_AMPDU_QUEUE,
  1843. IL49_FIRST_AMPDU_QUEUE +
  1844. il->cfg->num_of_ampdu_queues - 1);
  1845. return -EINVAL;
  1846. }
  1847. ra_tid = BUILD_RAxTID(sta_id, tid);
  1848. /* Modify device's station table to Tx this TID */
  1849. ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
  1850. if (ret)
  1851. return ret;
  1852. spin_lock_irqsave(&il->lock, flags);
  1853. /* Stop this Tx queue before configuring it */
  1854. il4965_tx_queue_stop_scheduler(il, txq_id);
  1855. /* Map receiver-address / traffic-ID to this queue */
  1856. il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
  1857. /* Set this queue as a chain-building queue */
  1858. il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  1859. /* Place first TFD at idx corresponding to start sequence number.
  1860. * Assumes that ssn_idx is valid (!= 0xFFF) */
  1861. il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  1862. il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  1863. il4965_set_wr_ptrs(il, txq_id, ssn_idx);
  1864. /* Set up Tx win size and frame limit for this queue */
  1865. il_write_targ_mem(il,
  1866. il->scd_base_addr +
  1867. IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  1868. (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS)
  1869. & IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  1870. il_write_targ_mem(il,
  1871. il->scd_base_addr +
  1872. IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  1873. (SCD_FRAME_LIMIT <<
  1874. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  1875. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  1876. il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  1877. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  1878. il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
  1879. spin_unlock_irqrestore(&il->lock, flags);
  1880. return 0;
  1881. }
  1882. int
  1883. il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
  1884. struct ieee80211_sta *sta, u16 tid, u16 * ssn)
  1885. {
  1886. int sta_id;
  1887. int tx_fifo;
  1888. int txq_id;
  1889. int ret;
  1890. unsigned long flags;
  1891. struct il_tid_data *tid_data;
  1892. /* FIXME: warning if tx fifo not found ? */
  1893. tx_fifo = il4965_get_fifo_from_tid(tid);
  1894. if (unlikely(tx_fifo < 0))
  1895. return tx_fifo;
  1896. D_HT("%s on ra = %pM tid = %d\n", __func__, sta->addr, tid);
  1897. sta_id = il_sta_id(sta);
  1898. if (sta_id == IL_INVALID_STATION) {
  1899. IL_ERR("Start AGG on invalid station\n");
  1900. return -ENXIO;
  1901. }
  1902. if (unlikely(tid >= MAX_TID_COUNT))
  1903. return -EINVAL;
  1904. if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
  1905. IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
  1906. return -ENXIO;
  1907. }
  1908. txq_id = il4965_txq_ctx_activate_free(il);
  1909. if (txq_id == -1) {
  1910. IL_ERR("No free aggregation queue available\n");
  1911. return -ENXIO;
  1912. }
  1913. spin_lock_irqsave(&il->sta_lock, flags);
  1914. tid_data = &il->stations[sta_id].tid[tid];
  1915. *ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  1916. tid_data->agg.txq_id = txq_id;
  1917. il_set_swq_id(&il->txq[txq_id], il4965_get_ac_from_tid(tid), txq_id);
  1918. spin_unlock_irqrestore(&il->sta_lock, flags);
  1919. ret = il4965_txq_agg_enable(il, txq_id, tx_fifo, sta_id, tid, *ssn);
  1920. if (ret)
  1921. return ret;
  1922. spin_lock_irqsave(&il->sta_lock, flags);
  1923. tid_data = &il->stations[sta_id].tid[tid];
  1924. if (tid_data->tfds_in_queue == 0) {
  1925. D_HT("HW queue is empty\n");
  1926. tid_data->agg.state = IL_AGG_ON;
  1927. ret = IEEE80211_AMPDU_TX_START_IMMEDIATE;
  1928. } else {
  1929. D_HT("HW queue is NOT empty: %d packets in HW queue\n",
  1930. tid_data->tfds_in_queue);
  1931. tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
  1932. }
  1933. spin_unlock_irqrestore(&il->sta_lock, flags);
  1934. return ret;
  1935. }
  1936. /*
  1937. * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
  1938. * il->lock must be held by the caller
  1939. */
  1940. static int
  1941. il4965_txq_agg_disable(struct il_priv *il, u16 txq_id, u16 ssn_idx, u8 tx_fifo)
  1942. {
  1943. if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
  1944. (IL49_FIRST_AMPDU_QUEUE +
  1945. il->cfg->num_of_ampdu_queues <= txq_id)) {
  1946. IL_WARN("queue number out of range: %d, must be %d to %d\n",
  1947. txq_id, IL49_FIRST_AMPDU_QUEUE,
  1948. IL49_FIRST_AMPDU_QUEUE +
  1949. il->cfg->num_of_ampdu_queues - 1);
  1950. return -EINVAL;
  1951. }
  1952. il4965_tx_queue_stop_scheduler(il, txq_id);
  1953. il_clear_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  1954. il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  1955. il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  1956. /* supposes that ssn_idx is valid (!= 0xFFF) */
  1957. il4965_set_wr_ptrs(il, txq_id, ssn_idx);
  1958. il_clear_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  1959. il_txq_ctx_deactivate(il, txq_id);
  1960. il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
  1961. return 0;
  1962. }
  1963. int
  1964. il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
  1965. struct ieee80211_sta *sta, u16 tid)
  1966. {
  1967. int tx_fifo_id, txq_id, sta_id, ssn;
  1968. struct il_tid_data *tid_data;
  1969. int write_ptr, read_ptr;
  1970. unsigned long flags;
  1971. /* FIXME: warning if tx_fifo_id not found ? */
  1972. tx_fifo_id = il4965_get_fifo_from_tid(tid);
  1973. if (unlikely(tx_fifo_id < 0))
  1974. return tx_fifo_id;
  1975. sta_id = il_sta_id(sta);
  1976. if (sta_id == IL_INVALID_STATION) {
  1977. IL_ERR("Invalid station for AGG tid %d\n", tid);
  1978. return -ENXIO;
  1979. }
  1980. spin_lock_irqsave(&il->sta_lock, flags);
  1981. tid_data = &il->stations[sta_id].tid[tid];
  1982. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  1983. txq_id = tid_data->agg.txq_id;
  1984. switch (il->stations[sta_id].tid[tid].agg.state) {
  1985. case IL_EMPTYING_HW_QUEUE_ADDBA:
  1986. /*
  1987. * This can happen if the peer stops aggregation
  1988. * again before we've had a chance to drain the
  1989. * queue we selected previously, i.e. before the
  1990. * session was really started completely.
  1991. */
  1992. D_HT("AGG stop before setup done\n");
  1993. goto turn_off;
  1994. case IL_AGG_ON:
  1995. break;
  1996. default:
  1997. IL_WARN("Stopping AGG while state not ON or starting\n");
  1998. }
  1999. write_ptr = il->txq[txq_id].q.write_ptr;
  2000. read_ptr = il->txq[txq_id].q.read_ptr;
  2001. /* The queue is not empty */
  2002. if (write_ptr != read_ptr) {
  2003. D_HT("Stopping a non empty AGG HW QUEUE\n");
  2004. il->stations[sta_id].tid[tid].agg.state =
  2005. IL_EMPTYING_HW_QUEUE_DELBA;
  2006. spin_unlock_irqrestore(&il->sta_lock, flags);
  2007. return 0;
  2008. }
  2009. D_HT("HW queue is empty\n");
  2010. turn_off:
  2011. il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
  2012. /* do not restore/save irqs */
  2013. spin_unlock(&il->sta_lock);
  2014. spin_lock(&il->lock);
  2015. /*
  2016. * the only reason this call can fail is queue number out of range,
  2017. * which can happen if uCode is reloaded and all the station
  2018. * information are lost. if it is outside the range, there is no need
  2019. * to deactivate the uCode queue, just return "success" to allow
  2020. * mac80211 to clean up it own data.
  2021. */
  2022. il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
  2023. spin_unlock_irqrestore(&il->lock, flags);
  2024. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2025. return 0;
  2026. }
  2027. int
  2028. il4965_txq_check_empty(struct il_priv *il, int sta_id, u8 tid, int txq_id)
  2029. {
  2030. struct il_queue *q = &il->txq[txq_id].q;
  2031. u8 *addr = il->stations[sta_id].sta.sta.addr;
  2032. struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
  2033. lockdep_assert_held(&il->sta_lock);
  2034. switch (il->stations[sta_id].tid[tid].agg.state) {
  2035. case IL_EMPTYING_HW_QUEUE_DELBA:
  2036. /* We are reclaiming the last packet of the */
  2037. /* aggregated HW queue */
  2038. if (txq_id == tid_data->agg.txq_id &&
  2039. q->read_ptr == q->write_ptr) {
  2040. u16 ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  2041. int tx_fifo = il4965_get_fifo_from_tid(tid);
  2042. D_HT("HW queue empty: continue DELBA flow\n");
  2043. il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
  2044. tid_data->agg.state = IL_AGG_OFF;
  2045. ieee80211_stop_tx_ba_cb_irqsafe(il->vif, addr, tid);
  2046. }
  2047. break;
  2048. case IL_EMPTYING_HW_QUEUE_ADDBA:
  2049. /* We are reclaiming the last packet of the queue */
  2050. if (tid_data->tfds_in_queue == 0) {
  2051. D_HT("HW queue empty: continue ADDBA flow\n");
  2052. tid_data->agg.state = IL_AGG_ON;
  2053. ieee80211_start_tx_ba_cb_irqsafe(il->vif, addr, tid);
  2054. }
  2055. break;
  2056. }
  2057. return 0;
  2058. }
  2059. static void
  2060. il4965_non_agg_tx_status(struct il_priv *il, const u8 *addr1)
  2061. {
  2062. struct ieee80211_sta *sta;
  2063. struct il_station_priv *sta_priv;
  2064. rcu_read_lock();
  2065. sta = ieee80211_find_sta(il->vif, addr1);
  2066. if (sta) {
  2067. sta_priv = (void *)sta->drv_priv;
  2068. /* avoid atomic ops if this isn't a client */
  2069. if (sta_priv->client &&
  2070. atomic_dec_return(&sta_priv->pending_frames) == 0)
  2071. ieee80211_sta_block_awake(il->hw, sta, false);
  2072. }
  2073. rcu_read_unlock();
  2074. }
  2075. static void
  2076. il4965_tx_status(struct il_priv *il, struct sk_buff *skb, bool is_agg)
  2077. {
  2078. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2079. if (!is_agg)
  2080. il4965_non_agg_tx_status(il, hdr->addr1);
  2081. ieee80211_tx_status_irqsafe(il->hw, skb);
  2082. }
  2083. int
  2084. il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
  2085. {
  2086. struct il_tx_queue *txq = &il->txq[txq_id];
  2087. struct il_queue *q = &txq->q;
  2088. int nfreed = 0;
  2089. struct ieee80211_hdr *hdr;
  2090. struct sk_buff *skb;
  2091. if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
  2092. IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
  2093. "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
  2094. q->write_ptr, q->read_ptr);
  2095. return 0;
  2096. }
  2097. for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  2098. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2099. skb = txq->skbs[txq->q.read_ptr];
  2100. if (WARN_ON_ONCE(skb == NULL))
  2101. continue;
  2102. hdr = (struct ieee80211_hdr *) skb->data;
  2103. if (ieee80211_is_data_qos(hdr->frame_control))
  2104. nfreed++;
  2105. il4965_tx_status(il, skb, txq_id >= IL4965_FIRST_AMPDU_QUEUE);
  2106. txq->skbs[txq->q.read_ptr] = NULL;
  2107. il->ops->txq_free_tfd(il, txq);
  2108. }
  2109. return nfreed;
  2110. }
  2111. /*
  2112. * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
  2113. *
  2114. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  2115. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  2116. */
  2117. static int
  2118. il4965_tx_status_reply_compressed_ba(struct il_priv *il, struct il_ht_agg *agg,
  2119. struct il_compressed_ba_resp *ba_resp)
  2120. {
  2121. int i, sh, ack;
  2122. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  2123. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  2124. int successes = 0;
  2125. struct ieee80211_tx_info *info;
  2126. u64 bitmap, sent_bitmap;
  2127. if (unlikely(!agg->wait_for_ba)) {
  2128. if (unlikely(ba_resp->bitmap))
  2129. IL_ERR("Received BA when not expected\n");
  2130. return -EINVAL;
  2131. }
  2132. /* Mark that the expected block-ack response arrived */
  2133. agg->wait_for_ba = 0;
  2134. D_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  2135. /* Calculate shift to align block-ack bits with our Tx win bits */
  2136. sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
  2137. if (sh < 0) /* tbw something is wrong with indices */
  2138. sh += 0x100;
  2139. if (agg->frame_count > (64 - sh)) {
  2140. D_TX_REPLY("more frames than bitmap size");
  2141. return -1;
  2142. }
  2143. /* don't use 64-bit values for now */
  2144. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  2145. /* check for success or failure according to the
  2146. * transmitted bitmap and block-ack bitmap */
  2147. sent_bitmap = bitmap & agg->bitmap;
  2148. /* For each frame attempted in aggregation,
  2149. * update driver's record of tx frame's status. */
  2150. i = 0;
  2151. while (sent_bitmap) {
  2152. ack = sent_bitmap & 1ULL;
  2153. successes += ack;
  2154. D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n", ack ? "ACK" : "NACK",
  2155. i, (agg->start_idx + i) & 0xff, agg->start_idx + i);
  2156. sent_bitmap >>= 1;
  2157. ++i;
  2158. }
  2159. D_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
  2160. info = IEEE80211_SKB_CB(il->txq[scd_flow].skbs[agg->start_idx]);
  2161. memset(&info->status, 0, sizeof(info->status));
  2162. info->flags |= IEEE80211_TX_STAT_ACK;
  2163. info->flags |= IEEE80211_TX_STAT_AMPDU;
  2164. info->status.ampdu_ack_len = successes;
  2165. info->status.ampdu_len = agg->frame_count;
  2166. il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
  2167. return 0;
  2168. }
  2169. static inline bool
  2170. il4965_is_tx_success(u32 status)
  2171. {
  2172. status &= TX_STATUS_MSK;
  2173. return (status == TX_STATUS_SUCCESS || status == TX_STATUS_DIRECT_DONE);
  2174. }
  2175. static u8
  2176. il4965_find_station(struct il_priv *il, const u8 *addr)
  2177. {
  2178. int i;
  2179. int start = 0;
  2180. int ret = IL_INVALID_STATION;
  2181. unsigned long flags;
  2182. if (il->iw_mode == NL80211_IFTYPE_ADHOC)
  2183. start = IL_STA_ID;
  2184. if (is_broadcast_ether_addr(addr))
  2185. return il->hw_params.bcast_id;
  2186. spin_lock_irqsave(&il->sta_lock, flags);
  2187. for (i = start; i < il->hw_params.max_stations; i++)
  2188. if (il->stations[i].used &&
  2189. ether_addr_equal(il->stations[i].sta.sta.addr, addr)) {
  2190. ret = i;
  2191. goto out;
  2192. }
  2193. D_ASSOC("can not find STA %pM total %d\n", addr, il->num_stations);
  2194. out:
  2195. /*
  2196. * It may be possible that more commands interacting with stations
  2197. * arrive before we completed processing the adding of
  2198. * station
  2199. */
  2200. if (ret != IL_INVALID_STATION &&
  2201. (!(il->stations[ret].used & IL_STA_UCODE_ACTIVE) ||
  2202. (il->stations[ret].used & IL_STA_UCODE_INPROGRESS))) {
  2203. IL_ERR("Requested station info for sta %d before ready.\n",
  2204. ret);
  2205. ret = IL_INVALID_STATION;
  2206. }
  2207. spin_unlock_irqrestore(&il->sta_lock, flags);
  2208. return ret;
  2209. }
  2210. static int
  2211. il4965_get_ra_sta_id(struct il_priv *il, struct ieee80211_hdr *hdr)
  2212. {
  2213. if (il->iw_mode == NL80211_IFTYPE_STATION)
  2214. return IL_AP_ID;
  2215. else {
  2216. u8 *da = ieee80211_get_DA(hdr);
  2217. return il4965_find_station(il, da);
  2218. }
  2219. }
  2220. static inline u32
  2221. il4965_get_scd_ssn(struct il4965_tx_resp *tx_resp)
  2222. {
  2223. return le32_to_cpup(&tx_resp->u.status +
  2224. tx_resp->frame_count) & IEEE80211_MAX_SN;
  2225. }
  2226. static inline u32
  2227. il4965_tx_status_to_mac80211(u32 status)
  2228. {
  2229. status &= TX_STATUS_MSK;
  2230. switch (status) {
  2231. case TX_STATUS_SUCCESS:
  2232. case TX_STATUS_DIRECT_DONE:
  2233. return IEEE80211_TX_STAT_ACK;
  2234. case TX_STATUS_FAIL_DEST_PS:
  2235. return IEEE80211_TX_STAT_TX_FILTERED;
  2236. default:
  2237. return 0;
  2238. }
  2239. }
  2240. /*
  2241. * il4965_tx_status_reply_tx - Handle Tx response for frames in aggregation queue
  2242. */
  2243. static int
  2244. il4965_tx_status_reply_tx(struct il_priv *il, struct il_ht_agg *agg,
  2245. struct il4965_tx_resp *tx_resp, int txq_id,
  2246. u16 start_idx)
  2247. {
  2248. u16 status;
  2249. struct agg_tx_status *frame_status = tx_resp->u.agg_status;
  2250. struct ieee80211_tx_info *info = NULL;
  2251. struct ieee80211_hdr *hdr = NULL;
  2252. u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  2253. int i, sh, idx;
  2254. u16 seq;
  2255. if (agg->wait_for_ba)
  2256. D_TX_REPLY("got tx response w/o block-ack\n");
  2257. agg->frame_count = tx_resp->frame_count;
  2258. agg->start_idx = start_idx;
  2259. agg->rate_n_flags = rate_n_flags;
  2260. agg->bitmap = 0;
  2261. /* num frames attempted by Tx command */
  2262. if (agg->frame_count == 1) {
  2263. /* Only one frame was attempted; no block-ack will arrive */
  2264. status = le16_to_cpu(frame_status[0].status);
  2265. idx = start_idx;
  2266. D_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
  2267. agg->frame_count, agg->start_idx, idx);
  2268. info = IEEE80211_SKB_CB(il->txq[txq_id].skbs[idx]);
  2269. info->status.rates[0].count = tx_resp->failure_frame + 1;
  2270. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  2271. info->flags |= il4965_tx_status_to_mac80211(status);
  2272. il4965_hwrate_to_tx_control(il, rate_n_flags, info);
  2273. D_TX_REPLY("1 Frame 0x%x failure :%d\n", status & 0xff,
  2274. tx_resp->failure_frame);
  2275. D_TX_REPLY("Rate Info rate_n_flags=%x\n", rate_n_flags);
  2276. agg->wait_for_ba = 0;
  2277. } else {
  2278. /* Two or more frames were attempted; expect block-ack */
  2279. u64 bitmap = 0;
  2280. int start = agg->start_idx;
  2281. struct sk_buff *skb;
  2282. /* Construct bit-map of pending frames within Tx win */
  2283. for (i = 0; i < agg->frame_count; i++) {
  2284. u16 sc;
  2285. status = le16_to_cpu(frame_status[i].status);
  2286. seq = le16_to_cpu(frame_status[i].sequence);
  2287. idx = SEQ_TO_IDX(seq);
  2288. txq_id = SEQ_TO_QUEUE(seq);
  2289. if (status &
  2290. (AGG_TX_STATE_FEW_BYTES_MSK |
  2291. AGG_TX_STATE_ABORT_MSK))
  2292. continue;
  2293. D_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
  2294. agg->frame_count, txq_id, idx);
  2295. skb = il->txq[txq_id].skbs[idx];
  2296. if (WARN_ON_ONCE(skb == NULL))
  2297. return -1;
  2298. hdr = (struct ieee80211_hdr *) skb->data;
  2299. sc = le16_to_cpu(hdr->seq_ctrl);
  2300. if (idx != (IEEE80211_SEQ_TO_SN(sc) & 0xff)) {
  2301. IL_ERR("BUG_ON idx doesn't match seq control"
  2302. " idx=%d, seq_idx=%d, seq=%d\n", idx,
  2303. IEEE80211_SEQ_TO_SN(sc), hdr->seq_ctrl);
  2304. return -1;
  2305. }
  2306. D_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n", i, idx,
  2307. IEEE80211_SEQ_TO_SN(sc));
  2308. sh = idx - start;
  2309. if (sh > 64) {
  2310. sh = (start - idx) + 0xff;
  2311. bitmap = bitmap << sh;
  2312. sh = 0;
  2313. start = idx;
  2314. } else if (sh < -64)
  2315. sh = 0xff - (start - idx);
  2316. else if (sh < 0) {
  2317. sh = start - idx;
  2318. start = idx;
  2319. bitmap = bitmap << sh;
  2320. sh = 0;
  2321. }
  2322. bitmap |= 1ULL << sh;
  2323. D_TX_REPLY("start=%d bitmap=0x%llx\n", start,
  2324. (unsigned long long)bitmap);
  2325. }
  2326. agg->bitmap = bitmap;
  2327. agg->start_idx = start;
  2328. D_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
  2329. agg->frame_count, agg->start_idx,
  2330. (unsigned long long)agg->bitmap);
  2331. if (bitmap)
  2332. agg->wait_for_ba = 1;
  2333. }
  2334. return 0;
  2335. }
  2336. /*
  2337. * il4965_hdl_tx - Handle standard (non-aggregation) Tx response
  2338. */
  2339. static void
  2340. il4965_hdl_tx(struct il_priv *il, struct il_rx_buf *rxb)
  2341. {
  2342. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2343. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2344. int txq_id = SEQ_TO_QUEUE(sequence);
  2345. int idx = SEQ_TO_IDX(sequence);
  2346. struct il_tx_queue *txq = &il->txq[txq_id];
  2347. struct sk_buff *skb;
  2348. struct ieee80211_hdr *hdr;
  2349. struct ieee80211_tx_info *info;
  2350. struct il4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  2351. u32 status = le32_to_cpu(tx_resp->u.status);
  2352. int tid;
  2353. int sta_id;
  2354. int freed;
  2355. u8 *qc = NULL;
  2356. unsigned long flags;
  2357. if (idx >= txq->q.n_bd || il_queue_used(&txq->q, idx) == 0) {
  2358. IL_ERR("Read idx for DMA queue txq_id (%d) idx %d "
  2359. "is out of range [0-%d] %d %d\n", txq_id, idx,
  2360. txq->q.n_bd, txq->q.write_ptr, txq->q.read_ptr);
  2361. return;
  2362. }
  2363. txq->time_stamp = jiffies;
  2364. skb = txq->skbs[txq->q.read_ptr];
  2365. info = IEEE80211_SKB_CB(skb);
  2366. memset(&info->status, 0, sizeof(info->status));
  2367. hdr = (struct ieee80211_hdr *) skb->data;
  2368. if (ieee80211_is_data_qos(hdr->frame_control)) {
  2369. qc = ieee80211_get_qos_ctl(hdr);
  2370. tid = qc[0] & 0xf;
  2371. }
  2372. sta_id = il4965_get_ra_sta_id(il, hdr);
  2373. if (txq->sched_retry && unlikely(sta_id == IL_INVALID_STATION)) {
  2374. IL_ERR("Station not known\n");
  2375. return;
  2376. }
  2377. /*
  2378. * Firmware will not transmit frame on passive channel, if it not yet
  2379. * received some valid frame on that channel. When this error happen
  2380. * we have to wait until firmware will unblock itself i.e. when we
  2381. * note received beacon or other frame. We unblock queues in
  2382. * il4965_pass_packet_to_mac80211 or in il_mac_bss_info_changed.
  2383. */
  2384. if (unlikely((status & TX_STATUS_MSK) == TX_STATUS_FAIL_PASSIVE_NO_RX) &&
  2385. il->iw_mode == NL80211_IFTYPE_STATION) {
  2386. il_stop_queues_by_reason(il, IL_STOP_REASON_PASSIVE);
  2387. D_INFO("Stopped queues - RX waiting on passive channel\n");
  2388. }
  2389. spin_lock_irqsave(&il->sta_lock, flags);
  2390. if (txq->sched_retry) {
  2391. const u32 scd_ssn = il4965_get_scd_ssn(tx_resp);
  2392. struct il_ht_agg *agg;
  2393. if (WARN_ON(!qc))
  2394. goto out;
  2395. agg = &il->stations[sta_id].tid[tid].agg;
  2396. il4965_tx_status_reply_tx(il, agg, tx_resp, txq_id, idx);
  2397. /* check if BAR is needed */
  2398. if (tx_resp->frame_count == 1 &&
  2399. !il4965_is_tx_success(status))
  2400. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  2401. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  2402. idx = il_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  2403. D_TX_REPLY("Retry scheduler reclaim scd_ssn "
  2404. "%d idx %d\n", scd_ssn, idx);
  2405. freed = il4965_tx_queue_reclaim(il, txq_id, idx);
  2406. il4965_free_tfds_in_queue(il, sta_id, tid, freed);
  2407. if (il->mac80211_registered &&
  2408. il_queue_space(&txq->q) > txq->q.low_mark &&
  2409. agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
  2410. il_wake_queue(il, txq);
  2411. }
  2412. } else {
  2413. info->status.rates[0].count = tx_resp->failure_frame + 1;
  2414. info->flags |= il4965_tx_status_to_mac80211(status);
  2415. il4965_hwrate_to_tx_control(il,
  2416. le32_to_cpu(tx_resp->rate_n_flags),
  2417. info);
  2418. D_TX_REPLY("TXQ %d status %s (0x%08x) "
  2419. "rate_n_flags 0x%x retries %d\n", txq_id,
  2420. il4965_get_tx_fail_reason(status), status,
  2421. le32_to_cpu(tx_resp->rate_n_flags),
  2422. tx_resp->failure_frame);
  2423. freed = il4965_tx_queue_reclaim(il, txq_id, idx);
  2424. if (qc && likely(sta_id != IL_INVALID_STATION))
  2425. il4965_free_tfds_in_queue(il, sta_id, tid, freed);
  2426. else if (sta_id == IL_INVALID_STATION)
  2427. D_TX_REPLY("Station not known\n");
  2428. if (il->mac80211_registered &&
  2429. il_queue_space(&txq->q) > txq->q.low_mark)
  2430. il_wake_queue(il, txq);
  2431. }
  2432. out:
  2433. if (qc && likely(sta_id != IL_INVALID_STATION))
  2434. il4965_txq_check_empty(il, sta_id, tid, txq_id);
  2435. il4965_check_abort_status(il, tx_resp->frame_count, status);
  2436. spin_unlock_irqrestore(&il->sta_lock, flags);
  2437. }
  2438. /*
  2439. * translate ucode response to mac80211 tx status control values
  2440. */
  2441. void
  2442. il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
  2443. struct ieee80211_tx_info *info)
  2444. {
  2445. struct ieee80211_tx_rate *r = &info->status.rates[0];
  2446. info->status.antenna =
  2447. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  2448. if (rate_n_flags & RATE_MCS_HT_MSK)
  2449. r->flags |= IEEE80211_TX_RC_MCS;
  2450. if (rate_n_flags & RATE_MCS_GF_MSK)
  2451. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  2452. if (rate_n_flags & RATE_MCS_HT40_MSK)
  2453. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  2454. if (rate_n_flags & RATE_MCS_DUP_MSK)
  2455. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  2456. if (rate_n_flags & RATE_MCS_SGI_MSK)
  2457. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  2458. r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  2459. }
  2460. /*
  2461. * il4965_hdl_compressed_ba - Handler for N_COMPRESSED_BA
  2462. *
  2463. * Handles block-acknowledge notification from device, which reports success
  2464. * of frames sent via aggregation.
  2465. */
  2466. static void
  2467. il4965_hdl_compressed_ba(struct il_priv *il, struct il_rx_buf *rxb)
  2468. {
  2469. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2470. struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  2471. struct il_tx_queue *txq = NULL;
  2472. struct il_ht_agg *agg;
  2473. int idx;
  2474. int sta_id;
  2475. int tid;
  2476. unsigned long flags;
  2477. /* "flow" corresponds to Tx queue */
  2478. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  2479. /* "ssn" is start of block-ack Tx win, corresponds to idx
  2480. * (in Tx queue's circular buffer) of first TFD/frame in win */
  2481. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  2482. if (scd_flow >= il->hw_params.max_txq_num) {
  2483. IL_ERR("BUG_ON scd_flow is bigger than number of queues\n");
  2484. return;
  2485. }
  2486. txq = &il->txq[scd_flow];
  2487. sta_id = ba_resp->sta_id;
  2488. tid = ba_resp->tid;
  2489. agg = &il->stations[sta_id].tid[tid].agg;
  2490. if (unlikely(agg->txq_id != scd_flow)) {
  2491. /*
  2492. * FIXME: this is a uCode bug which need to be addressed,
  2493. * log the information and return for now!
  2494. * since it is possible happen very often and in order
  2495. * not to fill the syslog, don't enable the logging by default
  2496. */
  2497. D_TX_REPLY("BA scd_flow %d does not match txq_id %d\n",
  2498. scd_flow, agg->txq_id);
  2499. return;
  2500. }
  2501. /* Find idx just before block-ack win */
  2502. idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  2503. spin_lock_irqsave(&il->sta_lock, flags);
  2504. D_TX_REPLY("N_COMPRESSED_BA [%d] Received from %pM, " "sta_id = %d\n",
  2505. agg->wait_for_ba, (u8 *) &ba_resp->sta_addr_lo32,
  2506. ba_resp->sta_id);
  2507. D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx," "scd_flow = "
  2508. "%d, scd_ssn = %d\n", ba_resp->tid, ba_resp->seq_ctl,
  2509. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  2510. ba_resp->scd_flow, ba_resp->scd_ssn);
  2511. D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n", agg->start_idx,
  2512. (unsigned long long)agg->bitmap);
  2513. /* Update driver's record of ACK vs. not for each frame in win */
  2514. il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
  2515. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  2516. * block-ack win (we assume that they've been successfully
  2517. * transmitted ... if not, it's too late anyway). */
  2518. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  2519. /* calculate mac80211 ampdu sw queue to wake */
  2520. int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
  2521. il4965_free_tfds_in_queue(il, sta_id, tid, freed);
  2522. if (il_queue_space(&txq->q) > txq->q.low_mark &&
  2523. il->mac80211_registered &&
  2524. agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
  2525. il_wake_queue(il, txq);
  2526. il4965_txq_check_empty(il, sta_id, tid, scd_flow);
  2527. }
  2528. spin_unlock_irqrestore(&il->sta_lock, flags);
  2529. }
  2530. #ifdef CONFIG_IWLEGACY_DEBUG
  2531. const char *
  2532. il4965_get_tx_fail_reason(u32 status)
  2533. {
  2534. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  2535. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  2536. switch (status & TX_STATUS_MSK) {
  2537. case TX_STATUS_SUCCESS:
  2538. return "SUCCESS";
  2539. TX_STATUS_POSTPONE(DELAY);
  2540. TX_STATUS_POSTPONE(FEW_BYTES);
  2541. TX_STATUS_POSTPONE(QUIET_PERIOD);
  2542. TX_STATUS_POSTPONE(CALC_TTAK);
  2543. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  2544. TX_STATUS_FAIL(SHORT_LIMIT);
  2545. TX_STATUS_FAIL(LONG_LIMIT);
  2546. TX_STATUS_FAIL(FIFO_UNDERRUN);
  2547. TX_STATUS_FAIL(DRAIN_FLOW);
  2548. TX_STATUS_FAIL(RFKILL_FLUSH);
  2549. TX_STATUS_FAIL(LIFE_EXPIRE);
  2550. TX_STATUS_FAIL(DEST_PS);
  2551. TX_STATUS_FAIL(HOST_ABORTED);
  2552. TX_STATUS_FAIL(BT_RETRY);
  2553. TX_STATUS_FAIL(STA_INVALID);
  2554. TX_STATUS_FAIL(FRAG_DROPPED);
  2555. TX_STATUS_FAIL(TID_DISABLE);
  2556. TX_STATUS_FAIL(FIFO_FLUSHED);
  2557. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  2558. TX_STATUS_FAIL(PASSIVE_NO_RX);
  2559. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  2560. }
  2561. return "UNKNOWN";
  2562. #undef TX_STATUS_FAIL
  2563. #undef TX_STATUS_POSTPONE
  2564. }
  2565. #endif /* CONFIG_IWLEGACY_DEBUG */
  2566. static struct il_link_quality_cmd *
  2567. il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
  2568. {
  2569. int i, r;
  2570. struct il_link_quality_cmd *link_cmd;
  2571. u32 rate_flags = 0;
  2572. __le32 rate_n_flags;
  2573. link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
  2574. if (!link_cmd) {
  2575. IL_ERR("Unable to allocate memory for LQ cmd.\n");
  2576. return NULL;
  2577. }
  2578. /* Set up the rate scaling to start at selected rate, fall back
  2579. * all the way down to 1M in IEEE order, and then spin on 1M */
  2580. if (il->band == NL80211_BAND_5GHZ)
  2581. r = RATE_6M_IDX;
  2582. else
  2583. r = RATE_1M_IDX;
  2584. if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
  2585. rate_flags |= RATE_MCS_CCK_MSK;
  2586. rate_flags |=
  2587. il4965_first_antenna(il->hw_params.
  2588. valid_tx_ant) << RATE_MCS_ANT_POS;
  2589. rate_n_flags = cpu_to_le32(il_rates[r].plcp | rate_flags);
  2590. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  2591. link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
  2592. link_cmd->general_params.single_stream_ant_msk =
  2593. il4965_first_antenna(il->hw_params.valid_tx_ant);
  2594. link_cmd->general_params.dual_stream_ant_msk =
  2595. il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
  2596. valid_tx_ant);
  2597. if (!link_cmd->general_params.dual_stream_ant_msk) {
  2598. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  2599. } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
  2600. link_cmd->general_params.dual_stream_ant_msk =
  2601. il->hw_params.valid_tx_ant;
  2602. }
  2603. link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2604. link_cmd->agg_params.agg_time_limit =
  2605. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2606. link_cmd->sta_id = sta_id;
  2607. return link_cmd;
  2608. }
  2609. /*
  2610. * il4965_add_bssid_station - Add the special IBSS BSSID station
  2611. *
  2612. * Function sleeps.
  2613. */
  2614. int
  2615. il4965_add_bssid_station(struct il_priv *il, const u8 *addr, u8 *sta_id_r)
  2616. {
  2617. int ret;
  2618. u8 sta_id;
  2619. struct il_link_quality_cmd *link_cmd;
  2620. unsigned long flags;
  2621. if (sta_id_r)
  2622. *sta_id_r = IL_INVALID_STATION;
  2623. ret = il_add_station_common(il, addr, 0, NULL, &sta_id);
  2624. if (ret) {
  2625. IL_ERR("Unable to add station %pM\n", addr);
  2626. return ret;
  2627. }
  2628. if (sta_id_r)
  2629. *sta_id_r = sta_id;
  2630. spin_lock_irqsave(&il->sta_lock, flags);
  2631. il->stations[sta_id].used |= IL_STA_LOCAL;
  2632. spin_unlock_irqrestore(&il->sta_lock, flags);
  2633. /* Set up default rate scaling table in device's station table */
  2634. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  2635. if (!link_cmd) {
  2636. IL_ERR("Unable to initialize rate scaling for station %pM.\n",
  2637. addr);
  2638. return -ENOMEM;
  2639. }
  2640. ret = il_send_lq_cmd(il, link_cmd, CMD_SYNC, true);
  2641. if (ret)
  2642. IL_ERR("Link quality command failed (%d)\n", ret);
  2643. spin_lock_irqsave(&il->sta_lock, flags);
  2644. il->stations[sta_id].lq = link_cmd;
  2645. spin_unlock_irqrestore(&il->sta_lock, flags);
  2646. return 0;
  2647. }
  2648. static int
  2649. il4965_static_wepkey_cmd(struct il_priv *il, bool send_if_empty)
  2650. {
  2651. int i;
  2652. u8 buff[sizeof(struct il_wep_cmd) +
  2653. sizeof(struct il_wep_key) * WEP_KEYS_MAX];
  2654. struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
  2655. size_t cmd_size = sizeof(struct il_wep_cmd);
  2656. struct il_host_cmd cmd = {
  2657. .id = C_WEPKEY,
  2658. .data = wep_cmd,
  2659. .flags = CMD_SYNC,
  2660. };
  2661. bool not_empty = false;
  2662. might_sleep();
  2663. memset(wep_cmd, 0,
  2664. cmd_size + (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
  2665. for (i = 0; i < WEP_KEYS_MAX; i++) {
  2666. u8 key_size = il->_4965.wep_keys[i].key_size;
  2667. wep_cmd->key[i].key_idx = i;
  2668. if (key_size) {
  2669. wep_cmd->key[i].key_offset = i;
  2670. not_empty = true;
  2671. } else
  2672. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  2673. wep_cmd->key[i].key_size = key_size;
  2674. memcpy(&wep_cmd->key[i].key[3], il->_4965.wep_keys[i].key, key_size);
  2675. }
  2676. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  2677. wep_cmd->num_keys = WEP_KEYS_MAX;
  2678. cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
  2679. cmd.len = cmd_size;
  2680. if (not_empty || send_if_empty)
  2681. return il_send_cmd(il, &cmd);
  2682. else
  2683. return 0;
  2684. }
  2685. int
  2686. il4965_restore_default_wep_keys(struct il_priv *il)
  2687. {
  2688. lockdep_assert_held(&il->mutex);
  2689. return il4965_static_wepkey_cmd(il, false);
  2690. }
  2691. int
  2692. il4965_remove_default_wep_key(struct il_priv *il,
  2693. struct ieee80211_key_conf *keyconf)
  2694. {
  2695. int ret;
  2696. int idx = keyconf->keyidx;
  2697. lockdep_assert_held(&il->mutex);
  2698. D_WEP("Removing default WEP key: idx=%d\n", idx);
  2699. memset(&il->_4965.wep_keys[idx], 0, sizeof(struct il_wep_key));
  2700. if (il_is_rfkill(il)) {
  2701. D_WEP("Not sending C_WEPKEY command due to RFKILL.\n");
  2702. /* but keys in device are clear anyway so return success */
  2703. return 0;
  2704. }
  2705. ret = il4965_static_wepkey_cmd(il, 1);
  2706. D_WEP("Remove default WEP key: idx=%d ret=%d\n", idx, ret);
  2707. return ret;
  2708. }
  2709. int
  2710. il4965_set_default_wep_key(struct il_priv *il,
  2711. struct ieee80211_key_conf *keyconf)
  2712. {
  2713. int ret;
  2714. int len = keyconf->keylen;
  2715. int idx = keyconf->keyidx;
  2716. lockdep_assert_held(&il->mutex);
  2717. if (len != WEP_KEY_LEN_128 && len != WEP_KEY_LEN_64) {
  2718. D_WEP("Bad WEP key length %d\n", keyconf->keylen);
  2719. return -EINVAL;
  2720. }
  2721. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  2722. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  2723. il->stations[IL_AP_ID].keyinfo.cipher = keyconf->cipher;
  2724. il->_4965.wep_keys[idx].key_size = len;
  2725. memcpy(&il->_4965.wep_keys[idx].key, &keyconf->key, len);
  2726. ret = il4965_static_wepkey_cmd(il, false);
  2727. D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n", len, idx, ret);
  2728. return ret;
  2729. }
  2730. static int
  2731. il4965_set_wep_dynamic_key_info(struct il_priv *il,
  2732. struct ieee80211_key_conf *keyconf, u8 sta_id)
  2733. {
  2734. unsigned long flags;
  2735. __le16 key_flags = 0;
  2736. struct il_addsta_cmd sta_cmd;
  2737. lockdep_assert_held(&il->mutex);
  2738. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  2739. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  2740. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  2741. key_flags &= ~STA_KEY_FLG_INVALID;
  2742. if (keyconf->keylen == WEP_KEY_LEN_128)
  2743. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  2744. if (sta_id == il->hw_params.bcast_id)
  2745. key_flags |= STA_KEY_MULTICAST_MSK;
  2746. spin_lock_irqsave(&il->sta_lock, flags);
  2747. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  2748. il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  2749. il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  2750. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
  2751. memcpy(&il->stations[sta_id].sta.key.key[3], keyconf->key,
  2752. keyconf->keylen);
  2753. if ((il->stations[sta_id].sta.key.
  2754. key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
  2755. il->stations[sta_id].sta.key.key_offset =
  2756. il_get_free_ucode_key_idx(il);
  2757. /* else, we are overriding an existing key => no need to allocated room
  2758. * in uCode. */
  2759. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  2760. "no space for a new key");
  2761. il->stations[sta_id].sta.key.key_flags = key_flags;
  2762. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  2763. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2764. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  2765. sizeof(struct il_addsta_cmd));
  2766. spin_unlock_irqrestore(&il->sta_lock, flags);
  2767. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  2768. }
  2769. static int
  2770. il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
  2771. struct ieee80211_key_conf *keyconf, u8 sta_id)
  2772. {
  2773. unsigned long flags;
  2774. __le16 key_flags = 0;
  2775. struct il_addsta_cmd sta_cmd;
  2776. lockdep_assert_held(&il->mutex);
  2777. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  2778. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  2779. key_flags &= ~STA_KEY_FLG_INVALID;
  2780. if (sta_id == il->hw_params.bcast_id)
  2781. key_flags |= STA_KEY_MULTICAST_MSK;
  2782. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2783. spin_lock_irqsave(&il->sta_lock, flags);
  2784. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  2785. il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  2786. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
  2787. memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
  2788. if ((il->stations[sta_id].sta.key.
  2789. key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
  2790. il->stations[sta_id].sta.key.key_offset =
  2791. il_get_free_ucode_key_idx(il);
  2792. /* else, we are overriding an existing key => no need to allocated room
  2793. * in uCode. */
  2794. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  2795. "no space for a new key");
  2796. il->stations[sta_id].sta.key.key_flags = key_flags;
  2797. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  2798. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2799. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  2800. sizeof(struct il_addsta_cmd));
  2801. spin_unlock_irqrestore(&il->sta_lock, flags);
  2802. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  2803. }
  2804. static int
  2805. il4965_set_tkip_dynamic_key_info(struct il_priv *il,
  2806. struct ieee80211_key_conf *keyconf, u8 sta_id)
  2807. {
  2808. unsigned long flags;
  2809. __le16 key_flags = 0;
  2810. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  2811. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  2812. key_flags &= ~STA_KEY_FLG_INVALID;
  2813. if (sta_id == il->hw_params.bcast_id)
  2814. key_flags |= STA_KEY_MULTICAST_MSK;
  2815. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2816. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2817. spin_lock_irqsave(&il->sta_lock, flags);
  2818. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  2819. il->stations[sta_id].keyinfo.keylen = 16;
  2820. if ((il->stations[sta_id].sta.key.
  2821. key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
  2822. il->stations[sta_id].sta.key.key_offset =
  2823. il_get_free_ucode_key_idx(il);
  2824. /* else, we are overriding an existing key => no need to allocated room
  2825. * in uCode. */
  2826. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  2827. "no space for a new key");
  2828. il->stations[sta_id].sta.key.key_flags = key_flags;
  2829. /* This copy is acutally not needed: we get the key with each TX */
  2830. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
  2831. memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
  2832. spin_unlock_irqrestore(&il->sta_lock, flags);
  2833. return 0;
  2834. }
  2835. void
  2836. il4965_update_tkip_key(struct il_priv *il, struct ieee80211_key_conf *keyconf,
  2837. struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
  2838. {
  2839. u8 sta_id;
  2840. unsigned long flags;
  2841. int i;
  2842. if (il_scan_cancel(il)) {
  2843. /* cancel scan failed, just live w/ bad key and rely
  2844. briefly on SW decryption */
  2845. return;
  2846. }
  2847. sta_id = il_sta_id_or_broadcast(il, sta);
  2848. if (sta_id == IL_INVALID_STATION)
  2849. return;
  2850. spin_lock_irqsave(&il->sta_lock, flags);
  2851. il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  2852. for (i = 0; i < 5; i++)
  2853. il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  2854. cpu_to_le16(phase1key[i]);
  2855. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  2856. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2857. il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
  2858. spin_unlock_irqrestore(&il->sta_lock, flags);
  2859. }
  2860. int
  2861. il4965_remove_dynamic_key(struct il_priv *il,
  2862. struct ieee80211_key_conf *keyconf, u8 sta_id)
  2863. {
  2864. unsigned long flags;
  2865. u16 key_flags;
  2866. u8 keyidx;
  2867. struct il_addsta_cmd sta_cmd;
  2868. lockdep_assert_held(&il->mutex);
  2869. il->_4965.key_mapping_keys--;
  2870. spin_lock_irqsave(&il->sta_lock, flags);
  2871. key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
  2872. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  2873. D_WEP("Remove dynamic key: idx=%d sta=%d\n", keyconf->keyidx, sta_id);
  2874. if (keyconf->keyidx != keyidx) {
  2875. /* We need to remove a key with idx different that the one
  2876. * in the uCode. This means that the key we need to remove has
  2877. * been replaced by another one with different idx.
  2878. * Don't do anything and return ok
  2879. */
  2880. spin_unlock_irqrestore(&il->sta_lock, flags);
  2881. return 0;
  2882. }
  2883. if (il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_INVALID) {
  2884. IL_WARN("Removing wrong key %d 0x%x\n", keyconf->keyidx,
  2885. key_flags);
  2886. spin_unlock_irqrestore(&il->sta_lock, flags);
  2887. return 0;
  2888. }
  2889. if (!test_and_clear_bit
  2890. (il->stations[sta_id].sta.key.key_offset, &il->ucode_key_table))
  2891. IL_ERR("idx %d not used in uCode key table.\n",
  2892. il->stations[sta_id].sta.key.key_offset);
  2893. memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
  2894. memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
  2895. il->stations[sta_id].sta.key.key_flags =
  2896. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  2897. il->stations[sta_id].sta.key.key_offset = keyconf->hw_key_idx;
  2898. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  2899. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2900. if (il_is_rfkill(il)) {
  2901. D_WEP
  2902. ("Not sending C_ADD_STA command because RFKILL enabled.\n");
  2903. spin_unlock_irqrestore(&il->sta_lock, flags);
  2904. return 0;
  2905. }
  2906. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  2907. sizeof(struct il_addsta_cmd));
  2908. spin_unlock_irqrestore(&il->sta_lock, flags);
  2909. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  2910. }
  2911. int
  2912. il4965_set_dynamic_key(struct il_priv *il, struct ieee80211_key_conf *keyconf,
  2913. u8 sta_id)
  2914. {
  2915. int ret;
  2916. lockdep_assert_held(&il->mutex);
  2917. il->_4965.key_mapping_keys++;
  2918. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  2919. switch (keyconf->cipher) {
  2920. case WLAN_CIPHER_SUITE_CCMP:
  2921. ret =
  2922. il4965_set_ccmp_dynamic_key_info(il, keyconf, sta_id);
  2923. break;
  2924. case WLAN_CIPHER_SUITE_TKIP:
  2925. ret =
  2926. il4965_set_tkip_dynamic_key_info(il, keyconf, sta_id);
  2927. break;
  2928. case WLAN_CIPHER_SUITE_WEP40:
  2929. case WLAN_CIPHER_SUITE_WEP104:
  2930. ret = il4965_set_wep_dynamic_key_info(il, keyconf, sta_id);
  2931. break;
  2932. default:
  2933. IL_ERR("Unknown alg: %s cipher = %x\n", __func__,
  2934. keyconf->cipher);
  2935. ret = -EINVAL;
  2936. }
  2937. D_WEP("Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
  2938. keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
  2939. return ret;
  2940. }
  2941. /*
  2942. * il4965_alloc_bcast_station - add broadcast station into driver's station table.
  2943. *
  2944. * This adds the broadcast station into the driver's station table
  2945. * and marks it driver active, so that it will be restored to the
  2946. * device at the next best time.
  2947. */
  2948. int
  2949. il4965_alloc_bcast_station(struct il_priv *il)
  2950. {
  2951. struct il_link_quality_cmd *link_cmd;
  2952. unsigned long flags;
  2953. u8 sta_id;
  2954. spin_lock_irqsave(&il->sta_lock, flags);
  2955. sta_id = il_prep_station(il, il_bcast_addr, false, NULL);
  2956. if (sta_id == IL_INVALID_STATION) {
  2957. IL_ERR("Unable to prepare broadcast station\n");
  2958. spin_unlock_irqrestore(&il->sta_lock, flags);
  2959. return -EINVAL;
  2960. }
  2961. il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
  2962. il->stations[sta_id].used |= IL_STA_BCAST;
  2963. spin_unlock_irqrestore(&il->sta_lock, flags);
  2964. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  2965. if (!link_cmd) {
  2966. IL_ERR
  2967. ("Unable to initialize rate scaling for bcast station.\n");
  2968. return -ENOMEM;
  2969. }
  2970. spin_lock_irqsave(&il->sta_lock, flags);
  2971. il->stations[sta_id].lq = link_cmd;
  2972. spin_unlock_irqrestore(&il->sta_lock, flags);
  2973. return 0;
  2974. }
  2975. /*
  2976. * il4965_update_bcast_station - update broadcast station's LQ command
  2977. *
  2978. * Only used by iwl4965. Placed here to have all bcast station management
  2979. * code together.
  2980. */
  2981. static int
  2982. il4965_update_bcast_station(struct il_priv *il)
  2983. {
  2984. unsigned long flags;
  2985. struct il_link_quality_cmd *link_cmd;
  2986. u8 sta_id = il->hw_params.bcast_id;
  2987. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  2988. if (!link_cmd) {
  2989. IL_ERR("Unable to initialize rate scaling for bcast sta.\n");
  2990. return -ENOMEM;
  2991. }
  2992. spin_lock_irqsave(&il->sta_lock, flags);
  2993. if (il->stations[sta_id].lq)
  2994. kfree(il->stations[sta_id].lq);
  2995. else
  2996. D_INFO("Bcast sta rate scaling has not been initialized.\n");
  2997. il->stations[sta_id].lq = link_cmd;
  2998. spin_unlock_irqrestore(&il->sta_lock, flags);
  2999. return 0;
  3000. }
  3001. int
  3002. il4965_update_bcast_stations(struct il_priv *il)
  3003. {
  3004. return il4965_update_bcast_station(il);
  3005. }
  3006. /*
  3007. * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  3008. */
  3009. int
  3010. il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
  3011. {
  3012. unsigned long flags;
  3013. struct il_addsta_cmd sta_cmd;
  3014. lockdep_assert_held(&il->mutex);
  3015. /* Remove "disable" flag, to enable Tx for this TID */
  3016. spin_lock_irqsave(&il->sta_lock, flags);
  3017. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  3018. il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  3019. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3020. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  3021. sizeof(struct il_addsta_cmd));
  3022. spin_unlock_irqrestore(&il->sta_lock, flags);
  3023. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  3024. }
  3025. int
  3026. il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta, int tid,
  3027. u16 ssn)
  3028. {
  3029. unsigned long flags;
  3030. int sta_id;
  3031. struct il_addsta_cmd sta_cmd;
  3032. lockdep_assert_held(&il->mutex);
  3033. sta_id = il_sta_id(sta);
  3034. if (sta_id == IL_INVALID_STATION)
  3035. return -ENXIO;
  3036. spin_lock_irqsave(&il->sta_lock, flags);
  3037. il->stations[sta_id].sta.station_flags_msk = 0;
  3038. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  3039. il->stations[sta_id].sta.add_immediate_ba_tid = (u8) tid;
  3040. il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  3041. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3042. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  3043. sizeof(struct il_addsta_cmd));
  3044. spin_unlock_irqrestore(&il->sta_lock, flags);
  3045. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  3046. }
  3047. int
  3048. il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta, int tid)
  3049. {
  3050. unsigned long flags;
  3051. int sta_id;
  3052. struct il_addsta_cmd sta_cmd;
  3053. lockdep_assert_held(&il->mutex);
  3054. sta_id = il_sta_id(sta);
  3055. if (sta_id == IL_INVALID_STATION) {
  3056. IL_ERR("Invalid station for AGG tid %d\n", tid);
  3057. return -ENXIO;
  3058. }
  3059. spin_lock_irqsave(&il->sta_lock, flags);
  3060. il->stations[sta_id].sta.station_flags_msk = 0;
  3061. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  3062. il->stations[sta_id].sta.remove_immediate_ba_tid = (u8) tid;
  3063. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3064. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  3065. sizeof(struct il_addsta_cmd));
  3066. spin_unlock_irqrestore(&il->sta_lock, flags);
  3067. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  3068. }
  3069. void
  3070. il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
  3071. {
  3072. unsigned long flags;
  3073. spin_lock_irqsave(&il->sta_lock, flags);
  3074. il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  3075. il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  3076. il->stations[sta_id].sta.sta.modify_mask =
  3077. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  3078. il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  3079. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3080. il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
  3081. spin_unlock_irqrestore(&il->sta_lock, flags);
  3082. }
  3083. void
  3084. il4965_update_chain_flags(struct il_priv *il)
  3085. {
  3086. if (il->ops->set_rxon_chain) {
  3087. il->ops->set_rxon_chain(il);
  3088. if (il->active.rx_chain != il->staging.rx_chain)
  3089. il_commit_rxon(il);
  3090. }
  3091. }
  3092. static void
  3093. il4965_clear_free_frames(struct il_priv *il)
  3094. {
  3095. struct list_head *element;
  3096. D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
  3097. while (!list_empty(&il->free_frames)) {
  3098. element = il->free_frames.next;
  3099. list_del(element);
  3100. kfree(list_entry(element, struct il_frame, list));
  3101. il->frames_count--;
  3102. }
  3103. if (il->frames_count) {
  3104. IL_WARN("%d frames still in use. Did we lose one?\n",
  3105. il->frames_count);
  3106. il->frames_count = 0;
  3107. }
  3108. }
  3109. static struct il_frame *
  3110. il4965_get_free_frame(struct il_priv *il)
  3111. {
  3112. struct il_frame *frame;
  3113. struct list_head *element;
  3114. if (list_empty(&il->free_frames)) {
  3115. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  3116. if (!frame) {
  3117. IL_ERR("Could not allocate frame!\n");
  3118. return NULL;
  3119. }
  3120. il->frames_count++;
  3121. return frame;
  3122. }
  3123. element = il->free_frames.next;
  3124. list_del(element);
  3125. return list_entry(element, struct il_frame, list);
  3126. }
  3127. static void
  3128. il4965_free_frame(struct il_priv *il, struct il_frame *frame)
  3129. {
  3130. memset(frame, 0, sizeof(*frame));
  3131. list_add(&frame->list, &il->free_frames);
  3132. }
  3133. static u32
  3134. il4965_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
  3135. int left)
  3136. {
  3137. lockdep_assert_held(&il->mutex);
  3138. if (!il->beacon_skb)
  3139. return 0;
  3140. if (il->beacon_skb->len > left)
  3141. return 0;
  3142. memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
  3143. return il->beacon_skb->len;
  3144. }
  3145. /* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
  3146. static void
  3147. il4965_set_beacon_tim(struct il_priv *il,
  3148. struct il_tx_beacon_cmd *tx_beacon_cmd, u8 * beacon,
  3149. u32 frame_size)
  3150. {
  3151. u16 tim_idx;
  3152. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  3153. /*
  3154. * The idx is relative to frame start but we start looking at the
  3155. * variable-length part of the beacon.
  3156. */
  3157. tim_idx = mgmt->u.beacon.variable - beacon;
  3158. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  3159. while ((tim_idx < (frame_size - 2)) &&
  3160. (beacon[tim_idx] != WLAN_EID_TIM))
  3161. tim_idx += beacon[tim_idx + 1] + 2;
  3162. /* If TIM field was found, set variables */
  3163. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  3164. tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
  3165. tx_beacon_cmd->tim_size = beacon[tim_idx + 1];
  3166. } else
  3167. IL_WARN("Unable to find TIM Element in beacon\n");
  3168. }
  3169. static unsigned int
  3170. il4965_hw_get_beacon_cmd(struct il_priv *il, struct il_frame *frame)
  3171. {
  3172. struct il_tx_beacon_cmd *tx_beacon_cmd;
  3173. u32 frame_size;
  3174. u32 rate_flags;
  3175. u32 rate;
  3176. /*
  3177. * We have to set up the TX command, the TX Beacon command, and the
  3178. * beacon contents.
  3179. */
  3180. lockdep_assert_held(&il->mutex);
  3181. if (!il->beacon_enabled) {
  3182. IL_ERR("Trying to build beacon without beaconing enabled\n");
  3183. return 0;
  3184. }
  3185. /* Initialize memory */
  3186. tx_beacon_cmd = &frame->u.beacon;
  3187. memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
  3188. /* Set up TX beacon contents */
  3189. frame_size =
  3190. il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
  3191. sizeof(frame->u) - sizeof(*tx_beacon_cmd));
  3192. if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
  3193. return 0;
  3194. if (!frame_size)
  3195. return 0;
  3196. /* Set up TX command fields */
  3197. tx_beacon_cmd->tx.len = cpu_to_le16((u16) frame_size);
  3198. tx_beacon_cmd->tx.sta_id = il->hw_params.bcast_id;
  3199. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  3200. tx_beacon_cmd->tx.tx_flags =
  3201. TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK |
  3202. TX_CMD_FLG_STA_RATE_MSK;
  3203. /* Set up TX beacon command fields */
  3204. il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *) tx_beacon_cmd->frame,
  3205. frame_size);
  3206. /* Set up packet rate and flags */
  3207. rate = il_get_lowest_plcp(il);
  3208. il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
  3209. rate_flags = BIT(il->mgmt_tx_ant) << RATE_MCS_ANT_POS;
  3210. if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
  3211. rate_flags |= RATE_MCS_CCK_MSK;
  3212. tx_beacon_cmd->tx.rate_n_flags = cpu_to_le32(rate | rate_flags);
  3213. return sizeof(*tx_beacon_cmd) + frame_size;
  3214. }
  3215. int
  3216. il4965_send_beacon_cmd(struct il_priv *il)
  3217. {
  3218. struct il_frame *frame;
  3219. unsigned int frame_size;
  3220. int rc;
  3221. frame = il4965_get_free_frame(il);
  3222. if (!frame) {
  3223. IL_ERR("Could not obtain free frame buffer for beacon "
  3224. "command.\n");
  3225. return -ENOMEM;
  3226. }
  3227. frame_size = il4965_hw_get_beacon_cmd(il, frame);
  3228. if (!frame_size) {
  3229. IL_ERR("Error configuring the beacon command\n");
  3230. il4965_free_frame(il, frame);
  3231. return -EINVAL;
  3232. }
  3233. rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
  3234. il4965_free_frame(il, frame);
  3235. return rc;
  3236. }
  3237. static inline dma_addr_t
  3238. il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
  3239. {
  3240. struct il_tfd_tb *tb = &tfd->tbs[idx];
  3241. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  3242. if (sizeof(dma_addr_t) > sizeof(u32))
  3243. addr |=
  3244. ((dma_addr_t) (le16_to_cpu(tb->hi_n_len) & 0xF) << 16) <<
  3245. 16;
  3246. return addr;
  3247. }
  3248. static inline u16
  3249. il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
  3250. {
  3251. struct il_tfd_tb *tb = &tfd->tbs[idx];
  3252. return le16_to_cpu(tb->hi_n_len) >> 4;
  3253. }
  3254. static inline void
  3255. il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx, dma_addr_t addr, u16 len)
  3256. {
  3257. struct il_tfd_tb *tb = &tfd->tbs[idx];
  3258. u16 hi_n_len = len << 4;
  3259. put_unaligned_le32(addr, &tb->lo);
  3260. if (sizeof(dma_addr_t) > sizeof(u32))
  3261. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  3262. tb->hi_n_len = cpu_to_le16(hi_n_len);
  3263. tfd->num_tbs = idx + 1;
  3264. }
  3265. static inline u8
  3266. il4965_tfd_get_num_tbs(struct il_tfd *tfd)
  3267. {
  3268. return tfd->num_tbs & 0x1f;
  3269. }
  3270. /*
  3271. * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  3272. *
  3273. * Does NOT advance any TFD circular buffer read/write idxes
  3274. * Does NOT free the TFD itself (which is within circular buffer)
  3275. */
  3276. void
  3277. il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
  3278. {
  3279. struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
  3280. struct il_tfd *tfd;
  3281. struct pci_dev *dev = il->pci_dev;
  3282. int idx = txq->q.read_ptr;
  3283. int i;
  3284. int num_tbs;
  3285. tfd = &tfd_tmp[idx];
  3286. /* Sanity check on number of chunks */
  3287. num_tbs = il4965_tfd_get_num_tbs(tfd);
  3288. if (num_tbs >= IL_NUM_OF_TBS) {
  3289. IL_ERR("Too many chunks: %i\n", num_tbs);
  3290. /* @todo issue fatal error, it is quite serious situation */
  3291. return;
  3292. }
  3293. /* Unmap tx_cmd */
  3294. if (num_tbs)
  3295. dma_unmap_single(&dev->dev,
  3296. dma_unmap_addr(&txq->meta[idx], mapping),
  3297. dma_unmap_len(&txq->meta[idx], len),
  3298. DMA_BIDIRECTIONAL);
  3299. /* Unmap chunks, if any. */
  3300. for (i = 1; i < num_tbs; i++)
  3301. dma_unmap_single(&dev->dev, il4965_tfd_tb_get_addr(tfd, i),
  3302. il4965_tfd_tb_get_len(tfd, i), DMA_TO_DEVICE);
  3303. /* free SKB */
  3304. if (txq->skbs) {
  3305. struct sk_buff *skb = txq->skbs[txq->q.read_ptr];
  3306. /* can be called from irqs-disabled context */
  3307. if (skb) {
  3308. dev_kfree_skb_any(skb);
  3309. txq->skbs[txq->q.read_ptr] = NULL;
  3310. }
  3311. }
  3312. }
  3313. int
  3314. il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il, struct il_tx_queue *txq,
  3315. dma_addr_t addr, u16 len, u8 reset, u8 pad)
  3316. {
  3317. struct il_queue *q;
  3318. struct il_tfd *tfd, *tfd_tmp;
  3319. u32 num_tbs;
  3320. q = &txq->q;
  3321. tfd_tmp = (struct il_tfd *)txq->tfds;
  3322. tfd = &tfd_tmp[q->write_ptr];
  3323. if (reset)
  3324. memset(tfd, 0, sizeof(*tfd));
  3325. num_tbs = il4965_tfd_get_num_tbs(tfd);
  3326. /* Each TFD can point to a maximum 20 Tx buffers */
  3327. if (num_tbs >= IL_NUM_OF_TBS) {
  3328. IL_ERR("Error can not send more than %d chunks\n",
  3329. IL_NUM_OF_TBS);
  3330. return -EINVAL;
  3331. }
  3332. BUG_ON(addr & ~DMA_BIT_MASK(36));
  3333. if (unlikely(addr & ~IL_TX_DMA_MASK))
  3334. IL_ERR("Unaligned address = %llx\n", (unsigned long long)addr);
  3335. il4965_tfd_set_tb(tfd, num_tbs, addr, len);
  3336. return 0;
  3337. }
  3338. /*
  3339. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  3340. * given Tx queue, and enable the DMA channel used for that queue.
  3341. *
  3342. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  3343. * channels supported in hardware.
  3344. */
  3345. int
  3346. il4965_hw_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq)
  3347. {
  3348. int txq_id = txq->q.id;
  3349. /* Circular buffer (TFD queue in DRAM) physical base address */
  3350. il_wr(il, FH49_MEM_CBBC_QUEUE(txq_id), txq->q.dma_addr >> 8);
  3351. return 0;
  3352. }
  3353. /******************************************************************************
  3354. *
  3355. * Generic RX handler implementations
  3356. *
  3357. ******************************************************************************/
  3358. static void
  3359. il4965_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
  3360. {
  3361. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3362. struct il_alive_resp *palive;
  3363. struct delayed_work *pwork;
  3364. palive = &pkt->u.alive_frame;
  3365. D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
  3366. palive->is_valid, palive->ver_type, palive->ver_subtype);
  3367. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  3368. D_INFO("Initialization Alive received.\n");
  3369. memcpy(&il->card_alive_init, &pkt->u.raw,
  3370. sizeof(struct il_init_alive_resp));
  3371. pwork = &il->init_alive_start;
  3372. } else {
  3373. D_INFO("Runtime Alive received.\n");
  3374. memcpy(&il->card_alive, &pkt->u.alive_frame,
  3375. sizeof(struct il_alive_resp));
  3376. pwork = &il->alive_start;
  3377. }
  3378. /* We delay the ALIVE response by 5ms to
  3379. * give the HW RF Kill time to activate... */
  3380. if (palive->is_valid == UCODE_VALID_OK)
  3381. queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
  3382. else
  3383. IL_WARN("uCode did not respond OK.\n");
  3384. }
  3385. /*
  3386. * il4965_bg_stats_periodic - Timer callback to queue stats
  3387. *
  3388. * This callback is provided in order to send a stats request.
  3389. *
  3390. * This timer function is continually reset to execute within
  3391. * 60 seconds since the last N_STATS was received. We need to
  3392. * ensure we receive the stats in order to update the temperature
  3393. * used for calibrating the TXPOWER.
  3394. */
  3395. static void
  3396. il4965_bg_stats_periodic(struct timer_list *t)
  3397. {
  3398. struct il_priv *il = from_timer(il, t, stats_periodic);
  3399. if (test_bit(S_EXIT_PENDING, &il->status))
  3400. return;
  3401. /* dont send host command if rf-kill is on */
  3402. if (!il_is_ready_rf(il))
  3403. return;
  3404. il_send_stats_request(il, CMD_ASYNC, false);
  3405. }
  3406. static void
  3407. il4965_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
  3408. {
  3409. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3410. struct il4965_beacon_notif *beacon =
  3411. (struct il4965_beacon_notif *)pkt->u.raw;
  3412. #ifdef CONFIG_IWLEGACY_DEBUG
  3413. u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
  3414. D_RX("beacon status %x retries %d iss %d tsf:0x%.8x%.8x rate %d\n",
  3415. le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
  3416. beacon->beacon_notify_hdr.failure_frame,
  3417. le32_to_cpu(beacon->ibss_mgr_status),
  3418. le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
  3419. #endif
  3420. il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  3421. }
  3422. static void
  3423. il4965_perform_ct_kill_task(struct il_priv *il)
  3424. {
  3425. unsigned long flags;
  3426. D_POWER("Stop all queues\n");
  3427. if (il->mac80211_registered)
  3428. ieee80211_stop_queues(il->hw);
  3429. _il_wr(il, CSR_UCODE_DRV_GP1_SET,
  3430. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  3431. _il_rd(il, CSR_UCODE_DRV_GP1);
  3432. spin_lock_irqsave(&il->reg_lock, flags);
  3433. if (likely(_il_grab_nic_access(il)))
  3434. _il_release_nic_access(il);
  3435. spin_unlock_irqrestore(&il->reg_lock, flags);
  3436. }
  3437. /* Handle notification from uCode that card's power state is changing
  3438. * due to software, hardware, or critical temperature RFKILL */
  3439. static void
  3440. il4965_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
  3441. {
  3442. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3443. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  3444. unsigned long status = il->status;
  3445. D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
  3446. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  3447. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  3448. (flags & CT_CARD_DISABLED) ? "Reached" : "Not reached");
  3449. if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED | CT_CARD_DISABLED)) {
  3450. _il_wr(il, CSR_UCODE_DRV_GP1_SET,
  3451. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  3452. il_wr(il, HBUS_TARG_MBX_C, HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  3453. if (!(flags & RXON_CARD_DISABLED)) {
  3454. _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
  3455. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  3456. il_wr(il, HBUS_TARG_MBX_C,
  3457. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  3458. }
  3459. }
  3460. if (flags & CT_CARD_DISABLED)
  3461. il4965_perform_ct_kill_task(il);
  3462. if (flags & HW_CARD_DISABLED)
  3463. set_bit(S_RFKILL, &il->status);
  3464. else
  3465. clear_bit(S_RFKILL, &il->status);
  3466. if (!(flags & RXON_CARD_DISABLED))
  3467. il_scan_cancel(il);
  3468. if ((test_bit(S_RFKILL, &status) !=
  3469. test_bit(S_RFKILL, &il->status)))
  3470. wiphy_rfkill_set_hw_state(il->hw->wiphy,
  3471. test_bit(S_RFKILL, &il->status));
  3472. else
  3473. wake_up(&il->wait_command_queue);
  3474. }
  3475. /*
  3476. * il4965_setup_handlers - Initialize Rx handler callbacks
  3477. *
  3478. * Setup the RX handlers for each of the reply types sent from the uCode
  3479. * to the host.
  3480. *
  3481. * This function chains into the hardware specific files for them to setup
  3482. * any hardware specific handlers as well.
  3483. */
  3484. static void
  3485. il4965_setup_handlers(struct il_priv *il)
  3486. {
  3487. il->handlers[N_ALIVE] = il4965_hdl_alive;
  3488. il->handlers[N_ERROR] = il_hdl_error;
  3489. il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
  3490. il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
  3491. il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
  3492. il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
  3493. il->handlers[N_BEACON] = il4965_hdl_beacon;
  3494. /*
  3495. * The same handler is used for both the REPLY to a discrete
  3496. * stats request from the host as well as for the periodic
  3497. * stats notifications (after received beacons) from the uCode.
  3498. */
  3499. il->handlers[C_STATS] = il4965_hdl_c_stats;
  3500. il->handlers[N_STATS] = il4965_hdl_stats;
  3501. il_setup_rx_scan_handlers(il);
  3502. /* status change handler */
  3503. il->handlers[N_CARD_STATE] = il4965_hdl_card_state;
  3504. il->handlers[N_MISSED_BEACONS] = il4965_hdl_missed_beacon;
  3505. /* Rx handlers */
  3506. il->handlers[N_RX_PHY] = il4965_hdl_rx_phy;
  3507. il->handlers[N_RX_MPDU] = il4965_hdl_rx;
  3508. il->handlers[N_RX] = il4965_hdl_rx;
  3509. /* block ack */
  3510. il->handlers[N_COMPRESSED_BA] = il4965_hdl_compressed_ba;
  3511. /* Tx response */
  3512. il->handlers[C_TX] = il4965_hdl_tx;
  3513. }
  3514. /*
  3515. * il4965_rx_handle - Main entry function for receiving responses from uCode
  3516. *
  3517. * Uses the il->handlers callback function array to invoke
  3518. * the appropriate handlers, including command responses,
  3519. * frame-received notifications, and other notifications.
  3520. */
  3521. void
  3522. il4965_rx_handle(struct il_priv *il)
  3523. {
  3524. struct il_rx_buf *rxb;
  3525. struct il_rx_pkt *pkt;
  3526. struct il_rx_queue *rxq = &il->rxq;
  3527. u32 r, i;
  3528. int reclaim;
  3529. unsigned long flags;
  3530. u8 fill_rx = 0;
  3531. u32 count = 8;
  3532. int total_empty;
  3533. /* uCode's read idx (stored in shared DRAM) indicates the last Rx
  3534. * buffer that the driver may process (last buffer filled by ucode). */
  3535. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  3536. i = rxq->read;
  3537. /* Rx interrupt, but nothing sent from uCode */
  3538. if (i == r)
  3539. D_RX("r = %d, i = %d\n", r, i);
  3540. /* calculate total frames need to be restock after handling RX */
  3541. total_empty = r - rxq->write_actual;
  3542. if (total_empty < 0)
  3543. total_empty += RX_QUEUE_SIZE;
  3544. if (total_empty > (RX_QUEUE_SIZE / 2))
  3545. fill_rx = 1;
  3546. while (i != r) {
  3547. rxb = rxq->queue[i];
  3548. /* If an RXB doesn't have a Rx queue slot associated with it,
  3549. * then a bug has been introduced in the queue refilling
  3550. * routines -- catch it here */
  3551. BUG_ON(rxb == NULL);
  3552. rxq->queue[i] = NULL;
  3553. dma_unmap_page(&il->pci_dev->dev, rxb->page_dma,
  3554. PAGE_SIZE << il->hw_params.rx_page_order,
  3555. DMA_FROM_DEVICE);
  3556. pkt = rxb_addr(rxb);
  3557. reclaim = il_need_reclaim(il, pkt);
  3558. /* Based on type of command response or notification,
  3559. * handle those that need handling via function in
  3560. * handlers table. See il4965_setup_handlers() */
  3561. if (il->handlers[pkt->hdr.cmd]) {
  3562. D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
  3563. il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  3564. il->isr_stats.handlers[pkt->hdr.cmd]++;
  3565. il->handlers[pkt->hdr.cmd] (il, rxb);
  3566. } else {
  3567. /* No handling needed */
  3568. D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
  3569. i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  3570. }
  3571. /*
  3572. * XXX: After here, we should always check rxb->page
  3573. * against NULL before touching it or its virtual
  3574. * memory (pkt). Because some handler might have
  3575. * already taken or freed the pages.
  3576. */
  3577. if (reclaim) {
  3578. /* Invoke any callbacks, transfer the buffer to caller,
  3579. * and fire off the (possibly) blocking il_send_cmd()
  3580. * as we reclaim the driver command queue */
  3581. if (rxb->page)
  3582. il_tx_cmd_complete(il, rxb);
  3583. else
  3584. IL_WARN("Claim null rxb?\n");
  3585. }
  3586. /* Reuse the page if possible. For notification packets and
  3587. * SKBs that fail to Rx correctly, add them back into the
  3588. * rx_free list for reuse later. */
  3589. spin_lock_irqsave(&rxq->lock, flags);
  3590. if (rxb->page != NULL) {
  3591. rxb->page_dma =
  3592. dma_map_page(&il->pci_dev->dev, rxb->page, 0,
  3593. PAGE_SIZE << il->hw_params.rx_page_order,
  3594. DMA_FROM_DEVICE);
  3595. if (unlikely(dma_mapping_error(&il->pci_dev->dev,
  3596. rxb->page_dma))) {
  3597. __il_free_pages(il, rxb->page);
  3598. rxb->page = NULL;
  3599. list_add_tail(&rxb->list, &rxq->rx_used);
  3600. } else {
  3601. list_add_tail(&rxb->list, &rxq->rx_free);
  3602. rxq->free_count++;
  3603. }
  3604. } else
  3605. list_add_tail(&rxb->list, &rxq->rx_used);
  3606. spin_unlock_irqrestore(&rxq->lock, flags);
  3607. i = (i + 1) & RX_QUEUE_MASK;
  3608. /* If there are a lot of unused frames,
  3609. * restock the Rx queue so ucode wont assert. */
  3610. if (fill_rx) {
  3611. count++;
  3612. if (count >= 8) {
  3613. rxq->read = i;
  3614. il4965_rx_replenish_now(il);
  3615. count = 0;
  3616. }
  3617. }
  3618. }
  3619. /* Backtrack one entry */
  3620. rxq->read = i;
  3621. if (fill_rx)
  3622. il4965_rx_replenish_now(il);
  3623. else
  3624. il4965_rx_queue_restock(il);
  3625. }
  3626. /* call this function to flush any scheduled tasklet */
  3627. static inline void
  3628. il4965_synchronize_irq(struct il_priv *il)
  3629. {
  3630. /* wait to make sure we flush pending tasklet */
  3631. synchronize_irq(il->pci_dev->irq);
  3632. tasklet_kill(&il->irq_tasklet);
  3633. }
  3634. static void
  3635. il4965_irq_tasklet(struct tasklet_struct *t)
  3636. {
  3637. struct il_priv *il = from_tasklet(il, t, irq_tasklet);
  3638. u32 inta, handled = 0;
  3639. u32 inta_fh;
  3640. unsigned long flags;
  3641. u32 i;
  3642. #ifdef CONFIG_IWLEGACY_DEBUG
  3643. u32 inta_mask;
  3644. #endif
  3645. spin_lock_irqsave(&il->lock, flags);
  3646. /* Ack/clear/reset pending uCode interrupts.
  3647. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  3648. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  3649. inta = _il_rd(il, CSR_INT);
  3650. _il_wr(il, CSR_INT, inta);
  3651. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  3652. * Any new interrupts that happen after this, either while we're
  3653. * in this tasklet, or later, will show up in next ISR/tasklet. */
  3654. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  3655. _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
  3656. #ifdef CONFIG_IWLEGACY_DEBUG
  3657. if (il_get_debug_level(il) & IL_DL_ISR) {
  3658. /* just for debug */
  3659. inta_mask = _il_rd(il, CSR_INT_MASK);
  3660. D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
  3661. inta_mask, inta_fh);
  3662. }
  3663. #endif
  3664. spin_unlock_irqrestore(&il->lock, flags);
  3665. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  3666. * atomic, make sure that inta covers all the interrupts that
  3667. * we've discovered, even if FH interrupt came in just after
  3668. * reading CSR_INT. */
  3669. if (inta_fh & CSR49_FH_INT_RX_MASK)
  3670. inta |= CSR_INT_BIT_FH_RX;
  3671. if (inta_fh & CSR49_FH_INT_TX_MASK)
  3672. inta |= CSR_INT_BIT_FH_TX;
  3673. /* Now service all interrupt bits discovered above. */
  3674. if (inta & CSR_INT_BIT_HW_ERR) {
  3675. IL_ERR("Hardware error detected. Restarting.\n");
  3676. /* Tell the device to stop sending interrupts */
  3677. il_disable_interrupts(il);
  3678. il->isr_stats.hw++;
  3679. il_irq_handle_error(il);
  3680. handled |= CSR_INT_BIT_HW_ERR;
  3681. return;
  3682. }
  3683. #ifdef CONFIG_IWLEGACY_DEBUG
  3684. if (il_get_debug_level(il) & (IL_DL_ISR)) {
  3685. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  3686. if (inta & CSR_INT_BIT_SCD) {
  3687. D_ISR("Scheduler finished to transmit "
  3688. "the frame/frames.\n");
  3689. il->isr_stats.sch++;
  3690. }
  3691. /* Alive notification via Rx interrupt will do the real work */
  3692. if (inta & CSR_INT_BIT_ALIVE) {
  3693. D_ISR("Alive interrupt\n");
  3694. il->isr_stats.alive++;
  3695. }
  3696. }
  3697. #endif
  3698. /* Safely ignore these bits for debug checks below */
  3699. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  3700. /* HW RF KILL switch toggled */
  3701. if (inta & CSR_INT_BIT_RF_KILL) {
  3702. int hw_rf_kill = 0;
  3703. if (!(_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  3704. hw_rf_kill = 1;
  3705. IL_WARN("RF_KILL bit toggled to %s.\n",
  3706. hw_rf_kill ? "disable radio" : "enable radio");
  3707. il->isr_stats.rfkill++;
  3708. /* driver only loads ucode once setting the interface up.
  3709. * the driver allows loading the ucode even if the radio
  3710. * is killed. Hence update the killswitch state here. The
  3711. * rfkill handler will care about restarting if needed.
  3712. */
  3713. if (hw_rf_kill) {
  3714. set_bit(S_RFKILL, &il->status);
  3715. } else {
  3716. clear_bit(S_RFKILL, &il->status);
  3717. il_force_reset(il, true);
  3718. }
  3719. wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
  3720. handled |= CSR_INT_BIT_RF_KILL;
  3721. }
  3722. /* Chip got too hot and stopped itself */
  3723. if (inta & CSR_INT_BIT_CT_KILL) {
  3724. IL_ERR("Microcode CT kill error detected.\n");
  3725. il->isr_stats.ctkill++;
  3726. handled |= CSR_INT_BIT_CT_KILL;
  3727. }
  3728. /* Error detected by uCode */
  3729. if (inta & CSR_INT_BIT_SW_ERR) {
  3730. IL_ERR("Microcode SW error detected. " " Restarting 0x%X.\n",
  3731. inta);
  3732. il->isr_stats.sw++;
  3733. il_irq_handle_error(il);
  3734. handled |= CSR_INT_BIT_SW_ERR;
  3735. }
  3736. /*
  3737. * uCode wakes up after power-down sleep.
  3738. * Tell device about any new tx or host commands enqueued,
  3739. * and about any Rx buffers made available while asleep.
  3740. */
  3741. if (inta & CSR_INT_BIT_WAKEUP) {
  3742. D_ISR("Wakeup interrupt\n");
  3743. il_rx_queue_update_write_ptr(il, &il->rxq);
  3744. for (i = 0; i < il->hw_params.max_txq_num; i++)
  3745. il_txq_update_write_ptr(il, &il->txq[i]);
  3746. il->isr_stats.wakeup++;
  3747. handled |= CSR_INT_BIT_WAKEUP;
  3748. }
  3749. /* All uCode command responses, including Tx command responses,
  3750. * Rx "responses" (frame-received notification), and other
  3751. * notifications from uCode come through here*/
  3752. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  3753. il4965_rx_handle(il);
  3754. il->isr_stats.rx++;
  3755. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  3756. }
  3757. /* This "Tx" DMA channel is used only for loading uCode */
  3758. if (inta & CSR_INT_BIT_FH_TX) {
  3759. D_ISR("uCode load interrupt\n");
  3760. il->isr_stats.tx++;
  3761. handled |= CSR_INT_BIT_FH_TX;
  3762. /* Wake up uCode load routine, now that load is complete */
  3763. il->ucode_write_complete = 1;
  3764. wake_up(&il->wait_command_queue);
  3765. }
  3766. if (inta & ~handled) {
  3767. IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
  3768. il->isr_stats.unhandled++;
  3769. }
  3770. if (inta & ~(il->inta_mask)) {
  3771. IL_WARN("Disabled INTA bits 0x%08x were pending\n",
  3772. inta & ~il->inta_mask);
  3773. IL_WARN(" with FH49_INT = 0x%08x\n", inta_fh);
  3774. }
  3775. /* Re-enable all interrupts */
  3776. /* only Re-enable if disabled by irq */
  3777. if (test_bit(S_INT_ENABLED, &il->status))
  3778. il_enable_interrupts(il);
  3779. /* Re-enable RF_KILL if it occurred */
  3780. else if (handled & CSR_INT_BIT_RF_KILL)
  3781. il_enable_rfkill_int(il);
  3782. #ifdef CONFIG_IWLEGACY_DEBUG
  3783. if (il_get_debug_level(il) & (IL_DL_ISR)) {
  3784. inta = _il_rd(il, CSR_INT);
  3785. inta_mask = _il_rd(il, CSR_INT_MASK);
  3786. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  3787. D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  3788. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  3789. }
  3790. #endif
  3791. }
  3792. /*****************************************************************************
  3793. *
  3794. * sysfs attributes
  3795. *
  3796. *****************************************************************************/
  3797. #ifdef CONFIG_IWLEGACY_DEBUG
  3798. /*
  3799. * The following adds a new attribute to the sysfs representation
  3800. * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
  3801. * used for controlling the debug level.
  3802. *
  3803. * See the level definitions in iwl for details.
  3804. *
  3805. * The debug_level being managed using sysfs below is a per device debug
  3806. * level that is used instead of the global debug level if it (the per
  3807. * device debug level) is set.
  3808. */
  3809. static ssize_t
  3810. il4965_show_debug_level(struct device *d, struct device_attribute *attr,
  3811. char *buf)
  3812. {
  3813. struct il_priv *il = dev_get_drvdata(d);
  3814. return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
  3815. }
  3816. static ssize_t
  3817. il4965_store_debug_level(struct device *d, struct device_attribute *attr,
  3818. const char *buf, size_t count)
  3819. {
  3820. struct il_priv *il = dev_get_drvdata(d);
  3821. unsigned long val;
  3822. int ret;
  3823. ret = kstrtoul(buf, 0, &val);
  3824. if (ret)
  3825. IL_ERR("%s is not in hex or decimal form.\n", buf);
  3826. else
  3827. il->debug_level = val;
  3828. return strnlen(buf, count);
  3829. }
  3830. static DEVICE_ATTR(debug_level, 0644, il4965_show_debug_level,
  3831. il4965_store_debug_level);
  3832. #endif /* CONFIG_IWLEGACY_DEBUG */
  3833. static ssize_t
  3834. il4965_show_temperature(struct device *d, struct device_attribute *attr,
  3835. char *buf)
  3836. {
  3837. struct il_priv *il = dev_get_drvdata(d);
  3838. if (!il_is_alive(il))
  3839. return -EAGAIN;
  3840. return sprintf(buf, "%d\n", il->temperature);
  3841. }
  3842. static DEVICE_ATTR(temperature, 0444, il4965_show_temperature, NULL);
  3843. static ssize_t
  3844. il4965_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
  3845. {
  3846. struct il_priv *il = dev_get_drvdata(d);
  3847. if (!il_is_ready_rf(il))
  3848. return sprintf(buf, "off\n");
  3849. else
  3850. return sprintf(buf, "%d\n", il->tx_power_user_lmt);
  3851. }
  3852. static ssize_t
  3853. il4965_store_tx_power(struct device *d, struct device_attribute *attr,
  3854. const char *buf, size_t count)
  3855. {
  3856. struct il_priv *il = dev_get_drvdata(d);
  3857. unsigned long val;
  3858. int ret;
  3859. ret = kstrtoul(buf, 10, &val);
  3860. if (ret)
  3861. IL_INFO("%s is not in decimal form.\n", buf);
  3862. else {
  3863. ret = il_set_tx_power(il, val, false);
  3864. if (ret)
  3865. IL_ERR("failed setting tx power (0x%08x).\n", ret);
  3866. else
  3867. ret = count;
  3868. }
  3869. return ret;
  3870. }
  3871. static DEVICE_ATTR(tx_power, 0644, il4965_show_tx_power,
  3872. il4965_store_tx_power);
  3873. static struct attribute *il_sysfs_entries[] = {
  3874. &dev_attr_temperature.attr,
  3875. &dev_attr_tx_power.attr,
  3876. #ifdef CONFIG_IWLEGACY_DEBUG
  3877. &dev_attr_debug_level.attr,
  3878. #endif
  3879. NULL
  3880. };
  3881. static const struct attribute_group il_attribute_group = {
  3882. .name = NULL, /* put in device directory */
  3883. .attrs = il_sysfs_entries,
  3884. };
  3885. /******************************************************************************
  3886. *
  3887. * uCode download functions
  3888. *
  3889. ******************************************************************************/
  3890. static void
  3891. il4965_dealloc_ucode_pci(struct il_priv *il)
  3892. {
  3893. il_free_fw_desc(il->pci_dev, &il->ucode_code);
  3894. il_free_fw_desc(il->pci_dev, &il->ucode_data);
  3895. il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
  3896. il_free_fw_desc(il->pci_dev, &il->ucode_init);
  3897. il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
  3898. il_free_fw_desc(il->pci_dev, &il->ucode_boot);
  3899. }
  3900. static void
  3901. il4965_nic_start(struct il_priv *il)
  3902. {
  3903. /* Remove all resets to allow NIC to operate */
  3904. _il_wr(il, CSR_RESET, 0);
  3905. }
  3906. static void il4965_ucode_callback(const struct firmware *ucode_raw,
  3907. void *context);
  3908. static int il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length);
  3909. static int __must_check
  3910. il4965_request_firmware(struct il_priv *il, bool first)
  3911. {
  3912. const char *name_pre = il->cfg->fw_name_pre;
  3913. char tag[8];
  3914. if (first) {
  3915. il->fw_idx = il->cfg->ucode_api_max;
  3916. sprintf(tag, "%d", il->fw_idx);
  3917. } else {
  3918. il->fw_idx--;
  3919. sprintf(tag, "%d", il->fw_idx);
  3920. }
  3921. if (il->fw_idx < il->cfg->ucode_api_min) {
  3922. IL_ERR("no suitable firmware found!\n");
  3923. return -ENOENT;
  3924. }
  3925. sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  3926. D_INFO("attempting to load firmware '%s'\n", il->firmware_name);
  3927. return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
  3928. &il->pci_dev->dev, GFP_KERNEL, il,
  3929. il4965_ucode_callback);
  3930. }
  3931. struct il4965_firmware_pieces {
  3932. const void *inst, *data, *init, *init_data, *boot;
  3933. size_t inst_size, data_size, init_size, init_data_size, boot_size;
  3934. };
  3935. static int
  3936. il4965_load_firmware(struct il_priv *il, const struct firmware *ucode_raw,
  3937. struct il4965_firmware_pieces *pieces)
  3938. {
  3939. struct il_ucode_header *ucode = (void *)ucode_raw->data;
  3940. u32 api_ver, hdr_size;
  3941. const u8 *src;
  3942. il->ucode_ver = le32_to_cpu(ucode->ver);
  3943. api_ver = IL_UCODE_API(il->ucode_ver);
  3944. switch (api_ver) {
  3945. default:
  3946. case 0:
  3947. case 1:
  3948. case 2:
  3949. hdr_size = 24;
  3950. if (ucode_raw->size < hdr_size) {
  3951. IL_ERR("File size too small!\n");
  3952. return -EINVAL;
  3953. }
  3954. pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
  3955. pieces->data_size = le32_to_cpu(ucode->v1.data_size);
  3956. pieces->init_size = le32_to_cpu(ucode->v1.init_size);
  3957. pieces->init_data_size = le32_to_cpu(ucode->v1.init_data_size);
  3958. pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
  3959. src = ucode->v1.data;
  3960. break;
  3961. }
  3962. /* Verify size of file vs. image size info in file's header */
  3963. if (ucode_raw->size !=
  3964. hdr_size + pieces->inst_size + pieces->data_size +
  3965. pieces->init_size + pieces->init_data_size + pieces->boot_size) {
  3966. IL_ERR("uCode file size %d does not match expected size\n",
  3967. (int)ucode_raw->size);
  3968. return -EINVAL;
  3969. }
  3970. pieces->inst = src;
  3971. src += pieces->inst_size;
  3972. pieces->data = src;
  3973. src += pieces->data_size;
  3974. pieces->init = src;
  3975. src += pieces->init_size;
  3976. pieces->init_data = src;
  3977. src += pieces->init_data_size;
  3978. pieces->boot = src;
  3979. src += pieces->boot_size;
  3980. return 0;
  3981. }
  3982. /*
  3983. * il4965_ucode_callback - callback when firmware was loaded
  3984. *
  3985. * If loaded successfully, copies the firmware into buffers
  3986. * for the card to fetch (via DMA).
  3987. */
  3988. static void
  3989. il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
  3990. {
  3991. struct il_priv *il = context;
  3992. int err;
  3993. struct il4965_firmware_pieces pieces;
  3994. const unsigned int api_max = il->cfg->ucode_api_max;
  3995. const unsigned int api_min = il->cfg->ucode_api_min;
  3996. u32 api_ver;
  3997. u32 max_probe_length = 200;
  3998. u32 standard_phy_calibration_size =
  3999. IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  4000. memset(&pieces, 0, sizeof(pieces));
  4001. if (!ucode_raw) {
  4002. if (il->fw_idx <= il->cfg->ucode_api_max)
  4003. IL_ERR("request for firmware file '%s' failed.\n",
  4004. il->firmware_name);
  4005. goto try_again;
  4006. }
  4007. D_INFO("Loaded firmware file '%s' (%zd bytes).\n", il->firmware_name,
  4008. ucode_raw->size);
  4009. /* Make sure that we got at least the API version number */
  4010. if (ucode_raw->size < 4) {
  4011. IL_ERR("File size way too small!\n");
  4012. goto try_again;
  4013. }
  4014. /* Data from ucode file: header followed by uCode images */
  4015. err = il4965_load_firmware(il, ucode_raw, &pieces);
  4016. if (err)
  4017. goto try_again;
  4018. api_ver = IL_UCODE_API(il->ucode_ver);
  4019. /*
  4020. * api_ver should match the api version forming part of the
  4021. * firmware filename ... but we don't check for that and only rely
  4022. * on the API version read from firmware header from here on forward
  4023. */
  4024. if (api_ver < api_min || api_ver > api_max) {
  4025. IL_ERR("Driver unable to support your firmware API. "
  4026. "Driver supports v%u, firmware is v%u.\n", api_max,
  4027. api_ver);
  4028. goto try_again;
  4029. }
  4030. if (api_ver != api_max)
  4031. IL_ERR("Firmware has old API version. Expected v%u, "
  4032. "got v%u. New firmware can be obtained "
  4033. "from http://www.intellinuxwireless.org.\n", api_max,
  4034. api_ver);
  4035. IL_INFO("loaded firmware version %u.%u.%u.%u\n",
  4036. IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
  4037. IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
  4038. snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
  4039. "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
  4040. IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
  4041. IL_UCODE_SERIAL(il->ucode_ver));
  4042. /*
  4043. * For any of the failures below (before allocating pci memory)
  4044. * we will try to load a version with a smaller API -- maybe the
  4045. * user just got a corrupted version of the latest API.
  4046. */
  4047. D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
  4048. D_INFO("f/w package hdr runtime inst size = %zd\n", pieces.inst_size);
  4049. D_INFO("f/w package hdr runtime data size = %zd\n", pieces.data_size);
  4050. D_INFO("f/w package hdr init inst size = %zd\n", pieces.init_size);
  4051. D_INFO("f/w package hdr init data size = %zd\n", pieces.init_data_size);
  4052. D_INFO("f/w package hdr boot inst size = %zd\n", pieces.boot_size);
  4053. /* Verify that uCode images will fit in card's SRAM */
  4054. if (pieces.inst_size > il->hw_params.max_inst_size) {
  4055. IL_ERR("uCode instr len %zd too large to fit in\n",
  4056. pieces.inst_size);
  4057. goto try_again;
  4058. }
  4059. if (pieces.data_size > il->hw_params.max_data_size) {
  4060. IL_ERR("uCode data len %zd too large to fit in\n",
  4061. pieces.data_size);
  4062. goto try_again;
  4063. }
  4064. if (pieces.init_size > il->hw_params.max_inst_size) {
  4065. IL_ERR("uCode init instr len %zd too large to fit in\n",
  4066. pieces.init_size);
  4067. goto try_again;
  4068. }
  4069. if (pieces.init_data_size > il->hw_params.max_data_size) {
  4070. IL_ERR("uCode init data len %zd too large to fit in\n",
  4071. pieces.init_data_size);
  4072. goto try_again;
  4073. }
  4074. if (pieces.boot_size > il->hw_params.max_bsm_size) {
  4075. IL_ERR("uCode boot instr len %zd too large to fit in\n",
  4076. pieces.boot_size);
  4077. goto try_again;
  4078. }
  4079. /* Allocate ucode buffers for card's bus-master loading ... */
  4080. /* Runtime instructions and 2 copies of data:
  4081. * 1) unmodified from disk
  4082. * 2) backup cache for save/restore during power-downs */
  4083. il->ucode_code.len = pieces.inst_size;
  4084. il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
  4085. il->ucode_data.len = pieces.data_size;
  4086. il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
  4087. il->ucode_data_backup.len = pieces.data_size;
  4088. il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
  4089. if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
  4090. !il->ucode_data_backup.v_addr)
  4091. goto err_pci_alloc;
  4092. /* Initialization instructions and data */
  4093. if (pieces.init_size && pieces.init_data_size) {
  4094. il->ucode_init.len = pieces.init_size;
  4095. il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
  4096. il->ucode_init_data.len = pieces.init_data_size;
  4097. il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
  4098. if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
  4099. goto err_pci_alloc;
  4100. }
  4101. /* Bootstrap (instructions only, no data) */
  4102. if (pieces.boot_size) {
  4103. il->ucode_boot.len = pieces.boot_size;
  4104. il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
  4105. if (!il->ucode_boot.v_addr)
  4106. goto err_pci_alloc;
  4107. }
  4108. /* Now that we can no longer fail, copy information */
  4109. il->sta_key_max_num = STA_KEY_MAX_NUM;
  4110. /* Copy images into buffers for card's bus-master reads ... */
  4111. /* Runtime instructions (first block of data in file) */
  4112. D_INFO("Copying (but not loading) uCode instr len %zd\n",
  4113. pieces.inst_size);
  4114. memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
  4115. D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  4116. il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
  4117. /*
  4118. * Runtime data
  4119. * NOTE: Copy into backup buffer will be done in il_up()
  4120. */
  4121. D_INFO("Copying (but not loading) uCode data len %zd\n",
  4122. pieces.data_size);
  4123. memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
  4124. memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
  4125. /* Initialization instructions */
  4126. if (pieces.init_size) {
  4127. D_INFO("Copying (but not loading) init instr len %zd\n",
  4128. pieces.init_size);
  4129. memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
  4130. }
  4131. /* Initialization data */
  4132. if (pieces.init_data_size) {
  4133. D_INFO("Copying (but not loading) init data len %zd\n",
  4134. pieces.init_data_size);
  4135. memcpy(il->ucode_init_data.v_addr, pieces.init_data,
  4136. pieces.init_data_size);
  4137. }
  4138. /* Bootstrap instructions */
  4139. D_INFO("Copying (but not loading) boot instr len %zd\n",
  4140. pieces.boot_size);
  4141. memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
  4142. /*
  4143. * figure out the offset of chain noise reset and gain commands
  4144. * base on the size of standard phy calibration commands table size
  4145. */
  4146. il->_4965.phy_calib_chain_noise_reset_cmd =
  4147. standard_phy_calibration_size;
  4148. il->_4965.phy_calib_chain_noise_gain_cmd =
  4149. standard_phy_calibration_size + 1;
  4150. /**************************************************
  4151. * This is still part of probe() in a sense...
  4152. *
  4153. * 9. Setup and register with mac80211 and debugfs
  4154. **************************************************/
  4155. err = il4965_mac_setup_register(il, max_probe_length);
  4156. if (err)
  4157. goto out_unbind;
  4158. il_dbgfs_register(il, DRV_NAME);
  4159. err = sysfs_create_group(&il->pci_dev->dev.kobj, &il_attribute_group);
  4160. if (err) {
  4161. IL_ERR("failed to create sysfs device attributes\n");
  4162. goto out_unbind;
  4163. }
  4164. /* We have our copies now, allow OS release its copies */
  4165. release_firmware(ucode_raw);
  4166. complete(&il->_4965.firmware_loading_complete);
  4167. return;
  4168. try_again:
  4169. /* try next, if any */
  4170. if (il4965_request_firmware(il, false))
  4171. goto out_unbind;
  4172. release_firmware(ucode_raw);
  4173. return;
  4174. err_pci_alloc:
  4175. IL_ERR("failed to allocate pci memory\n");
  4176. il4965_dealloc_ucode_pci(il);
  4177. out_unbind:
  4178. complete(&il->_4965.firmware_loading_complete);
  4179. device_release_driver(&il->pci_dev->dev);
  4180. release_firmware(ucode_raw);
  4181. }
  4182. static const char *const desc_lookup_text[] = {
  4183. "OK",
  4184. "FAIL",
  4185. "BAD_PARAM",
  4186. "BAD_CHECKSUM",
  4187. "NMI_INTERRUPT_WDG",
  4188. "SYSASSERT",
  4189. "FATAL_ERROR",
  4190. "BAD_COMMAND",
  4191. "HW_ERROR_TUNE_LOCK",
  4192. "HW_ERROR_TEMPERATURE",
  4193. "ILLEGAL_CHAN_FREQ",
  4194. "VCC_NOT_STBL",
  4195. "FH49_ERROR",
  4196. "NMI_INTERRUPT_HOST",
  4197. "NMI_INTERRUPT_ACTION_PT",
  4198. "NMI_INTERRUPT_UNKNOWN",
  4199. "UCODE_VERSION_MISMATCH",
  4200. "HW_ERROR_ABS_LOCK",
  4201. "HW_ERROR_CAL_LOCK_FAIL",
  4202. "NMI_INTERRUPT_INST_ACTION_PT",
  4203. "NMI_INTERRUPT_DATA_ACTION_PT",
  4204. "NMI_TRM_HW_ER",
  4205. "NMI_INTERRUPT_TRM",
  4206. "NMI_INTERRUPT_BREAK_POINT",
  4207. "DEBUG_0",
  4208. "DEBUG_1",
  4209. "DEBUG_2",
  4210. "DEBUG_3",
  4211. };
  4212. static struct {
  4213. char *name;
  4214. u8 num;
  4215. } advanced_lookup[] = {
  4216. {
  4217. "NMI_INTERRUPT_WDG", 0x34}, {
  4218. "SYSASSERT", 0x35}, {
  4219. "UCODE_VERSION_MISMATCH", 0x37}, {
  4220. "BAD_COMMAND", 0x38}, {
  4221. "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C}, {
  4222. "FATAL_ERROR", 0x3D}, {
  4223. "NMI_TRM_HW_ERR", 0x46}, {
  4224. "NMI_INTERRUPT_TRM", 0x4C}, {
  4225. "NMI_INTERRUPT_BREAK_POINT", 0x54}, {
  4226. "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C}, {
  4227. "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64}, {
  4228. "NMI_INTERRUPT_HOST", 0x66}, {
  4229. "NMI_INTERRUPT_ACTION_PT", 0x7C}, {
  4230. "NMI_INTERRUPT_UNKNOWN", 0x84}, {
  4231. "NMI_INTERRUPT_INST_ACTION_PT", 0x86}, {
  4232. "ADVANCED_SYSASSERT", 0},};
  4233. static const char *
  4234. il4965_desc_lookup(u32 num)
  4235. {
  4236. int i;
  4237. int max = ARRAY_SIZE(desc_lookup_text);
  4238. if (num < max)
  4239. return desc_lookup_text[num];
  4240. max = ARRAY_SIZE(advanced_lookup) - 1;
  4241. for (i = 0; i < max; i++) {
  4242. if (advanced_lookup[i].num == num)
  4243. break;
  4244. }
  4245. return advanced_lookup[i].name;
  4246. }
  4247. #define ERROR_START_OFFSET (1 * sizeof(u32))
  4248. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  4249. void
  4250. il4965_dump_nic_error_log(struct il_priv *il)
  4251. {
  4252. u32 data2, line;
  4253. u32 desc, time, count, base, data1;
  4254. u32 blink1, blink2, ilink1, ilink2;
  4255. u32 pc, hcmd;
  4256. if (il->ucode_type == UCODE_INIT)
  4257. base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
  4258. else
  4259. base = le32_to_cpu(il->card_alive.error_event_table_ptr);
  4260. if (!il->ops->is_valid_rtc_data_addr(base)) {
  4261. IL_ERR("Not valid error log pointer 0x%08X for %s uCode\n",
  4262. base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
  4263. return;
  4264. }
  4265. count = il_read_targ_mem(il, base);
  4266. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  4267. IL_ERR("Start IWL Error Log Dump:\n");
  4268. IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
  4269. }
  4270. desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
  4271. il->isr_stats.err_code = desc;
  4272. pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
  4273. blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
  4274. blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
  4275. ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
  4276. ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
  4277. data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
  4278. data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
  4279. line = il_read_targ_mem(il, base + 9 * sizeof(u32));
  4280. time = il_read_targ_mem(il, base + 11 * sizeof(u32));
  4281. hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
  4282. IL_ERR("Desc Time "
  4283. "data1 data2 line\n");
  4284. IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
  4285. il4965_desc_lookup(desc), desc, time, data1, data2, line);
  4286. IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
  4287. IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n", pc, blink1,
  4288. blink2, ilink1, ilink2, hcmd);
  4289. }
  4290. static void
  4291. il4965_rf_kill_ct_config(struct il_priv *il)
  4292. {
  4293. struct il_ct_kill_config cmd;
  4294. unsigned long flags;
  4295. int ret = 0;
  4296. spin_lock_irqsave(&il->lock, flags);
  4297. _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
  4298. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  4299. spin_unlock_irqrestore(&il->lock, flags);
  4300. cmd.critical_temperature_R =
  4301. cpu_to_le32(il->hw_params.ct_kill_threshold);
  4302. ret = il_send_cmd_pdu(il, C_CT_KILL_CONFIG, sizeof(cmd), &cmd);
  4303. if (ret)
  4304. IL_ERR("C_CT_KILL_CONFIG failed\n");
  4305. else
  4306. D_INFO("C_CT_KILL_CONFIG " "succeeded, "
  4307. "critical temperature is %d\n",
  4308. il->hw_params.ct_kill_threshold);
  4309. }
  4310. static const s8 default_queue_to_tx_fifo[] = {
  4311. IL_TX_FIFO_VO,
  4312. IL_TX_FIFO_VI,
  4313. IL_TX_FIFO_BE,
  4314. IL_TX_FIFO_BK,
  4315. IL49_CMD_FIFO_NUM,
  4316. IL_TX_FIFO_UNUSED,
  4317. IL_TX_FIFO_UNUSED,
  4318. };
  4319. #define IL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
  4320. static int
  4321. il4965_alive_notify(struct il_priv *il)
  4322. {
  4323. u32 a;
  4324. unsigned long flags;
  4325. int i, chan;
  4326. u32 reg_val;
  4327. spin_lock_irqsave(&il->lock, flags);
  4328. /* Clear 4965's internal Tx Scheduler data base */
  4329. il->scd_base_addr = il_rd_prph(il, IL49_SCD_SRAM_BASE_ADDR);
  4330. a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
  4331. for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
  4332. il_write_targ_mem(il, a, 0);
  4333. for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
  4334. il_write_targ_mem(il, a, 0);
  4335. for (;
  4336. a <
  4337. il->scd_base_addr +
  4338. IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num);
  4339. a += 4)
  4340. il_write_targ_mem(il, a, 0);
  4341. /* Tel 4965 where to find Tx byte count tables */
  4342. il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR, il->scd_bc_tbls.dma >> 10);
  4343. /* Enable DMA channel */
  4344. for (chan = 0; chan < FH49_TCSR_CHNL_NUM; chan++)
  4345. il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(chan),
  4346. FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  4347. FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  4348. /* Update FH chicken bits */
  4349. reg_val = il_rd(il, FH49_TX_CHICKEN_BITS_REG);
  4350. il_wr(il, FH49_TX_CHICKEN_BITS_REG,
  4351. reg_val | FH49_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  4352. /* Disable chain mode for all queues */
  4353. il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
  4354. /* Initialize each Tx queue (including the command queue) */
  4355. for (i = 0; i < il->hw_params.max_txq_num; i++) {
  4356. /* TFD circular buffer read/write idxes */
  4357. il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
  4358. il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
  4359. /* Max Tx Window size for Scheduler-ACK mode */
  4360. il_write_targ_mem(il,
  4361. il->scd_base_addr +
  4362. IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
  4363. (SCD_WIN_SIZE <<
  4364. IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  4365. IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  4366. /* Frame limit */
  4367. il_write_targ_mem(il,
  4368. il->scd_base_addr +
  4369. IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
  4370. sizeof(u32),
  4371. (SCD_FRAME_LIMIT <<
  4372. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  4373. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  4374. }
  4375. il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
  4376. (1 << il->hw_params.max_txq_num) - 1);
  4377. /* Activate all Tx DMA/FIFO channels */
  4378. il4965_txq_set_sched(il, IL_MASK(0, 6));
  4379. il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
  4380. /* make sure all queue are not stopped */
  4381. memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
  4382. for (i = 0; i < 4; i++)
  4383. atomic_set(&il->queue_stop_count[i], 0);
  4384. /* reset to 0 to enable all the queue first */
  4385. il->txq_ctx_active_msk = 0;
  4386. /* Map each Tx/cmd queue to its corresponding fifo */
  4387. BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
  4388. for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
  4389. int ac = default_queue_to_tx_fifo[i];
  4390. il_txq_ctx_activate(il, i);
  4391. if (ac == IL_TX_FIFO_UNUSED)
  4392. continue;
  4393. il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
  4394. }
  4395. spin_unlock_irqrestore(&il->lock, flags);
  4396. return 0;
  4397. }
  4398. /*
  4399. * il4965_alive_start - called after N_ALIVE notification received
  4400. * from protocol/runtime uCode (initialization uCode's
  4401. * Alive gets handled by il_init_alive_start()).
  4402. */
  4403. static void
  4404. il4965_alive_start(struct il_priv *il)
  4405. {
  4406. int ret = 0;
  4407. D_INFO("Runtime Alive received.\n");
  4408. if (il->card_alive.is_valid != UCODE_VALID_OK) {
  4409. /* We had an error bringing up the hardware, so take it
  4410. * all the way back down so we can try again */
  4411. D_INFO("Alive failed.\n");
  4412. goto restart;
  4413. }
  4414. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  4415. * This is a paranoid check, because we would not have gotten the
  4416. * "runtime" alive if code weren't properly loaded. */
  4417. if (il4965_verify_ucode(il)) {
  4418. /* Runtime instruction load was bad;
  4419. * take it all the way back down so we can try again */
  4420. D_INFO("Bad runtime uCode load.\n");
  4421. goto restart;
  4422. }
  4423. ret = il4965_alive_notify(il);
  4424. if (ret) {
  4425. IL_WARN("Could not complete ALIVE transition [ntf]: %d\n", ret);
  4426. goto restart;
  4427. }
  4428. /* After the ALIVE response, we can send host commands to the uCode */
  4429. set_bit(S_ALIVE, &il->status);
  4430. /* Enable watchdog to monitor the driver tx queues */
  4431. il_setup_watchdog(il);
  4432. if (il_is_rfkill(il))
  4433. return;
  4434. ieee80211_wake_queues(il->hw);
  4435. il->active_rate = RATES_MASK;
  4436. il_power_update_mode(il, true);
  4437. D_INFO("Updated power mode\n");
  4438. if (il_is_associated(il)) {
  4439. struct il_rxon_cmd *active_rxon =
  4440. (struct il_rxon_cmd *)&il->active;
  4441. /* apply any changes in staging */
  4442. il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  4443. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4444. } else {
  4445. /* Initialize our rx_config data */
  4446. il_connection_init_rx_config(il);
  4447. if (il->ops->set_rxon_chain)
  4448. il->ops->set_rxon_chain(il);
  4449. }
  4450. /* Configure bluetooth coexistence if enabled */
  4451. il_send_bt_config(il);
  4452. il4965_reset_run_time_calib(il);
  4453. set_bit(S_READY, &il->status);
  4454. /* Configure the adapter for unassociated operation */
  4455. il_commit_rxon(il);
  4456. /* At this point, the NIC is initialized and operational */
  4457. il4965_rf_kill_ct_config(il);
  4458. D_INFO("ALIVE processing complete.\n");
  4459. wake_up(&il->wait_command_queue);
  4460. return;
  4461. restart:
  4462. queue_work(il->workqueue, &il->restart);
  4463. }
  4464. static void il4965_cancel_deferred_work(struct il_priv *il);
  4465. static void
  4466. __il4965_down(struct il_priv *il)
  4467. {
  4468. unsigned long flags;
  4469. int exit_pending;
  4470. D_INFO(DRV_NAME " is going down\n");
  4471. il_scan_cancel_timeout(il, 200);
  4472. exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
  4473. /* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
  4474. * to prevent rearm timer */
  4475. del_timer_sync(&il->watchdog);
  4476. il_clear_ucode_stations(il);
  4477. /* FIXME: race conditions ? */
  4478. spin_lock_irq(&il->sta_lock);
  4479. /*
  4480. * Remove all key information that is not stored as part
  4481. * of station information since mac80211 may not have had
  4482. * a chance to remove all the keys. When device is
  4483. * reconfigured by mac80211 after an error all keys will
  4484. * be reconfigured.
  4485. */
  4486. memset(il->_4965.wep_keys, 0, sizeof(il->_4965.wep_keys));
  4487. il->_4965.key_mapping_keys = 0;
  4488. spin_unlock_irq(&il->sta_lock);
  4489. il_dealloc_bcast_stations(il);
  4490. il_clear_driver_stations(il);
  4491. /* Unblock any waiting calls */
  4492. wake_up_all(&il->wait_command_queue);
  4493. /* Wipe out the EXIT_PENDING status bit if we are not actually
  4494. * exiting the module */
  4495. if (!exit_pending)
  4496. clear_bit(S_EXIT_PENDING, &il->status);
  4497. /* stop and reset the on-board processor */
  4498. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  4499. /* tell the device to stop sending interrupts */
  4500. spin_lock_irqsave(&il->lock, flags);
  4501. il_disable_interrupts(il);
  4502. spin_unlock_irqrestore(&il->lock, flags);
  4503. il4965_synchronize_irq(il);
  4504. if (il->mac80211_registered)
  4505. ieee80211_stop_queues(il->hw);
  4506. /* If we have not previously called il_init() then
  4507. * clear all bits but the RF Kill bit and return */
  4508. if (!il_is_init(il)) {
  4509. il->status =
  4510. test_bit(S_RFKILL, &il->status) << S_RFKILL |
  4511. test_bit(S_GEO_CONFIGURED, &il->status) << S_GEO_CONFIGURED |
  4512. test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
  4513. goto exit;
  4514. }
  4515. /* ...otherwise clear out all the status bits but the RF Kill
  4516. * bit and continue taking the NIC down. */
  4517. il->status &=
  4518. test_bit(S_RFKILL, &il->status) << S_RFKILL |
  4519. test_bit(S_GEO_CONFIGURED, &il->status) << S_GEO_CONFIGURED |
  4520. test_bit(S_FW_ERROR, &il->status) << S_FW_ERROR |
  4521. test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
  4522. /*
  4523. * We disabled and synchronized interrupt, and priv->mutex is taken, so
  4524. * here is the only thread which will program device registers, but
  4525. * still have lockdep assertions, so we are taking reg_lock.
  4526. */
  4527. spin_lock_irq(&il->reg_lock);
  4528. /* FIXME: il_grab_nic_access if rfkill is off ? */
  4529. il4965_txq_ctx_stop(il);
  4530. il4965_rxq_stop(il);
  4531. /* Power-down device's busmaster DMA clocks */
  4532. _il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  4533. udelay(5);
  4534. /* Make sure (redundant) we've released our request to stay awake */
  4535. _il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  4536. /* Stop the device, and put it in low power state */
  4537. _il_apm_stop(il);
  4538. spin_unlock_irq(&il->reg_lock);
  4539. il4965_txq_ctx_unmap(il);
  4540. exit:
  4541. memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
  4542. dev_kfree_skb(il->beacon_skb);
  4543. il->beacon_skb = NULL;
  4544. /* clear out any free frames */
  4545. il4965_clear_free_frames(il);
  4546. }
  4547. static void
  4548. il4965_down(struct il_priv *il)
  4549. {
  4550. mutex_lock(&il->mutex);
  4551. __il4965_down(il);
  4552. mutex_unlock(&il->mutex);
  4553. il4965_cancel_deferred_work(il);
  4554. }
  4555. static void
  4556. il4965_set_hw_ready(struct il_priv *il)
  4557. {
  4558. int ret;
  4559. il_set_bit(il, CSR_HW_IF_CONFIG_REG,
  4560. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  4561. /* See if we got it */
  4562. ret = _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
  4563. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  4564. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  4565. 100);
  4566. if (ret >= 0)
  4567. il->hw_ready = true;
  4568. D_INFO("hardware %s ready\n", (il->hw_ready) ? "" : "not");
  4569. }
  4570. static void
  4571. il4965_prepare_card_hw(struct il_priv *il)
  4572. {
  4573. int ret;
  4574. il->hw_ready = false;
  4575. il4965_set_hw_ready(il);
  4576. if (il->hw_ready)
  4577. return;
  4578. /* If HW is not ready, prepare the conditions to check again */
  4579. il_set_bit(il, CSR_HW_IF_CONFIG_REG, CSR_HW_IF_CONFIG_REG_PREPARE);
  4580. ret =
  4581. _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
  4582. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  4583. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  4584. /* HW should be ready by now, check again. */
  4585. if (ret != -ETIMEDOUT)
  4586. il4965_set_hw_ready(il);
  4587. }
  4588. #define MAX_HW_RESTARTS 5
  4589. static int
  4590. __il4965_up(struct il_priv *il)
  4591. {
  4592. int i;
  4593. int ret;
  4594. if (test_bit(S_EXIT_PENDING, &il->status)) {
  4595. IL_WARN("Exit pending; will not bring the NIC up\n");
  4596. return -EIO;
  4597. }
  4598. if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
  4599. IL_ERR("ucode not available for device bringup\n");
  4600. return -EIO;
  4601. }
  4602. ret = il4965_alloc_bcast_station(il);
  4603. if (ret) {
  4604. il_dealloc_bcast_stations(il);
  4605. return ret;
  4606. }
  4607. il4965_prepare_card_hw(il);
  4608. if (!il->hw_ready) {
  4609. il_dealloc_bcast_stations(il);
  4610. IL_ERR("HW not ready\n");
  4611. return -EIO;
  4612. }
  4613. /* If platform's RF_KILL switch is NOT set to KILL */
  4614. if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  4615. clear_bit(S_RFKILL, &il->status);
  4616. else {
  4617. set_bit(S_RFKILL, &il->status);
  4618. wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
  4619. il_dealloc_bcast_stations(il);
  4620. il_enable_rfkill_int(il);
  4621. IL_WARN("Radio disabled by HW RF Kill switch\n");
  4622. return 0;
  4623. }
  4624. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  4625. /* must be initialised before il_hw_nic_init */
  4626. il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
  4627. ret = il4965_hw_nic_init(il);
  4628. if (ret) {
  4629. IL_ERR("Unable to init nic\n");
  4630. il_dealloc_bcast_stations(il);
  4631. return ret;
  4632. }
  4633. /* make sure rfkill handshake bits are cleared */
  4634. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4635. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  4636. /* clear (again), then enable host interrupts */
  4637. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  4638. il_enable_interrupts(il);
  4639. /* really make sure rfkill handshake bits are cleared */
  4640. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4641. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4642. /* Copy original ucode data image from disk into backup cache.
  4643. * This will be used to initialize the on-board processor's
  4644. * data SRAM for a clean start when the runtime program first loads. */
  4645. memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
  4646. il->ucode_data.len);
  4647. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  4648. /* load bootstrap state machine,
  4649. * load bootstrap program into processor's memory,
  4650. * prepare to load the "initialize" uCode */
  4651. ret = il->ops->load_ucode(il);
  4652. if (ret) {
  4653. IL_ERR("Unable to set up bootstrap uCode: %d\n", ret);
  4654. continue;
  4655. }
  4656. /* start card; "initialize" will load runtime ucode */
  4657. il4965_nic_start(il);
  4658. D_INFO(DRV_NAME " is coming up\n");
  4659. return 0;
  4660. }
  4661. set_bit(S_EXIT_PENDING, &il->status);
  4662. __il4965_down(il);
  4663. clear_bit(S_EXIT_PENDING, &il->status);
  4664. /* tried to restart and config the device for as long as our
  4665. * patience could withstand */
  4666. IL_ERR("Unable to initialize device after %d attempts.\n", i);
  4667. return -EIO;
  4668. }
  4669. /*****************************************************************************
  4670. *
  4671. * Workqueue callbacks
  4672. *
  4673. *****************************************************************************/
  4674. static void
  4675. il4965_bg_init_alive_start(struct work_struct *data)
  4676. {
  4677. struct il_priv *il =
  4678. container_of(data, struct il_priv, init_alive_start.work);
  4679. mutex_lock(&il->mutex);
  4680. if (test_bit(S_EXIT_PENDING, &il->status))
  4681. goto out;
  4682. il->ops->init_alive_start(il);
  4683. out:
  4684. mutex_unlock(&il->mutex);
  4685. }
  4686. static void
  4687. il4965_bg_alive_start(struct work_struct *data)
  4688. {
  4689. struct il_priv *il =
  4690. container_of(data, struct il_priv, alive_start.work);
  4691. mutex_lock(&il->mutex);
  4692. if (test_bit(S_EXIT_PENDING, &il->status))
  4693. goto out;
  4694. il4965_alive_start(il);
  4695. out:
  4696. mutex_unlock(&il->mutex);
  4697. }
  4698. static void
  4699. il4965_bg_run_time_calib_work(struct work_struct *work)
  4700. {
  4701. struct il_priv *il = container_of(work, struct il_priv,
  4702. run_time_calib_work);
  4703. mutex_lock(&il->mutex);
  4704. if (test_bit(S_EXIT_PENDING, &il->status) ||
  4705. test_bit(S_SCANNING, &il->status)) {
  4706. mutex_unlock(&il->mutex);
  4707. return;
  4708. }
  4709. if (il->start_calib) {
  4710. il4965_chain_noise_calibration(il, (void *)&il->_4965.stats);
  4711. il4965_sensitivity_calibration(il, (void *)&il->_4965.stats);
  4712. }
  4713. mutex_unlock(&il->mutex);
  4714. }
  4715. static void
  4716. il4965_bg_restart(struct work_struct *data)
  4717. {
  4718. struct il_priv *il = container_of(data, struct il_priv, restart);
  4719. if (test_bit(S_EXIT_PENDING, &il->status))
  4720. return;
  4721. if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
  4722. mutex_lock(&il->mutex);
  4723. il->is_open = 0;
  4724. __il4965_down(il);
  4725. mutex_unlock(&il->mutex);
  4726. il4965_cancel_deferred_work(il);
  4727. ieee80211_restart_hw(il->hw);
  4728. } else {
  4729. il4965_down(il);
  4730. mutex_lock(&il->mutex);
  4731. if (test_bit(S_EXIT_PENDING, &il->status)) {
  4732. mutex_unlock(&il->mutex);
  4733. return;
  4734. }
  4735. __il4965_up(il);
  4736. mutex_unlock(&il->mutex);
  4737. }
  4738. }
  4739. static void
  4740. il4965_bg_rx_replenish(struct work_struct *data)
  4741. {
  4742. struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
  4743. if (test_bit(S_EXIT_PENDING, &il->status))
  4744. return;
  4745. mutex_lock(&il->mutex);
  4746. il4965_rx_replenish(il);
  4747. mutex_unlock(&il->mutex);
  4748. }
  4749. /*****************************************************************************
  4750. *
  4751. * mac80211 entry point functions
  4752. *
  4753. *****************************************************************************/
  4754. #define UCODE_READY_TIMEOUT (4 * HZ)
  4755. /*
  4756. * Not a mac80211 entry point function, but it fits in with all the
  4757. * other mac80211 functions grouped here.
  4758. */
  4759. static int
  4760. il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length)
  4761. {
  4762. int ret;
  4763. struct ieee80211_hw *hw = il->hw;
  4764. hw->rate_control_algorithm = "iwl-4965-rs";
  4765. /* Tell mac80211 our characteristics */
  4766. ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
  4767. ieee80211_hw_set(hw, SUPPORTS_PS);
  4768. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  4769. ieee80211_hw_set(hw, SPECTRUM_MGMT);
  4770. ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
  4771. ieee80211_hw_set(hw, SIGNAL_DBM);
  4772. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  4773. if (il->cfg->sku & IL_SKU_N)
  4774. hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS |
  4775. NL80211_FEATURE_STATIC_SMPS;
  4776. hw->sta_data_size = sizeof(struct il_station_priv);
  4777. hw->vif_data_size = sizeof(struct il_vif_priv);
  4778. hw->wiphy->interface_modes =
  4779. BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
  4780. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  4781. hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
  4782. REGULATORY_DISABLE_BEACON_HINTS;
  4783. /*
  4784. * For now, disable PS by default because it affects
  4785. * RX performance significantly.
  4786. */
  4787. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  4788. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  4789. /* we create the 802.11 header and a zero-length SSID element */
  4790. hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
  4791. /* Default value; 4 EDCA QOS priorities */
  4792. hw->queues = 4;
  4793. hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
  4794. if (il->bands[NL80211_BAND_2GHZ].n_channels)
  4795. il->hw->wiphy->bands[NL80211_BAND_2GHZ] =
  4796. &il->bands[NL80211_BAND_2GHZ];
  4797. if (il->bands[NL80211_BAND_5GHZ].n_channels)
  4798. il->hw->wiphy->bands[NL80211_BAND_5GHZ] =
  4799. &il->bands[NL80211_BAND_5GHZ];
  4800. il_leds_init(il);
  4801. wiphy_ext_feature_set(il->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  4802. ret = ieee80211_register_hw(il->hw);
  4803. if (ret) {
  4804. IL_ERR("Failed to register hw (error %d)\n", ret);
  4805. return ret;
  4806. }
  4807. il->mac80211_registered = 1;
  4808. return 0;
  4809. }
  4810. int
  4811. il4965_mac_start(struct ieee80211_hw *hw)
  4812. {
  4813. struct il_priv *il = hw->priv;
  4814. int ret;
  4815. D_MAC80211("enter\n");
  4816. /* we should be verifying the device is ready to be opened */
  4817. mutex_lock(&il->mutex);
  4818. ret = __il4965_up(il);
  4819. mutex_unlock(&il->mutex);
  4820. if (ret)
  4821. return ret;
  4822. if (il_is_rfkill(il))
  4823. goto out;
  4824. D_INFO("Start UP work done.\n");
  4825. /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
  4826. * mac80211 will not be run successfully. */
  4827. ret = wait_event_timeout(il->wait_command_queue,
  4828. test_bit(S_READY, &il->status),
  4829. UCODE_READY_TIMEOUT);
  4830. if (!ret) {
  4831. if (!test_bit(S_READY, &il->status)) {
  4832. IL_ERR("START_ALIVE timeout after %dms.\n",
  4833. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  4834. return -ETIMEDOUT;
  4835. }
  4836. }
  4837. il4965_led_enable(il);
  4838. out:
  4839. il->is_open = 1;
  4840. D_MAC80211("leave\n");
  4841. return 0;
  4842. }
  4843. void
  4844. il4965_mac_stop(struct ieee80211_hw *hw, bool suspend)
  4845. {
  4846. struct il_priv *il = hw->priv;
  4847. D_MAC80211("enter\n");
  4848. if (!il->is_open)
  4849. return;
  4850. il->is_open = 0;
  4851. il4965_down(il);
  4852. flush_workqueue(il->workqueue);
  4853. /* User space software may expect getting rfkill changes
  4854. * even if interface is down */
  4855. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  4856. il_enable_rfkill_int(il);
  4857. D_MAC80211("leave\n");
  4858. }
  4859. void
  4860. il4965_mac_tx(struct ieee80211_hw *hw,
  4861. struct ieee80211_tx_control *control,
  4862. struct sk_buff *skb)
  4863. {
  4864. struct il_priv *il = hw->priv;
  4865. D_MACDUMP("enter\n");
  4866. D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  4867. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  4868. if (il4965_tx_skb(il, control->sta, skb))
  4869. dev_kfree_skb_any(skb);
  4870. D_MACDUMP("leave\n");
  4871. }
  4872. void
  4873. il4965_mac_update_tkip_key(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4874. struct ieee80211_key_conf *keyconf,
  4875. struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
  4876. {
  4877. struct il_priv *il = hw->priv;
  4878. D_MAC80211("enter\n");
  4879. il4965_update_tkip_key(il, keyconf, sta, iv32, phase1key);
  4880. D_MAC80211("leave\n");
  4881. }
  4882. int
  4883. il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  4884. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  4885. struct ieee80211_key_conf *key)
  4886. {
  4887. struct il_priv *il = hw->priv;
  4888. int ret;
  4889. u8 sta_id;
  4890. bool is_default_wep_key = false;
  4891. D_MAC80211("enter\n");
  4892. if (il->cfg->mod_params->sw_crypto) {
  4893. D_MAC80211("leave - hwcrypto disabled\n");
  4894. return -EOPNOTSUPP;
  4895. }
  4896. /*
  4897. * To support IBSS RSN, don't program group keys in IBSS, the
  4898. * hardware will then not attempt to decrypt the frames.
  4899. */
  4900. if (vif->type == NL80211_IFTYPE_ADHOC &&
  4901. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  4902. D_MAC80211("leave - ad-hoc group key\n");
  4903. return -EOPNOTSUPP;
  4904. }
  4905. sta_id = il_sta_id_or_broadcast(il, sta);
  4906. if (sta_id == IL_INVALID_STATION)
  4907. return -EINVAL;
  4908. mutex_lock(&il->mutex);
  4909. il_scan_cancel_timeout(il, 100);
  4910. /*
  4911. * If we are getting WEP group key and we didn't receive any key mapping
  4912. * so far, we are in legacy wep mode (group key only), otherwise we are
  4913. * in 1X mode.
  4914. * In legacy wep mode, we use another host command to the uCode.
  4915. */
  4916. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  4917. key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
  4918. if (cmd == SET_KEY)
  4919. is_default_wep_key = !il->_4965.key_mapping_keys;
  4920. else
  4921. is_default_wep_key =
  4922. (key->hw_key_idx == HW_KEY_DEFAULT);
  4923. }
  4924. switch (cmd) {
  4925. case SET_KEY:
  4926. if (is_default_wep_key)
  4927. ret = il4965_set_default_wep_key(il, key);
  4928. else
  4929. ret = il4965_set_dynamic_key(il, key, sta_id);
  4930. D_MAC80211("enable hwcrypto key\n");
  4931. break;
  4932. case DISABLE_KEY:
  4933. if (is_default_wep_key)
  4934. ret = il4965_remove_default_wep_key(il, key);
  4935. else
  4936. ret = il4965_remove_dynamic_key(il, key, sta_id);
  4937. D_MAC80211("disable hwcrypto key\n");
  4938. break;
  4939. default:
  4940. ret = -EINVAL;
  4941. }
  4942. mutex_unlock(&il->mutex);
  4943. D_MAC80211("leave\n");
  4944. return ret;
  4945. }
  4946. int
  4947. il4965_mac_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4948. struct ieee80211_ampdu_params *params)
  4949. {
  4950. struct il_priv *il = hw->priv;
  4951. int ret = -EINVAL;
  4952. struct ieee80211_sta *sta = params->sta;
  4953. enum ieee80211_ampdu_mlme_action action = params->action;
  4954. u16 tid = params->tid;
  4955. u16 *ssn = &params->ssn;
  4956. D_HT("A-MPDU action on addr %pM tid %d\n", sta->addr, tid);
  4957. if (!(il->cfg->sku & IL_SKU_N))
  4958. return -EACCES;
  4959. mutex_lock(&il->mutex);
  4960. switch (action) {
  4961. case IEEE80211_AMPDU_RX_START:
  4962. D_HT("start Rx\n");
  4963. ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
  4964. break;
  4965. case IEEE80211_AMPDU_RX_STOP:
  4966. D_HT("stop Rx\n");
  4967. ret = il4965_sta_rx_agg_stop(il, sta, tid);
  4968. if (test_bit(S_EXIT_PENDING, &il->status))
  4969. ret = 0;
  4970. break;
  4971. case IEEE80211_AMPDU_TX_START:
  4972. D_HT("start Tx\n");
  4973. ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
  4974. break;
  4975. case IEEE80211_AMPDU_TX_STOP_CONT:
  4976. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  4977. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  4978. D_HT("stop Tx\n");
  4979. ret = il4965_tx_agg_stop(il, vif, sta, tid);
  4980. if (test_bit(S_EXIT_PENDING, &il->status))
  4981. ret = 0;
  4982. break;
  4983. case IEEE80211_AMPDU_TX_OPERATIONAL:
  4984. ret = 0;
  4985. break;
  4986. }
  4987. mutex_unlock(&il->mutex);
  4988. return ret;
  4989. }
  4990. int
  4991. il4965_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4992. struct ieee80211_sta *sta)
  4993. {
  4994. struct il_priv *il = hw->priv;
  4995. struct il_station_priv *sta_priv = (void *)sta->drv_priv;
  4996. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  4997. int ret;
  4998. u8 sta_id;
  4999. D_INFO("received request to add station %pM\n", sta->addr);
  5000. mutex_lock(&il->mutex);
  5001. D_INFO("proceeding to add station %pM\n", sta->addr);
  5002. sta_priv->common.sta_id = IL_INVALID_STATION;
  5003. atomic_set(&sta_priv->pending_frames, 0);
  5004. ret =
  5005. il_add_station_common(il, sta->addr, is_ap, sta, &sta_id);
  5006. if (ret) {
  5007. IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
  5008. /* Should we return success if return code is EEXIST ? */
  5009. mutex_unlock(&il->mutex);
  5010. return ret;
  5011. }
  5012. sta_priv->common.sta_id = sta_id;
  5013. /* Initialize rate scaling */
  5014. D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
  5015. il4965_rs_rate_init(il, sta, sta_id);
  5016. mutex_unlock(&il->mutex);
  5017. return 0;
  5018. }
  5019. void
  5020. il4965_mac_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  5021. struct ieee80211_channel_switch *ch_switch)
  5022. {
  5023. struct il_priv *il = hw->priv;
  5024. const struct il_channel_info *ch_info;
  5025. struct ieee80211_conf *conf = &hw->conf;
  5026. struct ieee80211_channel *channel = ch_switch->chandef.chan;
  5027. struct il_ht_config *ht_conf = &il->current_ht_config;
  5028. u16 ch;
  5029. D_MAC80211("enter\n");
  5030. mutex_lock(&il->mutex);
  5031. if (il_is_rfkill(il))
  5032. goto out;
  5033. if (test_bit(S_EXIT_PENDING, &il->status) ||
  5034. test_bit(S_SCANNING, &il->status) ||
  5035. test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
  5036. goto out;
  5037. if (!il_is_associated(il))
  5038. goto out;
  5039. if (!il->ops->set_channel_switch)
  5040. goto out;
  5041. ch = channel->hw_value;
  5042. if (le16_to_cpu(il->active.channel) == ch)
  5043. goto out;
  5044. ch_info = il_get_channel_info(il, channel->band, ch);
  5045. if (!il_is_channel_valid(ch_info)) {
  5046. D_MAC80211("invalid channel\n");
  5047. goto out;
  5048. }
  5049. spin_lock_irq(&il->lock);
  5050. il->current_ht_config.smps = conf->smps_mode;
  5051. /* Configure HT40 channels */
  5052. switch (cfg80211_get_chandef_type(&ch_switch->chandef)) {
  5053. case NL80211_CHAN_NO_HT:
  5054. case NL80211_CHAN_HT20:
  5055. il->ht.is_40mhz = false;
  5056. il->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_NONE;
  5057. break;
  5058. case NL80211_CHAN_HT40MINUS:
  5059. il->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  5060. il->ht.is_40mhz = true;
  5061. break;
  5062. case NL80211_CHAN_HT40PLUS:
  5063. il->ht.extension_chan_offset = IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  5064. il->ht.is_40mhz = true;
  5065. break;
  5066. }
  5067. if ((le16_to_cpu(il->staging.channel) != ch))
  5068. il->staging.flags = 0;
  5069. il_set_rxon_channel(il, channel);
  5070. il_set_rxon_ht(il, ht_conf);
  5071. il_set_flags_for_band(il, channel->band, il->vif);
  5072. spin_unlock_irq(&il->lock);
  5073. il_set_rate(il);
  5074. /*
  5075. * at this point, staging_rxon has the
  5076. * configuration for channel switch
  5077. */
  5078. set_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
  5079. il->switch_channel = cpu_to_le16(ch);
  5080. if (il->ops->set_channel_switch(il, ch_switch)) {
  5081. clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
  5082. il->switch_channel = 0;
  5083. ieee80211_chswitch_done(il->vif, false, 0);
  5084. }
  5085. out:
  5086. mutex_unlock(&il->mutex);
  5087. D_MAC80211("leave\n");
  5088. }
  5089. void
  5090. il4965_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
  5091. unsigned int *total_flags, u64 multicast)
  5092. {
  5093. struct il_priv *il = hw->priv;
  5094. __le32 filter_or = 0, filter_nand = 0;
  5095. #define CHK(test, flag) do { \
  5096. if (*total_flags & (test)) \
  5097. filter_or |= (flag); \
  5098. else \
  5099. filter_nand |= (flag); \
  5100. } while (0)
  5101. D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
  5102. *total_flags);
  5103. CHK(FIF_OTHER_BSS, RXON_FILTER_PROMISC_MSK);
  5104. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  5105. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  5106. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  5107. #undef CHK
  5108. mutex_lock(&il->mutex);
  5109. il->staging.filter_flags &= ~filter_nand;
  5110. il->staging.filter_flags |= filter_or;
  5111. /*
  5112. * Not committing directly because hardware can perform a scan,
  5113. * but we'll eventually commit the filter flags change anyway.
  5114. */
  5115. mutex_unlock(&il->mutex);
  5116. /*
  5117. * Receiving all multicast frames is always enabled by the
  5118. * default flags setup in il_connection_init_rx_config()
  5119. * since we currently do not support programming multicast
  5120. * filters into the device.
  5121. */
  5122. *total_flags &=
  5123. FIF_OTHER_BSS | FIF_ALLMULTI |
  5124. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  5125. }
  5126. /*****************************************************************************
  5127. *
  5128. * driver setup and teardown
  5129. *
  5130. *****************************************************************************/
  5131. static void
  5132. il4965_bg_txpower_work(struct work_struct *work)
  5133. {
  5134. struct il_priv *il = container_of(work, struct il_priv,
  5135. txpower_work);
  5136. mutex_lock(&il->mutex);
  5137. /* If a scan happened to start before we got here
  5138. * then just return; the stats notification will
  5139. * kick off another scheduled work to compensate for
  5140. * any temperature delta we missed here. */
  5141. if (test_bit(S_EXIT_PENDING, &il->status) ||
  5142. test_bit(S_SCANNING, &il->status))
  5143. goto out;
  5144. /* Regardless of if we are associated, we must reconfigure the
  5145. * TX power since frames can be sent on non-radar channels while
  5146. * not associated */
  5147. il->ops->send_tx_power(il);
  5148. /* Update last_temperature to keep is_calib_needed from running
  5149. * when it isn't needed... */
  5150. il->last_temperature = il->temperature;
  5151. out:
  5152. mutex_unlock(&il->mutex);
  5153. }
  5154. static int
  5155. il4965_setup_deferred_work(struct il_priv *il)
  5156. {
  5157. il->workqueue = create_singlethread_workqueue(DRV_NAME);
  5158. if (!il->workqueue)
  5159. return -ENOMEM;
  5160. init_waitqueue_head(&il->wait_command_queue);
  5161. INIT_WORK(&il->restart, il4965_bg_restart);
  5162. INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
  5163. INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
  5164. INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
  5165. INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
  5166. il_setup_scan_deferred_work(il);
  5167. INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
  5168. timer_setup(&il->stats_periodic, il4965_bg_stats_periodic, 0);
  5169. timer_setup(&il->watchdog, il_bg_watchdog, 0);
  5170. tasklet_setup(&il->irq_tasklet, il4965_irq_tasklet);
  5171. return 0;
  5172. }
  5173. static void
  5174. il4965_cancel_deferred_work(struct il_priv *il)
  5175. {
  5176. cancel_work_sync(&il->txpower_work);
  5177. cancel_delayed_work_sync(&il->init_alive_start);
  5178. cancel_delayed_work(&il->alive_start);
  5179. cancel_work_sync(&il->run_time_calib_work);
  5180. il_cancel_scan_deferred_work(il);
  5181. del_timer_sync(&il->stats_periodic);
  5182. }
  5183. static void
  5184. il4965_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
  5185. {
  5186. int i;
  5187. for (i = 0; i < RATE_COUNT_LEGACY; i++) {
  5188. rates[i].bitrate = il_rates[i].ieee * 5;
  5189. rates[i].hw_value = i; /* Rate scaling will work on idxes */
  5190. rates[i].hw_value_short = i;
  5191. rates[i].flags = 0;
  5192. if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
  5193. /*
  5194. * If CCK != 1M then set short preamble rate flag.
  5195. */
  5196. rates[i].flags |=
  5197. (il_rates[i].plcp ==
  5198. RATE_1M_PLCP) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  5199. }
  5200. }
  5201. }
  5202. /*
  5203. * Acquire il->lock before calling this function !
  5204. */
  5205. void
  5206. il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
  5207. {
  5208. il_wr(il, HBUS_TARG_WRPTR, (idx & 0xff) | (txq_id << 8));
  5209. il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
  5210. }
  5211. void
  5212. il4965_tx_queue_set_status(struct il_priv *il, struct il_tx_queue *txq,
  5213. int tx_fifo_id, int scd_retry)
  5214. {
  5215. int txq_id = txq->q.id;
  5216. /* Find out whether to activate Tx queue */
  5217. int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
  5218. /* Set up and activate */
  5219. il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
  5220. (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  5221. (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
  5222. (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
  5223. (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
  5224. IL49_SCD_QUEUE_STTS_REG_MSK);
  5225. txq->sched_retry = scd_retry;
  5226. D_INFO("%s %s Queue %d on AC %d\n", active ? "Activate" : "Deactivate",
  5227. scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
  5228. }
  5229. static const struct ieee80211_ops il4965_mac_ops = {
  5230. .add_chanctx = ieee80211_emulate_add_chanctx,
  5231. .remove_chanctx = ieee80211_emulate_remove_chanctx,
  5232. .change_chanctx = ieee80211_emulate_change_chanctx,
  5233. .switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
  5234. .tx = il4965_mac_tx,
  5235. .wake_tx_queue = ieee80211_handle_wake_tx_queue,
  5236. .start = il4965_mac_start,
  5237. .stop = il4965_mac_stop,
  5238. .add_interface = il_mac_add_interface,
  5239. .remove_interface = il_mac_remove_interface,
  5240. .change_interface = il_mac_change_interface,
  5241. .config = il_mac_config,
  5242. .configure_filter = il4965_configure_filter,
  5243. .set_key = il4965_mac_set_key,
  5244. .update_tkip_key = il4965_mac_update_tkip_key,
  5245. .conf_tx = il_mac_conf_tx,
  5246. .reset_tsf = il_mac_reset_tsf,
  5247. .bss_info_changed = il_mac_bss_info_changed,
  5248. .ampdu_action = il4965_mac_ampdu_action,
  5249. .hw_scan = il_mac_hw_scan,
  5250. .sta_add = il4965_mac_sta_add,
  5251. .sta_remove = il_mac_sta_remove,
  5252. .channel_switch = il4965_mac_channel_switch,
  5253. .tx_last_beacon = il_mac_tx_last_beacon,
  5254. .flush = il_mac_flush,
  5255. };
  5256. static int
  5257. il4965_init_drv(struct il_priv *il)
  5258. {
  5259. int ret;
  5260. spin_lock_init(&il->sta_lock);
  5261. spin_lock_init(&il->hcmd_lock);
  5262. INIT_LIST_HEAD(&il->free_frames);
  5263. mutex_init(&il->mutex);
  5264. il->ieee_channels = NULL;
  5265. il->ieee_rates = NULL;
  5266. il->band = NL80211_BAND_2GHZ;
  5267. il->iw_mode = NL80211_IFTYPE_STATION;
  5268. il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
  5269. il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
  5270. /* initialize force reset */
  5271. il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
  5272. /* Choose which receivers/antennas to use */
  5273. if (il->ops->set_rxon_chain)
  5274. il->ops->set_rxon_chain(il);
  5275. il_init_scan_params(il);
  5276. ret = il_init_channel_map(il);
  5277. if (ret) {
  5278. IL_ERR("initializing regulatory failed: %d\n", ret);
  5279. goto err;
  5280. }
  5281. ret = il_init_geos(il);
  5282. if (ret) {
  5283. IL_ERR("initializing geos failed: %d\n", ret);
  5284. goto err_free_channel_map;
  5285. }
  5286. il4965_init_hw_rates(il, il->ieee_rates);
  5287. return 0;
  5288. err_free_channel_map:
  5289. il_free_channel_map(il);
  5290. err:
  5291. return ret;
  5292. }
  5293. static void
  5294. il4965_uninit_drv(struct il_priv *il)
  5295. {
  5296. il_free_geos(il);
  5297. il_free_channel_map(il);
  5298. kfree(il->scan_cmd);
  5299. }
  5300. static void
  5301. il4965_hw_detect(struct il_priv *il)
  5302. {
  5303. il->hw_rev = _il_rd(il, CSR_HW_REV);
  5304. il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
  5305. il->rev_id = il->pci_dev->revision;
  5306. D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
  5307. }
  5308. static const struct il_sensitivity_ranges il4965_sensitivity = {
  5309. .min_nrg_cck = 97,
  5310. .max_nrg_cck = 0, /* not used, set to 0 */
  5311. .auto_corr_min_ofdm = 85,
  5312. .auto_corr_min_ofdm_mrc = 170,
  5313. .auto_corr_min_ofdm_x1 = 105,
  5314. .auto_corr_min_ofdm_mrc_x1 = 220,
  5315. .auto_corr_max_ofdm = 120,
  5316. .auto_corr_max_ofdm_mrc = 210,
  5317. .auto_corr_max_ofdm_x1 = 140,
  5318. .auto_corr_max_ofdm_mrc_x1 = 270,
  5319. .auto_corr_min_cck = 125,
  5320. .auto_corr_max_cck = 200,
  5321. .auto_corr_min_cck_mrc = 200,
  5322. .auto_corr_max_cck_mrc = 400,
  5323. .nrg_th_cck = 100,
  5324. .nrg_th_ofdm = 100,
  5325. .barker_corr_th_min = 190,
  5326. .barker_corr_th_min_mrc = 390,
  5327. .nrg_th_cca = 62,
  5328. };
  5329. static void
  5330. il4965_set_hw_params(struct il_priv *il)
  5331. {
  5332. il->hw_params.bcast_id = IL4965_BROADCAST_ID;
  5333. il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
  5334. il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
  5335. if (il->cfg->mod_params->amsdu_size_8K)
  5336. il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
  5337. else
  5338. il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
  5339. il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
  5340. if (il->cfg->mod_params->disable_11n)
  5341. il->cfg->sku &= ~IL_SKU_N;
  5342. if (il->cfg->mod_params->num_of_queues >= IL_MIN_NUM_QUEUES &&
  5343. il->cfg->mod_params->num_of_queues <= IL49_NUM_QUEUES)
  5344. il->cfg->num_of_queues =
  5345. il->cfg->mod_params->num_of_queues;
  5346. il->hw_params.max_txq_num = il->cfg->num_of_queues;
  5347. il->hw_params.dma_chnl_num = FH49_TCSR_CHNL_NUM;
  5348. il->hw_params.scd_bc_tbls_size =
  5349. il->cfg->num_of_queues *
  5350. sizeof(struct il4965_scd_bc_tbl);
  5351. il->hw_params.tfd_size = sizeof(struct il_tfd);
  5352. il->hw_params.max_stations = IL4965_STATION_COUNT;
  5353. il->hw_params.max_data_size = IL49_RTC_DATA_SIZE;
  5354. il->hw_params.max_inst_size = IL49_RTC_INST_SIZE;
  5355. il->hw_params.max_bsm_size = BSM_SRAM_SIZE;
  5356. il->hw_params.ht40_channel = BIT(NL80211_BAND_5GHZ);
  5357. il->hw_params.rx_wrt_ptr_reg = FH49_RSCSR_CHNL0_WPTR;
  5358. il->hw_params.tx_chains_num = il4965_num_of_ant(il->cfg->valid_tx_ant);
  5359. il->hw_params.rx_chains_num = il4965_num_of_ant(il->cfg->valid_rx_ant);
  5360. il->hw_params.valid_tx_ant = il->cfg->valid_tx_ant;
  5361. il->hw_params.valid_rx_ant = il->cfg->valid_rx_ant;
  5362. il->hw_params.ct_kill_threshold =
  5363. celsius_to_kelvin(CT_KILL_THRESHOLD_LEGACY);
  5364. il->hw_params.sens = &il4965_sensitivity;
  5365. il->hw_params.beacon_time_tsf_bits = IL4965_EXT_BEACON_TIME_POS;
  5366. }
  5367. static int
  5368. il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  5369. {
  5370. int err = 0;
  5371. struct il_priv *il;
  5372. struct ieee80211_hw *hw;
  5373. struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
  5374. unsigned long flags;
  5375. u16 pci_cmd;
  5376. /************************
  5377. * 1. Allocating HW data
  5378. ************************/
  5379. hw = ieee80211_alloc_hw(sizeof(struct il_priv), &il4965_mac_ops);
  5380. if (!hw) {
  5381. err = -ENOMEM;
  5382. goto out;
  5383. }
  5384. il = hw->priv;
  5385. il->hw = hw;
  5386. SET_IEEE80211_DEV(hw, &pdev->dev);
  5387. D_INFO("*** LOAD DRIVER ***\n");
  5388. il->cfg = cfg;
  5389. il->ops = &il4965_ops;
  5390. #ifdef CONFIG_IWLEGACY_DEBUGFS
  5391. il->debugfs_ops = &il4965_debugfs_ops;
  5392. #endif
  5393. il->pci_dev = pdev;
  5394. il->inta_mask = CSR_INI_SET_MASK;
  5395. /**************************
  5396. * 2. Initializing PCI bus
  5397. **************************/
  5398. pci_disable_link_state(pdev,
  5399. PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  5400. PCIE_LINK_STATE_CLKPM);
  5401. if (pci_enable_device(pdev)) {
  5402. err = -ENODEV;
  5403. goto out_ieee80211_free_hw;
  5404. }
  5405. pci_set_master(pdev);
  5406. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(36));
  5407. if (err) {
  5408. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  5409. /* both attempts failed: */
  5410. if (err) {
  5411. IL_WARN("No suitable DMA available.\n");
  5412. goto out_pci_disable_device;
  5413. }
  5414. }
  5415. err = pci_request_regions(pdev, DRV_NAME);
  5416. if (err)
  5417. goto out_pci_disable_device;
  5418. pci_set_drvdata(pdev, il);
  5419. /***********************
  5420. * 3. Read REV register
  5421. ***********************/
  5422. il->hw_base = pci_ioremap_bar(pdev, 0);
  5423. if (!il->hw_base) {
  5424. err = -ENODEV;
  5425. goto out_pci_release_regions;
  5426. }
  5427. D_INFO("pci_resource_len = 0x%08llx\n",
  5428. (unsigned long long)pci_resource_len(pdev, 0));
  5429. D_INFO("pci_resource_base = %p\n", il->hw_base);
  5430. /* these spin locks will be used in apm_ops.init and EEPROM access
  5431. * we should init now
  5432. */
  5433. spin_lock_init(&il->reg_lock);
  5434. spin_lock_init(&il->lock);
  5435. /*
  5436. * stop and reset the on-board processor just in case it is in a
  5437. * strange state ... like being left stranded by a primary kernel
  5438. * and this is now the kdump kernel trying to start up
  5439. */
  5440. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  5441. il4965_hw_detect(il);
  5442. IL_INFO("Detected %s, REV=0x%X\n", il->cfg->name, il->hw_rev);
  5443. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  5444. * PCI Tx retries from interfering with C3 CPU state */
  5445. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  5446. il4965_prepare_card_hw(il);
  5447. if (!il->hw_ready) {
  5448. IL_WARN("Failed, HW not ready\n");
  5449. err = -EIO;
  5450. goto out_iounmap;
  5451. }
  5452. /*****************
  5453. * 4. Read EEPROM
  5454. *****************/
  5455. /* Read the EEPROM */
  5456. err = il_eeprom_init(il);
  5457. if (err) {
  5458. IL_ERR("Unable to init EEPROM\n");
  5459. goto out_iounmap;
  5460. }
  5461. err = il4965_eeprom_check_version(il);
  5462. if (err)
  5463. goto out_free_eeprom;
  5464. /* extract MAC Address */
  5465. il4965_eeprom_get_mac(il, il->addresses[0].addr);
  5466. D_INFO("MAC address: %pM\n", il->addresses[0].addr);
  5467. il->hw->wiphy->addresses = il->addresses;
  5468. il->hw->wiphy->n_addresses = 1;
  5469. /************************
  5470. * 5. Setup HW constants
  5471. ************************/
  5472. il4965_set_hw_params(il);
  5473. /*******************
  5474. * 6. Setup il
  5475. *******************/
  5476. err = il4965_init_drv(il);
  5477. if (err)
  5478. goto out_free_eeprom;
  5479. /* At this point both hw and il are initialized. */
  5480. /********************
  5481. * 7. Setup services
  5482. ********************/
  5483. spin_lock_irqsave(&il->lock, flags);
  5484. il_disable_interrupts(il);
  5485. spin_unlock_irqrestore(&il->lock, flags);
  5486. pci_enable_msi(il->pci_dev);
  5487. err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
  5488. if (err) {
  5489. IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
  5490. goto out_disable_msi;
  5491. }
  5492. err = il4965_setup_deferred_work(il);
  5493. if (err)
  5494. goto out_free_irq;
  5495. il4965_setup_handlers(il);
  5496. /*********************************************
  5497. * 8. Enable interrupts and read RFKILL state
  5498. *********************************************/
  5499. /* enable rfkill interrupt: hw bug w/a */
  5500. pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
  5501. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  5502. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  5503. pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
  5504. }
  5505. il_enable_rfkill_int(il);
  5506. /* If platform's RF_KILL switch is NOT set to KILL */
  5507. if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  5508. clear_bit(S_RFKILL, &il->status);
  5509. else
  5510. set_bit(S_RFKILL, &il->status);
  5511. wiphy_rfkill_set_hw_state(il->hw->wiphy,
  5512. test_bit(S_RFKILL, &il->status));
  5513. il_power_initialize(il);
  5514. init_completion(&il->_4965.firmware_loading_complete);
  5515. err = il4965_request_firmware(il, true);
  5516. if (err)
  5517. goto out_destroy_workqueue;
  5518. return 0;
  5519. out_destroy_workqueue:
  5520. destroy_workqueue(il->workqueue);
  5521. il->workqueue = NULL;
  5522. out_free_irq:
  5523. free_irq(il->pci_dev->irq, il);
  5524. out_disable_msi:
  5525. pci_disable_msi(il->pci_dev);
  5526. il4965_uninit_drv(il);
  5527. out_free_eeprom:
  5528. il_eeprom_free(il);
  5529. out_iounmap:
  5530. iounmap(il->hw_base);
  5531. out_pci_release_regions:
  5532. pci_release_regions(pdev);
  5533. out_pci_disable_device:
  5534. pci_disable_device(pdev);
  5535. out_ieee80211_free_hw:
  5536. ieee80211_free_hw(il->hw);
  5537. out:
  5538. return err;
  5539. }
  5540. static void
  5541. il4965_pci_remove(struct pci_dev *pdev)
  5542. {
  5543. struct il_priv *il = pci_get_drvdata(pdev);
  5544. unsigned long flags;
  5545. if (!il)
  5546. return;
  5547. wait_for_completion(&il->_4965.firmware_loading_complete);
  5548. D_INFO("*** UNLOAD DRIVER ***\n");
  5549. il_dbgfs_unregister(il);
  5550. sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
  5551. /* ieee80211_unregister_hw call wil cause il_mac_stop to
  5552. * be called and il4965_down since we are removing the device
  5553. * we need to set S_EXIT_PENDING bit.
  5554. */
  5555. set_bit(S_EXIT_PENDING, &il->status);
  5556. il_leds_exit(il);
  5557. if (il->mac80211_registered) {
  5558. ieee80211_unregister_hw(il->hw);
  5559. il->mac80211_registered = 0;
  5560. } else {
  5561. il4965_down(il);
  5562. }
  5563. /*
  5564. * Make sure device is reset to low power before unloading driver.
  5565. * This may be redundant with il4965_down(), but there are paths to
  5566. * run il4965_down() without calling apm_ops.stop(), and there are
  5567. * paths to avoid running il4965_down() at all before leaving driver.
  5568. * This (inexpensive) call *makes sure* device is reset.
  5569. */
  5570. il_apm_stop(il);
  5571. /* make sure we flush any pending irq or
  5572. * tasklet for the driver
  5573. */
  5574. spin_lock_irqsave(&il->lock, flags);
  5575. il_disable_interrupts(il);
  5576. spin_unlock_irqrestore(&il->lock, flags);
  5577. il4965_synchronize_irq(il);
  5578. il4965_dealloc_ucode_pci(il);
  5579. if (il->rxq.bd)
  5580. il4965_rx_queue_free(il, &il->rxq);
  5581. il4965_hw_txq_ctx_free(il);
  5582. il_eeprom_free(il);
  5583. /*netif_stop_queue(dev); */
  5584. /* ieee80211_unregister_hw calls il_mac_stop, which flushes
  5585. * il->workqueue... so we can't take down the workqueue
  5586. * until now... */
  5587. destroy_workqueue(il->workqueue);
  5588. il->workqueue = NULL;
  5589. free_irq(il->pci_dev->irq, il);
  5590. pci_disable_msi(il->pci_dev);
  5591. iounmap(il->hw_base);
  5592. pci_release_regions(pdev);
  5593. pci_disable_device(pdev);
  5594. il4965_uninit_drv(il);
  5595. dev_kfree_skb(il->beacon_skb);
  5596. ieee80211_free_hw(il->hw);
  5597. }
  5598. /*
  5599. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  5600. * must be called under il->lock and mac access
  5601. */
  5602. void
  5603. il4965_txq_set_sched(struct il_priv *il, u32 mask)
  5604. {
  5605. il_wr_prph(il, IL49_SCD_TXFACT, mask);
  5606. }
  5607. /*****************************************************************************
  5608. *
  5609. * driver and module entry point
  5610. *
  5611. *****************************************************************************/
  5612. /* Hardware specific file defines the PCI IDs table for that hardware module */
  5613. static const struct pci_device_id il4965_hw_card_ids[] = {
  5614. {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
  5615. {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
  5616. {0}
  5617. };
  5618. MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
  5619. static struct pci_driver il4965_driver = {
  5620. .name = DRV_NAME,
  5621. .id_table = il4965_hw_card_ids,
  5622. .probe = il4965_pci_probe,
  5623. .remove = il4965_pci_remove,
  5624. .driver.pm = IL_LEGACY_PM_OPS,
  5625. };
  5626. static int __init
  5627. il4965_init(void)
  5628. {
  5629. int ret;
  5630. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  5631. pr_info(DRV_COPYRIGHT "\n");
  5632. ret = il4965_rate_control_register();
  5633. if (ret) {
  5634. pr_err("Unable to register rate control algorithm: %d\n", ret);
  5635. return ret;
  5636. }
  5637. ret = pci_register_driver(&il4965_driver);
  5638. if (ret) {
  5639. pr_err("Unable to initialize PCI module\n");
  5640. goto error_register;
  5641. }
  5642. return ret;
  5643. error_register:
  5644. il4965_rate_control_unregister();
  5645. return ret;
  5646. }
  5647. static void __exit
  5648. il4965_exit(void)
  5649. {
  5650. pci_unregister_driver(&il4965_driver);
  5651. il4965_rate_control_unregister();
  5652. }
  5653. module_exit(il4965_exit);
  5654. module_init(il4965_init);
  5655. #ifdef CONFIG_IWLEGACY_DEBUG
  5656. module_param_named(debug, il_debug_level, uint, 0644);
  5657. MODULE_PARM_DESC(debug, "debug output mask");
  5658. #endif
  5659. module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, 0444);
  5660. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  5661. module_param_named(queues_num, il4965_mod_params.num_of_queues, int, 0444);
  5662. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  5663. module_param_named(11n_disable, il4965_mod_params.disable_11n, int, 0444);
  5664. MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
  5665. module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K, int, 0444);
  5666. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size (default 0 [disabled])");
  5667. module_param_named(fw_restart, il4965_mod_params.restart_fw, int, 0444);
  5668. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");