mac.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777
  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. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  50. break;
  51. case WLAN_CIPHER_SUITE_TKIP:
  52. arg.key_cipher = WMI_CIPHER_TKIP;
  53. arg.key_txmic_len = 8;
  54. arg.key_rxmic_len = 8;
  55. break;
  56. case WLAN_CIPHER_SUITE_WEP40:
  57. case WLAN_CIPHER_SUITE_WEP104:
  58. arg.key_cipher = WMI_CIPHER_WEP;
  59. /* AP/IBSS mode requires self-key to be groupwise
  60. * Otherwise pairwise key must be set */
  61. if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
  62. arg.key_flags = WMI_KEY_PAIRWISE;
  63. break;
  64. default:
  65. ath10k_warn("cipher %d is not supported\n", key->cipher);
  66. return -EOPNOTSUPP;
  67. }
  68. if (cmd == DISABLE_KEY) {
  69. arg.key_cipher = WMI_CIPHER_NONE;
  70. arg.key_data = NULL;
  71. }
  72. return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
  73. }
  74. static int ath10k_install_key(struct ath10k_vif *arvif,
  75. struct ieee80211_key_conf *key,
  76. enum set_key_cmd cmd,
  77. const u8 *macaddr)
  78. {
  79. struct ath10k *ar = arvif->ar;
  80. int ret;
  81. lockdep_assert_held(&ar->conf_mutex);
  82. reinit_completion(&ar->install_key_done);
  83. ret = ath10k_send_key(arvif, key, cmd, macaddr);
  84. if (ret)
  85. return ret;
  86. ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
  87. if (ret == 0)
  88. return -ETIMEDOUT;
  89. return 0;
  90. }
  91. static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
  92. const u8 *addr)
  93. {
  94. struct ath10k *ar = arvif->ar;
  95. struct ath10k_peer *peer;
  96. int ret;
  97. int i;
  98. lockdep_assert_held(&ar->conf_mutex);
  99. spin_lock_bh(&ar->data_lock);
  100. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  101. spin_unlock_bh(&ar->data_lock);
  102. if (!peer)
  103. return -ENOENT;
  104. for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
  105. if (arvif->wep_keys[i] == NULL)
  106. continue;
  107. ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
  108. addr);
  109. if (ret)
  110. return ret;
  111. peer->keys[i] = arvif->wep_keys[i];
  112. }
  113. return 0;
  114. }
  115. static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
  116. const u8 *addr)
  117. {
  118. struct ath10k *ar = arvif->ar;
  119. struct ath10k_peer *peer;
  120. int first_errno = 0;
  121. int ret;
  122. int i;
  123. lockdep_assert_held(&ar->conf_mutex);
  124. spin_lock_bh(&ar->data_lock);
  125. peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
  126. spin_unlock_bh(&ar->data_lock);
  127. if (!peer)
  128. return -ENOENT;
  129. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  130. if (peer->keys[i] == NULL)
  131. continue;
  132. ret = ath10k_install_key(arvif, peer->keys[i],
  133. DISABLE_KEY, addr);
  134. if (ret && first_errno == 0)
  135. first_errno = ret;
  136. if (ret)
  137. ath10k_warn("could not remove peer wep key %d (%d)\n",
  138. i, ret);
  139. peer->keys[i] = NULL;
  140. }
  141. return first_errno;
  142. }
  143. static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
  144. struct ieee80211_key_conf *key)
  145. {
  146. struct ath10k *ar = arvif->ar;
  147. struct ath10k_peer *peer;
  148. u8 addr[ETH_ALEN];
  149. int first_errno = 0;
  150. int ret;
  151. int i;
  152. lockdep_assert_held(&ar->conf_mutex);
  153. for (;;) {
  154. /* since ath10k_install_key we can't hold data_lock all the
  155. * time, so we try to remove the keys incrementally */
  156. spin_lock_bh(&ar->data_lock);
  157. i = 0;
  158. list_for_each_entry(peer, &ar->peers, list) {
  159. for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
  160. if (peer->keys[i] == key) {
  161. memcpy(addr, peer->addr, ETH_ALEN);
  162. peer->keys[i] = NULL;
  163. break;
  164. }
  165. }
  166. if (i < ARRAY_SIZE(peer->keys))
  167. break;
  168. }
  169. spin_unlock_bh(&ar->data_lock);
  170. if (i == ARRAY_SIZE(peer->keys))
  171. break;
  172. ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
  173. if (ret && first_errno == 0)
  174. first_errno = ret;
  175. if (ret)
  176. ath10k_warn("could not remove key for %pM\n", addr);
  177. }
  178. return first_errno;
  179. }
  180. /*********************/
  181. /* General utilities */
  182. /*********************/
  183. static inline enum wmi_phy_mode
  184. chan_to_phymode(const struct cfg80211_chan_def *chandef)
  185. {
  186. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  187. switch (chandef->chan->band) {
  188. case IEEE80211_BAND_2GHZ:
  189. switch (chandef->width) {
  190. case NL80211_CHAN_WIDTH_20_NOHT:
  191. phymode = MODE_11G;
  192. break;
  193. case NL80211_CHAN_WIDTH_20:
  194. phymode = MODE_11NG_HT20;
  195. break;
  196. case NL80211_CHAN_WIDTH_40:
  197. phymode = MODE_11NG_HT40;
  198. break;
  199. case NL80211_CHAN_WIDTH_5:
  200. case NL80211_CHAN_WIDTH_10:
  201. case NL80211_CHAN_WIDTH_80:
  202. case NL80211_CHAN_WIDTH_80P80:
  203. case NL80211_CHAN_WIDTH_160:
  204. phymode = MODE_UNKNOWN;
  205. break;
  206. }
  207. break;
  208. case IEEE80211_BAND_5GHZ:
  209. switch (chandef->width) {
  210. case NL80211_CHAN_WIDTH_20_NOHT:
  211. phymode = MODE_11A;
  212. break;
  213. case NL80211_CHAN_WIDTH_20:
  214. phymode = MODE_11NA_HT20;
  215. break;
  216. case NL80211_CHAN_WIDTH_40:
  217. phymode = MODE_11NA_HT40;
  218. break;
  219. case NL80211_CHAN_WIDTH_80:
  220. phymode = MODE_11AC_VHT80;
  221. break;
  222. case NL80211_CHAN_WIDTH_5:
  223. case NL80211_CHAN_WIDTH_10:
  224. case NL80211_CHAN_WIDTH_80P80:
  225. case NL80211_CHAN_WIDTH_160:
  226. phymode = MODE_UNKNOWN;
  227. break;
  228. }
  229. break;
  230. default:
  231. break;
  232. }
  233. WARN_ON(phymode == MODE_UNKNOWN);
  234. return phymode;
  235. }
  236. static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
  237. {
  238. /*
  239. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  240. * 0 for no restriction
  241. * 1 for 1/4 us
  242. * 2 for 1/2 us
  243. * 3 for 1 us
  244. * 4 for 2 us
  245. * 5 for 4 us
  246. * 6 for 8 us
  247. * 7 for 16 us
  248. */
  249. switch (mpdudensity) {
  250. case 0:
  251. return 0;
  252. case 1:
  253. case 2:
  254. case 3:
  255. /* Our lower layer calculations limit our precision to
  256. 1 microsecond */
  257. return 1;
  258. case 4:
  259. return 2;
  260. case 5:
  261. return 4;
  262. case 6:
  263. return 8;
  264. case 7:
  265. return 16;
  266. default:
  267. return 0;
  268. }
  269. }
  270. static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
  271. {
  272. int ret;
  273. lockdep_assert_held(&ar->conf_mutex);
  274. ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
  275. if (ret) {
  276. ath10k_warn("Failed to create wmi peer: %i\n", ret);
  277. return ret;
  278. }
  279. ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
  280. if (ret) {
  281. ath10k_warn("Failed to wait for created wmi peer: %i\n", ret);
  282. return ret;
  283. }
  284. return 0;
  285. }
  286. static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
  287. {
  288. struct ath10k *ar = arvif->ar;
  289. u32 vdev_param;
  290. if (value != 0xFFFFFFFF)
  291. value = min_t(u32, arvif->ar->hw->wiphy->rts_threshold,
  292. ATH10K_RTS_MAX);
  293. vdev_param = ar->wmi.vdev_param->rts_threshold;
  294. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
  295. }
  296. static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
  297. {
  298. struct ath10k *ar = arvif->ar;
  299. u32 vdev_param;
  300. if (value != 0xFFFFFFFF)
  301. value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
  302. ATH10K_FRAGMT_THRESHOLD_MIN,
  303. ATH10K_FRAGMT_THRESHOLD_MAX);
  304. vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
  305. return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
  306. }
  307. static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
  308. {
  309. int ret;
  310. lockdep_assert_held(&ar->conf_mutex);
  311. ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
  312. if (ret)
  313. return ret;
  314. ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
  315. if (ret)
  316. return ret;
  317. return 0;
  318. }
  319. static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
  320. {
  321. struct ath10k_peer *peer, *tmp;
  322. lockdep_assert_held(&ar->conf_mutex);
  323. spin_lock_bh(&ar->data_lock);
  324. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  325. if (peer->vdev_id != vdev_id)
  326. continue;
  327. ath10k_warn("removing stale peer %pM from vdev_id %d\n",
  328. peer->addr, vdev_id);
  329. list_del(&peer->list);
  330. kfree(peer);
  331. }
  332. spin_unlock_bh(&ar->data_lock);
  333. }
  334. static void ath10k_peer_cleanup_all(struct ath10k *ar)
  335. {
  336. struct ath10k_peer *peer, *tmp;
  337. lockdep_assert_held(&ar->conf_mutex);
  338. spin_lock_bh(&ar->data_lock);
  339. list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
  340. list_del(&peer->list);
  341. kfree(peer);
  342. }
  343. spin_unlock_bh(&ar->data_lock);
  344. }
  345. /************************/
  346. /* Interface management */
  347. /************************/
  348. static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
  349. {
  350. int ret;
  351. lockdep_assert_held(&ar->conf_mutex);
  352. ret = wait_for_completion_timeout(&ar->vdev_setup_done,
  353. ATH10K_VDEV_SETUP_TIMEOUT_HZ);
  354. if (ret == 0)
  355. return -ETIMEDOUT;
  356. return 0;
  357. }
  358. static int ath10k_vdev_start(struct ath10k_vif *arvif)
  359. {
  360. struct ath10k *ar = arvif->ar;
  361. struct ieee80211_conf *conf = &ar->hw->conf;
  362. struct ieee80211_channel *channel = conf->chandef.chan;
  363. struct wmi_vdev_start_request_arg arg = {};
  364. int ret = 0;
  365. lockdep_assert_held(&ar->conf_mutex);
  366. reinit_completion(&ar->vdev_setup_done);
  367. arg.vdev_id = arvif->vdev_id;
  368. arg.dtim_period = arvif->dtim_period;
  369. arg.bcn_intval = arvif->beacon_interval;
  370. arg.channel.freq = channel->center_freq;
  371. arg.channel.band_center_freq1 = conf->chandef.center_freq1;
  372. arg.channel.mode = chan_to_phymode(&conf->chandef);
  373. arg.channel.min_power = 0;
  374. arg.channel.max_power = channel->max_power * 2;
  375. arg.channel.max_reg_power = channel->max_reg_power * 2;
  376. arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
  377. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  378. arg.ssid = arvif->u.ap.ssid;
  379. arg.ssid_len = arvif->u.ap.ssid_len;
  380. arg.hidden_ssid = arvif->u.ap.hidden_ssid;
  381. /* For now allow DFS for AP mode */
  382. arg.channel.chan_radar =
  383. !!(channel->flags & IEEE80211_CHAN_RADAR);
  384. } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
  385. arg.ssid = arvif->vif->bss_conf.ssid;
  386. arg.ssid_len = arvif->vif->bss_conf.ssid_len;
  387. }
  388. ath10k_dbg(ATH10K_DBG_MAC,
  389. "mac vdev %d start center_freq %d phymode %s\n",
  390. arg.vdev_id, arg.channel.freq,
  391. ath10k_wmi_phymode_str(arg.channel.mode));
  392. ret = ath10k_wmi_vdev_start(ar, &arg);
  393. if (ret) {
  394. ath10k_warn("WMI vdev start failed: ret %d\n", ret);
  395. return ret;
  396. }
  397. ret = ath10k_vdev_setup_sync(ar);
  398. if (ret) {
  399. ath10k_warn("vdev setup failed %d\n", ret);
  400. return ret;
  401. }
  402. return ret;
  403. }
  404. static int ath10k_vdev_stop(struct ath10k_vif *arvif)
  405. {
  406. struct ath10k *ar = arvif->ar;
  407. int ret;
  408. lockdep_assert_held(&ar->conf_mutex);
  409. reinit_completion(&ar->vdev_setup_done);
  410. ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
  411. if (ret) {
  412. ath10k_warn("WMI vdev stop failed: ret %d\n", ret);
  413. return ret;
  414. }
  415. ret = ath10k_vdev_setup_sync(ar);
  416. if (ret) {
  417. ath10k_warn("vdev setup failed %d\n", ret);
  418. return ret;
  419. }
  420. return ret;
  421. }
  422. static int ath10k_monitor_start(struct ath10k *ar, int vdev_id)
  423. {
  424. struct ieee80211_channel *channel = ar->hw->conf.chandef.chan;
  425. struct wmi_vdev_start_request_arg arg = {};
  426. int ret = 0;
  427. lockdep_assert_held(&ar->conf_mutex);
  428. if (!ar->monitor_present) {
  429. ath10k_warn("mac montor stop -- monitor is not present\n");
  430. return -EINVAL;
  431. }
  432. arg.vdev_id = vdev_id;
  433. arg.channel.freq = channel->center_freq;
  434. arg.channel.band_center_freq1 = ar->hw->conf.chandef.center_freq1;
  435. /* TODO setup this dynamically, what in case we
  436. don't have any vifs? */
  437. arg.channel.mode = chan_to_phymode(&ar->hw->conf.chandef);
  438. arg.channel.chan_radar =
  439. !!(channel->flags & IEEE80211_CHAN_RADAR);
  440. arg.channel.min_power = 0;
  441. arg.channel.max_power = channel->max_power * 2;
  442. arg.channel.max_reg_power = channel->max_reg_power * 2;
  443. arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
  444. ret = ath10k_wmi_vdev_start(ar, &arg);
  445. if (ret) {
  446. ath10k_warn("Monitor vdev start failed: ret %d\n", ret);
  447. return ret;
  448. }
  449. ret = ath10k_vdev_setup_sync(ar);
  450. if (ret) {
  451. ath10k_warn("Monitor vdev setup failed %d\n", ret);
  452. return ret;
  453. }
  454. ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
  455. if (ret) {
  456. ath10k_warn("Monitor vdev up failed: %d\n", ret);
  457. goto vdev_stop;
  458. }
  459. ar->monitor_vdev_id = vdev_id;
  460. ar->monitor_enabled = true;
  461. return 0;
  462. vdev_stop:
  463. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  464. if (ret)
  465. ath10k_warn("Monitor vdev stop failed: %d\n", ret);
  466. return ret;
  467. }
  468. static int ath10k_monitor_stop(struct ath10k *ar)
  469. {
  470. int ret = 0;
  471. lockdep_assert_held(&ar->conf_mutex);
  472. if (!ar->monitor_present) {
  473. ath10k_warn("mac montor stop -- monitor is not present\n");
  474. return -EINVAL;
  475. }
  476. if (!ar->monitor_enabled) {
  477. ath10k_warn("mac montor stop -- monitor is not enabled\n");
  478. return -EINVAL;
  479. }
  480. ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
  481. if (ret)
  482. ath10k_warn("Monitor vdev down failed: %d\n", ret);
  483. ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
  484. if (ret)
  485. ath10k_warn("Monitor vdev stop failed: %d\n", ret);
  486. ret = ath10k_vdev_setup_sync(ar);
  487. if (ret)
  488. ath10k_warn("Monitor_down sync failed: %d\n", ret);
  489. ar->monitor_enabled = false;
  490. return ret;
  491. }
  492. static int ath10k_monitor_create(struct ath10k *ar)
  493. {
  494. int bit, ret = 0;
  495. lockdep_assert_held(&ar->conf_mutex);
  496. if (ar->monitor_present) {
  497. ath10k_warn("Monitor mode already enabled\n");
  498. return 0;
  499. }
  500. bit = ffs(ar->free_vdev_map);
  501. if (bit == 0) {
  502. ath10k_warn("No free VDEV slots\n");
  503. return -ENOMEM;
  504. }
  505. ar->monitor_vdev_id = bit - 1;
  506. ar->free_vdev_map &= ~(1 << ar->monitor_vdev_id);
  507. ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
  508. WMI_VDEV_TYPE_MONITOR,
  509. 0, ar->mac_addr);
  510. if (ret) {
  511. ath10k_warn("WMI vdev monitor create failed: ret %d\n", ret);
  512. goto vdev_fail;
  513. }
  514. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
  515. ar->monitor_vdev_id);
  516. ar->monitor_present = true;
  517. return 0;
  518. vdev_fail:
  519. /*
  520. * Restore the ID to the global map.
  521. */
  522. ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
  523. return ret;
  524. }
  525. static int ath10k_monitor_destroy(struct ath10k *ar)
  526. {
  527. int ret = 0;
  528. lockdep_assert_held(&ar->conf_mutex);
  529. if (!ar->monitor_present)
  530. return 0;
  531. ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
  532. if (ret) {
  533. ath10k_warn("WMI vdev monitor delete failed: %d\n", ret);
  534. return ret;
  535. }
  536. ar->free_vdev_map |= 1 << (ar->monitor_vdev_id);
  537. ar->monitor_present = false;
  538. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
  539. ar->monitor_vdev_id);
  540. return ret;
  541. }
  542. static int ath10k_start_cac(struct ath10k *ar)
  543. {
  544. int ret;
  545. lockdep_assert_held(&ar->conf_mutex);
  546. set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  547. ret = ath10k_monitor_create(ar);
  548. if (ret) {
  549. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  550. return ret;
  551. }
  552. ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
  553. if (ret) {
  554. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  555. ath10k_monitor_destroy(ar);
  556. return ret;
  557. }
  558. ath10k_dbg(ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
  559. ar->monitor_vdev_id);
  560. return 0;
  561. }
  562. static int ath10k_stop_cac(struct ath10k *ar)
  563. {
  564. lockdep_assert_held(&ar->conf_mutex);
  565. /* CAC is not running - do nothing */
  566. if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
  567. return 0;
  568. ath10k_monitor_stop(ar);
  569. ath10k_monitor_destroy(ar);
  570. clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
  571. ath10k_dbg(ATH10K_DBG_MAC, "mac cac finished\n");
  572. return 0;
  573. }
  574. static const char *ath10k_dfs_state(enum nl80211_dfs_state dfs_state)
  575. {
  576. switch (dfs_state) {
  577. case NL80211_DFS_USABLE:
  578. return "USABLE";
  579. case NL80211_DFS_UNAVAILABLE:
  580. return "UNAVAILABLE";
  581. case NL80211_DFS_AVAILABLE:
  582. return "AVAILABLE";
  583. default:
  584. WARN_ON(1);
  585. return "bug";
  586. }
  587. }
  588. static void ath10k_config_radar_detection(struct ath10k *ar)
  589. {
  590. struct ieee80211_channel *chan = ar->hw->conf.chandef.chan;
  591. bool radar = ar->hw->conf.radar_enabled;
  592. bool chan_radar = !!(chan->flags & IEEE80211_CHAN_RADAR);
  593. enum nl80211_dfs_state dfs_state = chan->dfs_state;
  594. int ret;
  595. lockdep_assert_held(&ar->conf_mutex);
  596. ath10k_dbg(ATH10K_DBG_MAC,
  597. "mac radar config update: chan %dMHz radar %d chan radar %d chan state %s\n",
  598. chan->center_freq, radar, chan_radar,
  599. ath10k_dfs_state(dfs_state));
  600. /*
  601. * It's safe to call it even if CAC is not started.
  602. * This call here guarantees changing channel, etc. will stop CAC.
  603. */
  604. ath10k_stop_cac(ar);
  605. if (!radar)
  606. return;
  607. if (!chan_radar)
  608. return;
  609. if (dfs_state != NL80211_DFS_USABLE)
  610. return;
  611. ret = ath10k_start_cac(ar);
  612. if (ret) {
  613. /*
  614. * Not possible to start CAC on current channel so starting
  615. * radiation is not allowed, make this channel DFS_UNAVAILABLE
  616. * by indicating that radar was detected.
  617. */
  618. ath10k_warn("failed to start CAC (%d)\n", ret);
  619. ieee80211_radar_detected(ar->hw);
  620. }
  621. }
  622. static void ath10k_control_beaconing(struct ath10k_vif *arvif,
  623. struct ieee80211_bss_conf *info)
  624. {
  625. int ret = 0;
  626. lockdep_assert_held(&arvif->ar->conf_mutex);
  627. if (!info->enable_beacon) {
  628. ath10k_vdev_stop(arvif);
  629. return;
  630. }
  631. arvif->tx_seq_no = 0x1000;
  632. ret = ath10k_vdev_start(arvif);
  633. if (ret)
  634. return;
  635. ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, 0, info->bssid);
  636. if (ret) {
  637. ath10k_warn("Failed to bring up VDEV: %d\n",
  638. arvif->vdev_id);
  639. return;
  640. }
  641. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
  642. }
  643. static void ath10k_control_ibss(struct ath10k_vif *arvif,
  644. struct ieee80211_bss_conf *info,
  645. const u8 self_peer[ETH_ALEN])
  646. {
  647. u32 vdev_param;
  648. int ret = 0;
  649. lockdep_assert_held(&arvif->ar->conf_mutex);
  650. if (!info->ibss_joined) {
  651. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
  652. if (ret)
  653. ath10k_warn("Failed to delete IBSS self peer:%pM for VDEV:%d ret:%d\n",
  654. self_peer, arvif->vdev_id, ret);
  655. if (is_zero_ether_addr(arvif->u.ibss.bssid))
  656. return;
  657. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id,
  658. arvif->u.ibss.bssid);
  659. if (ret) {
  660. ath10k_warn("Failed to delete IBSS BSSID peer:%pM for VDEV:%d ret:%d\n",
  661. arvif->u.ibss.bssid, arvif->vdev_id, ret);
  662. return;
  663. }
  664. memset(arvif->u.ibss.bssid, 0, ETH_ALEN);
  665. return;
  666. }
  667. ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
  668. if (ret) {
  669. ath10k_warn("Failed to create IBSS self peer:%pM for VDEV:%d ret:%d\n",
  670. self_peer, arvif->vdev_id, ret);
  671. return;
  672. }
  673. vdev_param = arvif->ar->wmi.vdev_param->atim_window;
  674. ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
  675. ATH10K_DEFAULT_ATIM);
  676. if (ret)
  677. ath10k_warn("Failed to set IBSS ATIM for VDEV:%d ret:%d\n",
  678. arvif->vdev_id, ret);
  679. }
  680. /*
  681. * Review this when mac80211 gains per-interface powersave support.
  682. */
  683. static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
  684. {
  685. struct ath10k *ar = arvif->ar;
  686. struct ieee80211_conf *conf = &ar->hw->conf;
  687. enum wmi_sta_powersave_param param;
  688. enum wmi_sta_ps_mode psmode;
  689. int ret;
  690. lockdep_assert_held(&arvif->ar->conf_mutex);
  691. if (arvif->vif->type != NL80211_IFTYPE_STATION)
  692. return 0;
  693. if (conf->flags & IEEE80211_CONF_PS) {
  694. psmode = WMI_STA_PS_MODE_ENABLED;
  695. param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
  696. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
  697. conf->dynamic_ps_timeout);
  698. if (ret) {
  699. ath10k_warn("Failed to set inactivity time for VDEV: %d\n",
  700. arvif->vdev_id);
  701. return ret;
  702. }
  703. } else {
  704. psmode = WMI_STA_PS_MODE_DISABLED;
  705. }
  706. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
  707. arvif->vdev_id, psmode ? "enable" : "disable");
  708. ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
  709. if (ret) {
  710. ath10k_warn("Failed to set PS Mode: %d for VDEV: %d\n",
  711. psmode, arvif->vdev_id);
  712. return ret;
  713. }
  714. return 0;
  715. }
  716. /**********************/
  717. /* Station management */
  718. /**********************/
  719. static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
  720. struct ath10k_vif *arvif,
  721. struct ieee80211_sta *sta,
  722. struct ieee80211_bss_conf *bss_conf,
  723. struct wmi_peer_assoc_complete_arg *arg)
  724. {
  725. lockdep_assert_held(&ar->conf_mutex);
  726. memcpy(arg->addr, sta->addr, ETH_ALEN);
  727. arg->vdev_id = arvif->vdev_id;
  728. arg->peer_aid = sta->aid;
  729. arg->peer_flags |= WMI_PEER_AUTH;
  730. if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
  731. /*
  732. * Seems FW have problems with Power Save in STA
  733. * mode when we setup this parameter to high (eg. 5).
  734. * Often we see that FW don't send NULL (with clean P flags)
  735. * frame even there is info about buffered frames in beacons.
  736. * Sometimes we have to wait more than 10 seconds before FW
  737. * will wakeup. Often sending one ping from AP to our device
  738. * just fail (more than 50%).
  739. *
  740. * Seems setting this FW parameter to 1 couse FW
  741. * will check every beacon and will wakup immediately
  742. * after detection buffered data.
  743. */
  744. arg->peer_listen_intval = 1;
  745. else
  746. arg->peer_listen_intval = ar->hw->conf.listen_interval;
  747. arg->peer_num_spatial_streams = 1;
  748. /*
  749. * The assoc capabilities are available only in managed mode.
  750. */
  751. if (arvif->vdev_type == WMI_VDEV_TYPE_STA && bss_conf)
  752. arg->peer_caps = bss_conf->assoc_capability;
  753. }
  754. static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
  755. struct ath10k_vif *arvif,
  756. struct wmi_peer_assoc_complete_arg *arg)
  757. {
  758. struct ieee80211_vif *vif = arvif->vif;
  759. struct ieee80211_bss_conf *info = &vif->bss_conf;
  760. struct cfg80211_bss *bss;
  761. const u8 *rsnie = NULL;
  762. const u8 *wpaie = NULL;
  763. lockdep_assert_held(&ar->conf_mutex);
  764. bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
  765. info->bssid, NULL, 0, 0, 0);
  766. if (bss) {
  767. const struct cfg80211_bss_ies *ies;
  768. rcu_read_lock();
  769. rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
  770. ies = rcu_dereference(bss->ies);
  771. wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
  772. WLAN_OUI_TYPE_MICROSOFT_WPA,
  773. ies->data,
  774. ies->len);
  775. rcu_read_unlock();
  776. cfg80211_put_bss(ar->hw->wiphy, bss);
  777. }
  778. /* FIXME: base on RSN IE/WPA IE is a correct idea? */
  779. if (rsnie || wpaie) {
  780. ath10k_dbg(ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
  781. arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
  782. }
  783. if (wpaie) {
  784. ath10k_dbg(ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
  785. arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
  786. }
  787. }
  788. static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
  789. struct ieee80211_sta *sta,
  790. struct wmi_peer_assoc_complete_arg *arg)
  791. {
  792. struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
  793. const struct ieee80211_supported_band *sband;
  794. const struct ieee80211_rate *rates;
  795. u32 ratemask;
  796. int i;
  797. lockdep_assert_held(&ar->conf_mutex);
  798. sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
  799. ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
  800. rates = sband->bitrates;
  801. rateset->num_rates = 0;
  802. for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
  803. if (!(ratemask & 1))
  804. continue;
  805. rateset->rates[rateset->num_rates] = rates->hw_value;
  806. rateset->num_rates++;
  807. }
  808. }
  809. static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
  810. struct ieee80211_sta *sta,
  811. struct wmi_peer_assoc_complete_arg *arg)
  812. {
  813. const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  814. int smps;
  815. int i, n;
  816. lockdep_assert_held(&ar->conf_mutex);
  817. if (!ht_cap->ht_supported)
  818. return;
  819. arg->peer_flags |= WMI_PEER_HT;
  820. arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  821. ht_cap->ampdu_factor)) - 1;
  822. arg->peer_mpdu_density =
  823. ath10k_parse_mpdudensity(ht_cap->ampdu_density);
  824. arg->peer_ht_caps = ht_cap->cap;
  825. arg->peer_rate_caps |= WMI_RC_HT_FLAG;
  826. if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
  827. arg->peer_flags |= WMI_PEER_LDPC;
  828. if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
  829. arg->peer_flags |= WMI_PEER_40MHZ;
  830. arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
  831. }
  832. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
  833. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  834. if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
  835. arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
  836. if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
  837. arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
  838. arg->peer_flags |= WMI_PEER_STBC;
  839. }
  840. if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
  841. u32 stbc;
  842. stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
  843. stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
  844. stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
  845. arg->peer_rate_caps |= stbc;
  846. arg->peer_flags |= WMI_PEER_STBC;
  847. }
  848. smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
  849. smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
  850. if (smps == WLAN_HT_CAP_SM_PS_STATIC) {
  851. arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
  852. arg->peer_flags |= WMI_PEER_STATIC_MIMOPS;
  853. } else if (smps == WLAN_HT_CAP_SM_PS_DYNAMIC) {
  854. arg->peer_flags |= WMI_PEER_SPATIAL_MUX;
  855. arg->peer_flags |= WMI_PEER_DYN_MIMOPS;
  856. }
  857. if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
  858. arg->peer_rate_caps |= WMI_RC_TS_FLAG;
  859. else if (ht_cap->mcs.rx_mask[1])
  860. arg->peer_rate_caps |= WMI_RC_DS_FLAG;
  861. for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
  862. if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
  863. arg->peer_ht_rates.rates[n++] = i;
  864. arg->peer_ht_rates.num_rates = n;
  865. arg->peer_num_spatial_streams = max((n+7) / 8, 1);
  866. ath10k_dbg(ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
  867. arg->addr,
  868. arg->peer_ht_rates.num_rates,
  869. arg->peer_num_spatial_streams);
  870. }
  871. static void ath10k_peer_assoc_h_qos_ap(struct ath10k *ar,
  872. struct ath10k_vif *arvif,
  873. struct ieee80211_sta *sta,
  874. struct ieee80211_bss_conf *bss_conf,
  875. struct wmi_peer_assoc_complete_arg *arg)
  876. {
  877. u32 uapsd = 0;
  878. u32 max_sp = 0;
  879. lockdep_assert_held(&ar->conf_mutex);
  880. if (sta->wme)
  881. arg->peer_flags |= WMI_PEER_QOS;
  882. if (sta->wme && sta->uapsd_queues) {
  883. ath10k_dbg(ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
  884. sta->uapsd_queues, sta->max_sp);
  885. arg->peer_flags |= WMI_PEER_APSD;
  886. arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
  887. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
  888. uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
  889. WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
  890. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
  891. uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
  892. WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
  893. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
  894. uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
  895. WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
  896. if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
  897. uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
  898. WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
  899. if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
  900. max_sp = sta->max_sp;
  901. ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  902. sta->addr,
  903. WMI_AP_PS_PEER_PARAM_UAPSD,
  904. uapsd);
  905. ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  906. sta->addr,
  907. WMI_AP_PS_PEER_PARAM_MAX_SP,
  908. max_sp);
  909. /* TODO setup this based on STA listen interval and
  910. beacon interval. Currently we don't know
  911. sta->listen_interval - mac80211 patch required.
  912. Currently use 10 seconds */
  913. ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
  914. sta->addr,
  915. WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
  916. 10);
  917. }
  918. }
  919. static void ath10k_peer_assoc_h_qos_sta(struct ath10k *ar,
  920. struct ath10k_vif *arvif,
  921. struct ieee80211_sta *sta,
  922. struct ieee80211_bss_conf *bss_conf,
  923. struct wmi_peer_assoc_complete_arg *arg)
  924. {
  925. if (bss_conf->qos)
  926. arg->peer_flags |= WMI_PEER_QOS;
  927. }
  928. static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
  929. struct ieee80211_sta *sta,
  930. struct wmi_peer_assoc_complete_arg *arg)
  931. {
  932. const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  933. u8 ampdu_factor;
  934. if (!vht_cap->vht_supported)
  935. return;
  936. arg->peer_flags |= WMI_PEER_VHT;
  937. arg->peer_vht_caps = vht_cap->cap;
  938. ampdu_factor = (vht_cap->cap &
  939. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
  940. IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
  941. /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
  942. * zero in VHT IE. Using it would result in degraded throughput.
  943. * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
  944. * it if VHT max_mpdu is smaller. */
  945. arg->peer_max_mpdu = max(arg->peer_max_mpdu,
  946. (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  947. ampdu_factor)) - 1);
  948. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  949. arg->peer_flags |= WMI_PEER_80MHZ;
  950. arg->peer_vht_rates.rx_max_rate =
  951. __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
  952. arg->peer_vht_rates.rx_mcs_set =
  953. __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
  954. arg->peer_vht_rates.tx_max_rate =
  955. __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
  956. arg->peer_vht_rates.tx_mcs_set =
  957. __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
  958. ath10k_dbg(ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
  959. sta->addr, arg->peer_max_mpdu, arg->peer_flags);
  960. }
  961. static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
  962. struct ath10k_vif *arvif,
  963. struct ieee80211_sta *sta,
  964. struct ieee80211_bss_conf *bss_conf,
  965. struct wmi_peer_assoc_complete_arg *arg)
  966. {
  967. switch (arvif->vdev_type) {
  968. case WMI_VDEV_TYPE_AP:
  969. ath10k_peer_assoc_h_qos_ap(ar, arvif, sta, bss_conf, arg);
  970. break;
  971. case WMI_VDEV_TYPE_STA:
  972. ath10k_peer_assoc_h_qos_sta(ar, arvif, sta, bss_conf, arg);
  973. break;
  974. default:
  975. break;
  976. }
  977. }
  978. static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
  979. struct ath10k_vif *arvif,
  980. struct ieee80211_sta *sta,
  981. struct wmi_peer_assoc_complete_arg *arg)
  982. {
  983. enum wmi_phy_mode phymode = MODE_UNKNOWN;
  984. switch (ar->hw->conf.chandef.chan->band) {
  985. case IEEE80211_BAND_2GHZ:
  986. if (sta->ht_cap.ht_supported) {
  987. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  988. phymode = MODE_11NG_HT40;
  989. else
  990. phymode = MODE_11NG_HT20;
  991. } else {
  992. phymode = MODE_11G;
  993. }
  994. break;
  995. case IEEE80211_BAND_5GHZ:
  996. /*
  997. * Check VHT first.
  998. */
  999. if (sta->vht_cap.vht_supported) {
  1000. if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
  1001. phymode = MODE_11AC_VHT80;
  1002. else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1003. phymode = MODE_11AC_VHT40;
  1004. else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
  1005. phymode = MODE_11AC_VHT20;
  1006. } else if (sta->ht_cap.ht_supported) {
  1007. if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
  1008. phymode = MODE_11NA_HT40;
  1009. else
  1010. phymode = MODE_11NA_HT20;
  1011. } else {
  1012. phymode = MODE_11A;
  1013. }
  1014. break;
  1015. default:
  1016. break;
  1017. }
  1018. ath10k_dbg(ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
  1019. sta->addr, ath10k_wmi_phymode_str(phymode));
  1020. arg->peer_phymode = phymode;
  1021. WARN_ON(phymode == MODE_UNKNOWN);
  1022. }
  1023. static int ath10k_peer_assoc_prepare(struct ath10k *ar,
  1024. struct ath10k_vif *arvif,
  1025. struct ieee80211_sta *sta,
  1026. struct ieee80211_bss_conf *bss_conf,
  1027. struct wmi_peer_assoc_complete_arg *arg)
  1028. {
  1029. lockdep_assert_held(&ar->conf_mutex);
  1030. memset(arg, 0, sizeof(*arg));
  1031. ath10k_peer_assoc_h_basic(ar, arvif, sta, bss_conf, arg);
  1032. ath10k_peer_assoc_h_crypto(ar, arvif, arg);
  1033. ath10k_peer_assoc_h_rates(ar, sta, arg);
  1034. ath10k_peer_assoc_h_ht(ar, sta, arg);
  1035. ath10k_peer_assoc_h_vht(ar, sta, arg);
  1036. ath10k_peer_assoc_h_qos(ar, arvif, sta, bss_conf, arg);
  1037. ath10k_peer_assoc_h_phymode(ar, arvif, sta, arg);
  1038. return 0;
  1039. }
  1040. /* can be called only in mac80211 callbacks due to `key_count` usage */
  1041. static void ath10k_bss_assoc(struct ieee80211_hw *hw,
  1042. struct ieee80211_vif *vif,
  1043. struct ieee80211_bss_conf *bss_conf)
  1044. {
  1045. struct ath10k *ar = hw->priv;
  1046. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1047. struct wmi_peer_assoc_complete_arg peer_arg;
  1048. struct ieee80211_sta *ap_sta;
  1049. int ret;
  1050. lockdep_assert_held(&ar->conf_mutex);
  1051. rcu_read_lock();
  1052. ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
  1053. if (!ap_sta) {
  1054. ath10k_warn("Failed to find station entry for %pM\n",
  1055. bss_conf->bssid);
  1056. rcu_read_unlock();
  1057. return;
  1058. }
  1059. ret = ath10k_peer_assoc_prepare(ar, arvif, ap_sta,
  1060. bss_conf, &peer_arg);
  1061. if (ret) {
  1062. ath10k_warn("Peer assoc prepare failed for %pM\n: %d",
  1063. bss_conf->bssid, ret);
  1064. rcu_read_unlock();
  1065. return;
  1066. }
  1067. rcu_read_unlock();
  1068. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  1069. if (ret) {
  1070. ath10k_warn("Peer assoc failed for %pM\n: %d",
  1071. bss_conf->bssid, ret);
  1072. return;
  1073. }
  1074. ath10k_dbg(ATH10K_DBG_MAC,
  1075. "mac vdev %d up (associated) bssid %pM aid %d\n",
  1076. arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
  1077. ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, bss_conf->aid,
  1078. bss_conf->bssid);
  1079. if (ret)
  1080. ath10k_warn("VDEV: %d up failed: ret %d\n",
  1081. arvif->vdev_id, ret);
  1082. }
  1083. /*
  1084. * FIXME: flush TIDs
  1085. */
  1086. static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
  1087. struct ieee80211_vif *vif)
  1088. {
  1089. struct ath10k *ar = hw->priv;
  1090. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1091. int ret;
  1092. lockdep_assert_held(&ar->conf_mutex);
  1093. /*
  1094. * For some reason, calling VDEV-DOWN before VDEV-STOP
  1095. * makes the FW to send frames via HTT after disassociation.
  1096. * No idea why this happens, even though VDEV-DOWN is supposed
  1097. * to be analogous to link down, so just stop the VDEV.
  1098. */
  1099. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d stop (disassociated\n",
  1100. arvif->vdev_id);
  1101. /* FIXME: check return value */
  1102. ret = ath10k_vdev_stop(arvif);
  1103. /*
  1104. * If we don't call VDEV-DOWN after VDEV-STOP FW will remain active and
  1105. * report beacons from previously associated network through HTT.
  1106. * This in turn would spam mac80211 WARN_ON if we bring down all
  1107. * interfaces as it expects there is no rx when no interface is
  1108. * running.
  1109. */
  1110. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d down\n", arvif->vdev_id);
  1111. /* FIXME: why don't we print error if wmi call fails? */
  1112. ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
  1113. arvif->def_wep_key_idx = 0;
  1114. }
  1115. static int ath10k_station_assoc(struct ath10k *ar, struct ath10k_vif *arvif,
  1116. struct ieee80211_sta *sta)
  1117. {
  1118. struct wmi_peer_assoc_complete_arg peer_arg;
  1119. int ret = 0;
  1120. lockdep_assert_held(&ar->conf_mutex);
  1121. ret = ath10k_peer_assoc_prepare(ar, arvif, sta, NULL, &peer_arg);
  1122. if (ret) {
  1123. ath10k_warn("WMI peer assoc prepare failed for %pM\n",
  1124. sta->addr);
  1125. return ret;
  1126. }
  1127. ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
  1128. if (ret) {
  1129. ath10k_warn("Peer assoc failed for STA %pM\n: %d",
  1130. sta->addr, ret);
  1131. return ret;
  1132. }
  1133. ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
  1134. if (ret) {
  1135. ath10k_warn("could not install peer wep keys (%d)\n", ret);
  1136. return ret;
  1137. }
  1138. return ret;
  1139. }
  1140. static int ath10k_station_disassoc(struct ath10k *ar, struct ath10k_vif *arvif,
  1141. struct ieee80211_sta *sta)
  1142. {
  1143. int ret = 0;
  1144. lockdep_assert_held(&ar->conf_mutex);
  1145. ret = ath10k_clear_peer_keys(arvif, sta->addr);
  1146. if (ret) {
  1147. ath10k_warn("could not clear all peer wep keys (%d)\n", ret);
  1148. return ret;
  1149. }
  1150. return ret;
  1151. }
  1152. /**************/
  1153. /* Regulatory */
  1154. /**************/
  1155. static int ath10k_update_channel_list(struct ath10k *ar)
  1156. {
  1157. struct ieee80211_hw *hw = ar->hw;
  1158. struct ieee80211_supported_band **bands;
  1159. enum ieee80211_band band;
  1160. struct ieee80211_channel *channel;
  1161. struct wmi_scan_chan_list_arg arg = {0};
  1162. struct wmi_channel_arg *ch;
  1163. bool passive;
  1164. int len;
  1165. int ret;
  1166. int i;
  1167. lockdep_assert_held(&ar->conf_mutex);
  1168. bands = hw->wiphy->bands;
  1169. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1170. if (!bands[band])
  1171. continue;
  1172. for (i = 0; i < bands[band]->n_channels; i++) {
  1173. if (bands[band]->channels[i].flags &
  1174. IEEE80211_CHAN_DISABLED)
  1175. continue;
  1176. arg.n_channels++;
  1177. }
  1178. }
  1179. len = sizeof(struct wmi_channel_arg) * arg.n_channels;
  1180. arg.channels = kzalloc(len, GFP_KERNEL);
  1181. if (!arg.channels)
  1182. return -ENOMEM;
  1183. ch = arg.channels;
  1184. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1185. if (!bands[band])
  1186. continue;
  1187. for (i = 0; i < bands[band]->n_channels; i++) {
  1188. channel = &bands[band]->channels[i];
  1189. if (channel->flags & IEEE80211_CHAN_DISABLED)
  1190. continue;
  1191. ch->allow_ht = true;
  1192. /* FIXME: when should we really allow VHT? */
  1193. ch->allow_vht = true;
  1194. ch->allow_ibss =
  1195. !(channel->flags & IEEE80211_CHAN_NO_IR);
  1196. ch->ht40plus =
  1197. !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
  1198. ch->chan_radar =
  1199. !!(channel->flags & IEEE80211_CHAN_RADAR);
  1200. passive = channel->flags & IEEE80211_CHAN_NO_IR;
  1201. ch->passive = passive;
  1202. ch->freq = channel->center_freq;
  1203. ch->min_power = 0;
  1204. ch->max_power = channel->max_power * 2;
  1205. ch->max_reg_power = channel->max_reg_power * 2;
  1206. ch->max_antenna_gain = channel->max_antenna_gain * 2;
  1207. ch->reg_class_id = 0; /* FIXME */
  1208. /* FIXME: why use only legacy modes, why not any
  1209. * HT/VHT modes? Would that even make any
  1210. * difference? */
  1211. if (channel->band == IEEE80211_BAND_2GHZ)
  1212. ch->mode = MODE_11G;
  1213. else
  1214. ch->mode = MODE_11A;
  1215. if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
  1216. continue;
  1217. ath10k_dbg(ATH10K_DBG_WMI,
  1218. "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
  1219. ch - arg.channels, arg.n_channels,
  1220. ch->freq, ch->max_power, ch->max_reg_power,
  1221. ch->max_antenna_gain, ch->mode);
  1222. ch++;
  1223. }
  1224. }
  1225. ret = ath10k_wmi_scan_chan_list(ar, &arg);
  1226. kfree(arg.channels);
  1227. return ret;
  1228. }
  1229. static void ath10k_regd_update(struct ath10k *ar)
  1230. {
  1231. struct reg_dmn_pair_mapping *regpair;
  1232. int ret;
  1233. lockdep_assert_held(&ar->conf_mutex);
  1234. ret = ath10k_update_channel_list(ar);
  1235. if (ret)
  1236. ath10k_warn("could not update channel list (%d)\n", ret);
  1237. regpair = ar->ath_common.regulatory.regpair;
  1238. /* Target allows setting up per-band regdomain but ath_common provides
  1239. * a combined one only */
  1240. ret = ath10k_wmi_pdev_set_regdomain(ar,
  1241. regpair->regDmnEnum,
  1242. regpair->regDmnEnum, /* 2ghz */
  1243. regpair->regDmnEnum, /* 5ghz */
  1244. regpair->reg_2ghz_ctl,
  1245. regpair->reg_5ghz_ctl);
  1246. if (ret)
  1247. ath10k_warn("could not set pdev regdomain (%d)\n", ret);
  1248. }
  1249. static void ath10k_reg_notifier(struct wiphy *wiphy,
  1250. struct regulatory_request *request)
  1251. {
  1252. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1253. struct ath10k *ar = hw->priv;
  1254. bool result;
  1255. ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
  1256. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
  1257. ath10k_dbg(ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
  1258. request->dfs_region);
  1259. result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
  1260. request->dfs_region);
  1261. if (!result)
  1262. ath10k_warn("dfs region 0x%X not supported, will trigger radar for every pulse\n",
  1263. request->dfs_region);
  1264. }
  1265. mutex_lock(&ar->conf_mutex);
  1266. if (ar->state == ATH10K_STATE_ON)
  1267. ath10k_regd_update(ar);
  1268. mutex_unlock(&ar->conf_mutex);
  1269. }
  1270. /***************/
  1271. /* TX handlers */
  1272. /***************/
  1273. static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
  1274. {
  1275. if (ieee80211_is_mgmt(hdr->frame_control))
  1276. return HTT_DATA_TX_EXT_TID_MGMT;
  1277. if (!ieee80211_is_data_qos(hdr->frame_control))
  1278. return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  1279. if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
  1280. return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
  1281. return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
  1282. }
  1283. static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar,
  1284. struct ieee80211_tx_info *info)
  1285. {
  1286. if (info->control.vif)
  1287. return ath10k_vif_to_arvif(info->control.vif)->vdev_id;
  1288. if (ar->monitor_enabled)
  1289. return ar->monitor_vdev_id;
  1290. ath10k_warn("could not resolve vdev id\n");
  1291. return 0;
  1292. }
  1293. /*
  1294. * Frames sent to the FW have to be in "Native Wifi" format.
  1295. * Strip the QoS field from the 802.11 header.
  1296. */
  1297. static void ath10k_tx_h_qos_workaround(struct ieee80211_hw *hw,
  1298. struct ieee80211_tx_control *control,
  1299. struct sk_buff *skb)
  1300. {
  1301. struct ieee80211_hdr *hdr = (void *)skb->data;
  1302. u8 *qos_ctl;
  1303. if (!ieee80211_is_data_qos(hdr->frame_control))
  1304. return;
  1305. qos_ctl = ieee80211_get_qos_ctl(hdr);
  1306. memmove(skb->data + IEEE80211_QOS_CTL_LEN,
  1307. skb->data, (void *)qos_ctl - (void *)skb->data);
  1308. skb_pull(skb, IEEE80211_QOS_CTL_LEN);
  1309. }
  1310. static void ath10k_tx_wep_key_work(struct work_struct *work)
  1311. {
  1312. struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
  1313. wep_key_work);
  1314. int ret, keyidx = arvif->def_wep_key_newidx;
  1315. if (arvif->def_wep_key_idx == keyidx)
  1316. return;
  1317. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
  1318. arvif->vdev_id, keyidx);
  1319. ret = ath10k_wmi_vdev_set_param(arvif->ar,
  1320. arvif->vdev_id,
  1321. arvif->ar->wmi.vdev_param->def_keyid,
  1322. keyidx);
  1323. if (ret) {
  1324. ath10k_warn("could not update wep keyidx (%d)\n", ret);
  1325. return;
  1326. }
  1327. arvif->def_wep_key_idx = keyidx;
  1328. }
  1329. static void ath10k_tx_h_update_wep_key(struct sk_buff *skb)
  1330. {
  1331. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1332. struct ieee80211_vif *vif = info->control.vif;
  1333. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1334. struct ath10k *ar = arvif->ar;
  1335. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1336. struct ieee80211_key_conf *key = info->control.hw_key;
  1337. if (!ieee80211_has_protected(hdr->frame_control))
  1338. return;
  1339. if (!key)
  1340. return;
  1341. if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
  1342. key->cipher != WLAN_CIPHER_SUITE_WEP104)
  1343. return;
  1344. if (key->keyidx == arvif->def_wep_key_idx)
  1345. return;
  1346. /* FIXME: Most likely a few frames will be TXed with an old key. Simply
  1347. * queueing frames until key index is updated is not an option because
  1348. * sk_buff may need more processing to be done, e.g. offchannel */
  1349. arvif->def_wep_key_newidx = key->keyidx;
  1350. ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
  1351. }
  1352. static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar, struct sk_buff *skb)
  1353. {
  1354. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1355. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1356. struct ieee80211_vif *vif = info->control.vif;
  1357. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1358. /* This is case only for P2P_GO */
  1359. if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
  1360. arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
  1361. return;
  1362. if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
  1363. spin_lock_bh(&ar->data_lock);
  1364. if (arvif->u.ap.noa_data)
  1365. if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
  1366. GFP_ATOMIC))
  1367. memcpy(skb_put(skb, arvif->u.ap.noa_len),
  1368. arvif->u.ap.noa_data,
  1369. arvif->u.ap.noa_len);
  1370. spin_unlock_bh(&ar->data_lock);
  1371. }
  1372. }
  1373. static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
  1374. {
  1375. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1376. int ret = 0;
  1377. if (ar->htt.target_version_major >= 3) {
  1378. /* Since HTT 3.0 there is no separate mgmt tx command */
  1379. ret = ath10k_htt_tx(&ar->htt, skb);
  1380. goto exit;
  1381. }
  1382. if (ieee80211_is_mgmt(hdr->frame_control)) {
  1383. if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  1384. ar->fw_features)) {
  1385. if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
  1386. ATH10K_MAX_NUM_MGMT_PENDING) {
  1387. ath10k_warn("wmi mgmt_tx queue limit reached\n");
  1388. ret = -EBUSY;
  1389. goto exit;
  1390. }
  1391. skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
  1392. ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
  1393. } else {
  1394. ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
  1395. }
  1396. } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
  1397. ar->fw_features) &&
  1398. ieee80211_is_nullfunc(hdr->frame_control)) {
  1399. /* FW does not report tx status properly for NullFunc frames
  1400. * unless they are sent through mgmt tx path. mac80211 sends
  1401. * those frames when it detects link/beacon loss and depends
  1402. * on the tx status to be correct. */
  1403. ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
  1404. } else {
  1405. ret = ath10k_htt_tx(&ar->htt, skb);
  1406. }
  1407. exit:
  1408. if (ret) {
  1409. ath10k_warn("tx failed (%d). dropping packet.\n", ret);
  1410. ieee80211_free_txskb(ar->hw, skb);
  1411. }
  1412. }
  1413. void ath10k_offchan_tx_purge(struct ath10k *ar)
  1414. {
  1415. struct sk_buff *skb;
  1416. for (;;) {
  1417. skb = skb_dequeue(&ar->offchan_tx_queue);
  1418. if (!skb)
  1419. break;
  1420. ieee80211_free_txskb(ar->hw, skb);
  1421. }
  1422. }
  1423. void ath10k_offchan_tx_work(struct work_struct *work)
  1424. {
  1425. struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
  1426. struct ath10k_peer *peer;
  1427. struct ieee80211_hdr *hdr;
  1428. struct sk_buff *skb;
  1429. const u8 *peer_addr;
  1430. int vdev_id;
  1431. int ret;
  1432. /* FW requirement: We must create a peer before FW will send out
  1433. * an offchannel frame. Otherwise the frame will be stuck and
  1434. * never transmitted. We delete the peer upon tx completion.
  1435. * It is unlikely that a peer for offchannel tx will already be
  1436. * present. However it may be in some rare cases so account for that.
  1437. * Otherwise we might remove a legitimate peer and break stuff. */
  1438. for (;;) {
  1439. skb = skb_dequeue(&ar->offchan_tx_queue);
  1440. if (!skb)
  1441. break;
  1442. mutex_lock(&ar->conf_mutex);
  1443. ath10k_dbg(ATH10K_DBG_MAC, "mac offchannel skb %p\n",
  1444. skb);
  1445. hdr = (struct ieee80211_hdr *)skb->data;
  1446. peer_addr = ieee80211_get_DA(hdr);
  1447. vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
  1448. spin_lock_bh(&ar->data_lock);
  1449. peer = ath10k_peer_find(ar, vdev_id, peer_addr);
  1450. spin_unlock_bh(&ar->data_lock);
  1451. if (peer)
  1452. /* FIXME: should this use ath10k_warn()? */
  1453. ath10k_dbg(ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
  1454. peer_addr, vdev_id);
  1455. if (!peer) {
  1456. ret = ath10k_peer_create(ar, vdev_id, peer_addr);
  1457. if (ret)
  1458. ath10k_warn("peer %pM on vdev %d not created (%d)\n",
  1459. peer_addr, vdev_id, ret);
  1460. }
  1461. spin_lock_bh(&ar->data_lock);
  1462. reinit_completion(&ar->offchan_tx_completed);
  1463. ar->offchan_tx_skb = skb;
  1464. spin_unlock_bh(&ar->data_lock);
  1465. ath10k_tx_htt(ar, skb);
  1466. ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
  1467. 3 * HZ);
  1468. if (ret <= 0)
  1469. ath10k_warn("timed out waiting for offchannel skb %p\n",
  1470. skb);
  1471. if (!peer) {
  1472. ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
  1473. if (ret)
  1474. ath10k_warn("peer %pM on vdev %d not deleted (%d)\n",
  1475. peer_addr, vdev_id, ret);
  1476. }
  1477. mutex_unlock(&ar->conf_mutex);
  1478. }
  1479. }
  1480. void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
  1481. {
  1482. struct sk_buff *skb;
  1483. for (;;) {
  1484. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  1485. if (!skb)
  1486. break;
  1487. ieee80211_free_txskb(ar->hw, skb);
  1488. }
  1489. }
  1490. void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
  1491. {
  1492. struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
  1493. struct sk_buff *skb;
  1494. int ret;
  1495. for (;;) {
  1496. skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
  1497. if (!skb)
  1498. break;
  1499. ret = ath10k_wmi_mgmt_tx(ar, skb);
  1500. if (ret) {
  1501. ath10k_warn("wmi mgmt_tx failed (%d)\n", ret);
  1502. ieee80211_free_txskb(ar->hw, skb);
  1503. }
  1504. }
  1505. }
  1506. /************/
  1507. /* Scanning */
  1508. /************/
  1509. /*
  1510. * This gets called if we dont get a heart-beat during scan.
  1511. * This may indicate the FW has hung and we need to abort the
  1512. * scan manually to prevent cancel_hw_scan() from deadlocking
  1513. */
  1514. void ath10k_reset_scan(unsigned long ptr)
  1515. {
  1516. struct ath10k *ar = (struct ath10k *)ptr;
  1517. spin_lock_bh(&ar->data_lock);
  1518. if (!ar->scan.in_progress) {
  1519. spin_unlock_bh(&ar->data_lock);
  1520. return;
  1521. }
  1522. ath10k_warn("scan timeout. resetting. fw issue?\n");
  1523. if (ar->scan.is_roc)
  1524. ieee80211_remain_on_channel_expired(ar->hw);
  1525. else
  1526. ieee80211_scan_completed(ar->hw, 1 /* aborted */);
  1527. ar->scan.in_progress = false;
  1528. complete_all(&ar->scan.completed);
  1529. spin_unlock_bh(&ar->data_lock);
  1530. }
  1531. static int ath10k_abort_scan(struct ath10k *ar)
  1532. {
  1533. struct wmi_stop_scan_arg arg = {
  1534. .req_id = 1, /* FIXME */
  1535. .req_type = WMI_SCAN_STOP_ONE,
  1536. .u.scan_id = ATH10K_SCAN_ID,
  1537. };
  1538. int ret;
  1539. lockdep_assert_held(&ar->conf_mutex);
  1540. del_timer_sync(&ar->scan.timeout);
  1541. spin_lock_bh(&ar->data_lock);
  1542. if (!ar->scan.in_progress) {
  1543. spin_unlock_bh(&ar->data_lock);
  1544. return 0;
  1545. }
  1546. ar->scan.aborting = true;
  1547. spin_unlock_bh(&ar->data_lock);
  1548. ret = ath10k_wmi_stop_scan(ar, &arg);
  1549. if (ret) {
  1550. ath10k_warn("could not submit wmi stop scan (%d)\n", ret);
  1551. spin_lock_bh(&ar->data_lock);
  1552. ar->scan.in_progress = false;
  1553. ath10k_offchan_tx_purge(ar);
  1554. spin_unlock_bh(&ar->data_lock);
  1555. return -EIO;
  1556. }
  1557. ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
  1558. if (ret == 0)
  1559. ath10k_warn("timed out while waiting for scan to stop\n");
  1560. /* scan completion may be done right after we timeout here, so let's
  1561. * check the in_progress and tell mac80211 scan is completed. if we
  1562. * don't do that and FW fails to send us scan completion indication
  1563. * then userspace won't be able to scan anymore */
  1564. ret = 0;
  1565. spin_lock_bh(&ar->data_lock);
  1566. if (ar->scan.in_progress) {
  1567. ath10k_warn("could not stop scan. its still in progress\n");
  1568. ar->scan.in_progress = false;
  1569. ath10k_offchan_tx_purge(ar);
  1570. ret = -ETIMEDOUT;
  1571. }
  1572. spin_unlock_bh(&ar->data_lock);
  1573. return ret;
  1574. }
  1575. static int ath10k_start_scan(struct ath10k *ar,
  1576. const struct wmi_start_scan_arg *arg)
  1577. {
  1578. int ret;
  1579. lockdep_assert_held(&ar->conf_mutex);
  1580. ret = ath10k_wmi_start_scan(ar, arg);
  1581. if (ret)
  1582. return ret;
  1583. ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
  1584. if (ret == 0) {
  1585. ath10k_abort_scan(ar);
  1586. return ret;
  1587. }
  1588. /* the scan can complete earlier, before we even
  1589. * start the timer. in that case the timer handler
  1590. * checks ar->scan.in_progress and bails out if its
  1591. * false. Add a 200ms margin to account event/command
  1592. * processing. */
  1593. mod_timer(&ar->scan.timeout, jiffies +
  1594. msecs_to_jiffies(arg->max_scan_time+200));
  1595. return 0;
  1596. }
  1597. /**********************/
  1598. /* mac80211 callbacks */
  1599. /**********************/
  1600. static void ath10k_tx(struct ieee80211_hw *hw,
  1601. struct ieee80211_tx_control *control,
  1602. struct sk_buff *skb)
  1603. {
  1604. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1605. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1606. struct ath10k *ar = hw->priv;
  1607. u8 tid, vdev_id;
  1608. /* We should disable CCK RATE due to P2P */
  1609. if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
  1610. ath10k_dbg(ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
  1611. /* we must calculate tid before we apply qos workaround
  1612. * as we'd lose the qos control field */
  1613. tid = ath10k_tx_h_get_tid(hdr);
  1614. vdev_id = ath10k_tx_h_get_vdev_id(ar, info);
  1615. /* it makes no sense to process injected frames like that */
  1616. if (info->control.vif &&
  1617. info->control.vif->type != NL80211_IFTYPE_MONITOR) {
  1618. ath10k_tx_h_qos_workaround(hw, control, skb);
  1619. ath10k_tx_h_update_wep_key(skb);
  1620. ath10k_tx_h_add_p2p_noa_ie(ar, skb);
  1621. ath10k_tx_h_seq_no(skb);
  1622. }
  1623. ATH10K_SKB_CB(skb)->vdev_id = vdev_id;
  1624. ATH10K_SKB_CB(skb)->htt.is_offchan = false;
  1625. ATH10K_SKB_CB(skb)->htt.tid = tid;
  1626. if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
  1627. spin_lock_bh(&ar->data_lock);
  1628. ATH10K_SKB_CB(skb)->htt.is_offchan = true;
  1629. ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
  1630. spin_unlock_bh(&ar->data_lock);
  1631. ath10k_dbg(ATH10K_DBG_MAC, "queued offchannel skb %p\n", skb);
  1632. skb_queue_tail(&ar->offchan_tx_queue, skb);
  1633. ieee80211_queue_work(hw, &ar->offchan_tx_work);
  1634. return;
  1635. }
  1636. ath10k_tx_htt(ar, skb);
  1637. }
  1638. /*
  1639. * Initialize various parameters with default vaules.
  1640. */
  1641. void ath10k_halt(struct ath10k *ar)
  1642. {
  1643. lockdep_assert_held(&ar->conf_mutex);
  1644. ath10k_stop_cac(ar);
  1645. del_timer_sync(&ar->scan.timeout);
  1646. ath10k_offchan_tx_purge(ar);
  1647. ath10k_mgmt_over_wmi_tx_purge(ar);
  1648. ath10k_peer_cleanup_all(ar);
  1649. ath10k_core_stop(ar);
  1650. ath10k_hif_power_down(ar);
  1651. spin_lock_bh(&ar->data_lock);
  1652. if (ar->scan.in_progress) {
  1653. del_timer(&ar->scan.timeout);
  1654. ar->scan.in_progress = false;
  1655. ieee80211_scan_completed(ar->hw, true);
  1656. }
  1657. spin_unlock_bh(&ar->data_lock);
  1658. }
  1659. static int ath10k_start(struct ieee80211_hw *hw)
  1660. {
  1661. struct ath10k *ar = hw->priv;
  1662. int ret = 0;
  1663. mutex_lock(&ar->conf_mutex);
  1664. if (ar->state != ATH10K_STATE_OFF &&
  1665. ar->state != ATH10K_STATE_RESTARTING) {
  1666. ret = -EINVAL;
  1667. goto exit;
  1668. }
  1669. ret = ath10k_hif_power_up(ar);
  1670. if (ret) {
  1671. ath10k_err("could not init hif (%d)\n", ret);
  1672. ar->state = ATH10K_STATE_OFF;
  1673. goto exit;
  1674. }
  1675. ret = ath10k_core_start(ar);
  1676. if (ret) {
  1677. ath10k_err("could not init core (%d)\n", ret);
  1678. ath10k_hif_power_down(ar);
  1679. ar->state = ATH10K_STATE_OFF;
  1680. goto exit;
  1681. }
  1682. if (ar->state == ATH10K_STATE_OFF)
  1683. ar->state = ATH10K_STATE_ON;
  1684. else if (ar->state == ATH10K_STATE_RESTARTING)
  1685. ar->state = ATH10K_STATE_RESTARTED;
  1686. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
  1687. if (ret)
  1688. ath10k_warn("could not enable WMI_PDEV_PARAM_PMF_QOS (%d)\n",
  1689. ret);
  1690. ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
  1691. if (ret)
  1692. ath10k_warn("could not init WMI_PDEV_PARAM_DYNAMIC_BW (%d)\n",
  1693. ret);
  1694. ath10k_regd_update(ar);
  1695. exit:
  1696. mutex_unlock(&ar->conf_mutex);
  1697. return 0;
  1698. }
  1699. static void ath10k_stop(struct ieee80211_hw *hw)
  1700. {
  1701. struct ath10k *ar = hw->priv;
  1702. mutex_lock(&ar->conf_mutex);
  1703. if (ar->state == ATH10K_STATE_ON ||
  1704. ar->state == ATH10K_STATE_RESTARTED ||
  1705. ar->state == ATH10K_STATE_WEDGED)
  1706. ath10k_halt(ar);
  1707. ar->state = ATH10K_STATE_OFF;
  1708. mutex_unlock(&ar->conf_mutex);
  1709. ath10k_mgmt_over_wmi_tx_purge(ar);
  1710. cancel_work_sync(&ar->offchan_tx_work);
  1711. cancel_work_sync(&ar->wmi_mgmt_tx_work);
  1712. cancel_work_sync(&ar->restart_work);
  1713. }
  1714. static int ath10k_config_ps(struct ath10k *ar)
  1715. {
  1716. struct ath10k_vif *arvif;
  1717. int ret = 0;
  1718. lockdep_assert_held(&ar->conf_mutex);
  1719. list_for_each_entry(arvif, &ar->arvifs, list) {
  1720. ret = ath10k_mac_vif_setup_ps(arvif);
  1721. if (ret) {
  1722. ath10k_warn("could not setup powersave (%d)\n", ret);
  1723. break;
  1724. }
  1725. }
  1726. return ret;
  1727. }
  1728. static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
  1729. {
  1730. struct ath10k *ar = hw->priv;
  1731. struct ieee80211_conf *conf = &hw->conf;
  1732. int ret = 0;
  1733. u32 param;
  1734. mutex_lock(&ar->conf_mutex);
  1735. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1736. ath10k_dbg(ATH10K_DBG_MAC,
  1737. "mac config channel %d mhz flags 0x%x\n",
  1738. conf->chandef.chan->center_freq,
  1739. conf->chandef.chan->flags);
  1740. spin_lock_bh(&ar->data_lock);
  1741. ar->rx_channel = conf->chandef.chan;
  1742. spin_unlock_bh(&ar->data_lock);
  1743. ath10k_config_radar_detection(ar);
  1744. }
  1745. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1746. ath10k_dbg(ATH10K_DBG_MAC, "mac config power %d\n",
  1747. hw->conf.power_level);
  1748. param = ar->wmi.pdev_param->txpower_limit2g;
  1749. ret = ath10k_wmi_pdev_set_param(ar, param,
  1750. hw->conf.power_level * 2);
  1751. if (ret)
  1752. ath10k_warn("mac failed to set 2g txpower %d (%d)\n",
  1753. hw->conf.power_level, ret);
  1754. param = ar->wmi.pdev_param->txpower_limit5g;
  1755. ret = ath10k_wmi_pdev_set_param(ar, param,
  1756. hw->conf.power_level * 2);
  1757. if (ret)
  1758. ath10k_warn("mac failed to set 5g txpower %d (%d)\n",
  1759. hw->conf.power_level, ret);
  1760. }
  1761. if (changed & IEEE80211_CONF_CHANGE_PS)
  1762. ath10k_config_ps(ar);
  1763. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  1764. if (conf->flags & IEEE80211_CONF_MONITOR)
  1765. ret = ath10k_monitor_create(ar);
  1766. else
  1767. ret = ath10k_monitor_destroy(ar);
  1768. }
  1769. mutex_unlock(&ar->conf_mutex);
  1770. return ret;
  1771. }
  1772. /*
  1773. * TODO:
  1774. * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
  1775. * because we will send mgmt frames without CCK. This requirement
  1776. * for P2P_FIND/GO_NEG should be handled by checking CCK flag
  1777. * in the TX packet.
  1778. */
  1779. static int ath10k_add_interface(struct ieee80211_hw *hw,
  1780. struct ieee80211_vif *vif)
  1781. {
  1782. struct ath10k *ar = hw->priv;
  1783. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1784. enum wmi_sta_powersave_param param;
  1785. int ret = 0;
  1786. u32 value;
  1787. int bit;
  1788. u32 vdev_param;
  1789. mutex_lock(&ar->conf_mutex);
  1790. memset(arvif, 0, sizeof(*arvif));
  1791. arvif->ar = ar;
  1792. arvif->vif = vif;
  1793. INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
  1794. INIT_LIST_HEAD(&arvif->list);
  1795. if ((vif->type == NL80211_IFTYPE_MONITOR) && ar->monitor_present) {
  1796. ath10k_warn("Only one monitor interface allowed\n");
  1797. ret = -EBUSY;
  1798. goto err;
  1799. }
  1800. bit = ffs(ar->free_vdev_map);
  1801. if (bit == 0) {
  1802. ret = -EBUSY;
  1803. goto err;
  1804. }
  1805. arvif->vdev_id = bit - 1;
  1806. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
  1807. if (ar->p2p)
  1808. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
  1809. switch (vif->type) {
  1810. case NL80211_IFTYPE_UNSPECIFIED:
  1811. case NL80211_IFTYPE_STATION:
  1812. arvif->vdev_type = WMI_VDEV_TYPE_STA;
  1813. if (vif->p2p)
  1814. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
  1815. break;
  1816. case NL80211_IFTYPE_ADHOC:
  1817. arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
  1818. break;
  1819. case NL80211_IFTYPE_AP:
  1820. arvif->vdev_type = WMI_VDEV_TYPE_AP;
  1821. if (vif->p2p)
  1822. arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
  1823. break;
  1824. case NL80211_IFTYPE_MONITOR:
  1825. arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
  1826. break;
  1827. default:
  1828. WARN_ON(1);
  1829. break;
  1830. }
  1831. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d\n",
  1832. arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype);
  1833. ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
  1834. arvif->vdev_subtype, vif->addr);
  1835. if (ret) {
  1836. ath10k_warn("WMI vdev create failed: ret %d\n", ret);
  1837. goto err;
  1838. }
  1839. ar->free_vdev_map &= ~BIT(arvif->vdev_id);
  1840. list_add(&arvif->list, &ar->arvifs);
  1841. vdev_param = ar->wmi.vdev_param->def_keyid;
  1842. ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
  1843. arvif->def_wep_key_idx);
  1844. if (ret) {
  1845. ath10k_warn("Failed to set default keyid: %d\n", ret);
  1846. goto err_vdev_delete;
  1847. }
  1848. vdev_param = ar->wmi.vdev_param->tx_encap_type;
  1849. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  1850. ATH10K_HW_TXRX_NATIVE_WIFI);
  1851. /* 10.X firmware does not support this VDEV parameter. Do not warn */
  1852. if (ret && ret != -EOPNOTSUPP) {
  1853. ath10k_warn("Failed to set TX encap: %d\n", ret);
  1854. goto err_vdev_delete;
  1855. }
  1856. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  1857. ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
  1858. if (ret) {
  1859. ath10k_warn("Failed to create peer for AP: %d\n", ret);
  1860. goto err_vdev_delete;
  1861. }
  1862. }
  1863. if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
  1864. param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
  1865. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  1866. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  1867. param, value);
  1868. if (ret) {
  1869. ath10k_warn("Failed to set RX wake policy: %d\n", ret);
  1870. goto err_peer_delete;
  1871. }
  1872. param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
  1873. value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
  1874. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  1875. param, value);
  1876. if (ret) {
  1877. ath10k_warn("Failed to set TX wake thresh: %d\n", ret);
  1878. goto err_peer_delete;
  1879. }
  1880. param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
  1881. value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
  1882. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  1883. param, value);
  1884. if (ret) {
  1885. ath10k_warn("Failed to set PSPOLL count: %d\n", ret);
  1886. goto err_peer_delete;
  1887. }
  1888. }
  1889. ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
  1890. if (ret) {
  1891. ath10k_warn("failed to set rts threshold for vdev %d (%d)\n",
  1892. arvif->vdev_id, ret);
  1893. goto err_peer_delete;
  1894. }
  1895. ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
  1896. if (ret) {
  1897. ath10k_warn("failed to set frag threshold for vdev %d (%d)\n",
  1898. arvif->vdev_id, ret);
  1899. goto err_peer_delete;
  1900. }
  1901. if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
  1902. ar->monitor_present = true;
  1903. mutex_unlock(&ar->conf_mutex);
  1904. return 0;
  1905. err_peer_delete:
  1906. if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
  1907. ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
  1908. err_vdev_delete:
  1909. ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  1910. ar->free_vdev_map &= ~BIT(arvif->vdev_id);
  1911. list_del(&arvif->list);
  1912. err:
  1913. mutex_unlock(&ar->conf_mutex);
  1914. return ret;
  1915. }
  1916. static void ath10k_remove_interface(struct ieee80211_hw *hw,
  1917. struct ieee80211_vif *vif)
  1918. {
  1919. struct ath10k *ar = hw->priv;
  1920. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  1921. int ret;
  1922. mutex_lock(&ar->conf_mutex);
  1923. cancel_work_sync(&arvif->wep_key_work);
  1924. spin_lock_bh(&ar->data_lock);
  1925. if (arvif->beacon) {
  1926. dev_kfree_skb_any(arvif->beacon);
  1927. arvif->beacon = NULL;
  1928. }
  1929. spin_unlock_bh(&ar->data_lock);
  1930. ar->free_vdev_map |= 1 << (arvif->vdev_id);
  1931. list_del(&arvif->list);
  1932. if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
  1933. ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
  1934. if (ret)
  1935. ath10k_warn("Failed to remove peer for AP: %d\n", ret);
  1936. kfree(arvif->u.ap.noa_data);
  1937. }
  1938. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev delete %d (remove interface)\n",
  1939. arvif->vdev_id);
  1940. ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
  1941. if (ret)
  1942. ath10k_warn("WMI vdev delete failed: %d\n", ret);
  1943. if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
  1944. ar->monitor_present = false;
  1945. ath10k_peer_cleanup(ar, arvif->vdev_id);
  1946. mutex_unlock(&ar->conf_mutex);
  1947. }
  1948. /*
  1949. * FIXME: Has to be verified.
  1950. */
  1951. #define SUPPORTED_FILTERS \
  1952. (FIF_PROMISC_IN_BSS | \
  1953. FIF_ALLMULTI | \
  1954. FIF_CONTROL | \
  1955. FIF_PSPOLL | \
  1956. FIF_OTHER_BSS | \
  1957. FIF_BCN_PRBRESP_PROMISC | \
  1958. FIF_PROBE_REQ | \
  1959. FIF_FCSFAIL)
  1960. static void ath10k_configure_filter(struct ieee80211_hw *hw,
  1961. unsigned int changed_flags,
  1962. unsigned int *total_flags,
  1963. u64 multicast)
  1964. {
  1965. struct ath10k *ar = hw->priv;
  1966. int ret;
  1967. mutex_lock(&ar->conf_mutex);
  1968. changed_flags &= SUPPORTED_FILTERS;
  1969. *total_flags &= SUPPORTED_FILTERS;
  1970. ar->filter_flags = *total_flags;
  1971. /* Monitor must not be started if it wasn't created first.
  1972. * Promiscuous mode may be started on a non-monitor interface - in
  1973. * such case the monitor vdev is not created so starting the
  1974. * monitor makes no sense. Since ath10k uses no special RX filters
  1975. * (only BSS filter in STA mode) there's no need for any special
  1976. * action here. */
  1977. if ((ar->filter_flags & FIF_PROMISC_IN_BSS) &&
  1978. !ar->monitor_enabled && ar->monitor_present) {
  1979. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d start\n",
  1980. ar->monitor_vdev_id);
  1981. ret = ath10k_monitor_start(ar, ar->monitor_vdev_id);
  1982. if (ret)
  1983. ath10k_warn("Unable to start monitor mode\n");
  1984. } else if (!(ar->filter_flags & FIF_PROMISC_IN_BSS) &&
  1985. ar->monitor_enabled && ar->monitor_present) {
  1986. ath10k_dbg(ATH10K_DBG_MAC, "mac monitor %d stop\n",
  1987. ar->monitor_vdev_id);
  1988. ret = ath10k_monitor_stop(ar);
  1989. if (ret)
  1990. ath10k_warn("Unable to stop monitor mode\n");
  1991. }
  1992. mutex_unlock(&ar->conf_mutex);
  1993. }
  1994. static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
  1995. struct ieee80211_vif *vif,
  1996. struct ieee80211_bss_conf *info,
  1997. u32 changed)
  1998. {
  1999. struct ath10k *ar = hw->priv;
  2000. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2001. int ret = 0;
  2002. u32 vdev_param, pdev_param;
  2003. mutex_lock(&ar->conf_mutex);
  2004. if (changed & BSS_CHANGED_IBSS)
  2005. ath10k_control_ibss(arvif, info, vif->addr);
  2006. if (changed & BSS_CHANGED_BEACON_INT) {
  2007. arvif->beacon_interval = info->beacon_int;
  2008. vdev_param = ar->wmi.vdev_param->beacon_interval;
  2009. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2010. arvif->beacon_interval);
  2011. ath10k_dbg(ATH10K_DBG_MAC,
  2012. "mac vdev %d beacon_interval %d\n",
  2013. arvif->vdev_id, arvif->beacon_interval);
  2014. if (ret)
  2015. ath10k_warn("Failed to set beacon interval for VDEV: %d\n",
  2016. arvif->vdev_id);
  2017. }
  2018. if (changed & BSS_CHANGED_BEACON) {
  2019. ath10k_dbg(ATH10K_DBG_MAC,
  2020. "vdev %d set beacon tx mode to staggered\n",
  2021. arvif->vdev_id);
  2022. pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
  2023. ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
  2024. WMI_BEACON_STAGGERED_MODE);
  2025. if (ret)
  2026. ath10k_warn("Failed to set beacon mode for VDEV: %d\n",
  2027. arvif->vdev_id);
  2028. }
  2029. if (changed & BSS_CHANGED_BEACON_INFO) {
  2030. arvif->dtim_period = info->dtim_period;
  2031. ath10k_dbg(ATH10K_DBG_MAC,
  2032. "mac vdev %d dtim_period %d\n",
  2033. arvif->vdev_id, arvif->dtim_period);
  2034. vdev_param = ar->wmi.vdev_param->dtim_period;
  2035. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2036. arvif->dtim_period);
  2037. if (ret)
  2038. ath10k_warn("Failed to set dtim period for VDEV: %d\n",
  2039. arvif->vdev_id);
  2040. }
  2041. if (changed & BSS_CHANGED_SSID &&
  2042. vif->type == NL80211_IFTYPE_AP) {
  2043. arvif->u.ap.ssid_len = info->ssid_len;
  2044. if (info->ssid_len)
  2045. memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
  2046. arvif->u.ap.hidden_ssid = info->hidden_ssid;
  2047. }
  2048. if (changed & BSS_CHANGED_BSSID) {
  2049. if (!is_zero_ether_addr(info->bssid)) {
  2050. ath10k_dbg(ATH10K_DBG_MAC,
  2051. "mac vdev %d create peer %pM\n",
  2052. arvif->vdev_id, info->bssid);
  2053. ret = ath10k_peer_create(ar, arvif->vdev_id,
  2054. info->bssid);
  2055. if (ret)
  2056. ath10k_warn("Failed to add peer %pM for vdev %d when changin bssid: %i\n",
  2057. info->bssid, arvif->vdev_id, ret);
  2058. if (vif->type == NL80211_IFTYPE_STATION) {
  2059. /*
  2060. * this is never erased as we it for crypto key
  2061. * clearing; this is FW requirement
  2062. */
  2063. memcpy(arvif->u.sta.bssid, info->bssid,
  2064. ETH_ALEN);
  2065. ath10k_dbg(ATH10K_DBG_MAC,
  2066. "mac vdev %d start %pM\n",
  2067. arvif->vdev_id, info->bssid);
  2068. /* FIXME: check return value */
  2069. ret = ath10k_vdev_start(arvif);
  2070. }
  2071. /*
  2072. * Mac80211 does not keep IBSS bssid when leaving IBSS,
  2073. * so driver need to store it. It is needed when leaving
  2074. * IBSS in order to remove BSSID peer.
  2075. */
  2076. if (vif->type == NL80211_IFTYPE_ADHOC)
  2077. memcpy(arvif->u.ibss.bssid, info->bssid,
  2078. ETH_ALEN);
  2079. }
  2080. }
  2081. if (changed & BSS_CHANGED_BEACON_ENABLED)
  2082. ath10k_control_beaconing(arvif, info);
  2083. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2084. u32 cts_prot;
  2085. if (info->use_cts_prot)
  2086. cts_prot = 1;
  2087. else
  2088. cts_prot = 0;
  2089. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
  2090. arvif->vdev_id, cts_prot);
  2091. vdev_param = ar->wmi.vdev_param->enable_rtscts;
  2092. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2093. cts_prot);
  2094. if (ret)
  2095. ath10k_warn("Failed to set CTS prot for VDEV: %d\n",
  2096. arvif->vdev_id);
  2097. }
  2098. if (changed & BSS_CHANGED_ERP_SLOT) {
  2099. u32 slottime;
  2100. if (info->use_short_slot)
  2101. slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
  2102. else
  2103. slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
  2104. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
  2105. arvif->vdev_id, slottime);
  2106. vdev_param = ar->wmi.vdev_param->slot_time;
  2107. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2108. slottime);
  2109. if (ret)
  2110. ath10k_warn("Failed to set erp slot for VDEV: %d\n",
  2111. arvif->vdev_id);
  2112. }
  2113. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2114. u32 preamble;
  2115. if (info->use_short_preamble)
  2116. preamble = WMI_VDEV_PREAMBLE_SHORT;
  2117. else
  2118. preamble = WMI_VDEV_PREAMBLE_LONG;
  2119. ath10k_dbg(ATH10K_DBG_MAC,
  2120. "mac vdev %d preamble %dn",
  2121. arvif->vdev_id, preamble);
  2122. vdev_param = ar->wmi.vdev_param->preamble;
  2123. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2124. preamble);
  2125. if (ret)
  2126. ath10k_warn("Failed to set preamble for VDEV: %d\n",
  2127. arvif->vdev_id);
  2128. }
  2129. if (changed & BSS_CHANGED_ASSOC) {
  2130. if (info->assoc)
  2131. ath10k_bss_assoc(hw, vif, info);
  2132. }
  2133. mutex_unlock(&ar->conf_mutex);
  2134. }
  2135. static int ath10k_hw_scan(struct ieee80211_hw *hw,
  2136. struct ieee80211_vif *vif,
  2137. struct cfg80211_scan_request *req)
  2138. {
  2139. struct ath10k *ar = hw->priv;
  2140. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2141. struct wmi_start_scan_arg arg;
  2142. int ret = 0;
  2143. int i;
  2144. mutex_lock(&ar->conf_mutex);
  2145. spin_lock_bh(&ar->data_lock);
  2146. if (ar->scan.in_progress) {
  2147. spin_unlock_bh(&ar->data_lock);
  2148. ret = -EBUSY;
  2149. goto exit;
  2150. }
  2151. reinit_completion(&ar->scan.started);
  2152. reinit_completion(&ar->scan.completed);
  2153. ar->scan.in_progress = true;
  2154. ar->scan.aborting = false;
  2155. ar->scan.is_roc = false;
  2156. ar->scan.vdev_id = arvif->vdev_id;
  2157. spin_unlock_bh(&ar->data_lock);
  2158. memset(&arg, 0, sizeof(arg));
  2159. ath10k_wmi_start_scan_init(ar, &arg);
  2160. arg.vdev_id = arvif->vdev_id;
  2161. arg.scan_id = ATH10K_SCAN_ID;
  2162. if (!req->no_cck)
  2163. arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
  2164. if (req->ie_len) {
  2165. arg.ie_len = req->ie_len;
  2166. memcpy(arg.ie, req->ie, arg.ie_len);
  2167. }
  2168. if (req->n_ssids) {
  2169. arg.n_ssids = req->n_ssids;
  2170. for (i = 0; i < arg.n_ssids; i++) {
  2171. arg.ssids[i].len = req->ssids[i].ssid_len;
  2172. arg.ssids[i].ssid = req->ssids[i].ssid;
  2173. }
  2174. } else {
  2175. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  2176. }
  2177. if (req->n_channels) {
  2178. arg.n_channels = req->n_channels;
  2179. for (i = 0; i < arg.n_channels; i++)
  2180. arg.channels[i] = req->channels[i]->center_freq;
  2181. }
  2182. ret = ath10k_start_scan(ar, &arg);
  2183. if (ret) {
  2184. ath10k_warn("could not start hw scan (%d)\n", ret);
  2185. spin_lock_bh(&ar->data_lock);
  2186. ar->scan.in_progress = false;
  2187. spin_unlock_bh(&ar->data_lock);
  2188. }
  2189. exit:
  2190. mutex_unlock(&ar->conf_mutex);
  2191. return ret;
  2192. }
  2193. static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
  2194. struct ieee80211_vif *vif)
  2195. {
  2196. struct ath10k *ar = hw->priv;
  2197. int ret;
  2198. mutex_lock(&ar->conf_mutex);
  2199. ret = ath10k_abort_scan(ar);
  2200. if (ret) {
  2201. ath10k_warn("couldn't abort scan (%d). forcefully sending scan completion to mac80211\n",
  2202. ret);
  2203. ieee80211_scan_completed(hw, 1 /* aborted */);
  2204. }
  2205. mutex_unlock(&ar->conf_mutex);
  2206. }
  2207. static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
  2208. struct ath10k_vif *arvif,
  2209. enum set_key_cmd cmd,
  2210. struct ieee80211_key_conf *key)
  2211. {
  2212. u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
  2213. int ret;
  2214. /* 10.1 firmware branch requires default key index to be set to group
  2215. * key index after installing it. Otherwise FW/HW Txes corrupted
  2216. * frames with multi-vif APs. This is not required for main firmware
  2217. * branch (e.g. 636).
  2218. *
  2219. * FIXME: This has been tested only in AP. It remains unknown if this
  2220. * is required for multi-vif STA interfaces on 10.1 */
  2221. if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
  2222. return;
  2223. if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
  2224. return;
  2225. if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
  2226. return;
  2227. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  2228. return;
  2229. if (cmd != SET_KEY)
  2230. return;
  2231. ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
  2232. key->keyidx);
  2233. if (ret)
  2234. ath10k_warn("failed to set group key as default key: %d\n",
  2235. ret);
  2236. }
  2237. static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2238. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2239. struct ieee80211_key_conf *key)
  2240. {
  2241. struct ath10k *ar = hw->priv;
  2242. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2243. struct ath10k_peer *peer;
  2244. const u8 *peer_addr;
  2245. bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2246. key->cipher == WLAN_CIPHER_SUITE_WEP104;
  2247. int ret = 0;
  2248. if (key->keyidx > WMI_MAX_KEY_INDEX)
  2249. return -ENOSPC;
  2250. mutex_lock(&ar->conf_mutex);
  2251. if (sta)
  2252. peer_addr = sta->addr;
  2253. else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
  2254. peer_addr = vif->bss_conf.bssid;
  2255. else
  2256. peer_addr = vif->addr;
  2257. key->hw_key_idx = key->keyidx;
  2258. /* the peer should not disappear in mid-way (unless FW goes awry) since
  2259. * we already hold conf_mutex. we just make sure its there now. */
  2260. spin_lock_bh(&ar->data_lock);
  2261. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  2262. spin_unlock_bh(&ar->data_lock);
  2263. if (!peer) {
  2264. if (cmd == SET_KEY) {
  2265. ath10k_warn("cannot install key for non-existent peer %pM\n",
  2266. peer_addr);
  2267. ret = -EOPNOTSUPP;
  2268. goto exit;
  2269. } else {
  2270. /* if the peer doesn't exist there is no key to disable
  2271. * anymore */
  2272. goto exit;
  2273. }
  2274. }
  2275. if (is_wep) {
  2276. if (cmd == SET_KEY)
  2277. arvif->wep_keys[key->keyidx] = key;
  2278. else
  2279. arvif->wep_keys[key->keyidx] = NULL;
  2280. if (cmd == DISABLE_KEY)
  2281. ath10k_clear_vdev_key(arvif, key);
  2282. }
  2283. ret = ath10k_install_key(arvif, key, cmd, peer_addr);
  2284. if (ret) {
  2285. ath10k_warn("ath10k_install_key failed (%d)\n", ret);
  2286. goto exit;
  2287. }
  2288. ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
  2289. spin_lock_bh(&ar->data_lock);
  2290. peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
  2291. if (peer && cmd == SET_KEY)
  2292. peer->keys[key->keyidx] = key;
  2293. else if (peer && cmd == DISABLE_KEY)
  2294. peer->keys[key->keyidx] = NULL;
  2295. else if (peer == NULL)
  2296. /* impossible unless FW goes crazy */
  2297. ath10k_warn("peer %pM disappeared!\n", peer_addr);
  2298. spin_unlock_bh(&ar->data_lock);
  2299. exit:
  2300. mutex_unlock(&ar->conf_mutex);
  2301. return ret;
  2302. }
  2303. static int ath10k_sta_state(struct ieee80211_hw *hw,
  2304. struct ieee80211_vif *vif,
  2305. struct ieee80211_sta *sta,
  2306. enum ieee80211_sta_state old_state,
  2307. enum ieee80211_sta_state new_state)
  2308. {
  2309. struct ath10k *ar = hw->priv;
  2310. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2311. int ret = 0;
  2312. mutex_lock(&ar->conf_mutex);
  2313. if (old_state == IEEE80211_STA_NOTEXIST &&
  2314. new_state == IEEE80211_STA_NONE &&
  2315. vif->type != NL80211_IFTYPE_STATION) {
  2316. /*
  2317. * New station addition.
  2318. */
  2319. ath10k_dbg(ATH10K_DBG_MAC,
  2320. "mac vdev %d peer create %pM (new sta)\n",
  2321. arvif->vdev_id, sta->addr);
  2322. ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
  2323. if (ret)
  2324. ath10k_warn("Failed to add peer %pM for vdev %d when adding a new sta: %i\n",
  2325. sta->addr, arvif->vdev_id, ret);
  2326. } else if ((old_state == IEEE80211_STA_NONE &&
  2327. new_state == IEEE80211_STA_NOTEXIST)) {
  2328. /*
  2329. * Existing station deletion.
  2330. */
  2331. ath10k_dbg(ATH10K_DBG_MAC,
  2332. "mac vdev %d peer delete %pM (sta gone)\n",
  2333. arvif->vdev_id, sta->addr);
  2334. ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
  2335. if (ret)
  2336. ath10k_warn("Failed to delete peer: %pM for VDEV: %d\n",
  2337. sta->addr, arvif->vdev_id);
  2338. if (vif->type == NL80211_IFTYPE_STATION)
  2339. ath10k_bss_disassoc(hw, vif);
  2340. } else if (old_state == IEEE80211_STA_AUTH &&
  2341. new_state == IEEE80211_STA_ASSOC &&
  2342. (vif->type == NL80211_IFTYPE_AP ||
  2343. vif->type == NL80211_IFTYPE_ADHOC)) {
  2344. /*
  2345. * New association.
  2346. */
  2347. ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM associated\n",
  2348. sta->addr);
  2349. ret = ath10k_station_assoc(ar, arvif, sta);
  2350. if (ret)
  2351. ath10k_warn("Failed to associate station: %pM\n",
  2352. sta->addr);
  2353. } else if (old_state == IEEE80211_STA_ASSOC &&
  2354. new_state == IEEE80211_STA_AUTH &&
  2355. (vif->type == NL80211_IFTYPE_AP ||
  2356. vif->type == NL80211_IFTYPE_ADHOC)) {
  2357. /*
  2358. * Disassociation.
  2359. */
  2360. ath10k_dbg(ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
  2361. sta->addr);
  2362. ret = ath10k_station_disassoc(ar, arvif, sta);
  2363. if (ret)
  2364. ath10k_warn("Failed to disassociate station: %pM\n",
  2365. sta->addr);
  2366. }
  2367. mutex_unlock(&ar->conf_mutex);
  2368. return ret;
  2369. }
  2370. static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
  2371. u16 ac, bool enable)
  2372. {
  2373. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2374. u32 value = 0;
  2375. int ret = 0;
  2376. lockdep_assert_held(&ar->conf_mutex);
  2377. if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
  2378. return 0;
  2379. switch (ac) {
  2380. case IEEE80211_AC_VO:
  2381. value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
  2382. WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
  2383. break;
  2384. case IEEE80211_AC_VI:
  2385. value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
  2386. WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
  2387. break;
  2388. case IEEE80211_AC_BE:
  2389. value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
  2390. WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
  2391. break;
  2392. case IEEE80211_AC_BK:
  2393. value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
  2394. WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
  2395. break;
  2396. }
  2397. if (enable)
  2398. arvif->u.sta.uapsd |= value;
  2399. else
  2400. arvif->u.sta.uapsd &= ~value;
  2401. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2402. WMI_STA_PS_PARAM_UAPSD,
  2403. arvif->u.sta.uapsd);
  2404. if (ret) {
  2405. ath10k_warn("could not set uapsd params %d\n", ret);
  2406. goto exit;
  2407. }
  2408. if (arvif->u.sta.uapsd)
  2409. value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
  2410. else
  2411. value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
  2412. ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
  2413. WMI_STA_PS_PARAM_RX_WAKE_POLICY,
  2414. value);
  2415. if (ret)
  2416. ath10k_warn("could not set rx wake param %d\n", ret);
  2417. exit:
  2418. return ret;
  2419. }
  2420. static int ath10k_conf_tx(struct ieee80211_hw *hw,
  2421. struct ieee80211_vif *vif, u16 ac,
  2422. const struct ieee80211_tx_queue_params *params)
  2423. {
  2424. struct ath10k *ar = hw->priv;
  2425. struct wmi_wmm_params_arg *p = NULL;
  2426. int ret;
  2427. mutex_lock(&ar->conf_mutex);
  2428. switch (ac) {
  2429. case IEEE80211_AC_VO:
  2430. p = &ar->wmm_params.ac_vo;
  2431. break;
  2432. case IEEE80211_AC_VI:
  2433. p = &ar->wmm_params.ac_vi;
  2434. break;
  2435. case IEEE80211_AC_BE:
  2436. p = &ar->wmm_params.ac_be;
  2437. break;
  2438. case IEEE80211_AC_BK:
  2439. p = &ar->wmm_params.ac_bk;
  2440. break;
  2441. }
  2442. if (WARN_ON(!p)) {
  2443. ret = -EINVAL;
  2444. goto exit;
  2445. }
  2446. p->cwmin = params->cw_min;
  2447. p->cwmax = params->cw_max;
  2448. p->aifs = params->aifs;
  2449. /*
  2450. * The channel time duration programmed in the HW is in absolute
  2451. * microseconds, while mac80211 gives the txop in units of
  2452. * 32 microseconds.
  2453. */
  2454. p->txop = params->txop * 32;
  2455. /* FIXME: FW accepts wmm params per hw, not per vif */
  2456. ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
  2457. if (ret) {
  2458. ath10k_warn("could not set wmm params %d\n", ret);
  2459. goto exit;
  2460. }
  2461. ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
  2462. if (ret)
  2463. ath10k_warn("could not set sta uapsd %d\n", ret);
  2464. exit:
  2465. mutex_unlock(&ar->conf_mutex);
  2466. return ret;
  2467. }
  2468. #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
  2469. static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
  2470. struct ieee80211_vif *vif,
  2471. struct ieee80211_channel *chan,
  2472. int duration,
  2473. enum ieee80211_roc_type type)
  2474. {
  2475. struct ath10k *ar = hw->priv;
  2476. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2477. struct wmi_start_scan_arg arg;
  2478. int ret;
  2479. mutex_lock(&ar->conf_mutex);
  2480. spin_lock_bh(&ar->data_lock);
  2481. if (ar->scan.in_progress) {
  2482. spin_unlock_bh(&ar->data_lock);
  2483. ret = -EBUSY;
  2484. goto exit;
  2485. }
  2486. reinit_completion(&ar->scan.started);
  2487. reinit_completion(&ar->scan.completed);
  2488. reinit_completion(&ar->scan.on_channel);
  2489. ar->scan.in_progress = true;
  2490. ar->scan.aborting = false;
  2491. ar->scan.is_roc = true;
  2492. ar->scan.vdev_id = arvif->vdev_id;
  2493. ar->scan.roc_freq = chan->center_freq;
  2494. spin_unlock_bh(&ar->data_lock);
  2495. memset(&arg, 0, sizeof(arg));
  2496. ath10k_wmi_start_scan_init(ar, &arg);
  2497. arg.vdev_id = arvif->vdev_id;
  2498. arg.scan_id = ATH10K_SCAN_ID;
  2499. arg.n_channels = 1;
  2500. arg.channels[0] = chan->center_freq;
  2501. arg.dwell_time_active = duration;
  2502. arg.dwell_time_passive = duration;
  2503. arg.max_scan_time = 2 * duration;
  2504. arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
  2505. arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
  2506. ret = ath10k_start_scan(ar, &arg);
  2507. if (ret) {
  2508. ath10k_warn("could not start roc scan (%d)\n", ret);
  2509. spin_lock_bh(&ar->data_lock);
  2510. ar->scan.in_progress = false;
  2511. spin_unlock_bh(&ar->data_lock);
  2512. goto exit;
  2513. }
  2514. ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
  2515. if (ret == 0) {
  2516. ath10k_warn("could not switch to channel for roc scan\n");
  2517. ath10k_abort_scan(ar);
  2518. ret = -ETIMEDOUT;
  2519. goto exit;
  2520. }
  2521. ret = 0;
  2522. exit:
  2523. mutex_unlock(&ar->conf_mutex);
  2524. return ret;
  2525. }
  2526. static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
  2527. {
  2528. struct ath10k *ar = hw->priv;
  2529. mutex_lock(&ar->conf_mutex);
  2530. ath10k_abort_scan(ar);
  2531. mutex_unlock(&ar->conf_mutex);
  2532. return 0;
  2533. }
  2534. /*
  2535. * Both RTS and Fragmentation threshold are interface-specific
  2536. * in ath10k, but device-specific in mac80211.
  2537. */
  2538. static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
  2539. {
  2540. struct ath10k *ar = hw->priv;
  2541. struct ath10k_vif *arvif;
  2542. int ret = 0;
  2543. mutex_lock(&ar->conf_mutex);
  2544. list_for_each_entry(arvif, &ar->arvifs, list) {
  2545. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
  2546. arvif->vdev_id, value);
  2547. ret = ath10k_mac_set_rts(arvif, value);
  2548. if (ret) {
  2549. ath10k_warn("could not set rts threshold for vdev %d (%d)\n",
  2550. arvif->vdev_id, ret);
  2551. break;
  2552. }
  2553. }
  2554. mutex_unlock(&ar->conf_mutex);
  2555. return ret;
  2556. }
  2557. static int ath10k_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
  2558. {
  2559. struct ath10k *ar = hw->priv;
  2560. struct ath10k_vif *arvif;
  2561. int ret = 0;
  2562. mutex_lock(&ar->conf_mutex);
  2563. list_for_each_entry(arvif, &ar->arvifs, list) {
  2564. ath10k_dbg(ATH10K_DBG_MAC, "mac vdev %d fragmentation threshold %d\n",
  2565. arvif->vdev_id, value);
  2566. ret = ath10k_mac_set_rts(arvif, value);
  2567. if (ret) {
  2568. ath10k_warn("could not set fragmentation threshold for vdev %d (%d)\n",
  2569. arvif->vdev_id, ret);
  2570. break;
  2571. }
  2572. }
  2573. mutex_unlock(&ar->conf_mutex);
  2574. return ret;
  2575. }
  2576. static void ath10k_flush(struct ieee80211_hw *hw, u32 queues, bool drop)
  2577. {
  2578. struct ath10k *ar = hw->priv;
  2579. bool skip;
  2580. int ret;
  2581. /* mac80211 doesn't care if we really xmit queued frames or not
  2582. * we'll collect those frames either way if we stop/delete vdevs */
  2583. if (drop)
  2584. return;
  2585. mutex_lock(&ar->conf_mutex);
  2586. if (ar->state == ATH10K_STATE_WEDGED)
  2587. goto skip;
  2588. ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
  2589. bool empty;
  2590. spin_lock_bh(&ar->htt.tx_lock);
  2591. empty = (ar->htt.num_pending_tx == 0);
  2592. spin_unlock_bh(&ar->htt.tx_lock);
  2593. skip = (ar->state == ATH10K_STATE_WEDGED);
  2594. (empty || skip);
  2595. }), ATH10K_FLUSH_TIMEOUT_HZ);
  2596. if (ret <= 0 || skip)
  2597. ath10k_warn("tx not flushed\n");
  2598. skip:
  2599. mutex_unlock(&ar->conf_mutex);
  2600. }
  2601. /* TODO: Implement this function properly
  2602. * For now it is needed to reply to Probe Requests in IBSS mode.
  2603. * Propably we need this information from FW.
  2604. */
  2605. static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
  2606. {
  2607. return 1;
  2608. }
  2609. #ifdef CONFIG_PM
  2610. static int ath10k_suspend(struct ieee80211_hw *hw,
  2611. struct cfg80211_wowlan *wowlan)
  2612. {
  2613. struct ath10k *ar = hw->priv;
  2614. int ret;
  2615. ar->is_target_paused = false;
  2616. ret = ath10k_wmi_pdev_suspend_target(ar);
  2617. if (ret) {
  2618. ath10k_warn("could not suspend target (%d)\n", ret);
  2619. return 1;
  2620. }
  2621. ret = wait_event_interruptible_timeout(ar->event_queue,
  2622. ar->is_target_paused == true,
  2623. 1 * HZ);
  2624. if (ret < 0) {
  2625. ath10k_warn("suspend interrupted (%d)\n", ret);
  2626. goto resume;
  2627. } else if (ret == 0) {
  2628. ath10k_warn("suspend timed out - target pause event never came\n");
  2629. goto resume;
  2630. }
  2631. ret = ath10k_hif_suspend(ar);
  2632. if (ret) {
  2633. ath10k_warn("could not suspend hif (%d)\n", ret);
  2634. goto resume;
  2635. }
  2636. return 0;
  2637. resume:
  2638. ret = ath10k_wmi_pdev_resume_target(ar);
  2639. if (ret)
  2640. ath10k_warn("could not resume target (%d)\n", ret);
  2641. return 1;
  2642. }
  2643. static int ath10k_resume(struct ieee80211_hw *hw)
  2644. {
  2645. struct ath10k *ar = hw->priv;
  2646. int ret;
  2647. ret = ath10k_hif_resume(ar);
  2648. if (ret) {
  2649. ath10k_warn("could not resume hif (%d)\n", ret);
  2650. return 1;
  2651. }
  2652. ret = ath10k_wmi_pdev_resume_target(ar);
  2653. if (ret) {
  2654. ath10k_warn("could not resume target (%d)\n", ret);
  2655. return 1;
  2656. }
  2657. return 0;
  2658. }
  2659. #endif
  2660. static void ath10k_restart_complete(struct ieee80211_hw *hw)
  2661. {
  2662. struct ath10k *ar = hw->priv;
  2663. mutex_lock(&ar->conf_mutex);
  2664. /* If device failed to restart it will be in a different state, e.g.
  2665. * ATH10K_STATE_WEDGED */
  2666. if (ar->state == ATH10K_STATE_RESTARTED) {
  2667. ath10k_info("device successfully recovered\n");
  2668. ar->state = ATH10K_STATE_ON;
  2669. }
  2670. mutex_unlock(&ar->conf_mutex);
  2671. }
  2672. static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
  2673. struct survey_info *survey)
  2674. {
  2675. struct ath10k *ar = hw->priv;
  2676. struct ieee80211_supported_band *sband;
  2677. struct survey_info *ar_survey = &ar->survey[idx];
  2678. int ret = 0;
  2679. mutex_lock(&ar->conf_mutex);
  2680. sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  2681. if (sband && idx >= sband->n_channels) {
  2682. idx -= sband->n_channels;
  2683. sband = NULL;
  2684. }
  2685. if (!sband)
  2686. sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
  2687. if (!sband || idx >= sband->n_channels) {
  2688. ret = -ENOENT;
  2689. goto exit;
  2690. }
  2691. spin_lock_bh(&ar->data_lock);
  2692. memcpy(survey, ar_survey, sizeof(*survey));
  2693. spin_unlock_bh(&ar->data_lock);
  2694. survey->channel = &sband->channels[idx];
  2695. exit:
  2696. mutex_unlock(&ar->conf_mutex);
  2697. return ret;
  2698. }
  2699. static const struct ieee80211_ops ath10k_ops = {
  2700. .tx = ath10k_tx,
  2701. .start = ath10k_start,
  2702. .stop = ath10k_stop,
  2703. .config = ath10k_config,
  2704. .add_interface = ath10k_add_interface,
  2705. .remove_interface = ath10k_remove_interface,
  2706. .configure_filter = ath10k_configure_filter,
  2707. .bss_info_changed = ath10k_bss_info_changed,
  2708. .hw_scan = ath10k_hw_scan,
  2709. .cancel_hw_scan = ath10k_cancel_hw_scan,
  2710. .set_key = ath10k_set_key,
  2711. .sta_state = ath10k_sta_state,
  2712. .conf_tx = ath10k_conf_tx,
  2713. .remain_on_channel = ath10k_remain_on_channel,
  2714. .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
  2715. .set_rts_threshold = ath10k_set_rts_threshold,
  2716. .set_frag_threshold = ath10k_set_frag_threshold,
  2717. .flush = ath10k_flush,
  2718. .tx_last_beacon = ath10k_tx_last_beacon,
  2719. .restart_complete = ath10k_restart_complete,
  2720. .get_survey = ath10k_get_survey,
  2721. #ifdef CONFIG_PM
  2722. .suspend = ath10k_suspend,
  2723. .resume = ath10k_resume,
  2724. #endif
  2725. };
  2726. #define RATETAB_ENT(_rate, _rateid, _flags) { \
  2727. .bitrate = (_rate), \
  2728. .flags = (_flags), \
  2729. .hw_value = (_rateid), \
  2730. }
  2731. #define CHAN2G(_channel, _freq, _flags) { \
  2732. .band = IEEE80211_BAND_2GHZ, \
  2733. .hw_value = (_channel), \
  2734. .center_freq = (_freq), \
  2735. .flags = (_flags), \
  2736. .max_antenna_gain = 0, \
  2737. .max_power = 30, \
  2738. }
  2739. #define CHAN5G(_channel, _freq, _flags) { \
  2740. .band = IEEE80211_BAND_5GHZ, \
  2741. .hw_value = (_channel), \
  2742. .center_freq = (_freq), \
  2743. .flags = (_flags), \
  2744. .max_antenna_gain = 0, \
  2745. .max_power = 30, \
  2746. }
  2747. static const struct ieee80211_channel ath10k_2ghz_channels[] = {
  2748. CHAN2G(1, 2412, 0),
  2749. CHAN2G(2, 2417, 0),
  2750. CHAN2G(3, 2422, 0),
  2751. CHAN2G(4, 2427, 0),
  2752. CHAN2G(5, 2432, 0),
  2753. CHAN2G(6, 2437, 0),
  2754. CHAN2G(7, 2442, 0),
  2755. CHAN2G(8, 2447, 0),
  2756. CHAN2G(9, 2452, 0),
  2757. CHAN2G(10, 2457, 0),
  2758. CHAN2G(11, 2462, 0),
  2759. CHAN2G(12, 2467, 0),
  2760. CHAN2G(13, 2472, 0),
  2761. CHAN2G(14, 2484, 0),
  2762. };
  2763. static const struct ieee80211_channel ath10k_5ghz_channels[] = {
  2764. CHAN5G(36, 5180, 0),
  2765. CHAN5G(40, 5200, 0),
  2766. CHAN5G(44, 5220, 0),
  2767. CHAN5G(48, 5240, 0),
  2768. CHAN5G(52, 5260, 0),
  2769. CHAN5G(56, 5280, 0),
  2770. CHAN5G(60, 5300, 0),
  2771. CHAN5G(64, 5320, 0),
  2772. CHAN5G(100, 5500, 0),
  2773. CHAN5G(104, 5520, 0),
  2774. CHAN5G(108, 5540, 0),
  2775. CHAN5G(112, 5560, 0),
  2776. CHAN5G(116, 5580, 0),
  2777. CHAN5G(120, 5600, 0),
  2778. CHAN5G(124, 5620, 0),
  2779. CHAN5G(128, 5640, 0),
  2780. CHAN5G(132, 5660, 0),
  2781. CHAN5G(136, 5680, 0),
  2782. CHAN5G(140, 5700, 0),
  2783. CHAN5G(149, 5745, 0),
  2784. CHAN5G(153, 5765, 0),
  2785. CHAN5G(157, 5785, 0),
  2786. CHAN5G(161, 5805, 0),
  2787. CHAN5G(165, 5825, 0),
  2788. };
  2789. static struct ieee80211_rate ath10k_rates[] = {
  2790. /* CCK */
  2791. RATETAB_ENT(10, 0x82, 0),
  2792. RATETAB_ENT(20, 0x84, 0),
  2793. RATETAB_ENT(55, 0x8b, 0),
  2794. RATETAB_ENT(110, 0x96, 0),
  2795. /* OFDM */
  2796. RATETAB_ENT(60, 0x0c, 0),
  2797. RATETAB_ENT(90, 0x12, 0),
  2798. RATETAB_ENT(120, 0x18, 0),
  2799. RATETAB_ENT(180, 0x24, 0),
  2800. RATETAB_ENT(240, 0x30, 0),
  2801. RATETAB_ENT(360, 0x48, 0),
  2802. RATETAB_ENT(480, 0x60, 0),
  2803. RATETAB_ENT(540, 0x6c, 0),
  2804. };
  2805. #define ath10k_a_rates (ath10k_rates + 4)
  2806. #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
  2807. #define ath10k_g_rates (ath10k_rates + 0)
  2808. #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
  2809. struct ath10k *ath10k_mac_create(void)
  2810. {
  2811. struct ieee80211_hw *hw;
  2812. struct ath10k *ar;
  2813. hw = ieee80211_alloc_hw(sizeof(struct ath10k), &ath10k_ops);
  2814. if (!hw)
  2815. return NULL;
  2816. ar = hw->priv;
  2817. ar->hw = hw;
  2818. return ar;
  2819. }
  2820. void ath10k_mac_destroy(struct ath10k *ar)
  2821. {
  2822. ieee80211_free_hw(ar->hw);
  2823. }
  2824. static const struct ieee80211_iface_limit ath10k_if_limits[] = {
  2825. {
  2826. .max = 8,
  2827. .types = BIT(NL80211_IFTYPE_STATION)
  2828. | BIT(NL80211_IFTYPE_P2P_CLIENT)
  2829. },
  2830. {
  2831. .max = 3,
  2832. .types = BIT(NL80211_IFTYPE_P2P_GO)
  2833. },
  2834. {
  2835. .max = 7,
  2836. .types = BIT(NL80211_IFTYPE_AP)
  2837. },
  2838. };
  2839. #ifdef CONFIG_ATH10K_DFS_CERTIFIED
  2840. static const struct ieee80211_iface_limit ath10k_if_dfs_limits[] = {
  2841. {
  2842. .max = 8,
  2843. .types = BIT(NL80211_IFTYPE_AP)
  2844. },
  2845. };
  2846. #endif
  2847. static const struct ieee80211_iface_combination ath10k_if_comb[] = {
  2848. {
  2849. .limits = ath10k_if_limits,
  2850. .n_limits = ARRAY_SIZE(ath10k_if_limits),
  2851. .max_interfaces = 8,
  2852. .num_different_channels = 1,
  2853. .beacon_int_infra_match = true,
  2854. },
  2855. #ifdef CONFIG_ATH10K_DFS_CERTIFIED
  2856. {
  2857. .limits = ath10k_if_dfs_limits,
  2858. .n_limits = ARRAY_SIZE(ath10k_if_dfs_limits),
  2859. .max_interfaces = 8,
  2860. .num_different_channels = 1,
  2861. .beacon_int_infra_match = true,
  2862. .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
  2863. BIT(NL80211_CHAN_WIDTH_20) |
  2864. BIT(NL80211_CHAN_WIDTH_40) |
  2865. BIT(NL80211_CHAN_WIDTH_80),
  2866. }
  2867. #endif
  2868. };
  2869. static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
  2870. {
  2871. struct ieee80211_sta_vht_cap vht_cap = {0};
  2872. u16 mcs_map;
  2873. int i;
  2874. vht_cap.vht_supported = 1;
  2875. vht_cap.cap = ar->vht_cap_info;
  2876. mcs_map = 0;
  2877. for (i = 0; i < 8; i++) {
  2878. if (i < ar->num_rf_chains)
  2879. mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
  2880. else
  2881. mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
  2882. }
  2883. vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
  2884. vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
  2885. return vht_cap;
  2886. }
  2887. static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
  2888. {
  2889. int i;
  2890. struct ieee80211_sta_ht_cap ht_cap = {0};
  2891. if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
  2892. return ht_cap;
  2893. ht_cap.ht_supported = 1;
  2894. ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  2895. ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  2896. ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  2897. ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
  2898. ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
  2899. if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
  2900. ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
  2901. if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
  2902. ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
  2903. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
  2904. u32 smps;
  2905. smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
  2906. smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
  2907. ht_cap.cap |= smps;
  2908. }
  2909. if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
  2910. ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
  2911. if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
  2912. u32 stbc;
  2913. stbc = ar->ht_cap_info;
  2914. stbc &= WMI_HT_CAP_RX_STBC;
  2915. stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
  2916. stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
  2917. stbc &= IEEE80211_HT_CAP_RX_STBC;
  2918. ht_cap.cap |= stbc;
  2919. }
  2920. if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
  2921. ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
  2922. if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
  2923. ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
  2924. /* max AMSDU is implicitly taken from vht_cap_info */
  2925. if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
  2926. ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  2927. for (i = 0; i < ar->num_rf_chains; i++)
  2928. ht_cap.mcs.rx_mask[i] = 0xFF;
  2929. ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  2930. return ht_cap;
  2931. }
  2932. static void ath10k_get_arvif_iter(void *data, u8 *mac,
  2933. struct ieee80211_vif *vif)
  2934. {
  2935. struct ath10k_vif_iter *arvif_iter = data;
  2936. struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
  2937. if (arvif->vdev_id == arvif_iter->vdev_id)
  2938. arvif_iter->arvif = arvif;
  2939. }
  2940. struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
  2941. {
  2942. struct ath10k_vif_iter arvif_iter;
  2943. u32 flags;
  2944. memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
  2945. arvif_iter.vdev_id = vdev_id;
  2946. flags = IEEE80211_IFACE_ITER_RESUME_ALL;
  2947. ieee80211_iterate_active_interfaces_atomic(ar->hw,
  2948. flags,
  2949. ath10k_get_arvif_iter,
  2950. &arvif_iter);
  2951. if (!arvif_iter.arvif) {
  2952. ath10k_warn("No VIF found for VDEV: %d\n", vdev_id);
  2953. return NULL;
  2954. }
  2955. return arvif_iter.arvif;
  2956. }
  2957. int ath10k_mac_register(struct ath10k *ar)
  2958. {
  2959. struct ieee80211_supported_band *band;
  2960. struct ieee80211_sta_vht_cap vht_cap;
  2961. struct ieee80211_sta_ht_cap ht_cap;
  2962. void *channels;
  2963. int ret;
  2964. SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
  2965. SET_IEEE80211_DEV(ar->hw, ar->dev);
  2966. ht_cap = ath10k_get_ht_cap(ar);
  2967. vht_cap = ath10k_create_vht_cap(ar);
  2968. if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
  2969. channels = kmemdup(ath10k_2ghz_channels,
  2970. sizeof(ath10k_2ghz_channels),
  2971. GFP_KERNEL);
  2972. if (!channels) {
  2973. ret = -ENOMEM;
  2974. goto err_free;
  2975. }
  2976. band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
  2977. band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
  2978. band->channels = channels;
  2979. band->n_bitrates = ath10k_g_rates_size;
  2980. band->bitrates = ath10k_g_rates;
  2981. band->ht_cap = ht_cap;
  2982. /* vht is not supported in 2.4 GHz */
  2983. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
  2984. }
  2985. if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
  2986. channels = kmemdup(ath10k_5ghz_channels,
  2987. sizeof(ath10k_5ghz_channels),
  2988. GFP_KERNEL);
  2989. if (!channels) {
  2990. ret = -ENOMEM;
  2991. goto err_free;
  2992. }
  2993. band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
  2994. band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
  2995. band->channels = channels;
  2996. band->n_bitrates = ath10k_a_rates_size;
  2997. band->bitrates = ath10k_a_rates;
  2998. band->ht_cap = ht_cap;
  2999. band->vht_cap = vht_cap;
  3000. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
  3001. }
  3002. ar->hw->wiphy->interface_modes =
  3003. BIT(NL80211_IFTYPE_STATION) |
  3004. BIT(NL80211_IFTYPE_ADHOC) |
  3005. BIT(NL80211_IFTYPE_AP) |
  3006. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  3007. BIT(NL80211_IFTYPE_P2P_GO);
  3008. ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
  3009. IEEE80211_HW_SUPPORTS_PS |
  3010. IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
  3011. IEEE80211_HW_SUPPORTS_UAPSD |
  3012. IEEE80211_HW_MFP_CAPABLE |
  3013. IEEE80211_HW_REPORTS_TX_ACK_STATUS |
  3014. IEEE80211_HW_HAS_RATE_CONTROL |
  3015. IEEE80211_HW_SUPPORTS_STATIC_SMPS |
  3016. IEEE80211_HW_WANT_MONITOR_VIF |
  3017. IEEE80211_HW_AP_LINK_PS;
  3018. /* MSDU can have HTT TX fragment pushed in front. The additional 4
  3019. * bytes is used for padding/alignment if necessary. */
  3020. ar->hw->extra_tx_headroom += sizeof(struct htt_data_tx_desc_frag)*2 + 4;
  3021. if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
  3022. ar->hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS;
  3023. if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
  3024. ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  3025. ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
  3026. }
  3027. ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
  3028. ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
  3029. ar->hw->vif_data_size = sizeof(struct ath10k_vif);
  3030. ar->hw->channel_change_time = 5000;
  3031. ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
  3032. ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  3033. ar->hw->wiphy->max_remain_on_channel_duration = 5000;
  3034. ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
  3035. /*
  3036. * on LL hardware queues are managed entirely by the FW
  3037. * so we only advertise to mac we can do the queues thing
  3038. */
  3039. ar->hw->queues = 4;
  3040. ar->hw->wiphy->iface_combinations = ath10k_if_comb;
  3041. ar->hw->wiphy->n_iface_combinations = ARRAY_SIZE(ath10k_if_comb);
  3042. ar->hw->netdev_features = NETIF_F_HW_CSUM;
  3043. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
  3044. /* Init ath dfs pattern detector */
  3045. ar->ath_common.debug_mask = ATH_DBG_DFS;
  3046. ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
  3047. NL80211_DFS_UNSET);
  3048. if (!ar->dfs_detector)
  3049. ath10k_warn("dfs pattern detector init failed\n");
  3050. }
  3051. ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
  3052. ath10k_reg_notifier);
  3053. if (ret) {
  3054. ath10k_err("Regulatory initialization failed\n");
  3055. goto err_free;
  3056. }
  3057. ret = ieee80211_register_hw(ar->hw);
  3058. if (ret) {
  3059. ath10k_err("ieee80211 registration failed: %d\n", ret);
  3060. goto err_free;
  3061. }
  3062. if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
  3063. ret = regulatory_hint(ar->hw->wiphy,
  3064. ar->ath_common.regulatory.alpha2);
  3065. if (ret)
  3066. goto err_unregister;
  3067. }
  3068. return 0;
  3069. err_unregister:
  3070. ieee80211_unregister_hw(ar->hw);
  3071. err_free:
  3072. kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
  3073. kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
  3074. return ret;
  3075. }
  3076. void ath10k_mac_unregister(struct ath10k *ar)
  3077. {
  3078. ieee80211_unregister_hw(ar->hw);
  3079. if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
  3080. ar->dfs_detector->exit(ar->dfs_detector);
  3081. kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
  3082. kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
  3083. SET_IEEE80211_DEV(ar->hw, NULL);
  3084. }