common.c 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  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. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/etherdevice.h>
  31. #include <linux/sched.h>
  32. #include <linux/slab.h>
  33. #include <linux/types.h>
  34. #include <linux/lockdep.h>
  35. #include <linux/init.h>
  36. #include <linux/pci.h>
  37. #include <linux/dma-mapping.h>
  38. #include <linux/delay.h>
  39. #include <linux/skbuff.h>
  40. #include <net/mac80211.h>
  41. #include "common.h"
  42. int
  43. _il_poll_bit(struct il_priv *il, u32 addr, u32 bits, u32 mask, int timeout)
  44. {
  45. const int interval = 10; /* microseconds */
  46. int t = 0;
  47. do {
  48. if ((_il_rd(il, addr) & mask) == (bits & mask))
  49. return t;
  50. udelay(interval);
  51. t += interval;
  52. } while (t < timeout);
  53. return -ETIMEDOUT;
  54. }
  55. EXPORT_SYMBOL(_il_poll_bit);
  56. void
  57. il_set_bit(struct il_priv *p, u32 r, u32 m)
  58. {
  59. unsigned long reg_flags;
  60. spin_lock_irqsave(&p->reg_lock, reg_flags);
  61. _il_set_bit(p, r, m);
  62. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  63. }
  64. EXPORT_SYMBOL(il_set_bit);
  65. void
  66. il_clear_bit(struct il_priv *p, u32 r, u32 m)
  67. {
  68. unsigned long reg_flags;
  69. spin_lock_irqsave(&p->reg_lock, reg_flags);
  70. _il_clear_bit(p, r, m);
  71. spin_unlock_irqrestore(&p->reg_lock, reg_flags);
  72. }
  73. EXPORT_SYMBOL(il_clear_bit);
  74. int
  75. _il_grab_nic_access(struct il_priv *il)
  76. {
  77. int ret;
  78. u32 val;
  79. /* this bit wakes up the NIC */
  80. _il_set_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  81. /*
  82. * These bits say the device is running, and should keep running for
  83. * at least a short while (at least as long as MAC_ACCESS_REQ stays 1),
  84. * but they do not indicate that embedded SRAM is restored yet;
  85. * 3945 and 4965 have volatile SRAM, and must save/restore contents
  86. * to/from host DRAM when sleeping/waking for power-saving.
  87. * Each direction takes approximately 1/4 millisecond; with this
  88. * overhead, it's a good idea to grab and hold MAC_ACCESS_REQUEST if a
  89. * series of register accesses are expected (e.g. reading Event Log),
  90. * to keep device from sleeping.
  91. *
  92. * CSR_UCODE_DRV_GP1 register bit MAC_SLEEP == 0 indicates that
  93. * SRAM is okay/restored. We don't check that here because this call
  94. * is just for hardware register access; but GP1 MAC_SLEEP check is a
  95. * good idea before accessing 3945/4965 SRAM (e.g. reading Event Log).
  96. *
  97. */
  98. ret =
  99. _il_poll_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  100. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  101. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000);
  102. if (ret < 0) {
  103. val = _il_rd(il, CSR_GP_CNTRL);
  104. IL_ERR("MAC is in deep sleep!. CSR_GP_CNTRL = 0x%08X\n", val);
  105. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_FORCE_NMI);
  106. return -EIO;
  107. }
  108. return 0;
  109. }
  110. EXPORT_SYMBOL_GPL(_il_grab_nic_access);
  111. int
  112. il_poll_bit(struct il_priv *il, u32 addr, u32 mask, int timeout)
  113. {
  114. const int interval = 10; /* microseconds */
  115. int t = 0;
  116. do {
  117. if ((il_rd(il, addr) & mask) == mask)
  118. return t;
  119. udelay(interval);
  120. t += interval;
  121. } while (t < timeout);
  122. return -ETIMEDOUT;
  123. }
  124. EXPORT_SYMBOL(il_poll_bit);
  125. u32
  126. il_rd_prph(struct il_priv *il, u32 reg)
  127. {
  128. unsigned long reg_flags;
  129. u32 val;
  130. spin_lock_irqsave(&il->reg_lock, reg_flags);
  131. _il_grab_nic_access(il);
  132. val = _il_rd_prph(il, reg);
  133. _il_release_nic_access(il);
  134. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  135. return val;
  136. }
  137. EXPORT_SYMBOL(il_rd_prph);
  138. void
  139. il_wr_prph(struct il_priv *il, u32 addr, u32 val)
  140. {
  141. unsigned long reg_flags;
  142. spin_lock_irqsave(&il->reg_lock, reg_flags);
  143. if (!_il_grab_nic_access(il)) {
  144. _il_wr_prph(il, addr, val);
  145. _il_release_nic_access(il);
  146. }
  147. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  148. }
  149. EXPORT_SYMBOL(il_wr_prph);
  150. u32
  151. il_read_targ_mem(struct il_priv *il, u32 addr)
  152. {
  153. unsigned long reg_flags;
  154. u32 value;
  155. spin_lock_irqsave(&il->reg_lock, reg_flags);
  156. _il_grab_nic_access(il);
  157. _il_wr(il, HBUS_TARG_MEM_RADDR, addr);
  158. rmb();
  159. value = _il_rd(il, HBUS_TARG_MEM_RDAT);
  160. _il_release_nic_access(il);
  161. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  162. return value;
  163. }
  164. EXPORT_SYMBOL(il_read_targ_mem);
  165. void
  166. il_write_targ_mem(struct il_priv *il, u32 addr, u32 val)
  167. {
  168. unsigned long reg_flags;
  169. spin_lock_irqsave(&il->reg_lock, reg_flags);
  170. if (!_il_grab_nic_access(il)) {
  171. _il_wr(il, HBUS_TARG_MEM_WADDR, addr);
  172. wmb();
  173. _il_wr(il, HBUS_TARG_MEM_WDAT, val);
  174. _il_release_nic_access(il);
  175. }
  176. spin_unlock_irqrestore(&il->reg_lock, reg_flags);
  177. }
  178. EXPORT_SYMBOL(il_write_targ_mem);
  179. const char *
  180. il_get_cmd_string(u8 cmd)
  181. {
  182. switch (cmd) {
  183. IL_CMD(N_ALIVE);
  184. IL_CMD(N_ERROR);
  185. IL_CMD(C_RXON);
  186. IL_CMD(C_RXON_ASSOC);
  187. IL_CMD(C_QOS_PARAM);
  188. IL_CMD(C_RXON_TIMING);
  189. IL_CMD(C_ADD_STA);
  190. IL_CMD(C_REM_STA);
  191. IL_CMD(C_WEPKEY);
  192. IL_CMD(N_3945_RX);
  193. IL_CMD(C_TX);
  194. IL_CMD(C_RATE_SCALE);
  195. IL_CMD(C_LEDS);
  196. IL_CMD(C_TX_LINK_QUALITY_CMD);
  197. IL_CMD(C_CHANNEL_SWITCH);
  198. IL_CMD(N_CHANNEL_SWITCH);
  199. IL_CMD(C_SPECTRUM_MEASUREMENT);
  200. IL_CMD(N_SPECTRUM_MEASUREMENT);
  201. IL_CMD(C_POWER_TBL);
  202. IL_CMD(N_PM_SLEEP);
  203. IL_CMD(N_PM_DEBUG_STATS);
  204. IL_CMD(C_SCAN);
  205. IL_CMD(C_SCAN_ABORT);
  206. IL_CMD(N_SCAN_START);
  207. IL_CMD(N_SCAN_RESULTS);
  208. IL_CMD(N_SCAN_COMPLETE);
  209. IL_CMD(N_BEACON);
  210. IL_CMD(C_TX_BEACON);
  211. IL_CMD(C_TX_PWR_TBL);
  212. IL_CMD(C_BT_CONFIG);
  213. IL_CMD(C_STATS);
  214. IL_CMD(N_STATS);
  215. IL_CMD(N_CARD_STATE);
  216. IL_CMD(N_MISSED_BEACONS);
  217. IL_CMD(C_CT_KILL_CONFIG);
  218. IL_CMD(C_SENSITIVITY);
  219. IL_CMD(C_PHY_CALIBRATION);
  220. IL_CMD(N_RX_PHY);
  221. IL_CMD(N_RX_MPDU);
  222. IL_CMD(N_RX);
  223. IL_CMD(N_COMPRESSED_BA);
  224. default:
  225. return "UNKNOWN";
  226. }
  227. }
  228. EXPORT_SYMBOL(il_get_cmd_string);
  229. #define HOST_COMPLETE_TIMEOUT (HZ / 2)
  230. static void
  231. il_generic_cmd_callback(struct il_priv *il, struct il_device_cmd *cmd,
  232. struct il_rx_pkt *pkt)
  233. {
  234. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  235. IL_ERR("Bad return from %s (0x%08X)\n",
  236. il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  237. return;
  238. }
  239. #ifdef CONFIG_IWLEGACY_DEBUG
  240. switch (cmd->hdr.cmd) {
  241. case C_TX_LINK_QUALITY_CMD:
  242. case C_SENSITIVITY:
  243. D_HC_DUMP("back from %s (0x%08X)\n",
  244. il_get_cmd_string(cmd->hdr.cmd), pkt->hdr.flags);
  245. break;
  246. default:
  247. D_HC("back from %s (0x%08X)\n", il_get_cmd_string(cmd->hdr.cmd),
  248. pkt->hdr.flags);
  249. }
  250. #endif
  251. }
  252. static int
  253. il_send_cmd_async(struct il_priv *il, struct il_host_cmd *cmd)
  254. {
  255. int ret;
  256. BUG_ON(!(cmd->flags & CMD_ASYNC));
  257. /* An asynchronous command can not expect an SKB to be set. */
  258. BUG_ON(cmd->flags & CMD_WANT_SKB);
  259. /* Assign a generic callback if one is not provided */
  260. if (!cmd->callback)
  261. cmd->callback = il_generic_cmd_callback;
  262. if (test_bit(S_EXIT_PENDING, &il->status))
  263. return -EBUSY;
  264. ret = il_enqueue_hcmd(il, cmd);
  265. if (ret < 0) {
  266. IL_ERR("Error sending %s: enqueue_hcmd failed: %d\n",
  267. il_get_cmd_string(cmd->id), ret);
  268. return ret;
  269. }
  270. return 0;
  271. }
  272. int
  273. il_send_cmd_sync(struct il_priv *il, struct il_host_cmd *cmd)
  274. {
  275. int cmd_idx;
  276. int ret;
  277. lockdep_assert_held(&il->mutex);
  278. BUG_ON(cmd->flags & CMD_ASYNC);
  279. /* A synchronous command can not have a callback set. */
  280. BUG_ON(cmd->callback);
  281. D_INFO("Attempting to send sync command %s\n",
  282. il_get_cmd_string(cmd->id));
  283. set_bit(S_HCMD_ACTIVE, &il->status);
  284. D_INFO("Setting HCMD_ACTIVE for command %s\n",
  285. il_get_cmd_string(cmd->id));
  286. cmd_idx = il_enqueue_hcmd(il, cmd);
  287. if (cmd_idx < 0) {
  288. ret = cmd_idx;
  289. IL_ERR("Error sending %s: enqueue_hcmd failed: %d\n",
  290. il_get_cmd_string(cmd->id), ret);
  291. goto out;
  292. }
  293. ret = wait_event_timeout(il->wait_command_queue,
  294. !test_bit(S_HCMD_ACTIVE, &il->status),
  295. HOST_COMPLETE_TIMEOUT);
  296. if (!ret) {
  297. if (test_bit(S_HCMD_ACTIVE, &il->status)) {
  298. IL_ERR("Error sending %s: time out after %dms.\n",
  299. il_get_cmd_string(cmd->id),
  300. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  301. clear_bit(S_HCMD_ACTIVE, &il->status);
  302. D_INFO("Clearing HCMD_ACTIVE for command %s\n",
  303. il_get_cmd_string(cmd->id));
  304. ret = -ETIMEDOUT;
  305. goto cancel;
  306. }
  307. }
  308. if (test_bit(S_RF_KILL_HW, &il->status)) {
  309. IL_ERR("Command %s aborted: RF KILL Switch\n",
  310. il_get_cmd_string(cmd->id));
  311. ret = -ECANCELED;
  312. goto fail;
  313. }
  314. if (test_bit(S_FW_ERROR, &il->status)) {
  315. IL_ERR("Command %s failed: FW Error\n",
  316. il_get_cmd_string(cmd->id));
  317. ret = -EIO;
  318. goto fail;
  319. }
  320. if ((cmd->flags & CMD_WANT_SKB) && !cmd->reply_page) {
  321. IL_ERR("Error: Response NULL in '%s'\n",
  322. il_get_cmd_string(cmd->id));
  323. ret = -EIO;
  324. goto cancel;
  325. }
  326. ret = 0;
  327. goto out;
  328. cancel:
  329. if (cmd->flags & CMD_WANT_SKB) {
  330. /*
  331. * Cancel the CMD_WANT_SKB flag for the cmd in the
  332. * TX cmd queue. Otherwise in case the cmd comes
  333. * in later, it will possibly set an invalid
  334. * address (cmd->meta.source).
  335. */
  336. il->txq[il->cmd_queue].meta[cmd_idx].flags &= ~CMD_WANT_SKB;
  337. }
  338. fail:
  339. if (cmd->reply_page) {
  340. il_free_pages(il, cmd->reply_page);
  341. cmd->reply_page = 0;
  342. }
  343. out:
  344. return ret;
  345. }
  346. EXPORT_SYMBOL(il_send_cmd_sync);
  347. int
  348. il_send_cmd(struct il_priv *il, struct il_host_cmd *cmd)
  349. {
  350. if (cmd->flags & CMD_ASYNC)
  351. return il_send_cmd_async(il, cmd);
  352. return il_send_cmd_sync(il, cmd);
  353. }
  354. EXPORT_SYMBOL(il_send_cmd);
  355. int
  356. il_send_cmd_pdu(struct il_priv *il, u8 id, u16 len, const void *data)
  357. {
  358. struct il_host_cmd cmd = {
  359. .id = id,
  360. .len = len,
  361. .data = data,
  362. };
  363. return il_send_cmd_sync(il, &cmd);
  364. }
  365. EXPORT_SYMBOL(il_send_cmd_pdu);
  366. int
  367. il_send_cmd_pdu_async(struct il_priv *il, u8 id, u16 len, const void *data,
  368. void (*callback) (struct il_priv *il,
  369. struct il_device_cmd *cmd,
  370. struct il_rx_pkt *pkt))
  371. {
  372. struct il_host_cmd cmd = {
  373. .id = id,
  374. .len = len,
  375. .data = data,
  376. };
  377. cmd.flags |= CMD_ASYNC;
  378. cmd.callback = callback;
  379. return il_send_cmd_async(il, &cmd);
  380. }
  381. EXPORT_SYMBOL(il_send_cmd_pdu_async);
  382. /* default: IL_LED_BLINK(0) using blinking idx table */
  383. static int led_mode;
  384. module_param(led_mode, int, S_IRUGO);
  385. MODULE_PARM_DESC(led_mode,
  386. "0=system default, " "1=On(RF On)/Off(RF Off), 2=blinking");
  387. /* Throughput OFF time(ms) ON time (ms)
  388. * >300 25 25
  389. * >200 to 300 40 40
  390. * >100 to 200 55 55
  391. * >70 to 100 65 65
  392. * >50 to 70 75 75
  393. * >20 to 50 85 85
  394. * >10 to 20 95 95
  395. * >5 to 10 110 110
  396. * >1 to 5 130 130
  397. * >0 to 1 167 167
  398. * <=0 SOLID ON
  399. */
  400. static const struct ieee80211_tpt_blink il_blink[] = {
  401. {.throughput = 0, .blink_time = 334},
  402. {.throughput = 1 * 1024 - 1, .blink_time = 260},
  403. {.throughput = 5 * 1024 - 1, .blink_time = 220},
  404. {.throughput = 10 * 1024 - 1, .blink_time = 190},
  405. {.throughput = 20 * 1024 - 1, .blink_time = 170},
  406. {.throughput = 50 * 1024 - 1, .blink_time = 150},
  407. {.throughput = 70 * 1024 - 1, .blink_time = 130},
  408. {.throughput = 100 * 1024 - 1, .blink_time = 110},
  409. {.throughput = 200 * 1024 - 1, .blink_time = 80},
  410. {.throughput = 300 * 1024 - 1, .blink_time = 50},
  411. };
  412. /*
  413. * Adjust led blink rate to compensate on a MAC Clock difference on every HW
  414. * Led blink rate analysis showed an average deviation of 0% on 3945,
  415. * 5% on 4965 HW.
  416. * Need to compensate on the led on/off time per HW according to the deviation
  417. * to achieve the desired led frequency
  418. * The calculation is: (100-averageDeviation)/100 * blinkTime
  419. * For code efficiency the calculation will be:
  420. * compensation = (100 - averageDeviation) * 64 / 100
  421. * NewBlinkTime = (compensation * BlinkTime) / 64
  422. */
  423. static inline u8
  424. il_blink_compensation(struct il_priv *il, u8 time, u16 compensation)
  425. {
  426. if (!compensation) {
  427. IL_ERR("undefined blink compensation: "
  428. "use pre-defined blinking time\n");
  429. return time;
  430. }
  431. return (u8) ((time * compensation) >> 6);
  432. }
  433. /* Set led pattern command */
  434. static int
  435. il_led_cmd(struct il_priv *il, unsigned long on, unsigned long off)
  436. {
  437. struct il_led_cmd led_cmd = {
  438. .id = IL_LED_LINK,
  439. .interval = IL_DEF_LED_INTRVL
  440. };
  441. int ret;
  442. if (!test_bit(S_READY, &il->status))
  443. return -EBUSY;
  444. if (il->blink_on == on && il->blink_off == off)
  445. return 0;
  446. if (off == 0) {
  447. /* led is SOLID_ON */
  448. on = IL_LED_SOLID;
  449. }
  450. D_LED("Led blink time compensation=%u\n",
  451. il->cfg->base_params->led_compensation);
  452. led_cmd.on =
  453. il_blink_compensation(il, on,
  454. il->cfg->base_params->led_compensation);
  455. led_cmd.off =
  456. il_blink_compensation(il, off,
  457. il->cfg->base_params->led_compensation);
  458. ret = il->cfg->ops->led->cmd(il, &led_cmd);
  459. if (!ret) {
  460. il->blink_on = on;
  461. il->blink_off = off;
  462. }
  463. return ret;
  464. }
  465. static void
  466. il_led_brightness_set(struct led_classdev *led_cdev,
  467. enum led_brightness brightness)
  468. {
  469. struct il_priv *il = container_of(led_cdev, struct il_priv, led);
  470. unsigned long on = 0;
  471. if (brightness > 0)
  472. on = IL_LED_SOLID;
  473. il_led_cmd(il, on, 0);
  474. }
  475. static int
  476. il_led_blink_set(struct led_classdev *led_cdev, unsigned long *delay_on,
  477. unsigned long *delay_off)
  478. {
  479. struct il_priv *il = container_of(led_cdev, struct il_priv, led);
  480. return il_led_cmd(il, *delay_on, *delay_off);
  481. }
  482. void
  483. il_leds_init(struct il_priv *il)
  484. {
  485. int mode = led_mode;
  486. int ret;
  487. if (mode == IL_LED_DEFAULT)
  488. mode = il->cfg->led_mode;
  489. il->led.name =
  490. kasprintf(GFP_KERNEL, "%s-led", wiphy_name(il->hw->wiphy));
  491. il->led.brightness_set = il_led_brightness_set;
  492. il->led.blink_set = il_led_blink_set;
  493. il->led.max_brightness = 1;
  494. switch (mode) {
  495. case IL_LED_DEFAULT:
  496. WARN_ON(1);
  497. break;
  498. case IL_LED_BLINK:
  499. il->led.default_trigger =
  500. ieee80211_create_tpt_led_trigger(il->hw,
  501. IEEE80211_TPT_LEDTRIG_FL_CONNECTED,
  502. il_blink,
  503. ARRAY_SIZE(il_blink));
  504. break;
  505. case IL_LED_RF_STATE:
  506. il->led.default_trigger = ieee80211_get_radio_led_name(il->hw);
  507. break;
  508. }
  509. ret = led_classdev_register(&il->pci_dev->dev, &il->led);
  510. if (ret) {
  511. kfree(il->led.name);
  512. return;
  513. }
  514. il->led_registered = true;
  515. }
  516. EXPORT_SYMBOL(il_leds_init);
  517. void
  518. il_leds_exit(struct il_priv *il)
  519. {
  520. if (!il->led_registered)
  521. return;
  522. led_classdev_unregister(&il->led);
  523. kfree(il->led.name);
  524. }
  525. EXPORT_SYMBOL(il_leds_exit);
  526. /************************** EEPROM BANDS ****************************
  527. *
  528. * The il_eeprom_band definitions below provide the mapping from the
  529. * EEPROM contents to the specific channel number supported for each
  530. * band.
  531. *
  532. * For example, il_priv->eeprom.band_3_channels[4] from the band_3
  533. * definition below maps to physical channel 42 in the 5.2GHz spectrum.
  534. * The specific geography and calibration information for that channel
  535. * is contained in the eeprom map itself.
  536. *
  537. * During init, we copy the eeprom information and channel map
  538. * information into il->channel_info_24/52 and il->channel_map_24/52
  539. *
  540. * channel_map_24/52 provides the idx in the channel_info array for a
  541. * given channel. We have to have two separate maps as there is channel
  542. * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
  543. * band_2
  544. *
  545. * A value of 0xff stored in the channel_map indicates that the channel
  546. * is not supported by the hardware at all.
  547. *
  548. * A value of 0xfe in the channel_map indicates that the channel is not
  549. * valid for Tx with the current hardware. This means that
  550. * while the system can tune and receive on a given channel, it may not
  551. * be able to associate or transmit any frames on that
  552. * channel. There is no corresponding channel information for that
  553. * entry.
  554. *
  555. *********************************************************************/
  556. /* 2.4 GHz */
  557. const u8 il_eeprom_band_1[14] = {
  558. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  559. };
  560. /* 5.2 GHz bands */
  561. static const u8 il_eeprom_band_2[] = { /* 4915-5080MHz */
  562. 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
  563. };
  564. static const u8 il_eeprom_band_3[] = { /* 5170-5320MHz */
  565. 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
  566. };
  567. static const u8 il_eeprom_band_4[] = { /* 5500-5700MHz */
  568. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
  569. };
  570. static const u8 il_eeprom_band_5[] = { /* 5725-5825MHz */
  571. 145, 149, 153, 157, 161, 165
  572. };
  573. static const u8 il_eeprom_band_6[] = { /* 2.4 ht40 channel */
  574. 1, 2, 3, 4, 5, 6, 7
  575. };
  576. static const u8 il_eeprom_band_7[] = { /* 5.2 ht40 channel */
  577. 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
  578. };
  579. /******************************************************************************
  580. *
  581. * EEPROM related functions
  582. *
  583. ******************************************************************************/
  584. static int
  585. il_eeprom_verify_signature(struct il_priv *il)
  586. {
  587. u32 gp = _il_rd(il, CSR_EEPROM_GP) & CSR_EEPROM_GP_VALID_MSK;
  588. int ret = 0;
  589. D_EEPROM("EEPROM signature=0x%08x\n", gp);
  590. switch (gp) {
  591. case CSR_EEPROM_GP_GOOD_SIG_EEP_LESS_THAN_4K:
  592. case CSR_EEPROM_GP_GOOD_SIG_EEP_MORE_THAN_4K:
  593. break;
  594. default:
  595. IL_ERR("bad EEPROM signature," "EEPROM_GP=0x%08x\n", gp);
  596. ret = -ENOENT;
  597. break;
  598. }
  599. return ret;
  600. }
  601. const u8 *
  602. il_eeprom_query_addr(const struct il_priv *il, size_t offset)
  603. {
  604. BUG_ON(offset >= il->cfg->base_params->eeprom_size);
  605. return &il->eeprom[offset];
  606. }
  607. EXPORT_SYMBOL(il_eeprom_query_addr);
  608. u16
  609. il_eeprom_query16(const struct il_priv *il, size_t offset)
  610. {
  611. if (!il->eeprom)
  612. return 0;
  613. return (u16) il->eeprom[offset] | ((u16) il->eeprom[offset + 1] << 8);
  614. }
  615. EXPORT_SYMBOL(il_eeprom_query16);
  616. /**
  617. * il_eeprom_init - read EEPROM contents
  618. *
  619. * Load the EEPROM contents from adapter into il->eeprom
  620. *
  621. * NOTE: This routine uses the non-debug IO access functions.
  622. */
  623. int
  624. il_eeprom_init(struct il_priv *il)
  625. {
  626. __le16 *e;
  627. u32 gp = _il_rd(il, CSR_EEPROM_GP);
  628. int sz;
  629. int ret;
  630. u16 addr;
  631. /* allocate eeprom */
  632. sz = il->cfg->base_params->eeprom_size;
  633. D_EEPROM("NVM size = %d\n", sz);
  634. il->eeprom = kzalloc(sz, GFP_KERNEL);
  635. if (!il->eeprom) {
  636. ret = -ENOMEM;
  637. goto alloc_err;
  638. }
  639. e = (__le16 *) il->eeprom;
  640. il->cfg->ops->lib->apm_ops.init(il);
  641. ret = il_eeprom_verify_signature(il);
  642. if (ret < 0) {
  643. IL_ERR("EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  644. ret = -ENOENT;
  645. goto err;
  646. }
  647. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  648. ret = il->cfg->ops->lib->eeprom_ops.acquire_semaphore(il);
  649. if (ret < 0) {
  650. IL_ERR("Failed to acquire EEPROM semaphore.\n");
  651. ret = -ENOENT;
  652. goto err;
  653. }
  654. /* eeprom is an array of 16bit values */
  655. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  656. u32 r;
  657. _il_wr(il, CSR_EEPROM_REG,
  658. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  659. ret =
  660. _il_poll_bit(il, CSR_EEPROM_REG,
  661. CSR_EEPROM_REG_READ_VALID_MSK,
  662. CSR_EEPROM_REG_READ_VALID_MSK,
  663. IL_EEPROM_ACCESS_TIMEOUT);
  664. if (ret < 0) {
  665. IL_ERR("Time out reading EEPROM[%d]\n", addr);
  666. goto done;
  667. }
  668. r = _il_rd(il, CSR_EEPROM_REG);
  669. e[addr / 2] = cpu_to_le16(r >> 16);
  670. }
  671. D_EEPROM("NVM Type: %s, version: 0x%x\n", "EEPROM",
  672. il_eeprom_query16(il, EEPROM_VERSION));
  673. ret = 0;
  674. done:
  675. il->cfg->ops->lib->eeprom_ops.release_semaphore(il);
  676. err:
  677. if (ret)
  678. il_eeprom_free(il);
  679. /* Reset chip to save power until we load uCode during "up". */
  680. il_apm_stop(il);
  681. alloc_err:
  682. return ret;
  683. }
  684. EXPORT_SYMBOL(il_eeprom_init);
  685. void
  686. il_eeprom_free(struct il_priv *il)
  687. {
  688. kfree(il->eeprom);
  689. il->eeprom = NULL;
  690. }
  691. EXPORT_SYMBOL(il_eeprom_free);
  692. static void
  693. il_init_band_reference(const struct il_priv *il, int eep_band,
  694. int *eeprom_ch_count,
  695. const struct il_eeprom_channel **eeprom_ch_info,
  696. const u8 **eeprom_ch_idx)
  697. {
  698. u32 offset =
  699. il->cfg->ops->lib->eeprom_ops.regulatory_bands[eep_band - 1];
  700. switch (eep_band) {
  701. case 1: /* 2.4GHz band */
  702. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_1);
  703. *eeprom_ch_info =
  704. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  705. offset);
  706. *eeprom_ch_idx = il_eeprom_band_1;
  707. break;
  708. case 2: /* 4.9GHz band */
  709. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_2);
  710. *eeprom_ch_info =
  711. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  712. offset);
  713. *eeprom_ch_idx = il_eeprom_band_2;
  714. break;
  715. case 3: /* 5.2GHz band */
  716. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_3);
  717. *eeprom_ch_info =
  718. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  719. offset);
  720. *eeprom_ch_idx = il_eeprom_band_3;
  721. break;
  722. case 4: /* 5.5GHz band */
  723. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_4);
  724. *eeprom_ch_info =
  725. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  726. offset);
  727. *eeprom_ch_idx = il_eeprom_band_4;
  728. break;
  729. case 5: /* 5.7GHz band */
  730. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_5);
  731. *eeprom_ch_info =
  732. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  733. offset);
  734. *eeprom_ch_idx = il_eeprom_band_5;
  735. break;
  736. case 6: /* 2.4GHz ht40 channels */
  737. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_6);
  738. *eeprom_ch_info =
  739. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  740. offset);
  741. *eeprom_ch_idx = il_eeprom_band_6;
  742. break;
  743. case 7: /* 5 GHz ht40 channels */
  744. *eeprom_ch_count = ARRAY_SIZE(il_eeprom_band_7);
  745. *eeprom_ch_info =
  746. (struct il_eeprom_channel *)il_eeprom_query_addr(il,
  747. offset);
  748. *eeprom_ch_idx = il_eeprom_band_7;
  749. break;
  750. default:
  751. BUG();
  752. }
  753. }
  754. #define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
  755. ? # x " " : "")
  756. /**
  757. * il_mod_ht40_chan_info - Copy ht40 channel info into driver's il.
  758. *
  759. * Does not set up a command, or touch hardware.
  760. */
  761. static int
  762. il_mod_ht40_chan_info(struct il_priv *il, enum ieee80211_band band, u16 channel,
  763. const struct il_eeprom_channel *eeprom_ch,
  764. u8 clear_ht40_extension_channel)
  765. {
  766. struct il_channel_info *ch_info;
  767. ch_info =
  768. (struct il_channel_info *)il_get_channel_info(il, band, channel);
  769. if (!il_is_channel_valid(ch_info))
  770. return -1;
  771. D_EEPROM("HT40 Ch. %d [%sGHz] %s%s%s%s%s(0x%02x %ddBm):"
  772. " Ad-Hoc %ssupported\n", ch_info->channel,
  773. il_is_channel_a_band(ch_info) ? "5.2" : "2.4",
  774. CHECK_AND_PRINT(IBSS), CHECK_AND_PRINT(ACTIVE),
  775. CHECK_AND_PRINT(RADAR), CHECK_AND_PRINT(WIDE),
  776. CHECK_AND_PRINT(DFS), eeprom_ch->flags,
  777. eeprom_ch->max_power_avg,
  778. ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS) &&
  779. !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ? "" : "not ");
  780. ch_info->ht40_eeprom = *eeprom_ch;
  781. ch_info->ht40_max_power_avg = eeprom_ch->max_power_avg;
  782. ch_info->ht40_flags = eeprom_ch->flags;
  783. if (eeprom_ch->flags & EEPROM_CHANNEL_VALID)
  784. ch_info->ht40_extension_channel &=
  785. ~clear_ht40_extension_channel;
  786. return 0;
  787. }
  788. #define CHECK_AND_PRINT_I(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
  789. ? # x " " : "")
  790. /**
  791. * il_init_channel_map - Set up driver's info for all possible channels
  792. */
  793. int
  794. il_init_channel_map(struct il_priv *il)
  795. {
  796. int eeprom_ch_count = 0;
  797. const u8 *eeprom_ch_idx = NULL;
  798. const struct il_eeprom_channel *eeprom_ch_info = NULL;
  799. int band, ch;
  800. struct il_channel_info *ch_info;
  801. if (il->channel_count) {
  802. D_EEPROM("Channel map already initialized.\n");
  803. return 0;
  804. }
  805. D_EEPROM("Initializing regulatory info from EEPROM\n");
  806. il->channel_count =
  807. ARRAY_SIZE(il_eeprom_band_1) + ARRAY_SIZE(il_eeprom_band_2) +
  808. ARRAY_SIZE(il_eeprom_band_3) + ARRAY_SIZE(il_eeprom_band_4) +
  809. ARRAY_SIZE(il_eeprom_band_5);
  810. D_EEPROM("Parsing data for %d channels.\n", il->channel_count);
  811. il->channel_info =
  812. kzalloc(sizeof(struct il_channel_info) * il->channel_count,
  813. GFP_KERNEL);
  814. if (!il->channel_info) {
  815. IL_ERR("Could not allocate channel_info\n");
  816. il->channel_count = 0;
  817. return -ENOMEM;
  818. }
  819. ch_info = il->channel_info;
  820. /* Loop through the 5 EEPROM bands adding them in order to the
  821. * channel map we maintain (that contains additional information than
  822. * what just in the EEPROM) */
  823. for (band = 1; band <= 5; band++) {
  824. il_init_band_reference(il, band, &eeprom_ch_count,
  825. &eeprom_ch_info, &eeprom_ch_idx);
  826. /* Loop through each band adding each of the channels */
  827. for (ch = 0; ch < eeprom_ch_count; ch++) {
  828. ch_info->channel = eeprom_ch_idx[ch];
  829. ch_info->band =
  830. (band ==
  831. 1) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  832. /* permanently store EEPROM's channel regulatory flags
  833. * and max power in channel info database. */
  834. ch_info->eeprom = eeprom_ch_info[ch];
  835. /* Copy the run-time flags so they are there even on
  836. * invalid channels */
  837. ch_info->flags = eeprom_ch_info[ch].flags;
  838. /* First write that ht40 is not enabled, and then enable
  839. * one by one */
  840. ch_info->ht40_extension_channel =
  841. IEEE80211_CHAN_NO_HT40;
  842. if (!(il_is_channel_valid(ch_info))) {
  843. D_EEPROM("Ch. %d Flags %x [%sGHz] - "
  844. "No traffic\n", ch_info->channel,
  845. ch_info->flags,
  846. il_is_channel_a_band(ch_info) ? "5.2" :
  847. "2.4");
  848. ch_info++;
  849. continue;
  850. }
  851. /* Initialize regulatory-based run-time data */
  852. ch_info->max_power_avg = ch_info->curr_txpow =
  853. eeprom_ch_info[ch].max_power_avg;
  854. ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
  855. ch_info->min_power = 0;
  856. D_EEPROM("Ch. %d [%sGHz] " "%s%s%s%s%s%s(0x%02x %ddBm):"
  857. " Ad-Hoc %ssupported\n", ch_info->channel,
  858. il_is_channel_a_band(ch_info) ? "5.2" : "2.4",
  859. CHECK_AND_PRINT_I(VALID),
  860. CHECK_AND_PRINT_I(IBSS),
  861. CHECK_AND_PRINT_I(ACTIVE),
  862. CHECK_AND_PRINT_I(RADAR),
  863. CHECK_AND_PRINT_I(WIDE),
  864. CHECK_AND_PRINT_I(DFS),
  865. eeprom_ch_info[ch].flags,
  866. eeprom_ch_info[ch].max_power_avg,
  867. ((eeprom_ch_info[ch].
  868. flags & EEPROM_CHANNEL_IBSS) &&
  869. !(eeprom_ch_info[ch].
  870. flags & EEPROM_CHANNEL_RADAR)) ? "" :
  871. "not ");
  872. ch_info++;
  873. }
  874. }
  875. /* Check if we do have HT40 channels */
  876. if (il->cfg->ops->lib->eeprom_ops.regulatory_bands[5] ==
  877. EEPROM_REGULATORY_BAND_NO_HT40 &&
  878. il->cfg->ops->lib->eeprom_ops.regulatory_bands[6] ==
  879. EEPROM_REGULATORY_BAND_NO_HT40)
  880. return 0;
  881. /* Two additional EEPROM bands for 2.4 and 5 GHz HT40 channels */
  882. for (band = 6; band <= 7; band++) {
  883. enum ieee80211_band ieeeband;
  884. il_init_band_reference(il, band, &eeprom_ch_count,
  885. &eeprom_ch_info, &eeprom_ch_idx);
  886. /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
  887. ieeeband =
  888. (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  889. /* Loop through each band adding each of the channels */
  890. for (ch = 0; ch < eeprom_ch_count; ch++) {
  891. /* Set up driver's info for lower half */
  892. il_mod_ht40_chan_info(il, ieeeband, eeprom_ch_idx[ch],
  893. &eeprom_ch_info[ch],
  894. IEEE80211_CHAN_NO_HT40PLUS);
  895. /* Set up driver's info for upper half */
  896. il_mod_ht40_chan_info(il, ieeeband,
  897. eeprom_ch_idx[ch] + 4,
  898. &eeprom_ch_info[ch],
  899. IEEE80211_CHAN_NO_HT40MINUS);
  900. }
  901. }
  902. return 0;
  903. }
  904. EXPORT_SYMBOL(il_init_channel_map);
  905. /*
  906. * il_free_channel_map - undo allocations in il_init_channel_map
  907. */
  908. void
  909. il_free_channel_map(struct il_priv *il)
  910. {
  911. kfree(il->channel_info);
  912. il->channel_count = 0;
  913. }
  914. EXPORT_SYMBOL(il_free_channel_map);
  915. /**
  916. * il_get_channel_info - Find driver's ilate channel info
  917. *
  918. * Based on band and channel number.
  919. */
  920. const struct il_channel_info *
  921. il_get_channel_info(const struct il_priv *il, enum ieee80211_band band,
  922. u16 channel)
  923. {
  924. int i;
  925. switch (band) {
  926. case IEEE80211_BAND_5GHZ:
  927. for (i = 14; i < il->channel_count; i++) {
  928. if (il->channel_info[i].channel == channel)
  929. return &il->channel_info[i];
  930. }
  931. break;
  932. case IEEE80211_BAND_2GHZ:
  933. if (channel >= 1 && channel <= 14)
  934. return &il->channel_info[channel - 1];
  935. break;
  936. default:
  937. BUG();
  938. }
  939. return NULL;
  940. }
  941. EXPORT_SYMBOL(il_get_channel_info);
  942. /*
  943. * Setting power level allows the card to go to sleep when not busy.
  944. *
  945. * We calculate a sleep command based on the required latency, which
  946. * we get from mac80211. In order to handle thermal throttling, we can
  947. * also use pre-defined power levels.
  948. */
  949. /*
  950. * This defines the old power levels. They are still used by default
  951. * (level 1) and for thermal throttle (levels 3 through 5)
  952. */
  953. struct il_power_vec_entry {
  954. struct il_powertable_cmd cmd;
  955. u8 no_dtim; /* number of skip dtim */
  956. };
  957. static void
  958. il_power_sleep_cam_cmd(struct il_priv *il, struct il_powertable_cmd *cmd)
  959. {
  960. memset(cmd, 0, sizeof(*cmd));
  961. if (il->power_data.pci_pm)
  962. cmd->flags |= IL_POWER_PCI_PM_MSK;
  963. D_POWER("Sleep command for CAM\n");
  964. }
  965. static int
  966. il_set_power(struct il_priv *il, struct il_powertable_cmd *cmd)
  967. {
  968. D_POWER("Sending power/sleep command\n");
  969. D_POWER("Flags value = 0x%08X\n", cmd->flags);
  970. D_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  971. D_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  972. D_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  973. le32_to_cpu(cmd->sleep_interval[0]),
  974. le32_to_cpu(cmd->sleep_interval[1]),
  975. le32_to_cpu(cmd->sleep_interval[2]),
  976. le32_to_cpu(cmd->sleep_interval[3]),
  977. le32_to_cpu(cmd->sleep_interval[4]));
  978. return il_send_cmd_pdu(il, C_POWER_TBL,
  979. sizeof(struct il_powertable_cmd), cmd);
  980. }
  981. int
  982. il_power_set_mode(struct il_priv *il, struct il_powertable_cmd *cmd, bool force)
  983. {
  984. int ret;
  985. bool update_chains;
  986. lockdep_assert_held(&il->mutex);
  987. /* Don't update the RX chain when chain noise calibration is running */
  988. update_chains = il->chain_noise_data.state == IL_CHAIN_NOISE_DONE ||
  989. il->chain_noise_data.state == IL_CHAIN_NOISE_ALIVE;
  990. if (!memcmp(&il->power_data.sleep_cmd, cmd, sizeof(*cmd)) && !force)
  991. return 0;
  992. if (!il_is_ready_rf(il))
  993. return -EIO;
  994. /* scan complete use sleep_power_next, need to be updated */
  995. memcpy(&il->power_data.sleep_cmd_next, cmd, sizeof(*cmd));
  996. if (test_bit(S_SCANNING, &il->status) && !force) {
  997. D_INFO("Defer power set mode while scanning\n");
  998. return 0;
  999. }
  1000. if (cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK)
  1001. set_bit(S_POWER_PMI, &il->status);
  1002. ret = il_set_power(il, cmd);
  1003. if (!ret) {
  1004. if (!(cmd->flags & IL_POWER_DRIVER_ALLOW_SLEEP_MSK))
  1005. clear_bit(S_POWER_PMI, &il->status);
  1006. if (il->cfg->ops->lib->update_chain_flags && update_chains)
  1007. il->cfg->ops->lib->update_chain_flags(il);
  1008. else if (il->cfg->ops->lib->update_chain_flags)
  1009. D_POWER("Cannot update the power, chain noise "
  1010. "calibration running: %d\n",
  1011. il->chain_noise_data.state);
  1012. memcpy(&il->power_data.sleep_cmd, cmd, sizeof(*cmd));
  1013. } else
  1014. IL_ERR("set power fail, ret = %d", ret);
  1015. return ret;
  1016. }
  1017. int
  1018. il_power_update_mode(struct il_priv *il, bool force)
  1019. {
  1020. struct il_powertable_cmd cmd;
  1021. il_power_sleep_cam_cmd(il, &cmd);
  1022. return il_power_set_mode(il, &cmd, force);
  1023. }
  1024. EXPORT_SYMBOL(il_power_update_mode);
  1025. /* initialize to default */
  1026. void
  1027. il_power_initialize(struct il_priv *il)
  1028. {
  1029. u16 lctl = il_pcie_link_ctl(il);
  1030. il->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  1031. il->power_data.debug_sleep_level_override = -1;
  1032. memset(&il->power_data.sleep_cmd, 0, sizeof(il->power_data.sleep_cmd));
  1033. }
  1034. EXPORT_SYMBOL(il_power_initialize);
  1035. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  1036. * sending probe req. This should be set long enough to hear probe responses
  1037. * from more than one AP. */
  1038. #define IL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  1039. #define IL_ACTIVE_DWELL_TIME_52 (20)
  1040. #define IL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  1041. #define IL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  1042. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  1043. * Must be set longer than active dwell time.
  1044. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  1045. #define IL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  1046. #define IL_PASSIVE_DWELL_TIME_52 (10)
  1047. #define IL_PASSIVE_DWELL_BASE (100)
  1048. #define IL_CHANNEL_TUNE_TIME 5
  1049. static int
  1050. il_send_scan_abort(struct il_priv *il)
  1051. {
  1052. int ret;
  1053. struct il_rx_pkt *pkt;
  1054. struct il_host_cmd cmd = {
  1055. .id = C_SCAN_ABORT,
  1056. .flags = CMD_WANT_SKB,
  1057. };
  1058. /* Exit instantly with error when device is not ready
  1059. * to receive scan abort command or it does not perform
  1060. * hardware scan currently */
  1061. if (!test_bit(S_READY, &il->status) ||
  1062. !test_bit(S_GEO_CONFIGURED, &il->status) ||
  1063. !test_bit(S_SCAN_HW, &il->status) ||
  1064. test_bit(S_FW_ERROR, &il->status) ||
  1065. test_bit(S_EXIT_PENDING, &il->status))
  1066. return -EIO;
  1067. ret = il_send_cmd_sync(il, &cmd);
  1068. if (ret)
  1069. return ret;
  1070. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1071. if (pkt->u.status != CAN_ABORT_STATUS) {
  1072. /* The scan abort will return 1 for success or
  1073. * 2 for "failure". A failure condition can be
  1074. * due to simply not being in an active scan which
  1075. * can occur if we send the scan abort before we
  1076. * the microcode has notified us that a scan is
  1077. * completed. */
  1078. D_SCAN("SCAN_ABORT ret %d.\n", pkt->u.status);
  1079. ret = -EIO;
  1080. }
  1081. il_free_pages(il, cmd.reply_page);
  1082. return ret;
  1083. }
  1084. static void
  1085. il_complete_scan(struct il_priv *il, bool aborted)
  1086. {
  1087. /* check if scan was requested from mac80211 */
  1088. if (il->scan_request) {
  1089. D_SCAN("Complete scan in mac80211\n");
  1090. ieee80211_scan_completed(il->hw, aborted);
  1091. }
  1092. il->scan_vif = NULL;
  1093. il->scan_request = NULL;
  1094. }
  1095. void
  1096. il_force_scan_end(struct il_priv *il)
  1097. {
  1098. lockdep_assert_held(&il->mutex);
  1099. if (!test_bit(S_SCANNING, &il->status)) {
  1100. D_SCAN("Forcing scan end while not scanning\n");
  1101. return;
  1102. }
  1103. D_SCAN("Forcing scan end\n");
  1104. clear_bit(S_SCANNING, &il->status);
  1105. clear_bit(S_SCAN_HW, &il->status);
  1106. clear_bit(S_SCAN_ABORTING, &il->status);
  1107. il_complete_scan(il, true);
  1108. }
  1109. static void
  1110. il_do_scan_abort(struct il_priv *il)
  1111. {
  1112. int ret;
  1113. lockdep_assert_held(&il->mutex);
  1114. if (!test_bit(S_SCANNING, &il->status)) {
  1115. D_SCAN("Not performing scan to abort\n");
  1116. return;
  1117. }
  1118. if (test_and_set_bit(S_SCAN_ABORTING, &il->status)) {
  1119. D_SCAN("Scan abort in progress\n");
  1120. return;
  1121. }
  1122. ret = il_send_scan_abort(il);
  1123. if (ret) {
  1124. D_SCAN("Send scan abort failed %d\n", ret);
  1125. il_force_scan_end(il);
  1126. } else
  1127. D_SCAN("Successfully send scan abort\n");
  1128. }
  1129. /**
  1130. * il_scan_cancel - Cancel any currently executing HW scan
  1131. */
  1132. int
  1133. il_scan_cancel(struct il_priv *il)
  1134. {
  1135. D_SCAN("Queuing abort scan\n");
  1136. queue_work(il->workqueue, &il->abort_scan);
  1137. return 0;
  1138. }
  1139. EXPORT_SYMBOL(il_scan_cancel);
  1140. /**
  1141. * il_scan_cancel_timeout - Cancel any currently executing HW scan
  1142. * @ms: amount of time to wait (in milliseconds) for scan to abort
  1143. *
  1144. */
  1145. int
  1146. il_scan_cancel_timeout(struct il_priv *il, unsigned long ms)
  1147. {
  1148. unsigned long timeout = jiffies + msecs_to_jiffies(ms);
  1149. lockdep_assert_held(&il->mutex);
  1150. D_SCAN("Scan cancel timeout\n");
  1151. il_do_scan_abort(il);
  1152. while (time_before_eq(jiffies, timeout)) {
  1153. if (!test_bit(S_SCAN_HW, &il->status))
  1154. break;
  1155. msleep(20);
  1156. }
  1157. return test_bit(S_SCAN_HW, &il->status);
  1158. }
  1159. EXPORT_SYMBOL(il_scan_cancel_timeout);
  1160. /* Service response to C_SCAN (0x80) */
  1161. static void
  1162. il_hdl_scan(struct il_priv *il, struct il_rx_buf *rxb)
  1163. {
  1164. #ifdef CONFIG_IWLEGACY_DEBUG
  1165. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1166. struct il_scanreq_notification *notif =
  1167. (struct il_scanreq_notification *)pkt->u.raw;
  1168. D_SCAN("Scan request status = 0x%x\n", notif->status);
  1169. #endif
  1170. }
  1171. /* Service N_SCAN_START (0x82) */
  1172. static void
  1173. il_hdl_scan_start(struct il_priv *il, struct il_rx_buf *rxb)
  1174. {
  1175. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1176. struct il_scanstart_notification *notif =
  1177. (struct il_scanstart_notification *)pkt->u.raw;
  1178. il->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  1179. D_SCAN("Scan start: " "%d [802.11%s] "
  1180. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n", notif->channel,
  1181. notif->band ? "bg" : "a", le32_to_cpu(notif->tsf_high),
  1182. le32_to_cpu(notif->tsf_low), notif->status, notif->beacon_timer);
  1183. }
  1184. /* Service N_SCAN_RESULTS (0x83) */
  1185. static void
  1186. il_hdl_scan_results(struct il_priv *il, struct il_rx_buf *rxb)
  1187. {
  1188. #ifdef CONFIG_IWLEGACY_DEBUG
  1189. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1190. struct il_scanresults_notification *notif =
  1191. (struct il_scanresults_notification *)pkt->u.raw;
  1192. D_SCAN("Scan ch.res: " "%d [802.11%s] " "(TSF: 0x%08X:%08X) - %d "
  1193. "elapsed=%lu usec\n", notif->channel, notif->band ? "bg" : "a",
  1194. le32_to_cpu(notif->tsf_high), le32_to_cpu(notif->tsf_low),
  1195. le32_to_cpu(notif->stats[0]),
  1196. le32_to_cpu(notif->tsf_low) - il->scan_start_tsf);
  1197. #endif
  1198. }
  1199. /* Service N_SCAN_COMPLETE (0x84) */
  1200. static void
  1201. il_hdl_scan_complete(struct il_priv *il, struct il_rx_buf *rxb)
  1202. {
  1203. #ifdef CONFIG_IWLEGACY_DEBUG
  1204. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1205. struct il_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  1206. #endif
  1207. D_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  1208. scan_notif->scanned_channels, scan_notif->tsf_low,
  1209. scan_notif->tsf_high, scan_notif->status);
  1210. /* The HW is no longer scanning */
  1211. clear_bit(S_SCAN_HW, &il->status);
  1212. D_SCAN("Scan on %sGHz took %dms\n",
  1213. (il->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  1214. jiffies_to_msecs(jiffies - il->scan_start));
  1215. queue_work(il->workqueue, &il->scan_completed);
  1216. }
  1217. void
  1218. il_setup_rx_scan_handlers(struct il_priv *il)
  1219. {
  1220. /* scan handlers */
  1221. il->handlers[C_SCAN] = il_hdl_scan;
  1222. il->handlers[N_SCAN_START] = il_hdl_scan_start;
  1223. il->handlers[N_SCAN_RESULTS] = il_hdl_scan_results;
  1224. il->handlers[N_SCAN_COMPLETE] = il_hdl_scan_complete;
  1225. }
  1226. EXPORT_SYMBOL(il_setup_rx_scan_handlers);
  1227. inline u16
  1228. il_get_active_dwell_time(struct il_priv *il, enum ieee80211_band band,
  1229. u8 n_probes)
  1230. {
  1231. if (band == IEEE80211_BAND_5GHZ)
  1232. return IL_ACTIVE_DWELL_TIME_52 +
  1233. IL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  1234. else
  1235. return IL_ACTIVE_DWELL_TIME_24 +
  1236. IL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  1237. }
  1238. EXPORT_SYMBOL(il_get_active_dwell_time);
  1239. u16
  1240. il_get_passive_dwell_time(struct il_priv *il, enum ieee80211_band band,
  1241. struct ieee80211_vif *vif)
  1242. {
  1243. struct il_rxon_context *ctx = &il->ctx;
  1244. u16 value;
  1245. u16 passive =
  1246. (band ==
  1247. IEEE80211_BAND_2GHZ) ? IL_PASSIVE_DWELL_BASE +
  1248. IL_PASSIVE_DWELL_TIME_24 : IL_PASSIVE_DWELL_BASE +
  1249. IL_PASSIVE_DWELL_TIME_52;
  1250. if (il_is_any_associated(il)) {
  1251. /*
  1252. * If we're associated, we clamp the maximum passive
  1253. * dwell time to be 98% of the smallest beacon interval
  1254. * (minus 2 * channel tune time)
  1255. */
  1256. value = ctx->vif ? ctx->vif->bss_conf.beacon_int : 0;
  1257. if (value > IL_PASSIVE_DWELL_BASE || !value)
  1258. value = IL_PASSIVE_DWELL_BASE;
  1259. value = (value * 98) / 100 - IL_CHANNEL_TUNE_TIME * 2;
  1260. passive = min(value, passive);
  1261. }
  1262. return passive;
  1263. }
  1264. EXPORT_SYMBOL(il_get_passive_dwell_time);
  1265. void
  1266. il_init_scan_params(struct il_priv *il)
  1267. {
  1268. u8 ant_idx = fls(il->hw_params.valid_tx_ant) - 1;
  1269. if (!il->scan_tx_ant[IEEE80211_BAND_5GHZ])
  1270. il->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  1271. if (!il->scan_tx_ant[IEEE80211_BAND_2GHZ])
  1272. il->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  1273. }
  1274. EXPORT_SYMBOL(il_init_scan_params);
  1275. static int
  1276. il_scan_initiate(struct il_priv *il, struct ieee80211_vif *vif)
  1277. {
  1278. int ret;
  1279. lockdep_assert_held(&il->mutex);
  1280. if (WARN_ON(!il->cfg->ops->utils->request_scan))
  1281. return -EOPNOTSUPP;
  1282. cancel_delayed_work(&il->scan_check);
  1283. if (!il_is_ready_rf(il)) {
  1284. IL_WARN("Request scan called when driver not ready.\n");
  1285. return -EIO;
  1286. }
  1287. if (test_bit(S_SCAN_HW, &il->status)) {
  1288. D_SCAN("Multiple concurrent scan requests in parallel.\n");
  1289. return -EBUSY;
  1290. }
  1291. if (test_bit(S_SCAN_ABORTING, &il->status)) {
  1292. D_SCAN("Scan request while abort pending.\n");
  1293. return -EBUSY;
  1294. }
  1295. D_SCAN("Starting scan...\n");
  1296. set_bit(S_SCANNING, &il->status);
  1297. il->scan_start = jiffies;
  1298. ret = il->cfg->ops->utils->request_scan(il, vif);
  1299. if (ret) {
  1300. clear_bit(S_SCANNING, &il->status);
  1301. return ret;
  1302. }
  1303. queue_delayed_work(il->workqueue, &il->scan_check,
  1304. IL_SCAN_CHECK_WATCHDOG);
  1305. return 0;
  1306. }
  1307. int
  1308. il_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1309. struct cfg80211_scan_request *req)
  1310. {
  1311. struct il_priv *il = hw->priv;
  1312. int ret;
  1313. D_MAC80211("enter\n");
  1314. if (req->n_channels == 0)
  1315. return -EINVAL;
  1316. mutex_lock(&il->mutex);
  1317. if (test_bit(S_SCANNING, &il->status)) {
  1318. D_SCAN("Scan already in progress.\n");
  1319. ret = -EAGAIN;
  1320. goto out_unlock;
  1321. }
  1322. /* mac80211 will only ask for one band at a time */
  1323. il->scan_request = req;
  1324. il->scan_vif = vif;
  1325. il->scan_band = req->channels[0]->band;
  1326. ret = il_scan_initiate(il, vif);
  1327. D_MAC80211("leave\n");
  1328. out_unlock:
  1329. mutex_unlock(&il->mutex);
  1330. return ret;
  1331. }
  1332. EXPORT_SYMBOL(il_mac_hw_scan);
  1333. static void
  1334. il_bg_scan_check(struct work_struct *data)
  1335. {
  1336. struct il_priv *il =
  1337. container_of(data, struct il_priv, scan_check.work);
  1338. D_SCAN("Scan check work\n");
  1339. /* Since we are here firmware does not finish scan and
  1340. * most likely is in bad shape, so we don't bother to
  1341. * send abort command, just force scan complete to mac80211 */
  1342. mutex_lock(&il->mutex);
  1343. il_force_scan_end(il);
  1344. mutex_unlock(&il->mutex);
  1345. }
  1346. /**
  1347. * il_fill_probe_req - fill in all required fields and IE for probe request
  1348. */
  1349. u16
  1350. il_fill_probe_req(struct il_priv *il, struct ieee80211_mgmt *frame,
  1351. const u8 *ta, const u8 *ies, int ie_len, int left)
  1352. {
  1353. int len = 0;
  1354. u8 *pos = NULL;
  1355. /* Make sure there is enough space for the probe request,
  1356. * two mandatory IEs and the data */
  1357. left -= 24;
  1358. if (left < 0)
  1359. return 0;
  1360. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1361. memcpy(frame->da, il_bcast_addr, ETH_ALEN);
  1362. memcpy(frame->sa, ta, ETH_ALEN);
  1363. memcpy(frame->bssid, il_bcast_addr, ETH_ALEN);
  1364. frame->seq_ctrl = 0;
  1365. len += 24;
  1366. /* ...next IE... */
  1367. pos = &frame->u.probe_req.variable[0];
  1368. /* fill in our indirect SSID IE */
  1369. left -= 2;
  1370. if (left < 0)
  1371. return 0;
  1372. *pos++ = WLAN_EID_SSID;
  1373. *pos++ = 0;
  1374. len += 2;
  1375. if (WARN_ON(left < ie_len))
  1376. return len;
  1377. if (ies && ie_len) {
  1378. memcpy(pos, ies, ie_len);
  1379. len += ie_len;
  1380. }
  1381. return (u16) len;
  1382. }
  1383. EXPORT_SYMBOL(il_fill_probe_req);
  1384. static void
  1385. il_bg_abort_scan(struct work_struct *work)
  1386. {
  1387. struct il_priv *il = container_of(work, struct il_priv, abort_scan);
  1388. D_SCAN("Abort scan work\n");
  1389. /* We keep scan_check work queued in case when firmware will not
  1390. * report back scan completed notification */
  1391. mutex_lock(&il->mutex);
  1392. il_scan_cancel_timeout(il, 200);
  1393. mutex_unlock(&il->mutex);
  1394. }
  1395. static void
  1396. il_bg_scan_completed(struct work_struct *work)
  1397. {
  1398. struct il_priv *il = container_of(work, struct il_priv, scan_completed);
  1399. bool aborted;
  1400. D_SCAN("Completed scan.\n");
  1401. cancel_delayed_work(&il->scan_check);
  1402. mutex_lock(&il->mutex);
  1403. aborted = test_and_clear_bit(S_SCAN_ABORTING, &il->status);
  1404. if (aborted)
  1405. D_SCAN("Aborted scan completed.\n");
  1406. if (!test_and_clear_bit(S_SCANNING, &il->status)) {
  1407. D_SCAN("Scan already completed.\n");
  1408. goto out_settings;
  1409. }
  1410. il_complete_scan(il, aborted);
  1411. out_settings:
  1412. /* Can we still talk to firmware ? */
  1413. if (!il_is_ready_rf(il))
  1414. goto out;
  1415. /*
  1416. * We do not commit power settings while scan is pending,
  1417. * do it now if the settings changed.
  1418. */
  1419. il_power_set_mode(il, &il->power_data.sleep_cmd_next, false);
  1420. il_set_tx_power(il, il->tx_power_next, false);
  1421. il->cfg->ops->utils->post_scan(il);
  1422. out:
  1423. mutex_unlock(&il->mutex);
  1424. }
  1425. void
  1426. il_setup_scan_deferred_work(struct il_priv *il)
  1427. {
  1428. INIT_WORK(&il->scan_completed, il_bg_scan_completed);
  1429. INIT_WORK(&il->abort_scan, il_bg_abort_scan);
  1430. INIT_DELAYED_WORK(&il->scan_check, il_bg_scan_check);
  1431. }
  1432. EXPORT_SYMBOL(il_setup_scan_deferred_work);
  1433. void
  1434. il_cancel_scan_deferred_work(struct il_priv *il)
  1435. {
  1436. cancel_work_sync(&il->abort_scan);
  1437. cancel_work_sync(&il->scan_completed);
  1438. if (cancel_delayed_work_sync(&il->scan_check)) {
  1439. mutex_lock(&il->mutex);
  1440. il_force_scan_end(il);
  1441. mutex_unlock(&il->mutex);
  1442. }
  1443. }
  1444. EXPORT_SYMBOL(il_cancel_scan_deferred_work);
  1445. /* il->sta_lock must be held */
  1446. static void
  1447. il_sta_ucode_activate(struct il_priv *il, u8 sta_id)
  1448. {
  1449. if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE))
  1450. IL_ERR("ACTIVATE a non DRIVER active station id %u addr %pM\n",
  1451. sta_id, il->stations[sta_id].sta.sta.addr);
  1452. if (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) {
  1453. D_ASSOC("STA id %u addr %pM already present"
  1454. " in uCode (according to driver)\n", sta_id,
  1455. il->stations[sta_id].sta.sta.addr);
  1456. } else {
  1457. il->stations[sta_id].used |= IL_STA_UCODE_ACTIVE;
  1458. D_ASSOC("Added STA id %u addr %pM to uCode\n", sta_id,
  1459. il->stations[sta_id].sta.sta.addr);
  1460. }
  1461. }
  1462. static int
  1463. il_process_add_sta_resp(struct il_priv *il, struct il_addsta_cmd *addsta,
  1464. struct il_rx_pkt *pkt, bool sync)
  1465. {
  1466. u8 sta_id = addsta->sta.sta_id;
  1467. unsigned long flags;
  1468. int ret = -EIO;
  1469. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  1470. IL_ERR("Bad return from C_ADD_STA (0x%08X)\n", pkt->hdr.flags);
  1471. return ret;
  1472. }
  1473. D_INFO("Processing response for adding station %u\n", sta_id);
  1474. spin_lock_irqsave(&il->sta_lock, flags);
  1475. switch (pkt->u.add_sta.status) {
  1476. case ADD_STA_SUCCESS_MSK:
  1477. D_INFO("C_ADD_STA PASSED\n");
  1478. il_sta_ucode_activate(il, sta_id);
  1479. ret = 0;
  1480. break;
  1481. case ADD_STA_NO_ROOM_IN_TBL:
  1482. IL_ERR("Adding station %d failed, no room in table.\n", sta_id);
  1483. break;
  1484. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  1485. IL_ERR("Adding station %d failed, no block ack resource.\n",
  1486. sta_id);
  1487. break;
  1488. case ADD_STA_MODIFY_NON_EXIST_STA:
  1489. IL_ERR("Attempting to modify non-existing station %d\n",
  1490. sta_id);
  1491. break;
  1492. default:
  1493. D_ASSOC("Received C_ADD_STA:(0x%08X)\n", pkt->u.add_sta.status);
  1494. break;
  1495. }
  1496. D_INFO("%s station id %u addr %pM\n",
  1497. il->stations[sta_id].sta.mode ==
  1498. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added", sta_id,
  1499. il->stations[sta_id].sta.sta.addr);
  1500. /*
  1501. * XXX: The MAC address in the command buffer is often changed from
  1502. * the original sent to the device. That is, the MAC address
  1503. * written to the command buffer often is not the same MAC address
  1504. * read from the command buffer when the command returns. This
  1505. * issue has not yet been resolved and this debugging is left to
  1506. * observe the problem.
  1507. */
  1508. D_INFO("%s station according to cmd buffer %pM\n",
  1509. il->stations[sta_id].sta.mode ==
  1510. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added", addsta->sta.addr);
  1511. spin_unlock_irqrestore(&il->sta_lock, flags);
  1512. return ret;
  1513. }
  1514. static void
  1515. il_add_sta_callback(struct il_priv *il, struct il_device_cmd *cmd,
  1516. struct il_rx_pkt *pkt)
  1517. {
  1518. struct il_addsta_cmd *addsta = (struct il_addsta_cmd *)cmd->cmd.payload;
  1519. il_process_add_sta_resp(il, addsta, pkt, false);
  1520. }
  1521. int
  1522. il_send_add_sta(struct il_priv *il, struct il_addsta_cmd *sta, u8 flags)
  1523. {
  1524. struct il_rx_pkt *pkt = NULL;
  1525. int ret = 0;
  1526. u8 data[sizeof(*sta)];
  1527. struct il_host_cmd cmd = {
  1528. .id = C_ADD_STA,
  1529. .flags = flags,
  1530. .data = data,
  1531. };
  1532. u8 sta_id __maybe_unused = sta->sta.sta_id;
  1533. D_INFO("Adding sta %u (%pM) %ssynchronously\n", sta_id, sta->sta.addr,
  1534. flags & CMD_ASYNC ? "a" : "");
  1535. if (flags & CMD_ASYNC)
  1536. cmd.callback = il_add_sta_callback;
  1537. else {
  1538. cmd.flags |= CMD_WANT_SKB;
  1539. might_sleep();
  1540. }
  1541. cmd.len = il->cfg->ops->utils->build_addsta_hcmd(sta, data);
  1542. ret = il_send_cmd(il, &cmd);
  1543. if (ret || (flags & CMD_ASYNC))
  1544. return ret;
  1545. if (ret == 0) {
  1546. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1547. ret = il_process_add_sta_resp(il, sta, pkt, true);
  1548. }
  1549. il_free_pages(il, cmd.reply_page);
  1550. return ret;
  1551. }
  1552. EXPORT_SYMBOL(il_send_add_sta);
  1553. static void
  1554. il_set_ht_add_station(struct il_priv *il, u8 idx, struct ieee80211_sta *sta,
  1555. struct il_rxon_context *ctx)
  1556. {
  1557. struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
  1558. __le32 sta_flags;
  1559. u8 mimo_ps_mode;
  1560. if (!sta || !sta_ht_inf->ht_supported)
  1561. goto done;
  1562. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  1563. D_ASSOC("spatial multiplexing power save mode: %s\n",
  1564. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ? "static" :
  1565. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ? "dynamic" :
  1566. "disabled");
  1567. sta_flags = il->stations[idx].sta.station_flags;
  1568. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  1569. switch (mimo_ps_mode) {
  1570. case WLAN_HT_CAP_SM_PS_STATIC:
  1571. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  1572. break;
  1573. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  1574. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  1575. break;
  1576. case WLAN_HT_CAP_SM_PS_DISABLED:
  1577. break;
  1578. default:
  1579. IL_WARN("Invalid MIMO PS mode %d\n", mimo_ps_mode);
  1580. break;
  1581. }
  1582. sta_flags |=
  1583. cpu_to_le32((u32) sta_ht_inf->
  1584. ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  1585. sta_flags |=
  1586. cpu_to_le32((u32) sta_ht_inf->
  1587. ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  1588. if (il_is_ht40_tx_allowed(il, ctx, &sta->ht_cap))
  1589. sta_flags |= STA_FLG_HT40_EN_MSK;
  1590. else
  1591. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  1592. il->stations[idx].sta.station_flags = sta_flags;
  1593. done:
  1594. return;
  1595. }
  1596. /**
  1597. * il_prep_station - Prepare station information for addition
  1598. *
  1599. * should be called with sta_lock held
  1600. */
  1601. u8
  1602. il_prep_station(struct il_priv *il, struct il_rxon_context *ctx,
  1603. const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
  1604. {
  1605. struct il_station_entry *station;
  1606. int i;
  1607. u8 sta_id = IL_INVALID_STATION;
  1608. u16 rate;
  1609. if (is_ap)
  1610. sta_id = IL_AP_ID;
  1611. else if (is_broadcast_ether_addr(addr))
  1612. sta_id = il->hw_params.bcast_id;
  1613. else
  1614. for (i = IL_STA_ID; i < il->hw_params.max_stations; i++) {
  1615. if (!compare_ether_addr
  1616. (il->stations[i].sta.sta.addr, addr)) {
  1617. sta_id = i;
  1618. break;
  1619. }
  1620. if (!il->stations[i].used &&
  1621. sta_id == IL_INVALID_STATION)
  1622. sta_id = i;
  1623. }
  1624. /*
  1625. * These two conditions have the same outcome, but keep them
  1626. * separate
  1627. */
  1628. if (unlikely(sta_id == IL_INVALID_STATION))
  1629. return sta_id;
  1630. /*
  1631. * uCode is not able to deal with multiple requests to add a
  1632. * station. Keep track if one is in progress so that we do not send
  1633. * another.
  1634. */
  1635. if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
  1636. D_INFO("STA %d already in process of being added.\n", sta_id);
  1637. return sta_id;
  1638. }
  1639. if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
  1640. (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) &&
  1641. !compare_ether_addr(il->stations[sta_id].sta.sta.addr, addr)) {
  1642. D_ASSOC("STA %d (%pM) already added, not adding again.\n",
  1643. sta_id, addr);
  1644. return sta_id;
  1645. }
  1646. station = &il->stations[sta_id];
  1647. station->used = IL_STA_DRIVER_ACTIVE;
  1648. D_ASSOC("Add STA to driver ID %d: %pM\n", sta_id, addr);
  1649. il->num_stations++;
  1650. /* Set up the C_ADD_STA command to send to device */
  1651. memset(&station->sta, 0, sizeof(struct il_addsta_cmd));
  1652. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  1653. station->sta.mode = 0;
  1654. station->sta.sta.sta_id = sta_id;
  1655. station->sta.station_flags = 0;
  1656. if (sta) {
  1657. struct il_station_priv_common *sta_priv;
  1658. sta_priv = (void *)sta->drv_priv;
  1659. sta_priv->ctx = ctx;
  1660. }
  1661. /*
  1662. * OK to call unconditionally, since local stations (IBSS BSSID
  1663. * STA and broadcast STA) pass in a NULL sta, and mac80211
  1664. * doesn't allow HT IBSS.
  1665. */
  1666. il_set_ht_add_station(il, sta_id, sta, ctx);
  1667. /* 3945 only */
  1668. rate = (il->band == IEEE80211_BAND_5GHZ) ? RATE_6M_PLCP : RATE_1M_PLCP;
  1669. /* Turn on both antennas for the station... */
  1670. station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
  1671. return sta_id;
  1672. }
  1673. EXPORT_SYMBOL_GPL(il_prep_station);
  1674. #define STA_WAIT_TIMEOUT (HZ/2)
  1675. /**
  1676. * il_add_station_common -
  1677. */
  1678. int
  1679. il_add_station_common(struct il_priv *il, struct il_rxon_context *ctx,
  1680. const u8 *addr, bool is_ap, struct ieee80211_sta *sta,
  1681. u8 *sta_id_r)
  1682. {
  1683. unsigned long flags_spin;
  1684. int ret = 0;
  1685. u8 sta_id;
  1686. struct il_addsta_cmd sta_cmd;
  1687. *sta_id_r = 0;
  1688. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1689. sta_id = il_prep_station(il, ctx, addr, is_ap, sta);
  1690. if (sta_id == IL_INVALID_STATION) {
  1691. IL_ERR("Unable to prepare station %pM for addition\n", addr);
  1692. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1693. return -EINVAL;
  1694. }
  1695. /*
  1696. * uCode is not able to deal with multiple requests to add a
  1697. * station. Keep track if one is in progress so that we do not send
  1698. * another.
  1699. */
  1700. if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
  1701. D_INFO("STA %d already in process of being added.\n", sta_id);
  1702. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1703. return -EEXIST;
  1704. }
  1705. if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
  1706. (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
  1707. D_ASSOC("STA %d (%pM) already added, not adding again.\n",
  1708. sta_id, addr);
  1709. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1710. return -EEXIST;
  1711. }
  1712. il->stations[sta_id].used |= IL_STA_UCODE_INPROGRESS;
  1713. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1714. sizeof(struct il_addsta_cmd));
  1715. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1716. /* Add station to device's station table */
  1717. ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1718. if (ret) {
  1719. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1720. IL_ERR("Adding station %pM failed.\n",
  1721. il->stations[sta_id].sta.sta.addr);
  1722. il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
  1723. il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  1724. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1725. }
  1726. *sta_id_r = sta_id;
  1727. return ret;
  1728. }
  1729. EXPORT_SYMBOL(il_add_station_common);
  1730. /**
  1731. * il_sta_ucode_deactivate - deactivate ucode status for a station
  1732. *
  1733. * il->sta_lock must be held
  1734. */
  1735. static void
  1736. il_sta_ucode_deactivate(struct il_priv *il, u8 sta_id)
  1737. {
  1738. /* Ucode must be active and driver must be non active */
  1739. if ((il->stations[sta_id].
  1740. used & (IL_STA_UCODE_ACTIVE | IL_STA_DRIVER_ACTIVE)) !=
  1741. IL_STA_UCODE_ACTIVE)
  1742. IL_ERR("removed non active STA %u\n", sta_id);
  1743. il->stations[sta_id].used &= ~IL_STA_UCODE_ACTIVE;
  1744. memset(&il->stations[sta_id], 0, sizeof(struct il_station_entry));
  1745. D_ASSOC("Removed STA %u\n", sta_id);
  1746. }
  1747. static int
  1748. il_send_remove_station(struct il_priv *il, const u8 * addr, int sta_id,
  1749. bool temporary)
  1750. {
  1751. struct il_rx_pkt *pkt;
  1752. int ret;
  1753. unsigned long flags_spin;
  1754. struct il_rem_sta_cmd rm_sta_cmd;
  1755. struct il_host_cmd cmd = {
  1756. .id = C_REM_STA,
  1757. .len = sizeof(struct il_rem_sta_cmd),
  1758. .flags = CMD_SYNC,
  1759. .data = &rm_sta_cmd,
  1760. };
  1761. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  1762. rm_sta_cmd.num_sta = 1;
  1763. memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
  1764. cmd.flags |= CMD_WANT_SKB;
  1765. ret = il_send_cmd(il, &cmd);
  1766. if (ret)
  1767. return ret;
  1768. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1769. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  1770. IL_ERR("Bad return from C_REM_STA (0x%08X)\n", pkt->hdr.flags);
  1771. ret = -EIO;
  1772. }
  1773. if (!ret) {
  1774. switch (pkt->u.rem_sta.status) {
  1775. case REM_STA_SUCCESS_MSK:
  1776. if (!temporary) {
  1777. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1778. il_sta_ucode_deactivate(il, sta_id);
  1779. spin_unlock_irqrestore(&il->sta_lock,
  1780. flags_spin);
  1781. }
  1782. D_ASSOC("C_REM_STA PASSED\n");
  1783. break;
  1784. default:
  1785. ret = -EIO;
  1786. IL_ERR("C_REM_STA failed\n");
  1787. break;
  1788. }
  1789. }
  1790. il_free_pages(il, cmd.reply_page);
  1791. return ret;
  1792. }
  1793. /**
  1794. * il_remove_station - Remove driver's knowledge of station.
  1795. */
  1796. int
  1797. il_remove_station(struct il_priv *il, const u8 sta_id, const u8 * addr)
  1798. {
  1799. unsigned long flags;
  1800. if (!il_is_ready(il)) {
  1801. D_INFO("Unable to remove station %pM, device not ready.\n",
  1802. addr);
  1803. /*
  1804. * It is typical for stations to be removed when we are
  1805. * going down. Return success since device will be down
  1806. * soon anyway
  1807. */
  1808. return 0;
  1809. }
  1810. D_ASSOC("Removing STA from driver:%d %pM\n", sta_id, addr);
  1811. if (WARN_ON(sta_id == IL_INVALID_STATION))
  1812. return -EINVAL;
  1813. spin_lock_irqsave(&il->sta_lock, flags);
  1814. if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE)) {
  1815. D_INFO("Removing %pM but non DRIVER active\n", addr);
  1816. goto out_err;
  1817. }
  1818. if (!(il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
  1819. D_INFO("Removing %pM but non UCODE active\n", addr);
  1820. goto out_err;
  1821. }
  1822. if (il->stations[sta_id].used & IL_STA_LOCAL) {
  1823. kfree(il->stations[sta_id].lq);
  1824. il->stations[sta_id].lq = NULL;
  1825. }
  1826. il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
  1827. il->num_stations--;
  1828. BUG_ON(il->num_stations < 0);
  1829. spin_unlock_irqrestore(&il->sta_lock, flags);
  1830. return il_send_remove_station(il, addr, sta_id, false);
  1831. out_err:
  1832. spin_unlock_irqrestore(&il->sta_lock, flags);
  1833. return -EINVAL;
  1834. }
  1835. EXPORT_SYMBOL_GPL(il_remove_station);
  1836. /**
  1837. * il_clear_ucode_stations - clear ucode station table bits
  1838. *
  1839. * This function clears all the bits in the driver indicating
  1840. * which stations are active in the ucode. Call when something
  1841. * other than explicit station management would cause this in
  1842. * the ucode, e.g. unassociated RXON.
  1843. */
  1844. void
  1845. il_clear_ucode_stations(struct il_priv *il, struct il_rxon_context *ctx)
  1846. {
  1847. int i;
  1848. unsigned long flags_spin;
  1849. bool cleared = false;
  1850. D_INFO("Clearing ucode stations in driver\n");
  1851. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1852. for (i = 0; i < il->hw_params.max_stations; i++) {
  1853. if (il->stations[i].used & IL_STA_UCODE_ACTIVE) {
  1854. D_INFO("Clearing ucode active for station %d\n", i);
  1855. il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
  1856. cleared = true;
  1857. }
  1858. }
  1859. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1860. if (!cleared)
  1861. D_INFO("No active stations found to be cleared\n");
  1862. }
  1863. EXPORT_SYMBOL(il_clear_ucode_stations);
  1864. /**
  1865. * il_restore_stations() - Restore driver known stations to device
  1866. *
  1867. * All stations considered active by driver, but not present in ucode, is
  1868. * restored.
  1869. *
  1870. * Function sleeps.
  1871. */
  1872. void
  1873. il_restore_stations(struct il_priv *il, struct il_rxon_context *ctx)
  1874. {
  1875. struct il_addsta_cmd sta_cmd;
  1876. struct il_link_quality_cmd lq;
  1877. unsigned long flags_spin;
  1878. int i;
  1879. bool found = false;
  1880. int ret;
  1881. bool send_lq;
  1882. if (!il_is_ready(il)) {
  1883. D_INFO("Not ready yet, not restoring any stations.\n");
  1884. return;
  1885. }
  1886. D_ASSOC("Restoring all known stations ... start.\n");
  1887. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1888. for (i = 0; i < il->hw_params.max_stations; i++) {
  1889. if ((il->stations[i].used & IL_STA_DRIVER_ACTIVE) &&
  1890. !(il->stations[i].used & IL_STA_UCODE_ACTIVE)) {
  1891. D_ASSOC("Restoring sta %pM\n",
  1892. il->stations[i].sta.sta.addr);
  1893. il->stations[i].sta.mode = 0;
  1894. il->stations[i].used |= IL_STA_UCODE_INPROGRESS;
  1895. found = true;
  1896. }
  1897. }
  1898. for (i = 0; i < il->hw_params.max_stations; i++) {
  1899. if ((il->stations[i].used & IL_STA_UCODE_INPROGRESS)) {
  1900. memcpy(&sta_cmd, &il->stations[i].sta,
  1901. sizeof(struct il_addsta_cmd));
  1902. send_lq = false;
  1903. if (il->stations[i].lq) {
  1904. memcpy(&lq, il->stations[i].lq,
  1905. sizeof(struct il_link_quality_cmd));
  1906. send_lq = true;
  1907. }
  1908. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1909. ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1910. if (ret) {
  1911. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1912. IL_ERR("Adding station %pM failed.\n",
  1913. il->stations[i].sta.sta.addr);
  1914. il->stations[i].used &= ~IL_STA_DRIVER_ACTIVE;
  1915. il->stations[i].used &=
  1916. ~IL_STA_UCODE_INPROGRESS;
  1917. spin_unlock_irqrestore(&il->sta_lock,
  1918. flags_spin);
  1919. }
  1920. /*
  1921. * Rate scaling has already been initialized, send
  1922. * current LQ command
  1923. */
  1924. if (send_lq)
  1925. il_send_lq_cmd(il, ctx, &lq, CMD_SYNC, true);
  1926. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1927. il->stations[i].used &= ~IL_STA_UCODE_INPROGRESS;
  1928. }
  1929. }
  1930. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1931. if (!found)
  1932. D_INFO("Restoring all known stations"
  1933. " .... no stations to be restored.\n");
  1934. else
  1935. D_INFO("Restoring all known stations" " .... complete.\n");
  1936. }
  1937. EXPORT_SYMBOL(il_restore_stations);
  1938. int
  1939. il_get_free_ucode_key_idx(struct il_priv *il)
  1940. {
  1941. int i;
  1942. for (i = 0; i < il->sta_key_max_num; i++)
  1943. if (!test_and_set_bit(i, &il->ucode_key_table))
  1944. return i;
  1945. return WEP_INVALID_OFFSET;
  1946. }
  1947. EXPORT_SYMBOL(il_get_free_ucode_key_idx);
  1948. void
  1949. il_dealloc_bcast_stations(struct il_priv *il)
  1950. {
  1951. unsigned long flags;
  1952. int i;
  1953. spin_lock_irqsave(&il->sta_lock, flags);
  1954. for (i = 0; i < il->hw_params.max_stations; i++) {
  1955. if (!(il->stations[i].used & IL_STA_BCAST))
  1956. continue;
  1957. il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
  1958. il->num_stations--;
  1959. BUG_ON(il->num_stations < 0);
  1960. kfree(il->stations[i].lq);
  1961. il->stations[i].lq = NULL;
  1962. }
  1963. spin_unlock_irqrestore(&il->sta_lock, flags);
  1964. }
  1965. EXPORT_SYMBOL_GPL(il_dealloc_bcast_stations);
  1966. #ifdef CONFIG_IWLEGACY_DEBUG
  1967. static void
  1968. il_dump_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq)
  1969. {
  1970. int i;
  1971. D_RATE("lq station id 0x%x\n", lq->sta_id);
  1972. D_RATE("lq ant 0x%X 0x%X\n", lq->general_params.single_stream_ant_msk,
  1973. lq->general_params.dual_stream_ant_msk);
  1974. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  1975. D_RATE("lq idx %d 0x%X\n", i, lq->rs_table[i].rate_n_flags);
  1976. }
  1977. #else
  1978. static inline void
  1979. il_dump_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq)
  1980. {
  1981. }
  1982. #endif
  1983. /**
  1984. * il_is_lq_table_valid() - Test one aspect of LQ cmd for validity
  1985. *
  1986. * It sometimes happens when a HT rate has been in use and we
  1987. * loose connectivity with AP then mac80211 will first tell us that the
  1988. * current channel is not HT anymore before removing the station. In such a
  1989. * scenario the RXON flags will be updated to indicate we are not
  1990. * communicating HT anymore, but the LQ command may still contain HT rates.
  1991. * Test for this to prevent driver from sending LQ command between the time
  1992. * RXON flags are updated and when LQ command is updated.
  1993. */
  1994. static bool
  1995. il_is_lq_table_valid(struct il_priv *il, struct il_rxon_context *ctx,
  1996. struct il_link_quality_cmd *lq)
  1997. {
  1998. int i;
  1999. if (il->ht.enabled)
  2000. return true;
  2001. D_INFO("Channel %u is not an HT channel\n", il->active.channel);
  2002. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  2003. if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
  2004. D_INFO("idx %d of LQ expects HT channel\n", i);
  2005. return false;
  2006. }
  2007. }
  2008. return true;
  2009. }
  2010. /**
  2011. * il_send_lq_cmd() - Send link quality command
  2012. * @init: This command is sent as part of station initialization right
  2013. * after station has been added.
  2014. *
  2015. * The link quality command is sent as the last step of station creation.
  2016. * This is the special case in which init is set and we call a callback in
  2017. * this case to clear the state indicating that station creation is in
  2018. * progress.
  2019. */
  2020. int
  2021. il_send_lq_cmd(struct il_priv *il, struct il_rxon_context *ctx,
  2022. struct il_link_quality_cmd *lq, u8 flags, bool init)
  2023. {
  2024. int ret = 0;
  2025. unsigned long flags_spin;
  2026. struct il_host_cmd cmd = {
  2027. .id = C_TX_LINK_QUALITY_CMD,
  2028. .len = sizeof(struct il_link_quality_cmd),
  2029. .flags = flags,
  2030. .data = lq,
  2031. };
  2032. if (WARN_ON(lq->sta_id == IL_INVALID_STATION))
  2033. return -EINVAL;
  2034. spin_lock_irqsave(&il->sta_lock, flags_spin);
  2035. if (!(il->stations[lq->sta_id].used & IL_STA_DRIVER_ACTIVE)) {
  2036. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2037. return -EINVAL;
  2038. }
  2039. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2040. il_dump_lq_cmd(il, lq);
  2041. BUG_ON(init && (cmd.flags & CMD_ASYNC));
  2042. if (il_is_lq_table_valid(il, ctx, lq))
  2043. ret = il_send_cmd(il, &cmd);
  2044. else
  2045. ret = -EINVAL;
  2046. if (cmd.flags & CMD_ASYNC)
  2047. return ret;
  2048. if (init) {
  2049. D_INFO("init LQ command complete,"
  2050. " clearing sta addition status for sta %d\n",
  2051. lq->sta_id);
  2052. spin_lock_irqsave(&il->sta_lock, flags_spin);
  2053. il->stations[lq->sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  2054. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2055. }
  2056. return ret;
  2057. }
  2058. EXPORT_SYMBOL(il_send_lq_cmd);
  2059. int
  2060. il_mac_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2061. struct ieee80211_sta *sta)
  2062. {
  2063. struct il_priv *il = hw->priv;
  2064. struct il_station_priv_common *sta_common = (void *)sta->drv_priv;
  2065. int ret;
  2066. D_INFO("received request to remove station %pM\n", sta->addr);
  2067. mutex_lock(&il->mutex);
  2068. D_INFO("proceeding to remove station %pM\n", sta->addr);
  2069. ret = il_remove_station(il, sta_common->sta_id, sta->addr);
  2070. if (ret)
  2071. IL_ERR("Error removing station %pM\n", sta->addr);
  2072. mutex_unlock(&il->mutex);
  2073. return ret;
  2074. }
  2075. EXPORT_SYMBOL(il_mac_sta_remove);
  2076. /************************** RX-FUNCTIONS ****************************/
  2077. /*
  2078. * Rx theory of operation
  2079. *
  2080. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  2081. * each of which point to Receive Buffers to be filled by the NIC. These get
  2082. * used not only for Rx frames, but for any command response or notification
  2083. * from the NIC. The driver and NIC manage the Rx buffers by means
  2084. * of idxes into the circular buffer.
  2085. *
  2086. * Rx Queue Indexes
  2087. * The host/firmware share two idx registers for managing the Rx buffers.
  2088. *
  2089. * The READ idx maps to the first position that the firmware may be writing
  2090. * to -- the driver can read up to (but not including) this position and get
  2091. * good data.
  2092. * The READ idx is managed by the firmware once the card is enabled.
  2093. *
  2094. * The WRITE idx maps to the last position the driver has read from -- the
  2095. * position preceding WRITE is the last slot the firmware can place a packet.
  2096. *
  2097. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  2098. * WRITE = READ.
  2099. *
  2100. * During initialization, the host sets up the READ queue position to the first
  2101. * IDX position, and WRITE to the last (READ - 1 wrapped)
  2102. *
  2103. * When the firmware places a packet in a buffer, it will advance the READ idx
  2104. * and fire the RX interrupt. The driver can then query the READ idx and
  2105. * process as many packets as possible, moving the WRITE idx forward as it
  2106. * resets the Rx queue buffers with new memory.
  2107. *
  2108. * The management in the driver is as follows:
  2109. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  2110. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  2111. * to replenish the iwl->rxq->rx_free.
  2112. * + In il_rx_replenish (scheduled) if 'processed' != 'read' then the
  2113. * iwl->rxq is replenished and the READ IDX is updated (updating the
  2114. * 'processed' and 'read' driver idxes as well)
  2115. * + A received packet is processed and handed to the kernel network stack,
  2116. * detached from the iwl->rxq. The driver 'processed' idx is updated.
  2117. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  2118. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  2119. * IDX is not incremented and iwl->status(RX_STALLED) is set. If there
  2120. * were enough free buffers and RX_STALLED is set it is cleared.
  2121. *
  2122. *
  2123. * Driver sequence:
  2124. *
  2125. * il_rx_queue_alloc() Allocates rx_free
  2126. * il_rx_replenish() Replenishes rx_free list from rx_used, and calls
  2127. * il_rx_queue_restock
  2128. * il_rx_queue_restock() Moves available buffers from rx_free into Rx
  2129. * queue, updates firmware pointers, and updates
  2130. * the WRITE idx. If insufficient rx_free buffers
  2131. * are available, schedules il_rx_replenish
  2132. *
  2133. * -- enable interrupts --
  2134. * ISR - il_rx() Detach il_rx_bufs from pool up to the
  2135. * READ IDX, detaching the SKB from the pool.
  2136. * Moves the packet buffer from queue to rx_used.
  2137. * Calls il_rx_queue_restock to refill any empty
  2138. * slots.
  2139. * ...
  2140. *
  2141. */
  2142. /**
  2143. * il_rx_queue_space - Return number of free slots available in queue.
  2144. */
  2145. int
  2146. il_rx_queue_space(const struct il_rx_queue *q)
  2147. {
  2148. int s = q->read - q->write;
  2149. if (s <= 0)
  2150. s += RX_QUEUE_SIZE;
  2151. /* keep some buffer to not confuse full and empty queue */
  2152. s -= 2;
  2153. if (s < 0)
  2154. s = 0;
  2155. return s;
  2156. }
  2157. EXPORT_SYMBOL(il_rx_queue_space);
  2158. /**
  2159. * il_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  2160. */
  2161. void
  2162. il_rx_queue_update_write_ptr(struct il_priv *il, struct il_rx_queue *q)
  2163. {
  2164. unsigned long flags;
  2165. u32 rx_wrt_ptr_reg = il->hw_params.rx_wrt_ptr_reg;
  2166. u32 reg;
  2167. spin_lock_irqsave(&q->lock, flags);
  2168. if (q->need_update == 0)
  2169. goto exit_unlock;
  2170. /* If power-saving is in use, make sure device is awake */
  2171. if (test_bit(S_POWER_PMI, &il->status)) {
  2172. reg = _il_rd(il, CSR_UCODE_DRV_GP1);
  2173. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2174. D_INFO("Rx queue requesting wakeup," " GP1 = 0x%x\n",
  2175. reg);
  2176. il_set_bit(il, CSR_GP_CNTRL,
  2177. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2178. goto exit_unlock;
  2179. }
  2180. q->write_actual = (q->write & ~0x7);
  2181. il_wr(il, rx_wrt_ptr_reg, q->write_actual);
  2182. /* Else device is assumed to be awake */
  2183. } else {
  2184. /* Device expects a multiple of 8 */
  2185. q->write_actual = (q->write & ~0x7);
  2186. il_wr(il, rx_wrt_ptr_reg, q->write_actual);
  2187. }
  2188. q->need_update = 0;
  2189. exit_unlock:
  2190. spin_unlock_irqrestore(&q->lock, flags);
  2191. }
  2192. EXPORT_SYMBOL(il_rx_queue_update_write_ptr);
  2193. int
  2194. il_rx_queue_alloc(struct il_priv *il)
  2195. {
  2196. struct il_rx_queue *rxq = &il->rxq;
  2197. struct device *dev = &il->pci_dev->dev;
  2198. int i;
  2199. spin_lock_init(&rxq->lock);
  2200. INIT_LIST_HEAD(&rxq->rx_free);
  2201. INIT_LIST_HEAD(&rxq->rx_used);
  2202. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  2203. rxq->bd =
  2204. dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->bd_dma,
  2205. GFP_KERNEL);
  2206. if (!rxq->bd)
  2207. goto err_bd;
  2208. rxq->rb_stts =
  2209. dma_alloc_coherent(dev, sizeof(struct il_rb_status),
  2210. &rxq->rb_stts_dma, GFP_KERNEL);
  2211. if (!rxq->rb_stts)
  2212. goto err_rb;
  2213. /* Fill the rx_used queue with _all_ of the Rx buffers */
  2214. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  2215. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  2216. /* Set us so that we have processed and used all buffers, but have
  2217. * not restocked the Rx queue with fresh buffers */
  2218. rxq->read = rxq->write = 0;
  2219. rxq->write_actual = 0;
  2220. rxq->free_count = 0;
  2221. rxq->need_update = 0;
  2222. return 0;
  2223. err_rb:
  2224. dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  2225. rxq->bd_dma);
  2226. err_bd:
  2227. return -ENOMEM;
  2228. }
  2229. EXPORT_SYMBOL(il_rx_queue_alloc);
  2230. void
  2231. il_hdl_spectrum_measurement(struct il_priv *il, struct il_rx_buf *rxb)
  2232. {
  2233. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2234. struct il_spectrum_notification *report = &(pkt->u.spectrum_notif);
  2235. if (!report->state) {
  2236. D_11H("Spectrum Measure Notification: Start\n");
  2237. return;
  2238. }
  2239. memcpy(&il->measure_report, report, sizeof(*report));
  2240. il->measurement_status |= MEASUREMENT_READY;
  2241. }
  2242. EXPORT_SYMBOL(il_hdl_spectrum_measurement);
  2243. /*
  2244. * returns non-zero if packet should be dropped
  2245. */
  2246. int
  2247. il_set_decrypted_flag(struct il_priv *il, struct ieee80211_hdr *hdr,
  2248. u32 decrypt_res, struct ieee80211_rx_status *stats)
  2249. {
  2250. u16 fc = le16_to_cpu(hdr->frame_control);
  2251. /*
  2252. * All contexts have the same setting here due to it being
  2253. * a module parameter, so OK to check any context.
  2254. */
  2255. if (il->active.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  2256. return 0;
  2257. if (!(fc & IEEE80211_FCTL_PROTECTED))
  2258. return 0;
  2259. D_RX("decrypt_res:0x%x\n", decrypt_res);
  2260. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  2261. case RX_RES_STATUS_SEC_TYPE_TKIP:
  2262. /* The uCode has got a bad phase 1 Key, pushes the packet.
  2263. * Decryption will be done in SW. */
  2264. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2265. RX_RES_STATUS_BAD_KEY_TTAK)
  2266. break;
  2267. case RX_RES_STATUS_SEC_TYPE_WEP:
  2268. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2269. RX_RES_STATUS_BAD_ICV_MIC) {
  2270. /* bad ICV, the packet is destroyed since the
  2271. * decryption is inplace, drop it */
  2272. D_RX("Packet destroyed\n");
  2273. return -1;
  2274. }
  2275. case RX_RES_STATUS_SEC_TYPE_CCMP:
  2276. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2277. RX_RES_STATUS_DECRYPT_OK) {
  2278. D_RX("hw decrypt successfully!!!\n");
  2279. stats->flag |= RX_FLAG_DECRYPTED;
  2280. }
  2281. break;
  2282. default:
  2283. break;
  2284. }
  2285. return 0;
  2286. }
  2287. EXPORT_SYMBOL(il_set_decrypted_flag);
  2288. /**
  2289. * il_txq_update_write_ptr - Send new write idx to hardware
  2290. */
  2291. void
  2292. il_txq_update_write_ptr(struct il_priv *il, struct il_tx_queue *txq)
  2293. {
  2294. u32 reg = 0;
  2295. int txq_id = txq->q.id;
  2296. if (txq->need_update == 0)
  2297. return;
  2298. /* if we're trying to save power */
  2299. if (test_bit(S_POWER_PMI, &il->status)) {
  2300. /* wake up nic if it's powered down ...
  2301. * uCode will wake up, and interrupt us again, so next
  2302. * time we'll skip this part. */
  2303. reg = _il_rd(il, CSR_UCODE_DRV_GP1);
  2304. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2305. D_INFO("Tx queue %d requesting wakeup," " GP1 = 0x%x\n",
  2306. txq_id, reg);
  2307. il_set_bit(il, CSR_GP_CNTRL,
  2308. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2309. return;
  2310. }
  2311. il_wr(il, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
  2312. /*
  2313. * else not in power-save mode,
  2314. * uCode will never sleep when we're
  2315. * trying to tx (during RFKILL, we're not trying to tx).
  2316. */
  2317. } else
  2318. _il_wr(il, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
  2319. txq->need_update = 0;
  2320. }
  2321. EXPORT_SYMBOL(il_txq_update_write_ptr);
  2322. /**
  2323. * il_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  2324. */
  2325. void
  2326. il_tx_queue_unmap(struct il_priv *il, int txq_id)
  2327. {
  2328. struct il_tx_queue *txq = &il->txq[txq_id];
  2329. struct il_queue *q = &txq->q;
  2330. if (q->n_bd == 0)
  2331. return;
  2332. while (q->write_ptr != q->read_ptr) {
  2333. il->cfg->ops->lib->txq_free_tfd(il, txq);
  2334. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
  2335. }
  2336. }
  2337. EXPORT_SYMBOL(il_tx_queue_unmap);
  2338. /**
  2339. * il_tx_queue_free - Deallocate DMA queue.
  2340. * @txq: Transmit queue to deallocate.
  2341. *
  2342. * Empty queue by removing and destroying all BD's.
  2343. * Free all buffers.
  2344. * 0-fill, but do not free "txq" descriptor structure.
  2345. */
  2346. void
  2347. il_tx_queue_free(struct il_priv *il, int txq_id)
  2348. {
  2349. struct il_tx_queue *txq = &il->txq[txq_id];
  2350. struct device *dev = &il->pci_dev->dev;
  2351. int i;
  2352. il_tx_queue_unmap(il, txq_id);
  2353. /* De-alloc array of command/tx buffers */
  2354. for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
  2355. kfree(txq->cmd[i]);
  2356. /* De-alloc circular buffer of TFDs */
  2357. if (txq->q.n_bd)
  2358. dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
  2359. txq->tfds, txq->q.dma_addr);
  2360. /* De-alloc array of per-TFD driver data */
  2361. kfree(txq->txb);
  2362. txq->txb = NULL;
  2363. /* deallocate arrays */
  2364. kfree(txq->cmd);
  2365. kfree(txq->meta);
  2366. txq->cmd = NULL;
  2367. txq->meta = NULL;
  2368. /* 0-fill queue descriptor structure */
  2369. memset(txq, 0, sizeof(*txq));
  2370. }
  2371. EXPORT_SYMBOL(il_tx_queue_free);
  2372. /**
  2373. * il_cmd_queue_unmap - Unmap any remaining DMA mappings from command queue
  2374. */
  2375. void
  2376. il_cmd_queue_unmap(struct il_priv *il)
  2377. {
  2378. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2379. struct il_queue *q = &txq->q;
  2380. int i;
  2381. if (q->n_bd == 0)
  2382. return;
  2383. while (q->read_ptr != q->write_ptr) {
  2384. i = il_get_cmd_idx(q, q->read_ptr, 0);
  2385. if (txq->meta[i].flags & CMD_MAPPED) {
  2386. pci_unmap_single(il->pci_dev,
  2387. dma_unmap_addr(&txq->meta[i], mapping),
  2388. dma_unmap_len(&txq->meta[i], len),
  2389. PCI_DMA_BIDIRECTIONAL);
  2390. txq->meta[i].flags = 0;
  2391. }
  2392. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
  2393. }
  2394. i = q->n_win;
  2395. if (txq->meta[i].flags & CMD_MAPPED) {
  2396. pci_unmap_single(il->pci_dev,
  2397. dma_unmap_addr(&txq->meta[i], mapping),
  2398. dma_unmap_len(&txq->meta[i], len),
  2399. PCI_DMA_BIDIRECTIONAL);
  2400. txq->meta[i].flags = 0;
  2401. }
  2402. }
  2403. EXPORT_SYMBOL(il_cmd_queue_unmap);
  2404. /**
  2405. * il_cmd_queue_free - Deallocate DMA queue.
  2406. * @txq: Transmit queue to deallocate.
  2407. *
  2408. * Empty queue by removing and destroying all BD's.
  2409. * Free all buffers.
  2410. * 0-fill, but do not free "txq" descriptor structure.
  2411. */
  2412. void
  2413. il_cmd_queue_free(struct il_priv *il)
  2414. {
  2415. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2416. struct device *dev = &il->pci_dev->dev;
  2417. int i;
  2418. il_cmd_queue_unmap(il);
  2419. /* De-alloc array of command/tx buffers */
  2420. for (i = 0; i <= TFD_CMD_SLOTS; i++)
  2421. kfree(txq->cmd[i]);
  2422. /* De-alloc circular buffer of TFDs */
  2423. if (txq->q.n_bd)
  2424. dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
  2425. txq->tfds, txq->q.dma_addr);
  2426. /* deallocate arrays */
  2427. kfree(txq->cmd);
  2428. kfree(txq->meta);
  2429. txq->cmd = NULL;
  2430. txq->meta = NULL;
  2431. /* 0-fill queue descriptor structure */
  2432. memset(txq, 0, sizeof(*txq));
  2433. }
  2434. EXPORT_SYMBOL(il_cmd_queue_free);
  2435. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  2436. * DMA services
  2437. *
  2438. * Theory of operation
  2439. *
  2440. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  2441. * of buffer descriptors, each of which points to one or more data buffers for
  2442. * the device to read from or fill. Driver and device exchange status of each
  2443. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  2444. * entries in each circular buffer, to protect against confusing empty and full
  2445. * queue states.
  2446. *
  2447. * The device reads or writes the data in the queues via the device's several
  2448. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  2449. *
  2450. * For Tx queue, there are low mark and high mark limits. If, after queuing
  2451. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  2452. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  2453. * Tx queue resumed.
  2454. *
  2455. * See more detailed info in 4965.h.
  2456. ***************************************************/
  2457. int
  2458. il_queue_space(const struct il_queue *q)
  2459. {
  2460. int s = q->read_ptr - q->write_ptr;
  2461. if (q->read_ptr > q->write_ptr)
  2462. s -= q->n_bd;
  2463. if (s <= 0)
  2464. s += q->n_win;
  2465. /* keep some reserve to not confuse empty and full situations */
  2466. s -= 2;
  2467. if (s < 0)
  2468. s = 0;
  2469. return s;
  2470. }
  2471. EXPORT_SYMBOL(il_queue_space);
  2472. /**
  2473. * il_queue_init - Initialize queue's high/low-water and read/write idxes
  2474. */
  2475. static int
  2476. il_queue_init(struct il_priv *il, struct il_queue *q, int count, int slots_num,
  2477. u32 id)
  2478. {
  2479. q->n_bd = count;
  2480. q->n_win = slots_num;
  2481. q->id = id;
  2482. /* count must be power-of-two size, otherwise il_queue_inc_wrap
  2483. * and il_queue_dec_wrap are broken. */
  2484. BUG_ON(!is_power_of_2(count));
  2485. /* slots_num must be power-of-two size, otherwise
  2486. * il_get_cmd_idx is broken. */
  2487. BUG_ON(!is_power_of_2(slots_num));
  2488. q->low_mark = q->n_win / 4;
  2489. if (q->low_mark < 4)
  2490. q->low_mark = 4;
  2491. q->high_mark = q->n_win / 8;
  2492. if (q->high_mark < 2)
  2493. q->high_mark = 2;
  2494. q->write_ptr = q->read_ptr = 0;
  2495. return 0;
  2496. }
  2497. /**
  2498. * il_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  2499. */
  2500. static int
  2501. il_tx_queue_alloc(struct il_priv *il, struct il_tx_queue *txq, u32 id)
  2502. {
  2503. struct device *dev = &il->pci_dev->dev;
  2504. size_t tfd_sz = il->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  2505. /* Driver ilate data, only for Tx (not command) queues,
  2506. * not shared with device. */
  2507. if (id != il->cmd_queue) {
  2508. txq->txb = kcalloc(TFD_QUEUE_SIZE_MAX, sizeof(txq->txb[0]),
  2509. GFP_KERNEL);
  2510. if (!txq->txb) {
  2511. IL_ERR("kmalloc for auxiliary BD "
  2512. "structures failed\n");
  2513. goto error;
  2514. }
  2515. } else {
  2516. txq->txb = NULL;
  2517. }
  2518. /* Circular buffer of transmit frame descriptors (TFDs),
  2519. * shared with device */
  2520. txq->tfds =
  2521. dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr, GFP_KERNEL);
  2522. if (!txq->tfds) {
  2523. IL_ERR("pci_alloc_consistent(%zd) failed\n", tfd_sz);
  2524. goto error;
  2525. }
  2526. txq->q.id = id;
  2527. return 0;
  2528. error:
  2529. kfree(txq->txb);
  2530. txq->txb = NULL;
  2531. return -ENOMEM;
  2532. }
  2533. /**
  2534. * il_tx_queue_init - Allocate and initialize one tx/cmd queue
  2535. */
  2536. int
  2537. il_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq, int slots_num,
  2538. u32 txq_id)
  2539. {
  2540. int i, len;
  2541. int ret;
  2542. int actual_slots = slots_num;
  2543. /*
  2544. * Alloc buffer array for commands (Tx or other types of commands).
  2545. * For the command queue (#4/#9), allocate command space + one big
  2546. * command for scan, since scan command is very huge; the system will
  2547. * not have two scans at the same time, so only one is needed.
  2548. * For normal Tx queues (all other queues), no super-size command
  2549. * space is needed.
  2550. */
  2551. if (txq_id == il->cmd_queue)
  2552. actual_slots++;
  2553. txq->meta =
  2554. kzalloc(sizeof(struct il_cmd_meta) * actual_slots, GFP_KERNEL);
  2555. txq->cmd =
  2556. kzalloc(sizeof(struct il_device_cmd *) * actual_slots, GFP_KERNEL);
  2557. if (!txq->meta || !txq->cmd)
  2558. goto out_free_arrays;
  2559. len = sizeof(struct il_device_cmd);
  2560. for (i = 0; i < actual_slots; i++) {
  2561. /* only happens for cmd queue */
  2562. if (i == slots_num)
  2563. len = IL_MAX_CMD_SIZE;
  2564. txq->cmd[i] = kmalloc(len, GFP_KERNEL);
  2565. if (!txq->cmd[i])
  2566. goto err;
  2567. }
  2568. /* Alloc driver data array and TFD circular buffer */
  2569. ret = il_tx_queue_alloc(il, txq, txq_id);
  2570. if (ret)
  2571. goto err;
  2572. txq->need_update = 0;
  2573. /*
  2574. * For the default queues 0-3, set up the swq_id
  2575. * already -- all others need to get one later
  2576. * (if they need one at all).
  2577. */
  2578. if (txq_id < 4)
  2579. il_set_swq_id(txq, txq_id, txq_id);
  2580. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  2581. * il_queue_inc_wrap and il_queue_dec_wrap are broken. */
  2582. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  2583. /* Initialize queue's high/low-water marks, and head/tail idxes */
  2584. il_queue_init(il, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  2585. /* Tell device where to find queue */
  2586. il->cfg->ops->lib->txq_init(il, txq);
  2587. return 0;
  2588. err:
  2589. for (i = 0; i < actual_slots; i++)
  2590. kfree(txq->cmd[i]);
  2591. out_free_arrays:
  2592. kfree(txq->meta);
  2593. kfree(txq->cmd);
  2594. return -ENOMEM;
  2595. }
  2596. EXPORT_SYMBOL(il_tx_queue_init);
  2597. void
  2598. il_tx_queue_reset(struct il_priv *il, struct il_tx_queue *txq, int slots_num,
  2599. u32 txq_id)
  2600. {
  2601. int actual_slots = slots_num;
  2602. if (txq_id == il->cmd_queue)
  2603. actual_slots++;
  2604. memset(txq->meta, 0, sizeof(struct il_cmd_meta) * actual_slots);
  2605. txq->need_update = 0;
  2606. /* Initialize queue's high/low-water marks, and head/tail idxes */
  2607. il_queue_init(il, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  2608. /* Tell device where to find queue */
  2609. il->cfg->ops->lib->txq_init(il, txq);
  2610. }
  2611. EXPORT_SYMBOL(il_tx_queue_reset);
  2612. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  2613. /**
  2614. * il_enqueue_hcmd - enqueue a uCode command
  2615. * @il: device ilate data point
  2616. * @cmd: a point to the ucode command structure
  2617. *
  2618. * The function returns < 0 values to indicate the operation is
  2619. * failed. On success, it turns the idx (> 0) of command in the
  2620. * command queue.
  2621. */
  2622. int
  2623. il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
  2624. {
  2625. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2626. struct il_queue *q = &txq->q;
  2627. struct il_device_cmd *out_cmd;
  2628. struct il_cmd_meta *out_meta;
  2629. dma_addr_t phys_addr;
  2630. unsigned long flags;
  2631. int len;
  2632. u32 idx;
  2633. u16 fix_size;
  2634. cmd->len = il->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
  2635. fix_size = (u16) (cmd->len + sizeof(out_cmd->hdr));
  2636. /* If any of the command structures end up being larger than
  2637. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  2638. * we will need to increase the size of the TFD entries
  2639. * Also, check to see if command buffer should not exceed the size
  2640. * of device_cmd and max_cmd_size. */
  2641. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  2642. !(cmd->flags & CMD_SIZE_HUGE));
  2643. BUG_ON(fix_size > IL_MAX_CMD_SIZE);
  2644. if (il_is_rfkill(il) || il_is_ctkill(il)) {
  2645. IL_WARN("Not sending command - %s KILL\n",
  2646. il_is_rfkill(il) ? "RF" : "CT");
  2647. return -EIO;
  2648. }
  2649. spin_lock_irqsave(&il->hcmd_lock, flags);
  2650. if (il_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  2651. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2652. IL_ERR("Restarting adapter due to command queue full\n");
  2653. queue_work(il->workqueue, &il->restart);
  2654. return -ENOSPC;
  2655. }
  2656. idx = il_get_cmd_idx(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
  2657. out_cmd = txq->cmd[idx];
  2658. out_meta = &txq->meta[idx];
  2659. if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
  2660. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2661. return -ENOSPC;
  2662. }
  2663. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  2664. out_meta->flags = cmd->flags | CMD_MAPPED;
  2665. if (cmd->flags & CMD_WANT_SKB)
  2666. out_meta->source = cmd;
  2667. if (cmd->flags & CMD_ASYNC)
  2668. out_meta->callback = cmd->callback;
  2669. out_cmd->hdr.cmd = cmd->id;
  2670. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  2671. /* At this point, the out_cmd now has all of the incoming cmd
  2672. * information */
  2673. out_cmd->hdr.flags = 0;
  2674. out_cmd->hdr.sequence =
  2675. cpu_to_le16(QUEUE_TO_SEQ(il->cmd_queue) | IDX_TO_SEQ(q->write_ptr));
  2676. if (cmd->flags & CMD_SIZE_HUGE)
  2677. out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
  2678. len = sizeof(struct il_device_cmd);
  2679. if (idx == TFD_CMD_SLOTS)
  2680. len = IL_MAX_CMD_SIZE;
  2681. #ifdef CONFIG_IWLEGACY_DEBUG
  2682. switch (out_cmd->hdr.cmd) {
  2683. case C_TX_LINK_QUALITY_CMD:
  2684. case C_SENSITIVITY:
  2685. D_HC_DUMP("Sending command %s (#%x), seq: 0x%04X, "
  2686. "%d bytes at %d[%d]:%d\n",
  2687. il_get_cmd_string(out_cmd->hdr.cmd), out_cmd->hdr.cmd,
  2688. le16_to_cpu(out_cmd->hdr.sequence), fix_size,
  2689. q->write_ptr, idx, il->cmd_queue);
  2690. break;
  2691. default:
  2692. D_HC("Sending command %s (#%x), seq: 0x%04X, "
  2693. "%d bytes at %d[%d]:%d\n",
  2694. il_get_cmd_string(out_cmd->hdr.cmd), out_cmd->hdr.cmd,
  2695. le16_to_cpu(out_cmd->hdr.sequence), fix_size, q->write_ptr,
  2696. idx, il->cmd_queue);
  2697. }
  2698. #endif
  2699. txq->need_update = 1;
  2700. if (il->cfg->ops->lib->txq_update_byte_cnt_tbl)
  2701. /* Set up entry in queue's byte count circular buffer */
  2702. il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq, 0);
  2703. phys_addr =
  2704. pci_map_single(il->pci_dev, &out_cmd->hdr, fix_size,
  2705. PCI_DMA_BIDIRECTIONAL);
  2706. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  2707. dma_unmap_len_set(out_meta, len, fix_size);
  2708. il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq, phys_addr, fix_size,
  2709. 1, U32_PAD(cmd->len));
  2710. /* Increment and update queue's write idx */
  2711. q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
  2712. il_txq_update_write_ptr(il, txq);
  2713. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2714. return idx;
  2715. }
  2716. /**
  2717. * il_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  2718. *
  2719. * When FW advances 'R' idx, all entries between old and new 'R' idx
  2720. * need to be reclaimed. As result, some free space forms. If there is
  2721. * enough free space (> low mark), wake the stack that feeds us.
  2722. */
  2723. static void
  2724. il_hcmd_queue_reclaim(struct il_priv *il, int txq_id, int idx, int cmd_idx)
  2725. {
  2726. struct il_tx_queue *txq = &il->txq[txq_id];
  2727. struct il_queue *q = &txq->q;
  2728. int nfreed = 0;
  2729. if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
  2730. IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
  2731. "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
  2732. q->write_ptr, q->read_ptr);
  2733. return;
  2734. }
  2735. for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  2736. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2737. if (nfreed++ > 0) {
  2738. IL_ERR("HCMD skipped: idx (%d) %d %d\n", idx,
  2739. q->write_ptr, q->read_ptr);
  2740. queue_work(il->workqueue, &il->restart);
  2741. }
  2742. }
  2743. }
  2744. /**
  2745. * il_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  2746. * @rxb: Rx buffer to reclaim
  2747. *
  2748. * If an Rx buffer has an async callback associated with it the callback
  2749. * will be executed. The attached skb (if present) will only be freed
  2750. * if the callback returns 1
  2751. */
  2752. void
  2753. il_tx_cmd_complete(struct il_priv *il, struct il_rx_buf *rxb)
  2754. {
  2755. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2756. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2757. int txq_id = SEQ_TO_QUEUE(sequence);
  2758. int idx = SEQ_TO_IDX(sequence);
  2759. int cmd_idx;
  2760. bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
  2761. struct il_device_cmd *cmd;
  2762. struct il_cmd_meta *meta;
  2763. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2764. unsigned long flags;
  2765. /* If a Tx command is being handled and it isn't in the actual
  2766. * command queue then there a command routing bug has been introduced
  2767. * in the queue management code. */
  2768. if (WARN
  2769. (txq_id != il->cmd_queue,
  2770. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  2771. txq_id, il->cmd_queue, sequence, il->txq[il->cmd_queue].q.read_ptr,
  2772. il->txq[il->cmd_queue].q.write_ptr)) {
  2773. il_print_hex_error(il, pkt, 32);
  2774. return;
  2775. }
  2776. cmd_idx = il_get_cmd_idx(&txq->q, idx, huge);
  2777. cmd = txq->cmd[cmd_idx];
  2778. meta = &txq->meta[cmd_idx];
  2779. txq->time_stamp = jiffies;
  2780. pci_unmap_single(il->pci_dev, dma_unmap_addr(meta, mapping),
  2781. dma_unmap_len(meta, len), PCI_DMA_BIDIRECTIONAL);
  2782. /* Input error checking is done when commands are added to queue. */
  2783. if (meta->flags & CMD_WANT_SKB) {
  2784. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  2785. rxb->page = NULL;
  2786. } else if (meta->callback)
  2787. meta->callback(il, cmd, pkt);
  2788. spin_lock_irqsave(&il->hcmd_lock, flags);
  2789. il_hcmd_queue_reclaim(il, txq_id, idx, cmd_idx);
  2790. if (!(meta->flags & CMD_ASYNC)) {
  2791. clear_bit(S_HCMD_ACTIVE, &il->status);
  2792. D_INFO("Clearing HCMD_ACTIVE for command %s\n",
  2793. il_get_cmd_string(cmd->hdr.cmd));
  2794. wake_up(&il->wait_command_queue);
  2795. }
  2796. /* Mark as unmapped */
  2797. meta->flags = 0;
  2798. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2799. }
  2800. EXPORT_SYMBOL(il_tx_cmd_complete);
  2801. MODULE_DESCRIPTION("iwl-legacy: common functions for 3945 and 4965");
  2802. MODULE_VERSION(IWLWIFI_VERSION);
  2803. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  2804. MODULE_LICENSE("GPL");
  2805. /*
  2806. * set bt_coex_active to true, uCode will do kill/defer
  2807. * every time the priority line is asserted (BT is sending signals on the
  2808. * priority line in the PCIx).
  2809. * set bt_coex_active to false, uCode will ignore the BT activity and
  2810. * perform the normal operation
  2811. *
  2812. * User might experience transmit issue on some platform due to WiFi/BT
  2813. * co-exist problem. The possible behaviors are:
  2814. * Able to scan and finding all the available AP
  2815. * Not able to associate with any AP
  2816. * On those platforms, WiFi communication can be restored by set
  2817. * "bt_coex_active" module parameter to "false"
  2818. *
  2819. * default: bt_coex_active = true (BT_COEX_ENABLE)
  2820. */
  2821. static bool bt_coex_active = true;
  2822. module_param(bt_coex_active, bool, S_IRUGO);
  2823. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
  2824. u32 il_debug_level;
  2825. EXPORT_SYMBOL(il_debug_level);
  2826. const u8 il_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  2827. EXPORT_SYMBOL(il_bcast_addr);
  2828. /* This function both allocates and initializes hw and il. */
  2829. struct ieee80211_hw *
  2830. il_alloc_all(struct il_cfg *cfg)
  2831. {
  2832. struct il_priv *il;
  2833. /* mac80211 allocates memory for this device instance, including
  2834. * space for this driver's ilate structure */
  2835. struct ieee80211_hw *hw;
  2836. hw = ieee80211_alloc_hw(sizeof(struct il_priv),
  2837. cfg->ops->ieee80211_ops);
  2838. if (hw == NULL) {
  2839. pr_err("%s: Can not allocate network device\n", cfg->name);
  2840. goto out;
  2841. }
  2842. il = hw->priv;
  2843. il->hw = hw;
  2844. out:
  2845. return hw;
  2846. }
  2847. EXPORT_SYMBOL(il_alloc_all);
  2848. #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
  2849. #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
  2850. static void
  2851. il_init_ht_hw_capab(const struct il_priv *il,
  2852. struct ieee80211_sta_ht_cap *ht_info,
  2853. enum ieee80211_band band)
  2854. {
  2855. u16 max_bit_rate = 0;
  2856. u8 rx_chains_num = il->hw_params.rx_chains_num;
  2857. u8 tx_chains_num = il->hw_params.tx_chains_num;
  2858. ht_info->cap = 0;
  2859. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  2860. ht_info->ht_supported = true;
  2861. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  2862. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  2863. if (il->hw_params.ht40_channel & BIT(band)) {
  2864. ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  2865. ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
  2866. ht_info->mcs.rx_mask[4] = 0x01;
  2867. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  2868. }
  2869. if (il->cfg->mod_params->amsdu_size_8K)
  2870. ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  2871. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  2872. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  2873. ht_info->mcs.rx_mask[0] = 0xFF;
  2874. if (rx_chains_num >= 2)
  2875. ht_info->mcs.rx_mask[1] = 0xFF;
  2876. if (rx_chains_num >= 3)
  2877. ht_info->mcs.rx_mask[2] = 0xFF;
  2878. /* Highest supported Rx data rate */
  2879. max_bit_rate *= rx_chains_num;
  2880. WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
  2881. ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
  2882. /* Tx MCS capabilities */
  2883. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  2884. if (tx_chains_num != rx_chains_num) {
  2885. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  2886. ht_info->mcs.tx_params |=
  2887. ((tx_chains_num -
  2888. 1) << IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  2889. }
  2890. }
  2891. /**
  2892. * il_init_geos - Initialize mac80211's geo/channel info based from eeprom
  2893. */
  2894. int
  2895. il_init_geos(struct il_priv *il)
  2896. {
  2897. struct il_channel_info *ch;
  2898. struct ieee80211_supported_band *sband;
  2899. struct ieee80211_channel *channels;
  2900. struct ieee80211_channel *geo_ch;
  2901. struct ieee80211_rate *rates;
  2902. int i = 0;
  2903. s8 max_tx_power = 0;
  2904. if (il->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  2905. il->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  2906. D_INFO("Geography modes already initialized.\n");
  2907. set_bit(S_GEO_CONFIGURED, &il->status);
  2908. return 0;
  2909. }
  2910. channels =
  2911. kzalloc(sizeof(struct ieee80211_channel) * il->channel_count,
  2912. GFP_KERNEL);
  2913. if (!channels)
  2914. return -ENOMEM;
  2915. rates =
  2916. kzalloc((sizeof(struct ieee80211_rate) * RATE_COUNT_LEGACY),
  2917. GFP_KERNEL);
  2918. if (!rates) {
  2919. kfree(channels);
  2920. return -ENOMEM;
  2921. }
  2922. /* 5.2GHz channels start after the 2.4GHz channels */
  2923. sband = &il->bands[IEEE80211_BAND_5GHZ];
  2924. sband->channels = &channels[ARRAY_SIZE(il_eeprom_band_1)];
  2925. /* just OFDM */
  2926. sband->bitrates = &rates[IL_FIRST_OFDM_RATE];
  2927. sband->n_bitrates = RATE_COUNT_LEGACY - IL_FIRST_OFDM_RATE;
  2928. if (il->cfg->sku & IL_SKU_N)
  2929. il_init_ht_hw_capab(il, &sband->ht_cap, IEEE80211_BAND_5GHZ);
  2930. sband = &il->bands[IEEE80211_BAND_2GHZ];
  2931. sband->channels = channels;
  2932. /* OFDM & CCK */
  2933. sband->bitrates = rates;
  2934. sband->n_bitrates = RATE_COUNT_LEGACY;
  2935. if (il->cfg->sku & IL_SKU_N)
  2936. il_init_ht_hw_capab(il, &sband->ht_cap, IEEE80211_BAND_2GHZ);
  2937. il->ieee_channels = channels;
  2938. il->ieee_rates = rates;
  2939. for (i = 0; i < il->channel_count; i++) {
  2940. ch = &il->channel_info[i];
  2941. if (!il_is_channel_valid(ch))
  2942. continue;
  2943. sband = &il->bands[ch->band];
  2944. geo_ch = &sband->channels[sband->n_channels++];
  2945. geo_ch->center_freq =
  2946. ieee80211_channel_to_frequency(ch->channel, ch->band);
  2947. geo_ch->max_power = ch->max_power_avg;
  2948. geo_ch->max_antenna_gain = 0xff;
  2949. geo_ch->hw_value = ch->channel;
  2950. if (il_is_channel_valid(ch)) {
  2951. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  2952. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  2953. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  2954. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  2955. if (ch->flags & EEPROM_CHANNEL_RADAR)
  2956. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  2957. geo_ch->flags |= ch->ht40_extension_channel;
  2958. if (ch->max_power_avg > max_tx_power)
  2959. max_tx_power = ch->max_power_avg;
  2960. } else {
  2961. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  2962. }
  2963. D_INFO("Channel %d Freq=%d[%sGHz] %s flag=0x%X\n", ch->channel,
  2964. geo_ch->center_freq,
  2965. il_is_channel_a_band(ch) ? "5.2" : "2.4",
  2966. geo_ch->
  2967. flags & IEEE80211_CHAN_DISABLED ? "restricted" : "valid",
  2968. geo_ch->flags);
  2969. }
  2970. il->tx_power_device_lmt = max_tx_power;
  2971. il->tx_power_user_lmt = max_tx_power;
  2972. il->tx_power_next = max_tx_power;
  2973. if (il->bands[IEEE80211_BAND_5GHZ].n_channels == 0 &&
  2974. (il->cfg->sku & IL_SKU_A)) {
  2975. IL_INFO("Incorrectly detected BG card as ABG. "
  2976. "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
  2977. il->pci_dev->device, il->pci_dev->subsystem_device);
  2978. il->cfg->sku &= ~IL_SKU_A;
  2979. }
  2980. IL_INFO("Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  2981. il->bands[IEEE80211_BAND_2GHZ].n_channels,
  2982. il->bands[IEEE80211_BAND_5GHZ].n_channels);
  2983. set_bit(S_GEO_CONFIGURED, &il->status);
  2984. return 0;
  2985. }
  2986. EXPORT_SYMBOL(il_init_geos);
  2987. /*
  2988. * il_free_geos - undo allocations in il_init_geos
  2989. */
  2990. void
  2991. il_free_geos(struct il_priv *il)
  2992. {
  2993. kfree(il->ieee_channels);
  2994. kfree(il->ieee_rates);
  2995. clear_bit(S_GEO_CONFIGURED, &il->status);
  2996. }
  2997. EXPORT_SYMBOL(il_free_geos);
  2998. static bool
  2999. il_is_channel_extension(struct il_priv *il, enum ieee80211_band band,
  3000. u16 channel, u8 extension_chan_offset)
  3001. {
  3002. const struct il_channel_info *ch_info;
  3003. ch_info = il_get_channel_info(il, band, channel);
  3004. if (!il_is_channel_valid(ch_info))
  3005. return false;
  3006. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  3007. return !(ch_info->
  3008. ht40_extension_channel & IEEE80211_CHAN_NO_HT40PLUS);
  3009. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  3010. return !(ch_info->
  3011. ht40_extension_channel & IEEE80211_CHAN_NO_HT40MINUS);
  3012. return false;
  3013. }
  3014. bool
  3015. il_is_ht40_tx_allowed(struct il_priv *il, struct il_rxon_context *ctx,
  3016. struct ieee80211_sta_ht_cap *ht_cap)
  3017. {
  3018. if (!il->ht.enabled || !il->ht.is_40mhz)
  3019. return false;
  3020. /*
  3021. * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
  3022. * the bit will not set if it is pure 40MHz case
  3023. */
  3024. if (ht_cap && !ht_cap->ht_supported)
  3025. return false;
  3026. #ifdef CONFIG_IWLEGACY_DEBUGFS
  3027. if (il->disable_ht40)
  3028. return false;
  3029. #endif
  3030. return il_is_channel_extension(il, il->band,
  3031. le16_to_cpu(il->staging.channel),
  3032. il->ht.extension_chan_offset);
  3033. }
  3034. EXPORT_SYMBOL(il_is_ht40_tx_allowed);
  3035. static u16
  3036. il_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
  3037. {
  3038. u16 new_val;
  3039. u16 beacon_factor;
  3040. /*
  3041. * If mac80211 hasn't given us a beacon interval, program
  3042. * the default into the device.
  3043. */
  3044. if (!beacon_val)
  3045. return DEFAULT_BEACON_INTERVAL;
  3046. /*
  3047. * If the beacon interval we obtained from the peer
  3048. * is too large, we'll have to wake up more often
  3049. * (and in IBSS case, we'll beacon too much)
  3050. *
  3051. * For example, if max_beacon_val is 4096, and the
  3052. * requested beacon interval is 7000, we'll have to
  3053. * use 3500 to be able to wake up on the beacons.
  3054. *
  3055. * This could badly influence beacon detection stats.
  3056. */
  3057. beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
  3058. new_val = beacon_val / beacon_factor;
  3059. if (!new_val)
  3060. new_val = max_beacon_val;
  3061. return new_val;
  3062. }
  3063. int
  3064. il_send_rxon_timing(struct il_priv *il, struct il_rxon_context *ctx)
  3065. {
  3066. u64 tsf;
  3067. s32 interval_tm, rem;
  3068. struct ieee80211_conf *conf = NULL;
  3069. u16 beacon_int;
  3070. struct ieee80211_vif *vif = ctx->vif;
  3071. conf = &il->hw->conf;
  3072. lockdep_assert_held(&il->mutex);
  3073. memset(&il->timing, 0, sizeof(struct il_rxon_time_cmd));
  3074. il->timing.timestamp = cpu_to_le64(il->timestamp);
  3075. il->timing.listen_interval = cpu_to_le16(conf->listen_interval);
  3076. beacon_int = vif ? vif->bss_conf.beacon_int : 0;
  3077. /*
  3078. * TODO: For IBSS we need to get atim_win from mac80211,
  3079. * for now just always use 0
  3080. */
  3081. il->timing.atim_win = 0;
  3082. beacon_int =
  3083. il_adjust_beacon_interval(beacon_int,
  3084. il->hw_params.max_beacon_itrvl *
  3085. TIME_UNIT);
  3086. il->timing.beacon_interval = cpu_to_le16(beacon_int);
  3087. tsf = il->timestamp; /* tsf is modifed by do_div: copy it */
  3088. interval_tm = beacon_int * TIME_UNIT;
  3089. rem = do_div(tsf, interval_tm);
  3090. il->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
  3091. il->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ? : 1) : 1;
  3092. D_ASSOC("beacon interval %d beacon timer %d beacon tim %d\n",
  3093. le16_to_cpu(il->timing.beacon_interval),
  3094. le32_to_cpu(il->timing.beacon_init_val),
  3095. le16_to_cpu(il->timing.atim_win));
  3096. return il_send_cmd_pdu(il, C_RXON_TIMING, sizeof(il->timing),
  3097. &il->timing);
  3098. }
  3099. EXPORT_SYMBOL(il_send_rxon_timing);
  3100. void
  3101. il_set_rxon_hwcrypto(struct il_priv *il, struct il_rxon_context *ctx,
  3102. int hw_decrypt)
  3103. {
  3104. struct il_rxon_cmd *rxon = &il->staging;
  3105. if (hw_decrypt)
  3106. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  3107. else
  3108. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  3109. }
  3110. EXPORT_SYMBOL(il_set_rxon_hwcrypto);
  3111. /* validate RXON structure is valid */
  3112. int
  3113. il_check_rxon_cmd(struct il_priv *il, struct il_rxon_context *ctx)
  3114. {
  3115. struct il_rxon_cmd *rxon = &il->staging;
  3116. bool error = false;
  3117. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  3118. if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
  3119. IL_WARN("check 2.4G: wrong narrow\n");
  3120. error = true;
  3121. }
  3122. if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
  3123. IL_WARN("check 2.4G: wrong radar\n");
  3124. error = true;
  3125. }
  3126. } else {
  3127. if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
  3128. IL_WARN("check 5.2G: not short slot!\n");
  3129. error = true;
  3130. }
  3131. if (rxon->flags & RXON_FLG_CCK_MSK) {
  3132. IL_WARN("check 5.2G: CCK!\n");
  3133. error = true;
  3134. }
  3135. }
  3136. if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
  3137. IL_WARN("mac/bssid mcast!\n");
  3138. error = true;
  3139. }
  3140. /* make sure basic rates 6Mbps and 1Mbps are supported */
  3141. if ((rxon->ofdm_basic_rates & RATE_6M_MASK) == 0 &&
  3142. (rxon->cck_basic_rates & RATE_1M_MASK) == 0) {
  3143. IL_WARN("neither 1 nor 6 are basic\n");
  3144. error = true;
  3145. }
  3146. if (le16_to_cpu(rxon->assoc_id) > 2007) {
  3147. IL_WARN("aid > 2007\n");
  3148. error = true;
  3149. }
  3150. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) ==
  3151. (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
  3152. IL_WARN("CCK and short slot\n");
  3153. error = true;
  3154. }
  3155. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) ==
  3156. (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
  3157. IL_WARN("CCK and auto detect");
  3158. error = true;
  3159. }
  3160. if ((rxon->
  3161. flags & (RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK)) ==
  3162. RXON_FLG_TGG_PROTECT_MSK) {
  3163. IL_WARN("TGg but no auto-detect\n");
  3164. error = true;
  3165. }
  3166. if (error)
  3167. IL_WARN("Tuning to channel %d\n", le16_to_cpu(rxon->channel));
  3168. if (error) {
  3169. IL_ERR("Invalid RXON\n");
  3170. return -EINVAL;
  3171. }
  3172. return 0;
  3173. }
  3174. EXPORT_SYMBOL(il_check_rxon_cmd);
  3175. /**
  3176. * il_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  3177. * @il: staging_rxon is compared to active_rxon
  3178. *
  3179. * If the RXON structure is changing enough to require a new tune,
  3180. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  3181. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  3182. */
  3183. int
  3184. il_full_rxon_required(struct il_priv *il, struct il_rxon_context *ctx)
  3185. {
  3186. const struct il_rxon_cmd *staging = &il->staging;
  3187. const struct il_rxon_cmd *active = &il->active;
  3188. #define CHK(cond) \
  3189. if ((cond)) { \
  3190. D_INFO("need full RXON - " #cond "\n"); \
  3191. return 1; \
  3192. }
  3193. #define CHK_NEQ(c1, c2) \
  3194. if ((c1) != (c2)) { \
  3195. D_INFO("need full RXON - " \
  3196. #c1 " != " #c2 " - %d != %d\n", \
  3197. (c1), (c2)); \
  3198. return 1; \
  3199. }
  3200. /* These items are only settable from the full RXON command */
  3201. CHK(!il_is_associated(il));
  3202. CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
  3203. CHK(compare_ether_addr(staging->node_addr, active->node_addr));
  3204. CHK(compare_ether_addr
  3205. (staging->wlap_bssid_addr, active->wlap_bssid_addr));
  3206. CHK_NEQ(staging->dev_type, active->dev_type);
  3207. CHK_NEQ(staging->channel, active->channel);
  3208. CHK_NEQ(staging->air_propagation, active->air_propagation);
  3209. CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
  3210. active->ofdm_ht_single_stream_basic_rates);
  3211. CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
  3212. active->ofdm_ht_dual_stream_basic_rates);
  3213. CHK_NEQ(staging->assoc_id, active->assoc_id);
  3214. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  3215. * be updated with the RXON_ASSOC command -- however only some
  3216. * flag transitions are allowed using RXON_ASSOC */
  3217. /* Check if we are not switching bands */
  3218. CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
  3219. active->flags & RXON_FLG_BAND_24G_MSK);
  3220. /* Check if we are switching association toggle */
  3221. CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
  3222. active->filter_flags & RXON_FILTER_ASSOC_MSK);
  3223. #undef CHK
  3224. #undef CHK_NEQ
  3225. return 0;
  3226. }
  3227. EXPORT_SYMBOL(il_full_rxon_required);
  3228. u8
  3229. il_get_lowest_plcp(struct il_priv *il, struct il_rxon_context *ctx)
  3230. {
  3231. /*
  3232. * Assign the lowest rate -- should really get this from
  3233. * the beacon skb from mac80211.
  3234. */
  3235. if (il->staging.flags & RXON_FLG_BAND_24G_MSK)
  3236. return RATE_1M_PLCP;
  3237. else
  3238. return RATE_6M_PLCP;
  3239. }
  3240. EXPORT_SYMBOL(il_get_lowest_plcp);
  3241. static void
  3242. _il_set_rxon_ht(struct il_priv *il, struct il_ht_config *ht_conf,
  3243. struct il_rxon_context *ctx)
  3244. {
  3245. struct il_rxon_cmd *rxon = &il->staging;
  3246. if (!il->ht.enabled) {
  3247. rxon->flags &=
  3248. ~(RXON_FLG_CHANNEL_MODE_MSK |
  3249. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK | RXON_FLG_HT40_PROT_MSK
  3250. | RXON_FLG_HT_PROT_MSK);
  3251. return;
  3252. }
  3253. rxon->flags |=
  3254. cpu_to_le32(il->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
  3255. /* Set up channel bandwidth:
  3256. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  3257. /* clear the HT channel mode before set the mode */
  3258. rxon->flags &=
  3259. ~(RXON_FLG_CHANNEL_MODE_MSK | RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  3260. if (il_is_ht40_tx_allowed(il, ctx, NULL)) {
  3261. /* pure ht40 */
  3262. if (il->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  3263. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  3264. /* Note: control channel is opposite of extension channel */
  3265. switch (il->ht.extension_chan_offset) {
  3266. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  3267. rxon->flags &=
  3268. ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3269. break;
  3270. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  3271. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3272. break;
  3273. }
  3274. } else {
  3275. /* Note: control channel is opposite of extension channel */
  3276. switch (il->ht.extension_chan_offset) {
  3277. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  3278. rxon->flags &=
  3279. ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  3280. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  3281. break;
  3282. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  3283. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3284. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  3285. break;
  3286. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  3287. default:
  3288. /* channel location only valid if in Mixed mode */
  3289. IL_ERR("invalid extension channel offset\n");
  3290. break;
  3291. }
  3292. }
  3293. } else {
  3294. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  3295. }
  3296. if (il->cfg->ops->hcmd->set_rxon_chain)
  3297. il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
  3298. D_ASSOC("rxon flags 0x%X operation mode :0x%X "
  3299. "extension channel offset 0x%x\n", le32_to_cpu(rxon->flags),
  3300. il->ht.protection, il->ht.extension_chan_offset);
  3301. }
  3302. void
  3303. il_set_rxon_ht(struct il_priv *il, struct il_ht_config *ht_conf)
  3304. {
  3305. _il_set_rxon_ht(il, ht_conf, &il->ctx);
  3306. }
  3307. EXPORT_SYMBOL(il_set_rxon_ht);
  3308. /* Return valid, unused, channel for a passive scan to reset the RF */
  3309. u8
  3310. il_get_single_channel_number(struct il_priv *il, enum ieee80211_band band)
  3311. {
  3312. const struct il_channel_info *ch_info;
  3313. int i;
  3314. u8 channel = 0;
  3315. u8 min, max;
  3316. if (band == IEEE80211_BAND_5GHZ) {
  3317. min = 14;
  3318. max = il->channel_count;
  3319. } else {
  3320. min = 0;
  3321. max = 14;
  3322. }
  3323. for (i = min; i < max; i++) {
  3324. channel = il->channel_info[i].channel;
  3325. if (channel == le16_to_cpu(il->staging.channel))
  3326. continue;
  3327. ch_info = il_get_channel_info(il, band, channel);
  3328. if (il_is_channel_valid(ch_info))
  3329. break;
  3330. }
  3331. return channel;
  3332. }
  3333. EXPORT_SYMBOL(il_get_single_channel_number);
  3334. /**
  3335. * il_set_rxon_channel - Set the band and channel values in staging RXON
  3336. * @ch: requested channel as a pointer to struct ieee80211_channel
  3337. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  3338. * in the staging RXON flag structure based on the ch->band
  3339. */
  3340. int
  3341. il_set_rxon_channel(struct il_priv *il, struct ieee80211_channel *ch,
  3342. struct il_rxon_context *ctx)
  3343. {
  3344. enum ieee80211_band band = ch->band;
  3345. u16 channel = ch->hw_value;
  3346. if (le16_to_cpu(il->staging.channel) == channel && il->band == band)
  3347. return 0;
  3348. il->staging.channel = cpu_to_le16(channel);
  3349. if (band == IEEE80211_BAND_5GHZ)
  3350. il->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  3351. else
  3352. il->staging.flags |= RXON_FLG_BAND_24G_MSK;
  3353. il->band = band;
  3354. D_INFO("Staging channel set to %d [%d]\n", channel, band);
  3355. return 0;
  3356. }
  3357. EXPORT_SYMBOL(il_set_rxon_channel);
  3358. void
  3359. il_set_flags_for_band(struct il_priv *il, struct il_rxon_context *ctx,
  3360. enum ieee80211_band band, struct ieee80211_vif *vif)
  3361. {
  3362. if (band == IEEE80211_BAND_5GHZ) {
  3363. il->staging.flags &=
  3364. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK |
  3365. RXON_FLG_CCK_MSK);
  3366. il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  3367. } else {
  3368. /* Copied from il_post_associate() */
  3369. if (vif && vif->bss_conf.use_short_slot)
  3370. il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  3371. else
  3372. il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  3373. il->staging.flags |= RXON_FLG_BAND_24G_MSK;
  3374. il->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  3375. il->staging.flags &= ~RXON_FLG_CCK_MSK;
  3376. }
  3377. }
  3378. EXPORT_SYMBOL(il_set_flags_for_band);
  3379. /*
  3380. * initialize rxon structure with default values from eeprom
  3381. */
  3382. void
  3383. il_connection_init_rx_config(struct il_priv *il, struct il_rxon_context *ctx)
  3384. {
  3385. const struct il_channel_info *ch_info;
  3386. memset(&il->staging, 0, sizeof(il->staging));
  3387. if (!ctx->vif) {
  3388. il->staging.dev_type = RXON_DEV_TYPE_ESS;
  3389. } else
  3390. switch (ctx->vif->type) {
  3391. case NL80211_IFTYPE_STATION:
  3392. il->staging.dev_type = RXON_DEV_TYPE_ESS;
  3393. il->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  3394. break;
  3395. case NL80211_IFTYPE_ADHOC:
  3396. il->staging.dev_type = RXON_DEV_TYPE_IBSS;
  3397. il->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  3398. il->staging.filter_flags =
  3399. RXON_FILTER_BCON_AWARE_MSK |
  3400. RXON_FILTER_ACCEPT_GRP_MSK;
  3401. break;
  3402. default:
  3403. IL_ERR("Unsupported interface type %d\n",
  3404. ctx->vif->type);
  3405. break;
  3406. }
  3407. #if 0
  3408. /* TODO: Figure out when short_preamble would be set and cache from
  3409. * that */
  3410. if (!hw_to_local(il->hw)->short_preamble)
  3411. il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3412. else
  3413. il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  3414. #endif
  3415. ch_info =
  3416. il_get_channel_info(il, il->band, le16_to_cpu(il->active.channel));
  3417. if (!ch_info)
  3418. ch_info = &il->channel_info[0];
  3419. il->staging.channel = cpu_to_le16(ch_info->channel);
  3420. il->band = ch_info->band;
  3421. il_set_flags_for_band(il, ctx, il->band, ctx->vif);
  3422. il->staging.ofdm_basic_rates =
  3423. (IL_OFDM_RATES_MASK >> IL_FIRST_OFDM_RATE) & 0xFF;
  3424. il->staging.cck_basic_rates =
  3425. (IL_CCK_RATES_MASK >> IL_FIRST_CCK_RATE) & 0xF;
  3426. /* clear both MIX and PURE40 mode flag */
  3427. il->staging.flags &=
  3428. ~(RXON_FLG_CHANNEL_MODE_MIXED | RXON_FLG_CHANNEL_MODE_PURE_40);
  3429. if (ctx->vif)
  3430. memcpy(il->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  3431. il->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  3432. il->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  3433. }
  3434. EXPORT_SYMBOL(il_connection_init_rx_config);
  3435. void
  3436. il_set_rate(struct il_priv *il)
  3437. {
  3438. const struct ieee80211_supported_band *hw = NULL;
  3439. struct ieee80211_rate *rate;
  3440. int i;
  3441. hw = il_get_hw_mode(il, il->band);
  3442. if (!hw) {
  3443. IL_ERR("Failed to set rate: unable to get hw mode\n");
  3444. return;
  3445. }
  3446. il->active_rate = 0;
  3447. for (i = 0; i < hw->n_bitrates; i++) {
  3448. rate = &(hw->bitrates[i]);
  3449. if (rate->hw_value < RATE_COUNT_LEGACY)
  3450. il->active_rate |= (1 << rate->hw_value);
  3451. }
  3452. D_RATE("Set active_rate = %0x\n", il->active_rate);
  3453. il->staging.cck_basic_rates =
  3454. (IL_CCK_BASIC_RATES_MASK >> IL_FIRST_CCK_RATE) & 0xF;
  3455. il->staging.ofdm_basic_rates =
  3456. (IL_OFDM_BASIC_RATES_MASK >> IL_FIRST_OFDM_RATE) & 0xFF;
  3457. }
  3458. EXPORT_SYMBOL(il_set_rate);
  3459. void
  3460. il_chswitch_done(struct il_priv *il, bool is_success)
  3461. {
  3462. struct il_rxon_context *ctx = &il->ctx;
  3463. if (test_bit(S_EXIT_PENDING, &il->status))
  3464. return;
  3465. if (test_and_clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
  3466. ieee80211_chswitch_done(ctx->vif, is_success);
  3467. }
  3468. EXPORT_SYMBOL(il_chswitch_done);
  3469. void
  3470. il_hdl_csa(struct il_priv *il, struct il_rx_buf *rxb)
  3471. {
  3472. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3473. struct il_csa_notification *csa = &(pkt->u.csa_notif);
  3474. struct il_rxon_cmd *rxon = (void *)&il->active;
  3475. if (!test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
  3476. return;
  3477. if (!le32_to_cpu(csa->status) && csa->channel == il->switch_channel) {
  3478. rxon->channel = csa->channel;
  3479. il->staging.channel = csa->channel;
  3480. D_11H("CSA notif: channel %d\n", le16_to_cpu(csa->channel));
  3481. il_chswitch_done(il, true);
  3482. } else {
  3483. IL_ERR("CSA notif (fail) : channel %d\n",
  3484. le16_to_cpu(csa->channel));
  3485. il_chswitch_done(il, false);
  3486. }
  3487. }
  3488. EXPORT_SYMBOL(il_hdl_csa);
  3489. #ifdef CONFIG_IWLEGACY_DEBUG
  3490. void
  3491. il_print_rx_config_cmd(struct il_priv *il, struct il_rxon_context *ctx)
  3492. {
  3493. struct il_rxon_cmd *rxon = &il->staging;
  3494. D_RADIO("RX CONFIG:\n");
  3495. il_print_hex_dump(il, IL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  3496. D_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  3497. D_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  3498. D_RADIO("u32 filter_flags: 0x%08x\n", le32_to_cpu(rxon->filter_flags));
  3499. D_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
  3500. D_RADIO("u8 ofdm_basic_rates: 0x%02x\n", rxon->ofdm_basic_rates);
  3501. D_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  3502. D_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
  3503. D_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  3504. D_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  3505. }
  3506. EXPORT_SYMBOL(il_print_rx_config_cmd);
  3507. #endif
  3508. /**
  3509. * il_irq_handle_error - called for HW or SW error interrupt from card
  3510. */
  3511. void
  3512. il_irq_handle_error(struct il_priv *il)
  3513. {
  3514. /* Set the FW error flag -- cleared on il_down */
  3515. set_bit(S_FW_ERROR, &il->status);
  3516. /* Cancel currently queued command. */
  3517. clear_bit(S_HCMD_ACTIVE, &il->status);
  3518. IL_ERR("Loaded firmware version: %s\n", il->hw->wiphy->fw_version);
  3519. il->cfg->ops->lib->dump_nic_error_log(il);
  3520. if (il->cfg->ops->lib->dump_fh)
  3521. il->cfg->ops->lib->dump_fh(il, NULL, false);
  3522. #ifdef CONFIG_IWLEGACY_DEBUG
  3523. if (il_get_debug_level(il) & IL_DL_FW_ERRORS)
  3524. il_print_rx_config_cmd(il, &il->ctx);
  3525. #endif
  3526. wake_up(&il->wait_command_queue);
  3527. /* Keep the restart process from trying to send host
  3528. * commands by clearing the INIT status bit */
  3529. clear_bit(S_READY, &il->status);
  3530. if (!test_bit(S_EXIT_PENDING, &il->status)) {
  3531. IL_DBG(IL_DL_FW_ERRORS,
  3532. "Restarting adapter due to uCode error.\n");
  3533. if (il->cfg->mod_params->restart_fw)
  3534. queue_work(il->workqueue, &il->restart);
  3535. }
  3536. }
  3537. EXPORT_SYMBOL(il_irq_handle_error);
  3538. static int
  3539. il_apm_stop_master(struct il_priv *il)
  3540. {
  3541. int ret = 0;
  3542. /* stop device's busmaster DMA activity */
  3543. il_set_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  3544. ret =
  3545. _il_poll_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
  3546. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  3547. if (ret)
  3548. IL_WARN("Master Disable Timed Out, 100 usec\n");
  3549. D_INFO("stop master\n");
  3550. return ret;
  3551. }
  3552. void
  3553. il_apm_stop(struct il_priv *il)
  3554. {
  3555. D_INFO("Stop card, put in low power state\n");
  3556. /* Stop device's DMA activity */
  3557. il_apm_stop_master(il);
  3558. /* Reset the entire device */
  3559. il_set_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  3560. udelay(10);
  3561. /*
  3562. * Clear "initialization complete" bit to move adapter from
  3563. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  3564. */
  3565. il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  3566. }
  3567. EXPORT_SYMBOL(il_apm_stop);
  3568. /*
  3569. * Start up NIC's basic functionality after it has been reset
  3570. * (e.g. after platform boot, or shutdown via il_apm_stop())
  3571. * NOTE: This does not load uCode nor start the embedded processor
  3572. */
  3573. int
  3574. il_apm_init(struct il_priv *il)
  3575. {
  3576. int ret = 0;
  3577. u16 lctl;
  3578. D_INFO("Init card's basic functions\n");
  3579. /*
  3580. * Use "set_bit" below rather than "write", to preserve any hardware
  3581. * bits already set by default after reset.
  3582. */
  3583. /* Disable L0S exit timer (platform NMI Work/Around) */
  3584. il_set_bit(il, CSR_GIO_CHICKEN_BITS,
  3585. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  3586. /*
  3587. * Disable L0s without affecting L1;
  3588. * don't wait for ICH L0s (ICH bug W/A)
  3589. */
  3590. il_set_bit(il, CSR_GIO_CHICKEN_BITS,
  3591. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  3592. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  3593. il_set_bit(il, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  3594. /*
  3595. * Enable HAP INTA (interrupt from management bus) to
  3596. * wake device's PCI Express link L1a -> L0s
  3597. * NOTE: This is no-op for 3945 (non-existent bit)
  3598. */
  3599. il_set_bit(il, CSR_HW_IF_CONFIG_REG,
  3600. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  3601. /*
  3602. * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
  3603. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  3604. * If so (likely), disable L0S, so device moves directly L0->L1;
  3605. * costs negligible amount of power savings.
  3606. * If not (unlikely), enable L0S, so there is at least some
  3607. * power savings, even without L1.
  3608. */
  3609. if (il->cfg->base_params->set_l0s) {
  3610. lctl = il_pcie_link_ctl(il);
  3611. if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
  3612. PCI_CFG_LINK_CTRL_VAL_L1_EN) {
  3613. /* L1-ASPM enabled; disable(!) L0S */
  3614. il_set_bit(il, CSR_GIO_REG,
  3615. CSR_GIO_REG_VAL_L0S_ENABLED);
  3616. D_POWER("L1 Enabled; Disabling L0S\n");
  3617. } else {
  3618. /* L1-ASPM disabled; enable(!) L0S */
  3619. il_clear_bit(il, CSR_GIO_REG,
  3620. CSR_GIO_REG_VAL_L0S_ENABLED);
  3621. D_POWER("L1 Disabled; Enabling L0S\n");
  3622. }
  3623. }
  3624. /* Configure analog phase-lock-loop before activating to D0A */
  3625. if (il->cfg->base_params->pll_cfg_val)
  3626. il_set_bit(il, CSR_ANA_PLL_CFG,
  3627. il->cfg->base_params->pll_cfg_val);
  3628. /*
  3629. * Set "initialization complete" bit to move adapter from
  3630. * D0U* --> D0A* (powered-up active) state.
  3631. */
  3632. il_set_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  3633. /*
  3634. * Wait for clock stabilization; once stabilized, access to
  3635. * device-internal resources is supported, e.g. il_wr_prph()
  3636. * and accesses to uCode SRAM.
  3637. */
  3638. ret =
  3639. _il_poll_bit(il, CSR_GP_CNTRL,
  3640. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  3641. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  3642. if (ret < 0) {
  3643. D_INFO("Failed to init the card\n");
  3644. goto out;
  3645. }
  3646. /*
  3647. * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
  3648. * BSM (Boostrap State Machine) is only in 3945 and 4965.
  3649. *
  3650. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  3651. * do not disable clocks. This preserves any hardware bits already
  3652. * set by default in "CLK_CTRL_REG" after reset.
  3653. */
  3654. if (il->cfg->base_params->use_bsm)
  3655. il_wr_prph(il, APMG_CLK_EN_REG,
  3656. APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
  3657. else
  3658. il_wr_prph(il, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  3659. udelay(20);
  3660. /* Disable L1-Active */
  3661. il_set_bits_prph(il, APMG_PCIDEV_STT_REG,
  3662. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  3663. out:
  3664. return ret;
  3665. }
  3666. EXPORT_SYMBOL(il_apm_init);
  3667. int
  3668. il_set_tx_power(struct il_priv *il, s8 tx_power, bool force)
  3669. {
  3670. int ret;
  3671. s8 prev_tx_power;
  3672. bool defer;
  3673. lockdep_assert_held(&il->mutex);
  3674. if (il->tx_power_user_lmt == tx_power && !force)
  3675. return 0;
  3676. if (!il->cfg->ops->lib->send_tx_power)
  3677. return -EOPNOTSUPP;
  3678. /* 0 dBm mean 1 milliwatt */
  3679. if (tx_power < 0) {
  3680. IL_WARN("Requested user TXPOWER %d below 1 mW.\n", tx_power);
  3681. return -EINVAL;
  3682. }
  3683. if (tx_power > il->tx_power_device_lmt) {
  3684. IL_WARN("Requested user TXPOWER %d above upper limit %d.\n",
  3685. tx_power, il->tx_power_device_lmt);
  3686. return -EINVAL;
  3687. }
  3688. if (!il_is_ready_rf(il))
  3689. return -EIO;
  3690. /* scan complete and commit_rxon use tx_power_next value,
  3691. * it always need to be updated for newest request */
  3692. il->tx_power_next = tx_power;
  3693. /* do not set tx power when scanning or channel changing */
  3694. defer = test_bit(S_SCANNING, &il->status) ||
  3695. memcmp(&il->active, &il->staging, sizeof(il->staging));
  3696. if (defer && !force) {
  3697. D_INFO("Deferring tx power set\n");
  3698. return 0;
  3699. }
  3700. prev_tx_power = il->tx_power_user_lmt;
  3701. il->tx_power_user_lmt = tx_power;
  3702. ret = il->cfg->ops->lib->send_tx_power(il);
  3703. /* if fail to set tx_power, restore the orig. tx power */
  3704. if (ret) {
  3705. il->tx_power_user_lmt = prev_tx_power;
  3706. il->tx_power_next = prev_tx_power;
  3707. }
  3708. return ret;
  3709. }
  3710. EXPORT_SYMBOL(il_set_tx_power);
  3711. void
  3712. il_send_bt_config(struct il_priv *il)
  3713. {
  3714. struct il_bt_cmd bt_cmd = {
  3715. .lead_time = BT_LEAD_TIME_DEF,
  3716. .max_kill = BT_MAX_KILL_DEF,
  3717. .kill_ack_mask = 0,
  3718. .kill_cts_mask = 0,
  3719. };
  3720. if (!bt_coex_active)
  3721. bt_cmd.flags = BT_COEX_DISABLE;
  3722. else
  3723. bt_cmd.flags = BT_COEX_ENABLE;
  3724. D_INFO("BT coex %s\n",
  3725. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  3726. if (il_send_cmd_pdu(il, C_BT_CONFIG, sizeof(struct il_bt_cmd), &bt_cmd))
  3727. IL_ERR("failed to send BT Coex Config\n");
  3728. }
  3729. EXPORT_SYMBOL(il_send_bt_config);
  3730. int
  3731. il_send_stats_request(struct il_priv *il, u8 flags, bool clear)
  3732. {
  3733. struct il_stats_cmd stats_cmd = {
  3734. .configuration_flags = clear ? IL_STATS_CONF_CLEAR_STATS : 0,
  3735. };
  3736. if (flags & CMD_ASYNC)
  3737. return il_send_cmd_pdu_async(il, C_STATS, sizeof(struct il_stats_cmd),
  3738. &stats_cmd, NULL);
  3739. else
  3740. return il_send_cmd_pdu(il, C_STATS, sizeof(struct il_stats_cmd),
  3741. &stats_cmd);
  3742. }
  3743. EXPORT_SYMBOL(il_send_stats_request);
  3744. void
  3745. il_hdl_pm_sleep(struct il_priv *il, struct il_rx_buf *rxb)
  3746. {
  3747. #ifdef CONFIG_IWLEGACY_DEBUG
  3748. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3749. struct il_sleep_notification *sleep = &(pkt->u.sleep_notif);
  3750. D_RX("sleep mode: %d, src: %d\n",
  3751. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  3752. #endif
  3753. }
  3754. EXPORT_SYMBOL(il_hdl_pm_sleep);
  3755. void
  3756. il_hdl_pm_debug_stats(struct il_priv *il, struct il_rx_buf *rxb)
  3757. {
  3758. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3759. u32 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
  3760. D_RADIO("Dumping %d bytes of unhandled notification for %s:\n", len,
  3761. il_get_cmd_string(pkt->hdr.cmd));
  3762. il_print_hex_dump(il, IL_DL_RADIO, pkt->u.raw, len);
  3763. }
  3764. EXPORT_SYMBOL(il_hdl_pm_debug_stats);
  3765. void
  3766. il_hdl_error(struct il_priv *il, struct il_rx_buf *rxb)
  3767. {
  3768. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3769. IL_ERR("Error Reply type 0x%08X cmd %s (0x%02X) "
  3770. "seq 0x%04X ser 0x%08X\n",
  3771. le32_to_cpu(pkt->u.err_resp.error_type),
  3772. il_get_cmd_string(pkt->u.err_resp.cmd_id),
  3773. pkt->u.err_resp.cmd_id,
  3774. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  3775. le32_to_cpu(pkt->u.err_resp.error_info));
  3776. }
  3777. EXPORT_SYMBOL(il_hdl_error);
  3778. void
  3779. il_clear_isr_stats(struct il_priv *il)
  3780. {
  3781. memset(&il->isr_stats, 0, sizeof(il->isr_stats));
  3782. }
  3783. int
  3784. il_mac_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
  3785. const struct ieee80211_tx_queue_params *params)
  3786. {
  3787. struct il_priv *il = hw->priv;
  3788. unsigned long flags;
  3789. int q;
  3790. D_MAC80211("enter\n");
  3791. if (!il_is_ready_rf(il)) {
  3792. D_MAC80211("leave - RF not ready\n");
  3793. return -EIO;
  3794. }
  3795. if (queue >= AC_NUM) {
  3796. D_MAC80211("leave - queue >= AC_NUM %d\n", queue);
  3797. return 0;
  3798. }
  3799. q = AC_NUM - 1 - queue;
  3800. spin_lock_irqsave(&il->lock, flags);
  3801. il->qos_data.def_qos_parm.ac[q].cw_min =
  3802. cpu_to_le16(params->cw_min);
  3803. il->qos_data.def_qos_parm.ac[q].cw_max =
  3804. cpu_to_le16(params->cw_max);
  3805. il->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  3806. il->qos_data.def_qos_parm.ac[q].edca_txop =
  3807. cpu_to_le16((params->txop * 32));
  3808. il->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  3809. spin_unlock_irqrestore(&il->lock, flags);
  3810. D_MAC80211("leave\n");
  3811. return 0;
  3812. }
  3813. EXPORT_SYMBOL(il_mac_conf_tx);
  3814. int
  3815. il_mac_tx_last_beacon(struct ieee80211_hw *hw)
  3816. {
  3817. struct il_priv *il = hw->priv;
  3818. return il->ibss_manager == IL_IBSS_MANAGER;
  3819. }
  3820. EXPORT_SYMBOL_GPL(il_mac_tx_last_beacon);
  3821. static int
  3822. il_set_mode(struct il_priv *il, struct il_rxon_context *ctx)
  3823. {
  3824. il_connection_init_rx_config(il, ctx);
  3825. if (il->cfg->ops->hcmd->set_rxon_chain)
  3826. il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
  3827. return il_commit_rxon(il, ctx);
  3828. }
  3829. static int
  3830. il_setup_interface(struct il_priv *il, struct il_rxon_context *ctx)
  3831. {
  3832. struct ieee80211_vif *vif = ctx->vif;
  3833. lockdep_assert_held(&il->mutex);
  3834. /*
  3835. * This variable will be correct only when there's just
  3836. * a single context, but all code using it is for hardware
  3837. * that supports only one context.
  3838. */
  3839. il->iw_mode = vif->type;
  3840. return il_set_mode(il, ctx);
  3841. }
  3842. int
  3843. il_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  3844. {
  3845. struct il_priv *il = hw->priv;
  3846. struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
  3847. int err;
  3848. D_MAC80211("enter: type %d, addr %pM\n", vif->type, vif->addr);
  3849. mutex_lock(&il->mutex);
  3850. if (!il_is_ready_rf(il)) {
  3851. IL_WARN("Try to add interface when device not ready\n");
  3852. err = -EINVAL;
  3853. goto out;
  3854. }
  3855. if (il->ctx.vif) {
  3856. err = -EOPNOTSUPP;
  3857. goto out;
  3858. }
  3859. vif_priv->ctx = &il->ctx;
  3860. il->ctx.vif = vif;
  3861. err = il_setup_interface(il, &il->ctx);
  3862. if (err) {
  3863. il->ctx.vif = NULL;
  3864. il->iw_mode = NL80211_IFTYPE_STATION;
  3865. }
  3866. out:
  3867. mutex_unlock(&il->mutex);
  3868. D_MAC80211("leave\n");
  3869. return err;
  3870. }
  3871. EXPORT_SYMBOL(il_mac_add_interface);
  3872. static void
  3873. il_teardown_interface(struct il_priv *il, struct ieee80211_vif *vif,
  3874. bool mode_change)
  3875. {
  3876. struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
  3877. lockdep_assert_held(&il->mutex);
  3878. if (il->scan_vif == vif) {
  3879. il_scan_cancel_timeout(il, 200);
  3880. il_force_scan_end(il);
  3881. }
  3882. if (!mode_change)
  3883. il_set_mode(il, ctx);
  3884. }
  3885. void
  3886. il_mac_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  3887. {
  3888. struct il_priv *il = hw->priv;
  3889. struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
  3890. D_MAC80211("enter\n");
  3891. mutex_lock(&il->mutex);
  3892. WARN_ON(ctx->vif != vif);
  3893. ctx->vif = NULL;
  3894. il_teardown_interface(il, vif, false);
  3895. memset(il->bssid, 0, ETH_ALEN);
  3896. mutex_unlock(&il->mutex);
  3897. D_MAC80211("leave\n");
  3898. }
  3899. EXPORT_SYMBOL(il_mac_remove_interface);
  3900. int
  3901. il_alloc_txq_mem(struct il_priv *il)
  3902. {
  3903. if (!il->txq)
  3904. il->txq =
  3905. kzalloc(sizeof(struct il_tx_queue) *
  3906. il->cfg->base_params->num_of_queues, GFP_KERNEL);
  3907. if (!il->txq) {
  3908. IL_ERR("Not enough memory for txq\n");
  3909. return -ENOMEM;
  3910. }
  3911. return 0;
  3912. }
  3913. EXPORT_SYMBOL(il_alloc_txq_mem);
  3914. void
  3915. il_txq_mem(struct il_priv *il)
  3916. {
  3917. kfree(il->txq);
  3918. il->txq = NULL;
  3919. }
  3920. EXPORT_SYMBOL(il_txq_mem);
  3921. #ifdef CONFIG_IWLEGACY_DEBUGFS
  3922. #define IL_TRAFFIC_DUMP_SIZE (IL_TRAFFIC_ENTRY_SIZE * IL_TRAFFIC_ENTRIES)
  3923. void
  3924. il_reset_traffic_log(struct il_priv *il)
  3925. {
  3926. il->tx_traffic_idx = 0;
  3927. il->rx_traffic_idx = 0;
  3928. if (il->tx_traffic)
  3929. memset(il->tx_traffic, 0, IL_TRAFFIC_DUMP_SIZE);
  3930. if (il->rx_traffic)
  3931. memset(il->rx_traffic, 0, IL_TRAFFIC_DUMP_SIZE);
  3932. }
  3933. int
  3934. il_alloc_traffic_mem(struct il_priv *il)
  3935. {
  3936. u32 traffic_size = IL_TRAFFIC_DUMP_SIZE;
  3937. if (il_debug_level & IL_DL_TX) {
  3938. if (!il->tx_traffic) {
  3939. il->tx_traffic = kzalloc(traffic_size, GFP_KERNEL);
  3940. if (!il->tx_traffic)
  3941. return -ENOMEM;
  3942. }
  3943. }
  3944. if (il_debug_level & IL_DL_RX) {
  3945. if (!il->rx_traffic) {
  3946. il->rx_traffic = kzalloc(traffic_size, GFP_KERNEL);
  3947. if (!il->rx_traffic)
  3948. return -ENOMEM;
  3949. }
  3950. }
  3951. il_reset_traffic_log(il);
  3952. return 0;
  3953. }
  3954. EXPORT_SYMBOL(il_alloc_traffic_mem);
  3955. void
  3956. il_free_traffic_mem(struct il_priv *il)
  3957. {
  3958. kfree(il->tx_traffic);
  3959. il->tx_traffic = NULL;
  3960. kfree(il->rx_traffic);
  3961. il->rx_traffic = NULL;
  3962. }
  3963. EXPORT_SYMBOL(il_free_traffic_mem);
  3964. void
  3965. il_dbg_log_tx_data_frame(struct il_priv *il, u16 length,
  3966. struct ieee80211_hdr *header)
  3967. {
  3968. __le16 fc;
  3969. u16 len;
  3970. if (likely(!(il_debug_level & IL_DL_TX)))
  3971. return;
  3972. if (!il->tx_traffic)
  3973. return;
  3974. fc = header->frame_control;
  3975. if (ieee80211_is_data(fc)) {
  3976. len =
  3977. (length >
  3978. IL_TRAFFIC_ENTRY_SIZE) ? IL_TRAFFIC_ENTRY_SIZE : length;
  3979. memcpy((il->tx_traffic +
  3980. (il->tx_traffic_idx * IL_TRAFFIC_ENTRY_SIZE)), header,
  3981. len);
  3982. il->tx_traffic_idx =
  3983. (il->tx_traffic_idx + 1) % IL_TRAFFIC_ENTRIES;
  3984. }
  3985. }
  3986. EXPORT_SYMBOL(il_dbg_log_tx_data_frame);
  3987. void
  3988. il_dbg_log_rx_data_frame(struct il_priv *il, u16 length,
  3989. struct ieee80211_hdr *header)
  3990. {
  3991. __le16 fc;
  3992. u16 len;
  3993. if (likely(!(il_debug_level & IL_DL_RX)))
  3994. return;
  3995. if (!il->rx_traffic)
  3996. return;
  3997. fc = header->frame_control;
  3998. if (ieee80211_is_data(fc)) {
  3999. len =
  4000. (length >
  4001. IL_TRAFFIC_ENTRY_SIZE) ? IL_TRAFFIC_ENTRY_SIZE : length;
  4002. memcpy((il->rx_traffic +
  4003. (il->rx_traffic_idx * IL_TRAFFIC_ENTRY_SIZE)), header,
  4004. len);
  4005. il->rx_traffic_idx =
  4006. (il->rx_traffic_idx + 1) % IL_TRAFFIC_ENTRIES;
  4007. }
  4008. }
  4009. EXPORT_SYMBOL(il_dbg_log_rx_data_frame);
  4010. const char *
  4011. il_get_mgmt_string(int cmd)
  4012. {
  4013. switch (cmd) {
  4014. IL_CMD(MANAGEMENT_ASSOC_REQ);
  4015. IL_CMD(MANAGEMENT_ASSOC_RESP);
  4016. IL_CMD(MANAGEMENT_REASSOC_REQ);
  4017. IL_CMD(MANAGEMENT_REASSOC_RESP);
  4018. IL_CMD(MANAGEMENT_PROBE_REQ);
  4019. IL_CMD(MANAGEMENT_PROBE_RESP);
  4020. IL_CMD(MANAGEMENT_BEACON);
  4021. IL_CMD(MANAGEMENT_ATIM);
  4022. IL_CMD(MANAGEMENT_DISASSOC);
  4023. IL_CMD(MANAGEMENT_AUTH);
  4024. IL_CMD(MANAGEMENT_DEAUTH);
  4025. IL_CMD(MANAGEMENT_ACTION);
  4026. default:
  4027. return "UNKNOWN";
  4028. }
  4029. }
  4030. const char *
  4031. il_get_ctrl_string(int cmd)
  4032. {
  4033. switch (cmd) {
  4034. IL_CMD(CONTROL_BACK_REQ);
  4035. IL_CMD(CONTROL_BACK);
  4036. IL_CMD(CONTROL_PSPOLL);
  4037. IL_CMD(CONTROL_RTS);
  4038. IL_CMD(CONTROL_CTS);
  4039. IL_CMD(CONTROL_ACK);
  4040. IL_CMD(CONTROL_CFEND);
  4041. IL_CMD(CONTROL_CFENDACK);
  4042. default:
  4043. return "UNKNOWN";
  4044. }
  4045. }
  4046. void
  4047. il_clear_traffic_stats(struct il_priv *il)
  4048. {
  4049. memset(&il->tx_stats, 0, sizeof(struct traffic_stats));
  4050. memset(&il->rx_stats, 0, sizeof(struct traffic_stats));
  4051. }
  4052. /*
  4053. * if CONFIG_IWLEGACY_DEBUGFS defined,
  4054. * il_update_stats function will
  4055. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass
  4056. * Use debugFs to display the rx/rx_stats
  4057. * if CONFIG_IWLEGACY_DEBUGFS not being defined, then no MGMT and CTRL
  4058. * information will be recorded, but DATA pkt still will be recorded
  4059. * for the reason of il_led.c need to control the led blinking based on
  4060. * number of tx and rx data.
  4061. *
  4062. */
  4063. void
  4064. il_update_stats(struct il_priv *il, bool is_tx, __le16 fc, u16 len)
  4065. {
  4066. struct traffic_stats *stats;
  4067. if (is_tx)
  4068. stats = &il->tx_stats;
  4069. else
  4070. stats = &il->rx_stats;
  4071. if (ieee80211_is_mgmt(fc)) {
  4072. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  4073. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  4074. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  4075. break;
  4076. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  4077. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  4078. break;
  4079. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  4080. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  4081. break;
  4082. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  4083. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  4084. break;
  4085. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  4086. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  4087. break;
  4088. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  4089. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  4090. break;
  4091. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  4092. stats->mgmt[MANAGEMENT_BEACON]++;
  4093. break;
  4094. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  4095. stats->mgmt[MANAGEMENT_ATIM]++;
  4096. break;
  4097. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  4098. stats->mgmt[MANAGEMENT_DISASSOC]++;
  4099. break;
  4100. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  4101. stats->mgmt[MANAGEMENT_AUTH]++;
  4102. break;
  4103. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  4104. stats->mgmt[MANAGEMENT_DEAUTH]++;
  4105. break;
  4106. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  4107. stats->mgmt[MANAGEMENT_ACTION]++;
  4108. break;
  4109. }
  4110. } else if (ieee80211_is_ctl(fc)) {
  4111. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  4112. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  4113. stats->ctrl[CONTROL_BACK_REQ]++;
  4114. break;
  4115. case cpu_to_le16(IEEE80211_STYPE_BACK):
  4116. stats->ctrl[CONTROL_BACK]++;
  4117. break;
  4118. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  4119. stats->ctrl[CONTROL_PSPOLL]++;
  4120. break;
  4121. case cpu_to_le16(IEEE80211_STYPE_RTS):
  4122. stats->ctrl[CONTROL_RTS]++;
  4123. break;
  4124. case cpu_to_le16(IEEE80211_STYPE_CTS):
  4125. stats->ctrl[CONTROL_CTS]++;
  4126. break;
  4127. case cpu_to_le16(IEEE80211_STYPE_ACK):
  4128. stats->ctrl[CONTROL_ACK]++;
  4129. break;
  4130. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  4131. stats->ctrl[CONTROL_CFEND]++;
  4132. break;
  4133. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  4134. stats->ctrl[CONTROL_CFENDACK]++;
  4135. break;
  4136. }
  4137. } else {
  4138. /* data */
  4139. stats->data_cnt++;
  4140. stats->data_bytes += len;
  4141. }
  4142. }
  4143. EXPORT_SYMBOL(il_update_stats);
  4144. #endif
  4145. int
  4146. il_force_reset(struct il_priv *il, bool external)
  4147. {
  4148. struct il_force_reset *force_reset;
  4149. if (test_bit(S_EXIT_PENDING, &il->status))
  4150. return -EINVAL;
  4151. force_reset = &il->force_reset;
  4152. force_reset->reset_request_count++;
  4153. if (!external) {
  4154. if (force_reset->last_force_reset_jiffies &&
  4155. time_after(force_reset->last_force_reset_jiffies +
  4156. force_reset->reset_duration, jiffies)) {
  4157. D_INFO("force reset rejected\n");
  4158. force_reset->reset_reject_count++;
  4159. return -EAGAIN;
  4160. }
  4161. }
  4162. force_reset->reset_success_count++;
  4163. force_reset->last_force_reset_jiffies = jiffies;
  4164. /*
  4165. * if the request is from external(ex: debugfs),
  4166. * then always perform the request in regardless the module
  4167. * parameter setting
  4168. * if the request is from internal (uCode error or driver
  4169. * detect failure), then fw_restart module parameter
  4170. * need to be check before performing firmware reload
  4171. */
  4172. if (!external && !il->cfg->mod_params->restart_fw) {
  4173. D_INFO("Cancel firmware reload based on "
  4174. "module parameter setting\n");
  4175. return 0;
  4176. }
  4177. IL_ERR("On demand firmware reload\n");
  4178. /* Set the FW error flag -- cleared on il_down */
  4179. set_bit(S_FW_ERROR, &il->status);
  4180. wake_up(&il->wait_command_queue);
  4181. /*
  4182. * Keep the restart process from trying to send host
  4183. * commands by clearing the INIT status bit
  4184. */
  4185. clear_bit(S_READY, &il->status);
  4186. queue_work(il->workqueue, &il->restart);
  4187. return 0;
  4188. }
  4189. int
  4190. il_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4191. enum nl80211_iftype newtype, bool newp2p)
  4192. {
  4193. struct il_priv *il = hw->priv;
  4194. struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
  4195. int err;
  4196. if (newp2p)
  4197. return -EOPNOTSUPP;
  4198. mutex_lock(&il->mutex);
  4199. if (!ctx->vif || !il_is_ready_rf(il)) {
  4200. /*
  4201. * Huh? But wait ... this can maybe happen when
  4202. * we're in the middle of a firmware restart!
  4203. */
  4204. err = -EBUSY;
  4205. goto out;
  4206. }
  4207. /* success */
  4208. il_teardown_interface(il, vif, true);
  4209. vif->type = newtype;
  4210. vif->p2p = false;
  4211. err = il_setup_interface(il, ctx);
  4212. WARN_ON(err);
  4213. /*
  4214. * We've switched internally, but submitting to the
  4215. * device may have failed for some reason. Mask this
  4216. * error, because otherwise mac80211 will not switch
  4217. * (and set the interface type back) and we'll be
  4218. * out of sync with it.
  4219. */
  4220. err = 0;
  4221. out:
  4222. mutex_unlock(&il->mutex);
  4223. return err;
  4224. }
  4225. EXPORT_SYMBOL(il_mac_change_interface);
  4226. /*
  4227. * On every watchdog tick we check (latest) time stamp. If it does not
  4228. * change during timeout period and queue is not empty we reset firmware.
  4229. */
  4230. static int
  4231. il_check_stuck_queue(struct il_priv *il, int cnt)
  4232. {
  4233. struct il_tx_queue *txq = &il->txq[cnt];
  4234. struct il_queue *q = &txq->q;
  4235. unsigned long timeout;
  4236. int ret;
  4237. if (q->read_ptr == q->write_ptr) {
  4238. txq->time_stamp = jiffies;
  4239. return 0;
  4240. }
  4241. timeout =
  4242. txq->time_stamp +
  4243. msecs_to_jiffies(il->cfg->base_params->wd_timeout);
  4244. if (time_after(jiffies, timeout)) {
  4245. IL_ERR("Queue %d stuck for %u ms.\n", q->id,
  4246. il->cfg->base_params->wd_timeout);
  4247. ret = il_force_reset(il, false);
  4248. return (ret == -EAGAIN) ? 0 : 1;
  4249. }
  4250. return 0;
  4251. }
  4252. /*
  4253. * Making watchdog tick be a quarter of timeout assure we will
  4254. * discover the queue hung between timeout and 1.25*timeout
  4255. */
  4256. #define IL_WD_TICK(timeout) ((timeout) / 4)
  4257. /*
  4258. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  4259. * we reset the firmware. If everything is fine just rearm the timer.
  4260. */
  4261. void
  4262. il_bg_watchdog(unsigned long data)
  4263. {
  4264. struct il_priv *il = (struct il_priv *)data;
  4265. int cnt;
  4266. unsigned long timeout;
  4267. if (test_bit(S_EXIT_PENDING, &il->status))
  4268. return;
  4269. timeout = il->cfg->base_params->wd_timeout;
  4270. if (timeout == 0)
  4271. return;
  4272. /* monitor and check for stuck cmd queue */
  4273. if (il_check_stuck_queue(il, il->cmd_queue))
  4274. return;
  4275. /* monitor and check for other stuck queues */
  4276. if (il_is_any_associated(il)) {
  4277. for (cnt = 0; cnt < il->hw_params.max_txq_num; cnt++) {
  4278. /* skip as we already checked the command queue */
  4279. if (cnt == il->cmd_queue)
  4280. continue;
  4281. if (il_check_stuck_queue(il, cnt))
  4282. return;
  4283. }
  4284. }
  4285. mod_timer(&il->watchdog,
  4286. jiffies + msecs_to_jiffies(IL_WD_TICK(timeout)));
  4287. }
  4288. EXPORT_SYMBOL(il_bg_watchdog);
  4289. void
  4290. il_setup_watchdog(struct il_priv *il)
  4291. {
  4292. unsigned int timeout = il->cfg->base_params->wd_timeout;
  4293. if (timeout)
  4294. mod_timer(&il->watchdog,
  4295. jiffies + msecs_to_jiffies(IL_WD_TICK(timeout)));
  4296. else
  4297. del_timer(&il->watchdog);
  4298. }
  4299. EXPORT_SYMBOL(il_setup_watchdog);
  4300. /*
  4301. * extended beacon time format
  4302. * time in usec will be changed into a 32-bit value in extended:internal format
  4303. * the extended part is the beacon counts
  4304. * the internal part is the time in usec within one beacon interval
  4305. */
  4306. u32
  4307. il_usecs_to_beacons(struct il_priv *il, u32 usec, u32 beacon_interval)
  4308. {
  4309. u32 quot;
  4310. u32 rem;
  4311. u32 interval = beacon_interval * TIME_UNIT;
  4312. if (!interval || !usec)
  4313. return 0;
  4314. quot =
  4315. (usec /
  4316. interval) & (il_beacon_time_mask_high(il,
  4317. il->hw_params.
  4318. beacon_time_tsf_bits) >> il->
  4319. hw_params.beacon_time_tsf_bits);
  4320. rem =
  4321. (usec % interval) & il_beacon_time_mask_low(il,
  4322. il->hw_params.
  4323. beacon_time_tsf_bits);
  4324. return (quot << il->hw_params.beacon_time_tsf_bits) + rem;
  4325. }
  4326. EXPORT_SYMBOL(il_usecs_to_beacons);
  4327. /* base is usually what we get from ucode with each received frame,
  4328. * the same as HW timer counter counting down
  4329. */
  4330. __le32
  4331. il_add_beacon_time(struct il_priv *il, u32 base, u32 addon,
  4332. u32 beacon_interval)
  4333. {
  4334. u32 base_low = base & il_beacon_time_mask_low(il,
  4335. il->hw_params.
  4336. beacon_time_tsf_bits);
  4337. u32 addon_low = addon & il_beacon_time_mask_low(il,
  4338. il->hw_params.
  4339. beacon_time_tsf_bits);
  4340. u32 interval = beacon_interval * TIME_UNIT;
  4341. u32 res = (base & il_beacon_time_mask_high(il,
  4342. il->hw_params.
  4343. beacon_time_tsf_bits)) +
  4344. (addon & il_beacon_time_mask_high(il,
  4345. il->hw_params.
  4346. beacon_time_tsf_bits));
  4347. if (base_low > addon_low)
  4348. res += base_low - addon_low;
  4349. else if (base_low < addon_low) {
  4350. res += interval + base_low - addon_low;
  4351. res += (1 << il->hw_params.beacon_time_tsf_bits);
  4352. } else
  4353. res += (1 << il->hw_params.beacon_time_tsf_bits);
  4354. return cpu_to_le32(res);
  4355. }
  4356. EXPORT_SYMBOL(il_add_beacon_time);
  4357. #ifdef CONFIG_PM
  4358. int
  4359. il_pci_suspend(struct device *device)
  4360. {
  4361. struct pci_dev *pdev = to_pci_dev(device);
  4362. struct il_priv *il = pci_get_drvdata(pdev);
  4363. /*
  4364. * This function is called when system goes into suspend state
  4365. * mac80211 will call il_mac_stop() from the mac80211 suspend function
  4366. * first but since il_mac_stop() has no knowledge of who the caller is,
  4367. * it will not call apm_ops.stop() to stop the DMA operation.
  4368. * Calling apm_ops.stop here to make sure we stop the DMA.
  4369. */
  4370. il_apm_stop(il);
  4371. return 0;
  4372. }
  4373. EXPORT_SYMBOL(il_pci_suspend);
  4374. int
  4375. il_pci_resume(struct device *device)
  4376. {
  4377. struct pci_dev *pdev = to_pci_dev(device);
  4378. struct il_priv *il = pci_get_drvdata(pdev);
  4379. bool hw_rfkill = false;
  4380. /*
  4381. * We disable the RETRY_TIMEOUT register (0x41) to keep
  4382. * PCI Tx retries from interfering with C3 CPU state.
  4383. */
  4384. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  4385. il_enable_interrupts(il);
  4386. if (!(_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  4387. hw_rfkill = true;
  4388. if (hw_rfkill)
  4389. set_bit(S_RF_KILL_HW, &il->status);
  4390. else
  4391. clear_bit(S_RF_KILL_HW, &il->status);
  4392. wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rfkill);
  4393. return 0;
  4394. }
  4395. EXPORT_SYMBOL(il_pci_resume);
  4396. const struct dev_pm_ops il_pm_ops = {
  4397. .suspend = il_pci_suspend,
  4398. .resume = il_pci_resume,
  4399. .freeze = il_pci_suspend,
  4400. .thaw = il_pci_resume,
  4401. .poweroff = il_pci_suspend,
  4402. .restore = il_pci_resume,
  4403. };
  4404. EXPORT_SYMBOL(il_pm_ops);
  4405. #endif /* CONFIG_PM */
  4406. static void
  4407. il_update_qos(struct il_priv *il, struct il_rxon_context *ctx)
  4408. {
  4409. if (test_bit(S_EXIT_PENDING, &il->status))
  4410. return;
  4411. il->qos_data.def_qos_parm.qos_flags = 0;
  4412. if (il->qos_data.qos_active)
  4413. il->qos_data.def_qos_parm.qos_flags |=
  4414. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  4415. if (il->ht.enabled)
  4416. il->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
  4417. D_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
  4418. il->qos_data.qos_active, il->qos_data.def_qos_parm.qos_flags);
  4419. il_send_cmd_pdu_async(il, C_QOS_PARAM, sizeof(struct il_qosparam_cmd),
  4420. &il->qos_data.def_qos_parm, NULL);
  4421. }
  4422. /**
  4423. * il_mac_config - mac80211 config callback
  4424. */
  4425. int
  4426. il_mac_config(struct ieee80211_hw *hw, u32 changed)
  4427. {
  4428. struct il_priv *il = hw->priv;
  4429. const struct il_channel_info *ch_info;
  4430. struct ieee80211_conf *conf = &hw->conf;
  4431. struct ieee80211_channel *channel = conf->channel;
  4432. struct il_ht_config *ht_conf = &il->current_ht_config;
  4433. struct il_rxon_context *ctx = &il->ctx;
  4434. unsigned long flags = 0;
  4435. int ret = 0;
  4436. u16 ch;
  4437. int scan_active = 0;
  4438. bool ht_changed = false;
  4439. if (WARN_ON(!il->cfg->ops->legacy))
  4440. return -EOPNOTSUPP;
  4441. mutex_lock(&il->mutex);
  4442. D_MAC80211("enter to channel %d changed 0x%X\n", channel->hw_value,
  4443. changed);
  4444. if (unlikely(test_bit(S_SCANNING, &il->status))) {
  4445. scan_active = 1;
  4446. D_MAC80211("scan active\n");
  4447. }
  4448. if (changed &
  4449. (IEEE80211_CONF_CHANGE_SMPS | IEEE80211_CONF_CHANGE_CHANNEL)) {
  4450. /* mac80211 uses static for non-HT which is what we want */
  4451. il->current_ht_config.smps = conf->smps_mode;
  4452. /*
  4453. * Recalculate chain counts.
  4454. *
  4455. * If monitor mode is enabled then mac80211 will
  4456. * set up the SM PS mode to OFF if an HT channel is
  4457. * configured.
  4458. */
  4459. if (il->cfg->ops->hcmd->set_rxon_chain)
  4460. il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
  4461. }
  4462. /* during scanning mac80211 will delay channel setting until
  4463. * scan finish with changed = 0
  4464. */
  4465. if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
  4466. if (scan_active)
  4467. goto set_ch_out;
  4468. ch = channel->hw_value;
  4469. ch_info = il_get_channel_info(il, channel->band, ch);
  4470. if (!il_is_channel_valid(ch_info)) {
  4471. D_MAC80211("leave - invalid channel\n");
  4472. ret = -EINVAL;
  4473. goto set_ch_out;
  4474. }
  4475. if (il->iw_mode == NL80211_IFTYPE_ADHOC &&
  4476. !il_is_channel_ibss(ch_info)) {
  4477. D_MAC80211("leave - not IBSS channel\n");
  4478. ret = -EINVAL;
  4479. goto set_ch_out;
  4480. }
  4481. spin_lock_irqsave(&il->lock, flags);
  4482. /* Configure HT40 channels */
  4483. if (il->ht.enabled != conf_is_ht(conf)) {
  4484. il->ht.enabled = conf_is_ht(conf);
  4485. ht_changed = true;
  4486. }
  4487. if (il->ht.enabled) {
  4488. if (conf_is_ht40_minus(conf)) {
  4489. il->ht.extension_chan_offset =
  4490. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  4491. il->ht.is_40mhz = true;
  4492. } else if (conf_is_ht40_plus(conf)) {
  4493. il->ht.extension_chan_offset =
  4494. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  4495. il->ht.is_40mhz = true;
  4496. } else {
  4497. il->ht.extension_chan_offset =
  4498. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  4499. il->ht.is_40mhz = false;
  4500. }
  4501. } else
  4502. il->ht.is_40mhz = false;
  4503. /*
  4504. * Default to no protection. Protection mode will
  4505. * later be set from BSS config in il_ht_conf
  4506. */
  4507. il->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  4508. /* if we are switching from ht to 2.4 clear flags
  4509. * from any ht related info since 2.4 does not
  4510. * support ht */
  4511. if ((le16_to_cpu(il->staging.channel) != ch))
  4512. il->staging.flags = 0;
  4513. il_set_rxon_channel(il, channel, ctx);
  4514. il_set_rxon_ht(il, ht_conf);
  4515. il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
  4516. spin_unlock_irqrestore(&il->lock, flags);
  4517. if (il->cfg->ops->legacy->update_bcast_stations)
  4518. ret = il->cfg->ops->legacy->update_bcast_stations(il);
  4519. set_ch_out:
  4520. /* The list of supported rates and rate mask can be different
  4521. * for each band; since the band may have changed, reset
  4522. * the rate mask to what mac80211 lists */
  4523. il_set_rate(il);
  4524. }
  4525. if (changed & (IEEE80211_CONF_CHANGE_PS | IEEE80211_CONF_CHANGE_IDLE)) {
  4526. ret = il_power_update_mode(il, false);
  4527. if (ret)
  4528. D_MAC80211("Error setting sleep level\n");
  4529. }
  4530. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  4531. D_MAC80211("TX Power old=%d new=%d\n", il->tx_power_user_lmt,
  4532. conf->power_level);
  4533. il_set_tx_power(il, conf->power_level, false);
  4534. }
  4535. if (!il_is_ready(il)) {
  4536. D_MAC80211("leave - not ready\n");
  4537. goto out;
  4538. }
  4539. if (scan_active)
  4540. goto out;
  4541. if (memcmp(&il->active, &il->staging, sizeof(il->staging)))
  4542. il_commit_rxon(il, ctx);
  4543. else
  4544. D_INFO("Not re-sending same RXON configuration.\n");
  4545. if (ht_changed)
  4546. il_update_qos(il, ctx);
  4547. out:
  4548. D_MAC80211("leave\n");
  4549. mutex_unlock(&il->mutex);
  4550. return ret;
  4551. }
  4552. EXPORT_SYMBOL(il_mac_config);
  4553. void
  4554. il_mac_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  4555. {
  4556. struct il_priv *il = hw->priv;
  4557. unsigned long flags;
  4558. struct il_rxon_context *ctx = &il->ctx;
  4559. if (WARN_ON(!il->cfg->ops->legacy))
  4560. return;
  4561. mutex_lock(&il->mutex);
  4562. D_MAC80211("enter\n");
  4563. spin_lock_irqsave(&il->lock, flags);
  4564. memset(&il->current_ht_config, 0, sizeof(struct il_ht_config));
  4565. spin_unlock_irqrestore(&il->lock, flags);
  4566. spin_lock_irqsave(&il->lock, flags);
  4567. /* new association get rid of ibss beacon skb */
  4568. if (il->beacon_skb)
  4569. dev_kfree_skb(il->beacon_skb);
  4570. il->beacon_skb = NULL;
  4571. il->timestamp = 0;
  4572. spin_unlock_irqrestore(&il->lock, flags);
  4573. il_scan_cancel_timeout(il, 100);
  4574. if (!il_is_ready_rf(il)) {
  4575. D_MAC80211("leave - not ready\n");
  4576. mutex_unlock(&il->mutex);
  4577. return;
  4578. }
  4579. /* we are restarting association process
  4580. * clear RXON_FILTER_ASSOC_MSK bit
  4581. */
  4582. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4583. il_commit_rxon(il, ctx);
  4584. il_set_rate(il);
  4585. mutex_unlock(&il->mutex);
  4586. D_MAC80211("leave\n");
  4587. }
  4588. EXPORT_SYMBOL(il_mac_reset_tsf);
  4589. static void
  4590. il_ht_conf(struct il_priv *il, struct ieee80211_vif *vif)
  4591. {
  4592. struct il_ht_config *ht_conf = &il->current_ht_config;
  4593. struct ieee80211_sta *sta;
  4594. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  4595. D_ASSOC("enter:\n");
  4596. if (!il->ht.enabled)
  4597. return;
  4598. il->ht.protection =
  4599. bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
  4600. il->ht.non_gf_sta_present =
  4601. !!(bss_conf->
  4602. ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  4603. ht_conf->single_chain_sufficient = false;
  4604. switch (vif->type) {
  4605. case NL80211_IFTYPE_STATION:
  4606. rcu_read_lock();
  4607. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  4608. if (sta) {
  4609. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  4610. int maxstreams;
  4611. maxstreams =
  4612. (ht_cap->mcs.
  4613. tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  4614. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  4615. maxstreams += 1;
  4616. if (ht_cap->mcs.rx_mask[1] == 0 &&
  4617. ht_cap->mcs.rx_mask[2] == 0)
  4618. ht_conf->single_chain_sufficient = true;
  4619. if (maxstreams <= 1)
  4620. ht_conf->single_chain_sufficient = true;
  4621. } else {
  4622. /*
  4623. * If at all, this can only happen through a race
  4624. * when the AP disconnects us while we're still
  4625. * setting up the connection, in that case mac80211
  4626. * will soon tell us about that.
  4627. */
  4628. ht_conf->single_chain_sufficient = true;
  4629. }
  4630. rcu_read_unlock();
  4631. break;
  4632. case NL80211_IFTYPE_ADHOC:
  4633. ht_conf->single_chain_sufficient = true;
  4634. break;
  4635. default:
  4636. break;
  4637. }
  4638. D_ASSOC("leave\n");
  4639. }
  4640. static inline void
  4641. il_set_no_assoc(struct il_priv *il, struct ieee80211_vif *vif)
  4642. {
  4643. struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
  4644. /*
  4645. * inform the ucode that there is no longer an
  4646. * association and that no more packets should be
  4647. * sent
  4648. */
  4649. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4650. il->staging.assoc_id = 0;
  4651. il_commit_rxon(il, ctx);
  4652. }
  4653. static void
  4654. il_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  4655. {
  4656. struct il_priv *il = hw->priv;
  4657. unsigned long flags;
  4658. __le64 timestamp;
  4659. struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
  4660. if (!skb)
  4661. return;
  4662. D_MAC80211("enter\n");
  4663. lockdep_assert_held(&il->mutex);
  4664. if (!il->beacon_ctx) {
  4665. IL_ERR("update beacon but no beacon context!\n");
  4666. dev_kfree_skb(skb);
  4667. return;
  4668. }
  4669. spin_lock_irqsave(&il->lock, flags);
  4670. if (il->beacon_skb)
  4671. dev_kfree_skb(il->beacon_skb);
  4672. il->beacon_skb = skb;
  4673. timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
  4674. il->timestamp = le64_to_cpu(timestamp);
  4675. D_MAC80211("leave\n");
  4676. spin_unlock_irqrestore(&il->lock, flags);
  4677. if (!il_is_ready_rf(il)) {
  4678. D_MAC80211("leave - RF not ready\n");
  4679. return;
  4680. }
  4681. il->cfg->ops->legacy->post_associate(il);
  4682. }
  4683. void
  4684. il_mac_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4685. struct ieee80211_bss_conf *bss_conf, u32 changes)
  4686. {
  4687. struct il_priv *il = hw->priv;
  4688. struct il_rxon_context *ctx = il_rxon_ctx_from_vif(vif);
  4689. int ret;
  4690. if (WARN_ON(!il->cfg->ops->legacy))
  4691. return;
  4692. D_MAC80211("changes = 0x%X\n", changes);
  4693. mutex_lock(&il->mutex);
  4694. if (!il_is_alive(il)) {
  4695. mutex_unlock(&il->mutex);
  4696. return;
  4697. }
  4698. if (changes & BSS_CHANGED_QOS) {
  4699. unsigned long flags;
  4700. spin_lock_irqsave(&il->lock, flags);
  4701. il->qos_data.qos_active = bss_conf->qos;
  4702. il_update_qos(il, ctx);
  4703. spin_unlock_irqrestore(&il->lock, flags);
  4704. }
  4705. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  4706. /*
  4707. * the add_interface code must make sure we only ever
  4708. * have a single interface that could be beaconing at
  4709. * any time.
  4710. */
  4711. if (vif->bss_conf.enable_beacon)
  4712. il->beacon_ctx = ctx;
  4713. else
  4714. il->beacon_ctx = NULL;
  4715. }
  4716. if (changes & BSS_CHANGED_BSSID) {
  4717. D_MAC80211("BSSID %pM\n", bss_conf->bssid);
  4718. /*
  4719. * If there is currently a HW scan going on in the
  4720. * background then we need to cancel it else the RXON
  4721. * below/in post_associate will fail.
  4722. */
  4723. if (il_scan_cancel_timeout(il, 100)) {
  4724. IL_WARN("Aborted scan still in progress after 100ms\n");
  4725. D_MAC80211("leaving - scan abort failed.\n");
  4726. mutex_unlock(&il->mutex);
  4727. return;
  4728. }
  4729. /* mac80211 only sets assoc when in STATION mode */
  4730. if (vif->type == NL80211_IFTYPE_ADHOC || bss_conf->assoc) {
  4731. memcpy(il->staging.bssid_addr, bss_conf->bssid,
  4732. ETH_ALEN);
  4733. /* currently needed in a few places */
  4734. memcpy(il->bssid, bss_conf->bssid, ETH_ALEN);
  4735. } else {
  4736. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4737. }
  4738. }
  4739. /*
  4740. * This needs to be after setting the BSSID in case
  4741. * mac80211 decides to do both changes at once because
  4742. * it will invoke post_associate.
  4743. */
  4744. if (vif->type == NL80211_IFTYPE_ADHOC && (changes & BSS_CHANGED_BEACON))
  4745. il_beacon_update(hw, vif);
  4746. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  4747. D_MAC80211("ERP_PREAMBLE %d\n", bss_conf->use_short_preamble);
  4748. if (bss_conf->use_short_preamble)
  4749. il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  4750. else
  4751. il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  4752. }
  4753. if (changes & BSS_CHANGED_ERP_CTS_PROT) {
  4754. D_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
  4755. if (bss_conf->use_cts_prot && il->band != IEEE80211_BAND_5GHZ)
  4756. il->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
  4757. else
  4758. il->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  4759. if (bss_conf->use_cts_prot)
  4760. il->staging.flags |= RXON_FLG_SELF_CTS_EN;
  4761. else
  4762. il->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
  4763. }
  4764. if (changes & BSS_CHANGED_BASIC_RATES) {
  4765. /* XXX use this information
  4766. *
  4767. * To do that, remove code from il_set_rate() and put something
  4768. * like this here:
  4769. *
  4770. if (A-band)
  4771. il->staging.ofdm_basic_rates =
  4772. bss_conf->basic_rates;
  4773. else
  4774. il->staging.ofdm_basic_rates =
  4775. bss_conf->basic_rates >> 4;
  4776. il->staging.cck_basic_rates =
  4777. bss_conf->basic_rates & 0xF;
  4778. */
  4779. }
  4780. if (changes & BSS_CHANGED_HT) {
  4781. il_ht_conf(il, vif);
  4782. if (il->cfg->ops->hcmd->set_rxon_chain)
  4783. il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
  4784. }
  4785. if (changes & BSS_CHANGED_ASSOC) {
  4786. D_MAC80211("ASSOC %d\n", bss_conf->assoc);
  4787. if (bss_conf->assoc) {
  4788. il->timestamp = bss_conf->timestamp;
  4789. if (!il_is_rfkill(il))
  4790. il->cfg->ops->legacy->post_associate(il);
  4791. } else
  4792. il_set_no_assoc(il, vif);
  4793. }
  4794. if (changes && il_is_associated(il) && bss_conf->aid) {
  4795. D_MAC80211("Changes (%#x) while associated\n", changes);
  4796. ret = il_send_rxon_assoc(il, ctx);
  4797. if (!ret) {
  4798. /* Sync active_rxon with latest change. */
  4799. memcpy((void *)&il->active, &il->staging,
  4800. sizeof(struct il_rxon_cmd));
  4801. }
  4802. }
  4803. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  4804. if (vif->bss_conf.enable_beacon) {
  4805. memcpy(il->staging.bssid_addr, bss_conf->bssid,
  4806. ETH_ALEN);
  4807. memcpy(il->bssid, bss_conf->bssid, ETH_ALEN);
  4808. il->cfg->ops->legacy->config_ap(il);
  4809. } else
  4810. il_set_no_assoc(il, vif);
  4811. }
  4812. if (changes & BSS_CHANGED_IBSS) {
  4813. ret =
  4814. il->cfg->ops->legacy->manage_ibss_station(il, vif,
  4815. bss_conf->
  4816. ibss_joined);
  4817. if (ret)
  4818. IL_ERR("failed to %s IBSS station %pM\n",
  4819. bss_conf->ibss_joined ? "add" : "remove",
  4820. bss_conf->bssid);
  4821. }
  4822. mutex_unlock(&il->mutex);
  4823. D_MAC80211("leave\n");
  4824. }
  4825. EXPORT_SYMBOL(il_mac_bss_info_changed);
  4826. irqreturn_t
  4827. il_isr(int irq, void *data)
  4828. {
  4829. struct il_priv *il = data;
  4830. u32 inta, inta_mask;
  4831. u32 inta_fh;
  4832. unsigned long flags;
  4833. if (!il)
  4834. return IRQ_NONE;
  4835. spin_lock_irqsave(&il->lock, flags);
  4836. /* Disable (but don't clear!) interrupts here to avoid
  4837. * back-to-back ISRs and sporadic interrupts from our NIC.
  4838. * If we have something to service, the tasklet will re-enable ints.
  4839. * If we *don't* have something, we'll re-enable before leaving here. */
  4840. inta_mask = _il_rd(il, CSR_INT_MASK); /* just for debug */
  4841. _il_wr(il, CSR_INT_MASK, 0x00000000);
  4842. /* Discover which interrupts are active/pending */
  4843. inta = _il_rd(il, CSR_INT);
  4844. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  4845. /* Ignore interrupt if there's nothing in NIC to service.
  4846. * This may be due to IRQ shared with another device,
  4847. * or due to sporadic interrupts thrown from our NIC. */
  4848. if (!inta && !inta_fh) {
  4849. D_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
  4850. goto none;
  4851. }
  4852. if (inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0) {
  4853. /* Hardware disappeared. It might have already raised
  4854. * an interrupt */
  4855. IL_WARN("HARDWARE GONE?? INTA == 0x%08x\n", inta);
  4856. goto unplugged;
  4857. }
  4858. D_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta, inta_mask,
  4859. inta_fh);
  4860. inta &= ~CSR_INT_BIT_SCD;
  4861. /* il_irq_tasklet() will service interrupts and re-enable them */
  4862. if (likely(inta || inta_fh))
  4863. tasklet_schedule(&il->irq_tasklet);
  4864. unplugged:
  4865. spin_unlock_irqrestore(&il->lock, flags);
  4866. return IRQ_HANDLED;
  4867. none:
  4868. /* re-enable interrupts here since we don't have anything to service. */
  4869. /* only Re-enable if disabled by irq */
  4870. if (test_bit(S_INT_ENABLED, &il->status))
  4871. il_enable_interrupts(il);
  4872. spin_unlock_irqrestore(&il->lock, flags);
  4873. return IRQ_NONE;
  4874. }
  4875. EXPORT_SYMBOL(il_isr);
  4876. /*
  4877. * il_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
  4878. * function.
  4879. */
  4880. void
  4881. il_tx_cmd_protection(struct il_priv *il, struct ieee80211_tx_info *info,
  4882. __le16 fc, __le32 *tx_flags)
  4883. {
  4884. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  4885. *tx_flags |= TX_CMD_FLG_RTS_MSK;
  4886. *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  4887. *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  4888. if (!ieee80211_is_mgmt(fc))
  4889. return;
  4890. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  4891. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  4892. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  4893. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  4894. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  4895. *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  4896. *tx_flags |= TX_CMD_FLG_CTS_MSK;
  4897. break;
  4898. }
  4899. } else if (info->control.rates[0].
  4900. flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  4901. *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  4902. *tx_flags |= TX_CMD_FLG_CTS_MSK;
  4903. *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  4904. }
  4905. }
  4906. EXPORT_SYMBOL(il_tx_cmd_protection);