common.c 147 KB

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