core.c 46 KB

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