main.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215
  1. /*
  2. * Copyright (c) 2008-2009 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/nl80211.h>
  17. #include "ath9k.h"
  18. #include "btcoex.h"
  19. static void ath_update_txpow(struct ath_softc *sc)
  20. {
  21. struct ath_hw *ah = sc->sc_ah;
  22. if (sc->curtxpow != sc->config.txpowlimit) {
  23. ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit);
  24. /* read back in case value is clamped */
  25. sc->curtxpow = ath9k_hw_regulatory(ah)->power_limit;
  26. }
  27. }
  28. static u8 parse_mpdudensity(u8 mpdudensity)
  29. {
  30. /*
  31. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  32. * 0 for no restriction
  33. * 1 for 1/4 us
  34. * 2 for 1/2 us
  35. * 3 for 1 us
  36. * 4 for 2 us
  37. * 5 for 4 us
  38. * 6 for 8 us
  39. * 7 for 16 us
  40. */
  41. switch (mpdudensity) {
  42. case 0:
  43. return 0;
  44. case 1:
  45. case 2:
  46. case 3:
  47. /* Our lower layer calculations limit our precision to
  48. 1 microsecond */
  49. return 1;
  50. case 4:
  51. return 2;
  52. case 5:
  53. return 4;
  54. case 6:
  55. return 8;
  56. case 7:
  57. return 16;
  58. default:
  59. return 0;
  60. }
  61. }
  62. static struct ath9k_channel *ath_get_curchannel(struct ath_softc *sc,
  63. struct ieee80211_hw *hw)
  64. {
  65. struct ieee80211_channel *curchan = hw->conf.channel;
  66. struct ath9k_channel *channel;
  67. u8 chan_idx;
  68. chan_idx = curchan->hw_value;
  69. channel = &sc->sc_ah->channels[chan_idx];
  70. ath9k_update_ichannel(sc, hw, channel);
  71. return channel;
  72. }
  73. bool ath9k_setpower(struct ath_softc *sc, enum ath9k_power_mode mode)
  74. {
  75. unsigned long flags;
  76. bool ret;
  77. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  78. ret = ath9k_hw_setpower(sc->sc_ah, mode);
  79. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  80. return ret;
  81. }
  82. void ath9k_ps_wakeup(struct ath_softc *sc)
  83. {
  84. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  85. unsigned long flags;
  86. enum ath9k_power_mode power_mode;
  87. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  88. if (++sc->ps_usecount != 1)
  89. goto unlock;
  90. power_mode = sc->sc_ah->power_mode;
  91. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  92. /*
  93. * While the hardware is asleep, the cycle counters contain no
  94. * useful data. Better clear them now so that they don't mess up
  95. * survey data results.
  96. */
  97. if (power_mode != ATH9K_PM_AWAKE) {
  98. spin_lock(&common->cc_lock);
  99. ath_hw_cycle_counters_update(common);
  100. memset(&common->cc_survey, 0, sizeof(common->cc_survey));
  101. spin_unlock(&common->cc_lock);
  102. }
  103. unlock:
  104. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  105. }
  106. void ath9k_ps_restore(struct ath_softc *sc)
  107. {
  108. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  109. unsigned long flags;
  110. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  111. if (--sc->ps_usecount != 0)
  112. goto unlock;
  113. spin_lock(&common->cc_lock);
  114. ath_hw_cycle_counters_update(common);
  115. spin_unlock(&common->cc_lock);
  116. if (sc->ps_idle)
  117. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_FULL_SLEEP);
  118. else if (sc->ps_enabled &&
  119. !(sc->ps_flags & (PS_WAIT_FOR_BEACON |
  120. PS_WAIT_FOR_CAB |
  121. PS_WAIT_FOR_PSPOLL_DATA |
  122. PS_WAIT_FOR_TX_ACK)))
  123. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  124. unlock:
  125. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  126. }
  127. static void ath_start_ani(struct ath_common *common)
  128. {
  129. struct ath_hw *ah = common->ah;
  130. unsigned long timestamp = jiffies_to_msecs(jiffies);
  131. struct ath_softc *sc = (struct ath_softc *) common->priv;
  132. if (!(sc->sc_flags & SC_OP_ANI_RUN))
  133. return;
  134. if (sc->sc_flags & SC_OP_OFFCHANNEL)
  135. return;
  136. common->ani.longcal_timer = timestamp;
  137. common->ani.shortcal_timer = timestamp;
  138. common->ani.checkani_timer = timestamp;
  139. mod_timer(&common->ani.timer,
  140. jiffies +
  141. msecs_to_jiffies((u32)ah->config.ani_poll_interval));
  142. }
  143. static void ath_update_survey_nf(struct ath_softc *sc, int channel)
  144. {
  145. struct ath_hw *ah = sc->sc_ah;
  146. struct ath9k_channel *chan = &ah->channels[channel];
  147. struct survey_info *survey = &sc->survey[channel];
  148. if (chan->noisefloor) {
  149. survey->filled |= SURVEY_INFO_NOISE_DBM;
  150. survey->noise = chan->noisefloor;
  151. }
  152. }
  153. static void ath_update_survey_stats(struct ath_softc *sc)
  154. {
  155. struct ath_hw *ah = sc->sc_ah;
  156. struct ath_common *common = ath9k_hw_common(ah);
  157. int pos = ah->curchan - &ah->channels[0];
  158. struct survey_info *survey = &sc->survey[pos];
  159. struct ath_cycle_counters *cc = &common->cc_survey;
  160. unsigned int div = common->clockrate * 1000;
  161. if (!ah->curchan)
  162. return;
  163. if (ah->power_mode == ATH9K_PM_AWAKE)
  164. ath_hw_cycle_counters_update(common);
  165. if (cc->cycles > 0) {
  166. survey->filled |= SURVEY_INFO_CHANNEL_TIME |
  167. SURVEY_INFO_CHANNEL_TIME_BUSY |
  168. SURVEY_INFO_CHANNEL_TIME_RX |
  169. SURVEY_INFO_CHANNEL_TIME_TX;
  170. survey->channel_time += cc->cycles / div;
  171. survey->channel_time_busy += cc->rx_busy / div;
  172. survey->channel_time_rx += cc->rx_frame / div;
  173. survey->channel_time_tx += cc->tx_frame / div;
  174. }
  175. memset(cc, 0, sizeof(*cc));
  176. ath_update_survey_nf(sc, pos);
  177. }
  178. /*
  179. * Set/change channels. If the channel is really being changed, it's done
  180. * by reseting the chip. To accomplish this we must first cleanup any pending
  181. * DMA, then restart stuff.
  182. */
  183. int ath_set_channel(struct ath_softc *sc, struct ieee80211_hw *hw,
  184. struct ath9k_channel *hchan)
  185. {
  186. struct ath_wiphy *aphy = hw->priv;
  187. struct ath_hw *ah = sc->sc_ah;
  188. struct ath_common *common = ath9k_hw_common(ah);
  189. struct ieee80211_conf *conf = &common->hw->conf;
  190. bool fastcc = true, stopped;
  191. struct ieee80211_channel *channel = hw->conf.channel;
  192. struct ath9k_hw_cal_data *caldata = NULL;
  193. int r;
  194. if (sc->sc_flags & SC_OP_INVALID)
  195. return -EIO;
  196. del_timer_sync(&common->ani.timer);
  197. cancel_work_sync(&sc->paprd_work);
  198. cancel_work_sync(&sc->hw_check_work);
  199. cancel_delayed_work_sync(&sc->tx_complete_work);
  200. ath9k_ps_wakeup(sc);
  201. /*
  202. * This is only performed if the channel settings have
  203. * actually changed.
  204. *
  205. * To switch channels clear any pending DMA operations;
  206. * wait long enough for the RX fifo to drain, reset the
  207. * hardware at the new frequency, and then re-enable
  208. * the relevant bits of the h/w.
  209. */
  210. ath9k_hw_set_interrupts(ah, 0);
  211. stopped = ath_drain_all_txq(sc, false);
  212. spin_lock_bh(&sc->rx.pcu_lock);
  213. if (!ath_stoprecv(sc))
  214. stopped = false;
  215. /* XXX: do not flush receive queue here. We don't want
  216. * to flush data frames already in queue because of
  217. * changing channel. */
  218. if (!stopped || !(sc->sc_flags & SC_OP_OFFCHANNEL))
  219. fastcc = false;
  220. if (!(sc->sc_flags & SC_OP_OFFCHANNEL))
  221. caldata = &aphy->caldata;
  222. ath_print(common, ATH_DBG_CONFIG,
  223. "(%u MHz) -> (%u MHz), conf_is_ht40: %d fastcc: %d\n",
  224. sc->sc_ah->curchan->channel,
  225. channel->center_freq, conf_is_ht40(conf),
  226. fastcc);
  227. spin_lock_bh(&sc->sc_resetlock);
  228. r = ath9k_hw_reset(ah, hchan, caldata, fastcc);
  229. if (r) {
  230. ath_print(common, ATH_DBG_FATAL,
  231. "Unable to reset channel (%u MHz), "
  232. "reset status %d\n",
  233. channel->center_freq, r);
  234. spin_unlock_bh(&sc->sc_resetlock);
  235. spin_unlock_bh(&sc->rx.pcu_lock);
  236. goto ps_restore;
  237. }
  238. spin_unlock_bh(&sc->sc_resetlock);
  239. if (ath_startrecv(sc) != 0) {
  240. ath_print(common, ATH_DBG_FATAL,
  241. "Unable to restart recv logic\n");
  242. r = -EIO;
  243. spin_unlock_bh(&sc->rx.pcu_lock);
  244. goto ps_restore;
  245. }
  246. spin_unlock_bh(&sc->rx.pcu_lock);
  247. ath_update_txpow(sc);
  248. ath9k_hw_set_interrupts(ah, ah->imask);
  249. if (!(sc->sc_flags & (SC_OP_OFFCHANNEL))) {
  250. ath_beacon_config(sc, NULL);
  251. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  252. ath_start_ani(common);
  253. }
  254. ps_restore:
  255. ath9k_ps_restore(sc);
  256. return r;
  257. }
  258. static void ath_paprd_activate(struct ath_softc *sc)
  259. {
  260. struct ath_hw *ah = sc->sc_ah;
  261. struct ath9k_hw_cal_data *caldata = ah->caldata;
  262. struct ath_common *common = ath9k_hw_common(ah);
  263. int chain;
  264. if (!caldata || !caldata->paprd_done)
  265. return;
  266. ath9k_ps_wakeup(sc);
  267. ar9003_paprd_enable(ah, false);
  268. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  269. if (!(common->tx_chainmask & BIT(chain)))
  270. continue;
  271. ar9003_paprd_populate_single_table(ah, caldata, chain);
  272. }
  273. ar9003_paprd_enable(ah, true);
  274. ath9k_ps_restore(sc);
  275. }
  276. void ath_paprd_calibrate(struct work_struct *work)
  277. {
  278. struct ath_softc *sc = container_of(work, struct ath_softc, paprd_work);
  279. struct ieee80211_hw *hw = sc->hw;
  280. struct ath_hw *ah = sc->sc_ah;
  281. struct ieee80211_hdr *hdr;
  282. struct sk_buff *skb = NULL;
  283. struct ieee80211_tx_info *tx_info;
  284. int band = hw->conf.channel->band;
  285. struct ieee80211_supported_band *sband = &sc->sbands[band];
  286. struct ath_tx_control txctl;
  287. struct ath9k_hw_cal_data *caldata = ah->caldata;
  288. struct ath_common *common = ath9k_hw_common(ah);
  289. int qnum, ftype;
  290. int chain_ok = 0;
  291. int chain;
  292. int len = 1800;
  293. int time_left;
  294. int i;
  295. if (!caldata)
  296. return;
  297. skb = alloc_skb(len, GFP_KERNEL);
  298. if (!skb)
  299. return;
  300. tx_info = IEEE80211_SKB_CB(skb);
  301. skb_put(skb, len);
  302. memset(skb->data, 0, len);
  303. hdr = (struct ieee80211_hdr *)skb->data;
  304. ftype = IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC;
  305. hdr->frame_control = cpu_to_le16(ftype);
  306. hdr->duration_id = cpu_to_le16(10);
  307. memcpy(hdr->addr1, hw->wiphy->perm_addr, ETH_ALEN);
  308. memcpy(hdr->addr2, hw->wiphy->perm_addr, ETH_ALEN);
  309. memcpy(hdr->addr3, hw->wiphy->perm_addr, ETH_ALEN);
  310. memset(&txctl, 0, sizeof(txctl));
  311. qnum = sc->tx.hwq_map[WME_AC_BE];
  312. txctl.txq = &sc->tx.txq[qnum];
  313. ath9k_ps_wakeup(sc);
  314. ar9003_paprd_init_table(ah);
  315. for (chain = 0; chain < AR9300_MAX_CHAINS; chain++) {
  316. if (!(common->tx_chainmask & BIT(chain)))
  317. continue;
  318. chain_ok = 0;
  319. memset(tx_info, 0, sizeof(*tx_info));
  320. tx_info->band = band;
  321. for (i = 0; i < 4; i++) {
  322. tx_info->control.rates[i].idx = sband->n_bitrates - 1;
  323. tx_info->control.rates[i].count = 6;
  324. }
  325. init_completion(&sc->paprd_complete);
  326. ar9003_paprd_setup_gain_table(ah, chain);
  327. txctl.paprd = BIT(chain);
  328. if (ath_tx_start(hw, skb, &txctl) != 0)
  329. break;
  330. time_left = wait_for_completion_timeout(&sc->paprd_complete,
  331. msecs_to_jiffies(ATH_PAPRD_TIMEOUT));
  332. if (!time_left) {
  333. ath_print(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  334. "Timeout waiting for paprd training on "
  335. "TX chain %d\n",
  336. chain);
  337. goto fail_paprd;
  338. }
  339. if (!ar9003_paprd_is_done(ah))
  340. break;
  341. if (ar9003_paprd_create_curve(ah, caldata, chain) != 0)
  342. break;
  343. chain_ok = 1;
  344. }
  345. kfree_skb(skb);
  346. if (chain_ok) {
  347. caldata->paprd_done = true;
  348. ath_paprd_activate(sc);
  349. }
  350. fail_paprd:
  351. ath9k_ps_restore(sc);
  352. }
  353. /*
  354. * This routine performs the periodic noise floor calibration function
  355. * that is used to adjust and optimize the chip performance. This
  356. * takes environmental changes (location, temperature) into account.
  357. * When the task is complete, it reschedules itself depending on the
  358. * appropriate interval that was calculated.
  359. */
  360. void ath_ani_calibrate(unsigned long data)
  361. {
  362. struct ath_softc *sc = (struct ath_softc *)data;
  363. struct ath_hw *ah = sc->sc_ah;
  364. struct ath_common *common = ath9k_hw_common(ah);
  365. bool longcal = false;
  366. bool shortcal = false;
  367. bool aniflag = false;
  368. unsigned int timestamp = jiffies_to_msecs(jiffies);
  369. u32 cal_interval, short_cal_interval, long_cal_interval;
  370. unsigned long flags;
  371. if (ah->caldata && ah->caldata->nfcal_interference)
  372. long_cal_interval = ATH_LONG_CALINTERVAL_INT;
  373. else
  374. long_cal_interval = ATH_LONG_CALINTERVAL;
  375. short_cal_interval = (ah->opmode == NL80211_IFTYPE_AP) ?
  376. ATH_AP_SHORT_CALINTERVAL : ATH_STA_SHORT_CALINTERVAL;
  377. /* Only calibrate if awake */
  378. if (sc->sc_ah->power_mode != ATH9K_PM_AWAKE)
  379. goto set_timer;
  380. ath9k_ps_wakeup(sc);
  381. /* Long calibration runs independently of short calibration. */
  382. if ((timestamp - common->ani.longcal_timer) >= long_cal_interval) {
  383. longcal = true;
  384. ath_print(common, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  385. common->ani.longcal_timer = timestamp;
  386. }
  387. /* Short calibration applies only while caldone is false */
  388. if (!common->ani.caldone) {
  389. if ((timestamp - common->ani.shortcal_timer) >= short_cal_interval) {
  390. shortcal = true;
  391. ath_print(common, ATH_DBG_ANI,
  392. "shortcal @%lu\n", jiffies);
  393. common->ani.shortcal_timer = timestamp;
  394. common->ani.resetcal_timer = timestamp;
  395. }
  396. } else {
  397. if ((timestamp - common->ani.resetcal_timer) >=
  398. ATH_RESTART_CALINTERVAL) {
  399. common->ani.caldone = ath9k_hw_reset_calvalid(ah);
  400. if (common->ani.caldone)
  401. common->ani.resetcal_timer = timestamp;
  402. }
  403. }
  404. /* Verify whether we must check ANI */
  405. if ((timestamp - common->ani.checkani_timer) >=
  406. ah->config.ani_poll_interval) {
  407. aniflag = true;
  408. common->ani.checkani_timer = timestamp;
  409. }
  410. /* Skip all processing if there's nothing to do. */
  411. if (longcal || shortcal || aniflag) {
  412. /* Call ANI routine if necessary */
  413. if (aniflag) {
  414. spin_lock_irqsave(&common->cc_lock, flags);
  415. ath9k_hw_ani_monitor(ah, ah->curchan);
  416. ath_update_survey_stats(sc);
  417. spin_unlock_irqrestore(&common->cc_lock, flags);
  418. }
  419. /* Perform calibration if necessary */
  420. if (longcal || shortcal) {
  421. common->ani.caldone =
  422. ath9k_hw_calibrate(ah,
  423. ah->curchan,
  424. common->rx_chainmask,
  425. longcal);
  426. }
  427. }
  428. ath9k_ps_restore(sc);
  429. set_timer:
  430. /*
  431. * Set timer interval based on previous results.
  432. * The interval must be the shortest necessary to satisfy ANI,
  433. * short calibration and long calibration.
  434. */
  435. cal_interval = ATH_LONG_CALINTERVAL;
  436. if (sc->sc_ah->config.enable_ani)
  437. cal_interval = min(cal_interval,
  438. (u32)ah->config.ani_poll_interval);
  439. if (!common->ani.caldone)
  440. cal_interval = min(cal_interval, (u32)short_cal_interval);
  441. mod_timer(&common->ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  442. if ((sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_PAPRD) && ah->caldata) {
  443. if (!ah->caldata->paprd_done)
  444. ieee80211_queue_work(sc->hw, &sc->paprd_work);
  445. else
  446. ath_paprd_activate(sc);
  447. }
  448. }
  449. /*
  450. * Update tx/rx chainmask. For legacy association,
  451. * hard code chainmask to 1x1, for 11n association, use
  452. * the chainmask configuration, for bt coexistence, use
  453. * the chainmask configuration even in legacy mode.
  454. */
  455. void ath_update_chainmask(struct ath_softc *sc, int is_ht)
  456. {
  457. struct ath_hw *ah = sc->sc_ah;
  458. struct ath_common *common = ath9k_hw_common(ah);
  459. if ((sc->sc_flags & SC_OP_OFFCHANNEL) || is_ht ||
  460. (ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE)) {
  461. common->tx_chainmask = ah->caps.tx_chainmask;
  462. common->rx_chainmask = ah->caps.rx_chainmask;
  463. } else {
  464. common->tx_chainmask = 1;
  465. common->rx_chainmask = 1;
  466. }
  467. ath_print(common, ATH_DBG_CONFIG,
  468. "tx chmask: %d, rx chmask: %d\n",
  469. common->tx_chainmask,
  470. common->rx_chainmask);
  471. }
  472. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  473. {
  474. struct ath_node *an;
  475. an = (struct ath_node *)sta->drv_priv;
  476. if (sc->sc_flags & SC_OP_TXAGGR) {
  477. ath_tx_node_init(sc, an);
  478. an->maxampdu = 1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
  479. sta->ht_cap.ampdu_factor);
  480. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  481. an->last_rssi = ATH_RSSI_DUMMY_MARKER;
  482. }
  483. }
  484. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  485. {
  486. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  487. if (sc->sc_flags & SC_OP_TXAGGR)
  488. ath_tx_node_cleanup(sc, an);
  489. }
  490. void ath_hw_check(struct work_struct *work)
  491. {
  492. struct ath_softc *sc = container_of(work, struct ath_softc, hw_check_work);
  493. int i;
  494. ath9k_ps_wakeup(sc);
  495. for (i = 0; i < 3; i++) {
  496. if (ath9k_hw_check_alive(sc->sc_ah))
  497. goto out;
  498. msleep(1);
  499. }
  500. ath_reset(sc, true);
  501. out:
  502. ath9k_ps_restore(sc);
  503. }
  504. void ath9k_tasklet(unsigned long data)
  505. {
  506. struct ath_softc *sc = (struct ath_softc *)data;
  507. struct ath_hw *ah = sc->sc_ah;
  508. struct ath_common *common = ath9k_hw_common(ah);
  509. u32 status = sc->intrstatus;
  510. u32 rxmask;
  511. ath9k_ps_wakeup(sc);
  512. if (status & ATH9K_INT_FATAL) {
  513. ath_reset(sc, true);
  514. ath9k_ps_restore(sc);
  515. return;
  516. }
  517. if (!ath9k_hw_check_alive(ah))
  518. ieee80211_queue_work(sc->hw, &sc->hw_check_work);
  519. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  520. rxmask = (ATH9K_INT_RXHP | ATH9K_INT_RXLP | ATH9K_INT_RXEOL |
  521. ATH9K_INT_RXORN);
  522. else
  523. rxmask = (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  524. if (status & rxmask) {
  525. spin_lock_bh(&sc->rx.pcu_lock);
  526. /* Check for high priority Rx first */
  527. if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
  528. (status & ATH9K_INT_RXHP))
  529. ath_rx_tasklet(sc, 0, true);
  530. ath_rx_tasklet(sc, 0, false);
  531. spin_unlock_bh(&sc->rx.pcu_lock);
  532. }
  533. if (status & ATH9K_INT_TX) {
  534. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  535. ath_tx_edma_tasklet(sc);
  536. else
  537. ath_tx_tasklet(sc);
  538. }
  539. if ((status & ATH9K_INT_TSFOOR) && sc->ps_enabled) {
  540. /*
  541. * TSF sync does not look correct; remain awake to sync with
  542. * the next Beacon.
  543. */
  544. ath_print(common, ATH_DBG_PS,
  545. "TSFOOR - Sync with next Beacon\n");
  546. sc->ps_flags |= PS_WAIT_FOR_BEACON | PS_BEACON_SYNC;
  547. }
  548. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  549. if (status & ATH9K_INT_GENTIMER)
  550. ath_gen_timer_isr(sc->sc_ah);
  551. /* re-enable hardware interrupt */
  552. ath9k_hw_set_interrupts(ah, ah->imask);
  553. ath9k_ps_restore(sc);
  554. }
  555. irqreturn_t ath_isr(int irq, void *dev)
  556. {
  557. #define SCHED_INTR ( \
  558. ATH9K_INT_FATAL | \
  559. ATH9K_INT_RXORN | \
  560. ATH9K_INT_RXEOL | \
  561. ATH9K_INT_RX | \
  562. ATH9K_INT_RXLP | \
  563. ATH9K_INT_RXHP | \
  564. ATH9K_INT_TX | \
  565. ATH9K_INT_BMISS | \
  566. ATH9K_INT_CST | \
  567. ATH9K_INT_TSFOOR | \
  568. ATH9K_INT_GENTIMER)
  569. struct ath_softc *sc = dev;
  570. struct ath_hw *ah = sc->sc_ah;
  571. struct ath_common *common = ath9k_hw_common(ah);
  572. enum ath9k_int status;
  573. bool sched = false;
  574. /*
  575. * The hardware is not ready/present, don't
  576. * touch anything. Note this can happen early
  577. * on if the IRQ is shared.
  578. */
  579. if (sc->sc_flags & SC_OP_INVALID)
  580. return IRQ_NONE;
  581. /* shared irq, not for us */
  582. if (!ath9k_hw_intrpend(ah))
  583. return IRQ_NONE;
  584. /*
  585. * Figure out the reason(s) for the interrupt. Note
  586. * that the hal returns a pseudo-ISR that may include
  587. * bits we haven't explicitly enabled so we mask the
  588. * value to insure we only process bits we requested.
  589. */
  590. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  591. status &= ah->imask; /* discard unasked-for bits */
  592. /*
  593. * If there are no status bits set, then this interrupt was not
  594. * for me (should have been caught above).
  595. */
  596. if (!status)
  597. return IRQ_NONE;
  598. /* Cache the status */
  599. sc->intrstatus = status;
  600. if (status & SCHED_INTR)
  601. sched = true;
  602. /*
  603. * If a FATAL or RXORN interrupt is received, we have to reset the
  604. * chip immediately.
  605. */
  606. if ((status & ATH9K_INT_FATAL) || ((status & ATH9K_INT_RXORN) &&
  607. !(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)))
  608. goto chip_reset;
  609. if ((ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) &&
  610. (status & ATH9K_INT_BB_WATCHDOG)) {
  611. spin_lock(&common->cc_lock);
  612. ath_hw_cycle_counters_update(common);
  613. ar9003_hw_bb_watchdog_dbg_info(ah);
  614. spin_unlock(&common->cc_lock);
  615. goto chip_reset;
  616. }
  617. if (status & ATH9K_INT_SWBA)
  618. tasklet_schedule(&sc->bcon_tasklet);
  619. if (status & ATH9K_INT_TXURN)
  620. ath9k_hw_updatetxtriglevel(ah, true);
  621. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA) {
  622. if (status & ATH9K_INT_RXEOL) {
  623. ah->imask &= ~(ATH9K_INT_RXEOL | ATH9K_INT_RXORN);
  624. ath9k_hw_set_interrupts(ah, ah->imask);
  625. }
  626. }
  627. if (status & ATH9K_INT_MIB) {
  628. /*
  629. * Disable interrupts until we service the MIB
  630. * interrupt; otherwise it will continue to
  631. * fire.
  632. */
  633. ath9k_hw_set_interrupts(ah, 0);
  634. /*
  635. * Let the hal handle the event. We assume
  636. * it will clear whatever condition caused
  637. * the interrupt.
  638. */
  639. spin_lock(&common->cc_lock);
  640. ath9k_hw_proc_mib_event(ah);
  641. spin_unlock(&common->cc_lock);
  642. ath9k_hw_set_interrupts(ah, ah->imask);
  643. }
  644. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  645. if (status & ATH9K_INT_TIM_TIMER) {
  646. /* Clear RxAbort bit so that we can
  647. * receive frames */
  648. ath9k_setpower(sc, ATH9K_PM_AWAKE);
  649. ath9k_hw_setrxabort(sc->sc_ah, 0);
  650. sc->ps_flags |= PS_WAIT_FOR_BEACON;
  651. }
  652. chip_reset:
  653. ath_debug_stat_interrupt(sc, status);
  654. if (sched) {
  655. /* turn off every interrupt except SWBA */
  656. ath9k_hw_set_interrupts(ah, (ah->imask & ATH9K_INT_SWBA));
  657. tasklet_schedule(&sc->intr_tq);
  658. }
  659. return IRQ_HANDLED;
  660. #undef SCHED_INTR
  661. }
  662. static u32 ath_get_extchanmode(struct ath_softc *sc,
  663. struct ieee80211_channel *chan,
  664. enum nl80211_channel_type channel_type)
  665. {
  666. u32 chanmode = 0;
  667. switch (chan->band) {
  668. case IEEE80211_BAND_2GHZ:
  669. switch(channel_type) {
  670. case NL80211_CHAN_NO_HT:
  671. case NL80211_CHAN_HT20:
  672. chanmode = CHANNEL_G_HT20;
  673. break;
  674. case NL80211_CHAN_HT40PLUS:
  675. chanmode = CHANNEL_G_HT40PLUS;
  676. break;
  677. case NL80211_CHAN_HT40MINUS:
  678. chanmode = CHANNEL_G_HT40MINUS;
  679. break;
  680. }
  681. break;
  682. case IEEE80211_BAND_5GHZ:
  683. switch(channel_type) {
  684. case NL80211_CHAN_NO_HT:
  685. case NL80211_CHAN_HT20:
  686. chanmode = CHANNEL_A_HT20;
  687. break;
  688. case NL80211_CHAN_HT40PLUS:
  689. chanmode = CHANNEL_A_HT40PLUS;
  690. break;
  691. case NL80211_CHAN_HT40MINUS:
  692. chanmode = CHANNEL_A_HT40MINUS;
  693. break;
  694. }
  695. break;
  696. default:
  697. break;
  698. }
  699. return chanmode;
  700. }
  701. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  702. struct ieee80211_vif *vif,
  703. struct ieee80211_bss_conf *bss_conf)
  704. {
  705. struct ath_hw *ah = sc->sc_ah;
  706. struct ath_common *common = ath9k_hw_common(ah);
  707. if (bss_conf->assoc) {
  708. ath_print(common, ATH_DBG_CONFIG,
  709. "Bss Info ASSOC %d, bssid: %pM\n",
  710. bss_conf->aid, common->curbssid);
  711. /* New association, store aid */
  712. common->curaid = bss_conf->aid;
  713. ath9k_hw_write_associd(ah);
  714. /*
  715. * Request a re-configuration of Beacon related timers
  716. * on the receipt of the first Beacon frame (i.e.,
  717. * after time sync with the AP).
  718. */
  719. sc->ps_flags |= PS_BEACON_SYNC;
  720. /* Configure the beacon */
  721. ath_beacon_config(sc, vif);
  722. /* Reset rssi stats */
  723. sc->sc_ah->stats.avgbrssi = ATH_RSSI_DUMMY_MARKER;
  724. sc->sc_flags |= SC_OP_ANI_RUN;
  725. ath_start_ani(common);
  726. } else {
  727. ath_print(common, ATH_DBG_CONFIG, "Bss Info DISASSOC\n");
  728. common->curaid = 0;
  729. /* Stop ANI */
  730. sc->sc_flags &= ~SC_OP_ANI_RUN;
  731. del_timer_sync(&common->ani.timer);
  732. }
  733. }
  734. void ath_radio_enable(struct ath_softc *sc, struct ieee80211_hw *hw)
  735. {
  736. struct ath_hw *ah = sc->sc_ah;
  737. struct ath_common *common = ath9k_hw_common(ah);
  738. struct ieee80211_channel *channel = hw->conf.channel;
  739. int r;
  740. ath9k_ps_wakeup(sc);
  741. ath9k_hw_configpcipowersave(ah, 0, 0);
  742. if (!ah->curchan)
  743. ah->curchan = ath_get_curchannel(sc, sc->hw);
  744. spin_lock_bh(&sc->rx.pcu_lock);
  745. spin_lock_bh(&sc->sc_resetlock);
  746. r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  747. if (r) {
  748. ath_print(common, ATH_DBG_FATAL,
  749. "Unable to reset channel (%u MHz), "
  750. "reset status %d\n",
  751. channel->center_freq, r);
  752. }
  753. spin_unlock_bh(&sc->sc_resetlock);
  754. ath_update_txpow(sc);
  755. if (ath_startrecv(sc) != 0) {
  756. ath_print(common, ATH_DBG_FATAL,
  757. "Unable to restart recv logic\n");
  758. spin_unlock_bh(&sc->rx.pcu_lock);
  759. return;
  760. }
  761. spin_unlock_bh(&sc->rx.pcu_lock);
  762. if (sc->sc_flags & SC_OP_BEACONS)
  763. ath_beacon_config(sc, NULL); /* restart beacons */
  764. /* Re-Enable interrupts */
  765. ath9k_hw_set_interrupts(ah, ah->imask);
  766. /* Enable LED */
  767. ath9k_hw_cfg_output(ah, ah->led_pin,
  768. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  769. ath9k_hw_set_gpio(ah, ah->led_pin, 0);
  770. ieee80211_wake_queues(hw);
  771. ath9k_ps_restore(sc);
  772. }
  773. void ath_radio_disable(struct ath_softc *sc, struct ieee80211_hw *hw)
  774. {
  775. struct ath_hw *ah = sc->sc_ah;
  776. struct ieee80211_channel *channel = hw->conf.channel;
  777. int r;
  778. ath9k_ps_wakeup(sc);
  779. ieee80211_stop_queues(hw);
  780. /*
  781. * Keep the LED on when the radio is disabled
  782. * during idle unassociated state.
  783. */
  784. if (!sc->ps_idle) {
  785. ath9k_hw_set_gpio(ah, ah->led_pin, 1);
  786. ath9k_hw_cfg_gpio_input(ah, ah->led_pin);
  787. }
  788. /* Disable interrupts */
  789. ath9k_hw_set_interrupts(ah, 0);
  790. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  791. spin_lock_bh(&sc->rx.pcu_lock);
  792. ath_stoprecv(sc); /* turn off frame recv */
  793. ath_flushrecv(sc); /* flush recv queue */
  794. if (!ah->curchan)
  795. ah->curchan = ath_get_curchannel(sc, hw);
  796. spin_lock_bh(&sc->sc_resetlock);
  797. r = ath9k_hw_reset(ah, ah->curchan, ah->caldata, false);
  798. if (r) {
  799. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  800. "Unable to reset channel (%u MHz), "
  801. "reset status %d\n",
  802. channel->center_freq, r);
  803. }
  804. spin_unlock_bh(&sc->sc_resetlock);
  805. ath9k_hw_phy_disable(ah);
  806. spin_unlock_bh(&sc->rx.pcu_lock);
  807. ath9k_hw_configpcipowersave(ah, 1, 1);
  808. ath9k_ps_restore(sc);
  809. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  810. }
  811. int ath_reset(struct ath_softc *sc, bool retry_tx)
  812. {
  813. struct ath_hw *ah = sc->sc_ah;
  814. struct ath_common *common = ath9k_hw_common(ah);
  815. struct ieee80211_hw *hw = sc->hw;
  816. int r;
  817. /* Stop ANI */
  818. del_timer_sync(&common->ani.timer);
  819. ieee80211_stop_queues(hw);
  820. ath9k_hw_set_interrupts(ah, 0);
  821. ath_drain_all_txq(sc, retry_tx);
  822. spin_lock_bh(&sc->rx.pcu_lock);
  823. ath_stoprecv(sc);
  824. ath_flushrecv(sc);
  825. spin_lock_bh(&sc->sc_resetlock);
  826. r = ath9k_hw_reset(ah, sc->sc_ah->curchan, ah->caldata, false);
  827. if (r)
  828. ath_print(common, ATH_DBG_FATAL,
  829. "Unable to reset hardware; reset status %d\n", r);
  830. spin_unlock_bh(&sc->sc_resetlock);
  831. if (ath_startrecv(sc) != 0)
  832. ath_print(common, ATH_DBG_FATAL,
  833. "Unable to start recv logic\n");
  834. spin_unlock_bh(&sc->rx.pcu_lock);
  835. /*
  836. * We may be doing a reset in response to a request
  837. * that changes the channel so update any state that
  838. * might change as a result.
  839. */
  840. ath_update_txpow(sc);
  841. if ((sc->sc_flags & SC_OP_BEACONS) || !(sc->sc_flags & (SC_OP_OFFCHANNEL)))
  842. ath_beacon_config(sc, NULL); /* restart beacons */
  843. ath9k_hw_set_interrupts(ah, ah->imask);
  844. if (retry_tx) {
  845. int i;
  846. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  847. if (ATH_TXQ_SETUP(sc, i)) {
  848. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  849. ath_txq_schedule(sc, &sc->tx.txq[i]);
  850. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  851. }
  852. }
  853. }
  854. ieee80211_wake_queues(hw);
  855. /* Start ANI */
  856. ath_start_ani(common);
  857. return r;
  858. }
  859. static int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  860. {
  861. int qnum;
  862. switch (queue) {
  863. case 0:
  864. qnum = sc->tx.hwq_map[WME_AC_VO];
  865. break;
  866. case 1:
  867. qnum = sc->tx.hwq_map[WME_AC_VI];
  868. break;
  869. case 2:
  870. qnum = sc->tx.hwq_map[WME_AC_BE];
  871. break;
  872. case 3:
  873. qnum = sc->tx.hwq_map[WME_AC_BK];
  874. break;
  875. default:
  876. qnum = sc->tx.hwq_map[WME_AC_BE];
  877. break;
  878. }
  879. return qnum;
  880. }
  881. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  882. {
  883. int qnum;
  884. switch (queue) {
  885. case WME_AC_VO:
  886. qnum = 0;
  887. break;
  888. case WME_AC_VI:
  889. qnum = 1;
  890. break;
  891. case WME_AC_BE:
  892. qnum = 2;
  893. break;
  894. case WME_AC_BK:
  895. qnum = 3;
  896. break;
  897. default:
  898. qnum = -1;
  899. break;
  900. }
  901. return qnum;
  902. }
  903. /* XXX: Remove me once we don't depend on ath9k_channel for all
  904. * this redundant data */
  905. void ath9k_update_ichannel(struct ath_softc *sc, struct ieee80211_hw *hw,
  906. struct ath9k_channel *ichan)
  907. {
  908. struct ieee80211_channel *chan = hw->conf.channel;
  909. struct ieee80211_conf *conf = &hw->conf;
  910. ichan->channel = chan->center_freq;
  911. ichan->chan = chan;
  912. if (chan->band == IEEE80211_BAND_2GHZ) {
  913. ichan->chanmode = CHANNEL_G;
  914. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM | CHANNEL_G;
  915. } else {
  916. ichan->chanmode = CHANNEL_A;
  917. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  918. }
  919. if (conf_is_ht(conf))
  920. ichan->chanmode = ath_get_extchanmode(sc, chan,
  921. conf->channel_type);
  922. }
  923. /**********************/
  924. /* mac80211 callbacks */
  925. /**********************/
  926. static int ath9k_start(struct ieee80211_hw *hw)
  927. {
  928. struct ath_wiphy *aphy = hw->priv;
  929. struct ath_softc *sc = aphy->sc;
  930. struct ath_hw *ah = sc->sc_ah;
  931. struct ath_common *common = ath9k_hw_common(ah);
  932. struct ieee80211_channel *curchan = hw->conf.channel;
  933. struct ath9k_channel *init_channel;
  934. int r;
  935. ath_print(common, ATH_DBG_CONFIG,
  936. "Starting driver with initial channel: %d MHz\n",
  937. curchan->center_freq);
  938. mutex_lock(&sc->mutex);
  939. if (ath9k_wiphy_started(sc)) {
  940. if (sc->chan_idx == curchan->hw_value) {
  941. /*
  942. * Already on the operational channel, the new wiphy
  943. * can be marked active.
  944. */
  945. aphy->state = ATH_WIPHY_ACTIVE;
  946. ieee80211_wake_queues(hw);
  947. } else {
  948. /*
  949. * Another wiphy is on another channel, start the new
  950. * wiphy in paused state.
  951. */
  952. aphy->state = ATH_WIPHY_PAUSED;
  953. ieee80211_stop_queues(hw);
  954. }
  955. mutex_unlock(&sc->mutex);
  956. return 0;
  957. }
  958. aphy->state = ATH_WIPHY_ACTIVE;
  959. /* setup initial channel */
  960. sc->chan_idx = curchan->hw_value;
  961. init_channel = ath_get_curchannel(sc, hw);
  962. /* Reset SERDES registers */
  963. ath9k_hw_configpcipowersave(ah, 0, 0);
  964. /*
  965. * The basic interface to setting the hardware in a good
  966. * state is ``reset''. On return the hardware is known to
  967. * be powered up and with interrupts disabled. This must
  968. * be followed by initialization of the appropriate bits
  969. * and then setup of the interrupt mask.
  970. */
  971. spin_lock_bh(&sc->rx.pcu_lock);
  972. spin_lock_bh(&sc->sc_resetlock);
  973. r = ath9k_hw_reset(ah, init_channel, ah->caldata, false);
  974. if (r) {
  975. ath_print(common, ATH_DBG_FATAL,
  976. "Unable to reset hardware; reset status %d "
  977. "(freq %u MHz)\n", r,
  978. curchan->center_freq);
  979. spin_unlock_bh(&sc->sc_resetlock);
  980. spin_unlock_bh(&sc->rx.pcu_lock);
  981. goto mutex_unlock;
  982. }
  983. spin_unlock_bh(&sc->sc_resetlock);
  984. /*
  985. * This is needed only to setup initial state
  986. * but it's best done after a reset.
  987. */
  988. ath_update_txpow(sc);
  989. /*
  990. * Setup the hardware after reset:
  991. * The receive engine is set going.
  992. * Frame transmit is handled entirely
  993. * in the frame output path; there's nothing to do
  994. * here except setup the interrupt mask.
  995. */
  996. if (ath_startrecv(sc) != 0) {
  997. ath_print(common, ATH_DBG_FATAL,
  998. "Unable to start recv logic\n");
  999. r = -EIO;
  1000. spin_unlock_bh(&sc->rx.pcu_lock);
  1001. goto mutex_unlock;
  1002. }
  1003. spin_unlock_bh(&sc->rx.pcu_lock);
  1004. /* Setup our intr mask. */
  1005. ah->imask = ATH9K_INT_TX | ATH9K_INT_RXEOL |
  1006. ATH9K_INT_RXORN | ATH9K_INT_FATAL |
  1007. ATH9K_INT_GLOBAL;
  1008. if (ah->caps.hw_caps & ATH9K_HW_CAP_EDMA)
  1009. ah->imask |= ATH9K_INT_RXHP |
  1010. ATH9K_INT_RXLP |
  1011. ATH9K_INT_BB_WATCHDOG;
  1012. else
  1013. ah->imask |= ATH9K_INT_RX;
  1014. ah->imask |= ATH9K_INT_GTT;
  1015. if (ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  1016. ah->imask |= ATH9K_INT_CST;
  1017. sc->sc_flags &= ~SC_OP_INVALID;
  1018. sc->sc_ah->is_monitoring = false;
  1019. /* Disable BMISS interrupt when we're not associated */
  1020. ah->imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1021. ath9k_hw_set_interrupts(ah, ah->imask);
  1022. ieee80211_wake_queues(hw);
  1023. ieee80211_queue_delayed_work(sc->hw, &sc->tx_complete_work, 0);
  1024. if ((ah->btcoex_hw.scheme != ATH_BTCOEX_CFG_NONE) &&
  1025. !ah->btcoex_hw.enabled) {
  1026. ath9k_hw_btcoex_set_weight(ah, AR_BT_COEX_WGHT,
  1027. AR_STOMP_LOW_WLAN_WGHT);
  1028. ath9k_hw_btcoex_enable(ah);
  1029. if (common->bus_ops->bt_coex_prep)
  1030. common->bus_ops->bt_coex_prep(common);
  1031. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1032. ath9k_btcoex_timer_resume(sc);
  1033. }
  1034. pm_qos_update_request(&sc->pm_qos_req, 55);
  1035. mutex_unlock:
  1036. mutex_unlock(&sc->mutex);
  1037. return r;
  1038. }
  1039. static int ath9k_tx(struct ieee80211_hw *hw,
  1040. struct sk_buff *skb)
  1041. {
  1042. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1043. struct ath_wiphy *aphy = hw->priv;
  1044. struct ath_softc *sc = aphy->sc;
  1045. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1046. struct ath_tx_control txctl;
  1047. int padpos, padsize;
  1048. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1049. int qnum;
  1050. if (aphy->state != ATH_WIPHY_ACTIVE && aphy->state != ATH_WIPHY_SCAN) {
  1051. ath_print(common, ATH_DBG_XMIT,
  1052. "ath9k: %s: TX in unexpected wiphy state "
  1053. "%d\n", wiphy_name(hw->wiphy), aphy->state);
  1054. goto exit;
  1055. }
  1056. if (sc->ps_enabled) {
  1057. /*
  1058. * mac80211 does not set PM field for normal data frames, so we
  1059. * need to update that based on the current PS mode.
  1060. */
  1061. if (ieee80211_is_data(hdr->frame_control) &&
  1062. !ieee80211_is_nullfunc(hdr->frame_control) &&
  1063. !ieee80211_has_pm(hdr->frame_control)) {
  1064. ath_print(common, ATH_DBG_PS, "Add PM=1 for a TX frame "
  1065. "while in PS mode\n");
  1066. hdr->frame_control |= cpu_to_le16(IEEE80211_FCTL_PM);
  1067. }
  1068. }
  1069. if (unlikely(sc->sc_ah->power_mode != ATH9K_PM_AWAKE)) {
  1070. /*
  1071. * We are using PS-Poll and mac80211 can request TX while in
  1072. * power save mode. Need to wake up hardware for the TX to be
  1073. * completed and if needed, also for RX of buffered frames.
  1074. */
  1075. ath9k_ps_wakeup(sc);
  1076. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP))
  1077. ath9k_hw_setrxabort(sc->sc_ah, 0);
  1078. if (ieee80211_is_pspoll(hdr->frame_control)) {
  1079. ath_print(common, ATH_DBG_PS,
  1080. "Sending PS-Poll to pick a buffered frame\n");
  1081. sc->ps_flags |= PS_WAIT_FOR_PSPOLL_DATA;
  1082. } else {
  1083. ath_print(common, ATH_DBG_PS,
  1084. "Wake up to complete TX\n");
  1085. sc->ps_flags |= PS_WAIT_FOR_TX_ACK;
  1086. }
  1087. /*
  1088. * The actual restore operation will happen only after
  1089. * the sc_flags bit is cleared. We are just dropping
  1090. * the ps_usecount here.
  1091. */
  1092. ath9k_ps_restore(sc);
  1093. }
  1094. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1095. /*
  1096. * As a temporary workaround, assign seq# here; this will likely need
  1097. * to be cleaned up to work better with Beacon transmission and virtual
  1098. * BSSes.
  1099. */
  1100. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1101. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1102. sc->tx.seq_no += 0x10;
  1103. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1104. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1105. }
  1106. /* Add the padding after the header if this is not already done */
  1107. padpos = ath9k_cmn_padpos(hdr->frame_control);
  1108. padsize = padpos & 3;
  1109. if (padsize && skb->len>padpos) {
  1110. if (skb_headroom(skb) < padsize)
  1111. return -1;
  1112. skb_push(skb, padsize);
  1113. memmove(skb->data, skb->data + padsize, padpos);
  1114. }
  1115. qnum = ath_get_hal_qnum(skb_get_queue_mapping(skb), sc);
  1116. txctl.txq = &sc->tx.txq[qnum];
  1117. ath_print(common, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  1118. if (ath_tx_start(hw, skb, &txctl) != 0) {
  1119. ath_print(common, ATH_DBG_XMIT, "TX failed\n");
  1120. goto exit;
  1121. }
  1122. return 0;
  1123. exit:
  1124. dev_kfree_skb_any(skb);
  1125. return 0;
  1126. }
  1127. static void ath9k_stop(struct ieee80211_hw *hw)
  1128. {
  1129. struct ath_wiphy *aphy = hw->priv;
  1130. struct ath_softc *sc = aphy->sc;
  1131. struct ath_hw *ah = sc->sc_ah;
  1132. struct ath_common *common = ath9k_hw_common(ah);
  1133. int i;
  1134. mutex_lock(&sc->mutex);
  1135. aphy->state = ATH_WIPHY_INACTIVE;
  1136. if (led_blink)
  1137. cancel_delayed_work_sync(&sc->ath_led_blink_work);
  1138. cancel_delayed_work_sync(&sc->tx_complete_work);
  1139. cancel_work_sync(&sc->paprd_work);
  1140. cancel_work_sync(&sc->hw_check_work);
  1141. for (i = 0; i < sc->num_sec_wiphy; i++) {
  1142. if (sc->sec_wiphy[i])
  1143. break;
  1144. }
  1145. if (i == sc->num_sec_wiphy) {
  1146. cancel_delayed_work_sync(&sc->wiphy_work);
  1147. cancel_work_sync(&sc->chan_work);
  1148. }
  1149. if (sc->sc_flags & SC_OP_INVALID) {
  1150. ath_print(common, ATH_DBG_ANY, "Device not present\n");
  1151. mutex_unlock(&sc->mutex);
  1152. return;
  1153. }
  1154. if (ath9k_wiphy_started(sc)) {
  1155. mutex_unlock(&sc->mutex);
  1156. return; /* another wiphy still in use */
  1157. }
  1158. /* Ensure HW is awake when we try to shut it down. */
  1159. ath9k_ps_wakeup(sc);
  1160. if (ah->btcoex_hw.enabled) {
  1161. ath9k_hw_btcoex_disable(ah);
  1162. if (ah->btcoex_hw.scheme == ATH_BTCOEX_CFG_3WIRE)
  1163. ath9k_btcoex_timer_pause(sc);
  1164. }
  1165. /* make sure h/w will not generate any interrupt
  1166. * before setting the invalid flag. */
  1167. ath9k_hw_set_interrupts(ah, 0);
  1168. spin_lock_bh(&sc->rx.pcu_lock);
  1169. if (!(sc->sc_flags & SC_OP_INVALID)) {
  1170. ath_drain_all_txq(sc, false);
  1171. ath_stoprecv(sc);
  1172. ath9k_hw_phy_disable(ah);
  1173. } else
  1174. sc->rx.rxlink = NULL;
  1175. spin_unlock_bh(&sc->rx.pcu_lock);
  1176. /* disable HAL and put h/w to sleep */
  1177. ath9k_hw_disable(ah);
  1178. ath9k_hw_configpcipowersave(ah, 1, 1);
  1179. ath9k_ps_restore(sc);
  1180. /* Finally, put the chip in FULL SLEEP mode */
  1181. ath9k_setpower(sc, ATH9K_PM_FULL_SLEEP);
  1182. sc->sc_flags |= SC_OP_INVALID;
  1183. pm_qos_update_request(&sc->pm_qos_req, PM_QOS_DEFAULT_VALUE);
  1184. mutex_unlock(&sc->mutex);
  1185. ath_print(common, ATH_DBG_CONFIG, "Driver halt\n");
  1186. }
  1187. static int ath9k_add_interface(struct ieee80211_hw *hw,
  1188. struct ieee80211_vif *vif)
  1189. {
  1190. struct ath_wiphy *aphy = hw->priv;
  1191. struct ath_softc *sc = aphy->sc;
  1192. struct ath_hw *ah = sc->sc_ah;
  1193. struct ath_common *common = ath9k_hw_common(ah);
  1194. struct ath_vif *avp = (void *)vif->drv_priv;
  1195. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  1196. int ret = 0;
  1197. mutex_lock(&sc->mutex);
  1198. switch (vif->type) {
  1199. case NL80211_IFTYPE_STATION:
  1200. ic_opmode = NL80211_IFTYPE_STATION;
  1201. break;
  1202. case NL80211_IFTYPE_WDS:
  1203. ic_opmode = NL80211_IFTYPE_WDS;
  1204. break;
  1205. case NL80211_IFTYPE_ADHOC:
  1206. case NL80211_IFTYPE_AP:
  1207. case NL80211_IFTYPE_MESH_POINT:
  1208. if (sc->nbcnvifs >= ATH_BCBUF) {
  1209. ret = -ENOBUFS;
  1210. goto out;
  1211. }
  1212. ic_opmode = vif->type;
  1213. break;
  1214. default:
  1215. ath_print(common, ATH_DBG_FATAL,
  1216. "Interface type %d not yet supported\n", vif->type);
  1217. ret = -EOPNOTSUPP;
  1218. goto out;
  1219. }
  1220. ath_print(common, ATH_DBG_CONFIG,
  1221. "Attach a VIF of type: %d\n", ic_opmode);
  1222. /* Set the VIF opmode */
  1223. avp->av_opmode = ic_opmode;
  1224. avp->av_bslot = -1;
  1225. sc->nvifs++;
  1226. ath9k_set_bssid_mask(hw, vif);
  1227. if (sc->nvifs > 1)
  1228. goto out; /* skip global settings for secondary vif */
  1229. if (ic_opmode == NL80211_IFTYPE_AP) {
  1230. ath9k_hw_set_tsfadjust(ah, 1);
  1231. sc->sc_flags |= SC_OP_TSF_RESET;
  1232. }
  1233. /* Set the device opmode */
  1234. ah->opmode = ic_opmode;
  1235. /*
  1236. * Enable MIB interrupts when there are hardware phy counters.
  1237. * Note we only do this (at the moment) for station mode.
  1238. */
  1239. if ((vif->type == NL80211_IFTYPE_STATION) ||
  1240. (vif->type == NL80211_IFTYPE_ADHOC) ||
  1241. (vif->type == NL80211_IFTYPE_MESH_POINT)) {
  1242. if (ah->config.enable_ani)
  1243. ah->imask |= ATH9K_INT_MIB;
  1244. ah->imask |= ATH9K_INT_TSFOOR;
  1245. }
  1246. ath9k_hw_set_interrupts(ah, ah->imask);
  1247. if (vif->type == NL80211_IFTYPE_AP ||
  1248. vif->type == NL80211_IFTYPE_ADHOC) {
  1249. sc->sc_flags |= SC_OP_ANI_RUN;
  1250. ath_start_ani(common);
  1251. }
  1252. out:
  1253. mutex_unlock(&sc->mutex);
  1254. return ret;
  1255. }
  1256. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  1257. struct ieee80211_vif *vif)
  1258. {
  1259. struct ath_wiphy *aphy = hw->priv;
  1260. struct ath_softc *sc = aphy->sc;
  1261. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1262. struct ath_vif *avp = (void *)vif->drv_priv;
  1263. ath_print(common, ATH_DBG_CONFIG, "Detach Interface\n");
  1264. mutex_lock(&sc->mutex);
  1265. /* Stop ANI */
  1266. sc->sc_flags &= ~SC_OP_ANI_RUN;
  1267. del_timer_sync(&common->ani.timer);
  1268. /* Reclaim beacon resources */
  1269. if ((sc->sc_ah->opmode == NL80211_IFTYPE_AP) ||
  1270. (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC) ||
  1271. (sc->sc_ah->opmode == NL80211_IFTYPE_MESH_POINT)) {
  1272. /* Disable SWBA interrupt */
  1273. sc->sc_ah->imask &= ~ATH9K_INT_SWBA;
  1274. ath9k_ps_wakeup(sc);
  1275. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_ah->imask);
  1276. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1277. ath9k_ps_restore(sc);
  1278. tasklet_kill(&sc->bcon_tasklet);
  1279. }
  1280. ath_beacon_return(sc, avp);
  1281. sc->sc_flags &= ~SC_OP_BEACONS;
  1282. if (sc->nbcnvifs) {
  1283. /* Re-enable SWBA interrupt */
  1284. sc->sc_ah->imask |= ATH9K_INT_SWBA;
  1285. ath9k_ps_wakeup(sc);
  1286. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_ah->imask);
  1287. ath9k_ps_restore(sc);
  1288. }
  1289. sc->nvifs--;
  1290. mutex_unlock(&sc->mutex);
  1291. }
  1292. static void ath9k_enable_ps(struct ath_softc *sc)
  1293. {
  1294. struct ath_hw *ah = sc->sc_ah;
  1295. sc->ps_enabled = true;
  1296. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1297. if ((ah->imask & ATH9K_INT_TIM_TIMER) == 0) {
  1298. ah->imask |= ATH9K_INT_TIM_TIMER;
  1299. ath9k_hw_set_interrupts(ah, ah->imask);
  1300. }
  1301. ath9k_hw_setrxabort(ah, 1);
  1302. }
  1303. }
  1304. static void ath9k_disable_ps(struct ath_softc *sc)
  1305. {
  1306. struct ath_hw *ah = sc->sc_ah;
  1307. sc->ps_enabled = false;
  1308. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  1309. if (!(ah->caps.hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1310. ath9k_hw_setrxabort(ah, 0);
  1311. sc->ps_flags &= ~(PS_WAIT_FOR_BEACON |
  1312. PS_WAIT_FOR_CAB |
  1313. PS_WAIT_FOR_PSPOLL_DATA |
  1314. PS_WAIT_FOR_TX_ACK);
  1315. if (ah->imask & ATH9K_INT_TIM_TIMER) {
  1316. ah->imask &= ~ATH9K_INT_TIM_TIMER;
  1317. ath9k_hw_set_interrupts(ah, ah->imask);
  1318. }
  1319. }
  1320. }
  1321. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1322. {
  1323. struct ath_wiphy *aphy = hw->priv;
  1324. struct ath_softc *sc = aphy->sc;
  1325. struct ath_hw *ah = sc->sc_ah;
  1326. struct ath_common *common = ath9k_hw_common(ah);
  1327. struct ieee80211_conf *conf = &hw->conf;
  1328. bool disable_radio;
  1329. mutex_lock(&sc->mutex);
  1330. /*
  1331. * Leave this as the first check because we need to turn on the
  1332. * radio if it was disabled before prior to processing the rest
  1333. * of the changes. Likewise we must only disable the radio towards
  1334. * the end.
  1335. */
  1336. if (changed & IEEE80211_CONF_CHANGE_IDLE) {
  1337. bool enable_radio;
  1338. bool all_wiphys_idle;
  1339. bool idle = !!(conf->flags & IEEE80211_CONF_IDLE);
  1340. spin_lock_bh(&sc->wiphy_lock);
  1341. all_wiphys_idle = ath9k_all_wiphys_idle(sc);
  1342. ath9k_set_wiphy_idle(aphy, idle);
  1343. enable_radio = (!idle && all_wiphys_idle);
  1344. /*
  1345. * After we unlock here its possible another wiphy
  1346. * can be re-renabled so to account for that we will
  1347. * only disable the radio toward the end of this routine
  1348. * if by then all wiphys are still idle.
  1349. */
  1350. spin_unlock_bh(&sc->wiphy_lock);
  1351. if (enable_radio) {
  1352. sc->ps_idle = false;
  1353. ath_radio_enable(sc, hw);
  1354. ath_print(common, ATH_DBG_CONFIG,
  1355. "not-idle: enabling radio\n");
  1356. }
  1357. }
  1358. /*
  1359. * We just prepare to enable PS. We have to wait until our AP has
  1360. * ACK'd our null data frame to disable RX otherwise we'll ignore
  1361. * those ACKs and end up retransmitting the same null data frames.
  1362. * IEEE80211_CONF_CHANGE_PS is only passed by mac80211 for STA mode.
  1363. */
  1364. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1365. unsigned long flags;
  1366. spin_lock_irqsave(&sc->sc_pm_lock, flags);
  1367. if (conf->flags & IEEE80211_CONF_PS)
  1368. ath9k_enable_ps(sc);
  1369. else
  1370. ath9k_disable_ps(sc);
  1371. spin_unlock_irqrestore(&sc->sc_pm_lock, flags);
  1372. }
  1373. if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
  1374. if (conf->flags & IEEE80211_CONF_MONITOR) {
  1375. ath_print(common, ATH_DBG_CONFIG,
  1376. "Monitor mode is enabled\n");
  1377. sc->sc_ah->is_monitoring = true;
  1378. } else {
  1379. ath_print(common, ATH_DBG_CONFIG,
  1380. "Monitor mode is disabled\n");
  1381. sc->sc_ah->is_monitoring = false;
  1382. }
  1383. }
  1384. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1385. struct ieee80211_channel *curchan = hw->conf.channel;
  1386. int pos = curchan->hw_value;
  1387. int old_pos = -1;
  1388. unsigned long flags;
  1389. if (ah->curchan)
  1390. old_pos = ah->curchan - &ah->channels[0];
  1391. aphy->chan_idx = pos;
  1392. aphy->chan_is_ht = conf_is_ht(conf);
  1393. if (hw->conf.flags & IEEE80211_CONF_OFFCHANNEL)
  1394. sc->sc_flags |= SC_OP_OFFCHANNEL;
  1395. else
  1396. sc->sc_flags &= ~SC_OP_OFFCHANNEL;
  1397. if (aphy->state == ATH_WIPHY_SCAN ||
  1398. aphy->state == ATH_WIPHY_ACTIVE)
  1399. ath9k_wiphy_pause_all_forced(sc, aphy);
  1400. else {
  1401. /*
  1402. * Do not change operational channel based on a paused
  1403. * wiphy changes.
  1404. */
  1405. goto skip_chan_change;
  1406. }
  1407. ath_print(common, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  1408. curchan->center_freq);
  1409. /* XXX: remove me eventualy */
  1410. ath9k_update_ichannel(sc, hw, &sc->sc_ah->channels[pos]);
  1411. ath_update_chainmask(sc, conf_is_ht(conf));
  1412. /* update survey stats for the old channel before switching */
  1413. spin_lock_irqsave(&common->cc_lock, flags);
  1414. ath_update_survey_stats(sc);
  1415. spin_unlock_irqrestore(&common->cc_lock, flags);
  1416. /*
  1417. * If the operating channel changes, change the survey in-use flags
  1418. * along with it.
  1419. * Reset the survey data for the new channel, unless we're switching
  1420. * back to the operating channel from an off-channel operation.
  1421. */
  1422. if (!(hw->conf.flags & IEEE80211_CONF_OFFCHANNEL) &&
  1423. sc->cur_survey != &sc->survey[pos]) {
  1424. if (sc->cur_survey)
  1425. sc->cur_survey->filled &= ~SURVEY_INFO_IN_USE;
  1426. sc->cur_survey = &sc->survey[pos];
  1427. memset(sc->cur_survey, 0, sizeof(struct survey_info));
  1428. sc->cur_survey->filled |= SURVEY_INFO_IN_USE;
  1429. } else if (!(sc->survey[pos].filled & SURVEY_INFO_IN_USE)) {
  1430. memset(&sc->survey[pos], 0, sizeof(struct survey_info));
  1431. }
  1432. if (ath_set_channel(sc, hw, &sc->sc_ah->channels[pos]) < 0) {
  1433. ath_print(common, ATH_DBG_FATAL,
  1434. "Unable to set channel\n");
  1435. mutex_unlock(&sc->mutex);
  1436. return -EINVAL;
  1437. }
  1438. /*
  1439. * The most recent snapshot of channel->noisefloor for the old
  1440. * channel is only available after the hardware reset. Copy it to
  1441. * the survey stats now.
  1442. */
  1443. if (old_pos >= 0)
  1444. ath_update_survey_nf(sc, old_pos);
  1445. }
  1446. skip_chan_change:
  1447. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1448. sc->config.txpowlimit = 2 * conf->power_level;
  1449. ath_update_txpow(sc);
  1450. }
  1451. spin_lock_bh(&sc->wiphy_lock);
  1452. disable_radio = ath9k_all_wiphys_idle(sc);
  1453. spin_unlock_bh(&sc->wiphy_lock);
  1454. if (disable_radio) {
  1455. ath_print(common, ATH_DBG_CONFIG, "idle: disabling radio\n");
  1456. sc->ps_idle = true;
  1457. ath_radio_disable(sc, hw);
  1458. }
  1459. mutex_unlock(&sc->mutex);
  1460. return 0;
  1461. }
  1462. #define SUPPORTED_FILTERS \
  1463. (FIF_PROMISC_IN_BSS | \
  1464. FIF_ALLMULTI | \
  1465. FIF_CONTROL | \
  1466. FIF_PSPOLL | \
  1467. FIF_OTHER_BSS | \
  1468. FIF_BCN_PRBRESP_PROMISC | \
  1469. FIF_PROBE_REQ | \
  1470. FIF_FCSFAIL)
  1471. /* FIXME: sc->sc_full_reset ? */
  1472. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  1473. unsigned int changed_flags,
  1474. unsigned int *total_flags,
  1475. u64 multicast)
  1476. {
  1477. struct ath_wiphy *aphy = hw->priv;
  1478. struct ath_softc *sc = aphy->sc;
  1479. u32 rfilt;
  1480. changed_flags &= SUPPORTED_FILTERS;
  1481. *total_flags &= SUPPORTED_FILTERS;
  1482. sc->rx.rxfilter = *total_flags;
  1483. ath9k_ps_wakeup(sc);
  1484. rfilt = ath_calcrxfilter(sc);
  1485. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  1486. ath9k_ps_restore(sc);
  1487. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
  1488. "Set HW RX filter: 0x%x\n", rfilt);
  1489. }
  1490. static int ath9k_sta_add(struct ieee80211_hw *hw,
  1491. struct ieee80211_vif *vif,
  1492. struct ieee80211_sta *sta)
  1493. {
  1494. struct ath_wiphy *aphy = hw->priv;
  1495. struct ath_softc *sc = aphy->sc;
  1496. ath_node_attach(sc, sta);
  1497. return 0;
  1498. }
  1499. static int ath9k_sta_remove(struct ieee80211_hw *hw,
  1500. struct ieee80211_vif *vif,
  1501. struct ieee80211_sta *sta)
  1502. {
  1503. struct ath_wiphy *aphy = hw->priv;
  1504. struct ath_softc *sc = aphy->sc;
  1505. ath_node_detach(sc, sta);
  1506. return 0;
  1507. }
  1508. static int ath9k_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1509. const struct ieee80211_tx_queue_params *params)
  1510. {
  1511. struct ath_wiphy *aphy = hw->priv;
  1512. struct ath_softc *sc = aphy->sc;
  1513. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1514. struct ath9k_tx_queue_info qi;
  1515. int ret = 0, qnum;
  1516. if (queue >= WME_NUM_AC)
  1517. return 0;
  1518. mutex_lock(&sc->mutex);
  1519. memset(&qi, 0, sizeof(struct ath9k_tx_queue_info));
  1520. qi.tqi_aifs = params->aifs;
  1521. qi.tqi_cwmin = params->cw_min;
  1522. qi.tqi_cwmax = params->cw_max;
  1523. qi.tqi_burstTime = params->txop;
  1524. qnum = ath_get_hal_qnum(queue, sc);
  1525. ath_print(common, ATH_DBG_CONFIG,
  1526. "Configure tx [queue/halq] [%d/%d], "
  1527. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  1528. queue, qnum, params->aifs, params->cw_min,
  1529. params->cw_max, params->txop);
  1530. ret = ath_txq_update(sc, qnum, &qi);
  1531. if (ret)
  1532. ath_print(common, ATH_DBG_FATAL, "TXQ Update failed\n");
  1533. if (sc->sc_ah->opmode == NL80211_IFTYPE_ADHOC)
  1534. if ((qnum == sc->tx.hwq_map[WME_AC_BE]) && !ret)
  1535. ath_beaconq_config(sc);
  1536. mutex_unlock(&sc->mutex);
  1537. return ret;
  1538. }
  1539. static int ath9k_set_key(struct ieee80211_hw *hw,
  1540. enum set_key_cmd cmd,
  1541. struct ieee80211_vif *vif,
  1542. struct ieee80211_sta *sta,
  1543. struct ieee80211_key_conf *key)
  1544. {
  1545. struct ath_wiphy *aphy = hw->priv;
  1546. struct ath_softc *sc = aphy->sc;
  1547. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1548. int ret = 0;
  1549. if (modparam_nohwcrypt)
  1550. return -ENOSPC;
  1551. mutex_lock(&sc->mutex);
  1552. ath9k_ps_wakeup(sc);
  1553. ath_print(common, ATH_DBG_CONFIG, "Set HW Key\n");
  1554. switch (cmd) {
  1555. case SET_KEY:
  1556. ret = ath_key_config(common, vif, sta, key);
  1557. if (ret >= 0) {
  1558. key->hw_key_idx = ret;
  1559. /* push IV and Michael MIC generation to stack */
  1560. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1561. if (key->cipher == WLAN_CIPHER_SUITE_TKIP)
  1562. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1563. if (sc->sc_ah->sw_mgmt_crypto &&
  1564. key->cipher == WLAN_CIPHER_SUITE_CCMP)
  1565. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  1566. ret = 0;
  1567. }
  1568. break;
  1569. case DISABLE_KEY:
  1570. ath_key_delete(common, key);
  1571. break;
  1572. default:
  1573. ret = -EINVAL;
  1574. }
  1575. ath9k_ps_restore(sc);
  1576. mutex_unlock(&sc->mutex);
  1577. return ret;
  1578. }
  1579. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  1580. struct ieee80211_vif *vif,
  1581. struct ieee80211_bss_conf *bss_conf,
  1582. u32 changed)
  1583. {
  1584. struct ath_wiphy *aphy = hw->priv;
  1585. struct ath_softc *sc = aphy->sc;
  1586. struct ath_hw *ah = sc->sc_ah;
  1587. struct ath_common *common = ath9k_hw_common(ah);
  1588. struct ath_vif *avp = (void *)vif->drv_priv;
  1589. int slottime;
  1590. int error;
  1591. mutex_lock(&sc->mutex);
  1592. if (changed & BSS_CHANGED_BSSID) {
  1593. /* Set BSSID */
  1594. memcpy(common->curbssid, bss_conf->bssid, ETH_ALEN);
  1595. memcpy(avp->bssid, bss_conf->bssid, ETH_ALEN);
  1596. common->curaid = 0;
  1597. ath9k_hw_write_associd(ah);
  1598. /* Set aggregation protection mode parameters */
  1599. sc->config.ath_aggr_prot = 0;
  1600. /* Only legacy IBSS for now */
  1601. if (vif->type == NL80211_IFTYPE_ADHOC)
  1602. ath_update_chainmask(sc, 0);
  1603. ath_print(common, ATH_DBG_CONFIG,
  1604. "BSSID: %pM aid: 0x%x\n",
  1605. common->curbssid, common->curaid);
  1606. /* need to reconfigure the beacon */
  1607. sc->sc_flags &= ~SC_OP_BEACONS ;
  1608. }
  1609. /* Enable transmission of beacons (AP, IBSS, MESH) */
  1610. if ((changed & BSS_CHANGED_BEACON) ||
  1611. ((changed & BSS_CHANGED_BEACON_ENABLED) && bss_conf->enable_beacon)) {
  1612. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1613. error = ath_beacon_alloc(aphy, vif);
  1614. if (!error)
  1615. ath_beacon_config(sc, vif);
  1616. }
  1617. if (changed & BSS_CHANGED_ERP_SLOT) {
  1618. if (bss_conf->use_short_slot)
  1619. slottime = 9;
  1620. else
  1621. slottime = 20;
  1622. if (vif->type == NL80211_IFTYPE_AP) {
  1623. /*
  1624. * Defer update, so that connected stations can adjust
  1625. * their settings at the same time.
  1626. * See beacon.c for more details
  1627. */
  1628. sc->beacon.slottime = slottime;
  1629. sc->beacon.updateslot = UPDATE;
  1630. } else {
  1631. ah->slottime = slottime;
  1632. ath9k_hw_init_global_settings(ah);
  1633. }
  1634. }
  1635. /* Disable transmission of beacons */
  1636. if ((changed & BSS_CHANGED_BEACON_ENABLED) && !bss_conf->enable_beacon)
  1637. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1638. if (changed & BSS_CHANGED_BEACON_INT) {
  1639. sc->beacon_interval = bss_conf->beacon_int;
  1640. /*
  1641. * In case of AP mode, the HW TSF has to be reset
  1642. * when the beacon interval changes.
  1643. */
  1644. if (vif->type == NL80211_IFTYPE_AP) {
  1645. sc->sc_flags |= SC_OP_TSF_RESET;
  1646. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1647. error = ath_beacon_alloc(aphy, vif);
  1648. if (!error)
  1649. ath_beacon_config(sc, vif);
  1650. } else {
  1651. ath_beacon_config(sc, vif);
  1652. }
  1653. }
  1654. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  1655. ath_print(common, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  1656. bss_conf->use_short_preamble);
  1657. if (bss_conf->use_short_preamble)
  1658. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  1659. else
  1660. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  1661. }
  1662. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  1663. ath_print(common, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  1664. bss_conf->use_cts_prot);
  1665. if (bss_conf->use_cts_prot &&
  1666. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  1667. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  1668. else
  1669. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  1670. }
  1671. if (changed & BSS_CHANGED_ASSOC) {
  1672. ath_print(common, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  1673. bss_conf->assoc);
  1674. ath9k_bss_assoc_info(sc, vif, bss_conf);
  1675. }
  1676. mutex_unlock(&sc->mutex);
  1677. }
  1678. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  1679. {
  1680. u64 tsf;
  1681. struct ath_wiphy *aphy = hw->priv;
  1682. struct ath_softc *sc = aphy->sc;
  1683. mutex_lock(&sc->mutex);
  1684. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  1685. mutex_unlock(&sc->mutex);
  1686. return tsf;
  1687. }
  1688. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  1689. {
  1690. struct ath_wiphy *aphy = hw->priv;
  1691. struct ath_softc *sc = aphy->sc;
  1692. mutex_lock(&sc->mutex);
  1693. ath9k_hw_settsf64(sc->sc_ah, tsf);
  1694. mutex_unlock(&sc->mutex);
  1695. }
  1696. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  1697. {
  1698. struct ath_wiphy *aphy = hw->priv;
  1699. struct ath_softc *sc = aphy->sc;
  1700. mutex_lock(&sc->mutex);
  1701. ath9k_ps_wakeup(sc);
  1702. ath9k_hw_reset_tsf(sc->sc_ah);
  1703. ath9k_ps_restore(sc);
  1704. mutex_unlock(&sc->mutex);
  1705. }
  1706. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  1707. struct ieee80211_vif *vif,
  1708. enum ieee80211_ampdu_mlme_action action,
  1709. struct ieee80211_sta *sta,
  1710. u16 tid, u16 *ssn)
  1711. {
  1712. struct ath_wiphy *aphy = hw->priv;
  1713. struct ath_softc *sc = aphy->sc;
  1714. int ret = 0;
  1715. local_bh_disable();
  1716. switch (action) {
  1717. case IEEE80211_AMPDU_RX_START:
  1718. if (!(sc->sc_flags & SC_OP_RXAGGR))
  1719. ret = -ENOTSUPP;
  1720. break;
  1721. case IEEE80211_AMPDU_RX_STOP:
  1722. break;
  1723. case IEEE80211_AMPDU_TX_START:
  1724. ath9k_ps_wakeup(sc);
  1725. ret = ath_tx_aggr_start(sc, sta, tid, ssn);
  1726. if (!ret)
  1727. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1728. ath9k_ps_restore(sc);
  1729. break;
  1730. case IEEE80211_AMPDU_TX_STOP:
  1731. ath9k_ps_wakeup(sc);
  1732. ath_tx_aggr_stop(sc, sta, tid);
  1733. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1734. ath9k_ps_restore(sc);
  1735. break;
  1736. case IEEE80211_AMPDU_TX_OPERATIONAL:
  1737. ath9k_ps_wakeup(sc);
  1738. ath_tx_aggr_resume(sc, sta, tid);
  1739. ath9k_ps_restore(sc);
  1740. break;
  1741. default:
  1742. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  1743. "Unknown AMPDU action\n");
  1744. }
  1745. local_bh_enable();
  1746. return ret;
  1747. }
  1748. static int ath9k_get_survey(struct ieee80211_hw *hw, int idx,
  1749. struct survey_info *survey)
  1750. {
  1751. struct ath_wiphy *aphy = hw->priv;
  1752. struct ath_softc *sc = aphy->sc;
  1753. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1754. struct ieee80211_supported_band *sband;
  1755. struct ieee80211_channel *chan;
  1756. unsigned long flags;
  1757. int pos;
  1758. spin_lock_irqsave(&common->cc_lock, flags);
  1759. if (idx == 0)
  1760. ath_update_survey_stats(sc);
  1761. sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
  1762. if (sband && idx >= sband->n_channels) {
  1763. idx -= sband->n_channels;
  1764. sband = NULL;
  1765. }
  1766. if (!sband)
  1767. sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
  1768. if (!sband || idx >= sband->n_channels) {
  1769. spin_unlock_irqrestore(&common->cc_lock, flags);
  1770. return -ENOENT;
  1771. }
  1772. chan = &sband->channels[idx];
  1773. pos = chan->hw_value;
  1774. memcpy(survey, &sc->survey[pos], sizeof(*survey));
  1775. survey->channel = chan;
  1776. spin_unlock_irqrestore(&common->cc_lock, flags);
  1777. return 0;
  1778. }
  1779. static void ath9k_sw_scan_start(struct ieee80211_hw *hw)
  1780. {
  1781. struct ath_wiphy *aphy = hw->priv;
  1782. struct ath_softc *sc = aphy->sc;
  1783. mutex_lock(&sc->mutex);
  1784. if (ath9k_wiphy_scanning(sc)) {
  1785. /*
  1786. * There is a race here in mac80211 but fixing it requires
  1787. * we revisit how we handle the scan complete callback.
  1788. * After mac80211 fixes we will not have configured hardware
  1789. * to the home channel nor would we have configured the RX
  1790. * filter yet.
  1791. */
  1792. mutex_unlock(&sc->mutex);
  1793. return;
  1794. }
  1795. aphy->state = ATH_WIPHY_SCAN;
  1796. ath9k_wiphy_pause_all_forced(sc, aphy);
  1797. mutex_unlock(&sc->mutex);
  1798. }
  1799. /*
  1800. * XXX: this requires a revisit after the driver
  1801. * scan_complete gets moved to another place/removed in mac80211.
  1802. */
  1803. static void ath9k_sw_scan_complete(struct ieee80211_hw *hw)
  1804. {
  1805. struct ath_wiphy *aphy = hw->priv;
  1806. struct ath_softc *sc = aphy->sc;
  1807. mutex_lock(&sc->mutex);
  1808. aphy->state = ATH_WIPHY_ACTIVE;
  1809. mutex_unlock(&sc->mutex);
  1810. }
  1811. static void ath9k_set_coverage_class(struct ieee80211_hw *hw, u8 coverage_class)
  1812. {
  1813. struct ath_wiphy *aphy = hw->priv;
  1814. struct ath_softc *sc = aphy->sc;
  1815. struct ath_hw *ah = sc->sc_ah;
  1816. mutex_lock(&sc->mutex);
  1817. ah->coverage_class = coverage_class;
  1818. ath9k_hw_init_global_settings(ah);
  1819. mutex_unlock(&sc->mutex);
  1820. }
  1821. struct ieee80211_ops ath9k_ops = {
  1822. .tx = ath9k_tx,
  1823. .start = ath9k_start,
  1824. .stop = ath9k_stop,
  1825. .add_interface = ath9k_add_interface,
  1826. .remove_interface = ath9k_remove_interface,
  1827. .config = ath9k_config,
  1828. .configure_filter = ath9k_configure_filter,
  1829. .sta_add = ath9k_sta_add,
  1830. .sta_remove = ath9k_sta_remove,
  1831. .conf_tx = ath9k_conf_tx,
  1832. .bss_info_changed = ath9k_bss_info_changed,
  1833. .set_key = ath9k_set_key,
  1834. .get_tsf = ath9k_get_tsf,
  1835. .set_tsf = ath9k_set_tsf,
  1836. .reset_tsf = ath9k_reset_tsf,
  1837. .ampdu_action = ath9k_ampdu_action,
  1838. .get_survey = ath9k_get_survey,
  1839. .sw_scan_start = ath9k_sw_scan_start,
  1840. .sw_scan_complete = ath9k_sw_scan_complete,
  1841. .rfkill_poll = ath9k_rfkill_poll_state,
  1842. .set_coverage_class = ath9k_set_coverage_class,
  1843. };