mac.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "mac.h"
  18. #include <net/mac80211.h>
  19. #include <linux/etherdevice.h>
  20. #include "hif.h"
  21. #include "core.h"
  22. #include "debug.h"
  23. #include "wmi.h"
  24. #include "htt.h"
  25. #include "txrx.h"
  26. /**********/
  27. /* Crypto */
  28. /**********/
  29. static int ath10k_send_key(struct ath10k_vif *arvif,
  30. struct ieee80211_key_conf *key,
  31. enum set_key_cmd cmd,
  32. const u8 *macaddr)
  33. {
  34. struct wmi_vdev_install_key_arg arg = {
  35. .vdev_id = arvif->vdev_id,
  36. .key_idx = key->keyidx,
  37. .key_len = key->keylen,
  38. .key_data = key->key,
  39. .macaddr = macaddr,
  40. };
  41. lockdep_assert_held(&arvif->ar->conf_mutex);
  42. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  43. arg.key_flags = WMI_KEY_PAIRWISE;
  44. else
  45. arg.key_flags = WMI_KEY_GROUP;
  46. switch (key->cipher) {
  47. case WLAN_CIPHER_SUITE_CCMP:
  48. arg.key_cipher = WMI_CIPHER_AES_CCM;
  49. if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
  50. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
  51. else
  52. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  53. break;
  54. case WLAN_CIPHER_SUITE_TKIP:
  55. arg.key_cipher = WMI_CIPHER_TKIP;
  56. arg.key_txmic_len = 8;
  57. arg.key_rxmic_len = 8;
  58. break;
  59. case WLAN_CIPHER_SUITE_WEP40:
  60. case WLAN_CIPHER_SUITE_WEP104:
  61. arg.key_cipher = WMI_CIPHER_WEP;
  62. /* AP/IBSS mode requires self-key to be groupwise
  63. * Otherwise pairwise key must be set */
  64. if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
  65. arg.key_flags = WMI_KEY_PAIRWISE;
  66. break;
  67. default:
  68. ath10k_warn("cipher %d is not supported\n", key->cipher);
  69. return -EOPNOTSUPP;
  70. }
  71. if (cmd == DISABLE_KEY) {
  72. arg.key_cipher = WMI_CIPHER_NONE;
  73. arg.key_data = NULL;
  74. }
  75. return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
  76. }
  77. static int ath10k_install_key(struct ath10k_vif *arvif,
  78. struct ieee80211_key_conf *key,
  79. enum set_key_cmd cmd,
  80. const u8 *macaddr)
  81. {
  82. struct ath10k *ar = arvif->ar;
  83. int ret;
  84. lockdep_assert_held(&ar->conf_mutex);
  85. reinit_completion(&ar->install_key_done);
  86. ret = ath10k_send_key(arvif, key, cmd, macaddr);
  87. if (ret)
  88. return ret;
  89. ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
  90. if (ret == 0)
  91. return -ETIMEDOUT;
  92. return 0;
  93. }
  94. static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
  95. const u8 *addr)
  96. {
  97. struct ath10k *ar = arvif->ar;
  98. struct ath10k_peer *peer;
  99. int ret;
  100. int i;
  101. lockdep_assert_held(&ar->conf_mutex);
  102. spin_lock_bh(&ar->data_lock);
  103. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  104. spin_unlock_bh(&ar->data_lock);
  105. if (!peer)
  106. return -ENOENT;
  107. for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
  108. if (arvif->wep_keys[i] == NULL)
  109. continue;
  110. ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
  111. addr);
  112. if (ret)
  113. return ret;
  114. peer->keys[i] = arvif->wep_keys[i];
  115. }
  116. return 0;
  117. }
  118. static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
  119. const u8 *addr)
  120. {
  121. struct ath10k *ar = arvif->ar;
  122. struct ath10k_peer *peer;
  123. int first_errno = 0;
  124. int ret;
  125. int i;
  126. lockdep_assert_held(&ar->conf_mutex);
  127. spin_lock_bh(&ar->data_lock);
  128. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  129. spin_unlock_bh(&ar->data_lock);
  130. if (!peer)
  131. return -ENOENT;
  132. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  133. if (peer->keys[i] == NULL)
  134. continue;
  135. ret = ath10k_install_key(arvif, peer->keys[i],
  136. DISABLE_KEY, addr);
  137. if (ret && first_errno == 0)
  138. first_errno = ret;
  139. if (ret)
  140. ath10k_warn("failed to remove peer wep key %d: %d\n",
  141. i, ret);
  142. peer->keys[i] = NULL;
  143. }
  144. return first_errno;
  145. }
  146. static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
  147. struct ieee80211_key_conf *key)
  148. {
  149. struct ath10k *ar = arvif->ar;
  150. struct ath10k_peer *peer;
  151. u8 addr[ETH_ALEN];
  152. int first_errno = 0;
  153. int ret;
  154. int i;
  155. lockdep_assert_held(&ar->conf_mutex);
  156. for (;;) {
  157. /* since ath10k_install_key we can't hold data_lock all the
  158. * time, so we try to remove the keys incrementally */
  159. spin_lock_bh(&ar->data_lock);
  160. i = 0;
  161. list_for_each_entry(peer, &ar->peers, list) {
  162. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  163. if (peer->keys[i] == key) {
  164. memcpy(addr, peer->addr, ETH_ALEN);
  165. peer->keys[i] = NULL;
  166. break;
  167. }
  168. }
  169. if (i < ARRAY_SIZE(peer->keys))
  170. break;
  171. }
  172. spin_unlock_bh(&ar->data_lock);
  173. if (i == ARRAY_SIZE(peer->keys))
  174. break;
  175. ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
  176. if (ret && first_errno == 0)
  177. first_errno = ret;
  178. if (ret)
  179. ath10k_warn("failed to remove key for %pM: %d\n",
  180. addr, ret);
  181. }
  182. return first_errno;
  183. }
  184. /*********************/
  185. /* General utilities */
  186. /*********************/
  187. static inline enum wmi_phy_mode
  188. chan_to_phymode(const struct cfg80211_chan_def *chandef)
  189. {
  190. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  191. switch (chandef->chan->band) {
  192. case IEEE80211_BAND_2GHZ:
  193. switch (chandef->width) {
  194. case NL80211_CHAN_WIDTH_20_NOHT:
  195. phymode = MODE_11G;
  196. break;
  197. case NL80211_CHAN_WIDTH_20:
  198. phymode = MODE_11NG_HT20;
  199. break;
  200. case NL80211_CHAN_WIDTH_40:
  201. phymode = MODE_11NG_HT40;
  202. break;
  203. case NL80211_CHAN_WIDTH_5:
  204. case NL80211_CHAN_WIDTH_10:
  205. case NL80211_CHAN_WIDTH_80:
  206. case NL80211_CHAN_WIDTH_80P80:
  207. case NL80211_CHAN_WIDTH_160:
  208. phymode = MODE_UNKNOWN;
  209. break;
  210. }
  211. break;
  212. case IEEE80211_BAND_5GHZ:
  213. switch (chandef->width) {
  214. case NL80211_CHAN_WIDTH_20_NOHT:
  215. phymode = MODE_11A;
  216. break;
  217. case NL80211_CHAN_WIDTH_20:
  218. phymode = MODE_11NA_HT20;
  219. break;
  220. case NL80211_CHAN_WIDTH_40:
  221. phymode = MODE_11NA_HT40;
  222. break;
  223. case NL80211_CHAN_WIDTH_80:
  224. phymode = MODE_11AC_VHT80;
  225. break;
  226. case NL80211_CHAN_WIDTH_5:
  227. case NL80211_CHAN_WIDTH_10:
  228. case NL80211_CHAN_WIDTH_80P80:
  229. case NL80211_CHAN_WIDTH_160:
  230. phymode = MODE_UNKNOWN;
  231. break;
  232. }
  233. break;
  234. default:
  235. break;
  236. }
  237. WARN_ON(phymode == MODE_UNKNOWN);
  238. return phymode;
  239. }
  240. static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
  241. {
  242. /*
  243. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  244. * 0 for no restriction
  245. * 1 for 1/4 us
  246. * 2 for 1/2 us
  247. * 3 for 1 us
  248. * 4 for 2 us
  249. * 5 for 4 us
  250. * 6 for 8 us
  251. * 7 for 16 us
  252. */
  253. switch (mpdudensity) {
  254. case 0:
  255. return 0;
  256. case 1:
  257. case 2:
  258. case 3:
  259. /* Our lower layer calculations limit our precision to
  260. 1 microsecond */
  261. return 1;
  262. case 4:
  263. return 2;
  264. case 5:
  265. return 4;
  266. case 6:
  267. return 8;
  268. case 7:
  269. return 16;
  270. default:
  271. return 0;
  272. }
  273. }
  274. static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
  275. {
  276. int ret;
  277. lockdep_assert_held(&ar->conf_mutex);
  278. ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
  279. if (ret) {
  280. ath10k_warn("failed to create wmi peer %pM on vdev %i: %i\n",
  281. addr, vdev_id, ret);
  282. return ret;
  283. }
  284. ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
  285. if (ret) {
  286. ath10k_warn("failed to wait for created wmi peer %pM on vdev %i: %i\n",
  287. addr, vdev_id, ret);
  288. return ret;
  289. }
  290. spin_lock_bh(&ar->data_lock);
  291. ar->num_peers++;
  292. spin_unlock_bh(&ar->data_lock);
  293. return 0;
  294. }
  295. static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
  296. {
  297. struct ath10k *ar = arvif->ar;
  298. u32 param;
  299. int ret;
  300. param = ar->wmi.pdev_param->sta_kickout_th;
  301. ret = ath10k_wmi_pdev_set_param(ar, param,
  302. ATH10K_KICKOUT_THRESHOLD);
  303. if (ret) {
  304. ath10k_warn("failed to set kickout threshold on vdev %i: %d\n",
  305. arvif->vdev_id, ret);
  306. return ret;
  307. }
  308. param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
  309. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  310. ATH10K_KEEPALIVE_MIN_IDLE);
  311. if (ret) {
  312. ath10k_warn("failed to set keepalive minimum idle time on vdev %i: %d\n",
  313. arvif->vdev_id, ret);
  314. return ret;
  315. }
  316. param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
  317. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  318. ATH10K_KEEPALIVE_MAX_IDLE);
  319. if (ret) {
  320. ath10k_warn("failed to set keepalive maximum idle time on vdev %i: %d\n",
  321. arvif->vdev_id, ret);
  322. return ret;
  323. }
  324. param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
  325. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
  326. ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
  327. if (ret) {
  328. ath10k_warn("failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
  329. arvif->vdev_id, ret);
  330. return ret;
  331. }
  332. return 0;
  333. }
  334. static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
  335. {
  336. struct ath10k *ar = arvif->ar;
  337. u32 vdev_param;
  338. if (value != 0xFFFFFFFF)
  339. value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
  340. ATH10K_RTS_MAX);
  341. vdev_param = ar->wmi.vdev_param->rts_threshold;
  342. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
  343. }
  344. static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
  345. {
  346. struct ath10k *ar = arvif->ar;
  347. u32 vdev_param;
  348. if (value != 0xFFFFFFFF)
  349. value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
  350. ATH10K_FRAGMT_THRESHOLD_MIN,
  351. ATH10K_FRAGMT_THRESHOLD_MAX);
  352. vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
  353. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
  354. }
  355. static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
  356. {
  357. int ret;
  358. lockdep_assert_held(&ar->conf_mutex);
  359. ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
  360. if (ret)
  361. return ret;
  362. ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
  363. if (ret)
  364. return ret;
  365. spin_lock_bh(&ar->data_lock);
  366. ar->num_peers--;
  367. spin_unlock_bh(&ar->data_lock);
  368. return 0;
  369. }
  370. static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
  371. {
  372. struct ath10k_peer *peer, *tmp;
  373. lockdep_assert_held(&ar->conf_mutex);
  374. spin_lock_bh(&ar->data_lock);
  375. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  376. if (peer->vdev_id != vdev_id)
  377. continue;
  378. ath10k_warn("removing stale peer %pM from vdev_id %d\n",
  379. peer->addr, vdev_id);
  380. list_del(&peer->list);
  381. kfree(peer);
  382. ar->num_peers--;
  383. }
  384. spin_unlock_bh(&ar->data_lock);
  385. }
  386. static void ath10k_peer_cleanup_all(struct ath10k *ar)
  387. {
  388. struct ath10k_peer *peer, *tmp;
  389. lockdep_assert_held(&ar->conf_mutex);
  390. spin_lock_bh(&ar->data_lock);
  391. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  392. list_del(&peer->list);
  393. kfree(peer);
  394. }
  395. ar->num_peers = 0;
  396. spin_unlock_bh(&ar->data_lock);
  397. }
  398. /************************/
  399. /* Interface management */
  400. /************************/
  401. static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
  402. {
  403. int ret;
  404. lockdep_assert_held(&ar->conf_mutex);
  405. ret = wait_for_completion_timeout(&ar->vdev_setup_done,
  406. ATH10K_VDEV_SETUP_TIMEOUT_HZ);
  407. if (ret == 0)
  408. return -ETIMEDOUT;
  409. return 0;
  410. }
  411. static bool ath10k_monitor_is_enabled(struct ath10k *ar)
  412. {
  413. lockdep_assert_held(&ar->conf_mutex);
  414. ath10k_dbg(ATH10K_DBG_MAC,
  415. "mac monitor refs: promisc %d monitor %d cac %d\n",
  416. ar->promisc, ar->monitor,
  417. test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags));
  418. return ar->promisc || ar->monitor ||
  419. test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  420. }
  421. static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
  422. {
  423. struct cfg80211_chan_def *chandef = &ar->chandef;
  424. struct ieee80211_channel *channel = chandef->chan;
  425. struct wmi_vdev_start_request_arg arg = {};
  426. int ret = 0;
  427. lockdep_assert_held(&ar->conf_mutex);
  428. arg.vdev_id = vdev_id;
  429. arg.channel.freq = channel->center_freq;
  430. arg.channel.band_center_freq1 = chandef->center_freq1;
  431. /* TODO setup this dynamically, what in case we
  432. don't have any vifs? */
  433. arg.channel.mode = chan_to_phymode(chandef);
  434. arg.channel.chan_radar =
  435. !!(channel->flags & IEEE80211_CHAN_RADAR);
  436. arg.channel.min_power = 0;
  437. arg.channel.max_power = channel->max_power * 2;
  438. arg.channel.max_reg_power = channel->max_reg_power * 2;
  439. arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
  440. ret = ath10k_wmi_vdev_start(ar, &arg);
  441. if (ret) {
  442. ath10k_warn("failed to request monitor vdev %i start: %d\n",
  443. vdev_id, ret);
  444. return ret;
  445. }
  446. ret = ath10k_vdev_setup_sync(ar);
  447. if (ret) {
  448. ath10k_warn("failed to synchronize setup for monitor vdev %i: %d\n",
  449. vdev_id, ret);
  450. return ret;
  451. }
  452. ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
  453. if (ret) {
  454. ath10k_warn("failed to put up monitor vdev %i: %d\n",
  455. vdev_id, ret);
  456. goto vdev_stop;
  457. }
  458. ar->monitor_vdev_id = vdev_id;
  459. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
  460. ar->monitor_vdev_id);
  461. return 0;
  462. vdev_stop:
  463. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  464. if (ret)
  465. ath10k_warn("failed to stop monitor vdev %i after start failure: %d\n",
  466. ar->monitor_vdev_id, ret);
  467. return ret;
  468. }
  469. static int ath10k_monitor_vdev_stop(struct ath10k *ar)
  470. {
  471. int ret = 0;
  472. lockdep_assert_held(&ar->conf_mutex);
  473. ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
  474. if (ret)
  475. ath10k_warn("failed to put down monitor vdev %i: %d\n",
  476. ar->monitor_vdev_id, ret);
  477. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  478. if (ret)
  479. ath10k_warn("failed to to request monitor vdev %i stop: %d\n",
  480. ar->monitor_vdev_id, ret);
  481. ret = ath10k_vdev_setup_sync(ar);
  482. if (ret)
  483. ath10k_warn("failed to synchronise monitor vdev %i: %d\n",
  484. ar->monitor_vdev_id, ret);
  485. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
  486. ar->monitor_vdev_id);
  487. return ret;
  488. }
  489. static int ath10k_monitor_vdev_create(struct ath10k *ar)
  490. {
  491. int bit, ret = 0;
  492. lockdep_assert_held(&ar->conf_mutex);
  493. bit = ffs(ar->free_vdev_map);
  494. if (bit == 0) {
  495. ath10k_warn("failed to find free vdev id for monitor vdev\n");
  496. return -ENOMEM;
  497. }
  498. ar->monitor_vdev_id = bit - 1;
  499. ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
  500. ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
  501. WMI_VDEV_TYPE_MONITOR,
  502. 0, ar->mac_addr);
  503. if (ret) {
  504. ath10k_warn("failed to request monitor vdev %i creation: %d\n",
  505. ar->monitor_vdev_id, ret);
  506. goto vdev_fail;
  507. }
  508. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
  509. ar->monitor_vdev_id);
  510. return 0;
  511. vdev_fail:
  512. /*
  513. * Restore the ID to the global map.
  514. */
  515. ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
  516. return ret;
  517. }
  518. static int ath10k_monitor_vdev_delete(struct ath10k *ar)
  519. {
  520. int ret = 0;
  521. lockdep_assert_held(&ar->conf_mutex);
  522. ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
  523. if (ret) {
  524. ath10k_warn("failed to request wmi monitor vdev %i removal: %d\n",
  525. ar->monitor_vdev_id, ret);
  526. return ret;
  527. }
  528. ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
  529. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
  530. ar->monitor_vdev_id);
  531. return ret;
  532. }
  533. static int ath10k_monitor_start(struct ath10k *ar)
  534. {
  535. int ret;
  536. lockdep_assert_held(&ar->conf_mutex);
  537. if (!ath10k_monitor_is_enabled(ar)) {
  538. ath10k_warn("trying to start monitor with no references\n");
  539. return 0;
  540. }
  541. if (ar->monitor_started) {
  542. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor already started\n");
  543. return 0;
  544. }
  545. ret = ath10k_monitor_vdev_create(ar);
  546. if (ret) {
  547. ath10k_warn("failed to create monitor vdev: %d\n", ret);
  548. return ret;
  549. }
  550. ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
  551. if (ret) {
  552. ath10k_warn("failed to start monitor vdev: %d\n", ret);
  553. ath10k_monitor_vdev_delete(ar);
  554. return ret;
  555. }
  556. ar->monitor_started = true;
  557. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor started\n");
  558. return 0;
  559. }
  560. static void ath10k_monitor_stop(struct ath10k *ar)
  561. {
  562. int ret;
  563. lockdep_assert_held(&ar->conf_mutex);
  564. if (ath10k_monitor_is_enabled(ar)) {
  565. ath10k_dbg(ATH10K_DBG_MAC,
  566. "mac monitor will be stopped later\n");
  567. return;
  568. }
  569. if (!ar->monitor_started) {
  570. ath10k_dbg(ATH10K_DBG_MAC,
  571. "mac monitor probably failed to start earlier\n");
  572. return;
  573. }
  574. ret = ath10k_monitor_vdev_stop(ar);
  575. if (ret)
  576. ath10k_warn("failed to stop monitor vdev: %d\n", ret);
  577. ret = ath10k_monitor_vdev_delete(ar);
  578. if (ret)
  579. ath10k_warn("failed to delete monitor vdev: %d\n", ret);
  580. ar->monitor_started = false;
  581. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor stopped\n");
  582. }
  583. static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
  584. {
  585. struct ath10k *ar = arvif->ar;
  586. u32 vdev_param, rts_cts = 0;
  587. lockdep_assert_held(&ar->conf_mutex);
  588. vdev_param = ar->wmi.vdev_param->enable_rtscts;
  589. if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
  590. rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
  591. if (arvif->num_legacy_stations > 0)
  592. rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
  593. WMI_RTSCTS_PROFILE);
  594. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  595. rts_cts);
  596. }
  597. static int ath10k_start_cac(struct ath10k *ar)
  598. {
  599. int ret;
  600. lockdep_assert_held(&ar->conf_mutex);
  601. set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  602. ret = ath10k_monitor_start(ar);
  603. if (ret) {
  604. ath10k_warn("failed to start monitor (cac): %d\n", ret);
  605. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  606. return ret;
  607. }
  608. ath10k_dbg(ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
  609. ar->monitor_vdev_id);
  610. return 0;
  611. }
  612. static int ath10k_stop_cac(struct ath10k *ar)
  613. {
  614. lockdep_assert_held(&ar->conf_mutex);
  615. /* CAC is not running - do nothing */
  616. if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
  617. return 0;
  618. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  619. ath10k_monitor_stop(ar);
  620. ath10k_dbg(ATH10K_DBG_MAC, "mac cac finished\n");
  621. return 0;
  622. }
  623. static void ath10k_recalc_radar_detection(struct ath10k *ar)
  624. {
  625. int ret;
  626. lockdep_assert_held(&ar->conf_mutex);
  627. ath10k_stop_cac(ar);
  628. if (!ar->radar_enabled)
  629. return;
  630. if (ar->num_started_vdevs > 0)
  631. return;
  632. ret = ath10k_start_cac(ar);
  633. if (ret) {
  634. /*
  635. * Not possible to start CAC on current channel so starting
  636. * radiation is not allowed, make this channel DFS_UNAVAILABLE
  637. * by indicating that radar was detected.
  638. */
  639. ath10k_warn("failed to start CAC: %d\n", ret);
  640. ieee80211_radar_detected(ar->hw);
  641. }
  642. }
  643. static int ath10k_vdev_start(struct ath10k_vif *arvif)
  644. {
  645. struct ath10k *ar = arvif->ar;
  646. struct cfg80211_chan_def *chandef = &ar->chandef;
  647. struct wmi_vdev_start_request_arg arg = {};
  648. int ret = 0;
  649. lockdep_assert_held(&ar->conf_mutex);
  650. reinit_completion(&ar->vdev_setup_done);
  651. arg.vdev_id = arvif->vdev_id;
  652. arg.dtim_period = arvif->dtim_period;
  653. arg.bcn_intval = arvif->beacon_interval;
  654. arg.channel.freq = chandef->chan->center_freq;
  655. arg.channel.band_center_freq1 = chandef->center_freq1;
  656. arg.channel.mode = chan_to_phymode(chandef);
  657. arg.channel.min_power = 0;
  658. arg.channel.max_power = chandef->chan->max_power * 2;
  659. arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
  660. arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
  661. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  662. arg.ssid = arvif->u.ap.ssid;
  663. arg.ssid_len = arvif->u.ap.ssid_len;
  664. arg.hidden_ssid = arvif->u.ap.hidden_ssid;
  665. /* For now allow DFS for AP mode */
  666. arg.channel.chan_radar =
  667. !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
  668. } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  669. arg.ssid = arvif->vif->bss_conf.ssid;
  670. arg.ssid_len = arvif->vif->bss_conf.ssid_len;
  671. }
  672. ath10k_dbg(ATH10K_DBG_MAC,
  673. "mac vdev %d start center_freq %d phymode %s\n",
  674. arg.vdev_id, arg.channel.freq,
  675. ath10k_wmi_phymode_str(arg.channel.mode));
  676. ret = ath10k_wmi_vdev_start(ar, &arg);
  677. if (ret) {
  678. ath10k_warn("failed to start WMI vdev %i: %d\n",
  679. arg.vdev_id, ret);
  680. return ret;
  681. }
  682. ret = ath10k_vdev_setup_sync(ar);
  683. if (ret) {
  684. ath10k_warn("failed to synchronise setup for vdev %i: %d\n",
  685. arg.vdev_id, ret);
  686. return ret;
  687. }
  688. ar->num_started_vdevs++;
  689. ath10k_recalc_radar_detection(ar);
  690. return ret;
  691. }
  692. static int ath10k_vdev_stop(struct ath10k_vif *arvif)
  693. {
  694. struct ath10k *ar = arvif->ar;
  695. int ret;
  696. lockdep_assert_held(&ar->conf_mutex);
  697. reinit_completion(&ar->vdev_setup_done);
  698. ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
  699. if (ret) {
  700. ath10k_warn("failed to stop WMI vdev %i: %d\n",
  701. arvif->vdev_id, ret);
  702. return ret;
  703. }
  704. ret = ath10k_vdev_setup_sync(ar);
  705. if (ret) {
  706. ath10k_warn("failed to syncronise setup for vdev %i: %d\n",
  707. arvif->vdev_id, ret);
  708. return ret;
  709. }
  710. WARN_ON(ar->num_started_vdevs == 0);
  711. if (ar->num_started_vdevs != 0) {
  712. ar->num_started_vdevs--;
  713. ath10k_recalc_radar_detection(ar);
  714. }
  715. return ret;
  716. }
  717. static void ath10k_control_beaconing(struct ath10k_vif *arvif,
  718. struct ieee80211_bss_conf *info)
  719. {
  720. int ret = 0;
  721. lockdep_assert_held(&arvif->ar->conf_mutex);
  722. if (!info->enable_beacon) {
  723. ath10k_vdev_stop(arvif);
  724. arvif->is_started = false;
  725. arvif->is_up = false;
  726. spin_lock_bh(&arvif->ar->data_lock);
  727. if (arvif->beacon) {
  728. dma_unmap_single(arvif->ar->dev,
  729. ATH10K_SKB_CB(arvif->beacon)->paddr,
  730. arvif->beacon->len, DMA_TO_DEVICE);
  731. dev_kfree_skb_any(arvif->beacon);
  732. arvif->beacon = NULL;
  733. arvif->beacon_sent = false;
  734. }
  735. spin_unlock_bh(&arvif->ar->data_lock);
  736. return;
  737. }
  738. arvif->tx_seq_no = 0x1000;
  739. ret = ath10k_vdev_start(arvif);
  740. if (ret)
  741. return;
  742. arvif->aid = 0;
  743. memcpy(arvif->bssid, info->bssid, ETH_ALEN);
  744. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  745. arvif->bssid);
  746. if (ret) {
  747. ath10k_warn("failed to bring up vdev %d: %i\n",
  748. arvif->vdev_id, ret);
  749. ath10k_vdev_stop(arvif);
  750. return;
  751. }
  752. arvif->is_started = true;
  753. arvif->is_up = true;
  754. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
  755. }
  756. static void ath10k_control_ibss(struct ath10k_vif *arvif,
  757. struct ieee80211_bss_conf *info,
  758. const u8 self_peer[ETH_ALEN])
  759. {
  760. u32 vdev_param;
  761. int ret = 0;
  762. lockdep_assert_held(&arvif->ar->conf_mutex);
  763. if (!info->ibss_joined) {
  764. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
  765. if (ret)
  766. ath10k_warn("failed to delete IBSS self peer %pM for vdev %d: %d\n",
  767. self_peer, arvif->vdev_id, ret);
  768. if (is_zero_ether_addr(arvif->bssid))
  769. return;
  770. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
  771. arvif->bssid);
  772. if (ret) {
  773. ath10k_warn("failed to delete IBSS BSSID peer %pM for vdev %d: %d\n",
  774. arvif->bssid, arvif->vdev_id, ret);
  775. return;
  776. }
  777. memset(arvif->bssid, 0, ETH_ALEN);
  778. return;
  779. }
  780. ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
  781. if (ret) {
  782. ath10k_warn("failed to create IBSS self peer %pM for vdev %d: %d\n",
  783. self_peer, arvif->vdev_id, ret);
  784. return;
  785. }
  786. vdev_param = arvif->ar->wmi.vdev_param->atim_window;
  787. ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
  788. ATH10K_DEFAULT_ATIM);
  789. if (ret)
  790. ath10k_warn("failed to set IBSS ATIM for vdev %d: %d\n",
  791. arvif->vdev_id, ret);
  792. }
  793. /*
  794. * Review this when mac80211 gains per-interface powersave support.
  795. */
  796. static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
  797. {
  798. struct ath10k *ar = arvif->ar;
  799. struct ieee80211_conf *conf = &ar->hw->conf;
  800. enum wmi_sta_powersave_param param;
  801. enum wmi_sta_ps_mode psmode;
  802. int ret;
  803. lockdep_assert_held(&arvif->ar->conf_mutex);
  804. if (arvif->vif->type != NL80211_IFTYPE_STATION)
  805. return 0;
  806. if (conf->flags & IEEE80211_CONF_PS) {
  807. psmode = WMI_STA_PS_MODE_ENABLED;
  808. param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
  809. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
  810. conf->dynamic_ps_timeout);
  811. if (ret) {
  812. ath10k_warn("failed to set inactivity time for vdev %d: %i\n",
  813. arvif->vdev_id, ret);
  814. return ret;
  815. }
  816. } else {
  817. psmode = WMI_STA_PS_MODE_DISABLED;
  818. }
  819. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
  820. arvif->vdev_id, psmode ? "enable" : "disable");
  821. ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
  822. if (ret) {
  823. ath10k_warn("failed to set PS Mode %d for vdev %d: %d\n",
  824. psmode, arvif->vdev_id, ret);
  825. return ret;
  826. }
  827. return 0;
  828. }
  829. /**********************/
  830. /* Station management */
  831. /**********************/
  832. static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
  833. struct ath10k_vif *arvif,
  834. struct ieee80211_sta *sta,
  835. struct ieee80211_bss_conf *bss_conf,
  836. struct wmi_peer_assoc_complete_arg *arg)
  837. {
  838. lockdep_assert_held(&ar->conf_mutex);
  839. memcpy(arg->addr, sta->addr, ETH_ALEN);
  840. arg->vdev_id = arvif->vdev_id;
  841. arg->peer_aid = sta->aid;
  842. arg->peer_flags |= WMI_PEER_AUTH;
  843. if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
  844. /*
  845. * Seems FW have problems with Power Save in STA
  846. * mode when we setup this parameter to high (eg. 5).
  847. * Often we see that FW don't send NULL (with clean P flags)
  848. * frame even there is info about buffered frames in beacons.
  849. * Sometimes we have to wait more than 10 seconds before FW
  850. * will wakeup. Often sending one ping from AP to our device
  851. * just fail (more than 50%).
  852. *
  853. * Seems setting this FW parameter to 1 couse FW
  854. * will check every beacon and will wakup immediately
  855. * after detection buffered data.
  856. */
  857. arg->peer_listen_intval = 1;
  858. else
  859. arg->peer_listen_intval = ar->hw->conf.listen_interval;
  860. arg->peer_num_spatial_streams = 1;
  861. /*
  862. * The assoc capabilities are available only in managed mode.
  863. */
  864. if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
  865. arg->peer_caps = bss_conf->assoc_capability;
  866. }
  867. static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
  868. struct ath10k_vif *arvif,
  869. struct wmi_peer_assoc_complete_arg *arg)
  870. {
  871. struct ieee80211_vif *vif = arvif->vif;
  872. struct ieee80211_bss_conf *info = &vif->bss_conf;
  873. struct cfg80211_bss *bss;
  874. const u8 *rsnie = NULL;
  875. const u8 *wpaie = NULL;
  876. lockdep_assert_held(&ar->conf_mutex);
  877. bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
  878. info->bssid, NULL, 0, 0, 0);
  879. if (bss) {
  880. const struct cfg80211_bss_ies *ies;
  881. rcu_read_lock();
  882. rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
  883. ies = rcu_dereference(bss->ies);
  884. wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  885. WLAN_OUI_TYPE_MICROSOFT_WPA,
  886. ies->data,
  887. ies->len);
  888. rcu_read_unlock();
  889. cfg80211_put_bss(ar->hw->wiphy, bss);
  890. }
  891. /* FIXME: base on RSN IE/WPA IE is a correct idea? */
  892. if (rsnie || wpaie) {
  893. ath10k_dbg(ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
  894. arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
  895. }
  896. if (wpaie) {
  897. ath10k_dbg(ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
  898. arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
  899. }
  900. }
  901. static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
  902. struct ieee80211_sta *sta,
  903. struct wmi_peer_assoc_complete_arg *arg)
  904. {
  905. struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
  906. const struct ieee80211_supported_band *sband;
  907. const struct ieee80211_rate *rates;
  908. u32 ratemask;
  909. int i;
  910. lockdep_assert_held(&ar->conf_mutex);
  911. sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
  912. ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
  913. rates = sband->bitrates;
  914. rateset->num_rates = 0;
  915. for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
  916. if (!(ratemask & 1))
  917. continue;
  918. rateset->rates[rateset->num_rates] = rates->hw_value;
  919. rateset->num_rates++;
  920. }
  921. }
  922. static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
  923. struct ieee80211_sta *sta,
  924. struct wmi_peer_assoc_complete_arg *arg)
  925. {
  926. const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  927. int i, n;
  928. lockdep_assert_held(&ar->conf_mutex);
  929. if (!ht_cap->ht_supported)
  930. return;
  931. arg->peer_flags |= WMI_PEER_HT;
  932. arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  933. ht_cap->ampdu_factor)) - 1;
  934. arg->peer_mpdu_density =
  935. ath10k_parse_mpdudensity(ht_cap->ampdu_density);
  936. arg->peer_ht_caps = ht_cap->cap;
  937. arg->peer_rate_caps |= WMI_RC_HT_FLAG;
  938. if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
  939. arg->peer_flags |= WMI_PEER_LDPC;
  940. if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
  941. arg->peer_flags |= WMI_PEER_40MHZ;
  942. arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
  943. }
  944. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
  945. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  946. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
  947. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  948. if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
  949. arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
  950. arg->peer_flags |= WMI_PEER_STBC;
  951. }
  952. if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
  953. u32 stbc;
  954. stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
  955. stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
  956. stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
  957. arg->peer_rate_caps |= stbc;
  958. arg->peer_flags |= WMI_PEER_STBC;
  959. }
  960. if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
  961. arg->peer_rate_caps |= WMI_RC_TS_FLAG;
  962. else if (ht_cap->mcs.rx_mask[1])
  963. arg->peer_rate_caps |= WMI_RC_DS_FLAG;
  964. for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
  965. if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
  966. arg->peer_ht_rates.rates[n++] = i;
  967. /*
  968. * This is a workaround for HT-enabled STAs which break the spec
  969. * and have no HT capabilities RX mask (no HT RX MCS map).
  970. *
  971. * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
  972. * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
  973. *
  974. * Firmware asserts if such situation occurs.
  975. */
  976. if (n == 0) {
  977. arg->peer_ht_rates.num_rates = 8;
  978. for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
  979. arg->peer_ht_rates.rates[i] = i;
  980. } else {
  981. arg->peer_ht_rates.num_rates = n;
  982. arg->peer_num_spatial_streams = sta->rx_nss;
  983. }
  984. ath10k_dbg(ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
  985. arg->addr,
  986. arg->peer_ht_rates.num_rates,
  987. arg->peer_num_spatial_streams);
  988. }
  989. static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
  990. struct ath10k_vif *arvif,
  991. struct ieee80211_sta *sta)
  992. {
  993. u32 uapsd = 0;
  994. u32 max_sp = 0;
  995. int ret = 0;
  996. lockdep_assert_held(&ar->conf_mutex);
  997. if (sta->wme && sta->uapsd_queues) {
  998. ath10k_dbg(ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
  999. sta->uapsd_queues, sta->max_sp);
  1000. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  1001. uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
  1002. WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
  1003. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  1004. uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
  1005. WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
  1006. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  1007. uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
  1008. WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
  1009. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  1010. uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
  1011. WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
  1012. if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
  1013. max_sp = sta->max_sp;
  1014. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  1015. sta->addr,
  1016. WMI_AP_PS_PEER_PARAM_UAPSD,
  1017. uapsd);
  1018. if (ret) {
  1019. ath10k_warn("failed to set ap ps peer param uapsd for vdev %i: %d\n",
  1020. arvif->vdev_id, ret);
  1021. return ret;
  1022. }
  1023. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  1024. sta->addr,
  1025. WMI_AP_PS_PEER_PARAM_MAX_SP,
  1026. max_sp);
  1027. if (ret) {
  1028. ath10k_warn("failed to set ap ps peer param max sp for vdev %i: %d\n",
  1029. arvif->vdev_id, ret);
  1030. return ret;
  1031. }
  1032. /* TODO setup this based on STA listen interval and
  1033. beacon interval. Currently we don't know
  1034. sta->listen_interval - mac80211 patch required.
  1035. Currently use 10 seconds */
  1036. ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
  1037. WMI_AP_PS_PEER_PARAM_AGEOUT_TIME, 10);
  1038. if (ret) {
  1039. ath10k_warn("failed to set ap ps peer param ageout time for vdev %i: %d\n",
  1040. arvif->vdev_id, ret);
  1041. return ret;
  1042. }
  1043. }
  1044. return 0;
  1045. }
  1046. static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
  1047. struct ieee80211_sta *sta,
  1048. struct wmi_peer_assoc_complete_arg *arg)
  1049. {
  1050. const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  1051. u8 ampdu_factor;
  1052. if (!vht_cap->vht_supported)
  1053. return;
  1054. arg->peer_flags |= WMI_PEER_VHT;
  1055. arg->peer_vht_caps = vht_cap->cap;
  1056. ampdu_factor = (vht_cap->cap &
  1057. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
  1058. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  1059. /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
  1060. * zero in VHT IE. Using it would result in degraded throughput.
  1061. * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
  1062. * it if VHT max_mpdu is smaller. */
  1063. arg->peer_max_mpdu = max(arg->peer_max_mpdu,
  1064. (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  1065. ampdu_factor)) - 1);
  1066. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  1067. arg->peer_flags |= WMI_PEER_80MHZ;
  1068. arg->peer_vht_rates.rx_max_rate =
  1069. __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
  1070. arg->peer_vht_rates.rx_mcs_set =
  1071. __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
  1072. arg->peer_vht_rates.tx_max_rate =
  1073. __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
  1074. arg->peer_vht_rates.tx_mcs_set =
  1075. __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
  1076. ath10k_dbg(ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
  1077. sta->addr, arg->peer_max_mpdu, arg->peer_flags);
  1078. }
  1079. static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
  1080. struct ath10k_vif *arvif,
  1081. struct ieee80211_sta *sta,
  1082. struct ieee80211_bss_conf *bss_conf,
  1083. struct wmi_peer_assoc_complete_arg *arg)
  1084. {
  1085. switch (arvif->vdev_type) {
  1086. case WMI_VDEV_TYPE_AP:
  1087. if (sta->wme)
  1088. arg->peer_flags |= WMI_PEER_QOS;
  1089. if (sta->wme && sta->uapsd_queues) {
  1090. arg->peer_flags |= WMI_PEER_APSD;
  1091. arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
  1092. }
  1093. break;
  1094. case WMI_VDEV_TYPE_STA:
  1095. if (bss_conf->qos)
  1096. arg->peer_flags |= WMI_PEER_QOS;
  1097. break;
  1098. default:
  1099. break;
  1100. }
  1101. }
  1102. static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
  1103. struct ath10k_vif *arvif,
  1104. struct ieee80211_sta *sta,
  1105. struct wmi_peer_assoc_complete_arg *arg)
  1106. {
  1107. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  1108. switch (ar->hw->conf.chandef.chan->band) {
  1109. case IEEE80211_BAND_2GHZ:
  1110. if (sta->ht_cap.ht_supported) {
  1111. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1112. phymode = MODE_11NG_HT40;
  1113. else
  1114. phymode = MODE_11NG_HT20;
  1115. } else {
  1116. phymode = MODE_11G;
  1117. }
  1118. break;
  1119. case IEEE80211_BAND_5GHZ:
  1120. /*
  1121. * Check VHT first.
  1122. */
  1123. if (sta->vht_cap.vht_supported) {
  1124. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  1125. phymode = MODE_11AC_VHT80;
  1126. else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1127. phymode = MODE_11AC_VHT40;
  1128. else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
  1129. phymode = MODE_11AC_VHT20;
  1130. } else if (sta->ht_cap.ht_supported) {
  1131. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1132. phymode = MODE_11NA_HT40;
  1133. else
  1134. phymode = MODE_11NA_HT20;
  1135. } else {
  1136. phymode = MODE_11A;
  1137. }
  1138. break;
  1139. default:
  1140. break;
  1141. }
  1142. ath10k_dbg(ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
  1143. sta->addr, ath10k_wmi_phymode_str(phymode));
  1144. arg->peer_phymode = phymode;
  1145. WARN_ON(phymode == MODE_UNKNOWN);
  1146. }
  1147. static int ath10k_peer_assoc_prepare(struct ath10k *ar,
  1148. struct ath10k_vif *arvif,
  1149. struct ieee80211_sta *sta,
  1150. struct ieee80211_bss_conf *bss_conf,
  1151. struct wmi_peer_assoc_complete_arg *arg)
  1152. {
  1153. lockdep_assert_held(&ar->conf_mutex);
  1154. memset(arg, 0, sizeof(*arg));
  1155. ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, arg);
  1156. ath10k_peer_assoc_h_crypto(ar, arvif, arg);
  1157. ath10k_peer_assoc_h_rates(ar, sta, arg);
  1158. ath10k_peer_assoc_h_ht(ar, sta, arg);
  1159. ath10k_peer_assoc_h_vht(ar, sta, arg);
  1160. ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, arg);
  1161. ath10k_peer_assoc_h_phymode(ar, arvif, sta, arg);
  1162. return 0;
  1163. }
  1164. static const u32 ath10k_smps_map[] = {
  1165. [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
  1166. [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
  1167. [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
  1168. [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
  1169. };
  1170. static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
  1171. const u8 *addr,
  1172. const struct ieee80211_sta_ht_cap *ht_cap)
  1173. {
  1174. int smps;
  1175. if (!ht_cap->ht_supported)
  1176. return 0;
  1177. smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
  1178. smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
  1179. if (smps >= ARRAY_SIZE(ath10k_smps_map))
  1180. return -EINVAL;
  1181. return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
  1182. WMI_PEER_SMPS_STATE,
  1183. ath10k_smps_map[smps]);
  1184. }
  1185. /* can be called only in mac80211 callbacks due to `key_count` usage */
  1186. static void ath10k_bss_assoc(struct ieee80211_hw *hw,
  1187. struct ieee80211_vif *vif,
  1188. struct ieee80211_bss_conf *bss_conf)
  1189. {
  1190. struct ath10k *ar = hw->priv;
  1191. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1192. struct ieee80211_sta_ht_cap ht_cap;
  1193. struct wmi_peer_assoc_complete_arg peer_arg;
  1194. struct ieee80211_sta *ap_sta;
  1195. int ret;
  1196. lockdep_assert_held(&ar->conf_mutex);
  1197. rcu_read_lock();
  1198. ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
  1199. if (!ap_sta) {
  1200. ath10k_warn("failed to find station entry for bss %pM vdev %i\n",
  1201. bss_conf->bssid, arvif->vdev_id);
  1202. rcu_read_unlock();
  1203. return;
  1204. }
  1205. /* ap_sta must be accessed only within rcu section which must be left
  1206. * before calling ath10k_setup_peer_smps() which might sleep. */
  1207. ht_cap = ap_sta->ht_cap;
  1208. ret = ath10k_peer_assoc_prepare(ar, arvif, ap_sta,
  1209. bss_conf, &peer_arg);
  1210. if (ret) {
  1211. ath10k_warn("failed to prepare peer assoc for %pM vdev %i: %d\n",
  1212. bss_conf->bssid, arvif->vdev_id, ret);
  1213. rcu_read_unlock();
  1214. return;
  1215. }
  1216. rcu_read_unlock();
  1217. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  1218. if (ret) {
  1219. ath10k_warn("failed to run peer assoc for %pM vdev %i: %d\n",
  1220. bss_conf->bssid, arvif->vdev_id, ret);
  1221. return;
  1222. }
  1223. ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
  1224. if (ret) {
  1225. ath10k_warn("failed to setup peer SMPS for vdev %i: %d\n",
  1226. arvif->vdev_id, ret);
  1227. return;
  1228. }
  1229. ath10k_dbg(ATH10K_DBG_MAC,
  1230. "mac vdev %d up (associated) bssid %pM aid %d\n",
  1231. arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
  1232. arvif->aid = bss_conf->aid;
  1233. memcpy(arvif->bssid, bss_conf->bssid, ETH_ALEN);
  1234. ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
  1235. if (ret) {
  1236. ath10k_warn("failed to set vdev %d up: %d\n",
  1237. arvif->vdev_id, ret);
  1238. return;
  1239. }
  1240. arvif->is_up = true;
  1241. }
  1242. /*
  1243. * FIXME: flush TIDs
  1244. */
  1245. static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
  1246. struct ieee80211_vif *vif)
  1247. {
  1248. struct ath10k *ar = hw->priv;
  1249. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1250. int ret;
  1251. lockdep_assert_held(&ar->conf_mutex);
  1252. /*
  1253. * For some reason, calling VDEV-DOWN before VDEV-STOP
  1254. * makes the FW to send frames via HTT after disassociation.
  1255. * No idea why this happens, even though VDEV-DOWN is supposed
  1256. * to be analogous to link down, so just stop the VDEV.
  1257. */
  1258. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
  1259. arvif->vdev_id);
  1260. /* FIXME: check return value */
  1261. ret = ath10k_vdev_stop(arvif);
  1262. /*
  1263. * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
  1264. * report beacons from previously associated network through HTT.
  1265. * This in turn would spam mac80211 WARN_ON if we bring down all
  1266. * interfaces as it expects there is no rx when no interface is
  1267. * running.
  1268. */
  1269. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
  1270. /* FIXME: why don't we print error if wmi call fails? */
  1271. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  1272. arvif->def_wep_key_idx = 0;
  1273. arvif->is_started = false;
  1274. arvif->is_up = false;
  1275. }
  1276. static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
  1277. struct ieee80211_sta *sta, bool reassoc)
  1278. {
  1279. struct wmi_peer_assoc_complete_arg peer_arg;
  1280. int ret = 0;
  1281. lockdep_assert_held(&ar->conf_mutex);
  1282. ret = ath10k_peer_assoc_prepare(ar, arvif, sta, NULL, &peer_arg);
  1283. if (ret) {
  1284. ath10k_warn("failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
  1285. sta->addr, arvif->vdev_id, ret);
  1286. return ret;
  1287. }
  1288. peer_arg.peer_reassoc = reassoc;
  1289. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  1290. if (ret) {
  1291. ath10k_warn("failed to run peer assoc for STA %pM vdev %i: %d\n",
  1292. sta->addr, arvif->vdev_id, ret);
  1293. return ret;
  1294. }
  1295. ret = ath10k_setup_peer_smps(ar, arvif, sta->addr, &sta->ht_cap);
  1296. if (ret) {
  1297. ath10k_warn("failed to setup peer SMPS for vdev %d: %d\n",
  1298. arvif->vdev_id, ret);
  1299. return ret;
  1300. }
  1301. if (!sta->wme) {
  1302. arvif->num_legacy_stations++;
  1303. ret = ath10k_recalc_rtscts_prot(arvif);
  1304. if (ret) {
  1305. ath10k_warn("failed to recalculate rts/cts prot for vdev %d: %d\n",
  1306. arvif->vdev_id, ret);
  1307. return ret;
  1308. }
  1309. }
  1310. ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
  1311. if (ret) {
  1312. ath10k_warn("failed to install peer wep keys for vdev %i: %d\n",
  1313. arvif->vdev_id, ret);
  1314. return ret;
  1315. }
  1316. ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
  1317. if (ret) {
  1318. ath10k_warn("failed to set qos params for STA %pM for vdev %i: %d\n",
  1319. sta->addr, arvif->vdev_id, ret);
  1320. return ret;
  1321. }
  1322. return ret;
  1323. }
  1324. static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
  1325. struct ieee80211_sta *sta)
  1326. {
  1327. int ret = 0;
  1328. lockdep_assert_held(&ar->conf_mutex);
  1329. if (!sta->wme) {
  1330. arvif->num_legacy_stations--;
  1331. ret = ath10k_recalc_rtscts_prot(arvif);
  1332. if (ret) {
  1333. ath10k_warn("failed to recalculate rts/cts prot for vdev %d: %d\n",
  1334. arvif->vdev_id, ret);
  1335. return ret;
  1336. }
  1337. }
  1338. ret = ath10k_clear_peer_keys(arvif, sta->addr);
  1339. if (ret) {
  1340. ath10k_warn("failed to clear all peer wep keys for vdev %i: %d\n",
  1341. arvif->vdev_id, ret);
  1342. return ret;
  1343. }
  1344. return ret;
  1345. }
  1346. /**************/
  1347. /* Regulatory */
  1348. /**************/
  1349. static int ath10k_update_channel_list(struct ath10k *ar)
  1350. {
  1351. struct ieee80211_hw *hw = ar->hw;
  1352. struct ieee80211_supported_band **bands;
  1353. enum ieee80211_band band;
  1354. struct ieee80211_channel *channel;
  1355. struct wmi_scan_chan_list_arg arg = {0};
  1356. struct wmi_channel_arg *ch;
  1357. bool passive;
  1358. int len;
  1359. int ret;
  1360. int i;
  1361. lockdep_assert_held(&ar->conf_mutex);
  1362. bands = hw->wiphy->bands;
  1363. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1364. if (!bands[band])
  1365. continue;
  1366. for (i = 0; i < bands[band]->n_channels; i++) {
  1367. if (bands[band]->channels[i].flags &
  1368. IEEE80211_CHAN_DISABLED)
  1369. continue;
  1370. arg.n_channels++;
  1371. }
  1372. }
  1373. len = sizeof(struct wmi_channel_arg) * arg.n_channels;
  1374. arg.channels = kzalloc(len, GFP_KERNEL);
  1375. if (!arg.channels)
  1376. return -ENOMEM;
  1377. ch = arg.channels;
  1378. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1379. if (!bands[band])
  1380. continue;
  1381. for (i = 0; i < bands[band]->n_channels; i++) {
  1382. channel = &bands[band]->channels[i];
  1383. if (channel->flags & IEEE80211_CHAN_DISABLED)
  1384. continue;
  1385. ch->allow_ht = true;
  1386. /* FIXME: when should we really allow VHT? */
  1387. ch->allow_vht = true;
  1388. ch->allow_ibss =
  1389. !(channel->flags & IEEE80211_CHAN_NO_IR);
  1390. ch->ht40plus =
  1391. !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
  1392. ch->chan_radar =
  1393. !!(channel->flags & IEEE80211_CHAN_RADAR);
  1394. passive = channel->flags & IEEE80211_CHAN_NO_IR;
  1395. ch->passive = passive;
  1396. ch->freq = channel->center_freq;
  1397. ch->min_power = 0;
  1398. ch->max_power = channel->max_power * 2;
  1399. ch->max_reg_power = channel->max_reg_power * 2;
  1400. ch->max_antenna_gain = channel->max_antenna_gain * 2;
  1401. ch->reg_class_id = 0; /* FIXME */
  1402. /* FIXME: why use only legacy modes, why not any
  1403. * HT/VHT modes? Would that even make any
  1404. * difference? */
  1405. if (channel->band == IEEE80211_BAND_2GHZ)
  1406. ch->mode = MODE_11G;
  1407. else
  1408. ch->mode = MODE_11A;
  1409. if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
  1410. continue;
  1411. ath10k_dbg(ATH10K_DBG_WMI,
  1412. "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
  1413. ch - arg.channels, arg.n_channels,
  1414. ch->freq, ch->max_power, ch->max_reg_power,
  1415. ch->max_antenna_gain, ch->mode);
  1416. ch++;
  1417. }
  1418. }
  1419. ret = ath10k_wmi_scan_chan_list(ar, &arg);
  1420. kfree(arg.channels);
  1421. return ret;
  1422. }
  1423. static enum wmi_dfs_region
  1424. ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
  1425. {
  1426. switch (dfs_region) {
  1427. case NL80211_DFS_UNSET:
  1428. return WMI_UNINIT_DFS_DOMAIN;
  1429. case NL80211_DFS_FCC:
  1430. return WMI_FCC_DFS_DOMAIN;
  1431. case NL80211_DFS_ETSI:
  1432. return WMI_ETSI_DFS_DOMAIN;
  1433. case NL80211_DFS_JP:
  1434. return WMI_MKK4_DFS_DOMAIN;
  1435. }
  1436. return WMI_UNINIT_DFS_DOMAIN;
  1437. }
  1438. static void ath10k_regd_update(struct ath10k *ar)
  1439. {
  1440. struct reg_dmn_pair_mapping *regpair;
  1441. int ret;
  1442. enum wmi_dfs_region wmi_dfs_reg;
  1443. enum nl80211_dfs_regions nl_dfs_reg;
  1444. lockdep_assert_held(&ar->conf_mutex);
  1445. ret = ath10k_update_channel_list(ar);
  1446. if (ret)
  1447. ath10k_warn("failed to update channel list: %d\n", ret);
  1448. regpair = ar->ath_common.regulatory.regpair;
  1449. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  1450. nl_dfs_reg = ar->dfs_detector->region;
  1451. wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
  1452. } else {
  1453. wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
  1454. }
  1455. /* Target allows setting up per-band regdomain but ath_common provides
  1456. * a combined one only */
  1457. ret = ath10k_wmi_pdev_set_regdomain(ar,
  1458. regpair->reg_domain,
  1459. regpair->reg_domain, /* 2ghz */
  1460. regpair->reg_domain, /* 5ghz */
  1461. regpair->reg_2ghz_ctl,
  1462. regpair->reg_5ghz_ctl,
  1463. wmi_dfs_reg);
  1464. if (ret)
  1465. ath10k_warn("failed to set pdev regdomain: %d\n", ret);
  1466. }
  1467. static void ath10k_reg_notifier(struct wiphy *wiphy,
  1468. struct regulatory_request *request)
  1469. {
  1470. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1471. struct ath10k *ar = hw->priv;
  1472. bool result;
  1473. ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
  1474. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  1475. ath10k_dbg(ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
  1476. request->dfs_region);
  1477. result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
  1478. request->dfs_region);
  1479. if (!result)
  1480. ath10k_warn("DFS region 0x%X not supported, will trigger radar for every pulse\n",
  1481. request->dfs_region);
  1482. }
  1483. mutex_lock(&ar->conf_mutex);
  1484. if (ar->state == ATH10K_STATE_ON)
  1485. ath10k_regd_update(ar);
  1486. mutex_unlock(&ar->conf_mutex);
  1487. }
  1488. /***************/
  1489. /* TX handlers */
  1490. /***************/
  1491. static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
  1492. {
  1493. if (ieee80211_is_mgmt(hdr->frame_control))
  1494. return HTT_DATA_TX_EXT_TID_MGMT;
  1495. if (!ieee80211_is_data_qos(hdr->frame_control))
  1496. return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  1497. if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
  1498. return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  1499. return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
  1500. }
  1501. static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar,
  1502. struct ieee80211_tx_info *info)
  1503. {
  1504. if (info->control.vif)
  1505. return ath10k_vif_to_arvif(info->control.vif)->vdev_id;
  1506. if (ar->monitor_started)
  1507. return ar->monitor_vdev_id;
  1508. ath10k_warn("failed to resolve vdev id\n");
  1509. return 0;
  1510. }
  1511. /*
  1512. * Frames sent to the FW have to be in "Native Wifi" format.
  1513. * Strip the QoS field from the 802.11 header.
  1514. */
  1515. static void ath10k_tx_h_qos_workaround(struct ieee80211_hw *hw,
  1516. struct ieee80211_tx_control *control,
  1517. struct sk_buff *skb)
  1518. {
  1519. struct ieee80211_hdr *hdr = (void *)skb->data;
  1520. u8 *qos_ctl;
  1521. if (!ieee80211_is_data_qos(hdr->frame_control))
  1522. return;
  1523. qos_ctl = ieee80211_get_qos_ctl(hdr);
  1524. memmove(skb->data + IEEE80211_QOS_CTL_LEN,
  1525. skb->data, (void *)qos_ctl - (void *)skb->data);
  1526. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  1527. }
  1528. static void ath10k_tx_wep_key_work(struct work_struct *work)
  1529. {
  1530. struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
  1531. wep_key_work);
  1532. int ret, keyidx = arvif->def_wep_key_newidx;
  1533. mutex_lock(&arvif->ar->conf_mutex);
  1534. if (arvif->ar->state != ATH10K_STATE_ON)
  1535. goto unlock;
  1536. if (arvif->def_wep_key_idx == keyidx)
  1537. goto unlock;
  1538. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
  1539. arvif->vdev_id, keyidx);
  1540. ret = ath10k_wmi_vdev_set_param(arvif->ar,
  1541. arvif->vdev_id,
  1542. arvif->ar->wmi.vdev_param->def_keyid,
  1543. keyidx);
  1544. if (ret) {
  1545. ath10k_warn("failed to update wep key index for vdev %d: %d\n",
  1546. arvif->vdev_id,
  1547. ret);
  1548. goto unlock;
  1549. }
  1550. arvif->def_wep_key_idx = keyidx;
  1551. unlock:
  1552. mutex_unlock(&arvif->ar->conf_mutex);
  1553. }
  1554. static void ath10k_tx_h_update_wep_key(struct sk_buff *skb)
  1555. {
  1556. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1557. struct ieee80211_vif *vif = info->control.vif;
  1558. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1559. struct ath10k *ar = arvif->ar;
  1560. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1561. struct ieee80211_key_conf *key = info->control.hw_key;
  1562. if (!ieee80211_has_protected(hdr->frame_control))
  1563. return;
  1564. if (!key)
  1565. return;
  1566. if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
  1567. key->cipher != WLAN_CIPHER_SUITE_WEP104)
  1568. return;
  1569. if (key->keyidx == arvif->def_wep_key_idx)
  1570. return;
  1571. /* FIXME: Most likely a few frames will be TXed with an old key. Simply
  1572. * queueing frames until key index is updated is not an option because
  1573. * sk_buff may need more processing to be done, e.g. offchannel */
  1574. arvif->def_wep_key_newidx = key->keyidx;
  1575. ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
  1576. }
  1577. static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar, struct sk_buff *skb)
  1578. {
  1579. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1580. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1581. struct ieee80211_vif *vif = info->control.vif;
  1582. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1583. /* This is case only for P2P_GO */
  1584. if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
  1585. arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
  1586. return;
  1587. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
  1588. spin_lock_bh(&ar->data_lock);
  1589. if (arvif->u.ap.noa_data)
  1590. if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
  1591. GFP_ATOMIC))
  1592. memcpy(skb_put(skb, arvif->u.ap.noa_len),
  1593. arvif->u.ap.noa_data,
  1594. arvif->u.ap.noa_len);
  1595. spin_unlock_bh(&ar->data_lock);
  1596. }
  1597. }
  1598. static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
  1599. {
  1600. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1601. int ret = 0;
  1602. if (ar->htt.target_version_major >= 3) {
  1603. /* Since HTT 3.0 there is no separate mgmt tx command */
  1604. ret = ath10k_htt_tx(&ar->htt, skb);
  1605. goto exit;
  1606. }
  1607. if (ieee80211_is_mgmt(hdr->frame_control)) {
  1608. if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  1609. ar->fw_features)) {
  1610. if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
  1611. ATH10K_MAX_NUM_MGMT_PENDING) {
  1612. ath10k_warn("reached WMI management tranmist queue limit\n");
  1613. ret = -EBUSY;
  1614. goto exit;
  1615. }
  1616. skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
  1617. ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
  1618. } else {
  1619. ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
  1620. }
  1621. } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  1622. ar->fw_features) &&
  1623. ieee80211_is_nullfunc(hdr->frame_control)) {
  1624. /* FW does not report tx status properly for NullFunc frames
  1625. * unless they are sent through mgmt tx path. mac80211 sends
  1626. * those frames when it detects link/beacon loss and depends
  1627. * on the tx status to be correct. */
  1628. ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
  1629. } else {
  1630. ret = ath10k_htt_tx(&ar->htt, skb);
  1631. }
  1632. exit:
  1633. if (ret) {
  1634. ath10k_warn("failed to transmit packet, dropping: %d\n", ret);
  1635. ieee80211_free_txskb(ar->hw, skb);
  1636. }
  1637. }
  1638. void ath10k_offchan_tx_purge(struct ath10k *ar)
  1639. {
  1640. struct sk_buff *skb;
  1641. for (;;) {
  1642. skb = skb_dequeue(&ar->offchan_tx_queue);
  1643. if (!skb)
  1644. break;
  1645. ieee80211_free_txskb(ar->hw, skb);
  1646. }
  1647. }
  1648. void ath10k_offchan_tx_work(struct work_struct *work)
  1649. {
  1650. struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
  1651. struct ath10k_peer *peer;
  1652. struct ieee80211_hdr *hdr;
  1653. struct sk_buff *skb;
  1654. const u8 *peer_addr;
  1655. int vdev_id;
  1656. int ret;
  1657. /* FW requirement: We must create a peer before FW will send out
  1658. * an offchannel frame. Otherwise the frame will be stuck and
  1659. * never transmitted. We delete the peer upon tx completion.
  1660. * It is unlikely that a peer for offchannel tx will already be
  1661. * present. However it may be in some rare cases so account for that.
  1662. * Otherwise we might remove a legitimate peer and break stuff. */
  1663. for (;;) {
  1664. skb = skb_dequeue(&ar->offchan_tx_queue);
  1665. if (!skb)
  1666. break;
  1667. mutex_lock(&ar->conf_mutex);
  1668. ath10k_dbg(ATH10K_DBG_MAC, "mac offchannel skb %p\n",
  1669. skb);
  1670. hdr = (struct ieee80211_hdr *)skb->data;
  1671. peer_addr = ieee80211_get_DA(hdr);
  1672. vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
  1673. spin_lock_bh(&ar->data_lock);
  1674. peer = ath10k_peer_find(ar, vdev_id, peer_addr);
  1675. spin_unlock_bh(&ar->data_lock);
  1676. if (peer)
  1677. /* FIXME: should this use ath10k_warn()? */
  1678. ath10k_dbg(ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
  1679. peer_addr, vdev_id);
  1680. if (!peer) {
  1681. ret = ath10k_peer_create(ar, vdev_id, peer_addr);
  1682. if (ret)
  1683. ath10k_warn("failed to create peer %pM on vdev %d: %d\n",
  1684. peer_addr, vdev_id, ret);
  1685. }
  1686. spin_lock_bh(&ar->data_lock);
  1687. reinit_completion(&ar->offchan_tx_completed);
  1688. ar->offchan_tx_skb = skb;
  1689. spin_unlock_bh(&ar->data_lock);
  1690. ath10k_tx_htt(ar, skb);
  1691. ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
  1692. 3 * HZ);
  1693. if (ret <= 0)
  1694. ath10k_warn("timed out waiting for offchannel skb %p\n",
  1695. skb);
  1696. if (!peer) {
  1697. ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
  1698. if (ret)
  1699. ath10k_warn("failed to delete peer %pM on vdev %d: %d\n",
  1700. peer_addr, vdev_id, ret);
  1701. }
  1702. mutex_unlock(&ar->conf_mutex);
  1703. }
  1704. }
  1705. void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
  1706. {
  1707. struct sk_buff *skb;
  1708. for (;;) {
  1709. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  1710. if (!skb)
  1711. break;
  1712. ieee80211_free_txskb(ar->hw, skb);
  1713. }
  1714. }
  1715. void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
  1716. {
  1717. struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
  1718. struct sk_buff *skb;
  1719. int ret;
  1720. for (;;) {
  1721. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  1722. if (!skb)
  1723. break;
  1724. ret = ath10k_wmi_mgmt_tx(ar, skb);
  1725. if (ret) {
  1726. ath10k_warn("failed to transmit management frame via WMI: %d\n",
  1727. ret);
  1728. ieee80211_free_txskb(ar->hw, skb);
  1729. }
  1730. }
  1731. }
  1732. /************/
  1733. /* Scanning */
  1734. /************/
  1735. /*
  1736. * This gets called if we dont get a heart-beat during scan.
  1737. * This may indicate the FW has hung and we need to abort the
  1738. * scan manually to prevent cancel_hw_scan() from deadlocking
  1739. */
  1740. void ath10k_reset_scan(unsigned long ptr)
  1741. {
  1742. struct ath10k *ar = (struct ath10k *)ptr;
  1743. spin_lock_bh(&ar->data_lock);
  1744. if (!ar->scan.in_progress) {
  1745. spin_unlock_bh(&ar->data_lock);
  1746. return;
  1747. }
  1748. ath10k_warn("scan timed out, firmware problem?\n");
  1749. if (ar->scan.is_roc)
  1750. ieee80211_remain_on_channel_expired(ar->hw);
  1751. else
  1752. ieee80211_scan_completed(ar->hw, 1 /* aborted */);
  1753. ar->scan.in_progress = false;
  1754. complete_all(&ar->scan.completed);
  1755. spin_unlock_bh(&ar->data_lock);
  1756. }
  1757. static int ath10k_abort_scan(struct ath10k *ar)
  1758. {
  1759. struct wmi_stop_scan_arg arg = {
  1760. .req_id = 1, /* FIXME */
  1761. .req_type = WMI_SCAN_STOP_ONE,
  1762. .u.scan_id = ATH10K_SCAN_ID,
  1763. };
  1764. int ret;
  1765. lockdep_assert_held(&ar->conf_mutex);
  1766. del_timer_sync(&ar->scan.timeout);
  1767. spin_lock_bh(&ar->data_lock);
  1768. if (!ar->scan.in_progress) {
  1769. spin_unlock_bh(&ar->data_lock);
  1770. return 0;
  1771. }
  1772. ar->scan.aborting = true;
  1773. spin_unlock_bh(&ar->data_lock);
  1774. ret = ath10k_wmi_stop_scan(ar, &arg);
  1775. if (ret) {
  1776. ath10k_warn("failed to stop wmi scan: %d\n", ret);
  1777. spin_lock_bh(&ar->data_lock);
  1778. ar->scan.in_progress = false;
  1779. ath10k_offchan_tx_purge(ar);
  1780. spin_unlock_bh(&ar->data_lock);
  1781. return -EIO;
  1782. }
  1783. ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
  1784. if (ret == 0)
  1785. ath10k_warn("timed out while waiting for scan to stop\n");
  1786. /* scan completion may be done right after we timeout here, so let's
  1787. * check the in_progress and tell mac80211 scan is completed. if we
  1788. * don't do that and FW fails to send us scan completion indication
  1789. * then userspace won't be able to scan anymore */
  1790. ret = 0;
  1791. spin_lock_bh(&ar->data_lock);
  1792. if (ar->scan.in_progress) {
  1793. ath10k_warn("failed to stop scan, it's still in progress\n");
  1794. ar->scan.in_progress = false;
  1795. ath10k_offchan_tx_purge(ar);
  1796. ret = -ETIMEDOUT;
  1797. }
  1798. spin_unlock_bh(&ar->data_lock);
  1799. return ret;
  1800. }
  1801. static int ath10k_start_scan(struct ath10k *ar,
  1802. const struct wmi_start_scan_arg *arg)
  1803. {
  1804. int ret;
  1805. lockdep_assert_held(&ar->conf_mutex);
  1806. ret = ath10k_wmi_start_scan(ar, arg);
  1807. if (ret)
  1808. return ret;
  1809. ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
  1810. if (ret == 0) {
  1811. ath10k_abort_scan(ar);
  1812. return ret;
  1813. }
  1814. /* the scan can complete earlier, before we even
  1815. * start the timer. in that case the timer handler
  1816. * checks ar->scan.in_progress and bails out if its
  1817. * false. Add a 200ms margin to account event/command
  1818. * processing. */
  1819. mod_timer(&ar->scan.timeout, jiffies +
  1820. msecs_to_jiffies(arg->max_scan_time+200));
  1821. return 0;
  1822. }
  1823. /**********************/
  1824. /* mac80211 callbacks */
  1825. /**********************/
  1826. static void ath10k_tx(struct ieee80211_hw *hw,
  1827. struct ieee80211_tx_control *control,
  1828. struct sk_buff *skb)
  1829. {
  1830. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1831. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1832. struct ath10k *ar = hw->priv;
  1833. u8 tid, vdev_id;
  1834. /* We should disable CCK RATE due to P2P */
  1835. if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
  1836. ath10k_dbg(ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
  1837. /* we must calculate tid before we apply qos workaround
  1838. * as we'd lose the qos control field */
  1839. tid = ath10k_tx_h_get_tid(hdr);
  1840. vdev_id = ath10k_tx_h_get_vdev_id(ar, info);
  1841. /* it makes no sense to process injected frames like that */
  1842. if (info->control.vif &&
  1843. info->control.vif->type != NL80211_IFTYPE_MONITOR) {
  1844. ath10k_tx_h_qos_workaround(hw, control, skb);
  1845. ath10k_tx_h_update_wep_key(skb);
  1846. ath10k_tx_h_add_p2p_noa_ie(ar, skb);
  1847. ath10k_tx_h_seq_no(skb);
  1848. }
  1849. ATH10K_SKB_CB(skb)->vdev_id = vdev_id;
  1850. ATH10K_SKB_CB(skb)->htt.is_offchan = false;
  1851. ATH10K_SKB_CB(skb)->htt.tid = tid;
  1852. if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
  1853. spin_lock_bh(&ar->data_lock);
  1854. ATH10K_SKB_CB(skb)->htt.is_offchan = true;
  1855. ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
  1856. spin_unlock_bh(&ar->data_lock);
  1857. ath10k_dbg(ATH10K_DBG_MAC, "queued offchannel skb %p\n", skb);
  1858. skb_queue_tail(&ar->offchan_tx_queue, skb);
  1859. ieee80211_queue_work(hw, &ar->offchan_tx_work);
  1860. return;
  1861. }
  1862. ath10k_tx_htt(ar, skb);
  1863. }
  1864. /* Must not be called with conf_mutex held as workers can use that also. */
  1865. static void ath10k_drain_tx(struct ath10k *ar)
  1866. {
  1867. /* make sure rcu-protected mac80211 tx path itself is drained */
  1868. synchronize_net();
  1869. ath10k_offchan_tx_purge(ar);
  1870. ath10k_mgmt_over_wmi_tx_purge(ar);
  1871. cancel_work_sync(&ar->offchan_tx_work);
  1872. cancel_work_sync(&ar->wmi_mgmt_tx_work);
  1873. }
  1874. void ath10k_halt(struct ath10k *ar)
  1875. {
  1876. struct ath10k_vif *arvif;
  1877. lockdep_assert_held(&ar->conf_mutex);
  1878. if (ath10k_monitor_is_enabled(ar)) {
  1879. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  1880. ar->promisc = false;
  1881. ar->monitor = false;
  1882. ath10k_monitor_stop(ar);
  1883. }
  1884. del_timer_sync(&ar->scan.timeout);
  1885. ath10k_reset_scan((unsigned long)ar);
  1886. ath10k_peer_cleanup_all(ar);
  1887. ath10k_core_stop(ar);
  1888. ath10k_hif_power_down(ar);
  1889. spin_lock_bh(&ar->data_lock);
  1890. list_for_each_entry(arvif, &ar->arvifs, list) {
  1891. if (!arvif->beacon)
  1892. continue;
  1893. dma_unmap_single(arvif->ar->dev,
  1894. ATH10K_SKB_CB(arvif->beacon)->paddr,
  1895. arvif->beacon->len, DMA_TO_DEVICE);
  1896. dev_kfree_skb_any(arvif->beacon);
  1897. arvif->beacon = NULL;
  1898. }
  1899. spin_unlock_bh(&ar->data_lock);
  1900. }
  1901. static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
  1902. {
  1903. struct ath10k *ar = hw->priv;
  1904. mutex_lock(&ar->conf_mutex);
  1905. if (ar->cfg_tx_chainmask) {
  1906. *tx_ant = ar->cfg_tx_chainmask;
  1907. *rx_ant = ar->cfg_rx_chainmask;
  1908. } else {
  1909. *tx_ant = ar->supp_tx_chainmask;
  1910. *rx_ant = ar->supp_rx_chainmask;
  1911. }
  1912. mutex_unlock(&ar->conf_mutex);
  1913. return 0;
  1914. }
  1915. static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
  1916. {
  1917. int ret;
  1918. lockdep_assert_held(&ar->conf_mutex);
  1919. ar->cfg_tx_chainmask = tx_ant;
  1920. ar->cfg_rx_chainmask = rx_ant;
  1921. if ((ar->state != ATH10K_STATE_ON) &&
  1922. (ar->state != ATH10K_STATE_RESTARTED))
  1923. return 0;
  1924. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
  1925. tx_ant);
  1926. if (ret) {
  1927. ath10k_warn("failed to set tx-chainmask: %d, req 0x%x\n",
  1928. ret, tx_ant);
  1929. return ret;
  1930. }
  1931. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
  1932. rx_ant);
  1933. if (ret) {
  1934. ath10k_warn("failed to set rx-chainmask: %d, req 0x%x\n",
  1935. ret, rx_ant);
  1936. return ret;
  1937. }
  1938. return 0;
  1939. }
  1940. static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
  1941. {
  1942. struct ath10k *ar = hw->priv;
  1943. int ret;
  1944. mutex_lock(&ar->conf_mutex);
  1945. ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
  1946. mutex_unlock(&ar->conf_mutex);
  1947. return ret;
  1948. }
  1949. static int ath10k_start(struct ieee80211_hw *hw)
  1950. {
  1951. struct ath10k *ar = hw->priv;
  1952. int ret = 0;
  1953. /*
  1954. * This makes sense only when restarting hw. It is harmless to call
  1955. * uncoditionally. This is necessary to make sure no HTT/WMI tx
  1956. * commands will be submitted while restarting.
  1957. */
  1958. ath10k_drain_tx(ar);
  1959. mutex_lock(&ar->conf_mutex);
  1960. switch (ar->state) {
  1961. case ATH10K_STATE_OFF:
  1962. ar->state = ATH10K_STATE_ON;
  1963. break;
  1964. case ATH10K_STATE_RESTARTING:
  1965. ath10k_halt(ar);
  1966. ar->state = ATH10K_STATE_RESTARTED;
  1967. break;
  1968. case ATH10K_STATE_ON:
  1969. case ATH10K_STATE_RESTARTED:
  1970. case ATH10K_STATE_WEDGED:
  1971. WARN_ON(1);
  1972. ret = -EINVAL;
  1973. goto err;
  1974. }
  1975. ret = ath10k_hif_power_up(ar);
  1976. if (ret) {
  1977. ath10k_err("Could not init hif: %d\n", ret);
  1978. goto err_off;
  1979. }
  1980. ret = ath10k_core_start(ar);
  1981. if (ret) {
  1982. ath10k_err("Could not init core: %d\n", ret);
  1983. goto err_power_down;
  1984. }
  1985. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
  1986. if (ret) {
  1987. ath10k_warn("failed to enable PMF QOS: %d\n", ret);
  1988. goto err_core_stop;
  1989. }
  1990. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
  1991. if (ret) {
  1992. ath10k_warn("failed to enable dynamic BW: %d\n", ret);
  1993. goto err_core_stop;
  1994. }
  1995. if (ar->cfg_tx_chainmask)
  1996. __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
  1997. ar->cfg_rx_chainmask);
  1998. /*
  1999. * By default FW set ARP frames ac to voice (6). In that case ARP
  2000. * exchange is not working properly for UAPSD enabled AP. ARP requests
  2001. * which arrives with access category 0 are processed by network stack
  2002. * and send back with access category 0, but FW changes access category
  2003. * to 6. Set ARP frames access category to best effort (0) solves
  2004. * this problem.
  2005. */
  2006. ret = ath10k_wmi_pdev_set_param(ar,
  2007. ar->wmi.pdev_param->arp_ac_override, 0);
  2008. if (ret) {
  2009. ath10k_warn("failed to set arp ac override parameter: %d\n",
  2010. ret);
  2011. goto err_core_stop;
  2012. }
  2013. ar->num_started_vdevs = 0;
  2014. ath10k_regd_update(ar);
  2015. mutex_unlock(&ar->conf_mutex);
  2016. return 0;
  2017. err_core_stop:
  2018. ath10k_core_stop(ar);
  2019. err_power_down:
  2020. ath10k_hif_power_down(ar);
  2021. err_off:
  2022. ar->state = ATH10K_STATE_OFF;
  2023. err:
  2024. mutex_unlock(&ar->conf_mutex);
  2025. return ret;
  2026. }
  2027. static void ath10k_stop(struct ieee80211_hw *hw)
  2028. {
  2029. struct ath10k *ar = hw->priv;
  2030. ath10k_drain_tx(ar);
  2031. mutex_lock(&ar->conf_mutex);
  2032. if (ar->state != ATH10K_STATE_OFF) {
  2033. ath10k_halt(ar);
  2034. ar->state = ATH10K_STATE_OFF;
  2035. }
  2036. mutex_unlock(&ar->conf_mutex);
  2037. cancel_work_sync(&ar->restart_work);
  2038. }
  2039. static int ath10k_config_ps(struct ath10k *ar)
  2040. {
  2041. struct ath10k_vif *arvif;
  2042. int ret = 0;
  2043. lockdep_assert_held(&ar->conf_mutex);
  2044. list_for_each_entry(arvif, &ar->arvifs, list) {
  2045. ret = ath10k_mac_vif_setup_ps(arvif);
  2046. if (ret) {
  2047. ath10k_warn("failed to setup powersave: %d\n", ret);
  2048. break;
  2049. }
  2050. }
  2051. return ret;
  2052. }
  2053. static const char *chandef_get_width(enum nl80211_chan_width width)
  2054. {
  2055. switch (width) {
  2056. case NL80211_CHAN_WIDTH_20_NOHT:
  2057. return "20 (noht)";
  2058. case NL80211_CHAN_WIDTH_20:
  2059. return "20";
  2060. case NL80211_CHAN_WIDTH_40:
  2061. return "40";
  2062. case NL80211_CHAN_WIDTH_80:
  2063. return "80";
  2064. case NL80211_CHAN_WIDTH_80P80:
  2065. return "80+80";
  2066. case NL80211_CHAN_WIDTH_160:
  2067. return "160";
  2068. case NL80211_CHAN_WIDTH_5:
  2069. return "5";
  2070. case NL80211_CHAN_WIDTH_10:
  2071. return "10";
  2072. }
  2073. return "?";
  2074. }
  2075. static void ath10k_config_chan(struct ath10k *ar)
  2076. {
  2077. struct ath10k_vif *arvif;
  2078. int ret;
  2079. lockdep_assert_held(&ar->conf_mutex);
  2080. ath10k_dbg(ATH10K_DBG_MAC,
  2081. "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
  2082. ar->chandef.chan->center_freq,
  2083. ar->chandef.center_freq1,
  2084. ar->chandef.center_freq2,
  2085. chandef_get_width(ar->chandef.width));
  2086. /* First stop monitor interface. Some FW versions crash if there's a
  2087. * lone monitor interface. */
  2088. if (ar->monitor_started)
  2089. ath10k_monitor_vdev_stop(ar);
  2090. list_for_each_entry(arvif, &ar->arvifs, list) {
  2091. if (!arvif->is_started)
  2092. continue;
  2093. if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
  2094. continue;
  2095. ret = ath10k_vdev_stop(arvif);
  2096. if (ret) {
  2097. ath10k_warn("failed to stop vdev %d: %d\n",
  2098. arvif->vdev_id, ret);
  2099. continue;
  2100. }
  2101. }
  2102. /* all vdevs are now stopped - now attempt to restart them */
  2103. list_for_each_entry(arvif, &ar->arvifs, list) {
  2104. if (!arvif->is_started)
  2105. continue;
  2106. if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
  2107. continue;
  2108. ret = ath10k_vdev_start(arvif);
  2109. if (ret) {
  2110. ath10k_warn("failed to start vdev %d: %d\n",
  2111. arvif->vdev_id, ret);
  2112. continue;
  2113. }
  2114. if (!arvif->is_up)
  2115. continue;
  2116. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
  2117. arvif->bssid);
  2118. if (ret) {
  2119. ath10k_warn("failed to bring vdev up %d: %d\n",
  2120. arvif->vdev_id, ret);
  2121. continue;
  2122. }
  2123. }
  2124. if (ath10k_monitor_is_enabled(ar))
  2125. ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
  2126. }
  2127. static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
  2128. {
  2129. struct ath10k *ar = hw->priv;
  2130. struct ieee80211_conf *conf = &hw->conf;
  2131. int ret = 0;
  2132. u32 param;
  2133. mutex_lock(&ar->conf_mutex);
  2134. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  2135. ath10k_dbg(ATH10K_DBG_MAC,
  2136. "mac config channel %dMHz flags 0x%x radar %d\n",
  2137. conf->chandef.chan->center_freq,
  2138. conf->chandef.chan->flags,
  2139. conf->radar_enabled);
  2140. spin_lock_bh(&ar->data_lock);
  2141. ar->rx_channel = conf->chandef.chan;
  2142. spin_unlock_bh(&ar->data_lock);
  2143. ar->radar_enabled = conf->radar_enabled;
  2144. ath10k_recalc_radar_detection(ar);
  2145. if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
  2146. ar->chandef = conf->chandef;
  2147. ath10k_config_chan(ar);
  2148. }
  2149. }
  2150. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  2151. ath10k_dbg(ATH10K_DBG_MAC, "mac config power %d\n",
  2152. hw->conf.power_level);
  2153. param = ar->wmi.pdev_param->txpower_limit2g;
  2154. ret = ath10k_wmi_pdev_set_param(ar, param,
  2155. hw->conf.power_level * 2);
  2156. if (ret)
  2157. ath10k_warn("failed to set 2g txpower %d: %d\n",
  2158. hw->conf.power_level, ret);
  2159. param = ar->wmi.pdev_param->txpower_limit5g;
  2160. ret = ath10k_wmi_pdev_set_param(ar, param,
  2161. hw->conf.power_level * 2);
  2162. if (ret)
  2163. ath10k_warn("failed to set 5g txpower %d: %d\n",
  2164. hw->conf.power_level, ret);
  2165. }
  2166. if (changed & IEEE80211_CONF_CHANGE_PS)
  2167. ath10k_config_ps(ar);
  2168. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  2169. if (conf->flags & IEEE80211_CONF_MONITOR && !ar->monitor) {
  2170. ar->monitor = true;
  2171. ret = ath10k_monitor_start(ar);
  2172. if (ret) {
  2173. ath10k_warn("failed to start monitor (config): %d\n",
  2174. ret);
  2175. ar->monitor = false;
  2176. }
  2177. } else if (!(conf->flags & IEEE80211_CONF_MONITOR) &&
  2178. ar->monitor) {
  2179. ar->monitor = false;
  2180. ath10k_monitor_stop(ar);
  2181. }
  2182. }
  2183. mutex_unlock(&ar->conf_mutex);
  2184. return ret;
  2185. }
  2186. /*
  2187. * TODO:
  2188. * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
  2189. * because we will send mgmt frames without CCK. This requirement
  2190. * for P2P_FIND/GO_NEG should be handled by checking CCK flag
  2191. * in the TX packet.
  2192. */
  2193. static int ath10k_add_interface(struct ieee80211_hw *hw,
  2194. struct ieee80211_vif *vif)
  2195. {
  2196. struct ath10k *ar = hw->priv;
  2197. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2198. enum wmi_sta_powersave_param param;
  2199. int ret = 0;
  2200. u32 value;
  2201. int bit;
  2202. u32 vdev_param;
  2203. mutex_lock(&ar->conf_mutex);
  2204. memset(arvif, 0, sizeof(*arvif));
  2205. arvif->ar = ar;
  2206. arvif->vif = vif;
  2207. INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
  2208. INIT_LIST_HEAD(&arvif->list);
  2209. bit = ffs(ar->free_vdev_map);
  2210. if (bit == 0) {
  2211. ret = -EBUSY;
  2212. goto err;
  2213. }
  2214. arvif->vdev_id = bit - 1;
  2215. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
  2216. if (ar->p2p)
  2217. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
  2218. switch (vif->type) {
  2219. case NL80211_IFTYPE_UNSPECIFIED:
  2220. case NL80211_IFTYPE_STATION:
  2221. arvif->vdev_type = WMI_VDEV_TYPE_STA;
  2222. if (vif->p2p)
  2223. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
  2224. break;
  2225. case NL80211_IFTYPE_ADHOC:
  2226. arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
  2227. break;
  2228. case NL80211_IFTYPE_AP:
  2229. arvif->vdev_type = WMI_VDEV_TYPE_AP;
  2230. if (vif->p2p)
  2231. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
  2232. break;
  2233. case NL80211_IFTYPE_MONITOR:
  2234. arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
  2235. break;
  2236. default:
  2237. WARN_ON(1);
  2238. break;
  2239. }
  2240. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
  2241. arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
  2242. ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
  2243. arvif->vdev_subtype, vif->addr);
  2244. if (ret) {
  2245. ath10k_warn("failed to create WMI vdev %i: %d\n",
  2246. arvif->vdev_id, ret);
  2247. goto err;
  2248. }
  2249. ar->free_vdev_map &= ~BIT(arvif->vdev_id);
  2250. list_add(&arvif->list, &ar->arvifs);
  2251. vdev_param = ar->wmi.vdev_param->def_keyid;
  2252. ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
  2253. arvif->def_wep_key_idx);
  2254. if (ret) {
  2255. ath10k_warn("failed to set vdev %i default key id: %d\n",
  2256. arvif->vdev_id, ret);
  2257. goto err_vdev_delete;
  2258. }
  2259. vdev_param = ar->wmi.vdev_param->tx_encap_type;
  2260. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2261. ATH10K_HW_TXRX_NATIVE_WIFI);
  2262. /* 10.X firmware does not support this VDEV parameter. Do not warn */
  2263. if (ret && ret != -EOPNOTSUPP) {
  2264. ath10k_warn("failed to set vdev %i TX encapsulation: %d\n",
  2265. arvif->vdev_id, ret);
  2266. goto err_vdev_delete;
  2267. }
  2268. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  2269. ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
  2270. if (ret) {
  2271. ath10k_warn("failed to create vdev %i peer for AP: %d\n",
  2272. arvif->vdev_id, ret);
  2273. goto err_vdev_delete;
  2274. }
  2275. ret = ath10k_mac_set_kickout(arvif);
  2276. if (ret) {
  2277. ath10k_warn("failed to set vdev %i kickout parameters: %d\n",
  2278. arvif->vdev_id, ret);
  2279. goto err_peer_delete;
  2280. }
  2281. }
  2282. if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
  2283. param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
  2284. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  2285. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2286. param, value);
  2287. if (ret) {
  2288. ath10k_warn("failed to set vdev %i RX wake policy: %d\n",
  2289. arvif->vdev_id, ret);
  2290. goto err_peer_delete;
  2291. }
  2292. param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
  2293. value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
  2294. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2295. param, value);
  2296. if (ret) {
  2297. ath10k_warn("failed to set vdev %i TX wake thresh: %d\n",
  2298. arvif->vdev_id, ret);
  2299. goto err_peer_delete;
  2300. }
  2301. param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
  2302. value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
  2303. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2304. param, value);
  2305. if (ret) {
  2306. ath10k_warn("failed to set vdev %i PSPOLL count: %d\n",
  2307. arvif->vdev_id, ret);
  2308. goto err_peer_delete;
  2309. }
  2310. }
  2311. ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
  2312. if (ret) {
  2313. ath10k_warn("failed to set rts threshold for vdev %d: %d\n",
  2314. arvif->vdev_id, ret);
  2315. goto err_peer_delete;
  2316. }
  2317. ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
  2318. if (ret) {
  2319. ath10k_warn("failed to set frag threshold for vdev %d: %d\n",
  2320. arvif->vdev_id, ret);
  2321. goto err_peer_delete;
  2322. }
  2323. mutex_unlock(&ar->conf_mutex);
  2324. return 0;
  2325. err_peer_delete:
  2326. if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
  2327. ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
  2328. err_vdev_delete:
  2329. ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  2330. ar->free_vdev_map &= ~BIT(arvif->vdev_id);
  2331. list_del(&arvif->list);
  2332. err:
  2333. mutex_unlock(&ar->conf_mutex);
  2334. return ret;
  2335. }
  2336. static void ath10k_remove_interface(struct ieee80211_hw *hw,
  2337. struct ieee80211_vif *vif)
  2338. {
  2339. struct ath10k *ar = hw->priv;
  2340. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2341. int ret;
  2342. mutex_lock(&ar->conf_mutex);
  2343. cancel_work_sync(&arvif->wep_key_work);
  2344. spin_lock_bh(&ar->data_lock);
  2345. if (arvif->beacon) {
  2346. dma_unmap_single(arvif->ar->dev,
  2347. ATH10K_SKB_CB(arvif->beacon)->paddr,
  2348. arvif->beacon->len, DMA_TO_DEVICE);
  2349. dev_kfree_skb_any(arvif->beacon);
  2350. arvif->beacon = NULL;
  2351. }
  2352. spin_unlock_bh(&ar->data_lock);
  2353. ar->free_vdev_map |= 1 << (arvif->vdev_id);
  2354. list_del(&arvif->list);
  2355. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  2356. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
  2357. if (ret)
  2358. ath10k_warn("failed to remove peer for AP vdev %i: %d\n",
  2359. arvif->vdev_id, ret);
  2360. kfree(arvif->u.ap.noa_data);
  2361. }
  2362. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
  2363. arvif->vdev_id);
  2364. ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  2365. if (ret)
  2366. ath10k_warn("failed to delete WMI vdev %i: %d\n",
  2367. arvif->vdev_id, ret);
  2368. ath10k_peer_cleanup(ar, arvif->vdev_id);
  2369. mutex_unlock(&ar->conf_mutex);
  2370. }
  2371. /*
  2372. * FIXME: Has to be verified.
  2373. */
  2374. #define SUPPORTED_FILTERS \
  2375. (FIF_PROMISC_IN_BSS | \
  2376. FIF_ALLMULTI | \
  2377. FIF_CONTROL | \
  2378. FIF_PSPOLL | \
  2379. FIF_OTHER_BSS | \
  2380. FIF_BCN_PRBRESP_PROMISC | \
  2381. FIF_PROBE_REQ | \
  2382. FIF_FCSFAIL)
  2383. static void ath10k_configure_filter(struct ieee80211_hw *hw,
  2384. unsigned int changed_flags,
  2385. unsigned int *total_flags,
  2386. u64 multicast)
  2387. {
  2388. struct ath10k *ar = hw->priv;
  2389. int ret;
  2390. mutex_lock(&ar->conf_mutex);
  2391. changed_flags &= SUPPORTED_FILTERS;
  2392. *total_flags &= SUPPORTED_FILTERS;
  2393. ar->filter_flags = *total_flags;
  2394. if (ar->filter_flags & FIF_PROMISC_IN_BSS && !ar->promisc) {
  2395. ar->promisc = true;
  2396. ret = ath10k_monitor_start(ar);
  2397. if (ret) {
  2398. ath10k_warn("failed to start monitor (promisc): %d\n",
  2399. ret);
  2400. ar->promisc = false;
  2401. }
  2402. } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) && ar->promisc) {
  2403. ar->promisc = false;
  2404. ath10k_monitor_stop(ar);
  2405. }
  2406. mutex_unlock(&ar->conf_mutex);
  2407. }
  2408. static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
  2409. struct ieee80211_vif *vif,
  2410. struct ieee80211_bss_conf *info,
  2411. u32 changed)
  2412. {
  2413. struct ath10k *ar = hw->priv;
  2414. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2415. int ret = 0;
  2416. u32 vdev_param, pdev_param;
  2417. mutex_lock(&ar->conf_mutex);
  2418. if (changed & BSS_CHANGED_IBSS)
  2419. ath10k_control_ibss(arvif, info, vif->addr);
  2420. if (changed & BSS_CHANGED_BEACON_INT) {
  2421. arvif->beacon_interval = info->beacon_int;
  2422. vdev_param = ar->wmi.vdev_param->beacon_interval;
  2423. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2424. arvif->beacon_interval);
  2425. ath10k_dbg(ATH10K_DBG_MAC,
  2426. "mac vdev %d beacon_interval %d\n",
  2427. arvif->vdev_id, arvif->beacon_interval);
  2428. if (ret)
  2429. ath10k_warn("failed to set beacon interval for vdev %d: %i\n",
  2430. arvif->vdev_id, ret);
  2431. }
  2432. if (changed & BSS_CHANGED_BEACON) {
  2433. ath10k_dbg(ATH10K_DBG_MAC,
  2434. "vdev %d set beacon tx mode to staggered\n",
  2435. arvif->vdev_id);
  2436. pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
  2437. ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
  2438. WMI_BEACON_STAGGERED_MODE);
  2439. if (ret)
  2440. ath10k_warn("failed to set beacon mode for vdev %d: %i\n",
  2441. arvif->vdev_id, ret);
  2442. }
  2443. if (changed & BSS_CHANGED_BEACON_INFO) {
  2444. arvif->dtim_period = info->dtim_period;
  2445. ath10k_dbg(ATH10K_DBG_MAC,
  2446. "mac vdev %d dtim_period %d\n",
  2447. arvif->vdev_id, arvif->dtim_period);
  2448. vdev_param = ar->wmi.vdev_param->dtim_period;
  2449. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2450. arvif->dtim_period);
  2451. if (ret)
  2452. ath10k_warn("failed to set dtim period for vdev %d: %i\n",
  2453. arvif->vdev_id, ret);
  2454. }
  2455. if (changed & BSS_CHANGED_SSID &&
  2456. vif->type == NL80211_IFTYPE_AP) {
  2457. arvif->u.ap.ssid_len = info->ssid_len;
  2458. if (info->ssid_len)
  2459. memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
  2460. arvif->u.ap.hidden_ssid = info->hidden_ssid;
  2461. }
  2462. /*
  2463. * Firmware manages AP self-peer internally so make sure to not create
  2464. * it in driver. Otherwise AP self-peer deletion may timeout later.
  2465. */
  2466. if (changed & BSS_CHANGED_BSSID &&
  2467. vif->type != NL80211_IFTYPE_AP) {
  2468. if (!is_zero_ether_addr(info->bssid)) {
  2469. ath10k_dbg(ATH10K_DBG_MAC,
  2470. "mac vdev %d create peer %pM\n",
  2471. arvif->vdev_id, info->bssid);
  2472. ret = ath10k_peer_create(ar, arvif->vdev_id,
  2473. info->bssid);
  2474. if (ret)
  2475. ath10k_warn("failed to add peer %pM for vdev %d when changing bssid: %i\n",
  2476. info->bssid, arvif->vdev_id, ret);
  2477. if (vif->type == NL80211_IFTYPE_STATION) {
  2478. /*
  2479. * this is never erased as we it for crypto key
  2480. * clearing; this is FW requirement
  2481. */
  2482. memcpy(arvif->bssid, info->bssid, ETH_ALEN);
  2483. ath10k_dbg(ATH10K_DBG_MAC,
  2484. "mac vdev %d start %pM\n",
  2485. arvif->vdev_id, info->bssid);
  2486. ret = ath10k_vdev_start(arvif);
  2487. if (ret) {
  2488. ath10k_warn("failed to start vdev %i: %d\n",
  2489. arvif->vdev_id, ret);
  2490. goto exit;
  2491. }
  2492. arvif->is_started = true;
  2493. }
  2494. /*
  2495. * Mac80211 does not keep IBSS bssid when leaving IBSS,
  2496. * so driver need to store it. It is needed when leaving
  2497. * IBSS in order to remove BSSID peer.
  2498. */
  2499. if (vif->type == NL80211_IFTYPE_ADHOC)
  2500. memcpy(arvif->bssid, info->bssid,
  2501. ETH_ALEN);
  2502. }
  2503. }
  2504. if (changed & BSS_CHANGED_BEACON_ENABLED)
  2505. ath10k_control_beaconing(arvif, info);
  2506. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2507. arvif->use_cts_prot = info->use_cts_prot;
  2508. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
  2509. arvif->vdev_id, info->use_cts_prot);
  2510. ret = ath10k_recalc_rtscts_prot(arvif);
  2511. if (ret)
  2512. ath10k_warn("failed to recalculate rts/cts prot for vdev %d: %d\n",
  2513. arvif->vdev_id, ret);
  2514. }
  2515. if (changed & BSS_CHANGED_ERP_SLOT) {
  2516. u32 slottime;
  2517. if (info->use_short_slot)
  2518. slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
  2519. else
  2520. slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
  2521. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
  2522. arvif->vdev_id, slottime);
  2523. vdev_param = ar->wmi.vdev_param->slot_time;
  2524. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2525. slottime);
  2526. if (ret)
  2527. ath10k_warn("failed to set erp slot for vdev %d: %i\n",
  2528. arvif->vdev_id, ret);
  2529. }
  2530. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2531. u32 preamble;
  2532. if (info->use_short_preamble)
  2533. preamble = WMI_VDEV_PREAMBLE_SHORT;
  2534. else
  2535. preamble = WMI_VDEV_PREAMBLE_LONG;
  2536. ath10k_dbg(ATH10K_DBG_MAC,
  2537. "mac vdev %d preamble %dn",
  2538. arvif->vdev_id, preamble);
  2539. vdev_param = ar->wmi.vdev_param->preamble;
  2540. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2541. preamble);
  2542. if (ret)
  2543. ath10k_warn("failed to set preamble for vdev %d: %i\n",
  2544. arvif->vdev_id, ret);
  2545. }
  2546. if (changed & BSS_CHANGED_ASSOC) {
  2547. if (info->assoc)
  2548. ath10k_bss_assoc(hw, vif, info);
  2549. }
  2550. exit:
  2551. mutex_unlock(&ar->conf_mutex);
  2552. }
  2553. static int ath10k_hw_scan(struct ieee80211_hw *hw,
  2554. struct ieee80211_vif *vif,
  2555. struct cfg80211_scan_request *req)
  2556. {
  2557. struct ath10k *ar = hw->priv;
  2558. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2559. struct wmi_start_scan_arg arg;
  2560. int ret = 0;
  2561. int i;
  2562. mutex_lock(&ar->conf_mutex);
  2563. spin_lock_bh(&ar->data_lock);
  2564. if (ar->scan.in_progress) {
  2565. spin_unlock_bh(&ar->data_lock);
  2566. ret = -EBUSY;
  2567. goto exit;
  2568. }
  2569. reinit_completion(&ar->scan.started);
  2570. reinit_completion(&ar->scan.completed);
  2571. ar->scan.in_progress = true;
  2572. ar->scan.aborting = false;
  2573. ar->scan.is_roc = false;
  2574. ar->scan.vdev_id = arvif->vdev_id;
  2575. spin_unlock_bh(&ar->data_lock);
  2576. memset(&arg, 0, sizeof(arg));
  2577. ath10k_wmi_start_scan_init(ar, &arg);
  2578. arg.vdev_id = arvif->vdev_id;
  2579. arg.scan_id = ATH10K_SCAN_ID;
  2580. if (!req->no_cck)
  2581. arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
  2582. if (req->ie_len) {
  2583. arg.ie_len = req->ie_len;
  2584. memcpy(arg.ie, req->ie, arg.ie_len);
  2585. }
  2586. if (req->n_ssids) {
  2587. arg.n_ssids = req->n_ssids;
  2588. for (i = 0; i < arg.n_ssids; i++) {
  2589. arg.ssids[i].len = req->ssids[i].ssid_len;
  2590. arg.ssids[i].ssid = req->ssids[i].ssid;
  2591. }
  2592. } else {
  2593. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  2594. }
  2595. if (req->n_channels) {
  2596. arg.n_channels = req->n_channels;
  2597. for (i = 0; i < arg.n_channels; i++)
  2598. arg.channels[i] = req->channels[i]->center_freq;
  2599. }
  2600. ret = ath10k_start_scan(ar, &arg);
  2601. if (ret) {
  2602. ath10k_warn("failed to start hw scan: %d\n", ret);
  2603. spin_lock_bh(&ar->data_lock);
  2604. ar->scan.in_progress = false;
  2605. spin_unlock_bh(&ar->data_lock);
  2606. }
  2607. exit:
  2608. mutex_unlock(&ar->conf_mutex);
  2609. return ret;
  2610. }
  2611. static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
  2612. struct ieee80211_vif *vif)
  2613. {
  2614. struct ath10k *ar = hw->priv;
  2615. int ret;
  2616. mutex_lock(&ar->conf_mutex);
  2617. ret = ath10k_abort_scan(ar);
  2618. if (ret) {
  2619. ath10k_warn("failed to abort scan: %d\n", ret);
  2620. ieee80211_scan_completed(hw, 1 /* aborted */);
  2621. }
  2622. mutex_unlock(&ar->conf_mutex);
  2623. }
  2624. static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
  2625. struct ath10k_vif *arvif,
  2626. enum set_key_cmd cmd,
  2627. struct ieee80211_key_conf *key)
  2628. {
  2629. u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
  2630. int ret;
  2631. /* 10.1 firmware branch requires default key index to be set to group
  2632. * key index after installing it. Otherwise FW/HW Txes corrupted
  2633. * frames with multi-vif APs. This is not required for main firmware
  2634. * branch (e.g. 636).
  2635. *
  2636. * FIXME: This has been tested only in AP. It remains unknown if this
  2637. * is required for multi-vif STA interfaces on 10.1 */
  2638. if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
  2639. return;
  2640. if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
  2641. return;
  2642. if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
  2643. return;
  2644. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  2645. return;
  2646. if (cmd != SET_KEY)
  2647. return;
  2648. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2649. key->keyidx);
  2650. if (ret)
  2651. ath10k_warn("failed to set vdev %i group key as default key: %d\n",
  2652. arvif->vdev_id, ret);
  2653. }
  2654. static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2655. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2656. struct ieee80211_key_conf *key)
  2657. {
  2658. struct ath10k *ar = hw->priv;
  2659. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2660. struct ath10k_peer *peer;
  2661. const u8 *peer_addr;
  2662. bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2663. key->cipher == WLAN_CIPHER_SUITE_WEP104;
  2664. int ret = 0;
  2665. if (key->keyidx > WMI_MAX_KEY_INDEX)
  2666. return -ENOSPC;
  2667. mutex_lock(&ar->conf_mutex);
  2668. if (sta)
  2669. peer_addr = sta->addr;
  2670. else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
  2671. peer_addr = vif->bss_conf.bssid;
  2672. else
  2673. peer_addr = vif->addr;
  2674. key->hw_key_idx = key->keyidx;
  2675. /* the peer should not disappear in mid-way (unless FW goes awry) since
  2676. * we already hold conf_mutex. we just make sure its there now. */
  2677. spin_lock_bh(&ar->data_lock);
  2678. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  2679. spin_unlock_bh(&ar->data_lock);
  2680. if (!peer) {
  2681. if (cmd == SET_KEY) {
  2682. ath10k_warn("failed to install key for non-existent peer %pM\n",
  2683. peer_addr);
  2684. ret = -EOPNOTSUPP;
  2685. goto exit;
  2686. } else {
  2687. /* if the peer doesn't exist there is no key to disable
  2688. * anymore */
  2689. goto exit;
  2690. }
  2691. }
  2692. if (is_wep) {
  2693. if (cmd == SET_KEY)
  2694. arvif->wep_keys[key->keyidx] = key;
  2695. else
  2696. arvif->wep_keys[key->keyidx] = NULL;
  2697. if (cmd == DISABLE_KEY)
  2698. ath10k_clear_vdev_key(arvif, key);
  2699. }
  2700. ret = ath10k_install_key(arvif, key, cmd, peer_addr);
  2701. if (ret) {
  2702. ath10k_warn("failed to install key for vdev %i peer %pM: %d\n",
  2703. arvif->vdev_id, peer_addr, ret);
  2704. goto exit;
  2705. }
  2706. ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
  2707. spin_lock_bh(&ar->data_lock);
  2708. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  2709. if (peer && cmd == SET_KEY)
  2710. peer->keys[key->keyidx] = key;
  2711. else if (peer && cmd == DISABLE_KEY)
  2712. peer->keys[key->keyidx] = NULL;
  2713. else if (peer == NULL)
  2714. /* impossible unless FW goes crazy */
  2715. ath10k_warn("Peer %pM disappeared!\n", peer_addr);
  2716. spin_unlock_bh(&ar->data_lock);
  2717. exit:
  2718. mutex_unlock(&ar->conf_mutex);
  2719. return ret;
  2720. }
  2721. static void ath10k_sta_rc_update_wk(struct work_struct *wk)
  2722. {
  2723. struct ath10k *ar;
  2724. struct ath10k_vif *arvif;
  2725. struct ath10k_sta *arsta;
  2726. struct ieee80211_sta *sta;
  2727. u32 changed, bw, nss, smps;
  2728. int err;
  2729. arsta = container_of(wk, struct ath10k_sta, update_wk);
  2730. sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
  2731. arvif = arsta->arvif;
  2732. ar = arvif->ar;
  2733. spin_lock_bh(&ar->data_lock);
  2734. changed = arsta->changed;
  2735. arsta->changed = 0;
  2736. bw = arsta->bw;
  2737. nss = arsta->nss;
  2738. smps = arsta->smps;
  2739. spin_unlock_bh(&ar->data_lock);
  2740. mutex_lock(&ar->conf_mutex);
  2741. if (changed & IEEE80211_RC_BW_CHANGED) {
  2742. ath10k_dbg(ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
  2743. sta->addr, bw);
  2744. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  2745. WMI_PEER_CHAN_WIDTH, bw);
  2746. if (err)
  2747. ath10k_warn("failed to update STA %pM peer bw %d: %d\n",
  2748. sta->addr, bw, err);
  2749. }
  2750. if (changed & IEEE80211_RC_NSS_CHANGED) {
  2751. ath10k_dbg(ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
  2752. sta->addr, nss);
  2753. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  2754. WMI_PEER_NSS, nss);
  2755. if (err)
  2756. ath10k_warn("failed to update STA %pM nss %d: %d\n",
  2757. sta->addr, nss, err);
  2758. }
  2759. if (changed & IEEE80211_RC_SMPS_CHANGED) {
  2760. ath10k_dbg(ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
  2761. sta->addr, smps);
  2762. err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
  2763. WMI_PEER_SMPS_STATE, smps);
  2764. if (err)
  2765. ath10k_warn("failed to update STA %pM smps %d: %d\n",
  2766. sta->addr, smps, err);
  2767. }
  2768. if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
  2769. ath10k_dbg(ATH10K_DBG_MAC, "mac update sta %pM supp rates\n",
  2770. sta->addr);
  2771. err = ath10k_station_assoc(ar, arvif, sta, true);
  2772. if (err)
  2773. ath10k_warn("failed to reassociate station: %pM\n",
  2774. sta->addr);
  2775. }
  2776. mutex_unlock(&ar->conf_mutex);
  2777. }
  2778. static int ath10k_sta_state(struct ieee80211_hw *hw,
  2779. struct ieee80211_vif *vif,
  2780. struct ieee80211_sta *sta,
  2781. enum ieee80211_sta_state old_state,
  2782. enum ieee80211_sta_state new_state)
  2783. {
  2784. struct ath10k *ar = hw->priv;
  2785. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2786. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  2787. int max_num_peers;
  2788. int ret = 0;
  2789. if (old_state == IEEE80211_STA_NOTEXIST &&
  2790. new_state == IEEE80211_STA_NONE) {
  2791. memset(arsta, 0, sizeof(*arsta));
  2792. arsta->arvif = arvif;
  2793. INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
  2794. }
  2795. /* cancel must be done outside the mutex to avoid deadlock */
  2796. if ((old_state == IEEE80211_STA_NONE &&
  2797. new_state == IEEE80211_STA_NOTEXIST))
  2798. cancel_work_sync(&arsta->update_wk);
  2799. mutex_lock(&ar->conf_mutex);
  2800. if (old_state == IEEE80211_STA_NOTEXIST &&
  2801. new_state == IEEE80211_STA_NONE &&
  2802. vif->type != NL80211_IFTYPE_STATION) {
  2803. /*
  2804. * New station addition.
  2805. */
  2806. if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features))
  2807. max_num_peers = TARGET_10X_NUM_PEERS_MAX - 1;
  2808. else
  2809. max_num_peers = TARGET_NUM_PEERS;
  2810. if (ar->num_peers >= max_num_peers) {
  2811. ath10k_warn("number of peers exceeded: peers number %d (max peers %d)\n",
  2812. ar->num_peers, max_num_peers);
  2813. ret = -ENOBUFS;
  2814. goto exit;
  2815. }
  2816. ath10k_dbg(ATH10K_DBG_MAC,
  2817. "mac vdev %d peer create %pM (new sta) num_peers %d\n",
  2818. arvif->vdev_id, sta->addr, ar->num_peers);
  2819. ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
  2820. if (ret)
  2821. ath10k_warn("failed to add peer %pM for vdev %d when adding a new sta: %i\n",
  2822. sta->addr, arvif->vdev_id, ret);
  2823. } else if ((old_state == IEEE80211_STA_NONE &&
  2824. new_state == IEEE80211_STA_NOTEXIST)) {
  2825. /*
  2826. * Existing station deletion.
  2827. */
  2828. ath10k_dbg(ATH10K_DBG_MAC,
  2829. "mac vdev %d peer delete %pM (sta gone)\n",
  2830. arvif->vdev_id, sta->addr);
  2831. ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  2832. if (ret)
  2833. ath10k_warn("failed to delete peer %pM for vdev %d: %i\n",
  2834. sta->addr, arvif->vdev_id, ret);
  2835. if (vif->type == NL80211_IFTYPE_STATION)
  2836. ath10k_bss_disassoc(hw, vif);
  2837. } else if (old_state == IEEE80211_STA_AUTH &&
  2838. new_state == IEEE80211_STA_ASSOC &&
  2839. (vif->type == NL80211_IFTYPE_AP ||
  2840. vif->type == NL80211_IFTYPE_ADHOC)) {
  2841. /*
  2842. * New association.
  2843. */
  2844. ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM associated\n",
  2845. sta->addr);
  2846. ret = ath10k_station_assoc(ar, arvif, sta, false);
  2847. if (ret)
  2848. ath10k_warn("failed to associate station %pM for vdev %i: %i\n",
  2849. sta->addr, arvif->vdev_id, ret);
  2850. } else if (old_state == IEEE80211_STA_ASSOC &&
  2851. new_state == IEEE80211_STA_AUTH &&
  2852. (vif->type == NL80211_IFTYPE_AP ||
  2853. vif->type == NL80211_IFTYPE_ADHOC)) {
  2854. /*
  2855. * Disassociation.
  2856. */
  2857. ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
  2858. sta->addr);
  2859. ret = ath10k_station_disassoc(ar, arvif, sta);
  2860. if (ret)
  2861. ath10k_warn("failed to disassociate station: %pM vdev %i: %i\n",
  2862. sta->addr, arvif->vdev_id, ret);
  2863. }
  2864. exit:
  2865. mutex_unlock(&ar->conf_mutex);
  2866. return ret;
  2867. }
  2868. static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
  2869. u16 ac, bool enable)
  2870. {
  2871. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2872. u32 value = 0;
  2873. int ret = 0;
  2874. lockdep_assert_held(&ar->conf_mutex);
  2875. if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
  2876. return 0;
  2877. switch (ac) {
  2878. case IEEE80211_AC_VO:
  2879. value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
  2880. WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
  2881. break;
  2882. case IEEE80211_AC_VI:
  2883. value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
  2884. WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
  2885. break;
  2886. case IEEE80211_AC_BE:
  2887. value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
  2888. WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
  2889. break;
  2890. case IEEE80211_AC_BK:
  2891. value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
  2892. WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
  2893. break;
  2894. }
  2895. if (enable)
  2896. arvif->u.sta.uapsd |= value;
  2897. else
  2898. arvif->u.sta.uapsd &= ~value;
  2899. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2900. WMI_STA_PS_PARAM_UAPSD,
  2901. arvif->u.sta.uapsd);
  2902. if (ret) {
  2903. ath10k_warn("failed to set uapsd params: %d\n", ret);
  2904. goto exit;
  2905. }
  2906. if (arvif->u.sta.uapsd)
  2907. value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
  2908. else
  2909. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  2910. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2911. WMI_STA_PS_PARAM_RX_WAKE_POLICY,
  2912. value);
  2913. if (ret)
  2914. ath10k_warn("failed to set rx wake param: %d\n", ret);
  2915. exit:
  2916. return ret;
  2917. }
  2918. static int ath10k_conf_tx(struct ieee80211_hw *hw,
  2919. struct ieee80211_vif *vif, u16 ac,
  2920. const struct ieee80211_tx_queue_params *params)
  2921. {
  2922. struct ath10k *ar = hw->priv;
  2923. struct wmi_wmm_params_arg *p = NULL;
  2924. int ret;
  2925. mutex_lock(&ar->conf_mutex);
  2926. switch (ac) {
  2927. case IEEE80211_AC_VO:
  2928. p = &ar->wmm_params.ac_vo;
  2929. break;
  2930. case IEEE80211_AC_VI:
  2931. p = &ar->wmm_params.ac_vi;
  2932. break;
  2933. case IEEE80211_AC_BE:
  2934. p = &ar->wmm_params.ac_be;
  2935. break;
  2936. case IEEE80211_AC_BK:
  2937. p = &ar->wmm_params.ac_bk;
  2938. break;
  2939. }
  2940. if (WARN_ON(!p)) {
  2941. ret = -EINVAL;
  2942. goto exit;
  2943. }
  2944. p->cwmin = params->cw_min;
  2945. p->cwmax = params->cw_max;
  2946. p->aifs = params->aifs;
  2947. /*
  2948. * The channel time duration programmed in the HW is in absolute
  2949. * microseconds, while mac80211 gives the txop in units of
  2950. * 32 microseconds.
  2951. */
  2952. p->txop = params->txop * 32;
  2953. /* FIXME: FW accepts wmm params per hw, not per vif */
  2954. ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
  2955. if (ret) {
  2956. ath10k_warn("failed to set wmm params: %d\n", ret);
  2957. goto exit;
  2958. }
  2959. ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
  2960. if (ret)
  2961. ath10k_warn("failed to set sta uapsd: %d\n", ret);
  2962. exit:
  2963. mutex_unlock(&ar->conf_mutex);
  2964. return ret;
  2965. }
  2966. #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
  2967. static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
  2968. struct ieee80211_vif *vif,
  2969. struct ieee80211_channel *chan,
  2970. int duration,
  2971. enum ieee80211_roc_type type)
  2972. {
  2973. struct ath10k *ar = hw->priv;
  2974. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2975. struct wmi_start_scan_arg arg;
  2976. int ret;
  2977. mutex_lock(&ar->conf_mutex);
  2978. spin_lock_bh(&ar->data_lock);
  2979. if (ar->scan.in_progress) {
  2980. spin_unlock_bh(&ar->data_lock);
  2981. ret = -EBUSY;
  2982. goto exit;
  2983. }
  2984. reinit_completion(&ar->scan.started);
  2985. reinit_completion(&ar->scan.completed);
  2986. reinit_completion(&ar->scan.on_channel);
  2987. ar->scan.in_progress = true;
  2988. ar->scan.aborting = false;
  2989. ar->scan.is_roc = true;
  2990. ar->scan.vdev_id = arvif->vdev_id;
  2991. ar->scan.roc_freq = chan->center_freq;
  2992. spin_unlock_bh(&ar->data_lock);
  2993. memset(&arg, 0, sizeof(arg));
  2994. ath10k_wmi_start_scan_init(ar, &arg);
  2995. arg.vdev_id = arvif->vdev_id;
  2996. arg.scan_id = ATH10K_SCAN_ID;
  2997. arg.n_channels = 1;
  2998. arg.channels[0] = chan->center_freq;
  2999. arg.dwell_time_active = duration;
  3000. arg.dwell_time_passive = duration;
  3001. arg.max_scan_time = 2 * duration;
  3002. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  3003. arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
  3004. ret = ath10k_start_scan(ar, &arg);
  3005. if (ret) {
  3006. ath10k_warn("failed to start roc scan: %d\n", ret);
  3007. spin_lock_bh(&ar->data_lock);
  3008. ar->scan.in_progress = false;
  3009. spin_unlock_bh(&ar->data_lock);
  3010. goto exit;
  3011. }
  3012. ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
  3013. if (ret == 0) {
  3014. ath10k_warn("failed to switch to channel for roc scan\n");
  3015. ath10k_abort_scan(ar);
  3016. ret = -ETIMEDOUT;
  3017. goto exit;
  3018. }
  3019. ret = 0;
  3020. exit:
  3021. mutex_unlock(&ar->conf_mutex);
  3022. return ret;
  3023. }
  3024. static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
  3025. {
  3026. struct ath10k *ar = hw->priv;
  3027. mutex_lock(&ar->conf_mutex);
  3028. ath10k_abort_scan(ar);
  3029. mutex_unlock(&ar->conf_mutex);
  3030. return 0;
  3031. }
  3032. /*
  3033. * Both RTS and Fragmentation threshold are interface-specific
  3034. * in ath10k, but device-specific in mac80211.
  3035. */
  3036. static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  3037. {
  3038. struct ath10k *ar = hw->priv;
  3039. struct ath10k_vif *arvif;
  3040. int ret = 0;
  3041. mutex_lock(&ar->conf_mutex);
  3042. list_for_each_entry(arvif, &ar->arvifs, list) {
  3043. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
  3044. arvif->vdev_id, value);
  3045. ret = ath10k_mac_set_rts(arvif, value);
  3046. if (ret) {
  3047. ath10k_warn("failed to set rts threshold for vdev %d: %d\n",
  3048. arvif->vdev_id, ret);
  3049. break;
  3050. }
  3051. }
  3052. mutex_unlock(&ar->conf_mutex);
  3053. return ret;
  3054. }
  3055. static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
  3056. {
  3057. struct ath10k *ar = hw->priv;
  3058. struct ath10k_vif *arvif;
  3059. int ret = 0;
  3060. mutex_lock(&ar->conf_mutex);
  3061. list_for_each_entry(arvif, &ar->arvifs, list) {
  3062. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
  3063. arvif->vdev_id, value);
  3064. ret = ath10k_mac_set_rts(arvif, value);
  3065. if (ret) {
  3066. ath10k_warn("failed to set fragmentation threshold for vdev %d: %d\n",
  3067. arvif->vdev_id, ret);
  3068. break;
  3069. }
  3070. }
  3071. mutex_unlock(&ar->conf_mutex);
  3072. return ret;
  3073. }
  3074. static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  3075. u32 queues, bool drop)
  3076. {
  3077. struct ath10k *ar = hw->priv;
  3078. bool skip;
  3079. int ret;
  3080. /* mac80211 doesn't care if we really xmit queued frames or not
  3081. * we'll collect those frames either way if we stop/delete vdevs */
  3082. if (drop)
  3083. return;
  3084. mutex_lock(&ar->conf_mutex);
  3085. if (ar->state == ATH10K_STATE_WEDGED)
  3086. goto skip;
  3087. ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
  3088. bool empty;
  3089. spin_lock_bh(&ar->htt.tx_lock);
  3090. empty = (ar->htt.num_pending_tx == 0);
  3091. spin_unlock_bh(&ar->htt.tx_lock);
  3092. skip = (ar->state == ATH10K_STATE_WEDGED);
  3093. (empty || skip);
  3094. }), ATH10K_FLUSH_TIMEOUT_HZ);
  3095. if (ret <= 0 || skip)
  3096. ath10k_warn("failed to flush transmit queue (skip %i ar-state %i): %i\n",
  3097. skip, ar->state, ret);
  3098. skip:
  3099. mutex_unlock(&ar->conf_mutex);
  3100. }
  3101. /* TODO: Implement this function properly
  3102. * For now it is needed to reply to Probe Requests in IBSS mode.
  3103. * Propably we need this information from FW.
  3104. */
  3105. static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
  3106. {
  3107. return 1;
  3108. }
  3109. #ifdef CONFIG_PM
  3110. static int ath10k_suspend(struct ieee80211_hw *hw,
  3111. struct cfg80211_wowlan *wowlan)
  3112. {
  3113. struct ath10k *ar = hw->priv;
  3114. int ret;
  3115. mutex_lock(&ar->conf_mutex);
  3116. ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
  3117. if (ret) {
  3118. if (ret == -ETIMEDOUT)
  3119. goto resume;
  3120. ret = 1;
  3121. goto exit;
  3122. }
  3123. ret = ath10k_hif_suspend(ar);
  3124. if (ret) {
  3125. ath10k_warn("failed to suspend hif: %d\n", ret);
  3126. goto resume;
  3127. }
  3128. ret = 0;
  3129. goto exit;
  3130. resume:
  3131. ret = ath10k_wmi_pdev_resume_target(ar);
  3132. if (ret)
  3133. ath10k_warn("failed to resume target: %d\n", ret);
  3134. ret = 1;
  3135. exit:
  3136. mutex_unlock(&ar->conf_mutex);
  3137. return ret;
  3138. }
  3139. static int ath10k_resume(struct ieee80211_hw *hw)
  3140. {
  3141. struct ath10k *ar = hw->priv;
  3142. int ret;
  3143. mutex_lock(&ar->conf_mutex);
  3144. ret = ath10k_hif_resume(ar);
  3145. if (ret) {
  3146. ath10k_warn("failed to resume hif: %d\n", ret);
  3147. ret = 1;
  3148. goto exit;
  3149. }
  3150. ret = ath10k_wmi_pdev_resume_target(ar);
  3151. if (ret) {
  3152. ath10k_warn("failed to resume target: %d\n", ret);
  3153. ret = 1;
  3154. goto exit;
  3155. }
  3156. ret = 0;
  3157. exit:
  3158. mutex_unlock(&ar->conf_mutex);
  3159. return ret;
  3160. }
  3161. #endif
  3162. static void ath10k_restart_complete(struct ieee80211_hw *hw)
  3163. {
  3164. struct ath10k *ar = hw->priv;
  3165. mutex_lock(&ar->conf_mutex);
  3166. /* If device failed to restart it will be in a different state, e.g.
  3167. * ATH10K_STATE_WEDGED */
  3168. if (ar->state == ATH10K_STATE_RESTARTED) {
  3169. ath10k_info("device successfully recovered\n");
  3170. ar->state = ATH10K_STATE_ON;
  3171. }
  3172. mutex_unlock(&ar->conf_mutex);
  3173. }
  3174. static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
  3175. struct survey_info *survey)
  3176. {
  3177. struct ath10k *ar = hw->priv;
  3178. struct ieee80211_supported_band *sband;
  3179. struct survey_info *ar_survey = &ar->survey[idx];
  3180. int ret = 0;
  3181. mutex_lock(&ar->conf_mutex);
  3182. sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  3183. if (sband && idx >= sband->n_channels) {
  3184. idx -= sband->n_channels;
  3185. sband = NULL;
  3186. }
  3187. if (!sband)
  3188. sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
  3189. if (!sband || idx >= sband->n_channels) {
  3190. ret = -ENOENT;
  3191. goto exit;
  3192. }
  3193. spin_lock_bh(&ar->data_lock);
  3194. memcpy(survey, ar_survey, sizeof(*survey));
  3195. spin_unlock_bh(&ar->data_lock);
  3196. survey->channel = &sband->channels[idx];
  3197. exit:
  3198. mutex_unlock(&ar->conf_mutex);
  3199. return ret;
  3200. }
  3201. /* Helper table for legacy fixed_rate/bitrate_mask */
  3202. static const u8 cck_ofdm_rate[] = {
  3203. /* CCK */
  3204. 3, /* 1Mbps */
  3205. 2, /* 2Mbps */
  3206. 1, /* 5.5Mbps */
  3207. 0, /* 11Mbps */
  3208. /* OFDM */
  3209. 3, /* 6Mbps */
  3210. 7, /* 9Mbps */
  3211. 2, /* 12Mbps */
  3212. 6, /* 18Mbps */
  3213. 1, /* 24Mbps */
  3214. 5, /* 36Mbps */
  3215. 0, /* 48Mbps */
  3216. 4, /* 54Mbps */
  3217. };
  3218. /* Check if only one bit set */
  3219. static int ath10k_check_single_mask(u32 mask)
  3220. {
  3221. int bit;
  3222. bit = ffs(mask);
  3223. if (!bit)
  3224. return 0;
  3225. mask &= ~BIT(bit - 1);
  3226. if (mask)
  3227. return 2;
  3228. return 1;
  3229. }
  3230. static bool
  3231. ath10k_default_bitrate_mask(struct ath10k *ar,
  3232. enum ieee80211_band band,
  3233. const struct cfg80211_bitrate_mask *mask)
  3234. {
  3235. u32 legacy = 0x00ff;
  3236. u8 ht = 0xff, i;
  3237. u16 vht = 0x3ff;
  3238. switch (band) {
  3239. case IEEE80211_BAND_2GHZ:
  3240. legacy = 0x00fff;
  3241. vht = 0;
  3242. break;
  3243. case IEEE80211_BAND_5GHZ:
  3244. break;
  3245. default:
  3246. return false;
  3247. }
  3248. if (mask->control[band].legacy != legacy)
  3249. return false;
  3250. for (i = 0; i < ar->num_rf_chains; i++)
  3251. if (mask->control[band].ht_mcs[i] != ht)
  3252. return false;
  3253. for (i = 0; i < ar->num_rf_chains; i++)
  3254. if (mask->control[band].vht_mcs[i] != vht)
  3255. return false;
  3256. return true;
  3257. }
  3258. static bool
  3259. ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
  3260. enum ieee80211_band band,
  3261. u8 *fixed_nss)
  3262. {
  3263. int ht_nss = 0, vht_nss = 0, i;
  3264. /* check legacy */
  3265. if (ath10k_check_single_mask(mask->control[band].legacy))
  3266. return false;
  3267. /* check HT */
  3268. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  3269. if (mask->control[band].ht_mcs[i] == 0xff)
  3270. continue;
  3271. else if (mask->control[band].ht_mcs[i] == 0x00)
  3272. break;
  3273. else
  3274. return false;
  3275. }
  3276. ht_nss = i;
  3277. /* check VHT */
  3278. for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
  3279. if (mask->control[band].vht_mcs[i] == 0x03ff)
  3280. continue;
  3281. else if (mask->control[band].vht_mcs[i] == 0x0000)
  3282. break;
  3283. else
  3284. return false;
  3285. }
  3286. vht_nss = i;
  3287. if (ht_nss > 0 && vht_nss > 0)
  3288. return false;
  3289. if (ht_nss)
  3290. *fixed_nss = ht_nss;
  3291. else if (vht_nss)
  3292. *fixed_nss = vht_nss;
  3293. else
  3294. return false;
  3295. return true;
  3296. }
  3297. static bool
  3298. ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
  3299. enum ieee80211_band band,
  3300. enum wmi_rate_preamble *preamble)
  3301. {
  3302. int legacy = 0, ht = 0, vht = 0, i;
  3303. *preamble = WMI_RATE_PREAMBLE_OFDM;
  3304. /* check legacy */
  3305. legacy = ath10k_check_single_mask(mask->control[band].legacy);
  3306. if (legacy > 1)
  3307. return false;
  3308. /* check HT */
  3309. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
  3310. ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
  3311. if (ht > 1)
  3312. return false;
  3313. /* check VHT */
  3314. for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
  3315. vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
  3316. if (vht > 1)
  3317. return false;
  3318. /* Currently we support only one fixed_rate */
  3319. if ((legacy + ht + vht) != 1)
  3320. return false;
  3321. if (ht)
  3322. *preamble = WMI_RATE_PREAMBLE_HT;
  3323. else if (vht)
  3324. *preamble = WMI_RATE_PREAMBLE_VHT;
  3325. return true;
  3326. }
  3327. static bool
  3328. ath10k_bitrate_mask_rate(const struct cfg80211_bitrate_mask *mask,
  3329. enum ieee80211_band band,
  3330. u8 *fixed_rate,
  3331. u8 *fixed_nss)
  3332. {
  3333. u8 rate = 0, pream = 0, nss = 0, i;
  3334. enum wmi_rate_preamble preamble;
  3335. /* Check if single rate correct */
  3336. if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
  3337. return false;
  3338. pream = preamble;
  3339. switch (preamble) {
  3340. case WMI_RATE_PREAMBLE_CCK:
  3341. case WMI_RATE_PREAMBLE_OFDM:
  3342. i = ffs(mask->control[band].legacy) - 1;
  3343. if (band == IEEE80211_BAND_2GHZ && i < 4)
  3344. pream = WMI_RATE_PREAMBLE_CCK;
  3345. if (band == IEEE80211_BAND_5GHZ)
  3346. i += 4;
  3347. if (i >= ARRAY_SIZE(cck_ofdm_rate))
  3348. return false;
  3349. rate = cck_ofdm_rate[i];
  3350. break;
  3351. case WMI_RATE_PREAMBLE_HT:
  3352. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
  3353. if (mask->control[band].ht_mcs[i])
  3354. break;
  3355. if (i == IEEE80211_HT_MCS_MASK_LEN)
  3356. return false;
  3357. rate = ffs(mask->control[band].ht_mcs[i]) - 1;
  3358. nss = i;
  3359. break;
  3360. case WMI_RATE_PREAMBLE_VHT:
  3361. for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
  3362. if (mask->control[band].vht_mcs[i])
  3363. break;
  3364. if (i == NL80211_VHT_NSS_MAX)
  3365. return false;
  3366. rate = ffs(mask->control[band].vht_mcs[i]) - 1;
  3367. nss = i;
  3368. break;
  3369. }
  3370. *fixed_nss = nss + 1;
  3371. nss <<= 4;
  3372. pream <<= 6;
  3373. ath10k_dbg(ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
  3374. pream, nss, rate);
  3375. *fixed_rate = pream | nss | rate;
  3376. return true;
  3377. }
  3378. static bool ath10k_get_fixed_rate_nss(const struct cfg80211_bitrate_mask *mask,
  3379. enum ieee80211_band band,
  3380. u8 *fixed_rate,
  3381. u8 *fixed_nss)
  3382. {
  3383. /* First check full NSS mask, if we can simply limit NSS */
  3384. if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
  3385. return true;
  3386. /* Next Check single rate is set */
  3387. return ath10k_bitrate_mask_rate(mask, band, fixed_rate, fixed_nss);
  3388. }
  3389. static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
  3390. u8 fixed_rate,
  3391. u8 fixed_nss,
  3392. u8 force_sgi)
  3393. {
  3394. struct ath10k *ar = arvif->ar;
  3395. u32 vdev_param;
  3396. int ret = 0;
  3397. mutex_lock(&ar->conf_mutex);
  3398. if (arvif->fixed_rate == fixed_rate &&
  3399. arvif->fixed_nss == fixed_nss &&
  3400. arvif->force_sgi == force_sgi)
  3401. goto exit;
  3402. if (fixed_rate == WMI_FIXED_RATE_NONE)
  3403. ath10k_dbg(ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
  3404. if (force_sgi)
  3405. ath10k_dbg(ATH10K_DBG_MAC, "mac force sgi\n");
  3406. vdev_param = ar->wmi.vdev_param->fixed_rate;
  3407. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
  3408. vdev_param, fixed_rate);
  3409. if (ret) {
  3410. ath10k_warn("failed to set fixed rate param 0x%02x: %d\n",
  3411. fixed_rate, ret);
  3412. ret = -EINVAL;
  3413. goto exit;
  3414. }
  3415. arvif->fixed_rate = fixed_rate;
  3416. vdev_param = ar->wmi.vdev_param->nss;
  3417. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
  3418. vdev_param, fixed_nss);
  3419. if (ret) {
  3420. ath10k_warn("failed to set fixed nss param %d: %d\n",
  3421. fixed_nss, ret);
  3422. ret = -EINVAL;
  3423. goto exit;
  3424. }
  3425. arvif->fixed_nss = fixed_nss;
  3426. vdev_param = ar->wmi.vdev_param->sgi;
  3427. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  3428. force_sgi);
  3429. if (ret) {
  3430. ath10k_warn("failed to set sgi param %d: %d\n",
  3431. force_sgi, ret);
  3432. ret = -EINVAL;
  3433. goto exit;
  3434. }
  3435. arvif->force_sgi = force_sgi;
  3436. exit:
  3437. mutex_unlock(&ar->conf_mutex);
  3438. return ret;
  3439. }
  3440. static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
  3441. struct ieee80211_vif *vif,
  3442. const struct cfg80211_bitrate_mask *mask)
  3443. {
  3444. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3445. struct ath10k *ar = arvif->ar;
  3446. enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
  3447. u8 fixed_rate = WMI_FIXED_RATE_NONE;
  3448. u8 fixed_nss = ar->num_rf_chains;
  3449. u8 force_sgi;
  3450. force_sgi = mask->control[band].gi;
  3451. if (force_sgi == NL80211_TXRATE_FORCE_LGI)
  3452. return -EINVAL;
  3453. if (!ath10k_default_bitrate_mask(ar, band, mask)) {
  3454. if (!ath10k_get_fixed_rate_nss(mask, band,
  3455. &fixed_rate,
  3456. &fixed_nss))
  3457. return -EINVAL;
  3458. }
  3459. if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
  3460. ath10k_warn("failed to force SGI usage for default rate settings\n");
  3461. return -EINVAL;
  3462. }
  3463. return ath10k_set_fixed_rate_param(arvif, fixed_rate,
  3464. fixed_nss, force_sgi);
  3465. }
  3466. static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
  3467. struct ieee80211_vif *vif,
  3468. struct ieee80211_sta *sta,
  3469. u32 changed)
  3470. {
  3471. struct ath10k *ar = hw->priv;
  3472. struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
  3473. u32 bw, smps;
  3474. spin_lock_bh(&ar->data_lock);
  3475. ath10k_dbg(ATH10K_DBG_MAC,
  3476. "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
  3477. sta->addr, changed, sta->bandwidth, sta->rx_nss,
  3478. sta->smps_mode);
  3479. if (changed & IEEE80211_RC_BW_CHANGED) {
  3480. bw = WMI_PEER_CHWIDTH_20MHZ;
  3481. switch (sta->bandwidth) {
  3482. case IEEE80211_STA_RX_BW_20:
  3483. bw = WMI_PEER_CHWIDTH_20MHZ;
  3484. break;
  3485. case IEEE80211_STA_RX_BW_40:
  3486. bw = WMI_PEER_CHWIDTH_40MHZ;
  3487. break;
  3488. case IEEE80211_STA_RX_BW_80:
  3489. bw = WMI_PEER_CHWIDTH_80MHZ;
  3490. break;
  3491. case IEEE80211_STA_RX_BW_160:
  3492. ath10k_warn("Invalid bandwith %d in rc update for %pM\n",
  3493. sta->bandwidth, sta->addr);
  3494. bw = WMI_PEER_CHWIDTH_20MHZ;
  3495. break;
  3496. }
  3497. arsta->bw = bw;
  3498. }
  3499. if (changed & IEEE80211_RC_NSS_CHANGED)
  3500. arsta->nss = sta->rx_nss;
  3501. if (changed & IEEE80211_RC_SMPS_CHANGED) {
  3502. smps = WMI_PEER_SMPS_PS_NONE;
  3503. switch (sta->smps_mode) {
  3504. case IEEE80211_SMPS_AUTOMATIC:
  3505. case IEEE80211_SMPS_OFF:
  3506. smps = WMI_PEER_SMPS_PS_NONE;
  3507. break;
  3508. case IEEE80211_SMPS_STATIC:
  3509. smps = WMI_PEER_SMPS_STATIC;
  3510. break;
  3511. case IEEE80211_SMPS_DYNAMIC:
  3512. smps = WMI_PEER_SMPS_DYNAMIC;
  3513. break;
  3514. case IEEE80211_SMPS_NUM_MODES:
  3515. ath10k_warn("Invalid smps %d in sta rc update for %pM\n",
  3516. sta->smps_mode, sta->addr);
  3517. smps = WMI_PEER_SMPS_PS_NONE;
  3518. break;
  3519. }
  3520. arsta->smps = smps;
  3521. }
  3522. arsta->changed |= changed;
  3523. spin_unlock_bh(&ar->data_lock);
  3524. ieee80211_queue_work(hw, &arsta->update_wk);
  3525. }
  3526. static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  3527. {
  3528. /*
  3529. * FIXME: Return 0 for time being. Need to figure out whether FW
  3530. * has the API to fetch 64-bit local TSF
  3531. */
  3532. return 0;
  3533. }
  3534. static const struct ieee80211_ops ath10k_ops = {
  3535. .tx = ath10k_tx,
  3536. .start = ath10k_start,
  3537. .stop = ath10k_stop,
  3538. .config = ath10k_config,
  3539. .add_interface = ath10k_add_interface,
  3540. .remove_interface = ath10k_remove_interface,
  3541. .configure_filter = ath10k_configure_filter,
  3542. .bss_info_changed = ath10k_bss_info_changed,
  3543. .hw_scan = ath10k_hw_scan,
  3544. .cancel_hw_scan = ath10k_cancel_hw_scan,
  3545. .set_key = ath10k_set_key,
  3546. .sta_state = ath10k_sta_state,
  3547. .conf_tx = ath10k_conf_tx,
  3548. .remain_on_channel = ath10k_remain_on_channel,
  3549. .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
  3550. .set_rts_threshold = ath10k_set_rts_threshold,
  3551. .set_frag_threshold = ath10k_set_frag_threshold,
  3552. .flush = ath10k_flush,
  3553. .tx_last_beacon = ath10k_tx_last_beacon,
  3554. .set_antenna = ath10k_set_antenna,
  3555. .get_antenna = ath10k_get_antenna,
  3556. .restart_complete = ath10k_restart_complete,
  3557. .get_survey = ath10k_get_survey,
  3558. .set_bitrate_mask = ath10k_set_bitrate_mask,
  3559. .sta_rc_update = ath10k_sta_rc_update,
  3560. .get_tsf = ath10k_get_tsf,
  3561. #ifdef CONFIG_PM
  3562. .suspend = ath10k_suspend,
  3563. .resume = ath10k_resume,
  3564. #endif
  3565. };
  3566. #define RATETAB_ENT(_rate, _rateid, _flags) { \
  3567. .bitrate = (_rate), \
  3568. .flags = (_flags), \
  3569. .hw_value = (_rateid), \
  3570. }
  3571. #define CHAN2G(_channel, _freq, _flags) { \
  3572. .band = IEEE80211_BAND_2GHZ, \
  3573. .hw_value = (_channel), \
  3574. .center_freq = (_freq), \
  3575. .flags = (_flags), \
  3576. .max_antenna_gain = 0, \
  3577. .max_power = 30, \
  3578. }
  3579. #define CHAN5G(_channel, _freq, _flags) { \
  3580. .band = IEEE80211_BAND_5GHZ, \
  3581. .hw_value = (_channel), \
  3582. .center_freq = (_freq), \
  3583. .flags = (_flags), \
  3584. .max_antenna_gain = 0, \
  3585. .max_power = 30, \
  3586. }
  3587. static const struct ieee80211_channel ath10k_2ghz_channels[] = {
  3588. CHAN2G(1, 2412, 0),
  3589. CHAN2G(2, 2417, 0),
  3590. CHAN2G(3, 2422, 0),
  3591. CHAN2G(4, 2427, 0),
  3592. CHAN2G(5, 2432, 0),
  3593. CHAN2G(6, 2437, 0),
  3594. CHAN2G(7, 2442, 0),
  3595. CHAN2G(8, 2447, 0),
  3596. CHAN2G(9, 2452, 0),
  3597. CHAN2G(10, 2457, 0),
  3598. CHAN2G(11, 2462, 0),
  3599. CHAN2G(12, 2467, 0),
  3600. CHAN2G(13, 2472, 0),
  3601. CHAN2G(14, 2484, 0),
  3602. };
  3603. static const struct ieee80211_channel ath10k_5ghz_channels[] = {
  3604. CHAN5G(36, 5180, 0),
  3605. CHAN5G(40, 5200, 0),
  3606. CHAN5G(44, 5220, 0),
  3607. CHAN5G(48, 5240, 0),
  3608. CHAN5G(52, 5260, 0),
  3609. CHAN5G(56, 5280, 0),
  3610. CHAN5G(60, 5300, 0),
  3611. CHAN5G(64, 5320, 0),
  3612. CHAN5G(100, 5500, 0),
  3613. CHAN5G(104, 5520, 0),
  3614. CHAN5G(108, 5540, 0),
  3615. CHAN5G(112, 5560, 0),
  3616. CHAN5G(116, 5580, 0),
  3617. CHAN5G(120, 5600, 0),
  3618. CHAN5G(124, 5620, 0),
  3619. CHAN5G(128, 5640, 0),
  3620. CHAN5G(132, 5660, 0),
  3621. CHAN5G(136, 5680, 0),
  3622. CHAN5G(140, 5700, 0),
  3623. CHAN5G(149, 5745, 0),
  3624. CHAN5G(153, 5765, 0),
  3625. CHAN5G(157, 5785, 0),
  3626. CHAN5G(161, 5805, 0),
  3627. CHAN5G(165, 5825, 0),
  3628. };
  3629. static struct ieee80211_rate ath10k_rates[] = {
  3630. /* CCK */
  3631. RATETAB_ENT(10, 0x82, 0),
  3632. RATETAB_ENT(20, 0x84, 0),
  3633. RATETAB_ENT(55, 0x8b, 0),
  3634. RATETAB_ENT(110, 0x96, 0),
  3635. /* OFDM */
  3636. RATETAB_ENT(60, 0x0c, 0),
  3637. RATETAB_ENT(90, 0x12, 0),
  3638. RATETAB_ENT(120, 0x18, 0),
  3639. RATETAB_ENT(180, 0x24, 0),
  3640. RATETAB_ENT(240, 0x30, 0),
  3641. RATETAB_ENT(360, 0x48, 0),
  3642. RATETAB_ENT(480, 0x60, 0),
  3643. RATETAB_ENT(540, 0x6c, 0),
  3644. };
  3645. #define ath10k_a_rates (ath10k_rates + 4)
  3646. #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
  3647. #define ath10k_g_rates (ath10k_rates + 0)
  3648. #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
  3649. struct ath10k *ath10k_mac_create(void)
  3650. {
  3651. struct ieee80211_hw *hw;
  3652. struct ath10k *ar;
  3653. hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
  3654. if (!hw)
  3655. return NULL;
  3656. ar = hw->priv;
  3657. ar->hw = hw;
  3658. return ar;
  3659. }
  3660. void ath10k_mac_destroy(struct ath10k *ar)
  3661. {
  3662. ieee80211_free_hw(ar->hw);
  3663. }
  3664. static const struct ieee80211_iface_limit ath10k_if_limits[] = {
  3665. {
  3666. .max = 8,
  3667. .types = BIT(NL80211_IFTYPE_STATION)
  3668. | BIT(NL80211_IFTYPE_P2P_CLIENT)
  3669. },
  3670. {
  3671. .max = 3,
  3672. .types = BIT(NL80211_IFTYPE_P2P_GO)
  3673. },
  3674. {
  3675. .max = 7,
  3676. .types = BIT(NL80211_IFTYPE_AP)
  3677. },
  3678. };
  3679. static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
  3680. {
  3681. .max = 8,
  3682. .types = BIT(NL80211_IFTYPE_AP)
  3683. },
  3684. };
  3685. static const struct ieee80211_iface_combination ath10k_if_comb[] = {
  3686. {
  3687. .limits = ath10k_if_limits,
  3688. .n_limits = ARRAY_SIZE(ath10k_if_limits),
  3689. .max_interfaces = 8,
  3690. .num_different_channels = 1,
  3691. .beacon_int_infra_match = true,
  3692. },
  3693. };
  3694. static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
  3695. {
  3696. .limits = ath10k_10x_if_limits,
  3697. .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
  3698. .max_interfaces = 8,
  3699. .num_different_channels = 1,
  3700. .beacon_int_infra_match = true,
  3701. #ifdef CONFIG_ATH10K_DFS_CERTIFIED
  3702. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  3703. BIT(NL80211_CHAN_WIDTH_20) |
  3704. BIT(NL80211_CHAN_WIDTH_40) |
  3705. BIT(NL80211_CHAN_WIDTH_80),
  3706. #endif
  3707. },
  3708. };
  3709. static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
  3710. {
  3711. struct ieee80211_sta_vht_cap vht_cap = {0};
  3712. u16 mcs_map;
  3713. int i;
  3714. vht_cap.vht_supported = 1;
  3715. vht_cap.cap = ar->vht_cap_info;
  3716. mcs_map = 0;
  3717. for (i = 0; i < 8; i++) {
  3718. if (i < ar->num_rf_chains)
  3719. mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
  3720. else
  3721. mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
  3722. }
  3723. vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
  3724. vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
  3725. return vht_cap;
  3726. }
  3727. static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
  3728. {
  3729. int i;
  3730. struct ieee80211_sta_ht_cap ht_cap = {0};
  3731. if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
  3732. return ht_cap;
  3733. ht_cap.ht_supported = 1;
  3734. ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  3735. ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  3736. ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  3737. ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
  3738. ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
  3739. if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
  3740. ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
  3741. if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
  3742. ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  3743. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
  3744. u32 smps;
  3745. smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
  3746. smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
  3747. ht_cap.cap |= smps;
  3748. }
  3749. if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
  3750. ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
  3751. if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
  3752. u32 stbc;
  3753. stbc = ar->ht_cap_info;
  3754. stbc &= WMI_HT_CAP_RX_STBC;
  3755. stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
  3756. stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
  3757. stbc &= IEEE80211_HT_CAP_RX_STBC;
  3758. ht_cap.cap |= stbc;
  3759. }
  3760. if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
  3761. ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
  3762. if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
  3763. ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
  3764. /* max AMSDU is implicitly taken from vht_cap_info */
  3765. if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
  3766. ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  3767. for (i = 0; i < ar->num_rf_chains; i++)
  3768. ht_cap.mcs.rx_mask[i] = 0xFF;
  3769. ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  3770. return ht_cap;
  3771. }
  3772. static void ath10k_get_arvif_iter(void *data, u8 *mac,
  3773. struct ieee80211_vif *vif)
  3774. {
  3775. struct ath10k_vif_iter *arvif_iter = data;
  3776. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  3777. if (arvif->vdev_id == arvif_iter->vdev_id)
  3778. arvif_iter->arvif = arvif;
  3779. }
  3780. struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
  3781. {
  3782. struct ath10k_vif_iter arvif_iter;
  3783. u32 flags;
  3784. memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
  3785. arvif_iter.vdev_id = vdev_id;
  3786. flags = IEEE80211_IFACE_ITER_RESUME_ALL;
  3787. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  3788. flags,
  3789. ath10k_get_arvif_iter,
  3790. &arvif_iter);
  3791. if (!arvif_iter.arvif) {
  3792. ath10k_warn("No VIF found for vdev %d\n", vdev_id);
  3793. return NULL;
  3794. }
  3795. return arvif_iter.arvif;
  3796. }
  3797. int ath10k_mac_register(struct ath10k *ar)
  3798. {
  3799. struct ieee80211_supported_band *band;
  3800. struct ieee80211_sta_vht_cap vht_cap;
  3801. struct ieee80211_sta_ht_cap ht_cap;
  3802. void *channels;
  3803. int ret;
  3804. SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
  3805. SET_IEEE80211_DEV(ar->hw, ar->dev);
  3806. ht_cap = ath10k_get_ht_cap(ar);
  3807. vht_cap = ath10k_create_vht_cap(ar);
  3808. if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
  3809. channels = kmemdup(ath10k_2ghz_channels,
  3810. sizeof(ath10k_2ghz_channels),
  3811. GFP_KERNEL);
  3812. if (!channels) {
  3813. ret = -ENOMEM;
  3814. goto err_free;
  3815. }
  3816. band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
  3817. band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
  3818. band->channels = channels;
  3819. band->n_bitrates = ath10k_g_rates_size;
  3820. band->bitrates = ath10k_g_rates;
  3821. band->ht_cap = ht_cap;
  3822. /* vht is not supported in 2.4 GHz */
  3823. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
  3824. }
  3825. if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
  3826. channels = kmemdup(ath10k_5ghz_channels,
  3827. sizeof(ath10k_5ghz_channels),
  3828. GFP_KERNEL);
  3829. if (!channels) {
  3830. ret = -ENOMEM;
  3831. goto err_free;
  3832. }
  3833. band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
  3834. band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
  3835. band->channels = channels;
  3836. band->n_bitrates = ath10k_a_rates_size;
  3837. band->bitrates = ath10k_a_rates;
  3838. band->ht_cap = ht_cap;
  3839. band->vht_cap = vht_cap;
  3840. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
  3841. }
  3842. ar->hw->wiphy->interface_modes =
  3843. BIT(NL80211_IFTYPE_STATION) |
  3844. BIT(NL80211_IFTYPE_ADHOC) |
  3845. BIT(NL80211_IFTYPE_AP);
  3846. if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
  3847. /* TODO: Have to deal with 2x2 chips if/when the come out. */
  3848. ar->supp_tx_chainmask = TARGET_10X_TX_CHAIN_MASK;
  3849. ar->supp_rx_chainmask = TARGET_10X_RX_CHAIN_MASK;
  3850. } else {
  3851. ar->supp_tx_chainmask = TARGET_TX_CHAIN_MASK;
  3852. ar->supp_rx_chainmask = TARGET_RX_CHAIN_MASK;
  3853. }
  3854. ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
  3855. ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
  3856. if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
  3857. ar->hw->wiphy->interface_modes |=
  3858. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  3859. BIT(NL80211_IFTYPE_P2P_GO);
  3860. ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  3861. IEEE80211_HW_SUPPORTS_PS |
  3862. IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
  3863. IEEE80211_HW_SUPPORTS_UAPSD |
  3864. IEEE80211_HW_MFP_CAPABLE |
  3865. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  3866. IEEE80211_HW_HAS_RATE_CONTROL |
  3867. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  3868. IEEE80211_HW_AP_LINK_PS |
  3869. IEEE80211_HW_SPECTRUM_MGMT;
  3870. /* MSDU can have HTT TX fragment pushed in front. The additional 4
  3871. * bytes is used for padding/alignment if necessary. */
  3872. ar->hw->extra_tx_headroom += sizeof(struct htt_data_tx_desc_frag)*2 + 4;
  3873. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
  3874. ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
  3875. if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
  3876. ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  3877. ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
  3878. }
  3879. ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
  3880. ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
  3881. ar->hw->vif_data_size = sizeof(struct ath10k_vif);
  3882. ar->hw->sta_data_size = sizeof(struct ath10k_sta);
  3883. ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
  3884. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  3885. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
  3886. ar->hw->wiphy->max_remain_on_channel_duration = 5000;
  3887. ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
  3888. /*
  3889. * on LL hardware queues are managed entirely by the FW
  3890. * so we only advertise to mac we can do the queues thing
  3891. */
  3892. ar->hw->queues = 4;
  3893. if (test_bit(ATH10K_FW_FEATURE_WMI_10X, ar->fw_features)) {
  3894. ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
  3895. ar->hw->wiphy->n_iface_combinations =
  3896. ARRAY_SIZE(ath10k_10x_if_comb);
  3897. } else {
  3898. ar->hw->wiphy->iface_combinations = ath10k_if_comb;
  3899. ar->hw->wiphy->n_iface_combinations =
  3900. ARRAY_SIZE(ath10k_if_comb);
  3901. }
  3902. ar->hw->netdev_features = NETIF_F_HW_CSUM;
  3903. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
  3904. /* Init ath dfs pattern detector */
  3905. ar->ath_common.debug_mask = ATH_DBG_DFS;
  3906. ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
  3907. NL80211_DFS_UNSET);
  3908. if (!ar->dfs_detector)
  3909. ath10k_warn("failed to initialise DFS pattern detector\n");
  3910. }
  3911. ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
  3912. ath10k_reg_notifier);
  3913. if (ret) {
  3914. ath10k_err("failed to initialise regulatory: %i\n", ret);
  3915. goto err_free;
  3916. }
  3917. ret = ieee80211_register_hw(ar->hw);
  3918. if (ret) {
  3919. ath10k_err("failed to register ieee80211: %d\n", ret);
  3920. goto err_free;
  3921. }
  3922. if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
  3923. ret = regulatory_hint(ar->hw->wiphy,
  3924. ar->ath_common.regulatory.alpha2);
  3925. if (ret)
  3926. goto err_unregister;
  3927. }
  3928. return 0;
  3929. err_unregister:
  3930. ieee80211_unregister_hw(ar->hw);
  3931. err_free:
  3932. kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
  3933. kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
  3934. return ret;
  3935. }
  3936. void ath10k_mac_unregister(struct ath10k *ar)
  3937. {
  3938. ieee80211_unregister_hw(ar->hw);
  3939. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
  3940. ar->dfs_detector->exit(ar->dfs_detector);
  3941. kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
  3942. kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
  3943. SET_IEEE80211_DEV(ar->hw, NULL);
  3944. }