4965-mac.c 186 KB

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