core.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  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 "core.h"
  17. #include "regd.h"
  18. static u32 ath_chainmask_sel_up_rssi_thres =
  19. ATH_CHAINMASK_SEL_UP_RSSI_THRES;
  20. static u32 ath_chainmask_sel_down_rssi_thres =
  21. ATH_CHAINMASK_SEL_DOWN_RSSI_THRES;
  22. static u32 ath_chainmask_sel_period =
  23. ATH_CHAINMASK_SEL_TIMEOUT;
  24. /* return bus cachesize in 4B word units */
  25. static void bus_read_cachesize(struct ath_softc *sc, int *csz)
  26. {
  27. u8 u8tmp;
  28. pci_read_config_byte(sc->pdev, PCI_CACHE_LINE_SIZE, (u8 *)&u8tmp);
  29. *csz = (int)u8tmp;
  30. /*
  31. * This check was put in to avoid "unplesant" consequences if
  32. * the bootrom has not fully initialized all PCI devices.
  33. * Sometimes the cache line size register is not set
  34. */
  35. if (*csz == 0)
  36. *csz = DEFAULT_CACHELINE >> 2; /* Use the default size */
  37. }
  38. static u8 parse_mpdudensity(u8 mpdudensity)
  39. {
  40. /*
  41. * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
  42. * 0 for no restriction
  43. * 1 for 1/4 us
  44. * 2 for 1/2 us
  45. * 3 for 1 us
  46. * 4 for 2 us
  47. * 5 for 4 us
  48. * 6 for 8 us
  49. * 7 for 16 us
  50. */
  51. switch (mpdudensity) {
  52. case 0:
  53. return 0;
  54. case 1:
  55. case 2:
  56. case 3:
  57. /* Our lower layer calculations limit our precision to
  58. 1 microsecond */
  59. return 1;
  60. case 4:
  61. return 2;
  62. case 5:
  63. return 4;
  64. case 6:
  65. return 8;
  66. case 7:
  67. return 16;
  68. default:
  69. return 0;
  70. }
  71. }
  72. /*
  73. * Set current operating mode
  74. *
  75. * This function initializes and fills the rate table in the ATH object based
  76. * on the operating mode.
  77. */
  78. static void ath_setcurmode(struct ath_softc *sc, enum wireless_mode mode)
  79. {
  80. const struct ath9k_rate_table *rt;
  81. int i;
  82. memset(sc->sc_rixmap, 0xff, sizeof(sc->sc_rixmap));
  83. rt = ath9k_hw_getratetable(sc->sc_ah, mode);
  84. BUG_ON(!rt);
  85. for (i = 0; i < rt->rateCount; i++)
  86. sc->sc_rixmap[rt->info[i].rateCode] = (u8) i;
  87. memset(sc->sc_hwmap, 0, sizeof(sc->sc_hwmap));
  88. for (i = 0; i < 256; i++) {
  89. u8 ix = rt->rateCodeToIndex[i];
  90. if (ix == 0xff)
  91. continue;
  92. sc->sc_hwmap[i].ieeerate =
  93. rt->info[ix].dot11Rate & IEEE80211_RATE_VAL;
  94. sc->sc_hwmap[i].rateKbps = rt->info[ix].rateKbps;
  95. if (rt->info[ix].shortPreamble ||
  96. rt->info[ix].phy == PHY_OFDM) {
  97. /* XXX: Handle this */
  98. }
  99. /* NB: this uses the last entry if the rate isn't found */
  100. /* XXX beware of overlow */
  101. }
  102. sc->sc_currates = rt;
  103. sc->sc_curmode = mode;
  104. /*
  105. * All protection frames are transmited at 2Mb/s for
  106. * 11g, otherwise at 1Mb/s.
  107. * XXX select protection rate index from rate table.
  108. */
  109. sc->sc_protrix = (mode == ATH9K_MODE_11G ? 1 : 0);
  110. }
  111. /*
  112. * Set up rate table (legacy rates)
  113. */
  114. static void ath_setup_rates(struct ath_softc *sc, enum ieee80211_band band)
  115. {
  116. struct ath_hal *ah = sc->sc_ah;
  117. const struct ath9k_rate_table *rt = NULL;
  118. struct ieee80211_supported_band *sband;
  119. struct ieee80211_rate *rate;
  120. int i, maxrates;
  121. switch (band) {
  122. case IEEE80211_BAND_2GHZ:
  123. rt = ath9k_hw_getratetable(ah, ATH9K_MODE_11G);
  124. break;
  125. case IEEE80211_BAND_5GHZ:
  126. rt = ath9k_hw_getratetable(ah, ATH9K_MODE_11A);
  127. break;
  128. default:
  129. break;
  130. }
  131. if (rt == NULL)
  132. return;
  133. sband = &sc->sbands[band];
  134. rate = sc->rates[band];
  135. if (rt->rateCount > ATH_RATE_MAX)
  136. maxrates = ATH_RATE_MAX;
  137. else
  138. maxrates = rt->rateCount;
  139. for (i = 0; i < maxrates; i++) {
  140. rate[i].bitrate = rt->info[i].rateKbps / 100;
  141. rate[i].hw_value = rt->info[i].rateCode;
  142. sband->n_bitrates++;
  143. DPRINTF(sc, ATH_DBG_CONFIG,
  144. "%s: Rate: %2dMbps, ratecode: %2d\n",
  145. __func__,
  146. rate[i].bitrate / 10,
  147. rate[i].hw_value);
  148. }
  149. }
  150. /*
  151. * Set up channel list
  152. */
  153. static int ath_setup_channels(struct ath_softc *sc)
  154. {
  155. struct ath_hal *ah = sc->sc_ah;
  156. int nchan, i, a = 0, b = 0;
  157. u8 regclassids[ATH_REGCLASSIDS_MAX];
  158. u32 nregclass = 0;
  159. struct ieee80211_supported_band *band_2ghz;
  160. struct ieee80211_supported_band *band_5ghz;
  161. struct ieee80211_channel *chan_2ghz;
  162. struct ieee80211_channel *chan_5ghz;
  163. struct ath9k_channel *c;
  164. /* Fill in ah->ah_channels */
  165. if (!ath9k_regd_init_channels(ah, ATH_CHAN_MAX, (u32 *)&nchan,
  166. regclassids, ATH_REGCLASSIDS_MAX,
  167. &nregclass, CTRY_DEFAULT, false, 1)) {
  168. u32 rd = ah->ah_currentRD;
  169. DPRINTF(sc, ATH_DBG_FATAL,
  170. "%s: unable to collect channel list; "
  171. "regdomain likely %u country code %u\n",
  172. __func__, rd, CTRY_DEFAULT);
  173. return -EINVAL;
  174. }
  175. band_2ghz = &sc->sbands[IEEE80211_BAND_2GHZ];
  176. band_5ghz = &sc->sbands[IEEE80211_BAND_5GHZ];
  177. chan_2ghz = sc->channels[IEEE80211_BAND_2GHZ];
  178. chan_5ghz = sc->channels[IEEE80211_BAND_5GHZ];
  179. for (i = 0; i < nchan; i++) {
  180. c = &ah->ah_channels[i];
  181. if (IS_CHAN_2GHZ(c)) {
  182. chan_2ghz[a].band = IEEE80211_BAND_2GHZ;
  183. chan_2ghz[a].center_freq = c->channel;
  184. chan_2ghz[a].max_power = c->maxTxPower;
  185. if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
  186. chan_2ghz[a].flags |= IEEE80211_CHAN_NO_IBSS;
  187. if (c->channelFlags & CHANNEL_PASSIVE)
  188. chan_2ghz[a].flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  189. band_2ghz->n_channels = ++a;
  190. DPRINTF(sc, ATH_DBG_CONFIG,
  191. "%s: 2MHz channel: %d, "
  192. "channelFlags: 0x%x\n",
  193. __func__, c->channel, c->channelFlags);
  194. } else if (IS_CHAN_5GHZ(c)) {
  195. chan_5ghz[b].band = IEEE80211_BAND_5GHZ;
  196. chan_5ghz[b].center_freq = c->channel;
  197. chan_5ghz[b].max_power = c->maxTxPower;
  198. if (c->privFlags & CHANNEL_DISALLOW_ADHOC)
  199. chan_5ghz[b].flags |= IEEE80211_CHAN_NO_IBSS;
  200. if (c->channelFlags & CHANNEL_PASSIVE)
  201. chan_5ghz[b].flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  202. band_5ghz->n_channels = ++b;
  203. DPRINTF(sc, ATH_DBG_CONFIG,
  204. "%s: 5MHz channel: %d, "
  205. "channelFlags: 0x%x\n",
  206. __func__, c->channel, c->channelFlags);
  207. }
  208. }
  209. return 0;
  210. }
  211. /*
  212. * Determine mode from channel flags
  213. *
  214. * This routine will provide the enumerated WIRELESSS_MODE value based
  215. * on the settings of the channel flags. If no valid set of flags
  216. * exist, the lowest mode (11b) is selected.
  217. */
  218. static enum wireless_mode ath_chan2mode(struct ath9k_channel *chan)
  219. {
  220. if (chan->chanmode == CHANNEL_A)
  221. return ATH9K_MODE_11A;
  222. else if (chan->chanmode == CHANNEL_G)
  223. return ATH9K_MODE_11G;
  224. else if (chan->chanmode == CHANNEL_B)
  225. return ATH9K_MODE_11B;
  226. else if (chan->chanmode == CHANNEL_A_HT20)
  227. return ATH9K_MODE_11NA_HT20;
  228. else if (chan->chanmode == CHANNEL_G_HT20)
  229. return ATH9K_MODE_11NG_HT20;
  230. else if (chan->chanmode == CHANNEL_A_HT40PLUS)
  231. return ATH9K_MODE_11NA_HT40PLUS;
  232. else if (chan->chanmode == CHANNEL_A_HT40MINUS)
  233. return ATH9K_MODE_11NA_HT40MINUS;
  234. else if (chan->chanmode == CHANNEL_G_HT40PLUS)
  235. return ATH9K_MODE_11NG_HT40PLUS;
  236. else if (chan->chanmode == CHANNEL_G_HT40MINUS)
  237. return ATH9K_MODE_11NG_HT40MINUS;
  238. WARN_ON(1); /* should not get here */
  239. return ATH9K_MODE_11B;
  240. }
  241. /*
  242. * Set the current channel
  243. *
  244. * Set/change channels. If the channel is really being changed, it's done
  245. * by reseting the chip. To accomplish this we must first cleanup any pending
  246. * DMA, then restart stuff after a la ath_init.
  247. */
  248. int ath_set_channel(struct ath_softc *sc, struct ath9k_channel *hchan)
  249. {
  250. struct ath_hal *ah = sc->sc_ah;
  251. bool fastcc = true, stopped;
  252. if (sc->sc_flags & SC_OP_INVALID) /* the device is invalid or removed */
  253. return -EIO;
  254. DPRINTF(sc, ATH_DBG_CONFIG,
  255. "%s: %u (%u MHz) -> %u (%u MHz), cflags:%x\n",
  256. __func__,
  257. ath9k_hw_mhz2ieee(ah, sc->sc_ah->ah_curchan->channel,
  258. sc->sc_ah->ah_curchan->channelFlags),
  259. sc->sc_ah->ah_curchan->channel,
  260. ath9k_hw_mhz2ieee(ah, hchan->channel, hchan->channelFlags),
  261. hchan->channel, hchan->channelFlags);
  262. if (hchan->channel != sc->sc_ah->ah_curchan->channel ||
  263. hchan->channelFlags != sc->sc_ah->ah_curchan->channelFlags ||
  264. (sc->sc_flags & SC_OP_CHAINMASK_UPDATE) ||
  265. (sc->sc_flags & SC_OP_FULL_RESET)) {
  266. int status;
  267. /*
  268. * This is only performed if the channel settings have
  269. * actually changed.
  270. *
  271. * To switch channels clear any pending DMA operations;
  272. * wait long enough for the RX fifo to drain, reset the
  273. * hardware at the new frequency, and then re-enable
  274. * the relevant bits of the h/w.
  275. */
  276. ath9k_hw_set_interrupts(ah, 0); /* disable interrupts */
  277. ath_draintxq(sc, false); /* clear pending tx frames */
  278. stopped = ath_stoprecv(sc); /* turn off frame recv */
  279. /* XXX: do not flush receive queue here. We don't want
  280. * to flush data frames already in queue because of
  281. * changing channel. */
  282. if (!stopped || (sc->sc_flags & SC_OP_FULL_RESET))
  283. fastcc = false;
  284. spin_lock_bh(&sc->sc_resetlock);
  285. if (!ath9k_hw_reset(ah, hchan,
  286. sc->sc_ht_info.tx_chan_width,
  287. sc->sc_tx_chainmask,
  288. sc->sc_rx_chainmask,
  289. sc->sc_ht_extprotspacing,
  290. fastcc, &status)) {
  291. DPRINTF(sc, ATH_DBG_FATAL,
  292. "%s: unable to reset channel %u (%uMhz) "
  293. "flags 0x%x hal status %u\n", __func__,
  294. ath9k_hw_mhz2ieee(ah, hchan->channel,
  295. hchan->channelFlags),
  296. hchan->channel, hchan->channelFlags, status);
  297. spin_unlock_bh(&sc->sc_resetlock);
  298. return -EIO;
  299. }
  300. spin_unlock_bh(&sc->sc_resetlock);
  301. sc->sc_flags &= ~SC_OP_CHAINMASK_UPDATE;
  302. sc->sc_flags &= ~SC_OP_FULL_RESET;
  303. /* Re-enable rx framework */
  304. if (ath_startrecv(sc) != 0) {
  305. DPRINTF(sc, ATH_DBG_FATAL,
  306. "%s: unable to restart recv logic\n", __func__);
  307. return -EIO;
  308. }
  309. /*
  310. * Change channels and update the h/w rate map
  311. * if we're switching; e.g. 11a to 11b/g.
  312. */
  313. ath_setcurmode(sc, ath_chan2mode(hchan));
  314. ath_update_txpow(sc); /* update tx power state */
  315. /*
  316. * Re-enable interrupts.
  317. */
  318. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  319. }
  320. return 0;
  321. }
  322. /**********************/
  323. /* Chainmask Handling */
  324. /**********************/
  325. static void ath_chainmask_sel_timertimeout(unsigned long data)
  326. {
  327. struct ath_chainmask_sel *cm = (struct ath_chainmask_sel *)data;
  328. cm->switch_allowed = 1;
  329. }
  330. /* Start chainmask select timer */
  331. static void ath_chainmask_sel_timerstart(struct ath_chainmask_sel *cm)
  332. {
  333. cm->switch_allowed = 0;
  334. mod_timer(&cm->timer, ath_chainmask_sel_period);
  335. }
  336. /* Stop chainmask select timer */
  337. static void ath_chainmask_sel_timerstop(struct ath_chainmask_sel *cm)
  338. {
  339. cm->switch_allowed = 0;
  340. del_timer_sync(&cm->timer);
  341. }
  342. static void ath_chainmask_sel_init(struct ath_softc *sc, struct ath_node *an)
  343. {
  344. struct ath_chainmask_sel *cm = &an->an_chainmask_sel;
  345. memset(cm, 0, sizeof(struct ath_chainmask_sel));
  346. cm->cur_tx_mask = sc->sc_tx_chainmask;
  347. cm->cur_rx_mask = sc->sc_rx_chainmask;
  348. cm->tx_avgrssi = ATH_RSSI_DUMMY_MARKER;
  349. setup_timer(&cm->timer,
  350. ath_chainmask_sel_timertimeout, (unsigned long) cm);
  351. }
  352. int ath_chainmask_sel_logic(struct ath_softc *sc, struct ath_node *an)
  353. {
  354. struct ath_chainmask_sel *cm = &an->an_chainmask_sel;
  355. /*
  356. * Disable auto-swtiching in one of the following if conditions.
  357. * sc_chainmask_auto_sel is used for internal global auto-switching
  358. * enabled/disabled setting
  359. */
  360. if (sc->sc_ah->ah_caps.tx_chainmask != ATH_CHAINMASK_SEL_3X3) {
  361. cm->cur_tx_mask = sc->sc_tx_chainmask;
  362. return cm->cur_tx_mask;
  363. }
  364. if (cm->tx_avgrssi == ATH_RSSI_DUMMY_MARKER)
  365. return cm->cur_tx_mask;
  366. if (cm->switch_allowed) {
  367. /* Switch down from tx 3 to tx 2. */
  368. if (cm->cur_tx_mask == ATH_CHAINMASK_SEL_3X3 &&
  369. ATH_RSSI_OUT(cm->tx_avgrssi) >=
  370. ath_chainmask_sel_down_rssi_thres) {
  371. cm->cur_tx_mask = sc->sc_tx_chainmask;
  372. /* Don't let another switch happen until
  373. * this timer expires */
  374. ath_chainmask_sel_timerstart(cm);
  375. }
  376. /* Switch up from tx 2 to 3. */
  377. else if (cm->cur_tx_mask == sc->sc_tx_chainmask &&
  378. ATH_RSSI_OUT(cm->tx_avgrssi) <=
  379. ath_chainmask_sel_up_rssi_thres) {
  380. cm->cur_tx_mask = ATH_CHAINMASK_SEL_3X3;
  381. /* Don't let another switch happen
  382. * until this timer expires */
  383. ath_chainmask_sel_timerstart(cm);
  384. }
  385. }
  386. return cm->cur_tx_mask;
  387. }
  388. /*
  389. * Update tx/rx chainmask. For legacy association,
  390. * hard code chainmask to 1x1, for 11n association, use
  391. * the chainmask configuration.
  392. */
  393. void ath_update_chainmask(struct ath_softc *sc, int is_ht)
  394. {
  395. sc->sc_flags |= SC_OP_CHAINMASK_UPDATE;
  396. if (is_ht) {
  397. sc->sc_tx_chainmask = sc->sc_ah->ah_caps.tx_chainmask;
  398. sc->sc_rx_chainmask = sc->sc_ah->ah_caps.rx_chainmask;
  399. } else {
  400. sc->sc_tx_chainmask = 1;
  401. sc->sc_rx_chainmask = 1;
  402. }
  403. DPRINTF(sc, ATH_DBG_CONFIG, "%s: tx chmask: %d, rx chmask: %d\n",
  404. __func__, sc->sc_tx_chainmask, sc->sc_rx_chainmask);
  405. }
  406. /*******/
  407. /* ANI */
  408. /*******/
  409. /*
  410. * This routine performs the periodic noise floor calibration function
  411. * that is used to adjust and optimize the chip performance. This
  412. * takes environmental changes (location, temperature) into account.
  413. * When the task is complete, it reschedules itself depending on the
  414. * appropriate interval that was calculated.
  415. */
  416. static void ath_ani_calibrate(unsigned long data)
  417. {
  418. struct ath_softc *sc;
  419. struct ath_hal *ah;
  420. bool longcal = false;
  421. bool shortcal = false;
  422. bool aniflag = false;
  423. unsigned int timestamp = jiffies_to_msecs(jiffies);
  424. u32 cal_interval;
  425. sc = (struct ath_softc *)data;
  426. ah = sc->sc_ah;
  427. /*
  428. * don't calibrate when we're scanning.
  429. * we are most likely not on our home channel.
  430. */
  431. if (sc->rx_filter & FIF_BCN_PRBRESP_PROMISC)
  432. return;
  433. /* Long calibration runs independently of short calibration. */
  434. if ((timestamp - sc->sc_ani.sc_longcal_timer) >= ATH_LONG_CALINTERVAL) {
  435. longcal = true;
  436. DPRINTF(sc, ATH_DBG_ANI, "%s: longcal @%lu\n",
  437. __func__, jiffies);
  438. sc->sc_ani.sc_longcal_timer = timestamp;
  439. }
  440. /* Short calibration applies only while sc_caldone is false */
  441. if (!sc->sc_ani.sc_caldone) {
  442. if ((timestamp - sc->sc_ani.sc_shortcal_timer) >=
  443. ATH_SHORT_CALINTERVAL) {
  444. shortcal = true;
  445. DPRINTF(sc, ATH_DBG_ANI, "%s: shortcal @%lu\n",
  446. __func__, jiffies);
  447. sc->sc_ani.sc_shortcal_timer = timestamp;
  448. sc->sc_ani.sc_resetcal_timer = timestamp;
  449. }
  450. } else {
  451. if ((timestamp - sc->sc_ani.sc_resetcal_timer) >=
  452. ATH_RESTART_CALINTERVAL) {
  453. ath9k_hw_reset_calvalid(ah, ah->ah_curchan,
  454. &sc->sc_ani.sc_caldone);
  455. if (sc->sc_ani.sc_caldone)
  456. sc->sc_ani.sc_resetcal_timer = timestamp;
  457. }
  458. }
  459. /* Verify whether we must check ANI */
  460. if ((timestamp - sc->sc_ani.sc_checkani_timer) >=
  461. ATH_ANI_POLLINTERVAL) {
  462. aniflag = true;
  463. sc->sc_ani.sc_checkani_timer = timestamp;
  464. }
  465. /* Skip all processing if there's nothing to do. */
  466. if (longcal || shortcal || aniflag) {
  467. /* Call ANI routine if necessary */
  468. if (aniflag)
  469. ath9k_hw_ani_monitor(ah, &sc->sc_halstats,
  470. ah->ah_curchan);
  471. /* Perform calibration if necessary */
  472. if (longcal || shortcal) {
  473. bool iscaldone = false;
  474. if (ath9k_hw_calibrate(ah, ah->ah_curchan,
  475. sc->sc_rx_chainmask, longcal,
  476. &iscaldone)) {
  477. if (longcal)
  478. sc->sc_ani.sc_noise_floor =
  479. ath9k_hw_getchan_noise(ah,
  480. ah->ah_curchan);
  481. DPRINTF(sc, ATH_DBG_ANI,
  482. "%s: calibrate chan %u/%x nf: %d\n",
  483. __func__,
  484. ah->ah_curchan->channel,
  485. ah->ah_curchan->channelFlags,
  486. sc->sc_ani.sc_noise_floor);
  487. } else {
  488. DPRINTF(sc, ATH_DBG_ANY,
  489. "%s: calibrate chan %u/%x failed\n",
  490. __func__,
  491. ah->ah_curchan->channel,
  492. ah->ah_curchan->channelFlags);
  493. }
  494. sc->sc_ani.sc_caldone = iscaldone;
  495. }
  496. }
  497. /*
  498. * Set timer interval based on previous results.
  499. * The interval must be the shortest necessary to satisfy ANI,
  500. * short calibration and long calibration.
  501. */
  502. cal_interval = ATH_ANI_POLLINTERVAL;
  503. if (!sc->sc_ani.sc_caldone)
  504. cal_interval = min(cal_interval, (u32)ATH_SHORT_CALINTERVAL);
  505. mod_timer(&sc->sc_ani.timer, jiffies + msecs_to_jiffies(cal_interval));
  506. }
  507. /********/
  508. /* Core */
  509. /********/
  510. int ath_open(struct ath_softc *sc, struct ath9k_channel *initial_chan)
  511. {
  512. struct ath_hal *ah = sc->sc_ah;
  513. int status;
  514. int error = 0;
  515. DPRINTF(sc, ATH_DBG_CONFIG, "%s: mode %d\n",
  516. __func__, sc->sc_ah->ah_opmode);
  517. /* Reset SERDES registers */
  518. ath9k_hw_configpcipowersave(ah, 0);
  519. /*
  520. * The basic interface to setting the hardware in a good
  521. * state is ``reset''. On return the hardware is known to
  522. * be powered up and with interrupts disabled. This must
  523. * be followed by initialization of the appropriate bits
  524. * and then setup of the interrupt mask.
  525. */
  526. spin_lock_bh(&sc->sc_resetlock);
  527. if (!ath9k_hw_reset(ah, initial_chan,
  528. sc->sc_ht_info.tx_chan_width,
  529. sc->sc_tx_chainmask, sc->sc_rx_chainmask,
  530. sc->sc_ht_extprotspacing, false, &status)) {
  531. DPRINTF(sc, ATH_DBG_FATAL,
  532. "%s: unable to reset hardware; hal status %u "
  533. "(freq %u flags 0x%x)\n", __func__, status,
  534. initial_chan->channel, initial_chan->channelFlags);
  535. error = -EIO;
  536. spin_unlock_bh(&sc->sc_resetlock);
  537. goto done;
  538. }
  539. spin_unlock_bh(&sc->sc_resetlock);
  540. /*
  541. * This is needed only to setup initial state
  542. * but it's best done after a reset.
  543. */
  544. ath_update_txpow(sc);
  545. /*
  546. * Setup the hardware after reset:
  547. * The receive engine is set going.
  548. * Frame transmit is handled entirely
  549. * in the frame output path; there's nothing to do
  550. * here except setup the interrupt mask.
  551. */
  552. if (ath_startrecv(sc) != 0) {
  553. DPRINTF(sc, ATH_DBG_FATAL,
  554. "%s: unable to start recv logic\n", __func__);
  555. error = -EIO;
  556. goto done;
  557. }
  558. /* Setup our intr mask. */
  559. sc->sc_imask = ATH9K_INT_RX | ATH9K_INT_TX
  560. | ATH9K_INT_RXEOL | ATH9K_INT_RXORN
  561. | ATH9K_INT_FATAL | ATH9K_INT_GLOBAL;
  562. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_GTT)
  563. sc->sc_imask |= ATH9K_INT_GTT;
  564. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT)
  565. sc->sc_imask |= ATH9K_INT_CST;
  566. /*
  567. * Enable MIB interrupts when there are hardware phy counters.
  568. * Note we only do this (at the moment) for station mode.
  569. */
  570. if (ath9k_hw_phycounters(ah) &&
  571. ((sc->sc_ah->ah_opmode == ATH9K_M_STA) ||
  572. (sc->sc_ah->ah_opmode == ATH9K_M_IBSS)))
  573. sc->sc_imask |= ATH9K_INT_MIB;
  574. /*
  575. * Some hardware processes the TIM IE and fires an
  576. * interrupt when the TIM bit is set. For hardware
  577. * that does, if not overridden by configuration,
  578. * enable the TIM interrupt when operating as station.
  579. */
  580. if ((ah->ah_caps.hw_caps & ATH9K_HW_CAP_ENHANCEDPM) &&
  581. (sc->sc_ah->ah_opmode == ATH9K_M_STA) &&
  582. !sc->sc_config.swBeaconProcess)
  583. sc->sc_imask |= ATH9K_INT_TIM;
  584. ath_setcurmode(sc, ath_chan2mode(initial_chan));
  585. sc->sc_flags &= ~SC_OP_INVALID;
  586. /* Disable BMISS interrupt when we're not associated */
  587. sc->sc_imask &= ~(ATH9K_INT_SWBA | ATH9K_INT_BMISS);
  588. ath9k_hw_set_interrupts(sc->sc_ah,sc->sc_imask);
  589. ieee80211_wake_queues(sc->hw);
  590. done:
  591. return error;
  592. }
  593. void ath_stop(struct ath_softc *sc)
  594. {
  595. struct ath_hal *ah = sc->sc_ah;
  596. DPRINTF(sc, ATH_DBG_CONFIG, "%s: Cleaning up\n", __func__);
  597. ieee80211_stop_queues(sc->hw);
  598. /* make sure h/w will not generate any interrupt
  599. * before setting the invalid flag. */
  600. ath9k_hw_set_interrupts(ah, 0);
  601. if (!(sc->sc_flags & SC_OP_INVALID)) {
  602. ath_draintxq(sc, false);
  603. ath_stoprecv(sc);
  604. ath9k_hw_phy_disable(ah);
  605. } else
  606. sc->sc_rxlink = NULL;
  607. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  608. if (sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  609. cancel_delayed_work_sync(&sc->rf_kill.rfkill_poll);
  610. #endif
  611. /* disable HAL and put h/w to sleep */
  612. ath9k_hw_disable(sc->sc_ah);
  613. ath9k_hw_configpcipowersave(sc->sc_ah, 1);
  614. sc->sc_flags |= SC_OP_INVALID;
  615. }
  616. int ath_reset(struct ath_softc *sc, bool retry_tx)
  617. {
  618. struct ath_hal *ah = sc->sc_ah;
  619. int status;
  620. int error = 0;
  621. ath9k_hw_set_interrupts(ah, 0);
  622. ath_draintxq(sc, retry_tx);
  623. ath_stoprecv(sc);
  624. ath_flushrecv(sc);
  625. /* Reset chip */
  626. spin_lock_bh(&sc->sc_resetlock);
  627. if (!ath9k_hw_reset(ah, sc->sc_ah->ah_curchan,
  628. sc->sc_ht_info.tx_chan_width,
  629. sc->sc_tx_chainmask, sc->sc_rx_chainmask,
  630. sc->sc_ht_extprotspacing, false, &status)) {
  631. DPRINTF(sc, ATH_DBG_FATAL,
  632. "%s: unable to reset hardware; hal status %u\n",
  633. __func__, status);
  634. error = -EIO;
  635. }
  636. spin_unlock_bh(&sc->sc_resetlock);
  637. if (ath_startrecv(sc) != 0)
  638. DPRINTF(sc, ATH_DBG_FATAL,
  639. "%s: unable to start recv logic\n", __func__);
  640. /*
  641. * We may be doing a reset in response to a request
  642. * that changes the channel so update any state that
  643. * might change as a result.
  644. */
  645. ath_setcurmode(sc, ath_chan2mode(sc->sc_ah->ah_curchan));
  646. ath_update_txpow(sc);
  647. if (sc->sc_flags & SC_OP_BEACONS)
  648. ath_beacon_config(sc, ATH_IF_ID_ANY); /* restart beacons */
  649. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  650. /* Restart the txq */
  651. if (retry_tx) {
  652. int i;
  653. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++) {
  654. if (ATH_TXQ_SETUP(sc, i)) {
  655. spin_lock_bh(&sc->sc_txq[i].axq_lock);
  656. ath_txq_schedule(sc, &sc->sc_txq[i]);
  657. spin_unlock_bh(&sc->sc_txq[i].axq_lock);
  658. }
  659. }
  660. }
  661. return error;
  662. }
  663. /* Interrupt handler. Most of the actual processing is deferred.
  664. * It's the caller's responsibility to ensure the chip is awake. */
  665. irqreturn_t ath_isr(int irq, void *dev)
  666. {
  667. struct ath_softc *sc = dev;
  668. struct ath_hal *ah = sc->sc_ah;
  669. enum ath9k_int status;
  670. bool sched = false;
  671. do {
  672. if (sc->sc_flags & SC_OP_INVALID) {
  673. /*
  674. * The hardware is not ready/present, don't
  675. * touch anything. Note this can happen early
  676. * on if the IRQ is shared.
  677. */
  678. return IRQ_NONE;
  679. }
  680. if (!ath9k_hw_intrpend(ah)) { /* shared irq, not for us */
  681. return IRQ_NONE;
  682. }
  683. /*
  684. * Figure out the reason(s) for the interrupt. Note
  685. * that the hal returns a pseudo-ISR that may include
  686. * bits we haven't explicitly enabled so we mask the
  687. * value to insure we only process bits we requested.
  688. */
  689. ath9k_hw_getisr(ah, &status); /* NB: clears ISR too */
  690. status &= sc->sc_imask; /* discard unasked-for bits */
  691. /*
  692. * If there are no status bits set, then this interrupt was not
  693. * for me (should have been caught above).
  694. */
  695. if (!status)
  696. return IRQ_NONE;
  697. sc->sc_intrstatus = status;
  698. if (status & ATH9K_INT_FATAL) {
  699. /* need a chip reset */
  700. sched = true;
  701. } else if (status & ATH9K_INT_RXORN) {
  702. /* need a chip reset */
  703. sched = true;
  704. } else {
  705. if (status & ATH9K_INT_SWBA) {
  706. /* schedule a tasklet for beacon handling */
  707. tasklet_schedule(&sc->bcon_tasklet);
  708. }
  709. if (status & ATH9K_INT_RXEOL) {
  710. /*
  711. * NB: the hardware should re-read the link when
  712. * RXE bit is written, but it doesn't work
  713. * at least on older hardware revs.
  714. */
  715. sched = true;
  716. }
  717. if (status & ATH9K_INT_TXURN)
  718. /* bump tx trigger level */
  719. ath9k_hw_updatetxtriglevel(ah, true);
  720. /* XXX: optimize this */
  721. if (status & ATH9K_INT_RX)
  722. sched = true;
  723. if (status & ATH9K_INT_TX)
  724. sched = true;
  725. if (status & ATH9K_INT_BMISS)
  726. sched = true;
  727. /* carrier sense timeout */
  728. if (status & ATH9K_INT_CST)
  729. sched = true;
  730. if (status & ATH9K_INT_MIB) {
  731. /*
  732. * Disable interrupts until we service the MIB
  733. * interrupt; otherwise it will continue to
  734. * fire.
  735. */
  736. ath9k_hw_set_interrupts(ah, 0);
  737. /*
  738. * Let the hal handle the event. We assume
  739. * it will clear whatever condition caused
  740. * the interrupt.
  741. */
  742. ath9k_hw_procmibevent(ah, &sc->sc_halstats);
  743. ath9k_hw_set_interrupts(ah, sc->sc_imask);
  744. }
  745. if (status & ATH9K_INT_TIM_TIMER) {
  746. if (!(ah->ah_caps.hw_caps &
  747. ATH9K_HW_CAP_AUTOSLEEP)) {
  748. /* Clear RxAbort bit so that we can
  749. * receive frames */
  750. ath9k_hw_setrxabort(ah, 0);
  751. sched = true;
  752. }
  753. }
  754. }
  755. } while (0);
  756. if (sched) {
  757. /* turn off every interrupt except SWBA */
  758. ath9k_hw_set_interrupts(ah, (sc->sc_imask & ATH9K_INT_SWBA));
  759. tasklet_schedule(&sc->intr_tq);
  760. }
  761. return IRQ_HANDLED;
  762. }
  763. /* Deferred interrupt processing */
  764. static void ath9k_tasklet(unsigned long data)
  765. {
  766. struct ath_softc *sc = (struct ath_softc *)data;
  767. u32 status = sc->sc_intrstatus;
  768. if (status & ATH9K_INT_FATAL) {
  769. /* need a chip reset */
  770. ath_reset(sc, false);
  771. return;
  772. } else {
  773. if (status &
  774. (ATH9K_INT_RX | ATH9K_INT_RXEOL | ATH9K_INT_RXORN)) {
  775. /* XXX: fill me in */
  776. /*
  777. if (status & ATH9K_INT_RXORN) {
  778. }
  779. if (status & ATH9K_INT_RXEOL) {
  780. }
  781. */
  782. spin_lock_bh(&sc->sc_rxflushlock);
  783. ath_rx_tasklet(sc, 0);
  784. spin_unlock_bh(&sc->sc_rxflushlock);
  785. }
  786. /* XXX: optimize this */
  787. if (status & ATH9K_INT_TX)
  788. ath_tx_tasklet(sc);
  789. /* XXX: fill me in */
  790. /*
  791. if (status & ATH9K_INT_BMISS) {
  792. }
  793. if (status & (ATH9K_INT_TIM | ATH9K_INT_DTIMSYNC)) {
  794. if (status & ATH9K_INT_TIM) {
  795. }
  796. if (status & ATH9K_INT_DTIMSYNC) {
  797. }
  798. }
  799. */
  800. }
  801. /* re-enable hardware interrupt */
  802. ath9k_hw_set_interrupts(sc->sc_ah, sc->sc_imask);
  803. }
  804. int ath_init(u16 devid, struct ath_softc *sc)
  805. {
  806. struct ath_hal *ah = NULL;
  807. int status;
  808. int error = 0, i;
  809. int csz = 0;
  810. /* XXX: hardware will not be ready until ath_open() being called */
  811. sc->sc_flags |= SC_OP_INVALID;
  812. sc->sc_debug = DBG_DEFAULT;
  813. spin_lock_init(&sc->sc_resetlock);
  814. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  815. tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
  816. (unsigned long)sc);
  817. /*
  818. * Cache line size is used to size and align various
  819. * structures used to communicate with the hardware.
  820. */
  821. bus_read_cachesize(sc, &csz);
  822. /* XXX assert csz is non-zero */
  823. sc->sc_cachelsz = csz << 2; /* convert to bytes */
  824. ah = ath9k_hw_attach(devid, sc, sc->mem, &status);
  825. if (ah == NULL) {
  826. DPRINTF(sc, ATH_DBG_FATAL,
  827. "%s: unable to attach hardware; HAL status %u\n",
  828. __func__, status);
  829. error = -ENXIO;
  830. goto bad;
  831. }
  832. sc->sc_ah = ah;
  833. /* Get the hardware key cache size. */
  834. sc->sc_keymax = ah->ah_caps.keycache_size;
  835. if (sc->sc_keymax > ATH_KEYMAX) {
  836. DPRINTF(sc, ATH_DBG_KEYCACHE,
  837. "%s: Warning, using only %u entries in %u key cache\n",
  838. __func__, ATH_KEYMAX, sc->sc_keymax);
  839. sc->sc_keymax = ATH_KEYMAX;
  840. }
  841. /*
  842. * Reset the key cache since some parts do not
  843. * reset the contents on initial power up.
  844. */
  845. for (i = 0; i < sc->sc_keymax; i++)
  846. ath9k_hw_keyreset(ah, (u16) i);
  847. /*
  848. * Mark key cache slots associated with global keys
  849. * as in use. If we knew TKIP was not to be used we
  850. * could leave the +32, +64, and +32+64 slots free.
  851. * XXX only for splitmic.
  852. */
  853. for (i = 0; i < IEEE80211_WEP_NKID; i++) {
  854. set_bit(i, sc->sc_keymap);
  855. set_bit(i + 32, sc->sc_keymap);
  856. set_bit(i + 64, sc->sc_keymap);
  857. set_bit(i + 32 + 64, sc->sc_keymap);
  858. }
  859. /* Collect the channel list using the default country code */
  860. error = ath_setup_channels(sc);
  861. if (error)
  862. goto bad;
  863. /* default to MONITOR mode */
  864. sc->sc_ah->ah_opmode = ATH9K_M_MONITOR;
  865. /* Setup rate tables */
  866. ath_setup_rates(sc, IEEE80211_BAND_2GHZ);
  867. ath_setup_rates(sc, IEEE80211_BAND_5GHZ);
  868. /* NB: setup here so ath_rate_update is happy */
  869. ath_setcurmode(sc, ATH9K_MODE_11A);
  870. /*
  871. * Allocate hardware transmit queues: one queue for
  872. * beacon frames and one data queue for each QoS
  873. * priority. Note that the hal handles reseting
  874. * these queues at the needed time.
  875. */
  876. sc->sc_bhalq = ath_beaconq_setup(ah);
  877. if (sc->sc_bhalq == -1) {
  878. DPRINTF(sc, ATH_DBG_FATAL,
  879. "%s: unable to setup a beacon xmit queue\n", __func__);
  880. error = -EIO;
  881. goto bad2;
  882. }
  883. sc->sc_cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  884. if (sc->sc_cabq == NULL) {
  885. DPRINTF(sc, ATH_DBG_FATAL,
  886. "%s: unable to setup CAB xmit queue\n", __func__);
  887. error = -EIO;
  888. goto bad2;
  889. }
  890. sc->sc_config.cabqReadytime = ATH_CABQ_READY_TIME;
  891. ath_cabq_update(sc);
  892. for (i = 0; i < ARRAY_SIZE(sc->sc_haltype2q); i++)
  893. sc->sc_haltype2q[i] = -1;
  894. /* Setup data queues */
  895. /* NB: ensure BK queue is the lowest priority h/w queue */
  896. if (!ath_tx_setup(sc, ATH9K_WME_AC_BK)) {
  897. DPRINTF(sc, ATH_DBG_FATAL,
  898. "%s: unable to setup xmit queue for BK traffic\n",
  899. __func__);
  900. error = -EIO;
  901. goto bad2;
  902. }
  903. if (!ath_tx_setup(sc, ATH9K_WME_AC_BE)) {
  904. DPRINTF(sc, ATH_DBG_FATAL,
  905. "%s: unable to setup xmit queue for BE traffic\n",
  906. __func__);
  907. error = -EIO;
  908. goto bad2;
  909. }
  910. if (!ath_tx_setup(sc, ATH9K_WME_AC_VI)) {
  911. DPRINTF(sc, ATH_DBG_FATAL,
  912. "%s: unable to setup xmit queue for VI traffic\n",
  913. __func__);
  914. error = -EIO;
  915. goto bad2;
  916. }
  917. if (!ath_tx_setup(sc, ATH9K_WME_AC_VO)) {
  918. DPRINTF(sc, ATH_DBG_FATAL,
  919. "%s: unable to setup xmit queue for VO traffic\n",
  920. __func__);
  921. error = -EIO;
  922. goto bad2;
  923. }
  924. /* Initializes the noise floor to a reasonable default value.
  925. * Later on this will be updated during ANI processing. */
  926. sc->sc_ani.sc_noise_floor = ATH_DEFAULT_NOISE_FLOOR;
  927. setup_timer(&sc->sc_ani.timer, ath_ani_calibrate, (unsigned long)sc);
  928. sc->sc_rc = ath_rate_attach(sc);
  929. if (sc->sc_rc == NULL) {
  930. error = -EIO;
  931. goto bad2;
  932. }
  933. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  934. ATH9K_CIPHER_TKIP, NULL)) {
  935. /*
  936. * Whether we should enable h/w TKIP MIC.
  937. * XXX: if we don't support WME TKIP MIC, then we wouldn't
  938. * report WMM capable, so it's always safe to turn on
  939. * TKIP MIC in this case.
  940. */
  941. ath9k_hw_setcapability(sc->sc_ah, ATH9K_CAP_TKIP_MIC,
  942. 0, 1, NULL);
  943. }
  944. /*
  945. * Check whether the separate key cache entries
  946. * are required to handle both tx+rx MIC keys.
  947. * With split mic keys the number of stations is limited
  948. * to 27 otherwise 59.
  949. */
  950. if (ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  951. ATH9K_CIPHER_TKIP, NULL)
  952. && ath9k_hw_getcapability(ah, ATH9K_CAP_CIPHER,
  953. ATH9K_CIPHER_MIC, NULL)
  954. && ath9k_hw_getcapability(ah, ATH9K_CAP_TKIP_SPLIT,
  955. 0, NULL))
  956. sc->sc_splitmic = 1;
  957. /* turn on mcast key search if possible */
  958. if (!ath9k_hw_getcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 0, NULL))
  959. (void)ath9k_hw_setcapability(ah, ATH9K_CAP_MCAST_KEYSRCH, 1,
  960. 1, NULL);
  961. sc->sc_config.txpowlimit = ATH_TXPOWER_MAX;
  962. sc->sc_config.txpowlimit_override = 0;
  963. /* 11n Capabilities */
  964. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_HT) {
  965. sc->sc_flags |= SC_OP_TXAGGR;
  966. sc->sc_flags |= SC_OP_RXAGGR;
  967. }
  968. sc->sc_tx_chainmask = ah->ah_caps.tx_chainmask;
  969. sc->sc_rx_chainmask = ah->ah_caps.rx_chainmask;
  970. ath9k_hw_setcapability(ah, ATH9K_CAP_DIVERSITY, 1, true, NULL);
  971. sc->sc_defant = ath9k_hw_getdefantenna(ah);
  972. ath9k_hw_getmac(ah, sc->sc_myaddr);
  973. if (ah->ah_caps.hw_caps & ATH9K_HW_CAP_BSSIDMASK) {
  974. ath9k_hw_getbssidmask(ah, sc->sc_bssidmask);
  975. ATH_SET_VAP_BSSID_MASK(sc->sc_bssidmask);
  976. ath9k_hw_setbssidmask(ah, sc->sc_bssidmask);
  977. }
  978. sc->sc_slottime = ATH9K_SLOT_TIME_9; /* default to short slot time */
  979. /* initialize beacon slots */
  980. for (i = 0; i < ARRAY_SIZE(sc->sc_bslot); i++)
  981. sc->sc_bslot[i] = ATH_IF_ID_ANY;
  982. /* save MISC configurations */
  983. sc->sc_config.swBeaconProcess = 1;
  984. #ifdef CONFIG_SLOW_ANT_DIV
  985. /* range is 40 - 255, we use something in the middle */
  986. ath_slow_ant_div_init(&sc->sc_antdiv, sc, 0x127);
  987. #endif
  988. /* setup channels and rates */
  989. sc->sbands[IEEE80211_BAND_2GHZ].channels =
  990. sc->channels[IEEE80211_BAND_2GHZ];
  991. sc->sbands[IEEE80211_BAND_2GHZ].bitrates =
  992. sc->rates[IEEE80211_BAND_2GHZ];
  993. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  994. if (test_bit(ATH9K_MODE_11A, sc->sc_ah->ah_caps.wireless_modes)) {
  995. sc->sbands[IEEE80211_BAND_5GHZ].channels =
  996. sc->channels[IEEE80211_BAND_5GHZ];
  997. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  998. sc->rates[IEEE80211_BAND_5GHZ];
  999. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  1000. }
  1001. return 0;
  1002. bad2:
  1003. /* cleanup tx queues */
  1004. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1005. if (ATH_TXQ_SETUP(sc, i))
  1006. ath_tx_cleanupq(sc, &sc->sc_txq[i]);
  1007. bad:
  1008. if (ah)
  1009. ath9k_hw_detach(ah);
  1010. return error;
  1011. }
  1012. /*******************/
  1013. /* Node Management */
  1014. /*******************/
  1015. void ath_node_attach(struct ath_softc *sc, struct ieee80211_sta *sta)
  1016. {
  1017. struct ath_node *an;
  1018. an = (struct ath_node *)sta->drv_priv;
  1019. if (sc->sc_flags & SC_OP_TXAGGR)
  1020. ath_tx_node_init(sc, an);
  1021. an->maxampdu = 1 << (IEEE80211_HTCAP_MAXRXAMPDU_FACTOR +
  1022. sta->ht_cap.ampdu_factor);
  1023. an->mpdudensity = parse_mpdudensity(sta->ht_cap.ampdu_density);
  1024. ath_chainmask_sel_init(sc, an);
  1025. ath_chainmask_sel_timerstart(&an->an_chainmask_sel);
  1026. }
  1027. void ath_node_detach(struct ath_softc *sc, struct ieee80211_sta *sta)
  1028. {
  1029. struct ath_node *an = (struct ath_node *)sta->drv_priv;
  1030. ath_chainmask_sel_timerstop(&an->an_chainmask_sel);
  1031. if (sc->sc_flags & SC_OP_TXAGGR)
  1032. ath_tx_node_cleanup(sc, an);
  1033. }
  1034. /*
  1035. * Set up New Node
  1036. *
  1037. * Setup driver-specific state for a newly associated node. This routine
  1038. * really only applies if compression or XR are enabled, there is no code
  1039. * covering any other cases.
  1040. */
  1041. void ath_newassoc(struct ath_softc *sc,
  1042. struct ath_node *an, int isnew, int isuapsd)
  1043. {
  1044. int tidno;
  1045. /* if station reassociates, tear down the aggregation state. */
  1046. if (!isnew) {
  1047. for (tidno = 0; tidno < WME_NUM_TID; tidno++) {
  1048. if (sc->sc_flags & SC_OP_TXAGGR)
  1049. ath_tx_aggr_teardown(sc, an, tidno);
  1050. }
  1051. }
  1052. }
  1053. /**************/
  1054. /* Encryption */
  1055. /**************/
  1056. void ath_key_reset(struct ath_softc *sc, u16 keyix, int freeslot)
  1057. {
  1058. ath9k_hw_keyreset(sc->sc_ah, keyix);
  1059. if (freeslot)
  1060. clear_bit(keyix, sc->sc_keymap);
  1061. }
  1062. int ath_keyset(struct ath_softc *sc,
  1063. u16 keyix,
  1064. struct ath9k_keyval *hk,
  1065. const u8 mac[ETH_ALEN])
  1066. {
  1067. bool status;
  1068. status = ath9k_hw_set_keycache_entry(sc->sc_ah,
  1069. keyix, hk, mac, false);
  1070. return status != false;
  1071. }
  1072. /***********************/
  1073. /* TX Power/Regulatory */
  1074. /***********************/
  1075. /*
  1076. * Set Transmit power in HAL
  1077. *
  1078. * This routine makes the actual HAL calls to set the new transmit power
  1079. * limit.
  1080. */
  1081. void ath_update_txpow(struct ath_softc *sc)
  1082. {
  1083. struct ath_hal *ah = sc->sc_ah;
  1084. u32 txpow;
  1085. if (sc->sc_curtxpow != sc->sc_config.txpowlimit) {
  1086. ath9k_hw_set_txpowerlimit(ah, sc->sc_config.txpowlimit);
  1087. /* read back in case value is clamped */
  1088. ath9k_hw_getcapability(ah, ATH9K_CAP_TXPOW, 1, &txpow);
  1089. sc->sc_curtxpow = txpow;
  1090. }
  1091. }
  1092. /**************************/
  1093. /* Slow Antenna Diversity */
  1094. /**************************/
  1095. void ath_slow_ant_div_init(struct ath_antdiv *antdiv,
  1096. struct ath_softc *sc,
  1097. int32_t rssitrig)
  1098. {
  1099. int trig;
  1100. /* antdivf_rssitrig can range from 40 - 0xff */
  1101. trig = (rssitrig > 0xff) ? 0xff : rssitrig;
  1102. trig = (rssitrig < 40) ? 40 : rssitrig;
  1103. antdiv->antdiv_sc = sc;
  1104. antdiv->antdivf_rssitrig = trig;
  1105. }
  1106. void ath_slow_ant_div_start(struct ath_antdiv *antdiv,
  1107. u8 num_antcfg,
  1108. const u8 *bssid)
  1109. {
  1110. antdiv->antdiv_num_antcfg =
  1111. num_antcfg < ATH_ANT_DIV_MAX_CFG ?
  1112. num_antcfg : ATH_ANT_DIV_MAX_CFG;
  1113. antdiv->antdiv_state = ATH_ANT_DIV_IDLE;
  1114. antdiv->antdiv_curcfg = 0;
  1115. antdiv->antdiv_bestcfg = 0;
  1116. antdiv->antdiv_laststatetsf = 0;
  1117. memcpy(antdiv->antdiv_bssid, bssid, sizeof(antdiv->antdiv_bssid));
  1118. antdiv->antdiv_start = 1;
  1119. }
  1120. void ath_slow_ant_div_stop(struct ath_antdiv *antdiv)
  1121. {
  1122. antdiv->antdiv_start = 0;
  1123. }
  1124. static int32_t ath_find_max_val(int32_t *val,
  1125. u8 num_val, u8 *max_index)
  1126. {
  1127. u32 MaxVal = *val++;
  1128. u32 cur_index = 0;
  1129. *max_index = 0;
  1130. while (++cur_index < num_val) {
  1131. if (*val > MaxVal) {
  1132. MaxVal = *val;
  1133. *max_index = cur_index;
  1134. }
  1135. val++;
  1136. }
  1137. return MaxVal;
  1138. }
  1139. void ath_slow_ant_div(struct ath_antdiv *antdiv,
  1140. struct ieee80211_hdr *hdr,
  1141. struct ath_rx_status *rx_stats)
  1142. {
  1143. struct ath_softc *sc = antdiv->antdiv_sc;
  1144. struct ath_hal *ah = sc->sc_ah;
  1145. u64 curtsf = 0;
  1146. u8 bestcfg, curcfg = antdiv->antdiv_curcfg;
  1147. __le16 fc = hdr->frame_control;
  1148. if (antdiv->antdiv_start && ieee80211_is_beacon(fc)
  1149. && !compare_ether_addr(hdr->addr3, antdiv->antdiv_bssid)) {
  1150. antdiv->antdiv_lastbrssi[curcfg] = rx_stats->rs_rssi;
  1151. antdiv->antdiv_lastbtsf[curcfg] = ath9k_hw_gettsf64(sc->sc_ah);
  1152. curtsf = antdiv->antdiv_lastbtsf[curcfg];
  1153. } else {
  1154. return;
  1155. }
  1156. switch (antdiv->antdiv_state) {
  1157. case ATH_ANT_DIV_IDLE:
  1158. if ((antdiv->antdiv_lastbrssi[curcfg] <
  1159. antdiv->antdivf_rssitrig)
  1160. && ((curtsf - antdiv->antdiv_laststatetsf) >
  1161. ATH_ANT_DIV_MIN_IDLE_US)) {
  1162. curcfg++;
  1163. if (curcfg == antdiv->antdiv_num_antcfg)
  1164. curcfg = 0;
  1165. if (!ath9k_hw_select_antconfig(ah, curcfg)) {
  1166. antdiv->antdiv_bestcfg = antdiv->antdiv_curcfg;
  1167. antdiv->antdiv_curcfg = curcfg;
  1168. antdiv->antdiv_laststatetsf = curtsf;
  1169. antdiv->antdiv_state = ATH_ANT_DIV_SCAN;
  1170. }
  1171. }
  1172. break;
  1173. case ATH_ANT_DIV_SCAN:
  1174. if ((curtsf - antdiv->antdiv_laststatetsf) <
  1175. ATH_ANT_DIV_MIN_SCAN_US)
  1176. break;
  1177. curcfg++;
  1178. if (curcfg == antdiv->antdiv_num_antcfg)
  1179. curcfg = 0;
  1180. if (curcfg == antdiv->antdiv_bestcfg) {
  1181. ath_find_max_val(antdiv->antdiv_lastbrssi,
  1182. antdiv->antdiv_num_antcfg, &bestcfg);
  1183. if (!ath9k_hw_select_antconfig(ah, bestcfg)) {
  1184. antdiv->antdiv_bestcfg = bestcfg;
  1185. antdiv->antdiv_curcfg = bestcfg;
  1186. antdiv->antdiv_laststatetsf = curtsf;
  1187. antdiv->antdiv_state = ATH_ANT_DIV_IDLE;
  1188. }
  1189. } else {
  1190. if (!ath9k_hw_select_antconfig(ah, curcfg)) {
  1191. antdiv->antdiv_curcfg = curcfg;
  1192. antdiv->antdiv_laststatetsf = curtsf;
  1193. antdiv->antdiv_state = ATH_ANT_DIV_SCAN;
  1194. }
  1195. }
  1196. break;
  1197. }
  1198. }
  1199. /***********************/
  1200. /* Descriptor Handling */
  1201. /***********************/
  1202. /*
  1203. * Set up DMA descriptors
  1204. *
  1205. * This function will allocate both the DMA descriptor structure, and the
  1206. * buffers it contains. These are used to contain the descriptors used
  1207. * by the system.
  1208. */
  1209. int ath_descdma_setup(struct ath_softc *sc,
  1210. struct ath_descdma *dd,
  1211. struct list_head *head,
  1212. const char *name,
  1213. int nbuf,
  1214. int ndesc)
  1215. {
  1216. #define DS2PHYS(_dd, _ds) \
  1217. ((_dd)->dd_desc_paddr + ((caddr_t)(_ds) - (caddr_t)(_dd)->dd_desc))
  1218. #define ATH_DESC_4KB_BOUND_CHECK(_daddr) ((((_daddr) & 0xFFF) > 0xF7F) ? 1 : 0)
  1219. #define ATH_DESC_4KB_BOUND_NUM_SKIPPED(_len) ((_len) / 4096)
  1220. struct ath_desc *ds;
  1221. struct ath_buf *bf;
  1222. int i, bsize, error;
  1223. DPRINTF(sc, ATH_DBG_CONFIG, "%s: %s DMA: %u buffers %u desc/buf\n",
  1224. __func__, name, nbuf, ndesc);
  1225. /* ath_desc must be a multiple of DWORDs */
  1226. if ((sizeof(struct ath_desc) % 4) != 0) {
  1227. DPRINTF(sc, ATH_DBG_FATAL, "%s: ath_desc not DWORD aligned\n",
  1228. __func__);
  1229. ASSERT((sizeof(struct ath_desc) % 4) == 0);
  1230. error = -ENOMEM;
  1231. goto fail;
  1232. }
  1233. dd->dd_name = name;
  1234. dd->dd_desc_len = sizeof(struct ath_desc) * nbuf * ndesc;
  1235. /*
  1236. * Need additional DMA memory because we can't use
  1237. * descriptors that cross the 4K page boundary. Assume
  1238. * one skipped descriptor per 4K page.
  1239. */
  1240. if (!(sc->sc_ah->ah_caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1241. u32 ndesc_skipped =
  1242. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  1243. u32 dma_len;
  1244. while (ndesc_skipped) {
  1245. dma_len = ndesc_skipped * sizeof(struct ath_desc);
  1246. dd->dd_desc_len += dma_len;
  1247. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  1248. };
  1249. }
  1250. /* allocate descriptors */
  1251. dd->dd_desc = pci_alloc_consistent(sc->pdev,
  1252. dd->dd_desc_len,
  1253. &dd->dd_desc_paddr);
  1254. if (dd->dd_desc == NULL) {
  1255. error = -ENOMEM;
  1256. goto fail;
  1257. }
  1258. ds = dd->dd_desc;
  1259. DPRINTF(sc, ATH_DBG_CONFIG, "%s: %s DMA map: %p (%u) -> %llx (%u)\n",
  1260. __func__, dd->dd_name, ds, (u32) dd->dd_desc_len,
  1261. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  1262. /* allocate buffers */
  1263. bsize = sizeof(struct ath_buf) * nbuf;
  1264. bf = kmalloc(bsize, GFP_KERNEL);
  1265. if (bf == NULL) {
  1266. error = -ENOMEM;
  1267. goto fail2;
  1268. }
  1269. memset(bf, 0, bsize);
  1270. dd->dd_bufptr = bf;
  1271. INIT_LIST_HEAD(head);
  1272. for (i = 0; i < nbuf; i++, bf++, ds += ndesc) {
  1273. bf->bf_desc = ds;
  1274. bf->bf_daddr = DS2PHYS(dd, ds);
  1275. if (!(sc->sc_ah->ah_caps.hw_caps &
  1276. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  1277. /*
  1278. * Skip descriptor addresses which can cause 4KB
  1279. * boundary crossing (addr + length) with a 32 dword
  1280. * descriptor fetch.
  1281. */
  1282. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  1283. ASSERT((caddr_t) bf->bf_desc <
  1284. ((caddr_t) dd->dd_desc +
  1285. dd->dd_desc_len));
  1286. ds += ndesc;
  1287. bf->bf_desc = ds;
  1288. bf->bf_daddr = DS2PHYS(dd, ds);
  1289. }
  1290. }
  1291. list_add_tail(&bf->list, head);
  1292. }
  1293. return 0;
  1294. fail2:
  1295. pci_free_consistent(sc->pdev,
  1296. dd->dd_desc_len, dd->dd_desc, dd->dd_desc_paddr);
  1297. fail:
  1298. memset(dd, 0, sizeof(*dd));
  1299. return error;
  1300. #undef ATH_DESC_4KB_BOUND_CHECK
  1301. #undef ATH_DESC_4KB_BOUND_NUM_SKIPPED
  1302. #undef DS2PHYS
  1303. }
  1304. /*
  1305. * Cleanup DMA descriptors
  1306. *
  1307. * This function will free the DMA block that was allocated for the descriptor
  1308. * pool. Since this was allocated as one "chunk", it is freed in the same
  1309. * manner.
  1310. */
  1311. void ath_descdma_cleanup(struct ath_softc *sc,
  1312. struct ath_descdma *dd,
  1313. struct list_head *head)
  1314. {
  1315. /* Free memory associated with descriptors */
  1316. pci_free_consistent(sc->pdev,
  1317. dd->dd_desc_len, dd->dd_desc, dd->dd_desc_paddr);
  1318. INIT_LIST_HEAD(head);
  1319. kfree(dd->dd_bufptr);
  1320. memset(dd, 0, sizeof(*dd));
  1321. }
  1322. /*************/
  1323. /* Utilities */
  1324. /*************/
  1325. int ath_get_hal_qnum(u16 queue, struct ath_softc *sc)
  1326. {
  1327. int qnum;
  1328. switch (queue) {
  1329. case 0:
  1330. qnum = sc->sc_haltype2q[ATH9K_WME_AC_VO];
  1331. break;
  1332. case 1:
  1333. qnum = sc->sc_haltype2q[ATH9K_WME_AC_VI];
  1334. break;
  1335. case 2:
  1336. qnum = sc->sc_haltype2q[ATH9K_WME_AC_BE];
  1337. break;
  1338. case 3:
  1339. qnum = sc->sc_haltype2q[ATH9K_WME_AC_BK];
  1340. break;
  1341. default:
  1342. qnum = sc->sc_haltype2q[ATH9K_WME_AC_BE];
  1343. break;
  1344. }
  1345. return qnum;
  1346. }
  1347. int ath_get_mac80211_qnum(u32 queue, struct ath_softc *sc)
  1348. {
  1349. int qnum;
  1350. switch (queue) {
  1351. case ATH9K_WME_AC_VO:
  1352. qnum = 0;
  1353. break;
  1354. case ATH9K_WME_AC_VI:
  1355. qnum = 1;
  1356. break;
  1357. case ATH9K_WME_AC_BE:
  1358. qnum = 2;
  1359. break;
  1360. case ATH9K_WME_AC_BK:
  1361. qnum = 3;
  1362. break;
  1363. default:
  1364. qnum = -1;
  1365. break;
  1366. }
  1367. return qnum;
  1368. }
  1369. /*
  1370. * Expand time stamp to TSF
  1371. *
  1372. * Extend 15-bit time stamp from rx descriptor to
  1373. * a full 64-bit TSF using the current h/w TSF.
  1374. */
  1375. u64 ath_extend_tsf(struct ath_softc *sc, u32 rstamp)
  1376. {
  1377. u64 tsf;
  1378. tsf = ath9k_hw_gettsf64(sc->sc_ah);
  1379. if ((tsf & 0x7fff) < rstamp)
  1380. tsf -= 0x8000;
  1381. return (tsf & ~0x7fff) | rstamp;
  1382. }
  1383. /*
  1384. * Set Default Antenna
  1385. *
  1386. * Call into the HAL to set the default antenna to use. Not really valid for
  1387. * MIMO technology.
  1388. */
  1389. void ath_setdefantenna(void *context, u32 antenna)
  1390. {
  1391. struct ath_softc *sc = (struct ath_softc *)context;
  1392. struct ath_hal *ah = sc->sc_ah;
  1393. /* XXX block beacon interrupts */
  1394. ath9k_hw_setantenna(ah, antenna);
  1395. sc->sc_defant = antenna;
  1396. sc->sc_rxotherant = 0;
  1397. }
  1398. /*
  1399. * Set Slot Time
  1400. *
  1401. * This will wake up the chip if required, and set the slot time for the
  1402. * frame (maximum transmit time). Slot time is assumed to be already set
  1403. * in the ATH object member sc_slottime
  1404. */
  1405. void ath_setslottime(struct ath_softc *sc)
  1406. {
  1407. ath9k_hw_setslottime(sc->sc_ah, sc->sc_slottime);
  1408. sc->sc_updateslot = OK;
  1409. }