common.c 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489
  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_RFKILL, &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->send_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->apm_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->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->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->update_chain_flags && update_chains)
  1003. il->ops->update_chain_flags(il);
  1004. else if (il->ops->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;
  1026. pcie_capability_read_word(il->pci_dev, PCI_EXP_LNKCTL, &lctl);
  1027. il->power_data.pci_pm = !(lctl & PCI_CFG_LINK_CTRL_VAL_L0S_EN);
  1028. il->power_data.debug_sleep_level_override = -1;
  1029. memset(&il->power_data.sleep_cmd, 0, sizeof(il->power_data.sleep_cmd));
  1030. }
  1031. EXPORT_SYMBOL(il_power_initialize);
  1032. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  1033. * sending probe req. This should be set long enough to hear probe responses
  1034. * from more than one AP. */
  1035. #define IL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  1036. #define IL_ACTIVE_DWELL_TIME_52 (20)
  1037. #define IL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  1038. #define IL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  1039. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  1040. * Must be set longer than active dwell time.
  1041. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  1042. #define IL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  1043. #define IL_PASSIVE_DWELL_TIME_52 (10)
  1044. #define IL_PASSIVE_DWELL_BASE (100)
  1045. #define IL_CHANNEL_TUNE_TIME 5
  1046. static int
  1047. il_send_scan_abort(struct il_priv *il)
  1048. {
  1049. int ret;
  1050. struct il_rx_pkt *pkt;
  1051. struct il_host_cmd cmd = {
  1052. .id = C_SCAN_ABORT,
  1053. .flags = CMD_WANT_SKB,
  1054. };
  1055. /* Exit instantly with error when device is not ready
  1056. * to receive scan abort command or it does not perform
  1057. * hardware scan currently */
  1058. if (!test_bit(S_READY, &il->status) ||
  1059. !test_bit(S_GEO_CONFIGURED, &il->status) ||
  1060. !test_bit(S_SCAN_HW, &il->status) ||
  1061. test_bit(S_FW_ERROR, &il->status) ||
  1062. test_bit(S_EXIT_PENDING, &il->status))
  1063. return -EIO;
  1064. ret = il_send_cmd_sync(il, &cmd);
  1065. if (ret)
  1066. return ret;
  1067. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1068. if (pkt->u.status != CAN_ABORT_STATUS) {
  1069. /* The scan abort will return 1 for success or
  1070. * 2 for "failure". A failure condition can be
  1071. * due to simply not being in an active scan which
  1072. * can occur if we send the scan abort before we
  1073. * the microcode has notified us that a scan is
  1074. * completed. */
  1075. D_SCAN("SCAN_ABORT ret %d.\n", pkt->u.status);
  1076. ret = -EIO;
  1077. }
  1078. il_free_pages(il, cmd.reply_page);
  1079. return ret;
  1080. }
  1081. static void
  1082. il_complete_scan(struct il_priv *il, bool aborted)
  1083. {
  1084. /* check if scan was requested from mac80211 */
  1085. if (il->scan_request) {
  1086. D_SCAN("Complete scan in mac80211\n");
  1087. ieee80211_scan_completed(il->hw, aborted);
  1088. }
  1089. il->scan_vif = NULL;
  1090. il->scan_request = NULL;
  1091. }
  1092. void
  1093. il_force_scan_end(struct il_priv *il)
  1094. {
  1095. lockdep_assert_held(&il->mutex);
  1096. if (!test_bit(S_SCANNING, &il->status)) {
  1097. D_SCAN("Forcing scan end while not scanning\n");
  1098. return;
  1099. }
  1100. D_SCAN("Forcing scan end\n");
  1101. clear_bit(S_SCANNING, &il->status);
  1102. clear_bit(S_SCAN_HW, &il->status);
  1103. clear_bit(S_SCAN_ABORTING, &il->status);
  1104. il_complete_scan(il, true);
  1105. }
  1106. static void
  1107. il_do_scan_abort(struct il_priv *il)
  1108. {
  1109. int ret;
  1110. lockdep_assert_held(&il->mutex);
  1111. if (!test_bit(S_SCANNING, &il->status)) {
  1112. D_SCAN("Not performing scan to abort\n");
  1113. return;
  1114. }
  1115. if (test_and_set_bit(S_SCAN_ABORTING, &il->status)) {
  1116. D_SCAN("Scan abort in progress\n");
  1117. return;
  1118. }
  1119. ret = il_send_scan_abort(il);
  1120. if (ret) {
  1121. D_SCAN("Send scan abort failed %d\n", ret);
  1122. il_force_scan_end(il);
  1123. } else
  1124. D_SCAN("Successfully send scan abort\n");
  1125. }
  1126. /**
  1127. * il_scan_cancel - Cancel any currently executing HW scan
  1128. */
  1129. int
  1130. il_scan_cancel(struct il_priv *il)
  1131. {
  1132. D_SCAN("Queuing abort scan\n");
  1133. queue_work(il->workqueue, &il->abort_scan);
  1134. return 0;
  1135. }
  1136. EXPORT_SYMBOL(il_scan_cancel);
  1137. /**
  1138. * il_scan_cancel_timeout - Cancel any currently executing HW scan
  1139. * @ms: amount of time to wait (in milliseconds) for scan to abort
  1140. *
  1141. */
  1142. int
  1143. il_scan_cancel_timeout(struct il_priv *il, unsigned long ms)
  1144. {
  1145. unsigned long timeout = jiffies + msecs_to_jiffies(ms);
  1146. lockdep_assert_held(&il->mutex);
  1147. D_SCAN("Scan cancel timeout\n");
  1148. il_do_scan_abort(il);
  1149. while (time_before_eq(jiffies, timeout)) {
  1150. if (!test_bit(S_SCAN_HW, &il->status))
  1151. break;
  1152. msleep(20);
  1153. }
  1154. return test_bit(S_SCAN_HW, &il->status);
  1155. }
  1156. EXPORT_SYMBOL(il_scan_cancel_timeout);
  1157. /* Service response to C_SCAN (0x80) */
  1158. static void
  1159. il_hdl_scan(struct il_priv *il, struct il_rx_buf *rxb)
  1160. {
  1161. #ifdef CONFIG_IWLEGACY_DEBUG
  1162. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1163. struct il_scanreq_notification *notif =
  1164. (struct il_scanreq_notification *)pkt->u.raw;
  1165. D_SCAN("Scan request status = 0x%x\n", notif->status);
  1166. #endif
  1167. }
  1168. /* Service N_SCAN_START (0x82) */
  1169. static void
  1170. il_hdl_scan_start(struct il_priv *il, struct il_rx_buf *rxb)
  1171. {
  1172. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1173. struct il_scanstart_notification *notif =
  1174. (struct il_scanstart_notification *)pkt->u.raw;
  1175. il->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  1176. D_SCAN("Scan start: " "%d [802.11%s] "
  1177. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n", notif->channel,
  1178. notif->band ? "bg" : "a", le32_to_cpu(notif->tsf_high),
  1179. le32_to_cpu(notif->tsf_low), notif->status, notif->beacon_timer);
  1180. }
  1181. /* Service N_SCAN_RESULTS (0x83) */
  1182. static void
  1183. il_hdl_scan_results(struct il_priv *il, struct il_rx_buf *rxb)
  1184. {
  1185. #ifdef CONFIG_IWLEGACY_DEBUG
  1186. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1187. struct il_scanresults_notification *notif =
  1188. (struct il_scanresults_notification *)pkt->u.raw;
  1189. D_SCAN("Scan ch.res: " "%d [802.11%s] " "(TSF: 0x%08X:%08X) - %d "
  1190. "elapsed=%lu usec\n", notif->channel, notif->band ? "bg" : "a",
  1191. le32_to_cpu(notif->tsf_high), le32_to_cpu(notif->tsf_low),
  1192. le32_to_cpu(notif->stats[0]),
  1193. le32_to_cpu(notif->tsf_low) - il->scan_start_tsf);
  1194. #endif
  1195. }
  1196. /* Service N_SCAN_COMPLETE (0x84) */
  1197. static void
  1198. il_hdl_scan_complete(struct il_priv *il, struct il_rx_buf *rxb)
  1199. {
  1200. #ifdef CONFIG_IWLEGACY_DEBUG
  1201. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1202. struct il_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  1203. #endif
  1204. D_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  1205. scan_notif->scanned_channels, scan_notif->tsf_low,
  1206. scan_notif->tsf_high, scan_notif->status);
  1207. /* The HW is no longer scanning */
  1208. clear_bit(S_SCAN_HW, &il->status);
  1209. D_SCAN("Scan on %sGHz took %dms\n",
  1210. (il->scan_band == IEEE80211_BAND_2GHZ) ? "2.4" : "5.2",
  1211. jiffies_to_msecs(jiffies - il->scan_start));
  1212. queue_work(il->workqueue, &il->scan_completed);
  1213. }
  1214. void
  1215. il_setup_rx_scan_handlers(struct il_priv *il)
  1216. {
  1217. /* scan handlers */
  1218. il->handlers[C_SCAN] = il_hdl_scan;
  1219. il->handlers[N_SCAN_START] = il_hdl_scan_start;
  1220. il->handlers[N_SCAN_RESULTS] = il_hdl_scan_results;
  1221. il->handlers[N_SCAN_COMPLETE] = il_hdl_scan_complete;
  1222. }
  1223. EXPORT_SYMBOL(il_setup_rx_scan_handlers);
  1224. inline u16
  1225. il_get_active_dwell_time(struct il_priv *il, enum ieee80211_band band,
  1226. u8 n_probes)
  1227. {
  1228. if (band == IEEE80211_BAND_5GHZ)
  1229. return IL_ACTIVE_DWELL_TIME_52 +
  1230. IL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  1231. else
  1232. return IL_ACTIVE_DWELL_TIME_24 +
  1233. IL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  1234. }
  1235. EXPORT_SYMBOL(il_get_active_dwell_time);
  1236. u16
  1237. il_get_passive_dwell_time(struct il_priv *il, enum ieee80211_band band,
  1238. struct ieee80211_vif *vif)
  1239. {
  1240. u16 value;
  1241. u16 passive =
  1242. (band ==
  1243. IEEE80211_BAND_2GHZ) ? IL_PASSIVE_DWELL_BASE +
  1244. IL_PASSIVE_DWELL_TIME_24 : IL_PASSIVE_DWELL_BASE +
  1245. IL_PASSIVE_DWELL_TIME_52;
  1246. if (il_is_any_associated(il)) {
  1247. /*
  1248. * If we're associated, we clamp the maximum passive
  1249. * dwell time to be 98% of the smallest beacon interval
  1250. * (minus 2 * channel tune time)
  1251. */
  1252. value = il->vif ? il->vif->bss_conf.beacon_int : 0;
  1253. if (value > IL_PASSIVE_DWELL_BASE || !value)
  1254. value = IL_PASSIVE_DWELL_BASE;
  1255. value = (value * 98) / 100 - IL_CHANNEL_TUNE_TIME * 2;
  1256. passive = min(value, passive);
  1257. }
  1258. return passive;
  1259. }
  1260. EXPORT_SYMBOL(il_get_passive_dwell_time);
  1261. void
  1262. il_init_scan_params(struct il_priv *il)
  1263. {
  1264. u8 ant_idx = fls(il->hw_params.valid_tx_ant) - 1;
  1265. if (!il->scan_tx_ant[IEEE80211_BAND_5GHZ])
  1266. il->scan_tx_ant[IEEE80211_BAND_5GHZ] = ant_idx;
  1267. if (!il->scan_tx_ant[IEEE80211_BAND_2GHZ])
  1268. il->scan_tx_ant[IEEE80211_BAND_2GHZ] = ant_idx;
  1269. }
  1270. EXPORT_SYMBOL(il_init_scan_params);
  1271. static int
  1272. il_scan_initiate(struct il_priv *il, struct ieee80211_vif *vif)
  1273. {
  1274. int ret;
  1275. lockdep_assert_held(&il->mutex);
  1276. cancel_delayed_work(&il->scan_check);
  1277. if (!il_is_ready_rf(il)) {
  1278. IL_WARN("Request scan called when driver not ready.\n");
  1279. return -EIO;
  1280. }
  1281. if (test_bit(S_SCAN_HW, &il->status)) {
  1282. D_SCAN("Multiple concurrent scan requests in parallel.\n");
  1283. return -EBUSY;
  1284. }
  1285. if (test_bit(S_SCAN_ABORTING, &il->status)) {
  1286. D_SCAN("Scan request while abort pending.\n");
  1287. return -EBUSY;
  1288. }
  1289. D_SCAN("Starting scan...\n");
  1290. set_bit(S_SCANNING, &il->status);
  1291. il->scan_start = jiffies;
  1292. ret = il->ops->request_scan(il, vif);
  1293. if (ret) {
  1294. clear_bit(S_SCANNING, &il->status);
  1295. return ret;
  1296. }
  1297. queue_delayed_work(il->workqueue, &il->scan_check,
  1298. IL_SCAN_CHECK_WATCHDOG);
  1299. return 0;
  1300. }
  1301. int
  1302. il_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1303. struct cfg80211_scan_request *req)
  1304. {
  1305. struct il_priv *il = hw->priv;
  1306. int ret;
  1307. if (req->n_channels == 0) {
  1308. IL_ERR("Can not scan on no channels.\n");
  1309. return -EINVAL;
  1310. }
  1311. mutex_lock(&il->mutex);
  1312. D_MAC80211("enter\n");
  1313. if (test_bit(S_SCANNING, &il->status)) {
  1314. D_SCAN("Scan already in progress.\n");
  1315. ret = -EAGAIN;
  1316. goto out_unlock;
  1317. }
  1318. /* mac80211 will only ask for one band at a time */
  1319. il->scan_request = req;
  1320. il->scan_vif = vif;
  1321. il->scan_band = req->channels[0]->band;
  1322. ret = il_scan_initiate(il, vif);
  1323. out_unlock:
  1324. D_MAC80211("leave ret %d\n", ret);
  1325. mutex_unlock(&il->mutex);
  1326. return ret;
  1327. }
  1328. EXPORT_SYMBOL(il_mac_hw_scan);
  1329. static void
  1330. il_bg_scan_check(struct work_struct *data)
  1331. {
  1332. struct il_priv *il =
  1333. container_of(data, struct il_priv, scan_check.work);
  1334. D_SCAN("Scan check work\n");
  1335. /* Since we are here firmware does not finish scan and
  1336. * most likely is in bad shape, so we don't bother to
  1337. * send abort command, just force scan complete to mac80211 */
  1338. mutex_lock(&il->mutex);
  1339. il_force_scan_end(il);
  1340. mutex_unlock(&il->mutex);
  1341. }
  1342. /**
  1343. * il_fill_probe_req - fill in all required fields and IE for probe request
  1344. */
  1345. u16
  1346. il_fill_probe_req(struct il_priv *il, struct ieee80211_mgmt *frame,
  1347. const u8 *ta, const u8 *ies, int ie_len, int left)
  1348. {
  1349. int len = 0;
  1350. u8 *pos = NULL;
  1351. /* Make sure there is enough space for the probe request,
  1352. * two mandatory IEs and the data */
  1353. left -= 24;
  1354. if (left < 0)
  1355. return 0;
  1356. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1357. eth_broadcast_addr(frame->da);
  1358. memcpy(frame->sa, ta, ETH_ALEN);
  1359. eth_broadcast_addr(frame->bssid);
  1360. frame->seq_ctrl = 0;
  1361. len += 24;
  1362. /* ...next IE... */
  1363. pos = &frame->u.probe_req.variable[0];
  1364. /* fill in our indirect SSID IE */
  1365. left -= 2;
  1366. if (left < 0)
  1367. return 0;
  1368. *pos++ = WLAN_EID_SSID;
  1369. *pos++ = 0;
  1370. len += 2;
  1371. if (WARN_ON(left < ie_len))
  1372. return len;
  1373. if (ies && ie_len) {
  1374. memcpy(pos, ies, ie_len);
  1375. len += ie_len;
  1376. }
  1377. return (u16) len;
  1378. }
  1379. EXPORT_SYMBOL(il_fill_probe_req);
  1380. static void
  1381. il_bg_abort_scan(struct work_struct *work)
  1382. {
  1383. struct il_priv *il = container_of(work, struct il_priv, abort_scan);
  1384. D_SCAN("Abort scan work\n");
  1385. /* We keep scan_check work queued in case when firmware will not
  1386. * report back scan completed notification */
  1387. mutex_lock(&il->mutex);
  1388. il_scan_cancel_timeout(il, 200);
  1389. mutex_unlock(&il->mutex);
  1390. }
  1391. static void
  1392. il_bg_scan_completed(struct work_struct *work)
  1393. {
  1394. struct il_priv *il = container_of(work, struct il_priv, scan_completed);
  1395. bool aborted;
  1396. D_SCAN("Completed scan.\n");
  1397. cancel_delayed_work(&il->scan_check);
  1398. mutex_lock(&il->mutex);
  1399. aborted = test_and_clear_bit(S_SCAN_ABORTING, &il->status);
  1400. if (aborted)
  1401. D_SCAN("Aborted scan completed.\n");
  1402. if (!test_and_clear_bit(S_SCANNING, &il->status)) {
  1403. D_SCAN("Scan already completed.\n");
  1404. goto out_settings;
  1405. }
  1406. il_complete_scan(il, aborted);
  1407. out_settings:
  1408. /* Can we still talk to firmware ? */
  1409. if (!il_is_ready_rf(il))
  1410. goto out;
  1411. /*
  1412. * We do not commit power settings while scan is pending,
  1413. * do it now if the settings changed.
  1414. */
  1415. il_power_set_mode(il, &il->power_data.sleep_cmd_next, false);
  1416. il_set_tx_power(il, il->tx_power_next, false);
  1417. il->ops->post_scan(il);
  1418. out:
  1419. mutex_unlock(&il->mutex);
  1420. }
  1421. void
  1422. il_setup_scan_deferred_work(struct il_priv *il)
  1423. {
  1424. INIT_WORK(&il->scan_completed, il_bg_scan_completed);
  1425. INIT_WORK(&il->abort_scan, il_bg_abort_scan);
  1426. INIT_DELAYED_WORK(&il->scan_check, il_bg_scan_check);
  1427. }
  1428. EXPORT_SYMBOL(il_setup_scan_deferred_work);
  1429. void
  1430. il_cancel_scan_deferred_work(struct il_priv *il)
  1431. {
  1432. cancel_work_sync(&il->abort_scan);
  1433. cancel_work_sync(&il->scan_completed);
  1434. if (cancel_delayed_work_sync(&il->scan_check)) {
  1435. mutex_lock(&il->mutex);
  1436. il_force_scan_end(il);
  1437. mutex_unlock(&il->mutex);
  1438. }
  1439. }
  1440. EXPORT_SYMBOL(il_cancel_scan_deferred_work);
  1441. /* il->sta_lock must be held */
  1442. static void
  1443. il_sta_ucode_activate(struct il_priv *il, u8 sta_id)
  1444. {
  1445. if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE))
  1446. IL_ERR("ACTIVATE a non DRIVER active station id %u addr %pM\n",
  1447. sta_id, il->stations[sta_id].sta.sta.addr);
  1448. if (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) {
  1449. D_ASSOC("STA id %u addr %pM already present"
  1450. " in uCode (according to driver)\n", sta_id,
  1451. il->stations[sta_id].sta.sta.addr);
  1452. } else {
  1453. il->stations[sta_id].used |= IL_STA_UCODE_ACTIVE;
  1454. D_ASSOC("Added STA id %u addr %pM to uCode\n", sta_id,
  1455. il->stations[sta_id].sta.sta.addr);
  1456. }
  1457. }
  1458. static int
  1459. il_process_add_sta_resp(struct il_priv *il, struct il_addsta_cmd *addsta,
  1460. struct il_rx_pkt *pkt, bool sync)
  1461. {
  1462. u8 sta_id = addsta->sta.sta_id;
  1463. unsigned long flags;
  1464. int ret = -EIO;
  1465. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  1466. IL_ERR("Bad return from C_ADD_STA (0x%08X)\n", pkt->hdr.flags);
  1467. return ret;
  1468. }
  1469. D_INFO("Processing response for adding station %u\n", sta_id);
  1470. spin_lock_irqsave(&il->sta_lock, flags);
  1471. switch (pkt->u.add_sta.status) {
  1472. case ADD_STA_SUCCESS_MSK:
  1473. D_INFO("C_ADD_STA PASSED\n");
  1474. il_sta_ucode_activate(il, sta_id);
  1475. ret = 0;
  1476. break;
  1477. case ADD_STA_NO_ROOM_IN_TBL:
  1478. IL_ERR("Adding station %d failed, no room in table.\n", sta_id);
  1479. break;
  1480. case ADD_STA_NO_BLOCK_ACK_RESOURCE:
  1481. IL_ERR("Adding station %d failed, no block ack resource.\n",
  1482. sta_id);
  1483. break;
  1484. case ADD_STA_MODIFY_NON_EXIST_STA:
  1485. IL_ERR("Attempting to modify non-existing station %d\n",
  1486. sta_id);
  1487. break;
  1488. default:
  1489. D_ASSOC("Received C_ADD_STA:(0x%08X)\n", pkt->u.add_sta.status);
  1490. break;
  1491. }
  1492. D_INFO("%s station id %u addr %pM\n",
  1493. il->stations[sta_id].sta.mode ==
  1494. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added", sta_id,
  1495. il->stations[sta_id].sta.sta.addr);
  1496. /*
  1497. * XXX: The MAC address in the command buffer is often changed from
  1498. * the original sent to the device. That is, the MAC address
  1499. * written to the command buffer often is not the same MAC address
  1500. * read from the command buffer when the command returns. This
  1501. * issue has not yet been resolved and this debugging is left to
  1502. * observe the problem.
  1503. */
  1504. D_INFO("%s station according to cmd buffer %pM\n",
  1505. il->stations[sta_id].sta.mode ==
  1506. STA_CONTROL_MODIFY_MSK ? "Modified" : "Added", addsta->sta.addr);
  1507. spin_unlock_irqrestore(&il->sta_lock, flags);
  1508. return ret;
  1509. }
  1510. static void
  1511. il_add_sta_callback(struct il_priv *il, struct il_device_cmd *cmd,
  1512. struct il_rx_pkt *pkt)
  1513. {
  1514. struct il_addsta_cmd *addsta = (struct il_addsta_cmd *)cmd->cmd.payload;
  1515. il_process_add_sta_resp(il, addsta, pkt, false);
  1516. }
  1517. int
  1518. il_send_add_sta(struct il_priv *il, struct il_addsta_cmd *sta, u8 flags)
  1519. {
  1520. struct il_rx_pkt *pkt = NULL;
  1521. int ret = 0;
  1522. u8 data[sizeof(*sta)];
  1523. struct il_host_cmd cmd = {
  1524. .id = C_ADD_STA,
  1525. .flags = flags,
  1526. .data = data,
  1527. };
  1528. u8 sta_id __maybe_unused = sta->sta.sta_id;
  1529. D_INFO("Adding sta %u (%pM) %ssynchronously\n", sta_id, sta->sta.addr,
  1530. flags & CMD_ASYNC ? "a" : "");
  1531. if (flags & CMD_ASYNC)
  1532. cmd.callback = il_add_sta_callback;
  1533. else {
  1534. cmd.flags |= CMD_WANT_SKB;
  1535. might_sleep();
  1536. }
  1537. cmd.len = il->ops->build_addsta_hcmd(sta, data);
  1538. ret = il_send_cmd(il, &cmd);
  1539. if (ret || (flags & CMD_ASYNC))
  1540. return ret;
  1541. if (ret == 0) {
  1542. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1543. ret = il_process_add_sta_resp(il, sta, pkt, true);
  1544. }
  1545. il_free_pages(il, cmd.reply_page);
  1546. return ret;
  1547. }
  1548. EXPORT_SYMBOL(il_send_add_sta);
  1549. static void
  1550. il_set_ht_add_station(struct il_priv *il, u8 idx, struct ieee80211_sta *sta)
  1551. {
  1552. struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
  1553. __le32 sta_flags;
  1554. u8 mimo_ps_mode;
  1555. if (!sta || !sta_ht_inf->ht_supported)
  1556. goto done;
  1557. mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
  1558. D_ASSOC("spatial multiplexing power save mode: %s\n",
  1559. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ? "static" :
  1560. (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ? "dynamic" :
  1561. "disabled");
  1562. sta_flags = il->stations[idx].sta.station_flags;
  1563. sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
  1564. switch (mimo_ps_mode) {
  1565. case WLAN_HT_CAP_SM_PS_STATIC:
  1566. sta_flags |= STA_FLG_MIMO_DIS_MSK;
  1567. break;
  1568. case WLAN_HT_CAP_SM_PS_DYNAMIC:
  1569. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  1570. break;
  1571. case WLAN_HT_CAP_SM_PS_DISABLED:
  1572. break;
  1573. default:
  1574. IL_WARN("Invalid MIMO PS mode %d\n", mimo_ps_mode);
  1575. break;
  1576. }
  1577. sta_flags |=
  1578. cpu_to_le32((u32) sta_ht_inf->
  1579. ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  1580. sta_flags |=
  1581. cpu_to_le32((u32) sta_ht_inf->
  1582. ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  1583. if (il_is_ht40_tx_allowed(il, &sta->ht_cap))
  1584. sta_flags |= STA_FLG_HT40_EN_MSK;
  1585. else
  1586. sta_flags &= ~STA_FLG_HT40_EN_MSK;
  1587. il->stations[idx].sta.station_flags = sta_flags;
  1588. done:
  1589. return;
  1590. }
  1591. /**
  1592. * il_prep_station - Prepare station information for addition
  1593. *
  1594. * should be called with sta_lock held
  1595. */
  1596. u8
  1597. il_prep_station(struct il_priv *il, const u8 *addr, bool is_ap,
  1598. struct ieee80211_sta *sta)
  1599. {
  1600. struct il_station_entry *station;
  1601. int i;
  1602. u8 sta_id = IL_INVALID_STATION;
  1603. u16 rate;
  1604. if (is_ap)
  1605. sta_id = IL_AP_ID;
  1606. else if (is_broadcast_ether_addr(addr))
  1607. sta_id = il->hw_params.bcast_id;
  1608. else
  1609. for (i = IL_STA_ID; i < il->hw_params.max_stations; i++) {
  1610. if (ether_addr_equal(il->stations[i].sta.sta.addr,
  1611. addr)) {
  1612. sta_id = i;
  1613. break;
  1614. }
  1615. if (!il->stations[i].used &&
  1616. sta_id == IL_INVALID_STATION)
  1617. sta_id = i;
  1618. }
  1619. /*
  1620. * These two conditions have the same outcome, but keep them
  1621. * separate
  1622. */
  1623. if (unlikely(sta_id == IL_INVALID_STATION))
  1624. return sta_id;
  1625. /*
  1626. * uCode is not able to deal with multiple requests to add a
  1627. * station. Keep track if one is in progress so that we do not send
  1628. * another.
  1629. */
  1630. if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
  1631. D_INFO("STA %d already in process of being added.\n", sta_id);
  1632. return sta_id;
  1633. }
  1634. if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
  1635. (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE) &&
  1636. ether_addr_equal(il->stations[sta_id].sta.sta.addr, addr)) {
  1637. D_ASSOC("STA %d (%pM) already added, not adding again.\n",
  1638. sta_id, addr);
  1639. return sta_id;
  1640. }
  1641. station = &il->stations[sta_id];
  1642. station->used = IL_STA_DRIVER_ACTIVE;
  1643. D_ASSOC("Add STA to driver ID %d: %pM\n", sta_id, addr);
  1644. il->num_stations++;
  1645. /* Set up the C_ADD_STA command to send to device */
  1646. memset(&station->sta, 0, sizeof(struct il_addsta_cmd));
  1647. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  1648. station->sta.mode = 0;
  1649. station->sta.sta.sta_id = sta_id;
  1650. station->sta.station_flags = 0;
  1651. /*
  1652. * OK to call unconditionally, since local stations (IBSS BSSID
  1653. * STA and broadcast STA) pass in a NULL sta, and mac80211
  1654. * doesn't allow HT IBSS.
  1655. */
  1656. il_set_ht_add_station(il, sta_id, sta);
  1657. /* 3945 only */
  1658. rate = (il->band == IEEE80211_BAND_5GHZ) ? RATE_6M_PLCP : RATE_1M_PLCP;
  1659. /* Turn on both antennas for the station... */
  1660. station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
  1661. return sta_id;
  1662. }
  1663. EXPORT_SYMBOL_GPL(il_prep_station);
  1664. #define STA_WAIT_TIMEOUT (HZ/2)
  1665. /**
  1666. * il_add_station_common -
  1667. */
  1668. int
  1669. il_add_station_common(struct il_priv *il, const u8 *addr, bool is_ap,
  1670. struct ieee80211_sta *sta, u8 *sta_id_r)
  1671. {
  1672. unsigned long flags_spin;
  1673. int ret = 0;
  1674. u8 sta_id;
  1675. struct il_addsta_cmd sta_cmd;
  1676. *sta_id_r = 0;
  1677. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1678. sta_id = il_prep_station(il, addr, is_ap, sta);
  1679. if (sta_id == IL_INVALID_STATION) {
  1680. IL_ERR("Unable to prepare station %pM for addition\n", addr);
  1681. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1682. return -EINVAL;
  1683. }
  1684. /*
  1685. * uCode is not able to deal with multiple requests to add a
  1686. * station. Keep track if one is in progress so that we do not send
  1687. * another.
  1688. */
  1689. if (il->stations[sta_id].used & IL_STA_UCODE_INPROGRESS) {
  1690. D_INFO("STA %d already in process of being added.\n", sta_id);
  1691. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1692. return -EEXIST;
  1693. }
  1694. if ((il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE) &&
  1695. (il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
  1696. D_ASSOC("STA %d (%pM) already added, not adding again.\n",
  1697. sta_id, addr);
  1698. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1699. return -EEXIST;
  1700. }
  1701. il->stations[sta_id].used |= IL_STA_UCODE_INPROGRESS;
  1702. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1703. sizeof(struct il_addsta_cmd));
  1704. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1705. /* Add station to device's station table */
  1706. ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1707. if (ret) {
  1708. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1709. IL_ERR("Adding station %pM failed.\n",
  1710. il->stations[sta_id].sta.sta.addr);
  1711. il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
  1712. il->stations[sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  1713. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1714. }
  1715. *sta_id_r = sta_id;
  1716. return ret;
  1717. }
  1718. EXPORT_SYMBOL(il_add_station_common);
  1719. /**
  1720. * il_sta_ucode_deactivate - deactivate ucode status for a station
  1721. *
  1722. * il->sta_lock must be held
  1723. */
  1724. static void
  1725. il_sta_ucode_deactivate(struct il_priv *il, u8 sta_id)
  1726. {
  1727. /* Ucode must be active and driver must be non active */
  1728. if ((il->stations[sta_id].
  1729. used & (IL_STA_UCODE_ACTIVE | IL_STA_DRIVER_ACTIVE)) !=
  1730. IL_STA_UCODE_ACTIVE)
  1731. IL_ERR("removed non active STA %u\n", sta_id);
  1732. il->stations[sta_id].used &= ~IL_STA_UCODE_ACTIVE;
  1733. memset(&il->stations[sta_id], 0, sizeof(struct il_station_entry));
  1734. D_ASSOC("Removed STA %u\n", sta_id);
  1735. }
  1736. static int
  1737. il_send_remove_station(struct il_priv *il, const u8 * addr, int sta_id,
  1738. bool temporary)
  1739. {
  1740. struct il_rx_pkt *pkt;
  1741. int ret;
  1742. unsigned long flags_spin;
  1743. struct il_rem_sta_cmd rm_sta_cmd;
  1744. struct il_host_cmd cmd = {
  1745. .id = C_REM_STA,
  1746. .len = sizeof(struct il_rem_sta_cmd),
  1747. .flags = CMD_SYNC,
  1748. .data = &rm_sta_cmd,
  1749. };
  1750. memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
  1751. rm_sta_cmd.num_sta = 1;
  1752. memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
  1753. cmd.flags |= CMD_WANT_SKB;
  1754. ret = il_send_cmd(il, &cmd);
  1755. if (ret)
  1756. return ret;
  1757. pkt = (struct il_rx_pkt *)cmd.reply_page;
  1758. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  1759. IL_ERR("Bad return from C_REM_STA (0x%08X)\n", pkt->hdr.flags);
  1760. ret = -EIO;
  1761. }
  1762. if (!ret) {
  1763. switch (pkt->u.rem_sta.status) {
  1764. case REM_STA_SUCCESS_MSK:
  1765. if (!temporary) {
  1766. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1767. il_sta_ucode_deactivate(il, sta_id);
  1768. spin_unlock_irqrestore(&il->sta_lock,
  1769. flags_spin);
  1770. }
  1771. D_ASSOC("C_REM_STA PASSED\n");
  1772. break;
  1773. default:
  1774. ret = -EIO;
  1775. IL_ERR("C_REM_STA failed\n");
  1776. break;
  1777. }
  1778. }
  1779. il_free_pages(il, cmd.reply_page);
  1780. return ret;
  1781. }
  1782. /**
  1783. * il_remove_station - Remove driver's knowledge of station.
  1784. */
  1785. int
  1786. il_remove_station(struct il_priv *il, const u8 sta_id, const u8 * addr)
  1787. {
  1788. unsigned long flags;
  1789. if (!il_is_ready(il)) {
  1790. D_INFO("Unable to remove station %pM, device not ready.\n",
  1791. addr);
  1792. /*
  1793. * It is typical for stations to be removed when we are
  1794. * going down. Return success since device will be down
  1795. * soon anyway
  1796. */
  1797. return 0;
  1798. }
  1799. D_ASSOC("Removing STA from driver:%d %pM\n", sta_id, addr);
  1800. if (WARN_ON(sta_id == IL_INVALID_STATION))
  1801. return -EINVAL;
  1802. spin_lock_irqsave(&il->sta_lock, flags);
  1803. if (!(il->stations[sta_id].used & IL_STA_DRIVER_ACTIVE)) {
  1804. D_INFO("Removing %pM but non DRIVER active\n", addr);
  1805. goto out_err;
  1806. }
  1807. if (!(il->stations[sta_id].used & IL_STA_UCODE_ACTIVE)) {
  1808. D_INFO("Removing %pM but non UCODE active\n", addr);
  1809. goto out_err;
  1810. }
  1811. if (il->stations[sta_id].used & IL_STA_LOCAL) {
  1812. kfree(il->stations[sta_id].lq);
  1813. il->stations[sta_id].lq = NULL;
  1814. }
  1815. il->stations[sta_id].used &= ~IL_STA_DRIVER_ACTIVE;
  1816. il->num_stations--;
  1817. BUG_ON(il->num_stations < 0);
  1818. spin_unlock_irqrestore(&il->sta_lock, flags);
  1819. return il_send_remove_station(il, addr, sta_id, false);
  1820. out_err:
  1821. spin_unlock_irqrestore(&il->sta_lock, flags);
  1822. return -EINVAL;
  1823. }
  1824. EXPORT_SYMBOL_GPL(il_remove_station);
  1825. /**
  1826. * il_clear_ucode_stations - clear ucode station table bits
  1827. *
  1828. * This function clears all the bits in the driver indicating
  1829. * which stations are active in the ucode. Call when something
  1830. * other than explicit station management would cause this in
  1831. * the ucode, e.g. unassociated RXON.
  1832. */
  1833. void
  1834. il_clear_ucode_stations(struct il_priv *il)
  1835. {
  1836. int i;
  1837. unsigned long flags_spin;
  1838. bool cleared = false;
  1839. D_INFO("Clearing ucode stations in driver\n");
  1840. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1841. for (i = 0; i < il->hw_params.max_stations; i++) {
  1842. if (il->stations[i].used & IL_STA_UCODE_ACTIVE) {
  1843. D_INFO("Clearing ucode active for station %d\n", i);
  1844. il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
  1845. cleared = true;
  1846. }
  1847. }
  1848. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1849. if (!cleared)
  1850. D_INFO("No active stations found to be cleared\n");
  1851. }
  1852. EXPORT_SYMBOL(il_clear_ucode_stations);
  1853. /**
  1854. * il_restore_stations() - Restore driver known stations to device
  1855. *
  1856. * All stations considered active by driver, but not present in ucode, is
  1857. * restored.
  1858. *
  1859. * Function sleeps.
  1860. */
  1861. void
  1862. il_restore_stations(struct il_priv *il)
  1863. {
  1864. struct il_addsta_cmd sta_cmd;
  1865. struct il_link_quality_cmd lq;
  1866. unsigned long flags_spin;
  1867. int i;
  1868. bool found = false;
  1869. int ret;
  1870. bool send_lq;
  1871. if (!il_is_ready(il)) {
  1872. D_INFO("Not ready yet, not restoring any stations.\n");
  1873. return;
  1874. }
  1875. D_ASSOC("Restoring all known stations ... start.\n");
  1876. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1877. for (i = 0; i < il->hw_params.max_stations; i++) {
  1878. if ((il->stations[i].used & IL_STA_DRIVER_ACTIVE) &&
  1879. !(il->stations[i].used & IL_STA_UCODE_ACTIVE)) {
  1880. D_ASSOC("Restoring sta %pM\n",
  1881. il->stations[i].sta.sta.addr);
  1882. il->stations[i].sta.mode = 0;
  1883. il->stations[i].used |= IL_STA_UCODE_INPROGRESS;
  1884. found = true;
  1885. }
  1886. }
  1887. for (i = 0; i < il->hw_params.max_stations; i++) {
  1888. if ((il->stations[i].used & IL_STA_UCODE_INPROGRESS)) {
  1889. memcpy(&sta_cmd, &il->stations[i].sta,
  1890. sizeof(struct il_addsta_cmd));
  1891. send_lq = false;
  1892. if (il->stations[i].lq) {
  1893. memcpy(&lq, il->stations[i].lq,
  1894. sizeof(struct il_link_quality_cmd));
  1895. send_lq = true;
  1896. }
  1897. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1898. ret = il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1899. if (ret) {
  1900. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1901. IL_ERR("Adding station %pM failed.\n",
  1902. il->stations[i].sta.sta.addr);
  1903. il->stations[i].used &= ~IL_STA_DRIVER_ACTIVE;
  1904. il->stations[i].used &=
  1905. ~IL_STA_UCODE_INPROGRESS;
  1906. spin_unlock_irqrestore(&il->sta_lock,
  1907. flags_spin);
  1908. }
  1909. /*
  1910. * Rate scaling has already been initialized, send
  1911. * current LQ command
  1912. */
  1913. if (send_lq)
  1914. il_send_lq_cmd(il, &lq, CMD_SYNC, true);
  1915. spin_lock_irqsave(&il->sta_lock, flags_spin);
  1916. il->stations[i].used &= ~IL_STA_UCODE_INPROGRESS;
  1917. }
  1918. }
  1919. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  1920. if (!found)
  1921. D_INFO("Restoring all known stations"
  1922. " .... no stations to be restored.\n");
  1923. else
  1924. D_INFO("Restoring all known stations" " .... complete.\n");
  1925. }
  1926. EXPORT_SYMBOL(il_restore_stations);
  1927. int
  1928. il_get_free_ucode_key_idx(struct il_priv *il)
  1929. {
  1930. int i;
  1931. for (i = 0; i < il->sta_key_max_num; i++)
  1932. if (!test_and_set_bit(i, &il->ucode_key_table))
  1933. return i;
  1934. return WEP_INVALID_OFFSET;
  1935. }
  1936. EXPORT_SYMBOL(il_get_free_ucode_key_idx);
  1937. void
  1938. il_dealloc_bcast_stations(struct il_priv *il)
  1939. {
  1940. unsigned long flags;
  1941. int i;
  1942. spin_lock_irqsave(&il->sta_lock, flags);
  1943. for (i = 0; i < il->hw_params.max_stations; i++) {
  1944. if (!(il->stations[i].used & IL_STA_BCAST))
  1945. continue;
  1946. il->stations[i].used &= ~IL_STA_UCODE_ACTIVE;
  1947. il->num_stations--;
  1948. BUG_ON(il->num_stations < 0);
  1949. kfree(il->stations[i].lq);
  1950. il->stations[i].lq = NULL;
  1951. }
  1952. spin_unlock_irqrestore(&il->sta_lock, flags);
  1953. }
  1954. EXPORT_SYMBOL_GPL(il_dealloc_bcast_stations);
  1955. #ifdef CONFIG_IWLEGACY_DEBUG
  1956. static void
  1957. il_dump_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq)
  1958. {
  1959. int i;
  1960. D_RATE("lq station id 0x%x\n", lq->sta_id);
  1961. D_RATE("lq ant 0x%X 0x%X\n", lq->general_params.single_stream_ant_msk,
  1962. lq->general_params.dual_stream_ant_msk);
  1963. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  1964. D_RATE("lq idx %d 0x%X\n", i, lq->rs_table[i].rate_n_flags);
  1965. }
  1966. #else
  1967. static inline void
  1968. il_dump_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq)
  1969. {
  1970. }
  1971. #endif
  1972. /**
  1973. * il_is_lq_table_valid() - Test one aspect of LQ cmd for validity
  1974. *
  1975. * It sometimes happens when a HT rate has been in use and we
  1976. * loose connectivity with AP then mac80211 will first tell us that the
  1977. * current channel is not HT anymore before removing the station. In such a
  1978. * scenario the RXON flags will be updated to indicate we are not
  1979. * communicating HT anymore, but the LQ command may still contain HT rates.
  1980. * Test for this to prevent driver from sending LQ command between the time
  1981. * RXON flags are updated and when LQ command is updated.
  1982. */
  1983. static bool
  1984. il_is_lq_table_valid(struct il_priv *il, struct il_link_quality_cmd *lq)
  1985. {
  1986. int i;
  1987. if (il->ht.enabled)
  1988. return true;
  1989. D_INFO("Channel %u is not an HT channel\n", il->active.channel);
  1990. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  1991. if (le32_to_cpu(lq->rs_table[i].rate_n_flags) & RATE_MCS_HT_MSK) {
  1992. D_INFO("idx %d of LQ expects HT channel\n", i);
  1993. return false;
  1994. }
  1995. }
  1996. return true;
  1997. }
  1998. /**
  1999. * il_send_lq_cmd() - Send link quality command
  2000. * @init: This command is sent as part of station initialization right
  2001. * after station has been added.
  2002. *
  2003. * The link quality command is sent as the last step of station creation.
  2004. * This is the special case in which init is set and we call a callback in
  2005. * this case to clear the state indicating that station creation is in
  2006. * progress.
  2007. */
  2008. int
  2009. il_send_lq_cmd(struct il_priv *il, struct il_link_quality_cmd *lq,
  2010. u8 flags, bool init)
  2011. {
  2012. int ret = 0;
  2013. unsigned long flags_spin;
  2014. struct il_host_cmd cmd = {
  2015. .id = C_TX_LINK_QUALITY_CMD,
  2016. .len = sizeof(struct il_link_quality_cmd),
  2017. .flags = flags,
  2018. .data = lq,
  2019. };
  2020. if (WARN_ON(lq->sta_id == IL_INVALID_STATION))
  2021. return -EINVAL;
  2022. spin_lock_irqsave(&il->sta_lock, flags_spin);
  2023. if (!(il->stations[lq->sta_id].used & IL_STA_DRIVER_ACTIVE)) {
  2024. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2025. return -EINVAL;
  2026. }
  2027. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2028. il_dump_lq_cmd(il, lq);
  2029. BUG_ON(init && (cmd.flags & CMD_ASYNC));
  2030. if (il_is_lq_table_valid(il, lq))
  2031. ret = il_send_cmd(il, &cmd);
  2032. else
  2033. ret = -EINVAL;
  2034. if (cmd.flags & CMD_ASYNC)
  2035. return ret;
  2036. if (init) {
  2037. D_INFO("init LQ command complete,"
  2038. " clearing sta addition status for sta %d\n",
  2039. lq->sta_id);
  2040. spin_lock_irqsave(&il->sta_lock, flags_spin);
  2041. il->stations[lq->sta_id].used &= ~IL_STA_UCODE_INPROGRESS;
  2042. spin_unlock_irqrestore(&il->sta_lock, flags_spin);
  2043. }
  2044. return ret;
  2045. }
  2046. EXPORT_SYMBOL(il_send_lq_cmd);
  2047. int
  2048. il_mac_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2049. struct ieee80211_sta *sta)
  2050. {
  2051. struct il_priv *il = hw->priv;
  2052. struct il_station_priv_common *sta_common = (void *)sta->drv_priv;
  2053. int ret;
  2054. mutex_lock(&il->mutex);
  2055. D_MAC80211("enter 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. D_MAC80211("leave ret %d\n", ret);
  2060. mutex_unlock(&il->mutex);
  2061. return ret;
  2062. }
  2063. EXPORT_SYMBOL(il_mac_sta_remove);
  2064. /************************** RX-FUNCTIONS ****************************/
  2065. /*
  2066. * Rx theory of operation
  2067. *
  2068. * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
  2069. * each of which point to Receive Buffers to be filled by the NIC. These get
  2070. * used not only for Rx frames, but for any command response or notification
  2071. * from the NIC. The driver and NIC manage the Rx buffers by means
  2072. * of idxes into the circular buffer.
  2073. *
  2074. * Rx Queue Indexes
  2075. * The host/firmware share two idx registers for managing the Rx buffers.
  2076. *
  2077. * The READ idx maps to the first position that the firmware may be writing
  2078. * to -- the driver can read up to (but not including) this position and get
  2079. * good data.
  2080. * The READ idx is managed by the firmware once the card is enabled.
  2081. *
  2082. * The WRITE idx maps to the last position the driver has read from -- the
  2083. * position preceding WRITE is the last slot the firmware can place a packet.
  2084. *
  2085. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  2086. * WRITE = READ.
  2087. *
  2088. * During initialization, the host sets up the READ queue position to the first
  2089. * IDX position, and WRITE to the last (READ - 1 wrapped)
  2090. *
  2091. * When the firmware places a packet in a buffer, it will advance the READ idx
  2092. * and fire the RX interrupt. The driver can then query the READ idx and
  2093. * process as many packets as possible, moving the WRITE idx forward as it
  2094. * resets the Rx queue buffers with new memory.
  2095. *
  2096. * The management in the driver is as follows:
  2097. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  2098. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  2099. * to replenish the iwl->rxq->rx_free.
  2100. * + In il_rx_replenish (scheduled) if 'processed' != 'read' then the
  2101. * iwl->rxq is replenished and the READ IDX is updated (updating the
  2102. * 'processed' and 'read' driver idxes as well)
  2103. * + A received packet is processed and handed to the kernel network stack,
  2104. * detached from the iwl->rxq. The driver 'processed' idx is updated.
  2105. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  2106. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  2107. * IDX is not incremented and iwl->status(RX_STALLED) is set. If there
  2108. * were enough free buffers and RX_STALLED is set it is cleared.
  2109. *
  2110. *
  2111. * Driver sequence:
  2112. *
  2113. * il_rx_queue_alloc() Allocates rx_free
  2114. * il_rx_replenish() Replenishes rx_free list from rx_used, and calls
  2115. * il_rx_queue_restock
  2116. * il_rx_queue_restock() Moves available buffers from rx_free into Rx
  2117. * queue, updates firmware pointers, and updates
  2118. * the WRITE idx. If insufficient rx_free buffers
  2119. * are available, schedules il_rx_replenish
  2120. *
  2121. * -- enable interrupts --
  2122. * ISR - il_rx() Detach il_rx_bufs from pool up to the
  2123. * READ IDX, detaching the SKB from the pool.
  2124. * Moves the packet buffer from queue to rx_used.
  2125. * Calls il_rx_queue_restock to refill any empty
  2126. * slots.
  2127. * ...
  2128. *
  2129. */
  2130. /**
  2131. * il_rx_queue_space - Return number of free slots available in queue.
  2132. */
  2133. int
  2134. il_rx_queue_space(const struct il_rx_queue *q)
  2135. {
  2136. int s = q->read - q->write;
  2137. if (s <= 0)
  2138. s += RX_QUEUE_SIZE;
  2139. /* keep some buffer to not confuse full and empty queue */
  2140. s -= 2;
  2141. if (s < 0)
  2142. s = 0;
  2143. return s;
  2144. }
  2145. EXPORT_SYMBOL(il_rx_queue_space);
  2146. /**
  2147. * il_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  2148. */
  2149. void
  2150. il_rx_queue_update_write_ptr(struct il_priv *il, struct il_rx_queue *q)
  2151. {
  2152. unsigned long flags;
  2153. u32 rx_wrt_ptr_reg = il->hw_params.rx_wrt_ptr_reg;
  2154. u32 reg;
  2155. spin_lock_irqsave(&q->lock, flags);
  2156. if (q->need_update == 0)
  2157. goto exit_unlock;
  2158. /* If power-saving is in use, make sure device is awake */
  2159. if (test_bit(S_POWER_PMI, &il->status)) {
  2160. reg = _il_rd(il, CSR_UCODE_DRV_GP1);
  2161. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2162. D_INFO("Rx queue requesting wakeup," " GP1 = 0x%x\n",
  2163. reg);
  2164. il_set_bit(il, CSR_GP_CNTRL,
  2165. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2166. goto exit_unlock;
  2167. }
  2168. q->write_actual = (q->write & ~0x7);
  2169. il_wr(il, rx_wrt_ptr_reg, q->write_actual);
  2170. /* Else device is assumed to be awake */
  2171. } else {
  2172. /* Device expects a multiple of 8 */
  2173. q->write_actual = (q->write & ~0x7);
  2174. il_wr(il, rx_wrt_ptr_reg, q->write_actual);
  2175. }
  2176. q->need_update = 0;
  2177. exit_unlock:
  2178. spin_unlock_irqrestore(&q->lock, flags);
  2179. }
  2180. EXPORT_SYMBOL(il_rx_queue_update_write_ptr);
  2181. int
  2182. il_rx_queue_alloc(struct il_priv *il)
  2183. {
  2184. struct il_rx_queue *rxq = &il->rxq;
  2185. struct device *dev = &il->pci_dev->dev;
  2186. int i;
  2187. spin_lock_init(&rxq->lock);
  2188. INIT_LIST_HEAD(&rxq->rx_free);
  2189. INIT_LIST_HEAD(&rxq->rx_used);
  2190. /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
  2191. rxq->bd =
  2192. dma_alloc_coherent(dev, 4 * RX_QUEUE_SIZE, &rxq->bd_dma,
  2193. GFP_KERNEL);
  2194. if (!rxq->bd)
  2195. goto err_bd;
  2196. rxq->rb_stts =
  2197. dma_alloc_coherent(dev, sizeof(struct il_rb_status),
  2198. &rxq->rb_stts_dma, GFP_KERNEL);
  2199. if (!rxq->rb_stts)
  2200. goto err_rb;
  2201. /* Fill the rx_used queue with _all_ of the Rx buffers */
  2202. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  2203. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  2204. /* Set us so that we have processed and used all buffers, but have
  2205. * not restocked the Rx queue with fresh buffers */
  2206. rxq->read = rxq->write = 0;
  2207. rxq->write_actual = 0;
  2208. rxq->free_count = 0;
  2209. rxq->need_update = 0;
  2210. return 0;
  2211. err_rb:
  2212. dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  2213. rxq->bd_dma);
  2214. err_bd:
  2215. return -ENOMEM;
  2216. }
  2217. EXPORT_SYMBOL(il_rx_queue_alloc);
  2218. void
  2219. il_hdl_spectrum_measurement(struct il_priv *il, struct il_rx_buf *rxb)
  2220. {
  2221. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2222. struct il_spectrum_notification *report = &(pkt->u.spectrum_notif);
  2223. if (!report->state) {
  2224. D_11H("Spectrum Measure Notification: Start\n");
  2225. return;
  2226. }
  2227. memcpy(&il->measure_report, report, sizeof(*report));
  2228. il->measurement_status |= MEASUREMENT_READY;
  2229. }
  2230. EXPORT_SYMBOL(il_hdl_spectrum_measurement);
  2231. /*
  2232. * returns non-zero if packet should be dropped
  2233. */
  2234. int
  2235. il_set_decrypted_flag(struct il_priv *il, struct ieee80211_hdr *hdr,
  2236. u32 decrypt_res, struct ieee80211_rx_status *stats)
  2237. {
  2238. u16 fc = le16_to_cpu(hdr->frame_control);
  2239. /*
  2240. * All contexts have the same setting here due to it being
  2241. * a module parameter, so OK to check any context.
  2242. */
  2243. if (il->active.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  2244. return 0;
  2245. if (!(fc & IEEE80211_FCTL_PROTECTED))
  2246. return 0;
  2247. D_RX("decrypt_res:0x%x\n", decrypt_res);
  2248. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  2249. case RX_RES_STATUS_SEC_TYPE_TKIP:
  2250. /* The uCode has got a bad phase 1 Key, pushes the packet.
  2251. * Decryption will be done in SW. */
  2252. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2253. RX_RES_STATUS_BAD_KEY_TTAK)
  2254. break;
  2255. case RX_RES_STATUS_SEC_TYPE_WEP:
  2256. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2257. RX_RES_STATUS_BAD_ICV_MIC) {
  2258. /* bad ICV, the packet is destroyed since the
  2259. * decryption is inplace, drop it */
  2260. D_RX("Packet destroyed\n");
  2261. return -1;
  2262. }
  2263. case RX_RES_STATUS_SEC_TYPE_CCMP:
  2264. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2265. RX_RES_STATUS_DECRYPT_OK) {
  2266. D_RX("hw decrypt successfully!!!\n");
  2267. stats->flag |= RX_FLAG_DECRYPTED;
  2268. }
  2269. break;
  2270. default:
  2271. break;
  2272. }
  2273. return 0;
  2274. }
  2275. EXPORT_SYMBOL(il_set_decrypted_flag);
  2276. /**
  2277. * il_txq_update_write_ptr - Send new write idx to hardware
  2278. */
  2279. void
  2280. il_txq_update_write_ptr(struct il_priv *il, struct il_tx_queue *txq)
  2281. {
  2282. u32 reg = 0;
  2283. int txq_id = txq->q.id;
  2284. if (txq->need_update == 0)
  2285. return;
  2286. /* if we're trying to save power */
  2287. if (test_bit(S_POWER_PMI, &il->status)) {
  2288. /* wake up nic if it's powered down ...
  2289. * uCode will wake up, and interrupt us again, so next
  2290. * time we'll skip this part. */
  2291. reg = _il_rd(il, CSR_UCODE_DRV_GP1);
  2292. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2293. D_INFO("Tx queue %d requesting wakeup," " GP1 = 0x%x\n",
  2294. txq_id, reg);
  2295. il_set_bit(il, CSR_GP_CNTRL,
  2296. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2297. return;
  2298. }
  2299. il_wr(il, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
  2300. /*
  2301. * else not in power-save mode,
  2302. * uCode will never sleep when we're
  2303. * trying to tx (during RFKILL, we're not trying to tx).
  2304. */
  2305. } else
  2306. _il_wr(il, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
  2307. txq->need_update = 0;
  2308. }
  2309. EXPORT_SYMBOL(il_txq_update_write_ptr);
  2310. /**
  2311. * il_tx_queue_unmap - Unmap any remaining DMA mappings and free skb's
  2312. */
  2313. void
  2314. il_tx_queue_unmap(struct il_priv *il, int txq_id)
  2315. {
  2316. struct il_tx_queue *txq = &il->txq[txq_id];
  2317. struct il_queue *q = &txq->q;
  2318. if (q->n_bd == 0)
  2319. return;
  2320. while (q->write_ptr != q->read_ptr) {
  2321. il->ops->txq_free_tfd(il, txq);
  2322. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
  2323. }
  2324. }
  2325. EXPORT_SYMBOL(il_tx_queue_unmap);
  2326. /**
  2327. * il_tx_queue_free - Deallocate DMA queue.
  2328. * @txq: Transmit queue to deallocate.
  2329. *
  2330. * Empty queue by removing and destroying all BD's.
  2331. * Free all buffers.
  2332. * 0-fill, but do not free "txq" descriptor structure.
  2333. */
  2334. void
  2335. il_tx_queue_free(struct il_priv *il, int txq_id)
  2336. {
  2337. struct il_tx_queue *txq = &il->txq[txq_id];
  2338. struct device *dev = &il->pci_dev->dev;
  2339. int i;
  2340. il_tx_queue_unmap(il, txq_id);
  2341. /* De-alloc array of command/tx buffers */
  2342. for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
  2343. kfree(txq->cmd[i]);
  2344. /* De-alloc circular buffer of TFDs */
  2345. if (txq->q.n_bd)
  2346. dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
  2347. txq->tfds, txq->q.dma_addr);
  2348. /* De-alloc array of per-TFD driver data */
  2349. kfree(txq->skbs);
  2350. txq->skbs = NULL;
  2351. /* deallocate arrays */
  2352. kfree(txq->cmd);
  2353. kfree(txq->meta);
  2354. txq->cmd = NULL;
  2355. txq->meta = NULL;
  2356. /* 0-fill queue descriptor structure */
  2357. memset(txq, 0, sizeof(*txq));
  2358. }
  2359. EXPORT_SYMBOL(il_tx_queue_free);
  2360. /**
  2361. * il_cmd_queue_unmap - Unmap any remaining DMA mappings from command queue
  2362. */
  2363. void
  2364. il_cmd_queue_unmap(struct il_priv *il)
  2365. {
  2366. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2367. struct il_queue *q = &txq->q;
  2368. int i;
  2369. if (q->n_bd == 0)
  2370. return;
  2371. while (q->read_ptr != q->write_ptr) {
  2372. i = il_get_cmd_idx(q, q->read_ptr, 0);
  2373. if (txq->meta[i].flags & CMD_MAPPED) {
  2374. pci_unmap_single(il->pci_dev,
  2375. dma_unmap_addr(&txq->meta[i], mapping),
  2376. dma_unmap_len(&txq->meta[i], len),
  2377. PCI_DMA_BIDIRECTIONAL);
  2378. txq->meta[i].flags = 0;
  2379. }
  2380. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd);
  2381. }
  2382. i = q->n_win;
  2383. if (txq->meta[i].flags & CMD_MAPPED) {
  2384. pci_unmap_single(il->pci_dev,
  2385. dma_unmap_addr(&txq->meta[i], mapping),
  2386. dma_unmap_len(&txq->meta[i], len),
  2387. PCI_DMA_BIDIRECTIONAL);
  2388. txq->meta[i].flags = 0;
  2389. }
  2390. }
  2391. EXPORT_SYMBOL(il_cmd_queue_unmap);
  2392. /**
  2393. * il_cmd_queue_free - Deallocate DMA queue.
  2394. * @txq: Transmit queue to deallocate.
  2395. *
  2396. * Empty queue by removing and destroying all BD's.
  2397. * Free all buffers.
  2398. * 0-fill, but do not free "txq" descriptor structure.
  2399. */
  2400. void
  2401. il_cmd_queue_free(struct il_priv *il)
  2402. {
  2403. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2404. struct device *dev = &il->pci_dev->dev;
  2405. int i;
  2406. il_cmd_queue_unmap(il);
  2407. /* De-alloc array of command/tx buffers */
  2408. for (i = 0; i <= TFD_CMD_SLOTS; i++)
  2409. kfree(txq->cmd[i]);
  2410. /* De-alloc circular buffer of TFDs */
  2411. if (txq->q.n_bd)
  2412. dma_free_coherent(dev, il->hw_params.tfd_size * txq->q.n_bd,
  2413. txq->tfds, txq->q.dma_addr);
  2414. /* deallocate arrays */
  2415. kfree(txq->cmd);
  2416. kfree(txq->meta);
  2417. txq->cmd = NULL;
  2418. txq->meta = NULL;
  2419. /* 0-fill queue descriptor structure */
  2420. memset(txq, 0, sizeof(*txq));
  2421. }
  2422. EXPORT_SYMBOL(il_cmd_queue_free);
  2423. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  2424. * DMA services
  2425. *
  2426. * Theory of operation
  2427. *
  2428. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  2429. * of buffer descriptors, each of which points to one or more data buffers for
  2430. * the device to read from or fill. Driver and device exchange status of each
  2431. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  2432. * entries in each circular buffer, to protect against confusing empty and full
  2433. * queue states.
  2434. *
  2435. * The device reads or writes the data in the queues via the device's several
  2436. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  2437. *
  2438. * For Tx queue, there are low mark and high mark limits. If, after queuing
  2439. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  2440. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  2441. * Tx queue resumed.
  2442. *
  2443. * See more detailed info in 4965.h.
  2444. ***************************************************/
  2445. int
  2446. il_queue_space(const struct il_queue *q)
  2447. {
  2448. int s = q->read_ptr - q->write_ptr;
  2449. if (q->read_ptr > q->write_ptr)
  2450. s -= q->n_bd;
  2451. if (s <= 0)
  2452. s += q->n_win;
  2453. /* keep some reserve to not confuse empty and full situations */
  2454. s -= 2;
  2455. if (s < 0)
  2456. s = 0;
  2457. return s;
  2458. }
  2459. EXPORT_SYMBOL(il_queue_space);
  2460. /**
  2461. * il_queue_init - Initialize queue's high/low-water and read/write idxes
  2462. */
  2463. static int
  2464. il_queue_init(struct il_priv *il, struct il_queue *q, int slots, u32 id)
  2465. {
  2466. /*
  2467. * TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  2468. * il_queue_inc_wrap and il_queue_dec_wrap are broken.
  2469. */
  2470. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  2471. /* FIXME: remove q->n_bd */
  2472. q->n_bd = TFD_QUEUE_SIZE_MAX;
  2473. q->n_win = slots;
  2474. q->id = id;
  2475. /* slots_must be power-of-two size, otherwise
  2476. * il_get_cmd_idx is broken. */
  2477. BUG_ON(!is_power_of_2(slots));
  2478. q->low_mark = q->n_win / 4;
  2479. if (q->low_mark < 4)
  2480. q->low_mark = 4;
  2481. q->high_mark = q->n_win / 8;
  2482. if (q->high_mark < 2)
  2483. q->high_mark = 2;
  2484. q->write_ptr = q->read_ptr = 0;
  2485. return 0;
  2486. }
  2487. /**
  2488. * il_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  2489. */
  2490. static int
  2491. il_tx_queue_alloc(struct il_priv *il, struct il_tx_queue *txq, u32 id)
  2492. {
  2493. struct device *dev = &il->pci_dev->dev;
  2494. size_t tfd_sz = il->hw_params.tfd_size * TFD_QUEUE_SIZE_MAX;
  2495. /* Driver ilate data, only for Tx (not command) queues,
  2496. * not shared with device. */
  2497. if (id != il->cmd_queue) {
  2498. txq->skbs = kcalloc(TFD_QUEUE_SIZE_MAX, sizeof(struct skb *),
  2499. GFP_KERNEL);
  2500. if (!txq->skbs) {
  2501. IL_ERR("Fail to alloc skbs\n");
  2502. goto error;
  2503. }
  2504. } else
  2505. txq->skbs = NULL;
  2506. /* Circular buffer of transmit frame descriptors (TFDs),
  2507. * shared with device */
  2508. txq->tfds =
  2509. dma_alloc_coherent(dev, tfd_sz, &txq->q.dma_addr, GFP_KERNEL);
  2510. if (!txq->tfds) {
  2511. IL_ERR("Fail to alloc TFDs\n");
  2512. goto error;
  2513. }
  2514. txq->q.id = id;
  2515. return 0;
  2516. error:
  2517. kfree(txq->skbs);
  2518. txq->skbs = NULL;
  2519. return -ENOMEM;
  2520. }
  2521. /**
  2522. * il_tx_queue_init - Allocate and initialize one tx/cmd queue
  2523. */
  2524. int
  2525. il_tx_queue_init(struct il_priv *il, u32 txq_id)
  2526. {
  2527. int i, len, ret;
  2528. int slots, actual_slots;
  2529. struct il_tx_queue *txq = &il->txq[txq_id];
  2530. /*
  2531. * Alloc buffer array for commands (Tx or other types of commands).
  2532. * For the command queue (#4/#9), allocate command space + one big
  2533. * command for scan, since scan command is very huge; the system will
  2534. * not have two scans at the same time, so only one is needed.
  2535. * For normal Tx queues (all other queues), no super-size command
  2536. * space is needed.
  2537. */
  2538. if (txq_id == il->cmd_queue) {
  2539. slots = TFD_CMD_SLOTS;
  2540. actual_slots = slots + 1;
  2541. } else {
  2542. slots = TFD_TX_CMD_SLOTS;
  2543. actual_slots = slots;
  2544. }
  2545. txq->meta =
  2546. kzalloc(sizeof(struct il_cmd_meta) * actual_slots, GFP_KERNEL);
  2547. txq->cmd =
  2548. kzalloc(sizeof(struct il_device_cmd *) * actual_slots, GFP_KERNEL);
  2549. if (!txq->meta || !txq->cmd)
  2550. goto out_free_arrays;
  2551. len = sizeof(struct il_device_cmd);
  2552. for (i = 0; i < actual_slots; i++) {
  2553. /* only happens for cmd queue */
  2554. if (i == slots)
  2555. len = IL_MAX_CMD_SIZE;
  2556. txq->cmd[i] = kmalloc(len, GFP_KERNEL);
  2557. if (!txq->cmd[i])
  2558. goto err;
  2559. }
  2560. /* Alloc driver data array and TFD circular buffer */
  2561. ret = il_tx_queue_alloc(il, txq, txq_id);
  2562. if (ret)
  2563. goto err;
  2564. txq->need_update = 0;
  2565. /*
  2566. * For the default queues 0-3, set up the swq_id
  2567. * already -- all others need to get one later
  2568. * (if they need one at all).
  2569. */
  2570. if (txq_id < 4)
  2571. il_set_swq_id(txq, txq_id, txq_id);
  2572. /* Initialize queue's high/low-water marks, and head/tail idxes */
  2573. il_queue_init(il, &txq->q, slots, txq_id);
  2574. /* Tell device where to find queue */
  2575. il->ops->txq_init(il, txq);
  2576. return 0;
  2577. err:
  2578. for (i = 0; i < actual_slots; i++)
  2579. kfree(txq->cmd[i]);
  2580. out_free_arrays:
  2581. kfree(txq->meta);
  2582. kfree(txq->cmd);
  2583. return -ENOMEM;
  2584. }
  2585. EXPORT_SYMBOL(il_tx_queue_init);
  2586. void
  2587. il_tx_queue_reset(struct il_priv *il, u32 txq_id)
  2588. {
  2589. int slots, actual_slots;
  2590. struct il_tx_queue *txq = &il->txq[txq_id];
  2591. if (txq_id == il->cmd_queue) {
  2592. slots = TFD_CMD_SLOTS;
  2593. actual_slots = TFD_CMD_SLOTS + 1;
  2594. } else {
  2595. slots = TFD_TX_CMD_SLOTS;
  2596. actual_slots = TFD_TX_CMD_SLOTS;
  2597. }
  2598. memset(txq->meta, 0, sizeof(struct il_cmd_meta) * actual_slots);
  2599. txq->need_update = 0;
  2600. /* Initialize queue's high/low-water marks, and head/tail idxes */
  2601. il_queue_init(il, &txq->q, slots, txq_id);
  2602. /* Tell device where to find queue */
  2603. il->ops->txq_init(il, txq);
  2604. }
  2605. EXPORT_SYMBOL(il_tx_queue_reset);
  2606. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  2607. /**
  2608. * il_enqueue_hcmd - enqueue a uCode command
  2609. * @il: device ilate data point
  2610. * @cmd: a point to the ucode command structure
  2611. *
  2612. * The function returns < 0 values to indicate the operation is
  2613. * failed. On success, it turns the idx (> 0) of command in the
  2614. * command queue.
  2615. */
  2616. int
  2617. il_enqueue_hcmd(struct il_priv *il, struct il_host_cmd *cmd)
  2618. {
  2619. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2620. struct il_queue *q = &txq->q;
  2621. struct il_device_cmd *out_cmd;
  2622. struct il_cmd_meta *out_meta;
  2623. dma_addr_t phys_addr;
  2624. unsigned long flags;
  2625. int len;
  2626. u32 idx;
  2627. u16 fix_size;
  2628. cmd->len = il->ops->get_hcmd_size(cmd->id, cmd->len);
  2629. fix_size = (u16) (cmd->len + sizeof(out_cmd->hdr));
  2630. /* If any of the command structures end up being larger than
  2631. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  2632. * we will need to increase the size of the TFD entries
  2633. * Also, check to see if command buffer should not exceed the size
  2634. * of device_cmd and max_cmd_size. */
  2635. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  2636. !(cmd->flags & CMD_SIZE_HUGE));
  2637. BUG_ON(fix_size > IL_MAX_CMD_SIZE);
  2638. if (il_is_rfkill(il) || il_is_ctkill(il)) {
  2639. IL_WARN("Not sending command - %s KILL\n",
  2640. il_is_rfkill(il) ? "RF" : "CT");
  2641. return -EIO;
  2642. }
  2643. spin_lock_irqsave(&il->hcmd_lock, flags);
  2644. if (il_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  2645. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2646. IL_ERR("Restarting adapter due to command queue full\n");
  2647. queue_work(il->workqueue, &il->restart);
  2648. return -ENOSPC;
  2649. }
  2650. idx = il_get_cmd_idx(q, q->write_ptr, cmd->flags & CMD_SIZE_HUGE);
  2651. out_cmd = txq->cmd[idx];
  2652. out_meta = &txq->meta[idx];
  2653. if (WARN_ON(out_meta->flags & CMD_MAPPED)) {
  2654. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2655. return -ENOSPC;
  2656. }
  2657. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  2658. out_meta->flags = cmd->flags | CMD_MAPPED;
  2659. if (cmd->flags & CMD_WANT_SKB)
  2660. out_meta->source = cmd;
  2661. if (cmd->flags & CMD_ASYNC)
  2662. out_meta->callback = cmd->callback;
  2663. out_cmd->hdr.cmd = cmd->id;
  2664. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  2665. /* At this point, the out_cmd now has all of the incoming cmd
  2666. * information */
  2667. out_cmd->hdr.flags = 0;
  2668. out_cmd->hdr.sequence =
  2669. cpu_to_le16(QUEUE_TO_SEQ(il->cmd_queue) | IDX_TO_SEQ(q->write_ptr));
  2670. if (cmd->flags & CMD_SIZE_HUGE)
  2671. out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
  2672. len = sizeof(struct il_device_cmd);
  2673. if (idx == TFD_CMD_SLOTS)
  2674. len = IL_MAX_CMD_SIZE;
  2675. #ifdef CONFIG_IWLEGACY_DEBUG
  2676. switch (out_cmd->hdr.cmd) {
  2677. case C_TX_LINK_QUALITY_CMD:
  2678. case C_SENSITIVITY:
  2679. D_HC_DUMP("Sending command %s (#%x), seq: 0x%04X, "
  2680. "%d bytes at %d[%d]:%d\n",
  2681. il_get_cmd_string(out_cmd->hdr.cmd), out_cmd->hdr.cmd,
  2682. le16_to_cpu(out_cmd->hdr.sequence), fix_size,
  2683. q->write_ptr, idx, il->cmd_queue);
  2684. break;
  2685. default:
  2686. D_HC("Sending command %s (#%x), seq: 0x%04X, "
  2687. "%d bytes at %d[%d]:%d\n",
  2688. il_get_cmd_string(out_cmd->hdr.cmd), out_cmd->hdr.cmd,
  2689. le16_to_cpu(out_cmd->hdr.sequence), fix_size, q->write_ptr,
  2690. idx, il->cmd_queue);
  2691. }
  2692. #endif
  2693. txq->need_update = 1;
  2694. if (il->ops->txq_update_byte_cnt_tbl)
  2695. /* Set up entry in queue's byte count circular buffer */
  2696. il->ops->txq_update_byte_cnt_tbl(il, txq, 0);
  2697. phys_addr =
  2698. pci_map_single(il->pci_dev, &out_cmd->hdr, fix_size,
  2699. PCI_DMA_BIDIRECTIONAL);
  2700. dma_unmap_addr_set(out_meta, mapping, phys_addr);
  2701. dma_unmap_len_set(out_meta, len, fix_size);
  2702. il->ops->txq_attach_buf_to_tfd(il, txq, phys_addr, fix_size, 1,
  2703. U32_PAD(cmd->len));
  2704. /* Increment and update queue's write idx */
  2705. q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
  2706. il_txq_update_write_ptr(il, txq);
  2707. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2708. return idx;
  2709. }
  2710. /**
  2711. * il_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  2712. *
  2713. * When FW advances 'R' idx, all entries between old and new 'R' idx
  2714. * need to be reclaimed. As result, some free space forms. If there is
  2715. * enough free space (> low mark), wake the stack that feeds us.
  2716. */
  2717. static void
  2718. il_hcmd_queue_reclaim(struct il_priv *il, int txq_id, int idx, int cmd_idx)
  2719. {
  2720. struct il_tx_queue *txq = &il->txq[txq_id];
  2721. struct il_queue *q = &txq->q;
  2722. int nfreed = 0;
  2723. if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
  2724. IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
  2725. "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
  2726. q->write_ptr, q->read_ptr);
  2727. return;
  2728. }
  2729. for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  2730. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2731. if (nfreed++ > 0) {
  2732. IL_ERR("HCMD skipped: idx (%d) %d %d\n", idx,
  2733. q->write_ptr, q->read_ptr);
  2734. queue_work(il->workqueue, &il->restart);
  2735. }
  2736. }
  2737. }
  2738. /**
  2739. * il_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  2740. * @rxb: Rx buffer to reclaim
  2741. *
  2742. * If an Rx buffer has an async callback associated with it the callback
  2743. * will be executed. The attached skb (if present) will only be freed
  2744. * if the callback returns 1
  2745. */
  2746. void
  2747. il_tx_cmd_complete(struct il_priv *il, struct il_rx_buf *rxb)
  2748. {
  2749. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2750. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2751. int txq_id = SEQ_TO_QUEUE(sequence);
  2752. int idx = SEQ_TO_IDX(sequence);
  2753. int cmd_idx;
  2754. bool huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
  2755. struct il_device_cmd *cmd;
  2756. struct il_cmd_meta *meta;
  2757. struct il_tx_queue *txq = &il->txq[il->cmd_queue];
  2758. unsigned long flags;
  2759. /* If a Tx command is being handled and it isn't in the actual
  2760. * command queue then there a command routing bug has been introduced
  2761. * in the queue management code. */
  2762. if (WARN
  2763. (txq_id != il->cmd_queue,
  2764. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  2765. txq_id, il->cmd_queue, sequence, il->txq[il->cmd_queue].q.read_ptr,
  2766. il->txq[il->cmd_queue].q.write_ptr)) {
  2767. il_print_hex_error(il, pkt, 32);
  2768. return;
  2769. }
  2770. cmd_idx = il_get_cmd_idx(&txq->q, idx, huge);
  2771. cmd = txq->cmd[cmd_idx];
  2772. meta = &txq->meta[cmd_idx];
  2773. txq->time_stamp = jiffies;
  2774. pci_unmap_single(il->pci_dev, dma_unmap_addr(meta, mapping),
  2775. dma_unmap_len(meta, len), PCI_DMA_BIDIRECTIONAL);
  2776. /* Input error checking is done when commands are added to queue. */
  2777. if (meta->flags & CMD_WANT_SKB) {
  2778. meta->source->reply_page = (unsigned long)rxb_addr(rxb);
  2779. rxb->page = NULL;
  2780. } else if (meta->callback)
  2781. meta->callback(il, cmd, pkt);
  2782. spin_lock_irqsave(&il->hcmd_lock, flags);
  2783. il_hcmd_queue_reclaim(il, txq_id, idx, cmd_idx);
  2784. if (!(meta->flags & CMD_ASYNC)) {
  2785. clear_bit(S_HCMD_ACTIVE, &il->status);
  2786. D_INFO("Clearing HCMD_ACTIVE for command %s\n",
  2787. il_get_cmd_string(cmd->hdr.cmd));
  2788. wake_up(&il->wait_command_queue);
  2789. }
  2790. /* Mark as unmapped */
  2791. meta->flags = 0;
  2792. spin_unlock_irqrestore(&il->hcmd_lock, flags);
  2793. }
  2794. EXPORT_SYMBOL(il_tx_cmd_complete);
  2795. MODULE_DESCRIPTION("iwl-legacy: common functions for 3945 and 4965");
  2796. MODULE_VERSION(IWLWIFI_VERSION);
  2797. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  2798. MODULE_LICENSE("GPL");
  2799. /*
  2800. * set bt_coex_active to true, uCode will do kill/defer
  2801. * every time the priority line is asserted (BT is sending signals on the
  2802. * priority line in the PCIx).
  2803. * set bt_coex_active to false, uCode will ignore the BT activity and
  2804. * perform the normal operation
  2805. *
  2806. * User might experience transmit issue on some platform due to WiFi/BT
  2807. * co-exist problem. The possible behaviors are:
  2808. * Able to scan and finding all the available AP
  2809. * Not able to associate with any AP
  2810. * On those platforms, WiFi communication can be restored by set
  2811. * "bt_coex_active" module parameter to "false"
  2812. *
  2813. * default: bt_coex_active = true (BT_COEX_ENABLE)
  2814. */
  2815. static bool bt_coex_active = true;
  2816. module_param(bt_coex_active, bool, S_IRUGO);
  2817. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
  2818. u32 il_debug_level;
  2819. EXPORT_SYMBOL(il_debug_level);
  2820. const u8 il_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  2821. EXPORT_SYMBOL(il_bcast_addr);
  2822. #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
  2823. #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
  2824. static void
  2825. il_init_ht_hw_capab(const struct il_priv *il,
  2826. struct ieee80211_sta_ht_cap *ht_info,
  2827. enum ieee80211_band band)
  2828. {
  2829. u16 max_bit_rate = 0;
  2830. u8 rx_chains_num = il->hw_params.rx_chains_num;
  2831. u8 tx_chains_num = il->hw_params.tx_chains_num;
  2832. ht_info->cap = 0;
  2833. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  2834. ht_info->ht_supported = true;
  2835. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  2836. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  2837. if (il->hw_params.ht40_channel & BIT(band)) {
  2838. ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  2839. ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
  2840. ht_info->mcs.rx_mask[4] = 0x01;
  2841. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  2842. }
  2843. if (il->cfg->mod_params->amsdu_size_8K)
  2844. ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  2845. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  2846. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  2847. ht_info->mcs.rx_mask[0] = 0xFF;
  2848. if (rx_chains_num >= 2)
  2849. ht_info->mcs.rx_mask[1] = 0xFF;
  2850. if (rx_chains_num >= 3)
  2851. ht_info->mcs.rx_mask[2] = 0xFF;
  2852. /* Highest supported Rx data rate */
  2853. max_bit_rate *= rx_chains_num;
  2854. WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
  2855. ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
  2856. /* Tx MCS capabilities */
  2857. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  2858. if (tx_chains_num != rx_chains_num) {
  2859. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  2860. ht_info->mcs.tx_params |=
  2861. ((tx_chains_num -
  2862. 1) << IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  2863. }
  2864. }
  2865. /**
  2866. * il_init_geos - Initialize mac80211's geo/channel info based from eeprom
  2867. */
  2868. int
  2869. il_init_geos(struct il_priv *il)
  2870. {
  2871. struct il_channel_info *ch;
  2872. struct ieee80211_supported_band *sband;
  2873. struct ieee80211_channel *channels;
  2874. struct ieee80211_channel *geo_ch;
  2875. struct ieee80211_rate *rates;
  2876. int i = 0;
  2877. s8 max_tx_power = 0;
  2878. if (il->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  2879. il->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  2880. D_INFO("Geography modes already initialized.\n");
  2881. set_bit(S_GEO_CONFIGURED, &il->status);
  2882. return 0;
  2883. }
  2884. channels =
  2885. kzalloc(sizeof(struct ieee80211_channel) * il->channel_count,
  2886. GFP_KERNEL);
  2887. if (!channels)
  2888. return -ENOMEM;
  2889. rates =
  2890. kzalloc((sizeof(struct ieee80211_rate) * RATE_COUNT_LEGACY),
  2891. GFP_KERNEL);
  2892. if (!rates) {
  2893. kfree(channels);
  2894. return -ENOMEM;
  2895. }
  2896. /* 5.2GHz channels start after the 2.4GHz channels */
  2897. sband = &il->bands[IEEE80211_BAND_5GHZ];
  2898. sband->channels = &channels[ARRAY_SIZE(il_eeprom_band_1)];
  2899. /* just OFDM */
  2900. sband->bitrates = &rates[IL_FIRST_OFDM_RATE];
  2901. sband->n_bitrates = RATE_COUNT_LEGACY - IL_FIRST_OFDM_RATE;
  2902. if (il->cfg->sku & IL_SKU_N)
  2903. il_init_ht_hw_capab(il, &sband->ht_cap, IEEE80211_BAND_5GHZ);
  2904. sband = &il->bands[IEEE80211_BAND_2GHZ];
  2905. sband->channels = channels;
  2906. /* OFDM & CCK */
  2907. sband->bitrates = rates;
  2908. sband->n_bitrates = RATE_COUNT_LEGACY;
  2909. if (il->cfg->sku & IL_SKU_N)
  2910. il_init_ht_hw_capab(il, &sband->ht_cap, IEEE80211_BAND_2GHZ);
  2911. il->ieee_channels = channels;
  2912. il->ieee_rates = rates;
  2913. for (i = 0; i < il->channel_count; i++) {
  2914. ch = &il->channel_info[i];
  2915. if (!il_is_channel_valid(ch))
  2916. continue;
  2917. sband = &il->bands[ch->band];
  2918. geo_ch = &sband->channels[sband->n_channels++];
  2919. geo_ch->center_freq =
  2920. ieee80211_channel_to_frequency(ch->channel, ch->band);
  2921. geo_ch->max_power = ch->max_power_avg;
  2922. geo_ch->max_antenna_gain = 0xff;
  2923. geo_ch->hw_value = ch->channel;
  2924. if (il_is_channel_valid(ch)) {
  2925. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  2926. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  2927. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  2928. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  2929. if (ch->flags & EEPROM_CHANNEL_RADAR)
  2930. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  2931. geo_ch->flags |= ch->ht40_extension_channel;
  2932. if (ch->max_power_avg > max_tx_power)
  2933. max_tx_power = ch->max_power_avg;
  2934. } else {
  2935. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  2936. }
  2937. D_INFO("Channel %d Freq=%d[%sGHz] %s flag=0x%X\n", ch->channel,
  2938. geo_ch->center_freq,
  2939. il_is_channel_a_band(ch) ? "5.2" : "2.4",
  2940. geo_ch->
  2941. flags & IEEE80211_CHAN_DISABLED ? "restricted" : "valid",
  2942. geo_ch->flags);
  2943. }
  2944. il->tx_power_device_lmt = max_tx_power;
  2945. il->tx_power_user_lmt = max_tx_power;
  2946. il->tx_power_next = max_tx_power;
  2947. if (il->bands[IEEE80211_BAND_5GHZ].n_channels == 0 &&
  2948. (il->cfg->sku & IL_SKU_A)) {
  2949. IL_INFO("Incorrectly detected BG card as ABG. "
  2950. "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
  2951. il->pci_dev->device, il->pci_dev->subsystem_device);
  2952. il->cfg->sku &= ~IL_SKU_A;
  2953. }
  2954. IL_INFO("Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  2955. il->bands[IEEE80211_BAND_2GHZ].n_channels,
  2956. il->bands[IEEE80211_BAND_5GHZ].n_channels);
  2957. set_bit(S_GEO_CONFIGURED, &il->status);
  2958. return 0;
  2959. }
  2960. EXPORT_SYMBOL(il_init_geos);
  2961. /*
  2962. * il_free_geos - undo allocations in il_init_geos
  2963. */
  2964. void
  2965. il_free_geos(struct il_priv *il)
  2966. {
  2967. kfree(il->ieee_channels);
  2968. kfree(il->ieee_rates);
  2969. clear_bit(S_GEO_CONFIGURED, &il->status);
  2970. }
  2971. EXPORT_SYMBOL(il_free_geos);
  2972. static bool
  2973. il_is_channel_extension(struct il_priv *il, enum ieee80211_band band,
  2974. u16 channel, u8 extension_chan_offset)
  2975. {
  2976. const struct il_channel_info *ch_info;
  2977. ch_info = il_get_channel_info(il, band, channel);
  2978. if (!il_is_channel_valid(ch_info))
  2979. return false;
  2980. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  2981. return !(ch_info->
  2982. ht40_extension_channel & IEEE80211_CHAN_NO_HT40PLUS);
  2983. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  2984. return !(ch_info->
  2985. ht40_extension_channel & IEEE80211_CHAN_NO_HT40MINUS);
  2986. return false;
  2987. }
  2988. bool
  2989. il_is_ht40_tx_allowed(struct il_priv *il, struct ieee80211_sta_ht_cap *ht_cap)
  2990. {
  2991. if (!il->ht.enabled || !il->ht.is_40mhz)
  2992. return false;
  2993. /*
  2994. * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
  2995. * the bit will not set if it is pure 40MHz case
  2996. */
  2997. if (ht_cap && !ht_cap->ht_supported)
  2998. return false;
  2999. #ifdef CONFIG_IWLEGACY_DEBUGFS
  3000. if (il->disable_ht40)
  3001. return false;
  3002. #endif
  3003. return il_is_channel_extension(il, il->band,
  3004. le16_to_cpu(il->staging.channel),
  3005. il->ht.extension_chan_offset);
  3006. }
  3007. EXPORT_SYMBOL(il_is_ht40_tx_allowed);
  3008. static u16
  3009. il_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
  3010. {
  3011. u16 new_val;
  3012. u16 beacon_factor;
  3013. /*
  3014. * If mac80211 hasn't given us a beacon interval, program
  3015. * the default into the device.
  3016. */
  3017. if (!beacon_val)
  3018. return DEFAULT_BEACON_INTERVAL;
  3019. /*
  3020. * If the beacon interval we obtained from the peer
  3021. * is too large, we'll have to wake up more often
  3022. * (and in IBSS case, we'll beacon too much)
  3023. *
  3024. * For example, if max_beacon_val is 4096, and the
  3025. * requested beacon interval is 7000, we'll have to
  3026. * use 3500 to be able to wake up on the beacons.
  3027. *
  3028. * This could badly influence beacon detection stats.
  3029. */
  3030. beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
  3031. new_val = beacon_val / beacon_factor;
  3032. if (!new_val)
  3033. new_val = max_beacon_val;
  3034. return new_val;
  3035. }
  3036. int
  3037. il_send_rxon_timing(struct il_priv *il)
  3038. {
  3039. u64 tsf;
  3040. s32 interval_tm, rem;
  3041. struct ieee80211_conf *conf = NULL;
  3042. u16 beacon_int;
  3043. struct ieee80211_vif *vif = il->vif;
  3044. conf = &il->hw->conf;
  3045. lockdep_assert_held(&il->mutex);
  3046. memset(&il->timing, 0, sizeof(struct il_rxon_time_cmd));
  3047. il->timing.timestamp = cpu_to_le64(il->timestamp);
  3048. il->timing.listen_interval = cpu_to_le16(conf->listen_interval);
  3049. beacon_int = vif ? vif->bss_conf.beacon_int : 0;
  3050. /*
  3051. * TODO: For IBSS we need to get atim_win from mac80211,
  3052. * for now just always use 0
  3053. */
  3054. il->timing.atim_win = 0;
  3055. beacon_int =
  3056. il_adjust_beacon_interval(beacon_int,
  3057. il->hw_params.max_beacon_itrvl *
  3058. TIME_UNIT);
  3059. il->timing.beacon_interval = cpu_to_le16(beacon_int);
  3060. tsf = il->timestamp; /* tsf is modifed by do_div: copy it */
  3061. interval_tm = beacon_int * TIME_UNIT;
  3062. rem = do_div(tsf, interval_tm);
  3063. il->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
  3064. il->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ? : 1) : 1;
  3065. D_ASSOC("beacon interval %d beacon timer %d beacon tim %d\n",
  3066. le16_to_cpu(il->timing.beacon_interval),
  3067. le32_to_cpu(il->timing.beacon_init_val),
  3068. le16_to_cpu(il->timing.atim_win));
  3069. return il_send_cmd_pdu(il, C_RXON_TIMING, sizeof(il->timing),
  3070. &il->timing);
  3071. }
  3072. EXPORT_SYMBOL(il_send_rxon_timing);
  3073. void
  3074. il_set_rxon_hwcrypto(struct il_priv *il, int hw_decrypt)
  3075. {
  3076. struct il_rxon_cmd *rxon = &il->staging;
  3077. if (hw_decrypt)
  3078. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  3079. else
  3080. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  3081. }
  3082. EXPORT_SYMBOL(il_set_rxon_hwcrypto);
  3083. /* validate RXON structure is valid */
  3084. int
  3085. il_check_rxon_cmd(struct il_priv *il)
  3086. {
  3087. struct il_rxon_cmd *rxon = &il->staging;
  3088. bool error = false;
  3089. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  3090. if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
  3091. IL_WARN("check 2.4G: wrong narrow\n");
  3092. error = true;
  3093. }
  3094. if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
  3095. IL_WARN("check 2.4G: wrong radar\n");
  3096. error = true;
  3097. }
  3098. } else {
  3099. if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
  3100. IL_WARN("check 5.2G: not short slot!\n");
  3101. error = true;
  3102. }
  3103. if (rxon->flags & RXON_FLG_CCK_MSK) {
  3104. IL_WARN("check 5.2G: CCK!\n");
  3105. error = true;
  3106. }
  3107. }
  3108. if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
  3109. IL_WARN("mac/bssid mcast!\n");
  3110. error = true;
  3111. }
  3112. /* make sure basic rates 6Mbps and 1Mbps are supported */
  3113. if ((rxon->ofdm_basic_rates & RATE_6M_MASK) == 0 &&
  3114. (rxon->cck_basic_rates & RATE_1M_MASK) == 0) {
  3115. IL_WARN("neither 1 nor 6 are basic\n");
  3116. error = true;
  3117. }
  3118. if (le16_to_cpu(rxon->assoc_id) > 2007) {
  3119. IL_WARN("aid > 2007\n");
  3120. error = true;
  3121. }
  3122. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) ==
  3123. (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
  3124. IL_WARN("CCK and short slot\n");
  3125. error = true;
  3126. }
  3127. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) ==
  3128. (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
  3129. IL_WARN("CCK and auto detect");
  3130. error = true;
  3131. }
  3132. if ((rxon->
  3133. flags & (RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK)) ==
  3134. RXON_FLG_TGG_PROTECT_MSK) {
  3135. IL_WARN("TGg but no auto-detect\n");
  3136. error = true;
  3137. }
  3138. if (error)
  3139. IL_WARN("Tuning to channel %d\n", le16_to_cpu(rxon->channel));
  3140. if (error) {
  3141. IL_ERR("Invalid RXON\n");
  3142. return -EINVAL;
  3143. }
  3144. return 0;
  3145. }
  3146. EXPORT_SYMBOL(il_check_rxon_cmd);
  3147. /**
  3148. * il_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  3149. * @il: staging_rxon is compared to active_rxon
  3150. *
  3151. * If the RXON structure is changing enough to require a new tune,
  3152. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  3153. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  3154. */
  3155. int
  3156. il_full_rxon_required(struct il_priv *il)
  3157. {
  3158. const struct il_rxon_cmd *staging = &il->staging;
  3159. const struct il_rxon_cmd *active = &il->active;
  3160. #define CHK(cond) \
  3161. if ((cond)) { \
  3162. D_INFO("need full RXON - " #cond "\n"); \
  3163. return 1; \
  3164. }
  3165. #define CHK_NEQ(c1, c2) \
  3166. if ((c1) != (c2)) { \
  3167. D_INFO("need full RXON - " \
  3168. #c1 " != " #c2 " - %d != %d\n", \
  3169. (c1), (c2)); \
  3170. return 1; \
  3171. }
  3172. /* These items are only settable from the full RXON command */
  3173. CHK(!il_is_associated(il));
  3174. CHK(!ether_addr_equal(staging->bssid_addr, active->bssid_addr));
  3175. CHK(!ether_addr_equal(staging->node_addr, active->node_addr));
  3176. CHK(!ether_addr_equal(staging->wlap_bssid_addr,
  3177. active->wlap_bssid_addr));
  3178. CHK_NEQ(staging->dev_type, active->dev_type);
  3179. CHK_NEQ(staging->channel, active->channel);
  3180. CHK_NEQ(staging->air_propagation, active->air_propagation);
  3181. CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
  3182. active->ofdm_ht_single_stream_basic_rates);
  3183. CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
  3184. active->ofdm_ht_dual_stream_basic_rates);
  3185. CHK_NEQ(staging->assoc_id, active->assoc_id);
  3186. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  3187. * be updated with the RXON_ASSOC command -- however only some
  3188. * flag transitions are allowed using RXON_ASSOC */
  3189. /* Check if we are not switching bands */
  3190. CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
  3191. active->flags & RXON_FLG_BAND_24G_MSK);
  3192. /* Check if we are switching association toggle */
  3193. CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
  3194. active->filter_flags & RXON_FILTER_ASSOC_MSK);
  3195. #undef CHK
  3196. #undef CHK_NEQ
  3197. return 0;
  3198. }
  3199. EXPORT_SYMBOL(il_full_rxon_required);
  3200. u8
  3201. il_get_lowest_plcp(struct il_priv *il)
  3202. {
  3203. /*
  3204. * Assign the lowest rate -- should really get this from
  3205. * the beacon skb from mac80211.
  3206. */
  3207. if (il->staging.flags & RXON_FLG_BAND_24G_MSK)
  3208. return RATE_1M_PLCP;
  3209. else
  3210. return RATE_6M_PLCP;
  3211. }
  3212. EXPORT_SYMBOL(il_get_lowest_plcp);
  3213. static void
  3214. _il_set_rxon_ht(struct il_priv *il, struct il_ht_config *ht_conf)
  3215. {
  3216. struct il_rxon_cmd *rxon = &il->staging;
  3217. if (!il->ht.enabled) {
  3218. rxon->flags &=
  3219. ~(RXON_FLG_CHANNEL_MODE_MSK |
  3220. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK | RXON_FLG_HT40_PROT_MSK
  3221. | RXON_FLG_HT_PROT_MSK);
  3222. return;
  3223. }
  3224. rxon->flags |=
  3225. cpu_to_le32(il->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
  3226. /* Set up channel bandwidth:
  3227. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  3228. /* clear the HT channel mode before set the mode */
  3229. rxon->flags &=
  3230. ~(RXON_FLG_CHANNEL_MODE_MSK | RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  3231. if (il_is_ht40_tx_allowed(il, NULL)) {
  3232. /* pure ht40 */
  3233. if (il->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  3234. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  3235. /* Note: control channel is opposite of extension channel */
  3236. switch (il->ht.extension_chan_offset) {
  3237. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  3238. rxon->flags &=
  3239. ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3240. break;
  3241. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  3242. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3243. break;
  3244. }
  3245. } else {
  3246. /* Note: control channel is opposite of extension channel */
  3247. switch (il->ht.extension_chan_offset) {
  3248. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  3249. rxon->flags &=
  3250. ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  3251. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  3252. break;
  3253. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  3254. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3255. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  3256. break;
  3257. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  3258. default:
  3259. /* channel location only valid if in Mixed mode */
  3260. IL_ERR("invalid extension channel offset\n");
  3261. break;
  3262. }
  3263. }
  3264. } else {
  3265. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  3266. }
  3267. if (il->ops->set_rxon_chain)
  3268. il->ops->set_rxon_chain(il);
  3269. D_ASSOC("rxon flags 0x%X operation mode :0x%X "
  3270. "extension channel offset 0x%x\n", le32_to_cpu(rxon->flags),
  3271. il->ht.protection, il->ht.extension_chan_offset);
  3272. }
  3273. void
  3274. il_set_rxon_ht(struct il_priv *il, struct il_ht_config *ht_conf)
  3275. {
  3276. _il_set_rxon_ht(il, ht_conf);
  3277. }
  3278. EXPORT_SYMBOL(il_set_rxon_ht);
  3279. /* Return valid, unused, channel for a passive scan to reset the RF */
  3280. u8
  3281. il_get_single_channel_number(struct il_priv *il, enum ieee80211_band band)
  3282. {
  3283. const struct il_channel_info *ch_info;
  3284. int i;
  3285. u8 channel = 0;
  3286. u8 min, max;
  3287. if (band == IEEE80211_BAND_5GHZ) {
  3288. min = 14;
  3289. max = il->channel_count;
  3290. } else {
  3291. min = 0;
  3292. max = 14;
  3293. }
  3294. for (i = min; i < max; i++) {
  3295. channel = il->channel_info[i].channel;
  3296. if (channel == le16_to_cpu(il->staging.channel))
  3297. continue;
  3298. ch_info = il_get_channel_info(il, band, channel);
  3299. if (il_is_channel_valid(ch_info))
  3300. break;
  3301. }
  3302. return channel;
  3303. }
  3304. EXPORT_SYMBOL(il_get_single_channel_number);
  3305. /**
  3306. * il_set_rxon_channel - Set the band and channel values in staging RXON
  3307. * @ch: requested channel as a pointer to struct ieee80211_channel
  3308. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  3309. * in the staging RXON flag structure based on the ch->band
  3310. */
  3311. int
  3312. il_set_rxon_channel(struct il_priv *il, struct ieee80211_channel *ch)
  3313. {
  3314. enum ieee80211_band band = ch->band;
  3315. u16 channel = ch->hw_value;
  3316. if (le16_to_cpu(il->staging.channel) == channel && il->band == band)
  3317. return 0;
  3318. il->staging.channel = cpu_to_le16(channel);
  3319. if (band == IEEE80211_BAND_5GHZ)
  3320. il->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  3321. else
  3322. il->staging.flags |= RXON_FLG_BAND_24G_MSK;
  3323. il->band = band;
  3324. D_INFO("Staging channel set to %d [%d]\n", channel, band);
  3325. return 0;
  3326. }
  3327. EXPORT_SYMBOL(il_set_rxon_channel);
  3328. void
  3329. il_set_flags_for_band(struct il_priv *il, enum ieee80211_band band,
  3330. struct ieee80211_vif *vif)
  3331. {
  3332. if (band == IEEE80211_BAND_5GHZ) {
  3333. il->staging.flags &=
  3334. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK |
  3335. RXON_FLG_CCK_MSK);
  3336. il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  3337. } else {
  3338. /* Copied from il_post_associate() */
  3339. if (vif && vif->bss_conf.use_short_slot)
  3340. il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  3341. else
  3342. il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  3343. il->staging.flags |= RXON_FLG_BAND_24G_MSK;
  3344. il->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  3345. il->staging.flags &= ~RXON_FLG_CCK_MSK;
  3346. }
  3347. }
  3348. EXPORT_SYMBOL(il_set_flags_for_band);
  3349. /*
  3350. * initialize rxon structure with default values from eeprom
  3351. */
  3352. void
  3353. il_connection_init_rx_config(struct il_priv *il)
  3354. {
  3355. const struct il_channel_info *ch_info;
  3356. memset(&il->staging, 0, sizeof(il->staging));
  3357. if (!il->vif) {
  3358. il->staging.dev_type = RXON_DEV_TYPE_ESS;
  3359. } else if (il->vif->type == NL80211_IFTYPE_STATION) {
  3360. il->staging.dev_type = RXON_DEV_TYPE_ESS;
  3361. il->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  3362. } else if (il->vif->type == NL80211_IFTYPE_ADHOC) {
  3363. il->staging.dev_type = RXON_DEV_TYPE_IBSS;
  3364. il->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  3365. il->staging.filter_flags =
  3366. RXON_FILTER_BCON_AWARE_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
  3367. } else {
  3368. IL_ERR("Unsupported interface type %d\n", il->vif->type);
  3369. return;
  3370. }
  3371. #if 0
  3372. /* TODO: Figure out when short_preamble would be set and cache from
  3373. * that */
  3374. if (!hw_to_local(il->hw)->short_preamble)
  3375. il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3376. else
  3377. il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  3378. #endif
  3379. ch_info =
  3380. il_get_channel_info(il, il->band, le16_to_cpu(il->active.channel));
  3381. if (!ch_info)
  3382. ch_info = &il->channel_info[0];
  3383. il->staging.channel = cpu_to_le16(ch_info->channel);
  3384. il->band = ch_info->band;
  3385. il_set_flags_for_band(il, il->band, il->vif);
  3386. il->staging.ofdm_basic_rates =
  3387. (IL_OFDM_RATES_MASK >> IL_FIRST_OFDM_RATE) & 0xFF;
  3388. il->staging.cck_basic_rates =
  3389. (IL_CCK_RATES_MASK >> IL_FIRST_CCK_RATE) & 0xF;
  3390. /* clear both MIX and PURE40 mode flag */
  3391. il->staging.flags &=
  3392. ~(RXON_FLG_CHANNEL_MODE_MIXED | RXON_FLG_CHANNEL_MODE_PURE_40);
  3393. if (il->vif)
  3394. memcpy(il->staging.node_addr, il->vif->addr, ETH_ALEN);
  3395. il->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  3396. il->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  3397. }
  3398. EXPORT_SYMBOL(il_connection_init_rx_config);
  3399. void
  3400. il_set_rate(struct il_priv *il)
  3401. {
  3402. const struct ieee80211_supported_band *hw = NULL;
  3403. struct ieee80211_rate *rate;
  3404. int i;
  3405. hw = il_get_hw_mode(il, il->band);
  3406. if (!hw) {
  3407. IL_ERR("Failed to set rate: unable to get hw mode\n");
  3408. return;
  3409. }
  3410. il->active_rate = 0;
  3411. for (i = 0; i < hw->n_bitrates; i++) {
  3412. rate = &(hw->bitrates[i]);
  3413. if (rate->hw_value < RATE_COUNT_LEGACY)
  3414. il->active_rate |= (1 << rate->hw_value);
  3415. }
  3416. D_RATE("Set active_rate = %0x\n", il->active_rate);
  3417. il->staging.cck_basic_rates =
  3418. (IL_CCK_BASIC_RATES_MASK >> IL_FIRST_CCK_RATE) & 0xF;
  3419. il->staging.ofdm_basic_rates =
  3420. (IL_OFDM_BASIC_RATES_MASK >> IL_FIRST_OFDM_RATE) & 0xFF;
  3421. }
  3422. EXPORT_SYMBOL(il_set_rate);
  3423. void
  3424. il_chswitch_done(struct il_priv *il, bool is_success)
  3425. {
  3426. if (test_bit(S_EXIT_PENDING, &il->status))
  3427. return;
  3428. if (test_and_clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
  3429. ieee80211_chswitch_done(il->vif, is_success);
  3430. }
  3431. EXPORT_SYMBOL(il_chswitch_done);
  3432. void
  3433. il_hdl_csa(struct il_priv *il, struct il_rx_buf *rxb)
  3434. {
  3435. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3436. struct il_csa_notification *csa = &(pkt->u.csa_notif);
  3437. struct il_rxon_cmd *rxon = (void *)&il->active;
  3438. if (!test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
  3439. return;
  3440. if (!le32_to_cpu(csa->status) && csa->channel == il->switch_channel) {
  3441. rxon->channel = csa->channel;
  3442. il->staging.channel = csa->channel;
  3443. D_11H("CSA notif: channel %d\n", le16_to_cpu(csa->channel));
  3444. il_chswitch_done(il, true);
  3445. } else {
  3446. IL_ERR("CSA notif (fail) : channel %d\n",
  3447. le16_to_cpu(csa->channel));
  3448. il_chswitch_done(il, false);
  3449. }
  3450. }
  3451. EXPORT_SYMBOL(il_hdl_csa);
  3452. #ifdef CONFIG_IWLEGACY_DEBUG
  3453. void
  3454. il_print_rx_config_cmd(struct il_priv *il)
  3455. {
  3456. struct il_rxon_cmd *rxon = &il->staging;
  3457. D_RADIO("RX CONFIG:\n");
  3458. il_print_hex_dump(il, IL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  3459. D_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  3460. D_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  3461. D_RADIO("u32 filter_flags: 0x%08x\n", le32_to_cpu(rxon->filter_flags));
  3462. D_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
  3463. D_RADIO("u8 ofdm_basic_rates: 0x%02x\n", rxon->ofdm_basic_rates);
  3464. D_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  3465. D_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
  3466. D_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  3467. D_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  3468. }
  3469. EXPORT_SYMBOL(il_print_rx_config_cmd);
  3470. #endif
  3471. /**
  3472. * il_irq_handle_error - called for HW or SW error interrupt from card
  3473. */
  3474. void
  3475. il_irq_handle_error(struct il_priv *il)
  3476. {
  3477. /* Set the FW error flag -- cleared on il_down */
  3478. set_bit(S_FW_ERROR, &il->status);
  3479. /* Cancel currently queued command. */
  3480. clear_bit(S_HCMD_ACTIVE, &il->status);
  3481. IL_ERR("Loaded firmware version: %s\n", il->hw->wiphy->fw_version);
  3482. il->ops->dump_nic_error_log(il);
  3483. if (il->ops->dump_fh)
  3484. il->ops->dump_fh(il, NULL, false);
  3485. #ifdef CONFIG_IWLEGACY_DEBUG
  3486. if (il_get_debug_level(il) & IL_DL_FW_ERRORS)
  3487. il_print_rx_config_cmd(il);
  3488. #endif
  3489. wake_up(&il->wait_command_queue);
  3490. /* Keep the restart process from trying to send host
  3491. * commands by clearing the INIT status bit */
  3492. clear_bit(S_READY, &il->status);
  3493. if (!test_bit(S_EXIT_PENDING, &il->status)) {
  3494. IL_DBG(IL_DL_FW_ERRORS,
  3495. "Restarting adapter due to uCode error.\n");
  3496. if (il->cfg->mod_params->restart_fw)
  3497. queue_work(il->workqueue, &il->restart);
  3498. }
  3499. }
  3500. EXPORT_SYMBOL(il_irq_handle_error);
  3501. static int
  3502. _il_apm_stop_master(struct il_priv *il)
  3503. {
  3504. int ret = 0;
  3505. /* stop device's busmaster DMA activity */
  3506. _il_set_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  3507. ret =
  3508. _il_poll_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
  3509. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  3510. if (ret < 0)
  3511. IL_WARN("Master Disable Timed Out, 100 usec\n");
  3512. D_INFO("stop master\n");
  3513. return ret;
  3514. }
  3515. void
  3516. _il_apm_stop(struct il_priv *il)
  3517. {
  3518. lockdep_assert_held(&il->reg_lock);
  3519. D_INFO("Stop card, put in low power state\n");
  3520. /* Stop device's DMA activity */
  3521. _il_apm_stop_master(il);
  3522. /* Reset the entire device */
  3523. _il_set_bit(il, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  3524. udelay(10);
  3525. /*
  3526. * Clear "initialization complete" bit to move adapter from
  3527. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  3528. */
  3529. _il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  3530. }
  3531. EXPORT_SYMBOL(_il_apm_stop);
  3532. void
  3533. il_apm_stop(struct il_priv *il)
  3534. {
  3535. unsigned long flags;
  3536. spin_lock_irqsave(&il->reg_lock, flags);
  3537. _il_apm_stop(il);
  3538. spin_unlock_irqrestore(&il->reg_lock, flags);
  3539. }
  3540. EXPORT_SYMBOL(il_apm_stop);
  3541. /*
  3542. * Start up NIC's basic functionality after it has been reset
  3543. * (e.g. after platform boot, or shutdown via il_apm_stop())
  3544. * NOTE: This does not load uCode nor start the embedded processor
  3545. */
  3546. int
  3547. il_apm_init(struct il_priv *il)
  3548. {
  3549. int ret = 0;
  3550. u16 lctl;
  3551. D_INFO("Init card's basic functions\n");
  3552. /*
  3553. * Use "set_bit" below rather than "write", to preserve any hardware
  3554. * bits already set by default after reset.
  3555. */
  3556. /* Disable L0S exit timer (platform NMI Work/Around) */
  3557. il_set_bit(il, CSR_GIO_CHICKEN_BITS,
  3558. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  3559. /*
  3560. * Disable L0s without affecting L1;
  3561. * don't wait for ICH L0s (ICH bug W/A)
  3562. */
  3563. il_set_bit(il, CSR_GIO_CHICKEN_BITS,
  3564. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  3565. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  3566. il_set_bit(il, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  3567. /*
  3568. * Enable HAP INTA (interrupt from management bus) to
  3569. * wake device's PCI Express link L1a -> L0s
  3570. * NOTE: This is no-op for 3945 (non-existent bit)
  3571. */
  3572. il_set_bit(il, CSR_HW_IF_CONFIG_REG,
  3573. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  3574. /*
  3575. * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
  3576. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  3577. * If so (likely), disable L0S, so device moves directly L0->L1;
  3578. * costs negligible amount of power savings.
  3579. * If not (unlikely), enable L0S, so there is at least some
  3580. * power savings, even without L1.
  3581. */
  3582. if (il->cfg->set_l0s) {
  3583. pcie_capability_read_word(il->pci_dev, PCI_EXP_LNKCTL, &lctl);
  3584. if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
  3585. PCI_CFG_LINK_CTRL_VAL_L1_EN) {
  3586. /* L1-ASPM enabled; disable(!) L0S */
  3587. il_set_bit(il, CSR_GIO_REG,
  3588. CSR_GIO_REG_VAL_L0S_ENABLED);
  3589. D_POWER("L1 Enabled; Disabling L0S\n");
  3590. } else {
  3591. /* L1-ASPM disabled; enable(!) L0S */
  3592. il_clear_bit(il, CSR_GIO_REG,
  3593. CSR_GIO_REG_VAL_L0S_ENABLED);
  3594. D_POWER("L1 Disabled; Enabling L0S\n");
  3595. }
  3596. }
  3597. /* Configure analog phase-lock-loop before activating to D0A */
  3598. if (il->cfg->pll_cfg_val)
  3599. il_set_bit(il, CSR_ANA_PLL_CFG,
  3600. il->cfg->pll_cfg_val);
  3601. /*
  3602. * Set "initialization complete" bit to move adapter from
  3603. * D0U* --> D0A* (powered-up active) state.
  3604. */
  3605. il_set_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  3606. /*
  3607. * Wait for clock stabilization; once stabilized, access to
  3608. * device-internal resources is supported, e.g. il_wr_prph()
  3609. * and accesses to uCode SRAM.
  3610. */
  3611. ret =
  3612. _il_poll_bit(il, CSR_GP_CNTRL,
  3613. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  3614. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  3615. if (ret < 0) {
  3616. D_INFO("Failed to init the card\n");
  3617. goto out;
  3618. }
  3619. /*
  3620. * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
  3621. * BSM (Boostrap State Machine) is only in 3945 and 4965.
  3622. *
  3623. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  3624. * do not disable clocks. This preserves any hardware bits already
  3625. * set by default in "CLK_CTRL_REG" after reset.
  3626. */
  3627. if (il->cfg->use_bsm)
  3628. il_wr_prph(il, APMG_CLK_EN_REG,
  3629. APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
  3630. else
  3631. il_wr_prph(il, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  3632. udelay(20);
  3633. /* Disable L1-Active */
  3634. il_set_bits_prph(il, APMG_PCIDEV_STT_REG,
  3635. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  3636. out:
  3637. return ret;
  3638. }
  3639. EXPORT_SYMBOL(il_apm_init);
  3640. int
  3641. il_set_tx_power(struct il_priv *il, s8 tx_power, bool force)
  3642. {
  3643. int ret;
  3644. s8 prev_tx_power;
  3645. bool defer;
  3646. lockdep_assert_held(&il->mutex);
  3647. if (il->tx_power_user_lmt == tx_power && !force)
  3648. return 0;
  3649. if (!il->ops->send_tx_power)
  3650. return -EOPNOTSUPP;
  3651. /* 0 dBm mean 1 milliwatt */
  3652. if (tx_power < 0) {
  3653. IL_WARN("Requested user TXPOWER %d below 1 mW.\n", tx_power);
  3654. return -EINVAL;
  3655. }
  3656. if (tx_power > il->tx_power_device_lmt) {
  3657. IL_WARN("Requested user TXPOWER %d above upper limit %d.\n",
  3658. tx_power, il->tx_power_device_lmt);
  3659. return -EINVAL;
  3660. }
  3661. if (!il_is_ready_rf(il))
  3662. return -EIO;
  3663. /* scan complete and commit_rxon use tx_power_next value,
  3664. * it always need to be updated for newest request */
  3665. il->tx_power_next = tx_power;
  3666. /* do not set tx power when scanning or channel changing */
  3667. defer = test_bit(S_SCANNING, &il->status) ||
  3668. memcmp(&il->active, &il->staging, sizeof(il->staging));
  3669. if (defer && !force) {
  3670. D_INFO("Deferring tx power set\n");
  3671. return 0;
  3672. }
  3673. prev_tx_power = il->tx_power_user_lmt;
  3674. il->tx_power_user_lmt = tx_power;
  3675. ret = il->ops->send_tx_power(il);
  3676. /* if fail to set tx_power, restore the orig. tx power */
  3677. if (ret) {
  3678. il->tx_power_user_lmt = prev_tx_power;
  3679. il->tx_power_next = prev_tx_power;
  3680. }
  3681. return ret;
  3682. }
  3683. EXPORT_SYMBOL(il_set_tx_power);
  3684. void
  3685. il_send_bt_config(struct il_priv *il)
  3686. {
  3687. struct il_bt_cmd bt_cmd = {
  3688. .lead_time = BT_LEAD_TIME_DEF,
  3689. .max_kill = BT_MAX_KILL_DEF,
  3690. .kill_ack_mask = 0,
  3691. .kill_cts_mask = 0,
  3692. };
  3693. if (!bt_coex_active)
  3694. bt_cmd.flags = BT_COEX_DISABLE;
  3695. else
  3696. bt_cmd.flags = BT_COEX_ENABLE;
  3697. D_INFO("BT coex %s\n",
  3698. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  3699. if (il_send_cmd_pdu(il, C_BT_CONFIG, sizeof(struct il_bt_cmd), &bt_cmd))
  3700. IL_ERR("failed to send BT Coex Config\n");
  3701. }
  3702. EXPORT_SYMBOL(il_send_bt_config);
  3703. int
  3704. il_send_stats_request(struct il_priv *il, u8 flags, bool clear)
  3705. {
  3706. struct il_stats_cmd stats_cmd = {
  3707. .configuration_flags = clear ? IL_STATS_CONF_CLEAR_STATS : 0,
  3708. };
  3709. if (flags & CMD_ASYNC)
  3710. return il_send_cmd_pdu_async(il, C_STATS, sizeof(struct il_stats_cmd),
  3711. &stats_cmd, NULL);
  3712. else
  3713. return il_send_cmd_pdu(il, C_STATS, sizeof(struct il_stats_cmd),
  3714. &stats_cmd);
  3715. }
  3716. EXPORT_SYMBOL(il_send_stats_request);
  3717. void
  3718. il_hdl_pm_sleep(struct il_priv *il, struct il_rx_buf *rxb)
  3719. {
  3720. #ifdef CONFIG_IWLEGACY_DEBUG
  3721. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3722. struct il_sleep_notification *sleep = &(pkt->u.sleep_notif);
  3723. D_RX("sleep mode: %d, src: %d\n",
  3724. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  3725. #endif
  3726. }
  3727. EXPORT_SYMBOL(il_hdl_pm_sleep);
  3728. void
  3729. il_hdl_pm_debug_stats(struct il_priv *il, struct il_rx_buf *rxb)
  3730. {
  3731. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3732. u32 len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
  3733. D_RADIO("Dumping %d bytes of unhandled notification for %s:\n", len,
  3734. il_get_cmd_string(pkt->hdr.cmd));
  3735. il_print_hex_dump(il, IL_DL_RADIO, pkt->u.raw, len);
  3736. }
  3737. EXPORT_SYMBOL(il_hdl_pm_debug_stats);
  3738. void
  3739. il_hdl_error(struct il_priv *il, struct il_rx_buf *rxb)
  3740. {
  3741. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3742. IL_ERR("Error Reply type 0x%08X cmd %s (0x%02X) "
  3743. "seq 0x%04X ser 0x%08X\n",
  3744. le32_to_cpu(pkt->u.err_resp.error_type),
  3745. il_get_cmd_string(pkt->u.err_resp.cmd_id),
  3746. pkt->u.err_resp.cmd_id,
  3747. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  3748. le32_to_cpu(pkt->u.err_resp.error_info));
  3749. }
  3750. EXPORT_SYMBOL(il_hdl_error);
  3751. void
  3752. il_clear_isr_stats(struct il_priv *il)
  3753. {
  3754. memset(&il->isr_stats, 0, sizeof(il->isr_stats));
  3755. }
  3756. int
  3757. il_mac_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u16 queue,
  3758. const struct ieee80211_tx_queue_params *params)
  3759. {
  3760. struct il_priv *il = hw->priv;
  3761. unsigned long flags;
  3762. int q;
  3763. D_MAC80211("enter\n");
  3764. if (!il_is_ready_rf(il)) {
  3765. D_MAC80211("leave - RF not ready\n");
  3766. return -EIO;
  3767. }
  3768. if (queue >= AC_NUM) {
  3769. D_MAC80211("leave - queue >= AC_NUM %d\n", queue);
  3770. return 0;
  3771. }
  3772. q = AC_NUM - 1 - queue;
  3773. spin_lock_irqsave(&il->lock, flags);
  3774. il->qos_data.def_qos_parm.ac[q].cw_min =
  3775. cpu_to_le16(params->cw_min);
  3776. il->qos_data.def_qos_parm.ac[q].cw_max =
  3777. cpu_to_le16(params->cw_max);
  3778. il->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  3779. il->qos_data.def_qos_parm.ac[q].edca_txop =
  3780. cpu_to_le16((params->txop * 32));
  3781. il->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  3782. spin_unlock_irqrestore(&il->lock, flags);
  3783. D_MAC80211("leave\n");
  3784. return 0;
  3785. }
  3786. EXPORT_SYMBOL(il_mac_conf_tx);
  3787. int
  3788. il_mac_tx_last_beacon(struct ieee80211_hw *hw)
  3789. {
  3790. struct il_priv *il = hw->priv;
  3791. int ret;
  3792. D_MAC80211("enter\n");
  3793. ret = (il->ibss_manager == IL_IBSS_MANAGER);
  3794. D_MAC80211("leave ret %d\n", ret);
  3795. return ret;
  3796. }
  3797. EXPORT_SYMBOL_GPL(il_mac_tx_last_beacon);
  3798. static int
  3799. il_set_mode(struct il_priv *il)
  3800. {
  3801. il_connection_init_rx_config(il);
  3802. if (il->ops->set_rxon_chain)
  3803. il->ops->set_rxon_chain(il);
  3804. return il_commit_rxon(il);
  3805. }
  3806. int
  3807. il_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  3808. {
  3809. struct il_priv *il = hw->priv;
  3810. int err;
  3811. bool reset;
  3812. mutex_lock(&il->mutex);
  3813. D_MAC80211("enter: type %d, addr %pM\n", vif->type, vif->addr);
  3814. if (!il_is_ready_rf(il)) {
  3815. IL_WARN("Try to add interface when device not ready\n");
  3816. err = -EINVAL;
  3817. goto out;
  3818. }
  3819. /*
  3820. * We do not support multiple virtual interfaces, but on hardware reset
  3821. * we have to add the same interface again.
  3822. */
  3823. reset = (il->vif == vif);
  3824. if (il->vif && !reset) {
  3825. err = -EOPNOTSUPP;
  3826. goto out;
  3827. }
  3828. il->vif = vif;
  3829. il->iw_mode = vif->type;
  3830. err = il_set_mode(il);
  3831. if (err) {
  3832. IL_WARN("Fail to set mode %d\n", vif->type);
  3833. if (!reset) {
  3834. il->vif = NULL;
  3835. il->iw_mode = NL80211_IFTYPE_STATION;
  3836. }
  3837. }
  3838. out:
  3839. D_MAC80211("leave err %d\n", err);
  3840. mutex_unlock(&il->mutex);
  3841. return err;
  3842. }
  3843. EXPORT_SYMBOL(il_mac_add_interface);
  3844. static void
  3845. il_teardown_interface(struct il_priv *il, struct ieee80211_vif *vif,
  3846. bool mode_change)
  3847. {
  3848. lockdep_assert_held(&il->mutex);
  3849. if (il->scan_vif == vif) {
  3850. il_scan_cancel_timeout(il, 200);
  3851. il_force_scan_end(il);
  3852. }
  3853. if (!mode_change)
  3854. il_set_mode(il);
  3855. }
  3856. void
  3857. il_mac_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  3858. {
  3859. struct il_priv *il = hw->priv;
  3860. mutex_lock(&il->mutex);
  3861. D_MAC80211("enter: type %d, addr %pM\n", vif->type, vif->addr);
  3862. WARN_ON(il->vif != vif);
  3863. il->vif = NULL;
  3864. il_teardown_interface(il, vif, false);
  3865. memset(il->bssid, 0, ETH_ALEN);
  3866. D_MAC80211("leave\n");
  3867. mutex_unlock(&il->mutex);
  3868. }
  3869. EXPORT_SYMBOL(il_mac_remove_interface);
  3870. int
  3871. il_alloc_txq_mem(struct il_priv *il)
  3872. {
  3873. if (!il->txq)
  3874. il->txq =
  3875. kzalloc(sizeof(struct il_tx_queue) *
  3876. il->cfg->num_of_queues, GFP_KERNEL);
  3877. if (!il->txq) {
  3878. IL_ERR("Not enough memory for txq\n");
  3879. return -ENOMEM;
  3880. }
  3881. return 0;
  3882. }
  3883. EXPORT_SYMBOL(il_alloc_txq_mem);
  3884. void
  3885. il_free_txq_mem(struct il_priv *il)
  3886. {
  3887. kfree(il->txq);
  3888. il->txq = NULL;
  3889. }
  3890. EXPORT_SYMBOL(il_free_txq_mem);
  3891. int
  3892. il_force_reset(struct il_priv *il, bool external)
  3893. {
  3894. struct il_force_reset *force_reset;
  3895. if (test_bit(S_EXIT_PENDING, &il->status))
  3896. return -EINVAL;
  3897. force_reset = &il->force_reset;
  3898. force_reset->reset_request_count++;
  3899. if (!external) {
  3900. if (force_reset->last_force_reset_jiffies &&
  3901. time_after(force_reset->last_force_reset_jiffies +
  3902. force_reset->reset_duration, jiffies)) {
  3903. D_INFO("force reset rejected\n");
  3904. force_reset->reset_reject_count++;
  3905. return -EAGAIN;
  3906. }
  3907. }
  3908. force_reset->reset_success_count++;
  3909. force_reset->last_force_reset_jiffies = jiffies;
  3910. /*
  3911. * if the request is from external(ex: debugfs),
  3912. * then always perform the request in regardless the module
  3913. * parameter setting
  3914. * if the request is from internal (uCode error or driver
  3915. * detect failure), then fw_restart module parameter
  3916. * need to be check before performing firmware reload
  3917. */
  3918. if (!external && !il->cfg->mod_params->restart_fw) {
  3919. D_INFO("Cancel firmware reload based on "
  3920. "module parameter setting\n");
  3921. return 0;
  3922. }
  3923. IL_ERR("On demand firmware reload\n");
  3924. /* Set the FW error flag -- cleared on il_down */
  3925. set_bit(S_FW_ERROR, &il->status);
  3926. wake_up(&il->wait_command_queue);
  3927. /*
  3928. * Keep the restart process from trying to send host
  3929. * commands by clearing the INIT status bit
  3930. */
  3931. clear_bit(S_READY, &il->status);
  3932. queue_work(il->workqueue, &il->restart);
  3933. return 0;
  3934. }
  3935. int
  3936. il_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  3937. enum nl80211_iftype newtype, bool newp2p)
  3938. {
  3939. struct il_priv *il = hw->priv;
  3940. int err;
  3941. mutex_lock(&il->mutex);
  3942. D_MAC80211("enter: type %d, addr %pM newtype %d newp2p %d\n",
  3943. vif->type, vif->addr, newtype, newp2p);
  3944. if (newp2p) {
  3945. err = -EOPNOTSUPP;
  3946. goto out;
  3947. }
  3948. if (!il->vif || !il_is_ready_rf(il)) {
  3949. /*
  3950. * Huh? But wait ... this can maybe happen when
  3951. * we're in the middle of a firmware restart!
  3952. */
  3953. err = -EBUSY;
  3954. goto out;
  3955. }
  3956. /* success */
  3957. il_teardown_interface(il, vif, true);
  3958. vif->type = newtype;
  3959. vif->p2p = false;
  3960. err = il_set_mode(il);
  3961. WARN_ON(err);
  3962. /*
  3963. * We've switched internally, but submitting to the
  3964. * device may have failed for some reason. Mask this
  3965. * error, because otherwise mac80211 will not switch
  3966. * (and set the interface type back) and we'll be
  3967. * out of sync with it.
  3968. */
  3969. err = 0;
  3970. out:
  3971. D_MAC80211("leave err %d\n", err);
  3972. mutex_unlock(&il->mutex);
  3973. return err;
  3974. }
  3975. EXPORT_SYMBOL(il_mac_change_interface);
  3976. /*
  3977. * On every watchdog tick we check (latest) time stamp. If it does not
  3978. * change during timeout period and queue is not empty we reset firmware.
  3979. */
  3980. static int
  3981. il_check_stuck_queue(struct il_priv *il, int cnt)
  3982. {
  3983. struct il_tx_queue *txq = &il->txq[cnt];
  3984. struct il_queue *q = &txq->q;
  3985. unsigned long timeout;
  3986. unsigned long now = jiffies;
  3987. int ret;
  3988. if (q->read_ptr == q->write_ptr) {
  3989. txq->time_stamp = now;
  3990. return 0;
  3991. }
  3992. timeout =
  3993. txq->time_stamp +
  3994. msecs_to_jiffies(il->cfg->wd_timeout);
  3995. if (time_after(now, timeout)) {
  3996. IL_ERR("Queue %d stuck for %u ms.\n", q->id,
  3997. jiffies_to_msecs(now - txq->time_stamp));
  3998. ret = il_force_reset(il, false);
  3999. return (ret == -EAGAIN) ? 0 : 1;
  4000. }
  4001. return 0;
  4002. }
  4003. /*
  4004. * Making watchdog tick be a quarter of timeout assure we will
  4005. * discover the queue hung between timeout and 1.25*timeout
  4006. */
  4007. #define IL_WD_TICK(timeout) ((timeout) / 4)
  4008. /*
  4009. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  4010. * we reset the firmware. If everything is fine just rearm the timer.
  4011. */
  4012. void
  4013. il_bg_watchdog(unsigned long data)
  4014. {
  4015. struct il_priv *il = (struct il_priv *)data;
  4016. int cnt;
  4017. unsigned long timeout;
  4018. if (test_bit(S_EXIT_PENDING, &il->status))
  4019. return;
  4020. timeout = il->cfg->wd_timeout;
  4021. if (timeout == 0)
  4022. return;
  4023. /* monitor and check for stuck cmd queue */
  4024. if (il_check_stuck_queue(il, il->cmd_queue))
  4025. return;
  4026. /* monitor and check for other stuck queues */
  4027. for (cnt = 0; cnt < il->hw_params.max_txq_num; cnt++) {
  4028. /* skip as we already checked the command queue */
  4029. if (cnt == il->cmd_queue)
  4030. continue;
  4031. if (il_check_stuck_queue(il, cnt))
  4032. return;
  4033. }
  4034. mod_timer(&il->watchdog,
  4035. jiffies + msecs_to_jiffies(IL_WD_TICK(timeout)));
  4036. }
  4037. EXPORT_SYMBOL(il_bg_watchdog);
  4038. void
  4039. il_setup_watchdog(struct il_priv *il)
  4040. {
  4041. unsigned int timeout = il->cfg->wd_timeout;
  4042. if (timeout)
  4043. mod_timer(&il->watchdog,
  4044. jiffies + msecs_to_jiffies(IL_WD_TICK(timeout)));
  4045. else
  4046. del_timer(&il->watchdog);
  4047. }
  4048. EXPORT_SYMBOL(il_setup_watchdog);
  4049. /*
  4050. * extended beacon time format
  4051. * time in usec will be changed into a 32-bit value in extended:internal format
  4052. * the extended part is the beacon counts
  4053. * the internal part is the time in usec within one beacon interval
  4054. */
  4055. u32
  4056. il_usecs_to_beacons(struct il_priv *il, u32 usec, u32 beacon_interval)
  4057. {
  4058. u32 quot;
  4059. u32 rem;
  4060. u32 interval = beacon_interval * TIME_UNIT;
  4061. if (!interval || !usec)
  4062. return 0;
  4063. quot =
  4064. (usec /
  4065. interval) & (il_beacon_time_mask_high(il,
  4066. il->hw_params.
  4067. beacon_time_tsf_bits) >> il->
  4068. hw_params.beacon_time_tsf_bits);
  4069. rem =
  4070. (usec % interval) & il_beacon_time_mask_low(il,
  4071. il->hw_params.
  4072. beacon_time_tsf_bits);
  4073. return (quot << il->hw_params.beacon_time_tsf_bits) + rem;
  4074. }
  4075. EXPORT_SYMBOL(il_usecs_to_beacons);
  4076. /* base is usually what we get from ucode with each received frame,
  4077. * the same as HW timer counter counting down
  4078. */
  4079. __le32
  4080. il_add_beacon_time(struct il_priv *il, u32 base, u32 addon,
  4081. u32 beacon_interval)
  4082. {
  4083. u32 base_low = base & il_beacon_time_mask_low(il,
  4084. il->hw_params.
  4085. beacon_time_tsf_bits);
  4086. u32 addon_low = addon & il_beacon_time_mask_low(il,
  4087. il->hw_params.
  4088. beacon_time_tsf_bits);
  4089. u32 interval = beacon_interval * TIME_UNIT;
  4090. u32 res = (base & il_beacon_time_mask_high(il,
  4091. il->hw_params.
  4092. beacon_time_tsf_bits)) +
  4093. (addon & il_beacon_time_mask_high(il,
  4094. il->hw_params.
  4095. beacon_time_tsf_bits));
  4096. if (base_low > addon_low)
  4097. res += base_low - addon_low;
  4098. else if (base_low < addon_low) {
  4099. res += interval + base_low - addon_low;
  4100. res += (1 << il->hw_params.beacon_time_tsf_bits);
  4101. } else
  4102. res += (1 << il->hw_params.beacon_time_tsf_bits);
  4103. return cpu_to_le32(res);
  4104. }
  4105. EXPORT_SYMBOL(il_add_beacon_time);
  4106. #ifdef CONFIG_PM
  4107. static int
  4108. il_pci_suspend(struct device *device)
  4109. {
  4110. struct pci_dev *pdev = to_pci_dev(device);
  4111. struct il_priv *il = pci_get_drvdata(pdev);
  4112. /*
  4113. * This function is called when system goes into suspend state
  4114. * mac80211 will call il_mac_stop() from the mac80211 suspend function
  4115. * first but since il_mac_stop() has no knowledge of who the caller is,
  4116. * it will not call apm_ops.stop() to stop the DMA operation.
  4117. * Calling apm_ops.stop here to make sure we stop the DMA.
  4118. */
  4119. il_apm_stop(il);
  4120. return 0;
  4121. }
  4122. static int
  4123. il_pci_resume(struct device *device)
  4124. {
  4125. struct pci_dev *pdev = to_pci_dev(device);
  4126. struct il_priv *il = pci_get_drvdata(pdev);
  4127. bool hw_rfkill = false;
  4128. /*
  4129. * We disable the RETRY_TIMEOUT register (0x41) to keep
  4130. * PCI Tx retries from interfering with C3 CPU state.
  4131. */
  4132. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  4133. il_enable_interrupts(il);
  4134. if (!(_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  4135. hw_rfkill = true;
  4136. if (hw_rfkill)
  4137. set_bit(S_RFKILL, &il->status);
  4138. else
  4139. clear_bit(S_RFKILL, &il->status);
  4140. wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rfkill);
  4141. return 0;
  4142. }
  4143. SIMPLE_DEV_PM_OPS(il_pm_ops, il_pci_suspend, il_pci_resume);
  4144. EXPORT_SYMBOL(il_pm_ops);
  4145. #endif /* CONFIG_PM */
  4146. static void
  4147. il_update_qos(struct il_priv *il)
  4148. {
  4149. if (test_bit(S_EXIT_PENDING, &il->status))
  4150. return;
  4151. il->qos_data.def_qos_parm.qos_flags = 0;
  4152. if (il->qos_data.qos_active)
  4153. il->qos_data.def_qos_parm.qos_flags |=
  4154. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  4155. if (il->ht.enabled)
  4156. il->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
  4157. D_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
  4158. il->qos_data.qos_active, il->qos_data.def_qos_parm.qos_flags);
  4159. il_send_cmd_pdu_async(il, C_QOS_PARAM, sizeof(struct il_qosparam_cmd),
  4160. &il->qos_data.def_qos_parm, NULL);
  4161. }
  4162. /**
  4163. * il_mac_config - mac80211 config callback
  4164. */
  4165. int
  4166. il_mac_config(struct ieee80211_hw *hw, u32 changed)
  4167. {
  4168. struct il_priv *il = hw->priv;
  4169. const struct il_channel_info *ch_info;
  4170. struct ieee80211_conf *conf = &hw->conf;
  4171. struct ieee80211_channel *channel = conf->channel;
  4172. struct il_ht_config *ht_conf = &il->current_ht_config;
  4173. unsigned long flags = 0;
  4174. int ret = 0;
  4175. u16 ch;
  4176. int scan_active = 0;
  4177. bool ht_changed = false;
  4178. mutex_lock(&il->mutex);
  4179. D_MAC80211("enter: channel %d changed 0x%X\n", channel->hw_value,
  4180. changed);
  4181. if (unlikely(test_bit(S_SCANNING, &il->status))) {
  4182. scan_active = 1;
  4183. D_MAC80211("scan active\n");
  4184. }
  4185. if (changed &
  4186. (IEEE80211_CONF_CHANGE_SMPS | IEEE80211_CONF_CHANGE_CHANNEL)) {
  4187. /* mac80211 uses static for non-HT which is what we want */
  4188. il->current_ht_config.smps = conf->smps_mode;
  4189. /*
  4190. * Recalculate chain counts.
  4191. *
  4192. * If monitor mode is enabled then mac80211 will
  4193. * set up the SM PS mode to OFF if an HT channel is
  4194. * configured.
  4195. */
  4196. if (il->ops->set_rxon_chain)
  4197. il->ops->set_rxon_chain(il);
  4198. }
  4199. /* during scanning mac80211 will delay channel setting until
  4200. * scan finish with changed = 0
  4201. */
  4202. if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
  4203. if (scan_active)
  4204. goto set_ch_out;
  4205. ch = channel->hw_value;
  4206. ch_info = il_get_channel_info(il, channel->band, ch);
  4207. if (!il_is_channel_valid(ch_info)) {
  4208. D_MAC80211("leave - invalid channel\n");
  4209. ret = -EINVAL;
  4210. goto set_ch_out;
  4211. }
  4212. if (il->iw_mode == NL80211_IFTYPE_ADHOC &&
  4213. !il_is_channel_ibss(ch_info)) {
  4214. D_MAC80211("leave - not IBSS channel\n");
  4215. ret = -EINVAL;
  4216. goto set_ch_out;
  4217. }
  4218. spin_lock_irqsave(&il->lock, flags);
  4219. /* Configure HT40 channels */
  4220. if (il->ht.enabled != conf_is_ht(conf)) {
  4221. il->ht.enabled = conf_is_ht(conf);
  4222. ht_changed = true;
  4223. }
  4224. if (il->ht.enabled) {
  4225. if (conf_is_ht40_minus(conf)) {
  4226. il->ht.extension_chan_offset =
  4227. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  4228. il->ht.is_40mhz = true;
  4229. } else if (conf_is_ht40_plus(conf)) {
  4230. il->ht.extension_chan_offset =
  4231. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  4232. il->ht.is_40mhz = true;
  4233. } else {
  4234. il->ht.extension_chan_offset =
  4235. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  4236. il->ht.is_40mhz = false;
  4237. }
  4238. } else
  4239. il->ht.is_40mhz = false;
  4240. /*
  4241. * Default to no protection. Protection mode will
  4242. * later be set from BSS config in il_ht_conf
  4243. */
  4244. il->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  4245. /* if we are switching from ht to 2.4 clear flags
  4246. * from any ht related info since 2.4 does not
  4247. * support ht */
  4248. if ((le16_to_cpu(il->staging.channel) != ch))
  4249. il->staging.flags = 0;
  4250. il_set_rxon_channel(il, channel);
  4251. il_set_rxon_ht(il, ht_conf);
  4252. il_set_flags_for_band(il, channel->band, il->vif);
  4253. spin_unlock_irqrestore(&il->lock, flags);
  4254. if (il->ops->update_bcast_stations)
  4255. ret = il->ops->update_bcast_stations(il);
  4256. set_ch_out:
  4257. /* The list of supported rates and rate mask can be different
  4258. * for each band; since the band may have changed, reset
  4259. * the rate mask to what mac80211 lists */
  4260. il_set_rate(il);
  4261. }
  4262. if (changed & (IEEE80211_CONF_CHANGE_PS | IEEE80211_CONF_CHANGE_IDLE)) {
  4263. ret = il_power_update_mode(il, false);
  4264. if (ret)
  4265. D_MAC80211("Error setting sleep level\n");
  4266. }
  4267. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  4268. D_MAC80211("TX Power old=%d new=%d\n", il->tx_power_user_lmt,
  4269. conf->power_level);
  4270. il_set_tx_power(il, conf->power_level, false);
  4271. }
  4272. if (!il_is_ready(il)) {
  4273. D_MAC80211("leave - not ready\n");
  4274. goto out;
  4275. }
  4276. if (scan_active)
  4277. goto out;
  4278. if (memcmp(&il->active, &il->staging, sizeof(il->staging)))
  4279. il_commit_rxon(il);
  4280. else
  4281. D_INFO("Not re-sending same RXON configuration.\n");
  4282. if (ht_changed)
  4283. il_update_qos(il);
  4284. out:
  4285. D_MAC80211("leave ret %d\n", ret);
  4286. mutex_unlock(&il->mutex);
  4287. return ret;
  4288. }
  4289. EXPORT_SYMBOL(il_mac_config);
  4290. void
  4291. il_mac_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  4292. {
  4293. struct il_priv *il = hw->priv;
  4294. unsigned long flags;
  4295. mutex_lock(&il->mutex);
  4296. D_MAC80211("enter: type %d, addr %pM\n", vif->type, vif->addr);
  4297. spin_lock_irqsave(&il->lock, flags);
  4298. memset(&il->current_ht_config, 0, sizeof(struct il_ht_config));
  4299. /* new association get rid of ibss beacon skb */
  4300. if (il->beacon_skb)
  4301. dev_kfree_skb(il->beacon_skb);
  4302. il->beacon_skb = NULL;
  4303. il->timestamp = 0;
  4304. spin_unlock_irqrestore(&il->lock, flags);
  4305. il_scan_cancel_timeout(il, 100);
  4306. if (!il_is_ready_rf(il)) {
  4307. D_MAC80211("leave - not ready\n");
  4308. mutex_unlock(&il->mutex);
  4309. return;
  4310. }
  4311. /* we are restarting association process */
  4312. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4313. il_commit_rxon(il);
  4314. il_set_rate(il);
  4315. D_MAC80211("leave\n");
  4316. mutex_unlock(&il->mutex);
  4317. }
  4318. EXPORT_SYMBOL(il_mac_reset_tsf);
  4319. static void
  4320. il_ht_conf(struct il_priv *il, struct ieee80211_vif *vif)
  4321. {
  4322. struct il_ht_config *ht_conf = &il->current_ht_config;
  4323. struct ieee80211_sta *sta;
  4324. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  4325. D_ASSOC("enter:\n");
  4326. if (!il->ht.enabled)
  4327. return;
  4328. il->ht.protection =
  4329. bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
  4330. il->ht.non_gf_sta_present =
  4331. !!(bss_conf->
  4332. ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  4333. ht_conf->single_chain_sufficient = false;
  4334. switch (vif->type) {
  4335. case NL80211_IFTYPE_STATION:
  4336. rcu_read_lock();
  4337. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  4338. if (sta) {
  4339. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  4340. int maxstreams;
  4341. maxstreams =
  4342. (ht_cap->mcs.
  4343. tx_params & IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  4344. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  4345. maxstreams += 1;
  4346. if (ht_cap->mcs.rx_mask[1] == 0 &&
  4347. ht_cap->mcs.rx_mask[2] == 0)
  4348. ht_conf->single_chain_sufficient = true;
  4349. if (maxstreams <= 1)
  4350. ht_conf->single_chain_sufficient = true;
  4351. } else {
  4352. /*
  4353. * If at all, this can only happen through a race
  4354. * when the AP disconnects us while we're still
  4355. * setting up the connection, in that case mac80211
  4356. * will soon tell us about that.
  4357. */
  4358. ht_conf->single_chain_sufficient = true;
  4359. }
  4360. rcu_read_unlock();
  4361. break;
  4362. case NL80211_IFTYPE_ADHOC:
  4363. ht_conf->single_chain_sufficient = true;
  4364. break;
  4365. default:
  4366. break;
  4367. }
  4368. D_ASSOC("leave\n");
  4369. }
  4370. static inline void
  4371. il_set_no_assoc(struct il_priv *il, struct ieee80211_vif *vif)
  4372. {
  4373. /*
  4374. * inform the ucode that there is no longer an
  4375. * association and that no more packets should be
  4376. * sent
  4377. */
  4378. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4379. il->staging.assoc_id = 0;
  4380. il_commit_rxon(il);
  4381. }
  4382. static void
  4383. il_beacon_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  4384. {
  4385. struct il_priv *il = hw->priv;
  4386. unsigned long flags;
  4387. __le64 timestamp;
  4388. struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
  4389. if (!skb)
  4390. return;
  4391. D_MAC80211("enter\n");
  4392. lockdep_assert_held(&il->mutex);
  4393. if (!il->beacon_enabled) {
  4394. IL_ERR("update beacon with no beaconing enabled\n");
  4395. dev_kfree_skb(skb);
  4396. return;
  4397. }
  4398. spin_lock_irqsave(&il->lock, flags);
  4399. if (il->beacon_skb)
  4400. dev_kfree_skb(il->beacon_skb);
  4401. il->beacon_skb = skb;
  4402. timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
  4403. il->timestamp = le64_to_cpu(timestamp);
  4404. D_MAC80211("leave\n");
  4405. spin_unlock_irqrestore(&il->lock, flags);
  4406. if (!il_is_ready_rf(il)) {
  4407. D_MAC80211("leave - RF not ready\n");
  4408. return;
  4409. }
  4410. il->ops->post_associate(il);
  4411. }
  4412. void
  4413. il_mac_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4414. struct ieee80211_bss_conf *bss_conf, u32 changes)
  4415. {
  4416. struct il_priv *il = hw->priv;
  4417. int ret;
  4418. mutex_lock(&il->mutex);
  4419. D_MAC80211("enter: changes 0x%x\n", changes);
  4420. if (!il_is_alive(il)) {
  4421. D_MAC80211("leave - not alive\n");
  4422. mutex_unlock(&il->mutex);
  4423. return;
  4424. }
  4425. if (changes & BSS_CHANGED_QOS) {
  4426. unsigned long flags;
  4427. spin_lock_irqsave(&il->lock, flags);
  4428. il->qos_data.qos_active = bss_conf->qos;
  4429. il_update_qos(il);
  4430. spin_unlock_irqrestore(&il->lock, flags);
  4431. }
  4432. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  4433. /* FIXME: can we remove beacon_enabled ? */
  4434. if (vif->bss_conf.enable_beacon)
  4435. il->beacon_enabled = true;
  4436. else
  4437. il->beacon_enabled = false;
  4438. }
  4439. if (changes & BSS_CHANGED_BSSID) {
  4440. D_MAC80211("BSSID %pM\n", bss_conf->bssid);
  4441. /*
  4442. * If there is currently a HW scan going on in the background,
  4443. * then we need to cancel it, otherwise sometimes we are not
  4444. * able to authenticate (FIXME: why ?)
  4445. */
  4446. if (il_scan_cancel_timeout(il, 100)) {
  4447. D_MAC80211("leave - scan abort failed\n");
  4448. mutex_unlock(&il->mutex);
  4449. return;
  4450. }
  4451. /* mac80211 only sets assoc when in STATION mode */
  4452. memcpy(il->staging.bssid_addr, bss_conf->bssid, ETH_ALEN);
  4453. /* FIXME: currently needed in a few places */
  4454. memcpy(il->bssid, bss_conf->bssid, ETH_ALEN);
  4455. }
  4456. /*
  4457. * This needs to be after setting the BSSID in case
  4458. * mac80211 decides to do both changes at once because
  4459. * it will invoke post_associate.
  4460. */
  4461. if (vif->type == NL80211_IFTYPE_ADHOC && (changes & BSS_CHANGED_BEACON))
  4462. il_beacon_update(hw, vif);
  4463. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  4464. D_MAC80211("ERP_PREAMBLE %d\n", bss_conf->use_short_preamble);
  4465. if (bss_conf->use_short_preamble)
  4466. il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  4467. else
  4468. il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  4469. }
  4470. if (changes & BSS_CHANGED_ERP_CTS_PROT) {
  4471. D_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
  4472. if (bss_conf->use_cts_prot && il->band != IEEE80211_BAND_5GHZ)
  4473. il->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
  4474. else
  4475. il->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  4476. if (bss_conf->use_cts_prot)
  4477. il->staging.flags |= RXON_FLG_SELF_CTS_EN;
  4478. else
  4479. il->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
  4480. }
  4481. if (changes & BSS_CHANGED_BASIC_RATES) {
  4482. /* XXX use this information
  4483. *
  4484. * To do that, remove code from il_set_rate() and put something
  4485. * like this here:
  4486. *
  4487. if (A-band)
  4488. il->staging.ofdm_basic_rates =
  4489. bss_conf->basic_rates;
  4490. else
  4491. il->staging.ofdm_basic_rates =
  4492. bss_conf->basic_rates >> 4;
  4493. il->staging.cck_basic_rates =
  4494. bss_conf->basic_rates & 0xF;
  4495. */
  4496. }
  4497. if (changes & BSS_CHANGED_HT) {
  4498. il_ht_conf(il, vif);
  4499. if (il->ops->set_rxon_chain)
  4500. il->ops->set_rxon_chain(il);
  4501. }
  4502. if (changes & BSS_CHANGED_ASSOC) {
  4503. D_MAC80211("ASSOC %d\n", bss_conf->assoc);
  4504. if (bss_conf->assoc) {
  4505. il->timestamp = bss_conf->sync_tsf;
  4506. if (!il_is_rfkill(il))
  4507. il->ops->post_associate(il);
  4508. } else
  4509. il_set_no_assoc(il, vif);
  4510. }
  4511. if (changes && il_is_associated(il) && bss_conf->aid) {
  4512. D_MAC80211("Changes (%#x) while associated\n", changes);
  4513. ret = il_send_rxon_assoc(il);
  4514. if (!ret) {
  4515. /* Sync active_rxon with latest change. */
  4516. memcpy((void *)&il->active, &il->staging,
  4517. sizeof(struct il_rxon_cmd));
  4518. }
  4519. }
  4520. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  4521. if (vif->bss_conf.enable_beacon) {
  4522. memcpy(il->staging.bssid_addr, bss_conf->bssid,
  4523. ETH_ALEN);
  4524. memcpy(il->bssid, bss_conf->bssid, ETH_ALEN);
  4525. il->ops->config_ap(il);
  4526. } else
  4527. il_set_no_assoc(il, vif);
  4528. }
  4529. if (changes & BSS_CHANGED_IBSS) {
  4530. ret = il->ops->manage_ibss_station(il, vif,
  4531. bss_conf->ibss_joined);
  4532. if (ret)
  4533. IL_ERR("failed to %s IBSS station %pM\n",
  4534. bss_conf->ibss_joined ? "add" : "remove",
  4535. bss_conf->bssid);
  4536. }
  4537. D_MAC80211("leave\n");
  4538. mutex_unlock(&il->mutex);
  4539. }
  4540. EXPORT_SYMBOL(il_mac_bss_info_changed);
  4541. irqreturn_t
  4542. il_isr(int irq, void *data)
  4543. {
  4544. struct il_priv *il = data;
  4545. u32 inta, inta_mask;
  4546. u32 inta_fh;
  4547. unsigned long flags;
  4548. if (!il)
  4549. return IRQ_NONE;
  4550. spin_lock_irqsave(&il->lock, flags);
  4551. /* Disable (but don't clear!) interrupts here to avoid
  4552. * back-to-back ISRs and sporadic interrupts from our NIC.
  4553. * If we have something to service, the tasklet will re-enable ints.
  4554. * If we *don't* have something, we'll re-enable before leaving here. */
  4555. inta_mask = _il_rd(il, CSR_INT_MASK); /* just for debug */
  4556. _il_wr(il, CSR_INT_MASK, 0x00000000);
  4557. /* Discover which interrupts are active/pending */
  4558. inta = _il_rd(il, CSR_INT);
  4559. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  4560. /* Ignore interrupt if there's nothing in NIC to service.
  4561. * This may be due to IRQ shared with another device,
  4562. * or due to sporadic interrupts thrown from our NIC. */
  4563. if (!inta && !inta_fh) {
  4564. D_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
  4565. goto none;
  4566. }
  4567. if (inta == 0xFFFFFFFF || (inta & 0xFFFFFFF0) == 0xa5a5a5a0) {
  4568. /* Hardware disappeared. It might have already raised
  4569. * an interrupt */
  4570. IL_WARN("HARDWARE GONE?? INTA == 0x%08x\n", inta);
  4571. goto unplugged;
  4572. }
  4573. D_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta, inta_mask,
  4574. inta_fh);
  4575. inta &= ~CSR_INT_BIT_SCD;
  4576. /* il_irq_tasklet() will service interrupts and re-enable them */
  4577. if (likely(inta || inta_fh))
  4578. tasklet_schedule(&il->irq_tasklet);
  4579. unplugged:
  4580. spin_unlock_irqrestore(&il->lock, flags);
  4581. return IRQ_HANDLED;
  4582. none:
  4583. /* re-enable interrupts here since we don't have anything to service. */
  4584. /* only Re-enable if disabled by irq */
  4585. if (test_bit(S_INT_ENABLED, &il->status))
  4586. il_enable_interrupts(il);
  4587. spin_unlock_irqrestore(&il->lock, flags);
  4588. return IRQ_NONE;
  4589. }
  4590. EXPORT_SYMBOL(il_isr);
  4591. /*
  4592. * il_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
  4593. * function.
  4594. */
  4595. void
  4596. il_tx_cmd_protection(struct il_priv *il, struct ieee80211_tx_info *info,
  4597. __le16 fc, __le32 *tx_flags)
  4598. {
  4599. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  4600. *tx_flags |= TX_CMD_FLG_RTS_MSK;
  4601. *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  4602. *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  4603. if (!ieee80211_is_mgmt(fc))
  4604. return;
  4605. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  4606. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  4607. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  4608. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  4609. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  4610. *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  4611. *tx_flags |= TX_CMD_FLG_CTS_MSK;
  4612. break;
  4613. }
  4614. } else if (info->control.rates[0].
  4615. flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  4616. *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  4617. *tx_flags |= TX_CMD_FLG_CTS_MSK;
  4618. *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  4619. }
  4620. }
  4621. EXPORT_SYMBOL(il_tx_cmd_protection);