main.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  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, 0444);
  50. MODULE_PARM_DESC(nohwcrypt, "Disable hardware crypto offload.");
  51. int modparam_noht;
  52. module_param_named(noht, modparam_noht, int, 0444);
  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. /* The driver used to have P2P GO+CLIENT support,
  492. * but since this was dropped and we don't know if
  493. * there are any gremlins lurking in the shadows,
  494. * so best we keep HW offload disabled for P2P.
  495. */
  496. ar->disable_offload |= vif->p2p;
  497. ar->rx_software_decryption = ar->disable_offload;
  498. err = carl9170_set_operating_mode(ar);
  499. return err;
  500. }
  501. static int carl9170_op_add_interface(struct ieee80211_hw *hw,
  502. struct ieee80211_vif *vif)
  503. {
  504. struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
  505. struct ieee80211_vif *main_vif, *old_main = NULL;
  506. struct ar9170 *ar = hw->priv;
  507. int vif_id = -1, err = 0;
  508. mutex_lock(&ar->mutex);
  509. rcu_read_lock();
  510. if (vif_priv->active) {
  511. /*
  512. * Skip the interface structure initialization,
  513. * if the vif survived the _restart call.
  514. */
  515. vif_id = vif_priv->id;
  516. vif_priv->enable_beacon = false;
  517. spin_lock_bh(&ar->beacon_lock);
  518. dev_kfree_skb_any(vif_priv->beacon);
  519. vif_priv->beacon = NULL;
  520. spin_unlock_bh(&ar->beacon_lock);
  521. goto init;
  522. }
  523. /* Because the AR9170 HW's MAC doesn't provide full support for
  524. * multiple, independent interfaces [of different operation modes].
  525. * We have to select ONE main interface [main mode of HW], but we
  526. * can have multiple slaves [AKA: entry in the ACK-table].
  527. *
  528. * The first (from HEAD/TOP) interface in the ar->vif_list is
  529. * always the main intf. All following intfs in this list
  530. * are considered to be slave intfs.
  531. */
  532. main_vif = carl9170_get_main_vif(ar);
  533. if (main_vif) {
  534. switch (main_vif->type) {
  535. case NL80211_IFTYPE_STATION:
  536. if (vif->type == NL80211_IFTYPE_STATION)
  537. break;
  538. err = -EBUSY;
  539. rcu_read_unlock();
  540. goto unlock;
  541. case NL80211_IFTYPE_MESH_POINT:
  542. case NL80211_IFTYPE_AP:
  543. if ((vif->type == NL80211_IFTYPE_STATION) ||
  544. (vif->type == NL80211_IFTYPE_WDS) ||
  545. (vif->type == NL80211_IFTYPE_AP) ||
  546. (vif->type == NL80211_IFTYPE_MESH_POINT))
  547. break;
  548. err = -EBUSY;
  549. rcu_read_unlock();
  550. goto unlock;
  551. default:
  552. rcu_read_unlock();
  553. goto unlock;
  554. }
  555. }
  556. vif_id = bitmap_find_free_region(&ar->vif_bitmap, ar->fw.vif_num, 0);
  557. if (vif_id < 0) {
  558. rcu_read_unlock();
  559. err = -ENOSPC;
  560. goto unlock;
  561. }
  562. BUG_ON(ar->vif_priv[vif_id].id != vif_id);
  563. vif_priv->active = true;
  564. vif_priv->id = vif_id;
  565. vif_priv->enable_beacon = false;
  566. ar->vifs++;
  567. if (old_main) {
  568. /* We end up in here, if the main interface is being replaced.
  569. * Put the new main interface at the HEAD of the list and the
  570. * previous inteface will automatically become second in line.
  571. */
  572. list_add_rcu(&vif_priv->list, &ar->vif_list);
  573. } else {
  574. /* Add new inteface. If the list is empty, it will become the
  575. * main inteface, otherwise it will be slave.
  576. */
  577. list_add_tail_rcu(&vif_priv->list, &ar->vif_list);
  578. }
  579. rcu_assign_pointer(ar->vif_priv[vif_id].vif, vif);
  580. init:
  581. main_vif = carl9170_get_main_vif(ar);
  582. if (main_vif == vif) {
  583. rcu_assign_pointer(ar->beacon_iter, vif_priv);
  584. rcu_read_unlock();
  585. if (old_main) {
  586. struct carl9170_vif_info *old_main_priv =
  587. (void *) old_main->drv_priv;
  588. /* downgrade old main intf to slave intf.
  589. * NOTE: We are no longer under rcu_read_lock.
  590. * But we are still holding ar->mutex, so the
  591. * vif data [id, addr] is safe.
  592. */
  593. err = carl9170_mod_virtual_mac(ar, old_main_priv->id,
  594. old_main->addr);
  595. if (err)
  596. goto unlock;
  597. }
  598. err = carl9170_init_interface(ar, vif);
  599. if (err)
  600. goto unlock;
  601. } else {
  602. rcu_read_unlock();
  603. err = carl9170_mod_virtual_mac(ar, vif_id, vif->addr);
  604. if (err)
  605. goto unlock;
  606. }
  607. if (ar->fw.tx_seq_table) {
  608. err = carl9170_write_reg(ar, ar->fw.tx_seq_table + vif_id * 4,
  609. 0);
  610. if (err)
  611. goto unlock;
  612. }
  613. unlock:
  614. if (err && (vif_id >= 0)) {
  615. vif_priv->active = false;
  616. bitmap_release_region(&ar->vif_bitmap, vif_id, 0);
  617. ar->vifs--;
  618. RCU_INIT_POINTER(ar->vif_priv[vif_id].vif, NULL);
  619. list_del_rcu(&vif_priv->list);
  620. mutex_unlock(&ar->mutex);
  621. synchronize_rcu();
  622. } else {
  623. if (ar->vifs > 1)
  624. ar->ps.off_override |= PS_OFF_VIF;
  625. mutex_unlock(&ar->mutex);
  626. }
  627. return err;
  628. }
  629. static void carl9170_op_remove_interface(struct ieee80211_hw *hw,
  630. struct ieee80211_vif *vif)
  631. {
  632. struct carl9170_vif_info *vif_priv = (void *) vif->drv_priv;
  633. struct ieee80211_vif *main_vif;
  634. struct ar9170 *ar = hw->priv;
  635. unsigned int id;
  636. mutex_lock(&ar->mutex);
  637. if (WARN_ON_ONCE(!vif_priv->active))
  638. goto unlock;
  639. ar->vifs--;
  640. rcu_read_lock();
  641. main_vif = carl9170_get_main_vif(ar);
  642. id = vif_priv->id;
  643. vif_priv->active = false;
  644. WARN_ON(vif_priv->enable_beacon);
  645. vif_priv->enable_beacon = false;
  646. list_del_rcu(&vif_priv->list);
  647. RCU_INIT_POINTER(ar->vif_priv[id].vif, NULL);
  648. if (vif == main_vif) {
  649. rcu_read_unlock();
  650. if (ar->vifs) {
  651. WARN_ON(carl9170_init_interface(ar,
  652. carl9170_get_main_vif(ar)));
  653. } else {
  654. carl9170_set_operating_mode(ar);
  655. }
  656. } else {
  657. rcu_read_unlock();
  658. WARN_ON(carl9170_mod_virtual_mac(ar, id, NULL));
  659. }
  660. carl9170_update_beacon(ar, false);
  661. carl9170_flush_cab(ar, id);
  662. spin_lock_bh(&ar->beacon_lock);
  663. dev_kfree_skb_any(vif_priv->beacon);
  664. vif_priv->beacon = NULL;
  665. spin_unlock_bh(&ar->beacon_lock);
  666. bitmap_release_region(&ar->vif_bitmap, id, 0);
  667. carl9170_set_beacon_timers(ar);
  668. if (ar->vifs == 1)
  669. ar->ps.off_override &= ~PS_OFF_VIF;
  670. unlock:
  671. mutex_unlock(&ar->mutex);
  672. synchronize_rcu();
  673. }
  674. void carl9170_ps_check(struct ar9170 *ar)
  675. {
  676. ieee80211_queue_work(ar->hw, &ar->ps_work);
  677. }
  678. /* caller must hold ar->mutex */
  679. static int carl9170_ps_update(struct ar9170 *ar)
  680. {
  681. bool ps = false;
  682. int err = 0;
  683. if (!ar->ps.off_override)
  684. ps = (ar->hw->conf.flags & IEEE80211_CONF_PS);
  685. if (ps != ar->ps.state) {
  686. err = carl9170_powersave(ar, ps);
  687. if (err)
  688. return err;
  689. if (ar->ps.state && !ps) {
  690. ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
  691. ar->ps.last_action);
  692. }
  693. if (ps)
  694. ar->ps.last_slept = jiffies;
  695. ar->ps.last_action = jiffies;
  696. ar->ps.state = ps;
  697. }
  698. return 0;
  699. }
  700. static void carl9170_ps_work(struct work_struct *work)
  701. {
  702. struct ar9170 *ar = container_of(work, struct ar9170,
  703. ps_work);
  704. mutex_lock(&ar->mutex);
  705. if (IS_STARTED(ar))
  706. WARN_ON_ONCE(carl9170_ps_update(ar) != 0);
  707. mutex_unlock(&ar->mutex);
  708. }
  709. static int carl9170_update_survey(struct ar9170 *ar, bool flush, bool noise)
  710. {
  711. int err;
  712. if (noise) {
  713. err = carl9170_get_noisefloor(ar);
  714. if (err)
  715. return err;
  716. }
  717. if (ar->fw.hw_counters) {
  718. err = carl9170_collect_tally(ar);
  719. if (err)
  720. return err;
  721. }
  722. if (flush)
  723. memset(&ar->tally, 0, sizeof(ar->tally));
  724. return 0;
  725. }
  726. static void carl9170_stat_work(struct work_struct *work)
  727. {
  728. struct ar9170 *ar = container_of(work, struct ar9170, stat_work.work);
  729. int err;
  730. mutex_lock(&ar->mutex);
  731. err = carl9170_update_survey(ar, false, true);
  732. mutex_unlock(&ar->mutex);
  733. if (err)
  734. return;
  735. ieee80211_queue_delayed_work(ar->hw, &ar->stat_work,
  736. round_jiffies(msecs_to_jiffies(CARL9170_STAT_WORK)));
  737. }
  738. static int carl9170_op_config(struct ieee80211_hw *hw, u32 changed)
  739. {
  740. struct ar9170 *ar = hw->priv;
  741. int err = 0;
  742. mutex_lock(&ar->mutex);
  743. if (changed & IEEE80211_CONF_CHANGE_LISTEN_INTERVAL) {
  744. /* TODO */
  745. err = 0;
  746. }
  747. if (changed & IEEE80211_CONF_CHANGE_PS) {
  748. err = carl9170_ps_update(ar);
  749. if (err)
  750. goto out;
  751. }
  752. if (changed & IEEE80211_CONF_CHANGE_SMPS) {
  753. /* TODO */
  754. err = 0;
  755. }
  756. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  757. enum nl80211_channel_type channel_type =
  758. cfg80211_get_chandef_type(&hw->conf.chandef);
  759. /* adjust slot time for 5 GHz */
  760. err = carl9170_set_slot_time(ar);
  761. if (err)
  762. goto out;
  763. err = carl9170_update_survey(ar, true, false);
  764. if (err)
  765. goto out;
  766. err = carl9170_set_channel(ar, hw->conf.chandef.chan,
  767. channel_type);
  768. if (err)
  769. goto out;
  770. err = carl9170_update_survey(ar, false, true);
  771. if (err)
  772. goto out;
  773. err = carl9170_set_dyn_sifs_ack(ar);
  774. if (err)
  775. goto out;
  776. err = carl9170_set_rts_cts_rate(ar);
  777. if (err)
  778. goto out;
  779. }
  780. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  781. err = carl9170_set_mac_tpc(ar, ar->hw->conf.chandef.chan);
  782. if (err)
  783. goto out;
  784. }
  785. out:
  786. mutex_unlock(&ar->mutex);
  787. return err;
  788. }
  789. static u64 carl9170_op_prepare_multicast(struct ieee80211_hw *hw,
  790. struct netdev_hw_addr_list *mc_list)
  791. {
  792. struct netdev_hw_addr *ha;
  793. u64 mchash;
  794. /* always get broadcast frames */
  795. mchash = 1ULL << (0xff >> 2);
  796. netdev_hw_addr_list_for_each(ha, mc_list)
  797. mchash |= 1ULL << (ha->addr[5] >> 2);
  798. return mchash;
  799. }
  800. static void carl9170_op_configure_filter(struct ieee80211_hw *hw,
  801. unsigned int changed_flags,
  802. unsigned int *new_flags,
  803. u64 multicast)
  804. {
  805. struct ar9170 *ar = hw->priv;
  806. /* mask supported flags */
  807. *new_flags &= FIF_ALLMULTI | ar->rx_filter_caps;
  808. if (!IS_ACCEPTING_CMD(ar))
  809. return;
  810. mutex_lock(&ar->mutex);
  811. ar->filter_state = *new_flags;
  812. /*
  813. * We can support more by setting the sniffer bit and
  814. * then checking the error flags, later.
  815. */
  816. if (*new_flags & FIF_ALLMULTI)
  817. multicast = ~0ULL;
  818. if (multicast != ar->cur_mc_hash)
  819. WARN_ON(carl9170_update_multicast(ar, multicast));
  820. if (changed_flags & FIF_OTHER_BSS) {
  821. ar->sniffer_enabled = !!(*new_flags & FIF_OTHER_BSS);
  822. WARN_ON(carl9170_set_operating_mode(ar));
  823. }
  824. if (ar->fw.rx_filter && changed_flags & ar->rx_filter_caps) {
  825. u32 rx_filter = 0;
  826. if (!ar->fw.ba_filter)
  827. rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
  828. if (!(*new_flags & (FIF_FCSFAIL | FIF_PLCPFAIL)))
  829. rx_filter |= CARL9170_RX_FILTER_BAD;
  830. if (!(*new_flags & FIF_CONTROL))
  831. rx_filter |= CARL9170_RX_FILTER_CTL_OTHER;
  832. if (!(*new_flags & FIF_PSPOLL))
  833. rx_filter |= CARL9170_RX_FILTER_CTL_PSPOLL;
  834. if (!(*new_flags & FIF_OTHER_BSS)) {
  835. rx_filter |= CARL9170_RX_FILTER_OTHER_RA;
  836. rx_filter |= CARL9170_RX_FILTER_DECRY_FAIL;
  837. }
  838. WARN_ON(carl9170_rx_filter(ar, rx_filter));
  839. }
  840. mutex_unlock(&ar->mutex);
  841. }
  842. static void carl9170_op_bss_info_changed(struct ieee80211_hw *hw,
  843. struct ieee80211_vif *vif,
  844. struct ieee80211_bss_conf *bss_conf,
  845. u32 changed)
  846. {
  847. struct ar9170 *ar = hw->priv;
  848. struct ath_common *common = &ar->common;
  849. int err = 0;
  850. struct carl9170_vif_info *vif_priv;
  851. struct ieee80211_vif *main_vif;
  852. mutex_lock(&ar->mutex);
  853. vif_priv = (void *) vif->drv_priv;
  854. main_vif = carl9170_get_main_vif(ar);
  855. if (WARN_ON(!main_vif))
  856. goto out;
  857. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  858. struct carl9170_vif_info *iter;
  859. int i = 0;
  860. vif_priv->enable_beacon = bss_conf->enable_beacon;
  861. rcu_read_lock();
  862. list_for_each_entry_rcu(iter, &ar->vif_list, list) {
  863. if (iter->active && iter->enable_beacon)
  864. i++;
  865. }
  866. rcu_read_unlock();
  867. ar->beacon_enabled = i;
  868. }
  869. if (changed & BSS_CHANGED_BEACON) {
  870. err = carl9170_update_beacon(ar, false);
  871. if (err)
  872. goto out;
  873. }
  874. if (changed & (BSS_CHANGED_BEACON_ENABLED | BSS_CHANGED_BEACON |
  875. BSS_CHANGED_BEACON_INT)) {
  876. if (main_vif != vif) {
  877. bss_conf->beacon_int = main_vif->bss_conf.beacon_int;
  878. bss_conf->dtim_period = main_vif->bss_conf.dtim_period;
  879. }
  880. /*
  881. * Therefore a hard limit for the broadcast traffic should
  882. * prevent false alarms.
  883. */
  884. if (vif->type != NL80211_IFTYPE_STATION &&
  885. (bss_conf->beacon_int * bss_conf->dtim_period >=
  886. (CARL9170_QUEUE_STUCK_TIMEOUT / 2))) {
  887. err = -EINVAL;
  888. goto out;
  889. }
  890. err = carl9170_set_beacon_timers(ar);
  891. if (err)
  892. goto out;
  893. }
  894. if (changed & BSS_CHANGED_HT) {
  895. /* TODO */
  896. err = 0;
  897. if (err)
  898. goto out;
  899. }
  900. if (main_vif != vif)
  901. goto out;
  902. /*
  903. * The following settings can only be changed by the
  904. * master interface.
  905. */
  906. if (changed & BSS_CHANGED_BSSID) {
  907. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  908. err = carl9170_set_operating_mode(ar);
  909. if (err)
  910. goto out;
  911. }
  912. if (changed & BSS_CHANGED_ASSOC) {
  913. ar->common.curaid = bss_conf->aid;
  914. err = carl9170_set_beacon_timers(ar);
  915. if (err)
  916. goto out;
  917. }
  918. if (changed & BSS_CHANGED_ERP_SLOT) {
  919. err = carl9170_set_slot_time(ar);
  920. if (err)
  921. goto out;
  922. }
  923. if (changed & BSS_CHANGED_BASIC_RATES) {
  924. err = carl9170_set_mac_rates(ar);
  925. if (err)
  926. goto out;
  927. }
  928. out:
  929. WARN_ON_ONCE(err && IS_STARTED(ar));
  930. mutex_unlock(&ar->mutex);
  931. }
  932. static u64 carl9170_op_get_tsf(struct ieee80211_hw *hw,
  933. struct ieee80211_vif *vif)
  934. {
  935. struct ar9170 *ar = hw->priv;
  936. struct carl9170_tsf_rsp tsf;
  937. int err;
  938. mutex_lock(&ar->mutex);
  939. err = carl9170_exec_cmd(ar, CARL9170_CMD_READ_TSF,
  940. 0, NULL, sizeof(tsf), &tsf);
  941. mutex_unlock(&ar->mutex);
  942. if (WARN_ON(err))
  943. return 0;
  944. return le64_to_cpu(tsf.tsf_64);
  945. }
  946. static int carl9170_op_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  947. struct ieee80211_vif *vif,
  948. struct ieee80211_sta *sta,
  949. struct ieee80211_key_conf *key)
  950. {
  951. struct ar9170 *ar = hw->priv;
  952. int err = 0, i;
  953. u8 ktype;
  954. if (ar->disable_offload || !vif)
  955. return -EOPNOTSUPP;
  956. /* Fall back to software encryption whenever the driver is connected
  957. * to more than one network.
  958. *
  959. * This is very unfortunate, because some machines cannot handle
  960. * the high througput speed in 802.11n networks.
  961. */
  962. if (!is_main_vif(ar, vif)) {
  963. mutex_lock(&ar->mutex);
  964. goto err_softw;
  965. }
  966. /*
  967. * While the hardware supports *catch-all* key, for offloading
  968. * group-key en-/de-cryption. The way of how the hardware
  969. * decides which keyId maps to which key, remains a mystery...
  970. */
  971. if ((vif->type != NL80211_IFTYPE_STATION &&
  972. vif->type != NL80211_IFTYPE_ADHOC) &&
  973. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
  974. return -EOPNOTSUPP;
  975. switch (key->cipher) {
  976. case WLAN_CIPHER_SUITE_WEP40:
  977. ktype = AR9170_ENC_ALG_WEP64;
  978. break;
  979. case WLAN_CIPHER_SUITE_WEP104:
  980. ktype = AR9170_ENC_ALG_WEP128;
  981. break;
  982. case WLAN_CIPHER_SUITE_TKIP:
  983. ktype = AR9170_ENC_ALG_TKIP;
  984. break;
  985. case WLAN_CIPHER_SUITE_CCMP:
  986. ktype = AR9170_ENC_ALG_AESCCMP;
  987. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
  988. break;
  989. default:
  990. return -EOPNOTSUPP;
  991. }
  992. mutex_lock(&ar->mutex);
  993. if (cmd == SET_KEY) {
  994. if (!IS_STARTED(ar)) {
  995. err = -EOPNOTSUPP;
  996. goto out;
  997. }
  998. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  999. sta = NULL;
  1000. i = 64 + key->keyidx;
  1001. } else {
  1002. for (i = 0; i < 64; i++)
  1003. if (!(ar->usedkeys & BIT(i)))
  1004. break;
  1005. if (i == 64)
  1006. goto err_softw;
  1007. }
  1008. key->hw_key_idx = i;
  1009. err = carl9170_upload_key(ar, i, sta ? sta->addr : NULL,
  1010. ktype, 0, key->key,
  1011. min_t(u8, 16, key->keylen));
  1012. if (err)
  1013. goto out;
  1014. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  1015. err = carl9170_upload_key(ar, i, sta ? sta->addr :
  1016. NULL, ktype, 1,
  1017. key->key + 16, 16);
  1018. if (err)
  1019. goto out;
  1020. /*
  1021. * hardware is not capable generating MMIC
  1022. * of fragmented frames!
  1023. */
  1024. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1025. }
  1026. if (i < 64)
  1027. ar->usedkeys |= BIT(i);
  1028. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1029. } else {
  1030. if (!IS_STARTED(ar)) {
  1031. /* The device is gone... together with the key ;-) */
  1032. err = 0;
  1033. goto out;
  1034. }
  1035. if (key->hw_key_idx < 64) {
  1036. ar->usedkeys &= ~BIT(key->hw_key_idx);
  1037. } else {
  1038. err = carl9170_upload_key(ar, key->hw_key_idx, NULL,
  1039. AR9170_ENC_ALG_NONE, 0,
  1040. NULL, 0);
  1041. if (err)
  1042. goto out;
  1043. if (key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  1044. err = carl9170_upload_key(ar, key->hw_key_idx,
  1045. NULL,
  1046. AR9170_ENC_ALG_NONE,
  1047. 1, NULL, 0);
  1048. if (err)
  1049. goto out;
  1050. }
  1051. }
  1052. err = carl9170_disable_key(ar, key->hw_key_idx);
  1053. if (err)
  1054. goto out;
  1055. }
  1056. out:
  1057. mutex_unlock(&ar->mutex);
  1058. return err;
  1059. err_softw:
  1060. if (!ar->rx_software_decryption) {
  1061. ar->rx_software_decryption = true;
  1062. carl9170_set_operating_mode(ar);
  1063. }
  1064. mutex_unlock(&ar->mutex);
  1065. return -ENOSPC;
  1066. }
  1067. static int carl9170_op_sta_add(struct ieee80211_hw *hw,
  1068. struct ieee80211_vif *vif,
  1069. struct ieee80211_sta *sta)
  1070. {
  1071. struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
  1072. unsigned int i;
  1073. atomic_set(&sta_info->pending_frames, 0);
  1074. if (sta->ht_cap.ht_supported) {
  1075. if (sta->ht_cap.ampdu_density > 6) {
  1076. /*
  1077. * HW does support 16us AMPDU density.
  1078. * No HT-Xmit for station.
  1079. */
  1080. return 0;
  1081. }
  1082. for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++)
  1083. RCU_INIT_POINTER(sta_info->agg[i], NULL);
  1084. sta_info->ampdu_max_len = 1 << (3 + sta->ht_cap.ampdu_factor);
  1085. sta_info->ht_sta = true;
  1086. }
  1087. return 0;
  1088. }
  1089. static int carl9170_op_sta_remove(struct ieee80211_hw *hw,
  1090. struct ieee80211_vif *vif,
  1091. struct ieee80211_sta *sta)
  1092. {
  1093. struct ar9170 *ar = hw->priv;
  1094. struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
  1095. unsigned int i;
  1096. bool cleanup = false;
  1097. if (sta->ht_cap.ht_supported) {
  1098. sta_info->ht_sta = false;
  1099. rcu_read_lock();
  1100. for (i = 0; i < ARRAY_SIZE(sta_info->agg); i++) {
  1101. struct carl9170_sta_tid *tid_info;
  1102. tid_info = rcu_dereference(sta_info->agg[i]);
  1103. RCU_INIT_POINTER(sta_info->agg[i], NULL);
  1104. if (!tid_info)
  1105. continue;
  1106. spin_lock_bh(&ar->tx_ampdu_list_lock);
  1107. if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
  1108. tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
  1109. spin_unlock_bh(&ar->tx_ampdu_list_lock);
  1110. cleanup = true;
  1111. }
  1112. rcu_read_unlock();
  1113. if (cleanup)
  1114. carl9170_ampdu_gc(ar);
  1115. }
  1116. return 0;
  1117. }
  1118. static int carl9170_op_conf_tx(struct ieee80211_hw *hw,
  1119. struct ieee80211_vif *vif, u16 queue,
  1120. const struct ieee80211_tx_queue_params *param)
  1121. {
  1122. struct ar9170 *ar = hw->priv;
  1123. int ret;
  1124. mutex_lock(&ar->mutex);
  1125. if (queue < ar->hw->queues) {
  1126. memcpy(&ar->edcf[ar9170_qmap[queue]], param, sizeof(*param));
  1127. ret = carl9170_set_qos(ar);
  1128. } else {
  1129. ret = -EINVAL;
  1130. }
  1131. mutex_unlock(&ar->mutex);
  1132. return ret;
  1133. }
  1134. static void carl9170_ampdu_work(struct work_struct *work)
  1135. {
  1136. struct ar9170 *ar = container_of(work, struct ar9170,
  1137. ampdu_work);
  1138. if (!IS_STARTED(ar))
  1139. return;
  1140. mutex_lock(&ar->mutex);
  1141. carl9170_ampdu_gc(ar);
  1142. mutex_unlock(&ar->mutex);
  1143. }
  1144. static int carl9170_op_ampdu_action(struct ieee80211_hw *hw,
  1145. struct ieee80211_vif *vif,
  1146. struct ieee80211_ampdu_params *params)
  1147. {
  1148. struct ieee80211_sta *sta = params->sta;
  1149. enum ieee80211_ampdu_mlme_action action = params->action;
  1150. u16 tid = params->tid;
  1151. u16 *ssn = &params->ssn;
  1152. struct ar9170 *ar = hw->priv;
  1153. struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
  1154. struct carl9170_sta_tid *tid_info;
  1155. if (modparam_noht)
  1156. return -EOPNOTSUPP;
  1157. switch (action) {
  1158. case IEEE80211_AMPDU_TX_START:
  1159. if (!sta_info->ht_sta)
  1160. return -EOPNOTSUPP;
  1161. tid_info = kzalloc(sizeof(struct carl9170_sta_tid),
  1162. GFP_ATOMIC);
  1163. if (!tid_info)
  1164. return -ENOMEM;
  1165. tid_info->hsn = tid_info->bsn = tid_info->snx = (*ssn);
  1166. tid_info->state = CARL9170_TID_STATE_PROGRESS;
  1167. tid_info->tid = tid;
  1168. tid_info->max = sta_info->ampdu_max_len;
  1169. tid_info->sta = sta;
  1170. tid_info->vif = vif;
  1171. INIT_LIST_HEAD(&tid_info->list);
  1172. INIT_LIST_HEAD(&tid_info->tmp_list);
  1173. skb_queue_head_init(&tid_info->queue);
  1174. spin_lock_init(&tid_info->lock);
  1175. spin_lock_bh(&ar->tx_ampdu_list_lock);
  1176. ar->tx_ampdu_list_len++;
  1177. list_add_tail_rcu(&tid_info->list, &ar->tx_ampdu_list);
  1178. rcu_assign_pointer(sta_info->agg[tid], tid_info);
  1179. spin_unlock_bh(&ar->tx_ampdu_list_lock);
  1180. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1181. break;
  1182. case IEEE80211_AMPDU_TX_STOP_CONT:
  1183. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  1184. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  1185. rcu_read_lock();
  1186. tid_info = rcu_dereference(sta_info->agg[tid]);
  1187. if (tid_info) {
  1188. spin_lock_bh(&ar->tx_ampdu_list_lock);
  1189. if (tid_info->state > CARL9170_TID_STATE_SHUTDOWN)
  1190. tid_info->state = CARL9170_TID_STATE_SHUTDOWN;
  1191. spin_unlock_bh(&ar->tx_ampdu_list_lock);
  1192. }
  1193. RCU_INIT_POINTER(sta_info->agg[tid], NULL);
  1194. rcu_read_unlock();
  1195. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1196. ieee80211_queue_work(ar->hw, &ar->ampdu_work);
  1197. break;
  1198. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1199. rcu_read_lock();
  1200. tid_info = rcu_dereference(sta_info->agg[tid]);
  1201. sta_info->stats[tid].clear = true;
  1202. sta_info->stats[tid].req = false;
  1203. if (tid_info) {
  1204. bitmap_zero(tid_info->bitmap, CARL9170_BAW_SIZE);
  1205. tid_info->state = CARL9170_TID_STATE_IDLE;
  1206. }
  1207. rcu_read_unlock();
  1208. if (WARN_ON_ONCE(!tid_info))
  1209. return -EFAULT;
  1210. break;
  1211. case IEEE80211_AMPDU_RX_START:
  1212. case IEEE80211_AMPDU_RX_STOP:
  1213. /* Handled by hardware */
  1214. break;
  1215. default:
  1216. return -EOPNOTSUPP;
  1217. }
  1218. return 0;
  1219. }
  1220. #ifdef CONFIG_CARL9170_WPC
  1221. static int carl9170_register_wps_button(struct ar9170 *ar)
  1222. {
  1223. struct input_dev *input;
  1224. int err;
  1225. if (!(ar->features & CARL9170_WPS_BUTTON))
  1226. return 0;
  1227. input = input_allocate_device();
  1228. if (!input)
  1229. return -ENOMEM;
  1230. snprintf(ar->wps.name, sizeof(ar->wps.name), "%s WPS Button",
  1231. wiphy_name(ar->hw->wiphy));
  1232. snprintf(ar->wps.phys, sizeof(ar->wps.phys),
  1233. "ieee80211/%s/input0", wiphy_name(ar->hw->wiphy));
  1234. input->name = ar->wps.name;
  1235. input->phys = ar->wps.phys;
  1236. input->id.bustype = BUS_USB;
  1237. input->dev.parent = &ar->hw->wiphy->dev;
  1238. input_set_capability(input, EV_KEY, KEY_WPS_BUTTON);
  1239. err = input_register_device(input);
  1240. if (err) {
  1241. input_free_device(input);
  1242. return err;
  1243. }
  1244. ar->wps.pbc = input;
  1245. return 0;
  1246. }
  1247. #endif /* CONFIG_CARL9170_WPC */
  1248. #ifdef CONFIG_CARL9170_HWRNG
  1249. static int carl9170_rng_get(struct ar9170 *ar)
  1250. {
  1251. #define RW (CARL9170_MAX_CMD_PAYLOAD_LEN / sizeof(u32))
  1252. #define RB (CARL9170_MAX_CMD_PAYLOAD_LEN)
  1253. static const __le32 rng_load[RW] = {
  1254. [0 ... (RW - 1)] = cpu_to_le32(AR9170_RAND_REG_NUM)};
  1255. u32 buf[RW];
  1256. unsigned int i, off = 0, transfer, count;
  1257. int err;
  1258. BUILD_BUG_ON(RB > CARL9170_MAX_CMD_PAYLOAD_LEN);
  1259. if (!IS_ACCEPTING_CMD(ar) || !ar->rng.initialized)
  1260. return -EAGAIN;
  1261. count = ARRAY_SIZE(ar->rng.cache);
  1262. while (count) {
  1263. err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
  1264. RB, (u8 *) rng_load,
  1265. RB, (u8 *) buf);
  1266. if (err)
  1267. return err;
  1268. transfer = min_t(unsigned int, count, RW);
  1269. for (i = 0; i < transfer; i++)
  1270. ar->rng.cache[off + i] = buf[i];
  1271. off += transfer;
  1272. count -= transfer;
  1273. }
  1274. ar->rng.cache_idx = 0;
  1275. #undef RW
  1276. #undef RB
  1277. return 0;
  1278. }
  1279. static int carl9170_rng_read(struct hwrng *rng, u32 *data)
  1280. {
  1281. struct ar9170 *ar = (struct ar9170 *)rng->priv;
  1282. int ret = -EIO;
  1283. mutex_lock(&ar->mutex);
  1284. if (ar->rng.cache_idx >= ARRAY_SIZE(ar->rng.cache)) {
  1285. ret = carl9170_rng_get(ar);
  1286. if (ret) {
  1287. mutex_unlock(&ar->mutex);
  1288. return ret;
  1289. }
  1290. }
  1291. *data = ar->rng.cache[ar->rng.cache_idx++];
  1292. mutex_unlock(&ar->mutex);
  1293. return sizeof(u16);
  1294. }
  1295. static void carl9170_unregister_hwrng(struct ar9170 *ar)
  1296. {
  1297. if (ar->rng.initialized) {
  1298. hwrng_unregister(&ar->rng.rng);
  1299. ar->rng.initialized = false;
  1300. }
  1301. }
  1302. static int carl9170_register_hwrng(struct ar9170 *ar)
  1303. {
  1304. int err;
  1305. snprintf(ar->rng.name, ARRAY_SIZE(ar->rng.name),
  1306. "%s_%s", KBUILD_MODNAME, wiphy_name(ar->hw->wiphy));
  1307. ar->rng.rng.name = ar->rng.name;
  1308. ar->rng.rng.data_read = carl9170_rng_read;
  1309. ar->rng.rng.priv = (unsigned long)ar;
  1310. if (WARN_ON(ar->rng.initialized))
  1311. return -EALREADY;
  1312. err = hwrng_register(&ar->rng.rng);
  1313. if (err) {
  1314. dev_err(&ar->udev->dev, "Failed to register the random "
  1315. "number generator (%d)\n", err);
  1316. return err;
  1317. }
  1318. ar->rng.initialized = true;
  1319. err = carl9170_rng_get(ar);
  1320. if (err) {
  1321. carl9170_unregister_hwrng(ar);
  1322. return err;
  1323. }
  1324. return 0;
  1325. }
  1326. #endif /* CONFIG_CARL9170_HWRNG */
  1327. static int carl9170_op_get_survey(struct ieee80211_hw *hw, int idx,
  1328. struct survey_info *survey)
  1329. {
  1330. struct ar9170 *ar = hw->priv;
  1331. struct ieee80211_channel *chan;
  1332. struct ieee80211_supported_band *band;
  1333. int err, b, i;
  1334. chan = ar->channel;
  1335. if (!chan)
  1336. return -ENODEV;
  1337. if (idx == chan->hw_value) {
  1338. mutex_lock(&ar->mutex);
  1339. err = carl9170_update_survey(ar, false, true);
  1340. mutex_unlock(&ar->mutex);
  1341. if (err)
  1342. return err;
  1343. }
  1344. for (b = 0; b < NUM_NL80211_BANDS; b++) {
  1345. band = ar->hw->wiphy->bands[b];
  1346. if (!band)
  1347. continue;
  1348. for (i = 0; i < band->n_channels; i++) {
  1349. if (band->channels[i].hw_value == idx) {
  1350. chan = &band->channels[i];
  1351. goto found;
  1352. }
  1353. }
  1354. }
  1355. return -ENOENT;
  1356. found:
  1357. memcpy(survey, &ar->survey[idx], sizeof(*survey));
  1358. survey->channel = chan;
  1359. survey->filled = SURVEY_INFO_NOISE_DBM;
  1360. if (ar->channel == chan)
  1361. survey->filled |= SURVEY_INFO_IN_USE;
  1362. if (ar->fw.hw_counters) {
  1363. survey->filled |= SURVEY_INFO_TIME |
  1364. SURVEY_INFO_TIME_BUSY |
  1365. SURVEY_INFO_TIME_TX;
  1366. }
  1367. return 0;
  1368. }
  1369. static void carl9170_op_flush(struct ieee80211_hw *hw,
  1370. struct ieee80211_vif *vif,
  1371. u32 queues, bool drop)
  1372. {
  1373. struct ar9170 *ar = hw->priv;
  1374. unsigned int vid;
  1375. mutex_lock(&ar->mutex);
  1376. for_each_set_bit(vid, &ar->vif_bitmap, ar->fw.vif_num)
  1377. carl9170_flush_cab(ar, vid);
  1378. carl9170_flush(ar, drop);
  1379. mutex_unlock(&ar->mutex);
  1380. }
  1381. static int carl9170_op_get_stats(struct ieee80211_hw *hw,
  1382. struct ieee80211_low_level_stats *stats)
  1383. {
  1384. struct ar9170 *ar = hw->priv;
  1385. memset(stats, 0, sizeof(*stats));
  1386. stats->dot11ACKFailureCount = ar->tx_ack_failures;
  1387. stats->dot11FCSErrorCount = ar->tx_fcs_errors;
  1388. return 0;
  1389. }
  1390. static void carl9170_op_sta_notify(struct ieee80211_hw *hw,
  1391. struct ieee80211_vif *vif,
  1392. enum sta_notify_cmd cmd,
  1393. struct ieee80211_sta *sta)
  1394. {
  1395. struct carl9170_sta_info *sta_info = (void *) sta->drv_priv;
  1396. switch (cmd) {
  1397. case STA_NOTIFY_SLEEP:
  1398. sta_info->sleeping = true;
  1399. if (atomic_read(&sta_info->pending_frames))
  1400. ieee80211_sta_block_awake(hw, sta, true);
  1401. break;
  1402. case STA_NOTIFY_AWAKE:
  1403. sta_info->sleeping = false;
  1404. break;
  1405. }
  1406. }
  1407. static bool carl9170_tx_frames_pending(struct ieee80211_hw *hw)
  1408. {
  1409. struct ar9170 *ar = hw->priv;
  1410. return !!atomic_read(&ar->tx_total_queued);
  1411. }
  1412. static const struct ieee80211_ops carl9170_ops = {
  1413. .start = carl9170_op_start,
  1414. .stop = carl9170_op_stop,
  1415. .tx = carl9170_op_tx,
  1416. .flush = carl9170_op_flush,
  1417. .add_interface = carl9170_op_add_interface,
  1418. .remove_interface = carl9170_op_remove_interface,
  1419. .config = carl9170_op_config,
  1420. .prepare_multicast = carl9170_op_prepare_multicast,
  1421. .configure_filter = carl9170_op_configure_filter,
  1422. .conf_tx = carl9170_op_conf_tx,
  1423. .bss_info_changed = carl9170_op_bss_info_changed,
  1424. .get_tsf = carl9170_op_get_tsf,
  1425. .set_key = carl9170_op_set_key,
  1426. .sta_add = carl9170_op_sta_add,
  1427. .sta_remove = carl9170_op_sta_remove,
  1428. .sta_notify = carl9170_op_sta_notify,
  1429. .get_survey = carl9170_op_get_survey,
  1430. .get_stats = carl9170_op_get_stats,
  1431. .ampdu_action = carl9170_op_ampdu_action,
  1432. .tx_frames_pending = carl9170_tx_frames_pending,
  1433. };
  1434. void *carl9170_alloc(size_t priv_size)
  1435. {
  1436. struct ieee80211_hw *hw;
  1437. struct ar9170 *ar;
  1438. struct sk_buff *skb;
  1439. int i;
  1440. /*
  1441. * this buffer is used for rx stream reconstruction.
  1442. * Under heavy load this device (or the transport layer?)
  1443. * tends to split the streams into separate rx descriptors.
  1444. */
  1445. skb = __dev_alloc_skb(AR9170_RX_STREAM_MAX_SIZE, GFP_KERNEL);
  1446. if (!skb)
  1447. goto err_nomem;
  1448. hw = ieee80211_alloc_hw(priv_size, &carl9170_ops);
  1449. if (!hw)
  1450. goto err_nomem;
  1451. ar = hw->priv;
  1452. ar->hw = hw;
  1453. ar->rx_failover = skb;
  1454. memset(&ar->rx_plcp, 0, sizeof(struct ar9170_rx_head));
  1455. ar->rx_has_plcp = false;
  1456. /*
  1457. * Here's a hidden pitfall!
  1458. *
  1459. * All 4 AC queues work perfectly well under _legacy_ operation.
  1460. * However as soon as aggregation is enabled, the traffic flow
  1461. * gets very bumpy. Therefore we have to _switch_ to a
  1462. * software AC with a single HW queue.
  1463. */
  1464. hw->queues = __AR9170_NUM_TXQ;
  1465. mutex_init(&ar->mutex);
  1466. spin_lock_init(&ar->beacon_lock);
  1467. spin_lock_init(&ar->cmd_lock);
  1468. spin_lock_init(&ar->tx_stats_lock);
  1469. spin_lock_init(&ar->tx_ampdu_list_lock);
  1470. spin_lock_init(&ar->mem_lock);
  1471. spin_lock_init(&ar->state_lock);
  1472. atomic_set(&ar->pending_restarts, 0);
  1473. ar->vifs = 0;
  1474. for (i = 0; i < ar->hw->queues; i++) {
  1475. skb_queue_head_init(&ar->tx_status[i]);
  1476. skb_queue_head_init(&ar->tx_pending[i]);
  1477. INIT_LIST_HEAD(&ar->bar_list[i]);
  1478. spin_lock_init(&ar->bar_list_lock[i]);
  1479. }
  1480. INIT_WORK(&ar->ps_work, carl9170_ps_work);
  1481. INIT_WORK(&ar->ping_work, carl9170_ping_work);
  1482. INIT_WORK(&ar->restart_work, carl9170_restart_work);
  1483. INIT_WORK(&ar->ampdu_work, carl9170_ampdu_work);
  1484. INIT_DELAYED_WORK(&ar->stat_work, carl9170_stat_work);
  1485. INIT_DELAYED_WORK(&ar->tx_janitor, carl9170_tx_janitor);
  1486. INIT_LIST_HEAD(&ar->tx_ampdu_list);
  1487. rcu_assign_pointer(ar->tx_ampdu_iter,
  1488. (struct carl9170_sta_tid *) &ar->tx_ampdu_list);
  1489. bitmap_zero(&ar->vif_bitmap, ar->fw.vif_num);
  1490. INIT_LIST_HEAD(&ar->vif_list);
  1491. init_completion(&ar->tx_flush);
  1492. /* firmware decides which modes we support */
  1493. hw->wiphy->interface_modes = 0;
  1494. ieee80211_hw_set(hw, RX_INCLUDES_FCS);
  1495. ieee80211_hw_set(hw, MFP_CAPABLE);
  1496. ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
  1497. ieee80211_hw_set(hw, SUPPORTS_PS);
  1498. ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
  1499. ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
  1500. ieee80211_hw_set(hw, SUPPORTS_RC_TABLE);
  1501. ieee80211_hw_set(hw, SIGNAL_DBM);
  1502. ieee80211_hw_set(hw, SUPPORTS_HT_CCK_RATES);
  1503. if (!modparam_noht) {
  1504. /*
  1505. * see the comment above, why we allow the user
  1506. * to disable HT by a module parameter.
  1507. */
  1508. ieee80211_hw_set(hw, AMPDU_AGGREGATION);
  1509. }
  1510. hw->extra_tx_headroom = sizeof(struct _carl9170_tx_superframe);
  1511. hw->sta_data_size = sizeof(struct carl9170_sta_info);
  1512. hw->vif_data_size = sizeof(struct carl9170_vif_info);
  1513. hw->max_rates = CARL9170_TX_MAX_RATES;
  1514. hw->max_rate_tries = CARL9170_TX_USER_RATE_TRIES;
  1515. for (i = 0; i < ARRAY_SIZE(ar->noise); i++)
  1516. ar->noise[i] = -95; /* ATH_DEFAULT_NOISE_FLOOR */
  1517. wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  1518. return ar;
  1519. err_nomem:
  1520. kfree_skb(skb);
  1521. return ERR_PTR(-ENOMEM);
  1522. }
  1523. static int carl9170_read_eeprom(struct ar9170 *ar)
  1524. {
  1525. #define RW 8 /* number of words to read at once */
  1526. #define RB (sizeof(u32) * RW)
  1527. u8 *eeprom = (void *)&ar->eeprom;
  1528. __le32 offsets[RW];
  1529. int i, j, err;
  1530. BUILD_BUG_ON(sizeof(ar->eeprom) & 3);
  1531. BUILD_BUG_ON(RB > CARL9170_MAX_CMD_LEN - 4);
  1532. #ifndef __CHECKER__
  1533. /* don't want to handle trailing remains */
  1534. BUILD_BUG_ON(sizeof(ar->eeprom) % RB);
  1535. #endif
  1536. for (i = 0; i < sizeof(ar->eeprom) / RB; i++) {
  1537. for (j = 0; j < RW; j++)
  1538. offsets[j] = cpu_to_le32(AR9170_EEPROM_START +
  1539. RB * i + 4 * j);
  1540. err = carl9170_exec_cmd(ar, CARL9170_CMD_RREG,
  1541. RB, (u8 *) &offsets,
  1542. RB, eeprom + RB * i);
  1543. if (err)
  1544. return err;
  1545. }
  1546. #undef RW
  1547. #undef RB
  1548. return 0;
  1549. }
  1550. static int carl9170_parse_eeprom(struct ar9170 *ar)
  1551. {
  1552. struct ath_regulatory *regulatory = &ar->common.regulatory;
  1553. unsigned int rx_streams, tx_streams, tx_params = 0;
  1554. int bands = 0;
  1555. int chans = 0;
  1556. if (ar->eeprom.length == cpu_to_le16(0xffff))
  1557. return -ENODATA;
  1558. rx_streams = hweight8(ar->eeprom.rx_mask);
  1559. tx_streams = hweight8(ar->eeprom.tx_mask);
  1560. if (rx_streams != tx_streams) {
  1561. tx_params = IEEE80211_HT_MCS_TX_RX_DIFF;
  1562. WARN_ON(!(tx_streams >= 1 && tx_streams <=
  1563. IEEE80211_HT_MCS_TX_MAX_STREAMS));
  1564. tx_params = (tx_streams - 1) <<
  1565. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  1566. carl9170_band_2GHz.ht_cap.mcs.tx_params |= tx_params;
  1567. carl9170_band_5GHz.ht_cap.mcs.tx_params |= tx_params;
  1568. }
  1569. if (ar->eeprom.operating_flags & AR9170_OPFLAG_2GHZ) {
  1570. ar->hw->wiphy->bands[NL80211_BAND_2GHZ] =
  1571. &carl9170_band_2GHz;
  1572. chans += carl9170_band_2GHz.n_channels;
  1573. bands++;
  1574. }
  1575. if (ar->eeprom.operating_flags & AR9170_OPFLAG_5GHZ) {
  1576. ar->hw->wiphy->bands[NL80211_BAND_5GHZ] =
  1577. &carl9170_band_5GHz;
  1578. chans += carl9170_band_5GHz.n_channels;
  1579. bands++;
  1580. }
  1581. if (!bands)
  1582. return -EINVAL;
  1583. ar->survey = kcalloc(chans, sizeof(struct survey_info), GFP_KERNEL);
  1584. if (!ar->survey)
  1585. return -ENOMEM;
  1586. ar->num_channels = chans;
  1587. regulatory->current_rd = le16_to_cpu(ar->eeprom.reg_domain[0]);
  1588. /* second part of wiphy init */
  1589. SET_IEEE80211_PERM_ADDR(ar->hw, ar->eeprom.mac_address);
  1590. return 0;
  1591. }
  1592. static void carl9170_reg_notifier(struct wiphy *wiphy,
  1593. struct regulatory_request *request)
  1594. {
  1595. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  1596. struct ar9170 *ar = hw->priv;
  1597. ath_reg_notifier_apply(wiphy, request, &ar->common.regulatory);
  1598. }
  1599. int carl9170_register(struct ar9170 *ar)
  1600. {
  1601. struct ath_regulatory *regulatory = &ar->common.regulatory;
  1602. int err = 0, i;
  1603. if (WARN_ON(ar->mem_bitmap))
  1604. return -EINVAL;
  1605. ar->mem_bitmap = kcalloc(roundup(ar->fw.mem_blocks, BITS_PER_LONG),
  1606. sizeof(unsigned long),
  1607. GFP_KERNEL);
  1608. if (!ar->mem_bitmap)
  1609. return -ENOMEM;
  1610. /* try to read EEPROM, init MAC addr */
  1611. err = carl9170_read_eeprom(ar);
  1612. if (err)
  1613. return err;
  1614. err = carl9170_parse_eeprom(ar);
  1615. if (err)
  1616. return err;
  1617. err = ath_regd_init(regulatory, ar->hw->wiphy,
  1618. carl9170_reg_notifier);
  1619. if (err)
  1620. return err;
  1621. if (modparam_noht) {
  1622. carl9170_band_2GHz.ht_cap.ht_supported = false;
  1623. carl9170_band_5GHz.ht_cap.ht_supported = false;
  1624. }
  1625. for (i = 0; i < ar->fw.vif_num; i++) {
  1626. ar->vif_priv[i].id = i;
  1627. ar->vif_priv[i].vif = NULL;
  1628. }
  1629. err = ieee80211_register_hw(ar->hw);
  1630. if (err)
  1631. return err;
  1632. /* mac80211 interface is now registered */
  1633. ar->registered = true;
  1634. if (!ath_is_world_regd(regulatory))
  1635. regulatory_hint(ar->hw->wiphy, regulatory->alpha2);
  1636. #ifdef CONFIG_CARL9170_DEBUGFS
  1637. carl9170_debugfs_register(ar);
  1638. #endif /* CONFIG_CARL9170_DEBUGFS */
  1639. err = carl9170_led_init(ar);
  1640. if (err)
  1641. goto err_unreg;
  1642. #ifdef CONFIG_CARL9170_LEDS
  1643. err = carl9170_led_register(ar);
  1644. if (err)
  1645. goto err_unreg;
  1646. #endif /* CONFIG_CARL9170_LEDS */
  1647. #ifdef CONFIG_CARL9170_WPC
  1648. err = carl9170_register_wps_button(ar);
  1649. if (err)
  1650. goto err_unreg;
  1651. #endif /* CONFIG_CARL9170_WPC */
  1652. #ifdef CONFIG_CARL9170_HWRNG
  1653. err = carl9170_register_hwrng(ar);
  1654. if (err)
  1655. goto err_unreg;
  1656. #endif /* CONFIG_CARL9170_HWRNG */
  1657. dev_info(&ar->udev->dev, "Atheros AR9170 is registered as '%s'\n",
  1658. wiphy_name(ar->hw->wiphy));
  1659. return 0;
  1660. err_unreg:
  1661. carl9170_unregister(ar);
  1662. return err;
  1663. }
  1664. void carl9170_unregister(struct ar9170 *ar)
  1665. {
  1666. if (!ar->registered)
  1667. return;
  1668. ar->registered = false;
  1669. #ifdef CONFIG_CARL9170_LEDS
  1670. carl9170_led_unregister(ar);
  1671. #endif /* CONFIG_CARL9170_LEDS */
  1672. #ifdef CONFIG_CARL9170_DEBUGFS
  1673. carl9170_debugfs_unregister(ar);
  1674. #endif /* CONFIG_CARL9170_DEBUGFS */
  1675. #ifdef CONFIG_CARL9170_WPC
  1676. if (ar->wps.pbc) {
  1677. input_unregister_device(ar->wps.pbc);
  1678. ar->wps.pbc = NULL;
  1679. }
  1680. #endif /* CONFIG_CARL9170_WPC */
  1681. #ifdef CONFIG_CARL9170_HWRNG
  1682. carl9170_unregister_hwrng(ar);
  1683. #endif /* CONFIG_CARL9170_HWRNG */
  1684. carl9170_cancel_worker(ar);
  1685. cancel_work_sync(&ar->restart_work);
  1686. ieee80211_unregister_hw(ar->hw);
  1687. }
  1688. void carl9170_free(struct ar9170 *ar)
  1689. {
  1690. WARN_ON(ar->registered);
  1691. WARN_ON(IS_INITIALIZED(ar));
  1692. kfree_skb(ar->rx_failover);
  1693. ar->rx_failover = NULL;
  1694. kfree(ar->mem_bitmap);
  1695. ar->mem_bitmap = NULL;
  1696. kfree(ar->survey);
  1697. ar->survey = NULL;
  1698. mutex_destroy(&ar->mutex);
  1699. ieee80211_free_hw(ar->hw);
  1700. }