main.c 70 KB

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