main.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635
  1. /*
  2. * Copyright (c) 2008 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 "core.h"
  18. #include "reg.h"
  19. #include "hw.h"
  20. #define ATH_PCI_VERSION "0.1"
  21. static char *dev_info = "ath9k";
  22. MODULE_AUTHOR("Atheros Communications");
  23. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  24. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  25. MODULE_LICENSE("Dual BSD/GPL");
  26. /* We use the hw_value as an index into our private channel structure */
  27. #define CHAN2G(_freq, _idx) { \
  28. .center_freq = (_freq), \
  29. .hw_value = (_idx), \
  30. .max_power = 30, \
  31. }
  32. #define CHAN5G(_freq, _idx) { \
  33. .band = IEEE80211_BAND_5GHZ, \
  34. .center_freq = (_freq), \
  35. .hw_value = (_idx), \
  36. .max_power = 30, \
  37. }
  38. /* Some 2 GHz radios are actually tunable on 2312-2732
  39. * on 5 MHz steps, we support the channels which we know
  40. * we have calibration data for all cards though to make
  41. * this static */
  42. static struct ieee80211_channel ath9k_2ghz_chantable[] = {
  43. CHAN2G(2412, 0), /* Channel 1 */
  44. CHAN2G(2417, 1), /* Channel 2 */
  45. CHAN2G(2422, 2), /* Channel 3 */
  46. CHAN2G(2427, 3), /* Channel 4 */
  47. CHAN2G(2432, 4), /* Channel 5 */
  48. CHAN2G(2437, 5), /* Channel 6 */
  49. CHAN2G(2442, 6), /* Channel 7 */
  50. CHAN2G(2447, 7), /* Channel 8 */
  51. CHAN2G(2452, 8), /* Channel 9 */
  52. CHAN2G(2457, 9), /* Channel 10 */
  53. CHAN2G(2462, 10), /* Channel 11 */
  54. CHAN2G(2467, 11), /* Channel 12 */
  55. CHAN2G(2472, 12), /* Channel 13 */
  56. CHAN2G(2484, 13), /* Channel 14 */
  57. };
  58. /* Some 5 GHz radios are actually tunable on XXXX-YYYY
  59. * on 5 MHz steps, we support the channels which we know
  60. * we have calibration data for all cards though to make
  61. * this static */
  62. static struct ieee80211_channel ath9k_5ghz_chantable[] = {
  63. /* _We_ call this UNII 1 */
  64. CHAN5G(5180, 14), /* Channel 36 */
  65. CHAN5G(5200, 15), /* Channel 40 */
  66. CHAN5G(5220, 16), /* Channel 44 */
  67. CHAN5G(5240, 17), /* Channel 48 */
  68. /* _We_ call this UNII 2 */
  69. CHAN5G(5260, 18), /* Channel 52 */
  70. CHAN5G(5280, 19), /* Channel 56 */
  71. CHAN5G(5300, 20), /* Channel 60 */
  72. CHAN5G(5320, 21), /* Channel 64 */
  73. /* _We_ call this "Middle band" */
  74. CHAN5G(5500, 22), /* Channel 100 */
  75. CHAN5G(5520, 23), /* Channel 104 */
  76. CHAN5G(5540, 24), /* Channel 108 */
  77. CHAN5G(5560, 25), /* Channel 112 */
  78. CHAN5G(5580, 26), /* Channel 116 */
  79. CHAN5G(5600, 27), /* Channel 120 */
  80. CHAN5G(5620, 28), /* Channel 124 */
  81. CHAN5G(5640, 29), /* Channel 128 */
  82. CHAN5G(5660, 30), /* Channel 132 */
  83. CHAN5G(5680, 31), /* Channel 136 */
  84. CHAN5G(5700, 32), /* Channel 140 */
  85. /* _We_ call this UNII 3 */
  86. CHAN5G(5745, 33), /* Channel 149 */
  87. CHAN5G(5765, 34), /* Channel 153 */
  88. CHAN5G(5785, 35), /* Channel 157 */
  89. CHAN5G(5805, 36), /* Channel 161 */
  90. CHAN5G(5825, 37), /* Channel 165 */
  91. };
  92. static void ath_cache_conf_rate(struct ath_softc *sc,
  93. struct ieee80211_conf *conf)
  94. {
  95. switch (conf->channel->band) {
  96. case IEEE80211_BAND_2GHZ:
  97. if (conf_is_ht20(conf))
  98. sc->cur_rate_table =
  99. sc->hw_rate_table[ATH9K_MODE_11NG_HT20];
  100. else if (conf_is_ht40_minus(conf))
  101. sc->cur_rate_table =
  102. sc->hw_rate_table[ATH9K_MODE_11NG_HT40MINUS];
  103. else if (conf_is_ht40_plus(conf))
  104. sc->cur_rate_table =
  105. sc->hw_rate_table[ATH9K_MODE_11NG_HT40PLUS];
  106. else
  107. sc->cur_rate_table =
  108. sc->hw_rate_table[ATH9K_MODE_11G];
  109. break;
  110. case IEEE80211_BAND_5GHZ:
  111. if (conf_is_ht20(conf))
  112. sc->cur_rate_table =
  113. sc->hw_rate_table[ATH9K_MODE_11NA_HT20];
  114. else if (conf_is_ht40_minus(conf))
  115. sc->cur_rate_table =
  116. sc->hw_rate_table[ATH9K_MODE_11NA_HT40MINUS];
  117. else if (conf_is_ht40_plus(conf))
  118. sc->cur_rate_table =
  119. sc->hw_rate_table[ATH9K_MODE_11NA_HT40PLUS];
  120. else
  121. sc->cur_rate_table =
  122. sc->hw_rate_table[ATH9K_MODE_11A];
  123. break;
  124. default:
  125. BUG_ON(1);
  126. break;
  127. }
  128. }
  129. static void ath_update_txpow(struct ath_softc *sc)
  130. {
  131. struct ath_hal *ah = sc->sc_ah;
  132. u32 txpow;
  133. if (sc->sc_curtxpow != sc->sc_config.txpowlimit) {
  134. ath9k_hw_set_txpowerlimit(ah, sc->sc_config.txpowlimit);
  135. /* read back in case value is clamped */
  136. ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
  137. sc->sc_curtxpow = txpow;
  138. }
  139. }
  140. static u8 parse_mpdudensity(u8 mpdudensity)
  141. {
  142. /*
  143. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  144. * 0 for no restriction
  145. * 1 for 1/4 us
  146. * 2 for 1/2 us
  147. * 3 for 1 us
  148. * 4 for 2 us
  149. * 5 for 4 us
  150. * 6 for 8 us
  151. * 7 for 16 us
  152. */
  153. switch (mpdudensity) {
  154. case 0:
  155. return 0;
  156. case 1:
  157. case 2:
  158. case 3:
  159. /* Our lower layer calculations limit our precision to
  160. 1 microsecond */
  161. return 1;
  162. case 4:
  163. return 2;
  164. case 5:
  165. return 4;
  166. case 6:
  167. return 8;
  168. case 7:
  169. return 16;
  170. default:
  171. return 0;
  172. }
  173. }
  174. static void ath_setup_rates(struct ath_softc *sc, enum ieee80211_band band)
  175. {
  176. struct ath_rate_table *rate_table = NULL;
  177. struct ieee80211_supported_band *sband;
  178. struct ieee80211_rate *rate;
  179. int i, maxrates;
  180. switch (band) {
  181. case IEEE80211_BAND_2GHZ:
  182. rate_table = sc->hw_rate_table[ATH9K_MODE_11G];
  183. break;
  184. case IEEE80211_BAND_5GHZ:
  185. rate_table = sc->hw_rate_table[ATH9K_MODE_11A];
  186. break;
  187. default:
  188. break;
  189. }
  190. if (rate_table == NULL)
  191. return;
  192. sband = &sc->sbands[band];
  193. rate = sc->rates[band];
  194. if (rate_table->rate_cnt > ATH_RATE_MAX)
  195. maxrates = ATH_RATE_MAX;
  196. else
  197. maxrates = rate_table->rate_cnt;
  198. for (i = 0; i < maxrates; i++) {
  199. rate[i].bitrate = rate_table->info[i].ratekbps / 100;
  200. rate[i].hw_value = rate_table->info[i].ratecode;
  201. sband->n_bitrates++;
  202. DPRINTF(sc, ATH_DBG_CONFIG, "Rate: %2dMbps, ratecode: %2d\n",
  203. rate[i].bitrate / 10, rate[i].hw_value);
  204. }
  205. }
  206. /*
  207. * Set/change channels. If the channel is really being changed, it's done
  208. * by reseting the chip. To accomplish this we must first cleanup any pending
  209. * DMA, then restart stuff.
  210. */
  211. static int ath_set_channel(struct ath_softc *sc, struct ath9k_channel *hchan)
  212. {
  213. struct ath_hal *ah = sc->sc_ah;
  214. bool fastcc = true, stopped;
  215. struct ieee80211_hw *hw = sc->hw;
  216. struct ieee80211_channel *channel = hw->conf.channel;
  217. int r;
  218. if (sc->sc_flags & SC_OP_INVALID)
  219. return -EIO;
  220. ath9k_ps_wakeup(sc);
  221. /*
  222. * This is only performed if the channel settings have
  223. * actually changed.
  224. *
  225. * To switch channels clear any pending DMA operations;
  226. * wait long enough for the RX fifo to drain, reset the
  227. * hardware at the new frequency, and then re-enable
  228. * the relevant bits of the h/w.
  229. */
  230. ath9k_hw_set_interrupts(ah, 0);
  231. ath_drain_all_txq(sc, false);
  232. stopped = ath_stoprecv(sc);
  233. /* XXX: do not flush receive queue here. We don't want
  234. * to flush data frames already in queue because of
  235. * changing channel. */
  236. if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
  237. fastcc = false;
  238. DPRINTF(sc, ATH_DBG_CONFIG,
  239. "(%u MHz) -> (%u MHz), chanwidth: %d\n",
  240. sc->sc_ah->ah_curchan->channel,
  241. channel->center_freq, sc->tx_chan_width);
  242. spin_lock_bh(&sc->sc_resetlock);
  243. r = ath9k_hw_reset(ah, hchan, fastcc);
  244. if (r) {
  245. DPRINTF(sc, ATH_DBG_FATAL,
  246. "Unable to reset channel (%u Mhz) "
  247. "reset status %u\n",
  248. channel->center_freq, r);
  249. spin_unlock_bh(&sc->sc_resetlock);
  250. return r;
  251. }
  252. spin_unlock_bh(&sc->sc_resetlock);
  253. sc->sc_flags &= ~SC_OP_CHAINMASK_UPDATE;
  254. sc->sc_flags &= ~SC_OP_FULL_RESET;
  255. if (ath_startrecv(sc) != 0) {
  256. DPRINTF(sc, ATH_DBG_FATAL,
  257. "Unable to restart recv logic\n");
  258. return -EIO;
  259. }
  260. ath_cache_conf_rate(sc, &hw->conf);
  261. ath_update_txpow(sc);
  262. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  263. ath9k_ps_restore(sc);
  264. return 0;
  265. }
  266. /*
  267. * This routine performs the periodic noise floor calibration function
  268. * that is used to adjust and optimize the chip performance. This
  269. * takes environmental changes (location, temperature) into account.
  270. * When the task is complete, it reschedules itself depending on the
  271. * appropriate interval that was calculated.
  272. */
  273. static void ath_ani_calibrate(unsigned long data)
  274. {
  275. struct ath_softc *sc;
  276. struct ath_hal *ah;
  277. bool longcal = false;
  278. bool shortcal = false;
  279. bool aniflag = false;
  280. unsigned int timestamp = jiffies_to_msecs(jiffies);
  281. u32 cal_interval;
  282. sc = (struct ath_softc *)data;
  283. ah = sc->sc_ah;
  284. /*
  285. * don't calibrate when we're scanning.
  286. * we are most likely not on our home channel.
  287. */
  288. if (sc->rx.rxfilter & FIF_BCN_PRBRESP_PROMISC)
  289. return;
  290. /* Long calibration runs independently of short calibration. */
  291. if ((timestamp - sc->sc_ani.sc_longcal_timer) >= ATH_LONG_CALINTERVAL) {
  292. longcal = true;
  293. DPRINTF(sc, ATH_DBG_ANI, "longcal @%lu\n", jiffies);
  294. sc->sc_ani.sc_longcal_timer = timestamp;
  295. }
  296. /* Short calibration applies only while sc_caldone is false */
  297. if (!sc->sc_ani.sc_caldone) {
  298. if ((timestamp - sc->sc_ani.sc_shortcal_timer) >=
  299. ATH_SHORT_CALINTERVAL) {
  300. shortcal = true;
  301. DPRINTF(sc, ATH_DBG_ANI, "shortcal @%lu\n", jiffies);
  302. sc->sc_ani.sc_shortcal_timer = timestamp;
  303. sc->sc_ani.sc_resetcal_timer = timestamp;
  304. }
  305. } else {
  306. if ((timestamp - sc->sc_ani.sc_resetcal_timer) >=
  307. ATH_RESTART_CALINTERVAL) {
  308. sc->sc_ani.sc_caldone = ath9k_hw_reset_calvalid(ah);
  309. if (sc->sc_ani.sc_caldone)
  310. sc->sc_ani.sc_resetcal_timer = timestamp;
  311. }
  312. }
  313. /* Verify whether we must check ANI */
  314. if ((timestamp - sc->sc_ani.sc_checkani_timer) >=
  315. ATH_ANI_POLLINTERVAL) {
  316. aniflag = true;
  317. sc->sc_ani.sc_checkani_timer = timestamp;
  318. }
  319. /* Skip all processing if there's nothing to do. */
  320. if (longcal || shortcal || aniflag) {
  321. /* Call ANI routine if necessary */
  322. if (aniflag)
  323. ath9k_hw_ani_monitor(ah, &sc->sc_halstats,
  324. ah->ah_curchan);
  325. /* Perform calibration if necessary */
  326. if (longcal || shortcal) {
  327. bool iscaldone = false;
  328. if (ath9k_hw_calibrate(ah, ah->ah_curchan,
  329. sc->sc_rx_chainmask, longcal,
  330. &iscaldone)) {
  331. if (longcal)
  332. sc->sc_ani.sc_noise_floor =
  333. ath9k_hw_getchan_noise(ah,
  334. ah->ah_curchan);
  335. DPRINTF(sc, ATH_DBG_ANI,
  336. "calibrate chan %u/%x nf: %d\n",
  337. ah->ah_curchan->channel,
  338. ah->ah_curchan->channelFlags,
  339. sc->sc_ani.sc_noise_floor);
  340. } else {
  341. DPRINTF(sc, ATH_DBG_ANY,
  342. "calibrate chan %u/%x failed\n",
  343. ah->ah_curchan->channel,
  344. ah->ah_curchan->channelFlags);
  345. }
  346. sc->sc_ani.sc_caldone = iscaldone;
  347. }
  348. }
  349. /*
  350. * Set timer interval based on previous results.
  351. * The interval must be the shortest necessary to satisfy ANI,
  352. * short calibration and long calibration.
  353. */
  354. cal_interval = ATH_LONG_CALINTERVAL;
  355. if (sc->sc_ah->ah_config.enable_ani)
  356. cal_interval = min(cal_interval, (u32)ATH_ANI_POLLINTERVAL);
  357. if (!sc->sc_ani.sc_caldone)
  358. cal_interval = min(cal_interval, (u32)ATH_SHORT_CALINTERVAL);
  359. mod_timer(&sc->sc_ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  360. }
  361. /*
  362. * Update tx/rx chainmask. For legacy association,
  363. * hard code chainmask to 1x1, for 11n association, use
  364. * the chainmask configuration, for bt coexistence, use
  365. * the chainmask configuration even in legacy mode.
  366. */
  367. static void ath_update_chainmask(struct ath_softc *sc, int is_ht)
  368. {
  369. sc->sc_flags |= SC_OP_CHAINMASK_UPDATE;
  370. if (is_ht ||
  371. (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_BT_COEX)) {
  372. sc->sc_tx_chainmask = sc->sc_ah->ah_caps.tx_chainmask;
  373. sc->sc_rx_chainmask = sc->sc_ah->ah_caps.rx_chainmask;
  374. } else {
  375. sc->sc_tx_chainmask = 1;
  376. sc->sc_rx_chainmask = 1;
  377. }
  378. DPRINTF(sc, ATH_DBG_CONFIG, "tx chmask: %d, rx chmask: %d\n",
  379. sc->sc_tx_chainmask, sc->sc_rx_chainmask);
  380. }
  381. static void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  382. {
  383. struct ath_node *an;
  384. an = (struct ath_node *)sta->drv_priv;
  385. if (sc->sc_flags & SC_OP_TXAGGR)
  386. ath_tx_node_init(sc, an);
  387. an->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
  388. sta->ht_cap.ampdu_factor);
  389. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  390. }
  391. static void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  392. {
  393. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  394. if (sc->sc_flags & SC_OP_TXAGGR)
  395. ath_tx_node_cleanup(sc, an);
  396. }
  397. static void ath9k_tasklet(unsigned long data)
  398. {
  399. struct ath_softc *sc = (struct ath_softc *)data;
  400. u32 status = sc->sc_intrstatus;
  401. if (status & ATH9K_INT_FATAL) {
  402. /* need a chip reset */
  403. ath_reset(sc, false);
  404. return;
  405. } else {
  406. if (status &
  407. (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
  408. spin_lock_bh(&sc->rx.rxflushlock);
  409. ath_rx_tasklet(sc, 0);
  410. spin_unlock_bh(&sc->rx.rxflushlock);
  411. }
  412. /* XXX: optimize this */
  413. if (status & ATH9K_INT_TX)
  414. ath_tx_tasklet(sc);
  415. }
  416. /* re-enable hardware interrupt */
  417. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
  418. }
  419. irqreturn_t ath_isr(int irq, void *dev)
  420. {
  421. struct ath_softc *sc = dev;
  422. struct ath_hal *ah = sc->sc_ah;
  423. enum ath9k_int status;
  424. bool sched = false;
  425. do {
  426. if (sc->sc_flags & SC_OP_INVALID) {
  427. /*
  428. * The hardware is not ready/present, don't
  429. * touch anything. Note this can happen early
  430. * on if the IRQ is shared.
  431. */
  432. return IRQ_NONE;
  433. }
  434. if (!ath9k_hw_intrpend(ah)) { /* shared irq, not for us */
  435. return IRQ_NONE;
  436. }
  437. /*
  438. * Figure out the reason(s) for the interrupt. Note
  439. * that the hal returns a pseudo-ISR that may include
  440. * bits we haven't explicitly enabled so we mask the
  441. * value to insure we only process bits we requested.
  442. */
  443. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  444. status &= sc->sc_imask; /* discard unasked-for bits */
  445. /*
  446. * If there are no status bits set, then this interrupt was not
  447. * for me (should have been caught above).
  448. */
  449. if (!status)
  450. return IRQ_NONE;
  451. sc->sc_intrstatus = status;
  452. if (status & ATH9K_INT_FATAL) {
  453. /* need a chip reset */
  454. sched = true;
  455. } else if (status & ATH9K_INT_RXORN) {
  456. /* need a chip reset */
  457. sched = true;
  458. } else {
  459. if (status & ATH9K_INT_SWBA) {
  460. /* schedule a tasklet for beacon handling */
  461. tasklet_schedule(&sc->bcon_tasklet);
  462. }
  463. if (status & ATH9K_INT_RXEOL) {
  464. /*
  465. * NB: the hardware should re-read the link when
  466. * RXE bit is written, but it doesn't work
  467. * at least on older hardware revs.
  468. */
  469. sched = true;
  470. }
  471. if (status & ATH9K_INT_TXURN)
  472. /* bump tx trigger level */
  473. ath9k_hw_updatetxtriglevel(ah, true);
  474. /* XXX: optimize this */
  475. if (status & ATH9K_INT_RX)
  476. sched = true;
  477. if (status & ATH9K_INT_TX)
  478. sched = true;
  479. if (status & ATH9K_INT_BMISS)
  480. sched = true;
  481. /* carrier sense timeout */
  482. if (status & ATH9K_INT_CST)
  483. sched = true;
  484. if (status & ATH9K_INT_MIB) {
  485. /*
  486. * Disable interrupts until we service the MIB
  487. * interrupt; otherwise it will continue to
  488. * fire.
  489. */
  490. ath9k_hw_set_interrupts(ah, 0);
  491. /*
  492. * Let the hal handle the event. We assume
  493. * it will clear whatever condition caused
  494. * the interrupt.
  495. */
  496. ath9k_hw_procmibevent(ah, &sc->sc_halstats);
  497. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  498. }
  499. if (status & ATH9K_INT_TIM_TIMER) {
  500. if (!(ah->ah_caps.hw_caps &
  501. ATH9K_HW_CAP_AUTOSLEEP)) {
  502. /* Clear RxAbort bit so that we can
  503. * receive frames */
  504. ath9k_hw_setpower(ah, ATH9K_PM_AWAKE);
  505. ath9k_hw_setrxabort(ah, 0);
  506. sched = true;
  507. sc->sc_flags |= SC_OP_WAIT_FOR_BEACON;
  508. }
  509. }
  510. }
  511. } while (0);
  512. ath_debug_stat_interrupt(sc, status);
  513. if (sched) {
  514. /* turn off every interrupt except SWBA */
  515. ath9k_hw_set_interrupts(ah, (sc->sc_imask & ATH9K_INT_SWBA));
  516. tasklet_schedule(&sc->intr_tq);
  517. }
  518. return IRQ_HANDLED;
  519. }
  520. static u32 ath_get_extchanmode(struct ath_softc *sc,
  521. struct ieee80211_channel *chan,
  522. enum nl80211_channel_type channel_type)
  523. {
  524. u32 chanmode = 0;
  525. switch (chan->band) {
  526. case IEEE80211_BAND_2GHZ:
  527. switch(channel_type) {
  528. case NL80211_CHAN_NO_HT:
  529. case NL80211_CHAN_HT20:
  530. chanmode = CHANNEL_G_HT20;
  531. break;
  532. case NL80211_CHAN_HT40PLUS:
  533. chanmode = CHANNEL_G_HT40PLUS;
  534. break;
  535. case NL80211_CHAN_HT40MINUS:
  536. chanmode = CHANNEL_G_HT40MINUS;
  537. break;
  538. }
  539. break;
  540. case IEEE80211_BAND_5GHZ:
  541. switch(channel_type) {
  542. case NL80211_CHAN_NO_HT:
  543. case NL80211_CHAN_HT20:
  544. chanmode = CHANNEL_A_HT20;
  545. break;
  546. case NL80211_CHAN_HT40PLUS:
  547. chanmode = CHANNEL_A_HT40PLUS;
  548. break;
  549. case NL80211_CHAN_HT40MINUS:
  550. chanmode = CHANNEL_A_HT40MINUS;
  551. break;
  552. }
  553. break;
  554. default:
  555. break;
  556. }
  557. return chanmode;
  558. }
  559. static int ath_keyset(struct ath_softc *sc, u16 keyix,
  560. struct ath9k_keyval *hk, const u8 mac[ETH_ALEN])
  561. {
  562. bool status;
  563. status = ath9k_hw_set_keycache_entry(sc->sc_ah,
  564. keyix, hk, mac, false);
  565. return status != false;
  566. }
  567. static int ath_setkey_tkip(struct ath_softc *sc, u16 keyix, const u8 *key,
  568. struct ath9k_keyval *hk,
  569. const u8 *addr)
  570. {
  571. const u8 *key_rxmic;
  572. const u8 *key_txmic;
  573. key_txmic = key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  574. key_rxmic = key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  575. if (addr == NULL) {
  576. /* Group key installation */
  577. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  578. return ath_keyset(sc, keyix, hk, addr);
  579. }
  580. if (!sc->sc_splitmic) {
  581. /*
  582. * data key goes at first index,
  583. * the hal handles the MIC keys at index+64.
  584. */
  585. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  586. memcpy(hk->kv_txmic, key_txmic, sizeof(hk->kv_txmic));
  587. return ath_keyset(sc, keyix, hk, addr);
  588. }
  589. /*
  590. * TX key goes at first index, RX key at +32.
  591. * The hal handles the MIC keys at index+64.
  592. */
  593. memcpy(hk->kv_mic, key_txmic, sizeof(hk->kv_mic));
  594. if (!ath_keyset(sc, keyix, hk, NULL)) {
  595. /* Txmic entry failed. No need to proceed further */
  596. DPRINTF(sc, ATH_DBG_KEYCACHE,
  597. "Setting TX MIC Key Failed\n");
  598. return 0;
  599. }
  600. memcpy(hk->kv_mic, key_rxmic, sizeof(hk->kv_mic));
  601. /* XXX delete tx key on failure? */
  602. return ath_keyset(sc, keyix + 32, hk, addr);
  603. }
  604. static int ath_reserve_key_cache_slot_tkip(struct ath_softc *sc)
  605. {
  606. int i;
  607. for (i = IEEE80211_WEP_NKID; i < sc->sc_keymax / 2; i++) {
  608. if (test_bit(i, sc->sc_keymap) ||
  609. test_bit(i + 64, sc->sc_keymap))
  610. continue; /* At least one part of TKIP key allocated */
  611. if (sc->sc_splitmic &&
  612. (test_bit(i + 32, sc->sc_keymap) ||
  613. test_bit(i + 64 + 32, sc->sc_keymap)))
  614. continue; /* At least one part of TKIP key allocated */
  615. /* Found a free slot for a TKIP key */
  616. return i;
  617. }
  618. return -1;
  619. }
  620. static int ath_reserve_key_cache_slot(struct ath_softc *sc)
  621. {
  622. int i;
  623. /* First, try to find slots that would not be available for TKIP. */
  624. if (sc->sc_splitmic) {
  625. for (i = IEEE80211_WEP_NKID; i < sc->sc_keymax / 4; i++) {
  626. if (!test_bit(i, sc->sc_keymap) &&
  627. (test_bit(i + 32, sc->sc_keymap) ||
  628. test_bit(i + 64, sc->sc_keymap) ||
  629. test_bit(i + 64 + 32, sc->sc_keymap)))
  630. return i;
  631. if (!test_bit(i + 32, sc->sc_keymap) &&
  632. (test_bit(i, sc->sc_keymap) ||
  633. test_bit(i + 64, sc->sc_keymap) ||
  634. test_bit(i + 64 + 32, sc->sc_keymap)))
  635. return i + 32;
  636. if (!test_bit(i + 64, sc->sc_keymap) &&
  637. (test_bit(i , sc->sc_keymap) ||
  638. test_bit(i + 32, sc->sc_keymap) ||
  639. test_bit(i + 64 + 32, sc->sc_keymap)))
  640. return i + 64;
  641. if (!test_bit(i + 64 + 32, sc->sc_keymap) &&
  642. (test_bit(i, sc->sc_keymap) ||
  643. test_bit(i + 32, sc->sc_keymap) ||
  644. test_bit(i + 64, sc->sc_keymap)))
  645. return i + 64 + 32;
  646. }
  647. } else {
  648. for (i = IEEE80211_WEP_NKID; i < sc->sc_keymax / 2; i++) {
  649. if (!test_bit(i, sc->sc_keymap) &&
  650. test_bit(i + 64, sc->sc_keymap))
  651. return i;
  652. if (test_bit(i, sc->sc_keymap) &&
  653. !test_bit(i + 64, sc->sc_keymap))
  654. return i + 64;
  655. }
  656. }
  657. /* No partially used TKIP slots, pick any available slot */
  658. for (i = IEEE80211_WEP_NKID; i < sc->sc_keymax; i++) {
  659. /* Do not allow slots that could be needed for TKIP group keys
  660. * to be used. This limitation could be removed if we know that
  661. * TKIP will not be used. */
  662. if (i >= 64 && i < 64 + IEEE80211_WEP_NKID)
  663. continue;
  664. if (sc->sc_splitmic) {
  665. if (i >= 32 && i < 32 + IEEE80211_WEP_NKID)
  666. continue;
  667. if (i >= 64 + 32 && i < 64 + 32 + IEEE80211_WEP_NKID)
  668. continue;
  669. }
  670. if (!test_bit(i, sc->sc_keymap))
  671. return i; /* Found a free slot for a key */
  672. }
  673. /* No free slot found */
  674. return -1;
  675. }
  676. static int ath_key_config(struct ath_softc *sc,
  677. struct ieee80211_sta *sta,
  678. struct ieee80211_key_conf *key)
  679. {
  680. struct ath9k_keyval hk;
  681. const u8 *mac = NULL;
  682. int ret = 0;
  683. int idx;
  684. memset(&hk, 0, sizeof(hk));
  685. switch (key->alg) {
  686. case ALG_WEP:
  687. hk.kv_type = ATH9K_CIPHER_WEP;
  688. break;
  689. case ALG_TKIP:
  690. hk.kv_type = ATH9K_CIPHER_TKIP;
  691. break;
  692. case ALG_CCMP:
  693. hk.kv_type = ATH9K_CIPHER_AES_CCM;
  694. break;
  695. default:
  696. return -EOPNOTSUPP;
  697. }
  698. hk.kv_len = key->keylen;
  699. memcpy(hk.kv_val, key->key, key->keylen);
  700. if (!(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  701. /* For now, use the default keys for broadcast keys. This may
  702. * need to change with virtual interfaces. */
  703. idx = key->keyidx;
  704. } else if (key->keyidx) {
  705. struct ieee80211_vif *vif;
  706. if (WARN_ON(!sta))
  707. return -EOPNOTSUPP;
  708. mac = sta->addr;
  709. vif = sc->sc_vaps[0];
  710. if (vif->type != NL80211_IFTYPE_AP) {
  711. /* Only keyidx 0 should be used with unicast key, but
  712. * allow this for client mode for now. */
  713. idx = key->keyidx;
  714. } else
  715. return -EIO;
  716. } else {
  717. if (WARN_ON(!sta))
  718. return -EOPNOTSUPP;
  719. mac = sta->addr;
  720. if (key->alg == ALG_TKIP)
  721. idx = ath_reserve_key_cache_slot_tkip(sc);
  722. else
  723. idx = ath_reserve_key_cache_slot(sc);
  724. if (idx < 0)
  725. return -ENOSPC; /* no free key cache entries */
  726. }
  727. if (key->alg == ALG_TKIP)
  728. ret = ath_setkey_tkip(sc, idx, key->key, &hk, mac);
  729. else
  730. ret = ath_keyset(sc, idx, &hk, mac);
  731. if (!ret)
  732. return -EIO;
  733. set_bit(idx, sc->sc_keymap);
  734. if (key->alg == ALG_TKIP) {
  735. set_bit(idx + 64, sc->sc_keymap);
  736. if (sc->sc_splitmic) {
  737. set_bit(idx + 32, sc->sc_keymap);
  738. set_bit(idx + 64 + 32, sc->sc_keymap);
  739. }
  740. }
  741. return idx;
  742. }
  743. static void ath_key_delete(struct ath_softc *sc, struct ieee80211_key_conf *key)
  744. {
  745. ath9k_hw_keyreset(sc->sc_ah, key->hw_key_idx);
  746. if (key->hw_key_idx < IEEE80211_WEP_NKID)
  747. return;
  748. clear_bit(key->hw_key_idx, sc->sc_keymap);
  749. if (key->alg != ALG_TKIP)
  750. return;
  751. clear_bit(key->hw_key_idx + 64, sc->sc_keymap);
  752. if (sc->sc_splitmic) {
  753. clear_bit(key->hw_key_idx + 32, sc->sc_keymap);
  754. clear_bit(key->hw_key_idx + 64 + 32, sc->sc_keymap);
  755. }
  756. }
  757. static void setup_ht_cap(struct ath_softc *sc,
  758. struct ieee80211_sta_ht_cap *ht_info)
  759. {
  760. #define ATH9K_HT_CAP_MAXRXAMPDU_65536 0x3 /* 2 ^ 16 */
  761. #define ATH9K_HT_CAP_MPDUDENSITY_8 0x6 /* 8 usec */
  762. ht_info->ht_supported = true;
  763. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  764. IEEE80211_HT_CAP_SM_PS |
  765. IEEE80211_HT_CAP_SGI_40 |
  766. IEEE80211_HT_CAP_DSSSCCK40;
  767. ht_info->ampdu_factor = ATH9K_HT_CAP_MAXRXAMPDU_65536;
  768. ht_info->ampdu_density = ATH9K_HT_CAP_MPDUDENSITY_8;
  769. /* set up supported mcs set */
  770. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  771. switch(sc->sc_rx_chainmask) {
  772. case 1:
  773. ht_info->mcs.rx_mask[0] = 0xff;
  774. break;
  775. case 3:
  776. case 5:
  777. case 7:
  778. default:
  779. ht_info->mcs.rx_mask[0] = 0xff;
  780. ht_info->mcs.rx_mask[1] = 0xff;
  781. break;
  782. }
  783. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  784. }
  785. static void ath9k_bss_assoc_info(struct ath_softc *sc,
  786. struct ieee80211_vif *vif,
  787. struct ieee80211_bss_conf *bss_conf)
  788. {
  789. struct ath_vap *avp = (void *)vif->drv_priv;
  790. if (bss_conf->assoc) {
  791. DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info ASSOC %d, bssid: %pM\n",
  792. bss_conf->aid, sc->sc_curbssid);
  793. /* New association, store aid */
  794. if (avp->av_opmode == NL80211_IFTYPE_STATION) {
  795. sc->sc_curaid = bss_conf->aid;
  796. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  797. sc->sc_curaid);
  798. }
  799. /* Configure the beacon */
  800. ath_beacon_config(sc, 0);
  801. sc->sc_flags |= SC_OP_BEACONS;
  802. /* Reset rssi stats */
  803. sc->sc_halstats.ns_avgbrssi = ATH_RSSI_DUMMY_MARKER;
  804. sc->sc_halstats.ns_avgrssi = ATH_RSSI_DUMMY_MARKER;
  805. sc->sc_halstats.ns_avgtxrssi = ATH_RSSI_DUMMY_MARKER;
  806. sc->sc_halstats.ns_avgtxrate = ATH_RATE_DUMMY_MARKER;
  807. /* Start ANI */
  808. mod_timer(&sc->sc_ani.timer,
  809. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  810. } else {
  811. DPRINTF(sc, ATH_DBG_CONFIG, "Bss Info DISSOC\n");
  812. sc->sc_curaid = 0;
  813. }
  814. }
  815. /********************************/
  816. /* LED functions */
  817. /********************************/
  818. static void ath_led_brightness(struct led_classdev *led_cdev,
  819. enum led_brightness brightness)
  820. {
  821. struct ath_led *led = container_of(led_cdev, struct ath_led, led_cdev);
  822. struct ath_softc *sc = led->sc;
  823. switch (brightness) {
  824. case LED_OFF:
  825. if (led->led_type == ATH_LED_ASSOC ||
  826. led->led_type == ATH_LED_RADIO)
  827. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  828. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN,
  829. (led->led_type == ATH_LED_RADIO) ? 1 :
  830. !!(sc->sc_flags & SC_OP_LED_ASSOCIATED));
  831. break;
  832. case LED_FULL:
  833. if (led->led_type == ATH_LED_ASSOC)
  834. sc->sc_flags |= SC_OP_LED_ASSOCIATED;
  835. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 0);
  836. break;
  837. default:
  838. break;
  839. }
  840. }
  841. static int ath_register_led(struct ath_softc *sc, struct ath_led *led,
  842. char *trigger)
  843. {
  844. int ret;
  845. led->sc = sc;
  846. led->led_cdev.name = led->name;
  847. led->led_cdev.default_trigger = trigger;
  848. led->led_cdev.brightness_set = ath_led_brightness;
  849. ret = led_classdev_register(wiphy_dev(sc->hw->wiphy), &led->led_cdev);
  850. if (ret)
  851. DPRINTF(sc, ATH_DBG_FATAL,
  852. "Failed to register led:%s", led->name);
  853. else
  854. led->registered = 1;
  855. return ret;
  856. }
  857. static void ath_unregister_led(struct ath_led *led)
  858. {
  859. if (led->registered) {
  860. led_classdev_unregister(&led->led_cdev);
  861. led->registered = 0;
  862. }
  863. }
  864. static void ath_deinit_leds(struct ath_softc *sc)
  865. {
  866. ath_unregister_led(&sc->assoc_led);
  867. sc->sc_flags &= ~SC_OP_LED_ASSOCIATED;
  868. ath_unregister_led(&sc->tx_led);
  869. ath_unregister_led(&sc->rx_led);
  870. ath_unregister_led(&sc->radio_led);
  871. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  872. }
  873. static void ath_init_leds(struct ath_softc *sc)
  874. {
  875. char *trigger;
  876. int ret;
  877. /* Configure gpio 1 for output */
  878. ath9k_hw_cfg_output(sc->sc_ah, ATH_LED_PIN,
  879. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  880. /* LED off, active low */
  881. ath9k_hw_set_gpio(sc->sc_ah, ATH_LED_PIN, 1);
  882. trigger = ieee80211_get_radio_led_name(sc->hw);
  883. snprintf(sc->radio_led.name, sizeof(sc->radio_led.name),
  884. "ath9k-%s:radio", wiphy_name(sc->hw->wiphy));
  885. ret = ath_register_led(sc, &sc->radio_led, trigger);
  886. sc->radio_led.led_type = ATH_LED_RADIO;
  887. if (ret)
  888. goto fail;
  889. trigger = ieee80211_get_assoc_led_name(sc->hw);
  890. snprintf(sc->assoc_led.name, sizeof(sc->assoc_led.name),
  891. "ath9k-%s:assoc", wiphy_name(sc->hw->wiphy));
  892. ret = ath_register_led(sc, &sc->assoc_led, trigger);
  893. sc->assoc_led.led_type = ATH_LED_ASSOC;
  894. if (ret)
  895. goto fail;
  896. trigger = ieee80211_get_tx_led_name(sc->hw);
  897. snprintf(sc->tx_led.name, sizeof(sc->tx_led.name),
  898. "ath9k-%s:tx", wiphy_name(sc->hw->wiphy));
  899. ret = ath_register_led(sc, &sc->tx_led, trigger);
  900. sc->tx_led.led_type = ATH_LED_TX;
  901. if (ret)
  902. goto fail;
  903. trigger = ieee80211_get_rx_led_name(sc->hw);
  904. snprintf(sc->rx_led.name, sizeof(sc->rx_led.name),
  905. "ath9k-%s:rx", wiphy_name(sc->hw->wiphy));
  906. ret = ath_register_led(sc, &sc->rx_led, trigger);
  907. sc->rx_led.led_type = ATH_LED_RX;
  908. if (ret)
  909. goto fail;
  910. return;
  911. fail:
  912. ath_deinit_leds(sc);
  913. }
  914. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  915. /*******************/
  916. /* Rfkill */
  917. /*******************/
  918. static void ath_radio_enable(struct ath_softc *sc)
  919. {
  920. struct ath_hal *ah = sc->sc_ah;
  921. struct ieee80211_channel *channel = sc->hw->conf.channel;
  922. int r;
  923. ath9k_ps_wakeup(sc);
  924. spin_lock_bh(&sc->sc_resetlock);
  925. r = ath9k_hw_reset(ah, ah->ah_curchan, false);
  926. if (r) {
  927. DPRINTF(sc, ATH_DBG_FATAL,
  928. "Unable to reset channel %u (%uMhz) ",
  929. "reset status %u\n",
  930. channel->center_freq, r);
  931. }
  932. spin_unlock_bh(&sc->sc_resetlock);
  933. ath_update_txpow(sc);
  934. if (ath_startrecv(sc) != 0) {
  935. DPRINTF(sc, ATH_DBG_FATAL,
  936. "Unable to restart recv logic\n");
  937. return;
  938. }
  939. if (sc->sc_flags & SC_OP_BEACONS)
  940. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  941. /* Re-Enable interrupts */
  942. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  943. /* Enable LED */
  944. ath9k_hw_cfg_output(ah, ATH_LED_PIN,
  945. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  946. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 0);
  947. ieee80211_wake_queues(sc->hw);
  948. ath9k_ps_restore(sc);
  949. }
  950. static void ath_radio_disable(struct ath_softc *sc)
  951. {
  952. struct ath_hal *ah = sc->sc_ah;
  953. struct ieee80211_channel *channel = sc->hw->conf.channel;
  954. int r;
  955. ath9k_ps_wakeup(sc);
  956. ieee80211_stop_queues(sc->hw);
  957. /* Disable LED */
  958. ath9k_hw_set_gpio(ah, ATH_LED_PIN, 1);
  959. ath9k_hw_cfg_gpio_input(ah, ATH_LED_PIN);
  960. /* Disable interrupts */
  961. ath9k_hw_set_interrupts(ah, 0);
  962. ath_drain_all_txq(sc, false); /* clear pending tx frames */
  963. ath_stoprecv(sc); /* turn off frame recv */
  964. ath_flushrecv(sc); /* flush recv queue */
  965. spin_lock_bh(&sc->sc_resetlock);
  966. r = ath9k_hw_reset(ah, ah->ah_curchan, false);
  967. if (r) {
  968. DPRINTF(sc, ATH_DBG_FATAL,
  969. "Unable to reset channel %u (%uMhz) "
  970. "reset status %u\n",
  971. channel->center_freq, r);
  972. }
  973. spin_unlock_bh(&sc->sc_resetlock);
  974. ath9k_hw_phy_disable(ah);
  975. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  976. ath9k_ps_restore(sc);
  977. }
  978. static bool ath_is_rfkill_set(struct ath_softc *sc)
  979. {
  980. struct ath_hal *ah = sc->sc_ah;
  981. return ath9k_hw_gpio_get(ah, ah->ah_rfkill_gpio) ==
  982. ah->ah_rfkill_polarity;
  983. }
  984. /* h/w rfkill poll function */
  985. static void ath_rfkill_poll(struct work_struct *work)
  986. {
  987. struct ath_softc *sc = container_of(work, struct ath_softc,
  988. rf_kill.rfkill_poll.work);
  989. bool radio_on;
  990. if (sc->sc_flags & SC_OP_INVALID)
  991. return;
  992. radio_on = !ath_is_rfkill_set(sc);
  993. /*
  994. * enable/disable radio only when there is a
  995. * state change in RF switch
  996. */
  997. if (radio_on == !!(sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED)) {
  998. enum rfkill_state state;
  999. if (sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED) {
  1000. state = radio_on ? RFKILL_STATE_SOFT_BLOCKED
  1001. : RFKILL_STATE_HARD_BLOCKED;
  1002. } else if (radio_on) {
  1003. ath_radio_enable(sc);
  1004. state = RFKILL_STATE_UNBLOCKED;
  1005. } else {
  1006. ath_radio_disable(sc);
  1007. state = RFKILL_STATE_HARD_BLOCKED;
  1008. }
  1009. if (state == RFKILL_STATE_HARD_BLOCKED)
  1010. sc->sc_flags |= SC_OP_RFKILL_HW_BLOCKED;
  1011. else
  1012. sc->sc_flags &= ~SC_OP_RFKILL_HW_BLOCKED;
  1013. rfkill_force_state(sc->rf_kill.rfkill, state);
  1014. }
  1015. queue_delayed_work(sc->hw->workqueue, &sc->rf_kill.rfkill_poll,
  1016. msecs_to_jiffies(ATH_RFKILL_POLL_INTERVAL));
  1017. }
  1018. /* s/w rfkill handler */
  1019. static int ath_sw_toggle_radio(void *data, enum rfkill_state state)
  1020. {
  1021. struct ath_softc *sc = data;
  1022. switch (state) {
  1023. case RFKILL_STATE_SOFT_BLOCKED:
  1024. if (!(sc->sc_flags & (SC_OP_RFKILL_HW_BLOCKED |
  1025. SC_OP_RFKILL_SW_BLOCKED)))
  1026. ath_radio_disable(sc);
  1027. sc->sc_flags |= SC_OP_RFKILL_SW_BLOCKED;
  1028. return 0;
  1029. case RFKILL_STATE_UNBLOCKED:
  1030. if ((sc->sc_flags & SC_OP_RFKILL_SW_BLOCKED)) {
  1031. sc->sc_flags &= ~SC_OP_RFKILL_SW_BLOCKED;
  1032. if (sc->sc_flags & SC_OP_RFKILL_HW_BLOCKED) {
  1033. DPRINTF(sc, ATH_DBG_FATAL, "Can't turn on the"
  1034. "radio as it is disabled by h/w\n");
  1035. return -EPERM;
  1036. }
  1037. ath_radio_enable(sc);
  1038. }
  1039. return 0;
  1040. default:
  1041. return -EINVAL;
  1042. }
  1043. }
  1044. /* Init s/w rfkill */
  1045. static int ath_init_sw_rfkill(struct ath_softc *sc)
  1046. {
  1047. sc->rf_kill.rfkill = rfkill_allocate(wiphy_dev(sc->hw->wiphy),
  1048. RFKILL_TYPE_WLAN);
  1049. if (!sc->rf_kill.rfkill) {
  1050. DPRINTF(sc, ATH_DBG_FATAL, "Failed to allocate rfkill\n");
  1051. return -ENOMEM;
  1052. }
  1053. snprintf(sc->rf_kill.rfkill_name, sizeof(sc->rf_kill.rfkill_name),
  1054. "ath9k-%s:rfkill", wiphy_name(sc->hw->wiphy));
  1055. sc->rf_kill.rfkill->name = sc->rf_kill.rfkill_name;
  1056. sc->rf_kill.rfkill->data = sc;
  1057. sc->rf_kill.rfkill->toggle_radio = ath_sw_toggle_radio;
  1058. sc->rf_kill.rfkill->state = RFKILL_STATE_UNBLOCKED;
  1059. sc->rf_kill.rfkill->user_claim_unsupported = 1;
  1060. return 0;
  1061. }
  1062. /* Deinitialize rfkill */
  1063. static void ath_deinit_rfkill(struct ath_softc *sc)
  1064. {
  1065. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1066. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1067. if (sc->sc_flags & SC_OP_RFKILL_REGISTERED) {
  1068. rfkill_unregister(sc->rf_kill.rfkill);
  1069. sc->sc_flags &= ~SC_OP_RFKILL_REGISTERED;
  1070. sc->rf_kill.rfkill = NULL;
  1071. }
  1072. }
  1073. static int ath_start_rfkill_poll(struct ath_softc *sc)
  1074. {
  1075. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1076. queue_delayed_work(sc->hw->workqueue,
  1077. &sc->rf_kill.rfkill_poll, 0);
  1078. if (!(sc->sc_flags & SC_OP_RFKILL_REGISTERED)) {
  1079. if (rfkill_register(sc->rf_kill.rfkill)) {
  1080. DPRINTF(sc, ATH_DBG_FATAL,
  1081. "Unable to register rfkill\n");
  1082. rfkill_free(sc->rf_kill.rfkill);
  1083. /* Deinitialize the device */
  1084. ath_cleanup(sc);
  1085. return -EIO;
  1086. } else {
  1087. sc->sc_flags |= SC_OP_RFKILL_REGISTERED;
  1088. }
  1089. }
  1090. return 0;
  1091. }
  1092. #endif /* CONFIG_RFKILL */
  1093. void ath_cleanup(struct ath_softc *sc)
  1094. {
  1095. ath_detach(sc);
  1096. free_irq(sc->irq, sc);
  1097. ath_bus_cleanup(sc);
  1098. ieee80211_free_hw(sc->hw);
  1099. }
  1100. void ath_detach(struct ath_softc *sc)
  1101. {
  1102. struct ieee80211_hw *hw = sc->hw;
  1103. int i = 0;
  1104. ath9k_ps_wakeup(sc);
  1105. DPRINTF(sc, ATH_DBG_CONFIG, "Detach ATH hw\n");
  1106. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1107. ath_deinit_rfkill(sc);
  1108. #endif
  1109. ath_deinit_leds(sc);
  1110. ieee80211_unregister_hw(hw);
  1111. ath_rx_cleanup(sc);
  1112. ath_tx_cleanup(sc);
  1113. tasklet_kill(&sc->intr_tq);
  1114. tasklet_kill(&sc->bcon_tasklet);
  1115. if (!(sc->sc_flags & SC_OP_INVALID))
  1116. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  1117. /* cleanup tx queues */
  1118. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1119. if (ATH_TXQ_SETUP(sc, i))
  1120. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1121. ath9k_hw_detach(sc->sc_ah);
  1122. ath9k_exit_debug(sc);
  1123. ath9k_ps_restore(sc);
  1124. }
  1125. static int ath_init(u16 devid, struct ath_softc *sc)
  1126. {
  1127. struct ath_hal *ah = NULL;
  1128. int status;
  1129. int error = 0, i;
  1130. int csz = 0;
  1131. /* XXX: hardware will not be ready until ath_open() being called */
  1132. sc->sc_flags |= SC_OP_INVALID;
  1133. if (ath9k_init_debug(sc) < 0)
  1134. printk(KERN_ERR "Unable to create debugfs files\n");
  1135. spin_lock_init(&sc->sc_resetlock);
  1136. mutex_init(&sc->mutex);
  1137. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  1138. tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
  1139. (unsigned long)sc);
  1140. /*
  1141. * Cache line size is used to size and align various
  1142. * structures used to communicate with the hardware.
  1143. */
  1144. ath_read_cachesize(sc, &csz);
  1145. /* XXX assert csz is non-zero */
  1146. sc->sc_cachelsz = csz << 2; /* convert to bytes */
  1147. ah = ath9k_hw_attach(devid, sc, sc->mem, &status);
  1148. if (ah == NULL) {
  1149. DPRINTF(sc, ATH_DBG_FATAL,
  1150. "Unable to attach hardware; HAL status %d\n", status);
  1151. error = -ENXIO;
  1152. goto bad;
  1153. }
  1154. sc->sc_ah = ah;
  1155. /* Get the hardware key cache size. */
  1156. sc->sc_keymax = ah->ah_caps.keycache_size;
  1157. if (sc->sc_keymax > ATH_KEYMAX) {
  1158. DPRINTF(sc, ATH_DBG_KEYCACHE,
  1159. "Warning, using only %u entries in %u key cache\n",
  1160. ATH_KEYMAX, sc->sc_keymax);
  1161. sc->sc_keymax = ATH_KEYMAX;
  1162. }
  1163. /*
  1164. * Reset the key cache since some parts do not
  1165. * reset the contents on initial power up.
  1166. */
  1167. for (i = 0; i < sc->sc_keymax; i++)
  1168. ath9k_hw_keyreset(ah, (u16) i);
  1169. if (ath9k_regd_init(sc->sc_ah))
  1170. goto bad;
  1171. /* default to MONITOR mode */
  1172. sc->sc_ah->ah_opmode = NL80211_IFTYPE_MONITOR;
  1173. /* Setup rate tables */
  1174. ath_rate_attach(sc);
  1175. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  1176. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  1177. /*
  1178. * Allocate hardware transmit queues: one queue for
  1179. * beacon frames and one data queue for each QoS
  1180. * priority. Note that the hal handles reseting
  1181. * these queues at the needed time.
  1182. */
  1183. sc->beacon.beaconq = ath_beaconq_setup(ah);
  1184. if (sc->beacon.beaconq == -1) {
  1185. DPRINTF(sc, ATH_DBG_FATAL,
  1186. "Unable to setup a beacon xmit queue\n");
  1187. error = -EIO;
  1188. goto bad2;
  1189. }
  1190. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  1191. if (sc->beacon.cabq == NULL) {
  1192. DPRINTF(sc, ATH_DBG_FATAL,
  1193. "Unable to setup CAB xmit queue\n");
  1194. error = -EIO;
  1195. goto bad2;
  1196. }
  1197. sc->sc_config.cabqReadytime = ATH_CABQ_READY_TIME;
  1198. ath_cabq_update(sc);
  1199. for (i = 0; i < ARRAY_SIZE(sc->tx.hwq_map); i++)
  1200. sc->tx.hwq_map[i] = -1;
  1201. /* Setup data queues */
  1202. /* NB: ensure BK queue is the lowest priority h/w queue */
  1203. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  1204. DPRINTF(sc, ATH_DBG_FATAL,
  1205. "Unable to setup xmit queue for BK traffic\n");
  1206. error = -EIO;
  1207. goto bad2;
  1208. }
  1209. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  1210. DPRINTF(sc, ATH_DBG_FATAL,
  1211. "Unable to setup xmit queue for BE traffic\n");
  1212. error = -EIO;
  1213. goto bad2;
  1214. }
  1215. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  1216. DPRINTF(sc, ATH_DBG_FATAL,
  1217. "Unable to setup xmit queue for VI traffic\n");
  1218. error = -EIO;
  1219. goto bad2;
  1220. }
  1221. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  1222. DPRINTF(sc, ATH_DBG_FATAL,
  1223. "Unable to setup xmit queue for VO traffic\n");
  1224. error = -EIO;
  1225. goto bad2;
  1226. }
  1227. /* Initializes the noise floor to a reasonable default value.
  1228. * Later on this will be updated during ANI processing. */
  1229. sc->sc_ani.sc_noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  1230. setup_timer(&sc->sc_ani.timer, ath_ani_calibrate, (unsigned long)sc);
  1231. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1232. ATH9K_CIPHER_TKIP, NULL)) {
  1233. /*
  1234. * Whether we should enable h/w TKIP MIC.
  1235. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  1236. * report WMM capable, so it's always safe to turn on
  1237. * TKIP MIC in this case.
  1238. */
  1239. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  1240. 0, 1, NULL);
  1241. }
  1242. /*
  1243. * Check whether the separate key cache entries
  1244. * are required to handle both tx+rx MIC keys.
  1245. * With split mic keys the number of stations is limited
  1246. * to 27 otherwise 59.
  1247. */
  1248. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1249. ATH9K_CIPHER_TKIP, NULL)
  1250. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  1251. ATH9K_CIPHER_MIC, NULL)
  1252. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  1253. 0, NULL))
  1254. sc->sc_splitmic = 1;
  1255. /* turn on mcast key search if possible */
  1256. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  1257. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  1258. 1, NULL);
  1259. sc->sc_config.txpowlimit = ATH_TXPOWER_MAX;
  1260. sc->sc_config.txpowlimit_override = 0;
  1261. /* 11n Capabilities */
  1262. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
  1263. sc->sc_flags |= SC_OP_TXAGGR;
  1264. sc->sc_flags |= SC_OP_RXAGGR;
  1265. }
  1266. sc->sc_tx_chainmask = ah->ah_caps.tx_chainmask;
  1267. sc->sc_rx_chainmask = ah->ah_caps.rx_chainmask;
  1268. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  1269. sc->rx.defant = ath9k_hw_getdefantenna(ah);
  1270. ath9k_hw_getmac(ah, sc->sc_myaddr);
  1271. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) {
  1272. ath9k_hw_getbssidmask(ah, sc->sc_bssidmask);
  1273. ATH_SET_VAP_BSSID_MASK(sc->sc_bssidmask);
  1274. ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
  1275. }
  1276. sc->beacon.slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  1277. /* initialize beacon slots */
  1278. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++)
  1279. sc->beacon.bslot[i] = ATH_IF_ID_ANY;
  1280. /* save MISC configurations */
  1281. sc->sc_config.swBeaconProcess = 1;
  1282. /* setup channels and rates */
  1283. sc->sbands[IEEE80211_BAND_2GHZ].channels = ath9k_2ghz_chantable;
  1284. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  1285. sc->rates[IEEE80211_BAND_2GHZ];
  1286. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  1287. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  1288. ARRAY_SIZE(ath9k_2ghz_chantable);
  1289. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
  1290. sc->sbands[IEEE80211_BAND_5GHZ].channels = ath9k_5ghz_chantable;
  1291. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  1292. sc->rates[IEEE80211_BAND_5GHZ];
  1293. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1294. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  1295. ARRAY_SIZE(ath9k_5ghz_chantable);
  1296. }
  1297. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_BT_COEX)
  1298. ath9k_hw_btcoex_enable(sc->sc_ah);
  1299. return 0;
  1300. bad2:
  1301. /* cleanup tx queues */
  1302. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1303. if (ATH_TXQ_SETUP(sc, i))
  1304. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  1305. bad:
  1306. if (ah)
  1307. ath9k_hw_detach(ah);
  1308. return error;
  1309. }
  1310. int ath_attach(u16 devid, struct ath_softc *sc)
  1311. {
  1312. struct ieee80211_hw *hw = sc->hw;
  1313. int error = 0;
  1314. DPRINTF(sc, ATH_DBG_CONFIG, "Attach ATH hw\n");
  1315. error = ath_init(devid, sc);
  1316. if (error != 0)
  1317. return error;
  1318. /* get mac address from hardware and set in mac80211 */
  1319. SET_IEEE80211_PERM_ADDR(hw, sc->sc_myaddr);
  1320. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  1321. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  1322. IEEE80211_HW_SIGNAL_DBM |
  1323. IEEE80211_HW_AMPDU_AGGREGATION |
  1324. IEEE80211_HW_SUPPORTS_PS |
  1325. IEEE80211_HW_PS_NULLFUNC_STACK;
  1326. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah))
  1327. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1328. hw->wiphy->interface_modes =
  1329. BIT(NL80211_IFTYPE_AP) |
  1330. BIT(NL80211_IFTYPE_STATION) |
  1331. BIT(NL80211_IFTYPE_ADHOC);
  1332. hw->wiphy->reg_notifier = ath9k_reg_notifier;
  1333. hw->wiphy->strict_regulatory = true;
  1334. hw->queues = 4;
  1335. hw->max_rates = 4;
  1336. hw->max_rate_tries = ATH_11N_TXMAXTRY;
  1337. hw->sta_data_size = sizeof(struct ath_node);
  1338. hw->vif_data_size = sizeof(struct ath_vap);
  1339. hw->rate_control_algorithm = "ath9k_rate_control";
  1340. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
  1341. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  1342. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
  1343. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  1344. }
  1345. hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &sc->sbands[IEEE80211_BAND_2GHZ];
  1346. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes))
  1347. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1348. &sc->sbands[IEEE80211_BAND_5GHZ];
  1349. /* initialize tx/rx engine */
  1350. error = ath_tx_init(sc, ATH_TXBUF);
  1351. if (error != 0)
  1352. goto detach;
  1353. error = ath_rx_init(sc, ATH_RXBUF);
  1354. if (error != 0)
  1355. goto detach;
  1356. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1357. /* Initialze h/w Rfkill */
  1358. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1359. INIT_DELAYED_WORK(&sc->rf_kill.rfkill_poll, ath_rfkill_poll);
  1360. /* Initialize s/w rfkill */
  1361. if (ath_init_sw_rfkill(sc))
  1362. goto detach;
  1363. #endif
  1364. if (ath9k_is_world_regd(sc->sc_ah)) {
  1365. /* Anything applied here (prior to wiphy registratoin) gets
  1366. * saved on the wiphy orig_* parameters */
  1367. const struct ieee80211_regdomain *regd =
  1368. ath9k_world_regdomain(sc->sc_ah);
  1369. hw->wiphy->custom_regulatory = true;
  1370. hw->wiphy->strict_regulatory = false;
  1371. wiphy_apply_custom_regulatory(sc->hw->wiphy, regd);
  1372. ath9k_reg_apply_radar_flags(hw->wiphy);
  1373. ath9k_reg_apply_world_flags(hw->wiphy, REGDOM_SET_BY_INIT);
  1374. } else {
  1375. /* This gets applied in the case of the absense of CRDA,
  1376. * its our own custom world regulatory domain, similar to
  1377. * cfg80211's but we enable passive scanning */
  1378. const struct ieee80211_regdomain *regd =
  1379. ath9k_default_world_regdomain();
  1380. wiphy_apply_custom_regulatory(sc->hw->wiphy, regd);
  1381. ath9k_reg_apply_radar_flags(hw->wiphy);
  1382. ath9k_reg_apply_world_flags(hw->wiphy, REGDOM_SET_BY_INIT);
  1383. }
  1384. error = ieee80211_register_hw(hw);
  1385. if (!ath9k_is_world_regd(sc->sc_ah))
  1386. regulatory_hint(hw->wiphy, sc->sc_ah->alpha2);
  1387. /* Initialize LED control */
  1388. ath_init_leds(sc);
  1389. return 0;
  1390. detach:
  1391. ath_detach(sc);
  1392. return error;
  1393. }
  1394. int ath_reset(struct ath_softc *sc, bool retry_tx)
  1395. {
  1396. struct ath_hal *ah = sc->sc_ah;
  1397. struct ieee80211_hw *hw = sc->hw;
  1398. int r;
  1399. ath9k_hw_set_interrupts(ah, 0);
  1400. ath_drain_all_txq(sc, retry_tx);
  1401. ath_stoprecv(sc);
  1402. ath_flushrecv(sc);
  1403. spin_lock_bh(&sc->sc_resetlock);
  1404. r = ath9k_hw_reset(ah, sc->sc_ah->ah_curchan, false);
  1405. if (r)
  1406. DPRINTF(sc, ATH_DBG_FATAL,
  1407. "Unable to reset hardware; reset status %u\n", r);
  1408. spin_unlock_bh(&sc->sc_resetlock);
  1409. if (ath_startrecv(sc) != 0)
  1410. DPRINTF(sc, ATH_DBG_FATAL, "Unable to start recv logic\n");
  1411. /*
  1412. * We may be doing a reset in response to a request
  1413. * that changes the channel so update any state that
  1414. * might change as a result.
  1415. */
  1416. ath_cache_conf_rate(sc, &hw->conf);
  1417. ath_update_txpow(sc);
  1418. if (sc->sc_flags & SC_OP_BEACONS)
  1419. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  1420. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  1421. if (retry_tx) {
  1422. int i;
  1423. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  1424. if (ATH_TXQ_SETUP(sc, i)) {
  1425. spin_lock_bh(&sc->tx.txq[i].axq_lock);
  1426. ath_txq_schedule(sc, &sc->tx.txq[i]);
  1427. spin_unlock_bh(&sc->tx.txq[i].axq_lock);
  1428. }
  1429. }
  1430. }
  1431. return r;
  1432. }
  1433. /*
  1434. * This function will allocate both the DMA descriptor structure, and the
  1435. * buffers it contains. These are used to contain the descriptors used
  1436. * by the system.
  1437. */
  1438. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  1439. struct list_head *head, const char *name,
  1440. int nbuf, int ndesc)
  1441. {
  1442. #define DS2PHYS(_dd, _ds) \
  1443. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1444. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1445. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1446. struct ath_desc *ds;
  1447. struct ath_buf *bf;
  1448. int i, bsize, error;
  1449. DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  1450. name, nbuf, ndesc);
  1451. /* ath_desc must be a multiple of DWORDs */
  1452. if ((sizeof(struct ath_desc) % 4) != 0) {
  1453. DPRINTF(sc, ATH_DBG_FATAL, "ath_desc not DWORD aligned\n");
  1454. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1455. error = -ENOMEM;
  1456. goto fail;
  1457. }
  1458. dd->dd_name = name;
  1459. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1460. /*
  1461. * Need additional DMA memory because we can't use
  1462. * descriptors that cross the 4K page boundary. Assume
  1463. * one skipped descriptor per 4K page.
  1464. */
  1465. if (!(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1466. u32 ndesc_skipped =
  1467. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1468. u32 dma_len;
  1469. while (ndesc_skipped) {
  1470. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1471. dd->dd_desc_len += dma_len;
  1472. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1473. };
  1474. }
  1475. /* allocate descriptors */
  1476. dd->dd_desc = dma_alloc_coherent(sc->dev, dd->dd_desc_len,
  1477. &dd->dd_desc_paddr, GFP_ATOMIC);
  1478. if (dd->dd_desc == NULL) {
  1479. error = -ENOMEM;
  1480. goto fail;
  1481. }
  1482. ds = dd->dd_desc;
  1483. DPRINTF(sc, ATH_DBG_CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  1484. dd->dd_name, ds, (u32) dd->dd_desc_len,
  1485. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1486. /* allocate buffers */
  1487. bsize = sizeof(struct ath_buf) * nbuf;
  1488. bf = kmalloc(bsize, GFP_KERNEL);
  1489. if (bf == NULL) {
  1490. error = -ENOMEM;
  1491. goto fail2;
  1492. }
  1493. memset(bf, 0, bsize);
  1494. dd->dd_bufptr = bf;
  1495. INIT_LIST_HEAD(head);
  1496. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1497. bf->bf_desc = ds;
  1498. bf->bf_daddr = DS2PHYS(dd, ds);
  1499. if (!(sc->sc_ah->ah_caps.hw_caps &
  1500. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1501. /*
  1502. * Skip descriptor addresses which can cause 4KB
  1503. * boundary crossing (addr + length) with a 32 dword
  1504. * descriptor fetch.
  1505. */
  1506. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1507. ASSERT((caddr_t) bf->bf_desc <
  1508. ((caddr_t) dd->dd_desc +
  1509. dd->dd_desc_len));
  1510. ds += ndesc;
  1511. bf->bf_desc = ds;
  1512. bf->bf_daddr = DS2PHYS(dd, ds);
  1513. }
  1514. }
  1515. list_add_tail(&bf->list, head);
  1516. }
  1517. return 0;
  1518. fail2:
  1519. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1520. dd->dd_desc_paddr);
  1521. fail:
  1522. memset(dd, 0, sizeof(*dd));
  1523. return error;
  1524. #undef ATH_DESC_4KB_BOUND_CHECK
  1525. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1526. #undef DS2PHYS
  1527. }
  1528. void ath_descdma_cleanup(struct ath_softc *sc,
  1529. struct ath_descdma *dd,
  1530. struct list_head *head)
  1531. {
  1532. dma_free_coherent(sc->dev, dd->dd_desc_len, dd->dd_desc,
  1533. dd->dd_desc_paddr);
  1534. INIT_LIST_HEAD(head);
  1535. kfree(dd->dd_bufptr);
  1536. memset(dd, 0, sizeof(*dd));
  1537. }
  1538. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1539. {
  1540. int qnum;
  1541. switch (queue) {
  1542. case 0:
  1543. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VO];
  1544. break;
  1545. case 1:
  1546. qnum = sc->tx.hwq_map[ATH9K_WME_AC_VI];
  1547. break;
  1548. case 2:
  1549. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1550. break;
  1551. case 3:
  1552. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BK];
  1553. break;
  1554. default:
  1555. qnum = sc->tx.hwq_map[ATH9K_WME_AC_BE];
  1556. break;
  1557. }
  1558. return qnum;
  1559. }
  1560. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1561. {
  1562. int qnum;
  1563. switch (queue) {
  1564. case ATH9K_WME_AC_VO:
  1565. qnum = 0;
  1566. break;
  1567. case ATH9K_WME_AC_VI:
  1568. qnum = 1;
  1569. break;
  1570. case ATH9K_WME_AC_BE:
  1571. qnum = 2;
  1572. break;
  1573. case ATH9K_WME_AC_BK:
  1574. qnum = 3;
  1575. break;
  1576. default:
  1577. qnum = -1;
  1578. break;
  1579. }
  1580. return qnum;
  1581. }
  1582. /* XXX: Remove me once we don't depend on ath9k_channel for all
  1583. * this redundant data */
  1584. static void ath9k_update_ichannel(struct ath_softc *sc,
  1585. struct ath9k_channel *ichan)
  1586. {
  1587. struct ieee80211_hw *hw = sc->hw;
  1588. struct ieee80211_channel *chan = hw->conf.channel;
  1589. struct ieee80211_conf *conf = &hw->conf;
  1590. ichan->channel = chan->center_freq;
  1591. ichan->chan = chan;
  1592. if (chan->band == IEEE80211_BAND_2GHZ) {
  1593. ichan->chanmode = CHANNEL_G;
  1594. ichan->channelFlags = CHANNEL_2GHZ | CHANNEL_OFDM;
  1595. } else {
  1596. ichan->chanmode = CHANNEL_A;
  1597. ichan->channelFlags = CHANNEL_5GHZ | CHANNEL_OFDM;
  1598. }
  1599. sc->tx_chan_width = ATH9K_HT_MACMODE_20;
  1600. if (conf_is_ht(conf)) {
  1601. if (conf_is_ht40(conf))
  1602. sc->tx_chan_width = ATH9K_HT_MACMODE_2040;
  1603. ichan->chanmode = ath_get_extchanmode(sc, chan,
  1604. conf->channel_type);
  1605. }
  1606. }
  1607. /**********************/
  1608. /* mac80211 callbacks */
  1609. /**********************/
  1610. static int ath9k_start(struct ieee80211_hw *hw)
  1611. {
  1612. struct ath_softc *sc = hw->priv;
  1613. struct ieee80211_channel *curchan = hw->conf.channel;
  1614. struct ath9k_channel *init_channel;
  1615. int r, pos;
  1616. DPRINTF(sc, ATH_DBG_CONFIG, "Starting driver with "
  1617. "initial channel: %d MHz\n", curchan->center_freq);
  1618. /* setup initial channel */
  1619. pos = curchan->hw_value;
  1620. init_channel = &sc->sc_ah->ah_channels[pos];
  1621. ath9k_update_ichannel(sc, init_channel);
  1622. /* Reset SERDES registers */
  1623. ath9k_hw_configpcipowersave(sc->sc_ah, 0);
  1624. /*
  1625. * The basic interface to setting the hardware in a good
  1626. * state is ``reset''. On return the hardware is known to
  1627. * be powered up and with interrupts disabled. This must
  1628. * be followed by initialization of the appropriate bits
  1629. * and then setup of the interrupt mask.
  1630. */
  1631. spin_lock_bh(&sc->sc_resetlock);
  1632. r = ath9k_hw_reset(sc->sc_ah, init_channel, false);
  1633. if (r) {
  1634. DPRINTF(sc, ATH_DBG_FATAL,
  1635. "Unable to reset hardware; reset status %u "
  1636. "(freq %u MHz)\n", r,
  1637. curchan->center_freq);
  1638. spin_unlock_bh(&sc->sc_resetlock);
  1639. return r;
  1640. }
  1641. spin_unlock_bh(&sc->sc_resetlock);
  1642. /*
  1643. * This is needed only to setup initial state
  1644. * but it's best done after a reset.
  1645. */
  1646. ath_update_txpow(sc);
  1647. /*
  1648. * Setup the hardware after reset:
  1649. * The receive engine is set going.
  1650. * Frame transmit is handled entirely
  1651. * in the frame output path; there's nothing to do
  1652. * here except setup the interrupt mask.
  1653. */
  1654. if (ath_startrecv(sc) != 0) {
  1655. DPRINTF(sc, ATH_DBG_FATAL,
  1656. "Unable to start recv logic\n");
  1657. return -EIO;
  1658. }
  1659. /* Setup our intr mask. */
  1660. sc->sc_imask = ATH9K_INT_RX | ATH9K_INT_TX
  1661. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  1662. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  1663. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_GTT)
  1664. sc->sc_imask |= ATH9K_INT_GTT;
  1665. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
  1666. sc->sc_imask |= ATH9K_INT_CST;
  1667. /*
  1668. * Enable MIB interrupts when there are hardware phy counters.
  1669. * Note we only do this (at the moment) for station mode.
  1670. */
  1671. if (ath9k_hw_phycounters(sc->sc_ah) &&
  1672. ((sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION) ||
  1673. (sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC)))
  1674. sc->sc_imask |= ATH9K_INT_MIB;
  1675. /*
  1676. * Some hardware processes the TIM IE and fires an
  1677. * interrupt when the TIM bit is set. For hardware
  1678. * that does, if not overridden by configuration,
  1679. * enable the TIM interrupt when operating as station.
  1680. */
  1681. if ((sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_ENHANCEDPM) &&
  1682. (sc->sc_ah->ah_opmode == NL80211_IFTYPE_STATION) &&
  1683. !sc->sc_config.swBeaconProcess)
  1684. sc->sc_imask |= ATH9K_INT_TIM;
  1685. ath_cache_conf_rate(sc, &hw->conf);
  1686. sc->sc_flags &= ~SC_OP_INVALID;
  1687. /* Disable BMISS interrupt when we're not associated */
  1688. sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  1689. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
  1690. ieee80211_wake_queues(sc->hw);
  1691. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1692. r = ath_start_rfkill_poll(sc);
  1693. #endif
  1694. return r;
  1695. }
  1696. static int ath9k_tx(struct ieee80211_hw *hw,
  1697. struct sk_buff *skb)
  1698. {
  1699. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1700. struct ath_softc *sc = hw->priv;
  1701. struct ath_tx_control txctl;
  1702. int hdrlen, padsize;
  1703. memset(&txctl, 0, sizeof(struct ath_tx_control));
  1704. /*
  1705. * As a temporary workaround, assign seq# here; this will likely need
  1706. * to be cleaned up to work better with Beacon transmission and virtual
  1707. * BSSes.
  1708. */
  1709. if (info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ) {
  1710. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1711. if (info->flags & IEEE80211_TX_CTL_FIRST_FRAGMENT)
  1712. sc->tx.seq_no += 0x10;
  1713. hdr->seq_ctrl &= cpu_to_le16(IEEE80211_SCTL_FRAG);
  1714. hdr->seq_ctrl |= cpu_to_le16(sc->tx.seq_no);
  1715. }
  1716. /* Add the padding after the header if this is not already done */
  1717. hdrlen = ieee80211_get_hdrlen_from_skb(skb);
  1718. if (hdrlen & 3) {
  1719. padsize = hdrlen % 4;
  1720. if (skb_headroom(skb) < padsize)
  1721. return -1;
  1722. skb_push(skb, padsize);
  1723. memmove(skb->data, skb->data + padsize, hdrlen);
  1724. }
  1725. /* Check if a tx queue is available */
  1726. txctl.txq = ath_test_get_txq(sc, skb);
  1727. if (!txctl.txq)
  1728. goto exit;
  1729. DPRINTF(sc, ATH_DBG_XMIT, "transmitting packet, skb: %p\n", skb);
  1730. if (ath_tx_start(sc, skb, &txctl) != 0) {
  1731. DPRINTF(sc, ATH_DBG_XMIT, "TX failed\n");
  1732. goto exit;
  1733. }
  1734. return 0;
  1735. exit:
  1736. dev_kfree_skb_any(skb);
  1737. return 0;
  1738. }
  1739. static void ath9k_stop(struct ieee80211_hw *hw)
  1740. {
  1741. struct ath_softc *sc = hw->priv;
  1742. if (sc->sc_flags & SC_OP_INVALID) {
  1743. DPRINTF(sc, ATH_DBG_ANY, "Device not present\n");
  1744. return;
  1745. }
  1746. DPRINTF(sc, ATH_DBG_CONFIG, "Cleaning up\n");
  1747. ieee80211_stop_queues(sc->hw);
  1748. /* make sure h/w will not generate any interrupt
  1749. * before setting the invalid flag. */
  1750. ath9k_hw_set_interrupts(sc->sc_ah, 0);
  1751. if (!(sc->sc_flags & SC_OP_INVALID)) {
  1752. ath_drain_all_txq(sc, false);
  1753. ath_stoprecv(sc);
  1754. ath9k_hw_phy_disable(sc->sc_ah);
  1755. } else
  1756. sc->rx.rxlink = NULL;
  1757. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1758. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1759. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  1760. #endif
  1761. /* disable HAL and put h/w to sleep */
  1762. ath9k_hw_disable(sc->sc_ah);
  1763. ath9k_hw_configpcipowersave(sc->sc_ah, 1);
  1764. sc->sc_flags |= SC_OP_INVALID;
  1765. DPRINTF(sc, ATH_DBG_CONFIG, "Driver halt\n");
  1766. }
  1767. static int ath9k_add_interface(struct ieee80211_hw *hw,
  1768. struct ieee80211_if_init_conf *conf)
  1769. {
  1770. struct ath_softc *sc = hw->priv;
  1771. struct ath_vap *avp = (void *)conf->vif->drv_priv;
  1772. enum nl80211_iftype ic_opmode = NL80211_IFTYPE_UNSPECIFIED;
  1773. /* Support only vap for now */
  1774. if (sc->sc_nvaps)
  1775. return -ENOBUFS;
  1776. switch (conf->type) {
  1777. case NL80211_IFTYPE_STATION:
  1778. ic_opmode = NL80211_IFTYPE_STATION;
  1779. break;
  1780. case NL80211_IFTYPE_ADHOC:
  1781. ic_opmode = NL80211_IFTYPE_ADHOC;
  1782. break;
  1783. case NL80211_IFTYPE_AP:
  1784. ic_opmode = NL80211_IFTYPE_AP;
  1785. break;
  1786. default:
  1787. DPRINTF(sc, ATH_DBG_FATAL,
  1788. "Interface type %d not yet supported\n", conf->type);
  1789. return -EOPNOTSUPP;
  1790. }
  1791. DPRINTF(sc, ATH_DBG_CONFIG, "Attach a VAP of type: %d\n", ic_opmode);
  1792. /* Set the VAP opmode */
  1793. avp->av_opmode = ic_opmode;
  1794. avp->av_bslot = -1;
  1795. if (ic_opmode == NL80211_IFTYPE_AP)
  1796. ath9k_hw_set_tsfadjust(sc->sc_ah, 1);
  1797. sc->sc_vaps[0] = conf->vif;
  1798. sc->sc_nvaps++;
  1799. /* Set the device opmode */
  1800. sc->sc_ah->ah_opmode = ic_opmode;
  1801. if (conf->type == NL80211_IFTYPE_AP) {
  1802. /* TODO: is this a suitable place to start ANI for AP mode? */
  1803. /* Start ANI */
  1804. mod_timer(&sc->sc_ani.timer,
  1805. jiffies + msecs_to_jiffies(ATH_ANI_POLLINTERVAL));
  1806. }
  1807. return 0;
  1808. }
  1809. static void ath9k_remove_interface(struct ieee80211_hw *hw,
  1810. struct ieee80211_if_init_conf *conf)
  1811. {
  1812. struct ath_softc *sc = hw->priv;
  1813. struct ath_vap *avp = (void *)conf->vif->drv_priv;
  1814. DPRINTF(sc, ATH_DBG_CONFIG, "Detach Interface\n");
  1815. /* Stop ANI */
  1816. del_timer_sync(&sc->sc_ani.timer);
  1817. /* Reclaim beacon resources */
  1818. if (sc->sc_ah->ah_opmode == NL80211_IFTYPE_AP ||
  1819. sc->sc_ah->ah_opmode == NL80211_IFTYPE_ADHOC) {
  1820. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1821. ath_beacon_return(sc, avp);
  1822. }
  1823. sc->sc_flags &= ~SC_OP_BEACONS;
  1824. sc->sc_vaps[0] = NULL;
  1825. sc->sc_nvaps--;
  1826. }
  1827. static int ath9k_config(struct ieee80211_hw *hw, u32 changed)
  1828. {
  1829. struct ath_softc *sc = hw->priv;
  1830. struct ieee80211_conf *conf = &hw->conf;
  1831. mutex_lock(&sc->mutex);
  1832. if (changed & IEEE80211_CONF_CHANGE_PS) {
  1833. if (conf->flags & IEEE80211_CONF_PS) {
  1834. if ((sc->sc_imask & ATH9K_INT_TIM_TIMER) == 0) {
  1835. sc->sc_imask |= ATH9K_INT_TIM_TIMER;
  1836. ath9k_hw_set_interrupts(sc->sc_ah,
  1837. sc->sc_imask);
  1838. }
  1839. ath9k_hw_setrxabort(sc->sc_ah, 1);
  1840. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_NETWORK_SLEEP);
  1841. } else {
  1842. ath9k_hw_setpower(sc->sc_ah, ATH9K_PM_AWAKE);
  1843. ath9k_hw_setrxabort(sc->sc_ah, 0);
  1844. sc->sc_flags &= ~SC_OP_WAIT_FOR_BEACON;
  1845. if (sc->sc_imask & ATH9K_INT_TIM_TIMER) {
  1846. sc->sc_imask &= ~ATH9K_INT_TIM_TIMER;
  1847. ath9k_hw_set_interrupts(sc->sc_ah,
  1848. sc->sc_imask);
  1849. }
  1850. }
  1851. }
  1852. if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
  1853. struct ieee80211_channel *curchan = hw->conf.channel;
  1854. int pos = curchan->hw_value;
  1855. DPRINTF(sc, ATH_DBG_CONFIG, "Set channel: %d MHz\n",
  1856. curchan->center_freq);
  1857. /* XXX: remove me eventualy */
  1858. ath9k_update_ichannel(sc, &sc->sc_ah->ah_channels[pos]);
  1859. ath_update_chainmask(sc, conf_is_ht(conf));
  1860. if (ath_set_channel(sc, &sc->sc_ah->ah_channels[pos]) < 0) {
  1861. DPRINTF(sc, ATH_DBG_FATAL, "Unable to set channel\n");
  1862. mutex_unlock(&sc->mutex);
  1863. return -EINVAL;
  1864. }
  1865. }
  1866. if (changed & IEEE80211_CONF_CHANGE_POWER)
  1867. sc->sc_config.txpowlimit = 2 * conf->power_level;
  1868. mutex_unlock(&sc->mutex);
  1869. return 0;
  1870. }
  1871. static int ath9k_config_interface(struct ieee80211_hw *hw,
  1872. struct ieee80211_vif *vif,
  1873. struct ieee80211_if_conf *conf)
  1874. {
  1875. struct ath_softc *sc = hw->priv;
  1876. struct ath_hal *ah = sc->sc_ah;
  1877. struct ath_vap *avp = (void *)vif->drv_priv;
  1878. u32 rfilt = 0;
  1879. int error, i;
  1880. /* TODO: Need to decide which hw opmode to use for multi-interface
  1881. * cases */
  1882. if (vif->type == NL80211_IFTYPE_AP &&
  1883. ah->ah_opmode != NL80211_IFTYPE_AP) {
  1884. ah->ah_opmode = NL80211_IFTYPE_STATION;
  1885. ath9k_hw_setopmode(ah);
  1886. ath9k_hw_write_associd(ah, sc->sc_myaddr, 0);
  1887. /* Request full reset to get hw opmode changed properly */
  1888. sc->sc_flags |= SC_OP_FULL_RESET;
  1889. }
  1890. if ((conf->changed & IEEE80211_IFCC_BSSID) &&
  1891. !is_zero_ether_addr(conf->bssid)) {
  1892. switch (vif->type) {
  1893. case NL80211_IFTYPE_STATION:
  1894. case NL80211_IFTYPE_ADHOC:
  1895. /* Set BSSID */
  1896. memcpy(sc->sc_curbssid, conf->bssid, ETH_ALEN);
  1897. sc->sc_curaid = 0;
  1898. ath9k_hw_write_associd(sc->sc_ah, sc->sc_curbssid,
  1899. sc->sc_curaid);
  1900. /* Set aggregation protection mode parameters */
  1901. sc->sc_config.ath_aggr_prot = 0;
  1902. DPRINTF(sc, ATH_DBG_CONFIG,
  1903. "RX filter 0x%x bssid %pM aid 0x%x\n",
  1904. rfilt, sc->sc_curbssid, sc->sc_curaid);
  1905. /* need to reconfigure the beacon */
  1906. sc->sc_flags &= ~SC_OP_BEACONS ;
  1907. break;
  1908. default:
  1909. break;
  1910. }
  1911. }
  1912. if ((conf->changed & IEEE80211_IFCC_BEACON) &&
  1913. ((vif->type == NL80211_IFTYPE_ADHOC) ||
  1914. (vif->type == NL80211_IFTYPE_AP))) {
  1915. /*
  1916. * Allocate and setup the beacon frame.
  1917. *
  1918. * Stop any previous beacon DMA. This may be
  1919. * necessary, for example, when an ibss merge
  1920. * causes reconfiguration; we may be called
  1921. * with beacon transmission active.
  1922. */
  1923. ath9k_hw_stoptxdma(sc->sc_ah, sc->beacon.beaconq);
  1924. error = ath_beacon_alloc(sc, 0);
  1925. if (error != 0)
  1926. return error;
  1927. ath_beacon_sync(sc, 0);
  1928. }
  1929. /* Check for WLAN_CAPABILITY_PRIVACY ? */
  1930. if ((avp->av_opmode != NL80211_IFTYPE_STATION)) {
  1931. for (i = 0; i < IEEE80211_WEP_NKID; i++)
  1932. if (ath9k_hw_keyisvalid(sc->sc_ah, (u16)i))
  1933. ath9k_hw_keysetmac(sc->sc_ah,
  1934. (u16)i,
  1935. sc->sc_curbssid);
  1936. }
  1937. /* Only legacy IBSS for now */
  1938. if (vif->type == NL80211_IFTYPE_ADHOC)
  1939. ath_update_chainmask(sc, 0);
  1940. return 0;
  1941. }
  1942. #define SUPPORTED_FILTERS \
  1943. (FIF_PROMISC_IN_BSS | \
  1944. FIF_ALLMULTI | \
  1945. FIF_CONTROL | \
  1946. FIF_OTHER_BSS | \
  1947. FIF_BCN_PRBRESP_PROMISC | \
  1948. FIF_FCSFAIL)
  1949. /* FIXME: sc->sc_full_reset ? */
  1950. static void ath9k_configure_filter(struct ieee80211_hw *hw,
  1951. unsigned int changed_flags,
  1952. unsigned int *total_flags,
  1953. int mc_count,
  1954. struct dev_mc_list *mclist)
  1955. {
  1956. struct ath_softc *sc = hw->priv;
  1957. u32 rfilt;
  1958. changed_flags &= SUPPORTED_FILTERS;
  1959. *total_flags &= SUPPORTED_FILTERS;
  1960. sc->rx.rxfilter = *total_flags;
  1961. rfilt = ath_calcrxfilter(sc);
  1962. ath9k_hw_setrxfilter(sc->sc_ah, rfilt);
  1963. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  1964. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  1965. ath9k_hw_write_associd(sc->sc_ah, ath_bcast_mac, 0);
  1966. }
  1967. DPRINTF(sc, ATH_DBG_CONFIG, "Set HW RX filter: 0x%x\n", sc->rx.rxfilter);
  1968. }
  1969. static void ath9k_sta_notify(struct ieee80211_hw *hw,
  1970. struct ieee80211_vif *vif,
  1971. enum sta_notify_cmd cmd,
  1972. struct ieee80211_sta *sta)
  1973. {
  1974. struct ath_softc *sc = hw->priv;
  1975. switch (cmd) {
  1976. case STA_NOTIFY_ADD:
  1977. ath_node_attach(sc, sta);
  1978. break;
  1979. case STA_NOTIFY_REMOVE:
  1980. ath_node_detach(sc, sta);
  1981. break;
  1982. default:
  1983. break;
  1984. }
  1985. }
  1986. static int ath9k_conf_tx(struct ieee80211_hw *hw,
  1987. u16 queue,
  1988. const struct ieee80211_tx_queue_params *params)
  1989. {
  1990. struct ath_softc *sc = hw->priv;
  1991. struct ath9k_tx_queue_info qi;
  1992. int ret = 0, qnum;
  1993. if (queue >= WME_NUM_AC)
  1994. return 0;
  1995. qi.tqi_aifs = params->aifs;
  1996. qi.tqi_cwmin = params->cw_min;
  1997. qi.tqi_cwmax = params->cw_max;
  1998. qi.tqi_burstTime = params->txop;
  1999. qnum = ath_get_hal_qnum(queue, sc);
  2000. DPRINTF(sc, ATH_DBG_CONFIG,
  2001. "Configure tx [queue/halq] [%d/%d], "
  2002. "aifs: %d, cw_min: %d, cw_max: %d, txop: %d\n",
  2003. queue, qnum, params->aifs, params->cw_min,
  2004. params->cw_max, params->txop);
  2005. ret = ath_txq_update(sc, qnum, &qi);
  2006. if (ret)
  2007. DPRINTF(sc, ATH_DBG_FATAL, "TXQ Update failed\n");
  2008. return ret;
  2009. }
  2010. static int ath9k_set_key(struct ieee80211_hw *hw,
  2011. enum set_key_cmd cmd,
  2012. struct ieee80211_vif *vif,
  2013. struct ieee80211_sta *sta,
  2014. struct ieee80211_key_conf *key)
  2015. {
  2016. struct ath_softc *sc = hw->priv;
  2017. int ret = 0;
  2018. ath9k_ps_wakeup(sc);
  2019. DPRINTF(sc, ATH_DBG_KEYCACHE, "Set HW Key\n");
  2020. switch (cmd) {
  2021. case SET_KEY:
  2022. ret = ath_key_config(sc, sta, key);
  2023. if (ret >= 0) {
  2024. key->hw_key_idx = ret;
  2025. /* push IV and Michael MIC generation to stack */
  2026. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2027. if (key->alg == ALG_TKIP)
  2028. key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2029. if (sc->sc_ah->sw_mgmt_crypto && key->alg == ALG_CCMP)
  2030. key->flags |= IEEE80211_KEY_FLAG_SW_MGMT;
  2031. ret = 0;
  2032. }
  2033. break;
  2034. case DISABLE_KEY:
  2035. ath_key_delete(sc, key);
  2036. break;
  2037. default:
  2038. ret = -EINVAL;
  2039. }
  2040. ath9k_ps_restore(sc);
  2041. return ret;
  2042. }
  2043. static void ath9k_bss_info_changed(struct ieee80211_hw *hw,
  2044. struct ieee80211_vif *vif,
  2045. struct ieee80211_bss_conf *bss_conf,
  2046. u32 changed)
  2047. {
  2048. struct ath_softc *sc = hw->priv;
  2049. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  2050. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed PREAMBLE %d\n",
  2051. bss_conf->use_short_preamble);
  2052. if (bss_conf->use_short_preamble)
  2053. sc->sc_flags |= SC_OP_PREAMBLE_SHORT;
  2054. else
  2055. sc->sc_flags &= ~SC_OP_PREAMBLE_SHORT;
  2056. }
  2057. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  2058. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed CTS PROT %d\n",
  2059. bss_conf->use_cts_prot);
  2060. if (bss_conf->use_cts_prot &&
  2061. hw->conf.channel->band != IEEE80211_BAND_5GHZ)
  2062. sc->sc_flags |= SC_OP_PROTECT_ENABLE;
  2063. else
  2064. sc->sc_flags &= ~SC_OP_PROTECT_ENABLE;
  2065. }
  2066. if (changed & BSS_CHANGED_ASSOC) {
  2067. DPRINTF(sc, ATH_DBG_CONFIG, "BSS Changed ASSOC %d\n",
  2068. bss_conf->assoc);
  2069. ath9k_bss_assoc_info(sc, vif, bss_conf);
  2070. }
  2071. }
  2072. static u64 ath9k_get_tsf(struct ieee80211_hw *hw)
  2073. {
  2074. u64 tsf;
  2075. struct ath_softc *sc = hw->priv;
  2076. struct ath_hal *ah = sc->sc_ah;
  2077. tsf = ath9k_hw_gettsf64(ah);
  2078. return tsf;
  2079. }
  2080. static void ath9k_set_tsf(struct ieee80211_hw *hw, u64 tsf)
  2081. {
  2082. struct ath_softc *sc = hw->priv;
  2083. struct ath_hal *ah = sc->sc_ah;
  2084. ath9k_hw_settsf64(ah, tsf);
  2085. }
  2086. static void ath9k_reset_tsf(struct ieee80211_hw *hw)
  2087. {
  2088. struct ath_softc *sc = hw->priv;
  2089. struct ath_hal *ah = sc->sc_ah;
  2090. ath9k_hw_reset_tsf(ah);
  2091. }
  2092. static int ath9k_ampdu_action(struct ieee80211_hw *hw,
  2093. enum ieee80211_ampdu_mlme_action action,
  2094. struct ieee80211_sta *sta,
  2095. u16 tid, u16 *ssn)
  2096. {
  2097. struct ath_softc *sc = hw->priv;
  2098. int ret = 0;
  2099. switch (action) {
  2100. case IEEE80211_AMPDU_RX_START:
  2101. if (!(sc->sc_flags & SC_OP_RXAGGR))
  2102. ret = -ENOTSUPP;
  2103. break;
  2104. case IEEE80211_AMPDU_RX_STOP:
  2105. break;
  2106. case IEEE80211_AMPDU_TX_START:
  2107. ret = ath_tx_aggr_start(sc, sta, tid, ssn);
  2108. if (ret < 0)
  2109. DPRINTF(sc, ATH_DBG_FATAL,
  2110. "Unable to start TX aggregation\n");
  2111. else
  2112. ieee80211_start_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2113. break;
  2114. case IEEE80211_AMPDU_TX_STOP:
  2115. ret = ath_tx_aggr_stop(sc, sta, tid);
  2116. if (ret < 0)
  2117. DPRINTF(sc, ATH_DBG_FATAL,
  2118. "Unable to stop TX aggregation\n");
  2119. ieee80211_stop_tx_ba_cb_irqsafe(hw, sta->addr, tid);
  2120. break;
  2121. case IEEE80211_AMPDU_TX_RESUME:
  2122. ath_tx_aggr_resume(sc, sta, tid);
  2123. break;
  2124. default:
  2125. DPRINTF(sc, ATH_DBG_FATAL, "Unknown AMPDU action\n");
  2126. }
  2127. return ret;
  2128. }
  2129. struct ieee80211_ops ath9k_ops = {
  2130. .tx = ath9k_tx,
  2131. .start = ath9k_start,
  2132. .stop = ath9k_stop,
  2133. .add_interface = ath9k_add_interface,
  2134. .remove_interface = ath9k_remove_interface,
  2135. .config = ath9k_config,
  2136. .config_interface = ath9k_config_interface,
  2137. .configure_filter = ath9k_configure_filter,
  2138. .sta_notify = ath9k_sta_notify,
  2139. .conf_tx = ath9k_conf_tx,
  2140. .bss_info_changed = ath9k_bss_info_changed,
  2141. .set_key = ath9k_set_key,
  2142. .get_tsf = ath9k_get_tsf,
  2143. .set_tsf = ath9k_set_tsf,
  2144. .reset_tsf = ath9k_reset_tsf,
  2145. .ampdu_action = ath9k_ampdu_action,
  2146. };
  2147. static struct {
  2148. u32 version;
  2149. const char * name;
  2150. } ath_mac_bb_names[] = {
  2151. { AR_SREV_VERSION_5416_PCI, "5416" },
  2152. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2153. { AR_SREV_VERSION_9100, "9100" },
  2154. { AR_SREV_VERSION_9160, "9160" },
  2155. { AR_SREV_VERSION_9280, "9280" },
  2156. { AR_SREV_VERSION_9285, "9285" }
  2157. };
  2158. static struct {
  2159. u16 version;
  2160. const char * name;
  2161. } ath_rf_names[] = {
  2162. { 0, "5133" },
  2163. { AR_RAD5133_SREV_MAJOR, "5133" },
  2164. { AR_RAD5122_SREV_MAJOR, "5122" },
  2165. { AR_RAD2133_SREV_MAJOR, "2133" },
  2166. { AR_RAD2122_SREV_MAJOR, "2122" }
  2167. };
  2168. /*
  2169. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2170. */
  2171. const char *
  2172. ath_mac_bb_name(u32 mac_bb_version)
  2173. {
  2174. int i;
  2175. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2176. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2177. return ath_mac_bb_names[i].name;
  2178. }
  2179. }
  2180. return "????";
  2181. }
  2182. /*
  2183. * Return the RF name. "????" is returned if the RF is unknown.
  2184. */
  2185. const char *
  2186. ath_rf_name(u16 rf_version)
  2187. {
  2188. int i;
  2189. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2190. if (ath_rf_names[i].version == rf_version) {
  2191. return ath_rf_names[i].name;
  2192. }
  2193. }
  2194. return "????";
  2195. }
  2196. static int __init ath9k_init(void)
  2197. {
  2198. int error;
  2199. /* Register rate control algorithm */
  2200. error = ath_rate_control_register();
  2201. if (error != 0) {
  2202. printk(KERN_ERR
  2203. "ath9k: Unable to register rate control "
  2204. "algorithm: %d\n",
  2205. error);
  2206. goto err_out;
  2207. }
  2208. error = ath_pci_init();
  2209. if (error < 0) {
  2210. printk(KERN_ERR
  2211. "ath9k: No PCI devices found, driver not installed.\n");
  2212. error = -ENODEV;
  2213. goto err_rate_unregister;
  2214. }
  2215. error = ath_ahb_init();
  2216. if (error < 0) {
  2217. error = -ENODEV;
  2218. goto err_pci_exit;
  2219. }
  2220. return 0;
  2221. err_pci_exit:
  2222. ath_pci_exit();
  2223. err_rate_unregister:
  2224. ath_rate_control_unregister();
  2225. err_out:
  2226. return error;
  2227. }
  2228. module_init(ath9k_init);
  2229. static void __exit ath9k_exit(void)
  2230. {
  2231. ath_ahb_exit();
  2232. ath_pci_exit();
  2233. ath_rate_control_unregister();
  2234. printk(KERN_INFO "%s: Driver unloaded\n", dev_info);
  2235. }
  2236. module_exit(ath9k_exit);