init.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * Copyright (c) 2008-2011 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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/dma-mapping.h>
  18. #include <linux/slab.h>
  19. #include <linux/ath9k_platform.h>
  20. #include <linux/module.h>
  21. #include <linux/relay.h>
  22. #include "ath9k.h"
  23. struct ath9k_eeprom_ctx {
  24. struct completion complete;
  25. struct ath_hw *ah;
  26. };
  27. static char *dev_info = "ath9k";
  28. MODULE_AUTHOR("Atheros Communications");
  29. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  30. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  31. MODULE_LICENSE("Dual BSD/GPL");
  32. static unsigned int ath9k_debug = ATH_DBG_DEFAULT;
  33. module_param_named(debug, ath9k_debug, uint, 0);
  34. MODULE_PARM_DESC(debug, "Debugging mask");
  35. int ath9k_modparam_nohwcrypt;
  36. module_param_named(nohwcrypt, ath9k_modparam_nohwcrypt, int, 0444);
  37. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption");
  38. int led_blink;
  39. module_param_named(blink, led_blink, int, 0444);
  40. MODULE_PARM_DESC(blink, "Enable LED blink on activity");
  41. static int ath9k_btcoex_enable;
  42. module_param_named(btcoex_enable, ath9k_btcoex_enable, int, 0444);
  43. MODULE_PARM_DESC(btcoex_enable, "Enable wifi-BT coexistence");
  44. static int ath9k_enable_diversity;
  45. module_param_named(enable_diversity, ath9k_enable_diversity, int, 0444);
  46. MODULE_PARM_DESC(enable_diversity, "Enable Antenna diversity for AR9565");
  47. bool is_ath9k_unloaded;
  48. /* We use the hw_value as an index into our private channel structure */
  49. #define CHAN2G(_freq, _idx) { \
  50. .band = IEEE80211_BAND_2GHZ, \
  51. .center_freq = (_freq), \
  52. .hw_value = (_idx), \
  53. .max_power = 20, \
  54. }
  55. #define CHAN5G(_freq, _idx) { \
  56. .band = IEEE80211_BAND_5GHZ, \
  57. .center_freq = (_freq), \
  58. .hw_value = (_idx), \
  59. .max_power = 20, \
  60. }
  61. /* Some 2 GHz radios are actually tunable on 2312-2732
  62. * on 5 MHz steps, we support the channels which we know
  63. * we have calibration data for all cards though to make
  64. * this static */
  65. static const struct ieee80211_channel ath9k_2ghz_chantable[] = {
  66. CHAN2G(2412, 0), /* Channel 1 */
  67. CHAN2G(2417, 1), /* Channel 2 */
  68. CHAN2G(2422, 2), /* Channel 3 */
  69. CHAN2G(2427, 3), /* Channel 4 */
  70. CHAN2G(2432, 4), /* Channel 5 */
  71. CHAN2G(2437, 5), /* Channel 6 */
  72. CHAN2G(2442, 6), /* Channel 7 */
  73. CHAN2G(2447, 7), /* Channel 8 */
  74. CHAN2G(2452, 8), /* Channel 9 */
  75. CHAN2G(2457, 9), /* Channel 10 */
  76. CHAN2G(2462, 10), /* Channel 11 */
  77. CHAN2G(2467, 11), /* Channel 12 */
  78. CHAN2G(2472, 12), /* Channel 13 */
  79. CHAN2G(2484, 13), /* Channel 14 */
  80. };
  81. /* Some 5 GHz radios are actually tunable on XXXX-YYYY
  82. * on 5 MHz steps, we support the channels which we know
  83. * we have calibration data for all cards though to make
  84. * this static */
  85. static const struct ieee80211_channel ath9k_5ghz_chantable[] = {
  86. /* _We_ call this UNII 1 */
  87. CHAN5G(5180, 14), /* Channel 36 */
  88. CHAN5G(5200, 15), /* Channel 40 */
  89. CHAN5G(5220, 16), /* Channel 44 */
  90. CHAN5G(5240, 17), /* Channel 48 */
  91. /* _We_ call this UNII 2 */
  92. CHAN5G(5260, 18), /* Channel 52 */
  93. CHAN5G(5280, 19), /* Channel 56 */
  94. CHAN5G(5300, 20), /* Channel 60 */
  95. CHAN5G(5320, 21), /* Channel 64 */
  96. /* _We_ call this "Middle band" */
  97. CHAN5G(5500, 22), /* Channel 100 */
  98. CHAN5G(5520, 23), /* Channel 104 */
  99. CHAN5G(5540, 24), /* Channel 108 */
  100. CHAN5G(5560, 25), /* Channel 112 */
  101. CHAN5G(5580, 26), /* Channel 116 */
  102. CHAN5G(5600, 27), /* Channel 120 */
  103. CHAN5G(5620, 28), /* Channel 124 */
  104. CHAN5G(5640, 29), /* Channel 128 */
  105. CHAN5G(5660, 30), /* Channel 132 */
  106. CHAN5G(5680, 31), /* Channel 136 */
  107. CHAN5G(5700, 32), /* Channel 140 */
  108. /* _We_ call this UNII 3 */
  109. CHAN5G(5745, 33), /* Channel 149 */
  110. CHAN5G(5765, 34), /* Channel 153 */
  111. CHAN5G(5785, 35), /* Channel 157 */
  112. CHAN5G(5805, 36), /* Channel 161 */
  113. CHAN5G(5825, 37), /* Channel 165 */
  114. };
  115. /* Atheros hardware rate code addition for short premble */
  116. #define SHPCHECK(__hw_rate, __flags) \
  117. ((__flags & IEEE80211_RATE_SHORT_PREAMBLE) ? (__hw_rate | 0x04 ) : 0)
  118. #define RATE(_bitrate, _hw_rate, _flags) { \
  119. .bitrate = (_bitrate), \
  120. .flags = (_flags), \
  121. .hw_value = (_hw_rate), \
  122. .hw_value_short = (SHPCHECK(_hw_rate, _flags)) \
  123. }
  124. static struct ieee80211_rate ath9k_legacy_rates[] = {
  125. RATE(10, 0x1b, 0),
  126. RATE(20, 0x1a, IEEE80211_RATE_SHORT_PREAMBLE),
  127. RATE(55, 0x19, IEEE80211_RATE_SHORT_PREAMBLE),
  128. RATE(110, 0x18, IEEE80211_RATE_SHORT_PREAMBLE),
  129. RATE(60, 0x0b, 0),
  130. RATE(90, 0x0f, 0),
  131. RATE(120, 0x0a, 0),
  132. RATE(180, 0x0e, 0),
  133. RATE(240, 0x09, 0),
  134. RATE(360, 0x0d, 0),
  135. RATE(480, 0x08, 0),
  136. RATE(540, 0x0c, 0),
  137. };
  138. #ifdef CONFIG_MAC80211_LEDS
  139. static const struct ieee80211_tpt_blink ath9k_tpt_blink[] = {
  140. { .throughput = 0 * 1024, .blink_time = 334 },
  141. { .throughput = 1 * 1024, .blink_time = 260 },
  142. { .throughput = 5 * 1024, .blink_time = 220 },
  143. { .throughput = 10 * 1024, .blink_time = 190 },
  144. { .throughput = 20 * 1024, .blink_time = 170 },
  145. { .throughput = 50 * 1024, .blink_time = 150 },
  146. { .throughput = 70 * 1024, .blink_time = 130 },
  147. { .throughput = 100 * 1024, .blink_time = 110 },
  148. { .throughput = 200 * 1024, .blink_time = 80 },
  149. { .throughput = 300 * 1024, .blink_time = 50 },
  150. };
  151. #endif
  152. static void ath9k_deinit_softc(struct ath_softc *sc);
  153. /*
  154. * Read and write, they both share the same lock. We do this to serialize
  155. * reads and writes on Atheros 802.11n PCI devices only. This is required
  156. * as the FIFO on these devices can only accept sanely 2 requests.
  157. */
  158. static void ath9k_iowrite32(void *hw_priv, u32 val, u32 reg_offset)
  159. {
  160. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  161. struct ath_common *common = ath9k_hw_common(ah);
  162. struct ath_softc *sc = (struct ath_softc *) common->priv;
  163. if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_ON) {
  164. unsigned long flags;
  165. spin_lock_irqsave(&sc->sc_serial_rw, flags);
  166. iowrite32(val, sc->mem + reg_offset);
  167. spin_unlock_irqrestore(&sc->sc_serial_rw, flags);
  168. } else
  169. iowrite32(val, sc->mem + reg_offset);
  170. }
  171. static unsigned int ath9k_ioread32(void *hw_priv, u32 reg_offset)
  172. {
  173. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  174. struct ath_common *common = ath9k_hw_common(ah);
  175. struct ath_softc *sc = (struct ath_softc *) common->priv;
  176. u32 val;
  177. if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_ON) {
  178. unsigned long flags;
  179. spin_lock_irqsave(&sc->sc_serial_rw, flags);
  180. val = ioread32(sc->mem + reg_offset);
  181. spin_unlock_irqrestore(&sc->sc_serial_rw, flags);
  182. } else
  183. val = ioread32(sc->mem + reg_offset);
  184. return val;
  185. }
  186. static unsigned int __ath9k_reg_rmw(struct ath_softc *sc, u32 reg_offset,
  187. u32 set, u32 clr)
  188. {
  189. u32 val;
  190. val = ioread32(sc->mem + reg_offset);
  191. val &= ~clr;
  192. val |= set;
  193. iowrite32(val, sc->mem + reg_offset);
  194. return val;
  195. }
  196. static unsigned int ath9k_reg_rmw(void *hw_priv, u32 reg_offset, u32 set, u32 clr)
  197. {
  198. struct ath_hw *ah = (struct ath_hw *) hw_priv;
  199. struct ath_common *common = ath9k_hw_common(ah);
  200. struct ath_softc *sc = (struct ath_softc *) common->priv;
  201. unsigned long uninitialized_var(flags);
  202. u32 val;
  203. if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_ON) {
  204. spin_lock_irqsave(&sc->sc_serial_rw, flags);
  205. val = __ath9k_reg_rmw(sc, reg_offset, set, clr);
  206. spin_unlock_irqrestore(&sc->sc_serial_rw, flags);
  207. } else
  208. val = __ath9k_reg_rmw(sc, reg_offset, set, clr);
  209. return val;
  210. }
  211. /**************************/
  212. /* Initialization */
  213. /**************************/
  214. static void setup_ht_cap(struct ath_softc *sc,
  215. struct ieee80211_sta_ht_cap *ht_info)
  216. {
  217. struct ath_hw *ah = sc->sc_ah;
  218. struct ath_common *common = ath9k_hw_common(ah);
  219. u8 tx_streams, rx_streams;
  220. int i, max_streams;
  221. ht_info->ht_supported = true;
  222. ht_info->cap = IEEE80211_HT_CAP_SUP_WIDTH_20_40 |
  223. IEEE80211_HT_CAP_SM_PS |
  224. IEEE80211_HT_CAP_SGI_40 |
  225. IEEE80211_HT_CAP_DSSSCCK40;
  226. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_LDPC)
  227. ht_info->cap |= IEEE80211_HT_CAP_LDPC_CODING;
  228. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_SGI_20)
  229. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  230. ht_info->ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
  231. ht_info->ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
  232. if (AR_SREV_9330(ah) || AR_SREV_9485(ah) || AR_SREV_9565(ah))
  233. max_streams = 1;
  234. else if (AR_SREV_9462(ah))
  235. max_streams = 2;
  236. else if (AR_SREV_9300_20_OR_LATER(ah))
  237. max_streams = 3;
  238. else
  239. max_streams = 2;
  240. if (AR_SREV_9280_20_OR_LATER(ah)) {
  241. if (max_streams >= 2)
  242. ht_info->cap |= IEEE80211_HT_CAP_TX_STBC;
  243. ht_info->cap |= (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT);
  244. }
  245. /* set up supported mcs set */
  246. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  247. tx_streams = ath9k_cmn_count_streams(ah->txchainmask, max_streams);
  248. rx_streams = ath9k_cmn_count_streams(ah->rxchainmask, max_streams);
  249. ath_dbg(common, CONFIG, "TX streams %d, RX streams: %d\n",
  250. tx_streams, rx_streams);
  251. if (tx_streams != rx_streams) {
  252. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  253. ht_info->mcs.tx_params |= ((tx_streams - 1) <<
  254. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  255. }
  256. for (i = 0; i < rx_streams; i++)
  257. ht_info->mcs.rx_mask[i] = 0xff;
  258. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
  259. }
  260. static void ath9k_reg_notifier(struct wiphy *wiphy,
  261. struct regulatory_request *request)
  262. {
  263. struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
  264. struct ath_softc *sc = hw->priv;
  265. struct ath_hw *ah = sc->sc_ah;
  266. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  267. ath_reg_notifier_apply(wiphy, request, reg);
  268. /* Set tx power */
  269. if (ah->curchan) {
  270. sc->config.txpowlimit = 2 * ah->curchan->chan->max_power;
  271. ath9k_ps_wakeup(sc);
  272. ath9k_hw_set_txpowerlimit(ah, sc->config.txpowlimit, false);
  273. sc->curtxpow = ath9k_hw_regulatory(ah)->power_limit;
  274. ath9k_ps_restore(sc);
  275. }
  276. }
  277. /*
  278. * This function will allocate both the DMA descriptor structure, and the
  279. * buffers it contains. These are used to contain the descriptors used
  280. * by the system.
  281. */
  282. int ath_descdma_setup(struct ath_softc *sc, struct ath_descdma *dd,
  283. struct list_head *head, const char *name,
  284. int nbuf, int ndesc, bool is_tx)
  285. {
  286. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  287. u8 *ds;
  288. struct ath_buf *bf;
  289. int i, bsize, desc_len;
  290. ath_dbg(common, CONFIG, "%s DMA: %u buffers %u desc/buf\n",
  291. name, nbuf, ndesc);
  292. INIT_LIST_HEAD(head);
  293. if (is_tx)
  294. desc_len = sc->sc_ah->caps.tx_desc_len;
  295. else
  296. desc_len = sizeof(struct ath_desc);
  297. /* ath_desc must be a multiple of DWORDs */
  298. if ((desc_len % 4) != 0) {
  299. ath_err(common, "ath_desc not DWORD aligned\n");
  300. BUG_ON((desc_len % 4) != 0);
  301. return -ENOMEM;
  302. }
  303. dd->dd_desc_len = desc_len * nbuf * ndesc;
  304. /*
  305. * Need additional DMA memory because we can't use
  306. * descriptors that cross the 4K page boundary. Assume
  307. * one skipped descriptor per 4K page.
  308. */
  309. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  310. u32 ndesc_skipped =
  311. ATH_DESC_4KB_BOUND_NUM_SKIPPED(dd->dd_desc_len);
  312. u32 dma_len;
  313. while (ndesc_skipped) {
  314. dma_len = ndesc_skipped * desc_len;
  315. dd->dd_desc_len += dma_len;
  316. ndesc_skipped = ATH_DESC_4KB_BOUND_NUM_SKIPPED(dma_len);
  317. }
  318. }
  319. /* allocate descriptors */
  320. dd->dd_desc = dmam_alloc_coherent(sc->dev, dd->dd_desc_len,
  321. &dd->dd_desc_paddr, GFP_KERNEL);
  322. if (!dd->dd_desc)
  323. return -ENOMEM;
  324. ds = (u8 *) dd->dd_desc;
  325. ath_dbg(common, CONFIG, "%s DMA map: %p (%u) -> %llx (%u)\n",
  326. name, ds, (u32) dd->dd_desc_len,
  327. ito64(dd->dd_desc_paddr), /*XXX*/(u32) dd->dd_desc_len);
  328. /* allocate buffers */
  329. bsize = sizeof(struct ath_buf) * nbuf;
  330. bf = devm_kzalloc(sc->dev, bsize, GFP_KERNEL);
  331. if (!bf)
  332. return -ENOMEM;
  333. for (i = 0; i < nbuf; i++, bf++, ds += (desc_len * ndesc)) {
  334. bf->bf_desc = ds;
  335. bf->bf_daddr = DS2PHYS(dd, ds);
  336. if (!(sc->sc_ah->caps.hw_caps &
  337. ATH9K_HW_CAP_4KB_SPLITTRANS)) {
  338. /*
  339. * Skip descriptor addresses which can cause 4KB
  340. * boundary crossing (addr + length) with a 32 dword
  341. * descriptor fetch.
  342. */
  343. while (ATH_DESC_4KB_BOUND_CHECK(bf->bf_daddr)) {
  344. BUG_ON((caddr_t) bf->bf_desc >=
  345. ((caddr_t) dd->dd_desc +
  346. dd->dd_desc_len));
  347. ds += (desc_len * ndesc);
  348. bf->bf_desc = ds;
  349. bf->bf_daddr = DS2PHYS(dd, ds);
  350. }
  351. }
  352. list_add_tail(&bf->list, head);
  353. }
  354. return 0;
  355. }
  356. static int ath9k_init_queues(struct ath_softc *sc)
  357. {
  358. int i = 0;
  359. sc->beacon.beaconq = ath9k_hw_beaconq_setup(sc->sc_ah);
  360. sc->beacon.cabq = ath_txq_setup(sc, ATH9K_TX_QUEUE_CAB, 0);
  361. sc->config.cabqReadytime = ATH_CABQ_READY_TIME;
  362. ath_cabq_update(sc);
  363. for (i = 0; i < IEEE80211_NUM_ACS; i++) {
  364. sc->tx.txq_map[i] = ath_txq_setup(sc, ATH9K_TX_QUEUE_DATA, i);
  365. sc->tx.txq_map[i]->mac80211_qnum = i;
  366. sc->tx.txq_max_pending[i] = ATH_MAX_QDEPTH;
  367. }
  368. return 0;
  369. }
  370. static int ath9k_init_channels_rates(struct ath_softc *sc)
  371. {
  372. void *channels;
  373. BUILD_BUG_ON(ARRAY_SIZE(ath9k_2ghz_chantable) +
  374. ARRAY_SIZE(ath9k_5ghz_chantable) !=
  375. ATH9K_NUM_CHANNELS);
  376. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ) {
  377. channels = devm_kzalloc(sc->dev,
  378. sizeof(ath9k_2ghz_chantable), GFP_KERNEL);
  379. if (!channels)
  380. return -ENOMEM;
  381. memcpy(channels, ath9k_2ghz_chantable,
  382. sizeof(ath9k_2ghz_chantable));
  383. sc->sbands[IEEE80211_BAND_2GHZ].channels = channels;
  384. sc->sbands[IEEE80211_BAND_2GHZ].band = IEEE80211_BAND_2GHZ;
  385. sc->sbands[IEEE80211_BAND_2GHZ].n_channels =
  386. ARRAY_SIZE(ath9k_2ghz_chantable);
  387. sc->sbands[IEEE80211_BAND_2GHZ].bitrates = ath9k_legacy_rates;
  388. sc->sbands[IEEE80211_BAND_2GHZ].n_bitrates =
  389. ARRAY_SIZE(ath9k_legacy_rates);
  390. }
  391. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ) {
  392. channels = devm_kzalloc(sc->dev,
  393. sizeof(ath9k_5ghz_chantable), GFP_KERNEL);
  394. if (!channels)
  395. return -ENOMEM;
  396. memcpy(channels, ath9k_5ghz_chantable,
  397. sizeof(ath9k_5ghz_chantable));
  398. sc->sbands[IEEE80211_BAND_5GHZ].channels = channels;
  399. sc->sbands[IEEE80211_BAND_5GHZ].band = IEEE80211_BAND_5GHZ;
  400. sc->sbands[IEEE80211_BAND_5GHZ].n_channels =
  401. ARRAY_SIZE(ath9k_5ghz_chantable);
  402. sc->sbands[IEEE80211_BAND_5GHZ].bitrates =
  403. ath9k_legacy_rates + 4;
  404. sc->sbands[IEEE80211_BAND_5GHZ].n_bitrates =
  405. ARRAY_SIZE(ath9k_legacy_rates) - 4;
  406. }
  407. return 0;
  408. }
  409. static void ath9k_init_misc(struct ath_softc *sc)
  410. {
  411. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  412. int i = 0;
  413. setup_timer(&common->ani.timer, ath_ani_calibrate, (unsigned long)sc);
  414. sc->last_rssi = ATH_RSSI_DUMMY_MARKER;
  415. sc->config.txpowlimit = ATH_TXPOWER_MAX;
  416. memcpy(common->bssidmask, ath_bcast_mac, ETH_ALEN);
  417. sc->beacon.slottime = ATH9K_SLOT_TIME_9;
  418. for (i = 0; i < ARRAY_SIZE(sc->beacon.bslot); i++)
  419. sc->beacon.bslot[i] = NULL;
  420. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
  421. sc->ant_comb.count = ATH_ANT_DIV_COMB_INIT_COUNT;
  422. sc->spec_config.enabled = 0;
  423. sc->spec_config.short_repeat = true;
  424. sc->spec_config.count = 8;
  425. sc->spec_config.endless = false;
  426. sc->spec_config.period = 0xFF;
  427. sc->spec_config.fft_period = 0xF;
  428. }
  429. static void ath9k_eeprom_request_cb(const struct firmware *eeprom_blob,
  430. void *ctx)
  431. {
  432. struct ath9k_eeprom_ctx *ec = ctx;
  433. if (eeprom_blob)
  434. ec->ah->eeprom_blob = eeprom_blob;
  435. complete(&ec->complete);
  436. }
  437. static int ath9k_eeprom_request(struct ath_softc *sc, const char *name)
  438. {
  439. struct ath9k_eeprom_ctx ec;
  440. struct ath_hw *ah = ah = sc->sc_ah;
  441. int err;
  442. /* try to load the EEPROM content asynchronously */
  443. init_completion(&ec.complete);
  444. ec.ah = sc->sc_ah;
  445. err = request_firmware_nowait(THIS_MODULE, 1, name, sc->dev, GFP_KERNEL,
  446. &ec, ath9k_eeprom_request_cb);
  447. if (err < 0) {
  448. ath_err(ath9k_hw_common(ah),
  449. "EEPROM request failed\n");
  450. return err;
  451. }
  452. wait_for_completion(&ec.complete);
  453. if (!ah->eeprom_blob) {
  454. ath_err(ath9k_hw_common(ah),
  455. "Unable to load EEPROM file %s\n", name);
  456. return -EINVAL;
  457. }
  458. return 0;
  459. }
  460. static void ath9k_eeprom_release(struct ath_softc *sc)
  461. {
  462. release_firmware(sc->sc_ah->eeprom_blob);
  463. }
  464. static int ath9k_init_softc(u16 devid, struct ath_softc *sc,
  465. const struct ath_bus_ops *bus_ops)
  466. {
  467. struct ath9k_platform_data *pdata = sc->dev->platform_data;
  468. struct ath_hw *ah = NULL;
  469. struct ath_common *common;
  470. int ret = 0, i;
  471. int csz = 0;
  472. ah = devm_kzalloc(sc->dev, sizeof(struct ath_hw), GFP_KERNEL);
  473. if (!ah)
  474. return -ENOMEM;
  475. ah->dev = sc->dev;
  476. ah->hw = sc->hw;
  477. ah->hw_version.devid = devid;
  478. ah->reg_ops.read = ath9k_ioread32;
  479. ah->reg_ops.write = ath9k_iowrite32;
  480. ah->reg_ops.rmw = ath9k_reg_rmw;
  481. atomic_set(&ah->intr_ref_cnt, -1);
  482. sc->sc_ah = ah;
  483. sc->dfs_detector = dfs_pattern_detector_init(NL80211_DFS_UNSET);
  484. if (!pdata) {
  485. ah->ah_flags |= AH_USE_EEPROM;
  486. sc->sc_ah->led_pin = -1;
  487. } else {
  488. sc->sc_ah->gpio_mask = pdata->gpio_mask;
  489. sc->sc_ah->gpio_val = pdata->gpio_val;
  490. sc->sc_ah->led_pin = pdata->led_pin;
  491. ah->is_clk_25mhz = pdata->is_clk_25mhz;
  492. ah->get_mac_revision = pdata->get_mac_revision;
  493. ah->external_reset = pdata->external_reset;
  494. }
  495. common = ath9k_hw_common(ah);
  496. common->ops = &ah->reg_ops;
  497. common->bus_ops = bus_ops;
  498. common->ah = ah;
  499. common->hw = sc->hw;
  500. common->priv = sc;
  501. common->debug_mask = ath9k_debug;
  502. common->btcoex_enabled = ath9k_btcoex_enable == 1;
  503. common->disable_ani = false;
  504. /*
  505. * Enable Antenna diversity only when BTCOEX is disabled
  506. * and the user manually requests the feature.
  507. */
  508. if (!common->btcoex_enabled && ath9k_enable_diversity)
  509. common->antenna_diversity = 1;
  510. spin_lock_init(&common->cc_lock);
  511. spin_lock_init(&sc->sc_serial_rw);
  512. spin_lock_init(&sc->sc_pm_lock);
  513. mutex_init(&sc->mutex);
  514. #ifdef CONFIG_ATH9K_MAC_DEBUG
  515. spin_lock_init(&sc->debug.samp_lock);
  516. #endif
  517. tasklet_init(&sc->intr_tq, ath9k_tasklet, (unsigned long)sc);
  518. tasklet_init(&sc->bcon_tasklet, ath9k_beacon_tasklet,
  519. (unsigned long)sc);
  520. INIT_WORK(&sc->hw_reset_work, ath_reset_work);
  521. INIT_WORK(&sc->hw_check_work, ath_hw_check);
  522. INIT_WORK(&sc->paprd_work, ath_paprd_calibrate);
  523. INIT_DELAYED_WORK(&sc->hw_pll_work, ath_hw_pll_work);
  524. setup_timer(&sc->rx_poll_timer, ath_rx_poll, (unsigned long)sc);
  525. /*
  526. * Cache line size is used to size and align various
  527. * structures used to communicate with the hardware.
  528. */
  529. ath_read_cachesize(common, &csz);
  530. common->cachelsz = csz << 2; /* convert to bytes */
  531. if (pdata && pdata->eeprom_name) {
  532. ret = ath9k_eeprom_request(sc, pdata->eeprom_name);
  533. if (ret)
  534. return ret;
  535. }
  536. /* Initializes the hardware for all supported chipsets */
  537. ret = ath9k_hw_init(ah);
  538. if (ret)
  539. goto err_hw;
  540. if (pdata && pdata->macaddr)
  541. memcpy(common->macaddr, pdata->macaddr, ETH_ALEN);
  542. ret = ath9k_init_queues(sc);
  543. if (ret)
  544. goto err_queues;
  545. ret = ath9k_init_btcoex(sc);
  546. if (ret)
  547. goto err_btcoex;
  548. ret = ath9k_init_channels_rates(sc);
  549. if (ret)
  550. goto err_btcoex;
  551. ath9k_cmn_init_crypto(sc->sc_ah);
  552. ath9k_init_misc(sc);
  553. ath_fill_led_pin(sc);
  554. if (common->bus_ops->aspm_init)
  555. common->bus_ops->aspm_init(common);
  556. return 0;
  557. err_btcoex:
  558. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  559. if (ATH_TXQ_SETUP(sc, i))
  560. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  561. err_queues:
  562. ath9k_hw_deinit(ah);
  563. err_hw:
  564. ath9k_eeprom_release(sc);
  565. return ret;
  566. }
  567. static void ath9k_init_band_txpower(struct ath_softc *sc, int band)
  568. {
  569. struct ieee80211_supported_band *sband;
  570. struct ieee80211_channel *chan;
  571. struct ath_hw *ah = sc->sc_ah;
  572. int i;
  573. sband = &sc->sbands[band];
  574. for (i = 0; i < sband->n_channels; i++) {
  575. chan = &sband->channels[i];
  576. ah->curchan = &ah->channels[chan->hw_value];
  577. ath9k_cmn_update_ichannel(ah->curchan, chan, NL80211_CHAN_HT20);
  578. ath9k_hw_set_txpowerlimit(ah, MAX_RATE_POWER, true);
  579. }
  580. }
  581. static void ath9k_init_txpower_limits(struct ath_softc *sc)
  582. {
  583. struct ath_hw *ah = sc->sc_ah;
  584. struct ath9k_channel *curchan = ah->curchan;
  585. if (ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  586. ath9k_init_band_txpower(sc, IEEE80211_BAND_2GHZ);
  587. if (ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  588. ath9k_init_band_txpower(sc, IEEE80211_BAND_5GHZ);
  589. ah->curchan = curchan;
  590. }
  591. void ath9k_reload_chainmask_settings(struct ath_softc *sc)
  592. {
  593. if (!(sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT))
  594. return;
  595. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  596. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_2GHZ].ht_cap);
  597. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  598. setup_ht_cap(sc, &sc->sbands[IEEE80211_BAND_5GHZ].ht_cap);
  599. }
  600. static const struct ieee80211_iface_limit if_limits[] = {
  601. { .max = 2048, .types = BIT(NL80211_IFTYPE_STATION) |
  602. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  603. BIT(NL80211_IFTYPE_WDS) },
  604. { .max = 8, .types =
  605. #ifdef CONFIG_MAC80211_MESH
  606. BIT(NL80211_IFTYPE_MESH_POINT) |
  607. #endif
  608. BIT(NL80211_IFTYPE_AP) |
  609. BIT(NL80211_IFTYPE_P2P_GO) },
  610. };
  611. static const struct ieee80211_iface_combination if_comb = {
  612. .limits = if_limits,
  613. .n_limits = ARRAY_SIZE(if_limits),
  614. .max_interfaces = 2048,
  615. .num_different_channels = 1,
  616. .beacon_int_infra_match = true,
  617. };
  618. void ath9k_set_hw_capab(struct ath_softc *sc, struct ieee80211_hw *hw)
  619. {
  620. struct ath_hw *ah = sc->sc_ah;
  621. struct ath_common *common = ath9k_hw_common(ah);
  622. hw->flags = IEEE80211_HW_RX_INCLUDES_FCS |
  623. IEEE80211_HW_HOST_BROADCAST_PS_BUFFERING |
  624. IEEE80211_HW_SIGNAL_DBM |
  625. IEEE80211_HW_SUPPORTS_PS |
  626. IEEE80211_HW_PS_NULLFUNC_STACK |
  627. IEEE80211_HW_SPECTRUM_MGMT |
  628. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  629. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_HT)
  630. hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
  631. if (AR_SREV_9160_10_OR_LATER(sc->sc_ah) || ath9k_modparam_nohwcrypt)
  632. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  633. hw->wiphy->interface_modes =
  634. BIT(NL80211_IFTYPE_P2P_GO) |
  635. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  636. BIT(NL80211_IFTYPE_AP) |
  637. BIT(NL80211_IFTYPE_WDS) |
  638. BIT(NL80211_IFTYPE_STATION) |
  639. BIT(NL80211_IFTYPE_ADHOC) |
  640. BIT(NL80211_IFTYPE_MESH_POINT);
  641. hw->wiphy->iface_combinations = &if_comb;
  642. hw->wiphy->n_iface_combinations = 1;
  643. if (AR_SREV_5416(sc->sc_ah))
  644. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  645. hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
  646. hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
  647. hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
  648. #ifdef CONFIG_PM_SLEEP
  649. if ((ah->caps.hw_caps & ATH9K_HW_WOW_DEVICE_CAPABLE) &&
  650. device_can_wakeup(sc->dev)) {
  651. hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  652. WIPHY_WOWLAN_DISCONNECT;
  653. hw->wiphy->wowlan.n_patterns = MAX_NUM_USER_PATTERN;
  654. hw->wiphy->wowlan.pattern_min_len = 1;
  655. hw->wiphy->wowlan.pattern_max_len = MAX_PATTERN_SIZE;
  656. }
  657. atomic_set(&sc->wow_sleep_proc_intr, -1);
  658. atomic_set(&sc->wow_got_bmiss_intr, -1);
  659. #endif
  660. hw->queues = 4;
  661. hw->max_rates = 4;
  662. hw->channel_change_time = 5000;
  663. hw->max_listen_interval = 1;
  664. hw->max_rate_tries = 10;
  665. hw->sta_data_size = sizeof(struct ath_node);
  666. hw->vif_data_size = sizeof(struct ath_vif);
  667. hw->wiphy->available_antennas_rx = BIT(ah->caps.max_rxchains) - 1;
  668. hw->wiphy->available_antennas_tx = BIT(ah->caps.max_txchains) - 1;
  669. /* single chain devices with rx diversity */
  670. if (ah->caps.hw_caps & ATH9K_HW_CAP_ANT_DIV_COMB)
  671. hw->wiphy->available_antennas_rx = BIT(0) | BIT(1);
  672. sc->ant_rx = hw->wiphy->available_antennas_rx;
  673. sc->ant_tx = hw->wiphy->available_antennas_tx;
  674. #ifdef CONFIG_ATH9K_RATE_CONTROL
  675. hw->rate_control_algorithm = "ath9k_rate_control";
  676. #endif
  677. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_2GHZ)
  678. hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  679. &sc->sbands[IEEE80211_BAND_2GHZ];
  680. if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_5GHZ)
  681. hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  682. &sc->sbands[IEEE80211_BAND_5GHZ];
  683. ath9k_reload_chainmask_settings(sc);
  684. SET_IEEE80211_PERM_ADDR(hw, common->macaddr);
  685. }
  686. int ath9k_init_device(u16 devid, struct ath_softc *sc,
  687. const struct ath_bus_ops *bus_ops)
  688. {
  689. struct ieee80211_hw *hw = sc->hw;
  690. struct ath_common *common;
  691. struct ath_hw *ah;
  692. int error = 0;
  693. struct ath_regulatory *reg;
  694. /* Bring up device */
  695. error = ath9k_init_softc(devid, sc, bus_ops);
  696. if (error)
  697. return error;
  698. ah = sc->sc_ah;
  699. common = ath9k_hw_common(ah);
  700. ath9k_set_hw_capab(sc, hw);
  701. /* Initialize regulatory */
  702. error = ath_regd_init(&common->regulatory, sc->hw->wiphy,
  703. ath9k_reg_notifier);
  704. if (error)
  705. goto deinit;
  706. reg = &common->regulatory;
  707. /* Setup TX DMA */
  708. error = ath_tx_init(sc, ATH_TXBUF);
  709. if (error != 0)
  710. goto deinit;
  711. /* Setup RX DMA */
  712. error = ath_rx_init(sc, ATH_RXBUF);
  713. if (error != 0)
  714. goto deinit;
  715. ath9k_init_txpower_limits(sc);
  716. #ifdef CONFIG_MAC80211_LEDS
  717. /* must be initialized before ieee80211_register_hw */
  718. sc->led_cdev.default_trigger = ieee80211_create_tpt_led_trigger(sc->hw,
  719. IEEE80211_TPT_LEDTRIG_FL_RADIO, ath9k_tpt_blink,
  720. ARRAY_SIZE(ath9k_tpt_blink));
  721. #endif
  722. /* Register with mac80211 */
  723. error = ieee80211_register_hw(hw);
  724. if (error)
  725. goto rx_cleanup;
  726. error = ath9k_init_debug(ah);
  727. if (error) {
  728. ath_err(common, "Unable to create debugfs files\n");
  729. goto unregister;
  730. }
  731. /* Handle world regulatory */
  732. if (!ath_is_world_regd(reg)) {
  733. error = regulatory_hint(hw->wiphy, reg->alpha2);
  734. if (error)
  735. goto unregister;
  736. }
  737. ath_init_leds(sc);
  738. ath_start_rfkill_poll(sc);
  739. return 0;
  740. unregister:
  741. ieee80211_unregister_hw(hw);
  742. rx_cleanup:
  743. ath_rx_cleanup(sc);
  744. deinit:
  745. ath9k_deinit_softc(sc);
  746. return error;
  747. }
  748. /*****************************/
  749. /* De-Initialization */
  750. /*****************************/
  751. static void ath9k_deinit_softc(struct ath_softc *sc)
  752. {
  753. int i = 0;
  754. ath9k_deinit_btcoex(sc);
  755. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  756. if (ATH_TXQ_SETUP(sc, i))
  757. ath_tx_cleanupq(sc, &sc->tx.txq[i]);
  758. ath9k_hw_deinit(sc->sc_ah);
  759. if (sc->dfs_detector != NULL)
  760. sc->dfs_detector->exit(sc->dfs_detector);
  761. ath9k_eeprom_release(sc);
  762. if (config_enabled(CONFIG_ATH9K_DEBUGFS) && sc->rfs_chan_spec_scan) {
  763. relay_close(sc->rfs_chan_spec_scan);
  764. sc->rfs_chan_spec_scan = NULL;
  765. }
  766. }
  767. void ath9k_deinit_device(struct ath_softc *sc)
  768. {
  769. struct ieee80211_hw *hw = sc->hw;
  770. ath9k_ps_wakeup(sc);
  771. wiphy_rfkill_stop_polling(sc->hw->wiphy);
  772. ath_deinit_leds(sc);
  773. ath9k_ps_restore(sc);
  774. ieee80211_unregister_hw(hw);
  775. ath_rx_cleanup(sc);
  776. ath9k_deinit_softc(sc);
  777. }
  778. /************************/
  779. /* Module Hooks */
  780. /************************/
  781. static int __init ath9k_init(void)
  782. {
  783. int error;
  784. /* Register rate control algorithm */
  785. error = ath_rate_control_register();
  786. if (error != 0) {
  787. pr_err("Unable to register rate control algorithm: %d\n",
  788. error);
  789. goto err_out;
  790. }
  791. error = ath_pci_init();
  792. if (error < 0) {
  793. pr_err("No PCI devices found, driver not installed\n");
  794. error = -ENODEV;
  795. goto err_rate_unregister;
  796. }
  797. error = ath_ahb_init();
  798. if (error < 0) {
  799. error = -ENODEV;
  800. goto err_pci_exit;
  801. }
  802. return 0;
  803. err_pci_exit:
  804. ath_pci_exit();
  805. err_rate_unregister:
  806. ath_rate_control_unregister();
  807. err_out:
  808. return error;
  809. }
  810. module_init(ath9k_init);
  811. static void __exit ath9k_exit(void)
  812. {
  813. is_ath9k_unloaded = true;
  814. ath_ahb_exit();
  815. ath_pci_exit();
  816. ath_rate_control_unregister();
  817. pr_info("%s: Driver unloaded\n", dev_info);
  818. }
  819. module_exit(ath9k_exit);