4965-mac.c 184 KB

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