mac.c 83 KB

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