mac.c 83 KB

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