mac.c 78 KB

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