mac.c 77 KB

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