core.c 47 KB

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