main.c 69 KB

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