core.c 49 KB

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