core.c 48 KB

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