main.c 67 KB

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