main.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * mac80211 interaction code
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, see
  21. * http://www.gnu.org/licenses/.
  22. *
  23. * This file incorporates work covered by the following copyright and
  24. * permission notice:
  25. * Copyright (c) 2007-2008 Atheros Communications, Inc.
  26. *
  27. * Permission to use, copy, modify, and/or distribute this software for any
  28. * purpose with or without fee is hereby granted, provided that the above
  29. * copyright notice and this permission notice appear in all copies.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  32. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  34. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  35. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  36. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  37. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  38. */
  39. #include <linux/slab.h>
  40. #include <linux/module.h>
  41. #include <linux/etherdevice.h>
  42. #include <linux/random.h>
  43. #include <net/mac80211.h>
  44. #include <net/cfg80211.h>
  45. #include "hw.h"
  46. #include "carl9170.h"
  47. #include "cmd.h"
  48. static bool modparam_nohwcrypt;
  49. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  50. MODULE_PARM_DESC(nohwcrypt, "Disable hardware crypto offload.");
  51. int modparam_noht;
  52. module_param_named(noht, modparam_noht, int, S_IRUGO);
  53. MODULE_PARM_DESC(noht, "Disable MPDU aggregation.");
  54. #define RATE(_bitrate, _hw_rate, _txpidx, _flags) { \
  55. .bitrate = (_bitrate), \
  56. .flags = (_flags), \
  57. .hw_value = (_hw_rate) | (_txpidx) << 4, \
  58. }
  59. struct ieee80211_rate __carl9170_ratetable[] = {
  60. RATE(10, 0, 0, 0),
  61. RATE(20, 1, 1, IEEE80211_RATE_SHORT_PREAMBLE),
  62. RATE(55, 2, 2, IEEE80211_RATE_SHORT_PREAMBLE),
  63. RATE(110, 3, 3, IEEE80211_RATE_SHORT_PREAMBLE),
  64. RATE(60, 0xb, 0, 0),
  65. RATE(90, 0xf, 0, 0),
  66. RATE(120, 0xa, 0, 0),
  67. RATE(180, 0xe, 0, 0),
  68. RATE(240, 0x9, 0, 0),
  69. RATE(360, 0xd, 1, 0),
  70. RATE(480, 0x8, 2, 0),
  71. RATE(540, 0xc, 3, 0),
  72. };
  73. #undef RATE
  74. #define carl9170_g_ratetable (__carl9170_ratetable + 0)
  75. #define carl9170_g_ratetable_size 12
  76. #define carl9170_a_ratetable (__carl9170_ratetable + 4)
  77. #define carl9170_a_ratetable_size 8
  78. /*
  79. * NB: The hw_value is used as an index into the carl9170_phy_freq_params
  80. * array in phy.c so that we don't have to do frequency lookups!
  81. */
  82. #define CHAN(_freq, _idx) { \
  83. .center_freq = (_freq), \
  84. .hw_value = (_idx), \
  85. .max_power = 18, /* XXX */ \
  86. }
  87. static struct ieee80211_channel carl9170_2ghz_chantable[] = {
  88. CHAN(2412, 0),
  89. CHAN(2417, 1),
  90. CHAN(2422, 2),
  91. CHAN(2427, 3),
  92. CHAN(2432, 4),
  93. CHAN(2437, 5),
  94. CHAN(2442, 6),
  95. CHAN(2447, 7),
  96. CHAN(2452, 8),
  97. CHAN(2457, 9),
  98. CHAN(2462, 10),
  99. CHAN(2467, 11),
  100. CHAN(2472, 12),
  101. CHAN(2484, 13),
  102. };
  103. static struct ieee80211_channel carl9170_5ghz_chantable[] = {
  104. CHAN(4920, 14),
  105. CHAN(4940, 15),
  106. CHAN(4960, 16),
  107. CHAN(4980, 17),
  108. CHAN(5040, 18),
  109. CHAN(5060, 19),
  110. CHAN(5080, 20),
  111. CHAN(5180, 21),
  112. CHAN(5200, 22),
  113. CHAN(5220, 23),
  114. CHAN(5240, 24),
  115. CHAN(5260, 25),
  116. CHAN(5280, 26),
  117. CHAN(5300, 27),
  118. CHAN(5320, 28),
  119. CHAN(5500, 29),
  120. CHAN(5520, 30),
  121. CHAN(5540, 31),
  122. CHAN(5560, 32),
  123. CHAN(5580, 33),
  124. CHAN(5600, 34),
  125. CHAN(5620, 35),
  126. CHAN(5640, 36),
  127. CHAN(5660, 37),
  128. CHAN(5680, 38),
  129. CHAN(5700, 39),
  130. CHAN(5745, 40),
  131. CHAN(5765, 41),
  132. CHAN(5785, 42),
  133. CHAN(5805, 43),
  134. CHAN(5825, 44),
  135. CHAN(5170, 45),
  136. CHAN(5190, 46),
  137. CHAN(5210, 47),
  138. CHAN(5230, 48),
  139. };
  140. #undef CHAN
  141. #define CARL9170_HT_CAP \
  142. { \
  143. .ht_supported = true, \
  144. .cap = IEEE80211_HT_CAP_MAX_AMSDU | \
  145. IEEE80211_HT_CAP_SUP_WIDTH_20_40 | \
  146. IEEE80211_HT_CAP_SGI_40 | \
  147. IEEE80211_HT_CAP_DSSSCCK40 | \
  148. IEEE80211_HT_CAP_SM_PS, \
  149. .ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K, \
  150. .ampdu_density = IEEE80211_HT_MPDU_DENSITY_8, \
  151. .mcs = { \
  152. .rx_mask = { 0xff, 0xff, 0, 0, 0x1, 0, 0, 0, 0, 0, }, \
  153. .rx_highest = cpu_to_le16(300), \
  154. .tx_params = IEEE80211_HT_MCS_TX_DEFINED, \
  155. }, \
  156. }
  157. static struct ieee80211_supported_band carl9170_band_2GHz = {
  158. .channels = carl9170_2ghz_chantable,
  159. .n_channels = ARRAY_SIZE(carl9170_2ghz_chantable),
  160. .bitrates = carl9170_g_ratetable,
  161. .n_bitrates = carl9170_g_ratetable_size,
  162. .ht_cap = CARL9170_HT_CAP,
  163. };
  164. static struct ieee80211_supported_band carl9170_band_5GHz = {
  165. .channels = carl9170_5ghz_chantable,
  166. .n_channels = ARRAY_SIZE(carl9170_5ghz_chantable),
  167. .bitrates = carl9170_a_ratetable,
  168. .n_bitrates = carl9170_a_ratetable_size,
  169. .ht_cap = CARL9170_HT_CAP,
  170. };
  171. static void carl9170_ampdu_gc(struct ar9170 *ar)
  172. {
  173. struct carl9170_sta_tid *tid_info;
  174. LIST_HEAD(tid_gc);
  175. rcu_read_lock();
  176. list_for_each_entry_rcu(tid_info, &ar->tx_ampdu_list, list) {
  177. spin_lock_bh(&ar->tx_ampdu_list_lock);
  178. if (tid_info->state == CARL9170_TID_STATE_SHUTDOWN) {
  179. tid_info->state = CARL9170_TID_STATE_KILLED;
  180. list_del_rcu(&tid_info->list);
  181. ar->tx_ampdu_list_len--;
  182. list_add_tail(&tid_info->tmp_list, &tid_gc);
  183. }
  184. spin_unlock_bh(&ar->tx_ampdu_list_lock);
  185. }
  186. rcu_assign_pointer(ar->tx_ampdu_iter, tid_info);
  187. rcu_read_unlock();
  188. synchronize_rcu();
  189. while (!list_empty(&tid_gc)) {
  190. struct sk_buff *skb;
  191. tid_info = list_first_entry(&tid_gc, struct carl9170_sta_tid,
  192. tmp_list);
  193. while ((skb = __skb_dequeue(&tid_info->queue)))
  194. carl9170_tx_status(ar, skb, false);
  195. list_del_init(&tid_info->tmp_list);
  196. kfree(tid_info);
  197. }
  198. }
  199. static void carl9170_flush(struct ar9170 *ar, bool drop_queued)
  200. {
  201. if (drop_queued) {
  202. int i;
  203. /*
  204. * We can only drop frames which have not been uploaded
  205. * to the device yet.
  206. */
  207. for (i = 0; i < ar->hw->queues; i++) {
  208. struct sk_buff *skb;
  209. while ((skb = skb_dequeue(&ar->tx_pending[i]))) {
  210. struct ieee80211_tx_info *info;
  211. info = IEEE80211_SKB_CB(skb);
  212. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  213. atomic_dec(&ar->tx_ampdu_upload);
  214. carl9170_tx_status(ar, skb, false);
  215. }
  216. }
  217. }
  218. /* Wait for all other outstanding frames to timeout. */
  219. if (atomic_read(&ar->tx_total_queued))
  220. WARN_ON(wait_for_completion_timeout(&ar->tx_flush, HZ) == 0);
  221. }
  222. static void carl9170_flush_ba(struct ar9170 *ar)
  223. {
  224. struct sk_buff_head free;
  225. struct carl9170_sta_tid *tid_info;
  226. struct sk_buff *skb;
  227. __skb_queue_head_init(&free);
  228. rcu_read_lock();
  229. spin_lock_bh(&ar->tx_ampdu_list_lock);
  230. list_for_each_entry_rcu(tid_info, &ar->tx_ampdu_list, list) {
  231. if (tid_info->state > CARL9170_TID_STATE_SUSPEND) {
  232. tid_info->state = CARL9170_TID_STATE_SUSPEND;
  233. spin_lock(&tid_info->lock);
  234. while ((skb = __skb_dequeue(&tid_info->queue)))
  235. __skb_queue_tail(&free, skb);
  236. spin_unlock(&tid_info->lock);
  237. }
  238. }
  239. spin_unlock_bh(&ar->tx_ampdu_list_lock);
  240. rcu_read_unlock();
  241. while ((skb = __skb_dequeue(&free)))
  242. carl9170_tx_status(ar, skb, false);
  243. }
  244. static void carl9170_zap_queues(struct ar9170 *ar)
  245. {
  246. struct carl9170_vif_info *cvif;
  247. unsigned int i;
  248. carl9170_ampdu_gc(ar);
  249. carl9170_flush_ba(ar);
  250. carl9170_flush(ar, true);
  251. for (i = 0; i < ar->hw->queues; i++) {
  252. spin_lock_bh(&ar->tx_status[i].lock);
  253. while (!skb_queue_empty(&ar->tx_status[i])) {
  254. struct sk_buff *skb;
  255. skb = skb_peek(&ar->tx_status[i]);
  256. carl9170_tx_get_skb(skb);
  257. spin_unlock_bh(&ar->tx_status[i].lock);
  258. carl9170_tx_drop(ar, skb);
  259. spin_lock_bh(&ar->tx_status[i].lock);
  260. carl9170_tx_put_skb(skb);
  261. }
  262. spin_unlock_bh(&ar->tx_status[i].lock);
  263. }
  264. BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_SOFT < 1);
  265. BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD < CARL9170_NUM_TX_LIMIT_SOFT);
  266. BUILD_BUG_ON(CARL9170_NUM_TX_LIMIT_HARD >= CARL9170_BAW_BITS);
  267. /* reinitialize queues statistics */
  268. memset(&ar->tx_stats, 0, sizeof(ar->tx_stats));
  269. for (i = 0; i < ar->hw->queues; i++)
  270. ar->tx_stats[i].limit = CARL9170_NUM_TX_LIMIT_HARD;
  271. for (i = 0; i < DIV_ROUND_UP(ar->fw.mem_blocks, BITS_PER_LONG); i++)
  272. ar->mem_bitmap[i] = 0;
  273. rcu_read_lock();
  274. list_for_each_entry_rcu(cvif, &ar->vif_list, list) {
  275. spin_lock_bh(&ar->beacon_lock);
  276. dev_kfree_skb_any(cvif->beacon);
  277. cvif->beacon = NULL;
  278. spin_unlock_bh(&ar->beacon_lock);
  279. }
  280. rcu_read_unlock();
  281. atomic_set(&ar->tx_ampdu_upload, 0);
  282. atomic_set(&ar->tx_ampdu_scheduler, 0);
  283. atomic_set(&ar->tx_total_pending, 0);
  284. atomic_set(&ar->tx_total_queued, 0);
  285. atomic_set(&ar->mem_free_blocks, ar->fw.mem_blocks);
  286. }
  287. #define CARL9170_FILL_QUEUE(queue, ai_fs, cwmin, cwmax, _txop) \
  288. do { \
  289. queue.aifs = ai_fs; \
  290. queue.cw_min = cwmin; \
  291. queue.cw_max = cwmax; \
  292. queue.txop = _txop; \
  293. } while (0)
  294. static int carl9170_op_start(struct ieee80211_hw *hw)
  295. {
  296. struct ar9170 *ar = hw->priv;
  297. int err, i;
  298. mutex_lock(&ar->mutex);
  299. carl9170_zap_queues(ar);
  300. /* reset QoS defaults */
  301. CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_VO], 2, 3, 7, 47);
  302. CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_VI], 2, 7, 15, 94);
  303. CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_BE], 3, 15, 1023, 0);
  304. CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_BK], 7, 15, 1023, 0);
  305. CARL9170_FILL_QUEUE(ar->edcf[AR9170_TXQ_SPECIAL], 2, 3, 7, 0);
  306. ar->current_factor = ar->current_density = -1;
  307. /* "The first key is unique." */
  308. ar->usedkeys = 1;
  309. ar->filter_state = 0;
  310. ar->ps.last_action = jiffies;
  311. ar->ps.last_slept = jiffies;
  312. ar->erp_mode = CARL9170_ERP_AUTO;
  313. /* Set "disable hw crypto offload" whenever the module parameter
  314. * nohwcrypt is true or if the firmware does not support it.
  315. */
  316. ar->disable_offload = modparam_nohwcrypt |
  317. ar->fw.disable_offload_fw;
  318. ar->rx_software_decryption = ar->disable_offload;
  319. for (i = 0; i < ar->hw->queues; i++) {
  320. ar->queue_stop_timeout[i] = jiffies;
  321. ar->max_queue_stop_timeout[i] = 0;
  322. }
  323. atomic_set(&ar->mem_allocs, 0);
  324. err = carl9170_usb_open(ar);
  325. if (err)
  326. goto out;
  327. err = carl9170_init_mac(ar);
  328. if (err)
  329. goto out;
  330. err = carl9170_set_qos(ar);
  331. if (err)
  332. goto out;
  333. if (ar->fw.rx_filter) {
  334. err = carl9170_rx_filter(ar, CARL9170_RX_FILTER_OTHER_RA |
  335. CARL9170_RX_FILTER_CTL_OTHER | CARL9170_RX_FILTER_BAD);
  336. if (err)
  337. goto out;
  338. }
  339. err = carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER,
  340. AR9170_DMA_TRIGGER_RXQ);
  341. if (err)
  342. goto out;
  343. /* Clear key-cache */
  344. for (i = 0; i < AR9170_CAM_MAX_USER + 4; i++) {
  345. err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
  346. 0, NULL, 0);
  347. if (err)
  348. goto out;
  349. err = carl9170_upload_key(ar, i, NULL, AR9170_ENC_ALG_NONE,
  350. 1, NULL, 0);
  351. if (err)
  352. goto out;
  353. if (i < AR9170_CAM_MAX_USER) {
  354. err = carl9170_disable_key(ar, i);
  355. if (err)
  356. goto out;
  357. }
  358. }
  359. carl9170_set_state_when(ar, CARL9170_IDLE, CARL9170_STARTED);
  360. ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
  361. round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
  362. ieee80211_wake_queues(ar->hw);
  363. err = 0;
  364. out:
  365. mutex_unlock(&ar->mutex);
  366. return err;
  367. }
  368. static void carl9170_cancel_worker(struct ar9170 *ar)
  369. {
  370. cancel_delayed_work_sync(&ar->stat_work);
  371. cancel_delayed_work_sync(&ar->tx_janitor);
  372. #ifdef CONFIG_CARL9170_LEDS
  373. cancel_delayed_work_sync(&ar->led_work);
  374. #endif /* CONFIG_CARL9170_LEDS */
  375. cancel_work_sync(&ar->ps_work);
  376. cancel_work_sync(&ar->ping_work);
  377. cancel_work_sync(&ar->ampdu_work);
  378. }
  379. static void carl9170_op_stop(struct ieee80211_hw *hw)
  380. {
  381. struct ar9170 *ar = hw->priv;
  382. carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
  383. ieee80211_stop_queues(ar->hw);
  384. mutex_lock(&ar->mutex);
  385. if (IS_ACCEPTING_CMD(ar)) {
  386. RCU_INIT_POINTER(ar->beacon_iter, NULL);
  387. carl9170_led_set_state(ar, 0);
  388. /* stop DMA */
  389. carl9170_write_reg(ar, AR9170_MAC_REG_DMA_TRIGGER, 0);
  390. carl9170_usb_stop(ar);
  391. }
  392. carl9170_zap_queues(ar);
  393. mutex_unlock(&ar->mutex);
  394. carl9170_cancel_worker(ar);
  395. }
  396. static void carl9170_restart_work(struct work_struct *work)
  397. {
  398. struct ar9170 *ar = container_of(work, struct ar9170,
  399. restart_work);
  400. int err = -EIO;
  401. ar->usedkeys = 0;
  402. ar->filter_state = 0;
  403. carl9170_cancel_worker(ar);
  404. mutex_lock(&ar->mutex);
  405. if (!ar->force_usb_reset) {
  406. err = carl9170_usb_restart(ar);
  407. if (net_ratelimit()) {
  408. if (err)
  409. dev_err(&ar->udev->dev, "Failed to restart device (%d).\n", err);
  410. else
  411. dev_info(&ar->udev->dev, "device restarted successfully.\n");
  412. }
  413. }
  414. carl9170_zap_queues(ar);
  415. mutex_unlock(&ar->mutex);
  416. if (!err && !ar->force_usb_reset) {
  417. ar->restart_counter++;
  418. atomic_set(&ar->pending_restarts, 0);
  419. ieee80211_restart_hw(ar->hw);
  420. } else {
  421. /*
  422. * The reset was unsuccessful and the device seems to
  423. * be dead. But there's still one option: a low-level
  424. * usb subsystem reset...
  425. */
  426. carl9170_usb_reset(ar);
  427. }
  428. }
  429. void carl9170_restart(struct ar9170 *ar, const enum carl9170_restart_reasons r)
  430. {
  431. carl9170_set_state_when(ar, CARL9170_STARTED, CARL9170_IDLE);
  432. /*
  433. * Sometimes, an error can trigger several different reset events.
  434. * By ignoring these *surplus* reset events, the device won't be
  435. * killed again, right after it has recovered.
  436. */
  437. if (atomic_inc_return(&ar->pending_restarts) > 1) {
  438. dev_dbg(&ar->udev->dev, "ignoring restart (%d)\n", r);
  439. return;
  440. }
  441. ieee80211_stop_queues(ar->hw);
  442. dev_err(&ar->udev->dev, "restart device (%d)\n", r);
  443. if (!WARN_ON(r == CARL9170_RR_NO_REASON) ||
  444. !WARN_ON(r >= __CARL9170_RR_LAST))
  445. ar->last_reason = r;
  446. if (!ar->registered)
  447. return;
  448. if (!IS_ACCEPTING_CMD(ar) || ar->needs_full_reset)
  449. ar->force_usb_reset = true;
  450. ieee80211_queue_work(ar->hw, &ar->restart_work);
  451. /*
  452. * At this point, the device instance might have vanished/disabled.
  453. * So, don't put any code which access the ar9170 struct
  454. * without proper protection.
  455. */
  456. }
  457. static void carl9170_ping_work(struct work_struct *work)
  458. {
  459. struct ar9170 *ar = container_of(work, struct ar9170, ping_work);
  460. int err;
  461. if (!IS_STARTED(ar))
  462. return;
  463. mutex_lock(&ar->mutex);
  464. err = carl9170_echo_test(ar, 0xdeadbeef);
  465. if (err)
  466. carl9170_restart(ar, CARL9170_RR_UNRESPONSIVE_DEVICE);
  467. mutex_unlock(&ar->mutex);
  468. }
  469. static int carl9170_init_interface(struct ar9170 *ar,
  470. struct ieee80211_vif *vif)
  471. {
  472. struct ath_common *common = &ar->common;
  473. int err;
  474. if (!vif) {
  475. WARN_ON_ONCE(IS_STARTED(ar));
  476. return 0;
  477. }
  478. memcpy(common->macaddr, vif->addr, ETH_ALEN);
  479. /* We have to fall back to software crypto, whenever
  480. * the user choose to participates in an IBSS. HW
  481. * offload for IBSS RSN is not supported by this driver.
  482. *
  483. * NOTE: If the previous main interface has already
  484. * disabled hw crypto offload, we have to keep this
  485. * previous disable_offload setting as it was.
  486. * Altough ideally, we should notify mac80211 and tell
  487. * it to forget about any HW crypto offload for now.
  488. */
  489. ar->disable_offload |= ((vif->type != NL80211_IFTYPE_STATION) &&
  490. (vif->type != NL80211_IFTYPE_AP));
  491. /* While the driver supports HW offload in a single
  492. * P2P client configuration, it doesn't support HW
  493. * offload in the favourit, concurrent P2P GO+CLIENT
  494. * configuration. Hence, HW offload will always be
  495. * disabled for P2P.
  496. */
  497. ar->disable_offload |= vif->p2p;
  498. ar->rx_software_decryption = ar->disable_offload;
  499. err = carl9170_set_operating_mode(ar);
  500. return err;
  501. }
  502. static int carl9170_op_add_interface(struct ieee80211_hw *hw,
  503. struct ieee80211_vif *vif)
  504. {
  505. struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
  506. struct ieee80211_vif *main_vif, *old_main = NULL;
  507. struct ar9170 *ar = hw->priv;
  508. int vif_id = -1, err = 0;
  509. mutex_lock(&ar->mutex);
  510. rcu_read_lock();
  511. if (vif_priv->active) {
  512. /*
  513. * Skip the interface structure initialization,
  514. * if the vif survived the _restart call.
  515. */
  516. vif_id = vif_priv->id;
  517. vif_priv->enable_beacon = false;
  518. spin_lock_bh(&ar->beacon_lock);
  519. dev_kfree_skb_any(vif_priv->beacon);
  520. vif_priv->beacon = NULL;
  521. spin_unlock_bh(&ar->beacon_lock);
  522. goto init;
  523. }
  524. /* Because the AR9170 HW's MAC doesn't provide full support for
  525. * multiple, independent interfaces [of different operation modes].
  526. * We have to select ONE main interface [main mode of HW], but we
  527. * can have multiple slaves [AKA: entry in the ACK-table].
  528. *
  529. * The first (from HEAD/TOP) interface in the ar->vif_list is
  530. * always the main intf. All following intfs in this list
  531. * are considered to be slave intfs.
  532. */
  533. main_vif = carl9170_get_main_vif(ar);
  534. if (main_vif) {
  535. switch (main_vif->type) {
  536. case NL80211_IFTYPE_STATION:
  537. if (vif->type == NL80211_IFTYPE_STATION)
  538. break;
  539. /* P2P GO [master] use-case
  540. * Because the P2P GO station is selected dynamically
  541. * by all participating peers of a WIFI Direct network,
  542. * the driver has be able to change the main interface
  543. * operating mode on the fly.
  544. */
  545. if (main_vif->p2p && vif->p2p &&
  546. vif->type == NL80211_IFTYPE_AP) {
  547. old_main = main_vif;
  548. break;
  549. }
  550. err = -EBUSY;
  551. rcu_read_unlock();
  552. goto unlock;
  553. case NL80211_IFTYPE_MESH_POINT:
  554. case NL80211_IFTYPE_AP:
  555. if ((vif->type == NL80211_IFTYPE_STATION) ||
  556. (vif->type == NL80211_IFTYPE_WDS) ||
  557. (vif->type == NL80211_IFTYPE_AP) ||
  558. (vif->type == NL80211_IFTYPE_MESH_POINT))
  559. break;
  560. err = -EBUSY;
  561. rcu_read_unlock();
  562. goto unlock;
  563. default:
  564. rcu_read_unlock();
  565. goto unlock;
  566. }
  567. }
  568. vif_id = bitmap_find_free_region(&ar->vif_bitmap, ar->fw.vif_num, 0);
  569. if (vif_id < 0) {
  570. rcu_read_unlock();
  571. err = -ENOSPC;
  572. goto unlock;
  573. }
  574. BUG_ON(ar->vif_priv[vif_id].id != vif_id);
  575. vif_priv->active = true;
  576. vif_priv->id = vif_id;
  577. vif_priv->enable_beacon = false;
  578. ar->vifs++;
  579. if (old_main) {
  580. /* We end up in here, if the main interface is being replaced.
  581. * Put the new main interface at the HEAD of the list and the
  582. * previous inteface will automatically become second in line.
  583. */
  584. list_add_rcu(&vif_priv->list, &ar->vif_list);
  585. } else {
  586. /* Add new inteface. If the list is empty, it will become the
  587. * main inteface, otherwise it will be slave.
  588. */
  589. list_add_tail_rcu(&vif_priv->list, &ar->vif_list);
  590. }
  591. rcu_assign_pointer(ar->vif_priv[vif_id].vif, vif);
  592. init:
  593. main_vif = carl9170_get_main_vif(ar);
  594. if (main_vif == vif) {
  595. rcu_assign_pointer(ar->beacon_iter, vif_priv);
  596. rcu_read_unlock();
  597. if (old_main) {
  598. struct carl9170_vif_info *old_main_priv =
  599. (void *) old_main->drv_priv;
  600. /* downgrade old main intf to slave intf.
  601. * NOTE: We are no longer under rcu_read_lock.
  602. * But we are still holding ar->mutex, so the
  603. * vif data [id, addr] is safe.
  604. */
  605. err = carl9170_mod_virtual_mac(ar, old_main_priv->id,
  606. old_main->addr);
  607. if (err)
  608. goto unlock;
  609. }
  610. err = carl9170_init_interface(ar, vif);
  611. if (err)
  612. goto unlock;
  613. } else {
  614. rcu_read_unlock();
  615. err = carl9170_mod_virtual_mac(ar, vif_id, vif->addr);
  616. if (err)
  617. goto unlock;
  618. }
  619. if (ar->fw.tx_seq_table) {
  620. err = carl9170_write_reg(ar, ar->fw.tx_seq_table + vif_id * 4,
  621. 0);
  622. if (err)
  623. goto unlock;
  624. }
  625. unlock:
  626. if (err && (vif_id >= 0)) {
  627. vif_priv->active = false;
  628. bitmap_release_region(&ar->vif_bitmap, vif_id, 0);
  629. ar->vifs--;
  630. RCU_INIT_POINTER(ar->vif_priv[vif_id].vif, NULL);
  631. list_del_rcu(&vif_priv->list);
  632. mutex_unlock(&ar->mutex);
  633. synchronize_rcu();
  634. } else {
  635. if (ar->vifs > 1)
  636. ar->ps.off_override |= PS_OFF_VIF;
  637. mutex_unlock(&ar->mutex);
  638. }
  639. return err;
  640. }
  641. static void carl9170_op_remove_interface(struct ieee80211_hw *hw,
  642. struct ieee80211_vif *vif)
  643. {
  644. struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
  645. struct ieee80211_vif *main_vif;
  646. struct ar9170 *ar = hw->priv;
  647. unsigned int id;
  648. mutex_lock(&ar->mutex);
  649. if (WARN_ON_ONCE(!vif_priv->active))
  650. goto unlock;
  651. ar->vifs--;
  652. rcu_read_lock();
  653. main_vif = carl9170_get_main_vif(ar);
  654. id = vif_priv->id;
  655. vif_priv->active = false;
  656. WARN_ON(vif_priv->enable_beacon);
  657. vif_priv->enable_beacon = false;
  658. list_del_rcu(&vif_priv->list);
  659. RCU_INIT_POINTER(ar->vif_priv[id].vif, NULL);
  660. if (vif == main_vif) {
  661. rcu_read_unlock();
  662. if (ar->vifs) {
  663. WARN_ON(carl9170_init_interface(ar,
  664. carl9170_get_main_vif(ar)));
  665. } else {
  666. carl9170_set_operating_mode(ar);
  667. }
  668. } else {
  669. rcu_read_unlock();
  670. WARN_ON(carl9170_mod_virtual_mac(ar, id, NULL));
  671. }
  672. carl9170_update_beacon(ar, false);
  673. carl9170_flush_cab(ar, id);
  674. spin_lock_bh(&ar->beacon_lock);
  675. dev_kfree_skb_any(vif_priv->beacon);
  676. vif_priv->beacon = NULL;
  677. spin_unlock_bh(&ar->beacon_lock);
  678. bitmap_release_region(&ar->vif_bitmap, id, 0);
  679. carl9170_set_beacon_timers(ar);
  680. if (ar->vifs == 1)
  681. ar->ps.off_override &= ~PS_OFF_VIF;
  682. unlock:
  683. mutex_unlock(&ar->mutex);
  684. synchronize_rcu();
  685. }
  686. void carl9170_ps_check(struct ar9170 *ar)
  687. {
  688. ieee80211_queue_work(ar->hw, &ar->ps_work);
  689. }
  690. /* caller must hold ar->mutex */
  691. static int carl9170_ps_update(struct ar9170 *ar)
  692. {
  693. bool ps = false;
  694. int err = 0;
  695. if (!ar->ps.off_override)
  696. ps = (ar->hw->conf.flags & IEEE80211_CONF_PS);
  697. if (ps != ar->ps.state) {
  698. err = carl9170_powersave(ar, ps);
  699. if (err)
  700. return err;
  701. if (ar->ps.state && !ps) {
  702. ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
  703. ar->ps.last_action);
  704. }
  705. if (ps)
  706. ar->ps.last_slept = jiffies;
  707. ar->ps.last_action = jiffies;
  708. ar->ps.state = ps;
  709. }
  710. return 0;
  711. }
  712. static void carl9170_ps_work(struct work_struct *work)
  713. {
  714. struct ar9170 *ar = container_of(work, struct ar9170,
  715. ps_work);
  716. mutex_lock(&ar->mutex);
  717. if (IS_STARTED(ar))
  718. WARN_ON_ONCE(carl9170_ps_update(ar) != 0);
  719. mutex_unlock(&ar->mutex);
  720. }
  721. static int carl9170_update_survey(struct ar9170 *ar, bool flush, bool noise)
  722. {
  723. int err;
  724. if (noise) {
  725. err = carl9170_get_noisefloor(ar);
  726. if (err)
  727. return err;
  728. }
  729. if (ar->fw.hw_counters) {
  730. err = carl9170_collect_tally(ar);
  731. if (err)
  732. return err;
  733. }
  734. if (flush)
  735. memset(&ar->tally, 0, sizeof(ar->tally));
  736. return 0;
  737. }
  738. static void carl9170_stat_work(struct work_struct *work)
  739. {
  740. struct ar9170 *ar = container_of(work, struct ar9170, stat_work.work);
  741. int err;
  742. mutex_lock(&ar->mutex);
  743. err = carl9170_update_survey(ar, false, true);
  744. mutex_unlock(&ar->mutex);
  745. if (err)
  746. return;
  747. ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
  748. round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
  749. }
  750. static int carl9170_op_config(struct ieee80211_hw *hw, u32 changed)
  751. {
  752. struct ar9170 *ar = hw->priv;
  753. int err = 0;
  754. mutex_lock(&ar->mutex);
  755. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  756. /* TODO */
  757. err = 0;
  758. }
  759. if (changed & IEEE80211_CONF_CHANGE_PS) {
  760. err = carl9170_ps_update(ar);
  761. if (err)
  762. goto out;
  763. }
  764. if (changed & IEEE80211_CONF_CHANGE_SMPS) {
  765. /* TODO */
  766. err = 0;
  767. }
  768. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  769. enum nl80211_channel_type channel_type =
  770. cfg80211_get_chandef_type(&hw->conf.chandef);
  771. /* adjust slot time for 5 GHz */
  772. err = carl9170_set_slot_time(ar);
  773. if (err)
  774. goto out;
  775. err = carl9170_update_survey(ar, true, false);
  776. if (err)
  777. goto out;
  778. err = carl9170_set_channel(ar, hw->conf.chandef.chan,
  779. channel_type);
  780. if (err)
  781. goto out;
  782. err = carl9170_update_survey(ar, false, true);
  783. if (err)
  784. goto out;
  785. err = carl9170_set_dyn_sifs_ack(ar);
  786. if (err)
  787. goto out;
  788. err = carl9170_set_rts_cts_rate(ar);
  789. if (err)
  790. goto out;
  791. }
  792. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  793. err = carl9170_set_mac_tpc(ar, ar->hw->conf.chandef.chan);
  794. if (err)
  795. goto out;
  796. }
  797. out:
  798. mutex_unlock(&ar->mutex);
  799. return err;
  800. }
  801. static u64 carl9170_op_prepare_multicast(struct ieee80211_hw *hw,
  802. struct netdev_hw_addr_list *mc_list)
  803. {
  804. struct netdev_hw_addr *ha;
  805. u64 mchash;
  806. /* always get broadcast frames */
  807. mchash = 1ULL << (0xff >> 2);
  808. netdev_hw_addr_list_for_each(ha, mc_list)
  809. mchash |= 1ULL << (ha->addr[5] >> 2);
  810. return mchash;
  811. }
  812. static void carl9170_op_configure_filter(struct ieee80211_hw *hw,
  813. unsigned int changed_flags,
  814. unsigned int *new_flags,
  815. u64 multicast)
  816. {
  817. struct ar9170 *ar = hw->priv;
  818. /* mask supported flags */
  819. *new_flags &= FIF_ALLMULTI | ar->rx_filter_caps;
  820. if (!IS_ACCEPTING_CMD(ar))
  821. return;
  822. mutex_lock(&ar->mutex);
  823. ar->filter_state = *new_flags;
  824. /*
  825. * We can support more by setting the sniffer bit and
  826. * then checking the error flags, later.
  827. */
  828. if (*new_flags & FIF_ALLMULTI)
  829. multicast = ~0ULL;
  830. if (multicast != ar->cur_mc_hash)
  831. WARN_ON(carl9170_update_multicast(ar, multicast));
  832. if (changed_flags & FIF_OTHER_BSS) {
  833. ar->sniffer_enabled = !!(*new_flags & FIF_OTHER_BSS);
  834. WARN_ON(carl9170_set_operating_mode(ar));
  835. }
  836. if (ar->fw.rx_filter && changed_flags & ar->rx_filter_caps) {
  837. u32 rx_filter = 0;
  838. if (!ar->fw.ba_filter)
  839. rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
  840. if (!(*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL)))
  841. rx_filter |= CARL9170_RX_FILTER_BAD;
  842. if (!(*new_flags & FIF_CONTROL))
  843. rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
  844. if (!(*new_flags & FIF_PSPOLL))
  845. rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
  846. if (!(*new_flags & FIF_OTHER_BSS)) {
  847. rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
  848. rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
  849. }
  850. WARN_ON(carl9170_rx_filter(ar, rx_filter));
  851. }
  852. mutex_unlock(&ar->mutex);
  853. }
  854. static void carl9170_op_bss_info_changed(struct ieee80211_hw *hw,
  855. struct ieee80211_vif *vif,
  856. struct ieee80211_bss_conf *bss_conf,
  857. u32 changed)
  858. {
  859. struct ar9170 *ar = hw->priv;
  860. struct ath_common *common = &ar->common;
  861. int err = 0;
  862. struct carl9170_vif_info *vif_priv;
  863. struct ieee80211_vif *main_vif;
  864. mutex_lock(&ar->mutex);
  865. vif_priv = (void *) vif->drv_priv;
  866. main_vif = carl9170_get_main_vif(ar);
  867. if (WARN_ON(!main_vif))
  868. goto out;
  869. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  870. struct carl9170_vif_info *iter;
  871. int i = 0;
  872. vif_priv->enable_beacon = bss_conf->enable_beacon;
  873. rcu_read_lock();
  874. list_for_each_entry_rcu(iter, &ar->vif_list, list) {
  875. if (iter->active && iter->enable_beacon)
  876. i++;
  877. }
  878. rcu_read_unlock();
  879. ar->beacon_enabled = i;
  880. }
  881. if (changed & BSS_CHANGED_BEACON) {
  882. err = carl9170_update_beacon(ar, false);
  883. if (err)
  884. goto out;
  885. }
  886. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
  887. BSS_CHANGED_BEACON_INT)) {
  888. if (main_vif != vif) {
  889. bss_conf->beacon_int = main_vif->bss_conf.beacon_int;
  890. bss_conf->dtim_period = main_vif->bss_conf.dtim_period;
  891. }
  892. /*
  893. * Therefore a hard limit for the broadcast traffic should
  894. * prevent false alarms.
  895. */
  896. if (vif->type != NL80211_IFTYPE_STATION &&
  897. (bss_conf->beacon_int * bss_conf->dtim_period >=
  898. (CARL9170_QUEUE_STUCK_TIMEOUT / 2))) {
  899. err = -EINVAL;
  900. goto out;
  901. }
  902. err = carl9170_set_beacon_timers(ar);
  903. if (err)
  904. goto out;
  905. }
  906. if (changed & BSS_CHANGED_HT) {
  907. /* TODO */
  908. err = 0;
  909. if (err)
  910. goto out;
  911. }
  912. if (main_vif != vif)
  913. goto out;
  914. /*
  915. * The following settings can only be changed by the
  916. * master interface.
  917. */
  918. if (changed & BSS_CHANGED_BSSID) {
  919. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  920. err = carl9170_set_operating_mode(ar);
  921. if (err)
  922. goto out;
  923. }
  924. if (changed & BSS_CHANGED_ASSOC) {
  925. ar->common.curaid = bss_conf->aid;
  926. err = carl9170_set_beacon_timers(ar);
  927. if (err)
  928. goto out;
  929. }
  930. if (changed & BSS_CHANGED_ERP_SLOT) {
  931. err = carl9170_set_slot_time(ar);
  932. if (err)
  933. goto out;
  934. }
  935. if (changed & BSS_CHANGED_BASIC_RATES) {
  936. err = carl9170_set_mac_rates(ar);
  937. if (err)
  938. goto out;
  939. }
  940. out:
  941. WARN_ON_ONCE(err && IS_STARTED(ar));
  942. mutex_unlock(&ar->mutex);
  943. }
  944. static u64 carl9170_op_get_tsf(struct ieee80211_hw *hw,
  945. struct ieee80211_vif *vif)
  946. {
  947. struct ar9170 *ar = hw->priv;
  948. struct carl9170_tsf_rsp tsf;
  949. int err;
  950. mutex_lock(&ar->mutex);
  951. err = carl9170_exec_cmd(ar, CARL9170_CMD_READ_TSF,
  952. 0, NULL, sizeof(tsf), &tsf);
  953. mutex_unlock(&ar->mutex);
  954. if (WARN_ON(err))
  955. return 0;
  956. return le64_to_cpu(tsf.tsf_64);
  957. }
  958. static int carl9170_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  959. struct ieee80211_vif *vif,
  960. struct ieee80211_sta *sta,
  961. struct ieee80211_key_conf *key)
  962. {
  963. struct ar9170 *ar = hw->priv;
  964. int err = 0, i;
  965. u8 ktype;
  966. if (ar->disable_offload || !vif)
  967. return -EOPNOTSUPP;
  968. /* Fall back to software encryption whenever the driver is connected
  969. * to more than one network.
  970. *
  971. * This is very unfortunate, because some machines cannot handle
  972. * the high througput speed in 802.11n networks.
  973. */
  974. if (!is_main_vif(ar, vif)) {
  975. mutex_lock(&ar->mutex);
  976. goto err_softw;
  977. }
  978. /*
  979. * While the hardware supports *catch-all* key, for offloading
  980. * group-key en-/de-cryption. The way of how the hardware
  981. * decides which keyId maps to which key, remains a mystery...
  982. */
  983. if ((vif->type != NL80211_IFTYPE_STATION &&
  984. vif->type != NL80211_IFTYPE_ADHOC) &&
  985. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  986. return -EOPNOTSUPP;
  987. switch (key->cipher) {
  988. case WLAN_CIPHER_SUITE_WEP40:
  989. ktype = AR9170_ENC_ALG_WEP64;
  990. break;
  991. case WLAN_CIPHER_SUITE_WEP104:
  992. ktype = AR9170_ENC_ALG_WEP128;
  993. break;
  994. case WLAN_CIPHER_SUITE_TKIP:
  995. ktype = AR9170_ENC_ALG_TKIP;
  996. break;
  997. case WLAN_CIPHER_SUITE_CCMP:
  998. ktype = AR9170_ENC_ALG_AESCCMP;
  999. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  1000. break;
  1001. default:
  1002. return -EOPNOTSUPP;
  1003. }
  1004. mutex_lock(&ar->mutex);
  1005. if (cmd == SET_KEY) {
  1006. if (!IS_STARTED(ar)) {
  1007. err = -EOPNOTSUPP;
  1008. goto out;
  1009. }
  1010. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  1011. sta = NULL;
  1012. i = 64 + key->keyidx;
  1013. } else {
  1014. for (i = 0; i < 64; i++)
  1015. if (!(ar->usedkeys & BIT(i)))
  1016. break;
  1017. if (i == 64)
  1018. goto err_softw;
  1019. }
  1020. key->hw_key_idx = i;
  1021. err = carl9170_upload_key(ar, i, sta ? sta->addr : NULL,
  1022. ktype, 0, key->key,
  1023. min_t(u8, 16, key->keylen));
  1024. if (err)
  1025. goto out;
  1026. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  1027. err = carl9170_upload_key(ar, i, sta ? sta->addr :
  1028. NULL, ktype, 1,
  1029. key->key + 16, 16);
  1030. if (err)
  1031. goto out;
  1032. /*
  1033. * hardware is not capable generating MMIC
  1034. * of fragmented frames!
  1035. */
  1036. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1037. }
  1038. if (i < 64)
  1039. ar->usedkeys |= BIT(i);
  1040. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1041. } else {
  1042. if (!IS_STARTED(ar)) {
  1043. /* The device is gone... together with the key ;-) */
  1044. err = 0;
  1045. goto out;
  1046. }
  1047. if (key->hw_key_idx < 64) {
  1048. ar->usedkeys &= ~BIT(key->hw_key_idx);
  1049. } else {
  1050. err = carl9170_upload_key(ar, key->hw_key_idx, NULL,
  1051. AR9170_ENC_ALG_NONE, 0,
  1052. NULL, 0);
  1053. if (err)
  1054. goto out;
  1055. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  1056. err = carl9170_upload_key(ar, key->hw_key_idx,
  1057. NULL,
  1058. AR9170_ENC_ALG_NONE,
  1059. 1, NULL, 0);
  1060. if (err)
  1061. goto out;
  1062. }
  1063. }
  1064. err = carl9170_disable_key(ar, key->hw_key_idx);
  1065. if (err)
  1066. goto out;
  1067. }
  1068. out:
  1069. mutex_unlock(&ar->mutex);
  1070. return err;
  1071. err_softw:
  1072. if (!ar->rx_software_decryption) {
  1073. ar->rx_software_decryption = true;
  1074. carl9170_set_operating_mode(ar);
  1075. }
  1076. mutex_unlock(&ar->mutex);
  1077. return -ENOSPC;
  1078. }
  1079. static int carl9170_op_sta_add(struct ieee80211_hw *hw,
  1080. struct ieee80211_vif *vif,
  1081. struct ieee80211_sta *sta)
  1082. {
  1083. struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
  1084. unsigned int i;
  1085. atomic_set(&sta_info->pending_frames, 0);
  1086. if (sta->ht_cap.ht_supported) {
  1087. if (sta->ht_cap.ampdu_density > 6) {
  1088. /*
  1089. * HW does support 16us AMPDU density.
  1090. * No HT-Xmit for station.
  1091. */
  1092. return 0;
  1093. }
  1094. for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++)
  1095. RCU_INIT_POINTER(sta_info->agg[i], NULL);
  1096. sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
  1097. sta_info->ht_sta = true;
  1098. }
  1099. return 0;
  1100. }
  1101. static int carl9170_op_sta_remove(struct ieee80211_hw *hw,
  1102. struct ieee80211_vif *vif,
  1103. struct ieee80211_sta *sta)
  1104. {
  1105. struct ar9170 *ar = hw->priv;
  1106. struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
  1107. unsigned int i;
  1108. bool cleanup = false;
  1109. if (sta->ht_cap.ht_supported) {
  1110. sta_info->ht_sta = false;
  1111. rcu_read_lock();
  1112. for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++) {
  1113. struct carl9170_sta_tid *tid_info;
  1114. tid_info = rcu_dereference(sta_info->agg[i]);
  1115. RCU_INIT_POINTER(sta_info->agg[i], NULL);
  1116. if (!tid_info)
  1117. continue;
  1118. spin_lock_bh(&ar->tx_ampdu_list_lock);
  1119. if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
  1120. tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
  1121. spin_unlock_bh(&ar->tx_ampdu_list_lock);
  1122. cleanup = true;
  1123. }
  1124. rcu_read_unlock();
  1125. if (cleanup)
  1126. carl9170_ampdu_gc(ar);
  1127. }
  1128. return 0;
  1129. }
  1130. static int carl9170_op_conf_tx(struct ieee80211_hw *hw,
  1131. struct ieee80211_vif *vif, u16 queue,
  1132. const struct ieee80211_tx_queue_params *param)
  1133. {
  1134. struct ar9170 *ar = hw->priv;
  1135. int ret;
  1136. mutex_lock(&ar->mutex);
  1137. if (queue < ar->hw->queues) {
  1138. memcpy(&ar->edcf[ar9170_qmap[queue]], param, sizeof(*param));
  1139. ret = carl9170_set_qos(ar);
  1140. } else {
  1141. ret = -EINVAL;
  1142. }
  1143. mutex_unlock(&ar->mutex);
  1144. return ret;
  1145. }
  1146. static void carl9170_ampdu_work(struct work_struct *work)
  1147. {
  1148. struct ar9170 *ar = container_of(work, struct ar9170,
  1149. ampdu_work);
  1150. if (!IS_STARTED(ar))
  1151. return;
  1152. mutex_lock(&ar->mutex);
  1153. carl9170_ampdu_gc(ar);
  1154. mutex_unlock(&ar->mutex);
  1155. }
  1156. static int carl9170_op_ampdu_action(struct ieee80211_hw *hw,
  1157. struct ieee80211_vif *vif,
  1158. struct ieee80211_ampdu_params *params)
  1159. {
  1160. struct ieee80211_sta *sta = params->sta;
  1161. enum ieee80211_ampdu_mlme_action action = params->action;
  1162. u16 tid = params->tid;
  1163. u16 *ssn = &params->ssn;
  1164. struct ar9170 *ar = hw->priv;
  1165. struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
  1166. struct carl9170_sta_tid *tid_info;
  1167. if (modparam_noht)
  1168. return -EOPNOTSUPP;
  1169. switch (action) {
  1170. case IEEE80211_AMPDU_TX_START:
  1171. if (!sta_info->ht_sta)
  1172. return -EOPNOTSUPP;
  1173. tid_info = kzalloc(sizeof(struct carl9170_sta_tid),
  1174. GFP_ATOMIC);
  1175. if (!tid_info)
  1176. return -ENOMEM;
  1177. tid_info->hsn = tid_info->bsn = tid_info->snx = (*ssn);
  1178. tid_info->state = CARL9170_TID_STATE_PROGRESS;
  1179. tid_info->tid = tid;
  1180. tid_info->max = sta_info->ampdu_max_len;
  1181. tid_info->sta = sta;
  1182. tid_info->vif = vif;
  1183. INIT_LIST_HEAD(&tid_info->list);
  1184. INIT_LIST_HEAD(&tid_info->tmp_list);
  1185. skb_queue_head_init(&tid_info->queue);
  1186. spin_lock_init(&tid_info->lock);
  1187. spin_lock_bh(&ar->tx_ampdu_list_lock);
  1188. ar->tx_ampdu_list_len++;
  1189. list_add_tail_rcu(&tid_info->list, &ar->tx_ampdu_list);
  1190. rcu_assign_pointer(sta_info->agg[tid], tid_info);
  1191. spin_unlock_bh(&ar->tx_ampdu_list_lock);
  1192. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1193. break;
  1194. case IEEE80211_AMPDU_TX_STOP_CONT:
  1195. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1196. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1197. rcu_read_lock();
  1198. tid_info = rcu_dereference(sta_info->agg[tid]);
  1199. if (tid_info) {
  1200. spin_lock_bh(&ar->tx_ampdu_list_lock);
  1201. if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
  1202. tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
  1203. spin_unlock_bh(&ar->tx_ampdu_list_lock);
  1204. }
  1205. RCU_INIT_POINTER(sta_info->agg[tid], NULL);
  1206. rcu_read_unlock();
  1207. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1208. ieee80211_queue_work(ar->hw, &ar->ampdu_work);
  1209. break;
  1210. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1211. rcu_read_lock();
  1212. tid_info = rcu_dereference(sta_info->agg[tid]);
  1213. sta_info->stats[tid].clear = true;
  1214. sta_info->stats[tid].req = false;
  1215. if (tid_info) {
  1216. bitmap_zero(tid_info->bitmap, CARL9170_BAW_SIZE);
  1217. tid_info->state = CARL9170_TID_STATE_IDLE;
  1218. }
  1219. rcu_read_unlock();
  1220. if (WARN_ON_ONCE(!tid_info))
  1221. return -EFAULT;
  1222. break;
  1223. case IEEE80211_AMPDU_RX_START:
  1224. case IEEE80211_AMPDU_RX_STOP:
  1225. /* Handled by hardware */
  1226. break;
  1227. default:
  1228. return -EOPNOTSUPP;
  1229. }
  1230. return 0;
  1231. }
  1232. #ifdef CONFIG_CARL9170_WPC
  1233. static int carl9170_register_wps_button(struct ar9170 *ar)
  1234. {
  1235. struct input_dev *input;
  1236. int err;
  1237. if (!(ar->features & CARL9170_WPS_BUTTON))
  1238. return 0;
  1239. input = input_allocate_device();
  1240. if (!input)
  1241. return -ENOMEM;
  1242. snprintf(ar->wps.name, sizeof(ar->wps.name), "%s WPS Button",
  1243. wiphy_name(ar->hw->wiphy));
  1244. snprintf(ar->wps.phys, sizeof(ar->wps.phys),
  1245. "ieee80211/%s/input0", wiphy_name(ar->hw->wiphy));
  1246. input->name = ar->wps.name;
  1247. input->phys = ar->wps.phys;
  1248. input->id.bustype = BUS_USB;
  1249. input->dev.parent = &ar->hw->wiphy->dev;
  1250. input_set_capability(input, EV_KEY, KEY_WPS_BUTTON);
  1251. err = input_register_device(input);
  1252. if (err) {
  1253. input_free_device(input);
  1254. return err;
  1255. }
  1256. ar->wps.pbc = input;
  1257. return 0;
  1258. }
  1259. #endif /* CONFIG_CARL9170_WPC */
  1260. #ifdef CONFIG_CARL9170_HWRNG
  1261. static int carl9170_rng_get(struct ar9170 *ar)
  1262. {
  1263. #define RW (CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
  1264. #define RB (CARL9170_MAX_CMD_PAYLOAD_LEN)
  1265. static const __le32 rng_load[RW] = {
  1266. [0 ... (RW - 1)] = cpu_to_le32(AR9170_RAND_REG_NUM)};
  1267. u32 buf[RW];
  1268. unsigned int i, off = 0, transfer, count;
  1269. int err;
  1270. BUILD_BUG_ON(RB > CARL9170_MAX_CMD_PAYLOAD_LEN);
  1271. if (!IS_ACCEPTING_CMD(ar) || !ar->rng.initialized)
  1272. return -EAGAIN;
  1273. count = ARRAY_SIZE(ar->rng.cache);
  1274. while (count) {
  1275. err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
  1276. RB, (u8 *) rng_load,
  1277. RB, (u8 *) buf);
  1278. if (err)
  1279. return err;
  1280. transfer = min_t(unsigned int, count, RW);
  1281. for (i = 0; i < transfer; i++)
  1282. ar->rng.cache[off + i] = buf[i];
  1283. off += transfer;
  1284. count -= transfer;
  1285. }
  1286. ar->rng.cache_idx = 0;
  1287. #undef RW
  1288. #undef RB
  1289. return 0;
  1290. }
  1291. static int carl9170_rng_read(struct hwrng *rng, u32 *data)
  1292. {
  1293. struct ar9170 *ar = (struct ar9170 *)rng->priv;
  1294. int ret = -EIO;
  1295. mutex_lock(&ar->mutex);
  1296. if (ar->rng.cache_idx >= ARRAY_SIZE(ar->rng.cache)) {
  1297. ret = carl9170_rng_get(ar);
  1298. if (ret) {
  1299. mutex_unlock(&ar->mutex);
  1300. return ret;
  1301. }
  1302. }
  1303. *data = ar->rng.cache[ar->rng.cache_idx++];
  1304. mutex_unlock(&ar->mutex);
  1305. return sizeof(u16);
  1306. }
  1307. static void carl9170_unregister_hwrng(struct ar9170 *ar)
  1308. {
  1309. if (ar->rng.initialized) {
  1310. hwrng_unregister(&ar->rng.rng);
  1311. ar->rng.initialized = false;
  1312. }
  1313. }
  1314. static int carl9170_register_hwrng(struct ar9170 *ar)
  1315. {
  1316. int err;
  1317. snprintf(ar->rng.name, ARRAY_SIZE(ar->rng.name),
  1318. "%s_%s", KBUILD_MODNAME, wiphy_name(ar->hw->wiphy));
  1319. ar->rng.rng.name = ar->rng.name;
  1320. ar->rng.rng.data_read = carl9170_rng_read;
  1321. ar->rng.rng.priv = (unsigned long)ar;
  1322. if (WARN_ON(ar->rng.initialized))
  1323. return -EALREADY;
  1324. err = hwrng_register(&ar->rng.rng);
  1325. if (err) {
  1326. dev_err(&ar->udev->dev, "Failed to register the random "
  1327. "number generator (%d)\n", err);
  1328. return err;
  1329. }
  1330. ar->rng.initialized = true;
  1331. err = carl9170_rng_get(ar);
  1332. if (err) {
  1333. carl9170_unregister_hwrng(ar);
  1334. return err;
  1335. }
  1336. return 0;
  1337. }
  1338. #endif /* CONFIG_CARL9170_HWRNG */
  1339. static int carl9170_op_get_survey(struct ieee80211_hw *hw, int idx,
  1340. struct survey_info *survey)
  1341. {
  1342. struct ar9170 *ar = hw->priv;
  1343. struct ieee80211_channel *chan;
  1344. struct ieee80211_supported_band *band;
  1345. int err, b, i;
  1346. chan = ar->channel;
  1347. if (!chan)
  1348. return -ENODEV;
  1349. if (idx == chan->hw_value) {
  1350. mutex_lock(&ar->mutex);
  1351. err = carl9170_update_survey(ar, false, true);
  1352. mutex_unlock(&ar->mutex);
  1353. if (err)
  1354. return err;
  1355. }
  1356. for (b = 0; b < IEEE80211_NUM_BANDS; b++) {
  1357. band = ar->hw->wiphy->bands[b];
  1358. if (!band)
  1359. continue;
  1360. for (i = 0; i < band->n_channels; i++) {
  1361. if (band->channels[i].hw_value == idx) {
  1362. chan = &band->channels[i];
  1363. goto found;
  1364. }
  1365. }
  1366. }
  1367. return -ENOENT;
  1368. found:
  1369. memcpy(survey, &ar->survey[idx], sizeof(*survey));
  1370. survey->channel = chan;
  1371. survey->filled = SURVEY_INFO_NOISE_DBM;
  1372. if (ar->channel == chan)
  1373. survey->filled |= SURVEY_INFO_IN_USE;
  1374. if (ar->fw.hw_counters) {
  1375. survey->filled |= SURVEY_INFO_TIME |
  1376. SURVEY_INFO_TIME_BUSY |
  1377. SURVEY_INFO_TIME_TX;
  1378. }
  1379. return 0;
  1380. }
  1381. static void carl9170_op_flush(struct ieee80211_hw *hw,
  1382. struct ieee80211_vif *vif,
  1383. u32 queues, bool drop)
  1384. {
  1385. struct ar9170 *ar = hw->priv;
  1386. unsigned int vid;
  1387. mutex_lock(&ar->mutex);
  1388. for_each_set_bit(vid, &ar->vif_bitmap, ar->fw.vif_num)
  1389. carl9170_flush_cab(ar, vid);
  1390. carl9170_flush(ar, drop);
  1391. mutex_unlock(&ar->mutex);
  1392. }
  1393. static int carl9170_op_get_stats(struct ieee80211_hw *hw,
  1394. struct ieee80211_low_level_stats *stats)
  1395. {
  1396. struct ar9170 *ar = hw->priv;
  1397. memset(stats, 0, sizeof(*stats));
  1398. stats->dot11ACKFailureCount = ar->tx_ack_failures;
  1399. stats->dot11FCSErrorCount = ar->tx_fcs_errors;
  1400. return 0;
  1401. }
  1402. static void carl9170_op_sta_notify(struct ieee80211_hw *hw,
  1403. struct ieee80211_vif *vif,
  1404. enum sta_notify_cmd cmd,
  1405. struct ieee80211_sta *sta)
  1406. {
  1407. struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
  1408. switch (cmd) {
  1409. case STA_NOTIFY_SLEEP:
  1410. sta_info->sleeping = true;
  1411. if (atomic_read(&sta_info->pending_frames))
  1412. ieee80211_sta_block_awake(hw, sta, true);
  1413. break;
  1414. case STA_NOTIFY_AWAKE:
  1415. sta_info->sleeping = false;
  1416. break;
  1417. }
  1418. }
  1419. static bool carl9170_tx_frames_pending(struct ieee80211_hw *hw)
  1420. {
  1421. struct ar9170 *ar = hw->priv;
  1422. return !!atomic_read(&ar->tx_total_queued);
  1423. }
  1424. static const struct ieee80211_ops carl9170_ops = {
  1425. .start = carl9170_op_start,
  1426. .stop = carl9170_op_stop,
  1427. .tx = carl9170_op_tx,
  1428. .flush = carl9170_op_flush,
  1429. .add_interface = carl9170_op_add_interface,
  1430. .remove_interface = carl9170_op_remove_interface,
  1431. .config = carl9170_op_config,
  1432. .prepare_multicast = carl9170_op_prepare_multicast,
  1433. .configure_filter = carl9170_op_configure_filter,
  1434. .conf_tx = carl9170_op_conf_tx,
  1435. .bss_info_changed = carl9170_op_bss_info_changed,
  1436. .get_tsf = carl9170_op_get_tsf,
  1437. .set_key = carl9170_op_set_key,
  1438. .sta_add = carl9170_op_sta_add,
  1439. .sta_remove = carl9170_op_sta_remove,
  1440. .sta_notify = carl9170_op_sta_notify,
  1441. .get_survey = carl9170_op_get_survey,
  1442. .get_stats = carl9170_op_get_stats,
  1443. .ampdu_action = carl9170_op_ampdu_action,
  1444. .tx_frames_pending = carl9170_tx_frames_pending,
  1445. };
  1446. void *carl9170_alloc(size_t priv_size)
  1447. {
  1448. struct ieee80211_hw *hw;
  1449. struct ar9170 *ar;
  1450. struct sk_buff *skb;
  1451. int i;
  1452. /*
  1453. * this buffer is used for rx stream reconstruction.
  1454. * Under heavy load this device (or the transport layer?)
  1455. * tends to split the streams into separate rx descriptors.
  1456. */
  1457. skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
  1458. if (!skb)
  1459. goto err_nomem;
  1460. hw = ieee80211_alloc_hw(priv_size, &carl9170_ops);
  1461. if (!hw)
  1462. goto err_nomem;
  1463. ar = hw->priv;
  1464. ar->hw = hw;
  1465. ar->rx_failover = skb;
  1466. memset(&ar->rx_plcp, 0, sizeof(struct ar9170_rx_head));
  1467. ar->rx_has_plcp = false;
  1468. /*
  1469. * Here's a hidden pitfall!
  1470. *
  1471. * All 4 AC queues work perfectly well under _legacy_ operation.
  1472. * However as soon as aggregation is enabled, the traffic flow
  1473. * gets very bumpy. Therefore we have to _switch_ to a
  1474. * software AC with a single HW queue.
  1475. */
  1476. hw->queues = __AR9170_NUM_TXQ;
  1477. mutex_init(&ar->mutex);
  1478. spin_lock_init(&ar->beacon_lock);
  1479. spin_lock_init(&ar->cmd_lock);
  1480. spin_lock_init(&ar->tx_stats_lock);
  1481. spin_lock_init(&ar->tx_ampdu_list_lock);
  1482. spin_lock_init(&ar->mem_lock);
  1483. spin_lock_init(&ar->state_lock);
  1484. atomic_set(&ar->pending_restarts, 0);
  1485. ar->vifs = 0;
  1486. for (i = 0; i < ar->hw->queues; i++) {
  1487. skb_queue_head_init(&ar->tx_status[i]);
  1488. skb_queue_head_init(&ar->tx_pending[i]);
  1489. INIT_LIST_HEAD(&ar->bar_list[i]);
  1490. spin_lock_init(&ar->bar_list_lock[i]);
  1491. }
  1492. INIT_WORK(&ar->ps_work, carl9170_ps_work);
  1493. INIT_WORK(&ar->ping_work, carl9170_ping_work);
  1494. INIT_WORK(&ar->restart_work, carl9170_restart_work);
  1495. INIT_WORK(&ar->ampdu_work, carl9170_ampdu_work);
  1496. INIT_DELAYED_WORK(&ar->stat_work, carl9170_stat_work);
  1497. INIT_DELAYED_WORK(&ar->tx_janitor, carl9170_tx_janitor);
  1498. INIT_LIST_HEAD(&ar->tx_ampdu_list);
  1499. rcu_assign_pointer(ar->tx_ampdu_iter,
  1500. (struct carl9170_sta_tid *) &ar->tx_ampdu_list);
  1501. bitmap_zero(&ar->vif_bitmap, ar->fw.vif_num);
  1502. INIT_LIST_HEAD(&ar->vif_list);
  1503. init_completion(&ar->tx_flush);
  1504. /* firmware decides which modes we support */
  1505. hw->wiphy->interface_modes = 0;
  1506. ieee80211_hw_set(hw, RX_INCLUDES_FCS);
  1507. ieee80211_hw_set(hw, MFP_CAPABLE);
  1508. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  1509. ieee80211_hw_set(hw, SUPPORTS_PS);
  1510. ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
  1511. ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
  1512. ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
  1513. ieee80211_hw_set(hw, SIGNAL_DBM);
  1514. ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
  1515. if (!modparam_noht) {
  1516. /*
  1517. * see the comment above, why we allow the user
  1518. * to disable HT by a module parameter.
  1519. */
  1520. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  1521. }
  1522. hw->extra_tx_headroom = sizeof(struct _carl9170_tx_superframe);
  1523. hw->sta_data_size = sizeof(struct carl9170_sta_info);
  1524. hw->vif_data_size = sizeof(struct carl9170_vif_info);
  1525. hw->max_rates = CARL9170_TX_MAX_RATES;
  1526. hw->max_rate_tries = CARL9170_TX_USER_RATE_TRIES;
  1527. for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
  1528. ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
  1529. return ar;
  1530. err_nomem:
  1531. kfree_skb(skb);
  1532. return ERR_PTR(-ENOMEM);
  1533. }
  1534. static int carl9170_read_eeprom(struct ar9170 *ar)
  1535. {
  1536. #define RW 8 /* number of words to read at once */
  1537. #define RB (sizeof(u32) * RW)
  1538. u8 *eeprom = (void *)&ar->eeprom;
  1539. __le32 offsets[RW];
  1540. int i, j, err;
  1541. BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
  1542. BUILD_BUG_ON(RB > CARL9170_MAX_CMD_LEN - 4);
  1543. #ifndef __CHECKER__
  1544. /* don't want to handle trailing remains */
  1545. BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
  1546. #endif
  1547. for (i = 0; i < sizeof(ar->eeprom) / RB; i++) {
  1548. for (j = 0; j < RW; j++)
  1549. offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
  1550. RB * i + 4 * j);
  1551. err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
  1552. RB, (u8 *) &offsets,
  1553. RB, eeprom + RB * i);
  1554. if (err)
  1555. return err;
  1556. }
  1557. #undef RW
  1558. #undef RB
  1559. return 0;
  1560. }
  1561. static int carl9170_parse_eeprom(struct ar9170 *ar)
  1562. {
  1563. struct ath_regulatory *regulatory = &ar->common.regulatory;
  1564. unsigned int rx_streams, tx_streams, tx_params = 0;
  1565. int bands = 0;
  1566. int chans = 0;
  1567. if (ar->eeprom.length == cpu_to_le16(0xffff))
  1568. return -ENODATA;
  1569. rx_streams = hweight8(ar->eeprom.rx_mask);
  1570. tx_streams = hweight8(ar->eeprom.tx_mask);
  1571. if (rx_streams != tx_streams) {
  1572. tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
  1573. WARN_ON(!(tx_streams >= 1 && tx_streams <=
  1574. IEEE80211_HT_MCS_TX_MAX_STREAMS));
  1575. tx_params = (tx_streams - 1) <<
  1576. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  1577. carl9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
  1578. carl9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
  1579. }
  1580. if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
  1581. ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1582. &carl9170_band_2GHz;
  1583. chans += carl9170_band_2GHz.n_channels;
  1584. bands++;
  1585. }
  1586. if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
  1587. ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1588. &carl9170_band_5GHz;
  1589. chans += carl9170_band_5GHz.n_channels;
  1590. bands++;
  1591. }
  1592. if (!bands)
  1593. return -EINVAL;
  1594. ar->survey = kzalloc(sizeof(struct survey_info) * chans, GFP_KERNEL);
  1595. if (!ar->survey)
  1596. return -ENOMEM;
  1597. ar->num_channels = chans;
  1598. regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
  1599. /* second part of wiphy init */
  1600. SET_IEEE80211_PERM_ADDR(ar->hw, ar->eeprom.mac_address);
  1601. return 0;
  1602. }
  1603. static void carl9170_reg_notifier(struct wiphy *wiphy,
  1604. struct regulatory_request *request)
  1605. {
  1606. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1607. struct ar9170 *ar = hw->priv;
  1608. ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
  1609. }
  1610. int carl9170_register(struct ar9170 *ar)
  1611. {
  1612. struct ath_regulatory *regulatory = &ar->common.regulatory;
  1613. int err = 0, i;
  1614. if (WARN_ON(ar->mem_bitmap))
  1615. return -EINVAL;
  1616. ar->mem_bitmap = kzalloc(roundup(ar->fw.mem_blocks, BITS_PER_LONG) *
  1617. sizeof(unsigned long), GFP_KERNEL);
  1618. if (!ar->mem_bitmap)
  1619. return -ENOMEM;
  1620. /* try to read EEPROM, init MAC addr */
  1621. err = carl9170_read_eeprom(ar);
  1622. if (err)
  1623. return err;
  1624. err = carl9170_parse_eeprom(ar);
  1625. if (err)
  1626. return err;
  1627. err = ath_regd_init(regulatory, ar->hw->wiphy,
  1628. carl9170_reg_notifier);
  1629. if (err)
  1630. return err;
  1631. if (modparam_noht) {
  1632. carl9170_band_2GHz.ht_cap.ht_supported = false;
  1633. carl9170_band_5GHz.ht_cap.ht_supported = false;
  1634. }
  1635. for (i = 0; i < ar->fw.vif_num; i++) {
  1636. ar->vif_priv[i].id = i;
  1637. ar->vif_priv[i].vif = NULL;
  1638. }
  1639. err = ieee80211_register_hw(ar->hw);
  1640. if (err)
  1641. return err;
  1642. /* mac80211 interface is now registered */
  1643. ar->registered = true;
  1644. if (!ath_is_world_regd(regulatory))
  1645. regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
  1646. #ifdef CONFIG_CARL9170_DEBUGFS
  1647. carl9170_debugfs_register(ar);
  1648. #endif /* CONFIG_CARL9170_DEBUGFS */
  1649. err = carl9170_led_init(ar);
  1650. if (err)
  1651. goto err_unreg;
  1652. #ifdef CONFIG_CARL9170_LEDS
  1653. err = carl9170_led_register(ar);
  1654. if (err)
  1655. goto err_unreg;
  1656. #endif /* CONFIG_CARL9170_LEDS */
  1657. #ifdef CONFIG_CARL9170_WPC
  1658. err = carl9170_register_wps_button(ar);
  1659. if (err)
  1660. goto err_unreg;
  1661. #endif /* CONFIG_CARL9170_WPC */
  1662. #ifdef CONFIG_CARL9170_HWRNG
  1663. err = carl9170_register_hwrng(ar);
  1664. if (err)
  1665. goto err_unreg;
  1666. #endif /* CONFIG_CARL9170_HWRNG */
  1667. dev_info(&ar->udev->dev, "Atheros AR9170 is registered as '%s'\n",
  1668. wiphy_name(ar->hw->wiphy));
  1669. return 0;
  1670. err_unreg:
  1671. carl9170_unregister(ar);
  1672. return err;
  1673. }
  1674. void carl9170_unregister(struct ar9170 *ar)
  1675. {
  1676. if (!ar->registered)
  1677. return;
  1678. ar->registered = false;
  1679. #ifdef CONFIG_CARL9170_LEDS
  1680. carl9170_led_unregister(ar);
  1681. #endif /* CONFIG_CARL9170_LEDS */
  1682. #ifdef CONFIG_CARL9170_DEBUGFS
  1683. carl9170_debugfs_unregister(ar);
  1684. #endif /* CONFIG_CARL9170_DEBUGFS */
  1685. #ifdef CONFIG_CARL9170_WPC
  1686. if (ar->wps.pbc) {
  1687. input_unregister_device(ar->wps.pbc);
  1688. ar->wps.pbc = NULL;
  1689. }
  1690. #endif /* CONFIG_CARL9170_WPC */
  1691. #ifdef CONFIG_CARL9170_HWRNG
  1692. carl9170_unregister_hwrng(ar);
  1693. #endif /* CONFIG_CARL9170_HWRNG */
  1694. carl9170_cancel_worker(ar);
  1695. cancel_work_sync(&ar->restart_work);
  1696. ieee80211_unregister_hw(ar->hw);
  1697. }
  1698. void carl9170_free(struct ar9170 *ar)
  1699. {
  1700. WARN_ON(ar->registered);
  1701. WARN_ON(IS_INITIALIZED(ar));
  1702. kfree_skb(ar->rx_failover);
  1703. ar->rx_failover = NULL;
  1704. kfree(ar->mem_bitmap);
  1705. ar->mem_bitmap = NULL;
  1706. kfree(ar->survey);
  1707. ar->survey = NULL;
  1708. mutex_destroy(&ar->mutex);
  1709. ieee80211_free_hw(ar->hw);
  1710. }